aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/DocBook/videobook.tmpl9
-rw-r--r--Documentation/RCU/RTFP.txt210
-rw-r--r--Documentation/RCU/rcu.txt19
-rw-r--r--Documentation/RCU/torture.txt11
-rw-r--r--Documentation/cpu-hotplug.txt11
-rw-r--r--Documentation/dvb/bt8xx.txt12
-rw-r--r--Documentation/feature-removal-schedule.txt10
-rw-r--r--Documentation/filesystems/ocfs2.txt16
-rw-r--r--Documentation/ioctl-number.txt1
-rw-r--r--Documentation/video4linux/CARDLIST.cx238854
-rw-r--r--Documentation/video4linux/CARDLIST.cx881
-rw-r--r--Documentation/video4linux/CARDLIST.em28xx17
-rw-r--r--Documentation/video4linux/CARDLIST.ivtv6
-rw-r--r--Documentation/video4linux/CARDLIST.saa713417
-rw-r--r--Documentation/video4linux/CARDLIST.tuner5
-rw-r--r--Documentation/video4linux/CARDLIST.usbvision1
-rw-r--r--Documentation/video4linux/extract_xc3028.pl926
-rw-r--r--Documentation/video4linux/sn9c102.txt1
-rw-r--r--MAINTAINERS9
-rw-r--r--arch/arm/kernel/time.c11
-rw-r--r--arch/ia64/kernel/setup.c4
-rw-r--r--arch/ia64/kernel/time.c27
-rw-r--r--arch/ia64/sn/kernel/setup.c11
-rw-r--r--arch/mips/kernel/mips-mt-fpaff.c10
-rw-r--r--arch/powerpc/platforms/pseries/hotplug-cpu.c8
-rw-r--r--arch/powerpc/platforms/pseries/rtasd.c8
-rw-r--r--arch/x86/kernel/cpu/mtrr/main.c8
-rw-r--r--arch/x86/kernel/entry_64.S6
-rw-r--r--arch/x86/kernel/microcode.c16
-rw-r--r--arch/x86/kernel/signal_32.c3
-rw-r--r--arch/x86/kernel/signal_64.c3
-rw-r--r--arch/x86/kernel/stacktrace.c27
-rw-r--r--drivers/ata/Kconfig9
-rw-r--r--drivers/ata/Makefile1
-rw-r--r--drivers/ata/ahci.c72
-rw-r--r--drivers/ata/ata_generic.c51
-rw-r--r--drivers/ata/ata_piix.c393
-rw-r--r--drivers/ata/libata-acpi.c117
-rw-r--r--drivers/ata/libata-core.c805
-rw-r--r--drivers/ata/libata-eh.c299
-rw-r--r--drivers/ata/libata-scsi.c40
-rw-r--r--drivers/ata/libata-sff.c199
-rw-r--r--drivers/ata/libata.h6
-rw-r--r--drivers/ata/pata_acpi.c67
-rw-r--r--drivers/ata/pata_ali.c2
-rw-r--r--drivers/ata/pata_amd.c128
-rw-r--r--drivers/ata/pata_bf54x.c41
-rw-r--r--drivers/ata/pata_cs5520.c2
-rw-r--r--drivers/ata/pata_hpt37x.c7
-rw-r--r--drivers/ata/pata_icside.c3
-rw-r--r--drivers/ata/pata_it821x.c35
-rw-r--r--drivers/ata/pata_ixp4xx_cf.c26
-rw-r--r--drivers/ata/pata_legacy.c912
-rw-r--r--drivers/ata/pata_mpc52xx.c2
-rw-r--r--drivers/ata/pata_ninja32.c214
-rw-r--r--drivers/ata/pata_pcmcia.c101
-rw-r--r--drivers/ata/pata_pdc2027x.c2
-rw-r--r--drivers/ata/pata_pdc202xx_old.c5
-rw-r--r--drivers/ata/pata_qdi.c30
-rw-r--r--drivers/ata/pata_scc.c30
-rw-r--r--drivers/ata/pata_serverworks.c9
-rw-r--r--drivers/ata/pata_via.c3
-rw-r--r--drivers/ata/pata_winbond.c30
-rw-r--r--drivers/ata/pdc_adma.c5
-rw-r--r--drivers/ata/sata_fsl.c5
-rw-r--r--drivers/ata/sata_inic162x.c2
-rw-r--r--drivers/ata/sata_mv.c3
-rw-r--r--drivers/ata/sata_nv.c25
-rw-r--r--drivers/ata/sata_promise.c98
-rw-r--r--drivers/ata/sata_promise.h2
-rw-r--r--drivers/ata/sata_qstor.c15
-rw-r--r--drivers/ata/sata_sil.c10
-rw-r--r--drivers/ata/sata_sil24.c30
-rw-r--r--drivers/ata/sata_sx4.c15
-rw-r--r--drivers/ide/Kconfig12
-rw-r--r--drivers/ide/arm/icside.c2
-rw-r--r--drivers/ide/cris/ide-cris.c2
-rw-r--r--drivers/ide/ide-acpi.c16
-rw-r--r--drivers/ide/ide-cd.c12
-rw-r--r--drivers/ide/ide-disk.c418
-rw-r--r--drivers/ide/ide-dma.c36
-rw-r--r--drivers/ide/ide-floppy.c123
-rw-r--r--drivers/ide/ide-io.c293
-rw-r--r--drivers/ide/ide-iops.c50
-rw-r--r--drivers/ide/ide-lib.c207
-rw-r--r--drivers/ide/ide-probe.c32
-rw-r--r--drivers/ide/ide-tape.c124
-rw-r--r--drivers/ide/ide-taskfile.c492
-rw-r--r--drivers/ide/ide.c6
-rw-r--r--drivers/ide/mips/au1xxx-ide.c2
-rw-r--r--drivers/ide/pci/aec62xx.c13
-rw-r--r--drivers/ide/pci/alim15x3.c3
-rw-r--r--drivers/ide/pci/amd74xx.c1
-rw-r--r--drivers/ide/pci/atiixp.c3
-rw-r--r--drivers/ide/pci/cmd64x.c10
-rw-r--r--drivers/ide/pci/cs5520.c1
-rw-r--r--drivers/ide/pci/cs5530.c2
-rw-r--r--drivers/ide/pci/cs5535.c2
-rw-r--r--drivers/ide/pci/hpt34x.c12
-rw-r--r--drivers/ide/pci/hpt366.c194
-rw-r--r--drivers/ide/pci/it8213.c21
-rw-r--r--drivers/ide/pci/pdc202xx_new.c64
-rw-r--r--drivers/ide/pci/pdc202xx_old.c24
-rw-r--r--drivers/ide/pci/piix.c17
-rw-r--r--drivers/ide/pci/sc1200.c172
-rw-r--r--drivers/ide/pci/scc_pata.c14
-rw-r--r--drivers/ide/pci/serverworks.c17
-rw-r--r--drivers/ide/pci/sgiioc4.c1
-rw-r--r--drivers/ide/pci/siimage.c29
-rw-r--r--drivers/ide/pci/sis5513.c99
-rw-r--r--drivers/ide/pci/sl82c105.c40
-rw-r--r--drivers/ide/pci/slc90e66.c14
-rw-r--r--drivers/ide/pci/tc86c001.c3
-rw-r--r--drivers/ide/pci/triflex.c2
-rw-r--r--drivers/ide/pci/via82cxxx.c1
-rw-r--r--drivers/ide/ppc/pmac.c51
-rw-r--r--drivers/infiniband/core/cm.c306
-rw-r--r--drivers/infiniband/core/cma.c60
-rw-r--r--drivers/infiniband/core/fmr_pool.c33
-rw-r--r--drivers/infiniband/core/mad.c26
-rw-r--r--drivers/infiniband/core/mad_priv.h3
-rw-r--r--drivers/infiniband/core/mad_rmpp.c2
-rw-r--r--drivers/infiniband/core/multicast.c55
-rw-r--r--drivers/infiniband/core/smi.h18
-rw-r--r--drivers/infiniband/core/ucm.c37
-rw-r--r--drivers/infiniband/core/ucma.c92
-rw-r--r--drivers/infiniband/core/user_mad.c115
-rw-r--r--drivers/infiniband/hw/cxgb3/cxio_hal.c4
-rw-r--r--drivers/infiniband/hw/cxgb3/cxio_wr.h5
-rw-r--r--drivers/infiniband/hw/cxgb3/iwch_cm.c4
-rw-r--r--drivers/infiniband/hw/cxgb3/iwch_mem.c7
-rw-r--r--drivers/infiniband/hw/cxgb3/iwch_provider.c7
-rw-r--r--drivers/infiniband/hw/cxgb3/iwch_qp.c29
-rw-r--r--drivers/infiniband/hw/ehca/ehca_av.c2
-rw-r--r--drivers/infiniband/hw/ehca/ehca_classes.h23
-rw-r--r--drivers/infiniband/hw/ehca/ehca_cq.c2
-rw-r--r--drivers/infiniband/hw/ehca/ehca_irq.c38
-rw-r--r--drivers/infiniband/hw/ehca/ehca_iverbs.h2
-rw-r--r--drivers/infiniband/hw/ehca/ehca_main.c15
-rw-r--r--drivers/infiniband/hw/ehca/ehca_qp.c180
-rw-r--r--drivers/infiniband/hw/ehca/ehca_reqs.c112
-rw-r--r--drivers/infiniband/hw/ehca/ehca_sqp.c6
-rw-r--r--drivers/infiniband/hw/ipath/ipath_common.h35
-rw-r--r--drivers/infiniband/hw/ipath/ipath_cq.c2
-rw-r--r--drivers/infiniband/hw/ipath/ipath_debug.h4
-rw-r--r--drivers/infiniband/hw/ipath/ipath_driver.c180
-rw-r--r--drivers/infiniband/hw/ipath/ipath_eeprom.c23
-rw-r--r--drivers/infiniband/hw/ipath/ipath_file_ops.c94
-rw-r--r--drivers/infiniband/hw/ipath/ipath_fs.c14
-rw-r--r--drivers/infiniband/hw/ipath/ipath_iba6110.c395
-rw-r--r--drivers/infiniband/hw/ipath/ipath_iba6120.c439
-rw-r--r--drivers/infiniband/hw/ipath/ipath_init_chip.c67
-rw-r--r--drivers/infiniband/hw/ipath/ipath_intr.c81
-rw-r--r--drivers/infiniband/hw/ipath/ipath_kernel.h201
-rw-r--r--drivers/infiniband/hw/ipath/ipath_keys.c5
-rw-r--r--drivers/infiniband/hw/ipath/ipath_mad.c123
-rw-r--r--drivers/infiniband/hw/ipath/ipath_qp.c6
-rw-r--r--drivers/infiniband/hw/ipath/ipath_rc.c18
-rw-r--r--drivers/infiniband/hw/ipath/ipath_registers.h33
-rw-r--r--drivers/infiniband/hw/ipath/ipath_ruc.c13
-rw-r--r--drivers/infiniband/hw/ipath/ipath_srq.c4
-rw-r--r--drivers/infiniband/hw/ipath/ipath_stats.c24
-rw-r--r--drivers/infiniband/hw/ipath/ipath_sysfs.c364
-rw-r--r--drivers/infiniband/hw/ipath/ipath_ud.c3
-rw-r--r--drivers/infiniband/hw/ipath/ipath_verbs.c55
-rw-r--r--drivers/infiniband/hw/ipath/ipath_verbs.h12
-rw-r--r--drivers/infiniband/hw/mlx4/cq.c9
-rw-r--r--drivers/infiniband/hw/mthca/mthca_dev.h13
-rw-r--r--drivers/infiniband/hw/mthca/mthca_eq.c6
-rw-r--r--drivers/infiniband/hw/mthca/mthca_main.c40
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib.h184
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_cm.c376
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_fs.c4
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_ib.c8
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_main.c60
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_multicast.c8
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_verbs.c18
-rw-r--r--drivers/infiniband/ulp/iser/Kconfig4
-rw-r--r--drivers/infiniband/ulp/iser/iscsi_iser.c1
-rw-r--r--drivers/infiniband/ulp/iser/iser_initiator.c2
-rw-r--r--drivers/infiniband/ulp/iser/iser_verbs.c8
-rw-r--r--drivers/infiniband/ulp/srp/ib_srp.c131
-rw-r--r--drivers/infiniband/ulp/srp/ib_srp.h5
-rw-r--r--drivers/lguest/x86/core.c8
-rw-r--r--drivers/media/Kconfig22
-rw-r--r--drivers/media/common/Kconfig2
-rw-r--r--drivers/media/common/ir-functions.c3
-rw-r--r--drivers/media/common/ir-keymaps.c146
-rw-r--r--drivers/media/common/saa7146_fops.c9
-rw-r--r--drivers/media/common/saa7146_vbi.c10
-rw-r--r--drivers/media/common/saa7146_video.c8
-rw-r--r--drivers/media/dvb/b2c2/flexcop.c1
-rw-r--r--drivers/media/dvb/bt8xx/bt878.c76
-rw-r--r--drivers/media/dvb/bt8xx/bt878.h6
-rw-r--r--drivers/media/dvb/bt8xx/dst.c19
-rw-r--r--drivers/media/dvb/bt8xx/dst_common.h5
-rw-r--r--drivers/media/dvb/dvb-core/dvb_frontend.c6
-rw-r--r--drivers/media/dvb/dvb-core/dvb_frontend.h43
-rw-r--r--drivers/media/dvb/dvb-core/dvb_ringbuffer.c5
-rw-r--r--drivers/media/dvb/dvb-usb/af9005.c7
-rw-r--r--drivers/media/dvb/dvb-usb/au6610.c6
-rw-r--r--drivers/media/dvb/dvb-usb/cxusb.c436
-rw-r--r--drivers/media/dvb/dvb-usb/cxusb.h7
-rw-r--r--drivers/media/dvb/dvb-usb/dib0700_core.c5
-rw-r--r--drivers/media/dvb/dvb-usb/dib0700_devices.c149
-rw-r--r--drivers/media/dvb/dvb-usb/digitv.c3
-rw-r--r--drivers/media/dvb/dvb-usb/digitv.h3
-rw-r--r--drivers/media/dvb/dvb-usb/dvb-usb-ids.h18
-rw-r--r--drivers/media/dvb/dvb-usb/gl861.c6
-rw-r--r--drivers/media/dvb/dvb-usb/gp8psk.c16
-rw-r--r--drivers/media/dvb/dvb-usb/gp8psk.h1
-rw-r--r--drivers/media/dvb/dvb-usb/opera1.c6
-rw-r--r--drivers/media/dvb/dvb-usb/opera1.h9
-rw-r--r--drivers/media/dvb/dvb-usb/vp702x.c15
-rw-r--r--drivers/media/dvb/dvb-usb/vp702x.h2
-rw-r--r--drivers/media/dvb/dvb-usb/vp7045.c5
-rw-r--r--drivers/media/dvb/dvb-usb/vp7045.h5
-rw-r--r--drivers/media/dvb/frontends/Kconfig16
-rw-r--r--drivers/media/dvb/frontends/Makefile5
-rw-r--r--drivers/media/dvb/frontends/dib0070.c13
-rw-r--r--drivers/media/dvb/frontends/dib3000mc.c10
-rw-r--r--drivers/media/dvb/frontends/dib7000m.c9
-rw-r--r--drivers/media/dvb/frontends/dib7000p.c18
-rw-r--r--drivers/media/dvb/frontends/dibx000_common.h5
-rw-r--r--drivers/media/dvb/frontends/mt2266.c204
-rw-r--r--drivers/media/dvb/frontends/mt312.c257
-rw-r--r--drivers/media/dvb/frontends/mt312.h15
-rw-r--r--drivers/media/dvb/frontends/mt352.c8
-rw-r--r--drivers/media/dvb/frontends/or51132.c6
-rw-r--r--drivers/media/dvb/frontends/or51211.c6
-rw-r--r--drivers/media/dvb/frontends/s5h1409.c97
-rw-r--r--drivers/media/dvb/frontends/s5h1409.h11
-rw-r--r--drivers/media/dvb/frontends/tda18271-common.c653
-rw-r--r--drivers/media/dvb/frontends/tda18271-fe.c1225
-rw-r--r--drivers/media/dvb/frontends/tda18271-priv.h212
-rw-r--r--drivers/media/dvb/frontends/tda18271-tables.c1285
-rw-r--r--drivers/media/dvb/frontends/tda18271.h78
-rw-r--r--drivers/media/dvb/frontends/tda827x.c367
-rw-r--r--drivers/media/dvb/frontends/tda827x.h7
-rw-r--r--drivers/media/dvb/frontends/ves1820.c4
-rw-r--r--drivers/media/dvb/frontends/xc5000.c964
-rw-r--r--drivers/media/dvb/frontends/xc5000.h62
-rw-r--r--drivers/media/dvb/frontends/xc5000_priv.h36
-rw-r--r--drivers/media/dvb/frontends/zl10353.c327
-rw-r--r--drivers/media/dvb/frontends/zl10353.h9
-rw-r--r--drivers/media/dvb/frontends/zl10353_priv.h23
-rw-r--r--drivers/media/dvb/ttpci/Kconfig37
-rw-r--r--drivers/media/dvb/ttpci/Makefile12
-rw-r--r--drivers/media/dvb/ttpci/av7110.c3
-rw-r--r--drivers/media/dvb/ttpci/av7110.h7
-rw-r--r--drivers/media/dvb/ttpci/av7110_av.c16
-rw-r--r--drivers/media/dvb/ttpci/av7110_av.h3
-rw-r--r--drivers/media/dvb/ttpci/av7110_v4l.c4
-rw-r--r--drivers/media/radio/Kconfig10
-rw-r--r--drivers/media/radio/Makefile1
-rw-r--r--drivers/media/radio/dsbr100.c18
-rw-r--r--drivers/media/radio/radio-gemtek.c6
-rw-r--r--drivers/media/radio/radio-maestro.c2
-rw-r--r--drivers/media/radio/radio-sf16fmi.c2
-rw-r--r--drivers/media/radio/radio-sf16fmr2.c3
-rw-r--r--drivers/media/radio/radio-si470x.c1432
-rw-r--r--drivers/media/video/Kconfig52
-rw-r--r--drivers/media/video/Makefile10
-rw-r--r--drivers/media/video/bt8xx/Kconfig2
-rw-r--r--drivers/media/video/bt8xx/Makefile2
-rw-r--r--drivers/media/video/bt8xx/bttv-audio-hook.c382
-rw-r--r--drivers/media/video/bt8xx/bttv-audio-hook.h23
-rw-r--r--drivers/media/video/bt8xx/bttv-cards.c444
-rw-r--r--drivers/media/video/bt8xx/bttv-driver.c2374
-rw-r--r--drivers/media/video/bt8xx/bttv-input.c5
-rw-r--r--drivers/media/video/bt8xx/bttv-risc.c14
-rw-r--r--drivers/media/video/bt8xx/bttv-vbi.c58
-rw-r--r--drivers/media/video/bt8xx/bttv.h5
-rw-r--r--drivers/media/video/bt8xx/bttvp.h20
-rw-r--r--drivers/media/video/bw-qcam.c10
-rw-r--r--drivers/media/video/cs5345.c168
-rw-r--r--drivers/media/video/cs53l32a.c78
-rw-r--r--drivers/media/video/cx2341x.c313
-rw-r--r--drivers/media/video/cx23885/Kconfig4
-rw-r--r--drivers/media/video/cx23885/Makefile2
-rw-r--r--drivers/media/video/cx23885/cx23885-cards.c135
-rw-r--r--drivers/media/video/cx23885/cx23885-core.c296
-rw-r--r--drivers/media/video/cx23885/cx23885-dvb.c179
-rw-r--r--drivers/media/video/cx23885/cx23885-i2c.c98
-rw-r--r--drivers/media/video/cx23885/cx23885-reg.h13
-rw-r--r--drivers/media/video/cx23885/cx23885-vbi.c258
-rw-r--r--drivers/media/video/cx23885/cx23885-video.c1557
-rw-r--r--drivers/media/video/cx23885/cx23885.h168
-rw-r--r--drivers/media/video/cx25840/cx25840-audio.c123
-rw-r--r--drivers/media/video/cx25840/cx25840-core.c695
-rw-r--r--drivers/media/video/cx25840/cx25840-core.h1
-rw-r--r--drivers/media/video/cx25840/cx25840-firmware.c5
-rw-r--r--drivers/media/video/cx25840/cx25840-vbi.c2
-rw-r--r--drivers/media/video/cx88/Kconfig1
-rw-r--r--drivers/media/video/cx88/cx88-alsa.c2
-rw-r--r--drivers/media/video/cx88/cx88-blackbird.c92
-rw-r--r--drivers/media/video/cx88/cx88-cards.c120
-rw-r--r--drivers/media/video/cx88/cx88-core.c4
-rw-r--r--drivers/media/video/cx88/cx88-dvb.c33
-rw-r--r--drivers/media/video/cx88/cx88-i2c.c27
-rw-r--r--drivers/media/video/cx88/cx88-input.c6
-rw-r--r--drivers/media/video/cx88/cx88-mpeg.c39
-rw-r--r--drivers/media/video/cx88/cx88-vbi.c10
-rw-r--r--drivers/media/video/cx88/cx88-video.c58
-rw-r--r--drivers/media/video/cx88/cx88.h11
-rw-r--r--drivers/media/video/em28xx/Kconfig17
-rw-r--r--drivers/media/video/em28xx/Makefile6
-rw-r--r--drivers/media/video/em28xx/em28xx-audio.c489
-rw-r--r--drivers/media/video/em28xx/em28xx-cards.c688
-rw-r--r--drivers/media/video/em28xx/em28xx-core.c88
-rw-r--r--drivers/media/video/em28xx/em28xx-i2c.c81
-rw-r--r--drivers/media/video/em28xx/em28xx-input.c52
-rw-r--r--drivers/media/video/em28xx/em28xx-video.c2622
-rw-r--r--drivers/media/video/em28xx/em28xx.h166
-rw-r--r--drivers/media/video/et61x251/et61x251_core.c157
-rw-r--r--drivers/media/video/et61x251/et61x251_sensor.h8
-rw-r--r--drivers/media/video/ir-kbd-i2c.c3
-rw-r--r--drivers/media/video/ivtv/Kconfig2
-rw-r--r--drivers/media/video/ivtv/Makefile5
-rw-r--r--drivers/media/video/ivtv/ivtv-cards.c173
-rw-r--r--drivers/media/video/ivtv/ivtv-cards.h27
-rw-r--r--drivers/media/video/ivtv/ivtv-driver.c117
-rw-r--r--drivers/media/video/ivtv/ivtv-driver.h26
-rw-r--r--drivers/media/video/ivtv/ivtv-fileops.c52
-rw-r--r--drivers/media/video/ivtv/ivtv-gpio.c24
-rw-r--r--drivers/media/video/ivtv/ivtv-i2c.c119
-rw-r--r--drivers/media/video/ivtv/ivtv-i2c.h1
-rw-r--r--drivers/media/video/ivtv/ivtv-ioctl.c127
-rw-r--r--drivers/media/video/ivtv/ivtv-irq.c95
-rw-r--r--drivers/media/video/ivtv/ivtv-mailbox.c2
-rw-r--r--drivers/media/video/ivtv/ivtv-mailbox.h2
-rw-r--r--drivers/media/video/ivtv/ivtv-routing.c25
-rw-r--r--drivers/media/video/ivtv/ivtv-streams.c25
-rw-r--r--drivers/media/video/ivtv/ivtv-version.h2
-rw-r--r--drivers/media/video/ivtv/ivtv-yuv.c1094
-rw-r--r--drivers/media/video/ivtv/ivtv-yuv.h12
-rw-r--r--drivers/media/video/ivtv/ivtvfb.c6
-rw-r--r--drivers/media/video/m52790.c168
-rw-r--r--drivers/media/video/meye.c2
-rw-r--r--drivers/media/video/msp3400-driver.c238
-rw-r--r--drivers/media/video/msp3400-kthreads.c166
-rw-r--r--drivers/media/video/mt20xx.c2
-rw-r--r--drivers/media/video/pvrusb2/Kconfig37
-rw-r--r--drivers/media/video/pvrusb2/Makefile2
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-audio.c62
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-context.c55
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-context.h5
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.c97
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-debug.h39
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-debugifc.c177
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-devattr.c217
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-devattr.h119
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-eeprom.c1
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-encoder.c75
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-encoder.h1
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h80
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-hdw.c1301
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-hdw.h144
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-i2c-core.c10
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-main.c6
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-std.c7
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-sysfs.c60
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-v4l2.c7
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-video-v4l.c55
-rw-r--r--drivers/media/video/saa7115.c80
-rw-r--r--drivers/media/video/saa7127.c148
-rw-r--r--drivers/media/video/saa7134/Kconfig13
-rw-r--r--drivers/media/video/saa7134/Makefile1
-rw-r--r--drivers/media/video/saa7134/saa7134-alsa.c16
-rw-r--r--drivers/media/video/saa7134/saa7134-cards.c614
-rw-r--r--drivers/media/video/saa7134/saa7134-core.c35
-rw-r--r--drivers/media/video/saa7134/saa7134-dvb.c19
-rw-r--r--drivers/media/video/saa7134/saa7134-empress.c293
-rw-r--r--drivers/media/video/saa7134/saa7134-i2c.c15
-rw-r--r--drivers/media/video/saa7134/saa7134-input.c63
-rw-r--r--drivers/media/video/saa7134/saa7134-oss.c1046
-rw-r--r--drivers/media/video/saa7134/saa7134-ts.c8
-rw-r--r--drivers/media/video/saa7134/saa7134-tvaudio.c79
-rw-r--r--drivers/media/video/saa7134/saa7134-vbi.c8
-rw-r--r--drivers/media/video/saa7134/saa7134-video.c1536
-rw-r--r--drivers/media/video/saa7134/saa7134.h34
-rw-r--r--drivers/media/video/sn9c102/Makefile1
-rw-r--r--drivers/media/video/sn9c102/sn9c102_core.c4
-rw-r--r--drivers/media/video/sn9c102/sn9c102_devtable.h2
-rw-r--r--drivers/media/video/sn9c102/sn9c102_mt9v111.c259
-rw-r--r--drivers/media/video/stk-sensor.c578
-rw-r--r--drivers/media/video/stk-webcam.c1465
-rw-r--r--drivers/media/video/stk-webcam.h138
-rw-r--r--drivers/media/video/tda7432.c232
-rw-r--r--drivers/media/video/tda8290.c901
-rw-r--r--drivers/media/video/tda8290.h31
-rw-r--r--drivers/media/video/tda9875.c167
-rw-r--r--drivers/media/video/tda9887.c305
-rw-r--r--drivers/media/video/tda9887.h38
-rw-r--r--drivers/media/video/tea5761.c2
-rw-r--r--drivers/media/video/tea5767.c92
-rw-r--r--drivers/media/video/tea5767.h19
-rw-r--r--drivers/media/video/tlv320aic23b.c134
-rw-r--r--drivers/media/video/tuner-core.c748
-rw-r--r--drivers/media/video/tuner-driver.h99
-rw-r--r--drivers/media/video/tuner-i2c.h57
-rw-r--r--drivers/media/video/tuner-simple.c17
-rw-r--r--drivers/media/video/tuner-types.c12
-rw-r--r--drivers/media/video/tuner-xc2028-types.h128
-rw-r--r--drivers/media/video/tuner-xc2028.c1213
-rw-r--r--drivers/media/video/tuner-xc2028.h63
-rw-r--r--drivers/media/video/tvaudio.c504
-rw-r--r--drivers/media/video/tveeprom.c504
-rw-r--r--drivers/media/video/upd64031a.c111
-rw-r--r--drivers/media/video/upd64083.c103
-rw-r--r--drivers/media/video/usbvision/usbvision-cards.c18
-rw-r--r--drivers/media/video/usbvision/usbvision-cards.h1
-rw-r--r--drivers/media/video/usbvision/usbvision-core.c330
-rw-r--r--drivers/media/video/usbvision/usbvision-video.c60
-rw-r--r--drivers/media/video/usbvision/usbvision.h8
-rw-r--r--drivers/media/video/v4l2-common.c32
-rw-r--r--drivers/media/video/v4l2-int-device.c6
-rw-r--r--drivers/media/video/videobuf-core.c357
-rw-r--r--drivers/media/video/videobuf-dma-sg.c23
-rw-r--r--drivers/media/video/videobuf-dvb.c2
-rw-r--r--drivers/media/video/videobuf-vmalloc.c2
-rw-r--r--drivers/media/video/videodev.c8
-rw-r--r--drivers/media/video/vivi.c706
-rw-r--r--drivers/media/video/vp27smpx.c113
-rw-r--r--drivers/media/video/wm8739.c137
-rw-r--r--drivers/media/video/wm8775.c140
-rw-r--r--drivers/media/video/zr364xx.c2
-rw-r--r--drivers/net/mlx4/fw.c2
-rw-r--r--drivers/s390/char/sclp_config.c4
-rw-r--r--drivers/scsi/ide-scsi.c75
-rw-r--r--drivers/scsi/ipr.c9
-rw-r--r--drivers/scsi/libsas/sas_ata.c14
-rw-r--r--fs/Kconfig15
-rw-r--r--fs/configfs/dir.c5
-rw-r--r--fs/configfs/file.c2
-rw-r--r--fs/ocfs2/Makefile5
-rw-r--r--fs/ocfs2/alloc.c8
-rw-r--r--fs/ocfs2/aops.c137
-rw-r--r--fs/ocfs2/buffer_head_io.c65
-rw-r--r--fs/ocfs2/buffer_head_io.h2
-rw-r--r--fs/ocfs2/cluster/heartbeat.h2
-rw-r--r--fs/ocfs2/cluster/tcp.h4
-rw-r--r--fs/ocfs2/cluster/tcp_internal.h8
-rw-r--r--fs/ocfs2/cluster/ver.c2
-rw-r--r--fs/ocfs2/dcache.c8
-rw-r--r--fs/ocfs2/dir.c8
-rw-r--r--fs/ocfs2/dlm/dlmfsver.c2
-rw-r--r--fs/ocfs2/dlm/dlmrecovery.c19
-rw-r--r--fs/ocfs2/dlm/dlmver.c2
-rw-r--r--fs/ocfs2/dlmglue.c546
-rw-r--r--fs/ocfs2/dlmglue.h31
-rw-r--r--fs/ocfs2/endian.h5
-rw-r--r--fs/ocfs2/export.c8
-rw-r--r--fs/ocfs2/file.c163
-rw-r--r--fs/ocfs2/file.h6
-rw-r--r--fs/ocfs2/heartbeat.c80
-rw-r--r--fs/ocfs2/heartbeat.h2
-rw-r--r--fs/ocfs2/inode.c84
-rw-r--r--fs/ocfs2/inode.h10
-rw-r--r--fs/ocfs2/ioctl.c31
-rw-r--r--fs/ocfs2/journal.c51
-rw-r--r--fs/ocfs2/journal.h6
-rw-r--r--fs/ocfs2/localalloc.c50
-rw-r--r--fs/ocfs2/locks.c125
-rw-r--r--fs/ocfs2/locks.h (renamed from fs/ocfs2/vote.h)29
-rw-r--r--fs/ocfs2/mmap.c17
-rw-r--r--fs/ocfs2/namei.c66
-rw-r--r--fs/ocfs2/ocfs2.h35
-rw-r--r--fs/ocfs2/ocfs2_fs.h22
-rw-r--r--fs/ocfs2/ocfs2_lockid.h5
-rw-r--r--fs/ocfs2/resize.c634
-rw-r--r--fs/ocfs2/resize.h32
-rw-r--r--fs/ocfs2/slot_map.c19
-rw-r--r--fs/ocfs2/slot_map.h2
-rw-r--r--fs/ocfs2/suballoc.c20
-rw-r--r--fs/ocfs2/suballoc.h8
-rw-r--r--fs/ocfs2/super.c140
-rw-r--r--fs/ocfs2/sysfile.c2
-rw-r--r--fs/ocfs2/ver.c2
-rw-r--r--fs/ocfs2/vote.c756
-rw-r--r--fs/proc/base.c78
-rw-r--r--include/asm-cris/arch-v10/ide.h5
-rw-r--r--include/asm-cris/arch-v32/ide.h5
-rw-r--r--include/asm-frv/ide.h6
-rw-r--r--include/asm-generic/resource.h5
-rw-r--r--include/asm-powerpc/ide.h3
-rw-r--r--include/asm-x86/thread_info_32.h2
-rw-r--r--include/asm-x86/thread_info_64.h5
-rw-r--r--include/linux/Kbuild1
-rw-r--r--include/linux/ata.h110
-rw-r--r--include/linux/blkdev.h1
-rw-r--r--include/linux/cdrom.h3
-rw-r--r--include/linux/cpu.h17
-rw-r--r--include/linux/debug_locks.h5
-rw-r--r--include/linux/dlm.h140
-rw-r--r--include/linux/dlmconstants.h159
-rw-r--r--include/linux/futex.h6
-rw-r--r--include/linux/hardirq.h6
-rw-r--r--include/linux/hdreg.h64
-rw-r--r--include/linux/hrtimer.h14
-rw-r--r--include/linux/i2c-id.h2
-rw-r--r--include/linux/ide.h322
-rw-r--r--include/linux/init_task.h7
-rw-r--r--include/linux/interrupt.h1
-rw-r--r--include/linux/jiffies.h6
-rw-r--r--include/linux/kernel.h4
-rw-r--r--include/linux/latencytop.h44
-rw-r--r--include/linux/libata.h184
-rw-r--r--include/linux/notifier.h4
-rw-r--r--include/linux/pci_ids.h3
-rw-r--r--include/linux/rcuclassic.h164
-rw-r--r--include/linux/rcupdate.h173
-rw-r--r--include/linux/rcupreempt.h86
-rw-r--r--include/linux/rcupreempt_trace.h99
-rw-r--r--include/linux/sched.h83
-rw-r--r--include/linux/smp_lock.h14
-rw-r--r--include/linux/stacktrace.h3
-rw-r--r--include/linux/topology.h5
-rw-r--r--include/media/cs5345.h39
-rw-r--r--include/media/cx2341x.h2
-rw-r--r--include/media/cx25840.h19
-rw-r--r--include/media/ir-common.h3
-rw-r--r--include/media/m52790.h93
-rw-r--r--include/media/saa7146_vv.h1
-rw-r--r--include/media/tuner.h5
-rw-r--r--include/media/v4l2-chip-ident.h6
-rw-r--r--include/media/v4l2-common.h18
-rw-r--r--include/media/v4l2-i2c-drv-legacy.h140
-rw-r--r--include/media/v4l2-i2c-drv.h68
-rw-r--r--include/media/v4l2-int-device.h5
-rw-r--r--include/media/videobuf-core.h20
-rw-r--r--include/net/if_inet6.h11
-rw-r--r--include/net/ip.h10
-rw-r--r--include/rdma/ib_mad.h4
-rw-r--r--include/rdma/rdma_user_cm.h13
-rw-r--r--init/Kconfig28
-rw-r--r--init/main.c1
-rw-r--r--kernel/Kconfig.hz2
-rw-r--r--kernel/Kconfig.preempt13
-rw-r--r--kernel/Makefile6
-rw-r--r--kernel/cpu.c164
-rw-r--r--kernel/cpuset.c14
-rw-r--r--kernel/fork.c11
-rw-r--r--kernel/hrtimer.c256
-rw-r--r--kernel/kthread.c12
-rw-r--r--kernel/latencytop.c239
-rw-r--r--kernel/lockdep.c12
-rw-r--r--kernel/module.c27
-rw-r--r--kernel/posix-cpu-timers.c30
-rw-r--r--kernel/printk.c57
-rw-r--r--kernel/profile.c99
-rw-r--r--kernel/rcuclassic.c575
-rw-r--r--kernel/rcupdate.c576
-rw-r--r--kernel/rcupreempt.c953
-rw-r--r--kernel/rcupreempt_trace.c330
-rw-r--r--kernel/rcutorture.c6
-rw-r--r--kernel/sched.c1384
-rw-r--r--kernel/sched_debug.c5
-rw-r--r--kernel/sched_fair.c391
-rw-r--r--kernel/sched_idletask.c42
-rw-r--r--kernel/sched_rt.c1112
-rw-r--r--kernel/softlockup.c116
-rw-r--r--kernel/stop_machine.c4
-rw-r--r--kernel/sysctl.c77
-rw-r--r--kernel/time/tick-sched.c13
-rw-r--r--kernel/timer.c3
-rw-r--r--kernel/user.c47
-rw-r--r--kernel/workqueue.c35
-rw-r--r--lib/Kconfig.debug14
-rw-r--r--lib/kernel_lock.c123
-rw-r--r--mm/oom_kill.c2
-rw-r--r--mm/slab.c18
-rw-r--r--net/core/flow.c4
-rw-r--r--net/ipv4/arp.c2
-rw-r--r--net/ipv6/ndisc.c2
575 files changed, 45055 insertions, 19383 deletions
diff --git a/Documentation/DocBook/videobook.tmpl b/Documentation/DocBook/videobook.tmpl
index b629da33951d..b3d93ee27693 100644
--- a/Documentation/DocBook/videobook.tmpl
+++ b/Documentation/DocBook/videobook.tmpl
@@ -96,7 +96,6 @@ static struct video_device my_radio
96{ 96{
97 "My radio", 97 "My radio",
98 VID_TYPE_TUNER, 98 VID_TYPE_TUNER,
99 VID_HARDWARE_MYRADIO,
100 radio_open. 99 radio_open.
101 radio_close, 100 radio_close,
102 NULL, /* no read */ 101 NULL, /* no read */
@@ -119,13 +118,6 @@ static struct video_device my_radio
119 way to change channel so it is tuneable. 118 way to change channel so it is tuneable.
120 </para> 119 </para>
121 <para> 120 <para>
122 The VID_HARDWARE_ types are unique to each device. Numbers are assigned by
123 <email>alan@redhat.com</email> when device drivers are going to be released. Until then you
124 can pull a suitably large number out of your hat and use it. 10000 should be
125 safe for a very long time even allowing for the huge number of vendors
126 making new and different radio cards at the moment.
127 </para>
128 <para>
129 We declare an open and close routine, but we do not need read or write, 121 We declare an open and close routine, but we do not need read or write,
130 which are used to read and write video data to or from the card itself. As 122 which are used to read and write video data to or from the card itself. As
131 we have no read or write there is no poll function. 123 we have no read or write there is no poll function.
@@ -844,7 +836,6 @@ static struct video_device my_camera
844 "My Camera", 836 "My Camera",
845 VID_TYPE_OVERLAY|VID_TYPE_SCALES|\ 837 VID_TYPE_OVERLAY|VID_TYPE_SCALES|\
846 VID_TYPE_CAPTURE|VID_TYPE_CHROMAKEY, 838 VID_TYPE_CAPTURE|VID_TYPE_CHROMAKEY,
847 VID_HARDWARE_MYCAMERA,
848 camera_open. 839 camera_open.
849 camera_close, 840 camera_close,
850 camera_read, /* no read */ 841 camera_read, /* no read */
diff --git a/Documentation/RCU/RTFP.txt b/Documentation/RCU/RTFP.txt
index 6221464d1a7e..39ad8f56783a 100644
--- a/Documentation/RCU/RTFP.txt
+++ b/Documentation/RCU/RTFP.txt
@@ -9,8 +9,8 @@ The first thing resembling RCU was published in 1980, when Kung and Lehman
9[Kung80] recommended use of a garbage collector to defer destruction 9[Kung80] recommended use of a garbage collector to defer destruction
10of nodes in a parallel binary search tree in order to simplify its 10of nodes in a parallel binary search tree in order to simplify its
11implementation. This works well in environments that have garbage 11implementation. This works well in environments that have garbage
12collectors, but current production garbage collectors incur significant 12collectors, but most production garbage collectors incur significant
13read-side overhead. 13overhead.
14 14
15In 1982, Manber and Ladner [Manber82,Manber84] recommended deferring 15In 1982, Manber and Ladner [Manber82,Manber84] recommended deferring
16destruction until all threads running at that time have terminated, again 16destruction until all threads running at that time have terminated, again
@@ -99,16 +99,25 @@ locking, reduces contention, reduces memory latency for readers, and
99parallelizes pipeline stalls and memory latency for writers. However, 99parallelizes pipeline stalls and memory latency for writers. However,
100these techniques still impose significant read-side overhead in the 100these techniques still impose significant read-side overhead in the
101form of memory barriers. Researchers at Sun worked along similar lines 101form of memory barriers. Researchers at Sun worked along similar lines
102in the same timeframe [HerlihyLM02,HerlihyLMS03]. These techniques 102in the same timeframe [HerlihyLM02]. These techniques can be thought
103can be thought of as inside-out reference counts, where the count is 103of as inside-out reference counts, where the count is represented by the
104represented by the number of hazard pointers referencing a given data 104number of hazard pointers referencing a given data structure (rather than
105structure (rather than the more conventional counter field within the 105the more conventional counter field within the data structure itself).
106data structure itself). 106
107By the same token, RCU can be thought of as a "bulk reference count",
108where some form of reference counter covers all reference by a given CPU
109or thread during a set timeframe. This timeframe is related to, but
110not necessarily exactly the same as, an RCU grace period. In classic
111RCU, the reference counter is the per-CPU bit in the "bitmask" field,
112and each such bit covers all references that might have been made by
113the corresponding CPU during the prior grace period. Of course, RCU
114can be thought of in other terms as well.
107 115
108In 2003, the K42 group described how RCU could be used to create 116In 2003, the K42 group described how RCU could be used to create
109hot-pluggable implementations of operating-system functions. Later that 117hot-pluggable implementations of operating-system functions [Appavoo03a].
110year saw a paper describing an RCU implementation of System V IPC 118Later that year saw a paper describing an RCU implementation of System
111[Arcangeli03], and an introduction to RCU in Linux Journal [McKenney03a]. 119V IPC [Arcangeli03], and an introduction to RCU in Linux Journal
120[McKenney03a].
112 121
1132004 has seen a Linux-Journal article on use of RCU in dcache 1222004 has seen a Linux-Journal article on use of RCU in dcache
114[McKenney04a], a performance comparison of locking to RCU on several 123[McKenney04a], a performance comparison of locking to RCU on several
@@ -117,10 +126,19 @@ number of operating-system kernels [PaulEdwardMcKenneyPhD], a paper
117describing how to make RCU safe for soft-realtime applications [Sarma04c], 126describing how to make RCU safe for soft-realtime applications [Sarma04c],
118and a paper describing SELinux performance with RCU [JamesMorris04b]. 127and a paper describing SELinux performance with RCU [JamesMorris04b].
119 128
1202005 has seen further adaptation of RCU to realtime use, permitting 1292005 brought further adaptation of RCU to realtime use, permitting
121preemption of RCU realtime critical sections [PaulMcKenney05a, 130preemption of RCU realtime critical sections [PaulMcKenney05a,
122PaulMcKenney05b]. 131PaulMcKenney05b].
123 132
1332006 saw the first best-paper award for an RCU paper [ThomasEHart2006a],
134as well as further work on efficient implementations of preemptible
135RCU [PaulEMcKenney2006b], but priority-boosting of RCU read-side critical
136sections proved elusive. An RCU implementation permitting general
137blocking in read-side critical sections appeared [PaulEMcKenney2006c],
138Robert Olsson described an RCU-protected trie-hash combination
139[RobertOlsson2006a].
140
141
124Bibtex Entries 142Bibtex Entries
125 143
126@article{Kung80 144@article{Kung80
@@ -203,6 +221,41 @@ Bibtex Entries
203,Address="New Orleans, LA" 221,Address="New Orleans, LA"
204} 222}
205 223
224@conference{Pu95a,
225Author = "Calton Pu and Tito Autrey and Andrew Black and Charles Consel and
226Crispin Cowan and Jon Inouye and Lakshmi Kethana and Jonathan Walpole and
227Ke Zhang",
228Title = "Optimistic Incremental Specialization: Streamlining a Commercial
229Operating System",
230Booktitle = "15\textsuperscript{th} ACM Symposium on
231Operating Systems Principles (SOSP'95)",
232address = "Copper Mountain, CO",
233month="December",
234year="1995",
235pages="314-321",
236annotation="
237 Uses a replugger, but with a flag to signal when people are
238 using the resource at hand. Only one reader at a time.
239"
240}
241
242@conference{Cowan96a,
243Author = "Crispin Cowan and Tito Autrey and Charles Krasic and
244Calton Pu and Jonathan Walpole",
245Title = "Fast Concurrent Dynamic Linking for an Adaptive Operating System",
246Booktitle = "International Conference on Configurable Distributed Systems
247(ICCDS'96)",
248address = "Annapolis, MD",
249month="May",
250year="1996",
251pages="108",
252isbn="0-8186-7395-8",
253annotation="
254 Uses a replugger, but with a counter to signal when people are
255 using the resource at hand. Allows multiple readers.
256"
257}
258
206@techreport{Slingwine95 259@techreport{Slingwine95
207,author="John D. Slingwine and Paul E. McKenney" 260,author="John D. Slingwine and Paul E. McKenney"
208,title="Apparatus and Method for Achieving Reduced Overhead Mutual 261,title="Apparatus and Method for Achieving Reduced Overhead Mutual
@@ -312,6 +365,49 @@ Andrea Arcangeli and Andi Kleen and Orran Krieger and Rusty Russell"
312[Viewed June 23, 2004]" 365[Viewed June 23, 2004]"
313} 366}
314 367
368@conference{Michael02a
369,author="Maged M. Michael"
370,title="Safe Memory Reclamation for Dynamic Lock-Free Objects Using Atomic
371Reads and Writes"
372,Year="2002"
373,Month="August"
374,booktitle="{Proceedings of the 21\textsuperscript{st} Annual ACM
375Symposium on Principles of Distributed Computing}"
376,pages="21-30"
377,annotation="
378 Each thread keeps an array of pointers to items that it is
379 currently referencing. Sort of an inside-out garbage collection
380 mechanism, but one that requires the accessing code to explicitly
381 state its needs. Also requires read-side memory barriers on
382 most architectures.
383"
384}
385
386@conference{Michael02b
387,author="Maged M. Michael"
388,title="High Performance Dynamic Lock-Free Hash Tables and List-Based Sets"
389,Year="2002"
390,Month="August"
391,booktitle="{Proceedings of the 14\textsuperscript{th} Annual ACM
392Symposium on Parallel
393Algorithms and Architecture}"
394,pages="73-82"
395,annotation="
396 Like the title says...
397"
398}
399
400@InProceedings{HerlihyLM02
401,author={Maurice Herlihy and Victor Luchangco and Mark Moir}
402,title="The Repeat Offender Problem: A Mechanism for Supporting Dynamic-Sized,
403Lock-Free Data Structures"
404,booktitle={Proceedings of 16\textsuperscript{th} International
405Symposium on Distributed Computing}
406,year=2002
407,month="October"
408,pages="339-353"
409}
410
315@article{Appavoo03a 411@article{Appavoo03a
316,author="J. Appavoo and K. Hui and C. A. N. Soules and R. W. Wisniewski and 412,author="J. Appavoo and K. Hui and C. A. N. Soules and R. W. Wisniewski and
317D. M. {Da Silva} and O. Krieger and M. A. Auslander and D. J. Edelsohn and 413D. M. {Da Silva} and O. Krieger and M. A. Auslander and D. J. Edelsohn and
@@ -447,3 +543,95 @@ Oregon Health and Sciences University"
447 Realtime turns into making RCU yet more realtime friendly. 543 Realtime turns into making RCU yet more realtime friendly.
448" 544"
449} 545}
546
547@conference{ThomasEHart2006a
548,Author="Thomas E. Hart and Paul E. McKenney and Angela Demke Brown"
549,Title="Making Lockless Synchronization Fast: Performance Implications
550of Memory Reclamation"
551,Booktitle="20\textsuperscript{th} {IEEE} International Parallel and
552Distributed Processing Symposium"
553,month="April"
554,year="2006"
555,day="25-29"
556,address="Rhodes, Greece"
557,annotation="
558 Compares QSBR (AKA "classic RCU"), HPBR, EBR, and lock-free
559 reference counting.
560"
561}
562
563@Conference{PaulEMcKenney2006b
564,Author="Paul E. McKenney and Dipankar Sarma and Ingo Molnar and
565Suparna Bhattacharya"
566,Title="Extending RCU for Realtime and Embedded Workloads"
567,Booktitle="{Ottawa Linux Symposium}"
568,Month="July"
569,Year="2006"
570,pages="v2 123-138"
571,note="Available:
572\url{http://www.linuxsymposium.org/2006/view_abstract.php?content_key=184}
573\url{http://www.rdrop.com/users/paulmck/RCU/OLSrtRCU.2006.08.11a.pdf}
574[Viewed January 1, 2007]"
575,annotation="
576 Described how to improve the -rt implementation of realtime RCU.
577"
578}
579
580@unpublished{PaulEMcKenney2006c
581,Author="Paul E. McKenney"
582,Title="Sleepable {RCU}"
583,month="October"
584,day="9"
585,year="2006"
586,note="Available:
587\url{http://lwn.net/Articles/202847/}
588Revised:
589\url{http://www.rdrop.com/users/paulmck/RCU/srcu.2007.01.14a.pdf}
590[Viewed August 21, 2006]"
591,annotation="
592 LWN article introducing SRCU.
593"
594}
595
596@unpublished{RobertOlsson2006a
597,Author="Robert Olsson and Stefan Nilsson"
598,Title="{TRASH}: A dynamic {LC}-trie and hash data structure"
599,month="August"
600,day="18"
601,year="2006"
602,note="Available:
603\url{http://www.nada.kth.se/~snilsson/public/papers/trash/trash.pdf}
604[Viewed February 24, 2007]"
605,annotation="
606 RCU-protected dynamic trie-hash combination.
607"
608}
609
610@unpublished{ThomasEHart2007a
611,Author="Thomas E. Hart and Paul E. McKenney and Angela Demke Brown and Jonathan Walpole"
612,Title="Performance of memory reclamation for lockless synchronization"
613,journal="J. Parallel Distrib. Comput."
614,year="2007"
615,note="To appear in J. Parallel Distrib. Comput.
616 \url{doi=10.1016/j.jpdc.2007.04.010}"
617,annotation={
618 Compares QSBR (AKA "classic RCU"), HPBR, EBR, and lock-free
619 reference counting. Journal version of ThomasEHart2006a.
620}
621}
622
623@unpublished{PaulEMcKenney2007QRCUspin
624,Author="Paul E. McKenney"
625,Title="Using Promela and Spin to verify parallel algorithms"
626,month="August"
627,day="1"
628,year="2007"
629,note="Available:
630\url{http://lwn.net/Articles/243851/}
631[Viewed September 8, 2007]"
632,annotation="
633 LWN article describing Promela and spin, and also using Oleg
634 Nesterov's QRCU as an example (with Paul McKenney's fastpath).
635"
636}
637
diff --git a/Documentation/RCU/rcu.txt b/Documentation/RCU/rcu.txt
index f84407cba816..95821a29ae41 100644
--- a/Documentation/RCU/rcu.txt
+++ b/Documentation/RCU/rcu.txt
@@ -36,6 +36,14 @@ o How can the updater tell when a grace period has completed
36 executed in user mode, or executed in the idle loop, we can 36 executed in user mode, or executed in the idle loop, we can
37 safely free up that item. 37 safely free up that item.
38 38
39 Preemptible variants of RCU (CONFIG_PREEMPT_RCU) get the
40 same effect, but require that the readers manipulate CPU-local
41 counters. These counters allow limited types of blocking
42 within RCU read-side critical sections. SRCU also uses
43 CPU-local counters, and permits general blocking within
44 RCU read-side critical sections. These two variants of
45 RCU detect grace periods by sampling these counters.
46
39o If I am running on a uniprocessor kernel, which can only do one 47o If I am running on a uniprocessor kernel, which can only do one
40 thing at a time, why should I wait for a grace period? 48 thing at a time, why should I wait for a grace period?
41 49
@@ -46,7 +54,10 @@ o How can I see where RCU is currently used in the Linux kernel?
46 Search for "rcu_read_lock", "rcu_read_unlock", "call_rcu", 54 Search for "rcu_read_lock", "rcu_read_unlock", "call_rcu",
47 "rcu_read_lock_bh", "rcu_read_unlock_bh", "call_rcu_bh", 55 "rcu_read_lock_bh", "rcu_read_unlock_bh", "call_rcu_bh",
48 "srcu_read_lock", "srcu_read_unlock", "synchronize_rcu", 56 "srcu_read_lock", "srcu_read_unlock", "synchronize_rcu",
49 "synchronize_net", and "synchronize_srcu". 57 "synchronize_net", "synchronize_srcu", and the other RCU
58 primitives. Or grab one of the cscope databases from:
59
60 http://www.rdrop.com/users/paulmck/RCU/linuxusage/rculocktab.html
50 61
51o What guidelines should I follow when writing code that uses RCU? 62o What guidelines should I follow when writing code that uses RCU?
52 63
@@ -67,7 +78,11 @@ o I hear that RCU is patented? What is with that?
67 78
68o I hear that RCU needs work in order to support realtime kernels? 79o I hear that RCU needs work in order to support realtime kernels?
69 80
70 Yes, work in progress. 81 This work is largely completed. Realtime-friendly RCU can be
82 enabled via the CONFIG_PREEMPT_RCU kernel configuration parameter.
83 However, work is in progress for enabling priority boosting of
84 preempted RCU read-side critical sections.This is needed if you
85 have CPU-bound realtime threads.
71 86
72o Where can I find more information on RCU? 87o Where can I find more information on RCU?
73 88
diff --git a/Documentation/RCU/torture.txt b/Documentation/RCU/torture.txt
index 25a3c3f7d378..2967a65269d8 100644
--- a/Documentation/RCU/torture.txt
+++ b/Documentation/RCU/torture.txt
@@ -46,12 +46,13 @@ stat_interval The number of seconds between output of torture
46 46
47shuffle_interval 47shuffle_interval
48 The number of seconds to keep the test threads affinitied 48 The number of seconds to keep the test threads affinitied
49 to a particular subset of the CPUs. Used in conjunction 49 to a particular subset of the CPUs, defaults to 5 seconds.
50 with test_no_idle_hz. 50 Used in conjunction with test_no_idle_hz.
51 51
52test_no_idle_hz Whether or not to test the ability of RCU to operate in 52test_no_idle_hz Whether or not to test the ability of RCU to operate in
53 a kernel that disables the scheduling-clock interrupt to 53 a kernel that disables the scheduling-clock interrupt to
54 idle CPUs. Boolean parameter, "1" to test, "0" otherwise. 54 idle CPUs. Boolean parameter, "1" to test, "0" otherwise.
55 Defaults to omitting this test.
55 56
56torture_type The type of RCU to test: "rcu" for the rcu_read_lock() API, 57torture_type The type of RCU to test: "rcu" for the rcu_read_lock() API,
57 "rcu_sync" for rcu_read_lock() with synchronous reclamation, 58 "rcu_sync" for rcu_read_lock() with synchronous reclamation,
@@ -82,8 +83,6 @@ be evident. ;-)
82 83
83The entries are as follows: 84The entries are as follows:
84 85
85o "ggp": The number of counter flips (or batches) since boot.
86
87o "rtc": The hexadecimal address of the structure currently visible 86o "rtc": The hexadecimal address of the structure currently visible
88 to readers. 87 to readers.
89 88
@@ -117,8 +116,8 @@ o "Reader Pipe": Histogram of "ages" of structures seen by readers.
117o "Reader Batch": Another histogram of "ages" of structures seen 116o "Reader Batch": Another histogram of "ages" of structures seen
118 by readers, but in terms of counter flips (or batches) rather 117 by readers, but in terms of counter flips (or batches) rather
119 than in terms of grace periods. The legal number of non-zero 118 than in terms of grace periods. The legal number of non-zero
120 entries is again two. The reason for this separate view is 119 entries is again two. The reason for this separate view is that
121 that it is easier to get the third entry to show up in the 120 it is sometimes easier to get the third entry to show up in the
122 "Reader Batch" list than in the "Reader Pipe" list. 121 "Reader Batch" list than in the "Reader Pipe" list.
123 122
124o "Free-Block Circulation": Shows the number of torture structures 123o "Free-Block Circulation": Shows the number of torture structures
diff --git a/Documentation/cpu-hotplug.txt b/Documentation/cpu-hotplug.txt
index a741f658a3c9..fb94f5a71b68 100644
--- a/Documentation/cpu-hotplug.txt
+++ b/Documentation/cpu-hotplug.txt
@@ -109,12 +109,13 @@ Never use anything other than cpumask_t to represent bitmap of CPUs.
109 for_each_cpu_mask(x,mask) - Iterate over some random collection of cpu mask. 109 for_each_cpu_mask(x,mask) - Iterate over some random collection of cpu mask.
110 110
111 #include <linux/cpu.h> 111 #include <linux/cpu.h>
112 lock_cpu_hotplug() and unlock_cpu_hotplug(): 112 get_online_cpus() and put_online_cpus():
113 113
114The above calls are used to inhibit cpu hotplug operations. While holding the 114The above calls are used to inhibit cpu hotplug operations. While the
115cpucontrol mutex, cpu_online_map will not change. If you merely need to avoid 115cpu_hotplug.refcount is non zero, the cpu_online_map will not change.
116cpus going away, you could also use preempt_disable() and preempt_enable() 116If you merely need to avoid cpus going away, you could also use
117for those sections. Just remember the critical section cannot call any 117preempt_disable() and preempt_enable() for those sections.
118Just remember the critical section cannot call any
118function that can sleep or schedule this process away. The preempt_disable() 119function that can sleep or schedule this process away. The preempt_disable()
119will work as long as stop_machine_run() is used to take a cpu down. 120will work as long as stop_machine_run() is used to take a cpu down.
120 121
diff --git a/Documentation/dvb/bt8xx.txt b/Documentation/dvb/bt8xx.txt
index ecb47adda063..b7b1d1b1da46 100644
--- a/Documentation/dvb/bt8xx.txt
+++ b/Documentation/dvb/bt8xx.txt
@@ -78,6 +78,18 @@ Example:
78For a full list of card ID's please see Documentation/video4linux/CARDLIST.bttv. 78For a full list of card ID's please see Documentation/video4linux/CARDLIST.bttv.
79In case of further problems please subscribe and send questions to the mailing list: linux-dvb@linuxtv.org. 79In case of further problems please subscribe and send questions to the mailing list: linux-dvb@linuxtv.org.
80 80
812c) Probing the cards with broken PCI subsystem ID
82--------------------------------------------------
83There are some TwinHan cards that the EEPROM has become corrupted for some
84reason. The cards do not have correct PCI subsystem ID. But we can force
85probing the cards with broken PCI subsystem ID
86
87 $ echo 109e 0878 $subvendor $subdevice > \
88 /sys/bus/pci/drivers/bt878/new_id
89
90109e: PCI_VENDOR_ID_BROOKTREE
910878: PCI_DEVICE_ID_BROOKTREE_878
92
81Authors: Richard Walker, 93Authors: Richard Walker,
82 Jamie Honan, 94 Jamie Honan,
83 Michael Hunold, 95 Michael Hunold,
diff --git a/Documentation/feature-removal-schedule.txt b/Documentation/feature-removal-schedule.txt
index 20c4c8bac9d7..9b8291f4c211 100644
--- a/Documentation/feature-removal-schedule.txt
+++ b/Documentation/feature-removal-schedule.txt
@@ -295,16 +295,6 @@ Who: linuxppc-dev@ozlabs.org
295 295
296--------------------------- 296---------------------------
297 297
298What: mthca driver's MSI support
299When: January 2008
300Files: drivers/infiniband/hw/mthca/*.[ch]
301Why: All mthca hardware also supports MSI-X, which provides
302 strictly more functionality than MSI. So there is no point in
303 having both MSI-X and MSI support in the driver.
304Who: Roland Dreier <rolandd@cisco.com>
305
306---------------------------
307
308What: sk98lin network driver 298What: sk98lin network driver
309When: Feburary 2008 299When: Feburary 2008
310Why: In kernel tree version of driver is unmaintained. Sk98lin driver 300Why: In kernel tree version of driver is unmaintained. Sk98lin driver
diff --git a/Documentation/filesystems/ocfs2.txt b/Documentation/filesystems/ocfs2.txt
index ed55238023a9..c318a8bbb1ef 100644
--- a/Documentation/filesystems/ocfs2.txt
+++ b/Documentation/filesystems/ocfs2.txt
@@ -35,7 +35,6 @@ Features which OCFS2 does not support yet:
35 - Directory change notification (F_NOTIFY) 35 - Directory change notification (F_NOTIFY)
36 - Distributed Caching (F_SETLEASE/F_GETLEASE/break_lease) 36 - Distributed Caching (F_SETLEASE/F_GETLEASE/break_lease)
37 - POSIX ACLs 37 - POSIX ACLs
38 - readpages / writepages (not user visible)
39 38
40Mount options 39Mount options
41============= 40=============
@@ -62,3 +61,18 @@ data=writeback Data ordering is not preserved, data may be written
62preferred_slot=0(*) During mount, try to use this filesystem slot first. If 61preferred_slot=0(*) During mount, try to use this filesystem slot first. If
63 it is in use by another node, the first empty one found 62 it is in use by another node, the first empty one found
64 will be chosen. Invalid values will be ignored. 63 will be chosen. Invalid values will be ignored.
64commit=nrsec (*) Ocfs2 can be told to sync all its data and metadata
65 every 'nrsec' seconds. The default value is 5 seconds.
66 This means that if you lose your power, you will lose
67 as much as the latest 5 seconds of work (your
68 filesystem will not be damaged though, thanks to the
69 journaling). This default value (or any low value)
70 will hurt performance, but it's good for data-safety.
71 Setting it to 0 will have the same effect as leaving
72 it at the default (5 seconds).
73 Setting it to very large values will improve
74 performance.
75localalloc=8(*) Allows custom localalloc size in MB. If the value is too
76 large, the fs will silently revert it to the default.
77 Localalloc is not enabled for local mounts.
78localflocks This disables cluster aware flock.
diff --git a/Documentation/ioctl-number.txt b/Documentation/ioctl-number.txt
index 5c7fbf9d96b4..c18363bd8d11 100644
--- a/Documentation/ioctl-number.txt
+++ b/Documentation/ioctl-number.txt
@@ -138,6 +138,7 @@ Code Seq# Include File Comments
138'm' 00-1F net/irda/irmod.h conflict! 138'm' 00-1F net/irda/irmod.h conflict!
139'n' 00-7F linux/ncp_fs.h 139'n' 00-7F linux/ncp_fs.h
140'n' E0-FF video/matrox.h matroxfb 140'n' E0-FF video/matrox.h matroxfb
141'o' 00-1F fs/ocfs2/ocfs2_fs.h OCFS2
141'p' 00-0F linux/phantom.h conflict! (OpenHaptics needs this) 142'p' 00-0F linux/phantom.h conflict! (OpenHaptics needs this)
142'p' 00-3F linux/mc146818rtc.h conflict! 143'p' 00-3F linux/mc146818rtc.h conflict!
143'p' 40-7F linux/nvram.h 144'p' 40-7F linux/nvram.h
diff --git a/Documentation/video4linux/CARDLIST.cx23885 b/Documentation/video4linux/CARDLIST.cx23885
index 00cb646a4bde..0924e6e142c4 100644
--- a/Documentation/video4linux/CARDLIST.cx23885
+++ b/Documentation/video4linux/CARDLIST.cx23885
@@ -1,5 +1,7 @@
1 0 -> UNKNOWN/GENERIC [0070:3400] 1 0 -> UNKNOWN/GENERIC [0070:3400]
2 1 -> Hauppauge WinTV-HVR1800lp [0070:7600] 2 1 -> Hauppauge WinTV-HVR1800lp [0070:7600]
3 2 -> Hauppauge WinTV-HVR1800 [0070:7800,0070:7801] 3 2 -> Hauppauge WinTV-HVR1800 [0070:7800,0070:7801,0070:7809]
4 3 -> Hauppauge WinTV-HVR1250 [0070:7911] 4 3 -> Hauppauge WinTV-HVR1250 [0070:7911]
5 4 -> DViCO FusionHDTV5 Express [18ac:d500] 5 4 -> DViCO FusionHDTV5 Express [18ac:d500]
6 5 -> Hauppauge WinTV-HVR1500Q [0070:7790,0070:7797]
7 6 -> Hauppauge WinTV-HVR1500 [0070:7710,0070:7717]
diff --git a/Documentation/video4linux/CARDLIST.cx88 b/Documentation/video4linux/CARDLIST.cx88
index 82ac8250e978..bc5593bd9704 100644
--- a/Documentation/video4linux/CARDLIST.cx88
+++ b/Documentation/video4linux/CARDLIST.cx88
@@ -56,3 +56,4 @@
56 55 -> Shenzhen Tungsten Ages Tech TE-DTV-250 / Swann OEM [c180:c980] 56 55 -> Shenzhen Tungsten Ages Tech TE-DTV-250 / Swann OEM [c180:c980]
57 56 -> Hauppauge WinTV-HVR1300 DVB-T/Hybrid MPEG Encoder [0070:9600,0070:9601,0070:9602] 57 56 -> Hauppauge WinTV-HVR1300 DVB-T/Hybrid MPEG Encoder [0070:9600,0070:9601,0070:9602]
58 57 -> ADS Tech Instant Video PCI [1421:0390] 58 57 -> ADS Tech Instant Video PCI [1421:0390]
59 58 -> Pinnacle PCTV HD 800i [11bd:0051]
diff --git a/Documentation/video4linux/CARDLIST.em28xx b/Documentation/video4linux/CARDLIST.em28xx
index 37f0e3cedf43..6a8469f2bcae 100644
--- a/Documentation/video4linux/CARDLIST.em28xx
+++ b/Documentation/video4linux/CARDLIST.em28xx
@@ -1,14 +1,17 @@
1 0 -> Unknown EM2800 video grabber (em2800) [eb1a:2800] 1 0 -> Unknown EM2800 video grabber (em2800) [eb1a:2800]
2 1 -> Unknown EM2820/2840 video grabber (em2820/em2840) 2 1 -> Unknown EM2750/28xx video grabber (em2820/em2840) [eb1a:2750,eb1a:2820,eb1a:2821,eb1a:2860,eb1a:2861,eb1a:2870,eb1a:2881,eb1a:2883]
3 2 -> Terratec Cinergy 250 USB (em2820/em2840) [0ccd:0036] 3 2 -> Terratec Cinergy 250 USB (em2820/em2840) [0ccd:0036]
4 3 -> Pinnacle PCTV USB 2 (em2820/em2840) [2304:0208] 4 3 -> Pinnacle PCTV USB 2 (em2820/em2840) [2304:0208]
5 4 -> Hauppauge WinTV USB 2 (em2820/em2840) [2040:4200] 5 4 -> Hauppauge WinTV USB 2 (em2820/em2840) [2040:4200,2040:4201]
6 5 -> MSI VOX USB 2.0 (em2820/em2840) [eb1a:2820] 6 5 -> MSI VOX USB 2.0 (em2820/em2840)
7 6 -> Terratec Cinergy 200 USB (em2800) 7 6 -> Terratec Cinergy 200 USB (em2800)
8 7 -> Leadtek Winfast USB II (em2800) 8 7 -> Leadtek Winfast USB II (em2800)
9 8 -> Kworld USB2800 (em2800) 9 8 -> Kworld USB2800 (em2800)
10 9 -> Pinnacle Dazzle DVC 90 (em2820/em2840) [2304:0207] 10 9 -> Pinnacle Dazzle DVC 90/DVC 100 (em2820/em2840) [2304:0207,2304:021a]
11 10 -> Hauppauge WinTV HVR 900 (em2880) 11 10 -> Hauppauge WinTV HVR 900 (em2880) [2040:6500]
12 11 -> Terratec Hybrid XS (em2880) 12 11 -> Terratec Hybrid XS (em2880) [0ccd:0042]
13 12 -> Kworld PVR TV 2800 RF (em2820/em2840) 13 12 -> Kworld PVR TV 2800 RF (em2820/em2840)
14 13 -> Terratec Prodigy XS (em2880) 14 13 -> Terratec Prodigy XS (em2880) [0ccd:0047]
15 14 -> Pixelview Prolink PlayTV USB 2.0 (em2820/em2840)
16 15 -> V-Gear PocketTV (em2800)
17 16 -> Hauppauge WinTV HVR 950 (em2880) [2040:6513]
diff --git a/Documentation/video4linux/CARDLIST.ivtv b/Documentation/video4linux/CARDLIST.ivtv
index ddd76a0eb100..a019e27e42b3 100644
--- a/Documentation/video4linux/CARDLIST.ivtv
+++ b/Documentation/video4linux/CARDLIST.ivtv
@@ -16,3 +16,9 @@
1616 -> GOTVIEW PCI DVD2 Deluxe [ffac:0600] 1616 -> GOTVIEW PCI DVD2 Deluxe [ffac:0600]
1717 -> Yuan MPC622 [ff01:d998] 1717 -> Yuan MPC622 [ff01:d998]
1818 -> Digital Cowboy DCT-MTVP1 [1461:bfff] 1818 -> Digital Cowboy DCT-MTVP1 [1461:bfff]
1919 -> Yuan PG600V2/GotView PCI DVD Lite [ffab:0600,ffad:0600]
2020 -> Club3D ZAP-TV1x01 [ffab:0600]
2121 -> AverTV MCE 116 Plus [1461:c439]
2222 -> ASUS Falcon2 [1043:4b66,1043:462e,1043:4b2e]
2323 -> AverMedia PVR-150 Plus [1461:c035]
2424 -> AverMedia EZMaker PCI Deluxe [1461:c03f]
diff --git a/Documentation/video4linux/CARDLIST.saa7134 b/Documentation/video4linux/CARDLIST.saa7134
index a14545300e4c..5d3b6b4d2515 100644
--- a/Documentation/video4linux/CARDLIST.saa7134
+++ b/Documentation/video4linux/CARDLIST.saa7134
@@ -80,7 +80,7 @@
80 79 -> Sedna/MuchTV PC TV Cardbus TV/Radio (ITO25 Rev:2B) 80 79 -> Sedna/MuchTV PC TV Cardbus TV/Radio (ITO25 Rev:2B)
81 80 -> ASUS Digimatrix TV [1043:0210] 81 80 -> ASUS Digimatrix TV [1043:0210]
82 81 -> Philips Tiger reference design [1131:2018] 82 81 -> Philips Tiger reference design [1131:2018]
83 82 -> MSI TV@Anywhere plus [1462:6231] 83 82 -> MSI TV@Anywhere plus [1462:6231,1462:8624]
84 83 -> Terratec Cinergy 250 PCI TV [153b:1160] 84 83 -> Terratec Cinergy 250 PCI TV [153b:1160]
85 84 -> LifeView FlyDVB Trio [5168:0319] 85 84 -> LifeView FlyDVB Trio [5168:0319]
86 85 -> AverTV DVB-T 777 [1461:2c05,1461:2c05] 86 85 -> AverTV DVB-T 777 [1461:2c05,1461:2c05]
@@ -102,7 +102,7 @@
102101 -> Pinnacle PCTV 310i [11bd:002f] 102101 -> Pinnacle PCTV 310i [11bd:002f]
103102 -> Avermedia AVerTV Studio 507 [1461:9715] 103102 -> Avermedia AVerTV Studio 507 [1461:9715]
104103 -> Compro Videomate DVB-T200A 104103 -> Compro Videomate DVB-T200A
105104 -> Hauppauge WinTV-HVR1110 DVB-T/Hybrid [0070:6701] 105104 -> Hauppauge WinTV-HVR1110 DVB-T/Hybrid [0070:6700,0070:6701,0070:6702,0070:6703,0070:6704,0070:6705]
106105 -> Terratec Cinergy HT PCMCIA [153b:1172] 106105 -> Terratec Cinergy HT PCMCIA [153b:1172]
107106 -> Encore ENLTV [1131:2342,1131:2341,3016:2344] 107106 -> Encore ENLTV [1131:2342,1131:2341,3016:2344]
108107 -> Encore ENLTV-FM [1131:230f] 108107 -> Encore ENLTV-FM [1131:230f]
@@ -116,3 +116,16 @@
116115 -> Sabrent PCMCIA TV-PCB05 [0919:2003] 116115 -> Sabrent PCMCIA TV-PCB05 [0919:2003]
117116 -> 10MOONS TM300 TV Card [1131:2304] 117116 -> 10MOONS TM300 TV Card [1131:2304]
118117 -> Avermedia Super 007 [1461:f01d] 118117 -> Avermedia Super 007 [1461:f01d]
119118 -> Beholder BeholdTV 401 [0000:4016]
120119 -> Beholder BeholdTV 403 [0000:4036]
121120 -> Beholder BeholdTV 403 FM [0000:4037]
122121 -> Beholder BeholdTV 405 [0000:4050]
123122 -> Beholder BeholdTV 405 FM [0000:4051]
124123 -> Beholder BeholdTV 407 [0000:4070]
125124 -> Beholder BeholdTV 407 FM [0000:4071]
126125 -> Beholder BeholdTV 409 [0000:4090]
127126 -> Beholder BeholdTV 505 FM/RDS [0000:5051,0000:505B,5ace:5050]
128127 -> Beholder BeholdTV 507 FM/RDS / BeholdTV 509 FM [0000:5071,0000:507B,5ace:5070,5ace:5090]
129128 -> Beholder BeholdTV Columbus TVFM [0000:5201]
130129 -> Beholder BeholdTV 607 / BeholdTV 609 [5ace:6070,5ace:6071,5ace:6072,5ace:6073,5ace:6090,5ace:6091,5ace:6092,5ace:6093]
131130 -> Beholder BeholdTV M6 / BeholdTV M6 Extra [5ace:6190,5ace:6193]
diff --git a/Documentation/video4linux/CARDLIST.tuner b/Documentation/video4linux/CARDLIST.tuner
index a88c02d23805..0e2394695bb8 100644
--- a/Documentation/video4linux/CARDLIST.tuner
+++ b/Documentation/video4linux/CARDLIST.tuner
@@ -52,7 +52,7 @@ tuner=50 - TCL 2002N
52tuner=51 - Philips PAL/SECAM_D (FM 1256 I-H3) 52tuner=51 - Philips PAL/SECAM_D (FM 1256 I-H3)
53tuner=52 - Thomson DTT 7610 (ATSC/NTSC) 53tuner=52 - Thomson DTT 7610 (ATSC/NTSC)
54tuner=53 - Philips FQ1286 54tuner=53 - Philips FQ1286
55tuner=54 - tda8290+75 55tuner=54 - Philips/NXP TDA 8290/8295 + 8275/8275A/18271
56tuner=55 - TCL 2002MB 56tuner=55 - TCL 2002MB
57tuner=56 - Philips PAL/SECAM multi (FQ1216AME MK4) 57tuner=56 - Philips PAL/SECAM multi (FQ1216AME MK4)
58tuner=57 - Philips FQ1236A MK4 58tuner=57 - Philips FQ1236A MK4
@@ -69,7 +69,8 @@ tuner=67 - Philips TD1316 Hybrid Tuner
69tuner=68 - Philips TUV1236D ATSC/NTSC dual in 69tuner=68 - Philips TUV1236D ATSC/NTSC dual in
70tuner=69 - Tena TNF 5335 and similar models 70tuner=69 - Tena TNF 5335 and similar models
71tuner=70 - Samsung TCPN 2121P30A 71tuner=70 - Samsung TCPN 2121P30A
72tuner=71 - Xceive xc3028 72tuner=71 - Xceive xc2028/xc3028 tuner
73tuner=72 - Thomson FE6600 73tuner=72 - Thomson FE6600
74tuner=73 - Samsung TCPG 6121P30A 74tuner=73 - Samsung TCPG 6121P30A
75tuner=75 - Philips TEA5761 FM Radio 75tuner=75 - Philips TEA5761 FM Radio
76tuner=76 - Xceive 5000 tuner
diff --git a/Documentation/video4linux/CARDLIST.usbvision b/Documentation/video4linux/CARDLIST.usbvision
index 3d6850ef0245..0b72d3fee17e 100644
--- a/Documentation/video4linux/CARDLIST.usbvision
+++ b/Documentation/video4linux/CARDLIST.usbvision
@@ -62,3 +62,4 @@
62 61 -> Pinnacle Studio Linx Video input cable (PAL) [2304:0301] 62 61 -> Pinnacle Studio Linx Video input cable (PAL) [2304:0301]
63 62 -> Pinnacle PCTV Bungee USB (PAL) FM [2304:0419] 63 62 -> Pinnacle PCTV Bungee USB (PAL) FM [2304:0419]
64 63 -> Hauppauge WinTv-USB [2400:4200] 64 63 -> Hauppauge WinTv-USB [2400:4200]
65 64 -> Pinnacle Studio PCTV USB (NTSC) FM V3 [2304:0113]
diff --git a/Documentation/video4linux/extract_xc3028.pl b/Documentation/video4linux/extract_xc3028.pl
new file mode 100644
index 000000000000..cced8ac5c543
--- /dev/null
+++ b/Documentation/video4linux/extract_xc3028.pl
@@ -0,0 +1,926 @@
1#!/usr/bin/perl
2
3# Copyright (c) Mauro Carvalho Chehab <mchehab@infradead.org>
4# Released under GPLv2
5#
6# In order to use, you need to:
7# 1) Download the windows driver with something like:
8# wget http://www.steventoth.net/linux/xc5000/HVR-12x0-14x0-17x0_1_25_25271_WHQL.zip
9# 2) Extract the file hcw85bda.sys from the zip into the current dir:
10# unzip -j HVR-12x0-14x0-17x0_1_25_25271_WHQL.zip Driver85/hcw85bda.sys
11# 3) run the script:
12# ./extract_xc3028.pl
13# 4) copy the generated file:
14# cp xc3028-v27.fw /lib/firmware
15
16#use strict;
17use IO::Handle;
18
19my $debug=0;
20
21sub verify ($$)
22{
23 my ($filename, $hash) = @_;
24 my ($testhash);
25
26 if (system("which md5sum > /dev/null 2>&1")) {
27 die "This firmware requires the md5sum command - see http://www.gnu.org/software/coreutils/\n";
28 }
29
30 open(CMD, "md5sum ".$filename."|");
31 $testhash = <CMD>;
32 $testhash =~ /([a-zA-Z0-9]*)/;
33 $testhash = $1;
34 close CMD;
35 die "Hash of extracted file does not match (found $testhash, expected $hash!\n" if ($testhash ne $hash);
36}
37
38sub get_hunk ($$)
39{
40 my ($offset, $length) = @_;
41 my ($chunklength, $buf, $rcount, $out);
42
43 sysseek(INFILE, $offset, SEEK_SET);
44 while ($length > 0) {
45 # Calc chunk size
46 $chunklength = 2048;
47 $chunklength = $length if ($chunklength > $length);
48
49 $rcount = sysread(INFILE, $buf, $chunklength);
50 die "Ran out of data\n" if ($rcount != $chunklength);
51 $out .= $buf;
52 $length -= $rcount;
53 }
54 return $out;
55}
56
57sub write_le16($)
58{
59 my $val = shift;
60 my $msb = ($val >> 8) &0xff;
61 my $lsb = $val & 0xff;
62
63 syswrite(OUTFILE, chr($lsb).chr($msb));
64}
65
66sub write_le32($)
67{
68 my $val = shift;
69 my $l3 = ($val >> 24) & 0xff;
70 my $l2 = ($val >> 16) & 0xff;
71 my $l1 = ($val >> 8) & 0xff;
72 my $l0 = $val & 0xff;
73
74 syswrite(OUTFILE, chr($l0).chr($l1).chr($l2).chr($l3));
75}
76
77sub write_le64($$)
78{
79 my $msb_val = shift;
80 my $lsb_val = shift;
81 my $l7 = ($msb_val >> 24) & 0xff;
82 my $l6 = ($msb_val >> 16) & 0xff;
83 my $l5 = ($msb_val >> 8) & 0xff;
84 my $l4 = $msb_val & 0xff;
85
86 my $l3 = ($lsb_val >> 24) & 0xff;
87 my $l2 = ($lsb_val >> 16) & 0xff;
88 my $l1 = ($lsb_val >> 8) & 0xff;
89 my $l0 = $lsb_val & 0xff;
90
91 syswrite(OUTFILE,
92 chr($l0).chr($l1).chr($l2).chr($l3).
93 chr($l4).chr($l5).chr($l6).chr($l7));
94}
95
96sub write_hunk($$)
97{
98 my ($offset, $length) = @_;
99 my $out = get_hunk($offset, $length);
100
101 printf "(len %d) ",$length if ($debug);
102
103 for (my $i=0;$i<$length;$i++) {
104 printf "%02x ",ord(substr($out,$i,1)) if ($debug);
105 }
106 printf "\n" if ($debug);
107
108 syswrite(OUTFILE, $out);
109}
110
111sub write_hunk_fix_endian($$)
112{
113 my ($offset, $length) = @_;
114 my $out = get_hunk($offset, $length);
115
116 printf "(len_fix %d) ",$length if ($debug);
117
118 for (my $i=0;$i<$length;$i++) {
119 printf "%02x ",ord(substr($out,$i,1)) if ($debug);
120 }
121 printf "\n" if ($debug);
122
123 my $i=0;
124 while ($i<$length) {
125 my $size = ord(substr($out,$i,1))*256+ord(substr($out,$i+1,1));
126 syswrite(OUTFILE, substr($out,$i+1,1));
127 syswrite(OUTFILE, substr($out,$i,1));
128 $i+=2;
129 if ($size>0 && $size <0x8000) {
130 for (my $j=0;$j<$size;$j++) {
131 syswrite(OUTFILE, substr($out,$j+$i,1));
132 }
133 $i+=$size;
134 }
135 }
136}
137
138sub main_firmware($$$$)
139{
140 my $out;
141 my $j=0;
142 my $outfile = shift;
143 my $name = shift;
144 my $version = shift;
145 my $nr_desc = shift;
146
147 for ($j = length($name); $j <32; $j++) {
148 $name = $name.chr(0);
149}
150
151 open OUTFILE, ">$outfile";
152 syswrite(OUTFILE, $name);
153 write_le16($version);
154 write_le16($nr_desc);
155
156 #
157 # Firmware 0, type: BASE FW F8MHZ (0x00000003), id: (0000000000000000), size: 8718
158 #
159
160 write_le32(0x00000003); # Type
161 write_le64(0x00000000, 0x00000000); # ID
162 write_le32(8718); # Size
163 write_hunk_fix_endian(813432, 8718);
164
165 #
166 # Firmware 1, type: BASE FW F8MHZ MTS (0x00000007), id: (0000000000000000), size: 8712
167 #
168
169 write_le32(0x00000007); # Type
170 write_le64(0x00000000, 0x00000000); # ID
171 write_le32(8712); # Size
172 write_hunk_fix_endian(822152, 8712);
173
174 #
175 # Firmware 2, type: BASE FW FM (0x00000401), id: (0000000000000000), size: 8562
176 #
177
178 write_le32(0x00000401); # Type
179 write_le64(0x00000000, 0x00000000); # ID
180 write_le32(8562); # Size
181 write_hunk_fix_endian(830872, 8562);
182
183 #
184 # Firmware 3, type: BASE FW FM INPUT1 (0x00000c01), id: (0000000000000000), size: 8576
185 #
186
187 write_le32(0x00000c01); # Type
188 write_le64(0x00000000, 0x00000000); # ID
189 write_le32(8576); # Size
190 write_hunk_fix_endian(839440, 8576);
191
192 #
193 # Firmware 4, type: BASE FW (0x00000001), id: (0000000000000000), size: 8706
194 #
195
196 write_le32(0x00000001); # Type
197 write_le64(0x00000000, 0x00000000); # ID
198 write_le32(8706); # Size
199 write_hunk_fix_endian(848024, 8706);
200
201 #
202 # Firmware 5, type: BASE FW MTS (0x00000005), id: (0000000000000000), size: 8682
203 #
204
205 write_le32(0x00000005); # Type
206 write_le64(0x00000000, 0x00000000); # ID
207 write_le32(8682); # Size
208 write_hunk_fix_endian(856736, 8682);
209
210 #
211 # Firmware 6, type: STD FW (0x00000000), id: PAL/BG A2/A (0000000100000007), size: 161
212 #
213
214 write_le32(0x00000000); # Type
215 write_le64(0x00000001, 0x00000007); # ID
216 write_le32(161); # Size
217 write_hunk_fix_endian(865424, 161);
218
219 #
220 # Firmware 7, type: STD FW MTS (0x00000004), id: PAL/BG A2/A (0000000100000007), size: 169
221 #
222
223 write_le32(0x00000004); # Type
224 write_le64(0x00000001, 0x00000007); # ID
225 write_le32(169); # Size
226 write_hunk_fix_endian(865592, 169);
227
228 #
229 # Firmware 8, type: STD FW (0x00000000), id: PAL/BG A2/B (0000000200000007), size: 161
230 #
231
232 write_le32(0x00000000); # Type
233 write_le64(0x00000002, 0x00000007); # ID
234 write_le32(161); # Size
235 write_hunk_fix_endian(865424, 161);
236
237 #
238 # Firmware 9, type: STD FW MTS (0x00000004), id: PAL/BG A2/B (0000000200000007), size: 169
239 #
240
241 write_le32(0x00000004); # Type
242 write_le64(0x00000002, 0x00000007); # ID
243 write_le32(169); # Size
244 write_hunk_fix_endian(865592, 169);
245
246 #
247 # Firmware 10, type: STD FW (0x00000000), id: PAL/BG NICAM/A (0000000400000007), size: 161
248 #
249
250 write_le32(0x00000000); # Type
251 write_le64(0x00000004, 0x00000007); # ID
252 write_le32(161); # Size
253 write_hunk_fix_endian(866112, 161);
254
255 #
256 # Firmware 11, type: STD FW MTS (0x00000004), id: PAL/BG NICAM/A (0000000400000007), size: 169
257 #
258
259 write_le32(0x00000004); # Type
260 write_le64(0x00000004, 0x00000007); # ID
261 write_le32(169); # Size
262 write_hunk_fix_endian(866280, 169);
263
264 #
265 # Firmware 12, type: STD FW (0x00000000), id: PAL/BG NICAM/B (0000000800000007), size: 161
266 #
267
268 write_le32(0x00000000); # Type
269 write_le64(0x00000008, 0x00000007); # ID
270 write_le32(161); # Size
271 write_hunk_fix_endian(866112, 161);
272
273 #
274 # Firmware 13, type: STD FW MTS (0x00000004), id: PAL/BG NICAM/B (0000000800000007), size: 169
275 #
276
277 write_le32(0x00000004); # Type
278 write_le64(0x00000008, 0x00000007); # ID
279 write_le32(169); # Size
280 write_hunk_fix_endian(866280, 169);
281
282 #
283 # Firmware 14, type: STD FW (0x00000000), id: PAL/DK A2 (00000003000000e0), size: 161
284 #
285
286 write_le32(0x00000000); # Type
287 write_le64(0x00000003, 0x000000e0); # ID
288 write_le32(161); # Size
289 write_hunk_fix_endian(866800, 161);
290
291 #
292 # Firmware 15, type: STD FW MTS (0x00000004), id: PAL/DK A2 (00000003000000e0), size: 169
293 #
294
295 write_le32(0x00000004); # Type
296 write_le64(0x00000003, 0x000000e0); # ID
297 write_le32(169); # Size
298 write_hunk_fix_endian(866968, 169);
299
300 #
301 # Firmware 16, type: STD FW (0x00000000), id: PAL/DK NICAM (0000000c000000e0), size: 161
302 #
303
304 write_le32(0x00000000); # Type
305 write_le64(0x0000000c, 0x000000e0); # ID
306 write_le32(161); # Size
307 write_hunk_fix_endian(867144, 161);
308
309 #
310 # Firmware 17, type: STD FW MTS (0x00000004), id: PAL/DK NICAM (0000000c000000e0), size: 169
311 #
312
313 write_le32(0x00000004); # Type
314 write_le64(0x0000000c, 0x000000e0); # ID
315 write_le32(169); # Size
316 write_hunk_fix_endian(867312, 169);
317
318 #
319 # Firmware 18, type: STD FW (0x00000000), id: SECAM/K1 (0000000000200000), size: 161
320 #
321
322 write_le32(0x00000000); # Type
323 write_le64(0x00000000, 0x00200000); # ID
324 write_le32(161); # Size
325 write_hunk_fix_endian(867488, 161);
326
327 #
328 # Firmware 19, type: STD FW MTS (0x00000004), id: SECAM/K1 (0000000000200000), size: 169
329 #
330
331 write_le32(0x00000004); # Type
332 write_le64(0x00000000, 0x00200000); # ID
333 write_le32(169); # Size
334 write_hunk_fix_endian(867656, 169);
335
336 #
337 # Firmware 20, type: STD FW (0x00000000), id: SECAM/K3 (0000000004000000), size: 161
338 #
339
340 write_le32(0x00000000); # Type
341 write_le64(0x00000000, 0x04000000); # ID
342 write_le32(161); # Size
343 write_hunk_fix_endian(867832, 161);
344
345 #
346 # Firmware 21, type: STD FW MTS (0x00000004), id: SECAM/K3 (0000000004000000), size: 169
347 #
348
349 write_le32(0x00000004); # Type
350 write_le64(0x00000000, 0x04000000); # ID
351 write_le32(169); # Size
352 write_hunk_fix_endian(868000, 169);
353
354 #
355 # Firmware 22, type: STD FW D2633 DTV6 ATSC (0x00010030), id: (0000000000000000), size: 149
356 #
357
358 write_le32(0x00010030); # Type
359 write_le64(0x00000000, 0x00000000); # ID
360 write_le32(149); # Size
361 write_hunk_fix_endian(868176, 149);
362
363 #
364 # Firmware 23, type: STD FW D2620 DTV6 QAM (0x00000068), id: (0000000000000000), size: 149
365 #
366
367 write_le32(0x00000068); # Type
368 write_le64(0x00000000, 0x00000000); # ID
369 write_le32(149); # Size
370 write_hunk_fix_endian(868336, 149);
371
372 #
373 # Firmware 24, type: STD FW D2633 DTV6 QAM (0x00000070), id: (0000000000000000), size: 149
374 #
375
376 write_le32(0x00000070); # Type
377 write_le64(0x00000000, 0x00000000); # ID
378 write_le32(149); # Size
379 write_hunk_fix_endian(868488, 149);
380
381 #
382 # Firmware 25, type: STD FW D2620 DTV7 (0x00000088), id: (0000000000000000), size: 149
383 #
384
385 write_le32(0x00000088); # Type
386 write_le64(0x00000000, 0x00000000); # ID
387 write_le32(149); # Size
388 write_hunk_fix_endian(868648, 149);
389
390 #
391 # Firmware 26, type: STD FW D2633 DTV7 (0x00000090), id: (0000000000000000), size: 149
392 #
393
394 write_le32(0x00000090); # Type
395 write_le64(0x00000000, 0x00000000); # ID
396 write_le32(149); # Size
397 write_hunk_fix_endian(868800, 149);
398
399 #
400 # Firmware 27, type: STD FW D2620 DTV78 (0x00000108), id: (0000000000000000), size: 149
401 #
402
403 write_le32(0x00000108); # Type
404 write_le64(0x00000000, 0x00000000); # ID
405 write_le32(149); # Size
406 write_hunk_fix_endian(868960, 149);
407
408 #
409 # Firmware 28, type: STD FW D2633 DTV78 (0x00000110), id: (0000000000000000), size: 149
410 #
411
412 write_le32(0x00000110); # Type
413 write_le64(0x00000000, 0x00000000); # ID
414 write_le32(149); # Size
415 write_hunk_fix_endian(869112, 149);
416
417 #
418 # Firmware 29, type: STD FW D2620 DTV8 (0x00000208), id: (0000000000000000), size: 149
419 #
420
421 write_le32(0x00000208); # Type
422 write_le64(0x00000000, 0x00000000); # ID
423 write_le32(149); # Size
424 write_hunk_fix_endian(868648, 149);
425
426 #
427 # Firmware 30, type: STD FW D2633 DTV8 (0x00000210), id: (0000000000000000), size: 149
428 #
429
430 write_le32(0x00000210); # Type
431 write_le64(0x00000000, 0x00000000); # ID
432 write_le32(149); # Size
433 write_hunk_fix_endian(868800, 149);
434
435 #
436 # Firmware 31, type: STD FW FM (0x00000400), id: (0000000000000000), size: 135
437 #
438
439 write_le32(0x00000400); # Type
440 write_le64(0x00000000, 0x00000000); # ID
441 write_le32(135); # Size
442 write_hunk_fix_endian(869584, 135);
443
444 #
445 # Firmware 32, type: STD FW (0x00000000), id: PAL/I (0000000000000010), size: 161
446 #
447
448 write_le32(0x00000000); # Type
449 write_le64(0x00000000, 0x00000010); # ID
450 write_le32(161); # Size
451 write_hunk_fix_endian(869728, 161);
452
453 #
454 # Firmware 33, type: STD FW MTS (0x00000004), id: PAL/I (0000000000000010), size: 169
455 #
456
457 write_le32(0x00000004); # Type
458 write_le64(0x00000000, 0x00000010); # ID
459 write_le32(169); # Size
460 write_hunk_fix_endian(869896, 169);
461
462 #
463 # Firmware 34, type: STD FW (0x00000000), id: SECAM/L AM (0000001000400000), size: 169
464 #
465
466 write_le32(0x00000000); # Type
467 write_le64(0x00000010, 0x00400000); # ID
468 write_le32(169); # Size
469 write_hunk_fix_endian(870072, 169);
470
471 #
472 # Firmware 35, type: STD FW (0x00000000), id: SECAM/L NICAM (0000000c00400000), size: 161
473 #
474
475 write_le32(0x00000000); # Type
476 write_le64(0x0000000c, 0x00400000); # ID
477 write_le32(161); # Size
478 write_hunk_fix_endian(870248, 161);
479
480 #
481 # Firmware 36, type: STD FW (0x00000000), id: SECAM/Lc (0000000000800000), size: 161
482 #
483
484 write_le32(0x00000000); # Type
485 write_le64(0x00000000, 0x00800000); # ID
486 write_le32(161); # Size
487 write_hunk_fix_endian(870416, 161);
488
489 #
490 # Firmware 37, type: STD FW (0x00000000), id: NTSC/M Kr (0000000000008000), size: 161
491 #
492
493 write_le32(0x00000000); # Type
494 write_le64(0x00000000, 0x00008000); # ID
495 write_le32(161); # Size
496 write_hunk_fix_endian(870584, 161);
497
498 #
499 # Firmware 38, type: STD FW LCD (0x00001000), id: NTSC/M Kr (0000000000008000), size: 161
500 #
501
502 write_le32(0x00001000); # Type
503 write_le64(0x00000000, 0x00008000); # ID
504 write_le32(161); # Size
505 write_hunk_fix_endian(870752, 161);
506
507 #
508 # Firmware 39, type: STD FW LCD NOGD (0x00003000), id: NTSC/M Kr (0000000000008000), size: 161
509 #
510
511 write_le32(0x00003000); # Type
512 write_le64(0x00000000, 0x00008000); # ID
513 write_le32(161); # Size
514 write_hunk_fix_endian(870920, 161);
515
516 #
517 # Firmware 40, type: STD FW MTS (0x00000004), id: NTSC/M Kr (0000000000008000), size: 169
518 #
519
520 write_le32(0x00000004); # Type
521 write_le64(0x00000000, 0x00008000); # ID
522 write_le32(169); # Size
523 write_hunk_fix_endian(871088, 169);
524
525 #
526 # Firmware 41, type: STD FW (0x00000000), id: NTSC PAL/M PAL/N (000000000000b700), size: 161
527 #
528
529 write_le32(0x00000000); # Type
530 write_le64(0x00000000, 0x0000b700); # ID
531 write_le32(161); # Size
532 write_hunk_fix_endian(871264, 161);
533
534 #
535 # Firmware 42, type: STD FW LCD (0x00001000), id: NTSC PAL/M PAL/N (000000000000b700), size: 161
536 #
537
538 write_le32(0x00001000); # Type
539 write_le64(0x00000000, 0x0000b700); # ID
540 write_le32(161); # Size
541 write_hunk_fix_endian(871432, 161);
542
543 #
544 # Firmware 43, type: STD FW LCD NOGD (0x00003000), id: NTSC PAL/M PAL/N (000000000000b700), size: 161
545 #
546
547 write_le32(0x00003000); # Type
548 write_le64(0x00000000, 0x0000b700); # ID
549 write_le32(161); # Size
550 write_hunk_fix_endian(871600, 161);
551
552 #
553 # Firmware 44, type: STD FW (0x00000000), id: NTSC/M Jp (0000000000002000), size: 161
554 #
555
556 write_le32(0x00000000); # Type
557 write_le64(0x00000000, 0x00002000); # ID
558 write_le32(161); # Size
559 write_hunk_fix_endian(871264, 161);
560
561 #
562 # Firmware 45, type: STD FW MTS (0x00000004), id: NTSC PAL/M PAL/N (000000000000b700), size: 169
563 #
564
565 write_le32(0x00000004); # Type
566 write_le64(0x00000000, 0x0000b700); # ID
567 write_le32(169); # Size
568 write_hunk_fix_endian(871936, 169);
569
570 #
571 # Firmware 46, type: STD FW MTS LCD (0x00001004), id: NTSC PAL/M PAL/N (000000000000b700), size: 169
572 #
573
574 write_le32(0x00001004); # Type
575 write_le64(0x00000000, 0x0000b700); # ID
576 write_le32(169); # Size
577 write_hunk_fix_endian(872112, 169);
578
579 #
580 # Firmware 47, type: STD FW MTS LCD NOGD (0x00003004), id: NTSC PAL/M PAL/N (000000000000b700), size: 169
581 #
582
583 write_le32(0x00003004); # Type
584 write_le64(0x00000000, 0x0000b700); # ID
585 write_le32(169); # Size
586 write_hunk_fix_endian(872288, 169);
587
588 #
589 # Firmware 48, type: SCODE FW HAS IF (0x60000000), IF = 3.28 MHz id: (0000000000000000), size: 192
590 #
591
592 write_le32(0x60000000); # Type
593 write_le64(0x00000000, 0x00000000); # ID
594 write_le16(3280); # IF
595 write_le32(192); # Size
596 write_hunk(811896, 192);
597
598 #
599 # Firmware 49, type: SCODE FW HAS IF (0x60000000), IF = 3.30 MHz id: (0000000000000000), size: 192
600 #
601
602 write_le32(0x60000000); # Type
603 write_le64(0x00000000, 0x00000000); # ID
604 write_le16(3300); # IF
605 write_le32(192); # Size
606 write_hunk(813048, 192);
607
608 #
609 # Firmware 50, type: SCODE FW HAS IF (0x60000000), IF = 3.44 MHz id: (0000000000000000), size: 192
610 #
611
612 write_le32(0x60000000); # Type
613 write_le64(0x00000000, 0x00000000); # ID
614 write_le16(3440); # IF
615 write_le32(192); # Size
616 write_hunk(812280, 192);
617
618 #
619 # Firmware 51, type: SCODE FW HAS IF (0x60000000), IF = 3.46 MHz id: (0000000000000000), size: 192
620 #
621
622 write_le32(0x60000000); # Type
623 write_le64(0x00000000, 0x00000000); # ID
624 write_le16(3460); # IF
625 write_le32(192); # Size
626 write_hunk(812472, 192);
627
628 #
629 # Firmware 52, type: SCODE FW DTV6 ATSC OREN36 HAS IF (0x60210020), IF = 3.80 MHz id: (0000000000000000), size: 192
630 #
631
632 write_le32(0x60210020); # Type
633 write_le64(0x00000000, 0x00000000); # ID
634 write_le16(3800); # IF
635 write_le32(192); # Size
636 write_hunk(809784, 192);
637
638 #
639 # Firmware 53, type: SCODE FW HAS IF (0x60000000), IF = 4.00 MHz id: (0000000000000000), size: 192
640 #
641
642 write_le32(0x60000000); # Type
643 write_le64(0x00000000, 0x00000000); # ID
644 write_le16(4000); # IF
645 write_le32(192); # Size
646 write_hunk(812088, 192);
647
648 #
649 # Firmware 54, type: SCODE FW DTV6 ATSC TOYOTA388 HAS IF (0x60410020), IF = 4.08 MHz id: (0000000000000000), size: 192
650 #
651
652 write_le32(0x60410020); # Type
653 write_le64(0x00000000, 0x00000000); # ID
654 write_le16(4080); # IF
655 write_le32(192); # Size
656 write_hunk(809976, 192);
657
658 #
659 # Firmware 55, type: SCODE FW HAS IF (0x60000000), IF = 4.20 MHz id: (0000000000000000), size: 192
660 #
661
662 write_le32(0x60000000); # Type
663 write_le64(0x00000000, 0x00000000); # ID
664 write_le16(4200); # IF
665 write_le32(192); # Size
666 write_hunk(811704, 192);
667
668 #
669 # Firmware 56, type: SCODE FW MONO HAS IF (0x60008000), IF = 4.32 MHz id: NTSC/M Kr (0000000000008000), size: 192
670 #
671
672 write_le32(0x60008000); # Type
673 write_le64(0x00000000, 0x00008000); # ID
674 write_le16(4320); # IF
675 write_le32(192); # Size
676 write_hunk(808056, 192);
677
678 #
679 # Firmware 57, type: SCODE FW HAS IF (0x60000000), IF = 4.45 MHz id: (0000000000000000), size: 192
680 #
681
682 write_le32(0x60000000); # Type
683 write_le64(0x00000000, 0x00000000); # ID
684 write_le16(4450); # IF
685 write_le32(192); # Size
686 write_hunk(812664, 192);
687
688 #
689 # Firmware 58, type: SCODE FW HAS IF (0x60000000), IF = 4.50 MHz id: NTSC/M Jp (0000000000002000), size: 192
690 #
691
692 write_le32(0x60000000); # Type
693 write_le64(0x00000000, 0x00002000); # ID
694 write_le16(4500); # IF
695 write_le32(192); # Size
696 write_hunk(807672, 192);
697
698 #
699 # Firmware 59, type: SCODE FW LCD NOGD IF HAS IF (0x60023000), IF = 4.60 MHz id: NTSC/M Kr (0000000000008000), size: 192
700 #
701
702 write_le32(0x60023000); # Type
703 write_le64(0x00000000, 0x00008000); # ID
704 write_le16(4600); # IF
705 write_le32(192); # Size
706 write_hunk(807864, 192);
707
708 #
709 # Firmware 60, type: SCODE FW DTV78 ZARLINK456 HAS IF (0x62000100), IF = 4.76 MHz id: (0000000000000000), size: 192
710 #
711
712 write_le32(0x62000100); # Type
713 write_le64(0x00000000, 0x00000000); # ID
714 write_le16(4760); # IF
715 write_le32(192); # Size
716 write_hunk(807288, 192);
717
718 #
719 # Firmware 61, type: SCODE FW HAS IF (0x60000000), IF = 4.94 MHz id: (0000000000000000), size: 192
720 #
721
722 write_le32(0x60000000); # Type
723 write_le64(0x00000000, 0x00000000); # ID
724 write_le16(4940); # IF
725 write_le32(192); # Size
726 write_hunk(811512, 192);
727
728 #
729 # Firmware 62, type: SCODE FW DTV7 ZARLINK456 HAS IF (0x62000080), IF = 5.26 MHz id: (0000000000000000), size: 192
730 #
731
732 write_le32(0x62000080); # Type
733 write_le64(0x00000000, 0x00000000); # ID
734 write_le16(5260); # IF
735 write_le32(192); # Size
736 write_hunk(810552, 192);
737
738 #
739 # Firmware 63, type: SCODE FW MONO HAS IF (0x60008000), IF = 5.32 MHz id: PAL/BG NICAM/B (0000000800000007), size: 192
740 #
741
742 write_le32(0x60008000); # Type
743 write_le64(0x00000008, 0x00000007); # ID
744 write_le16(5320); # IF
745 write_le32(192); # Size
746 write_hunk(810744, 192);
747
748 #
749 # Firmware 64, type: SCODE FW DTV8 CHINA HAS IF (0x64000200), IF = 5.40 MHz id: (0000000000000000), size: 192
750 #
751
752 write_le32(0x64000200); # Type
753 write_le64(0x00000000, 0x00000000); # ID
754 write_le16(5400); # IF
755 write_le32(192); # Size
756 write_hunk(807096, 192);
757
758 #
759 # Firmware 65, type: SCODE FW DTV6 ATSC OREN538 HAS IF (0x60110020), IF = 5.58 MHz id: (0000000000000000), size: 192
760 #
761
762 write_le32(0x60110020); # Type
763 write_le64(0x00000000, 0x00000000); # ID
764 write_le16(5580); # IF
765 write_le32(192); # Size
766 write_hunk(809592, 192);
767
768 #
769 # Firmware 66, type: SCODE FW HAS IF (0x60000000), IF = 5.64 MHz id: PAL/BG A2/B (0000000200000007), size: 192
770 #
771
772 write_le32(0x60000000); # Type
773 write_le64(0x00000002, 0x00000007); # ID
774 write_le16(5640); # IF
775 write_le32(192); # Size
776 write_hunk(808440, 192);
777
778 #
779 # Firmware 67, type: SCODE FW HAS IF (0x60000000), IF = 5.74 MHz id: PAL/BG NICAM/B (0000000800000007), size: 192
780 #
781
782 write_le32(0x60000000); # Type
783 write_le64(0x00000008, 0x00000007); # ID
784 write_le16(5740); # IF
785 write_le32(192); # Size
786 write_hunk(808632, 192);
787
788 #
789 # Firmware 68, type: SCODE FW DTV7 DIBCOM52 HAS IF (0x61000080), IF = 5.90 MHz id: (0000000000000000), size: 192
790 #
791
792 write_le32(0x61000080); # Type
793 write_le64(0x00000000, 0x00000000); # ID
794 write_le16(5900); # IF
795 write_le32(192); # Size
796 write_hunk(810360, 192);
797
798 #
799 # Firmware 69, type: SCODE FW MONO HAS IF (0x60008000), IF = 6.00 MHz id: PAL/I (0000000000000010), size: 192
800 #
801
802 write_le32(0x60008000); # Type
803 write_le64(0x00000000, 0x00000010); # ID
804 write_le16(6000); # IF
805 write_le32(192); # Size
806 write_hunk(808824, 192);
807
808 #
809 # Firmware 70, type: SCODE FW DTV6 QAM F6MHZ HAS IF (0x68000060), IF = 6.20 MHz id: (0000000000000000), size: 192
810 #
811
812 write_le32(0x68000060); # Type
813 write_le64(0x00000000, 0x00000000); # ID
814 write_le16(6200); # IF
815 write_le32(192); # Size
816 write_hunk(809400, 192);
817
818 #
819 # Firmware 71, type: SCODE FW HAS IF (0x60000000), IF = 6.24 MHz id: PAL/I (0000000000000010), size: 192
820 #
821
822 write_le32(0x60000000); # Type
823 write_le64(0x00000000, 0x00000010); # ID
824 write_le16(6240); # IF
825 write_le32(192); # Size
826 write_hunk(808248, 192);
827
828 #
829 # Firmware 72, type: SCODE FW MONO HAS IF (0x60008000), IF = 6.32 MHz id: SECAM/K1 (0000000000200000), size: 192
830 #
831
832 write_le32(0x60008000); # Type
833 write_le64(0x00000000, 0x00200000); # ID
834 write_le16(6320); # IF
835 write_le32(192); # Size
836 write_hunk(811320, 192);
837
838 #
839 # Firmware 73, type: SCODE FW HAS IF (0x60000000), IF = 6.34 MHz id: SECAM/K1 (0000000000200000), size: 192
840 #
841
842 write_le32(0x60000000); # Type
843 write_le64(0x00000000, 0x00200000); # ID
844 write_le16(6340); # IF
845 write_le32(192); # Size
846 write_hunk(809208, 192);
847
848 #
849 # Firmware 74, type: SCODE FW MONO HAS IF (0x60008000), IF = 6.50 MHz id: SECAM/K3 (0000000004000000), size: 192
850 #
851
852 write_le32(0x60008000); # Type
853 write_le64(0x00000000, 0x04000000); # ID
854 write_le16(6500); # IF
855 write_le32(192); # Size
856 write_hunk(811128, 192);
857
858 #
859 # Firmware 75, type: SCODE FW DTV6 ATSC ATI638 HAS IF (0x60090020), IF = 6.58 MHz id: (0000000000000000), size: 192
860 #
861
862 write_le32(0x60090020); # Type
863 write_le64(0x00000000, 0x00000000); # ID
864 write_le16(6580); # IF
865 write_le32(192); # Size
866 write_hunk(807480, 192);
867
868 #
869 # Firmware 76, type: SCODE FW HAS IF (0x60000000), IF = 6.60 MHz id: PAL/DK A2 (00000003000000e0), size: 192
870 #
871
872 write_le32(0x60000000); # Type
873 write_le64(0x00000003, 0x000000e0); # ID
874 write_le16(6600); # IF
875 write_le32(192); # Size
876 write_hunk(809016, 192);
877
878 #
879 # Firmware 77, type: SCODE FW MONO HAS IF (0x60008000), IF = 6.68 MHz id: PAL/DK A2 (00000003000000e0), size: 192
880 #
881
882 write_le32(0x60008000); # Type
883 write_le64(0x00000003, 0x000000e0); # ID
884 write_le16(6680); # IF
885 write_le32(192); # Size
886 write_hunk(810936, 192);
887
888 #
889 # Firmware 78, type: SCODE FW DTV6 ATSC TOYOTA794 HAS IF (0x60810020), IF = 8.14 MHz id: (0000000000000000), size: 192
890 #
891
892 write_le32(0x60810020); # Type
893 write_le64(0x00000000, 0x00000000); # ID
894 write_le16(8140); # IF
895 write_le32(192); # Size
896 write_hunk(810168, 192);
897
898 #
899 # Firmware 79, type: SCODE FW HAS IF (0x60000000), IF = 8.20 MHz id: (0000000000000000), size: 192
900 #
901
902 write_le32(0x60000000); # Type
903 write_le64(0x00000000, 0x00000000); # ID
904 write_le16(8200); # IF
905 write_le32(192); # Size
906 write_hunk(812856, 192);
907}
908
909sub extract_firmware {
910 my $sourcefile = "hcw85bda.sys";
911 my $hash = "0e44dbf63bb0169d57446aec21881ff2";
912 my $outfile = "xc3028-v27.fw";
913 my $name = "xc2028 firmware";
914 my $version = 519;
915 my $nr_desc = 80;
916 my $out;
917
918 verify($sourcefile, $hash);
919
920 open INFILE, "<$sourcefile";
921 main_firmware($outfile, $name, $version, $nr_desc);
922 close INFILE;
923}
924
925extract_firmware;
926printf "Firmwares generated.\n";
diff --git a/Documentation/video4linux/sn9c102.txt b/Documentation/video4linux/sn9c102.txt
index 1ffad19ce891..b26f5195af51 100644
--- a/Documentation/video4linux/sn9c102.txt
+++ b/Documentation/video4linux/sn9c102.txt
@@ -568,6 +568,7 @@ the fingerprint is: '88E8 F32F 7244 68BA 3958 5D40 99DA 5D2A FCE6 35A4'.
568Many thanks to following persons for their contribute (listed in alphabetical 568Many thanks to following persons for their contribute (listed in alphabetical
569order): 569order):
570 570
571- David Anderson for the donation of a webcam;
571- Luca Capello for the donation of a webcam; 572- Luca Capello for the donation of a webcam;
572- Philippe Coval for having helped testing the PAS202BCA image sensor; 573- Philippe Coval for having helped testing the PAS202BCA image sensor;
573- Joao Rodrigo Fuzaro, Joao Limirio, Claudio Filho and Caio Begotti for the 574- Joao Rodrigo Fuzaro, Joao Limirio, Claudio Filho and Caio Begotti for the
diff --git a/MAINTAINERS b/MAINTAINERS
index 2340cfb1e25d..17524afa7475 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2141,6 +2141,15 @@ L: isdn4linux@listserv.isdn4linux.de
2141W: http://www.melware.de 2141W: http://www.melware.de
2142S: Maintained 2142S: Maintained
2143 2143
2144IVTV VIDEO4LINUX DRIVER
2145P: Hans Verkuil
2146M: hverkuil@xs4all.nl
2147L: ivtv-devel@ivtvdriver.org
2148L: ivtv-users@ivtvdriver.org
2149L: video4linux-list@redhat.com
2150W: http://www.ivtvdriver.org
2151S: Maintained
2152
2144JOURNALLING FLASH FILE SYSTEM V2 (JFFS2) 2153JOURNALLING FLASH FILE SYSTEM V2 (JFFS2)
2145P: David Woodhouse 2154P: David Woodhouse
2146M: dwmw2@infradead.org 2155M: dwmw2@infradead.org
diff --git a/arch/arm/kernel/time.c b/arch/arm/kernel/time.c
index f6f3689a86ee..e59b5b84168d 100644
--- a/arch/arm/kernel/time.c
+++ b/arch/arm/kernel/time.c
@@ -79,17 +79,6 @@ static unsigned long dummy_gettimeoffset(void)
79} 79}
80#endif 80#endif
81 81
82/*
83 * An implementation of printk_clock() independent from
84 * sched_clock(). This avoids non-bootable kernels when
85 * printk_clock is enabled.
86 */
87unsigned long long printk_clock(void)
88{
89 return (unsigned long long)(jiffies - INITIAL_JIFFIES) *
90 (1000000000 / HZ);
91}
92
93static unsigned long next_rtc_update; 82static unsigned long next_rtc_update;
94 83
95/* 84/*
diff --git a/arch/ia64/kernel/setup.c b/arch/ia64/kernel/setup.c
index 4ac2b1f1bd3b..86028c69861e 100644
--- a/arch/ia64/kernel/setup.c
+++ b/arch/ia64/kernel/setup.c
@@ -71,8 +71,6 @@ unsigned long __per_cpu_offset[NR_CPUS];
71EXPORT_SYMBOL(__per_cpu_offset); 71EXPORT_SYMBOL(__per_cpu_offset);
72#endif 72#endif
73 73
74extern void ia64_setup_printk_clock(void);
75
76DEFINE_PER_CPU(struct cpuinfo_ia64, cpu_info); 74DEFINE_PER_CPU(struct cpuinfo_ia64, cpu_info);
77DEFINE_PER_CPU(unsigned long, local_per_cpu_offset); 75DEFINE_PER_CPU(unsigned long, local_per_cpu_offset);
78unsigned long ia64_cycles_per_usec; 76unsigned long ia64_cycles_per_usec;
@@ -507,8 +505,6 @@ setup_arch (char **cmdline_p)
507 /* process SAL system table: */ 505 /* process SAL system table: */
508 ia64_sal_init(__va(efi.sal_systab)); 506 ia64_sal_init(__va(efi.sal_systab));
509 507
510 ia64_setup_printk_clock();
511
512#ifdef CONFIG_SMP 508#ifdef CONFIG_SMP
513 cpu_physical_id(0) = hard_smp_processor_id(); 509 cpu_physical_id(0) = hard_smp_processor_id();
514#endif 510#endif
diff --git a/arch/ia64/kernel/time.c b/arch/ia64/kernel/time.c
index 2bb84214e5f1..3ab042720970 100644
--- a/arch/ia64/kernel/time.c
+++ b/arch/ia64/kernel/time.c
@@ -344,33 +344,6 @@ udelay (unsigned long usecs)
344} 344}
345EXPORT_SYMBOL(udelay); 345EXPORT_SYMBOL(udelay);
346 346
347static unsigned long long ia64_itc_printk_clock(void)
348{
349 if (ia64_get_kr(IA64_KR_PER_CPU_DATA))
350 return sched_clock();
351 return 0;
352}
353
354static unsigned long long ia64_default_printk_clock(void)
355{
356 return (unsigned long long)(jiffies_64 - INITIAL_JIFFIES) *
357 (1000000000/HZ);
358}
359
360unsigned long long (*ia64_printk_clock)(void) = &ia64_default_printk_clock;
361
362unsigned long long printk_clock(void)
363{
364 return ia64_printk_clock();
365}
366
367void __init
368ia64_setup_printk_clock(void)
369{
370 if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT))
371 ia64_printk_clock = ia64_itc_printk_clock;
372}
373
374/* IA64 doesn't cache the timezone */ 347/* IA64 doesn't cache the timezone */
375void update_vsyscall_tz(void) 348void update_vsyscall_tz(void)
376{ 349{
diff --git a/arch/ia64/sn/kernel/setup.c b/arch/ia64/sn/kernel/setup.c
index 1f38a3a68390..bb1d24929640 100644
--- a/arch/ia64/sn/kernel/setup.c
+++ b/arch/ia64/sn/kernel/setup.c
@@ -64,7 +64,6 @@ extern void sn_timer_init(void);
64extern unsigned long last_time_offset; 64extern unsigned long last_time_offset;
65extern void (*ia64_mark_idle) (int); 65extern void (*ia64_mark_idle) (int);
66extern void snidle(int); 66extern void snidle(int);
67extern unsigned long long (*ia64_printk_clock)(void);
68 67
69unsigned long sn_rtc_cycles_per_second; 68unsigned long sn_rtc_cycles_per_second;
70EXPORT_SYMBOL(sn_rtc_cycles_per_second); 69EXPORT_SYMBOL(sn_rtc_cycles_per_second);
@@ -360,14 +359,6 @@ sn_scan_pcdp(void)
360 359
361static unsigned long sn2_rtc_initial; 360static unsigned long sn2_rtc_initial;
362 361
363static unsigned long long ia64_sn2_printk_clock(void)
364{
365 unsigned long rtc_now = rtc_time();
366
367 return (rtc_now - sn2_rtc_initial) *
368 (1000000000 / sn_rtc_cycles_per_second);
369}
370
371/** 362/**
372 * sn_setup - SN platform setup routine 363 * sn_setup - SN platform setup routine
373 * @cmdline_p: kernel command line 364 * @cmdline_p: kernel command line
@@ -468,8 +459,6 @@ void __init sn_setup(char **cmdline_p)
468 459
469 platform_intr_list[ACPI_INTERRUPT_CPEI] = IA64_CPE_VECTOR; 460 platform_intr_list[ACPI_INTERRUPT_CPEI] = IA64_CPE_VECTOR;
470 461
471 ia64_printk_clock = ia64_sn2_printk_clock;
472
473 printk("SGI SAL version %x.%02x\n", version >> 8, version & 0x00FF); 462 printk("SGI SAL version %x.%02x\n", version >> 8, version & 0x00FF);
474 463
475 /* 464 /*
diff --git a/arch/mips/kernel/mips-mt-fpaff.c b/arch/mips/kernel/mips-mt-fpaff.c
index 892665bb12b1..bb4f00c0cbe9 100644
--- a/arch/mips/kernel/mips-mt-fpaff.c
+++ b/arch/mips/kernel/mips-mt-fpaff.c
@@ -58,13 +58,13 @@ asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
58 if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask))) 58 if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
59 return -EFAULT; 59 return -EFAULT;
60 60
61 lock_cpu_hotplug(); 61 get_online_cpus();
62 read_lock(&tasklist_lock); 62 read_lock(&tasklist_lock);
63 63
64 p = find_process_by_pid(pid); 64 p = find_process_by_pid(pid);
65 if (!p) { 65 if (!p) {
66 read_unlock(&tasklist_lock); 66 read_unlock(&tasklist_lock);
67 unlock_cpu_hotplug(); 67 put_online_cpus();
68 return -ESRCH; 68 return -ESRCH;
69 } 69 }
70 70
@@ -106,7 +106,7 @@ asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
106 106
107out_unlock: 107out_unlock:
108 put_task_struct(p); 108 put_task_struct(p);
109 unlock_cpu_hotplug(); 109 put_online_cpus();
110 return retval; 110 return retval;
111} 111}
112 112
@@ -125,7 +125,7 @@ asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
125 if (len < real_len) 125 if (len < real_len)
126 return -EINVAL; 126 return -EINVAL;
127 127
128 lock_cpu_hotplug(); 128 get_online_cpus();
129 read_lock(&tasklist_lock); 129 read_lock(&tasklist_lock);
130 130
131 retval = -ESRCH; 131 retval = -ESRCH;
@@ -140,7 +140,7 @@ asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
140 140
141out_unlock: 141out_unlock:
142 read_unlock(&tasklist_lock); 142 read_unlock(&tasklist_lock);
143 unlock_cpu_hotplug(); 143 put_online_cpus();
144 if (retval) 144 if (retval)
145 return retval; 145 return retval;
146 if (copy_to_user(user_mask_ptr, &mask, real_len)) 146 if (copy_to_user(user_mask_ptr, &mask, real_len))
diff --git a/arch/powerpc/platforms/pseries/hotplug-cpu.c b/arch/powerpc/platforms/pseries/hotplug-cpu.c
index 412e6b42986f..c4ad54e0f288 100644
--- a/arch/powerpc/platforms/pseries/hotplug-cpu.c
+++ b/arch/powerpc/platforms/pseries/hotplug-cpu.c
@@ -153,7 +153,7 @@ static int pseries_add_processor(struct device_node *np)
153 for (i = 0; i < nthreads; i++) 153 for (i = 0; i < nthreads; i++)
154 cpu_set(i, tmp); 154 cpu_set(i, tmp);
155 155
156 lock_cpu_hotplug(); 156 cpu_maps_update_begin();
157 157
158 BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map)); 158 BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map));
159 159
@@ -190,7 +190,7 @@ static int pseries_add_processor(struct device_node *np)
190 } 190 }
191 err = 0; 191 err = 0;
192out_unlock: 192out_unlock:
193 unlock_cpu_hotplug(); 193 cpu_maps_update_done();
194 return err; 194 return err;
195} 195}
196 196
@@ -211,7 +211,7 @@ static void pseries_remove_processor(struct device_node *np)
211 211
212 nthreads = len / sizeof(u32); 212 nthreads = len / sizeof(u32);
213 213
214 lock_cpu_hotplug(); 214 cpu_maps_update_begin();
215 for (i = 0; i < nthreads; i++) { 215 for (i = 0; i < nthreads; i++) {
216 for_each_present_cpu(cpu) { 216 for_each_present_cpu(cpu) {
217 if (get_hard_smp_processor_id(cpu) != intserv[i]) 217 if (get_hard_smp_processor_id(cpu) != intserv[i])
@@ -225,7 +225,7 @@ static void pseries_remove_processor(struct device_node *np)
225 printk(KERN_WARNING "Could not find cpu to remove " 225 printk(KERN_WARNING "Could not find cpu to remove "
226 "with physical id 0x%x\n", intserv[i]); 226 "with physical id 0x%x\n", intserv[i]);
227 } 227 }
228 unlock_cpu_hotplug(); 228 cpu_maps_update_done();
229} 229}
230 230
231static int pseries_smp_notifier(struct notifier_block *nb, 231static int pseries_smp_notifier(struct notifier_block *nb,
diff --git a/arch/powerpc/platforms/pseries/rtasd.c b/arch/powerpc/platforms/pseries/rtasd.c
index 73401c820110..e3078ce41518 100644
--- a/arch/powerpc/platforms/pseries/rtasd.c
+++ b/arch/powerpc/platforms/pseries/rtasd.c
@@ -382,7 +382,7 @@ static void do_event_scan_all_cpus(long delay)
382{ 382{
383 int cpu; 383 int cpu;
384 384
385 lock_cpu_hotplug(); 385 get_online_cpus();
386 cpu = first_cpu(cpu_online_map); 386 cpu = first_cpu(cpu_online_map);
387 for (;;) { 387 for (;;) {
388 set_cpus_allowed(current, cpumask_of_cpu(cpu)); 388 set_cpus_allowed(current, cpumask_of_cpu(cpu));
@@ -390,15 +390,15 @@ static void do_event_scan_all_cpus(long delay)
390 set_cpus_allowed(current, CPU_MASK_ALL); 390 set_cpus_allowed(current, CPU_MASK_ALL);
391 391
392 /* Drop hotplug lock, and sleep for the specified delay */ 392 /* Drop hotplug lock, and sleep for the specified delay */
393 unlock_cpu_hotplug(); 393 put_online_cpus();
394 msleep_interruptible(delay); 394 msleep_interruptible(delay);
395 lock_cpu_hotplug(); 395 get_online_cpus();
396 396
397 cpu = next_cpu(cpu, cpu_online_map); 397 cpu = next_cpu(cpu, cpu_online_map);
398 if (cpu == NR_CPUS) 398 if (cpu == NR_CPUS)
399 break; 399 break;
400 } 400 }
401 unlock_cpu_hotplug(); 401 put_online_cpus();
402} 402}
403 403
404static int rtasd(void *unused) 404static int rtasd(void *unused)
diff --git a/arch/x86/kernel/cpu/mtrr/main.c b/arch/x86/kernel/cpu/mtrr/main.c
index 3b20613325dc..beb45c9c0835 100644
--- a/arch/x86/kernel/cpu/mtrr/main.c
+++ b/arch/x86/kernel/cpu/mtrr/main.c
@@ -349,7 +349,7 @@ int mtrr_add_page(unsigned long base, unsigned long size,
349 replace = -1; 349 replace = -1;
350 350
351 /* No CPU hotplug when we change MTRR entries */ 351 /* No CPU hotplug when we change MTRR entries */
352 lock_cpu_hotplug(); 352 get_online_cpus();
353 /* Search for existing MTRR */ 353 /* Search for existing MTRR */
354 mutex_lock(&mtrr_mutex); 354 mutex_lock(&mtrr_mutex);
355 for (i = 0; i < num_var_ranges; ++i) { 355 for (i = 0; i < num_var_ranges; ++i) {
@@ -405,7 +405,7 @@ int mtrr_add_page(unsigned long base, unsigned long size,
405 error = i; 405 error = i;
406 out: 406 out:
407 mutex_unlock(&mtrr_mutex); 407 mutex_unlock(&mtrr_mutex);
408 unlock_cpu_hotplug(); 408 put_online_cpus();
409 return error; 409 return error;
410} 410}
411 411
@@ -495,7 +495,7 @@ int mtrr_del_page(int reg, unsigned long base, unsigned long size)
495 495
496 max = num_var_ranges; 496 max = num_var_ranges;
497 /* No CPU hotplug when we change MTRR entries */ 497 /* No CPU hotplug when we change MTRR entries */
498 lock_cpu_hotplug(); 498 get_online_cpus();
499 mutex_lock(&mtrr_mutex); 499 mutex_lock(&mtrr_mutex);
500 if (reg < 0) { 500 if (reg < 0) {
501 /* Search for existing MTRR */ 501 /* Search for existing MTRR */
@@ -536,7 +536,7 @@ int mtrr_del_page(int reg, unsigned long base, unsigned long size)
536 error = reg; 536 error = reg;
537 out: 537 out:
538 mutex_unlock(&mtrr_mutex); 538 mutex_unlock(&mtrr_mutex);
539 unlock_cpu_hotplug(); 539 put_online_cpus();
540 return error; 540 return error;
541} 541}
542/** 542/**
diff --git a/arch/x86/kernel/entry_64.S b/arch/x86/kernel/entry_64.S
index 3a058bb16409..e70f3881d7e4 100644
--- a/arch/x86/kernel/entry_64.S
+++ b/arch/x86/kernel/entry_64.S
@@ -283,7 +283,7 @@ sysret_careful:
283sysret_signal: 283sysret_signal:
284 TRACE_IRQS_ON 284 TRACE_IRQS_ON
285 sti 285 sti
286 testl $(_TIF_SIGPENDING|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY),%edx 286 testl $_TIF_DO_NOTIFY_MASK,%edx
287 jz 1f 287 jz 1f
288 288
289 /* Really a signal */ 289 /* Really a signal */
@@ -377,7 +377,7 @@ int_very_careful:
377 jmp int_restore_rest 377 jmp int_restore_rest
378 378
379int_signal: 379int_signal:
380 testl $(_TIF_SIGPENDING|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY),%edx 380 testl $_TIF_DO_NOTIFY_MASK,%edx
381 jz 1f 381 jz 1f
382 movq %rsp,%rdi # &ptregs -> arg1 382 movq %rsp,%rdi # &ptregs -> arg1
383 xorl %esi,%esi # oldset -> arg2 383 xorl %esi,%esi # oldset -> arg2
@@ -603,7 +603,7 @@ retint_careful:
603 jmp retint_check 603 jmp retint_check
604 604
605retint_signal: 605retint_signal:
606 testl $(_TIF_SIGPENDING|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY),%edx 606 testl $_TIF_DO_NOTIFY_MASK,%edx
607 jz retint_swapgs 607 jz retint_swapgs
608 TRACE_IRQS_ON 608 TRACE_IRQS_ON
609 sti 609 sti
diff --git a/arch/x86/kernel/microcode.c b/arch/x86/kernel/microcode.c
index 09c315214a5e..40cfd5488719 100644
--- a/arch/x86/kernel/microcode.c
+++ b/arch/x86/kernel/microcode.c
@@ -436,7 +436,7 @@ static ssize_t microcode_write (struct file *file, const char __user *buf, size_
436 return -EINVAL; 436 return -EINVAL;
437 } 437 }
438 438
439 lock_cpu_hotplug(); 439 get_online_cpus();
440 mutex_lock(&microcode_mutex); 440 mutex_lock(&microcode_mutex);
441 441
442 user_buffer = (void __user *) buf; 442 user_buffer = (void __user *) buf;
@@ -447,7 +447,7 @@ static ssize_t microcode_write (struct file *file, const char __user *buf, size_
447 ret = (ssize_t)len; 447 ret = (ssize_t)len;
448 448
449 mutex_unlock(&microcode_mutex); 449 mutex_unlock(&microcode_mutex);
450 unlock_cpu_hotplug(); 450 put_online_cpus();
451 451
452 return ret; 452 return ret;
453} 453}
@@ -658,14 +658,14 @@ static ssize_t reload_store(struct sys_device *dev, const char *buf, size_t sz)
658 658
659 old = current->cpus_allowed; 659 old = current->cpus_allowed;
660 660
661 lock_cpu_hotplug(); 661 get_online_cpus();
662 set_cpus_allowed(current, cpumask_of_cpu(cpu)); 662 set_cpus_allowed(current, cpumask_of_cpu(cpu));
663 663
664 mutex_lock(&microcode_mutex); 664 mutex_lock(&microcode_mutex);
665 if (uci->valid) 665 if (uci->valid)
666 err = cpu_request_microcode(cpu); 666 err = cpu_request_microcode(cpu);
667 mutex_unlock(&microcode_mutex); 667 mutex_unlock(&microcode_mutex);
668 unlock_cpu_hotplug(); 668 put_online_cpus();
669 set_cpus_allowed(current, old); 669 set_cpus_allowed(current, old);
670 } 670 }
671 if (err) 671 if (err)
@@ -817,9 +817,9 @@ static int __init microcode_init (void)
817 return PTR_ERR(microcode_pdev); 817 return PTR_ERR(microcode_pdev);
818 } 818 }
819 819
820 lock_cpu_hotplug(); 820 get_online_cpus();
821 error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver); 821 error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
822 unlock_cpu_hotplug(); 822 put_online_cpus();
823 if (error) { 823 if (error) {
824 microcode_dev_exit(); 824 microcode_dev_exit();
825 platform_device_unregister(microcode_pdev); 825 platform_device_unregister(microcode_pdev);
@@ -839,9 +839,9 @@ static void __exit microcode_exit (void)
839 839
840 unregister_hotcpu_notifier(&mc_cpu_notifier); 840 unregister_hotcpu_notifier(&mc_cpu_notifier);
841 841
842 lock_cpu_hotplug(); 842 get_online_cpus();
843 sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver); 843 sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
844 unlock_cpu_hotplug(); 844 put_online_cpus();
845 845
846 platform_device_unregister(microcode_pdev); 846 platform_device_unregister(microcode_pdev);
847} 847}
diff --git a/arch/x86/kernel/signal_32.c b/arch/x86/kernel/signal_32.c
index 9bdd83022f5f..20f29e4c1d33 100644
--- a/arch/x86/kernel/signal_32.c
+++ b/arch/x86/kernel/signal_32.c
@@ -658,6 +658,9 @@ void do_notify_resume(struct pt_regs *regs, void *_unused,
658 /* deal with pending signal delivery */ 658 /* deal with pending signal delivery */
659 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK)) 659 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
660 do_signal(regs); 660 do_signal(regs);
661
662 if (thread_info_flags & _TIF_HRTICK_RESCHED)
663 hrtick_resched();
661 664
662 clear_thread_flag(TIF_IRET); 665 clear_thread_flag(TIF_IRET);
663} 666}
diff --git a/arch/x86/kernel/signal_64.c b/arch/x86/kernel/signal_64.c
index ab086b0357fc..38d806467c0f 100644
--- a/arch/x86/kernel/signal_64.c
+++ b/arch/x86/kernel/signal_64.c
@@ -480,6 +480,9 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
480 /* deal with pending signal delivery */ 480 /* deal with pending signal delivery */
481 if (thread_info_flags & (_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK)) 481 if (thread_info_flags & (_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK))
482 do_signal(regs); 482 do_signal(regs);
483
484 if (thread_info_flags & _TIF_HRTICK_RESCHED)
485 hrtick_resched();
483} 486}
484 487
485void signal_fault(struct pt_regs *regs, void __user *frame, char *where) 488void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
diff --git a/arch/x86/kernel/stacktrace.c b/arch/x86/kernel/stacktrace.c
index 6fa6cf036c70..55771fd7e545 100644
--- a/arch/x86/kernel/stacktrace.c
+++ b/arch/x86/kernel/stacktrace.c
@@ -33,6 +33,19 @@ static void save_stack_address(void *data, unsigned long addr)
33 trace->entries[trace->nr_entries++] = addr; 33 trace->entries[trace->nr_entries++] = addr;
34} 34}
35 35
36static void save_stack_address_nosched(void *data, unsigned long addr)
37{
38 struct stack_trace *trace = (struct stack_trace *)data;
39 if (in_sched_functions(addr))
40 return;
41 if (trace->skip > 0) {
42 trace->skip--;
43 return;
44 }
45 if (trace->nr_entries < trace->max_entries)
46 trace->entries[trace->nr_entries++] = addr;
47}
48
36static const struct stacktrace_ops save_stack_ops = { 49static const struct stacktrace_ops save_stack_ops = {
37 .warning = save_stack_warning, 50 .warning = save_stack_warning,
38 .warning_symbol = save_stack_warning_symbol, 51 .warning_symbol = save_stack_warning_symbol,
@@ -40,6 +53,13 @@ static const struct stacktrace_ops save_stack_ops = {
40 .address = save_stack_address, 53 .address = save_stack_address,
41}; 54};
42 55
56static const struct stacktrace_ops save_stack_ops_nosched = {
57 .warning = save_stack_warning,
58 .warning_symbol = save_stack_warning_symbol,
59 .stack = save_stack_stack,
60 .address = save_stack_address_nosched,
61};
62
43/* 63/*
44 * Save stack-backtrace addresses into a stack_trace buffer. 64 * Save stack-backtrace addresses into a stack_trace buffer.
45 */ 65 */
@@ -50,3 +70,10 @@ void save_stack_trace(struct stack_trace *trace)
50 trace->entries[trace->nr_entries++] = ULONG_MAX; 70 trace->entries[trace->nr_entries++] = ULONG_MAX;
51} 71}
52EXPORT_SYMBOL(save_stack_trace); 72EXPORT_SYMBOL(save_stack_trace);
73
74void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
75{
76 dump_trace(tsk, NULL, NULL, &save_stack_ops_nosched, trace);
77 if (trace->nr_entries < trace->max_entries)
78 trace->entries[trace->nr_entries++] = ULONG_MAX;
79}
diff --git a/drivers/ata/Kconfig b/drivers/ata/Kconfig
index ba63619ae5df..2478cca653de 100644
--- a/drivers/ata/Kconfig
+++ b/drivers/ata/Kconfig
@@ -459,6 +459,15 @@ config PATA_NETCELL
459 459
460 If unsure, say N. 460 If unsure, say N.
461 461
462config PATA_NINJA32
463 tristate "Ninja32/Delkin Cardbus ATA support (Experimental)"
464 depends on PCI && EXPERIMENTAL
465 help
466 This option enables support for the Ninja32, Delkin and
467 possibly other brands of Cardbus ATA adapter
468
469 If unsure, say N.
470
462config PATA_NS87410 471config PATA_NS87410
463 tristate "Nat Semi NS87410 PATA support (Experimental)" 472 tristate "Nat Semi NS87410 PATA support (Experimental)"
464 depends on PCI && EXPERIMENTAL 473 depends on PCI && EXPERIMENTAL
diff --git a/drivers/ata/Makefile b/drivers/ata/Makefile
index b13feb2c5dae..82550c16818c 100644
--- a/drivers/ata/Makefile
+++ b/drivers/ata/Makefile
@@ -41,6 +41,7 @@ obj-$(CONFIG_PATA_IT821X) += pata_it821x.o
41obj-$(CONFIG_PATA_IT8213) += pata_it8213.o 41obj-$(CONFIG_PATA_IT8213) += pata_it8213.o
42obj-$(CONFIG_PATA_JMICRON) += pata_jmicron.o 42obj-$(CONFIG_PATA_JMICRON) += pata_jmicron.o
43obj-$(CONFIG_PATA_NETCELL) += pata_netcell.o 43obj-$(CONFIG_PATA_NETCELL) += pata_netcell.o
44obj-$(CONFIG_PATA_NINJA32) += pata_ninja32.o
44obj-$(CONFIG_PATA_NS87410) += pata_ns87410.o 45obj-$(CONFIG_PATA_NS87410) += pata_ns87410.o
45obj-$(CONFIG_PATA_NS87415) += pata_ns87415.o 46obj-$(CONFIG_PATA_NS87415) += pata_ns87415.o
46obj-$(CONFIG_PATA_OPTI) += pata_opti.o 47obj-$(CONFIG_PATA_OPTI) += pata_opti.o
diff --git a/drivers/ata/ahci.c b/drivers/ata/ahci.c
index 54f38c21dd95..6f089b899a1a 100644
--- a/drivers/ata/ahci.c
+++ b/drivers/ata/ahci.c
@@ -198,18 +198,18 @@ enum {
198}; 198};
199 199
200struct ahci_cmd_hdr { 200struct ahci_cmd_hdr {
201 u32 opts; 201 __le32 opts;
202 u32 status; 202 __le32 status;
203 u32 tbl_addr; 203 __le32 tbl_addr;
204 u32 tbl_addr_hi; 204 __le32 tbl_addr_hi;
205 u32 reserved[4]; 205 __le32 reserved[4];
206}; 206};
207 207
208struct ahci_sg { 208struct ahci_sg {
209 u32 addr; 209 __le32 addr;
210 u32 addr_hi; 210 __le32 addr_hi;
211 u32 reserved; 211 __le32 reserved;
212 u32 flags_size; 212 __le32 flags_size;
213}; 213};
214 214
215struct ahci_host_priv { 215struct ahci_host_priv {
@@ -597,6 +597,20 @@ static inline void __iomem *ahci_port_base(struct ata_port *ap)
597 return __ahci_port_base(ap->host, ap->port_no); 597 return __ahci_port_base(ap->host, ap->port_no);
598} 598}
599 599
600static void ahci_enable_ahci(void __iomem *mmio)
601{
602 u32 tmp;
603
604 /* turn on AHCI_EN */
605 tmp = readl(mmio + HOST_CTL);
606 if (!(tmp & HOST_AHCI_EN)) {
607 tmp |= HOST_AHCI_EN;
608 writel(tmp, mmio + HOST_CTL);
609 tmp = readl(mmio + HOST_CTL); /* flush && sanity check */
610 WARN_ON(!(tmp & HOST_AHCI_EN));
611 }
612}
613
600/** 614/**
601 * ahci_save_initial_config - Save and fixup initial config values 615 * ahci_save_initial_config - Save and fixup initial config values
602 * @pdev: target PCI device 616 * @pdev: target PCI device
@@ -619,6 +633,9 @@ static void ahci_save_initial_config(struct pci_dev *pdev,
619 u32 cap, port_map; 633 u32 cap, port_map;
620 int i; 634 int i;
621 635
636 /* make sure AHCI mode is enabled before accessing CAP */
637 ahci_enable_ahci(mmio);
638
622 /* Values prefixed with saved_ are written back to host after 639 /* Values prefixed with saved_ are written back to host after
623 * reset. Values without are used for driver operation. 640 * reset. Values without are used for driver operation.
624 */ 641 */
@@ -1036,19 +1053,17 @@ static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
1036static int ahci_reset_controller(struct ata_host *host) 1053static int ahci_reset_controller(struct ata_host *host)
1037{ 1054{
1038 struct pci_dev *pdev = to_pci_dev(host->dev); 1055 struct pci_dev *pdev = to_pci_dev(host->dev);
1056 struct ahci_host_priv *hpriv = host->private_data;
1039 void __iomem *mmio = host->iomap[AHCI_PCI_BAR]; 1057 void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
1040 u32 tmp; 1058 u32 tmp;
1041 1059
1042 /* we must be in AHCI mode, before using anything 1060 /* we must be in AHCI mode, before using anything
1043 * AHCI-specific, such as HOST_RESET. 1061 * AHCI-specific, such as HOST_RESET.
1044 */ 1062 */
1045 tmp = readl(mmio + HOST_CTL); 1063 ahci_enable_ahci(mmio);
1046 if (!(tmp & HOST_AHCI_EN)) {
1047 tmp |= HOST_AHCI_EN;
1048 writel(tmp, mmio + HOST_CTL);
1049 }
1050 1064
1051 /* global controller reset */ 1065 /* global controller reset */
1066 tmp = readl(mmio + HOST_CTL);
1052 if ((tmp & HOST_RESET) == 0) { 1067 if ((tmp & HOST_RESET) == 0) {
1053 writel(tmp | HOST_RESET, mmio + HOST_CTL); 1068 writel(tmp | HOST_RESET, mmio + HOST_CTL);
1054 readl(mmio + HOST_CTL); /* flush */ 1069 readl(mmio + HOST_CTL); /* flush */
@@ -1067,8 +1082,7 @@ static int ahci_reset_controller(struct ata_host *host)
1067 } 1082 }
1068 1083
1069 /* turn on AHCI mode */ 1084 /* turn on AHCI mode */
1070 writel(HOST_AHCI_EN, mmio + HOST_CTL); 1085 ahci_enable_ahci(mmio);
1071 (void) readl(mmio + HOST_CTL); /* flush */
1072 1086
1073 /* some registers might be cleared on reset. restore initial values */ 1087 /* some registers might be cleared on reset. restore initial values */
1074 ahci_restore_initial_config(host); 1088 ahci_restore_initial_config(host);
@@ -1078,8 +1092,10 @@ static int ahci_reset_controller(struct ata_host *host)
1078 1092
1079 /* configure PCS */ 1093 /* configure PCS */
1080 pci_read_config_word(pdev, 0x92, &tmp16); 1094 pci_read_config_word(pdev, 0x92, &tmp16);
1081 tmp16 |= 0xf; 1095 if ((tmp16 & hpriv->port_map) != hpriv->port_map) {
1082 pci_write_config_word(pdev, 0x92, tmp16); 1096 tmp16 |= hpriv->port_map;
1097 pci_write_config_word(pdev, 0x92, tmp16);
1098 }
1083 } 1099 }
1084 1100
1085 return 0; 1101 return 0;
@@ -1480,35 +1496,31 @@ static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
1480static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl) 1496static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
1481{ 1497{
1482 struct scatterlist *sg; 1498 struct scatterlist *sg;
1483 struct ahci_sg *ahci_sg; 1499 struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
1484 unsigned int n_sg = 0; 1500 unsigned int si;
1485 1501
1486 VPRINTK("ENTER\n"); 1502 VPRINTK("ENTER\n");
1487 1503
1488 /* 1504 /*
1489 * Next, the S/G list. 1505 * Next, the S/G list.
1490 */ 1506 */
1491 ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ; 1507 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1492 ata_for_each_sg(sg, qc) {
1493 dma_addr_t addr = sg_dma_address(sg); 1508 dma_addr_t addr = sg_dma_address(sg);
1494 u32 sg_len = sg_dma_len(sg); 1509 u32 sg_len = sg_dma_len(sg);
1495 1510
1496 ahci_sg->addr = cpu_to_le32(addr & 0xffffffff); 1511 ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
1497 ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16); 1512 ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
1498 ahci_sg->flags_size = cpu_to_le32(sg_len - 1); 1513 ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
1499
1500 ahci_sg++;
1501 n_sg++;
1502 } 1514 }
1503 1515
1504 return n_sg; 1516 return si;
1505} 1517}
1506 1518
1507static void ahci_qc_prep(struct ata_queued_cmd *qc) 1519static void ahci_qc_prep(struct ata_queued_cmd *qc)
1508{ 1520{
1509 struct ata_port *ap = qc->ap; 1521 struct ata_port *ap = qc->ap;
1510 struct ahci_port_priv *pp = ap->private_data; 1522 struct ahci_port_priv *pp = ap->private_data;
1511 int is_atapi = is_atapi_taskfile(&qc->tf); 1523 int is_atapi = ata_is_atapi(qc->tf.protocol);
1512 void *cmd_tbl; 1524 void *cmd_tbl;
1513 u32 opts; 1525 u32 opts;
1514 const u32 cmd_fis_len = 5; /* five dwords */ 1526 const u32 cmd_fis_len = 5; /* five dwords */
diff --git a/drivers/ata/ata_generic.c b/drivers/ata/ata_generic.c
index 90329982bef7..20534202fc79 100644
--- a/drivers/ata/ata_generic.c
+++ b/drivers/ata/ata_generic.c
@@ -26,7 +26,7 @@
26#include <linux/libata.h> 26#include <linux/libata.h>
27 27
28#define DRV_NAME "ata_generic" 28#define DRV_NAME "ata_generic"
29#define DRV_VERSION "0.2.13" 29#define DRV_VERSION "0.2.15"
30 30
31/* 31/*
32 * A generic parallel ATA driver using libata 32 * A generic parallel ATA driver using libata
@@ -48,27 +48,47 @@ static int generic_set_mode(struct ata_link *link, struct ata_device **unused)
48 struct ata_port *ap = link->ap; 48 struct ata_port *ap = link->ap;
49 int dma_enabled = 0; 49 int dma_enabled = 0;
50 struct ata_device *dev; 50 struct ata_device *dev;
51 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
51 52
52 /* Bits 5 and 6 indicate if DMA is active on master/slave */ 53 /* Bits 5 and 6 indicate if DMA is active on master/slave */
53 if (ap->ioaddr.bmdma_addr) 54 if (ap->ioaddr.bmdma_addr)
54 dma_enabled = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS); 55 dma_enabled = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
55 56
57 if (pdev->vendor == PCI_VENDOR_ID_CENATEK)
58 dma_enabled = 0xFF;
59
56 ata_link_for_each_dev(dev, link) { 60 ata_link_for_each_dev(dev, link) {
57 if (ata_dev_enabled(dev)) { 61 if (!ata_dev_enabled(dev))
58 /* We don't really care */ 62 continue;
59 dev->pio_mode = XFER_PIO_0; 63
60 dev->dma_mode = XFER_MW_DMA_0; 64 /* We don't really care */
61 /* We do need the right mode information for DMA or PIO 65 dev->pio_mode = XFER_PIO_0;
62 and this comes from the current configuration flags */ 66 dev->dma_mode = XFER_MW_DMA_0;
63 if (dma_enabled & (1 << (5 + dev->devno))) { 67 /* We do need the right mode information for DMA or PIO
64 ata_id_to_dma_mode(dev, XFER_MW_DMA_0); 68 and this comes from the current configuration flags */
65 dev->flags &= ~ATA_DFLAG_PIO; 69 if (dma_enabled & (1 << (5 + dev->devno))) {
66 } else { 70 unsigned int xfer_mask = ata_id_xfermask(dev->id);
67 ata_dev_printk(dev, KERN_INFO, "configured for PIO\n"); 71 const char *name;
68 dev->xfer_mode = XFER_PIO_0; 72
69 dev->xfer_shift = ATA_SHIFT_PIO; 73 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
70 dev->flags |= ATA_DFLAG_PIO; 74 name = ata_mode_string(xfer_mask);
75 else {
76 /* SWDMA perhaps? */
77 name = "DMA";
78 xfer_mask |= ata_xfer_mode2mask(XFER_MW_DMA_0);
71 } 79 }
80
81 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
82 name);
83
84 dev->xfer_mode = ata_xfer_mask2mode(xfer_mask);
85 dev->xfer_shift = ata_xfer_mode2shift(dev->xfer_mode);
86 dev->flags &= ~ATA_DFLAG_PIO;
87 } else {
88 ata_dev_printk(dev, KERN_INFO, "configured for PIO\n");
89 dev->xfer_mode = XFER_PIO_0;
90 dev->xfer_shift = ATA_SHIFT_PIO;
91 dev->flags |= ATA_DFLAG_PIO;
72 } 92 }
73 } 93 }
74 return 0; 94 return 0;
@@ -185,6 +205,7 @@ static struct pci_device_id ata_generic[] = {
185 { PCI_DEVICE(PCI_VENDOR_ID_HINT, PCI_DEVICE_ID_HINT_VXPROII_IDE), }, 205 { PCI_DEVICE(PCI_VENDOR_ID_HINT, PCI_DEVICE_ID_HINT_VXPROII_IDE), },
186 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C561), }, 206 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C561), },
187 { PCI_DEVICE(PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C558), }, 207 { PCI_DEVICE(PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C558), },
208 { PCI_DEVICE(PCI_VENDOR_ID_CENATEK,PCI_DEVICE_ID_CENATEK_IDE), },
188 { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO), }, 209 { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO), },
189 { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_1), }, 210 { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_1), },
190 { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_2), }, 211 { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_2), },
diff --git a/drivers/ata/ata_piix.c b/drivers/ata/ata_piix.c
index b406b39b878e..a65c8ae5c461 100644
--- a/drivers/ata/ata_piix.c
+++ b/drivers/ata/ata_piix.c
@@ -101,39 +101,21 @@ enum {
101 ICH5_PMR = 0x90, /* port mapping register */ 101 ICH5_PMR = 0x90, /* port mapping register */
102 ICH5_PCS = 0x92, /* port control and status */ 102 ICH5_PCS = 0x92, /* port control and status */
103 PIIX_SCC = 0x0A, /* sub-class code register */ 103 PIIX_SCC = 0x0A, /* sub-class code register */
104 PIIX_SIDPR_BAR = 5,
105 PIIX_SIDPR_LEN = 16,
106 PIIX_SIDPR_IDX = 0,
107 PIIX_SIDPR_DATA = 4,
104 108
105 PIIX_FLAG_SCR = (1 << 26), /* SCR available */
106 PIIX_FLAG_AHCI = (1 << 27), /* AHCI possible */ 109 PIIX_FLAG_AHCI = (1 << 27), /* AHCI possible */
107 PIIX_FLAG_CHECKINTR = (1 << 28), /* make sure PCI INTx enabled */ 110 PIIX_FLAG_CHECKINTR = (1 << 28), /* make sure PCI INTx enabled */
111 PIIX_FLAG_SIDPR = (1 << 29), /* SATA idx/data pair regs */
108 112
109 PIIX_PATA_FLAGS = ATA_FLAG_SLAVE_POSS, 113 PIIX_PATA_FLAGS = ATA_FLAG_SLAVE_POSS,
110 PIIX_SATA_FLAGS = ATA_FLAG_SATA | PIIX_FLAG_CHECKINTR, 114 PIIX_SATA_FLAGS = ATA_FLAG_SATA | PIIX_FLAG_CHECKINTR,
111 115
112 /* combined mode. if set, PATA is channel 0.
113 * if clear, PATA is channel 1.
114 */
115 PIIX_PORT_ENABLED = (1 << 0),
116 PIIX_PORT_PRESENT = (1 << 4),
117
118 PIIX_80C_PRI = (1 << 5) | (1 << 4), 116 PIIX_80C_PRI = (1 << 5) | (1 << 4),
119 PIIX_80C_SEC = (1 << 7) | (1 << 6), 117 PIIX_80C_SEC = (1 << 7) | (1 << 6),
120 118
121 /* controller IDs */
122 piix_pata_mwdma = 0, /* PIIX3 MWDMA only */
123 piix_pata_33, /* PIIX4 at 33Mhz */
124 ich_pata_33, /* ICH up to UDMA 33 only */
125 ich_pata_66, /* ICH up to 66 Mhz */
126 ich_pata_100, /* ICH up to UDMA 100 */
127 ich5_sata,
128 ich6_sata,
129 ich6_sata_ahci,
130 ich6m_sata_ahci,
131 ich8_sata_ahci,
132 ich8_2port_sata,
133 ich8m_apple_sata_ahci, /* locks up on second port enable */
134 tolapai_sata_ahci,
135 piix_pata_vmw, /* PIIX4 for VMware, spurious DMA_ERR */
136
137 /* constants for mapping table */ 119 /* constants for mapping table */
138 P0 = 0, /* port 0 */ 120 P0 = 0, /* port 0 */
139 P1 = 1, /* port 1 */ 121 P1 = 1, /* port 1 */
@@ -149,6 +131,24 @@ enum {
149 PIIX_HOST_BROKEN_SUSPEND = (1 << 24), 131 PIIX_HOST_BROKEN_SUSPEND = (1 << 24),
150}; 132};
151 133
134enum piix_controller_ids {
135 /* controller IDs */
136 piix_pata_mwdma, /* PIIX3 MWDMA only */
137 piix_pata_33, /* PIIX4 at 33Mhz */
138 ich_pata_33, /* ICH up to UDMA 33 only */
139 ich_pata_66, /* ICH up to 66 Mhz */
140 ich_pata_100, /* ICH up to UDMA 100 */
141 ich5_sata,
142 ich6_sata,
143 ich6_sata_ahci,
144 ich6m_sata_ahci,
145 ich8_sata_ahci,
146 ich8_2port_sata,
147 ich8m_apple_sata_ahci, /* locks up on second port enable */
148 tolapai_sata_ahci,
149 piix_pata_vmw, /* PIIX4 for VMware, spurious DMA_ERR */
150};
151
152struct piix_map_db { 152struct piix_map_db {
153 const u32 mask; 153 const u32 mask;
154 const u16 port_enable; 154 const u16 port_enable;
@@ -157,6 +157,7 @@ struct piix_map_db {
157 157
158struct piix_host_priv { 158struct piix_host_priv {
159 const int *map; 159 const int *map;
160 void __iomem *sidpr;
160}; 161};
161 162
162static int piix_init_one(struct pci_dev *pdev, 163static int piix_init_one(struct pci_dev *pdev,
@@ -167,6 +168,9 @@ static void piix_set_dmamode(struct ata_port *ap, struct ata_device *adev);
167static void ich_set_dmamode(struct ata_port *ap, struct ata_device *adev); 168static void ich_set_dmamode(struct ata_port *ap, struct ata_device *adev);
168static int ich_pata_cable_detect(struct ata_port *ap); 169static int ich_pata_cable_detect(struct ata_port *ap);
169static u8 piix_vmw_bmdma_status(struct ata_port *ap); 170static u8 piix_vmw_bmdma_status(struct ata_port *ap);
171static int piix_sidpr_scr_read(struct ata_port *ap, unsigned int reg, u32 *val);
172static int piix_sidpr_scr_write(struct ata_port *ap, unsigned int reg, u32 val);
173static void piix_sidpr_error_handler(struct ata_port *ap);
170#ifdef CONFIG_PM 174#ifdef CONFIG_PM
171static int piix_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg); 175static int piix_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
172static int piix_pci_device_resume(struct pci_dev *pdev); 176static int piix_pci_device_resume(struct pci_dev *pdev);
@@ -321,7 +325,6 @@ static const struct ata_port_operations piix_pata_ops = {
321 .post_internal_cmd = ata_bmdma_post_internal_cmd, 325 .post_internal_cmd = ata_bmdma_post_internal_cmd,
322 .cable_detect = ata_cable_40wire, 326 .cable_detect = ata_cable_40wire,
323 327
324 .irq_handler = ata_interrupt,
325 .irq_clear = ata_bmdma_irq_clear, 328 .irq_clear = ata_bmdma_irq_clear,
326 .irq_on = ata_irq_on, 329 .irq_on = ata_irq_on,
327 330
@@ -353,7 +356,6 @@ static const struct ata_port_operations ich_pata_ops = {
353 .post_internal_cmd = ata_bmdma_post_internal_cmd, 356 .post_internal_cmd = ata_bmdma_post_internal_cmd,
354 .cable_detect = ich_pata_cable_detect, 357 .cable_detect = ich_pata_cable_detect,
355 358
356 .irq_handler = ata_interrupt,
357 .irq_clear = ata_bmdma_irq_clear, 359 .irq_clear = ata_bmdma_irq_clear,
358 .irq_on = ata_irq_on, 360 .irq_on = ata_irq_on,
359 361
@@ -380,7 +382,6 @@ static const struct ata_port_operations piix_sata_ops = {
380 .error_handler = ata_bmdma_error_handler, 382 .error_handler = ata_bmdma_error_handler,
381 .post_internal_cmd = ata_bmdma_post_internal_cmd, 383 .post_internal_cmd = ata_bmdma_post_internal_cmd,
382 384
383 .irq_handler = ata_interrupt,
384 .irq_clear = ata_bmdma_irq_clear, 385 .irq_clear = ata_bmdma_irq_clear,
385 .irq_on = ata_irq_on, 386 .irq_on = ata_irq_on,
386 387
@@ -419,6 +420,35 @@ static const struct ata_port_operations piix_vmw_ops = {
419 .port_start = ata_port_start, 420 .port_start = ata_port_start,
420}; 421};
421 422
423static const struct ata_port_operations piix_sidpr_sata_ops = {
424 .tf_load = ata_tf_load,
425 .tf_read = ata_tf_read,
426 .check_status = ata_check_status,
427 .exec_command = ata_exec_command,
428 .dev_select = ata_std_dev_select,
429
430 .bmdma_setup = ata_bmdma_setup,
431 .bmdma_start = ata_bmdma_start,
432 .bmdma_stop = ata_bmdma_stop,
433 .bmdma_status = ata_bmdma_status,
434 .qc_prep = ata_qc_prep,
435 .qc_issue = ata_qc_issue_prot,
436 .data_xfer = ata_data_xfer,
437
438 .scr_read = piix_sidpr_scr_read,
439 .scr_write = piix_sidpr_scr_write,
440
441 .freeze = ata_bmdma_freeze,
442 .thaw = ata_bmdma_thaw,
443 .error_handler = piix_sidpr_error_handler,
444 .post_internal_cmd = ata_bmdma_post_internal_cmd,
445
446 .irq_clear = ata_bmdma_irq_clear,
447 .irq_on = ata_irq_on,
448
449 .port_start = ata_port_start,
450};
451
422static const struct piix_map_db ich5_map_db = { 452static const struct piix_map_db ich5_map_db = {
423 .mask = 0x7, 453 .mask = 0x7,
424 .port_enable = 0x3, 454 .port_enable = 0x3,
@@ -526,7 +556,6 @@ static const struct piix_map_db *piix_map_db_table[] = {
526static struct ata_port_info piix_port_info[] = { 556static struct ata_port_info piix_port_info[] = {
527 [piix_pata_mwdma] = /* PIIX3 MWDMA only */ 557 [piix_pata_mwdma] = /* PIIX3 MWDMA only */
528 { 558 {
529 .sht = &piix_sht,
530 .flags = PIIX_PATA_FLAGS, 559 .flags = PIIX_PATA_FLAGS,
531 .pio_mask = 0x1f, /* pio0-4 */ 560 .pio_mask = 0x1f, /* pio0-4 */
532 .mwdma_mask = 0x06, /* mwdma1-2 ?? CHECK 0 should be ok but slow */ 561 .mwdma_mask = 0x06, /* mwdma1-2 ?? CHECK 0 should be ok but slow */
@@ -535,7 +564,6 @@ static struct ata_port_info piix_port_info[] = {
535 564
536 [piix_pata_33] = /* PIIX4 at 33MHz */ 565 [piix_pata_33] = /* PIIX4 at 33MHz */
537 { 566 {
538 .sht = &piix_sht,
539 .flags = PIIX_PATA_FLAGS, 567 .flags = PIIX_PATA_FLAGS,
540 .pio_mask = 0x1f, /* pio0-4 */ 568 .pio_mask = 0x1f, /* pio0-4 */
541 .mwdma_mask = 0x06, /* mwdma1-2 ?? CHECK 0 should be ok but slow */ 569 .mwdma_mask = 0x06, /* mwdma1-2 ?? CHECK 0 should be ok but slow */
@@ -545,7 +573,6 @@ static struct ata_port_info piix_port_info[] = {
545 573
546 [ich_pata_33] = /* ICH0 - ICH at 33Mhz*/ 574 [ich_pata_33] = /* ICH0 - ICH at 33Mhz*/
547 { 575 {
548 .sht = &piix_sht,
549 .flags = PIIX_PATA_FLAGS, 576 .flags = PIIX_PATA_FLAGS,
550 .pio_mask = 0x1f, /* pio 0-4 */ 577 .pio_mask = 0x1f, /* pio 0-4 */
551 .mwdma_mask = 0x06, /* Check: maybe 0x07 */ 578 .mwdma_mask = 0x06, /* Check: maybe 0x07 */
@@ -555,7 +582,6 @@ static struct ata_port_info piix_port_info[] = {
555 582
556 [ich_pata_66] = /* ICH controllers up to 66MHz */ 583 [ich_pata_66] = /* ICH controllers up to 66MHz */
557 { 584 {
558 .sht = &piix_sht,
559 .flags = PIIX_PATA_FLAGS, 585 .flags = PIIX_PATA_FLAGS,
560 .pio_mask = 0x1f, /* pio 0-4 */ 586 .pio_mask = 0x1f, /* pio 0-4 */
561 .mwdma_mask = 0x06, /* MWDMA0 is broken on chip */ 587 .mwdma_mask = 0x06, /* MWDMA0 is broken on chip */
@@ -565,7 +591,6 @@ static struct ata_port_info piix_port_info[] = {
565 591
566 [ich_pata_100] = 592 [ich_pata_100] =
567 { 593 {
568 .sht = &piix_sht,
569 .flags = PIIX_PATA_FLAGS | PIIX_FLAG_CHECKINTR, 594 .flags = PIIX_PATA_FLAGS | PIIX_FLAG_CHECKINTR,
570 .pio_mask = 0x1f, /* pio0-4 */ 595 .pio_mask = 0x1f, /* pio0-4 */
571 .mwdma_mask = 0x06, /* mwdma1-2 */ 596 .mwdma_mask = 0x06, /* mwdma1-2 */
@@ -575,7 +600,6 @@ static struct ata_port_info piix_port_info[] = {
575 600
576 [ich5_sata] = 601 [ich5_sata] =
577 { 602 {
578 .sht = &piix_sht,
579 .flags = PIIX_SATA_FLAGS, 603 .flags = PIIX_SATA_FLAGS,
580 .pio_mask = 0x1f, /* pio0-4 */ 604 .pio_mask = 0x1f, /* pio0-4 */
581 .mwdma_mask = 0x07, /* mwdma0-2 */ 605 .mwdma_mask = 0x07, /* mwdma0-2 */
@@ -585,8 +609,7 @@ static struct ata_port_info piix_port_info[] = {
585 609
586 [ich6_sata] = 610 [ich6_sata] =
587 { 611 {
588 .sht = &piix_sht, 612 .flags = PIIX_SATA_FLAGS,
589 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_SCR,
590 .pio_mask = 0x1f, /* pio0-4 */ 613 .pio_mask = 0x1f, /* pio0-4 */
591 .mwdma_mask = 0x07, /* mwdma0-2 */ 614 .mwdma_mask = 0x07, /* mwdma0-2 */
592 .udma_mask = ATA_UDMA6, 615 .udma_mask = ATA_UDMA6,
@@ -595,9 +618,7 @@ static struct ata_port_info piix_port_info[] = {
595 618
596 [ich6_sata_ahci] = 619 [ich6_sata_ahci] =
597 { 620 {
598 .sht = &piix_sht, 621 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_AHCI,
599 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_SCR |
600 PIIX_FLAG_AHCI,
601 .pio_mask = 0x1f, /* pio0-4 */ 622 .pio_mask = 0x1f, /* pio0-4 */
602 .mwdma_mask = 0x07, /* mwdma0-2 */ 623 .mwdma_mask = 0x07, /* mwdma0-2 */
603 .udma_mask = ATA_UDMA6, 624 .udma_mask = ATA_UDMA6,
@@ -606,9 +627,7 @@ static struct ata_port_info piix_port_info[] = {
606 627
607 [ich6m_sata_ahci] = 628 [ich6m_sata_ahci] =
608 { 629 {
609 .sht = &piix_sht, 630 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_AHCI,
610 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_SCR |
611 PIIX_FLAG_AHCI,
612 .pio_mask = 0x1f, /* pio0-4 */ 631 .pio_mask = 0x1f, /* pio0-4 */
613 .mwdma_mask = 0x07, /* mwdma0-2 */ 632 .mwdma_mask = 0x07, /* mwdma0-2 */
614 .udma_mask = ATA_UDMA6, 633 .udma_mask = ATA_UDMA6,
@@ -617,9 +636,8 @@ static struct ata_port_info piix_port_info[] = {
617 636
618 [ich8_sata_ahci] = 637 [ich8_sata_ahci] =
619 { 638 {
620 .sht = &piix_sht, 639 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_AHCI |
621 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_SCR | 640 PIIX_FLAG_SIDPR,
622 PIIX_FLAG_AHCI,
623 .pio_mask = 0x1f, /* pio0-4 */ 641 .pio_mask = 0x1f, /* pio0-4 */
624 .mwdma_mask = 0x07, /* mwdma0-2 */ 642 .mwdma_mask = 0x07, /* mwdma0-2 */
625 .udma_mask = ATA_UDMA6, 643 .udma_mask = ATA_UDMA6,
@@ -628,9 +646,8 @@ static struct ata_port_info piix_port_info[] = {
628 646
629 [ich8_2port_sata] = 647 [ich8_2port_sata] =
630 { 648 {
631 .sht = &piix_sht, 649 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_AHCI |
632 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_SCR | 650 PIIX_FLAG_SIDPR,
633 PIIX_FLAG_AHCI,
634 .pio_mask = 0x1f, /* pio0-4 */ 651 .pio_mask = 0x1f, /* pio0-4 */
635 .mwdma_mask = 0x07, /* mwdma0-2 */ 652 .mwdma_mask = 0x07, /* mwdma0-2 */
636 .udma_mask = ATA_UDMA6, 653 .udma_mask = ATA_UDMA6,
@@ -639,9 +656,7 @@ static struct ata_port_info piix_port_info[] = {
639 656
640 [tolapai_sata_ahci] = 657 [tolapai_sata_ahci] =
641 { 658 {
642 .sht = &piix_sht, 659 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_AHCI,
643 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_SCR |
644 PIIX_FLAG_AHCI,
645 .pio_mask = 0x1f, /* pio0-4 */ 660 .pio_mask = 0x1f, /* pio0-4 */
646 .mwdma_mask = 0x07, /* mwdma0-2 */ 661 .mwdma_mask = 0x07, /* mwdma0-2 */
647 .udma_mask = ATA_UDMA6, 662 .udma_mask = ATA_UDMA6,
@@ -650,9 +665,8 @@ static struct ata_port_info piix_port_info[] = {
650 665
651 [ich8m_apple_sata_ahci] = 666 [ich8m_apple_sata_ahci] =
652 { 667 {
653 .sht = &piix_sht, 668 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_AHCI |
654 .flags = PIIX_SATA_FLAGS | PIIX_FLAG_SCR | 669 PIIX_FLAG_SIDPR,
655 PIIX_FLAG_AHCI,
656 .pio_mask = 0x1f, /* pio0-4 */ 670 .pio_mask = 0x1f, /* pio0-4 */
657 .mwdma_mask = 0x07, /* mwdma0-2 */ 671 .mwdma_mask = 0x07, /* mwdma0-2 */
658 .udma_mask = ATA_UDMA6, 672 .udma_mask = ATA_UDMA6,
@@ -1001,6 +1015,180 @@ static void ich_set_dmamode(struct ata_port *ap, struct ata_device *adev)
1001 do_pata_set_dmamode(ap, adev, 1); 1015 do_pata_set_dmamode(ap, adev, 1);
1002} 1016}
1003 1017
1018/*
1019 * Serial ATA Index/Data Pair Superset Registers access
1020 *
1021 * Beginning from ICH8, there's a sane way to access SCRs using index
1022 * and data register pair located at BAR5. This creates an
1023 * interesting problem of mapping two SCRs to one port.
1024 *
1025 * Although they have separate SCRs, the master and slave aren't
1026 * independent enough to be treated as separate links - e.g. softreset
1027 * resets both. Also, there's no protocol defined for hard resetting
1028 * singled device sharing the virtual port (no defined way to acquire
1029 * device signature). This is worked around by merging the SCR values
1030 * into one sensible value and requesting follow-up SRST after
1031 * hardreset.
1032 *
1033 * SCR merging is perfomed in nibbles which is the unit contents in
1034 * SCRs are organized. If two values are equal, the value is used.
1035 * When they differ, merge table which lists precedence of possible
1036 * values is consulted and the first match or the last entry when
1037 * nothing matches is used. When there's no merge table for the
1038 * specific nibble, value from the first port is used.
1039 */
1040static const int piix_sidx_map[] = {
1041 [SCR_STATUS] = 0,
1042 [SCR_ERROR] = 2,
1043 [SCR_CONTROL] = 1,
1044};
1045
1046static void piix_sidpr_sel(struct ata_device *dev, unsigned int reg)
1047{
1048 struct ata_port *ap = dev->link->ap;
1049 struct piix_host_priv *hpriv = ap->host->private_data;
1050
1051 iowrite32(((ap->port_no * 2 + dev->devno) << 8) | piix_sidx_map[reg],
1052 hpriv->sidpr + PIIX_SIDPR_IDX);
1053}
1054
1055static int piix_sidpr_read(struct ata_device *dev, unsigned int reg)
1056{
1057 struct piix_host_priv *hpriv = dev->link->ap->host->private_data;
1058
1059 piix_sidpr_sel(dev, reg);
1060 return ioread32(hpriv->sidpr + PIIX_SIDPR_DATA);
1061}
1062
1063static void piix_sidpr_write(struct ata_device *dev, unsigned int reg, u32 val)
1064{
1065 struct piix_host_priv *hpriv = dev->link->ap->host->private_data;
1066
1067 piix_sidpr_sel(dev, reg);
1068 iowrite32(val, hpriv->sidpr + PIIX_SIDPR_DATA);
1069}
1070
1071u32 piix_merge_scr(u32 val0, u32 val1, const int * const *merge_tbl)
1072{
1073 u32 val = 0;
1074 int i, mi;
1075
1076 for (i = 0, mi = 0; i < 32 / 4; i++) {
1077 u8 c0 = (val0 >> (i * 4)) & 0xf;
1078 u8 c1 = (val1 >> (i * 4)) & 0xf;
1079 u8 merged = c0;
1080 const int *cur;
1081
1082 /* if no merge preference, assume the first value */
1083 cur = merge_tbl[mi];
1084 if (!cur)
1085 goto done;
1086 mi++;
1087
1088 /* if two values equal, use it */
1089 if (c0 == c1)
1090 goto done;
1091
1092 /* choose the first match or the last from the merge table */
1093 while (*cur != -1) {
1094 if (c0 == *cur || c1 == *cur)
1095 break;
1096 cur++;
1097 }
1098 if (*cur == -1)
1099 cur--;
1100 merged = *cur;
1101 done:
1102 val |= merged << (i * 4);
1103 }
1104
1105 return val;
1106}
1107
1108static int piix_sidpr_scr_read(struct ata_port *ap, unsigned int reg, u32 *val)
1109{
1110 const int * const sstatus_merge_tbl[] = {
1111 /* DET */ (const int []){ 1, 3, 0, 4, 3, -1 },
1112 /* SPD */ (const int []){ 2, 1, 0, -1 },
1113 /* IPM */ (const int []){ 6, 2, 1, 0, -1 },
1114 NULL,
1115 };
1116 const int * const scontrol_merge_tbl[] = {
1117 /* DET */ (const int []){ 1, 0, 4, 0, -1 },
1118 /* SPD */ (const int []){ 0, 2, 1, 0, -1 },
1119 /* IPM */ (const int []){ 0, 1, 2, 3, 0, -1 },
1120 NULL,
1121 };
1122 u32 v0, v1;
1123
1124 if (reg >= ARRAY_SIZE(piix_sidx_map))
1125 return -EINVAL;
1126
1127 if (!(ap->flags & ATA_FLAG_SLAVE_POSS)) {
1128 *val = piix_sidpr_read(&ap->link.device[0], reg);
1129 return 0;
1130 }
1131
1132 v0 = piix_sidpr_read(&ap->link.device[0], reg);
1133 v1 = piix_sidpr_read(&ap->link.device[1], reg);
1134
1135 switch (reg) {
1136 case SCR_STATUS:
1137 *val = piix_merge_scr(v0, v1, sstatus_merge_tbl);
1138 break;
1139 case SCR_ERROR:
1140 *val = v0 | v1;
1141 break;
1142 case SCR_CONTROL:
1143 *val = piix_merge_scr(v0, v1, scontrol_merge_tbl);
1144 break;
1145 }
1146
1147 return 0;
1148}
1149
1150static int piix_sidpr_scr_write(struct ata_port *ap, unsigned int reg, u32 val)
1151{
1152 if (reg >= ARRAY_SIZE(piix_sidx_map))
1153 return -EINVAL;
1154
1155 piix_sidpr_write(&ap->link.device[0], reg, val);
1156
1157 if (ap->flags & ATA_FLAG_SLAVE_POSS)
1158 piix_sidpr_write(&ap->link.device[1], reg, val);
1159
1160 return 0;
1161}
1162
1163static int piix_sidpr_hardreset(struct ata_link *link, unsigned int *class,
1164 unsigned long deadline)
1165{
1166 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1167 int rc;
1168
1169 /* do hardreset */
1170 rc = sata_link_hardreset(link, timing, deadline);
1171 if (rc) {
1172 ata_link_printk(link, KERN_ERR,
1173 "COMRESET failed (errno=%d)\n", rc);
1174 return rc;
1175 }
1176
1177 /* TODO: phy layer with polling, timeouts, etc. */
1178 if (ata_link_offline(link)) {
1179 *class = ATA_DEV_NONE;
1180 return 0;
1181 }
1182
1183 return -EAGAIN;
1184}
1185
1186static void piix_sidpr_error_handler(struct ata_port *ap)
1187{
1188 ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset,
1189 piix_sidpr_hardreset, ata_std_postreset);
1190}
1191
1004#ifdef CONFIG_PM 1192#ifdef CONFIG_PM
1005static int piix_broken_suspend(void) 1193static int piix_broken_suspend(void)
1006{ 1194{
@@ -1034,6 +1222,13 @@ static int piix_broken_suspend(void)
1034 }, 1222 },
1035 }, 1223 },
1036 { 1224 {
1225 .ident = "TECRA M6",
1226 .matches = {
1227 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1228 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA M6"),
1229 },
1230 },
1231 {
1037 .ident = "TECRA M7", 1232 .ident = "TECRA M7",
1038 .matches = { 1233 .matches = {
1039 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"), 1234 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
@@ -1048,6 +1243,13 @@ static int piix_broken_suspend(void)
1048 }, 1243 },
1049 }, 1244 },
1050 { 1245 {
1246 .ident = "Satellite R20",
1247 .matches = {
1248 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1249 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite R20"),
1250 },
1251 },
1252 {
1051 .ident = "Satellite R25", 1253 .ident = "Satellite R25",
1052 .matches = { 1254 .matches = {
1053 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"), 1255 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
@@ -1253,10 +1455,10 @@ static int __devinit piix_check_450nx_errata(struct pci_dev *ata_dev)
1253 return no_piix_dma; 1455 return no_piix_dma;
1254} 1456}
1255 1457
1256static void __devinit piix_init_pcs(struct pci_dev *pdev, 1458static void __devinit piix_init_pcs(struct ata_host *host,
1257 struct ata_port_info *pinfo,
1258 const struct piix_map_db *map_db) 1459 const struct piix_map_db *map_db)
1259{ 1460{
1461 struct pci_dev *pdev = to_pci_dev(host->dev);
1260 u16 pcs, new_pcs; 1462 u16 pcs, new_pcs;
1261 1463
1262 pci_read_config_word(pdev, ICH5_PCS, &pcs); 1464 pci_read_config_word(pdev, ICH5_PCS, &pcs);
@@ -1270,11 +1472,10 @@ static void __devinit piix_init_pcs(struct pci_dev *pdev,
1270 } 1472 }
1271} 1473}
1272 1474
1273static void __devinit piix_init_sata_map(struct pci_dev *pdev, 1475static const int *__devinit piix_init_sata_map(struct pci_dev *pdev,
1274 struct ata_port_info *pinfo, 1476 struct ata_port_info *pinfo,
1275 const struct piix_map_db *map_db) 1477 const struct piix_map_db *map_db)
1276{ 1478{
1277 struct piix_host_priv *hpriv = pinfo[0].private_data;
1278 const int *map; 1479 const int *map;
1279 int i, invalid_map = 0; 1480 int i, invalid_map = 0;
1280 u8 map_value; 1481 u8 map_value;
@@ -1298,7 +1499,6 @@ static void __devinit piix_init_sata_map(struct pci_dev *pdev,
1298 case IDE: 1499 case IDE:
1299 WARN_ON((i & 1) || map[i + 1] != IDE); 1500 WARN_ON((i & 1) || map[i + 1] != IDE);
1300 pinfo[i / 2] = piix_port_info[ich_pata_100]; 1501 pinfo[i / 2] = piix_port_info[ich_pata_100];
1301 pinfo[i / 2].private_data = hpriv;
1302 i++; 1502 i++;
1303 printk(" IDE IDE"); 1503 printk(" IDE IDE");
1304 break; 1504 break;
@@ -1316,7 +1516,33 @@ static void __devinit piix_init_sata_map(struct pci_dev *pdev,
1316 dev_printk(KERN_ERR, &pdev->dev, 1516 dev_printk(KERN_ERR, &pdev->dev,
1317 "invalid MAP value %u\n", map_value); 1517 "invalid MAP value %u\n", map_value);
1318 1518
1319 hpriv->map = map; 1519 return map;
1520}
1521
1522static void __devinit piix_init_sidpr(struct ata_host *host)
1523{
1524 struct pci_dev *pdev = to_pci_dev(host->dev);
1525 struct piix_host_priv *hpriv = host->private_data;
1526 int i;
1527
1528 /* check for availability */
1529 for (i = 0; i < 4; i++)
1530 if (hpriv->map[i] == IDE)
1531 return;
1532
1533 if (!(host->ports[0]->flags & PIIX_FLAG_SIDPR))
1534 return;
1535
1536 if (pci_resource_start(pdev, PIIX_SIDPR_BAR) == 0 ||
1537 pci_resource_len(pdev, PIIX_SIDPR_BAR) != PIIX_SIDPR_LEN)
1538 return;
1539
1540 if (pcim_iomap_regions(pdev, 1 << PIIX_SIDPR_BAR, DRV_NAME))
1541 return;
1542
1543 hpriv->sidpr = pcim_iomap_table(pdev)[PIIX_SIDPR_BAR];
1544 host->ports[0]->ops = &piix_sidpr_sata_ops;
1545 host->ports[1]->ops = &piix_sidpr_sata_ops;
1320} 1546}
1321 1547
1322static void piix_iocfg_bit18_quirk(struct pci_dev *pdev) 1548static void piix_iocfg_bit18_quirk(struct pci_dev *pdev)
@@ -1375,8 +1601,10 @@ static int piix_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1375 struct device *dev = &pdev->dev; 1601 struct device *dev = &pdev->dev;
1376 struct ata_port_info port_info[2]; 1602 struct ata_port_info port_info[2];
1377 const struct ata_port_info *ppi[] = { &port_info[0], &port_info[1] }; 1603 const struct ata_port_info *ppi[] = { &port_info[0], &port_info[1] };
1378 struct piix_host_priv *hpriv;
1379 unsigned long port_flags; 1604 unsigned long port_flags;
1605 struct ata_host *host;
1606 struct piix_host_priv *hpriv;
1607 int rc;
1380 1608
1381 if (!printed_version++) 1609 if (!printed_version++)
1382 dev_printk(KERN_DEBUG, &pdev->dev, 1610 dev_printk(KERN_DEBUG, &pdev->dev,
@@ -1386,17 +1614,31 @@ static int piix_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1386 if (!in_module_init) 1614 if (!in_module_init)
1387 return -ENODEV; 1615 return -ENODEV;
1388 1616
1617 port_info[0] = piix_port_info[ent->driver_data];
1618 port_info[1] = piix_port_info[ent->driver_data];
1619
1620 port_flags = port_info[0].flags;
1621
1622 /* enable device and prepare host */
1623 rc = pcim_enable_device(pdev);
1624 if (rc)
1625 return rc;
1626
1627 /* SATA map init can change port_info, do it before prepping host */
1389 hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL); 1628 hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
1390 if (!hpriv) 1629 if (!hpriv)
1391 return -ENOMEM; 1630 return -ENOMEM;
1392 1631
1393 port_info[0] = piix_port_info[ent->driver_data]; 1632 if (port_flags & ATA_FLAG_SATA)
1394 port_info[1] = piix_port_info[ent->driver_data]; 1633 hpriv->map = piix_init_sata_map(pdev, port_info,
1395 port_info[0].private_data = hpriv; 1634 piix_map_db_table[ent->driver_data]);
1396 port_info[1].private_data = hpriv;
1397 1635
1398 port_flags = port_info[0].flags; 1636 rc = ata_pci_prepare_sff_host(pdev, ppi, &host);
1637 if (rc)
1638 return rc;
1639 host->private_data = hpriv;
1399 1640
1641 /* initialize controller */
1400 if (port_flags & PIIX_FLAG_AHCI) { 1642 if (port_flags & PIIX_FLAG_AHCI) {
1401 u8 tmp; 1643 u8 tmp;
1402 pci_read_config_byte(pdev, PIIX_SCC, &tmp); 1644 pci_read_config_byte(pdev, PIIX_SCC, &tmp);
@@ -1407,12 +1649,9 @@ static int piix_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1407 } 1649 }
1408 } 1650 }
1409 1651
1410 /* Initialize SATA map */
1411 if (port_flags & ATA_FLAG_SATA) { 1652 if (port_flags & ATA_FLAG_SATA) {
1412 piix_init_sata_map(pdev, port_info, 1653 piix_init_pcs(host, piix_map_db_table[ent->driver_data]);
1413 piix_map_db_table[ent->driver_data]); 1654 piix_init_sidpr(host);
1414 piix_init_pcs(pdev, port_info,
1415 piix_map_db_table[ent->driver_data]);
1416 } 1655 }
1417 1656
1418 /* apply IOCFG bit18 quirk */ 1657 /* apply IOCFG bit18 quirk */
@@ -1431,12 +1670,14 @@ static int piix_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1431 /* This writes into the master table but it does not 1670 /* This writes into the master table but it does not
1432 really matter for this errata as we will apply it to 1671 really matter for this errata as we will apply it to
1433 all the PIIX devices on the board */ 1672 all the PIIX devices on the board */
1434 port_info[0].mwdma_mask = 0; 1673 host->ports[0]->mwdma_mask = 0;
1435 port_info[0].udma_mask = 0; 1674 host->ports[0]->udma_mask = 0;
1436 port_info[1].mwdma_mask = 0; 1675 host->ports[1]->mwdma_mask = 0;
1437 port_info[1].udma_mask = 0; 1676 host->ports[1]->udma_mask = 0;
1438 } 1677 }
1439 return ata_pci_init_one(pdev, ppi); 1678
1679 pci_set_master(pdev);
1680 return ata_pci_activate_sff_host(host, ata_interrupt, &piix_sht);
1440} 1681}
1441 1682
1442static int __init piix_init(void) 1683static int __init piix_init(void)
diff --git a/drivers/ata/libata-acpi.c b/drivers/ata/libata-acpi.c
index 7bf4befd96bc..9e8ec19260af 100644
--- a/drivers/ata/libata-acpi.c
+++ b/drivers/ata/libata-acpi.c
@@ -442,40 +442,77 @@ static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
442} 442}
443 443
444/** 444/**
445 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
446 * @dev: target device
447 * @gtm: GTM parameter to use
448 *
449 * Determine xfermask for @dev from @gtm.
450 *
451 * LOCKING:
452 * None.
453 *
454 * RETURNS:
455 * Determined xfermask.
456 */
457unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
458 const struct ata_acpi_gtm *gtm)
459{
460 unsigned long xfer_mask = 0;
461 unsigned int type;
462 int unit;
463 u8 mode;
464
465 /* we always use the 0 slot for crap hardware */
466 unit = dev->devno;
467 if (!(gtm->flags & 0x10))
468 unit = 0;
469
470 /* PIO */
471 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
472 xfer_mask |= ata_xfer_mode2mask(mode);
473
474 /* See if we have MWDMA or UDMA data. We don't bother with
475 * MWDMA if UDMA is available as this means the BIOS set UDMA
476 * and our error changedown if it works is UDMA to PIO anyway.
477 */
478 if (!(gtm->flags & (1 << (2 * unit))))
479 type = ATA_SHIFT_MWDMA;
480 else
481 type = ATA_SHIFT_UDMA;
482
483 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
484 xfer_mask |= ata_xfer_mode2mask(mode);
485
486 return xfer_mask;
487}
488EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
489
490/**
445 * ata_acpi_cbl_80wire - Check for 80 wire cable 491 * ata_acpi_cbl_80wire - Check for 80 wire cable
446 * @ap: Port to check 492 * @ap: Port to check
493 * @gtm: GTM data to use
447 * 494 *
448 * Return 1 if the ACPI mode data for this port indicates the BIOS selected 495 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
449 * an 80wire mode.
450 */ 496 */
451 497int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
452int ata_acpi_cbl_80wire(struct ata_port *ap)
453{ 498{
454 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap); 499 struct ata_device *dev;
455 int valid = 0;
456 500
457 if (!gtm) 501 ata_link_for_each_dev(dev, &ap->link) {
458 return 0; 502 unsigned long xfer_mask, udma_mask;
503
504 if (!ata_dev_enabled(dev))
505 continue;
506
507 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
508 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
509
510 if (udma_mask & ~ATA_UDMA_MASK_40C)
511 return 1;
512 }
459 513
460 /* Split timing, DMA enabled */
461 if ((gtm->flags & 0x11) == 0x11 && gtm->drive[0].dma < 55)
462 valid |= 1;
463 if ((gtm->flags & 0x14) == 0x14 && gtm->drive[1].dma < 55)
464 valid |= 2;
465 /* Shared timing, DMA enabled */
466 if ((gtm->flags & 0x11) == 0x01 && gtm->drive[0].dma < 55)
467 valid |= 1;
468 if ((gtm->flags & 0x14) == 0x04 && gtm->drive[0].dma < 55)
469 valid |= 2;
470
471 /* Drive check */
472 if ((valid & 1) && ata_dev_enabled(&ap->link.device[0]))
473 return 1;
474 if ((valid & 2) && ata_dev_enabled(&ap->link.device[1]))
475 return 1;
476 return 0; 514 return 0;
477} 515}
478
479EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire); 516EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
480 517
481static void ata_acpi_gtf_to_tf(struct ata_device *dev, 518static void ata_acpi_gtf_to_tf(struct ata_device *dev,
@@ -776,6 +813,36 @@ void ata_acpi_on_resume(struct ata_port *ap)
776} 813}
777 814
778/** 815/**
816 * ata_acpi_set_state - set the port power state
817 * @ap: target ATA port
818 * @state: state, on/off
819 *
820 * This function executes the _PS0/_PS3 ACPI method to set the power state.
821 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
822 */
823void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
824{
825 struct ata_device *dev;
826
827 if (!ap->acpi_handle || (ap->flags & ATA_FLAG_ACPI_SATA))
828 return;
829
830 /* channel first and then drives for power on and vica versa
831 for power off */
832 if (state.event == PM_EVENT_ON)
833 acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D0);
834
835 ata_link_for_each_dev(dev, &ap->link) {
836 if (dev->acpi_handle && ata_dev_enabled(dev))
837 acpi_bus_set_power(dev->acpi_handle,
838 state.event == PM_EVENT_ON ?
839 ACPI_STATE_D0 : ACPI_STATE_D3);
840 }
841 if (state.event != PM_EVENT_ON)
842 acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D3);
843}
844
845/**
779 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration 846 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
780 * @dev: target ATA device 847 * @dev: target ATA device
781 * 848 *
diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index 6380726f7538..ce803d18e96a 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -119,6 +119,10 @@ int libata_noacpi = 0;
119module_param_named(noacpi, libata_noacpi, int, 0444); 119module_param_named(noacpi, libata_noacpi, int, 0444);
120MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set"); 120MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
121 121
122int libata_allow_tpm = 0;
123module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
124MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
125
122MODULE_AUTHOR("Jeff Garzik"); 126MODULE_AUTHOR("Jeff Garzik");
123MODULE_DESCRIPTION("Library module for ATA devices"); 127MODULE_DESCRIPTION("Library module for ATA devices");
124MODULE_LICENSE("GPL"); 128MODULE_LICENSE("GPL");
@@ -450,9 +454,9 @@ int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
450 * RETURNS: 454 * RETURNS:
451 * Packed xfer_mask. 455 * Packed xfer_mask.
452 */ 456 */
453static unsigned int ata_pack_xfermask(unsigned int pio_mask, 457unsigned long ata_pack_xfermask(unsigned long pio_mask,
454 unsigned int mwdma_mask, 458 unsigned long mwdma_mask,
455 unsigned int udma_mask) 459 unsigned long udma_mask)
456{ 460{
457 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) | 461 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
458 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) | 462 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
@@ -469,10 +473,8 @@ static unsigned int ata_pack_xfermask(unsigned int pio_mask,
469 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask. 473 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
470 * Any NULL distination masks will be ignored. 474 * Any NULL distination masks will be ignored.
471 */ 475 */
472static void ata_unpack_xfermask(unsigned int xfer_mask, 476void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
473 unsigned int *pio_mask, 477 unsigned long *mwdma_mask, unsigned long *udma_mask)
474 unsigned int *mwdma_mask,
475 unsigned int *udma_mask)
476{ 478{
477 if (pio_mask) 479 if (pio_mask)
478 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO; 480 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
@@ -486,9 +488,9 @@ static const struct ata_xfer_ent {
486 int shift, bits; 488 int shift, bits;
487 u8 base; 489 u8 base;
488} ata_xfer_tbl[] = { 490} ata_xfer_tbl[] = {
489 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 }, 491 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
490 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 }, 492 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
491 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 }, 493 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
492 { -1, }, 494 { -1, },
493}; 495};
494 496
@@ -503,9 +505,9 @@ static const struct ata_xfer_ent {
503 * None. 505 * None.
504 * 506 *
505 * RETURNS: 507 * RETURNS:
506 * Matching XFER_* value, 0 if no match found. 508 * Matching XFER_* value, 0xff if no match found.
507 */ 509 */
508static u8 ata_xfer_mask2mode(unsigned int xfer_mask) 510u8 ata_xfer_mask2mode(unsigned long xfer_mask)
509{ 511{
510 int highbit = fls(xfer_mask) - 1; 512 int highbit = fls(xfer_mask) - 1;
511 const struct ata_xfer_ent *ent; 513 const struct ata_xfer_ent *ent;
@@ -513,7 +515,7 @@ static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
513 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) 515 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
514 if (highbit >= ent->shift && highbit < ent->shift + ent->bits) 516 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
515 return ent->base + highbit - ent->shift; 517 return ent->base + highbit - ent->shift;
516 return 0; 518 return 0xff;
517} 519}
518 520
519/** 521/**
@@ -528,13 +530,14 @@ static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
528 * RETURNS: 530 * RETURNS:
529 * Matching xfer_mask, 0 if no match found. 531 * Matching xfer_mask, 0 if no match found.
530 */ 532 */
531static unsigned int ata_xfer_mode2mask(u8 xfer_mode) 533unsigned long ata_xfer_mode2mask(u8 xfer_mode)
532{ 534{
533 const struct ata_xfer_ent *ent; 535 const struct ata_xfer_ent *ent;
534 536
535 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) 537 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
536 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) 538 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
537 return 1 << (ent->shift + xfer_mode - ent->base); 539 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
540 & ~((1 << ent->shift) - 1);
538 return 0; 541 return 0;
539} 542}
540 543
@@ -550,7 +553,7 @@ static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
550 * RETURNS: 553 * RETURNS:
551 * Matching xfer_shift, -1 if no match found. 554 * Matching xfer_shift, -1 if no match found.
552 */ 555 */
553static int ata_xfer_mode2shift(unsigned int xfer_mode) 556int ata_xfer_mode2shift(unsigned long xfer_mode)
554{ 557{
555 const struct ata_xfer_ent *ent; 558 const struct ata_xfer_ent *ent;
556 559
@@ -574,7 +577,7 @@ static int ata_xfer_mode2shift(unsigned int xfer_mode)
574 * Constant C string representing highest speed listed in 577 * Constant C string representing highest speed listed in
575 * @mode_mask, or the constant C string "<n/a>". 578 * @mode_mask, or the constant C string "<n/a>".
576 */ 579 */
577static const char *ata_mode_string(unsigned int xfer_mask) 580const char *ata_mode_string(unsigned long xfer_mask)
578{ 581{
579 static const char * const xfer_mode_str[] = { 582 static const char * const xfer_mode_str[] = {
580 "PIO0", 583 "PIO0",
@@ -947,8 +950,8 @@ unsigned int ata_dev_try_classify(struct ata_device *dev, int present,
947 if (r_err) 950 if (r_err)
948 *r_err = err; 951 *r_err = err;
949 952
950 /* see if device passed diags: if master then continue and warn later */ 953 /* see if device passed diags: continue and warn later */
951 if (err == 0 && dev->devno == 0) 954 if (err == 0)
952 /* diagnostic fail : do nothing _YET_ */ 955 /* diagnostic fail : do nothing _YET_ */
953 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC; 956 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
954 else if (err == 1) 957 else if (err == 1)
@@ -1286,48 +1289,6 @@ static int ata_hpa_resize(struct ata_device *dev)
1286} 1289}
1287 1290
1288/** 1291/**
1289 * ata_id_to_dma_mode - Identify DMA mode from id block
1290 * @dev: device to identify
1291 * @unknown: mode to assume if we cannot tell
1292 *
1293 * Set up the timing values for the device based upon the identify
1294 * reported values for the DMA mode. This function is used by drivers
1295 * which rely upon firmware configured modes, but wish to report the
1296 * mode correctly when possible.
1297 *
1298 * In addition we emit similarly formatted messages to the default
1299 * ata_dev_set_mode handler, in order to provide consistency of
1300 * presentation.
1301 */
1302
1303void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown)
1304{
1305 unsigned int mask;
1306 u8 mode;
1307
1308 /* Pack the DMA modes */
1309 mask = ((dev->id[63] >> 8) << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA;
1310 if (dev->id[53] & 0x04)
1311 mask |= ((dev->id[88] >> 8) << ATA_SHIFT_UDMA) & ATA_MASK_UDMA;
1312
1313 /* Select the mode in use */
1314 mode = ata_xfer_mask2mode(mask);
1315
1316 if (mode != 0) {
1317 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
1318 ata_mode_string(mask));
1319 } else {
1320 /* SWDMA perhaps ? */
1321 mode = unknown;
1322 ata_dev_printk(dev, KERN_INFO, "configured for DMA\n");
1323 }
1324
1325 /* Configure the device reporting */
1326 dev->xfer_mode = mode;
1327 dev->xfer_shift = ata_xfer_mode2shift(mode);
1328}
1329
1330/**
1331 * ata_noop_dev_select - Select device 0/1 on ATA bus 1292 * ata_noop_dev_select - Select device 0/1 on ATA bus
1332 * @ap: ATA channel to manipulate 1293 * @ap: ATA channel to manipulate
1333 * @device: ATA device (numbered from zero) to select 1294 * @device: ATA device (numbered from zero) to select
@@ -1464,9 +1425,9 @@ static inline void ata_dump_id(const u16 *id)
1464 * RETURNS: 1425 * RETURNS:
1465 * Computed xfermask 1426 * Computed xfermask
1466 */ 1427 */
1467static unsigned int ata_id_xfermask(const u16 *id) 1428unsigned long ata_id_xfermask(const u16 *id)
1468{ 1429{
1469 unsigned int pio_mask, mwdma_mask, udma_mask; 1430 unsigned long pio_mask, mwdma_mask, udma_mask;
1470 1431
1471 /* Usual case. Word 53 indicates word 64 is valid */ 1432 /* Usual case. Word 53 indicates word 64 is valid */
1472 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) { 1433 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
@@ -1519,7 +1480,7 @@ static unsigned int ata_id_xfermask(const u16 *id)
1519} 1480}
1520 1481
1521/** 1482/**
1522 * ata_port_queue_task - Queue port_task 1483 * ata_pio_queue_task - Queue port_task
1523 * @ap: The ata_port to queue port_task for 1484 * @ap: The ata_port to queue port_task for
1524 * @fn: workqueue function to be scheduled 1485 * @fn: workqueue function to be scheduled
1525 * @data: data for @fn to use 1486 * @data: data for @fn to use
@@ -1531,16 +1492,15 @@ static unsigned int ata_id_xfermask(const u16 *id)
1531 * one task is active at any given time. 1492 * one task is active at any given time.
1532 * 1493 *
1533 * libata core layer takes care of synchronization between 1494 * libata core layer takes care of synchronization between
1534 * port_task and EH. ata_port_queue_task() may be ignored for EH 1495 * port_task and EH. ata_pio_queue_task() may be ignored for EH
1535 * synchronization. 1496 * synchronization.
1536 * 1497 *
1537 * LOCKING: 1498 * LOCKING:
1538 * Inherited from caller. 1499 * Inherited from caller.
1539 */ 1500 */
1540void ata_port_queue_task(struct ata_port *ap, work_func_t fn, void *data, 1501static void ata_pio_queue_task(struct ata_port *ap, void *data,
1541 unsigned long delay) 1502 unsigned long delay)
1542{ 1503{
1543 PREPARE_DELAYED_WORK(&ap->port_task, fn);
1544 ap->port_task_data = data; 1504 ap->port_task_data = data;
1545 1505
1546 /* may fail if ata_port_flush_task() in progress */ 1506 /* may fail if ata_port_flush_task() in progress */
@@ -2090,7 +2050,7 @@ int ata_dev_configure(struct ata_device *dev)
2090 struct ata_eh_context *ehc = &dev->link->eh_context; 2050 struct ata_eh_context *ehc = &dev->link->eh_context;
2091 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO; 2051 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2092 const u16 *id = dev->id; 2052 const u16 *id = dev->id;
2093 unsigned int xfer_mask; 2053 unsigned long xfer_mask;
2094 char revbuf[7]; /* XYZ-99\0 */ 2054 char revbuf[7]; /* XYZ-99\0 */
2095 char fwrevbuf[ATA_ID_FW_REV_LEN+1]; 2055 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2096 char modelbuf[ATA_ID_PROD_LEN+1]; 2056 char modelbuf[ATA_ID_PROD_LEN+1];
@@ -2161,8 +2121,14 @@ int ata_dev_configure(struct ata_device *dev)
2161 "supports DRM functions and may " 2121 "supports DRM functions and may "
2162 "not be fully accessable.\n"); 2122 "not be fully accessable.\n");
2163 snprintf(revbuf, 7, "CFA"); 2123 snprintf(revbuf, 7, "CFA");
2164 } else 2124 } else {
2165 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id)); 2125 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2126 /* Warn the user if the device has TPM extensions */
2127 if (ata_id_has_tpm(id))
2128 ata_dev_printk(dev, KERN_WARNING,
2129 "supports DRM functions and may "
2130 "not be fully accessable.\n");
2131 }
2166 2132
2167 dev->n_sectors = ata_id_n_sectors(id); 2133 dev->n_sectors = ata_id_n_sectors(id);
2168 2134
@@ -2295,19 +2261,8 @@ int ata_dev_configure(struct ata_device *dev)
2295 dev->flags |= ATA_DFLAG_DIPM; 2261 dev->flags |= ATA_DFLAG_DIPM;
2296 } 2262 }
2297 2263
2298 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) { 2264 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2299 /* Let the user know. We don't want to disallow opens for 2265 200 sectors */
2300 rescue purposes, or in case the vendor is just a blithering
2301 idiot */
2302 if (print_info) {
2303 ata_dev_printk(dev, KERN_WARNING,
2304"Drive reports diagnostics failure. This may indicate a drive\n");
2305 ata_dev_printk(dev, KERN_WARNING,
2306"fault or invalid emulation. Contact drive vendor for information.\n");
2307 }
2308 }
2309
2310 /* limit bridge transfers to udma5, 200 sectors */
2311 if (ata_dev_knobble(dev)) { 2266 if (ata_dev_knobble(dev)) {
2312 if (ata_msg_drv(ap) && print_info) 2267 if (ata_msg_drv(ap) && print_info)
2313 ata_dev_printk(dev, KERN_INFO, 2268 ata_dev_printk(dev, KERN_INFO,
@@ -2336,6 +2291,21 @@ int ata_dev_configure(struct ata_device *dev)
2336 if (ap->ops->dev_config) 2291 if (ap->ops->dev_config)
2337 ap->ops->dev_config(dev); 2292 ap->ops->dev_config(dev);
2338 2293
2294 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2295 /* Let the user know. We don't want to disallow opens for
2296 rescue purposes, or in case the vendor is just a blithering
2297 idiot. Do this after the dev_config call as some controllers
2298 with buggy firmware may want to avoid reporting false device
2299 bugs */
2300
2301 if (print_info) {
2302 ata_dev_printk(dev, KERN_WARNING,
2303"Drive reports diagnostics failure. This may indicate a drive\n");
2304 ata_dev_printk(dev, KERN_WARNING,
2305"fault or invalid emulation. Contact drive vendor for information.\n");
2306 }
2307 }
2308
2339 if (ata_msg_probe(ap)) 2309 if (ata_msg_probe(ap))
2340 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n", 2310 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2341 __FUNCTION__, ata_chk_status(ap)); 2311 __FUNCTION__, ata_chk_status(ap));
@@ -2387,6 +2357,18 @@ int ata_cable_unknown(struct ata_port *ap)
2387} 2357}
2388 2358
2389/** 2359/**
2360 * ata_cable_ignore - return ignored PATA cable.
2361 * @ap: port
2362 *
2363 * Helper method for drivers which don't use cable type to limit
2364 * transfer mode.
2365 */
2366int ata_cable_ignore(struct ata_port *ap)
2367{
2368 return ATA_CBL_PATA_IGN;
2369}
2370
2371/**
2390 * ata_cable_sata - return SATA cable type 2372 * ata_cable_sata - return SATA cable type
2391 * @ap: port 2373 * @ap: port
2392 * 2374 *
@@ -2781,38 +2763,33 @@ int sata_set_spd(struct ata_link *link)
2781 */ 2763 */
2782 2764
2783static const struct ata_timing ata_timing[] = { 2765static const struct ata_timing ata_timing[] = {
2766/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2767 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2768 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2769 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2770 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2771 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2772 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
2773 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2784 2774
2785 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 }, 2775 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2786 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 }, 2776 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2787 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 }, 2777 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2788 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2789 2778
2790 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 }, 2779 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
2780 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2781 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2791 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 }, 2782 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
2792 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 }, 2783 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2793 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2794 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2795 2784
2796/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */ 2785/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
2797 2786 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2798 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 }, 2787 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2799 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 }, 2788 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2800 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 }, 2789 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2801 2790 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2802 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 }, 2791 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2803 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 }, 2792 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2804 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2805
2806 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2807 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
2808 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2809 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2810
2811 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2812 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2813 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2814
2815/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2816 2793
2817 { 0xFF } 2794 { 0xFF }
2818}; 2795};
@@ -2845,14 +2822,16 @@ void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2845 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma); 2822 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2846} 2823}
2847 2824
2848static const struct ata_timing *ata_timing_find_mode(unsigned short speed) 2825const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
2849{ 2826{
2850 const struct ata_timing *t; 2827 const struct ata_timing *t = ata_timing;
2828
2829 while (xfer_mode > t->mode)
2830 t++;
2851 2831
2852 for (t = ata_timing; t->mode != speed; t++) 2832 if (xfer_mode == t->mode)
2853 if (t->mode == 0xFF) 2833 return t;
2854 return NULL; 2834 return NULL;
2855 return t;
2856} 2835}
2857 2836
2858int ata_timing_compute(struct ata_device *adev, unsigned short speed, 2837int ata_timing_compute(struct ata_device *adev, unsigned short speed,
@@ -2927,6 +2906,57 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2927} 2906}
2928 2907
2929/** 2908/**
2909 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
2910 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
2911 * @cycle: cycle duration in ns
2912 *
2913 * Return matching xfer mode for @cycle. The returned mode is of
2914 * the transfer type specified by @xfer_shift. If @cycle is too
2915 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
2916 * than the fastest known mode, the fasted mode is returned.
2917 *
2918 * LOCKING:
2919 * None.
2920 *
2921 * RETURNS:
2922 * Matching xfer_mode, 0xff if no match found.
2923 */
2924u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
2925{
2926 u8 base_mode = 0xff, last_mode = 0xff;
2927 const struct ata_xfer_ent *ent;
2928 const struct ata_timing *t;
2929
2930 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
2931 if (ent->shift == xfer_shift)
2932 base_mode = ent->base;
2933
2934 for (t = ata_timing_find_mode(base_mode);
2935 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
2936 unsigned short this_cycle;
2937
2938 switch (xfer_shift) {
2939 case ATA_SHIFT_PIO:
2940 case ATA_SHIFT_MWDMA:
2941 this_cycle = t->cycle;
2942 break;
2943 case ATA_SHIFT_UDMA:
2944 this_cycle = t->udma;
2945 break;
2946 default:
2947 return 0xff;
2948 }
2949
2950 if (cycle > this_cycle)
2951 break;
2952
2953 last_mode = t->mode;
2954 }
2955
2956 return last_mode;
2957}
2958
2959/**
2930 * ata_down_xfermask_limit - adjust dev xfer masks downward 2960 * ata_down_xfermask_limit - adjust dev xfer masks downward
2931 * @dev: Device to adjust xfer masks 2961 * @dev: Device to adjust xfer masks
2932 * @sel: ATA_DNXFER_* selector 2962 * @sel: ATA_DNXFER_* selector
@@ -2944,8 +2974,8 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2944int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel) 2974int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
2945{ 2975{
2946 char buf[32]; 2976 char buf[32];
2947 unsigned int orig_mask, xfer_mask; 2977 unsigned long orig_mask, xfer_mask;
2948 unsigned int pio_mask, mwdma_mask, udma_mask; 2978 unsigned long pio_mask, mwdma_mask, udma_mask;
2949 int quiet, highbit; 2979 int quiet, highbit;
2950 2980
2951 quiet = !!(sel & ATA_DNXFER_QUIET); 2981 quiet = !!(sel & ATA_DNXFER_QUIET);
@@ -3039,7 +3069,7 @@ static int ata_dev_set_mode(struct ata_device *dev)
3039 3069
3040 /* Early MWDMA devices do DMA but don't allow DMA mode setting. 3070 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3041 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */ 3071 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3042 if (dev->xfer_shift == ATA_SHIFT_MWDMA && 3072 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3043 dev->dma_mode == XFER_MW_DMA_0 && 3073 dev->dma_mode == XFER_MW_DMA_0 &&
3044 (dev->id[63] >> 8) & 1) 3074 (dev->id[63] >> 8) & 1)
3045 err_mask &= ~AC_ERR_DEV; 3075 err_mask &= ~AC_ERR_DEV;
@@ -3089,7 +3119,7 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3089 3119
3090 /* step 1: calculate xfer_mask */ 3120 /* step 1: calculate xfer_mask */
3091 ata_link_for_each_dev(dev, link) { 3121 ata_link_for_each_dev(dev, link) {
3092 unsigned int pio_mask, dma_mask; 3122 unsigned long pio_mask, dma_mask;
3093 unsigned int mode_mask; 3123 unsigned int mode_mask;
3094 3124
3095 if (!ata_dev_enabled(dev)) 3125 if (!ata_dev_enabled(dev))
@@ -3115,7 +3145,7 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3115 dev->dma_mode = ata_xfer_mask2mode(dma_mask); 3145 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3116 3146
3117 found = 1; 3147 found = 1;
3118 if (dev->dma_mode) 3148 if (dev->dma_mode != 0xff)
3119 used_dma = 1; 3149 used_dma = 1;
3120 } 3150 }
3121 if (!found) 3151 if (!found)
@@ -3126,7 +3156,7 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3126 if (!ata_dev_enabled(dev)) 3156 if (!ata_dev_enabled(dev))
3127 continue; 3157 continue;
3128 3158
3129 if (!dev->pio_mode) { 3159 if (dev->pio_mode == 0xff) {
3130 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n"); 3160 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3131 rc = -EINVAL; 3161 rc = -EINVAL;
3132 goto out; 3162 goto out;
@@ -3140,7 +3170,7 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3140 3170
3141 /* step 3: set host DMA timings */ 3171 /* step 3: set host DMA timings */
3142 ata_link_for_each_dev(dev, link) { 3172 ata_link_for_each_dev(dev, link) {
3143 if (!ata_dev_enabled(dev) || !dev->dma_mode) 3173 if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff)
3144 continue; 3174 continue;
3145 3175
3146 dev->xfer_mode = dev->dma_mode; 3176 dev->xfer_mode = dev->dma_mode;
@@ -3173,31 +3203,6 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3173} 3203}
3174 3204
3175/** 3205/**
3176 * ata_set_mode - Program timings and issue SET FEATURES - XFER
3177 * @link: link on which timings will be programmed
3178 * @r_failed_dev: out paramter for failed device
3179 *
3180 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3181 * ata_set_mode() fails, pointer to the failing device is
3182 * returned in @r_failed_dev.
3183 *
3184 * LOCKING:
3185 * PCI/etc. bus probe sem.
3186 *
3187 * RETURNS:
3188 * 0 on success, negative errno otherwise
3189 */
3190int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3191{
3192 struct ata_port *ap = link->ap;
3193
3194 /* has private set_mode? */
3195 if (ap->ops->set_mode)
3196 return ap->ops->set_mode(link, r_failed_dev);
3197 return ata_do_set_mode(link, r_failed_dev);
3198}
3199
3200/**
3201 * ata_tf_to_host - issue ATA taskfile to host controller 3206 * ata_tf_to_host - issue ATA taskfile to host controller
3202 * @ap: port to which command is being issued 3207 * @ap: port to which command is being issued
3203 * @tf: ATA taskfile register set 3208 * @tf: ATA taskfile register set
@@ -4363,7 +4368,14 @@ static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4363 tf.feature = SETFEATURES_XFER; 4368 tf.feature = SETFEATURES_XFER;
4364 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING; 4369 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4365 tf.protocol = ATA_PROT_NODATA; 4370 tf.protocol = ATA_PROT_NODATA;
4366 tf.nsect = dev->xfer_mode; 4371 /* If we are using IORDY we must send the mode setting command */
4372 if (ata_pio_need_iordy(dev))
4373 tf.nsect = dev->xfer_mode;
4374 /* If the device has IORDY and the controller does not - turn it off */
4375 else if (ata_id_has_iordy(dev->id))
4376 tf.nsect = 0x01;
4377 else /* In the ancient relic department - skip all of this */
4378 return 0;
4367 4379
4368 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 4380 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4369 4381
@@ -4462,17 +4474,13 @@ static unsigned int ata_dev_init_params(struct ata_device *dev,
4462void ata_sg_clean(struct ata_queued_cmd *qc) 4474void ata_sg_clean(struct ata_queued_cmd *qc)
4463{ 4475{
4464 struct ata_port *ap = qc->ap; 4476 struct ata_port *ap = qc->ap;
4465 struct scatterlist *sg = qc->__sg; 4477 struct scatterlist *sg = qc->sg;
4466 int dir = qc->dma_dir; 4478 int dir = qc->dma_dir;
4467 void *pad_buf = NULL; 4479 void *pad_buf = NULL;
4468 4480
4469 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
4470 WARN_ON(sg == NULL); 4481 WARN_ON(sg == NULL);
4471 4482
4472 if (qc->flags & ATA_QCFLAG_SINGLE) 4483 VPRINTK("unmapping %u sg elements\n", qc->mapped_n_elem);
4473 WARN_ON(qc->n_elem > 1);
4474
4475 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4476 4484
4477 /* if we padded the buffer out to 32-bit bound, and data 4485 /* if we padded the buffer out to 32-bit bound, and data
4478 * xfer direction is from-device, we must copy from the 4486 * xfer direction is from-device, we must copy from the
@@ -4481,31 +4489,20 @@ void ata_sg_clean(struct ata_queued_cmd *qc)
4481 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE)) 4489 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
4482 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ); 4490 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4483 4491
4484 if (qc->flags & ATA_QCFLAG_SG) { 4492 if (qc->mapped_n_elem)
4485 if (qc->n_elem) 4493 dma_unmap_sg(ap->dev, sg, qc->mapped_n_elem, dir);
4486 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir); 4494 /* restore last sg */
4487 /* restore last sg */ 4495 if (qc->last_sg)
4488 sg_last(sg, qc->orig_n_elem)->length += qc->pad_len; 4496 *qc->last_sg = qc->saved_last_sg;
4489 if (pad_buf) { 4497 if (pad_buf) {
4490 struct scatterlist *psg = &qc->pad_sgent; 4498 struct scatterlist *psg = &qc->extra_sg[1];
4491 void *addr = kmap_atomic(sg_page(psg), KM_IRQ0); 4499 void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
4492 memcpy(addr + psg->offset, pad_buf, qc->pad_len); 4500 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
4493 kunmap_atomic(addr, KM_IRQ0); 4501 kunmap_atomic(addr, KM_IRQ0);
4494 }
4495 } else {
4496 if (qc->n_elem)
4497 dma_unmap_single(ap->dev,
4498 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
4499 dir);
4500 /* restore sg */
4501 sg->length += qc->pad_len;
4502 if (pad_buf)
4503 memcpy(qc->buf_virt + sg->length - qc->pad_len,
4504 pad_buf, qc->pad_len);
4505 } 4502 }
4506 4503
4507 qc->flags &= ~ATA_QCFLAG_DMAMAP; 4504 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4508 qc->__sg = NULL; 4505 qc->sg = NULL;
4509} 4506}
4510 4507
4511/** 4508/**
@@ -4523,13 +4520,10 @@ static void ata_fill_sg(struct ata_queued_cmd *qc)
4523{ 4520{
4524 struct ata_port *ap = qc->ap; 4521 struct ata_port *ap = qc->ap;
4525 struct scatterlist *sg; 4522 struct scatterlist *sg;
4526 unsigned int idx; 4523 unsigned int si, pi;
4527 4524
4528 WARN_ON(qc->__sg == NULL); 4525 pi = 0;
4529 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0); 4526 for_each_sg(qc->sg, sg, qc->n_elem, si) {
4530
4531 idx = 0;
4532 ata_for_each_sg(sg, qc) {
4533 u32 addr, offset; 4527 u32 addr, offset;
4534 u32 sg_len, len; 4528 u32 sg_len, len;
4535 4529
@@ -4546,18 +4540,17 @@ static void ata_fill_sg(struct ata_queued_cmd *qc)
4546 if ((offset + sg_len) > 0x10000) 4540 if ((offset + sg_len) > 0x10000)
4547 len = 0x10000 - offset; 4541 len = 0x10000 - offset;
4548 4542
4549 ap->prd[idx].addr = cpu_to_le32(addr); 4543 ap->prd[pi].addr = cpu_to_le32(addr);
4550 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff); 4544 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
4551 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len); 4545 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
4552 4546
4553 idx++; 4547 pi++;
4554 sg_len -= len; 4548 sg_len -= len;
4555 addr += len; 4549 addr += len;
4556 } 4550 }
4557 } 4551 }
4558 4552
4559 if (idx) 4553 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4560 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4561} 4554}
4562 4555
4563/** 4556/**
@@ -4577,13 +4570,10 @@ static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4577{ 4570{
4578 struct ata_port *ap = qc->ap; 4571 struct ata_port *ap = qc->ap;
4579 struct scatterlist *sg; 4572 struct scatterlist *sg;
4580 unsigned int idx; 4573 unsigned int si, pi;
4581
4582 WARN_ON(qc->__sg == NULL);
4583 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
4584 4574
4585 idx = 0; 4575 pi = 0;
4586 ata_for_each_sg(sg, qc) { 4576 for_each_sg(qc->sg, sg, qc->n_elem, si) {
4587 u32 addr, offset; 4577 u32 addr, offset;
4588 u32 sg_len, len, blen; 4578 u32 sg_len, len, blen;
4589 4579
@@ -4601,25 +4591,24 @@ static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4601 len = 0x10000 - offset; 4591 len = 0x10000 - offset;
4602 4592
4603 blen = len & 0xffff; 4593 blen = len & 0xffff;
4604 ap->prd[idx].addr = cpu_to_le32(addr); 4594 ap->prd[pi].addr = cpu_to_le32(addr);
4605 if (blen == 0) { 4595 if (blen == 0) {
4606 /* Some PATA chipsets like the CS5530 can't 4596 /* Some PATA chipsets like the CS5530 can't
4607 cope with 0x0000 meaning 64K as the spec says */ 4597 cope with 0x0000 meaning 64K as the spec says */
4608 ap->prd[idx].flags_len = cpu_to_le32(0x8000); 4598 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
4609 blen = 0x8000; 4599 blen = 0x8000;
4610 ap->prd[++idx].addr = cpu_to_le32(addr + 0x8000); 4600 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
4611 } 4601 }
4612 ap->prd[idx].flags_len = cpu_to_le32(blen); 4602 ap->prd[pi].flags_len = cpu_to_le32(blen);
4613 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len); 4603 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
4614 4604
4615 idx++; 4605 pi++;
4616 sg_len -= len; 4606 sg_len -= len;
4617 addr += len; 4607 addr += len;
4618 } 4608 }
4619 } 4609 }
4620 4610
4621 if (idx) 4611 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4622 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4623} 4612}
4624 4613
4625/** 4614/**
@@ -4669,8 +4658,8 @@ int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4669 */ 4658 */
4670static int atapi_qc_may_overflow(struct ata_queued_cmd *qc) 4659static int atapi_qc_may_overflow(struct ata_queued_cmd *qc)
4671{ 4660{
4672 if (qc->tf.protocol != ATA_PROT_ATAPI && 4661 if (qc->tf.protocol != ATAPI_PROT_PIO &&
4673 qc->tf.protocol != ATA_PROT_ATAPI_DMA) 4662 qc->tf.protocol != ATAPI_PROT_DMA)
4674 return 0; 4663 return 0;
4675 4664
4676 if (qc->tf.flags & ATA_TFLAG_WRITE) 4665 if (qc->tf.flags & ATA_TFLAG_WRITE)
@@ -4756,33 +4745,6 @@ void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
4756void ata_noop_qc_prep(struct ata_queued_cmd *qc) { } 4745void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4757 4746
4758/** 4747/**
4759 * ata_sg_init_one - Associate command with memory buffer
4760 * @qc: Command to be associated
4761 * @buf: Memory buffer
4762 * @buflen: Length of memory buffer, in bytes.
4763 *
4764 * Initialize the data-related elements of queued_cmd @qc
4765 * to point to a single memory buffer, @buf of byte length @buflen.
4766 *
4767 * LOCKING:
4768 * spin_lock_irqsave(host lock)
4769 */
4770
4771void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4772{
4773 qc->flags |= ATA_QCFLAG_SINGLE;
4774
4775 qc->__sg = &qc->sgent;
4776 qc->n_elem = 1;
4777 qc->orig_n_elem = 1;
4778 qc->buf_virt = buf;
4779 qc->nbytes = buflen;
4780 qc->cursg = qc->__sg;
4781
4782 sg_init_one(&qc->sgent, buf, buflen);
4783}
4784
4785/**
4786 * ata_sg_init - Associate command with scatter-gather table. 4748 * ata_sg_init - Associate command with scatter-gather table.
4787 * @qc: Command to be associated 4749 * @qc: Command to be associated
4788 * @sg: Scatter-gather table. 4750 * @sg: Scatter-gather table.
@@ -4795,84 +4757,103 @@ void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4795 * LOCKING: 4757 * LOCKING:
4796 * spin_lock_irqsave(host lock) 4758 * spin_lock_irqsave(host lock)
4797 */ 4759 */
4798
4799void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg, 4760void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4800 unsigned int n_elem) 4761 unsigned int n_elem)
4801{ 4762{
4802 qc->flags |= ATA_QCFLAG_SG; 4763 qc->sg = sg;
4803 qc->__sg = sg;
4804 qc->n_elem = n_elem; 4764 qc->n_elem = n_elem;
4805 qc->orig_n_elem = n_elem; 4765 qc->cursg = qc->sg;
4806 qc->cursg = qc->__sg;
4807} 4766}
4808 4767
4809/** 4768static unsigned int ata_sg_setup_extra(struct ata_queued_cmd *qc,
4810 * ata_sg_setup_one - DMA-map the memory buffer associated with a command. 4769 unsigned int *n_elem_extra,
4811 * @qc: Command with memory buffer to be mapped. 4770 unsigned int *nbytes_extra)
4812 *
4813 * DMA-map the memory buffer associated with queued_cmd @qc.
4814 *
4815 * LOCKING:
4816 * spin_lock_irqsave(host lock)
4817 *
4818 * RETURNS:
4819 * Zero on success, negative on error.
4820 */
4821
4822static int ata_sg_setup_one(struct ata_queued_cmd *qc)
4823{ 4771{
4824 struct ata_port *ap = qc->ap; 4772 struct ata_port *ap = qc->ap;
4825 int dir = qc->dma_dir; 4773 unsigned int n_elem = qc->n_elem;
4826 struct scatterlist *sg = qc->__sg; 4774 struct scatterlist *lsg, *copy_lsg = NULL, *tsg = NULL, *esg = NULL;
4827 dma_addr_t dma_address; 4775
4828 int trim_sg = 0; 4776 *n_elem_extra = 0;
4777 *nbytes_extra = 0;
4778
4779 /* needs padding? */
4780 qc->pad_len = qc->nbytes & 3;
4781
4782 if (likely(!qc->pad_len))
4783 return n_elem;
4784
4785 /* locate last sg and save it */
4786 lsg = sg_last(qc->sg, n_elem);
4787 qc->last_sg = lsg;
4788 qc->saved_last_sg = *lsg;
4789
4790 sg_init_table(qc->extra_sg, ARRAY_SIZE(qc->extra_sg));
4829 4791
4830 /* we must lengthen transfers to end on a 32-bit boundary */
4831 qc->pad_len = sg->length & 3;
4832 if (qc->pad_len) { 4792 if (qc->pad_len) {
4793 struct scatterlist *psg = &qc->extra_sg[1];
4833 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ); 4794 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4834 struct scatterlist *psg = &qc->pad_sgent; 4795 unsigned int offset;
4835 4796
4836 WARN_ON(qc->dev->class != ATA_DEV_ATAPI); 4797 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
4837 4798
4838 memset(pad_buf, 0, ATA_DMA_PAD_SZ); 4799 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4839 4800
4840 if (qc->tf.flags & ATA_TFLAG_WRITE) 4801 /* psg->page/offset are used to copy to-be-written
4841 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len, 4802 * data in this function or read data in ata_sg_clean.
4842 qc->pad_len); 4803 */
4804 offset = lsg->offset + lsg->length - qc->pad_len;
4805 sg_set_page(psg, nth_page(sg_page(lsg), offset >> PAGE_SHIFT),
4806 qc->pad_len, offset_in_page(offset));
4807
4808 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4809 void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
4810 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
4811 kunmap_atomic(addr, KM_IRQ0);
4812 }
4843 4813
4844 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ); 4814 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4845 sg_dma_len(psg) = ATA_DMA_PAD_SZ; 4815 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4846 /* trim sg */
4847 sg->length -= qc->pad_len;
4848 if (sg->length == 0)
4849 trim_sg = 1;
4850 4816
4851 DPRINTK("padding done, sg->length=%u pad_len=%u\n", 4817 /* Trim the last sg entry and chain the original and
4852 sg->length, qc->pad_len); 4818 * padding sg lists.
4853 } 4819 *
4820 * Because chaining consumes one sg entry, one extra
4821 * sg entry is allocated and the last sg entry is
4822 * copied to it if the length isn't zero after padded
4823 * amount is removed.
4824 *
4825 * If the last sg entry is completely replaced by
4826 * padding sg entry, the first sg entry is skipped
4827 * while chaining.
4828 */
4829 lsg->length -= qc->pad_len;
4830 if (lsg->length) {
4831 copy_lsg = &qc->extra_sg[0];
4832 tsg = &qc->extra_sg[0];
4833 } else {
4834 n_elem--;
4835 tsg = &qc->extra_sg[1];
4836 }
4854 4837
4855 if (trim_sg) { 4838 esg = &qc->extra_sg[1];
4856 qc->n_elem--;
4857 goto skip_map;
4858 }
4859 4839
4860 dma_address = dma_map_single(ap->dev, qc->buf_virt, 4840 (*n_elem_extra)++;
4861 sg->length, dir); 4841 (*nbytes_extra) += 4 - qc->pad_len;
4862 if (dma_mapping_error(dma_address)) {
4863 /* restore sg */
4864 sg->length += qc->pad_len;
4865 return -1;
4866 } 4842 }
4867 4843
4868 sg_dma_address(sg) = dma_address; 4844 if (copy_lsg)
4869 sg_dma_len(sg) = sg->length; 4845 sg_set_page(copy_lsg, sg_page(lsg), lsg->length, lsg->offset);
4870 4846
4871skip_map: 4847 sg_chain(lsg, 1, tsg);
4872 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg), 4848 sg_mark_end(esg);
4873 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4874 4849
4875 return 0; 4850 /* sglist can't start with chaining sg entry, fast forward */
4851 if (qc->sg == lsg) {
4852 qc->sg = tsg;
4853 qc->cursg = tsg;
4854 }
4855
4856 return n_elem;
4876} 4857}
4877 4858
4878/** 4859/**
@@ -4888,75 +4869,30 @@ skip_map:
4888 * Zero on success, negative on error. 4869 * Zero on success, negative on error.
4889 * 4870 *
4890 */ 4871 */
4891
4892static int ata_sg_setup(struct ata_queued_cmd *qc) 4872static int ata_sg_setup(struct ata_queued_cmd *qc)
4893{ 4873{
4894 struct ata_port *ap = qc->ap; 4874 struct ata_port *ap = qc->ap;
4895 struct scatterlist *sg = qc->__sg; 4875 unsigned int n_elem, n_elem_extra, nbytes_extra;
4896 struct scatterlist *lsg = sg_last(qc->__sg, qc->n_elem);
4897 int n_elem, pre_n_elem, dir, trim_sg = 0;
4898 4876
4899 VPRINTK("ENTER, ata%u\n", ap->print_id); 4877 VPRINTK("ENTER, ata%u\n", ap->print_id);
4900 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
4901 4878
4902 /* we must lengthen transfers to end on a 32-bit boundary */ 4879 n_elem = ata_sg_setup_extra(qc, &n_elem_extra, &nbytes_extra);
4903 qc->pad_len = lsg->length & 3;
4904 if (qc->pad_len) {
4905 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4906 struct scatterlist *psg = &qc->pad_sgent;
4907 unsigned int offset;
4908
4909 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
4910 4880
4911 memset(pad_buf, 0, ATA_DMA_PAD_SZ); 4881 if (n_elem) {
4912 4882 n_elem = dma_map_sg(ap->dev, qc->sg, n_elem, qc->dma_dir);
4913 /* 4883 if (n_elem < 1) {
4914 * psg->page/offset are used to copy to-be-written 4884 /* restore last sg */
4915 * data in this function or read data in ata_sg_clean. 4885 if (qc->last_sg)
4916 */ 4886 *qc->last_sg = qc->saved_last_sg;
4917 offset = lsg->offset + lsg->length - qc->pad_len; 4887 return -1;
4918 sg_init_table(psg, 1);
4919 sg_set_page(psg, nth_page(sg_page(lsg), offset >> PAGE_SHIFT),
4920 qc->pad_len, offset_in_page(offset));
4921
4922 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4923 void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
4924 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
4925 kunmap_atomic(addr, KM_IRQ0);
4926 } 4888 }
4927 4889 DPRINTK("%d sg elements mapped\n", n_elem);
4928 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4929 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4930 /* trim last sg */
4931 lsg->length -= qc->pad_len;
4932 if (lsg->length == 0)
4933 trim_sg = 1;
4934
4935 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
4936 qc->n_elem - 1, lsg->length, qc->pad_len);
4937 }
4938
4939 pre_n_elem = qc->n_elem;
4940 if (trim_sg && pre_n_elem)
4941 pre_n_elem--;
4942
4943 if (!pre_n_elem) {
4944 n_elem = 0;
4945 goto skip_map;
4946 }
4947
4948 dir = qc->dma_dir;
4949 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
4950 if (n_elem < 1) {
4951 /* restore last sg */
4952 lsg->length += qc->pad_len;
4953 return -1;
4954 } 4890 }
4955 4891
4956 DPRINTK("%d sg elements mapped\n", n_elem); 4892 qc->n_elem = qc->mapped_n_elem = n_elem;
4957 4893 qc->n_elem += n_elem_extra;
4958skip_map: 4894 qc->nbytes += nbytes_extra;
4959 qc->n_elem = n_elem; 4895 qc->flags |= ATA_QCFLAG_DMAMAP;
4960 4896
4961 return 0; 4897 return 0;
4962} 4898}
@@ -4985,7 +4921,7 @@ void swap_buf_le16(u16 *buf, unsigned int buf_words)
4985 4921
4986/** 4922/**
4987 * ata_data_xfer - Transfer data by PIO 4923 * ata_data_xfer - Transfer data by PIO
4988 * @adev: device to target 4924 * @dev: device to target
4989 * @buf: data buffer 4925 * @buf: data buffer
4990 * @buflen: buffer length 4926 * @buflen: buffer length
4991 * @write_data: read/write 4927 * @write_data: read/write
@@ -4994,37 +4930,44 @@ void swap_buf_le16(u16 *buf, unsigned int buf_words)
4994 * 4930 *
4995 * LOCKING: 4931 * LOCKING:
4996 * Inherited from caller. 4932 * Inherited from caller.
4933 *
4934 * RETURNS:
4935 * Bytes consumed.
4997 */ 4936 */
4998void ata_data_xfer(struct ata_device *adev, unsigned char *buf, 4937unsigned int ata_data_xfer(struct ata_device *dev, unsigned char *buf,
4999 unsigned int buflen, int write_data) 4938 unsigned int buflen, int rw)
5000{ 4939{
5001 struct ata_port *ap = adev->link->ap; 4940 struct ata_port *ap = dev->link->ap;
4941 void __iomem *data_addr = ap->ioaddr.data_addr;
5002 unsigned int words = buflen >> 1; 4942 unsigned int words = buflen >> 1;
5003 4943
5004 /* Transfer multiple of 2 bytes */ 4944 /* Transfer multiple of 2 bytes */
5005 if (write_data) 4945 if (rw == READ)
5006 iowrite16_rep(ap->ioaddr.data_addr, buf, words); 4946 ioread16_rep(data_addr, buf, words);
5007 else 4947 else
5008 ioread16_rep(ap->ioaddr.data_addr, buf, words); 4948 iowrite16_rep(data_addr, buf, words);
5009 4949
5010 /* Transfer trailing 1 byte, if any. */ 4950 /* Transfer trailing 1 byte, if any. */
5011 if (unlikely(buflen & 0x01)) { 4951 if (unlikely(buflen & 0x01)) {
5012 u16 align_buf[1] = { 0 }; 4952 __le16 align_buf[1] = { 0 };
5013 unsigned char *trailing_buf = buf + buflen - 1; 4953 unsigned char *trailing_buf = buf + buflen - 1;
5014 4954
5015 if (write_data) { 4955 if (rw == READ) {
5016 memcpy(align_buf, trailing_buf, 1); 4956 align_buf[0] = cpu_to_le16(ioread16(data_addr));
5017 iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
5018 } else {
5019 align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
5020 memcpy(trailing_buf, align_buf, 1); 4957 memcpy(trailing_buf, align_buf, 1);
4958 } else {
4959 memcpy(align_buf, trailing_buf, 1);
4960 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
5021 } 4961 }
4962 words++;
5022 } 4963 }
4964
4965 return words << 1;
5023} 4966}
5024 4967
5025/** 4968/**
5026 * ata_data_xfer_noirq - Transfer data by PIO 4969 * ata_data_xfer_noirq - Transfer data by PIO
5027 * @adev: device to target 4970 * @dev: device to target
5028 * @buf: data buffer 4971 * @buf: data buffer
5029 * @buflen: buffer length 4972 * @buflen: buffer length
5030 * @write_data: read/write 4973 * @write_data: read/write
@@ -5034,14 +4977,21 @@ void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
5034 * 4977 *
5035 * LOCKING: 4978 * LOCKING:
5036 * Inherited from caller. 4979 * Inherited from caller.
4980 *
4981 * RETURNS:
4982 * Bytes consumed.
5037 */ 4983 */
5038void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf, 4984unsigned int ata_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
5039 unsigned int buflen, int write_data) 4985 unsigned int buflen, int rw)
5040{ 4986{
5041 unsigned long flags; 4987 unsigned long flags;
4988 unsigned int consumed;
4989
5042 local_irq_save(flags); 4990 local_irq_save(flags);
5043 ata_data_xfer(adev, buf, buflen, write_data); 4991 consumed = ata_data_xfer(dev, buf, buflen, rw);
5044 local_irq_restore(flags); 4992 local_irq_restore(flags);
4993
4994 return consumed;
5045} 4995}
5046 4996
5047 4997
@@ -5152,13 +5102,13 @@ static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
5152 ata_altstatus(ap); /* flush */ 5102 ata_altstatus(ap); /* flush */
5153 5103
5154 switch (qc->tf.protocol) { 5104 switch (qc->tf.protocol) {
5155 case ATA_PROT_ATAPI: 5105 case ATAPI_PROT_PIO:
5156 ap->hsm_task_state = HSM_ST; 5106 ap->hsm_task_state = HSM_ST;
5157 break; 5107 break;
5158 case ATA_PROT_ATAPI_NODATA: 5108 case ATAPI_PROT_NODATA:
5159 ap->hsm_task_state = HSM_ST_LAST; 5109 ap->hsm_task_state = HSM_ST_LAST;
5160 break; 5110 break;
5161 case ATA_PROT_ATAPI_DMA: 5111 case ATAPI_PROT_DMA:
5162 ap->hsm_task_state = HSM_ST_LAST; 5112 ap->hsm_task_state = HSM_ST_LAST;
5163 /* initiate bmdma */ 5113 /* initiate bmdma */
5164 ap->ops->bmdma_start(qc); 5114 ap->ops->bmdma_start(qc);
@@ -5300,12 +5250,15 @@ static void atapi_pio_bytes(struct ata_queued_cmd *qc)
5300 bytes = (bc_hi << 8) | bc_lo; 5250 bytes = (bc_hi << 8) | bc_lo;
5301 5251
5302 /* shall be cleared to zero, indicating xfer of data */ 5252 /* shall be cleared to zero, indicating xfer of data */
5303 if (ireason & (1 << 0)) 5253 if (unlikely(ireason & (1 << 0)))
5304 goto err_out; 5254 goto err_out;
5305 5255
5306 /* make sure transfer direction matches expected */ 5256 /* make sure transfer direction matches expected */
5307 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0; 5257 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
5308 if (do_write != i_write) 5258 if (unlikely(do_write != i_write))
5259 goto err_out;
5260
5261 if (unlikely(!bytes))
5309 goto err_out; 5262 goto err_out;
5310 5263
5311 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes); 5264 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
@@ -5341,7 +5294,7 @@ static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *q
5341 (qc->tf.flags & ATA_TFLAG_WRITE)) 5294 (qc->tf.flags & ATA_TFLAG_WRITE))
5342 return 1; 5295 return 1;
5343 5296
5344 if (is_atapi_taskfile(&qc->tf) && 5297 if (ata_is_atapi(qc->tf.protocol) &&
5345 !(qc->dev->flags & ATA_DFLAG_CDB_INTR)) 5298 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
5346 return 1; 5299 return 1;
5347 } 5300 }
@@ -5506,7 +5459,7 @@ fsm_start:
5506 5459
5507 case HSM_ST: 5460 case HSM_ST:
5508 /* complete command or read/write the data register */ 5461 /* complete command or read/write the data register */
5509 if (qc->tf.protocol == ATA_PROT_ATAPI) { 5462 if (qc->tf.protocol == ATAPI_PROT_PIO) {
5510 /* ATAPI PIO protocol */ 5463 /* ATAPI PIO protocol */
5511 if ((status & ATA_DRQ) == 0) { 5464 if ((status & ATA_DRQ) == 0) {
5512 /* No more data to transfer or device error. 5465 /* No more data to transfer or device error.
@@ -5664,7 +5617,7 @@ fsm_start:
5664 msleep(2); 5617 msleep(2);
5665 status = ata_busy_wait(ap, ATA_BUSY, 10); 5618 status = ata_busy_wait(ap, ATA_BUSY, 10);
5666 if (status & ATA_BUSY) { 5619 if (status & ATA_BUSY) {
5667 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE); 5620 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
5668 return; 5621 return;
5669 } 5622 }
5670 } 5623 }
@@ -5805,6 +5758,22 @@ static void fill_result_tf(struct ata_queued_cmd *qc)
5805 ap->ops->tf_read(ap, &qc->result_tf); 5758 ap->ops->tf_read(ap, &qc->result_tf);
5806} 5759}
5807 5760
5761static void ata_verify_xfer(struct ata_queued_cmd *qc)
5762{
5763 struct ata_device *dev = qc->dev;
5764
5765 if (ata_tag_internal(qc->tag))
5766 return;
5767
5768 if (ata_is_nodata(qc->tf.protocol))
5769 return;
5770
5771 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
5772 return;
5773
5774 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
5775}
5776
5808/** 5777/**
5809 * ata_qc_complete - Complete an active ATA command 5778 * ata_qc_complete - Complete an active ATA command
5810 * @qc: Command to complete 5779 * @qc: Command to complete
@@ -5876,6 +5845,9 @@ void ata_qc_complete(struct ata_queued_cmd *qc)
5876 break; 5845 break;
5877 } 5846 }
5878 5847
5848 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5849 ata_verify_xfer(qc);
5850
5879 __ata_qc_complete(qc); 5851 __ata_qc_complete(qc);
5880 } else { 5852 } else {
5881 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED) 5853 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
@@ -5938,30 +5910,6 @@ int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
5938 return nr_done; 5910 return nr_done;
5939} 5911}
5940 5912
5941static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
5942{
5943 struct ata_port *ap = qc->ap;
5944
5945 switch (qc->tf.protocol) {
5946 case ATA_PROT_NCQ:
5947 case ATA_PROT_DMA:
5948 case ATA_PROT_ATAPI_DMA:
5949 return 1;
5950
5951 case ATA_PROT_ATAPI:
5952 case ATA_PROT_PIO:
5953 if (ap->flags & ATA_FLAG_PIO_DMA)
5954 return 1;
5955
5956 /* fall through */
5957
5958 default:
5959 return 0;
5960 }
5961
5962 /* never reached */
5963}
5964
5965/** 5913/**
5966 * ata_qc_issue - issue taskfile to device 5914 * ata_qc_issue - issue taskfile to device
5967 * @qc: command to issue to device 5915 * @qc: command to issue to device
@@ -5978,6 +5926,7 @@ void ata_qc_issue(struct ata_queued_cmd *qc)
5978{ 5926{
5979 struct ata_port *ap = qc->ap; 5927 struct ata_port *ap = qc->ap;
5980 struct ata_link *link = qc->dev->link; 5928 struct ata_link *link = qc->dev->link;
5929 u8 prot = qc->tf.protocol;
5981 5930
5982 /* Make sure only one non-NCQ command is outstanding. The 5931 /* Make sure only one non-NCQ command is outstanding. The
5983 * check is skipped for old EH because it reuses active qc to 5932 * check is skipped for old EH because it reuses active qc to
@@ -5985,7 +5934,7 @@ void ata_qc_issue(struct ata_queued_cmd *qc)
5985 */ 5934 */
5986 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag)); 5935 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5987 5936
5988 if (qc->tf.protocol == ATA_PROT_NCQ) { 5937 if (ata_is_ncq(prot)) {
5989 WARN_ON(link->sactive & (1 << qc->tag)); 5938 WARN_ON(link->sactive & (1 << qc->tag));
5990 5939
5991 if (!link->sactive) 5940 if (!link->sactive)
@@ -6001,17 +5950,18 @@ void ata_qc_issue(struct ata_queued_cmd *qc)
6001 qc->flags |= ATA_QCFLAG_ACTIVE; 5950 qc->flags |= ATA_QCFLAG_ACTIVE;
6002 ap->qc_active |= 1 << qc->tag; 5951 ap->qc_active |= 1 << qc->tag;
6003 5952
6004 if (ata_should_dma_map(qc)) { 5953 /* We guarantee to LLDs that they will have at least one
6005 if (qc->flags & ATA_QCFLAG_SG) { 5954 * non-zero sg if the command is a data command.
6006 if (ata_sg_setup(qc)) 5955 */
6007 goto sg_err; 5956 BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
6008 } else if (qc->flags & ATA_QCFLAG_SINGLE) { 5957
6009 if (ata_sg_setup_one(qc)) 5958 /* ata_sg_setup() may update nbytes */
6010 goto sg_err; 5959 qc->raw_nbytes = qc->nbytes;
6011 } 5960
6012 } else { 5961 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
6013 qc->flags &= ~ATA_QCFLAG_DMAMAP; 5962 (ap->flags & ATA_FLAG_PIO_DMA)))
6014 } 5963 if (ata_sg_setup(qc))
5964 goto sg_err;
6015 5965
6016 /* if device is sleeping, schedule softreset and abort the link */ 5966 /* if device is sleeping, schedule softreset and abort the link */
6017 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) { 5967 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
@@ -6029,7 +5979,6 @@ void ata_qc_issue(struct ata_queued_cmd *qc)
6029 return; 5979 return;
6030 5980
6031sg_err: 5981sg_err:
6032 qc->flags &= ~ATA_QCFLAG_DMAMAP;
6033 qc->err_mask |= AC_ERR_SYSTEM; 5982 qc->err_mask |= AC_ERR_SYSTEM;
6034err: 5983err:
6035 ata_qc_complete(qc); 5984 ata_qc_complete(qc);
@@ -6064,11 +6013,11 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
6064 switch (qc->tf.protocol) { 6013 switch (qc->tf.protocol) {
6065 case ATA_PROT_PIO: 6014 case ATA_PROT_PIO:
6066 case ATA_PROT_NODATA: 6015 case ATA_PROT_NODATA:
6067 case ATA_PROT_ATAPI: 6016 case ATAPI_PROT_PIO:
6068 case ATA_PROT_ATAPI_NODATA: 6017 case ATAPI_PROT_NODATA:
6069 qc->tf.flags |= ATA_TFLAG_POLLING; 6018 qc->tf.flags |= ATA_TFLAG_POLLING;
6070 break; 6019 break;
6071 case ATA_PROT_ATAPI_DMA: 6020 case ATAPI_PROT_DMA:
6072 if (qc->dev->flags & ATA_DFLAG_CDB_INTR) 6021 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
6073 /* see ata_dma_blacklisted() */ 6022 /* see ata_dma_blacklisted() */
6074 BUG(); 6023 BUG();
@@ -6091,7 +6040,7 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
6091 ap->hsm_task_state = HSM_ST_LAST; 6040 ap->hsm_task_state = HSM_ST_LAST;
6092 6041
6093 if (qc->tf.flags & ATA_TFLAG_POLLING) 6042 if (qc->tf.flags & ATA_TFLAG_POLLING)
6094 ata_port_queue_task(ap, ata_pio_task, qc, 0); 6043 ata_pio_queue_task(ap, qc, 0);
6095 6044
6096 break; 6045 break;
6097 6046
@@ -6113,7 +6062,7 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
6113 if (qc->tf.flags & ATA_TFLAG_WRITE) { 6062 if (qc->tf.flags & ATA_TFLAG_WRITE) {
6114 /* PIO data out protocol */ 6063 /* PIO data out protocol */
6115 ap->hsm_task_state = HSM_ST_FIRST; 6064 ap->hsm_task_state = HSM_ST_FIRST;
6116 ata_port_queue_task(ap, ata_pio_task, qc, 0); 6065 ata_pio_queue_task(ap, qc, 0);
6117 6066
6118 /* always send first data block using 6067 /* always send first data block using
6119 * the ata_pio_task() codepath. 6068 * the ata_pio_task() codepath.
@@ -6123,7 +6072,7 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
6123 ap->hsm_task_state = HSM_ST; 6072 ap->hsm_task_state = HSM_ST;
6124 6073
6125 if (qc->tf.flags & ATA_TFLAG_POLLING) 6074 if (qc->tf.flags & ATA_TFLAG_POLLING)
6126 ata_port_queue_task(ap, ata_pio_task, qc, 0); 6075 ata_pio_queue_task(ap, qc, 0);
6127 6076
6128 /* if polling, ata_pio_task() handles the rest. 6077 /* if polling, ata_pio_task() handles the rest.
6129 * otherwise, interrupt handler takes over from here. 6078 * otherwise, interrupt handler takes over from here.
@@ -6132,8 +6081,8 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
6132 6081
6133 break; 6082 break;
6134 6083
6135 case ATA_PROT_ATAPI: 6084 case ATAPI_PROT_PIO:
6136 case ATA_PROT_ATAPI_NODATA: 6085 case ATAPI_PROT_NODATA:
6137 if (qc->tf.flags & ATA_TFLAG_POLLING) 6086 if (qc->tf.flags & ATA_TFLAG_POLLING)
6138 ata_qc_set_polling(qc); 6087 ata_qc_set_polling(qc);
6139 6088
@@ -6144,10 +6093,10 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
6144 /* send cdb by polling if no cdb interrupt */ 6093 /* send cdb by polling if no cdb interrupt */
6145 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) || 6094 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
6146 (qc->tf.flags & ATA_TFLAG_POLLING)) 6095 (qc->tf.flags & ATA_TFLAG_POLLING))
6147 ata_port_queue_task(ap, ata_pio_task, qc, 0); 6096 ata_pio_queue_task(ap, qc, 0);
6148 break; 6097 break;
6149 6098
6150 case ATA_PROT_ATAPI_DMA: 6099 case ATAPI_PROT_DMA:
6151 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING); 6100 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
6152 6101
6153 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */ 6102 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
@@ -6156,7 +6105,7 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
6156 6105
6157 /* send cdb by polling if no cdb interrupt */ 6106 /* send cdb by polling if no cdb interrupt */
6158 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) 6107 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
6159 ata_port_queue_task(ap, ata_pio_task, qc, 0); 6108 ata_pio_queue_task(ap, qc, 0);
6160 break; 6109 break;
6161 6110
6162 default: 6111 default:
@@ -6200,15 +6149,15 @@ inline unsigned int ata_host_intr(struct ata_port *ap,
6200 */ 6149 */
6201 6150
6202 /* Check the ATA_DFLAG_CDB_INTR flag is enough here. 6151 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
6203 * The flag was turned on only for atapi devices. 6152 * The flag was turned on only for atapi devices. No
6204 * No need to check is_atapi_taskfile(&qc->tf) again. 6153 * need to check ata_is_atapi(qc->tf.protocol) again.
6205 */ 6154 */
6206 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) 6155 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
6207 goto idle_irq; 6156 goto idle_irq;
6208 break; 6157 break;
6209 case HSM_ST_LAST: 6158 case HSM_ST_LAST:
6210 if (qc->tf.protocol == ATA_PROT_DMA || 6159 if (qc->tf.protocol == ATA_PROT_DMA ||
6211 qc->tf.protocol == ATA_PROT_ATAPI_DMA) { 6160 qc->tf.protocol == ATAPI_PROT_DMA) {
6212 /* check status of DMA engine */ 6161 /* check status of DMA engine */
6213 host_stat = ap->ops->bmdma_status(ap); 6162 host_stat = ap->ops->bmdma_status(ap);
6214 VPRINTK("ata%u: host_stat 0x%X\n", 6163 VPRINTK("ata%u: host_stat 0x%X\n",
@@ -6250,7 +6199,7 @@ inline unsigned int ata_host_intr(struct ata_port *ap,
6250 ata_hsm_move(ap, qc, status, 0); 6199 ata_hsm_move(ap, qc, status, 0);
6251 6200
6252 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA || 6201 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
6253 qc->tf.protocol == ATA_PROT_ATAPI_DMA)) 6202 qc->tf.protocol == ATAPI_PROT_DMA))
6254 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat); 6203 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
6255 6204
6256 return 1; /* irq handled */ 6205 return 1; /* irq handled */
@@ -6772,7 +6721,7 @@ struct ata_port *ata_port_alloc(struct ata_host *host)
6772 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN; 6721 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
6773#endif 6722#endif
6774 6723
6775 INIT_DELAYED_WORK(&ap->port_task, NULL); 6724 INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
6776 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug); 6725 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6777 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan); 6726 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
6778 INIT_LIST_HEAD(&ap->eh_done_q); 6727 INIT_LIST_HEAD(&ap->eh_done_q);
@@ -7589,7 +7538,6 @@ EXPORT_SYMBOL_GPL(ata_host_register);
7589EXPORT_SYMBOL_GPL(ata_host_activate); 7538EXPORT_SYMBOL_GPL(ata_host_activate);
7590EXPORT_SYMBOL_GPL(ata_host_detach); 7539EXPORT_SYMBOL_GPL(ata_host_detach);
7591EXPORT_SYMBOL_GPL(ata_sg_init); 7540EXPORT_SYMBOL_GPL(ata_sg_init);
7592EXPORT_SYMBOL_GPL(ata_sg_init_one);
7593EXPORT_SYMBOL_GPL(ata_hsm_move); 7541EXPORT_SYMBOL_GPL(ata_hsm_move);
7594EXPORT_SYMBOL_GPL(ata_qc_complete); 7542EXPORT_SYMBOL_GPL(ata_qc_complete);
7595EXPORT_SYMBOL_GPL(ata_qc_complete_multiple); 7543EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
@@ -7601,6 +7549,13 @@ EXPORT_SYMBOL_GPL(ata_std_dev_select);
7601EXPORT_SYMBOL_GPL(sata_print_link_status); 7549EXPORT_SYMBOL_GPL(sata_print_link_status);
7602EXPORT_SYMBOL_GPL(ata_tf_to_fis); 7550EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7603EXPORT_SYMBOL_GPL(ata_tf_from_fis); 7551EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7552EXPORT_SYMBOL_GPL(ata_pack_xfermask);
7553EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
7554EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
7555EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
7556EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
7557EXPORT_SYMBOL_GPL(ata_mode_string);
7558EXPORT_SYMBOL_GPL(ata_id_xfermask);
7604EXPORT_SYMBOL_GPL(ata_check_status); 7559EXPORT_SYMBOL_GPL(ata_check_status);
7605EXPORT_SYMBOL_GPL(ata_altstatus); 7560EXPORT_SYMBOL_GPL(ata_altstatus);
7606EXPORT_SYMBOL_GPL(ata_exec_command); 7561EXPORT_SYMBOL_GPL(ata_exec_command);
@@ -7643,7 +7598,6 @@ EXPORT_SYMBOL_GPL(ata_wait_register);
7643EXPORT_SYMBOL_GPL(ata_busy_sleep); 7598EXPORT_SYMBOL_GPL(ata_busy_sleep);
7644EXPORT_SYMBOL_GPL(ata_wait_after_reset); 7599EXPORT_SYMBOL_GPL(ata_wait_after_reset);
7645EXPORT_SYMBOL_GPL(ata_wait_ready); 7600EXPORT_SYMBOL_GPL(ata_wait_ready);
7646EXPORT_SYMBOL_GPL(ata_port_queue_task);
7647EXPORT_SYMBOL_GPL(ata_scsi_ioctl); 7601EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
7648EXPORT_SYMBOL_GPL(ata_scsi_queuecmd); 7602EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
7649EXPORT_SYMBOL_GPL(ata_scsi_slave_config); 7603EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
@@ -7662,18 +7616,20 @@ EXPORT_SYMBOL_GPL(ata_host_resume);
7662#endif /* CONFIG_PM */ 7616#endif /* CONFIG_PM */
7663EXPORT_SYMBOL_GPL(ata_id_string); 7617EXPORT_SYMBOL_GPL(ata_id_string);
7664EXPORT_SYMBOL_GPL(ata_id_c_string); 7618EXPORT_SYMBOL_GPL(ata_id_c_string);
7665EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
7666EXPORT_SYMBOL_GPL(ata_scsi_simulate); 7619EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7667 7620
7668EXPORT_SYMBOL_GPL(ata_pio_need_iordy); 7621EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
7622EXPORT_SYMBOL_GPL(ata_timing_find_mode);
7669EXPORT_SYMBOL_GPL(ata_timing_compute); 7623EXPORT_SYMBOL_GPL(ata_timing_compute);
7670EXPORT_SYMBOL_GPL(ata_timing_merge); 7624EXPORT_SYMBOL_GPL(ata_timing_merge);
7625EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
7671 7626
7672#ifdef CONFIG_PCI 7627#ifdef CONFIG_PCI
7673EXPORT_SYMBOL_GPL(pci_test_config_bits); 7628EXPORT_SYMBOL_GPL(pci_test_config_bits);
7674EXPORT_SYMBOL_GPL(ata_pci_init_sff_host); 7629EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
7675EXPORT_SYMBOL_GPL(ata_pci_init_bmdma); 7630EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
7676EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host); 7631EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
7632EXPORT_SYMBOL_GPL(ata_pci_activate_sff_host);
7677EXPORT_SYMBOL_GPL(ata_pci_init_one); 7633EXPORT_SYMBOL_GPL(ata_pci_init_one);
7678EXPORT_SYMBOL_GPL(ata_pci_remove_one); 7634EXPORT_SYMBOL_GPL(ata_pci_remove_one);
7679#ifdef CONFIG_PM 7635#ifdef CONFIG_PM
@@ -7715,4 +7671,5 @@ EXPORT_SYMBOL_GPL(ata_dev_try_classify);
7715EXPORT_SYMBOL_GPL(ata_cable_40wire); 7671EXPORT_SYMBOL_GPL(ata_cable_40wire);
7716EXPORT_SYMBOL_GPL(ata_cable_80wire); 7672EXPORT_SYMBOL_GPL(ata_cable_80wire);
7717EXPORT_SYMBOL_GPL(ata_cable_unknown); 7673EXPORT_SYMBOL_GPL(ata_cable_unknown);
7674EXPORT_SYMBOL_GPL(ata_cable_ignore);
7718EXPORT_SYMBOL_GPL(ata_cable_sata); 7675EXPORT_SYMBOL_GPL(ata_cable_sata);
diff --git a/drivers/ata/libata-eh.c b/drivers/ata/libata-eh.c
index 21a81cd148e4..4e31071acc02 100644
--- a/drivers/ata/libata-eh.c
+++ b/drivers/ata/libata-eh.c
@@ -46,9 +46,26 @@
46#include "libata.h" 46#include "libata.h"
47 47
48enum { 48enum {
49 /* speed down verdicts */
49 ATA_EH_SPDN_NCQ_OFF = (1 << 0), 50 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
50 ATA_EH_SPDN_SPEED_DOWN = (1 << 1), 51 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
51 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2), 52 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
53 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
54
55 /* error flags */
56 ATA_EFLAG_IS_IO = (1 << 0),
57 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
58
59 /* error categories */
60 ATA_ECAT_NONE = 0,
61 ATA_ECAT_ATA_BUS = 1,
62 ATA_ECAT_TOUT_HSM = 2,
63 ATA_ECAT_UNK_DEV = 3,
64 ATA_ECAT_DUBIOUS_NONE = 4,
65 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
66 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
67 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
68 ATA_ECAT_NR = 8,
52}; 69};
53 70
54/* Waiting in ->prereset can never be reliable. It's sometimes nice 71/* Waiting in ->prereset can never be reliable. It's sometimes nice
@@ -213,12 +230,13 @@ void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
213 if (offset < 0) 230 if (offset < 0)
214 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start); 231 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
215 else 232 else
216 ata_port_desc(ap, "%s 0x%llx", name, start + offset); 233 ata_port_desc(ap, "%s 0x%llx", name,
234 start + (unsigned long long)offset);
217} 235}
218 236
219#endif /* CONFIG_PCI */ 237#endif /* CONFIG_PCI */
220 238
221static void ata_ering_record(struct ata_ering *ering, int is_io, 239static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
222 unsigned int err_mask) 240 unsigned int err_mask)
223{ 241{
224 struct ata_ering_entry *ent; 242 struct ata_ering_entry *ent;
@@ -229,11 +247,20 @@ static void ata_ering_record(struct ata_ering *ering, int is_io,
229 ering->cursor %= ATA_ERING_SIZE; 247 ering->cursor %= ATA_ERING_SIZE;
230 248
231 ent = &ering->ring[ering->cursor]; 249 ent = &ering->ring[ering->cursor];
232 ent->is_io = is_io; 250 ent->eflags = eflags;
233 ent->err_mask = err_mask; 251 ent->err_mask = err_mask;
234 ent->timestamp = get_jiffies_64(); 252 ent->timestamp = get_jiffies_64();
235} 253}
236 254
255static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
256{
257 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
258
259 if (ent->err_mask)
260 return ent;
261 return NULL;
262}
263
237static void ata_ering_clear(struct ata_ering *ering) 264static void ata_ering_clear(struct ata_ering *ering)
238{ 265{
239 memset(ering, 0, sizeof(*ering)); 266 memset(ering, 0, sizeof(*ering));
@@ -445,9 +472,20 @@ void ata_scsi_error(struct Scsi_Host *host)
445 spin_lock_irqsave(ap->lock, flags); 472 spin_lock_irqsave(ap->lock, flags);
446 473
447 __ata_port_for_each_link(link, ap) { 474 __ata_port_for_each_link(link, ap) {
475 struct ata_eh_context *ehc = &link->eh_context;
476 struct ata_device *dev;
477
448 memset(&link->eh_context, 0, sizeof(link->eh_context)); 478 memset(&link->eh_context, 0, sizeof(link->eh_context));
449 link->eh_context.i = link->eh_info; 479 link->eh_context.i = link->eh_info;
450 memset(&link->eh_info, 0, sizeof(link->eh_info)); 480 memset(&link->eh_info, 0, sizeof(link->eh_info));
481
482 ata_link_for_each_dev(dev, link) {
483 int devno = dev->devno;
484
485 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
486 if (ata_ncq_enabled(dev))
487 ehc->saved_ncq_enabled |= 1 << devno;
488 }
451 } 489 }
452 490
453 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS; 491 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
@@ -1260,10 +1298,10 @@ static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1260 1298
1261 /* is it pointless to prefer PIO for "safety reasons"? */ 1299 /* is it pointless to prefer PIO for "safety reasons"? */
1262 if (ap->flags & ATA_FLAG_PIO_DMA) { 1300 if (ap->flags & ATA_FLAG_PIO_DMA) {
1263 tf.protocol = ATA_PROT_ATAPI_DMA; 1301 tf.protocol = ATAPI_PROT_DMA;
1264 tf.feature |= ATAPI_PKT_DMA; 1302 tf.feature |= ATAPI_PKT_DMA;
1265 } else { 1303 } else {
1266 tf.protocol = ATA_PROT_ATAPI; 1304 tf.protocol = ATAPI_PROT_PIO;
1267 tf.lbam = SCSI_SENSE_BUFFERSIZE; 1305 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1268 tf.lbah = 0; 1306 tf.lbah = 0;
1269 } 1307 }
@@ -1451,20 +1489,29 @@ static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1451 return action; 1489 return action;
1452} 1490}
1453 1491
1454static int ata_eh_categorize_error(int is_io, unsigned int err_mask) 1492static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1493 int *xfer_ok)
1455{ 1494{
1495 int base = 0;
1496
1497 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1498 *xfer_ok = 1;
1499
1500 if (!*xfer_ok)
1501 base = ATA_ECAT_DUBIOUS_NONE;
1502
1456 if (err_mask & AC_ERR_ATA_BUS) 1503 if (err_mask & AC_ERR_ATA_BUS)
1457 return 1; 1504 return base + ATA_ECAT_ATA_BUS;
1458 1505
1459 if (err_mask & AC_ERR_TIMEOUT) 1506 if (err_mask & AC_ERR_TIMEOUT)
1460 return 2; 1507 return base + ATA_ECAT_TOUT_HSM;
1461 1508
1462 if (is_io) { 1509 if (eflags & ATA_EFLAG_IS_IO) {
1463 if (err_mask & AC_ERR_HSM) 1510 if (err_mask & AC_ERR_HSM)
1464 return 2; 1511 return base + ATA_ECAT_TOUT_HSM;
1465 if ((err_mask & 1512 if ((err_mask &
1466 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV) 1513 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1467 return 3; 1514 return base + ATA_ECAT_UNK_DEV;
1468 } 1515 }
1469 1516
1470 return 0; 1517 return 0;
@@ -1472,18 +1519,22 @@ static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1472 1519
1473struct speed_down_verdict_arg { 1520struct speed_down_verdict_arg {
1474 u64 since; 1521 u64 since;
1475 int nr_errors[4]; 1522 int xfer_ok;
1523 int nr_errors[ATA_ECAT_NR];
1476}; 1524};
1477 1525
1478static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg) 1526static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1479{ 1527{
1480 struct speed_down_verdict_arg *arg = void_arg; 1528 struct speed_down_verdict_arg *arg = void_arg;
1481 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask); 1529 int cat;
1482 1530
1483 if (ent->timestamp < arg->since) 1531 if (ent->timestamp < arg->since)
1484 return -1; 1532 return -1;
1485 1533
1534 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1535 &arg->xfer_ok);
1486 arg->nr_errors[cat]++; 1536 arg->nr_errors[cat]++;
1537
1487 return 0; 1538 return 0;
1488} 1539}
1489 1540
@@ -1495,22 +1546,48 @@ static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1495 * whether NCQ needs to be turned off, transfer speed should be 1546 * whether NCQ needs to be turned off, transfer speed should be
1496 * stepped down, or falling back to PIO is necessary. 1547 * stepped down, or falling back to PIO is necessary.
1497 * 1548 *
1498 * Cat-1 is ATA_BUS error for any command. 1549 * ECAT_ATA_BUS : ATA_BUS error for any command
1550 *
1551 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1552 * IO commands
1553 *
1554 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1555 *
1556 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1557 * data transfer hasn't been verified.
1558 *
1559 * Verdicts are
1560 *
1561 * NCQ_OFF : Turn off NCQ.
1562 *
1563 * SPEED_DOWN : Speed down transfer speed but don't fall back
1564 * to PIO.
1565 *
1566 * FALLBACK_TO_PIO : Fall back to PIO.
1567 *
1568 * Even if multiple verdicts are returned, only one action is
1569 * taken per error. An action triggered by non-DUBIOUS errors
1570 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1571 * This is to expedite speed down decisions right after device is
1572 * initially configured.
1499 * 1573 *
1500 * Cat-2 is TIMEOUT for any command or HSM violation for known 1574 * The followings are speed down rules. #1 and #2 deal with
1501 * supported commands. 1575 * DUBIOUS errors.
1502 * 1576 *
1503 * Cat-3 is is unclassified DEV error for known supported 1577 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1504 * command. 1578 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1505 * 1579 *
1506 * NCQ needs to be turned off if there have been more than 3 1580 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1507 * Cat-2 + Cat-3 errors during last 10 minutes. 1581 * occurred during last 5 mins, NCQ_OFF.
1508 * 1582 *
1509 * Speed down is necessary if there have been more than 3 Cat-1 + 1583 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1510 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes. 1584 * ocurred during last 5 mins, FALLBACK_TO_PIO
1511 * 1585 *
1512 * Falling back to PIO mode is necessary if there have been more 1586 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1513 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes. 1587 * during last 10 mins, NCQ_OFF.
1588 *
1589 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1590 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1514 * 1591 *
1515 * LOCKING: 1592 * LOCKING:
1516 * Inherited from caller. 1593 * Inherited from caller.
@@ -1525,23 +1602,38 @@ static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1525 struct speed_down_verdict_arg arg; 1602 struct speed_down_verdict_arg arg;
1526 unsigned int verdict = 0; 1603 unsigned int verdict = 0;
1527 1604
1528 /* scan past 10 mins of error history */ 1605 /* scan past 5 mins of error history */
1529 memset(&arg, 0, sizeof(arg)); 1606 memset(&arg, 0, sizeof(arg));
1530 arg.since = j64 - min(j64, j10mins); 1607 arg.since = j64 - min(j64, j5mins);
1531 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); 1608 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1532 1609
1533 if (arg.nr_errors[2] + arg.nr_errors[3] > 3) 1610 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1534 verdict |= ATA_EH_SPDN_NCQ_OFF; 1611 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1535 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10) 1612 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1536 verdict |= ATA_EH_SPDN_SPEED_DOWN; 1613 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1537 1614
1538 /* scan past 3 mins of error history */ 1615 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1616 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1617 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1618
1619 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1620 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1621 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1622 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1623
1624 /* scan past 10 mins of error history */
1539 memset(&arg, 0, sizeof(arg)); 1625 memset(&arg, 0, sizeof(arg));
1540 arg.since = j64 - min(j64, j5mins); 1626 arg.since = j64 - min(j64, j10mins);
1541 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); 1627 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1542 1628
1543 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10) 1629 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1544 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO; 1630 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1631 verdict |= ATA_EH_SPDN_NCQ_OFF;
1632
1633 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1634 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1635 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1636 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1545 1637
1546 return verdict; 1638 return verdict;
1547} 1639}
@@ -1549,7 +1641,7 @@ static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1549/** 1641/**
1550 * ata_eh_speed_down - record error and speed down if necessary 1642 * ata_eh_speed_down - record error and speed down if necessary
1551 * @dev: Failed device 1643 * @dev: Failed device
1552 * @is_io: Did the device fail during normal IO? 1644 * @eflags: mask of ATA_EFLAG_* flags
1553 * @err_mask: err_mask of the error 1645 * @err_mask: err_mask of the error
1554 * 1646 *
1555 * Record error and examine error history to determine whether 1647 * Record error and examine error history to determine whether
@@ -1563,18 +1655,20 @@ static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1563 * RETURNS: 1655 * RETURNS:
1564 * Determined recovery action. 1656 * Determined recovery action.
1565 */ 1657 */
1566static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io, 1658static unsigned int ata_eh_speed_down(struct ata_device *dev,
1567 unsigned int err_mask) 1659 unsigned int eflags, unsigned int err_mask)
1568{ 1660{
1661 struct ata_link *link = dev->link;
1662 int xfer_ok = 0;
1569 unsigned int verdict; 1663 unsigned int verdict;
1570 unsigned int action = 0; 1664 unsigned int action = 0;
1571 1665
1572 /* don't bother if Cat-0 error */ 1666 /* don't bother if Cat-0 error */
1573 if (ata_eh_categorize_error(is_io, err_mask) == 0) 1667 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1574 return 0; 1668 return 0;
1575 1669
1576 /* record error and determine whether speed down is necessary */ 1670 /* record error and determine whether speed down is necessary */
1577 ata_ering_record(&dev->ering, is_io, err_mask); 1671 ata_ering_record(&dev->ering, eflags, err_mask);
1578 verdict = ata_eh_speed_down_verdict(dev); 1672 verdict = ata_eh_speed_down_verdict(dev);
1579 1673
1580 /* turn off NCQ? */ 1674 /* turn off NCQ? */
@@ -1590,7 +1684,7 @@ static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1590 /* speed down? */ 1684 /* speed down? */
1591 if (verdict & ATA_EH_SPDN_SPEED_DOWN) { 1685 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1592 /* speed down SATA link speed if possible */ 1686 /* speed down SATA link speed if possible */
1593 if (sata_down_spd_limit(dev->link) == 0) { 1687 if (sata_down_spd_limit(link) == 0) {
1594 action |= ATA_EH_HARDRESET; 1688 action |= ATA_EH_HARDRESET;
1595 goto done; 1689 goto done;
1596 } 1690 }
@@ -1618,10 +1712,10 @@ static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1618 } 1712 }
1619 1713
1620 /* Fall back to PIO? Slowing down to PIO is meaningless for 1714 /* Fall back to PIO? Slowing down to PIO is meaningless for
1621 * SATA. Consider it only for PATA. 1715 * SATA ATA devices. Consider it only for PATA and SATAPI.
1622 */ 1716 */
1623 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) && 1717 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1624 (dev->link->ap->cbl != ATA_CBL_SATA) && 1718 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
1625 (dev->xfer_shift != ATA_SHIFT_PIO)) { 1719 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1626 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) { 1720 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1627 dev->spdn_cnt = 0; 1721 dev->spdn_cnt = 0;
@@ -1633,7 +1727,8 @@ static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1633 return 0; 1727 return 0;
1634 done: 1728 done:
1635 /* device has been slowed down, blow error history */ 1729 /* device has been slowed down, blow error history */
1636 ata_ering_clear(&dev->ering); 1730 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1731 ata_ering_clear(&dev->ering);
1637 return action; 1732 return action;
1638} 1733}
1639 1734
@@ -1653,8 +1748,8 @@ static void ata_eh_link_autopsy(struct ata_link *link)
1653 struct ata_port *ap = link->ap; 1748 struct ata_port *ap = link->ap;
1654 struct ata_eh_context *ehc = &link->eh_context; 1749 struct ata_eh_context *ehc = &link->eh_context;
1655 struct ata_device *dev; 1750 struct ata_device *dev;
1656 unsigned int all_err_mask = 0; 1751 unsigned int all_err_mask = 0, eflags = 0;
1657 int tag, is_io = 0; 1752 int tag;
1658 u32 serror; 1753 u32 serror;
1659 int rc; 1754 int rc;
1660 1755
@@ -1713,15 +1808,15 @@ static void ata_eh_link_autopsy(struct ata_link *link)
1713 ehc->i.dev = qc->dev; 1808 ehc->i.dev = qc->dev;
1714 all_err_mask |= qc->err_mask; 1809 all_err_mask |= qc->err_mask;
1715 if (qc->flags & ATA_QCFLAG_IO) 1810 if (qc->flags & ATA_QCFLAG_IO)
1716 is_io = 1; 1811 eflags |= ATA_EFLAG_IS_IO;
1717 } 1812 }
1718 1813
1719 /* enforce default EH actions */ 1814 /* enforce default EH actions */
1720 if (ap->pflags & ATA_PFLAG_FROZEN || 1815 if (ap->pflags & ATA_PFLAG_FROZEN ||
1721 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT)) 1816 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1722 ehc->i.action |= ATA_EH_SOFTRESET; 1817 ehc->i.action |= ATA_EH_SOFTRESET;
1723 else if ((is_io && all_err_mask) || 1818 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
1724 (!is_io && (all_err_mask & ~AC_ERR_DEV))) 1819 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
1725 ehc->i.action |= ATA_EH_REVALIDATE; 1820 ehc->i.action |= ATA_EH_REVALIDATE;
1726 1821
1727 /* If we have offending qcs and the associated failed device, 1822 /* If we have offending qcs and the associated failed device,
@@ -1743,8 +1838,11 @@ static void ata_eh_link_autopsy(struct ata_link *link)
1743 ata_dev_enabled(link->device)))) 1838 ata_dev_enabled(link->device))))
1744 dev = link->device; 1839 dev = link->device;
1745 1840
1746 if (dev) 1841 if (dev) {
1747 ehc->i.action |= ata_eh_speed_down(dev, is_io, all_err_mask); 1842 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
1843 eflags |= ATA_EFLAG_DUBIOUS_XFER;
1844 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
1845 }
1748 1846
1749 DPRINTK("EXIT\n"); 1847 DPRINTK("EXIT\n");
1750} 1848}
@@ -1880,8 +1978,8 @@ static void ata_eh_link_report(struct ata_link *link)
1880 [ATA_PROT_PIO] = "pio", 1978 [ATA_PROT_PIO] = "pio",
1881 [ATA_PROT_DMA] = "dma", 1979 [ATA_PROT_DMA] = "dma",
1882 [ATA_PROT_NCQ] = "ncq", 1980 [ATA_PROT_NCQ] = "ncq",
1883 [ATA_PROT_ATAPI] = "pio", 1981 [ATAPI_PROT_PIO] = "pio",
1884 [ATA_PROT_ATAPI_DMA] = "dma", 1982 [ATAPI_PROT_DMA] = "dma",
1885 }; 1983 };
1886 1984
1887 snprintf(data_buf, sizeof(data_buf), " %s %u %s", 1985 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
@@ -1889,7 +1987,7 @@ static void ata_eh_link_report(struct ata_link *link)
1889 dma_str[qc->dma_dir]); 1987 dma_str[qc->dma_dir]);
1890 } 1988 }
1891 1989
1892 if (is_atapi_taskfile(&qc->tf)) 1990 if (ata_is_atapi(qc->tf.protocol))
1893 snprintf(cdb_buf, sizeof(cdb_buf), 1991 snprintf(cdb_buf, sizeof(cdb_buf),
1894 "cdb %02x %02x %02x %02x %02x %02x %02x %02x " 1992 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
1895 "%02x %02x %02x %02x %02x %02x %02x %02x\n ", 1993 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
@@ -2329,6 +2427,58 @@ static int ata_eh_revalidate_and_attach(struct ata_link *link,
2329 return rc; 2427 return rc;
2330} 2428}
2331 2429
2430/**
2431 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2432 * @link: link on which timings will be programmed
2433 * @r_failed_dev: out paramter for failed device
2434 *
2435 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2436 * ata_set_mode() fails, pointer to the failing device is
2437 * returned in @r_failed_dev.
2438 *
2439 * LOCKING:
2440 * PCI/etc. bus probe sem.
2441 *
2442 * RETURNS:
2443 * 0 on success, negative errno otherwise
2444 */
2445int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2446{
2447 struct ata_port *ap = link->ap;
2448 struct ata_device *dev;
2449 int rc;
2450
2451 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2452 ata_link_for_each_dev(dev, link) {
2453 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
2454 struct ata_ering_entry *ent;
2455
2456 ent = ata_ering_top(&dev->ering);
2457 if (ent)
2458 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
2459 }
2460 }
2461
2462 /* has private set_mode? */
2463 if (ap->ops->set_mode)
2464 rc = ap->ops->set_mode(link, r_failed_dev);
2465 else
2466 rc = ata_do_set_mode(link, r_failed_dev);
2467
2468 /* if transfer mode has changed, set DUBIOUS_XFER on device */
2469 ata_link_for_each_dev(dev, link) {
2470 struct ata_eh_context *ehc = &link->eh_context;
2471 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
2472 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
2473
2474 if (dev->xfer_mode != saved_xfer_mode ||
2475 ata_ncq_enabled(dev) != saved_ncq)
2476 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
2477 }
2478
2479 return rc;
2480}
2481
2332static int ata_link_nr_enabled(struct ata_link *link) 2482static int ata_link_nr_enabled(struct ata_link *link)
2333{ 2483{
2334 struct ata_device *dev; 2484 struct ata_device *dev;
@@ -2375,6 +2525,24 @@ static int ata_eh_skip_recovery(struct ata_link *link)
2375 return 1; 2525 return 1;
2376} 2526}
2377 2527
2528static int ata_eh_schedule_probe(struct ata_device *dev)
2529{
2530 struct ata_eh_context *ehc = &dev->link->eh_context;
2531
2532 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
2533 (ehc->did_probe_mask & (1 << dev->devno)))
2534 return 0;
2535
2536 ata_eh_detach_dev(dev);
2537 ata_dev_init(dev);
2538 ehc->did_probe_mask |= (1 << dev->devno);
2539 ehc->i.action |= ATA_EH_SOFTRESET;
2540 ehc->saved_xfer_mode[dev->devno] = 0;
2541 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
2542
2543 return 1;
2544}
2545
2378static int ata_eh_handle_dev_fail(struct ata_device *dev, int err) 2546static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2379{ 2547{
2380 struct ata_eh_context *ehc = &dev->link->eh_context; 2548 struct ata_eh_context *ehc = &dev->link->eh_context;
@@ -2406,16 +2574,9 @@ static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2406 if (ata_link_offline(dev->link)) 2574 if (ata_link_offline(dev->link))
2407 ata_eh_detach_dev(dev); 2575 ata_eh_detach_dev(dev);
2408 2576
2409 /* probe if requested */ 2577 /* schedule probe if necessary */
2410 if ((ehc->i.probe_mask & (1 << dev->devno)) && 2578 if (ata_eh_schedule_probe(dev))
2411 !(ehc->did_probe_mask & (1 << dev->devno))) {
2412 ata_eh_detach_dev(dev);
2413 ata_dev_init(dev);
2414
2415 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; 2579 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2416 ehc->did_probe_mask |= (1 << dev->devno);
2417 ehc->i.action |= ATA_EH_SOFTRESET;
2418 }
2419 2580
2420 return 1; 2581 return 1;
2421 } else { 2582 } else {
@@ -2492,14 +2653,9 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2492 if (dev->flags & ATA_DFLAG_DETACH) 2653 if (dev->flags & ATA_DFLAG_DETACH)
2493 ata_eh_detach_dev(dev); 2654 ata_eh_detach_dev(dev);
2494 2655
2495 if (!ata_dev_enabled(dev) && 2656 /* schedule probe if necessary */
2496 ((ehc->i.probe_mask & (1 << dev->devno)) && 2657 if (!ata_dev_enabled(dev))
2497 !(ehc->did_probe_mask & (1 << dev->devno)))) { 2658 ata_eh_schedule_probe(dev);
2498 ata_eh_detach_dev(dev);
2499 ata_dev_init(dev);
2500 ehc->did_probe_mask |= (1 << dev->devno);
2501 ehc->i.action |= ATA_EH_SOFTRESET;
2502 }
2503 } 2659 }
2504 } 2660 }
2505 2661
@@ -2747,6 +2903,7 @@ static void ata_eh_handle_port_suspend(struct ata_port *ap)
2747 if (ap->ops->port_suspend) 2903 if (ap->ops->port_suspend)
2748 rc = ap->ops->port_suspend(ap, ap->pm_mesg); 2904 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2749 2905
2906 ata_acpi_set_state(ap, PMSG_SUSPEND);
2750 out: 2907 out:
2751 /* report result */ 2908 /* report result */
2752 spin_lock_irqsave(ap->lock, flags); 2909 spin_lock_irqsave(ap->lock, flags);
@@ -2792,6 +2949,8 @@ static void ata_eh_handle_port_resume(struct ata_port *ap)
2792 2949
2793 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED)); 2950 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2794 2951
2952 ata_acpi_set_state(ap, PMSG_ON);
2953
2795 if (ap->ops->port_resume) 2954 if (ap->ops->port_resume)
2796 rc = ap->ops->port_resume(ap); 2955 rc = ap->ops->port_resume(ap);
2797 2956
diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
index 14daf4848f09..3fd08201bef4 100644
--- a/drivers/ata/libata-scsi.c
+++ b/drivers/ata/libata-scsi.c
@@ -517,7 +517,7 @@ static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
517 qc->scsicmd = cmd; 517 qc->scsicmd = cmd;
518 qc->scsidone = done; 518 qc->scsidone = done;
519 519
520 qc->__sg = scsi_sglist(cmd); 520 qc->sg = scsi_sglist(cmd);
521 qc->n_elem = scsi_sg_count(cmd); 521 qc->n_elem = scsi_sg_count(cmd);
522 } else { 522 } else {
523 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1); 523 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
@@ -2210,7 +2210,7 @@ unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
2210 2210
2211 /* sector size */ 2211 /* sector size */
2212 ATA_SCSI_RBUF_SET(6, ATA_SECT_SIZE >> 8); 2212 ATA_SCSI_RBUF_SET(6, ATA_SECT_SIZE >> 8);
2213 ATA_SCSI_RBUF_SET(7, ATA_SECT_SIZE); 2213 ATA_SCSI_RBUF_SET(7, ATA_SECT_SIZE & 0xff);
2214 } else { 2214 } else {
2215 /* sector count, 64-bit */ 2215 /* sector count, 64-bit */
2216 ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 7)); 2216 ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 7));
@@ -2224,7 +2224,7 @@ unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
2224 2224
2225 /* sector size */ 2225 /* sector size */
2226 ATA_SCSI_RBUF_SET(10, ATA_SECT_SIZE >> 8); 2226 ATA_SCSI_RBUF_SET(10, ATA_SECT_SIZE >> 8);
2227 ATA_SCSI_RBUF_SET(11, ATA_SECT_SIZE); 2227 ATA_SCSI_RBUF_SET(11, ATA_SECT_SIZE & 0xff);
2228 } 2228 }
2229 2229
2230 return 0; 2230 return 0;
@@ -2331,7 +2331,7 @@ static void atapi_request_sense(struct ata_queued_cmd *qc)
2331 DPRINTK("ATAPI request sense\n"); 2331 DPRINTK("ATAPI request sense\n");
2332 2332
2333 /* FIXME: is this needed? */ 2333 /* FIXME: is this needed? */
2334 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer)); 2334 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2335 2335
2336 ap->ops->tf_read(ap, &qc->tf); 2336 ap->ops->tf_read(ap, &qc->tf);
2337 2337
@@ -2341,7 +2341,9 @@ static void atapi_request_sense(struct ata_queued_cmd *qc)
2341 2341
2342 ata_qc_reinit(qc); 2342 ata_qc_reinit(qc);
2343 2343
2344 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer)); 2344 /* setup sg table and init transfer direction */
2345 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
2346 ata_sg_init(qc, &qc->sgent, 1);
2345 qc->dma_dir = DMA_FROM_DEVICE; 2347 qc->dma_dir = DMA_FROM_DEVICE;
2346 2348
2347 memset(&qc->cdb, 0, qc->dev->cdb_len); 2349 memset(&qc->cdb, 0, qc->dev->cdb_len);
@@ -2352,10 +2354,10 @@ static void atapi_request_sense(struct ata_queued_cmd *qc)
2352 qc->tf.command = ATA_CMD_PACKET; 2354 qc->tf.command = ATA_CMD_PACKET;
2353 2355
2354 if (ata_pio_use_silly(ap)) { 2356 if (ata_pio_use_silly(ap)) {
2355 qc->tf.protocol = ATA_PROT_ATAPI_DMA; 2357 qc->tf.protocol = ATAPI_PROT_DMA;
2356 qc->tf.feature |= ATAPI_PKT_DMA; 2358 qc->tf.feature |= ATAPI_PKT_DMA;
2357 } else { 2359 } else {
2358 qc->tf.protocol = ATA_PROT_ATAPI; 2360 qc->tf.protocol = ATAPI_PROT_PIO;
2359 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE; 2361 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2360 qc->tf.lbah = 0; 2362 qc->tf.lbah = 0;
2361 } 2363 }
@@ -2526,12 +2528,12 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
2526 if (using_pio || nodata) { 2528 if (using_pio || nodata) {
2527 /* no data, or PIO data xfer */ 2529 /* no data, or PIO data xfer */
2528 if (nodata) 2530 if (nodata)
2529 qc->tf.protocol = ATA_PROT_ATAPI_NODATA; 2531 qc->tf.protocol = ATAPI_PROT_NODATA;
2530 else 2532 else
2531 qc->tf.protocol = ATA_PROT_ATAPI; 2533 qc->tf.protocol = ATAPI_PROT_PIO;
2532 } else { 2534 } else {
2533 /* DMA data xfer */ 2535 /* DMA data xfer */
2534 qc->tf.protocol = ATA_PROT_ATAPI_DMA; 2536 qc->tf.protocol = ATAPI_PROT_DMA;
2535 qc->tf.feature |= ATAPI_PKT_DMA; 2537 qc->tf.feature |= ATAPI_PKT_DMA;
2536 2538
2537 if (atapi_dmadir && (scmd->sc_data_direction != DMA_TO_DEVICE)) 2539 if (atapi_dmadir && (scmd->sc_data_direction != DMA_TO_DEVICE))
@@ -2690,6 +2692,24 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
2690 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN) 2692 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
2691 goto invalid_fld; 2693 goto invalid_fld;
2692 2694
2695 /*
2696 * Filter TPM commands by default. These provide an
2697 * essentially uncontrolled encrypted "back door" between
2698 * applications and the disk. Set libata.allow_tpm=1 if you
2699 * have a real reason for wanting to use them. This ensures
2700 * that installed software cannot easily mess stuff up without
2701 * user intent. DVR type users will probably ship with this enabled
2702 * for movie content management.
2703 *
2704 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
2705 * for this and should do in future but that it is not sufficient as
2706 * DCS is an optional feature set. Thus we also do the software filter
2707 * so that we comply with the TC consortium stated goal that the user
2708 * can turn off TC features of their system.
2709 */
2710 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
2711 goto invalid_fld;
2712
2693 /* We may not issue DMA commands if no DMA mode is set */ 2713 /* We may not issue DMA commands if no DMA mode is set */
2694 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) 2714 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
2695 goto invalid_fld; 2715 goto invalid_fld;
diff --git a/drivers/ata/libata-sff.c b/drivers/ata/libata-sff.c
index b7ac80b4b1fb..60cd4b179766 100644
--- a/drivers/ata/libata-sff.c
+++ b/drivers/ata/libata-sff.c
@@ -147,7 +147,9 @@ void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
147 * @tf: ATA taskfile register set for storing input 147 * @tf: ATA taskfile register set for storing input
148 * 148 *
149 * Reads ATA taskfile registers for currently-selected device 149 * Reads ATA taskfile registers for currently-selected device
150 * into @tf. 150 * into @tf. Assumes the device has a fully SFF compliant task file
151 * layout and behaviour. If you device does not (eg has a different
152 * status method) then you will need to provide a replacement tf_read
151 * 153 *
152 * LOCKING: 154 * LOCKING:
153 * Inherited from caller. 155 * Inherited from caller.
@@ -156,7 +158,7 @@ void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
156{ 158{
157 struct ata_ioports *ioaddr = &ap->ioaddr; 159 struct ata_ioports *ioaddr = &ap->ioaddr;
158 160
159 tf->command = ata_chk_status(ap); 161 tf->command = ata_check_status(ap);
160 tf->feature = ioread8(ioaddr->error_addr); 162 tf->feature = ioread8(ioaddr->error_addr);
161 tf->nsect = ioread8(ioaddr->nsect_addr); 163 tf->nsect = ioread8(ioaddr->nsect_addr);
162 tf->lbal = ioread8(ioaddr->lbal_addr); 164 tf->lbal = ioread8(ioaddr->lbal_addr);
@@ -415,7 +417,7 @@ void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
415 ap->hsm_task_state = HSM_ST_IDLE; 417 ap->hsm_task_state = HSM_ST_IDLE;
416 418
417 if (qc && (qc->tf.protocol == ATA_PROT_DMA || 419 if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
418 qc->tf.protocol == ATA_PROT_ATAPI_DMA)) { 420 qc->tf.protocol == ATAPI_PROT_DMA)) {
419 u8 host_stat; 421 u8 host_stat;
420 422
421 host_stat = ap->ops->bmdma_status(ap); 423 host_stat = ap->ops->bmdma_status(ap);
@@ -549,7 +551,7 @@ int ata_pci_init_bmdma(struct ata_host *host)
549 return rc; 551 return rc;
550 552
551 /* request and iomap DMA region */ 553 /* request and iomap DMA region */
552 rc = pcim_iomap_regions(pdev, 1 << 4, DRV_NAME); 554 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
553 if (rc) { 555 if (rc) {
554 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n"); 556 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
555 return -ENOMEM; 557 return -ENOMEM;
@@ -619,7 +621,8 @@ int ata_pci_init_sff_host(struct ata_host *host)
619 continue; 621 continue;
620 } 622 }
621 623
622 rc = pcim_iomap_regions(pdev, 0x3 << base, DRV_NAME); 624 rc = pcim_iomap_regions(pdev, 0x3 << base,
625 dev_driver_string(gdev));
623 if (rc) { 626 if (rc) {
624 dev_printk(KERN_WARNING, gdev, 627 dev_printk(KERN_WARNING, gdev,
625 "failed to request/iomap BARs for port %d " 628 "failed to request/iomap BARs for port %d "
@@ -711,6 +714,99 @@ int ata_pci_prepare_sff_host(struct pci_dev *pdev,
711} 714}
712 715
713/** 716/**
717 * ata_pci_activate_sff_host - start SFF host, request IRQ and register it
718 * @host: target SFF ATA host
719 * @irq_handler: irq_handler used when requesting IRQ(s)
720 * @sht: scsi_host_template to use when registering the host
721 *
722 * This is the counterpart of ata_host_activate() for SFF ATA
723 * hosts. This separate helper is necessary because SFF hosts
724 * use two separate interrupts in legacy mode.
725 *
726 * LOCKING:
727 * Inherited from calling layer (may sleep).
728 *
729 * RETURNS:
730 * 0 on success, -errno otherwise.
731 */
732int ata_pci_activate_sff_host(struct ata_host *host,
733 irq_handler_t irq_handler,
734 struct scsi_host_template *sht)
735{
736 struct device *dev = host->dev;
737 struct pci_dev *pdev = to_pci_dev(dev);
738 const char *drv_name = dev_driver_string(host->dev);
739 int legacy_mode = 0, rc;
740
741 rc = ata_host_start(host);
742 if (rc)
743 return rc;
744
745 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
746 u8 tmp8, mask;
747
748 /* TODO: What if one channel is in native mode ... */
749 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
750 mask = (1 << 2) | (1 << 0);
751 if ((tmp8 & mask) != mask)
752 legacy_mode = 1;
753#if defined(CONFIG_NO_ATA_LEGACY)
754 /* Some platforms with PCI limits cannot address compat
755 port space. In that case we punt if their firmware has
756 left a device in compatibility mode */
757 if (legacy_mode) {
758 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
759 return -EOPNOTSUPP;
760 }
761#endif
762 }
763
764 if (!devres_open_group(dev, NULL, GFP_KERNEL))
765 return -ENOMEM;
766
767 if (!legacy_mode && pdev->irq) {
768 rc = devm_request_irq(dev, pdev->irq, irq_handler,
769 IRQF_SHARED, drv_name, host);
770 if (rc)
771 goto out;
772
773 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
774 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
775 } else if (legacy_mode) {
776 if (!ata_port_is_dummy(host->ports[0])) {
777 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
778 irq_handler, IRQF_SHARED,
779 drv_name, host);
780 if (rc)
781 goto out;
782
783 ata_port_desc(host->ports[0], "irq %d",
784 ATA_PRIMARY_IRQ(pdev));
785 }
786
787 if (!ata_port_is_dummy(host->ports[1])) {
788 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
789 irq_handler, IRQF_SHARED,
790 drv_name, host);
791 if (rc)
792 goto out;
793
794 ata_port_desc(host->ports[1], "irq %d",
795 ATA_SECONDARY_IRQ(pdev));
796 }
797 }
798
799 rc = ata_host_register(host, sht);
800 out:
801 if (rc == 0)
802 devres_remove_group(dev, NULL);
803 else
804 devres_release_group(dev, NULL);
805
806 return rc;
807}
808
809/**
714 * ata_pci_init_one - Initialize/register PCI IDE host controller 810 * ata_pci_init_one - Initialize/register PCI IDE host controller
715 * @pdev: Controller to be initialized 811 * @pdev: Controller to be initialized
716 * @ppi: array of port_info, must be enough for two ports 812 * @ppi: array of port_info, must be enough for two ports
@@ -739,8 +835,6 @@ int ata_pci_init_one(struct pci_dev *pdev,
739 struct device *dev = &pdev->dev; 835 struct device *dev = &pdev->dev;
740 const struct ata_port_info *pi = NULL; 836 const struct ata_port_info *pi = NULL;
741 struct ata_host *host = NULL; 837 struct ata_host *host = NULL;
742 u8 mask;
743 int legacy_mode = 0;
744 int i, rc; 838 int i, rc;
745 839
746 DPRINTK("ENTER\n"); 840 DPRINTK("ENTER\n");
@@ -762,95 +856,24 @@ int ata_pci_init_one(struct pci_dev *pdev,
762 if (!devres_open_group(dev, NULL, GFP_KERNEL)) 856 if (!devres_open_group(dev, NULL, GFP_KERNEL))
763 return -ENOMEM; 857 return -ENOMEM;
764 858
765 /* FIXME: Really for ATA it isn't safe because the device may be
766 multi-purpose and we want to leave it alone if it was already
767 enabled. Secondly for shared use as Arjan says we want refcounting
768
769 Checking dev->is_enabled is insufficient as this is not set at
770 boot for the primary video which is BIOS enabled
771 */
772
773 rc = pcim_enable_device(pdev); 859 rc = pcim_enable_device(pdev);
774 if (rc) 860 if (rc)
775 goto err_out; 861 goto out;
776 862
777 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) { 863 /* prepare and activate SFF host */
778 u8 tmp8;
779
780 /* TODO: What if one channel is in native mode ... */
781 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
782 mask = (1 << 2) | (1 << 0);
783 if ((tmp8 & mask) != mask)
784 legacy_mode = 1;
785#if defined(CONFIG_NO_ATA_LEGACY)
786 /* Some platforms with PCI limits cannot address compat
787 port space. In that case we punt if their firmware has
788 left a device in compatibility mode */
789 if (legacy_mode) {
790 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
791 rc = -EOPNOTSUPP;
792 goto err_out;
793 }
794#endif
795 }
796
797 /* prepare host */
798 rc = ata_pci_prepare_sff_host(pdev, ppi, &host); 864 rc = ata_pci_prepare_sff_host(pdev, ppi, &host);
799 if (rc) 865 if (rc)
800 goto err_out; 866 goto out;
801 867
802 pci_set_master(pdev); 868 pci_set_master(pdev);
869 rc = ata_pci_activate_sff_host(host, pi->port_ops->irq_handler,
870 pi->sht);
871 out:
872 if (rc == 0)
873 devres_remove_group(&pdev->dev, NULL);
874 else
875 devres_release_group(&pdev->dev, NULL);
803 876
804 /* start host and request IRQ */
805 rc = ata_host_start(host);
806 if (rc)
807 goto err_out;
808
809 if (!legacy_mode && pdev->irq) {
810 /* We may have no IRQ assigned in which case we can poll. This
811 shouldn't happen on a sane system but robustness is cheap
812 in this case */
813 rc = devm_request_irq(dev, pdev->irq, pi->port_ops->irq_handler,
814 IRQF_SHARED, DRV_NAME, host);
815 if (rc)
816 goto err_out;
817
818 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
819 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
820 } else if (legacy_mode) {
821 if (!ata_port_is_dummy(host->ports[0])) {
822 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
823 pi->port_ops->irq_handler,
824 IRQF_SHARED, DRV_NAME, host);
825 if (rc)
826 goto err_out;
827
828 ata_port_desc(host->ports[0], "irq %d",
829 ATA_PRIMARY_IRQ(pdev));
830 }
831
832 if (!ata_port_is_dummy(host->ports[1])) {
833 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
834 pi->port_ops->irq_handler,
835 IRQF_SHARED, DRV_NAME, host);
836 if (rc)
837 goto err_out;
838
839 ata_port_desc(host->ports[1], "irq %d",
840 ATA_SECONDARY_IRQ(pdev));
841 }
842 }
843
844 /* register */
845 rc = ata_host_register(host, pi->sht);
846 if (rc)
847 goto err_out;
848
849 devres_remove_group(dev, NULL);
850 return 0;
851
852err_out:
853 devres_release_group(dev, NULL);
854 return rc; 877 return rc;
855} 878}
856 879
diff --git a/drivers/ata/libata.h b/drivers/ata/libata.h
index bbe59c2fd1e2..409ffb9af163 100644
--- a/drivers/ata/libata.h
+++ b/drivers/ata/libata.h
@@ -60,6 +60,7 @@ extern int atapi_dmadir;
60extern int atapi_passthru16; 60extern int atapi_passthru16;
61extern int libata_fua; 61extern int libata_fua;
62extern int libata_noacpi; 62extern int libata_noacpi;
63extern int libata_allow_tpm;
63extern struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev); 64extern struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev);
64extern int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev, 65extern int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
65 u64 block, u32 n_block, unsigned int tf_flags, 66 u64 block, u32 n_block, unsigned int tf_flags,
@@ -85,7 +86,6 @@ extern int ata_dev_configure(struct ata_device *dev);
85extern int sata_down_spd_limit(struct ata_link *link); 86extern int sata_down_spd_limit(struct ata_link *link);
86extern int sata_set_spd_needed(struct ata_link *link); 87extern int sata_set_spd_needed(struct ata_link *link);
87extern int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel); 88extern int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel);
88extern int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev);
89extern void ata_sg_clean(struct ata_queued_cmd *qc); 89extern void ata_sg_clean(struct ata_queued_cmd *qc);
90extern void ata_qc_free(struct ata_queued_cmd *qc); 90extern void ata_qc_free(struct ata_queued_cmd *qc);
91extern void ata_qc_issue(struct ata_queued_cmd *qc); 91extern void ata_qc_issue(struct ata_queued_cmd *qc);
@@ -113,6 +113,7 @@ extern int ata_acpi_on_suspend(struct ata_port *ap);
113extern void ata_acpi_on_resume(struct ata_port *ap); 113extern void ata_acpi_on_resume(struct ata_port *ap);
114extern int ata_acpi_on_devcfg(struct ata_device *dev); 114extern int ata_acpi_on_devcfg(struct ata_device *dev);
115extern void ata_acpi_on_disable(struct ata_device *dev); 115extern void ata_acpi_on_disable(struct ata_device *dev);
116extern void ata_acpi_set_state(struct ata_port *ap, pm_message_t state);
116#else 117#else
117static inline void ata_acpi_associate_sata_port(struct ata_port *ap) { } 118static inline void ata_acpi_associate_sata_port(struct ata_port *ap) { }
118static inline void ata_acpi_associate(struct ata_host *host) { } 119static inline void ata_acpi_associate(struct ata_host *host) { }
@@ -121,6 +122,8 @@ static inline int ata_acpi_on_suspend(struct ata_port *ap) { return 0; }
121static inline void ata_acpi_on_resume(struct ata_port *ap) { } 122static inline void ata_acpi_on_resume(struct ata_port *ap) { }
122static inline int ata_acpi_on_devcfg(struct ata_device *dev) { return 0; } 123static inline int ata_acpi_on_devcfg(struct ata_device *dev) { return 0; }
123static inline void ata_acpi_on_disable(struct ata_device *dev) { } 124static inline void ata_acpi_on_disable(struct ata_device *dev) { }
125static inline void ata_acpi_set_state(struct ata_port *ap,
126 pm_message_t state) { }
124#endif 127#endif
125 128
126/* libata-scsi.c */ 129/* libata-scsi.c */
@@ -183,6 +186,7 @@ extern void ata_eh_report(struct ata_port *ap);
183extern int ata_eh_reset(struct ata_link *link, int classify, 186extern int ata_eh_reset(struct ata_link *link, int classify,
184 ata_prereset_fn_t prereset, ata_reset_fn_t softreset, 187 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
185 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset); 188 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset);
189extern int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev);
186extern int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset, 190extern int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
187 ata_reset_fn_t softreset, ata_reset_fn_t hardreset, 191 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
188 ata_postreset_fn_t postreset, 192 ata_postreset_fn_t postreset,
diff --git a/drivers/ata/pata_acpi.c b/drivers/ata/pata_acpi.c
index e4542ab9c7f8..244098a80ce4 100644
--- a/drivers/ata/pata_acpi.c
+++ b/drivers/ata/pata_acpi.c
@@ -81,17 +81,6 @@ static void pacpi_error_handler(struct ata_port *ap)
81 NULL, ata_std_postreset); 81 NULL, ata_std_postreset);
82} 82}
83 83
84/* Welcome to ACPI, bring a bucket */
85static const unsigned int pio_cycle[7] = {
86 600, 383, 240, 180, 120, 100, 80
87};
88static const unsigned int mwdma_cycle[5] = {
89 480, 150, 120, 100, 80
90};
91static const unsigned int udma_cycle[7] = {
92 120, 80, 60, 45, 30, 20, 15
93};
94
95/** 84/**
96 * pacpi_discover_modes - filter non ACPI modes 85 * pacpi_discover_modes - filter non ACPI modes
97 * @adev: ATA device 86 * @adev: ATA device
@@ -103,56 +92,20 @@ static const unsigned int udma_cycle[7] = {
103 92
104static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev) 93static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev)
105{ 94{
106 int unit = adev->devno;
107 struct pata_acpi *acpi = ap->private_data; 95 struct pata_acpi *acpi = ap->private_data;
108 int i;
109 u32 t;
110 unsigned long mask = (0x7f << ATA_SHIFT_UDMA) | (0x7 << ATA_SHIFT_MWDMA) | (0x1F << ATA_SHIFT_PIO);
111
112 struct ata_acpi_gtm probe; 96 struct ata_acpi_gtm probe;
97 unsigned int xfer_mask;
113 98
114 probe = acpi->gtm; 99 probe = acpi->gtm;
115 100
116 /* We always use the 0 slot for crap hardware */
117 if (!(probe.flags & 0x10))
118 unit = 0;
119
120 ata_acpi_gtm(ap, &probe); 101 ata_acpi_gtm(ap, &probe);
121 102
122 /* Start by scanning for PIO modes */ 103 xfer_mask = ata_acpi_gtm_xfermask(adev, &probe);
123 for (i = 0; i < 7; i++) {
124 t = probe.drive[unit].pio;
125 if (t <= pio_cycle[i]) {
126 mask |= (2 << (ATA_SHIFT_PIO + i)) - 1;
127 break;
128 }
129 }
130 104
131 /* See if we have MWDMA or UDMA data. We don't bother with MWDMA 105 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
132 if UDMA is availabe as this means the BIOS set UDMA and our
133 error changedown if it works is UDMA to PIO anyway */
134 if (probe.flags & (1 << (2 * unit))) {
135 /* MWDMA */
136 for (i = 0; i < 5; i++) {
137 t = probe.drive[unit].dma;
138 if (t <= mwdma_cycle[i]) {
139 mask |= (2 << (ATA_SHIFT_MWDMA + i)) - 1;
140 break;
141 }
142 }
143 } else {
144 /* UDMA */
145 for (i = 0; i < 7; i++) {
146 t = probe.drive[unit].dma;
147 if (t <= udma_cycle[i]) {
148 mask |= (2 << (ATA_SHIFT_UDMA + i)) - 1;
149 break;
150 }
151 }
152 }
153 if (mask & (0xF8 << ATA_SHIFT_UDMA))
154 ap->cbl = ATA_CBL_PATA80; 106 ap->cbl = ATA_CBL_PATA80;
155 return mask; 107
108 return xfer_mask;
156} 109}
157 110
158/** 111/**
@@ -180,12 +133,14 @@ static void pacpi_set_piomode(struct ata_port *ap, struct ata_device *adev)
180{ 133{
181 int unit = adev->devno; 134 int unit = adev->devno;
182 struct pata_acpi *acpi = ap->private_data; 135 struct pata_acpi *acpi = ap->private_data;
136 const struct ata_timing *t;
183 137
184 if (!(acpi->gtm.flags & 0x10)) 138 if (!(acpi->gtm.flags & 0x10))
185 unit = 0; 139 unit = 0;
186 140
187 /* Now stuff the nS values into the structure */ 141 /* Now stuff the nS values into the structure */
188 acpi->gtm.drive[unit].pio = pio_cycle[adev->pio_mode - XFER_PIO_0]; 142 t = ata_timing_find_mode(adev->pio_mode);
143 acpi->gtm.drive[unit].pio = t->cycle;
189 ata_acpi_stm(ap, &acpi->gtm); 144 ata_acpi_stm(ap, &acpi->gtm);
190 /* See what mode we actually got */ 145 /* See what mode we actually got */
191 ata_acpi_gtm(ap, &acpi->gtm); 146 ata_acpi_gtm(ap, &acpi->gtm);
@@ -201,16 +156,18 @@ static void pacpi_set_dmamode(struct ata_port *ap, struct ata_device *adev)
201{ 156{
202 int unit = adev->devno; 157 int unit = adev->devno;
203 struct pata_acpi *acpi = ap->private_data; 158 struct pata_acpi *acpi = ap->private_data;
159 const struct ata_timing *t;
204 160
205 if (!(acpi->gtm.flags & 0x10)) 161 if (!(acpi->gtm.flags & 0x10))
206 unit = 0; 162 unit = 0;
207 163
208 /* Now stuff the nS values into the structure */ 164 /* Now stuff the nS values into the structure */
165 t = ata_timing_find_mode(adev->dma_mode);
209 if (adev->dma_mode >= XFER_UDMA_0) { 166 if (adev->dma_mode >= XFER_UDMA_0) {
210 acpi->gtm.drive[unit].dma = udma_cycle[adev->dma_mode - XFER_UDMA_0]; 167 acpi->gtm.drive[unit].dma = t->udma;
211 acpi->gtm.flags |= (1 << (2 * unit)); 168 acpi->gtm.flags |= (1 << (2 * unit));
212 } else { 169 } else {
213 acpi->gtm.drive[unit].dma = mwdma_cycle[adev->dma_mode - XFER_MW_DMA_0]; 170 acpi->gtm.drive[unit].dma = t->cycle;
214 acpi->gtm.flags &= ~(1 << (2 * unit)); 171 acpi->gtm.flags &= ~(1 << (2 * unit));
215 } 172 }
216 ata_acpi_stm(ap, &acpi->gtm); 173 ata_acpi_stm(ap, &acpi->gtm);
diff --git a/drivers/ata/pata_ali.c b/drivers/ata/pata_ali.c
index 8caf9afc8b90..7e68edf3c0f3 100644
--- a/drivers/ata/pata_ali.c
+++ b/drivers/ata/pata_ali.c
@@ -64,7 +64,7 @@ static int ali_cable_override(struct pci_dev *pdev)
64 if (pdev->subsystem_vendor == 0x10CF && pdev->subsystem_device == 0x10AF) 64 if (pdev->subsystem_vendor == 0x10CF && pdev->subsystem_device == 0x10AF)
65 return 1; 65 return 1;
66 /* Mitac 8317 (Winbook-A) and relatives */ 66 /* Mitac 8317 (Winbook-A) and relatives */
67 if (pdev->subsystem_vendor == 0x1071 && pdev->subsystem_device == 0x8317) 67 if (pdev->subsystem_vendor == 0x1071 && pdev->subsystem_device == 0x8317)
68 return 1; 68 return 1;
69 /* Systems by DMI */ 69 /* Systems by DMI */
70 if (dmi_check_system(cable_dmi_table)) 70 if (dmi_check_system(cable_dmi_table))
diff --git a/drivers/ata/pata_amd.c b/drivers/ata/pata_amd.c
index 3cc27b514654..761a66608d7b 100644
--- a/drivers/ata/pata_amd.c
+++ b/drivers/ata/pata_amd.c
@@ -220,6 +220,62 @@ static void amd133_set_dmamode(struct ata_port *ap, struct ata_device *adev)
220 timing_setup(ap, adev, 0x40, adev->dma_mode, 4); 220 timing_setup(ap, adev, 0x40, adev->dma_mode, 4);
221} 221}
222 222
223/* Both host-side and drive-side detection results are worthless on NV
224 * PATAs. Ignore them and just follow what BIOS configured. Both the
225 * current configuration in PCI config reg and ACPI GTM result are
226 * cached during driver attach and are consulted to select transfer
227 * mode.
228 */
229static unsigned long nv_mode_filter(struct ata_device *dev,
230 unsigned long xfer_mask)
231{
232 static const unsigned int udma_mask_map[] =
233 { ATA_UDMA2, ATA_UDMA1, ATA_UDMA0, 0,
234 ATA_UDMA3, ATA_UDMA4, ATA_UDMA5, ATA_UDMA6 };
235 struct ata_port *ap = dev->link->ap;
236 char acpi_str[32] = "";
237 u32 saved_udma, udma;
238 const struct ata_acpi_gtm *gtm;
239 unsigned long bios_limit = 0, acpi_limit = 0, limit;
240
241 /* find out what BIOS configured */
242 udma = saved_udma = (unsigned long)ap->host->private_data;
243
244 if (ap->port_no == 0)
245 udma >>= 16;
246 if (dev->devno == 0)
247 udma >>= 8;
248
249 if ((udma & 0xc0) == 0xc0)
250 bios_limit = ata_pack_xfermask(0, 0, udma_mask_map[udma & 0x7]);
251
252 /* consult ACPI GTM too */
253 gtm = ata_acpi_init_gtm(ap);
254 if (gtm) {
255 acpi_limit = ata_acpi_gtm_xfermask(dev, gtm);
256
257 snprintf(acpi_str, sizeof(acpi_str), " (%u:%u:0x%x)",
258 gtm->drive[0].dma, gtm->drive[1].dma, gtm->flags);
259 }
260
261 /* be optimistic, EH can take care of things if something goes wrong */
262 limit = bios_limit | acpi_limit;
263
264 /* If PIO or DMA isn't configured at all, don't limit. Let EH
265 * handle it.
266 */
267 if (!(limit & ATA_MASK_PIO))
268 limit |= ATA_MASK_PIO;
269 if (!(limit & (ATA_MASK_MWDMA | ATA_MASK_UDMA)))
270 limit |= ATA_MASK_MWDMA | ATA_MASK_UDMA;
271
272 ata_port_printk(ap, KERN_DEBUG, "nv_mode_filter: 0x%lx&0x%lx->0x%lx, "
273 "BIOS=0x%lx (0x%x) ACPI=0x%lx%s\n",
274 xfer_mask, limit, xfer_mask & limit, bios_limit,
275 saved_udma, acpi_limit, acpi_str);
276
277 return xfer_mask & limit;
278}
223 279
224/** 280/**
225 * nv_probe_init - cable detection 281 * nv_probe_init - cable detection
@@ -252,31 +308,6 @@ static void nv_error_handler(struct ata_port *ap)
252 ata_std_postreset); 308 ata_std_postreset);
253} 309}
254 310
255static int nv_cable_detect(struct ata_port *ap)
256{
257 static const u8 bitmask[2] = {0x03, 0x0C};
258 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
259 u8 ata66;
260 u16 udma;
261 int cbl;
262
263 pci_read_config_byte(pdev, 0x52, &ata66);
264 if (ata66 & bitmask[ap->port_no])
265 cbl = ATA_CBL_PATA80;
266 else
267 cbl = ATA_CBL_PATA40;
268
269 /* We now have to double check because the Nvidia boxes BIOS
270 doesn't always set the cable bits but does set mode bits */
271 pci_read_config_word(pdev, 0x62 - 2 * ap->port_no, &udma);
272 if ((udma & 0xC4) == 0xC4 || (udma & 0xC400) == 0xC400)
273 cbl = ATA_CBL_PATA80;
274 /* And a triple check across suspend/resume with ACPI around */
275 if (ata_acpi_cbl_80wire(ap))
276 cbl = ATA_CBL_PATA80;
277 return cbl;
278}
279
280/** 311/**
281 * nv100_set_piomode - set initial PIO mode data 312 * nv100_set_piomode - set initial PIO mode data
282 * @ap: ATA interface 313 * @ap: ATA interface
@@ -314,6 +345,14 @@ static void nv133_set_dmamode(struct ata_port *ap, struct ata_device *adev)
314 timing_setup(ap, adev, 0x50, adev->dma_mode, 4); 345 timing_setup(ap, adev, 0x50, adev->dma_mode, 4);
315} 346}
316 347
348static void nv_host_stop(struct ata_host *host)
349{
350 u32 udma = (unsigned long)host->private_data;
351
352 /* restore PCI config register 0x60 */
353 pci_write_config_dword(to_pci_dev(host->dev), 0x60, udma);
354}
355
317static struct scsi_host_template amd_sht = { 356static struct scsi_host_template amd_sht = {
318 .module = THIS_MODULE, 357 .module = THIS_MODULE,
319 .name = DRV_NAME, 358 .name = DRV_NAME,
@@ -478,7 +517,8 @@ static struct ata_port_operations nv100_port_ops = {
478 .thaw = ata_bmdma_thaw, 517 .thaw = ata_bmdma_thaw,
479 .error_handler = nv_error_handler, 518 .error_handler = nv_error_handler,
480 .post_internal_cmd = ata_bmdma_post_internal_cmd, 519 .post_internal_cmd = ata_bmdma_post_internal_cmd,
481 .cable_detect = nv_cable_detect, 520 .cable_detect = ata_cable_ignore,
521 .mode_filter = nv_mode_filter,
482 522
483 .bmdma_setup = ata_bmdma_setup, 523 .bmdma_setup = ata_bmdma_setup,
484 .bmdma_start = ata_bmdma_start, 524 .bmdma_start = ata_bmdma_start,
@@ -495,6 +535,7 @@ static struct ata_port_operations nv100_port_ops = {
495 .irq_on = ata_irq_on, 535 .irq_on = ata_irq_on,
496 536
497 .port_start = ata_sff_port_start, 537 .port_start = ata_sff_port_start,
538 .host_stop = nv_host_stop,
498}; 539};
499 540
500static struct ata_port_operations nv133_port_ops = { 541static struct ata_port_operations nv133_port_ops = {
@@ -511,7 +552,8 @@ static struct ata_port_operations nv133_port_ops = {
511 .thaw = ata_bmdma_thaw, 552 .thaw = ata_bmdma_thaw,
512 .error_handler = nv_error_handler, 553 .error_handler = nv_error_handler,
513 .post_internal_cmd = ata_bmdma_post_internal_cmd, 554 .post_internal_cmd = ata_bmdma_post_internal_cmd,
514 .cable_detect = nv_cable_detect, 555 .cable_detect = ata_cable_ignore,
556 .mode_filter = nv_mode_filter,
515 557
516 .bmdma_setup = ata_bmdma_setup, 558 .bmdma_setup = ata_bmdma_setup,
517 .bmdma_start = ata_bmdma_start, 559 .bmdma_start = ata_bmdma_start,
@@ -528,6 +570,7 @@ static struct ata_port_operations nv133_port_ops = {
528 .irq_on = ata_irq_on, 570 .irq_on = ata_irq_on,
529 571
530 .port_start = ata_sff_port_start, 572 .port_start = ata_sff_port_start,
573 .host_stop = nv_host_stop,
531}; 574};
532 575
533static int amd_init_one(struct pci_dev *pdev, const struct pci_device_id *id) 576static int amd_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
@@ -614,7 +657,8 @@ static int amd_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
614 .port_ops = &amd100_port_ops 657 .port_ops = &amd100_port_ops
615 } 658 }
616 }; 659 };
617 const struct ata_port_info *ppi[] = { NULL, NULL }; 660 struct ata_port_info pi;
661 const struct ata_port_info *ppi[] = { &pi, NULL };
618 static int printed_version; 662 static int printed_version;
619 int type = id->driver_data; 663 int type = id->driver_data;
620 u8 fifo; 664 u8 fifo;
@@ -628,6 +672,19 @@ static int amd_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
628 if (type == 1 && pdev->revision > 0x7) 672 if (type == 1 && pdev->revision > 0x7)
629 type = 2; 673 type = 2;
630 674
675 /* Serenade ? */
676 if (type == 5 && pdev->subsystem_vendor == PCI_VENDOR_ID_AMD &&
677 pdev->subsystem_device == PCI_DEVICE_ID_AMD_SERENADE)
678 type = 6; /* UDMA 100 only */
679
680 /*
681 * Okay, type is determined now. Apply type-specific workarounds.
682 */
683 pi = info[type];
684
685 if (type < 3)
686 ata_pci_clear_simplex(pdev);
687
631 /* Check for AMD7411 */ 688 /* Check for AMD7411 */
632 if (type == 3) 689 if (type == 3)
633 /* FIFO is broken */ 690 /* FIFO is broken */
@@ -635,16 +692,17 @@ static int amd_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
635 else 692 else
636 pci_write_config_byte(pdev, 0x41, fifo | 0xF0); 693 pci_write_config_byte(pdev, 0x41, fifo | 0xF0);
637 694
638 /* Serenade ? */ 695 /* Cable detection on Nvidia chips doesn't work too well,
639 if (type == 5 && pdev->subsystem_vendor == PCI_VENDOR_ID_AMD && 696 * cache BIOS programmed UDMA mode.
640 pdev->subsystem_device == PCI_DEVICE_ID_AMD_SERENADE) 697 */
641 type = 6; /* UDMA 100 only */ 698 if (type == 7 || type == 8) {
699 u32 udma;
642 700
643 if (type < 3) 701 pci_read_config_dword(pdev, 0x60, &udma);
644 ata_pci_clear_simplex(pdev); 702 pi.private_data = (void *)(unsigned long)udma;
703 }
645 704
646 /* And fire it up */ 705 /* And fire it up */
647 ppi[0] = &info[type];
648 return ata_pci_init_one(pdev, ppi); 706 return ata_pci_init_one(pdev, ppi);
649} 707}
650 708
diff --git a/drivers/ata/pata_bf54x.c b/drivers/ata/pata_bf54x.c
index 7842cc487359..a32e3c44a606 100644
--- a/drivers/ata/pata_bf54x.c
+++ b/drivers/ata/pata_bf54x.c
@@ -832,6 +832,7 @@ static void bfin_bmdma_setup(struct ata_queued_cmd *qc)
832{ 832{
833 unsigned short config = WDSIZE_16; 833 unsigned short config = WDSIZE_16;
834 struct scatterlist *sg; 834 struct scatterlist *sg;
835 unsigned int si;
835 836
836 pr_debug("in atapi dma setup\n"); 837 pr_debug("in atapi dma setup\n");
837 /* Program the ATA_CTRL register with dir */ 838 /* Program the ATA_CTRL register with dir */
@@ -839,7 +840,7 @@ static void bfin_bmdma_setup(struct ata_queued_cmd *qc)
839 /* fill the ATAPI DMA controller */ 840 /* fill the ATAPI DMA controller */
840 set_dma_config(CH_ATAPI_TX, config); 841 set_dma_config(CH_ATAPI_TX, config);
841 set_dma_x_modify(CH_ATAPI_TX, 2); 842 set_dma_x_modify(CH_ATAPI_TX, 2);
842 ata_for_each_sg(sg, qc) { 843 for_each_sg(qc->sg, sg, qc->n_elem, si) {
843 set_dma_start_addr(CH_ATAPI_TX, sg_dma_address(sg)); 844 set_dma_start_addr(CH_ATAPI_TX, sg_dma_address(sg));
844 set_dma_x_count(CH_ATAPI_TX, sg_dma_len(sg) >> 1); 845 set_dma_x_count(CH_ATAPI_TX, sg_dma_len(sg) >> 1);
845 } 846 }
@@ -848,7 +849,7 @@ static void bfin_bmdma_setup(struct ata_queued_cmd *qc)
848 /* fill the ATAPI DMA controller */ 849 /* fill the ATAPI DMA controller */
849 set_dma_config(CH_ATAPI_RX, config); 850 set_dma_config(CH_ATAPI_RX, config);
850 set_dma_x_modify(CH_ATAPI_RX, 2); 851 set_dma_x_modify(CH_ATAPI_RX, 2);
851 ata_for_each_sg(sg, qc) { 852 for_each_sg(qc->sg, sg, qc->n_elem, si) {
852 set_dma_start_addr(CH_ATAPI_RX, sg_dma_address(sg)); 853 set_dma_start_addr(CH_ATAPI_RX, sg_dma_address(sg));
853 set_dma_x_count(CH_ATAPI_RX, sg_dma_len(sg) >> 1); 854 set_dma_x_count(CH_ATAPI_RX, sg_dma_len(sg) >> 1);
854 } 855 }
@@ -867,6 +868,7 @@ static void bfin_bmdma_start(struct ata_queued_cmd *qc)
867 struct ata_port *ap = qc->ap; 868 struct ata_port *ap = qc->ap;
868 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr; 869 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
869 struct scatterlist *sg; 870 struct scatterlist *sg;
871 unsigned int si;
870 872
871 pr_debug("in atapi dma start\n"); 873 pr_debug("in atapi dma start\n");
872 if (!(ap->udma_mask || ap->mwdma_mask)) 874 if (!(ap->udma_mask || ap->mwdma_mask))
@@ -881,7 +883,7 @@ static void bfin_bmdma_start(struct ata_queued_cmd *qc)
881 * data cache is enabled. Otherwise, this loop 883 * data cache is enabled. Otherwise, this loop
882 * is an empty loop and optimized out. 884 * is an empty loop and optimized out.
883 */ 885 */
884 ata_for_each_sg(sg, qc) { 886 for_each_sg(qc->sg, sg, qc->n_elem, si) {
885 flush_dcache_range(sg_dma_address(sg), 887 flush_dcache_range(sg_dma_address(sg),
886 sg_dma_address(sg) + sg_dma_len(sg)); 888 sg_dma_address(sg) + sg_dma_len(sg));
887 } 889 }
@@ -910,7 +912,7 @@ static void bfin_bmdma_start(struct ata_queued_cmd *qc)
910 ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) | TFRCNT_RST); 912 ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) | TFRCNT_RST);
911 913
912 /* Set transfer length to buffer len */ 914 /* Set transfer length to buffer len */
913 ata_for_each_sg(sg, qc) { 915 for_each_sg(qc->sg, sg, qc->n_elem, si) {
914 ATAPI_SET_XFER_LEN(base, (sg_dma_len(sg) >> 1)); 916 ATAPI_SET_XFER_LEN(base, (sg_dma_len(sg) >> 1));
915 } 917 }
916 918
@@ -932,6 +934,7 @@ static void bfin_bmdma_stop(struct ata_queued_cmd *qc)
932{ 934{
933 struct ata_port *ap = qc->ap; 935 struct ata_port *ap = qc->ap;
934 struct scatterlist *sg; 936 struct scatterlist *sg;
937 unsigned int si;
935 938
936 pr_debug("in atapi dma stop\n"); 939 pr_debug("in atapi dma stop\n");
937 if (!(ap->udma_mask || ap->mwdma_mask)) 940 if (!(ap->udma_mask || ap->mwdma_mask))
@@ -950,7 +953,7 @@ static void bfin_bmdma_stop(struct ata_queued_cmd *qc)
950 * data cache is enabled. Otherwise, this loop 953 * data cache is enabled. Otherwise, this loop
951 * is an empty loop and optimized out. 954 * is an empty loop and optimized out.
952 */ 955 */
953 ata_for_each_sg(sg, qc) { 956 for_each_sg(qc->sg, sg, qc->n_elem, si) {
954 invalidate_dcache_range( 957 invalidate_dcache_range(
955 sg_dma_address(sg), 958 sg_dma_address(sg),
956 sg_dma_address(sg) 959 sg_dma_address(sg)
@@ -1167,34 +1170,36 @@ static unsigned char bfin_bmdma_status(struct ata_port *ap)
1167 * Note: Original code is ata_data_xfer(). 1170 * Note: Original code is ata_data_xfer().
1168 */ 1171 */
1169 1172
1170static void bfin_data_xfer(struct ata_device *adev, unsigned char *buf, 1173static unsigned int bfin_data_xfer(struct ata_device *dev, unsigned char *buf,
1171 unsigned int buflen, int write_data) 1174 unsigned int buflen, int rw)
1172{ 1175{
1173 struct ata_port *ap = adev->link->ap; 1176 struct ata_port *ap = dev->link->ap;
1174 unsigned int words = buflen >> 1;
1175 unsigned short *buf16 = (u16 *) buf;
1176 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr; 1177 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
1178 unsigned int words = buflen >> 1;
1179 unsigned short *buf16 = (u16 *)buf;
1177 1180
1178 /* Transfer multiple of 2 bytes */ 1181 /* Transfer multiple of 2 bytes */
1179 if (write_data) { 1182 if (rw == READ)
1180 write_atapi_data(base, words, buf16);
1181 } else {
1182 read_atapi_data(base, words, buf16); 1183 read_atapi_data(base, words, buf16);
1183 } 1184 else
1185 write_atapi_data(base, words, buf16);
1184 1186
1185 /* Transfer trailing 1 byte, if any. */ 1187 /* Transfer trailing 1 byte, if any. */
1186 if (unlikely(buflen & 0x01)) { 1188 if (unlikely(buflen & 0x01)) {
1187 unsigned short align_buf[1] = { 0 }; 1189 unsigned short align_buf[1] = { 0 };
1188 unsigned char *trailing_buf = buf + buflen - 1; 1190 unsigned char *trailing_buf = buf + buflen - 1;
1189 1191
1190 if (write_data) { 1192 if (rw == READ) {
1191 memcpy(align_buf, trailing_buf, 1);
1192 write_atapi_data(base, 1, align_buf);
1193 } else {
1194 read_atapi_data(base, 1, align_buf); 1193 read_atapi_data(base, 1, align_buf);
1195 memcpy(trailing_buf, align_buf, 1); 1194 memcpy(trailing_buf, align_buf, 1);
1195 } else {
1196 memcpy(align_buf, trailing_buf, 1);
1197 write_atapi_data(base, 1, align_buf);
1196 } 1198 }
1199 words++;
1197 } 1200 }
1201
1202 return words << 1;
1198} 1203}
1199 1204
1200/** 1205/**
diff --git a/drivers/ata/pata_cs5520.c b/drivers/ata/pata_cs5520.c
index 33f7f0843f4f..d4590f546c49 100644
--- a/drivers/ata/pata_cs5520.c
+++ b/drivers/ata/pata_cs5520.c
@@ -198,7 +198,7 @@ static int __devinit cs5520_init_one(struct pci_dev *pdev, const struct pci_devi
198 }; 198 };
199 const struct ata_port_info *ppi[2]; 199 const struct ata_port_info *ppi[2];
200 u8 pcicfg; 200 u8 pcicfg;
201 void *iomap[5]; 201 void __iomem *iomap[5];
202 struct ata_host *host; 202 struct ata_host *host;
203 struct ata_ioports *ioaddr; 203 struct ata_ioports *ioaddr;
204 int i, rc; 204 int i, rc;
diff --git a/drivers/ata/pata_hpt37x.c b/drivers/ata/pata_hpt37x.c
index c79f066c2bc9..68eb34929cec 100644
--- a/drivers/ata/pata_hpt37x.c
+++ b/drivers/ata/pata_hpt37x.c
@@ -847,15 +847,16 @@ static u32 hpt374_read_freq(struct pci_dev *pdev)
847 u32 freq; 847 u32 freq;
848 unsigned long io_base = pci_resource_start(pdev, 4); 848 unsigned long io_base = pci_resource_start(pdev, 4);
849 if (PCI_FUNC(pdev->devfn) & 1) { 849 if (PCI_FUNC(pdev->devfn) & 1) {
850 struct pci_dev *pdev_0 = pci_get_slot(pdev->bus, pdev->devfn - 1); 850 struct pci_dev *pdev_0;
851
852 pdev_0 = pci_get_slot(pdev->bus, pdev->devfn - 1);
851 /* Someone hot plugged the controller on us ? */ 853 /* Someone hot plugged the controller on us ? */
852 if (pdev_0 == NULL) 854 if (pdev_0 == NULL)
853 return 0; 855 return 0;
854 io_base = pci_resource_start(pdev_0, 4); 856 io_base = pci_resource_start(pdev_0, 4);
855 freq = inl(io_base + 0x90); 857 freq = inl(io_base + 0x90);
856 pci_dev_put(pdev_0); 858 pci_dev_put(pdev_0);
857 } 859 } else
858 else
859 freq = inl(io_base + 0x90); 860 freq = inl(io_base + 0x90);
860 return freq; 861 return freq;
861} 862}
diff --git a/drivers/ata/pata_icside.c b/drivers/ata/pata_icside.c
index 842fe08a3c13..5b8586dac63b 100644
--- a/drivers/ata/pata_icside.c
+++ b/drivers/ata/pata_icside.c
@@ -224,6 +224,7 @@ static void pata_icside_bmdma_setup(struct ata_queued_cmd *qc)
224 struct pata_icside_state *state = ap->host->private_data; 224 struct pata_icside_state *state = ap->host->private_data;
225 struct scatterlist *sg, *rsg = state->sg; 225 struct scatterlist *sg, *rsg = state->sg;
226 unsigned int write = qc->tf.flags & ATA_TFLAG_WRITE; 226 unsigned int write = qc->tf.flags & ATA_TFLAG_WRITE;
227 unsigned int si;
227 228
228 /* 229 /*
229 * We are simplex; BUG if we try to fiddle with DMA 230 * We are simplex; BUG if we try to fiddle with DMA
@@ -234,7 +235,7 @@ static void pata_icside_bmdma_setup(struct ata_queued_cmd *qc)
234 /* 235 /*
235 * Copy ATAs scattered sg list into a contiguous array of sg 236 * Copy ATAs scattered sg list into a contiguous array of sg
236 */ 237 */
237 ata_for_each_sg(sg, qc) { 238 for_each_sg(qc->sg, sg, qc->n_elem, si) {
238 memcpy(rsg, sg, sizeof(*sg)); 239 memcpy(rsg, sg, sizeof(*sg));
239 rsg++; 240 rsg++;
240 } 241 }
diff --git a/drivers/ata/pata_it821x.c b/drivers/ata/pata_it821x.c
index ca9aae09daed..109ddd42c266 100644
--- a/drivers/ata/pata_it821x.c
+++ b/drivers/ata/pata_it821x.c
@@ -430,7 +430,7 @@ static unsigned int it821x_smart_qc_issue_prot(struct ata_queued_cmd *qc)
430 return ata_qc_issue_prot(qc); 430 return ata_qc_issue_prot(qc);
431 } 431 }
432 printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", qc->tf.command); 432 printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", qc->tf.command);
433 return AC_ERR_INVALID; 433 return AC_ERR_DEV;
434} 434}
435 435
436/** 436/**
@@ -516,6 +516,37 @@ static void it821x_dev_config(struct ata_device *adev)
516 printk("(%dK stripe)", adev->id[146]); 516 printk("(%dK stripe)", adev->id[146]);
517 printk(".\n"); 517 printk(".\n");
518 } 518 }
519 /* This is a controller firmware triggered funny, don't
520 report the drive faulty! */
521 adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
522}
523
524/**
525 * it821x_ident_hack - Hack identify data up
526 * @ap: Port
527 *
528 * Walk the devices on this firmware driven port and slightly
529 * mash the identify data to stop us and common tools trying to
530 * use features not firmware supported. The firmware itself does
531 * some masking (eg SMART) but not enough.
532 *
533 * This is a bit of an abuse of the cable method, but it is the
534 * only method called at the right time. We could modify the libata
535 * core specifically for ident hacking but while we have one offender
536 * it seems better to keep the fallout localised.
537 */
538
539static int it821x_ident_hack(struct ata_port *ap)
540{
541 struct ata_device *adev;
542 ata_link_for_each_dev(adev, &ap->link) {
543 if (ata_dev_enabled(adev)) {
544 adev->id[84] &= ~(1 << 6); /* No FUA */
545 adev->id[85] &= ~(1 << 10); /* No HPA */
546 adev->id[76] = 0; /* No NCQ/AN etc */
547 }
548 }
549 return ata_cable_unknown(ap);
519} 550}
520 551
521 552
@@ -634,7 +665,7 @@ static struct ata_port_operations it821x_smart_port_ops = {
634 .thaw = ata_bmdma_thaw, 665 .thaw = ata_bmdma_thaw,
635 .error_handler = ata_bmdma_error_handler, 666 .error_handler = ata_bmdma_error_handler,
636 .post_internal_cmd = ata_bmdma_post_internal_cmd, 667 .post_internal_cmd = ata_bmdma_post_internal_cmd,
637 .cable_detect = ata_cable_unknown, 668 .cable_detect = it821x_ident_hack,
638 669
639 .bmdma_setup = ata_bmdma_setup, 670 .bmdma_setup = ata_bmdma_setup,
640 .bmdma_start = ata_bmdma_start, 671 .bmdma_start = ata_bmdma_start,
diff --git a/drivers/ata/pata_ixp4xx_cf.c b/drivers/ata/pata_ixp4xx_cf.c
index 120b5bfa7ce6..030878fedeb5 100644
--- a/drivers/ata/pata_ixp4xx_cf.c
+++ b/drivers/ata/pata_ixp4xx_cf.c
@@ -42,13 +42,13 @@ static int ixp4xx_set_mode(struct ata_link *link, struct ata_device **error)
42 return 0; 42 return 0;
43} 43}
44 44
45static void ixp4xx_mmio_data_xfer(struct ata_device *adev, unsigned char *buf, 45static unsigned int ixp4xx_mmio_data_xfer(struct ata_device *dev,
46 unsigned int buflen, int write_data) 46 unsigned char *buf, unsigned int buflen, int rw)
47{ 47{
48 unsigned int i; 48 unsigned int i;
49 unsigned int words = buflen >> 1; 49 unsigned int words = buflen >> 1;
50 u16 *buf16 = (u16 *) buf; 50 u16 *buf16 = (u16 *) buf;
51 struct ata_port *ap = adev->link->ap; 51 struct ata_port *ap = dev->link->ap;
52 void __iomem *mmio = ap->ioaddr.data_addr; 52 void __iomem *mmio = ap->ioaddr.data_addr;
53 struct ixp4xx_pata_data *data = ap->host->dev->platform_data; 53 struct ixp4xx_pata_data *data = ap->host->dev->platform_data;
54 54
@@ -59,30 +59,32 @@ static void ixp4xx_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
59 udelay(100); 59 udelay(100);
60 60
61 /* Transfer multiple of 2 bytes */ 61 /* Transfer multiple of 2 bytes */
62 if (write_data) { 62 if (rw == READ)
63 for (i = 0; i < words; i++)
64 writew(buf16[i], mmio);
65 } else {
66 for (i = 0; i < words; i++) 63 for (i = 0; i < words; i++)
67 buf16[i] = readw(mmio); 64 buf16[i] = readw(mmio);
68 } 65 else
66 for (i = 0; i < words; i++)
67 writew(buf16[i], mmio);
69 68
70 /* Transfer trailing 1 byte, if any. */ 69 /* Transfer trailing 1 byte, if any. */
71 if (unlikely(buflen & 0x01)) { 70 if (unlikely(buflen & 0x01)) {
72 u16 align_buf[1] = { 0 }; 71 u16 align_buf[1] = { 0 };
73 unsigned char *trailing_buf = buf + buflen - 1; 72 unsigned char *trailing_buf = buf + buflen - 1;
74 73
75 if (write_data) { 74 if (rw == READ) {
76 memcpy(align_buf, trailing_buf, 1);
77 writew(align_buf[0], mmio);
78 } else {
79 align_buf[0] = readw(mmio); 75 align_buf[0] = readw(mmio);
80 memcpy(trailing_buf, align_buf, 1); 76 memcpy(trailing_buf, align_buf, 1);
77 } else {
78 memcpy(align_buf, trailing_buf, 1);
79 writew(align_buf[0], mmio);
81 } 80 }
81 words++;
82 } 82 }
83 83
84 udelay(100); 84 udelay(100);
85 *data->cs0_cfg |= 0x01; 85 *data->cs0_cfg |= 0x01;
86
87 return words << 1;
86} 88}
87 89
88static struct scsi_host_template ixp4xx_sht = { 90static struct scsi_host_template ixp4xx_sht = {
diff --git a/drivers/ata/pata_legacy.c b/drivers/ata/pata_legacy.c
index 17159b5e1e43..333dc15f8ccf 100644
--- a/drivers/ata/pata_legacy.c
+++ b/drivers/ata/pata_legacy.c
@@ -28,7 +28,6 @@
28 * 28 *
29 * Unsupported but docs exist: 29 * Unsupported but docs exist:
30 * Appian/Adaptec AIC25VL01/Cirrus Logic PD7220 30 * Appian/Adaptec AIC25VL01/Cirrus Logic PD7220
31 * Winbond W83759A
32 * 31 *
33 * This driver handles legacy (that is "ISA/VLB side") IDE ports found 32 * This driver handles legacy (that is "ISA/VLB side") IDE ports found
34 * on PC class systems. There are three hybrid devices that are exceptions 33 * on PC class systems. There are three hybrid devices that are exceptions
@@ -36,7 +35,7 @@
36 * the MPIIX where the tuning is PCI side but the IDE is "ISA side". 35 * the MPIIX where the tuning is PCI side but the IDE is "ISA side".
37 * 36 *
38 * Specific support is included for the ht6560a/ht6560b/opti82c611a/ 37 * Specific support is included for the ht6560a/ht6560b/opti82c611a/
39 * opti82c465mv/promise 20230c/20630 38 * opti82c465mv/promise 20230c/20630/winbond83759A
40 * 39 *
41 * Use the autospeed and pio_mask options with: 40 * Use the autospeed and pio_mask options with:
42 * Appian ADI/2 aka CLPD7220 or AIC25VL01. 41 * Appian ADI/2 aka CLPD7220 or AIC25VL01.
@@ -47,9 +46,6 @@
47 * For now use autospeed and pio_mask as above with the W83759A. This may 46 * For now use autospeed and pio_mask as above with the W83759A. This may
48 * change. 47 * change.
49 * 48 *
50 * TODO
51 * Merge existing pata_qdi driver
52 *
53 */ 49 */
54 50
55#include <linux/kernel.h> 51#include <linux/kernel.h>
@@ -64,12 +60,13 @@
64#include <linux/platform_device.h> 60#include <linux/platform_device.h>
65 61
66#define DRV_NAME "pata_legacy" 62#define DRV_NAME "pata_legacy"
67#define DRV_VERSION "0.5.5" 63#define DRV_VERSION "0.6.5"
68 64
69#define NR_HOST 6 65#define NR_HOST 6
70 66
71static int legacy_port[NR_HOST] = { 0x1f0, 0x170, 0x1e8, 0x168, 0x1e0, 0x160 }; 67static int all;
72static int legacy_irq[NR_HOST] = { 14, 15, 11, 10, 8, 12 }; 68module_param(all, int, 0444);
69MODULE_PARM_DESC(all, "Grab all legacy port devices, even if PCI(0=off, 1=on)");
73 70
74struct legacy_data { 71struct legacy_data {
75 unsigned long timing; 72 unsigned long timing;
@@ -80,21 +77,107 @@ struct legacy_data {
80 77
81}; 78};
82 79
80enum controller {
81 BIOS = 0,
82 SNOOP = 1,
83 PDC20230 = 2,
84 HT6560A = 3,
85 HT6560B = 4,
86 OPTI611A = 5,
87 OPTI46X = 6,
88 QDI6500 = 7,
89 QDI6580 = 8,
90 QDI6580DP = 9, /* Dual channel mode is different */
91 W83759A = 10,
92
93 UNKNOWN = -1
94};
95
96
97struct legacy_probe {
98 unsigned char *name;
99 unsigned long port;
100 unsigned int irq;
101 unsigned int slot;
102 enum controller type;
103 unsigned long private;
104};
105
106struct legacy_controller {
107 const char *name;
108 struct ata_port_operations *ops;
109 unsigned int pio_mask;
110 unsigned int flags;
111 int (*setup)(struct platform_device *, struct legacy_probe *probe,
112 struct legacy_data *data);
113};
114
115static int legacy_port[NR_HOST] = { 0x1f0, 0x170, 0x1e8, 0x168, 0x1e0, 0x160 };
116
117static struct legacy_probe probe_list[NR_HOST];
83static struct legacy_data legacy_data[NR_HOST]; 118static struct legacy_data legacy_data[NR_HOST];
84static struct ata_host *legacy_host[NR_HOST]; 119static struct ata_host *legacy_host[NR_HOST];
85static int nr_legacy_host; 120static int nr_legacy_host;
86 121
87 122
88static int probe_all; /* Set to check all ISA port ranges */ 123static int probe_all; /* Set to check all ISA port ranges */
89static int ht6560a; /* HT 6560A on primary 1, secondary 2, both 3 */ 124static int ht6560a; /* HT 6560A on primary 1, second 2, both 3 */
90static int ht6560b; /* HT 6560A on primary 1, secondary 2, both 3 */ 125static int ht6560b; /* HT 6560A on primary 1, second 2, both 3 */
91static int opti82c611a; /* Opti82c611A on primary 1, secondary 2, both 3 */ 126static int opti82c611a; /* Opti82c611A on primary 1, sec 2, both 3 */
92static int opti82c46x; /* Opti 82c465MV present (pri/sec autodetect) */ 127static int opti82c46x; /* Opti 82c465MV present(pri/sec autodetect) */
93static int autospeed; /* Chip present which snoops speed changes */ 128static int qdi; /* Set to probe QDI controllers */
94static int pio_mask = 0x1F; /* PIO range for autospeed devices */ 129static int winbond; /* Set to probe Winbond controllers,
130 give I/O port if non stdanard */
131static int autospeed; /* Chip present which snoops speed changes */
132static int pio_mask = 0x1F; /* PIO range for autospeed devices */
95static int iordy_mask = 0xFFFFFFFF; /* Use iordy if available */ 133static int iordy_mask = 0xFFFFFFFF; /* Use iordy if available */
96 134
97/** 135/**
136 * legacy_probe_add - Add interface to probe list
137 * @port: Controller port
138 * @irq: IRQ number
139 * @type: Controller type
140 * @private: Controller specific info
141 *
142 * Add an entry into the probe list for ATA controllers. This is used
143 * to add the default ISA slots and then to build up the table
144 * further according to other ISA/VLB/Weird device scans
145 *
146 * An I/O port list is used to keep ordering stable and sane, as we
147 * don't have any good way to talk about ordering otherwise
148 */
149
150static int legacy_probe_add(unsigned long port, unsigned int irq,
151 enum controller type, unsigned long private)
152{
153 struct legacy_probe *lp = &probe_list[0];
154 int i;
155 struct legacy_probe *free = NULL;
156
157 for (i = 0; i < NR_HOST; i++) {
158 if (lp->port == 0 && free == NULL)
159 free = lp;
160 /* Matching port, or the correct slot for ordering */
161 if (lp->port == port || legacy_port[i] == port) {
162 free = lp;
163 break;
164 }
165 lp++;
166 }
167 if (free == NULL) {
168 printk(KERN_ERR "pata_legacy: Too many interfaces.\n");
169 return -1;
170 }
171 /* Fill in the entry for later probing */
172 free->port = port;
173 free->irq = irq;
174 free->type = type;
175 free->private = private;
176 return 0;
177}
178
179
180/**
98 * legacy_set_mode - mode setting 181 * legacy_set_mode - mode setting
99 * @link: IDE link 182 * @link: IDE link
100 * @unused: Device that failed when error is returned 183 * @unused: Device that failed when error is returned
@@ -113,7 +196,8 @@ static int legacy_set_mode(struct ata_link *link, struct ata_device **unused)
113 196
114 ata_link_for_each_dev(dev, link) { 197 ata_link_for_each_dev(dev, link) {
115 if (ata_dev_enabled(dev)) { 198 if (ata_dev_enabled(dev)) {
116 ata_dev_printk(dev, KERN_INFO, "configured for PIO\n"); 199 ata_dev_printk(dev, KERN_INFO,
200 "configured for PIO\n");
117 dev->pio_mode = XFER_PIO_0; 201 dev->pio_mode = XFER_PIO_0;
118 dev->xfer_mode = XFER_PIO_0; 202 dev->xfer_mode = XFER_PIO_0;
119 dev->xfer_shift = ATA_SHIFT_PIO; 203 dev->xfer_shift = ATA_SHIFT_PIO;
@@ -171,7 +255,7 @@ static struct ata_port_operations simple_port_ops = {
171 .irq_clear = ata_bmdma_irq_clear, 255 .irq_clear = ata_bmdma_irq_clear,
172 .irq_on = ata_irq_on, 256 .irq_on = ata_irq_on,
173 257
174 .port_start = ata_port_start, 258 .port_start = ata_sff_port_start,
175}; 259};
176 260
177static struct ata_port_operations legacy_port_ops = { 261static struct ata_port_operations legacy_port_ops = {
@@ -198,15 +282,16 @@ static struct ata_port_operations legacy_port_ops = {
198 .irq_clear = ata_bmdma_irq_clear, 282 .irq_clear = ata_bmdma_irq_clear,
199 .irq_on = ata_irq_on, 283 .irq_on = ata_irq_on,
200 284
201 .port_start = ata_port_start, 285 .port_start = ata_sff_port_start,
202}; 286};
203 287
204/* 288/*
205 * Promise 20230C and 20620 support 289 * Promise 20230C and 20620 support
206 * 290 *
207 * This controller supports PIO0 to PIO2. We set PIO timings conservatively to 291 * This controller supports PIO0 to PIO2. We set PIO timings
208 * allow for 50MHz Vesa Local Bus. The 20620 DMA support is weird being DMA to 292 * conservatively to allow for 50MHz Vesa Local Bus. The 20620 DMA
209 * controller and PIO'd to the host and not supported. 293 * support is weird being DMA to controller and PIO'd to the host
294 * and not supported.
210 */ 295 */
211 296
212static void pdc20230_set_piomode(struct ata_port *ap, struct ata_device *adev) 297static void pdc20230_set_piomode(struct ata_port *ap, struct ata_device *adev)
@@ -221,8 +306,7 @@ static void pdc20230_set_piomode(struct ata_port *ap, struct ata_device *adev)
221 local_irq_save(flags); 306 local_irq_save(flags);
222 307
223 /* Unlock the control interface */ 308 /* Unlock the control interface */
224 do 309 do {
225 {
226 inb(0x1F5); 310 inb(0x1F5);
227 outb(inb(0x1F2) | 0x80, 0x1F2); 311 outb(inb(0x1F2) | 0x80, 0x1F2);
228 inb(0x1F2); 312 inb(0x1F2);
@@ -231,7 +315,7 @@ static void pdc20230_set_piomode(struct ata_port *ap, struct ata_device *adev)
231 inb(0x1F2); 315 inb(0x1F2);
232 inb(0x1F2); 316 inb(0x1F2);
233 } 317 }
234 while((inb(0x1F2) & 0x80) && --tries); 318 while ((inb(0x1F2) & 0x80) && --tries);
235 319
236 local_irq_restore(flags); 320 local_irq_restore(flags);
237 321
@@ -249,13 +333,14 @@ static void pdc20230_set_piomode(struct ata_port *ap, struct ata_device *adev)
249 333
250} 334}
251 335
252static void pdc_data_xfer_vlb(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data) 336static unsigned int pdc_data_xfer_vlb(struct ata_device *dev,
337 unsigned char *buf, unsigned int buflen, int rw)
253{ 338{
254 struct ata_port *ap = adev->link->ap; 339 if (ata_id_has_dword_io(dev->id)) {
255 int slop = buflen & 3; 340 struct ata_port *ap = dev->link->ap;
256 unsigned long flags; 341 int slop = buflen & 3;
342 unsigned long flags;
257 343
258 if (ata_id_has_dword_io(adev->id)) {
259 local_irq_save(flags); 344 local_irq_save(flags);
260 345
261 /* Perform the 32bit I/O synchronization sequence */ 346 /* Perform the 32bit I/O synchronization sequence */
@@ -264,26 +349,27 @@ static void pdc_data_xfer_vlb(struct ata_device *adev, unsigned char *buf, unsig
264 ioread8(ap->ioaddr.nsect_addr); 349 ioread8(ap->ioaddr.nsect_addr);
265 350
266 /* Now the data */ 351 /* Now the data */
267 352 if (rw == READ)
268 if (write_data)
269 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
270 else
271 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2); 353 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
354 else
355 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
272 356
273 if (unlikely(slop)) { 357 if (unlikely(slop)) {
274 __le32 pad = 0; 358 u32 pad;
275 if (write_data) { 359 if (rw == READ) {
276 memcpy(&pad, buf + buflen - slop, slop);
277 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
278 } else {
279 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr)); 360 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
280 memcpy(buf + buflen - slop, &pad, slop); 361 memcpy(buf + buflen - slop, &pad, slop);
362 } else {
363 memcpy(&pad, buf + buflen - slop, slop);
364 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
281 } 365 }
366 buflen += 4 - slop;
282 } 367 }
283 local_irq_restore(flags); 368 local_irq_restore(flags);
284 } 369 } else
285 else 370 buflen = ata_data_xfer_noirq(dev, buf, buflen, rw);
286 ata_data_xfer_noirq(adev, buf, buflen, write_data); 371
372 return buflen;
287} 373}
288 374
289static struct ata_port_operations pdc20230_port_ops = { 375static struct ata_port_operations pdc20230_port_ops = {
@@ -310,14 +396,14 @@ static struct ata_port_operations pdc20230_port_ops = {
310 .irq_clear = ata_bmdma_irq_clear, 396 .irq_clear = ata_bmdma_irq_clear,
311 .irq_on = ata_irq_on, 397 .irq_on = ata_irq_on,
312 398
313 .port_start = ata_port_start, 399 .port_start = ata_sff_port_start,
314}; 400};
315 401
316/* 402/*
317 * Holtek 6560A support 403 * Holtek 6560A support
318 * 404 *
319 * This controller supports PIO0 to PIO2 (no IORDY even though higher timings 405 * This controller supports PIO0 to PIO2 (no IORDY even though higher
320 * can be loaded). 406 * timings can be loaded).
321 */ 407 */
322 408
323static void ht6560a_set_piomode(struct ata_port *ap, struct ata_device *adev) 409static void ht6560a_set_piomode(struct ata_port *ap, struct ata_device *adev)
@@ -364,14 +450,14 @@ static struct ata_port_operations ht6560a_port_ops = {
364 .irq_clear = ata_bmdma_irq_clear, 450 .irq_clear = ata_bmdma_irq_clear,
365 .irq_on = ata_irq_on, 451 .irq_on = ata_irq_on,
366 452
367 .port_start = ata_port_start, 453 .port_start = ata_sff_port_start,
368}; 454};
369 455
370/* 456/*
371 * Holtek 6560B support 457 * Holtek 6560B support
372 * 458 *
373 * This controller supports PIO0 to PIO4. We honour the BIOS/jumper FIFO setting 459 * This controller supports PIO0 to PIO4. We honour the BIOS/jumper FIFO
374 * unless we see an ATAPI device in which case we force it off. 460 * setting unless we see an ATAPI device in which case we force it off.
375 * 461 *
376 * FIXME: need to implement 2nd channel support. 462 * FIXME: need to implement 2nd channel support.
377 */ 463 */
@@ -398,7 +484,7 @@ static void ht6560b_set_piomode(struct ata_port *ap, struct ata_device *adev)
398 if (adev->class != ATA_DEV_ATA) { 484 if (adev->class != ATA_DEV_ATA) {
399 u8 rconf = inb(0x3E6); 485 u8 rconf = inb(0x3E6);
400 if (rconf & 0x24) { 486 if (rconf & 0x24) {
401 rconf &= ~ 0x24; 487 rconf &= ~0x24;
402 outb(rconf, 0x3E6); 488 outb(rconf, 0x3E6);
403 } 489 }
404 } 490 }
@@ -423,13 +509,13 @@ static struct ata_port_operations ht6560b_port_ops = {
423 .qc_prep = ata_qc_prep, 509 .qc_prep = ata_qc_prep,
424 .qc_issue = ata_qc_issue_prot, 510 .qc_issue = ata_qc_issue_prot,
425 511
426 .data_xfer = ata_data_xfer, /* FIXME: Check 32bit and noirq */ 512 .data_xfer = ata_data_xfer, /* FIXME: Check 32bit and noirq */
427 513
428 .irq_handler = ata_interrupt, 514 .irq_handler = ata_interrupt,
429 .irq_clear = ata_bmdma_irq_clear, 515 .irq_clear = ata_bmdma_irq_clear,
430 .irq_on = ata_irq_on, 516 .irq_on = ata_irq_on,
431 517
432 .port_start = ata_port_start, 518 .port_start = ata_sff_port_start,
433}; 519};
434 520
435/* 521/*
@@ -462,7 +548,8 @@ static u8 opti_syscfg(u8 reg)
462 * This controller supports PIO0 to PIO3. 548 * This controller supports PIO0 to PIO3.
463 */ 549 */
464 550
465static void opti82c611a_set_piomode(struct ata_port *ap, struct ata_device *adev) 551static void opti82c611a_set_piomode(struct ata_port *ap,
552 struct ata_device *adev)
466{ 553{
467 u8 active, recover, setup; 554 u8 active, recover, setup;
468 struct ata_timing t; 555 struct ata_timing t;
@@ -549,7 +636,7 @@ static struct ata_port_operations opti82c611a_port_ops = {
549 .irq_clear = ata_bmdma_irq_clear, 636 .irq_clear = ata_bmdma_irq_clear,
550 .irq_on = ata_irq_on, 637 .irq_on = ata_irq_on,
551 638
552 .port_start = ata_port_start, 639 .port_start = ata_sff_port_start,
553}; 640};
554 641
555/* 642/*
@@ -681,77 +768,398 @@ static struct ata_port_operations opti82c46x_port_ops = {
681 .irq_clear = ata_bmdma_irq_clear, 768 .irq_clear = ata_bmdma_irq_clear,
682 .irq_on = ata_irq_on, 769 .irq_on = ata_irq_on,
683 770
684 .port_start = ata_port_start, 771 .port_start = ata_sff_port_start,
685}; 772};
686 773
774static void qdi6500_set_piomode(struct ata_port *ap, struct ata_device *adev)
775{
776 struct ata_timing t;
777 struct legacy_data *qdi = ap->host->private_data;
778 int active, recovery;
779 u8 timing;
780
781 /* Get the timing data in cycles */
782 ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
783
784 if (qdi->fast) {
785 active = 8 - FIT(t.active, 1, 8);
786 recovery = 18 - FIT(t.recover, 3, 18);
787 } else {
788 active = 9 - FIT(t.active, 2, 9);
789 recovery = 15 - FIT(t.recover, 0, 15);
790 }
791 timing = (recovery << 4) | active | 0x08;
792
793 qdi->clock[adev->devno] = timing;
794
795 outb(timing, qdi->timing);
796}
687 797
688/** 798/**
689 * legacy_init_one - attach a legacy interface 799 * qdi6580dp_set_piomode - PIO setup for dual channel
690 * @port: port number 800 * @ap: Port
691 * @io: I/O port start 801 * @adev: Device
692 * @ctrl: control port
693 * @irq: interrupt line 802 * @irq: interrupt line
694 * 803 *
695 * Register an ISA bus IDE interface. Such interfaces are PIO and we 804 * In dual channel mode the 6580 has one clock per channel and we have
696 * assume do not support IRQ sharing. 805 * to software clockswitch in qc_issue_prot.
697 */ 806 */
698 807
699static __init int legacy_init_one(int port, unsigned long io, unsigned long ctrl, int irq) 808static void qdi6580dp_set_piomode(struct ata_port *ap, struct ata_device *adev)
700{ 809{
701 struct legacy_data *ld = &legacy_data[nr_legacy_host]; 810 struct ata_timing t;
702 struct ata_host *host; 811 struct legacy_data *qdi = ap->host->private_data;
703 struct ata_port *ap; 812 int active, recovery;
704 struct platform_device *pdev; 813 u8 timing;
705 struct ata_port_operations *ops = &legacy_port_ops;
706 void __iomem *io_addr, *ctrl_addr;
707 int pio_modes = pio_mask;
708 u32 mask = (1 << port);
709 u32 iordy = (iordy_mask & mask) ? 0: ATA_FLAG_NO_IORDY;
710 int ret;
711 814
712 pdev = platform_device_register_simple(DRV_NAME, nr_legacy_host, NULL, 0); 815 /* Get the timing data in cycles */
713 if (IS_ERR(pdev)) 816 ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
714 return PTR_ERR(pdev); 817
818 if (qdi->fast) {
819 active = 8 - FIT(t.active, 1, 8);
820 recovery = 18 - FIT(t.recover, 3, 18);
821 } else {
822 active = 9 - FIT(t.active, 2, 9);
823 recovery = 15 - FIT(t.recover, 0, 15);
824 }
825 timing = (recovery << 4) | active | 0x08;
715 826
716 ret = -EBUSY; 827 qdi->clock[adev->devno] = timing;
717 if (devm_request_region(&pdev->dev, io, 8, "pata_legacy") == NULL ||
718 devm_request_region(&pdev->dev, ctrl, 1, "pata_legacy") == NULL)
719 goto fail;
720 828
721 ret = -ENOMEM; 829 outb(timing, qdi->timing + 2 * ap->port_no);
722 io_addr = devm_ioport_map(&pdev->dev, io, 8); 830 /* Clear the FIFO */
723 ctrl_addr = devm_ioport_map(&pdev->dev, ctrl, 1); 831 if (adev->class != ATA_DEV_ATA)
724 if (!io_addr || !ctrl_addr) 832 outb(0x5F, qdi->timing + 3);
725 goto fail; 833}
726 834
727 if (ht6560a & mask) { 835/**
728 ops = &ht6560a_port_ops; 836 * qdi6580_set_piomode - PIO setup for single channel
729 pio_modes = 0x07; 837 * @ap: Port
730 iordy = ATA_FLAG_NO_IORDY; 838 * @adev: Device
731 } 839 *
732 if (ht6560b & mask) { 840 * In single channel mode the 6580 has one clock per device and we can
733 ops = &ht6560b_port_ops; 841 * avoid the requirement to clock switch. We also have to load the timing
734 pio_modes = 0x1F; 842 * into the right clock according to whether we are master or slave.
735 } 843 */
736 if (opti82c611a & mask) { 844
737 ops = &opti82c611a_port_ops; 845static void qdi6580_set_piomode(struct ata_port *ap, struct ata_device *adev)
738 pio_modes = 0x0F; 846{
847 struct ata_timing t;
848 struct legacy_data *qdi = ap->host->private_data;
849 int active, recovery;
850 u8 timing;
851
852 /* Get the timing data in cycles */
853 ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
854
855 if (qdi->fast) {
856 active = 8 - FIT(t.active, 1, 8);
857 recovery = 18 - FIT(t.recover, 3, 18);
858 } else {
859 active = 9 - FIT(t.active, 2, 9);
860 recovery = 15 - FIT(t.recover, 0, 15);
739 } 861 }
740 if (opti82c46x & mask) { 862 timing = (recovery << 4) | active | 0x08;
741 ops = &opti82c46x_port_ops; 863 qdi->clock[adev->devno] = timing;
742 pio_modes = 0x0F; 864 outb(timing, qdi->timing + 2 * adev->devno);
865 /* Clear the FIFO */
866 if (adev->class != ATA_DEV_ATA)
867 outb(0x5F, qdi->timing + 3);
868}
869
870/**
871 * qdi_qc_issue_prot - command issue
872 * @qc: command pending
873 *
874 * Called when the libata layer is about to issue a command. We wrap
875 * this interface so that we can load the correct ATA timings.
876 */
877
878static unsigned int qdi_qc_issue_prot(struct ata_queued_cmd *qc)
879{
880 struct ata_port *ap = qc->ap;
881 struct ata_device *adev = qc->dev;
882 struct legacy_data *qdi = ap->host->private_data;
883
884 if (qdi->clock[adev->devno] != qdi->last) {
885 if (adev->pio_mode) {
886 qdi->last = qdi->clock[adev->devno];
887 outb(qdi->clock[adev->devno], qdi->timing +
888 2 * ap->port_no);
889 }
743 } 890 }
891 return ata_qc_issue_prot(qc);
892}
744 893
745 /* Probe for automatically detectable controllers */ 894static unsigned int vlb32_data_xfer(struct ata_device *adev, unsigned char *buf,
895 unsigned int buflen, int rw)
896{
897 struct ata_port *ap = adev->link->ap;
898 int slop = buflen & 3;
746 899
747 if (io == 0x1F0 && ops == &legacy_port_ops) { 900 if (ata_id_has_dword_io(adev->id)) {
748 unsigned long flags; 901 if (rw == WRITE)
902 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
903 else
904 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
749 905
750 local_irq_save(flags); 906 if (unlikely(slop)) {
907 u32 pad;
908 if (rw == WRITE) {
909 memcpy(&pad, buf + buflen - slop, slop);
910 pad = le32_to_cpu(pad);
911 iowrite32(pad, ap->ioaddr.data_addr);
912 } else {
913 pad = ioread32(ap->ioaddr.data_addr);
914 pad = cpu_to_le32(pad);
915 memcpy(buf + buflen - slop, &pad, slop);
916 }
917 }
918 return (buflen + 3) & ~3;
919 } else
920 return ata_data_xfer(adev, buf, buflen, rw);
921}
922
923static int qdi_port(struct platform_device *dev,
924 struct legacy_probe *lp, struct legacy_data *ld)
925{
926 if (devm_request_region(&dev->dev, lp->private, 4, "qdi") == NULL)
927 return -EBUSY;
928 ld->timing = lp->private;
929 return 0;
930}
931
932static struct ata_port_operations qdi6500_port_ops = {
933 .set_piomode = qdi6500_set_piomode,
934
935 .tf_load = ata_tf_load,
936 .tf_read = ata_tf_read,
937 .check_status = ata_check_status,
938 .exec_command = ata_exec_command,
939 .dev_select = ata_std_dev_select,
940
941 .freeze = ata_bmdma_freeze,
942 .thaw = ata_bmdma_thaw,
943 .error_handler = ata_bmdma_error_handler,
944 .post_internal_cmd = ata_bmdma_post_internal_cmd,
945 .cable_detect = ata_cable_40wire,
946
947 .qc_prep = ata_qc_prep,
948 .qc_issue = qdi_qc_issue_prot,
949
950 .data_xfer = vlb32_data_xfer,
951
952 .irq_handler = ata_interrupt,
953 .irq_clear = ata_bmdma_irq_clear,
954 .irq_on = ata_irq_on,
955
956 .port_start = ata_sff_port_start,
957};
958
959static struct ata_port_operations qdi6580_port_ops = {
960 .set_piomode = qdi6580_set_piomode,
961
962 .tf_load = ata_tf_load,
963 .tf_read = ata_tf_read,
964 .check_status = ata_check_status,
965 .exec_command = ata_exec_command,
966 .dev_select = ata_std_dev_select,
967
968 .freeze = ata_bmdma_freeze,
969 .thaw = ata_bmdma_thaw,
970 .error_handler = ata_bmdma_error_handler,
971 .post_internal_cmd = ata_bmdma_post_internal_cmd,
972 .cable_detect = ata_cable_40wire,
973
974 .qc_prep = ata_qc_prep,
975 .qc_issue = ata_qc_issue_prot,
976
977 .data_xfer = vlb32_data_xfer,
978
979 .irq_handler = ata_interrupt,
980 .irq_clear = ata_bmdma_irq_clear,
981 .irq_on = ata_irq_on,
982
983 .port_start = ata_sff_port_start,
984};
985
986static struct ata_port_operations qdi6580dp_port_ops = {
987 .set_piomode = qdi6580dp_set_piomode,
988
989 .tf_load = ata_tf_load,
990 .tf_read = ata_tf_read,
991 .check_status = ata_check_status,
992 .exec_command = ata_exec_command,
993 .dev_select = ata_std_dev_select,
994
995 .freeze = ata_bmdma_freeze,
996 .thaw = ata_bmdma_thaw,
997 .error_handler = ata_bmdma_error_handler,
998 .post_internal_cmd = ata_bmdma_post_internal_cmd,
999 .cable_detect = ata_cable_40wire,
1000
1001 .qc_prep = ata_qc_prep,
1002 .qc_issue = qdi_qc_issue_prot,
1003
1004 .data_xfer = vlb32_data_xfer,
1005
1006 .irq_handler = ata_interrupt,
1007 .irq_clear = ata_bmdma_irq_clear,
1008 .irq_on = ata_irq_on,
1009
1010 .port_start = ata_sff_port_start,
1011};
1012
1013static DEFINE_SPINLOCK(winbond_lock);
1014
1015static void winbond_writecfg(unsigned long port, u8 reg, u8 val)
1016{
1017 unsigned long flags;
1018 spin_lock_irqsave(&winbond_lock, flags);
1019 outb(reg, port + 0x01);
1020 outb(val, port + 0x02);
1021 spin_unlock_irqrestore(&winbond_lock, flags);
1022}
1023
1024static u8 winbond_readcfg(unsigned long port, u8 reg)
1025{
1026 u8 val;
1027
1028 unsigned long flags;
1029 spin_lock_irqsave(&winbond_lock, flags);
1030 outb(reg, port + 0x01);
1031 val = inb(port + 0x02);
1032 spin_unlock_irqrestore(&winbond_lock, flags);
1033
1034 return val;
1035}
1036
1037static void winbond_set_piomode(struct ata_port *ap, struct ata_device *adev)
1038{
1039 struct ata_timing t;
1040 struct legacy_data *winbond = ap->host->private_data;
1041 int active, recovery;
1042 u8 reg;
1043 int timing = 0x88 + (ap->port_no * 4) + (adev->devno * 2);
1044
1045 reg = winbond_readcfg(winbond->timing, 0x81);
1046
1047 /* Get the timing data in cycles */
1048 if (reg & 0x40) /* Fast VLB bus, assume 50MHz */
1049 ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
1050 else
1051 ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
1052
1053 active = (FIT(t.active, 3, 17) - 1) & 0x0F;
1054 recovery = (FIT(t.recover, 1, 15) + 1) & 0x0F;
1055 timing = (active << 4) | recovery;
1056 winbond_writecfg(winbond->timing, timing, reg);
1057
1058 /* Load the setup timing */
1059
1060 reg = 0x35;
1061 if (adev->class != ATA_DEV_ATA)
1062 reg |= 0x08; /* FIFO off */
1063 if (!ata_pio_need_iordy(adev))
1064 reg |= 0x02; /* IORDY off */
1065 reg |= (FIT(t.setup, 0, 3) << 6);
1066 winbond_writecfg(winbond->timing, timing + 1, reg);
1067}
1068
1069static int winbond_port(struct platform_device *dev,
1070 struct legacy_probe *lp, struct legacy_data *ld)
1071{
1072 if (devm_request_region(&dev->dev, lp->private, 4, "winbond") == NULL)
1073 return -EBUSY;
1074 ld->timing = lp->private;
1075 return 0;
1076}
1077
1078static struct ata_port_operations winbond_port_ops = {
1079 .set_piomode = winbond_set_piomode,
1080
1081 .tf_load = ata_tf_load,
1082 .tf_read = ata_tf_read,
1083 .check_status = ata_check_status,
1084 .exec_command = ata_exec_command,
1085 .dev_select = ata_std_dev_select,
1086
1087 .freeze = ata_bmdma_freeze,
1088 .thaw = ata_bmdma_thaw,
1089 .error_handler = ata_bmdma_error_handler,
1090 .post_internal_cmd = ata_bmdma_post_internal_cmd,
1091 .cable_detect = ata_cable_40wire,
1092
1093 .qc_prep = ata_qc_prep,
1094 .qc_issue = ata_qc_issue_prot,
1095
1096 .data_xfer = vlb32_data_xfer,
1097
1098 .irq_clear = ata_bmdma_irq_clear,
1099 .irq_on = ata_irq_on,
751 1100
1101 .port_start = ata_sff_port_start,
1102};
1103
1104static struct legacy_controller controllers[] = {
1105 {"BIOS", &legacy_port_ops, 0x1F,
1106 ATA_FLAG_NO_IORDY, NULL },
1107 {"Snooping", &simple_port_ops, 0x1F,
1108 0 , NULL },
1109 {"PDC20230", &pdc20230_port_ops, 0x7,
1110 ATA_FLAG_NO_IORDY, NULL },
1111 {"HT6560A", &ht6560a_port_ops, 0x07,
1112 ATA_FLAG_NO_IORDY, NULL },
1113 {"HT6560B", &ht6560b_port_ops, 0x1F,
1114 ATA_FLAG_NO_IORDY, NULL },
1115 {"OPTI82C611A", &opti82c611a_port_ops, 0x0F,
1116 0 , NULL },
1117 {"OPTI82C46X", &opti82c46x_port_ops, 0x0F,
1118 0 , NULL },
1119 {"QDI6500", &qdi6500_port_ops, 0x07,
1120 ATA_FLAG_NO_IORDY, qdi_port },
1121 {"QDI6580", &qdi6580_port_ops, 0x1F,
1122 0 , qdi_port },
1123 {"QDI6580DP", &qdi6580dp_port_ops, 0x1F,
1124 0 , qdi_port },
1125 {"W83759A", &winbond_port_ops, 0x1F,
1126 0 , winbond_port }
1127};
1128
1129/**
1130 * probe_chip_type - Discover controller
1131 * @probe: Probe entry to check
1132 *
1133 * Probe an ATA port and identify the type of controller. We don't
1134 * check if the controller appears to be driveless at this point.
1135 */
1136
1137static __init int probe_chip_type(struct legacy_probe *probe)
1138{
1139 int mask = 1 << probe->slot;
1140
1141 if (winbond && (probe->port == 0x1F0 || probe->port == 0x170)) {
1142 u8 reg = winbond_readcfg(winbond, 0x81);
1143 reg |= 0x80; /* jumpered mode off */
1144 winbond_writecfg(winbond, 0x81, reg);
1145 reg = winbond_readcfg(winbond, 0x83);
1146 reg |= 0xF0; /* local control */
1147 winbond_writecfg(winbond, 0x83, reg);
1148 reg = winbond_readcfg(winbond, 0x85);
1149 reg |= 0xF0; /* programmable timing */
1150 winbond_writecfg(winbond, 0x85, reg);
1151
1152 reg = winbond_readcfg(winbond, 0x81);
1153
1154 if (reg & mask)
1155 return W83759A;
1156 }
1157 if (probe->port == 0x1F0) {
1158 unsigned long flags;
1159 local_irq_save(flags);
752 /* Probes */ 1160 /* Probes */
753 inb(0x1F5);
754 outb(inb(0x1F2) | 0x80, 0x1F2); 1161 outb(inb(0x1F2) | 0x80, 0x1F2);
1162 inb(0x1F5);
755 inb(0x1F2); 1163 inb(0x1F2);
756 inb(0x3F6); 1164 inb(0x3F6);
757 inb(0x3F6); 1165 inb(0x3F6);
@@ -760,29 +1168,83 @@ static __init int legacy_init_one(int port, unsigned long io, unsigned long ctrl
760 1168
761 if ((inb(0x1F2) & 0x80) == 0) { 1169 if ((inb(0x1F2) & 0x80) == 0) {
762 /* PDC20230c or 20630 ? */ 1170 /* PDC20230c or 20630 ? */
763 printk(KERN_INFO "PDC20230-C/20630 VLB ATA controller detected.\n"); 1171 printk(KERN_INFO "PDC20230-C/20630 VLB ATA controller"
764 pio_modes = 0x07; 1172 " detected.\n");
765 ops = &pdc20230_port_ops;
766 iordy = ATA_FLAG_NO_IORDY;
767 udelay(100); 1173 udelay(100);
768 inb(0x1F5); 1174 inb(0x1F5);
1175 local_irq_restore(flags);
1176 return PDC20230;
769 } else { 1177 } else {
770 outb(0x55, 0x1F2); 1178 outb(0x55, 0x1F2);
771 inb(0x1F2); 1179 inb(0x1F2);
772 inb(0x1F2); 1180 inb(0x1F2);
773 if (inb(0x1F2) == 0x00) { 1181 if (inb(0x1F2) == 0x00)
774 printk(KERN_INFO "PDC20230-B VLB ATA controller detected.\n"); 1182 printk(KERN_INFO "PDC20230-B VLB ATA "
775 } 1183 "controller detected.\n");
1184 local_irq_restore(flags);
1185 return BIOS;
776 } 1186 }
777 local_irq_restore(flags); 1187 local_irq_restore(flags);
778 } 1188 }
779 1189
1190 if (ht6560a & mask)
1191 return HT6560A;
1192 if (ht6560b & mask)
1193 return HT6560B;
1194 if (opti82c611a & mask)
1195 return OPTI611A;
1196 if (opti82c46x & mask)
1197 return OPTI46X;
1198 if (autospeed & mask)
1199 return SNOOP;
1200 return BIOS;
1201}
1202
1203
1204/**
1205 * legacy_init_one - attach a legacy interface
1206 * @pl: probe record
1207 *
1208 * Register an ISA bus IDE interface. Such interfaces are PIO and we
1209 * assume do not support IRQ sharing.
1210 */
1211
1212static __init int legacy_init_one(struct legacy_probe *probe)
1213{
1214 struct legacy_controller *controller = &controllers[probe->type];
1215 int pio_modes = controller->pio_mask;
1216 unsigned long io = probe->port;
1217 u32 mask = (1 << probe->slot);
1218 struct ata_port_operations *ops = controller->ops;
1219 struct legacy_data *ld = &legacy_data[probe->slot];
1220 struct ata_host *host = NULL;
1221 struct ata_port *ap;
1222 struct platform_device *pdev;
1223 struct ata_device *dev;
1224 void __iomem *io_addr, *ctrl_addr;
1225 u32 iordy = (iordy_mask & mask) ? 0: ATA_FLAG_NO_IORDY;
1226 int ret;
780 1227
781 /* Chip does mode setting by command snooping */ 1228 iordy |= controller->flags;
782 if (ops == &legacy_port_ops && (autospeed & mask)) 1229
783 ops = &simple_port_ops; 1230 pdev = platform_device_register_simple(DRV_NAME, probe->slot, NULL, 0);
1231 if (IS_ERR(pdev))
1232 return PTR_ERR(pdev);
1233
1234 ret = -EBUSY;
1235 if (devm_request_region(&pdev->dev, io, 8, "pata_legacy") == NULL ||
1236 devm_request_region(&pdev->dev, io + 0x0206, 1,
1237 "pata_legacy") == NULL)
1238 goto fail;
784 1239
785 ret = -ENOMEM; 1240 ret = -ENOMEM;
1241 io_addr = devm_ioport_map(&pdev->dev, io, 8);
1242 ctrl_addr = devm_ioport_map(&pdev->dev, io + 0x0206, 1);
1243 if (!io_addr || !ctrl_addr)
1244 goto fail;
1245 if (controller->setup)
1246 if (controller->setup(pdev, probe, ld) < 0)
1247 goto fail;
786 host = ata_host_alloc(&pdev->dev, 1); 1248 host = ata_host_alloc(&pdev->dev, 1);
787 if (!host) 1249 if (!host)
788 goto fail; 1250 goto fail;
@@ -795,19 +1257,29 @@ static __init int legacy_init_one(int port, unsigned long io, unsigned long ctrl
795 ap->ioaddr.altstatus_addr = ctrl_addr; 1257 ap->ioaddr.altstatus_addr = ctrl_addr;
796 ap->ioaddr.ctl_addr = ctrl_addr; 1258 ap->ioaddr.ctl_addr = ctrl_addr;
797 ata_std_ports(&ap->ioaddr); 1259 ata_std_ports(&ap->ioaddr);
798 ap->private_data = ld; 1260 ap->host->private_data = ld;
799 1261
800 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", io, ctrl); 1262 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", io, io + 0x0206);
801 1263
802 ret = ata_host_activate(host, irq, ata_interrupt, 0, &legacy_sht); 1264 ret = ata_host_activate(host, probe->irq, ata_interrupt, 0,
1265 &legacy_sht);
803 if (ret) 1266 if (ret)
804 goto fail; 1267 goto fail;
805
806 legacy_host[nr_legacy_host++] = dev_get_drvdata(&pdev->dev);
807 ld->platform_dev = pdev; 1268 ld->platform_dev = pdev;
808 return 0;
809 1269
1270 /* Nothing found means we drop the port as its probably not there */
1271
1272 ret = -ENODEV;
1273 ata_link_for_each_dev(dev, &ap->link) {
1274 if (!ata_dev_absent(dev)) {
1275 legacy_host[probe->slot] = host;
1276 ld->platform_dev = pdev;
1277 return 0;
1278 }
1279 }
810fail: 1280fail:
1281 if (host)
1282 ata_host_detach(host);
811 platform_device_unregister(pdev); 1283 platform_device_unregister(pdev);
812 return ret; 1284 return ret;
813} 1285}
@@ -818,13 +1290,15 @@ fail:
818 * @master: set this if we find an ATA master 1290 * @master: set this if we find an ATA master
819 * @master: set this if we find an ATA secondary 1291 * @master: set this if we find an ATA secondary
820 * 1292 *
821 * A small number of vendors implemented early PCI ATA interfaces on bridge logic 1293 * A small number of vendors implemented early PCI ATA interfaces
822 * without the ATA interface being PCI visible. Where we have a matching PCI driver 1294 * on bridge logic without the ATA interface being PCI visible.
823 * we must skip the relevant device here. If we don't know about it then the legacy 1295 * Where we have a matching PCI driver we must skip the relevant
824 * driver is the right driver anyway. 1296 * device here. If we don't know about it then the legacy driver
1297 * is the right driver anyway.
825 */ 1298 */
826 1299
827static void legacy_check_special_cases(struct pci_dev *p, int *primary, int *secondary) 1300static void __init legacy_check_special_cases(struct pci_dev *p, int *primary,
1301 int *secondary)
828{ 1302{
829 /* Cyrix CS5510 pre SFF MWDMA ATA on the bridge */ 1303 /* Cyrix CS5510 pre SFF MWDMA ATA on the bridge */
830 if (p->vendor == 0x1078 && p->device == 0x0000) { 1304 if (p->vendor == 0x1078 && p->device == 0x0000) {
@@ -840,7 +1314,8 @@ static void legacy_check_special_cases(struct pci_dev *p, int *primary, int *sec
840 if (p->vendor == 0x8086 && p->device == 0x1234) { 1314 if (p->vendor == 0x8086 && p->device == 0x1234) {
841 u16 r; 1315 u16 r;
842 pci_read_config_word(p, 0x6C, &r); 1316 pci_read_config_word(p, 0x6C, &r);
843 if (r & 0x8000) { /* ATA port enabled */ 1317 if (r & 0x8000) {
1318 /* ATA port enabled */
844 if (r & 0x4000) 1319 if (r & 0x4000)
845 *secondary = 1; 1320 *secondary = 1;
846 else 1321 else
@@ -850,6 +1325,114 @@ static void legacy_check_special_cases(struct pci_dev *p, int *primary, int *sec
850 } 1325 }
851} 1326}
852 1327
1328static __init void probe_opti_vlb(void)
1329{
1330 /* If an OPTI 82C46X is present find out where the channels are */
1331 static const char *optis[4] = {
1332 "3/463MV", "5MV",
1333 "5MVA", "5MVB"
1334 };
1335 u8 chans = 1;
1336 u8 ctrl = (opti_syscfg(0x30) & 0xC0) >> 6;
1337
1338 opti82c46x = 3; /* Assume master and slave first */
1339 printk(KERN_INFO DRV_NAME ": Opti 82C46%s chipset support.\n",
1340 optis[ctrl]);
1341 if (ctrl == 3)
1342 chans = (opti_syscfg(0x3F) & 0x20) ? 2 : 1;
1343 ctrl = opti_syscfg(0xAC);
1344 /* Check enabled and this port is the 465MV port. On the
1345 MVB we may have two channels */
1346 if (ctrl & 8) {
1347 if (chans == 2) {
1348 legacy_probe_add(0x1F0, 14, OPTI46X, 0);
1349 legacy_probe_add(0x170, 15, OPTI46X, 0);
1350 }
1351 if (ctrl & 4)
1352 legacy_probe_add(0x170, 15, OPTI46X, 0);
1353 else
1354 legacy_probe_add(0x1F0, 14, OPTI46X, 0);
1355 } else
1356 legacy_probe_add(0x1F0, 14, OPTI46X, 0);
1357}
1358
1359static __init void qdi65_identify_port(u8 r, u8 res, unsigned long port)
1360{
1361 static const unsigned long ide_port[2] = { 0x170, 0x1F0 };
1362 /* Check card type */
1363 if ((r & 0xF0) == 0xC0) {
1364 /* QD6500: single channel */
1365 if (r & 8)
1366 /* Disabled ? */
1367 return;
1368 legacy_probe_add(ide_port[r & 0x01], 14 + (r & 0x01),
1369 QDI6500, port);
1370 }
1371 if (((r & 0xF0) == 0xA0) || (r & 0xF0) == 0x50) {
1372 /* QD6580: dual channel */
1373 if (!request_region(port + 2 , 2, "pata_qdi")) {
1374 release_region(port, 2);
1375 return;
1376 }
1377 res = inb(port + 3);
1378 /* Single channel mode ? */
1379 if (res & 1)
1380 legacy_probe_add(ide_port[r & 0x01], 14 + (r & 0x01),
1381 QDI6580, port);
1382 else { /* Dual channel mode */
1383 legacy_probe_add(0x1F0, 14, QDI6580DP, port);
1384 /* port + 0x02, r & 0x04 */
1385 legacy_probe_add(0x170, 15, QDI6580DP, port + 2);
1386 }
1387 release_region(port + 2, 2);
1388 }
1389}
1390
1391static __init void probe_qdi_vlb(void)
1392{
1393 unsigned long flags;
1394 static const unsigned long qd_port[2] = { 0x30, 0xB0 };
1395 int i;
1396
1397 /*
1398 * Check each possible QD65xx base address
1399 */
1400
1401 for (i = 0; i < 2; i++) {
1402 unsigned long port = qd_port[i];
1403 u8 r, res;
1404
1405
1406 if (request_region(port, 2, "pata_qdi")) {
1407 /* Check for a card */
1408 local_irq_save(flags);
1409 /* I have no h/w that needs this delay but it
1410 is present in the historic code */
1411 r = inb(port);
1412 udelay(1);
1413 outb(0x19, port);
1414 udelay(1);
1415 res = inb(port);
1416 udelay(1);
1417 outb(r, port);
1418 udelay(1);
1419 local_irq_restore(flags);
1420
1421 /* Fail */
1422 if (res == 0x19) {
1423 release_region(port, 2);
1424 continue;
1425 }
1426 /* Passes the presence test */
1427 r = inb(port + 1);
1428 udelay(1);
1429 /* Check port agrees with port set */
1430 if ((r & 2) >> 1 == i)
1431 qdi65_identify_port(r, res, port);
1432 release_region(port, 2);
1433 }
1434 }
1435}
853 1436
854/** 1437/**
855 * legacy_init - attach legacy interfaces 1438 * legacy_init - attach legacy interfaces
@@ -867,15 +1450,17 @@ static __init int legacy_init(void)
867 int ct = 0; 1450 int ct = 0;
868 int primary = 0; 1451 int primary = 0;
869 int secondary = 0; 1452 int secondary = 0;
870 int last_port = NR_HOST; 1453 int pci_present = 0;
1454 struct legacy_probe *pl = &probe_list[0];
1455 int slot = 0;
871 1456
872 struct pci_dev *p = NULL; 1457 struct pci_dev *p = NULL;
873 1458
874 for_each_pci_dev(p) { 1459 for_each_pci_dev(p) {
875 int r; 1460 int r;
876 /* Check for any overlap of the system ATA mappings. Native mode controllers 1461 /* Check for any overlap of the system ATA mappings. Native
877 stuck on these addresses or some devices in 'raid' mode won't be found by 1462 mode controllers stuck on these addresses or some devices
878 the storage class test */ 1463 in 'raid' mode won't be found by the storage class test */
879 for (r = 0; r < 6; r++) { 1464 for (r = 0; r < 6; r++) {
880 if (pci_resource_start(p, r) == 0x1f0) 1465 if (pci_resource_start(p, r) == 0x1f0)
881 primary = 1; 1466 primary = 1;
@@ -885,49 +1470,39 @@ static __init int legacy_init(void)
885 /* Check for special cases */ 1470 /* Check for special cases */
886 legacy_check_special_cases(p, &primary, &secondary); 1471 legacy_check_special_cases(p, &primary, &secondary);
887 1472
888 /* If PCI bus is present then don't probe for tertiary legacy ports */ 1473 /* If PCI bus is present then don't probe for tertiary
889 if (probe_all == 0) 1474 legacy ports */
890 last_port = 2; 1475 pci_present = 1;
891 } 1476 }
892 1477
893 /* If an OPTI 82C46X is present find out where the channels are */ 1478 if (winbond == 1)
894 if (opti82c46x) { 1479 winbond = 0x130; /* Default port, alt is 1B0 */
895 static const char *optis[4] = { 1480
896 "3/463MV", "5MV", 1481 if (primary == 0 || all)
897 "5MVA", "5MVB" 1482 legacy_probe_add(0x1F0, 14, UNKNOWN, 0);
898 }; 1483 if (secondary == 0 || all)
899 u8 chans = 1; 1484 legacy_probe_add(0x170, 15, UNKNOWN, 0);
900 u8 ctrl = (opti_syscfg(0x30) & 0xC0) >> 6; 1485
901 1486 if (probe_all || !pci_present) {
902 opti82c46x = 3; /* Assume master and slave first */ 1487 /* ISA/VLB extra ports */
903 printk(KERN_INFO DRV_NAME ": Opti 82C46%s chipset support.\n", optis[ctrl]); 1488 legacy_probe_add(0x1E8, 11, UNKNOWN, 0);
904 if (ctrl == 3) 1489 legacy_probe_add(0x168, 10, UNKNOWN, 0);
905 chans = (opti_syscfg(0x3F) & 0x20) ? 2 : 1; 1490 legacy_probe_add(0x1E0, 8, UNKNOWN, 0);
906 ctrl = opti_syscfg(0xAC); 1491 legacy_probe_add(0x160, 12, UNKNOWN, 0);
907 /* Check enabled and this port is the 465MV port. On the
908 MVB we may have two channels */
909 if (ctrl & 8) {
910 if (ctrl & 4)
911 opti82c46x = 2; /* Slave */
912 else
913 opti82c46x = 1; /* Master */
914 if (chans == 2)
915 opti82c46x = 3; /* Master and Slave */
916 } /* Slave only */
917 else if (chans == 1)
918 opti82c46x = 1;
919 } 1492 }
920 1493
921 for (i = 0; i < last_port; i++) { 1494 if (opti82c46x)
922 /* Skip primary if we have seen a PCI one */ 1495 probe_opti_vlb();
923 if (i == 0 && primary == 1) 1496 if (qdi)
924 continue; 1497 probe_qdi_vlb();
925 /* Skip secondary if we have seen a PCI one */ 1498
926 if (i == 1 && secondary == 1) 1499 for (i = 0; i < NR_HOST; i++, pl++) {
1500 if (pl->port == 0)
927 continue; 1501 continue;
928 if (legacy_init_one(i, legacy_port[i], 1502 if (pl->type == UNKNOWN)
929 legacy_port[i] + 0x0206, 1503 pl->type = probe_chip_type(pl);
930 legacy_irq[i]) == 0) 1504 pl->slot = slot++;
1505 if (legacy_init_one(pl) == 0)
931 ct++; 1506 ct++;
932 } 1507 }
933 if (ct != 0) 1508 if (ct != 0)
@@ -941,11 +1516,8 @@ static __exit void legacy_exit(void)
941 1516
942 for (i = 0; i < nr_legacy_host; i++) { 1517 for (i = 0; i < nr_legacy_host; i++) {
943 struct legacy_data *ld = &legacy_data[i]; 1518 struct legacy_data *ld = &legacy_data[i];
944
945 ata_host_detach(legacy_host[i]); 1519 ata_host_detach(legacy_host[i]);
946 platform_device_unregister(ld->platform_dev); 1520 platform_device_unregister(ld->platform_dev);
947 if (ld->timing)
948 release_region(ld->timing, 2);
949 } 1521 }
950} 1522}
951 1523
@@ -960,9 +1532,9 @@ module_param(ht6560a, int, 0);
960module_param(ht6560b, int, 0); 1532module_param(ht6560b, int, 0);
961module_param(opti82c611a, int, 0); 1533module_param(opti82c611a, int, 0);
962module_param(opti82c46x, int, 0); 1534module_param(opti82c46x, int, 0);
1535module_param(qdi, int, 0);
963module_param(pio_mask, int, 0); 1536module_param(pio_mask, int, 0);
964module_param(iordy_mask, int, 0); 1537module_param(iordy_mask, int, 0);
965 1538
966module_init(legacy_init); 1539module_init(legacy_init);
967module_exit(legacy_exit); 1540module_exit(legacy_exit);
968
diff --git a/drivers/ata/pata_mpc52xx.c b/drivers/ata/pata_mpc52xx.c
index 50c56e2814c1..dc401626cdb2 100644
--- a/drivers/ata/pata_mpc52xx.c
+++ b/drivers/ata/pata_mpc52xx.c
@@ -364,7 +364,7 @@ mpc52xx_ata_probe(struct of_device *op, const struct of_device_id *match)
364{ 364{
365 unsigned int ipb_freq; 365 unsigned int ipb_freq;
366 struct resource res_mem; 366 struct resource res_mem;
367 int ata_irq = NO_IRQ; 367 int ata_irq;
368 struct mpc52xx_ata __iomem *ata_regs; 368 struct mpc52xx_ata __iomem *ata_regs;
369 struct mpc52xx_ata_priv *priv; 369 struct mpc52xx_ata_priv *priv;
370 int rv; 370 int rv;
diff --git a/drivers/ata/pata_ninja32.c b/drivers/ata/pata_ninja32.c
new file mode 100644
index 000000000000..1c1b83541d13
--- /dev/null
+++ b/drivers/ata/pata_ninja32.c
@@ -0,0 +1,214 @@
1/*
2 * pata_ninja32.c - Ninja32 PATA for new ATA layer
3 * (C) 2007 Red Hat Inc
4 * Alan Cox <alan@redhat.com>
5 *
6 * Note: The controller like many controllers has shared timings for
7 * PIO and DMA. We thus flip to the DMA timings in dma_start and flip back
8 * in the dma_stop function. Thus we actually don't need a set_dmamode
9 * method as the PIO method is always called and will set the right PIO
10 * timing parameters.
11 *
12 * The Ninja32 Cardbus is not a generic SFF controller. Instead it is
13 * laid out as follows off BAR 0. This is based upon Mark Lord's delkin
14 * driver and the extensive analysis done by the BSD developers, notably
15 * ITOH Yasufumi.
16 *
17 * Base + 0x00 IRQ Status
18 * Base + 0x01 IRQ control
19 * Base + 0x02 Chipset control
20 * Base + 0x04 VDMA and reset control + wait bits
21 * Base + 0x08 BMIMBA
22 * Base + 0x0C DMA Length
23 * Base + 0x10 Taskfile
24 * Base + 0x18 BMDMA Status ?
25 * Base + 0x1C
26 * Base + 0x1D Bus master control
27 * bit 0 = enable
28 * bit 1 = 0 write/1 read
29 * bit 2 = 1 sgtable
30 * bit 3 = go
31 * bit 4-6 wait bits
32 * bit 7 = done
33 * Base + 0x1E AltStatus
34 * Base + 0x1F timing register
35 */
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/pci.h>
40#include <linux/init.h>
41#include <linux/blkdev.h>
42#include <linux/delay.h>
43#include <scsi/scsi_host.h>
44#include <linux/libata.h>
45
46#define DRV_NAME "pata_ninja32"
47#define DRV_VERSION "0.0.1"
48
49
50/**
51 * ninja32_set_piomode - set initial PIO mode data
52 * @ap: ATA interface
53 * @adev: ATA device
54 *
55 * Called to do the PIO mode setup. Our timing registers are shared
56 * but we want to set the PIO timing by default.
57 */
58
59static void ninja32_set_piomode(struct ata_port *ap, struct ata_device *adev)
60{
61 static u16 pio_timing[5] = {
62 0xd6, 0x85, 0x44, 0x33, 0x13
63 };
64 iowrite8(pio_timing[adev->pio_mode - XFER_PIO_0],
65 ap->ioaddr.bmdma_addr + 0x1f);
66 ap->private_data = adev;
67}
68
69
70static void ninja32_dev_select(struct ata_port *ap, unsigned int device)
71{
72 struct ata_device *adev = &ap->link.device[device];
73 if (ap->private_data != adev) {
74 iowrite8(0xd6, ap->ioaddr.bmdma_addr + 0x1f);
75 ata_std_dev_select(ap, device);
76 ninja32_set_piomode(ap, adev);
77 }
78}
79
80static struct scsi_host_template ninja32_sht = {
81 .module = THIS_MODULE,
82 .name = DRV_NAME,
83 .ioctl = ata_scsi_ioctl,
84 .queuecommand = ata_scsi_queuecmd,
85 .can_queue = ATA_DEF_QUEUE,
86 .this_id = ATA_SHT_THIS_ID,
87 .sg_tablesize = LIBATA_MAX_PRD,
88 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
89 .emulated = ATA_SHT_EMULATED,
90 .use_clustering = ATA_SHT_USE_CLUSTERING,
91 .proc_name = DRV_NAME,
92 .dma_boundary = ATA_DMA_BOUNDARY,
93 .slave_configure = ata_scsi_slave_config,
94 .slave_destroy = ata_scsi_slave_destroy,
95 .bios_param = ata_std_bios_param,
96};
97
98static struct ata_port_operations ninja32_port_ops = {
99 .set_piomode = ninja32_set_piomode,
100 .mode_filter = ata_pci_default_filter,
101
102 .tf_load = ata_tf_load,
103 .tf_read = ata_tf_read,
104 .check_status = ata_check_status,
105 .exec_command = ata_exec_command,
106 .dev_select = ninja32_dev_select,
107
108 .freeze = ata_bmdma_freeze,
109 .thaw = ata_bmdma_thaw,
110 .error_handler = ata_bmdma_error_handler,
111 .post_internal_cmd = ata_bmdma_post_internal_cmd,
112 .cable_detect = ata_cable_40wire,
113
114 .bmdma_setup = ata_bmdma_setup,
115 .bmdma_start = ata_bmdma_start,
116 .bmdma_stop = ata_bmdma_stop,
117 .bmdma_status = ata_bmdma_status,
118
119 .qc_prep = ata_qc_prep,
120 .qc_issue = ata_qc_issue_prot,
121
122 .data_xfer = ata_data_xfer,
123
124 .irq_handler = ata_interrupt,
125 .irq_clear = ata_bmdma_irq_clear,
126 .irq_on = ata_irq_on,
127
128 .port_start = ata_sff_port_start,
129};
130
131static int ninja32_init_one(struct pci_dev *dev, const struct pci_device_id *id)
132{
133 struct ata_host *host;
134 struct ata_port *ap;
135 void __iomem *base;
136 int rc;
137
138 host = ata_host_alloc(&dev->dev, 1);
139 if (!host)
140 return -ENOMEM;
141 ap = host->ports[0];
142
143 /* Set up the PCI device */
144 rc = pcim_enable_device(dev);
145 if (rc)
146 return rc;
147 rc = pcim_iomap_regions(dev, 1 << 0, DRV_NAME);
148 if (rc == -EBUSY)
149 pcim_pin_device(dev);
150 if (rc)
151 return rc;
152
153 host->iomap = pcim_iomap_table(dev);
154 rc = pci_set_dma_mask(dev, ATA_DMA_MASK);
155 if (rc)
156 return rc;
157 rc = pci_set_consistent_dma_mask(dev, ATA_DMA_MASK);
158 if (rc)
159 return rc;
160 pci_set_master(dev);
161
162 /* Set up the register mappings */
163 base = host->iomap[0];
164 if (!base)
165 return -ENOMEM;
166 ap->ops = &ninja32_port_ops;
167 ap->pio_mask = 0x1F;
168 ap->flags |= ATA_FLAG_SLAVE_POSS;
169
170 ap->ioaddr.cmd_addr = base + 0x10;
171 ap->ioaddr.ctl_addr = base + 0x1E;
172 ap->ioaddr.altstatus_addr = base + 0x1E;
173 ap->ioaddr.bmdma_addr = base;
174 ata_std_ports(&ap->ioaddr);
175
176 iowrite8(0x05, base + 0x01); /* Enable interrupt lines */
177 iowrite8(0xB3, base + 0x02); /* Burst, ?? setup */
178 iowrite8(0x00, base + 0x04); /* WAIT0 ? */
179 /* FIXME: Should we disable them at remove ? */
180 return ata_host_activate(host, dev->irq, ata_interrupt,
181 IRQF_SHARED, &ninja32_sht);
182}
183
184static const struct pci_device_id ninja32[] = {
185 { 0x1145, 0xf021, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
186 { 0x1145, 0xf024, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
187 { },
188};
189
190static struct pci_driver ninja32_pci_driver = {
191 .name = DRV_NAME,
192 .id_table = ninja32,
193 .probe = ninja32_init_one,
194 .remove = ata_pci_remove_one
195};
196
197static int __init ninja32_init(void)
198{
199 return pci_register_driver(&ninja32_pci_driver);
200}
201
202static void __exit ninja32_exit(void)
203{
204 pci_unregister_driver(&ninja32_pci_driver);
205}
206
207MODULE_AUTHOR("Alan Cox");
208MODULE_DESCRIPTION("low-level driver for Ninja32 ATA");
209MODULE_LICENSE("GPL");
210MODULE_DEVICE_TABLE(pci, ninja32);
211MODULE_VERSION(DRV_VERSION);
212
213module_init(ninja32_init);
214module_exit(ninja32_exit);
diff --git a/drivers/ata/pata_pcmcia.c b/drivers/ata/pata_pcmcia.c
index fd36099428a4..3e7f6a9da28b 100644
--- a/drivers/ata/pata_pcmcia.c
+++ b/drivers/ata/pata_pcmcia.c
@@ -42,7 +42,7 @@
42 42
43 43
44#define DRV_NAME "pata_pcmcia" 44#define DRV_NAME "pata_pcmcia"
45#define DRV_VERSION "0.3.2" 45#define DRV_VERSION "0.3.3"
46 46
47/* 47/*
48 * Private data structure to glue stuff together 48 * Private data structure to glue stuff together
@@ -86,6 +86,47 @@ static int pcmcia_set_mode(struct ata_link *link, struct ata_device **r_failed_d
86 return ata_do_set_mode(link, r_failed_dev); 86 return ata_do_set_mode(link, r_failed_dev);
87} 87}
88 88
89/**
90 * pcmcia_set_mode_8bit - PCMCIA specific mode setup
91 * @link: link
92 * @r_failed_dev: Return pointer for failed device
93 *
94 * For the simple emulated 8bit stuff the less we do the better.
95 */
96
97static int pcmcia_set_mode_8bit(struct ata_link *link,
98 struct ata_device **r_failed_dev)
99{
100 return 0;
101}
102
103/**
104 * ata_data_xfer_8bit - Transfer data by 8bit PIO
105 * @dev: device to target
106 * @buf: data buffer
107 * @buflen: buffer length
108 * @rw: read/write
109 *
110 * Transfer data from/to the device data register by 8 bit PIO.
111 *
112 * LOCKING:
113 * Inherited from caller.
114 */
115
116static unsigned int ata_data_xfer_8bit(struct ata_device *dev,
117 unsigned char *buf, unsigned int buflen, int rw)
118{
119 struct ata_port *ap = dev->link->ap;
120
121 if (rw == READ)
122 ioread8_rep(ap->ioaddr.data_addr, buf, buflen);
123 else
124 iowrite8_rep(ap->ioaddr.data_addr, buf, buflen);
125
126 return buflen;
127}
128
129
89static struct scsi_host_template pcmcia_sht = { 130static struct scsi_host_template pcmcia_sht = {
90 .module = THIS_MODULE, 131 .module = THIS_MODULE,
91 .name = DRV_NAME, 132 .name = DRV_NAME,
@@ -129,6 +170,31 @@ static struct ata_port_operations pcmcia_port_ops = {
129 .port_start = ata_sff_port_start, 170 .port_start = ata_sff_port_start,
130}; 171};
131 172
173static struct ata_port_operations pcmcia_8bit_port_ops = {
174 .set_mode = pcmcia_set_mode_8bit,
175 .tf_load = ata_tf_load,
176 .tf_read = ata_tf_read,
177 .check_status = ata_check_status,
178 .exec_command = ata_exec_command,
179 .dev_select = ata_std_dev_select,
180
181 .freeze = ata_bmdma_freeze,
182 .thaw = ata_bmdma_thaw,
183 .error_handler = ata_bmdma_error_handler,
184 .post_internal_cmd = ata_bmdma_post_internal_cmd,
185 .cable_detect = ata_cable_40wire,
186
187 .qc_prep = ata_qc_prep,
188 .qc_issue = ata_qc_issue_prot,
189
190 .data_xfer = ata_data_xfer_8bit,
191
192 .irq_clear = ata_bmdma_irq_clear,
193 .irq_on = ata_irq_on,
194
195 .port_start = ata_sff_port_start,
196};
197
132#define CS_CHECK(fn, ret) \ 198#define CS_CHECK(fn, ret) \
133do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0) 199do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
134 200
@@ -153,9 +219,12 @@ static int pcmcia_init_one(struct pcmcia_device *pdev)
153 cistpl_cftable_entry_t dflt; 219 cistpl_cftable_entry_t dflt;
154 } *stk = NULL; 220 } *stk = NULL;
155 cistpl_cftable_entry_t *cfg; 221 cistpl_cftable_entry_t *cfg;
156 int pass, last_ret = 0, last_fn = 0, is_kme = 0, ret = -ENOMEM; 222 int pass, last_ret = 0, last_fn = 0, is_kme = 0, ret = -ENOMEM, p;
157 unsigned long io_base, ctl_base; 223 unsigned long io_base, ctl_base;
158 void __iomem *io_addr, *ctl_addr; 224 void __iomem *io_addr, *ctl_addr;
225 int n_ports = 1;
226
227 struct ata_port_operations *ops = &pcmcia_port_ops;
159 228
160 info = kzalloc(sizeof(*info), GFP_KERNEL); 229 info = kzalloc(sizeof(*info), GFP_KERNEL);
161 if (info == NULL) 230 if (info == NULL)
@@ -282,27 +351,32 @@ next_entry:
282 /* FIXME: Could be more ports at base + 0x10 but we only deal with 351 /* FIXME: Could be more ports at base + 0x10 but we only deal with
283 one right now */ 352 one right now */
284 if (pdev->io.NumPorts1 >= 0x20) 353 if (pdev->io.NumPorts1 >= 0x20)
285 printk(KERN_WARNING DRV_NAME ": second channel not yet supported.\n"); 354 n_ports = 2;
286 355
356 if (pdev->manf_id == 0x0097 && pdev->card_id == 0x1620)
357 ops = &pcmcia_8bit_port_ops;
287 /* 358 /*
288 * Having done the PCMCIA plumbing the ATA side is relatively 359 * Having done the PCMCIA plumbing the ATA side is relatively
289 * sane. 360 * sane.
290 */ 361 */
291 ret = -ENOMEM; 362 ret = -ENOMEM;
292 host = ata_host_alloc(&pdev->dev, 1); 363 host = ata_host_alloc(&pdev->dev, n_ports);
293 if (!host) 364 if (!host)
294 goto failed; 365 goto failed;
295 ap = host->ports[0];
296 366
297 ap->ops = &pcmcia_port_ops; 367 for (p = 0; p < n_ports; p++) {
298 ap->pio_mask = 1; /* ISA so PIO 0 cycles */ 368 ap = host->ports[p];
299 ap->flags |= ATA_FLAG_SLAVE_POSS;
300 ap->ioaddr.cmd_addr = io_addr;
301 ap->ioaddr.altstatus_addr = ctl_addr;
302 ap->ioaddr.ctl_addr = ctl_addr;
303 ata_std_ports(&ap->ioaddr);
304 369
305 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", io_base, ctl_base); 370 ap->ops = ops;
371 ap->pio_mask = 1; /* ISA so PIO 0 cycles */
372 ap->flags |= ATA_FLAG_SLAVE_POSS;
373 ap->ioaddr.cmd_addr = io_addr + 0x10 * p;
374 ap->ioaddr.altstatus_addr = ctl_addr + 0x10 * p;
375 ap->ioaddr.ctl_addr = ctl_addr + 0x10 * p;
376 ata_std_ports(&ap->ioaddr);
377
378 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", io_base, ctl_base);
379 }
306 380
307 /* activate */ 381 /* activate */
308 ret = ata_host_activate(host, pdev->irq.AssignedIRQ, ata_interrupt, 382 ret = ata_host_activate(host, pdev->irq.AssignedIRQ, ata_interrupt,
@@ -360,6 +434,7 @@ static struct pcmcia_device_id pcmcia_devices[] = {
360 PCMCIA_DEVICE_MANF_CARD(0x0032, 0x0704), 434 PCMCIA_DEVICE_MANF_CARD(0x0032, 0x0704),
361 PCMCIA_DEVICE_MANF_CARD(0x0032, 0x2904), 435 PCMCIA_DEVICE_MANF_CARD(0x0032, 0x2904),
362 PCMCIA_DEVICE_MANF_CARD(0x0045, 0x0401), /* SanDisk CFA */ 436 PCMCIA_DEVICE_MANF_CARD(0x0045, 0x0401), /* SanDisk CFA */
437 PCMCIA_DEVICE_MANF_CARD(0x0097, 0x1620), /* TI emulated */
363 PCMCIA_DEVICE_MANF_CARD(0x0098, 0x0000), /* Toshiba */ 438 PCMCIA_DEVICE_MANF_CARD(0x0098, 0x0000), /* Toshiba */
364 PCMCIA_DEVICE_MANF_CARD(0x00a4, 0x002d), 439 PCMCIA_DEVICE_MANF_CARD(0x00a4, 0x002d),
365 PCMCIA_DEVICE_MANF_CARD(0x00ce, 0x0000), /* Samsung */ 440 PCMCIA_DEVICE_MANF_CARD(0x00ce, 0x0000), /* Samsung */
diff --git a/drivers/ata/pata_pdc2027x.c b/drivers/ata/pata_pdc2027x.c
index 2622577521a1..028af5dbeed6 100644
--- a/drivers/ata/pata_pdc2027x.c
+++ b/drivers/ata/pata_pdc2027x.c
@@ -348,7 +348,7 @@ static unsigned long pdc2027x_mode_filter(struct ata_device *adev, unsigned long
348 ata_id_c_string(pair->id, model_num, ATA_ID_PROD, 348 ata_id_c_string(pair->id, model_num, ATA_ID_PROD,
349 ATA_ID_PROD_LEN + 1); 349 ATA_ID_PROD_LEN + 1);
350 /* If the master is a maxtor in UDMA6 then the slave should not use UDMA 6 */ 350 /* If the master is a maxtor in UDMA6 then the slave should not use UDMA 6 */
351 if (strstr(model_num, "Maxtor") == 0 && pair->dma_mode == XFER_UDMA_6) 351 if (strstr(model_num, "Maxtor") == NULL && pair->dma_mode == XFER_UDMA_6)
352 mask &= ~ (1 << (6 + ATA_SHIFT_UDMA)); 352 mask &= ~ (1 << (6 + ATA_SHIFT_UDMA));
353 353
354 return ata_pci_default_filter(adev, mask); 354 return ata_pci_default_filter(adev, mask);
diff --git a/drivers/ata/pata_pdc202xx_old.c b/drivers/ata/pata_pdc202xx_old.c
index 6c9689b59b06..3ed866723e0c 100644
--- a/drivers/ata/pata_pdc202xx_old.c
+++ b/drivers/ata/pata_pdc202xx_old.c
@@ -168,8 +168,7 @@ static void pdc2026x_bmdma_start(struct ata_queued_cmd *qc)
168 pdc202xx_set_dmamode(ap, qc->dev); 168 pdc202xx_set_dmamode(ap, qc->dev);
169 169
170 /* Cases the state machine will not complete correctly without help */ 170 /* Cases the state machine will not complete correctly without help */
171 if ((tf->flags & ATA_TFLAG_LBA48) || tf->protocol == ATA_PROT_ATAPI_DMA) 171 if ((tf->flags & ATA_TFLAG_LBA48) || tf->protocol == ATAPI_PROT_DMA) {
172 {
173 len = qc->nbytes / 2; 172 len = qc->nbytes / 2;
174 173
175 if (tf->flags & ATA_TFLAG_WRITE) 174 if (tf->flags & ATA_TFLAG_WRITE)
@@ -208,7 +207,7 @@ static void pdc2026x_bmdma_stop(struct ata_queued_cmd *qc)
208 void __iomem *atapi_reg = master + 0x20 + (4 * ap->port_no); 207 void __iomem *atapi_reg = master + 0x20 + (4 * ap->port_no);
209 208
210 /* Cases the state machine will not complete correctly */ 209 /* Cases the state machine will not complete correctly */
211 if (tf->protocol == ATA_PROT_ATAPI_DMA || ( tf->flags & ATA_TFLAG_LBA48)) { 210 if (tf->protocol == ATAPI_PROT_DMA || (tf->flags & ATA_TFLAG_LBA48)) {
212 iowrite32(0, atapi_reg); 211 iowrite32(0, atapi_reg);
213 iowrite8(ioread8(clock) & ~sel66, clock); 212 iowrite8(ioread8(clock) & ~sel66, clock);
214 } 213 }
diff --git a/drivers/ata/pata_qdi.c b/drivers/ata/pata_qdi.c
index a4c0e502cb42..9f308ed76cc8 100644
--- a/drivers/ata/pata_qdi.c
+++ b/drivers/ata/pata_qdi.c
@@ -124,29 +124,33 @@ static unsigned int qdi_qc_issue_prot(struct ata_queued_cmd *qc)
124 return ata_qc_issue_prot(qc); 124 return ata_qc_issue_prot(qc);
125} 125}
126 126
127static void qdi_data_xfer(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data) 127static unsigned int qdi_data_xfer(struct ata_device *dev, unsigned char *buf,
128 unsigned int buflen, int rw)
128{ 129{
129 struct ata_port *ap = adev->link->ap; 130 if (ata_id_has_dword_io(dev->id)) {
130 int slop = buflen & 3; 131 struct ata_port *ap = dev->link->ap;
132 int slop = buflen & 3;
131 133
132 if (ata_id_has_dword_io(adev->id)) { 134 if (rw == READ)
133 if (write_data)
134 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
135 else
136 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2); 135 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
136 else
137 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
137 138
138 if (unlikely(slop)) { 139 if (unlikely(slop)) {
139 __le32 pad = 0; 140 u32 pad;
140 if (write_data) { 141 if (rw == READ) {
141 memcpy(&pad, buf + buflen - slop, slop);
142 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
143 } else {
144 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr)); 142 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
145 memcpy(buf + buflen - slop, &pad, slop); 143 memcpy(buf + buflen - slop, &pad, slop);
144 } else {
145 memcpy(&pad, buf + buflen - slop, slop);
146 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
146 } 147 }
148 buflen += 4 - slop;
147 } 149 }
148 } else 150 } else
149 ata_data_xfer(adev, buf, buflen, write_data); 151 buflen = ata_data_xfer(dev, buf, buflen, rw);
152
153 return buflen;
150} 154}
151 155
152static struct scsi_host_template qdi_sht = { 156static struct scsi_host_template qdi_sht = {
diff --git a/drivers/ata/pata_scc.c b/drivers/ata/pata_scc.c
index ea2ef9fc15be..55055b27524c 100644
--- a/drivers/ata/pata_scc.c
+++ b/drivers/ata/pata_scc.c
@@ -768,45 +768,47 @@ static u8 scc_bmdma_status (struct ata_port *ap)
768 768
769/** 769/**
770 * scc_data_xfer - Transfer data by PIO 770 * scc_data_xfer - Transfer data by PIO
771 * @adev: device for this I/O 771 * @dev: device for this I/O
772 * @buf: data buffer 772 * @buf: data buffer
773 * @buflen: buffer length 773 * @buflen: buffer length
774 * @write_data: read/write 774 * @rw: read/write
775 * 775 *
776 * Note: Original code is ata_data_xfer(). 776 * Note: Original code is ata_data_xfer().
777 */ 777 */
778 778
779static void scc_data_xfer (struct ata_device *adev, unsigned char *buf, 779static unsigned int scc_data_xfer (struct ata_device *dev, unsigned char *buf,
780 unsigned int buflen, int write_data) 780 unsigned int buflen, int rw)
781{ 781{
782 struct ata_port *ap = adev->link->ap; 782 struct ata_port *ap = dev->link->ap;
783 unsigned int words = buflen >> 1; 783 unsigned int words = buflen >> 1;
784 unsigned int i; 784 unsigned int i;
785 u16 *buf16 = (u16 *) buf; 785 u16 *buf16 = (u16 *) buf;
786 void __iomem *mmio = ap->ioaddr.data_addr; 786 void __iomem *mmio = ap->ioaddr.data_addr;
787 787
788 /* Transfer multiple of 2 bytes */ 788 /* Transfer multiple of 2 bytes */
789 if (write_data) { 789 if (rw == READ)
790 for (i = 0; i < words; i++)
791 out_be32(mmio, cpu_to_le16(buf16[i]));
792 } else {
793 for (i = 0; i < words; i++) 790 for (i = 0; i < words; i++)
794 buf16[i] = le16_to_cpu(in_be32(mmio)); 791 buf16[i] = le16_to_cpu(in_be32(mmio));
795 } 792 else
793 for (i = 0; i < words; i++)
794 out_be32(mmio, cpu_to_le16(buf16[i]));
796 795
797 /* Transfer trailing 1 byte, if any. */ 796 /* Transfer trailing 1 byte, if any. */
798 if (unlikely(buflen & 0x01)) { 797 if (unlikely(buflen & 0x01)) {
799 u16 align_buf[1] = { 0 }; 798 u16 align_buf[1] = { 0 };
800 unsigned char *trailing_buf = buf + buflen - 1; 799 unsigned char *trailing_buf = buf + buflen - 1;
801 800
802 if (write_data) { 801 if (rw == READ) {
803 memcpy(align_buf, trailing_buf, 1);
804 out_be32(mmio, cpu_to_le16(align_buf[0]));
805 } else {
806 align_buf[0] = le16_to_cpu(in_be32(mmio)); 802 align_buf[0] = le16_to_cpu(in_be32(mmio));
807 memcpy(trailing_buf, align_buf, 1); 803 memcpy(trailing_buf, align_buf, 1);
804 } else {
805 memcpy(align_buf, trailing_buf, 1);
806 out_be32(mmio, cpu_to_le16(align_buf[0]));
808 } 807 }
808 words++;
809 } 809 }
810
811 return words << 1;
810} 812}
811 813
812/** 814/**
diff --git a/drivers/ata/pata_serverworks.c b/drivers/ata/pata_serverworks.c
index 8bed88873720..9c523fbf529e 100644
--- a/drivers/ata/pata_serverworks.c
+++ b/drivers/ata/pata_serverworks.c
@@ -41,7 +41,7 @@
41#include <linux/libata.h> 41#include <linux/libata.h>
42 42
43#define DRV_NAME "pata_serverworks" 43#define DRV_NAME "pata_serverworks"
44#define DRV_VERSION "0.4.2" 44#define DRV_VERSION "0.4.3"
45 45
46#define SVWKS_CSB5_REVISION_NEW 0x92 /* min PCI_REVISION_ID for UDMA5 (A2.0) */ 46#define SVWKS_CSB5_REVISION_NEW 0x92 /* min PCI_REVISION_ID for UDMA5 (A2.0) */
47#define SVWKS_CSB6_REVISION 0xa0 /* min PCI_REVISION_ID for UDMA4 (A1.0) */ 47#define SVWKS_CSB6_REVISION 0xa0 /* min PCI_REVISION_ID for UDMA4 (A1.0) */
@@ -102,7 +102,7 @@ static int osb4_cable(struct ata_port *ap) {
102} 102}
103 103
104/** 104/**
105 * csb4_cable - CSB5/6 cable detect 105 * csb_cable - CSB5/6 cable detect
106 * @ap: ATA port to check 106 * @ap: ATA port to check
107 * 107 *
108 * Serverworks default arrangement is to use the drive side detection 108 * Serverworks default arrangement is to use the drive side detection
@@ -110,7 +110,7 @@ static int osb4_cable(struct ata_port *ap) {
110 */ 110 */
111 111
112static int csb_cable(struct ata_port *ap) { 112static int csb_cable(struct ata_port *ap) {
113 return ATA_CBL_PATA80; 113 return ATA_CBL_PATA_UNK;
114} 114}
115 115
116struct sv_cable_table { 116struct sv_cable_table {
@@ -231,7 +231,6 @@ static unsigned long serverworks_csb_filter(struct ata_device *adev, unsigned lo
231 return ata_pci_default_filter(adev, mask); 231 return ata_pci_default_filter(adev, mask);
232} 232}
233 233
234
235/** 234/**
236 * serverworks_set_piomode - set initial PIO mode data 235 * serverworks_set_piomode - set initial PIO mode data
237 * @ap: ATA interface 236 * @ap: ATA interface
@@ -243,7 +242,7 @@ static unsigned long serverworks_csb_filter(struct ata_device *adev, unsigned lo
243static void serverworks_set_piomode(struct ata_port *ap, struct ata_device *adev) 242static void serverworks_set_piomode(struct ata_port *ap, struct ata_device *adev)
244{ 243{
245 static const u8 pio_mode[] = { 0x5d, 0x47, 0x34, 0x22, 0x20 }; 244 static const u8 pio_mode[] = { 0x5d, 0x47, 0x34, 0x22, 0x20 };
246 int offset = 1 + (2 * ap->port_no) - adev->devno; 245 int offset = 1 + 2 * ap->port_no - adev->devno;
247 int devbits = (2 * ap->port_no + adev->devno) * 4; 246 int devbits = (2 * ap->port_no + adev->devno) * 4;
248 u16 csb5_pio; 247 u16 csb5_pio;
249 struct pci_dev *pdev = to_pci_dev(ap->host->dev); 248 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
diff --git a/drivers/ata/pata_via.c b/drivers/ata/pata_via.c
index 453d72bf2598..39627ab684bf 100644
--- a/drivers/ata/pata_via.c
+++ b/drivers/ata/pata_via.c
@@ -185,7 +185,8 @@ static int via_cable_detect(struct ata_port *ap) {
185 if (ata66 & (0x10100000 >> (16 * ap->port_no))) 185 if (ata66 & (0x10100000 >> (16 * ap->port_no)))
186 return ATA_CBL_PATA80; 186 return ATA_CBL_PATA80;
187 /* Check with ACPI so we can spot BIOS reported SATA bridges */ 187 /* Check with ACPI so we can spot BIOS reported SATA bridges */
188 if (ata_acpi_cbl_80wire(ap)) 188 if (ata_acpi_init_gtm(ap) &&
189 ata_acpi_cbl_80wire(ap, ata_acpi_init_gtm(ap)))
189 return ATA_CBL_PATA80; 190 return ATA_CBL_PATA80;
190 return ATA_CBL_PATA40; 191 return ATA_CBL_PATA40;
191} 192}
diff --git a/drivers/ata/pata_winbond.c b/drivers/ata/pata_winbond.c
index 7116a9e7a8b2..99c92eda217b 100644
--- a/drivers/ata/pata_winbond.c
+++ b/drivers/ata/pata_winbond.c
@@ -92,29 +92,33 @@ static void winbond_set_piomode(struct ata_port *ap, struct ata_device *adev)
92} 92}
93 93
94 94
95static void winbond_data_xfer(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data) 95static unsigned int winbond_data_xfer(struct ata_device *dev,
96 unsigned char *buf, unsigned int buflen, int rw)
96{ 97{
97 struct ata_port *ap = adev->link->ap; 98 struct ata_port *ap = dev->link->ap;
98 int slop = buflen & 3; 99 int slop = buflen & 3;
99 100
100 if (ata_id_has_dword_io(adev->id)) { 101 if (ata_id_has_dword_io(dev->id)) {
101 if (write_data) 102 if (rw == READ)
102 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
103 else
104 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2); 103 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
104 else
105 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
105 106
106 if (unlikely(slop)) { 107 if (unlikely(slop)) {
107 __le32 pad = 0; 108 u32 pad;
108 if (write_data) { 109 if (rw == READ) {
109 memcpy(&pad, buf + buflen - slop, slop);
110 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
111 } else {
112 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr)); 110 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
113 memcpy(buf + buflen - slop, &pad, slop); 111 memcpy(buf + buflen - slop, &pad, slop);
112 } else {
113 memcpy(&pad, buf + buflen - slop, slop);
114 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
114 } 115 }
116 buflen += 4 - slop;
115 } 117 }
116 } else 118 } else
117 ata_data_xfer(adev, buf, buflen, write_data); 119 buflen = ata_data_xfer(dev, buf, buflen, rw);
120
121 return buflen;
118} 122}
119 123
120static struct scsi_host_template winbond_sht = { 124static struct scsi_host_template winbond_sht = {
@@ -191,7 +195,7 @@ static __init int winbond_init_one(unsigned long port)
191 reg = winbond_readcfg(port, 0x81); 195 reg = winbond_readcfg(port, 0x81);
192 196
193 if (!(reg & 0x03)) /* Disabled */ 197 if (!(reg & 0x03)) /* Disabled */
194 return 0; 198 return -ENODEV;
195 199
196 for (i = 0; i < 2 ; i ++) { 200 for (i = 0; i < 2 ; i ++) {
197 unsigned long cmd_port = 0x1F0 - (0x80 * i); 201 unsigned long cmd_port = 0x1F0 - (0x80 * i);
diff --git a/drivers/ata/pdc_adma.c b/drivers/ata/pdc_adma.c
index bd4c2a3c88d7..8e1b7e9c0ae4 100644
--- a/drivers/ata/pdc_adma.c
+++ b/drivers/ata/pdc_adma.c
@@ -321,8 +321,9 @@ static int adma_fill_sg(struct ata_queued_cmd *qc)
321 u8 *buf = pp->pkt, *last_buf = NULL; 321 u8 *buf = pp->pkt, *last_buf = NULL;
322 int i = (2 + buf[3]) * 8; 322 int i = (2 + buf[3]) * 8;
323 u8 pFLAGS = pORD | ((qc->tf.flags & ATA_TFLAG_WRITE) ? pDIRO : 0); 323 u8 pFLAGS = pORD | ((qc->tf.flags & ATA_TFLAG_WRITE) ? pDIRO : 0);
324 unsigned int si;
324 325
325 ata_for_each_sg(sg, qc) { 326 for_each_sg(qc->sg, sg, qc->n_elem, si) {
326 u32 addr; 327 u32 addr;
327 u32 len; 328 u32 len;
328 329
@@ -455,7 +456,7 @@ static unsigned int adma_qc_issue(struct ata_queued_cmd *qc)
455 adma_packet_start(qc); 456 adma_packet_start(qc);
456 return 0; 457 return 0;
457 458
458 case ATA_PROT_ATAPI_DMA: 459 case ATAPI_PROT_DMA:
459 BUG(); 460 BUG();
460 break; 461 break;
461 462
diff --git a/drivers/ata/sata_fsl.c b/drivers/ata/sata_fsl.c
index d015b4adcfe0..922d7b2efba8 100644
--- a/drivers/ata/sata_fsl.c
+++ b/drivers/ata/sata_fsl.c
@@ -333,13 +333,14 @@ static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
333 struct prde *prd_ptr_to_indirect_ext = NULL; 333 struct prde *prd_ptr_to_indirect_ext = NULL;
334 unsigned indirect_ext_segment_sz = 0; 334 unsigned indirect_ext_segment_sz = 0;
335 dma_addr_t indirect_ext_segment_paddr; 335 dma_addr_t indirect_ext_segment_paddr;
336 unsigned int si;
336 337
337 VPRINTK("SATA FSL : cd = 0x%x, prd = 0x%x\n", cmd_desc, prd); 338 VPRINTK("SATA FSL : cd = 0x%x, prd = 0x%x\n", cmd_desc, prd);
338 339
339 indirect_ext_segment_paddr = cmd_desc_paddr + 340 indirect_ext_segment_paddr = cmd_desc_paddr +
340 SATA_FSL_CMD_DESC_OFFSET_TO_PRDT + SATA_FSL_MAX_PRD_DIRECT * 16; 341 SATA_FSL_CMD_DESC_OFFSET_TO_PRDT + SATA_FSL_MAX_PRD_DIRECT * 16;
341 342
342 ata_for_each_sg(sg, qc) { 343 for_each_sg(qc->sg, sg, qc->n_elem, si) {
343 dma_addr_t sg_addr = sg_dma_address(sg); 344 dma_addr_t sg_addr = sg_dma_address(sg);
344 u32 sg_len = sg_dma_len(sg); 345 u32 sg_len = sg_dma_len(sg);
345 346
@@ -417,7 +418,7 @@ static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
417 } 418 }
418 419
419 /* setup "ACMD - atapi command" in cmd. desc. if this is ATAPI cmd */ 420 /* setup "ACMD - atapi command" in cmd. desc. if this is ATAPI cmd */
420 if (is_atapi_taskfile(&qc->tf)) { 421 if (ata_is_atapi(qc->tf.protocol)) {
421 desc_info |= ATAPI_CMD; 422 desc_info |= ATAPI_CMD;
422 memset((void *)&cd->acmd, 0, 32); 423 memset((void *)&cd->acmd, 0, 32);
423 memcpy((void *)&cd->acmd, qc->cdb, qc->dev->cdb_len); 424 memcpy((void *)&cd->acmd, qc->cdb, qc->dev->cdb_len);
diff --git a/drivers/ata/sata_inic162x.c b/drivers/ata/sata_inic162x.c
index 323c087e8cc1..96e614a1c169 100644
--- a/drivers/ata/sata_inic162x.c
+++ b/drivers/ata/sata_inic162x.c
@@ -585,7 +585,7 @@ static struct ata_port_operations inic_port_ops = {
585}; 585};
586 586
587static struct ata_port_info inic_port_info = { 587static struct ata_port_info inic_port_info = {
588 /* For some reason, ATA_PROT_ATAPI is broken on this 588 /* For some reason, ATAPI_PROT_PIO is broken on this
589 * controller, and no, PIO_POLLING does't fix it. It somehow 589 * controller, and no, PIO_POLLING does't fix it. It somehow
590 * manages to report the wrong ireason and ignoring ireason 590 * manages to report the wrong ireason and ignoring ireason
591 * results in machine lock up. Tell libata to always prefer 591 * results in machine lock up. Tell libata to always prefer
diff --git a/drivers/ata/sata_mv.c b/drivers/ata/sata_mv.c
index 37b850ae0845..7e72463a90eb 100644
--- a/drivers/ata/sata_mv.c
+++ b/drivers/ata/sata_mv.c
@@ -1136,9 +1136,10 @@ static void mv_fill_sg(struct ata_queued_cmd *qc)
1136 struct mv_port_priv *pp = qc->ap->private_data; 1136 struct mv_port_priv *pp = qc->ap->private_data;
1137 struct scatterlist *sg; 1137 struct scatterlist *sg;
1138 struct mv_sg *mv_sg, *last_sg = NULL; 1138 struct mv_sg *mv_sg, *last_sg = NULL;
1139 unsigned int si;
1139 1140
1140 mv_sg = pp->sg_tbl; 1141 mv_sg = pp->sg_tbl;
1141 ata_for_each_sg(sg, qc) { 1142 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1142 dma_addr_t addr = sg_dma_address(sg); 1143 dma_addr_t addr = sg_dma_address(sg);
1143 u32 sg_len = sg_dma_len(sg); 1144 u32 sg_len = sg_dma_len(sg);
1144 1145
diff --git a/drivers/ata/sata_nv.c b/drivers/ata/sata_nv.c
index ed5dc7cb50cd..a0f98fdab7a0 100644
--- a/drivers/ata/sata_nv.c
+++ b/drivers/ata/sata_nv.c
@@ -1336,21 +1336,18 @@ static void nv_adma_fill_aprd(struct ata_queued_cmd *qc,
1336static void nv_adma_fill_sg(struct ata_queued_cmd *qc, struct nv_adma_cpb *cpb) 1336static void nv_adma_fill_sg(struct ata_queued_cmd *qc, struct nv_adma_cpb *cpb)
1337{ 1337{
1338 struct nv_adma_port_priv *pp = qc->ap->private_data; 1338 struct nv_adma_port_priv *pp = qc->ap->private_data;
1339 unsigned int idx;
1340 struct nv_adma_prd *aprd; 1339 struct nv_adma_prd *aprd;
1341 struct scatterlist *sg; 1340 struct scatterlist *sg;
1341 unsigned int si;
1342 1342
1343 VPRINTK("ENTER\n"); 1343 VPRINTK("ENTER\n");
1344 1344
1345 idx = 0; 1345 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1346 1346 aprd = (si < 5) ? &cpb->aprd[si] :
1347 ata_for_each_sg(sg, qc) { 1347 &pp->aprd[NV_ADMA_SGTBL_LEN * qc->tag + (si-5)];
1348 aprd = (idx < 5) ? &cpb->aprd[idx] : 1348 nv_adma_fill_aprd(qc, sg, si, aprd);
1349 &pp->aprd[NV_ADMA_SGTBL_LEN * qc->tag + (idx-5)];
1350 nv_adma_fill_aprd(qc, sg, idx, aprd);
1351 idx++;
1352 } 1349 }
1353 if (idx > 5) 1350 if (si > 5)
1354 cpb->next_aprd = cpu_to_le64(((u64)(pp->aprd_dma + NV_ADMA_SGTBL_SZ * qc->tag))); 1351 cpb->next_aprd = cpu_to_le64(((u64)(pp->aprd_dma + NV_ADMA_SGTBL_SZ * qc->tag)));
1355 else 1352 else
1356 cpb->next_aprd = cpu_to_le64(0); 1353 cpb->next_aprd = cpu_to_le64(0);
@@ -1995,17 +1992,14 @@ static void nv_swncq_fill_sg(struct ata_queued_cmd *qc)
1995{ 1992{
1996 struct ata_port *ap = qc->ap; 1993 struct ata_port *ap = qc->ap;
1997 struct scatterlist *sg; 1994 struct scatterlist *sg;
1998 unsigned int idx;
1999 struct nv_swncq_port_priv *pp = ap->private_data; 1995 struct nv_swncq_port_priv *pp = ap->private_data;
2000 struct ata_prd *prd; 1996 struct ata_prd *prd;
2001 1997 unsigned int si, idx;
2002 WARN_ON(qc->__sg == NULL);
2003 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
2004 1998
2005 prd = pp->prd + ATA_MAX_PRD * qc->tag; 1999 prd = pp->prd + ATA_MAX_PRD * qc->tag;
2006 2000
2007 idx = 0; 2001 idx = 0;
2008 ata_for_each_sg(sg, qc) { 2002 for_each_sg(qc->sg, sg, qc->n_elem, si) {
2009 u32 addr, offset; 2003 u32 addr, offset;
2010 u32 sg_len, len; 2004 u32 sg_len, len;
2011 2005
@@ -2027,8 +2021,7 @@ static void nv_swncq_fill_sg(struct ata_queued_cmd *qc)
2027 } 2021 }
2028 } 2022 }
2029 2023
2030 if (idx) 2024 prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2031 prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2032} 2025}
2033 2026
2034static unsigned int nv_swncq_issue_atacmd(struct ata_port *ap, 2027static unsigned int nv_swncq_issue_atacmd(struct ata_port *ap,
diff --git a/drivers/ata/sata_promise.c b/drivers/ata/sata_promise.c
index 7914def54fa3..a07d319f6e8c 100644
--- a/drivers/ata/sata_promise.c
+++ b/drivers/ata/sata_promise.c
@@ -450,19 +450,19 @@ static void pdc_atapi_pkt(struct ata_queued_cmd *qc)
450 struct pdc_port_priv *pp = ap->private_data; 450 struct pdc_port_priv *pp = ap->private_data;
451 u8 *buf = pp->pkt; 451 u8 *buf = pp->pkt;
452 u32 *buf32 = (u32 *) buf; 452 u32 *buf32 = (u32 *) buf;
453 unsigned int dev_sel, feature, nbytes; 453 unsigned int dev_sel, feature;
454 454
455 /* set control bits (byte 0), zero delay seq id (byte 3), 455 /* set control bits (byte 0), zero delay seq id (byte 3),
456 * and seq id (byte 2) 456 * and seq id (byte 2)
457 */ 457 */
458 switch (qc->tf.protocol) { 458 switch (qc->tf.protocol) {
459 case ATA_PROT_ATAPI_DMA: 459 case ATAPI_PROT_DMA:
460 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) 460 if (!(qc->tf.flags & ATA_TFLAG_WRITE))
461 buf32[0] = cpu_to_le32(PDC_PKT_READ); 461 buf32[0] = cpu_to_le32(PDC_PKT_READ);
462 else 462 else
463 buf32[0] = 0; 463 buf32[0] = 0;
464 break; 464 break;
465 case ATA_PROT_ATAPI_NODATA: 465 case ATAPI_PROT_NODATA:
466 buf32[0] = cpu_to_le32(PDC_PKT_NODATA); 466 buf32[0] = cpu_to_le32(PDC_PKT_NODATA);
467 break; 467 break;
468 default: 468 default:
@@ -473,45 +473,37 @@ static void pdc_atapi_pkt(struct ata_queued_cmd *qc)
473 buf32[2] = 0; /* no next-packet */ 473 buf32[2] = 0; /* no next-packet */
474 474
475 /* select drive */ 475 /* select drive */
476 if (sata_scr_valid(&ap->link)) { 476 if (sata_scr_valid(&ap->link))
477 dev_sel = PDC_DEVICE_SATA; 477 dev_sel = PDC_DEVICE_SATA;
478 } else { 478 else
479 dev_sel = ATA_DEVICE_OBS; 479 dev_sel = qc->tf.device;
480 if (qc->dev->devno != 0) 480
481 dev_sel |= ATA_DEV1;
482 }
483 buf[12] = (1 << 5) | ATA_REG_DEVICE; 481 buf[12] = (1 << 5) | ATA_REG_DEVICE;
484 buf[13] = dev_sel; 482 buf[13] = dev_sel;
485 buf[14] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_CLEAR_BSY; 483 buf[14] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_CLEAR_BSY;
486 buf[15] = dev_sel; /* once more, waiting for BSY to clear */ 484 buf[15] = dev_sel; /* once more, waiting for BSY to clear */
487 485
488 buf[16] = (1 << 5) | ATA_REG_NSECT; 486 buf[16] = (1 << 5) | ATA_REG_NSECT;
489 buf[17] = 0x00; 487 buf[17] = qc->tf.nsect;
490 buf[18] = (1 << 5) | ATA_REG_LBAL; 488 buf[18] = (1 << 5) | ATA_REG_LBAL;
491 buf[19] = 0x00; 489 buf[19] = qc->tf.lbal;
492 490
493 /* set feature and byte counter registers */ 491 /* set feature and byte counter registers */
494 if (qc->tf.protocol != ATA_PROT_ATAPI_DMA) { 492 if (qc->tf.protocol != ATAPI_PROT_DMA)
495 feature = PDC_FEATURE_ATAPI_PIO; 493 feature = PDC_FEATURE_ATAPI_PIO;
496 /* set byte counter register to real transfer byte count */ 494 else
497 nbytes = qc->nbytes;
498 if (nbytes > 0xffff)
499 nbytes = 0xffff;
500 } else {
501 feature = PDC_FEATURE_ATAPI_DMA; 495 feature = PDC_FEATURE_ATAPI_DMA;
502 /* set byte counter register to 0 */ 496
503 nbytes = 0;
504 }
505 buf[20] = (1 << 5) | ATA_REG_FEATURE; 497 buf[20] = (1 << 5) | ATA_REG_FEATURE;
506 buf[21] = feature; 498 buf[21] = feature;
507 buf[22] = (1 << 5) | ATA_REG_BYTEL; 499 buf[22] = (1 << 5) | ATA_REG_BYTEL;
508 buf[23] = nbytes & 0xFF; 500 buf[23] = qc->tf.lbam;
509 buf[24] = (1 << 5) | ATA_REG_BYTEH; 501 buf[24] = (1 << 5) | ATA_REG_BYTEH;
510 buf[25] = (nbytes >> 8) & 0xFF; 502 buf[25] = qc->tf.lbah;
511 503
512 /* send ATAPI packet command 0xA0 */ 504 /* send ATAPI packet command 0xA0 */
513 buf[26] = (1 << 5) | ATA_REG_CMD; 505 buf[26] = (1 << 5) | ATA_REG_CMD;
514 buf[27] = ATA_CMD_PACKET; 506 buf[27] = qc->tf.command;
515 507
516 /* select drive and check DRQ */ 508 /* select drive and check DRQ */
517 buf[28] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_WAIT_DRDY; 509 buf[28] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_WAIT_DRDY;
@@ -541,17 +533,15 @@ static void pdc_fill_sg(struct ata_queued_cmd *qc)
541{ 533{
542 struct ata_port *ap = qc->ap; 534 struct ata_port *ap = qc->ap;
543 struct scatterlist *sg; 535 struct scatterlist *sg;
544 unsigned int idx;
545 const u32 SG_COUNT_ASIC_BUG = 41*4; 536 const u32 SG_COUNT_ASIC_BUG = 41*4;
537 unsigned int si, idx;
538 u32 len;
546 539
547 if (!(qc->flags & ATA_QCFLAG_DMAMAP)) 540 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
548 return; 541 return;
549 542
550 WARN_ON(qc->__sg == NULL);
551 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
552
553 idx = 0; 543 idx = 0;
554 ata_for_each_sg(sg, qc) { 544 for_each_sg(qc->sg, sg, qc->n_elem, si) {
555 u32 addr, offset; 545 u32 addr, offset;
556 u32 sg_len, len; 546 u32 sg_len, len;
557 547
@@ -578,29 +568,27 @@ static void pdc_fill_sg(struct ata_queued_cmd *qc)
578 } 568 }
579 } 569 }
580 570
581 if (idx) { 571 len = le32_to_cpu(ap->prd[idx - 1].flags_len);
582 u32 len = le32_to_cpu(ap->prd[idx - 1].flags_len);
583 572
584 if (len > SG_COUNT_ASIC_BUG) { 573 if (len > SG_COUNT_ASIC_BUG) {
585 u32 addr; 574 u32 addr;
586 575
587 VPRINTK("Splitting last PRD.\n"); 576 VPRINTK("Splitting last PRD.\n");
588 577
589 addr = le32_to_cpu(ap->prd[idx - 1].addr); 578 addr = le32_to_cpu(ap->prd[idx - 1].addr);
590 ap->prd[idx - 1].flags_len = cpu_to_le32(len - SG_COUNT_ASIC_BUG); 579 ap->prd[idx - 1].flags_len = cpu_to_le32(len - SG_COUNT_ASIC_BUG);
591 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx - 1, addr, SG_COUNT_ASIC_BUG); 580 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx - 1, addr, SG_COUNT_ASIC_BUG);
592 581
593 addr = addr + len - SG_COUNT_ASIC_BUG; 582 addr = addr + len - SG_COUNT_ASIC_BUG;
594 len = SG_COUNT_ASIC_BUG; 583 len = SG_COUNT_ASIC_BUG;
595 ap->prd[idx].addr = cpu_to_le32(addr); 584 ap->prd[idx].addr = cpu_to_le32(addr);
596 ap->prd[idx].flags_len = cpu_to_le32(len); 585 ap->prd[idx].flags_len = cpu_to_le32(len);
597 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len); 586 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
598 587
599 idx++; 588 idx++;
600 }
601
602 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
603 } 589 }
590
591 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
604} 592}
605 593
606static void pdc_qc_prep(struct ata_queued_cmd *qc) 594static void pdc_qc_prep(struct ata_queued_cmd *qc)
@@ -627,14 +615,14 @@ static void pdc_qc_prep(struct ata_queued_cmd *qc)
627 pdc_pkt_footer(&qc->tf, pp->pkt, i); 615 pdc_pkt_footer(&qc->tf, pp->pkt, i);
628 break; 616 break;
629 617
630 case ATA_PROT_ATAPI: 618 case ATAPI_PROT_PIO:
631 pdc_fill_sg(qc); 619 pdc_fill_sg(qc);
632 break; 620 break;
633 621
634 case ATA_PROT_ATAPI_DMA: 622 case ATAPI_PROT_DMA:
635 pdc_fill_sg(qc); 623 pdc_fill_sg(qc);
636 /*FALLTHROUGH*/ 624 /*FALLTHROUGH*/
637 case ATA_PROT_ATAPI_NODATA: 625 case ATAPI_PROT_NODATA:
638 pdc_atapi_pkt(qc); 626 pdc_atapi_pkt(qc);
639 break; 627 break;
640 628
@@ -754,8 +742,8 @@ static inline unsigned int pdc_host_intr(struct ata_port *ap,
754 switch (qc->tf.protocol) { 742 switch (qc->tf.protocol) {
755 case ATA_PROT_DMA: 743 case ATA_PROT_DMA:
756 case ATA_PROT_NODATA: 744 case ATA_PROT_NODATA:
757 case ATA_PROT_ATAPI_DMA: 745 case ATAPI_PROT_DMA:
758 case ATA_PROT_ATAPI_NODATA: 746 case ATAPI_PROT_NODATA:
759 qc->err_mask |= ac_err_mask(ata_wait_idle(ap)); 747 qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
760 ata_qc_complete(qc); 748 ata_qc_complete(qc);
761 handled = 1; 749 handled = 1;
@@ -900,7 +888,7 @@ static inline void pdc_packet_start(struct ata_queued_cmd *qc)
900static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc) 888static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc)
901{ 889{
902 switch (qc->tf.protocol) { 890 switch (qc->tf.protocol) {
903 case ATA_PROT_ATAPI_NODATA: 891 case ATAPI_PROT_NODATA:
904 if (qc->dev->flags & ATA_DFLAG_CDB_INTR) 892 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
905 break; 893 break;
906 /*FALLTHROUGH*/ 894 /*FALLTHROUGH*/
@@ -908,7 +896,7 @@ static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc)
908 if (qc->tf.flags & ATA_TFLAG_POLLING) 896 if (qc->tf.flags & ATA_TFLAG_POLLING)
909 break; 897 break;
910 /*FALLTHROUGH*/ 898 /*FALLTHROUGH*/
911 case ATA_PROT_ATAPI_DMA: 899 case ATAPI_PROT_DMA:
912 case ATA_PROT_DMA: 900 case ATA_PROT_DMA:
913 pdc_packet_start(qc); 901 pdc_packet_start(qc);
914 return 0; 902 return 0;
@@ -922,16 +910,14 @@ static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc)
922 910
923static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf) 911static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
924{ 912{
925 WARN_ON(tf->protocol == ATA_PROT_DMA || 913 WARN_ON(tf->protocol == ATA_PROT_DMA || tf->protocol == ATAPI_PROT_DMA);
926 tf->protocol == ATA_PROT_ATAPI_DMA);
927 ata_tf_load(ap, tf); 914 ata_tf_load(ap, tf);
928} 915}
929 916
930static void pdc_exec_command_mmio(struct ata_port *ap, 917static void pdc_exec_command_mmio(struct ata_port *ap,
931 const struct ata_taskfile *tf) 918 const struct ata_taskfile *tf)
932{ 919{
933 WARN_ON(tf->protocol == ATA_PROT_DMA || 920 WARN_ON(tf->protocol == ATA_PROT_DMA || tf->protocol == ATAPI_PROT_DMA);
934 tf->protocol == ATA_PROT_ATAPI_DMA);
935 ata_exec_command(ap, tf); 921 ata_exec_command(ap, tf);
936} 922}
937 923
diff --git a/drivers/ata/sata_promise.h b/drivers/ata/sata_promise.h
index 6ee5e190262d..00d6000e546f 100644
--- a/drivers/ata/sata_promise.h
+++ b/drivers/ata/sata_promise.h
@@ -46,7 +46,7 @@ static inline unsigned int pdc_pkt_header(struct ata_taskfile *tf,
46 unsigned int devno, u8 *buf) 46 unsigned int devno, u8 *buf)
47{ 47{
48 u8 dev_reg; 48 u8 dev_reg;
49 u32 *buf32 = (u32 *) buf; 49 __le32 *buf32 = (__le32 *) buf;
50 50
51 /* set control bits (byte 0), zero delay seq id (byte 3), 51 /* set control bits (byte 0), zero delay seq id (byte 3),
52 * and seq id (byte 2) 52 * and seq id (byte 2)
diff --git a/drivers/ata/sata_qstor.c b/drivers/ata/sata_qstor.c
index c68b241805fd..91cc12c82040 100644
--- a/drivers/ata/sata_qstor.c
+++ b/drivers/ata/sata_qstor.c
@@ -287,14 +287,10 @@ static unsigned int qs_fill_sg(struct ata_queued_cmd *qc)
287 struct scatterlist *sg; 287 struct scatterlist *sg;
288 struct ata_port *ap = qc->ap; 288 struct ata_port *ap = qc->ap;
289 struct qs_port_priv *pp = ap->private_data; 289 struct qs_port_priv *pp = ap->private_data;
290 unsigned int nelem;
291 u8 *prd = pp->pkt + QS_CPB_BYTES; 290 u8 *prd = pp->pkt + QS_CPB_BYTES;
291 unsigned int si;
292 292
293 WARN_ON(qc->__sg == NULL); 293 for_each_sg(qc->sg, sg, qc->n_elem, si) {
294 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
295
296 nelem = 0;
297 ata_for_each_sg(sg, qc) {
298 u64 addr; 294 u64 addr;
299 u32 len; 295 u32 len;
300 296
@@ -306,12 +302,11 @@ static unsigned int qs_fill_sg(struct ata_queued_cmd *qc)
306 *(__le32 *)prd = cpu_to_le32(len); 302 *(__le32 *)prd = cpu_to_le32(len);
307 prd += sizeof(u64); 303 prd += sizeof(u64);
308 304
309 VPRINTK("PRD[%u] = (0x%llX, 0x%X)\n", nelem, 305 VPRINTK("PRD[%u] = (0x%llX, 0x%X)\n", si,
310 (unsigned long long)addr, len); 306 (unsigned long long)addr, len);
311 nelem++;
312 } 307 }
313 308
314 return nelem; 309 return si;
315} 310}
316 311
317static void qs_qc_prep(struct ata_queued_cmd *qc) 312static void qs_qc_prep(struct ata_queued_cmd *qc)
@@ -376,7 +371,7 @@ static unsigned int qs_qc_issue(struct ata_queued_cmd *qc)
376 qs_packet_start(qc); 371 qs_packet_start(qc);
377 return 0; 372 return 0;
378 373
379 case ATA_PROT_ATAPI_DMA: 374 case ATAPI_PROT_DMA:
380 BUG(); 375 BUG();
381 break; 376 break;
382 377
diff --git a/drivers/ata/sata_sil.c b/drivers/ata/sata_sil.c
index f5119bf40c24..0b8191b52f97 100644
--- a/drivers/ata/sata_sil.c
+++ b/drivers/ata/sata_sil.c
@@ -416,15 +416,14 @@ static void sil_host_intr(struct ata_port *ap, u32 bmdma2)
416 */ 416 */
417 417
418 /* Check the ATA_DFLAG_CDB_INTR flag is enough here. 418 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
419 * The flag was turned on only for atapi devices. 419 * The flag was turned on only for atapi devices. No
420 * No need to check is_atapi_taskfile(&qc->tf) again. 420 * need to check ata_is_atapi(qc->tf.protocol) again.
421 */ 421 */
422 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) 422 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
423 goto err_hsm; 423 goto err_hsm;
424 break; 424 break;
425 case HSM_ST_LAST: 425 case HSM_ST_LAST:
426 if (qc->tf.protocol == ATA_PROT_DMA || 426 if (ata_is_dma(qc->tf.protocol)) {
427 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
428 /* clear DMA-Start bit */ 427 /* clear DMA-Start bit */
429 ap->ops->bmdma_stop(qc); 428 ap->ops->bmdma_stop(qc);
430 429
@@ -451,8 +450,7 @@ static void sil_host_intr(struct ata_port *ap, u32 bmdma2)
451 /* kick HSM in the ass */ 450 /* kick HSM in the ass */
452 ata_hsm_move(ap, qc, status, 0); 451 ata_hsm_move(ap, qc, status, 0);
453 452
454 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA || 453 if (unlikely(qc->err_mask) && ata_is_dma(qc->tf.protocol))
455 qc->tf.protocol == ATA_PROT_ATAPI_DMA))
456 ata_ehi_push_desc(ehi, "BMDMA2 stat 0x%x", bmdma2); 454 ata_ehi_push_desc(ehi, "BMDMA2 stat 0x%x", bmdma2);
457 455
458 return; 456 return;
diff --git a/drivers/ata/sata_sil24.c b/drivers/ata/sata_sil24.c
index 864c1c1b8511..b4b1f91ea693 100644
--- a/drivers/ata/sata_sil24.c
+++ b/drivers/ata/sata_sil24.c
@@ -813,8 +813,9 @@ static inline void sil24_fill_sg(struct ata_queued_cmd *qc,
813{ 813{
814 struct scatterlist *sg; 814 struct scatterlist *sg;
815 struct sil24_sge *last_sge = NULL; 815 struct sil24_sge *last_sge = NULL;
816 unsigned int si;
816 817
817 ata_for_each_sg(sg, qc) { 818 for_each_sg(qc->sg, sg, qc->n_elem, si) {
818 sge->addr = cpu_to_le64(sg_dma_address(sg)); 819 sge->addr = cpu_to_le64(sg_dma_address(sg));
819 sge->cnt = cpu_to_le32(sg_dma_len(sg)); 820 sge->cnt = cpu_to_le32(sg_dma_len(sg));
820 sge->flags = 0; 821 sge->flags = 0;
@@ -823,8 +824,7 @@ static inline void sil24_fill_sg(struct ata_queued_cmd *qc,
823 sge++; 824 sge++;
824 } 825 }
825 826
826 if (likely(last_sge)) 827 last_sge->flags = cpu_to_le32(SGE_TRM);
827 last_sge->flags = cpu_to_le32(SGE_TRM);
828} 828}
829 829
830static int sil24_qc_defer(struct ata_queued_cmd *qc) 830static int sil24_qc_defer(struct ata_queued_cmd *qc)
@@ -852,9 +852,7 @@ static int sil24_qc_defer(struct ata_queued_cmd *qc)
852 * set. 852 * set.
853 * 853 *
854 */ 854 */
855 int is_excl = (prot == ATA_PROT_ATAPI || 855 int is_excl = (ata_is_atapi(prot) ||
856 prot == ATA_PROT_ATAPI_NODATA ||
857 prot == ATA_PROT_ATAPI_DMA ||
858 (qc->flags & ATA_QCFLAG_RESULT_TF)); 856 (qc->flags & ATA_QCFLAG_RESULT_TF));
859 857
860 if (unlikely(ap->excl_link)) { 858 if (unlikely(ap->excl_link)) {
@@ -885,35 +883,21 @@ static void sil24_qc_prep(struct ata_queued_cmd *qc)
885 883
886 cb = &pp->cmd_block[sil24_tag(qc->tag)]; 884 cb = &pp->cmd_block[sil24_tag(qc->tag)];
887 885
888 switch (qc->tf.protocol) { 886 if (!ata_is_atapi(qc->tf.protocol)) {
889 case ATA_PROT_PIO:
890 case ATA_PROT_DMA:
891 case ATA_PROT_NCQ:
892 case ATA_PROT_NODATA:
893 prb = &cb->ata.prb; 887 prb = &cb->ata.prb;
894 sge = cb->ata.sge; 888 sge = cb->ata.sge;
895 break; 889 } else {
896
897 case ATA_PROT_ATAPI:
898 case ATA_PROT_ATAPI_DMA:
899 case ATA_PROT_ATAPI_NODATA:
900 prb = &cb->atapi.prb; 890 prb = &cb->atapi.prb;
901 sge = cb->atapi.sge; 891 sge = cb->atapi.sge;
902 memset(cb->atapi.cdb, 0, 32); 892 memset(cb->atapi.cdb, 0, 32);
903 memcpy(cb->atapi.cdb, qc->cdb, qc->dev->cdb_len); 893 memcpy(cb->atapi.cdb, qc->cdb, qc->dev->cdb_len);
904 894
905 if (qc->tf.protocol != ATA_PROT_ATAPI_NODATA) { 895 if (ata_is_data(qc->tf.protocol)) {
906 if (qc->tf.flags & ATA_TFLAG_WRITE) 896 if (qc->tf.flags & ATA_TFLAG_WRITE)
907 ctrl = PRB_CTRL_PACKET_WRITE; 897 ctrl = PRB_CTRL_PACKET_WRITE;
908 else 898 else
909 ctrl = PRB_CTRL_PACKET_READ; 899 ctrl = PRB_CTRL_PACKET_READ;
910 } 900 }
911 break;
912
913 default:
914 prb = NULL; /* shut up, gcc */
915 sge = NULL;
916 BUG();
917 } 901 }
918 902
919 prb->ctrl = cpu_to_le16(ctrl); 903 prb->ctrl = cpu_to_le16(ctrl);
diff --git a/drivers/ata/sata_sx4.c b/drivers/ata/sata_sx4.c
index 4d857185f33b..e3d56bc6726d 100644
--- a/drivers/ata/sata_sx4.c
+++ b/drivers/ata/sata_sx4.c
@@ -334,7 +334,7 @@ static inline void pdc20621_ata_sg(struct ata_taskfile *tf, u8 *buf,
334{ 334{
335 u32 addr; 335 u32 addr;
336 unsigned int dw = PDC_DIMM_APKT_PRD >> 2; 336 unsigned int dw = PDC_DIMM_APKT_PRD >> 2;
337 u32 *buf32 = (u32 *) buf; 337 __le32 *buf32 = (__le32 *) buf;
338 338
339 /* output ATA packet S/G table */ 339 /* output ATA packet S/G table */
340 addr = PDC_20621_DIMM_BASE + PDC_20621_DIMM_DATA + 340 addr = PDC_20621_DIMM_BASE + PDC_20621_DIMM_DATA +
@@ -356,7 +356,7 @@ static inline void pdc20621_host_sg(struct ata_taskfile *tf, u8 *buf,
356{ 356{
357 u32 addr; 357 u32 addr;
358 unsigned int dw = PDC_DIMM_HPKT_PRD >> 2; 358 unsigned int dw = PDC_DIMM_HPKT_PRD >> 2;
359 u32 *buf32 = (u32 *) buf; 359 __le32 *buf32 = (__le32 *) buf;
360 360
361 /* output Host DMA packet S/G table */ 361 /* output Host DMA packet S/G table */
362 addr = PDC_20621_DIMM_BASE + PDC_20621_DIMM_DATA + 362 addr = PDC_20621_DIMM_BASE + PDC_20621_DIMM_DATA +
@@ -377,7 +377,7 @@ static inline unsigned int pdc20621_ata_pkt(struct ata_taskfile *tf,
377 unsigned int portno) 377 unsigned int portno)
378{ 378{
379 unsigned int i, dw; 379 unsigned int i, dw;
380 u32 *buf32 = (u32 *) buf; 380 __le32 *buf32 = (__le32 *) buf;
381 u8 dev_reg; 381 u8 dev_reg;
382 382
383 unsigned int dimm_sg = PDC_20621_DIMM_BASE + 383 unsigned int dimm_sg = PDC_20621_DIMM_BASE +
@@ -429,7 +429,8 @@ static inline void pdc20621_host_pkt(struct ata_taskfile *tf, u8 *buf,
429 unsigned int portno) 429 unsigned int portno)
430{ 430{
431 unsigned int dw; 431 unsigned int dw;
432 u32 tmp, *buf32 = (u32 *) buf; 432 u32 tmp;
433 __le32 *buf32 = (__le32 *) buf;
433 434
434 unsigned int host_sg = PDC_20621_DIMM_BASE + 435 unsigned int host_sg = PDC_20621_DIMM_BASE +
435 (PDC_DIMM_WINDOW_STEP * portno) + 436 (PDC_DIMM_WINDOW_STEP * portno) +
@@ -473,7 +474,7 @@ static void pdc20621_dma_prep(struct ata_queued_cmd *qc)
473 void __iomem *mmio = ap->host->iomap[PDC_MMIO_BAR]; 474 void __iomem *mmio = ap->host->iomap[PDC_MMIO_BAR];
474 void __iomem *dimm_mmio = ap->host->iomap[PDC_DIMM_BAR]; 475 void __iomem *dimm_mmio = ap->host->iomap[PDC_DIMM_BAR];
475 unsigned int portno = ap->port_no; 476 unsigned int portno = ap->port_no;
476 unsigned int i, idx, total_len = 0, sgt_len; 477 unsigned int i, si, idx, total_len = 0, sgt_len;
477 u32 *buf = (u32 *) &pp->dimm_buf[PDC_DIMM_HEADER_SZ]; 478 u32 *buf = (u32 *) &pp->dimm_buf[PDC_DIMM_HEADER_SZ];
478 479
479 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP)); 480 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
@@ -487,7 +488,7 @@ static void pdc20621_dma_prep(struct ata_queued_cmd *qc)
487 * Build S/G table 488 * Build S/G table
488 */ 489 */
489 idx = 0; 490 idx = 0;
490 ata_for_each_sg(sg, qc) { 491 for_each_sg(qc->sg, sg, qc->n_elem, si) {
491 buf[idx++] = cpu_to_le32(sg_dma_address(sg)); 492 buf[idx++] = cpu_to_le32(sg_dma_address(sg));
492 buf[idx++] = cpu_to_le32(sg_dma_len(sg)); 493 buf[idx++] = cpu_to_le32(sg_dma_len(sg));
493 total_len += sg_dma_len(sg); 494 total_len += sg_dma_len(sg);
@@ -700,7 +701,7 @@ static unsigned int pdc20621_qc_issue_prot(struct ata_queued_cmd *qc)
700 pdc20621_packet_start(qc); 701 pdc20621_packet_start(qc);
701 return 0; 702 return 0;
702 703
703 case ATA_PROT_ATAPI_DMA: 704 case ATAPI_PROT_DMA:
704 BUG(); 705 BUG();
705 break; 706 break;
706 707
diff --git a/drivers/ide/Kconfig b/drivers/ide/Kconfig
index fb06555708a8..ee01e273a537 100644
--- a/drivers/ide/Kconfig
+++ b/drivers/ide/Kconfig
@@ -374,17 +374,6 @@ comment "PCI IDE chipsets support"
374config BLK_DEV_IDEPCI 374config BLK_DEV_IDEPCI
375 bool 375 bool
376 376
377config IDEPCI_SHARE_IRQ
378 bool "Sharing PCI IDE interrupts support"
379 depends on BLK_DEV_IDEPCI
380 help
381 Some ATA/IDE chipsets have hardware support which allows for
382 sharing a single IRQ with other cards. To enable support for
383 this in the ATA/IDE driver, say Y here.
384
385 It is safe to say Y to this question, in most cases.
386 If unsure, say N.
387
388config IDEPCI_PCIBUS_ORDER 377config IDEPCI_PCIBUS_ORDER
389 def_bool BLK_DEV_IDE=y && BLK_DEV_IDEPCI 378 def_bool BLK_DEV_IDE=y && BLK_DEV_IDEPCI
390 379
@@ -707,7 +696,6 @@ config BLK_DEV_SVWKS
707config BLK_DEV_SGIIOC4 696config BLK_DEV_SGIIOC4
708 tristate "Silicon Graphics IOC4 chipset ATA/ATAPI support" 697 tristate "Silicon Graphics IOC4 chipset ATA/ATAPI support"
709 depends on (IA64_SGI_SN2 || IA64_GENERIC) && SGI_IOC4 698 depends on (IA64_SGI_SN2 || IA64_GENERIC) && SGI_IOC4
710 select IDEPCI_SHARE_IRQ
711 select BLK_DEV_IDEDMA_PCI 699 select BLK_DEV_IDEDMA_PCI
712 help 700 help
713 This driver adds PIO & MultiMode DMA-2 support for the SGI IOC4 701 This driver adds PIO & MultiMode DMA-2 support for the SGI IOC4
diff --git a/drivers/ide/arm/icside.c b/drivers/ide/arm/icside.c
index 93f71fcfc04d..673402f4a295 100644
--- a/drivers/ide/arm/icside.c
+++ b/drivers/ide/arm/icside.c
@@ -272,8 +272,6 @@ static void icside_set_dma_mode(ide_drive_t *drive, const u8 xfer_mode)
272 case XFER_SW_DMA_0: 272 case XFER_SW_DMA_0:
273 cycle_time = 480; 273 cycle_time = 480;
274 break; 274 break;
275 default:
276 return;
277 } 275 }
278 276
279 /* 277 /*
diff --git a/drivers/ide/cris/ide-cris.c b/drivers/ide/cris/ide-cris.c
index 476e0d65ed43..325e608d9e62 100644
--- a/drivers/ide/cris/ide-cris.c
+++ b/drivers/ide/cris/ide-cris.c
@@ -747,8 +747,6 @@ static void cris_set_dma_mode(ide_drive_t *drive, const u8 speed)
747 strobe = ATA_DMA2_STROBE; 747 strobe = ATA_DMA2_STROBE;
748 hold = ATA_DMA2_HOLD; 748 hold = ATA_DMA2_HOLD;
749 break; 749 break;
750 default:
751 return;
752 } 750 }
753 751
754 if (speed >= XFER_UDMA_0) 752 if (speed >= XFER_UDMA_0)
diff --git a/drivers/ide/ide-acpi.c b/drivers/ide/ide-acpi.c
index 899d56536e80..e0bb0cfa7bdd 100644
--- a/drivers/ide/ide-acpi.c
+++ b/drivers/ide/ide-acpi.c
@@ -383,27 +383,19 @@ static int taskfile_load_raw(ide_drive_t *drive,
383 gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]); 383 gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]);
384 384
385 memset(&args, 0, sizeof(ide_task_t)); 385 memset(&args, 0, sizeof(ide_task_t));
386 args.command_type = IDE_DRIVE_TASK_NO_DATA;
387 args.data_phase = TASKFILE_NO_DATA;
388 args.handler = &task_no_data_intr;
389 386
390 /* convert gtf to IDE Taskfile */ 387 /* convert gtf to IDE Taskfile */
391 args.tfRegister[1] = gtf->tfa[0]; /* 0x1f1 */ 388 memcpy(&args.tf_array[7], &gtf->tfa, 7);
392 args.tfRegister[2] = gtf->tfa[1]; /* 0x1f2 */ 389 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
393 args.tfRegister[3] = gtf->tfa[2]; /* 0x1f3 */
394 args.tfRegister[4] = gtf->tfa[3]; /* 0x1f4 */
395 args.tfRegister[5] = gtf->tfa[4]; /* 0x1f5 */
396 args.tfRegister[6] = gtf->tfa[5]; /* 0x1f6 */
397 args.tfRegister[7] = gtf->tfa[6]; /* 0x1f7 */
398 390
399 if (ide_noacpitfs) { 391 if (ide_noacpitfs) {
400 DEBPRINT("_GTF execution disabled\n"); 392 DEBPRINT("_GTF execution disabled\n");
401 return err; 393 return err;
402 } 394 }
403 395
404 err = ide_raw_taskfile(drive, &args, NULL); 396 err = ide_no_data_taskfile(drive, &args);
405 if (err) 397 if (err)
406 printk(KERN_ERR "%s: ide_raw_taskfile failed: %u\n", 398 printk(KERN_ERR "%s: ide_no_data_taskfile failed: %u\n",
407 __FUNCTION__, err); 399 __FUNCTION__, err);
408 400
409 return err; 401 return err;
diff --git a/drivers/ide/ide-cd.c b/drivers/ide/ide-cd.c
index c7d77f0ad892..44b033ec0ab0 100644
--- a/drivers/ide/ide-cd.c
+++ b/drivers/ide/ide-cd.c
@@ -917,19 +917,13 @@ static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
917 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY)) 917 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
918 return startstop; 918 return startstop;
919 919
920 /* FIXME: for Virtual DMA we must check harder */
920 if (info->dma) 921 if (info->dma)
921 info->dma = !hwif->dma_setup(drive); 922 info->dma = !hwif->dma_setup(drive);
922 923
923 /* Set up the controller registers. */ 924 /* Set up the controller registers. */
924 /* FIXME: for Virtual DMA we must check harder */ 925 ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL |
925 HWIF(drive)->OUTB(info->dma, IDE_FEATURE_REG); 926 IDE_TFLAG_NO_SELECT_MASK, xferlen, info->dma);
926 HWIF(drive)->OUTB(0, IDE_IREASON_REG);
927 HWIF(drive)->OUTB(0, IDE_SECTOR_REG);
928
929 HWIF(drive)->OUTB(xferlen & 0xff, IDE_BCOUNTL_REG);
930 HWIF(drive)->OUTB(xferlen >> 8 , IDE_BCOUNTH_REG);
931 if (IDE_CONTROL_REG)
932 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
933 927
934 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) { 928 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
935 /* waiting for CDB interrupt, not DMA yet. */ 929 /* waiting for CDB interrupt, not DMA yet. */
diff --git a/drivers/ide/ide-disk.c b/drivers/ide/ide-disk.c
index b1781908e1f2..d8fdd865dea9 100644
--- a/drivers/ide/ide-disk.c
+++ b/drivers/ide/ide-disk.c
@@ -129,6 +129,50 @@ static int lba_capacity_is_ok (struct hd_driveid *id)
129 return 0; /* lba_capacity value may be bad */ 129 return 0; /* lba_capacity value may be bad */
130} 130}
131 131
132static const u8 ide_rw_cmds[] = {
133 WIN_MULTREAD,
134 WIN_MULTWRITE,
135 WIN_MULTREAD_EXT,
136 WIN_MULTWRITE_EXT,
137 WIN_READ,
138 WIN_WRITE,
139 WIN_READ_EXT,
140 WIN_WRITE_EXT,
141 WIN_READDMA,
142 WIN_WRITEDMA,
143 WIN_READDMA_EXT,
144 WIN_WRITEDMA_EXT,
145};
146
147static const u8 ide_data_phases[] = {
148 TASKFILE_MULTI_IN,
149 TASKFILE_MULTI_OUT,
150 TASKFILE_IN,
151 TASKFILE_OUT,
152 TASKFILE_IN_DMA,
153 TASKFILE_OUT_DMA,
154};
155
156static void ide_tf_set_cmd(ide_drive_t *drive, ide_task_t *task, u8 dma)
157{
158 u8 index, lba48, write;
159
160 lba48 = (task->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0;
161 write = (task->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0;
162
163 if (dma)
164 index = drive->vdma ? 4 : 8;
165 else
166 index = drive->mult_count ? 0 : 4;
167
168 task->tf.command = ide_rw_cmds[index + lba48 + write];
169
170 if (dma)
171 index = 8; /* fixup index */
172
173 task->data_phase = ide_data_phases[index / 2 + write];
174}
175
132/* 176/*
133 * __ide_do_rw_disk() issues READ and WRITE commands to a disk, 177 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
134 * using LBA if supported, or CHS otherwise, to address sectors. 178 * using LBA if supported, or CHS otherwise, to address sectors.
@@ -137,11 +181,11 @@ static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
137{ 181{
138 ide_hwif_t *hwif = HWIF(drive); 182 ide_hwif_t *hwif = HWIF(drive);
139 unsigned int dma = drive->using_dma; 183 unsigned int dma = drive->using_dma;
184 u16 nsectors = (u16)rq->nr_sectors;
140 u8 lba48 = (drive->addressing == 1) ? 1 : 0; 185 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
141 task_ioreg_t command = WIN_NOP; 186 ide_task_t task;
142 ata_nsector_t nsectors; 187 struct ide_taskfile *tf = &task.tf;
143 188 ide_startstop_t rc;
144 nsectors.all = (u16) rq->nr_sectors;
145 189
146 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) { 190 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
147 if (block + rq->nr_sectors > 1ULL << 28) 191 if (block + rq->nr_sectors > 1ULL << 28)
@@ -155,121 +199,76 @@ static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
155 ide_map_sg(drive, rq); 199 ide_map_sg(drive, rq);
156 } 200 }
157 201
158 if (IDE_CONTROL_REG) 202 memset(&task, 0, sizeof(task));
159 hwif->OUTB(drive->ctl, IDE_CONTROL_REG); 203 task.tf_flags = IDE_TFLAG_NO_SELECT_MASK; /* FIXME? */
160 204 task.tf_flags |= (IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE);
161 /* FIXME: SELECT_MASK(drive, 0) ? */
162 205
163 if (drive->select.b.lba) { 206 if (drive->select.b.lba) {
164 if (lba48) { 207 if (lba48) {
165 task_ioreg_t tasklets[10];
166
167 pr_debug("%s: LBA=0x%012llx\n", drive->name, 208 pr_debug("%s: LBA=0x%012llx\n", drive->name,
168 (unsigned long long)block); 209 (unsigned long long)block);
169 210
170 tasklets[0] = 0; 211 tf->hob_nsect = (nsectors >> 8) & 0xff;
171 tasklets[1] = 0; 212 tf->hob_lbal = (u8)(block >> 24);
172 tasklets[2] = nsectors.b.low; 213 if (sizeof(block) != 4) {
173 tasklets[3] = nsectors.b.high; 214 tf->hob_lbam = (u8)((u64)block >> 32);
174 tasklets[4] = (task_ioreg_t) block; 215 tf->hob_lbah = (u8)((u64)block >> 40);
175 tasklets[5] = (task_ioreg_t) (block>>8);
176 tasklets[6] = (task_ioreg_t) (block>>16);
177 tasklets[7] = (task_ioreg_t) (block>>24);
178 if (sizeof(block) == 4) {
179 tasklets[8] = (task_ioreg_t) 0;
180 tasklets[9] = (task_ioreg_t) 0;
181 } else {
182 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
183 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
184 } 216 }
217
218 tf->nsect = nsectors & 0xff;
219 tf->lbal = (u8) block;
220 tf->lbam = (u8)(block >> 8);
221 tf->lbah = (u8)(block >> 16);
185#ifdef DEBUG 222#ifdef DEBUG
186 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n", 223 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
187 drive->name, tasklets[3], tasklets[2], 224 drive->name, tf->hob_nsect, tf->nsect,
188 tasklets[9], tasklets[8], tasklets[7], 225 tf->hob_lbah, tf->hob_lbam, tf->hob_lbal,
189 tasklets[6], tasklets[5], tasklets[4]); 226 tf->lbah, tf->lbam, tf->lbal);
190#endif 227#endif
191 hwif->OUTB(tasklets[1], IDE_FEATURE_REG); 228 task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_OUT_HOB);
192 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
193 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
194 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
195 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
196
197 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
198 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
199 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
200 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
201 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
202 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
203 } else { 229 } else {
204 hwif->OUTB(0x00, IDE_FEATURE_REG); 230 tf->nsect = nsectors & 0xff;
205 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG); 231 tf->lbal = block;
206 hwif->OUTB(block, IDE_SECTOR_REG); 232 tf->lbam = block >>= 8;
207 hwif->OUTB(block>>=8, IDE_LCYL_REG); 233 tf->lbah = block >>= 8;
208 hwif->OUTB(block>>=8, IDE_HCYL_REG); 234 tf->device = (block >> 8) & 0xf;
209 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
210 } 235 }
211 } else { 236 } else {
212 unsigned int sect,head,cyl,track; 237 unsigned int sect,head,cyl,track;
213 track = (int)block / drive->sect; 238 track = (int)block / drive->sect;
214 sect = (int)block % drive->sect + 1; 239 sect = (int)block % drive->sect + 1;
215 hwif->OUTB(sect, IDE_SECTOR_REG);
216 head = track % drive->head; 240 head = track % drive->head;
217 cyl = track / drive->head; 241 cyl = track / drive->head;
218 242
219 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect); 243 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
220 244
221 hwif->OUTB(0x00, IDE_FEATURE_REG); 245 tf->nsect = nsectors & 0xff;
222 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG); 246 tf->lbal = sect;
223 hwif->OUTB(cyl, IDE_LCYL_REG); 247 tf->lbam = cyl;
224 hwif->OUTB(cyl>>8, IDE_HCYL_REG); 248 tf->lbah = cyl >> 8;
225 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG); 249 tf->device = head;
226 } 250 }
227 251
228 if (dma) { 252 if (rq_data_dir(rq))
229 if (!hwif->dma_setup(drive)) { 253 task.tf_flags |= IDE_TFLAG_WRITE;
230 if (rq_data_dir(rq)) {
231 command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
232 if (drive->vdma)
233 command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
234 } else {
235 command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
236 if (drive->vdma)
237 command = lba48 ? WIN_READ_EXT: WIN_READ;
238 }
239 hwif->dma_exec_cmd(drive, command);
240 hwif->dma_start(drive);
241 return ide_started;
242 }
243 /* fallback to PIO */
244 ide_init_sg_cmd(drive, rq);
245 }
246 254
247 if (rq_data_dir(rq) == READ) { 255 ide_tf_set_cmd(drive, &task, dma);
256 if (!dma)
257 hwif->data_phase = task.data_phase;
258 task.rq = rq;
248 259
249 if (drive->mult_count) { 260 rc = do_rw_taskfile(drive, &task);
250 hwif->data_phase = TASKFILE_MULTI_IN;
251 command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
252 } else {
253 hwif->data_phase = TASKFILE_IN;
254 command = lba48 ? WIN_READ_EXT : WIN_READ;
255 }
256 261
257 ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL); 262 if (rc == ide_stopped && dma) {
258 return ide_started; 263 /* fallback to PIO */
259 } else { 264 task.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK;
260 if (drive->mult_count) { 265 ide_tf_set_cmd(drive, &task, 0);
261 hwif->data_phase = TASKFILE_MULTI_OUT; 266 hwif->data_phase = task.data_phase;
262 command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE; 267 ide_init_sg_cmd(drive, rq);
263 } else { 268 rc = do_rw_taskfile(drive, &task);
264 hwif->data_phase = TASKFILE_OUT;
265 command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
266 }
267
268 /* FIXME: ->OUTBSYNC ? */
269 hwif->OUTB(command, IDE_COMMAND_REG);
270
271 return pre_task_out_intr(drive, rq);
272 } 269 }
270
271 return rc;
273} 272}
274 273
275/* 274/*
@@ -307,57 +306,29 @@ static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, s
307 * Queries for true maximum capacity of the drive. 306 * Queries for true maximum capacity of the drive.
308 * Returns maximum LBA address (> 0) of the drive, 0 if failed. 307 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
309 */ 308 */
310static unsigned long idedisk_read_native_max_address(ide_drive_t *drive) 309static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
311{ 310{
312 ide_task_t args; 311 ide_task_t args;
313 unsigned long addr = 0; 312 struct ide_taskfile *tf = &args.tf;
313 u64 addr = 0;
314 314
315 /* Create IDE/ATA command request structure */ 315 /* Create IDE/ATA command request structure */
316 memset(&args, 0, sizeof(ide_task_t)); 316 memset(&args, 0, sizeof(ide_task_t));
317 args.tfRegister[IDE_SELECT_OFFSET] = 0x40; 317 if (lba48)
318 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX; 318 tf->command = WIN_READ_NATIVE_MAX_EXT;
319 args.command_type = IDE_DRIVE_TASK_NO_DATA; 319 else
320 args.handler = &task_no_data_intr; 320 tf->command = WIN_READ_NATIVE_MAX;
321 tf->device = ATA_LBA;
322 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
323 if (lba48)
324 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_OUT_HOB);
321 /* submit command request */ 325 /* submit command request */
322 ide_raw_taskfile(drive, &args, NULL); 326 ide_no_data_taskfile(drive, &args);
323 327
324 /* if OK, compute maximum address value */ 328 /* if OK, compute maximum address value */
325 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) { 329 if ((tf->status & 0x01) == 0)
326 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24) 330 addr = ide_get_lba_addr(tf, lba48) + 1;
327 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
328 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
329 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
330 addr++; /* since the return value is (maxlba - 1), we add 1 */
331 }
332 return addr;
333}
334
335static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
336{
337 ide_task_t args;
338 unsigned long long addr = 0;
339
340 /* Create IDE/ATA command request structure */
341 memset(&args, 0, sizeof(ide_task_t));
342 331
343 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
344 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
345 args.command_type = IDE_DRIVE_TASK_NO_DATA;
346 args.handler = &task_no_data_intr;
347 /* submit command request */
348 ide_raw_taskfile(drive, &args, NULL);
349
350 /* if OK, compute maximum address value */
351 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
352 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
353 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
354 args.hobRegister[IDE_SECTOR_OFFSET];
355 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
356 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
357 (args.tfRegister[IDE_SECTOR_OFFSET]);
358 addr = ((__u64)high << 24) | low;
359 addr++; /* since the return value is (maxlba - 1), we add 1 */
360 }
361 return addr; 332 return addr;
362} 333}
363 334
@@ -365,67 +336,37 @@ static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive
365 * Sets maximum virtual LBA address of the drive. 336 * Sets maximum virtual LBA address of the drive.
366 * Returns new maximum virtual LBA address (> 0) or 0 on failure. 337 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
367 */ 338 */
368static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req) 339static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
369{ 340{
370 ide_task_t args; 341 ide_task_t args;
371 unsigned long addr_set = 0; 342 struct ide_taskfile *tf = &args.tf;
372 343 u64 addr_set = 0;
373 addr_req--;
374 /* Create IDE/ATA command request structure */
375 memset(&args, 0, sizeof(ide_task_t));
376 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
377 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
378 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
379 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
380 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
381 args.command_type = IDE_DRIVE_TASK_NO_DATA;
382 args.handler = &task_no_data_intr;
383 /* submit command request */
384 ide_raw_taskfile(drive, &args, NULL);
385 /* if OK, read new maximum address value */
386 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
387 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
388 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
389 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
390 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
391 addr_set++;
392 }
393 return addr_set;
394}
395
396static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
397{
398 ide_task_t args;
399 unsigned long long addr_set = 0;
400 344
401 addr_req--; 345 addr_req--;
402 /* Create IDE/ATA command request structure */ 346 /* Create IDE/ATA command request structure */
403 memset(&args, 0, sizeof(ide_task_t)); 347 memset(&args, 0, sizeof(ide_task_t));
404 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff); 348 tf->lbal = (addr_req >> 0) & 0xff;
405 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff); 349 tf->lbam = (addr_req >>= 8) & 0xff;
406 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff); 350 tf->lbah = (addr_req >>= 8) & 0xff;
407 args.tfRegister[IDE_SELECT_OFFSET] = 0x40; 351 if (lba48) {
408 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT; 352 tf->hob_lbal = (addr_req >>= 8) & 0xff;
409 args.hobRegister[IDE_SECTOR_OFFSET] = (addr_req >>= 8) & 0xff; 353 tf->hob_lbam = (addr_req >>= 8) & 0xff;
410 args.hobRegister[IDE_LCYL_OFFSET] = (addr_req >>= 8) & 0xff; 354 tf->hob_lbah = (addr_req >>= 8) & 0xff;
411 args.hobRegister[IDE_HCYL_OFFSET] = (addr_req >>= 8) & 0xff; 355 tf->command = WIN_SET_MAX_EXT;
412 args.hobRegister[IDE_SELECT_OFFSET] = 0x40; 356 } else {
413 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80); 357 tf->device = (addr_req >>= 8) & 0x0f;
414 args.command_type = IDE_DRIVE_TASK_NO_DATA; 358 tf->command = WIN_SET_MAX;
415 args.handler = &task_no_data_intr; 359 }
360 tf->device |= ATA_LBA;
361 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
362 if (lba48)
363 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_OUT_HOB);
416 /* submit command request */ 364 /* submit command request */
417 ide_raw_taskfile(drive, &args, NULL); 365 ide_no_data_taskfile(drive, &args);
418 /* if OK, compute maximum address value */ 366 /* if OK, compute maximum address value */
419 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) { 367 if ((tf->status & 0x01) == 0)
420 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) | 368 addr_set = ide_get_lba_addr(tf, lba48) + 1;
421 (args.hobRegister[IDE_LCYL_OFFSET] << 8) | 369
422 args.hobRegister[IDE_SECTOR_OFFSET];
423 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
424 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
425 (args.tfRegister[IDE_SECTOR_OFFSET]);
426 addr_set = ((__u64)high << 24) | low;
427 addr_set++;
428 }
429 return addr_set; 370 return addr_set;
430} 371}
431 372
@@ -471,10 +412,8 @@ static void idedisk_check_hpa(ide_drive_t *drive)
471 int lba48 = idedisk_supports_lba48(drive->id); 412 int lba48 = idedisk_supports_lba48(drive->id);
472 413
473 capacity = drive->capacity64; 414 capacity = drive->capacity64;
474 if (lba48) 415
475 set_max = idedisk_read_native_max_address_ext(drive); 416 set_max = idedisk_read_native_max_address(drive, lba48);
476 else
477 set_max = idedisk_read_native_max_address(drive);
478 417
479 if (ide_in_drive_list(drive->id, hpa_list)) { 418 if (ide_in_drive_list(drive->id, hpa_list)) {
480 /* 419 /*
@@ -495,10 +434,8 @@ static void idedisk_check_hpa(ide_drive_t *drive)
495 capacity, sectors_to_MB(capacity), 434 capacity, sectors_to_MB(capacity),
496 set_max, sectors_to_MB(set_max)); 435 set_max, sectors_to_MB(set_max));
497 436
498 if (lba48) 437 set_max = idedisk_set_max_address(drive, set_max, lba48);
499 set_max = idedisk_set_max_address_ext(drive, set_max); 438
500 else
501 set_max = idedisk_set_max_address(drive, set_max);
502 if (set_max) { 439 if (set_max) {
503 drive->capacity64 = set_max; 440 drive->capacity64 = set_max;
504 printk(KERN_INFO "%s: Host Protected Area disabled.\n", 441 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
@@ -556,32 +493,32 @@ static sector_t idedisk_capacity (ide_drive_t *drive)
556static int smart_enable(ide_drive_t *drive) 493static int smart_enable(ide_drive_t *drive)
557{ 494{
558 ide_task_t args; 495 ide_task_t args;
496 struct ide_taskfile *tf = &args.tf;
559 497
560 memset(&args, 0, sizeof(ide_task_t)); 498 memset(&args, 0, sizeof(ide_task_t));
561 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE; 499 tf->feature = SMART_ENABLE;
562 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS; 500 tf->lbam = SMART_LCYL_PASS;
563 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS; 501 tf->lbah = SMART_HCYL_PASS;
564 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART; 502 tf->command = WIN_SMART;
565 args.command_type = IDE_DRIVE_TASK_NO_DATA; 503 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
566 args.handler = &task_no_data_intr; 504 return ide_no_data_taskfile(drive, &args);
567 return ide_raw_taskfile(drive, &args, NULL);
568} 505}
569 506
570static int get_smart_data(ide_drive_t *drive, u8 *buf, u8 sub_cmd) 507static int get_smart_data(ide_drive_t *drive, u8 *buf, u8 sub_cmd)
571{ 508{
572 ide_task_t args; 509 ide_task_t args;
510 struct ide_taskfile *tf = &args.tf;
573 511
574 memset(&args, 0, sizeof(ide_task_t)); 512 memset(&args, 0, sizeof(ide_task_t));
575 args.tfRegister[IDE_FEATURE_OFFSET] = sub_cmd; 513 tf->feature = sub_cmd;
576 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01; 514 tf->nsect = 0x01;
577 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS; 515 tf->lbam = SMART_LCYL_PASS;
578 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS; 516 tf->lbah = SMART_HCYL_PASS;
579 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART; 517 tf->command = WIN_SMART;
580 args.command_type = IDE_DRIVE_TASK_IN; 518 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
581 args.data_phase = TASKFILE_IN; 519 args.data_phase = TASKFILE_IN;
582 args.handler = &task_in_intr;
583 (void) smart_enable(drive); 520 (void) smart_enable(drive);
584 return ide_raw_taskfile(drive, &args, buf); 521 return ide_raw_taskfile(drive, &args, buf, 1);
585} 522}
586 523
587static int proc_idedisk_read_cache 524static int proc_idedisk_read_cache
@@ -659,19 +596,20 @@ static ide_proc_entry_t idedisk_proc[] = {
659static void idedisk_prepare_flush(struct request_queue *q, struct request *rq) 596static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
660{ 597{
661 ide_drive_t *drive = q->queuedata; 598 ide_drive_t *drive = q->queuedata;
599 ide_task_t task;
662 600
663 memset(rq->cmd, 0, sizeof(rq->cmd)); 601 memset(&task, 0, sizeof(task));
664
665 if (ide_id_has_flush_cache_ext(drive->id) && 602 if (ide_id_has_flush_cache_ext(drive->id) &&
666 (drive->capacity64 >= (1UL << 28))) 603 (drive->capacity64 >= (1UL << 28)))
667 rq->cmd[0] = WIN_FLUSH_CACHE_EXT; 604 task.tf.command = WIN_FLUSH_CACHE_EXT;
668 else 605 else
669 rq->cmd[0] = WIN_FLUSH_CACHE; 606 task.tf.command = WIN_FLUSH_CACHE;
670 607 task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
608 task.data_phase = TASKFILE_NO_DATA;
671 609
672 rq->cmd_type = REQ_TYPE_ATA_TASK; 610 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
673 rq->cmd_flags |= REQ_SOFTBARRIER; 611 rq->cmd_flags |= REQ_SOFTBARRIER;
674 rq->buffer = rq->cmd; 612 rq->special = &task;
675} 613}
676 614
677/* 615/*
@@ -753,12 +691,11 @@ static int write_cache(ide_drive_t *drive, int arg)
753 691
754 if (ide_id_has_flush_cache(drive->id)) { 692 if (ide_id_has_flush_cache(drive->id)) {
755 memset(&args, 0, sizeof(ide_task_t)); 693 memset(&args, 0, sizeof(ide_task_t));
756 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? 694 args.tf.feature = arg ?
757 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE; 695 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
758 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES; 696 args.tf.command = WIN_SETFEATURES;
759 args.command_type = IDE_DRIVE_TASK_NO_DATA; 697 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
760 args.handler = &task_no_data_intr; 698 err = ide_no_data_taskfile(drive, &args);
761 err = ide_raw_taskfile(drive, &args, NULL);
762 if (err == 0) 699 if (err == 0)
763 drive->wcache = arg; 700 drive->wcache = arg;
764 } 701 }
@@ -774,12 +711,11 @@ static int do_idedisk_flushcache (ide_drive_t *drive)
774 711
775 memset(&args, 0, sizeof(ide_task_t)); 712 memset(&args, 0, sizeof(ide_task_t));
776 if (ide_id_has_flush_cache_ext(drive->id)) 713 if (ide_id_has_flush_cache_ext(drive->id))
777 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT; 714 args.tf.command = WIN_FLUSH_CACHE_EXT;
778 else 715 else
779 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE; 716 args.tf.command = WIN_FLUSH_CACHE;
780 args.command_type = IDE_DRIVE_TASK_NO_DATA; 717 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
781 args.handler = &task_no_data_intr; 718 return ide_no_data_taskfile(drive, &args);
782 return ide_raw_taskfile(drive, &args, NULL);
783} 719}
784 720
785static int set_acoustic (ide_drive_t *drive, int arg) 721static int set_acoustic (ide_drive_t *drive, int arg)
@@ -790,13 +726,11 @@ static int set_acoustic (ide_drive_t *drive, int arg)
790 return -EINVAL; 726 return -EINVAL;
791 727
792 memset(&args, 0, sizeof(ide_task_t)); 728 memset(&args, 0, sizeof(ide_task_t));
793 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM : 729 args.tf.feature = arg ? SETFEATURES_EN_AAM : SETFEATURES_DIS_AAM;
794 SETFEATURES_DIS_AAM; 730 args.tf.nsect = arg;
795 args.tfRegister[IDE_NSECTOR_OFFSET] = arg; 731 args.tf.command = WIN_SETFEATURES;
796 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES; 732 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
797 args.command_type = IDE_DRIVE_TASK_NO_DATA; 733 ide_no_data_taskfile(drive, &args);
798 args.handler = &task_no_data_intr;
799 ide_raw_taskfile(drive, &args, NULL);
800 drive->acoustic = arg; 734 drive->acoustic = arg;
801 return 0; 735 return 0;
802} 736}
@@ -1057,16 +991,15 @@ static int idedisk_open(struct inode *inode, struct file *filp)
1057 if (drive->removable && idkp->openers == 1) { 991 if (drive->removable && idkp->openers == 1) {
1058 ide_task_t args; 992 ide_task_t args;
1059 memset(&args, 0, sizeof(ide_task_t)); 993 memset(&args, 0, sizeof(ide_task_t));
1060 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK; 994 args.tf.command = WIN_DOORLOCK;
1061 args.command_type = IDE_DRIVE_TASK_NO_DATA; 995 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
1062 args.handler = &task_no_data_intr;
1063 check_disk_change(inode->i_bdev); 996 check_disk_change(inode->i_bdev);
1064 /* 997 /*
1065 * Ignore the return code from door_lock, 998 * Ignore the return code from door_lock,
1066 * since the open() has already succeeded, 999 * since the open() has already succeeded,
1067 * and the door_lock is irrelevant at this point. 1000 * and the door_lock is irrelevant at this point.
1068 */ 1001 */
1069 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL)) 1002 if (drive->doorlocking && ide_no_data_taskfile(drive, &args))
1070 drive->doorlocking = 0; 1003 drive->doorlocking = 0;
1071 } 1004 }
1072 return 0; 1005 return 0;
@@ -1084,10 +1017,9 @@ static int idedisk_release(struct inode *inode, struct file *filp)
1084 if (drive->removable && idkp->openers == 1) { 1017 if (drive->removable && idkp->openers == 1) {
1085 ide_task_t args; 1018 ide_task_t args;
1086 memset(&args, 0, sizeof(ide_task_t)); 1019 memset(&args, 0, sizeof(ide_task_t));
1087 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK; 1020 args.tf.command = WIN_DOORUNLOCK;
1088 args.command_type = IDE_DRIVE_TASK_NO_DATA; 1021 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
1089 args.handler = &task_no_data_intr; 1022 if (drive->doorlocking && ide_no_data_taskfile(drive, &args))
1090 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1091 drive->doorlocking = 0; 1023 drive->doorlocking = 0;
1092 } 1024 }
1093 1025
diff --git a/drivers/ide/ide-dma.c b/drivers/ide/ide-dma.c
index 4703837bf1fc..18c78ad2b31e 100644
--- a/drivers/ide/ide-dma.c
+++ b/drivers/ide/ide-dma.c
@@ -491,10 +491,6 @@ EXPORT_SYMBOL(ide_dma_host_on);
491 491
492int __ide_dma_on (ide_drive_t *drive) 492int __ide_dma_on (ide_drive_t *drive)
493{ 493{
494 /* consult the list of known "bad" drives */
495 if (__ide_dma_bad_drive(drive))
496 return 1;
497
498 drive->using_dma = 1; 494 drive->using_dma = 1;
499 ide_toggle_bounce(drive, 1); 495 ide_toggle_bounce(drive, 1);
500 496
@@ -827,22 +823,19 @@ int ide_set_dma(ide_drive_t *drive)
827 ide_hwif_t *hwif = drive->hwif; 823 ide_hwif_t *hwif = drive->hwif;
828 int rc; 824 int rc;
829 825
830 rc = ide_dma_check(drive); 826 /*
827 * Force DMAing for the beginning of the check.
828 * Some chipsets appear to do interesting
829 * things, if not checked and cleared.
830 * PARANOIA!!!
831 */
832 hwif->dma_off_quietly(drive);
831 833
832 switch(rc) { 834 rc = ide_dma_check(drive);
833 case -1: /* DMA needs to be disabled */ 835 if (rc)
834 hwif->dma_off_quietly(drive); 836 return rc;
835 return -1;
836 case 0: /* DMA needs to be enabled */
837 return hwif->ide_dma_on(drive);
838 case 1: /* DMA setting cannot be changed */
839 break;
840 default:
841 BUG();
842 break;
843 }
844 837
845 return rc; 838 return hwif->ide_dma_on(drive);
846} 839}
847 840
848#ifdef CONFIG_BLK_DEV_IDEDMA_PCI 841#ifdef CONFIG_BLK_DEV_IDEDMA_PCI
@@ -968,11 +961,6 @@ void ide_setup_dma(ide_hwif_t *hwif, unsigned long base, unsigned num_ports)
968 961
969 hwif->dma_base = base; 962 hwif->dma_base = base;
970 963
971 if (hwif->mate)
972 hwif->dma_master = hwif->channel ? hwif->mate->dma_base : base;
973 else
974 hwif->dma_master = base;
975
976 if (!(hwif->dma_command)) 964 if (!(hwif->dma_command))
977 hwif->dma_command = hwif->dma_base; 965 hwif->dma_command = hwif->dma_base;
978 if (!(hwif->dma_vendor1)) 966 if (!(hwif->dma_vendor1))
@@ -1014,8 +1002,6 @@ void ide_setup_dma(ide_hwif_t *hwif, unsigned long base, unsigned num_ports)
1014 hwif->drives[1].name, (dma_stat & 0x40) ? "DMA" : "pio"); 1002 hwif->drives[1].name, (dma_stat & 0x40) ? "DMA" : "pio");
1015 } 1003 }
1016 printk("\n"); 1004 printk("\n");
1017
1018 BUG_ON(!hwif->dma_master);
1019} 1005}
1020 1006
1021EXPORT_SYMBOL_GPL(ide_setup_dma); 1007EXPORT_SYMBOL_GPL(ide_setup_dma);
diff --git a/drivers/ide/ide-floppy.c b/drivers/ide/ide-floppy.c
index 04a357808f2e..ff8232ef9659 100644
--- a/drivers/ide/ide-floppy.c
+++ b/drivers/ide/ide-floppy.c
@@ -369,27 +369,6 @@ typedef struct ide_floppy_obj {
369#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602 369#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
370#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603 370#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
371 371
372#if 0
373/*
374 * Special requests for our block device strategy routine.
375 */
376#define IDEFLOPPY_FIRST_RQ 90
377
378/*
379 * IDEFLOPPY_PC_RQ is used to queue a packet command in the request queue.
380 */
381#define IDEFLOPPY_PC_RQ 90
382
383#define IDEFLOPPY_LAST_RQ 90
384
385/*
386 * A macro which can be used to check if a given request command
387 * originated in the driver or in the buffer cache layer.
388 */
389#define IDEFLOPPY_RQ_CMD(cmd) ((cmd >= IDEFLOPPY_FIRST_RQ) && (cmd <= IDEFLOPPY_LAST_RQ))
390
391#endif
392
393/* 372/*
394 * Error codes which are returned in rq->errors to the higher part 373 * Error codes which are returned in rq->errors to the higher part
395 * of the driver. 374 * of the driver.
@@ -793,9 +772,8 @@ static void idefloppy_retry_pc (ide_drive_t *drive)
793{ 772{
794 idefloppy_pc_t *pc; 773 idefloppy_pc_t *pc;
795 struct request *rq; 774 struct request *rq;
796 atapi_error_t error;
797 775
798 error.all = HWIF(drive)->INB(IDE_ERROR_REG); 776 (void)drive->hwif->INB(IDE_ERROR_REG);
799 pc = idefloppy_next_pc_storage(drive); 777 pc = idefloppy_next_pc_storage(drive);
800 rq = idefloppy_next_rq_storage(drive); 778 rq = idefloppy_next_rq_storage(drive);
801 idefloppy_create_request_sense_cmd(pc); 779 idefloppy_create_request_sense_cmd(pc);
@@ -809,12 +787,12 @@ static void idefloppy_retry_pc (ide_drive_t *drive)
809static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive) 787static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
810{ 788{
811 idefloppy_floppy_t *floppy = drive->driver_data; 789 idefloppy_floppy_t *floppy = drive->driver_data;
812 atapi_status_t status; 790 ide_hwif_t *hwif = drive->hwif;
813 atapi_bcount_t bcount;
814 atapi_ireason_t ireason;
815 idefloppy_pc_t *pc = floppy->pc; 791 idefloppy_pc_t *pc = floppy->pc;
816 struct request *rq = pc->rq; 792 struct request *rq = pc->rq;
817 unsigned int temp; 793 unsigned int temp;
794 u16 bcount;
795 u8 stat, ireason;
818 796
819 debug_log(KERN_INFO "ide-floppy: Reached %s interrupt handler\n", 797 debug_log(KERN_INFO "ide-floppy: Reached %s interrupt handler\n",
820 __FUNCTION__); 798 __FUNCTION__);
@@ -830,16 +808,16 @@ static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
830 } 808 }
831 809
832 /* Clear the interrupt */ 810 /* Clear the interrupt */
833 status.all = HWIF(drive)->INB(IDE_STATUS_REG); 811 stat = drive->hwif->INB(IDE_STATUS_REG);
834 812
835 if (!status.b.drq) { /* No more interrupts */ 813 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
836 debug_log(KERN_INFO "Packet command completed, %d bytes " 814 debug_log(KERN_INFO "Packet command completed, %d bytes "
837 "transferred\n", pc->actually_transferred); 815 "transferred\n", pc->actually_transferred);
838 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags); 816 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
839 817
840 local_irq_enable_in_hardirq(); 818 local_irq_enable_in_hardirq();
841 819
842 if (status.b.check || test_bit(PC_DMA_ERROR, &pc->flags)) { 820 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
843 /* Error detected */ 821 /* Error detected */
844 debug_log(KERN_INFO "ide-floppy: %s: I/O error\n", 822 debug_log(KERN_INFO "ide-floppy: %s: I/O error\n",
845 drive->name); 823 drive->name);
@@ -870,32 +848,32 @@ static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
870 } 848 }
871 849
872 /* Get the number of bytes to transfer */ 850 /* Get the number of bytes to transfer */
873 bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG); 851 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
874 bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG); 852 hwif->INB(IDE_BCOUNTL_REG);
875 /* on this interrupt */ 853 /* on this interrupt */
876 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG); 854 ireason = hwif->INB(IDE_IREASON_REG);
877 855
878 if (ireason.b.cod) { 856 if (ireason & CD) {
879 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n"); 857 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
880 return ide_do_reset(drive); 858 return ide_do_reset(drive);
881 } 859 }
882 if (ireason.b.io == test_bit(PC_WRITING, &pc->flags)) { 860 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
883 /* Hopefully, we will never get here */ 861 /* Hopefully, we will never get here */
884 printk(KERN_ERR "ide-floppy: We wanted to %s, ", 862 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
885 ireason.b.io ? "Write":"Read"); 863 (ireason & IO) ? "Write" : "Read");
886 printk(KERN_ERR "but the floppy wants us to %s !\n", 864 printk(KERN_ERR "but the floppy wants us to %s !\n",
887 ireason.b.io ? "Read":"Write"); 865 (ireason & IO) ? "Read" : "Write");
888 return ide_do_reset(drive); 866 return ide_do_reset(drive);
889 } 867 }
890 if (!test_bit(PC_WRITING, &pc->flags)) { 868 if (!test_bit(PC_WRITING, &pc->flags)) {
891 /* Reading - Check that we have enough space */ 869 /* Reading - Check that we have enough space */
892 temp = pc->actually_transferred + bcount.all; 870 temp = pc->actually_transferred + bcount;
893 if (temp > pc->request_transfer) { 871 if (temp > pc->request_transfer) {
894 if (temp > pc->buffer_size) { 872 if (temp > pc->buffer_size) {
895 printk(KERN_ERR "ide-floppy: The floppy wants " 873 printk(KERN_ERR "ide-floppy: The floppy wants "
896 "to send us more data than expected " 874 "to send us more data than expected "
897 "- discarding data\n"); 875 "- discarding data\n");
898 idefloppy_discard_data(drive,bcount.all); 876 idefloppy_discard_data(drive, bcount);
899 BUG_ON(HWGROUP(drive)->handler != NULL); 877 BUG_ON(HWGROUP(drive)->handler != NULL);
900 ide_set_handler(drive, 878 ide_set_handler(drive,
901 &idefloppy_pc_intr, 879 &idefloppy_pc_intr,
@@ -911,23 +889,21 @@ static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
911 if (test_bit(PC_WRITING, &pc->flags)) { 889 if (test_bit(PC_WRITING, &pc->flags)) {
912 if (pc->buffer != NULL) 890 if (pc->buffer != NULL)
913 /* Write the current buffer */ 891 /* Write the current buffer */
914 HWIF(drive)->atapi_output_bytes(drive, 892 hwif->atapi_output_bytes(drive, pc->current_position,
915 pc->current_position, 893 bcount);
916 bcount.all);
917 else 894 else
918 idefloppy_output_buffers(drive, pc, bcount.all); 895 idefloppy_output_buffers(drive, pc, bcount);
919 } else { 896 } else {
920 if (pc->buffer != NULL) 897 if (pc->buffer != NULL)
921 /* Read the current buffer */ 898 /* Read the current buffer */
922 HWIF(drive)->atapi_input_bytes(drive, 899 hwif->atapi_input_bytes(drive, pc->current_position,
923 pc->current_position, 900 bcount);
924 bcount.all);
925 else 901 else
926 idefloppy_input_buffers(drive, pc, bcount.all); 902 idefloppy_input_buffers(drive, pc, bcount);
927 } 903 }
928 /* Update the current position */ 904 /* Update the current position */
929 pc->actually_transferred += bcount.all; 905 pc->actually_transferred += bcount;
930 pc->current_position += bcount.all; 906 pc->current_position += bcount;
931 907
932 BUG_ON(HWGROUP(drive)->handler != NULL); 908 BUG_ON(HWGROUP(drive)->handler != NULL);
933 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */ 909 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
@@ -943,15 +919,15 @@ static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
943{ 919{
944 ide_startstop_t startstop; 920 ide_startstop_t startstop;
945 idefloppy_floppy_t *floppy = drive->driver_data; 921 idefloppy_floppy_t *floppy = drive->driver_data;
946 atapi_ireason_t ireason; 922 u8 ireason;
947 923
948 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) { 924 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
949 printk(KERN_ERR "ide-floppy: Strange, packet command " 925 printk(KERN_ERR "ide-floppy: Strange, packet command "
950 "initiated yet DRQ isn't asserted\n"); 926 "initiated yet DRQ isn't asserted\n");
951 return startstop; 927 return startstop;
952 } 928 }
953 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG); 929 ireason = drive->hwif->INB(IDE_IREASON_REG);
954 if (!ireason.b.cod || ireason.b.io) { 930 if ((ireason & CD) == 0 || (ireason & IO)) {
955 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while " 931 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
956 "issuing a packet command\n"); 932 "issuing a packet command\n");
957 return ide_do_reset(drive); 933 return ide_do_reset(drive);
@@ -991,15 +967,15 @@ static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
991{ 967{
992 idefloppy_floppy_t *floppy = drive->driver_data; 968 idefloppy_floppy_t *floppy = drive->driver_data;
993 ide_startstop_t startstop; 969 ide_startstop_t startstop;
994 atapi_ireason_t ireason; 970 u8 ireason;
995 971
996 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) { 972 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
997 printk(KERN_ERR "ide-floppy: Strange, packet command " 973 printk(KERN_ERR "ide-floppy: Strange, packet command "
998 "initiated yet DRQ isn't asserted\n"); 974 "initiated yet DRQ isn't asserted\n");
999 return startstop; 975 return startstop;
1000 } 976 }
1001 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG); 977 ireason = drive->hwif->INB(IDE_IREASON_REG);
1002 if (!ireason.b.cod || ireason.b.io) { 978 if ((ireason & CD) == 0 || (ireason & IO)) {
1003 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) " 979 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
1004 "while issuing a packet command\n"); 980 "while issuing a packet command\n");
1005 return ide_do_reset(drive); 981 return ide_do_reset(drive);
@@ -1041,21 +1017,9 @@ static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *p
1041{ 1017{
1042 idefloppy_floppy_t *floppy = drive->driver_data; 1018 idefloppy_floppy_t *floppy = drive->driver_data;
1043 ide_hwif_t *hwif = drive->hwif; 1019 ide_hwif_t *hwif = drive->hwif;
1044 atapi_feature_t feature;
1045 atapi_bcount_t bcount;
1046 ide_handler_t *pkt_xfer_routine; 1020 ide_handler_t *pkt_xfer_routine;
1047 1021 u16 bcount;
1048#if 0 /* Accessing floppy->pc is not valid here, the previous pc may be gone 1022 u8 dma;
1049 and have lived on another thread's stack; that stack may have become
1050 unmapped meanwhile (CONFIG_DEBUG_PAGEALLOC). */
1051#if IDEFLOPPY_DEBUG_BUGS
1052 if (floppy->pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD &&
1053 pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
1054 printk(KERN_ERR "ide-floppy: possible ide-floppy.c bug - "
1055 "Two request sense in serial were issued\n");
1056 }
1057#endif /* IDEFLOPPY_DEBUG_BUGS */
1058#endif
1059 1023
1060 if (floppy->failed_pc == NULL && 1024 if (floppy->failed_pc == NULL &&
1061 pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD) 1025 pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD)
@@ -1093,25 +1057,20 @@ static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *p
1093 /* We haven't transferred any data yet */ 1057 /* We haven't transferred any data yet */
1094 pc->actually_transferred = 0; 1058 pc->actually_transferred = 0;
1095 pc->current_position = pc->buffer; 1059 pc->current_position = pc->buffer;
1096 bcount.all = min(pc->request_transfer, 63 * 1024); 1060 bcount = min(pc->request_transfer, 63 * 1024);
1097 1061
1098 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) 1062 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
1099 ide_dma_off(drive); 1063 ide_dma_off(drive);
1100 1064
1101 feature.all = 0; 1065 dma = 0;
1102 1066
1103 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma) 1067 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1104 feature.b.dma = !hwif->dma_setup(drive); 1068 dma = !hwif->dma_setup(drive);
1105 1069
1106 if (IDE_CONTROL_REG) 1070 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1107 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG); 1071 IDE_TFLAG_OUT_DEVICE, bcount, dma);
1108 /* Use PIO/DMA */
1109 HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
1110 HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
1111 HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
1112 HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
1113 1072
1114 if (feature.b.dma) { /* Begin DMA, if necessary */ 1073 if (dma) { /* Begin DMA, if necessary */
1115 set_bit(PC_DMA_IN_PROGRESS, &pc->flags); 1074 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1116 hwif->dma_start(drive); 1075 hwif->dma_start(drive);
1117 } 1076 }
@@ -1665,14 +1624,14 @@ static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1665 /* Else assume format_unit has finished, and we're 1624 /* Else assume format_unit has finished, and we're
1666 ** at 0x10000 */ 1625 ** at 0x10000 */
1667 } else { 1626 } else {
1668 atapi_status_t status;
1669 unsigned long flags; 1627 unsigned long flags;
1628 u8 stat;
1670 1629
1671 local_irq_save(flags); 1630 local_irq_save(flags);
1672 status.all = HWIF(drive)->INB(IDE_STATUS_REG); 1631 stat = drive->hwif->INB(IDE_STATUS_REG);
1673 local_irq_restore(flags); 1632 local_irq_restore(flags);
1674 1633
1675 progress_indication = !status.b.dsc ? 0 : 0x10000; 1634 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1676 } 1635 }
1677 if (put_user(progress_indication, arg)) 1636 if (put_user(progress_indication, arg))
1678 return (-EFAULT); 1637 return (-EFAULT);
diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
index bef781fec500..2711b5a6962d 100644
--- a/drivers/ide/ide-io.c
+++ b/drivers/ide/ide-io.c
@@ -189,18 +189,14 @@ static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *
189 return ide_stopped; 189 return ide_stopped;
190 } 190 }
191 if (ide_id_has_flush_cache_ext(drive->id)) 191 if (ide_id_has_flush_cache_ext(drive->id))
192 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT; 192 args->tf.command = WIN_FLUSH_CACHE_EXT;
193 else 193 else
194 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE; 194 args->tf.command = WIN_FLUSH_CACHE;
195 args->command_type = IDE_DRIVE_TASK_NO_DATA; 195 goto out_do_tf;
196 args->handler = &task_no_data_intr;
197 return do_rw_taskfile(drive, args);
198 196
199 case idedisk_pm_standby: /* Suspend step 2 (standby) */ 197 case idedisk_pm_standby: /* Suspend step 2 (standby) */
200 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1; 198 args->tf.command = WIN_STANDBYNOW1;
201 args->command_type = IDE_DRIVE_TASK_NO_DATA; 199 goto out_do_tf;
202 args->handler = &task_no_data_intr;
203 return do_rw_taskfile(drive, args);
204 200
205 case idedisk_pm_restore_pio: /* Resume step 1 (restore PIO) */ 201 case idedisk_pm_restore_pio: /* Resume step 1 (restore PIO) */
206 ide_set_max_pio(drive); 202 ide_set_max_pio(drive);
@@ -214,10 +210,8 @@ static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *
214 return ide_stopped; 210 return ide_stopped;
215 211
216 case idedisk_pm_idle: /* Resume step 2 (idle) */ 212 case idedisk_pm_idle: /* Resume step 2 (idle) */
217 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE; 213 args->tf.command = WIN_IDLEIMMEDIATE;
218 args->command_type = IDE_DRIVE_TASK_NO_DATA; 214 goto out_do_tf;
219 args->handler = task_no_data_intr;
220 return do_rw_taskfile(drive, args);
221 215
222 case ide_pm_restore_dma: /* Resume step 3 (restore DMA) */ 216 case ide_pm_restore_dma: /* Resume step 3 (restore DMA) */
223 /* 217 /*
@@ -227,7 +221,6 @@ static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *
227 */ 221 */
228 if (drive->hwif->ide_dma_on == NULL) 222 if (drive->hwif->ide_dma_on == NULL)
229 break; 223 break;
230 drive->hwif->dma_off_quietly(drive);
231 /* 224 /*
232 * TODO: respect ->using_dma setting 225 * TODO: respect ->using_dma setting
233 */ 226 */
@@ -236,6 +229,11 @@ static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *
236 } 229 }
237 pm->pm_step = ide_pm_state_completed; 230 pm->pm_step = ide_pm_state_completed;
238 return ide_stopped; 231 return ide_stopped;
232
233out_do_tf:
234 args->tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
235 args->data_phase = TASKFILE_NO_DATA;
236 return do_rw_taskfile(drive, args);
239} 237}
240 238
241/** 239/**
@@ -298,6 +296,48 @@ static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
298 spin_unlock_irqrestore(&ide_lock, flags); 296 spin_unlock_irqrestore(&ide_lock, flags);
299} 297}
300 298
299void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
300{
301 ide_hwif_t *hwif = drive->hwif;
302 struct ide_taskfile *tf = &task->tf;
303
304 if (task->tf_flags & IDE_TFLAG_IN_DATA) {
305 u16 data = hwif->INW(IDE_DATA_REG);
306
307 tf->data = data & 0xff;
308 tf->hob_data = (data >> 8) & 0xff;
309 }
310
311 /* be sure we're looking at the low order bits */
312 hwif->OUTB(drive->ctl & ~0x80, IDE_CONTROL_REG);
313
314 if (task->tf_flags & IDE_TFLAG_IN_NSECT)
315 tf->nsect = hwif->INB(IDE_NSECTOR_REG);
316 if (task->tf_flags & IDE_TFLAG_IN_LBAL)
317 tf->lbal = hwif->INB(IDE_SECTOR_REG);
318 if (task->tf_flags & IDE_TFLAG_IN_LBAM)
319 tf->lbam = hwif->INB(IDE_LCYL_REG);
320 if (task->tf_flags & IDE_TFLAG_IN_LBAH)
321 tf->lbah = hwif->INB(IDE_HCYL_REG);
322 if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
323 tf->device = hwif->INB(IDE_SELECT_REG);
324
325 if (task->tf_flags & IDE_TFLAG_LBA48) {
326 hwif->OUTB(drive->ctl | 0x80, IDE_CONTROL_REG);
327
328 if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
329 tf->hob_feature = hwif->INB(IDE_FEATURE_REG);
330 if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
331 tf->hob_nsect = hwif->INB(IDE_NSECTOR_REG);
332 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
333 tf->hob_lbal = hwif->INB(IDE_SECTOR_REG);
334 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
335 tf->hob_lbam = hwif->INB(IDE_LCYL_REG);
336 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
337 tf->hob_lbah = hwif->INB(IDE_HCYL_REG);
338 }
339}
340
301/** 341/**
302 * ide_end_drive_cmd - end an explicit drive command 342 * ide_end_drive_cmd - end an explicit drive command
303 * @drive: command 343 * @drive: command
@@ -332,51 +372,22 @@ void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
332 args[1] = err; 372 args[1] = err;
333 args[2] = hwif->INB(IDE_NSECTOR_REG); 373 args[2] = hwif->INB(IDE_NSECTOR_REG);
334 } 374 }
335 } else if (rq->cmd_type == REQ_TYPE_ATA_TASK) {
336 u8 *args = (u8 *) rq->buffer;
337 if (rq->errors == 0)
338 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
339
340 if (args) {
341 args[0] = stat;
342 args[1] = err;
343 /* be sure we're looking at the low order bits */
344 hwif->OUTB(drive->ctl & ~0x80, IDE_CONTROL_REG);
345 args[2] = hwif->INB(IDE_NSECTOR_REG);
346 args[3] = hwif->INB(IDE_SECTOR_REG);
347 args[4] = hwif->INB(IDE_LCYL_REG);
348 args[5] = hwif->INB(IDE_HCYL_REG);
349 args[6] = hwif->INB(IDE_SELECT_REG);
350 }
351 } else if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) { 375 } else if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
352 ide_task_t *args = (ide_task_t *) rq->special; 376 ide_task_t *args = (ide_task_t *) rq->special;
353 if (rq->errors == 0) 377 if (rq->errors == 0)
354 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT); 378 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
355 379
356 if (args) { 380 if (args) {
357 if (args->tf_in_flags.b.data) { 381 struct ide_taskfile *tf = &args->tf;
358 u16 data = hwif->INW(IDE_DATA_REG); 382
359 args->tfRegister[IDE_DATA_OFFSET] = (data) & 0xFF; 383 tf->error = err;
360 args->hobRegister[IDE_DATA_OFFSET] = (data >> 8) & 0xFF; 384 tf->status = stat;
361 } 385
362 args->tfRegister[IDE_ERROR_OFFSET] = err; 386 args->tf_flags |= (IDE_TFLAG_IN_TF|IDE_TFLAG_IN_DEVICE);
363 /* be sure we're looking at the low order bits */ 387 if (args->tf_flags & IDE_TFLAG_LBA48)
364 hwif->OUTB(drive->ctl & ~0x80, IDE_CONTROL_REG); 388 args->tf_flags |= IDE_TFLAG_IN_HOB;
365 args->tfRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG); 389
366 args->tfRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG); 390 ide_tf_read(drive, args);
367 args->tfRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
368 args->tfRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
369 args->tfRegister[IDE_SELECT_OFFSET] = hwif->INB(IDE_SELECT_REG);
370 args->tfRegister[IDE_STATUS_OFFSET] = stat;
371
372 if (drive->addressing == 1) {
373 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
374 args->hobRegister[IDE_FEATURE_OFFSET] = hwif->INB(IDE_FEATURE_REG);
375 args->hobRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
376 args->hobRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
377 args->hobRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
378 args->hobRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
379 }
380 } 391 }
381 } else if (blk_pm_request(rq)) { 392 } else if (blk_pm_request(rq)) {
382 struct request_pm_state *pm = rq->data; 393 struct request_pm_state *pm = rq->data;
@@ -616,28 +627,6 @@ ide_startstop_t ide_abort(ide_drive_t *drive, const char *msg)
616} 627}
617 628
618/** 629/**
619 * ide_cmd - issue a simple drive command
620 * @drive: drive the command is for
621 * @cmd: command byte
622 * @nsect: sector byte
623 * @handler: handler for the command completion
624 *
625 * Issue a simple drive command with interrupts.
626 * The drive must be selected beforehand.
627 */
628
629static void ide_cmd (ide_drive_t *drive, u8 cmd, u8 nsect,
630 ide_handler_t *handler)
631{
632 ide_hwif_t *hwif = HWIF(drive);
633 if (IDE_CONTROL_REG)
634 hwif->OUTB(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
635 SELECT_MASK(drive,0);
636 hwif->OUTB(nsect,IDE_NSECTOR_REG);
637 ide_execute_command(drive, cmd, handler, WAIT_CMD, NULL);
638}
639
640/**
641 * drive_cmd_intr - drive command completion interrupt 630 * drive_cmd_intr - drive command completion interrupt
642 * @drive: drive the completion interrupt occurred on 631 * @drive: drive the completion interrupt occurred on
643 * 632 *
@@ -673,32 +662,26 @@ static ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
673 return ide_stopped; 662 return ide_stopped;
674} 663}
675 664
676static void ide_init_specify_cmd(ide_drive_t *drive, ide_task_t *task) 665static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
677{ 666{
678 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->sect; 667 tf->nsect = drive->sect;
679 task->tfRegister[IDE_SECTOR_OFFSET] = drive->sect; 668 tf->lbal = drive->sect;
680 task->tfRegister[IDE_LCYL_OFFSET] = drive->cyl; 669 tf->lbam = drive->cyl;
681 task->tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8; 670 tf->lbah = drive->cyl >> 8;
682 task->tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF; 671 tf->device = ((drive->head - 1) | drive->select.all) & ~ATA_LBA;
683 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY; 672 tf->command = WIN_SPECIFY;
684
685 task->handler = &set_geometry_intr;
686} 673}
687 674
688static void ide_init_restore_cmd(ide_drive_t *drive, ide_task_t *task) 675static void ide_tf_set_restore_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
689{ 676{
690 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->sect; 677 tf->nsect = drive->sect;
691 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE; 678 tf->command = WIN_RESTORE;
692
693 task->handler = &recal_intr;
694} 679}
695 680
696static void ide_init_setmult_cmd(ide_drive_t *drive, ide_task_t *task) 681static void ide_tf_set_setmult_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
697{ 682{
698 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req; 683 tf->nsect = drive->mult_req;
699 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT; 684 tf->command = WIN_SETMULT;
700
701 task->handler = &set_multmode_intr;
702} 685}
703 686
704static ide_startstop_t ide_disk_special(ide_drive_t *drive) 687static ide_startstop_t ide_disk_special(ide_drive_t *drive)
@@ -707,19 +690,19 @@ static ide_startstop_t ide_disk_special(ide_drive_t *drive)
707 ide_task_t args; 690 ide_task_t args;
708 691
709 memset(&args, 0, sizeof(ide_task_t)); 692 memset(&args, 0, sizeof(ide_task_t));
710 args.command_type = IDE_DRIVE_TASK_NO_DATA; 693 args.data_phase = TASKFILE_NO_DATA;
711 694
712 if (s->b.set_geometry) { 695 if (s->b.set_geometry) {
713 s->b.set_geometry = 0; 696 s->b.set_geometry = 0;
714 ide_init_specify_cmd(drive, &args); 697 ide_tf_set_specify_cmd(drive, &args.tf);
715 } else if (s->b.recalibrate) { 698 } else if (s->b.recalibrate) {
716 s->b.recalibrate = 0; 699 s->b.recalibrate = 0;
717 ide_init_restore_cmd(drive, &args); 700 ide_tf_set_restore_cmd(drive, &args.tf);
718 } else if (s->b.set_multmode) { 701 } else if (s->b.set_multmode) {
719 s->b.set_multmode = 0; 702 s->b.set_multmode = 0;
720 if (drive->mult_req > drive->id->max_multsect) 703 if (drive->mult_req > drive->id->max_multsect)
721 drive->mult_req = drive->id->max_multsect; 704 drive->mult_req = drive->id->max_multsect;
722 ide_init_setmult_cmd(drive, &args); 705 ide_tf_set_setmult_cmd(drive, &args.tf);
723 } else if (s->all) { 706 } else if (s->all) {
724 int special = s->all; 707 int special = s->all;
725 s->all = 0; 708 s->all = 0;
@@ -727,6 +710,9 @@ static ide_startstop_t ide_disk_special(ide_drive_t *drive)
727 return ide_stopped; 710 return ide_stopped;
728 } 711 }
729 712
713 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE |
714 IDE_TFLAG_CUSTOM_HANDLER;
715
730 do_rw_taskfile(drive, &args); 716 do_rw_taskfile(drive, &args);
731 717
732 return ide_started; 718 return ide_started;
@@ -861,13 +847,17 @@ static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
861 struct request *rq) 847 struct request *rq)
862{ 848{
863 ide_hwif_t *hwif = HWIF(drive); 849 ide_hwif_t *hwif = HWIF(drive);
850 u8 *args = rq->buffer;
851 ide_task_t ltask;
852 struct ide_taskfile *tf = &ltask.tf;
853
864 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) { 854 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
865 ide_task_t *args = rq->special; 855 ide_task_t *task = rq->special;
866 856
867 if (!args) 857 if (task == NULL)
868 goto done; 858 goto done;
869 859
870 hwif->data_phase = args->data_phase; 860 hwif->data_phase = task->data_phase;
871 861
872 switch (hwif->data_phase) { 862 switch (hwif->data_phase) {
873 case TASKFILE_MULTI_OUT: 863 case TASKFILE_MULTI_OUT:
@@ -880,55 +870,34 @@ static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
880 break; 870 break;
881 } 871 }
882 872
883 if (args->tf_out_flags.all != 0) 873 return do_rw_taskfile(drive, task);
884 return flagged_taskfile(drive, args); 874 }
885 return do_rw_taskfile(drive, args); 875
886 } else if (rq->cmd_type == REQ_TYPE_ATA_TASK) { 876 if (args == NULL)
887 u8 *args = rq->buffer; 877 goto done;
888 878
889 if (!args) 879 memset(&ltask, 0, sizeof(ltask));
890 goto done; 880 if (rq->cmd_type == REQ_TYPE_ATA_CMD) {
891#ifdef DEBUG
892 printk("%s: DRIVE_TASK_CMD ", drive->name);
893 printk("cmd=0x%02x ", args[0]);
894 printk("fr=0x%02x ", args[1]);
895 printk("ns=0x%02x ", args[2]);
896 printk("sc=0x%02x ", args[3]);
897 printk("lcyl=0x%02x ", args[4]);
898 printk("hcyl=0x%02x ", args[5]);
899 printk("sel=0x%02x\n", args[6]);
900#endif
901 hwif->OUTB(args[1], IDE_FEATURE_REG);
902 hwif->OUTB(args[3], IDE_SECTOR_REG);
903 hwif->OUTB(args[4], IDE_LCYL_REG);
904 hwif->OUTB(args[5], IDE_HCYL_REG);
905 hwif->OUTB((args[6] & 0xEF)|drive->select.all, IDE_SELECT_REG);
906 ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
907 return ide_started;
908 } else if (rq->cmd_type == REQ_TYPE_ATA_CMD) {
909 u8 *args = rq->buffer;
910
911 if (!args)
912 goto done;
913#ifdef DEBUG 881#ifdef DEBUG
914 printk("%s: DRIVE_CMD ", drive->name); 882 printk("%s: DRIVE_CMD\n", drive->name);
915 printk("cmd=0x%02x ", args[0]);
916 printk("sc=0x%02x ", args[1]);
917 printk("fr=0x%02x ", args[2]);
918 printk("xx=0x%02x\n", args[3]);
919#endif 883#endif
920 if (args[0] == WIN_SMART) { 884 tf->feature = args[2];
921 hwif->OUTB(0x4f, IDE_LCYL_REG); 885 if (args[0] == WIN_SMART) {
922 hwif->OUTB(0xc2, IDE_HCYL_REG); 886 tf->nsect = args[3];
923 hwif->OUTB(args[2],IDE_FEATURE_REG); 887 tf->lbal = args[1];
924 hwif->OUTB(args[1],IDE_SECTOR_REG); 888 tf->lbam = 0x4f;
925 ide_cmd(drive, args[0], args[3], &drive_cmd_intr); 889 tf->lbah = 0xc2;
926 return ide_started; 890 ltask.tf_flags = IDE_TFLAG_OUT_TF;
927 } 891 } else {
928 hwif->OUTB(args[2],IDE_FEATURE_REG); 892 tf->nsect = args[1];
929 ide_cmd(drive, args[0], args[1], &drive_cmd_intr); 893 ltask.tf_flags = IDE_TFLAG_OUT_FEATURE |
930 return ide_started; 894 IDE_TFLAG_OUT_NSECT;
895 }
931 } 896 }
897 tf->command = args[0];
898 ide_tf_load(drive, &ltask);
899 ide_execute_command(drive, args[0], &drive_cmd_intr, WAIT_WORSTCASE, NULL);
900 return ide_started;
932 901
933done: 902done:
934 /* 903 /*
@@ -1003,6 +972,7 @@ static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
1003 972
1004 /* bail early if we've exceeded max_failures */ 973 /* bail early if we've exceeded max_failures */
1005 if (drive->max_failures && (drive->failures > drive->max_failures)) { 974 if (drive->max_failures && (drive->failures > drive->max_failures)) {
975 rq->cmd_flags |= REQ_FAILED;
1006 goto kill_rq; 976 goto kill_rq;
1007 } 977 }
1008 978
@@ -1035,7 +1005,6 @@ static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
1035 ide_config_drive_speed(drive, drive->desired_speed); 1005 ide_config_drive_speed(drive, drive->desired_speed);
1036 1006
1037 if (rq->cmd_type == REQ_TYPE_ATA_CMD || 1007 if (rq->cmd_type == REQ_TYPE_ATA_CMD ||
1038 rq->cmd_type == REQ_TYPE_ATA_TASK ||
1039 rq->cmd_type == REQ_TYPE_ATA_TASKFILE) 1008 rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
1040 return execute_drive_cmd(drive, rq); 1009 return execute_drive_cmd(drive, rq);
1041 else if (blk_pm_request(rq)) { 1010 else if (blk_pm_request(rq)) {
@@ -1247,8 +1216,12 @@ static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
1247 if (hwgroup->hwif->sharing_irq && 1216 if (hwgroup->hwif->sharing_irq &&
1248 hwif != hwgroup->hwif && 1217 hwif != hwgroup->hwif &&
1249 hwif->io_ports[IDE_CONTROL_OFFSET]) { 1218 hwif->io_ports[IDE_CONTROL_OFFSET]) {
1250 /* set nIEN for previous hwif */ 1219 /*
1251 SELECT_INTERRUPT(drive); 1220 * set nIEN for previous hwif, drives in the
1221 * quirk_list may not like intr setups/cleanups
1222 */
1223 if (drive->quirk_list != 1)
1224 hwif->OUTB(drive->ctl | 2, IDE_CONTROL_REG);
1252 } 1225 }
1253 hwgroup->hwif = hwif; 1226 hwgroup->hwif = hwif;
1254 hwgroup->drive = drive; 1227 hwgroup->drive = drive;
@@ -1454,12 +1427,8 @@ void ide_timer_expiry (unsigned long data)
1454 */ 1427 */
1455 spin_unlock(&ide_lock); 1428 spin_unlock(&ide_lock);
1456 hwif = HWIF(drive); 1429 hwif = HWIF(drive);
1457#if DISABLE_IRQ_NOSYNC
1458 disable_irq_nosync(hwif->irq);
1459#else
1460 /* disable_irq_nosync ?? */ 1430 /* disable_irq_nosync ?? */
1461 disable_irq(hwif->irq); 1431 disable_irq(hwif->irq);
1462#endif /* DISABLE_IRQ_NOSYNC */
1463 /* local CPU only, 1432 /* local CPU only,
1464 * as if we were handling an interrupt */ 1433 * as if we were handling an interrupt */
1465 local_irq_disable(); 1434 local_irq_disable();
@@ -1785,3 +1754,19 @@ int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t actio
1785} 1754}
1786 1755
1787EXPORT_SYMBOL(ide_do_drive_cmd); 1756EXPORT_SYMBOL(ide_do_drive_cmd);
1757
1758void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma)
1759{
1760 ide_task_t task;
1761
1762 memset(&task, 0, sizeof(task));
1763 task.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
1764 IDE_TFLAG_OUT_FEATURE | tf_flags;
1765 task.tf.feature = dma; /* Use PIO/DMA */
1766 task.tf.lbam = bcount & 0xff;
1767 task.tf.lbah = (bcount >> 8) & 0xff;
1768
1769 ide_tf_load(drive, &task);
1770}
1771
1772EXPORT_SYMBOL_GPL(ide_pktcmd_tf_load);
diff --git a/drivers/ide/ide-iops.c b/drivers/ide/ide-iops.c
index bb9693dabe41..c97c0719ddf1 100644
--- a/drivers/ide/ide-iops.c
+++ b/drivers/ide/ide-iops.c
@@ -158,14 +158,6 @@ void default_hwif_mmiops (ide_hwif_t *hwif)
158 158
159EXPORT_SYMBOL(default_hwif_mmiops); 159EXPORT_SYMBOL(default_hwif_mmiops);
160 160
161u32 ide_read_24 (ide_drive_t *drive)
162{
163 u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
164 u8 lcyl = HWIF(drive)->INB(IDE_LCYL_REG);
165 u8 sect = HWIF(drive)->INB(IDE_SECTOR_REG);
166 return (hcyl<<16)|(lcyl<<8)|sect;
167}
168
169void SELECT_DRIVE (ide_drive_t *drive) 161void SELECT_DRIVE (ide_drive_t *drive)
170{ 162{
171 if (HWIF(drive)->selectproc) 163 if (HWIF(drive)->selectproc)
@@ -175,26 +167,12 @@ void SELECT_DRIVE (ide_drive_t *drive)
175 167
176EXPORT_SYMBOL(SELECT_DRIVE); 168EXPORT_SYMBOL(SELECT_DRIVE);
177 169
178void SELECT_INTERRUPT (ide_drive_t *drive)
179{
180 if (HWIF(drive)->intrproc)
181 HWIF(drive)->intrproc(drive);
182 else
183 HWIF(drive)->OUTB(drive->ctl|2, IDE_CONTROL_REG);
184}
185
186void SELECT_MASK (ide_drive_t *drive, int mask) 170void SELECT_MASK (ide_drive_t *drive, int mask)
187{ 171{
188 if (HWIF(drive)->maskproc) 172 if (HWIF(drive)->maskproc)
189 HWIF(drive)->maskproc(drive, mask); 173 HWIF(drive)->maskproc(drive, mask);
190} 174}
191 175
192void QUIRK_LIST (ide_drive_t *drive)
193{
194 if (HWIF(drive)->quirkproc)
195 drive->quirk_list = HWIF(drive)->quirkproc(drive);
196}
197
198/* 176/*
199 * Some localbus EIDE interfaces require a special access sequence 177 * Some localbus EIDE interfaces require a special access sequence
200 * when using 32-bit I/O instructions to transfer data. We call this 178 * when using 32-bit I/O instructions to transfer data. We call this
@@ -449,7 +427,6 @@ int drive_is_ready (ide_drive_t *drive)
449 udelay(1); 427 udelay(1);
450#endif 428#endif
451 429
452#ifdef CONFIG_IDEPCI_SHARE_IRQ
453 /* 430 /*
454 * We do a passive status test under shared PCI interrupts on 431 * We do a passive status test under shared PCI interrupts on
455 * cards that truly share the ATA side interrupt, but may also share 432 * cards that truly share the ATA side interrupt, but may also share
@@ -459,7 +436,6 @@ int drive_is_ready (ide_drive_t *drive)
459 if (IDE_CONTROL_REG) 436 if (IDE_CONTROL_REG)
460 stat = hwif->INB(IDE_ALTSTATUS_REG); 437 stat = hwif->INB(IDE_ALTSTATUS_REG);
461 else 438 else
462#endif /* CONFIG_IDEPCI_SHARE_IRQ */
463 /* Note: this may clear a pending IRQ!! */ 439 /* Note: this may clear a pending IRQ!! */
464 stat = hwif->INB(IDE_STATUS_REG); 440 stat = hwif->INB(IDE_STATUS_REG);
465 441
@@ -642,9 +618,9 @@ no_80w:
642 618
643int ide_ata66_check (ide_drive_t *drive, ide_task_t *args) 619int ide_ata66_check (ide_drive_t *drive, ide_task_t *args)
644{ 620{
645 if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) && 621 if (args->tf.command == WIN_SETFEATURES &&
646 (args->tfRegister[IDE_SECTOR_OFFSET] > XFER_UDMA_2) && 622 args->tf.lbal > XFER_UDMA_2 &&
647 (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER)) { 623 args->tf.feature == SETFEATURES_XFER) {
648 if (eighty_ninty_three(drive) == 0) { 624 if (eighty_ninty_three(drive) == 0) {
649 printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot " 625 printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
650 "be set\n", drive->name); 626 "be set\n", drive->name);
@@ -662,9 +638,9 @@ int ide_ata66_check (ide_drive_t *drive, ide_task_t *args)
662 */ 638 */
663int set_transfer (ide_drive_t *drive, ide_task_t *args) 639int set_transfer (ide_drive_t *drive, ide_task_t *args)
664{ 640{
665 if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) && 641 if (args->tf.command == WIN_SETFEATURES &&
666 (args->tfRegister[IDE_SECTOR_OFFSET] >= XFER_SW_DMA_0) && 642 args->tf.lbal >= XFER_SW_DMA_0 &&
667 (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER) && 643 args->tf.feature == SETFEATURES_XFER &&
668 (drive->id->dma_ultra || 644 (drive->id->dma_ultra ||
669 drive->id->dma_mword || 645 drive->id->dma_mword ||
670 drive->id->dma_1word)) 646 drive->id->dma_1word))
@@ -902,8 +878,9 @@ EXPORT_SYMBOL(ide_set_handler);
902 * handler and IRQ setup do not race. All IDE command kick off 878 * handler and IRQ setup do not race. All IDE command kick off
903 * should go via this function or do equivalent locking. 879 * should go via this function or do equivalent locking.
904 */ 880 */
905 881
906void ide_execute_command(ide_drive_t *drive, task_ioreg_t cmd, ide_handler_t *handler, unsigned timeout, ide_expiry_t *expiry) 882void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
883 unsigned timeout, ide_expiry_t *expiry)
907{ 884{
908 unsigned long flags; 885 unsigned long flags;
909 ide_hwgroup_t *hwgroup = HWGROUP(drive); 886 ide_hwgroup_t *hwgroup = HWGROUP(drive);
@@ -1051,8 +1028,7 @@ static void ide_disk_pre_reset(ide_drive_t *drive)
1051 drive->special.all = 0; 1028 drive->special.all = 0;
1052 drive->special.b.set_geometry = legacy; 1029 drive->special.b.set_geometry = legacy;
1053 drive->special.b.recalibrate = legacy; 1030 drive->special.b.recalibrate = legacy;
1054 if (OK_TO_RESET_CONTROLLER) 1031 drive->mult_count = 0;
1055 drive->mult_count = 0;
1056 if (!drive->keep_settings && !drive->using_dma) 1032 if (!drive->keep_settings && !drive->using_dma)
1057 drive->mult_req = 0; 1033 drive->mult_req = 0;
1058 if (drive->mult_req != drive->mult_count) 1034 if (drive->mult_req != drive->mult_count)
@@ -1137,7 +1113,6 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1137 for (unit = 0; unit < MAX_DRIVES; ++unit) 1113 for (unit = 0; unit < MAX_DRIVES; ++unit)
1138 pre_reset(&hwif->drives[unit]); 1114 pre_reset(&hwif->drives[unit]);
1139 1115
1140#if OK_TO_RESET_CONTROLLER
1141 if (!IDE_CONTROL_REG) { 1116 if (!IDE_CONTROL_REG) {
1142 spin_unlock_irqrestore(&ide_lock, flags); 1117 spin_unlock_irqrestore(&ide_lock, flags);
1143 return ide_stopped; 1118 return ide_stopped;
@@ -1174,11 +1149,8 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1174 * state when the disks are reset this way. At least, the Winbond 1149 * state when the disks are reset this way. At least, the Winbond
1175 * 553 documentation says that 1150 * 553 documentation says that
1176 */ 1151 */
1177 if (hwif->resetproc != NULL) { 1152 if (hwif->resetproc)
1178 hwif->resetproc(drive); 1153 hwif->resetproc(drive);
1179 }
1180
1181#endif /* OK_TO_RESET_CONTROLLER */
1182 1154
1183 spin_unlock_irqrestore(&ide_lock, flags); 1155 spin_unlock_irqrestore(&ide_lock, flags);
1184 return ide_started; 1156 return ide_started;
diff --git a/drivers/ide/ide-lib.c b/drivers/ide/ide-lib.c
index 062d3bcb2471..a3bd8e8ed6b0 100644
--- a/drivers/ide/ide-lib.c
+++ b/drivers/ide/ide-lib.c
@@ -441,6 +441,12 @@ int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
441 * case could happen iff the transfer mode has already been set on 441 * case could happen iff the transfer mode has already been set on
442 * the device by ide-proc.c::set_xfer_rate()). 442 * the device by ide-proc.c::set_xfer_rate()).
443 */ 443 */
444 if (rate < XFER_PIO_0) {
445 if (hwif->host_flags & IDE_HFLAG_ABUSE_SET_DMA_MODE)
446 return ide_set_dma_mode(drive, rate);
447 else
448 return ide_config_drive_speed(drive, rate);
449 }
444 450
445 return ide_set_dma_mode(drive, rate); 451 return ide_set_dma_mode(drive, rate);
446} 452}
@@ -458,8 +464,7 @@ static void ide_dump_opcode(ide_drive_t *drive)
458 spin_unlock(&ide_lock); 464 spin_unlock(&ide_lock);
459 if (!rq) 465 if (!rq)
460 return; 466 return;
461 if (rq->cmd_type == REQ_TYPE_ATA_CMD || 467 if (rq->cmd_type == REQ_TYPE_ATA_CMD) {
462 rq->cmd_type == REQ_TYPE_ATA_TASK) {
463 char *args = rq->buffer; 468 char *args = rq->buffer;
464 if (args) { 469 if (args) {
465 opcode = args[0]; 470 opcode = args[0];
@@ -468,8 +473,7 @@ static void ide_dump_opcode(ide_drive_t *drive)
468 } else if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) { 473 } else if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
469 ide_task_t *args = rq->special; 474 ide_task_t *args = rq->special;
470 if (args) { 475 if (args) {
471 task_struct_t *tf = (task_struct_t *) args->tfRegister; 476 opcode = args->tf.command;
472 opcode = tf->command;
473 found = 1; 477 found = 1;
474 } 478 }
475 } 479 }
@@ -481,141 +485,118 @@ static void ide_dump_opcode(ide_drive_t *drive)
481 printk("0x%02x\n", opcode); 485 printk("0x%02x\n", opcode);
482} 486}
483 487
484static u8 ide_dump_ata_status(ide_drive_t *drive, const char *msg, u8 stat) 488u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
485{ 489{
486 ide_hwif_t *hwif = HWIF(drive); 490 u32 high, low;
487 unsigned long flags;
488 u8 err = 0;
489 491
490 local_irq_save(flags); 492 if (lba48)
491 printk("%s: %s: status=0x%02x { ", drive->name, msg, stat); 493 high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
492 if (stat & BUSY_STAT) 494 tf->hob_lbal;
493 printk("Busy "); 495 else
494 else { 496 high = tf->device & 0xf;
495 if (stat & READY_STAT) printk("DriveReady "); 497 low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
496 if (stat & WRERR_STAT) printk("DeviceFault "); 498
497 if (stat & SEEK_STAT) printk("SeekComplete "); 499 return ((u64)high << 24) | low;
498 if (stat & DRQ_STAT) printk("DataRequest "); 500}
499 if (stat & ECC_STAT) printk("CorrectedError "); 501EXPORT_SYMBOL_GPL(ide_get_lba_addr);
500 if (stat & INDEX_STAT) printk("Index "); 502
501 if (stat & ERR_STAT) printk("Error "); 503static void ide_dump_sector(ide_drive_t *drive)
504{
505 ide_task_t task;
506 struct ide_taskfile *tf = &task.tf;
507 int lba48 = (drive->addressing == 1) ? 1 : 0;
508
509 memset(&task, 0, sizeof(task));
510 if (lba48)
511 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
512 IDE_TFLAG_LBA48;
513 else
514 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
515
516 ide_tf_read(drive, &task);
517
518 if (lba48 || (tf->device & ATA_LBA))
519 printk(", LBAsect=%llu",
520 (unsigned long long)ide_get_lba_addr(tf, lba48));
521 else
522 printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
523 tf->device & 0xf, tf->lbal);
524}
525
526static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
527{
528 printk("{ ");
529 if (err & ABRT_ERR) printk("DriveStatusError ");
530 if (err & ICRC_ERR)
531 printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
532 if (err & ECC_ERR) printk("UncorrectableError ");
533 if (err & ID_ERR) printk("SectorIdNotFound ");
534 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
535 if (err & MARK_ERR) printk("AddrMarkNotFound ");
536 printk("}");
537 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
538 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
539 ide_dump_sector(drive);
540 if (HWGROUP(drive) && HWGROUP(drive)->rq)
541 printk(", sector=%llu",
542 (unsigned long long)HWGROUP(drive)->rq->sector);
502 } 543 }
544 printk("\n");
545}
546
547static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
548{
549 printk("{ ");
550 if (err & ILI_ERR) printk("IllegalLengthIndication ");
551 if (err & EOM_ERR) printk("EndOfMedia ");
552 if (err & ABRT_ERR) printk("AbortedCommand ");
553 if (err & MCR_ERR) printk("MediaChangeRequested ");
554 if (err & LFS_ERR) printk("LastFailedSense=0x%02x ",
555 (err & LFS_ERR) >> 4);
503 printk("}\n"); 556 printk("}\n");
504 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
505 err = hwif->INB(IDE_ERROR_REG);
506 printk("%s: %s: error=0x%02x { ", drive->name, msg, err);
507 if (err & ABRT_ERR) printk("DriveStatusError ");
508 if (err & ICRC_ERR)
509 printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
510 if (err & ECC_ERR) printk("UncorrectableError ");
511 if (err & ID_ERR) printk("SectorIdNotFound ");
512 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
513 if (err & MARK_ERR) printk("AddrMarkNotFound ");
514 printk("}");
515 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
516 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
517 if (drive->addressing == 1) {
518 __u64 sectors = 0;
519 u32 low = 0, high = 0;
520 hwif->OUTB(drive->ctl&~0x80, IDE_CONTROL_REG);
521 low = ide_read_24(drive);
522 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
523 high = ide_read_24(drive);
524 sectors = ((__u64)high << 24) | low;
525 printk(", LBAsect=%llu, high=%d, low=%d",
526 (unsigned long long) sectors,
527 high, low);
528 } else {
529 u8 cur = hwif->INB(IDE_SELECT_REG);
530 if (cur & 0x40) { /* using LBA? */
531 printk(", LBAsect=%ld", (unsigned long)
532 ((cur&0xf)<<24)
533 |(hwif->INB(IDE_HCYL_REG)<<16)
534 |(hwif->INB(IDE_LCYL_REG)<<8)
535 | hwif->INB(IDE_SECTOR_REG));
536 } else {
537 printk(", CHS=%d/%d/%d",
538 (hwif->INB(IDE_HCYL_REG)<<8) +
539 hwif->INB(IDE_LCYL_REG),
540 cur & 0xf,
541 hwif->INB(IDE_SECTOR_REG));
542 }
543 }
544 if (HWGROUP(drive) && HWGROUP(drive)->rq)
545 printk(", sector=%llu",
546 (unsigned long long)HWGROUP(drive)->rq->sector);
547 }
548 printk("\n");
549 }
550 ide_dump_opcode(drive);
551 local_irq_restore(flags);
552 return err;
553} 557}
554 558
555/** 559/**
556 * ide_dump_atapi_status - print human readable atapi status 560 * ide_dump_status - translate ATA/ATAPI error
557 * @drive: drive that status applies to 561 * @drive: drive that status applies to
558 * @msg: text message to print 562 * @msg: text message to print
559 * @stat: status byte to decode 563 * @stat: status byte to decode
560 * 564 *
561 * Error reporting, in human readable form (luxurious, but a memory hog). 565 * Error reporting, in human readable form (luxurious, but a memory hog).
566 * Combines the drive name, message and status byte to provide a
567 * user understandable explanation of the device error.
562 */ 568 */
563 569
564static u8 ide_dump_atapi_status(ide_drive_t *drive, const char *msg, u8 stat) 570u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
565{ 571{
566 unsigned long flags; 572 unsigned long flags;
573 u8 err = 0;
567 574
568 atapi_status_t status;
569 atapi_error_t error;
570
571 status.all = stat;
572 error.all = 0;
573 local_irq_save(flags); 575 local_irq_save(flags);
574 printk("%s: %s: status=0x%02x { ", drive->name, msg, stat); 576 printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
575 if (status.b.bsy) 577 if (stat & BUSY_STAT)
576 printk("Busy "); 578 printk("Busy ");
577 else { 579 else {
578 if (status.b.drdy) printk("DriveReady "); 580 if (stat & READY_STAT) printk("DriveReady ");
579 if (status.b.df) printk("DeviceFault "); 581 if (stat & WRERR_STAT) printk("DeviceFault ");
580 if (status.b.dsc) printk("SeekComplete "); 582 if (stat & SEEK_STAT) printk("SeekComplete ");
581 if (status.b.drq) printk("DataRequest "); 583 if (stat & DRQ_STAT) printk("DataRequest ");
582 if (status.b.corr) printk("CorrectedError "); 584 if (stat & ECC_STAT) printk("CorrectedError ");
583 if (status.b.idx) printk("Index "); 585 if (stat & INDEX_STAT) printk("Index ");
584 if (status.b.check) printk("Error "); 586 if (stat & ERR_STAT) printk("Error ");
585 } 587 }
586 printk("}\n"); 588 printk("}\n");
587 if (status.b.check && !status.b.bsy) { 589 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
588 error.all = HWIF(drive)->INB(IDE_ERROR_REG); 590 err = drive->hwif->INB(IDE_ERROR_REG);
589 printk("%s: %s: error=0x%02x { ", drive->name, msg, error.all); 591 printk("%s: %s: error=0x%02x ", drive->name, msg, err);
590 if (error.b.ili) printk("IllegalLengthIndication "); 592 if (drive->media == ide_disk)
591 if (error.b.eom) printk("EndOfMedia "); 593 ide_dump_ata_error(drive, err);
592 if (error.b.abrt) printk("AbortedCommand "); 594 else
593 if (error.b.mcr) printk("MediaChangeRequested "); 595 ide_dump_atapi_error(drive, err);
594 if (error.b.sense_key) printk("LastFailedSense=0x%02x ",
595 error.b.sense_key);
596 printk("}\n");
597 } 596 }
598 ide_dump_opcode(drive); 597 ide_dump_opcode(drive);
599 local_irq_restore(flags); 598 local_irq_restore(flags);
600 return error.all; 599 return err;
601}
602
603/**
604 * ide_dump_status - translate ATA/ATAPI error
605 * @drive: drive the error occured on
606 * @msg: information string
607 * @stat: status byte
608 *
609 * Error reporting, in human readable form (luxurious, but a memory hog).
610 * Combines the drive name, message and status byte to provide a
611 * user understandable explanation of the device error.
612 */
613
614u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
615{
616 if (drive->media == ide_disk)
617 return ide_dump_ata_status(drive, msg, stat);
618 return ide_dump_atapi_status(drive, msg, stat);
619} 600}
620 601
621EXPORT_SYMBOL(ide_dump_status); 602EXPORT_SYMBOL(ide_dump_status);
diff --git a/drivers/ide/ide-probe.c b/drivers/ide/ide-probe.c
index 0cb3d2bb3ab9..0379d1f697cf 100644
--- a/drivers/ide/ide-probe.c
+++ b/drivers/ide/ide-probe.c
@@ -95,10 +95,10 @@ static void ide_disk_init_mult_count(ide_drive_t *drive)
95#ifdef CONFIG_IDEDISK_MULTI_MODE 95#ifdef CONFIG_IDEDISK_MULTI_MODE
96 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0; 96 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
97 id->multsect_valid = id->multsect ? 1 : 0; 97 id->multsect_valid = id->multsect ? 1 : 0;
98 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT; 98 drive->mult_req = id->multsect_valid ? id->max_multsect : 0;
99 drive->special.b.set_multmode = drive->mult_req ? 1 : 0; 99 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
100#else /* original, pre IDE-NFG, per request of AC */ 100#else /* original, pre IDE-NFG, per request of AC */
101 drive->mult_req = INITIAL_MULT_COUNT; 101 drive->mult_req = 0;
102 if (drive->mult_req > id->max_multsect) 102 if (drive->mult_req > id->max_multsect)
103 drive->mult_req = id->max_multsect; 103 drive->mult_req = id->max_multsect;
104 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect)) 104 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
@@ -234,7 +234,10 @@ static inline void do_identify (ide_drive_t *drive, u8 cmd)
234 234
235 drive->media = ide_disk; 235 drive->media = ide_disk;
236 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" ); 236 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
237 QUIRK_LIST(drive); 237
238 if (hwif->quirkproc)
239 drive->quirk_list = hwif->quirkproc(drive);
240
238 return; 241 return;
239 242
240err_misc: 243err_misc:
@@ -830,16 +833,8 @@ static void probe_hwif(ide_hwif_t *hwif)
830 833
831 drive->nice1 = 1; 834 drive->nice1 = 1;
832 835
833 if (hwif->ide_dma_on) { 836 if (hwif->ide_dma_on)
834 /*
835 * Force DMAing for the beginning of the check.
836 * Some chipsets appear to do interesting
837 * things, if not checked and cleared.
838 * PARANOIA!!!
839 */
840 hwif->dma_off_quietly(drive);
841 ide_set_dma(drive); 837 ide_set_dma(drive);
842 }
843 } 838 }
844 } 839 }
845 840
@@ -968,11 +963,6 @@ static int ide_init_queue(ide_drive_t *drive)
968 * Much of the code is for correctly detecting/handling irq sharing 963 * Much of the code is for correctly detecting/handling irq sharing
969 * and irq serialization situations. This is somewhat complex because 964 * and irq serialization situations. This is somewhat complex because
970 * it handles static as well as dynamic (PCMCIA) IDE interfaces. 965 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
971 *
972 * The IRQF_DISABLED in sa_flags means ide_intr() is always entered with
973 * interrupts completely disabled. This can be bad for interrupt latency,
974 * but anything else has led to problems on some machines. We re-enable
975 * interrupts as much as we can safely do in most places.
976 */ 966 */
977static int init_irq (ide_hwif_t *hwif) 967static int init_irq (ide_hwif_t *hwif)
978{ 968{
@@ -1055,17 +1045,13 @@ static int init_irq (ide_hwif_t *hwif)
1055 * Allocate the irq, if not already obtained for another hwif 1045 * Allocate the irq, if not already obtained for another hwif
1056 */ 1046 */
1057 if (!match || match->irq != hwif->irq) { 1047 if (!match || match->irq != hwif->irq) {
1058 int sa = IRQF_DISABLED; 1048 int sa = 0;
1059#if defined(__mc68000__) || defined(CONFIG_APUS) 1049#if defined(__mc68000__) || defined(CONFIG_APUS)
1060 sa = IRQF_SHARED; 1050 sa = IRQF_SHARED;
1061#endif /* __mc68000__ || CONFIG_APUS */ 1051#endif /* __mc68000__ || CONFIG_APUS */
1062 1052
1063 if (IDE_CHIPSET_IS_PCI(hwif->chipset)) { 1053 if (IDE_CHIPSET_IS_PCI(hwif->chipset))
1064 sa = IRQF_SHARED; 1054 sa = IRQF_SHARED;
1065#ifndef CONFIG_IDEPCI_SHARE_IRQ
1066 sa |= IRQF_DISABLED;
1067#endif /* CONFIG_IDEPCI_SHARE_IRQ */
1068 }
1069 1055
1070 if (hwif->io_ports[IDE_CONTROL_OFFSET]) 1056 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1071 /* clear nIEN */ 1057 /* clear nIEN */
diff --git a/drivers/ide/ide-tape.c b/drivers/ide/ide-tape.c
index 1495792d7917..3cbca3f4628a 100644
--- a/drivers/ide/ide-tape.c
+++ b/drivers/ide/ide-tape.c
@@ -615,16 +615,6 @@ typedef struct os_dat_s {
615/*************************** End of tunable parameters ***********************/ 615/*************************** End of tunable parameters ***********************/
616 616
617/* 617/*
618 * Debugging/Performance analysis
619 *
620 * I/O trace support
621 */
622#define USE_IOTRACE 0
623#if USE_IOTRACE
624#define IO_IDETAPE_FIFO 500
625#endif
626
627/*
628 * Read/Write error simulation 618 * Read/Write error simulation
629 */ 619 */
630#define SIMULATE_ERRORS 0 620#define SIMULATE_ERRORS 0
@@ -1818,9 +1808,8 @@ static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1818 idetape_tape_t *tape = drive->driver_data; 1808 idetape_tape_t *tape = drive->driver_data;
1819 idetape_pc_t *pc; 1809 idetape_pc_t *pc;
1820 struct request *rq; 1810 struct request *rq;
1821 atapi_error_t error;
1822 1811
1823 error.all = HWIF(drive)->INB(IDE_ERROR_REG); 1812 (void)drive->hwif->INB(IDE_ERROR_REG);
1824 pc = idetape_next_pc_storage(drive); 1813 pc = idetape_next_pc_storage(drive);
1825 rq = idetape_next_rq_storage(drive); 1814 rq = idetape_next_rq_storage(drive);
1826 idetape_create_request_sense_cmd(pc); 1815 idetape_create_request_sense_cmd(pc);
@@ -1858,15 +1847,13 @@ static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1858{ 1847{
1859 ide_hwif_t *hwif = drive->hwif; 1848 ide_hwif_t *hwif = drive->hwif;
1860 idetape_tape_t *tape = drive->driver_data; 1849 idetape_tape_t *tape = drive->driver_data;
1861 atapi_status_t status;
1862 atapi_bcount_t bcount;
1863 atapi_ireason_t ireason;
1864 idetape_pc_t *pc = tape->pc; 1850 idetape_pc_t *pc = tape->pc;
1865
1866 unsigned int temp; 1851 unsigned int temp;
1867#if SIMULATE_ERRORS 1852#if SIMULATE_ERRORS
1868 static int error_sim_count = 0; 1853 static int error_sim_count = 0;
1869#endif 1854#endif
1855 u16 bcount;
1856 u8 stat, ireason;
1870 1857
1871#if IDETAPE_DEBUG_LOG 1858#if IDETAPE_DEBUG_LOG
1872 if (tape->debug_level >= 4) 1859 if (tape->debug_level >= 4)
@@ -1875,10 +1862,10 @@ static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1875#endif /* IDETAPE_DEBUG_LOG */ 1862#endif /* IDETAPE_DEBUG_LOG */
1876 1863
1877 /* Clear the interrupt */ 1864 /* Clear the interrupt */
1878 status.all = HWIF(drive)->INB(IDE_STATUS_REG); 1865 stat = hwif->INB(IDE_STATUS_REG);
1879 1866
1880 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) { 1867 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1881 if (HWIF(drive)->ide_dma_end(drive) || status.b.check) { 1868 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
1882 /* 1869 /*
1883 * A DMA error is sometimes expected. For example, 1870 * A DMA error is sometimes expected. For example,
1884 * if the tape is crossing a filemark during a 1871 * if the tape is crossing a filemark during a
@@ -1912,7 +1899,7 @@ static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1912 } 1899 }
1913 1900
1914 /* No more interrupts */ 1901 /* No more interrupts */
1915 if (!status.b.drq) { 1902 if ((stat & DRQ_STAT) == 0) {
1916#if IDETAPE_DEBUG_LOG 1903#if IDETAPE_DEBUG_LOG
1917 if (tape->debug_level >= 2) 1904 if (tape->debug_level >= 2)
1918 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred); 1905 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
@@ -1927,12 +1914,13 @@ static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1927 (++error_sim_count % 100) == 0) { 1914 (++error_sim_count % 100) == 0) {
1928 printk(KERN_INFO "ide-tape: %s: simulating error\n", 1915 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1929 tape->name); 1916 tape->name);
1930 status.b.check = 1; 1917 stat |= ERR_STAT;
1931 } 1918 }
1932#endif 1919#endif
1933 if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) 1920 if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1934 status.b.check = 0; 1921 stat &= ~ERR_STAT;
1935 if (status.b.check || test_bit(PC_DMA_ERROR, &pc->flags)) { /* Error detected */ 1922 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1923 /* Error detected */
1936#if IDETAPE_DEBUG_LOG 1924#if IDETAPE_DEBUG_LOG
1937 if (tape->debug_level >= 1) 1925 if (tape->debug_level >= 1)
1938 printk(KERN_INFO "ide-tape: %s: I/O error\n", 1926 printk(KERN_INFO "ide-tape: %s: I/O error\n",
@@ -1951,7 +1939,7 @@ static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1951 } 1939 }
1952 pc->error = 0; 1940 pc->error = 0;
1953 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) && 1941 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
1954 !status.b.dsc) { 1942 (stat & SEEK_STAT) == 0) {
1955 /* Media access command */ 1943 /* Media access command */
1956 tape->dsc_polling_start = jiffies; 1944 tape->dsc_polling_start = jiffies;
1957 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST; 1945 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
@@ -1973,30 +1961,30 @@ static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1973 return ide_do_reset(drive); 1961 return ide_do_reset(drive);
1974 } 1962 }
1975 /* Get the number of bytes to transfer on this interrupt. */ 1963 /* Get the number of bytes to transfer on this interrupt. */
1976 bcount.b.high = hwif->INB(IDE_BCOUNTH_REG); 1964 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1977 bcount.b.low = hwif->INB(IDE_BCOUNTL_REG); 1965 hwif->INB(IDE_BCOUNTL_REG);
1978 1966
1979 ireason.all = hwif->INB(IDE_IREASON_REG); 1967 ireason = hwif->INB(IDE_IREASON_REG);
1980 1968
1981 if (ireason.b.cod) { 1969 if (ireason & CD) {
1982 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n"); 1970 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1983 return ide_do_reset(drive); 1971 return ide_do_reset(drive);
1984 } 1972 }
1985 if (ireason.b.io == test_bit(PC_WRITING, &pc->flags)) { 1973 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
1986 /* Hopefully, we will never get here */ 1974 /* Hopefully, we will never get here */
1987 printk(KERN_ERR "ide-tape: We wanted to %s, ", 1975 printk(KERN_ERR "ide-tape: We wanted to %s, ",
1988 ireason.b.io ? "Write":"Read"); 1976 (ireason & IO) ? "Write" : "Read");
1989 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n", 1977 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
1990 ireason.b.io ? "Read":"Write"); 1978 (ireason & IO) ? "Read" : "Write");
1991 return ide_do_reset(drive); 1979 return ide_do_reset(drive);
1992 } 1980 }
1993 if (!test_bit(PC_WRITING, &pc->flags)) { 1981 if (!test_bit(PC_WRITING, &pc->flags)) {
1994 /* Reading - Check that we have enough space */ 1982 /* Reading - Check that we have enough space */
1995 temp = pc->actually_transferred + bcount.all; 1983 temp = pc->actually_transferred + bcount;
1996 if (temp > pc->request_transfer) { 1984 if (temp > pc->request_transfer) {
1997 if (temp > pc->buffer_size) { 1985 if (temp > pc->buffer_size) {
1998 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n"); 1986 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
1999 idetape_discard_data(drive, bcount.all); 1987 idetape_discard_data(drive, bcount);
2000 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); 1988 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2001 return ide_started; 1989 return ide_started;
2002 } 1990 }
@@ -2008,23 +1996,26 @@ static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
2008 } 1996 }
2009 if (test_bit(PC_WRITING, &pc->flags)) { 1997 if (test_bit(PC_WRITING, &pc->flags)) {
2010 if (pc->bh != NULL) 1998 if (pc->bh != NULL)
2011 idetape_output_buffers(drive, pc, bcount.all); 1999 idetape_output_buffers(drive, pc, bcount);
2012 else 2000 else
2013 /* Write the current buffer */ 2001 /* Write the current buffer */
2014 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all); 2002 hwif->atapi_output_bytes(drive, pc->current_position,
2003 bcount);
2015 } else { 2004 } else {
2016 if (pc->bh != NULL) 2005 if (pc->bh != NULL)
2017 idetape_input_buffers(drive, pc, bcount.all); 2006 idetape_input_buffers(drive, pc, bcount);
2018 else 2007 else
2019 /* Read the current buffer */ 2008 /* Read the current buffer */
2020 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all); 2009 hwif->atapi_input_bytes(drive, pc->current_position,
2010 bcount);
2021 } 2011 }
2022 /* Update the current position */ 2012 /* Update the current position */
2023 pc->actually_transferred += bcount.all; 2013 pc->actually_transferred += bcount;
2024 pc->current_position += bcount.all; 2014 pc->current_position += bcount;
2025#if IDETAPE_DEBUG_LOG 2015#if IDETAPE_DEBUG_LOG
2026 if (tape->debug_level >= 2) 2016 if (tape->debug_level >= 2)
2027 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes on that interrupt\n", pc->c[0], bcount.all); 2017 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
2018 "on that interrupt\n", pc->c[0], bcount);
2028#endif 2019#endif
2029 /* And set the interrupt handler again */ 2020 /* And set the interrupt handler again */
2030 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); 2021 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
@@ -2078,28 +2069,28 @@ static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
2078 ide_hwif_t *hwif = drive->hwif; 2069 ide_hwif_t *hwif = drive->hwif;
2079 idetape_tape_t *tape = drive->driver_data; 2070 idetape_tape_t *tape = drive->driver_data;
2080 idetape_pc_t *pc = tape->pc; 2071 idetape_pc_t *pc = tape->pc;
2081 atapi_ireason_t ireason;
2082 int retries = 100; 2072 int retries = 100;
2083 ide_startstop_t startstop; 2073 ide_startstop_t startstop;
2074 u8 ireason;
2084 2075
2085 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) { 2076 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
2086 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n"); 2077 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
2087 return startstop; 2078 return startstop;
2088 } 2079 }
2089 ireason.all = hwif->INB(IDE_IREASON_REG); 2080 ireason = hwif->INB(IDE_IREASON_REG);
2090 while (retries-- && (!ireason.b.cod || ireason.b.io)) { 2081 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
2091 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing " 2082 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
2092 "a packet command, retrying\n"); 2083 "a packet command, retrying\n");
2093 udelay(100); 2084 udelay(100);
2094 ireason.all = hwif->INB(IDE_IREASON_REG); 2085 ireason = hwif->INB(IDE_IREASON_REG);
2095 if (retries == 0) { 2086 if (retries == 0) {
2096 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while " 2087 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
2097 "issuing a packet command, ignoring\n"); 2088 "issuing a packet command, ignoring\n");
2098 ireason.b.cod = 1; 2089 ireason |= CD;
2099 ireason.b.io = 0; 2090 ireason &= ~IO;
2100 } 2091 }
2101 } 2092 }
2102 if (!ireason.b.cod || ireason.b.io) { 2093 if ((ireason & CD) == 0 || (ireason & IO)) {
2103 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing " 2094 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
2104 "a packet command\n"); 2095 "a packet command\n");
2105 return ide_do_reset(drive); 2096 return ide_do_reset(drive);
@@ -2120,8 +2111,8 @@ static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape
2120{ 2111{
2121 ide_hwif_t *hwif = drive->hwif; 2112 ide_hwif_t *hwif = drive->hwif;
2122 idetape_tape_t *tape = drive->driver_data; 2113 idetape_tape_t *tape = drive->driver_data;
2123 atapi_bcount_t bcount;
2124 int dma_ok = 0; 2114 int dma_ok = 0;
2115 u16 bcount;
2125 2116
2126#if IDETAPE_DEBUG_BUGS 2117#if IDETAPE_DEBUG_BUGS
2127 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD && 2118 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
@@ -2170,7 +2161,7 @@ static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape
2170 pc->actually_transferred = 0; 2161 pc->actually_transferred = 0;
2171 pc->current_position = pc->buffer; 2162 pc->current_position = pc->buffer;
2172 /* Request to transfer the entire buffer at once */ 2163 /* Request to transfer the entire buffer at once */
2173 bcount.all = pc->request_transfer; 2164 bcount = pc->request_transfer;
2174 2165
2175 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) { 2166 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
2176 printk(KERN_WARNING "ide-tape: DMA disabled, " 2167 printk(KERN_WARNING "ide-tape: DMA disabled, "
@@ -2180,12 +2171,9 @@ static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape
2180 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma) 2171 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
2181 dma_ok = !hwif->dma_setup(drive); 2172 dma_ok = !hwif->dma_setup(drive);
2182 2173
2183 if (IDE_CONTROL_REG) 2174 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
2184 hwif->OUTB(drive->ctl, IDE_CONTROL_REG); 2175 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
2185 hwif->OUTB(dma_ok ? 1 : 0, IDE_FEATURE_REG); /* Use PIO/DMA */ 2176
2186 hwif->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
2187 hwif->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
2188 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
2189 if (dma_ok) /* Will begin DMA later */ 2177 if (dma_ok) /* Will begin DMA later */
2190 set_bit(PC_DMA_IN_PROGRESS, &pc->flags); 2178 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
2191 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) { 2179 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
@@ -2295,11 +2283,11 @@ static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
2295{ 2283{
2296 idetape_tape_t *tape = drive->driver_data; 2284 idetape_tape_t *tape = drive->driver_data;
2297 idetape_pc_t *pc = tape->pc; 2285 idetape_pc_t *pc = tape->pc;
2298 atapi_status_t status; 2286 u8 stat;
2299 2287
2300 status.all = HWIF(drive)->INB(IDE_STATUS_REG); 2288 stat = drive->hwif->INB(IDE_STATUS_REG);
2301 if (status.b.dsc) { 2289 if (stat & SEEK_STAT) {
2302 if (status.b.check) { 2290 if (stat & ERR_STAT) {
2303 /* Error detected */ 2291 /* Error detected */
2304 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD) 2292 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
2305 printk(KERN_ERR "ide-tape: %s: I/O error, ", 2293 printk(KERN_ERR "ide-tape: %s: I/O error, ",
@@ -2417,7 +2405,7 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
2417 idetape_tape_t *tape = drive->driver_data; 2405 idetape_tape_t *tape = drive->driver_data;
2418 idetape_pc_t *pc = NULL; 2406 idetape_pc_t *pc = NULL;
2419 struct request *postponed_rq = tape->postponed_rq; 2407 struct request *postponed_rq = tape->postponed_rq;
2420 atapi_status_t status; 2408 u8 stat;
2421 2409
2422#if IDETAPE_DEBUG_LOG 2410#if IDETAPE_DEBUG_LOG
2423#if 0 2411#if 0
@@ -2465,7 +2453,7 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
2465 * If the tape is still busy, postpone our request and service 2453 * If the tape is still busy, postpone our request and service
2466 * the other device meanwhile. 2454 * the other device meanwhile.
2467 */ 2455 */
2468 status.all = HWIF(drive)->INB(IDE_STATUS_REG); 2456 stat = drive->hwif->INB(IDE_STATUS_REG);
2469 2457
2470 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2)) 2458 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
2471 set_bit(IDETAPE_IGNORE_DSC, &tape->flags); 2459 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
@@ -2481,7 +2469,7 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
2481 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time); 2469 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2482 calculate_speeds(drive); 2470 calculate_speeds(drive);
2483 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) && 2471 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
2484 !status.b.dsc) { 2472 (stat & SEEK_STAT) == 0) {
2485 if (postponed_rq == NULL) { 2473 if (postponed_rq == NULL) {
2486 tape->dsc_polling_start = jiffies; 2474 tape->dsc_polling_start = jiffies;
2487 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency; 2475 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
@@ -2502,9 +2490,6 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
2502 } 2490 }
2503 if (rq->cmd[0] & REQ_IDETAPE_READ) { 2491 if (rq->cmd[0] & REQ_IDETAPE_READ) {
2504 tape->buffer_head++; 2492 tape->buffer_head++;
2505#if USE_IOTRACE
2506 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2507#endif
2508 tape->postpone_cnt = 0; 2493 tape->postpone_cnt = 0;
2509 pc = idetape_next_pc_storage(drive); 2494 pc = idetape_next_pc_storage(drive);
2510 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special); 2495 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
@@ -2512,9 +2497,6 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
2512 } 2497 }
2513 if (rq->cmd[0] & REQ_IDETAPE_WRITE) { 2498 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
2514 tape->buffer_head++; 2499 tape->buffer_head++;
2515#if USE_IOTRACE
2516 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2517#endif
2518 tape->postpone_cnt = 0; 2500 tape->postpone_cnt = 0;
2519 pc = idetape_next_pc_storage(drive); 2501 pc = idetape_next_pc_storage(drive);
2520 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special); 2502 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
@@ -3241,9 +3223,6 @@ static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
3241 idetape_switch_buffers(tape, new_stage); 3223 idetape_switch_buffers(tape, new_stage);
3242 idetape_add_stage_tail(drive, new_stage); 3224 idetape_add_stage_tail(drive, new_stage);
3243 tape->pipeline_head++; 3225 tape->pipeline_head++;
3244#if USE_IOTRACE
3245 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
3246#endif
3247 calculate_speeds(drive); 3226 calculate_speeds(drive);
3248 3227
3249 /* 3228 /*
@@ -3493,9 +3472,6 @@ static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
3493 idetape_remove_stage_head(drive); 3472 idetape_remove_stage_head(drive);
3494 spin_unlock_irqrestore(&tape->spinlock, flags); 3473 spin_unlock_irqrestore(&tape->spinlock, flags);
3495 tape->pipeline_head++; 3474 tape->pipeline_head++;
3496#if USE_IOTRACE
3497 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
3498#endif
3499 calculate_speeds(drive); 3475 calculate_speeds(drive);
3500 } 3476 }
3501#if IDETAPE_DEBUG_BUGS 3477#if IDETAPE_DEBUG_BUGS
diff --git a/drivers/ide/ide-taskfile.c b/drivers/ide/ide-taskfile.c
index 2b60f1b0437e..2d63ea9ee61b 100644
--- a/drivers/ide/ide-taskfile.c
+++ b/drivers/ide/ide-taskfile.c
@@ -63,65 +63,78 @@ static void taskfile_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
63 } 63 }
64} 64}
65 65
66void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
67{
68 ide_hwif_t *hwif = drive->hwif;
69 struct ide_taskfile *tf = &task->tf;
70 u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
71
72 if (task->tf_flags & IDE_TFLAG_FLAGGED)
73 HIHI = 0xFF;
74
75#ifdef DEBUG
76 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
77 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
78 drive->name, tf->feature, tf->nsect, tf->lbal,
79 tf->lbam, tf->lbah, tf->device, tf->command);
80#endif
81
82 if (IDE_CONTROL_REG)
83 hwif->OUTB(drive->ctl, IDE_CONTROL_REG); /* clear nIEN */
84
85 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
86 SELECT_MASK(drive, 0);
87
88 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
89 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
90
91 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
92 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
93 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
94 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
95 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
96 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
97 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
98 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
99 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
100 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
101
102 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
103 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
104 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
105 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
106 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
107 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
108 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
109 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
110 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
111 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
112
113 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
114 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
115}
116
66int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf) 117int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
67{ 118{
68 ide_task_t args; 119 ide_task_t args;
120
69 memset(&args, 0, sizeof(ide_task_t)); 121 memset(&args, 0, sizeof(ide_task_t));
70 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01; 122 args.tf.nsect = 0x01;
71 if (drive->media == ide_disk) 123 if (drive->media == ide_disk)
72 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_IDENTIFY; 124 args.tf.command = WIN_IDENTIFY;
73 else 125 else
74 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_PIDENTIFY; 126 args.tf.command = WIN_PIDENTIFY;
75 args.command_type = IDE_DRIVE_TASK_IN; 127 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
76 args.data_phase = TASKFILE_IN; 128 args.data_phase = TASKFILE_IN;
77 args.handler = &task_in_intr; 129 return ide_raw_taskfile(drive, &args, buf, 1);
78 return ide_raw_taskfile(drive, &args, buf);
79} 130}
80 131
81ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task) 132static int inline task_dma_ok(ide_task_t *task)
82{ 133{
83 ide_hwif_t *hwif = HWIF(drive); 134 if (blk_fs_request(task->rq) || (task->tf_flags & IDE_TFLAG_FLAGGED))
84 task_struct_t *taskfile = (task_struct_t *) task->tfRegister; 135 return 1;
85 hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
86 u8 HIHI = (drive->addressing == 1) ? 0xE0 : 0xEF;
87
88 /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
89 if (IDE_CONTROL_REG) {
90 /* clear nIEN */
91 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
92 }
93 SELECT_MASK(drive, 0);
94
95 if (drive->addressing == 1) {
96 hwif->OUTB(hobfile->feature, IDE_FEATURE_REG);
97 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
98 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
99 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
100 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
101 }
102
103 hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
104 hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
105 hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
106 hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
107 hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
108
109 hwif->OUTB((taskfile->device_head & HIHI) | drive->select.all, IDE_SELECT_REG);
110
111 if (task->handler != NULL) {
112 if (task->prehandler != NULL) {
113 hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
114 ndelay(400); /* FIXME */
115 return task->prehandler(drive, task->rq);
116 }
117 ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
118 return ide_started;
119 }
120
121 if (!drive->using_dma)
122 return ide_stopped;
123 136
124 switch (taskfile->command) { 137 switch (task->tf.command) {
125 case WIN_WRITEDMA_ONCE: 138 case WIN_WRITEDMA_ONCE:
126 case WIN_WRITEDMA: 139 case WIN_WRITEDMA:
127 case WIN_WRITEDMA_EXT: 140 case WIN_WRITEDMA_EXT:
@@ -129,24 +142,79 @@ ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
129 case WIN_READDMA: 142 case WIN_READDMA:
130 case WIN_READDMA_EXT: 143 case WIN_READDMA_EXT:
131 case WIN_IDENTIFY_DMA: 144 case WIN_IDENTIFY_DMA:
132 if (!hwif->dma_setup(drive)) { 145 return 1;
133 hwif->dma_exec_cmd(drive, taskfile->command);
134 hwif->dma_start(drive);
135 return ide_started;
136 }
137 break;
138 default:
139 if (task->handler == NULL)
140 return ide_stopped;
141 } 146 }
142 147
143 return ide_stopped; 148 return 0;
149}
150
151static ide_startstop_t task_no_data_intr(ide_drive_t *);
152static ide_startstop_t set_geometry_intr(ide_drive_t *);
153static ide_startstop_t recal_intr(ide_drive_t *);
154static ide_startstop_t set_multmode_intr(ide_drive_t *);
155static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
156static ide_startstop_t task_in_intr(ide_drive_t *);
157
158ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
159{
160 ide_hwif_t *hwif = HWIF(drive);
161 struct ide_taskfile *tf = &task->tf;
162 ide_handler_t *handler = NULL;
163
164 if (task->data_phase == TASKFILE_MULTI_IN ||
165 task->data_phase == TASKFILE_MULTI_OUT) {
166 if (!drive->mult_count) {
167 printk(KERN_ERR "%s: multimode not set!\n",
168 drive->name);
169 return ide_stopped;
170 }
171 }
172
173 if (task->tf_flags & IDE_TFLAG_FLAGGED)
174 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
175
176 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0)
177 ide_tf_load(drive, task);
178
179 switch (task->data_phase) {
180 case TASKFILE_MULTI_OUT:
181 case TASKFILE_OUT:
182 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
183 ndelay(400); /* FIXME */
184 return pre_task_out_intr(drive, task->rq);
185 case TASKFILE_MULTI_IN:
186 case TASKFILE_IN:
187 handler = task_in_intr;
188 /* fall-through */
189 case TASKFILE_NO_DATA:
190 if (handler == NULL)
191 handler = task_no_data_intr;
192 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
193 if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
194 switch (tf->command) {
195 case WIN_SPECIFY: handler = set_geometry_intr; break;
196 case WIN_RESTORE: handler = recal_intr; break;
197 case WIN_SETMULT: handler = set_multmode_intr; break;
198 }
199 }
200 ide_execute_command(drive, tf->command, handler,
201 WAIT_WORSTCASE, NULL);
202 return ide_started;
203 default:
204 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
205 hwif->dma_setup(drive))
206 return ide_stopped;
207 hwif->dma_exec_cmd(drive, tf->command);
208 hwif->dma_start(drive);
209 return ide_started;
210 }
144} 211}
212EXPORT_SYMBOL_GPL(do_rw_taskfile);
145 213
146/* 214/*
147 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd. 215 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
148 */ 216 */
149ide_startstop_t set_multmode_intr (ide_drive_t *drive) 217static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
150{ 218{
151 ide_hwif_t *hwif = HWIF(drive); 219 ide_hwif_t *hwif = HWIF(drive);
152 u8 stat; 220 u8 stat;
@@ -164,7 +232,7 @@ ide_startstop_t set_multmode_intr (ide_drive_t *drive)
164/* 232/*
165 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd. 233 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
166 */ 234 */
167ide_startstop_t set_geometry_intr (ide_drive_t *drive) 235static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
168{ 236{
169 ide_hwif_t *hwif = HWIF(drive); 237 ide_hwif_t *hwif = HWIF(drive);
170 int retries = 5; 238 int retries = 5;
@@ -187,7 +255,7 @@ ide_startstop_t set_geometry_intr (ide_drive_t *drive)
187/* 255/*
188 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd. 256 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
189 */ 257 */
190ide_startstop_t recal_intr (ide_drive_t *drive) 258static ide_startstop_t recal_intr(ide_drive_t *drive)
191{ 259{
192 ide_hwif_t *hwif = HWIF(drive); 260 ide_hwif_t *hwif = HWIF(drive);
193 u8 stat; 261 u8 stat;
@@ -200,7 +268,7 @@ ide_startstop_t recal_intr (ide_drive_t *drive)
200/* 268/*
201 * Handler for commands without a data phase 269 * Handler for commands without a data phase
202 */ 270 */
203ide_startstop_t task_no_data_intr (ide_drive_t *drive) 271static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
204{ 272{
205 ide_task_t *args = HWGROUP(drive)->rq->special; 273 ide_task_t *args = HWGROUP(drive)->rq->special;
206 ide_hwif_t *hwif = HWIF(drive); 274 ide_hwif_t *hwif = HWIF(drive);
@@ -217,8 +285,6 @@ ide_startstop_t task_no_data_intr (ide_drive_t *drive)
217 return ide_stopped; 285 return ide_stopped;
218} 286}
219 287
220EXPORT_SYMBOL(task_no_data_intr);
221
222static u8 wait_drive_not_busy(ide_drive_t *drive) 288static u8 wait_drive_not_busy(ide_drive_t *drive)
223{ 289{
224 ide_hwif_t *hwif = HWIF(drive); 290 ide_hwif_t *hwif = HWIF(drive);
@@ -363,7 +429,7 @@ static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
363 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) { 429 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
364 ide_task_t *task = rq->special; 430 ide_task_t *task = rq->special;
365 431
366 if (task->tf_out_flags.all) { 432 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
367 u8 err = drive->hwif->INB(IDE_ERROR_REG); 433 u8 err = drive->hwif->INB(IDE_ERROR_REG);
368 ide_end_drive_cmd(drive, stat, err); 434 ide_end_drive_cmd(drive, stat, err);
369 return; 435 return;
@@ -382,7 +448,7 @@ static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
382/* 448/*
383 * Handler for command with PIO data-in phase (Read/Read Multiple). 449 * Handler for command with PIO data-in phase (Read/Read Multiple).
384 */ 450 */
385ide_startstop_t task_in_intr (ide_drive_t *drive) 451static ide_startstop_t task_in_intr(ide_drive_t *drive)
386{ 452{
387 ide_hwif_t *hwif = drive->hwif; 453 ide_hwif_t *hwif = drive->hwif;
388 struct request *rq = HWGROUP(drive)->rq; 454 struct request *rq = HWGROUP(drive)->rq;
@@ -413,7 +479,6 @@ ide_startstop_t task_in_intr (ide_drive_t *drive)
413 479
414 return ide_started; 480 return ide_started;
415} 481}
416EXPORT_SYMBOL(task_in_intr);
417 482
418/* 483/*
419 * Handler for command with PIO data-out phase (Write/Write Multiple). 484 * Handler for command with PIO data-out phase (Write/Write Multiple).
@@ -443,7 +508,7 @@ static ide_startstop_t task_out_intr (ide_drive_t *drive)
443 return ide_started; 508 return ide_started;
444} 509}
445 510
446ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq) 511static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
447{ 512{
448 ide_startstop_t startstop; 513 ide_startstop_t startstop;
449 514
@@ -464,9 +529,8 @@ ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
464 529
465 return ide_started; 530 return ide_started;
466} 531}
467EXPORT_SYMBOL(pre_task_out_intr);
468 532
469static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf) 533int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
470{ 534{
471 struct request rq; 535 struct request rq;
472 536
@@ -481,36 +545,27 @@ static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long
481 * if we would find a solution to transfer any size. 545 * if we would find a solution to transfer any size.
482 * To support special commands like READ LONG. 546 * To support special commands like READ LONG.
483 */ 547 */
484 if (args->command_type != IDE_DRIVE_TASK_NO_DATA) { 548 rq.hard_nr_sectors = rq.nr_sectors = nsect;
485 if (data_size == 0) 549 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
486 rq.nr_sectors = (args->hobRegister[IDE_NSECTOR_OFFSET] << 8) | args->tfRegister[IDE_NSECTOR_OFFSET];
487 else
488 rq.nr_sectors = data_size / SECTOR_SIZE;
489
490 if (!rq.nr_sectors) {
491 printk(KERN_ERR "%s: in/out command without data\n",
492 drive->name);
493 return -EFAULT;
494 }
495 550
496 rq.hard_nr_sectors = rq.nr_sectors; 551 if (task->tf_flags & IDE_TFLAG_WRITE)
497 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors; 552 rq.cmd_flags |= REQ_RW;
498 553
499 if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE) 554 rq.special = task;
500 rq.cmd_flags |= REQ_RW; 555 task->rq = &rq;
501 }
502 556
503 rq.special = args;
504 args->rq = &rq;
505 return ide_do_drive_cmd(drive, &rq, ide_wait); 557 return ide_do_drive_cmd(drive, &rq, ide_wait);
506} 558}
507 559
508int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf) 560EXPORT_SYMBOL(ide_raw_taskfile);
561
562int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
509{ 563{
510 return ide_diag_taskfile(drive, args, 0, buf); 564 task->data_phase = TASKFILE_NO_DATA;
511}
512 565
513EXPORT_SYMBOL(ide_raw_taskfile); 566 return ide_raw_taskfile(drive, task, NULL, 0);
567}
568EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
514 569
515#ifdef CONFIG_IDE_TASK_IOCTL 570#ifdef CONFIG_IDE_TASK_IOCTL
516int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg) 571int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
@@ -519,12 +574,12 @@ int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
519 ide_task_t args; 574 ide_task_t args;
520 u8 *outbuf = NULL; 575 u8 *outbuf = NULL;
521 u8 *inbuf = NULL; 576 u8 *inbuf = NULL;
522 task_ioreg_t *argsptr = args.tfRegister; 577 u8 *data_buf = NULL;
523 task_ioreg_t *hobsptr = args.hobRegister;
524 int err = 0; 578 int err = 0;
525 int tasksize = sizeof(struct ide_task_request_s); 579 int tasksize = sizeof(struct ide_task_request_s);
526 unsigned int taskin = 0; 580 unsigned int taskin = 0;
527 unsigned int taskout = 0; 581 unsigned int taskout = 0;
582 u16 nsect = 0;
528 u8 io_32bit = drive->io_32bit; 583 u8 io_32bit = drive->io_32bit;
529 char __user *buf = (char __user *)arg; 584 char __user *buf = (char __user *)arg;
530 585
@@ -572,24 +627,52 @@ int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
572 } 627 }
573 628
574 memset(&args, 0, sizeof(ide_task_t)); 629 memset(&args, 0, sizeof(ide_task_t));
575 memcpy(argsptr, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
576 memcpy(hobsptr, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE);
577 630
578 args.tf_in_flags = req_task->in_flags; 631 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
579 args.tf_out_flags = req_task->out_flags; 632 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
580 args.data_phase = req_task->data_phase; 633
581 args.command_type = req_task->req_cmd; 634 args.data_phase = req_task->data_phase;
635
636 args.tf_flags = IDE_TFLAG_OUT_DEVICE;
637 if (drive->addressing == 1)
638 args.tf_flags |= IDE_TFLAG_LBA48;
639
640 if (req_task->out_flags.all) {
641 args.tf_flags |= IDE_TFLAG_FLAGGED;
642
643 if (req_task->out_flags.b.data)
644 args.tf_flags |= IDE_TFLAG_OUT_DATA;
645
646 if (req_task->out_flags.b.nsector_hob)
647 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
648 if (req_task->out_flags.b.sector_hob)
649 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
650 if (req_task->out_flags.b.lcyl_hob)
651 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
652 if (req_task->out_flags.b.hcyl_hob)
653 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
654
655 if (req_task->out_flags.b.error_feature)
656 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
657 if (req_task->out_flags.b.nsector)
658 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
659 if (req_task->out_flags.b.sector)
660 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
661 if (req_task->out_flags.b.lcyl)
662 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
663 if (req_task->out_flags.b.hcyl)
664 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
665 } else {
666 args.tf_flags |= IDE_TFLAG_OUT_TF;
667 if (args.tf_flags & IDE_TFLAG_LBA48)
668 args.tf_flags |= IDE_TFLAG_OUT_HOB;
669 }
670
671 if (req_task->in_flags.b.data)
672 args.tf_flags |= IDE_TFLAG_IN_DATA;
582 673
583 drive->io_32bit = 0; 674 drive->io_32bit = 0;
584 switch(req_task->data_phase) { 675 switch(req_task->data_phase) {
585 case TASKFILE_OUT_DMAQ:
586 case TASKFILE_OUT_DMA:
587 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
588 break;
589 case TASKFILE_IN_DMAQ:
590 case TASKFILE_IN_DMA:
591 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
592 break;
593 case TASKFILE_MULTI_OUT: 676 case TASKFILE_MULTI_OUT:
594 if (!drive->mult_count) { 677 if (!drive->mult_count) {
595 /* (hs): give up if multcount is not set */ 678 /* (hs): give up if multcount is not set */
@@ -601,9 +684,11 @@ int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
601 } 684 }
602 /* fall through */ 685 /* fall through */
603 case TASKFILE_OUT: 686 case TASKFILE_OUT:
604 args.prehandler = &pre_task_out_intr; 687 /* fall through */
605 args.handler = &task_out_intr; 688 case TASKFILE_OUT_DMAQ:
606 err = ide_diag_taskfile(drive, &args, taskout, outbuf); 689 case TASKFILE_OUT_DMA:
690 nsect = taskout / SECTOR_SIZE;
691 data_buf = outbuf;
607 break; 692 break;
608 case TASKFILE_MULTI_IN: 693 case TASKFILE_MULTI_IN:
609 if (!drive->mult_count) { 694 if (!drive->mult_count) {
@@ -616,22 +701,46 @@ int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
616 } 701 }
617 /* fall through */ 702 /* fall through */
618 case TASKFILE_IN: 703 case TASKFILE_IN:
619 args.handler = &task_in_intr; 704 /* fall through */
620 err = ide_diag_taskfile(drive, &args, taskin, inbuf); 705 case TASKFILE_IN_DMAQ:
706 case TASKFILE_IN_DMA:
707 nsect = taskin / SECTOR_SIZE;
708 data_buf = inbuf;
621 break; 709 break;
622 case TASKFILE_NO_DATA: 710 case TASKFILE_NO_DATA:
623 args.handler = &task_no_data_intr;
624 err = ide_diag_taskfile(drive, &args, 0, NULL);
625 break; 711 break;
626 default: 712 default:
627 err = -EFAULT; 713 err = -EFAULT;
628 goto abort; 714 goto abort;
629 } 715 }
630 716
631 memcpy(req_task->io_ports, &(args.tfRegister), HDIO_DRIVE_TASK_HDR_SIZE); 717 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
632 memcpy(req_task->hob_ports, &(args.hobRegister), HDIO_DRIVE_HOB_HDR_SIZE); 718 nsect = 0;
633 req_task->in_flags = args.tf_in_flags; 719 else if (!nsect) {
634 req_task->out_flags = args.tf_out_flags; 720 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
721
722 if (!nsect) {
723 printk(KERN_ERR "%s: in/out command without data\n",
724 drive->name);
725 err = -EFAULT;
726 goto abort;
727 }
728 }
729
730 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
731 args.tf_flags |= IDE_TFLAG_WRITE;
732
733 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
734
735 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
736 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
737
738 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
739 req_task->in_flags.all == 0) {
740 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
741 if (drive->addressing == 1)
742 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
743 }
635 744
636 if (copy_to_user(buf, req_task, tasksize)) { 745 if (copy_to_user(buf, req_task, tasksize)) {
637 err = -EFAULT; 746 err = -EFAULT;
@@ -688,6 +797,7 @@ int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
688 u8 xfer_rate = 0; 797 u8 xfer_rate = 0;
689 int argsize = 4; 798 int argsize = 4;
690 ide_task_t tfargs; 799 ide_task_t tfargs;
800 struct ide_taskfile *tf = &tfargs.tf;
691 801
692 if (NULL == (void *) arg) { 802 if (NULL == (void *) arg) {
693 struct request rq; 803 struct request rq;
@@ -699,13 +809,10 @@ int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
699 return -EFAULT; 809 return -EFAULT;
700 810
701 memset(&tfargs, 0, sizeof(ide_task_t)); 811 memset(&tfargs, 0, sizeof(ide_task_t));
702 tfargs.tfRegister[IDE_FEATURE_OFFSET] = args[2]; 812 tf->feature = args[2];
703 tfargs.tfRegister[IDE_NSECTOR_OFFSET] = args[3]; 813 tf->nsect = args[3];
704 tfargs.tfRegister[IDE_SECTOR_OFFSET] = args[1]; 814 tf->lbal = args[1];
705 tfargs.tfRegister[IDE_LCYL_OFFSET] = 0x00; 815 tf->command = args[0];
706 tfargs.tfRegister[IDE_HCYL_OFFSET] = 0x00;
707 tfargs.tfRegister[IDE_SELECT_OFFSET] = 0x00;
708 tfargs.tfRegister[IDE_COMMAND_OFFSET] = args[0];
709 816
710 if (args[3]) { 817 if (args[3]) {
711 argsize = 4 + (SECTOR_WORDS * 4 * args[3]); 818 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
@@ -734,135 +841,28 @@ abort:
734 return err; 841 return err;
735} 842}
736 843
737static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
738{
739 struct request rq;
740
741 ide_init_drive_cmd(&rq);
742 rq.cmd_type = REQ_TYPE_ATA_TASK;
743 rq.buffer = buf;
744 return ide_do_drive_cmd(drive, &rq, ide_wait);
745}
746
747int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg) 844int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
748{ 845{
749 void __user *p = (void __user *)arg; 846 void __user *p = (void __user *)arg;
750 int err = 0; 847 int err = 0;
751 u8 args[7], *argbuf = args; 848 u8 args[7];
752 int argsize = 7; 849 ide_task_t task;
753 850
754 if (copy_from_user(args, p, 7)) 851 if (copy_from_user(args, p, 7))
755 return -EFAULT; 852 return -EFAULT;
756 err = ide_wait_cmd_task(drive, argbuf);
757 if (copy_to_user(p, argbuf, argsize))
758 err = -EFAULT;
759 return err;
760}
761
762/*
763 * NOTICE: This is additions from IBM to provide a discrete interface,
764 * for selective taskregister access operations. Nice JOB Klaus!!!
765 * Glad to be able to work and co-develop this with you and IBM.
766 */
767ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
768{
769 ide_hwif_t *hwif = HWIF(drive);
770 task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
771 hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
772
773 if (task->data_phase == TASKFILE_MULTI_IN ||
774 task->data_phase == TASKFILE_MULTI_OUT) {
775 if (!drive->mult_count) {
776 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
777 return ide_stopped;
778 }
779 }
780
781 /*
782 * (ks) Check taskfile in flags.
783 * If set, then execute as it is defined.
784 * If not set, then define default settings.
785 * The default values are:
786 * read all taskfile registers (except data)
787 * read the hob registers (sector, nsector, lcyl, hcyl)
788 */
789 if (task->tf_in_flags.all == 0) {
790 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
791 if (drive->addressing == 1)
792 task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
793 }
794
795 /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
796 if (IDE_CONTROL_REG)
797 /* clear nIEN */
798 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
799 SELECT_MASK(drive, 0);
800
801 if (task->tf_out_flags.b.data) {
802 u16 data = taskfile->data + (hobfile->data << 8);
803 hwif->OUTW(data, IDE_DATA_REG);
804 }
805
806 /* (ks) send hob registers first */
807 if (task->tf_out_flags.b.nsector_hob)
808 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
809 if (task->tf_out_flags.b.sector_hob)
810 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
811 if (task->tf_out_flags.b.lcyl_hob)
812 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
813 if (task->tf_out_flags.b.hcyl_hob)
814 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
815
816 /* (ks) Send now the standard registers */
817 if (task->tf_out_flags.b.error_feature)
818 hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
819 /* refers to number of sectors to transfer */
820 if (task->tf_out_flags.b.nsector)
821 hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
822 /* refers to sector offset or start sector */
823 if (task->tf_out_flags.b.sector)
824 hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
825 if (task->tf_out_flags.b.lcyl)
826 hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
827 if (task->tf_out_flags.b.hcyl)
828 hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
829
830 /*
831 * (ks) In the flagged taskfile approch, we will use all specified
832 * registers and the register value will not be changed, except the
833 * select bit (master/slave) in the drive_head register. We must make
834 * sure that the desired drive is selected.
835 */
836 hwif->OUTB(taskfile->device_head | drive->select.all, IDE_SELECT_REG);
837 switch(task->data_phase) {
838 853
839 case TASKFILE_OUT_DMAQ: 854 memset(&task, 0, sizeof(task));
840 case TASKFILE_OUT_DMA: 855 memcpy(&task.tf_array[7], &args[1], 6);
841 case TASKFILE_IN_DMAQ: 856 task.tf.command = args[0];
842 case TASKFILE_IN_DMA: 857 task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
843 if (!drive->using_dma)
844 break;
845 858
846 if (!hwif->dma_setup(drive)) { 859 err = ide_no_data_taskfile(drive, &task);
847 hwif->dma_exec_cmd(drive, taskfile->command);
848 hwif->dma_start(drive);
849 return ide_started;
850 }
851 break;
852 860
853 default: 861 args[0] = task.tf.command;
854 if (task->handler == NULL) 862 memcpy(&args[1], &task.tf_array[7], 6);
855 return ide_stopped;
856 863
857 /* Issue the command */ 864 if (copy_to_user(p, args, 7))
858 if (task->prehandler) { 865 err = -EFAULT;
859 hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
860 ndelay(400); /* FIXME */
861 return task->prehandler(drive, task->rq);
862 }
863 ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
864 return ide_started;
865 }
866 866
867 return ide_stopped; 867 return err;
868} 868}
diff --git a/drivers/ide/ide.c b/drivers/ide/ide.c
index 54943da6e4e5..c6d4f630e18a 100644
--- a/drivers/ide/ide.c
+++ b/drivers/ide/ide.c
@@ -424,7 +424,6 @@ static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
424 hwif->reset_poll = tmp_hwif->reset_poll; 424 hwif->reset_poll = tmp_hwif->reset_poll;
425 hwif->pre_reset = tmp_hwif->pre_reset; 425 hwif->pre_reset = tmp_hwif->pre_reset;
426 hwif->resetproc = tmp_hwif->resetproc; 426 hwif->resetproc = tmp_hwif->resetproc;
427 hwif->intrproc = tmp_hwif->intrproc;
428 hwif->maskproc = tmp_hwif->maskproc; 427 hwif->maskproc = tmp_hwif->maskproc;
429 hwif->quirkproc = tmp_hwif->quirkproc; 428 hwif->quirkproc = tmp_hwif->quirkproc;
430 hwif->busproc = tmp_hwif->busproc; 429 hwif->busproc = tmp_hwif->busproc;
@@ -468,7 +467,6 @@ static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
468#endif 467#endif
469 468
470 hwif->dma_base = tmp_hwif->dma_base; 469 hwif->dma_base = tmp_hwif->dma_base;
471 hwif->dma_master = tmp_hwif->dma_master;
472 hwif->dma_command = tmp_hwif->dma_command; 470 hwif->dma_command = tmp_hwif->dma_command;
473 hwif->dma_vendor1 = tmp_hwif->dma_vendor1; 471 hwif->dma_vendor1 = tmp_hwif->dma_vendor1;
474 hwif->dma_status = tmp_hwif->dma_status; 472 hwif->dma_status = tmp_hwif->dma_status;
@@ -602,7 +600,6 @@ void ide_unregister(unsigned int index)
602 (void) ide_release_dma(hwif); 600 (void) ide_release_dma(hwif);
603 601
604 hwif->dma_base = 0; 602 hwif->dma_base = 0;
605 hwif->dma_master = 0;
606 hwif->dma_command = 0; 603 hwif->dma_command = 0;
607 hwif->dma_vendor1 = 0; 604 hwif->dma_vendor1 = 0;
608 hwif->dma_status = 0; 605 hwif->dma_status = 0;
@@ -854,8 +851,7 @@ int set_using_dma(ide_drive_t *drive, int arg)
854 err = 0; 851 err = 0;
855 852
856 if (arg) { 853 if (arg) {
857 hwif->dma_off_quietly(drive); 854 if (ide_set_dma(drive))
858 if (ide_set_dma(drive) || hwif->ide_dma_on(drive))
859 err = -EIO; 855 err = -EIO;
860 } else 856 } else
861 ide_dma_off(drive); 857 ide_dma_off(drive);
diff --git a/drivers/ide/mips/au1xxx-ide.c b/drivers/ide/mips/au1xxx-ide.c
index a4ce3ba15d61..a4d0d4ca73d0 100644
--- a/drivers/ide/mips/au1xxx-ide.c
+++ b/drivers/ide/mips/au1xxx-ide.c
@@ -198,8 +198,6 @@ static void auide_set_dma_mode(ide_drive_t *drive, const u8 speed)
198 198
199 break; 199 break;
200#endif 200#endif
201 default:
202 return;
203 } 201 }
204 202
205 au_writel(mem_sttime,MEM_STTIME2); 203 au_writel(mem_sttime,MEM_STTIME2);
diff --git a/drivers/ide/pci/aec62xx.c b/drivers/ide/pci/aec62xx.c
index 44268504ae43..7f4d1857d555 100644
--- a/drivers/ide/pci/aec62xx.c
+++ b/drivers/ide/pci/aec62xx.c
@@ -202,6 +202,7 @@ static const struct ide_port_info aec62xx_chipsets[] __devinitdata = {
202 .enablebits = {{0x4a,0x02,0x02}, {0x4a,0x04,0x04}}, 202 .enablebits = {{0x4a,0x02,0x02}, {0x4a,0x04,0x04}},
203 .host_flags = IDE_HFLAG_SERIALIZE | 203 .host_flags = IDE_HFLAG_SERIALIZE |
204 IDE_HFLAG_NO_ATAPI_DMA | 204 IDE_HFLAG_NO_ATAPI_DMA |
205 IDE_HFLAG_ABUSE_SET_DMA_MODE |
205 IDE_HFLAG_OFF_BOARD, 206 IDE_HFLAG_OFF_BOARD,
206 .pio_mask = ATA_PIO4, 207 .pio_mask = ATA_PIO4,
207 .mwdma_mask = ATA_MWDMA2, 208 .mwdma_mask = ATA_MWDMA2,
@@ -211,6 +212,7 @@ static const struct ide_port_info aec62xx_chipsets[] __devinitdata = {
211 .init_chipset = init_chipset_aec62xx, 212 .init_chipset = init_chipset_aec62xx,
212 .init_hwif = init_hwif_aec62xx, 213 .init_hwif = init_hwif_aec62xx,
213 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | IDE_HFLAG_NO_AUTODMA | 214 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | IDE_HFLAG_NO_AUTODMA |
215 IDE_HFLAG_ABUSE_SET_DMA_MODE |
214 IDE_HFLAG_OFF_BOARD, 216 IDE_HFLAG_OFF_BOARD,
215 .pio_mask = ATA_PIO4, 217 .pio_mask = ATA_PIO4,
216 .mwdma_mask = ATA_MWDMA2, 218 .mwdma_mask = ATA_MWDMA2,
@@ -220,7 +222,8 @@ static const struct ide_port_info aec62xx_chipsets[] __devinitdata = {
220 .init_chipset = init_chipset_aec62xx, 222 .init_chipset = init_chipset_aec62xx,
221 .init_hwif = init_hwif_aec62xx, 223 .init_hwif = init_hwif_aec62xx,
222 .enablebits = {{0x4a,0x02,0x02}, {0x4a,0x04,0x04}}, 224 .enablebits = {{0x4a,0x02,0x02}, {0x4a,0x04,0x04}},
223 .host_flags = IDE_HFLAG_NO_ATAPI_DMA, 225 .host_flags = IDE_HFLAG_NO_ATAPI_DMA |
226 IDE_HFLAG_ABUSE_SET_DMA_MODE,
224 .pio_mask = ATA_PIO4, 227 .pio_mask = ATA_PIO4,
225 .mwdma_mask = ATA_MWDMA2, 228 .mwdma_mask = ATA_MWDMA2,
226 .udma_mask = ATA_UDMA4, 229 .udma_mask = ATA_UDMA4,
@@ -228,7 +231,9 @@ static const struct ide_port_info aec62xx_chipsets[] __devinitdata = {
228 .name = "AEC6280", 231 .name = "AEC6280",
229 .init_chipset = init_chipset_aec62xx, 232 .init_chipset = init_chipset_aec62xx,
230 .init_hwif = init_hwif_aec62xx, 233 .init_hwif = init_hwif_aec62xx,
231 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | IDE_HFLAG_OFF_BOARD, 234 .host_flags = IDE_HFLAG_NO_ATAPI_DMA |
235 IDE_HFLAG_ABUSE_SET_DMA_MODE |
236 IDE_HFLAG_OFF_BOARD,
232 .pio_mask = ATA_PIO4, 237 .pio_mask = ATA_PIO4,
233 .mwdma_mask = ATA_MWDMA2, 238 .mwdma_mask = ATA_MWDMA2,
234 .udma_mask = ATA_UDMA5, 239 .udma_mask = ATA_UDMA5,
@@ -237,7 +242,9 @@ static const struct ide_port_info aec62xx_chipsets[] __devinitdata = {
237 .init_chipset = init_chipset_aec62xx, 242 .init_chipset = init_chipset_aec62xx,
238 .init_hwif = init_hwif_aec62xx, 243 .init_hwif = init_hwif_aec62xx,
239 .enablebits = {{0x4a,0x02,0x02}, {0x4a,0x04,0x04}}, 244 .enablebits = {{0x4a,0x02,0x02}, {0x4a,0x04,0x04}},
240 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | IDE_HFLAG_OFF_BOARD, 245 .host_flags = IDE_HFLAG_NO_ATAPI_DMA |
246 IDE_HFLAG_ABUSE_SET_DMA_MODE |
247 IDE_HFLAG_OFF_BOARD,
241 .pio_mask = ATA_PIO4, 248 .pio_mask = ATA_PIO4,
242 .mwdma_mask = ATA_MWDMA2, 249 .mwdma_mask = ATA_MWDMA2,
243 .udma_mask = ATA_UDMA5, 250 .udma_mask = ATA_UDMA5,
diff --git a/drivers/ide/pci/alim15x3.c b/drivers/ide/pci/alim15x3.c
index ce293936af4b..49aa82e412b6 100644
--- a/drivers/ide/pci/alim15x3.c
+++ b/drivers/ide/pci/alim15x3.c
@@ -402,9 +402,6 @@ static void ali_set_dma_mode(ide_drive_t *drive, const u8 speed)
402 u8 tmpbyte = 0x00; 402 u8 tmpbyte = 0x00;
403 int m5229_udma = (hwif->channel) ? 0x57 : 0x56; 403 int m5229_udma = (hwif->channel) ? 0x57 : 0x56;
404 404
405 if (speed < XFER_PIO_0)
406 return;
407
408 if (speed == XFER_UDMA_6) 405 if (speed == XFER_UDMA_6)
409 speed1 = 0x47; 406 speed1 = 0x47;
410 407
diff --git a/drivers/ide/pci/amd74xx.c b/drivers/ide/pci/amd74xx.c
index 8d4125ec252c..cee51fdafcf6 100644
--- a/drivers/ide/pci/amd74xx.c
+++ b/drivers/ide/pci/amd74xx.c
@@ -266,6 +266,7 @@ static void __devinit init_hwif_amd74xx(ide_hwif_t *hwif)
266#define IDE_HFLAGS_AMD \ 266#define IDE_HFLAGS_AMD \
267 (IDE_HFLAG_PIO_NO_BLACKLIST | \ 267 (IDE_HFLAG_PIO_NO_BLACKLIST | \
268 IDE_HFLAG_PIO_NO_DOWNGRADE | \ 268 IDE_HFLAG_PIO_NO_DOWNGRADE | \
269 IDE_HFLAG_ABUSE_SET_DMA_MODE | \
269 IDE_HFLAG_POST_SET_MODE | \ 270 IDE_HFLAG_POST_SET_MODE | \
270 IDE_HFLAG_IO_32BIT | \ 271 IDE_HFLAG_IO_32BIT | \
271 IDE_HFLAG_UNMASK_IRQS | \ 272 IDE_HFLAG_UNMASK_IRQS | \
diff --git a/drivers/ide/pci/atiixp.c b/drivers/ide/pci/atiixp.c
index ef8e0164ef7a..5ae26564fb72 100644
--- a/drivers/ide/pci/atiixp.c
+++ b/drivers/ide/pci/atiixp.c
@@ -133,9 +133,6 @@ static void atiixp_set_dma_mode(ide_drive_t *drive, const u8 speed)
133 u32 tmp32; 133 u32 tmp32;
134 u16 tmp16; 134 u16 tmp16;
135 135
136 if (speed < XFER_MW_DMA_0)
137 return;
138
139 spin_lock_irqsave(&atiixp_lock, flags); 136 spin_lock_irqsave(&atiixp_lock, flags);
140 137
141 save_mdma_mode[drive->dn] = 0; 138 save_mdma_mode[drive->dn] = 0;
diff --git a/drivers/ide/pci/cmd64x.c b/drivers/ide/pci/cmd64x.c
index bc553337b1be..0b1e9479f019 100644
--- a/drivers/ide/pci/cmd64x.c
+++ b/drivers/ide/pci/cmd64x.c
@@ -322,8 +322,6 @@ static void cmd64x_set_dma_mode(ide_drive_t *drive, const u8 speed)
322 case XFER_MW_DMA_0: 322 case XFER_MW_DMA_0:
323 program_cycle_times(drive, 480, 215); 323 program_cycle_times(drive, 480, 215);
324 break; 324 break;
325 default:
326 return;
327 } 325 }
328 326
329 if (speed >= XFER_SW_DMA_0) 327 if (speed >= XFER_SW_DMA_0)
@@ -333,14 +331,15 @@ static void cmd64x_set_dma_mode(ide_drive_t *drive, const u8 speed)
333static int cmd648_ide_dma_end (ide_drive_t *drive) 331static int cmd648_ide_dma_end (ide_drive_t *drive)
334{ 332{
335 ide_hwif_t *hwif = HWIF(drive); 333 ide_hwif_t *hwif = HWIF(drive);
334 unsigned long base = hwif->dma_base - (hwif->channel * 8);
336 int err = __ide_dma_end(drive); 335 int err = __ide_dma_end(drive);
337 u8 irq_mask = hwif->channel ? MRDMODE_INTR_CH1 : 336 u8 irq_mask = hwif->channel ? MRDMODE_INTR_CH1 :
338 MRDMODE_INTR_CH0; 337 MRDMODE_INTR_CH0;
339 u8 mrdmode = inb(hwif->dma_master + 0x01); 338 u8 mrdmode = inb(base + 1);
340 339
341 /* clear the interrupt bit */ 340 /* clear the interrupt bit */
342 outb((mrdmode & ~(MRDMODE_INTR_CH0 | MRDMODE_INTR_CH1)) | irq_mask, 341 outb((mrdmode & ~(MRDMODE_INTR_CH0 | MRDMODE_INTR_CH1)) | irq_mask,
343 hwif->dma_master + 0x01); 342 base + 1);
344 343
345 return err; 344 return err;
346} 345}
@@ -365,10 +364,11 @@ static int cmd64x_ide_dma_end (ide_drive_t *drive)
365static int cmd648_ide_dma_test_irq (ide_drive_t *drive) 364static int cmd648_ide_dma_test_irq (ide_drive_t *drive)
366{ 365{
367 ide_hwif_t *hwif = HWIF(drive); 366 ide_hwif_t *hwif = HWIF(drive);
367 unsigned long base = hwif->dma_base - (hwif->channel * 8);
368 u8 irq_mask = hwif->channel ? MRDMODE_INTR_CH1 : 368 u8 irq_mask = hwif->channel ? MRDMODE_INTR_CH1 :
369 MRDMODE_INTR_CH0; 369 MRDMODE_INTR_CH0;
370 u8 dma_stat = inb(hwif->dma_status); 370 u8 dma_stat = inb(hwif->dma_status);
371 u8 mrdmode = inb(hwif->dma_master + 0x01); 371 u8 mrdmode = inb(base + 1);
372 372
373#ifdef DEBUG 373#ifdef DEBUG
374 printk("%s: dma_stat: 0x%02x mrdmode: 0x%02x irq_mask: 0x%02x\n", 374 printk("%s: dma_stat: 0x%02x mrdmode: 0x%02x irq_mask: 0x%02x\n",
diff --git a/drivers/ide/pci/cs5520.c b/drivers/ide/pci/cs5520.c
index 0466462fd21b..d1a91bcb5b29 100644
--- a/drivers/ide/pci/cs5520.c
+++ b/drivers/ide/pci/cs5520.c
@@ -137,6 +137,7 @@ static void __devinit init_hwif_cs5520(ide_hwif_t *hwif)
137 IDE_HFLAG_CS5520 | \ 137 IDE_HFLAG_CS5520 | \
138 IDE_HFLAG_VDMA | \ 138 IDE_HFLAG_VDMA | \
139 IDE_HFLAG_NO_ATAPI_DMA | \ 139 IDE_HFLAG_NO_ATAPI_DMA | \
140 IDE_HFLAG_ABUSE_SET_DMA_MODE |\
140 IDE_HFLAG_BOOTABLE, \ 141 IDE_HFLAG_BOOTABLE, \
141 .pio_mask = ATA_PIO4, \ 142 .pio_mask = ATA_PIO4, \
142 } 143 }
diff --git a/drivers/ide/pci/cs5530.c b/drivers/ide/pci/cs5530.c
index 547690395eee..df5966b33460 100644
--- a/drivers/ide/pci/cs5530.c
+++ b/drivers/ide/pci/cs5530.c
@@ -116,8 +116,6 @@ static void cs5530_set_dma_mode(ide_drive_t *drive, const u8 mode)
116 case XFER_MW_DMA_0: timings = 0x00077771; break; 116 case XFER_MW_DMA_0: timings = 0x00077771; break;
117 case XFER_MW_DMA_1: timings = 0x00012121; break; 117 case XFER_MW_DMA_1: timings = 0x00012121; break;
118 case XFER_MW_DMA_2: timings = 0x00002020; break; 118 case XFER_MW_DMA_2: timings = 0x00002020; break;
119 default:
120 return;
121 } 119 }
122 basereg = CS5530_BASEREG(drive->hwif); 120 basereg = CS5530_BASEREG(drive->hwif);
123 reg = inl(basereg + 4); /* get drive0 config register */ 121 reg = inl(basereg + 4); /* get drive0 config register */
diff --git a/drivers/ide/pci/cs5535.c b/drivers/ide/pci/cs5535.c
index ddcbeba671e1..50b3d7791f55 100644
--- a/drivers/ide/pci/cs5535.c
+++ b/drivers/ide/pci/cs5535.c
@@ -190,7 +190,7 @@ static const struct ide_port_info cs5535_chipset __devinitdata = {
190 .name = "CS5535", 190 .name = "CS5535",
191 .init_hwif = init_hwif_cs5535, 191 .init_hwif = init_hwif_cs5535,
192 .host_flags = IDE_HFLAG_SINGLE | IDE_HFLAG_POST_SET_MODE | 192 .host_flags = IDE_HFLAG_SINGLE | IDE_HFLAG_POST_SET_MODE |
193 IDE_HFLAG_BOOTABLE, 193 IDE_HFLAG_ABUSE_SET_DMA_MODE | IDE_HFLAG_BOOTABLE,
194 .pio_mask = ATA_PIO4, 194 .pio_mask = ATA_PIO4,
195 .mwdma_mask = ATA_MWDMA2, 195 .mwdma_mask = ATA_MWDMA2,
196 .udma_mask = ATA_UDMA4, 196 .udma_mask = ATA_UDMA4,
diff --git a/drivers/ide/pci/hpt34x.c b/drivers/ide/pci/hpt34x.c
index ae6307fae4f9..dfba0d13fcd3 100644
--- a/drivers/ide/pci/hpt34x.c
+++ b/drivers/ide/pci/hpt34x.c
@@ -129,14 +129,18 @@ static void __devinit init_hwif_hpt34x(ide_hwif_t *hwif)
129 hwif->set_dma_mode = &hpt34x_set_mode; 129 hwif->set_dma_mode = &hpt34x_set_mode;
130} 130}
131 131
132#define IDE_HFLAGS_HPT34X \
133 (IDE_HFLAG_NO_ATAPI_DMA | \
134 IDE_HFLAG_ABUSE_SET_DMA_MODE | \
135 IDE_HFLAG_NO_AUTODMA)
136
132static const struct ide_port_info hpt34x_chipsets[] __devinitdata = { 137static const struct ide_port_info hpt34x_chipsets[] __devinitdata = {
133 { /* 0 */ 138 { /* 0 */
134 .name = "HPT343", 139 .name = "HPT343",
135 .init_chipset = init_chipset_hpt34x, 140 .init_chipset = init_chipset_hpt34x,
136 .init_hwif = init_hwif_hpt34x, 141 .init_hwif = init_hwif_hpt34x,
137 .extra = 16, 142 .extra = 16,
138 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | 143 .host_flags = IDE_HFLAGS_HPT34X,
139 IDE_HFLAG_NO_AUTODMA,
140 .pio_mask = ATA_PIO5, 144 .pio_mask = ATA_PIO5,
141 }, 145 },
142 { /* 1 */ 146 { /* 1 */
@@ -144,9 +148,7 @@ static const struct ide_port_info hpt34x_chipsets[] __devinitdata = {
144 .init_chipset = init_chipset_hpt34x, 148 .init_chipset = init_chipset_hpt34x,
145 .init_hwif = init_hwif_hpt34x, 149 .init_hwif = init_hwif_hpt34x,
146 .extra = 16, 150 .extra = 16,
147 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | 151 .host_flags = IDE_HFLAGS_HPT34X | IDE_HFLAG_OFF_BOARD,
148 IDE_HFLAG_NO_AUTODMA |
149 IDE_HFLAG_OFF_BOARD,
150 .pio_mask = ATA_PIO5, 152 .pio_mask = ATA_PIO5,
151#ifdef CONFIG_HPT34X_AUTODMA 153#ifdef CONFIG_HPT34X_AUTODMA
152 .swdma_mask = ATA_SWDMA2, 154 .swdma_mask = ATA_SWDMA2,
diff --git a/drivers/ide/pci/hpt366.c b/drivers/ide/pci/hpt366.c
index 9fce25bdec8a..3777fb8c8043 100644
--- a/drivers/ide/pci/hpt366.c
+++ b/drivers/ide/pci/hpt366.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * linux/drivers/ide/pci/hpt366.c Version 1.22 Dec 4, 2007 2 * linux/drivers/ide/pci/hpt366.c Version 1.30 Dec 12, 2007
3 * 3 *
4 * Copyright (C) 1999-2003 Andre Hedrick <andre@linux-ide.org> 4 * Copyright (C) 1999-2003 Andre Hedrick <andre@linux-ide.org>
5 * Portions Copyright (C) 2001 Sun Microsystems, Inc. 5 * Portions Copyright (C) 2001 Sun Microsystems, Inc.
@@ -88,7 +88,7 @@
88 * - rename all the register related variables consistently 88 * - rename all the register related variables consistently
89 * - move all the interrupt twiddling code from the speedproc handlers into 89 * - move all the interrupt twiddling code from the speedproc handlers into
90 * init_hwif_hpt366(), also grouping all the DMA related code together there 90 * init_hwif_hpt366(), also grouping all the DMA related code together there
91 * - merge two HPT37x speedproc handlers, fix the PIO timing register mask and 91 * - merge HPT36x/HPT37x speedproc handlers, fix PIO timing register mask and
92 * separate the UltraDMA and MWDMA masks there to avoid changing PIO timings 92 * separate the UltraDMA and MWDMA masks there to avoid changing PIO timings
93 * when setting an UltraDMA mode 93 * when setting an UltraDMA mode
94 * - fix hpt3xx_tune_drive() to set the PIO mode requested, not always select 94 * - fix hpt3xx_tune_drive() to set the PIO mode requested, not always select
@@ -458,6 +458,13 @@ enum ata_clock {
458 NUM_ATA_CLOCKS 458 NUM_ATA_CLOCKS
459}; 459};
460 460
461struct hpt_timings {
462 u32 pio_mask;
463 u32 dma_mask;
464 u32 ultra_mask;
465 u32 *clock_table[NUM_ATA_CLOCKS];
466};
467
461/* 468/*
462 * Hold all the HighPoint chip information in one place. 469 * Hold all the HighPoint chip information in one place.
463 */ 470 */
@@ -468,7 +475,8 @@ struct hpt_info {
468 u8 udma_mask; /* Allowed UltraDMA modes mask. */ 475 u8 udma_mask; /* Allowed UltraDMA modes mask. */
469 u8 dpll_clk; /* DPLL clock in MHz */ 476 u8 dpll_clk; /* DPLL clock in MHz */
470 u8 pci_clk; /* PCI clock in MHz */ 477 u8 pci_clk; /* PCI clock in MHz */
471 u32 **settings; /* Chipset settings table */ 478 struct hpt_timings *timings; /* Chipset timing data */
479 u8 clock; /* ATA clock selected */
472}; 480};
473 481
474/* Supported HighPoint chips */ 482/* Supported HighPoint chips */
@@ -486,20 +494,30 @@ enum {
486 HPT371N 494 HPT371N
487}; 495};
488 496
489static u32 *hpt36x_settings[NUM_ATA_CLOCKS] = { 497static struct hpt_timings hpt36x_timings = {
490 twenty_five_base_hpt36x, 498 .pio_mask = 0xc1f8ffff,
491 thirty_three_base_hpt36x, 499 .dma_mask = 0x303800ff,
492 forty_base_hpt36x, 500 .ultra_mask = 0x30070000,
493 NULL, 501 .clock_table = {
494 NULL 502 [ATA_CLOCK_25MHZ] = twenty_five_base_hpt36x,
503 [ATA_CLOCK_33MHZ] = thirty_three_base_hpt36x,
504 [ATA_CLOCK_40MHZ] = forty_base_hpt36x,
505 [ATA_CLOCK_50MHZ] = NULL,
506 [ATA_CLOCK_66MHZ] = NULL
507 }
495}; 508};
496 509
497static u32 *hpt37x_settings[NUM_ATA_CLOCKS] = { 510static struct hpt_timings hpt37x_timings = {
498 NULL, 511 .pio_mask = 0xcfc3ffff,
499 thirty_three_base_hpt37x, 512 .dma_mask = 0x31c001ff,
500 NULL, 513 .ultra_mask = 0x303c0000,
501 fifty_base_hpt37x, 514 .clock_table = {
502 sixty_six_base_hpt37x 515 [ATA_CLOCK_25MHZ] = NULL,
516 [ATA_CLOCK_33MHZ] = thirty_three_base_hpt37x,
517 [ATA_CLOCK_40MHZ] = NULL,
518 [ATA_CLOCK_50MHZ] = fifty_base_hpt37x,
519 [ATA_CLOCK_66MHZ] = sixty_six_base_hpt37x
520 }
503}; 521};
504 522
505static const struct hpt_info hpt36x __devinitdata = { 523static const struct hpt_info hpt36x __devinitdata = {
@@ -507,7 +525,7 @@ static const struct hpt_info hpt36x __devinitdata = {
507 .chip_type = HPT36x, 525 .chip_type = HPT36x,
508 .udma_mask = HPT366_ALLOW_ATA66_3 ? (HPT366_ALLOW_ATA66_4 ? ATA_UDMA4 : ATA_UDMA3) : ATA_UDMA2, 526 .udma_mask = HPT366_ALLOW_ATA66_3 ? (HPT366_ALLOW_ATA66_4 ? ATA_UDMA4 : ATA_UDMA3) : ATA_UDMA2,
509 .dpll_clk = 0, /* no DPLL */ 527 .dpll_clk = 0, /* no DPLL */
510 .settings = hpt36x_settings 528 .timings = &hpt36x_timings
511}; 529};
512 530
513static const struct hpt_info hpt370 __devinitdata = { 531static const struct hpt_info hpt370 __devinitdata = {
@@ -515,7 +533,7 @@ static const struct hpt_info hpt370 __devinitdata = {
515 .chip_type = HPT370, 533 .chip_type = HPT370,
516 .udma_mask = HPT370_ALLOW_ATA100_5 ? ATA_UDMA5 : ATA_UDMA4, 534 .udma_mask = HPT370_ALLOW_ATA100_5 ? ATA_UDMA5 : ATA_UDMA4,
517 .dpll_clk = 48, 535 .dpll_clk = 48,
518 .settings = hpt37x_settings 536 .timings = &hpt37x_timings
519}; 537};
520 538
521static const struct hpt_info hpt370a __devinitdata = { 539static const struct hpt_info hpt370a __devinitdata = {
@@ -523,7 +541,7 @@ static const struct hpt_info hpt370a __devinitdata = {
523 .chip_type = HPT370A, 541 .chip_type = HPT370A,
524 .udma_mask = HPT370_ALLOW_ATA100_5 ? ATA_UDMA5 : ATA_UDMA4, 542 .udma_mask = HPT370_ALLOW_ATA100_5 ? ATA_UDMA5 : ATA_UDMA4,
525 .dpll_clk = 48, 543 .dpll_clk = 48,
526 .settings = hpt37x_settings 544 .timings = &hpt37x_timings
527}; 545};
528 546
529static const struct hpt_info hpt374 __devinitdata = { 547static const struct hpt_info hpt374 __devinitdata = {
@@ -531,7 +549,7 @@ static const struct hpt_info hpt374 __devinitdata = {
531 .chip_type = HPT374, 549 .chip_type = HPT374,
532 .udma_mask = ATA_UDMA5, 550 .udma_mask = ATA_UDMA5,
533 .dpll_clk = 48, 551 .dpll_clk = 48,
534 .settings = hpt37x_settings 552 .timings = &hpt37x_timings
535}; 553};
536 554
537static const struct hpt_info hpt372 __devinitdata = { 555static const struct hpt_info hpt372 __devinitdata = {
@@ -539,7 +557,7 @@ static const struct hpt_info hpt372 __devinitdata = {
539 .chip_type = HPT372, 557 .chip_type = HPT372,
540 .udma_mask = HPT372_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5, 558 .udma_mask = HPT372_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
541 .dpll_clk = 55, 559 .dpll_clk = 55,
542 .settings = hpt37x_settings 560 .timings = &hpt37x_timings
543}; 561};
544 562
545static const struct hpt_info hpt372a __devinitdata = { 563static const struct hpt_info hpt372a __devinitdata = {
@@ -547,7 +565,7 @@ static const struct hpt_info hpt372a __devinitdata = {
547 .chip_type = HPT372A, 565 .chip_type = HPT372A,
548 .udma_mask = HPT372_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5, 566 .udma_mask = HPT372_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
549 .dpll_clk = 66, 567 .dpll_clk = 66,
550 .settings = hpt37x_settings 568 .timings = &hpt37x_timings
551}; 569};
552 570
553static const struct hpt_info hpt302 __devinitdata = { 571static const struct hpt_info hpt302 __devinitdata = {
@@ -555,7 +573,7 @@ static const struct hpt_info hpt302 __devinitdata = {
555 .chip_type = HPT302, 573 .chip_type = HPT302,
556 .udma_mask = HPT302_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5, 574 .udma_mask = HPT302_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
557 .dpll_clk = 66, 575 .dpll_clk = 66,
558 .settings = hpt37x_settings 576 .timings = &hpt37x_timings
559}; 577};
560 578
561static const struct hpt_info hpt371 __devinitdata = { 579static const struct hpt_info hpt371 __devinitdata = {
@@ -563,7 +581,7 @@ static const struct hpt_info hpt371 __devinitdata = {
563 .chip_type = HPT371, 581 .chip_type = HPT371,
564 .udma_mask = HPT371_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5, 582 .udma_mask = HPT371_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
565 .dpll_clk = 66, 583 .dpll_clk = 66,
566 .settings = hpt37x_settings 584 .timings = &hpt37x_timings
567}; 585};
568 586
569static const struct hpt_info hpt372n __devinitdata = { 587static const struct hpt_info hpt372n __devinitdata = {
@@ -571,7 +589,7 @@ static const struct hpt_info hpt372n __devinitdata = {
571 .chip_type = HPT372N, 589 .chip_type = HPT372N,
572 .udma_mask = HPT372_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5, 590 .udma_mask = HPT372_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
573 .dpll_clk = 77, 591 .dpll_clk = 77,
574 .settings = hpt37x_settings 592 .timings = &hpt37x_timings
575}; 593};
576 594
577static const struct hpt_info hpt302n __devinitdata = { 595static const struct hpt_info hpt302n __devinitdata = {
@@ -579,7 +597,7 @@ static const struct hpt_info hpt302n __devinitdata = {
579 .chip_type = HPT302N, 597 .chip_type = HPT302N,
580 .udma_mask = HPT302_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5, 598 .udma_mask = HPT302_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
581 .dpll_clk = 77, 599 .dpll_clk = 77,
582 .settings = hpt37x_settings 600 .timings = &hpt37x_timings
583}; 601};
584 602
585static const struct hpt_info hpt371n __devinitdata = { 603static const struct hpt_info hpt371n __devinitdata = {
@@ -587,7 +605,7 @@ static const struct hpt_info hpt371n __devinitdata = {
587 .chip_type = HPT371N, 605 .chip_type = HPT371N,
588 .udma_mask = HPT371_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5, 606 .udma_mask = HPT371_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
589 .dpll_clk = 77, 607 .dpll_clk = 77,
590 .settings = hpt37x_settings 608 .timings = &hpt37x_timings
591}; 609};
592 610
593static int check_in_drive_list(ide_drive_t *drive, const char **list) 611static int check_in_drive_list(ide_drive_t *drive, const char **list)
@@ -675,71 +693,33 @@ static u32 get_speed_setting(u8 speed, struct hpt_info *info)
675 for (i = 0; i < ARRAY_SIZE(xfer_speeds) - 1; i++) 693 for (i = 0; i < ARRAY_SIZE(xfer_speeds) - 1; i++)
676 if (xfer_speeds[i] == speed) 694 if (xfer_speeds[i] == speed)
677 break; 695 break;
678 /* 696
679 * NOTE: info->settings only points to the pointer 697 return info->timings->clock_table[info->clock][i];
680 * to the list of the actual register values
681 */
682 return (*info->settings)[i];
683} 698}
684 699
685static void hpt36x_set_mode(ide_drive_t *drive, const u8 speed) 700static void hpt3xx_set_mode(ide_drive_t *drive, const u8 speed)
686{ 701{
687 ide_hwif_t *hwif = HWIF(drive); 702 struct pci_dev *dev = HWIF(drive)->pci_dev;
688 struct pci_dev *dev = hwif->pci_dev;
689 struct hpt_info *info = pci_get_drvdata(dev); 703 struct hpt_info *info = pci_get_drvdata(dev);
690 u8 itr_addr = drive->dn ? 0x44 : 0x40; 704 struct hpt_timings *t = info->timings;
705 u8 itr_addr = 0x40 + (drive->dn * 4);
691 u32 old_itr = 0; 706 u32 old_itr = 0;
692 u32 itr_mask, new_itr; 707 u32 new_itr = get_speed_setting(speed, info);
693 708 u32 itr_mask = speed < XFER_MW_DMA_0 ? t->pio_mask :
694 itr_mask = speed < XFER_MW_DMA_0 ? 0x30070000 : 709 (speed < XFER_UDMA_0 ? t->dma_mask :
695 (speed < XFER_UDMA_0 ? 0xc0070000 : 0xc03800ff); 710 t->ultra_mask);
696
697 new_itr = get_speed_setting(speed, info);
698 711
712 pci_read_config_dword(dev, itr_addr, &old_itr);
713 new_itr = (old_itr & ~itr_mask) | (new_itr & itr_mask);
699 /* 714 /*
700 * Disable on-chip PIO FIFO/buffer (and PIO MST mode as well) 715 * Disable on-chip PIO FIFO/buffer (and PIO MST mode as well)
701 * to avoid problems handling I/O errors later 716 * to avoid problems handling I/O errors later
702 */ 717 */
703 pci_read_config_dword(dev, itr_addr, &old_itr);
704 new_itr = (new_itr & ~itr_mask) | (old_itr & itr_mask);
705 new_itr &= ~0xc0000000; 718 new_itr &= ~0xc0000000;
706 719
707 pci_write_config_dword(dev, itr_addr, new_itr); 720 pci_write_config_dword(dev, itr_addr, new_itr);
708} 721}
709 722
710static void hpt37x_set_mode(ide_drive_t *drive, const u8 speed)
711{
712 ide_hwif_t *hwif = HWIF(drive);
713 struct pci_dev *dev = hwif->pci_dev;
714 struct hpt_info *info = pci_get_drvdata(dev);
715 u8 itr_addr = 0x40 + (drive->dn * 4);
716 u32 old_itr = 0;
717 u32 itr_mask, new_itr;
718
719 itr_mask = speed < XFER_MW_DMA_0 ? 0x303c0000 :
720 (speed < XFER_UDMA_0 ? 0xc03c0000 : 0xc1c001ff);
721
722 new_itr = get_speed_setting(speed, info);
723
724 pci_read_config_dword(dev, itr_addr, &old_itr);
725 new_itr = (new_itr & ~itr_mask) | (old_itr & itr_mask);
726
727 if (speed < XFER_MW_DMA_0)
728 new_itr &= ~0x80000000; /* Disable on-chip PIO FIFO/buffer */
729 pci_write_config_dword(dev, itr_addr, new_itr);
730}
731
732static void hpt3xx_set_mode(ide_drive_t *drive, const u8 speed)
733{
734 ide_hwif_t *hwif = HWIF(drive);
735 struct hpt_info *info = pci_get_drvdata(hwif->pci_dev);
736
737 if (info->chip_type >= HPT370)
738 hpt37x_set_mode(drive, speed);
739 else /* hpt368: hpt_minimum_revision(dev, 2) */
740 hpt36x_set_mode(drive, speed);
741}
742
743static void hpt3xx_set_pio_mode(ide_drive_t *drive, const u8 pio) 723static void hpt3xx_set_pio_mode(ide_drive_t *drive, const u8 pio)
744{ 724{
745 hpt3xx_set_mode(drive, XFER_PIO_0 + pio); 725 hpt3xx_set_mode(drive, XFER_PIO_0 + pio);
@@ -756,15 +736,6 @@ static int hpt3xx_quirkproc(ide_drive_t *drive)
756 return 0; 736 return 0;
757} 737}
758 738
759static void hpt3xx_intrproc(ide_drive_t *drive)
760{
761 if (drive->quirk_list)
762 return;
763
764 /* drives in the quirk_list may not like intr setups/cleanups */
765 outb(drive->ctl | 2, IDE_CONTROL_REG);
766}
767
768static void hpt3xx_maskproc(ide_drive_t *drive, int mask) 739static void hpt3xx_maskproc(ide_drive_t *drive, int mask)
769{ 740{
770 ide_hwif_t *hwif = HWIF(drive); 741 ide_hwif_t *hwif = HWIF(drive);
@@ -914,32 +885,33 @@ static int hpt374_ide_dma_end(ide_drive_t *drive)
914 885
915static void hpt3xxn_set_clock(ide_hwif_t *hwif, u8 mode) 886static void hpt3xxn_set_clock(ide_hwif_t *hwif, u8 mode)
916{ 887{
917 u8 scr2 = inb(hwif->dma_master + 0x7b); 888 unsigned long base = hwif->extra_base;
889 u8 scr2 = inb(base + 0x6b);
918 890
919 if ((scr2 & 0x7f) == mode) 891 if ((scr2 & 0x7f) == mode)
920 return; 892 return;
921 893
922 /* Tristate the bus */ 894 /* Tristate the bus */
923 outb(0x80, hwif->dma_master + 0x73); 895 outb(0x80, base + 0x63);
924 outb(0x80, hwif->dma_master + 0x77); 896 outb(0x80, base + 0x67);
925 897
926 /* Switch clock and reset channels */ 898 /* Switch clock and reset channels */
927 outb(mode, hwif->dma_master + 0x7b); 899 outb(mode, base + 0x6b);
928 outb(0xc0, hwif->dma_master + 0x79); 900 outb(0xc0, base + 0x69);
929 901
930 /* 902 /*
931 * Reset the state machines. 903 * Reset the state machines.
932 * NOTE: avoid accidentally enabling the disabled channels. 904 * NOTE: avoid accidentally enabling the disabled channels.
933 */ 905 */
934 outb(inb(hwif->dma_master + 0x70) | 0x32, hwif->dma_master + 0x70); 906 outb(inb(base + 0x60) | 0x32, base + 0x60);
935 outb(inb(hwif->dma_master + 0x74) | 0x32, hwif->dma_master + 0x74); 907 outb(inb(base + 0x64) | 0x32, base + 0x64);
936 908
937 /* Complete reset */ 909 /* Complete reset */
938 outb(0x00, hwif->dma_master + 0x79); 910 outb(0x00, base + 0x69);
939 911
940 /* Reconnect channels to bus */ 912 /* Reconnect channels to bus */
941 outb(0x00, hwif->dma_master + 0x73); 913 outb(0x00, base + 0x63);
942 outb(0x00, hwif->dma_master + 0x77); 914 outb(0x00, base + 0x67);
943} 915}
944 916
945/** 917/**
@@ -1210,7 +1182,7 @@ static unsigned int __devinit init_chipset_hpt366(struct pci_dev *dev, const cha
1210 * We also don't like using the DPLL because this causes glitches 1182 * We also don't like using the DPLL because this causes glitches
1211 * on PRST-/SRST- when the state engine gets reset... 1183 * on PRST-/SRST- when the state engine gets reset...
1212 */ 1184 */
1213 if (chip_type >= HPT374 || info->settings[clock] == NULL) { 1185 if (chip_type >= HPT374 || info->timings->clock_table[clock] == NULL) {
1214 u16 f_low, delta = pci_clk < 50 ? 2 : 4; 1186 u16 f_low, delta = pci_clk < 50 ? 2 : 4;
1215 int adjust; 1187 int adjust;
1216 1188
@@ -1226,7 +1198,7 @@ static unsigned int __devinit init_chipset_hpt366(struct pci_dev *dev, const cha
1226 clock = ATA_CLOCK_50MHZ; 1198 clock = ATA_CLOCK_50MHZ;
1227 } 1199 }
1228 1200
1229 if (info->settings[clock] == NULL) { 1201 if (info->timings->clock_table[clock] == NULL) {
1230 printk(KERN_ERR "%s: unknown bus timing!\n", name); 1202 printk(KERN_ERR "%s: unknown bus timing!\n", name);
1231 kfree(info); 1203 kfree(info);
1232 return -EIO; 1204 return -EIO;
@@ -1267,15 +1239,10 @@ static unsigned int __devinit init_chipset_hpt366(struct pci_dev *dev, const cha
1267 printk("%s: using %d MHz PCI clock\n", name, pci_clk); 1239 printk("%s: using %d MHz PCI clock\n", name, pci_clk);
1268 } 1240 }
1269 1241
1270 /*
1271 * Advance the table pointer to a slot which points to the list
1272 * of the register values settings matching the clock being used.
1273 */
1274 info->settings += clock;
1275
1276 /* Store the clock frequencies. */ 1242 /* Store the clock frequencies. */
1277 info->dpll_clk = dpll_clk; 1243 info->dpll_clk = dpll_clk;
1278 info->pci_clk = pci_clk; 1244 info->pci_clk = pci_clk;
1245 info->clock = clock;
1279 1246
1280 /* Point to this chip's own instance of the hpt_info structure. */ 1247 /* Point to this chip's own instance of the hpt_info structure. */
1281 pci_set_drvdata(dev, info); 1248 pci_set_drvdata(dev, info);
@@ -1320,8 +1287,8 @@ static void __devinit init_hwif_hpt366(ide_hwif_t *hwif)
1320 1287
1321 hwif->set_pio_mode = &hpt3xx_set_pio_mode; 1288 hwif->set_pio_mode = &hpt3xx_set_pio_mode;
1322 hwif->set_dma_mode = &hpt3xx_set_mode; 1289 hwif->set_dma_mode = &hpt3xx_set_mode;
1290
1323 hwif->quirkproc = &hpt3xx_quirkproc; 1291 hwif->quirkproc = &hpt3xx_quirkproc;
1324 hwif->intrproc = &hpt3xx_intrproc;
1325 hwif->maskproc = &hpt3xx_maskproc; 1292 hwif->maskproc = &hpt3xx_maskproc;
1326 hwif->busproc = &hpt3xx_busproc; 1293 hwif->busproc = &hpt3xx_busproc;
1327 1294
@@ -1494,6 +1461,11 @@ static int __devinit hpt36x_init(struct pci_dev *dev, struct pci_dev *dev2)
1494 return 0; 1461 return 0;
1495} 1462}
1496 1463
1464#define IDE_HFLAGS_HPT3XX \
1465 (IDE_HFLAG_NO_ATAPI_DMA | \
1466 IDE_HFLAG_ABUSE_SET_DMA_MODE | \
1467 IDE_HFLAG_OFF_BOARD)
1468
1497static const struct ide_port_info hpt366_chipsets[] __devinitdata = { 1469static const struct ide_port_info hpt366_chipsets[] __devinitdata = {
1498 { /* 0 */ 1470 { /* 0 */
1499 .name = "HPT36x", 1471 .name = "HPT36x",
@@ -1508,9 +1480,7 @@ static const struct ide_port_info hpt366_chipsets[] __devinitdata = {
1508 */ 1480 */
1509 .enablebits = {{0x50,0x10,0x10}, {0x54,0x04,0x04}}, 1481 .enablebits = {{0x50,0x10,0x10}, {0x54,0x04,0x04}},
1510 .extra = 240, 1482 .extra = 240,
1511 .host_flags = IDE_HFLAG_SINGLE | 1483 .host_flags = IDE_HFLAGS_HPT3XX | IDE_HFLAG_SINGLE,
1512 IDE_HFLAG_NO_ATAPI_DMA |
1513 IDE_HFLAG_OFF_BOARD,
1514 .pio_mask = ATA_PIO4, 1484 .pio_mask = ATA_PIO4,
1515 .mwdma_mask = ATA_MWDMA2, 1485 .mwdma_mask = ATA_MWDMA2,
1516 },{ /* 1 */ 1486 },{ /* 1 */
@@ -1520,7 +1490,7 @@ static const struct ide_port_info hpt366_chipsets[] __devinitdata = {
1520 .init_dma = init_dma_hpt366, 1490 .init_dma = init_dma_hpt366,
1521 .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}}, 1491 .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}},
1522 .extra = 240, 1492 .extra = 240,
1523 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | IDE_HFLAG_OFF_BOARD, 1493 .host_flags = IDE_HFLAGS_HPT3XX,
1524 .pio_mask = ATA_PIO4, 1494 .pio_mask = ATA_PIO4,
1525 .mwdma_mask = ATA_MWDMA2, 1495 .mwdma_mask = ATA_MWDMA2,
1526 },{ /* 2 */ 1496 },{ /* 2 */
@@ -1530,7 +1500,7 @@ static const struct ide_port_info hpt366_chipsets[] __devinitdata = {
1530 .init_dma = init_dma_hpt366, 1500 .init_dma = init_dma_hpt366,
1531 .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}}, 1501 .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}},
1532 .extra = 240, 1502 .extra = 240,
1533 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | IDE_HFLAG_OFF_BOARD, 1503 .host_flags = IDE_HFLAGS_HPT3XX,
1534 .pio_mask = ATA_PIO4, 1504 .pio_mask = ATA_PIO4,
1535 .mwdma_mask = ATA_MWDMA2, 1505 .mwdma_mask = ATA_MWDMA2,
1536 },{ /* 3 */ 1506 },{ /* 3 */
@@ -1540,7 +1510,7 @@ static const struct ide_port_info hpt366_chipsets[] __devinitdata = {
1540 .init_dma = init_dma_hpt366, 1510 .init_dma = init_dma_hpt366,
1541 .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}}, 1511 .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}},
1542 .extra = 240, 1512 .extra = 240,
1543 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | IDE_HFLAG_OFF_BOARD, 1513 .host_flags = IDE_HFLAGS_HPT3XX,
1544 .pio_mask = ATA_PIO4, 1514 .pio_mask = ATA_PIO4,
1545 .mwdma_mask = ATA_MWDMA2, 1515 .mwdma_mask = ATA_MWDMA2,
1546 },{ /* 4 */ 1516 },{ /* 4 */
@@ -1551,7 +1521,7 @@ static const struct ide_port_info hpt366_chipsets[] __devinitdata = {
1551 .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}}, 1521 .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}},
1552 .udma_mask = ATA_UDMA5, 1522 .udma_mask = ATA_UDMA5,
1553 .extra = 240, 1523 .extra = 240,
1554 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | IDE_HFLAG_OFF_BOARD, 1524 .host_flags = IDE_HFLAGS_HPT3XX,
1555 .pio_mask = ATA_PIO4, 1525 .pio_mask = ATA_PIO4,
1556 .mwdma_mask = ATA_MWDMA2, 1526 .mwdma_mask = ATA_MWDMA2,
1557 },{ /* 5 */ 1527 },{ /* 5 */
@@ -1561,7 +1531,7 @@ static const struct ide_port_info hpt366_chipsets[] __devinitdata = {
1561 .init_dma = init_dma_hpt366, 1531 .init_dma = init_dma_hpt366,
1562 .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}}, 1532 .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}},
1563 .extra = 240, 1533 .extra = 240,
1564 .host_flags = IDE_HFLAG_NO_ATAPI_DMA | IDE_HFLAG_OFF_BOARD, 1534 .host_flags = IDE_HFLAGS_HPT3XX,
1565 .pio_mask = ATA_PIO4, 1535 .pio_mask = ATA_PIO4,
1566 .mwdma_mask = ATA_MWDMA2, 1536 .mwdma_mask = ATA_MWDMA2,
1567 } 1537 }
diff --git a/drivers/ide/pci/it8213.c b/drivers/ide/pci/it8213.c
index 90b52ed37bfc..2a0f45c4f4c4 100644
--- a/drivers/ide/pci/it8213.c
+++ b/drivers/ide/pci/it8213.c
@@ -101,24 +101,11 @@ static void it8213_set_dma_mode(ide_drive_t *drive, const u8 speed)
101 pci_read_config_byte(dev, 0x54, &reg54); 101 pci_read_config_byte(dev, 0x54, &reg54);
102 pci_read_config_byte(dev, 0x55, &reg55); 102 pci_read_config_byte(dev, 0x55, &reg55);
103 103
104 switch(speed) {
105 case XFER_UDMA_6:
106 case XFER_UDMA_4:
107 case XFER_UDMA_2: u_speed = 2 << (drive->dn * 4); break;
108 case XFER_UDMA_5:
109 case XFER_UDMA_3:
110 case XFER_UDMA_1: u_speed = 1 << (drive->dn * 4); break;
111 case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break;
112 break;
113 case XFER_MW_DMA_2:
114 case XFER_MW_DMA_1:
115 case XFER_SW_DMA_2:
116 break;
117 default:
118 return;
119 }
120
121 if (speed >= XFER_UDMA_0) { 104 if (speed >= XFER_UDMA_0) {
105 u8 udma = speed - XFER_UDMA_0;
106
107 u_speed = min_t(u8, 2 - (udma & 1), udma) << (drive->dn * 4);
108
122 if (!(reg48 & u_flag)) 109 if (!(reg48 & u_flag))
123 pci_write_config_byte(dev, 0x48, reg48 | u_flag); 110 pci_write_config_byte(dev, 0x48, reg48 | u_flag);
124 if (speed >= XFER_UDMA_5) { 111 if (speed >= XFER_UDMA_5) {
diff --git a/drivers/ide/pci/pdc202xx_new.c b/drivers/ide/pci/pdc202xx_new.c
index 2b4f44e45a1a..ef4a99b99d1f 100644
--- a/drivers/ide/pci/pdc202xx_new.c
+++ b/drivers/ide/pci/pdc202xx_new.c
@@ -146,7 +146,7 @@ static struct udma_timing {
146 { 0x1a, 0x01, 0xcb }, /* UDMA mode 6 */ 146 { 0x1a, 0x01, 0xcb }, /* UDMA mode 6 */
147}; 147};
148 148
149static void pdcnew_set_mode(ide_drive_t *drive, const u8 speed) 149static void pdcnew_set_dma_mode(ide_drive_t *drive, const u8 speed)
150{ 150{
151 ide_hwif_t *hwif = HWIF(drive); 151 ide_hwif_t *hwif = HWIF(drive);
152 u8 adj = (drive->dn & 1) ? 0x08 : 0x00; 152 u8 adj = (drive->dn & 1) ? 0x08 : 0x00;
@@ -162,45 +162,18 @@ static void pdcnew_set_mode(ide_drive_t *drive, const u8 speed)
162 if (max_dma_rate(hwif->pci_dev) == 4) { 162 if (max_dma_rate(hwif->pci_dev) == 4) {
163 u8 mode = speed & 0x07; 163 u8 mode = speed & 0x07;
164 164
165 switch (speed) { 165 if (speed >= XFER_UDMA_0) {
166 case XFER_UDMA_6: 166 set_indexed_reg(hwif, 0x10 + adj,
167 case XFER_UDMA_5: 167 udma_timings[mode].reg10);
168 case XFER_UDMA_4: 168 set_indexed_reg(hwif, 0x11 + adj,
169 case XFER_UDMA_3: 169 udma_timings[mode].reg11);
170 case XFER_UDMA_2: 170 set_indexed_reg(hwif, 0x12 + adj,
171 case XFER_UDMA_1: 171 udma_timings[mode].reg12);
172 case XFER_UDMA_0: 172 } else {
173 set_indexed_reg(hwif, 0x10 + adj, 173 set_indexed_reg(hwif, 0x0e + adj,
174 udma_timings[mode].reg10); 174 mwdma_timings[mode].reg0e);
175 set_indexed_reg(hwif, 0x11 + adj, 175 set_indexed_reg(hwif, 0x0f + adj,
176 udma_timings[mode].reg11); 176 mwdma_timings[mode].reg0f);
177 set_indexed_reg(hwif, 0x12 + adj,
178 udma_timings[mode].reg12);
179 break;
180
181 case XFER_MW_DMA_2:
182 case XFER_MW_DMA_1:
183 case XFER_MW_DMA_0:
184 set_indexed_reg(hwif, 0x0e + adj,
185 mwdma_timings[mode].reg0e);
186 set_indexed_reg(hwif, 0x0f + adj,
187 mwdma_timings[mode].reg0f);
188 break;
189 case XFER_PIO_4:
190 case XFER_PIO_3:
191 case XFER_PIO_2:
192 case XFER_PIO_1:
193 case XFER_PIO_0:
194 set_indexed_reg(hwif, 0x0c + adj,
195 pio_timings[mode].reg0c);
196 set_indexed_reg(hwif, 0x0d + adj,
197 pio_timings[mode].reg0d);
198 set_indexed_reg(hwif, 0x13 + adj,
199 pio_timings[mode].reg13);
200 break;
201 default:
202 printk(KERN_ERR "pdc202xx_new: "
203 "Unknown speed %d ignored\n", speed);
204 } 177 }
205 } else if (speed == XFER_UDMA_2) { 178 } else if (speed == XFER_UDMA_2) {
206 /* Set tHOLD bit to 0 if using UDMA mode 2 */ 179 /* Set tHOLD bit to 0 if using UDMA mode 2 */
@@ -212,7 +185,14 @@ static void pdcnew_set_mode(ide_drive_t *drive, const u8 speed)
212 185
213static void pdcnew_set_pio_mode(ide_drive_t *drive, const u8 pio) 186static void pdcnew_set_pio_mode(ide_drive_t *drive, const u8 pio)
214{ 187{
215 pdcnew_set_mode(drive, XFER_PIO_0 + pio); 188 ide_hwif_t *hwif = drive->hwif;
189 u8 adj = (drive->dn & 1) ? 0x08 : 0x00;
190
191 if (max_dma_rate(hwif->pci_dev) == 4) {
192 set_indexed_reg(hwif, 0x0c + adj, pio_timings[pio].reg0c);
193 set_indexed_reg(hwif, 0x0d + adj, pio_timings[pio].reg0d);
194 set_indexed_reg(hwif, 0x13 + adj, pio_timings[pio].reg13);
195 }
216} 196}
217 197
218static u8 pdcnew_cable_detect(ide_hwif_t *hwif) 198static u8 pdcnew_cable_detect(ide_hwif_t *hwif)
@@ -466,7 +446,7 @@ static unsigned int __devinit init_chipset_pdcnew(struct pci_dev *dev, const cha
466static void __devinit init_hwif_pdc202new(ide_hwif_t *hwif) 446static void __devinit init_hwif_pdc202new(ide_hwif_t *hwif)
467{ 447{
468 hwif->set_pio_mode = &pdcnew_set_pio_mode; 448 hwif->set_pio_mode = &pdcnew_set_pio_mode;
469 hwif->set_dma_mode = &pdcnew_set_mode; 449 hwif->set_dma_mode = &pdcnew_set_dma_mode;
470 450
471 hwif->quirkproc = &pdcnew_quirkproc; 451 hwif->quirkproc = &pdcnew_quirkproc;
472 hwif->resetproc = &pdcnew_reset; 452 hwif->resetproc = &pdcnew_reset;
diff --git a/drivers/ide/pci/pdc202xx_old.c b/drivers/ide/pci/pdc202xx_old.c
index e09742e2ba59..67b2781e2213 100644
--- a/drivers/ide/pci/pdc202xx_old.c
+++ b/drivers/ide/pci/pdc202xx_old.c
@@ -162,7 +162,7 @@ static u8 pdc202xx_old_cable_detect (ide_hwif_t *hwif)
162 */ 162 */
163static void pdc_old_enable_66MHz_clock(ide_hwif_t *hwif) 163static void pdc_old_enable_66MHz_clock(ide_hwif_t *hwif)
164{ 164{
165 unsigned long clock_reg = hwif->dma_master + 0x11; 165 unsigned long clock_reg = hwif->extra_base + 0x01;
166 u8 clock = inb(clock_reg); 166 u8 clock = inb(clock_reg);
167 167
168 outb(clock | (hwif->channel ? 0x08 : 0x02), clock_reg); 168 outb(clock | (hwif->channel ? 0x08 : 0x02), clock_reg);
@@ -170,7 +170,7 @@ static void pdc_old_enable_66MHz_clock(ide_hwif_t *hwif)
170 170
171static void pdc_old_disable_66MHz_clock(ide_hwif_t *hwif) 171static void pdc_old_disable_66MHz_clock(ide_hwif_t *hwif)
172{ 172{
173 unsigned long clock_reg = hwif->dma_master + 0x11; 173 unsigned long clock_reg = hwif->extra_base + 0x01;
174 u8 clock = inb(clock_reg); 174 u8 clock = inb(clock_reg);
175 175
176 outb(clock & ~(hwif->channel ? 0x08 : 0x02), clock_reg); 176 outb(clock & ~(hwif->channel ? 0x08 : 0x02), clock_reg);
@@ -193,7 +193,7 @@ static void pdc202xx_old_ide_dma_start(ide_drive_t *drive)
193 if (drive->media != ide_disk || drive->addressing == 1) { 193 if (drive->media != ide_disk || drive->addressing == 1) {
194 struct request *rq = HWGROUP(drive)->rq; 194 struct request *rq = HWGROUP(drive)->rq;
195 ide_hwif_t *hwif = HWIF(drive); 195 ide_hwif_t *hwif = HWIF(drive);
196 unsigned long high_16 = hwif->dma_master; 196 unsigned long high_16 = hwif->extra_base - 16;
197 unsigned long atapi_reg = high_16 + (hwif->channel ? 0x24 : 0x20); 197 unsigned long atapi_reg = high_16 + (hwif->channel ? 0x24 : 0x20);
198 u32 word_count = 0; 198 u32 word_count = 0;
199 u8 clock = inb(high_16 + 0x11); 199 u8 clock = inb(high_16 + 0x11);
@@ -212,7 +212,7 @@ static int pdc202xx_old_ide_dma_end(ide_drive_t *drive)
212{ 212{
213 if (drive->media != ide_disk || drive->addressing == 1) { 213 if (drive->media != ide_disk || drive->addressing == 1) {
214 ide_hwif_t *hwif = HWIF(drive); 214 ide_hwif_t *hwif = HWIF(drive);
215 unsigned long high_16 = hwif->dma_master; 215 unsigned long high_16 = hwif->extra_base - 16;
216 unsigned long atapi_reg = high_16 + (hwif->channel ? 0x24 : 0x20); 216 unsigned long atapi_reg = high_16 + (hwif->channel ? 0x24 : 0x20);
217 u8 clock = 0; 217 u8 clock = 0;
218 218
@@ -228,7 +228,7 @@ static int pdc202xx_old_ide_dma_end(ide_drive_t *drive)
228static int pdc202xx_old_ide_dma_test_irq(ide_drive_t *drive) 228static int pdc202xx_old_ide_dma_test_irq(ide_drive_t *drive)
229{ 229{
230 ide_hwif_t *hwif = HWIF(drive); 230 ide_hwif_t *hwif = HWIF(drive);
231 unsigned long high_16 = hwif->dma_master; 231 unsigned long high_16 = hwif->extra_base - 16;
232 u8 dma_stat = inb(hwif->dma_status); 232 u8 dma_stat = inb(hwif->dma_status);
233 u8 sc1d = inb(high_16 + 0x001d); 233 u8 sc1d = inb(high_16 + 0x001d);
234 234
@@ -271,7 +271,7 @@ static void pdc202xx_dma_timeout(ide_drive_t *drive)
271 271
272static void pdc202xx_reset_host (ide_hwif_t *hwif) 272static void pdc202xx_reset_host (ide_hwif_t *hwif)
273{ 273{
274 unsigned long high_16 = hwif->dma_master; 274 unsigned long high_16 = hwif->extra_base - 16;
275 u8 udma_speed_flag = inb(high_16 | 0x001f); 275 u8 udma_speed_flag = inb(high_16 | 0x001f);
276 276
277 outb(udma_speed_flag | 0x10, high_16 | 0x001f); 277 outb(udma_speed_flag | 0x10, high_16 | 0x001f);
@@ -375,6 +375,11 @@ static void __devinit pdc202ata4_fixup_irq(struct pci_dev *dev,
375 } 375 }
376} 376}
377 377
378#define IDE_HFLAGS_PDC202XX \
379 (IDE_HFLAG_ERROR_STOPS_FIFO | \
380 IDE_HFLAG_ABUSE_SET_DMA_MODE | \
381 IDE_HFLAG_OFF_BOARD)
382
378#define DECLARE_PDC2026X_DEV(name_str, udma, extra_flags) \ 383#define DECLARE_PDC2026X_DEV(name_str, udma, extra_flags) \
379 { \ 384 { \
380 .name = name_str, \ 385 .name = name_str, \
@@ -382,9 +387,7 @@ static void __devinit pdc202ata4_fixup_irq(struct pci_dev *dev,
382 .init_hwif = init_hwif_pdc202xx, \ 387 .init_hwif = init_hwif_pdc202xx, \
383 .init_dma = init_dma_pdc202xx, \ 388 .init_dma = init_dma_pdc202xx, \
384 .extra = 48, \ 389 .extra = 48, \
385 .host_flags = IDE_HFLAG_ERROR_STOPS_FIFO | \ 390 .host_flags = IDE_HFLAGS_PDC202XX | extra_flags, \
386 extra_flags | \
387 IDE_HFLAG_OFF_BOARD, \
388 .pio_mask = ATA_PIO4, \ 391 .pio_mask = ATA_PIO4, \
389 .mwdma_mask = ATA_MWDMA2, \ 392 .mwdma_mask = ATA_MWDMA2, \
390 .udma_mask = udma, \ 393 .udma_mask = udma, \
@@ -397,8 +400,7 @@ static const struct ide_port_info pdc202xx_chipsets[] __devinitdata = {
397 .init_hwif = init_hwif_pdc202xx, 400 .init_hwif = init_hwif_pdc202xx,
398 .init_dma = init_dma_pdc202xx, 401 .init_dma = init_dma_pdc202xx,
399 .extra = 16, 402 .extra = 16,
400 .host_flags = IDE_HFLAG_ERROR_STOPS_FIFO | 403 .host_flags = IDE_HFLAGS_PDC202XX,
401 IDE_HFLAG_OFF_BOARD,
402 .pio_mask = ATA_PIO4, 404 .pio_mask = ATA_PIO4,
403 .mwdma_mask = ATA_MWDMA2, 405 .mwdma_mask = ATA_MWDMA2,
404 .udma_mask = ATA_UDMA2, 406 .udma_mask = ATA_UDMA2,
diff --git a/drivers/ide/pci/piix.c b/drivers/ide/pci/piix.c
index 27781d294cea..bd6d3f77d30c 100644
--- a/drivers/ide/pci/piix.c
+++ b/drivers/ide/pci/piix.c
@@ -203,20 +203,11 @@ static void piix_set_dma_mode(ide_drive_t *drive, const u8 speed)
203 pci_read_config_byte(dev, 0x54, &reg54); 203 pci_read_config_byte(dev, 0x54, &reg54);
204 pci_read_config_byte(dev, 0x55, &reg55); 204 pci_read_config_byte(dev, 0x55, &reg55);
205 205
206 switch(speed) {
207 case XFER_UDMA_4:
208 case XFER_UDMA_2: u_speed = 2 << (drive->dn * 4); break;
209 case XFER_UDMA_5:
210 case XFER_UDMA_3:
211 case XFER_UDMA_1: u_speed = 1 << (drive->dn * 4); break;
212 case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break;
213 case XFER_MW_DMA_2:
214 case XFER_MW_DMA_1:
215 case XFER_SW_DMA_2: break;
216 default: return;
217 }
218
219 if (speed >= XFER_UDMA_0) { 206 if (speed >= XFER_UDMA_0) {
207 u8 udma = speed - XFER_UDMA_0;
208
209 u_speed = min_t(u8, 2 - (udma & 1), udma) << (drive->dn * 4);
210
220 if (!(reg48 & u_flag)) 211 if (!(reg48 & u_flag))
221 pci_write_config_byte(dev, 0x48, reg48 | u_flag); 212 pci_write_config_byte(dev, 0x48, reg48 | u_flag);
222 if (speed == XFER_UDMA_5) { 213 if (speed == XFER_UDMA_5) {
diff --git a/drivers/ide/pci/sc1200.c b/drivers/ide/pci/sc1200.c
index 707d5ff66b03..fef20bd4aa78 100644
--- a/drivers/ide/pci/sc1200.c
+++ b/drivers/ide/pci/sc1200.c
@@ -135,59 +135,29 @@ static void sc1200_set_dma_mode(ide_drive_t *drive, const u8 mode)
135 unsigned short pci_clock; 135 unsigned short pci_clock;
136 unsigned int basereg = hwif->channel ? 0x50 : 0x40; 136 unsigned int basereg = hwif->channel ? 0x50 : 0x40;
137 137
138 static const u32 udma_timing[3][3] = {
139 { 0x00921250, 0x00911140, 0x00911030 },
140 { 0x00932470, 0x00922260, 0x00922140 },
141 { 0x009436a1, 0x00933481, 0x00923261 },
142 };
143
144 static const u32 mwdma_timing[3][3] = {
145 { 0x00077771, 0x00012121, 0x00002020 },
146 { 0x000bbbb2, 0x00024241, 0x00013131 },
147 { 0x000ffff3, 0x00035352, 0x00015151 },
148 };
149
138 pci_clock = sc1200_get_pci_clock(); 150 pci_clock = sc1200_get_pci_clock();
139 151
140 /* 152 /*
141 * Note that each DMA mode has several timings associated with it. 153 * Note that each DMA mode has several timings associated with it.
142 * The correct timing depends on the fast PCI clock freq. 154 * The correct timing depends on the fast PCI clock freq.
143 */ 155 */
144 timings = 0; 156
145 switch (mode) { 157 if (mode >= XFER_UDMA_0)
146 case XFER_UDMA_0: 158 timings = udma_timing[pci_clock][mode - XFER_UDMA_0];
147 switch (pci_clock) { 159 else
148 case PCI_CLK_33: timings = 0x00921250; break; 160 timings = mwdma_timing[pci_clock][mode - XFER_MW_DMA_0];
149 case PCI_CLK_48: timings = 0x00932470; break;
150 case PCI_CLK_66: timings = 0x009436a1; break;
151 }
152 break;
153 case XFER_UDMA_1:
154 switch (pci_clock) {
155 case PCI_CLK_33: timings = 0x00911140; break;
156 case PCI_CLK_48: timings = 0x00922260; break;
157 case PCI_CLK_66: timings = 0x00933481; break;
158 }
159 break;
160 case XFER_UDMA_2:
161 switch (pci_clock) {
162 case PCI_CLK_33: timings = 0x00911030; break;
163 case PCI_CLK_48: timings = 0x00922140; break;
164 case PCI_CLK_66: timings = 0x00923261; break;
165 }
166 break;
167 case XFER_MW_DMA_0:
168 switch (pci_clock) {
169 case PCI_CLK_33: timings = 0x00077771; break;
170 case PCI_CLK_48: timings = 0x000bbbb2; break;
171 case PCI_CLK_66: timings = 0x000ffff3; break;
172 }
173 break;
174 case XFER_MW_DMA_1:
175 switch (pci_clock) {
176 case PCI_CLK_33: timings = 0x00012121; break;
177 case PCI_CLK_48: timings = 0x00024241; break;
178 case PCI_CLK_66: timings = 0x00035352; break;
179 }
180 break;
181 case XFER_MW_DMA_2:
182 switch (pci_clock) {
183 case PCI_CLK_33: timings = 0x00002020; break;
184 case PCI_CLK_48: timings = 0x00013131; break;
185 case PCI_CLK_66: timings = 0x00015151; break;
186 }
187 break;
188 default:
189 return;
190 }
191 161
192 if (unit == 0) { /* are we configuring drive0? */ 162 if (unit == 0) { /* are we configuring drive0? */
193 pci_read_config_dword(hwif->pci_dev, basereg+4, &reg); 163 pci_read_config_dword(hwif->pci_dev, basereg+4, &reg);
@@ -260,66 +230,39 @@ static void sc1200_set_pio_mode(ide_drive_t *drive, const u8 pio)
260} 230}
261 231
262#ifdef CONFIG_PM 232#ifdef CONFIG_PM
263static ide_hwif_t *lookup_pci_dev (ide_hwif_t *prev, struct pci_dev *dev) 233struct sc1200_saved_state {
264{ 234 u32 regs[8];
265 int h; 235};
266
267 for (h = 0; h < MAX_HWIFS; h++) {
268 ide_hwif_t *hwif = &ide_hwifs[h];
269 if (prev) {
270 if (hwif == prev)
271 prev = NULL; // found previous, now look for next match
272 } else {
273 if (hwif && hwif->pci_dev == dev)
274 return hwif; // found next match
275 }
276 }
277 return NULL; // not found
278}
279
280typedef struct sc1200_saved_state_s {
281 __u32 regs[4];
282} sc1200_saved_state_t;
283
284 236
285static int sc1200_suspend (struct pci_dev *dev, pm_message_t state) 237static int sc1200_suspend (struct pci_dev *dev, pm_message_t state)
286{ 238{
287 ide_hwif_t *hwif = NULL;
288
289 printk("SC1200: suspend(%u)\n", state.event); 239 printk("SC1200: suspend(%u)\n", state.event);
290 240
241 /*
242 * we only save state when going from full power to less
243 */
291 if (state.event == PM_EVENT_ON) { 244 if (state.event == PM_EVENT_ON) {
292 // we only save state when going from full power to less 245 struct sc1200_saved_state *ss;
293 246 unsigned int r;
294 // 247
295 // Loop over all interfaces that are part of this PCI device: 248 /*
296 // 249 * allocate a permanent save area, if not already allocated
297 while ((hwif = lookup_pci_dev(hwif, dev)) != NULL) { 250 */
298 sc1200_saved_state_t *ss; 251 ss = (struct sc1200_saved_state *)pci_get_drvdata(dev);
299 unsigned int basereg, r; 252 if (ss == NULL) {
300 // 253 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
301 // allocate a permanent save area, if not already allocated 254 if (ss == NULL)
302 // 255 return -ENOMEM;
303 ss = (sc1200_saved_state_t *)hwif->config_data; 256 pci_set_drvdata(dev, ss);
304 if (ss == NULL) {
305 ss = kmalloc(sizeof(sc1200_saved_state_t), GFP_KERNEL);
306 if (ss == NULL)
307 return -ENOMEM;
308 hwif->config_data = (unsigned long)ss;
309 }
310 ss = (sc1200_saved_state_t *)hwif->config_data;
311 //
312 // Save timing registers: this may be unnecessary if
313 // BIOS also does it
314 //
315 basereg = hwif->channel ? 0x50 : 0x40;
316 for (r = 0; r < 4; ++r) {
317 pci_read_config_dword (hwif->pci_dev, basereg + (r<<2), &ss->regs[r]);
318 }
319 } 257 }
320 }
321 258
322 /* You don't need to iterate over disks -- sysfs should have done that for you already */ 259 /*
260 * save timing registers
261 * (this may be unnecessary if BIOS also does it)
262 */
263 for (r = 0; r < 8; r++)
264 pci_read_config_dword(dev, 0x40 + r * 4, &ss->regs[r]);
265 }
323 266
324 pci_disable_device(dev); 267 pci_disable_device(dev);
325 pci_set_power_state(dev, pci_choose_state(dev, state)); 268 pci_set_power_state(dev, pci_choose_state(dev, state));
@@ -328,30 +271,25 @@ static int sc1200_suspend (struct pci_dev *dev, pm_message_t state)
328 271
329static int sc1200_resume (struct pci_dev *dev) 272static int sc1200_resume (struct pci_dev *dev)
330{ 273{
331 ide_hwif_t *hwif = NULL; 274 struct sc1200_saved_state *ss;
332 int i; 275 unsigned int r;
276 int i;
333 277
334 i = pci_enable_device(dev); 278 i = pci_enable_device(dev);
335 if (i) 279 if (i)
336 return i; 280 return i;
337 281
338 // 282 ss = (struct sc1200_saved_state *)pci_get_drvdata(dev);
339 // loop over all interfaces that are part of this pci device: 283
340 // 284 /*
341 while ((hwif = lookup_pci_dev(hwif, dev)) != NULL) { 285 * restore timing registers
342 unsigned int basereg, r; 286 * (this may be unnecessary if BIOS also does it)
343 sc1200_saved_state_t *ss = (sc1200_saved_state_t *)hwif->config_data; 287 */
344 288 if (ss) {
345 // 289 for (r = 0; r < 8; r++)
346 // Restore timing registers: this may be unnecessary if BIOS also does it 290 pci_write_config_dword(dev, 0x40 + r * 4, ss->regs[r]);
347 //
348 basereg = hwif->channel ? 0x50 : 0x40;
349 if (ss != NULL) {
350 for (r = 0; r < 4; ++r) {
351 pci_write_config_dword(hwif->pci_dev, basereg + (r<<2), ss->regs[r]);
352 }
353 }
354 } 291 }
292
355 return 0; 293 return 0;
356} 294}
357#endif 295#endif
diff --git a/drivers/ide/pci/scc_pata.c b/drivers/ide/pci/scc_pata.c
index ebb7132b9b84..24a85bbcd2a6 100644
--- a/drivers/ide/pci/scc_pata.c
+++ b/drivers/ide/pci/scc_pata.c
@@ -254,19 +254,7 @@ static void scc_set_dma_mode(ide_drive_t *drive, const u8 speed)
254 offset = 0; /* 100MHz */ 254 offset = 0; /* 100MHz */
255 } 255 }
256 256
257 switch (speed) { 257 idx = speed - XFER_UDMA_0;
258 case XFER_UDMA_6:
259 case XFER_UDMA_5:
260 case XFER_UDMA_4:
261 case XFER_UDMA_3:
262 case XFER_UDMA_2:
263 case XFER_UDMA_1:
264 case XFER_UDMA_0:
265 idx = speed - XFER_UDMA_0;
266 break;
267 default:
268 return;
269 }
270 258
271 jcactsel = JCACTSELtbl[offset][idx]; 259 jcactsel = JCACTSELtbl[offset][idx];
272 if (is_slave) { 260 if (is_slave) {
diff --git a/drivers/ide/pci/serverworks.c b/drivers/ide/pci/serverworks.c
index a7280311357b..e9bd269547bb 100644
--- a/drivers/ide/pci/serverworks.c
+++ b/drivers/ide/pci/serverworks.c
@@ -366,12 +366,17 @@ static void __devinit init_hwif_svwks (ide_hwif_t *hwif)
366 } 366 }
367} 367}
368 368
369#define IDE_HFLAGS_SVWKS \
370 (IDE_HFLAG_LEGACY_IRQS | \
371 IDE_HFLAG_ABUSE_SET_DMA_MODE | \
372 IDE_HFLAG_BOOTABLE)
373
369static const struct ide_port_info serverworks_chipsets[] __devinitdata = { 374static const struct ide_port_info serverworks_chipsets[] __devinitdata = {
370 { /* 0 */ 375 { /* 0 */
371 .name = "SvrWks OSB4", 376 .name = "SvrWks OSB4",
372 .init_chipset = init_chipset_svwks, 377 .init_chipset = init_chipset_svwks,
373 .init_hwif = init_hwif_svwks, 378 .init_hwif = init_hwif_svwks,
374 .host_flags = IDE_HFLAG_LEGACY_IRQS | IDE_HFLAG_BOOTABLE, 379 .host_flags = IDE_HFLAGS_SVWKS,
375 .pio_mask = ATA_PIO4, 380 .pio_mask = ATA_PIO4,
376 .mwdma_mask = ATA_MWDMA2, 381 .mwdma_mask = ATA_MWDMA2,
377 .udma_mask = 0x00, /* UDMA is problematic on OSB4 */ 382 .udma_mask = 0x00, /* UDMA is problematic on OSB4 */
@@ -379,7 +384,7 @@ static const struct ide_port_info serverworks_chipsets[] __devinitdata = {
379 .name = "SvrWks CSB5", 384 .name = "SvrWks CSB5",
380 .init_chipset = init_chipset_svwks, 385 .init_chipset = init_chipset_svwks,
381 .init_hwif = init_hwif_svwks, 386 .init_hwif = init_hwif_svwks,
382 .host_flags = IDE_HFLAG_LEGACY_IRQS | IDE_HFLAG_BOOTABLE, 387 .host_flags = IDE_HFLAGS_SVWKS,
383 .pio_mask = ATA_PIO4, 388 .pio_mask = ATA_PIO4,
384 .mwdma_mask = ATA_MWDMA2, 389 .mwdma_mask = ATA_MWDMA2,
385 .udma_mask = ATA_UDMA5, 390 .udma_mask = ATA_UDMA5,
@@ -387,7 +392,7 @@ static const struct ide_port_info serverworks_chipsets[] __devinitdata = {
387 .name = "SvrWks CSB6", 392 .name = "SvrWks CSB6",
388 .init_chipset = init_chipset_svwks, 393 .init_chipset = init_chipset_svwks,
389 .init_hwif = init_hwif_svwks, 394 .init_hwif = init_hwif_svwks,
390 .host_flags = IDE_HFLAG_LEGACY_IRQS | IDE_HFLAG_BOOTABLE, 395 .host_flags = IDE_HFLAGS_SVWKS,
391 .pio_mask = ATA_PIO4, 396 .pio_mask = ATA_PIO4,
392 .mwdma_mask = ATA_MWDMA2, 397 .mwdma_mask = ATA_MWDMA2,
393 .udma_mask = ATA_UDMA5, 398 .udma_mask = ATA_UDMA5,
@@ -395,8 +400,7 @@ static const struct ide_port_info serverworks_chipsets[] __devinitdata = {
395 .name = "SvrWks CSB6", 400 .name = "SvrWks CSB6",
396 .init_chipset = init_chipset_svwks, 401 .init_chipset = init_chipset_svwks,
397 .init_hwif = init_hwif_svwks, 402 .init_hwif = init_hwif_svwks,
398 .host_flags = IDE_HFLAG_LEGACY_IRQS | IDE_HFLAG_SINGLE | 403 .host_flags = IDE_HFLAGS_SVWKS | IDE_HFLAG_SINGLE,
399 IDE_HFLAG_BOOTABLE,
400 .pio_mask = ATA_PIO4, 404 .pio_mask = ATA_PIO4,
401 .mwdma_mask = ATA_MWDMA2, 405 .mwdma_mask = ATA_MWDMA2,
402 .udma_mask = ATA_UDMA5, 406 .udma_mask = ATA_UDMA5,
@@ -404,8 +408,7 @@ static const struct ide_port_info serverworks_chipsets[] __devinitdata = {
404 .name = "SvrWks HT1000", 408 .name = "SvrWks HT1000",
405 .init_chipset = init_chipset_svwks, 409 .init_chipset = init_chipset_svwks,
406 .init_hwif = init_hwif_svwks, 410 .init_hwif = init_hwif_svwks,
407 .host_flags = IDE_HFLAG_LEGACY_IRQS | IDE_HFLAG_SINGLE | 411 .host_flags = IDE_HFLAGS_SVWKS | IDE_HFLAG_SINGLE,
408 IDE_HFLAG_BOOTABLE,
409 .pio_mask = ATA_PIO4, 412 .pio_mask = ATA_PIO4,
410 .mwdma_mask = ATA_MWDMA2, 413 .mwdma_mask = ATA_MWDMA2,
411 .udma_mask = ATA_UDMA5, 414 .udma_mask = ATA_UDMA5,
diff --git a/drivers/ide/pci/sgiioc4.c b/drivers/ide/pci/sgiioc4.c
index de820aa58cd0..7e9dade5648d 100644
--- a/drivers/ide/pci/sgiioc4.c
+++ b/drivers/ide/pci/sgiioc4.c
@@ -582,7 +582,6 @@ ide_init_sgiioc4(ide_hwif_t * hwif)
582 hwif->pre_reset = NULL; /* No HBA specific pre_set needed */ 582 hwif->pre_reset = NULL; /* No HBA specific pre_set needed */
583 hwif->resetproc = &sgiioc4_resetproc;/* Reset DMA engine, 583 hwif->resetproc = &sgiioc4_resetproc;/* Reset DMA engine,
584 clear interrupts */ 584 clear interrupts */
585 hwif->intrproc = NULL; /* Enable or Disable interrupt from drive */
586 hwif->maskproc = &sgiioc4_maskproc; /* Mask on/off NIEN register */ 585 hwif->maskproc = &sgiioc4_maskproc; /* Mask on/off NIEN register */
587 hwif->quirkproc = NULL; 586 hwif->quirkproc = NULL;
588 hwif->busproc = NULL; 587 hwif->busproc = NULL;
diff --git a/drivers/ide/pci/siimage.c b/drivers/ide/pci/siimage.c
index 5709c252543b..7b45eaf5afd9 100644
--- a/drivers/ide/pci/siimage.c
+++ b/drivers/ide/pci/siimage.c
@@ -278,27 +278,14 @@ static void sil_set_dma_mode(ide_drive_t *drive, const u8 speed)
278 278
279 scsc = is_sata(hwif) ? 1 : scsc; 279 scsc = is_sata(hwif) ? 1 : scsc;
280 280
281 switch(speed) { 281 if (speed >= XFER_UDMA_0) {
282 case XFER_MW_DMA_2: 282 multi = dma[2];
283 case XFER_MW_DMA_1: 283 ultra |= (scsc ? ultra6[speed - XFER_UDMA_0] :
284 case XFER_MW_DMA_0: 284 ultra5[speed - XFER_UDMA_0]);
285 multi = dma[speed - XFER_MW_DMA_0]; 285 mode |= (unit ? 0x30 : 0x03);
286 mode |= ((unit) ? 0x20 : 0x02); 286 } else {
287 break; 287 multi = dma[speed - XFER_MW_DMA_0];
288 case XFER_UDMA_6: 288 mode |= (unit ? 0x20 : 0x02);
289 case XFER_UDMA_5:
290 case XFER_UDMA_4:
291 case XFER_UDMA_3:
292 case XFER_UDMA_2:
293 case XFER_UDMA_1:
294 case XFER_UDMA_0:
295 multi = dma[2];
296 ultra |= ((scsc) ? (ultra6[speed - XFER_UDMA_0]) :
297 (ultra5[speed - XFER_UDMA_0]));
298 mode |= ((unit) ? 0x30 : 0x03);
299 break;
300 default:
301 return;
302 } 289 }
303 290
304 if (hwif->mmio) { 291 if (hwif->mmio) {
diff --git a/drivers/ide/pci/sis5513.c b/drivers/ide/pci/sis5513.c
index d90b42917775..85d36996e6af 100644
--- a/drivers/ide/pci/sis5513.c
+++ b/drivers/ide/pci/sis5513.c
@@ -305,59 +305,56 @@ static void sis_set_pio_mode(ide_drive_t *drive, const u8 pio)
305 sis_program_timings(drive, XFER_PIO_0 + pio); 305 sis_program_timings(drive, XFER_PIO_0 + pio);
306} 306}
307 307
308static void sis_set_dma_mode(ide_drive_t *drive, const u8 speed) 308static void sis_ata133_program_udma_timings(ide_drive_t *drive, const u8 mode)
309{ 309{
310 ide_hwif_t *hwif = HWIF(drive); 310 struct pci_dev *dev = drive->hwif->pci_dev;
311 struct pci_dev *dev = hwif->pci_dev; 311 u32 regdw = 0;
312 u8 drive_pci = sis_ata133_get_base(drive), clk, idx;
312 313
313 /* Config chip for mode */ 314 pci_read_config_dword(dev, drive_pci, &regdw);
314 switch(speed) { 315
315 case XFER_UDMA_6: 316 regdw |= 0x04;
316 case XFER_UDMA_5: 317 regdw &= 0xfffff00f;
317 case XFER_UDMA_4: 318 /* check if ATA133 enable */
318 case XFER_UDMA_3: 319 clk = (regdw & 0x08) ? ATA_133 : ATA_100;
319 case XFER_UDMA_2: 320 idx = mode - XFER_UDMA_0;
320 case XFER_UDMA_1: 321 regdw |= cycle_time_value[clk][idx] << 4;
321 case XFER_UDMA_0: 322 regdw |= cvs_time_value[clk][idx] << 8;
322 if (chipset_family >= ATA_133) { 323
323 u32 regdw = 0; 324 pci_write_config_dword(dev, drive_pci, regdw);
324 u8 drive_pci = sis_ata133_get_base(drive); 325}
325 326
326 pci_read_config_dword(dev, drive_pci, &regdw); 327static void sis_ata33_program_udma_timings(ide_drive_t *drive, const u8 mode)
327 regdw |= 0x04; 328{
328 regdw &= 0xfffff00f; 329 struct pci_dev *dev = drive->hwif->pci_dev;
329 /* check if ATA133 enable */ 330 u8 drive_pci = 0x40 + drive->dn * 2, reg = 0, i = chipset_family;
330 if (regdw & 0x08) { 331
331 regdw |= (unsigned long)cycle_time_value[ATA_133][speed-XFER_UDMA_0] << 4; 332 pci_read_config_byte(dev, drive_pci + 1, &reg);
332 regdw |= (unsigned long)cvs_time_value[ATA_133][speed-XFER_UDMA_0] << 8; 333
333 } else { 334 /* force the UDMA bit on if we want to use UDMA */
334 regdw |= (unsigned long)cycle_time_value[ATA_100][speed-XFER_UDMA_0] << 4; 335 reg |= 0x80;
335 regdw |= (unsigned long)cvs_time_value[ATA_100][speed-XFER_UDMA_0] << 8; 336 /* clean reg cycle time bits */
336 } 337 reg &= ~((0xff >> (8 - cycle_time_range[i])) << cycle_time_offset[i]);
337 pci_write_config_dword(dev, (unsigned long)drive_pci, regdw); 338 /* set reg cycle time bits */
338 } else { 339 reg |= cycle_time_value[i][mode - XFER_UDMA_0] << cycle_time_offset[i];
339 u8 drive_pci = 0x40 + drive->dn * 2, reg = 0; 340
340 341 pci_write_config_byte(dev, drive_pci + 1, reg);
341 pci_read_config_byte(dev, drive_pci+1, &reg); 342}
342 /* Force the UDMA bit on if we want to use UDMA */ 343
343 reg |= 0x80; 344static void sis_program_udma_timings(ide_drive_t *drive, const u8 mode)
344 /* clean reg cycle time bits */ 345{
345 reg &= ~((0xFF >> (8 - cycle_time_range[chipset_family])) 346 if (chipset_family >= ATA_133) /* ATA_133 */
346 << cycle_time_offset[chipset_family]); 347 sis_ata133_program_udma_timings(drive, mode);
347 /* set reg cycle time bits */ 348 else /* ATA_33/66/100a/100/133a */
348 reg |= cycle_time_value[chipset_family][speed-XFER_UDMA_0] 349 sis_ata33_program_udma_timings(drive, mode);
349 << cycle_time_offset[chipset_family]; 350}
350 pci_write_config_byte(dev, drive_pci+1, reg); 351
351 } 352static void sis_set_dma_mode(ide_drive_t *drive, const u8 speed)
352 break; 353{
353 case XFER_MW_DMA_2: 354 if (speed >= XFER_UDMA_0)
354 case XFER_MW_DMA_1: 355 sis_program_udma_timings(drive, speed);
355 case XFER_MW_DMA_0: 356 else
356 sis_program_timings(drive, speed); 357 sis_program_timings(drive, speed);
357 break;
358 default:
359 break;
360 }
361} 358}
362 359
363static u8 sis5513_ata133_udma_filter(ide_drive_t *drive) 360static u8 sis5513_ata133_udma_filter(ide_drive_t *drive)
diff --git a/drivers/ide/pci/sl82c105.c b/drivers/ide/pci/sl82c105.c
index 147d783f7529..069f104fdcea 100644
--- a/drivers/ide/pci/sl82c105.c
+++ b/drivers/ide/pci/sl82c105.c
@@ -115,32 +115,24 @@ static void sl82c105_set_dma_mode(ide_drive_t *drive, const u8 speed)
115 DBG(("sl82c105_tune_chipset(drive:%s, speed:%s)\n", 115 DBG(("sl82c105_tune_chipset(drive:%s, speed:%s)\n",
116 drive->name, ide_xfer_verbose(speed))); 116 drive->name, ide_xfer_verbose(speed)));
117 117
118 switch (speed) { 118 drv_ctrl = mwdma_timings[speed - XFER_MW_DMA_0];
119 case XFER_MW_DMA_2:
120 case XFER_MW_DMA_1:
121 case XFER_MW_DMA_0:
122 drv_ctrl = mwdma_timings[speed - XFER_MW_DMA_0];
123 119
124 /* 120 /*
125 * Store the DMA timings so that we can actually program 121 * Store the DMA timings so that we can actually program
126 * them when DMA will be turned on... 122 * them when DMA will be turned on...
127 */ 123 */
128 drive->drive_data &= 0x0000ffff; 124 drive->drive_data &= 0x0000ffff;
129 drive->drive_data |= (unsigned long)drv_ctrl << 16; 125 drive->drive_data |= (unsigned long)drv_ctrl << 16;
130 126
131 /* 127 /*
132 * If we are already using DMA, we just reprogram 128 * If we are already using DMA, we just reprogram
133 * the drive control register. 129 * the drive control register.
134 */ 130 */
135 if (drive->using_dma) { 131 if (drive->using_dma) {
136 struct pci_dev *dev = HWIF(drive)->pci_dev; 132 struct pci_dev *dev = HWIF(drive)->pci_dev;
137 int reg = 0x44 + drive->dn * 4; 133 int reg = 0x44 + drive->dn * 4;
138 134
139 pci_write_config_word(dev, reg, drv_ctrl); 135 pci_write_config_word(dev, reg, drv_ctrl);
140 }
141 break;
142 default:
143 return;
144 } 136 }
145} 137}
146 138
diff --git a/drivers/ide/pci/slc90e66.c b/drivers/ide/pci/slc90e66.c
index eb4445b229ed..dbbb46819a2d 100644
--- a/drivers/ide/pci/slc90e66.c
+++ b/drivers/ide/pci/slc90e66.c
@@ -91,19 +91,9 @@ static void slc90e66_set_dma_mode(ide_drive_t *drive, const u8 speed)
91 pci_read_config_word(dev, 0x48, &reg48); 91 pci_read_config_word(dev, 0x48, &reg48);
92 pci_read_config_word(dev, 0x4a, &reg4a); 92 pci_read_config_word(dev, 0x4a, &reg4a);
93 93
94 switch(speed) {
95 case XFER_UDMA_4: u_speed = 4 << (drive->dn * 4); break;
96 case XFER_UDMA_3: u_speed = 3 << (drive->dn * 4); break;
97 case XFER_UDMA_2: u_speed = 2 << (drive->dn * 4); break;
98 case XFER_UDMA_1: u_speed = 1 << (drive->dn * 4); break;
99 case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break;
100 case XFER_MW_DMA_2:
101 case XFER_MW_DMA_1:
102 case XFER_SW_DMA_2: break;
103 default: return;
104 }
105
106 if (speed >= XFER_UDMA_0) { 94 if (speed >= XFER_UDMA_0) {
95 u_speed = (speed - XFER_UDMA_0) << (drive->dn * 4);
96
107 if (!(reg48 & u_flag)) 97 if (!(reg48 & u_flag))
108 pci_write_config_word(dev, 0x48, reg48|u_flag); 98 pci_write_config_word(dev, 0x48, reg48|u_flag);
109 /* FIXME: (reg4a & a_speed) ? */ 99 /* FIXME: (reg4a & a_speed) ? */
diff --git a/drivers/ide/pci/tc86c001.c b/drivers/ide/pci/tc86c001.c
index a66ebd14664e..e1faf6c2fe16 100644
--- a/drivers/ide/pci/tc86c001.c
+++ b/drivers/ide/pci/tc86c001.c
@@ -222,7 +222,8 @@ static const struct ide_port_info tc86c001_chipset __devinitdata = {
222 .name = "TC86C001", 222 .name = "TC86C001",
223 .init_chipset = init_chipset_tc86c001, 223 .init_chipset = init_chipset_tc86c001,
224 .init_hwif = init_hwif_tc86c001, 224 .init_hwif = init_hwif_tc86c001,
225 .host_flags = IDE_HFLAG_SINGLE | IDE_HFLAG_OFF_BOARD, 225 .host_flags = IDE_HFLAG_SINGLE | IDE_HFLAG_OFF_BOARD |
226 IDE_HFLAG_ABUSE_SET_DMA_MODE,
226 .pio_mask = ATA_PIO4, 227 .pio_mask = ATA_PIO4,
227 .mwdma_mask = ATA_MWDMA2, 228 .mwdma_mask = ATA_MWDMA2,
228 .udma_mask = ATA_UDMA4, 229 .udma_mask = ATA_UDMA4,
diff --git a/drivers/ide/pci/triflex.c b/drivers/ide/pci/triflex.c
index a227c41d23a3..ae52a96a1cf9 100644
--- a/drivers/ide/pci/triflex.c
+++ b/drivers/ide/pci/triflex.c
@@ -81,8 +81,6 @@ static void triflex_set_mode(ide_drive_t *drive, const u8 speed)
81 case XFER_PIO_0: 81 case XFER_PIO_0:
82 timing = 0x0808; 82 timing = 0x0808;
83 break; 83 break;
84 default:
85 return;
86 } 84 }
87 85
88 triflex_timings &= ~(0xFFFF << (16 * unit)); 86 triflex_timings &= ~(0xFFFF << (16 * unit));
diff --git a/drivers/ide/pci/via82cxxx.c b/drivers/ide/pci/via82cxxx.c
index a0d3c16b68ec..4b32c90f4896 100644
--- a/drivers/ide/pci/via82cxxx.c
+++ b/drivers/ide/pci/via82cxxx.c
@@ -439,6 +439,7 @@ static const struct ide_port_info via82cxxx_chipset __devinitdata = {
439 .enablebits = { { 0x40, 0x02, 0x02 }, { 0x40, 0x01, 0x01 } }, 439 .enablebits = { { 0x40, 0x02, 0x02 }, { 0x40, 0x01, 0x01 } },
440 .host_flags = IDE_HFLAG_PIO_NO_BLACKLIST | 440 .host_flags = IDE_HFLAG_PIO_NO_BLACKLIST |
441 IDE_HFLAG_PIO_NO_DOWNGRADE | 441 IDE_HFLAG_PIO_NO_DOWNGRADE |
442 IDE_HFLAG_ABUSE_SET_DMA_MODE |
442 IDE_HFLAG_POST_SET_MODE | 443 IDE_HFLAG_POST_SET_MODE |
443 IDE_HFLAG_IO_32BIT | 444 IDE_HFLAG_IO_32BIT |
444 IDE_HFLAG_BOOTABLE, 445 IDE_HFLAG_BOOTABLE,
diff --git a/drivers/ide/ppc/pmac.c b/drivers/ide/ppc/pmac.c
index 7f7a59885777..3dce80092fff 100644
--- a/drivers/ide/ppc/pmac.c
+++ b/drivers/ide/ppc/pmac.c
@@ -438,13 +438,8 @@ pmac_ide_init_hwif_ports(hw_regs_t *hw,
438 if (data_port == pmac_ide[ix].regbase) 438 if (data_port == pmac_ide[ix].regbase)
439 break; 439 break;
440 440
441 if (ix >= MAX_HWIFS) { 441 if (ix >= MAX_HWIFS)
442 /* Probably a PCI interface... */ 442 return; /* not an IDE PMAC interface */
443 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; ++i)
444 hw->io_ports[i] = data_port + i - IDE_DATA_OFFSET;
445 hw->io_ports[IDE_CONTROL_OFFSET] = ctrl_port;
446 return;
447 }
448 443
449 for (i = 0; i < 8; ++i) 444 for (i = 0; i < 8; ++i)
450 hw->io_ports[i] = data_port + i * 0x10; 445 hw->io_ports[i] = data_port + i * 0x10;
@@ -833,38 +828,20 @@ static void pmac_ide_set_dma_mode(ide_drive_t *drive, const u8 speed)
833 tl[0] = *timings; 828 tl[0] = *timings;
834 tl[1] = *timings2; 829 tl[1] = *timings2;
835 830
836 switch(speed) {
837#ifdef CONFIG_BLK_DEV_IDEDMA_PMAC 831#ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
838 case XFER_UDMA_6: 832 if (speed >= XFER_UDMA_0) {
839 case XFER_UDMA_5: 833 if (pmif->kind == controller_kl_ata4)
840 case XFER_UDMA_4: 834 ret = set_timings_udma_ata4(&tl[0], speed);
841 case XFER_UDMA_3: 835 else if (pmif->kind == controller_un_ata6
842 case XFER_UDMA_2: 836 || pmif->kind == controller_k2_ata6)
843 case XFER_UDMA_1: 837 ret = set_timings_udma_ata6(&tl[0], &tl[1], speed);
844 case XFER_UDMA_0: 838 else if (pmif->kind == controller_sh_ata6)
845 if (pmif->kind == controller_kl_ata4) 839 ret = set_timings_udma_shasta(&tl[0], &tl[1], speed);
846 ret = set_timings_udma_ata4(&tl[0], speed); 840 else
847 else if (pmif->kind == controller_un_ata6 841 ret = -1;
848 || pmif->kind == controller_k2_ata6) 842 } else
849 ret = set_timings_udma_ata6(&tl[0], &tl[1], speed); 843 set_timings_mdma(drive, pmif->kind, &tl[0], &tl[1], speed);
850 else if (pmif->kind == controller_sh_ata6)
851 ret = set_timings_udma_shasta(&tl[0], &tl[1], speed);
852 else
853 ret = 1;
854 break;
855 case XFER_MW_DMA_2:
856 case XFER_MW_DMA_1:
857 case XFER_MW_DMA_0:
858 set_timings_mdma(drive, pmif->kind, &tl[0], &tl[1], speed);
859 break;
860 case XFER_SW_DMA_2:
861 case XFER_SW_DMA_1:
862 case XFER_SW_DMA_0:
863 return;
864#endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */ 844#endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
865 default:
866 ret = 1;
867 }
868 if (ret) 845 if (ret)
869 return; 846 return;
870 847
diff --git a/drivers/infiniband/core/cm.c b/drivers/infiniband/core/cm.c
index 2e39236d189f..c0150147d347 100644
--- a/drivers/infiniband/core/cm.c
+++ b/drivers/infiniband/core/cm.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2004-2006 Intel Corporation. All rights reserved. 2 * Copyright (c) 2004-2007 Intel Corporation. All rights reserved.
3 * Copyright (c) 2004 Topspin Corporation. All rights reserved. 3 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
4 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved. 4 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
@@ -37,12 +37,14 @@
37 37
38#include <linux/completion.h> 38#include <linux/completion.h>
39#include <linux/dma-mapping.h> 39#include <linux/dma-mapping.h>
40#include <linux/device.h>
40#include <linux/err.h> 41#include <linux/err.h>
41#include <linux/idr.h> 42#include <linux/idr.h>
42#include <linux/interrupt.h> 43#include <linux/interrupt.h>
43#include <linux/random.h> 44#include <linux/random.h>
44#include <linux/rbtree.h> 45#include <linux/rbtree.h>
45#include <linux/spinlock.h> 46#include <linux/spinlock.h>
47#include <linux/sysfs.h>
46#include <linux/workqueue.h> 48#include <linux/workqueue.h>
47 49
48#include <rdma/ib_cache.h> 50#include <rdma/ib_cache.h>
@@ -78,17 +80,94 @@ static struct ib_cm {
78 struct workqueue_struct *wq; 80 struct workqueue_struct *wq;
79} cm; 81} cm;
80 82
83/* Counter indexes ordered by attribute ID */
84enum {
85 CM_REQ_COUNTER,
86 CM_MRA_COUNTER,
87 CM_REJ_COUNTER,
88 CM_REP_COUNTER,
89 CM_RTU_COUNTER,
90 CM_DREQ_COUNTER,
91 CM_DREP_COUNTER,
92 CM_SIDR_REQ_COUNTER,
93 CM_SIDR_REP_COUNTER,
94 CM_LAP_COUNTER,
95 CM_APR_COUNTER,
96 CM_ATTR_COUNT,
97 CM_ATTR_ID_OFFSET = 0x0010,
98};
99
100enum {
101 CM_XMIT,
102 CM_XMIT_RETRIES,
103 CM_RECV,
104 CM_RECV_DUPLICATES,
105 CM_COUNTER_GROUPS
106};
107
108static char const counter_group_names[CM_COUNTER_GROUPS]
109 [sizeof("cm_rx_duplicates")] = {
110 "cm_tx_msgs", "cm_tx_retries",
111 "cm_rx_msgs", "cm_rx_duplicates"
112};
113
114struct cm_counter_group {
115 struct kobject obj;
116 atomic_long_t counter[CM_ATTR_COUNT];
117};
118
119struct cm_counter_attribute {
120 struct attribute attr;
121 int index;
122};
123
124#define CM_COUNTER_ATTR(_name, _index) \
125struct cm_counter_attribute cm_##_name##_counter_attr = { \
126 .attr = { .name = __stringify(_name), .mode = 0444, .owner = THIS_MODULE }, \
127 .index = _index \
128}
129
130static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
131static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
132static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
133static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
134static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
135static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
136static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
137static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
138static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
139static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
140static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
141
142static struct attribute *cm_counter_default_attrs[] = {
143 &cm_req_counter_attr.attr,
144 &cm_mra_counter_attr.attr,
145 &cm_rej_counter_attr.attr,
146 &cm_rep_counter_attr.attr,
147 &cm_rtu_counter_attr.attr,
148 &cm_dreq_counter_attr.attr,
149 &cm_drep_counter_attr.attr,
150 &cm_sidr_req_counter_attr.attr,
151 &cm_sidr_rep_counter_attr.attr,
152 &cm_lap_counter_attr.attr,
153 &cm_apr_counter_attr.attr,
154 NULL
155};
156
81struct cm_port { 157struct cm_port {
82 struct cm_device *cm_dev; 158 struct cm_device *cm_dev;
83 struct ib_mad_agent *mad_agent; 159 struct ib_mad_agent *mad_agent;
160 struct kobject port_obj;
84 u8 port_num; 161 u8 port_num;
162 struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
85}; 163};
86 164
87struct cm_device { 165struct cm_device {
88 struct list_head list; 166 struct list_head list;
89 struct ib_device *device; 167 struct ib_device *device;
168 struct kobject dev_obj;
90 u8 ack_delay; 169 u8 ack_delay;
91 struct cm_port port[0]; 170 struct cm_port *port[0];
92}; 171};
93 172
94struct cm_av { 173struct cm_av {
@@ -278,7 +357,7 @@ static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
278 list_for_each_entry(cm_dev, &cm.device_list, list) { 357 list_for_each_entry(cm_dev, &cm.device_list, list) {
279 if (!ib_find_cached_gid(cm_dev->device, &path->sgid, 358 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
280 &p, NULL)) { 359 &p, NULL)) {
281 port = &cm_dev->port[p-1]; 360 port = cm_dev->port[p-1];
282 break; 361 break;
283 } 362 }
284 } 363 }
@@ -1270,6 +1349,9 @@ static void cm_dup_req_handler(struct cm_work *work,
1270 struct ib_mad_send_buf *msg = NULL; 1349 struct ib_mad_send_buf *msg = NULL;
1271 int ret; 1350 int ret;
1272 1351
1352 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1353 counter[CM_REQ_COUNTER]);
1354
1273 /* Quick state check to discard duplicate REQs. */ 1355 /* Quick state check to discard duplicate REQs. */
1274 if (cm_id_priv->id.state == IB_CM_REQ_RCVD) 1356 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1275 return; 1357 return;
@@ -1616,6 +1698,8 @@ static void cm_dup_rep_handler(struct cm_work *work)
1616 if (!cm_id_priv) 1698 if (!cm_id_priv)
1617 return; 1699 return;
1618 1700
1701 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1702 counter[CM_REP_COUNTER]);
1619 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg); 1703 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1620 if (ret) 1704 if (ret)
1621 goto deref; 1705 goto deref;
@@ -1781,6 +1865,8 @@ static int cm_rtu_handler(struct cm_work *work)
1781 if (cm_id_priv->id.state != IB_CM_REP_SENT && 1865 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1782 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) { 1866 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1783 spin_unlock_irq(&cm_id_priv->lock); 1867 spin_unlock_irq(&cm_id_priv->lock);
1868 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1869 counter[CM_RTU_COUNTER]);
1784 goto out; 1870 goto out;
1785 } 1871 }
1786 cm_id_priv->id.state = IB_CM_ESTABLISHED; 1872 cm_id_priv->id.state = IB_CM_ESTABLISHED;
@@ -1958,6 +2044,8 @@ static int cm_dreq_handler(struct cm_work *work)
1958 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id, 2044 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1959 dreq_msg->local_comm_id); 2045 dreq_msg->local_comm_id);
1960 if (!cm_id_priv) { 2046 if (!cm_id_priv) {
2047 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2048 counter[CM_DREQ_COUNTER]);
1961 cm_issue_drep(work->port, work->mad_recv_wc); 2049 cm_issue_drep(work->port, work->mad_recv_wc);
1962 return -EINVAL; 2050 return -EINVAL;
1963 } 2051 }
@@ -1977,6 +2065,8 @@ static int cm_dreq_handler(struct cm_work *work)
1977 case IB_CM_MRA_REP_RCVD: 2065 case IB_CM_MRA_REP_RCVD:
1978 break; 2066 break;
1979 case IB_CM_TIMEWAIT: 2067 case IB_CM_TIMEWAIT:
2068 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2069 counter[CM_DREQ_COUNTER]);
1980 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg)) 2070 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1981 goto unlock; 2071 goto unlock;
1982 2072
@@ -1988,6 +2078,10 @@ static int cm_dreq_handler(struct cm_work *work)
1988 if (ib_post_send_mad(msg, NULL)) 2078 if (ib_post_send_mad(msg, NULL))
1989 cm_free_msg(msg); 2079 cm_free_msg(msg);
1990 goto deref; 2080 goto deref;
2081 case IB_CM_DREQ_RCVD:
2082 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2083 counter[CM_DREQ_COUNTER]);
2084 goto unlock;
1991 default: 2085 default:
1992 goto unlock; 2086 goto unlock;
1993 } 2087 }
@@ -2339,10 +2433,20 @@ static int cm_mra_handler(struct cm_work *work)
2339 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER || 2433 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2340 cm_id_priv->id.lap_state != IB_CM_LAP_SENT || 2434 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2341 ib_modify_mad(cm_id_priv->av.port->mad_agent, 2435 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2342 cm_id_priv->msg, timeout)) 2436 cm_id_priv->msg, timeout)) {
2437 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2438 atomic_long_inc(&work->port->
2439 counter_group[CM_RECV_DUPLICATES].
2440 counter[CM_MRA_COUNTER]);
2343 goto out; 2441 goto out;
2442 }
2344 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD; 2443 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2345 break; 2444 break;
2445 case IB_CM_MRA_REQ_RCVD:
2446 case IB_CM_MRA_REP_RCVD:
2447 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2448 counter[CM_MRA_COUNTER]);
2449 /* fall through */
2346 default: 2450 default:
2347 goto out; 2451 goto out;
2348 } 2452 }
@@ -2502,6 +2606,8 @@ static int cm_lap_handler(struct cm_work *work)
2502 case IB_CM_LAP_IDLE: 2606 case IB_CM_LAP_IDLE:
2503 break; 2607 break;
2504 case IB_CM_MRA_LAP_SENT: 2608 case IB_CM_MRA_LAP_SENT:
2609 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2610 counter[CM_LAP_COUNTER]);
2505 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg)) 2611 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2506 goto unlock; 2612 goto unlock;
2507 2613
@@ -2515,6 +2621,10 @@ static int cm_lap_handler(struct cm_work *work)
2515 if (ib_post_send_mad(msg, NULL)) 2621 if (ib_post_send_mad(msg, NULL))
2516 cm_free_msg(msg); 2622 cm_free_msg(msg);
2517 goto deref; 2623 goto deref;
2624 case IB_CM_LAP_RCVD:
2625 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2626 counter[CM_LAP_COUNTER]);
2627 goto unlock;
2518 default: 2628 default:
2519 goto unlock; 2629 goto unlock;
2520 } 2630 }
@@ -2796,6 +2906,8 @@ static int cm_sidr_req_handler(struct cm_work *work)
2796 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv); 2906 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2797 if (cur_cm_id_priv) { 2907 if (cur_cm_id_priv) {
2798 spin_unlock_irq(&cm.lock); 2908 spin_unlock_irq(&cm.lock);
2909 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2910 counter[CM_SIDR_REQ_COUNTER]);
2799 goto out; /* Duplicate message. */ 2911 goto out; /* Duplicate message. */
2800 } 2912 }
2801 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD; 2913 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
@@ -2990,6 +3102,27 @@ static void cm_send_handler(struct ib_mad_agent *mad_agent,
2990 struct ib_mad_send_wc *mad_send_wc) 3102 struct ib_mad_send_wc *mad_send_wc)
2991{ 3103{
2992 struct ib_mad_send_buf *msg = mad_send_wc->send_buf; 3104 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3105 struct cm_port *port;
3106 u16 attr_index;
3107
3108 port = mad_agent->context;
3109 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3110 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3111
3112 /*
3113 * If the send was in response to a received message (context[0] is not
3114 * set to a cm_id), and is not a REJ, then it is a send that was
3115 * manually retried.
3116 */
3117 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3118 msg->retries = 1;
3119
3120 atomic_long_add(1 + msg->retries,
3121 &port->counter_group[CM_XMIT].counter[attr_index]);
3122 if (msg->retries)
3123 atomic_long_add(msg->retries,
3124 &port->counter_group[CM_XMIT_RETRIES].
3125 counter[attr_index]);
2993 3126
2994 switch (mad_send_wc->status) { 3127 switch (mad_send_wc->status) {
2995 case IB_WC_SUCCESS: 3128 case IB_WC_SUCCESS:
@@ -3148,8 +3281,10 @@ EXPORT_SYMBOL(ib_cm_notify);
3148static void cm_recv_handler(struct ib_mad_agent *mad_agent, 3281static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3149 struct ib_mad_recv_wc *mad_recv_wc) 3282 struct ib_mad_recv_wc *mad_recv_wc)
3150{ 3283{
3284 struct cm_port *port = mad_agent->context;
3151 struct cm_work *work; 3285 struct cm_work *work;
3152 enum ib_cm_event_type event; 3286 enum ib_cm_event_type event;
3287 u16 attr_id;
3153 int paths = 0; 3288 int paths = 0;
3154 3289
3155 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) { 3290 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
@@ -3194,6 +3329,10 @@ static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3194 return; 3329 return;
3195 } 3330 }
3196 3331
3332 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3333 atomic_long_inc(&port->counter_group[CM_RECV].
3334 counter[attr_id - CM_ATTR_ID_OFFSET]);
3335
3197 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths, 3336 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3198 GFP_KERNEL); 3337 GFP_KERNEL);
3199 if (!work) { 3338 if (!work) {
@@ -3204,7 +3343,7 @@ static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3204 INIT_DELAYED_WORK(&work->work, cm_work_handler); 3343 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3205 work->cm_event.event = event; 3344 work->cm_event.event = event;
3206 work->mad_recv_wc = mad_recv_wc; 3345 work->mad_recv_wc = mad_recv_wc;
3207 work->port = (struct cm_port *)mad_agent->context; 3346 work->port = port;
3208 queue_delayed_work(cm.wq, &work->work, 0); 3347 queue_delayed_work(cm.wq, &work->work, 0);
3209} 3348}
3210 3349
@@ -3379,6 +3518,108 @@ static void cm_get_ack_delay(struct cm_device *cm_dev)
3379 cm_dev->ack_delay = attr.local_ca_ack_delay; 3518 cm_dev->ack_delay = attr.local_ca_ack_delay;
3380} 3519}
3381 3520
3521static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3522 char *buf)
3523{
3524 struct cm_counter_group *group;
3525 struct cm_counter_attribute *cm_attr;
3526
3527 group = container_of(obj, struct cm_counter_group, obj);
3528 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3529
3530 return sprintf(buf, "%ld\n",
3531 atomic_long_read(&group->counter[cm_attr->index]));
3532}
3533
3534static struct sysfs_ops cm_counter_ops = {
3535 .show = cm_show_counter
3536};
3537
3538static struct kobj_type cm_counter_obj_type = {
3539 .sysfs_ops = &cm_counter_ops,
3540 .default_attrs = cm_counter_default_attrs
3541};
3542
3543static void cm_release_port_obj(struct kobject *obj)
3544{
3545 struct cm_port *cm_port;
3546
3547 printk(KERN_ERR "free cm port\n");
3548
3549 cm_port = container_of(obj, struct cm_port, port_obj);
3550 kfree(cm_port);
3551}
3552
3553static struct kobj_type cm_port_obj_type = {
3554 .release = cm_release_port_obj
3555};
3556
3557static void cm_release_dev_obj(struct kobject *obj)
3558{
3559 struct cm_device *cm_dev;
3560
3561 printk(KERN_ERR "free cm dev\n");
3562
3563 cm_dev = container_of(obj, struct cm_device, dev_obj);
3564 kfree(cm_dev);
3565}
3566
3567static struct kobj_type cm_dev_obj_type = {
3568 .release = cm_release_dev_obj
3569};
3570
3571struct class cm_class = {
3572 .name = "infiniband_cm",
3573};
3574EXPORT_SYMBOL(cm_class);
3575
3576static void cm_remove_fs_obj(struct kobject *obj)
3577{
3578 kobject_put(obj->parent);
3579 kobject_put(obj);
3580}
3581
3582static int cm_create_port_fs(struct cm_port *port)
3583{
3584 int i, ret;
3585
3586 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
3587 kobject_get(&port->cm_dev->dev_obj),
3588 "%d", port->port_num);
3589 if (ret) {
3590 kfree(port);
3591 return ret;
3592 }
3593
3594 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3595 ret = kobject_init_and_add(&port->counter_group[i].obj,
3596 &cm_counter_obj_type,
3597 kobject_get(&port->port_obj),
3598 "%s", counter_group_names[i]);
3599 if (ret)
3600 goto error;
3601 }
3602
3603 return 0;
3604
3605error:
3606 while (i--)
3607 cm_remove_fs_obj(&port->counter_group[i].obj);
3608 cm_remove_fs_obj(&port->port_obj);
3609 return ret;
3610
3611}
3612
3613static void cm_remove_port_fs(struct cm_port *port)
3614{
3615 int i;
3616
3617 for (i = 0; i < CM_COUNTER_GROUPS; i++)
3618 cm_remove_fs_obj(&port->counter_group[i].obj);
3619
3620 cm_remove_fs_obj(&port->port_obj);
3621}
3622
3382static void cm_add_one(struct ib_device *device) 3623static void cm_add_one(struct ib_device *device)
3383{ 3624{
3384 struct cm_device *cm_dev; 3625 struct cm_device *cm_dev;
@@ -3397,7 +3638,7 @@ static void cm_add_one(struct ib_device *device)
3397 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) 3638 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3398 return; 3639 return;
3399 3640
3400 cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) * 3641 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
3401 device->phys_port_cnt, GFP_KERNEL); 3642 device->phys_port_cnt, GFP_KERNEL);
3402 if (!cm_dev) 3643 if (!cm_dev)
3403 return; 3644 return;
@@ -3405,11 +3646,27 @@ static void cm_add_one(struct ib_device *device)
3405 cm_dev->device = device; 3646 cm_dev->device = device;
3406 cm_get_ack_delay(cm_dev); 3647 cm_get_ack_delay(cm_dev);
3407 3648
3649 ret = kobject_init_and_add(&cm_dev->dev_obj, &cm_dev_obj_type,
3650 &cm_class.subsys.kobj, "%s", device->name);
3651 if (ret) {
3652 kfree(cm_dev);
3653 return;
3654 }
3655
3408 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask); 3656 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3409 for (i = 1; i <= device->phys_port_cnt; i++) { 3657 for (i = 1; i <= device->phys_port_cnt; i++) {
3410 port = &cm_dev->port[i-1]; 3658 port = kzalloc(sizeof *port, GFP_KERNEL);
3659 if (!port)
3660 goto error1;
3661
3662 cm_dev->port[i-1] = port;
3411 port->cm_dev = cm_dev; 3663 port->cm_dev = cm_dev;
3412 port->port_num = i; 3664 port->port_num = i;
3665
3666 ret = cm_create_port_fs(port);
3667 if (ret)
3668 goto error1;
3669
3413 port->mad_agent = ib_register_mad_agent(device, i, 3670 port->mad_agent = ib_register_mad_agent(device, i,
3414 IB_QPT_GSI, 3671 IB_QPT_GSI,
3415 &reg_req, 3672 &reg_req,
@@ -3418,11 +3675,11 @@ static void cm_add_one(struct ib_device *device)
3418 cm_recv_handler, 3675 cm_recv_handler,
3419 port); 3676 port);
3420 if (IS_ERR(port->mad_agent)) 3677 if (IS_ERR(port->mad_agent))
3421 goto error1; 3678 goto error2;
3422 3679
3423 ret = ib_modify_port(device, i, 0, &port_modify); 3680 ret = ib_modify_port(device, i, 0, &port_modify);
3424 if (ret) 3681 if (ret)
3425 goto error2; 3682 goto error3;
3426 } 3683 }
3427 ib_set_client_data(device, &cm_client, cm_dev); 3684 ib_set_client_data(device, &cm_client, cm_dev);
3428 3685
@@ -3431,17 +3688,20 @@ static void cm_add_one(struct ib_device *device)
3431 write_unlock_irqrestore(&cm.device_lock, flags); 3688 write_unlock_irqrestore(&cm.device_lock, flags);
3432 return; 3689 return;
3433 3690
3434error2: 3691error3:
3435 ib_unregister_mad_agent(port->mad_agent); 3692 ib_unregister_mad_agent(port->mad_agent);
3693error2:
3694 cm_remove_port_fs(port);
3436error1: 3695error1:
3437 port_modify.set_port_cap_mask = 0; 3696 port_modify.set_port_cap_mask = 0;
3438 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP; 3697 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3439 while (--i) { 3698 while (--i) {
3440 port = &cm_dev->port[i-1]; 3699 port = cm_dev->port[i-1];
3441 ib_modify_port(device, port->port_num, 0, &port_modify); 3700 ib_modify_port(device, port->port_num, 0, &port_modify);
3442 ib_unregister_mad_agent(port->mad_agent); 3701 ib_unregister_mad_agent(port->mad_agent);
3702 cm_remove_port_fs(port);
3443 } 3703 }
3444 kfree(cm_dev); 3704 cm_remove_fs_obj(&cm_dev->dev_obj);
3445} 3705}
3446 3706
3447static void cm_remove_one(struct ib_device *device) 3707static void cm_remove_one(struct ib_device *device)
@@ -3463,11 +3723,12 @@ static void cm_remove_one(struct ib_device *device)
3463 write_unlock_irqrestore(&cm.device_lock, flags); 3723 write_unlock_irqrestore(&cm.device_lock, flags);
3464 3724
3465 for (i = 1; i <= device->phys_port_cnt; i++) { 3725 for (i = 1; i <= device->phys_port_cnt; i++) {
3466 port = &cm_dev->port[i-1]; 3726 port = cm_dev->port[i-1];
3467 ib_modify_port(device, port->port_num, 0, &port_modify); 3727 ib_modify_port(device, port->port_num, 0, &port_modify);
3468 ib_unregister_mad_agent(port->mad_agent); 3728 ib_unregister_mad_agent(port->mad_agent);
3729 cm_remove_port_fs(port);
3469 } 3730 }
3470 kfree(cm_dev); 3731 cm_remove_fs_obj(&cm_dev->dev_obj);
3471} 3732}
3472 3733
3473static int __init ib_cm_init(void) 3734static int __init ib_cm_init(void)
@@ -3488,17 +3749,25 @@ static int __init ib_cm_init(void)
3488 idr_pre_get(&cm.local_id_table, GFP_KERNEL); 3749 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3489 INIT_LIST_HEAD(&cm.timewait_list); 3750 INIT_LIST_HEAD(&cm.timewait_list);
3490 3751
3491 cm.wq = create_workqueue("ib_cm"); 3752 ret = class_register(&cm_class);
3492 if (!cm.wq) 3753 if (ret)
3493 return -ENOMEM; 3754 return -ENOMEM;
3494 3755
3756 cm.wq = create_workqueue("ib_cm");
3757 if (!cm.wq) {
3758 ret = -ENOMEM;
3759 goto error1;
3760 }
3761
3495 ret = ib_register_client(&cm_client); 3762 ret = ib_register_client(&cm_client);
3496 if (ret) 3763 if (ret)
3497 goto error; 3764 goto error2;
3498 3765
3499 return 0; 3766 return 0;
3500error: 3767error2:
3501 destroy_workqueue(cm.wq); 3768 destroy_workqueue(cm.wq);
3769error1:
3770 class_unregister(&cm_class);
3502 return ret; 3771 return ret;
3503} 3772}
3504 3773
@@ -3519,6 +3788,7 @@ static void __exit ib_cm_cleanup(void)
3519 } 3788 }
3520 3789
3521 ib_unregister_client(&cm_client); 3790 ib_unregister_client(&cm_client);
3791 class_unregister(&cm_class);
3522 idr_destroy(&cm.local_id_table); 3792 idr_destroy(&cm.local_id_table);
3523} 3793}
3524 3794
diff --git a/drivers/infiniband/core/cma.c b/drivers/infiniband/core/cma.c
index 0751697ef984..637efead97a0 100644
--- a/drivers/infiniband/core/cma.c
+++ b/drivers/infiniband/core/cma.c
@@ -488,7 +488,8 @@ void rdma_destroy_qp(struct rdma_cm_id *id)
488} 488}
489EXPORT_SYMBOL(rdma_destroy_qp); 489EXPORT_SYMBOL(rdma_destroy_qp);
490 490
491static int cma_modify_qp_rtr(struct rdma_id_private *id_priv) 491static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
492 struct rdma_conn_param *conn_param)
492{ 493{
493 struct ib_qp_attr qp_attr; 494 struct ib_qp_attr qp_attr;
494 int qp_attr_mask, ret; 495 int qp_attr_mask, ret;
@@ -514,13 +515,16 @@ static int cma_modify_qp_rtr(struct rdma_id_private *id_priv)
514 if (ret) 515 if (ret)
515 goto out; 516 goto out;
516 517
518 if (conn_param)
519 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
517 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); 520 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
518out: 521out:
519 mutex_unlock(&id_priv->qp_mutex); 522 mutex_unlock(&id_priv->qp_mutex);
520 return ret; 523 return ret;
521} 524}
522 525
523static int cma_modify_qp_rts(struct rdma_id_private *id_priv) 526static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
527 struct rdma_conn_param *conn_param)
524{ 528{
525 struct ib_qp_attr qp_attr; 529 struct ib_qp_attr qp_attr;
526 int qp_attr_mask, ret; 530 int qp_attr_mask, ret;
@@ -536,6 +540,8 @@ static int cma_modify_qp_rts(struct rdma_id_private *id_priv)
536 if (ret) 540 if (ret)
537 goto out; 541 goto out;
538 542
543 if (conn_param)
544 qp_attr.max_rd_atomic = conn_param->initiator_depth;
539 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); 545 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
540out: 546out:
541 mutex_unlock(&id_priv->qp_mutex); 547 mutex_unlock(&id_priv->qp_mutex);
@@ -866,11 +872,11 @@ static int cma_rep_recv(struct rdma_id_private *id_priv)
866{ 872{
867 int ret; 873 int ret;
868 874
869 ret = cma_modify_qp_rtr(id_priv); 875 ret = cma_modify_qp_rtr(id_priv, NULL);
870 if (ret) 876 if (ret)
871 goto reject; 877 goto reject;
872 878
873 ret = cma_modify_qp_rts(id_priv); 879 ret = cma_modify_qp_rts(id_priv, NULL);
874 if (ret) 880 if (ret)
875 goto reject; 881 goto reject;
876 882
@@ -1122,8 +1128,10 @@ static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1122 cm_id->cm_handler = cma_ib_handler; 1128 cm_id->cm_handler = cma_ib_handler;
1123 1129
1124 ret = conn_id->id.event_handler(&conn_id->id, &event); 1130 ret = conn_id->id.event_handler(&conn_id->id, &event);
1125 if (!ret) 1131 if (!ret) {
1132 cma_enable_remove(conn_id);
1126 goto out; 1133 goto out;
1134 }
1127 1135
1128 /* Destroy the CM ID by returning a non-zero value. */ 1136 /* Destroy the CM ID by returning a non-zero value. */
1129 conn_id->cm_id.ib = NULL; 1137 conn_id->cm_id.ib = NULL;
@@ -1262,6 +1270,7 @@ static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1262 struct net_device *dev = NULL; 1270 struct net_device *dev = NULL;
1263 struct rdma_cm_event event; 1271 struct rdma_cm_event event;
1264 int ret; 1272 int ret;
1273 struct ib_device_attr attr;
1265 1274
1266 listen_id = cm_id->context; 1275 listen_id = cm_id->context;
1267 if (cma_disable_remove(listen_id, CMA_LISTEN)) 1276 if (cma_disable_remove(listen_id, CMA_LISTEN))
@@ -1311,10 +1320,19 @@ static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1311 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr; 1320 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1312 *sin = iw_event->remote_addr; 1321 *sin = iw_event->remote_addr;
1313 1322
1323 ret = ib_query_device(conn_id->id.device, &attr);
1324 if (ret) {
1325 cma_enable_remove(conn_id);
1326 rdma_destroy_id(new_cm_id);
1327 goto out;
1328 }
1329
1314 memset(&event, 0, sizeof event); 1330 memset(&event, 0, sizeof event);
1315 event.event = RDMA_CM_EVENT_CONNECT_REQUEST; 1331 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1316 event.param.conn.private_data = iw_event->private_data; 1332 event.param.conn.private_data = iw_event->private_data;
1317 event.param.conn.private_data_len = iw_event->private_data_len; 1333 event.param.conn.private_data_len = iw_event->private_data_len;
1334 event.param.conn.initiator_depth = attr.max_qp_init_rd_atom;
1335 event.param.conn.responder_resources = attr.max_qp_rd_atom;
1318 ret = conn_id->id.event_handler(&conn_id->id, &event); 1336 ret = conn_id->id.event_handler(&conn_id->id, &event);
1319 if (ret) { 1337 if (ret) {
1320 /* User wants to destroy the CM ID */ 1338 /* User wants to destroy the CM ID */
@@ -2272,7 +2290,7 @@ static int cma_connect_iw(struct rdma_id_private *id_priv,
2272 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr; 2290 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2273 cm_id->remote_addr = *sin; 2291 cm_id->remote_addr = *sin;
2274 2292
2275 ret = cma_modify_qp_rtr(id_priv); 2293 ret = cma_modify_qp_rtr(id_priv, conn_param);
2276 if (ret) 2294 if (ret)
2277 goto out; 2295 goto out;
2278 2296
@@ -2335,25 +2353,15 @@ static int cma_accept_ib(struct rdma_id_private *id_priv,
2335 struct rdma_conn_param *conn_param) 2353 struct rdma_conn_param *conn_param)
2336{ 2354{
2337 struct ib_cm_rep_param rep; 2355 struct ib_cm_rep_param rep;
2338 struct ib_qp_attr qp_attr; 2356 int ret;
2339 int qp_attr_mask, ret;
2340
2341 if (id_priv->id.qp) {
2342 ret = cma_modify_qp_rtr(id_priv);
2343 if (ret)
2344 goto out;
2345 2357
2346 qp_attr.qp_state = IB_QPS_RTS; 2358 ret = cma_modify_qp_rtr(id_priv, conn_param);
2347 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, &qp_attr, 2359 if (ret)
2348 &qp_attr_mask); 2360 goto out;
2349 if (ret)
2350 goto out;
2351 2361
2352 qp_attr.max_rd_atomic = conn_param->initiator_depth; 2362 ret = cma_modify_qp_rts(id_priv, conn_param);
2353 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); 2363 if (ret)
2354 if (ret) 2364 goto out;
2355 goto out;
2356 }
2357 2365
2358 memset(&rep, 0, sizeof rep); 2366 memset(&rep, 0, sizeof rep);
2359 rep.qp_num = id_priv->qp_num; 2367 rep.qp_num = id_priv->qp_num;
@@ -2378,7 +2386,7 @@ static int cma_accept_iw(struct rdma_id_private *id_priv,
2378 struct iw_cm_conn_param iw_param; 2386 struct iw_cm_conn_param iw_param;
2379 int ret; 2387 int ret;
2380 2388
2381 ret = cma_modify_qp_rtr(id_priv); 2389 ret = cma_modify_qp_rtr(id_priv, conn_param);
2382 if (ret) 2390 if (ret)
2383 return ret; 2391 return ret;
2384 2392
@@ -2598,11 +2606,9 @@ static void cma_set_mgid(struct rdma_id_private *id_priv,
2598 /* IPv6 address is an SA assigned MGID. */ 2606 /* IPv6 address is an SA assigned MGID. */
2599 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); 2607 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2600 } else { 2608 } else {
2601 ip_ib_mc_map(sin->sin_addr.s_addr, mc_map); 2609 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
2602 if (id_priv->id.ps == RDMA_PS_UDP) 2610 if (id_priv->id.ps == RDMA_PS_UDP)
2603 mc_map[7] = 0x01; /* Use RDMA CM signature */ 2611 mc_map[7] = 0x01; /* Use RDMA CM signature */
2604 mc_map[8] = ib_addr_get_pkey(dev_addr) >> 8;
2605 mc_map[9] = (unsigned char) ib_addr_get_pkey(dev_addr);
2606 *mgid = *(union ib_gid *) (mc_map + 4); 2612 *mgid = *(union ib_gid *) (mc_map + 4);
2607 } 2613 }
2608} 2614}
diff --git a/drivers/infiniband/core/fmr_pool.c b/drivers/infiniband/core/fmr_pool.c
index e8d5f6b64998..6c7aa59794d4 100644
--- a/drivers/infiniband/core/fmr_pool.c
+++ b/drivers/infiniband/core/fmr_pool.c
@@ -139,7 +139,7 @@ static inline struct ib_pool_fmr *ib_fmr_cache_lookup(struct ib_fmr_pool *pool,
139static void ib_fmr_batch_release(struct ib_fmr_pool *pool) 139static void ib_fmr_batch_release(struct ib_fmr_pool *pool)
140{ 140{
141 int ret; 141 int ret;
142 struct ib_pool_fmr *fmr; 142 struct ib_pool_fmr *fmr, *next;
143 LIST_HEAD(unmap_list); 143 LIST_HEAD(unmap_list);
144 LIST_HEAD(fmr_list); 144 LIST_HEAD(fmr_list);
145 145
@@ -158,6 +158,20 @@ static void ib_fmr_batch_release(struct ib_fmr_pool *pool)
158#endif 158#endif
159 } 159 }
160 160
161 /*
162 * The free_list may hold FMRs that have been put there
163 * because they haven't reached the max_remap count.
164 * Invalidate their mapping as well.
165 */
166 list_for_each_entry_safe(fmr, next, &pool->free_list, list) {
167 if (fmr->remap_count == 0)
168 continue;
169 hlist_del_init(&fmr->cache_node);
170 fmr->remap_count = 0;
171 list_add_tail(&fmr->fmr->list, &fmr_list);
172 list_move(&fmr->list, &unmap_list);
173 }
174
161 list_splice(&pool->dirty_list, &unmap_list); 175 list_splice(&pool->dirty_list, &unmap_list);
162 INIT_LIST_HEAD(&pool->dirty_list); 176 INIT_LIST_HEAD(&pool->dirty_list);
163 pool->dirty_len = 0; 177 pool->dirty_len = 0;
@@ -182,8 +196,7 @@ static int ib_fmr_cleanup_thread(void *pool_ptr)
182 struct ib_fmr_pool *pool = pool_ptr; 196 struct ib_fmr_pool *pool = pool_ptr;
183 197
184 do { 198 do {
185 if (pool->dirty_len >= pool->dirty_watermark || 199 if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) < 0) {
186 atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) < 0) {
187 ib_fmr_batch_release(pool); 200 ib_fmr_batch_release(pool);
188 201
189 atomic_inc(&pool->flush_ser); 202 atomic_inc(&pool->flush_ser);
@@ -194,8 +207,7 @@ static int ib_fmr_cleanup_thread(void *pool_ptr)
194 } 207 }
195 208
196 set_current_state(TASK_INTERRUPTIBLE); 209 set_current_state(TASK_INTERRUPTIBLE);
197 if (pool->dirty_len < pool->dirty_watermark && 210 if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) >= 0 &&
198 atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) >= 0 &&
199 !kthread_should_stop()) 211 !kthread_should_stop())
200 schedule(); 212 schedule();
201 __set_current_state(TASK_RUNNING); 213 __set_current_state(TASK_RUNNING);
@@ -369,11 +381,6 @@ void ib_destroy_fmr_pool(struct ib_fmr_pool *pool)
369 381
370 i = 0; 382 i = 0;
371 list_for_each_entry_safe(fmr, tmp, &pool->free_list, list) { 383 list_for_each_entry_safe(fmr, tmp, &pool->free_list, list) {
372 if (fmr->remap_count) {
373 INIT_LIST_HEAD(&fmr_list);
374 list_add_tail(&fmr->fmr->list, &fmr_list);
375 ib_unmap_fmr(&fmr_list);
376 }
377 ib_dealloc_fmr(fmr->fmr); 384 ib_dealloc_fmr(fmr->fmr);
378 list_del(&fmr->list); 385 list_del(&fmr->list);
379 kfree(fmr); 386 kfree(fmr);
@@ -511,8 +518,10 @@ int ib_fmr_pool_unmap(struct ib_pool_fmr *fmr)
511 list_add_tail(&fmr->list, &pool->free_list); 518 list_add_tail(&fmr->list, &pool->free_list);
512 } else { 519 } else {
513 list_add_tail(&fmr->list, &pool->dirty_list); 520 list_add_tail(&fmr->list, &pool->dirty_list);
514 ++pool->dirty_len; 521 if (++pool->dirty_len >= pool->dirty_watermark) {
515 wake_up_process(pool->thread); 522 atomic_inc(&pool->req_ser);
523 wake_up_process(pool->thread);
524 }
516 } 525 }
517 } 526 }
518 527
diff --git a/drivers/infiniband/core/mad.c b/drivers/infiniband/core/mad.c
index 6f4287716ab1..fbe16d5250a4 100644
--- a/drivers/infiniband/core/mad.c
+++ b/drivers/infiniband/core/mad.c
@@ -701,7 +701,8 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
701 } 701 }
702 702
703 /* Check to post send on QP or process locally */ 703 /* Check to post send on QP or process locally */
704 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD) 704 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
705 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
705 goto out; 706 goto out;
706 707
707 local = kmalloc(sizeof *local, GFP_ATOMIC); 708 local = kmalloc(sizeof *local, GFP_ATOMIC);
@@ -752,8 +753,7 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
752 port_priv = ib_get_mad_port(mad_agent_priv->agent.device, 753 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
753 mad_agent_priv->agent.port_num); 754 mad_agent_priv->agent.port_num);
754 if (port_priv) { 755 if (port_priv) {
755 mad_priv->mad.mad.mad_hdr.tid = 756 memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad));
756 ((struct ib_mad *)smp)->mad_hdr.tid;
757 recv_mad_agent = find_mad_agent(port_priv, 757 recv_mad_agent = find_mad_agent(port_priv,
758 &mad_priv->mad.mad); 758 &mad_priv->mad.mad);
759 } 759 }
@@ -1100,7 +1100,9 @@ int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1100 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid; 1100 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
1101 /* Timeout will be updated after send completes */ 1101 /* Timeout will be updated after send completes */
1102 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms); 1102 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
1103 mad_send_wr->retries = send_buf->retries; 1103 mad_send_wr->max_retries = send_buf->retries;
1104 mad_send_wr->retries_left = send_buf->retries;
1105 send_buf->retries = 0;
1104 /* Reference for work request to QP + response */ 1106 /* Reference for work request to QP + response */
1105 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0); 1107 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1106 mad_send_wr->status = IB_WC_SUCCESS; 1108 mad_send_wr->status = IB_WC_SUCCESS;
@@ -1931,15 +1933,6 @@ local:
1931 if (port_priv->device->process_mad) { 1933 if (port_priv->device->process_mad) {
1932 int ret; 1934 int ret;
1933 1935
1934 if (!response) {
1935 printk(KERN_ERR PFX "No memory for response MAD\n");
1936 /*
1937 * Is it better to assume that
1938 * it wouldn't be processed ?
1939 */
1940 goto out;
1941 }
1942
1943 ret = port_priv->device->process_mad(port_priv->device, 0, 1936 ret = port_priv->device->process_mad(port_priv->device, 0,
1944 port_priv->port_num, 1937 port_priv->port_num,
1945 wc, &recv->grh, 1938 wc, &recv->grh,
@@ -2282,8 +2275,6 @@ static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2282 2275
2283 /* Empty wait list to prevent receives from finding a request */ 2276 /* Empty wait list to prevent receives from finding a request */
2284 list_splice_init(&mad_agent_priv->wait_list, &cancel_list); 2277 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2285 /* Empty local completion list as well */
2286 list_splice_init(&mad_agent_priv->local_list, &cancel_list);
2287 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2278 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2288 2279
2289 /* Report all cancelled requests */ 2280 /* Report all cancelled requests */
@@ -2445,9 +2436,12 @@ static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2445{ 2436{
2446 int ret; 2437 int ret;
2447 2438
2448 if (!mad_send_wr->retries--) 2439 if (!mad_send_wr->retries_left)
2449 return -ETIMEDOUT; 2440 return -ETIMEDOUT;
2450 2441
2442 mad_send_wr->retries_left--;
2443 mad_send_wr->send_buf.retries++;
2444
2451 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms); 2445 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
2452 2446
2453 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) { 2447 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
diff --git a/drivers/infiniband/core/mad_priv.h b/drivers/infiniband/core/mad_priv.h
index 9be5cc00a3a9..8b75010016ec 100644
--- a/drivers/infiniband/core/mad_priv.h
+++ b/drivers/infiniband/core/mad_priv.h
@@ -131,7 +131,8 @@ struct ib_mad_send_wr_private {
131 struct ib_sge sg_list[IB_MAD_SEND_REQ_MAX_SG]; 131 struct ib_sge sg_list[IB_MAD_SEND_REQ_MAX_SG];
132 __be64 tid; 132 __be64 tid;
133 unsigned long timeout; 133 unsigned long timeout;
134 int retries; 134 int max_retries;
135 int retries_left;
135 int retry; 136 int retry;
136 int refcount; 137 int refcount;
137 enum ib_wc_status status; 138 enum ib_wc_status status;
diff --git a/drivers/infiniband/core/mad_rmpp.c b/drivers/infiniband/core/mad_rmpp.c
index d43bc62005b3..a5e2a310f312 100644
--- a/drivers/infiniband/core/mad_rmpp.c
+++ b/drivers/infiniband/core/mad_rmpp.c
@@ -684,7 +684,7 @@ static void process_rmpp_ack(struct ib_mad_agent_private *agent,
684 684
685 if (seg_num > mad_send_wr->last_ack) { 685 if (seg_num > mad_send_wr->last_ack) {
686 adjust_last_ack(mad_send_wr, seg_num); 686 adjust_last_ack(mad_send_wr, seg_num);
687 mad_send_wr->retries = mad_send_wr->send_buf.retries; 687 mad_send_wr->retries_left = mad_send_wr->max_retries;
688 } 688 }
689 mad_send_wr->newwin = newwin; 689 mad_send_wr->newwin = newwin;
690 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) { 690 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
diff --git a/drivers/infiniband/core/multicast.c b/drivers/infiniband/core/multicast.c
index 1bc1fe605282..107f170c57cd 100644
--- a/drivers/infiniband/core/multicast.c
+++ b/drivers/infiniband/core/multicast.c
@@ -73,11 +73,20 @@ struct mcast_device {
73}; 73};
74 74
75enum mcast_state { 75enum mcast_state {
76 MCAST_IDLE,
77 MCAST_JOINING, 76 MCAST_JOINING,
78 MCAST_MEMBER, 77 MCAST_MEMBER,
78 MCAST_ERROR,
79};
80
81enum mcast_group_state {
82 MCAST_IDLE,
79 MCAST_BUSY, 83 MCAST_BUSY,
80 MCAST_ERROR 84 MCAST_GROUP_ERROR,
85 MCAST_PKEY_EVENT
86};
87
88enum {
89 MCAST_INVALID_PKEY_INDEX = 0xFFFF
81}; 90};
82 91
83struct mcast_member; 92struct mcast_member;
@@ -93,9 +102,10 @@ struct mcast_group {
93 struct mcast_member *last_join; 102 struct mcast_member *last_join;
94 int members[3]; 103 int members[3];
95 atomic_t refcount; 104 atomic_t refcount;
96 enum mcast_state state; 105 enum mcast_group_state state;
97 struct ib_sa_query *query; 106 struct ib_sa_query *query;
98 int query_id; 107 int query_id;
108 u16 pkey_index;
99}; 109};
100 110
101struct mcast_member { 111struct mcast_member {
@@ -378,9 +388,19 @@ static int fail_join(struct mcast_group *group, struct mcast_member *member,
378static void process_group_error(struct mcast_group *group) 388static void process_group_error(struct mcast_group *group)
379{ 389{
380 struct mcast_member *member; 390 struct mcast_member *member;
381 int ret; 391 int ret = 0;
392 u16 pkey_index;
393
394 if (group->state == MCAST_PKEY_EVENT)
395 ret = ib_find_pkey(group->port->dev->device,
396 group->port->port_num,
397 be16_to_cpu(group->rec.pkey), &pkey_index);
382 398
383 spin_lock_irq(&group->lock); 399 spin_lock_irq(&group->lock);
400 if (group->state == MCAST_PKEY_EVENT && !ret &&
401 group->pkey_index == pkey_index)
402 goto out;
403
384 while (!list_empty(&group->active_list)) { 404 while (!list_empty(&group->active_list)) {
385 member = list_entry(group->active_list.next, 405 member = list_entry(group->active_list.next,
386 struct mcast_member, list); 406 struct mcast_member, list);
@@ -399,6 +419,7 @@ static void process_group_error(struct mcast_group *group)
399 } 419 }
400 420
401 group->rec.join_state = 0; 421 group->rec.join_state = 0;
422out:
402 group->state = MCAST_BUSY; 423 group->state = MCAST_BUSY;
403 spin_unlock_irq(&group->lock); 424 spin_unlock_irq(&group->lock);
404} 425}
@@ -415,9 +436,9 @@ static void mcast_work_handler(struct work_struct *work)
415retest: 436retest:
416 spin_lock_irq(&group->lock); 437 spin_lock_irq(&group->lock);
417 while (!list_empty(&group->pending_list) || 438 while (!list_empty(&group->pending_list) ||
418 (group->state == MCAST_ERROR)) { 439 (group->state != MCAST_BUSY)) {
419 440
420 if (group->state == MCAST_ERROR) { 441 if (group->state != MCAST_BUSY) {
421 spin_unlock_irq(&group->lock); 442 spin_unlock_irq(&group->lock);
422 process_group_error(group); 443 process_group_error(group);
423 goto retest; 444 goto retest;
@@ -494,12 +515,19 @@ static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
494 void *context) 515 void *context)
495{ 516{
496 struct mcast_group *group = context; 517 struct mcast_group *group = context;
518 u16 pkey_index = MCAST_INVALID_PKEY_INDEX;
497 519
498 if (status) 520 if (status)
499 process_join_error(group, status); 521 process_join_error(group, status);
500 else { 522 else {
523 ib_find_pkey(group->port->dev->device, group->port->port_num,
524 be16_to_cpu(rec->pkey), &pkey_index);
525
501 spin_lock_irq(&group->port->lock); 526 spin_lock_irq(&group->port->lock);
502 group->rec = *rec; 527 group->rec = *rec;
528 if (group->state == MCAST_BUSY &&
529 group->pkey_index == MCAST_INVALID_PKEY_INDEX)
530 group->pkey_index = pkey_index;
503 if (!memcmp(&mgid0, &group->rec.mgid, sizeof mgid0)) { 531 if (!memcmp(&mgid0, &group->rec.mgid, sizeof mgid0)) {
504 rb_erase(&group->node, &group->port->table); 532 rb_erase(&group->node, &group->port->table);
505 mcast_insert(group->port, group, 1); 533 mcast_insert(group->port, group, 1);
@@ -539,6 +567,7 @@ static struct mcast_group *acquire_group(struct mcast_port *port,
539 567
540 group->port = port; 568 group->port = port;
541 group->rec.mgid = *mgid; 569 group->rec.mgid = *mgid;
570 group->pkey_index = MCAST_INVALID_PKEY_INDEX;
542 INIT_LIST_HEAD(&group->pending_list); 571 INIT_LIST_HEAD(&group->pending_list);
543 INIT_LIST_HEAD(&group->active_list); 572 INIT_LIST_HEAD(&group->active_list);
544 INIT_WORK(&group->work, mcast_work_handler); 573 INIT_WORK(&group->work, mcast_work_handler);
@@ -707,7 +736,8 @@ int ib_init_ah_from_mcmember(struct ib_device *device, u8 port_num,
707} 736}
708EXPORT_SYMBOL(ib_init_ah_from_mcmember); 737EXPORT_SYMBOL(ib_init_ah_from_mcmember);
709 738
710static void mcast_groups_lost(struct mcast_port *port) 739static void mcast_groups_event(struct mcast_port *port,
740 enum mcast_group_state state)
711{ 741{
712 struct mcast_group *group; 742 struct mcast_group *group;
713 struct rb_node *node; 743 struct rb_node *node;
@@ -721,7 +751,8 @@ static void mcast_groups_lost(struct mcast_port *port)
721 atomic_inc(&group->refcount); 751 atomic_inc(&group->refcount);
722 queue_work(mcast_wq, &group->work); 752 queue_work(mcast_wq, &group->work);
723 } 753 }
724 group->state = MCAST_ERROR; 754 if (group->state != MCAST_GROUP_ERROR)
755 group->state = state;
725 spin_unlock(&group->lock); 756 spin_unlock(&group->lock);
726 } 757 }
727 spin_unlock_irqrestore(&port->lock, flags); 758 spin_unlock_irqrestore(&port->lock, flags);
@@ -731,16 +762,20 @@ static void mcast_event_handler(struct ib_event_handler *handler,
731 struct ib_event *event) 762 struct ib_event *event)
732{ 763{
733 struct mcast_device *dev; 764 struct mcast_device *dev;
765 int index;
734 766
735 dev = container_of(handler, struct mcast_device, event_handler); 767 dev = container_of(handler, struct mcast_device, event_handler);
768 index = event->element.port_num - dev->start_port;
736 769
737 switch (event->event) { 770 switch (event->event) {
738 case IB_EVENT_PORT_ERR: 771 case IB_EVENT_PORT_ERR:
739 case IB_EVENT_LID_CHANGE: 772 case IB_EVENT_LID_CHANGE:
740 case IB_EVENT_SM_CHANGE: 773 case IB_EVENT_SM_CHANGE:
741 case IB_EVENT_CLIENT_REREGISTER: 774 case IB_EVENT_CLIENT_REREGISTER:
742 mcast_groups_lost(&dev->port[event->element.port_num - 775 mcast_groups_event(&dev->port[index], MCAST_GROUP_ERROR);
743 dev->start_port]); 776 break;
777 case IB_EVENT_PKEY_CHANGE:
778 mcast_groups_event(&dev->port[index], MCAST_PKEY_EVENT);
744 break; 779 break;
745 default: 780 default:
746 break; 781 break;
diff --git a/drivers/infiniband/core/smi.h b/drivers/infiniband/core/smi.h
index 1cfc2984434f..aff96bac49b4 100644
--- a/drivers/infiniband/core/smi.h
+++ b/drivers/infiniband/core/smi.h
@@ -59,7 +59,8 @@ extern enum smi_action smi_handle_dr_smp_send(struct ib_smp *smp,
59 u8 node_type, int port_num); 59 u8 node_type, int port_num);
60 60
61/* 61/*
62 * Return 1 if the SMP should be handled by the local SMA/SM via process_mad 62 * Return IB_SMI_HANDLE if the SMP should be handled by the local SMA/SM
63 * via process_mad
63 */ 64 */
64static inline enum smi_action smi_check_local_smp(struct ib_smp *smp, 65static inline enum smi_action smi_check_local_smp(struct ib_smp *smp,
65 struct ib_device *device) 66 struct ib_device *device)
@@ -71,4 +72,19 @@ static inline enum smi_action smi_check_local_smp(struct ib_smp *smp,
71 (smp->hop_ptr == smp->hop_cnt + 1)) ? 72 (smp->hop_ptr == smp->hop_cnt + 1)) ?
72 IB_SMI_HANDLE : IB_SMI_DISCARD); 73 IB_SMI_HANDLE : IB_SMI_DISCARD);
73} 74}
75
76/*
77 * Return IB_SMI_HANDLE if the SMP should be handled by the local SMA/SM
78 * via process_mad
79 */
80static inline enum smi_action smi_check_local_returning_smp(struct ib_smp *smp,
81 struct ib_device *device)
82{
83 /* C14-13:3 -- We're at the end of the DR segment of path */
84 /* C14-13:4 -- Hop Pointer == 0 -> give to SM */
85 return ((device->process_mad &&
86 ib_get_smp_direction(smp) &&
87 !smp->hop_ptr) ? IB_SMI_HANDLE : IB_SMI_DISCARD);
88}
89
74#endif /* __SMI_H_ */ 90#endif /* __SMI_H_ */
diff --git a/drivers/infiniband/core/ucm.c b/drivers/infiniband/core/ucm.c
index 424983f5b1ee..4291ab42a5b9 100644
--- a/drivers/infiniband/core/ucm.c
+++ b/drivers/infiniband/core/ucm.c
@@ -106,6 +106,9 @@ enum {
106 IB_UCM_MAX_DEVICES = 32 106 IB_UCM_MAX_DEVICES = 32
107}; 107};
108 108
109/* ib_cm and ib_user_cm modules share /sys/class/infiniband_cm */
110extern struct class cm_class;
111
109#define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR) 112#define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
110 113
111static void ib_ucm_add_one(struct ib_device *device); 114static void ib_ucm_add_one(struct ib_device *device);
@@ -1199,7 +1202,7 @@ static int ib_ucm_close(struct inode *inode, struct file *filp)
1199 return 0; 1202 return 0;
1200} 1203}
1201 1204
1202static void ib_ucm_release_class_dev(struct class_device *class_dev) 1205static void ucm_release_class_dev(struct class_device *class_dev)
1203{ 1206{
1204 struct ib_ucm_device *dev; 1207 struct ib_ucm_device *dev;
1205 1208
@@ -1217,11 +1220,6 @@ static const struct file_operations ucm_fops = {
1217 .poll = ib_ucm_poll, 1220 .poll = ib_ucm_poll,
1218}; 1221};
1219 1222
1220static struct class ucm_class = {
1221 .name = "infiniband_cm",
1222 .release = ib_ucm_release_class_dev
1223};
1224
1225static ssize_t show_ibdev(struct class_device *class_dev, char *buf) 1223static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1226{ 1224{
1227 struct ib_ucm_device *dev; 1225 struct ib_ucm_device *dev;
@@ -1257,9 +1255,10 @@ static void ib_ucm_add_one(struct ib_device *device)
1257 if (cdev_add(&ucm_dev->dev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1)) 1255 if (cdev_add(&ucm_dev->dev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1))
1258 goto err; 1256 goto err;
1259 1257
1260 ucm_dev->class_dev.class = &ucm_class; 1258 ucm_dev->class_dev.class = &cm_class;
1261 ucm_dev->class_dev.dev = device->dma_device; 1259 ucm_dev->class_dev.dev = device->dma_device;
1262 ucm_dev->class_dev.devt = ucm_dev->dev.dev; 1260 ucm_dev->class_dev.devt = ucm_dev->dev.dev;
1261 ucm_dev->class_dev.release = ucm_release_class_dev;
1263 snprintf(ucm_dev->class_dev.class_id, BUS_ID_SIZE, "ucm%d", 1262 snprintf(ucm_dev->class_dev.class_id, BUS_ID_SIZE, "ucm%d",
1264 ucm_dev->devnum); 1263 ucm_dev->devnum);
1265 if (class_device_register(&ucm_dev->class_dev)) 1264 if (class_device_register(&ucm_dev->class_dev))
@@ -1306,40 +1305,34 @@ static int __init ib_ucm_init(void)
1306 "infiniband_cm"); 1305 "infiniband_cm");
1307 if (ret) { 1306 if (ret) {
1308 printk(KERN_ERR "ucm: couldn't register device number\n"); 1307 printk(KERN_ERR "ucm: couldn't register device number\n");
1309 goto err; 1308 goto error1;
1310 } 1309 }
1311 1310
1312 ret = class_register(&ucm_class); 1311 ret = class_create_file(&cm_class, &class_attr_abi_version);
1313 if (ret) {
1314 printk(KERN_ERR "ucm: couldn't create class infiniband_cm\n");
1315 goto err_chrdev;
1316 }
1317
1318 ret = class_create_file(&ucm_class, &class_attr_abi_version);
1319 if (ret) { 1312 if (ret) {
1320 printk(KERN_ERR "ucm: couldn't create abi_version attribute\n"); 1313 printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
1321 goto err_class; 1314 goto error2;
1322 } 1315 }
1323 1316
1324 ret = ib_register_client(&ucm_client); 1317 ret = ib_register_client(&ucm_client);
1325 if (ret) { 1318 if (ret) {
1326 printk(KERN_ERR "ucm: couldn't register client\n"); 1319 printk(KERN_ERR "ucm: couldn't register client\n");
1327 goto err_class; 1320 goto error3;
1328 } 1321 }
1329 return 0; 1322 return 0;
1330 1323
1331err_class: 1324error3:
1332 class_unregister(&ucm_class); 1325 class_remove_file(&cm_class, &class_attr_abi_version);
1333err_chrdev: 1326error2:
1334 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES); 1327 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1335err: 1328error1:
1336 return ret; 1329 return ret;
1337} 1330}
1338 1331
1339static void __exit ib_ucm_cleanup(void) 1332static void __exit ib_ucm_cleanup(void)
1340{ 1333{
1341 ib_unregister_client(&ucm_client); 1334 ib_unregister_client(&ucm_client);
1342 class_unregister(&ucm_class); 1335 class_remove_file(&cm_class, &class_attr_abi_version);
1343 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES); 1336 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1344 idr_destroy(&ctx_id_table); 1337 idr_destroy(&ctx_id_table);
1345} 1338}
diff --git a/drivers/infiniband/core/ucma.c b/drivers/infiniband/core/ucma.c
index 90d675ad9ec8..15937eb38aae 100644
--- a/drivers/infiniband/core/ucma.c
+++ b/drivers/infiniband/core/ucma.c
@@ -31,6 +31,7 @@
31 */ 31 */
32 32
33#include <linux/completion.h> 33#include <linux/completion.h>
34#include <linux/file.h>
34#include <linux/mutex.h> 35#include <linux/mutex.h>
35#include <linux/poll.h> 36#include <linux/poll.h>
36#include <linux/idr.h> 37#include <linux/idr.h>
@@ -991,6 +992,96 @@ out:
991 return ret; 992 return ret;
992} 993}
993 994
995static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
996{
997 /* Acquire mutex's based on pointer comparison to prevent deadlock. */
998 if (file1 < file2) {
999 mutex_lock(&file1->mut);
1000 mutex_lock(&file2->mut);
1001 } else {
1002 mutex_lock(&file2->mut);
1003 mutex_lock(&file1->mut);
1004 }
1005}
1006
1007static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
1008{
1009 if (file1 < file2) {
1010 mutex_unlock(&file2->mut);
1011 mutex_unlock(&file1->mut);
1012 } else {
1013 mutex_unlock(&file1->mut);
1014 mutex_unlock(&file2->mut);
1015 }
1016}
1017
1018static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
1019{
1020 struct ucma_event *uevent, *tmp;
1021
1022 list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
1023 if (uevent->ctx == ctx)
1024 list_move_tail(&uevent->list, &file->event_list);
1025}
1026
1027static ssize_t ucma_migrate_id(struct ucma_file *new_file,
1028 const char __user *inbuf,
1029 int in_len, int out_len)
1030{
1031 struct rdma_ucm_migrate_id cmd;
1032 struct rdma_ucm_migrate_resp resp;
1033 struct ucma_context *ctx;
1034 struct file *filp;
1035 struct ucma_file *cur_file;
1036 int ret = 0;
1037
1038 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1039 return -EFAULT;
1040
1041 /* Get current fd to protect against it being closed */
1042 filp = fget(cmd.fd);
1043 if (!filp)
1044 return -ENOENT;
1045
1046 /* Validate current fd and prevent destruction of id. */
1047 ctx = ucma_get_ctx(filp->private_data, cmd.id);
1048 if (IS_ERR(ctx)) {
1049 ret = PTR_ERR(ctx);
1050 goto file_put;
1051 }
1052
1053 cur_file = ctx->file;
1054 if (cur_file == new_file) {
1055 resp.events_reported = ctx->events_reported;
1056 goto response;
1057 }
1058
1059 /*
1060 * Migrate events between fd's, maintaining order, and avoiding new
1061 * events being added before existing events.
1062 */
1063 ucma_lock_files(cur_file, new_file);
1064 mutex_lock(&mut);
1065
1066 list_move_tail(&ctx->list, &new_file->ctx_list);
1067 ucma_move_events(ctx, new_file);
1068 ctx->file = new_file;
1069 resp.events_reported = ctx->events_reported;
1070
1071 mutex_unlock(&mut);
1072 ucma_unlock_files(cur_file, new_file);
1073
1074response:
1075 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1076 &resp, sizeof(resp)))
1077 ret = -EFAULT;
1078
1079 ucma_put_ctx(ctx);
1080file_put:
1081 fput(filp);
1082 return ret;
1083}
1084
994static ssize_t (*ucma_cmd_table[])(struct ucma_file *file, 1085static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
995 const char __user *inbuf, 1086 const char __user *inbuf,
996 int in_len, int out_len) = { 1087 int in_len, int out_len) = {
@@ -1012,6 +1103,7 @@ static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
1012 [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify, 1103 [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
1013 [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast, 1104 [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast,
1014 [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast, 1105 [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
1106 [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id
1015}; 1107};
1016 1108
1017static ssize_t ucma_write(struct file *filp, const char __user *buf, 1109static ssize_t ucma_write(struct file *filp, const char __user *buf,
diff --git a/drivers/infiniband/core/user_mad.c b/drivers/infiniband/core/user_mad.c
index b53eac4611de..4e915104ac4c 100644
--- a/drivers/infiniband/core/user_mad.c
+++ b/drivers/infiniband/core/user_mad.c
@@ -2,6 +2,7 @@
2 * Copyright (c) 2004 Topspin Communications. All rights reserved. 2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Voltaire, Inc. All rights reserved. 3 * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5 * Copyright (c) 2008 Cisco. All rights reserved.
5 * 6 *
6 * This software is available to you under a choice of one of two 7 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU 8 * licenses. You may choose to be licensed under the terms of the GNU
@@ -42,7 +43,7 @@
42#include <linux/cdev.h> 43#include <linux/cdev.h>
43#include <linux/dma-mapping.h> 44#include <linux/dma-mapping.h>
44#include <linux/poll.h> 45#include <linux/poll.h>
45#include <linux/rwsem.h> 46#include <linux/mutex.h>
46#include <linux/kref.h> 47#include <linux/kref.h>
47#include <linux/compat.h> 48#include <linux/compat.h>
48 49
@@ -94,7 +95,7 @@ struct ib_umad_port {
94 struct class_device *sm_class_dev; 95 struct class_device *sm_class_dev;
95 struct semaphore sm_sem; 96 struct semaphore sm_sem;
96 97
97 struct rw_semaphore mutex; 98 struct mutex file_mutex;
98 struct list_head file_list; 99 struct list_head file_list;
99 100
100 struct ib_device *ib_dev; 101 struct ib_device *ib_dev;
@@ -110,11 +111,11 @@ struct ib_umad_device {
110}; 111};
111 112
112struct ib_umad_file { 113struct ib_umad_file {
114 struct mutex mutex;
113 struct ib_umad_port *port; 115 struct ib_umad_port *port;
114 struct list_head recv_list; 116 struct list_head recv_list;
115 struct list_head send_list; 117 struct list_head send_list;
116 struct list_head port_list; 118 struct list_head port_list;
117 spinlock_t recv_lock;
118 spinlock_t send_lock; 119 spinlock_t send_lock;
119 wait_queue_head_t recv_wait; 120 wait_queue_head_t recv_wait;
120 struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS]; 121 struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
@@ -156,7 +157,7 @@ static int hdr_size(struct ib_umad_file *file)
156 sizeof (struct ib_user_mad_hdr_old); 157 sizeof (struct ib_user_mad_hdr_old);
157} 158}
158 159
159/* caller must hold port->mutex at least for reading */ 160/* caller must hold file->mutex */
160static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id) 161static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
161{ 162{
162 return file->agents_dead ? NULL : file->agent[id]; 163 return file->agents_dead ? NULL : file->agent[id];
@@ -168,32 +169,30 @@ static int queue_packet(struct ib_umad_file *file,
168{ 169{
169 int ret = 1; 170 int ret = 1;
170 171
171 down_read(&file->port->mutex); 172 mutex_lock(&file->mutex);
172 173
173 for (packet->mad.hdr.id = 0; 174 for (packet->mad.hdr.id = 0;
174 packet->mad.hdr.id < IB_UMAD_MAX_AGENTS; 175 packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
175 packet->mad.hdr.id++) 176 packet->mad.hdr.id++)
176 if (agent == __get_agent(file, packet->mad.hdr.id)) { 177 if (agent == __get_agent(file, packet->mad.hdr.id)) {
177 spin_lock_irq(&file->recv_lock);
178 list_add_tail(&packet->list, &file->recv_list); 178 list_add_tail(&packet->list, &file->recv_list);
179 spin_unlock_irq(&file->recv_lock);
180 wake_up_interruptible(&file->recv_wait); 179 wake_up_interruptible(&file->recv_wait);
181 ret = 0; 180 ret = 0;
182 break; 181 break;
183 } 182 }
184 183
185 up_read(&file->port->mutex); 184 mutex_unlock(&file->mutex);
186 185
187 return ret; 186 return ret;
188} 187}
189 188
190static void dequeue_send(struct ib_umad_file *file, 189static void dequeue_send(struct ib_umad_file *file,
191 struct ib_umad_packet *packet) 190 struct ib_umad_packet *packet)
192 { 191{
193 spin_lock_irq(&file->send_lock); 192 spin_lock_irq(&file->send_lock);
194 list_del(&packet->list); 193 list_del(&packet->list);
195 spin_unlock_irq(&file->send_lock); 194 spin_unlock_irq(&file->send_lock);
196 } 195}
197 196
198static void send_handler(struct ib_mad_agent *agent, 197static void send_handler(struct ib_mad_agent *agent,
199 struct ib_mad_send_wc *send_wc) 198 struct ib_mad_send_wc *send_wc)
@@ -341,10 +340,10 @@ static ssize_t ib_umad_read(struct file *filp, char __user *buf,
341 if (count < hdr_size(file)) 340 if (count < hdr_size(file))
342 return -EINVAL; 341 return -EINVAL;
343 342
344 spin_lock_irq(&file->recv_lock); 343 mutex_lock(&file->mutex);
345 344
346 while (list_empty(&file->recv_list)) { 345 while (list_empty(&file->recv_list)) {
347 spin_unlock_irq(&file->recv_lock); 346 mutex_unlock(&file->mutex);
348 347
349 if (filp->f_flags & O_NONBLOCK) 348 if (filp->f_flags & O_NONBLOCK)
350 return -EAGAIN; 349 return -EAGAIN;
@@ -353,13 +352,13 @@ static ssize_t ib_umad_read(struct file *filp, char __user *buf,
353 !list_empty(&file->recv_list))) 352 !list_empty(&file->recv_list)))
354 return -ERESTARTSYS; 353 return -ERESTARTSYS;
355 354
356 spin_lock_irq(&file->recv_lock); 355 mutex_lock(&file->mutex);
357 } 356 }
358 357
359 packet = list_entry(file->recv_list.next, struct ib_umad_packet, list); 358 packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
360 list_del(&packet->list); 359 list_del(&packet->list);
361 360
362 spin_unlock_irq(&file->recv_lock); 361 mutex_unlock(&file->mutex);
363 362
364 if (packet->recv_wc) 363 if (packet->recv_wc)
365 ret = copy_recv_mad(file, buf, packet, count); 364 ret = copy_recv_mad(file, buf, packet, count);
@@ -368,9 +367,9 @@ static ssize_t ib_umad_read(struct file *filp, char __user *buf,
368 367
369 if (ret < 0) { 368 if (ret < 0) {
370 /* Requeue packet */ 369 /* Requeue packet */
371 spin_lock_irq(&file->recv_lock); 370 mutex_lock(&file->mutex);
372 list_add(&packet->list, &file->recv_list); 371 list_add(&packet->list, &file->recv_list);
373 spin_unlock_irq(&file->recv_lock); 372 mutex_unlock(&file->mutex);
374 } else { 373 } else {
375 if (packet->recv_wc) 374 if (packet->recv_wc)
376 ib_free_recv_mad(packet->recv_wc); 375 ib_free_recv_mad(packet->recv_wc);
@@ -481,7 +480,7 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
481 goto err; 480 goto err;
482 } 481 }
483 482
484 down_read(&file->port->mutex); 483 mutex_lock(&file->mutex);
485 484
486 agent = __get_agent(file, packet->mad.hdr.id); 485 agent = __get_agent(file, packet->mad.hdr.id);
487 if (!agent) { 486 if (!agent) {
@@ -577,7 +576,7 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
577 if (ret) 576 if (ret)
578 goto err_send; 577 goto err_send;
579 578
580 up_read(&file->port->mutex); 579 mutex_unlock(&file->mutex);
581 return count; 580 return count;
582 581
583err_send: 582err_send:
@@ -587,7 +586,7 @@ err_msg:
587err_ah: 586err_ah:
588 ib_destroy_ah(ah); 587 ib_destroy_ah(ah);
589err_up: 588err_up:
590 up_read(&file->port->mutex); 589 mutex_unlock(&file->mutex);
591err: 590err:
592 kfree(packet); 591 kfree(packet);
593 return ret; 592 return ret;
@@ -613,11 +612,12 @@ static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
613{ 612{
614 struct ib_user_mad_reg_req ureq; 613 struct ib_user_mad_reg_req ureq;
615 struct ib_mad_reg_req req; 614 struct ib_mad_reg_req req;
616 struct ib_mad_agent *agent; 615 struct ib_mad_agent *agent = NULL;
617 int agent_id; 616 int agent_id;
618 int ret; 617 int ret;
619 618
620 down_write(&file->port->mutex); 619 mutex_lock(&file->port->file_mutex);
620 mutex_lock(&file->mutex);
621 621
622 if (!file->port->ib_dev) { 622 if (!file->port->ib_dev) {
623 ret = -EPIPE; 623 ret = -EPIPE;
@@ -666,13 +666,13 @@ found:
666 send_handler, recv_handler, file); 666 send_handler, recv_handler, file);
667 if (IS_ERR(agent)) { 667 if (IS_ERR(agent)) {
668 ret = PTR_ERR(agent); 668 ret = PTR_ERR(agent);
669 agent = NULL;
669 goto out; 670 goto out;
670 } 671 }
671 672
672 if (put_user(agent_id, 673 if (put_user(agent_id,
673 (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) { 674 (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
674 ret = -EFAULT; 675 ret = -EFAULT;
675 ib_unregister_mad_agent(agent);
676 goto out; 676 goto out;
677 } 677 }
678 678
@@ -690,7 +690,13 @@ found:
690 ret = 0; 690 ret = 0;
691 691
692out: 692out:
693 up_write(&file->port->mutex); 693 mutex_unlock(&file->mutex);
694
695 if (ret && agent)
696 ib_unregister_mad_agent(agent);
697
698 mutex_unlock(&file->port->file_mutex);
699
694 return ret; 700 return ret;
695} 701}
696 702
@@ -703,7 +709,8 @@ static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
703 if (get_user(id, arg)) 709 if (get_user(id, arg))
704 return -EFAULT; 710 return -EFAULT;
705 711
706 down_write(&file->port->mutex); 712 mutex_lock(&file->port->file_mutex);
713 mutex_lock(&file->mutex);
707 714
708 if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) { 715 if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
709 ret = -EINVAL; 716 ret = -EINVAL;
@@ -714,11 +721,13 @@ static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
714 file->agent[id] = NULL; 721 file->agent[id] = NULL;
715 722
716out: 723out:
717 up_write(&file->port->mutex); 724 mutex_unlock(&file->mutex);
718 725
719 if (agent) 726 if (agent)
720 ib_unregister_mad_agent(agent); 727 ib_unregister_mad_agent(agent);
721 728
729 mutex_unlock(&file->port->file_mutex);
730
722 return ret; 731 return ret;
723} 732}
724 733
@@ -726,12 +735,12 @@ static long ib_umad_enable_pkey(struct ib_umad_file *file)
726{ 735{
727 int ret = 0; 736 int ret = 0;
728 737
729 down_write(&file->port->mutex); 738 mutex_lock(&file->mutex);
730 if (file->already_used) 739 if (file->already_used)
731 ret = -EINVAL; 740 ret = -EINVAL;
732 else 741 else
733 file->use_pkey_index = 1; 742 file->use_pkey_index = 1;
734 up_write(&file->port->mutex); 743 mutex_unlock(&file->mutex);
735 744
736 return ret; 745 return ret;
737} 746}
@@ -783,7 +792,7 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
783 if (!port) 792 if (!port)
784 return -ENXIO; 793 return -ENXIO;
785 794
786 down_write(&port->mutex); 795 mutex_lock(&port->file_mutex);
787 796
788 if (!port->ib_dev) { 797 if (!port->ib_dev) {
789 ret = -ENXIO; 798 ret = -ENXIO;
@@ -797,7 +806,7 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
797 goto out; 806 goto out;
798 } 807 }
799 808
800 spin_lock_init(&file->recv_lock); 809 mutex_init(&file->mutex);
801 spin_lock_init(&file->send_lock); 810 spin_lock_init(&file->send_lock);
802 INIT_LIST_HEAD(&file->recv_list); 811 INIT_LIST_HEAD(&file->recv_list);
803 INIT_LIST_HEAD(&file->send_list); 812 INIT_LIST_HEAD(&file->send_list);
@@ -809,7 +818,7 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
809 list_add_tail(&file->port_list, &port->file_list); 818 list_add_tail(&file->port_list, &port->file_list);
810 819
811out: 820out:
812 up_write(&port->mutex); 821 mutex_unlock(&port->file_mutex);
813 return ret; 822 return ret;
814} 823}
815 824
@@ -821,7 +830,8 @@ static int ib_umad_close(struct inode *inode, struct file *filp)
821 int already_dead; 830 int already_dead;
822 int i; 831 int i;
823 832
824 down_write(&file->port->mutex); 833 mutex_lock(&file->port->file_mutex);
834 mutex_lock(&file->mutex);
825 835
826 already_dead = file->agents_dead; 836 already_dead = file->agents_dead;
827 file->agents_dead = 1; 837 file->agents_dead = 1;
@@ -834,14 +844,14 @@ static int ib_umad_close(struct inode *inode, struct file *filp)
834 844
835 list_del(&file->port_list); 845 list_del(&file->port_list);
836 846
837 downgrade_write(&file->port->mutex); 847 mutex_unlock(&file->mutex);
838 848
839 if (!already_dead) 849 if (!already_dead)
840 for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i) 850 for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
841 if (file->agent[i]) 851 if (file->agent[i])
842 ib_unregister_mad_agent(file->agent[i]); 852 ib_unregister_mad_agent(file->agent[i]);
843 853
844 up_read(&file->port->mutex); 854 mutex_unlock(&file->port->file_mutex);
845 855
846 kfree(file); 856 kfree(file);
847 kref_put(&dev->ref, ib_umad_release_dev); 857 kref_put(&dev->ref, ib_umad_release_dev);
@@ -914,10 +924,10 @@ static int ib_umad_sm_close(struct inode *inode, struct file *filp)
914 }; 924 };
915 int ret = 0; 925 int ret = 0;
916 926
917 down_write(&port->mutex); 927 mutex_lock(&port->file_mutex);
918 if (port->ib_dev) 928 if (port->ib_dev)
919 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); 929 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
920 up_write(&port->mutex); 930 mutex_unlock(&port->file_mutex);
921 931
922 up(&port->sm_sem); 932 up(&port->sm_sem);
923 933
@@ -981,7 +991,7 @@ static int ib_umad_init_port(struct ib_device *device, int port_num,
981 port->ib_dev = device; 991 port->ib_dev = device;
982 port->port_num = port_num; 992 port->port_num = port_num;
983 init_MUTEX(&port->sm_sem); 993 init_MUTEX(&port->sm_sem);
984 init_rwsem(&port->mutex); 994 mutex_init(&port->file_mutex);
985 INIT_LIST_HEAD(&port->file_list); 995 INIT_LIST_HEAD(&port->file_list);
986 996
987 port->dev = cdev_alloc(); 997 port->dev = cdev_alloc();
@@ -1052,6 +1062,7 @@ err_cdev:
1052static void ib_umad_kill_port(struct ib_umad_port *port) 1062static void ib_umad_kill_port(struct ib_umad_port *port)
1053{ 1063{
1054 struct ib_umad_file *file; 1064 struct ib_umad_file *file;
1065 int already_dead;
1055 int id; 1066 int id;
1056 1067
1057 class_set_devdata(port->class_dev, NULL); 1068 class_set_devdata(port->class_dev, NULL);
@@ -1067,42 +1078,22 @@ static void ib_umad_kill_port(struct ib_umad_port *port)
1067 umad_port[port->dev_num] = NULL; 1078 umad_port[port->dev_num] = NULL;
1068 spin_unlock(&port_lock); 1079 spin_unlock(&port_lock);
1069 1080
1070 down_write(&port->mutex); 1081 mutex_lock(&port->file_mutex);
1071 1082
1072 port->ib_dev = NULL; 1083 port->ib_dev = NULL;
1073 1084
1074 /* 1085 list_for_each_entry(file, &port->file_list, port_list) {
1075 * Now go through the list of files attached to this port and 1086 mutex_lock(&file->mutex);
1076 * unregister all of their MAD agents. We need to hold 1087 already_dead = file->agents_dead;
1077 * port->mutex while doing this to avoid racing with
1078 * ib_umad_close(), but we can't hold the mutex for writing
1079 * while calling ib_unregister_mad_agent(), since that might
1080 * deadlock by calling back into queue_packet(). So we
1081 * downgrade our lock to a read lock, and then drop and
1082 * reacquire the write lock for the next iteration.
1083 *
1084 * We do list_del_init() on the file's list_head so that the
1085 * list_del in ib_umad_close() is still OK, even after the
1086 * file is removed from the list.
1087 */
1088 while (!list_empty(&port->file_list)) {
1089 file = list_entry(port->file_list.next, struct ib_umad_file,
1090 port_list);
1091
1092 file->agents_dead = 1; 1088 file->agents_dead = 1;
1093 list_del_init(&file->port_list); 1089 mutex_unlock(&file->mutex);
1094
1095 downgrade_write(&port->mutex);
1096 1090
1097 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id) 1091 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
1098 if (file->agent[id]) 1092 if (file->agent[id])
1099 ib_unregister_mad_agent(file->agent[id]); 1093 ib_unregister_mad_agent(file->agent[id]);
1100
1101 up_read(&port->mutex);
1102 down_write(&port->mutex);
1103 } 1094 }
1104 1095
1105 up_write(&port->mutex); 1096 mutex_unlock(&port->file_mutex);
1106 1097
1107 clear_bit(port->dev_num, dev_map); 1098 clear_bit(port->dev_num, dev_map);
1108} 1099}
diff --git a/drivers/infiniband/hw/cxgb3/cxio_hal.c b/drivers/infiniband/hw/cxgb3/cxio_hal.c
index eec6a30840ca..03c5ff62889a 100644
--- a/drivers/infiniband/hw/cxgb3/cxio_hal.c
+++ b/drivers/infiniband/hw/cxgb3/cxio_hal.c
@@ -179,7 +179,7 @@ int cxio_create_cq(struct cxio_rdev *rdev_p, struct t3_cq *cq)
179 setup.size = 1UL << cq->size_log2; 179 setup.size = 1UL << cq->size_log2;
180 setup.credits = 65535; 180 setup.credits = 65535;
181 setup.credit_thres = 1; 181 setup.credit_thres = 1;
182 if (rdev_p->t3cdev_p->type == T3B) 182 if (rdev_p->t3cdev_p->type != T3A)
183 setup.ovfl_mode = 0; 183 setup.ovfl_mode = 0;
184 else 184 else
185 setup.ovfl_mode = 1; 185 setup.ovfl_mode = 1;
@@ -584,7 +584,7 @@ static int cxio_hal_ctrl_qp_write_mem(struct cxio_rdev *rdev_p, u32 addr,
584{ 584{
585 u32 i, nr_wqe, copy_len; 585 u32 i, nr_wqe, copy_len;
586 u8 *copy_data; 586 u8 *copy_data;
587 u8 wr_len, utx_len; /* lenght in 8 byte flit */ 587 u8 wr_len, utx_len; /* length in 8 byte flit */
588 enum t3_wr_flags flag; 588 enum t3_wr_flags flag;
589 __be64 *wqe; 589 __be64 *wqe;
590 u64 utx_cmd; 590 u64 utx_cmd;
diff --git a/drivers/infiniband/hw/cxgb3/cxio_wr.h b/drivers/infiniband/hw/cxgb3/cxio_wr.h
index c84d4ac49355..969d4d928455 100644
--- a/drivers/infiniband/hw/cxgb3/cxio_wr.h
+++ b/drivers/infiniband/hw/cxgb3/cxio_wr.h
@@ -315,7 +315,7 @@ struct t3_rdma_init_wr {
315 __be32 ird; 315 __be32 ird;
316 __be64 qp_dma_addr; /* 7 */ 316 __be64 qp_dma_addr; /* 7 */
317 __be32 qp_dma_size; /* 8 */ 317 __be32 qp_dma_size; /* 8 */
318 u32 irs; 318 __be32 irs;
319}; 319};
320 320
321struct t3_genbit { 321struct t3_genbit {
@@ -324,7 +324,8 @@ struct t3_genbit {
324}; 324};
325 325
326enum rdma_init_wr_flags { 326enum rdma_init_wr_flags {
327 RECVS_POSTED = 1, 327 RECVS_POSTED = (1<<0),
328 PRIV_QP = (1<<1),
328}; 329};
329 330
330union t3_wr { 331union t3_wr {
diff --git a/drivers/infiniband/hw/cxgb3/iwch_cm.c b/drivers/infiniband/hw/cxgb3/iwch_cm.c
index 20ba372dd182..f8cb0fe748c3 100644
--- a/drivers/infiniband/hw/cxgb3/iwch_cm.c
+++ b/drivers/infiniband/hw/cxgb3/iwch_cm.c
@@ -1118,7 +1118,7 @@ static int act_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1118 status2errno(rpl->status)); 1118 status2errno(rpl->status));
1119 connect_reply_upcall(ep, status2errno(rpl->status)); 1119 connect_reply_upcall(ep, status2errno(rpl->status));
1120 state_set(&ep->com, DEAD); 1120 state_set(&ep->com, DEAD);
1121 if (ep->com.tdev->type == T3B && act_open_has_tid(rpl->status)) 1121 if (ep->com.tdev->type != T3A && act_open_has_tid(rpl->status))
1122 release_tid(ep->com.tdev, GET_TID(rpl), NULL); 1122 release_tid(ep->com.tdev, GET_TID(rpl), NULL);
1123 cxgb3_free_atid(ep->com.tdev, ep->atid); 1123 cxgb3_free_atid(ep->com.tdev, ep->atid);
1124 dst_release(ep->dst); 1124 dst_release(ep->dst);
@@ -1249,7 +1249,7 @@ static void reject_cr(struct t3cdev *tdev, u32 hwtid, __be32 peer_ip,
1249 skb_trim(skb, sizeof(struct cpl_tid_release)); 1249 skb_trim(skb, sizeof(struct cpl_tid_release));
1250 skb_get(skb); 1250 skb_get(skb);
1251 1251
1252 if (tdev->type == T3B) 1252 if (tdev->type != T3A)
1253 release_tid(tdev, hwtid, skb); 1253 release_tid(tdev, hwtid, skb);
1254 else { 1254 else {
1255 struct cpl_pass_accept_rpl *rpl; 1255 struct cpl_pass_accept_rpl *rpl;
diff --git a/drivers/infiniband/hw/cxgb3/iwch_mem.c b/drivers/infiniband/hw/cxgb3/iwch_mem.c
index a6c2c4ba29e6..73bfd1656f86 100644
--- a/drivers/infiniband/hw/cxgb3/iwch_mem.c
+++ b/drivers/infiniband/hw/cxgb3/iwch_mem.c
@@ -122,6 +122,13 @@ int build_phys_page_list(struct ib_phys_buf *buffer_list,
122 *total_size += buffer_list[i].size; 122 *total_size += buffer_list[i].size;
123 if (i > 0) 123 if (i > 0)
124 mask |= buffer_list[i].addr; 124 mask |= buffer_list[i].addr;
125 else
126 mask |= buffer_list[i].addr & PAGE_MASK;
127 if (i != num_phys_buf - 1)
128 mask |= buffer_list[i].addr + buffer_list[i].size;
129 else
130 mask |= (buffer_list[i].addr + buffer_list[i].size +
131 PAGE_SIZE - 1) & PAGE_MASK;
125 } 132 }
126 133
127 if (*total_size > 0xFFFFFFFFULL) 134 if (*total_size > 0xFFFFFFFFULL)
diff --git a/drivers/infiniband/hw/cxgb3/iwch_provider.c b/drivers/infiniband/hw/cxgb3/iwch_provider.c
index b5436ca92e68..df1838f8f94d 100644
--- a/drivers/infiniband/hw/cxgb3/iwch_provider.c
+++ b/drivers/infiniband/hw/cxgb3/iwch_provider.c
@@ -39,6 +39,7 @@
39#include <linux/list.h> 39#include <linux/list.h>
40#include <linux/spinlock.h> 40#include <linux/spinlock.h>
41#include <linux/ethtool.h> 41#include <linux/ethtool.h>
42#include <linux/rtnetlink.h>
42 43
43#include <asm/io.h> 44#include <asm/io.h>
44#include <asm/irq.h> 45#include <asm/irq.h>
@@ -645,7 +646,7 @@ static struct ib_mr *iwch_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
645 if (err) 646 if (err)
646 goto err; 647 goto err;
647 648
648 if (udata && t3b_device(rhp)) { 649 if (udata && !t3a_device(rhp)) {
649 uresp.pbl_addr = (mhp->attr.pbl_addr - 650 uresp.pbl_addr = (mhp->attr.pbl_addr -
650 rhp->rdev.rnic_info.pbl_base) >> 3; 651 rhp->rdev.rnic_info.pbl_base) >> 3;
651 PDBG("%s user resp pbl_addr 0x%x\n", __FUNCTION__, 652 PDBG("%s user resp pbl_addr 0x%x\n", __FUNCTION__,
@@ -1053,7 +1054,9 @@ static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
1053 struct net_device *lldev = dev->rdev.t3cdev_p->lldev; 1054 struct net_device *lldev = dev->rdev.t3cdev_p->lldev;
1054 1055
1055 PDBG("%s class dev 0x%p\n", __FUNCTION__, cdev); 1056 PDBG("%s class dev 0x%p\n", __FUNCTION__, cdev);
1057 rtnl_lock();
1056 lldev->ethtool_ops->get_drvinfo(lldev, &info); 1058 lldev->ethtool_ops->get_drvinfo(lldev, &info);
1059 rtnl_unlock();
1057 return sprintf(buf, "%s\n", info.fw_version); 1060 return sprintf(buf, "%s\n", info.fw_version);
1058} 1061}
1059 1062
@@ -1065,7 +1068,9 @@ static ssize_t show_hca(struct class_device *cdev, char *buf)
1065 struct net_device *lldev = dev->rdev.t3cdev_p->lldev; 1068 struct net_device *lldev = dev->rdev.t3cdev_p->lldev;
1066 1069
1067 PDBG("%s class dev 0x%p\n", __FUNCTION__, cdev); 1070 PDBG("%s class dev 0x%p\n", __FUNCTION__, cdev);
1071 rtnl_lock();
1068 lldev->ethtool_ops->get_drvinfo(lldev, &info); 1072 lldev->ethtool_ops->get_drvinfo(lldev, &info);
1073 rtnl_unlock();
1069 return sprintf(buf, "%s\n", info.driver); 1074 return sprintf(buf, "%s\n", info.driver);
1070} 1075}
1071 1076
diff --git a/drivers/infiniband/hw/cxgb3/iwch_qp.c b/drivers/infiniband/hw/cxgb3/iwch_qp.c
index dd89b6b91f9c..ea2cdd73dd85 100644
--- a/drivers/infiniband/hw/cxgb3/iwch_qp.c
+++ b/drivers/infiniband/hw/cxgb3/iwch_qp.c
@@ -208,36 +208,19 @@ static int iwch_sgl2pbl_map(struct iwch_dev *rhp, struct ib_sge *sg_list,
208static int iwch_build_rdma_recv(struct iwch_dev *rhp, union t3_wr *wqe, 208static int iwch_build_rdma_recv(struct iwch_dev *rhp, union t3_wr *wqe,
209 struct ib_recv_wr *wr) 209 struct ib_recv_wr *wr)
210{ 210{
211 int i, err = 0; 211 int i;
212 u32 pbl_addr[4];
213 u8 page_size[4];
214 if (wr->num_sge > T3_MAX_SGE) 212 if (wr->num_sge > T3_MAX_SGE)
215 return -EINVAL; 213 return -EINVAL;
216 err = iwch_sgl2pbl_map(rhp, wr->sg_list, wr->num_sge, pbl_addr,
217 page_size);
218 if (err)
219 return err;
220 wqe->recv.pagesz[0] = page_size[0];
221 wqe->recv.pagesz[1] = page_size[1];
222 wqe->recv.pagesz[2] = page_size[2];
223 wqe->recv.pagesz[3] = page_size[3];
224 wqe->recv.num_sgle = cpu_to_be32(wr->num_sge); 214 wqe->recv.num_sgle = cpu_to_be32(wr->num_sge);
225 for (i = 0; i < wr->num_sge; i++) { 215 for (i = 0; i < wr->num_sge; i++) {
226 wqe->recv.sgl[i].stag = cpu_to_be32(wr->sg_list[i].lkey); 216 wqe->recv.sgl[i].stag = cpu_to_be32(wr->sg_list[i].lkey);
227 wqe->recv.sgl[i].len = cpu_to_be32(wr->sg_list[i].length); 217 wqe->recv.sgl[i].len = cpu_to_be32(wr->sg_list[i].length);
228 218 wqe->recv.sgl[i].to = cpu_to_be64(wr->sg_list[i].addr);
229 /* to in the WQE == the offset into the page */
230 wqe->recv.sgl[i].to = cpu_to_be64(((u32) wr->sg_list[i].addr) %
231 (1UL << (12 + page_size[i])));
232
233 /* pbl_addr is the adapters address in the PBL */
234 wqe->recv.pbl_addr[i] = cpu_to_be32(pbl_addr[i]);
235 } 219 }
236 for (; i < T3_MAX_SGE; i++) { 220 for (; i < T3_MAX_SGE; i++) {
237 wqe->recv.sgl[i].stag = 0; 221 wqe->recv.sgl[i].stag = 0;
238 wqe->recv.sgl[i].len = 0; 222 wqe->recv.sgl[i].len = 0;
239 wqe->recv.sgl[i].to = 0; 223 wqe->recv.sgl[i].to = 0;
240 wqe->recv.pbl_addr[i] = 0;
241 } 224 }
242 return 0; 225 return 0;
243} 226}
@@ -659,6 +642,7 @@ static void __flush_qp(struct iwch_qp *qhp, unsigned long *flag)
659 cxio_flush_rq(&qhp->wq, &rchp->cq, count); 642 cxio_flush_rq(&qhp->wq, &rchp->cq, count);
660 spin_unlock(&qhp->lock); 643 spin_unlock(&qhp->lock);
661 spin_unlock_irqrestore(&rchp->lock, *flag); 644 spin_unlock_irqrestore(&rchp->lock, *flag);
645 (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
662 646
663 /* locking heirarchy: cq lock first, then qp lock. */ 647 /* locking heirarchy: cq lock first, then qp lock. */
664 spin_lock_irqsave(&schp->lock, *flag); 648 spin_lock_irqsave(&schp->lock, *flag);
@@ -668,6 +652,7 @@ static void __flush_qp(struct iwch_qp *qhp, unsigned long *flag)
668 cxio_flush_sq(&qhp->wq, &schp->cq, count); 652 cxio_flush_sq(&qhp->wq, &schp->cq, count);
669 spin_unlock(&qhp->lock); 653 spin_unlock(&qhp->lock);
670 spin_unlock_irqrestore(&schp->lock, *flag); 654 spin_unlock_irqrestore(&schp->lock, *flag);
655 (*schp->ibcq.comp_handler)(&schp->ibcq, schp->ibcq.cq_context);
671 656
672 /* deref */ 657 /* deref */
673 if (atomic_dec_and_test(&qhp->refcnt)) 658 if (atomic_dec_and_test(&qhp->refcnt))
@@ -678,7 +663,7 @@ static void __flush_qp(struct iwch_qp *qhp, unsigned long *flag)
678 663
679static void flush_qp(struct iwch_qp *qhp, unsigned long *flag) 664static void flush_qp(struct iwch_qp *qhp, unsigned long *flag)
680{ 665{
681 if (t3b_device(qhp->rhp)) 666 if (qhp->ibqp.uobject)
682 cxio_set_wq_in_error(&qhp->wq); 667 cxio_set_wq_in_error(&qhp->wq);
683 else 668 else
684 __flush_qp(qhp, flag); 669 __flush_qp(qhp, flag);
@@ -732,6 +717,7 @@ static int rdma_init(struct iwch_dev *rhp, struct iwch_qp *qhp,
732 init_attr.qp_dma_addr = qhp->wq.dma_addr; 717 init_attr.qp_dma_addr = qhp->wq.dma_addr;
733 init_attr.qp_dma_size = (1UL << qhp->wq.size_log2); 718 init_attr.qp_dma_size = (1UL << qhp->wq.size_log2);
734 init_attr.flags = rqes_posted(qhp) ? RECVS_POSTED : 0; 719 init_attr.flags = rqes_posted(qhp) ? RECVS_POSTED : 0;
720 init_attr.flags |= capable(CAP_NET_BIND_SERVICE) ? PRIV_QP : 0;
735 init_attr.irs = qhp->ep->rcv_seq; 721 init_attr.irs = qhp->ep->rcv_seq;
736 PDBG("%s init_attr.rq_addr 0x%x init_attr.rq_size = %d " 722 PDBG("%s init_attr.rq_addr 0x%x init_attr.rq_size = %d "
737 "flags 0x%x qpcaps 0x%x\n", __FUNCTION__, 723 "flags 0x%x qpcaps 0x%x\n", __FUNCTION__,
@@ -847,10 +833,11 @@ int iwch_modify_qp(struct iwch_dev *rhp, struct iwch_qp *qhp,
847 disconnect = 1; 833 disconnect = 1;
848 ep = qhp->ep; 834 ep = qhp->ep;
849 } 835 }
836 flush_qp(qhp, &flag);
850 break; 837 break;
851 case IWCH_QP_STATE_TERMINATE: 838 case IWCH_QP_STATE_TERMINATE:
852 qhp->attr.state = IWCH_QP_STATE_TERMINATE; 839 qhp->attr.state = IWCH_QP_STATE_TERMINATE;
853 if (t3b_device(qhp->rhp)) 840 if (qhp->ibqp.uobject)
854 cxio_set_wq_in_error(&qhp->wq); 841 cxio_set_wq_in_error(&qhp->wq);
855 if (!internal) 842 if (!internal)
856 terminate = 1; 843 terminate = 1;
diff --git a/drivers/infiniband/hw/ehca/ehca_av.c b/drivers/infiniband/hw/ehca/ehca_av.c
index f7782c882ab4..194c1c30cf63 100644
--- a/drivers/infiniband/hw/ehca/ehca_av.c
+++ b/drivers/infiniband/hw/ehca/ehca_av.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * IBM eServer eHCA Infiniband device driver for Linux on POWER 2 * IBM eServer eHCA Infiniband device driver for Linux on POWER
3 * 3 *
4 * adress vector functions 4 * address vector functions
5 * 5 *
6 * Authors: Hoang-Nam Nguyen <hnguyen@de.ibm.com> 6 * Authors: Hoang-Nam Nguyen <hnguyen@de.ibm.com>
7 * Khadija Souissi <souissik@de.ibm.com> 7 * Khadija Souissi <souissik@de.ibm.com>
diff --git a/drivers/infiniband/hw/ehca/ehca_classes.h b/drivers/infiniband/hw/ehca/ehca_classes.h
index 74d2b72a11d8..f281d16040f5 100644
--- a/drivers/infiniband/hw/ehca/ehca_classes.h
+++ b/drivers/infiniband/hw/ehca/ehca_classes.h
@@ -94,7 +94,11 @@ struct ehca_sma_attr {
94 94
95struct ehca_sport { 95struct ehca_sport {
96 struct ib_cq *ibcq_aqp1; 96 struct ib_cq *ibcq_aqp1;
97 struct ib_qp *ibqp_aqp1; 97 struct ib_qp *ibqp_sqp[2];
98 /* lock to serialze modify_qp() calls for sqp in normal
99 * and irq path (when event PORT_ACTIVE is received first time)
100 */
101 spinlock_t mod_sqp_lock;
98 enum ib_port_state port_state; 102 enum ib_port_state port_state;
99 struct ehca_sma_attr saved_attr; 103 struct ehca_sma_attr saved_attr;
100}; 104};
@@ -141,6 +145,14 @@ enum ehca_ext_qp_type {
141 EQPT_SRQ = 3, 145 EQPT_SRQ = 3,
142}; 146};
143 147
148/* struct to cache modify_qp()'s parms for GSI/SMI qp */
149struct ehca_mod_qp_parm {
150 int mask;
151 struct ib_qp_attr attr;
152};
153
154#define EHCA_MOD_QP_PARM_MAX 4
155
144struct ehca_qp { 156struct ehca_qp {
145 union { 157 union {
146 struct ib_qp ib_qp; 158 struct ib_qp ib_qp;
@@ -164,10 +176,18 @@ struct ehca_qp {
164 struct ehca_cq *recv_cq; 176 struct ehca_cq *recv_cq;
165 unsigned int sqerr_purgeflag; 177 unsigned int sqerr_purgeflag;
166 struct hlist_node list_entries; 178 struct hlist_node list_entries;
179 /* array to cache modify_qp()'s parms for GSI/SMI qp */
180 struct ehca_mod_qp_parm *mod_qp_parm;
181 int mod_qp_parm_idx;
167 /* mmap counter for resources mapped into user space */ 182 /* mmap counter for resources mapped into user space */
168 u32 mm_count_squeue; 183 u32 mm_count_squeue;
169 u32 mm_count_rqueue; 184 u32 mm_count_rqueue;
170 u32 mm_count_galpa; 185 u32 mm_count_galpa;
186 /* unsolicited ack circumvention */
187 int unsol_ack_circ;
188 int mtu_shift;
189 u32 message_count;
190 u32 packet_count;
171}; 191};
172 192
173#define IS_SRQ(qp) (qp->ext_type == EQPT_SRQ) 193#define IS_SRQ(qp) (qp->ext_type == EQPT_SRQ)
@@ -323,6 +343,7 @@ extern int ehca_port_act_time;
323extern int ehca_use_hp_mr; 343extern int ehca_use_hp_mr;
324extern int ehca_scaling_code; 344extern int ehca_scaling_code;
325extern int ehca_lock_hcalls; 345extern int ehca_lock_hcalls;
346extern int ehca_nr_ports;
326 347
327struct ipzu_queue_resp { 348struct ipzu_queue_resp {
328 u32 qe_size; /* queue entry size */ 349 u32 qe_size; /* queue entry size */
diff --git a/drivers/infiniband/hw/ehca/ehca_cq.c b/drivers/infiniband/hw/ehca/ehca_cq.c
index 79c25f51c21e..0467c158d4a9 100644
--- a/drivers/infiniband/hw/ehca/ehca_cq.c
+++ b/drivers/infiniband/hw/ehca/ehca_cq.c
@@ -246,7 +246,7 @@ struct ib_cq *ehca_create_cq(struct ib_device *device, int cqe, int comp_vector,
246 } else { 246 } else {
247 if (h_ret != H_PAGE_REGISTERED) { 247 if (h_ret != H_PAGE_REGISTERED) {
248 ehca_err(device, "Registration of page failed " 248 ehca_err(device, "Registration of page failed "
249 "ehca_cq=%p cq_num=%x h_ret=%li" 249 "ehca_cq=%p cq_num=%x h_ret=%li "
250 "counter=%i act_pages=%i", 250 "counter=%i act_pages=%i",
251 my_cq, my_cq->cq_number, 251 my_cq, my_cq->cq_number,
252 h_ret, counter, param.act_pages); 252 h_ret, counter, param.act_pages);
diff --git a/drivers/infiniband/hw/ehca/ehca_irq.c b/drivers/infiniband/hw/ehca/ehca_irq.c
index 3f617b27b954..863b34fa9ff9 100644
--- a/drivers/infiniband/hw/ehca/ehca_irq.c
+++ b/drivers/infiniband/hw/ehca/ehca_irq.c
@@ -62,6 +62,7 @@
62#define NEQE_PORT_NUMBER EHCA_BMASK_IBM( 8, 15) 62#define NEQE_PORT_NUMBER EHCA_BMASK_IBM( 8, 15)
63#define NEQE_PORT_AVAILABILITY EHCA_BMASK_IBM(16, 16) 63#define NEQE_PORT_AVAILABILITY EHCA_BMASK_IBM(16, 16)
64#define NEQE_DISRUPTIVE EHCA_BMASK_IBM(16, 16) 64#define NEQE_DISRUPTIVE EHCA_BMASK_IBM(16, 16)
65#define NEQE_SPECIFIC_EVENT EHCA_BMASK_IBM(16, 23)
65 66
66#define ERROR_DATA_LENGTH EHCA_BMASK_IBM(52, 63) 67#define ERROR_DATA_LENGTH EHCA_BMASK_IBM(52, 63)
67#define ERROR_DATA_TYPE EHCA_BMASK_IBM( 0, 7) 68#define ERROR_DATA_TYPE EHCA_BMASK_IBM( 0, 7)
@@ -354,17 +355,34 @@ static void parse_ec(struct ehca_shca *shca, u64 eqe)
354{ 355{
355 u8 ec = EHCA_BMASK_GET(NEQE_EVENT_CODE, eqe); 356 u8 ec = EHCA_BMASK_GET(NEQE_EVENT_CODE, eqe);
356 u8 port = EHCA_BMASK_GET(NEQE_PORT_NUMBER, eqe); 357 u8 port = EHCA_BMASK_GET(NEQE_PORT_NUMBER, eqe);
358 u8 spec_event;
359 struct ehca_sport *sport = &shca->sport[port - 1];
360 unsigned long flags;
357 361
358 switch (ec) { 362 switch (ec) {
359 case 0x30: /* port availability change */ 363 case 0x30: /* port availability change */
360 if (EHCA_BMASK_GET(NEQE_PORT_AVAILABILITY, eqe)) { 364 if (EHCA_BMASK_GET(NEQE_PORT_AVAILABILITY, eqe)) {
361 shca->sport[port - 1].port_state = IB_PORT_ACTIVE; 365 int suppress_event;
366 /* replay modify_qp for sqps */
367 spin_lock_irqsave(&sport->mod_sqp_lock, flags);
368 suppress_event = !sport->ibqp_sqp[IB_QPT_GSI];
369 if (sport->ibqp_sqp[IB_QPT_SMI])
370 ehca_recover_sqp(sport->ibqp_sqp[IB_QPT_SMI]);
371 if (!suppress_event)
372 ehca_recover_sqp(sport->ibqp_sqp[IB_QPT_GSI]);
373 spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);
374
375 /* AQP1 was destroyed, ignore this event */
376 if (suppress_event)
377 break;
378
379 sport->port_state = IB_PORT_ACTIVE;
362 dispatch_port_event(shca, port, IB_EVENT_PORT_ACTIVE, 380 dispatch_port_event(shca, port, IB_EVENT_PORT_ACTIVE,
363 "is active"); 381 "is active");
364 ehca_query_sma_attr(shca, port, 382 ehca_query_sma_attr(shca, port,
365 &shca->sport[port - 1].saved_attr); 383 &sport->saved_attr);
366 } else { 384 } else {
367 shca->sport[port - 1].port_state = IB_PORT_DOWN; 385 sport->port_state = IB_PORT_DOWN;
368 dispatch_port_event(shca, port, IB_EVENT_PORT_ERR, 386 dispatch_port_event(shca, port, IB_EVENT_PORT_ERR,
369 "is inactive"); 387 "is inactive");
370 } 388 }
@@ -378,11 +396,11 @@ static void parse_ec(struct ehca_shca *shca, u64 eqe)
378 ehca_warn(&shca->ib_device, "disruptive port " 396 ehca_warn(&shca->ib_device, "disruptive port "
379 "%d configuration change", port); 397 "%d configuration change", port);
380 398
381 shca->sport[port - 1].port_state = IB_PORT_DOWN; 399 sport->port_state = IB_PORT_DOWN;
382 dispatch_port_event(shca, port, IB_EVENT_PORT_ERR, 400 dispatch_port_event(shca, port, IB_EVENT_PORT_ERR,
383 "is inactive"); 401 "is inactive");
384 402
385 shca->sport[port - 1].port_state = IB_PORT_ACTIVE; 403 sport->port_state = IB_PORT_ACTIVE;
386 dispatch_port_event(shca, port, IB_EVENT_PORT_ACTIVE, 404 dispatch_port_event(shca, port, IB_EVENT_PORT_ACTIVE,
387 "is active"); 405 "is active");
388 } else 406 } else
@@ -394,6 +412,16 @@ static void parse_ec(struct ehca_shca *shca, u64 eqe)
394 case 0x33: /* trace stopped */ 412 case 0x33: /* trace stopped */
395 ehca_err(&shca->ib_device, "Traced stopped."); 413 ehca_err(&shca->ib_device, "Traced stopped.");
396 break; 414 break;
415 case 0x34: /* util async event */
416 spec_event = EHCA_BMASK_GET(NEQE_SPECIFIC_EVENT, eqe);
417 if (spec_event == 0x80) /* client reregister required */
418 dispatch_port_event(shca, port,
419 IB_EVENT_CLIENT_REREGISTER,
420 "client reregister req.");
421 else
422 ehca_warn(&shca->ib_device, "Unknown util async "
423 "event %x on port %x", spec_event, port);
424 break;
397 default: 425 default:
398 ehca_err(&shca->ib_device, "Unknown event code: %x on %s.", 426 ehca_err(&shca->ib_device, "Unknown event code: %x on %s.",
399 ec, shca->ib_device.name); 427 ec, shca->ib_device.name);
diff --git a/drivers/infiniband/hw/ehca/ehca_iverbs.h b/drivers/infiniband/hw/ehca/ehca_iverbs.h
index 5485799cdc8d..c469bfde2708 100644
--- a/drivers/infiniband/hw/ehca/ehca_iverbs.h
+++ b/drivers/infiniband/hw/ehca/ehca_iverbs.h
@@ -200,4 +200,6 @@ void ehca_free_fw_ctrlblock(void *ptr);
200#define ehca_free_fw_ctrlblock(ptr) free_page((unsigned long)(ptr)) 200#define ehca_free_fw_ctrlblock(ptr) free_page((unsigned long)(ptr))
201#endif 201#endif
202 202
203void ehca_recover_sqp(struct ib_qp *sqp);
204
203#endif 205#endif
diff --git a/drivers/infiniband/hw/ehca/ehca_main.c b/drivers/infiniband/hw/ehca/ehca_main.c
index c9e32b46387f..84c9b7b8669b 100644
--- a/drivers/infiniband/hw/ehca/ehca_main.c
+++ b/drivers/infiniband/hw/ehca/ehca_main.c
@@ -90,7 +90,8 @@ MODULE_PARM_DESC(hw_level,
90 "hardware level" 90 "hardware level"
91 " (0: autosensing (default), 1: v. 0.20, 2: v. 0.21)"); 91 " (0: autosensing (default), 1: v. 0.20, 2: v. 0.21)");
92MODULE_PARM_DESC(nr_ports, 92MODULE_PARM_DESC(nr_ports,
93 "number of connected ports (default: 2)"); 93 "number of connected ports (-1: autodetect, 1: port one only, "
94 "2: two ports (default)");
94MODULE_PARM_DESC(use_hp_mr, 95MODULE_PARM_DESC(use_hp_mr,
95 "high performance MRs (0: no (default), 1: yes)"); 96 "high performance MRs (0: no (default), 1: yes)");
96MODULE_PARM_DESC(port_act_time, 97MODULE_PARM_DESC(port_act_time,
@@ -511,7 +512,7 @@ static int ehca_create_aqp1(struct ehca_shca *shca, u32 port)
511 } 512 }
512 sport->ibcq_aqp1 = ibcq; 513 sport->ibcq_aqp1 = ibcq;
513 514
514 if (sport->ibqp_aqp1) { 515 if (sport->ibqp_sqp[IB_QPT_GSI]) {
515 ehca_err(&shca->ib_device, "AQP1 QP is already created."); 516 ehca_err(&shca->ib_device, "AQP1 QP is already created.");
516 ret = -EPERM; 517 ret = -EPERM;
517 goto create_aqp1; 518 goto create_aqp1;
@@ -537,7 +538,7 @@ static int ehca_create_aqp1(struct ehca_shca *shca, u32 port)
537 ret = PTR_ERR(ibqp); 538 ret = PTR_ERR(ibqp);
538 goto create_aqp1; 539 goto create_aqp1;
539 } 540 }
540 sport->ibqp_aqp1 = ibqp; 541 sport->ibqp_sqp[IB_QPT_GSI] = ibqp;
541 542
542 return 0; 543 return 0;
543 544
@@ -550,7 +551,7 @@ static int ehca_destroy_aqp1(struct ehca_sport *sport)
550{ 551{
551 int ret; 552 int ret;
552 553
553 ret = ib_destroy_qp(sport->ibqp_aqp1); 554 ret = ib_destroy_qp(sport->ibqp_sqp[IB_QPT_GSI]);
554 if (ret) { 555 if (ret) {
555 ehca_gen_err("Cannot destroy AQP1 QP. ret=%i", ret); 556 ehca_gen_err("Cannot destroy AQP1 QP. ret=%i", ret);
556 return ret; 557 return ret;
@@ -693,7 +694,7 @@ static int __devinit ehca_probe(struct of_device *dev,
693 struct ehca_shca *shca; 694 struct ehca_shca *shca;
694 const u64 *handle; 695 const u64 *handle;
695 struct ib_pd *ibpd; 696 struct ib_pd *ibpd;
696 int ret; 697 int ret, i;
697 698
698 handle = of_get_property(dev->node, "ibm,hca-handle", NULL); 699 handle = of_get_property(dev->node, "ibm,hca-handle", NULL);
699 if (!handle) { 700 if (!handle) {
@@ -714,6 +715,8 @@ static int __devinit ehca_probe(struct of_device *dev,
714 return -ENOMEM; 715 return -ENOMEM;
715 } 716 }
716 mutex_init(&shca->modify_mutex); 717 mutex_init(&shca->modify_mutex);
718 for (i = 0; i < ARRAY_SIZE(shca->sport); i++)
719 spin_lock_init(&shca->sport[i].mod_sqp_lock);
717 720
718 shca->ofdev = dev; 721 shca->ofdev = dev;
719 shca->ipz_hca_handle.handle = *handle; 722 shca->ipz_hca_handle.handle = *handle;
@@ -934,7 +937,7 @@ void ehca_poll_eqs(unsigned long data)
934 ehca_process_eq(shca, 0); 937 ehca_process_eq(shca, 0);
935 } 938 }
936 } 939 }
937 mod_timer(&poll_eqs_timer, jiffies + HZ); 940 mod_timer(&poll_eqs_timer, round_jiffies(jiffies + HZ));
938 spin_unlock(&shca_list_lock); 941 spin_unlock(&shca_list_lock);
939} 942}
940 943
diff --git a/drivers/infiniband/hw/ehca/ehca_qp.c b/drivers/infiniband/hw/ehca/ehca_qp.c
index eff5fb55604b..1012f15a7140 100644
--- a/drivers/infiniband/hw/ehca/ehca_qp.c
+++ b/drivers/infiniband/hw/ehca/ehca_qp.c
@@ -592,10 +592,8 @@ static struct ehca_qp *internal_create_qp(
592 goto create_qp_exit1; 592 goto create_qp_exit1;
593 } 593 }
594 594
595 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) 595 /* Always signal by WQE so we can hide circ. WQEs */
596 parms.sigtype = HCALL_SIGT_EVERY; 596 parms.sigtype = HCALL_SIGT_BY_WQE;
597 else
598 parms.sigtype = HCALL_SIGT_BY_WQE;
599 597
600 /* UD_AV CIRCUMVENTION */ 598 /* UD_AV CIRCUMVENTION */
601 max_send_sge = init_attr->cap.max_send_sge; 599 max_send_sge = init_attr->cap.max_send_sge;
@@ -618,6 +616,10 @@ static struct ehca_qp *internal_create_qp(
618 parms.squeue.max_sge = max_send_sge; 616 parms.squeue.max_sge = max_send_sge;
619 parms.rqueue.max_sge = max_recv_sge; 617 parms.rqueue.max_sge = max_recv_sge;
620 618
619 /* RC QPs need one more SWQE for unsolicited ack circumvention */
620 if (qp_type == IB_QPT_RC)
621 parms.squeue.max_wr++;
622
621 if (EHCA_BMASK_GET(HCA_CAP_MINI_QP, shca->hca_cap)) { 623 if (EHCA_BMASK_GET(HCA_CAP_MINI_QP, shca->hca_cap)) {
622 if (HAS_SQ(my_qp)) 624 if (HAS_SQ(my_qp))
623 ehca_determine_small_queue( 625 ehca_determine_small_queue(
@@ -650,6 +652,8 @@ static struct ehca_qp *internal_create_qp(
650 parms.squeue.act_nr_sges = 1; 652 parms.squeue.act_nr_sges = 1;
651 parms.rqueue.act_nr_sges = 1; 653 parms.rqueue.act_nr_sges = 1;
652 } 654 }
655 /* hide the extra WQE */
656 parms.squeue.act_nr_wqes--;
653 break; 657 break;
654 case IB_QPT_UD: 658 case IB_QPT_UD:
655 case IB_QPT_GSI: 659 case IB_QPT_GSI:
@@ -729,12 +733,31 @@ static struct ehca_qp *internal_create_qp(
729 init_attr->cap.max_send_wr = parms.squeue.act_nr_wqes; 733 init_attr->cap.max_send_wr = parms.squeue.act_nr_wqes;
730 my_qp->init_attr = *init_attr; 734 my_qp->init_attr = *init_attr;
731 735
736 if (qp_type == IB_QPT_SMI || qp_type == IB_QPT_GSI) {
737 shca->sport[init_attr->port_num - 1].ibqp_sqp[qp_type] =
738 &my_qp->ib_qp;
739 if (ehca_nr_ports < 0) {
740 /* alloc array to cache subsequent modify qp parms
741 * for autodetect mode
742 */
743 my_qp->mod_qp_parm =
744 kzalloc(EHCA_MOD_QP_PARM_MAX *
745 sizeof(*my_qp->mod_qp_parm),
746 GFP_KERNEL);
747 if (!my_qp->mod_qp_parm) {
748 ehca_err(pd->device,
749 "Could not alloc mod_qp_parm");
750 goto create_qp_exit4;
751 }
752 }
753 }
754
732 /* NOTE: define_apq0() not supported yet */ 755 /* NOTE: define_apq0() not supported yet */
733 if (qp_type == IB_QPT_GSI) { 756 if (qp_type == IB_QPT_GSI) {
734 h_ret = ehca_define_sqp(shca, my_qp, init_attr); 757 h_ret = ehca_define_sqp(shca, my_qp, init_attr);
735 if (h_ret != H_SUCCESS) { 758 if (h_ret != H_SUCCESS) {
736 ret = ehca2ib_return_code(h_ret); 759 ret = ehca2ib_return_code(h_ret);
737 goto create_qp_exit4; 760 goto create_qp_exit5;
738 } 761 }
739 } 762 }
740 763
@@ -743,7 +766,7 @@ static struct ehca_qp *internal_create_qp(
743 if (ret) { 766 if (ret) {
744 ehca_err(pd->device, 767 ehca_err(pd->device,
745 "Couldn't assign qp to send_cq ret=%i", ret); 768 "Couldn't assign qp to send_cq ret=%i", ret);
746 goto create_qp_exit4; 769 goto create_qp_exit5;
747 } 770 }
748 } 771 }
749 772
@@ -769,12 +792,18 @@ static struct ehca_qp *internal_create_qp(
769 if (ib_copy_to_udata(udata, &resp, sizeof resp)) { 792 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
770 ehca_err(pd->device, "Copy to udata failed"); 793 ehca_err(pd->device, "Copy to udata failed");
771 ret = -EINVAL; 794 ret = -EINVAL;
772 goto create_qp_exit4; 795 goto create_qp_exit6;
773 } 796 }
774 } 797 }
775 798
776 return my_qp; 799 return my_qp;
777 800
801create_qp_exit6:
802 ehca_cq_unassign_qp(my_qp->send_cq, my_qp->real_qp_num);
803
804create_qp_exit5:
805 kfree(my_qp->mod_qp_parm);
806
778create_qp_exit4: 807create_qp_exit4:
779 if (HAS_RQ(my_qp)) 808 if (HAS_RQ(my_qp))
780 ipz_queue_dtor(my_pd, &my_qp->ipz_rqueue); 809 ipz_queue_dtor(my_pd, &my_qp->ipz_rqueue);
@@ -858,7 +887,7 @@ struct ib_srq *ehca_create_srq(struct ib_pd *pd,
858 update_mask, 887 update_mask,
859 mqpcb, my_qp->galpas.kernel); 888 mqpcb, my_qp->galpas.kernel);
860 if (hret != H_SUCCESS) { 889 if (hret != H_SUCCESS) {
861 ehca_err(pd->device, "Could not modify SRQ to INIT" 890 ehca_err(pd->device, "Could not modify SRQ to INIT "
862 "ehca_qp=%p qp_num=%x h_ret=%li", 891 "ehca_qp=%p qp_num=%x h_ret=%li",
863 my_qp, my_qp->real_qp_num, hret); 892 my_qp, my_qp->real_qp_num, hret);
864 goto create_srq2; 893 goto create_srq2;
@@ -872,7 +901,7 @@ struct ib_srq *ehca_create_srq(struct ib_pd *pd,
872 update_mask, 901 update_mask,
873 mqpcb, my_qp->galpas.kernel); 902 mqpcb, my_qp->galpas.kernel);
874 if (hret != H_SUCCESS) { 903 if (hret != H_SUCCESS) {
875 ehca_err(pd->device, "Could not enable SRQ" 904 ehca_err(pd->device, "Could not enable SRQ "
876 "ehca_qp=%p qp_num=%x h_ret=%li", 905 "ehca_qp=%p qp_num=%x h_ret=%li",
877 my_qp, my_qp->real_qp_num, hret); 906 my_qp, my_qp->real_qp_num, hret);
878 goto create_srq2; 907 goto create_srq2;
@@ -886,7 +915,7 @@ struct ib_srq *ehca_create_srq(struct ib_pd *pd,
886 update_mask, 915 update_mask,
887 mqpcb, my_qp->galpas.kernel); 916 mqpcb, my_qp->galpas.kernel);
888 if (hret != H_SUCCESS) { 917 if (hret != H_SUCCESS) {
889 ehca_err(pd->device, "Could not modify SRQ to RTR" 918 ehca_err(pd->device, "Could not modify SRQ to RTR "
890 "ehca_qp=%p qp_num=%x h_ret=%li", 919 "ehca_qp=%p qp_num=%x h_ret=%li",
891 my_qp, my_qp->real_qp_num, hret); 920 my_qp, my_qp->real_qp_num, hret);
892 goto create_srq2; 921 goto create_srq2;
@@ -992,7 +1021,7 @@ static int internal_modify_qp(struct ib_qp *ibqp,
992 unsigned long flags = 0; 1021 unsigned long flags = 0;
993 1022
994 /* do query_qp to obtain current attr values */ 1023 /* do query_qp to obtain current attr values */
995 mqpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL); 1024 mqpcb = ehca_alloc_fw_ctrlblock(GFP_ATOMIC);
996 if (!mqpcb) { 1025 if (!mqpcb) {
997 ehca_err(ibqp->device, "Could not get zeroed page for mqpcb " 1026 ehca_err(ibqp->device, "Could not get zeroed page for mqpcb "
998 "ehca_qp=%p qp_num=%x ", my_qp, ibqp->qp_num); 1027 "ehca_qp=%p qp_num=%x ", my_qp, ibqp->qp_num);
@@ -1180,6 +1209,8 @@ static int internal_modify_qp(struct ib_qp *ibqp,
1180 update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_P_KEY_IDX, 1); 1209 update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_P_KEY_IDX, 1);
1181 } 1210 }
1182 if (attr_mask & IB_QP_PORT) { 1211 if (attr_mask & IB_QP_PORT) {
1212 struct ehca_sport *sport;
1213 struct ehca_qp *aqp1;
1183 if (attr->port_num < 1 || attr->port_num > shca->num_ports) { 1214 if (attr->port_num < 1 || attr->port_num > shca->num_ports) {
1184 ret = -EINVAL; 1215 ret = -EINVAL;
1185 ehca_err(ibqp->device, "Invalid port=%x. " 1216 ehca_err(ibqp->device, "Invalid port=%x. "
@@ -1188,6 +1219,29 @@ static int internal_modify_qp(struct ib_qp *ibqp,
1188 shca->num_ports); 1219 shca->num_ports);
1189 goto modify_qp_exit2; 1220 goto modify_qp_exit2;
1190 } 1221 }
1222 sport = &shca->sport[attr->port_num - 1];
1223 if (!sport->ibqp_sqp[IB_QPT_GSI]) {
1224 /* should not occur */
1225 ret = -EFAULT;
1226 ehca_err(ibqp->device, "AQP1 was not created for "
1227 "port=%x", attr->port_num);
1228 goto modify_qp_exit2;
1229 }
1230 aqp1 = container_of(sport->ibqp_sqp[IB_QPT_GSI],
1231 struct ehca_qp, ib_qp);
1232 if (ibqp->qp_type != IB_QPT_GSI &&
1233 ibqp->qp_type != IB_QPT_SMI &&
1234 aqp1->mod_qp_parm) {
1235 /*
1236 * firmware will reject this modify_qp() because
1237 * port is not activated/initialized fully
1238 */
1239 ret = -EFAULT;
1240 ehca_warn(ibqp->device, "Couldn't modify qp port=%x: "
1241 "either port is being activated (try again) "
1242 "or cabling issue", attr->port_num);
1243 goto modify_qp_exit2;
1244 }
1191 mqpcb->prim_phys_port = attr->port_num; 1245 mqpcb->prim_phys_port = attr->port_num;
1192 update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_PHYS_PORT, 1); 1246 update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_PHYS_PORT, 1);
1193 } 1247 }
@@ -1244,6 +1298,8 @@ static int internal_modify_qp(struct ib_qp *ibqp,
1244 } 1298 }
1245 1299
1246 if (attr_mask & IB_QP_PATH_MTU) { 1300 if (attr_mask & IB_QP_PATH_MTU) {
1301 /* store ld(MTU) */
1302 my_qp->mtu_shift = attr->path_mtu + 7;
1247 mqpcb->path_mtu = attr->path_mtu; 1303 mqpcb->path_mtu = attr->path_mtu;
1248 update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PATH_MTU, 1); 1304 update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PATH_MTU, 1);
1249 } 1305 }
@@ -1467,6 +1523,8 @@ modify_qp_exit1:
1467int ehca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask, 1523int ehca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask,
1468 struct ib_udata *udata) 1524 struct ib_udata *udata)
1469{ 1525{
1526 struct ehca_shca *shca = container_of(ibqp->device, struct ehca_shca,
1527 ib_device);
1470 struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp); 1528 struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
1471 struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd, 1529 struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
1472 ib_pd); 1530 ib_pd);
@@ -1479,9 +1537,100 @@ int ehca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask,
1479 return -EINVAL; 1537 return -EINVAL;
1480 } 1538 }
1481 1539
1540 /* The if-block below caches qp_attr to be modified for GSI and SMI
1541 * qps during the initialization by ib_mad. When the respective port
1542 * is activated, ie we got an event PORT_ACTIVE, we'll replay the
1543 * cached modify calls sequence, see ehca_recover_sqs() below.
1544 * Why that is required:
1545 * 1) If one port is connected, older code requires that port one
1546 * to be connected and module option nr_ports=1 to be given by
1547 * user, which is very inconvenient for end user.
1548 * 2) Firmware accepts modify_qp() only if respective port has become
1549 * active. Older code had a wait loop of 30sec create_qp()/
1550 * define_aqp1(), which is not appropriate in practice. This
1551 * code now removes that wait loop, see define_aqp1(), and always
1552 * reports all ports to ib_mad resp. users. Only activated ports
1553 * will then usable for the users.
1554 */
1555 if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI) {
1556 int port = my_qp->init_attr.port_num;
1557 struct ehca_sport *sport = &shca->sport[port - 1];
1558 unsigned long flags;
1559 spin_lock_irqsave(&sport->mod_sqp_lock, flags);
1560 /* cache qp_attr only during init */
1561 if (my_qp->mod_qp_parm) {
1562 struct ehca_mod_qp_parm *p;
1563 if (my_qp->mod_qp_parm_idx >= EHCA_MOD_QP_PARM_MAX) {
1564 ehca_err(&shca->ib_device,
1565 "mod_qp_parm overflow state=%x port=%x"
1566 " type=%x", attr->qp_state,
1567 my_qp->init_attr.port_num,
1568 ibqp->qp_type);
1569 spin_unlock_irqrestore(&sport->mod_sqp_lock,
1570 flags);
1571 return -EINVAL;
1572 }
1573 p = &my_qp->mod_qp_parm[my_qp->mod_qp_parm_idx];
1574 p->mask = attr_mask;
1575 p->attr = *attr;
1576 my_qp->mod_qp_parm_idx++;
1577 ehca_dbg(&shca->ib_device,
1578 "Saved qp_attr for state=%x port=%x type=%x",
1579 attr->qp_state, my_qp->init_attr.port_num,
1580 ibqp->qp_type);
1581 spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);
1582 return 0;
1583 }
1584 spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);
1585 }
1586
1482 return internal_modify_qp(ibqp, attr, attr_mask, 0); 1587 return internal_modify_qp(ibqp, attr, attr_mask, 0);
1483} 1588}
1484 1589
1590void ehca_recover_sqp(struct ib_qp *sqp)
1591{
1592 struct ehca_qp *my_sqp = container_of(sqp, struct ehca_qp, ib_qp);
1593 int port = my_sqp->init_attr.port_num;
1594 struct ib_qp_attr attr;
1595 struct ehca_mod_qp_parm *qp_parm;
1596 int i, qp_parm_idx, ret;
1597 unsigned long flags, wr_cnt;
1598
1599 if (!my_sqp->mod_qp_parm)
1600 return;
1601 ehca_dbg(sqp->device, "SQP port=%x qp_num=%x", port, sqp->qp_num);
1602
1603 qp_parm = my_sqp->mod_qp_parm;
1604 qp_parm_idx = my_sqp->mod_qp_parm_idx;
1605 for (i = 0; i < qp_parm_idx; i++) {
1606 attr = qp_parm[i].attr;
1607 ret = internal_modify_qp(sqp, &attr, qp_parm[i].mask, 0);
1608 if (ret) {
1609 ehca_err(sqp->device, "Could not modify SQP port=%x "
1610 "qp_num=%x ret=%x", port, sqp->qp_num, ret);
1611 goto free_qp_parm;
1612 }
1613 ehca_dbg(sqp->device, "SQP port=%x qp_num=%x in state=%x",
1614 port, sqp->qp_num, attr.qp_state);
1615 }
1616
1617 /* re-trigger posted recv wrs */
1618 wr_cnt = my_sqp->ipz_rqueue.current_q_offset /
1619 my_sqp->ipz_rqueue.qe_size;
1620 if (wr_cnt) {
1621 spin_lock_irqsave(&my_sqp->spinlock_r, flags);
1622 hipz_update_rqa(my_sqp, wr_cnt);
1623 spin_unlock_irqrestore(&my_sqp->spinlock_r, flags);
1624 ehca_dbg(sqp->device, "doorbell port=%x qp_num=%x wr_cnt=%lx",
1625 port, sqp->qp_num, wr_cnt);
1626 }
1627
1628free_qp_parm:
1629 kfree(qp_parm);
1630 /* this prevents subsequent calls to modify_qp() to cache qp_attr */
1631 my_sqp->mod_qp_parm = NULL;
1632}
1633
1485int ehca_query_qp(struct ib_qp *qp, 1634int ehca_query_qp(struct ib_qp *qp,
1486 struct ib_qp_attr *qp_attr, 1635 struct ib_qp_attr *qp_attr,
1487 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr) 1636 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
@@ -1769,6 +1918,7 @@ static int internal_destroy_qp(struct ib_device *dev, struct ehca_qp *my_qp,
1769 struct ehca_shca *shca = container_of(dev, struct ehca_shca, ib_device); 1918 struct ehca_shca *shca = container_of(dev, struct ehca_shca, ib_device);
1770 struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd, 1919 struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
1771 ib_pd); 1920 ib_pd);
1921 struct ehca_sport *sport = &shca->sport[my_qp->init_attr.port_num - 1];
1772 u32 cur_pid = current->tgid; 1922 u32 cur_pid = current->tgid;
1773 u32 qp_num = my_qp->real_qp_num; 1923 u32 qp_num = my_qp->real_qp_num;
1774 int ret; 1924 int ret;
@@ -1815,6 +1965,14 @@ static int internal_destroy_qp(struct ib_device *dev, struct ehca_qp *my_qp,
1815 port_num = my_qp->init_attr.port_num; 1965 port_num = my_qp->init_attr.port_num;
1816 qp_type = my_qp->init_attr.qp_type; 1966 qp_type = my_qp->init_attr.qp_type;
1817 1967
1968 if (qp_type == IB_QPT_SMI || qp_type == IB_QPT_GSI) {
1969 spin_lock_irqsave(&sport->mod_sqp_lock, flags);
1970 kfree(my_qp->mod_qp_parm);
1971 my_qp->mod_qp_parm = NULL;
1972 shca->sport[port_num - 1].ibqp_sqp[qp_type] = NULL;
1973 spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);
1974 }
1975
1818 /* no support for IB_QPT_SMI yet */ 1976 /* no support for IB_QPT_SMI yet */
1819 if (qp_type == IB_QPT_GSI) { 1977 if (qp_type == IB_QPT_GSI) {
1820 struct ib_event event; 1978 struct ib_event event;
diff --git a/drivers/infiniband/hw/ehca/ehca_reqs.c b/drivers/infiniband/hw/ehca/ehca_reqs.c
index ea91360835d3..3aacc8cf1e44 100644
--- a/drivers/infiniband/hw/ehca/ehca_reqs.c
+++ b/drivers/infiniband/hw/ehca/ehca_reqs.c
@@ -50,6 +50,9 @@
50#include "hcp_if.h" 50#include "hcp_if.h"
51#include "hipz_fns.h" 51#include "hipz_fns.h"
52 52
53/* in RC traffic, insert an empty RDMA READ every this many packets */
54#define ACK_CIRC_THRESHOLD 2000000
55
53static inline int ehca_write_rwqe(struct ipz_queue *ipz_rqueue, 56static inline int ehca_write_rwqe(struct ipz_queue *ipz_rqueue,
54 struct ehca_wqe *wqe_p, 57 struct ehca_wqe *wqe_p,
55 struct ib_recv_wr *recv_wr) 58 struct ib_recv_wr *recv_wr)
@@ -81,7 +84,7 @@ static inline int ehca_write_rwqe(struct ipz_queue *ipz_rqueue,
81 if (ehca_debug_level) { 84 if (ehca_debug_level) {
82 ehca_gen_dbg("RECEIVE WQE written into ipz_rqueue=%p", 85 ehca_gen_dbg("RECEIVE WQE written into ipz_rqueue=%p",
83 ipz_rqueue); 86 ipz_rqueue);
84 ehca_dmp( wqe_p, 16*(6 + wqe_p->nr_of_data_seg), "recv wqe"); 87 ehca_dmp(wqe_p, 16*(6 + wqe_p->nr_of_data_seg), "recv wqe");
85 } 88 }
86 89
87 return 0; 90 return 0;
@@ -135,7 +138,8 @@ static void trace_send_wr_ud(const struct ib_send_wr *send_wr)
135 138
136static inline int ehca_write_swqe(struct ehca_qp *qp, 139static inline int ehca_write_swqe(struct ehca_qp *qp,
137 struct ehca_wqe *wqe_p, 140 struct ehca_wqe *wqe_p,
138 const struct ib_send_wr *send_wr) 141 const struct ib_send_wr *send_wr,
142 int hidden)
139{ 143{
140 u32 idx; 144 u32 idx;
141 u64 dma_length; 145 u64 dma_length;
@@ -176,7 +180,9 @@ static inline int ehca_write_swqe(struct ehca_qp *qp,
176 180
177 wqe_p->wr_flag = 0; 181 wqe_p->wr_flag = 0;
178 182
179 if (send_wr->send_flags & IB_SEND_SIGNALED) 183 if ((send_wr->send_flags & IB_SEND_SIGNALED ||
184 qp->init_attr.sq_sig_type == IB_SIGNAL_ALL_WR)
185 && !hidden)
180 wqe_p->wr_flag |= WQE_WRFLAG_REQ_SIGNAL_COM; 186 wqe_p->wr_flag |= WQE_WRFLAG_REQ_SIGNAL_COM;
181 187
182 if (send_wr->opcode == IB_WR_SEND_WITH_IMM || 188 if (send_wr->opcode == IB_WR_SEND_WITH_IMM ||
@@ -199,7 +205,7 @@ static inline int ehca_write_swqe(struct ehca_qp *qp,
199 205
200 wqe_p->destination_qp_number = send_wr->wr.ud.remote_qpn << 8; 206 wqe_p->destination_qp_number = send_wr->wr.ud.remote_qpn << 8;
201 wqe_p->local_ee_context_qkey = remote_qkey; 207 wqe_p->local_ee_context_qkey = remote_qkey;
202 if (!send_wr->wr.ud.ah) { 208 if (unlikely(!send_wr->wr.ud.ah)) {
203 ehca_gen_err("wr.ud.ah is NULL. qp=%p", qp); 209 ehca_gen_err("wr.ud.ah is NULL. qp=%p", qp);
204 return -EINVAL; 210 return -EINVAL;
205 } 211 }
@@ -255,6 +261,15 @@ static inline int ehca_write_swqe(struct ehca_qp *qp,
255 } /* eof idx */ 261 } /* eof idx */
256 wqe_p->u.nud.atomic_1st_op_dma_len = dma_length; 262 wqe_p->u.nud.atomic_1st_op_dma_len = dma_length;
257 263
264 /* unsolicited ack circumvention */
265 if (send_wr->opcode == IB_WR_RDMA_READ) {
266 /* on RDMA read, switch on and reset counters */
267 qp->message_count = qp->packet_count = 0;
268 qp->unsol_ack_circ = 1;
269 } else
270 /* else estimate #packets */
271 qp->packet_count += (dma_length >> qp->mtu_shift) + 1;
272
258 break; 273 break;
259 274
260 default: 275 default:
@@ -355,13 +370,49 @@ static inline void map_ib_wc_status(u32 cqe_status,
355 *wc_status = IB_WC_SUCCESS; 370 *wc_status = IB_WC_SUCCESS;
356} 371}
357 372
373static inline int post_one_send(struct ehca_qp *my_qp,
374 struct ib_send_wr *cur_send_wr,
375 struct ib_send_wr **bad_send_wr,
376 int hidden)
377{
378 struct ehca_wqe *wqe_p;
379 int ret;
380 u64 start_offset = my_qp->ipz_squeue.current_q_offset;
381
382 /* get pointer next to free WQE */
383 wqe_p = ipz_qeit_get_inc(&my_qp->ipz_squeue);
384 if (unlikely(!wqe_p)) {
385 /* too many posted work requests: queue overflow */
386 if (bad_send_wr)
387 *bad_send_wr = cur_send_wr;
388 ehca_err(my_qp->ib_qp.device, "Too many posted WQEs "
389 "qp_num=%x", my_qp->ib_qp.qp_num);
390 return -ENOMEM;
391 }
392 /* write a SEND WQE into the QUEUE */
393 ret = ehca_write_swqe(my_qp, wqe_p, cur_send_wr, hidden);
394 /*
395 * if something failed,
396 * reset the free entry pointer to the start value
397 */
398 if (unlikely(ret)) {
399 my_qp->ipz_squeue.current_q_offset = start_offset;
400 if (bad_send_wr)
401 *bad_send_wr = cur_send_wr;
402 ehca_err(my_qp->ib_qp.device, "Could not write WQE "
403 "qp_num=%x", my_qp->ib_qp.qp_num);
404 return -EINVAL;
405 }
406
407 return 0;
408}
409
358int ehca_post_send(struct ib_qp *qp, 410int ehca_post_send(struct ib_qp *qp,
359 struct ib_send_wr *send_wr, 411 struct ib_send_wr *send_wr,
360 struct ib_send_wr **bad_send_wr) 412 struct ib_send_wr **bad_send_wr)
361{ 413{
362 struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp); 414 struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
363 struct ib_send_wr *cur_send_wr; 415 struct ib_send_wr *cur_send_wr;
364 struct ehca_wqe *wqe_p;
365 int wqe_cnt = 0; 416 int wqe_cnt = 0;
366 int ret = 0; 417 int ret = 0;
367 unsigned long flags; 418 unsigned long flags;
@@ -369,37 +420,33 @@ int ehca_post_send(struct ib_qp *qp,
369 /* LOCK the QUEUE */ 420 /* LOCK the QUEUE */
370 spin_lock_irqsave(&my_qp->spinlock_s, flags); 421 spin_lock_irqsave(&my_qp->spinlock_s, flags);
371 422
423 /* Send an empty extra RDMA read if:
424 * 1) there has been an RDMA read on this connection before
425 * 2) no RDMA read occurred for ACK_CIRC_THRESHOLD link packets
426 * 3) we can be sure that any previous extra RDMA read has been
427 * processed so we don't overflow the SQ
428 */
429 if (unlikely(my_qp->unsol_ack_circ &&
430 my_qp->packet_count > ACK_CIRC_THRESHOLD &&
431 my_qp->message_count > my_qp->init_attr.cap.max_send_wr)) {
432 /* insert an empty RDMA READ to fix up the remote QP state */
433 struct ib_send_wr circ_wr;
434 memset(&circ_wr, 0, sizeof(circ_wr));
435 circ_wr.opcode = IB_WR_RDMA_READ;
436 post_one_send(my_qp, &circ_wr, NULL, 1); /* ignore retcode */
437 wqe_cnt++;
438 ehca_dbg(qp->device, "posted circ wr qp_num=%x", qp->qp_num);
439 my_qp->message_count = my_qp->packet_count = 0;
440 }
441
372 /* loop processes list of send reqs */ 442 /* loop processes list of send reqs */
373 for (cur_send_wr = send_wr; cur_send_wr != NULL; 443 for (cur_send_wr = send_wr; cur_send_wr != NULL;
374 cur_send_wr = cur_send_wr->next) { 444 cur_send_wr = cur_send_wr->next) {
375 u64 start_offset = my_qp->ipz_squeue.current_q_offset; 445 ret = post_one_send(my_qp, cur_send_wr, bad_send_wr, 0);
376 /* get pointer next to free WQE */
377 wqe_p = ipz_qeit_get_inc(&my_qp->ipz_squeue);
378 if (unlikely(!wqe_p)) {
379 /* too many posted work requests: queue overflow */
380 if (bad_send_wr)
381 *bad_send_wr = cur_send_wr;
382 if (wqe_cnt == 0) {
383 ret = -ENOMEM;
384 ehca_err(qp->device, "Too many posted WQEs "
385 "qp_num=%x", qp->qp_num);
386 }
387 goto post_send_exit0;
388 }
389 /* write a SEND WQE into the QUEUE */
390 ret = ehca_write_swqe(my_qp, wqe_p, cur_send_wr);
391 /*
392 * if something failed,
393 * reset the free entry pointer to the start value
394 */
395 if (unlikely(ret)) { 446 if (unlikely(ret)) {
396 my_qp->ipz_squeue.current_q_offset = start_offset; 447 /* if one or more WQEs were successful, don't fail */
397 *bad_send_wr = cur_send_wr; 448 if (wqe_cnt)
398 if (wqe_cnt == 0) { 449 ret = 0;
399 ret = -EINVAL;
400 ehca_err(qp->device, "Could not write WQE "
401 "qp_num=%x", qp->qp_num);
402 }
403 goto post_send_exit0; 450 goto post_send_exit0;
404 } 451 }
405 wqe_cnt++; 452 wqe_cnt++;
@@ -410,6 +457,7 @@ int ehca_post_send(struct ib_qp *qp,
410post_send_exit0: 457post_send_exit0:
411 iosync(); /* serialize GAL register access */ 458 iosync(); /* serialize GAL register access */
412 hipz_update_sqa(my_qp, wqe_cnt); 459 hipz_update_sqa(my_qp, wqe_cnt);
460 my_qp->message_count += wqe_cnt;
413 spin_unlock_irqrestore(&my_qp->spinlock_s, flags); 461 spin_unlock_irqrestore(&my_qp->spinlock_s, flags);
414 return ret; 462 return ret;
415} 463}
diff --git a/drivers/infiniband/hw/ehca/ehca_sqp.c b/drivers/infiniband/hw/ehca/ehca_sqp.c
index f0792e5fbd02..79e72b25b252 100644
--- a/drivers/infiniband/hw/ehca/ehca_sqp.c
+++ b/drivers/infiniband/hw/ehca/ehca_sqp.c
@@ -40,11 +40,8 @@
40 */ 40 */
41 41
42 42
43#include <linux/module.h>
44#include <linux/err.h>
45#include "ehca_classes.h" 43#include "ehca_classes.h"
46#include "ehca_tools.h" 44#include "ehca_tools.h"
47#include "ehca_qes.h"
48#include "ehca_iverbs.h" 45#include "ehca_iverbs.h"
49#include "hcp_if.h" 46#include "hcp_if.h"
50 47
@@ -93,6 +90,9 @@ u64 ehca_define_sqp(struct ehca_shca *shca,
93 return H_PARAMETER; 90 return H_PARAMETER;
94 } 91 }
95 92
93 if (ehca_nr_ports < 0) /* autodetect mode */
94 return H_SUCCESS;
95
96 for (counter = 0; 96 for (counter = 0;
97 shca->sport[port - 1].port_state != IB_PORT_ACTIVE && 97 shca->sport[port - 1].port_state != IB_PORT_ACTIVE &&
98 counter < ehca_port_act_time; 98 counter < ehca_port_act_time;
diff --git a/drivers/infiniband/hw/ipath/ipath_common.h b/drivers/infiniband/hw/ipath/ipath_common.h
index 851df8a75e79..414621095540 100644
--- a/drivers/infiniband/hw/ipath/ipath_common.h
+++ b/drivers/infiniband/hw/ipath/ipath_common.h
@@ -82,6 +82,16 @@
82#define IPATH_IB_LINK_EXTERNAL 7 /* normal, disable local loopback */ 82#define IPATH_IB_LINK_EXTERNAL 7 /* normal, disable local loopback */
83 83
84/* 84/*
85 * These 3 values (SDR and DDR may be ORed for auto-speed
86 * negotiation) are used for the 3rd argument to path_f_set_ib_cfg
87 * with cmd IPATH_IB_CFG_SPD_ENB, by direct calls or via sysfs. They
88 * are also the the possible values for ipath_link_speed_enabled and active
89 * The values were chosen to match values used within the IB spec.
90 */
91#define IPATH_IB_SDR 1
92#define IPATH_IB_DDR 2
93
94/*
85 * stats maintained by the driver. For now, at least, this is global 95 * stats maintained by the driver. For now, at least, this is global
86 * to all minor devices. 96 * to all minor devices.
87 */ 97 */
@@ -433,8 +443,9 @@ struct ipath_user_info {
433#define IPATH_CMD_UNUSED_2 26 443#define IPATH_CMD_UNUSED_2 26
434#define IPATH_CMD_PIOAVAILUPD 27 /* force an update of PIOAvail reg */ 444#define IPATH_CMD_PIOAVAILUPD 27 /* force an update of PIOAvail reg */
435#define IPATH_CMD_POLL_TYPE 28 /* set the kind of polling we want */ 445#define IPATH_CMD_POLL_TYPE 28 /* set the kind of polling we want */
446#define IPATH_CMD_ARMLAUNCH_CTRL 29 /* armlaunch detection control */
436 447
437#define IPATH_CMD_MAX 28 448#define IPATH_CMD_MAX 29
438 449
439/* 450/*
440 * Poll types 451 * Poll types
@@ -477,6 +488,8 @@ struct ipath_cmd {
477 __u64 port_info; 488 __u64 port_info;
478 /* enable/disable receipt of packets */ 489 /* enable/disable receipt of packets */
479 __u32 recv_ctrl; 490 __u32 recv_ctrl;
491 /* enable/disable armlaunch errors (non-zero to enable) */
492 __u32 armlaunch_ctrl;
480 /* partition key to set */ 493 /* partition key to set */
481 __u16 part_key; 494 __u16 part_key;
482 /* user address of __u32 bitmask of active slaves */ 495 /* user address of __u32 bitmask of active slaves */
@@ -579,7 +592,7 @@ struct ipath_flash {
579struct infinipath_counters { 592struct infinipath_counters {
580 __u64 LBIntCnt; 593 __u64 LBIntCnt;
581 __u64 LBFlowStallCnt; 594 __u64 LBFlowStallCnt;
582 __u64 Reserved1; 595 __u64 TxSDmaDescCnt; /* was Reserved1 */
583 __u64 TxUnsupVLErrCnt; 596 __u64 TxUnsupVLErrCnt;
584 __u64 TxDataPktCnt; 597 __u64 TxDataPktCnt;
585 __u64 TxFlowPktCnt; 598 __u64 TxFlowPktCnt;
@@ -615,12 +628,26 @@ struct infinipath_counters {
615 __u64 RxP6HdrEgrOvflCnt; 628 __u64 RxP6HdrEgrOvflCnt;
616 __u64 RxP7HdrEgrOvflCnt; 629 __u64 RxP7HdrEgrOvflCnt;
617 __u64 RxP8HdrEgrOvflCnt; 630 __u64 RxP8HdrEgrOvflCnt;
618 __u64 Reserved6; 631 __u64 RxP9HdrEgrOvflCnt; /* was Reserved6 */
619 __u64 Reserved7; 632 __u64 RxP10HdrEgrOvflCnt; /* was Reserved7 */
633 __u64 RxP11HdrEgrOvflCnt; /* new for IBA7220 */
634 __u64 RxP12HdrEgrOvflCnt; /* new for IBA7220 */
635 __u64 RxP13HdrEgrOvflCnt; /* new for IBA7220 */
636 __u64 RxP14HdrEgrOvflCnt; /* new for IBA7220 */
637 __u64 RxP15HdrEgrOvflCnt; /* new for IBA7220 */
638 __u64 RxP16HdrEgrOvflCnt; /* new for IBA7220 */
620 __u64 IBStatusChangeCnt; 639 __u64 IBStatusChangeCnt;
621 __u64 IBLinkErrRecoveryCnt; 640 __u64 IBLinkErrRecoveryCnt;
622 __u64 IBLinkDownedCnt; 641 __u64 IBLinkDownedCnt;
623 __u64 IBSymbolErrCnt; 642 __u64 IBSymbolErrCnt;
643 /* The following are new for IBA7220 */
644 __u64 RxVL15DroppedPktCnt;
645 __u64 RxOtherLocalPhyErrCnt;
646 __u64 PcieRetryBufDiagQwordCnt;
647 __u64 ExcessBufferOvflCnt;
648 __u64 LocalLinkIntegrityErrCnt;
649 __u64 RxVlErrCnt;
650 __u64 RxDlidFltrCnt;
624}; 651};
625 652
626/* 653/*
diff --git a/drivers/infiniband/hw/ipath/ipath_cq.c b/drivers/infiniband/hw/ipath/ipath_cq.c
index d1380c7a1703..a03bd28d9b48 100644
--- a/drivers/infiniband/hw/ipath/ipath_cq.c
+++ b/drivers/infiniband/hw/ipath/ipath_cq.c
@@ -421,7 +421,7 @@ int ipath_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
421 else 421 else
422 n = head - tail; 422 n = head - tail;
423 if (unlikely((u32)cqe < n)) { 423 if (unlikely((u32)cqe < n)) {
424 ret = -EOVERFLOW; 424 ret = -EINVAL;
425 goto bail_unlock; 425 goto bail_unlock;
426 } 426 }
427 for (n = 0; tail != head; n++) { 427 for (n = 0; tail != head; n++) {
diff --git a/drivers/infiniband/hw/ipath/ipath_debug.h b/drivers/infiniband/hw/ipath/ipath_debug.h
index 19c56e6491eb..d6f69532d83f 100644
--- a/drivers/infiniband/hw/ipath/ipath_debug.h
+++ b/drivers/infiniband/hw/ipath/ipath_debug.h
@@ -55,7 +55,7 @@
55#define __IPATH_PKTDBG 0x80 /* print packet data */ 55#define __IPATH_PKTDBG 0x80 /* print packet data */
56/* print process startup (init)/exit messages */ 56/* print process startup (init)/exit messages */
57#define __IPATH_PROCDBG 0x100 57#define __IPATH_PROCDBG 0x100
58/* print mmap/nopage stuff, not using VDBG any more */ 58/* print mmap/fault stuff, not using VDBG any more */
59#define __IPATH_MMDBG 0x200 59#define __IPATH_MMDBG 0x200
60#define __IPATH_ERRPKTDBG 0x400 60#define __IPATH_ERRPKTDBG 0x400
61#define __IPATH_USER_SEND 0x1000 /* use user mode send */ 61#define __IPATH_USER_SEND 0x1000 /* use user mode send */
@@ -81,7 +81,7 @@
81#define __IPATH_VERBDBG 0x0 /* very verbose debug */ 81#define __IPATH_VERBDBG 0x0 /* very verbose debug */
82#define __IPATH_PKTDBG 0x0 /* print packet data */ 82#define __IPATH_PKTDBG 0x0 /* print packet data */
83#define __IPATH_PROCDBG 0x0 /* process startup (init)/exit messages */ 83#define __IPATH_PROCDBG 0x0 /* process startup (init)/exit messages */
84/* print mmap/nopage stuff, not using VDBG any more */ 84/* print mmap/fault stuff, not using VDBG any more */
85#define __IPATH_MMDBG 0x0 85#define __IPATH_MMDBG 0x0
86#define __IPATH_EPKTDBG 0x0 /* print ethernet packet data */ 86#define __IPATH_EPKTDBG 0x0 /* print ethernet packet data */
87#define __IPATH_IPATHDBG 0x0 /* Ethernet (IPATH) table dump on */ 87#define __IPATH_IPATHDBG 0x0 /* Ethernet (IPATH) table dump on */
diff --git a/drivers/infiniband/hw/ipath/ipath_driver.c b/drivers/infiniband/hw/ipath/ipath_driver.c
index fc355981bbab..d5ff6ca2db30 100644
--- a/drivers/infiniband/hw/ipath/ipath_driver.c
+++ b/drivers/infiniband/hw/ipath/ipath_driver.c
@@ -334,6 +334,8 @@ static void ipath_verify_pioperf(struct ipath_devdata *dd)
334 udelay(1); 334 udelay(1);
335 } 335 }
336 336
337 ipath_disable_armlaunch(dd);
338
337 writeq(0, piobuf); /* length 0, no dwords actually sent */ 339 writeq(0, piobuf); /* length 0, no dwords actually sent */
338 ipath_flush_wc(); 340 ipath_flush_wc();
339 341
@@ -365,6 +367,7 @@ static void ipath_verify_pioperf(struct ipath_devdata *dd)
365done: 367done:
366 /* disarm piobuf, so it's available again */ 368 /* disarm piobuf, so it's available again */
367 ipath_disarm_piobufs(dd, pbnum, 1); 369 ipath_disarm_piobufs(dd, pbnum, 1);
370 ipath_enable_armlaunch(dd);
368} 371}
369 372
370static int __devinit ipath_init_one(struct pci_dev *pdev, 373static int __devinit ipath_init_one(struct pci_dev *pdev,
@@ -803,31 +806,37 @@ void ipath_disarm_piobufs(struct ipath_devdata *dd, unsigned first,
803 unsigned cnt) 806 unsigned cnt)
804{ 807{
805 unsigned i, last = first + cnt; 808 unsigned i, last = first + cnt;
806 u64 sendctrl, sendorig; 809 unsigned long flags;
807 810
808 ipath_cdbg(PKT, "disarm %u PIObufs first=%u\n", cnt, first); 811 ipath_cdbg(PKT, "disarm %u PIObufs first=%u\n", cnt, first);
809 sendorig = dd->ipath_sendctrl;
810 for (i = first; i < last; i++) { 812 for (i = first; i < last; i++) {
811 sendctrl = sendorig | INFINIPATH_S_DISARM | 813 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
812 (i << INFINIPATH_S_DISARMPIOBUF_SHIFT); 814 /*
815 * The disarm-related bits are write-only, so it
816 * is ok to OR them in with our copy of sendctrl
817 * while we hold the lock.
818 */
813 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 819 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
814 sendctrl); 820 dd->ipath_sendctrl | INFINIPATH_S_DISARM |
821 (i << INFINIPATH_S_DISARMPIOBUF_SHIFT));
822 /* can't disarm bufs back-to-back per iba7220 spec */
823 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
824 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
815 } 825 }
816 826
817 /* 827 /*
818 * Write it again with current value, in case ipath_sendctrl changed 828 * Disable PIOAVAILUPD, then re-enable, reading scratch in
819 * while we were looping; no critical bits that would require
820 * locking.
821 *
822 * disable PIOAVAILUPD, then re-enable, reading scratch in
823 * between. This seems to avoid a chip timing race that causes 829 * between. This seems to avoid a chip timing race that causes
824 * pioavail updates to memory to stop. 830 * pioavail updates to memory to stop. We xor as we don't
831 * know the state of the bit when we're called.
825 */ 832 */
833 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
826 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 834 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
827 sendorig & ~INFINIPATH_S_PIOBUFAVAILUPD); 835 dd->ipath_sendctrl ^ INFINIPATH_S_PIOBUFAVAILUPD);
828 sendorig = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch); 836 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
829 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 837 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
830 dd->ipath_sendctrl); 838 dd->ipath_sendctrl);
839 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
831} 840}
832 841
833/** 842/**
@@ -1003,12 +1012,10 @@ static void get_rhf_errstring(u32 err, char *msg, size_t len)
1003 * ipath_get_egrbuf - get an eager buffer 1012 * ipath_get_egrbuf - get an eager buffer
1004 * @dd: the infinipath device 1013 * @dd: the infinipath device
1005 * @bufnum: the eager buffer to get 1014 * @bufnum: the eager buffer to get
1006 * @err: unused
1007 * 1015 *
1008 * must only be called if ipath_pd[port] is known to be allocated 1016 * must only be called if ipath_pd[port] is known to be allocated
1009 */ 1017 */
1010static inline void *ipath_get_egrbuf(struct ipath_devdata *dd, u32 bufnum, 1018static inline void *ipath_get_egrbuf(struct ipath_devdata *dd, u32 bufnum)
1011 int err)
1012{ 1019{
1013 return dd->ipath_port0_skbinfo ? 1020 return dd->ipath_port0_skbinfo ?
1014 (void *) dd->ipath_port0_skbinfo[bufnum].skb->data : NULL; 1021 (void *) dd->ipath_port0_skbinfo[bufnum].skb->data : NULL;
@@ -1100,13 +1107,14 @@ static void ipath_rcv_hdrerr(struct ipath_devdata *dd,
1100 1107
1101/* 1108/*
1102 * ipath_kreceive - receive a packet 1109 * ipath_kreceive - receive a packet
1103 * @dd: the infinipath device 1110 * @pd: the infinipath port
1104 * 1111 *
1105 * called from interrupt handler for errors or receive interrupt 1112 * called from interrupt handler for errors or receive interrupt
1106 */ 1113 */
1107void ipath_kreceive(struct ipath_devdata *dd) 1114void ipath_kreceive(struct ipath_portdata *pd)
1108{ 1115{
1109 u64 *rc; 1116 u64 *rc;
1117 struct ipath_devdata *dd = pd->port_dd;
1110 void *ebuf; 1118 void *ebuf;
1111 const u32 rsize = dd->ipath_rcvhdrentsize; /* words */ 1119 const u32 rsize = dd->ipath_rcvhdrentsize; /* words */
1112 const u32 maxcnt = dd->ipath_rcvhdrcnt * rsize; /* words */ 1120 const u32 maxcnt = dd->ipath_rcvhdrcnt * rsize; /* words */
@@ -1121,8 +1129,8 @@ void ipath_kreceive(struct ipath_devdata *dd)
1121 goto bail; 1129 goto bail;
1122 } 1130 }
1123 1131
1124 l = dd->ipath_port0head; 1132 l = pd->port_head;
1125 hdrqtail = (u32) le64_to_cpu(*dd->ipath_hdrqtailptr); 1133 hdrqtail = ipath_get_rcvhdrtail(pd);
1126 if (l == hdrqtail) 1134 if (l == hdrqtail)
1127 goto bail; 1135 goto bail;
1128 1136
@@ -1131,7 +1139,7 @@ reloop:
1131 u32 qp; 1139 u32 qp;
1132 u8 *bthbytes; 1140 u8 *bthbytes;
1133 1141
1134 rc = (u64 *) (dd->ipath_pd[0]->port_rcvhdrq + (l << 2)); 1142 rc = (u64 *) (pd->port_rcvhdrq + (l << 2));
1135 hdr = (struct ipath_message_header *)&rc[1]; 1143 hdr = (struct ipath_message_header *)&rc[1];
1136 /* 1144 /*
1137 * could make a network order version of IPATH_KD_QP, and 1145 * could make a network order version of IPATH_KD_QP, and
@@ -1156,7 +1164,7 @@ reloop:
1156 etail = ipath_hdrget_index((__le32 *) rc); 1164 etail = ipath_hdrget_index((__le32 *) rc);
1157 if (tlen > sizeof(*hdr) || 1165 if (tlen > sizeof(*hdr) ||
1158 etype == RCVHQ_RCV_TYPE_NON_KD) 1166 etype == RCVHQ_RCV_TYPE_NON_KD)
1159 ebuf = ipath_get_egrbuf(dd, etail, 0); 1167 ebuf = ipath_get_egrbuf(dd, etail);
1160 } 1168 }
1161 1169
1162 /* 1170 /*
@@ -1191,7 +1199,7 @@ reloop:
1191 be32_to_cpu(hdr->bth[0]) & 0xff); 1199 be32_to_cpu(hdr->bth[0]) & 0xff);
1192 else { 1200 else {
1193 /* 1201 /*
1194 * error packet, type of error unknown. 1202 * error packet, type of error unknown.
1195 * Probably type 3, but we don't know, so don't 1203 * Probably type 3, but we don't know, so don't
1196 * even try to print the opcode, etc. 1204 * even try to print the opcode, etc.
1197 */ 1205 */
@@ -1241,7 +1249,7 @@ reloop:
1241 * earlier packets, we "almost" guarantee we have covered 1249 * earlier packets, we "almost" guarantee we have covered
1242 * that case. 1250 * that case.
1243 */ 1251 */
1244 u32 hqtail = (u32)le64_to_cpu(*dd->ipath_hdrqtailptr); 1252 u32 hqtail = ipath_get_rcvhdrtail(pd);
1245 if (hqtail != hdrqtail) { 1253 if (hqtail != hdrqtail) {
1246 hdrqtail = hqtail; 1254 hdrqtail = hqtail;
1247 reloop = 1; /* loop 1 extra time at most */ 1255 reloop = 1; /* loop 1 extra time at most */
@@ -1251,7 +1259,7 @@ reloop:
1251 1259
1252 pkttot += i; 1260 pkttot += i;
1253 1261
1254 dd->ipath_port0head = l; 1262 pd->port_head = l;
1255 1263
1256 if (pkttot > ipath_stats.sps_maxpkts_call) 1264 if (pkttot > ipath_stats.sps_maxpkts_call)
1257 ipath_stats.sps_maxpkts_call = pkttot; 1265 ipath_stats.sps_maxpkts_call = pkttot;
@@ -1335,14 +1343,9 @@ static void ipath_update_pio_bufs(struct ipath_devdata *dd)
1335 /* 1343 /*
1336 * Chip Errata: bug 6641; even and odd qwords>3 are swapped 1344 * Chip Errata: bug 6641; even and odd qwords>3 are swapped
1337 */ 1345 */
1338 if (i > 3) { 1346 if (i > 3 && (dd->ipath_flags & IPATH_SWAP_PIOBUFS))
1339 if (i & 1) 1347 piov = le64_to_cpu(dd->ipath_pioavailregs_dma[i ^ 1]);
1340 piov = le64_to_cpu( 1348 else
1341 dd->ipath_pioavailregs_dma[i - 1]);
1342 else
1343 piov = le64_to_cpu(
1344 dd->ipath_pioavailregs_dma[i + 1]);
1345 } else
1346 piov = le64_to_cpu(dd->ipath_pioavailregs_dma[i]); 1349 piov = le64_to_cpu(dd->ipath_pioavailregs_dma[i]);
1347 pchg = _IPATH_ALL_CHECKBITS & 1350 pchg = _IPATH_ALL_CHECKBITS &
1348 ~(dd->ipath_pioavailshadow[i] ^ piov); 1351 ~(dd->ipath_pioavailshadow[i] ^ piov);
@@ -1601,7 +1604,8 @@ int ipath_create_rcvhdrq(struct ipath_devdata *dd,
1601 1604
1602 /* clear for security and sanity on each use */ 1605 /* clear for security and sanity on each use */
1603 memset(pd->port_rcvhdrq, 0, pd->port_rcvhdrq_size); 1606 memset(pd->port_rcvhdrq, 0, pd->port_rcvhdrq_size);
1604 memset(pd->port_rcvhdrtail_kvaddr, 0, PAGE_SIZE); 1607 if (pd->port_rcvhdrtail_kvaddr)
1608 memset(pd->port_rcvhdrtail_kvaddr, 0, PAGE_SIZE);
1605 1609
1606 /* 1610 /*
1607 * tell chip each time we init it, even if we are re-using previous 1611 * tell chip each time we init it, even if we are re-using previous
@@ -1617,77 +1621,6 @@ bail:
1617 return ret; 1621 return ret;
1618} 1622}
1619 1623
1620int ipath_waitfor_complete(struct ipath_devdata *dd, ipath_kreg reg_id,
1621 u64 bits_to_wait_for, u64 * valp)
1622{
1623 unsigned long timeout;
1624 u64 lastval, val;
1625 int ret;
1626
1627 lastval = ipath_read_kreg64(dd, reg_id);
1628 /* wait a ridiculously long time */
1629 timeout = jiffies + msecs_to_jiffies(5);
1630 do {
1631 val = ipath_read_kreg64(dd, reg_id);
1632 /* set so they have something, even on failures. */
1633 *valp = val;
1634 if ((val & bits_to_wait_for) == bits_to_wait_for) {
1635 ret = 0;
1636 break;
1637 }
1638 if (val != lastval)
1639 ipath_cdbg(VERBOSE, "Changed from %llx to %llx, "
1640 "waiting for %llx bits\n",
1641 (unsigned long long) lastval,
1642 (unsigned long long) val,
1643 (unsigned long long) bits_to_wait_for);
1644 cond_resched();
1645 if (time_after(jiffies, timeout)) {
1646 ipath_dbg("Didn't get bits %llx in register 0x%x, "
1647 "got %llx\n",
1648 (unsigned long long) bits_to_wait_for,
1649 reg_id, (unsigned long long) *valp);
1650 ret = -ENODEV;
1651 break;
1652 }
1653 } while (1);
1654
1655 return ret;
1656}
1657
1658/**
1659 * ipath_waitfor_mdio_cmdready - wait for last command to complete
1660 * @dd: the infinipath device
1661 *
1662 * Like ipath_waitfor_complete(), but we wait for the CMDVALID bit to go
1663 * away indicating the last command has completed. It doesn't return data
1664 */
1665int ipath_waitfor_mdio_cmdready(struct ipath_devdata *dd)
1666{
1667 unsigned long timeout;
1668 u64 val;
1669 int ret;
1670
1671 /* wait a ridiculously long time */
1672 timeout = jiffies + msecs_to_jiffies(5);
1673 do {
1674 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_mdio);
1675 if (!(val & IPATH_MDIO_CMDVALID)) {
1676 ret = 0;
1677 break;
1678 }
1679 cond_resched();
1680 if (time_after(jiffies, timeout)) {
1681 ipath_dbg("CMDVALID stuck in mdio reg? (%llx)\n",
1682 (unsigned long long) val);
1683 ret = -ENODEV;
1684 break;
1685 }
1686 } while (1);
1687
1688 return ret;
1689}
1690
1691 1624
1692/* 1625/*
1693 * Flush all sends that might be in the ready to send state, as well as any 1626 * Flush all sends that might be in the ready to send state, as well as any
@@ -2056,6 +1989,8 @@ void ipath_set_led_override(struct ipath_devdata *dd, unsigned int val)
2056 */ 1989 */
2057void ipath_shutdown_device(struct ipath_devdata *dd) 1990void ipath_shutdown_device(struct ipath_devdata *dd)
2058{ 1991{
1992 unsigned long flags;
1993
2059 ipath_dbg("Shutting down the device\n"); 1994 ipath_dbg("Shutting down the device\n");
2060 1995
2061 dd->ipath_flags |= IPATH_LINKUNK; 1996 dd->ipath_flags |= IPATH_LINKUNK;
@@ -2076,9 +2011,13 @@ void ipath_shutdown_device(struct ipath_devdata *dd)
2076 * gracefully stop all sends allowing any in progress to trickle out 2011 * gracefully stop all sends allowing any in progress to trickle out
2077 * first. 2012 * first.
2078 */ 2013 */
2079 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 0ULL); 2014 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
2015 dd->ipath_sendctrl = 0;
2016 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
2080 /* flush it */ 2017 /* flush it */
2081 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch); 2018 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
2019 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
2020
2082 /* 2021 /*
2083 * enough for anything that's going to trickle out to have actually 2022 * enough for anything that's going to trickle out to have actually
2084 * done so. 2023 * done so.
@@ -2335,5 +2274,34 @@ int ipath_set_rx_pol_inv(struct ipath_devdata *dd, u8 new_pol_inv)
2335 } 2274 }
2336 return 0; 2275 return 0;
2337} 2276}
2277
2278/*
2279 * Disable and enable the armlaunch error. Used for PIO bandwidth testing on
2280 * the 7220, which is count-based, rather than trigger-based. Safe for the
2281 * driver check, since it's at init. Not completely safe when used for
2282 * user-mode checking, since some error checking can be lost, but not
2283 * particularly risky, and only has problematic side-effects in the face of
2284 * very buggy user code. There is no reference counting, but that's also
2285 * fine, given the intended use.
2286 */
2287void ipath_enable_armlaunch(struct ipath_devdata *dd)
2288{
2289 dd->ipath_lasterror &= ~INFINIPATH_E_SPIOARMLAUNCH;
2290 ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear,
2291 INFINIPATH_E_SPIOARMLAUNCH);
2292 dd->ipath_errormask |= INFINIPATH_E_SPIOARMLAUNCH;
2293 ipath_write_kreg(dd, dd->ipath_kregs->kr_errormask,
2294 dd->ipath_errormask);
2295}
2296
2297void ipath_disable_armlaunch(struct ipath_devdata *dd)
2298{
2299 /* so don't re-enable if already set */
2300 dd->ipath_maskederrs &= ~INFINIPATH_E_SPIOARMLAUNCH;
2301 dd->ipath_errormask &= ~INFINIPATH_E_SPIOARMLAUNCH;
2302 ipath_write_kreg(dd, dd->ipath_kregs->kr_errormask,
2303 dd->ipath_errormask);
2304}
2305
2338module_init(infinipath_init); 2306module_init(infinipath_init);
2339module_exit(infinipath_cleanup); 2307module_exit(infinipath_cleanup);
diff --git a/drivers/infiniband/hw/ipath/ipath_eeprom.c b/drivers/infiniband/hw/ipath/ipath_eeprom.c
index e7c25dbbcdc9..e28a42f53769 100644
--- a/drivers/infiniband/hw/ipath/ipath_eeprom.c
+++ b/drivers/infiniband/hw/ipath/ipath_eeprom.c
@@ -510,10 +510,10 @@ int ipath_eeprom_read(struct ipath_devdata *dd, u8 eeprom_offset,
510{ 510{
511 int ret; 511 int ret;
512 512
513 ret = down_interruptible(&dd->ipath_eep_sem); 513 ret = mutex_lock_interruptible(&dd->ipath_eep_lock);
514 if (!ret) { 514 if (!ret) {
515 ret = ipath_eeprom_internal_read(dd, eeprom_offset, buff, len); 515 ret = ipath_eeprom_internal_read(dd, eeprom_offset, buff, len);
516 up(&dd->ipath_eep_sem); 516 mutex_unlock(&dd->ipath_eep_lock);
517 } 517 }
518 518
519 return ret; 519 return ret;
@@ -524,10 +524,10 @@ int ipath_eeprom_write(struct ipath_devdata *dd, u8 eeprom_offset,
524{ 524{
525 int ret; 525 int ret;
526 526
527 ret = down_interruptible(&dd->ipath_eep_sem); 527 ret = mutex_lock_interruptible(&dd->ipath_eep_lock);
528 if (!ret) { 528 if (!ret) {
529 ret = ipath_eeprom_internal_write(dd, eeprom_offset, buff, len); 529 ret = ipath_eeprom_internal_write(dd, eeprom_offset, buff, len);
530 up(&dd->ipath_eep_sem); 530 mutex_unlock(&dd->ipath_eep_lock);
531 } 531 }
532 532
533 return ret; 533 return ret;
@@ -574,7 +574,7 @@ void ipath_get_eeprom_info(struct ipath_devdata *dd)
574 struct ipath_devdata *dd0 = ipath_lookup(0); 574 struct ipath_devdata *dd0 = ipath_lookup(0);
575 575
576 if (t && dd0->ipath_nguid > 1 && t <= dd0->ipath_nguid) { 576 if (t && dd0->ipath_nguid > 1 && t <= dd0->ipath_nguid) {
577 u8 *bguid, oguid; 577 u8 oguid;
578 dd->ipath_guid = dd0->ipath_guid; 578 dd->ipath_guid = dd0->ipath_guid;
579 bguid = (u8 *) & dd->ipath_guid; 579 bguid = (u8 *) & dd->ipath_guid;
580 580
@@ -616,9 +616,9 @@ void ipath_get_eeprom_info(struct ipath_devdata *dd)
616 goto bail; 616 goto bail;
617 } 617 }
618 618
619 down(&dd->ipath_eep_sem); 619 mutex_lock(&dd->ipath_eep_lock);
620 eep_stat = ipath_eeprom_internal_read(dd, 0, buf, len); 620 eep_stat = ipath_eeprom_internal_read(dd, 0, buf, len);
621 up(&dd->ipath_eep_sem); 621 mutex_unlock(&dd->ipath_eep_lock);
622 622
623 if (eep_stat) { 623 if (eep_stat) {
624 ipath_dev_err(dd, "Failed reading GUID from eeprom\n"); 624 ipath_dev_err(dd, "Failed reading GUID from eeprom\n");
@@ -674,7 +674,6 @@ void ipath_get_eeprom_info(struct ipath_devdata *dd)
674 * elsewhere for backward-compatibility. 674 * elsewhere for backward-compatibility.
675 */ 675 */
676 char *snp = dd->ipath_serial; 676 char *snp = dd->ipath_serial;
677 int len;
678 memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix); 677 memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix);
679 snp[sizeof ifp->if_sprefix] = '\0'; 678 snp[sizeof ifp->if_sprefix] = '\0';
680 len = strlen(snp); 679 len = strlen(snp);
@@ -764,14 +763,14 @@ int ipath_update_eeprom_log(struct ipath_devdata *dd)
764 /* Grab semaphore and read current EEPROM. If we get an 763 /* Grab semaphore and read current EEPROM. If we get an
765 * error, let go, but if not, keep it until we finish write. 764 * error, let go, but if not, keep it until we finish write.
766 */ 765 */
767 ret = down_interruptible(&dd->ipath_eep_sem); 766 ret = mutex_lock_interruptible(&dd->ipath_eep_lock);
768 if (ret) { 767 if (ret) {
769 ipath_dev_err(dd, "Unable to acquire EEPROM for logging\n"); 768 ipath_dev_err(dd, "Unable to acquire EEPROM for logging\n");
770 goto free_bail; 769 goto free_bail;
771 } 770 }
772 ret = ipath_eeprom_internal_read(dd, 0, buf, len); 771 ret = ipath_eeprom_internal_read(dd, 0, buf, len);
773 if (ret) { 772 if (ret) {
774 up(&dd->ipath_eep_sem); 773 mutex_unlock(&dd->ipath_eep_lock);
775 ipath_dev_err(dd, "Unable read EEPROM for logging\n"); 774 ipath_dev_err(dd, "Unable read EEPROM for logging\n");
776 goto free_bail; 775 goto free_bail;
777 } 776 }
@@ -779,7 +778,7 @@ int ipath_update_eeprom_log(struct ipath_devdata *dd)
779 778
780 csum = flash_csum(ifp, 0); 779 csum = flash_csum(ifp, 0);
781 if (csum != ifp->if_csum) { 780 if (csum != ifp->if_csum) {
782 up(&dd->ipath_eep_sem); 781 mutex_unlock(&dd->ipath_eep_lock);
783 ipath_dev_err(dd, "EEPROM cks err (0x%02X, S/B 0x%02X)\n", 782 ipath_dev_err(dd, "EEPROM cks err (0x%02X, S/B 0x%02X)\n",
784 csum, ifp->if_csum); 783 csum, ifp->if_csum);
785 ret = 1; 784 ret = 1;
@@ -849,7 +848,7 @@ int ipath_update_eeprom_log(struct ipath_devdata *dd)
849 csum = flash_csum(ifp, 1); 848 csum = flash_csum(ifp, 1);
850 ret = ipath_eeprom_internal_write(dd, 0, buf, hi_water + 1); 849 ret = ipath_eeprom_internal_write(dd, 0, buf, hi_water + 1);
851 } 850 }
852 up(&dd->ipath_eep_sem); 851 mutex_unlock(&dd->ipath_eep_lock);
853 if (ret) 852 if (ret)
854 ipath_dev_err(dd, "Failed updating EEPROM\n"); 853 ipath_dev_err(dd, "Failed updating EEPROM\n");
855 854
diff --git a/drivers/infiniband/hw/ipath/ipath_file_ops.c b/drivers/infiniband/hw/ipath/ipath_file_ops.c
index 5de3243a47c3..7e025c8e01b6 100644
--- a/drivers/infiniband/hw/ipath/ipath_file_ops.c
+++ b/drivers/infiniband/hw/ipath/ipath_file_ops.c
@@ -169,7 +169,7 @@ static int ipath_get_base_info(struct file *fp,
169 kinfo->spi_piocnt = dd->ipath_pbufsport; 169 kinfo->spi_piocnt = dd->ipath_pbufsport;
170 kinfo->spi_piobufbase = (u64) pd->port_piobufs; 170 kinfo->spi_piobufbase = (u64) pd->port_piobufs;
171 kinfo->__spi_uregbase = (u64) dd->ipath_uregbase + 171 kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +
172 dd->ipath_palign * pd->port_port; 172 dd->ipath_ureg_align * pd->port_port;
173 } else if (master) { 173 } else if (master) {
174 kinfo->spi_piocnt = (dd->ipath_pbufsport / subport_cnt) + 174 kinfo->spi_piocnt = (dd->ipath_pbufsport / subport_cnt) +
175 (dd->ipath_pbufsport % subport_cnt); 175 (dd->ipath_pbufsport % subport_cnt);
@@ -186,7 +186,7 @@ static int ipath_get_base_info(struct file *fp,
186 } 186 }
187 if (shared) { 187 if (shared) {
188 kinfo->spi_port_uregbase = (u64) dd->ipath_uregbase + 188 kinfo->spi_port_uregbase = (u64) dd->ipath_uregbase +
189 dd->ipath_palign * pd->port_port; 189 dd->ipath_ureg_align * pd->port_port;
190 kinfo->spi_port_rcvegrbuf = kinfo->spi_rcv_egrbufs; 190 kinfo->spi_port_rcvegrbuf = kinfo->spi_rcv_egrbufs;
191 kinfo->spi_port_rcvhdr_base = kinfo->spi_rcvhdr_base; 191 kinfo->spi_port_rcvhdr_base = kinfo->spi_rcvhdr_base;
192 kinfo->spi_port_rcvhdr_tailaddr = kinfo->spi_rcvhdr_tailaddr; 192 kinfo->spi_port_rcvhdr_tailaddr = kinfo->spi_rcvhdr_tailaddr;
@@ -742,11 +742,12 @@ static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
742 * updated and correct itself, even in the face of software 742 * updated and correct itself, even in the face of software
743 * bugs. 743 * bugs.
744 */ 744 */
745 *(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0; 745 if (pd->port_rcvhdrtail_kvaddr)
746 set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port, 746 ipath_clear_rcvhdrtail(pd);
747 set_bit(dd->ipath_r_portenable_shift + pd->port_port,
747 &dd->ipath_rcvctrl); 748 &dd->ipath_rcvctrl);
748 } else 749 } else
749 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port, 750 clear_bit(dd->ipath_r_portenable_shift + pd->port_port,
750 &dd->ipath_rcvctrl); 751 &dd->ipath_rcvctrl);
751 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, 752 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
752 dd->ipath_rcvctrl); 753 dd->ipath_rcvctrl);
@@ -881,7 +882,7 @@ static int ipath_create_user_egr(struct ipath_portdata *pd)
881 882
882 egrcnt = dd->ipath_rcvegrcnt; 883 egrcnt = dd->ipath_rcvegrcnt;
883 /* TID number offset for this port */ 884 /* TID number offset for this port */
884 egroff = pd->port_port * egrcnt; 885 egroff = (pd->port_port - 1) * egrcnt + dd->ipath_p0_rcvegrcnt;
885 egrsize = dd->ipath_rcvegrbufsize; 886 egrsize = dd->ipath_rcvegrbufsize;
886 ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid " 887 ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
887 "offset %x, egrsize %u\n", egrcnt, egroff, egrsize); 888 "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
@@ -1049,11 +1050,6 @@ static int mmap_piobufs(struct vm_area_struct *vma,
1049 1050
1050 phys = dd->ipath_physaddr + piobufs; 1051 phys = dd->ipath_physaddr + piobufs;
1051 1052
1052 /*
1053 * Don't mark this as non-cached, or we don't get the
1054 * write combining behavior we want on the PIO buffers!
1055 */
1056
1057#if defined(__powerpc__) 1053#if defined(__powerpc__)
1058 /* There isn't a generic way to specify writethrough mappings */ 1054 /* There isn't a generic way to specify writethrough mappings */
1059 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE; 1055 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
@@ -1120,33 +1116,24 @@ bail:
1120} 1116}
1121 1117
1122/* 1118/*
1123 * ipath_file_vma_nopage - handle a VMA page fault. 1119 * ipath_file_vma_fault - handle a VMA page fault.
1124 */ 1120 */
1125static struct page *ipath_file_vma_nopage(struct vm_area_struct *vma, 1121static int ipath_file_vma_fault(struct vm_area_struct *vma,
1126 unsigned long address, int *type) 1122 struct vm_fault *vmf)
1127{ 1123{
1128 unsigned long offset = address - vma->vm_start; 1124 struct page *page;
1129 struct page *page = NOPAGE_SIGBUS;
1130 void *pageptr;
1131 1125
1132 /* 1126 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
1133 * Convert the vmalloc address into a struct page.
1134 */
1135 pageptr = (void *)(offset + (vma->vm_pgoff << PAGE_SHIFT));
1136 page = vmalloc_to_page(pageptr);
1137 if (!page) 1127 if (!page)
1138 goto out; 1128 return VM_FAULT_SIGBUS;
1139
1140 /* Increment the reference count. */
1141 get_page(page); 1129 get_page(page);
1142 if (type) 1130 vmf->page = page;
1143 *type = VM_FAULT_MINOR; 1131
1144out: 1132 return 0;
1145 return page;
1146} 1133}
1147 1134
1148static struct vm_operations_struct ipath_file_vm_ops = { 1135static struct vm_operations_struct ipath_file_vm_ops = {
1149 .nopage = ipath_file_vma_nopage, 1136 .fault = ipath_file_vma_fault,
1150}; 1137};
1151 1138
1152static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr, 1139static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
@@ -1284,7 +1271,7 @@ static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
1284 goto bail; 1271 goto bail;
1285 } 1272 }
1286 1273
1287 ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port; 1274 ureg = dd->ipath_uregbase + dd->ipath_ureg_align * pd->port_port;
1288 if (!pd->port_subport_cnt) { 1275 if (!pd->port_subport_cnt) {
1289 /* port is not shared */ 1276 /* port is not shared */
1290 piocnt = dd->ipath_pbufsport; 1277 piocnt = dd->ipath_pbufsport;
@@ -1400,7 +1387,10 @@ static unsigned int ipath_poll_next(struct ipath_portdata *pd,
1400 pollflag = ipath_poll_hdrqfull(pd); 1387 pollflag = ipath_poll_hdrqfull(pd);
1401 1388
1402 head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port); 1389 head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
1403 tail = *(volatile u64 *)pd->port_rcvhdrtail_kvaddr; 1390 if (pd->port_rcvhdrtail_kvaddr)
1391 tail = ipath_get_rcvhdrtail(pd);
1392 else
1393 tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
1404 1394
1405 if (head != tail) 1395 if (head != tail)
1406 pollflag |= POLLIN | POLLRDNORM; 1396 pollflag |= POLLIN | POLLRDNORM;
@@ -1410,7 +1400,7 @@ static unsigned int ipath_poll_next(struct ipath_portdata *pd,
1410 /* flush waiting flag so we don't miss an event */ 1400 /* flush waiting flag so we don't miss an event */
1411 wmb(); 1401 wmb();
1412 1402
1413 set_bit(pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT, 1403 set_bit(pd->port_port + dd->ipath_r_intravail_shift,
1414 &dd->ipath_rcvctrl); 1404 &dd->ipath_rcvctrl);
1415 1405
1416 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, 1406 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
@@ -1790,6 +1780,7 @@ static int find_shared_port(struct file *fp,
1790 } 1780 }
1791 port_fp(fp) = pd; 1781 port_fp(fp) = pd;
1792 subport_fp(fp) = pd->port_cnt++; 1782 subport_fp(fp) = pd->port_cnt++;
1783 pd->port_subpid[subport_fp(fp)] = current->pid;
1793 tidcursor_fp(fp) = 0; 1784 tidcursor_fp(fp) = 0;
1794 pd->active_slaves |= 1 << subport_fp(fp); 1785 pd->active_slaves |= 1 << subport_fp(fp);
1795 ipath_cdbg(PROC, 1786 ipath_cdbg(PROC,
@@ -1920,8 +1911,7 @@ static int ipath_do_user_init(struct file *fp,
1920 */ 1911 */
1921 head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port); 1912 head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
1922 ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port); 1913 ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
1923 dd->ipath_lastegrheads[pd->port_port] = -1; 1914 pd->port_lastrcvhdrqtail = -1;
1924 dd->ipath_lastrcvhdrqtails[pd->port_port] = -1;
1925 ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n", 1915 ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
1926 pd->port_port, head32); 1916 pd->port_port, head32);
1927 pd->port_tidcursor = 0; /* start at beginning after open */ 1917 pd->port_tidcursor = 0; /* start at beginning after open */
@@ -1941,11 +1931,13 @@ static int ipath_do_user_init(struct file *fp,
1941 * We explictly set the in-memory copy to 0 beforehand, so we don't 1931 * We explictly set the in-memory copy to 0 beforehand, so we don't
1942 * have to wait to be sure the DMA update has happened. 1932 * have to wait to be sure the DMA update has happened.
1943 */ 1933 */
1944 *(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0ULL; 1934 if (pd->port_rcvhdrtail_kvaddr)
1945 set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port, 1935 ipath_clear_rcvhdrtail(pd);
1936 set_bit(dd->ipath_r_portenable_shift + pd->port_port,
1946 &dd->ipath_rcvctrl); 1937 &dd->ipath_rcvctrl);
1947 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, 1938 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1948 dd->ipath_rcvctrl & ~INFINIPATH_R_TAILUPD); 1939 dd->ipath_rcvctrl &
1940 ~(1ULL << dd->ipath_r_tailupd_shift));
1949 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, 1941 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1950 dd->ipath_rcvctrl); 1942 dd->ipath_rcvctrl);
1951 /* Notify any waiting slaves */ 1943 /* Notify any waiting slaves */
@@ -2022,6 +2014,7 @@ static int ipath_close(struct inode *in, struct file *fp)
2022 * the slave(s) don't wait for receive data forever. 2014 * the slave(s) don't wait for receive data forever.
2023 */ 2015 */
2024 pd->active_slaves &= ~(1 << fd->subport); 2016 pd->active_slaves &= ~(1 << fd->subport);
2017 pd->port_subpid[fd->subport] = 0;
2025 mutex_unlock(&ipath_mutex); 2018 mutex_unlock(&ipath_mutex);
2026 goto bail; 2019 goto bail;
2027 } 2020 }
@@ -2054,9 +2047,9 @@ static int ipath_close(struct inode *in, struct file *fp)
2054 if (dd->ipath_kregbase) { 2047 if (dd->ipath_kregbase) {
2055 int i; 2048 int i;
2056 /* atomically clear receive enable port and intr avail. */ 2049 /* atomically clear receive enable port and intr avail. */
2057 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + port, 2050 clear_bit(dd->ipath_r_portenable_shift + port,
2058 &dd->ipath_rcvctrl); 2051 &dd->ipath_rcvctrl);
2059 clear_bit(pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT, 2052 clear_bit(pd->port_port + dd->ipath_r_intravail_shift,
2060 &dd->ipath_rcvctrl); 2053 &dd->ipath_rcvctrl);
2061 ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl, 2054 ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
2062 dd->ipath_rcvctrl); 2055 dd->ipath_rcvctrl);
@@ -2149,11 +2142,15 @@ static int ipath_get_slave_info(struct ipath_portdata *pd,
2149 2142
2150static int ipath_force_pio_avail_update(struct ipath_devdata *dd) 2143static int ipath_force_pio_avail_update(struct ipath_devdata *dd)
2151{ 2144{
2152 u64 reg = dd->ipath_sendctrl; 2145 unsigned long flags;
2153 2146
2154 clear_bit(IPATH_S_PIOBUFAVAILUPD, &reg); 2147 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
2155 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, reg); 2148 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
2149 dd->ipath_sendctrl & ~INFINIPATH_S_PIOBUFAVAILUPD);
2150 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
2156 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl); 2151 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
2152 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
2153 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
2157 2154
2158 return 0; 2155 return 0;
2159} 2156}
@@ -2227,6 +2224,11 @@ static ssize_t ipath_write(struct file *fp, const char __user *data,
2227 dest = &cmd.cmd.poll_type; 2224 dest = &cmd.cmd.poll_type;
2228 src = &ucmd->cmd.poll_type; 2225 src = &ucmd->cmd.poll_type;
2229 break; 2226 break;
2227 case IPATH_CMD_ARMLAUNCH_CTRL:
2228 copy = sizeof(cmd.cmd.armlaunch_ctrl);
2229 dest = &cmd.cmd.armlaunch_ctrl;
2230 src = &ucmd->cmd.armlaunch_ctrl;
2231 break;
2230 default: 2232 default:
2231 ret = -EINVAL; 2233 ret = -EINVAL;
2232 goto bail; 2234 goto bail;
@@ -2302,6 +2304,12 @@ static ssize_t ipath_write(struct file *fp, const char __user *data,
2302 case IPATH_CMD_POLL_TYPE: 2304 case IPATH_CMD_POLL_TYPE:
2303 pd->poll_type = cmd.cmd.poll_type; 2305 pd->poll_type = cmd.cmd.poll_type;
2304 break; 2306 break;
2307 case IPATH_CMD_ARMLAUNCH_CTRL:
2308 if (cmd.cmd.armlaunch_ctrl)
2309 ipath_enable_armlaunch(pd->port_dd);
2310 else
2311 ipath_disable_armlaunch(pd->port_dd);
2312 break;
2305 } 2313 }
2306 2314
2307 if (ret >= 0) 2315 if (ret >= 0)
diff --git a/drivers/infiniband/hw/ipath/ipath_fs.c b/drivers/infiniband/hw/ipath/ipath_fs.c
index 262c25db05cd..23faba9d21eb 100644
--- a/drivers/infiniband/hw/ipath/ipath_fs.c
+++ b/drivers/infiniband/hw/ipath/ipath_fs.c
@@ -108,21 +108,16 @@ static const struct file_operations atomic_stats_ops = {
108 .read = atomic_stats_read, 108 .read = atomic_stats_read,
109}; 109};
110 110
111#define NUM_COUNTERS sizeof(struct infinipath_counters) / sizeof(u64)
112
113static ssize_t atomic_counters_read(struct file *file, char __user *buf, 111static ssize_t atomic_counters_read(struct file *file, char __user *buf,
114 size_t count, loff_t *ppos) 112 size_t count, loff_t *ppos)
115{ 113{
116 u64 counters[NUM_COUNTERS]; 114 struct infinipath_counters counters;
117 u16 i;
118 struct ipath_devdata *dd; 115 struct ipath_devdata *dd;
119 116
120 dd = file->f_path.dentry->d_inode->i_private; 117 dd = file->f_path.dentry->d_inode->i_private;
118 dd->ipath_f_read_counters(dd, &counters);
121 119
122 for (i = 0; i < NUM_COUNTERS; i++) 120 return simple_read_from_buffer(buf, count, ppos, &counters,
123 counters[i] = ipath_snap_cntr(dd, i);
124
125 return simple_read_from_buffer(buf, count, ppos, counters,
126 sizeof counters); 121 sizeof counters);
127} 122}
128 123
@@ -243,8 +238,7 @@ static int create_device_files(struct super_block *sb,
243 238
244 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit); 239 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
245 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir, 240 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
246 (struct file_operations *) &simple_dir_operations, 241 &simple_dir_operations, dd);
247 dd);
248 if (ret) { 242 if (ret) {
249 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret); 243 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
250 goto bail; 244 goto bail;
diff --git a/drivers/infiniband/hw/ipath/ipath_iba6110.c b/drivers/infiniband/hw/ipath/ipath_iba6110.c
index ddbebe4bdb27..9e2ced3cdc5e 100644
--- a/drivers/infiniband/hw/ipath/ipath_iba6110.c
+++ b/drivers/infiniband/hw/ipath/ipath_iba6110.c
@@ -148,10 +148,57 @@ struct _infinipath_do_not_use_kernel_regs {
148 unsigned long long ReservedSW2[4]; 148 unsigned long long ReservedSW2[4];
149}; 149};
150 150
151#define IPATH_KREG_OFFSET(field) (offsetof(struct \ 151struct _infinipath_do_not_use_counters {
152 _infinipath_do_not_use_kernel_regs, field) / sizeof(u64)) 152 __u64 LBIntCnt;
153 __u64 LBFlowStallCnt;
154 __u64 Reserved1;
155 __u64 TxUnsupVLErrCnt;
156 __u64 TxDataPktCnt;
157 __u64 TxFlowPktCnt;
158 __u64 TxDwordCnt;
159 __u64 TxLenErrCnt;
160 __u64 TxMaxMinLenErrCnt;
161 __u64 TxUnderrunCnt;
162 __u64 TxFlowStallCnt;
163 __u64 TxDroppedPktCnt;
164 __u64 RxDroppedPktCnt;
165 __u64 RxDataPktCnt;
166 __u64 RxFlowPktCnt;
167 __u64 RxDwordCnt;
168 __u64 RxLenErrCnt;
169 __u64 RxMaxMinLenErrCnt;
170 __u64 RxICRCErrCnt;
171 __u64 RxVCRCErrCnt;
172 __u64 RxFlowCtrlErrCnt;
173 __u64 RxBadFormatCnt;
174 __u64 RxLinkProblemCnt;
175 __u64 RxEBPCnt;
176 __u64 RxLPCRCErrCnt;
177 __u64 RxBufOvflCnt;
178 __u64 RxTIDFullErrCnt;
179 __u64 RxTIDValidErrCnt;
180 __u64 RxPKeyMismatchCnt;
181 __u64 RxP0HdrEgrOvflCnt;
182 __u64 RxP1HdrEgrOvflCnt;
183 __u64 RxP2HdrEgrOvflCnt;
184 __u64 RxP3HdrEgrOvflCnt;
185 __u64 RxP4HdrEgrOvflCnt;
186 __u64 RxP5HdrEgrOvflCnt;
187 __u64 RxP6HdrEgrOvflCnt;
188 __u64 RxP7HdrEgrOvflCnt;
189 __u64 RxP8HdrEgrOvflCnt;
190 __u64 Reserved6;
191 __u64 Reserved7;
192 __u64 IBStatusChangeCnt;
193 __u64 IBLinkErrRecoveryCnt;
194 __u64 IBLinkDownedCnt;
195 __u64 IBSymbolErrCnt;
196};
197
198#define IPATH_KREG_OFFSET(field) (offsetof( \
199 struct _infinipath_do_not_use_kernel_regs, field) / sizeof(u64))
153#define IPATH_CREG_OFFSET(field) (offsetof( \ 200#define IPATH_CREG_OFFSET(field) (offsetof( \
154 struct infinipath_counters, field) / sizeof(u64)) 201 struct _infinipath_do_not_use_counters, field) / sizeof(u64))
155 202
156static const struct ipath_kregs ipath_ht_kregs = { 203static const struct ipath_kregs ipath_ht_kregs = {
157 .kr_control = IPATH_KREG_OFFSET(Control), 204 .kr_control = IPATH_KREG_OFFSET(Control),
@@ -282,6 +329,9 @@ static const struct ipath_cregs ipath_ht_cregs = {
282#define INFINIPATH_HWE_HTAPLL_RFSLIP 0x1000000000000000ULL 329#define INFINIPATH_HWE_HTAPLL_RFSLIP 0x1000000000000000ULL
283#define INFINIPATH_HWE_SERDESPLLFAILED 0x2000000000000000ULL 330#define INFINIPATH_HWE_SERDESPLLFAILED 0x2000000000000000ULL
284 331
332#define IBA6110_IBCS_LINKTRAININGSTATE_MASK 0xf
333#define IBA6110_IBCS_LINKSTATE_SHIFT 4
334
285/* kr_extstatus bits */ 335/* kr_extstatus bits */
286#define INFINIPATH_EXTS_FREQSEL 0x2 336#define INFINIPATH_EXTS_FREQSEL 0x2
287#define INFINIPATH_EXTS_SERDESSEL 0x4 337#define INFINIPATH_EXTS_SERDESSEL 0x4
@@ -296,6 +346,12 @@ static const struct ipath_cregs ipath_ht_cregs = {
296#define INFINIPATH_RT_BUFSIZE_MASK 0x3FFFULL 346#define INFINIPATH_RT_BUFSIZE_MASK 0x3FFFULL
297#define INFINIPATH_RT_BUFSIZE_SHIFT 48 347#define INFINIPATH_RT_BUFSIZE_SHIFT 48
298 348
349#define INFINIPATH_R_INTRAVAIL_SHIFT 16
350#define INFINIPATH_R_TAILUPD_SHIFT 31
351
352/* kr_xgxsconfig bits */
353#define INFINIPATH_XGXS_RESET 0x7ULL
354
299/* 355/*
300 * masks and bits that are different in different chips, or present only 356 * masks and bits that are different in different chips, or present only
301 * in one 357 * in one
@@ -652,7 +708,6 @@ static int ipath_ht_boardname(struct ipath_devdata *dd, char *name,
652 "with ID %u\n", boardrev); 708 "with ID %u\n", boardrev);
653 snprintf(name, namelen, "Unknown_InfiniPath_QHT7xxx_%u", 709 snprintf(name, namelen, "Unknown_InfiniPath_QHT7xxx_%u",
654 boardrev); 710 boardrev);
655 ret = 1;
656 break; 711 break;
657 } 712 }
658 if (n) 713 if (n)
@@ -686,6 +741,13 @@ static int ipath_ht_boardname(struct ipath_devdata *dd, char *name,
686 dd->ipath_htspeed); 741 dd->ipath_htspeed);
687 ret = 0; 742 ret = 0;
688 743
744 /*
745 * set here, not in ipath_init_*_funcs because we have to do
746 * it after we can read chip registers.
747 */
748 dd->ipath_ureg_align =
749 ipath_read_kreg32(dd, dd->ipath_kregs->kr_pagealign);
750
689bail: 751bail:
690 return ret; 752 return ret;
691} 753}
@@ -969,7 +1031,8 @@ static int ipath_setup_ht_config(struct ipath_devdata *dd,
969 do { 1031 do {
970 u8 cap_type; 1032 u8 cap_type;
971 1033
972 /* the HT capability type byte is 3 bytes after the 1034 /*
1035 * The HT capability type byte is 3 bytes after the
973 * capability byte. 1036 * capability byte.
974 */ 1037 */
975 if (pci_read_config_byte(pdev, pos + 3, &cap_type)) { 1038 if (pci_read_config_byte(pdev, pos + 3, &cap_type)) {
@@ -982,6 +1045,8 @@ static int ipath_setup_ht_config(struct ipath_devdata *dd,
982 } while ((pos = pci_find_next_capability(pdev, pos, 1045 } while ((pos = pci_find_next_capability(pdev, pos,
983 PCI_CAP_ID_HT))); 1046 PCI_CAP_ID_HT)));
984 1047
1048 dd->ipath_flags |= IPATH_SWAP_PIOBUFS;
1049
985bail: 1050bail:
986 return ret; 1051 return ret;
987} 1052}
@@ -1074,11 +1139,55 @@ static void ipath_setup_ht_setextled(struct ipath_devdata *dd,
1074 1139
1075static void ipath_init_ht_variables(struct ipath_devdata *dd) 1140static void ipath_init_ht_variables(struct ipath_devdata *dd)
1076{ 1141{
1142 /*
1143 * setup the register offsets, since they are different for each
1144 * chip
1145 */
1146 dd->ipath_kregs = &ipath_ht_kregs;
1147 dd->ipath_cregs = &ipath_ht_cregs;
1148
1077 dd->ipath_gpio_sda_num = _IPATH_GPIO_SDA_NUM; 1149 dd->ipath_gpio_sda_num = _IPATH_GPIO_SDA_NUM;
1078 dd->ipath_gpio_scl_num = _IPATH_GPIO_SCL_NUM; 1150 dd->ipath_gpio_scl_num = _IPATH_GPIO_SCL_NUM;
1079 dd->ipath_gpio_sda = IPATH_GPIO_SDA; 1151 dd->ipath_gpio_sda = IPATH_GPIO_SDA;
1080 dd->ipath_gpio_scl = IPATH_GPIO_SCL; 1152 dd->ipath_gpio_scl = IPATH_GPIO_SCL;
1081 1153
1154 /*
1155 * Fill in data for field-values that change in newer chips.
1156 * We dynamically specify only the mask for LINKTRAININGSTATE
1157 * and only the shift for LINKSTATE, as they are the only ones
1158 * that change. Also precalculate the 3 link states of interest
1159 * and the combined mask.
1160 */
1161 dd->ibcs_ls_shift = IBA6110_IBCS_LINKSTATE_SHIFT;
1162 dd->ibcs_lts_mask = IBA6110_IBCS_LINKTRAININGSTATE_MASK;
1163 dd->ibcs_mask = (INFINIPATH_IBCS_LINKSTATE_MASK <<
1164 dd->ibcs_ls_shift) | dd->ibcs_lts_mask;
1165 dd->ib_init = (INFINIPATH_IBCS_LT_STATE_LINKUP <<
1166 INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) |
1167 (INFINIPATH_IBCS_L_STATE_INIT << dd->ibcs_ls_shift);
1168 dd->ib_arm = (INFINIPATH_IBCS_LT_STATE_LINKUP <<
1169 INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) |
1170 (INFINIPATH_IBCS_L_STATE_ARM << dd->ibcs_ls_shift);
1171 dd->ib_active = (INFINIPATH_IBCS_LT_STATE_LINKUP <<
1172 INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) |
1173 (INFINIPATH_IBCS_L_STATE_ACTIVE << dd->ibcs_ls_shift);
1174
1175 /*
1176 * Fill in data for ibcc field-values that change in newer chips.
1177 * We dynamically specify only the mask for LINKINITCMD
1178 * and only the shift for LINKCMD and MAXPKTLEN, as they are
1179 * the only ones that change.
1180 */
1181 dd->ibcc_lic_mask = INFINIPATH_IBCC_LINKINITCMD_MASK;
1182 dd->ibcc_lc_shift = INFINIPATH_IBCC_LINKCMD_SHIFT;
1183 dd->ibcc_mpl_shift = INFINIPATH_IBCC_MAXPKTLEN_SHIFT;
1184
1185 /* Fill in shifts for RcvCtrl. */
1186 dd->ipath_r_portenable_shift = INFINIPATH_R_PORTENABLE_SHIFT;
1187 dd->ipath_r_intravail_shift = INFINIPATH_R_INTRAVAIL_SHIFT;
1188 dd->ipath_r_tailupd_shift = INFINIPATH_R_TAILUPD_SHIFT;
1189 dd->ipath_r_portcfg_shift = 0; /* Not on IBA6110 */
1190
1082 dd->ipath_i_bitsextant = 1191 dd->ipath_i_bitsextant =
1083 (INFINIPATH_I_RCVURG_MASK << INFINIPATH_I_RCVURG_SHIFT) | 1192 (INFINIPATH_I_RCVURG_MASK << INFINIPATH_I_RCVURG_SHIFT) |
1084 (INFINIPATH_I_RCVAVAIL_MASK << 1193 (INFINIPATH_I_RCVAVAIL_MASK <<
@@ -1135,6 +1244,8 @@ static void ipath_init_ht_variables(struct ipath_devdata *dd)
1135 1244
1136 dd->ipath_i_rcvavail_mask = INFINIPATH_I_RCVAVAIL_MASK; 1245 dd->ipath_i_rcvavail_mask = INFINIPATH_I_RCVAVAIL_MASK;
1137 dd->ipath_i_rcvurg_mask = INFINIPATH_I_RCVURG_MASK; 1246 dd->ipath_i_rcvurg_mask = INFINIPATH_I_RCVURG_MASK;
1247 dd->ipath_i_rcvavail_shift = INFINIPATH_I_RCVAVAIL_SHIFT;
1248 dd->ipath_i_rcvurg_shift = INFINIPATH_I_RCVURG_SHIFT;
1138 1249
1139 /* 1250 /*
1140 * EEPROM error log 0 is TXE Parity errors. 1 is RXE Parity. 1251 * EEPROM error log 0 is TXE Parity errors. 1 is RXE Parity.
@@ -1148,9 +1259,17 @@ static void ipath_init_ht_variables(struct ipath_devdata *dd)
1148 INFINIPATH_HWE_RXEMEMPARITYERR_MASK << 1259 INFINIPATH_HWE_RXEMEMPARITYERR_MASK <<
1149 INFINIPATH_HWE_RXEMEMPARITYERR_SHIFT; 1260 INFINIPATH_HWE_RXEMEMPARITYERR_SHIFT;
1150 1261
1151 dd->ipath_eep_st_masks[2].errs_to_log = 1262 dd->ipath_eep_st_masks[2].errs_to_log = INFINIPATH_E_RESET;
1152 INFINIPATH_E_INVALIDADDR | INFINIPATH_E_RESET;
1153 1263
1264 dd->delay_mult = 2; /* SDR, 4X, can't change */
1265
1266 dd->ipath_link_width_supported = IB_WIDTH_1X | IB_WIDTH_4X;
1267 dd->ipath_link_speed_supported = IPATH_IB_SDR;
1268 dd->ipath_link_width_enabled = IB_WIDTH_4X;
1269 dd->ipath_link_speed_enabled = dd->ipath_link_speed_supported;
1270 /* these can't change for this chip, so set once */
1271 dd->ipath_link_width_active = dd->ipath_link_width_enabled;
1272 dd->ipath_link_speed_active = dd->ipath_link_speed_enabled;
1154} 1273}
1155 1274
1156/** 1275/**
@@ -1205,14 +1324,16 @@ static void ipath_ht_init_hwerrors(struct ipath_devdata *dd)
1205 val &= ~INFINIPATH_HWE_HTCMISCERR4; 1324 val &= ~INFINIPATH_HWE_HTCMISCERR4;
1206 1325
1207 /* 1326 /*
1208 * PLL ignored because MDIO interface has a logic problem 1327 * PLL ignored because unused MDIO interface has a logic problem
1209 * for reads, on Comstock and Ponderosa. BRINGUP
1210 */ 1328 */
1211 if (dd->ipath_boardrev == 4 || dd->ipath_boardrev == 9) 1329 if (dd->ipath_boardrev == 4 || dd->ipath_boardrev == 9)
1212 val &= ~INFINIPATH_HWE_SERDESPLLFAILED; 1330 val &= ~INFINIPATH_HWE_SERDESPLLFAILED;
1213 dd->ipath_hwerrmask = val; 1331 dd->ipath_hwerrmask = val;
1214} 1332}
1215 1333
1334
1335
1336
1216/** 1337/**
1217 * ipath_ht_bringup_serdes - bring up the serdes 1338 * ipath_ht_bringup_serdes - bring up the serdes
1218 * @dd: the infinipath device 1339 * @dd: the infinipath device
@@ -1284,16 +1405,6 @@ static int ipath_ht_bringup_serdes(struct ipath_devdata *dd)
1284 } 1405 }
1285 1406
1286 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig); 1407 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig);
1287 if (((val >> INFINIPATH_XGXS_MDIOADDR_SHIFT) &
1288 INFINIPATH_XGXS_MDIOADDR_MASK) != 3) {
1289 val &= ~(INFINIPATH_XGXS_MDIOADDR_MASK <<
1290 INFINIPATH_XGXS_MDIOADDR_SHIFT);
1291 /*
1292 * we use address 3
1293 */
1294 val |= 3ULL << INFINIPATH_XGXS_MDIOADDR_SHIFT;
1295 change = 1;
1296 }
1297 if (val & INFINIPATH_XGXS_RESET) { 1408 if (val & INFINIPATH_XGXS_RESET) {
1298 /* normally true after boot */ 1409 /* normally true after boot */
1299 val &= ~INFINIPATH_XGXS_RESET; 1410 val &= ~INFINIPATH_XGXS_RESET;
@@ -1329,21 +1440,6 @@ static int ipath_ht_bringup_serdes(struct ipath_devdata *dd)
1329 (unsigned long long) 1440 (unsigned long long)
1330 ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig)); 1441 ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig));
1331 1442
1332 if (!ipath_waitfor_mdio_cmdready(dd)) {
1333 ipath_write_kreg(dd, dd->ipath_kregs->kr_mdio,
1334 ipath_mdio_req(IPATH_MDIO_CMD_READ, 31,
1335 IPATH_MDIO_CTRL_XGXS_REG_8,
1336 0));
1337 if (ipath_waitfor_complete(dd, dd->ipath_kregs->kr_mdio,
1338 IPATH_MDIO_DATAVALID, &val))
1339 ipath_dbg("Never got MDIO data for XGXS status "
1340 "read\n");
1341 else
1342 ipath_cdbg(VERBOSE, "MDIO Read reg8, "
1343 "'bank' 31 %x\n", (u32) val);
1344 } else
1345 ipath_dbg("Never got MDIO cmdready for XGXS status read\n");
1346
1347 return ret; /* for now, say we always succeeded */ 1443 return ret; /* for now, say we always succeeded */
1348} 1444}
1349 1445
@@ -1396,6 +1492,7 @@ static void ipath_ht_put_tid(struct ipath_devdata *dd,
1396 pa |= lenvalid | INFINIPATH_RT_VALID; 1492 pa |= lenvalid | INFINIPATH_RT_VALID;
1397 } 1493 }
1398 } 1494 }
1495
1399 writeq(pa, tidptr); 1496 writeq(pa, tidptr);
1400} 1497}
1401 1498
@@ -1526,8 +1623,7 @@ static int ipath_ht_early_init(struct ipath_devdata *dd)
1526 } 1623 }
1527 1624
1528 ipath_get_eeprom_info(dd); 1625 ipath_get_eeprom_info(dd);
1529 if (dd->ipath_boardrev == 5 && dd->ipath_serial[0] == '1' && 1626 if (dd->ipath_boardrev == 5) {
1530 dd->ipath_serial[1] == '2' && dd->ipath_serial[2] == '8') {
1531 /* 1627 /*
1532 * Later production QHT7040 has same changes as QHT7140, so 1628 * Later production QHT7040 has same changes as QHT7140, so
1533 * can use GPIO interrupts. They have serial #'s starting 1629 * can use GPIO interrupts. They have serial #'s starting
@@ -1602,6 +1698,210 @@ static void ipath_ht_free_irq(struct ipath_devdata *dd)
1602 dd->ipath_intconfig = 0; 1698 dd->ipath_intconfig = 0;
1603} 1699}
1604 1700
1701static struct ipath_message_header *
1702ipath_ht_get_msgheader(struct ipath_devdata *dd, __le32 *rhf_addr)
1703{
1704 return (struct ipath_message_header *)
1705 &rhf_addr[sizeof(u64) / sizeof(u32)];
1706}
1707
1708static void ipath_ht_config_ports(struct ipath_devdata *dd, ushort cfgports)
1709{
1710 dd->ipath_portcnt =
1711 ipath_read_kreg32(dd, dd->ipath_kregs->kr_portcnt);
1712 dd->ipath_p0_rcvegrcnt =
1713 ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvegrcnt);
1714}
1715
1716static void ipath_ht_read_counters(struct ipath_devdata *dd,
1717 struct infinipath_counters *cntrs)
1718{
1719 cntrs->LBIntCnt =
1720 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(LBIntCnt));
1721 cntrs->LBFlowStallCnt =
1722 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(LBFlowStallCnt));
1723 cntrs->TxSDmaDescCnt = 0;
1724 cntrs->TxUnsupVLErrCnt =
1725 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxUnsupVLErrCnt));
1726 cntrs->TxDataPktCnt =
1727 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxDataPktCnt));
1728 cntrs->TxFlowPktCnt =
1729 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxFlowPktCnt));
1730 cntrs->TxDwordCnt =
1731 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxDwordCnt));
1732 cntrs->TxLenErrCnt =
1733 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxLenErrCnt));
1734 cntrs->TxMaxMinLenErrCnt =
1735 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxMaxMinLenErrCnt));
1736 cntrs->TxUnderrunCnt =
1737 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxUnderrunCnt));
1738 cntrs->TxFlowStallCnt =
1739 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxFlowStallCnt));
1740 cntrs->TxDroppedPktCnt =
1741 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxDroppedPktCnt));
1742 cntrs->RxDroppedPktCnt =
1743 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxDroppedPktCnt));
1744 cntrs->RxDataPktCnt =
1745 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxDataPktCnt));
1746 cntrs->RxFlowPktCnt =
1747 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxFlowPktCnt));
1748 cntrs->RxDwordCnt =
1749 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxDwordCnt));
1750 cntrs->RxLenErrCnt =
1751 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxLenErrCnt));
1752 cntrs->RxMaxMinLenErrCnt =
1753 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxMaxMinLenErrCnt));
1754 cntrs->RxICRCErrCnt =
1755 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxICRCErrCnt));
1756 cntrs->RxVCRCErrCnt =
1757 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxVCRCErrCnt));
1758 cntrs->RxFlowCtrlErrCnt =
1759 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxFlowCtrlErrCnt));
1760 cntrs->RxBadFormatCnt =
1761 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxBadFormatCnt));
1762 cntrs->RxLinkProblemCnt =
1763 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxLinkProblemCnt));
1764 cntrs->RxEBPCnt =
1765 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxEBPCnt));
1766 cntrs->RxLPCRCErrCnt =
1767 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxLPCRCErrCnt));
1768 cntrs->RxBufOvflCnt =
1769 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxBufOvflCnt));
1770 cntrs->RxTIDFullErrCnt =
1771 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxTIDFullErrCnt));
1772 cntrs->RxTIDValidErrCnt =
1773 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxTIDValidErrCnt));
1774 cntrs->RxPKeyMismatchCnt =
1775 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxPKeyMismatchCnt));
1776 cntrs->RxP0HdrEgrOvflCnt =
1777 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP0HdrEgrOvflCnt));
1778 cntrs->RxP1HdrEgrOvflCnt =
1779 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP1HdrEgrOvflCnt));
1780 cntrs->RxP2HdrEgrOvflCnt =
1781 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP2HdrEgrOvflCnt));
1782 cntrs->RxP3HdrEgrOvflCnt =
1783 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP3HdrEgrOvflCnt));
1784 cntrs->RxP4HdrEgrOvflCnt =
1785 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP4HdrEgrOvflCnt));
1786 cntrs->RxP5HdrEgrOvflCnt =
1787 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP5HdrEgrOvflCnt));
1788 cntrs->RxP6HdrEgrOvflCnt =
1789 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP6HdrEgrOvflCnt));
1790 cntrs->RxP7HdrEgrOvflCnt =
1791 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP7HdrEgrOvflCnt));
1792 cntrs->RxP8HdrEgrOvflCnt =
1793 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP8HdrEgrOvflCnt));
1794 cntrs->RxP9HdrEgrOvflCnt = 0;
1795 cntrs->RxP10HdrEgrOvflCnt = 0;
1796 cntrs->RxP11HdrEgrOvflCnt = 0;
1797 cntrs->RxP12HdrEgrOvflCnt = 0;
1798 cntrs->RxP13HdrEgrOvflCnt = 0;
1799 cntrs->RxP14HdrEgrOvflCnt = 0;
1800 cntrs->RxP15HdrEgrOvflCnt = 0;
1801 cntrs->RxP16HdrEgrOvflCnt = 0;
1802 cntrs->IBStatusChangeCnt =
1803 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(IBStatusChangeCnt));
1804 cntrs->IBLinkErrRecoveryCnt =
1805 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(IBLinkErrRecoveryCnt));
1806 cntrs->IBLinkDownedCnt =
1807 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(IBLinkDownedCnt));
1808 cntrs->IBSymbolErrCnt =
1809 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(IBSymbolErrCnt));
1810 cntrs->RxVL15DroppedPktCnt = 0;
1811 cntrs->RxOtherLocalPhyErrCnt = 0;
1812 cntrs->PcieRetryBufDiagQwordCnt = 0;
1813 cntrs->ExcessBufferOvflCnt = dd->ipath_overrun_thresh_errs;
1814 cntrs->LocalLinkIntegrityErrCnt =
1815 (dd->ipath_flags & IPATH_GPIO_ERRINTRS) ?
1816 dd->ipath_lli_errs : dd->ipath_lli_errors;
1817 cntrs->RxVlErrCnt = 0;
1818 cntrs->RxDlidFltrCnt = 0;
1819}
1820
1821
1822/* no interrupt fallback for these chips */
1823static int ipath_ht_nointr_fallback(struct ipath_devdata *dd)
1824{
1825 return 0;
1826}
1827
1828
1829/*
1830 * reset the XGXS (between serdes and IBC). Slightly less intrusive
1831 * than resetting the IBC or external link state, and useful in some
1832 * cases to cause some retraining. To do this right, we reset IBC
1833 * as well.
1834 */
1835static void ipath_ht_xgxs_reset(struct ipath_devdata *dd)
1836{
1837 u64 val, prev_val;
1838
1839 prev_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig);
1840 val = prev_val | INFINIPATH_XGXS_RESET;
1841 prev_val &= ~INFINIPATH_XGXS_RESET; /* be sure */
1842 ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
1843 dd->ipath_control & ~INFINIPATH_C_LINKENABLE);
1844 ipath_write_kreg(dd, dd->ipath_kregs->kr_xgxsconfig, val);
1845 ipath_read_kreg32(dd, dd->ipath_kregs->kr_scratch);
1846 ipath_write_kreg(dd, dd->ipath_kregs->kr_xgxsconfig, prev_val);
1847 ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
1848 dd->ipath_control);
1849}
1850
1851
1852static int ipath_ht_get_ib_cfg(struct ipath_devdata *dd, int which)
1853{
1854 int ret;
1855
1856 switch (which) {
1857 case IPATH_IB_CFG_LWID:
1858 ret = dd->ipath_link_width_active;
1859 break;
1860 case IPATH_IB_CFG_SPD:
1861 ret = dd->ipath_link_speed_active;
1862 break;
1863 case IPATH_IB_CFG_LWID_ENB:
1864 ret = dd->ipath_link_width_enabled;
1865 break;
1866 case IPATH_IB_CFG_SPD_ENB:
1867 ret = dd->ipath_link_speed_enabled;
1868 break;
1869 default:
1870 ret = -ENOTSUPP;
1871 break;
1872 }
1873 return ret;
1874}
1875
1876
1877/* we assume range checking is already done, if needed */
1878static int ipath_ht_set_ib_cfg(struct ipath_devdata *dd, int which, u32 val)
1879{
1880 int ret = 0;
1881
1882 if (which == IPATH_IB_CFG_LWID_ENB)
1883 dd->ipath_link_width_enabled = val;
1884 else if (which == IPATH_IB_CFG_SPD_ENB)
1885 dd->ipath_link_speed_enabled = val;
1886 else
1887 ret = -ENOTSUPP;
1888 return ret;
1889}
1890
1891
1892static void ipath_ht_config_jint(struct ipath_devdata *dd, u16 a, u16 b)
1893{
1894}
1895
1896
1897static int ipath_ht_ib_updown(struct ipath_devdata *dd, int ibup, u64 ibcs)
1898{
1899 ipath_setup_ht_setextled(dd, ipath_ib_linkstate(dd, ibcs),
1900 ipath_ib_linktrstate(dd, ibcs));
1901 return 0;
1902}
1903
1904
1605/** 1905/**
1606 * ipath_init_iba6110_funcs - set up the chip-specific function pointers 1906 * ipath_init_iba6110_funcs - set up the chip-specific function pointers
1607 * @dd: the infinipath device 1907 * @dd: the infinipath device
@@ -1626,22 +1926,19 @@ void ipath_init_iba6110_funcs(struct ipath_devdata *dd)
1626 dd->ipath_f_setextled = ipath_setup_ht_setextled; 1926 dd->ipath_f_setextled = ipath_setup_ht_setextled;
1627 dd->ipath_f_get_base_info = ipath_ht_get_base_info; 1927 dd->ipath_f_get_base_info = ipath_ht_get_base_info;
1628 dd->ipath_f_free_irq = ipath_ht_free_irq; 1928 dd->ipath_f_free_irq = ipath_ht_free_irq;
1629
1630 /*
1631 * initialize chip-specific variables
1632 */
1633 dd->ipath_f_tidtemplate = ipath_ht_tidtemplate; 1929 dd->ipath_f_tidtemplate = ipath_ht_tidtemplate;
1930 dd->ipath_f_intr_fallback = ipath_ht_nointr_fallback;
1931 dd->ipath_f_get_msgheader = ipath_ht_get_msgheader;
1932 dd->ipath_f_config_ports = ipath_ht_config_ports;
1933 dd->ipath_f_read_counters = ipath_ht_read_counters;
1934 dd->ipath_f_xgxs_reset = ipath_ht_xgxs_reset;
1935 dd->ipath_f_get_ib_cfg = ipath_ht_get_ib_cfg;
1936 dd->ipath_f_set_ib_cfg = ipath_ht_set_ib_cfg;
1937 dd->ipath_f_config_jint = ipath_ht_config_jint;
1938 dd->ipath_f_ib_updown = ipath_ht_ib_updown;
1634 1939
1635 /* 1940 /*
1636 * setup the register offsets, since they are different for each 1941 * initialize chip-specific variables
1637 * chip
1638 */
1639 dd->ipath_kregs = &ipath_ht_kregs;
1640 dd->ipath_cregs = &ipath_ht_cregs;
1641
1642 /*
1643 * do very early init that is needed before ipath_f_bus is
1644 * called
1645 */ 1942 */
1646 ipath_init_ht_variables(dd); 1943 ipath_init_ht_variables(dd);
1647} 1944}
diff --git a/drivers/infiniband/hw/ipath/ipath_iba6120.c b/drivers/infiniband/hw/ipath/ipath_iba6120.c
index 0103d6f4847b..c7a2f50824c0 100644
--- a/drivers/infiniband/hw/ipath/ipath_iba6120.c
+++ b/drivers/infiniband/hw/ipath/ipath_iba6120.c
@@ -145,10 +145,57 @@ struct _infinipath_do_not_use_kernel_regs {
145 unsigned long long Reserved12; 145 unsigned long long Reserved12;
146}; 146};
147 147
148#define IPATH_KREG_OFFSET(field) (offsetof(struct \ 148struct _infinipath_do_not_use_counters {
149 _infinipath_do_not_use_kernel_regs, field) / sizeof(u64)) 149 __u64 LBIntCnt;
150 __u64 LBFlowStallCnt;
151 __u64 Reserved1;
152 __u64 TxUnsupVLErrCnt;
153 __u64 TxDataPktCnt;
154 __u64 TxFlowPktCnt;
155 __u64 TxDwordCnt;
156 __u64 TxLenErrCnt;
157 __u64 TxMaxMinLenErrCnt;
158 __u64 TxUnderrunCnt;
159 __u64 TxFlowStallCnt;
160 __u64 TxDroppedPktCnt;
161 __u64 RxDroppedPktCnt;
162 __u64 RxDataPktCnt;
163 __u64 RxFlowPktCnt;
164 __u64 RxDwordCnt;
165 __u64 RxLenErrCnt;
166 __u64 RxMaxMinLenErrCnt;
167 __u64 RxICRCErrCnt;
168 __u64 RxVCRCErrCnt;
169 __u64 RxFlowCtrlErrCnt;
170 __u64 RxBadFormatCnt;
171 __u64 RxLinkProblemCnt;
172 __u64 RxEBPCnt;
173 __u64 RxLPCRCErrCnt;
174 __u64 RxBufOvflCnt;
175 __u64 RxTIDFullErrCnt;
176 __u64 RxTIDValidErrCnt;
177 __u64 RxPKeyMismatchCnt;
178 __u64 RxP0HdrEgrOvflCnt;
179 __u64 RxP1HdrEgrOvflCnt;
180 __u64 RxP2HdrEgrOvflCnt;
181 __u64 RxP3HdrEgrOvflCnt;
182 __u64 RxP4HdrEgrOvflCnt;
183 __u64 RxP5HdrEgrOvflCnt;
184 __u64 RxP6HdrEgrOvflCnt;
185 __u64 RxP7HdrEgrOvflCnt;
186 __u64 RxP8HdrEgrOvflCnt;
187 __u64 Reserved6;
188 __u64 Reserved7;
189 __u64 IBStatusChangeCnt;
190 __u64 IBLinkErrRecoveryCnt;
191 __u64 IBLinkDownedCnt;
192 __u64 IBSymbolErrCnt;
193};
194
195#define IPATH_KREG_OFFSET(field) (offsetof( \
196 struct _infinipath_do_not_use_kernel_regs, field) / sizeof(u64))
150#define IPATH_CREG_OFFSET(field) (offsetof( \ 197#define IPATH_CREG_OFFSET(field) (offsetof( \
151 struct infinipath_counters, field) / sizeof(u64)) 198 struct _infinipath_do_not_use_counters, field) / sizeof(u64))
152 199
153static const struct ipath_kregs ipath_pe_kregs = { 200static const struct ipath_kregs ipath_pe_kregs = {
154 .kr_control = IPATH_KREG_OFFSET(Control), 201 .kr_control = IPATH_KREG_OFFSET(Control),
@@ -282,6 +329,9 @@ static const struct ipath_cregs ipath_pe_cregs = {
282#define INFINIPATH_HWE_PCIE0PLLFAILED 0x0800000000000000ULL 329#define INFINIPATH_HWE_PCIE0PLLFAILED 0x0800000000000000ULL
283#define INFINIPATH_HWE_SERDESPLLFAILED 0x1000000000000000ULL 330#define INFINIPATH_HWE_SERDESPLLFAILED 0x1000000000000000ULL
284 331
332#define IBA6120_IBCS_LINKTRAININGSTATE_MASK 0xf
333#define IBA6120_IBCS_LINKSTATE_SHIFT 4
334
285/* kr_extstatus bits */ 335/* kr_extstatus bits */
286#define INFINIPATH_EXTS_FREQSEL 0x2 336#define INFINIPATH_EXTS_FREQSEL 0x2
287#define INFINIPATH_EXTS_SERDESSEL 0x4 337#define INFINIPATH_EXTS_SERDESSEL 0x4
@@ -296,6 +346,9 @@ static const struct ipath_cregs ipath_pe_cregs = {
296#define IPATH_GPIO_SCL (1ULL << \ 346#define IPATH_GPIO_SCL (1ULL << \
297 (_IPATH_GPIO_SCL_NUM+INFINIPATH_EXTC_GPIOOE_SHIFT)) 347 (_IPATH_GPIO_SCL_NUM+INFINIPATH_EXTC_GPIOOE_SHIFT))
298 348
349#define INFINIPATH_R_INTRAVAIL_SHIFT 16
350#define INFINIPATH_R_TAILUPD_SHIFT 31
351
299/* 6120 specific hardware errors... */ 352/* 6120 specific hardware errors... */
300static const struct ipath_hwerror_msgs ipath_6120_hwerror_msgs[] = { 353static const struct ipath_hwerror_msgs ipath_6120_hwerror_msgs[] = {
301 INFINIPATH_HWE_MSG(PCIEPOISONEDTLP, "PCIe Poisoned TLP"), 354 INFINIPATH_HWE_MSG(PCIEPOISONEDTLP, "PCIe Poisoned TLP"),
@@ -320,10 +373,28 @@ static const struct ipath_hwerror_msgs ipath_6120_hwerror_msgs[] = {
320 INFINIPATH_HWE_TXEMEMPARITYERR_PIOPBC) \ 373 INFINIPATH_HWE_TXEMEMPARITYERR_PIOPBC) \
321 << INFINIPATH_HWE_TXEMEMPARITYERR_SHIFT) 374 << INFINIPATH_HWE_TXEMEMPARITYERR_SHIFT)
322 375
323static int ipath_pe_txe_recover(struct ipath_devdata *);
324static void ipath_pe_put_tid_2(struct ipath_devdata *, u64 __iomem *, 376static void ipath_pe_put_tid_2(struct ipath_devdata *, u64 __iomem *,
325 u32, unsigned long); 377 u32, unsigned long);
326 378
379/*
380 * On platforms using this chip, and not having ordered WC stores, we
381 * can get TXE parity errors due to speculative reads to the PIO buffers,
382 * and this, due to a chip bug can result in (many) false parity error
383 * reports. So it's a debug print on those, and an info print on systems
384 * where the speculative reads don't occur.
385 */
386static void ipath_pe_txe_recover(struct ipath_devdata *dd)
387{
388 if (ipath_unordered_wc())
389 ipath_dbg("Recovering from TXE PIO parity error\n");
390 else {
391 ++ipath_stats.sps_txeparity;
392 dev_info(&dd->pcidev->dev,
393 "Recovering from TXE PIO parity error\n");
394 }
395}
396
397
327/** 398/**
328 * ipath_pe_handle_hwerrors - display hardware errors. 399 * ipath_pe_handle_hwerrors - display hardware errors.
329 * @dd: the infinipath device 400 * @dd: the infinipath device
@@ -403,35 +474,11 @@ static void ipath_pe_handle_hwerrors(struct ipath_devdata *dd, char *msg,
403 * occur if a processor speculative read is done to the PIO 474 * occur if a processor speculative read is done to the PIO
404 * buffer while we are sending a packet, for example. 475 * buffer while we are sending a packet, for example.
405 */ 476 */
406 if ((hwerrs & TXE_PIO_PARITY) && ipath_pe_txe_recover(dd)) 477 if (hwerrs & TXE_PIO_PARITY) {
478 ipath_pe_txe_recover(dd);
407 hwerrs &= ~TXE_PIO_PARITY; 479 hwerrs &= ~TXE_PIO_PARITY;
408 if (hwerrs) { 480 }
409 /* 481 if (!hwerrs) {
410 * if any set that we aren't ignoring only make the
411 * complaint once, in case it's stuck or recurring,
412 * and we get here multiple times
413 * Force link down, so switch knows, and
414 * LEDs are turned off
415 */
416 if (dd->ipath_flags & IPATH_INITTED) {
417 ipath_set_linkstate(dd, IPATH_IB_LINKDOWN);
418 ipath_setup_pe_setextled(dd,
419 INFINIPATH_IBCS_L_STATE_DOWN,
420 INFINIPATH_IBCS_LT_STATE_DISABLED);
421 ipath_dev_err(dd, "Fatal Hardware Error (freeze "
422 "mode), no longer usable, SN %.16s\n",
423 dd->ipath_serial);
424 isfatal = 1;
425 }
426 /*
427 * Mark as having had an error for driver, and also
428 * for /sys and status word mapped to user programs.
429 * This marks unit as not usable, until reset
430 */
431 *dd->ipath_statusp &= ~IPATH_STATUS_IB_READY;
432 *dd->ipath_statusp |= IPATH_STATUS_HWERROR;
433 dd->ipath_flags &= ~IPATH_INITTED;
434 } else {
435 static u32 freeze_cnt; 482 static u32 freeze_cnt;
436 483
437 freeze_cnt++; 484 freeze_cnt++;
@@ -485,7 +532,7 @@ static void ipath_pe_handle_hwerrors(struct ipath_devdata *dd, char *msg,
485 532
486 if (hwerrs & INFINIPATH_HWE_SERDESPLLFAILED) { 533 if (hwerrs & INFINIPATH_HWE_SERDESPLLFAILED) {
487 /* 534 /*
488 * If it occurs, it is left masked since the eternal 535 * If it occurs, it is left masked since the external
489 * interface is unused 536 * interface is unused
490 */ 537 */
491 dd->ipath_hwerrmask &= ~INFINIPATH_HWE_SERDESPLLFAILED; 538 dd->ipath_hwerrmask &= ~INFINIPATH_HWE_SERDESPLLFAILED;
@@ -563,6 +610,14 @@ static int ipath_pe_boardname(struct ipath_devdata *dd, char *name,
563 dd->ipath_f_put_tid = ipath_pe_put_tid_2; 610 dd->ipath_f_put_tid = ipath_pe_put_tid_2;
564 } 611 }
565 612
613
614 /*
615 * set here, not in ipath_init_*_funcs because we have to do
616 * it after we can read chip registers.
617 */
618 dd->ipath_ureg_align =
619 ipath_read_kreg32(dd, dd->ipath_kregs->kr_pagealign);
620
566 return ret; 621 return ret;
567} 622}
568 623
@@ -667,17 +722,8 @@ static int ipath_pe_bringup_serdes(struct ipath_devdata *dd)
667 722
668 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig); 723 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig);
669 prev_val = val; 724 prev_val = val;
670 if (((val >> INFINIPATH_XGXS_MDIOADDR_SHIFT) & 725 if (val & INFINIPATH_XGXS_RESET)
671 INFINIPATH_XGXS_MDIOADDR_MASK) != 3) {
672 val &=
673 ~(INFINIPATH_XGXS_MDIOADDR_MASK <<
674 INFINIPATH_XGXS_MDIOADDR_SHIFT);
675 /* MDIO address 3 */
676 val |= 3ULL << INFINIPATH_XGXS_MDIOADDR_SHIFT;
677 }
678 if (val & INFINIPATH_XGXS_RESET) {
679 val &= ~INFINIPATH_XGXS_RESET; 726 val &= ~INFINIPATH_XGXS_RESET;
680 }
681 if (((val >> INFINIPATH_XGXS_RX_POL_SHIFT) & 727 if (((val >> INFINIPATH_XGXS_RX_POL_SHIFT) &
682 INFINIPATH_XGXS_RX_POL_MASK) != dd->ipath_rx_pol_inv ) { 728 INFINIPATH_XGXS_RX_POL_MASK) != dd->ipath_rx_pol_inv ) {
683 /* need to compensate for Tx inversion in partner */ 729 /* need to compensate for Tx inversion in partner */
@@ -707,21 +753,6 @@ static int ipath_pe_bringup_serdes(struct ipath_devdata *dd)
707 (unsigned long long) 753 (unsigned long long)
708 ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig)); 754 ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig));
709 755
710 if (!ipath_waitfor_mdio_cmdready(dd)) {
711 ipath_write_kreg(
712 dd, dd->ipath_kregs->kr_mdio,
713 ipath_mdio_req(IPATH_MDIO_CMD_READ, 31,
714 IPATH_MDIO_CTRL_XGXS_REG_8, 0));
715 if (ipath_waitfor_complete(dd, dd->ipath_kregs->kr_mdio,
716 IPATH_MDIO_DATAVALID, &val))
717 ipath_dbg("Never got MDIO data for XGXS "
718 "status read\n");
719 else
720 ipath_cdbg(VERBOSE, "MDIO Read reg8, "
721 "'bank' 31 %x\n", (u32) val);
722 } else
723 ipath_dbg("Never got MDIO cmdready for XGXS status read\n");
724
725 return ret; 756 return ret;
726} 757}
727 758
@@ -902,12 +933,27 @@ static int ipath_setup_pe_config(struct ipath_devdata *dd,
902 else 933 else
903 ipath_dev_err(dd, "Can't find PCI Express " 934 ipath_dev_err(dd, "Can't find PCI Express "
904 "capability!\n"); 935 "capability!\n");
936
937 dd->ipath_link_width_supported = IB_WIDTH_1X | IB_WIDTH_4X;
938 dd->ipath_link_speed_supported = IPATH_IB_SDR;
939 dd->ipath_link_width_enabled = IB_WIDTH_4X;
940 dd->ipath_link_speed_enabled = dd->ipath_link_speed_supported;
941 /* these can't change for this chip, so set once */
942 dd->ipath_link_width_active = dd->ipath_link_width_enabled;
943 dd->ipath_link_speed_active = dd->ipath_link_speed_enabled;
905 return 0; 944 return 0;
906} 945}
907 946
908static void ipath_init_pe_variables(struct ipath_devdata *dd) 947static void ipath_init_pe_variables(struct ipath_devdata *dd)
909{ 948{
910 /* 949 /*
950 * setup the register offsets, since they are different for each
951 * chip
952 */
953 dd->ipath_kregs = &ipath_pe_kregs;
954 dd->ipath_cregs = &ipath_pe_cregs;
955
956 /*
911 * bits for selecting i2c direction and values, 957 * bits for selecting i2c direction and values,
912 * used for I2C serial flash 958 * used for I2C serial flash
913 */ 959 */
@@ -916,6 +962,43 @@ static void ipath_init_pe_variables(struct ipath_devdata *dd)
916 dd->ipath_gpio_sda = IPATH_GPIO_SDA; 962 dd->ipath_gpio_sda = IPATH_GPIO_SDA;
917 dd->ipath_gpio_scl = IPATH_GPIO_SCL; 963 dd->ipath_gpio_scl = IPATH_GPIO_SCL;
918 964
965 /*
966 * Fill in data for field-values that change in newer chips.
967 * We dynamically specify only the mask for LINKTRAININGSTATE
968 * and only the shift for LINKSTATE, as they are the only ones
969 * that change. Also precalculate the 3 link states of interest
970 * and the combined mask.
971 */
972 dd->ibcs_ls_shift = IBA6120_IBCS_LINKSTATE_SHIFT;
973 dd->ibcs_lts_mask = IBA6120_IBCS_LINKTRAININGSTATE_MASK;
974 dd->ibcs_mask = (INFINIPATH_IBCS_LINKSTATE_MASK <<
975 dd->ibcs_ls_shift) | dd->ibcs_lts_mask;
976 dd->ib_init = (INFINIPATH_IBCS_LT_STATE_LINKUP <<
977 INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) |
978 (INFINIPATH_IBCS_L_STATE_INIT << dd->ibcs_ls_shift);
979 dd->ib_arm = (INFINIPATH_IBCS_LT_STATE_LINKUP <<
980 INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) |
981 (INFINIPATH_IBCS_L_STATE_ARM << dd->ibcs_ls_shift);
982 dd->ib_active = (INFINIPATH_IBCS_LT_STATE_LINKUP <<
983 INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) |
984 (INFINIPATH_IBCS_L_STATE_ACTIVE << dd->ibcs_ls_shift);
985
986 /*
987 * Fill in data for ibcc field-values that change in newer chips.
988 * We dynamically specify only the mask for LINKINITCMD
989 * and only the shift for LINKCMD and MAXPKTLEN, as they are
990 * the only ones that change.
991 */
992 dd->ibcc_lic_mask = INFINIPATH_IBCC_LINKINITCMD_MASK;
993 dd->ibcc_lc_shift = INFINIPATH_IBCC_LINKCMD_SHIFT;
994 dd->ibcc_mpl_shift = INFINIPATH_IBCC_MAXPKTLEN_SHIFT;
995
996 /* Fill in shifts for RcvCtrl. */
997 dd->ipath_r_portenable_shift = INFINIPATH_R_PORTENABLE_SHIFT;
998 dd->ipath_r_intravail_shift = INFINIPATH_R_INTRAVAIL_SHIFT;
999 dd->ipath_r_tailupd_shift = INFINIPATH_R_TAILUPD_SHIFT;
1000 dd->ipath_r_portcfg_shift = 0; /* Not on IBA6120 */
1001
919 /* variables for sanity checking interrupt and errors */ 1002 /* variables for sanity checking interrupt and errors */
920 dd->ipath_hwe_bitsextant = 1003 dd->ipath_hwe_bitsextant =
921 (INFINIPATH_HWE_RXEMEMPARITYERR_MASK << 1004 (INFINIPATH_HWE_RXEMEMPARITYERR_MASK <<
@@ -963,6 +1046,8 @@ static void ipath_init_pe_variables(struct ipath_devdata *dd)
963 1046
964 dd->ipath_i_rcvavail_mask = INFINIPATH_I_RCVAVAIL_MASK; 1047 dd->ipath_i_rcvavail_mask = INFINIPATH_I_RCVAVAIL_MASK;
965 dd->ipath_i_rcvurg_mask = INFINIPATH_I_RCVURG_MASK; 1048 dd->ipath_i_rcvurg_mask = INFINIPATH_I_RCVURG_MASK;
1049 dd->ipath_i_rcvavail_shift = INFINIPATH_I_RCVAVAIL_SHIFT;
1050 dd->ipath_i_rcvurg_shift = INFINIPATH_I_RCVURG_SHIFT;
966 1051
967 /* 1052 /*
968 * EEPROM error log 0 is TXE Parity errors. 1 is RXE Parity. 1053 * EEPROM error log 0 is TXE Parity errors. 1 is RXE Parity.
@@ -984,6 +1069,7 @@ static void ipath_init_pe_variables(struct ipath_devdata *dd)
984 INFINIPATH_E_INVALIDADDR | INFINIPATH_E_RESET; 1069 INFINIPATH_E_INVALIDADDR | INFINIPATH_E_RESET;
985 1070
986 1071
1072 dd->delay_mult = 2; /* SDR, 4X, can't change */
987} 1073}
988 1074
989/* setup the MSI stuff again after a reset. I'd like to just call 1075/* setup the MSI stuff again after a reset. I'd like to just call
@@ -1289,6 +1375,9 @@ static int ipath_pe_early_init(struct ipath_devdata *dd)
1289 */ 1375 */
1290 dd->ipath_rcvhdrentsize = 24; 1376 dd->ipath_rcvhdrentsize = 24;
1291 dd->ipath_rcvhdrsize = IPATH_DFLT_RCVHDRSIZE; 1377 dd->ipath_rcvhdrsize = IPATH_DFLT_RCVHDRSIZE;
1378 dd->ipath_rhf_offset = 0;
1379 dd->ipath_egrtidbase = (u64 __iomem *)
1380 ((char __iomem *) dd->ipath_kregbase + dd->ipath_rcvegrbase);
1292 1381
1293 /* 1382 /*
1294 * To truly support a 4KB MTU (for usermode), we need to 1383 * To truly support a 4KB MTU (for usermode), we need to
@@ -1359,34 +1448,204 @@ static void ipath_pe_free_irq(struct ipath_devdata *dd)
1359 dd->ipath_irq = 0; 1448 dd->ipath_irq = 0;
1360} 1449}
1361 1450
1451
1452static struct ipath_message_header *
1453ipath_pe_get_msgheader(struct ipath_devdata *dd, __le32 *rhf_addr)
1454{
1455 return (struct ipath_message_header *)
1456 &rhf_addr[sizeof(u64) / sizeof(u32)];
1457}
1458
1459static void ipath_pe_config_ports(struct ipath_devdata *dd, ushort cfgports)
1460{
1461 dd->ipath_portcnt =
1462 ipath_read_kreg32(dd, dd->ipath_kregs->kr_portcnt);
1463 dd->ipath_p0_rcvegrcnt =
1464 ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvegrcnt);
1465}
1466
1467static void ipath_pe_read_counters(struct ipath_devdata *dd,
1468 struct infinipath_counters *cntrs)
1469{
1470 cntrs->LBIntCnt =
1471 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(LBIntCnt));
1472 cntrs->LBFlowStallCnt =
1473 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(LBFlowStallCnt));
1474 cntrs->TxSDmaDescCnt = 0;
1475 cntrs->TxUnsupVLErrCnt =
1476 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxUnsupVLErrCnt));
1477 cntrs->TxDataPktCnt =
1478 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxDataPktCnt));
1479 cntrs->TxFlowPktCnt =
1480 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxFlowPktCnt));
1481 cntrs->TxDwordCnt =
1482 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxDwordCnt));
1483 cntrs->TxLenErrCnt =
1484 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxLenErrCnt));
1485 cntrs->TxMaxMinLenErrCnt =
1486 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxMaxMinLenErrCnt));
1487 cntrs->TxUnderrunCnt =
1488 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxUnderrunCnt));
1489 cntrs->TxFlowStallCnt =
1490 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxFlowStallCnt));
1491 cntrs->TxDroppedPktCnt =
1492 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(TxDroppedPktCnt));
1493 cntrs->RxDroppedPktCnt =
1494 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxDroppedPktCnt));
1495 cntrs->RxDataPktCnt =
1496 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxDataPktCnt));
1497 cntrs->RxFlowPktCnt =
1498 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxFlowPktCnt));
1499 cntrs->RxDwordCnt =
1500 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxDwordCnt));
1501 cntrs->RxLenErrCnt =
1502 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxLenErrCnt));
1503 cntrs->RxMaxMinLenErrCnt =
1504 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxMaxMinLenErrCnt));
1505 cntrs->RxICRCErrCnt =
1506 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxICRCErrCnt));
1507 cntrs->RxVCRCErrCnt =
1508 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxVCRCErrCnt));
1509 cntrs->RxFlowCtrlErrCnt =
1510 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxFlowCtrlErrCnt));
1511 cntrs->RxBadFormatCnt =
1512 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxBadFormatCnt));
1513 cntrs->RxLinkProblemCnt =
1514 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxLinkProblemCnt));
1515 cntrs->RxEBPCnt =
1516 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxEBPCnt));
1517 cntrs->RxLPCRCErrCnt =
1518 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxLPCRCErrCnt));
1519 cntrs->RxBufOvflCnt =
1520 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxBufOvflCnt));
1521 cntrs->RxTIDFullErrCnt =
1522 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxTIDFullErrCnt));
1523 cntrs->RxTIDValidErrCnt =
1524 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxTIDValidErrCnt));
1525 cntrs->RxPKeyMismatchCnt =
1526 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxPKeyMismatchCnt));
1527 cntrs->RxP0HdrEgrOvflCnt =
1528 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP0HdrEgrOvflCnt));
1529 cntrs->RxP1HdrEgrOvflCnt =
1530 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP1HdrEgrOvflCnt));
1531 cntrs->RxP2HdrEgrOvflCnt =
1532 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP2HdrEgrOvflCnt));
1533 cntrs->RxP3HdrEgrOvflCnt =
1534 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP3HdrEgrOvflCnt));
1535 cntrs->RxP4HdrEgrOvflCnt =
1536 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(RxP4HdrEgrOvflCnt));
1537 cntrs->RxP5HdrEgrOvflCnt = 0;
1538 cntrs->RxP6HdrEgrOvflCnt = 0;
1539 cntrs->RxP7HdrEgrOvflCnt = 0;
1540 cntrs->RxP8HdrEgrOvflCnt = 0;
1541 cntrs->RxP9HdrEgrOvflCnt = 0;
1542 cntrs->RxP10HdrEgrOvflCnt = 0;
1543 cntrs->RxP11HdrEgrOvflCnt = 0;
1544 cntrs->RxP12HdrEgrOvflCnt = 0;
1545 cntrs->RxP13HdrEgrOvflCnt = 0;
1546 cntrs->RxP14HdrEgrOvflCnt = 0;
1547 cntrs->RxP15HdrEgrOvflCnt = 0;
1548 cntrs->RxP16HdrEgrOvflCnt = 0;
1549 cntrs->IBStatusChangeCnt =
1550 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(IBStatusChangeCnt));
1551 cntrs->IBLinkErrRecoveryCnt =
1552 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(IBLinkErrRecoveryCnt));
1553 cntrs->IBLinkDownedCnt =
1554 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(IBLinkDownedCnt));
1555 cntrs->IBSymbolErrCnt =
1556 ipath_snap_cntr(dd, IPATH_CREG_OFFSET(IBSymbolErrCnt));
1557 cntrs->RxVL15DroppedPktCnt = 0;
1558 cntrs->RxOtherLocalPhyErrCnt = 0;
1559 cntrs->PcieRetryBufDiagQwordCnt = 0;
1560 cntrs->ExcessBufferOvflCnt = dd->ipath_overrun_thresh_errs;
1561 cntrs->LocalLinkIntegrityErrCnt = dd->ipath_lli_errs;
1562 cntrs->RxVlErrCnt = 0;
1563 cntrs->RxDlidFltrCnt = 0;
1564}
1565
1566
1567/* no interrupt fallback for these chips */
1568static int ipath_pe_nointr_fallback(struct ipath_devdata *dd)
1569{
1570 return 0;
1571}
1572
1573
1362/* 1574/*
1363 * On platforms using this chip, and not having ordered WC stores, we 1575 * reset the XGXS (between serdes and IBC). Slightly less intrusive
1364 * can get TXE parity errors due to speculative reads to the PIO buffers, 1576 * than resetting the IBC or external link state, and useful in some
1365 * and this, due to a chip bug can result in (many) false parity error 1577 * cases to cause some retraining. To do this right, we reset IBC
1366 * reports. So it's a debug print on those, and an info print on systems 1578 * as well.
1367 * where the speculative reads don't occur.
1368 * Because we can get lots of false errors, we have no upper limit
1369 * on recovery attempts on those platforms.
1370 */ 1579 */
1371static int ipath_pe_txe_recover(struct ipath_devdata *dd) 1580static void ipath_pe_xgxs_reset(struct ipath_devdata *dd)
1372{ 1581{
1373 if (ipath_unordered_wc()) 1582 u64 val, prev_val;
1374 ipath_dbg("Recovering from TXE PIO parity error\n"); 1583
1375 else { 1584 prev_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig);
1376 int cnt = ++ipath_stats.sps_txeparity; 1585 val = prev_val | INFINIPATH_XGXS_RESET;
1377 if (cnt >= IPATH_MAX_PARITY_ATTEMPTS) { 1586 prev_val &= ~INFINIPATH_XGXS_RESET; /* be sure */
1378 if (cnt == IPATH_MAX_PARITY_ATTEMPTS) 1587 ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
1379 ipath_dev_err(dd, 1588 dd->ipath_control & ~INFINIPATH_C_LINKENABLE);
1380 "Too many attempts to recover from " 1589 ipath_write_kreg(dd, dd->ipath_kregs->kr_xgxsconfig, val);
1381 "TXE parity, giving up\n"); 1590 ipath_read_kreg32(dd, dd->ipath_kregs->kr_scratch);
1382 return 0; 1591 ipath_write_kreg(dd, dd->ipath_kregs->kr_xgxsconfig, prev_val);
1383 } 1592 ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
1384 dev_info(&dd->pcidev->dev, 1593 dd->ipath_control);
1385 "Recovering from TXE PIO parity error\n"); 1594}
1595
1596
1597static int ipath_pe_get_ib_cfg(struct ipath_devdata *dd, int which)
1598{
1599 int ret;
1600
1601 switch (which) {
1602 case IPATH_IB_CFG_LWID:
1603 ret = dd->ipath_link_width_active;
1604 break;
1605 case IPATH_IB_CFG_SPD:
1606 ret = dd->ipath_link_speed_active;
1607 break;
1608 case IPATH_IB_CFG_LWID_ENB:
1609 ret = dd->ipath_link_width_enabled;
1610 break;
1611 case IPATH_IB_CFG_SPD_ENB:
1612 ret = dd->ipath_link_speed_enabled;
1613 break;
1614 default:
1615 ret = -ENOTSUPP;
1616 break;
1386 } 1617 }
1387 return 1; 1618 return ret;
1619}
1620
1621
1622/* we assume range checking is already done, if needed */
1623static int ipath_pe_set_ib_cfg(struct ipath_devdata *dd, int which, u32 val)
1624{
1625 int ret = 0;
1626
1627 if (which == IPATH_IB_CFG_LWID_ENB)
1628 dd->ipath_link_width_enabled = val;
1629 else if (which == IPATH_IB_CFG_SPD_ENB)
1630 dd->ipath_link_speed_enabled = val;
1631 else
1632 ret = -ENOTSUPP;
1633 return ret;
1388} 1634}
1389 1635
1636static void ipath_pe_config_jint(struct ipath_devdata *dd, u16 a, u16 b)
1637{
1638}
1639
1640
1641static int ipath_pe_ib_updown(struct ipath_devdata *dd, int ibup, u64 ibcs)
1642{
1643 ipath_setup_pe_setextled(dd, ipath_ib_linkstate(dd, ibcs),
1644 ipath_ib_linktrstate(dd, ibcs));
1645 return 0;
1646}
1647
1648
1390/** 1649/**
1391 * ipath_init_iba6120_funcs - set up the chip-specific function pointers 1650 * ipath_init_iba6120_funcs - set up the chip-specific function pointers
1392 * @dd: the infinipath device 1651 * @dd: the infinipath device
@@ -1407,7 +1666,7 @@ void ipath_init_iba6120_funcs(struct ipath_devdata *dd)
1407 dd->ipath_f_bringup_serdes = ipath_pe_bringup_serdes; 1666 dd->ipath_f_bringup_serdes = ipath_pe_bringup_serdes;
1408 dd->ipath_f_clear_tids = ipath_pe_clear_tids; 1667 dd->ipath_f_clear_tids = ipath_pe_clear_tids;
1409 /* 1668 /*
1410 * this may get changed after we read the chip revision, 1669 * _f_put_tid may get changed after we read the chip revision,
1411 * but we start with the safe version for all revs 1670 * but we start with the safe version for all revs
1412 */ 1671 */
1413 dd->ipath_f_put_tid = ipath_pe_put_tid; 1672 dd->ipath_f_put_tid = ipath_pe_put_tid;
@@ -1415,17 +1674,19 @@ void ipath_init_iba6120_funcs(struct ipath_devdata *dd)
1415 dd->ipath_f_setextled = ipath_setup_pe_setextled; 1674 dd->ipath_f_setextled = ipath_setup_pe_setextled;
1416 dd->ipath_f_get_base_info = ipath_pe_get_base_info; 1675 dd->ipath_f_get_base_info = ipath_pe_get_base_info;
1417 dd->ipath_f_free_irq = ipath_pe_free_irq; 1676 dd->ipath_f_free_irq = ipath_pe_free_irq;
1418
1419 /* initialize chip-specific variables */
1420 dd->ipath_f_tidtemplate = ipath_pe_tidtemplate; 1677 dd->ipath_f_tidtemplate = ipath_pe_tidtemplate;
1678 dd->ipath_f_intr_fallback = ipath_pe_nointr_fallback;
1679 dd->ipath_f_xgxs_reset = ipath_pe_xgxs_reset;
1680 dd->ipath_f_get_msgheader = ipath_pe_get_msgheader;
1681 dd->ipath_f_config_ports = ipath_pe_config_ports;
1682 dd->ipath_f_read_counters = ipath_pe_read_counters;
1683 dd->ipath_f_get_ib_cfg = ipath_pe_get_ib_cfg;
1684 dd->ipath_f_set_ib_cfg = ipath_pe_set_ib_cfg;
1685 dd->ipath_f_config_jint = ipath_pe_config_jint;
1686 dd->ipath_f_ib_updown = ipath_pe_ib_updown;
1421 1687
1422 /*
1423 * setup the register offsets, since they are different for each
1424 * chip
1425 */
1426 dd->ipath_kregs = &ipath_pe_kregs;
1427 dd->ipath_cregs = &ipath_pe_cregs;
1428 1688
1689 /* initialize chip-specific variables */
1429 ipath_init_pe_variables(dd); 1690 ipath_init_pe_variables(dd);
1430} 1691}
1431 1692
diff --git a/drivers/infiniband/hw/ipath/ipath_init_chip.c b/drivers/infiniband/hw/ipath/ipath_init_chip.c
index 9dd0bacf8461..4471674975cd 100644
--- a/drivers/infiniband/hw/ipath/ipath_init_chip.c
+++ b/drivers/infiniband/hw/ipath/ipath_init_chip.c
@@ -91,7 +91,7 @@ static int create_port0_egr(struct ipath_devdata *dd)
91 struct ipath_skbinfo *skbinfo; 91 struct ipath_skbinfo *skbinfo;
92 int ret; 92 int ret;
93 93
94 egrcnt = dd->ipath_rcvegrcnt; 94 egrcnt = dd->ipath_p0_rcvegrcnt;
95 95
96 skbinfo = vmalloc(sizeof(*dd->ipath_port0_skbinfo) * egrcnt); 96 skbinfo = vmalloc(sizeof(*dd->ipath_port0_skbinfo) * egrcnt);
97 if (skbinfo == NULL) { 97 if (skbinfo == NULL) {
@@ -244,8 +244,7 @@ static int init_chip_first(struct ipath_devdata *dd,
244 * cfgports. We do still check and report a difference, if 244 * cfgports. We do still check and report a difference, if
245 * not same (should be impossible). 245 * not same (should be impossible).
246 */ 246 */
247 dd->ipath_portcnt = 247 dd->ipath_f_config_ports(dd, ipath_cfgports);
248 ipath_read_kreg32(dd, dd->ipath_kregs->kr_portcnt);
249 if (!ipath_cfgports) 248 if (!ipath_cfgports)
250 dd->ipath_cfgports = dd->ipath_portcnt; 249 dd->ipath_cfgports = dd->ipath_portcnt;
251 else if (ipath_cfgports <= dd->ipath_portcnt) { 250 else if (ipath_cfgports <= dd->ipath_portcnt) {
@@ -272,22 +271,7 @@ static int init_chip_first(struct ipath_devdata *dd,
272 goto done; 271 goto done;
273 } 272 }
274 273
275 dd->ipath_lastegrheads = kzalloc(sizeof(*dd->ipath_lastegrheads)
276 * dd->ipath_cfgports,
277 GFP_KERNEL);
278 dd->ipath_lastrcvhdrqtails =
279 kzalloc(sizeof(*dd->ipath_lastrcvhdrqtails)
280 * dd->ipath_cfgports, GFP_KERNEL);
281
282 if (!dd->ipath_lastegrheads || !dd->ipath_lastrcvhdrqtails) {
283 ipath_dev_err(dd, "Unable to allocate head arrays, "
284 "failing\n");
285 ret = -ENOMEM;
286 goto done;
287 }
288
289 pd = create_portdata0(dd); 274 pd = create_portdata0(dd);
290
291 if (!pd) { 275 if (!pd) {
292 ipath_dev_err(dd, "Unable to allocate portdata for port " 276 ipath_dev_err(dd, "Unable to allocate portdata for port "
293 "0, failing\n"); 277 "0, failing\n");
@@ -345,10 +329,10 @@ static int init_chip_first(struct ipath_devdata *dd,
345 dd->ipath_piobcnt2k, dd->ipath_pio2kbase); 329 dd->ipath_piobcnt2k, dd->ipath_pio2kbase);
346 330
347 spin_lock_init(&dd->ipath_tid_lock); 331 spin_lock_init(&dd->ipath_tid_lock);
348 332 spin_lock_init(&dd->ipath_sendctrl_lock);
349 spin_lock_init(&dd->ipath_gpio_lock); 333 spin_lock_init(&dd->ipath_gpio_lock);
350 spin_lock_init(&dd->ipath_eep_st_lock); 334 spin_lock_init(&dd->ipath_eep_st_lock);
351 sema_init(&dd->ipath_eep_sem, 1); 335 mutex_init(&dd->ipath_eep_lock);
352 336
353done: 337done:
354 *pdp = pd; 338 *pdp = pd;
@@ -372,9 +356,9 @@ static int init_chip_reset(struct ipath_devdata *dd,
372 *pdp = dd->ipath_pd[0]; 356 *pdp = dd->ipath_pd[0];
373 /* ensure chip does no sends or receives while we re-initialize */ 357 /* ensure chip does no sends or receives while we re-initialize */
374 dd->ipath_control = dd->ipath_sendctrl = dd->ipath_rcvctrl = 0U; 358 dd->ipath_control = dd->ipath_sendctrl = dd->ipath_rcvctrl = 0U;
375 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, 0); 359 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, dd->ipath_rcvctrl);
376 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 0); 360 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
377 ipath_write_kreg(dd, dd->ipath_kregs->kr_control, 0); 361 ipath_write_kreg(dd, dd->ipath_kregs->kr_control, dd->ipath_control);
378 362
379 rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_portcnt); 363 rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_portcnt);
380 if (dd->ipath_portcnt != rtmp) 364 if (dd->ipath_portcnt != rtmp)
@@ -487,6 +471,7 @@ static void enable_chip(struct ipath_devdata *dd,
487 struct ipath_portdata *pd, int reinit) 471 struct ipath_portdata *pd, int reinit)
488{ 472{
489 u32 val; 473 u32 val;
474 unsigned long flags;
490 int i; 475 int i;
491 476
492 if (!reinit) 477 if (!reinit)
@@ -495,19 +480,21 @@ static void enable_chip(struct ipath_devdata *dd,
495 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, 480 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
496 dd->ipath_rcvctrl); 481 dd->ipath_rcvctrl);
497 482
483 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
498 /* Enable PIO send, and update of PIOavail regs to memory. */ 484 /* Enable PIO send, and update of PIOavail regs to memory. */
499 dd->ipath_sendctrl = INFINIPATH_S_PIOENABLE | 485 dd->ipath_sendctrl = INFINIPATH_S_PIOENABLE |
500 INFINIPATH_S_PIOBUFAVAILUPD; 486 INFINIPATH_S_PIOBUFAVAILUPD;
501 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 487 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
502 dd->ipath_sendctrl); 488 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
489 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
503 490
504 /* 491 /*
505 * enable port 0 receive, and receive interrupt. other ports 492 * enable port 0 receive, and receive interrupt. other ports
506 * done as user opens and inits them. 493 * done as user opens and inits them.
507 */ 494 */
508 dd->ipath_rcvctrl = INFINIPATH_R_TAILUPD | 495 dd->ipath_rcvctrl = (1ULL << dd->ipath_r_tailupd_shift) |
509 (1ULL << INFINIPATH_R_PORTENABLE_SHIFT) | 496 (1ULL << dd->ipath_r_portenable_shift) |
510 (1ULL << INFINIPATH_R_INTRAVAIL_SHIFT); 497 (1ULL << dd->ipath_r_intravail_shift);
511 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, 498 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
512 dd->ipath_rcvctrl); 499 dd->ipath_rcvctrl);
513 500
@@ -523,12 +510,11 @@ static void enable_chip(struct ipath_devdata *dd,
523 */ 510 */
524 val = ipath_read_ureg32(dd, ur_rcvegrindextail, 0); 511 val = ipath_read_ureg32(dd, ur_rcvegrindextail, 0);
525 (void)ipath_write_ureg(dd, ur_rcvegrindexhead, val, 0); 512 (void)ipath_write_ureg(dd, ur_rcvegrindexhead, val, 0);
526 dd->ipath_port0head = ipath_read_ureg32(dd, ur_rcvhdrtail, 0);
527 513
528 /* Initialize so we interrupt on next packet received */ 514 /* Initialize so we interrupt on next packet received */
529 (void)ipath_write_ureg(dd, ur_rcvhdrhead, 515 (void)ipath_write_ureg(dd, ur_rcvhdrhead,
530 dd->ipath_rhdrhead_intr_off | 516 dd->ipath_rhdrhead_intr_off |
531 dd->ipath_port0head, 0); 517 dd->ipath_pd[0]->port_head, 0);
532 518
533 /* 519 /*
534 * by now pioavail updates to memory should have occurred, so 520 * by now pioavail updates to memory should have occurred, so
@@ -542,12 +528,8 @@ static void enable_chip(struct ipath_devdata *dd,
542 /* 528 /*
543 * Chip Errata bug 6641; even and odd qwords>3 are swapped. 529 * Chip Errata bug 6641; even and odd qwords>3 are swapped.
544 */ 530 */
545 if (i > 3) { 531 if (i > 3 && (dd->ipath_flags & IPATH_SWAP_PIOBUFS))
546 if (i & 1) 532 val = dd->ipath_pioavailregs_dma[i ^ 1];
547 val = dd->ipath_pioavailregs_dma[i - 1];
548 else
549 val = dd->ipath_pioavailregs_dma[i + 1];
550 }
551 else 533 else
552 val = dd->ipath_pioavailregs_dma[i]; 534 val = dd->ipath_pioavailregs_dma[i];
553 dd->ipath_pioavailshadow[i] = le64_to_cpu(val); 535 dd->ipath_pioavailshadow[i] = le64_to_cpu(val);
@@ -690,12 +672,13 @@ done:
690 */ 672 */
691int ipath_init_chip(struct ipath_devdata *dd, int reinit) 673int ipath_init_chip(struct ipath_devdata *dd, int reinit)
692{ 674{
693 int ret = 0, i; 675 int ret = 0;
694 u32 val32, kpiobufs; 676 u32 val32, kpiobufs;
695 u32 piobufs, uports; 677 u32 piobufs, uports;
696 u64 val; 678 u64 val;
697 struct ipath_portdata *pd = NULL; /* keep gcc4 happy */ 679 struct ipath_portdata *pd = NULL; /* keep gcc4 happy */
698 gfp_t gfp_flags = GFP_USER | __GFP_COMP; 680 gfp_t gfp_flags = GFP_USER | __GFP_COMP;
681 unsigned long flags;
699 682
700 ret = init_housekeeping(dd, &pd, reinit); 683 ret = init_housekeeping(dd, &pd, reinit);
701 if (ret) 684 if (ret)
@@ -746,7 +729,7 @@ int ipath_init_chip(struct ipath_devdata *dd, int reinit)
746 kpiobufs = ipath_kpiobufs; 729 kpiobufs = ipath_kpiobufs;
747 730
748 if (kpiobufs + (uports * IPATH_MIN_USER_PORT_BUFCNT) > piobufs) { 731 if (kpiobufs + (uports * IPATH_MIN_USER_PORT_BUFCNT) > piobufs) {
749 i = (int) piobufs - 732 int i = (int) piobufs -
750 (int) (uports * IPATH_MIN_USER_PORT_BUFCNT); 733 (int) (uports * IPATH_MIN_USER_PORT_BUFCNT);
751 if (i < 0) 734 if (i < 0)
752 i = 0; 735 i = 0;
@@ -827,8 +810,12 @@ int ipath_init_chip(struct ipath_devdata *dd, int reinit)
827 ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrclear, 810 ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrclear,
828 ~0ULL&~INFINIPATH_HWE_MEMBISTFAILED); 811 ~0ULL&~INFINIPATH_HWE_MEMBISTFAILED);
829 ipath_write_kreg(dd, dd->ipath_kregs->kr_control, 0ULL); 812 ipath_write_kreg(dd, dd->ipath_kregs->kr_control, 0ULL);
830 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 813
831 INFINIPATH_S_PIOENABLE); 814 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
815 dd->ipath_sendctrl = INFINIPATH_S_PIOENABLE;
816 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
817 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
818 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
832 819
833 /* 820 /*
834 * before error clears, since we expect serdes pll errors during 821 * before error clears, since we expect serdes pll errors during
diff --git a/drivers/infiniband/hw/ipath/ipath_intr.c b/drivers/infiniband/hw/ipath/ipath_intr.c
index c61f9da2964a..92e58c921522 100644
--- a/drivers/infiniband/hw/ipath/ipath_intr.c
+++ b/drivers/infiniband/hw/ipath/ipath_intr.c
@@ -683,7 +683,7 @@ static int handle_errors(struct ipath_devdata *dd, ipath_err_t errs)
683 for (i = 0; i < dd->ipath_cfgports; i++) { 683 for (i = 0; i < dd->ipath_cfgports; i++) {
684 struct ipath_portdata *pd = dd->ipath_pd[i]; 684 struct ipath_portdata *pd = dd->ipath_pd[i];
685 if (i == 0) { 685 if (i == 0) {
686 hd = dd->ipath_port0head; 686 hd = pd->port_head;
687 tl = (u32) le64_to_cpu( 687 tl = (u32) le64_to_cpu(
688 *dd->ipath_hdrqtailptr); 688 *dd->ipath_hdrqtailptr);
689 } else if (pd && pd->port_cnt && 689 } else if (pd && pd->port_cnt &&
@@ -693,7 +693,7 @@ static int handle_errors(struct ipath_devdata *dd, ipath_err_t errs)
693 * except kernel 693 * except kernel
694 */ 694 */
695 tl = *(u64 *) pd->port_rcvhdrtail_kvaddr; 695 tl = *(u64 *) pd->port_rcvhdrtail_kvaddr;
696 if (tl == dd->ipath_lastrcvhdrqtails[i]) 696 if (tl == pd->port_lastrcvhdrqtail)
697 continue; 697 continue;
698 hd = ipath_read_ureg32(dd, ur_rcvhdrhead, 698 hd = ipath_read_ureg32(dd, ur_rcvhdrhead,
699 i); 699 i);
@@ -703,7 +703,7 @@ static int handle_errors(struct ipath_devdata *dd, ipath_err_t errs)
703 (!hd && tl == dd->ipath_hdrqlast)) { 703 (!hd && tl == dd->ipath_hdrqlast)) {
704 if (i == 0) 704 if (i == 0)
705 chkerrpkts = 1; 705 chkerrpkts = 1;
706 dd->ipath_lastrcvhdrqtails[i] = tl; 706 pd->port_lastrcvhdrqtail = tl;
707 pd->port_hdrqfull++; 707 pd->port_hdrqfull++;
708 /* flush hdrqfull so that poll() sees it */ 708 /* flush hdrqfull so that poll() sees it */
709 wmb(); 709 wmb();
@@ -712,6 +712,8 @@ static int handle_errors(struct ipath_devdata *dd, ipath_err_t errs)
712 } 712 }
713 } 713 }
714 if (errs & INFINIPATH_E_RRCVEGRFULL) { 714 if (errs & INFINIPATH_E_RRCVEGRFULL) {
715 struct ipath_portdata *pd = dd->ipath_pd[0];
716
715 /* 717 /*
716 * since this is of less importance and not likely to 718 * since this is of less importance and not likely to
717 * happen without also getting hdrfull, only count 719 * happen without also getting hdrfull, only count
@@ -719,7 +721,7 @@ static int handle_errors(struct ipath_devdata *dd, ipath_err_t errs)
719 * vs user) 721 * vs user)
720 */ 722 */
721 ipath_stats.sps_etidfull++; 723 ipath_stats.sps_etidfull++;
722 if (dd->ipath_port0head != 724 if (pd->port_head !=
723 (u32) le64_to_cpu(*dd->ipath_hdrqtailptr)) 725 (u32) le64_to_cpu(*dd->ipath_hdrqtailptr))
724 chkerrpkts = 1; 726 chkerrpkts = 1;
725 } 727 }
@@ -795,6 +797,7 @@ void ipath_clear_freeze(struct ipath_devdata *dd)
795{ 797{
796 int i, im; 798 int i, im;
797 __le64 val; 799 __le64 val;
800 unsigned long flags;
798 801
799 /* disable error interrupts, to avoid confusion */ 802 /* disable error interrupts, to avoid confusion */
800 ipath_write_kreg(dd, dd->ipath_kregs->kr_errormask, 0ULL); 803 ipath_write_kreg(dd, dd->ipath_kregs->kr_errormask, 0ULL);
@@ -813,11 +816,14 @@ void ipath_clear_freeze(struct ipath_devdata *dd)
813 dd->ipath_control); 816 dd->ipath_control);
814 817
815 /* ensure pio avail updates continue */ 818 /* ensure pio avail updates continue */
819 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
816 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 820 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
817 dd->ipath_sendctrl & ~INFINIPATH_S_PIOBUFAVAILUPD); 821 dd->ipath_sendctrl & ~INFINIPATH_S_PIOBUFAVAILUPD);
818 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch); 822 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
819 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 823 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
820 dd->ipath_sendctrl); 824 dd->ipath_sendctrl);
825 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
826 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
821 827
822 /* 828 /*
823 * We just enabled pioavailupdate, so dma copy is almost certainly 829 * We just enabled pioavailupdate, so dma copy is almost certainly
@@ -825,8 +831,8 @@ void ipath_clear_freeze(struct ipath_devdata *dd)
825 */ 831 */
826 for (i = 0; i < dd->ipath_pioavregs; i++) { 832 for (i = 0; i < dd->ipath_pioavregs; i++) {
827 /* deal with 6110 chip bug */ 833 /* deal with 6110 chip bug */
828 im = i > 3 ? ((i&1) ? i-1 : i+1) : i; 834 im = i > 3 ? i ^ 1 : i;
829 val = ipath_read_kreg64(dd, (0x1000/sizeof(u64))+im); 835 val = ipath_read_kreg64(dd, (0x1000 / sizeof(u64)) + im);
830 dd->ipath_pioavailregs_dma[i] = dd->ipath_pioavailshadow[i] 836 dd->ipath_pioavailregs_dma[i] = dd->ipath_pioavailshadow[i]
831 = le64_to_cpu(val); 837 = le64_to_cpu(val);
832 } 838 }
@@ -849,7 +855,7 @@ void ipath_clear_freeze(struct ipath_devdata *dd)
849 855
850/* this is separate to allow for better optimization of ipath_intr() */ 856/* this is separate to allow for better optimization of ipath_intr() */
851 857
852static void ipath_bad_intr(struct ipath_devdata *dd, u32 * unexpectp) 858static noinline void ipath_bad_intr(struct ipath_devdata *dd, u32 *unexpectp)
853{ 859{
854 /* 860 /*
855 * sometimes happen during driver init and unload, don't want 861 * sometimes happen during driver init and unload, don't want
@@ -877,7 +883,7 @@ static void ipath_bad_intr(struct ipath_devdata *dd, u32 * unexpectp)
877 dd->ipath_f_free_irq(dd); 883 dd->ipath_f_free_irq(dd);
878 } 884 }
879 } 885 }
880 if (ipath_read_kreg32(dd, dd->ipath_kregs->kr_intmask)) { 886 if (ipath_read_ireg(dd, dd->ipath_kregs->kr_intmask)) {
881 ipath_dev_err(dd, "%u unexpected interrupts, " 887 ipath_dev_err(dd, "%u unexpected interrupts, "
882 "disabling interrupts completely\n", 888 "disabling interrupts completely\n",
883 *unexpectp); 889 *unexpectp);
@@ -892,7 +898,7 @@ static void ipath_bad_intr(struct ipath_devdata *dd, u32 * unexpectp)
892 "ignoring\n"); 898 "ignoring\n");
893} 899}
894 900
895static void ipath_bad_regread(struct ipath_devdata *dd) 901static noinline void ipath_bad_regread(struct ipath_devdata *dd)
896{ 902{
897 static int allbits; 903 static int allbits;
898 904
@@ -920,31 +926,9 @@ static void ipath_bad_regread(struct ipath_devdata *dd)
920 } 926 }
921} 927}
922 928
923static void handle_port_pioavail(struct ipath_devdata *dd)
924{
925 u32 i;
926 /*
927 * start from port 1, since for now port 0 is never using
928 * wait_event for PIO
929 */
930 for (i = 1; dd->ipath_portpiowait && i < dd->ipath_cfgports; i++) {
931 struct ipath_portdata *pd = dd->ipath_pd[i];
932
933 if (pd && pd->port_cnt &&
934 dd->ipath_portpiowait & (1U << i)) {
935 clear_bit(i, &dd->ipath_portpiowait);
936 if (test_bit(IPATH_PORT_WAITING_PIO,
937 &pd->port_flag)) {
938 clear_bit(IPATH_PORT_WAITING_PIO,
939 &pd->port_flag);
940 wake_up_interruptible(&pd->port_wait);
941 }
942 }
943 }
944}
945
946static void handle_layer_pioavail(struct ipath_devdata *dd) 929static void handle_layer_pioavail(struct ipath_devdata *dd)
947{ 930{
931 unsigned long flags;
948 int ret; 932 int ret;
949 933
950 ret = ipath_ib_piobufavail(dd->verbs_dev); 934 ret = ipath_ib_piobufavail(dd->verbs_dev);
@@ -953,9 +937,12 @@ static void handle_layer_pioavail(struct ipath_devdata *dd)
953 937
954 return; 938 return;
955set: 939set:
956 set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl); 940 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
941 dd->ipath_sendctrl |= INFINIPATH_S_PIOINTBUFAVAIL;
957 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 942 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
958 dd->ipath_sendctrl); 943 dd->ipath_sendctrl);
944 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
945 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
959} 946}
960 947
961/* 948/*
@@ -969,7 +956,15 @@ static void handle_urcv(struct ipath_devdata *dd, u32 istat)
969 int i; 956 int i;
970 int rcvdint = 0; 957 int rcvdint = 0;
971 958
972 /* test_bit below needs this... */ 959 /*
960 * test_and_clear_bit(IPATH_PORT_WAITING_RCV) and
961 * test_and_clear_bit(IPATH_PORT_WAITING_URG) below
962 * would both like timely updates of the bits so that
963 * we don't pass them by unnecessarily. the rmb()
964 * here ensures that we see them promptly -- the
965 * corresponding wmb()'s are in ipath_poll_urgent()
966 * and ipath_poll_next()...
967 */
973 rmb(); 968 rmb();
974 portr = ((istat >> INFINIPATH_I_RCVAVAIL_SHIFT) & 969 portr = ((istat >> INFINIPATH_I_RCVAVAIL_SHIFT) &
975 dd->ipath_i_rcvavail_mask) 970 dd->ipath_i_rcvavail_mask)
@@ -980,7 +975,7 @@ static void handle_urcv(struct ipath_devdata *dd, u32 istat)
980 if (portr & (1 << i) && pd && pd->port_cnt) { 975 if (portr & (1 << i) && pd && pd->port_cnt) {
981 if (test_and_clear_bit(IPATH_PORT_WAITING_RCV, 976 if (test_and_clear_bit(IPATH_PORT_WAITING_RCV,
982 &pd->port_flag)) { 977 &pd->port_flag)) {
983 clear_bit(i + INFINIPATH_R_INTRAVAIL_SHIFT, 978 clear_bit(i + dd->ipath_r_intravail_shift,
984 &dd->ipath_rcvctrl); 979 &dd->ipath_rcvctrl);
985 wake_up_interruptible(&pd->port_wait); 980 wake_up_interruptible(&pd->port_wait);
986 rcvdint = 1; 981 rcvdint = 1;
@@ -1039,7 +1034,7 @@ irqreturn_t ipath_intr(int irq, void *data)
1039 goto bail; 1034 goto bail;
1040 } 1035 }
1041 1036
1042 istat = ipath_read_kreg32(dd, dd->ipath_kregs->kr_intstatus); 1037 istat = ipath_read_ireg(dd, dd->ipath_kregs->kr_intstatus);
1043 1038
1044 if (unlikely(!istat)) { 1039 if (unlikely(!istat)) {
1045 ipath_stats.sps_nullintr++; 1040 ipath_stats.sps_nullintr++;
@@ -1180,7 +1175,7 @@ irqreturn_t ipath_intr(int irq, void *data)
1180 * for receive are at the bottom. 1175 * for receive are at the bottom.
1181 */ 1176 */
1182 if (chk0rcv) { 1177 if (chk0rcv) {
1183 ipath_kreceive(dd); 1178 ipath_kreceive(dd->ipath_pd[0]);
1184 istat &= ~port0rbits; 1179 istat &= ~port0rbits;
1185 } 1180 }
1186 1181
@@ -1191,12 +1186,14 @@ irqreturn_t ipath_intr(int irq, void *data)
1191 handle_urcv(dd, istat); 1186 handle_urcv(dd, istat);
1192 1187
1193 if (istat & INFINIPATH_I_SPIOBUFAVAIL) { 1188 if (istat & INFINIPATH_I_SPIOBUFAVAIL) {
1194 clear_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl); 1189 unsigned long flags;
1190
1191 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
1192 dd->ipath_sendctrl &= ~INFINIPATH_S_PIOINTBUFAVAIL;
1195 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 1193 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
1196 dd->ipath_sendctrl); 1194 dd->ipath_sendctrl);
1197 1195 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
1198 if (dd->ipath_portpiowait) 1196 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
1199 handle_port_pioavail(dd);
1200 1197
1201 handle_layer_pioavail(dd); 1198 handle_layer_pioavail(dd);
1202 } 1199 }
diff --git a/drivers/infiniband/hw/ipath/ipath_kernel.h b/drivers/infiniband/hw/ipath/ipath_kernel.h
index bb1dc075f1d1..4cc0f95ea877 100644
--- a/drivers/infiniband/hw/ipath/ipath_kernel.h
+++ b/drivers/infiniband/hw/ipath/ipath_kernel.h
@@ -41,6 +41,7 @@
41#include <linux/interrupt.h> 41#include <linux/interrupt.h>
42#include <linux/pci.h> 42#include <linux/pci.h>
43#include <linux/dma-mapping.h> 43#include <linux/dma-mapping.h>
44#include <linux/mutex.h>
44#include <asm/io.h> 45#include <asm/io.h>
45#include <rdma/ib_verbs.h> 46#include <rdma/ib_verbs.h>
46 47
@@ -140,6 +141,11 @@ struct ipath_portdata {
140 u32 port_pionowait; 141 u32 port_pionowait;
141 /* total number of rcvhdrqfull errors */ 142 /* total number of rcvhdrqfull errors */
142 u32 port_hdrqfull; 143 u32 port_hdrqfull;
144 /*
145 * Used to suppress multiple instances of same
146 * port staying stuck at same point.
147 */
148 u32 port_lastrcvhdrqtail;
143 /* saved total number of rcvhdrqfull errors for poll edge trigger */ 149 /* saved total number of rcvhdrqfull errors for poll edge trigger */
144 u32 port_hdrqfull_poll; 150 u32 port_hdrqfull_poll;
145 /* total number of polled urgent packets */ 151 /* total number of polled urgent packets */
@@ -148,6 +154,7 @@ struct ipath_portdata {
148 u32 port_urgent_poll; 154 u32 port_urgent_poll;
149 /* pid of process using this port */ 155 /* pid of process using this port */
150 pid_t port_pid; 156 pid_t port_pid;
157 pid_t port_subpid[INFINIPATH_MAX_SUBPORT];
151 /* same size as task_struct .comm[] */ 158 /* same size as task_struct .comm[] */
152 char port_comm[16]; 159 char port_comm[16];
153 /* pkeys set by this use of this port */ 160 /* pkeys set by this use of this port */
@@ -166,6 +173,8 @@ struct ipath_portdata {
166 u32 active_slaves; 173 u32 active_slaves;
167 /* Type of packets or conditions we want to poll for */ 174 /* Type of packets or conditions we want to poll for */
168 u16 poll_type; 175 u16 poll_type;
176 /* port rcvhdrq head offset */
177 u32 port_head;
169}; 178};
170 179
171struct sk_buff; 180struct sk_buff;
@@ -182,6 +191,22 @@ struct ipath_skbinfo {
182 dma_addr_t phys; 191 dma_addr_t phys;
183}; 192};
184 193
194/*
195 * Possible IB config parameters for ipath_f_get/set_ib_cfg()
196 */
197#define IPATH_IB_CFG_LIDLMC 0 /* Get/set LID (LS16b) and Mask (MS16b) */
198#define IPATH_IB_CFG_HRTBT 1 /* Get/set Heartbeat off/enable/auto */
199#define IPATH_IB_HRTBT_ON 3 /* Heartbeat enabled, sent every 100msec */
200#define IPATH_IB_HRTBT_OFF 0 /* Heartbeat off */
201#define IPATH_IB_CFG_LWID_ENB 2 /* Get/set allowed Link-width */
202#define IPATH_IB_CFG_LWID 3 /* Get currently active Link-width */
203#define IPATH_IB_CFG_SPD_ENB 4 /* Get/set allowed Link speeds */
204#define IPATH_IB_CFG_SPD 5 /* Get current Link spd */
205#define IPATH_IB_CFG_RXPOL_ENB 6 /* Get/set Auto-RX-polarity enable */
206#define IPATH_IB_CFG_LREV_ENB 7 /* Get/set Auto-Lane-reversal enable */
207#define IPATH_IB_CFG_LINKLATENCY 8 /* Get Auto-Lane-reversal enable */
208
209
185struct ipath_devdata { 210struct ipath_devdata {
186 struct list_head ipath_list; 211 struct list_head ipath_list;
187 212
@@ -222,6 +247,8 @@ struct ipath_devdata {
222 struct _ipath_layer ipath_layer; 247 struct _ipath_layer ipath_layer;
223 /* setup intr */ 248 /* setup intr */
224 int (*ipath_f_intrsetup)(struct ipath_devdata *); 249 int (*ipath_f_intrsetup)(struct ipath_devdata *);
250 /* fallback to alternate interrupt type if possible */
251 int (*ipath_f_intr_fallback)(struct ipath_devdata *);
225 /* setup on-chip bus config */ 252 /* setup on-chip bus config */
226 int (*ipath_f_bus)(struct ipath_devdata *, struct pci_dev *); 253 int (*ipath_f_bus)(struct ipath_devdata *, struct pci_dev *);
227 /* hard reset chip */ 254 /* hard reset chip */
@@ -244,6 +271,18 @@ struct ipath_devdata {
244 int (*ipath_f_get_base_info)(struct ipath_portdata *, void *); 271 int (*ipath_f_get_base_info)(struct ipath_portdata *, void *);
245 /* free irq */ 272 /* free irq */
246 void (*ipath_f_free_irq)(struct ipath_devdata *); 273 void (*ipath_f_free_irq)(struct ipath_devdata *);
274 struct ipath_message_header *(*ipath_f_get_msgheader)
275 (struct ipath_devdata *, __le32 *);
276 void (*ipath_f_config_ports)(struct ipath_devdata *, ushort);
277 int (*ipath_f_get_ib_cfg)(struct ipath_devdata *, int);
278 int (*ipath_f_set_ib_cfg)(struct ipath_devdata *, int, u32);
279 void (*ipath_f_config_jint)(struct ipath_devdata *, u16 , u16);
280 void (*ipath_f_read_counters)(struct ipath_devdata *,
281 struct infinipath_counters *);
282 void (*ipath_f_xgxs_reset)(struct ipath_devdata *);
283 /* per chip actions needed for IB Link up/down changes */
284 int (*ipath_f_ib_updown)(struct ipath_devdata *, int, u64);
285
247 struct ipath_ibdev *verbs_dev; 286 struct ipath_ibdev *verbs_dev;
248 struct timer_list verbs_timer; 287 struct timer_list verbs_timer;
249 /* total dwords sent (summed from counter) */ 288 /* total dwords sent (summed from counter) */
@@ -313,22 +352,12 @@ struct ipath_devdata {
313 * supports, less gives more pio bufs/port, etc. 352 * supports, less gives more pio bufs/port, etc.
314 */ 353 */
315 u32 ipath_cfgports; 354 u32 ipath_cfgports;
316 /* port0 rcvhdrq head offset */
317 u32 ipath_port0head;
318 /* count of port 0 hdrqfull errors */ 355 /* count of port 0 hdrqfull errors */
319 u32 ipath_p0_hdrqfull; 356 u32 ipath_p0_hdrqfull;
357 /* port 0 number of receive eager buffers */
358 u32 ipath_p0_rcvegrcnt;
320 359
321 /* 360 /*
322 * (*cfgports) used to suppress multiple instances of same
323 * port staying stuck at same point
324 */
325 u32 *ipath_lastrcvhdrqtails;
326 /*
327 * (*cfgports) used to suppress multiple instances of same
328 * port staying stuck at same point
329 */
330 u32 *ipath_lastegrheads;
331 /*
332 * index of last piobuffer we used. Speeds up searching, by 361 * index of last piobuffer we used. Speeds up searching, by
333 * starting at this point. Doesn't matter if multiple cpu's use and 362 * starting at this point. Doesn't matter if multiple cpu's use and
334 * update, last updater is only write that matters. Whenever it 363 * update, last updater is only write that matters. Whenever it
@@ -367,14 +396,15 @@ struct ipath_devdata {
367 unsigned long ipath_wc_len; 396 unsigned long ipath_wc_len;
368 /* ref count for each pkey */ 397 /* ref count for each pkey */
369 atomic_t ipath_pkeyrefs[4]; 398 atomic_t ipath_pkeyrefs[4];
370 /* shadow copy of all exptids physaddr; used only by funcsim */
371 u64 *ipath_tidsimshadow;
372 /* shadow copy of struct page *'s for exp tid pages */ 399 /* shadow copy of struct page *'s for exp tid pages */
373 struct page **ipath_pageshadow; 400 struct page **ipath_pageshadow;
374 /* shadow copy of dma handles for exp tid pages */ 401 /* shadow copy of dma handles for exp tid pages */
375 dma_addr_t *ipath_physshadow; 402 dma_addr_t *ipath_physshadow;
376 /* lock to workaround chip bug 9437 */ 403 u64 __iomem *ipath_egrtidbase;
404 /* lock to workaround chip bug 9437 and others */
405 spinlock_t ipath_kernel_tid_lock;
377 spinlock_t ipath_tid_lock; 406 spinlock_t ipath_tid_lock;
407 spinlock_t ipath_sendctrl_lock;
378 408
379 /* 409 /*
380 * IPATH_STATUS_*, 410 * IPATH_STATUS_*,
@@ -395,6 +425,8 @@ struct ipath_devdata {
395 void *ipath_dummy_hdrq; /* used after port close */ 425 void *ipath_dummy_hdrq; /* used after port close */
396 dma_addr_t ipath_dummy_hdrq_phys; 426 dma_addr_t ipath_dummy_hdrq_phys;
397 427
428 unsigned long ipath_ureg_align; /* user register alignment */
429
398 /* 430 /*
399 * Shadow copies of registers; size indicates read access size. 431 * Shadow copies of registers; size indicates read access size.
400 * Most of them are readonly, but some are write-only register, 432 * Most of them are readonly, but some are write-only register,
@@ -456,8 +488,6 @@ struct ipath_devdata {
456 unsigned long ipath_rcvctrl; 488 unsigned long ipath_rcvctrl;
457 /* shadow kr_sendctrl */ 489 /* shadow kr_sendctrl */
458 unsigned long ipath_sendctrl; 490 unsigned long ipath_sendctrl;
459 /* ports waiting for PIOavail intr */
460 unsigned long ipath_portpiowait;
461 unsigned long ipath_lastcancel; /* to not count armlaunch after cancel */ 491 unsigned long ipath_lastcancel; /* to not count armlaunch after cancel */
462 492
463 /* value we put in kr_rcvhdrcnt */ 493 /* value we put in kr_rcvhdrcnt */
@@ -550,12 +580,26 @@ struct ipath_devdata {
550 u8 ipath_minrev; 580 u8 ipath_minrev;
551 /* board rev, from ipath_revision */ 581 /* board rev, from ipath_revision */
552 u8 ipath_boardrev; 582 u8 ipath_boardrev;
583
584 u8 ipath_r_portenable_shift;
585 u8 ipath_r_intravail_shift;
586 u8 ipath_r_tailupd_shift;
587 u8 ipath_r_portcfg_shift;
588
553 /* unit # of this chip, if present */ 589 /* unit # of this chip, if present */
554 int ipath_unit; 590 int ipath_unit;
555 /* saved for restore after reset */ 591 /* saved for restore after reset */
556 u8 ipath_pci_cacheline; 592 u8 ipath_pci_cacheline;
557 /* LID mask control */ 593 /* LID mask control */
558 u8 ipath_lmc; 594 u8 ipath_lmc;
595 /* link width supported */
596 u8 ipath_link_width_supported;
597 /* link speed supported */
598 u8 ipath_link_speed_supported;
599 u8 ipath_link_width_enabled;
600 u8 ipath_link_speed_enabled;
601 u8 ipath_link_width_active;
602 u8 ipath_link_speed_active;
559 /* Rx Polarity inversion (compensate for ~tx on partner) */ 603 /* Rx Polarity inversion (compensate for ~tx on partner) */
560 u8 ipath_rx_pol_inv; 604 u8 ipath_rx_pol_inv;
561 605
@@ -590,6 +634,8 @@ struct ipath_devdata {
590 */ 634 */
591 u32 ipath_i_rcvavail_mask; 635 u32 ipath_i_rcvavail_mask;
592 u32 ipath_i_rcvurg_mask; 636 u32 ipath_i_rcvurg_mask;
637 u16 ipath_i_rcvurg_shift;
638 u16 ipath_i_rcvavail_shift;
593 639
594 /* 640 /*
595 * Register bits for selecting i2c direction and values, used for 641 * Register bits for selecting i2c direction and values, used for
@@ -603,6 +649,29 @@ struct ipath_devdata {
603 /* lock for doing RMW of shadows/regs for ExtCtrl and GPIO */ 649 /* lock for doing RMW of shadows/regs for ExtCtrl and GPIO */
604 spinlock_t ipath_gpio_lock; 650 spinlock_t ipath_gpio_lock;
605 651
652 /*
653 * IB link and linktraining states and masks that vary per chip in
654 * some way. Set at init, to avoid each IB status change interrupt
655 */
656 u8 ibcs_ls_shift;
657 u8 ibcs_lts_mask;
658 u32 ibcs_mask;
659 u32 ib_init;
660 u32 ib_arm;
661 u32 ib_active;
662
663 u16 ipath_rhf_offset; /* offset of RHF within receive header entry */
664
665 /*
666 * shift/mask for linkcmd, linkinitcmd, maxpktlen in ibccontol
667 * reg. Changes for IBA7220
668 */
669 u8 ibcc_lic_mask; /* LinkInitCmd */
670 u8 ibcc_lc_shift; /* LinkCmd */
671 u8 ibcc_mpl_shift; /* Maxpktlen */
672
673 u8 delay_mult;
674
606 /* used to override LED behavior */ 675 /* used to override LED behavior */
607 u8 ipath_led_override; /* Substituted for normal value, if non-zero */ 676 u8 ipath_led_override; /* Substituted for normal value, if non-zero */
608 u16 ipath_led_override_timeoff; /* delta to next timer event */ 677 u16 ipath_led_override_timeoff; /* delta to next timer event */
@@ -616,7 +685,7 @@ struct ipath_devdata {
616 /* control access to actual counters, timer */ 685 /* control access to actual counters, timer */
617 spinlock_t ipath_eep_st_lock; 686 spinlock_t ipath_eep_st_lock;
618 /* control high-level access to EEPROM */ 687 /* control high-level access to EEPROM */
619 struct semaphore ipath_eep_sem; 688 struct mutex ipath_eep_lock;
620 /* Below inc'd by ipath_snap_cntrs(), locked by ipath_eep_st_lock */ 689 /* Below inc'd by ipath_snap_cntrs(), locked by ipath_eep_st_lock */
621 uint64_t ipath_traffic_wds; 690 uint64_t ipath_traffic_wds;
622 /* active time is kept in seconds, but logged in hours */ 691 /* active time is kept in seconds, but logged in hours */
@@ -630,6 +699,10 @@ struct ipath_devdata {
630 * each of the counters to increment. 699 * each of the counters to increment.
631 */ 700 */
632 struct ipath_eep_log_mask ipath_eep_st_masks[IPATH_EEP_LOG_CNT]; 701 struct ipath_eep_log_mask ipath_eep_st_masks[IPATH_EEP_LOG_CNT];
702
703 /* interrupt mitigation reload register info */
704 u16 ipath_jint_idle_ticks; /* idle clock ticks */
705 u16 ipath_jint_max_packets; /* max packets across all ports */
633}; 706};
634 707
635/* Private data for file operations */ 708/* Private data for file operations */
@@ -690,7 +763,7 @@ void ipath_free_pddata(struct ipath_devdata *, struct ipath_portdata *);
690 763
691int ipath_parse_ushort(const char *str, unsigned short *valp); 764int ipath_parse_ushort(const char *str, unsigned short *valp);
692 765
693void ipath_kreceive(struct ipath_devdata *); 766void ipath_kreceive(struct ipath_portdata *);
694int ipath_setrcvhdrsize(struct ipath_devdata *, unsigned); 767int ipath_setrcvhdrsize(struct ipath_devdata *, unsigned);
695int ipath_reset_device(int); 768int ipath_reset_device(int);
696void ipath_get_faststats(unsigned long); 769void ipath_get_faststats(unsigned long);
@@ -698,6 +771,8 @@ int ipath_set_linkstate(struct ipath_devdata *, u8);
698int ipath_set_mtu(struct ipath_devdata *, u16); 771int ipath_set_mtu(struct ipath_devdata *, u16);
699int ipath_set_lid(struct ipath_devdata *, u32, u8); 772int ipath_set_lid(struct ipath_devdata *, u32, u8);
700int ipath_set_rx_pol_inv(struct ipath_devdata *dd, u8 new_pol_inv); 773int ipath_set_rx_pol_inv(struct ipath_devdata *dd, u8 new_pol_inv);
774void ipath_enable_armlaunch(struct ipath_devdata *);
775void ipath_disable_armlaunch(struct ipath_devdata *);
701 776
702/* for use in system calls, where we want to know device type, etc. */ 777/* for use in system calls, where we want to know device type, etc. */
703#define port_fp(fp) ((struct ipath_filedata *)(fp)->private_data)->pd 778#define port_fp(fp) ((struct ipath_filedata *)(fp)->private_data)->pd
@@ -744,9 +819,15 @@ int ipath_set_rx_pol_inv(struct ipath_devdata *dd, u8 new_pol_inv);
744 * are 64bit */ 819 * are 64bit */
745#define IPATH_32BITCOUNTERS 0x20000 820#define IPATH_32BITCOUNTERS 0x20000
746 /* can miss port0 rx interrupts */ 821 /* can miss port0 rx interrupts */
822 /* Interrupt register is 64 bits */
823#define IPATH_INTREG_64 0x40000
747#define IPATH_DISABLED 0x80000 /* administratively disabled */ 824#define IPATH_DISABLED 0x80000 /* administratively disabled */
748 /* Use GPIO interrupts for new counters */ 825 /* Use GPIO interrupts for new counters */
749#define IPATH_GPIO_ERRINTRS 0x100000 826#define IPATH_GPIO_ERRINTRS 0x100000
827#define IPATH_SWAP_PIOBUFS 0x200000
828 /* Suppress heartbeat, even if turning off loopback */
829#define IPATH_NO_HRTBT 0x1000000
830#define IPATH_HAS_MULT_IB_SPEED 0x8000000
750 831
751/* Bits in GPIO for the added interrupts */ 832/* Bits in GPIO for the added interrupts */
752#define IPATH_GPIO_PORT0_BIT 2 833#define IPATH_GPIO_PORT0_BIT 2
@@ -758,8 +839,6 @@ int ipath_set_rx_pol_inv(struct ipath_devdata *dd, u8 new_pol_inv);
758/* portdata flag bit offsets */ 839/* portdata flag bit offsets */
759 /* waiting for a packet to arrive */ 840 /* waiting for a packet to arrive */
760#define IPATH_PORT_WAITING_RCV 2 841#define IPATH_PORT_WAITING_RCV 2
761 /* waiting for a PIO buffer to be available */
762#define IPATH_PORT_WAITING_PIO 3
763 /* master has not finished initializing */ 842 /* master has not finished initializing */
764#define IPATH_PORT_MASTER_UNINIT 4 843#define IPATH_PORT_MASTER_UNINIT 4
765 /* waiting for an urgent packet to arrive */ 844 /* waiting for an urgent packet to arrive */
@@ -767,8 +846,6 @@ int ipath_set_rx_pol_inv(struct ipath_devdata *dd, u8 new_pol_inv);
767 846
768/* free up any allocated data at closes */ 847/* free up any allocated data at closes */
769void ipath_free_data(struct ipath_portdata *dd); 848void ipath_free_data(struct ipath_portdata *dd);
770int ipath_waitfor_mdio_cmdready(struct ipath_devdata *);
771int ipath_waitfor_complete(struct ipath_devdata *, ipath_kreg, u64, u64 *);
772u32 __iomem *ipath_getpiobuf(struct ipath_devdata *, u32 *); 849u32 __iomem *ipath_getpiobuf(struct ipath_devdata *, u32 *);
773void ipath_init_iba6120_funcs(struct ipath_devdata *); 850void ipath_init_iba6120_funcs(struct ipath_devdata *);
774void ipath_init_iba6110_funcs(struct ipath_devdata *); 851void ipath_init_iba6110_funcs(struct ipath_devdata *);
@@ -792,33 +869,6 @@ void ipath_set_led_override(struct ipath_devdata *dd, unsigned int val);
792 */ 869 */
793#define IPATH_DFLT_RCVHDRSIZE 9 870#define IPATH_DFLT_RCVHDRSIZE 9
794 871
795#define IPATH_MDIO_CMD_WRITE 1
796#define IPATH_MDIO_CMD_READ 2
797#define IPATH_MDIO_CLD_DIV 25 /* to get 2.5 Mhz mdio clock */
798#define IPATH_MDIO_CMDVALID 0x40000000 /* bit 30 */
799#define IPATH_MDIO_DATAVALID 0x80000000 /* bit 31 */
800#define IPATH_MDIO_CTRL_STD 0x0
801
802static inline u64 ipath_mdio_req(int cmd, int dev, int reg, int data)
803{
804 return (((u64) IPATH_MDIO_CLD_DIV) << 32) |
805 (cmd << 26) |
806 (dev << 21) |
807 (reg << 16) |
808 (data & 0xFFFF);
809}
810
811 /* signal and fifo status, in bank 31 */
812#define IPATH_MDIO_CTRL_XGXS_REG_8 0x8
813 /* controls loopback, redundancy */
814#define IPATH_MDIO_CTRL_8355_REG_1 0x10
815 /* premph, encdec, etc. */
816#define IPATH_MDIO_CTRL_8355_REG_2 0x11
817 /* Kchars, etc. */
818#define IPATH_MDIO_CTRL_8355_REG_6 0x15
819#define IPATH_MDIO_CTRL_8355_REG_9 0x18
820#define IPATH_MDIO_CTRL_8355_REG_10 0x1D
821
822int ipath_get_user_pages(unsigned long, size_t, struct page **); 872int ipath_get_user_pages(unsigned long, size_t, struct page **);
823void ipath_release_user_pages(struct page **, size_t); 873void ipath_release_user_pages(struct page **, size_t);
824void ipath_release_user_pages_on_close(struct page **, size_t); 874void ipath_release_user_pages_on_close(struct page **, size_t);
@@ -863,7 +913,7 @@ static inline u32 ipath_read_ureg32(const struct ipath_devdata *dd,
863 return readl(regno + (u64 __iomem *) 913 return readl(regno + (u64 __iomem *)
864 (dd->ipath_uregbase + 914 (dd->ipath_uregbase +
865 (char __iomem *)dd->ipath_kregbase + 915 (char __iomem *)dd->ipath_kregbase +
866 dd->ipath_palign * port)); 916 dd->ipath_ureg_align * port));
867} 917}
868 918
869/** 919/**
@@ -880,7 +930,7 @@ static inline void ipath_write_ureg(const struct ipath_devdata *dd,
880{ 930{
881 u64 __iomem *ubase = (u64 __iomem *) 931 u64 __iomem *ubase = (u64 __iomem *)
882 (dd->ipath_uregbase + (char __iomem *) dd->ipath_kregbase + 932 (dd->ipath_uregbase + (char __iomem *) dd->ipath_kregbase +
883 dd->ipath_palign * port); 933 dd->ipath_ureg_align * port);
884 if (dd->ipath_kregbase) 934 if (dd->ipath_kregbase)
885 writeq(value, &ubase[regno]); 935 writeq(value, &ubase[regno]);
886} 936}
@@ -930,6 +980,53 @@ static inline u32 ipath_read_creg32(const struct ipath_devdata *dd,
930 (char __iomem *)dd->ipath_kregbase)); 980 (char __iomem *)dd->ipath_kregbase));
931} 981}
932 982
983static inline void ipath_write_creg(const struct ipath_devdata *dd,
984 ipath_creg regno, u64 value)
985{
986 if (dd->ipath_kregbase)
987 writeq(value, regno + (u64 __iomem *)
988 (dd->ipath_cregbase +
989 (char __iomem *)dd->ipath_kregbase));
990}
991
992static inline void ipath_clear_rcvhdrtail(const struct ipath_portdata *pd)
993{
994 *((u64 *) pd->port_rcvhdrtail_kvaddr) = 0ULL;
995}
996
997static inline u32 ipath_get_rcvhdrtail(const struct ipath_portdata *pd)
998{
999 return (u32) le64_to_cpu(*((volatile __le64 *)
1000 pd->port_rcvhdrtail_kvaddr));
1001}
1002
1003static inline u64 ipath_read_ireg(const struct ipath_devdata *dd, ipath_kreg r)
1004{
1005 return (dd->ipath_flags & IPATH_INTREG_64) ?
1006 ipath_read_kreg64(dd, r) : ipath_read_kreg32(dd, r);
1007}
1008
1009/*
1010 * from contents of IBCStatus (or a saved copy), return linkstate
1011 * Report ACTIVE_DEFER as ACTIVE, because we treat them the same
1012 * everywhere, anyway (and should be, for almost all purposes).
1013 */
1014static inline u32 ipath_ib_linkstate(struct ipath_devdata *dd, u64 ibcs)
1015{
1016 u32 state = (u32)(ibcs >> dd->ibcs_ls_shift) &
1017 INFINIPATH_IBCS_LINKSTATE_MASK;
1018 if (state == INFINIPATH_IBCS_L_STATE_ACT_DEFER)
1019 state = INFINIPATH_IBCS_L_STATE_ACTIVE;
1020 return state;
1021}
1022
1023/* from contents of IBCStatus (or a saved copy), return linktrainingstate */
1024static inline u32 ipath_ib_linktrstate(struct ipath_devdata *dd, u64 ibcs)
1025{
1026 return (u32)(ibcs >> INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) &
1027 dd->ibcs_lts_mask;
1028}
1029
933/* 1030/*
934 * sysfs interface. 1031 * sysfs interface.
935 */ 1032 */
diff --git a/drivers/infiniband/hw/ipath/ipath_keys.c b/drivers/infiniband/hw/ipath/ipath_keys.c
index 85a4aefc6c03..8f32b17a5eed 100644
--- a/drivers/infiniband/hw/ipath/ipath_keys.c
+++ b/drivers/infiniband/hw/ipath/ipath_keys.c
@@ -128,9 +128,8 @@ int ipath_lkey_ok(struct ipath_qp *qp, struct ipath_sge *isge,
128 int ret; 128 int ret;
129 129
130 /* 130 /*
131 * We use LKEY == zero to mean a physical kmalloc() address. 131 * We use LKEY == zero for kernel virtual addresses
132 * This is a bit of a hack since we rely on dma_map_single() 132 * (see ipath_get_dma_mr and ipath_dma.c).
133 * being reversible by calling bus_to_virt().
134 */ 133 */
135 if (sge->lkey == 0) { 134 if (sge->lkey == 0) {
136 struct ipath_pd *pd = to_ipd(qp->ibqp.pd); 135 struct ipath_pd *pd = to_ipd(qp->ibqp.pd);
diff --git a/drivers/infiniband/hw/ipath/ipath_mad.c b/drivers/infiniband/hw/ipath/ipath_mad.c
index 3d1432d1e3f4..d98d5f103700 100644
--- a/drivers/infiniband/hw/ipath/ipath_mad.c
+++ b/drivers/infiniband/hw/ipath/ipath_mad.c
@@ -934,6 +934,7 @@ static int recv_pma_get_portsamplescontrol(struct ib_perf *pmp,
934 struct ib_pma_portsamplescontrol *p = 934 struct ib_pma_portsamplescontrol *p =
935 (struct ib_pma_portsamplescontrol *)pmp->data; 935 (struct ib_pma_portsamplescontrol *)pmp->data;
936 struct ipath_ibdev *dev = to_idev(ibdev); 936 struct ipath_ibdev *dev = to_idev(ibdev);
937 struct ipath_cregs const *crp = dev->dd->ipath_cregs;
937 unsigned long flags; 938 unsigned long flags;
938 u8 port_select = p->port_select; 939 u8 port_select = p->port_select;
939 940
@@ -955,7 +956,10 @@ static int recv_pma_get_portsamplescontrol(struct ib_perf *pmp,
955 p->counter_width = 4; /* 32 bit counters */ 956 p->counter_width = 4; /* 32 bit counters */
956 p->counter_mask0_9 = COUNTER_MASK0_9; 957 p->counter_mask0_9 = COUNTER_MASK0_9;
957 spin_lock_irqsave(&dev->pending_lock, flags); 958 spin_lock_irqsave(&dev->pending_lock, flags);
958 p->sample_status = dev->pma_sample_status; 959 if (crp->cr_psstat)
960 p->sample_status = ipath_read_creg32(dev->dd, crp->cr_psstat);
961 else
962 p->sample_status = dev->pma_sample_status;
959 p->sample_start = cpu_to_be32(dev->pma_sample_start); 963 p->sample_start = cpu_to_be32(dev->pma_sample_start);
960 p->sample_interval = cpu_to_be32(dev->pma_sample_interval); 964 p->sample_interval = cpu_to_be32(dev->pma_sample_interval);
961 p->tag = cpu_to_be16(dev->pma_tag); 965 p->tag = cpu_to_be16(dev->pma_tag);
@@ -975,8 +979,9 @@ static int recv_pma_set_portsamplescontrol(struct ib_perf *pmp,
975 struct ib_pma_portsamplescontrol *p = 979 struct ib_pma_portsamplescontrol *p =
976 (struct ib_pma_portsamplescontrol *)pmp->data; 980 (struct ib_pma_portsamplescontrol *)pmp->data;
977 struct ipath_ibdev *dev = to_idev(ibdev); 981 struct ipath_ibdev *dev = to_idev(ibdev);
982 struct ipath_cregs const *crp = dev->dd->ipath_cregs;
978 unsigned long flags; 983 unsigned long flags;
979 u32 start; 984 u8 status;
980 int ret; 985 int ret;
981 986
982 if (pmp->attr_mod != 0 || 987 if (pmp->attr_mod != 0 ||
@@ -986,59 +991,67 @@ static int recv_pma_set_portsamplescontrol(struct ib_perf *pmp,
986 goto bail; 991 goto bail;
987 } 992 }
988 993
989 start = be32_to_cpu(p->sample_start); 994 spin_lock_irqsave(&dev->pending_lock, flags);
990 if (start != 0) { 995 if (crp->cr_psstat)
991 spin_lock_irqsave(&dev->pending_lock, flags); 996 status = ipath_read_creg32(dev->dd, crp->cr_psstat);
992 if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_DONE) { 997 else
993 dev->pma_sample_status = 998 status = dev->pma_sample_status;
994 IB_PMA_SAMPLE_STATUS_STARTED; 999 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
995 dev->pma_sample_start = start; 1000 dev->pma_sample_start = be32_to_cpu(p->sample_start);
996 dev->pma_sample_interval = 1001 dev->pma_sample_interval = be32_to_cpu(p->sample_interval);
997 be32_to_cpu(p->sample_interval); 1002 dev->pma_tag = be16_to_cpu(p->tag);
998 dev->pma_tag = be16_to_cpu(p->tag); 1003 dev->pma_counter_select[0] = p->counter_select[0];
999 if (p->counter_select[0]) 1004 dev->pma_counter_select[1] = p->counter_select[1];
1000 dev->pma_counter_select[0] = 1005 dev->pma_counter_select[2] = p->counter_select[2];
1001 p->counter_select[0]; 1006 dev->pma_counter_select[3] = p->counter_select[3];
1002 if (p->counter_select[1]) 1007 dev->pma_counter_select[4] = p->counter_select[4];
1003 dev->pma_counter_select[1] = 1008 if (crp->cr_psstat) {
1004 p->counter_select[1]; 1009 ipath_write_creg(dev->dd, crp->cr_psinterval,
1005 if (p->counter_select[2]) 1010 dev->pma_sample_interval);
1006 dev->pma_counter_select[2] = 1011 ipath_write_creg(dev->dd, crp->cr_psstart,
1007 p->counter_select[2]; 1012 dev->pma_sample_start);
1008 if (p->counter_select[3]) 1013 } else
1009 dev->pma_counter_select[3] = 1014 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_STARTED;
1010 p->counter_select[3];
1011 if (p->counter_select[4])
1012 dev->pma_counter_select[4] =
1013 p->counter_select[4];
1014 }
1015 spin_unlock_irqrestore(&dev->pending_lock, flags);
1016 } 1015 }
1016 spin_unlock_irqrestore(&dev->pending_lock, flags);
1017
1017 ret = recv_pma_get_portsamplescontrol(pmp, ibdev, port); 1018 ret = recv_pma_get_portsamplescontrol(pmp, ibdev, port);
1018 1019
1019bail: 1020bail:
1020 return ret; 1021 return ret;
1021} 1022}
1022 1023
1023static u64 get_counter(struct ipath_ibdev *dev, __be16 sel) 1024static u64 get_counter(struct ipath_ibdev *dev,
1025 struct ipath_cregs const *crp,
1026 __be16 sel)
1024{ 1027{
1025 u64 ret; 1028 u64 ret;
1026 1029
1027 switch (sel) { 1030 switch (sel) {
1028 case IB_PMA_PORT_XMIT_DATA: 1031 case IB_PMA_PORT_XMIT_DATA:
1029 ret = dev->ipath_sword; 1032 ret = (crp->cr_psxmitdatacount) ?
1033 ipath_read_creg32(dev->dd, crp->cr_psxmitdatacount) :
1034 dev->ipath_sword;
1030 break; 1035 break;
1031 case IB_PMA_PORT_RCV_DATA: 1036 case IB_PMA_PORT_RCV_DATA:
1032 ret = dev->ipath_rword; 1037 ret = (crp->cr_psrcvdatacount) ?
1038 ipath_read_creg32(dev->dd, crp->cr_psrcvdatacount) :
1039 dev->ipath_rword;
1033 break; 1040 break;
1034 case IB_PMA_PORT_XMIT_PKTS: 1041 case IB_PMA_PORT_XMIT_PKTS:
1035 ret = dev->ipath_spkts; 1042 ret = (crp->cr_psxmitpktscount) ?
1043 ipath_read_creg32(dev->dd, crp->cr_psxmitpktscount) :
1044 dev->ipath_spkts;
1036 break; 1045 break;
1037 case IB_PMA_PORT_RCV_PKTS: 1046 case IB_PMA_PORT_RCV_PKTS:
1038 ret = dev->ipath_rpkts; 1047 ret = (crp->cr_psrcvpktscount) ?
1048 ipath_read_creg32(dev->dd, crp->cr_psrcvpktscount) :
1049 dev->ipath_rpkts;
1039 break; 1050 break;
1040 case IB_PMA_PORT_XMIT_WAIT: 1051 case IB_PMA_PORT_XMIT_WAIT:
1041 ret = dev->ipath_xmit_wait; 1052 ret = (crp->cr_psxmitwaitcount) ?
1053 ipath_read_creg32(dev->dd, crp->cr_psxmitwaitcount) :
1054 dev->ipath_xmit_wait;
1042 break; 1055 break;
1043 default: 1056 default:
1044 ret = 0; 1057 ret = 0;
@@ -1053,14 +1066,21 @@ static int recv_pma_get_portsamplesresult(struct ib_perf *pmp,
1053 struct ib_pma_portsamplesresult *p = 1066 struct ib_pma_portsamplesresult *p =
1054 (struct ib_pma_portsamplesresult *)pmp->data; 1067 (struct ib_pma_portsamplesresult *)pmp->data;
1055 struct ipath_ibdev *dev = to_idev(ibdev); 1068 struct ipath_ibdev *dev = to_idev(ibdev);
1069 struct ipath_cregs const *crp = dev->dd->ipath_cregs;
1070 u8 status;
1056 int i; 1071 int i;
1057 1072
1058 memset(pmp->data, 0, sizeof(pmp->data)); 1073 memset(pmp->data, 0, sizeof(pmp->data));
1059 p->tag = cpu_to_be16(dev->pma_tag); 1074 p->tag = cpu_to_be16(dev->pma_tag);
1060 p->sample_status = cpu_to_be16(dev->pma_sample_status); 1075 if (crp->cr_psstat)
1076 status = ipath_read_creg32(dev->dd, crp->cr_psstat);
1077 else
1078 status = dev->pma_sample_status;
1079 p->sample_status = cpu_to_be16(status);
1061 for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++) 1080 for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++)
1062 p->counter[i] = cpu_to_be32( 1081 p->counter[i] = (status != IB_PMA_SAMPLE_STATUS_DONE) ? 0 :
1063 get_counter(dev, dev->pma_counter_select[i])); 1082 cpu_to_be32(
1083 get_counter(dev, crp, dev->pma_counter_select[i]));
1064 1084
1065 return reply((struct ib_smp *) pmp); 1085 return reply((struct ib_smp *) pmp);
1066} 1086}
@@ -1071,16 +1091,23 @@ static int recv_pma_get_portsamplesresult_ext(struct ib_perf *pmp,
1071 struct ib_pma_portsamplesresult_ext *p = 1091 struct ib_pma_portsamplesresult_ext *p =
1072 (struct ib_pma_portsamplesresult_ext *)pmp->data; 1092 (struct ib_pma_portsamplesresult_ext *)pmp->data;
1073 struct ipath_ibdev *dev = to_idev(ibdev); 1093 struct ipath_ibdev *dev = to_idev(ibdev);
1094 struct ipath_cregs const *crp = dev->dd->ipath_cregs;
1095 u8 status;
1074 int i; 1096 int i;
1075 1097
1076 memset(pmp->data, 0, sizeof(pmp->data)); 1098 memset(pmp->data, 0, sizeof(pmp->data));
1077 p->tag = cpu_to_be16(dev->pma_tag); 1099 p->tag = cpu_to_be16(dev->pma_tag);
1078 p->sample_status = cpu_to_be16(dev->pma_sample_status); 1100 if (crp->cr_psstat)
1101 status = ipath_read_creg32(dev->dd, crp->cr_psstat);
1102 else
1103 status = dev->pma_sample_status;
1104 p->sample_status = cpu_to_be16(status);
1079 /* 64 bits */ 1105 /* 64 bits */
1080 p->extended_width = __constant_cpu_to_be32(0x80000000); 1106 p->extended_width = __constant_cpu_to_be32(0x80000000);
1081 for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++) 1107 for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++)
1082 p->counter[i] = cpu_to_be64( 1108 p->counter[i] = (status != IB_PMA_SAMPLE_STATUS_DONE) ? 0 :
1083 get_counter(dev, dev->pma_counter_select[i])); 1109 cpu_to_be64(
1110 get_counter(dev, crp, dev->pma_counter_select[i]));
1084 1111
1085 return reply((struct ib_smp *) pmp); 1112 return reply((struct ib_smp *) pmp);
1086} 1113}
@@ -1113,6 +1140,8 @@ static int recv_pma_get_portcounters(struct ib_perf *pmp,
1113 dev->z_local_link_integrity_errors; 1140 dev->z_local_link_integrity_errors;
1114 cntrs.excessive_buffer_overrun_errors -= 1141 cntrs.excessive_buffer_overrun_errors -=
1115 dev->z_excessive_buffer_overrun_errors; 1142 dev->z_excessive_buffer_overrun_errors;
1143 cntrs.vl15_dropped -= dev->z_vl15_dropped;
1144 cntrs.vl15_dropped += dev->n_vl15_dropped;
1116 1145
1117 memset(pmp->data, 0, sizeof(pmp->data)); 1146 memset(pmp->data, 0, sizeof(pmp->data));
1118 1147
@@ -1156,10 +1185,10 @@ static int recv_pma_get_portcounters(struct ib_perf *pmp,
1156 cntrs.excessive_buffer_overrun_errors = 0xFUL; 1185 cntrs.excessive_buffer_overrun_errors = 0xFUL;
1157 p->lli_ebor_errors = (cntrs.local_link_integrity_errors << 4) | 1186 p->lli_ebor_errors = (cntrs.local_link_integrity_errors << 4) |
1158 cntrs.excessive_buffer_overrun_errors; 1187 cntrs.excessive_buffer_overrun_errors;
1159 if (dev->n_vl15_dropped > 0xFFFFUL) 1188 if (cntrs.vl15_dropped > 0xFFFFUL)
1160 p->vl15_dropped = __constant_cpu_to_be16(0xFFFF); 1189 p->vl15_dropped = __constant_cpu_to_be16(0xFFFF);
1161 else 1190 else
1162 p->vl15_dropped = cpu_to_be16((u16)dev->n_vl15_dropped); 1191 p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
1163 if (cntrs.port_xmit_data > 0xFFFFFFFFUL) 1192 if (cntrs.port_xmit_data > 0xFFFFFFFFUL)
1164 p->port_xmit_data = __constant_cpu_to_be32(0xFFFFFFFF); 1193 p->port_xmit_data = __constant_cpu_to_be32(0xFFFFFFFF);
1165 else 1194 else
@@ -1262,8 +1291,10 @@ static int recv_pma_set_portcounters(struct ib_perf *pmp,
1262 dev->z_excessive_buffer_overrun_errors = 1291 dev->z_excessive_buffer_overrun_errors =
1263 cntrs.excessive_buffer_overrun_errors; 1292 cntrs.excessive_buffer_overrun_errors;
1264 1293
1265 if (p->counter_select & IB_PMA_SEL_PORT_VL15_DROPPED) 1294 if (p->counter_select & IB_PMA_SEL_PORT_VL15_DROPPED) {
1266 dev->n_vl15_dropped = 0; 1295 dev->n_vl15_dropped = 0;
1296 dev->z_vl15_dropped = cntrs.vl15_dropped;
1297 }
1267 1298
1268 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA) 1299 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA)
1269 dev->z_port_xmit_data = cntrs.port_xmit_data; 1300 dev->z_port_xmit_data = cntrs.port_xmit_data;
@@ -1434,7 +1465,7 @@ static int process_subn(struct ib_device *ibdev, int mad_flags,
1434 * before checking for other consumers. 1465 * before checking for other consumers.
1435 * Just tell the caller to process it normally. 1466 * Just tell the caller to process it normally.
1436 */ 1467 */
1437 ret = IB_MAD_RESULT_FAILURE; 1468 ret = IB_MAD_RESULT_SUCCESS;
1438 goto bail; 1469 goto bail;
1439 default: 1470 default:
1440 smp->status |= IB_SMP_UNSUP_METHOD; 1471 smp->status |= IB_SMP_UNSUP_METHOD;
@@ -1516,7 +1547,7 @@ static int process_perf(struct ib_device *ibdev, u8 port_num,
1516 * before checking for other consumers. 1547 * before checking for other consumers.
1517 * Just tell the caller to process it normally. 1548 * Just tell the caller to process it normally.
1518 */ 1549 */
1519 ret = IB_MAD_RESULT_FAILURE; 1550 ret = IB_MAD_RESULT_SUCCESS;
1520 goto bail; 1551 goto bail;
1521 default: 1552 default:
1522 pmp->status |= IB_SMP_UNSUP_METHOD; 1553 pmp->status |= IB_SMP_UNSUP_METHOD;
diff --git a/drivers/infiniband/hw/ipath/ipath_qp.c b/drivers/infiniband/hw/ipath/ipath_qp.c
index b997ff88401b..80dc623cee40 100644
--- a/drivers/infiniband/hw/ipath/ipath_qp.c
+++ b/drivers/infiniband/hw/ipath/ipath_qp.c
@@ -387,8 +387,8 @@ int ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err)
387 struct ib_wc wc; 387 struct ib_wc wc;
388 int ret = 0; 388 int ret = 0;
389 389
390 ipath_dbg("QP%d/%d in error state\n", 390 ipath_dbg("QP%d/%d in error state (%d)\n",
391 qp->ibqp.qp_num, qp->remote_qpn); 391 qp->ibqp.qp_num, qp->remote_qpn, err);
392 392
393 spin_lock(&dev->pending_lock); 393 spin_lock(&dev->pending_lock);
394 /* XXX What if its already removed by the timeout code? */ 394 /* XXX What if its already removed by the timeout code? */
@@ -855,8 +855,6 @@ struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
855 * See ipath_mmap() for details. 855 * See ipath_mmap() for details.
856 */ 856 */
857 if (udata && udata->outlen >= sizeof(__u64)) { 857 if (udata && udata->outlen >= sizeof(__u64)) {
858 int err;
859
860 if (!qp->r_rq.wq) { 858 if (!qp->r_rq.wq) {
861 __u64 offset = 0; 859 __u64 offset = 0;
862 860
diff --git a/drivers/infiniband/hw/ipath/ipath_rc.c b/drivers/infiniband/hw/ipath/ipath_rc.c
index 120a61b03bc4..459e46e2c016 100644
--- a/drivers/infiniband/hw/ipath/ipath_rc.c
+++ b/drivers/infiniband/hw/ipath/ipath_rc.c
@@ -647,6 +647,7 @@ static void send_rc_ack(struct ipath_qp *qp)
647 647
648queue_ack: 648queue_ack:
649 spin_lock_irqsave(&qp->s_lock, flags); 649 spin_lock_irqsave(&qp->s_lock, flags);
650 dev->n_rc_qacks++;
650 qp->s_flags |= IPATH_S_ACK_PENDING; 651 qp->s_flags |= IPATH_S_ACK_PENDING;
651 qp->s_nak_state = qp->r_nak_state; 652 qp->s_nak_state = qp->r_nak_state;
652 qp->s_ack_psn = qp->r_ack_psn; 653 qp->s_ack_psn = qp->r_ack_psn;
@@ -798,11 +799,13 @@ bail:
798 799
799static inline void update_last_psn(struct ipath_qp *qp, u32 psn) 800static inline void update_last_psn(struct ipath_qp *qp, u32 psn)
800{ 801{
801 if (qp->s_wait_credit) { 802 if (qp->s_last_psn != psn) {
802 qp->s_wait_credit = 0; 803 qp->s_last_psn = psn;
803 tasklet_hi_schedule(&qp->s_task); 804 if (qp->s_wait_credit) {
805 qp->s_wait_credit = 0;
806 tasklet_hi_schedule(&qp->s_task);
807 }
804 } 808 }
805 qp->s_last_psn = psn;
806} 809}
807 810
808/** 811/**
@@ -1653,13 +1656,6 @@ void ipath_rc_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
1653 case OP(SEND_FIRST): 1656 case OP(SEND_FIRST):
1654 if (!ipath_get_rwqe(qp, 0)) { 1657 if (!ipath_get_rwqe(qp, 0)) {
1655 rnr_nak: 1658 rnr_nak:
1656 /*
1657 * A RNR NAK will ACK earlier sends and RDMA writes.
1658 * Don't queue the NAK if a RDMA read or atomic
1659 * is pending though.
1660 */
1661 if (qp->r_nak_state)
1662 goto done;
1663 qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer; 1659 qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
1664 qp->r_ack_psn = qp->r_psn; 1660 qp->r_ack_psn = qp->r_psn;
1665 goto send_ack; 1661 goto send_ack;
diff --git a/drivers/infiniband/hw/ipath/ipath_registers.h b/drivers/infiniband/hw/ipath/ipath_registers.h
index 708eba3165d7..6d2a17f9c1da 100644
--- a/drivers/infiniband/hw/ipath/ipath_registers.h
+++ b/drivers/infiniband/hw/ipath/ipath_registers.h
@@ -82,8 +82,7 @@
82 82
83/* kr_rcvctrl bits */ 83/* kr_rcvctrl bits */
84#define INFINIPATH_R_PORTENABLE_SHIFT 0 84#define INFINIPATH_R_PORTENABLE_SHIFT 0
85#define INFINIPATH_R_INTRAVAIL_SHIFT 16 85#define INFINIPATH_R_QPMAP_ENABLE (1ULL << 38)
86#define INFINIPATH_R_TAILUPD 0x80000000
87 86
88/* kr_intstatus, kr_intclear, kr_intmask bits */ 87/* kr_intstatus, kr_intclear, kr_intmask bits */
89#define INFINIPATH_I_RCVURG_SHIFT 0 88#define INFINIPATH_I_RCVURG_SHIFT 0
@@ -272,20 +271,6 @@
272#define INFINIPATH_EXTC_LEDGBLOK_ON 0x00000002ULL 271#define INFINIPATH_EXTC_LEDGBLOK_ON 0x00000002ULL
273#define INFINIPATH_EXTC_LEDGBLERR_OFF 0x00000001ULL 272#define INFINIPATH_EXTC_LEDGBLERR_OFF 0x00000001ULL
274 273
275/* kr_mdio bits */
276#define INFINIPATH_MDIO_CLKDIV_MASK 0x7FULL
277#define INFINIPATH_MDIO_CLKDIV_SHIFT 32
278#define INFINIPATH_MDIO_COMMAND_MASK 0x7ULL
279#define INFINIPATH_MDIO_COMMAND_SHIFT 26
280#define INFINIPATH_MDIO_DEVADDR_MASK 0x1FULL
281#define INFINIPATH_MDIO_DEVADDR_SHIFT 21
282#define INFINIPATH_MDIO_REGADDR_MASK 0x1FULL
283#define INFINIPATH_MDIO_REGADDR_SHIFT 16
284#define INFINIPATH_MDIO_DATA_MASK 0xFFFFULL
285#define INFINIPATH_MDIO_DATA_SHIFT 0
286#define INFINIPATH_MDIO_CMDVALID 0x0000000040000000ULL
287#define INFINIPATH_MDIO_RDDATAVALID 0x0000000080000000ULL
288
289/* kr_partitionkey bits */ 274/* kr_partitionkey bits */
290#define INFINIPATH_PKEY_SIZE 16 275#define INFINIPATH_PKEY_SIZE 16
291#define INFINIPATH_PKEY_MASK 0xFFFF 276#define INFINIPATH_PKEY_MASK 0xFFFF
@@ -303,8 +288,6 @@
303 288
304/* kr_xgxsconfig bits */ 289/* kr_xgxsconfig bits */
305#define INFINIPATH_XGXS_RESET 0x7ULL 290#define INFINIPATH_XGXS_RESET 0x7ULL
306#define INFINIPATH_XGXS_MDIOADDR_MASK 0xfULL
307#define INFINIPATH_XGXS_MDIOADDR_SHIFT 4
308#define INFINIPATH_XGXS_RX_POL_SHIFT 19 291#define INFINIPATH_XGXS_RX_POL_SHIFT 19
309#define INFINIPATH_XGXS_RX_POL_MASK 0xfULL 292#define INFINIPATH_XGXS_RX_POL_MASK 0xfULL
310 293
@@ -470,6 +453,20 @@ struct ipath_cregs {
470 ipath_creg cr_unsupvlcnt; 453 ipath_creg cr_unsupvlcnt;
471 ipath_creg cr_wordrcvcnt; 454 ipath_creg cr_wordrcvcnt;
472 ipath_creg cr_wordsendcnt; 455 ipath_creg cr_wordsendcnt;
456 ipath_creg cr_vl15droppedpktcnt;
457 ipath_creg cr_rxotherlocalphyerrcnt;
458 ipath_creg cr_excessbufferovflcnt;
459 ipath_creg cr_locallinkintegrityerrcnt;
460 ipath_creg cr_rxvlerrcnt;
461 ipath_creg cr_rxdlidfltrcnt;
462 ipath_creg cr_psstat;
463 ipath_creg cr_psstart;
464 ipath_creg cr_psinterval;
465 ipath_creg cr_psrcvdatacount;
466 ipath_creg cr_psrcvpktscount;
467 ipath_creg cr_psxmitdatacount;
468 ipath_creg cr_psxmitpktscount;
469 ipath_creg cr_psxmitwaitcount;
473}; 470};
474 471
475#endif /* _IPATH_REGISTERS_H */ 472#endif /* _IPATH_REGISTERS_H */
diff --git a/drivers/infiniband/hw/ipath/ipath_ruc.c b/drivers/infiniband/hw/ipath/ipath_ruc.c
index 54c61a972de2..a59bdbd0ed87 100644
--- a/drivers/infiniband/hw/ipath/ipath_ruc.c
+++ b/drivers/infiniband/hw/ipath/ipath_ruc.c
@@ -98,11 +98,15 @@ void ipath_insert_rnr_queue(struct ipath_qp *qp)
98 while (qp->s_rnr_timeout >= nqp->s_rnr_timeout) { 98 while (qp->s_rnr_timeout >= nqp->s_rnr_timeout) {
99 qp->s_rnr_timeout -= nqp->s_rnr_timeout; 99 qp->s_rnr_timeout -= nqp->s_rnr_timeout;
100 l = l->next; 100 l = l->next;
101 if (l->next == &dev->rnrwait) 101 if (l->next == &dev->rnrwait) {
102 nqp = NULL;
102 break; 103 break;
104 }
103 nqp = list_entry(l->next, struct ipath_qp, 105 nqp = list_entry(l->next, struct ipath_qp,
104 timerwait); 106 timerwait);
105 } 107 }
108 if (nqp)
109 nqp->s_rnr_timeout -= qp->s_rnr_timeout;
106 list_add(&qp->timerwait, l); 110 list_add(&qp->timerwait, l);
107 } 111 }
108 spin_unlock_irqrestore(&dev->pending_lock, flags); 112 spin_unlock_irqrestore(&dev->pending_lock, flags);
@@ -479,9 +483,14 @@ done:
479 483
480static void want_buffer(struct ipath_devdata *dd) 484static void want_buffer(struct ipath_devdata *dd)
481{ 485{
482 set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl); 486 unsigned long flags;
487
488 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
489 dd->ipath_sendctrl |= INFINIPATH_S_PIOINTBUFAVAIL;
483 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 490 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
484 dd->ipath_sendctrl); 491 dd->ipath_sendctrl);
492 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
493 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
485} 494}
486 495
487/** 496/**
diff --git a/drivers/infiniband/hw/ipath/ipath_srq.c b/drivers/infiniband/hw/ipath/ipath_srq.c
index 2fef36f4b675..f772102e4713 100644
--- a/drivers/infiniband/hw/ipath/ipath_srq.c
+++ b/drivers/infiniband/hw/ipath/ipath_srq.c
@@ -94,8 +94,8 @@ bail:
94/** 94/**
95 * ipath_create_srq - create a shared receive queue 95 * ipath_create_srq - create a shared receive queue
96 * @ibpd: the protection domain of the SRQ to create 96 * @ibpd: the protection domain of the SRQ to create
97 * @attr: the attributes of the SRQ 97 * @srq_init_attr: the attributes of the SRQ
98 * @udata: not used by the InfiniPath verbs driver 98 * @udata: data from libipathverbs when creating a user SRQ
99 */ 99 */
100struct ib_srq *ipath_create_srq(struct ib_pd *ibpd, 100struct ib_srq *ipath_create_srq(struct ib_pd *ibpd,
101 struct ib_srq_init_attr *srq_init_attr, 101 struct ib_srq_init_attr *srq_init_attr,
diff --git a/drivers/infiniband/hw/ipath/ipath_stats.c b/drivers/infiniband/hw/ipath/ipath_stats.c
index f0271415cd5b..d2725cd11bdc 100644
--- a/drivers/infiniband/hw/ipath/ipath_stats.c
+++ b/drivers/infiniband/hw/ipath/ipath_stats.c
@@ -133,15 +133,16 @@ bail:
133static void ipath_qcheck(struct ipath_devdata *dd) 133static void ipath_qcheck(struct ipath_devdata *dd)
134{ 134{
135 static u64 last_tot_hdrqfull; 135 static u64 last_tot_hdrqfull;
136 struct ipath_portdata *pd = dd->ipath_pd[0];
136 size_t blen = 0; 137 size_t blen = 0;
137 char buf[128]; 138 char buf[128];
138 139
139 *buf = 0; 140 *buf = 0;
140 if (dd->ipath_pd[0]->port_hdrqfull != dd->ipath_p0_hdrqfull) { 141 if (pd->port_hdrqfull != dd->ipath_p0_hdrqfull) {
141 blen = snprintf(buf, sizeof buf, "port 0 hdrqfull %u", 142 blen = snprintf(buf, sizeof buf, "port 0 hdrqfull %u",
142 dd->ipath_pd[0]->port_hdrqfull - 143 pd->port_hdrqfull -
143 dd->ipath_p0_hdrqfull); 144 dd->ipath_p0_hdrqfull);
144 dd->ipath_p0_hdrqfull = dd->ipath_pd[0]->port_hdrqfull; 145 dd->ipath_p0_hdrqfull = pd->port_hdrqfull;
145 } 146 }
146 if (ipath_stats.sps_etidfull != dd->ipath_last_tidfull) { 147 if (ipath_stats.sps_etidfull != dd->ipath_last_tidfull) {
147 blen += snprintf(buf + blen, sizeof buf - blen, 148 blen += snprintf(buf + blen, sizeof buf - blen,
@@ -173,7 +174,7 @@ static void ipath_qcheck(struct ipath_devdata *dd)
173 if (blen) 174 if (blen)
174 ipath_dbg("%s\n", buf); 175 ipath_dbg("%s\n", buf);
175 176
176 if (dd->ipath_port0head != (u32) 177 if (pd->port_head != (u32)
177 le64_to_cpu(*dd->ipath_hdrqtailptr)) { 178 le64_to_cpu(*dd->ipath_hdrqtailptr)) {
178 if (dd->ipath_lastport0rcv_cnt == 179 if (dd->ipath_lastport0rcv_cnt ==
179 ipath_stats.sps_port0pkts) { 180 ipath_stats.sps_port0pkts) {
@@ -181,7 +182,7 @@ static void ipath_qcheck(struct ipath_devdata *dd)
181 "port0 hd=%llx tl=%x; port0pkts %llx\n", 182 "port0 hd=%llx tl=%x; port0pkts %llx\n",
182 (unsigned long long) 183 (unsigned long long)
183 le64_to_cpu(*dd->ipath_hdrqtailptr), 184 le64_to_cpu(*dd->ipath_hdrqtailptr),
184 dd->ipath_port0head, 185 pd->port_head,
185 (unsigned long long) 186 (unsigned long long)
186 ipath_stats.sps_port0pkts); 187 ipath_stats.sps_port0pkts);
187 } 188 }
@@ -237,7 +238,7 @@ static void ipath_chk_errormask(struct ipath_devdata *dd)
237void ipath_get_faststats(unsigned long opaque) 238void ipath_get_faststats(unsigned long opaque)
238{ 239{
239 struct ipath_devdata *dd = (struct ipath_devdata *) opaque; 240 struct ipath_devdata *dd = (struct ipath_devdata *) opaque;
240 u32 val; 241 int i;
241 static unsigned cnt; 242 static unsigned cnt;
242 unsigned long flags; 243 unsigned long flags;
243 u64 traffic_wds; 244 u64 traffic_wds;
@@ -321,12 +322,11 @@ void ipath_get_faststats(unsigned long opaque)
321 322
322 /* limit qfull messages to ~one per minute per port */ 323 /* limit qfull messages to ~one per minute per port */
323 if ((++cnt & 0x10)) { 324 if ((++cnt & 0x10)) {
324 for (val = dd->ipath_cfgports - 1; ((int)val) >= 0; 325 for (i = (int) dd->ipath_cfgports; --i >= 0; ) {
325 val--) { 326 struct ipath_portdata *pd = dd->ipath_pd[i];
326 if (dd->ipath_lastegrheads[val] != -1) 327
327 dd->ipath_lastegrheads[val] = -1; 328 if (pd && pd->port_lastrcvhdrqtail != -1)
328 if (dd->ipath_lastrcvhdrqtails[val] != -1) 329 pd->port_lastrcvhdrqtail = -1;
329 dd->ipath_lastrcvhdrqtails[val] = -1;
330 } 330 }
331 } 331 }
332 332
diff --git a/drivers/infiniband/hw/ipath/ipath_sysfs.c b/drivers/infiniband/hw/ipath/ipath_sysfs.c
index aa27ca9f03b1..56dfc8a2344c 100644
--- a/drivers/infiniband/hw/ipath/ipath_sysfs.c
+++ b/drivers/infiniband/hw/ipath/ipath_sysfs.c
@@ -363,6 +363,60 @@ static ssize_t show_unit(struct device *dev,
363 return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_unit); 363 return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_unit);
364} 364}
365 365
366static ssize_t show_jint_max_packets(struct device *dev,
367 struct device_attribute *attr,
368 char *buf)
369{
370 struct ipath_devdata *dd = dev_get_drvdata(dev);
371
372 return scnprintf(buf, PAGE_SIZE, "%hu\n", dd->ipath_jint_max_packets);
373}
374
375static ssize_t store_jint_max_packets(struct device *dev,
376 struct device_attribute *attr,
377 const char *buf,
378 size_t count)
379{
380 struct ipath_devdata *dd = dev_get_drvdata(dev);
381 u16 v = 0;
382 int ret;
383
384 ret = ipath_parse_ushort(buf, &v);
385 if (ret < 0)
386 ipath_dev_err(dd, "invalid jint_max_packets.\n");
387 else
388 dd->ipath_f_config_jint(dd, dd->ipath_jint_idle_ticks, v);
389
390 return ret;
391}
392
393static ssize_t show_jint_idle_ticks(struct device *dev,
394 struct device_attribute *attr,
395 char *buf)
396{
397 struct ipath_devdata *dd = dev_get_drvdata(dev);
398
399 return scnprintf(buf, PAGE_SIZE, "%hu\n", dd->ipath_jint_idle_ticks);
400}
401
402static ssize_t store_jint_idle_ticks(struct device *dev,
403 struct device_attribute *attr,
404 const char *buf,
405 size_t count)
406{
407 struct ipath_devdata *dd = dev_get_drvdata(dev);
408 u16 v = 0;
409 int ret;
410
411 ret = ipath_parse_ushort(buf, &v);
412 if (ret < 0)
413 ipath_dev_err(dd, "invalid jint_idle_ticks.\n");
414 else
415 dd->ipath_f_config_jint(dd, v, dd->ipath_jint_max_packets);
416
417 return ret;
418}
419
366#define DEVICE_COUNTER(name, attr) \ 420#define DEVICE_COUNTER(name, attr) \
367 static ssize_t show_counter_##name(struct device *dev, \ 421 static ssize_t show_counter_##name(struct device *dev, \
368 struct device_attribute *attr, \ 422 struct device_attribute *attr, \
@@ -670,6 +724,257 @@ static ssize_t show_logged_errs(struct device *dev,
670 return count; 724 return count;
671} 725}
672 726
727/*
728 * New sysfs entries to control various IB config. These all turn into
729 * accesses via ipath_f_get/set_ib_cfg.
730 *
731 * Get/Set heartbeat enable. Or of 1=enabled, 2=auto
732 */
733static ssize_t show_hrtbt_enb(struct device *dev,
734 struct device_attribute *attr,
735 char *buf)
736{
737 struct ipath_devdata *dd = dev_get_drvdata(dev);
738 int ret;
739
740 ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_HRTBT);
741 if (ret >= 0)
742 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
743 return ret;
744}
745
746static ssize_t store_hrtbt_enb(struct device *dev,
747 struct device_attribute *attr,
748 const char *buf,
749 size_t count)
750{
751 struct ipath_devdata *dd = dev_get_drvdata(dev);
752 int ret, r;
753 u16 val;
754
755 ret = ipath_parse_ushort(buf, &val);
756 if (ret >= 0 && val > 3)
757 ret = -EINVAL;
758 if (ret < 0) {
759 ipath_dev_err(dd, "attempt to set invalid Heartbeat enable\n");
760 goto bail;
761 }
762
763 /*
764 * Set the "intentional" heartbeat enable per either of
765 * "Enable" and "Auto", as these are normally set together.
766 * This bit is consulted when leaving loopback mode,
767 * because entering loopback mode overrides it and automatically
768 * disables heartbeat.
769 */
770 r = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_HRTBT, val);
771 if (r < 0)
772 ret = r;
773 else if (val == IPATH_IB_HRTBT_OFF)
774 dd->ipath_flags |= IPATH_NO_HRTBT;
775 else
776 dd->ipath_flags &= ~IPATH_NO_HRTBT;
777
778bail:
779 return ret;
780}
781
782/*
783 * Get/Set Link-widths enabled. Or of 1=1x, 2=4x (this is human/IB centric,
784 * _not_ the particular encoding of any given chip)
785 */
786static ssize_t show_lwid_enb(struct device *dev,
787 struct device_attribute *attr,
788 char *buf)
789{
790 struct ipath_devdata *dd = dev_get_drvdata(dev);
791 int ret;
792
793 ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_LWID_ENB);
794 if (ret >= 0)
795 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
796 return ret;
797}
798
799static ssize_t store_lwid_enb(struct device *dev,
800 struct device_attribute *attr,
801 const char *buf,
802 size_t count)
803{
804 struct ipath_devdata *dd = dev_get_drvdata(dev);
805 int ret, r;
806 u16 val;
807
808 ret = ipath_parse_ushort(buf, &val);
809 if (ret >= 0 && (val == 0 || val > 3))
810 ret = -EINVAL;
811 if (ret < 0) {
812 ipath_dev_err(dd,
813 "attempt to set invalid Link Width (enable)\n");
814 goto bail;
815 }
816
817 r = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_LWID_ENB, val);
818 if (r < 0)
819 ret = r;
820
821bail:
822 return ret;
823}
824
825/* Get current link width */
826static ssize_t show_lwid(struct device *dev,
827 struct device_attribute *attr,
828 char *buf)
829
830{
831 struct ipath_devdata *dd = dev_get_drvdata(dev);
832 int ret;
833
834 ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_LWID);
835 if (ret >= 0)
836 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
837 return ret;
838}
839
840/*
841 * Get/Set Link-speeds enabled. Or of 1=SDR 2=DDR.
842 */
843static ssize_t show_spd_enb(struct device *dev,
844 struct device_attribute *attr,
845 char *buf)
846{
847 struct ipath_devdata *dd = dev_get_drvdata(dev);
848 int ret;
849
850 ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_SPD_ENB);
851 if (ret >= 0)
852 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
853 return ret;
854}
855
856static ssize_t store_spd_enb(struct device *dev,
857 struct device_attribute *attr,
858 const char *buf,
859 size_t count)
860{
861 struct ipath_devdata *dd = dev_get_drvdata(dev);
862 int ret, r;
863 u16 val;
864
865 ret = ipath_parse_ushort(buf, &val);
866 if (ret >= 0 && (val == 0 || val > (IPATH_IB_SDR | IPATH_IB_DDR)))
867 ret = -EINVAL;
868 if (ret < 0) {
869 ipath_dev_err(dd,
870 "attempt to set invalid Link Speed (enable)\n");
871 goto bail;
872 }
873
874 r = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_SPD_ENB, val);
875 if (r < 0)
876 ret = r;
877
878bail:
879 return ret;
880}
881
882/* Get current link speed */
883static ssize_t show_spd(struct device *dev,
884 struct device_attribute *attr,
885 char *buf)
886{
887 struct ipath_devdata *dd = dev_get_drvdata(dev);
888 int ret;
889
890 ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_SPD);
891 if (ret >= 0)
892 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
893 return ret;
894}
895
896/*
897 * Get/Set RX polarity-invert enable. 0=no, 1=yes.
898 */
899static ssize_t show_rx_polinv_enb(struct device *dev,
900 struct device_attribute *attr,
901 char *buf)
902{
903 struct ipath_devdata *dd = dev_get_drvdata(dev);
904 int ret;
905
906 ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_RXPOL_ENB);
907 if (ret >= 0)
908 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
909 return ret;
910}
911
912static ssize_t store_rx_polinv_enb(struct device *dev,
913 struct device_attribute *attr,
914 const char *buf,
915 size_t count)
916{
917 struct ipath_devdata *dd = dev_get_drvdata(dev);
918 int ret, r;
919 u16 val;
920
921 ret = ipath_parse_ushort(buf, &val);
922 if (ret < 0 || val > 1)
923 goto invalid;
924
925 r = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_RXPOL_ENB, val);
926 if (r < 0) {
927 ret = r;
928 goto bail;
929 }
930
931 goto bail;
932invalid:
933 ipath_dev_err(dd, "attempt to set invalid Rx Polarity (enable)\n");
934bail:
935 return ret;
936}
937/*
938 * Get/Set RX lane-reversal enable. 0=no, 1=yes.
939 */
940static ssize_t show_lanerev_enb(struct device *dev,
941 struct device_attribute *attr,
942 char *buf)
943{
944 struct ipath_devdata *dd = dev_get_drvdata(dev);
945 int ret;
946
947 ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_LREV_ENB);
948 if (ret >= 0)
949 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
950 return ret;
951}
952
953static ssize_t store_lanerev_enb(struct device *dev,
954 struct device_attribute *attr,
955 const char *buf,
956 size_t count)
957{
958 struct ipath_devdata *dd = dev_get_drvdata(dev);
959 int ret, r;
960 u16 val;
961
962 ret = ipath_parse_ushort(buf, &val);
963 if (ret >= 0 && val > 1) {
964 ret = -EINVAL;
965 ipath_dev_err(dd,
966 "attempt to set invalid Lane reversal (enable)\n");
967 goto bail;
968 }
969
970 r = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_LREV_ENB, val);
971 if (r < 0)
972 ret = r;
973
974bail:
975 return ret;
976}
977
673static DRIVER_ATTR(num_units, S_IRUGO, show_num_units, NULL); 978static DRIVER_ATTR(num_units, S_IRUGO, show_num_units, NULL);
674static DRIVER_ATTR(version, S_IRUGO, show_version, NULL); 979static DRIVER_ATTR(version, S_IRUGO, show_version, NULL);
675 980
@@ -706,6 +1011,10 @@ static DEVICE_ATTR(unit, S_IRUGO, show_unit, NULL);
706static DEVICE_ATTR(rx_pol_inv, S_IWUSR, NULL, store_rx_pol_inv); 1011static DEVICE_ATTR(rx_pol_inv, S_IWUSR, NULL, store_rx_pol_inv);
707static DEVICE_ATTR(led_override, S_IWUSR, NULL, store_led_override); 1012static DEVICE_ATTR(led_override, S_IWUSR, NULL, store_led_override);
708static DEVICE_ATTR(logged_errors, S_IRUGO, show_logged_errs, NULL); 1013static DEVICE_ATTR(logged_errors, S_IRUGO, show_logged_errs, NULL);
1014static DEVICE_ATTR(jint_max_packets, S_IWUSR | S_IRUGO,
1015 show_jint_max_packets, store_jint_max_packets);
1016static DEVICE_ATTR(jint_idle_ticks, S_IWUSR | S_IRUGO,
1017 show_jint_idle_ticks, store_jint_idle_ticks);
709 1018
710static struct attribute *dev_attributes[] = { 1019static struct attribute *dev_attributes[] = {
711 &dev_attr_guid.attr, 1020 &dev_attr_guid.attr,
@@ -732,6 +1041,34 @@ static struct attribute_group dev_attr_group = {
732 .attrs = dev_attributes 1041 .attrs = dev_attributes
733}; 1042};
734 1043
1044static DEVICE_ATTR(hrtbt_enable, S_IWUSR | S_IRUGO, show_hrtbt_enb,
1045 store_hrtbt_enb);
1046static DEVICE_ATTR(link_width_enable, S_IWUSR | S_IRUGO, show_lwid_enb,
1047 store_lwid_enb);
1048static DEVICE_ATTR(link_width, S_IRUGO, show_lwid, NULL);
1049static DEVICE_ATTR(link_speed_enable, S_IWUSR | S_IRUGO, show_spd_enb,
1050 store_spd_enb);
1051static DEVICE_ATTR(link_speed, S_IRUGO, show_spd, NULL);
1052static DEVICE_ATTR(rx_pol_inv_enable, S_IWUSR | S_IRUGO, show_rx_polinv_enb,
1053 store_rx_polinv_enb);
1054static DEVICE_ATTR(rx_lane_rev_enable, S_IWUSR | S_IRUGO, show_lanerev_enb,
1055 store_lanerev_enb);
1056
1057static struct attribute *dev_ibcfg_attributes[] = {
1058 &dev_attr_hrtbt_enable.attr,
1059 &dev_attr_link_width_enable.attr,
1060 &dev_attr_link_width.attr,
1061 &dev_attr_link_speed_enable.attr,
1062 &dev_attr_link_speed.attr,
1063 &dev_attr_rx_pol_inv_enable.attr,
1064 &dev_attr_rx_lane_rev_enable.attr,
1065 NULL
1066};
1067
1068static struct attribute_group dev_ibcfg_attr_group = {
1069 .attrs = dev_ibcfg_attributes
1070};
1071
735/** 1072/**
736 * ipath_expose_reset - create a device reset file 1073 * ipath_expose_reset - create a device reset file
737 * @dev: the device structure 1074 * @dev: the device structure
@@ -770,6 +1107,26 @@ int ipath_device_create_group(struct device *dev, struct ipath_devdata *dd)
770 if (ret) 1107 if (ret)
771 goto bail_attrs; 1108 goto bail_attrs;
772 1109
1110 if (dd->ipath_flags & IPATH_HAS_MULT_IB_SPEED) {
1111 ret = device_create_file(dev, &dev_attr_jint_idle_ticks);
1112 if (ret)
1113 goto bail_counter;
1114 ret = device_create_file(dev, &dev_attr_jint_max_packets);
1115 if (ret)
1116 goto bail_idle;
1117
1118 ret = sysfs_create_group(&dev->kobj, &dev_ibcfg_attr_group);
1119 if (ret)
1120 goto bail_max;
1121 }
1122
1123 return 0;
1124
1125bail_max:
1126 device_remove_file(dev, &dev_attr_jint_max_packets);
1127bail_idle:
1128 device_remove_file(dev, &dev_attr_jint_idle_ticks);
1129bail_counter:
773 sysfs_remove_group(&dev->kobj, &dev_counter_attr_group); 1130 sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
774bail_attrs: 1131bail_attrs:
775 sysfs_remove_group(&dev->kobj, &dev_attr_group); 1132 sysfs_remove_group(&dev->kobj, &dev_attr_group);
@@ -780,6 +1137,13 @@ bail:
780void ipath_device_remove_group(struct device *dev, struct ipath_devdata *dd) 1137void ipath_device_remove_group(struct device *dev, struct ipath_devdata *dd)
781{ 1138{
782 sysfs_remove_group(&dev->kobj, &dev_counter_attr_group); 1139 sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
1140
1141 if (dd->ipath_flags & IPATH_HAS_MULT_IB_SPEED) {
1142 sysfs_remove_group(&dev->kobj, &dev_ibcfg_attr_group);
1143 device_remove_file(dev, &dev_attr_jint_idle_ticks);
1144 device_remove_file(dev, &dev_attr_jint_max_packets);
1145 }
1146
783 sysfs_remove_group(&dev->kobj, &dev_attr_group); 1147 sysfs_remove_group(&dev->kobj, &dev_attr_group);
784 1148
785 device_remove_file(dev, &dev_attr_reset); 1149 device_remove_file(dev, &dev_attr_reset);
diff --git a/drivers/infiniband/hw/ipath/ipath_ud.c b/drivers/infiniband/hw/ipath/ipath_ud.c
index b3df6f3c705e..de67eed08ed0 100644
--- a/drivers/infiniband/hw/ipath/ipath_ud.c
+++ b/drivers/infiniband/hw/ipath/ipath_ud.c
@@ -301,8 +301,6 @@ int ipath_make_ud_req(struct ipath_qp *qp)
301 301
302 /* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */ 302 /* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
303 qp->s_hdrwords = 7; 303 qp->s_hdrwords = 7;
304 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
305 qp->s_hdrwords++;
306 qp->s_cur_size = wqe->length; 304 qp->s_cur_size = wqe->length;
307 qp->s_cur_sge = &qp->s_sge; 305 qp->s_cur_sge = &qp->s_sge;
308 qp->s_wqe = wqe; 306 qp->s_wqe = wqe;
@@ -327,6 +325,7 @@ int ipath_make_ud_req(struct ipath_qp *qp)
327 ohdr = &qp->s_hdr.u.oth; 325 ohdr = &qp->s_hdr.u.oth;
328 } 326 }
329 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) { 327 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
328 qp->s_hdrwords++;
330 ohdr->u.ud.imm_data = wqe->wr.imm_data; 329 ohdr->u.ud.imm_data = wqe->wr.imm_data;
331 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24; 330 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
332 } else 331 } else
diff --git a/drivers/infiniband/hw/ipath/ipath_verbs.c b/drivers/infiniband/hw/ipath/ipath_verbs.c
index c4c998446c7b..32d8f882e56c 100644
--- a/drivers/infiniband/hw/ipath/ipath_verbs.c
+++ b/drivers/infiniband/hw/ipath/ipath_verbs.c
@@ -943,7 +943,7 @@ bail:
943 * ipath_verbs_send - send a packet 943 * ipath_verbs_send - send a packet
944 * @qp: the QP to send on 944 * @qp: the QP to send on
945 * @hdr: the packet header 945 * @hdr: the packet header
946 * @hdrwords: the number of words in the header 946 * @hdrwords: the number of 32-bit words in the header
947 * @ss: the SGE to send 947 * @ss: the SGE to send
948 * @len: the length of the packet in bytes 948 * @len: the length of the packet in bytes
949 */ 949 */
@@ -955,7 +955,10 @@ int ipath_verbs_send(struct ipath_qp *qp, struct ipath_ib_header *hdr,
955 int ret; 955 int ret;
956 u32 dwords = (len + 3) >> 2; 956 u32 dwords = (len + 3) >> 2;
957 957
958 /* +1 is for the qword padding of pbc */ 958 /*
959 * Calculate the send buffer trigger address.
960 * The +1 counts for the pbc control dword following the pbc length.
961 */
959 plen = hdrwords + dwords + 1; 962 plen = hdrwords + dwords + 1;
960 963
961 /* Drop non-VL15 packets if we are not in the active state */ 964 /* Drop non-VL15 packets if we are not in the active state */
@@ -1130,20 +1133,34 @@ static int ipath_query_device(struct ib_device *ibdev,
1130 return 0; 1133 return 0;
1131} 1134}
1132 1135
1133const u8 ipath_cvt_physportstate[16] = { 1136const u8 ipath_cvt_physportstate[32] = {
1134 [INFINIPATH_IBCS_LT_STATE_DISABLED] = 3, 1137 [INFINIPATH_IBCS_LT_STATE_DISABLED] = IB_PHYSPORTSTATE_DISABLED,
1135 [INFINIPATH_IBCS_LT_STATE_LINKUP] = 5, 1138 [INFINIPATH_IBCS_LT_STATE_LINKUP] = IB_PHYSPORTSTATE_LINKUP,
1136 [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = 2, 1139 [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = IB_PHYSPORTSTATE_POLL,
1137 [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = 2, 1140 [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = IB_PHYSPORTSTATE_POLL,
1138 [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = 1, 1141 [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = IB_PHYSPORTSTATE_SLEEP,
1139 [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = 1, 1142 [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = IB_PHYSPORTSTATE_SLEEP,
1140 [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] = 4, 1143 [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] =
1141 [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] = 4, 1144 IB_PHYSPORTSTATE_CFG_TRAIN,
1142 [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] = 4, 1145 [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] =
1143 [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = 4, 1146 IB_PHYSPORTSTATE_CFG_TRAIN,
1144 [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] = 6, 1147 [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] =
1145 [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] = 6, 1148 IB_PHYSPORTSTATE_CFG_TRAIN,
1146 [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] = 6, 1149 [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = IB_PHYSPORTSTATE_CFG_TRAIN,
1150 [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] =
1151 IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
1152 [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] =
1153 IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
1154 [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] =
1155 IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
1156 [0x10] = IB_PHYSPORTSTATE_CFG_TRAIN,
1157 [0x11] = IB_PHYSPORTSTATE_CFG_TRAIN,
1158 [0x12] = IB_PHYSPORTSTATE_CFG_TRAIN,
1159 [0x13] = IB_PHYSPORTSTATE_CFG_TRAIN,
1160 [0x14] = IB_PHYSPORTSTATE_CFG_TRAIN,
1161 [0x15] = IB_PHYSPORTSTATE_CFG_TRAIN,
1162 [0x16] = IB_PHYSPORTSTATE_CFG_TRAIN,
1163 [0x17] = IB_PHYSPORTSTATE_CFG_TRAIN
1147}; 1164};
1148 1165
1149u32 ipath_get_cr_errpkey(struct ipath_devdata *dd) 1166u32 ipath_get_cr_errpkey(struct ipath_devdata *dd)
@@ -1168,8 +1185,9 @@ static int ipath_query_port(struct ib_device *ibdev,
1168 ibcstat = dd->ipath_lastibcstat; 1185 ibcstat = dd->ipath_lastibcstat;
1169 props->state = ((ibcstat >> 4) & 0x3) + 1; 1186 props->state = ((ibcstat >> 4) & 0x3) + 1;
1170 /* See phys_state_show() */ 1187 /* See phys_state_show() */
1171 props->phys_state = ipath_cvt_physportstate[ 1188 props->phys_state = /* MEA: assumes shift == 0 */
1172 dd->ipath_lastibcstat & 0xf]; 1189 ipath_cvt_physportstate[dd->ipath_lastibcstat &
1190 dd->ibcs_lts_mask];
1173 props->port_cap_flags = dev->port_cap_flags; 1191 props->port_cap_flags = dev->port_cap_flags;
1174 props->gid_tbl_len = 1; 1192 props->gid_tbl_len = 1;
1175 props->max_msg_sz = 0x80000000; 1193 props->max_msg_sz = 0x80000000;
@@ -1641,6 +1659,7 @@ int ipath_register_ib_device(struct ipath_devdata *dd)
1641 cntrs.local_link_integrity_errors; 1659 cntrs.local_link_integrity_errors;
1642 idev->z_excessive_buffer_overrun_errors = 1660 idev->z_excessive_buffer_overrun_errors =
1643 cntrs.excessive_buffer_overrun_errors; 1661 cntrs.excessive_buffer_overrun_errors;
1662 idev->z_vl15_dropped = cntrs.vl15_dropped;
1644 1663
1645 /* 1664 /*
1646 * The system image GUID is supposed to be the same for all 1665 * The system image GUID is supposed to be the same for all
diff --git a/drivers/infiniband/hw/ipath/ipath_verbs.h b/drivers/infiniband/hw/ipath/ipath_verbs.h
index 6ccb54f104a3..3d59736b49b2 100644
--- a/drivers/infiniband/hw/ipath/ipath_verbs.h
+++ b/drivers/infiniband/hw/ipath/ipath_verbs.h
@@ -554,6 +554,7 @@ struct ipath_ibdev {
554 u32 z_pkey_violations; /* starting count for PMA */ 554 u32 z_pkey_violations; /* starting count for PMA */
555 u32 z_local_link_integrity_errors; /* starting count for PMA */ 555 u32 z_local_link_integrity_errors; /* starting count for PMA */
556 u32 z_excessive_buffer_overrun_errors; /* starting count for PMA */ 556 u32 z_excessive_buffer_overrun_errors; /* starting count for PMA */
557 u32 z_vl15_dropped; /* starting count for PMA */
557 u32 n_rc_resends; 558 u32 n_rc_resends;
558 u32 n_rc_acks; 559 u32 n_rc_acks;
559 u32 n_rc_qacks; 560 u32 n_rc_qacks;
@@ -598,6 +599,7 @@ struct ipath_verbs_counters {
598 u64 port_rcv_packets; 599 u64 port_rcv_packets;
599 u32 local_link_integrity_errors; 600 u32 local_link_integrity_errors;
600 u32 excessive_buffer_overrun_errors; 601 u32 excessive_buffer_overrun_errors;
602 u32 vl15_dropped;
601}; 603};
602 604
603static inline struct ipath_mr *to_imr(struct ib_mr *ibmr) 605static inline struct ipath_mr *to_imr(struct ib_mr *ibmr)
@@ -830,7 +832,17 @@ unsigned ipath_get_pkey(struct ipath_devdata *, unsigned);
830 832
831extern const enum ib_wc_opcode ib_ipath_wc_opcode[]; 833extern const enum ib_wc_opcode ib_ipath_wc_opcode[];
832 834
835/*
836 * Below converts HCA-specific LinkTrainingState to IB PhysPortState
837 * values.
838 */
833extern const u8 ipath_cvt_physportstate[]; 839extern const u8 ipath_cvt_physportstate[];
840#define IB_PHYSPORTSTATE_SLEEP 1
841#define IB_PHYSPORTSTATE_POLL 2
842#define IB_PHYSPORTSTATE_DISABLED 3
843#define IB_PHYSPORTSTATE_CFG_TRAIN 4
844#define IB_PHYSPORTSTATE_LINKUP 5
845#define IB_PHYSPORTSTATE_LINK_ERR_RECOVER 6
834 846
835extern const int ib_ipath_state_ops[]; 847extern const int ib_ipath_state_ops[];
836 848
diff --git a/drivers/infiniband/hw/mlx4/cq.c b/drivers/infiniband/hw/mlx4/cq.c
index 9d32c49cc651..7950aa6e8184 100644
--- a/drivers/infiniband/hw/mlx4/cq.c
+++ b/drivers/infiniband/hw/mlx4/cq.c
@@ -313,6 +313,7 @@ static int mlx4_ib_poll_one(struct mlx4_ib_cq *cq,
313 struct mlx4_ib_srq *srq; 313 struct mlx4_ib_srq *srq;
314 int is_send; 314 int is_send;
315 int is_error; 315 int is_error;
316 u32 g_mlpath_rqpn;
316 u16 wqe_ctr; 317 u16 wqe_ctr;
317 318
318 cqe = next_cqe_sw(cq); 319 cqe = next_cqe_sw(cq);
@@ -426,10 +427,10 @@ static int mlx4_ib_poll_one(struct mlx4_ib_cq *cq,
426 427
427 wc->slid = be16_to_cpu(cqe->rlid); 428 wc->slid = be16_to_cpu(cqe->rlid);
428 wc->sl = cqe->sl >> 4; 429 wc->sl = cqe->sl >> 4;
429 wc->src_qp = be32_to_cpu(cqe->g_mlpath_rqpn) & 0xffffff; 430 g_mlpath_rqpn = be32_to_cpu(cqe->g_mlpath_rqpn);
430 wc->dlid_path_bits = (be32_to_cpu(cqe->g_mlpath_rqpn) >> 24) & 0x7f; 431 wc->src_qp = g_mlpath_rqpn & 0xffffff;
431 wc->wc_flags |= be32_to_cpu(cqe->g_mlpath_rqpn) & 0x80000000 ? 432 wc->dlid_path_bits = (g_mlpath_rqpn >> 24) & 0x7f;
432 IB_WC_GRH : 0; 433 wc->wc_flags |= g_mlpath_rqpn & 0x80000000 ? IB_WC_GRH : 0;
433 wc->pkey_index = be32_to_cpu(cqe->immed_rss_invalid) & 0x7f; 434 wc->pkey_index = be32_to_cpu(cqe->immed_rss_invalid) & 0x7f;
434 } 435 }
435 436
diff --git a/drivers/infiniband/hw/mthca/mthca_dev.h b/drivers/infiniband/hw/mthca/mthca_dev.h
index 15aa32eb78b6..7bbdd1f4e6c7 100644
--- a/drivers/infiniband/hw/mthca/mthca_dev.h
+++ b/drivers/infiniband/hw/mthca/mthca_dev.h
@@ -60,13 +60,12 @@
60enum { 60enum {
61 MTHCA_FLAG_DDR_HIDDEN = 1 << 1, 61 MTHCA_FLAG_DDR_HIDDEN = 1 << 1,
62 MTHCA_FLAG_SRQ = 1 << 2, 62 MTHCA_FLAG_SRQ = 1 << 2,
63 MTHCA_FLAG_MSI = 1 << 3, 63 MTHCA_FLAG_MSI_X = 1 << 3,
64 MTHCA_FLAG_MSI_X = 1 << 4, 64 MTHCA_FLAG_NO_LAM = 1 << 4,
65 MTHCA_FLAG_NO_LAM = 1 << 5, 65 MTHCA_FLAG_FMR = 1 << 5,
66 MTHCA_FLAG_FMR = 1 << 6, 66 MTHCA_FLAG_MEMFREE = 1 << 6,
67 MTHCA_FLAG_MEMFREE = 1 << 7, 67 MTHCA_FLAG_PCIE = 1 << 7,
68 MTHCA_FLAG_PCIE = 1 << 8, 68 MTHCA_FLAG_SINAI_OPT = 1 << 8
69 MTHCA_FLAG_SINAI_OPT = 1 << 9
70}; 69};
71 70
72enum { 71enum {
diff --git a/drivers/infiniband/hw/mthca/mthca_eq.c b/drivers/infiniband/hw/mthca/mthca_eq.c
index b29de51b7f35..b60eb5df96e8 100644
--- a/drivers/infiniband/hw/mthca/mthca_eq.c
+++ b/drivers/infiniband/hw/mthca/mthca_eq.c
@@ -827,8 +827,7 @@ int mthca_init_eq_table(struct mthca_dev *dev)
827 if (err) 827 if (err)
828 goto err_out_free; 828 goto err_out_free;
829 829
830 if (dev->mthca_flags & MTHCA_FLAG_MSI || 830 if (dev->mthca_flags & MTHCA_FLAG_MSI_X) {
831 dev->mthca_flags & MTHCA_FLAG_MSI_X) {
832 dev->eq_table.clr_mask = 0; 831 dev->eq_table.clr_mask = 0;
833 } else { 832 } else {
834 dev->eq_table.clr_mask = 833 dev->eq_table.clr_mask =
@@ -839,8 +838,7 @@ int mthca_init_eq_table(struct mthca_dev *dev)
839 838
840 dev->eq_table.arm_mask = 0; 839 dev->eq_table.arm_mask = 0;
841 840
842 intr = (dev->mthca_flags & MTHCA_FLAG_MSI) ? 841 intr = dev->eq_table.inta_pin;
843 128 : dev->eq_table.inta_pin;
844 842
845 err = mthca_create_eq(dev, dev->limits.num_cqs + MTHCA_NUM_SPARE_EQE, 843 err = mthca_create_eq(dev, dev->limits.num_cqs + MTHCA_NUM_SPARE_EQE,
846 (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 128 : intr, 844 (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 128 : intr,
diff --git a/drivers/infiniband/hw/mthca/mthca_main.c b/drivers/infiniband/hw/mthca/mthca_main.c
index 60de6f93869e..5cf8250d4e16 100644
--- a/drivers/infiniband/hw/mthca/mthca_main.c
+++ b/drivers/infiniband/hw/mthca/mthca_main.c
@@ -65,14 +65,9 @@ static int msi_x = 1;
65module_param(msi_x, int, 0444); 65module_param(msi_x, int, 0444);
66MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero"); 66MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
67 67
68static int msi = 0;
69module_param(msi, int, 0444);
70MODULE_PARM_DESC(msi, "attempt to use MSI if nonzero (deprecated, use MSI-X instead)");
71
72#else /* CONFIG_PCI_MSI */ 68#else /* CONFIG_PCI_MSI */
73 69
74#define msi_x (0) 70#define msi_x (0)
75#define msi (0)
76 71
77#endif /* CONFIG_PCI_MSI */ 72#endif /* CONFIG_PCI_MSI */
78 73
@@ -816,13 +811,11 @@ static int mthca_setup_hca(struct mthca_dev *dev)
816 811
817 err = mthca_NOP(dev, &status); 812 err = mthca_NOP(dev, &status);
818 if (err || status) { 813 if (err || status) {
819 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X)) { 814 if (dev->mthca_flags & MTHCA_FLAG_MSI_X) {
820 mthca_warn(dev, "NOP command failed to generate interrupt " 815 mthca_warn(dev, "NOP command failed to generate interrupt "
821 "(IRQ %d).\n", 816 "(IRQ %d).\n",
822 dev->mthca_flags & MTHCA_FLAG_MSI_X ? 817 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector);
823 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector : 818 mthca_warn(dev, "Trying again with MSI-X disabled.\n");
824 dev->pdev->irq);
825 mthca_warn(dev, "Trying again with MSI/MSI-X disabled.\n");
826 } else { 819 } else {
827 mthca_err(dev, "NOP command failed to generate interrupt " 820 mthca_err(dev, "NOP command failed to generate interrupt "
828 "(IRQ %d), aborting.\n", 821 "(IRQ %d), aborting.\n",
@@ -1005,7 +998,7 @@ static struct {
1005 .flags = 0 }, 998 .flags = 0 },
1006 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 8, 200), 999 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 8, 200),
1007 .flags = MTHCA_FLAG_PCIE }, 1000 .flags = MTHCA_FLAG_PCIE },
1008 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 2, 0), 1001 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 3, 0),
1009 .flags = MTHCA_FLAG_MEMFREE | 1002 .flags = MTHCA_FLAG_MEMFREE |
1010 MTHCA_FLAG_PCIE }, 1003 MTHCA_FLAG_PCIE },
1011 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 2, 0), 1004 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 2, 0),
@@ -1128,29 +1121,12 @@ static int __mthca_init_one(struct pci_dev *pdev, int hca_type)
1128 1121
1129 if (msi_x && !mthca_enable_msi_x(mdev)) 1122 if (msi_x && !mthca_enable_msi_x(mdev))
1130 mdev->mthca_flags |= MTHCA_FLAG_MSI_X; 1123 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1131 else if (msi) {
1132 static int warned;
1133
1134 if (!warned) {
1135 printk(KERN_WARNING PFX "WARNING: MSI support will be "
1136 "removed from the ib_mthca driver in January 2008.\n");
1137 printk(KERN_WARNING " If you are using MSI and cannot "
1138 "switch to MSI-X, please tell "
1139 "<general@lists.openfabrics.org>.\n");
1140 ++warned;
1141 }
1142
1143 if (!pci_enable_msi(pdev))
1144 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1145 }
1146 1124
1147 err = mthca_setup_hca(mdev); 1125 err = mthca_setup_hca(mdev);
1148 if (err == -EBUSY && (mdev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))) { 1126 if (err == -EBUSY && (mdev->mthca_flags & MTHCA_FLAG_MSI_X)) {
1149 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X) 1127 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1150 pci_disable_msix(pdev); 1128 pci_disable_msix(pdev);
1151 if (mdev->mthca_flags & MTHCA_FLAG_MSI) 1129 mdev->mthca_flags &= ~MTHCA_FLAG_MSI_X;
1152 pci_disable_msi(pdev);
1153 mdev->mthca_flags &= ~(MTHCA_FLAG_MSI_X | MTHCA_FLAG_MSI);
1154 1130
1155 err = mthca_setup_hca(mdev); 1131 err = mthca_setup_hca(mdev);
1156 } 1132 }
@@ -1192,8 +1168,6 @@ err_cleanup:
1192err_close: 1168err_close:
1193 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X) 1169 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1194 pci_disable_msix(pdev); 1170 pci_disable_msix(pdev);
1195 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1196 pci_disable_msi(pdev);
1197 1171
1198 mthca_close_hca(mdev); 1172 mthca_close_hca(mdev);
1199 1173
@@ -1246,8 +1220,6 @@ static void __mthca_remove_one(struct pci_dev *pdev)
1246 1220
1247 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X) 1221 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1248 pci_disable_msix(pdev); 1222 pci_disable_msix(pdev);
1249 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1250 pci_disable_msi(pdev);
1251 1223
1252 ib_dealloc_device(&mdev->ib_dev); 1224 ib_dealloc_device(&mdev->ib_dev);
1253 mthca_release_regions(pdev, mdev->mthca_flags & 1225 mthca_release_regions(pdev, mdev->mthca_flags &
diff --git a/drivers/infiniband/ulp/ipoib/ipoib.h b/drivers/infiniband/ulp/ipoib/ipoib.h
index eb7edab0e836..fe250c60607d 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib.h
+++ b/drivers/infiniband/ulp/ipoib/ipoib.h
@@ -56,42 +56,43 @@
56/* constants */ 56/* constants */
57 57
58enum { 58enum {
59 IPOIB_PACKET_SIZE = 2048, 59 IPOIB_PACKET_SIZE = 2048,
60 IPOIB_BUF_SIZE = IPOIB_PACKET_SIZE + IB_GRH_BYTES, 60 IPOIB_BUF_SIZE = IPOIB_PACKET_SIZE + IB_GRH_BYTES,
61 61
62 IPOIB_ENCAP_LEN = 4, 62 IPOIB_ENCAP_LEN = 4,
63 63
64 IPOIB_CM_MTU = 0x10000 - 0x10, /* padding to align header to 16 */ 64 IPOIB_CM_MTU = 0x10000 - 0x10, /* padding to align header to 16 */
65 IPOIB_CM_BUF_SIZE = IPOIB_CM_MTU + IPOIB_ENCAP_LEN, 65 IPOIB_CM_BUF_SIZE = IPOIB_CM_MTU + IPOIB_ENCAP_LEN,
66 IPOIB_CM_HEAD_SIZE = IPOIB_CM_BUF_SIZE % PAGE_SIZE, 66 IPOIB_CM_HEAD_SIZE = IPOIB_CM_BUF_SIZE % PAGE_SIZE,
67 IPOIB_CM_RX_SG = ALIGN(IPOIB_CM_BUF_SIZE, PAGE_SIZE) / PAGE_SIZE, 67 IPOIB_CM_RX_SG = ALIGN(IPOIB_CM_BUF_SIZE, PAGE_SIZE) / PAGE_SIZE,
68 IPOIB_RX_RING_SIZE = 128, 68 IPOIB_RX_RING_SIZE = 128,
69 IPOIB_TX_RING_SIZE = 64, 69 IPOIB_TX_RING_SIZE = 64,
70 IPOIB_MAX_QUEUE_SIZE = 8192, 70 IPOIB_MAX_QUEUE_SIZE = 8192,
71 IPOIB_MIN_QUEUE_SIZE = 2, 71 IPOIB_MIN_QUEUE_SIZE = 2,
72 IPOIB_CM_MAX_CONN_QP = 4096,
72 73
73 IPOIB_NUM_WC = 4, 74 IPOIB_NUM_WC = 4,
74 75
75 IPOIB_MAX_PATH_REC_QUEUE = 3, 76 IPOIB_MAX_PATH_REC_QUEUE = 3,
76 IPOIB_MAX_MCAST_QUEUE = 3, 77 IPOIB_MAX_MCAST_QUEUE = 3,
77 78
78 IPOIB_FLAG_OPER_UP = 0, 79 IPOIB_FLAG_OPER_UP = 0,
79 IPOIB_FLAG_INITIALIZED = 1, 80 IPOIB_FLAG_INITIALIZED = 1,
80 IPOIB_FLAG_ADMIN_UP = 2, 81 IPOIB_FLAG_ADMIN_UP = 2,
81 IPOIB_PKEY_ASSIGNED = 3, 82 IPOIB_PKEY_ASSIGNED = 3,
82 IPOIB_PKEY_STOP = 4, 83 IPOIB_PKEY_STOP = 4,
83 IPOIB_FLAG_SUBINTERFACE = 5, 84 IPOIB_FLAG_SUBINTERFACE = 5,
84 IPOIB_MCAST_RUN = 6, 85 IPOIB_MCAST_RUN = 6,
85 IPOIB_STOP_REAPER = 7, 86 IPOIB_STOP_REAPER = 7,
86 IPOIB_MCAST_STARTED = 8, 87 IPOIB_MCAST_STARTED = 8,
87 IPOIB_FLAG_ADMIN_CM = 9, 88 IPOIB_FLAG_ADMIN_CM = 9,
88 IPOIB_FLAG_UMCAST = 10, 89 IPOIB_FLAG_UMCAST = 10,
89 90
90 IPOIB_MAX_BACKOFF_SECONDS = 16, 91 IPOIB_MAX_BACKOFF_SECONDS = 16,
91 92
92 IPOIB_MCAST_FLAG_FOUND = 0, /* used in set_multicast_list */ 93 IPOIB_MCAST_FLAG_FOUND = 0, /* used in set_multicast_list */
93 IPOIB_MCAST_FLAG_SENDONLY = 1, 94 IPOIB_MCAST_FLAG_SENDONLY = 1,
94 IPOIB_MCAST_FLAG_BUSY = 2, /* joining or already joined */ 95 IPOIB_MCAST_FLAG_BUSY = 2, /* joining or already joined */
95 IPOIB_MCAST_FLAG_ATTACHED = 3, 96 IPOIB_MCAST_FLAG_ATTACHED = 3,
96}; 97};
97 98
@@ -117,7 +118,7 @@ struct ipoib_pseudoheader {
117struct ipoib_mcast { 118struct ipoib_mcast {
118 struct ib_sa_mcmember_rec mcmember; 119 struct ib_sa_mcmember_rec mcmember;
119 struct ib_sa_multicast *mc; 120 struct ib_sa_multicast *mc;
120 struct ipoib_ah *ah; 121 struct ipoib_ah *ah;
121 122
122 struct rb_node rb_node; 123 struct rb_node rb_node;
123 struct list_head list; 124 struct list_head list;
@@ -186,27 +187,29 @@ enum ipoib_cm_state {
186}; 187};
187 188
188struct ipoib_cm_rx { 189struct ipoib_cm_rx {
189 struct ib_cm_id *id; 190 struct ib_cm_id *id;
190 struct ib_qp *qp; 191 struct ib_qp *qp;
191 struct list_head list; 192 struct ipoib_cm_rx_buf *rx_ring;
192 struct net_device *dev; 193 struct list_head list;
193 unsigned long jiffies; 194 struct net_device *dev;
194 enum ipoib_cm_state state; 195 unsigned long jiffies;
196 enum ipoib_cm_state state;
197 int recv_count;
195}; 198};
196 199
197struct ipoib_cm_tx { 200struct ipoib_cm_tx {
198 struct ib_cm_id *id; 201 struct ib_cm_id *id;
199 struct ib_qp *qp; 202 struct ib_qp *qp;
200 struct list_head list; 203 struct list_head list;
201 struct net_device *dev; 204 struct net_device *dev;
202 struct ipoib_neigh *neigh; 205 struct ipoib_neigh *neigh;
203 struct ipoib_path *path; 206 struct ipoib_path *path;
204 struct ipoib_tx_buf *tx_ring; 207 struct ipoib_tx_buf *tx_ring;
205 unsigned tx_head; 208 unsigned tx_head;
206 unsigned tx_tail; 209 unsigned tx_tail;
207 unsigned long flags; 210 unsigned long flags;
208 u32 mtu; 211 u32 mtu;
209 struct ib_wc ibwc[IPOIB_NUM_WC]; 212 struct ib_wc ibwc[IPOIB_NUM_WC];
210}; 213};
211 214
212struct ipoib_cm_rx_buf { 215struct ipoib_cm_rx_buf {
@@ -215,25 +218,28 @@ struct ipoib_cm_rx_buf {
215}; 218};
216 219
217struct ipoib_cm_dev_priv { 220struct ipoib_cm_dev_priv {
218 struct ib_srq *srq; 221 struct ib_srq *srq;
219 struct ipoib_cm_rx_buf *srq_ring; 222 struct ipoib_cm_rx_buf *srq_ring;
220 struct ib_cm_id *id; 223 struct ib_cm_id *id;
221 struct list_head passive_ids; /* state: LIVE */ 224 struct list_head passive_ids; /* state: LIVE */
222 struct list_head rx_error_list; /* state: ERROR */ 225 struct list_head rx_error_list; /* state: ERROR */
223 struct list_head rx_flush_list; /* state: FLUSH, drain not started */ 226 struct list_head rx_flush_list; /* state: FLUSH, drain not started */
224 struct list_head rx_drain_list; /* state: FLUSH, drain started */ 227 struct list_head rx_drain_list; /* state: FLUSH, drain started */
225 struct list_head rx_reap_list; /* state: FLUSH, drain done */ 228 struct list_head rx_reap_list; /* state: FLUSH, drain done */
226 struct work_struct start_task; 229 struct work_struct start_task;
227 struct work_struct reap_task; 230 struct work_struct reap_task;
228 struct work_struct skb_task; 231 struct work_struct skb_task;
229 struct work_struct rx_reap_task; 232 struct work_struct rx_reap_task;
230 struct delayed_work stale_task; 233 struct delayed_work stale_task;
231 struct sk_buff_head skb_queue; 234 struct sk_buff_head skb_queue;
232 struct list_head start_list; 235 struct list_head start_list;
233 struct list_head reap_list; 236 struct list_head reap_list;
234 struct ib_wc ibwc[IPOIB_NUM_WC]; 237 struct ib_wc ibwc[IPOIB_NUM_WC];
235 struct ib_sge rx_sge[IPOIB_CM_RX_SG]; 238 struct ib_sge rx_sge[IPOIB_CM_RX_SG];
236 struct ib_recv_wr rx_wr; 239 struct ib_recv_wr rx_wr;
240 int nonsrq_conn_qp;
241 int max_cm_mtu;
242 int num_frags;
237}; 243};
238 244
239/* 245/*
@@ -269,30 +275,30 @@ struct ipoib_dev_priv {
269 struct work_struct pkey_event_task; 275 struct work_struct pkey_event_task;
270 276
271 struct ib_device *ca; 277 struct ib_device *ca;
272 u8 port; 278 u8 port;
273 u16 pkey; 279 u16 pkey;
274 u16 pkey_index; 280 u16 pkey_index;
275 struct ib_pd *pd; 281 struct ib_pd *pd;
276 struct ib_mr *mr; 282 struct ib_mr *mr;
277 struct ib_cq *cq; 283 struct ib_cq *cq;
278 struct ib_qp *qp; 284 struct ib_qp *qp;
279 u32 qkey; 285 u32 qkey;
280 286
281 union ib_gid local_gid; 287 union ib_gid local_gid;
282 u16 local_lid; 288 u16 local_lid;
283 289
284 unsigned int admin_mtu; 290 unsigned int admin_mtu;
285 unsigned int mcast_mtu; 291 unsigned int mcast_mtu;
286 292
287 struct ipoib_rx_buf *rx_ring; 293 struct ipoib_rx_buf *rx_ring;
288 294
289 spinlock_t tx_lock; 295 spinlock_t tx_lock;
290 struct ipoib_tx_buf *tx_ring; 296 struct ipoib_tx_buf *tx_ring;
291 unsigned tx_head; 297 unsigned tx_head;
292 unsigned tx_tail; 298 unsigned tx_tail;
293 struct ib_sge tx_sge; 299 struct ib_sge tx_sge;
294 struct ib_send_wr tx_wr; 300 struct ib_send_wr tx_wr;
295 unsigned tx_outstanding; 301 unsigned tx_outstanding;
296 302
297 struct ib_wc ibwc[IPOIB_NUM_WC]; 303 struct ib_wc ibwc[IPOIB_NUM_WC];
298 304
@@ -317,10 +323,10 @@ struct ipoib_dev_priv {
317 323
318struct ipoib_ah { 324struct ipoib_ah {
319 struct net_device *dev; 325 struct net_device *dev;
320 struct ib_ah *ah; 326 struct ib_ah *ah;
321 struct list_head list; 327 struct list_head list;
322 struct kref ref; 328 struct kref ref;
323 unsigned last_send; 329 unsigned last_send;
324}; 330};
325 331
326struct ipoib_path { 332struct ipoib_path {
@@ -331,11 +337,11 @@ struct ipoib_path {
331 337
332 struct list_head neigh_list; 338 struct list_head neigh_list;
333 339
334 int query_id; 340 int query_id;
335 struct ib_sa_query *query; 341 struct ib_sa_query *query;
336 struct completion done; 342 struct completion done;
337 343
338 struct rb_node rb_node; 344 struct rb_node rb_node;
339 struct list_head list; 345 struct list_head list;
340}; 346};
341 347
@@ -344,7 +350,7 @@ struct ipoib_neigh {
344#ifdef CONFIG_INFINIBAND_IPOIB_CM 350#ifdef CONFIG_INFINIBAND_IPOIB_CM
345 struct ipoib_cm_tx *cm; 351 struct ipoib_cm_tx *cm;
346#endif 352#endif
347 union ib_gid dgid; 353 union ib_gid dgid;
348 struct sk_buff_head queue; 354 struct sk_buff_head queue;
349 355
350 struct neighbour *neighbour; 356 struct neighbour *neighbour;
@@ -455,12 +461,14 @@ void ipoib_drain_cq(struct net_device *dev);
455 461
456#ifdef CONFIG_INFINIBAND_IPOIB_CM 462#ifdef CONFIG_INFINIBAND_IPOIB_CM
457 463
458#define IPOIB_FLAGS_RC 0x80 464#define IPOIB_FLAGS_RC 0x80
459#define IPOIB_FLAGS_UC 0x40 465#define IPOIB_FLAGS_UC 0x40
460 466
461/* We don't support UC connections at the moment */ 467/* We don't support UC connections at the moment */
462#define IPOIB_CM_SUPPORTED(ha) (ha[0] & (IPOIB_FLAGS_RC)) 468#define IPOIB_CM_SUPPORTED(ha) (ha[0] & (IPOIB_FLAGS_RC))
463 469
470extern int ipoib_max_conn_qp;
471
464static inline int ipoib_cm_admin_enabled(struct net_device *dev) 472static inline int ipoib_cm_admin_enabled(struct net_device *dev)
465{ 473{
466 struct ipoib_dev_priv *priv = netdev_priv(dev); 474 struct ipoib_dev_priv *priv = netdev_priv(dev);
@@ -491,6 +499,18 @@ static inline void ipoib_cm_set(struct ipoib_neigh *neigh, struct ipoib_cm_tx *t
491 neigh->cm = tx; 499 neigh->cm = tx;
492} 500}
493 501
502static inline int ipoib_cm_has_srq(struct net_device *dev)
503{
504 struct ipoib_dev_priv *priv = netdev_priv(dev);
505 return !!priv->cm.srq;
506}
507
508static inline unsigned int ipoib_cm_max_mtu(struct net_device *dev)
509{
510 struct ipoib_dev_priv *priv = netdev_priv(dev);
511 return priv->cm.max_cm_mtu;
512}
513
494void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx); 514void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx);
495int ipoib_cm_dev_open(struct net_device *dev); 515int ipoib_cm_dev_open(struct net_device *dev);
496void ipoib_cm_dev_stop(struct net_device *dev); 516void ipoib_cm_dev_stop(struct net_device *dev);
@@ -500,7 +520,7 @@ void ipoib_cm_dev_cleanup(struct net_device *dev);
500struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path, 520struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
501 struct ipoib_neigh *neigh); 521 struct ipoib_neigh *neigh);
502void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx); 522void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx);
503void ipoib_cm_skb_too_long(struct net_device* dev, struct sk_buff *skb, 523void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
504 unsigned int mtu); 524 unsigned int mtu);
505void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc); 525void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc);
506void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc); 526void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc);
@@ -508,6 +528,8 @@ void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc);
508 528
509struct ipoib_cm_tx; 529struct ipoib_cm_tx;
510 530
531#define ipoib_max_conn_qp 0
532
511static inline int ipoib_cm_admin_enabled(struct net_device *dev) 533static inline int ipoib_cm_admin_enabled(struct net_device *dev)
512{ 534{
513 return 0; 535 return 0;
@@ -533,6 +555,16 @@ static inline void ipoib_cm_set(struct ipoib_neigh *neigh, struct ipoib_cm_tx *t
533{ 555{
534} 556}
535 557
558static inline int ipoib_cm_has_srq(struct net_device *dev)
559{
560 return 0;
561}
562
563static inline unsigned int ipoib_cm_max_mtu(struct net_device *dev)
564{
565 return 0;
566}
567
536static inline 568static inline
537void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx) 569void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
538{ 570{
@@ -582,7 +614,7 @@ int ipoib_cm_add_mode_attr(struct net_device *dev)
582 return 0; 614 return 0;
583} 615}
584 616
585static inline void ipoib_cm_skb_too_long(struct net_device* dev, struct sk_buff *skb, 617static inline void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
586 unsigned int mtu) 618 unsigned int mtu)
587{ 619{
588 dev_kfree_skb_any(skb); 620 dev_kfree_skb_any(skb);
@@ -624,12 +656,12 @@ extern struct ib_sa_client ipoib_sa_client;
624extern int ipoib_debug_level; 656extern int ipoib_debug_level;
625 657
626#define ipoib_dbg(priv, format, arg...) \ 658#define ipoib_dbg(priv, format, arg...) \
627 do { \ 659 do { \
628 if (ipoib_debug_level > 0) \ 660 if (ipoib_debug_level > 0) \
629 ipoib_printk(KERN_DEBUG, priv, format , ## arg); \ 661 ipoib_printk(KERN_DEBUG, priv, format , ## arg); \
630 } while (0) 662 } while (0)
631#define ipoib_dbg_mcast(priv, format, arg...) \ 663#define ipoib_dbg_mcast(priv, format, arg...) \
632 do { \ 664 do { \
633 if (mcast_debug_level > 0) \ 665 if (mcast_debug_level > 0) \
634 ipoib_printk(KERN_DEBUG, priv, format , ## arg); \ 666 ipoib_printk(KERN_DEBUG, priv, format , ## arg); \
635 } while (0) 667 } while (0)
@@ -642,7 +674,7 @@ extern int ipoib_debug_level;
642 674
643#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA 675#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
644#define ipoib_dbg_data(priv, format, arg...) \ 676#define ipoib_dbg_data(priv, format, arg...) \
645 do { \ 677 do { \
646 if (data_debug_level > 0) \ 678 if (data_debug_level > 0) \
647 ipoib_printk(KERN_DEBUG, priv, format , ## arg); \ 679 ipoib_printk(KERN_DEBUG, priv, format , ## arg); \
648 } while (0) 680 } while (0)
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_cm.c b/drivers/infiniband/ulp/ipoib/ipoib_cm.c
index 059cf92b60a5..1818f958c250 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_cm.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_cm.c
@@ -39,6 +39,15 @@
39#include <linux/icmpv6.h> 39#include <linux/icmpv6.h>
40#include <linux/delay.h> 40#include <linux/delay.h>
41 41
42#include "ipoib.h"
43
44int ipoib_max_conn_qp = 128;
45
46module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
47MODULE_PARM_DESC(max_nonsrq_conn_qp,
48 "Max number of connected-mode QPs per interface "
49 "(applied only if shared receive queue is not available)");
50
42#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA 51#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
43static int data_debug_level; 52static int data_debug_level;
44 53
@@ -47,8 +56,6 @@ MODULE_PARM_DESC(cm_data_debug_level,
47 "Enable data path debug tracing for connected mode if > 0"); 56 "Enable data path debug tracing for connected mode if > 0");
48#endif 57#endif
49 58
50#include "ipoib.h"
51
52#define IPOIB_CM_IETF_ID 0x1000000000000000ULL 59#define IPOIB_CM_IETF_ID 0x1000000000000000ULL
53 60
54#define IPOIB_CM_RX_UPDATE_TIME (256 * HZ) 61#define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
@@ -81,7 +88,7 @@ static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
81 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE); 88 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
82} 89}
83 90
84static int ipoib_cm_post_receive(struct net_device *dev, int id) 91static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
85{ 92{
86 struct ipoib_dev_priv *priv = netdev_priv(dev); 93 struct ipoib_dev_priv *priv = netdev_priv(dev);
87 struct ib_recv_wr *bad_wr; 94 struct ib_recv_wr *bad_wr;
@@ -89,13 +96,13 @@ static int ipoib_cm_post_receive(struct net_device *dev, int id)
89 96
90 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV; 97 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
91 98
92 for (i = 0; i < IPOIB_CM_RX_SG; ++i) 99 for (i = 0; i < priv->cm.num_frags; ++i)
93 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i]; 100 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
94 101
95 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr); 102 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
96 if (unlikely(ret)) { 103 if (unlikely(ret)) {
97 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret); 104 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
98 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1, 105 ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
99 priv->cm.srq_ring[id].mapping); 106 priv->cm.srq_ring[id].mapping);
100 dev_kfree_skb_any(priv->cm.srq_ring[id].skb); 107 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
101 priv->cm.srq_ring[id].skb = NULL; 108 priv->cm.srq_ring[id].skb = NULL;
@@ -104,7 +111,33 @@ static int ipoib_cm_post_receive(struct net_device *dev, int id)
104 return ret; 111 return ret;
105} 112}
106 113
107static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev, int id, int frags, 114static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
115 struct ipoib_cm_rx *rx, int id)
116{
117 struct ipoib_dev_priv *priv = netdev_priv(dev);
118 struct ib_recv_wr *bad_wr;
119 int i, ret;
120
121 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
122
123 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
124 priv->cm.rx_sge[i].addr = rx->rx_ring[id].mapping[i];
125
126 ret = ib_post_recv(rx->qp, &priv->cm.rx_wr, &bad_wr);
127 if (unlikely(ret)) {
128 ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
129 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
130 rx->rx_ring[id].mapping);
131 dev_kfree_skb_any(rx->rx_ring[id].skb);
132 rx->rx_ring[id].skb = NULL;
133 }
134
135 return ret;
136}
137
138static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
139 struct ipoib_cm_rx_buf *rx_ring,
140 int id, int frags,
108 u64 mapping[IPOIB_CM_RX_SG]) 141 u64 mapping[IPOIB_CM_RX_SG])
109{ 142{
110 struct ipoib_dev_priv *priv = netdev_priv(dev); 143 struct ipoib_dev_priv *priv = netdev_priv(dev);
@@ -141,7 +174,7 @@ static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev, int id, int
141 goto partial_error; 174 goto partial_error;
142 } 175 }
143 176
144 priv->cm.srq_ring[id].skb = skb; 177 rx_ring[id].skb = skb;
145 return skb; 178 return skb;
146 179
147partial_error: 180partial_error:
@@ -155,7 +188,23 @@ partial_error:
155 return NULL; 188 return NULL;
156} 189}
157 190
158static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv* priv) 191static void ipoib_cm_free_rx_ring(struct net_device *dev,
192 struct ipoib_cm_rx_buf *rx_ring)
193{
194 struct ipoib_dev_priv *priv = netdev_priv(dev);
195 int i;
196
197 for (i = 0; i < ipoib_recvq_size; ++i)
198 if (rx_ring[i].skb) {
199 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
200 rx_ring[i].mapping);
201 dev_kfree_skb_any(rx_ring[i].skb);
202 }
203
204 kfree(rx_ring);
205}
206
207static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
159{ 208{
160 struct ib_send_wr *bad_wr; 209 struct ib_send_wr *bad_wr;
161 struct ipoib_cm_rx *p; 210 struct ipoib_cm_rx *p;
@@ -208,12 +257,18 @@ static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
208 .qp_type = IB_QPT_RC, 257 .qp_type = IB_QPT_RC,
209 .qp_context = p, 258 .qp_context = p,
210 }; 259 };
260
261 if (!ipoib_cm_has_srq(dev)) {
262 attr.cap.max_recv_wr = ipoib_recvq_size;
263 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
264 }
265
211 return ib_create_qp(priv->pd, &attr); 266 return ib_create_qp(priv->pd, &attr);
212} 267}
213 268
214static int ipoib_cm_modify_rx_qp(struct net_device *dev, 269static int ipoib_cm_modify_rx_qp(struct net_device *dev,
215 struct ib_cm_id *cm_id, struct ib_qp *qp, 270 struct ib_cm_id *cm_id, struct ib_qp *qp,
216 unsigned psn) 271 unsigned psn)
217{ 272{
218 struct ipoib_dev_priv *priv = netdev_priv(dev); 273 struct ipoib_dev_priv *priv = netdev_priv(dev);
219 struct ib_qp_attr qp_attr; 274 struct ib_qp_attr qp_attr;
@@ -266,6 +321,60 @@ static int ipoib_cm_modify_rx_qp(struct net_device *dev,
266 return 0; 321 return 0;
267} 322}
268 323
324static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
325 struct ipoib_cm_rx *rx)
326{
327 struct ipoib_dev_priv *priv = netdev_priv(dev);
328 int ret;
329 int i;
330
331 rx->rx_ring = kcalloc(ipoib_recvq_size, sizeof *rx->rx_ring, GFP_KERNEL);
332 if (!rx->rx_ring)
333 return -ENOMEM;
334
335 spin_lock_irq(&priv->lock);
336
337 if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
338 spin_unlock_irq(&priv->lock);
339 ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
340 ret = -EINVAL;
341 goto err_free;
342 } else
343 ++priv->cm.nonsrq_conn_qp;
344
345 spin_unlock_irq(&priv->lock);
346
347 for (i = 0; i < ipoib_recvq_size; ++i) {
348 if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
349 rx->rx_ring[i].mapping)) {
350 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
351 ret = -ENOMEM;
352 goto err_count;
353 }
354 ret = ipoib_cm_post_receive_nonsrq(dev, rx, i);
355 if (ret) {
356 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
357 "failed for buf %d\n", i);
358 ret = -EIO;
359 goto err_count;
360 }
361 }
362
363 rx->recv_count = ipoib_recvq_size;
364
365 return 0;
366
367err_count:
368 spin_lock_irq(&priv->lock);
369 --priv->cm.nonsrq_conn_qp;
370 spin_unlock_irq(&priv->lock);
371
372err_free:
373 ipoib_cm_free_rx_ring(dev, rx->rx_ring);
374
375 return ret;
376}
377
269static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id, 378static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
270 struct ib_qp *qp, struct ib_cm_req_event_param *req, 379 struct ib_qp *qp, struct ib_cm_req_event_param *req,
271 unsigned psn) 380 unsigned psn)
@@ -281,7 +390,7 @@ static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
281 rep.private_data_len = sizeof data; 390 rep.private_data_len = sizeof data;
282 rep.flow_control = 0; 391 rep.flow_control = 0;
283 rep.rnr_retry_count = req->rnr_retry_count; 392 rep.rnr_retry_count = req->rnr_retry_count;
284 rep.srq = 1; 393 rep.srq = ipoib_cm_has_srq(dev);
285 rep.qp_num = qp->qp_num; 394 rep.qp_num = qp->qp_num;
286 rep.starting_psn = psn; 395 rep.starting_psn = psn;
287 return ib_send_cm_rep(cm_id, &rep); 396 return ib_send_cm_rep(cm_id, &rep);
@@ -317,6 +426,12 @@ static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *even
317 if (ret) 426 if (ret)
318 goto err_modify; 427 goto err_modify;
319 428
429 if (!ipoib_cm_has_srq(dev)) {
430 ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
431 if (ret)
432 goto err_modify;
433 }
434
320 spin_lock_irq(&priv->lock); 435 spin_lock_irq(&priv->lock);
321 queue_delayed_work(ipoib_workqueue, 436 queue_delayed_work(ipoib_workqueue,
322 &priv->cm.stale_task, IPOIB_CM_RX_DELAY); 437 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
@@ -401,12 +516,14 @@ static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
401void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc) 516void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
402{ 517{
403 struct ipoib_dev_priv *priv = netdev_priv(dev); 518 struct ipoib_dev_priv *priv = netdev_priv(dev);
519 struct ipoib_cm_rx_buf *rx_ring;
404 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV); 520 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
405 struct sk_buff *skb, *newskb; 521 struct sk_buff *skb, *newskb;
406 struct ipoib_cm_rx *p; 522 struct ipoib_cm_rx *p;
407 unsigned long flags; 523 unsigned long flags;
408 u64 mapping[IPOIB_CM_RX_SG]; 524 u64 mapping[IPOIB_CM_RX_SG];
409 int frags; 525 int frags;
526 int has_srq;
410 527
411 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n", 528 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
412 wr_id, wc->status); 529 wr_id, wc->status);
@@ -424,18 +541,32 @@ void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
424 return; 541 return;
425 } 542 }
426 543
427 skb = priv->cm.srq_ring[wr_id].skb; 544 p = wc->qp->qp_context;
545
546 has_srq = ipoib_cm_has_srq(dev);
547 rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
548
549 skb = rx_ring[wr_id].skb;
428 550
429 if (unlikely(wc->status != IB_WC_SUCCESS)) { 551 if (unlikely(wc->status != IB_WC_SUCCESS)) {
430 ipoib_dbg(priv, "cm recv error " 552 ipoib_dbg(priv, "cm recv error "
431 "(status=%d, wrid=%d vend_err %x)\n", 553 "(status=%d, wrid=%d vend_err %x)\n",
432 wc->status, wr_id, wc->vendor_err); 554 wc->status, wr_id, wc->vendor_err);
433 ++dev->stats.rx_dropped; 555 ++dev->stats.rx_dropped;
434 goto repost; 556 if (has_srq)
557 goto repost;
558 else {
559 if (!--p->recv_count) {
560 spin_lock_irqsave(&priv->lock, flags);
561 list_move(&p->list, &priv->cm.rx_reap_list);
562 spin_unlock_irqrestore(&priv->lock, flags);
563 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
564 }
565 return;
566 }
435 } 567 }
436 568
437 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) { 569 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
438 p = wc->qp->qp_context;
439 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) { 570 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
440 spin_lock_irqsave(&priv->lock, flags); 571 spin_lock_irqsave(&priv->lock, flags);
441 p->jiffies = jiffies; 572 p->jiffies = jiffies;
@@ -450,7 +581,7 @@ void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
450 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len, 581 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
451 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE; 582 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
452 583
453 newskb = ipoib_cm_alloc_rx_skb(dev, wr_id, frags, mapping); 584 newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags, mapping);
454 if (unlikely(!newskb)) { 585 if (unlikely(!newskb)) {
455 /* 586 /*
456 * If we can't allocate a new RX buffer, dump 587 * If we can't allocate a new RX buffer, dump
@@ -461,8 +592,8 @@ void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
461 goto repost; 592 goto repost;
462 } 593 }
463 594
464 ipoib_cm_dma_unmap_rx(priv, frags, priv->cm.srq_ring[wr_id].mapping); 595 ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
465 memcpy(priv->cm.srq_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping); 596 memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
466 597
467 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n", 598 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
468 wc->byte_len, wc->slid); 599 wc->byte_len, wc->slid);
@@ -483,9 +614,17 @@ void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
483 netif_receive_skb(skb); 614 netif_receive_skb(skb);
484 615
485repost: 616repost:
486 if (unlikely(ipoib_cm_post_receive(dev, wr_id))) 617 if (has_srq) {
487 ipoib_warn(priv, "ipoib_cm_post_receive failed " 618 if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
488 "for buf %d\n", wr_id); 619 ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
620 "for buf %d\n", wr_id);
621 } else {
622 if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p, wr_id))) {
623 --p->recv_count;
624 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
625 "for buf %d\n", wr_id);
626 }
627 }
489} 628}
490 629
491static inline int post_send(struct ipoib_dev_priv *priv, 630static inline int post_send(struct ipoib_dev_priv *priv,
@@ -495,10 +634,10 @@ static inline int post_send(struct ipoib_dev_priv *priv,
495{ 634{
496 struct ib_send_wr *bad_wr; 635 struct ib_send_wr *bad_wr;
497 636
498 priv->tx_sge.addr = addr; 637 priv->tx_sge.addr = addr;
499 priv->tx_sge.length = len; 638 priv->tx_sge.length = len;
500 639
501 priv->tx_wr.wr_id = wr_id | IPOIB_OP_CM; 640 priv->tx_wr.wr_id = wr_id | IPOIB_OP_CM;
502 641
503 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr); 642 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
504} 643}
@@ -540,7 +679,7 @@ void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_
540 tx_req->mapping = addr; 679 tx_req->mapping = addr;
541 680
542 if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1), 681 if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
543 addr, skb->len))) { 682 addr, skb->len))) {
544 ipoib_warn(priv, "post_send failed\n"); 683 ipoib_warn(priv, "post_send failed\n");
545 ++dev->stats.tx_errors; 684 ++dev->stats.tx_errors;
546 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE); 685 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
@@ -657,10 +796,33 @@ err_cm:
657 return ret; 796 return ret;
658} 797}
659 798
799static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
800{
801 struct ipoib_dev_priv *priv = netdev_priv(dev);
802 struct ipoib_cm_rx *rx, *n;
803 LIST_HEAD(list);
804
805 spin_lock_irq(&priv->lock);
806 list_splice_init(&priv->cm.rx_reap_list, &list);
807 spin_unlock_irq(&priv->lock);
808
809 list_for_each_entry_safe(rx, n, &list, list) {
810 ib_destroy_cm_id(rx->id);
811 ib_destroy_qp(rx->qp);
812 if (!ipoib_cm_has_srq(dev)) {
813 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
814 spin_lock_irq(&priv->lock);
815 --priv->cm.nonsrq_conn_qp;
816 spin_unlock_irq(&priv->lock);
817 }
818 kfree(rx);
819 }
820}
821
660void ipoib_cm_dev_stop(struct net_device *dev) 822void ipoib_cm_dev_stop(struct net_device *dev)
661{ 823{
662 struct ipoib_dev_priv *priv = netdev_priv(dev); 824 struct ipoib_dev_priv *priv = netdev_priv(dev);
663 struct ipoib_cm_rx *p, *n; 825 struct ipoib_cm_rx *p;
664 unsigned long begin; 826 unsigned long begin;
665 LIST_HEAD(list); 827 LIST_HEAD(list);
666 int ret; 828 int ret;
@@ -706,15 +868,9 @@ void ipoib_cm_dev_stop(struct net_device *dev)
706 spin_lock_irq(&priv->lock); 868 spin_lock_irq(&priv->lock);
707 } 869 }
708 870
709 list_splice_init(&priv->cm.rx_reap_list, &list);
710
711 spin_unlock_irq(&priv->lock); 871 spin_unlock_irq(&priv->lock);
712 872
713 list_for_each_entry_safe(p, n, &list, list) { 873 ipoib_cm_free_rx_reap_list(dev);
714 ib_destroy_cm_id(p->id);
715 ib_destroy_qp(p->qp);
716 kfree(p);
717 }
718 874
719 cancel_delayed_work(&priv->cm.stale_task); 875 cancel_delayed_work(&priv->cm.stale_task);
720} 876}
@@ -799,7 +955,7 @@ static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ipoib_
799 .sq_sig_type = IB_SIGNAL_ALL_WR, 955 .sq_sig_type = IB_SIGNAL_ALL_WR,
800 .qp_type = IB_QPT_RC, 956 .qp_type = IB_QPT_RC,
801 .qp_context = tx 957 .qp_context = tx
802 }; 958 };
803 959
804 return ib_create_qp(priv->pd, &attr); 960 return ib_create_qp(priv->pd, &attr);
805} 961}
@@ -816,28 +972,28 @@ static int ipoib_cm_send_req(struct net_device *dev,
816 data.qpn = cpu_to_be32(priv->qp->qp_num); 972 data.qpn = cpu_to_be32(priv->qp->qp_num);
817 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE); 973 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
818 974
819 req.primary_path = pathrec; 975 req.primary_path = pathrec;
820 req.alternate_path = NULL; 976 req.alternate_path = NULL;
821 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn); 977 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
822 req.qp_num = qp->qp_num; 978 req.qp_num = qp->qp_num;
823 req.qp_type = qp->qp_type; 979 req.qp_type = qp->qp_type;
824 req.private_data = &data; 980 req.private_data = &data;
825 req.private_data_len = sizeof data; 981 req.private_data_len = sizeof data;
826 req.flow_control = 0; 982 req.flow_control = 0;
827 983
828 req.starting_psn = 0; /* FIXME */ 984 req.starting_psn = 0; /* FIXME */
829 985
830 /* 986 /*
831 * Pick some arbitrary defaults here; we could make these 987 * Pick some arbitrary defaults here; we could make these
832 * module parameters if anyone cared about setting them. 988 * module parameters if anyone cared about setting them.
833 */ 989 */
834 req.responder_resources = 4; 990 req.responder_resources = 4;
835 req.remote_cm_response_timeout = 20; 991 req.remote_cm_response_timeout = 20;
836 req.local_cm_response_timeout = 20; 992 req.local_cm_response_timeout = 20;
837 req.retry_count = 0; /* RFC draft warns against retries */ 993 req.retry_count = 0; /* RFC draft warns against retries */
838 req.rnr_retry_count = 0; /* RFC draft warns against retries */ 994 req.rnr_retry_count = 0; /* RFC draft warns against retries */
839 req.max_cm_retries = 15; 995 req.max_cm_retries = 15;
840 req.srq = 1; 996 req.srq = ipoib_cm_has_srq(dev);
841 return ib_send_cm_req(id, &req); 997 return ib_send_cm_req(id, &req);
842} 998}
843 999
@@ -1150,7 +1306,7 @@ static void ipoib_cm_skb_reap(struct work_struct *work)
1150 spin_unlock_irq(&priv->tx_lock); 1306 spin_unlock_irq(&priv->tx_lock);
1151} 1307}
1152 1308
1153void ipoib_cm_skb_too_long(struct net_device* dev, struct sk_buff *skb, 1309void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
1154 unsigned int mtu) 1310 unsigned int mtu)
1155{ 1311{
1156 struct ipoib_dev_priv *priv = netdev_priv(dev); 1312 struct ipoib_dev_priv *priv = netdev_priv(dev);
@@ -1166,20 +1322,8 @@ void ipoib_cm_skb_too_long(struct net_device* dev, struct sk_buff *skb,
1166 1322
1167static void ipoib_cm_rx_reap(struct work_struct *work) 1323static void ipoib_cm_rx_reap(struct work_struct *work)
1168{ 1324{
1169 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv, 1325 ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1170 cm.rx_reap_task); 1326 cm.rx_reap_task)->dev);
1171 struct ipoib_cm_rx *p, *n;
1172 LIST_HEAD(list);
1173
1174 spin_lock_irq(&priv->lock);
1175 list_splice_init(&priv->cm.rx_reap_list, &list);
1176 spin_unlock_irq(&priv->lock);
1177
1178 list_for_each_entry_safe(p, n, &list, list) {
1179 ib_destroy_cm_id(p->id);
1180 ib_destroy_qp(p->qp);
1181 kfree(p);
1182 }
1183} 1327}
1184 1328
1185static void ipoib_cm_stale_task(struct work_struct *work) 1329static void ipoib_cm_stale_task(struct work_struct *work)
@@ -1212,7 +1356,7 @@ static void ipoib_cm_stale_task(struct work_struct *work)
1212} 1356}
1213 1357
1214 1358
1215static ssize_t show_mode(struct device *d, struct device_attribute *attr, 1359static ssize_t show_mode(struct device *d, struct device_attribute *attr,
1216 char *buf) 1360 char *buf)
1217{ 1361{
1218 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d)); 1362 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
@@ -1255,16 +1399,40 @@ int ipoib_cm_add_mode_attr(struct net_device *dev)
1255 return device_create_file(&dev->dev, &dev_attr_mode); 1399 return device_create_file(&dev->dev, &dev_attr_mode);
1256} 1400}
1257 1401
1258int ipoib_cm_dev_init(struct net_device *dev) 1402static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
1259{ 1403{
1260 struct ipoib_dev_priv *priv = netdev_priv(dev); 1404 struct ipoib_dev_priv *priv = netdev_priv(dev);
1261 struct ib_srq_init_attr srq_init_attr = { 1405 struct ib_srq_init_attr srq_init_attr = {
1262 .attr = { 1406 .attr = {
1263 .max_wr = ipoib_recvq_size, 1407 .max_wr = ipoib_recvq_size,
1264 .max_sge = IPOIB_CM_RX_SG 1408 .max_sge = max_sge
1265 } 1409 }
1266 }; 1410 };
1267 int ret, i; 1411
1412 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1413 if (IS_ERR(priv->cm.srq)) {
1414 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1415 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1416 priv->ca->name, PTR_ERR(priv->cm.srq));
1417 priv->cm.srq = NULL;
1418 return;
1419 }
1420
1421 priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring,
1422 GFP_KERNEL);
1423 if (!priv->cm.srq_ring) {
1424 printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
1425 priv->ca->name, ipoib_recvq_size);
1426 ib_destroy_srq(priv->cm.srq);
1427 priv->cm.srq = NULL;
1428 }
1429}
1430
1431int ipoib_cm_dev_init(struct net_device *dev)
1432{
1433 struct ipoib_dev_priv *priv = netdev_priv(dev);
1434 int i, ret;
1435 struct ib_device_attr attr;
1268 1436
1269 INIT_LIST_HEAD(&priv->cm.passive_ids); 1437 INIT_LIST_HEAD(&priv->cm.passive_ids);
1270 INIT_LIST_HEAD(&priv->cm.reap_list); 1438 INIT_LIST_HEAD(&priv->cm.reap_list);
@@ -1281,43 +1449,53 @@ int ipoib_cm_dev_init(struct net_device *dev)
1281 1449
1282 skb_queue_head_init(&priv->cm.skb_queue); 1450 skb_queue_head_init(&priv->cm.skb_queue);
1283 1451
1284 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr); 1452 ret = ib_query_device(priv->ca, &attr);
1285 if (IS_ERR(priv->cm.srq)) { 1453 if (ret) {
1286 ret = PTR_ERR(priv->cm.srq); 1454 printk(KERN_WARNING "ib_query_device() failed with %d\n", ret);
1287 priv->cm.srq = NULL;
1288 return ret; 1455 return ret;
1289 } 1456 }
1290 1457
1291 priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring, 1458 ipoib_dbg(priv, "max_srq_sge=%d\n", attr.max_srq_sge);
1292 GFP_KERNEL); 1459
1293 if (!priv->cm.srq_ring) { 1460 attr.max_srq_sge = min_t(int, IPOIB_CM_RX_SG, attr.max_srq_sge);
1294 printk(KERN_WARNING "%s: failed to allocate CM ring (%d entries)\n", 1461 ipoib_cm_create_srq(dev, attr.max_srq_sge);
1295 priv->ca->name, ipoib_recvq_size); 1462 if (ipoib_cm_has_srq(dev)) {
1296 ipoib_cm_dev_cleanup(dev); 1463 priv->cm.max_cm_mtu = attr.max_srq_sge * PAGE_SIZE - 0x10;
1297 return -ENOMEM; 1464 priv->cm.num_frags = attr.max_srq_sge;
1465 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1466 priv->cm.max_cm_mtu, priv->cm.num_frags);
1467 } else {
1468 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1469 priv->cm.num_frags = IPOIB_CM_RX_SG;
1298 } 1470 }
1299 1471
1300 for (i = 0; i < IPOIB_CM_RX_SG; ++i) 1472 for (i = 0; i < priv->cm.num_frags; ++i)
1301 priv->cm.rx_sge[i].lkey = priv->mr->lkey; 1473 priv->cm.rx_sge[i].lkey = priv->mr->lkey;
1302 1474
1303 priv->cm.rx_sge[0].length = IPOIB_CM_HEAD_SIZE; 1475 priv->cm.rx_sge[0].length = IPOIB_CM_HEAD_SIZE;
1304 for (i = 1; i < IPOIB_CM_RX_SG; ++i) 1476 for (i = 1; i < priv->cm.num_frags; ++i)
1305 priv->cm.rx_sge[i].length = PAGE_SIZE; 1477 priv->cm.rx_sge[i].length = PAGE_SIZE;
1306 priv->cm.rx_wr.next = NULL; 1478 priv->cm.rx_wr.next = NULL;
1307 priv->cm.rx_wr.sg_list = priv->cm.rx_sge; 1479 priv->cm.rx_wr.sg_list = priv->cm.rx_sge;
1308 priv->cm.rx_wr.num_sge = IPOIB_CM_RX_SG; 1480 priv->cm.rx_wr.num_sge = priv->cm.num_frags;
1481
1482 if (ipoib_cm_has_srq(dev)) {
1483 for (i = 0; i < ipoib_recvq_size; ++i) {
1484 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
1485 priv->cm.num_frags - 1,
1486 priv->cm.srq_ring[i].mapping)) {
1487 ipoib_warn(priv, "failed to allocate "
1488 "receive buffer %d\n", i);
1489 ipoib_cm_dev_cleanup(dev);
1490 return -ENOMEM;
1491 }
1309 1492
1310 for (i = 0; i < ipoib_recvq_size; ++i) { 1493 if (ipoib_cm_post_receive_srq(dev, i)) {
1311 if (!ipoib_cm_alloc_rx_skb(dev, i, IPOIB_CM_RX_SG - 1, 1494 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1312 priv->cm.srq_ring[i].mapping)) { 1495 "failed for buf %d\n", i);
1313 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i); 1496 ipoib_cm_dev_cleanup(dev);
1314 ipoib_cm_dev_cleanup(dev); 1497 return -EIO;
1315 return -ENOMEM; 1498 }
1316 }
1317 if (ipoib_cm_post_receive(dev, i)) {
1318 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
1319 ipoib_cm_dev_cleanup(dev);
1320 return -EIO;
1321 } 1499 }
1322 } 1500 }
1323 1501
@@ -1328,7 +1506,7 @@ int ipoib_cm_dev_init(struct net_device *dev)
1328void ipoib_cm_dev_cleanup(struct net_device *dev) 1506void ipoib_cm_dev_cleanup(struct net_device *dev)
1329{ 1507{
1330 struct ipoib_dev_priv *priv = netdev_priv(dev); 1508 struct ipoib_dev_priv *priv = netdev_priv(dev);
1331 int i, ret; 1509 int ret;
1332 1510
1333 if (!priv->cm.srq) 1511 if (!priv->cm.srq)
1334 return; 1512 return;
@@ -1342,13 +1520,7 @@ void ipoib_cm_dev_cleanup(struct net_device *dev)
1342 priv->cm.srq = NULL; 1520 priv->cm.srq = NULL;
1343 if (!priv->cm.srq_ring) 1521 if (!priv->cm.srq_ring)
1344 return; 1522 return;
1345 for (i = 0; i < ipoib_recvq_size; ++i) 1523
1346 if (priv->cm.srq_ring[i].skb) { 1524 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
1347 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
1348 priv->cm.srq_ring[i].mapping);
1349 dev_kfree_skb_any(priv->cm.srq_ring[i].skb);
1350 priv->cm.srq_ring[i].skb = NULL;
1351 }
1352 kfree(priv->cm.srq_ring);
1353 priv->cm.srq_ring = NULL; 1525 priv->cm.srq_ring = NULL;
1354} 1526}
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_fs.c b/drivers/infiniband/ulp/ipoib/ipoib_fs.c
index 44c174182a82..8b882bbd1d05 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_fs.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_fs.c
@@ -124,7 +124,7 @@ static int ipoib_mcg_seq_show(struct seq_file *file, void *iter_ptr)
124 return 0; 124 return 0;
125} 125}
126 126
127static struct seq_operations ipoib_mcg_seq_ops = { 127static const struct seq_operations ipoib_mcg_seq_ops = {
128 .start = ipoib_mcg_seq_start, 128 .start = ipoib_mcg_seq_start,
129 .next = ipoib_mcg_seq_next, 129 .next = ipoib_mcg_seq_next,
130 .stop = ipoib_mcg_seq_stop, 130 .stop = ipoib_mcg_seq_stop,
@@ -230,7 +230,7 @@ static int ipoib_path_seq_show(struct seq_file *file, void *iter_ptr)
230 return 0; 230 return 0;
231} 231}
232 232
233static struct seq_operations ipoib_path_seq_ops = { 233static const struct seq_operations ipoib_path_seq_ops = {
234 .start = ipoib_path_seq_start, 234 .start = ipoib_path_seq_start,
235 .next = ipoib_path_seq_next, 235 .next = ipoib_path_seq_next,
236 .stop = ipoib_path_seq_stop, 236 .stop = ipoib_path_seq_stop,
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_ib.c b/drivers/infiniband/ulp/ipoib/ipoib_ib.c
index 5063dd509ad2..52bc2bd5799a 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_ib.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_ib.c
@@ -345,12 +345,12 @@ static inline int post_send(struct ipoib_dev_priv *priv,
345{ 345{
346 struct ib_send_wr *bad_wr; 346 struct ib_send_wr *bad_wr;
347 347
348 priv->tx_sge.addr = addr; 348 priv->tx_sge.addr = addr;
349 priv->tx_sge.length = len; 349 priv->tx_sge.length = len;
350 350
351 priv->tx_wr.wr_id = wr_id; 351 priv->tx_wr.wr_id = wr_id;
352 priv->tx_wr.wr.ud.remote_qpn = qpn; 352 priv->tx_wr.wr.ud.remote_qpn = qpn;
353 priv->tx_wr.wr.ud.ah = address; 353 priv->tx_wr.wr.ud.ah = address;
354 354
355 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr); 355 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
356} 356}
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_main.c b/drivers/infiniband/ulp/ipoib/ipoib_main.c
index c9f6077b615e..a082466f4a83 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_main.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_main.c
@@ -182,17 +182,20 @@ static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
182 struct ipoib_dev_priv *priv = netdev_priv(dev); 182 struct ipoib_dev_priv *priv = netdev_priv(dev);
183 183
184 /* dev->mtu > 2K ==> connected mode */ 184 /* dev->mtu > 2K ==> connected mode */
185 if (ipoib_cm_admin_enabled(dev) && new_mtu <= IPOIB_CM_MTU) { 185 if (ipoib_cm_admin_enabled(dev)) {
186 if (new_mtu > ipoib_cm_max_mtu(dev))
187 return -EINVAL;
188
186 if (new_mtu > priv->mcast_mtu) 189 if (new_mtu > priv->mcast_mtu)
187 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n", 190 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
188 priv->mcast_mtu); 191 priv->mcast_mtu);
192
189 dev->mtu = new_mtu; 193 dev->mtu = new_mtu;
190 return 0; 194 return 0;
191 } 195 }
192 196
193 if (new_mtu > IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN) { 197 if (new_mtu > IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN)
194 return -EINVAL; 198 return -EINVAL;
195 }
196 199
197 priv->admin_mtu = new_mtu; 200 priv->admin_mtu = new_mtu;
198 201
@@ -474,8 +477,8 @@ static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
474 INIT_LIST_HEAD(&path->neigh_list); 477 INIT_LIST_HEAD(&path->neigh_list);
475 478
476 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid)); 479 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
477 path->pathrec.sgid = priv->local_gid; 480 path->pathrec.sgid = priv->local_gid;
478 path->pathrec.pkey = cpu_to_be16(priv->pkey); 481 path->pathrec.pkey = cpu_to_be16(priv->pkey);
479 path->pathrec.numb_path = 1; 482 path->pathrec.numb_path = 1;
480 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class; 483 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
481 484
@@ -669,16 +672,6 @@ static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
669 if (unlikely(!spin_trylock_irqsave(&priv->tx_lock, flags))) 672 if (unlikely(!spin_trylock_irqsave(&priv->tx_lock, flags)))
670 return NETDEV_TX_LOCKED; 673 return NETDEV_TX_LOCKED;
671 674
672 /*
673 * Check if our queue is stopped. Since we have the LLTX bit
674 * set, we can't rely on netif_stop_queue() preventing our
675 * xmit function from being called with a full queue.
676 */
677 if (unlikely(netif_queue_stopped(dev))) {
678 spin_unlock_irqrestore(&priv->tx_lock, flags);
679 return NETDEV_TX_BUSY;
680 }
681
682 if (likely(skb->dst && skb->dst->neighbour)) { 675 if (likely(skb->dst && skb->dst->neighbour)) {
683 if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) { 676 if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
684 ipoib_path_lookup(skb, dev); 677 ipoib_path_lookup(skb, dev);
@@ -950,34 +943,34 @@ static void ipoib_setup(struct net_device *dev)
950{ 943{
951 struct ipoib_dev_priv *priv = netdev_priv(dev); 944 struct ipoib_dev_priv *priv = netdev_priv(dev);
952 945
953 dev->open = ipoib_open; 946 dev->open = ipoib_open;
954 dev->stop = ipoib_stop; 947 dev->stop = ipoib_stop;
955 dev->change_mtu = ipoib_change_mtu; 948 dev->change_mtu = ipoib_change_mtu;
956 dev->hard_start_xmit = ipoib_start_xmit; 949 dev->hard_start_xmit = ipoib_start_xmit;
957 dev->tx_timeout = ipoib_timeout; 950 dev->tx_timeout = ipoib_timeout;
958 dev->header_ops = &ipoib_header_ops; 951 dev->header_ops = &ipoib_header_ops;
959 dev->set_multicast_list = ipoib_set_mcast_list; 952 dev->set_multicast_list = ipoib_set_mcast_list;
960 dev->neigh_setup = ipoib_neigh_setup_dev; 953 dev->neigh_setup = ipoib_neigh_setup_dev;
961 954
962 netif_napi_add(dev, &priv->napi, ipoib_poll, 100); 955 netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
963 956
964 dev->watchdog_timeo = HZ; 957 dev->watchdog_timeo = HZ;
965 958
966 dev->flags |= IFF_BROADCAST | IFF_MULTICAST; 959 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
967 960
968 /* 961 /*
969 * We add in INFINIBAND_ALEN to allow for the destination 962 * We add in INFINIBAND_ALEN to allow for the destination
970 * address "pseudoheader" for skbs without neighbour struct. 963 * address "pseudoheader" for skbs without neighbour struct.
971 */ 964 */
972 dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN; 965 dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
973 dev->addr_len = INFINIBAND_ALEN; 966 dev->addr_len = INFINIBAND_ALEN;
974 dev->type = ARPHRD_INFINIBAND; 967 dev->type = ARPHRD_INFINIBAND;
975 dev->tx_queue_len = ipoib_sendq_size * 2; 968 dev->tx_queue_len = ipoib_sendq_size * 2;
976 dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX; 969 dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
977 970
978 /* MTU will be reset when mcast join happens */ 971 /* MTU will be reset when mcast join happens */
979 dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN; 972 dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
980 priv->mcast_mtu = priv->admin_mtu = dev->mtu; 973 priv->mcast_mtu = priv->admin_mtu = dev->mtu;
981 974
982 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN); 975 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
983 976
@@ -1268,6 +1261,9 @@ static int __init ipoib_init_module(void)
1268 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size); 1261 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1269 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE); 1262 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1270 ipoib_sendq_size = max(ipoib_sendq_size, IPOIB_MIN_QUEUE_SIZE); 1263 ipoib_sendq_size = max(ipoib_sendq_size, IPOIB_MIN_QUEUE_SIZE);
1264#ifdef CONFIG_INFINIBAND_IPOIB_CM
1265 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1266#endif
1271 1267
1272 ret = ipoib_register_debugfs(); 1268 ret = ipoib_register_debugfs();
1273 if (ret) 1269 if (ret)
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
index 9bcfc7ad6aa6..2628339e3a99 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
@@ -702,7 +702,7 @@ void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
702 702
703out: 703out:
704 if (mcast && mcast->ah) { 704 if (mcast && mcast->ah) {
705 if (skb->dst && 705 if (skb->dst &&
706 skb->dst->neighbour && 706 skb->dst->neighbour &&
707 !*to_ipoib_neigh(skb->dst->neighbour)) { 707 !*to_ipoib_neigh(skb->dst->neighbour)) {
708 struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour, 708 struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
@@ -710,7 +710,7 @@ out:
710 710
711 if (neigh) { 711 if (neigh) {
712 kref_get(&mcast->ah->ref); 712 kref_get(&mcast->ah->ref);
713 neigh->ah = mcast->ah; 713 neigh->ah = mcast->ah;
714 list_add_tail(&neigh->list, &mcast->neigh_list); 714 list_add_tail(&neigh->list, &mcast->neigh_list);
715 } 715 }
716 } 716 }
@@ -788,10 +788,6 @@ void ipoib_mcast_restart_task(struct work_struct *work)
788 788
789 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid); 789 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
790 790
791 /* Add in the P_Key */
792 mgid.raw[4] = (priv->pkey >> 8) & 0xff;
793 mgid.raw[5] = priv->pkey & 0xff;
794
795 mcast = __ipoib_mcast_find(dev, &mgid); 791 mcast = __ipoib_mcast_find(dev, &mgid);
796 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) { 792 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
797 struct ipoib_mcast *nmcast; 793 struct ipoib_mcast *nmcast;
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_verbs.c b/drivers/infiniband/ulp/ipoib/ipoib_verbs.c
index 3c6e45db0ab5..433e99ac227b 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_verbs.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_verbs.c
@@ -172,8 +172,12 @@ int ipoib_transport_dev_init(struct net_device *dev, struct ib_device *ca)
172 172
173 size = ipoib_sendq_size + ipoib_recvq_size + 1; 173 size = ipoib_sendq_size + ipoib_recvq_size + 1;
174 ret = ipoib_cm_dev_init(dev); 174 ret = ipoib_cm_dev_init(dev);
175 if (!ret) 175 if (!ret) {
176 size += ipoib_recvq_size + 1 /* 1 extra for rx_drain_qp */; 176 if (ipoib_cm_has_srq(dev))
177 size += ipoib_recvq_size + 1; /* 1 extra for rx_drain_qp */
178 else
179 size += ipoib_recvq_size * ipoib_max_conn_qp;
180 }
177 181
178 priv->cq = ib_create_cq(priv->ca, ipoib_ib_completion, NULL, dev, size, 0); 182 priv->cq = ib_create_cq(priv->ca, ipoib_ib_completion, NULL, dev, size, 0);
179 if (IS_ERR(priv->cq)) { 183 if (IS_ERR(priv->cq)) {
@@ -197,12 +201,12 @@ int ipoib_transport_dev_init(struct net_device *dev, struct ib_device *ca)
197 priv->dev->dev_addr[2] = (priv->qp->qp_num >> 8) & 0xff; 201 priv->dev->dev_addr[2] = (priv->qp->qp_num >> 8) & 0xff;
198 priv->dev->dev_addr[3] = (priv->qp->qp_num ) & 0xff; 202 priv->dev->dev_addr[3] = (priv->qp->qp_num ) & 0xff;
199 203
200 priv->tx_sge.lkey = priv->mr->lkey; 204 priv->tx_sge.lkey = priv->mr->lkey;
201 205
202 priv->tx_wr.opcode = IB_WR_SEND; 206 priv->tx_wr.opcode = IB_WR_SEND;
203 priv->tx_wr.sg_list = &priv->tx_sge; 207 priv->tx_wr.sg_list = &priv->tx_sge;
204 priv->tx_wr.num_sge = 1; 208 priv->tx_wr.num_sge = 1;
205 priv->tx_wr.send_flags = IB_SEND_SIGNALED; 209 priv->tx_wr.send_flags = IB_SEND_SIGNALED;
206 210
207 return 0; 211 return 0;
208 212
diff --git a/drivers/infiniband/ulp/iser/Kconfig b/drivers/infiniband/ulp/iser/Kconfig
index fe604c8d2996..77dedba829e6 100644
--- a/drivers/infiniband/ulp/iser/Kconfig
+++ b/drivers/infiniband/ulp/iser/Kconfig
@@ -8,5 +8,5 @@ config INFINIBAND_ISER
8 that speak iSCSI over iSER over InfiniBand. 8 that speak iSCSI over iSER over InfiniBand.
9 9
10 The iSER protocol is defined by IETF. 10 The iSER protocol is defined by IETF.
11 See <http://www.ietf.org/internet-drafts/draft-ietf-ips-iser-05.txt> 11 See <http://www.ietf.org/rfc/rfc5046.txt>
12 and <http://www.infinibandta.org/members/spec/iser_annex_060418.pdf> 12 and <http://www.infinibandta.org/members/spec/Annex_iSER.PDF>
diff --git a/drivers/infiniband/ulp/iser/iscsi_iser.c b/drivers/infiniband/ulp/iser/iscsi_iser.c
index bad8dacafd10..dfa5a4544187 100644
--- a/drivers/infiniband/ulp/iser/iscsi_iser.c
+++ b/drivers/infiniband/ulp/iser/iscsi_iser.c
@@ -551,6 +551,7 @@ static struct scsi_host_template iscsi_iser_sht = {
551 .module = THIS_MODULE, 551 .module = THIS_MODULE,
552 .name = "iSCSI Initiator over iSER, v." DRV_VER, 552 .name = "iSCSI Initiator over iSER, v." DRV_VER,
553 .queuecommand = iscsi_queuecommand, 553 .queuecommand = iscsi_queuecommand,
554 .change_queue_depth = iscsi_change_queue_depth,
554 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1, 555 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
555 .sg_tablesize = ISCSI_ISER_SG_TABLESIZE, 556 .sg_tablesize = ISCSI_ISER_SG_TABLESIZE,
556 .max_sectors = 1024, 557 .max_sectors = 1024,
diff --git a/drivers/infiniband/ulp/iser/iser_initiator.c b/drivers/infiniband/ulp/iser/iser_initiator.c
index a6f2303ed14a..ba1b455949c0 100644
--- a/drivers/infiniband/ulp/iser/iser_initiator.c
+++ b/drivers/infiniband/ulp/iser/iser_initiator.c
@@ -561,7 +561,7 @@ void iser_rcv_completion(struct iser_desc *rx_desc,
561 if (opcode == ISCSI_OP_SCSI_CMD_RSP) { 561 if (opcode == ISCSI_OP_SCSI_CMD_RSP) {
562 itt = get_itt(hdr->itt); /* mask out cid and age bits */ 562 itt = get_itt(hdr->itt); /* mask out cid and age bits */
563 if (!(itt < session->cmds_max)) 563 if (!(itt < session->cmds_max))
564 iser_err("itt can't be matched to task!!!" 564 iser_err("itt can't be matched to task!!! "
565 "conn %p opcode %d cmds_max %d itt %d\n", 565 "conn %p opcode %d cmds_max %d itt %d\n",
566 conn->iscsi_conn,opcode,session->cmds_max,itt); 566 conn->iscsi_conn,opcode,session->cmds_max,itt);
567 /* use the mapping given with the cmds array indexed by itt */ 567 /* use the mapping given with the cmds array indexed by itt */
diff --git a/drivers/infiniband/ulp/iser/iser_verbs.c b/drivers/infiniband/ulp/iser/iser_verbs.c
index 654a4dce0236..714b8db02b29 100644
--- a/drivers/infiniband/ulp/iser/iser_verbs.c
+++ b/drivers/infiniband/ulp/iser/iser_verbs.c
@@ -105,7 +105,7 @@ pd_err:
105} 105}
106 106
107/** 107/**
108 * iser_free_device_ib_res - destory/dealloc/dereg the DMA MR, 108 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
109 * CQ and PD created with the device associated with the adapator. 109 * CQ and PD created with the device associated with the adapator.
110 */ 110 */
111static void iser_free_device_ib_res(struct iser_device *device) 111static void iser_free_device_ib_res(struct iser_device *device)
@@ -475,13 +475,11 @@ static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *eve
475 iser_disconnected_handler(cma_id); 475 iser_disconnected_handler(cma_id);
476 break; 476 break;
477 case RDMA_CM_EVENT_DEVICE_REMOVAL: 477 case RDMA_CM_EVENT_DEVICE_REMOVAL:
478 iser_err("Device removal is currently unsupported\n");
478 BUG(); 479 BUG();
479 break; 480 break;
480 case RDMA_CM_EVENT_CONNECT_RESPONSE:
481 BUG();
482 break;
483 case RDMA_CM_EVENT_CONNECT_REQUEST:
484 default: 481 default:
482 iser_err("Unexpected RDMA CM event (%d)\n", event->event);
485 break; 483 break;
486 } 484 }
487 return ret; 485 return ret;
diff --git a/drivers/infiniband/ulp/srp/ib_srp.c b/drivers/infiniband/ulp/srp/ib_srp.c
index bdb6f8517401..f2d2c7e2c76b 100644
--- a/drivers/infiniband/ulp/srp/ib_srp.c
+++ b/drivers/infiniband/ulp/srp/ib_srp.c
@@ -272,7 +272,8 @@ static void srp_path_rec_completion(int status,
272 272
273 target->status = status; 273 target->status = status;
274 if (status) 274 if (status)
275 printk(KERN_ERR PFX "Got failed path rec status %d\n", status); 275 shost_printk(KERN_ERR, target->scsi_host,
276 PFX "Got failed path rec status %d\n", status);
276 else 277 else
277 target->path = *pathrec; 278 target->path = *pathrec;
278 complete(&target->done); 279 complete(&target->done);
@@ -303,7 +304,8 @@ static int srp_lookup_path(struct srp_target_port *target)
303 wait_for_completion(&target->done); 304 wait_for_completion(&target->done);
304 305
305 if (target->status < 0) 306 if (target->status < 0)
306 printk(KERN_WARNING PFX "Path record query failed\n"); 307 shost_printk(KERN_WARNING, target->scsi_host,
308 PFX "Path record query failed\n");
307 309
308 return target->status; 310 return target->status;
309} 311}
@@ -379,9 +381,10 @@ static int srp_send_req(struct srp_target_port *target)
379 * the second 8 bytes to the local node GUID. 381 * the second 8 bytes to the local node GUID.
380 */ 382 */
381 if (srp_target_is_topspin(target)) { 383 if (srp_target_is_topspin(target)) {
382 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround " 384 shost_printk(KERN_DEBUG, target->scsi_host,
383 "activated for target GUID %016llx\n", 385 PFX "Topspin/Cisco initiator port ID workaround "
384 (unsigned long long) be64_to_cpu(target->ioc_guid)); 386 "activated for target GUID %016llx\n",
387 (unsigned long long) be64_to_cpu(target->ioc_guid));
385 memset(req->priv.initiator_port_id, 0, 8); 388 memset(req->priv.initiator_port_id, 0, 8);
386 memcpy(req->priv.initiator_port_id + 8, 389 memcpy(req->priv.initiator_port_id + 8,
387 &target->srp_host->dev->dev->node_guid, 8); 390 &target->srp_host->dev->dev->node_guid, 8);
@@ -400,7 +403,8 @@ static void srp_disconnect_target(struct srp_target_port *target)
400 403
401 init_completion(&target->done); 404 init_completion(&target->done);
402 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) { 405 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
403 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n"); 406 shost_printk(KERN_DEBUG, target->scsi_host,
407 PFX "Sending CM DREQ failed\n");
404 return; 408 return;
405 } 409 }
406 wait_for_completion(&target->done); 410 wait_for_completion(&target->done);
@@ -568,7 +572,8 @@ static int srp_reconnect_target(struct srp_target_port *target)
568 return ret; 572 return ret;
569 573
570err: 574err:
571 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret); 575 shost_printk(KERN_ERR, target->scsi_host,
576 PFX "reconnect failed (%d), removing target port.\n", ret);
572 577
573 /* 578 /*
574 * We couldn't reconnect, so kill our target port off. 579 * We couldn't reconnect, so kill our target port off.
@@ -683,8 +688,9 @@ static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
683 688
684 if (scmnd->sc_data_direction != DMA_FROM_DEVICE && 689 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
685 scmnd->sc_data_direction != DMA_TO_DEVICE) { 690 scmnd->sc_data_direction != DMA_TO_DEVICE) {
686 printk(KERN_WARNING PFX "Unhandled data direction %d\n", 691 shost_printk(KERN_WARNING, target->scsi_host,
687 scmnd->sc_data_direction); 692 PFX "Unhandled data direction %d\n",
693 scmnd->sc_data_direction);
688 return -EINVAL; 694 return -EINVAL;
689 } 695 }
690 696
@@ -786,8 +792,9 @@ static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
786 } else { 792 } else {
787 scmnd = req->scmnd; 793 scmnd = req->scmnd;
788 if (!scmnd) 794 if (!scmnd)
789 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n", 795 shost_printk(KERN_ERR, target->scsi_host,
790 (unsigned long long) rsp->tag); 796 "Null scmnd for RSP w/tag %016llx\n",
797 (unsigned long long) rsp->tag);
791 scmnd->result = rsp->status; 798 scmnd->result = rsp->status;
792 799
793 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) { 800 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
@@ -831,7 +838,8 @@ static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
831 if (0) { 838 if (0) {
832 int i; 839 int i;
833 840
834 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode); 841 shost_printk(KERN_ERR, target->scsi_host,
842 PFX "recv completion, opcode 0x%02x\n", opcode);
835 843
836 for (i = 0; i < wc->byte_len; ++i) { 844 for (i = 0; i < wc->byte_len; ++i) {
837 if (i % 8 == 0) 845 if (i % 8 == 0)
@@ -852,11 +860,13 @@ static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
852 860
853 case SRP_T_LOGOUT: 861 case SRP_T_LOGOUT:
854 /* XXX Handle target logout */ 862 /* XXX Handle target logout */
855 printk(KERN_WARNING PFX "Got target logout request\n"); 863 shost_printk(KERN_WARNING, target->scsi_host,
864 PFX "Got target logout request\n");
856 break; 865 break;
857 866
858 default: 867 default:
859 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode); 868 shost_printk(KERN_WARNING, target->scsi_host,
869 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
860 break; 870 break;
861 } 871 }
862 872
@@ -872,9 +882,10 @@ static void srp_completion(struct ib_cq *cq, void *target_ptr)
872 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); 882 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
873 while (ib_poll_cq(cq, 1, &wc) > 0) { 883 while (ib_poll_cq(cq, 1, &wc) > 0) {
874 if (wc.status) { 884 if (wc.status) {
875 printk(KERN_ERR PFX "failed %s status %d\n", 885 shost_printk(KERN_ERR, target->scsi_host,
876 wc.wr_id & SRP_OP_RECV ? "receive" : "send", 886 PFX "failed %s status %d\n",
877 wc.status); 887 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
888 wc.status);
878 target->qp_in_error = 1; 889 target->qp_in_error = 1;
879 break; 890 break;
880 } 891 }
@@ -930,13 +941,18 @@ static int srp_post_recv(struct srp_target_port *target)
930 * req_lim and tx_head. Lock cannot be dropped between call here and 941 * req_lim and tx_head. Lock cannot be dropped between call here and
931 * call to __srp_post_send(). 942 * call to __srp_post_send().
932 */ 943 */
933static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target) 944static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
945 enum srp_request_type req_type)
934{ 946{
947 s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
948
935 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE) 949 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
936 return NULL; 950 return NULL;
937 951
938 if (unlikely(target->req_lim < 1)) 952 if (target->req_lim < min) {
939 ++target->zero_req_lim; 953 ++target->zero_req_lim;
954 return NULL;
955 }
940 956
941 return target->tx_ring[target->tx_head & SRP_SQ_SIZE]; 957 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
942} 958}
@@ -993,7 +1009,7 @@ static int srp_queuecommand(struct scsi_cmnd *scmnd,
993 return 0; 1009 return 0;
994 } 1010 }
995 1011
996 iu = __srp_get_tx_iu(target); 1012 iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
997 if (!iu) 1013 if (!iu)
998 goto err; 1014 goto err;
999 1015
@@ -1022,12 +1038,13 @@ static int srp_queuecommand(struct scsi_cmnd *scmnd,
1022 1038
1023 len = srp_map_data(scmnd, target, req); 1039 len = srp_map_data(scmnd, target, req);
1024 if (len < 0) { 1040 if (len < 0) {
1025 printk(KERN_ERR PFX "Failed to map data\n"); 1041 shost_printk(KERN_ERR, target->scsi_host,
1042 PFX "Failed to map data\n");
1026 goto err; 1043 goto err;
1027 } 1044 }
1028 1045
1029 if (__srp_post_recv(target)) { 1046 if (__srp_post_recv(target)) {
1030 printk(KERN_ERR PFX "Recv failed\n"); 1047 shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
1031 goto err_unmap; 1048 goto err_unmap;
1032 } 1049 }
1033 1050
@@ -1035,7 +1052,7 @@ static int srp_queuecommand(struct scsi_cmnd *scmnd,
1035 DMA_TO_DEVICE); 1052 DMA_TO_DEVICE);
1036 1053
1037 if (__srp_post_send(target, iu, len)) { 1054 if (__srp_post_send(target, iu, len)) {
1038 printk(KERN_ERR PFX "Send failed\n"); 1055 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
1039 goto err_unmap; 1056 goto err_unmap;
1040 } 1057 }
1041 1058
@@ -1090,6 +1107,7 @@ static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1090 struct ib_cm_event *event, 1107 struct ib_cm_event *event,
1091 struct srp_target_port *target) 1108 struct srp_target_port *target)
1092{ 1109{
1110 struct Scsi_Host *shost = target->scsi_host;
1093 struct ib_class_port_info *cpi; 1111 struct ib_class_port_info *cpi;
1094 int opcode; 1112 int opcode;
1095 1113
@@ -1115,19 +1133,22 @@ static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1115 memcpy(target->path.dgid.raw, 1133 memcpy(target->path.dgid.raw,
1116 event->param.rej_rcvd.ari, 16); 1134 event->param.rej_rcvd.ari, 16);
1117 1135
1118 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n", 1136 shost_printk(KERN_DEBUG, shost,
1119 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix), 1137 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1120 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id)); 1138 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1139 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1121 1140
1122 target->status = SRP_PORT_REDIRECT; 1141 target->status = SRP_PORT_REDIRECT;
1123 } else { 1142 } else {
1124 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n"); 1143 shost_printk(KERN_WARNING, shost,
1144 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1125 target->status = -ECONNRESET; 1145 target->status = -ECONNRESET;
1126 } 1146 }
1127 break; 1147 break;
1128 1148
1129 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID: 1149 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1130 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n"); 1150 shost_printk(KERN_WARNING, shost,
1151 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1131 target->status = -ECONNRESET; 1152 target->status = -ECONNRESET;
1132 break; 1153 break;
1133 1154
@@ -1138,20 +1159,21 @@ static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1138 u32 reason = be32_to_cpu(rej->reason); 1159 u32 reason = be32_to_cpu(rej->reason);
1139 1160
1140 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE) 1161 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1141 printk(KERN_WARNING PFX 1162 shost_printk(KERN_WARNING, shost,
1142 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n"); 1163 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1143 else 1164 else
1144 printk(KERN_WARNING PFX 1165 shost_printk(KERN_WARNING, shost,
1145 "SRP LOGIN REJECTED, reason 0x%08x\n", reason); 1166 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1146 } else 1167 } else
1147 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED," 1168 shost_printk(KERN_WARNING, shost,
1148 " opcode 0x%02x\n", opcode); 1169 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1170 " opcode 0x%02x\n", opcode);
1149 target->status = -ECONNRESET; 1171 target->status = -ECONNRESET;
1150 break; 1172 break;
1151 1173
1152 default: 1174 default:
1153 printk(KERN_WARNING " REJ reason 0x%x\n", 1175 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1154 event->param.rej_rcvd.reason); 1176 event->param.rej_rcvd.reason);
1155 target->status = -ECONNRESET; 1177 target->status = -ECONNRESET;
1156 } 1178 }
1157} 1179}
@@ -1166,7 +1188,8 @@ static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1166 1188
1167 switch (event->event) { 1189 switch (event->event) {
1168 case IB_CM_REQ_ERROR: 1190 case IB_CM_REQ_ERROR:
1169 printk(KERN_DEBUG PFX "Sending CM REQ failed\n"); 1191 shost_printk(KERN_DEBUG, target->scsi_host,
1192 PFX "Sending CM REQ failed\n");
1170 comp = 1; 1193 comp = 1;
1171 target->status = -ECONNRESET; 1194 target->status = -ECONNRESET;
1172 break; 1195 break;
@@ -1184,7 +1207,8 @@ static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1184 target->scsi_host->can_queue = min(target->req_lim, 1207 target->scsi_host->can_queue = min(target->req_lim,
1185 target->scsi_host->can_queue); 1208 target->scsi_host->can_queue);
1186 } else { 1209 } else {
1187 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode); 1210 shost_printk(KERN_WARNING, target->scsi_host,
1211 PFX "Unhandled RSP opcode %#x\n", opcode);
1188 target->status = -ECONNRESET; 1212 target->status = -ECONNRESET;
1189 break; 1213 break;
1190 } 1214 }
@@ -1230,20 +1254,23 @@ static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1230 break; 1254 break;
1231 1255
1232 case IB_CM_REJ_RECEIVED: 1256 case IB_CM_REJ_RECEIVED:
1233 printk(KERN_DEBUG PFX "REJ received\n"); 1257 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
1234 comp = 1; 1258 comp = 1;
1235 1259
1236 srp_cm_rej_handler(cm_id, event, target); 1260 srp_cm_rej_handler(cm_id, event, target);
1237 break; 1261 break;
1238 1262
1239 case IB_CM_DREQ_RECEIVED: 1263 case IB_CM_DREQ_RECEIVED:
1240 printk(KERN_WARNING PFX "DREQ received - connection closed\n"); 1264 shost_printk(KERN_WARNING, target->scsi_host,
1265 PFX "DREQ received - connection closed\n");
1241 if (ib_send_cm_drep(cm_id, NULL, 0)) 1266 if (ib_send_cm_drep(cm_id, NULL, 0))
1242 printk(KERN_ERR PFX "Sending CM DREP failed\n"); 1267 shost_printk(KERN_ERR, target->scsi_host,
1268 PFX "Sending CM DREP failed\n");
1243 break; 1269 break;
1244 1270
1245 case IB_CM_TIMEWAIT_EXIT: 1271 case IB_CM_TIMEWAIT_EXIT:
1246 printk(KERN_ERR PFX "connection closed\n"); 1272 shost_printk(KERN_ERR, target->scsi_host,
1273 PFX "connection closed\n");
1247 1274
1248 comp = 1; 1275 comp = 1;
1249 target->status = 0; 1276 target->status = 0;
@@ -1255,7 +1282,8 @@ static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1255 break; 1282 break;
1256 1283
1257 default: 1284 default:
1258 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event); 1285 shost_printk(KERN_WARNING, target->scsi_host,
1286 PFX "Unhandled CM event %d\n", event->event);
1259 break; 1287 break;
1260 } 1288 }
1261 1289
@@ -1283,7 +1311,7 @@ static int srp_send_tsk_mgmt(struct srp_target_port *target,
1283 1311
1284 init_completion(&req->done); 1312 init_completion(&req->done);
1285 1313
1286 iu = __srp_get_tx_iu(target); 1314 iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
1287 if (!iu) 1315 if (!iu)
1288 goto out; 1316 goto out;
1289 1317
@@ -1332,7 +1360,7 @@ static int srp_abort(struct scsi_cmnd *scmnd)
1332 struct srp_request *req; 1360 struct srp_request *req;
1333 int ret = SUCCESS; 1361 int ret = SUCCESS;
1334 1362
1335 printk(KERN_ERR "SRP abort called\n"); 1363 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
1336 1364
1337 if (target->qp_in_error) 1365 if (target->qp_in_error)
1338 return FAILED; 1366 return FAILED;
@@ -1362,7 +1390,7 @@ static int srp_reset_device(struct scsi_cmnd *scmnd)
1362 struct srp_target_port *target = host_to_target(scmnd->device->host); 1390 struct srp_target_port *target = host_to_target(scmnd->device->host);
1363 struct srp_request *req, *tmp; 1391 struct srp_request *req, *tmp;
1364 1392
1365 printk(KERN_ERR "SRP reset_device called\n"); 1393 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
1366 1394
1367 if (target->qp_in_error) 1395 if (target->qp_in_error)
1368 return FAILED; 1396 return FAILED;
@@ -1389,7 +1417,7 @@ static int srp_reset_host(struct scsi_cmnd *scmnd)
1389 struct srp_target_port *target = host_to_target(scmnd->device->host); 1417 struct srp_target_port *target = host_to_target(scmnd->device->host);
1390 int ret = FAILED; 1418 int ret = FAILED;
1391 1419
1392 printk(KERN_ERR PFX "SRP reset_host called\n"); 1420 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
1393 1421
1394 if (!srp_reconnect_target(target)) 1422 if (!srp_reconnect_target(target))
1395 ret = SUCCESS; 1423 ret = SUCCESS;
@@ -1543,6 +1571,7 @@ static struct scsi_host_template srp_template = {
1543 .this_id = -1, 1571 .this_id = -1,
1544 .cmd_per_lun = SRP_SQ_SIZE, 1572 .cmd_per_lun = SRP_SQ_SIZE,
1545 .use_clustering = ENABLE_CLUSTERING, 1573 .use_clustering = ENABLE_CLUSTERING,
1574 .use_sg_chaining = ENABLE_SG_CHAINING,
1546 .shost_attrs = srp_host_attrs 1575 .shost_attrs = srp_host_attrs
1547}; 1576};
1548 1577
@@ -1814,8 +1843,9 @@ static ssize_t srp_create_target(struct class_device *class_dev,
1814 1843
1815 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid); 1844 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
1816 1845
1817 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x " 1846 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1818 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", 1847 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1848 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1819 (unsigned long long) be64_to_cpu(target->id_ext), 1849 (unsigned long long) be64_to_cpu(target->id_ext),
1820 (unsigned long long) be64_to_cpu(target->ioc_guid), 1850 (unsigned long long) be64_to_cpu(target->ioc_guid),
1821 be16_to_cpu(target->path.pkey), 1851 be16_to_cpu(target->path.pkey),
@@ -1842,7 +1872,8 @@ static ssize_t srp_create_target(struct class_device *class_dev,
1842 target->qp_in_error = 0; 1872 target->qp_in_error = 0;
1843 ret = srp_connect_target(target); 1873 ret = srp_connect_target(target);
1844 if (ret) { 1874 if (ret) {
1845 printk(KERN_ERR PFX "Connection failed\n"); 1875 shost_printk(KERN_ERR, target->scsi_host,
1876 PFX "Connection failed\n");
1846 goto err_cm_id; 1877 goto err_cm_id;
1847 } 1878 }
1848 1879
diff --git a/drivers/infiniband/ulp/srp/ib_srp.h b/drivers/infiniband/ulp/srp/ib_srp.h
index e3573e7038c4..4a3c1f37e4c2 100644
--- a/drivers/infiniband/ulp/srp/ib_srp.h
+++ b/drivers/infiniband/ulp/srp/ib_srp.h
@@ -79,6 +79,11 @@ enum srp_target_state {
79 SRP_TARGET_REMOVED 79 SRP_TARGET_REMOVED
80}; 80};
81 81
82enum srp_request_type {
83 SRP_REQ_NORMAL,
84 SRP_REQ_TASK_MGMT,
85};
86
82struct srp_device { 87struct srp_device {
83 struct list_head dev_list; 88 struct list_head dev_list;
84 struct ib_device *dev; 89 struct ib_device *dev;
diff --git a/drivers/lguest/x86/core.c b/drivers/lguest/x86/core.c
index 482aec2a9631..96d0fd07c57d 100644
--- a/drivers/lguest/x86/core.c
+++ b/drivers/lguest/x86/core.c
@@ -459,7 +459,7 @@ void __init lguest_arch_host_init(void)
459 459
460 /* We don't need the complexity of CPUs coming and going while we're 460 /* We don't need the complexity of CPUs coming and going while we're
461 * doing this. */ 461 * doing this. */
462 lock_cpu_hotplug(); 462 get_online_cpus();
463 if (cpu_has_pge) { /* We have a broader idea of "global". */ 463 if (cpu_has_pge) { /* We have a broader idea of "global". */
464 /* Remember that this was originally set (for cleanup). */ 464 /* Remember that this was originally set (for cleanup). */
465 cpu_had_pge = 1; 465 cpu_had_pge = 1;
@@ -469,20 +469,20 @@ void __init lguest_arch_host_init(void)
469 /* Turn off the feature in the global feature set. */ 469 /* Turn off the feature in the global feature set. */
470 clear_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability); 470 clear_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
471 } 471 }
472 unlock_cpu_hotplug(); 472 put_online_cpus();
473}; 473};
474/*:*/ 474/*:*/
475 475
476void __exit lguest_arch_host_fini(void) 476void __exit lguest_arch_host_fini(void)
477{ 477{
478 /* If we had PGE before we started, turn it back on now. */ 478 /* If we had PGE before we started, turn it back on now. */
479 lock_cpu_hotplug(); 479 get_online_cpus();
480 if (cpu_had_pge) { 480 if (cpu_had_pge) {
481 set_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability); 481 set_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
482 /* adjust_pge's argument "1" means set PGE. */ 482 /* adjust_pge's argument "1" means set PGE. */
483 on_each_cpu(adjust_pge, (void *)1, 0, 1); 483 on_each_cpu(adjust_pge, (void *)1, 0, 1);
484 } 484 }
485 unlock_cpu_hotplug(); 485 put_online_cpus();
486} 486}
487 487
488 488
diff --git a/drivers/media/Kconfig b/drivers/media/Kconfig
index 1604f0490404..8f4a45346de7 100644
--- a/drivers/media/Kconfig
+++ b/drivers/media/Kconfig
@@ -69,11 +69,13 @@ source "drivers/media/common/Kconfig"
69config VIDEO_TUNER 69config VIDEO_TUNER
70 tristate 70 tristate
71 depends on I2C 71 depends on I2C
72 select TUNER_XC2028 if !VIDEO_TUNER_CUSTOMIZE
72 select TUNER_MT20XX if !VIDEO_TUNER_CUSTOMIZE 73 select TUNER_MT20XX if !VIDEO_TUNER_CUSTOMIZE
73 select TUNER_TDA8290 if !VIDEO_TUNER_CUSTOMIZE 74 select TUNER_TDA8290 if !VIDEO_TUNER_CUSTOMIZE
74 select TUNER_TEA5761 if !VIDEO_TUNER_CUSTOMIZE 75 select TUNER_TEA5761 if !VIDEO_TUNER_CUSTOMIZE
75 select TUNER_TEA5767 if !VIDEO_TUNER_CUSTOMIZE 76 select TUNER_TEA5767 if !VIDEO_TUNER_CUSTOMIZE
76 select TUNER_SIMPLE if !VIDEO_TUNER_CUSTOMIZE 77 select TUNER_SIMPLE if !VIDEO_TUNER_CUSTOMIZE
78 select TUNER_TDA9887 if !VIDEO_TUNER_CUSTOMIZE
77 79
78menuconfig VIDEO_TUNER_CUSTOMIZE 80menuconfig VIDEO_TUNER_CUSTOMIZE
79 bool "Customize analog tuner modules to build" 81 bool "Customize analog tuner modules to build"
@@ -89,6 +91,13 @@ menuconfig VIDEO_TUNER_CUSTOMIZE
89 91
90if VIDEO_TUNER_CUSTOMIZE 92if VIDEO_TUNER_CUSTOMIZE
91 93
94config TUNER_XC2028
95 tristate "XCeive xc2028/xc3028 tuners"
96 depends on I2C
97 default m if VIDEO_TUNER_CUSTOMIZE
98 help
99 Say Y here to include support for the xc2028/xc3028 tuners.
100
92config TUNER_MT20XX 101config TUNER_MT20XX
93 tristate "Microtune 2032 / 2050 tuners" 102 tristate "Microtune 2032 / 2050 tuners"
94 depends on I2C 103 depends on I2C
@@ -97,8 +106,10 @@ config TUNER_MT20XX
97 Say Y here to include support for the MT2032 / MT2050 tuner. 106 Say Y here to include support for the MT2032 / MT2050 tuner.
98 107
99config TUNER_TDA8290 108config TUNER_TDA8290
100 tristate "TDA 8290+8275(a) tuner combo" 109 tristate "TDA 8290/8295 + 8275(a)/18271 tuner combo"
101 depends on I2C 110 depends on I2C
111 select DVB_TDA827X
112 select DVB_TDA18271
102 default m if VIDEO_TUNER_CUSTOMIZE 113 default m if VIDEO_TUNER_CUSTOMIZE
103 help 114 help
104 Say Y here to include support for Philips TDA8290+8275(a) tuner. 115 Say Y here to include support for Philips TDA8290+8275(a) tuner.
@@ -120,10 +131,19 @@ config TUNER_TEA5767
120config TUNER_SIMPLE 131config TUNER_SIMPLE
121 tristate "Simple tuner support" 132 tristate "Simple tuner support"
122 depends on I2C 133 depends on I2C
134 select TUNER_TDA9887
123 default m if VIDEO_TUNER_CUSTOMIZE 135 default m if VIDEO_TUNER_CUSTOMIZE
124 help 136 help
125 Say Y here to include support for various simple tuners. 137 Say Y here to include support for various simple tuners.
126 138
139config TUNER_TDA9887
140 tristate "TDA 9885/6/7 analog IF demodulator"
141 depends on I2C
142 default m if VIDEO_TUNER_CUSTOMIZE
143 help
144 Say Y here to include support for Philips TDA9885/6/7
145 analog IF demodulator.
146
127endif # VIDEO_TUNER_CUSTOMIZE 147endif # VIDEO_TUNER_CUSTOMIZE
128 148
129config VIDEOBUF_GEN 149config VIDEOBUF_GEN
diff --git a/drivers/media/common/Kconfig b/drivers/media/common/Kconfig
index c5092ef1082f..06ca75911b7f 100644
--- a/drivers/media/common/Kconfig
+++ b/drivers/media/common/Kconfig
@@ -1,6 +1,6 @@
1config VIDEO_SAA7146 1config VIDEO_SAA7146
2 tristate 2 tristate
3 depends on I2C 3 depends on I2C && PCI
4 4
5config VIDEO_SAA7146_VV 5config VIDEO_SAA7146_VV
6 tristate 6 tristate
diff --git a/drivers/media/common/ir-functions.c b/drivers/media/common/ir-functions.c
index e7c3ab951a44..bb2a027b9483 100644
--- a/drivers/media/common/ir-functions.c
+++ b/drivers/media/common/ir-functions.c
@@ -258,7 +258,7 @@ int ir_decode_biphase(u32 *samples, int count, int low, int high)
258 * saa7134 */ 258 * saa7134 */
259 259
260/* decode raw bit pattern to RC5 code */ 260/* decode raw bit pattern to RC5 code */
261u32 ir_rc5_decode(unsigned int code) 261static u32 ir_rc5_decode(unsigned int code)
262{ 262{
263 unsigned int org_code = code; 263 unsigned int org_code = code;
264 unsigned int pair; 264 unsigned int pair;
@@ -371,7 +371,6 @@ EXPORT_SYMBOL_GPL(ir_dump_samples);
371EXPORT_SYMBOL_GPL(ir_decode_biphase); 371EXPORT_SYMBOL_GPL(ir_decode_biphase);
372EXPORT_SYMBOL_GPL(ir_decode_pulsedistance); 372EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
373 373
374EXPORT_SYMBOL_GPL(ir_rc5_decode);
375EXPORT_SYMBOL_GPL(ir_rc5_timer_end); 374EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
376EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup); 375EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);
377 376
diff --git a/drivers/media/common/ir-keymaps.c b/drivers/media/common/ir-keymaps.c
index 185e8a860c1a..a4a937c90534 100644
--- a/drivers/media/common/ir-keymaps.c
+++ b/drivers/media/common/ir-keymaps.c
@@ -1331,7 +1331,12 @@ IR_KEYTAB_TYPE ir_codes_winfast[IR_KEYTAB_SIZE] = {
1331 [ 0x35 ] = KEY_FASTFORWARD, 1331 [ 0x35 ] = KEY_FASTFORWARD,
1332 [ 0x36 ] = KEY_TV, 1332 [ 0x36 ] = KEY_TV,
1333 [ 0x37 ] = KEY_RADIO, /* FM */ 1333 [ 0x37 ] = KEY_RADIO, /* FM */
1334 [ 0x38 ] = KEY_DVD 1334 [ 0x38 ] = KEY_DVD,
1335
1336 [ 0x3e ] = KEY_F21, /* MCE +VOL, on Y04G0033 */
1337 [ 0x3a ] = KEY_F22, /* MCE -VOL, on Y04G0033 */
1338 [ 0x3b ] = KEY_F23, /* MCE +CH, on Y04G0033 */
1339 [ 0x3f ] = KEY_F24 /* MCE -CH, on Y04G0033 */
1335}; 1340};
1336 1341
1337EXPORT_SYMBOL_GPL(ir_codes_winfast); 1342EXPORT_SYMBOL_GPL(ir_codes_winfast);
@@ -1843,3 +1848,142 @@ IR_KEYTAB_TYPE ir_codes_fusionhdtv_mce[IR_KEYTAB_SIZE] = {
1843}; 1848};
1844 1849
1845EXPORT_SYMBOL_GPL(ir_codes_fusionhdtv_mce); 1850EXPORT_SYMBOL_GPL(ir_codes_fusionhdtv_mce);
1851
1852/* Pinnacle PCTV HD 800i mini remote */
1853IR_KEYTAB_TYPE ir_codes_pinnacle_pctv_hd[IR_KEYTAB_SIZE] = {
1854
1855 [0x0f] = KEY_1,
1856 [0x15] = KEY_2,
1857 [0x10] = KEY_3,
1858 [0x18] = KEY_4,
1859 [0x1b] = KEY_5,
1860 [0x1e] = KEY_6,
1861 [0x11] = KEY_7,
1862 [0x21] = KEY_8,
1863 [0x12] = KEY_9,
1864 [0x27] = KEY_0,
1865
1866 [0x24] = KEY_ZOOM,
1867 [0x2a] = KEY_SUBTITLE,
1868
1869 [0x00] = KEY_MUTE,
1870 [0x01] = KEY_ENTER, /* Pinnacle Logo */
1871 [0x39] = KEY_POWER,
1872
1873 [0x03] = KEY_VOLUMEUP,
1874 [0x09] = KEY_VOLUMEDOWN,
1875 [0x06] = KEY_CHANNELUP,
1876 [0x0c] = KEY_CHANNELDOWN,
1877
1878 [0x2d] = KEY_REWIND,
1879 [0x30] = KEY_PLAYPAUSE,
1880 [0x33] = KEY_FASTFORWARD,
1881 [0x3c] = KEY_STOP,
1882 [0x36] = KEY_RECORD,
1883 [0x3f] = KEY_EPG, /* Labeled "?" */
1884};
1885EXPORT_SYMBOL_GPL(ir_codes_pinnacle_pctv_hd);
1886
1887/*
1888 * Igor Kuznetsov <igk72@ya.ru>
1889 * Andrey J. Melnikov <temnota@kmv.ru>
1890 *
1891 * Keytable is used by BeholdTV 60x series, M6 series at
1892 * least, and probably other cards too.
1893 * The "ascii-art picture" below (in comments, first row
1894 * is the keycode in hex, and subsequent row(s) shows
1895 * the button labels (several variants when appropriate)
1896 * helps to descide which keycodes to assign to the buttons.
1897 */
1898IR_KEYTAB_TYPE ir_codes_behold[IR_KEYTAB_SIZE] = {
1899
1900 /* 0x1c 0x12 *
1901 * TV/FM POWER *
1902 * */
1903 [ 0x1c ] = KEY_TUNER, /*XXX KEY_TV KEY_RADIO */
1904 [ 0x12 ] = KEY_POWER,
1905
1906 /* 0x01 0x02 0x03 *
1907 * 1 2 3 *
1908 * *
1909 * 0x04 0x05 0x06 *
1910 * 4 5 6 *
1911 * *
1912 * 0x07 0x08 0x09 *
1913 * 7 8 9 *
1914 * */
1915 [ 0x01 ] = KEY_1,
1916 [ 0x02 ] = KEY_2,
1917 [ 0x03 ] = KEY_3,
1918 [ 0x04 ] = KEY_4,
1919 [ 0x05 ] = KEY_5,
1920 [ 0x06 ] = KEY_6,
1921 [ 0x07 ] = KEY_7,
1922 [ 0x08 ] = KEY_8,
1923 [ 0x09 ] = KEY_9,
1924
1925 /* 0x0a 0x00 0x17 *
1926 * RECALL 0 MODE *
1927 * */
1928 [ 0x0a ] = KEY_AGAIN,
1929 [ 0x00 ] = KEY_0,
1930 [ 0x17 ] = KEY_MODE,
1931
1932 /* 0x14 0x10 *
1933 * ASPECT FULLSCREEN *
1934 * */
1935 [ 0x14 ] = KEY_SCREEN,
1936 [ 0x10 ] = KEY_ZOOM,
1937
1938 /* 0x0b *
1939 * Up *
1940 * *
1941 * 0x18 0x16 0x0c *
1942 * Left Ok Right *
1943 * *
1944 * 0x015 *
1945 * Down *
1946 * */
1947 [ 0x0b ] = KEY_CHANNELUP, /*XXX KEY_UP */
1948 [ 0x18 ] = KEY_VOLUMEDOWN, /*XXX KEY_LEFT */
1949 [ 0x16 ] = KEY_OK, /*XXX KEY_ENTER */
1950 [ 0x0c ] = KEY_VOLUMEUP, /*XXX KEY_RIGHT */
1951 [ 0x15 ] = KEY_CHANNELDOWN, /*XXX KEY_DOWN */
1952
1953 /* 0x11 0x0d *
1954 * MUTE INFO *
1955 * */
1956 [ 0x11 ] = KEY_MUTE,
1957 [ 0x0d ] = KEY_INFO,
1958
1959 /* 0x0f 0x1b 0x1a *
1960 * RECORD PLAY/PAUSE STOP *
1961 * *
1962 * 0x0e 0x1f 0x1e *
1963 *TELETEXT AUDIO SOURCE *
1964 * RED YELLOW *
1965 * */
1966 [ 0x0f ] = KEY_RECORD,
1967 [ 0x1b ] = KEY_PLAYPAUSE,
1968 [ 0x1a ] = KEY_STOP,
1969 [ 0x0e ] = KEY_TEXT,
1970 [ 0x1f ] = KEY_RED, /*XXX KEY_AUDIO */
1971 [ 0x1e ] = KEY_YELLOW, /*XXX KEY_SOURCE */
1972
1973 /* 0x1d 0x13 0x19 *
1974 * SLEEP PREVIEW DVB *
1975 * GREEN BLUE *
1976 * */
1977 [ 0x1d ] = KEY_SLEEP,
1978 [ 0x13 ] = KEY_GREEN,
1979 [ 0x19 ] = KEY_BLUE, /*XXX KEY_SAT */
1980
1981 /* 0x58 0x5c *
1982 * FREEZE SNAPSHOT *
1983 * */
1984 [ 0x58 ] = KEY_SLOW,
1985 [ 0x5c ] = KEY_SAVE,
1986
1987};
1988
1989EXPORT_SYMBOL_GPL(ir_codes_behold);
diff --git a/drivers/media/common/saa7146_fops.c b/drivers/media/common/saa7146_fops.c
index 67d1b1b1b254..f0703d8bc3e8 100644
--- a/drivers/media/common/saa7146_fops.c
+++ b/drivers/media/common/saa7146_fops.c
@@ -61,7 +61,7 @@ void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q,
61 videobuf_waiton(&buf->vb,0,0); 61 videobuf_waiton(&buf->vb,0,0);
62 videobuf_dma_unmap(q, dma); 62 videobuf_dma_unmap(q, dma);
63 videobuf_dma_free(dma); 63 videobuf_dma_free(dma);
64 buf->vb.state = STATE_NEEDS_INIT; 64 buf->vb.state = VIDEOBUF_NEEDS_INIT;
65} 65}
66 66
67 67
@@ -83,7 +83,7 @@ int saa7146_buffer_queue(struct saa7146_dev *dev,
83 buf->activate(dev,buf,NULL); 83 buf->activate(dev,buf,NULL);
84 } else { 84 } else {
85 list_add_tail(&buf->vb.queue,&q->queue); 85 list_add_tail(&buf->vb.queue,&q->queue);
86 buf->vb.state = STATE_QUEUED; 86 buf->vb.state = VIDEOBUF_QUEUED;
87 DEB_D(("adding buffer %p to queue. (active buffer present)\n", buf)); 87 DEB_D(("adding buffer %p to queue. (active buffer present)\n", buf));
88 } 88 }
89 return 0; 89 return 0;
@@ -174,7 +174,7 @@ void saa7146_buffer_timeout(unsigned long data)
174 spin_lock_irqsave(&dev->slock,flags); 174 spin_lock_irqsave(&dev->slock,flags);
175 if (q->curr) { 175 if (q->curr) {
176 DEB_D(("timeout on %p\n", q->curr)); 176 DEB_D(("timeout on %p\n", q->curr));
177 saa7146_buffer_finish(dev,q,STATE_ERROR); 177 saa7146_buffer_finish(dev,q,VIDEOBUF_ERROR);
178 } 178 }
179 179
180 /* we don't restart the transfer here like other drivers do. when 180 /* we don't restart the transfer here like other drivers do. when
@@ -366,7 +366,7 @@ static unsigned int fops_poll(struct file *file, struct poll_table_struct *wait)
366 } 366 }
367 367
368 poll_wait(file, &buf->done, wait); 368 poll_wait(file, &buf->done, wait);
369 if (buf->state == STATE_DONE || buf->state == STATE_ERROR) { 369 if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR) {
370 DEB_D(("poll succeeded!\n")); 370 DEB_D(("poll succeeded!\n"));
371 return POLLIN|POLLRDNORM; 371 return POLLIN|POLLRDNORM;
372 } 372 }
@@ -538,6 +538,7 @@ int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev,
538 // fixme: -1 should be an insmod parameter *for the extension* (like "video_nr"); 538 // fixme: -1 should be an insmod parameter *for the extension* (like "video_nr");
539 if (video_register_device(vfd, type, -1) < 0) { 539 if (video_register_device(vfd, type, -1) < 0) {
540 ERR(("cannot register v4l2 device. skipping.\n")); 540 ERR(("cannot register v4l2 device. skipping.\n"));
541 video_device_release(vfd);
541 return -1; 542 return -1;
542 } 543 }
543 544
diff --git a/drivers/media/common/saa7146_vbi.c b/drivers/media/common/saa7146_vbi.c
index 6103484e4442..c32dda973e92 100644
--- a/drivers/media/common/saa7146_vbi.c
+++ b/drivers/media/common/saa7146_vbi.c
@@ -205,7 +205,7 @@ static int buffer_activate(struct saa7146_dev *dev,
205 struct saa7146_buf *next) 205 struct saa7146_buf *next)
206{ 206{
207 struct saa7146_vv *vv = dev->vv_data; 207 struct saa7146_vv *vv = dev->vv_data;
208 buf->vb.state = STATE_ACTIVE; 208 buf->vb.state = VIDEOBUF_ACTIVE;
209 209
210 DEB_VBI(("dev:%p, buf:%p, next:%p\n",dev,buf,next)); 210 DEB_VBI(("dev:%p, buf:%p, next:%p\n",dev,buf,next));
211 saa7146_set_vbi_capture(dev,buf,next); 211 saa7146_set_vbi_capture(dev,buf,next);
@@ -238,7 +238,7 @@ static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,e
238 if (buf->vb.size != size) 238 if (buf->vb.size != size)
239 saa7146_dma_free(dev,q,buf); 239 saa7146_dma_free(dev,q,buf);
240 240
241 if (STATE_NEEDS_INIT == buf->vb.state) { 241 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
242 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); 242 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
243 243
244 buf->vb.width = llength; 244 buf->vb.width = llength;
@@ -257,7 +257,7 @@ static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,e
257 if (0 != err) 257 if (0 != err)
258 return err; 258 return err;
259 } 259 }
260 buf->vb.state = STATE_PREPARED; 260 buf->vb.state = VIDEOBUF_PREPARED;
261 buf->activate = buffer_activate; 261 buf->activate = buffer_activate;
262 262
263 return 0; 263 return 0;
@@ -335,7 +335,7 @@ static void vbi_stop(struct saa7146_fh *fh, struct file *file)
335 saa7146_write(dev, MC1, MASK_20); 335 saa7146_write(dev, MC1, MASK_20);
336 336
337 if (vv->vbi_q.curr) { 337 if (vv->vbi_q.curr) {
338 saa7146_buffer_finish(dev,&vv->vbi_q,STATE_DONE); 338 saa7146_buffer_finish(dev,&vv->vbi_q,VIDEOBUF_DONE);
339 } 339 }
340 340
341 videobuf_queue_cancel(&fh->vbi_q); 341 videobuf_queue_cancel(&fh->vbi_q);
@@ -458,7 +458,7 @@ static void vbi_irq_done(struct saa7146_dev *dev, unsigned long status)
458 /* this must be += 2, one count for each field */ 458 /* this must be += 2, one count for each field */
459 vv->vbi_fieldcount+=2; 459 vv->vbi_fieldcount+=2;
460 vv->vbi_q.curr->vb.field_count = vv->vbi_fieldcount; 460 vv->vbi_q.curr->vb.field_count = vv->vbi_fieldcount;
461 saa7146_buffer_finish(dev,&vv->vbi_q,STATE_DONE); 461 saa7146_buffer_finish(dev,&vv->vbi_q,VIDEOBUF_DONE);
462 } else { 462 } else {
463 DEB_VBI(("dev:%p\n",dev)); 463 DEB_VBI(("dev:%p\n",dev));
464 } 464 }
diff --git a/drivers/media/common/saa7146_video.c b/drivers/media/common/saa7146_video.c
index ae36d101006b..c31ab480d8e1 100644
--- a/drivers/media/common/saa7146_video.c
+++ b/drivers/media/common/saa7146_video.c
@@ -1235,7 +1235,7 @@ static int buffer_activate (struct saa7146_dev *dev,
1235{ 1235{
1236 struct saa7146_vv *vv = dev->vv_data; 1236 struct saa7146_vv *vv = dev->vv_data;
1237 1237
1238 buf->vb.state = STATE_ACTIVE; 1238 buf->vb.state = VIDEOBUF_ACTIVE;
1239 saa7146_set_capture(dev,buf,next); 1239 saa7146_set_capture(dev,buf,next);
1240 1240
1241 mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT); 1241 mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
@@ -1281,7 +1281,7 @@ static int buffer_prepare(struct videobuf_queue *q,
1281 saa7146_dma_free(dev,q,buf); 1281 saa7146_dma_free(dev,q,buf);
1282 } 1282 }
1283 1283
1284 if (STATE_NEEDS_INIT == buf->vb.state) { 1284 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1285 struct saa7146_format *sfmt; 1285 struct saa7146_format *sfmt;
1286 1286
1287 buf->vb.bytesperline = fh->video_fmt.bytesperline; 1287 buf->vb.bytesperline = fh->video_fmt.bytesperline;
@@ -1314,7 +1314,7 @@ static int buffer_prepare(struct videobuf_queue *q,
1314 if (err) 1314 if (err)
1315 goto oops; 1315 goto oops;
1316 } 1316 }
1317 buf->vb.state = STATE_PREPARED; 1317 buf->vb.state = VIDEOBUF_PREPARED;
1318 buf->activate = buffer_activate; 1318 buf->activate = buffer_activate;
1319 1319
1320 return 0; 1320 return 0;
@@ -1453,7 +1453,7 @@ static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1453 1453
1454 /* only finish the buffer if we have one... */ 1454 /* only finish the buffer if we have one... */
1455 if( NULL != q->curr ) { 1455 if( NULL != q->curr ) {
1456 saa7146_buffer_finish(dev,q,STATE_DONE); 1456 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
1457 } 1457 }
1458 saa7146_buffer_next(dev,q,0); 1458 saa7146_buffer_next(dev,q,0);
1459 1459
diff --git a/drivers/media/dvb/b2c2/flexcop.c b/drivers/media/dvb/b2c2/flexcop.c
index 29ec4183118e..2ddafd071c97 100644
--- a/drivers/media/dvb/b2c2/flexcop.c
+++ b/drivers/media/dvb/b2c2/flexcop.c
@@ -212,7 +212,6 @@ void flexcop_reset_block_300(struct flexcop_device *fc)
212 212
213 fc->write_ibi_reg(fc,ctrl_208,v208_save); 213 fc->write_ibi_reg(fc,ctrl_208,v208_save);
214} 214}
215EXPORT_SYMBOL(flexcop_reset_block_300);
216 215
217struct flexcop_device *flexcop_device_kmalloc(size_t bus_specific_len) 216struct flexcop_device *flexcop_device_kmalloc(size_t bus_specific_len)
218{ 217{
diff --git a/drivers/media/dvb/bt8xx/bt878.c b/drivers/media/dvb/bt8xx/bt878.c
index 85e36a1d6d78..c7bbb40223f5 100644
--- a/drivers/media/dvb/bt8xx/bt878.c
+++ b/drivers/media/dvb/bt8xx/bt878.c
@@ -378,23 +378,37 @@ bt878_device_control(struct bt878 *bt, unsigned int cmd, union dst_gpio_packet *
378 378
379EXPORT_SYMBOL(bt878_device_control); 379EXPORT_SYMBOL(bt878_device_control);
380 380
381#define BROOKTREE_878_DEVICE(vend, dev, name) \
382 { \
383 .vendor = PCI_VENDOR_ID_BROOKTREE, \
384 .device = PCI_DEVICE_ID_BROOKTREE_878, \
385 .subvendor = (vend), .subdevice = (dev), \
386 .driver_data = (unsigned long) name \
387 }
381 388
382static struct cards card_list[] __devinitdata = { 389static struct pci_device_id bt878_pci_tbl[] __devinitdata = {
383 390 BROOKTREE_878_DEVICE(0x0071, 0x0101, "Nebula Electronics DigiTV"),
384 { 0x01010071, BTTV_BOARD_NEBULA_DIGITV, "Nebula Electronics DigiTV" }, 391 BROOKTREE_878_DEVICE(0x1461, 0x0761, "AverMedia AverTV DVB-T 761"),
385 { 0x07611461, BTTV_BOARD_AVDVBT_761, "AverMedia AverTV DVB-T 761" }, 392 BROOKTREE_878_DEVICE(0x11bd, 0x001c, "Pinnacle PCTV Sat"),
386 { 0x001c11bd, BTTV_BOARD_PINNACLESAT, "Pinnacle PCTV Sat" }, 393 BROOKTREE_878_DEVICE(0x11bd, 0x0026, "Pinnacle PCTV SAT CI"),
387 { 0x002611bd, BTTV_BOARD_TWINHAN_DST, "Pinnacle PCTV SAT CI" }, 394 BROOKTREE_878_DEVICE(0x1822, 0x0001, "Twinhan VisionPlus DVB"),
388 { 0x00011822, BTTV_BOARD_TWINHAN_DST, "Twinhan VisionPlus DVB" }, 395 BROOKTREE_878_DEVICE(0x270f, 0xfc00,
389 { 0xfc00270f, BTTV_BOARD_TWINHAN_DST, "ChainTech digitop DST-1000 DVB-S" }, 396 "ChainTech digitop DST-1000 DVB-S"),
390 { 0x07711461, BTTV_BOARD_AVDVBT_771, "AVermedia AverTV DVB-T 771" }, 397 BROOKTREE_878_DEVICE(0x1461, 0x0771, "AVermedia AverTV DVB-T 771"),
391 { 0xdb1018ac, BTTV_BOARD_DVICO_DVBT_LITE, "DViCO FusionHDTV DVB-T Lite" }, 398 BROOKTREE_878_DEVICE(0x18ac, 0xdb10, "DViCO FusionHDTV DVB-T Lite"),
392 { 0xdb1118ac, BTTV_BOARD_DVICO_DVBT_LITE, "Ultraview DVB-T Lite" }, 399 BROOKTREE_878_DEVICE(0x18ac, 0xdb11, "Ultraview DVB-T Lite"),
393 { 0xd50018ac, BTTV_BOARD_DVICO_FUSIONHDTV_5_LITE, "DViCO FusionHDTV 5 Lite" }, 400 BROOKTREE_878_DEVICE(0x18ac, 0xd500, "DViCO FusionHDTV 5 Lite"),
394 { 0x20007063, BTTV_BOARD_PC_HDTV, "pcHDTV HD-2000 TV" }, 401 BROOKTREE_878_DEVICE(0x7063, 0x2000, "pcHDTV HD-2000 TV"),
395 { 0x00261822, BTTV_BOARD_TWINHAN_DST, "DNTV Live! Mini" } 402 BROOKTREE_878_DEVICE(0x1822, 0x0026, "DNTV Live! Mini"),
403 { }
396}; 404};
397 405
406MODULE_DEVICE_TABLE(pci, bt878_pci_tbl);
407
408static const char * __devinit card_name(const struct pci_device_id *id)
409{
410 return id->driver_data ? (const char *)id->driver_data : "Unknown";
411}
398 412
399/***********************/ 413/***********************/
400/* PCI device handling */ 414/* PCI device handling */
@@ -403,15 +417,13 @@ static struct cards card_list[] __devinitdata = {
403static int __devinit bt878_probe(struct pci_dev *dev, 417static int __devinit bt878_probe(struct pci_dev *dev,
404 const struct pci_device_id *pci_id) 418 const struct pci_device_id *pci_id)
405{ 419{
406 int result = 0, has_dvb = 0, i; 420 int result = 0;
407 unsigned char lat; 421 unsigned char lat;
408 struct bt878 *bt; 422 struct bt878 *bt;
409#if defined(__powerpc__) 423#if defined(__powerpc__)
410 unsigned int cmd; 424 unsigned int cmd;
411#endif 425#endif
412 unsigned int cardid; 426 unsigned int cardid;
413 unsigned short id;
414 struct cards *dvb_cards;
415 427
416 printk(KERN_INFO "bt878: Bt878 AUDIO function found (%d).\n", 428 printk(KERN_INFO "bt878: Bt878 AUDIO function found (%d).\n",
417 bt878_num); 429 bt878_num);
@@ -423,25 +435,11 @@ static int __devinit bt878_probe(struct pci_dev *dev,
423 if (pci_enable_device(dev)) 435 if (pci_enable_device(dev))
424 return -EIO; 436 return -EIO;
425 437
426 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &id); 438 cardid = dev->subsystem_device << 16;
427 cardid = id << 16; 439 cardid |= dev->subsystem_vendor;
428 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &id);
429 cardid |= id;
430
431 for (i = 0, dvb_cards = card_list; i < ARRAY_SIZE(card_list); i++, dvb_cards++) {
432 if (cardid == dvb_cards->pci_id) {
433 printk("%s: card id=[0x%x],[ %s ] has DVB functions.\n",
434 __func__, cardid, dvb_cards->name);
435 has_dvb = 1;
436 }
437 }
438 440
439 if (!has_dvb) { 441 printk(KERN_INFO "%s: card id=[0x%x],[ %s ] has DVB functions.\n",
440 printk("%s: card id=[0x%x], Unknown card.\nExiting..\n", __func__, cardid); 442 __func__, cardid, card_name(pci_id));
441 result = -EINVAL;
442
443 goto fail0;
444 }
445 443
446 bt = &bt878[bt878_num]; 444 bt = &bt878[bt878_num];
447 bt->dev = dev; 445 bt->dev = dev;
@@ -572,14 +570,6 @@ static void __devexit bt878_remove(struct pci_dev *pci_dev)
572 return; 570 return;
573} 571}
574 572
575static struct pci_device_id bt878_pci_tbl[] __devinitdata = {
576 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BROOKTREE_878,
577 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
578 {0,}
579};
580
581MODULE_DEVICE_TABLE(pci, bt878_pci_tbl);
582
583static struct pci_driver bt878_pci_driver = { 573static struct pci_driver bt878_pci_driver = {
584 .name = "bt878", 574 .name = "bt878",
585 .id_table = bt878_pci_tbl, 575 .id_table = bt878_pci_tbl,
diff --git a/drivers/media/dvb/bt8xx/bt878.h b/drivers/media/dvb/bt8xx/bt878.h
index d593bc145628..375fd2892a11 100644
--- a/drivers/media/dvb/bt8xx/bt878.h
+++ b/drivers/media/dvb/bt8xx/bt878.h
@@ -101,12 +101,6 @@
101#define BTTV_BOARD_DVICO_DVBT_LITE 0x80 101#define BTTV_BOARD_DVICO_DVBT_LITE 0x80
102#define BTTV_BOARD_DVICO_FUSIONHDTV_5_LITE 0x87 102#define BTTV_BOARD_DVICO_FUSIONHDTV_5_LITE 0x87
103 103
104struct cards {
105 __u32 pci_id;
106 __u16 card_id;
107 char *name;
108};
109
110extern int bt878_num; 104extern int bt878_num;
111 105
112struct bt878 { 106struct bt878 {
diff --git a/drivers/media/dvb/bt8xx/dst.c b/drivers/media/dvb/bt8xx/dst.c
index b7a17e69ca4d..307ff35bdf13 100644
--- a/drivers/media/dvb/bt8xx/dst.c
+++ b/drivers/media/dvb/bt8xx/dst.c
@@ -71,6 +71,7 @@ MODULE_PARM_DESC(dst_algo, "tuning algo: default is 0=(SW), 1=(HW)");
71 } \ 71 } \
72} while(0) 72} while(0)
73 73
74static int dst_command(struct dst_state *state, u8 *data, u8 len);
74 75
75static void dst_packsize(struct dst_state *state, int psize) 76static void dst_packsize(struct dst_state *state, int psize)
76{ 77{
@@ -80,7 +81,8 @@ static void dst_packsize(struct dst_state *state, int psize)
80 bt878_device_control(state->bt, DST_IG_TS, &bits); 81 bt878_device_control(state->bt, DST_IG_TS, &bits);
81} 82}
82 83
83int dst_gpio_outb(struct dst_state *state, u32 mask, u32 enbb, u32 outhigh, int delay) 84static int dst_gpio_outb(struct dst_state *state, u32 mask, u32 enbb,
85 u32 outhigh, int delay)
84{ 86{
85 union dst_gpio_packet enb; 87 union dst_gpio_packet enb;
86 union dst_gpio_packet bits; 88 union dst_gpio_packet bits;
@@ -109,9 +111,8 @@ int dst_gpio_outb(struct dst_state *state, u32 mask, u32 enbb, u32 outhigh, int
109 111
110 return 0; 112 return 0;
111} 113}
112EXPORT_SYMBOL(dst_gpio_outb);
113 114
114int dst_gpio_inb(struct dst_state *state, u8 *result) 115static int dst_gpio_inb(struct dst_state *state, u8 *result)
115{ 116{
116 union dst_gpio_packet rd_packet; 117 union dst_gpio_packet rd_packet;
117 int err; 118 int err;
@@ -125,7 +126,6 @@ int dst_gpio_inb(struct dst_state *state, u8 *result)
125 126
126 return 0; 127 return 0;
127} 128}
128EXPORT_SYMBOL(dst_gpio_inb);
129 129
130int rdc_reset_state(struct dst_state *state) 130int rdc_reset_state(struct dst_state *state)
131{ 131{
@@ -145,7 +145,7 @@ int rdc_reset_state(struct dst_state *state)
145} 145}
146EXPORT_SYMBOL(rdc_reset_state); 146EXPORT_SYMBOL(rdc_reset_state);
147 147
148int rdc_8820_reset(struct dst_state *state) 148static int rdc_8820_reset(struct dst_state *state)
149{ 149{
150 dprintk(verbose, DST_DEBUG, 1, "Resetting DST"); 150 dprintk(verbose, DST_DEBUG, 1, "Resetting DST");
151 if (dst_gpio_outb(state, RDC_8820_RESET, RDC_8820_RESET, 0, NO_DELAY) < 0) { 151 if (dst_gpio_outb(state, RDC_8820_RESET, RDC_8820_RESET, 0, NO_DELAY) < 0) {
@@ -160,9 +160,8 @@ int rdc_8820_reset(struct dst_state *state)
160 160
161 return 0; 161 return 0;
162} 162}
163EXPORT_SYMBOL(rdc_8820_reset);
164 163
165int dst_pio_enable(struct dst_state *state) 164static int dst_pio_enable(struct dst_state *state)
166{ 165{
167 if (dst_gpio_outb(state, ~0, RDC_8820_PIO_0_ENABLE, 0, NO_DELAY) < 0) { 166 if (dst_gpio_outb(state, ~0, RDC_8820_PIO_0_ENABLE, 0, NO_DELAY) < 0) {
168 dprintk(verbose, DST_ERROR, 1, "dst_gpio_outb ERROR !"); 167 dprintk(verbose, DST_ERROR, 1, "dst_gpio_outb ERROR !");
@@ -172,7 +171,6 @@ int dst_pio_enable(struct dst_state *state)
172 171
173 return 0; 172 return 0;
174} 173}
175EXPORT_SYMBOL(dst_pio_enable);
176 174
177int dst_pio_disable(struct dst_state *state) 175int dst_pio_disable(struct dst_state *state)
178{ 176{
@@ -611,7 +609,7 @@ static int dst_type_print(struct dst_state *state, u8 type)
611 return 0; 609 return 0;
612} 610}
613 611
614struct tuner_types tuner_list[] = { 612static struct tuner_types tuner_list[] = {
615 { 613 {
616 .tuner_type = TUNER_TYPE_L64724, 614 .tuner_type = TUNER_TYPE_L64724,
617 .tuner_name = "L 64724", 615 .tuner_name = "L 64724",
@@ -1224,7 +1222,7 @@ static int dst_probe(struct dst_state *state)
1224 return 0; 1222 return 0;
1225} 1223}
1226 1224
1227int dst_command(struct dst_state *state, u8 *data, u8 len) 1225static int dst_command(struct dst_state *state, u8 *data, u8 len)
1228{ 1226{
1229 u8 reply; 1227 u8 reply;
1230 1228
@@ -1287,7 +1285,6 @@ error:
1287 return -EIO; 1285 return -EIO;
1288 1286
1289} 1287}
1290EXPORT_SYMBOL(dst_command);
1291 1288
1292static int dst_get_signal(struct dst_state *state) 1289static int dst_get_signal(struct dst_state *state)
1293{ 1290{
diff --git a/drivers/media/dvb/bt8xx/dst_common.h b/drivers/media/dvb/bt8xx/dst_common.h
index 87623d203a89..d88cf2add82b 100644
--- a/drivers/media/dvb/bt8xx/dst_common.h
+++ b/drivers/media/dvb/bt8xx/dst_common.h
@@ -165,10 +165,8 @@ struct dst_config
165}; 165};
166 166
167int rdc_reset_state(struct dst_state *state); 167int rdc_reset_state(struct dst_state *state);
168int rdc_8820_reset(struct dst_state *state);
169 168
170int dst_wait_dst_ready(struct dst_state *state, u8 delay_mode); 169int dst_wait_dst_ready(struct dst_state *state, u8 delay_mode);
171int dst_pio_enable(struct dst_state *state);
172int dst_pio_disable(struct dst_state *state); 170int dst_pio_disable(struct dst_state *state);
173int dst_error_recovery(struct dst_state* state); 171int dst_error_recovery(struct dst_state* state);
174int dst_error_bailout(struct dst_state *state); 172int dst_error_bailout(struct dst_state *state);
@@ -179,9 +177,6 @@ int read_dst(struct dst_state *state, u8 * ret, u8 len);
179u8 dst_check_sum(u8 * buf, u32 len); 177u8 dst_check_sum(u8 * buf, u32 len);
180struct dst_state* dst_attach(struct dst_state* state, struct dvb_adapter *dvb_adapter); 178struct dst_state* dst_attach(struct dst_state* state, struct dvb_adapter *dvb_adapter);
181struct dvb_device *dst_ca_attach(struct dst_state *state, struct dvb_adapter *dvb_adapter); 179struct dvb_device *dst_ca_attach(struct dst_state *state, struct dvb_adapter *dvb_adapter);
182int dst_gpio_outb(struct dst_state* state, u32 mask, u32 enbb, u32 outhigh, int delay);
183
184int dst_command(struct dst_state* state, u8 * data, u8 len);
185 180
186 181
187#endif // DST_COMMON_H 182#endif // DST_COMMON_H
diff --git a/drivers/media/dvb/dvb-core/dvb_frontend.c b/drivers/media/dvb/dvb-core/dvb_frontend.c
index 445f02665577..925cfa6221ad 100644
--- a/drivers/media/dvb/dvb-core/dvb_frontend.c
+++ b/drivers/media/dvb/dvb-core/dvb_frontend.c
@@ -1202,6 +1202,10 @@ void dvb_frontend_detach(struct dvb_frontend* fe)
1202 fe->ops.tuner_ops.release(fe); 1202 fe->ops.tuner_ops.release(fe);
1203 symbol_put_addr(fe->ops.tuner_ops.release); 1203 symbol_put_addr(fe->ops.tuner_ops.release);
1204 } 1204 }
1205 if (fe->ops.analog_ops.release) {
1206 fe->ops.analog_ops.release(fe);
1207 symbol_put_addr(fe->ops.analog_ops.release);
1208 }
1205 ptr = (void*)fe->ops.release; 1209 ptr = (void*)fe->ops.release;
1206 if (ptr) { 1210 if (ptr) {
1207 fe->ops.release(fe); 1211 fe->ops.release(fe);
@@ -1215,6 +1219,8 @@ void dvb_frontend_detach(struct dvb_frontend* fe)
1215 fe->ops.release_sec(fe); 1219 fe->ops.release_sec(fe);
1216 if (fe->ops.tuner_ops.release) 1220 if (fe->ops.tuner_ops.release)
1217 fe->ops.tuner_ops.release(fe); 1221 fe->ops.tuner_ops.release(fe);
1222 if (fe->ops.analog_ops.release)
1223 fe->ops.analog_ops.release(fe);
1218 if (fe->ops.release) 1224 if (fe->ops.release)
1219 fe->ops.release(fe); 1225 fe->ops.release(fe);
1220} 1226}
diff --git a/drivers/media/dvb/dvb-core/dvb_frontend.h b/drivers/media/dvb/dvb-core/dvb_frontend.h
index a5262e852c82..aa4133f0bd19 100644
--- a/drivers/media/dvb/dvb-core/dvb_frontend.h
+++ b/drivers/media/dvb/dvb-core/dvb_frontend.h
@@ -84,6 +84,9 @@ struct dvb_tuner_ops {
84 /** This is support for demods like the mt352 - fills out the supplied buffer with what to write. */ 84 /** This is support for demods like the mt352 - fills out the supplied buffer with what to write. */
85 int (*calc_regs)(struct dvb_frontend *fe, struct dvb_frontend_parameters *p, u8 *buf, int buf_len); 85 int (*calc_regs)(struct dvb_frontend *fe, struct dvb_frontend_parameters *p, u8 *buf, int buf_len);
86 86
87 /** This is to allow setting tuner-specific configs */
88 int (*set_config)(struct dvb_frontend *fe, void *priv_cfg);
89
87 int (*get_frequency)(struct dvb_frontend *fe, u32 *frequency); 90 int (*get_frequency)(struct dvb_frontend *fe, u32 *frequency);
88 int (*get_bandwidth)(struct dvb_frontend *fe, u32 *bandwidth); 91 int (*get_bandwidth)(struct dvb_frontend *fe, u32 *bandwidth);
89 92
@@ -98,6 +101,28 @@ struct dvb_tuner_ops {
98 int (*set_bandwidth)(struct dvb_frontend *fe, u32 bandwidth); 101 int (*set_bandwidth)(struct dvb_frontend *fe, u32 bandwidth);
99}; 102};
100 103
104struct analog_demod_info {
105 char *name;
106};
107
108struct analog_demod_ops {
109
110 struct analog_demod_info info;
111
112 void (*set_params)(struct dvb_frontend *fe,
113 struct analog_parameters *params);
114 int (*has_signal)(struct dvb_frontend *fe);
115 int (*is_stereo)(struct dvb_frontend *fe);
116 int (*get_afc)(struct dvb_frontend *fe);
117 void (*tuner_status)(struct dvb_frontend *fe);
118 void (*standby)(struct dvb_frontend *fe);
119 void (*release)(struct dvb_frontend *fe);
120 int (*i2c_gate_ctrl)(struct dvb_frontend *fe, int enable);
121
122 /** This is to allow setting tuner-specific configuration */
123 int (*set_config)(struct dvb_frontend *fe, void *priv_cfg);
124};
125
101struct dvb_frontend_ops { 126struct dvb_frontend_ops {
102 127
103 struct dvb_frontend_info info; 128 struct dvb_frontend_info info;
@@ -143,6 +168,7 @@ struct dvb_frontend_ops {
143 int (*ts_bus_ctrl)(struct dvb_frontend* fe, int acquire); 168 int (*ts_bus_ctrl)(struct dvb_frontend* fe, int acquire);
144 169
145 struct dvb_tuner_ops tuner_ops; 170 struct dvb_tuner_ops tuner_ops;
171 struct analog_demod_ops analog_ops;
146}; 172};
147 173
148#define MAX_EVENT 8 174#define MAX_EVENT 8
@@ -159,18 +185,19 @@ struct dvb_fe_events {
159struct dvb_frontend { 185struct dvb_frontend {
160 struct dvb_frontend_ops ops; 186 struct dvb_frontend_ops ops;
161 struct dvb_adapter *dvb; 187 struct dvb_adapter *dvb;
162 void* demodulator_priv; 188 void *demodulator_priv;
163 void* tuner_priv; 189 void *tuner_priv;
164 void* frontend_priv; 190 void *frontend_priv;
165 void* sec_priv; 191 void *sec_priv;
192 void *analog_demod_priv;
166}; 193};
167 194
168extern int dvb_register_frontend(struct dvb_adapter* dvb, 195extern int dvb_register_frontend(struct dvb_adapter *dvb,
169 struct dvb_frontend* fe); 196 struct dvb_frontend *fe);
170 197
171extern int dvb_unregister_frontend(struct dvb_frontend* fe); 198extern int dvb_unregister_frontend(struct dvb_frontend *fe);
172 199
173extern void dvb_frontend_detach(struct dvb_frontend* fe); 200extern void dvb_frontend_detach(struct dvb_frontend *fe);
174 201
175extern void dvb_frontend_reinitialise(struct dvb_frontend *fe); 202extern void dvb_frontend_reinitialise(struct dvb_frontend *fe);
176 203
diff --git a/drivers/media/dvb/dvb-core/dvb_ringbuffer.c b/drivers/media/dvb/dvb-core/dvb_ringbuffer.c
index 9878183ba3f0..ac9d93cf83c6 100644
--- a/drivers/media/dvb/dvb-core/dvb_ringbuffer.c
+++ b/drivers/media/dvb/dvb-core/dvb_ringbuffer.c
@@ -261,11 +261,6 @@ EXPORT_SYMBOL(dvb_ringbuffer_init);
261EXPORT_SYMBOL(dvb_ringbuffer_empty); 261EXPORT_SYMBOL(dvb_ringbuffer_empty);
262EXPORT_SYMBOL(dvb_ringbuffer_free); 262EXPORT_SYMBOL(dvb_ringbuffer_free);
263EXPORT_SYMBOL(dvb_ringbuffer_avail); 263EXPORT_SYMBOL(dvb_ringbuffer_avail);
264EXPORT_SYMBOL(dvb_ringbuffer_flush);
265EXPORT_SYMBOL(dvb_ringbuffer_flush_spinlock_wakeup); 264EXPORT_SYMBOL(dvb_ringbuffer_flush_spinlock_wakeup);
266EXPORT_SYMBOL(dvb_ringbuffer_read); 265EXPORT_SYMBOL(dvb_ringbuffer_read);
267EXPORT_SYMBOL(dvb_ringbuffer_write); 266EXPORT_SYMBOL(dvb_ringbuffer_write);
268EXPORT_SYMBOL(dvb_ringbuffer_pkt_write);
269EXPORT_SYMBOL(dvb_ringbuffer_pkt_read);
270EXPORT_SYMBOL(dvb_ringbuffer_pkt_dispose);
271EXPORT_SYMBOL(dvb_ringbuffer_pkt_next);
diff --git a/drivers/media/dvb/dvb-usb/af9005.c b/drivers/media/dvb/dvb-usb/af9005.c
index 7db6eee50e39..e7f76f515b4f 100644
--- a/drivers/media/dvb/dvb-usb/af9005.c
+++ b/drivers/media/dvb/dvb-usb/af9005.c
@@ -1026,6 +1026,7 @@ static int af9005_usb_probe(struct usb_interface *intf,
1026static struct usb_device_id af9005_usb_table[] = { 1026static struct usb_device_id af9005_usb_table[] = {
1027 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9005)}, 1027 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9005)},
1028 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE)}, 1028 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE)},
1029 {USB_DEVICE(USB_VID_ANSONIC, USB_PID_ANSONIC_DVBT_USB)},
1029 {0}, 1030 {0},
1030}; 1031};
1031 1032
@@ -1075,7 +1076,7 @@ static struct dvb_usb_device_properties af9005_properties = {
1075 .rc_key_map_size = 0, 1076 .rc_key_map_size = 0,
1076 .rc_query = af9005_rc_query, 1077 .rc_query = af9005_rc_query,
1077 1078
1078 .num_device_descs = 2, 1079 .num_device_descs = 3,
1079 .devices = { 1080 .devices = {
1080 {.name = "Afatech DVB-T USB1.1 stick", 1081 {.name = "Afatech DVB-T USB1.1 stick",
1081 .cold_ids = {&af9005_usb_table[0], NULL}, 1082 .cold_ids = {&af9005_usb_table[0], NULL},
@@ -1085,6 +1086,10 @@ static struct dvb_usb_device_properties af9005_properties = {
1085 .cold_ids = {&af9005_usb_table[1], NULL}, 1086 .cold_ids = {&af9005_usb_table[1], NULL},
1086 .warm_ids = {NULL}, 1087 .warm_ids = {NULL},
1087 }, 1088 },
1089 {.name = "Ansonic DVB-T USB1.1 stick",
1090 .cold_ids = {&af9005_usb_table[2], NULL},
1091 .warm_ids = {NULL},
1092 },
1088 {NULL}, 1093 {NULL},
1089 } 1094 }
1090}; 1095};
diff --git a/drivers/media/dvb/dvb-usb/au6610.c b/drivers/media/dvb/dvb-usb/au6610.c
index 18e0b16fb2a9..f3ff81314696 100644
--- a/drivers/media/dvb/dvb-usb/au6610.c
+++ b/drivers/media/dvb/dvb-usb/au6610.c
@@ -79,12 +79,12 @@ static int au6610_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
79 struct dvb_usb_device *d = i2c_get_adapdata(adap); 79 struct dvb_usb_device *d = i2c_get_adapdata(adap);
80 int i; 80 int i;
81 81
82 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
83 return -EAGAIN;
84
85 if (num > 2) 82 if (num > 2)
86 return -EINVAL; 83 return -EINVAL;
87 84
85 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
86 return -EAGAIN;
87
88 for (i = 0; i < num; i++) { 88 for (i = 0; i < num; i++) {
89 /* write/read request */ 89 /* write/read request */
90 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) { 90 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
diff --git a/drivers/media/dvb/dvb-usb/cxusb.c b/drivers/media/dvb/dvb-usb/cxusb.c
index 04e31cf7d530..c58365005ac1 100644
--- a/drivers/media/dvb/dvb-usb/cxusb.c
+++ b/drivers/media/dvb/dvb-usb/cxusb.c
@@ -15,7 +15,7 @@
15 * 15 *
16 * Copyright (C) 2005 Patrick Boettcher (patrick.boettcher@desy.de) 16 * Copyright (C) 2005 Patrick Boettcher (patrick.boettcher@desy.de)
17 * Copyright (C) 2006 Michael Krufky (mkrufky@linuxtv.org) 17 * Copyright (C) 2006 Michael Krufky (mkrufky@linuxtv.org)
18 * Copyright (C) 2006 Chris Pascoe (c.pascoe@itee.uq.edu.au) 18 * Copyright (C) 2006, 2007 Chris Pascoe (c.pascoe@itee.uq.edu.au)
19 * 19 *
20 * This program is free software; you can redistribute it and/or modify it 20 * This program is free software; you can redistribute it and/or modify it
21 * under the terms of the GNU General Public License as published by the Free 21 * under the terms of the GNU General Public License as published by the Free
@@ -30,11 +30,16 @@
30#include "mt352.h" 30#include "mt352.h"
31#include "mt352_priv.h" 31#include "mt352_priv.h"
32#include "zl10353.h" 32#include "zl10353.h"
33#include "tuner-xc2028.h"
34#include "tuner-xc2028-types.h"
33 35
34/* debug */ 36/* debug */
35int dvb_usb_cxusb_debug; 37static int dvb_usb_cxusb_debug;
36module_param_named(debug, dvb_usb_cxusb_debug, int, 0644); 38module_param_named(debug, dvb_usb_cxusb_debug, int, 0644);
37MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS); 39MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
40#define deb_info(args...) dprintk(dvb_usb_cxusb_debug,0x01,args)
41#define deb_i2c(args...) if (d->udev->descriptor.idVendor == USB_VID_MEDION) \
42 dprintk(dvb_usb_cxusb_debug,0x01,args)
38 43
39static int cxusb_ctrl_msg(struct dvb_usb_device *d, 44static int cxusb_ctrl_msg(struct dvb_usb_device *d,
40 u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen) 45 u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
@@ -46,11 +51,9 @@ static int cxusb_ctrl_msg(struct dvb_usb_device *d,
46 sndbuf[0] = cmd; 51 sndbuf[0] = cmd;
47 memcpy(&sndbuf[1], wbuf, wlen); 52 memcpy(&sndbuf[1], wbuf, wlen);
48 if (wo) 53 if (wo)
49 dvb_usb_generic_write(d, sndbuf, 1+wlen); 54 return dvb_usb_generic_write(d, sndbuf, 1+wlen);
50 else 55 else
51 dvb_usb_generic_rw(d, sndbuf, 1+wlen, rbuf, rlen, 0); 56 return dvb_usb_generic_rw(d, sndbuf, 1+wlen, rbuf, rlen, 0);
52
53 return 0;
54} 57}
55 58
56/* GPIO */ 59/* GPIO */
@@ -72,6 +75,34 @@ static void cxusb_gpio_tuner(struct dvb_usb_device *d, int onoff)
72 st->gpio_write_state[GPIO_TUNER] = onoff; 75 st->gpio_write_state[GPIO_TUNER] = onoff;
73} 76}
74 77
78static int cxusb_bluebird_gpio_rw(struct dvb_usb_device *d, u8 changemask,
79 u8 newval)
80{
81 u8 o[2], gpio_state;
82 int rc;
83
84 o[0] = 0xff & ~changemask; /* mask of bits to keep */
85 o[1] = newval & changemask; /* new values for bits */
86
87 rc = cxusb_ctrl_msg(d, CMD_BLUEBIRD_GPIO_RW, o, 2, &gpio_state, 1);
88 if (rc < 0 || (gpio_state & changemask) != (newval & changemask))
89 deb_info("bluebird_gpio_write failed.\n");
90
91 return rc < 0 ? rc : gpio_state;
92}
93
94static void cxusb_bluebird_gpio_pulse(struct dvb_usb_device *d, u8 pin, int low)
95{
96 cxusb_bluebird_gpio_rw(d, pin, low ? 0 : pin);
97 msleep(5);
98 cxusb_bluebird_gpio_rw(d, pin, low ? pin : 0);
99}
100
101static void cxusb_nano2_led(struct dvb_usb_device *d, int onoff)
102{
103 cxusb_bluebird_gpio_rw(d, 0x40, onoff ? 0 : 0x40);
104}
105
75/* I2C */ 106/* I2C */
76static int cxusb_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 107static int cxusb_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
77 int num) 108 int num)
@@ -82,9 +113,6 @@ static int cxusb_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
82 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 113 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
83 return -EAGAIN; 114 return -EAGAIN;
84 115
85 if (num > 2)
86 warn("more than two i2c messages at a time is not handled yet. TODO.");
87
88 for (i = 0; i < num; i++) { 116 for (i = 0; i < num; i++) {
89 117
90 if (d->udev->descriptor.idVendor == USB_VID_MEDION) 118 if (d->udev->descriptor.idVendor == USB_VID_MEDION)
@@ -97,8 +125,22 @@ static int cxusb_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
97 break; 125 break;
98 } 126 }
99 127
100 /* read request */ 128 if (msg[i].flags & I2C_M_RD) {
101 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) { 129 /* read only */
130 u8 obuf[3], ibuf[1+msg[i].len];
131 obuf[0] = 0;
132 obuf[1] = msg[i].len;
133 obuf[2] = msg[i].addr;
134 if (cxusb_ctrl_msg(d, CMD_I2C_READ,
135 obuf, 3,
136 ibuf, 1+msg[i].len) < 0) {
137 warn("i2c read failed");
138 break;
139 }
140 memcpy(msg[i].buf, &ibuf[1], msg[i].len);
141 } else if (i+1 < num && (msg[i+1].flags & I2C_M_RD) &&
142 msg[i].addr == msg[i+1].addr) {
143 /* write to then read from same address */
102 u8 obuf[3+msg[i].len], ibuf[1+msg[i+1].len]; 144 u8 obuf[3+msg[i].len], ibuf[1+msg[i+1].len];
103 obuf[0] = msg[i].len; 145 obuf[0] = msg[i].len;
104 obuf[1] = msg[i+1].len; 146 obuf[1] = msg[i+1].len;
@@ -116,7 +158,8 @@ static int cxusb_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
116 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len); 158 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
117 159
118 i++; 160 i++;
119 } else { /* write */ 161 } else {
162 /* write only */
120 u8 obuf[2+msg[i].len], ibuf; 163 u8 obuf[2+msg[i].len], ibuf;
121 obuf[0] = msg[i].addr; 164 obuf[0] = msg[i].addr;
122 obuf[1] = msg[i].len; 165 obuf[1] = msg[i].len;
@@ -131,7 +174,7 @@ static int cxusb_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
131 } 174 }
132 175
133 mutex_unlock(&d->i2c_mutex); 176 mutex_unlock(&d->i2c_mutex);
134 return i; 177 return i == num ? num : -EREMOTEIO;
135} 178}
136 179
137static u32 cxusb_i2c_func(struct i2c_adapter *adapter) 180static u32 cxusb_i2c_func(struct i2c_adapter *adapter)
@@ -162,6 +205,17 @@ static int cxusb_bluebird_power_ctrl(struct dvb_usb_device *d, int onoff)
162 return 0; 205 return 0;
163} 206}
164 207
208static int cxusb_nano2_power_ctrl(struct dvb_usb_device *d, int onoff)
209{
210 int rc = 0;
211
212 rc = cxusb_power_ctrl(d, onoff);
213 if (!onoff)
214 cxusb_nano2_led(d, 0);
215
216 return rc;
217}
218
165static int cxusb_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff) 219static int cxusb_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
166{ 220{
167 u8 buf[2] = { 0x03, 0x00 }; 221 u8 buf[2] = { 0x03, 0x00 };
@@ -197,6 +251,34 @@ static int cxusb_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
197 return 0; 251 return 0;
198} 252}
199 253
254static int cxusb_bluebird2_rc_query(struct dvb_usb_device *d, u32 *event,
255 int *state)
256{
257 struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
258 u8 ircode[4];
259 int i;
260 struct i2c_msg msg = { .addr = 0x6b, .flags = I2C_M_RD,
261 .buf = ircode, .len = 4 };
262
263 *event = 0;
264 *state = REMOTE_NO_KEY_PRESSED;
265
266 if (cxusb_i2c_xfer(&d->i2c_adap, &msg, 1) != 1)
267 return 0;
268
269 for (i = 0; i < d->props.rc_key_map_size; i++) {
270 if (keymap[i].custom == ircode[1] &&
271 keymap[i].data == ircode[2]) {
272 *event = keymap[i].event;
273 *state = REMOTE_KEY_PRESSED;
274
275 return 0;
276 }
277 }
278
279 return 0;
280}
281
200static struct dvb_usb_rc_key dvico_mce_rc_keys[] = { 282static struct dvb_usb_rc_key dvico_mce_rc_keys[] = {
201 { 0xfe, 0x02, KEY_TV }, 283 { 0xfe, 0x02, KEY_TV },
202 { 0xfe, 0x0e, KEY_MP3 }, 284 { 0xfe, 0x0e, KEY_MP3 },
@@ -351,6 +433,20 @@ static struct mt352_config cxusb_mt352_config = {
351 .demod_init = cxusb_mt352_demod_init, 433 .demod_init = cxusb_mt352_demod_init,
352}; 434};
353 435
436static struct zl10353_config cxusb_zl10353_xc3028_config = {
437 .demod_address = 0x0f,
438 .if2 = 45600,
439 .no_tuner = 1,
440 .parallel_ts = 1,
441};
442
443static struct mt352_config cxusb_mt352_xc3028_config = {
444 .demod_address = 0x0f,
445 .if2 = 4560,
446 .no_tuner = 1,
447 .demod_init = cxusb_mt352_demod_init,
448};
449
354/* Callbacks for DVB USB */ 450/* Callbacks for DVB USB */
355static int cxusb_fmd1216me_tuner_attach(struct dvb_usb_adapter *adap) 451static int cxusb_fmd1216me_tuner_attach(struct dvb_usb_adapter *adap)
356{ 452{
@@ -386,6 +482,51 @@ static int cxusb_lgh064f_tuner_attach(struct dvb_usb_adapter *adap)
386 return 0; 482 return 0;
387} 483}
388 484
485static int dvico_bluebird_xc2028_callback(void *ptr, int command, int arg)
486{
487 struct dvb_usb_device *d = ptr;
488
489 switch (command) {
490 case XC2028_TUNER_RESET:
491 deb_info("%s: XC2028_TUNER_RESET %d\n", __FUNCTION__, arg);
492 cxusb_bluebird_gpio_pulse(d, 0x01, 1);
493 break;
494 case XC2028_RESET_CLK:
495 deb_info("%s: XC2028_RESET_CLK %d\n", __FUNCTION__, arg);
496 break;
497 default:
498 deb_info("%s: unknown command %d, arg %d\n", __FUNCTION__,
499 command, arg);
500 return -EINVAL;
501 }
502
503 return 0;
504}
505
506static int cxusb_dvico_xc3028_tuner_attach(struct dvb_usb_adapter *adap)
507{
508 struct dvb_frontend *fe;
509 struct xc2028_config cfg = {
510 .i2c_adap = &adap->dev->i2c_adap,
511 .i2c_addr = 0x61,
512 .video_dev = adap->dev,
513 .callback = dvico_bluebird_xc2028_callback,
514 };
515 static struct xc2028_ctrl ctl = {
516 .fname = "xc3028-dvico-au-01.fw",
517 .max_len = 64,
518 .scode_table = ZARLINK456,
519 };
520
521 fe = dvb_attach(xc2028_attach, adap->fe, &cfg);
522 if (fe == NULL || fe->ops.tuner_ops.set_config == NULL)
523 return -EIO;
524
525 fe->ops.tuner_ops.set_config(fe, &ctl);
526
527 return 0;
528}
529
389static int cxusb_cx22702_frontend_attach(struct dvb_usb_adapter *adap) 530static int cxusb_cx22702_frontend_attach(struct dvb_usb_adapter *adap)
390{ 531{
391 u8 b; 532 u8 b;
@@ -447,27 +588,120 @@ static int cxusb_dee1601_frontend_attach(struct dvb_usb_adapter *adap)
447 return -EIO; 588 return -EIO;
448} 589}
449 590
591static int cxusb_dualdig4_frontend_attach(struct dvb_usb_adapter *adap)
592{
593 u8 ircode[4];
594 int i;
595 struct i2c_msg msg = { .addr = 0x6b, .flags = I2C_M_RD,
596 .buf = ircode, .len = 4 };
597
598 if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
599 err("set interface failed");
600
601 cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
602
603 /* reset the tuner and demodulator */
604 cxusb_bluebird_gpio_rw(adap->dev, 0x04, 0);
605 cxusb_bluebird_gpio_pulse(adap->dev, 0x01, 1);
606 cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
607
608 if ((adap->fe = dvb_attach(zl10353_attach,
609 &cxusb_zl10353_xc3028_config,
610 &adap->dev->i2c_adap)) == NULL)
611 return -EIO;
612
613 /* try to determine if there is no IR decoder on the I2C bus */
614 for (i = 0; adap->dev->props.rc_key_map != NULL && i < 5; i++) {
615 msleep(20);
616 if (cxusb_i2c_xfer(&adap->dev->i2c_adap, &msg, 1) != 1)
617 goto no_IR;
618 if (ircode[0] == 0 && ircode[1] == 0)
619 continue;
620 if (ircode[2] + ircode[3] != 0xff) {
621no_IR:
622 adap->dev->props.rc_key_map = NULL;
623 info("No IR receiver detected on this device.");
624 break;
625 }
626 }
627
628 return 0;
629}
630
631static int cxusb_nano2_frontend_attach(struct dvb_usb_adapter *adap)
632{
633 if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
634 err("set interface failed");
635
636 cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
637
638 /* reset the tuner and demodulator */
639 cxusb_bluebird_gpio_rw(adap->dev, 0x04, 0);
640 cxusb_bluebird_gpio_pulse(adap->dev, 0x01, 1);
641 cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
642
643 if ((adap->fe = dvb_attach(zl10353_attach,
644 &cxusb_zl10353_xc3028_config,
645 &adap->dev->i2c_adap)) != NULL)
646 return 0;
647
648 if ((adap->fe = dvb_attach(mt352_attach,
649 &cxusb_mt352_xc3028_config,
650 &adap->dev->i2c_adap)) != NULL)
651 return 0;
652
653 return -EIO;
654}
655
656/*
657 * DViCO has shipped two devices with the same USB ID, but only one of them
658 * needs a firmware download. Check the device class details to see if they
659 * have non-default values to decide whether the device is actually cold or
660 * not, and forget a match if it turns out we selected the wrong device.
661 */
662static int bluebird_fx2_identify_state(struct usb_device *udev,
663 struct dvb_usb_device_properties *props,
664 struct dvb_usb_device_description **desc,
665 int *cold)
666{
667 int wascold = *cold;
668
669 *cold = udev->descriptor.bDeviceClass == 0xff &&
670 udev->descriptor.bDeviceSubClass == 0xff &&
671 udev->descriptor.bDeviceProtocol == 0xff;
672
673 if (*cold && !wascold)
674 *desc = NULL;
675
676 return 0;
677}
678
450/* 679/*
451 * DViCO bluebird firmware needs the "warm" product ID to be patched into the 680 * DViCO bluebird firmware needs the "warm" product ID to be patched into the
452 * firmware file before download. 681 * firmware file before download.
453 */ 682 */
454 683
455#define BLUEBIRD_01_ID_OFFSET 6638 684static const int dvico_firmware_id_offsets[] = { 6638, 3204 };
456static int bluebird_patch_dvico_firmware_download(struct usb_device *udev, 685static int bluebird_patch_dvico_firmware_download(struct usb_device *udev,
457 const struct firmware *fw) 686 const struct firmware *fw)
458{ 687{
459 if (fw->size < BLUEBIRD_01_ID_OFFSET + 4) 688 int pos;
460 return -EINVAL; 689
690 for (pos = 0; pos < ARRAY_SIZE(dvico_firmware_id_offsets); pos++) {
691 int idoff = dvico_firmware_id_offsets[pos];
461 692
462 if (fw->data[BLUEBIRD_01_ID_OFFSET] == (USB_VID_DVICO & 0xff) && 693 if (fw->size < idoff + 4)
463 fw->data[BLUEBIRD_01_ID_OFFSET + 1] == USB_VID_DVICO >> 8) { 694 continue;
464 695
465 fw->data[BLUEBIRD_01_ID_OFFSET + 2] = 696 if (fw->data[idoff] == (USB_VID_DVICO & 0xff) &&
466 le16_to_cpu(udev->descriptor.idProduct) + 1; 697 fw->data[idoff + 1] == USB_VID_DVICO >> 8) {
467 fw->data[BLUEBIRD_01_ID_OFFSET + 3] = 698 fw->data[idoff + 2] =
468 le16_to_cpu(udev->descriptor.idProduct) >> 8; 699 le16_to_cpu(udev->descriptor.idProduct) + 1;
700 fw->data[idoff + 3] =
701 le16_to_cpu(udev->descriptor.idProduct) >> 8;
469 702
470 return usb_cypress_load_firmware(udev, fw, CYPRESS_FX2); 703 return usb_cypress_load_firmware(udev, fw, CYPRESS_FX2);
704 }
471 } 705 }
472 706
473 return -EINVAL; 707 return -EINVAL;
@@ -479,6 +713,9 @@ static struct dvb_usb_device_properties cxusb_bluebird_lgh064f_properties;
479static struct dvb_usb_device_properties cxusb_bluebird_dee1601_properties; 713static struct dvb_usb_device_properties cxusb_bluebird_dee1601_properties;
480static struct dvb_usb_device_properties cxusb_bluebird_lgz201_properties; 714static struct dvb_usb_device_properties cxusb_bluebird_lgz201_properties;
481static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties; 715static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties;
716static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_properties;
717static struct dvb_usb_device_properties cxusb_bluebird_nano2_properties;
718static struct dvb_usb_device_properties cxusb_bluebird_nano2_needsfirmware_properties;
482 719
483static int cxusb_probe(struct usb_interface *intf, 720static int cxusb_probe(struct usb_interface *intf,
484 const struct usb_device_id *id) 721 const struct usb_device_id *id)
@@ -487,7 +724,10 @@ static int cxusb_probe(struct usb_interface *intf,
487 dvb_usb_device_init(intf,&cxusb_bluebird_lgh064f_properties,THIS_MODULE,NULL) == 0 || 724 dvb_usb_device_init(intf,&cxusb_bluebird_lgh064f_properties,THIS_MODULE,NULL) == 0 ||
488 dvb_usb_device_init(intf,&cxusb_bluebird_dee1601_properties,THIS_MODULE,NULL) == 0 || 725 dvb_usb_device_init(intf,&cxusb_bluebird_dee1601_properties,THIS_MODULE,NULL) == 0 ||
489 dvb_usb_device_init(intf,&cxusb_bluebird_lgz201_properties,THIS_MODULE,NULL) == 0 || 726 dvb_usb_device_init(intf,&cxusb_bluebird_lgz201_properties,THIS_MODULE,NULL) == 0 ||
490 dvb_usb_device_init(intf,&cxusb_bluebird_dtt7579_properties,THIS_MODULE,NULL) == 0) { 727 dvb_usb_device_init(intf,&cxusb_bluebird_dtt7579_properties,THIS_MODULE,NULL) == 0 ||
728 dvb_usb_device_init(intf,&cxusb_bluebird_dualdig4_properties,THIS_MODULE,NULL) == 0 ||
729 dvb_usb_device_init(intf,&cxusb_bluebird_nano2_properties,THIS_MODULE,NULL) == 0 ||
730 dvb_usb_device_init(intf,&cxusb_bluebird_nano2_needsfirmware_properties,THIS_MODULE,NULL) == 0) {
491 return 0; 731 return 0;
492 } 732 }
493 733
@@ -508,6 +748,9 @@ static struct usb_device_id cxusb_table [] = {
508 { USB_DEVICE(USB_VID_DVICO, USB_PID_DIGITALNOW_BLUEBIRD_DUAL_1_WARM) }, 748 { USB_DEVICE(USB_VID_DVICO, USB_PID_DIGITALNOW_BLUEBIRD_DUAL_1_WARM) },
509 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_2_COLD) }, 749 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_2_COLD) },
510 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_2_WARM) }, 750 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_2_WARM) },
751 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_4) },
752 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DVB_T_NANO_2) },
753 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DVB_T_NANO_2_NFW_WARM) },
511 {} /* Terminating entry */ 754 {} /* Terminating entry */
512}; 755};
513MODULE_DEVICE_TABLE (usb, cxusb_table); 756MODULE_DEVICE_TABLE (usb, cxusb_table);
@@ -766,6 +1009,151 @@ static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties = {
766 } 1009 }
767}; 1010};
768 1011
1012static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_properties = {
1013 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1014
1015 .usb_ctrl = CYPRESS_FX2,
1016
1017 .size_of_priv = sizeof(struct cxusb_state),
1018
1019 .num_adapters = 1,
1020 .adapter = {
1021 {
1022 .streaming_ctrl = cxusb_streaming_ctrl,
1023 .frontend_attach = cxusb_dualdig4_frontend_attach,
1024 .tuner_attach = cxusb_dvico_xc3028_tuner_attach,
1025 /* parameter for the MPEG2-data transfer */
1026 .stream = {
1027 .type = USB_BULK,
1028 .count = 5,
1029 .endpoint = 0x02,
1030 .u = {
1031 .bulk = {
1032 .buffersize = 8192,
1033 }
1034 }
1035 },
1036 },
1037 },
1038
1039 .power_ctrl = cxusb_power_ctrl,
1040
1041 .i2c_algo = &cxusb_i2c_algo,
1042
1043 .generic_bulk_ctrl_endpoint = 0x01,
1044
1045 .rc_interval = 100,
1046 .rc_key_map = dvico_mce_rc_keys,
1047 .rc_key_map_size = ARRAY_SIZE(dvico_mce_rc_keys),
1048 .rc_query = cxusb_bluebird2_rc_query,
1049
1050 .num_device_descs = 1,
1051 .devices = {
1052 { "DViCO FusionHDTV DVB-T Dual Digital 4",
1053 { NULL },
1054 { &cxusb_table[13], NULL },
1055 },
1056 }
1057};
1058
1059static struct dvb_usb_device_properties cxusb_bluebird_nano2_properties = {
1060 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1061
1062 .usb_ctrl = CYPRESS_FX2,
1063 .identify_state = bluebird_fx2_identify_state,
1064
1065 .size_of_priv = sizeof(struct cxusb_state),
1066
1067 .num_adapters = 1,
1068 .adapter = {
1069 {
1070 .streaming_ctrl = cxusb_streaming_ctrl,
1071 .frontend_attach = cxusb_nano2_frontend_attach,
1072 .tuner_attach = cxusb_dvico_xc3028_tuner_attach,
1073 /* parameter for the MPEG2-data transfer */
1074 .stream = {
1075 .type = USB_BULK,
1076 .count = 5,
1077 .endpoint = 0x02,
1078 .u = {
1079 .bulk = {
1080 .buffersize = 8192,
1081 }
1082 }
1083 },
1084 },
1085 },
1086
1087 .power_ctrl = cxusb_nano2_power_ctrl,
1088
1089 .i2c_algo = &cxusb_i2c_algo,
1090
1091 .generic_bulk_ctrl_endpoint = 0x01,
1092
1093 .rc_interval = 100,
1094 .rc_key_map = dvico_portable_rc_keys,
1095 .rc_key_map_size = ARRAY_SIZE(dvico_portable_rc_keys),
1096 .rc_query = cxusb_bluebird2_rc_query,
1097
1098 .num_device_descs = 1,
1099 .devices = {
1100 { "DViCO FusionHDTV DVB-T NANO2",
1101 { NULL },
1102 { &cxusb_table[14], NULL },
1103 },
1104 }
1105};
1106
1107static struct dvb_usb_device_properties cxusb_bluebird_nano2_needsfirmware_properties = {
1108 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1109
1110 .usb_ctrl = DEVICE_SPECIFIC,
1111 .firmware = "dvb-usb-bluebird-02.fw",
1112 .download_firmware = bluebird_patch_dvico_firmware_download,
1113 .identify_state = bluebird_fx2_identify_state,
1114
1115 .size_of_priv = sizeof(struct cxusb_state),
1116
1117 .num_adapters = 1,
1118 .adapter = {
1119 {
1120 .streaming_ctrl = cxusb_streaming_ctrl,
1121 .frontend_attach = cxusb_nano2_frontend_attach,
1122 .tuner_attach = cxusb_dvico_xc3028_tuner_attach,
1123 /* parameter for the MPEG2-data transfer */
1124 .stream = {
1125 .type = USB_BULK,
1126 .count = 5,
1127 .endpoint = 0x02,
1128 .u = {
1129 .bulk = {
1130 .buffersize = 8192,
1131 }
1132 }
1133 },
1134 },
1135 },
1136
1137 .power_ctrl = cxusb_nano2_power_ctrl,
1138
1139 .i2c_algo = &cxusb_i2c_algo,
1140
1141 .generic_bulk_ctrl_endpoint = 0x01,
1142
1143 .rc_interval = 100,
1144 .rc_key_map = dvico_portable_rc_keys,
1145 .rc_key_map_size = ARRAY_SIZE(dvico_portable_rc_keys),
1146 .rc_query = cxusb_rc_query,
1147
1148 .num_device_descs = 1,
1149 .devices = {
1150 { "DViCO FusionHDTV DVB-T NANO2 w/o firmware",
1151 { &cxusb_table[14], NULL },
1152 { &cxusb_table[15], NULL },
1153 },
1154 }
1155};
1156
769static struct usb_driver cxusb_driver = { 1157static struct usb_driver cxusb_driver = {
770 .name = "dvb_usb_cxusb", 1158 .name = "dvb_usb_cxusb",
771 .probe = cxusb_probe, 1159 .probe = cxusb_probe,
diff --git a/drivers/media/dvb/dvb-usb/cxusb.h b/drivers/media/dvb/dvb-usb/cxusb.h
index c8ef77554b00..4768a2c35517 100644
--- a/drivers/media/dvb/dvb-usb/cxusb.h
+++ b/drivers/media/dvb/dvb-usb/cxusb.h
@@ -4,12 +4,9 @@
4#define DVB_USB_LOG_PREFIX "cxusb" 4#define DVB_USB_LOG_PREFIX "cxusb"
5#include "dvb-usb.h" 5#include "dvb-usb.h"
6 6
7extern int dvb_usb_cxusb_debug;
8#define deb_info(args...) dprintk(dvb_usb_cxusb_debug,0x01,args)
9#define deb_i2c(args...) if (d->udev->descriptor.idVendor == USB_VID_MEDION) \
10 dprintk(dvb_usb_cxusb_debug,0x01,args)
11
12/* usb commands - some of it are guesses, don't have a reference yet */ 7/* usb commands - some of it are guesses, don't have a reference yet */
8#define CMD_BLUEBIRD_GPIO_RW 0x05
9
13#define CMD_I2C_WRITE 0x08 10#define CMD_I2C_WRITE 0x08
14#define CMD_I2C_READ 0x09 11#define CMD_I2C_READ 0x09
15 12
diff --git a/drivers/media/dvb/dvb-usb/dib0700_core.c b/drivers/media/dvb/dvb-usb/dib0700_core.c
index 3ea294eb96bd..c9857d5c6982 100644
--- a/drivers/media/dvb/dvb-usb/dib0700_core.c
+++ b/drivers/media/dvb/dvb-usb/dib0700_core.c
@@ -243,7 +243,7 @@ int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
243 u8 b[4]; 243 u8 b[4];
244 244
245 b[0] = REQUEST_ENABLE_VIDEO; 245 b[0] = REQUEST_ENABLE_VIDEO;
246 b[1] = 0x00; 246 b[1] = (onoff << 4) | 0x00; /* this bit gives a kind of command, rather than enabling something or not */
247 b[2] = (0x01 << 4); /* Master mode */ 247 b[2] = (0x01 << 4); /* Master mode */
248 b[3] = 0x00; 248 b[3] = 0x00;
249 249
@@ -256,9 +256,6 @@ int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
256 256
257 b[2] |= st->channel_state; 257 b[2] |= st->channel_state;
258 258
259 if (st->channel_state) /* if at least one channel is active */
260 b[1] = (0x01 << 4) | 0x00;
261
262 deb_info("data for streaming: %x %x\n",b[1],b[2]); 259 deb_info("data for streaming: %x %x\n",b[1],b[2]);
263 260
264 return dib0700_ctrl_wr(adap->dev, b, 4); 261 return dib0700_ctrl_wr(adap->dev, b, 4);
diff --git a/drivers/media/dvb/dvb-usb/dib0700_devices.c b/drivers/media/dvb/dvb-usb/dib0700_devices.c
index 58452b52002c..e7093826e975 100644
--- a/drivers/media/dvb/dvb-usb/dib0700_devices.c
+++ b/drivers/media/dvb/dvb-usb/dib0700_devices.c
@@ -94,12 +94,28 @@ static int bristol_frontend_attach(struct dvb_usb_adapter *adap)
94 (10 + adap->id) << 1, &bristol_dib3000mc_config[adap->id])) == NULL ? -ENODEV : 0; 94 (10 + adap->id) << 1, &bristol_dib3000mc_config[adap->id])) == NULL ? -ENODEV : 0;
95} 95}
96 96
97static int eeprom_read(struct i2c_adapter *adap,u8 adrs,u8 *pval)
98{
99 struct i2c_msg msg[2] = {
100 { .addr = 0x50, .flags = 0, .buf = &adrs, .len = 1 },
101 { .addr = 0x50, .flags = I2C_M_RD, .buf = pval, .len = 1 },
102 };
103 if (i2c_transfer(adap, msg, 2) != 2) return -EREMOTEIO;
104 return 0;
105}
106
97static int bristol_tuner_attach(struct dvb_usb_adapter *adap) 107static int bristol_tuner_attach(struct dvb_usb_adapter *adap)
98{ 108{
99 struct dib0700_state *st = adap->dev->priv; 109 struct i2c_adapter *prim_i2c = &adap->dev->i2c_adap;
100 struct i2c_adapter *tun_i2c = dib3000mc_get_tuner_i2c_master(adap->fe, 1); 110 struct i2c_adapter *tun_i2c = dib3000mc_get_tuner_i2c_master(adap->fe, 1);
101 return dvb_attach(mt2060_attach,adap->fe, tun_i2c, &bristol_mt2060_config[adap->id], 111 s8 a;
102 st->mt2060_if1[adap->id]) == NULL ? -ENODEV : 0; 112 int if1=1220;
113 if (adap->dev->udev->descriptor.idVendor == USB_VID_HAUPPAUGE &&
114 adap->dev->udev->descriptor.idProduct == USB_PID_HAUPPAUGE_NOVA_T_500_2) {
115 if (!eeprom_read(prim_i2c,0x59 + adap->id,&a)) if1=1220+a;
116 }
117 return dvb_attach(mt2060_attach,adap->fe, tun_i2c,&bristol_mt2060_config[adap->id],
118 if1) == NULL ? -ENODEV : 0;
103} 119}
104 120
105/* STK7700D: Pinnacle/Terratec/Hauppauge Dual DVB-T Diversity */ 121/* STK7700D: Pinnacle/Terratec/Hauppauge Dual DVB-T Diversity */
@@ -230,6 +246,27 @@ static struct mt2266_config stk7700d_mt2266_config[2] = {
230 } 246 }
231}; 247};
232 248
249static int stk7700P2_frontend_attach(struct dvb_usb_adapter *adap)
250{
251 if (adap->id == 0) {
252 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
253 msleep(10);
254 dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
255 dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
256 dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
257 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
258 msleep(10);
259 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
260 msleep(10);
261 dib7000p_i2c_enumeration(&adap->dev->i2c_adap,1,18,stk7700d_dib7000p_mt2266_config);
262 }
263
264 adap->fe = dvb_attach(dib7000p_attach, &adap->dev->i2c_adap,0x80+(adap->id << 1),
265 &stk7700d_dib7000p_mt2266_config[adap->id]);
266
267 return adap->fe == NULL ? -ENODEV : 0;
268}
269
233static int stk7700d_frontend_attach(struct dvb_usb_adapter *adap) 270static int stk7700d_frontend_attach(struct dvb_usb_adapter *adap)
234{ 271{
235 if (adap->id == 0) { 272 if (adap->id == 0) {
@@ -415,6 +452,35 @@ static struct dvb_usb_rc_key dib0700_rc_keys[] = {
415 { 0x1e, 0x38, KEY_YELLOW }, 452 { 0x1e, 0x38, KEY_YELLOW },
416 { 0x1e, 0x3b, KEY_GOTO }, 453 { 0x1e, 0x3b, KEY_GOTO },
417 { 0x1e, 0x3d, KEY_POWER }, 454 { 0x1e, 0x3d, KEY_POWER },
455
456 /* Key codes for the Leadtek Winfast DTV Dongle */
457 { 0x00, 0x42, KEY_POWER },
458 { 0x07, 0x7c, KEY_TUNER },
459 { 0x0f, 0x4e, KEY_PRINT }, /* PREVIEW */
460 { 0x08, 0x40, KEY_SCREEN }, /* full screen toggle*/
461 { 0x0f, 0x71, KEY_DOT }, /* frequency */
462 { 0x07, 0x43, KEY_0 },
463 { 0x0c, 0x41, KEY_1 },
464 { 0x04, 0x43, KEY_2 },
465 { 0x0b, 0x7f, KEY_3 },
466 { 0x0e, 0x41, KEY_4 },
467 { 0x06, 0x43, KEY_5 },
468 { 0x09, 0x7f, KEY_6 },
469 { 0x0d, 0x7e, KEY_7 },
470 { 0x05, 0x7c, KEY_8 },
471 { 0x0a, 0x40, KEY_9 },
472 { 0x0e, 0x4e, KEY_CLEAR },
473 { 0x04, 0x7c, KEY_CHANNEL }, /* show channel number */
474 { 0x0f, 0x41, KEY_LAST }, /* recall */
475 { 0x03, 0x42, KEY_MUTE },
476 { 0x06, 0x4c, KEY_RESERVED }, /* PIP button*/
477 { 0x01, 0x72, KEY_SHUFFLE }, /* SNAPSHOT */
478 { 0x0c, 0x4e, KEY_PLAYPAUSE }, /* TIMESHIFT */
479 { 0x0b, 0x70, KEY_RECORD },
480 { 0x03, 0x7d, KEY_VOLUMEUP },
481 { 0x01, 0x7d, KEY_VOLUMEDOWN },
482 { 0x02, 0x42, KEY_CHANNELUP },
483 { 0x00, 0x7d, KEY_CHANNELDOWN },
418}; 484};
419 485
420/* STK7700P: Hauppauge Nova-T Stick, AVerMedia Volar */ 486/* STK7700P: Hauppauge Nova-T Stick, AVerMedia Volar */
@@ -578,16 +644,22 @@ static struct mt2060_config stk7700p_mt2060_config = {
578 644
579static int stk7700p_tuner_attach(struct dvb_usb_adapter *adap) 645static int stk7700p_tuner_attach(struct dvb_usb_adapter *adap)
580{ 646{
647 struct i2c_adapter *prim_i2c = &adap->dev->i2c_adap;
581 struct dib0700_state *st = adap->dev->priv; 648 struct dib0700_state *st = adap->dev->priv;
582 struct i2c_adapter *tun_i2c; 649 struct i2c_adapter *tun_i2c;
583 650 s8 a;
651 int if1=1220;
652 if (adap->dev->udev->descriptor.idVendor == USB_VID_HAUPPAUGE &&
653 adap->dev->udev->descriptor.idProduct == USB_PID_HAUPPAUGE_NOVA_T_STICK) {
654 if (!eeprom_read(prim_i2c,0x58,&a)) if1=1220+a;
655 }
584 if (st->is_dib7000pc) 656 if (st->is_dib7000pc)
585 tun_i2c = dib7000p_get_i2c_master(adap->fe, DIBX000_I2C_INTERFACE_TUNER, 1); 657 tun_i2c = dib7000p_get_i2c_master(adap->fe, DIBX000_I2C_INTERFACE_TUNER, 1);
586 else 658 else
587 tun_i2c = dib7000m_get_i2c_master(adap->fe, DIBX000_I2C_INTERFACE_TUNER, 1); 659 tun_i2c = dib7000m_get_i2c_master(adap->fe, DIBX000_I2C_INTERFACE_TUNER, 1);
588 660
589 return dvb_attach(mt2060_attach, adap->fe, tun_i2c, &stk7700p_mt2060_config, 661 return dvb_attach(mt2060_attach, adap->fe, tun_i2c, &stk7700p_mt2060_config,
590 st->mt2060_if1[0]) == NULL ? -ENODEV : 0; 662 if1) == NULL ? -ENODEV : 0;
591} 663}
592 664
593/* DIB7070 generic */ 665/* DIB7070 generic */
@@ -709,6 +781,8 @@ static struct dib7000p_config dib7070p_dib7000p_config = {
709 .agc_config_count = 1, 781 .agc_config_count = 1,
710 .agc = &dib7070_agc_config, 782 .agc = &dib7070_agc_config,
711 .bw = &dib7070_bw_config_12_mhz, 783 .bw = &dib7070_bw_config_12_mhz,
784 .tuner_is_baseband = 1,
785 .spur_protect = 1,
712 786
713 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS, 787 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
714 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES, 788 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
@@ -748,6 +822,8 @@ static struct dib7000p_config stk7070pd_dib7000p_config[2] = {
748 .agc_config_count = 1, 822 .agc_config_count = 1,
749 .agc = &dib7070_agc_config, 823 .agc = &dib7070_agc_config,
750 .bw = &dib7070_bw_config_12_mhz, 824 .bw = &dib7070_bw_config_12_mhz,
825 .tuner_is_baseband = 1,
826 .spur_protect = 1,
751 827
752 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS, 828 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
753 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES, 829 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
@@ -760,6 +836,8 @@ static struct dib7000p_config stk7070pd_dib7000p_config[2] = {
760 .agc_config_count = 1, 836 .agc_config_count = 1,
761 .agc = &dib7070_agc_config, 837 .agc = &dib7070_agc_config,
762 .bw = &dib7070_bw_config_12_mhz, 838 .bw = &dib7070_bw_config_12_mhz,
839 .tuner_is_baseband = 1,
840 .spur_protect = 1,
763 841
764 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS, 842 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
765 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES, 843 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
@@ -821,6 +899,12 @@ struct usb_device_id dib0700_usb_id_table[] = {
821 { USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV_DUAL_DIVERSITY_DVB_T) }, 899 { USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV_DUAL_DIVERSITY_DVB_T) },
822 { USB_DEVICE(USB_VID_COMPRO, USB_PID_COMPRO_VIDEOMATE_U500_PC) }, 900 { USB_DEVICE(USB_VID_COMPRO, USB_PID_COMPRO_VIDEOMATE_U500_PC) },
823/* 20 */{ USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_EXPRESS) }, 901/* 20 */{ USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_EXPRESS) },
902 { USB_DEVICE(USB_VID_GIGABYTE, USB_PID_GIGABYTE_U7000) },
903 { USB_DEVICE(USB_VID_ULTIMA_ELECTRONIC, USB_PID_ARTEC_T14BR) },
904 { USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3000) },
905 { USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100) },
906/* 25 */ { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_STICK_3) },
907 { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_MYTV_T) },
824 { 0 } /* Terminating entry */ 908 { 0 } /* Terminating entry */
825}; 909};
826MODULE_DEVICE_TABLE(usb, dib0700_usb_id_table); 910MODULE_DEVICE_TABLE(usb, dib0700_usb_id_table);
@@ -862,7 +946,7 @@ struct dvb_usb_device_properties dib0700_devices[] = {
862 }, 946 },
863 }, 947 },
864 948
865 .num_device_descs = 7, 949 .num_device_descs = 8,
866 .devices = { 950 .devices = {
867 { "DiBcom STK7700P reference design", 951 { "DiBcom STK7700P reference design",
868 { &dib0700_usb_id_table[0], &dib0700_usb_id_table[1] }, 952 { &dib0700_usb_id_table[0], &dib0700_usb_id_table[1] },
@@ -891,6 +975,10 @@ struct dvb_usb_device_properties dib0700_devices[] = {
891 { "AVerMedia AVerTV DVB-T Express", 975 { "AVerMedia AVerTV DVB-T Express",
892 { &dib0700_usb_id_table[20] }, 976 { &dib0700_usb_id_table[20] },
893 { NULL }, 977 { NULL },
978 },
979 { "Gigabyte U7000",
980 { &dib0700_usb_id_table[21], NULL },
981 { NULL },
894 } 982 }
895 }, 983 },
896 984
@@ -961,7 +1049,7 @@ struct dvb_usb_device_properties dib0700_devices[] = {
961 { "DiBcom STK7700D reference design", 1049 { "DiBcom STK7700D reference design",
962 { &dib0700_usb_id_table[14], NULL }, 1050 { &dib0700_usb_id_table[14], NULL },
963 { NULL }, 1051 { NULL },
964 }, 1052 }
965 }, 1053 },
966 1054
967 .rc_interval = DEFAULT_RC_INTERVAL, 1055 .rc_interval = DEFAULT_RC_INTERVAL,
@@ -974,6 +1062,25 @@ struct dvb_usb_device_properties dib0700_devices[] = {
974 .num_adapters = 1, 1062 .num_adapters = 1,
975 .adapter = { 1063 .adapter = {
976 { 1064 {
1065 .frontend_attach = stk7700P2_frontend_attach,
1066 .tuner_attach = stk7700d_tuner_attach,
1067
1068 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
1069 },
1070 },
1071
1072 .num_device_descs = 1,
1073 .devices = {
1074 { "ASUS My Cinema U3000 Mini DVBT Tuner",
1075 { &dib0700_usb_id_table[23], NULL },
1076 { NULL },
1077 },
1078 }
1079 }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
1080
1081 .num_adapters = 1,
1082 .adapter = {
1083 {
977 .frontend_attach = stk7070p_frontend_attach, 1084 .frontend_attach = stk7070p_frontend_attach,
978 .tuner_attach = dib7070p_tuner_attach, 1085 .tuner_attach = dib7070p_tuner_attach,
979 1086
@@ -983,7 +1090,7 @@ struct dvb_usb_device_properties dib0700_devices[] = {
983 }, 1090 },
984 }, 1091 },
985 1092
986 .num_device_descs = 2, 1093 .num_device_descs = 6,
987 .devices = { 1094 .devices = {
988 { "DiBcom STK7070P reference design", 1095 { "DiBcom STK7070P reference design",
989 { &dib0700_usb_id_table[15], NULL }, 1096 { &dib0700_usb_id_table[15], NULL },
@@ -993,7 +1100,29 @@ struct dvb_usb_device_properties dib0700_devices[] = {
993 { &dib0700_usb_id_table[16], NULL }, 1100 { &dib0700_usb_id_table[16], NULL },
994 { NULL }, 1101 { NULL },
995 }, 1102 },
996 } 1103 { "Artec T14BR DVB-T",
1104 { &dib0700_usb_id_table[22], NULL },
1105 { NULL },
1106 },
1107 { "ASUS My Cinema U3100 Mini DVBT Tuner",
1108 { &dib0700_usb_id_table[24], NULL },
1109 { NULL },
1110 },
1111 { "Hauppauge Nova-T Stick",
1112 { &dib0700_usb_id_table[25], NULL },
1113 { NULL },
1114 },
1115 { "Hauppauge Nova-T MyTV.t",
1116 { &dib0700_usb_id_table[26], NULL },
1117 { NULL },
1118 },
1119 },
1120
1121 .rc_interval = DEFAULT_RC_INTERVAL,
1122 .rc_key_map = dib0700_rc_keys,
1123 .rc_key_map_size = ARRAY_SIZE(dib0700_rc_keys),
1124 .rc_query = dib0700_rc_query
1125
997 }, { DIB0700_DEFAULT_DEVICE_PROPERTIES, 1126 }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
998 1127
999 .num_adapters = 2, 1128 .num_adapters = 2,
@@ -1024,7 +1153,7 @@ struct dvb_usb_device_properties dib0700_devices[] = {
1024 { "Pinnacle PCTV Dual DVB-T Diversity Stick", 1153 { "Pinnacle PCTV Dual DVB-T Diversity Stick",
1025 { &dib0700_usb_id_table[18], NULL }, 1154 { &dib0700_usb_id_table[18], NULL },
1026 { NULL }, 1155 { NULL },
1027 }, 1156 }
1028 } 1157 }
1029 }, 1158 },
1030}; 1159};
diff --git a/drivers/media/dvb/dvb-usb/digitv.c b/drivers/media/dvb/dvb-usb/digitv.c
index bca1e0905739..3acbda4aa27e 100644
--- a/drivers/media/dvb/dvb-usb/digitv.c
+++ b/drivers/media/dvb/dvb-usb/digitv.c
@@ -17,9 +17,10 @@
17#include "nxt6000.h" 17#include "nxt6000.h"
18 18
19/* debug */ 19/* debug */
20int dvb_usb_digitv_debug; 20static int dvb_usb_digitv_debug;
21module_param_named(debug,dvb_usb_digitv_debug, int, 0644); 21module_param_named(debug,dvb_usb_digitv_debug, int, 0644);
22MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS); 22MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
23#define deb_rc(args...) dprintk(dvb_usb_digitv_debug,0x01,args)
23 24
24static int digitv_ctrl_msg(struct dvb_usb_device *d, 25static int digitv_ctrl_msg(struct dvb_usb_device *d,
25 u8 cmd, u8 vv, u8 *wbuf, int wlen, u8 *rbuf, int rlen) 26 u8 cmd, u8 vv, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
diff --git a/drivers/media/dvb/dvb-usb/digitv.h b/drivers/media/dvb/dvb-usb/digitv.h
index 8b43e3db8691..908c09f4966b 100644
--- a/drivers/media/dvb/dvb-usb/digitv.h
+++ b/drivers/media/dvb/dvb-usb/digitv.h
@@ -8,9 +8,6 @@ struct digitv_state {
8 int is_nxt6000; 8 int is_nxt6000;
9}; 9};
10 10
11extern int dvb_usb_digitv_debug;
12#define deb_rc(args...) dprintk(dvb_usb_digitv_debug,0x01,args)
13
14/* protocol (from usblogging and the SDK: 11/* protocol (from usblogging and the SDK:
15 * 12 *
16 * Always 7 bytes bulk message(s) for controlling 13 * Always 7 bytes bulk message(s) for controlling
diff --git a/drivers/media/dvb/dvb-usb/dvb-usb-ids.h b/drivers/media/dvb/dvb-usb/dvb-usb-ids.h
index 4fa3e895028a..aa4844ef875e 100644
--- a/drivers/media/dvb/dvb-usb/dvb-usb-ids.h
+++ b/drivers/media/dvb/dvb-usb/dvb-usb-ids.h
@@ -15,7 +15,9 @@
15#define USB_VID_ALCOR_MICRO 0x058f 15#define USB_VID_ALCOR_MICRO 0x058f
16#define USB_VID_ALINK 0x05e3 16#define USB_VID_ALINK 0x05e3
17#define USB_VID_ANCHOR 0x0547 17#define USB_VID_ANCHOR 0x0547
18#define USB_VID_ANSONIC 0x10b9
18#define USB_VID_ANUBIS_ELECTRONIC 0x10fd 19#define USB_VID_ANUBIS_ELECTRONIC 0x10fd
20#define USB_VID_ASUS 0x0b05
19#define USB_VID_AVERMEDIA 0x07ca 21#define USB_VID_AVERMEDIA 0x07ca
20#define USB_VID_COMPRO 0x185b 22#define USB_VID_COMPRO 0x185b
21#define USB_VID_COMPRO_UNK 0x145f 23#define USB_VID_COMPRO_UNK 0x145f
@@ -44,12 +46,16 @@
44#define USB_VID_ULTIMA_ELECTRONIC 0x05d8 46#define USB_VID_ULTIMA_ELECTRONIC 0x05d8
45#define USB_VID_UNIWILL 0x1584 47#define USB_VID_UNIWILL 0x1584
46#define USB_VID_WIDEVIEW 0x14aa 48#define USB_VID_WIDEVIEW 0x14aa
49/* dom : pour gigabyte u7000 */
50#define USB_VID_GIGABYTE 0x1044
51
47 52
48/* Product IDs */ 53/* Product IDs */
49#define USB_PID_ADSTECH_USB2_COLD 0xa333 54#define USB_PID_ADSTECH_USB2_COLD 0xa333
50#define USB_PID_ADSTECH_USB2_WARM 0xa334 55#define USB_PID_ADSTECH_USB2_WARM 0xa334
51#define USB_PID_AFATECH_AF9005 0x9020 56#define USB_PID_AFATECH_AF9005 0x9020
52#define USB_VID_ALINK_DTU 0xf170 57#define USB_VID_ALINK_DTU 0xf170
58#define USB_PID_ANSONIC_DVBT_USB 0x6000
53#define USB_PID_AVERMEDIA_DVBT_USB_COLD 0x0001 59#define USB_PID_AVERMEDIA_DVBT_USB_COLD 0x0001
54#define USB_PID_AVERMEDIA_DVBT_USB_WARM 0x0002 60#define USB_PID_AVERMEDIA_DVBT_USB_WARM 0x0002
55#define USB_PID_AVERMEDIA_DVBT_USB2_COLD 0xa800 61#define USB_PID_AVERMEDIA_DVBT_USB2_COLD 0xa800
@@ -69,6 +75,7 @@
69#define USB_PID_DIBCOM_STK7700P 0x1e14 75#define USB_PID_DIBCOM_STK7700P 0x1e14
70#define USB_PID_DIBCOM_STK7700P_PC 0x1e78 76#define USB_PID_DIBCOM_STK7700P_PC 0x1e78
71#define USB_PID_DIBCOM_STK7700D 0x1ef0 77#define USB_PID_DIBCOM_STK7700D 0x1ef0
78#define USB_PID_DIBCOM_STK7700_U7000 0x7001
72#define USB_PID_DIBCOM_STK7070P 0x1ebc 79#define USB_PID_DIBCOM_STK7070P 0x1ebc
73#define USB_PID_DIBCOM_STK7070PD 0x1ebe 80#define USB_PID_DIBCOM_STK7070PD 0x1ebe
74#define USB_PID_DIBCOM_ANCHOR_2135_COLD 0x2131 81#define USB_PID_DIBCOM_ANCHOR_2135_COLD 0x2131
@@ -99,6 +106,7 @@
99#define USB_PID_ULTIMA_TVBOX_USB2_WARM 0x810a 106#define USB_PID_ULTIMA_TVBOX_USB2_WARM 0x810a
100#define USB_PID_ARTEC_T14_COLD 0x810b 107#define USB_PID_ARTEC_T14_COLD 0x810b
101#define USB_PID_ARTEC_T14_WARM 0x810c 108#define USB_PID_ARTEC_T14_WARM 0x810c
109#define USB_PID_ARTEC_T14BR 0x810f
102#define USB_PID_ULTIMA_TVBOX_USB2_FX_COLD 0x8613 110#define USB_PID_ULTIMA_TVBOX_USB2_FX_COLD 0x8613
103#define USB_PID_ULTIMA_TVBOX_USB2_FX_WARM 0x1002 111#define USB_PID_ULTIMA_TVBOX_USB2_FX_WARM 0x1002
104#define USB_PID_UNK_HYPER_PALTEK_COLD 0x005e 112#define USB_PID_UNK_HYPER_PALTEK_COLD 0x005e
@@ -120,6 +128,8 @@
120#define USB_PID_HAUPPAUGE_NOVA_T_500_2 0x9950 128#define USB_PID_HAUPPAUGE_NOVA_T_500_2 0x9950
121#define USB_PID_HAUPPAUGE_NOVA_T_STICK 0x7050 129#define USB_PID_HAUPPAUGE_NOVA_T_STICK 0x7050
122#define USB_PID_HAUPPAUGE_NOVA_T_STICK_2 0x7060 130#define USB_PID_HAUPPAUGE_NOVA_T_STICK_2 0x7060
131#define USB_PID_HAUPPAUGE_NOVA_T_STICK_3 0x7070
132#define USB_PID_HAUPPAUGE_MYTV_T 0x7080
123#define USB_PID_HAUPPAUGE_NOVA_TD_STICK 0x9580 133#define USB_PID_HAUPPAUGE_NOVA_TD_STICK 0x9580
124#define USB_PID_AVERMEDIA_EXPRESS 0xb568 134#define USB_PID_AVERMEDIA_EXPRESS 0xb568
125#define USB_PID_AVERMEDIA_VOLAR 0xa807 135#define USB_PID_AVERMEDIA_VOLAR 0xa807
@@ -143,6 +153,9 @@
143#define USB_PID_DVICO_BLUEBIRD_DUAL_1_WARM 0xdb51 153#define USB_PID_DVICO_BLUEBIRD_DUAL_1_WARM 0xdb51
144#define USB_PID_DVICO_BLUEBIRD_DUAL_2_COLD 0xdb58 154#define USB_PID_DVICO_BLUEBIRD_DUAL_2_COLD 0xdb58
145#define USB_PID_DVICO_BLUEBIRD_DUAL_2_WARM 0xdb59 155#define USB_PID_DVICO_BLUEBIRD_DUAL_2_WARM 0xdb59
156#define USB_PID_DVICO_BLUEBIRD_DUAL_4 0xdb78
157#define USB_PID_DVICO_BLUEBIRD_DVB_T_NANO_2 0xdb70
158#define USB_PID_DVICO_BLUEBIRD_DVB_T_NANO_2_NFW_WARM 0xdb71
146#define USB_PID_DIGITALNOW_BLUEBIRD_DUAL_1_COLD 0xdb54 159#define USB_PID_DIGITALNOW_BLUEBIRD_DUAL_1_COLD 0xdb54
147#define USB_PID_DIGITALNOW_BLUEBIRD_DUAL_1_WARM 0xdb55 160#define USB_PID_DIGITALNOW_BLUEBIRD_DUAL_1_WARM 0xdb55
148#define USB_PID_MEDION_MD95700 0x0932 161#define USB_PID_MEDION_MD95700 0x0932
@@ -170,6 +183,9 @@
170#define USB_PID_OPERA1_WARM 0x3829 183#define USB_PID_OPERA1_WARM 0x3829
171#define USB_PID_LIFEVIEW_TV_WALKER_TWIN_COLD 0x0514 184#define USB_PID_LIFEVIEW_TV_WALKER_TWIN_COLD 0x0514
172#define USB_PID_LIFEVIEW_TV_WALKER_TWIN_WARM 0x0513 185#define USB_PID_LIFEVIEW_TV_WALKER_TWIN_WARM 0x0513
173 186/* dom pour gigabyte u7000 */
187#define USB_PID_GIGABYTE_U7000 0x7001
188#define USB_PID_ASUS_U3000 0x171f
189#define USB_PID_ASUS_U3100 0x173f
174 190
175#endif 191#endif
diff --git a/drivers/media/dvb/dvb-usb/gl861.c b/drivers/media/dvb/dvb-usb/gl861.c
index f01d99c1c43c..6b99d9f4d5b3 100644
--- a/drivers/media/dvb/dvb-usb/gl861.c
+++ b/drivers/media/dvb/dvb-usb/gl861.c
@@ -56,12 +56,12 @@ static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
56 struct dvb_usb_device *d = i2c_get_adapdata(adap); 56 struct dvb_usb_device *d = i2c_get_adapdata(adap);
57 int i; 57 int i;
58 58
59 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
60 return -EAGAIN;
61
62 if (num > 2) 59 if (num > 2)
63 return -EINVAL; 60 return -EINVAL;
64 61
62 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
63 return -EAGAIN;
64
65 for (i = 0; i < num; i++) { 65 for (i = 0; i < num; i++) {
66 /* write/read request */ 66 /* write/read request */
67 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) { 67 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
diff --git a/drivers/media/dvb/dvb-usb/gp8psk.c b/drivers/media/dvb/dvb-usb/gp8psk.c
index 92147ee3e14f..83e8535014c6 100644
--- a/drivers/media/dvb/dvb-usb/gp8psk.c
+++ b/drivers/media/dvb/dvb-usb/gp8psk.c
@@ -171,22 +171,6 @@ static int gp8psk_power_ctrl(struct dvb_usb_device *d, int onoff)
171 return 0; 171 return 0;
172} 172}
173 173
174int gp8psk_bcm4500_reload(struct dvb_usb_device *d)
175{
176 u8 buf;
177 int gp_product_id = le16_to_cpu(d->udev->descriptor.idProduct);
178 /* Turn off 8psk power */
179 if (gp8psk_usb_in_op(d, BOOT_8PSK, 0, 0, &buf, 1))
180 return -EINVAL;
181 /* Turn On 8psk power */
182 if (gp8psk_usb_in_op(d, BOOT_8PSK, 1, 0, &buf, 1))
183 return -EINVAL;
184 /* load BCM4500 firmware */
185 if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM)
186 if (gp8psk_load_bcm4500fw(d))
187 return EINVAL;
188 return 0;
189}
190 174
191static int gp8psk_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff) 175static int gp8psk_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
192{ 176{
diff --git a/drivers/media/dvb/dvb-usb/gp8psk.h b/drivers/media/dvb/dvb-usb/gp8psk.h
index e83a57506cfa..e5cd8149c23d 100644
--- a/drivers/media/dvb/dvb-usb/gp8psk.h
+++ b/drivers/media/dvb/dvb-usb/gp8psk.h
@@ -92,6 +92,5 @@ extern struct dvb_frontend * gp8psk_fe_attach(struct dvb_usb_device *d);
92extern int gp8psk_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen); 92extern int gp8psk_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen);
93extern int gp8psk_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value, 93extern int gp8psk_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
94 u16 index, u8 *b, int blen); 94 u16 index, u8 *b, int blen);
95extern int gp8psk_bcm4500_reload(struct dvb_usb_device *d);
96 95
97#endif 96#endif
diff --git a/drivers/media/dvb/dvb-usb/opera1.c b/drivers/media/dvb/dvb-usb/opera1.c
index d7c04951ceab..21935bf7059e 100644
--- a/drivers/media/dvb/dvb-usb/opera1.c
+++ b/drivers/media/dvb/dvb-usb/opera1.c
@@ -10,7 +10,9 @@
10* see Documentation/dvb/README.dvb-usb for more information 10* see Documentation/dvb/README.dvb-usb for more information
11*/ 11*/
12 12
13#include "opera1.h" 13#define DVB_USB_LOG_PREFIX "opera"
14
15#include "dvb-usb.h"
14#include "stv0299.h" 16#include "stv0299.h"
15 17
16#define OPERA_READ_MSG 0 18#define OPERA_READ_MSG 0
@@ -38,7 +40,7 @@ struct opera_rc_keys {
38 u32 event; 40 u32 event;
39}; 41};
40 42
41int dvb_usb_opera1_debug; 43static int dvb_usb_opera1_debug;
42module_param_named(debug, dvb_usb_opera1_debug, int, 0644); 44module_param_named(debug, dvb_usb_opera1_debug, int, 0644);
43MODULE_PARM_DESC(debug, 45MODULE_PARM_DESC(debug,
44 "set debugging level (1=info,xfer=2,pll=4,ts=8,err=16,rc=32,fw=64 (or-able))." 46 "set debugging level (1=info,xfer=2,pll=4,ts=8,err=16,rc=32,fw=64 (or-able))."
diff --git a/drivers/media/dvb/dvb-usb/opera1.h b/drivers/media/dvb/dvb-usb/opera1.h
deleted file mode 100644
index 53174427902d..000000000000
--- a/drivers/media/dvb/dvb-usb/opera1.h
+++ /dev/null
@@ -1,9 +0,0 @@
1#ifndef _OPERA1_H_
2#define _OPERA1_H_
3
4#define DVB_USB_LOG_PREFIX "opera"
5#include "dvb-usb.h"
6
7extern int dvb_usb_opera1_debug;
8#define deb_xfer(args...) dprintk(dvb_usb_opera1_debug,0x02,args)
9#endif
diff --git a/drivers/media/dvb/dvb-usb/vp702x.c b/drivers/media/dvb/dvb-usb/vp702x.c
index 16533b31a82d..e553c139ac44 100644
--- a/drivers/media/dvb/dvb-usb/vp702x.c
+++ b/drivers/media/dvb/dvb-usb/vp702x.c
@@ -56,7 +56,7 @@ int vp702x_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8
56 return ret; 56 return ret;
57} 57}
58 58
59int vp702x_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value, 59static int vp702x_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
60 u16 index, u8 *b, int blen) 60 u16 index, u8 *b, int blen)
61{ 61{
62 int ret; 62 int ret;
@@ -204,19 +204,6 @@ static int vp702x_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
204 return 0; 204 return 0;
205} 205}
206 206
207int vp702x_power_ctrl(struct dvb_usb_device *d, int onoff)
208{
209 struct vp702x_device_state *st = d->priv;
210
211 if (st->power_state == 0 && onoff)
212 vp702x_usb_out_op(d, SET_TUNER_POWER_REQ, 1, 7, NULL, 0);
213 else if (st->power_state == 1 && onoff == 0)
214 vp702x_usb_out_op(d, SET_TUNER_POWER_REQ, 0, 7, NULL, 0);
215
216 st->power_state = onoff;
217
218 return 0;
219}
220 207
221static int vp702x_read_mac_addr(struct dvb_usb_device *d,u8 mac[6]) 208static int vp702x_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
222{ 209{
diff --git a/drivers/media/dvb/dvb-usb/vp702x.h b/drivers/media/dvb/dvb-usb/vp702x.h
index 25a9dee4c824..c2f97f96c21f 100644
--- a/drivers/media/dvb/dvb-usb/vp702x.h
+++ b/drivers/media/dvb/dvb-usb/vp702x.h
@@ -102,7 +102,5 @@ extern struct dvb_frontend * vp702x_fe_attach(struct dvb_usb_device *d);
102 102
103extern int vp702x_usb_inout_op(struct dvb_usb_device *d, u8 *o, int olen, u8 *i, int ilen, int msec); 103extern int vp702x_usb_inout_op(struct dvb_usb_device *d, u8 *o, int olen, u8 *i, int ilen, int msec);
104extern int vp702x_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen); 104extern int vp702x_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen);
105extern int vp702x_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen);
106extern int vp702x_power_ctrl(struct dvb_usb_device *d, int onoff);
107 105
108#endif 106#endif
diff --git a/drivers/media/dvb/dvb-usb/vp7045.c b/drivers/media/dvb/dvb-usb/vp7045.c
index 5bbd2d5192f0..c172babf59bb 100644
--- a/drivers/media/dvb/dvb-usb/vp7045.c
+++ b/drivers/media/dvb/dvb-usb/vp7045.c
@@ -15,9 +15,12 @@
15#include "vp7045.h" 15#include "vp7045.h"
16 16
17/* debug */ 17/* debug */
18int dvb_usb_vp7045_debug; 18static int dvb_usb_vp7045_debug;
19module_param_named(debug,dvb_usb_vp7045_debug, int, 0644); 19module_param_named(debug,dvb_usb_vp7045_debug, int, 0644);
20MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS); 20MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS);
21#define deb_info(args...) dprintk(dvb_usb_vp7045_debug,0x01,args)
22#define deb_xfer(args...) dprintk(dvb_usb_vp7045_debug,0x02,args)
23#define deb_rc(args...) dprintk(dvb_usb_vp7045_debug,0x04,args)
21 24
22int vp7045_usb_op(struct dvb_usb_device *d, u8 cmd, u8 *out, int outlen, u8 *in, int inlen, int msec) 25int vp7045_usb_op(struct dvb_usb_device *d, u8 cmd, u8 *out, int outlen, u8 *in, int inlen, int msec)
23{ 26{
diff --git a/drivers/media/dvb/dvb-usb/vp7045.h b/drivers/media/dvb/dvb-usb/vp7045.h
index 9ce21a20fa86..969688f85267 100644
--- a/drivers/media/dvb/dvb-usb/vp7045.h
+++ b/drivers/media/dvb/dvb-usb/vp7045.h
@@ -17,11 +17,6 @@
17#define DVB_USB_LOG_PREFIX "vp7045" 17#define DVB_USB_LOG_PREFIX "vp7045"
18#include "dvb-usb.h" 18#include "dvb-usb.h"
19 19
20extern int dvb_usb_vp7045_debug;
21#define deb_info(args...) dprintk(dvb_usb_vp7045_debug,0x01,args)
22#define deb_xfer(args...) dprintk(dvb_usb_vp7045_debug,0x02,args)
23#define deb_rc(args...) dprintk(dvb_usb_vp7045_debug,0x04,args)
24
25/* vp7045 commands */ 20/* vp7045 commands */
26 21
27/* Twinhan Vendor requests */ 22/* Twinhan Vendor requests */
diff --git a/drivers/media/dvb/frontends/Kconfig b/drivers/media/dvb/frontends/Kconfig
index 59b9ed1f1aec..9ad86ce4a4e5 100644
--- a/drivers/media/dvb/frontends/Kconfig
+++ b/drivers/media/dvb/frontends/Kconfig
@@ -316,6 +316,13 @@ config DVB_TDA827X
316 help 316 help
317 A DVB-T silicon tuner module. Say Y when you want to support this tuner. 317 A DVB-T silicon tuner module. Say Y when you want to support this tuner.
318 318
319config DVB_TDA18271
320 tristate "NXP TDA18271 silicon tuner"
321 depends on I2C
322 default m if DVB_FE_CUSTOMISE
323 help
324 A silicon tuner module. Say Y when you want to support this tuner.
325
319config DVB_TUNER_QT1010 326config DVB_TUNER_QT1010
320 tristate "Quantek QT1010 silicon tuner" 327 tristate "Quantek QT1010 silicon tuner"
321 depends on DVB_CORE && I2C 328 depends on DVB_CORE && I2C
@@ -353,6 +360,15 @@ config DVB_TUNER_DIB0070
353 This device is only used inside a SiP called togther with a 360 This device is only used inside a SiP called togther with a
354 demodulator for now. 361 demodulator for now.
355 362
363config DVB_TUNER_XC5000
364 tristate "Xceive XC5000 silicon tuner"
365 depends on I2C
366 default m if DVB_FE_CUSTOMISE
367 help
368 A driver for the silicon tuner XC5000 from Xceive.
369 This device is only used inside a SiP called togther with a
370 demodulator for now.
371
356comment "Miscellaneous devices" 372comment "Miscellaneous devices"
357 depends on DVB_CORE 373 depends on DVB_CORE
358 374
diff --git a/drivers/media/dvb/frontends/Makefile b/drivers/media/dvb/frontends/Makefile
index 4b8ad1f132aa..16bd107ebd32 100644
--- a/drivers/media/dvb/frontends/Makefile
+++ b/drivers/media/dvb/frontends/Makefile
@@ -3,6 +3,9 @@
3# 3#
4 4
5EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core/ 5EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core/
6EXTRA_CFLAGS += -Idrivers/media/video/
7
8tda18271-objs := tda18271-tables.o tda18271-common.o tda18271-fe.o
6 9
7obj-$(CONFIG_DVB_PLL) += dvb-pll.o 10obj-$(CONFIG_DVB_PLL) += dvb-pll.o
8obj-$(CONFIG_DVB_STV0299) += stv0299.o 11obj-$(CONFIG_DVB_STV0299) += stv0299.o
@@ -39,6 +42,7 @@ obj-$(CONFIG_DVB_ISL6421) += isl6421.o
39obj-$(CONFIG_DVB_TDA10086) += tda10086.o 42obj-$(CONFIG_DVB_TDA10086) += tda10086.o
40obj-$(CONFIG_DVB_TDA826X) += tda826x.o 43obj-$(CONFIG_DVB_TDA826X) += tda826x.o
41obj-$(CONFIG_DVB_TDA827X) += tda827x.o 44obj-$(CONFIG_DVB_TDA827X) += tda827x.o
45obj-$(CONFIG_DVB_TDA18271) += tda18271.o
42obj-$(CONFIG_DVB_TUNER_MT2060) += mt2060.o 46obj-$(CONFIG_DVB_TUNER_MT2060) += mt2060.o
43obj-$(CONFIG_DVB_TUNER_MT2266) += mt2266.o 47obj-$(CONFIG_DVB_TUNER_MT2266) += mt2266.o
44obj-$(CONFIG_DVB_TUNER_DIB0070) += dib0070.o 48obj-$(CONFIG_DVB_TUNER_DIB0070) += dib0070.o
@@ -46,3 +50,4 @@ obj-$(CONFIG_DVB_TUNER_QT1010) += qt1010.o
46obj-$(CONFIG_DVB_TUA6100) += tua6100.o 50obj-$(CONFIG_DVB_TUA6100) += tua6100.o
47obj-$(CONFIG_DVB_TUNER_MT2131) += mt2131.o 51obj-$(CONFIG_DVB_TUNER_MT2131) += mt2131.o
48obj-$(CONFIG_DVB_S5H1409) += s5h1409.o 52obj-$(CONFIG_DVB_S5H1409) += s5h1409.o
53obj-$(CONFIG_DVB_TUNER_XC5000) += xc5000.o
diff --git a/drivers/media/dvb/frontends/dib0070.c b/drivers/media/dvb/frontends/dib0070.c
index 481eaa684157..fe895bf7b18f 100644
--- a/drivers/media/dvb/frontends/dib0070.c
+++ b/drivers/media/dvb/frontends/dib0070.c
@@ -434,9 +434,14 @@ static u16 dib0070_p1f_defaults[] =
434 0, 434 0,
435}; 435};
436 436
437static void dib0070_wbd_calibration(struct dib0070_state *state) 437static void dib0070_wbd_calibration(struct dvb_frontend *fe)
438{ 438{
439 u16 wbd_offs; 439 u16 wbd_offs;
440 struct dib0070_state *state = fe->tuner_priv;
441
442 if (state->cfg->sleep)
443 state->cfg->sleep(fe, 0);
444
440 dib0070_write_reg(state, 0x0f, 0x6d81); 445 dib0070_write_reg(state, 0x0f, 0x6d81);
441 dib0070_write_reg(state, 0x20, 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001); 446 dib0070_write_reg(state, 0x20, 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001);
442 msleep(9); 447 msleep(9);
@@ -444,6 +449,10 @@ static void dib0070_wbd_calibration(struct dib0070_state *state)
444 dib0070_write_reg(state, 0x20, 0); 449 dib0070_write_reg(state, 0x20, 0);
445 state->wbd_ff_offset = ((wbd_offs * 8 * 18 / 33 + 1) / 2); 450 state->wbd_ff_offset = ((wbd_offs * 8 * 18 / 33 + 1) / 2);
446 dprintk( "WBDStart = %d (Vargen) - FF = %hd", (u32) wbd_offs * 1800/1024, state->wbd_ff_offset); 451 dprintk( "WBDStart = %d (Vargen) - FF = %hd", (u32) wbd_offs * 1800/1024, state->wbd_ff_offset);
452
453 if (state->cfg->sleep)
454 state->cfg->sleep(fe, 1);
455
447} 456}
448 457
449u16 dib0070_wbd_offset(struct dvb_frontend *fe) 458u16 dib0070_wbd_offset(struct dvb_frontend *fe)
@@ -560,7 +569,7 @@ struct dvb_frontend * dib0070_attach(struct dvb_frontend *fe, struct i2c_adapter
560 if (dib0070_reset(state) != 0) 569 if (dib0070_reset(state) != 0)
561 goto free_mem; 570 goto free_mem;
562 571
563 dib0070_wbd_calibration(state); 572 dib0070_wbd_calibration(fe);
564 573
565 printk(KERN_INFO "DiB0070: successfully identified\n"); 574 printk(KERN_INFO "DiB0070: successfully identified\n");
566 memcpy(&fe->ops.tuner_ops, &dib0070_ops, sizeof(struct dvb_tuner_ops)); 575 memcpy(&fe->ops.tuner_ops, &dib0070_ops, sizeof(struct dvb_tuner_ops));
diff --git a/drivers/media/dvb/frontends/dib3000mc.c b/drivers/media/dvb/frontends/dib3000mc.c
index edae0be063f5..fa851601e7d4 100644
--- a/drivers/media/dvb/frontends/dib3000mc.c
+++ b/drivers/media/dvb/frontends/dib3000mc.c
@@ -684,6 +684,9 @@ static int dib3000mc_set_frontend(struct dvb_frontend* fe,
684 struct dvb_frontend_parameters *fep) 684 struct dvb_frontend_parameters *fep)
685{ 685{
686 struct dib3000mc_state *state = fe->demodulator_priv; 686 struct dib3000mc_state *state = fe->demodulator_priv;
687 int ret;
688
689 dib3000mc_set_output_mode(state, OUTMODE_HIGH_Z);
687 690
688 state->current_bandwidth = fep->u.ofdm.bandwidth; 691 state->current_bandwidth = fep->u.ofdm.bandwidth;
689 dib3000mc_set_bandwidth(state, BANDWIDTH_TO_KHZ(fep->u.ofdm.bandwidth)); 692 dib3000mc_set_bandwidth(state, BANDWIDTH_TO_KHZ(fep->u.ofdm.bandwidth));
@@ -700,7 +703,7 @@ static int dib3000mc_set_frontend(struct dvb_frontend* fe,
700 fep->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO || 703 fep->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO ||
701 fep->u.ofdm.constellation == QAM_AUTO || 704 fep->u.ofdm.constellation == QAM_AUTO ||
702 fep->u.ofdm.code_rate_HP == FEC_AUTO) { 705 fep->u.ofdm.code_rate_HP == FEC_AUTO) {
703 int i = 100, found; 706 int i = 1000, found;
704 707
705 dib3000mc_autosearch_start(fe, fep); 708 dib3000mc_autosearch_start(fe, fep);
706 do { 709 do {
@@ -715,10 +718,11 @@ static int dib3000mc_set_frontend(struct dvb_frontend* fe,
715 dib3000mc_get_frontend(fe, fep); 718 dib3000mc_get_frontend(fe, fep);
716 } 719 }
717 720
721 ret = dib3000mc_tune(fe, fep);
722
718 /* make this a config parameter */ 723 /* make this a config parameter */
719 dib3000mc_set_output_mode(state, OUTMODE_MPEG2_FIFO); 724 dib3000mc_set_output_mode(state, OUTMODE_MPEG2_FIFO);
720 725 return ret;
721 return dib3000mc_tune(fe, fep);
722} 726}
723 727
724static int dib3000mc_read_status(struct dvb_frontend *fe, fe_status_t *stat) 728static int dib3000mc_read_status(struct dvb_frontend *fe, fe_status_t *stat)
diff --git a/drivers/media/dvb/frontends/dib7000m.c b/drivers/media/dvb/frontends/dib7000m.c
index fb18441a8c57..5f1375e30dfc 100644
--- a/drivers/media/dvb/frontends/dib7000m.c
+++ b/drivers/media/dvb/frontends/dib7000m.c
@@ -1171,7 +1171,9 @@ static int dib7000m_set_frontend(struct dvb_frontend* fe,
1171 struct dvb_frontend_parameters *fep) 1171 struct dvb_frontend_parameters *fep)
1172{ 1172{
1173 struct dib7000m_state *state = fe->demodulator_priv; 1173 struct dib7000m_state *state = fe->demodulator_priv;
1174 int time; 1174 int time, ret;
1175
1176 dib7000m_set_output_mode(state, OUTMODE_HIGH_Z);
1175 1177
1176 state->current_bandwidth = fep->u.ofdm.bandwidth; 1178 state->current_bandwidth = fep->u.ofdm.bandwidth;
1177 dib7000m_set_bandwidth(state, BANDWIDTH_TO_KHZ(fep->u.ofdm.bandwidth)); 1179 dib7000m_set_bandwidth(state, BANDWIDTH_TO_KHZ(fep->u.ofdm.bandwidth));
@@ -1206,10 +1208,11 @@ static int dib7000m_set_frontend(struct dvb_frontend* fe,
1206 dib7000m_get_frontend(fe, fep); 1208 dib7000m_get_frontend(fe, fep);
1207 } 1209 }
1208 1210
1211 ret = dib7000m_tune(fe, fep);
1212
1209 /* make this a config parameter */ 1213 /* make this a config parameter */
1210 dib7000m_set_output_mode(state, OUTMODE_MPEG2_FIFO); 1214 dib7000m_set_output_mode(state, OUTMODE_MPEG2_FIFO);
1211 1215 return ret;
1212 return dib7000m_tune(fe, fep);
1213} 1216}
1214 1217
1215static int dib7000m_read_status(struct dvb_frontend *fe, fe_status_t *stat) 1218static int dib7000m_read_status(struct dvb_frontend *fe, fe_status_t *stat)
diff --git a/drivers/media/dvb/frontends/dib7000p.c b/drivers/media/dvb/frontends/dib7000p.c
index f45bcfc51cf8..47c23e29753e 100644
--- a/drivers/media/dvb/frontends/dib7000p.c
+++ b/drivers/media/dvb/frontends/dib7000p.c
@@ -35,8 +35,8 @@ struct dib7000p_state {
35 35
36 u16 wbd_ref; 36 u16 wbd_ref;
37 37
38 u8 current_band; 38 u8 current_band;
39 fe_bandwidth_t current_bandwidth; 39 u32 current_bandwidth;
40 struct dibx000_agc_config *current_agc; 40 struct dibx000_agc_config *current_agc;
41 u32 timf; 41 u32 timf;
42 42
@@ -1074,7 +1074,7 @@ static int dib7000p_get_frontend(struct dvb_frontend* fe,
1074 1074
1075 fep->inversion = INVERSION_AUTO; 1075 fep->inversion = INVERSION_AUTO;
1076 1076
1077 fep->u.ofdm.bandwidth = state->current_bandwidth; 1077 fep->u.ofdm.bandwidth = BANDWIDTH_TO_INDEX(state->current_bandwidth);
1078 1078
1079 switch ((tps >> 8) & 0x3) { 1079 switch ((tps >> 8) & 0x3) {
1080 case 0: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K; break; 1080 case 0: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K; break;
@@ -1128,12 +1128,11 @@ static int dib7000p_set_frontend(struct dvb_frontend* fe,
1128 struct dvb_frontend_parameters *fep) 1128 struct dvb_frontend_parameters *fep)
1129{ 1129{
1130 struct dib7000p_state *state = fe->demodulator_priv; 1130 struct dib7000p_state *state = fe->demodulator_priv;
1131 int time; 1131 int time, ret;
1132 1132
1133 state->current_bandwidth = fep->u.ofdm.bandwidth; 1133 dib7000p_set_output_mode(state, OUTMODE_HIGH_Z);
1134 dib7000p_set_bandwidth(state, BANDWIDTH_TO_KHZ(fep->u.ofdm.bandwidth));
1135 1134
1136 /* maybe the parameter has been changed */ 1135 /* maybe the parameter has been changed */
1137 state->sfn_workaround_active = buggy_sfn_workaround; 1136 state->sfn_workaround_active = buggy_sfn_workaround;
1138 1137
1139 if (fe->ops.tuner_ops.set_params) 1138 if (fe->ops.tuner_ops.set_params)
@@ -1166,10 +1165,11 @@ static int dib7000p_set_frontend(struct dvb_frontend* fe,
1166 dib7000p_get_frontend(fe, fep); 1165 dib7000p_get_frontend(fe, fep);
1167 } 1166 }
1168 1167
1168 ret = dib7000p_tune(fe, fep);
1169
1169 /* make this a config parameter */ 1170 /* make this a config parameter */
1170 dib7000p_set_output_mode(state, OUTMODE_MPEG2_FIFO); 1171 dib7000p_set_output_mode(state, OUTMODE_MPEG2_FIFO);
1171 1172 return ret;
1172 return dib7000p_tune(fe, fep);
1173} 1173}
1174 1174
1175static int dib7000p_read_status(struct dvb_frontend *fe, fe_status_t *stat) 1175static int dib7000p_read_status(struct dvb_frontend *fe, fe_status_t *stat)
diff --git a/drivers/media/dvb/frontends/dibx000_common.h b/drivers/media/dvb/frontends/dibx000_common.h
index 5e17275afd25..84e4d5362922 100644
--- a/drivers/media/dvb/frontends/dibx000_common.h
+++ b/drivers/media/dvb/frontends/dibx000_common.h
@@ -128,6 +128,11 @@ enum dibx000_adc_states {
128 (v) == BANDWIDTH_7_MHZ ? 7000 : \ 128 (v) == BANDWIDTH_7_MHZ ? 7000 : \
129 (v) == BANDWIDTH_6_MHZ ? 6000 : 8000 ) 129 (v) == BANDWIDTH_6_MHZ ? 6000 : 8000 )
130 130
131#define BANDWIDTH_TO_INDEX(v) ( \
132 (v) == 8000 ? BANDWIDTH_8_MHZ : \
133 (v) == 7000 ? BANDWIDTH_7_MHZ : \
134 (v) == 6000 ? BANDWIDTH_6_MHZ : BANDWIDTH_8_MHZ )
135
131/* Chip output mode. */ 136/* Chip output mode. */
132#define OUTMODE_HIGH_Z 0 137#define OUTMODE_HIGH_Z 0
133#define OUTMODE_MPEG2_PAR_GATED_CLK 1 138#define OUTMODE_MPEG2_PAR_GATED_CLK 1
diff --git a/drivers/media/dvb/frontends/mt2266.c b/drivers/media/dvb/frontends/mt2266.c
index 03fe8265745f..54b18f94b14b 100644
--- a/drivers/media/dvb/frontends/mt2266.c
+++ b/drivers/media/dvb/frontends/mt2266.c
@@ -38,8 +38,12 @@ struct mt2266_priv {
38 38
39 u32 frequency; 39 u32 frequency;
40 u32 bandwidth; 40 u32 bandwidth;
41 u8 band;
41}; 42};
42 43
44#define MT2266_VHF 1
45#define MT2266_UHF 0
46
43/* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */ 47/* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
44 48
45static int debug; 49static int debug;
@@ -90,26 +94,30 @@ static int mt2266_writeregs(struct mt2266_priv *priv,u8 *buf, u8 len)
90} 94}
91 95
92// Initialisation sequences 96// Initialisation sequences
93static u8 mt2266_init1[] = { 97static u8 mt2266_init1[] = { REG_TUNE, 0x00, 0x00, 0x28,
94 REG_TUNE, 98 0x00, 0x52, 0x99, 0x3f };
95 0x00, 0x00, 0x28, 0x00, 0x52, 0x99, 0x3f };
96 99
97static u8 mt2266_init2[] = { 100static u8 mt2266_init2[] = {
98 0x17, 0x6d, 0x71, 0x61, 0xc0, 0xbf, 0xff, 0xdc, 0x00, 0x0a, 101 0x17, 0x6d, 0x71, 0x61, 0xc0, 0xbf, 0xff, 0xdc, 0x00, 0x0a, 0xd4,
99 0xd4, 0x03, 0x64, 0x64, 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14, 0x01, 0x01, 0x01, 0x01, 102 0x03, 0x64, 0x64, 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14,
100 0x01, 0x01, 0x7f, 0x5e, 0x3f, 0xff, 0xff, 0xff, 0x00, 0x77, 0x0f, 0x2d }; 103 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x7f, 0x5e, 0x3f, 0xff, 0xff,
104 0xff, 0x00, 0x77, 0x0f, 0x2d
105};
106
107static u8 mt2266_init_8mhz[] = { REG_BANDWIDTH, 0x22, 0x22, 0x22, 0x22,
108 0x22, 0x22, 0x22, 0x22 };
101 109
102static u8 mt2266_init_8mhz[] = { 110static u8 mt2266_init_7mhz[] = { REG_BANDWIDTH, 0x32, 0x32, 0x32, 0x32,
103 REG_BANDWIDTH, 111 0x32, 0x32, 0x32, 0x32 };
104 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 };
105 112
106static u8 mt2266_init_7mhz[] = { 113static u8 mt2266_init_6mhz[] = { REG_BANDWIDTH, 0xa7, 0xa7, 0xa7, 0xa7,
107 REG_BANDWIDTH, 114 0xa7, 0xa7, 0xa7, 0xa7 };
108 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32 };
109 115
110static u8 mt2266_init_6mhz[] = { 116static u8 mt2266_uhf[] = { 0x1d, 0xdc, 0x00, 0x0a, 0xd4, 0x03, 0x64, 0x64,
111 REG_BANDWIDTH, 117 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14 };
112 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7 }; 118
119static u8 mt2266_vhf[] = { 0x1d, 0xfe, 0x00, 0x00, 0xb4, 0x03, 0xa5, 0xa5,
120 0xa5, 0xa5, 0x82, 0xaa, 0xf1, 0x17, 0x80, 0x1f };
113 121
114#define FREF 30000 // Quartz oscillator 30 MHz 122#define FREF 30000 // Quartz oscillator 30 MHz
115 123
@@ -122,35 +130,78 @@ static int mt2266_set_params(struct dvb_frontend *fe, struct dvb_frontend_parame
122 u8 lnaband; 130 u8 lnaband;
123 u8 b[10]; 131 u8 b[10];
124 int i; 132 int i;
133 u8 band;
125 134
126 priv = fe->tuner_priv; 135 priv = fe->tuner_priv;
127 136
128 mt2266_writereg(priv,0x17,0x6d);
129 mt2266_writereg(priv,0x1c,0xff);
130
131 freq = params->frequency / 1000; // Hz -> kHz 137 freq = params->frequency / 1000; // Hz -> kHz
138 if (freq < 470000 && freq > 230000)
139 return -EINVAL; /* Gap between VHF and UHF bands */
132 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0; 140 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
133 priv->frequency = freq * 1000; 141 priv->frequency = freq * 1000;
134 tune=2 * freq * (8192/16) / (FREF/16); 142
135 143 tune = 2 * freq * (8192/16) / (FREF/16);
136 if (freq <= 495000) lnaband = 0xEE; else 144 band = (freq < 300000) ? MT2266_VHF : MT2266_UHF;
137 if (freq <= 525000) lnaband = 0xDD; else 145 if (band == MT2266_VHF)
138 if (freq <= 550000) lnaband = 0xCC; else 146 tune *= 2;
139 if (freq <= 580000) lnaband = 0xBB; else 147
140 if (freq <= 605000) lnaband = 0xAA; else 148 switch (params->u.ofdm.bandwidth) {
141 if (freq <= 630000) lnaband = 0x99; else 149 case BANDWIDTH_6_MHZ:
142 if (freq <= 655000) lnaband = 0x88; else 150 mt2266_writeregs(priv, mt2266_init_6mhz,
143 if (freq <= 685000) lnaband = 0x77; else 151 sizeof(mt2266_init_6mhz));
144 if (freq <= 710000) lnaband = 0x66; else 152 break;
145 if (freq <= 735000) lnaband = 0x55; else 153 case BANDWIDTH_7_MHZ:
146 if (freq <= 765000) lnaband = 0x44; else 154 mt2266_writeregs(priv, mt2266_init_7mhz,
147 if (freq <= 802000) lnaband = 0x33; else 155 sizeof(mt2266_init_7mhz));
148 if (freq <= 840000) lnaband = 0x22; else lnaband = 0x11; 156 break;
149 157 case BANDWIDTH_8_MHZ:
150 msleep(100); 158 default:
151 mt2266_writeregs(priv,(params->u.ofdm.bandwidth==BANDWIDTH_6_MHZ)?mt2266_init_6mhz: 159 mt2266_writeregs(priv, mt2266_init_8mhz,
152 (params->u.ofdm.bandwidth==BANDWIDTH_7_MHZ)?mt2266_init_7mhz: 160 sizeof(mt2266_init_8mhz));
153 mt2266_init_8mhz,sizeof(mt2266_init_8mhz)); 161 break;
162 }
163
164 if (band == MT2266_VHF && priv->band == MT2266_UHF) {
165 dprintk("Switch from UHF to VHF");
166 mt2266_writereg(priv, 0x05, 0x04);
167 mt2266_writereg(priv, 0x19, 0x61);
168 mt2266_writeregs(priv, mt2266_vhf, sizeof(mt2266_vhf));
169 } else if (band == MT2266_UHF && priv->band == MT2266_VHF) {
170 dprintk("Switch from VHF to UHF");
171 mt2266_writereg(priv, 0x05, 0x52);
172 mt2266_writereg(priv, 0x19, 0x61);
173 mt2266_writeregs(priv, mt2266_uhf, sizeof(mt2266_uhf));
174 }
175 msleep(10);
176
177 if (freq <= 495000)
178 lnaband = 0xEE;
179 else if (freq <= 525000)
180 lnaband = 0xDD;
181 else if (freq <= 550000)
182 lnaband = 0xCC;
183 else if (freq <= 580000)
184 lnaband = 0xBB;
185 else if (freq <= 605000)
186 lnaband = 0xAA;
187 else if (freq <= 630000)
188 lnaband = 0x99;
189 else if (freq <= 655000)
190 lnaband = 0x88;
191 else if (freq <= 685000)
192 lnaband = 0x77;
193 else if (freq <= 710000)
194 lnaband = 0x66;
195 else if (freq <= 735000)
196 lnaband = 0x55;
197 else if (freq <= 765000)
198 lnaband = 0x44;
199 else if (freq <= 802000)
200 lnaband = 0x33;
201 else if (freq <= 840000)
202 lnaband = 0x22;
203 else
204 lnaband = 0x11;
154 205
155 b[0] = REG_TUNE; 206 b[0] = REG_TUNE;
156 b[1] = (tune >> 8) & 0x1F; 207 b[1] = (tune >> 8) & 0x1F;
@@ -158,47 +209,54 @@ static int mt2266_set_params(struct dvb_frontend *fe, struct dvb_frontend_parame
158 b[3] = tune >> 13; 209 b[3] = tune >> 13;
159 mt2266_writeregs(priv,b,4); 210 mt2266_writeregs(priv,b,4);
160 211
161 dprintk("set_parms: tune=%d band=%d",(int)tune,(int)lnaband); 212 dprintk("set_parms: tune=%d band=%d %s",
162 dprintk("set_parms: [1..3]: %2x %2x %2x",(int)b[1],(int)b[2],(int)b[3]); 213 (int) tune, (int) lnaband,
163 214 (band == MT2266_UHF) ? "UHF" : "VHF");
164 b[0] = 0x05; 215 dprintk("set_parms: [1..3]: %2x %2x %2x",
165 b[1] = 0x62; 216 (int) b[1], (int) b[2], (int)b[3]);
166 b[2] = lnaband; 217
167 mt2266_writeregs(priv,b,3); 218 if (band == MT2266_UHF) {
219 b[0] = 0x05;
220 b[1] = (priv->band == MT2266_VHF) ? 0x52 : 0x62;
221 b[2] = lnaband;
222 mt2266_writeregs(priv, b, 3);
223 }
168 224
169 //Waits for pll lock or timeout 225 /* Wait for pll lock or timeout */
170 i = 0; 226 i = 0;
171 do { 227 do {
172 mt2266_readreg(priv,REG_LOCK,b); 228 mt2266_readreg(priv,REG_LOCK,b);
173 if ((b[0] & 0x40)==0x40) 229 if (b[0] & 0x40)
174 break; 230 break;
175 msleep(10); 231 msleep(10);
176 i++; 232 i++;
177 } while (i<10); 233 } while (i<10);
178 dprintk("Lock when i=%i",(int)i); 234 dprintk("Lock when i=%i",(int)i);
235
236 if (band == MT2266_UHF && priv->band == MT2266_VHF)
237 mt2266_writereg(priv, 0x05, 0x62);
238
239 priv->band = band;
240
179 return ret; 241 return ret;
180} 242}
181 243
182static void mt2266_calibrate(struct mt2266_priv *priv) 244static void mt2266_calibrate(struct mt2266_priv *priv)
183{ 245{
184 mt2266_writereg(priv,0x11,0x03); 246 mt2266_writereg(priv, 0x11, 0x03);
185 mt2266_writereg(priv,0x11,0x01); 247 mt2266_writereg(priv, 0x11, 0x01);
186 248 mt2266_writeregs(priv, mt2266_init1, sizeof(mt2266_init1));
187 mt2266_writeregs(priv,mt2266_init1,sizeof(mt2266_init1)); 249 mt2266_writeregs(priv, mt2266_init2, sizeof(mt2266_init2));
188 mt2266_writeregs(priv,mt2266_init2,sizeof(mt2266_init2)); 250 mt2266_writereg(priv, 0x33, 0x5e);
189 251 mt2266_writereg(priv, 0x10, 0x10);
190 mt2266_writereg(priv,0x33,0x5e); 252 mt2266_writereg(priv, 0x10, 0x00);
191 mt2266_writereg(priv,0x10,0x10); 253 mt2266_writeregs(priv, mt2266_init_8mhz, sizeof(mt2266_init_8mhz));
192 mt2266_writereg(priv,0x10,0x00);
193
194 mt2266_writeregs(priv,mt2266_init_8mhz,sizeof(mt2266_init_8mhz));
195
196 msleep(25); 254 msleep(25);
197 mt2266_writereg(priv,0x17,0x6d); 255 mt2266_writereg(priv, 0x17, 0x6d);
198 mt2266_writereg(priv,0x1c,0x00); 256 mt2266_writereg(priv, 0x1c, 0x00);
199 msleep(75); 257 msleep(75);
200 mt2266_writereg(priv,0x17,0x6d); 258 mt2266_writereg(priv, 0x17, 0x6d);
201 mt2266_writereg(priv,0x1c,0xff); 259 mt2266_writereg(priv, 0x1c, 0xff);
202} 260}
203 261
204static int mt2266_get_frequency(struct dvb_frontend *fe, u32 *frequency) 262static int mt2266_get_frequency(struct dvb_frontend *fe, u32 *frequency)
@@ -217,17 +275,22 @@ static int mt2266_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
217 275
218static int mt2266_init(struct dvb_frontend *fe) 276static int mt2266_init(struct dvb_frontend *fe)
219{ 277{
278 int ret;
220 struct mt2266_priv *priv = fe->tuner_priv; 279 struct mt2266_priv *priv = fe->tuner_priv;
221 mt2266_writereg(priv,0x17,0x6d); 280 ret = mt2266_writereg(priv, 0x17, 0x6d);
222 mt2266_writereg(priv,0x1c,0xff); 281 if (ret < 0)
282 return ret;
283 ret = mt2266_writereg(priv, 0x1c, 0xff);
284 if (ret < 0)
285 return ret;
223 return 0; 286 return 0;
224} 287}
225 288
226static int mt2266_sleep(struct dvb_frontend *fe) 289static int mt2266_sleep(struct dvb_frontend *fe)
227{ 290{
228 struct mt2266_priv *priv = fe->tuner_priv; 291 struct mt2266_priv *priv = fe->tuner_priv;
229 mt2266_writereg(priv,0x17,0x6d); 292 mt2266_writereg(priv, 0x17, 0x6d);
230 mt2266_writereg(priv,0x1c,0x00); 293 mt2266_writereg(priv, 0x1c, 0x00);
231 return 0; 294 return 0;
232} 295}
233 296
@@ -241,8 +304,8 @@ static int mt2266_release(struct dvb_frontend *fe)
241static const struct dvb_tuner_ops mt2266_tuner_ops = { 304static const struct dvb_tuner_ops mt2266_tuner_ops = {
242 .info = { 305 .info = {
243 .name = "Microtune MT2266", 306 .name = "Microtune MT2266",
244 .frequency_min = 470000000, 307 .frequency_min = 174000000,
245 .frequency_max = 860000000, 308 .frequency_max = 862000000,
246 .frequency_step = 50000, 309 .frequency_step = 50000,
247 }, 310 },
248 .release = mt2266_release, 311 .release = mt2266_release,
@@ -264,8 +327,9 @@ struct dvb_frontend * mt2266_attach(struct dvb_frontend *fe, struct i2c_adapter
264 327
265 priv->cfg = cfg; 328 priv->cfg = cfg;
266 priv->i2c = i2c; 329 priv->i2c = i2c;
330 priv->band = MT2266_UHF;
267 331
268 if (mt2266_readreg(priv,0,&id) != 0) { 332 if (mt2266_readreg(priv, 0, &id)) {
269 kfree(priv); 333 kfree(priv);
270 return NULL; 334 return NULL;
271 } 335 }
diff --git a/drivers/media/dvb/frontends/mt312.c b/drivers/media/dvb/frontends/mt312.c
index 0606b9a5b616..1638301fbd6e 100644
--- a/drivers/media/dvb/frontends/mt312.c
+++ b/drivers/media/dvb/frontends/mt312.c
@@ -37,9 +37,9 @@
37 37
38 38
39struct mt312_state { 39struct mt312_state {
40 struct i2c_adapter* i2c; 40 struct i2c_adapter *i2c;
41 /* configuration settings */ 41 /* configuration settings */
42 const struct mt312_config* config; 42 const struct mt312_config *config;
43 struct dvb_frontend frontend; 43 struct dvb_frontend frontend;
44 44
45 u8 id; 45 u8 id;
@@ -49,14 +49,15 @@ struct mt312_state {
49static int debug; 49static int debug;
50#define dprintk(args...) \ 50#define dprintk(args...) \
51 do { \ 51 do { \
52 if (debug) printk(KERN_DEBUG "mt312: " args); \ 52 if (debug) \
53 printk(KERN_DEBUG "mt312: " args); \
53 } while (0) 54 } while (0)
54 55
55#define MT312_SYS_CLK 90000000UL /* 90 MHz */ 56#define MT312_SYS_CLK 90000000UL /* 90 MHz */
56#define MT312_LPOWER_SYS_CLK 60000000UL /* 60 MHz */ 57#define MT312_LPOWER_SYS_CLK 60000000UL /* 60 MHz */
57#define MT312_PLL_CLK 10000000UL /* 10 MHz */ 58#define MT312_PLL_CLK 10000000UL /* 10 MHz */
58 59
59static int mt312_read(struct mt312_state* state, const enum mt312_reg_addr reg, 60static int mt312_read(struct mt312_state *state, const enum mt312_reg_addr reg,
60 void *buf, const size_t count) 61 void *buf, const size_t count)
61{ 62{
62 int ret; 63 int ret;
@@ -79,7 +80,7 @@ static int mt312_read(struct mt312_state* state, const enum mt312_reg_addr reg,
79 return -EREMOTEIO; 80 return -EREMOTEIO;
80 } 81 }
81 82
82 if(debug) { 83 if (debug) {
83 int i; 84 int i;
84 dprintk("R(%d):", reg & 0x7f); 85 dprintk("R(%d):", reg & 0x7f);
85 for (i = 0; i < count; i++) 86 for (i = 0; i < count; i++)
@@ -90,14 +91,14 @@ static int mt312_read(struct mt312_state* state, const enum mt312_reg_addr reg,
90 return 0; 91 return 0;
91} 92}
92 93
93static int mt312_write(struct mt312_state* state, const enum mt312_reg_addr reg, 94static int mt312_write(struct mt312_state *state, const enum mt312_reg_addr reg,
94 const void *src, const size_t count) 95 const void *src, const size_t count)
95{ 96{
96 int ret; 97 int ret;
97 u8 buf[count + 1]; 98 u8 buf[count + 1];
98 struct i2c_msg msg; 99 struct i2c_msg msg;
99 100
100 if(debug) { 101 if (debug) {
101 int i; 102 int i;
102 dprintk("W(%d):", reg & 0x7f); 103 dprintk("W(%d):", reg & 0x7f);
103 for (i = 0; i < count; i++) 104 for (i = 0; i < count; i++)
@@ -123,13 +124,13 @@ static int mt312_write(struct mt312_state* state, const enum mt312_reg_addr reg,
123 return 0; 124 return 0;
124} 125}
125 126
126static inline int mt312_readreg(struct mt312_state* state, 127static inline int mt312_readreg(struct mt312_state *state,
127 const enum mt312_reg_addr reg, u8 *val) 128 const enum mt312_reg_addr reg, u8 *val)
128{ 129{
129 return mt312_read(state, reg, val, 1); 130 return mt312_read(state, reg, val, 1);
130} 131}
131 132
132static inline int mt312_writereg(struct mt312_state* state, 133static inline int mt312_writereg(struct mt312_state *state,
133 const enum mt312_reg_addr reg, const u8 val) 134 const enum mt312_reg_addr reg, const u8 val)
134{ 135{
135 return mt312_write(state, reg, &val, 1); 136 return mt312_write(state, reg, &val, 1);
@@ -140,18 +141,19 @@ static inline u32 mt312_div(u32 a, u32 b)
140 return (a + (b / 2)) / b; 141 return (a + (b / 2)) / b;
141} 142}
142 143
143static int mt312_reset(struct mt312_state* state, const u8 full) 144static int mt312_reset(struct mt312_state *state, const u8 full)
144{ 145{
145 return mt312_writereg(state, RESET, full ? 0x80 : 0x40); 146 return mt312_writereg(state, RESET, full ? 0x80 : 0x40);
146} 147}
147 148
148static int mt312_get_inversion(struct mt312_state* state, 149static int mt312_get_inversion(struct mt312_state *state,
149 fe_spectral_inversion_t *i) 150 fe_spectral_inversion_t *i)
150{ 151{
151 int ret; 152 int ret;
152 u8 vit_mode; 153 u8 vit_mode;
153 154
154 if ((ret = mt312_readreg(state, VIT_MODE, &vit_mode)) < 0) 155 ret = mt312_readreg(state, VIT_MODE, &vit_mode);
156 if (ret < 0)
155 return ret; 157 return ret;
156 158
157 if (vit_mode & 0x80) /* auto inversion was used */ 159 if (vit_mode & 0x80) /* auto inversion was used */
@@ -160,7 +162,7 @@ static int mt312_get_inversion(struct mt312_state* state,
160 return 0; 162 return 0;
161} 163}
162 164
163static int mt312_get_symbol_rate(struct mt312_state* state, u32 *sr) 165static int mt312_get_symbol_rate(struct mt312_state *state, u32 *sr)
164{ 166{
165 int ret; 167 int ret;
166 u8 sym_rate_h; 168 u8 sym_rate_h;
@@ -169,37 +171,44 @@ static int mt312_get_symbol_rate(struct mt312_state* state, u32 *sr)
169 u16 monitor; 171 u16 monitor;
170 u8 buf[2]; 172 u8 buf[2];
171 173
172 if ((ret = mt312_readreg(state, SYM_RATE_H, &sym_rate_h)) < 0) 174 ret = mt312_readreg(state, SYM_RATE_H, &sym_rate_h);
175 if (ret < 0)
173 return ret; 176 return ret;
174 177
175 if (sym_rate_h & 0x80) { /* symbol rate search was used */ 178 if (sym_rate_h & 0x80) {
176 if ((ret = mt312_writereg(state, MON_CTRL, 0x03)) < 0) 179 /* symbol rate search was used */
180 ret = mt312_writereg(state, MON_CTRL, 0x03);
181 if (ret < 0)
177 return ret; 182 return ret;
178 183
179 if ((ret = mt312_read(state, MONITOR_H, buf, sizeof(buf))) < 0) 184 ret = mt312_read(state, MONITOR_H, buf, sizeof(buf));
185 if (ret < 0)
180 return ret; 186 return ret;
181 187
182 monitor = (buf[0] << 8) | buf[1]; 188 monitor = (buf[0] << 8) | buf[1];
183 189
184 dprintk(KERN_DEBUG "sr(auto) = %u\n", 190 dprintk("sr(auto) = %u\n",
185 mt312_div(monitor * 15625, 4)); 191 mt312_div(monitor * 15625, 4));
186 } else { 192 } else {
187 if ((ret = mt312_writereg(state, MON_CTRL, 0x05)) < 0) 193 ret = mt312_writereg(state, MON_CTRL, 0x05);
194 if (ret < 0)
188 return ret; 195 return ret;
189 196
190 if ((ret = mt312_read(state, MONITOR_H, buf, sizeof(buf))) < 0) 197 ret = mt312_read(state, MONITOR_H, buf, sizeof(buf));
198 if (ret < 0)
191 return ret; 199 return ret;
192 200
193 dec_ratio = ((buf[0] >> 5) & 0x07) * 32; 201 dec_ratio = ((buf[0] >> 5) & 0x07) * 32;
194 202
195 if ((ret = mt312_read(state, SYM_RAT_OP_H, buf, sizeof(buf))) < 0) 203 ret = mt312_read(state, SYM_RAT_OP_H, buf, sizeof(buf));
204 if (ret < 0)
196 return ret; 205 return ret;
197 206
198 sym_rat_op = (buf[0] << 8) | buf[1]; 207 sym_rat_op = (buf[0] << 8) | buf[1];
199 208
200 dprintk(KERN_DEBUG "sym_rat_op=%d dec_ratio=%d\n", 209 dprintk("sym_rat_op=%d dec_ratio=%d\n",
201 sym_rat_op, dec_ratio); 210 sym_rat_op, dec_ratio);
202 dprintk(KERN_DEBUG "*sr(manual) = %lu\n", 211 dprintk("*sr(manual) = %lu\n",
203 (((MT312_PLL_CLK * 8192) / (sym_rat_op + 8192)) * 212 (((MT312_PLL_CLK * 8192) / (sym_rat_op + 8192)) *
204 2) - dec_ratio); 213 2) - dec_ratio);
205 } 214 }
@@ -207,7 +216,7 @@ static int mt312_get_symbol_rate(struct mt312_state* state, u32 *sr)
207 return 0; 216 return 0;
208} 217}
209 218
210static int mt312_get_code_rate(struct mt312_state* state, fe_code_rate_t *cr) 219static int mt312_get_code_rate(struct mt312_state *state, fe_code_rate_t *cr)
211{ 220{
212 const fe_code_rate_t fec_tab[8] = 221 const fe_code_rate_t fec_tab[8] =
213 { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8, 222 { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8,
@@ -216,7 +225,8 @@ static int mt312_get_code_rate(struct mt312_state* state, fe_code_rate_t *cr)
216 int ret; 225 int ret;
217 u8 fec_status; 226 u8 fec_status;
218 227
219 if ((ret = mt312_readreg(state, FEC_STATUS, &fec_status)) < 0) 228 ret = mt312_readreg(state, FEC_STATUS, &fec_status);
229 if (ret < 0)
220 return ret; 230 return ret;
221 231
222 *cr = fec_tab[(fec_status >> 4) & 0x07]; 232 *cr = fec_tab[(fec_status >> 4) & 0x07];
@@ -224,61 +234,72 @@ static int mt312_get_code_rate(struct mt312_state* state, fe_code_rate_t *cr)
224 return 0; 234 return 0;
225} 235}
226 236
227static int mt312_initfe(struct dvb_frontend* fe) 237static int mt312_initfe(struct dvb_frontend *fe)
228{ 238{
229 struct mt312_state *state = fe->demodulator_priv; 239 struct mt312_state *state = fe->demodulator_priv;
230 int ret; 240 int ret;
231 u8 buf[2]; 241 u8 buf[2];
232 242
233 /* wake up */ 243 /* wake up */
234 if ((ret = mt312_writereg(state, CONFIG, (state->frequency == 60 ? 0x88 : 0x8c))) < 0) 244 ret = mt312_writereg(state, CONFIG,
245 (state->frequency == 60 ? 0x88 : 0x8c));
246 if (ret < 0)
235 return ret; 247 return ret;
236 248
237 /* wait at least 150 usec */ 249 /* wait at least 150 usec */
238 udelay(150); 250 udelay(150);
239 251
240 /* full reset */ 252 /* full reset */
241 if ((ret = mt312_reset(state, 1)) < 0) 253 ret = mt312_reset(state, 1);
254 if (ret < 0)
242 return ret; 255 return ret;
243 256
244// Per datasheet, write correct values. 09/28/03 ACCJr. 257/* Per datasheet, write correct values. 09/28/03 ACCJr.
245// If we don't do this, we won't get FE_HAS_VITERBI in the VP310. 258 * If we don't do this, we won't get FE_HAS_VITERBI in the VP310. */
246 { 259 {
247 u8 buf_def[8]={0x14, 0x12, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00}; 260 u8 buf_def[8] = { 0x14, 0x12, 0x03, 0x02,
261 0x01, 0x00, 0x00, 0x00 };
248 262
249 if ((ret = mt312_write(state, VIT_SETUP, buf_def, sizeof(buf_def))) < 0) 263 ret = mt312_write(state, VIT_SETUP, buf_def, sizeof(buf_def));
264 if (ret < 0)
250 return ret; 265 return ret;
251 } 266 }
252 267
253 /* SYS_CLK */ 268 /* SYS_CLK */
254 buf[0] = mt312_div((state->frequency == 60 ? MT312_LPOWER_SYS_CLK : MT312_SYS_CLK) * 2, 1000000); 269 buf[0] = mt312_div((state->frequency == 60 ? MT312_LPOWER_SYS_CLK :
270 MT312_SYS_CLK) * 2, 1000000);
255 271
256 /* DISEQC_RATIO */ 272 /* DISEQC_RATIO */
257 buf[1] = mt312_div(MT312_PLL_CLK, 15000 * 4); 273 buf[1] = mt312_div(MT312_PLL_CLK, 15000 * 4);
258 274
259 if ((ret = mt312_write(state, SYS_CLK, buf, sizeof(buf))) < 0) 275 ret = mt312_write(state, SYS_CLK, buf, sizeof(buf));
276 if (ret < 0)
260 return ret; 277 return ret;
261 278
262 if ((ret = mt312_writereg(state, SNR_THS_HIGH, 0x32)) < 0) 279 ret = mt312_writereg(state, SNR_THS_HIGH, 0x32);
280 if (ret < 0)
263 return ret; 281 return ret;
264 282
265 if ((ret = mt312_writereg(state, OP_CTRL, 0x53)) < 0) 283 ret = mt312_writereg(state, OP_CTRL, 0x53);
284 if (ret < 0)
266 return ret; 285 return ret;
267 286
268 /* TS_SW_LIM */ 287 /* TS_SW_LIM */
269 buf[0] = 0x8c; 288 buf[0] = 0x8c;
270 buf[1] = 0x98; 289 buf[1] = 0x98;
271 290
272 if ((ret = mt312_write(state, TS_SW_LIM_L, buf, sizeof(buf))) < 0) 291 ret = mt312_write(state, TS_SW_LIM_L, buf, sizeof(buf));
292 if (ret < 0)
273 return ret; 293 return ret;
274 294
275 if ((ret = mt312_writereg(state, CS_SW_LIM, 0x69)) < 0) 295 ret = mt312_writereg(state, CS_SW_LIM, 0x69);
296 if (ret < 0)
276 return ret; 297 return ret;
277 298
278 return 0; 299 return 0;
279} 300}
280 301
281static int mt312_send_master_cmd(struct dvb_frontend* fe, 302static int mt312_send_master_cmd(struct dvb_frontend *fe,
282 struct dvb_diseqc_master_cmd *c) 303 struct dvb_diseqc_master_cmd *c)
283{ 304{
284 struct mt312_state *state = fe->demodulator_priv; 305 struct mt312_state *state = fe->demodulator_priv;
@@ -288,29 +309,31 @@ static int mt312_send_master_cmd(struct dvb_frontend* fe,
288 if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg))) 309 if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg)))
289 return -EINVAL; 310 return -EINVAL;
290 311
291 if ((ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode)) < 0) 312 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
313 if (ret < 0)
292 return ret; 314 return ret;
293 315
294 if ((ret = 316 ret = mt312_write(state, (0x80 | DISEQC_INSTR), c->msg, c->msg_len);
295 mt312_write(state, (0x80 | DISEQC_INSTR), c->msg, c->msg_len)) < 0) 317 if (ret < 0)
296 return ret; 318 return ret;
297 319
298 if ((ret = 320 ret = mt312_writereg(state, DISEQC_MODE,
299 mt312_writereg(state, DISEQC_MODE, 321 (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3)
300 (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3) 322 | 0x04);
301 | 0x04)) < 0) 323 if (ret < 0)
302 return ret; 324 return ret;
303 325
304 /* set DISEQC_MODE[2:0] to zero if a return message is expected */ 326 /* set DISEQC_MODE[2:0] to zero if a return message is expected */
305 if (c->msg[0] & 0x02) 327 if (c->msg[0] & 0x02) {
306 if ((ret = 328 ret = mt312_writereg(state, DISEQC_MODE, (diseqc_mode & 0x40));
307 mt312_writereg(state, DISEQC_MODE, (diseqc_mode & 0x40))) < 0) 329 if (ret < 0)
308 return ret; 330 return ret;
331 }
309 332
310 return 0; 333 return 0;
311} 334}
312 335
313static int mt312_send_burst(struct dvb_frontend* fe, const fe_sec_mini_cmd_t c) 336static int mt312_send_burst(struct dvb_frontend *fe, const fe_sec_mini_cmd_t c)
314{ 337{
315 struct mt312_state *state = fe->demodulator_priv; 338 struct mt312_state *state = fe->demodulator_priv;
316 const u8 mini_tab[2] = { 0x02, 0x03 }; 339 const u8 mini_tab[2] = { 0x02, 0x03 };
@@ -321,18 +344,19 @@ static int mt312_send_burst(struct dvb_frontend* fe, const fe_sec_mini_cmd_t c)
321 if (c > SEC_MINI_B) 344 if (c > SEC_MINI_B)
322 return -EINVAL; 345 return -EINVAL;
323 346
324 if ((ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode)) < 0) 347 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
348 if (ret < 0)
325 return ret; 349 return ret;
326 350
327 if ((ret = 351 ret = mt312_writereg(state, DISEQC_MODE,
328 mt312_writereg(state, DISEQC_MODE, 352 (diseqc_mode & 0x40) | mini_tab[c]);
329 (diseqc_mode & 0x40) | mini_tab[c])) < 0) 353 if (ret < 0)
330 return ret; 354 return ret;
331 355
332 return 0; 356 return 0;
333} 357}
334 358
335static int mt312_set_tone(struct dvb_frontend* fe, const fe_sec_tone_mode_t t) 359static int mt312_set_tone(struct dvb_frontend *fe, const fe_sec_tone_mode_t t)
336{ 360{
337 struct mt312_state *state = fe->demodulator_priv; 361 struct mt312_state *state = fe->demodulator_priv;
338 const u8 tone_tab[2] = { 0x01, 0x00 }; 362 const u8 tone_tab[2] = { 0x01, 0x00 };
@@ -343,18 +367,19 @@ static int mt312_set_tone(struct dvb_frontend* fe, const fe_sec_tone_mode_t t)
343 if (t > SEC_TONE_OFF) 367 if (t > SEC_TONE_OFF)
344 return -EINVAL; 368 return -EINVAL;
345 369
346 if ((ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode)) < 0) 370 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
371 if (ret < 0)
347 return ret; 372 return ret;
348 373
349 if ((ret = 374 ret = mt312_writereg(state, DISEQC_MODE,
350 mt312_writereg(state, DISEQC_MODE, 375 (diseqc_mode & 0x40) | tone_tab[t]);
351 (diseqc_mode & 0x40) | tone_tab[t])) < 0) 376 if (ret < 0)
352 return ret; 377 return ret;
353 378
354 return 0; 379 return 0;
355} 380}
356 381
357static int mt312_set_voltage(struct dvb_frontend* fe, const fe_sec_voltage_t v) 382static int mt312_set_voltage(struct dvb_frontend *fe, const fe_sec_voltage_t v)
358{ 383{
359 struct mt312_state *state = fe->demodulator_priv; 384 struct mt312_state *state = fe->demodulator_priv;
360 const u8 volt_tab[3] = { 0x00, 0x40, 0x00 }; 385 const u8 volt_tab[3] = { 0x00, 0x40, 0x00 };
@@ -365,7 +390,7 @@ static int mt312_set_voltage(struct dvb_frontend* fe, const fe_sec_voltage_t v)
365 return mt312_writereg(state, DISEQC_MODE, volt_tab[v]); 390 return mt312_writereg(state, DISEQC_MODE, volt_tab[v]);
366} 391}
367 392
368static int mt312_read_status(struct dvb_frontend* fe, fe_status_t *s) 393static int mt312_read_status(struct dvb_frontend *fe, fe_status_t *s)
369{ 394{
370 struct mt312_state *state = fe->demodulator_priv; 395 struct mt312_state *state = fe->demodulator_priv;
371 int ret; 396 int ret;
@@ -373,10 +398,12 @@ static int mt312_read_status(struct dvb_frontend* fe, fe_status_t *s)
373 398
374 *s = 0; 399 *s = 0;
375 400
376 if ((ret = mt312_read(state, QPSK_STAT_H, status, sizeof(status))) < 0) 401 ret = mt312_read(state, QPSK_STAT_H, status, sizeof(status));
402 if (ret < 0)
377 return ret; 403 return ret;
378 404
379 dprintk(KERN_DEBUG "QPSK_STAT_H: 0x%02x, QPSK_STAT_L: 0x%02x, FEC_STATUS: 0x%02x\n", status[0], status[1], status[2]); 405 dprintk("QPSK_STAT_H: 0x%02x, QPSK_STAT_L: 0x%02x,"
406 " FEC_STATUS: 0x%02x\n", status[0], status[1], status[2]);
380 407
381 if (status[0] & 0xc0) 408 if (status[0] & 0xc0)
382 *s |= FE_HAS_SIGNAL; /* signal noise ratio */ 409 *s |= FE_HAS_SIGNAL; /* signal noise ratio */
@@ -392,13 +419,14 @@ static int mt312_read_status(struct dvb_frontend* fe, fe_status_t *s)
392 return 0; 419 return 0;
393} 420}
394 421
395static int mt312_read_ber(struct dvb_frontend* fe, u32 *ber) 422static int mt312_read_ber(struct dvb_frontend *fe, u32 *ber)
396{ 423{
397 struct mt312_state *state = fe->demodulator_priv; 424 struct mt312_state *state = fe->demodulator_priv;
398 int ret; 425 int ret;
399 u8 buf[3]; 426 u8 buf[3];
400 427
401 if ((ret = mt312_read(state, RS_BERCNT_H, buf, 3)) < 0) 428 ret = mt312_read(state, RS_BERCNT_H, buf, 3);
429 if (ret < 0)
402 return ret; 430 return ret;
403 431
404 *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64; 432 *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64;
@@ -406,7 +434,8 @@ static int mt312_read_ber(struct dvb_frontend* fe, u32 *ber)
406 return 0; 434 return 0;
407} 435}
408 436
409static int mt312_read_signal_strength(struct dvb_frontend* fe, u16 *signal_strength) 437static int mt312_read_signal_strength(struct dvb_frontend *fe,
438 u16 *signal_strength)
410{ 439{
411 struct mt312_state *state = fe->demodulator_priv; 440 struct mt312_state *state = fe->demodulator_priv;
412 int ret; 441 int ret;
@@ -414,7 +443,8 @@ static int mt312_read_signal_strength(struct dvb_frontend* fe, u16 *signal_stren
414 u16 agc; 443 u16 agc;
415 s16 err_db; 444 s16 err_db;
416 445
417 if ((ret = mt312_read(state, AGC_H, buf, sizeof(buf))) < 0) 446 ret = mt312_read(state, AGC_H, buf, sizeof(buf));
447 if (ret < 0)
418 return ret; 448 return ret;
419 449
420 agc = (buf[0] << 6) | (buf[1] >> 2); 450 agc = (buf[0] << 6) | (buf[1] >> 2);
@@ -422,18 +452,19 @@ static int mt312_read_signal_strength(struct dvb_frontend* fe, u16 *signal_stren
422 452
423 *signal_strength = agc; 453 *signal_strength = agc;
424 454
425 dprintk(KERN_DEBUG "agc=%08x err_db=%hd\n", agc, err_db); 455 dprintk("agc=%08x err_db=%hd\n", agc, err_db);
426 456
427 return 0; 457 return 0;
428} 458}
429 459
430static int mt312_read_snr(struct dvb_frontend* fe, u16 *snr) 460static int mt312_read_snr(struct dvb_frontend *fe, u16 *snr)
431{ 461{
432 struct mt312_state *state = fe->demodulator_priv; 462 struct mt312_state *state = fe->demodulator_priv;
433 int ret; 463 int ret;
434 u8 buf[2]; 464 u8 buf[2];
435 465
436 if ((ret = mt312_read(state, M_SNR_H, &buf, sizeof(buf))) < 0) 466 ret = mt312_read(state, M_SNR_H, &buf, sizeof(buf));
467 if (ret < 0)
437 return ret; 468 return ret;
438 469
439 *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1); 470 *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1);
@@ -441,13 +472,14 @@ static int mt312_read_snr(struct dvb_frontend* fe, u16 *snr)
441 return 0; 472 return 0;
442} 473}
443 474
444static int mt312_read_ucblocks(struct dvb_frontend* fe, u32 *ubc) 475static int mt312_read_ucblocks(struct dvb_frontend *fe, u32 *ubc)
445{ 476{
446 struct mt312_state *state = fe->demodulator_priv; 477 struct mt312_state *state = fe->demodulator_priv;
447 int ret; 478 int ret;
448 u8 buf[2]; 479 u8 buf[2];
449 480
450 if ((ret = mt312_read(state, RS_UBC_H, &buf, sizeof(buf))) < 0) 481 ret = mt312_read(state, RS_UBC_H, &buf, sizeof(buf));
482 if (ret < 0)
451 return ret; 483 return ret;
452 484
453 *ubc = (buf[0] << 8) | buf[1]; 485 *ubc = (buf[0] << 8) | buf[1];
@@ -455,7 +487,7 @@ static int mt312_read_ucblocks(struct dvb_frontend* fe, u32 *ubc)
455 return 0; 487 return 0;
456} 488}
457 489
458static int mt312_set_frontend(struct dvb_frontend* fe, 490static int mt312_set_frontend(struct dvb_frontend *fe,
459 struct dvb_frontend_parameters *p) 491 struct dvb_frontend_parameters *p)
460{ 492{
461 struct mt312_state *state = fe->demodulator_priv; 493 struct mt312_state *state = fe->demodulator_priv;
@@ -491,24 +523,28 @@ static int mt312_set_frontend(struct dvb_frontend* fe,
491 523
492 switch (state->id) { 524 switch (state->id) {
493 case ID_VP310: 525 case ID_VP310:
494 // For now we will do this only for the VP310. 526 /* For now we will do this only for the VP310.
495 // It should be better for the mt312 as well, but tunning will be slower. ACCJr 09/29/03 527 * It should be better for the mt312 as well,
528 * but tuning will be slower. ACCJr 09/29/03
529 */
496 ret = mt312_readreg(state, CONFIG, &config_val); 530 ret = mt312_readreg(state, CONFIG, &config_val);
497 if (ret < 0) 531 if (ret < 0)
498 return ret; 532 return ret;
499 if (p->u.qpsk.symbol_rate >= 30000000) //Note that 30MS/s should use 90MHz 533 if (p->u.qpsk.symbol_rate >= 30000000) {
500 { 534 /* Note that 30MS/s should use 90MHz */
501 if ((config_val & 0x0c) == 0x08) { //We are running 60MHz 535 if ((config_val & 0x0c) == 0x08) {
536 /* We are running 60MHz */
502 state->frequency = 90; 537 state->frequency = 90;
503 if ((ret = mt312_initfe(fe)) < 0) 538 ret = mt312_initfe(fe);
539 if (ret < 0)
504 return ret; 540 return ret;
505 } 541 }
506 } 542 } else {
507 else 543 if ((config_val & 0x0c) == 0x0C) {
508 { 544 /* We are running 90MHz */
509 if ((config_val & 0x0c) == 0x0C) { //We are running 90MHz
510 state->frequency = 60; 545 state->frequency = 60;
511 if ((ret = mt312_initfe(fe)) < 0) 546 ret = mt312_initfe(fe);
547 if (ret < 0)
512 return ret; 548 return ret;
513 } 549 }
514 } 550 }
@@ -523,7 +559,8 @@ static int mt312_set_frontend(struct dvb_frontend* fe,
523 559
524 if (fe->ops.tuner_ops.set_params) { 560 if (fe->ops.tuner_ops.set_params) {
525 fe->ops.tuner_ops.set_params(fe, p); 561 fe->ops.tuner_ops.set_params(fe, p);
526 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0); 562 if (fe->ops.i2c_gate_ctrl)
563 fe->ops.i2c_gate_ctrl(fe, 0);
527 } 564 }
528 565
529 /* sr = (u16)(sr * 256.0 / 1000000.0) */ 566 /* sr = (u16)(sr * 256.0 / 1000000.0) */
@@ -545,7 +582,8 @@ static int mt312_set_frontend(struct dvb_frontend* fe,
545 /* GO */ 582 /* GO */
546 buf[4] = 0x01; 583 buf[4] = 0x01;
547 584
548 if ((ret = mt312_write(state, SYM_RATE_H, buf, sizeof(buf))) < 0) 585 ret = mt312_write(state, SYM_RATE_H, buf, sizeof(buf));
586 if (ret < 0)
549 return ret; 587 return ret;
550 588
551 mt312_reset(state, 0); 589 mt312_reset(state, 0);
@@ -553,27 +591,30 @@ static int mt312_set_frontend(struct dvb_frontend* fe,
553 return 0; 591 return 0;
554} 592}
555 593
556static int mt312_get_frontend(struct dvb_frontend* fe, 594static int mt312_get_frontend(struct dvb_frontend *fe,
557 struct dvb_frontend_parameters *p) 595 struct dvb_frontend_parameters *p)
558{ 596{
559 struct mt312_state *state = fe->demodulator_priv; 597 struct mt312_state *state = fe->demodulator_priv;
560 int ret; 598 int ret;
561 599
562 if ((ret = mt312_get_inversion(state, &p->inversion)) < 0) 600 ret = mt312_get_inversion(state, &p->inversion);
601 if (ret < 0)
563 return ret; 602 return ret;
564 603
565 if ((ret = mt312_get_symbol_rate(state, &p->u.qpsk.symbol_rate)) < 0) 604 ret = mt312_get_symbol_rate(state, &p->u.qpsk.symbol_rate);
605 if (ret < 0)
566 return ret; 606 return ret;
567 607
568 if ((ret = mt312_get_code_rate(state, &p->u.qpsk.fec_inner)) < 0) 608 ret = mt312_get_code_rate(state, &p->u.qpsk.fec_inner);
609 if (ret < 0)
569 return ret; 610 return ret;
570 611
571 return 0; 612 return 0;
572} 613}
573 614
574static int mt312_i2c_gate_ctrl(struct dvb_frontend* fe, int enable) 615static int mt312_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
575{ 616{
576 struct mt312_state* state = fe->demodulator_priv; 617 struct mt312_state *state = fe->demodulator_priv;
577 618
578 if (enable) { 619 if (enable) {
579 return mt312_writereg(state, GPP_CTRL, 0x40); 620 return mt312_writereg(state, GPP_CTRL, 0x40);
@@ -582,27 +623,31 @@ static int mt312_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
582 } 623 }
583} 624}
584 625
585static int mt312_sleep(struct dvb_frontend* fe) 626static int mt312_sleep(struct dvb_frontend *fe)
586{ 627{
587 struct mt312_state *state = fe->demodulator_priv; 628 struct mt312_state *state = fe->demodulator_priv;
588 int ret; 629 int ret;
589 u8 config; 630 u8 config;
590 631
591 /* reset all registers to defaults */ 632 /* reset all registers to defaults */
592 if ((ret = mt312_reset(state, 1)) < 0) 633 ret = mt312_reset(state, 1);
634 if (ret < 0)
593 return ret; 635 return ret;
594 636
595 if ((ret = mt312_readreg(state, CONFIG, &config)) < 0) 637 ret = mt312_readreg(state, CONFIG, &config);
638 if (ret < 0)
596 return ret; 639 return ret;
597 640
598 /* enter standby */ 641 /* enter standby */
599 if ((ret = mt312_writereg(state, CONFIG, config & 0x7f)) < 0) 642 ret = mt312_writereg(state, CONFIG, config & 0x7f);
643 if (ret < 0)
600 return ret; 644 return ret;
601 645
602 return 0; 646 return 0;
603} 647}
604 648
605static int mt312_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings) 649static int mt312_get_tune_settings(struct dvb_frontend *fe,
650 struct dvb_frontend_tune_settings *fesettings)
606{ 651{
607 fesettings->min_delay_ms = 50; 652 fesettings->min_delay_ms = 50;
608 fesettings->step_size = 0; 653 fesettings->step_size = 0;
@@ -610,9 +655,9 @@ static int mt312_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_
610 return 0; 655 return 0;
611} 656}
612 657
613static void mt312_release(struct dvb_frontend* fe) 658static void mt312_release(struct dvb_frontend *fe)
614{ 659{
615 struct mt312_state* state = fe->demodulator_priv; 660 struct mt312_state *state = fe->demodulator_priv;
616 kfree(state); 661 kfree(state);
617} 662}
618 663
@@ -655,10 +700,10 @@ static struct dvb_frontend_ops vp310_mt312_ops = {
655 .set_voltage = mt312_set_voltage, 700 .set_voltage = mt312_set_voltage,
656}; 701};
657 702
658struct dvb_frontend* vp310_mt312_attach(const struct mt312_config* config, 703struct dvb_frontend *vp310_mt312_attach(const struct mt312_config *config,
659 struct i2c_adapter* i2c) 704 struct i2c_adapter *i2c)
660{ 705{
661 struct mt312_state* state = NULL; 706 struct mt312_state *state = NULL;
662 707
663 /* allocate memory for the internal state */ 708 /* allocate memory for the internal state */
664 state = kmalloc(sizeof(struct mt312_state), GFP_KERNEL); 709 state = kmalloc(sizeof(struct mt312_state), GFP_KERNEL);
@@ -674,7 +719,8 @@ struct dvb_frontend* vp310_mt312_attach(const struct mt312_config* config,
674 goto error; 719 goto error;
675 720
676 /* create dvb_frontend */ 721 /* create dvb_frontend */
677 memcpy(&state->frontend.ops, &vp310_mt312_ops, sizeof(struct dvb_frontend_ops)); 722 memcpy(&state->frontend.ops, &vp310_mt312_ops,
723 sizeof(struct dvb_frontend_ops));
678 state->frontend.demodulator_priv = state; 724 state->frontend.demodulator_priv = state;
679 725
680 switch (state->id) { 726 switch (state->id) {
@@ -687,7 +733,8 @@ struct dvb_frontend* vp310_mt312_attach(const struct mt312_config* config,
687 state->frequency = 60; 733 state->frequency = 60;
688 break; 734 break;
689 default: 735 default:
690 printk (KERN_WARNING "Only Zarlink VP310/MT312 are supported chips.\n"); 736 printk(KERN_WARNING "Only Zarlink VP310/MT312"
737 " are supported chips.\n");
691 goto error; 738 goto error;
692 } 739 }
693 740
@@ -697,6 +744,7 @@ error:
697 kfree(state); 744 kfree(state);
698 return NULL; 745 return NULL;
699} 746}
747EXPORT_SYMBOL(vp310_mt312_attach);
700 748
701module_param(debug, int, 0644); 749module_param(debug, int, 0644);
702MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); 750MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
@@ -705,4 +753,3 @@ MODULE_DESCRIPTION("Zarlink VP310/MT312 DVB-S Demodulator driver");
705MODULE_AUTHOR("Andreas Oberritter <obi@linuxtv.org>"); 753MODULE_AUTHOR("Andreas Oberritter <obi@linuxtv.org>");
706MODULE_LICENSE("GPL"); 754MODULE_LICENSE("GPL");
707 755
708EXPORT_SYMBOL(vp310_mt312_attach);
diff --git a/drivers/media/dvb/frontends/mt312.h b/drivers/media/dvb/frontends/mt312.h
index cf9a1505ad4b..f17cb93ba9ba 100644
--- a/drivers/media/dvb/frontends/mt312.h
+++ b/drivers/media/dvb/frontends/mt312.h
@@ -28,22 +28,21 @@
28 28
29#include <linux/dvb/frontend.h> 29#include <linux/dvb/frontend.h>
30 30
31struct mt312_config 31struct mt312_config {
32{
33 /* the demodulator's i2c address */ 32 /* the demodulator's i2c address */
34 u8 demod_address; 33 u8 demod_address;
35}; 34};
36 35
37#if defined(CONFIG_DVB_MT312) || (defined(CONFIG_DVB_MT312_MODULE) && defined(MODULE)) 36#if defined(CONFIG_DVB_MT312) || (defined(CONFIG_DVB_MT312_MODULE) && defined(MODULE))
38struct dvb_frontend* vp310_mt312_attach(const struct mt312_config* config, 37struct dvb_frontend *vp310_mt312_attach(const struct mt312_config *config,
39 struct i2c_adapter* i2c); 38 struct i2c_adapter *i2c);
40#else 39#else
41static inline struct dvb_frontend* vp310_mt312_attach(const struct mt312_config* config, 40static inline struct dvb_frontend *vp310_mt312_attach(
42 struct i2c_adapter* i2c) 41 const struct mt312_config *config, struct i2c_adapter *i2c)
43{ 42{
44 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __FUNCTION__); 43 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __FUNCTION__);
45 return NULL; 44 return NULL;
46} 45}
47#endif // CONFIG_DVB_MT312 46#endif /* CONFIG_DVB_MT312 */
48 47
49#endif // MT312_H 48#endif /* MT312_H */
diff --git a/drivers/media/dvb/frontends/mt352.c b/drivers/media/dvb/frontends/mt352.c
index 5dd9b731f6f2..7cd190b6f015 100644
--- a/drivers/media/dvb/frontends/mt352.c
+++ b/drivers/media/dvb/frontends/mt352.c
@@ -152,7 +152,13 @@ static void mt352_calc_input_freq(struct mt352_state* state,
152 if (state->config.if2) 152 if (state->config.if2)
153 if2 = state->config.if2; 153 if2 = state->config.if2;
154 154
155 ife = (2*adc_clock - if2); 155 if (adc_clock >= if2 * 2)
156 ife = if2;
157 else {
158 ife = adc_clock - (if2 % adc_clock);
159 if (ife > adc_clock / 2)
160 ife = adc_clock - ife;
161 }
156 value = -16374 * ife / adc_clock; 162 value = -16374 * ife / adc_clock;
157 dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n", 163 dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
158 __FUNCTION__, if2, ife, adc_clock, value, value & 0x3fff); 164 __FUNCTION__, if2, ife, adc_clock, value, value & 0x3fff);
diff --git a/drivers/media/dvb/frontends/or51132.c b/drivers/media/dvb/frontends/or51132.c
index b314a1f2deed..1d2d28ce823d 100644
--- a/drivers/media/dvb/frontends/or51132.c
+++ b/drivers/media/dvb/frontends/or51132.c
@@ -564,7 +564,7 @@ struct dvb_frontend* or51132_attach(const struct or51132_config* config,
564 /* Allocate memory for the internal state */ 564 /* Allocate memory for the internal state */
565 state = kmalloc(sizeof(struct or51132_state), GFP_KERNEL); 565 state = kmalloc(sizeof(struct or51132_state), GFP_KERNEL);
566 if (state == NULL) 566 if (state == NULL)
567 goto error; 567 return NULL;
568 568
569 /* Setup the state */ 569 /* Setup the state */
570 state->config = config; 570 state->config = config;
@@ -576,10 +576,6 @@ struct dvb_frontend* or51132_attach(const struct or51132_config* config,
576 memcpy(&state->frontend.ops, &or51132_ops, sizeof(struct dvb_frontend_ops)); 576 memcpy(&state->frontend.ops, &or51132_ops, sizeof(struct dvb_frontend_ops));
577 state->frontend.demodulator_priv = state; 577 state->frontend.demodulator_priv = state;
578 return &state->frontend; 578 return &state->frontend;
579
580error:
581 kfree(state);
582 return NULL;
583} 579}
584 580
585static struct dvb_frontend_ops or51132_ops = { 581static struct dvb_frontend_ops or51132_ops = {
diff --git a/drivers/media/dvb/frontends/or51211.c b/drivers/media/dvb/frontends/or51211.c
index f02bd9445955..6a6b0d727c6f 100644
--- a/drivers/media/dvb/frontends/or51211.c
+++ b/drivers/media/dvb/frontends/or51211.c
@@ -529,7 +529,7 @@ struct dvb_frontend* or51211_attach(const struct or51211_config* config,
529 /* Allocate memory for the internal state */ 529 /* Allocate memory for the internal state */
530 state = kmalloc(sizeof(struct or51211_state), GFP_KERNEL); 530 state = kmalloc(sizeof(struct or51211_state), GFP_KERNEL);
531 if (state == NULL) 531 if (state == NULL)
532 goto error; 532 return NULL;
533 533
534 /* Setup the state */ 534 /* Setup the state */
535 state->config = config; 535 state->config = config;
@@ -541,10 +541,6 @@ struct dvb_frontend* or51211_attach(const struct or51211_config* config,
541 memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops)); 541 memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
542 state->frontend.demodulator_priv = state; 542 state->frontend.demodulator_priv = state;
543 return &state->frontend; 543 return &state->frontend;
544
545error:
546 kfree(state);
547 return NULL;
548} 544}
549 545
550static struct dvb_frontend_ops or51211_ops = { 546static struct dvb_frontend_ops or51211_ops = {
diff --git a/drivers/media/dvb/frontends/s5h1409.c b/drivers/media/dvb/frontends/s5h1409.c
index 562d9208857a..819433485d3b 100644
--- a/drivers/media/dvb/frontends/s5h1409.c
+++ b/drivers/media/dvb/frontends/s5h1409.c
@@ -42,6 +42,7 @@ struct s5h1409_state {
42 fe_modulation_t current_modulation; 42 fe_modulation_t current_modulation;
43 43
44 u32 current_frequency; 44 u32 current_frequency;
45 int if_freq;
45 46
46 u32 is_qam_locked; 47 u32 is_qam_locked;
47 u32 qam_state; 48 u32 qam_state;
@@ -97,7 +98,7 @@ static struct init_tab {
97 { 0xac, 0x1003, }, 98 { 0xac, 0x1003, },
98 { 0xad, 0x103f, }, 99 { 0xad, 0x103f, },
99 { 0xe2, 0x0100, }, 100 { 0xe2, 0x0100, },
100 { 0xe3, 0x0000, }, 101 { 0xe3, 0x1000, },
101 { 0x28, 0x1010, }, 102 { 0x28, 0x1010, },
102 { 0xb1, 0x000e, }, 103 { 0xb1, 0x000e, },
103}; 104};
@@ -348,28 +349,32 @@ static int s5h1409_softreset(struct dvb_frontend* fe)
348 return 0; 349 return 0;
349} 350}
350 351
352#define S5H1409_VSB_IF_FREQ 5380
353#define S5H1409_QAM_IF_FREQ state->config->qam_if
354
351static int s5h1409_set_if_freq(struct dvb_frontend* fe, int KHz) 355static int s5h1409_set_if_freq(struct dvb_frontend* fe, int KHz)
352{ 356{
353 struct s5h1409_state* state = fe->demodulator_priv; 357 struct s5h1409_state* state = fe->demodulator_priv;
354 int ret = 0;
355 358
356 dprintk("%s(%d KHz)\n", __FUNCTION__, KHz); 359 dprintk("%s(%d KHz)\n", __FUNCTION__, KHz);
357 360
358 if( (KHz == 44000) || (KHz == 5380) ) { 361 switch (KHz) {
359 s5h1409_writereg(state, 0x87, 0x01be); 362 case 4000:
360 s5h1409_writereg(state, 0x88, 0x0436);
361 s5h1409_writereg(state, 0x89, 0x054d);
362 } else
363 if (KHz == 4000) {
364 s5h1409_writereg(state, 0x87, 0x014b); 363 s5h1409_writereg(state, 0x87, 0x014b);
365 s5h1409_writereg(state, 0x88, 0x0cb5); 364 s5h1409_writereg(state, 0x88, 0x0cb5);
366 s5h1409_writereg(state, 0x89, 0x03e2); 365 s5h1409_writereg(state, 0x89, 0x03e2);
367 } else { 366 break;
368 printk("%s() Invalid arg = %d KHz\n", __FUNCTION__, KHz); 367 case 5380:
369 ret = -1; 368 case 44000:
369 default:
370 s5h1409_writereg(state, 0x87, 0x01be);
371 s5h1409_writereg(state, 0x88, 0x0436);
372 s5h1409_writereg(state, 0x89, 0x054d);
373 break;
370 } 374 }
375 state->if_freq = KHz;
371 376
372 return ret; 377 return 0;
373} 378}
374 379
375static int s5h1409_set_spectralinversion(struct dvb_frontend* fe, int inverted) 380static int s5h1409_set_spectralinversion(struct dvb_frontend* fe, int inverted)
@@ -394,11 +399,15 @@ static int s5h1409_enable_modulation(struct dvb_frontend* fe,
394 switch(m) { 399 switch(m) {
395 case VSB_8: 400 case VSB_8:
396 dprintk("%s() VSB_8\n", __FUNCTION__); 401 dprintk("%s() VSB_8\n", __FUNCTION__);
402 if (state->if_freq != S5H1409_VSB_IF_FREQ)
403 s5h1409_set_if_freq(fe, S5H1409_VSB_IF_FREQ);
397 s5h1409_writereg(state, 0xf4, 0); 404 s5h1409_writereg(state, 0xf4, 0);
398 break; 405 break;
399 case QAM_64: 406 case QAM_64:
400 case QAM_256: 407 case QAM_256:
401 dprintk("%s() QAM_AUTO (64/256)\n", __FUNCTION__); 408 dprintk("%s() QAM_AUTO (64/256)\n", __FUNCTION__);
409 if (state->if_freq != S5H1409_QAM_IF_FREQ)
410 s5h1409_set_if_freq(fe, S5H1409_QAM_IF_FREQ);
402 s5h1409_writereg(state, 0xf4, 1); 411 s5h1409_writereg(state, 0xf4, 1);
403 s5h1409_writereg(state, 0x85, 0x110); 412 s5h1409_writereg(state, 0x85, 0x110);
404 break; 413 break;
@@ -432,9 +441,11 @@ static int s5h1409_set_gpio(struct dvb_frontend* fe, int enable)
432 dprintk("%s(%d)\n", __FUNCTION__, enable); 441 dprintk("%s(%d)\n", __FUNCTION__, enable);
433 442
434 if (enable) 443 if (enable)
435 return s5h1409_writereg(state, 0xe3, 0x1100); 444 return s5h1409_writereg(state, 0xe3,
445 s5h1409_readreg(state, 0xe3) | 0x1100);
436 else 446 else
437 return s5h1409_writereg(state, 0xe3, 0x1000); 447 return s5h1409_writereg(state, 0xe3,
448 s5h1409_readreg(state, 0xe3) & 0xeeff);
438} 449}
439 450
440static int s5h1409_sleep(struct dvb_frontend* fe, int enable) 451static int s5h1409_sleep(struct dvb_frontend* fe, int enable)
@@ -504,13 +515,15 @@ static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
504 s5h1409_writereg(state, 0x96, 0x20); 515 s5h1409_writereg(state, 0x96, 0x20);
505 s5h1409_writereg(state, 0xad, 516 s5h1409_writereg(state, 0xad,
506 ( ((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)) ); 517 ( ((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)) );
507 s5h1409_writereg(state, 0xab, 0x1100); 518 s5h1409_writereg(state, 0xab,
519 s5h1409_readreg(state, 0xab) & 0xeffe);
508 } 520 }
509 } else { 521 } else {
510 if (state->qam_state != 1) { 522 if (state->qam_state != 1) {
511 state->qam_state = 1; 523 state->qam_state = 1;
512 s5h1409_writereg(state, 0x96, 0x08); 524 s5h1409_writereg(state, 0x96, 0x08);
513 s5h1409_writereg(state, 0xab, 0x1101); 525 s5h1409_writereg(state, 0xab,
526 s5h1409_readreg(state, 0xab) | 0x1001);
514 } 527 }
515 } 528 }
516} 529}
@@ -547,6 +560,36 @@ static int s5h1409_set_frontend (struct dvb_frontend* fe,
547 return 0; 560 return 0;
548} 561}
549 562
563static int s5h1409_set_mpeg_timing(struct dvb_frontend *fe, int mode)
564{
565 struct s5h1409_state *state = fe->demodulator_priv;
566 u16 val;
567
568 dprintk("%s(%d)\n", __FUNCTION__, mode);
569
570 val = s5h1409_readreg(state, 0xac) & 0xcfff;
571 switch (mode) {
572 case S5H1409_MPEGTIMING_CONTINOUS_INVERTING_CLOCK:
573 val |= 0x0000;
574 break;
575 case S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK:
576 dprintk("%s(%d) Mode1 or Defaulting\n", __FUNCTION__, mode);
577 val |= 0x1000;
578 break;
579 case S5H1409_MPEGTIMING_NONCONTINOUS_INVERTING_CLOCK:
580 val |= 0x2000;
581 break;
582 case S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK:
583 val |= 0x3000;
584 break;
585 default:
586 return -EINVAL;
587 }
588
589 /* Configure MPEG Signal Timing charactistics */
590 return s5h1409_writereg(state, 0xac, val);
591}
592
550/* Reset the demod hardware and reset all of the configuration registers 593/* Reset the demod hardware and reset all of the configuration registers
551 to a default state. */ 594 to a default state. */
552static int s5h1409_init (struct dvb_frontend* fe) 595static int s5h1409_init (struct dvb_frontend* fe)
@@ -566,13 +609,16 @@ static int s5h1409_init (struct dvb_frontend* fe)
566 state->current_modulation = VSB_8; 609 state->current_modulation = VSB_8;
567 610
568 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT) 611 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
569 s5h1409_writereg(state, 0xab, 0x100); /* Serial */ 612 s5h1409_writereg(state, 0xab,
613 s5h1409_readreg(state, 0xab) | 0x100); /* Serial */
570 else 614 else
571 s5h1409_writereg(state, 0xab, 0x0); /* Parallel */ 615 s5h1409_writereg(state, 0xab,
616 s5h1409_readreg(state, 0xab) & 0xfeff); /* Parallel */
572 617
573 s5h1409_set_spectralinversion(fe, state->config->inversion); 618 s5h1409_set_spectralinversion(fe, state->config->inversion);
574 s5h1409_set_if_freq(fe, state->config->if_freq); 619 s5h1409_set_if_freq(fe, state->if_freq);
575 s5h1409_set_gpio(fe, state->config->gpio); 620 s5h1409_set_gpio(fe, state->config->gpio);
621 s5h1409_set_mpeg_timing(fe, state->config->mpeg_timing);
576 s5h1409_softreset(fe); 622 s5h1409_softreset(fe);
577 623
578 /* Note: Leaving the I2C gate closed. */ 624 /* Note: Leaving the I2C gate closed. */
@@ -741,6 +787,7 @@ struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
741 struct i2c_adapter* i2c) 787 struct i2c_adapter* i2c)
742{ 788{
743 struct s5h1409_state* state = NULL; 789 struct s5h1409_state* state = NULL;
790 u16 reg;
744 791
745 /* allocate memory for the internal state */ 792 /* allocate memory for the internal state */
746 state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL); 793 state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
@@ -751,9 +798,11 @@ struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
751 state->config = config; 798 state->config = config;
752 state->i2c = i2c; 799 state->i2c = i2c;
753 state->current_modulation = 0; 800 state->current_modulation = 0;
801 state->if_freq = S5H1409_VSB_IF_FREQ;
754 802
755 /* check if the demod exists */ 803 /* check if the demod exists */
756 if (s5h1409_readreg(state, 0x04) != 0x0066) 804 reg = s5h1409_readreg(state, 0x04);
805 if ((reg != 0x0066) && (reg != 0x007f))
757 goto error; 806 goto error;
758 807
759 /* create dvb_frontend */ 808 /* create dvb_frontend */
@@ -761,8 +810,14 @@ struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
761 sizeof(struct dvb_frontend_ops)); 810 sizeof(struct dvb_frontend_ops));
762 state->frontend.demodulator_priv = state; 811 state->frontend.demodulator_priv = state;
763 812
813 if (s5h1409_init(&state->frontend) != 0) {
814 printk(KERN_ERR "%s: Failed to initialize correctly\n",
815 __FUNCTION__);
816 goto error;
817 }
818
764 /* Note: Leaving the I2C gate open here. */ 819 /* Note: Leaving the I2C gate open here. */
765 s5h1409_writereg(state, 0xf3, 1); 820 s5h1409_i2c_gate_ctrl(&state->frontend, 1);
766 821
767 return &state->frontend; 822 return &state->frontend;
768 823
diff --git a/drivers/media/dvb/frontends/s5h1409.h b/drivers/media/dvb/frontends/s5h1409.h
index 20f9af1af445..f0bb13fe808b 100644
--- a/drivers/media/dvb/frontends/s5h1409.h
+++ b/drivers/media/dvb/frontends/s5h1409.h
@@ -39,8 +39,8 @@ struct s5h1409_config
39#define S5H1409_GPIO_ON 1 39#define S5H1409_GPIO_ON 1
40 u8 gpio; 40 u8 gpio;
41 41
42 /* IF Freq in KHz */ 42 /* IF Freq for QAM in KHz, VSB is hardcoded to 5380 */
43 u16 if_freq; 43 u16 qam_if;
44 44
45 /* Spectral Inversion */ 45 /* Spectral Inversion */
46#define S5H1409_INVERSION_OFF 0 46#define S5H1409_INVERSION_OFF 0
@@ -51,6 +51,13 @@ struct s5h1409_config
51#define S5H1409_TUNERLOCKING 0 51#define S5H1409_TUNERLOCKING 0
52#define S5H1409_DEMODLOCKING 1 52#define S5H1409_DEMODLOCKING 1
53 u8 status_mode; 53 u8 status_mode;
54
55 /* MPEG signal timing */
56#define S5H1409_MPEGTIMING_CONTINOUS_INVERTING_CLOCK 0
57#define S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK 1
58#define S5H1409_MPEGTIMING_NONCONTINOUS_INVERTING_CLOCK 2
59#define S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK 3
60 u16 mpeg_timing;
54}; 61};
55 62
56#if defined(CONFIG_DVB_S5H1409) || (defined(CONFIG_DVB_S5H1409_MODULE) && defined(MODULE)) 63#if defined(CONFIG_DVB_S5H1409) || (defined(CONFIG_DVB_S5H1409_MODULE) && defined(MODULE))
diff --git a/drivers/media/dvb/frontends/tda18271-common.c b/drivers/media/dvb/frontends/tda18271-common.c
new file mode 100644
index 000000000000..cebb6b90b7e0
--- /dev/null
+++ b/drivers/media/dvb/frontends/tda18271-common.c
@@ -0,0 +1,653 @@
1/*
2 tda18271-common.c - driver for the Philips / NXP TDA18271 silicon tuner
3
4 Copyright (C) 2007, 2008 Michael Krufky <mkrufky@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 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#include "tda18271-priv.h"
22
23static int tda18271_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
24{
25 struct tda18271_priv *priv = fe->tuner_priv;
26 enum tda18271_i2c_gate gate;
27 int ret = 0;
28
29 switch (priv->gate) {
30 case TDA18271_GATE_DIGITAL:
31 case TDA18271_GATE_ANALOG:
32 gate = priv->gate;
33 break;
34 case TDA18271_GATE_AUTO:
35 default:
36 switch (priv->mode) {
37 case TDA18271_DIGITAL:
38 gate = TDA18271_GATE_DIGITAL;
39 break;
40 case TDA18271_ANALOG:
41 default:
42 gate = TDA18271_GATE_ANALOG;
43 break;
44 }
45 }
46
47 switch (gate) {
48 case TDA18271_GATE_ANALOG:
49 if (fe->ops.analog_ops.i2c_gate_ctrl)
50 ret = fe->ops.analog_ops.i2c_gate_ctrl(fe, enable);
51 break;
52 case TDA18271_GATE_DIGITAL:
53 if (fe->ops.i2c_gate_ctrl)
54 ret = fe->ops.i2c_gate_ctrl(fe, enable);
55 break;
56 default:
57 ret = -EINVAL;
58 break;
59 }
60
61 return ret;
62};
63
64/*---------------------------------------------------------------------*/
65
66static void tda18271_dump_regs(struct dvb_frontend *fe, int extended)
67{
68 struct tda18271_priv *priv = fe->tuner_priv;
69 unsigned char *regs = priv->tda18271_regs;
70
71 tda_reg("=== TDA18271 REG DUMP ===\n");
72 tda_reg("ID_BYTE = 0x%02x\n", 0xff & regs[R_ID]);
73 tda_reg("THERMO_BYTE = 0x%02x\n", 0xff & regs[R_TM]);
74 tda_reg("POWER_LEVEL_BYTE = 0x%02x\n", 0xff & regs[R_PL]);
75 tda_reg("EASY_PROG_BYTE_1 = 0x%02x\n", 0xff & regs[R_EP1]);
76 tda_reg("EASY_PROG_BYTE_2 = 0x%02x\n", 0xff & regs[R_EP2]);
77 tda_reg("EASY_PROG_BYTE_3 = 0x%02x\n", 0xff & regs[R_EP3]);
78 tda_reg("EASY_PROG_BYTE_4 = 0x%02x\n", 0xff & regs[R_EP4]);
79 tda_reg("EASY_PROG_BYTE_5 = 0x%02x\n", 0xff & regs[R_EP5]);
80 tda_reg("CAL_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_CPD]);
81 tda_reg("CAL_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_CD1]);
82 tda_reg("CAL_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_CD2]);
83 tda_reg("CAL_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_CD3]);
84 tda_reg("MAIN_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_MPD]);
85 tda_reg("MAIN_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_MD1]);
86 tda_reg("MAIN_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_MD2]);
87 tda_reg("MAIN_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_MD3]);
88
89 /* only dump extended regs if DBG_ADV is set */
90 if (!(tda18271_debug & DBG_ADV))
91 return;
92
93 /* W indicates write-only registers.
94 * Register dump for write-only registers shows last value written. */
95
96 tda_reg("EXTENDED_BYTE_1 = 0x%02x\n", 0xff & regs[R_EB1]);
97 tda_reg("EXTENDED_BYTE_2 = 0x%02x\n", 0xff & regs[R_EB2]);
98 tda_reg("EXTENDED_BYTE_3 = 0x%02x\n", 0xff & regs[R_EB3]);
99 tda_reg("EXTENDED_BYTE_4 = 0x%02x\n", 0xff & regs[R_EB4]);
100 tda_reg("EXTENDED_BYTE_5 = 0x%02x\n", 0xff & regs[R_EB5]);
101 tda_reg("EXTENDED_BYTE_6 = 0x%02x\n", 0xff & regs[R_EB6]);
102 tda_reg("EXTENDED_BYTE_7 = 0x%02x\n", 0xff & regs[R_EB7]);
103 tda_reg("EXTENDED_BYTE_8 = 0x%02x\n", 0xff & regs[R_EB8]);
104 tda_reg("EXTENDED_BYTE_9 W = 0x%02x\n", 0xff & regs[R_EB9]);
105 tda_reg("EXTENDED_BYTE_10 = 0x%02x\n", 0xff & regs[R_EB10]);
106 tda_reg("EXTENDED_BYTE_11 = 0x%02x\n", 0xff & regs[R_EB11]);
107 tda_reg("EXTENDED_BYTE_12 = 0x%02x\n", 0xff & regs[R_EB12]);
108 tda_reg("EXTENDED_BYTE_13 = 0x%02x\n", 0xff & regs[R_EB13]);
109 tda_reg("EXTENDED_BYTE_14 = 0x%02x\n", 0xff & regs[R_EB14]);
110 tda_reg("EXTENDED_BYTE_15 = 0x%02x\n", 0xff & regs[R_EB15]);
111 tda_reg("EXTENDED_BYTE_16 W = 0x%02x\n", 0xff & regs[R_EB16]);
112 tda_reg("EXTENDED_BYTE_17 W = 0x%02x\n", 0xff & regs[R_EB17]);
113 tda_reg("EXTENDED_BYTE_18 = 0x%02x\n", 0xff & regs[R_EB18]);
114 tda_reg("EXTENDED_BYTE_19 W = 0x%02x\n", 0xff & regs[R_EB19]);
115 tda_reg("EXTENDED_BYTE_20 W = 0x%02x\n", 0xff & regs[R_EB20]);
116 tda_reg("EXTENDED_BYTE_21 = 0x%02x\n", 0xff & regs[R_EB21]);
117 tda_reg("EXTENDED_BYTE_22 = 0x%02x\n", 0xff & regs[R_EB22]);
118 tda_reg("EXTENDED_BYTE_23 = 0x%02x\n", 0xff & regs[R_EB23]);
119}
120
121int tda18271_read_regs(struct dvb_frontend *fe)
122{
123 struct tda18271_priv *priv = fe->tuner_priv;
124 unsigned char *regs = priv->tda18271_regs;
125 unsigned char buf = 0x00;
126 int ret;
127 struct i2c_msg msg[] = {
128 { .addr = priv->i2c_addr, .flags = 0,
129 .buf = &buf, .len = 1 },
130 { .addr = priv->i2c_addr, .flags = I2C_M_RD,
131 .buf = regs, .len = 16 }
132 };
133
134 tda18271_i2c_gate_ctrl(fe, 1);
135
136 /* read all registers */
137 ret = i2c_transfer(priv->i2c_adap, msg, 2);
138
139 tda18271_i2c_gate_ctrl(fe, 0);
140
141 if (ret != 2)
142 tda_err("ERROR: i2c_transfer returned: %d\n", ret);
143
144 if (tda18271_debug & DBG_REG)
145 tda18271_dump_regs(fe, 0);
146
147 return (ret == 2 ? 0 : ret);
148}
149
150int tda18271_read_extended(struct dvb_frontend *fe)
151{
152 struct tda18271_priv *priv = fe->tuner_priv;
153 unsigned char *regs = priv->tda18271_regs;
154 unsigned char regdump[TDA18271_NUM_REGS];
155 unsigned char buf = 0x00;
156 int ret, i;
157 struct i2c_msg msg[] = {
158 { .addr = priv->i2c_addr, .flags = 0,
159 .buf = &buf, .len = 1 },
160 { .addr = priv->i2c_addr, .flags = I2C_M_RD,
161 .buf = regdump, .len = TDA18271_NUM_REGS }
162 };
163
164 tda18271_i2c_gate_ctrl(fe, 1);
165
166 /* read all registers */
167 ret = i2c_transfer(priv->i2c_adap, msg, 2);
168
169 tda18271_i2c_gate_ctrl(fe, 0);
170
171 if (ret != 2)
172 tda_err("ERROR: i2c_transfer returned: %d\n", ret);
173
174 for (i = 0; i <= TDA18271_NUM_REGS; i++) {
175 /* don't update write-only registers */
176 if ((i != R_EB9) &&
177 (i != R_EB16) &&
178 (i != R_EB17) &&
179 (i != R_EB19) &&
180 (i != R_EB20))
181 regs[i] = regdump[i];
182 }
183
184 if (tda18271_debug & DBG_REG)
185 tda18271_dump_regs(fe, 1);
186
187 return (ret == 2 ? 0 : ret);
188}
189
190int tda18271_write_regs(struct dvb_frontend *fe, int idx, int len)
191{
192 struct tda18271_priv *priv = fe->tuner_priv;
193 unsigned char *regs = priv->tda18271_regs;
194 unsigned char buf[TDA18271_NUM_REGS + 1];
195 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
196 .buf = buf, .len = len + 1 };
197 int i, ret;
198
199 BUG_ON((len == 0) || (idx + len > sizeof(buf)));
200
201 buf[0] = idx;
202 for (i = 1; i <= len; i++)
203 buf[i] = regs[idx - 1 + i];
204
205 tda18271_i2c_gate_ctrl(fe, 1);
206
207 /* write registers */
208 ret = i2c_transfer(priv->i2c_adap, &msg, 1);
209
210 tda18271_i2c_gate_ctrl(fe, 0);
211
212 if (ret != 1)
213 tda_err("ERROR: i2c_transfer returned: %d\n", ret);
214
215 return (ret == 1 ? 0 : ret);
216}
217
218/*---------------------------------------------------------------------*/
219
220int tda18271_init_regs(struct dvb_frontend *fe)
221{
222 struct tda18271_priv *priv = fe->tuner_priv;
223 unsigned char *regs = priv->tda18271_regs;
224
225 tda_dbg("initializing registers for device @ %d-%04x\n",
226 i2c_adapter_id(priv->i2c_adap), priv->i2c_addr);
227
228 /* initialize registers */
229 switch (priv->id) {
230 case TDA18271HDC1:
231 regs[R_ID] = 0x83;
232 break;
233 case TDA18271HDC2:
234 regs[R_ID] = 0x84;
235 break;
236 };
237
238 regs[R_TM] = 0x08;
239 regs[R_PL] = 0x80;
240 regs[R_EP1] = 0xc6;
241 regs[R_EP2] = 0xdf;
242 regs[R_EP3] = 0x16;
243 regs[R_EP4] = 0x60;
244 regs[R_EP5] = 0x80;
245 regs[R_CPD] = 0x80;
246 regs[R_CD1] = 0x00;
247 regs[R_CD2] = 0x00;
248 regs[R_CD3] = 0x00;
249 regs[R_MPD] = 0x00;
250 regs[R_MD1] = 0x00;
251 regs[R_MD2] = 0x00;
252 regs[R_MD3] = 0x00;
253
254 switch (priv->id) {
255 case TDA18271HDC1:
256 regs[R_EB1] = 0xff;
257 break;
258 case TDA18271HDC2:
259 regs[R_EB1] = 0xfc;
260 break;
261 };
262
263 regs[R_EB2] = 0x01;
264 regs[R_EB3] = 0x84;
265 regs[R_EB4] = 0x41;
266 regs[R_EB5] = 0x01;
267 regs[R_EB6] = 0x84;
268 regs[R_EB7] = 0x40;
269 regs[R_EB8] = 0x07;
270 regs[R_EB9] = 0x00;
271 regs[R_EB10] = 0x00;
272 regs[R_EB11] = 0x96;
273
274 switch (priv->id) {
275 case TDA18271HDC1:
276 regs[R_EB12] = 0x0f;
277 break;
278 case TDA18271HDC2:
279 regs[R_EB12] = 0x33;
280 break;
281 };
282
283 regs[R_EB13] = 0xc1;
284 regs[R_EB14] = 0x00;
285 regs[R_EB15] = 0x8f;
286 regs[R_EB16] = 0x00;
287 regs[R_EB17] = 0x00;
288
289 switch (priv->id) {
290 case TDA18271HDC1:
291 regs[R_EB18] = 0x00;
292 break;
293 case TDA18271HDC2:
294 regs[R_EB18] = 0x8c;
295 break;
296 };
297
298 regs[R_EB19] = 0x00;
299 regs[R_EB20] = 0x20;
300
301 switch (priv->id) {
302 case TDA18271HDC1:
303 regs[R_EB21] = 0x33;
304 break;
305 case TDA18271HDC2:
306 regs[R_EB21] = 0xb3;
307 break;
308 };
309
310 regs[R_EB22] = 0x48;
311 regs[R_EB23] = 0xb0;
312
313 tda18271_write_regs(fe, 0x00, TDA18271_NUM_REGS);
314
315 /* setup agc1 gain */
316 regs[R_EB17] = 0x00;
317 tda18271_write_regs(fe, R_EB17, 1);
318 regs[R_EB17] = 0x03;
319 tda18271_write_regs(fe, R_EB17, 1);
320 regs[R_EB17] = 0x43;
321 tda18271_write_regs(fe, R_EB17, 1);
322 regs[R_EB17] = 0x4c;
323 tda18271_write_regs(fe, R_EB17, 1);
324
325 /* setup agc2 gain */
326 if ((priv->id) == TDA18271HDC1) {
327 regs[R_EB20] = 0xa0;
328 tda18271_write_regs(fe, R_EB20, 1);
329 regs[R_EB20] = 0xa7;
330 tda18271_write_regs(fe, R_EB20, 1);
331 regs[R_EB20] = 0xe7;
332 tda18271_write_regs(fe, R_EB20, 1);
333 regs[R_EB20] = 0xec;
334 tda18271_write_regs(fe, R_EB20, 1);
335 }
336
337 /* image rejection calibration */
338
339 /* low-band */
340 regs[R_EP3] = 0x1f;
341 regs[R_EP4] = 0x66;
342 regs[R_EP5] = 0x81;
343 regs[R_CPD] = 0xcc;
344 regs[R_CD1] = 0x6c;
345 regs[R_CD2] = 0x00;
346 regs[R_CD3] = 0x00;
347 regs[R_MPD] = 0xcd;
348 regs[R_MD1] = 0x77;
349 regs[R_MD2] = 0x08;
350 regs[R_MD3] = 0x00;
351
352 switch (priv->id) {
353 case TDA18271HDC1:
354 tda18271_write_regs(fe, R_EP3, 11);
355 break;
356 case TDA18271HDC2:
357 tda18271_write_regs(fe, R_EP3, 12);
358 break;
359 };
360
361 if ((priv->id) == TDA18271HDC2) {
362 /* main pll cp source on */
363 regs[R_EB4] = 0x61;
364 tda18271_write_regs(fe, R_EB4, 1);
365 msleep(1);
366
367 /* main pll cp source off */
368 regs[R_EB4] = 0x41;
369 tda18271_write_regs(fe, R_EB4, 1);
370 }
371
372 msleep(5); /* pll locking */
373
374 /* launch detector */
375 tda18271_write_regs(fe, R_EP1, 1);
376 msleep(5); /* wanted low measurement */
377
378 regs[R_EP5] = 0x85;
379 regs[R_CPD] = 0xcb;
380 regs[R_CD1] = 0x66;
381 regs[R_CD2] = 0x70;
382
383 tda18271_write_regs(fe, R_EP3, 7);
384 msleep(5); /* pll locking */
385
386 /* launch optimization algorithm */
387 tda18271_write_regs(fe, R_EP2, 1);
388 msleep(30); /* image low optimization completion */
389
390 /* mid-band */
391 regs[R_EP5] = 0x82;
392 regs[R_CPD] = 0xa8;
393 regs[R_CD2] = 0x00;
394 regs[R_MPD] = 0xa9;
395 regs[R_MD1] = 0x73;
396 regs[R_MD2] = 0x1a;
397
398 tda18271_write_regs(fe, R_EP3, 11);
399 msleep(5); /* pll locking */
400
401 tda18271_write_regs(fe, R_EP1, 1);
402 msleep(5); /* wanted mid measurement */
403
404 regs[R_EP5] = 0x86;
405 regs[R_CPD] = 0xa8;
406 regs[R_CD1] = 0x66;
407 regs[R_CD2] = 0xa0;
408
409 tda18271_write_regs(fe, R_EP3, 7);
410 msleep(5); /* pll locking */
411
412 /* launch optimization algorithm */
413 tda18271_write_regs(fe, R_EP2, 1);
414 msleep(30); /* image mid optimization completion */
415
416 /* high-band */
417 regs[R_EP5] = 0x83;
418 regs[R_CPD] = 0x98;
419 regs[R_CD1] = 0x65;
420 regs[R_CD2] = 0x00;
421 regs[R_MPD] = 0x99;
422 regs[R_MD1] = 0x71;
423 regs[R_MD2] = 0xcd;
424
425 tda18271_write_regs(fe, R_EP3, 11);
426 msleep(5); /* pll locking */
427
428 /* launch detector */
429 tda18271_write_regs(fe, R_EP1, 1);
430 msleep(5); /* wanted high measurement */
431
432 regs[R_EP5] = 0x87;
433 regs[R_CD1] = 0x65;
434 regs[R_CD2] = 0x50;
435
436 tda18271_write_regs(fe, R_EP3, 7);
437 msleep(5); /* pll locking */
438
439 /* launch optimization algorithm */
440 tda18271_write_regs(fe, R_EP2, 1);
441 msleep(30); /* image high optimization completion */
442
443 /* return to normal mode */
444 regs[R_EP4] = 0x64;
445 tda18271_write_regs(fe, R_EP4, 1);
446
447 /* synchronize */
448 tda18271_write_regs(fe, R_EP1, 1);
449
450 return 0;
451}
452
453/*---------------------------------------------------------------------*/
454
455/*
456 * Standby modes, EP3 [7:5]
457 *
458 * | SM || SM_LT || SM_XT || mode description
459 * |=====\\=======\\=======\\===================================
460 * | 0 || 0 || 0 || normal mode
461 * |-----||-------||-------||-----------------------------------
462 * | || || || standby mode w/ slave tuner output
463 * | 1 || 0 || 0 || & loop thru & xtal oscillator on
464 * |-----||-------||-------||-----------------------------------
465 * | 1 || 1 || 0 || standby mode w/ xtal oscillator on
466 * |-----||-------||-------||-----------------------------------
467 * | 1 || 1 || 1 || power off
468 *
469 */
470
471int tda18271_set_standby_mode(struct dvb_frontend *fe,
472 int sm, int sm_lt, int sm_xt)
473{
474 struct tda18271_priv *priv = fe->tuner_priv;
475 unsigned char *regs = priv->tda18271_regs;
476
477 tda_dbg("sm = %d, sm_lt = %d, sm_xt = %d\n", sm, sm_lt, sm_xt);
478
479 regs[R_EP3] &= ~0xe0; /* clear sm, sm_lt, sm_xt */
480 regs[R_EP3] |= sm ? (1 << 7) : 0 |
481 sm_lt ? (1 << 6) : 0 |
482 sm_xt ? (1 << 5) : 0;
483
484 tda18271_write_regs(fe, R_EP3, 1);
485
486 return 0;
487}
488
489/*---------------------------------------------------------------------*/
490
491int tda18271_calc_main_pll(struct dvb_frontend *fe, u32 freq)
492{
493 /* sets main post divider & divider bytes, but does not write them */
494 struct tda18271_priv *priv = fe->tuner_priv;
495 unsigned char *regs = priv->tda18271_regs;
496 u8 d, pd;
497 u32 div;
498
499 int ret = tda18271_lookup_pll_map(fe, MAIN_PLL, &freq, &pd, &d);
500 if (ret < 0)
501 goto fail;
502
503 regs[R_MPD] = (0x77 & pd);
504
505 switch (priv->mode) {
506 case TDA18271_ANALOG:
507 regs[R_MPD] &= ~0x08;
508 break;
509 case TDA18271_DIGITAL:
510 regs[R_MPD] |= 0x08;
511 break;
512 }
513
514 div = ((d * (freq / 1000)) << 7) / 125;
515
516 regs[R_MD1] = 0x7f & (div >> 16);
517 regs[R_MD2] = 0xff & (div >> 8);
518 regs[R_MD3] = 0xff & div;
519fail:
520 return ret;
521}
522
523int tda18271_calc_cal_pll(struct dvb_frontend *fe, u32 freq)
524{
525 /* sets cal post divider & divider bytes, but does not write them */
526 struct tda18271_priv *priv = fe->tuner_priv;
527 unsigned char *regs = priv->tda18271_regs;
528 u8 d, pd;
529 u32 div;
530
531 int ret = tda18271_lookup_pll_map(fe, CAL_PLL, &freq, &pd, &d);
532 if (ret < 0)
533 goto fail;
534
535 regs[R_CPD] = pd;
536
537 div = ((d * (freq / 1000)) << 7) / 125;
538
539 regs[R_CD1] = 0x7f & (div >> 16);
540 regs[R_CD2] = 0xff & (div >> 8);
541 regs[R_CD3] = 0xff & div;
542fail:
543 return ret;
544}
545
546/*---------------------------------------------------------------------*/
547
548int tda18271_calc_bp_filter(struct dvb_frontend *fe, u32 *freq)
549{
550 /* sets bp filter bits, but does not write them */
551 struct tda18271_priv *priv = fe->tuner_priv;
552 unsigned char *regs = priv->tda18271_regs;
553 u8 val;
554
555 int ret = tda18271_lookup_map(fe, BP_FILTER, freq, &val);
556 if (ret < 0)
557 goto fail;
558
559 regs[R_EP1] &= ~0x07; /* clear bp filter bits */
560 regs[R_EP1] |= (0x07 & val);
561fail:
562 return ret;
563}
564
565int tda18271_calc_km(struct dvb_frontend *fe, u32 *freq)
566{
567 /* sets K & M bits, but does not write them */
568 struct tda18271_priv *priv = fe->tuner_priv;
569 unsigned char *regs = priv->tda18271_regs;
570 u8 val;
571
572 int ret = tda18271_lookup_map(fe, RF_CAL_KMCO, freq, &val);
573 if (ret < 0)
574 goto fail;
575
576 regs[R_EB13] &= ~0x7c; /* clear k & m bits */
577 regs[R_EB13] |= (0x7c & val);
578fail:
579 return ret;
580}
581
582int tda18271_calc_rf_band(struct dvb_frontend *fe, u32 *freq)
583{
584 /* sets rf band bits, but does not write them */
585 struct tda18271_priv *priv = fe->tuner_priv;
586 unsigned char *regs = priv->tda18271_regs;
587 u8 val;
588
589 int ret = tda18271_lookup_map(fe, RF_BAND, freq, &val);
590 if (ret < 0)
591 goto fail;
592
593 regs[R_EP2] &= ~0xe0; /* clear rf band bits */
594 regs[R_EP2] |= (0xe0 & (val << 5));
595fail:
596 return ret;
597}
598
599int tda18271_calc_gain_taper(struct dvb_frontend *fe, u32 *freq)
600{
601 /* sets gain taper bits, but does not write them */
602 struct tda18271_priv *priv = fe->tuner_priv;
603 unsigned char *regs = priv->tda18271_regs;
604 u8 val;
605
606 int ret = tda18271_lookup_map(fe, GAIN_TAPER, freq, &val);
607 if (ret < 0)
608 goto fail;
609
610 regs[R_EP2] &= ~0x1f; /* clear gain taper bits */
611 regs[R_EP2] |= (0x1f & val);
612fail:
613 return ret;
614}
615
616int tda18271_calc_ir_measure(struct dvb_frontend *fe, u32 *freq)
617{
618 /* sets IR Meas bits, but does not write them */
619 struct tda18271_priv *priv = fe->tuner_priv;
620 unsigned char *regs = priv->tda18271_regs;
621 u8 val;
622
623 int ret = tda18271_lookup_map(fe, IR_MEASURE, freq, &val);
624 if (ret < 0)
625 goto fail;
626
627 regs[R_EP5] &= ~0x07;
628 regs[R_EP5] |= (0x07 & val);
629fail:
630 return ret;
631}
632
633int tda18271_calc_rf_cal(struct dvb_frontend *fe, u32 *freq)
634{
635 /* sets rf cal byte (RFC_Cprog), but does not write it */
636 struct tda18271_priv *priv = fe->tuner_priv;
637 unsigned char *regs = priv->tda18271_regs;
638 u8 val;
639
640 tda18271_lookup_map(fe, RF_CAL, freq, &val);
641
642 regs[R_EB14] = val;
643
644 return 0;
645}
646
647/*
648 * Overrides for Emacs so that we follow Linus's tabbing style.
649 * ---------------------------------------------------------------------------
650 * Local variables:
651 * c-basic-offset: 8
652 * End:
653 */
diff --git a/drivers/media/dvb/frontends/tda18271-fe.c b/drivers/media/dvb/frontends/tda18271-fe.c
new file mode 100644
index 000000000000..dfe72aaec380
--- /dev/null
+++ b/drivers/media/dvb/frontends/tda18271-fe.c
@@ -0,0 +1,1225 @@
1/*
2 tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
3
4 Copyright (C) 2007, 2008 Michael Krufky <mkrufky@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 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#include <linux/delay.h>
22#include <linux/videodev2.h>
23#include "tda18271-priv.h"
24
25int tda18271_debug;
26module_param_named(debug, tda18271_debug, int, 0644);
27MODULE_PARM_DESC(debug, "set debug level "
28 "(info=1, map=2, reg=4, adv=8, cal=16 (or-able))");
29
30static int tda18271_cal_on_startup;
31module_param_named(cal, tda18271_cal_on_startup, int, 0644);
32MODULE_PARM_DESC(cal, "perform RF tracking filter calibration on startup");
33
34static LIST_HEAD(tda18271_list);
35static DEFINE_MUTEX(tda18271_list_mutex);
36
37/*---------------------------------------------------------------------*/
38
39static int tda18271_ir_cal_init(struct dvb_frontend *fe)
40{
41 struct tda18271_priv *priv = fe->tuner_priv;
42 unsigned char *regs = priv->tda18271_regs;
43
44 tda18271_read_regs(fe);
45
46 /* test IR_CAL_OK to see if we need init */
47 if ((regs[R_EP1] & 0x08) == 0)
48 tda18271_init_regs(fe);
49
50 return 0;
51}
52
53/* ------------------------------------------------------------------ */
54
55static int tda18271_channel_configuration(struct dvb_frontend *fe,
56 u32 ifc, u32 freq, u32 bw, u8 std,
57 int radio)
58{
59 struct tda18271_priv *priv = fe->tuner_priv;
60 unsigned char *regs = priv->tda18271_regs;
61 u32 N;
62
63 /* update TV broadcast parameters */
64
65 /* set standard */
66 regs[R_EP3] &= ~0x1f; /* clear std bits */
67 regs[R_EP3] |= std;
68
69 /* set cal mode to normal */
70 regs[R_EP4] &= ~0x03;
71
72 /* update IF output level & IF notch frequency */
73 regs[R_EP4] &= ~0x1c; /* clear if level bits */
74
75 switch (priv->mode) {
76 case TDA18271_ANALOG:
77 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
78 break;
79 case TDA18271_DIGITAL:
80 regs[R_EP4] |= 0x04; /* IF level = 1 */
81 regs[R_MPD] |= 0x80; /* IF notch = 1 */
82 break;
83 }
84
85 if (radio)
86 regs[R_EP4] |= 0x80;
87 else
88 regs[R_EP4] &= ~0x80;
89
90 /* update RF_TOP / IF_TOP */
91 switch (priv->mode) {
92 case TDA18271_ANALOG:
93 regs[R_EB22] = 0x2c;
94 break;
95 case TDA18271_DIGITAL:
96 regs[R_EB22] = 0x37;
97 break;
98 }
99 tda18271_write_regs(fe, R_EB22, 1);
100
101 /* --------------------------------------------------------------- */
102
103 /* disable Power Level Indicator */
104 regs[R_EP1] |= 0x40;
105
106 /* frequency dependent parameters */
107
108 tda18271_calc_ir_measure(fe, &freq);
109
110 tda18271_calc_bp_filter(fe, &freq);
111
112 tda18271_calc_rf_band(fe, &freq);
113
114 tda18271_calc_gain_taper(fe, &freq);
115
116 /* --------------------------------------------------------------- */
117
118 /* dual tuner and agc1 extra configuration */
119
120 /* main vco when Master, cal vco when slave */
121 regs[R_EB1] |= 0x04; /* FIXME: assumes master */
122
123 /* agc1 always active */
124 regs[R_EB1] &= ~0x02;
125
126 /* agc1 has priority on agc2 */
127 regs[R_EB1] &= ~0x01;
128
129 tda18271_write_regs(fe, R_EB1, 1);
130
131 /* --------------------------------------------------------------- */
132
133 N = freq + ifc;
134
135 /* FIXME: assumes master */
136 tda18271_calc_main_pll(fe, N);
137 tda18271_write_regs(fe, R_MPD, 4);
138
139 tda18271_write_regs(fe, R_TM, 7);
140
141 /* main pll charge pump source */
142 regs[R_EB4] |= 0x20;
143 tda18271_write_regs(fe, R_EB4, 1);
144
145 msleep(1);
146
147 /* normal operation for the main pll */
148 regs[R_EB4] &= ~0x20;
149 tda18271_write_regs(fe, R_EB4, 1);
150
151 msleep(5);
152
153 return 0;
154}
155
156static int tda18271_read_thermometer(struct dvb_frontend *fe)
157{
158 struct tda18271_priv *priv = fe->tuner_priv;
159 unsigned char *regs = priv->tda18271_regs;
160 int tm;
161
162 /* switch thermometer on */
163 regs[R_TM] |= 0x10;
164 tda18271_write_regs(fe, R_TM, 1);
165
166 /* read thermometer info */
167 tda18271_read_regs(fe);
168
169 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
170 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
171
172 if ((regs[R_TM] & 0x20) == 0x20)
173 regs[R_TM] &= ~0x20;
174 else
175 regs[R_TM] |= 0x20;
176
177 tda18271_write_regs(fe, R_TM, 1);
178
179 msleep(10); /* temperature sensing */
180
181 /* read thermometer info */
182 tda18271_read_regs(fe);
183 }
184
185 tm = tda18271_lookup_thermometer(fe);
186
187 /* switch thermometer off */
188 regs[R_TM] &= ~0x10;
189 tda18271_write_regs(fe, R_TM, 1);
190
191 /* set CAL mode to normal */
192 regs[R_EP4] &= ~0x03;
193 tda18271_write_regs(fe, R_EP4, 1);
194
195 return tm;
196}
197
198static int tda18271_rf_tracking_filters_correction(struct dvb_frontend *fe,
199 u32 freq)
200{
201 struct tda18271_priv *priv = fe->tuner_priv;
202 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
203 unsigned char *regs = priv->tda18271_regs;
204 int tm_current, rfcal_comp, approx, i;
205 u8 dc_over_dt, rf_tab;
206
207 /* power up */
208 tda18271_set_standby_mode(fe, 0, 0, 0);
209
210 /* read die current temperature */
211 tm_current = tda18271_read_thermometer(fe);
212
213 /* frequency dependent parameters */
214
215 tda18271_calc_rf_cal(fe, &freq);
216 rf_tab = regs[R_EB14];
217
218 i = tda18271_lookup_rf_band(fe, &freq, NULL);
219 if (i < 0)
220 return -EINVAL;
221
222 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
223 approx = map[i].rf_a1 *
224 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
225 } else {
226 approx = map[i].rf_a2 *
227 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
228 }
229
230 if (approx < 0)
231 approx = 0;
232 if (approx > 255)
233 approx = 255;
234
235 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
236
237 /* calculate temperature compensation */
238 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
239
240 regs[R_EB14] = approx + rfcal_comp;
241 tda18271_write_regs(fe, R_EB14, 1);
242
243 return 0;
244}
245
246static int tda18271_por(struct dvb_frontend *fe)
247{
248 struct tda18271_priv *priv = fe->tuner_priv;
249 unsigned char *regs = priv->tda18271_regs;
250
251 /* power up detector 1 */
252 regs[R_EB12] &= ~0x20;
253 tda18271_write_regs(fe, R_EB12, 1);
254
255 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
256 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
257 tda18271_write_regs(fe, R_EB18, 1);
258
259 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
260
261 /* POR mode */
262 tda18271_set_standby_mode(fe, 1, 0, 0);
263
264 /* disable 1.5 MHz low pass filter */
265 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
266 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
267 tda18271_write_regs(fe, R_EB21, 3);
268
269 return 0;
270}
271
272static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
273{
274 struct tda18271_priv *priv = fe->tuner_priv;
275 unsigned char *regs = priv->tda18271_regs;
276 u32 N;
277
278 /* set CAL mode to normal */
279 regs[R_EP4] &= ~0x03;
280 tda18271_write_regs(fe, R_EP4, 1);
281
282 /* switch off agc1 */
283 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
284
285 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
286 tda18271_write_regs(fe, R_EB18, 1);
287
288 /* frequency dependent parameters */
289
290 tda18271_calc_bp_filter(fe, &freq);
291 tda18271_calc_gain_taper(fe, &freq);
292 tda18271_calc_rf_band(fe, &freq);
293 tda18271_calc_km(fe, &freq);
294
295 tda18271_write_regs(fe, R_EP1, 3);
296 tda18271_write_regs(fe, R_EB13, 1);
297
298 /* main pll charge pump source */
299 regs[R_EB4] |= 0x20;
300 tda18271_write_regs(fe, R_EB4, 1);
301
302 /* cal pll charge pump source */
303 regs[R_EB7] |= 0x20;
304 tda18271_write_regs(fe, R_EB7, 1);
305
306 /* force dcdc converter to 0 V */
307 regs[R_EB14] = 0x00;
308 tda18271_write_regs(fe, R_EB14, 1);
309
310 /* disable plls lock */
311 regs[R_EB20] &= ~0x20;
312 tda18271_write_regs(fe, R_EB20, 1);
313
314 /* set CAL mode to RF tracking filter calibration */
315 regs[R_EP4] |= 0x03;
316 tda18271_write_regs(fe, R_EP4, 2);
317
318 /* --------------------------------------------------------------- */
319
320 /* set the internal calibration signal */
321 N = freq;
322
323 tda18271_calc_main_pll(fe, N);
324 tda18271_write_regs(fe, R_MPD, 4);
325
326 /* downconvert internal calibration */
327 N += 1000000;
328
329 tda18271_calc_main_pll(fe, N);
330 tda18271_write_regs(fe, R_MPD, 4);
331
332 msleep(5);
333
334 tda18271_write_regs(fe, R_EP2, 1);
335 tda18271_write_regs(fe, R_EP1, 1);
336 tda18271_write_regs(fe, R_EP2, 1);
337 tda18271_write_regs(fe, R_EP1, 1);
338
339 /* --------------------------------------------------------------- */
340
341 /* normal operation for the main pll */
342 regs[R_EB4] &= ~0x20;
343 tda18271_write_regs(fe, R_EB4, 1);
344
345 /* normal operation for the cal pll */
346 regs[R_EB7] &= ~0x20;
347 tda18271_write_regs(fe, R_EB7, 1);
348
349 msleep(5); /* plls locking */
350
351 /* launch the rf tracking filters calibration */
352 regs[R_EB20] |= 0x20;
353 tda18271_write_regs(fe, R_EB20, 1);
354
355 msleep(60); /* calibration */
356
357 /* --------------------------------------------------------------- */
358
359 /* set CAL mode to normal */
360 regs[R_EP4] &= ~0x03;
361
362 /* switch on agc1 */
363 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
364
365 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
366 tda18271_write_regs(fe, R_EB18, 1);
367
368 tda18271_write_regs(fe, R_EP3, 2);
369
370 /* synchronization */
371 tda18271_write_regs(fe, R_EP1, 1);
372
373 /* get calibration result */
374 tda18271_read_extended(fe);
375
376 return regs[R_EB14];
377}
378
379static int tda18271_powerscan(struct dvb_frontend *fe,
380 u32 *freq_in, u32 *freq_out)
381{
382 struct tda18271_priv *priv = fe->tuner_priv;
383 unsigned char *regs = priv->tda18271_regs;
384 int sgn, bcal, count, wait;
385 u8 cid_target;
386 u16 count_limit;
387 u32 freq;
388
389 freq = *freq_in;
390
391 tda18271_calc_rf_band(fe, &freq);
392 tda18271_calc_rf_cal(fe, &freq);
393 tda18271_calc_gain_taper(fe, &freq);
394 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
395
396 tda18271_write_regs(fe, R_EP2, 1);
397 tda18271_write_regs(fe, R_EB14, 1);
398
399 /* downconvert frequency */
400 freq += 1000000;
401
402 tda18271_calc_main_pll(fe, freq);
403 tda18271_write_regs(fe, R_MPD, 4);
404
405 msleep(5); /* pll locking */
406
407 /* detection mode */
408 regs[R_EP4] &= ~0x03;
409 regs[R_EP4] |= 0x01;
410 tda18271_write_regs(fe, R_EP4, 1);
411
412 /* launch power detection measurement */
413 tda18271_write_regs(fe, R_EP2, 1);
414
415 /* read power detection info, stored in EB10 */
416 tda18271_read_extended(fe);
417
418 /* algorithm initialization */
419 sgn = 1;
420 *freq_out = *freq_in;
421 bcal = 0;
422 count = 0;
423 wait = false;
424
425 while ((regs[R_EB10] & 0x3f) < cid_target) {
426 /* downconvert updated freq to 1 MHz */
427 freq = *freq_in + (sgn * count) + 1000000;
428
429 tda18271_calc_main_pll(fe, freq);
430 tda18271_write_regs(fe, R_MPD, 4);
431
432 if (wait) {
433 msleep(5); /* pll locking */
434 wait = false;
435 } else
436 udelay(100); /* pll locking */
437
438 /* launch power detection measurement */
439 tda18271_write_regs(fe, R_EP2, 1);
440
441 /* read power detection info, stored in EB10 */
442 tda18271_read_extended(fe);
443
444 count += 200;
445
446 if (count < count_limit)
447 continue;
448
449 if (sgn <= 0)
450 break;
451
452 sgn = -1 * sgn;
453 count = 200;
454 wait = true;
455 }
456
457 if ((regs[R_EB10] & 0x3f) >= cid_target) {
458 bcal = 1;
459 *freq_out = freq - 1000000;
460 } else
461 bcal = 0;
462
463 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
464 bcal, *freq_in, *freq_out, freq);
465
466 return bcal;
467}
468
469static int tda18271_powerscan_init(struct dvb_frontend *fe)
470{
471 struct tda18271_priv *priv = fe->tuner_priv;
472 unsigned char *regs = priv->tda18271_regs;
473
474 /* set standard to digital */
475 regs[R_EP3] &= ~0x1f; /* clear std bits */
476 regs[R_EP3] |= 0x12;
477
478 /* set cal mode to normal */
479 regs[R_EP4] &= ~0x03;
480
481 /* update IF output level & IF notch frequency */
482 regs[R_EP4] &= ~0x1c; /* clear if level bits */
483
484 tda18271_write_regs(fe, R_EP3, 2);
485
486 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
487 tda18271_write_regs(fe, R_EB18, 1);
488
489 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
490
491 /* 1.5 MHz low pass filter */
492 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
493 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
494
495 tda18271_write_regs(fe, R_EB21, 3);
496
497 return 0;
498}
499
500static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
501{
502 struct tda18271_priv *priv = fe->tuner_priv;
503 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
504 unsigned char *regs = priv->tda18271_regs;
505 int bcal, rf, i;
506#define RF1 0
507#define RF2 1
508#define RF3 2
509 u32 rf_default[3];
510 u32 rf_freq[3];
511 u8 prog_cal[3];
512 u8 prog_tab[3];
513
514 i = tda18271_lookup_rf_band(fe, &freq, NULL);
515
516 if (i < 0)
517 return i;
518
519 rf_default[RF1] = 1000 * map[i].rf1_def;
520 rf_default[RF2] = 1000 * map[i].rf2_def;
521 rf_default[RF3] = 1000 * map[i].rf3_def;
522
523 for (rf = RF1; rf <= RF3; rf++) {
524 if (0 == rf_default[rf])
525 return 0;
526 tda_cal("freq = %d, rf = %d\n", freq, rf);
527
528 /* look for optimized calibration frequency */
529 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
530
531 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
532 prog_tab[rf] = regs[R_EB14];
533
534 if (1 == bcal)
535 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
536 else
537 prog_cal[rf] = prog_tab[rf];
538
539 switch (rf) {
540 case RF1:
541 map[i].rf_a1 = 0;
542 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
543 map[i].rf1 = rf_freq[RF1] / 1000;
544 break;
545 case RF2:
546 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
547 prog_cal[RF1] + prog_tab[RF1]) /
548 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
549 map[i].rf2 = rf_freq[RF2] / 1000;
550 break;
551 case RF3:
552 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
553 prog_cal[RF2] + prog_tab[RF2]) /
554 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
555 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
556 map[i].rf3 = rf_freq[RF3] / 1000;
557 break;
558 default:
559 BUG();
560 }
561 }
562
563 return 0;
564}
565
566static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
567{
568 struct tda18271_priv *priv = fe->tuner_priv;
569 unsigned int i;
570
571 tda_info("tda18271: performing RF tracking filter calibration\n");
572
573 /* wait for die temperature stabilization */
574 msleep(200);
575
576 tda18271_powerscan_init(fe);
577
578 /* rf band calibration */
579 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
580 tda18271_rf_tracking_filters_init(fe, 1000 *
581 priv->rf_cal_state[i].rfmax);
582
583 priv->tm_rfcal = tda18271_read_thermometer(fe);
584
585 return 0;
586}
587
588/* ------------------------------------------------------------------ */
589
590static int tda18271_rf_cal_init(struct dvb_frontend *fe)
591{
592 struct tda18271_priv *priv = fe->tuner_priv;
593 unsigned char *regs = priv->tda18271_regs;
594
595 /* test RF_CAL_OK to see if we need init */
596 if ((regs[R_EP1] & 0x10) == 0)
597 priv->cal_initialized = false;
598
599 if (priv->cal_initialized)
600 return 0;
601
602 tda18271_calc_rf_filter_curve(fe);
603
604 tda18271_por(fe);
605
606 tda_info("tda18271: RF tracking filter calibration complete\n");
607
608 priv->cal_initialized = true;
609
610 return 0;
611}
612
613static int tda18271_init(struct dvb_frontend *fe)
614{
615 struct tda18271_priv *priv = fe->tuner_priv;
616
617 mutex_lock(&priv->lock);
618
619 /* power up */
620 tda18271_set_standby_mode(fe, 0, 0, 0);
621
622 /* initialization */
623 tda18271_ir_cal_init(fe);
624
625 if (priv->id == TDA18271HDC2)
626 tda18271_rf_cal_init(fe);
627
628 mutex_unlock(&priv->lock);
629
630 return 0;
631}
632
633static int tda18271c2_tune(struct dvb_frontend *fe,
634 u32 ifc, u32 freq, u32 bw, u8 std, int radio)
635{
636 struct tda18271_priv *priv = fe->tuner_priv;
637
638 tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
639
640 tda18271_init(fe);
641
642 mutex_lock(&priv->lock);
643
644 tda18271_rf_tracking_filters_correction(fe, freq);
645
646 tda18271_channel_configuration(fe, ifc, freq, bw, std, radio);
647
648 mutex_unlock(&priv->lock);
649
650 return 0;
651}
652
653/* ------------------------------------------------------------------ */
654
655static int tda18271c1_tune(struct dvb_frontend *fe,
656 u32 ifc, u32 freq, u32 bw, u8 std, int radio)
657{
658 struct tda18271_priv *priv = fe->tuner_priv;
659 unsigned char *regs = priv->tda18271_regs;
660 u32 N = 0;
661
662 tda18271_init(fe);
663
664 mutex_lock(&priv->lock);
665
666 tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
667
668 /* RF tracking filter calibration */
669
670 /* calculate bp filter */
671 tda18271_calc_bp_filter(fe, &freq);
672 tda18271_write_regs(fe, R_EP1, 1);
673
674 regs[R_EB4] &= 0x07;
675 regs[R_EB4] |= 0x60;
676 tda18271_write_regs(fe, R_EB4, 1);
677
678 regs[R_EB7] = 0x60;
679 tda18271_write_regs(fe, R_EB7, 1);
680
681 regs[R_EB14] = 0x00;
682 tda18271_write_regs(fe, R_EB14, 1);
683
684 regs[R_EB20] = 0xcc;
685 tda18271_write_regs(fe, R_EB20, 1);
686
687 /* set cal mode to RF tracking filter calibration */
688 regs[R_EP4] |= 0x03;
689
690 /* calculate cal pll */
691
692 switch (priv->mode) {
693 case TDA18271_ANALOG:
694 N = freq - 1250000;
695 break;
696 case TDA18271_DIGITAL:
697 N = freq + bw / 2;
698 break;
699 }
700
701 tda18271_calc_cal_pll(fe, N);
702
703 /* calculate main pll */
704
705 switch (priv->mode) {
706 case TDA18271_ANALOG:
707 N = freq - 250000;
708 break;
709 case TDA18271_DIGITAL:
710 N = freq + bw / 2 + 1000000;
711 break;
712 }
713
714 tda18271_calc_main_pll(fe, N);
715
716 tda18271_write_regs(fe, R_EP3, 11);
717 msleep(5); /* RF tracking filter calibration initialization */
718
719 /* search for K,M,CO for RF calibration */
720 tda18271_calc_km(fe, &freq);
721 tda18271_write_regs(fe, R_EB13, 1);
722
723 /* search for rf band */
724 tda18271_calc_rf_band(fe, &freq);
725
726 /* search for gain taper */
727 tda18271_calc_gain_taper(fe, &freq);
728
729 tda18271_write_regs(fe, R_EP2, 1);
730 tda18271_write_regs(fe, R_EP1, 1);
731 tda18271_write_regs(fe, R_EP2, 1);
732 tda18271_write_regs(fe, R_EP1, 1);
733
734 regs[R_EB4] &= 0x07;
735 regs[R_EB4] |= 0x40;
736 tda18271_write_regs(fe, R_EB4, 1);
737
738 regs[R_EB7] = 0x40;
739 tda18271_write_regs(fe, R_EB7, 1);
740 msleep(10);
741
742 regs[R_EB20] = 0xec;
743 tda18271_write_regs(fe, R_EB20, 1);
744 msleep(60); /* RF tracking filter calibration completion */
745
746 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
747 tda18271_write_regs(fe, R_EP4, 1);
748
749 tda18271_write_regs(fe, R_EP1, 1);
750
751 /* RF tracking filter correction for VHF_Low band */
752 if (0 == tda18271_calc_rf_cal(fe, &freq))
753 tda18271_write_regs(fe, R_EB14, 1);
754
755 /* Channel Configuration */
756
757 switch (priv->mode) {
758 case TDA18271_ANALOG:
759 regs[R_EB22] = 0x2c;
760 break;
761 case TDA18271_DIGITAL:
762 regs[R_EB22] = 0x37;
763 break;
764 }
765 tda18271_write_regs(fe, R_EB22, 1);
766
767 regs[R_EP1] |= 0x40; /* set dis power level on */
768
769 /* set standard */
770 regs[R_EP3] &= ~0x1f; /* clear std bits */
771
772 /* see table 22 */
773 regs[R_EP3] |= std;
774
775 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
776
777 regs[R_EP4] &= ~0x1c; /* clear if level bits */
778 switch (priv->mode) {
779 case TDA18271_ANALOG:
780 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
781 break;
782 case TDA18271_DIGITAL:
783 regs[R_EP4] |= 0x04;
784 regs[R_MPD] |= 0x80;
785 break;
786 }
787
788 if (radio)
789 regs[R_EP4] |= 0x80;
790 else
791 regs[R_EP4] &= ~0x80;
792
793 /* image rejection validity */
794 tda18271_calc_ir_measure(fe, &freq);
795
796 /* calculate MAIN PLL */
797 N = freq + ifc;
798
799 tda18271_calc_main_pll(fe, N);
800
801 tda18271_write_regs(fe, R_TM, 15);
802 msleep(5);
803 mutex_unlock(&priv->lock);
804
805 return 0;
806}
807
808static inline int tda18271_tune(struct dvb_frontend *fe,
809 u32 ifc, u32 freq, u32 bw, u8 std, int radio)
810{
811 struct tda18271_priv *priv = fe->tuner_priv;
812 int ret = -EINVAL;
813
814 switch (priv->id) {
815 case TDA18271HDC1:
816 ret = tda18271c1_tune(fe, ifc, freq, bw, std, radio);
817 break;
818 case TDA18271HDC2:
819 ret = tda18271c2_tune(fe, ifc, freq, bw, std, radio);
820 break;
821 }
822 return ret;
823}
824
825/* ------------------------------------------------------------------ */
826
827static int tda18271_set_params(struct dvb_frontend *fe,
828 struct dvb_frontend_parameters *params)
829{
830 struct tda18271_priv *priv = fe->tuner_priv;
831 struct tda18271_std_map *std_map = &priv->std;
832 int ret;
833 u8 std;
834 u16 sgIF;
835 u32 bw, freq = params->frequency;
836
837 priv->mode = TDA18271_DIGITAL;
838
839 if (fe->ops.info.type == FE_ATSC) {
840 switch (params->u.vsb.modulation) {
841 case VSB_8:
842 case VSB_16:
843 std = std_map->atsc_6.std_bits;
844 sgIF = std_map->atsc_6.if_freq;
845 break;
846 case QAM_64:
847 case QAM_256:
848 std = std_map->qam_6.std_bits;
849 sgIF = std_map->qam_6.if_freq;
850 break;
851 default:
852 tda_warn("modulation not set!\n");
853 return -EINVAL;
854 }
855#if 0
856 /* userspace request is already center adjusted */
857 freq += 1750000; /* Adjust to center (+1.75MHZ) */
858#endif
859 bw = 6000000;
860 } else if (fe->ops.info.type == FE_OFDM) {
861 switch (params->u.ofdm.bandwidth) {
862 case BANDWIDTH_6_MHZ:
863 bw = 6000000;
864 std = std_map->dvbt_6.std_bits;
865 sgIF = std_map->dvbt_6.if_freq;
866 break;
867 case BANDWIDTH_7_MHZ:
868 bw = 7000000;
869 std = std_map->dvbt_7.std_bits;
870 sgIF = std_map->dvbt_7.if_freq;
871 break;
872 case BANDWIDTH_8_MHZ:
873 bw = 8000000;
874 std = std_map->dvbt_8.std_bits;
875 sgIF = std_map->dvbt_8.if_freq;
876 break;
877 default:
878 tda_warn("bandwidth not set!\n");
879 return -EINVAL;
880 }
881 } else {
882 tda_warn("modulation type not supported!\n");
883 return -EINVAL;
884 }
885
886 /* When tuning digital, the analog demod must be tri-stated */
887 if (fe->ops.analog_ops.standby)
888 fe->ops.analog_ops.standby(fe);
889
890 ret = tda18271_tune(fe, sgIF * 1000, freq, bw, std, 0);
891
892 if (ret < 0)
893 goto fail;
894
895 priv->frequency = freq;
896 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
897 params->u.ofdm.bandwidth : 0;
898fail:
899 return ret;
900}
901
902static int tda18271_set_analog_params(struct dvb_frontend *fe,
903 struct analog_parameters *params)
904{
905 struct tda18271_priv *priv = fe->tuner_priv;
906 struct tda18271_std_map *std_map = &priv->std;
907 char *mode;
908 int ret, radio = 0;
909 u8 std;
910 u16 sgIF;
911 u32 freq = params->frequency * 62500;
912
913 priv->mode = TDA18271_ANALOG;
914
915 if (params->mode == V4L2_TUNER_RADIO) {
916 radio = 1;
917 freq = freq / 1000;
918 std = std_map->fm_radio.std_bits;
919 sgIF = std_map->fm_radio.if_freq;
920 mode = "fm";
921 } else if (params->std & V4L2_STD_MN) {
922 std = std_map->atv_mn.std_bits;
923 sgIF = std_map->atv_mn.if_freq;
924 mode = "MN";
925 } else if (params->std & V4L2_STD_B) {
926 std = std_map->atv_b.std_bits;
927 sgIF = std_map->atv_b.if_freq;
928 mode = "B";
929 } else if (params->std & V4L2_STD_GH) {
930 std = std_map->atv_gh.std_bits;
931 sgIF = std_map->atv_gh.if_freq;
932 mode = "GH";
933 } else if (params->std & V4L2_STD_PAL_I) {
934 std = std_map->atv_i.std_bits;
935 sgIF = std_map->atv_i.if_freq;
936 mode = "I";
937 } else if (params->std & V4L2_STD_DK) {
938 std = std_map->atv_dk.std_bits;
939 sgIF = std_map->atv_dk.if_freq;
940 mode = "DK";
941 } else if (params->std & V4L2_STD_SECAM_L) {
942 std = std_map->atv_l.std_bits;
943 sgIF = std_map->atv_l.if_freq;
944 mode = "L";
945 } else if (params->std & V4L2_STD_SECAM_LC) {
946 std = std_map->atv_lc.std_bits;
947 sgIF = std_map->atv_lc.if_freq;
948 mode = "L'";
949 } else {
950 std = std_map->atv_i.std_bits;
951 sgIF = std_map->atv_i.if_freq;
952 mode = "xx";
953 }
954
955 tda_dbg("setting tda18271 to system %s\n", mode);
956
957 ret = tda18271_tune(fe, sgIF * 1000, freq, 0, std, radio);
958
959 if (ret < 0)
960 goto fail;
961
962 priv->frequency = freq;
963 priv->bandwidth = 0;
964fail:
965 return ret;
966}
967
968static int tda18271_sleep(struct dvb_frontend *fe)
969{
970 struct tda18271_priv *priv = fe->tuner_priv;
971
972 mutex_lock(&priv->lock);
973
974 /* standby mode w/ slave tuner output
975 * & loop thru & xtal oscillator on */
976 tda18271_set_standby_mode(fe, 1, 0, 0);
977
978 mutex_unlock(&priv->lock);
979
980 return 0;
981}
982
983static int tda18271_release(struct dvb_frontend *fe)
984{
985 struct tda18271_priv *priv = fe->tuner_priv;
986
987 mutex_lock(&tda18271_list_mutex);
988
989 priv->count--;
990
991 if (!priv->count) {
992 tda_dbg("destroying instance @ %d-%04x\n",
993 i2c_adapter_id(priv->i2c_adap),
994 priv->i2c_addr);
995 list_del(&priv->tda18271_list);
996
997 kfree(priv);
998 }
999 mutex_unlock(&tda18271_list_mutex);
1000
1001 fe->tuner_priv = NULL;
1002
1003 return 0;
1004}
1005
1006static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1007{
1008 struct tda18271_priv *priv = fe->tuner_priv;
1009 *frequency = priv->frequency;
1010 return 0;
1011}
1012
1013static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1014{
1015 struct tda18271_priv *priv = fe->tuner_priv;
1016 *bandwidth = priv->bandwidth;
1017 return 0;
1018}
1019
1020/* ------------------------------------------------------------------ */
1021
1022#define tda18271_update_std(std_cfg, name) do { \
1023 if (map->std_cfg.if_freq + map->std_cfg.std_bits > 0) { \
1024 tda_dbg("Using custom std config for %s\n", name); \
1025 memcpy(&std->std_cfg, &map->std_cfg, \
1026 sizeof(struct tda18271_std_map_item)); \
1027 } } while (0)
1028
1029#define tda18271_dump_std_item(std_cfg, name) do { \
1030 tda_dbg("(%s) if freq = %d, std bits = 0x%02x\n", \
1031 name, std->std_cfg.if_freq, std->std_cfg.std_bits); \
1032 } while (0)
1033
1034static int tda18271_dump_std_map(struct dvb_frontend *fe)
1035{
1036 struct tda18271_priv *priv = fe->tuner_priv;
1037 struct tda18271_std_map *std = &priv->std;
1038
1039 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
1040 tda18271_dump_std_item(fm_radio, "fm");
1041 tda18271_dump_std_item(atv_b, "pal b");
1042 tda18271_dump_std_item(atv_dk, "pal dk");
1043 tda18271_dump_std_item(atv_gh, "pal gh");
1044 tda18271_dump_std_item(atv_i, "pal i");
1045 tda18271_dump_std_item(atv_l, "pal l");
1046 tda18271_dump_std_item(atv_lc, "pal l'");
1047 tda18271_dump_std_item(atv_mn, "atv mn");
1048 tda18271_dump_std_item(atsc_6, "atsc 6");
1049 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1050 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1051 tda18271_dump_std_item(dvbt_8, "dvbt 8");
1052 tda18271_dump_std_item(qam_6, "qam 6");
1053 tda18271_dump_std_item(qam_8, "qam 8");
1054
1055 return 0;
1056}
1057
1058static int tda18271_update_std_map(struct dvb_frontend *fe,
1059 struct tda18271_std_map *map)
1060{
1061 struct tda18271_priv *priv = fe->tuner_priv;
1062 struct tda18271_std_map *std = &priv->std;
1063
1064 if (!map)
1065 return -EINVAL;
1066
1067 tda18271_update_std(fm_radio, "fm");
1068 tda18271_update_std(atv_b, "atv b");
1069 tda18271_update_std(atv_dk, "atv dk");
1070 tda18271_update_std(atv_gh, "atv gh");
1071 tda18271_update_std(atv_i, "atv i");
1072 tda18271_update_std(atv_l, "atv l");
1073 tda18271_update_std(atv_lc, "atv l'");
1074 tda18271_update_std(atv_mn, "atv mn");
1075 tda18271_update_std(atsc_6, "atsc 6");
1076 tda18271_update_std(dvbt_6, "dvbt 6");
1077 tda18271_update_std(dvbt_7, "dvbt 7");
1078 tda18271_update_std(dvbt_8, "dvbt 8");
1079 tda18271_update_std(qam_6, "qam 6");
1080 tda18271_update_std(qam_8, "qam 8");
1081
1082 return 0;
1083}
1084
1085static int tda18271_get_id(struct dvb_frontend *fe)
1086{
1087 struct tda18271_priv *priv = fe->tuner_priv;
1088 unsigned char *regs = priv->tda18271_regs;
1089 char *name;
1090 int ret = 0;
1091
1092 mutex_lock(&priv->lock);
1093 tda18271_read_regs(fe);
1094 mutex_unlock(&priv->lock);
1095
1096 switch (regs[R_ID] & 0x7f) {
1097 case 3:
1098 name = "TDA18271HD/C1";
1099 priv->id = TDA18271HDC1;
1100 break;
1101 case 4:
1102 name = "TDA18271HD/C2";
1103 priv->id = TDA18271HDC2;
1104 break;
1105 default:
1106 name = "Unknown device";
1107 ret = -EINVAL;
1108 break;
1109 }
1110
1111 tda_info("%s detected @ %d-%04x%s\n", name,
1112 i2c_adapter_id(priv->i2c_adap), priv->i2c_addr,
1113 (0 == ret) ? "" : ", device not supported.");
1114
1115 return ret;
1116}
1117
1118static struct dvb_tuner_ops tda18271_tuner_ops = {
1119 .info = {
1120 .name = "NXP TDA18271HD",
1121 .frequency_min = 45000000,
1122 .frequency_max = 864000000,
1123 .frequency_step = 62500
1124 },
1125 .init = tda18271_init,
1126 .sleep = tda18271_sleep,
1127 .set_params = tda18271_set_params,
1128 .set_analog_params = tda18271_set_analog_params,
1129 .release = tda18271_release,
1130 .get_frequency = tda18271_get_frequency,
1131 .get_bandwidth = tda18271_get_bandwidth,
1132};
1133
1134struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1135 struct i2c_adapter *i2c,
1136 struct tda18271_config *cfg)
1137{
1138 struct tda18271_priv *priv = NULL;
1139 int state_found = 0;
1140
1141 mutex_lock(&tda18271_list_mutex);
1142
1143 list_for_each_entry(priv, &tda18271_list, tda18271_list) {
1144 if ((i2c_adapter_id(priv->i2c_adap) == i2c_adapter_id(i2c)) &&
1145 (priv->i2c_addr == addr)) {
1146 tda_dbg("attaching existing tuner @ %d-%04x\n",
1147 i2c_adapter_id(priv->i2c_adap),
1148 priv->i2c_addr);
1149 priv->count++;
1150 fe->tuner_priv = priv;
1151 state_found = 1;
1152 /* allow dvb driver to override i2c gate setting */
1153 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1154 priv->gate = cfg->gate;
1155 break;
1156 }
1157 }
1158 if (state_found == 0) {
1159 tda_dbg("creating new tuner instance @ %d-%04x\n",
1160 i2c_adapter_id(i2c), addr);
1161
1162 priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
1163 if (priv == NULL) {
1164 mutex_unlock(&tda18271_list_mutex);
1165 return NULL;
1166 }
1167
1168 priv->i2c_addr = addr;
1169 priv->i2c_adap = i2c;
1170 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1171 priv->cal_initialized = false;
1172 mutex_init(&priv->lock);
1173 priv->count++;
1174
1175 fe->tuner_priv = priv;
1176
1177 list_add_tail(&priv->tda18271_list, &tda18271_list);
1178
1179 if (tda18271_get_id(fe) < 0)
1180 goto fail;
1181
1182 if (tda18271_assign_map_layout(fe) < 0)
1183 goto fail;
1184
1185 mutex_lock(&priv->lock);
1186 tda18271_init_regs(fe);
1187
1188 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
1189 tda18271_rf_cal_init(fe);
1190
1191 mutex_unlock(&priv->lock);
1192 }
1193
1194 /* override default std map with values in config struct */
1195 if ((cfg) && (cfg->std_map))
1196 tda18271_update_std_map(fe, cfg->std_map);
1197
1198 mutex_unlock(&tda18271_list_mutex);
1199
1200 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1201 sizeof(struct dvb_tuner_ops));
1202
1203 if (tda18271_debug & DBG_MAP)
1204 tda18271_dump_std_map(fe);
1205
1206 return fe;
1207fail:
1208 mutex_unlock(&tda18271_list_mutex);
1209
1210 tda18271_release(fe);
1211 return NULL;
1212}
1213EXPORT_SYMBOL_GPL(tda18271_attach);
1214MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1215MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1216MODULE_LICENSE("GPL");
1217MODULE_VERSION("0.2");
1218
1219/*
1220 * Overrides for Emacs so that we follow Linus's tabbing style.
1221 * ---------------------------------------------------------------------------
1222 * Local variables:
1223 * c-basic-offset: 8
1224 * End:
1225 */
diff --git a/drivers/media/dvb/frontends/tda18271-priv.h b/drivers/media/dvb/frontends/tda18271-priv.h
new file mode 100644
index 000000000000..7b939a5325fb
--- /dev/null
+++ b/drivers/media/dvb/frontends/tda18271-priv.h
@@ -0,0 +1,212 @@
1/*
2 tda18271-priv.h - private header for the NXP TDA18271 silicon tuner
3
4 Copyright (C) 2007, 2008 Michael Krufky <mkrufky@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 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 __TDA18271_PRIV_H__
22#define __TDA18271_PRIV_H__
23
24#include <linux/kernel.h>
25#include <linux/types.h>
26#include <linux/mutex.h>
27#include "tda18271.h"
28
29#define R_ID 0x00 /* ID byte */
30#define R_TM 0x01 /* Thermo byte */
31#define R_PL 0x02 /* Power level byte */
32#define R_EP1 0x03 /* Easy Prog byte 1 */
33#define R_EP2 0x04 /* Easy Prog byte 2 */
34#define R_EP3 0x05 /* Easy Prog byte 3 */
35#define R_EP4 0x06 /* Easy Prog byte 4 */
36#define R_EP5 0x07 /* Easy Prog byte 5 */
37#define R_CPD 0x08 /* Cal Post-Divider byte */
38#define R_CD1 0x09 /* Cal Divider byte 1 */
39#define R_CD2 0x0a /* Cal Divider byte 2 */
40#define R_CD3 0x0b /* Cal Divider byte 3 */
41#define R_MPD 0x0c /* Main Post-Divider byte */
42#define R_MD1 0x0d /* Main Divider byte 1 */
43#define R_MD2 0x0e /* Main Divider byte 2 */
44#define R_MD3 0x0f /* Main Divider byte 3 */
45#define R_EB1 0x10 /* Extended byte 1 */
46#define R_EB2 0x11 /* Extended byte 2 */
47#define R_EB3 0x12 /* Extended byte 3 */
48#define R_EB4 0x13 /* Extended byte 4 */
49#define R_EB5 0x14 /* Extended byte 5 */
50#define R_EB6 0x15 /* Extended byte 6 */
51#define R_EB7 0x16 /* Extended byte 7 */
52#define R_EB8 0x17 /* Extended byte 8 */
53#define R_EB9 0x18 /* Extended byte 9 */
54#define R_EB10 0x19 /* Extended byte 10 */
55#define R_EB11 0x1a /* Extended byte 11 */
56#define R_EB12 0x1b /* Extended byte 12 */
57#define R_EB13 0x1c /* Extended byte 13 */
58#define R_EB14 0x1d /* Extended byte 14 */
59#define R_EB15 0x1e /* Extended byte 15 */
60#define R_EB16 0x1f /* Extended byte 16 */
61#define R_EB17 0x20 /* Extended byte 17 */
62#define R_EB18 0x21 /* Extended byte 18 */
63#define R_EB19 0x22 /* Extended byte 19 */
64#define R_EB20 0x23 /* Extended byte 20 */
65#define R_EB21 0x24 /* Extended byte 21 */
66#define R_EB22 0x25 /* Extended byte 22 */
67#define R_EB23 0x26 /* Extended byte 23 */
68
69#define TDA18271_NUM_REGS 39
70
71/*---------------------------------------------------------------------*/
72
73struct tda18271_rf_tracking_filter_cal {
74 u32 rfmax;
75 u8 rfband;
76 u32 rf1_def;
77 u32 rf2_def;
78 u32 rf3_def;
79 u32 rf1;
80 u32 rf2;
81 u32 rf3;
82 int rf_a1;
83 int rf_b1;
84 int rf_a2;
85 int rf_b2;
86};
87
88enum tda18271_mode {
89 TDA18271_ANALOG,
90 TDA18271_DIGITAL,
91};
92
93struct tda18271_map_layout;
94
95enum tda18271_ver {
96 TDA18271HDC1,
97 TDA18271HDC2,
98};
99
100struct tda18271_priv {
101 u8 i2c_addr;
102 struct i2c_adapter *i2c_adap;
103 unsigned char tda18271_regs[TDA18271_NUM_REGS];
104
105 struct list_head tda18271_list;
106
107 enum tda18271_mode mode;
108 enum tda18271_i2c_gate gate;
109 enum tda18271_ver id;
110
111 unsigned int count;
112 unsigned int tm_rfcal;
113 unsigned int cal_initialized:1;
114
115 struct tda18271_map_layout *maps;
116 struct tda18271_std_map std;
117 struct tda18271_rf_tracking_filter_cal rf_cal_state[8];
118
119 struct mutex lock;
120
121 u32 frequency;
122 u32 bandwidth;
123};
124
125/*---------------------------------------------------------------------*/
126
127extern int tda18271_debug;
128
129#define DBG_INFO 1
130#define DBG_MAP 2
131#define DBG_REG 4
132#define DBG_ADV 8
133#define DBG_CAL 16
134
135#define tda_printk(kern, fmt, arg...) \
136 printk(kern "%s: " fmt, __FUNCTION__, ##arg)
137
138#define dprintk(kern, lvl, fmt, arg...) do {\
139 if (tda18271_debug & lvl) \
140 tda_printk(kern, fmt, ##arg); } while (0)
141
142#define tda_info(fmt, arg...) printk(KERN_INFO fmt, ##arg)
143#define tda_warn(fmt, arg...) tda_printk(KERN_WARNING, fmt, ##arg)
144#define tda_err(fmt, arg...) tda_printk(KERN_ERR, fmt, ##arg)
145#define tda_dbg(fmt, arg...) dprintk(KERN_DEBUG, DBG_INFO, fmt, ##arg)
146#define tda_map(fmt, arg...) dprintk(KERN_DEBUG, DBG_MAP, fmt, ##arg)
147#define tda_reg(fmt, arg...) dprintk(KERN_DEBUG, DBG_REG, fmt, ##arg)
148#define tda_cal(fmt, arg...) dprintk(KERN_DEBUG, DBG_CAL, fmt, ##arg)
149
150/*---------------------------------------------------------------------*/
151
152enum tda18271_map_type {
153 /* tda18271_pll_map */
154 MAIN_PLL,
155 CAL_PLL,
156 /* tda18271_map */
157 RF_CAL,
158 RF_CAL_KMCO,
159 RF_CAL_DC_OVER_DT,
160 BP_FILTER,
161 RF_BAND,
162 GAIN_TAPER,
163 IR_MEASURE,
164};
165
166extern int tda18271_lookup_pll_map(struct dvb_frontend *fe,
167 enum tda18271_map_type map_type,
168 u32 *freq, u8 *post_div, u8 *div);
169extern int tda18271_lookup_map(struct dvb_frontend *fe,
170 enum tda18271_map_type map_type,
171 u32 *freq, u8 *val);
172
173extern int tda18271_lookup_thermometer(struct dvb_frontend *fe);
174
175extern int tda18271_lookup_rf_band(struct dvb_frontend *fe,
176 u32 *freq, u8 *rf_band);
177
178extern int tda18271_lookup_cid_target(struct dvb_frontend *fe,
179 u32 *freq, u8 *cid_target,
180 u16 *count_limit);
181
182extern int tda18271_assign_map_layout(struct dvb_frontend *fe);
183
184/*---------------------------------------------------------------------*/
185
186extern int tda18271_read_regs(struct dvb_frontend *fe);
187extern int tda18271_read_extended(struct dvb_frontend *fe);
188extern int tda18271_write_regs(struct dvb_frontend *fe, int idx, int len);
189extern int tda18271_init_regs(struct dvb_frontend *fe);
190
191extern int tda18271_set_standby_mode(struct dvb_frontend *fe,
192 int sm, int sm_lt, int sm_xt);
193
194extern int tda18271_calc_main_pll(struct dvb_frontend *fe, u32 freq);
195extern int tda18271_calc_cal_pll(struct dvb_frontend *fe, u32 freq);
196
197extern int tda18271_calc_bp_filter(struct dvb_frontend *fe, u32 *freq);
198extern int tda18271_calc_km(struct dvb_frontend *fe, u32 *freq);
199extern int tda18271_calc_rf_band(struct dvb_frontend *fe, u32 *freq);
200extern int tda18271_calc_gain_taper(struct dvb_frontend *fe, u32 *freq);
201extern int tda18271_calc_ir_measure(struct dvb_frontend *fe, u32 *freq);
202extern int tda18271_calc_rf_cal(struct dvb_frontend *fe, u32 *freq);
203
204#endif /* __TDA18271_PRIV_H__ */
205
206/*
207 * Overrides for Emacs so that we follow Linus's tabbing style.
208 * ---------------------------------------------------------------------------
209 * Local variables:
210 * c-basic-offset: 8
211 * End:
212 */
diff --git a/drivers/media/dvb/frontends/tda18271-tables.c b/drivers/media/dvb/frontends/tda18271-tables.c
new file mode 100644
index 000000000000..e94afcfdc5bc
--- /dev/null
+++ b/drivers/media/dvb/frontends/tda18271-tables.c
@@ -0,0 +1,1285 @@
1/*
2 tda18271-tables.c - driver for the Philips / NXP TDA18271 silicon tuner
3
4 Copyright (C) 2007, 2008 Michael Krufky <mkrufky@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 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#include "tda18271-priv.h"
22
23struct tda18271_pll_map {
24 u32 lomax;
25 u8 pd; /* post div */
26 u8 d; /* div */
27};
28
29struct tda18271_map {
30 u32 rfmax;
31 u8 val;
32};
33
34/*---------------------------------------------------------------------*/
35
36static struct tda18271_pll_map tda18271c1_main_pll[] = {
37 { .lomax = 32000, .pd = 0x5f, .d = 0xf0 },
38 { .lomax = 35000, .pd = 0x5e, .d = 0xe0 },
39 { .lomax = 37000, .pd = 0x5d, .d = 0xd0 },
40 { .lomax = 41000, .pd = 0x5c, .d = 0xc0 },
41 { .lomax = 44000, .pd = 0x5b, .d = 0xb0 },
42 { .lomax = 49000, .pd = 0x5a, .d = 0xa0 },
43 { .lomax = 54000, .pd = 0x59, .d = 0x90 },
44 { .lomax = 61000, .pd = 0x58, .d = 0x80 },
45 { .lomax = 65000, .pd = 0x4f, .d = 0x78 },
46 { .lomax = 70000, .pd = 0x4e, .d = 0x70 },
47 { .lomax = 75000, .pd = 0x4d, .d = 0x68 },
48 { .lomax = 82000, .pd = 0x4c, .d = 0x60 },
49 { .lomax = 89000, .pd = 0x4b, .d = 0x58 },
50 { .lomax = 98000, .pd = 0x4a, .d = 0x50 },
51 { .lomax = 109000, .pd = 0x49, .d = 0x48 },
52 { .lomax = 123000, .pd = 0x48, .d = 0x40 },
53 { .lomax = 131000, .pd = 0x3f, .d = 0x3c },
54 { .lomax = 141000, .pd = 0x3e, .d = 0x38 },
55 { .lomax = 151000, .pd = 0x3d, .d = 0x34 },
56 { .lomax = 164000, .pd = 0x3c, .d = 0x30 },
57 { .lomax = 179000, .pd = 0x3b, .d = 0x2c },
58 { .lomax = 197000, .pd = 0x3a, .d = 0x28 },
59 { .lomax = 219000, .pd = 0x39, .d = 0x24 },
60 { .lomax = 246000, .pd = 0x38, .d = 0x20 },
61 { .lomax = 263000, .pd = 0x2f, .d = 0x1e },
62 { .lomax = 282000, .pd = 0x2e, .d = 0x1c },
63 { .lomax = 303000, .pd = 0x2d, .d = 0x1a },
64 { .lomax = 329000, .pd = 0x2c, .d = 0x18 },
65 { .lomax = 359000, .pd = 0x2b, .d = 0x16 },
66 { .lomax = 395000, .pd = 0x2a, .d = 0x14 },
67 { .lomax = 438000, .pd = 0x29, .d = 0x12 },
68 { .lomax = 493000, .pd = 0x28, .d = 0x10 },
69 { .lomax = 526000, .pd = 0x1f, .d = 0x0f },
70 { .lomax = 564000, .pd = 0x1e, .d = 0x0e },
71 { .lomax = 607000, .pd = 0x1d, .d = 0x0d },
72 { .lomax = 658000, .pd = 0x1c, .d = 0x0c },
73 { .lomax = 718000, .pd = 0x1b, .d = 0x0b },
74 { .lomax = 790000, .pd = 0x1a, .d = 0x0a },
75 { .lomax = 877000, .pd = 0x19, .d = 0x09 },
76 { .lomax = 987000, .pd = 0x18, .d = 0x08 },
77 { .lomax = 0, .pd = 0x00, .d = 0x00 }, /* end */
78};
79
80static struct tda18271_pll_map tda18271c2_main_pll[] = {
81 { .lomax = 33125, .pd = 0x57, .d = 0xf0 },
82 { .lomax = 35500, .pd = 0x56, .d = 0xe0 },
83 { .lomax = 38188, .pd = 0x55, .d = 0xd0 },
84 { .lomax = 41375, .pd = 0x54, .d = 0xc0 },
85 { .lomax = 45125, .pd = 0x53, .d = 0xb0 },
86 { .lomax = 49688, .pd = 0x52, .d = 0xa0 },
87 { .lomax = 55188, .pd = 0x51, .d = 0x90 },
88 { .lomax = 62125, .pd = 0x50, .d = 0x80 },
89 { .lomax = 66250, .pd = 0x47, .d = 0x78 },
90 { .lomax = 71000, .pd = 0x46, .d = 0x70 },
91 { .lomax = 76375, .pd = 0x45, .d = 0x68 },
92 { .lomax = 82750, .pd = 0x44, .d = 0x60 },
93 { .lomax = 90250, .pd = 0x43, .d = 0x58 },
94 { .lomax = 99375, .pd = 0x42, .d = 0x50 },
95 { .lomax = 110375, .pd = 0x41, .d = 0x48 },
96 { .lomax = 124250, .pd = 0x40, .d = 0x40 },
97 { .lomax = 132500, .pd = 0x37, .d = 0x3c },
98 { .lomax = 142000, .pd = 0x36, .d = 0x38 },
99 { .lomax = 152750, .pd = 0x35, .d = 0x34 },
100 { .lomax = 165500, .pd = 0x34, .d = 0x30 },
101 { .lomax = 180500, .pd = 0x33, .d = 0x2c },
102 { .lomax = 198750, .pd = 0x32, .d = 0x28 },
103 { .lomax = 220750, .pd = 0x31, .d = 0x24 },
104 { .lomax = 248500, .pd = 0x30, .d = 0x20 },
105 { .lomax = 265000, .pd = 0x27, .d = 0x1e },
106 { .lomax = 284000, .pd = 0x26, .d = 0x1c },
107 { .lomax = 305500, .pd = 0x25, .d = 0x1a },
108 { .lomax = 331000, .pd = 0x24, .d = 0x18 },
109 { .lomax = 361000, .pd = 0x23, .d = 0x16 },
110 { .lomax = 397500, .pd = 0x22, .d = 0x14 },
111 { .lomax = 441500, .pd = 0x21, .d = 0x12 },
112 { .lomax = 497000, .pd = 0x20, .d = 0x10 },
113 { .lomax = 530000, .pd = 0x17, .d = 0x0f },
114 { .lomax = 568000, .pd = 0x16, .d = 0x0e },
115 { .lomax = 611000, .pd = 0x15, .d = 0x0d },
116 { .lomax = 662000, .pd = 0x14, .d = 0x0c },
117 { .lomax = 722000, .pd = 0x13, .d = 0x0b },
118 { .lomax = 795000, .pd = 0x12, .d = 0x0a },
119 { .lomax = 883000, .pd = 0x11, .d = 0x09 },
120 { .lomax = 994000, .pd = 0x10, .d = 0x08 },
121 { .lomax = 0, .pd = 0x00, .d = 0x00 }, /* end */
122};
123
124static struct tda18271_pll_map tda18271c1_cal_pll[] = {
125 { .lomax = 33000, .pd = 0xdd, .d = 0xd0 },
126 { .lomax = 36000, .pd = 0xdc, .d = 0xc0 },
127 { .lomax = 40000, .pd = 0xdb, .d = 0xb0 },
128 { .lomax = 44000, .pd = 0xda, .d = 0xa0 },
129 { .lomax = 49000, .pd = 0xd9, .d = 0x90 },
130 { .lomax = 55000, .pd = 0xd8, .d = 0x80 },
131 { .lomax = 63000, .pd = 0xd3, .d = 0x70 },
132 { .lomax = 67000, .pd = 0xcd, .d = 0x68 },
133 { .lomax = 73000, .pd = 0xcc, .d = 0x60 },
134 { .lomax = 80000, .pd = 0xcb, .d = 0x58 },
135 { .lomax = 88000, .pd = 0xca, .d = 0x50 },
136 { .lomax = 98000, .pd = 0xc9, .d = 0x48 },
137 { .lomax = 110000, .pd = 0xc8, .d = 0x40 },
138 { .lomax = 126000, .pd = 0xc3, .d = 0x38 },
139 { .lomax = 135000, .pd = 0xbd, .d = 0x34 },
140 { .lomax = 147000, .pd = 0xbc, .d = 0x30 },
141 { .lomax = 160000, .pd = 0xbb, .d = 0x2c },
142 { .lomax = 176000, .pd = 0xba, .d = 0x28 },
143 { .lomax = 196000, .pd = 0xb9, .d = 0x24 },
144 { .lomax = 220000, .pd = 0xb8, .d = 0x20 },
145 { .lomax = 252000, .pd = 0xb3, .d = 0x1c },
146 { .lomax = 271000, .pd = 0xad, .d = 0x1a },
147 { .lomax = 294000, .pd = 0xac, .d = 0x18 },
148 { .lomax = 321000, .pd = 0xab, .d = 0x16 },
149 { .lomax = 353000, .pd = 0xaa, .d = 0x14 },
150 { .lomax = 392000, .pd = 0xa9, .d = 0x12 },
151 { .lomax = 441000, .pd = 0xa8, .d = 0x10 },
152 { .lomax = 505000, .pd = 0xa3, .d = 0x0e },
153 { .lomax = 543000, .pd = 0x9d, .d = 0x0d },
154 { .lomax = 589000, .pd = 0x9c, .d = 0x0c },
155 { .lomax = 642000, .pd = 0x9b, .d = 0x0b },
156 { .lomax = 707000, .pd = 0x9a, .d = 0x0a },
157 { .lomax = 785000, .pd = 0x99, .d = 0x09 },
158 { .lomax = 883000, .pd = 0x98, .d = 0x08 },
159 { .lomax = 1010000, .pd = 0x93, .d = 0x07 },
160 { .lomax = 0, .pd = 0x00, .d = 0x00 }, /* end */
161};
162
163static struct tda18271_pll_map tda18271c2_cal_pll[] = {
164 { .lomax = 33813, .pd = 0xdd, .d = 0xd0 },
165 { .lomax = 36625, .pd = 0xdc, .d = 0xc0 },
166 { .lomax = 39938, .pd = 0xdb, .d = 0xb0 },
167 { .lomax = 43938, .pd = 0xda, .d = 0xa0 },
168 { .lomax = 48813, .pd = 0xd9, .d = 0x90 },
169 { .lomax = 54938, .pd = 0xd8, .d = 0x80 },
170 { .lomax = 62813, .pd = 0xd3, .d = 0x70 },
171 { .lomax = 67625, .pd = 0xcd, .d = 0x68 },
172 { .lomax = 73250, .pd = 0xcc, .d = 0x60 },
173 { .lomax = 79875, .pd = 0xcb, .d = 0x58 },
174 { .lomax = 87875, .pd = 0xca, .d = 0x50 },
175 { .lomax = 97625, .pd = 0xc9, .d = 0x48 },
176 { .lomax = 109875, .pd = 0xc8, .d = 0x40 },
177 { .lomax = 125625, .pd = 0xc3, .d = 0x38 },
178 { .lomax = 135250, .pd = 0xbd, .d = 0x34 },
179 { .lomax = 146500, .pd = 0xbc, .d = 0x30 },
180 { .lomax = 159750, .pd = 0xbb, .d = 0x2c },
181 { .lomax = 175750, .pd = 0xba, .d = 0x28 },
182 { .lomax = 195250, .pd = 0xb9, .d = 0x24 },
183 { .lomax = 219750, .pd = 0xb8, .d = 0x20 },
184 { .lomax = 251250, .pd = 0xb3, .d = 0x1c },
185 { .lomax = 270500, .pd = 0xad, .d = 0x1a },
186 { .lomax = 293000, .pd = 0xac, .d = 0x18 },
187 { .lomax = 319500, .pd = 0xab, .d = 0x16 },
188 { .lomax = 351500, .pd = 0xaa, .d = 0x14 },
189 { .lomax = 390500, .pd = 0xa9, .d = 0x12 },
190 { .lomax = 439500, .pd = 0xa8, .d = 0x10 },
191 { .lomax = 502500, .pd = 0xa3, .d = 0x0e },
192 { .lomax = 541000, .pd = 0x9d, .d = 0x0d },
193 { .lomax = 586000, .pd = 0x9c, .d = 0x0c },
194 { .lomax = 639000, .pd = 0x9b, .d = 0x0b },
195 { .lomax = 703000, .pd = 0x9a, .d = 0x0a },
196 { .lomax = 781000, .pd = 0x99, .d = 0x09 },
197 { .lomax = 879000, .pd = 0x98, .d = 0x08 },
198 { .lomax = 0, .pd = 0x00, .d = 0x00 }, /* end */
199};
200
201static struct tda18271_map tda18271_bp_filter[] = {
202 { .rfmax = 62000, .val = 0x00 },
203 { .rfmax = 84000, .val = 0x01 },
204 { .rfmax = 100000, .val = 0x02 },
205 { .rfmax = 140000, .val = 0x03 },
206 { .rfmax = 170000, .val = 0x04 },
207 { .rfmax = 180000, .val = 0x05 },
208 { .rfmax = 865000, .val = 0x06 },
209 { .rfmax = 0, .val = 0x00 }, /* end */
210};
211
212static struct tda18271_map tda18271c1_km[] = {
213 { .rfmax = 61100, .val = 0x74 },
214 { .rfmax = 350000, .val = 0x40 },
215 { .rfmax = 720000, .val = 0x30 },
216 { .rfmax = 865000, .val = 0x40 },
217 { .rfmax = 0, .val = 0x00 }, /* end */
218};
219
220static struct tda18271_map tda18271c2_km[] = {
221 { .rfmax = 47900, .val = 0x38 },
222 { .rfmax = 61100, .val = 0x44 },
223 { .rfmax = 350000, .val = 0x30 },
224 { .rfmax = 720000, .val = 0x24 },
225 { .rfmax = 865000, .val = 0x3c },
226 { .rfmax = 0, .val = 0x00 }, /* end */
227};
228
229static struct tda18271_map tda18271_rf_band[] = {
230 { .rfmax = 47900, .val = 0x00 },
231 { .rfmax = 61100, .val = 0x01 },
232/* { .rfmax = 152600, .val = 0x02 }, */
233 { .rfmax = 121200, .val = 0x02 },
234 { .rfmax = 164700, .val = 0x03 },
235 { .rfmax = 203500, .val = 0x04 },
236 { .rfmax = 457800, .val = 0x05 },
237 { .rfmax = 865000, .val = 0x06 },
238 { .rfmax = 0, .val = 0x00 }, /* end */
239};
240
241static struct tda18271_map tda18271_gain_taper[] = {
242 { .rfmax = 45400, .val = 0x1f },
243 { .rfmax = 45800, .val = 0x1e },
244 { .rfmax = 46200, .val = 0x1d },
245 { .rfmax = 46700, .val = 0x1c },
246 { .rfmax = 47100, .val = 0x1b },
247 { .rfmax = 47500, .val = 0x1a },
248 { .rfmax = 47900, .val = 0x19 },
249 { .rfmax = 49600, .val = 0x17 },
250 { .rfmax = 51200, .val = 0x16 },
251 { .rfmax = 52900, .val = 0x15 },
252 { .rfmax = 54500, .val = 0x14 },
253 { .rfmax = 56200, .val = 0x13 },
254 { .rfmax = 57800, .val = 0x12 },
255 { .rfmax = 59500, .val = 0x11 },
256 { .rfmax = 61100, .val = 0x10 },
257 { .rfmax = 67600, .val = 0x0d },
258 { .rfmax = 74200, .val = 0x0c },
259 { .rfmax = 80700, .val = 0x0b },
260 { .rfmax = 87200, .val = 0x0a },
261 { .rfmax = 93800, .val = 0x09 },
262 { .rfmax = 100300, .val = 0x08 },
263 { .rfmax = 106900, .val = 0x07 },
264 { .rfmax = 113400, .val = 0x06 },
265 { .rfmax = 119900, .val = 0x05 },
266 { .rfmax = 126500, .val = 0x04 },
267 { .rfmax = 133000, .val = 0x03 },
268 { .rfmax = 139500, .val = 0x02 },
269 { .rfmax = 146100, .val = 0x01 },
270 { .rfmax = 152600, .val = 0x00 },
271 { .rfmax = 154300, .val = 0x1f },
272 { .rfmax = 156100, .val = 0x1e },
273 { .rfmax = 157800, .val = 0x1d },
274 { .rfmax = 159500, .val = 0x1c },
275 { .rfmax = 161200, .val = 0x1b },
276 { .rfmax = 163000, .val = 0x1a },
277 { .rfmax = 164700, .val = 0x19 },
278 { .rfmax = 170200, .val = 0x17 },
279 { .rfmax = 175800, .val = 0x16 },
280 { .rfmax = 181300, .val = 0x15 },
281 { .rfmax = 186900, .val = 0x14 },
282 { .rfmax = 192400, .val = 0x13 },
283 { .rfmax = 198000, .val = 0x12 },
284 { .rfmax = 203500, .val = 0x11 },
285 { .rfmax = 216200, .val = 0x14 },
286 { .rfmax = 228900, .val = 0x13 },
287 { .rfmax = 241600, .val = 0x12 },
288 { .rfmax = 254400, .val = 0x11 },
289 { .rfmax = 267100, .val = 0x10 },
290 { .rfmax = 279800, .val = 0x0f },
291 { .rfmax = 292500, .val = 0x0e },
292 { .rfmax = 305200, .val = 0x0d },
293 { .rfmax = 317900, .val = 0x0c },
294 { .rfmax = 330700, .val = 0x0b },
295 { .rfmax = 343400, .val = 0x0a },
296 { .rfmax = 356100, .val = 0x09 },
297 { .rfmax = 368800, .val = 0x08 },
298 { .rfmax = 381500, .val = 0x07 },
299 { .rfmax = 394200, .val = 0x06 },
300 { .rfmax = 406900, .val = 0x05 },
301 { .rfmax = 419700, .val = 0x04 },
302 { .rfmax = 432400, .val = 0x03 },
303 { .rfmax = 445100, .val = 0x02 },
304 { .rfmax = 457800, .val = 0x01 },
305 { .rfmax = 476300, .val = 0x19 },
306 { .rfmax = 494800, .val = 0x18 },
307 { .rfmax = 513300, .val = 0x17 },
308 { .rfmax = 531800, .val = 0x16 },
309 { .rfmax = 550300, .val = 0x15 },
310 { .rfmax = 568900, .val = 0x14 },
311 { .rfmax = 587400, .val = 0x13 },
312 { .rfmax = 605900, .val = 0x12 },
313 { .rfmax = 624400, .val = 0x11 },
314 { .rfmax = 642900, .val = 0x10 },
315 { .rfmax = 661400, .val = 0x0f },
316 { .rfmax = 679900, .val = 0x0e },
317 { .rfmax = 698400, .val = 0x0d },
318 { .rfmax = 716900, .val = 0x0c },
319 { .rfmax = 735400, .val = 0x0b },
320 { .rfmax = 753900, .val = 0x0a },
321 { .rfmax = 772500, .val = 0x09 },
322 { .rfmax = 791000, .val = 0x08 },
323 { .rfmax = 809500, .val = 0x07 },
324 { .rfmax = 828000, .val = 0x06 },
325 { .rfmax = 846500, .val = 0x05 },
326 { .rfmax = 865000, .val = 0x04 },
327 { .rfmax = 0, .val = 0x00 }, /* end */
328};
329
330static struct tda18271_map tda18271c1_rf_cal[] = {
331 { .rfmax = 41000, .val = 0x1e },
332 { .rfmax = 43000, .val = 0x30 },
333 { .rfmax = 45000, .val = 0x43 },
334 { .rfmax = 46000, .val = 0x4d },
335 { .rfmax = 47000, .val = 0x54 },
336 { .rfmax = 47900, .val = 0x64 },
337 { .rfmax = 49100, .val = 0x20 },
338 { .rfmax = 50000, .val = 0x22 },
339 { .rfmax = 51000, .val = 0x2a },
340 { .rfmax = 53000, .val = 0x32 },
341 { .rfmax = 55000, .val = 0x35 },
342 { .rfmax = 56000, .val = 0x3c },
343 { .rfmax = 57000, .val = 0x3f },
344 { .rfmax = 58000, .val = 0x48 },
345 { .rfmax = 59000, .val = 0x4d },
346 { .rfmax = 60000, .val = 0x58 },
347 { .rfmax = 61100, .val = 0x5f },
348 { .rfmax = 0, .val = 0x00 }, /* end */
349};
350
351static struct tda18271_map tda18271c2_rf_cal[] = {
352 { .rfmax = 41000, .val = 0x0f },
353 { .rfmax = 43000, .val = 0x1c },
354 { .rfmax = 45000, .val = 0x2f },
355 { .rfmax = 46000, .val = 0x39 },
356 { .rfmax = 47000, .val = 0x40 },
357 { .rfmax = 47900, .val = 0x50 },
358 { .rfmax = 49100, .val = 0x16 },
359 { .rfmax = 50000, .val = 0x18 },
360 { .rfmax = 51000, .val = 0x20 },
361 { .rfmax = 53000, .val = 0x28 },
362 { .rfmax = 55000, .val = 0x2b },
363 { .rfmax = 56000, .val = 0x32 },
364 { .rfmax = 57000, .val = 0x35 },
365 { .rfmax = 58000, .val = 0x3e },
366 { .rfmax = 59000, .val = 0x43 },
367 { .rfmax = 60000, .val = 0x4e },
368 { .rfmax = 61100, .val = 0x55 },
369 { .rfmax = 63000, .val = 0x0f },
370 { .rfmax = 64000, .val = 0x11 },
371 { .rfmax = 65000, .val = 0x12 },
372 { .rfmax = 66000, .val = 0x15 },
373 { .rfmax = 67000, .val = 0x16 },
374 { .rfmax = 68000, .val = 0x17 },
375 { .rfmax = 70000, .val = 0x19 },
376 { .rfmax = 71000, .val = 0x1c },
377 { .rfmax = 72000, .val = 0x1d },
378 { .rfmax = 73000, .val = 0x1f },
379 { .rfmax = 74000, .val = 0x20 },
380 { .rfmax = 75000, .val = 0x21 },
381 { .rfmax = 76000, .val = 0x24 },
382 { .rfmax = 77000, .val = 0x25 },
383 { .rfmax = 78000, .val = 0x27 },
384 { .rfmax = 80000, .val = 0x28 },
385 { .rfmax = 81000, .val = 0x29 },
386 { .rfmax = 82000, .val = 0x2d },
387 { .rfmax = 83000, .val = 0x2e },
388 { .rfmax = 84000, .val = 0x2f },
389 { .rfmax = 85000, .val = 0x31 },
390 { .rfmax = 86000, .val = 0x33 },
391 { .rfmax = 87000, .val = 0x34 },
392 { .rfmax = 88000, .val = 0x35 },
393 { .rfmax = 89000, .val = 0x37 },
394 { .rfmax = 90000, .val = 0x38 },
395 { .rfmax = 91000, .val = 0x39 },
396 { .rfmax = 93000, .val = 0x3c },
397 { .rfmax = 94000, .val = 0x3e },
398 { .rfmax = 95000, .val = 0x3f },
399 { .rfmax = 96000, .val = 0x40 },
400 { .rfmax = 97000, .val = 0x42 },
401 { .rfmax = 99000, .val = 0x45 },
402 { .rfmax = 100000, .val = 0x46 },
403 { .rfmax = 102000, .val = 0x48 },
404 { .rfmax = 103000, .val = 0x4a },
405 { .rfmax = 105000, .val = 0x4d },
406 { .rfmax = 106000, .val = 0x4e },
407 { .rfmax = 107000, .val = 0x50 },
408 { .rfmax = 108000, .val = 0x51 },
409 { .rfmax = 110000, .val = 0x54 },
410 { .rfmax = 111000, .val = 0x56 },
411 { .rfmax = 112000, .val = 0x57 },
412 { .rfmax = 113000, .val = 0x58 },
413 { .rfmax = 114000, .val = 0x59 },
414 { .rfmax = 115000, .val = 0x5c },
415 { .rfmax = 116000, .val = 0x5d },
416 { .rfmax = 117000, .val = 0x5f },
417 { .rfmax = 119000, .val = 0x60 },
418 { .rfmax = 120000, .val = 0x64 },
419 { .rfmax = 121000, .val = 0x65 },
420 { .rfmax = 122000, .val = 0x66 },
421 { .rfmax = 123000, .val = 0x68 },
422 { .rfmax = 124000, .val = 0x69 },
423 { .rfmax = 125000, .val = 0x6c },
424 { .rfmax = 126000, .val = 0x6d },
425 { .rfmax = 127000, .val = 0x6e },
426 { .rfmax = 128000, .val = 0x70 },
427 { .rfmax = 129000, .val = 0x71 },
428 { .rfmax = 130000, .val = 0x75 },
429 { .rfmax = 131000, .val = 0x77 },
430 { .rfmax = 132000, .val = 0x78 },
431 { .rfmax = 133000, .val = 0x7b },
432 { .rfmax = 134000, .val = 0x7e },
433 { .rfmax = 135000, .val = 0x81 },
434 { .rfmax = 136000, .val = 0x82 },
435 { .rfmax = 137000, .val = 0x87 },
436 { .rfmax = 138000, .val = 0x88 },
437 { .rfmax = 139000, .val = 0x8d },
438 { .rfmax = 140000, .val = 0x8e },
439 { .rfmax = 141000, .val = 0x91 },
440 { .rfmax = 142000, .val = 0x95 },
441 { .rfmax = 143000, .val = 0x9a },
442 { .rfmax = 144000, .val = 0x9d },
443 { .rfmax = 145000, .val = 0xa1 },
444 { .rfmax = 146000, .val = 0xa2 },
445 { .rfmax = 147000, .val = 0xa4 },
446 { .rfmax = 148000, .val = 0xa9 },
447 { .rfmax = 149000, .val = 0xae },
448 { .rfmax = 150000, .val = 0xb0 },
449 { .rfmax = 151000, .val = 0xb1 },
450 { .rfmax = 152000, .val = 0xb7 },
451 { .rfmax = 153000, .val = 0xbd },
452 { .rfmax = 154000, .val = 0x20 },
453 { .rfmax = 155000, .val = 0x22 },
454 { .rfmax = 156000, .val = 0x24 },
455 { .rfmax = 157000, .val = 0x25 },
456 { .rfmax = 158000, .val = 0x27 },
457 { .rfmax = 159000, .val = 0x29 },
458 { .rfmax = 160000, .val = 0x2c },
459 { .rfmax = 161000, .val = 0x2d },
460 { .rfmax = 163000, .val = 0x2e },
461 { .rfmax = 164000, .val = 0x2f },
462 { .rfmax = 165000, .val = 0x30 },
463 { .rfmax = 166000, .val = 0x11 },
464 { .rfmax = 167000, .val = 0x12 },
465 { .rfmax = 168000, .val = 0x13 },
466 { .rfmax = 169000, .val = 0x14 },
467 { .rfmax = 170000, .val = 0x15 },
468 { .rfmax = 172000, .val = 0x16 },
469 { .rfmax = 173000, .val = 0x17 },
470 { .rfmax = 174000, .val = 0x18 },
471 { .rfmax = 175000, .val = 0x1a },
472 { .rfmax = 176000, .val = 0x1b },
473 { .rfmax = 178000, .val = 0x1d },
474 { .rfmax = 179000, .val = 0x1e },
475 { .rfmax = 180000, .val = 0x1f },
476 { .rfmax = 181000, .val = 0x20 },
477 { .rfmax = 182000, .val = 0x21 },
478 { .rfmax = 183000, .val = 0x22 },
479 { .rfmax = 184000, .val = 0x24 },
480 { .rfmax = 185000, .val = 0x25 },
481 { .rfmax = 186000, .val = 0x26 },
482 { .rfmax = 187000, .val = 0x27 },
483 { .rfmax = 188000, .val = 0x29 },
484 { .rfmax = 189000, .val = 0x2a },
485 { .rfmax = 190000, .val = 0x2c },
486 { .rfmax = 191000, .val = 0x2d },
487 { .rfmax = 192000, .val = 0x2e },
488 { .rfmax = 193000, .val = 0x2f },
489 { .rfmax = 194000, .val = 0x30 },
490 { .rfmax = 195000, .val = 0x33 },
491 { .rfmax = 196000, .val = 0x35 },
492 { .rfmax = 198000, .val = 0x36 },
493 { .rfmax = 200000, .val = 0x38 },
494 { .rfmax = 201000, .val = 0x3c },
495 { .rfmax = 202000, .val = 0x3d },
496 { .rfmax = 203500, .val = 0x3e },
497 { .rfmax = 206000, .val = 0x0e },
498 { .rfmax = 208000, .val = 0x0f },
499 { .rfmax = 212000, .val = 0x10 },
500 { .rfmax = 216000, .val = 0x11 },
501 { .rfmax = 217000, .val = 0x12 },
502 { .rfmax = 218000, .val = 0x13 },
503 { .rfmax = 220000, .val = 0x14 },
504 { .rfmax = 222000, .val = 0x15 },
505 { .rfmax = 225000, .val = 0x16 },
506 { .rfmax = 228000, .val = 0x17 },
507 { .rfmax = 231000, .val = 0x18 },
508 { .rfmax = 234000, .val = 0x19 },
509 { .rfmax = 235000, .val = 0x1a },
510 { .rfmax = 236000, .val = 0x1b },
511 { .rfmax = 237000, .val = 0x1c },
512 { .rfmax = 240000, .val = 0x1d },
513 { .rfmax = 242000, .val = 0x1f },
514 { .rfmax = 247000, .val = 0x20 },
515 { .rfmax = 249000, .val = 0x21 },
516 { .rfmax = 252000, .val = 0x22 },
517 { .rfmax = 253000, .val = 0x23 },
518 { .rfmax = 254000, .val = 0x24 },
519 { .rfmax = 256000, .val = 0x25 },
520 { .rfmax = 259000, .val = 0x26 },
521 { .rfmax = 262000, .val = 0x27 },
522 { .rfmax = 264000, .val = 0x28 },
523 { .rfmax = 267000, .val = 0x29 },
524 { .rfmax = 269000, .val = 0x2a },
525 { .rfmax = 271000, .val = 0x2b },
526 { .rfmax = 273000, .val = 0x2c },
527 { .rfmax = 275000, .val = 0x2d },
528 { .rfmax = 277000, .val = 0x2e },
529 { .rfmax = 279000, .val = 0x2f },
530 { .rfmax = 282000, .val = 0x30 },
531 { .rfmax = 284000, .val = 0x31 },
532 { .rfmax = 286000, .val = 0x32 },
533 { .rfmax = 287000, .val = 0x33 },
534 { .rfmax = 290000, .val = 0x34 },
535 { .rfmax = 293000, .val = 0x35 },
536 { .rfmax = 295000, .val = 0x36 },
537 { .rfmax = 297000, .val = 0x37 },
538 { .rfmax = 300000, .val = 0x38 },
539 { .rfmax = 303000, .val = 0x39 },
540 { .rfmax = 305000, .val = 0x3a },
541 { .rfmax = 306000, .val = 0x3b },
542 { .rfmax = 307000, .val = 0x3c },
543 { .rfmax = 310000, .val = 0x3d },
544 { .rfmax = 312000, .val = 0x3e },
545 { .rfmax = 315000, .val = 0x3f },
546 { .rfmax = 318000, .val = 0x40 },
547 { .rfmax = 320000, .val = 0x41 },
548 { .rfmax = 323000, .val = 0x42 },
549 { .rfmax = 324000, .val = 0x43 },
550 { .rfmax = 325000, .val = 0x44 },
551 { .rfmax = 327000, .val = 0x45 },
552 { .rfmax = 331000, .val = 0x46 },
553 { .rfmax = 334000, .val = 0x47 },
554 { .rfmax = 337000, .val = 0x48 },
555 { .rfmax = 339000, .val = 0x49 },
556 { .rfmax = 340000, .val = 0x4a },
557 { .rfmax = 341000, .val = 0x4b },
558 { .rfmax = 343000, .val = 0x4c },
559 { .rfmax = 345000, .val = 0x4d },
560 { .rfmax = 349000, .val = 0x4e },
561 { .rfmax = 352000, .val = 0x4f },
562 { .rfmax = 353000, .val = 0x50 },
563 { .rfmax = 355000, .val = 0x51 },
564 { .rfmax = 357000, .val = 0x52 },
565 { .rfmax = 359000, .val = 0x53 },
566 { .rfmax = 361000, .val = 0x54 },
567 { .rfmax = 362000, .val = 0x55 },
568 { .rfmax = 364000, .val = 0x56 },
569 { .rfmax = 368000, .val = 0x57 },
570 { .rfmax = 370000, .val = 0x58 },
571 { .rfmax = 372000, .val = 0x59 },
572 { .rfmax = 375000, .val = 0x5a },
573 { .rfmax = 376000, .val = 0x5b },
574 { .rfmax = 377000, .val = 0x5c },
575 { .rfmax = 379000, .val = 0x5d },
576 { .rfmax = 382000, .val = 0x5e },
577 { .rfmax = 384000, .val = 0x5f },
578 { .rfmax = 385000, .val = 0x60 },
579 { .rfmax = 386000, .val = 0x61 },
580 { .rfmax = 388000, .val = 0x62 },
581 { .rfmax = 390000, .val = 0x63 },
582 { .rfmax = 393000, .val = 0x64 },
583 { .rfmax = 394000, .val = 0x65 },
584 { .rfmax = 396000, .val = 0x66 },
585 { .rfmax = 397000, .val = 0x67 },
586 { .rfmax = 398000, .val = 0x68 },
587 { .rfmax = 400000, .val = 0x69 },
588 { .rfmax = 402000, .val = 0x6a },
589 { .rfmax = 403000, .val = 0x6b },
590 { .rfmax = 407000, .val = 0x6c },
591 { .rfmax = 408000, .val = 0x6d },
592 { .rfmax = 409000, .val = 0x6e },
593 { .rfmax = 410000, .val = 0x6f },
594 { .rfmax = 411000, .val = 0x70 },
595 { .rfmax = 412000, .val = 0x71 },
596 { .rfmax = 413000, .val = 0x72 },
597 { .rfmax = 414000, .val = 0x73 },
598 { .rfmax = 417000, .val = 0x74 },
599 { .rfmax = 418000, .val = 0x75 },
600 { .rfmax = 420000, .val = 0x76 },
601 { .rfmax = 422000, .val = 0x77 },
602 { .rfmax = 423000, .val = 0x78 },
603 { .rfmax = 424000, .val = 0x79 },
604 { .rfmax = 427000, .val = 0x7a },
605 { .rfmax = 428000, .val = 0x7b },
606 { .rfmax = 429000, .val = 0x7d },
607 { .rfmax = 432000, .val = 0x7f },
608 { .rfmax = 434000, .val = 0x80 },
609 { .rfmax = 435000, .val = 0x81 },
610 { .rfmax = 436000, .val = 0x83 },
611 { .rfmax = 437000, .val = 0x84 },
612 { .rfmax = 438000, .val = 0x85 },
613 { .rfmax = 439000, .val = 0x86 },
614 { .rfmax = 440000, .val = 0x87 },
615 { .rfmax = 441000, .val = 0x88 },
616 { .rfmax = 442000, .val = 0x89 },
617 { .rfmax = 445000, .val = 0x8a },
618 { .rfmax = 446000, .val = 0x8b },
619 { .rfmax = 447000, .val = 0x8c },
620 { .rfmax = 448000, .val = 0x8e },
621 { .rfmax = 449000, .val = 0x8f },
622 { .rfmax = 450000, .val = 0x90 },
623 { .rfmax = 452000, .val = 0x91 },
624 { .rfmax = 453000, .val = 0x93 },
625 { .rfmax = 454000, .val = 0x94 },
626 { .rfmax = 456000, .val = 0x96 },
627 { .rfmax = 457000, .val = 0x98 },
628 { .rfmax = 461000, .val = 0x11 },
629 { .rfmax = 468000, .val = 0x12 },
630 { .rfmax = 472000, .val = 0x13 },
631 { .rfmax = 473000, .val = 0x14 },
632 { .rfmax = 474000, .val = 0x15 },
633 { .rfmax = 481000, .val = 0x16 },
634 { .rfmax = 486000, .val = 0x17 },
635 { .rfmax = 491000, .val = 0x18 },
636 { .rfmax = 498000, .val = 0x19 },
637 { .rfmax = 499000, .val = 0x1a },
638 { .rfmax = 501000, .val = 0x1b },
639 { .rfmax = 506000, .val = 0x1c },
640 { .rfmax = 511000, .val = 0x1d },
641 { .rfmax = 516000, .val = 0x1e },
642 { .rfmax = 520000, .val = 0x1f },
643 { .rfmax = 521000, .val = 0x20 },
644 { .rfmax = 525000, .val = 0x21 },
645 { .rfmax = 529000, .val = 0x22 },
646 { .rfmax = 533000, .val = 0x23 },
647 { .rfmax = 539000, .val = 0x24 },
648 { .rfmax = 541000, .val = 0x25 },
649 { .rfmax = 547000, .val = 0x26 },
650 { .rfmax = 549000, .val = 0x27 },
651 { .rfmax = 551000, .val = 0x28 },
652 { .rfmax = 556000, .val = 0x29 },
653 { .rfmax = 561000, .val = 0x2a },
654 { .rfmax = 563000, .val = 0x2b },
655 { .rfmax = 565000, .val = 0x2c },
656 { .rfmax = 569000, .val = 0x2d },
657 { .rfmax = 571000, .val = 0x2e },
658 { .rfmax = 577000, .val = 0x2f },
659 { .rfmax = 580000, .val = 0x30 },
660 { .rfmax = 582000, .val = 0x31 },
661 { .rfmax = 584000, .val = 0x32 },
662 { .rfmax = 588000, .val = 0x33 },
663 { .rfmax = 591000, .val = 0x34 },
664 { .rfmax = 596000, .val = 0x35 },
665 { .rfmax = 598000, .val = 0x36 },
666 { .rfmax = 603000, .val = 0x37 },
667 { .rfmax = 604000, .val = 0x38 },
668 { .rfmax = 606000, .val = 0x39 },
669 { .rfmax = 612000, .val = 0x3a },
670 { .rfmax = 615000, .val = 0x3b },
671 { .rfmax = 617000, .val = 0x3c },
672 { .rfmax = 621000, .val = 0x3d },
673 { .rfmax = 622000, .val = 0x3e },
674 { .rfmax = 625000, .val = 0x3f },
675 { .rfmax = 632000, .val = 0x40 },
676 { .rfmax = 633000, .val = 0x41 },
677 { .rfmax = 634000, .val = 0x42 },
678 { .rfmax = 642000, .val = 0x43 },
679 { .rfmax = 643000, .val = 0x44 },
680 { .rfmax = 647000, .val = 0x45 },
681 { .rfmax = 650000, .val = 0x46 },
682 { .rfmax = 652000, .val = 0x47 },
683 { .rfmax = 657000, .val = 0x48 },
684 { .rfmax = 661000, .val = 0x49 },
685 { .rfmax = 662000, .val = 0x4a },
686 { .rfmax = 665000, .val = 0x4b },
687 { .rfmax = 667000, .val = 0x4c },
688 { .rfmax = 670000, .val = 0x4d },
689 { .rfmax = 673000, .val = 0x4e },
690 { .rfmax = 676000, .val = 0x4f },
691 { .rfmax = 677000, .val = 0x50 },
692 { .rfmax = 681000, .val = 0x51 },
693 { .rfmax = 683000, .val = 0x52 },
694 { .rfmax = 686000, .val = 0x53 },
695 { .rfmax = 688000, .val = 0x54 },
696 { .rfmax = 689000, .val = 0x55 },
697 { .rfmax = 691000, .val = 0x56 },
698 { .rfmax = 695000, .val = 0x57 },
699 { .rfmax = 698000, .val = 0x58 },
700 { .rfmax = 703000, .val = 0x59 },
701 { .rfmax = 704000, .val = 0x5a },
702 { .rfmax = 705000, .val = 0x5b },
703 { .rfmax = 707000, .val = 0x5c },
704 { .rfmax = 710000, .val = 0x5d },
705 { .rfmax = 712000, .val = 0x5e },
706 { .rfmax = 717000, .val = 0x5f },
707 { .rfmax = 718000, .val = 0x60 },
708 { .rfmax = 721000, .val = 0x61 },
709 { .rfmax = 722000, .val = 0x62 },
710 { .rfmax = 723000, .val = 0x63 },
711 { .rfmax = 725000, .val = 0x64 },
712 { .rfmax = 727000, .val = 0x65 },
713 { .rfmax = 730000, .val = 0x66 },
714 { .rfmax = 732000, .val = 0x67 },
715 { .rfmax = 735000, .val = 0x68 },
716 { .rfmax = 740000, .val = 0x69 },
717 { .rfmax = 741000, .val = 0x6a },
718 { .rfmax = 742000, .val = 0x6b },
719 { .rfmax = 743000, .val = 0x6c },
720 { .rfmax = 745000, .val = 0x6d },
721 { .rfmax = 747000, .val = 0x6e },
722 { .rfmax = 748000, .val = 0x6f },
723 { .rfmax = 750000, .val = 0x70 },
724 { .rfmax = 752000, .val = 0x71 },
725 { .rfmax = 754000, .val = 0x72 },
726 { .rfmax = 757000, .val = 0x73 },
727 { .rfmax = 758000, .val = 0x74 },
728 { .rfmax = 760000, .val = 0x75 },
729 { .rfmax = 763000, .val = 0x76 },
730 { .rfmax = 764000, .val = 0x77 },
731 { .rfmax = 766000, .val = 0x78 },
732 { .rfmax = 767000, .val = 0x79 },
733 { .rfmax = 768000, .val = 0x7a },
734 { .rfmax = 773000, .val = 0x7b },
735 { .rfmax = 774000, .val = 0x7c },
736 { .rfmax = 776000, .val = 0x7d },
737 { .rfmax = 777000, .val = 0x7e },
738 { .rfmax = 778000, .val = 0x7f },
739 { .rfmax = 779000, .val = 0x80 },
740 { .rfmax = 781000, .val = 0x81 },
741 { .rfmax = 783000, .val = 0x82 },
742 { .rfmax = 784000, .val = 0x83 },
743 { .rfmax = 785000, .val = 0x84 },
744 { .rfmax = 786000, .val = 0x85 },
745 { .rfmax = 793000, .val = 0x86 },
746 { .rfmax = 794000, .val = 0x87 },
747 { .rfmax = 795000, .val = 0x88 },
748 { .rfmax = 797000, .val = 0x89 },
749 { .rfmax = 799000, .val = 0x8a },
750 { .rfmax = 801000, .val = 0x8b },
751 { .rfmax = 802000, .val = 0x8c },
752 { .rfmax = 803000, .val = 0x8d },
753 { .rfmax = 804000, .val = 0x8e },
754 { .rfmax = 810000, .val = 0x90 },
755 { .rfmax = 811000, .val = 0x91 },
756 { .rfmax = 812000, .val = 0x92 },
757 { .rfmax = 814000, .val = 0x93 },
758 { .rfmax = 816000, .val = 0x94 },
759 { .rfmax = 817000, .val = 0x96 },
760 { .rfmax = 818000, .val = 0x97 },
761 { .rfmax = 820000, .val = 0x98 },
762 { .rfmax = 821000, .val = 0x99 },
763 { .rfmax = 822000, .val = 0x9a },
764 { .rfmax = 828000, .val = 0x9b },
765 { .rfmax = 829000, .val = 0x9d },
766 { .rfmax = 830000, .val = 0x9f },
767 { .rfmax = 831000, .val = 0xa0 },
768 { .rfmax = 833000, .val = 0xa1 },
769 { .rfmax = 835000, .val = 0xa2 },
770 { .rfmax = 836000, .val = 0xa3 },
771 { .rfmax = 837000, .val = 0xa4 },
772 { .rfmax = 838000, .val = 0xa6 },
773 { .rfmax = 840000, .val = 0xa8 },
774 { .rfmax = 842000, .val = 0xa9 },
775 { .rfmax = 845000, .val = 0xaa },
776 { .rfmax = 846000, .val = 0xab },
777 { .rfmax = 847000, .val = 0xad },
778 { .rfmax = 848000, .val = 0xae },
779 { .rfmax = 852000, .val = 0xaf },
780 { .rfmax = 853000, .val = 0xb0 },
781 { .rfmax = 858000, .val = 0xb1 },
782 { .rfmax = 860000, .val = 0xb2 },
783 { .rfmax = 861000, .val = 0xb3 },
784 { .rfmax = 862000, .val = 0xb4 },
785 { .rfmax = 863000, .val = 0xb6 },
786 { .rfmax = 864000, .val = 0xb8 },
787 { .rfmax = 865000, .val = 0xb9 },
788 { .rfmax = 0, .val = 0x00 }, /* end */
789};
790
791static struct tda18271_map tda18271_ir_measure[] = {
792 { .rfmax = 30000, .val = 4 },
793 { .rfmax = 200000, .val = 5 },
794 { .rfmax = 600000, .val = 6 },
795 { .rfmax = 865000, .val = 7 },
796 { .rfmax = 0, .val = 0 }, /* end */
797};
798
799static struct tda18271_map tda18271_rf_cal_dc_over_dt[] = {
800 { .rfmax = 47900, .val = 0x00 },
801 { .rfmax = 55000, .val = 0x00 },
802 { .rfmax = 61100, .val = 0x0a },
803 { .rfmax = 64000, .val = 0x0a },
804 { .rfmax = 82000, .val = 0x14 },
805 { .rfmax = 84000, .val = 0x19 },
806 { .rfmax = 119000, .val = 0x1c },
807 { .rfmax = 124000, .val = 0x20 },
808 { .rfmax = 129000, .val = 0x2a },
809 { .rfmax = 134000, .val = 0x32 },
810 { .rfmax = 139000, .val = 0x39 },
811 { .rfmax = 144000, .val = 0x3e },
812 { .rfmax = 149000, .val = 0x3f },
813 { .rfmax = 152600, .val = 0x40 },
814 { .rfmax = 154000, .val = 0x40 },
815 { .rfmax = 164700, .val = 0x41 },
816 { .rfmax = 203500, .val = 0x32 },
817 { .rfmax = 353000, .val = 0x19 },
818 { .rfmax = 356000, .val = 0x1a },
819 { .rfmax = 359000, .val = 0x1b },
820 { .rfmax = 363000, .val = 0x1c },
821 { .rfmax = 366000, .val = 0x1d },
822 { .rfmax = 369000, .val = 0x1e },
823 { .rfmax = 373000, .val = 0x1f },
824 { .rfmax = 376000, .val = 0x20 },
825 { .rfmax = 379000, .val = 0x21 },
826 { .rfmax = 383000, .val = 0x22 },
827 { .rfmax = 386000, .val = 0x23 },
828 { .rfmax = 389000, .val = 0x24 },
829 { .rfmax = 393000, .val = 0x25 },
830 { .rfmax = 396000, .val = 0x26 },
831 { .rfmax = 399000, .val = 0x27 },
832 { .rfmax = 402000, .val = 0x28 },
833 { .rfmax = 404000, .val = 0x29 },
834 { .rfmax = 407000, .val = 0x2a },
835 { .rfmax = 409000, .val = 0x2b },
836 { .rfmax = 412000, .val = 0x2c },
837 { .rfmax = 414000, .val = 0x2d },
838 { .rfmax = 417000, .val = 0x2e },
839 { .rfmax = 419000, .val = 0x2f },
840 { .rfmax = 422000, .val = 0x30 },
841 { .rfmax = 424000, .val = 0x31 },
842 { .rfmax = 427000, .val = 0x32 },
843 { .rfmax = 429000, .val = 0x33 },
844 { .rfmax = 432000, .val = 0x34 },
845 { .rfmax = 434000, .val = 0x35 },
846 { .rfmax = 437000, .val = 0x36 },
847 { .rfmax = 439000, .val = 0x37 },
848 { .rfmax = 442000, .val = 0x38 },
849 { .rfmax = 444000, .val = 0x39 },
850 { .rfmax = 447000, .val = 0x3a },
851 { .rfmax = 449000, .val = 0x3b },
852 { .rfmax = 457800, .val = 0x3c },
853 { .rfmax = 465000, .val = 0x0f },
854 { .rfmax = 477000, .val = 0x12 },
855 { .rfmax = 483000, .val = 0x14 },
856 { .rfmax = 502000, .val = 0x19 },
857 { .rfmax = 508000, .val = 0x1b },
858 { .rfmax = 519000, .val = 0x1c },
859 { .rfmax = 522000, .val = 0x1d },
860 { .rfmax = 524000, .val = 0x1e },
861 { .rfmax = 534000, .val = 0x1f },
862 { .rfmax = 549000, .val = 0x20 },
863 { .rfmax = 554000, .val = 0x22 },
864 { .rfmax = 584000, .val = 0x24 },
865 { .rfmax = 589000, .val = 0x26 },
866 { .rfmax = 658000, .val = 0x27 },
867 { .rfmax = 664000, .val = 0x2c },
868 { .rfmax = 669000, .val = 0x2d },
869 { .rfmax = 699000, .val = 0x2e },
870 { .rfmax = 704000, .val = 0x30 },
871 { .rfmax = 709000, .val = 0x31 },
872 { .rfmax = 714000, .val = 0x32 },
873 { .rfmax = 724000, .val = 0x33 },
874 { .rfmax = 729000, .val = 0x36 },
875 { .rfmax = 739000, .val = 0x38 },
876 { .rfmax = 744000, .val = 0x39 },
877 { .rfmax = 749000, .val = 0x3b },
878 { .rfmax = 754000, .val = 0x3c },
879 { .rfmax = 759000, .val = 0x3d },
880 { .rfmax = 764000, .val = 0x3e },
881 { .rfmax = 769000, .val = 0x3f },
882 { .rfmax = 774000, .val = 0x40 },
883 { .rfmax = 779000, .val = 0x41 },
884 { .rfmax = 784000, .val = 0x43 },
885 { .rfmax = 789000, .val = 0x46 },
886 { .rfmax = 794000, .val = 0x48 },
887 { .rfmax = 799000, .val = 0x4b },
888 { .rfmax = 804000, .val = 0x4f },
889 { .rfmax = 809000, .val = 0x54 },
890 { .rfmax = 814000, .val = 0x59 },
891 { .rfmax = 819000, .val = 0x5d },
892 { .rfmax = 824000, .val = 0x61 },
893 { .rfmax = 829000, .val = 0x68 },
894 { .rfmax = 834000, .val = 0x6e },
895 { .rfmax = 839000, .val = 0x75 },
896 { .rfmax = 844000, .val = 0x7e },
897 { .rfmax = 849000, .val = 0x82 },
898 { .rfmax = 854000, .val = 0x84 },
899 { .rfmax = 859000, .val = 0x8f },
900 { .rfmax = 865000, .val = 0x9a },
901 { .rfmax = 0, .val = 0x00 }, /* end */
902};
903
904/*---------------------------------------------------------------------*/
905
906struct tda18271_thermo_map {
907 u8 d;
908 u8 r0;
909 u8 r1;
910};
911
912static struct tda18271_thermo_map tda18271_thermometer[] = {
913 { .d = 0x00, .r0 = 60, .r1 = 92 },
914 { .d = 0x01, .r0 = 62, .r1 = 94 },
915 { .d = 0x02, .r0 = 66, .r1 = 98 },
916 { .d = 0x03, .r0 = 64, .r1 = 96 },
917 { .d = 0x04, .r0 = 74, .r1 = 106 },
918 { .d = 0x05, .r0 = 72, .r1 = 104 },
919 { .d = 0x06, .r0 = 68, .r1 = 100 },
920 { .d = 0x07, .r0 = 70, .r1 = 102 },
921 { .d = 0x08, .r0 = 90, .r1 = 122 },
922 { .d = 0x09, .r0 = 88, .r1 = 120 },
923 { .d = 0x0a, .r0 = 84, .r1 = 116 },
924 { .d = 0x0b, .r0 = 86, .r1 = 118 },
925 { .d = 0x0c, .r0 = 76, .r1 = 108 },
926 { .d = 0x0d, .r0 = 78, .r1 = 110 },
927 { .d = 0x0e, .r0 = 82, .r1 = 114 },
928 { .d = 0x0f, .r0 = 80, .r1 = 112 },
929 { .d = 0x00, .r0 = 0, .r1 = 0 }, /* end */
930};
931
932int tda18271_lookup_thermometer(struct dvb_frontend *fe)
933{
934 struct tda18271_priv *priv = fe->tuner_priv;
935 unsigned char *regs = priv->tda18271_regs;
936 int val, i = 0;
937
938 while (tda18271_thermometer[i].d < (regs[R_TM] & 0x0f)) {
939 if (tda18271_thermometer[i + 1].d == 0)
940 break;
941 i++;
942 }
943
944 if ((regs[R_TM] & 0x20) == 0x20)
945 val = tda18271_thermometer[i].r1;
946 else
947 val = tda18271_thermometer[i].r0;
948
949 tda_map("(%d) tm = %d\n", i, val);
950
951 return val;
952}
953
954/*---------------------------------------------------------------------*/
955
956struct tda18271_cid_target_map {
957 u32 rfmax;
958 u8 target;
959 u16 limit;
960};
961
962static struct tda18271_cid_target_map tda18271_cid_target[] = {
963 { .rfmax = 46000, .target = 0x04, .limit = 1800 },
964 { .rfmax = 52200, .target = 0x0a, .limit = 1500 },
965 { .rfmax = 79100, .target = 0x01, .limit = 4000 },
966 { .rfmax = 136800, .target = 0x18, .limit = 4000 },
967 { .rfmax = 156700, .target = 0x18, .limit = 4000 },
968 { .rfmax = 156700, .target = 0x18, .limit = 4000 },
969 { .rfmax = 186250, .target = 0x0a, .limit = 4000 },
970 { .rfmax = 230000, .target = 0x0a, .limit = 4000 },
971 { .rfmax = 345000, .target = 0x18, .limit = 4000 },
972 { .rfmax = 426000, .target = 0x0e, .limit = 4000 },
973 { .rfmax = 489500, .target = 0x1e, .limit = 4000 },
974 { .rfmax = 697500, .target = 0x32, .limit = 4000 },
975 { .rfmax = 842000, .target = 0x3a, .limit = 4000 },
976 { .rfmax = 0, .target = 0x00, .limit = 0 }, /* end */
977};
978
979int tda18271_lookup_cid_target(struct dvb_frontend *fe,
980 u32 *freq, u8 *cid_target, u16 *count_limit)
981{
982 int i = 0;
983
984 while ((tda18271_cid_target[i].rfmax * 1000) < *freq) {
985 if (tda18271_cid_target[i + 1].rfmax == 0)
986 break;
987 i++;
988 }
989 *cid_target = tda18271_cid_target[i].target;
990 *count_limit = tda18271_cid_target[i].limit;
991
992 tda_map("(%d) cid_target = %02x, count_limit = %d\n", i,
993 tda18271_cid_target[i].target, tda18271_cid_target[i].limit);
994
995 return 0;
996}
997
998/*---------------------------------------------------------------------*/
999
1000static struct tda18271_rf_tracking_filter_cal tda18271_rf_band_template[] = {
1001 { .rfmax = 47900, .rfband = 0x00,
1002 .rf1_def = 46000, .rf2_def = 0, .rf3_def = 0 },
1003 { .rfmax = 61100, .rfband = 0x01,
1004 .rf1_def = 52200, .rf2_def = 0, .rf3_def = 0 },
1005 { .rfmax = 152600, .rfband = 0x02,
1006 .rf1_def = 70100, .rf2_def = 136800, .rf3_def = 0 },
1007 { .rfmax = 164700, .rfband = 0x03,
1008 .rf1_def = 156700, .rf2_def = 0, .rf3_def = 0 },
1009 { .rfmax = 203500, .rfband = 0x04,
1010 .rf1_def = 186250, .rf2_def = 0, .rf3_def = 0 },
1011 { .rfmax = 457800, .rfband = 0x05,
1012 .rf1_def = 230000, .rf2_def = 345000, .rf3_def = 426000 },
1013 { .rfmax = 865000, .rfband = 0x06,
1014 .rf1_def = 489500, .rf2_def = 697500, .rf3_def = 842000 },
1015 { .rfmax = 0, .rfband = 0x00,
1016 .rf1_def = 0, .rf2_def = 0, .rf3_def = 0 }, /* end */
1017};
1018
1019int tda18271_lookup_rf_band(struct dvb_frontend *fe, u32 *freq, u8 *rf_band)
1020{
1021 struct tda18271_priv *priv = fe->tuner_priv;
1022 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
1023 int i = 0;
1024
1025 while ((map[i].rfmax * 1000) < *freq) {
1026 if (tda18271_debug & DBG_ADV)
1027 tda_map("(%d) rfmax = %d < freq = %d, "
1028 "rf1_def = %d, rf2_def = %d, rf3_def = %d, "
1029 "rf1 = %d, rf2 = %d, rf3 = %d, "
1030 "rf_a1 = %d, rf_a2 = %d, "
1031 "rf_b1 = %d, rf_b2 = %d\n",
1032 i, map[i].rfmax * 1000, *freq,
1033 map[i].rf1_def, map[i].rf2_def, map[i].rf3_def,
1034 map[i].rf1, map[i].rf2, map[i].rf3,
1035 map[i].rf_a1, map[i].rf_a2,
1036 map[i].rf_b1, map[i].rf_b2);
1037 if (map[i].rfmax == 0)
1038 return -EINVAL;
1039 i++;
1040 }
1041 if (rf_band)
1042 *rf_band = map[i].rfband;
1043
1044 tda_map("(%d) rf_band = %02x\n", i, map[i].rfband);
1045
1046 return i;
1047}
1048
1049/*---------------------------------------------------------------------*/
1050
1051struct tda18271_map_layout {
1052 struct tda18271_pll_map *main_pll;
1053 struct tda18271_pll_map *cal_pll;
1054
1055 struct tda18271_map *rf_cal;
1056 struct tda18271_map *rf_cal_kmco;
1057 struct tda18271_map *rf_cal_dc_over_dt;
1058
1059 struct tda18271_map *bp_filter;
1060 struct tda18271_map *rf_band;
1061 struct tda18271_map *gain_taper;
1062 struct tda18271_map *ir_measure;
1063};
1064
1065/*---------------------------------------------------------------------*/
1066
1067int tda18271_lookup_pll_map(struct dvb_frontend *fe,
1068 enum tda18271_map_type map_type,
1069 u32 *freq, u8 *post_div, u8 *div)
1070{
1071 struct tda18271_priv *priv = fe->tuner_priv;
1072 struct tda18271_pll_map *map = NULL;
1073 unsigned int i = 0;
1074 char *map_name;
1075 int ret = 0;
1076
1077 BUG_ON(!priv->maps);
1078
1079 switch (map_type) {
1080 case MAIN_PLL:
1081 map = priv->maps->main_pll;
1082 map_name = "main_pll";
1083 break;
1084 case CAL_PLL:
1085 map = priv->maps->cal_pll;
1086 map_name = "cal_pll";
1087 break;
1088 default:
1089 /* we should never get here */
1090 map_name = "undefined";
1091 break;
1092 }
1093
1094 if (!map) {
1095 tda_warn("%s map is not set!\n", map_name);
1096 ret = -EINVAL;
1097 goto fail;
1098 }
1099
1100 while ((map[i].lomax * 1000) < *freq) {
1101 if (map[i + 1].lomax == 0) {
1102 tda_map("%s: frequency (%d) out of range\n",
1103 map_name, *freq);
1104 ret = -ERANGE;
1105 break;
1106 }
1107 i++;
1108 }
1109 *post_div = map[i].pd;
1110 *div = map[i].d;
1111
1112 tda_map("(%d) %s: post div = 0x%02x, div = 0x%02x\n",
1113 i, map_name, *post_div, *div);
1114fail:
1115 return ret;
1116}
1117
1118int tda18271_lookup_map(struct dvb_frontend *fe,
1119 enum tda18271_map_type map_type,
1120 u32 *freq, u8 *val)
1121{
1122 struct tda18271_priv *priv = fe->tuner_priv;
1123 struct tda18271_map *map = NULL;
1124 unsigned int i = 0;
1125 char *map_name;
1126 int ret = 0;
1127
1128 BUG_ON(!priv->maps);
1129
1130 switch (map_type) {
1131 case BP_FILTER:
1132 map = priv->maps->bp_filter;
1133 map_name = "bp_filter";
1134 break;
1135 case RF_CAL_KMCO:
1136 map = priv->maps->rf_cal_kmco;
1137 map_name = "km";
1138 break;
1139 case RF_BAND:
1140 map = priv->maps->rf_band;
1141 map_name = "rf_band";
1142 break;
1143 case GAIN_TAPER:
1144 map = priv->maps->gain_taper;
1145 map_name = "gain_taper";
1146 break;
1147 case RF_CAL:
1148 map = priv->maps->rf_cal;
1149 map_name = "rf_cal";
1150 break;
1151 case IR_MEASURE:
1152 map = priv->maps->ir_measure;
1153 map_name = "ir_measure";
1154 break;
1155 case RF_CAL_DC_OVER_DT:
1156 map = priv->maps->rf_cal_dc_over_dt;
1157 map_name = "rf_cal_dc_over_dt";
1158 break;
1159 default:
1160 /* we should never get here */
1161 map_name = "undefined";
1162 break;
1163 }
1164
1165 if (!map) {
1166 tda_warn("%s map is not set!\n", map_name);
1167 ret = -EINVAL;
1168 goto fail;
1169 }
1170
1171 while ((map[i].rfmax * 1000) < *freq) {
1172 if (map[i + 1].rfmax == 0) {
1173 tda_map("%s: frequency (%d) out of range\n",
1174 map_name, *freq);
1175 ret = -ERANGE;
1176 break;
1177 }
1178 i++;
1179 }
1180 *val = map[i].val;
1181
1182 tda_map("(%d) %s: 0x%02x\n", i, map_name, *val);
1183fail:
1184 return ret;
1185}
1186
1187/*---------------------------------------------------------------------*/
1188
1189static struct tda18271_std_map tda18271c1_std_map = {
1190 .fm_radio = { .if_freq = 1250, .std_bits = 0x18 },
1191 .atv_b = { .if_freq = 6750, .std_bits = 0x0e },
1192 .atv_dk = { .if_freq = 7750, .std_bits = 0x0f },
1193 .atv_gh = { .if_freq = 7750, .std_bits = 0x0f },
1194 .atv_i = { .if_freq = 7750, .std_bits = 0x0f },
1195 .atv_l = { .if_freq = 7750, .std_bits = 0x0f },
1196 .atv_lc = { .if_freq = 1250, .std_bits = 0x0f },
1197 .atv_mn = { .if_freq = 5750, .std_bits = 0x0d },
1198 .atsc_6 = { .if_freq = 3250, .std_bits = 0x1c },
1199 .dvbt_6 = { .if_freq = 3300, .std_bits = 0x1c },
1200 .dvbt_7 = { .if_freq = 3800, .std_bits = 0x1d },
1201 .dvbt_8 = { .if_freq = 4300, .std_bits = 0x1e },
1202 .qam_6 = { .if_freq = 4000, .std_bits = 0x1d },
1203 .qam_8 = { .if_freq = 5000, .std_bits = 0x1f },
1204};
1205
1206static struct tda18271_std_map tda18271c2_std_map = {
1207 .fm_radio = { .if_freq = 1250, .std_bits = 0x18 },
1208 .atv_b = { .if_freq = 6000, .std_bits = 0x0d },
1209 .atv_dk = { .if_freq = 6900, .std_bits = 0x0e },
1210 .atv_gh = { .if_freq = 7100, .std_bits = 0x0e },
1211 .atv_i = { .if_freq = 7250, .std_bits = 0x0e },
1212 .atv_l = { .if_freq = 6900, .std_bits = 0x0e },
1213 .atv_lc = { .if_freq = 1250, .std_bits = 0x0e },
1214 .atv_mn = { .if_freq = 5400, .std_bits = 0x0c },
1215 .atsc_6 = { .if_freq = 3250, .std_bits = 0x1c },
1216 .dvbt_6 = { .if_freq = 3300, .std_bits = 0x1c },
1217 .dvbt_7 = { .if_freq = 3500, .std_bits = 0x1c },
1218 .dvbt_8 = { .if_freq = 4000, .std_bits = 0x1d },
1219 .qam_6 = { .if_freq = 4000, .std_bits = 0x1d },
1220 .qam_8 = { .if_freq = 5000, .std_bits = 0x1f },
1221};
1222
1223/*---------------------------------------------------------------------*/
1224
1225static struct tda18271_map_layout tda18271c1_map_layout = {
1226 .main_pll = tda18271c1_main_pll,
1227 .cal_pll = tda18271c1_cal_pll,
1228
1229 .rf_cal = tda18271c1_rf_cal,
1230 .rf_cal_kmco = tda18271c1_km,
1231
1232 .bp_filter = tda18271_bp_filter,
1233 .rf_band = tda18271_rf_band,
1234 .gain_taper = tda18271_gain_taper,
1235 .ir_measure = tda18271_ir_measure,
1236};
1237
1238static struct tda18271_map_layout tda18271c2_map_layout = {
1239 .main_pll = tda18271c2_main_pll,
1240 .cal_pll = tda18271c2_cal_pll,
1241
1242 .rf_cal = tda18271c2_rf_cal,
1243 .rf_cal_kmco = tda18271c2_km,
1244
1245 .rf_cal_dc_over_dt = tda18271_rf_cal_dc_over_dt,
1246
1247 .bp_filter = tda18271_bp_filter,
1248 .rf_band = tda18271_rf_band,
1249 .gain_taper = tda18271_gain_taper,
1250 .ir_measure = tda18271_ir_measure,
1251};
1252
1253int tda18271_assign_map_layout(struct dvb_frontend *fe)
1254{
1255 struct tda18271_priv *priv = fe->tuner_priv;
1256 int ret = 0;
1257
1258 switch (priv->id) {
1259 case TDA18271HDC1:
1260 priv->maps = &tda18271c1_map_layout;
1261 memcpy(&priv->std, &tda18271c1_std_map,
1262 sizeof(struct tda18271_std_map));
1263 break;
1264 case TDA18271HDC2:
1265 priv->maps = &tda18271c2_map_layout;
1266 memcpy(&priv->std, &tda18271c2_std_map,
1267 sizeof(struct tda18271_std_map));
1268 break;
1269 default:
1270 ret = -EINVAL;
1271 break;
1272 }
1273 memcpy(priv->rf_cal_state, &tda18271_rf_band_template,
1274 sizeof(tda18271_rf_band_template));
1275
1276 return ret;
1277}
1278
1279/*
1280 * Overrides for Emacs so that we follow Linus's tabbing style.
1281 * ---------------------------------------------------------------------------
1282 * Local variables:
1283 * c-basic-offset: 8
1284 * End:
1285 */
diff --git a/drivers/media/dvb/frontends/tda18271.h b/drivers/media/dvb/frontends/tda18271.h
new file mode 100644
index 000000000000..24b0e35a2ab3
--- /dev/null
+++ b/drivers/media/dvb/frontends/tda18271.h
@@ -0,0 +1,78 @@
1/*
2 tda18271.h - header for the Philips / NXP TDA18271 silicon tuner
3
4 Copyright (C) 2007, 2008 Michael Krufky <mkrufky@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 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 __TDA18271_H__
22#define __TDA18271_H__
23
24#include <linux/i2c.h>
25#include "dvb_frontend.h"
26
27struct tda18271_std_map_item {
28 u16 if_freq;
29 u8 std_bits;
30};
31
32struct tda18271_std_map {
33 struct tda18271_std_map_item fm_radio;
34 struct tda18271_std_map_item atv_b;
35 struct tda18271_std_map_item atv_dk;
36 struct tda18271_std_map_item atv_gh;
37 struct tda18271_std_map_item atv_i;
38 struct tda18271_std_map_item atv_l;
39 struct tda18271_std_map_item atv_lc;
40 struct tda18271_std_map_item atv_mn;
41 struct tda18271_std_map_item atsc_6;
42 struct tda18271_std_map_item dvbt_6;
43 struct tda18271_std_map_item dvbt_7;
44 struct tda18271_std_map_item dvbt_8;
45 struct tda18271_std_map_item qam_6;
46 struct tda18271_std_map_item qam_8;
47};
48
49enum tda18271_i2c_gate {
50 TDA18271_GATE_AUTO = 0,
51 TDA18271_GATE_ANALOG,
52 TDA18271_GATE_DIGITAL,
53};
54
55struct tda18271_config {
56 /* override default if freq / std settings (optional) */
57 struct tda18271_std_map *std_map;
58
59 /* use i2c gate provided by analog or digital demod */
60 enum tda18271_i2c_gate gate;
61};
62
63#if defined(CONFIG_DVB_TDA18271) || (defined(CONFIG_DVB_TDA18271_MODULE) && defined(MODULE))
64extern struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
65 struct i2c_adapter *i2c,
66 struct tda18271_config *cfg);
67#else
68static inline struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe,
69 u8 addr,
70 struct i2c_adapter *i2c,
71 struct tda18271_config *cfg)
72{
73 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __FUNCTION__);
74 return NULL;
75}
76#endif
77
78#endif /* __TDA18271_H__ */
diff --git a/drivers/media/dvb/frontends/tda827x.c b/drivers/media/dvb/frontends/tda827x.c
index 256fc4bf500b..229b11987a58 100644
--- a/drivers/media/dvb/frontends/tda827x.c
+++ b/drivers/media/dvb/frontends/tda827x.c
@@ -19,12 +19,16 @@
19 */ 19 */
20 20
21#include <linux/module.h> 21#include <linux/module.h>
22#include <linux/dvb/frontend.h>
23#include <asm/types.h> 22#include <asm/types.h>
23#include <linux/dvb/frontend.h>
24#include <linux/videodev2.h>
24 25
25#include "tda827x.h" 26#include "tda827x.h"
26 27
27static int debug = 0; 28static int debug = 0;
29module_param(debug, int, 0644);
30MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
31
28#define dprintk(args...) \ 32#define dprintk(args...) \
29 do { \ 33 do { \
30 if (debug) printk(KERN_DEBUG "tda827x: " args); \ 34 if (debug) printk(KERN_DEBUG "tda827x: " args); \
@@ -34,10 +38,57 @@ struct tda827x_priv {
34 int i2c_addr; 38 int i2c_addr;
35 struct i2c_adapter *i2c_adap; 39 struct i2c_adapter *i2c_adap;
36 struct tda827x_config *cfg; 40 struct tda827x_config *cfg;
41
42 unsigned int sgIF;
43 unsigned char lpsel;
44
37 u32 frequency; 45 u32 frequency;
38 u32 bandwidth; 46 u32 bandwidth;
39}; 47};
40 48
49static void tda827x_set_std(struct dvb_frontend *fe,
50 struct analog_parameters *params)
51{
52 struct tda827x_priv *priv = fe->tuner_priv;
53 char *mode;
54
55 priv->lpsel = 0;
56 if (params->std & V4L2_STD_MN) {
57 priv->sgIF = 92;
58 priv->lpsel = 1;
59 mode = "MN";
60 } else if (params->std & V4L2_STD_B) {
61 priv->sgIF = 108;
62 mode = "B";
63 } else if (params->std & V4L2_STD_GH) {
64 priv->sgIF = 124;
65 mode = "GH";
66 } else if (params->std & V4L2_STD_PAL_I) {
67 priv->sgIF = 124;
68 mode = "I";
69 } else if (params->std & V4L2_STD_DK) {
70 priv->sgIF = 124;
71 mode = "DK";
72 } else if (params->std & V4L2_STD_SECAM_L) {
73 priv->sgIF = 124;
74 mode = "L";
75 } else if (params->std & V4L2_STD_SECAM_LC) {
76 priv->sgIF = 20;
77 mode = "LC";
78 } else {
79 priv->sgIF = 124;
80 mode = "xx";
81 }
82
83 if (params->mode == V4L2_TUNER_RADIO)
84 priv->sgIF = 88; /* if frequency is 5.5 MHz */
85
86 dprintk("setting tda827x to system %s\n", mode);
87}
88
89
90/* ------------------------------------------------------------------ */
91
41struct tda827x_data { 92struct tda827x_data {
42 u32 lomax; 93 u32 lomax;
43 u8 spd; 94 u8 spd;
@@ -48,7 +99,7 @@ struct tda827x_data {
48 u8 div1p5; 99 u8 div1p5;
49}; 100};
50 101
51static const struct tda827x_data tda827x_dvbt[] = { 102static const struct tda827x_data tda827x_table[] = {
52 { .lomax = 62000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1}, 103 { .lomax = 62000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
53 { .lomax = 66000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1}, 104 { .lomax = 66000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
54 { .lomax = 76000000, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0}, 105 { .lomax = 76000000, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
@@ -106,21 +157,22 @@ static int tda827xo_set_params(struct dvb_frontend *fe,
106 tuner_freq = params->frequency + if_freq; 157 tuner_freq = params->frequency + if_freq;
107 158
108 i = 0; 159 i = 0;
109 while (tda827x_dvbt[i].lomax < tuner_freq) { 160 while (tda827x_table[i].lomax < tuner_freq) {
110 if(tda827x_dvbt[i + 1].lomax == 0) 161 if (tda827x_table[i + 1].lomax == 0)
111 break; 162 break;
112 i++; 163 i++;
113 } 164 }
114 165
115 N = ((tuner_freq + 125000) / 250000) << (tda827x_dvbt[i].spd + 2); 166 N = ((tuner_freq + 125000) / 250000) << (tda827x_table[i].spd + 2);
116 buf[0] = 0; 167 buf[0] = 0;
117 buf[1] = (N>>8) | 0x40; 168 buf[1] = (N>>8) | 0x40;
118 buf[2] = N & 0xff; 169 buf[2] = N & 0xff;
119 buf[3] = 0; 170 buf[3] = 0;
120 buf[4] = 0x52; 171 buf[4] = 0x52;
121 buf[5] = (tda827x_dvbt[i].spd << 6) + (tda827x_dvbt[i].div1p5 << 5) + 172 buf[5] = (tda827x_table[i].spd << 6) + (tda827x_table[i].div1p5 << 5) +
122 (tda827x_dvbt[i].bs << 3) + tda827x_dvbt[i].bp; 173 (tda827x_table[i].bs << 3) +
123 buf[6] = (tda827x_dvbt[i].gc3 << 4) + 0x8f; 174 tda827x_table[i].bp;
175 buf[6] = (tda827x_table[i].gc3 << 4) + 0x8f;
124 buf[7] = 0xbf; 176 buf[7] = 0xbf;
125 buf[8] = 0x2a; 177 buf[8] = 0x2a;
126 buf[9] = 0x05; 178 buf[9] = 0x05;
@@ -140,7 +192,7 @@ static int tda827xo_set_params(struct dvb_frontend *fe,
140 msleep(500); 192 msleep(500);
141 /* correct CP value */ 193 /* correct CP value */
142 buf[0] = 0x30; 194 buf[0] = 0x30;
143 buf[1] = 0x50 + tda827x_dvbt[i].cp; 195 buf[1] = 0x50 + tda827x_table[i].cp;
144 msg.len = 2; 196 msg.len = 2;
145 197
146 if (fe->ops.i2c_gate_ctrl) 198 if (fe->ops.i2c_gate_ctrl)
@@ -173,6 +225,102 @@ static int tda827xo_sleep(struct dvb_frontend *fe)
173 225
174/* ------------------------------------------------------------------ */ 226/* ------------------------------------------------------------------ */
175 227
228static int tda827xo_set_analog_params(struct dvb_frontend *fe,
229 struct analog_parameters *params)
230{
231 unsigned char tuner_reg[8];
232 unsigned char reg2[2];
233 u32 N;
234 int i;
235 struct tda827x_priv *priv = fe->tuner_priv;
236 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0 };
237 unsigned int freq = params->frequency;
238
239 tda827x_set_std(fe, params);
240
241 if (params->mode == V4L2_TUNER_RADIO)
242 freq = freq / 1000;
243
244 N = freq + priv->sgIF;
245
246 i = 0;
247 while (tda827x_table[i].lomax < N * 62500) {
248 if (tda827x_table[i + 1].lomax == 0)
249 break;
250 i++;
251 }
252
253 N = N << tda827x_table[i].spd;
254
255 tuner_reg[0] = 0;
256 tuner_reg[1] = (unsigned char)(N>>8);
257 tuner_reg[2] = (unsigned char) N;
258 tuner_reg[3] = 0x40;
259 tuner_reg[4] = 0x52 + (priv->lpsel << 5);
260 tuner_reg[5] = (tda827x_table[i].spd << 6) +
261 (tda827x_table[i].div1p5 << 5) +
262 (tda827x_table[i].bs << 3) + tda827x_table[i].bp;
263 tuner_reg[6] = 0x8f + (tda827x_table[i].gc3 << 4);
264 tuner_reg[7] = 0x8f;
265
266 msg.buf = tuner_reg;
267 msg.len = 8;
268 i2c_transfer(priv->i2c_adap, &msg, 1);
269
270 msg.buf = reg2;
271 msg.len = 2;
272 reg2[0] = 0x80;
273 reg2[1] = 0;
274 i2c_transfer(priv->i2c_adap, &msg, 1);
275
276 reg2[0] = 0x60;
277 reg2[1] = 0xbf;
278 i2c_transfer(priv->i2c_adap, &msg, 1);
279
280 reg2[0] = 0x30;
281 reg2[1] = tuner_reg[4] + 0x80;
282 i2c_transfer(priv->i2c_adap, &msg, 1);
283
284 msleep(1);
285 reg2[0] = 0x30;
286 reg2[1] = tuner_reg[4] + 4;
287 i2c_transfer(priv->i2c_adap, &msg, 1);
288
289 msleep(1);
290 reg2[0] = 0x30;
291 reg2[1] = tuner_reg[4];
292 i2c_transfer(priv->i2c_adap, &msg, 1);
293
294 msleep(550);
295 reg2[0] = 0x30;
296 reg2[1] = (tuner_reg[4] & 0xfc) + tda827x_table[i].cp;
297 i2c_transfer(priv->i2c_adap, &msg, 1);
298
299 reg2[0] = 0x60;
300 reg2[1] = 0x3f;
301 i2c_transfer(priv->i2c_adap, &msg, 1);
302
303 reg2[0] = 0x80;
304 reg2[1] = 0x08; /* Vsync en */
305 i2c_transfer(priv->i2c_adap, &msg, 1);
306
307 priv->frequency = freq * 62500;
308
309 return 0;
310}
311
312static void tda827xo_agcf(struct dvb_frontend *fe)
313{
314 struct tda827x_priv *priv = fe->tuner_priv;
315 unsigned char data[] = { 0x80, 0x0c };
316 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
317 .buf = data, .len = 2};
318
319 i2c_transfer(priv->i2c_adap, &msg, 1);
320}
321
322/* ------------------------------------------------------------------ */
323
176struct tda827xa_data { 324struct tda827xa_data {
177 u32 lomax; 325 u32 lomax;
178 u8 svco; 326 u8 svco;
@@ -212,6 +360,35 @@ static const struct tda827xa_data tda827xa_dvbt[] = {
212 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0} 360 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
213}; 361};
214 362
363static struct tda827xa_data tda827xa_analog[] = {
364 { .lomax = 56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 3},
365 { .lomax = 67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
366 { .lomax = 81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
367 { .lomax = 97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
368 { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
369 { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
370 { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
371 { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
372 { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
373 { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
374 { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 3},
375 { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 3},
376 { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
377 { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3},
378 { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3},
379 { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
380 { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
381 { .lomax = 554000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
382 { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
383 { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
384 { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
385 { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
386 { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
387 { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
388 { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
389 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
390};
391
215static int tda827xa_set_params(struct dvb_frontend *fe, 392static int tda827xa_set_params(struct dvb_frontend *fe,
216 struct dvb_frontend_parameters *params) 393 struct dvb_frontend_parameters *params)
217{ 394{
@@ -368,6 +545,163 @@ static int tda827xa_sleep(struct dvb_frontend *fe)
368 return 0; 545 return 0;
369} 546}
370 547
548/* ------------------------------------------------------------------ */
549
550static void tda827xa_lna_gain(struct dvb_frontend *fe, int high,
551 struct analog_parameters *params)
552{
553 struct tda827x_priv *priv = fe->tuner_priv;
554 unsigned char buf[] = {0x22, 0x01};
555 int arg;
556 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
557 .buf = buf, .len = sizeof(buf) };
558
559 if (NULL == priv->cfg) {
560 dprintk("tda827x_config not defined, cannot set LNA gain!\n");
561 return;
562 }
563
564 if (priv->cfg->config) {
565 if (high)
566 dprintk("setting LNA to high gain\n");
567 else
568 dprintk("setting LNA to low gain\n");
569 }
570 switch (*priv->cfg->config) {
571 case 0: /* no LNA */
572 break;
573 case 1: /* switch is GPIO 0 of tda8290 */
574 case 2:
575 /* turn Vsync on */
576 if (params->std & V4L2_STD_MN)
577 arg = 1;
578 else
579 arg = 0;
580 if (priv->cfg->tuner_callback)
581 priv->cfg->tuner_callback(priv->i2c_adap->algo_data,
582 1, arg);
583 buf[1] = high ? 0 : 1;
584 if (*priv->cfg->config == 2)
585 buf[1] = high ? 1 : 0;
586 i2c_transfer(priv->i2c_adap, &msg, 1);
587 break;
588 case 3: /* switch with GPIO of saa713x */
589 if (priv->cfg->tuner_callback)
590 priv->cfg->tuner_callback(priv->i2c_adap->algo_data,
591 0, high);
592 break;
593 }
594}
595
596static int tda827xa_set_analog_params(struct dvb_frontend *fe,
597 struct analog_parameters *params)
598{
599 unsigned char tuner_reg[11];
600 u32 N;
601 int i;
602 struct tda827x_priv *priv = fe->tuner_priv;
603 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
604 .buf = tuner_reg, .len = sizeof(tuner_reg) };
605 unsigned int freq = params->frequency;
606
607 tda827x_set_std(fe, params);
608
609 tda827xa_lna_gain(fe, 1, params);
610 msleep(10);
611
612 if (params->mode == V4L2_TUNER_RADIO)
613 freq = freq / 1000;
614
615 N = freq + priv->sgIF;
616
617 i = 0;
618 while (tda827xa_analog[i].lomax < N * 62500) {
619 if (tda827xa_analog[i + 1].lomax == 0)
620 break;
621 i++;
622 }
623
624 N = N << tda827xa_analog[i].spd;
625
626 tuner_reg[0] = 0;
627 tuner_reg[1] = (unsigned char)(N>>8);
628 tuner_reg[2] = (unsigned char) N;
629 tuner_reg[3] = 0;
630 tuner_reg[4] = 0x16;
631 tuner_reg[5] = (tda827xa_analog[i].spd << 5) +
632 (tda827xa_analog[i].svco << 3) +
633 tda827xa_analog[i].sbs;
634 tuner_reg[6] = 0x8b + (tda827xa_analog[i].gc3 << 4);
635 tuner_reg[7] = 0x1c;
636 tuner_reg[8] = 4;
637 tuner_reg[9] = 0x20;
638 tuner_reg[10] = 0x00;
639 msg.len = 11;
640 i2c_transfer(priv->i2c_adap, &msg, 1);
641
642 tuner_reg[0] = 0x90;
643 tuner_reg[1] = 0xff;
644 tuner_reg[2] = 0xe0;
645 tuner_reg[3] = 0;
646 tuner_reg[4] = 0x99 + (priv->lpsel << 1);
647 msg.len = 5;
648 i2c_transfer(priv->i2c_adap, &msg, 1);
649
650 tuner_reg[0] = 0xa0;
651 tuner_reg[1] = 0xc0;
652 msg.len = 2;
653 i2c_transfer(priv->i2c_adap, &msg, 1);
654
655 tuner_reg[0] = 0x30;
656 tuner_reg[1] = 0x10 + tda827xa_analog[i].scr;
657 i2c_transfer(priv->i2c_adap, &msg, 1);
658
659 msg.flags = I2C_M_RD;
660 i2c_transfer(priv->i2c_adap, &msg, 1);
661 msg.flags = 0;
662 tuner_reg[1] >>= 4;
663 dprintk("AGC2 gain is: %d\n", tuner_reg[1]);
664 if (tuner_reg[1] < 1)
665 tda827xa_lna_gain(fe, 0, params);
666
667 msleep(100);
668 tuner_reg[0] = 0x60;
669 tuner_reg[1] = 0x3c;
670 i2c_transfer(priv->i2c_adap, &msg, 1);
671
672 msleep(163);
673 tuner_reg[0] = 0x50;
674 tuner_reg[1] = 0x8f + (tda827xa_analog[i].gc3 << 4);
675 i2c_transfer(priv->i2c_adap, &msg, 1);
676
677 tuner_reg[0] = 0x80;
678 tuner_reg[1] = 0x28;
679 i2c_transfer(priv->i2c_adap, &msg, 1);
680
681 tuner_reg[0] = 0xb0;
682 tuner_reg[1] = 0x01;
683 i2c_transfer(priv->i2c_adap, &msg, 1);
684
685 tuner_reg[0] = 0xc0;
686 tuner_reg[1] = 0x19 + (priv->lpsel << 1);
687 i2c_transfer(priv->i2c_adap, &msg, 1);
688
689 priv->frequency = freq * 62500;
690
691 return 0;
692}
693
694static void tda827xa_agcf(struct dvb_frontend *fe)
695{
696 struct tda827x_priv *priv = fe->tuner_priv;
697 unsigned char data[] = {0x80, 0x2c};
698 struct i2c_msg msg = {.addr = priv->i2c_addr, .flags = 0,
699 .buf = data, .len = 2};
700 i2c_transfer(priv->i2c_adap, &msg, 1);
701}
702
703/* ------------------------------------------------------------------ */
704
371static int tda827x_release(struct dvb_frontend *fe) 705static int tda827x_release(struct dvb_frontend *fe)
372{ 706{
373 kfree(fe->tuner_priv); 707 kfree(fe->tuner_priv);
@@ -430,6 +764,7 @@ static struct dvb_tuner_ops tda827xo_tuner_ops = {
430 .init = tda827x_initial_init, 764 .init = tda827x_initial_init,
431 .sleep = tda827x_initial_sleep, 765 .sleep = tda827x_initial_sleep,
432 .set_params = tda827xo_set_params, 766 .set_params = tda827xo_set_params,
767 .set_analog_params = tda827xo_set_analog_params,
433 .get_frequency = tda827x_get_frequency, 768 .get_frequency = tda827x_get_frequency,
434 .get_bandwidth = tda827x_get_bandwidth, 769 .get_bandwidth = tda827x_get_bandwidth,
435}; 770};
@@ -445,6 +780,7 @@ static struct dvb_tuner_ops tda827xa_tuner_ops = {
445 .init = tda827x_init, 780 .init = tda827x_init,
446 .sleep = tda827xa_sleep, 781 .sleep = tda827xa_sleep,
447 .set_params = tda827xa_set_params, 782 .set_params = tda827xa_set_params,
783 .set_analog_params = tda827xa_set_analog_params,
448 .get_frequency = tda827x_get_frequency, 784 .get_frequency = tda827x_get_frequency,
449 .get_bandwidth = tda827x_get_bandwidth, 785 .get_bandwidth = tda827x_get_bandwidth,
450}; 786};
@@ -465,9 +801,13 @@ static int tda827x_probe_version(struct dvb_frontend *fe)
465 dprintk("tda827x tuner found\n"); 801 dprintk("tda827x tuner found\n");
466 fe->ops.tuner_ops.init = tda827x_init; 802 fe->ops.tuner_ops.init = tda827x_init;
467 fe->ops.tuner_ops.sleep = tda827xo_sleep; 803 fe->ops.tuner_ops.sleep = tda827xo_sleep;
804 if (priv->cfg)
805 priv->cfg->agcf = tda827xo_agcf;
468 } else { 806 } else {
469 dprintk("tda827xa tuner found\n"); 807 dprintk("tda827xa tuner found\n");
470 memcpy(&fe->ops.tuner_ops, &tda827xa_tuner_ops, sizeof(struct dvb_tuner_ops)); 808 memcpy(&fe->ops.tuner_ops, &tda827xa_tuner_ops, sizeof(struct dvb_tuner_ops));
809 if (priv->cfg)
810 priv->cfg->agcf = tda827xa_agcf;
471 } 811 }
472 return 0; 812 return 0;
473} 813}
@@ -487,16 +827,13 @@ struct dvb_frontend *tda827x_attach(struct dvb_frontend *fe, int addr,
487 priv->i2c_adap = i2c; 827 priv->i2c_adap = i2c;
488 priv->cfg = cfg; 828 priv->cfg = cfg;
489 memcpy(&fe->ops.tuner_ops, &tda827xo_tuner_ops, sizeof(struct dvb_tuner_ops)); 829 memcpy(&fe->ops.tuner_ops, &tda827xo_tuner_ops, sizeof(struct dvb_tuner_ops));
490
491 fe->tuner_priv = priv; 830 fe->tuner_priv = priv;
492 831
832 dprintk("type set to %s\n", fe->ops.tuner_ops.info.name);
833
493 return fe; 834 return fe;
494} 835}
495 836EXPORT_SYMBOL_GPL(tda827x_attach);
496EXPORT_SYMBOL(tda827x_attach);
497
498module_param(debug, int, 0644);
499MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
500 837
501MODULE_DESCRIPTION("DVB TDA827x driver"); 838MODULE_DESCRIPTION("DVB TDA827x driver");
502MODULE_AUTHOR("Hartmut Hackmann <hartmut.hackmann@t-online.de>"); 839MODULE_AUTHOR("Hartmut Hackmann <hartmut.hackmann@t-online.de>");
diff --git a/drivers/media/dvb/frontends/tda827x.h b/drivers/media/dvb/frontends/tda827x.h
index 69e8263d6d59..92eb65b4012b 100644
--- a/drivers/media/dvb/frontends/tda827x.h
+++ b/drivers/media/dvb/frontends/tda827x.h
@@ -29,9 +29,16 @@
29 29
30struct tda827x_config 30struct tda827x_config
31{ 31{
32 /* saa7134 - provided callbacks */
32 void (*lna_gain) (struct dvb_frontend *fe, int high); 33 void (*lna_gain) (struct dvb_frontend *fe, int high);
33 int (*init) (struct dvb_frontend *fe); 34 int (*init) (struct dvb_frontend *fe);
34 int (*sleep) (struct dvb_frontend *fe); 35 int (*sleep) (struct dvb_frontend *fe);
36
37 /* interface to tda829x driver */
38 unsigned int *config;
39 int (*tuner_callback) (void *dev, int command, int arg);
40
41 void (*agcf)(struct dvb_frontend *fe);
35}; 42};
36 43
37 44
diff --git a/drivers/media/dvb/frontends/ves1820.c b/drivers/media/dvb/frontends/ves1820.c
index 60433b5011fd..8791701c8f25 100644
--- a/drivers/media/dvb/frontends/ves1820.c
+++ b/drivers/media/dvb/frontends/ves1820.c
@@ -65,7 +65,7 @@ static int ves1820_writereg(struct ves1820_state *state, u8 reg, u8 data)
65 ret = i2c_transfer(state->i2c, &msg, 1); 65 ret = i2c_transfer(state->i2c, &msg, 1);
66 66
67 if (ret != 1) 67 if (ret != 1)
68 printk("ves1820: %s(): writereg error (reg == 0x%02x," 68 printk("ves1820: %s(): writereg error (reg == 0x%02x, "
69 "val == 0x%02x, ret == %i)\n", __FUNCTION__, reg, data, ret); 69 "val == 0x%02x, ret == %i)\n", __FUNCTION__, reg, data, ret);
70 70
71 return (ret != 1) ? -EREMOTEIO : 0; 71 return (ret != 1) ? -EREMOTEIO : 0;
@@ -84,7 +84,7 @@ static u8 ves1820_readreg(struct ves1820_state *state, u8 reg)
84 ret = i2c_transfer(state->i2c, msg, 2); 84 ret = i2c_transfer(state->i2c, msg, 2);
85 85
86 if (ret != 2) 86 if (ret != 2)
87 printk("ves1820: %s(): readreg error (reg == 0x%02x," 87 printk("ves1820: %s(): readreg error (reg == 0x%02x, "
88 "ret == %i)\n", __FUNCTION__, reg, ret); 88 "ret == %i)\n", __FUNCTION__, reg, ret);
89 89
90 return b1[0]; 90 return b1[0];
diff --git a/drivers/media/dvb/frontends/xc5000.c b/drivers/media/dvb/frontends/xc5000.c
new file mode 100644
index 000000000000..f642ca200b59
--- /dev/null
+++ b/drivers/media/dvb/frontends/xc5000.c
@@ -0,0 +1,964 @@
1/*
2 * Driver for Xceive XC5000 "QAM/8VSB single chip tuner"
3 *
4 * Copyright (c) 2007 Xceive Corporation
5 * Copyright (c) 2007 Steven Toth <stoth@hauppauge.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/module.h>
24#include <linux/moduleparam.h>
25#include <linux/videodev2.h>
26#include <linux/delay.h>
27#include <linux/dvb/frontend.h>
28#include <linux/i2c.h>
29
30#include "dvb_frontend.h"
31
32#include "xc5000.h"
33#include "xc5000_priv.h"
34
35static int debug;
36module_param(debug, int, 0644);
37MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
38
39#define dprintk(level,fmt, arg...) if (debug >= level) \
40 printk(KERN_INFO "%s: " fmt, "xc5000", ## arg)
41
42#define XC5000_DEFAULT_FIRMWARE "dvb-fe-xc5000-1.1.fw"
43#define XC5000_DEFAULT_FIRMWARE_SIZE 12332
44
45/* Misc Defines */
46#define MAX_TV_STANDARD 23
47#define XC_MAX_I2C_WRITE_LENGTH 64
48
49/* Signal Types */
50#define XC_RF_MODE_AIR 0
51#define XC_RF_MODE_CABLE 1
52
53/* Result codes */
54#define XC_RESULT_SUCCESS 0
55#define XC_RESULT_RESET_FAILURE 1
56#define XC_RESULT_I2C_WRITE_FAILURE 2
57#define XC_RESULT_I2C_READ_FAILURE 3
58#define XC_RESULT_OUT_OF_RANGE 5
59
60/* Product id */
61#define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
62#define XC_PRODUCT_ID_FW_LOADED 0x1388
63
64/* Registers */
65#define XREG_INIT 0x00
66#define XREG_VIDEO_MODE 0x01
67#define XREG_AUDIO_MODE 0x02
68#define XREG_RF_FREQ 0x03
69#define XREG_D_CODE 0x04
70#define XREG_IF_OUT 0x05
71#define XREG_SEEK_MODE 0x07
72#define XREG_POWER_DOWN 0x0A
73#define XREG_SIGNALSOURCE 0x0D /* 0=Air, 1=Cable */
74#define XREG_SMOOTHEDCVBS 0x0E
75#define XREG_XTALFREQ 0x0F
76#define XREG_FINERFFREQ 0x10
77#define XREG_DDIMODE 0x11
78
79#define XREG_ADC_ENV 0x00
80#define XREG_QUALITY 0x01
81#define XREG_FRAME_LINES 0x02
82#define XREG_HSYNC_FREQ 0x03
83#define XREG_LOCK 0x04
84#define XREG_FREQ_ERROR 0x05
85#define XREG_SNR 0x06
86#define XREG_VERSION 0x07
87#define XREG_PRODUCT_ID 0x08
88#define XREG_BUSY 0x09
89
90/*
91 Basic firmware description. This will remain with
92 the driver for documentation purposes.
93
94 This represents an I2C firmware file encoded as a
95 string of unsigned char. Format is as follows:
96
97 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
98 char[1 ]=len0_LSB -> length of first write transaction
99 char[2 ]=data0 -> first byte to be sent
100 char[3 ]=data1
101 char[4 ]=data2
102 char[ ]=...
103 char[M ]=dataN -> last byte to be sent
104 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
105 char[M+2]=len1_LSB -> length of second write transaction
106 char[M+3]=data0
107 char[M+4]=data1
108 ...
109 etc.
110
111 The [len] value should be interpreted as follows:
112
113 len= len_MSB _ len_LSB
114 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
115 len=0000_0000_0000_0000 : Reset command: Do hardware reset
116 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
117 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
118
119 For the RESET and WAIT commands, the two following bytes will contain
120 immediately the length of the following transaction.
121
122*/
123typedef struct {
124 char *Name;
125 u16 AudioMode;
126 u16 VideoMode;
127} XC_TV_STANDARD;
128
129/* Tuner standards */
130#define MN_NTSC_PAL_BTSC 0
131#define MN_NTSC_PAL_A2 1
132#define MN_NTSC_PAL_EIAJ 2
133#define MN_NTSC_PAL_Mono 3
134#define BG_PAL_A2 4
135#define BG_PAL_NICAM 5
136#define BG_PAL_MONO 6
137#define I_PAL_NICAM 7
138#define I_PAL_NICAM_MONO 8
139#define DK_PAL_A2 9
140#define DK_PAL_NICAM 10
141#define DK_PAL_MONO 11
142#define DK_SECAM_A2DK1 12
143#define DK_SECAM_A2LDK3 13
144#define DK_SECAM_A2MONO 14
145#define L_SECAM_NICAM 15
146#define LC_SECAM_NICAM 16
147#define DTV6 17
148#define DTV8 18
149#define DTV7_8 19
150#define DTV7 20
151#define FM_Radio_INPUT2 21
152#define FM_Radio_INPUT1 22
153
154XC_TV_STANDARD XC5000_Standard[MAX_TV_STANDARD] = {
155 {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
156 {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
157 {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
158 {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
159 {"B/G-PAL-A2", 0x0A00, 0x8049},
160 {"B/G-PAL-NICAM", 0x0C04, 0x8049},
161 {"B/G-PAL-MONO", 0x0878, 0x8059},
162 {"I-PAL-NICAM", 0x1080, 0x8009},
163 {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
164 {"D/K-PAL-A2", 0x1600, 0x8009},
165 {"D/K-PAL-NICAM", 0x0E80, 0x8009},
166 {"D/K-PAL-MONO", 0x1478, 0x8009},
167 {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
168 {"D/K-SECAM-A2 L/DK3",0x0E00, 0x8009},
169 {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
170 {"L-SECAM-NICAM", 0x8E82, 0x0009},
171 {"L'-SECAM-NICAM", 0x8E82, 0x4009},
172 {"DTV6", 0x00C0, 0x8002},
173 {"DTV8", 0x00C0, 0x800B},
174 {"DTV7/8", 0x00C0, 0x801B},
175 {"DTV7", 0x00C0, 0x8007},
176 {"FM Radio-INPUT2", 0x9802, 0x9002},
177 {"FM Radio-INPUT1", 0x0208, 0x9002}
178};
179
180static int xc5000_writeregs(struct xc5000_priv *priv, u8 *buf, u8 len);
181static int xc5000_readregs(struct xc5000_priv *priv, u8 *buf, u8 len);
182static void xc5000_TunerReset(struct dvb_frontend *fe);
183
184static int xc_send_i2c_data(struct xc5000_priv *priv, u8 *buf, int len)
185{
186 return xc5000_writeregs(priv, buf, len)
187 ? XC_RESULT_I2C_WRITE_FAILURE : XC_RESULT_SUCCESS;
188}
189
190static int xc_read_i2c_data(struct xc5000_priv *priv, u8 *buf, int len)
191{
192 return xc5000_readregs(priv, buf, len)
193 ? XC_RESULT_I2C_READ_FAILURE : XC_RESULT_SUCCESS;
194}
195
196static int xc_reset(struct dvb_frontend *fe)
197{
198 xc5000_TunerReset(fe);
199 return XC_RESULT_SUCCESS;
200}
201
202static void xc_wait(int wait_ms)
203{
204 msleep(wait_ms);
205}
206
207static void xc5000_TunerReset(struct dvb_frontend *fe)
208{
209 struct xc5000_priv *priv = fe->tuner_priv;
210 int ret;
211
212 dprintk(1, "%s()\n", __FUNCTION__);
213
214 if (priv->cfg->tuner_callback) {
215 ret = priv->cfg->tuner_callback(priv->cfg->priv,
216 XC5000_TUNER_RESET, 0);
217 if (ret)
218 printk(KERN_ERR "xc5000: reset failed\n");
219 } else
220 printk(KERN_ERR "xc5000: no tuner reset callback function, fatal\n");
221}
222
223static int xc_write_reg(struct xc5000_priv *priv, u16 regAddr, u16 i2cData)
224{
225 u8 buf[4];
226 int WatchDogTimer = 5;
227 int result;
228
229 buf[0] = (regAddr >> 8) & 0xFF;
230 buf[1] = regAddr & 0xFF;
231 buf[2] = (i2cData >> 8) & 0xFF;
232 buf[3] = i2cData & 0xFF;
233 result = xc_send_i2c_data(priv, buf, 4);
234 if (result == XC_RESULT_SUCCESS) {
235 /* wait for busy flag to clear */
236 while ((WatchDogTimer > 0) && (result == XC_RESULT_SUCCESS)) {
237 buf[0] = 0;
238 buf[1] = XREG_BUSY;
239
240 result = xc_send_i2c_data(priv, buf, 2);
241 if (result == XC_RESULT_SUCCESS) {
242 result = xc_read_i2c_data(priv, buf, 2);
243 if (result == XC_RESULT_SUCCESS) {
244 if ((buf[0] == 0) && (buf[1] == 0)) {
245 /* busy flag cleared */
246 break;
247 } else {
248 xc_wait(100); /* wait 5 ms */
249 WatchDogTimer--;
250 }
251 }
252 }
253 }
254 }
255 if (WatchDogTimer < 0)
256 result = XC_RESULT_I2C_WRITE_FAILURE;
257
258 return result;
259}
260
261static int xc_read_reg(struct xc5000_priv *priv, u16 regAddr, u16 *i2cData)
262{
263 u8 buf[2];
264 int result;
265
266 buf[0] = (regAddr >> 8) & 0xFF;
267 buf[1] = regAddr & 0xFF;
268 result = xc_send_i2c_data(priv, buf, 2);
269 if (result != XC_RESULT_SUCCESS)
270 return result;
271
272 result = xc_read_i2c_data(priv, buf, 2);
273 if (result != XC_RESULT_SUCCESS)
274 return result;
275
276 *i2cData = buf[0] * 256 + buf[1];
277 return result;
278}
279
280static int xc_load_i2c_sequence(struct dvb_frontend *fe, u8 i2c_sequence[])
281{
282 struct xc5000_priv *priv = fe->tuner_priv;
283
284 int i, nbytes_to_send, result;
285 unsigned int len, pos, index;
286 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
287
288 index=0;
289 while ((i2c_sequence[index]!=0xFF) || (i2c_sequence[index+1]!=0xFF)) {
290 len = i2c_sequence[index]* 256 + i2c_sequence[index+1];
291 if (len == 0x0000) {
292 /* RESET command */
293 result = xc_reset(fe);
294 index += 2;
295 if (result != XC_RESULT_SUCCESS)
296 return result;
297 } else if (len & 0x8000) {
298 /* WAIT command */
299 xc_wait(len & 0x7FFF);
300 index += 2;
301 } else {
302 /* Send i2c data whilst ensuring individual transactions
303 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
304 */
305 index += 2;
306 buf[0] = i2c_sequence[index];
307 buf[1] = i2c_sequence[index + 1];
308 pos = 2;
309 while (pos < len) {
310 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2) {
311 nbytes_to_send = XC_MAX_I2C_WRITE_LENGTH;
312 } else {
313 nbytes_to_send = (len - pos + 2);
314 }
315 for (i=2; i<nbytes_to_send; i++) {
316 buf[i] = i2c_sequence[index + pos + i - 2];
317 }
318 result = xc_send_i2c_data(priv, buf, nbytes_to_send);
319
320 if (result != XC_RESULT_SUCCESS)
321 return result;
322
323 pos += nbytes_to_send - 2;
324 }
325 index += len;
326 }
327 }
328 return XC_RESULT_SUCCESS;
329}
330
331static int xc_initialize(struct xc5000_priv *priv)
332{
333 dprintk(1, "%s()\n", __FUNCTION__);
334 return xc_write_reg(priv, XREG_INIT, 0);
335}
336
337static int xc_SetTVStandard(struct xc5000_priv *priv,
338 u16 VideoMode, u16 AudioMode)
339{
340 int ret;
341 dprintk(1, "%s(0x%04x,0x%04x)\n", __FUNCTION__, VideoMode, AudioMode);
342 dprintk(1, "%s() Standard = %s\n",
343 __FUNCTION__,
344 XC5000_Standard[priv->video_standard].Name);
345
346 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
347 if (ret == XC_RESULT_SUCCESS)
348 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
349
350 return ret;
351}
352
353static int xc_shutdown(struct xc5000_priv *priv)
354{
355 return 0;
356 /* Fixme: cannot bring tuner back alive once shutdown
357 * without reloading the driver modules.
358 * return xc_write_reg(priv, XREG_POWER_DOWN, 0);
359 */
360}
361
362static int xc_SetSignalSource(struct xc5000_priv *priv, u16 rf_mode)
363{
364 dprintk(1, "%s(%d) Source = %s\n", __FUNCTION__, rf_mode,
365 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
366
367 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE))
368 {
369 rf_mode = XC_RF_MODE_CABLE;
370 printk(KERN_ERR
371 "%s(), Invalid mode, defaulting to CABLE",
372 __FUNCTION__);
373 }
374 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
375}
376
377static const struct dvb_tuner_ops xc5000_tuner_ops;
378
379static int xc_set_RF_frequency(struct xc5000_priv *priv, u32 freq_hz)
380{
381 u16 freq_code;
382
383 dprintk(1, "%s(%u)\n", __FUNCTION__, freq_hz);
384
385 if ((freq_hz > xc5000_tuner_ops.info.frequency_max) ||
386 (freq_hz < xc5000_tuner_ops.info.frequency_min))
387 return XC_RESULT_OUT_OF_RANGE;
388
389 freq_code = (u16)(freq_hz / 15625);
390
391 return xc_write_reg(priv, XREG_RF_FREQ, freq_code);
392}
393
394
395static int xc_set_IF_frequency(struct xc5000_priv *priv, u32 freq_khz)
396{
397 u32 freq_code = (freq_khz * 1024)/1000;
398 dprintk(1, "%s(freq_khz = %d) freq_code = 0x%x\n",
399 __FUNCTION__, freq_khz, freq_code);
400
401 return xc_write_reg(priv, XREG_IF_OUT, freq_code);
402}
403
404
405static int xc_get_ADC_Envelope(struct xc5000_priv *priv, u16 *adc_envelope)
406{
407 return xc_read_reg(priv, XREG_ADC_ENV, adc_envelope);
408}
409
410static int xc_get_frequency_error(struct xc5000_priv *priv, u32 *freq_error_hz)
411{
412 int result;
413 u16 regData;
414 u32 tmp;
415
416 result = xc_read_reg(priv, XREG_FREQ_ERROR, &regData);
417 if (result)
418 return result;
419
420 tmp = (u32)regData;
421 (*freq_error_hz) = (tmp * 15625) / 1000;
422 return result;
423}
424
425static int xc_get_lock_status(struct xc5000_priv *priv, u16 *lock_status)
426{
427 return xc_read_reg(priv, XREG_LOCK, lock_status);
428}
429
430static int xc_get_version(struct xc5000_priv *priv,
431 u8 *hw_majorversion, u8 *hw_minorversion,
432 u8 *fw_majorversion, u8 *fw_minorversion)
433{
434 u16 data;
435 int result;
436
437 result = xc_read_reg(priv, XREG_VERSION, &data);
438 if (result)
439 return result;
440
441 (*hw_majorversion) = (data >> 12) & 0x0F;
442 (*hw_minorversion) = (data >> 8) & 0x0F;
443 (*fw_majorversion) = (data >> 4) & 0x0F;
444 (*fw_minorversion) = data & 0x0F;
445
446 return 0;
447}
448
449static int xc_get_hsync_freq(struct xc5000_priv *priv, u32 *hsync_freq_hz)
450{
451 u16 regData;
452 int result;
453
454 result = xc_read_reg(priv, XREG_HSYNC_FREQ, &regData);
455 if (result)
456 return result;
457
458 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
459 return result;
460}
461
462static int xc_get_frame_lines(struct xc5000_priv *priv, u16 *frame_lines)
463{
464 return xc_read_reg(priv, XREG_FRAME_LINES, frame_lines);
465}
466
467static int xc_get_quality(struct xc5000_priv *priv, u16 *quality)
468{
469 return xc_read_reg(priv, XREG_QUALITY, quality);
470}
471
472static u16 WaitForLock(struct xc5000_priv *priv)
473{
474 u16 lockState = 0;
475 int watchDogCount = 40;
476
477 while ((lockState == 0) && (watchDogCount > 0)) {
478 xc_get_lock_status(priv, &lockState);
479 if (lockState != 1) {
480 xc_wait(5);
481 watchDogCount--;
482 }
483 }
484 return lockState;
485}
486
487static int xc_tune_channel(struct xc5000_priv *priv, u32 freq_hz)
488{
489 int found = 0;
490
491 dprintk(1, "%s(%u)\n", __FUNCTION__, freq_hz);
492
493 if (xc_set_RF_frequency(priv, freq_hz) != XC_RESULT_SUCCESS)
494 return 0;
495
496 if (WaitForLock(priv) == 1)
497 found = 1;
498
499 return found;
500}
501
502static int xc5000_readreg(struct xc5000_priv *priv, u16 reg, u16 *val)
503{
504 u8 buf[2] = { reg >> 8, reg & 0xff };
505 u8 bval[2] = { 0, 0 };
506 struct i2c_msg msg[2] = {
507 { .addr = priv->cfg->i2c_address,
508 .flags = 0, .buf = &buf[0], .len = 2 },
509 { .addr = priv->cfg->i2c_address,
510 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
511 };
512
513 if (i2c_transfer(priv->i2c, msg, 2) != 2) {
514 printk(KERN_WARNING "xc5000: I2C read failed\n");
515 return -EREMOTEIO;
516 }
517
518 *val = (bval[0] << 8) | bval[1];
519 return 0;
520}
521
522static int xc5000_writeregs(struct xc5000_priv *priv, u8 *buf, u8 len)
523{
524 struct i2c_msg msg = { .addr = priv->cfg->i2c_address,
525 .flags = 0, .buf = buf, .len = len };
526
527 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
528 printk(KERN_ERR "xc5000: I2C write failed (len=%i)\n",
529 (int)len);
530 return -EREMOTEIO;
531 }
532 return 0;
533}
534
535static int xc5000_readregs(struct xc5000_priv *priv, u8 *buf, u8 len)
536{
537 struct i2c_msg msg = { .addr = priv->cfg->i2c_address,
538 .flags = I2C_M_RD, .buf = buf, .len = len };
539
540 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
541 printk(KERN_ERR "xc5000 I2C read failed (len=%i)\n",(int)len);
542 return -EREMOTEIO;
543 }
544 return 0;
545}
546
547static int xc5000_fwupload(struct dvb_frontend* fe)
548{
549 struct xc5000_priv *priv = fe->tuner_priv;
550 const struct firmware *fw;
551 int ret;
552
553 /* request the firmware, this will block and timeout */
554 printk(KERN_INFO "xc5000: waiting for firmware upload (%s)...\n",
555 XC5000_DEFAULT_FIRMWARE);
556
557 ret = request_firmware(&fw, XC5000_DEFAULT_FIRMWARE, &priv->i2c->dev);
558 if (ret) {
559 printk(KERN_ERR "xc5000: Upload failed. (file not found?)\n");
560 ret = XC_RESULT_RESET_FAILURE;
561 goto out;
562 } else {
563 printk(KERN_INFO "xc5000: firmware read %Zu bytes.\n",
564 fw->size);
565 ret = XC_RESULT_SUCCESS;
566 }
567
568 if (fw->size != XC5000_DEFAULT_FIRMWARE_SIZE) {
569 printk(KERN_ERR "xc5000: firmware incorrect size\n");
570 ret = XC_RESULT_RESET_FAILURE;
571 } else {
572 printk(KERN_INFO "xc5000: firmware upload\n");
573 ret = xc_load_i2c_sequence(fe, fw->data );
574 }
575
576out:
577 release_firmware(fw);
578 return ret;
579}
580
581static void xc_debug_dump(struct xc5000_priv *priv)
582{
583 u16 adc_envelope;
584 u32 freq_error_hz = 0;
585 u16 lock_status;
586 u32 hsync_freq_hz = 0;
587 u16 frame_lines;
588 u16 quality;
589 u8 hw_majorversion = 0, hw_minorversion = 0;
590 u8 fw_majorversion = 0, fw_minorversion = 0;
591
592 /* Wait for stats to stabilize.
593 * Frame Lines needs two frame times after initial lock
594 * before it is valid.
595 */
596 xc_wait(100);
597
598 xc_get_ADC_Envelope(priv, &adc_envelope);
599 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
600
601 xc_get_frequency_error(priv, &freq_error_hz);
602 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
603
604 xc_get_lock_status(priv, &lock_status);
605 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
606 lock_status);
607
608 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
609 &fw_majorversion, &fw_minorversion);
610 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
611 hw_majorversion, hw_minorversion,
612 fw_majorversion, fw_minorversion);
613
614 xc_get_hsync_freq(priv, &hsync_freq_hz);
615 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
616
617 xc_get_frame_lines(priv, &frame_lines);
618 dprintk(1, "*** Frame lines = %d\n", frame_lines);
619
620 xc_get_quality(priv, &quality);
621 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
622}
623
624static int xc5000_set_params(struct dvb_frontend *fe,
625 struct dvb_frontend_parameters *params)
626{
627 struct xc5000_priv *priv = fe->tuner_priv;
628 int ret;
629
630 dprintk(1, "%s() frequency=%d (Hz)\n", __FUNCTION__, params->frequency);
631
632 switch(params->u.vsb.modulation) {
633 case VSB_8:
634 case VSB_16:
635 dprintk(1, "%s() VSB modulation\n", __FUNCTION__);
636 priv->rf_mode = XC_RF_MODE_AIR;
637 priv->freq_hz = params->frequency - 1750000;
638 priv->bandwidth = BANDWIDTH_6_MHZ;
639 priv->video_standard = DTV6;
640 break;
641 case QAM_64:
642 case QAM_256:
643 case QAM_AUTO:
644 dprintk(1, "%s() QAM modulation\n", __FUNCTION__);
645 priv->rf_mode = XC_RF_MODE_CABLE;
646 priv->freq_hz = params->frequency - 1750000;
647 priv->bandwidth = BANDWIDTH_6_MHZ;
648 priv->video_standard = DTV6;
649 break;
650 default:
651 return -EINVAL;
652 }
653
654 dprintk(1, "%s() frequency=%d (compensated)\n",
655 __FUNCTION__, priv->freq_hz);
656
657 ret = xc_SetSignalSource(priv, priv->rf_mode);
658 if (ret != XC_RESULT_SUCCESS) {
659 printk(KERN_ERR
660 "xc5000: xc_SetSignalSource(%d) failed\n",
661 priv->rf_mode);
662 return -EREMOTEIO;
663 }
664
665 ret = xc_SetTVStandard(priv,
666 XC5000_Standard[priv->video_standard].VideoMode,
667 XC5000_Standard[priv->video_standard].AudioMode);
668 if (ret != XC_RESULT_SUCCESS) {
669 printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
670 return -EREMOTEIO;
671 }
672
673 ret = xc_set_IF_frequency(priv, priv->cfg->if_khz);
674 if (ret != XC_RESULT_SUCCESS) {
675 printk(KERN_ERR "xc5000: xc_Set_IF_frequency(%d) failed\n",
676 priv->cfg->if_khz);
677 return -EIO;
678 }
679
680 xc_tune_channel(priv, priv->freq_hz);
681
682 if (debug)
683 xc_debug_dump(priv);
684
685 return 0;
686}
687
688static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe);
689
690static int xc5000_set_analog_params(struct dvb_frontend *fe,
691 struct analog_parameters *params)
692{
693 struct xc5000_priv *priv = fe->tuner_priv;
694 int ret;
695
696 if(priv->fwloaded == 0)
697 xc_load_fw_and_init_tuner(fe);
698
699 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
700 __FUNCTION__, params->frequency);
701
702 priv->rf_mode = XC_RF_MODE_CABLE; /* Fix me: it could be air. */
703
704 /* params->frequency is in units of 62.5khz */
705 priv->freq_hz = params->frequency * 62500;
706
707 /* FIX ME: Some video standards may have several possible audio
708 standards. We simply default to one of them here.
709 */
710 if(params->std & V4L2_STD_MN) {
711 /* default to BTSC audio standard */
712 priv->video_standard = MN_NTSC_PAL_BTSC;
713 goto tune_channel;
714 }
715
716 if(params->std & V4L2_STD_PAL_BG) {
717 /* default to NICAM audio standard */
718 priv->video_standard = BG_PAL_NICAM;
719 goto tune_channel;
720 }
721
722 if(params->std & V4L2_STD_PAL_I) {
723 /* default to NICAM audio standard */
724 priv->video_standard = I_PAL_NICAM;
725 goto tune_channel;
726 }
727
728 if(params->std & V4L2_STD_PAL_DK) {
729 /* default to NICAM audio standard */
730 priv->video_standard = DK_PAL_NICAM;
731 goto tune_channel;
732 }
733
734 if(params->std & V4L2_STD_SECAM_DK) {
735 /* default to A2 DK1 audio standard */
736 priv->video_standard = DK_SECAM_A2DK1;
737 goto tune_channel;
738 }
739
740 if(params->std & V4L2_STD_SECAM_L) {
741 priv->video_standard = L_SECAM_NICAM;
742 goto tune_channel;
743 }
744
745 if(params->std & V4L2_STD_SECAM_LC) {
746 priv->video_standard = LC_SECAM_NICAM;
747 goto tune_channel;
748 }
749
750tune_channel:
751 ret = xc_SetSignalSource(priv, priv->rf_mode);
752 if (ret != XC_RESULT_SUCCESS) {
753 printk(KERN_ERR
754 "xc5000: xc_SetSignalSource(%d) failed\n",
755 priv->rf_mode);
756 return -EREMOTEIO;
757 }
758
759 ret = xc_SetTVStandard(priv,
760 XC5000_Standard[priv->video_standard].VideoMode,
761 XC5000_Standard[priv->video_standard].AudioMode);
762 if (ret != XC_RESULT_SUCCESS) {
763 printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
764 return -EREMOTEIO;
765 }
766
767 xc_tune_channel(priv, priv->freq_hz);
768
769 if (debug)
770 xc_debug_dump(priv);
771
772 return 0;
773}
774
775static int xc5000_get_frequency(struct dvb_frontend *fe, u32 *freq)
776{
777 struct xc5000_priv *priv = fe->tuner_priv;
778 dprintk(1, "%s()\n", __FUNCTION__);
779 *freq = priv->freq_hz;
780 return 0;
781}
782
783static int xc5000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
784{
785 struct xc5000_priv *priv = fe->tuner_priv;
786 dprintk(1, "%s()\n", __FUNCTION__);
787
788 *bw = priv->bandwidth;
789 return 0;
790}
791
792static int xc5000_get_status(struct dvb_frontend *fe, u32 *status)
793{
794 struct xc5000_priv *priv = fe->tuner_priv;
795 u16 lock_status = 0;
796
797 xc_get_lock_status(priv, &lock_status);
798
799 dprintk(1, "%s() lock_status = 0x%08x\n", __FUNCTION__, lock_status);
800
801 *status = lock_status;
802
803 return 0;
804}
805
806static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe)
807{
808 struct xc5000_priv *priv = fe->tuner_priv;
809 int ret = 0;
810
811 if (priv->fwloaded == 0) {
812 ret = xc5000_fwupload(fe);
813 if (ret != XC_RESULT_SUCCESS)
814 return ret;
815 priv->fwloaded = 1;
816 }
817
818 /* Start the tuner self-calibration process */
819 ret |= xc_initialize(priv);
820
821 /* Wait for calibration to complete.
822 * We could continue but XC5000 will clock stretch subsequent
823 * I2C transactions until calibration is complete. This way we
824 * don't have to rely on clock stretching working.
825 */
826 xc_wait( 100 );
827
828 /* Default to "CABLE" mode */
829 ret |= xc_write_reg(priv, XREG_SIGNALSOURCE, XC_RF_MODE_CABLE);
830
831 return ret;
832}
833
834static int xc5000_sleep(struct dvb_frontend *fe)
835{
836 struct xc5000_priv *priv = fe->tuner_priv;
837 int ret;
838
839 dprintk(1, "%s()\n", __FUNCTION__);
840
841 /* On Pinnacle PCTV HD 800i, the tuner cannot be reinitialized
842 * once shutdown without reloading the driver. Maybe I am not
843 * doing something right.
844 *
845 */
846
847 ret = xc_shutdown(priv);
848 if(ret != XC_RESULT_SUCCESS) {
849 printk(KERN_ERR
850 "xc5000: %s() unable to shutdown tuner\n",
851 __FUNCTION__);
852 return -EREMOTEIO;
853 }
854 else {
855 /* priv->fwloaded = 0; */
856 return XC_RESULT_SUCCESS;
857 }
858}
859
860static int xc5000_init(struct dvb_frontend *fe)
861{
862 struct xc5000_priv *priv = fe->tuner_priv;
863 dprintk(1, "%s()\n", __FUNCTION__);
864
865 if (xc_load_fw_and_init_tuner(fe) != XC_RESULT_SUCCESS) {
866 printk(KERN_ERR "xc5000: Unable to initialise tuner\n");
867 return -EREMOTEIO;
868 }
869
870 if (debug)
871 xc_debug_dump(priv);
872
873 return 0;
874}
875
876static int xc5000_release(struct dvb_frontend *fe)
877{
878 dprintk(1, "%s()\n", __FUNCTION__);
879 kfree(fe->tuner_priv);
880 fe->tuner_priv = NULL;
881 return 0;
882}
883
884static const struct dvb_tuner_ops xc5000_tuner_ops = {
885 .info = {
886 .name = "Xceive XC5000",
887 .frequency_min = 1000000,
888 .frequency_max = 1023000000,
889 .frequency_step = 50000,
890 },
891
892 .release = xc5000_release,
893 .init = xc5000_init,
894 .sleep = xc5000_sleep,
895
896 .set_params = xc5000_set_params,
897 .set_analog_params = xc5000_set_analog_params,
898 .get_frequency = xc5000_get_frequency,
899 .get_bandwidth = xc5000_get_bandwidth,
900 .get_status = xc5000_get_status
901};
902
903struct dvb_frontend * xc5000_attach(struct dvb_frontend *fe,
904 struct i2c_adapter *i2c,
905 struct xc5000_config *cfg)
906{
907 struct xc5000_priv *priv = NULL;
908 u16 id = 0;
909
910 dprintk(1, "%s()\n", __FUNCTION__);
911
912 priv = kzalloc(sizeof(struct xc5000_priv), GFP_KERNEL);
913 if (priv == NULL)
914 return NULL;
915
916 priv->cfg = cfg;
917 priv->bandwidth = BANDWIDTH_6_MHZ;
918 priv->i2c = i2c;
919
920 /* Check if firmware has been loaded. It is possible that another
921 instance of the driver has loaded the firmware.
922 */
923 if (xc5000_readreg(priv, XREG_PRODUCT_ID, &id) != 0) {
924 kfree(priv);
925 return NULL;
926 }
927
928 switch(id) {
929 case XC_PRODUCT_ID_FW_LOADED:
930 printk(KERN_INFO
931 "xc5000: Successfully identified at address 0x%02x\n",
932 cfg->i2c_address);
933 printk(KERN_INFO
934 "xc5000: Firmware has been loaded previously\n");
935 priv->fwloaded = 1;
936 break;
937 case XC_PRODUCT_ID_FW_NOT_LOADED:
938 printk(KERN_INFO
939 "xc5000: Successfully identified at address 0x%02x\n",
940 cfg->i2c_address);
941 printk(KERN_INFO
942 "xc5000: Firmware has not been loaded previously\n");
943 priv->fwloaded = 0;
944 break;
945 default:
946 printk(KERN_ERR
947 "xc5000: Device not found at addr 0x%02x (0x%x)\n",
948 cfg->i2c_address, id);
949 kfree(priv);
950 return NULL;
951 }
952
953 memcpy(&fe->ops.tuner_ops, &xc5000_tuner_ops,
954 sizeof(struct dvb_tuner_ops));
955
956 fe->tuner_priv = priv;
957
958 return fe;
959}
960EXPORT_SYMBOL(xc5000_attach);
961
962MODULE_AUTHOR("Steven Toth");
963MODULE_DESCRIPTION("Xceive xc5000 silicon tuner driver");
964MODULE_LICENSE("GPL");
diff --git a/drivers/media/dvb/frontends/xc5000.h b/drivers/media/dvb/frontends/xc5000.h
new file mode 100644
index 000000000000..e0e84562aed1
--- /dev/null
+++ b/drivers/media/dvb/frontends/xc5000.h
@@ -0,0 +1,62 @@
1/*
2 * Driver for Xceive XC5000 "QAM/8VSB single chip tuner"
3 *
4 * Copyright (c) 2007 Steven Toth <stoth@hauppauge.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#ifndef __XC5000_H__
23#define __XC5000_H__
24
25#include <linux/firmware.h>
26
27struct dvb_frontend;
28struct i2c_adapter;
29
30struct xc5000_config {
31 u8 i2c_address;
32 u32 if_khz;
33
34 /* For each bridge framework, when it attaches either analog or digital,
35 * it has to store a reference back to its _core equivalent structure,
36 * so that it can service the hardware by steering gpio's etc.
37 * Each bridge implementation is different so cast priv accordingly.
38 * The xc5000 driver cares not for this value, other than ensuring
39 * it's passed back to a bridge during tuner_callback().
40 */
41 void *priv;
42 int (*tuner_callback) (void *priv, int command, int arg);
43};
44
45/* xc5000 callback command */
46#define XC5000_TUNER_RESET 0
47
48#if defined(CONFIG_DVB_TUNER_XC5000) || defined(CONFIG_DVB_TUNER_XC5000_MODULE)
49extern struct dvb_frontend* xc5000_attach(struct dvb_frontend *fe,
50 struct i2c_adapter *i2c,
51 struct xc5000_config *cfg);
52#else
53static inline struct dvb_frontend* xc5000_attach(struct dvb_frontend *fe,
54 struct i2c_adapter *i2c,
55 struct xc5000_config *cfg)
56{
57 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __FUNCTION__);
58 return NULL;
59}
60#endif // CONFIG_DVB_TUNER_XC5000
61
62#endif // __XC5000_H__
diff --git a/drivers/media/dvb/frontends/xc5000_priv.h b/drivers/media/dvb/frontends/xc5000_priv.h
new file mode 100644
index 000000000000..13b2d19341da
--- /dev/null
+++ b/drivers/media/dvb/frontends/xc5000_priv.h
@@ -0,0 +1,36 @@
1/*
2 * Driver for Xceive XC5000 "QAM/8VSB single chip tuner"
3 *
4 * Copyright (c) 2007 Steven Toth <stoth@hauppauge.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#ifndef XC5000_PRIV_H
23#define XC5000_PRIV_H
24
25struct xc5000_priv {
26 struct xc5000_config *cfg;
27 struct i2c_adapter *i2c;
28
29 u32 freq_hz;
30 u32 bandwidth;
31 u8 video_standard;
32 u8 rf_mode;
33 u8 fwloaded;
34};
35
36#endif
diff --git a/drivers/media/dvb/frontends/zl10353.c b/drivers/media/dvb/frontends/zl10353.c
index 0106df4c55e8..276e3b631dc2 100644
--- a/drivers/media/dvb/frontends/zl10353.c
+++ b/drivers/media/dvb/frontends/zl10353.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * Driver for Zarlink DVB-T ZL10353 demodulator 2 * Driver for Zarlink DVB-T ZL10353 demodulator
3 * 3 *
4 * Copyright (C) 2006 Christopher Pascoe <c.pascoe@itee.uq.edu.au> 4 * Copyright (C) 2006, 2007 Christopher Pascoe <c.pascoe@itee.uq.edu.au>
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 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 7 * it under the terms of the GNU General Public License as published by
@@ -16,7 +16,7 @@
16 * 16 *
17 * You should have received a copy of the GNU General Public License 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 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.= 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */ 20 */
21 21
22#include <linux/kernel.h> 22#include <linux/kernel.h>
@@ -25,6 +25,7 @@
25#include <linux/delay.h> 25#include <linux/delay.h>
26#include <linux/string.h> 26#include <linux/string.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <asm/div64.h>
28 29
29#include "dvb_frontend.h" 30#include "dvb_frontend.h"
30#include "zl10353_priv.h" 31#include "zl10353_priv.h"
@@ -35,6 +36,8 @@ struct zl10353_state {
35 struct dvb_frontend frontend; 36 struct dvb_frontend frontend;
36 37
37 struct zl10353_config config; 38 struct zl10353_config config;
39
40 enum fe_bandwidth bandwidth;
38}; 41};
39 42
40static int debug; 43static int debug;
@@ -122,9 +125,10 @@ static void zl10353_calc_nominal_rate(struct dvb_frontend *fe,
122 enum fe_bandwidth bandwidth, 125 enum fe_bandwidth bandwidth,
123 u16 *nominal_rate) 126 u16 *nominal_rate)
124{ 127{
125 u32 adc_clock = 45056; /* 45.056 MHz */
126 u8 bw;
127 struct zl10353_state *state = fe->demodulator_priv; 128 struct zl10353_state *state = fe->demodulator_priv;
129 u32 adc_clock = 450560; /* 45.056 MHz */
130 u64 value;
131 u8 bw;
128 132
129 if (state->config.adc_clock) 133 if (state->config.adc_clock)
130 adc_clock = state->config.adc_clock; 134 adc_clock = state->config.adc_clock;
@@ -142,12 +146,44 @@ static void zl10353_calc_nominal_rate(struct dvb_frontend *fe,
142 break; 146 break;
143 } 147 }
144 148
145 *nominal_rate = (bw * (1 << 23) / 7 * 125 + adc_clock / 2) / adc_clock; 149 value = (u64)10 * (1 << 23) / 7 * 125;
150 value = (bw * value) + adc_clock / 2;
151 do_div(value, adc_clock);
152 *nominal_rate = value;
146 153
147 dprintk("%s: bw %d, adc_clock %d => 0x%x\n", 154 dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
148 __FUNCTION__, bw, adc_clock, *nominal_rate); 155 __FUNCTION__, bw, adc_clock, *nominal_rate);
149} 156}
150 157
158static void zl10353_calc_input_freq(struct dvb_frontend *fe,
159 u16 *input_freq)
160{
161 struct zl10353_state *state = fe->demodulator_priv;
162 u32 adc_clock = 450560; /* 45.056 MHz */
163 int if2 = 361667; /* 36.1667 MHz */
164 int ife;
165 u64 value;
166
167 if (state->config.adc_clock)
168 adc_clock = state->config.adc_clock;
169 if (state->config.if2)
170 if2 = state->config.if2;
171
172 if (adc_clock >= if2 * 2)
173 ife = if2;
174 else {
175 ife = adc_clock - (if2 % adc_clock);
176 if (ife > adc_clock / 2)
177 ife = adc_clock - ife;
178 }
179 value = (u64)65536 * ife + adc_clock / 2;
180 do_div(value, adc_clock);
181 *input_freq = -value;
182
183 dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
184 __FUNCTION__, if2, ife, adc_clock, -(int)value, *input_freq);
185}
186
151static int zl10353_sleep(struct dvb_frontend *fe) 187static int zl10353_sleep(struct dvb_frontend *fe)
152{ 188{
153 static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 }; 189 static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
@@ -160,64 +196,276 @@ static int zl10353_set_parameters(struct dvb_frontend *fe,
160 struct dvb_frontend_parameters *param) 196 struct dvb_frontend_parameters *param)
161{ 197{
162 struct zl10353_state *state = fe->demodulator_priv; 198 struct zl10353_state *state = fe->demodulator_priv;
163 u16 nominal_rate; 199 u16 nominal_rate, input_freq;
164 u8 pllbuf[6] = { 0x67 }; 200 u8 pllbuf[6] = { 0x67 }, acq_ctl = 0;
201 u16 tps = 0;
202 struct dvb_ofdm_parameters *op = &param->u.ofdm;
165 203
166 /* These settings set "auto-everything" and start the FSM. */ 204 zl10353_single_write(fe, RESET, 0x80);
167 zl10353_single_write(fe, 0x55, 0x80);
168 udelay(200); 205 udelay(200);
169 zl10353_single_write(fe, 0xEA, 0x01); 206 zl10353_single_write(fe, 0xEA, 0x01);
170 udelay(200); 207 udelay(200);
171 zl10353_single_write(fe, 0xEA, 0x00); 208 zl10353_single_write(fe, 0xEA, 0x00);
172 209
173 zl10353_single_write(fe, 0x56, 0x28); 210 zl10353_single_write(fe, AGC_TARGET, 0x28);
174 zl10353_single_write(fe, 0x89, 0x20); 211
175 zl10353_single_write(fe, 0x5E, 0x00); 212 if (op->transmission_mode != TRANSMISSION_MODE_AUTO)
213 acq_ctl |= (1 << 0);
214 if (op->guard_interval != GUARD_INTERVAL_AUTO)
215 acq_ctl |= (1 << 1);
216 zl10353_single_write(fe, ACQ_CTL, acq_ctl);
176 217
177 zl10353_calc_nominal_rate(fe, param->u.ofdm.bandwidth, &nominal_rate); 218 switch (op->bandwidth) {
219 case BANDWIDTH_6_MHZ:
220 /* These are extrapolated from the 7 and 8MHz values */
221 zl10353_single_write(fe, MCLK_RATIO, 0x97);
222 zl10353_single_write(fe, 0x64, 0x34);
223 break;
224 case BANDWIDTH_7_MHZ:
225 zl10353_single_write(fe, MCLK_RATIO, 0x86);
226 zl10353_single_write(fe, 0x64, 0x35);
227 break;
228 case BANDWIDTH_8_MHZ:
229 default:
230 zl10353_single_write(fe, MCLK_RATIO, 0x75);
231 zl10353_single_write(fe, 0x64, 0x36);
232 }
233
234 zl10353_calc_nominal_rate(fe, op->bandwidth, &nominal_rate);
178 zl10353_single_write(fe, TRL_NOMINAL_RATE_1, msb(nominal_rate)); 235 zl10353_single_write(fe, TRL_NOMINAL_RATE_1, msb(nominal_rate));
179 zl10353_single_write(fe, TRL_NOMINAL_RATE_0, lsb(nominal_rate)); 236 zl10353_single_write(fe, TRL_NOMINAL_RATE_0, lsb(nominal_rate));
237 state->bandwidth = op->bandwidth;
238
239 zl10353_calc_input_freq(fe, &input_freq);
240 zl10353_single_write(fe, INPUT_FREQ_1, msb(input_freq));
241 zl10353_single_write(fe, INPUT_FREQ_0, lsb(input_freq));
242
243 /* Hint at TPS settings */
244 switch (op->code_rate_HP) {
245 case FEC_2_3:
246 tps |= (1 << 7);
247 break;
248 case FEC_3_4:
249 tps |= (2 << 7);
250 break;
251 case FEC_5_6:
252 tps |= (3 << 7);
253 break;
254 case FEC_7_8:
255 tps |= (4 << 7);
256 break;
257 case FEC_1_2:
258 case FEC_AUTO:
259 break;
260 default:
261 return -EINVAL;
262 }
263
264 switch (op->code_rate_LP) {
265 case FEC_2_3:
266 tps |= (1 << 4);
267 break;
268 case FEC_3_4:
269 tps |= (2 << 4);
270 break;
271 case FEC_5_6:
272 tps |= (3 << 4);
273 break;
274 case FEC_7_8:
275 tps |= (4 << 4);
276 break;
277 case FEC_1_2:
278 case FEC_AUTO:
279 break;
280 case FEC_NONE:
281 if (op->hierarchy_information == HIERARCHY_AUTO ||
282 op->hierarchy_information == HIERARCHY_NONE)
283 break;
284 default:
285 return -EINVAL;
286 }
287
288 switch (op->constellation) {
289 case QPSK:
290 break;
291 case QAM_AUTO:
292 case QAM_16:
293 tps |= (1 << 13);
294 break;
295 case QAM_64:
296 tps |= (2 << 13);
297 break;
298 default:
299 return -EINVAL;
300 }
301
302 switch (op->transmission_mode) {
303 case TRANSMISSION_MODE_2K:
304 case TRANSMISSION_MODE_AUTO:
305 break;
306 case TRANSMISSION_MODE_8K:
307 tps |= (1 << 0);
308 break;
309 default:
310 return -EINVAL;
311 }
312
313 switch (op->guard_interval) {
314 case GUARD_INTERVAL_1_32:
315 case GUARD_INTERVAL_AUTO:
316 break;
317 case GUARD_INTERVAL_1_16:
318 tps |= (1 << 2);
319 break;
320 case GUARD_INTERVAL_1_8:
321 tps |= (2 << 2);
322 break;
323 case GUARD_INTERVAL_1_4:
324 tps |= (3 << 2);
325 break;
326 default:
327 return -EINVAL;
328 }
329
330 switch (op->hierarchy_information) {
331 case HIERARCHY_AUTO:
332 case HIERARCHY_NONE:
333 break;
334 case HIERARCHY_1:
335 tps |= (1 << 10);
336 break;
337 case HIERARCHY_2:
338 tps |= (2 << 10);
339 break;
340 case HIERARCHY_4:
341 tps |= (3 << 10);
342 break;
343 default:
344 return -EINVAL;
345 }
346
347 zl10353_single_write(fe, TPS_GIVEN_1, msb(tps));
348 zl10353_single_write(fe, TPS_GIVEN_0, lsb(tps));
180 349
181 zl10353_single_write(fe, 0x6C, 0xCD);
182 zl10353_single_write(fe, 0x6D, 0x7E);
183 if (fe->ops.i2c_gate_ctrl) 350 if (fe->ops.i2c_gate_ctrl)
184 fe->ops.i2c_gate_ctrl(fe, 0); 351 fe->ops.i2c_gate_ctrl(fe, 0);
185 352
186 // if there is no attached secondary tuner, we call set_params to program 353 /*
187 // a potential tuner attached somewhere else 354 * If there is no tuner attached to the secondary I2C bus, we call
355 * set_params to program a potential tuner attached somewhere else.
356 * Otherwise, we update the PLL registers via calc_regs.
357 */
188 if (state->config.no_tuner) { 358 if (state->config.no_tuner) {
189 if (fe->ops.tuner_ops.set_params) { 359 if (fe->ops.tuner_ops.set_params) {
190 fe->ops.tuner_ops.set_params(fe, param); 360 fe->ops.tuner_ops.set_params(fe, param);
191 if (fe->ops.i2c_gate_ctrl) 361 if (fe->ops.i2c_gate_ctrl)
192 fe->ops.i2c_gate_ctrl(fe, 0); 362 fe->ops.i2c_gate_ctrl(fe, 0);
193 } 363 }
364 } else if (fe->ops.tuner_ops.calc_regs) {
365 fe->ops.tuner_ops.calc_regs(fe, param, pllbuf + 1, 5);
366 pllbuf[1] <<= 1;
367 zl10353_write(fe, pllbuf, sizeof(pllbuf));
194 } 368 }
195 369
196 // if pllbuf is defined, retrieve the settings 370 zl10353_single_write(fe, 0x5F, 0x13);
197 if (fe->ops.tuner_ops.calc_regs) { 371
198 fe->ops.tuner_ops.calc_regs(fe, param, pllbuf+1, 5); 372 /* If no attached tuner or invalid PLL registers, just start the FSM. */
199 pllbuf[1] <<= 1; 373 if (state->config.no_tuner || fe->ops.tuner_ops.calc_regs == NULL)
200 } else { 374 zl10353_single_write(fe, FSM_GO, 0x01);
201 // fake pllbuf settings 375 else
202 pllbuf[1] = 0x61 << 1; 376 zl10353_single_write(fe, TUNER_GO, 0x01);
203 pllbuf[2] = 0; 377
204 pllbuf[3] = 0; 378 return 0;
205 pllbuf[3] = 0; 379}
206 pllbuf[4] = 0; 380
381static int zl10353_get_parameters(struct dvb_frontend *fe,
382 struct dvb_frontend_parameters *param)
383{
384 struct zl10353_state *state = fe->demodulator_priv;
385 struct dvb_ofdm_parameters *op = &param->u.ofdm;
386 int s6, s9;
387 u16 tps;
388 static const u8 tps_fec_to_api[8] = {
389 FEC_1_2,
390 FEC_2_3,
391 FEC_3_4,
392 FEC_5_6,
393 FEC_7_8,
394 FEC_AUTO,
395 FEC_AUTO,
396 FEC_AUTO
397 };
398
399 s6 = zl10353_read_register(state, STATUS_6);
400 s9 = zl10353_read_register(state, STATUS_9);
401 if (s6 < 0 || s9 < 0)
402 return -EREMOTEIO;
403 if ((s6 & (1 << 5)) == 0 || (s9 & (1 << 4)) == 0)
404 return -EINVAL; /* no FE or TPS lock */
405
406 tps = zl10353_read_register(state, TPS_RECEIVED_1) << 8 |
407 zl10353_read_register(state, TPS_RECEIVED_0);
408
409 op->code_rate_HP = tps_fec_to_api[(tps >> 7) & 7];
410 op->code_rate_LP = tps_fec_to_api[(tps >> 4) & 7];
411
412 switch ((tps >> 13) & 3) {
413 case 0:
414 op->constellation = QPSK;
415 break;
416 case 1:
417 op->constellation = QAM_16;
418 break;
419 case 2:
420 op->constellation = QAM_64;
421 break;
422 default:
423 op->constellation = QAM_AUTO;
424 break;
207 } 425 }
208 426
209 // there is no call to _just_ start decoding, so we send the pllbuf anyway 427 op->transmission_mode = (tps & 0x01) ? TRANSMISSION_MODE_8K :
210 // even if there isn't a PLL attached to the secondary bus 428 TRANSMISSION_MODE_2K;
211 zl10353_write(fe, pllbuf, sizeof(pllbuf));
212 429
213 zl10353_single_write(fe, 0x5F, 0x13); 430 switch ((tps >> 2) & 3) {
214 zl10353_single_write(fe, 0x70, 0x01); 431 case 0:
215 udelay(250); 432 op->guard_interval = GUARD_INTERVAL_1_32;
216 zl10353_single_write(fe, 0xE4, 0x00); 433 break;
217 zl10353_single_write(fe, 0xE5, 0x2A); 434 case 1:
218 zl10353_single_write(fe, 0xE9, 0x02); 435 op->guard_interval = GUARD_INTERVAL_1_16;
219 zl10353_single_write(fe, 0xE7, 0x40); 436 break;
220 zl10353_single_write(fe, 0xE8, 0x10); 437 case 2:
438 op->guard_interval = GUARD_INTERVAL_1_8;
439 break;
440 case 3:
441 op->guard_interval = GUARD_INTERVAL_1_4;
442 break;
443 default:
444 op->guard_interval = GUARD_INTERVAL_AUTO;
445 break;
446 }
447
448 switch ((tps >> 10) & 7) {
449 case 0:
450 op->hierarchy_information = HIERARCHY_NONE;
451 break;
452 case 1:
453 op->hierarchy_information = HIERARCHY_1;
454 break;
455 case 2:
456 op->hierarchy_information = HIERARCHY_2;
457 break;
458 case 3:
459 op->hierarchy_information = HIERARCHY_4;
460 break;
461 default:
462 op->hierarchy_information = HIERARCHY_AUTO;
463 break;
464 }
465
466 param->frequency = 0;
467 op->bandwidth = state->bandwidth;
468 param->inversion = INVERSION_AUTO;
221 469
222 return 0; 470 return 0;
223} 471}
@@ -406,6 +654,7 @@ static struct dvb_frontend_ops zl10353_ops = {
406 .write = zl10353_write, 654 .write = zl10353_write,
407 655
408 .set_frontend = zl10353_set_parameters, 656 .set_frontend = zl10353_set_parameters,
657 .get_frontend = zl10353_get_parameters,
409 .get_tune_settings = zl10353_get_tune_settings, 658 .get_tune_settings = zl10353_get_tune_settings,
410 659
411 .read_status = zl10353_read_status, 660 .read_status = zl10353_read_status,
diff --git a/drivers/media/dvb/frontends/zl10353.h b/drivers/media/dvb/frontends/zl10353.h
index 1c3d494a6da9..fc734c22b5fa 100644
--- a/drivers/media/dvb/frontends/zl10353.h
+++ b/drivers/media/dvb/frontends/zl10353.h
@@ -1,7 +1,7 @@
1/* 1/*
2 * Driver for Zarlink DVB-T ZL10353 demodulator 2 * Driver for Zarlink DVB-T ZL10353 demodulator
3 * 3 *
4 * Copyright (C) 2006 Christopher Pascoe <c.pascoe@itee.uq.edu.au> 4 * Copyright (C) 2006, 2007 Christopher Pascoe <c.pascoe@itee.uq.edu.au>
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 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 7 * it under the terms of the GNU General Public License as published by
@@ -29,8 +29,9 @@ struct zl10353_config
29 /* demodulator's I2C address */ 29 /* demodulator's I2C address */
30 u8 demod_address; 30 u8 demod_address;
31 31
32 /* frequencies in kHz */ 32 /* frequencies in units of 0.1kHz */
33 int adc_clock; /* default: 45056 */ 33 int adc_clock; /* default: 450560 (45.056 MHz) */
34 int if2; /* default: 361667 (36.1667 MHz) */
34 35
35 /* set if no pll is connected to the secondary i2c bus */ 36 /* set if no pll is connected to the secondary i2c bus */
36 int no_tuner; 37 int no_tuner;
@@ -49,6 +50,6 @@ static inline struct dvb_frontend* zl10353_attach(const struct zl10353_config *c
49 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __FUNCTION__); 50 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __FUNCTION__);
50 return NULL; 51 return NULL;
51} 52}
52#endif // CONFIG_DVB_ZL10353 53#endif /* CONFIG_DVB_ZL10353 */
53 54
54#endif /* ZL10353_H */ 55#endif /* ZL10353_H */
diff --git a/drivers/media/dvb/frontends/zl10353_priv.h b/drivers/media/dvb/frontends/zl10353_priv.h
index 4962434b35e7..055ff1f7e349 100644
--- a/drivers/media/dvb/frontends/zl10353_priv.h
+++ b/drivers/media/dvb/frontends/zl10353_priv.h
@@ -1,7 +1,7 @@
1/* 1/*
2 * Driver for Zarlink DVB-T ZL10353 demodulator 2 * Driver for Zarlink DVB-T ZL10353 demodulator
3 * 3 *
4 * Copyright (C) 2006 Christopher Pascoe <c.pascoe@itee.uq.edu.au> 4 * Copyright (C) 2006, 2007 Christopher Pascoe <c.pascoe@itee.uq.edu.au>
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 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 7 * it under the terms of the GNU General Public License as published by
@@ -16,7 +16,7 @@
16 * 16 *
17 * You should have received a copy of the GNU General Public License 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 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.= 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */ 20 */
21 21
22#ifndef _ZL10353_PRIV_ 22#ifndef _ZL10353_PRIV_
@@ -46,9 +46,28 @@ enum zl10353_reg_addr {
46 RS_ERR_CNT_0 = 0x13, 46 RS_ERR_CNT_0 = 0x13,
47 RS_UBC_1 = 0x14, 47 RS_UBC_1 = 0x14,
48 RS_UBC_0 = 0x15, 48 RS_UBC_0 = 0x15,
49 TPS_RECEIVED_1 = 0x1D,
50 TPS_RECEIVED_0 = 0x1E,
51 TPS_CURRENT_1 = 0x1F,
52 TPS_CURRENT_0 = 0x20,
53 RESET = 0x55,
54 AGC_TARGET = 0x56,
55 MCLK_RATIO = 0x5C,
56 ACQ_CTL = 0x5E,
49 TRL_NOMINAL_RATE_1 = 0x65, 57 TRL_NOMINAL_RATE_1 = 0x65,
50 TRL_NOMINAL_RATE_0 = 0x66, 58 TRL_NOMINAL_RATE_0 = 0x66,
59 INPUT_FREQ_1 = 0x6C,
60 INPUT_FREQ_0 = 0x6D,
61 TPS_GIVEN_1 = 0x6E,
62 TPS_GIVEN_0 = 0x6F,
63 TUNER_GO = 0x70,
64 FSM_GO = 0x71,
51 CHIP_ID = 0x7F, 65 CHIP_ID = 0x7F,
66 CHAN_STEP_1 = 0xE4,
67 CHAN_STEP_0 = 0xE5,
68 OFDM_LOCK_TIME = 0xE7,
69 FEC_LOCK_TIME = 0xE8,
70 ACQ_DELAY = 0xE9,
52}; 71};
53 72
54#endif /* _ZL10353_PRIV_ */ 73#endif /* _ZL10353_PRIV_ */
diff --git a/drivers/media/dvb/ttpci/Kconfig b/drivers/media/dvb/ttpci/Kconfig
index 54b91f26ca63..ae882432dd3d 100644
--- a/drivers/media/dvb/ttpci/Kconfig
+++ b/drivers/media/dvb/ttpci/Kconfig
@@ -1,8 +1,14 @@
1config TTPCI_EEPROM
2 tristate
3 default n
4
1config DVB_AV7110 5config DVB_AV7110
2 tristate "AV7110 cards" 6 tristate "AV7110 cards"
3 depends on DVB_CORE && PCI && I2C && VIDEO_V4L1 7 depends on DVB_CORE && PCI && I2C
4 select FW_LOADER if !DVB_AV7110_FIRMWARE 8 select FW_LOADER if !DVB_AV7110_FIRMWARE
9 select TTPCI_EEPROM
5 select VIDEO_SAA7146_VV 10 select VIDEO_SAA7146_VV
11 depends on VIDEO_DEV # dependencies of VIDEO_SAA7146_VV
6 select DVB_VES1820 if !DVB_FE_CUSTOMISE 12 select DVB_VES1820 if !DVB_FE_CUSTOMISE
7 select DVB_VES1X93 if !DVB_FE_CUSTOMISE 13 select DVB_VES1X93 if !DVB_FE_CUSTOMISE
8 select DVB_STV0299 if !DVB_FE_CUSTOMISE 14 select DVB_STV0299 if !DVB_FE_CUSTOMISE
@@ -57,10 +63,19 @@ config DVB_AV7110_OSD
57 63
58 All other people say N. 64 All other people say N.
59 65
66config DVB_BUDGET_CORE
67 tristate "SAA7146 DVB cards (aka Budget, Nova-PCI)"
68 depends on DVB_CORE && PCI && I2C
69 select VIDEO_SAA7146
70 select TTPCI_EEPROM
71 help
72 Support for simple SAA7146 based DVB cards
73 (so called Budget- or Nova-PCI cards) without onboard
74 MPEG2 decoder.
75
60config DVB_BUDGET 76config DVB_BUDGET
61 tristate "Budget cards" 77 tristate "Budget cards"
62 depends on DVB_CORE && PCI && I2C && VIDEO_V4L1 78 depends on DVB_BUDGET_CORE && I2C
63 select VIDEO_SAA7146
64 select DVB_STV0299 if !DVB_FE_CUSTOMISE 79 select DVB_STV0299 if !DVB_FE_CUSTOMISE
65 select DVB_VES1X93 if !DVB_FE_CUSTOMISE 80 select DVB_VES1X93 if !DVB_FE_CUSTOMISE
66 select DVB_VES1820 if !DVB_FE_CUSTOMISE 81 select DVB_VES1820 if !DVB_FE_CUSTOMISE
@@ -73,9 +88,9 @@ config DVB_BUDGET
73 select DVB_TDA826X if !DVB_FE_CUSTOMISE 88 select DVB_TDA826X if !DVB_FE_CUSTOMISE
74 select DVB_LNBP21 if !DVB_FE_CUSTOMISE 89 select DVB_LNBP21 if !DVB_FE_CUSTOMISE
75 help 90 help
76 Support for simple SAA7146 based DVB cards 91 Support for simple SAA7146 based DVB cards (so called Budget-
77 (so called Budget- or Nova-PCI cards) without onboard 92 or Nova-PCI cards) without onboard MPEG2 decoder, and without
78 MPEG2 decoder. 93 analog inputs or an onboard Common Interface connector.
79 94
80 Say Y if you own such a card and want to use it. 95 Say Y if you own such a card and want to use it.
81 96
@@ -84,8 +99,7 @@ config DVB_BUDGET
84 99
85config DVB_BUDGET_CI 100config DVB_BUDGET_CI
86 tristate "Budget cards with onboard CI connector" 101 tristate "Budget cards with onboard CI connector"
87 depends on DVB_CORE && PCI && I2C && VIDEO_V4L1 && INPUT 102 depends on DVB_BUDGET_CORE && I2C
88 select VIDEO_SAA7146
89 select DVB_STV0297 if !DVB_FE_CUSTOMISE 103 select DVB_STV0297 if !DVB_FE_CUSTOMISE
90 select DVB_STV0299 if !DVB_FE_CUSTOMISE 104 select DVB_STV0299 if !DVB_FE_CUSTOMISE
91 select DVB_TDA1004X if !DVB_FE_CUSTOMISE 105 select DVB_TDA1004X if !DVB_FE_CUSTOMISE
@@ -106,8 +120,9 @@ config DVB_BUDGET_CI
106 120
107config DVB_BUDGET_AV 121config DVB_BUDGET_AV
108 tristate "Budget cards with analog video inputs" 122 tristate "Budget cards with analog video inputs"
109 depends on DVB_CORE && PCI && I2C && VIDEO_V4L1 123 depends on DVB_BUDGET_CORE && I2C
110 select VIDEO_SAA7146_VV 124 select VIDEO_SAA7146_VV
125 depends on VIDEO_DEV # dependencies of VIDEO_SAA7146_VV
111 select DVB_PLL if !DVB_FE_CUSTOMISE 126 select DVB_PLL if !DVB_FE_CUSTOMISE
112 select DVB_STV0299 if !DVB_FE_CUSTOMISE 127 select DVB_STV0299 if !DVB_FE_CUSTOMISE
113 select DVB_TDA1004X if !DVB_FE_CUSTOMISE 128 select DVB_TDA1004X if !DVB_FE_CUSTOMISE
@@ -127,8 +142,8 @@ config DVB_BUDGET_AV
127 142
128config DVB_BUDGET_PATCH 143config DVB_BUDGET_PATCH
129 tristate "AV7110 cards with Budget Patch" 144 tristate "AV7110 cards with Budget Patch"
130 depends on DVB_CORE && DVB_BUDGET && VIDEO_V4L1 145 depends on DVB_BUDGET_CORE && I2C
131 select DVB_AV7110 146 depends on DVB_AV7110
132 select DVB_STV0299 if !DVB_FE_CUSTOMISE 147 select DVB_STV0299 if !DVB_FE_CUSTOMISE
133 select DVB_VES1X93 if !DVB_FE_CUSTOMISE 148 select DVB_VES1X93 if !DVB_FE_CUSTOMISE
134 select DVB_TDA8083 if !DVB_FE_CUSTOMISE 149 select DVB_TDA8083 if !DVB_FE_CUSTOMISE
diff --git a/drivers/media/dvb/ttpci/Makefile b/drivers/media/dvb/ttpci/Makefile
index 2c1145236ee6..d7483f1a9b3f 100644
--- a/drivers/media/dvb/ttpci/Makefile
+++ b/drivers/media/dvb/ttpci/Makefile
@@ -5,11 +5,13 @@
5 5
6dvb-ttpci-objs := av7110_hw.o av7110_v4l.o av7110_av.o av7110_ca.o av7110.o av7110_ipack.o av7110_ir.o 6dvb-ttpci-objs := av7110_hw.o av7110_v4l.o av7110_av.o av7110_ca.o av7110.o av7110_ipack.o av7110_ir.o
7 7
8obj-$(CONFIG_DVB_BUDGET) += budget-core.o budget.o ttpci-eeprom.o 8obj-$(CONFIG_TTPCI_EEPROM) += ttpci-eeprom.o
9obj-$(CONFIG_DVB_BUDGET_AV) += budget-core.o budget-av.o ttpci-eeprom.o 9obj-$(CONFIG_DVB_BUDGET_CORE) += budget-core.o
10obj-$(CONFIG_DVB_BUDGET_CI) += budget-core.o budget-ci.o ttpci-eeprom.o 10obj-$(CONFIG_DVB_BUDGET) += budget.o
11obj-$(CONFIG_DVB_BUDGET_PATCH) += budget-core.o budget-patch.o ttpci-eeprom.o 11obj-$(CONFIG_DVB_BUDGET_AV) += budget-av.o
12obj-$(CONFIG_DVB_AV7110) += dvb-ttpci.o ttpci-eeprom.o 12obj-$(CONFIG_DVB_BUDGET_CI) += budget-ci.o
13obj-$(CONFIG_DVB_BUDGET_PATCH) += budget-patch.o
14obj-$(CONFIG_DVB_AV7110) += dvb-ttpci.o
13 15
14EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core/ -Idrivers/media/dvb/frontends/ 16EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core/ -Idrivers/media/dvb/frontends/
15 17
diff --git a/drivers/media/dvb/ttpci/av7110.c b/drivers/media/dvb/ttpci/av7110.c
index 0d36c155695b..0e5701bdff19 100644
--- a/drivers/media/dvb/ttpci/av7110.c
+++ b/drivers/media/dvb/ttpci/av7110.c
@@ -2595,7 +2595,8 @@ static int __devinit av7110_attach(struct saa7146_dev* dev,
2595 mutex_init(&av7110->osd_mutex); 2595 mutex_init(&av7110->osd_mutex);
2596 2596
2597 /* TV standard */ 2597 /* TV standard */
2598 av7110->vidmode = tv_standard == 1 ? VIDEO_MODE_NTSC : VIDEO_MODE_PAL; 2598 av7110->vidmode = tv_standard == 1 ? AV7110_VIDEO_MODE_NTSC
2599 : AV7110_VIDEO_MODE_PAL;
2599 2600
2600 /* ARM "watchdog" */ 2601 /* ARM "watchdog" */
2601 init_waitqueue_head(&av7110->arm_wait); 2602 init_waitqueue_head(&av7110->arm_wait);
diff --git a/drivers/media/dvb/ttpci/av7110.h b/drivers/media/dvb/ttpci/av7110.h
index 0cb439527498..39fbf7d5cffb 100644
--- a/drivers/media/dvb/ttpci/av7110.h
+++ b/drivers/media/dvb/ttpci/av7110.h
@@ -46,6 +46,11 @@ extern int av7110_debug;
46 46
47enum {AV_PES_STREAM, PS_STREAM, TS_STREAM, PES_STREAM}; 47enum {AV_PES_STREAM, PS_STREAM, TS_STREAM, PES_STREAM};
48 48
49enum av7110_video_mode {
50 AV7110_VIDEO_MODE_PAL = 0,
51 AV7110_VIDEO_MODE_NTSC = 1
52};
53
49struct av7110_p2t { 54struct av7110_p2t {
50 u8 pes[TS_SIZE]; 55 u8 pes[TS_SIZE];
51 u8 counter; 56 u8 counter;
@@ -170,7 +175,7 @@ struct av7110 {
170 175
171 ca_slot_info_t ci_slot[2]; 176 ca_slot_info_t ci_slot[2];
172 177
173 int vidmode; 178 enum av7110_video_mode vidmode;
174 struct dmxdev dmxdev; 179 struct dmxdev dmxdev;
175 struct dvb_demux demux; 180 struct dvb_demux demux;
176 181
diff --git a/drivers/media/dvb/ttpci/av7110_av.c b/drivers/media/dvb/ttpci/av7110_av.c
index d75e7e48addc..aef6e36d7c5c 100644
--- a/drivers/media/dvb/ttpci/av7110_av.c
+++ b/drivers/media/dvb/ttpci/av7110_av.c
@@ -329,7 +329,7 @@ int av7110_set_volume(struct av7110 *av7110, int volleft, int volright)
329 return 0; 329 return 0;
330} 330}
331 331
332int av7110_set_vidmode(struct av7110 *av7110, int mode) 332int av7110_set_vidmode(struct av7110 *av7110, enum av7110_video_mode mode)
333{ 333{
334 int ret; 334 int ret;
335 dprintk(2, "av7110:%p, \n", av7110); 335 dprintk(2, "av7110:%p, \n", av7110);
@@ -348,11 +348,15 @@ int av7110_set_vidmode(struct av7110 *av7110, int mode)
348} 348}
349 349
350 350
351static int sw2mode[16] = { 351static enum av7110_video_mode sw2mode[16] = {
352 VIDEO_MODE_PAL, VIDEO_MODE_NTSC, VIDEO_MODE_NTSC, VIDEO_MODE_PAL, 352 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
353 VIDEO_MODE_NTSC, VIDEO_MODE_NTSC, VIDEO_MODE_PAL, VIDEO_MODE_NTSC, 353 AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_PAL,
354 VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL, 354 AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_NTSC,
355 VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL, 355 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
356 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
357 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
358 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
359 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
356}; 360};
357 361
358static int get_video_format(struct av7110 *av7110, u8 *buf, int count) 362static int get_video_format(struct av7110 *av7110, u8 *buf, int count)
diff --git a/drivers/media/dvb/ttpci/av7110_av.h b/drivers/media/dvb/ttpci/av7110_av.h
index 45dc144b8b43..5f02ef85e47d 100644
--- a/drivers/media/dvb/ttpci/av7110_av.h
+++ b/drivers/media/dvb/ttpci/av7110_av.h
@@ -3,7 +3,8 @@
3 3
4struct av7110; 4struct av7110;
5 5
6extern int av7110_set_vidmode(struct av7110 *av7110, int mode); 6extern int av7110_set_vidmode(struct av7110 *av7110,
7 enum av7110_video_mode mode);
7 8
8extern int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len); 9extern int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len);
9extern int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen); 10extern int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen);
diff --git a/drivers/media/dvb/ttpci/av7110_v4l.c b/drivers/media/dvb/ttpci/av7110_v4l.c
index 76cca003252f..e2f066fb7967 100644
--- a/drivers/media/dvb/ttpci/av7110_v4l.c
+++ b/drivers/media/dvb/ttpci/av7110_v4l.c
@@ -876,11 +876,11 @@ static int std_callback(struct saa7146_dev* dev, struct saa7146_standard *std)
876 struct av7110 *av7110 = (struct av7110*) dev->ext_priv; 876 struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
877 877
878 if (std->id & V4L2_STD_PAL) { 878 if (std->id & V4L2_STD_PAL) {
879 av7110->vidmode = VIDEO_MODE_PAL; 879 av7110->vidmode = AV7110_VIDEO_MODE_PAL;
880 av7110_set_vidmode(av7110, av7110->vidmode); 880 av7110_set_vidmode(av7110, av7110->vidmode);
881 } 881 }
882 else if (std->id & V4L2_STD_NTSC) { 882 else if (std->id & V4L2_STD_NTSC) {
883 av7110->vidmode = VIDEO_MODE_NTSC; 883 av7110->vidmode = AV7110_VIDEO_MODE_NTSC;
884 av7110_set_vidmode(av7110, av7110->vidmode); 884 av7110_set_vidmode(av7110, av7110->vidmode);
885 } 885 }
886 else 886 else
diff --git a/drivers/media/radio/Kconfig b/drivers/media/radio/Kconfig
index 11e962f1a97f..8d5214f18cf0 100644
--- a/drivers/media/radio/Kconfig
+++ b/drivers/media/radio/Kconfig
@@ -351,4 +351,14 @@ config USB_DSBR
351 To compile this driver as a module, choose M here: the 351 To compile this driver as a module, choose M here: the
352 module will be called dsbr100. 352 module will be called dsbr100.
353 353
354config USB_SI470X
355 tristate "Silicon Labs Si470x FM Radio Receiver support"
356 depends on USB && VIDEO_V4L2
357 ---help---
358 Say Y here if you want to connect this type of radio to your
359 computer's USB port.
360
361 To compile this driver as a module, choose M here: the
362 module will be called radio-silabs.
363
354endif # RADIO_ADAPTERS 364endif # RADIO_ADAPTERS
diff --git a/drivers/media/radio/Makefile b/drivers/media/radio/Makefile
index cf55a18e3ddf..a30159f6fa42 100644
--- a/drivers/media/radio/Makefile
+++ b/drivers/media/radio/Makefile
@@ -21,5 +21,6 @@ obj-$(CONFIG_RADIO_GEMTEK_PCI) += radio-gemtek-pci.o
21obj-$(CONFIG_RADIO_TRUST) += radio-trust.o 21obj-$(CONFIG_RADIO_TRUST) += radio-trust.o
22obj-$(CONFIG_RADIO_MAESTRO) += radio-maestro.o 22obj-$(CONFIG_RADIO_MAESTRO) += radio-maestro.o
23obj-$(CONFIG_USB_DSBR) += dsbr100.o 23obj-$(CONFIG_USB_DSBR) += dsbr100.o
24obj-$(CONFIG_USB_SI470X) += radio-si470x.o
24 25
25EXTRA_CFLAGS += -Isound 26EXTRA_CFLAGS += -Isound
diff --git a/drivers/media/radio/dsbr100.c b/drivers/media/radio/dsbr100.c
index 3bd07f7e3774..36c0e3651502 100644
--- a/drivers/media/radio/dsbr100.c
+++ b/drivers/media/radio/dsbr100.c
@@ -33,6 +33,9 @@
33 33
34 History: 34 History:
35 35
36 Version 0.43:
37 Oliver Neukum: avoided DMA coherency issue
38
36 Version 0.42: 39 Version 0.42:
37 Converted dsbr100 to use video_ioctl2 40 Converted dsbr100 to use video_ioctl2
38 by Douglas Landgraf <dougsland@gmail.com> 41 by Douglas Landgraf <dougsland@gmail.com>
@@ -135,7 +138,7 @@ module_param(radio_nr, int, 0);
135struct dsbr100_device { 138struct dsbr100_device {
136 struct usb_device *usbdev; 139 struct usb_device *usbdev;
137 struct video_device *videodev; 140 struct video_device *videodev;
138 unsigned char transfer_buffer[TB_LEN]; 141 u8 *transfer_buffer;
139 int curfreq; 142 int curfreq;
140 int stereo; 143 int stereo;
141 int users; 144 int users;
@@ -237,10 +240,7 @@ static void dsbr100_getstat(struct dsbr100_device *radio)
237/* handle unplugging of the device, release data structures 240/* handle unplugging of the device, release data structures
238if nothing keeps us from doing it. If something is still 241if nothing keeps us from doing it. If something is still
239keeping us busy, the release callback of v4l will take care 242keeping us busy, the release callback of v4l will take care
240of releasing it. stv680.c does not relase its private 243of releasing it. */
241data, so I don't do this here either. Checking out the
242code I'd expect I better did that, but if there's a memory
243leak here it's tiny (~50 bytes per disconnect) */
244static void usb_dsbr100_disconnect(struct usb_interface *intf) 244static void usb_dsbr100_disconnect(struct usb_interface *intf)
245{ 245{
246 struct dsbr100_device *radio = usb_get_intfdata(intf); 246 struct dsbr100_device *radio = usb_get_intfdata(intf);
@@ -250,6 +250,7 @@ static void usb_dsbr100_disconnect(struct usb_interface *intf)
250 video_unregister_device(radio->videodev); 250 video_unregister_device(radio->videodev);
251 radio->videodev = NULL; 251 radio->videodev = NULL;
252 if (radio->users) { 252 if (radio->users) {
253 kfree(radio->transfer_buffer);
253 kfree(radio); 254 kfree(radio);
254 } else { 255 } else {
255 radio->removed = 1; 256 radio->removed = 1;
@@ -425,6 +426,7 @@ static int usb_dsbr100_close(struct inode *inode, struct file *file)
425 return -ENODEV; 426 return -ENODEV;
426 radio->users = 0; 427 radio->users = 0;
427 if (radio->removed) { 428 if (radio->removed) {
429 kfree(radio->transfer_buffer);
428 kfree(radio); 430 kfree(radio);
429 } 431 }
430 return 0; 432 return 0;
@@ -471,7 +473,12 @@ static int usb_dsbr100_probe(struct usb_interface *intf,
471 473
472 if (!(radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL))) 474 if (!(radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL)))
473 return -ENOMEM; 475 return -ENOMEM;
476 if (!(radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL))) {
477 kfree(radio);
478 return -ENOMEM;
479 }
474 if (!(radio->videodev = video_device_alloc())) { 480 if (!(radio->videodev = video_device_alloc())) {
481 kfree(radio->transfer_buffer);
475 kfree(radio); 482 kfree(radio);
476 return -ENOMEM; 483 return -ENOMEM;
477 } 484 }
@@ -485,6 +492,7 @@ static int usb_dsbr100_probe(struct usb_interface *intf,
485 if (video_register_device(radio->videodev, VFL_TYPE_RADIO,radio_nr)) { 492 if (video_register_device(radio->videodev, VFL_TYPE_RADIO,radio_nr)) {
486 warn("Could not register video device"); 493 warn("Could not register video device");
487 video_device_release(radio->videodev); 494 video_device_release(radio->videodev);
495 kfree(radio->transfer_buffer);
488 kfree(radio); 496 kfree(radio);
489 return -EIO; 497 return -EIO;
490 } 498 }
diff --git a/drivers/media/radio/radio-gemtek.c b/drivers/media/radio/radio-gemtek.c
index 5e4b9ddb23c0..246422b49267 100644
--- a/drivers/media/radio/radio-gemtek.c
+++ b/drivers/media/radio/radio-gemtek.c
@@ -58,10 +58,10 @@ static int initmute = 1;
58static int radio_nr = -1; 58static int radio_nr = -1;
59 59
60module_param(io, int, 0444); 60module_param(io, int, 0444);
61MODULE_PARM_DESC(io, "Force I/O port for the GemTek Radio card if automatic" 61MODULE_PARM_DESC(io, "Force I/O port for the GemTek Radio card if automatic "
62 "probing is disabled or fails. The most common I/O ports are: 0x20c " 62 "probing is disabled or fails. The most common I/O ports are: 0x20c "
63 "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to " 63 "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to "
64 " work for the combined sound/radiocard)."); 64 "work for the combined sound/radiocard).");
65 65
66module_param(probe, bool, 0444); 66module_param(probe, bool, 0444);
67MODULE_PARM_DESC(probe, "Enable automatic device probing. Note: only the most " 67MODULE_PARM_DESC(probe, "Enable automatic device probing. Note: only the most "
@@ -392,7 +392,7 @@ static struct v4l2_queryctrl radio_qctrl[] = {
392 } 392 }
393}; 393};
394 394
395static struct file_operations gemtek_fops = { 395static const struct file_operations gemtek_fops = {
396 .owner = THIS_MODULE, 396 .owner = THIS_MODULE,
397 .open = video_exclusive_open, 397 .open = video_exclusive_open,
398 .release = video_exclusive_release, 398 .release = video_exclusive_release,
diff --git a/drivers/media/radio/radio-maestro.c b/drivers/media/radio/radio-maestro.c
index 8e33a19a22a3..bc51f4d23a5a 100644
--- a/drivers/media/radio/radio-maestro.c
+++ b/drivers/media/radio/radio-maestro.c
@@ -423,7 +423,7 @@ static int __devinit maestro_probe(struct pci_dev *pdev,
423errunr: 423errunr:
424 video_unregister_device(maestro_radio_inst); 424 video_unregister_device(maestro_radio_inst);
425errfr1: 425errfr1:
426 kfree(maestro_radio_inst); 426 video_device_release(maestro_radio_inst);
427errfr: 427errfr:
428 kfree(radio_unit); 428 kfree(radio_unit);
429err: 429err:
diff --git a/drivers/media/radio/radio-sf16fmi.c b/drivers/media/radio/radio-sf16fmi.c
index 395165367f37..3118bdab3183 100644
--- a/drivers/media/radio/radio-sf16fmi.c
+++ b/drivers/media/radio/radio-sf16fmi.c
@@ -321,7 +321,7 @@ static struct isapnp_device_id id_table[] __devinitdata = {
321 321
322MODULE_DEVICE_TABLE(isapnp, id_table); 322MODULE_DEVICE_TABLE(isapnp, id_table);
323 323
324static int isapnp_fmi_probe(void) 324static int __init isapnp_fmi_probe(void)
325{ 325{
326 int i = 0; 326 int i = 0;
327 327
diff --git a/drivers/media/radio/radio-sf16fmr2.c b/drivers/media/radio/radio-sf16fmr2.c
index c432c44bd634..f7c8b000404f 100644
--- a/drivers/media/radio/radio-sf16fmr2.c
+++ b/drivers/media/radio/radio-sf16fmr2.c
@@ -476,8 +476,7 @@ static int __init fmr2_init(void)
476 return -EBUSY; 476 return -EBUSY;
477 } 477 }
478 478
479 if(video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr)==-1) 479 if (video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
480 {
481 release_region(io, 2); 480 release_region(io, 2);
482 return -EINVAL; 481 return -EINVAL;
483 } 482 }
diff --git a/drivers/media/radio/radio-si470x.c b/drivers/media/radio/radio-si470x.c
new file mode 100644
index 000000000000..8e4bd4769048
--- /dev/null
+++ b/drivers/media/radio/radio-si470x.c
@@ -0,0 +1,1432 @@
1/*
2 * drivers/media/radio/radio-si470x.c
3 *
4 * Driver for USB radios for the Silicon Labs Si470x FM Radio Receivers:
5 * - Silicon Labs USB FM Radio Reference Design
6 * - ADS/Tech FM Radio Receiver (formerly Instant FM Music) (RDX-155-EF)
7 *
8 * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
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
26/*
27 * History:
28 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
29 * Version 1.0.0
30 * - First working version
31 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
32 * Version 1.0.1
33 * - Improved error handling, every function now returns errno
34 * - Improved multi user access (start/mute/stop)
35 * - Channel doesn't get lost anymore after start/mute/stop
36 * - RDS support added (polling mode via interrupt EP 1)
37 * - marked default module parameters with *value*
38 * - switched from bit structs to bit masks
39 * - header file cleaned and integrated
40 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
41 * Version 1.0.2
42 * - hex values are now lower case
43 * - commented USB ID for ADS/Tech moved on todo list
44 * - blacklisted si470x in hid-quirks.c
45 * - rds buffer handling functions integrated into *_work, *_read
46 * - rds_command in si470x_poll exchanged against simple retval
47 * - check for firmware version 15
48 * - code order and prototypes still remain the same
49 * - spacing and bottom of band codes remain the same
50 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
51 * Version 1.0.3
52 * - code reordered to avoid function prototypes
53 * - switch/case defaults are now more user-friendly
54 * - unified comment style
55 * - applied all checkpatch.pl v1.12 suggestions
56 * except the warning about the too long lines with bit comments
57 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
58 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
59 * Version 1.0.4
60 * - avoid poss. locking when doing copy_to_user which may sleep
61 * - RDS is automatically activated on read now
62 * - code cleaned of unnecessary rds_commands
63 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
64 * (thanks to Guillaume RAMOUSSE)
65 *
66 * ToDo:
67 * - add seeking support
68 * - add firmware download/update support
69 * - RDS support: interrupt mode, instead of polling
70 * - add LED status output (check if that's not already done in firmware)
71 */
72
73
74/* driver definitions */
75#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
76#define DRIVER_NAME "radio-si470x"
77#define DRIVER_VERSION KERNEL_VERSION(1, 0, 4)
78#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
79#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
80
81
82/* kernel includes */
83#include <linux/kernel.h>
84#include <linux/module.h>
85#include <linux/init.h>
86#include <linux/slab.h>
87#include <linux/input.h>
88#include <linux/usb.h>
89#include <linux/hid.h>
90#include <linux/version.h>
91#include <linux/videodev2.h>
92#include <media/v4l2-common.h>
93#include <media/rds.h>
94
95
96/* USB Device ID List */
97static struct usb_device_id si470x_usb_driver_id_table[] = {
98 /* Silicon Labs USB FM Radio Reference Design */
99 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
100 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
101 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
102 /* Terminating entry */
103 { }
104};
105MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
106
107
108
109/**************************************************************************
110 * Module Parameters
111 **************************************************************************/
112
113/* Radio Nr */
114static int radio_nr = -1;
115module_param(radio_nr, int, 0);
116MODULE_PARM_DESC(radio_nr, "Radio Nr");
117
118/* Spacing (kHz) */
119/* 0: 200 kHz (USA, Australia) */
120/* 1: 100 kHz (Europe, Japan) */
121/* 2: 50 kHz */
122static int space = 2;
123module_param(space, int, 0);
124MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
125
126/* Bottom of Band (MHz) */
127/* 0: 87.5 - 108 MHz (USA, Europe)*/
128/* 1: 76 - 108 MHz (Japan wide band) */
129/* 2: 76 - 90 MHz (Japan) */
130static int band = 1;
131module_param(band, int, 0);
132MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
133
134/* De-emphasis */
135/* 0: 75 us (USA) */
136/* 1: 50 us (Europe, Australia, Japan) */
137static int de = 1;
138module_param(de, int, 0);
139MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
140
141/* USB timeout */
142static int usb_timeout = 500;
143module_param(usb_timeout, int, 0);
144MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
145
146/* Seek retries */
147static int seek_retries = 100;
148module_param(seek_retries, int, 0);
149MODULE_PARM_DESC(seek_retries, "Seek retries: *100*");
150
151/* RDS buffer blocks */
152static int rds_buf = 100;
153module_param(rds_buf, int, 0);
154MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
155
156/* RDS maximum block errors */
157static int max_rds_errors = 1;
158/* 0 means 0 errors requiring correction */
159/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
160/* 2 means 3-5 errors requiring correction */
161/* 3 means 6+ errors or errors in checkword, correction not possible */
162module_param(max_rds_errors, int, 0);
163MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
164
165/* RDS poll frequency */
166static int rds_poll_time = 40;
167/* 40 is used by the original USBRadio.exe */
168/* 50 is used by radio-cadet */
169/* 75 should be okay */
170/* 80 is the usual RDS receive interval */
171module_param(rds_poll_time, int, 0);
172MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
173
174
175
176/**************************************************************************
177 * Register Definitions
178 **************************************************************************/
179#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
180#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
181#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
182
183#define DEVICEID 0 /* Device ID */
184#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
185#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
186
187#define CHIPID 1 /* Chip ID */
188#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
189#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
190#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
191
192#define POWERCFG 2 /* Power Configuration */
193#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
194#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
195#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
196#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
197#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
198#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
199#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
200#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
201#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
202
203#define CHANNEL 3 /* Channel */
204#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
205#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
206
207#define SYSCONFIG1 4 /* System Configuration 1 */
208#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
209#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
210#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
211#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
212#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
213#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
214#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
215#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
216#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
217
218#define SYSCONFIG2 5 /* System Configuration 2 */
219#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
220#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
221#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
222#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
223
224#define SYSCONFIG3 6 /* System Configuration 3 */
225#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
226#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
227#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
228#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
229
230#define TEST1 7 /* Test 1 */
231#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
232
233#define TEST2 8 /* Test 2 */
234/* TEST2 only contains reserved bits */
235
236#define BOOTCONFIG 9 /* Boot Configuration */
237/* BOOTCONFIG only contains reserved bits */
238
239#define STATUSRSSI 10 /* Status RSSI */
240#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
241#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
242#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
243#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
244#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
245#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
246#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
247#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
248
249#define READCHAN 11 /* Read Channel */
250#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
251#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
252#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
253#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
254
255#define RDSA 12 /* RDSA */
256#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
257
258#define RDSB 13 /* RDSB */
259#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
260
261#define RDSC 14 /* RDSC */
262#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
263
264#define RDSD 15 /* RDSD */
265#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
266
267
268
269/**************************************************************************
270 * USB HID Reports
271 **************************************************************************/
272
273/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
274/* with the (REPORT_ID - 1) corresponding to the register address across USB */
275/* endpoint 0 using GET_REPORT and SET_REPORT */
276#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
277#define REGISTER_REPORT(reg) ((reg) + 1)
278
279/* Report 17 gives direct read/write access to the entire Si470x register */
280/* map across endpoint 0 using GET_REPORT and SET_REPORT */
281#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
282#define ENTIRE_REPORT 17
283
284/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
285/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
286#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
287#define RDS_REPORT 18
288
289/* Report 19: LED state */
290#define LED_REPORT_SIZE 3
291#define LED_REPORT 19
292
293/* Report 19: stream */
294#define STREAM_REPORT_SIZE 3
295#define STREAM_REPORT 19
296
297/* Report 20: scratch */
298#define SCRATCH_PAGE_SIZE 63
299#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
300#define SCRATCH_REPORT 20
301
302/* Reports 19-22: flash upgrade of the C8051F321 */
303#define WRITE_REPORT 19
304#define FLASH_REPORT 20
305#define CRC_REPORT 21
306#define RESPONSE_REPORT 22
307
308/* Report 23: currently unused, but can accept 60 byte reports on the HID */
309/* interrupt out endpoint 2 every 1 millisecond */
310#define UNUSED_REPORT 23
311
312
313
314/**************************************************************************
315 * Software/Hardware Versions
316 **************************************************************************/
317#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
318#define RADIO_SW_VERSION 7
319#define RADIO_SW_VERSION_CURRENT 15
320#define RADIO_HW_VERSION 1
321
322#define SCRATCH_PAGE_SW_VERSION 1
323#define SCRATCH_PAGE_HW_VERSION 2
324
325
326
327/**************************************************************************
328 * LED State Definitions
329 **************************************************************************/
330#define LED_COMMAND 0x35
331
332#define NO_CHANGE_LED 0x00
333#define ALL_COLOR_LED 0x01 /* streaming state */
334#define BLINK_GREEN_LED 0x02 /* connect state */
335#define BLINK_RED_LED 0x04
336#define BLINK_ORANGE_LED 0x10 /* disconnect state */
337#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
338#define SOLID_RED_LED 0x40 /* bootload state */
339#define SOLID_ORANGE_LED 0x80
340
341
342
343/**************************************************************************
344 * Stream State Definitions
345 **************************************************************************/
346#define STREAM_COMMAND 0x36
347#define STREAM_VIDPID 0x00
348#define STREAM_AUDIO 0xff
349
350
351
352/**************************************************************************
353 * Bootloader / Flash Commands
354 **************************************************************************/
355
356/* unique id sent to bootloader and required to put into a bootload state */
357#define UNIQUE_BL_ID 0x34
358
359/* mask for the flash data */
360#define FLASH_DATA_MASK 0x55
361
362/* bootloader commands */
363#define GET_SW_VERSION_COMMAND 0x00
364#define SET_PAGE_COMMAND 0x01
365#define ERASE_PAGE_COMMAND 0x02
366#define WRITE_PAGE_COMMAND 0x03
367#define CRC_ON_PAGE_COMMAND 0x04
368#define READ_FLASH_BYTE_COMMAND 0x05
369#define RESET_DEVICE_COMMAND 0x06
370#define GET_HW_VERSION_COMMAND 0x07
371#define BLANK 0xff
372
373/* bootloader command responses */
374#define COMMAND_OK 0x01
375#define COMMAND_FAILED 0x02
376#define COMMAND_PENDING 0x03
377
378/* buffer sizes */
379#define COMMAND_BUFFER_SIZE 4
380#define RESPONSE_BUFFER_SIZE 2
381#define FLASH_BUFFER_SIZE 64
382#define CRC_BUFFER_SIZE 3
383
384
385
386/**************************************************************************
387 * General Driver Definitions
388 **************************************************************************/
389
390/*
391 * si470x_device - private data
392 */
393struct si470x_device {
394 /* reference to USB and video device */
395 struct usb_device *usbdev;
396 struct video_device *videodev;
397
398 /* are these really necessary ? */
399 int users;
400
401 /* report buffer (maximum 64 bytes) */
402 unsigned char buf[64];
403
404 /* Silabs internal registers (0..15) */
405 unsigned short registers[RADIO_REGISTER_NUM];
406
407 /* RDS receive buffer */
408 struct work_struct work;
409 wait_queue_head_t read_queue;
410 struct timer_list timer;
411 spinlock_t lock; /* buffer locking */
412 unsigned char *buffer; /* size is always multiple of three */
413 unsigned int buf_size;
414 unsigned int rd_index;
415 unsigned int wr_index;
416};
417
418
419/*
420 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
421 * 62.5 kHz otherwise.
422 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
423 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
424 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
425 */
426#define FREQ_MUL (1000000 / 62.5)
427
428
429
430/**************************************************************************
431 * General Driver Functions
432 **************************************************************************/
433
434/*
435 * si470x_get_report - receive a HID report
436 */
437static int si470x_get_report(struct si470x_device *radio, int size)
438{
439 return usb_control_msg(radio->usbdev,
440 usb_rcvctrlpipe(radio->usbdev, 0),
441 HID_REQ_GET_REPORT,
442 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
443 radio->buf[0], 2,
444 radio->buf, size, usb_timeout);
445}
446
447
448/*
449 * si470x_set_report - send a HID report
450 */
451static int si470x_set_report(struct si470x_device *radio, int size)
452{
453 return usb_control_msg(radio->usbdev,
454 usb_sndctrlpipe(radio->usbdev, 0),
455 HID_REQ_SET_REPORT,
456 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
457 radio->buf[0], 2,
458 radio->buf, size, usb_timeout);
459}
460
461
462/*
463 * si470x_get_register - read register
464 */
465static int si470x_get_register(struct si470x_device *radio, int regnr)
466{
467 int retval;
468
469 radio->buf[0] = REGISTER_REPORT(regnr);
470
471 retval = si470x_get_report(radio, REGISTER_REPORT_SIZE);
472 if (retval >= 0)
473 radio->registers[regnr] = (radio->buf[1] << 8) | radio->buf[2];
474
475 return (retval < 0) ? -EINVAL : 0;
476}
477
478
479/*
480 * si470x_set_register - write register
481 */
482static int si470x_set_register(struct si470x_device *radio, int regnr)
483{
484 int retval;
485
486 radio->buf[0] = REGISTER_REPORT(regnr);
487 radio->buf[1] = (radio->registers[regnr] & 0xff00) >> 8;
488 radio->buf[2] = (radio->registers[regnr] & 0x00ff);
489
490 retval = si470x_set_report(radio, REGISTER_REPORT_SIZE);
491
492 return (retval < 0) ? -EINVAL : 0;
493}
494
495
496/*
497 * si470x_get_all_registers - read entire registers
498 */
499static int si470x_get_all_registers(struct si470x_device *radio)
500{
501 int retval;
502 int regnr;
503
504 radio->buf[0] = ENTIRE_REPORT;
505
506 retval = si470x_get_report(radio, ENTIRE_REPORT_SIZE);
507
508 if (retval >= 0)
509 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
510 radio->registers[regnr] =
511 (radio->buf[regnr * RADIO_REGISTER_SIZE + 1] << 8) |
512 radio->buf[regnr * RADIO_REGISTER_SIZE + 2];
513
514 return (retval < 0) ? -EINVAL : 0;
515}
516
517
518/*
519 * si470x_get_rds_registers - read rds registers
520 */
521static int si470x_get_rds_registers(struct si470x_device *radio)
522{
523 int retval;
524 int regnr;
525 int size;
526
527 radio->buf[0] = RDS_REPORT;
528
529 retval = usb_interrupt_msg(radio->usbdev,
530 usb_rcvctrlpipe(radio->usbdev, 1),
531 radio->buf, RDS_REPORT_SIZE, &size, usb_timeout);
532
533 if (retval >= 0)
534 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
535 radio->registers[STATUSRSSI + regnr] =
536 (radio->buf[regnr * RADIO_REGISTER_SIZE + 1] << 8) |
537 radio->buf[regnr * RADIO_REGISTER_SIZE + 2];
538
539 return (retval < 0) ? -EINVAL : 0;
540}
541
542
543/*
544 * si470x_set_chan - set the channel
545 */
546static int si470x_set_chan(struct si470x_device *radio, int chan)
547{
548 int retval, i;
549
550 /* start tuning */
551 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
552 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
553 retval = si470x_set_register(radio, CHANNEL);
554 if (retval < 0)
555 return retval;
556
557 /* wait till seek operation has completed */
558 i = 0;
559 do {
560 retval = si470x_get_register(radio, STATUSRSSI);
561 if (retval < 0)
562 return retval;
563 } while ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) &&
564 (++i < seek_retries));
565 if (i >= seek_retries)
566 printk(KERN_WARNING DRIVER_NAME
567 ": seek does not finish after %d tries\n", i);
568
569 /* stop tuning */
570 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
571 return si470x_set_register(radio, CHANNEL);
572}
573
574
575/*
576 * si470x_get_freq - get the frequency
577 */
578static int si470x_get_freq(struct si470x_device *radio)
579{
580 int spacing, band_bottom, chan, freq;
581 int retval;
582
583 /* Spacing (kHz) */
584 switch (space) {
585 /* 0: 200 kHz (USA, Australia) */
586 case 0 : spacing = 0.200 * FREQ_MUL; break;
587 /* 1: 100 kHz (Europe, Japan) */
588 case 1 : spacing = 0.100 * FREQ_MUL; break;
589 /* 2: 50 kHz */
590 default: spacing = 0.050 * FREQ_MUL; break;
591 };
592
593 /* Bottom of Band (MHz) */
594 switch (band) {
595 /* 0: 87.5 - 108 MHz (USA, Europe) */
596 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
597 /* 1: 76 - 108 MHz (Japan wide band) */
598 default: band_bottom = 76 * FREQ_MUL; break;
599 /* 2: 76 - 90 MHz (Japan) */
600 case 2 : band_bottom = 76 * FREQ_MUL; break;
601 };
602
603 /* read channel */
604 retval = si470x_get_register(radio, READCHAN);
605 if (retval < 0)
606 return retval;
607 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
608
609 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
610 freq = chan * spacing + band_bottom;
611
612 return freq;
613}
614
615
616/*
617 * si470x_set_freq - set the frequency
618 */
619static int si470x_set_freq(struct si470x_device *radio, int freq)
620{
621 int spacing, band_bottom, chan;
622
623 /* Spacing (kHz) */
624 switch (space) {
625 /* 0: 200 kHz (USA, Australia) */
626 case 0 : spacing = 0.200 * FREQ_MUL; break;
627 /* 1: 100 kHz (Europe, Japan) */
628 case 1 : spacing = 0.100 * FREQ_MUL; break;
629 /* 2: 50 kHz */
630 default: spacing = 0.050 * FREQ_MUL; break;
631 };
632
633 /* Bottom of Band (MHz) */
634 switch (band) {
635 /* 0: 87.5 - 108 MHz (USA, Europe) */
636 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
637 /* 1: 76 - 108 MHz (Japan wide band) */
638 default: band_bottom = 76 * FREQ_MUL; break;
639 /* 2: 76 - 90 MHz (Japan) */
640 case 2 : band_bottom = 76 * FREQ_MUL; break;
641 };
642
643 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
644 chan = (freq - band_bottom) / spacing;
645
646 return si470x_set_chan(radio, chan);
647}
648
649
650/*
651 * si470x_start - switch on radio
652 */
653static int si470x_start(struct si470x_device *radio)
654{
655 int retval;
656
657 /* powercfg */
658 radio->registers[POWERCFG] =
659 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
660 retval = si470x_set_register(radio, POWERCFG);
661 if (retval < 0)
662 return retval;
663
664 /* sysconfig 1 */
665 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
666 retval = si470x_set_register(radio, SYSCONFIG1);
667 if (retval < 0)
668 return retval;
669
670 /* sysconfig 2 */
671 radio->registers[SYSCONFIG2] =
672 (0x3f << 8) | /* SEEKTH */
673 (band << 6) | /* BAND */
674 (space << 4) | /* SPACE */
675 15; /* VOLUME (max) */
676 retval = si470x_set_register(radio, SYSCONFIG2);
677 if (retval < 0)
678 return retval;
679
680 /* reset last channel */
681 return si470x_set_chan(radio,
682 radio->registers[CHANNEL] & CHANNEL_CHAN);
683}
684
685
686/*
687 * si470x_stop - switch off radio
688 */
689static int si470x_stop(struct si470x_device *radio)
690{
691 int retval;
692
693 /* sysconfig 1 */
694 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
695 retval = si470x_set_register(radio, SYSCONFIG1);
696 if (retval < 0)
697 return retval;
698
699 /* powercfg */
700 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
701 /* POWERCFG_ENABLE has to automatically go low */
702 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
703 return si470x_set_register(radio, POWERCFG);
704}
705
706
707/*
708 * si470x_rds_on - switch on rds reception
709 */
710static int si470x_rds_on(struct si470x_device *radio)
711{
712 /* sysconfig 1 */
713 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
714 return si470x_set_register(radio, SYSCONFIG1);
715}
716
717
718
719/**************************************************************************
720 * RDS Driver Functions
721 **************************************************************************/
722
723/*
724 * si470x_rds - rds processing function
725 */
726static void si470x_rds(struct si470x_device *radio)
727{
728 unsigned char tmpbuf[3];
729 unsigned char blocknum;
730 unsigned char bler; /* rds block errors */
731 unsigned short rds;
732 unsigned int i;
733
734 /* get rds blocks */
735 if (si470x_get_rds_registers(radio) < 0)
736 return;
737 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
738 /* No RDS group ready */
739 return;
740 }
741 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
742 /* RDS decoder not synchronized */
743 return;
744 }
745
746 /* copy four RDS blocks to internal buffer */
747 if (spin_trylock(&radio->lock)) {
748 /* process each rds block */
749 for (blocknum = 0; blocknum < 4; blocknum++) {
750 switch (blocknum) {
751 default:
752 bler = (radio->registers[STATUSRSSI] &
753 STATUSRSSI_BLERA) >> 9;
754 rds = radio->registers[RDSA];
755 break;
756 case 1:
757 bler = (radio->registers[READCHAN] &
758 READCHAN_BLERB) >> 14;
759 rds = radio->registers[RDSB];
760 break;
761 case 2:
762 bler = (radio->registers[READCHAN] &
763 READCHAN_BLERC) >> 12;
764 rds = radio->registers[RDSC];
765 break;
766 case 3:
767 bler = (radio->registers[READCHAN] &
768 READCHAN_BLERD) >> 10;
769 rds = radio->registers[RDSD];
770 break;
771 };
772
773 /* Fill the V4L2 RDS buffer */
774 tmpbuf[0] = rds & 0x00ff; /* LSB */
775 tmpbuf[1] = (rds & 0xff00) >> 8;/* MSB */
776 tmpbuf[2] = blocknum; /* offset name */
777 tmpbuf[2] |= blocknum << 3; /* received offset */
778 if (bler > max_rds_errors)
779 tmpbuf[2] |= 0x80; /* uncorrectable errors */
780 else if (bler > 0)
781 tmpbuf[2] |= 0x40; /* corrected error(s) */
782
783 /* copy RDS block to internal buffer */
784 for (i = 0; i < 3; i++) {
785 radio->buffer[radio->wr_index] = tmpbuf[i];
786 radio->wr_index++;
787 }
788
789 /* wrap write pointer */
790 if (radio->wr_index >= radio->buf_size)
791 radio->wr_index = 0;
792
793 /* check for overflow */
794 if (radio->wr_index == radio->rd_index) {
795 /* increment and wrap read pointer */
796 radio->rd_index += 3;
797 if (radio->rd_index >= radio->buf_size)
798 radio->rd_index = 0;
799 }
800 }
801 spin_unlock(&radio->lock);
802 }
803
804 /* wake up read queue */
805 if (radio->wr_index != radio->rd_index)
806 wake_up_interruptible(&radio->read_queue);
807}
808
809
810/*
811 * si470x_timer - rds timer function
812 */
813static void si470x_timer(unsigned long data)
814{
815 struct si470x_device *radio = (struct si470x_device *) data;
816
817 schedule_work(&radio->work);
818}
819
820
821/*
822 * si470x_work - rds work function
823 */
824static void si470x_work(struct work_struct *work)
825{
826 struct si470x_device *radio = container_of(work, struct si470x_device,
827 work);
828
829 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
830 return;
831
832 si470x_rds(radio);
833 mod_timer(&radio->timer, jiffies + msecs_to_jiffies(rds_poll_time));
834}
835
836
837
838/**************************************************************************
839 * File Operations Interface
840 **************************************************************************/
841
842/*
843 * si470x_fops_read - read RDS data
844 */
845static ssize_t si470x_fops_read(struct file *file, char __user *buf,
846 size_t count, loff_t *ppos)
847{
848 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
849 int retval = 0;
850 unsigned int block_count = 0;
851
852 /* switch on rds reception */
853 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
854 si470x_rds_on(radio);
855 schedule_work(&radio->work);
856 }
857
858 /* block if no new data available */
859 while (radio->wr_index == radio->rd_index) {
860 if (file->f_flags & O_NONBLOCK)
861 return -EWOULDBLOCK;
862 interruptible_sleep_on(&radio->read_queue);
863 }
864
865 /* calculate block count from byte count */
866 count /= 3;
867
868 /* copy RDS block out of internal buffer and to user buffer */
869 if (spin_trylock(&radio->lock)) {
870 while (block_count < count) {
871 if (radio->rd_index == radio->wr_index)
872 break;
873
874 /* always transfer rds complete blocks */
875 if (copy_to_user(buf,
876 &radio->buffer[radio->rd_index], 3))
877 /* retval = -EFAULT; */
878 break;
879
880 /* increment and wrap read pointer */
881 radio->rd_index += 3;
882 if (radio->rd_index >= radio->buf_size)
883 radio->rd_index = 0;
884
885 /* increment counters */
886 block_count++;
887 buf += 3;
888 retval += 3;
889 }
890
891 spin_unlock(&radio->lock);
892 }
893
894 return retval;
895}
896
897
898/*
899 * si470x_fops_poll - poll RDS data
900 */
901static unsigned int si470x_fops_poll(struct file *file,
902 struct poll_table_struct *pts)
903{
904 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
905
906 /* switch on rds reception */
907 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
908 si470x_rds_on(radio);
909 schedule_work(&radio->work);
910 }
911
912 poll_wait(file, &radio->read_queue, pts);
913
914 if (radio->rd_index != radio->wr_index)
915 return POLLIN | POLLRDNORM;
916
917 return 0;
918}
919
920
921/*
922 * si470x_fops_open - file open
923 */
924static int si470x_fops_open(struct inode *inode, struct file *file)
925{
926 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
927
928 radio->users++;
929 if (radio->users == 1)
930 return si470x_start(radio);
931
932 return 0;
933}
934
935
936/*
937 * si470x_fops_release - file release
938 */
939static int si470x_fops_release(struct inode *inode, struct file *file)
940{
941 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
942
943 if (!radio)
944 return -ENODEV;
945
946 radio->users--;
947 if (radio->users == 0) {
948 /* stop rds reception */
949 del_timer_sync(&radio->timer);
950 flush_scheduled_work();
951
952 /* cancel read processes */
953 wake_up_interruptible(&radio->read_queue);
954
955 return si470x_stop(radio);
956 }
957
958 return 0;
959}
960
961
962/*
963 * si470x_fops - file operations interface
964 */
965static const struct file_operations si470x_fops = {
966 .owner = THIS_MODULE,
967 .llseek = no_llseek,
968 .read = si470x_fops_read,
969 .poll = si470x_fops_poll,
970 .ioctl = video_ioctl2,
971 .compat_ioctl = v4l_compat_ioctl32,
972 .open = si470x_fops_open,
973 .release = si470x_fops_release,
974};
975
976
977
978/**************************************************************************
979 * Video4Linux Interface
980 **************************************************************************/
981
982/*
983 * si470x_v4l2_queryctrl - query control
984 */
985static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
986/* HINT: the disabled controls are only here to satify kradio and such apps */
987 {
988 .id = V4L2_CID_AUDIO_VOLUME,
989 .type = V4L2_CTRL_TYPE_INTEGER,
990 .name = "Volume",
991 .minimum = 0,
992 .maximum = 15,
993 .step = 1,
994 .default_value = 15,
995 },
996 {
997 .id = V4L2_CID_AUDIO_BALANCE,
998 .flags = V4L2_CTRL_FLAG_DISABLED,
999 },
1000 {
1001 .id = V4L2_CID_AUDIO_BASS,
1002 .flags = V4L2_CTRL_FLAG_DISABLED,
1003 },
1004 {
1005 .id = V4L2_CID_AUDIO_TREBLE,
1006 .flags = V4L2_CTRL_FLAG_DISABLED,
1007 },
1008 {
1009 .id = V4L2_CID_AUDIO_MUTE,
1010 .type = V4L2_CTRL_TYPE_BOOLEAN,
1011 .name = "Mute",
1012 .minimum = 0,
1013 .maximum = 1,
1014 .step = 1,
1015 .default_value = 1,
1016 },
1017 {
1018 .id = V4L2_CID_AUDIO_LOUDNESS,
1019 .flags = V4L2_CTRL_FLAG_DISABLED,
1020 },
1021};
1022
1023
1024/*
1025 * si470x_vidioc_querycap - query device capabilities
1026 */
1027static int si470x_vidioc_querycap(struct file *file, void *priv,
1028 struct v4l2_capability *capability)
1029{
1030 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1031 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1032 sprintf(capability->bus_info, "USB");
1033 capability->version = DRIVER_VERSION;
1034 capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1035
1036 return 0;
1037}
1038
1039
1040/*
1041 * si470x_vidioc_g_input - get input
1042 */
1043static int si470x_vidioc_g_input(struct file *filp, void *priv,
1044 unsigned int *i)
1045{
1046 *i = 0;
1047
1048 return 0;
1049}
1050
1051
1052/*
1053 * si470x_vidioc_s_input - set input
1054 */
1055static int si470x_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1056{
1057 if (i != 0)
1058 return -EINVAL;
1059
1060 return 0;
1061}
1062
1063
1064/*
1065 * si470x_vidioc_queryctrl - enumerate control items
1066 */
1067static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1068 struct v4l2_queryctrl *qc)
1069{
1070 int i;
1071
1072 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1073 if (qc->id && qc->id == si470x_v4l2_queryctrl[i].id) {
1074 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1075 return 0;
1076 }
1077 }
1078
1079 return -EINVAL;
1080}
1081
1082
1083/*
1084 * si470x_vidioc_g_ctrl - get the value of a control
1085 */
1086static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1087 struct v4l2_control *ctrl)
1088{
1089 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1090
1091 switch (ctrl->id) {
1092 case V4L2_CID_AUDIO_VOLUME:
1093 ctrl->value = radio->registers[SYSCONFIG2] &
1094 SYSCONFIG2_VOLUME;
1095 break;
1096 case V4L2_CID_AUDIO_MUTE:
1097 ctrl->value = ((radio->registers[POWERCFG] &
1098 POWERCFG_DMUTE) == 0) ? 1 : 0;
1099 break;
1100 }
1101
1102 return 0;
1103}
1104
1105
1106/*
1107 * si470x_vidioc_s_ctrl - set the value of a control
1108 */
1109static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1110 struct v4l2_control *ctrl)
1111{
1112 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1113
1114 switch (ctrl->id) {
1115 case V4L2_CID_AUDIO_VOLUME:
1116 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1117 radio->registers[SYSCONFIG2] |= ctrl->value;
1118 return si470x_set_register(radio, SYSCONFIG2);
1119 case V4L2_CID_AUDIO_MUTE:
1120 if (ctrl->value == 1)
1121 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1122 else
1123 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1124 return si470x_set_register(radio, POWERCFG);
1125 }
1126
1127 return -EINVAL;
1128}
1129
1130
1131/*
1132 * si470x_vidioc_g_audio - get audio attributes
1133 */
1134static int si470x_vidioc_g_audio(struct file *file, void *priv,
1135 struct v4l2_audio *audio)
1136{
1137 if (audio->index > 1)
1138 return -EINVAL;
1139
1140 strcpy(audio->name, "Radio");
1141 audio->capability = V4L2_AUDCAP_STEREO;
1142
1143 return 0;
1144}
1145
1146
1147/*
1148 * si470x_vidioc_s_audio - set audio attributes
1149 */
1150static int si470x_vidioc_s_audio(struct file *file, void *priv,
1151 struct v4l2_audio *audio)
1152{
1153 if (audio->index != 0)
1154 return -EINVAL;
1155
1156 return 0;
1157}
1158
1159
1160/*
1161 * si470x_vidioc_g_tuner - get tuner attributes
1162 */
1163static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1164 struct v4l2_tuner *tuner)
1165{
1166 int retval;
1167 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1168
1169 if (tuner->index > 0)
1170 return -EINVAL;
1171
1172 /* read status rssi */
1173 retval = si470x_get_register(radio, STATUSRSSI);
1174 if (retval < 0)
1175 return retval;
1176
1177 strcpy(tuner->name, "FM");
1178 tuner->type = V4L2_TUNER_RADIO;
1179 switch (band) {
1180 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1181 default:
1182 tuner->rangelow = 87.5 * FREQ_MUL;
1183 tuner->rangehigh = 108 * FREQ_MUL;
1184 break;
1185 /* 1: 76 - 108 MHz (Japan wide band) */
1186 case 1 :
1187 tuner->rangelow = 76 * FREQ_MUL;
1188 tuner->rangehigh = 108 * FREQ_MUL;
1189 break;
1190 /* 2: 76 - 90 MHz (Japan) */
1191 case 2 :
1192 tuner->rangelow = 76 * FREQ_MUL;
1193 tuner->rangehigh = 90 * FREQ_MUL;
1194 break;
1195 };
1196 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1197 tuner->capability = V4L2_TUNER_CAP_LOW;
1198
1199 /* Stereo indicator == Stereo (instead of Mono) */
1200 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1201 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1202 else
1203 tuner->audmode = V4L2_TUNER_MODE_MONO;
1204
1205 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1206 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1207 * 0x0101;
1208
1209 /* automatic frequency control: -1: freq to low, 1 freq to high */
1210 tuner->afc = 0;
1211
1212 return 0;
1213}
1214
1215
1216/*
1217 * si470x_vidioc_s_tuner - set tuner attributes
1218 */
1219static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1220 struct v4l2_tuner *tuner)
1221{
1222 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1223
1224 if (tuner->index > 0)
1225 return -EINVAL;
1226
1227 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1228 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1229 else
1230 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1231
1232 return si470x_set_register(radio, POWERCFG);
1233}
1234
1235
1236/*
1237 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1238 */
1239static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1240 struct v4l2_frequency *freq)
1241{
1242 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1243
1244 freq->type = V4L2_TUNER_RADIO;
1245 freq->frequency = si470x_get_freq(radio);
1246
1247 return 0;
1248}
1249
1250
1251/*
1252 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1253 */
1254static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1255 struct v4l2_frequency *freq)
1256{
1257 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1258
1259 if (freq->type != V4L2_TUNER_RADIO)
1260 return -EINVAL;
1261
1262 return si470x_set_freq(radio, freq->frequency);
1263}
1264
1265
1266/*
1267 * si470x_viddev_tamples - video device interface
1268 */
1269static struct video_device si470x_viddev_template = {
1270 .fops = &si470x_fops,
1271 .name = DRIVER_NAME,
1272 .type = VID_TYPE_TUNER,
1273 .release = video_device_release,
1274 .vidioc_querycap = si470x_vidioc_querycap,
1275 .vidioc_g_input = si470x_vidioc_g_input,
1276 .vidioc_s_input = si470x_vidioc_s_input,
1277 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1278 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1279 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1280 .vidioc_g_audio = si470x_vidioc_g_audio,
1281 .vidioc_s_audio = si470x_vidioc_s_audio,
1282 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1283 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1284 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1285 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1286 .owner = THIS_MODULE,
1287};
1288
1289
1290
1291/**************************************************************************
1292 * USB Interface
1293 **************************************************************************/
1294
1295/*
1296 * si470x_usb_driver_probe - probe for the device
1297 */
1298static int si470x_usb_driver_probe(struct usb_interface *intf,
1299 const struct usb_device_id *id)
1300{
1301 struct si470x_device *radio;
1302
1303 /* memory and interface allocations */
1304 radio = kmalloc(sizeof(struct si470x_device), GFP_KERNEL);
1305 if (!radio)
1306 return -ENOMEM;
1307 radio->videodev = video_device_alloc();
1308 if (!radio->videodev) {
1309 kfree(radio);
1310 return -ENOMEM;
1311 }
1312 memcpy(radio->videodev, &si470x_viddev_template,
1313 sizeof(si470x_viddev_template));
1314 radio->users = 0;
1315 radio->usbdev = interface_to_usbdev(intf);
1316 video_set_drvdata(radio->videodev, radio);
1317 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
1318 printk(KERN_WARNING DRIVER_NAME
1319 ": Could not register video device\n");
1320 video_device_release(radio->videodev);
1321 kfree(radio);
1322 return -EIO;
1323 }
1324 usb_set_intfdata(intf, radio);
1325
1326 /* show some infos about the specific device */
1327 if (si470x_get_all_registers(radio) < 0) {
1328 video_device_release(radio->videodev);
1329 kfree(radio);
1330 return -EIO;
1331 }
1332 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4x ChipID=0x%4.4x\n",
1333 radio->registers[DEVICEID], radio->registers[CHIPID]);
1334
1335 /* check if firmware is current */
1336 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1337 < RADIO_SW_VERSION_CURRENT)
1338 printk(KERN_WARNING DRIVER_NAME
1339 ": This driver is known to work with chip version %d, "
1340 "but the device has firmware %d.\n"
1341 DRIVER_NAME
1342 "If you have some trouble using this driver, please "
1343 "report to V4L ML at video4linux-list@redhat.com\n",
1344 radio->registers[CHIPID] & CHIPID_FIRMWARE,
1345 RADIO_SW_VERSION_CURRENT);
1346
1347 /* set initial frequency */
1348 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1349
1350 /* rds initialization */
1351 radio->buf_size = rds_buf * 3;
1352 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1353 if (!radio->buffer) {
1354 video_device_release(radio->videodev);
1355 kfree(radio);
1356 return -ENOMEM;
1357 }
1358 radio->wr_index = 0;
1359 radio->rd_index = 0;
1360 init_waitqueue_head(&radio->read_queue);
1361
1362 /* prepare polling via eventd */
1363 INIT_WORK(&radio->work, si470x_work);
1364 init_timer(&radio->timer);
1365 radio->timer.function = si470x_timer;
1366 radio->timer.data = (unsigned long) radio;
1367
1368 return 0;
1369}
1370
1371
1372/*
1373 * si470x_usb_driver_disconnect - disconnect the device
1374 */
1375static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1376{
1377 struct si470x_device *radio = usb_get_intfdata(intf);
1378
1379 del_timer_sync(&radio->timer);
1380 flush_scheduled_work();
1381
1382 usb_set_intfdata(intf, NULL);
1383 if (radio) {
1384 video_unregister_device(radio->videodev);
1385 kfree(radio->buffer);
1386 kfree(radio);
1387 }
1388}
1389
1390
1391/*
1392 * si470x_usb_driver - usb driver interface
1393 */
1394static struct usb_driver si470x_usb_driver = {
1395 .name = DRIVER_NAME,
1396 .probe = si470x_usb_driver_probe,
1397 .disconnect = si470x_usb_driver_disconnect,
1398 .id_table = si470x_usb_driver_id_table,
1399};
1400
1401
1402
1403/**************************************************************************
1404 * Module Interface
1405 **************************************************************************/
1406
1407/*
1408 * si470x_module_init - module init
1409 */
1410static int __init si470x_module_init(void)
1411{
1412 printk(KERN_INFO DRIVER_DESC "\n");
1413 return usb_register(&si470x_usb_driver);
1414}
1415
1416
1417/*
1418 * si470x_module_exit - module exit
1419 */
1420static void __exit si470x_module_exit(void)
1421{
1422 usb_deregister(&si470x_usb_driver);
1423}
1424
1425
1426module_init(si470x_module_init);
1427module_exit(si470x_module_exit);
1428
1429MODULE_LICENSE("GPL");
1430MODULE_AUTHOR(DRIVER_AUTHOR);
1431MODULE_DESCRIPTION(DRIVER_DESC);
1432MODULE_VERSION("1.0.4");
diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
index c9f14bfc8544..a2e8987a6195 100644
--- a/drivers/media/video/Kconfig
+++ b/drivers/media/video/Kconfig
@@ -45,7 +45,7 @@ comment "Audio decoders"
45 45
46config VIDEO_TVAUDIO 46config VIDEO_TVAUDIO
47 tristate "Simple audio decoder chips" 47 tristate "Simple audio decoder chips"
48 depends on VIDEO_V4L1 && I2C 48 depends on VIDEO_V4L2 && I2C
49 ---help--- 49 ---help---
50 Support for several audio decoder chips found on some bt8xx boards: 50 Support for several audio decoder chips found on some bt8xx boards:
51 Philips: tda9840, tda9873h, tda9874h/a, tda9850, tda985x, tea6300, 51 Philips: tda9840, tda9873h, tda9874h/a, tda9850, tda985x, tea6300,
@@ -57,7 +57,7 @@ config VIDEO_TVAUDIO
57 57
58config VIDEO_TDA7432 58config VIDEO_TDA7432
59 tristate "Philips TDA7432 audio processor" 59 tristate "Philips TDA7432 audio processor"
60 depends on VIDEO_V4L1 && I2C 60 depends on VIDEO_V4L2 && I2C
61 ---help--- 61 ---help---
62 Support for tda7432 audio decoder chip found on some bt8xx boards. 62 Support for tda7432 audio decoder chip found on some bt8xx boards.
63 63
@@ -75,7 +75,7 @@ config VIDEO_TDA9840
75 75
76config VIDEO_TDA9875 76config VIDEO_TDA9875
77 tristate "Philips TDA9875 audio processor" 77 tristate "Philips TDA9875 audio processor"
78 depends on VIDEO_V4L1 && I2C 78 depends on VIDEO_V4L2 && I2C
79 ---help--- 79 ---help---
80 Support for tda9875 audio decoder chip found on some bt8xx boards. 80 Support for tda9875 audio decoder chip found on some bt8xx boards.
81 81
@@ -109,9 +109,19 @@ config VIDEO_MSP3400
109 To compile this driver as a module, choose M here: the 109 To compile this driver as a module, choose M here: the
110 module will be called msp3400. 110 module will be called msp3400.
111 111
112config VIDEO_CS5345
113 tristate "Cirrus Logic CS5345 audio ADC"
114 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL
115 ---help---
116 Support for the Cirrus Logic CS5345 24-bit, 192 kHz
117 stereo A/D converter.
118
119 To compile this driver as a module, choose M here: the
120 module will be called cs5345.
121
112config VIDEO_CS53L32A 122config VIDEO_CS53L32A
113 tristate "Cirrus Logic CS53L32A audio ADC" 123 tristate "Cirrus Logic CS53L32A audio ADC"
114 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL 124 depends on VIDEO_V4L2 && I2C
115 ---help--- 125 ---help---
116 Support for the Cirrus Logic CS53L32A low voltage 126 Support for the Cirrus Logic CS53L32A low voltage
117 stereo A/D converter. 127 stereo A/D converter.
@@ -119,6 +129,15 @@ config VIDEO_CS53L32A
119 To compile this driver as a module, choose M here: the 129 To compile this driver as a module, choose M here: the
120 module will be called cs53l32a. 130 module will be called cs53l32a.
121 131
132config VIDEO_M52790
133 tristate "Mitsubishi M52790 A/V switch"
134 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL
135 ---help---
136 Support for the Mitsubishi M52790 A/V switch.
137
138 To compile this driver as a module, choose M here: the
139 module will be called m52790.
140
122config VIDEO_TLV320AIC23B 141config VIDEO_TLV320AIC23B
123 tristate "Texas Instruments TLV320AIC23B audio codec" 142 tristate "Texas Instruments TLV320AIC23B audio codec"
124 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL 143 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL
@@ -130,7 +149,7 @@ config VIDEO_TLV320AIC23B
130 149
131config VIDEO_WM8775 150config VIDEO_WM8775
132 tristate "Wolfson Microelectronics WM8775 audio ADC with input mixer" 151 tristate "Wolfson Microelectronics WM8775 audio ADC with input mixer"
133 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL 152 depends on VIDEO_V4L2 && I2C
134 ---help--- 153 ---help---
135 Support for the Wolfson Microelectronics WM8775 high 154 Support for the Wolfson Microelectronics WM8775 high
136 performance stereo A/D Converter with a 4 channel input mixer. 155 performance stereo A/D Converter with a 4 channel input mixer.
@@ -140,7 +159,7 @@ config VIDEO_WM8775
140 159
141config VIDEO_WM8739 160config VIDEO_WM8739
142 tristate "Wolfson Microelectronics WM8739 stereo audio ADC" 161 tristate "Wolfson Microelectronics WM8739 stereo audio ADC"
143 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL 162 depends on VIDEO_V4L2 && I2C
144 ---help--- 163 ---help---
145 Support for the Wolfson Microelectronics WM8739 164 Support for the Wolfson Microelectronics WM8739
146 stereo A/D Converter. 165 stereo A/D Converter.
@@ -244,7 +263,7 @@ config VIDEO_SAA7114
244 263
245config VIDEO_SAA711X 264config VIDEO_SAA711X
246 tristate "Philips SAA7113/4/5 video decoders" 265 tristate "Philips SAA7113/4/5 video decoders"
247 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL 266 depends on VIDEO_V4L2 && I2C
248 ---help--- 267 ---help---
249 Support for the Philips SAA7113/4/5 video decoders. 268 Support for the Philips SAA7113/4/5 video decoders.
250 269
@@ -300,7 +319,7 @@ comment "Video encoders"
300 319
301config VIDEO_SAA7127 320config VIDEO_SAA7127
302 tristate "Philips SAA7127/9 digital video encoders" 321 tristate "Philips SAA7127/9 digital video encoders"
303 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL 322 depends on VIDEO_V4L2 && I2C
304 ---help--- 323 ---help---
305 Support for the Philips SAA7127/9 digital video encoders. 324 Support for the Philips SAA7127/9 digital video encoders.
306 325
@@ -338,7 +357,7 @@ comment "Video improvement chips"
338 357
339config VIDEO_UPD64031A 358config VIDEO_UPD64031A
340 tristate "NEC Electronics uPD64031A Ghost Reduction" 359 tristate "NEC Electronics uPD64031A Ghost Reduction"
341 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL 360 depends on VIDEO_V4L2 && I2C
342 ---help--- 361 ---help---
343 Support for the NEC Electronics uPD64031A Ghost Reduction 362 Support for the NEC Electronics uPD64031A Ghost Reduction
344 video chip. It is most often found in NTSC TV cards made for 363 video chip. It is most often found in NTSC TV cards made for
@@ -350,7 +369,7 @@ config VIDEO_UPD64031A
350 369
351config VIDEO_UPD64083 370config VIDEO_UPD64083
352 tristate "NEC Electronics uPD64083 3-Dimensional Y/C separation" 371 tristate "NEC Electronics uPD64083 3-Dimensional Y/C separation"
353 depends on VIDEO_V4L2 && I2C && EXPERIMENTAL 372 depends on VIDEO_V4L2 && I2C
354 ---help--- 373 ---help---
355 Support for the NEC Electronics uPD64083 3-Dimensional Y/C 374 Support for the NEC Electronics uPD64083 3-Dimensional Y/C
356 separation video chip. It is used to improve the quality of 375 separation video chip. It is used to improve the quality of
@@ -802,6 +821,19 @@ config USB_ZR364XX
802 To compile this driver as a module, choose M here: the 821 To compile this driver as a module, choose M here: the
803 module will be called zr364xx. 822 module will be called zr364xx.
804 823
824config USB_STKWEBCAM
825 tristate "USB Syntek DC1125 Camera support"
826 depends on VIDEO_V4L2 && EXPERIMENTAL
827 ---help---
828 Say Y here if you want to use this type of camera.
829 Supported devices are typically found in some Asus laptops,
830 with USB id 174f:a311 and 05e1:0501. Other Syntek cameras
831 may be supported by the stk11xx driver, from which this is
832 derived, see http://stk11xx.sourceforge.net
833
834 To compile this driver as a module, choose M here: the
835 module will be called stkwebcam.
836
805endif # V4L_USB_DRIVERS 837endif # V4L_USB_DRIVERS
806 838
807endif # VIDEO_CAPTURE_DRIVERS 839endif # VIDEO_CAPTURE_DRIVERS
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile
index b5a064163e03..28ddd146c1c5 100644
--- a/drivers/media/video/Makefile
+++ b/drivers/media/video/Makefile
@@ -4,10 +4,12 @@
4 4
5zr36067-objs := zoran_procfs.o zoran_device.o \ 5zr36067-objs := zoran_procfs.o zoran_device.o \
6 zoran_driver.o zoran_card.o 6 zoran_driver.o zoran_card.o
7tuner-objs := tuner-core.o tuner-types.o tda9887.o 7tuner-objs := tuner-core.o tuner-types.o
8 8
9msp3400-objs := msp3400-driver.o msp3400-kthreads.o 9msp3400-objs := msp3400-driver.o msp3400-kthreads.o
10 10
11stkwebcam-objs := stk-webcam.o stk-sensor.o
12
11obj-$(CONFIG_VIDEO_DEV) += videodev.o v4l2-common.o compat_ioctl32.o \ 13obj-$(CONFIG_VIDEO_DEV) += videodev.o v4l2-common.o compat_ioctl32.o \
12 v4l2-int-device.o 14 v4l2-int-device.o
13 15
@@ -66,7 +68,9 @@ obj-$(CONFIG_VIDEO_USBVISION) += usbvision/
66obj-$(CONFIG_VIDEO_TVP5150) += tvp5150.o 68obj-$(CONFIG_VIDEO_TVP5150) += tvp5150.o
67obj-$(CONFIG_VIDEO_PVRUSB2) += pvrusb2/ 69obj-$(CONFIG_VIDEO_PVRUSB2) += pvrusb2/
68obj-$(CONFIG_VIDEO_MSP3400) += msp3400.o 70obj-$(CONFIG_VIDEO_MSP3400) += msp3400.o
71obj-$(CONFIG_VIDEO_CS5345) += cs5345.o
69obj-$(CONFIG_VIDEO_CS53L32A) += cs53l32a.o 72obj-$(CONFIG_VIDEO_CS53L32A) += cs53l32a.o
73obj-$(CONFIG_VIDEO_M52790) += m52790.o
70obj-$(CONFIG_VIDEO_TLV320AIC23B) += tlv320aic23b.o 74obj-$(CONFIG_VIDEO_TLV320AIC23B) += tlv320aic23b.o
71obj-$(CONFIG_VIDEO_WM8775) += wm8775.o 75obj-$(CONFIG_VIDEO_WM8775) += wm8775.o
72obj-$(CONFIG_VIDEO_WM8739) += wm8739.o 76obj-$(CONFIG_VIDEO_WM8739) += wm8739.o
@@ -81,11 +85,13 @@ obj-$(CONFIG_TUNER_3036) += tuner-3036.o
81 85
82obj-$(CONFIG_VIDEO_TUNER) += tuner.o 86obj-$(CONFIG_VIDEO_TUNER) += tuner.o
83 87
88obj-$(CONFIG_TUNER_XC2028) += tuner-xc2028.o
84obj-$(CONFIG_TUNER_SIMPLE) += tuner-simple.o 89obj-$(CONFIG_TUNER_SIMPLE) += tuner-simple.o
85obj-$(CONFIG_TUNER_MT20XX) += mt20xx.o 90obj-$(CONFIG_TUNER_MT20XX) += mt20xx.o
86obj-$(CONFIG_TUNER_TDA8290) += tda8290.o 91obj-$(CONFIG_TUNER_TDA8290) += tda8290.o
87obj-$(CONFIG_TUNER_TEA5767) += tea5767.o 92obj-$(CONFIG_TUNER_TEA5767) += tea5767.o
88obj-$(CONFIG_TUNER_TEA5761) += tea5761.o 93obj-$(CONFIG_TUNER_TEA5761) += tea5761.o
94obj-$(CONFIG_TUNER_TDA9887) += tda9887.o
89 95
90obj-$(CONFIG_VIDEOBUF_GEN) += videobuf-core.o 96obj-$(CONFIG_VIDEOBUF_GEN) += videobuf-core.o
91obj-$(CONFIG_VIDEOBUF_DMA_SG) += videobuf-dma-sg.o 97obj-$(CONFIG_VIDEOBUF_DMA_SG) += videobuf-dma-sg.o
@@ -112,6 +118,7 @@ obj-$(CONFIG_USB_SE401) += se401.o
112obj-$(CONFIG_USB_STV680) += stv680.o 118obj-$(CONFIG_USB_STV680) += stv680.o
113obj-$(CONFIG_USB_W9968CF) += w9968cf.o 119obj-$(CONFIG_USB_W9968CF) += w9968cf.o
114obj-$(CONFIG_USB_ZR364XX) += zr364xx.o 120obj-$(CONFIG_USB_ZR364XX) += zr364xx.o
121obj-$(CONFIG_USB_STKWEBCAM) += stkwebcam.o
115 122
116obj-$(CONFIG_USB_SN9C102) += sn9c102/ 123obj-$(CONFIG_USB_SN9C102) += sn9c102/
117obj-$(CONFIG_USB_ET61X251) += et61x251/ 124obj-$(CONFIG_USB_ET61X251) += et61x251/
@@ -129,3 +136,4 @@ obj-$(CONFIG_VIDEO_VIVI) += vivi.o
129obj-$(CONFIG_VIDEO_CX23885) += cx23885/ 136obj-$(CONFIG_VIDEO_CX23885) += cx23885/
130 137
131EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core 138EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core
139EXTRA_CFLAGS += -Idrivers/media/dvb/frontends
diff --git a/drivers/media/video/bt8xx/Kconfig b/drivers/media/video/bt8xx/Kconfig
index 2ca162b390a2..cfc822bb502a 100644
--- a/drivers/media/video/bt8xx/Kconfig
+++ b/drivers/media/video/bt8xx/Kconfig
@@ -1,6 +1,6 @@
1config VIDEO_BT848 1config VIDEO_BT848
2 tristate "BT848 Video For Linux" 2 tristate "BT848 Video For Linux"
3 depends on VIDEO_DEV && PCI && I2C && VIDEO_V4L1 3 depends on VIDEO_DEV && PCI && I2C && VIDEO_V4L2 && INPUT
4 select I2C_ALGOBIT 4 select I2C_ALGOBIT
5 select FW_LOADER 5 select FW_LOADER
6 select VIDEO_BTCX 6 select VIDEO_BTCX
diff --git a/drivers/media/video/bt8xx/Makefile b/drivers/media/video/bt8xx/Makefile
index a096a03418aa..924d216d9570 100644
--- a/drivers/media/video/bt8xx/Makefile
+++ b/drivers/media/video/bt8xx/Makefile
@@ -4,7 +4,7 @@
4 4
5bttv-objs := bttv-driver.o bttv-cards.o bttv-if.o \ 5bttv-objs := bttv-driver.o bttv-cards.o bttv-if.o \
6 bttv-risc.o bttv-vbi.o bttv-i2c.o bttv-gpio.o \ 6 bttv-risc.o bttv-vbi.o bttv-i2c.o bttv-gpio.o \
7 bttv-input.o 7 bttv-input.o bttv-audio-hook.o
8 8
9obj-$(CONFIG_VIDEO_BT848) += bttv.o 9obj-$(CONFIG_VIDEO_BT848) += bttv.o
10 10
diff --git a/drivers/media/video/bt8xx/bttv-audio-hook.c b/drivers/media/video/bt8xx/bttv-audio-hook.c
new file mode 100644
index 000000000000..2364d16586b3
--- /dev/null
+++ b/drivers/media/video/bt8xx/bttv-audio-hook.c
@@ -0,0 +1,382 @@
1/*
2 * Handlers for board audio hooks, splitted from bttv-cards
3 *
4 * Copyright (c) 2006 Mauro Carvalho Chehab (mchehab@infradead.org)
5 * This code is placed under the terms of the GNU General Public License
6 */
7
8#include "bttv-audio-hook.h"
9
10#include <linux/delay.h>
11
12/* ----------------------------------------------------------------------- */
13/* winview */
14
15void winview_volume(struct bttv *btv, __u16 volume)
16{
17 /* PT2254A programming Jon Tombs, jon@gte.esi.us.es */
18 int bits_out, loops, vol, data;
19
20 /* 32 levels logarithmic */
21 vol = 32 - ((volume>>11));
22 /* units */
23 bits_out = (PT2254_DBS_IN_2>>(vol%5));
24 /* tens */
25 bits_out |= (PT2254_DBS_IN_10>>(vol/5));
26 bits_out |= PT2254_L_CHANNEL | PT2254_R_CHANNEL;
27 data = gpio_read();
28 data &= ~(WINVIEW_PT2254_CLK| WINVIEW_PT2254_DATA|
29 WINVIEW_PT2254_STROBE);
30 for (loops = 17; loops >= 0 ; loops--) {
31 if (bits_out & (1<<loops))
32 data |= WINVIEW_PT2254_DATA;
33 else
34 data &= ~WINVIEW_PT2254_DATA;
35 gpio_write(data);
36 udelay(5);
37 data |= WINVIEW_PT2254_CLK;
38 gpio_write(data);
39 udelay(5);
40 data &= ~WINVIEW_PT2254_CLK;
41 gpio_write(data);
42 }
43 data |= WINVIEW_PT2254_STROBE;
44 data &= ~WINVIEW_PT2254_DATA;
45 gpio_write(data);
46 udelay(10);
47 data &= ~WINVIEW_PT2254_STROBE;
48 gpio_write(data);
49}
50
51/* ----------------------------------------------------------------------- */
52/* mono/stereo control for various cards (which don't use i2c chips but */
53/* connect something to the GPIO pins */
54
55void gvbctv3pci_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
56{
57 unsigned int con = 0;
58
59 if (set) {
60 gpio_inout(0x300, 0x300);
61 if (t->audmode & V4L2_TUNER_MODE_LANG1)
62 con = 0x000;
63 if (t->audmode & V4L2_TUNER_MODE_LANG2)
64 con = 0x300;
65 if (t->audmode & V4L2_TUNER_MODE_STEREO)
66 con = 0x200;
67/* if (t->audmode & V4L2_TUNER_MODE_MONO)
68 * con = 0x100; */
69 gpio_bits(0x300, con);
70 } else {
71 t->audmode = V4L2_TUNER_MODE_STEREO |
72 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
73 }
74}
75
76void gvbctv5pci_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
77{
78 unsigned int val, con;
79
80 if (btv->radio_user)
81 return;
82
83 val = gpio_read();
84 if (set) {
85 con = 0x000;
86 if (t->audmode & V4L2_TUNER_MODE_LANG2) {
87 if (t->audmode & V4L2_TUNER_MODE_LANG1) {
88 /* LANG1 + LANG2 */
89 con = 0x100;
90 }
91 else {
92 /* LANG2 */
93 con = 0x300;
94 }
95 }
96 if (con != (val & 0x300)) {
97 gpio_bits(0x300, con);
98 if (bttv_gpio)
99 bttv_gpio_tracking(btv,"gvbctv5pci");
100 }
101 } else {
102 switch (val & 0x70) {
103 case 0x10:
104 t->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
105 break;
106 case 0x30:
107 t->rxsubchans = V4L2_TUNER_SUB_LANG2;
108 break;
109 case 0x50:
110 t->rxsubchans = V4L2_TUNER_SUB_LANG1;
111 break;
112 case 0x60:
113 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
114 break;
115 case 0x70:
116 t->rxsubchans = V4L2_TUNER_SUB_MONO;
117 break;
118 default:
119 t->rxsubchans = V4L2_TUNER_SUB_MONO |
120 V4L2_TUNER_SUB_STEREO |
121 V4L2_TUNER_SUB_LANG1 |
122 V4L2_TUNER_SUB_LANG2;
123 }
124 t->audmode = V4L2_TUNER_MODE_STEREO |
125 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
126 }
127}
128
129/*
130 * Mario Medina Nussbaum <medisoft@alohabbs.org.mx>
131 * I discover that on BT848_GPIO_DATA address a byte 0xcce enable stereo,
132 * 0xdde enables mono and 0xccd enables sap
133 *
134 * Petr Vandrovec <VANDROVE@vc.cvut.cz>
135 * P.S.: At least mask in line above is wrong - GPIO pins 3,2 select
136 * input/output sound connection, so both must be set for output mode.
137 *
138 * Looks like it's needed only for the "tvphone", the "tvphone 98"
139 * handles this with a tda9840
140 *
141 */
142
143void avermedia_tvphone_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
144{
145 int val = 0;
146
147 if (set) {
148 if (t->audmode & V4L2_TUNER_MODE_LANG2) /* SAP */
149 val = 0x02;
150 if (t->audmode & V4L2_TUNER_MODE_STEREO)
151 val = 0x01;
152 if (val) {
153 gpio_bits(0x03,val);
154 if (bttv_gpio)
155 bttv_gpio_tracking(btv,"avermedia");
156 }
157 } else {
158 t->audmode = V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO |
159 V4L2_TUNER_MODE_LANG1;
160 return;
161 }
162}
163
164
165void avermedia_tv_stereo_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
166{
167 int val = 0;
168
169 if (set) {
170 if (t->audmode & V4L2_TUNER_MODE_LANG2) /* SAP */
171 val = 0x01;
172 if (t->audmode & V4L2_TUNER_MODE_STEREO) /* STEREO */
173 val = 0x02;
174 btaor(val, ~0x03, BT848_GPIO_DATA);
175 if (bttv_gpio)
176 bttv_gpio_tracking(btv,"avermedia");
177 } else {
178 t->audmode = V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO |
179 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
180 return;
181 }
182}
183
184/* Lifetec 9415 handling */
185
186void lt9415_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
187{
188 int val = 0;
189
190 if (gpio_read() & 0x4000) {
191 t->audmode = V4L2_TUNER_MODE_MONO;
192 return;
193 }
194
195 if (set) {
196 if (t->audmode & V4L2_TUNER_MODE_LANG2) /* A2 SAP */
197 val = 0x0080;
198 if (t->audmode & V4L2_TUNER_MODE_STEREO) /* A2 stereo */
199 val = 0x0880;
200 if ((t->audmode & V4L2_TUNER_MODE_LANG1) ||
201 (t->audmode & V4L2_TUNER_MODE_MONO))
202 val = 0;
203 gpio_bits(0x0880, val);
204 if (bttv_gpio)
205 bttv_gpio_tracking(btv,"lt9415");
206 } else {
207 /* autodetect doesn't work with this card :-( */
208 t->audmode = V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO |
209 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
210 return;
211 }
212}
213
214/* TDA9821 on TerraTV+ Bt848, Bt878 */
215void terratv_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
216{
217 unsigned int con = 0;
218
219 if (set) {
220 gpio_inout(0x180000,0x180000);
221 if (t->audmode & V4L2_TUNER_MODE_LANG2)
222 con = 0x080000;
223 if (t->audmode & V4L2_TUNER_MODE_STEREO)
224 con = 0x180000;
225 gpio_bits(0x180000, con);
226 if (bttv_gpio)
227 bttv_gpio_tracking(btv,"terratv");
228 } else {
229 t->audmode = V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO |
230 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
231 }
232}
233
234
235void winfast2000_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
236{
237 unsigned long val = 0;
238
239 if (set) {
240 /*btor (0xc32000, BT848_GPIO_OUT_EN);*/
241 if (t->audmode & V4L2_TUNER_MODE_MONO) /* Mono */
242 val = 0x420000;
243 if (t->audmode & V4L2_TUNER_MODE_LANG1) /* Mono */
244 val = 0x420000;
245 if (t->audmode & V4L2_TUNER_MODE_LANG2) /* SAP */
246 val = 0x410000;
247 if (t->audmode & V4L2_TUNER_MODE_STEREO) /* Stereo */
248 val = 0x020000;
249 if (val) {
250 gpio_bits(0x430000, val);
251 if (bttv_gpio)
252 bttv_gpio_tracking(btv,"winfast2000");
253 }
254 } else {
255 t->audmode = V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO |
256 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
257 }
258}
259
260/*
261 * Dariusz Kowalewski <darekk@automex.pl>
262 * sound control for Prolink PV-BT878P+9B (PixelView PlayTV Pro FM+NICAM
263 * revision 9B has on-board TDA9874A sound decoder).
264 *
265 * Note: There are card variants without tda9874a. Forcing the "stereo sound route"
266 * will mute this cards.
267 */
268void pvbt878p9b_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
269{
270 unsigned int val = 0;
271
272 if (btv->radio_user)
273 return;
274
275 if (set) {
276 if (t->audmode & V4L2_TUNER_MODE_MONO) {
277 val = 0x01;
278 }
279 if ((t->audmode & (V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2))
280 || (t->audmode & V4L2_TUNER_MODE_STEREO)) {
281 val = 0x02;
282 }
283 if (val) {
284 gpio_bits(0x03,val);
285 if (bttv_gpio)
286 bttv_gpio_tracking(btv,"pvbt878p9b");
287 }
288 } else {
289 t->audmode = V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO |
290 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
291 }
292}
293
294/*
295 * Dariusz Kowalewski <darekk@automex.pl>
296 * sound control for FlyVideo 2000S (with tda9874 decoder)
297 * based on pvbt878p9b_audio() - this is not tested, please fix!!!
298 */
299void fv2000s_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
300{
301 unsigned int val = 0xffff;
302
303 if (btv->radio_user)
304 return;
305
306 if (set) {
307 if (t->audmode & V4L2_TUNER_MODE_MONO) {
308 val = 0x0000;
309 }
310 if ((t->audmode & (V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2))
311 || (t->audmode & V4L2_TUNER_MODE_STEREO)) {
312 val = 0x1080; /*-dk-???: 0x0880, 0x0080, 0x1800 ... */
313 }
314 if (val != 0xffff) {
315 gpio_bits(0x1800, val);
316 if (bttv_gpio)
317 bttv_gpio_tracking(btv,"fv2000s");
318 }
319 } else {
320 t->audmode = V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO |
321 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
322 }
323}
324
325/*
326 * sound control for Canopus WinDVR PCI
327 * Masaki Suzuki <masaki@btree.org>
328 */
329void windvr_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
330{
331 unsigned long val = 0;
332
333 if (set) {
334 if (t->audmode & V4L2_TUNER_MODE_MONO)
335 val = 0x040000;
336 if (t->audmode & V4L2_TUNER_MODE_LANG1)
337 val = 0;
338 if (t->audmode & V4L2_TUNER_MODE_LANG2)
339 val = 0x100000;
340 if (t->audmode & V4L2_TUNER_MODE_STEREO)
341 val = 0;
342 if (val) {
343 gpio_bits(0x140000, val);
344 if (bttv_gpio)
345 bttv_gpio_tracking(btv,"windvr");
346 }
347 } else {
348 t->audmode = V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO |
349 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
350 }
351}
352
353/*
354 * sound control for AD-TVK503
355 * Hiroshi Takekawa <sian@big.or.jp>
356 */
357void adtvk503_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
358{
359 unsigned int con = 0xffffff;
360
361 /* btaor(0x1e0000, ~0x1e0000, BT848_GPIO_OUT_EN); */
362
363 if (set) {
364 /* btor(***, BT848_GPIO_OUT_EN); */
365 if (t->audmode & V4L2_TUNER_MODE_LANG1)
366 con = 0x00000000;
367 if (t->audmode & V4L2_TUNER_MODE_LANG2)
368 con = 0x00180000;
369 if (t->audmode & V4L2_TUNER_MODE_STEREO)
370 con = 0x00000000;
371 if (t->audmode & V4L2_TUNER_MODE_MONO)
372 con = 0x00060000;
373 if (con != 0xffffff) {
374 gpio_bits(0x1e0000,con);
375 if (bttv_gpio)
376 bttv_gpio_tracking(btv, "adtvk503");
377 }
378 } else {
379 t->audmode = V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO |
380 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
381 }
382}
diff --git a/drivers/media/video/bt8xx/bttv-audio-hook.h b/drivers/media/video/bt8xx/bttv-audio-hook.h
new file mode 100644
index 000000000000..159d07adeff8
--- /dev/null
+++ b/drivers/media/video/bt8xx/bttv-audio-hook.h
@@ -0,0 +1,23 @@
1/*
2 * Handlers for board audio hooks, splitted from bttv-cards
3 *
4 * Copyright (c) 2006 Mauro Carvalho Chehab (mchehab@infradead.org)
5 * This code is placed under the terms of the GNU General Public License
6 */
7
8#include "bttvp.h"
9
10void winview_volume (struct bttv *btv, __u16 volume);
11
12void lt9415_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
13void avermedia_tvphone_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
14void avermedia_tv_stereo_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
15void terratv_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
16void gvbctv3pci_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
17void gvbctv5pci_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
18void winfast2000_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
19void pvbt878p9b_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
20void fv2000s_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
21void windvr_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
22void adtvk503_audio(struct bttv *btv, struct v4l2_tuner *tuner, int set);
23
diff --git a/drivers/media/video/bt8xx/bttv-cards.c b/drivers/media/video/bt8xx/bttv-cards.c
index 585d1ef95afd..63a47cd4c161 100644
--- a/drivers/media/video/bt8xx/bttv-cards.c
+++ b/drivers/media/video/bt8xx/bttv-cards.c
@@ -39,6 +39,7 @@
39#include "bttvp.h" 39#include "bttvp.h"
40#include <media/v4l2-common.h> 40#include <media/v4l2-common.h>
41#include <media/tvaudio.h> 41#include <media/tvaudio.h>
42#include "bttv-audio-hook.h"
42 43
43/* fwd decl */ 44/* fwd decl */
44static void boot_msp34xx(struct bttv *btv, int pin); 45static void boot_msp34xx(struct bttv *btv, int pin);
@@ -50,20 +51,6 @@ static void modtec_eeprom(struct bttv *btv);
50static void init_PXC200(struct bttv *btv); 51static void init_PXC200(struct bttv *btv);
51static void init_RTV24(struct bttv *btv); 52static void init_RTV24(struct bttv *btv);
52 53
53static void winview_audio(struct bttv *btv, struct video_audio *v, int set);
54static void lt9415_audio(struct bttv *btv, struct video_audio *v, int set);
55static void avermedia_tvphone_audio(struct bttv *btv, struct video_audio *v,
56 int set);
57static void avermedia_tv_stereo_audio(struct bttv *btv, struct video_audio *v,
58 int set);
59static void terratv_audio(struct bttv *btv, struct video_audio *v, int set);
60static void gvbctv3pci_audio(struct bttv *btv, struct video_audio *v, int set);
61static void gvbctv5pci_audio(struct bttv *btv, struct video_audio *v, int set);
62static void winfast2000_audio(struct bttv *btv, struct video_audio *v, int set);
63static void pvbt878p9b_audio(struct bttv *btv, struct video_audio *v, int set);
64static void fv2000s_audio(struct bttv *btv, struct video_audio *v, int set);
65static void windvr_audio(struct bttv *btv, struct video_audio *v, int set);
66static void adtvk503_audio(struct bttv *btv, struct video_audio *v, int set);
67static void rv605_muxsel(struct bttv *btv, unsigned int input); 54static void rv605_muxsel(struct bttv *btv, unsigned int input);
68static void eagle_muxsel(struct bttv *btv, unsigned int input); 55static void eagle_muxsel(struct bttv *btv, unsigned int input);
69static void xguard_muxsel(struct bttv *btv, unsigned int input); 56static void xguard_muxsel(struct bttv *btv, unsigned int input);
@@ -427,7 +414,7 @@ struct tvcard bttv_tvcards[] = {
427 .tuner_type = UNSET, 414 .tuner_type = UNSET,
428 .tuner_addr = ADDR_UNSET, 415 .tuner_addr = ADDR_UNSET,
429 .radio_addr = ADDR_UNSET, 416 .radio_addr = ADDR_UNSET,
430 .audio_hook = avermedia_tvphone_audio, 417 .audio_mode_gpio= avermedia_tvphone_audio,
431 .has_remote = 1, 418 .has_remote = 1,
432 }, 419 },
433 [BTTV_BOARD_MATRIX_VISION] = { 420 [BTTV_BOARD_MATRIX_VISION] = {
@@ -539,7 +526,7 @@ struct tvcard bttv_tvcards[] = {
539 .tuner_type = TUNER_PHILIPS_PAL, 526 .tuner_type = TUNER_PHILIPS_PAL,
540 .tuner_addr = ADDR_UNSET, 527 .tuner_addr = ADDR_UNSET,
541 .radio_addr = ADDR_UNSET, 528 .radio_addr = ADDR_UNSET,
542 .audio_hook = avermedia_tv_stereo_audio, 529 .audio_mode_gpio= avermedia_tv_stereo_audio,
543 .no_gpioirq = 1, 530 .no_gpioirq = 1,
544 }, 531 },
545 [BTTV_BOARD_VHX] = { 532 [BTTV_BOARD_VHX] = {
@@ -604,7 +591,7 @@ struct tvcard bttv_tvcards[] = {
604 .tuner_type = UNSET, 591 .tuner_type = UNSET,
605 .tuner_addr = ADDR_UNSET, 592 .tuner_addr = ADDR_UNSET,
606 .radio_addr = ADDR_UNSET, 593 .radio_addr = ADDR_UNSET,
607 .audio_hook = winview_audio, 594 .volume_gpio = winview_volume,
608 .has_radio = 1, 595 .has_radio = 1,
609 }, 596 },
610 [BTTV_BOARD_AVEC_INTERCAP] = { 597 [BTTV_BOARD_AVEC_INTERCAP] = {
@@ -728,7 +715,7 @@ struct tvcard bttv_tvcards[] = {
728 .tuner_type = TUNER_PHILIPS_PAL, 715 .tuner_type = TUNER_PHILIPS_PAL,
729 .tuner_addr = ADDR_UNSET, 716 .tuner_addr = ADDR_UNSET,
730 .radio_addr = ADDR_UNSET, 717 .radio_addr = ADDR_UNSET,
731 .audio_hook = terratv_audio, 718 .audio_mode_gpio= terratv_audio,
732 }, 719 },
733 [BTTV_BOARD_HAUPPAUG_WCAM] = { 720 [BTTV_BOARD_HAUPPAUG_WCAM] = {
734 .name = "Hauppauge WinCam newer (bt878)", 721 .name = "Hauppauge WinCam newer (bt878)",
@@ -776,7 +763,7 @@ struct tvcard bttv_tvcards[] = {
776 .tuner_type = TUNER_PHILIPS_PAL, 763 .tuner_type = TUNER_PHILIPS_PAL,
777 .tuner_addr = ADDR_UNSET, 764 .tuner_addr = ADDR_UNSET,
778 .radio_addr = ADDR_UNSET, 765 .radio_addr = ADDR_UNSET,
779 .audio_hook = terratv_audio, 766 .audio_mode_gpio= terratv_audio,
780 /* GPIO wiring: 767 /* GPIO wiring:
781 External 20 pin connector (for Active Radio Upgrade board) 768 External 20 pin connector (for Active Radio Upgrade board)
782 gpio00: i2c-sda 769 gpio00: i2c-sda
@@ -915,7 +902,7 @@ struct tvcard bttv_tvcards[] = {
915 .tuner_type = TUNER_PHILIPS_PAL, /* default for now, gpio reads BFFF06 for Pal bg+dk */ 902 .tuner_type = TUNER_PHILIPS_PAL, /* default for now, gpio reads BFFF06 for Pal bg+dk */
916 .tuner_addr = ADDR_UNSET, 903 .tuner_addr = ADDR_UNSET,
917 .radio_addr = ADDR_UNSET, 904 .radio_addr = ADDR_UNSET,
918 .audio_hook = winfast2000_audio, 905 .audio_mode_gpio= winfast2000_audio,
919 .has_remote = 1, 906 .has_remote = 1,
920 }, 907 },
921 [BTTV_BOARD_CHRONOS_VS2] = { 908 [BTTV_BOARD_CHRONOS_VS2] = {
@@ -1035,7 +1022,7 @@ struct tvcard bttv_tvcards[] = {
1035 .tuner_addr = ADDR_UNSET, 1022 .tuner_addr = ADDR_UNSET,
1036 .radio_addr = ADDR_UNSET, 1023 .radio_addr = ADDR_UNSET,
1037 .has_radio = 1, 1024 .has_radio = 1,
1038 .audio_hook = avermedia_tvphone_audio, 1025 .audio_mode_gpio= avermedia_tvphone_audio,
1039 }, 1026 },
1040 [BTTV_BOARD_PV951] = { 1027 [BTTV_BOARD_PV951] = {
1041 .name = "ProVideo PV951", /* pic16c54 */ 1028 .name = "ProVideo PV951", /* pic16c54 */
@@ -1167,7 +1154,7 @@ struct tvcard bttv_tvcards[] = {
1167 .tuner_type = TUNER_ALPS_TSHC6_NTSC, 1154 .tuner_type = TUNER_ALPS_TSHC6_NTSC,
1168 .tuner_addr = ADDR_UNSET, 1155 .tuner_addr = ADDR_UNSET,
1169 .radio_addr = ADDR_UNSET, 1156 .radio_addr = ADDR_UNSET,
1170 .audio_hook = gvbctv3pci_audio, 1157 .audio_mode_gpio= gvbctv3pci_audio,
1171 }, 1158 },
1172 [BTTV_BOARD_PXELVWPLTVPAK] = { 1159 [BTTV_BOARD_PXELVWPLTVPAK] = {
1173 .name = "Prolink PV-BT878P+4E / PixelView PlayTV PAK / Lenco MXTV-9578 CP", 1160 .name = "Prolink PV-BT878P+4E / PixelView PlayTV PAK / Lenco MXTV-9578 CP",
@@ -1472,7 +1459,7 @@ struct tvcard bttv_tvcards[] = {
1472 /* -dk-???: set mute=0x1800 for tda9874h daughterboard */ 1459 /* -dk-???: set mute=0x1800 for tda9874h daughterboard */
1473 .gpiomux = { 0x0000,0x0800,0x1000,0x1000 }, 1460 .gpiomux = { 0x0000,0x0800,0x1000,0x1000 },
1474 .gpiomute = 0x1800, 1461 .gpiomute = 0x1800,
1475 .audio_hook = fv2000s_audio, 1462 .audio_mode_gpio= fv2000s_audio,
1476 .no_msp34xx = 1, 1463 .no_msp34xx = 1,
1477 .no_tda9875 = 1, 1464 .no_tda9875 = 1,
1478 .needs_tvaudio = 1, 1465 .needs_tvaudio = 1,
@@ -1513,7 +1500,7 @@ struct tvcard bttv_tvcards[] = {
1513 .tuner_type = TUNER_SHARP_2U5JF5540_NTSC, 1500 .tuner_type = TUNER_SHARP_2U5JF5540_NTSC,
1514 .tuner_addr = ADDR_UNSET, 1501 .tuner_addr = ADDR_UNSET,
1515 .radio_addr = ADDR_UNSET, 1502 .radio_addr = ADDR_UNSET,
1516 .audio_hook = gvbctv3pci_audio, 1503 .audio_mode_gpio= gvbctv3pci_audio,
1517 }, 1504 },
1518 1505
1519 /* ---- card 0x44 ---------------------------------- */ 1506 /* ---- card 0x44 ---------------------------------- */
@@ -1632,7 +1619,7 @@ struct tvcard bttv_tvcards[] = {
1632 .tuner_type = TUNER_PHILIPS_PAL, 1619 .tuner_type = TUNER_PHILIPS_PAL,
1633 .tuner_addr = ADDR_UNSET, 1620 .tuner_addr = ADDR_UNSET,
1634 .radio_addr = ADDR_UNSET, 1621 .radio_addr = ADDR_UNSET,
1635 .audio_hook = pvbt878p9b_audio, /* Note: not all cards have stereo */ 1622 .audio_mode_gpio= pvbt878p9b_audio, /* Note: not all cards have stereo */
1636 .has_radio = 1, /* Note: not all cards have radio */ 1623 .has_radio = 1, /* Note: not all cards have radio */
1637 .has_remote = 1, 1624 .has_remote = 1,
1638 /* GPIO wiring: 1625 /* GPIO wiring:
@@ -1710,7 +1697,7 @@ struct tvcard bttv_tvcards[] = {
1710 .tuner_type = TUNER_PHILIPS_NTSC, 1697 .tuner_type = TUNER_PHILIPS_NTSC,
1711 .tuner_addr = ADDR_UNSET, 1698 .tuner_addr = ADDR_UNSET,
1712 .radio_addr = ADDR_UNSET, 1699 .radio_addr = ADDR_UNSET,
1713 .audio_hook = windvr_audio, 1700 .audio_mode_gpio= windvr_audio,
1714 }, 1701 },
1715 [BTTV_BOARD_GRANDTEC_MULTI] = { 1702 [BTTV_BOARD_GRANDTEC_MULTI] = {
1716 .name = "GrandTec Multi Capture Card (Bt878)", 1703 .name = "GrandTec Multi Capture Card (Bt878)",
@@ -1807,7 +1794,7 @@ struct tvcard bttv_tvcards[] = {
1807 .tuner_type = TUNER_PHILIPS_NTSC_M, 1794 .tuner_type = TUNER_PHILIPS_NTSC_M,
1808 .tuner_addr = ADDR_UNSET, 1795 .tuner_addr = ADDR_UNSET,
1809 .radio_addr = ADDR_UNSET, 1796 .radio_addr = ADDR_UNSET,
1810 .audio_hook = gvbctv5pci_audio, 1797 .audio_mode_gpio= gvbctv5pci_audio,
1811 .has_radio = 1, 1798 .has_radio = 1,
1812 }, 1799 },
1813 [BTTV_BOARD_OSPREY1x0] = { 1800 [BTTV_BOARD_OSPREY1x0] = {
@@ -2106,7 +2093,7 @@ struct tvcard bttv_tvcards[] = {
2106 .tuner_type = TUNER_PHILIPS_NTSC, 2093 .tuner_type = TUNER_PHILIPS_NTSC,
2107 .tuner_addr = ADDR_UNSET, 2094 .tuner_addr = ADDR_UNSET,
2108 .radio_addr = ADDR_UNSET, 2095 .radio_addr = ADDR_UNSET,
2109 .audio_hook = adtvk503_audio, 2096 .audio_mode_gpio= adtvk503_audio,
2110 }, 2097 },
2111 2098
2112 /* ---- card 0x64 ---------------------------------- */ 2099 /* ---- card 0x64 ---------------------------------- */
@@ -3173,8 +3160,8 @@ static void flyvideo_gpio(struct bttv *btv)
3173 /* LR90 Audio Routing is done by 2 hef4052, so Audio_Mask has 4 bits: 0x001c80 3160 /* LR90 Audio Routing is done by 2 hef4052, so Audio_Mask has 4 bits: 0x001c80
3174 * LR26/LR50 only has 1 hef4052, Audio_Mask 0x000c00 3161 * LR26/LR50 only has 1 hef4052, Audio_Mask 0x000c00
3175 * Audio options: from tuner, from tda9821/tda9821(mono,stereo,sap), from tda9874, ext., mute */ 3162 * Audio options: from tuner, from tda9821/tda9821(mono,stereo,sap), from tda9874, ext., mute */
3176 if(has_tda9820_tda9821) btv->audio_hook = lt9415_audio; 3163 if(has_tda9820_tda9821) btv->audio_mode_gpio = lt9415_audio;
3177 /* todo: if(has_tda9874) btv->audio_hook = fv2000s_audio; */ 3164 /* todo: if(has_tda9874) btv->audio_mode_gpio = fv2000s_audio; */
3178} 3165}
3179 3166
3180static int miro_tunermap[] = { 0,6,2,3, 4,5,6,0, 3,0,4,5, 5,2,16,1, 3167static int miro_tunermap[] = { 0,6,2,3, 4,5,6,0, 3,0,4,5, 5,2,16,1,
@@ -3574,8 +3561,12 @@ void __devinit bttv_init_card2(struct bttv *btv)
3574 } 3561 }
3575 3562
3576 if (btv->tda9887_conf) { 3563 if (btv->tda9887_conf) {
3577 bttv_call_i2c_clients(btv, TDA9887_SET_CONFIG, 3564 struct v4l2_priv_tun_config tda9887_cfg;
3578 &btv->tda9887_conf); 3565
3566 tda9887_cfg.tuner = TUNER_TDA9887;
3567 tda9887_cfg.priv = &btv->tda9887_conf;
3568
3569 bttv_call_i2c_clients(btv, TUNER_SET_CONFIG, &tda9887_cfg);
3579 } 3570 }
3580 3571
3581 btv->svhs = bttv_tvcards[btv->c.type].svhs; 3572 btv->svhs = bttv_tvcards[btv->c.type].svhs;
@@ -3590,8 +3581,10 @@ void __devinit bttv_init_card2(struct bttv *btv)
3590 btv->has_remote=1; 3581 btv->has_remote=1;
3591 if (!bttv_tvcards[btv->c.type].no_gpioirq) 3582 if (!bttv_tvcards[btv->c.type].no_gpioirq)
3592 btv->gpioirq=1; 3583 btv->gpioirq=1;
3593 if (bttv_tvcards[btv->c.type].audio_hook) 3584 if (bttv_tvcards[btv->c.type].volume_gpio)
3594 btv->audio_hook=bttv_tvcards[btv->c.type].audio_hook; 3585 btv->volume_gpio=bttv_tvcards[btv->c.type].volume_gpio;
3586 if (bttv_tvcards[btv->c.type].audio_mode_gpio)
3587 btv->audio_mode_gpio=bttv_tvcards[btv->c.type].audio_mode_gpio;
3595 3588
3596 if (bttv_tvcards[btv->c.type].digital_mode == DIGITAL_MODE_CAMERA) { 3589 if (bttv_tvcards[btv->c.type].digital_mode == DIGITAL_MODE_CAMERA) {
3597 /* detect Bt832 chip for quartzsight digital camera */ 3590 /* detect Bt832 chip for quartzsight digital camera */
@@ -3950,7 +3943,7 @@ static void __devinit avermedia_eeprom(struct bttv *btv)
3950void bttv_tda9880_setnorm(struct bttv *btv, int norm) 3943void bttv_tda9880_setnorm(struct bttv *btv, int norm)
3951{ 3944{
3952 /* fix up our card entry */ 3945 /* fix up our card entry */
3953 if(norm==VIDEO_MODE_NTSC) { 3946 if(norm==V4L2_STD_NTSC) {
3954 bttv_tvcards[BTTV_BOARD_VOODOOTV_FM].gpiomux[TVAUDIO_INPUT_TUNER]=0x957fff; 3947 bttv_tvcards[BTTV_BOARD_VOODOOTV_FM].gpiomux[TVAUDIO_INPUT_TUNER]=0x957fff;
3955 bttv_tvcards[BTTV_BOARD_VOODOOTV_FM].gpiomute=0x957fff; 3948 bttv_tvcards[BTTV_BOARD_VOODOOTV_FM].gpiomute=0x957fff;
3956 bttv_tvcards[BTTV_BOARD_VOODOOTV_200].gpiomux[TVAUDIO_INPUT_TUNER]=0x957fff; 3949 bttv_tvcards[BTTV_BOARD_VOODOOTV_200].gpiomux[TVAUDIO_INPUT_TUNER]=0x957fff;
@@ -4319,387 +4312,6 @@ void tea5757_set_freq(struct bttv *btv, unsigned short freq)
4319 tea5757_write(btv, 5 * freq + 0x358); /* add 10.7MHz (see docs) */ 4312 tea5757_write(btv, 5 * freq + 0x358); /* add 10.7MHz (see docs) */
4320} 4313}
4321 4314
4322
4323/* ----------------------------------------------------------------------- */
4324/* winview */
4325
4326static void winview_audio(struct bttv *btv, struct video_audio *v, int set)
4327{
4328 /* PT2254A programming Jon Tombs, jon@gte.esi.us.es */
4329 int bits_out, loops, vol, data;
4330
4331 if (!set) {
4332 /* Fixed by Leandro Lucarella <luca@linuxmendoza.org.ar (07/31/01) */
4333 v->flags |= VIDEO_AUDIO_VOLUME;
4334 return;
4335 }
4336
4337 /* 32 levels logarithmic */
4338 vol = 32 - ((v->volume>>11));
4339 /* units */
4340 bits_out = (PT2254_DBS_IN_2>>(vol%5));
4341 /* tens */
4342 bits_out |= (PT2254_DBS_IN_10>>(vol/5));
4343 bits_out |= PT2254_L_CHANNEL | PT2254_R_CHANNEL;
4344 data = gpio_read();
4345 data &= ~(WINVIEW_PT2254_CLK| WINVIEW_PT2254_DATA|
4346 WINVIEW_PT2254_STROBE);
4347 for (loops = 17; loops >= 0 ; loops--) {
4348 if (bits_out & (1<<loops))
4349 data |= WINVIEW_PT2254_DATA;
4350 else
4351 data &= ~WINVIEW_PT2254_DATA;
4352 gpio_write(data);
4353 udelay(5);
4354 data |= WINVIEW_PT2254_CLK;
4355 gpio_write(data);
4356 udelay(5);
4357 data &= ~WINVIEW_PT2254_CLK;
4358 gpio_write(data);
4359 }
4360 data |= WINVIEW_PT2254_STROBE;
4361 data &= ~WINVIEW_PT2254_DATA;
4362 gpio_write(data);
4363 udelay(10);
4364 data &= ~WINVIEW_PT2254_STROBE;
4365 gpio_write(data);
4366}
4367
4368/* ----------------------------------------------------------------------- */
4369/* mono/stereo control for various cards (which don't use i2c chips but */
4370/* connect something to the GPIO pins */
4371
4372static void
4373gvbctv3pci_audio(struct bttv *btv, struct video_audio *v, int set)
4374{
4375 unsigned int con = 0;
4376
4377 if (set) {
4378 gpio_inout(0x300, 0x300);
4379 if (v->mode & VIDEO_SOUND_LANG1)
4380 con = 0x000;
4381 if (v->mode & VIDEO_SOUND_LANG2)
4382 con = 0x300;
4383 if (v->mode & VIDEO_SOUND_STEREO)
4384 con = 0x200;
4385/* if (v->mode & VIDEO_SOUND_MONO)
4386 * con = 0x100; */
4387 gpio_bits(0x300, con);
4388 } else {
4389 v->mode = VIDEO_SOUND_STEREO |
4390 VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4391 }
4392}
4393
4394static void
4395gvbctv5pci_audio(struct bttv *btv, struct video_audio *v, int set)
4396{
4397 unsigned int val, con;
4398
4399 if (btv->radio_user)
4400 return;
4401
4402 val = gpio_read();
4403 if (set) {
4404 con = 0x000;
4405 if (v->mode & VIDEO_SOUND_LANG2) {
4406 if (v->mode & VIDEO_SOUND_LANG1) {
4407 /* LANG1 + LANG2 */
4408 con = 0x100;
4409 }
4410 else {
4411 /* LANG2 */
4412 con = 0x300;
4413 }
4414 }
4415 if (con != (val & 0x300)) {
4416 gpio_bits(0x300, con);
4417 if (bttv_gpio)
4418 bttv_gpio_tracking(btv,"gvbctv5pci");
4419 }
4420 } else {
4421 switch (val & 0x70) {
4422 case 0x10:
4423 v->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4424 break;
4425 case 0x30:
4426 v->mode = VIDEO_SOUND_LANG2;
4427 break;
4428 case 0x50:
4429 v->mode = VIDEO_SOUND_LANG1;
4430 break;
4431 case 0x60:
4432 v->mode = VIDEO_SOUND_STEREO;
4433 break;
4434 case 0x70:
4435 v->mode = VIDEO_SOUND_MONO;
4436 break;
4437 default:
4438 v->mode = VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO |
4439 VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4440 }
4441 }
4442}
4443
4444/*
4445 * Mario Medina Nussbaum <medisoft@alohabbs.org.mx>
4446 * I discover that on BT848_GPIO_DATA address a byte 0xcce enable stereo,
4447 * 0xdde enables mono and 0xccd enables sap
4448 *
4449 * Petr Vandrovec <VANDROVE@vc.cvut.cz>
4450 * P.S.: At least mask in line above is wrong - GPIO pins 3,2 select
4451 * input/output sound connection, so both must be set for output mode.
4452 *
4453 * Looks like it's needed only for the "tvphone", the "tvphone 98"
4454 * handles this with a tda9840
4455 *
4456 */
4457static void
4458avermedia_tvphone_audio(struct bttv *btv, struct video_audio *v, int set)
4459{
4460 int val = 0;
4461
4462 if (set) {
4463 if (v->mode & VIDEO_SOUND_LANG2) /* SAP */
4464 val = 0x02;
4465 if (v->mode & VIDEO_SOUND_STEREO)
4466 val = 0x01;
4467 if (val) {
4468 gpio_bits(0x03,val);
4469 if (bttv_gpio)
4470 bttv_gpio_tracking(btv,"avermedia");
4471 }
4472 } else {
4473 v->mode = VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO |
4474 VIDEO_SOUND_LANG1;
4475 return;
4476 }
4477}
4478
4479static void
4480avermedia_tv_stereo_audio(struct bttv *btv, struct video_audio *v, int set)
4481{
4482 int val = 0;
4483
4484 if (set) {
4485 if (v->mode & VIDEO_SOUND_LANG2) /* SAP */
4486 val = 0x01;
4487 if (v->mode & VIDEO_SOUND_STEREO) /* STEREO */
4488 val = 0x02;
4489 btaor(val, ~0x03, BT848_GPIO_DATA);
4490 if (bttv_gpio)
4491 bttv_gpio_tracking(btv,"avermedia");
4492 } else {
4493 v->mode = VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO |
4494 VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4495 return;
4496 }
4497}
4498
4499/* Lifetec 9415 handling */
4500static void
4501lt9415_audio(struct bttv *btv, struct video_audio *v, int set)
4502{
4503 int val = 0;
4504
4505 if (gpio_read() & 0x4000) {
4506 v->mode = VIDEO_SOUND_MONO;
4507 return;
4508 }
4509
4510 if (set) {
4511 if (v->mode & VIDEO_SOUND_LANG2) /* A2 SAP */
4512 val = 0x0080;
4513 if (v->mode & VIDEO_SOUND_STEREO) /* A2 stereo */
4514 val = 0x0880;
4515 if ((v->mode & VIDEO_SOUND_LANG1) ||
4516 (v->mode & VIDEO_SOUND_MONO))
4517 val = 0;
4518 gpio_bits(0x0880, val);
4519 if (bttv_gpio)
4520 bttv_gpio_tracking(btv,"lt9415");
4521 } else {
4522 /* autodetect doesn't work with this card :-( */
4523 v->mode = VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO |
4524 VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4525 return;
4526 }
4527}
4528
4529/* TDA9821 on TerraTV+ Bt848, Bt878 */
4530static void
4531terratv_audio(struct bttv *btv, struct video_audio *v, int set)
4532{
4533 unsigned int con = 0;
4534
4535 if (set) {
4536 gpio_inout(0x180000,0x180000);
4537 if (v->mode & VIDEO_SOUND_LANG2)
4538 con = 0x080000;
4539 if (v->mode & VIDEO_SOUND_STEREO)
4540 con = 0x180000;
4541 gpio_bits(0x180000, con);
4542 if (bttv_gpio)
4543 bttv_gpio_tracking(btv,"terratv");
4544 } else {
4545 v->mode = VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO |
4546 VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4547 }
4548}
4549
4550static void
4551winfast2000_audio(struct bttv *btv, struct video_audio *v, int set)
4552{
4553 unsigned long val = 0;
4554
4555 if (set) {
4556 /*btor (0xc32000, BT848_GPIO_OUT_EN);*/
4557 if (v->mode & VIDEO_SOUND_MONO) /* Mono */
4558 val = 0x420000;
4559 if (v->mode & VIDEO_SOUND_LANG1) /* Mono */
4560 val = 0x420000;
4561 if (v->mode & VIDEO_SOUND_LANG2) /* SAP */
4562 val = 0x410000;
4563 if (v->mode & VIDEO_SOUND_STEREO) /* Stereo */
4564 val = 0x020000;
4565 if (val) {
4566 gpio_bits(0x430000, val);
4567 if (bttv_gpio)
4568 bttv_gpio_tracking(btv,"winfast2000");
4569 }
4570 } else {
4571 v->mode = VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO |
4572 VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4573 }
4574}
4575
4576/*
4577 * Dariusz Kowalewski <darekk@automex.pl>
4578 * sound control for Prolink PV-BT878P+9B (PixelView PlayTV Pro FM+NICAM
4579 * revision 9B has on-board TDA9874A sound decoder).
4580 *
4581 * Note: There are card variants without tda9874a. Forcing the "stereo sound route"
4582 * will mute this cards.
4583 */
4584static void
4585pvbt878p9b_audio(struct bttv *btv, struct video_audio *v, int set)
4586{
4587 unsigned int val = 0;
4588
4589 if (btv->radio_user)
4590 return;
4591
4592 if (set) {
4593 if (v->mode & VIDEO_SOUND_MONO) {
4594 val = 0x01;
4595 }
4596 if ((v->mode & (VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2))
4597 || (v->mode & VIDEO_SOUND_STEREO)) {
4598 val = 0x02;
4599 }
4600 if (val) {
4601 gpio_bits(0x03,val);
4602 if (bttv_gpio)
4603 bttv_gpio_tracking(btv,"pvbt878p9b");
4604 }
4605 } else {
4606 v->mode = VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO |
4607 VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4608 }
4609}
4610
4611/*
4612 * Dariusz Kowalewski <darekk@automex.pl>
4613 * sound control for FlyVideo 2000S (with tda9874 decoder)
4614 * based on pvbt878p9b_audio() - this is not tested, please fix!!!
4615 */
4616static void
4617fv2000s_audio(struct bttv *btv, struct video_audio *v, int set)
4618{
4619 unsigned int val = 0xffff;
4620
4621 if (btv->radio_user)
4622 return;
4623 if (set) {
4624 if (v->mode & VIDEO_SOUND_MONO) {
4625 val = 0x0000;
4626 }
4627 if ((v->mode & (VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2))
4628 || (v->mode & VIDEO_SOUND_STEREO)) {
4629 val = 0x1080; /*-dk-???: 0x0880, 0x0080, 0x1800 ... */
4630 }
4631 if (val != 0xffff) {
4632 gpio_bits(0x1800, val);
4633 if (bttv_gpio)
4634 bttv_gpio_tracking(btv,"fv2000s");
4635 }
4636 } else {
4637 v->mode = VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO |
4638 VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4639 }
4640}
4641
4642/*
4643 * sound control for Canopus WinDVR PCI
4644 * Masaki Suzuki <masaki@btree.org>
4645 */
4646static void
4647windvr_audio(struct bttv *btv, struct video_audio *v, int set)
4648{
4649 unsigned long val = 0;
4650
4651 if (set) {
4652 if (v->mode & VIDEO_SOUND_MONO)
4653 val = 0x040000;
4654 if (v->mode & VIDEO_SOUND_LANG1)
4655 val = 0;
4656 if (v->mode & VIDEO_SOUND_LANG2)
4657 val = 0x100000;
4658 if (v->mode & VIDEO_SOUND_STEREO)
4659 val = 0;
4660 if (val) {
4661 gpio_bits(0x140000, val);
4662 if (bttv_gpio)
4663 bttv_gpio_tracking(btv,"windvr");
4664 }
4665 } else {
4666 v->mode = VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO |
4667 VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4668 }
4669}
4670
4671/*
4672 * sound control for AD-TVK503
4673 * Hiroshi Takekawa <sian@big.or.jp>
4674 */
4675static void
4676adtvk503_audio(struct bttv *btv, struct video_audio *v, int set)
4677{
4678 unsigned int con = 0xffffff;
4679
4680 /* btaor(0x1e0000, ~0x1e0000, BT848_GPIO_OUT_EN); */
4681
4682 if (set) {
4683 /* btor(***, BT848_GPIO_OUT_EN); */
4684 if (v->mode & VIDEO_SOUND_LANG1)
4685 con = 0x00000000;
4686 if (v->mode & VIDEO_SOUND_LANG2)
4687 con = 0x00180000;
4688 if (v->mode & VIDEO_SOUND_STEREO)
4689 con = 0x00000000;
4690 if (v->mode & VIDEO_SOUND_MONO)
4691 con = 0x00060000;
4692 if (con != 0xffffff) {
4693 gpio_bits(0x1e0000,con);
4694 if (bttv_gpio)
4695 bttv_gpio_tracking(btv, "adtvk503");
4696 }
4697 } else {
4698 v->mode = VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO |
4699 VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
4700 }
4701}
4702
4703/* RemoteVision MX (rv605) muxsel helper [Miguel Freitas] 4315/* RemoteVision MX (rv605) muxsel helper [Miguel Freitas]
4704 * 4316 *
4705 * This is needed because rv605 don't use a normal multiplex, but a crosspoint 4317 * This is needed because rv605 don't use a normal multiplex, but a crosspoint
diff --git a/drivers/media/video/bt8xx/bttv-driver.c b/drivers/media/video/bt8xx/bttv-driver.c
index 581a3c955739..907dc62c1783 100644
--- a/drivers/media/video/bt8xx/bttv-driver.c
+++ b/drivers/media/video/bt8xx/bttv-driver.c
@@ -9,6 +9,12 @@
9 some v4l2 code lines are taken from Justin's bttv2 driver which is 9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za> 10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
11 11
12 V4L1 removal from:
13 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
14
15 Fixes to be fully V4L2 compliant by
16 (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
17
12 Cropping and overscan support 18 Cropping and overscan support
13 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at> 19 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
14 Sponsored by OPQ Systems AB 20 Sponsored by OPQ Systems AB
@@ -157,7 +163,7 @@ MODULE_LICENSE("GPL");
157static ssize_t show_card(struct device *cd, 163static ssize_t show_card(struct device *cd,
158 struct device_attribute *attr, char *buf) 164 struct device_attribute *attr, char *buf)
159{ 165{
160 struct video_device *vfd = to_video_device(cd); 166 struct video_device *vfd = container_of(cd, struct video_device, class_dev);
161 struct bttv *btv = dev_get_drvdata(vfd->dev); 167 struct bttv *btv = dev_get_drvdata(vfd->dev);
162 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET); 168 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
163} 169}
@@ -470,31 +476,27 @@ static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
470/* ----------------------------------------------------------------------- */ 476/* ----------------------------------------------------------------------- */
471/* bttv format list 477/* bttv format list
472 packed pixel formats must come first */ 478 packed pixel formats must come first */
473static const struct bttv_format bttv_formats[] = { 479static const struct bttv_format formats[] = {
474 { 480 {
475 .name = "8 bpp, gray", 481 .name = "8 bpp, gray",
476 .palette = VIDEO_PALETTE_GREY,
477 .fourcc = V4L2_PIX_FMT_GREY, 482 .fourcc = V4L2_PIX_FMT_GREY,
478 .btformat = BT848_COLOR_FMT_Y8, 483 .btformat = BT848_COLOR_FMT_Y8,
479 .depth = 8, 484 .depth = 8,
480 .flags = FORMAT_FLAGS_PACKED, 485 .flags = FORMAT_FLAGS_PACKED,
481 },{ 486 },{
482 .name = "8 bpp, dithered color", 487 .name = "8 bpp, dithered color",
483 .palette = VIDEO_PALETTE_HI240,
484 .fourcc = V4L2_PIX_FMT_HI240, 488 .fourcc = V4L2_PIX_FMT_HI240,
485 .btformat = BT848_COLOR_FMT_RGB8, 489 .btformat = BT848_COLOR_FMT_RGB8,
486 .depth = 8, 490 .depth = 8,
487 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER, 491 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
488 },{ 492 },{
489 .name = "15 bpp RGB, le", 493 .name = "15 bpp RGB, le",
490 .palette = VIDEO_PALETTE_RGB555,
491 .fourcc = V4L2_PIX_FMT_RGB555, 494 .fourcc = V4L2_PIX_FMT_RGB555,
492 .btformat = BT848_COLOR_FMT_RGB15, 495 .btformat = BT848_COLOR_FMT_RGB15,
493 .depth = 16, 496 .depth = 16,
494 .flags = FORMAT_FLAGS_PACKED, 497 .flags = FORMAT_FLAGS_PACKED,
495 },{ 498 },{
496 .name = "15 bpp RGB, be", 499 .name = "15 bpp RGB, be",
497 .palette = -1,
498 .fourcc = V4L2_PIX_FMT_RGB555X, 500 .fourcc = V4L2_PIX_FMT_RGB555X,
499 .btformat = BT848_COLOR_FMT_RGB15, 501 .btformat = BT848_COLOR_FMT_RGB15,
500 .btswap = 0x03, /* byteswap */ 502 .btswap = 0x03, /* byteswap */
@@ -502,14 +504,12 @@ static const struct bttv_format bttv_formats[] = {
502 .flags = FORMAT_FLAGS_PACKED, 504 .flags = FORMAT_FLAGS_PACKED,
503 },{ 505 },{
504 .name = "16 bpp RGB, le", 506 .name = "16 bpp RGB, le",
505 .palette = VIDEO_PALETTE_RGB565,
506 .fourcc = V4L2_PIX_FMT_RGB565, 507 .fourcc = V4L2_PIX_FMT_RGB565,
507 .btformat = BT848_COLOR_FMT_RGB16, 508 .btformat = BT848_COLOR_FMT_RGB16,
508 .depth = 16, 509 .depth = 16,
509 .flags = FORMAT_FLAGS_PACKED, 510 .flags = FORMAT_FLAGS_PACKED,
510 },{ 511 },{
511 .name = "16 bpp RGB, be", 512 .name = "16 bpp RGB, be",
512 .palette = -1,
513 .fourcc = V4L2_PIX_FMT_RGB565X, 513 .fourcc = V4L2_PIX_FMT_RGB565X,
514 .btformat = BT848_COLOR_FMT_RGB16, 514 .btformat = BT848_COLOR_FMT_RGB16,
515 .btswap = 0x03, /* byteswap */ 515 .btswap = 0x03, /* byteswap */
@@ -517,21 +517,18 @@ static const struct bttv_format bttv_formats[] = {
517 .flags = FORMAT_FLAGS_PACKED, 517 .flags = FORMAT_FLAGS_PACKED,
518 },{ 518 },{
519 .name = "24 bpp RGB, le", 519 .name = "24 bpp RGB, le",
520 .palette = VIDEO_PALETTE_RGB24,
521 .fourcc = V4L2_PIX_FMT_BGR24, 520 .fourcc = V4L2_PIX_FMT_BGR24,
522 .btformat = BT848_COLOR_FMT_RGB24, 521 .btformat = BT848_COLOR_FMT_RGB24,
523 .depth = 24, 522 .depth = 24,
524 .flags = FORMAT_FLAGS_PACKED, 523 .flags = FORMAT_FLAGS_PACKED,
525 },{ 524 },{
526 .name = "32 bpp RGB, le", 525 .name = "32 bpp RGB, le",
527 .palette = VIDEO_PALETTE_RGB32,
528 .fourcc = V4L2_PIX_FMT_BGR32, 526 .fourcc = V4L2_PIX_FMT_BGR32,
529 .btformat = BT848_COLOR_FMT_RGB32, 527 .btformat = BT848_COLOR_FMT_RGB32,
530 .depth = 32, 528 .depth = 32,
531 .flags = FORMAT_FLAGS_PACKED, 529 .flags = FORMAT_FLAGS_PACKED,
532 },{ 530 },{
533 .name = "32 bpp RGB, be", 531 .name = "32 bpp RGB, be",
534 .palette = -1,
535 .fourcc = V4L2_PIX_FMT_RGB32, 532 .fourcc = V4L2_PIX_FMT_RGB32,
536 .btformat = BT848_COLOR_FMT_RGB32, 533 .btformat = BT848_COLOR_FMT_RGB32,
537 .btswap = 0x0f, /* byte+word swap */ 534 .btswap = 0x0f, /* byte+word swap */
@@ -539,21 +536,18 @@ static const struct bttv_format bttv_formats[] = {
539 .flags = FORMAT_FLAGS_PACKED, 536 .flags = FORMAT_FLAGS_PACKED,
540 },{ 537 },{
541 .name = "4:2:2, packed, YUYV", 538 .name = "4:2:2, packed, YUYV",
542 .palette = VIDEO_PALETTE_YUV422,
543 .fourcc = V4L2_PIX_FMT_YUYV, 539 .fourcc = V4L2_PIX_FMT_YUYV,
544 .btformat = BT848_COLOR_FMT_YUY2, 540 .btformat = BT848_COLOR_FMT_YUY2,
545 .depth = 16, 541 .depth = 16,
546 .flags = FORMAT_FLAGS_PACKED, 542 .flags = FORMAT_FLAGS_PACKED,
547 },{ 543 },{
548 .name = "4:2:2, packed, YUYV", 544 .name = "4:2:2, packed, YUYV",
549 .palette = VIDEO_PALETTE_YUYV,
550 .fourcc = V4L2_PIX_FMT_YUYV, 545 .fourcc = V4L2_PIX_FMT_YUYV,
551 .btformat = BT848_COLOR_FMT_YUY2, 546 .btformat = BT848_COLOR_FMT_YUY2,
552 .depth = 16, 547 .depth = 16,
553 .flags = FORMAT_FLAGS_PACKED, 548 .flags = FORMAT_FLAGS_PACKED,
554 },{ 549 },{
555 .name = "4:2:2, packed, UYVY", 550 .name = "4:2:2, packed, UYVY",
556 .palette = VIDEO_PALETTE_UYVY,
557 .fourcc = V4L2_PIX_FMT_UYVY, 551 .fourcc = V4L2_PIX_FMT_UYVY,
558 .btformat = BT848_COLOR_FMT_YUY2, 552 .btformat = BT848_COLOR_FMT_YUY2,
559 .btswap = 0x03, /* byteswap */ 553 .btswap = 0x03, /* byteswap */
@@ -561,7 +555,6 @@ static const struct bttv_format bttv_formats[] = {
561 .flags = FORMAT_FLAGS_PACKED, 555 .flags = FORMAT_FLAGS_PACKED,
562 },{ 556 },{
563 .name = "4:2:2, planar, Y-Cb-Cr", 557 .name = "4:2:2, planar, Y-Cb-Cr",
564 .palette = VIDEO_PALETTE_YUV422P,
565 .fourcc = V4L2_PIX_FMT_YUV422P, 558 .fourcc = V4L2_PIX_FMT_YUV422P,
566 .btformat = BT848_COLOR_FMT_YCrCb422, 559 .btformat = BT848_COLOR_FMT_YCrCb422,
567 .depth = 16, 560 .depth = 16,
@@ -570,7 +563,6 @@ static const struct bttv_format bttv_formats[] = {
570 .vshift = 0, 563 .vshift = 0,
571 },{ 564 },{
572 .name = "4:2:0, planar, Y-Cb-Cr", 565 .name = "4:2:0, planar, Y-Cb-Cr",
573 .palette = VIDEO_PALETTE_YUV420P,
574 .fourcc = V4L2_PIX_FMT_YUV420, 566 .fourcc = V4L2_PIX_FMT_YUV420,
575 .btformat = BT848_COLOR_FMT_YCrCb422, 567 .btformat = BT848_COLOR_FMT_YCrCb422,
576 .depth = 12, 568 .depth = 12,
@@ -579,7 +571,6 @@ static const struct bttv_format bttv_formats[] = {
579 .vshift = 1, 571 .vshift = 1,
580 },{ 572 },{
581 .name = "4:2:0, planar, Y-Cr-Cb", 573 .name = "4:2:0, planar, Y-Cr-Cb",
582 .palette = -1,
583 .fourcc = V4L2_PIX_FMT_YVU420, 574 .fourcc = V4L2_PIX_FMT_YVU420,
584 .btformat = BT848_COLOR_FMT_YCrCb422, 575 .btformat = BT848_COLOR_FMT_YCrCb422,
585 .depth = 12, 576 .depth = 12,
@@ -588,7 +579,6 @@ static const struct bttv_format bttv_formats[] = {
588 .vshift = 1, 579 .vshift = 1,
589 },{ 580 },{
590 .name = "4:1:1, planar, Y-Cb-Cr", 581 .name = "4:1:1, planar, Y-Cb-Cr",
591 .palette = VIDEO_PALETTE_YUV411P,
592 .fourcc = V4L2_PIX_FMT_YUV411P, 582 .fourcc = V4L2_PIX_FMT_YUV411P,
593 .btformat = BT848_COLOR_FMT_YCrCb411, 583 .btformat = BT848_COLOR_FMT_YCrCb411,
594 .depth = 12, 584 .depth = 12,
@@ -597,7 +587,6 @@ static const struct bttv_format bttv_formats[] = {
597 .vshift = 0, 587 .vshift = 0,
598 },{ 588 },{
599 .name = "4:1:0, planar, Y-Cb-Cr", 589 .name = "4:1:0, planar, Y-Cb-Cr",
600 .palette = VIDEO_PALETTE_YUV410P,
601 .fourcc = V4L2_PIX_FMT_YUV410, 590 .fourcc = V4L2_PIX_FMT_YUV410,
602 .btformat = BT848_COLOR_FMT_YCrCb411, 591 .btformat = BT848_COLOR_FMT_YCrCb411,
603 .depth = 9, 592 .depth = 9,
@@ -606,7 +595,6 @@ static const struct bttv_format bttv_formats[] = {
606 .vshift = 2, 595 .vshift = 2,
607 },{ 596 },{
608 .name = "4:1:0, planar, Y-Cr-Cb", 597 .name = "4:1:0, planar, Y-Cr-Cb",
609 .palette = -1,
610 .fourcc = V4L2_PIX_FMT_YVU410, 598 .fourcc = V4L2_PIX_FMT_YVU410,
611 .btformat = BT848_COLOR_FMT_YCrCb411, 599 .btformat = BT848_COLOR_FMT_YCrCb411,
612 .depth = 9, 600 .depth = 9,
@@ -615,14 +603,13 @@ static const struct bttv_format bttv_formats[] = {
615 .vshift = 2, 603 .vshift = 2,
616 },{ 604 },{
617 .name = "raw scanlines", 605 .name = "raw scanlines",
618 .palette = VIDEO_PALETTE_RAW,
619 .fourcc = -1, 606 .fourcc = -1,
620 .btformat = BT848_COLOR_FMT_RAW, 607 .btformat = BT848_COLOR_FMT_RAW,
621 .depth = 8, 608 .depth = 8,
622 .flags = FORMAT_FLAGS_RAW, 609 .flags = FORMAT_FLAGS_RAW,
623 } 610 }
624}; 611};
625static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats); 612static const unsigned int FORMATS = ARRAY_SIZE(formats);
626 613
627/* ----------------------------------------------------------------------- */ 614/* ----------------------------------------------------------------------- */
628 615
@@ -798,7 +785,17 @@ static const struct v4l2_queryctrl bttv_ctls[] = {
798 785
799 786
800}; 787};
801static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls); 788
789static const struct v4l2_queryctrl *ctrl_by_id(int id)
790{
791 int i;
792
793 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
794 if (bttv_ctls[i].id == id)
795 return bttv_ctls+i;
796
797 return NULL;
798}
802 799
803/* ----------------------------------------------------------------------- */ 800/* ----------------------------------------------------------------------- */
804/* resource management */ 801/* resource management */
@@ -1255,16 +1252,6 @@ audio_input(struct bttv *btv, int input)
1255} 1252}
1256 1253
1257static void 1254static void
1258i2c_vidiocschan(struct bttv *btv)
1259{
1260 v4l2_std_id std = bttv_tvnorms[btv->tvnorm].v4l2_id;
1261
1262 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &std);
1263 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM || btv->c.type == BTTV_BOARD_VOODOOTV_200)
1264 bttv_tda9880_setnorm(btv,btv->tvnorm);
1265}
1266
1267static void
1268bttv_crop_calc_limits(struct bttv_crop *c) 1255bttv_crop_calc_limits(struct bttv_crop *c)
1269{ 1256{
1270 /* Scale factor min. 1:1, max. 16:1. Min. image size 1257 /* Scale factor min. 1:1, max. 16:1. Min. image size
@@ -1298,6 +1285,7 @@ static int
1298set_tvnorm(struct bttv *btv, unsigned int norm) 1285set_tvnorm(struct bttv *btv, unsigned int norm)
1299{ 1286{
1300 const struct bttv_tvnorm *tvnorm; 1287 const struct bttv_tvnorm *tvnorm;
1288 v4l2_std_id id;
1301 1289
1302 if (norm < 0 || norm >= BTTV_TVNORMS) 1290 if (norm < 0 || norm >= BTTV_TVNORMS)
1303 return -EINVAL; 1291 return -EINVAL;
@@ -1334,6 +1322,9 @@ set_tvnorm(struct bttv *btv, unsigned int norm)
1334 bttv_tda9880_setnorm(btv,norm); 1322 bttv_tda9880_setnorm(btv,norm);
1335 break; 1323 break;
1336 } 1324 }
1325 id = tvnorm->v4l2_id;
1326 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &id);
1327
1337 return 0; 1328 return 0;
1338} 1329}
1339 1330
@@ -1359,7 +1350,6 @@ set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1359 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ? 1350 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1360 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN)); 1351 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
1361 set_tvnorm(btv, norm); 1352 set_tvnorm(btv, norm);
1362 i2c_vidiocschan(btv);
1363} 1353}
1364 1354
1365static void init_irqreg(struct bttv *btv) 1355static void init_irqreg(struct bttv *btv)
@@ -1452,38 +1442,12 @@ static void bttv_reinit_bt848(struct bttv *btv)
1452 set_input(btv, btv->input, btv->tvnorm); 1442 set_input(btv, btv->input, btv->tvnorm);
1453} 1443}
1454 1444
1455static int get_control(struct bttv *btv, struct v4l2_control *c) 1445static int bttv_g_ctrl(struct file *file, void *priv,
1446 struct v4l2_control *c)
1456{ 1447{
1457 struct video_audio va; 1448 struct bttv_fh *fh = priv;
1458 int i; 1449 struct bttv *btv = fh->btv;
1459 1450
1460 for (i = 0; i < BTTV_CTLS; i++)
1461 if (bttv_ctls[i].id == c->id)
1462 break;
1463 if (i == BTTV_CTLS)
1464 return -EINVAL;
1465 if (btv->audio_hook && i >= 4 && i <= 8) {
1466 memset(&va,0,sizeof(va));
1467 btv->audio_hook(btv,&va,0);
1468 switch (c->id) {
1469 case V4L2_CID_AUDIO_MUTE:
1470 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1471 break;
1472 case V4L2_CID_AUDIO_VOLUME:
1473 c->value = va.volume;
1474 break;
1475 case V4L2_CID_AUDIO_BALANCE:
1476 c->value = va.balance;
1477 break;
1478 case V4L2_CID_AUDIO_BASS:
1479 c->value = va.bass;
1480 break;
1481 case V4L2_CID_AUDIO_TREBLE:
1482 c->value = va.treble;
1483 break;
1484 }
1485 return 0;
1486 }
1487 switch (c->id) { 1451 switch (c->id) {
1488 case V4L2_CID_BRIGHTNESS: 1452 case V4L2_CID_BRIGHTNESS:
1489 c->value = btv->bright; 1453 c->value = btv->bright;
@@ -1503,7 +1467,7 @@ static int get_control(struct bttv *btv, struct v4l2_control *c)
1503 case V4L2_CID_AUDIO_BALANCE: 1467 case V4L2_CID_AUDIO_BALANCE:
1504 case V4L2_CID_AUDIO_BASS: 1468 case V4L2_CID_AUDIO_BASS:
1505 case V4L2_CID_AUDIO_TREBLE: 1469 case V4L2_CID_AUDIO_TREBLE:
1506 bttv_call_i2c_clients(btv,VIDIOC_G_CTRL,c); 1470 bttv_call_i2c_clients(btv, VIDIOC_G_CTRL, c);
1507 break; 1471 break;
1508 1472
1509 case V4L2_CID_PRIVATE_CHROMA_AGC: 1473 case V4L2_CID_PRIVATE_CHROMA_AGC:
@@ -1545,67 +1509,44 @@ static int get_control(struct bttv *btv, struct v4l2_control *c)
1545 return 0; 1509 return 0;
1546} 1510}
1547 1511
1548static int set_control(struct bttv *btv, struct v4l2_control *c) 1512static int bttv_s_ctrl(struct file *file, void *f,
1513 struct v4l2_control *c)
1549{ 1514{
1550 struct video_audio va; 1515 int err;
1551 int i,val; 1516 int val;
1517 struct bttv_fh *fh = f;
1518 struct bttv *btv = fh->btv;
1552 1519
1553 for (i = 0; i < BTTV_CTLS; i++) 1520 err = v4l2_prio_check(&btv->prio, &fh->prio);
1554 if (bttv_ctls[i].id == c->id) 1521 if (0 != err)
1555 break; 1522 return err;
1556 if (i == BTTV_CTLS)
1557 return -EINVAL;
1558 if (btv->audio_hook && i >= 4 && i <= 8) {
1559 memset(&va,0,sizeof(va));
1560 btv->audio_hook(btv,&va,0);
1561 switch (c->id) {
1562 case V4L2_CID_AUDIO_MUTE:
1563 if (c->value) {
1564 va.flags |= VIDEO_AUDIO_MUTE;
1565 audio_mute(btv, 1);
1566 } else {
1567 va.flags &= ~VIDEO_AUDIO_MUTE;
1568 audio_mute(btv, 0);
1569 }
1570 break;
1571 1523
1572 case V4L2_CID_AUDIO_VOLUME:
1573 va.volume = c->value;
1574 break;
1575 case V4L2_CID_AUDIO_BALANCE:
1576 va.balance = c->value;
1577 break;
1578 case V4L2_CID_AUDIO_BASS:
1579 va.bass = c->value;
1580 break;
1581 case V4L2_CID_AUDIO_TREBLE:
1582 va.treble = c->value;
1583 break;
1584 }
1585 btv->audio_hook(btv,&va,1);
1586 return 0;
1587 }
1588 switch (c->id) { 1524 switch (c->id) {
1589 case V4L2_CID_BRIGHTNESS: 1525 case V4L2_CID_BRIGHTNESS:
1590 bt848_bright(btv,c->value); 1526 bt848_bright(btv, c->value);
1591 break; 1527 break;
1592 case V4L2_CID_HUE: 1528 case V4L2_CID_HUE:
1593 bt848_hue(btv,c->value); 1529 bt848_hue(btv, c->value);
1594 break; 1530 break;
1595 case V4L2_CID_CONTRAST: 1531 case V4L2_CID_CONTRAST:
1596 bt848_contrast(btv,c->value); 1532 bt848_contrast(btv, c->value);
1597 break; 1533 break;
1598 case V4L2_CID_SATURATION: 1534 case V4L2_CID_SATURATION:
1599 bt848_sat(btv,c->value); 1535 bt848_sat(btv, c->value);
1600 break; 1536 break;
1601 case V4L2_CID_AUDIO_MUTE: 1537 case V4L2_CID_AUDIO_MUTE:
1602 audio_mute(btv, c->value); 1538 audio_mute(btv, c->value);
1603 /* fall through */ 1539 /* fall through */
1604 case V4L2_CID_AUDIO_VOLUME: 1540 case V4L2_CID_AUDIO_VOLUME:
1541 if (btv->volume_gpio)
1542 btv->volume_gpio(btv, c->value);
1543
1544 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, c);
1545 break;
1605 case V4L2_CID_AUDIO_BALANCE: 1546 case V4L2_CID_AUDIO_BALANCE:
1606 case V4L2_CID_AUDIO_BASS: 1547 case V4L2_CID_AUDIO_BASS:
1607 case V4L2_CID_AUDIO_TREBLE: 1548 case V4L2_CID_AUDIO_TREBLE:
1608 bttv_call_i2c_clients(btv,VIDIOC_S_CTRL,c); 1549 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, c);
1609 break; 1550 break;
1610 1551
1611 case V4L2_CID_PRIVATE_CHROMA_AGC: 1552 case V4L2_CID_PRIVATE_CHROMA_AGC:
@@ -1632,8 +1573,9 @@ static int set_control(struct bttv *btv, struct v4l2_control *c)
1632 break; 1573 break;
1633 case V4L2_CID_PRIVATE_AGC_CRUSH: 1574 case V4L2_CID_PRIVATE_AGC_CRUSH:
1634 btv->opt_adc_crush = c->value; 1575 btv->opt_adc_crush = c->value;
1635 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0), 1576 btwrite(BT848_ADC_RESERVED |
1636 BT848_ADC); 1577 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1578 BT848_ADC);
1637 break; 1579 break;
1638 case V4L2_CID_PRIVATE_VCR_HACK: 1580 case V4L2_CID_PRIVATE_VCR_HACK:
1639 btv->opt_vcr_hack = c->value; 1581 btv->opt_vcr_hack = c->value;
@@ -1693,29 +1635,15 @@ static void bttv_field_count(struct bttv *btv)
1693} 1635}
1694 1636
1695static const struct bttv_format* 1637static const struct bttv_format*
1696format_by_palette(int palette)
1697{
1698 unsigned int i;
1699
1700 for (i = 0; i < BTTV_FORMATS; i++) {
1701 if (-1 == bttv_formats[i].palette)
1702 continue;
1703 if (bttv_formats[i].palette == palette)
1704 return bttv_formats+i;
1705 }
1706 return NULL;
1707}
1708
1709static const struct bttv_format*
1710format_by_fourcc(int fourcc) 1638format_by_fourcc(int fourcc)
1711{ 1639{
1712 unsigned int i; 1640 unsigned int i;
1713 1641
1714 for (i = 0; i < BTTV_FORMATS; i++) { 1642 for (i = 0; i < FORMATS; i++) {
1715 if (-1 == bttv_formats[i].fourcc) 1643 if (-1 == formats[i].fourcc)
1716 continue; 1644 continue;
1717 if (bttv_formats[i].fourcc == fourcc) 1645 if (formats[i].fourcc == fourcc)
1718 return bttv_formats+i; 1646 return formats+i;
1719 } 1647 }
1720 return NULL; 1648 return NULL;
1721} 1649}
@@ -1733,7 +1661,7 @@ bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1733 1661
1734 dprintk("switch_overlay: enter [new=%p]\n",new); 1662 dprintk("switch_overlay: enter [new=%p]\n",new);
1735 if (new) 1663 if (new)
1736 new->vb.state = STATE_DONE; 1664 new->vb.state = VIDEOBUF_DONE;
1737 spin_lock_irqsave(&btv->s_lock,flags); 1665 spin_lock_irqsave(&btv->s_lock,flags);
1738 old = btv->screen; 1666 old = btv->screen;
1739 btv->screen = new; 1667 btv->screen = new;
@@ -1844,7 +1772,7 @@ static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1844 } 1772 }
1845 1773
1846 /* alloc risc memory */ 1774 /* alloc risc memory */
1847 if (STATE_NEEDS_INIT == buf->vb.state) { 1775 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1848 redo_dma_risc = 1; 1776 redo_dma_risc = 1;
1849 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf))) 1777 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1850 goto fail; 1778 goto fail;
@@ -1854,7 +1782,7 @@ static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1854 if (0 != (rc = bttv_buffer_risc(btv,buf))) 1782 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1855 goto fail; 1783 goto fail;
1856 1784
1857 buf->vb.state = STATE_PREPARED; 1785 buf->vb.state = VIDEOBUF_PREPARED;
1858 return 0; 1786 return 0;
1859 1787
1860 fail: 1788 fail:
@@ -1893,7 +1821,7 @@ buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1893 struct bttv_fh *fh = q->priv_data; 1821 struct bttv_fh *fh = q->priv_data;
1894 struct bttv *btv = fh->btv; 1822 struct bttv *btv = fh->btv;
1895 1823
1896 buf->vb.state = STATE_QUEUED; 1824 buf->vb.state = VIDEOBUF_QUEUED;
1897 list_add_tail(&buf->vb.queue,&btv->capture); 1825 list_add_tail(&buf->vb.queue,&btv->capture);
1898 if (!btv->curr.frame_irq) { 1826 if (!btv->curr.frame_irq) {
1899 btv->loop_irq |= 1; 1827 btv->loop_irq |= 1;
@@ -1916,374 +1844,234 @@ static struct videobuf_queue_ops bttv_video_qops = {
1916 .buf_release = buffer_release, 1844 .buf_release = buffer_release,
1917}; 1845};
1918 1846
1919static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg) 1847static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
1920{ 1848{
1921 switch (cmd) { 1849 struct bttv_fh *fh = priv;
1922 case BTTV_VERSION: 1850 struct bttv *btv = fh->btv;
1923 return BTTV_VERSION_CODE; 1851 unsigned int i;
1852 int err;
1924 1853
1925 /* *** v4l1 *** ************************************************ */ 1854 err = v4l2_prio_check(&btv->prio, &fh->prio);
1926 case VIDIOCGFREQ: 1855 if (0 != err)
1927 { 1856 return err;
1928 unsigned long *freq = arg;
1929 *freq = btv->freq;
1930 return 0;
1931 }
1932 case VIDIOCSFREQ:
1933 {
1934 struct v4l2_frequency freq;
1935 1857
1936 memset(&freq, 0, sizeof(freq)); 1858 for (i = 0; i < BTTV_TVNORMS; i++)
1937 freq.frequency = *(unsigned long *)arg; 1859 if (*id & bttv_tvnorms[i].v4l2_id)
1938 mutex_lock(&btv->lock); 1860 break;
1939 freq.type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 1861 if (i == BTTV_TVNORMS)
1940 btv->freq = *(unsigned long *)arg; 1862 return -EINVAL;
1941 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,&freq);
1942 if (btv->has_matchbox && btv->radio_user)
1943 tea5757_set_freq(btv,*(unsigned long *)arg);
1944 mutex_unlock(&btv->lock);
1945 return 0;
1946 }
1947 1863
1948 case VIDIOCGTUNER: 1864 mutex_lock(&btv->lock);
1949 { 1865 set_tvnorm(btv, i);
1950 struct video_tuner *v = arg; 1866 mutex_unlock(&btv->lock);
1951 1867
1952 if (UNSET == bttv_tvcards[btv->c.type].tuner) 1868 return 0;
1953 return -EINVAL; 1869}
1954 if (v->tuner) /* Only tuner 0 */
1955 return -EINVAL;
1956 strcpy(v->name, "Television");
1957 v->rangelow = 0;
1958 v->rangehigh = 0x7FFFFFFF;
1959 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1960 v->mode = btv->tvnorm;
1961 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1962 bttv_call_i2c_clients(btv,cmd,v);
1963 return 0;
1964 }
1965 case VIDIOCSTUNER:
1966 {
1967 struct video_tuner *v = arg;
1968 1870
1969 if (v->tuner) /* Only tuner 0 */ 1871static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1970 return -EINVAL; 1872{
1971 if (v->mode >= BTTV_TVNORMS) 1873 struct bttv_fh *fh = f;
1972 return -EINVAL; 1874 struct bttv *btv = fh->btv;
1973 1875
1974 mutex_lock(&btv->lock); 1876 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1975 set_tvnorm(btv,v->mode); 1877 *id = V4L2_STD_625_50;
1976 bttv_call_i2c_clients(btv,cmd,v); 1878 else
1977 mutex_unlock(&btv->lock); 1879 *id = V4L2_STD_525_60;
1978 return 0; 1880 return 0;
1979 } 1881}
1980 1882
1981 case VIDIOCGCHAN: 1883static int bttv_enum_input(struct file *file, void *priv,
1982 { 1884 struct v4l2_input *i)
1983 struct video_channel *v = arg; 1885{
1984 unsigned int channel = v->channel; 1886 struct bttv_fh *fh = priv;
1887 struct bttv *btv = fh->btv;
1888 unsigned int n;
1985 1889
1986 if (channel >= bttv_tvcards[btv->c.type].video_inputs) 1890 n = i->index;
1987 return -EINVAL;
1988 v->tuners=0;
1989 v->flags = VIDEO_VC_AUDIO;
1990 v->type = VIDEO_TYPE_CAMERA;
1991 v->norm = btv->tvnorm;
1992 if (channel == bttv_tvcards[btv->c.type].tuner) {
1993 strcpy(v->name,"Television");
1994 v->flags|=VIDEO_VC_TUNER;
1995 v->type=VIDEO_TYPE_TV;
1996 v->tuners=1;
1997 } else if (channel == btv->svhs) {
1998 strcpy(v->name,"S-Video");
1999 } else {
2000 sprintf(v->name,"Composite%d",channel);
2001 }
2002 return 0;
2003 }
2004 case VIDIOCSCHAN:
2005 {
2006 struct video_channel *v = arg;
2007 unsigned int channel = v->channel;
2008 1891
2009 if (channel >= bttv_tvcards[btv->c.type].video_inputs) 1892 if (n >= bttv_tvcards[btv->c.type].video_inputs)
2010 return -EINVAL; 1893 return -EINVAL;
2011 if (v->norm >= BTTV_TVNORMS)
2012 return -EINVAL;
2013 1894
2014 mutex_lock(&btv->lock); 1895 memset(i, 0, sizeof(*i));
2015 if (channel == btv->input &&
2016 v->norm == btv->tvnorm) {
2017 /* nothing to do */
2018 mutex_unlock(&btv->lock);
2019 return 0;
2020 }
2021 1896
2022 set_input(btv, v->channel, v->norm); 1897 i->index = n;
2023 mutex_unlock(&btv->lock); 1898 i->type = V4L2_INPUT_TYPE_CAMERA;
2024 return 0; 1899 i->audioset = 1;
1900
1901 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1902 sprintf(i->name, "Television");
1903 i->type = V4L2_INPUT_TYPE_TUNER;
1904 i->tuner = 0;
1905 } else if (i->index == btv->svhs) {
1906 sprintf(i->name, "S-Video");
1907 } else {
1908 sprintf(i->name, "Composite%d", i->index);
2025 } 1909 }
2026 1910
2027 case VIDIOCGAUDIO: 1911 if (i->index == btv->input) {
2028 { 1912 __u32 dstatus = btread(BT848_DSTATUS);
2029 struct video_audio *v = arg; 1913 if (0 == (dstatus & BT848_DSTATUS_PRES))
1914 i->status |= V4L2_IN_ST_NO_SIGNAL;
1915 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1916 i->status |= V4L2_IN_ST_NO_H_LOCK;
1917 }
2030 1918
2031 memset(v,0,sizeof(*v)); 1919 for (n = 0; n < BTTV_TVNORMS; n++)
2032 strcpy(v->name,"Television"); 1920 i->std |= bttv_tvnorms[n].v4l2_id;
2033 v->flags |= VIDEO_AUDIO_MUTABLE;
2034 v->mode = VIDEO_SOUND_MONO;
2035 1921
2036 mutex_lock(&btv->lock); 1922 return 0;
2037 bttv_call_i2c_clients(btv,cmd,v); 1923}
2038 1924
2039 /* card specific hooks */ 1925static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
2040 if (btv->audio_hook) 1926{
2041 btv->audio_hook(btv,v,0); 1927 struct bttv_fh *fh = priv;
1928 struct bttv *btv = fh->btv;
2042 1929
2043 mutex_unlock(&btv->lock); 1930 *i = btv->input;
2044 return 0; 1931 return 0;
2045 } 1932}
2046 case VIDIOCSAUDIO:
2047 {
2048 struct video_audio *v = arg;
2049 unsigned int audio = v->audio;
2050 1933
2051 if (audio >= bttv_tvcards[btv->c.type].audio_inputs) 1934static int bttv_s_input(struct file *file, void *priv, unsigned int i)
2052 return -EINVAL; 1935{
1936 struct bttv_fh *fh = priv;
1937 struct bttv *btv = fh->btv;
2053 1938
2054 mutex_lock(&btv->lock); 1939 int err;
2055 audio_mute(btv, (v->flags&VIDEO_AUDIO_MUTE) ? 1 : 0);
2056 bttv_call_i2c_clients(btv,cmd,v);
2057 1940
2058 /* card specific hooks */ 1941 err = v4l2_prio_check(&btv->prio, &fh->prio);
2059 if (btv->audio_hook) 1942 if (0 != err)
2060 btv->audio_hook(btv,v,1); 1943 return err;
2061 1944
2062 mutex_unlock(&btv->lock); 1945 if (i > bttv_tvcards[btv->c.type].video_inputs)
2063 return 0; 1946 return -EINVAL;
2064 }
2065 1947
2066 /* *** v4l2 *** ************************************************ */ 1948 mutex_lock(&btv->lock);
2067 case VIDIOC_ENUMSTD: 1949 set_input(btv, i, btv->tvnorm);
2068 { 1950 mutex_unlock(&btv->lock);
2069 struct v4l2_standard *e = arg; 1951 return 0;
2070 unsigned int index = e->index; 1952}
2071 1953
2072 if (index >= BTTV_TVNORMS) 1954static int bttv_s_tuner(struct file *file, void *priv,
2073 return -EINVAL; 1955 struct v4l2_tuner *t)
2074 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id, 1956{
2075 bttv_tvnorms[e->index].name); 1957 struct bttv_fh *fh = priv;
2076 e->index = index; 1958 struct bttv *btv = fh->btv;
2077 return 0; 1959 int err;
2078 }
2079 case VIDIOC_G_STD:
2080 {
2081 v4l2_std_id *id = arg;
2082 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
2083 return 0;
2084 }
2085 case VIDIOC_S_STD:
2086 {
2087 v4l2_std_id *id = arg;
2088 unsigned int i;
2089 1960
2090 for (i = 0; i < BTTV_TVNORMS; i++) 1961 err = v4l2_prio_check(&btv->prio, &fh->prio);
2091 if (*id & bttv_tvnorms[i].v4l2_id) 1962 if (0 != err)
2092 break; 1963 return err;
2093 if (i == BTTV_TVNORMS)
2094 return -EINVAL;
2095 1964
2096 mutex_lock(&btv->lock); 1965 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2097 set_tvnorm(btv,i); 1966 return -EINVAL;
2098 i2c_vidiocschan(btv);
2099 mutex_unlock(&btv->lock);
2100 return 0;
2101 }
2102 case VIDIOC_QUERYSTD:
2103 {
2104 v4l2_std_id *id = arg;
2105 1967
2106 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML) 1968 if (0 != t->index)
2107 *id = V4L2_STD_625_50; 1969 return -EINVAL;
2108 else
2109 *id = V4L2_STD_525_60;
2110 return 0;
2111 }
2112 1970
2113 case VIDIOC_ENUMINPUT: 1971 mutex_lock(&btv->lock);
2114 { 1972 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
2115 struct v4l2_input *i = arg;
2116 unsigned int n;
2117 1973
2118 n = i->index; 1974 if (btv->audio_mode_gpio)
2119 if (n >= bttv_tvcards[btv->c.type].video_inputs) 1975 btv->audio_mode_gpio(btv, t, 1);
2120 return -EINVAL;
2121 memset(i,0,sizeof(*i));
2122 i->index = n;
2123 i->type = V4L2_INPUT_TYPE_CAMERA;
2124 i->audioset = 1;
2125 if (i->index == bttv_tvcards[btv->c.type].tuner) {
2126 sprintf(i->name, "Television");
2127 i->type = V4L2_INPUT_TYPE_TUNER;
2128 i->tuner = 0;
2129 } else if (i->index == btv->svhs) {
2130 sprintf(i->name, "S-Video");
2131 } else {
2132 sprintf(i->name,"Composite%d",i->index);
2133 }
2134 if (i->index == btv->input) {
2135 __u32 dstatus = btread(BT848_DSTATUS);
2136 if (0 == (dstatus & BT848_DSTATUS_PRES))
2137 i->status |= V4L2_IN_ST_NO_SIGNAL;
2138 if (0 == (dstatus & BT848_DSTATUS_HLOC))
2139 i->status |= V4L2_IN_ST_NO_H_LOCK;
2140 }
2141 for (n = 0; n < BTTV_TVNORMS; n++)
2142 i->std |= bttv_tvnorms[n].v4l2_id;
2143 return 0;
2144 }
2145 case VIDIOC_G_INPUT:
2146 {
2147 int *i = arg;
2148 *i = btv->input;
2149 return 0;
2150 }
2151 case VIDIOC_S_INPUT:
2152 {
2153 unsigned int *i = arg;
2154 1976
2155 if (*i > bttv_tvcards[btv->c.type].video_inputs) 1977 mutex_unlock(&btv->lock);
2156 return -EINVAL;
2157 mutex_lock(&btv->lock);
2158 set_input(btv, *i, btv->tvnorm);
2159 mutex_unlock(&btv->lock);
2160 return 0;
2161 }
2162 1978
2163 case VIDIOC_G_TUNER: 1979 return 0;
2164 { 1980}
2165 struct v4l2_tuner *t = arg;
2166 1981
2167 if (UNSET == bttv_tvcards[btv->c.type].tuner) 1982static int bttv_g_frequency(struct file *file, void *priv,
2168 return -EINVAL; 1983 struct v4l2_frequency *f)
2169 if (0 != t->index) 1984{
2170 return -EINVAL; 1985 struct bttv_fh *fh = priv;
2171 mutex_lock(&btv->lock); 1986 struct bttv *btv = fh->btv;
2172 memset(t,0,sizeof(*t)); 1987 int err;
2173 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2174 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
2175 strcpy(t->name, "Television");
2176 t->capability = V4L2_TUNER_CAP_NORM;
2177 t->type = V4L2_TUNER_ANALOG_TV;
2178 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2179 t->signal = 0xffff;
2180
2181 if (btv->audio_hook) {
2182 /* Hmmm ... */
2183 struct video_audio va;
2184 memset(&va, 0, sizeof(struct video_audio));
2185 btv->audio_hook(btv,&va,0);
2186 t->audmode = V4L2_TUNER_MODE_MONO;
2187 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2188 if(va.mode & VIDEO_SOUND_STEREO) {
2189 t->audmode = V4L2_TUNER_MODE_STEREO;
2190 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
2191 }
2192 if(va.mode & VIDEO_SOUND_LANG2) {
2193 t->audmode = V4L2_TUNER_MODE_LANG1;
2194 t->rxsubchans = V4L2_TUNER_SUB_LANG1
2195 | V4L2_TUNER_SUB_LANG2;
2196 }
2197 }
2198 /* FIXME: fill capability+audmode */
2199 mutex_unlock(&btv->lock);
2200 return 0;
2201 }
2202 case VIDIOC_S_TUNER:
2203 {
2204 struct v4l2_tuner *t = arg;
2205 1988
2206 if (UNSET == bttv_tvcards[btv->c.type].tuner) 1989 err = v4l2_prio_check(&btv->prio, &fh->prio);
2207 return -EINVAL; 1990 if (0 != err)
2208 if (0 != t->index) 1991 return err;
2209 return -EINVAL;
2210 mutex_lock(&btv->lock);
2211 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
2212 if (btv->audio_hook) {
2213 struct video_audio va;
2214 memset(&va, 0, sizeof(struct video_audio));
2215 if (t->audmode == V4L2_TUNER_MODE_MONO)
2216 va.mode = VIDEO_SOUND_MONO;
2217 else if (t->audmode == V4L2_TUNER_MODE_STEREO ||
2218 t->audmode == V4L2_TUNER_MODE_LANG1_LANG2)
2219 va.mode = VIDEO_SOUND_STEREO;
2220 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
2221 va.mode = VIDEO_SOUND_LANG1;
2222 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
2223 va.mode = VIDEO_SOUND_LANG2;
2224 btv->audio_hook(btv,&va,1);
2225 }
2226 mutex_unlock(&btv->lock);
2227 return 0;
2228 }
2229 1992
2230 case VIDIOC_G_FREQUENCY: 1993 f->type = V4L2_TUNER_ANALOG_TV;
2231 { 1994 f->frequency = btv->freq;
2232 struct v4l2_frequency *f = arg;
2233 1995
2234 memset(f,0,sizeof(*f)); 1996 return 0;
2235 f->type = V4L2_TUNER_ANALOG_TV; 1997}
2236 f->frequency = btv->freq; 1998
2237 return 0; 1999static int bttv_s_frequency(struct file *file, void *priv,
2238 } 2000 struct v4l2_frequency *f)
2239 case VIDIOC_S_FREQUENCY: 2001{
2240 { 2002 struct bttv_fh *fh = priv;
2241 struct v4l2_frequency *f = arg; 2003 struct bttv *btv = fh->btv;
2004 int err;
2005
2006 err = v4l2_prio_check(&btv->prio, &fh->prio);
2007 if (0 != err)
2008 return err;
2009
2010 if (unlikely(f->tuner != 0))
2011 return -EINVAL;
2012 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
2013 return -EINVAL;
2014 mutex_lock(&btv->lock);
2015 btv->freq = f->frequency;
2016 bttv_call_i2c_clients(btv, VIDIOC_S_FREQUENCY, f);
2017 if (btv->has_matchbox && btv->radio_user)
2018 tea5757_set_freq(btv, btv->freq);
2019 mutex_unlock(&btv->lock);
2020 return 0;
2021}
2022
2023static int bttv_log_status(struct file *file, void *f)
2024{
2025 struct bttv_fh *fh = f;
2026 struct bttv *btv = fh->btv;
2027
2028 printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
2029 btv->c.nr, btv->c.nr);
2030 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
2031 printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
2032 btv->c.nr, btv->c.nr);
2033 return 0;
2034}
2242 2035
2243 if (unlikely(f->tuner != 0))
2244 return -EINVAL;
2245 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
2246 return -EINVAL;
2247 mutex_lock(&btv->lock);
2248 btv->freq = f->frequency;
2249 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,f);
2250 if (btv->has_matchbox && btv->radio_user)
2251 tea5757_set_freq(btv,btv->freq);
2252 mutex_unlock(&btv->lock);
2253 return 0;
2254 }
2255 case VIDIOC_LOG_STATUS:
2256 {
2257 printk(KERN_INFO "bttv%d: ================= START STATUS CARD #%d =================\n", btv->c.nr, btv->c.nr);
2258 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
2259 printk(KERN_INFO "bttv%d: ================== END STATUS CARD #%d ==================\n", btv->c.nr, btv->c.nr);
2260 return 0;
2261 }
2262#ifdef CONFIG_VIDEO_ADV_DEBUG 2036#ifdef CONFIG_VIDEO_ADV_DEBUG
2263 case VIDIOC_DBG_G_REGISTER: 2037static int bttv_g_register(struct file *file, void *f,
2264 case VIDIOC_DBG_S_REGISTER: 2038 struct v4l2_register *reg)
2265 { 2039{
2266 struct v4l2_register *reg = arg; 2040 struct bttv_fh *fh = f;
2267 if (!capable(CAP_SYS_ADMIN)) 2041 struct bttv *btv = fh->btv;
2268 return -EPERM;
2269 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2270 return -EINVAL;
2271 /* bt848 has a 12-bit register space */
2272 reg->reg &= 0xfff;
2273 if (cmd == VIDIOC_DBG_G_REGISTER)
2274 reg->val = btread(reg->reg);
2275 else
2276 btwrite(reg->val, reg->reg);
2277 return 0;
2278 }
2279#endif
2280 2042
2281 default: 2043 if (!capable(CAP_SYS_ADMIN))
2282 return -ENOIOCTLCMD; 2044 return -EPERM;
2045
2046 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2047 return -EINVAL;
2048
2049 /* bt848 has a 12-bit register space */
2050 reg->reg &= 0xfff;
2051 reg->val = btread(reg->reg);
2052
2053 return 0;
2054}
2055
2056static int bttv_s_register(struct file *file, void *f,
2057 struct v4l2_register *reg)
2058{
2059 struct bttv_fh *fh = f;
2060 struct bttv *btv = fh->btv;
2061
2062 if (!capable(CAP_SYS_ADMIN))
2063 return -EPERM;
2064
2065 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2066 return -EINVAL;
2067
2068 /* bt848 has a 12-bit register space */
2069 reg->reg &= 0xfff;
2070 btwrite(reg->val, reg->reg);
2283 2071
2284 }
2285 return 0; 2072 return 0;
2286} 2073}
2074#endif
2287 2075
2288/* Given cropping boundaries b and the scaled width and height of a 2076/* Given cropping boundaries b and the scaled width and height of a
2289 single field or frame, which must not exceed hardware limits, this 2077 single field or frame, which must not exceed hardware limits, this
@@ -2659,983 +2447,681 @@ pix_format_set_size (struct v4l2_pix_format * f,
2659 } 2447 }
2660} 2448}
2661 2449
2662static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f) 2450static int bttv_g_fmt_cap(struct file *file, void *priv,
2451 struct v4l2_format *f)
2663{ 2452{
2664 switch (f->type) { 2453 struct bttv_fh *fh = priv;
2665 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 2454
2666 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format)); 2455 pix_format_set_size(&f->fmt.pix, fh->fmt,
2667 pix_format_set_size (&f->fmt.pix, fh->fmt, 2456 fh->width, fh->height);
2668 fh->width, fh->height); 2457 f->fmt.pix.field = fh->cap.field;
2669 f->fmt.pix.field = fh->cap.field; 2458 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2670 f->fmt.pix.pixelformat = fh->fmt->fourcc; 2459
2671 return 0; 2460 return 0;
2672 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2673 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2674 f->fmt.win.w = fh->ov.w;
2675 f->fmt.win.field = fh->ov.field;
2676 return 0;
2677 case V4L2_BUF_TYPE_VBI_CAPTURE:
2678 bttv_vbi_get_fmt(fh, &f->fmt.vbi);
2679 return 0;
2680 default:
2681 return -EINVAL;
2682 }
2683} 2461}
2684 2462
2685static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv, 2463static int bttv_g_fmt_overlay(struct file *file, void *priv,
2686 struct v4l2_format *f, int adjust_crop) 2464 struct v4l2_format *f)
2687{ 2465{
2688 switch (f->type) { 2466 struct bttv_fh *fh = priv;
2689 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2690 {
2691 const struct bttv_format *fmt;
2692 enum v4l2_field field;
2693 __s32 width, height;
2694 int rc;
2695 2467
2696 fmt = format_by_fourcc(f->fmt.pix.pixelformat); 2468 f->fmt.win.w = fh->ov.w;
2697 if (NULL == fmt) 2469 f->fmt.win.field = fh->ov.field;
2698 return -EINVAL;
2699 2470
2700 field = f->fmt.pix.field; 2471 return 0;
2701 if (V4L2_FIELD_ANY == field) { 2472}
2702 __s32 height2;
2703 2473
2704 height2 = btv->crop[!!fh->do_crop].rect.height >> 1; 2474static int bttv_try_fmt_cap(struct file *file, void *priv,
2705 field = (f->fmt.pix.height > height2) 2475 struct v4l2_format *f)
2706 ? V4L2_FIELD_INTERLACED 2476{
2707 : V4L2_FIELD_BOTTOM; 2477 const struct bttv_format *fmt;
2708 } 2478 struct bttv_fh *fh = priv;
2709 if (V4L2_FIELD_SEQ_BT == field) 2479 struct bttv *btv = fh->btv;
2710 field = V4L2_FIELD_SEQ_TB; 2480 enum v4l2_field field;
2711 switch (field) { 2481 __s32 width, height;
2712 case V4L2_FIELD_TOP: 2482 int rc;
2713 case V4L2_FIELD_BOTTOM:
2714 case V4L2_FIELD_ALTERNATE:
2715 case V4L2_FIELD_INTERLACED:
2716 break;
2717 case V4L2_FIELD_SEQ_TB:
2718 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2719 return -EINVAL;
2720 break;
2721 default:
2722 return -EINVAL;
2723 }
2724 2483
2725 width = f->fmt.pix.width; 2484 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2726 height = f->fmt.pix.height; 2485 if (NULL == fmt)
2486 return -EINVAL;
2727 2487
2728 rc = limit_scaled_size(fh, &width, &height, field, 2488 field = f->fmt.pix.field;
2729 /* width_mask: 4 pixels */ ~3,
2730 /* width_bias: nearest */ 2,
2731 /* adjust_size */ 1,
2732 adjust_crop);
2733 if (0 != rc)
2734 return rc;
2735 2489
2736 /* update data for the application */ 2490 if (V4L2_FIELD_ANY == field) {
2737 f->fmt.pix.field = field; 2491 __s32 height2;
2738 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2739 2492
2740 return 0; 2493 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2494 field = (f->fmt.pix.height > height2)
2495 ? V4L2_FIELD_INTERLACED
2496 : V4L2_FIELD_BOTTOM;
2741 } 2497 }
2742 case V4L2_BUF_TYPE_VIDEO_OVERLAY: 2498
2743 return verify_window(fh, &f->fmt.win, 2499 if (V4L2_FIELD_SEQ_BT == field)
2744 /* adjust_size */ 1, 2500 field = V4L2_FIELD_SEQ_TB;
2745 /* adjust_crop */ 0); 2501
2746 case V4L2_BUF_TYPE_VBI_CAPTURE: 2502 switch (field) {
2747 return bttv_vbi_try_fmt(fh, &f->fmt.vbi); 2503 case V4L2_FIELD_TOP:
2504 case V4L2_FIELD_BOTTOM:
2505 case V4L2_FIELD_ALTERNATE:
2506 case V4L2_FIELD_INTERLACED:
2507 break;
2508 case V4L2_FIELD_SEQ_TB:
2509 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2510 return -EINVAL;
2511 break;
2748 default: 2512 default:
2749 return -EINVAL; 2513 return -EINVAL;
2750 } 2514 }
2515
2516 width = f->fmt.pix.width;
2517 height = f->fmt.pix.height;
2518
2519 rc = limit_scaled_size(fh, &width, &height, field,
2520 /* width_mask: 4 pixels */ ~3,
2521 /* width_bias: nearest */ 2,
2522 /* adjust_size */ 1,
2523 /* adjust_crop */ 0);
2524 if (0 != rc)
2525 return rc;
2526
2527 /* update data for the application */
2528 f->fmt.pix.field = field;
2529 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2530
2531 return 0;
2751} 2532}
2752 2533
2753static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv, 2534static int bttv_try_fmt_overlay(struct file *file, void *priv,
2754 struct v4l2_format *f) 2535 struct v4l2_format *f)
2536{
2537 struct bttv_fh *fh = priv;
2538
2539 return verify_window(fh, &f->fmt.win,
2540 /* adjust_size */ 1,
2541 /* adjust_crop */ 0);
2542}
2543
2544static int bttv_s_fmt_cap(struct file *file, void *priv,
2545 struct v4l2_format *f)
2755{ 2546{
2756 int retval; 2547 int retval;
2548 const struct bttv_format *fmt;
2549 struct bttv_fh *fh = priv;
2550 struct bttv *btv = fh->btv;
2551 __s32 width, height;
2552 enum v4l2_field field;
2757 2553
2758 switch (f->type) { 2554 retval = bttv_switch_type(fh, f->type);
2759 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 2555 if (0 != retval)
2760 { 2556 return retval;
2761 const struct bttv_format *fmt;
2762 2557
2763 retval = bttv_switch_type(fh,f->type); 2558 retval = bttv_try_fmt_cap(file, priv, f);
2764 if (0 != retval) 2559 if (0 != retval)
2765 return retval; 2560 return retval;
2766 retval = bttv_try_fmt(fh,btv,f, /* adjust_crop */ 1);
2767 if (0 != retval)
2768 return retval;
2769 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2770 2561
2771 /* update our state informations */ 2562 width = f->fmt.pix.width;
2772 mutex_lock(&fh->cap.lock); 2563 height = f->fmt.pix.height;
2773 fh->fmt = fmt; 2564 field = f->fmt.pix.field;
2774 fh->cap.field = f->fmt.pix.field;
2775 fh->cap.last = V4L2_FIELD_NONE;
2776 fh->width = f->fmt.pix.width;
2777 fh->height = f->fmt.pix.height;
2778 btv->init.fmt = fmt;
2779 btv->init.width = f->fmt.pix.width;
2780 btv->init.height = f->fmt.pix.height;
2781 mutex_unlock(&fh->cap.lock);
2782 2565
2783 return 0; 2566 retval = limit_scaled_size(fh, &width, &height, f->fmt.pix.field,
2784 } 2567 /* width_mask: 4 pixels */ ~3,
2785 case V4L2_BUF_TYPE_VIDEO_OVERLAY: 2568 /* width_bias: nearest */ 2,
2786 if (no_overlay > 0) { 2569 /* adjust_size */ 1,
2787 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n"); 2570 /* adjust_crop */ 1);
2788 return -EINVAL; 2571 if (0 != retval)
2789 } 2572 return retval;
2790 return setup_window(fh, btv, &f->fmt.win, 1);
2791 case V4L2_BUF_TYPE_VBI_CAPTURE:
2792 retval = bttv_switch_type(fh,f->type);
2793 if (0 != retval)
2794 return retval;
2795 return bttv_vbi_set_fmt(fh, &f->fmt.vbi);
2796 default:
2797 return -EINVAL;
2798 }
2799}
2800 2573
2801static int bttv_do_ioctl(struct inode *inode, struct file *file, 2574 f->fmt.pix.field = field;
2802 unsigned int cmd, void *arg)
2803{
2804 struct bttv_fh *fh = file->private_data;
2805 struct bttv *btv = fh->btv;
2806 unsigned long flags;
2807 int retval = 0;
2808 2575
2809 if (bttv_debug > 1) 2576 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2810 v4l_print_ioctl(btv->c.name, cmd);
2811
2812 if (btv->errors)
2813 bttv_reinit_bt848(btv);
2814
2815 switch (cmd) {
2816 case VIDIOCSFREQ:
2817 case VIDIOCSTUNER:
2818 case VIDIOCSCHAN:
2819 case VIDIOC_S_CTRL:
2820 case VIDIOC_S_STD:
2821 case VIDIOC_S_INPUT:
2822 case VIDIOC_S_TUNER:
2823 case VIDIOC_S_FREQUENCY:
2824 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2825 if (0 != retval)
2826 return retval;
2827 };
2828 2577
2829 switch (cmd) { 2578 /* update our state informations */
2579 mutex_lock(&fh->cap.lock);
2580 fh->fmt = fmt;
2581 fh->cap.field = f->fmt.pix.field;
2582 fh->cap.last = V4L2_FIELD_NONE;
2583 fh->width = f->fmt.pix.width;
2584 fh->height = f->fmt.pix.height;
2585 btv->init.fmt = fmt;
2586 btv->init.width = f->fmt.pix.width;
2587 btv->init.height = f->fmt.pix.height;
2588 mutex_unlock(&fh->cap.lock);
2830 2589
2831 /* *** v4l1 *** ************************************************ */ 2590 return 0;
2832 case VIDIOCGCAP: 2591}
2833 {
2834 struct video_capability *cap = arg;
2835 2592
2836 memset(cap,0,sizeof(*cap)); 2593static int bttv_s_fmt_overlay(struct file *file, void *priv,
2837 strcpy(cap->name,btv->video_dev->name); 2594 struct v4l2_format *f)
2838 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) { 2595{
2839 /* vbi */ 2596 struct bttv_fh *fh = priv;
2840 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT; 2597 struct bttv *btv = fh->btv;
2841 } else {
2842 /* others */
2843 cap->type = VID_TYPE_CAPTURE|
2844 VID_TYPE_TUNER|
2845 VID_TYPE_CLIPPING|
2846 VID_TYPE_SCALES;
2847 if (no_overlay <= 0)
2848 cap->type |= VID_TYPE_OVERLAY;
2849
2850 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2851 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2852 cap->minwidth = 48;
2853 cap->minheight = 32;
2854 }
2855 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2856 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2857 return 0;
2858 }
2859 2598
2860 case VIDIOCGPICT: 2599 if (no_overlay > 0) {
2861 { 2600 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2862 struct video_picture *pic = arg; 2601 return -EINVAL;
2863
2864 memset(pic,0,sizeof(*pic));
2865 pic->brightness = btv->bright;
2866 pic->contrast = btv->contrast;
2867 pic->hue = btv->hue;
2868 pic->colour = btv->saturation;
2869 if (fh->fmt) {
2870 pic->depth = fh->fmt->depth;
2871 pic->palette = fh->fmt->palette;
2872 }
2873 return 0;
2874 } 2602 }
2875 case VIDIOCSPICT:
2876 {
2877 struct video_picture *pic = arg;
2878 const struct bttv_format *fmt;
2879 2603
2880 fmt = format_by_palette(pic->palette); 2604 return setup_window(fh, btv, &f->fmt.win, 1);
2881 if (NULL == fmt) 2605}
2882 return -EINVAL; 2606
2883 mutex_lock(&fh->cap.lock); 2607#ifdef CONFIG_VIDEO_V4L1_COMPAT
2884 if (fmt->flags & FORMAT_FLAGS_RAW) { 2608static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2885 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL * 2609{
2886 RAW_LINES * 2. F1 is stored at offset 0, F2 2610 int retval;
2887 at buffer size / 2. */ 2611 unsigned int i;
2888 fh->width = RAW_BPL; 2612 struct bttv_fh *fh = priv;
2889 fh->height = gbufsize / RAW_BPL; 2613
2890 btv->init.width = RAW_BPL; 2614 mutex_lock(&fh->cap.lock);
2891 btv->init.height = gbufsize / RAW_BPL; 2615 retval = videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
2892 } 2616 V4L2_MEMORY_MMAP);
2893 fh->ovfmt = fmt; 2617 if (retval < 0) {
2894 fh->fmt = fmt;
2895 btv->init.ovfmt = fmt;
2896 btv->init.fmt = fmt;
2897 if (bigendian) {
2898 /* dirty hack time: swap bytes for overlay if the
2899 display adaptor is big endian (insmod option) */
2900 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2901 fmt->palette == VIDEO_PALETTE_RGB565 ||
2902 fmt->palette == VIDEO_PALETTE_RGB32) {
2903 fh->ovfmt = fmt+1;
2904 }
2905 }
2906 bt848_bright(btv,pic->brightness);
2907 bt848_contrast(btv,pic->contrast);
2908 bt848_hue(btv,pic->hue);
2909 bt848_sat(btv,pic->colour);
2910 mutex_unlock(&fh->cap.lock); 2618 mutex_unlock(&fh->cap.lock);
2911 return 0; 2619 return retval;
2912 } 2620 }
2913 2621
2914 case VIDIOCGWIN: 2622 gbuffers = retval;
2915 { 2623 memset(mbuf, 0, sizeof(*mbuf));
2916 struct video_window *win = arg; 2624 mbuf->frames = gbuffers;
2625 mbuf->size = gbuffers * gbufsize;
2917 2626
2918 memset(win,0,sizeof(*win)); 2627 for (i = 0; i < gbuffers; i++)
2919 win->x = fh->ov.w.left; 2628 mbuf->offsets[i] = i * gbufsize;
2920 win->y = fh->ov.w.top;
2921 win->width = fh->ov.w.width;
2922 win->height = fh->ov.w.height;
2923 return 0;
2924 }
2925 case VIDIOCSWIN:
2926 {
2927 struct video_window *win = arg;
2928 struct v4l2_window w2;
2929 2629
2930 if (no_overlay > 0) { 2630 mutex_unlock(&fh->cap.lock);
2931 printk ("VIDIOCSWIN: no_overlay\n"); 2631 return 0;
2932 return -EINVAL; 2632}
2933 } 2633#endif
2934 2634
2935 w2.field = V4L2_FIELD_ANY; 2635static int bttv_querycap(struct file *file, void *priv,
2936 w2.w.left = win->x; 2636 struct v4l2_capability *cap)
2937 w2.w.top = win->y; 2637{
2938 w2.w.width = win->width; 2638 struct bttv_fh *fh = priv;
2939 w2.w.height = win->height; 2639 struct bttv *btv = fh->btv;
2940 w2.clipcount = win->clipcount;
2941 w2.clips = (struct v4l2_clip __user *)win->clips;
2942 retval = setup_window(fh, btv, &w2, 0);
2943 if (0 == retval) {
2944 /* on v4l1 this ioctl affects the read() size too */
2945 fh->width = fh->ov.w.width;
2946 fh->height = fh->ov.w.height;
2947 btv->init.width = fh->ov.w.width;
2948 btv->init.height = fh->ov.w.height;
2949 }
2950 return retval;
2951 }
2952 2640
2953 case VIDIOCGFBUF: 2641 if (0 == v4l2)
2954 { 2642 return -EINVAL;
2955 struct video_buffer *fbuf = arg;
2956
2957 fbuf->base = btv->fbuf.base;
2958 fbuf->width = btv->fbuf.fmt.width;
2959 fbuf->height = btv->fbuf.fmt.height;
2960 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2961 if (fh->ovfmt)
2962 fbuf->depth = fh->ovfmt->depth;
2963 else {
2964 if (fbuf->width)
2965 fbuf->depth = ((fbuf->bytesperline<<3)
2966 + (fbuf->width-1) )
2967 /fbuf->width;
2968 else
2969 fbuf->depth = 0;
2970 }
2971 return 0;
2972 }
2973 case VIDIOCSFBUF:
2974 {
2975 struct video_buffer *fbuf = arg;
2976 const struct bttv_format *fmt;
2977 unsigned long end;
2978
2979 if(!capable(CAP_SYS_ADMIN) &&
2980 !capable(CAP_SYS_RAWIO))
2981 return -EPERM;
2982 end = (unsigned long)fbuf->base +
2983 fbuf->height * fbuf->bytesperline;
2984 mutex_lock(&fh->cap.lock);
2985 retval = -EINVAL;
2986 2643
2987 switch (fbuf->depth) { 2644 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2988 case 8: 2645 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2989 fmt = format_by_palette(VIDEO_PALETTE_HI240); 2646 snprintf(cap->bus_info, sizeof(cap->bus_info),
2990 break; 2647 "PCI:%s", pci_name(btv->c.pci));
2991 case 16: 2648 cap->version = BTTV_VERSION_CODE;
2992 fmt = format_by_palette(VIDEO_PALETTE_RGB565); 2649 cap->capabilities =
2993 break; 2650 V4L2_CAP_VIDEO_CAPTURE |
2994 case 24: 2651 V4L2_CAP_VBI_CAPTURE |
2995 fmt = format_by_palette(VIDEO_PALETTE_RGB24); 2652 V4L2_CAP_READWRITE |
2996 break; 2653 V4L2_CAP_STREAMING;
2997 case 32: 2654 if (no_overlay <= 0)
2998 fmt = format_by_palette(VIDEO_PALETTE_RGB32); 2655 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2999 break; 2656
3000 case 15: 2657 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
3001 fbuf->depth = 16; 2658 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
3002 fmt = format_by_palette(VIDEO_PALETTE_RGB555); 2659 cap->capabilities |= V4L2_CAP_TUNER;
3003 break; 2660 return 0;
3004 default: 2661}
3005 fmt = NULL;
3006 break;
3007 }
3008 if (NULL == fmt)
3009 goto fh_unlock_and_return;
3010
3011 fh->ovfmt = fmt;
3012 fh->fmt = fmt;
3013 btv->init.ovfmt = fmt;
3014 btv->init.fmt = fmt;
3015 btv->fbuf.base = fbuf->base;
3016 btv->fbuf.fmt.width = fbuf->width;
3017 btv->fbuf.fmt.height = fbuf->height;
3018 if (fbuf->bytesperline)
3019 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
3020 else
3021 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
3022 mutex_unlock(&fh->cap.lock);
3023 return 0;
3024 }
3025 2662
3026 case VIDIOCCAPTURE: 2663static int bttv_enum_fmt_vbi(struct file *file, void *priv,
3027 case VIDIOC_OVERLAY: 2664 struct v4l2_fmtdesc *f)
3028 { 2665{
3029 struct bttv_buffer *new; 2666 if (0 != f->index)
3030 int *on = arg; 2667 return -EINVAL;
3031 2668
3032 if (*on) { 2669 f->pixelformat = V4L2_PIX_FMT_GREY;
3033 /* verify args */ 2670 strcpy(f->description, "vbi data");
3034 if (NULL == btv->fbuf.base)
3035 return -EINVAL;
3036 if (!fh->ov.setup_ok) {
3037 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
3038 return -EINVAL;
3039 }
3040 }
3041 2671
3042 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY)) 2672 return 0;
3043 return -EBUSY; 2673}
3044 2674
3045 mutex_lock(&fh->cap.lock); 2675static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
3046 if (*on) { 2676{
3047 fh->ov.tvnorm = btv->tvnorm; 2677 int index = -1, i;
3048 new = videobuf_pci_alloc(sizeof(*new));
3049 new->crop = btv->crop[!!fh->do_crop].rect;
3050 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
3051 } else {
3052 new = NULL;
3053 }
3054 2678
3055 /* switch over */ 2679 for (i = 0; i < FORMATS; i++) {
3056 retval = bttv_switch_overlay(btv,fh,new); 2680 if (formats[i].fourcc != -1)
3057 mutex_unlock(&fh->cap.lock); 2681 index++;
3058 return retval; 2682 if ((unsigned int)index == f->index)
2683 break;
3059 } 2684 }
2685 if (FORMATS == i)
2686 return -EINVAL;
3060 2687
3061 case VIDIOCGMBUF: 2688 f->pixelformat = formats[i].fourcc;
3062 { 2689 strlcpy(f->description, formats[i].name, sizeof(f->description));
3063 struct video_mbuf *mbuf = arg;
3064 unsigned int i;
3065 2690
3066 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize, 2691 return i;
3067 V4L2_MEMORY_MMAP); 2692}
3068 if (retval < 0)
3069 return retval;
3070 2693
3071 gbuffers = retval; 2694static int bttv_enum_fmt_cap(struct file *file, void *priv,
3072 memset(mbuf,0,sizeof(*mbuf)); 2695 struct v4l2_fmtdesc *f)
3073 mbuf->frames = gbuffers; 2696{
3074 mbuf->size = gbuffers * gbufsize; 2697 int rc = bttv_enum_fmt_cap_ovr(f);
3075 for (i = 0; i < gbuffers; i++)
3076 mbuf->offsets[i] = i * gbufsize;
3077 return 0;
3078 }
3079 case VIDIOCMCAPTURE:
3080 {
3081 struct video_mmap *vm = arg;
3082 struct bttv_buffer *buf;
3083 enum v4l2_field field;
3084 __s32 height2;
3085 int res;
3086 2698
3087 if (vm->frame >= VIDEO_MAX_FRAME) 2699 if (rc < 0)
3088 return -EINVAL; 2700 return rc;
3089 2701
3090 res = bttv_resource(fh); 2702 return 0;
3091 if (!check_alloc_btres(btv, fh, res)) 2703}
3092 return -EBUSY;
3093 2704
3094 mutex_lock(&fh->cap.lock); 2705static int bttv_enum_fmt_overlay(struct file *file, void *priv,
3095 retval = -EINVAL; 2706 struct v4l2_fmtdesc *f)
3096 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame]; 2707{
3097 if (NULL == buf) 2708 int rc;
3098 goto fh_unlock_and_return;
3099 if (0 == buf->vb.baddr)
3100 goto fh_unlock_and_return;
3101 if (buf->vb.state == STATE_QUEUED ||
3102 buf->vb.state == STATE_ACTIVE)
3103 goto fh_unlock_and_return;
3104 2709
3105 height2 = btv->crop[!!fh->do_crop].rect.height >> 1; 2710 if (no_overlay > 0) {
3106 field = (vm->height > height2) 2711 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
3107 ? V4L2_FIELD_INTERLACED 2712 return -EINVAL;
3108 : V4L2_FIELD_BOTTOM;
3109 retval = bttv_prepare_buffer(&fh->cap,btv,buf,
3110 format_by_palette(vm->format),
3111 vm->width,vm->height,field);
3112 if (0 != retval)
3113 goto fh_unlock_and_return;
3114 btv->init.width = vm->width;
3115 btv->init.height = vm->height;
3116 spin_lock_irqsave(&btv->s_lock,flags);
3117 buffer_queue(&fh->cap,&buf->vb);
3118 spin_unlock_irqrestore(&btv->s_lock,flags);
3119 mutex_unlock(&fh->cap.lock);
3120 return 0;
3121 } 2713 }
3122 case VIDIOCSYNC:
3123 {
3124 int *frame = arg;
3125 struct bttv_buffer *buf;
3126 2714
3127 if (*frame >= VIDEO_MAX_FRAME) 2715 rc = bttv_enum_fmt_cap_ovr(f);
3128 return -EINVAL;
3129 2716
3130 mutex_lock(&fh->cap.lock); 2717 if (rc < 0)
3131 retval = -EINVAL; 2718 return rc;
3132 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
3133 if (NULL == buf)
3134 goto fh_unlock_and_return;
3135 retval = videobuf_waiton(&buf->vb,0,1);
3136 if (0 != retval)
3137 goto fh_unlock_and_return;
3138 switch (buf->vb.state) {
3139 case STATE_ERROR:
3140 retval = -EIO;
3141 /* fall through */
3142 case STATE_DONE:
3143 {
3144 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
3145 videobuf_dma_sync(&fh->cap,dma);
3146 bttv_dma_free(&fh->cap,btv,buf);
3147 break;
3148 }
3149 default:
3150 retval = -EINVAL;
3151 break;
3152 }
3153 mutex_unlock(&fh->cap.lock);
3154 return retval;
3155 }
3156 2719
3157 case VIDIOCGVBIFMT: 2720 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
3158 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) { 2721 return -EINVAL;
3159 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
3160 if (0 != retval)
3161 return retval;
3162 }
3163 2722
3164 /* fall through */ 2723 return 0;
2724}
3165 2725
3166 case VIDIOCSVBIFMT: 2726static int bttv_g_fbuf(struct file *file, void *f,
3167 return v4l_compat_translate_ioctl(inode, file, cmd, 2727 struct v4l2_framebuffer *fb)
3168 arg, bttv_do_ioctl); 2728{
3169 2729 struct bttv_fh *fh = f;
3170 case BTTV_VERSION: 2730 struct bttv *btv = fh->btv;
3171 case VIDIOCGFREQ:
3172 case VIDIOCSFREQ:
3173 case VIDIOCGTUNER:
3174 case VIDIOCSTUNER:
3175 case VIDIOCGCHAN:
3176 case VIDIOCSCHAN:
3177 case VIDIOCGAUDIO:
3178 case VIDIOCSAUDIO:
3179 return bttv_common_ioctls(btv,cmd,arg);
3180
3181 /* *** v4l2 *** ************************************************ */
3182 case VIDIOC_QUERYCAP:
3183 {
3184 struct v4l2_capability *cap = arg;
3185 2731
3186 if (0 == v4l2) 2732 *fb = btv->fbuf;
3187 return -EINVAL; 2733 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3188 memset(cap, 0, sizeof (*cap)); 2734 if (fh->ovfmt)
3189 strlcpy(cap->driver, "bttv", sizeof (cap->driver)); 2735 fb->fmt.pixelformat = fh->ovfmt->fourcc;
3190 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card)); 2736 return 0;
3191 snprintf(cap->bus_info, sizeof (cap->bus_info), 2737}
3192 "PCI:%s", pci_name(btv->c.pci));
3193 cap->version = BTTV_VERSION_CODE;
3194 cap->capabilities =
3195 V4L2_CAP_VIDEO_CAPTURE |
3196 V4L2_CAP_VBI_CAPTURE |
3197 V4L2_CAP_READWRITE |
3198 V4L2_CAP_STREAMING;
3199 if (no_overlay <= 0)
3200 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
3201
3202 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
3203 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
3204 cap->capabilities |= V4L2_CAP_TUNER;
3205 return 0;
3206 }
3207 2738
3208 case VIDIOC_ENUM_FMT: 2739static int bttv_overlay(struct file *file, void *f, unsigned int on)
3209 { 2740{
3210 struct v4l2_fmtdesc *f = arg; 2741 struct bttv_fh *fh = f;
3211 enum v4l2_buf_type type; 2742 struct bttv *btv = fh->btv;
3212 unsigned int i; 2743 struct bttv_buffer *new;
3213 int index; 2744 int retval;
3214
3215 type = f->type;
3216 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
3217 /* vbi */
3218 index = f->index;
3219 if (0 != index)
3220 return -EINVAL;
3221 memset(f,0,sizeof(*f));
3222 f->index = index;
3223 f->type = type;
3224 f->pixelformat = V4L2_PIX_FMT_GREY;
3225 strcpy(f->description,"vbi data");
3226 return 0;
3227 }
3228 2745
3229 /* video capture + overlay */ 2746 if (on) {
3230 index = -1; 2747 /* verify args */
3231 for (i = 0; i < BTTV_FORMATS; i++) { 2748 if (NULL == btv->fbuf.base)
3232 if (bttv_formats[i].fourcc != -1)
3233 index++;
3234 if ((unsigned int)index == f->index)
3235 break;
3236 }
3237 if (BTTV_FORMATS == i)
3238 return -EINVAL; 2749 return -EINVAL;
3239 2750 if (!fh->ov.setup_ok) {
3240 switch (f->type) { 2751 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
3241 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3242 break;
3243 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3244 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
3245 return -EINVAL;
3246 break;
3247 default:
3248 return -EINVAL; 2752 return -EINVAL;
3249 } 2753 }
3250 memset(f,0,sizeof(*f));
3251 f->index = index;
3252 f->type = type;
3253 f->pixelformat = bttv_formats[i].fourcc;
3254 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
3255 return 0;
3256 } 2754 }
3257 2755
3258 case VIDIOC_TRY_FMT: 2756 if (!check_alloc_btres(btv, fh, RESOURCE_OVERLAY))
3259 { 2757 return -EBUSY;
3260 struct v4l2_format *f = arg; 2758
3261 return bttv_try_fmt(fh,btv,f, /* adjust_crop */ 0); 2759 mutex_lock(&fh->cap.lock);
3262 } 2760 if (on) {
3263 case VIDIOC_G_FMT: 2761 fh->ov.tvnorm = btv->tvnorm;
3264 { 2762 new = videobuf_pci_alloc(sizeof(*new));
3265 struct v4l2_format *f = arg; 2763 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
3266 return bttv_g_fmt(fh,f); 2764 } else {
3267 } 2765 new = NULL;
3268 case VIDIOC_S_FMT:
3269 {
3270 struct v4l2_format *f = arg;
3271 return bttv_s_fmt(fh,btv,f);
3272 } 2766 }
3273 2767
3274 case VIDIOC_G_FBUF: 2768 /* switch over */
3275 { 2769 retval = bttv_switch_overlay(btv, fh, new);
3276 struct v4l2_framebuffer *fb = arg; 2770 mutex_unlock(&fh->cap.lock);
2771 return retval;
2772}
3277 2773
3278 *fb = btv->fbuf; 2774static int bttv_s_fbuf(struct file *file, void *f,
3279 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING; 2775 struct v4l2_framebuffer *fb)
3280 if (fh->ovfmt) 2776{
3281 fb->fmt.pixelformat = fh->ovfmt->fourcc; 2777 struct bttv_fh *fh = f;
3282 return 0; 2778 struct bttv *btv = fh->btv;
3283 } 2779 const struct bttv_format *fmt;
3284 case VIDIOC_S_FBUF: 2780 int retval;
3285 {
3286 struct v4l2_framebuffer *fb = arg;
3287 const struct bttv_format *fmt;
3288 2781
3289 if(!capable(CAP_SYS_ADMIN) && 2782 if (!capable(CAP_SYS_ADMIN) &&
3290 !capable(CAP_SYS_RAWIO)) 2783 !capable(CAP_SYS_RAWIO))
3291 return -EPERM; 2784 return -EPERM;
3292 2785
3293 /* check args */ 2786 /* check args */
3294 fmt = format_by_fourcc(fb->fmt.pixelformat); 2787 fmt = format_by_fourcc(fb->fmt.pixelformat);
3295 if (NULL == fmt) 2788 if (NULL == fmt)
3296 return -EINVAL; 2789 return -EINVAL;
3297 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED)) 2790 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
3298 return -EINVAL; 2791 return -EINVAL;
3299 2792
3300 retval = -EINVAL; 2793 retval = -EINVAL;
3301 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) { 2794 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
3302 __s32 width = fb->fmt.width; 2795 __s32 width = fb->fmt.width;
3303 __s32 height = fb->fmt.height; 2796 __s32 height = fb->fmt.height;
3304 2797
3305 retval = limit_scaled_size(fh, &width, &height, 2798 retval = limit_scaled_size(fh, &width, &height,
3306 V4L2_FIELD_INTERLACED, 2799 V4L2_FIELD_INTERLACED,
3307 /* width_mask */ ~3, 2800 /* width_mask */ ~3,
3308 /* width_bias */ 2, 2801 /* width_bias */ 2,
3309 /* adjust_size */ 0, 2802 /* adjust_size */ 0,
3310 /* adjust_crop */ 0); 2803 /* adjust_crop */ 0);
3311 if (0 != retval) 2804 if (0 != retval)
3312 return retval; 2805 return retval;
3313 } 2806 }
3314 2807
3315 /* ok, accept it */ 2808 /* ok, accept it */
3316 mutex_lock(&fh->cap.lock); 2809 mutex_lock(&fh->cap.lock);
3317 btv->fbuf.base = fb->base; 2810 btv->fbuf.base = fb->base;
3318 btv->fbuf.fmt.width = fb->fmt.width; 2811 btv->fbuf.fmt.width = fb->fmt.width;
3319 btv->fbuf.fmt.height = fb->fmt.height; 2812 btv->fbuf.fmt.height = fb->fmt.height;
3320 if (0 != fb->fmt.bytesperline) 2813 if (0 != fb->fmt.bytesperline)
3321 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline; 2814 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
3322 else 2815 else
3323 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8; 2816 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
3324 2817
3325 retval = 0; 2818 retval = 0;
3326 fh->ovfmt = fmt; 2819 fh->ovfmt = fmt;
3327 btv->init.ovfmt = fmt; 2820 btv->init.ovfmt = fmt;
3328 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) { 2821 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
3329 fh->ov.w.left = 0; 2822 fh->ov.w.left = 0;
3330 fh->ov.w.top = 0; 2823 fh->ov.w.top = 0;
3331 fh->ov.w.width = fb->fmt.width; 2824 fh->ov.w.width = fb->fmt.width;
3332 fh->ov.w.height = fb->fmt.height; 2825 fh->ov.w.height = fb->fmt.height;
3333 btv->init.ov.w.width = fb->fmt.width; 2826 btv->init.ov.w.width = fb->fmt.width;
3334 btv->init.ov.w.height = fb->fmt.height; 2827 btv->init.ov.w.height = fb->fmt.height;
3335 kfree(fh->ov.clips); 2828 kfree(fh->ov.clips);
3336 fh->ov.clips = NULL; 2829 fh->ov.clips = NULL;
3337 fh->ov.nclips = 0; 2830 fh->ov.nclips = 0;
3338 2831
3339 if (check_btres(fh, RESOURCE_OVERLAY)) { 2832 if (check_btres(fh, RESOURCE_OVERLAY)) {
3340 struct bttv_buffer *new; 2833 struct bttv_buffer *new;
3341 2834
3342 new = videobuf_pci_alloc(sizeof(*new)); 2835 new = videobuf_pci_alloc(sizeof(*new));
3343 new->crop = btv->crop[!!fh->do_crop].rect; 2836 new->crop = btv->crop[!!fh->do_crop].rect;
3344 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new); 2837 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
3345 retval = bttv_switch_overlay(btv,fh,new); 2838 retval = bttv_switch_overlay(btv, fh, new);
3346 }
3347 } 2839 }
3348 mutex_unlock(&fh->cap.lock);
3349 return retval;
3350 } 2840 }
2841 mutex_unlock(&fh->cap.lock);
2842 return retval;
2843}
3351 2844
3352 case VIDIOC_REQBUFS: 2845static int bttv_reqbufs(struct file *file, void *priv,
3353 return videobuf_reqbufs(bttv_queue(fh),arg); 2846 struct v4l2_requestbuffers *p)
2847{
2848 struct bttv_fh *fh = priv;
2849 return videobuf_reqbufs(bttv_queue(fh), p);
2850}
3354 2851
3355 case VIDIOC_QUERYBUF: 2852static int bttv_querybuf(struct file *file, void *priv,
3356 return videobuf_querybuf(bttv_queue(fh),arg); 2853 struct v4l2_buffer *b)
2854{
2855 struct bttv_fh *fh = priv;
2856 return videobuf_querybuf(bttv_queue(fh), b);
2857}
3357 2858
3358 case VIDIOC_QBUF: 2859static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
3359 { 2860{
3360 int res = bttv_resource(fh); 2861 struct bttv_fh *fh = priv;
2862 struct bttv *btv = fh->btv;
2863 int res = bttv_resource(fh);
3361 2864
3362 if (!check_alloc_btres(btv, fh, res)) 2865 if (!check_alloc_btres(btv, fh, res))
3363 return -EBUSY; 2866 return -EBUSY;
3364 return videobuf_qbuf(bttv_queue(fh),arg);
3365 }
3366 2867
3367 case VIDIOC_DQBUF: 2868 return videobuf_qbuf(bttv_queue(fh), b);
3368 return videobuf_dqbuf(bttv_queue(fh),arg, 2869}
3369 file->f_flags & O_NONBLOCK);
3370 2870
3371 case VIDIOC_STREAMON: 2871static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
3372 { 2872{
3373 int res = bttv_resource(fh); 2873 struct bttv_fh *fh = priv;
2874 return videobuf_dqbuf(bttv_queue(fh), b,
2875 file->f_flags & O_NONBLOCK);
2876}
3374 2877
3375 if (!check_alloc_btres(btv,fh,res)) 2878static int bttv_streamon(struct file *file, void *priv,
3376 return -EBUSY; 2879 enum v4l2_buf_type type)
3377 return videobuf_streamon(bttv_queue(fh)); 2880{
3378 } 2881 struct bttv_fh *fh = priv;
3379 case VIDIOC_STREAMOFF: 2882 struct bttv *btv = fh->btv;
3380 { 2883 int res = bttv_resource(fh);
3381 int res = bttv_resource(fh);
3382 2884
3383 retval = videobuf_streamoff(bttv_queue(fh)); 2885 if (!check_alloc_btres(btv, fh, res))
3384 if (retval < 0) 2886 return -EBUSY;
3385 return retval; 2887 return videobuf_streamon(bttv_queue(fh));
3386 free_btres(btv,fh,res); 2888}
3387 return 0;
3388 }
3389 2889
3390 case VIDIOC_QUERYCTRL:
3391 {
3392 struct v4l2_queryctrl *c = arg;
3393 int i;
3394 2890
3395 if ((c->id < V4L2_CID_BASE || 2891static int bttv_streamoff(struct file *file, void *priv,
3396 c->id >= V4L2_CID_LASTP1) && 2892 enum v4l2_buf_type type)
3397 (c->id < V4L2_CID_PRIVATE_BASE || 2893{
3398 c->id >= V4L2_CID_PRIVATE_LASTP1)) 2894 struct bttv_fh *fh = priv;
3399 return -EINVAL; 2895 struct bttv *btv = fh->btv;
3400 for (i = 0; i < BTTV_CTLS; i++) 2896 int retval;
3401 if (bttv_ctls[i].id == c->id) 2897 int res = bttv_resource(fh);
3402 break;
3403 if (i == BTTV_CTLS) {
3404 *c = no_ctl;
3405 return 0;
3406 }
3407 *c = bttv_ctls[i];
3408 if (btv->audio_hook && i >= 4 && i <= 8) {
3409 struct video_audio va;
3410 memset(&va,0,sizeof(va));
3411 btv->audio_hook(btv,&va,0);
3412 switch (bttv_ctls[i].id) {
3413 case V4L2_CID_AUDIO_VOLUME:
3414 if (!(va.flags & VIDEO_AUDIO_VOLUME))
3415 *c = no_ctl;
3416 break;
3417 case V4L2_CID_AUDIO_BALANCE:
3418 if (!(va.flags & VIDEO_AUDIO_BALANCE))
3419 *c = no_ctl;
3420 break;
3421 case V4L2_CID_AUDIO_BASS:
3422 if (!(va.flags & VIDEO_AUDIO_BASS))
3423 *c = no_ctl;
3424 break;
3425 case V4L2_CID_AUDIO_TREBLE:
3426 if (!(va.flags & VIDEO_AUDIO_TREBLE))
3427 *c = no_ctl;
3428 break;
3429 }
3430 }
3431 return 0;
3432 }
3433 case VIDIOC_G_CTRL:
3434 return get_control(btv,arg);
3435 case VIDIOC_S_CTRL:
3436 return set_control(btv,arg);
3437 case VIDIOC_G_PARM:
3438 {
3439 struct v4l2_streamparm *parm = arg;
3440 struct v4l2_standard s;
3441 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
3442 return -EINVAL;
3443 memset(parm,0,sizeof(*parm));
3444 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
3445 bttv_tvnorms[btv->tvnorm].name);
3446 parm->parm.capture.timeperframe = s.frameperiod;
3447 return 0;
3448 }
3449 2898
3450 case VIDIOC_G_PRIORITY:
3451 {
3452 enum v4l2_priority *p = arg;
3453 2899
3454 *p = v4l2_prio_max(&btv->prio); 2900 retval = videobuf_streamoff(bttv_queue(fh));
3455 return 0; 2901 if (retval < 0)
3456 } 2902 return retval;
3457 case VIDIOC_S_PRIORITY: 2903 free_btres(btv, fh, res);
3458 { 2904 return 0;
3459 enum v4l2_priority *prio = arg; 2905}
3460 2906
3461 return v4l2_prio_change(&btv->prio, &fh->prio, *prio); 2907static int bttv_queryctrl(struct file *file, void *priv,
3462 } 2908 struct v4l2_queryctrl *c)
2909{
2910 struct bttv_fh *fh = priv;
2911 struct bttv *btv = fh->btv;
2912 const struct v4l2_queryctrl *ctrl;
3463 2913
3464 case VIDIOC_CROPCAP: 2914 if ((c->id < V4L2_CID_BASE ||
3465 { 2915 c->id >= V4L2_CID_LASTP1) &&
3466 struct v4l2_cropcap *cap = arg; 2916 (c->id < V4L2_CID_PRIVATE_BASE ||
3467 enum v4l2_buf_type type; 2917 c->id >= V4L2_CID_PRIVATE_LASTP1))
2918 return -EINVAL;
3468 2919
3469 type = cap->type; 2920 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2921 *c = no_ctl;
2922 else {
2923 ctrl = ctrl_by_id(c->id);
3470 2924
3471 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 2925 *c = (NULL != ctrl) ? *ctrl : no_ctl;
3472 type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 2926 }
3473 return -EINVAL;
3474 2927
3475 *cap = bttv_tvnorms[btv->tvnorm].cropcap; 2928 return 0;
3476 cap->type = type; 2929}
3477 2930
3478 return 0; 2931static int bttv_g_parm(struct file *file, void *f,
3479 } 2932 struct v4l2_streamparm *parm)
3480 case VIDIOC_G_CROP: 2933{
3481 { 2934 struct bttv_fh *fh = f;
3482 struct v4l2_crop * crop = arg; 2935 struct bttv *btv = fh->btv;
2936 struct v4l2_standard s;
3483 2937
3484 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 2938 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
3485 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 2939 return -EINVAL;
3486 return -EINVAL; 2940 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2941 bttv_tvnorms[btv->tvnorm].name);
2942 parm->parm.capture.timeperframe = s.frameperiod;
2943 return 0;
2944}
3487 2945
3488 /* No fh->do_crop = 1; because btv->crop[1] may be 2946static int bttv_g_tuner(struct file *file, void *priv,
3489 inconsistent with fh->width or fh->height and apps 2947 struct v4l2_tuner *t)
3490 do not expect a change here. */ 2948{
2949 struct bttv_fh *fh = priv;
2950 struct bttv *btv = fh->btv;
3491 2951
3492 crop->c = btv->crop[!!fh->do_crop].rect; 2952 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2953 return -EINVAL;
2954 if (0 != t->index)
2955 return -EINVAL;
3493 2956
3494 return 0; 2957 mutex_lock(&btv->lock);
3495 } 2958 memset(t, 0, sizeof(*t));
3496 case VIDIOC_S_CROP: 2959 t->rxsubchans = V4L2_TUNER_SUB_MONO;
3497 { 2960 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
3498 struct v4l2_crop *crop = arg; 2961 strcpy(t->name, "Television");
3499 const struct v4l2_rect *b; 2962 t->capability = V4L2_TUNER_CAP_NORM;
3500 struct bttv_crop c; 2963 t->type = V4L2_TUNER_ANALOG_TV;
3501 __s32 b_left; 2964 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
3502 __s32 b_top; 2965 t->signal = 0xffff;
3503 __s32 b_right; 2966
3504 __s32 b_bottom; 2967 if (btv->audio_mode_gpio)
3505 2968 btv->audio_mode_gpio(btv, t, 0);
3506 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3507 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3508 return -EINVAL;
3509 2969
3510 retval = v4l2_prio_check(&btv->prio,&fh->prio); 2970 mutex_unlock(&btv->lock);
3511 if (0 != retval) 2971 return 0;
3512 return retval; 2972}
3513 2973
3514 /* Make sure tvnorm, vbi_end and the current cropping 2974static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
3515 parameters remain consistent until we're done. Note 2975{
3516 read() may change vbi_end in check_alloc_btres(). */ 2976 struct bttv_fh *fh = f;
3517 mutex_lock(&btv->lock); 2977 struct bttv *btv = fh->btv;
3518 2978
3519 retval = -EBUSY; 2979 *p = v4l2_prio_max(&btv->prio);
3520 2980
3521 if (locked_btres(fh->btv, VIDEO_RESOURCES)) 2981 return 0;
3522 goto btv_unlock_and_return; 2982}
3523 2983
3524 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds; 2984static int bttv_s_priority(struct file *file, void *f,
2985 enum v4l2_priority prio)
2986{
2987 struct bttv_fh *fh = f;
2988 struct bttv *btv = fh->btv;
3525 2989
3526 b_left = b->left; 2990 return v4l2_prio_change(&btv->prio, &fh->prio, prio);
3527 b_right = b_left + b->width; 2991}
3528 b_bottom = b->top + b->height;
3529 2992
3530 b_top = max(b->top, btv->vbi_end); 2993static int bttv_cropcap(struct file *file, void *priv,
3531 if (b_top + 32 >= b_bottom) 2994 struct v4l2_cropcap *cap)
3532 goto btv_unlock_and_return; 2995{
2996 struct bttv_fh *fh = priv;
2997 struct bttv *btv = fh->btv;
3533 2998
3534 /* Min. scaled size 48 x 32. */ 2999 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3535 c.rect.left = clamp(crop->c.left, b_left, b_right - 48); 3000 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3536 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY); 3001 return -EINVAL;
3537 3002
3538 c.rect.width = clamp(crop->c.width, 3003 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3539 48, b_right - c.rect.left);
3540 3004
3541 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32); 3005 return 0;
3542 /* Top and height must be a multiple of two. */ 3006}
3543 c.rect.top = (c.rect.top + 1) & ~1;
3544 3007
3545 c.rect.height = clamp(crop->c.height, 3008static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
3546 32, b_bottom - c.rect.top); 3009{
3547 c.rect.height = (c.rect.height + 1) & ~1; 3010 struct bttv_fh *fh = f;
3011 struct bttv *btv = fh->btv;
3548 3012
3549 bttv_crop_calc_limits(&c); 3013 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3014 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3015 return -EINVAL;
3550 3016
3551 btv->crop[1] = c; 3017 /* No fh->do_crop = 1; because btv->crop[1] may be
3018 inconsistent with fh->width or fh->height and apps
3019 do not expect a change here. */
3552 3020
3553 mutex_unlock(&btv->lock); 3021 crop->c = btv->crop[!!fh->do_crop].rect;
3554 3022
3555 fh->do_crop = 1; 3023 return 0;
3024}
3556 3025
3557 mutex_lock(&fh->cap.lock); 3026static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
3027{
3028 struct bttv_fh *fh = f;
3029 struct bttv *btv = fh->btv;
3030 const struct v4l2_rect *b;
3031 int retval;
3032 struct bttv_crop c;
3033 __s32 b_left;
3034 __s32 b_top;
3035 __s32 b_right;
3036 __s32 b_bottom;
3558 3037
3559 if (fh->width < c.min_scaled_width) { 3038 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3560 fh->width = c.min_scaled_width; 3039 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3561 btv->init.width = c.min_scaled_width; 3040 return -EINVAL;
3562 } else if (fh->width > c.max_scaled_width) {
3563 fh->width = c.max_scaled_width;
3564 btv->init.width = c.max_scaled_width;
3565 }
3566 3041
3567 if (fh->height < c.min_scaled_height) { 3042 retval = v4l2_prio_check(&btv->prio, &fh->prio);
3568 fh->height = c.min_scaled_height; 3043 if (0 != retval)
3569 btv->init.height = c.min_scaled_height; 3044 return retval;
3570 } else if (fh->height > c.max_scaled_height) {
3571 fh->height = c.max_scaled_height;
3572 btv->init.height = c.max_scaled_height;
3573 }
3574 3045
3575 mutex_unlock(&fh->cap.lock); 3046 /* Make sure tvnorm, vbi_end and the current cropping
3047 parameters remain consistent until we're done. Note
3048 read() may change vbi_end in check_alloc_btres(). */
3049 mutex_lock(&btv->lock);
3576 3050
3577 return 0; 3051 retval = -EBUSY;
3052
3053 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3054 mutex_unlock(&btv->lock);
3055 return retval;
3578 } 3056 }
3579 3057
3580 case VIDIOC_ENUMSTD: 3058 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3581 case VIDIOC_G_STD:
3582 case VIDIOC_S_STD:
3583 case VIDIOC_ENUMINPUT:
3584 case VIDIOC_G_INPUT:
3585 case VIDIOC_S_INPUT:
3586 case VIDIOC_G_TUNER:
3587 case VIDIOC_S_TUNER:
3588 case VIDIOC_G_FREQUENCY:
3589 case VIDIOC_S_FREQUENCY:
3590 case VIDIOC_LOG_STATUS:
3591 case VIDIOC_DBG_G_REGISTER:
3592 case VIDIOC_DBG_S_REGISTER:
3593 return bttv_common_ioctls(btv,cmd,arg);
3594 3059
3595 default: 3060 b_left = b->left;
3596 return -ENOIOCTLCMD; 3061 b_right = b_left + b->width;
3062 b_bottom = b->top + b->height;
3063
3064 b_top = max(b->top, btv->vbi_end);
3065 if (b_top + 32 >= b_bottom) {
3066 mutex_unlock(&btv->lock);
3067 return retval;
3597 } 3068 }
3598 return 0;
3599 3069
3600 fh_unlock_and_return: 3070 /* Min. scaled size 48 x 32. */
3601 mutex_unlock(&fh->cap.lock); 3071 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3602 return retval; 3072 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3603 3073
3604 btv_unlock_and_return: 3074 c.rect.width = clamp(crop->c.width,
3605 mutex_unlock(&btv->lock); 3075 48, b_right - c.rect.left);
3606 return retval;
3607}
3608 3076
3609static int bttv_ioctl(struct inode *inode, struct file *file, 3077 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3610 unsigned int cmd, unsigned long arg) 3078 /* Top and height must be a multiple of two. */
3611{ 3079 c.rect.top = (c.rect.top + 1) & ~1;
3612 struct bttv_fh *fh = file->private_data;
3613 3080
3614 switch (cmd) { 3081 c.rect.height = clamp(crop->c.height,
3615 case BTTV_VBISIZE: 3082 32, b_bottom - c.rect.top);
3616 { 3083 c.rect.height = (c.rect.height + 1) & ~1;
3617 const struct bttv_tvnorm *tvnorm;
3618 3084
3619 tvnorm = fh->vbi_fmt.tvnorm; 3085 bttv_crop_calc_limits(&c);
3620 3086
3621 if (fh->vbi_fmt.fmt.start[0] != tvnorm->vbistart[0] || 3087 btv->crop[1] = c;
3622 fh->vbi_fmt.fmt.start[1] != tvnorm->vbistart[1] || 3088
3623 fh->vbi_fmt.fmt.count[0] != fh->vbi_fmt.fmt.count[1]) { 3089 mutex_unlock(&btv->lock);
3624 /* BTTV_VBISIZE cannot express these parameters, 3090
3625 however open() resets the paramters to defaults 3091 fh->do_crop = 1;
3626 and apps shouldn't call BTTV_VBISIZE after
3627 VIDIOC_S_FMT. */
3628 return -EINVAL;
3629 }
3630 3092
3631 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE); 3093 mutex_lock(&fh->cap.lock);
3632 return (fh->vbi_fmt.fmt.count[0] * 2 3094
3633 * fh->vbi_fmt.fmt.samples_per_line); 3095 if (fh->width < c.min_scaled_width) {
3096 fh->width = c.min_scaled_width;
3097 btv->init.width = c.min_scaled_width;
3098 } else if (fh->width > c.max_scaled_width) {
3099 fh->width = c.max_scaled_width;
3100 btv->init.width = c.max_scaled_width;
3634 } 3101 }
3635 3102
3636 default: 3103 if (fh->height < c.min_scaled_height) {
3637 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl); 3104 fh->height = c.min_scaled_height;
3105 btv->init.height = c.min_scaled_height;
3106 } else if (fh->height > c.max_scaled_height) {
3107 fh->height = c.max_scaled_height;
3108 btv->init.height = c.max_scaled_height;
3638 } 3109 }
3110
3111 mutex_unlock(&fh->cap.lock);
3112
3113 return 0;
3114}
3115
3116static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
3117{
3118 strcpy(a->name, "audio");
3119 return 0;
3120}
3121
3122static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
3123{
3124 return 0;
3639} 3125}
3640 3126
3641static ssize_t bttv_read(struct file *file, char __user *data, 3127static ssize_t bttv_read(struct file *file, char __user *data,
@@ -3719,8 +3205,8 @@ static unsigned int bttv_poll(struct file *file, poll_table *wait)
3719 } 3205 }
3720 3206
3721 poll_wait(file, &buf->vb.done, wait); 3207 poll_wait(file, &buf->vb.done, wait);
3722 if (buf->vb.state == STATE_DONE || 3208 if (buf->vb.state == VIDEOBUF_DONE ||
3723 buf->vb.state == STATE_ERROR) 3209 buf->vb.state == VIDEOBUF_ERROR)
3724 return POLLIN|POLLRDNORM; 3210 return POLLIN|POLLRDNORM;
3725 return 0; 3211 return 0;
3726} 3212}
@@ -3777,7 +3263,7 @@ static int bttv_open(struct inode *inode, struct file *file)
3777 V4L2_FIELD_SEQ_TB, 3263 V4L2_FIELD_SEQ_TB,
3778 sizeof(struct bttv_buffer), 3264 sizeof(struct bttv_buffer),
3779 fh); 3265 fh);
3780 i2c_vidiocschan(btv); 3266 set_tvnorm(btv,btv->tvnorm);
3781 3267
3782 btv->users++; 3268 btv->users++;
3783 3269
@@ -3857,7 +3343,7 @@ static const struct file_operations bttv_fops =
3857 .owner = THIS_MODULE, 3343 .owner = THIS_MODULE,
3858 .open = bttv_open, 3344 .open = bttv_open,
3859 .release = bttv_release, 3345 .release = bttv_release,
3860 .ioctl = bttv_ioctl, 3346 .ioctl = video_ioctl2,
3861 .compat_ioctl = v4l_compat_ioctl32, 3347 .compat_ioctl = v4l_compat_ioctl32,
3862 .llseek = no_llseek, 3348 .llseek = no_llseek,
3863 .read = bttv_read, 3349 .read = bttv_read,
@@ -3867,19 +3353,61 @@ static const struct file_operations bttv_fops =
3867 3353
3868static struct video_device bttv_video_template = 3354static struct video_device bttv_video_template =
3869{ 3355{
3870 .name = "UNSET",
3871 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3872 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3873 .fops = &bttv_fops,
3874 .minor = -1,
3875};
3876
3877static struct video_device bttv_vbi_template =
3878{
3879 .name = "bt848/878 vbi",
3880 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3881 .fops = &bttv_fops, 3356 .fops = &bttv_fops,
3882 .minor = -1, 3357 .minor = -1,
3358 .vidioc_querycap = bttv_querycap,
3359 .vidioc_enum_fmt_cap = bttv_enum_fmt_cap,
3360 .vidioc_g_fmt_cap = bttv_g_fmt_cap,
3361 .vidioc_try_fmt_cap = bttv_try_fmt_cap,
3362 .vidioc_s_fmt_cap = bttv_s_fmt_cap,
3363 .vidioc_enum_fmt_overlay = bttv_enum_fmt_overlay,
3364 .vidioc_g_fmt_overlay = bttv_g_fmt_overlay,
3365 .vidioc_try_fmt_overlay = bttv_try_fmt_overlay,
3366 .vidioc_s_fmt_overlay = bttv_s_fmt_overlay,
3367 .vidioc_enum_fmt_vbi = bttv_enum_fmt_vbi,
3368 .vidioc_g_fmt_vbi = bttv_g_fmt_vbi,
3369 .vidioc_try_fmt_vbi = bttv_try_fmt_vbi,
3370 .vidioc_s_fmt_vbi = bttv_s_fmt_vbi,
3371 .vidioc_g_audio = bttv_g_audio,
3372 .vidioc_s_audio = bttv_s_audio,
3373 .vidioc_cropcap = bttv_cropcap,
3374 .vidioc_reqbufs = bttv_reqbufs,
3375 .vidioc_querybuf = bttv_querybuf,
3376 .vidioc_qbuf = bttv_qbuf,
3377 .vidioc_dqbuf = bttv_dqbuf,
3378 .vidioc_s_std = bttv_s_std,
3379 .vidioc_enum_input = bttv_enum_input,
3380 .vidioc_g_input = bttv_g_input,
3381 .vidioc_s_input = bttv_s_input,
3382 .vidioc_queryctrl = bttv_queryctrl,
3383 .vidioc_g_ctrl = bttv_g_ctrl,
3384 .vidioc_s_ctrl = bttv_s_ctrl,
3385 .vidioc_streamon = bttv_streamon,
3386 .vidioc_streamoff = bttv_streamoff,
3387 .vidioc_g_tuner = bttv_g_tuner,
3388 .vidioc_s_tuner = bttv_s_tuner,
3389#ifdef CONFIG_VIDEO_V4L1_COMPAT
3390 .vidiocgmbuf = vidiocgmbuf,
3391#endif
3392 .vidioc_g_crop = bttv_g_crop,
3393 .vidioc_g_crop = bttv_g_crop,
3394 .vidioc_s_crop = bttv_s_crop,
3395 .vidioc_g_fbuf = bttv_g_fbuf,
3396 .vidioc_s_fbuf = bttv_s_fbuf,
3397 .vidioc_overlay = bttv_overlay,
3398 .vidioc_g_priority = bttv_g_priority,
3399 .vidioc_s_priority = bttv_s_priority,
3400 .vidioc_g_parm = bttv_g_parm,
3401 .vidioc_g_frequency = bttv_g_frequency,
3402 .vidioc_s_frequency = bttv_s_frequency,
3403 .vidioc_log_status = bttv_log_status,
3404 .vidioc_querystd = bttv_querystd,
3405#ifdef CONFIG_VIDEO_ADV_DEBUG
3406 .vidioc_g_register = bttv_g_register,
3407 .vidioc_s_register = bttv_s_register,
3408#endif
3409 .tvnorms = BTTV_NORMS,
3410 .current_norm = V4L2_STD_PAL,
3883}; 3411};
3884 3412
3885/* ----------------------------------------------------------------------- */ 3413/* ----------------------------------------------------------------------- */
@@ -3918,7 +3446,7 @@ static int radio_open(struct inode *inode, struct file *file)
3918 3446
3919static int radio_release(struct inode *inode, struct file *file) 3447static int radio_release(struct inode *inode, struct file *file)
3920{ 3448{
3921 struct bttv *btv = file->private_data; 3449 struct bttv *btv = file->private_data;
3922 struct rds_command cmd; 3450 struct rds_command cmd;
3923 3451
3924 btv->radio_user--; 3452 btv->radio_user--;
@@ -3928,59 +3456,116 @@ static int radio_release(struct inode *inode, struct file *file)
3928 return 0; 3456 return 0;
3929} 3457}
3930 3458
3931static int radio_do_ioctl(struct inode *inode, struct file *file, 3459static int radio_querycap(struct file *file, void *priv,
3932 unsigned int cmd, void *arg) 3460 struct v4l2_capability *cap)
3933{ 3461{
3934 struct bttv *btv = file->private_data; 3462 struct bttv_fh *fh = priv;
3463 struct bttv *btv = fh->btv;
3935 3464
3936 switch (cmd) { 3465 strcpy(cap->driver, "bttv");
3937 case VIDIOCGCAP: 3466 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3938 { 3467 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3939 struct video_capability *cap = arg; 3468 cap->version = BTTV_VERSION_CODE;
3469 cap->capabilities = V4L2_CAP_TUNER;
3940 3470
3941 memset(cap,0,sizeof(*cap)); 3471 return 0;
3942 strcpy(cap->name,btv->radio_dev->name); 3472}
3943 cap->type = VID_TYPE_TUNER;
3944 cap->channels = 1;
3945 cap->audios = 1;
3946 return 0;
3947 }
3948 3473
3949 case VIDIOCGTUNER: 3474static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
3950 { 3475{
3951 struct video_tuner *v = arg; 3476 struct bttv_fh *fh = priv;
3477 struct bttv *btv = fh->btv;
3952 3478
3953 if(v->tuner) 3479 if (UNSET == bttv_tvcards[btv->c.type].tuner)
3954 return -EINVAL; 3480 return -EINVAL;
3955 memset(v,0,sizeof(*v)); 3481 if (0 != t->index)
3956 strcpy(v->name, "Radio"); 3482 return -EINVAL;
3957 bttv_call_i2c_clients(btv,cmd,v); 3483 mutex_lock(&btv->lock);
3958 return 0; 3484 memset(t, 0, sizeof(*t));
3959 } 3485 strcpy(t->name, "Radio");
3960 case VIDIOCSTUNER: 3486 t->type = V4L2_TUNER_RADIO;
3961 /* nothing to do */
3962 return 0;
3963 3487
3964 case BTTV_VERSION: 3488 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
3965 case VIDIOCGFREQ: 3489
3966 case VIDIOCSFREQ: 3490 if (btv->audio_mode_gpio)
3967 case VIDIOCGAUDIO: 3491 btv->audio_mode_gpio(btv, t, 0);
3968 case VIDIOCSAUDIO: 3492
3969 case VIDIOC_LOG_STATUS: 3493 mutex_unlock(&btv->lock);
3970 case VIDIOC_DBG_G_REGISTER: 3494
3971 case VIDIOC_DBG_S_REGISTER: 3495 return 0;
3972 return bttv_common_ioctls(btv,cmd,arg); 3496}
3497
3498static int radio_enum_input(struct file *file, void *priv,
3499 struct v4l2_input *i)
3500{
3501 if (i->index != 0)
3502 return -EINVAL;
3503
3504 strcpy(i->name, "Radio");
3505 i->type = V4L2_INPUT_TYPE_TUNER;
3506
3507 return 0;
3508}
3509
3510static int radio_g_audio(struct file *file, void *priv,
3511 struct v4l2_audio *a)
3512{
3513 memset(a, 0, sizeof(*a));
3514 strcpy(a->name, "Radio");
3515 return 0;
3516}
3517
3518static int radio_s_tuner(struct file *file, void *priv,
3519 struct v4l2_tuner *t)
3520{
3521 struct bttv_fh *fh = priv;
3522 struct bttv *btv = fh->btv;
3523
3524 if (0 != t->index)
3525 return -EINVAL;
3526
3527 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
3528 return 0;
3529}
3530
3531static int radio_s_audio(struct file *file, void *priv,
3532 struct v4l2_audio *a)
3533{
3534 return 0;
3535}
3536
3537static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3538{
3539 return 0;
3540}
3541
3542static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3543{
3544 return 0;
3545}
3546
3547static int radio_queryctrl(struct file *file, void *priv,
3548 struct v4l2_queryctrl *c)
3549{
3550 const struct v4l2_queryctrl *ctrl;
3551
3552 if (c->id < V4L2_CID_BASE ||
3553 c->id >= V4L2_CID_LASTP1)
3554 return -EINVAL;
3555
3556 if (c->id == V4L2_CID_AUDIO_MUTE) {
3557 ctrl = ctrl_by_id(c->id);
3558 *c = *ctrl;
3559 } else
3560 *c = no_ctl;
3973 3561
3974 default:
3975 return -ENOIOCTLCMD;
3976 }
3977 return 0; 3562 return 0;
3978} 3563}
3979 3564
3980static int radio_ioctl(struct inode *inode, struct file *file, 3565static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3981 unsigned int cmd, unsigned long arg)
3982{ 3566{
3983 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl); 3567 *i = 0;
3568 return 0;
3984} 3569}
3985 3570
3986static ssize_t radio_read(struct file *file, char __user *data, 3571static ssize_t radio_read(struct file *file, char __user *data,
@@ -4016,17 +3601,29 @@ static const struct file_operations radio_fops =
4016 .open = radio_open, 3601 .open = radio_open,
4017 .read = radio_read, 3602 .read = radio_read,
4018 .release = radio_release, 3603 .release = radio_release,
4019 .ioctl = radio_ioctl, 3604 .ioctl = video_ioctl2,
4020 .llseek = no_llseek, 3605 .llseek = no_llseek,
4021 .poll = radio_poll, 3606 .poll = radio_poll,
4022}; 3607};
4023 3608
4024static struct video_device radio_template = 3609static struct video_device radio_template =
4025{ 3610{
4026 .name = "bt848/878 radio",
4027 .type = VID_TYPE_TUNER,
4028 .fops = &radio_fops, 3611 .fops = &radio_fops,
4029 .minor = -1, 3612 .minor = -1,
3613 .vidioc_querycap = radio_querycap,
3614 .vidioc_g_tuner = radio_g_tuner,
3615 .vidioc_enum_input = radio_enum_input,
3616 .vidioc_g_audio = radio_g_audio,
3617 .vidioc_s_tuner = radio_s_tuner,
3618 .vidioc_s_audio = radio_s_audio,
3619 .vidioc_s_input = radio_s_input,
3620 .vidioc_s_std = radio_s_std,
3621 .vidioc_queryctrl = radio_queryctrl,
3622 .vidioc_g_input = radio_g_input,
3623 .vidioc_g_ctrl = bttv_g_ctrl,
3624 .vidioc_s_ctrl = bttv_s_ctrl,
3625 .vidioc_g_frequency = bttv_g_frequency,
3626 .vidioc_s_frequency = bttv_s_frequency,
4030}; 3627};
4031 3628
4032/* ----------------------------------------------------------------------- */ 3629/* ----------------------------------------------------------------------- */
@@ -4308,20 +3905,20 @@ static void bttv_irq_timeout(unsigned long data)
4308 bttv_set_dma(btv, 0); 3905 bttv_set_dma(btv, 0);
4309 3906
4310 /* wake up */ 3907 /* wake up */
4311 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR); 3908 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
4312 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR); 3909 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
4313 3910
4314 /* cancel all outstanding capture / vbi requests */ 3911 /* cancel all outstanding capture / vbi requests */
4315 while (!list_empty(&btv->capture)) { 3912 while (!list_empty(&btv->capture)) {
4316 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue); 3913 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4317 list_del(&item->vb.queue); 3914 list_del(&item->vb.queue);
4318 item->vb.state = STATE_ERROR; 3915 item->vb.state = VIDEOBUF_ERROR;
4319 wake_up(&item->vb.done); 3916 wake_up(&item->vb.done);
4320 } 3917 }
4321 while (!list_empty(&btv->vcapture)) { 3918 while (!list_empty(&btv->vcapture)) {
4322 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue); 3919 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4323 list_del(&item->vb.queue); 3920 list_del(&item->vb.queue);
4324 item->vb.state = STATE_ERROR; 3921 item->vb.state = VIDEOBUF_ERROR;
4325 wake_up(&item->vb.done); 3922 wake_up(&item->vb.done);
4326 } 3923 }
4327 3924
@@ -4344,7 +3941,7 @@ bttv_irq_wakeup_top(struct bttv *btv)
4344 3941
4345 do_gettimeofday(&wakeup->vb.ts); 3942 do_gettimeofday(&wakeup->vb.ts);
4346 wakeup->vb.field_count = btv->field_count; 3943 wakeup->vb.field_count = btv->field_count;
4347 wakeup->vb.state = STATE_DONE; 3944 wakeup->vb.state = VIDEOBUF_DONE;
4348 wake_up(&wakeup->vb.done); 3945 wake_up(&wakeup->vb.done);
4349 spin_unlock(&btv->s_lock); 3946 spin_unlock(&btv->s_lock);
4350} 3947}
@@ -4393,7 +3990,7 @@ bttv_irq_switch_video(struct bttv *btv)
4393 } 3990 }
4394 3991
4395 /* wake up finished buffers */ 3992 /* wake up finished buffers */
4396 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE); 3993 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
4397 spin_unlock(&btv->s_lock); 3994 spin_unlock(&btv->s_lock);
4398} 3995}
4399 3996
@@ -4426,7 +4023,7 @@ bttv_irq_switch_vbi(struct bttv *btv)
4426 bttv_buffer_activate_vbi(btv, new); 4023 bttv_buffer_activate_vbi(btv, new);
4427 bttv_set_dma(btv, 0); 4024 bttv_set_dma(btv, 0);
4428 4025
4429 bttv_irq_wakeup_vbi(btv, old, STATE_DONE); 4026 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
4430 spin_unlock(&btv->s_lock); 4027 spin_unlock(&btv->s_lock);
4431} 4028}
4432 4029
@@ -4548,8 +4145,9 @@ static irqreturn_t bttv_irq(int irq, void *dev_id)
4548/* initialitation */ 4145/* initialitation */
4549 4146
4550static struct video_device *vdev_init(struct bttv *btv, 4147static struct video_device *vdev_init(struct bttv *btv,
4551 struct video_device *template, 4148 const struct video_device *template,
4552 char *type) 4149 const char *type_name,
4150 const int type)
4553{ 4151{
4554 struct video_device *vfd; 4152 struct video_device *vfd;
4555 4153
@@ -4560,9 +4158,10 @@ static struct video_device *vdev_init(struct bttv *btv,
4560 vfd->minor = -1; 4158 vfd->minor = -1;
4561 vfd->dev = &btv->c.pci->dev; 4159 vfd->dev = &btv->c.pci->dev;
4562 vfd->release = video_device_release; 4160 vfd->release = video_device_release;
4161 vfd->type = type;
4563 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)", 4162 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4564 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "", 4163 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4565 type, bttv_tvcards[btv->c.type].name); 4164 type_name, bttv_tvcards[btv->c.type].name);
4566 return vfd; 4165 return vfd;
4567} 4166}
4568 4167
@@ -4594,6 +4193,11 @@ static void bttv_unregister_video(struct bttv *btv)
4594/* register video4linux devices */ 4193/* register video4linux devices */
4595static int __devinit bttv_register_video(struct bttv *btv) 4194static int __devinit bttv_register_video(struct bttv *btv)
4596{ 4195{
4196 int video_type = VID_TYPE_CAPTURE |
4197 VID_TYPE_TUNER |
4198 VID_TYPE_CLIPPING|
4199 VID_TYPE_SCALES;
4200
4597 if (no_overlay <= 0) { 4201 if (no_overlay <= 0) {
4598 bttv_video_template.type |= VID_TYPE_OVERLAY; 4202 bttv_video_template.type |= VID_TYPE_OVERLAY;
4599 } else { 4203 } else {
@@ -4601,7 +4205,9 @@ static int __devinit bttv_register_video(struct bttv *btv)
4601 } 4205 }
4602 4206
4603 /* video */ 4207 /* video */
4604 btv->video_dev = vdev_init(btv, &bttv_video_template, "video"); 4208 btv->video_dev = vdev_init(btv, &bttv_video_template,
4209 "video", video_type);
4210
4605 if (NULL == btv->video_dev) 4211 if (NULL == btv->video_dev)
4606 goto err; 4212 goto err;
4607 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0) 4213 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
@@ -4616,7 +4222,9 @@ static int __devinit bttv_register_video(struct bttv *btv)
4616 } 4222 }
4617 4223
4618 /* vbi */ 4224 /* vbi */
4619 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi"); 4225 btv->vbi_dev = vdev_init(btv, &bttv_video_template,
4226 "vbi", VID_TYPE_TUNER | VID_TYPE_TELETEXT);
4227
4620 if (NULL == btv->vbi_dev) 4228 if (NULL == btv->vbi_dev)
4621 goto err; 4229 goto err;
4622 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0) 4230 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
@@ -4627,7 +4235,8 @@ static int __devinit bttv_register_video(struct bttv *btv)
4627 if (!btv->has_radio) 4235 if (!btv->has_radio)
4628 return 0; 4236 return 0;
4629 /* radio */ 4237 /* radio */
4630 btv->radio_dev = vdev_init(btv, &radio_template, "radio"); 4238 btv->radio_dev = vdev_init(btv, &radio_template,
4239 "radio", VID_TYPE_TUNER);
4631 if (NULL == btv->radio_dev) 4240 if (NULL == btv->radio_dev)
4632 goto err; 4241 goto err;
4633 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0) 4242 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
@@ -4768,7 +4377,7 @@ static int __devinit bttv_probe(struct pci_dev *dev,
4768 btv->init.btv = btv; 4377 btv->init.btv = btv;
4769 btv->init.ov.w.width = 320; 4378 btv->init.ov.w.width = 320;
4770 btv->init.ov.w.height = 240; 4379 btv->init.ov.w.height = 240;
4771 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24); 4380 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4772 btv->init.width = 320; 4381 btv->init.width = 320;
4773 btv->init.height = 240; 4382 btv->init.height = 240;
4774 btv->input = 0; 4383 btv->input = 0;
@@ -5013,14 +4622,17 @@ static int __init bttv_init_module(void)
5013 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret); 4622 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
5014 return ret; 4623 return ret;
5015 } 4624 }
5016 return pci_register_driver(&bttv_pci_driver); 4625 ret = pci_register_driver(&bttv_pci_driver);
4626 if (ret < 0)
4627 bus_unregister(&bttv_sub_bus_type);
4628
4629 return ret;
5017} 4630}
5018 4631
5019static void __exit bttv_cleanup_module(void) 4632static void __exit bttv_cleanup_module(void)
5020{ 4633{
5021 pci_unregister_driver(&bttv_pci_driver); 4634 pci_unregister_driver(&bttv_pci_driver);
5022 bus_unregister(&bttv_sub_bus_type); 4635 bus_unregister(&bttv_sub_bus_type);
5023 return;
5024} 4636}
5025 4637
5026module_init(bttv_init_module); 4638module_init(bttv_init_module);
diff --git a/drivers/media/video/bt8xx/bttv-input.c b/drivers/media/video/bt8xx/bttv-input.c
index e7c521b8444a..fc9ecb21eec6 100644
--- a/drivers/media/video/bt8xx/bttv-input.c
+++ b/drivers/media/video/bt8xx/bttv-input.c
@@ -69,6 +69,11 @@ static void ir_handle_key(struct bttv *btv)
69 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) { 69 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
70 ir_input_keydown(ir->dev,&ir->ir,data,data); 70 ir_input_keydown(ir->dev,&ir->ir,data,data);
71 } else { 71 } else {
72 /* HACK: Probably, ir->mask_keydown is missing
73 for this board */
74 if (btv->c.type == BTTV_BOARD_WINFAST2000)
75 ir_input_keydown(ir->dev, &ir->ir, data, data);
76
72 ir_input_nokey(ir->dev,&ir->ir); 77 ir_input_nokey(ir->dev,&ir->ir);
73 } 78 }
74 79
diff --git a/drivers/media/video/bt8xx/bttv-risc.c b/drivers/media/video/bt8xx/bttv-risc.c
index 58986f1a5f1a..e5979f77504c 100644
--- a/drivers/media/video/bt8xx/bttv-risc.c
+++ b/drivers/media/video/bt8xx/bttv-risc.c
@@ -582,7 +582,7 @@ bttv_dma_free(struct videobuf_queue *q,struct bttv *btv, struct bttv_buffer *buf
582 videobuf_dma_free(dma); 582 videobuf_dma_free(dma);
583 btcx_riscmem_free(btv->c.pci,&buf->bottom); 583 btcx_riscmem_free(btv->c.pci,&buf->bottom);
584 btcx_riscmem_free(btv->c.pci,&buf->top); 584 btcx_riscmem_free(btv->c.pci,&buf->top);
585 buf->vb.state = STATE_NEEDS_INIT; 585 buf->vb.state = VIDEOBUF_NEEDS_INIT;
586} 586}
587 587
588int 588int
@@ -602,7 +602,7 @@ bttv_buffer_activate_vbi(struct bttv *btv,
602 if (vbi) { 602 if (vbi) {
603 unsigned int crop, vdelay; 603 unsigned int crop, vdelay;
604 604
605 vbi->vb.state = STATE_ACTIVE; 605 vbi->vb.state = VIDEOBUF_ACTIVE;
606 list_del(&vbi->vb.queue); 606 list_del(&vbi->vb.queue);
607 607
608 /* VDELAY is start of video, end of VBI capturing. */ 608 /* VDELAY is start of video, end of VBI capturing. */
@@ -644,12 +644,12 @@ bttv_buffer_activate_video(struct bttv *btv,
644 /* video capture */ 644 /* video capture */
645 if (NULL != set->top && NULL != set->bottom) { 645 if (NULL != set->top && NULL != set->bottom) {
646 if (set->top == set->bottom) { 646 if (set->top == set->bottom) {
647 set->top->vb.state = STATE_ACTIVE; 647 set->top->vb.state = VIDEOBUF_ACTIVE;
648 if (set->top->vb.queue.next) 648 if (set->top->vb.queue.next)
649 list_del(&set->top->vb.queue); 649 list_del(&set->top->vb.queue);
650 } else { 650 } else {
651 set->top->vb.state = STATE_ACTIVE; 651 set->top->vb.state = VIDEOBUF_ACTIVE;
652 set->bottom->vb.state = STATE_ACTIVE; 652 set->bottom->vb.state = VIDEOBUF_ACTIVE;
653 if (set->top->vb.queue.next) 653 if (set->top->vb.queue.next)
654 list_del(&set->top->vb.queue); 654 list_del(&set->top->vb.queue);
655 if (set->bottom->vb.queue.next) 655 if (set->bottom->vb.queue.next)
@@ -666,7 +666,7 @@ bttv_buffer_activate_video(struct bttv *btv,
666 btaor((set->top->btswap & 0x0a) | (set->bottom->btswap & 0x05), 666 btaor((set->top->btswap & 0x0a) | (set->bottom->btswap & 0x05),
667 ~0x0f, BT848_COLOR_CTL); 667 ~0x0f, BT848_COLOR_CTL);
668 } else if (NULL != set->top) { 668 } else if (NULL != set->top) {
669 set->top->vb.state = STATE_ACTIVE; 669 set->top->vb.state = VIDEOBUF_ACTIVE;
670 if (set->top->vb.queue.next) 670 if (set->top->vb.queue.next)
671 list_del(&set->top->vb.queue); 671 list_del(&set->top->vb.queue);
672 bttv_apply_geo(btv, &set->top->geo,1); 672 bttv_apply_geo(btv, &set->top->geo,1);
@@ -677,7 +677,7 @@ bttv_buffer_activate_video(struct bttv *btv,
677 btaor(set->top->btformat & 0xff, ~0xff, BT848_COLOR_FMT); 677 btaor(set->top->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
678 btaor(set->top->btswap & 0x0f, ~0x0f, BT848_COLOR_CTL); 678 btaor(set->top->btswap & 0x0f, ~0x0f, BT848_COLOR_CTL);
679 } else if (NULL != set->bottom) { 679 } else if (NULL != set->bottom) {
680 set->bottom->vb.state = STATE_ACTIVE; 680 set->bottom->vb.state = VIDEOBUF_ACTIVE;
681 if (set->bottom->vb.queue.next) 681 if (set->bottom->vb.queue.next)
682 list_del(&set->bottom->vb.queue); 682 list_del(&set->bottom->vb.queue);
683 bttv_apply_geo(btv, &set->bottom->geo,1); 683 bttv_apply_geo(btv, &set->bottom->geo,1);
diff --git a/drivers/media/video/bt8xx/bttv-vbi.c b/drivers/media/video/bt8xx/bttv-vbi.c
index 346ce019bdcb..1f0cc79e2a33 100644
--- a/drivers/media/video/bt8xx/bttv-vbi.c
+++ b/drivers/media/video/bt8xx/bttv-vbi.c
@@ -142,7 +142,7 @@ static int vbi_buffer_prepare(struct videobuf_queue *q,
142 redo_dma_risc = 1; 142 redo_dma_risc = 1;
143 } 143 }
144 144
145 if (STATE_NEEDS_INIT == buf->vb.state) { 145 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
146 redo_dma_risc = 1; 146 redo_dma_risc = 1;
147 if (0 != (rc = videobuf_iolock(q, &buf->vb, NULL))) 147 if (0 != (rc = videobuf_iolock(q, &buf->vb, NULL)))
148 goto fail; 148 goto fail;
@@ -189,7 +189,7 @@ static int vbi_buffer_prepare(struct videobuf_queue *q,
189 /* For bttv_buffer_activate_vbi(). */ 189 /* For bttv_buffer_activate_vbi(). */
190 buf->geo.vdelay = min_vdelay; 190 buf->geo.vdelay = min_vdelay;
191 191
192 buf->vb.state = STATE_PREPARED; 192 buf->vb.state = VIDEOBUF_PREPARED;
193 buf->vb.field = field; 193 buf->vb.field = field;
194 dprintk("buf prepare %p: top=%p bottom=%p field=%s\n", 194 dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
195 vb, &buf->top, &buf->bottom, 195 vb, &buf->top, &buf->bottom,
@@ -209,7 +209,7 @@ vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
209 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb); 209 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
210 210
211 dprintk("queue %p\n",vb); 211 dprintk("queue %p\n",vb);
212 buf->vb.state = STATE_QUEUED; 212 buf->vb.state = VIDEOBUF_QUEUED;
213 list_add_tail(&buf->vb.queue,&btv->vcapture); 213 list_add_tail(&buf->vb.queue,&btv->vcapture);
214 if (NULL == btv->cvbi) { 214 if (NULL == btv->cvbi) {
215 fh->btv->loop_irq |= 4; 215 fh->btv->loop_irq |= 4;
@@ -236,10 +236,8 @@ struct videobuf_queue_ops bttv_vbi_qops = {
236 236
237/* ----------------------------------------------------------------------- */ 237/* ----------------------------------------------------------------------- */
238 238
239static int 239static int try_fmt(struct v4l2_vbi_format *f, const struct bttv_tvnorm *tvnorm,
240try_fmt (struct v4l2_vbi_format * f, 240 __s32 crop_start)
241 const struct bttv_tvnorm * tvnorm,
242 __s32 crop_start)
243{ 241{
244 __s32 min_start, max_start, max_end, f2_offset; 242 __s32 min_start, max_start, max_end, f2_offset;
245 unsigned int i; 243 unsigned int i;
@@ -305,10 +303,9 @@ try_fmt (struct v4l2_vbi_format * f,
305 return 0; 303 return 0;
306} 304}
307 305
308int 306int bttv_try_fmt_vbi(struct file *file, void *f, struct v4l2_format *frt)
309bttv_vbi_try_fmt (struct bttv_fh * fh,
310 struct v4l2_vbi_format * f)
311{ 307{
308 struct bttv_fh *fh = f;
312 struct bttv *btv = fh->btv; 309 struct bttv *btv = fh->btv;
313 const struct bttv_tvnorm *tvnorm; 310 const struct bttv_tvnorm *tvnorm;
314 __s32 crop_start; 311 __s32 crop_start;
@@ -320,13 +317,13 @@ bttv_vbi_try_fmt (struct bttv_fh * fh,
320 317
321 mutex_unlock(&btv->lock); 318 mutex_unlock(&btv->lock);
322 319
323 return try_fmt(f, tvnorm, crop_start); 320 return try_fmt(&frt->fmt.vbi, tvnorm, crop_start);
324} 321}
325 322
326int 323
327bttv_vbi_set_fmt (struct bttv_fh * fh, 324int bttv_s_fmt_vbi(struct file *file, void *f, struct v4l2_format *frt)
328 struct v4l2_vbi_format * f)
329{ 325{
326 struct bttv_fh *fh = f;
330 struct bttv *btv = fh->btv; 327 struct bttv *btv = fh->btv;
331 const struct bttv_tvnorm *tvnorm; 328 const struct bttv_tvnorm *tvnorm;
332 __s32 start1, end; 329 __s32 start1, end;
@@ -340,11 +337,12 @@ bttv_vbi_set_fmt (struct bttv_fh * fh,
340 337
341 tvnorm = &bttv_tvnorms[btv->tvnorm]; 338 tvnorm = &bttv_tvnorms[btv->tvnorm];
342 339
343 rc = try_fmt(f, tvnorm, btv->crop_start); 340 rc = try_fmt(&frt->fmt.vbi, tvnorm, btv->crop_start);
344 if (0 != rc) 341 if (0 != rc)
345 goto fail; 342 goto fail;
346 343
347 start1 = f->start[1] - tvnorm->vbistart[1] + tvnorm->vbistart[0]; 344 start1 = frt->fmt.vbi.start[1] - tvnorm->vbistart[1] +
345 tvnorm->vbistart[0];
348 346
349 /* First possible line of video capturing. Should be 347 /* First possible line of video capturing. Should be
350 max(f->start[0] + f->count[0], start1 + f->count[1]) * 2 348 max(f->start[0] + f->count[0], start1 + f->count[1]) * 2
@@ -352,11 +350,11 @@ bttv_vbi_set_fmt (struct bttv_fh * fh,
352 pretend the VBI and video capture window may overlap, 350 pretend the VBI and video capture window may overlap,
353 so end = start + 1, the lowest possible value, times two 351 so end = start + 1, the lowest possible value, times two
354 because vbi_fmt.end counts field lines times two. */ 352 because vbi_fmt.end counts field lines times two. */
355 end = max(f->start[0], start1) * 2 + 2; 353 end = max(frt->fmt.vbi.start[0], start1) * 2 + 2;
356 354
357 mutex_lock(&fh->vbi.lock); 355 mutex_lock(&fh->vbi.lock);
358 356
359 fh->vbi_fmt.fmt = *f; 357 fh->vbi_fmt.fmt = frt->fmt.vbi;
360 fh->vbi_fmt.tvnorm = tvnorm; 358 fh->vbi_fmt.tvnorm = tvnorm;
361 fh->vbi_fmt.end = end; 359 fh->vbi_fmt.end = end;
362 360
@@ -370,13 +368,13 @@ bttv_vbi_set_fmt (struct bttv_fh * fh,
370 return rc; 368 return rc;
371} 369}
372 370
373void 371
374bttv_vbi_get_fmt (struct bttv_fh * fh, 372int bttv_g_fmt_vbi(struct file *file, void *f, struct v4l2_format *frt)
375 struct v4l2_vbi_format * f)
376{ 373{
374 struct bttv_fh *fh = f;
377 const struct bttv_tvnorm *tvnorm; 375 const struct bttv_tvnorm *tvnorm;
378 376
379 *f = fh->vbi_fmt.fmt; 377 frt->fmt.vbi = fh->vbi_fmt.fmt;
380 378
381 tvnorm = &bttv_tvnorms[fh->btv->tvnorm]; 379 tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
382 380
@@ -391,28 +389,28 @@ bttv_vbi_get_fmt (struct bttv_fh * fh,
391 max_end = (tvnorm->cropcap.bounds.top 389 max_end = (tvnorm->cropcap.bounds.top
392 + tvnorm->cropcap.bounds.height) >> 1; 390 + tvnorm->cropcap.bounds.height) >> 1;
393 391
394 f->sampling_rate = tvnorm->Fsc; 392 frt->fmt.vbi.sampling_rate = tvnorm->Fsc;
395 393
396 for (i = 0; i < 2; ++i) { 394 for (i = 0; i < 2; ++i) {
397 __s32 new_start; 395 __s32 new_start;
398 396
399 new_start = f->start[i] 397 new_start = frt->fmt.vbi.start[i]
400 + tvnorm->vbistart[i] 398 + tvnorm->vbistart[i]
401 - fh->vbi_fmt.tvnorm->vbistart[i]; 399 - fh->vbi_fmt.tvnorm->vbistart[i];
402 400
403 f->start[i] = min(new_start, max_end - 1); 401 frt->fmt.vbi.start[i] = min(new_start, max_end - 1);
404 f->count[i] = min((__s32) f->count[i], 402 frt->fmt.vbi.count[i] =
405 max_end - f->start[i]); 403 min((__s32) frt->fmt.vbi.count[i],
404 max_end - frt->fmt.vbi.start[i]);
406 405
407 max_end += tvnorm->vbistart[1] 406 max_end += tvnorm->vbistart[1]
408 - tvnorm->vbistart[0]; 407 - tvnorm->vbistart[0];
409 } 408 }
410 } 409 }
410 return 0;
411} 411}
412 412
413void 413void bttv_vbi_fmt_reset(struct bttv_vbi_fmt *f, int norm)
414bttv_vbi_fmt_reset (struct bttv_vbi_fmt * f,
415 int norm)
416{ 414{
417 const struct bttv_tvnorm *tvnorm; 415 const struct bttv_tvnorm *tvnorm;
418 unsigned int real_samples_per_line; 416 unsigned int real_samples_per_line;
diff --git a/drivers/media/video/bt8xx/bttv.h b/drivers/media/video/bt8xx/bttv.h
index 19e75d50a107..bf4c339a520c 100644
--- a/drivers/media/video/bt8xx/bttv.h
+++ b/drivers/media/video/bt8xx/bttv.h
@@ -241,7 +241,10 @@ struct tvcard
241 unsigned int radio_addr; 241 unsigned int radio_addr;
242 242
243 unsigned int has_radio; 243 unsigned int has_radio;
244 void (*audio_hook)(struct bttv *btv, struct video_audio *v, int set); 244
245 void (*volume_gpio)(struct bttv *btv, __u16 volume);
246 void (*audio_mode_gpio)(struct bttv *btv, struct v4l2_tuner *tuner, int set);
247
245 void (*muxsel_hook)(struct bttv *btv, unsigned int input); 248 void (*muxsel_hook)(struct bttv *btv, unsigned int input);
246}; 249};
247 250
diff --git a/drivers/media/video/bt8xx/bttvp.h b/drivers/media/video/bt8xx/bttvp.h
index d4ac4c4b49b4..1305d315cfc5 100644
--- a/drivers/media/video/bt8xx/bttvp.h
+++ b/drivers/media/video/bt8xx/bttvp.h
@@ -84,6 +84,11 @@
84 84
85#define clamp(x, low, high) min (max (low, x), high) 85#define clamp(x, low, high) min (max (low, x), high)
86 86
87#define BTTV_NORMS (\
88 V4L2_STD_PAL | V4L2_STD_PAL_N | \
89 V4L2_STD_PAL_Nc | V4L2_STD_SECAM | \
90 V4L2_STD_NTSC | V4L2_STD_PAL_M | \
91 V4L2_STD_PAL_60)
87/* ---------------------------------------------------------- */ 92/* ---------------------------------------------------------- */
88 93
89struct bttv_tvnorm { 94struct bttv_tvnorm {
@@ -112,7 +117,6 @@ extern const struct bttv_tvnorm bttv_tvnorms[];
112 117
113struct bttv_format { 118struct bttv_format {
114 char *name; 119 char *name;
115 int palette; /* video4linux 1 */
116 int fourcc; /* video4linux 2 */ 120 int fourcc; /* video4linux 2 */
117 int btformat; /* BT848_COLOR_FMT_* */ 121 int btformat; /* BT848_COLOR_FMT_* */
118 int btswap; /* BT848_COLOR_CTL_* */ 122 int btswap; /* BT848_COLOR_CTL_* */
@@ -253,9 +257,9 @@ int bttv_overlay_risc(struct bttv *btv, struct bttv_overlay *ov,
253/* ---------------------------------------------------------- */ 257/* ---------------------------------------------------------- */
254/* bttv-vbi.c */ 258/* bttv-vbi.c */
255 259
256int bttv_vbi_try_fmt(struct bttv_fh *fh, struct v4l2_vbi_format *f); 260int bttv_try_fmt_vbi(struct file *file, void *fh, struct v4l2_format *f);
257void bttv_vbi_get_fmt(struct bttv_fh *fh, struct v4l2_vbi_format *f); 261int bttv_g_fmt_vbi(struct file *file, void *fh, struct v4l2_format *f);
258int bttv_vbi_set_fmt(struct bttv_fh *fh, struct v4l2_vbi_format *f); 262int bttv_s_fmt_vbi(struct file *file, void *fh, struct v4l2_format *f);
259 263
260extern struct videobuf_queue_ops bttv_vbi_qops; 264extern struct videobuf_queue_ops bttv_vbi_qops;
261 265
@@ -337,7 +341,9 @@ struct bttv {
337 /* old gpio interface */ 341 /* old gpio interface */
338 wait_queue_head_t gpioq; 342 wait_queue_head_t gpioq;
339 int shutdown; 343 int shutdown;
340 void (*audio_hook)(struct bttv *btv, struct video_audio *v, int set); 344
345 void (*volume_gpio)(struct bttv *btv, __u16 volume);
346 void (*audio_mode_gpio)(struct bttv *btv, struct v4l2_tuner *tuner, int set);
341 347
342 /* new gpio interface */ 348 /* new gpio interface */
343 spinlock_t gpio_lock; 349 spinlock_t gpio_lock;
@@ -458,10 +464,6 @@ struct bttv {
458extern unsigned int bttv_num; 464extern unsigned int bttv_num;
459extern struct bttv bttvs[BTTV_MAX]; 465extern struct bttv bttvs[BTTV_MAX];
460 466
461/* private ioctls */
462#define BTTV_VERSION _IOR('v' , BASE_VIDIOCPRIVATE+6, int)
463#define BTTV_VBISIZE _IOR('v' , BASE_VIDIOCPRIVATE+8, int)
464
465#endif 467#endif
466 468
467#define btwrite(dat,adr) writel((dat), btv->bt848_mmio+(adr)) 469#define btwrite(dat,adr) writel((dat), btv->bt848_mmio+(adr))
diff --git a/drivers/media/video/bw-qcam.c b/drivers/media/video/bw-qcam.c
index 58423525591f..032265383df2 100644
--- a/drivers/media/video/bw-qcam.c
+++ b/drivers/media/video/bw-qcam.c
@@ -82,11 +82,16 @@ OTHER DEALINGS IN THE SOFTWARE.
82static unsigned int maxpoll=250; /* Maximum busy-loop count for qcam I/O */ 82static unsigned int maxpoll=250; /* Maximum busy-loop count for qcam I/O */
83static unsigned int yieldlines=4; /* Yield after this many during capture */ 83static unsigned int yieldlines=4; /* Yield after this many during capture */
84static int video_nr = -1; 84static int video_nr = -1;
85static unsigned int force_init; /* Whether to probe aggressively */
85 86
86module_param(maxpoll, int, 0); 87module_param(maxpoll, int, 0);
87module_param(yieldlines, int, 0); 88module_param(yieldlines, int, 0);
88module_param(video_nr, int, 0); 89module_param(video_nr, int, 0);
89 90
91/* Set force_init=1 to avoid detection by polling status register and
92 * immediately attempt to initialize qcam */
93module_param(force_init, int, 0);
94
90static inline int read_lpstatus(struct qcam_device *q) 95static inline int read_lpstatus(struct qcam_device *q)
91{ 96{
92 return parport_read_status(q->pport); 97 return parport_read_status(q->pport);
@@ -331,6 +336,9 @@ static int qc_detect(struct qcam_device *q)
331 int count = 0; 336 int count = 0;
332 int i; 337 int i;
333 338
339 if (force_init)
340 return 1;
341
334 lastreg = reg = read_lpstatus(q) & 0xf0; 342 lastreg = reg = read_lpstatus(q) & 0xf0;
335 343
336 for (i = 0; i < 500; i++) 344 for (i = 0; i < 500; i++)
@@ -354,12 +362,12 @@ static int qc_detect(struct qcam_device *q)
354 362
355 /* Be (even more) liberal in what you accept... */ 363 /* Be (even more) liberal in what you accept... */
356 364
357/* if (count > 30 && count < 200) */
358 if (count > 20 && count < 400) { 365 if (count > 20 && count < 400) {
359 return 1; /* found */ 366 return 1; /* found */
360 } else { 367 } else {
361 printk(KERN_ERR "No Quickcam found on port %s\n", 368 printk(KERN_ERR "No Quickcam found on port %s\n",
362 q->pport->name); 369 q->pport->name);
370 printk(KERN_DEBUG "Quickcam detection counter: %u\n", count);
363 return 0; /* not found */ 371 return 0; /* not found */
364 } 372 }
365} 373}
diff --git a/drivers/media/video/cs5345.c b/drivers/media/video/cs5345.c
new file mode 100644
index 000000000000..fae469ce16f5
--- /dev/null
+++ b/drivers/media/video/cs5345.c
@@ -0,0 +1,168 @@
1/*
2 * cs5345 Cirrus Logic 24-bit, 192 kHz Stereo Audio ADC
3 * Copyright (C) 2007 Hans Verkuil
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
21#include <linux/version.h>
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/i2c.h>
25#include <linux/videodev2.h>
26#include <media/v4l2-i2c-drv.h>
27#include <media/v4l2-common.h>
28#include <media/v4l2-chip-ident.h>
29
30MODULE_DESCRIPTION("i2c device driver for cs5345 Audio ADC");
31MODULE_AUTHOR("Hans Verkuil");
32MODULE_LICENSE("GPL");
33
34static int debug;
35
36module_param(debug, bool, 0644);
37
38MODULE_PARM_DESC(debug, "Debugging messages\n\t\t\t0=Off (default), 1=On");
39
40
41/* ----------------------------------------------------------------------- */
42
43static inline int cs5345_write(struct i2c_client *client, u8 reg, u8 value)
44{
45 return i2c_smbus_write_byte_data(client, reg, value);
46}
47
48static inline int cs5345_read(struct i2c_client *client, u8 reg)
49{
50 return i2c_smbus_read_byte_data(client, reg);
51}
52
53static int cs5345_command(struct i2c_client *client, unsigned cmd, void *arg)
54{
55 struct v4l2_routing *route = arg;
56 struct v4l2_control *ctrl = arg;
57
58 switch (cmd) {
59 case VIDIOC_INT_G_AUDIO_ROUTING:
60 route->input = cs5345_read(client, 0x09) & 7;
61 route->input |= cs5345_read(client, 0x05) & 0x70;
62 route->output = 0;
63 break;
64
65 case VIDIOC_INT_S_AUDIO_ROUTING:
66 if ((route->input & 0xf) > 6) {
67 v4l_err(client, "Invalid input %d.\n", route->input);
68 return -EINVAL;
69 }
70 cs5345_write(client, 0x09, route->input & 0xf);
71 cs5345_write(client, 0x05, route->input & 0xf0);
72 break;
73
74 case VIDIOC_G_CTRL:
75 if (ctrl->id == V4L2_CID_AUDIO_MUTE) {
76 ctrl->value = (cs5345_read(client, 0x04) & 0x08) != 0;
77 break;
78 }
79 if (ctrl->id != V4L2_CID_AUDIO_VOLUME)
80 return -EINVAL;
81 ctrl->value = cs5345_read(client, 0x07) & 0x3f;
82 if (ctrl->value >= 32)
83 ctrl->value = ctrl->value - 64;
84 break;
85
86 case VIDIOC_S_CTRL:
87 break;
88 if (ctrl->id == V4L2_CID_AUDIO_MUTE) {
89 cs5345_write(client, 0x04, ctrl->value ? 0x80 : 0);
90 break;
91 }
92 if (ctrl->id != V4L2_CID_AUDIO_VOLUME)
93 return -EINVAL;
94 if (ctrl->value > 24 || ctrl->value < -24)
95 return -EINVAL;
96 cs5345_write(client, 0x07, ((u8)ctrl->value) & 0x3f);
97 cs5345_write(client, 0x08, ((u8)ctrl->value) & 0x3f);
98 break;
99
100#ifdef CONFIG_VIDEO_ADV_DEBUG
101 case VIDIOC_DBG_G_REGISTER:
102 case VIDIOC_DBG_S_REGISTER:
103 {
104 struct v4l2_register *reg = arg;
105
106 if (!v4l2_chip_match_i2c_client(client,
107 reg->match_type, reg->match_chip))
108 return -EINVAL;
109 if (!capable(CAP_SYS_ADMIN))
110 return -EPERM;
111 if (cmd == VIDIOC_DBG_G_REGISTER)
112 reg->val = cs5345_read(client, reg->reg & 0x1f);
113 else
114 cs5345_write(client, reg->reg & 0x1f, reg->val & 0x1f);
115 break;
116 }
117#endif
118
119 case VIDIOC_G_CHIP_IDENT:
120 return v4l2_chip_ident_i2c_client(client,
121 arg, V4L2_IDENT_CS5345, 0);
122
123 case VIDIOC_LOG_STATUS:
124 {
125 u8 v = cs5345_read(client, 0x09) & 7;
126 u8 m = cs5345_read(client, 0x04);
127 int vol = cs5345_read(client, 0x08) & 0x3f;
128
129 v4l_info(client, "Input: %d%s\n", v,
130 (m & 0x80) ? " (muted)" : "");
131 if (vol >= 32)
132 vol = vol - 64;
133 v4l_info(client, "Volume: %d dB\n", vol);
134 break;
135 }
136
137 default:
138 return -EINVAL;
139 }
140 return 0;
141}
142
143/* ----------------------------------------------------------------------- */
144
145static int cs5345_probe(struct i2c_client *client)
146{
147 /* Check if the adapter supports the needed features */
148 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
149 return -EIO;
150
151 v4l_info(client, "chip found @ 0x%x (%s)\n",
152 client->addr << 1, client->adapter->name);
153
154 cs5345_write(client, 0x02, 0x00);
155 cs5345_write(client, 0x04, 0x01);
156 cs5345_write(client, 0x09, 0x01);
157 return 0;
158}
159
160/* ----------------------------------------------------------------------- */
161
162static struct v4l2_i2c_driver_data v4l2_i2c_data = {
163 .name = "cs5345",
164 .driverid = I2C_DRIVERID_CS5345,
165 .command = cs5345_command,
166 .probe = cs5345_probe,
167};
168
diff --git a/drivers/media/video/cs53l32a.c b/drivers/media/video/cs53l32a.c
index a73e285af730..f41bfde045fe 100644
--- a/drivers/media/video/cs53l32a.c
+++ b/drivers/media/video/cs53l32a.c
@@ -29,12 +29,13 @@
29#include <linux/videodev.h> 29#include <linux/videodev.h>
30#include <media/v4l2-common.h> 30#include <media/v4l2-common.h>
31#include <media/v4l2-chip-ident.h> 31#include <media/v4l2-chip-ident.h>
32#include <media/v4l2-i2c-drv-legacy.h>
32 33
33MODULE_DESCRIPTION("i2c device driver for cs53l32a Audio ADC"); 34MODULE_DESCRIPTION("i2c device driver for cs53l32a Audio ADC");
34MODULE_AUTHOR("Martin Vaughan"); 35MODULE_AUTHOR("Martin Vaughan");
35MODULE_LICENSE("GPL"); 36MODULE_LICENSE("GPL");
36 37
37static int debug = 0; 38static int debug;
38 39
39module_param(debug, bool, 0644); 40module_param(debug, bool, 0644);
40 41
@@ -57,8 +58,7 @@ static int cs53l32a_read(struct i2c_client *client, u8 reg)
57 return i2c_smbus_read_byte_data(client, reg); 58 return i2c_smbus_read_byte_data(client, reg);
58} 59}
59 60
60static int cs53l32a_command(struct i2c_client *client, unsigned int cmd, 61static int cs53l32a_command(struct i2c_client *client, unsigned cmd, void *arg)
61 void *arg)
62{ 62{
63 struct v4l2_routing *route = arg; 63 struct v4l2_routing *route = arg;
64 struct v4l2_control *ctrl = arg; 64 struct v4l2_control *ctrl = arg;
@@ -105,7 +105,8 @@ static int cs53l32a_command(struct i2c_client *client, unsigned int cmd,
105 break; 105 break;
106 106
107 case VIDIOC_G_CHIP_IDENT: 107 case VIDIOC_G_CHIP_IDENT:
108 return v4l2_chip_ident_i2c_client(client, arg, V4L2_IDENT_CS53l32A, 0); 108 return v4l2_chip_ident_i2c_client(client,
109 arg, V4L2_IDENT_CS53l32A, 0);
109 110
110 case VIDIOC_LOG_STATUS: 111 case VIDIOC_LOG_STATUS:
111 { 112 {
@@ -134,27 +135,18 @@ static int cs53l32a_command(struct i2c_client *client, unsigned int cmd,
134 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1' 135 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
135 */ 136 */
136 137
137static struct i2c_driver i2c_driver; 138static int cs53l32a_probe(struct i2c_client *client)
138
139static int cs53l32a_attach(struct i2c_adapter *adapter, int address, int kind)
140{ 139{
141 struct i2c_client *client;
142 int i; 140 int i;
143 141
144 /* Check if the adapter supports the needed features */ 142 /* Check if the adapter supports the needed features */
145 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 143 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
146 return 0; 144 return -EIO;
147
148 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
149 if (client == 0)
150 return -ENOMEM;
151 145
152 client->addr = address;
153 client->adapter = adapter;
154 client->driver = &i2c_driver;
155 snprintf(client->name, sizeof(client->name) - 1, "cs53l32a"); 146 snprintf(client->name, sizeof(client->name) - 1, "cs53l32a");
156 147
157 v4l_info(client, "chip found @ 0x%x (%s)\n", address << 1, adapter->name); 148 v4l_info(client, "chip found @ 0x%x (%s)\n",
149 client->addr << 1, client->adapter->name);
158 150
159 for (i = 1; i <= 7; i++) { 151 for (i = 1; i <= 7; i++) {
160 u8 v = cs53l32a_read(client, i); 152 u8 v = cs53l32a_read(client, i);
@@ -179,55 +171,13 @@ static int cs53l32a_attach(struct i2c_adapter *adapter, int address, int kind)
179 171
180 v4l_dbg(1, debug, client, "Read Reg %d %02x\n", i, v); 172 v4l_dbg(1, debug, client, "Read Reg %d %02x\n", i, v);
181 } 173 }
182
183 i2c_attach_client(client);
184
185 return 0; 174 return 0;
186} 175}
187 176
188static int cs53l32a_probe(struct i2c_adapter *adapter) 177static struct v4l2_i2c_driver_data v4l2_i2c_data = {
189{ 178 .name = "cs53l32a",
190 if (adapter->class & I2C_CLASS_TV_ANALOG) 179 .driverid = I2C_DRIVERID_CS53L32A,
191 return i2c_probe(adapter, &addr_data, cs53l32a_attach);
192 return 0;
193}
194
195static int cs53l32a_detach(struct i2c_client *client)
196{
197 int err;
198
199 err = i2c_detach_client(client);
200 if (err) {
201 return err;
202 }
203 kfree(client);
204
205 return 0;
206}
207
208/* ----------------------------------------------------------------------- */
209
210/* i2c implementation */
211static struct i2c_driver i2c_driver = {
212 .driver = {
213 .name = "cs53l32a",
214 },
215 .id = I2C_DRIVERID_CS53L32A,
216 .attach_adapter = cs53l32a_probe,
217 .detach_client = cs53l32a_detach,
218 .command = cs53l32a_command, 180 .command = cs53l32a_command,
181 .probe = cs53l32a_probe,
219}; 182};
220 183
221
222static int __init cs53l32a_init_module(void)
223{
224 return i2c_add_driver(&i2c_driver);
225}
226
227static void __exit cs53l32a_cleanup_module(void)
228{
229 i2c_del_driver(&i2c_driver);
230}
231
232module_init(cs53l32a_init_module);
233module_exit(cs53l32a_cleanup_module);
diff --git a/drivers/media/video/cx2341x.c b/drivers/media/video/cx2341x.c
index 62304255dcae..c592899a2317 100644
--- a/drivers/media/video/cx2341x.c
+++ b/drivers/media/video/cx2341x.c
@@ -34,7 +34,7 @@ MODULE_DESCRIPTION("cx23415/6 driver");
34MODULE_AUTHOR("Hans Verkuil"); 34MODULE_AUTHOR("Hans Verkuil");
35MODULE_LICENSE("GPL"); 35MODULE_LICENSE("GPL");
36 36
37static int debug = 0; 37static int debug;
38module_param(debug, int, 0644); 38module_param(debug, int, 0644);
39MODULE_PARM_DESC(debug, "Debug level (0-1)"); 39MODULE_PARM_DESC(debug, "Debug level (0-1)");
40 40
@@ -75,6 +75,7 @@ const u32 cx2341x_mpeg_ctrls[] = {
75 V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS, 75 V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS,
76 0 76 0
77}; 77};
78EXPORT_SYMBOL(cx2341x_mpeg_ctrls);
78 79
79 80
80/* Map the control ID to the correct field in the cx2341x_mpeg_params 81/* Map the control ID to the correct field in the cx2341x_mpeg_params
@@ -281,13 +282,14 @@ static int cx2341x_set_ctrl(struct cx2341x_mpeg_params *params, int busy,
281 return -EBUSY; 282 return -EBUSY;
282 params->stream_type = ctrl->value; 283 params->stream_type = ctrl->value;
283 params->video_encoding = 284 params->video_encoding =
284 (params->stream_type == V4L2_MPEG_STREAM_TYPE_MPEG1_SS || 285 (params->stream_type == V4L2_MPEG_STREAM_TYPE_MPEG1_SS ||
285 params->stream_type == V4L2_MPEG_STREAM_TYPE_MPEG1_VCD) ? 286 params->stream_type == V4L2_MPEG_STREAM_TYPE_MPEG1_VCD) ?
286 V4L2_MPEG_VIDEO_ENCODING_MPEG_1 : V4L2_MPEG_VIDEO_ENCODING_MPEG_2; 287 V4L2_MPEG_VIDEO_ENCODING_MPEG_1 :
287 if (params->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1) { 288 V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
289 if (params->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
288 /* MPEG-1 implies CBR */ 290 /* MPEG-1 implies CBR */
289 params->video_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR; 291 params->video_bitrate_mode =
290 } 292 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
291 break; 293 break;
292 case V4L2_CID_MPEG_STREAM_VBI_FMT: 294 case V4L2_CID_MPEG_STREAM_VBI_FMT:
293 params->stream_vbi_fmt = ctrl->value; 295 params->stream_vbi_fmt = ctrl->value;
@@ -334,7 +336,8 @@ static int cx2341x_set_ctrl(struct cx2341x_mpeg_params *params, int busy,
334 return 0; 336 return 0;
335} 337}
336 338
337static int cx2341x_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def) 339static int cx2341x_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
340 s32 min, s32 max, s32 step, s32 def)
338{ 341{
339 const char *name; 342 const char *name;
340 343
@@ -417,7 +420,8 @@ static int cx2341x_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 ma
417 return 0; 420 return 0;
418} 421}
419 422
420int cx2341x_ctrl_query(struct cx2341x_mpeg_params *params, struct v4l2_queryctrl *qctrl) 423int cx2341x_ctrl_query(struct cx2341x_mpeg_params *params,
424 struct v4l2_queryctrl *qctrl)
421{ 425{
422 int err; 426 int err;
423 427
@@ -440,7 +444,8 @@ int cx2341x_ctrl_query(struct cx2341x_mpeg_params *params, struct v4l2_queryctrl
440 444
441 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: 445 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
442 err = v4l2_ctrl_query_fill_std(qctrl); 446 err = v4l2_ctrl_query_fill_std(qctrl);
443 if (err == 0 && params->audio_mode != V4L2_MPEG_AUDIO_MODE_JOINT_STEREO) 447 if (err == 0 &&
448 params->audio_mode != V4L2_MPEG_AUDIO_MODE_JOINT_STEREO)
444 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 449 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
445 return err; 450 return err;
446 451
@@ -455,13 +460,16 @@ int cx2341x_ctrl_query(struct cx2341x_mpeg_params *params, struct v4l2_queryctrl
455 460
456 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: 461 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
457 err = v4l2_ctrl_query_fill_std(qctrl); 462 err = v4l2_ctrl_query_fill_std(qctrl);
458 if (err == 0 && params->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1) 463 if (err == 0 &&
464 params->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
459 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 465 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
460 return err; 466 return err;
461 467
462 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: 468 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
463 err = v4l2_ctrl_query_fill_std(qctrl); 469 err = v4l2_ctrl_query_fill_std(qctrl);
464 if (err == 0 && params->video_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) 470 if (err == 0 &&
471 params->video_bitrate_mode ==
472 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
465 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 473 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
466 return err; 474 return err;
467 475
@@ -476,80 +484,90 @@ int cx2341x_ctrl_query(struct cx2341x_mpeg_params *params, struct v4l2_queryctrl
476 /* CX23415/6 specific */ 484 /* CX23415/6 specific */
477 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE: 485 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
478 return cx2341x_ctrl_query_fill(qctrl, 486 return cx2341x_ctrl_query_fill(qctrl,
479 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL, 487 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL,
480 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO, 1, 488 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO, 1,
481 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL); 489 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL);
482 490
483 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER: 491 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
484 cx2341x_ctrl_query_fill(qctrl, 0, 15, 1, 0); 492 cx2341x_ctrl_query_fill(qctrl, 0, 15, 1, 0);
485 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER; 493 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
486 if (params->video_spatial_filter_mode == V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO) 494 if (params->video_spatial_filter_mode ==
487 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 495 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO)
496 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
488 return 0; 497 return 0;
489 498
490 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE: 499 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
491 cx2341x_ctrl_query_fill(qctrl, 500 cx2341x_ctrl_query_fill(qctrl,
492 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_OFF, 501 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_OFF,
493 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_2D_SYM_NON_SEPARABLE, 1, 502 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_2D_SYM_NON_SEPARABLE,
494 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_OFF); 503 1,
495 if (params->video_spatial_filter_mode == V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO) 504 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_OFF);
496 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 505 if (params->video_spatial_filter_mode ==
506 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO)
507 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
497 return 0; 508 return 0;
498 509
499 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE: 510 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
500 cx2341x_ctrl_query_fill(qctrl, 511 cx2341x_ctrl_query_fill(qctrl,
501 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_OFF, 512 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_OFF,
502 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR, 1, 513 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR,
503 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_OFF); 514 1,
504 if (params->video_spatial_filter_mode == V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO) 515 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_OFF);
505 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 516 if (params->video_spatial_filter_mode ==
517 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO)
518 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
506 return 0; 519 return 0;
507 520
508 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE: 521 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
509 return cx2341x_ctrl_query_fill(qctrl, 522 return cx2341x_ctrl_query_fill(qctrl,
510 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL, 523 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL,
511 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO, 1, 524 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO, 1,
512 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL); 525 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL);
513 526
514 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER: 527 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
515 cx2341x_ctrl_query_fill(qctrl, 0, 31, 1, 0); 528 cx2341x_ctrl_query_fill(qctrl, 0, 31, 1, 0);
516 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER; 529 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
517 if (params->video_temporal_filter_mode == V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO) 530 if (params->video_temporal_filter_mode ==
518 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 531 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO)
532 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
519 return 0; 533 return 0;
520 534
521 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE: 535 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
522 return cx2341x_ctrl_query_fill(qctrl, 536 return cx2341x_ctrl_query_fill(qctrl,
523 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF, 537 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF,
524 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_DIAG, 1, 538 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_DIAG, 1,
525 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF); 539 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF);
526 540
527 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP: 541 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
528 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 255); 542 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 255);
529 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER; 543 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
530 if (params->video_median_filter_type == V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF) 544 if (params->video_median_filter_type ==
531 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 545 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
546 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
532 return 0; 547 return 0;
533 548
534 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM: 549 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
535 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 0); 550 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 0);
536 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER; 551 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
537 if (params->video_median_filter_type == V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF) 552 if (params->video_median_filter_type ==
538 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 553 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
554 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
539 return 0; 555 return 0;
540 556
541 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP: 557 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
542 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 255); 558 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 255);
543 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER; 559 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
544 if (params->video_median_filter_type == V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF) 560 if (params->video_median_filter_type ==
545 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 561 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
562 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
546 return 0; 563 return 0;
547 564
548 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM: 565 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
549 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 0); 566 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 0);
550 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER; 567 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
551 if (params->video_median_filter_type == V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF) 568 if (params->video_median_filter_type ==
552 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 569 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
570 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
553 return 0; 571 return 0;
554 572
555 case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS: 573 case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
@@ -560,6 +578,7 @@ int cx2341x_ctrl_query(struct cx2341x_mpeg_params *params, struct v4l2_queryctrl
560 578
561 } 579 }
562} 580}
581EXPORT_SYMBOL(cx2341x_ctrl_query);
563 582
564const char **cx2341x_ctrl_get_menu(u32 id) 583const char **cx2341x_ctrl_get_menu(u32 id)
565{ 584{
@@ -629,6 +648,7 @@ const char **cx2341x_ctrl_get_menu(u32 id)
629 return v4l2_ctrl_get_menu(id); 648 return v4l2_ctrl_get_menu(id);
630 } 649 }
631} 650}
651EXPORT_SYMBOL(cx2341x_ctrl_get_menu);
632 652
633static void cx2341x_calc_audio_properties(struct cx2341x_mpeg_params *params) 653static void cx2341x_calc_audio_properties(struct cx2341x_mpeg_params *params)
634{ 654{
@@ -637,9 +657,8 @@ static void cx2341x_calc_audio_properties(struct cx2341x_mpeg_params *params)
637 ((1 + params->audio_l2_bitrate) << 4) | 657 ((1 + params->audio_l2_bitrate) << 4) |
638 (params->audio_mode << 8) | 658 (params->audio_mode << 8) |
639 (params->audio_mode_extension << 10) | 659 (params->audio_mode_extension << 10) |
640 (((params->audio_emphasis == V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17) ? 660 (((params->audio_emphasis == V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17)
641 3 : 661 ? 3 : params->audio_emphasis) << 12) |
642 params->audio_emphasis) << 12) |
643 (params->audio_crc << 14); 662 (params->audio_crc << 14);
644} 663}
645 664
@@ -679,19 +698,19 @@ int cx2341x_ext_ctrls(struct cx2341x_mpeg_params *params, int busy,
679 if (err) 698 if (err)
680 break; 699 break;
681 } 700 }
682 if (err == 0 && params->video_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR && 701 if (err == 0 &&
683 params->video_bitrate_peak < params->video_bitrate) { 702 params->video_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
703 params->video_bitrate_peak < params->video_bitrate) {
684 err = -ERANGE; 704 err = -ERANGE;
685 ctrls->error_idx = ctrls->count; 705 ctrls->error_idx = ctrls->count;
686 } 706 }
687 if (err) { 707 if (err)
688 ctrls->error_idx = i; 708 ctrls->error_idx = i;
689 } 709 else
690 else {
691 cx2341x_calc_audio_properties(params); 710 cx2341x_calc_audio_properties(params);
692 }
693 return err; 711 return err;
694} 712}
713EXPORT_SYMBOL(cx2341x_ext_ctrls);
695 714
696void cx2341x_fill_defaults(struct cx2341x_mpeg_params *p) 715void cx2341x_fill_defaults(struct cx2341x_mpeg_params *p)
697{ 716{
@@ -732,13 +751,18 @@ void cx2341x_fill_defaults(struct cx2341x_mpeg_params *p)
732 .video_mute_yuv = 0x008080, /* YCbCr value for black */ 751 .video_mute_yuv = 0x008080, /* YCbCr value for black */
733 752
734 /* encoding filters */ 753 /* encoding filters */
735 .video_spatial_filter_mode = V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL, 754 .video_spatial_filter_mode =
755 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL,
736 .video_spatial_filter = 0, 756 .video_spatial_filter = 0,
737 .video_luma_spatial_filter_type = V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_1D_HOR, 757 .video_luma_spatial_filter_type =
738 .video_chroma_spatial_filter_type = V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR, 758 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_1D_HOR,
739 .video_temporal_filter_mode = V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL, 759 .video_chroma_spatial_filter_type =
760 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR,
761 .video_temporal_filter_mode =
762 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL,
740 .video_temporal_filter = 8, 763 .video_temporal_filter = 8,
741 .video_median_filter_type = V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF, 764 .video_median_filter_type =
765 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF,
742 .video_luma_median_filter_top = 255, 766 .video_luma_median_filter_top = 255,
743 .video_luma_median_filter_bottom = 0, 767 .video_luma_median_filter_bottom = 0,
744 .video_chroma_median_filter_top = 255, 768 .video_chroma_median_filter_top = 255,
@@ -748,8 +772,10 @@ void cx2341x_fill_defaults(struct cx2341x_mpeg_params *p)
748 *p = default_params; 772 *p = default_params;
749 cx2341x_calc_audio_properties(p); 773 cx2341x_calc_audio_properties(p);
750} 774}
775EXPORT_SYMBOL(cx2341x_fill_defaults);
751 776
752static int cx2341x_api(void *priv, cx2341x_mbox_func func, int cmd, int args, ...) 777static int cx2341x_api(void *priv, cx2341x_mbox_func func,
778 u32 cmd, int args, ...)
753{ 779{
754 u32 data[CX2341X_MBOX_MAX_DATA]; 780 u32 data[CX2341X_MBOX_MAX_DATA];
755 va_list vargs; 781 va_list vargs;
@@ -757,15 +783,17 @@ static int cx2341x_api(void *priv, cx2341x_mbox_func func, int cmd, int args, ..
757 783
758 va_start(vargs, args); 784 va_start(vargs, args);
759 785
760 for (i = 0; i < args; i++) { 786 for (i = 0; i < args; i++)
761 data[i] = va_arg(vargs, int); 787 data[i] = va_arg(vargs, int);
762 }
763 va_end(vargs); 788 va_end(vargs);
764 return func(priv, cmd, args, 0, data); 789 return func(priv, cmd, args, 0, data);
765} 790}
766 791
792#define NEQ(field) (old->field != new->field)
793
767int cx2341x_update(void *priv, cx2341x_mbox_func func, 794int cx2341x_update(void *priv, cx2341x_mbox_func func,
768 const struct cx2341x_mpeg_params *old, const struct cx2341x_mpeg_params *new) 795 const struct cx2341x_mpeg_params *old,
796 const struct cx2341x_mpeg_params *new)
769{ 797{
770 static int mpeg_stream_type[] = { 798 static int mpeg_stream_type[] = {
771 0, /* MPEG-2 PS */ 799 0, /* MPEG-2 PS */
@@ -777,17 +805,18 @@ int cx2341x_update(void *priv, cx2341x_mbox_func func,
777 }; 805 };
778 806
779 int err = 0; 807 int err = 0;
808 int force = (old == NULL);
780 u16 temporal = new->video_temporal_filter; 809 u16 temporal = new->video_temporal_filter;
781 810
782 cx2341x_api(priv, func, CX2341X_ENC_SET_OUTPUT_PORT, 2, new->port, 0); 811 cx2341x_api(priv, func, CX2341X_ENC_SET_OUTPUT_PORT, 2, new->port, 0);
783 812
784 if (old == NULL || old->is_50hz != new->is_50hz) { 813 if (force || NEQ(is_50hz)) {
785 err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_RATE, 1, new->is_50hz); 814 err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_RATE, 1,
815 new->is_50hz);
786 if (err) return err; 816 if (err) return err;
787 } 817 }
788 818
789 if (old == NULL || old->width != new->width || old->height != new->height || 819 if (force || NEQ(width) || NEQ(height) || NEQ(video_encoding)) {
790 old->video_encoding != new->video_encoding) {
791 u16 w = new->width; 820 u16 w = new->width;
792 u16 h = new->height; 821 u16 h = new->height;
793 822
@@ -795,69 +824,74 @@ int cx2341x_update(void *priv, cx2341x_mbox_func func,
795 w /= 2; 824 w /= 2;
796 h /= 2; 825 h /= 2;
797 } 826 }
798 err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_SIZE, 2, h, w); 827 err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_SIZE, 2,
828 h, w);
799 if (err) return err; 829 if (err) return err;
800 } 830 }
801 831
802 if (new->width != 720 || new->height != (new->is_50hz ? 576 : 480)) { 832 if (new->width != 720 || new->height != (new->is_50hz ? 576 : 480)) {
803 /* Adjust temporal filter if necessary. The problem with the temporal 833 /* Adjust temporal filter if necessary. The problem with the
804 filter is that it works well with full resolution capturing, but 834 temporal filter is that it works well with full resolution
805 not when the capture window is scaled (the filter introduces 835 capturing, but not when the capture window is scaled (the
806 a ghosting effect). So if the capture window is scaled, then 836 filter introduces a ghosting effect). So if the capture
807 force the filter to 0. 837 window is scaled, then force the filter to 0.
808 838
809 For full resolution the filter really improves the video 839 For full resolution the filter really improves the video
810 quality, especially if the original video quality is suboptimal. */ 840 quality, especially if the original video quality is
841 suboptimal. */
811 temporal = 0; 842 temporal = 0;
812 } 843 }
813 844
814 if (old == NULL || old->stream_type != new->stream_type) { 845 if (force || NEQ(stream_type)) {
815 err = cx2341x_api(priv, func, CX2341X_ENC_SET_STREAM_TYPE, 1, mpeg_stream_type[new->stream_type]); 846 err = cx2341x_api(priv, func, CX2341X_ENC_SET_STREAM_TYPE, 1,
847 mpeg_stream_type[new->stream_type]);
816 if (err) return err; 848 if (err) return err;
817 } 849 }
818 if (old == NULL || old->video_aspect != new->video_aspect) { 850 if (force || NEQ(video_aspect)) {
819 err = cx2341x_api(priv, func, CX2341X_ENC_SET_ASPECT_RATIO, 1, 1 + new->video_aspect); 851 err = cx2341x_api(priv, func, CX2341X_ENC_SET_ASPECT_RATIO, 1,
852 1 + new->video_aspect);
820 if (err) return err; 853 if (err) return err;
821 } 854 }
822 if (old == NULL || old->video_b_frames != new->video_b_frames || 855 if (force || NEQ(video_b_frames) || NEQ(video_gop_size)) {
823 old->video_gop_size != new->video_gop_size) {
824 err = cx2341x_api(priv, func, CX2341X_ENC_SET_GOP_PROPERTIES, 2, 856 err = cx2341x_api(priv, func, CX2341X_ENC_SET_GOP_PROPERTIES, 2,
825 new->video_gop_size, new->video_b_frames + 1); 857 new->video_gop_size, new->video_b_frames + 1);
826 if (err) return err; 858 if (err) return err;
827 } 859 }
828 if (old == NULL || old->video_gop_closure != new->video_gop_closure) { 860 if (force || NEQ(video_gop_closure)) {
829 err = cx2341x_api(priv, func, CX2341X_ENC_SET_GOP_CLOSURE, 1, new->video_gop_closure); 861 err = cx2341x_api(priv, func, CX2341X_ENC_SET_GOP_CLOSURE, 1,
862 new->video_gop_closure);
830 if (err) return err; 863 if (err) return err;
831 } 864 }
832 if (old == NULL || old->audio_properties != new->audio_properties) { 865 if (force || NEQ(audio_properties)) {
833 err = cx2341x_api(priv, func, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, new->audio_properties); 866 err = cx2341x_api(priv, func, CX2341X_ENC_SET_AUDIO_PROPERTIES,
867 1, new->audio_properties);
834 if (err) return err; 868 if (err) return err;
835 } 869 }
836 if (old == NULL || old->audio_mute != new->audio_mute) { 870 if (force || NEQ(audio_mute)) {
837 err = cx2341x_api(priv, func, CX2341X_ENC_MUTE_AUDIO, 1, new->audio_mute); 871 err = cx2341x_api(priv, func, CX2341X_ENC_MUTE_AUDIO, 1,
872 new->audio_mute);
838 if (err) return err; 873 if (err) return err;
839 } 874 }
840 if (old == NULL || old->video_bitrate_mode != new->video_bitrate_mode || 875 if (force || NEQ(video_bitrate_mode) || NEQ(video_bitrate) ||
841 old->video_bitrate != new->video_bitrate || 876 NEQ(video_bitrate_peak)) {
842 old->video_bitrate_peak != new->video_bitrate_peak) {
843 err = cx2341x_api(priv, func, CX2341X_ENC_SET_BIT_RATE, 5, 877 err = cx2341x_api(priv, func, CX2341X_ENC_SET_BIT_RATE, 5,
844 new->video_bitrate_mode, new->video_bitrate, 878 new->video_bitrate_mode, new->video_bitrate,
845 new->video_bitrate_peak / 400, 0, 0); 879 new->video_bitrate_peak / 400, 0, 0);
846 if (err) return err; 880 if (err) return err;
847 } 881 }
848 if (old == NULL || old->video_spatial_filter_mode != new->video_spatial_filter_mode || 882 if (force || NEQ(video_spatial_filter_mode) ||
849 old->video_temporal_filter_mode != new->video_temporal_filter_mode || 883 NEQ(video_temporal_filter_mode) ||
850 old->video_median_filter_type != new->video_median_filter_type) { 884 NEQ(video_median_filter_type)) {
851 err = cx2341x_api(priv, func, CX2341X_ENC_SET_DNR_FILTER_MODE, 2, 885 err = cx2341x_api(priv, func, CX2341X_ENC_SET_DNR_FILTER_MODE,
852 new->video_spatial_filter_mode | (new->video_temporal_filter_mode << 1), 886 2, new->video_spatial_filter_mode |
887 (new->video_temporal_filter_mode << 1),
853 new->video_median_filter_type); 888 new->video_median_filter_type);
854 if (err) return err; 889 if (err) return err;
855 } 890 }
856 if (old == NULL || 891 if (force || NEQ(video_luma_median_filter_bottom) ||
857 old->video_luma_median_filter_bottom != new->video_luma_median_filter_bottom || 892 NEQ(video_luma_median_filter_top) ||
858 old->video_luma_median_filter_top != new->video_luma_median_filter_top || 893 NEQ(video_chroma_median_filter_bottom) ||
859 old->video_chroma_median_filter_bottom != new->video_chroma_median_filter_bottom || 894 NEQ(video_chroma_median_filter_top)) {
860 old->video_chroma_median_filter_top != new->video_chroma_median_filter_top) {
861 err = cx2341x_api(priv, func, CX2341X_ENC_SET_CORING_LEVELS, 4, 895 err = cx2341x_api(priv, func, CX2341X_ENC_SET_CORING_LEVELS, 4,
862 new->video_luma_median_filter_bottom, 896 new->video_luma_median_filter_bottom,
863 new->video_luma_median_filter_top, 897 new->video_luma_median_filter_top,
@@ -865,36 +899,39 @@ int cx2341x_update(void *priv, cx2341x_mbox_func func,
865 new->video_chroma_median_filter_top); 899 new->video_chroma_median_filter_top);
866 if (err) return err; 900 if (err) return err;
867 } 901 }
868 if (old == NULL || 902 if (force || NEQ(video_luma_spatial_filter_type) ||
869 old->video_luma_spatial_filter_type != new->video_luma_spatial_filter_type || 903 NEQ(video_chroma_spatial_filter_type)) {
870 old->video_chroma_spatial_filter_type != new->video_chroma_spatial_filter_type) { 904 err = cx2341x_api(priv, func,
871 err = cx2341x_api(priv, func, CX2341X_ENC_SET_SPATIAL_FILTER_TYPE, 2, 905 CX2341X_ENC_SET_SPATIAL_FILTER_TYPE,
872 new->video_luma_spatial_filter_type, new->video_chroma_spatial_filter_type); 906 2, new->video_luma_spatial_filter_type,
907 new->video_chroma_spatial_filter_type);
873 if (err) return err; 908 if (err) return err;
874 } 909 }
875 if (old == NULL || 910 if (force || NEQ(video_spatial_filter) ||
876 old->video_spatial_filter != new->video_spatial_filter || 911 old->video_temporal_filter != temporal) {
877 old->video_temporal_filter != temporal) { 912 err = cx2341x_api(priv, func, CX2341X_ENC_SET_DNR_FILTER_PROPS,
878 err = cx2341x_api(priv, func, CX2341X_ENC_SET_DNR_FILTER_PROPS, 2, 913 2, new->video_spatial_filter, temporal);
879 new->video_spatial_filter, temporal);
880 if (err) return err; 914 if (err) return err;
881 } 915 }
882 if (old == NULL || old->video_temporal_decimation != new->video_temporal_decimation) { 916 if (force || NEQ(video_temporal_decimation)) {
883 err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_DROP_RATE, 1, 917 err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_DROP_RATE,
884 new->video_temporal_decimation); 918 1, new->video_temporal_decimation);
885 if (err) return err; 919 if (err) return err;
886 } 920 }
887 if (old == NULL || old->video_mute != new->video_mute || 921 if (force || NEQ(video_mute) ||
888 (new->video_mute && old->video_mute_yuv != new->video_mute_yuv)) { 922 (new->video_mute && NEQ(video_mute_yuv))) {
889 err = cx2341x_api(priv, func, CX2341X_ENC_MUTE_VIDEO, 1, new->video_mute | (new->video_mute_yuv << 8)); 923 err = cx2341x_api(priv, func, CX2341X_ENC_MUTE_VIDEO, 1,
924 new->video_mute | (new->video_mute_yuv << 8));
890 if (err) return err; 925 if (err) return err;
891 } 926 }
892 if (old == NULL || old->stream_insert_nav_packets != new->stream_insert_nav_packets) { 927 if (force || NEQ(stream_insert_nav_packets)) {
893 err = cx2341x_api(priv, func, CX2341X_ENC_MISC, 2, 7, new->stream_insert_nav_packets); 928 err = cx2341x_api(priv, func, CX2341X_ENC_MISC, 2,
929 7, new->stream_insert_nav_packets);
894 if (err) return err; 930 if (err) return err;
895 } 931 }
896 return 0; 932 return 0;
897} 933}
934EXPORT_SYMBOL(cx2341x_update);
898 935
899static const char *cx2341x_menu_item(struct cx2341x_mpeg_params *p, u32 id) 936static const char *cx2341x_menu_item(struct cx2341x_mpeg_params *p, u32 id)
900{ 937{
@@ -943,18 +980,17 @@ void cx2341x_log_status(struct cx2341x_mpeg_params *p, const char *prefix)
943 cx2341x_menu_item(p, V4L2_CID_MPEG_VIDEO_ASPECT), 980 cx2341x_menu_item(p, V4L2_CID_MPEG_VIDEO_ASPECT),
944 cx2341x_menu_item(p, V4L2_CID_MPEG_VIDEO_BITRATE_MODE), 981 cx2341x_menu_item(p, V4L2_CID_MPEG_VIDEO_BITRATE_MODE),
945 p->video_bitrate); 982 p->video_bitrate);
946 if (p->video_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) { 983 if (p->video_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
947 printk(", Peak %d", p->video_bitrate_peak); 984 printk(", Peak %d", p->video_bitrate_peak);
948 }
949 printk("\n"); 985 printk("\n");
950 printk(KERN_INFO "%s: Video: GOP Size %d, %d B-Frames, %sGOP Closure\n", 986 printk(KERN_INFO
987 "%s: Video: GOP Size %d, %d B-Frames, %sGOP Closure\n",
951 prefix, 988 prefix,
952 p->video_gop_size, p->video_b_frames, 989 p->video_gop_size, p->video_b_frames,
953 p->video_gop_closure ? "" : "No "); 990 p->video_gop_closure ? "" : "No ");
954 if (p->video_temporal_decimation) { 991 if (p->video_temporal_decimation)
955 printk(KERN_INFO "%s: Video: Temporal Decimation %d\n", 992 printk(KERN_INFO "%s: Video: Temporal Decimation %d\n",
956 prefix, p->video_temporal_decimation); 993 prefix, p->video_temporal_decimation);
957 }
958 994
959 /* Audio */ 995 /* Audio */
960 printk(KERN_INFO "%s: Audio: %s, %s, %s, %s%s", 996 printk(KERN_INFO "%s: Audio: %s, %s, %s, %s%s",
@@ -964,10 +1000,9 @@ void cx2341x_log_status(struct cx2341x_mpeg_params *p, const char *prefix)
964 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_L2_BITRATE), 1000 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_L2_BITRATE),
965 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_MODE), 1001 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_MODE),
966 p->audio_mute ? " (muted)" : ""); 1002 p->audio_mute ? " (muted)" : "");
967 if (p->audio_mode == V4L2_MPEG_AUDIO_MODE_JOINT_STEREO) { 1003 if (p->audio_mode == V4L2_MPEG_AUDIO_MODE_JOINT_STEREO)
968 printk(", %s", 1004 printk(", %s", cx2341x_menu_item(p,
969 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_MODE_EXTENSION)); 1005 V4L2_CID_MPEG_AUDIO_MODE_EXTENSION));
970 }
971 printk(", %s, %s\n", 1006 printk(", %s, %s\n",
972 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_EMPHASIS), 1007 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_EMPHASIS),
973 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_CRC)); 1008 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_CRC));
@@ -975,33 +1010,33 @@ void cx2341x_log_status(struct cx2341x_mpeg_params *p, const char *prefix)
975 /* Encoding filters */ 1010 /* Encoding filters */
976 printk(KERN_INFO "%s: Spatial Filter: %s, Luma %s, Chroma %s, %d\n", 1011 printk(KERN_INFO "%s: Spatial Filter: %s, Luma %s, Chroma %s, %d\n",
977 prefix, 1012 prefix,
978 cx2341x_menu_item(p, V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE), 1013 cx2341x_menu_item(p,
979 cx2341x_menu_item(p, V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE), 1014 V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE),
980 cx2341x_menu_item(p, V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE), 1015 cx2341x_menu_item(p,
1016 V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE),
1017 cx2341x_menu_item(p,
1018 V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE),
981 p->video_spatial_filter); 1019 p->video_spatial_filter);
982 if (p->width != 720 || p->height != (p->is_50hz ? 576 : 480)) { 1020
1021 if (p->width != 720 || p->height != (p->is_50hz ? 576 : 480))
983 temporal = 0; 1022 temporal = 0;
984 } 1023
985 printk(KERN_INFO "%s: Temporal Filter: %s, %d\n", 1024 printk(KERN_INFO "%s: Temporal Filter: %s, %d\n",
986 prefix, 1025 prefix,
987 cx2341x_menu_item(p, V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE), 1026 cx2341x_menu_item(p,
1027 V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE),
988 temporal); 1028 temporal);
989 printk(KERN_INFO "%s: Median Filter: %s, Luma [%d, %d], Chroma [%d, %d]\n", 1029 printk(KERN_INFO
1030 "%s: Median Filter: %s, Luma [%d, %d], Chroma [%d, %d]\n",
990 prefix, 1031 prefix,
991 cx2341x_menu_item(p, V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE), 1032 cx2341x_menu_item(p,
1033 V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE),
992 p->video_luma_median_filter_bottom, 1034 p->video_luma_median_filter_bottom,
993 p->video_luma_median_filter_top, 1035 p->video_luma_median_filter_top,
994 p->video_chroma_median_filter_bottom, 1036 p->video_chroma_median_filter_bottom,
995 p->video_chroma_median_filter_top); 1037 p->video_chroma_median_filter_top);
996} 1038}
997
998EXPORT_SYMBOL(cx2341x_fill_defaults);
999EXPORT_SYMBOL(cx2341x_ctrl_query);
1000EXPORT_SYMBOL(cx2341x_ctrl_get_menu);
1001EXPORT_SYMBOL(cx2341x_ext_ctrls);
1002EXPORT_SYMBOL(cx2341x_update);
1003EXPORT_SYMBOL(cx2341x_log_status); 1039EXPORT_SYMBOL(cx2341x_log_status);
1004EXPORT_SYMBOL(cx2341x_mpeg_ctrls);
1005 1040
1006/* 1041/*
1007 * Local variables: 1042 * Local variables:
diff --git a/drivers/media/video/cx23885/Kconfig b/drivers/media/video/cx23885/Kconfig
index 081ee6e1536f..1fd326fe4113 100644
--- a/drivers/media/video/cx23885/Kconfig
+++ b/drivers/media/video/cx23885/Kconfig
@@ -12,6 +12,10 @@ config VIDEO_CX23885
12 select DVB_S5H1409 if !DVB_FE_CUSTOMISE 12 select DVB_S5H1409 if !DVB_FE_CUSTOMISE
13 select DVB_LGDT330X if !DVB_FE_CUSTOMISE 13 select DVB_LGDT330X if !DVB_FE_CUSTOMISE
14 select DVB_PLL if !DVB_FE_CUSTOMISE 14 select DVB_PLL if !DVB_FE_CUSTOMISE
15 select TUNER_XC2028 if !DVB_FE_CUSTOMIZE
16 select TUNER_TDA8290 if !DVB_FE_CUSTOMIZE
17 select DVB_TDA18271 if !DVB_FE_CUSTOMIZE
18 select DVB_TUNER_XC5000 if !DVB_FE_CUSTOMIZE
15 ---help--- 19 ---help---
16 This is a video4linux driver for Conexant 23885 based 20 This is a video4linux driver for Conexant 23885 based
17 TV cards. 21 TV cards.
diff --git a/drivers/media/video/cx23885/Makefile b/drivers/media/video/cx23885/Makefile
index 665067022d2a..32c90be50602 100644
--- a/drivers/media/video/cx23885/Makefile
+++ b/drivers/media/video/cx23885/Makefile
@@ -1,4 +1,4 @@
1cx23885-objs := cx23885-cards.o cx23885-core.o cx23885-i2c.o cx23885-dvb.o 1cx23885-objs := cx23885-cards.o cx23885-video.o cx23885-vbi.o cx23885-core.o cx23885-i2c.o cx23885-dvb.o
2 2
3obj-$(CONFIG_VIDEO_CX23885) += cx23885.o 3obj-$(CONFIG_VIDEO_CX23885) += cx23885.o
4 4
diff --git a/drivers/media/video/cx23885/cx23885-cards.c b/drivers/media/video/cx23885/cx23885-cards.c
index b9012acabb2f..2d414dad5c31 100644
--- a/drivers/media/video/cx23885/cx23885-cards.c
+++ b/drivers/media/video/cx23885/cx23885-cards.c
@@ -23,6 +23,7 @@
23#include <linux/module.h> 23#include <linux/module.h>
24#include <linux/pci.h> 24#include <linux/pci.h>
25#include <linux/delay.h> 25#include <linux/delay.h>
26#include <media/cx25840.h>
26 27
27#include "cx23885.h" 28#include "cx23885.h"
28 29
@@ -32,6 +33,8 @@
32struct cx23885_board cx23885_boards[] = { 33struct cx23885_board cx23885_boards[] = {
33 [CX23885_BOARD_UNKNOWN] = { 34 [CX23885_BOARD_UNKNOWN] = {
34 .name = "UNKNOWN/GENERIC", 35 .name = "UNKNOWN/GENERIC",
36 /* Ensure safe default for unknown boards */
37 .clk_freq = 0,
35 .input = {{ 38 .input = {{
36 .type = CX23885_VMUX_COMPOSITE1, 39 .type = CX23885_VMUX_COMPOSITE1,
37 .vmux = 0, 40 .vmux = 0,
@@ -69,23 +72,29 @@ struct cx23885_board cx23885_boards[] = {
69 }, 72 },
70 [CX23885_BOARD_HAUPPAUGE_HVR1800] = { 73 [CX23885_BOARD_HAUPPAUGE_HVR1800] = {
71 .name = "Hauppauge WinTV-HVR1800", 74 .name = "Hauppauge WinTV-HVR1800",
75 .porta = CX23885_ANALOG_VIDEO,
72 .portc = CX23885_MPEG_DVB, 76 .portc = CX23885_MPEG_DVB,
77 .tuner_type = TUNER_PHILIPS_TDA8290,
78 .tuner_addr = 0x42, /* 0x84 >> 1 */
73 .input = {{ 79 .input = {{
74 .type = CX23885_VMUX_TELEVISION, 80 .type = CX23885_VMUX_TELEVISION,
75 .vmux = 0, 81 .vmux = CX25840_VIN7_CH3 |
76 .gpio0 = 0xff00, 82 CX25840_VIN5_CH2 |
77 },{ 83 CX25840_VIN2_CH1,
78 .type = CX23885_VMUX_DEBUG, 84 .gpio0 = 0,
79 .vmux = 0,
80 .gpio0 = 0xff01,
81 },{ 85 },{
82 .type = CX23885_VMUX_COMPOSITE1, 86 .type = CX23885_VMUX_COMPOSITE1,
83 .vmux = 1, 87 .vmux = CX25840_VIN7_CH3 |
84 .gpio0 = 0xff02, 88 CX25840_VIN4_CH2 |
89 CX25840_VIN6_CH1,
90 .gpio0 = 0,
85 },{ 91 },{
86 .type = CX23885_VMUX_SVIDEO, 92 .type = CX23885_VMUX_SVIDEO,
87 .vmux = 2, 93 .vmux = CX25840_VIN7_CH3 |
88 .gpio0 = 0xff02, 94 CX25840_VIN4_CH2 |
95 CX25840_VIN8_CH1 |
96 CX25840_SVIDEO_ON,
97 .gpio0 = 0,
89 }}, 98 }},
90 }, 99 },
91 [CX23885_BOARD_HAUPPAUGE_HVR1250] = { 100 [CX23885_BOARD_HAUPPAUGE_HVR1250] = {
@@ -113,6 +122,14 @@ struct cx23885_board cx23885_boards[] = {
113 .name = "DViCO FusionHDTV5 Express", 122 .name = "DViCO FusionHDTV5 Express",
114 .portb = CX23885_MPEG_DVB, 123 .portb = CX23885_MPEG_DVB,
115 }, 124 },
125 [CX23885_BOARD_HAUPPAUGE_HVR1500Q] = {
126 .name = "Hauppauge WinTV-HVR1500Q",
127 .portc = CX23885_MPEG_DVB,
128 },
129 [CX23885_BOARD_HAUPPAUGE_HVR1500] = {
130 .name = "Hauppauge WinTV-HVR1500",
131 .portc = CX23885_MPEG_DVB,
132 },
116}; 133};
117const unsigned int cx23885_bcount = ARRAY_SIZE(cx23885_boards); 134const unsigned int cx23885_bcount = ARRAY_SIZE(cx23885_boards);
118 135
@@ -138,12 +155,32 @@ struct cx23885_subid cx23885_subids[] = {
138 .card = CX23885_BOARD_HAUPPAUGE_HVR1800, 155 .card = CX23885_BOARD_HAUPPAUGE_HVR1800,
139 },{ 156 },{
140 .subvendor = 0x0070, 157 .subvendor = 0x0070,
158 .subdevice = 0x7809,
159 .card = CX23885_BOARD_HAUPPAUGE_HVR1800,
160 },{
161 .subvendor = 0x0070,
141 .subdevice = 0x7911, 162 .subdevice = 0x7911,
142 .card = CX23885_BOARD_HAUPPAUGE_HVR1250, 163 .card = CX23885_BOARD_HAUPPAUGE_HVR1250,
143 },{ 164 },{
144 .subvendor = 0x18ac, 165 .subvendor = 0x18ac,
145 .subdevice = 0xd500, 166 .subdevice = 0xd500,
146 .card = CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP, 167 .card = CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP,
168 },{
169 .subvendor = 0x0070,
170 .subdevice = 0x7790,
171 .card = CX23885_BOARD_HAUPPAUGE_HVR1500Q,
172 },{
173 .subvendor = 0x0070,
174 .subdevice = 0x7797,
175 .card = CX23885_BOARD_HAUPPAUGE_HVR1500Q,
176 },{
177 .subvendor = 0x0070,
178 .subdevice = 0x7710,
179 .card = CX23885_BOARD_HAUPPAUGE_HVR1500,
180 },{
181 .subvendor = 0x0070,
182 .subdevice = 0x7717,
183 .card = CX23885_BOARD_HAUPPAUGE_HVR1500,
147 }, 184 },
148}; 185};
149const unsigned int cx23885_idcount = ARRAY_SIZE(cx23885_subids); 186const unsigned int cx23885_idcount = ARRAY_SIZE(cx23885_subids);
@@ -184,9 +221,19 @@ static void hauppauge_eeprom(struct cx23885_dev *dev, u8 *eeprom_data)
184 switch (tv.model) 221 switch (tv.model)
185 { 222 {
186 case 76601: /* WinTV-HVR1800lp (PCIe, Retail, No IR, Dual channel ATSC and MPEG2 HW Encoder */ 223 case 76601: /* WinTV-HVR1800lp (PCIe, Retail, No IR, Dual channel ATSC and MPEG2 HW Encoder */
187 case 77001: /* WinTV-HVR1500 (Express Card, Retail, No IR, ATSC and Basic analog */ 224 case 77001: /* WinTV-HVR1500 (Express Card, OEM, No IR, ATSC and Basic analog */
188 case 78501: /* WinTV-HVR1800 (PCIe, Retail, IR, Dual channel ATSC and MPEG2 HW Encoder */ 225 case 77011: /* WinTV-HVR1500 (Express Card, Retail, No IR, ATSC and Basic analog */
189 case 78521: /* WinTV-HVR1800 (PCIe, Retail, IR, Dual channel ATSC and MPEG2 HW Encoder */ 226 case 77041: /* WinTV-HVR1500Q (Express Card, OEM, No IR, ATSC/QAM and Basic analog */
227 case 77051: /* WinTV-HVR1500Q (Express Card, Retail, No IR, ATSC/QAM and Basic analog */
228 case 78011: /* WinTV-HVR1800 (PCIe, Retail, 3.5mm in, IR, No FM, Dual channel ATSC and MPEG2 HW Encoder */
229 case 78501: /* WinTV-HVR1800 (PCIe, OEM, RCA in, No IR, FM, Dual channel ATSC and MPEG2 HW Encoder */
230 case 78521: /* WinTV-HVR1800 (PCIe, OEM, RCA in, No IR, FM, Dual channel ATSC and MPEG2 HW Encoder */
231 case 78531: /* WinTV-HVR1800 (PCIe, OEM, RCA in, No IR, No FM, Dual channel ATSC and MPEG2 HW Encoder */
232 case 78631: /* WinTV-HVR1800 (PCIe, OEM, No IR, No FM, Dual channel ATSC and MPEG2 HW Encoder */
233 case 79001: /* WinTV-HVR1250 (PCIe, Retail, IR, full height, ATSC and Basic analog */
234 case 79101: /* WinTV-HVR1250 (PCIe, Retail, IR, half height, ATSC and Basic analog */
235 case 79571: /* WinTV-HVR1250 (PCIe, OEM, No IR, full height, ATSC and Basic analog */
236 case 79671: /* WinTV-HVR1250 (PCIe, OEM, No IR, half height, ATSC and Basic analog */
190 break; 237 break;
191 default: 238 default:
192 printk("%s: warning: unknown hauppauge model #%d\n", dev->name, tv.model); 239 printk("%s: warning: unknown hauppauge model #%d\n", dev->name, tv.model);
@@ -197,6 +244,34 @@ static void hauppauge_eeprom(struct cx23885_dev *dev, u8 *eeprom_data)
197 dev->name, tv.model); 244 dev->name, tv.model);
198} 245}
199 246
247/* Tuner callback function for cx23885 boards. Currently only needed
248 * for HVR1500Q, which has an xc5000 tuner.
249 */
250int cx23885_tuner_callback(void *priv, int command, int arg)
251{
252 struct cx23885_i2c *bus = priv;
253 struct cx23885_dev *dev = bus->dev;
254
255 switch(dev->board) {
256 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
257 if(command == 0) { /* Tuner Reset Command from xc5000 */
258 /* Drive the tuner into reset and out */
259 cx_clear(GP0_IO, 0x00000004);
260 mdelay(200);
261 cx_set(GP0_IO, 0x00000004);
262 return 0;
263 }
264 else {
265 printk(KERN_ERR
266 "%s(): Unknow command.\n", __FUNCTION__);
267 return -EINVAL;
268 }
269 break;
270 }
271
272 return 0; /* Should never be here */
273}
274
200void cx23885_gpio_setup(struct cx23885_dev *dev) 275void cx23885_gpio_setup(struct cx23885_dev *dev)
201{ 276{
202 switch(dev->board) { 277 switch(dev->board) {
@@ -204,6 +279,23 @@ void cx23885_gpio_setup(struct cx23885_dev *dev)
204 /* GPIO-0 cx24227 demodulator reset */ 279 /* GPIO-0 cx24227 demodulator reset */
205 cx_set(GP0_IO, 0x00010001); /* Bring the part out of reset */ 280 cx_set(GP0_IO, 0x00010001); /* Bring the part out of reset */
206 break; 281 break;
282 case CX23885_BOARD_HAUPPAUGE_HVR1500:
283 /* GPIO-0 cx24227 demodulator */
284 /* GPIO-2 xc3028 tuner */
285
286 /* Put the parts into reset */
287 cx_set(GP0_IO, 0x00050000);
288 cx_clear(GP0_IO, 0x00000005);
289 msleep(5);
290
291 /* Bring the parts out of reset */
292 cx_set(GP0_IO, 0x00050005);
293 break;
294 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
295 /* GPIO-0 cx24227 demodulator reset */
296 /* GPIO-2 xc5000 tuner reset */
297 cx_set(GP0_IO, 0x00050005); /* Bring the part out of reset */
298 break;
207 case CX23885_BOARD_HAUPPAUGE_HVR1800: 299 case CX23885_BOARD_HAUPPAUGE_HVR1800:
208 /* GPIO-0 656_CLK */ 300 /* GPIO-0 656_CLK */
209 /* GPIO-1 656_D0 */ 301 /* GPIO-1 656_D0 */
@@ -212,7 +304,14 @@ void cx23885_gpio_setup(struct cx23885_dev *dev)
212 /* GPIO-11-14 cx23417 addr0-3 */ 304 /* GPIO-11-14 cx23417 addr0-3 */
213 /* GPIO-15-18 cx23417 READY, CS, RD, WR */ 305 /* GPIO-15-18 cx23417 READY, CS, RD, WR */
214 /* GPIO-19 IR_RX */ 306 /* GPIO-19 IR_RX */
215 // FIXME: Analog requires the tuner is brought out of reset 307
308 /* Force the TDA8295A into reset and back */
309 cx_set(GP0_IO, 0x00040004);
310 mdelay(20);
311 cx_clear(GP0_IO, 0x00000004);
312 mdelay(20);
313 cx_set(GP0_IO, 0x00040004);
314 mdelay(20);
216 break; 315 break;
217 } 316 }
218} 317}
@@ -221,6 +320,8 @@ int cx23885_ir_init(struct cx23885_dev *dev)
221{ 320{
222 switch (dev->board) { 321 switch (dev->board) {
223 case CX23885_BOARD_HAUPPAUGE_HVR1250: 322 case CX23885_BOARD_HAUPPAUGE_HVR1250:
323 case CX23885_BOARD_HAUPPAUGE_HVR1500:
324 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
224 case CX23885_BOARD_HAUPPAUGE_HVR1800: 325 case CX23885_BOARD_HAUPPAUGE_HVR1800:
225 /* FIXME: Implement me */ 326 /* FIXME: Implement me */
226 break; 327 break;
@@ -244,6 +345,8 @@ void cx23885_card_setup(struct cx23885_dev *dev)
244 345
245 switch (dev->board) { 346 switch (dev->board) {
246 case CX23885_BOARD_HAUPPAUGE_HVR1250: 347 case CX23885_BOARD_HAUPPAUGE_HVR1250:
348 case CX23885_BOARD_HAUPPAUGE_HVR1500:
349 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
247 case CX23885_BOARD_HAUPPAUGE_HVR1800: 350 case CX23885_BOARD_HAUPPAUGE_HVR1800:
248 case CX23885_BOARD_HAUPPAUGE_HVR1800lp: 351 case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
249 if (dev->i2c_bus[0].i2c_rc == 0) 352 if (dev->i2c_bus[0].i2c_rc == 0)
@@ -258,6 +361,8 @@ void cx23885_card_setup(struct cx23885_dev *dev)
258 ts1->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO; 361 ts1->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
259 break; 362 break;
260 case CX23885_BOARD_HAUPPAUGE_HVR1250: 363 case CX23885_BOARD_HAUPPAUGE_HVR1250:
364 case CX23885_BOARD_HAUPPAUGE_HVR1500:
365 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
261 case CX23885_BOARD_HAUPPAUGE_HVR1800: 366 case CX23885_BOARD_HAUPPAUGE_HVR1800:
262 case CX23885_BOARD_HAUPPAUGE_HVR1800lp: 367 case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
263 default: 368 default:
@@ -270,8 +375,6 @@ void cx23885_card_setup(struct cx23885_dev *dev)
270 375
271/* ------------------------------------------------------------------ */ 376/* ------------------------------------------------------------------ */
272 377
273EXPORT_SYMBOL(cx23885_boards);
274
275/* 378/*
276 * Local variables: 379 * Local variables:
277 * c-basic-offset: 8 380 * c-basic-offset: 8
diff --git a/drivers/media/video/cx23885/cx23885-core.c b/drivers/media/video/cx23885/cx23885-core.c
index 3cdd136477e5..8e40c7bcc06d 100644
--- a/drivers/media/video/cx23885/cx23885-core.c
+++ b/drivers/media/video/cx23885/cx23885-core.c
@@ -36,7 +36,7 @@ MODULE_DESCRIPTION("Driver for cx23885 based TV cards");
36MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>"); 36MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
37MODULE_LICENSE("GPL"); 37MODULE_LICENSE("GPL");
38 38
39static unsigned int debug = 0; 39static unsigned int debug;
40module_param(debug,int,0644); 40module_param(debug,int,0644);
41MODULE_PARM_DESC(debug,"enable debug messages"); 41MODULE_PARM_DESC(debug,"enable debug messages");
42 42
@@ -44,13 +44,15 @@ static unsigned int card[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
44module_param_array(card, int, NULL, 0444); 44module_param_array(card, int, NULL, 0444);
45MODULE_PARM_DESC(card,"card type"); 45MODULE_PARM_DESC(card,"card type");
46 46
47#define dprintk(level,fmt, arg...) if (debug >= level) \ 47#define dprintk(level, fmt, arg...)\
48 printk(KERN_DEBUG "%s/0: " fmt, dev->name , ## arg) 48 do { if (debug >= level)\
49 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
50 } while (0)
49 51
50static unsigned int cx23885_devcount; 52static unsigned int cx23885_devcount;
51 53
52static DEFINE_MUTEX(devlist); 54static DEFINE_MUTEX(devlist);
53static LIST_HEAD(cx23885_devlist); 55LIST_HEAD(cx23885_devlist);
54 56
55#define NO_SYNC_LINE (-1U) 57#define NO_SYNC_LINE (-1U)
56 58
@@ -73,14 +75,14 @@ static LIST_HEAD(cx23885_devlist);
73 * 0x00010ea0 0x00010xxx Free 75 * 0x00010ea0 0x00010xxx Free
74 */ 76 */
75 77
76struct sram_channel cx23885_sram_channels[] = { 78static struct sram_channel cx23885_sram_channels[] = {
77 [SRAM_CH01] = { 79 [SRAM_CH01] = {
78 .name = "test ch1", 80 .name = "VID A",
79 .cmds_start = 0x10000, 81 .cmds_start = 0x10000,
80 .ctrl_start = 0x10500, 82 .ctrl_start = 0x105b0,
81 .cdt = 0x10900, 83 .cdt = 0x107b0,
82 .fifo_start = 0x3000, 84 .fifo_start = 0x40,
83 .fifo_size = 0x1000, 85 .fifo_size = 0x2800,
84 .ptr1_reg = DMA1_PTR1, 86 .ptr1_reg = DMA1_PTR1,
85 .ptr2_reg = DMA1_PTR2, 87 .ptr2_reg = DMA1_PTR2,
86 .cnt1_reg = DMA1_CNT1, 88 .cnt1_reg = DMA1_CNT1,
@@ -102,8 +104,8 @@ struct sram_channel cx23885_sram_channels[] = {
102 [SRAM_CH03] = { 104 [SRAM_CH03] = {
103 .name = "TS1 B", 105 .name = "TS1 B",
104 .cmds_start = 0x100A0, 106 .cmds_start = 0x100A0,
105 .ctrl_start = 0x10780, 107 .ctrl_start = 0x10630,
106 .cdt = 0x10400, 108 .cdt = 0x10870,
107 .fifo_start = 0x5000, 109 .fifo_start = 0x5000,
108 .fifo_size = 0x1000, 110 .fifo_size = 0x1000,
109 .ptr1_reg = DMA3_PTR1, 111 .ptr1_reg = DMA3_PTR1,
@@ -139,7 +141,7 @@ struct sram_channel cx23885_sram_channels[] = {
139 .name = "TS2 C", 141 .name = "TS2 C",
140 .cmds_start = 0x10140, 142 .cmds_start = 0x10140,
141 .ctrl_start = 0x10680, 143 .ctrl_start = 0x10680,
142 .cdt = 0x10480, 144 .cdt = 0x108d0,
143 .fifo_start = 0x6000, 145 .fifo_start = 0x6000,
144 .fifo_size = 0x1000, 146 .fifo_size = 0x1000,
145 .ptr1_reg = DMA5_PTR1, 147 .ptr1_reg = DMA5_PTR1,
@@ -205,14 +207,14 @@ struct sram_channel cx23885_sram_channels[] = {
205 * 0x00010ea0 0x00010xxx Free 207 * 0x00010ea0 0x00010xxx Free
206 */ 208 */
207 209
208struct sram_channel cx23887_sram_channels[] = { 210static struct sram_channel cx23887_sram_channels[] = {
209 [SRAM_CH01] = { 211 [SRAM_CH01] = {
210 .name = "test ch1", 212 .name = "VID A",
211 .cmds_start = 0x0, 213 .cmds_start = 0x10000,
212 .ctrl_start = 0x0, 214 .ctrl_start = 0x105b0,
213 .cdt = 0x0, 215 .cdt = 0x107b0,
214 .fifo_start = 0x0, 216 .fifo_start = 0x40,
215 .fifo_size = 0x0, 217 .fifo_size = 0x2800,
216 .ptr1_reg = DMA1_PTR1, 218 .ptr1_reg = DMA1_PTR1,
217 .ptr2_reg = DMA1_PTR2, 219 .ptr2_reg = DMA1_PTR2,
218 .cnt1_reg = DMA1_CNT1, 220 .cnt1_reg = DMA1_CNT1,
@@ -231,12 +233,12 @@ struct sram_channel cx23887_sram_channels[] = {
231 .cnt2_reg = DMA2_CNT2, 233 .cnt2_reg = DMA2_CNT2,
232 }, 234 },
233 [SRAM_CH03] = { 235 [SRAM_CH03] = {
234 .name = "ch3", 236 .name = "TS1 B",
235 .cmds_start = 0x0, 237 .cmds_start = 0x100A0,
236 .ctrl_start = 0x0, 238 .ctrl_start = 0x10780,
237 .cdt = 0x0, 239 .cdt = 0x10400,
238 .fifo_start = 0x0, 240 .fifo_start = 0x5000,
239 .fifo_size = 0x0, 241 .fifo_size = 0x1000,
240 .ptr1_reg = DMA3_PTR1, 242 .ptr1_reg = DMA3_PTR1,
241 .ptr2_reg = DMA3_PTR2, 243 .ptr2_reg = DMA3_PTR2,
242 .cnt1_reg = DMA3_CNT1, 244 .cnt1_reg = DMA3_CNT1,
@@ -357,7 +359,7 @@ static int cx23885_risc_decode(u32 risc)
357} 359}
358 360
359void cx23885_wakeup(struct cx23885_tsport *port, 361void cx23885_wakeup(struct cx23885_tsport *port,
360 struct cx23885_dmaqueue *q, u32 count) 362 struct cx23885_dmaqueue *q, u32 count)
361{ 363{
362 struct cx23885_dev *dev = port->dev; 364 struct cx23885_dev *dev = port->dev;
363 struct cx23885_buffer *buf; 365 struct cx23885_buffer *buf;
@@ -378,7 +380,7 @@ void cx23885_wakeup(struct cx23885_tsport *port,
378 do_gettimeofday(&buf->vb.ts); 380 do_gettimeofday(&buf->vb.ts);
379 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i, 381 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
380 count, buf->count); 382 count, buf->count);
381 buf->vb.state = STATE_DONE; 383 buf->vb.state = VIDEOBUF_DONE;
382 list_del(&buf->vb.queue); 384 list_del(&buf->vb.queue);
383 wake_up(&buf->vb.done); 385 wake_up(&buf->vb.done);
384 } 386 }
@@ -391,12 +393,10 @@ void cx23885_wakeup(struct cx23885_tsport *port,
391 printk("%s: %d buffers handled (should be 1)\n", 393 printk("%s: %d buffers handled (should be 1)\n",
392 __FUNCTION__, bc); 394 __FUNCTION__, bc);
393} 395}
394void cx23885_sram_channel_dump(struct cx23885_dev *dev,
395 struct sram_channel *ch);
396 396
397int cx23885_sram_channel_setup(struct cx23885_dev *dev, 397int cx23885_sram_channel_setup(struct cx23885_dev *dev,
398 struct sram_channel *ch, 398 struct sram_channel *ch,
399 unsigned int bpl, u32 risc) 399 unsigned int bpl, u32 risc)
400{ 400{
401 unsigned int i, lines; 401 unsigned int i, lines;
402 u32 cdt; 402 u32 cdt;
@@ -468,7 +468,7 @@ int cx23885_sram_channel_setup(struct cx23885_dev *dev,
468} 468}
469 469
470void cx23885_sram_channel_dump(struct cx23885_dev *dev, 470void cx23885_sram_channel_dump(struct cx23885_dev *dev,
471 struct sram_channel *ch) 471 struct sram_channel *ch)
472{ 472{
473 static char *name[] = { 473 static char *name[] = {
474 "init risc lo", 474 "init risc lo",
@@ -529,8 +529,8 @@ void cx23885_sram_channel_dump(struct cx23885_dev *dev,
529 dev->name, cx_read(ch->cnt2_reg)); 529 dev->name, cx_read(ch->cnt2_reg));
530} 530}
531 531
532void cx23885_risc_disasm(struct cx23885_tsport *port, 532static void cx23885_risc_disasm(struct cx23885_tsport *port,
533 struct btcx_riscmem *risc) 533 struct btcx_riscmem *risc)
534{ 534{
535 struct cx23885_dev *dev = port->dev; 535 struct cx23885_dev *dev = port->dev;
536 unsigned int i, j, n; 536 unsigned int i, j, n;
@@ -548,7 +548,7 @@ void cx23885_risc_disasm(struct cx23885_tsport *port,
548 } 548 }
549} 549}
550 550
551void cx23885_shutdown(struct cx23885_dev *dev) 551static void cx23885_shutdown(struct cx23885_dev *dev)
552{ 552{
553 /* disable RISC controller */ 553 /* disable RISC controller */
554 cx_write(DEV_CNTRL2, 0); 554 cx_write(DEV_CNTRL2, 0);
@@ -578,7 +578,7 @@ void cx23885_shutdown(struct cx23885_dev *dev)
578 578
579} 579}
580 580
581void cx23885_reset(struct cx23885_dev *dev) 581static void cx23885_reset(struct cx23885_dev *dev)
582{ 582{
583 dprintk(1, "%s()\n", __FUNCTION__); 583 dprintk(1, "%s()\n", __FUNCTION__);
584 584
@@ -594,15 +594,18 @@ void cx23885_reset(struct cx23885_dev *dev)
594 594
595 mdelay(100); 595 mdelay(100);
596 596
597 cx23885_sram_channel_setup(dev, &dev->sram_channels[ SRAM_CH01 ], 188*4, 0); 597 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
598 cx23885_sram_channel_setup(dev, &dev->sram_channels[ SRAM_CH02 ], 128, 0); 598 720*4, 0);
599 cx23885_sram_channel_setup(dev, &dev->sram_channels[ SRAM_CH03 ], 188*4, 0); 599 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02], 128, 0);
600 cx23885_sram_channel_setup(dev, &dev->sram_channels[ SRAM_CH04 ], 128, 0); 600 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH03],
601 cx23885_sram_channel_setup(dev, &dev->sram_channels[ SRAM_CH05 ], 128, 0); 601 188*4, 0);
602 cx23885_sram_channel_setup(dev, &dev->sram_channels[ SRAM_CH06 ], 188*4, 0); 602 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH04], 128, 0);
603 cx23885_sram_channel_setup(dev, &dev->sram_channels[ SRAM_CH07 ], 128, 0); 603 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH05], 128, 0);
604 cx23885_sram_channel_setup(dev, &dev->sram_channels[ SRAM_CH08 ], 128, 0); 604 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH06],
605 cx23885_sram_channel_setup(dev, &dev->sram_channels[ SRAM_CH09 ], 128, 0); 605 188*4, 0);
606 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH07], 128, 0);
607 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH08], 128, 0);
608 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH09], 128, 0);
606 609
607 cx23885_gpio_setup(dev); 610 cx23885_gpio_setup(dev);
608} 611}
@@ -637,7 +640,7 @@ static int get_resources(struct cx23885_dev *dev)
637 640
638static void cx23885_timeout(unsigned long data); 641static void cx23885_timeout(unsigned long data);
639int cx23885_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc, 642int cx23885_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
640 u32 reg, u32 mask, u32 value); 643 u32 reg, u32 mask, u32 value);
641 644
642static int cx23885_init_tsport(struct cx23885_dev *dev, struct cx23885_tsport *port, int portno) 645static int cx23885_init_tsport(struct cx23885_dev *dev, struct cx23885_tsport *port, int portno)
643{ 646{
@@ -704,6 +707,44 @@ static int cx23885_init_tsport(struct cx23885_dev *dev, struct cx23885_tsport *p
704 return 0; 707 return 0;
705} 708}
706 709
710static void cx23885_dev_checkrevision(struct cx23885_dev *dev)
711{
712 switch (cx_read(RDR_CFG2) & 0xff) {
713 case 0x00:
714 /* cx23885 */
715 dev->hwrevision = 0xa0;
716 break;
717 case 0x01:
718 /* CX23885-12Z */
719 dev->hwrevision = 0xa1;
720 break;
721 case 0x02:
722 /* CX23885-13Z */
723 dev->hwrevision = 0xb0;
724 break;
725 case 0x03:
726 /* CX23888-22Z */
727 dev->hwrevision = 0xc0;
728 break;
729 case 0x0e:
730 /* CX23887-15Z */
731 dev->hwrevision = 0xc0;
732 case 0x0f:
733 /* CX23887-14Z */
734 dev->hwrevision = 0xb1;
735 break;
736 default:
737 printk(KERN_ERR "%s() New hardware revision found 0x%x\n",
738 __FUNCTION__, dev->hwrevision);
739 }
740 if (dev->hwrevision)
741 printk(KERN_INFO "%s() Hardware revision = 0x%02x\n",
742 __FUNCTION__, dev->hwrevision);
743 else
744 printk(KERN_ERR "%s() Hardware revision unknown 0x%x\n",
745 __FUNCTION__, dev->hwrevision);
746}
747
707static int cx23885_dev_setup(struct cx23885_dev *dev) 748static int cx23885_dev_setup(struct cx23885_dev *dev)
708{ 749{
709 int i; 750 int i;
@@ -723,10 +764,14 @@ static int cx23885_dev_setup(struct cx23885_dev *dev)
723 if(dev->pci->device == 0x8880) { 764 if(dev->pci->device == 0x8880) {
724 dev->bridge = CX23885_BRIDGE_887; 765 dev->bridge = CX23885_BRIDGE_887;
725 dev->sram_channels = cx23887_sram_channels; 766 dev->sram_channels = cx23887_sram_channels;
767 /* Apply a sensible clock frequency for the PCIe bridge */
768 dev->clk_freq = 25000000;
726 } else 769 } else
727 if(dev->pci->device == 0x8852) { 770 if(dev->pci->device == 0x8852) {
728 dev->bridge = CX23885_BRIDGE_885; 771 dev->bridge = CX23885_BRIDGE_885;
729 dev->sram_channels = cx23885_sram_channels; 772 dev->sram_channels = cx23885_sram_channels;
773 /* Apply a sensible clock frequency for the PCIe bridge */
774 dev->clk_freq = 28000000;
730 } else 775 } else
731 BUG(); 776 BUG();
732 777
@@ -746,6 +791,10 @@ static int cx23885_dev_setup(struct cx23885_dev *dev)
746 cx23885_card_list(dev); 791 cx23885_card_list(dev);
747 } 792 }
748 793
794 /* If the user specific a clk freq override, apply it */
795 if (cx23885_boards[dev->board].clk_freq > 0)
796 dev->clk_freq = cx23885_boards[dev->board].clk_freq;
797
749 dev->pci_bus = dev->pci->bus->number; 798 dev->pci_bus = dev->pci->bus->number;
750 dev->pci_slot = PCI_SLOT(dev->pci->devfn); 799 dev->pci_slot = PCI_SLOT(dev->pci->devfn);
751 dev->pci_irqmask = 0x001f00; 800 dev->pci_irqmask = 0x001f00;
@@ -810,6 +859,17 @@ static int cx23885_dev_setup(struct cx23885_dev *dev)
810 859
811 cx23885_pci_quirks(dev); 860 cx23885_pci_quirks(dev);
812 861
862 /* Assume some sensible defaults */
863 dev->tuner_type = cx23885_boards[dev->board].tuner_type;
864 dev->tuner_addr = cx23885_boards[dev->board].tuner_addr;
865 dev->radio_type = cx23885_boards[dev->board].radio_type;
866 dev->radio_addr = cx23885_boards[dev->board].radio_addr;
867
868 dprintk(1, "%s() tuner_type = 0x%x tuner_addr = 0x%x\n",
869 __FUNCTION__, dev->tuner_type, dev->tuner_addr);
870 dprintk(1, "%s() radio_type = 0x%x radio_addr = 0x%x\n",
871 __FUNCTION__, dev->radio_type, dev->radio_addr);
872
813 /* init hardware */ 873 /* init hardware */
814 cx23885_reset(dev); 874 cx23885_reset(dev);
815 875
@@ -820,24 +880,33 @@ static int cx23885_dev_setup(struct cx23885_dev *dev)
820 cx23885_card_setup(dev); 880 cx23885_card_setup(dev);
821 cx23885_ir_init(dev); 881 cx23885_ir_init(dev);
822 882
823 if(cx23885_boards[dev->board].portb == CX23885_MPEG_DVB) { 883 if (cx23885_boards[dev->board].porta == CX23885_ANALOG_VIDEO) {
884 if (cx23885_video_register(dev) < 0) {
885 printk(KERN_ERR "%s() Failed to register analog "
886 "video adapters on VID_A\n", __FUNCTION__);
887 }
888 }
889
890 if (cx23885_boards[dev->board].portb == CX23885_MPEG_DVB) {
824 if (cx23885_dvb_register(&dev->ts1) < 0) { 891 if (cx23885_dvb_register(&dev->ts1) < 0) {
825 printk(KERN_ERR "%s() Failed to register dvb adapters on VID_B\n", 892 printk(KERN_ERR "%s() Failed to register dvb adapters on VID_B\n",
826 __FUNCTION__); 893 __FUNCTION__);
827 } 894 }
828 } 895 }
829 896
830 if(cx23885_boards[dev->board].portc == CX23885_MPEG_DVB) { 897 if (cx23885_boards[dev->board].portc == CX23885_MPEG_DVB) {
831 if (cx23885_dvb_register(&dev->ts2) < 0) { 898 if (cx23885_dvb_register(&dev->ts2) < 0) {
832 printk(KERN_ERR "%s() Failed to register dvb adapters on VID_C\n", 899 printk(KERN_ERR "%s() Failed to register dvb adapters on VID_C\n",
833 __FUNCTION__); 900 __FUNCTION__);
834 } 901 }
835 } 902 }
836 903
904 cx23885_dev_checkrevision(dev);
905
837 return 0; 906 return 0;
838} 907}
839 908
840void cx23885_dev_unregister(struct cx23885_dev *dev) 909static void cx23885_dev_unregister(struct cx23885_dev *dev)
841{ 910{
842 release_mem_region(pci_resource_start(dev->pci,0), 911 release_mem_region(pci_resource_start(dev->pci,0),
843 pci_resource_len(dev->pci,0)); 912 pci_resource_len(dev->pci,0));
@@ -845,6 +914,9 @@ void cx23885_dev_unregister(struct cx23885_dev *dev)
845 if (!atomic_dec_and_test(&dev->refcount)) 914 if (!atomic_dec_and_test(&dev->refcount))
846 return; 915 return;
847 916
917 if (cx23885_boards[dev->board].porta == CX23885_ANALOG_VIDEO)
918 cx23885_video_unregister(dev);
919
848 if(cx23885_boards[dev->board].portb == CX23885_MPEG_DVB) 920 if(cx23885_boards[dev->board].portb == CX23885_MPEG_DVB)
849 cx23885_dvb_unregister(&dev->ts1); 921 cx23885_dvb_unregister(&dev->ts1);
850 922
@@ -952,9 +1024,11 @@ int cx23885_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
952 return 0; 1024 return 0;
953} 1025}
954 1026
955int cx23885_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc, 1027static int cx23885_risc_databuffer(struct pci_dev *pci,
956 struct scatterlist *sglist, unsigned int bpl, 1028 struct btcx_riscmem *risc,
957 unsigned int lines) 1029 struct scatterlist *sglist,
1030 unsigned int bpl,
1031 unsigned int lines)
958{ 1032{
959 u32 instructions; 1033 u32 instructions;
960 u32 *rp; 1034 u32 *rp;
@@ -982,7 +1056,7 @@ int cx23885_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc,
982} 1056}
983 1057
984int cx23885_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc, 1058int cx23885_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
985 u32 reg, u32 mask, u32 value) 1059 u32 reg, u32 mask, u32 value)
986{ 1060{
987 u32 *rp; 1061 u32 *rp;
988 int rc; 1062 int rc;
@@ -1011,7 +1085,58 @@ void cx23885_free_buffer(struct videobuf_queue *q, struct cx23885_buffer *buf)
1011 videobuf_dma_unmap(q, dma); 1085 videobuf_dma_unmap(q, dma);
1012 videobuf_dma_free(dma); 1086 videobuf_dma_free(dma);
1013 btcx_riscmem_free((struct pci_dev *)q->dev, &buf->risc); 1087 btcx_riscmem_free((struct pci_dev *)q->dev, &buf->risc);
1014 buf->vb.state = STATE_NEEDS_INIT; 1088 buf->vb.state = VIDEOBUF_NEEDS_INIT;
1089}
1090
1091static void cx23885_tsport_reg_dump(struct cx23885_tsport *port)
1092{
1093 struct cx23885_dev *dev = port->dev;
1094
1095 dprintk(1, "%s() Register Dump\n", __FUNCTION__);
1096 dprintk(1, "%s() DEV_CNTRL2 0x%08X\n", __FUNCTION__,
1097 cx_read(DEV_CNTRL2));
1098 dprintk(1, "%s() PCI_INT_MSK 0x%08X\n", __FUNCTION__,
1099 cx_read(PCI_INT_MSK));
1100 dprintk(1, "%s() AUD_INT_INT_MSK 0x%08X\n", __FUNCTION__,
1101 cx_read(AUDIO_INT_INT_MSK));
1102 dprintk(1, "%s() AUD_INT_DMA_CTL 0x%08X\n", __FUNCTION__,
1103 cx_read(AUD_INT_DMA_CTL));
1104 dprintk(1, "%s() AUD_EXT_INT_MSK 0x%08X\n", __FUNCTION__,
1105 cx_read(AUDIO_EXT_INT_MSK));
1106 dprintk(1, "%s() AUD_EXT_DMA_CTL 0x%08X\n", __FUNCTION__,
1107 cx_read(AUD_EXT_DMA_CTL));
1108 dprintk(1, "%s() PAD_CTRL 0x%08X\n", __FUNCTION__,
1109 cx_read(PAD_CTRL));
1110 dprintk(1, "%s() ALT_PIN_OUT_SEL 0x%08X\n", __FUNCTION__,
1111 cx_read(ALT_PIN_OUT_SEL));
1112 dprintk(1, "%s() GPIO2 0x%08X\n", __FUNCTION__,
1113 cx_read(GPIO2));
1114 dprintk(1, "%s() gpcnt(0x%08X) 0x%08X\n", __FUNCTION__,
1115 port->reg_gpcnt, cx_read(port->reg_gpcnt));
1116 dprintk(1, "%s() gpcnt_ctl(0x%08X) 0x%08x\n", __FUNCTION__,
1117 port->reg_gpcnt_ctl, cx_read(port->reg_gpcnt_ctl));
1118 dprintk(1, "%s() dma_ctl(0x%08X) 0x%08x\n", __FUNCTION__,
1119 port->reg_dma_ctl, cx_read(port->reg_dma_ctl));
1120 dprintk(1, "%s() src_sel(0x%08X) 0x%08x\n", __FUNCTION__,
1121 port->reg_src_sel, cx_read(port->reg_src_sel));
1122 dprintk(1, "%s() lngth(0x%08X) 0x%08x\n", __FUNCTION__,
1123 port->reg_lngth, cx_read(port->reg_lngth));
1124 dprintk(1, "%s() hw_sop_ctrl(0x%08X) 0x%08x\n", __FUNCTION__,
1125 port->reg_hw_sop_ctrl, cx_read(port->reg_hw_sop_ctrl));
1126 dprintk(1, "%s() gen_ctrl(0x%08X) 0x%08x\n", __FUNCTION__,
1127 port->reg_gen_ctrl, cx_read(port->reg_gen_ctrl));
1128 dprintk(1, "%s() bd_pkt_status(0x%08X) 0x%08x\n", __FUNCTION__,
1129 port->reg_bd_pkt_status, cx_read(port->reg_bd_pkt_status));
1130 dprintk(1, "%s() sop_status(0x%08X) 0x%08x\n", __FUNCTION__,
1131 port->reg_sop_status, cx_read(port->reg_sop_status));
1132 dprintk(1, "%s() fifo_ovfl_stat(0x%08X) 0x%08x\n", __FUNCTION__,
1133 port->reg_fifo_ovfl_stat, cx_read(port->reg_fifo_ovfl_stat));
1134 dprintk(1, "%s() vld_misc(0x%08X) 0x%08x\n", __FUNCTION__,
1135 port->reg_vld_misc, cx_read(port->reg_vld_misc));
1136 dprintk(1, "%s() ts_clk_en(0x%08X) 0x%08x\n", __FUNCTION__,
1137 port->reg_ts_clk_en, cx_read(port->reg_ts_clk_en));
1138 dprintk(1, "%s() ts_int_msk(0x%08X) 0x%08x\n", __FUNCTION__,
1139 port->reg_ts_int_msk, cx_read(port->reg_ts_int_msk));
1015} 1140}
1016 1141
1017static int cx23885_start_dma(struct cx23885_tsport *port, 1142static int cx23885_start_dma(struct cx23885_tsport *port,
@@ -1076,6 +1201,9 @@ static int cx23885_start_dma(struct cx23885_tsport *port,
1076 1201
1077 cx_set(DEV_CNTRL2, (1<<5)); /* Enable RISC controller */ 1202 cx_set(DEV_CNTRL2, (1<<5)); /* Enable RISC controller */
1078 1203
1204 if (debug > 4)
1205 cx23885_tsport_reg_dump(port);
1206
1079 return 0; 1207 return 0;
1080} 1208}
1081 1209
@@ -1091,7 +1219,7 @@ static int cx23885_stop_dma(struct cx23885_tsport *port)
1091 return 0; 1219 return 0;
1092} 1220}
1093 1221
1094static int cx23885_restart_queue(struct cx23885_tsport *port, 1222int cx23885_restart_queue(struct cx23885_tsport *port,
1095 struct cx23885_dmaqueue *q) 1223 struct cx23885_dmaqueue *q)
1096{ 1224{
1097 struct cx23885_dev *dev = port->dev; 1225 struct cx23885_dev *dev = port->dev;
@@ -1114,7 +1242,7 @@ static int cx23885_restart_queue(struct cx23885_tsport *port,
1114 list_del(&buf->vb.queue); 1242 list_del(&buf->vb.queue);
1115 list_add_tail(&buf->vb.queue, &q->active); 1243 list_add_tail(&buf->vb.queue, &q->active);
1116 cx23885_start_dma(port, q, buf); 1244 cx23885_start_dma(port, q, buf);
1117 buf->vb.state = STATE_ACTIVE; 1245 buf->vb.state = VIDEOBUF_ACTIVE;
1118 buf->count = q->count++; 1246 buf->count = q->count++;
1119 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); 1247 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
1120 dprintk(5, "[%p/%d] restart_queue - first active\n", 1248 dprintk(5, "[%p/%d] restart_queue - first active\n",
@@ -1125,7 +1253,7 @@ static int cx23885_restart_queue(struct cx23885_tsport *port,
1125 prev->fmt == buf->fmt) { 1253 prev->fmt == buf->fmt) {
1126 list_del(&buf->vb.queue); 1254 list_del(&buf->vb.queue);
1127 list_add_tail(&buf->vb.queue, &q->active); 1255 list_add_tail(&buf->vb.queue, &q->active);
1128 buf->vb.state = STATE_ACTIVE; 1256 buf->vb.state = VIDEOBUF_ACTIVE;
1129 buf->count = q->count++; 1257 buf->count = q->count++;
1130 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 1258 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
1131 prev->risc.jmp[2] = cpu_to_le32(0); /* 64 bit bits 63-32 */ 1259 prev->risc.jmp[2] = cpu_to_le32(0); /* 64 bit bits 63-32 */
@@ -1162,7 +1290,7 @@ int cx23885_buf_prepare(struct videobuf_queue *q, struct cx23885_tsport *port,
1162 if (0 != buf->vb.baddr && buf->vb.bsize < size) 1290 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1163 return -EINVAL; 1291 return -EINVAL;
1164 1292
1165 if (STATE_NEEDS_INIT == buf->vb.state) { 1293 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1166 buf->vb.width = port->ts_packet_size; 1294 buf->vb.width = port->ts_packet_size;
1167 buf->vb.height = port->ts_packet_count; 1295 buf->vb.height = port->ts_packet_count;
1168 buf->vb.size = size; 1296 buf->vb.size = size;
@@ -1174,7 +1302,7 @@ int cx23885_buf_prepare(struct videobuf_queue *q, struct cx23885_tsport *port,
1174 videobuf_to_dma(&buf->vb)->sglist, 1302 videobuf_to_dma(&buf->vb)->sglist,
1175 buf->vb.width, buf->vb.height); 1303 buf->vb.width, buf->vb.height);
1176 } 1304 }
1177 buf->vb.state = STATE_PREPARED; 1305 buf->vb.state = VIDEOBUF_PREPARED;
1178 return 0; 1306 return 0;
1179 1307
1180 fail: 1308 fail:
@@ -1197,7 +1325,7 @@ void cx23885_buf_queue(struct cx23885_tsport *port, struct cx23885_buffer *buf)
1197 dprintk( 1, "queue is empty - first active\n" ); 1325 dprintk( 1, "queue is empty - first active\n" );
1198 list_add_tail(&buf->vb.queue, &cx88q->active); 1326 list_add_tail(&buf->vb.queue, &cx88q->active);
1199 cx23885_start_dma(port, cx88q, buf); 1327 cx23885_start_dma(port, cx88q, buf);
1200 buf->vb.state = STATE_ACTIVE; 1328 buf->vb.state = VIDEOBUF_ACTIVE;
1201 buf->count = cx88q->count++; 1329 buf->count = cx88q->count++;
1202 mod_timer(&cx88q->timeout, jiffies + BUFFER_TIMEOUT); 1330 mod_timer(&cx88q->timeout, jiffies + BUFFER_TIMEOUT);
1203 dprintk(1, "[%p/%d] %s - first active\n", 1331 dprintk(1, "[%p/%d] %s - first active\n",
@@ -1207,7 +1335,7 @@ void cx23885_buf_queue(struct cx23885_tsport *port, struct cx23885_buffer *buf)
1207 prev = list_entry(cx88q->active.prev, struct cx23885_buffer, 1335 prev = list_entry(cx88q->active.prev, struct cx23885_buffer,
1208 vb.queue); 1336 vb.queue);
1209 list_add_tail(&buf->vb.queue, &cx88q->active); 1337 list_add_tail(&buf->vb.queue, &cx88q->active);
1210 buf->vb.state = STATE_ACTIVE; 1338 buf->vb.state = VIDEOBUF_ACTIVE;
1211 buf->count = cx88q->count++; 1339 buf->count = cx88q->count++;
1212 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 1340 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
1213 prev->risc.jmp[2] = cpu_to_le32(0); /* 64 bit bits 63-32 */ 1341 prev->risc.jmp[2] = cpu_to_le32(0); /* 64 bit bits 63-32 */
@@ -1231,7 +1359,7 @@ static void do_cancel_buffers(struct cx23885_tsport *port, char *reason,
1231 buf = list_entry(q->active.next, struct cx23885_buffer, 1359 buf = list_entry(q->active.next, struct cx23885_buffer,
1232 vb.queue); 1360 vb.queue);
1233 list_del(&buf->vb.queue); 1361 list_del(&buf->vb.queue);
1234 buf->vb.state = STATE_ERROR; 1362 buf->vb.state = VIDEOBUF_ERROR;
1235 wake_up(&buf->vb.done); 1363 wake_up(&buf->vb.done);
1236 dprintk(1, "[%p/%d] %s - dma=0x%08lx\n", 1364 dprintk(1, "[%p/%d] %s - dma=0x%08lx\n",
1237 buf, buf->vb.i, reason, (unsigned long)buf->risc.dma); 1365 buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
@@ -1243,16 +1371,6 @@ static void do_cancel_buffers(struct cx23885_tsport *port, char *reason,
1243 spin_unlock_irqrestore(&port->slock, flags); 1371 spin_unlock_irqrestore(&port->slock, flags);
1244} 1372}
1245 1373
1246void cx23885_cancel_buffers(struct cx23885_tsport *port)
1247{
1248 struct cx23885_dev *dev = port->dev;
1249 struct cx23885_dmaqueue *q = &port->mpegq;
1250
1251 dprintk(1, "%s()\n", __FUNCTION__);
1252 del_timer_sync(&q->timeout);
1253 cx23885_stop_dma(port);
1254 do_cancel_buffers(port, "cancel", 0);
1255}
1256 1374
1257static void cx23885_timeout(unsigned long data) 1375static void cx23885_timeout(unsigned long data)
1258{ 1376{
@@ -1325,12 +1443,15 @@ static irqreturn_t cx23885_irq(int irq, void *dev_id)
1325 struct cx23885_tsport *ts1 = &dev->ts1; 1443 struct cx23885_tsport *ts1 = &dev->ts1;
1326 struct cx23885_tsport *ts2 = &dev->ts2; 1444 struct cx23885_tsport *ts2 = &dev->ts2;
1327 u32 pci_status, pci_mask; 1445 u32 pci_status, pci_mask;
1446 u32 vida_status, vida_mask;
1328 u32 ts1_status, ts1_mask; 1447 u32 ts1_status, ts1_mask;
1329 u32 ts2_status, ts2_mask; 1448 u32 ts2_status, ts2_mask;
1330 int ts1_count = 0, ts2_count = 0, handled = 0; 1449 int vida_count = 0, ts1_count = 0, ts2_count = 0, handled = 0;
1331 1450
1332 pci_status = cx_read(PCI_INT_STAT); 1451 pci_status = cx_read(PCI_INT_STAT);
1333 pci_mask = cx_read(PCI_INT_MSK); 1452 pci_mask = cx_read(PCI_INT_MSK);
1453 vida_status = cx_read(VID_A_INT_STAT);
1454 vida_mask = cx_read(VID_A_INT_MSK);
1334 ts1_status = cx_read(VID_B_INT_STAT); 1455 ts1_status = cx_read(VID_B_INT_STAT);
1335 ts1_mask = cx_read(VID_B_INT_MSK); 1456 ts1_mask = cx_read(VID_B_INT_MSK);
1336 ts2_status = cx_read(VID_C_INT_STAT); 1457 ts2_status = cx_read(VID_C_INT_STAT);
@@ -1339,11 +1460,17 @@ static irqreturn_t cx23885_irq(int irq, void *dev_id)
1339 if ( (pci_status == 0) && (ts2_status == 0) && (ts1_status == 0) ) 1460 if ( (pci_status == 0) && (ts2_status == 0) && (ts1_status == 0) )
1340 goto out; 1461 goto out;
1341 1462
1463 vida_count = cx_read(VID_A_GPCNT);
1342 ts1_count = cx_read(ts1->reg_gpcnt); 1464 ts1_count = cx_read(ts1->reg_gpcnt);
1343 ts2_count = cx_read(ts2->reg_gpcnt); 1465 ts2_count = cx_read(ts2->reg_gpcnt);
1344 dprintk(7, "pci_status: 0x%08x pci_mask: 0x%08x\n", pci_status, pci_mask ); 1466 dprintk(7, "pci_status: 0x%08x pci_mask: 0x%08x\n",
1345 dprintk(7, "ts1_status: 0x%08x ts1_mask: 0x%08x count: 0x%x\n", ts1_status, ts1_mask, ts1_count ); 1467 pci_status, pci_mask);
1346 dprintk(7, "ts2_status: 0x%08x ts2_mask: 0x%08x count: 0x%x\n", ts2_status, ts2_mask, ts2_count ); 1468 dprintk(7, "vida_status: 0x%08x vida_mask: 0x%08x count: 0x%x\n",
1469 vida_status, vida_mask, vida_count);
1470 dprintk(7, "ts1_status: 0x%08x ts1_mask: 0x%08x count: 0x%x\n",
1471 ts1_status, ts1_mask, ts1_count);
1472 dprintk(7, "ts2_status: 0x%08x ts2_mask: 0x%08x count: 0x%x\n",
1473 ts2_status, ts2_mask, ts2_count);
1347 1474
1348 if ( (pci_status & PCI_MSK_RISC_RD) || 1475 if ( (pci_status & PCI_MSK_RISC_RD) ||
1349 (pci_status & PCI_MSK_RISC_WR) || 1476 (pci_status & PCI_MSK_RISC_WR) ||
@@ -1380,11 +1507,18 @@ static irqreturn_t cx23885_irq(int irq, void *dev_id)
1380 1507
1381 } 1508 }
1382 1509
1383 if (ts1_status) 1510 if (ts1_status) {
1384 handled += cx23885_irq_ts(ts1, ts1_status); 1511 if (cx23885_boards[dev->board].portb == CX23885_MPEG_DVB)
1512 handled += cx23885_irq_ts(ts1, ts1_status);
1513 }
1514
1515 if (ts2_status) {
1516 if (cx23885_boards[dev->board].portc == CX23885_MPEG_DVB)
1517 handled += cx23885_irq_ts(ts2, ts2_status);
1518 }
1385 1519
1386 if (ts2_status) 1520 if (vida_status)
1387 handled += cx23885_irq_ts(ts2, ts2_status); 1521 handled += cx23885_video_irq(dev, vida_status);
1388 1522
1389 if (handled) 1523 if (handled)
1390 cx_write(PCI_INT_STAT, pci_status); 1524 cx_write(PCI_INT_STAT, pci_status);
diff --git a/drivers/media/video/cx23885/cx23885-dvb.c b/drivers/media/video/cx23885/cx23885-dvb.c
index eda8c05d0931..ed465c007cea 100644
--- a/drivers/media/video/cx23885/cx23885-dvb.c
+++ b/drivers/media/video/cx23885/cx23885-dvb.c
@@ -32,13 +32,26 @@
32 32
33#include "s5h1409.h" 33#include "s5h1409.h"
34#include "mt2131.h" 34#include "mt2131.h"
35#include "tda8290.h"
36#include "tda18271.h"
35#include "lgdt330x.h" 37#include "lgdt330x.h"
38#include "xc5000.h"
36#include "dvb-pll.h" 39#include "dvb-pll.h"
40#include "tuner-xc2028.h"
41#include "tuner-xc2028-types.h"
37 42
38static unsigned int debug = 0; 43static unsigned int debug;
39 44
40#define dprintk(level,fmt, arg...) if (debug >= level) \ 45#define dprintk(level, fmt, arg...)\
41 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg) 46 do { if (debug >= level)\
47 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
48 } while (0)
49
50/* ------------------------------------------------------------------ */
51
52static unsigned int alt_tuner;
53module_param(alt_tuner, int, 0644);
54MODULE_PARM_DESC(alt_tuner, "Enable alternate tuner configuration");
42 55
43/* ------------------------------------------------------------------ */ 56/* ------------------------------------------------------------------ */
44 57
@@ -85,18 +98,39 @@ static struct s5h1409_config hauppauge_generic_config = {
85 .demod_address = 0x32 >> 1, 98 .demod_address = 0x32 >> 1,
86 .output_mode = S5H1409_SERIAL_OUTPUT, 99 .output_mode = S5H1409_SERIAL_OUTPUT,
87 .gpio = S5H1409_GPIO_ON, 100 .gpio = S5H1409_GPIO_ON,
88 .if_freq = 44000, 101 .qam_if = 44000,
89 .inversion = S5H1409_INVERSION_OFF, 102 .inversion = S5H1409_INVERSION_OFF,
90 .status_mode = S5H1409_DEMODLOCKING 103 .status_mode = S5H1409_DEMODLOCKING,
104 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
105};
106
107static struct s5h1409_config hauppauge_ezqam_config = {
108 .demod_address = 0x32 >> 1,
109 .output_mode = S5H1409_SERIAL_OUTPUT,
110 .gpio = S5H1409_GPIO_OFF,
111 .qam_if = 4000,
112 .inversion = S5H1409_INVERSION_ON,
113 .status_mode = S5H1409_DEMODLOCKING,
114 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
91}; 115};
92 116
93static struct s5h1409_config hauppauge_hvr1800lp_config = { 117static struct s5h1409_config hauppauge_hvr1800lp_config = {
94 .demod_address = 0x32 >> 1, 118 .demod_address = 0x32 >> 1,
95 .output_mode = S5H1409_SERIAL_OUTPUT, 119 .output_mode = S5H1409_SERIAL_OUTPUT,
96 .gpio = S5H1409_GPIO_OFF, 120 .gpio = S5H1409_GPIO_OFF,
97 .if_freq = 44000, 121 .qam_if = 44000,
122 .inversion = S5H1409_INVERSION_OFF,
123 .status_mode = S5H1409_DEMODLOCKING,
124 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
125};
126
127static struct s5h1409_config hauppauge_hvr1500_config = {
128 .demod_address = 0x32 >> 1,
129 .output_mode = S5H1409_SERIAL_OUTPUT,
130 .gpio = S5H1409_GPIO_OFF,
98 .inversion = S5H1409_INVERSION_OFF, 131 .inversion = S5H1409_INVERSION_OFF,
99 .status_mode = S5H1409_DEMODLOCKING 132 .status_mode = S5H1409_DEMODLOCKING,
133 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
100}; 134};
101 135
102static struct mt2131_config hauppauge_generic_tunerconfig = { 136static struct mt2131_config hauppauge_generic_tunerconfig = {
@@ -109,6 +143,66 @@ static struct lgdt330x_config fusionhdtv_5_express = {
109 .serial_mpeg = 0x40, 143 .serial_mpeg = 0x40,
110}; 144};
111 145
146static struct s5h1409_config hauppauge_hvr1500q_config = {
147 .demod_address = 0x32 >> 1,
148 .output_mode = S5H1409_SERIAL_OUTPUT,
149 .gpio = S5H1409_GPIO_ON,
150 .qam_if = 44000,
151 .inversion = S5H1409_INVERSION_OFF,
152 .status_mode = S5H1409_DEMODLOCKING,
153 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
154};
155
156static struct xc5000_config hauppauge_hvr1500q_tunerconfig = {
157 .i2c_address = 0x61,
158 .if_khz = 5380,
159 .tuner_callback = cx23885_tuner_callback
160};
161
162static struct tda829x_config tda829x_no_probe = {
163 .probe_tuner = TDA829X_DONT_PROBE,
164};
165
166static struct tda18271_std_map hauppauge_tda18271_std_map = {
167 .atsc_6 = { .if_freq = 5380, .std_bits = 0x1b },
168 .qam_6 = { .if_freq = 4000, .std_bits = 0x18 },
169};
170
171static struct tda18271_config hauppauge_tda18271_config = {
172 .std_map = &hauppauge_tda18271_std_map,
173 .gate = TDA18271_GATE_ANALOG,
174};
175
176static int cx23885_hvr1500_xc3028_callback(void *ptr, int command, int arg)
177{
178 struct cx23885_tsport *port = ptr;
179 struct cx23885_dev *dev = port->dev;
180
181 switch (command) {
182 case XC2028_TUNER_RESET:
183 /* Send the tuner in then out of reset */
184 /* GPIO-2 xc3028 tuner */
185 dprintk(1, "%s: XC2028_TUNER_RESET %d\n", __FUNCTION__, arg);
186
187 cx_set(GP0_IO, 0x00040000);
188 cx_clear(GP0_IO, 0x00000004);
189 msleep(5);
190
191 cx_set(GP0_IO, 0x00040004);
192 msleep(5);
193 break;
194 case XC2028_RESET_CLK:
195 dprintk(1, "%s: XC2028_RESET_CLK %d\n", __FUNCTION__, arg);
196 break;
197 default:
198 dprintk(1, "%s: unknown command %d, arg %d\n", __FUNCTION__,
199 command, arg);
200 return -EINVAL;
201 }
202
203 return 0;
204}
205
112static int dvb_register(struct cx23885_tsport *port) 206static int dvb_register(struct cx23885_tsport *port)
113{ 207{
114 struct cx23885_dev *dev = port->dev; 208 struct cx23885_dev *dev = port->dev;
@@ -120,7 +214,6 @@ static int dvb_register(struct cx23885_tsport *port)
120 /* init frontend */ 214 /* init frontend */
121 switch (dev->board) { 215 switch (dev->board) {
122 case CX23885_BOARD_HAUPPAUGE_HVR1250: 216 case CX23885_BOARD_HAUPPAUGE_HVR1250:
123 case CX23885_BOARD_HAUPPAUGE_HVR1800:
124 i2c_bus = &dev->i2c_bus[0]; 217 i2c_bus = &dev->i2c_bus[0];
125 port->dvb.frontend = dvb_attach(s5h1409_attach, 218 port->dvb.frontend = dvb_attach(s5h1409_attach,
126 &hauppauge_generic_config, 219 &hauppauge_generic_config,
@@ -131,6 +224,36 @@ static int dvb_register(struct cx23885_tsport *port)
131 &hauppauge_generic_tunerconfig, 0); 224 &hauppauge_generic_tunerconfig, 0);
132 } 225 }
133 break; 226 break;
227 case CX23885_BOARD_HAUPPAUGE_HVR1800:
228 i2c_bus = &dev->i2c_bus[0];
229 switch (alt_tuner) {
230 case 1:
231 port->dvb.frontend =
232 dvb_attach(s5h1409_attach,
233 &hauppauge_ezqam_config,
234 &i2c_bus->i2c_adap);
235 if (port->dvb.frontend != NULL) {
236 dvb_attach(tda829x_attach, port->dvb.frontend,
237 &dev->i2c_bus[1].i2c_adap, 0x42,
238 &tda829x_no_probe);
239 dvb_attach(tda18271_attach, port->dvb.frontend,
240 0x60, &dev->i2c_bus[1].i2c_adap,
241 &hauppauge_tda18271_config);
242 }
243 break;
244 case 0:
245 default:
246 port->dvb.frontend =
247 dvb_attach(s5h1409_attach,
248 &hauppauge_generic_config,
249 &i2c_bus->i2c_adap);
250 if (port->dvb.frontend != NULL)
251 dvb_attach(mt2131_attach, port->dvb.frontend,
252 &i2c_bus->i2c_adap,
253 &hauppauge_generic_tunerconfig, 0);
254 break;
255 }
256 break;
134 case CX23885_BOARD_HAUPPAUGE_HVR1800lp: 257 case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
135 i2c_bus = &dev->i2c_bus[0]; 258 i2c_bus = &dev->i2c_bus[0];
136 port->dvb.frontend = dvb_attach(s5h1409_attach, 259 port->dvb.frontend = dvb_attach(s5h1409_attach,
@@ -152,6 +275,43 @@ static int dvb_register(struct cx23885_tsport *port)
152 &i2c_bus->i2c_adap, DVB_PLL_LG_TDVS_H06XF); 275 &i2c_bus->i2c_adap, DVB_PLL_LG_TDVS_H06XF);
153 } 276 }
154 break; 277 break;
278 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
279 i2c_bus = &dev->i2c_bus[1];
280 port->dvb.frontend = dvb_attach(s5h1409_attach,
281 &hauppauge_hvr1500q_config,
282 &dev->i2c_bus[0].i2c_adap);
283 if (port->dvb.frontend != NULL) {
284 hauppauge_hvr1500q_tunerconfig.priv = i2c_bus;
285 dvb_attach(xc5000_attach, port->dvb.frontend,
286 &i2c_bus->i2c_adap,
287 &hauppauge_hvr1500q_tunerconfig);
288 }
289 break;
290 case CX23885_BOARD_HAUPPAUGE_HVR1500:
291 i2c_bus = &dev->i2c_bus[1];
292 port->dvb.frontend = dvb_attach(s5h1409_attach,
293 &hauppauge_hvr1500_config,
294 &dev->i2c_bus[0].i2c_adap);
295 if (port->dvb.frontend != NULL) {
296 struct dvb_frontend *fe;
297 struct xc2028_config cfg = {
298 .i2c_adap = &i2c_bus->i2c_adap,
299 .i2c_addr = 0x61,
300 .video_dev = port,
301 .callback = cx23885_hvr1500_xc3028_callback,
302 };
303 static struct xc2028_ctrl ctl = {
304 .fname = "xc3028-v27.fw",
305 .max_len = 64,
306 .scode_table = OREN538,
307 };
308
309 fe = dvb_attach(xc2028_attach,
310 port->dvb.frontend, &cfg);
311 if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
312 fe->ops.tuner_ops.set_config(fe, &ctl);
313 }
314 break;
155 default: 315 default:
156 printk("%s: The frontend of your DVB/ATSC card isn't supported yet\n", 316 printk("%s: The frontend of your DVB/ATSC card isn't supported yet\n",
157 dev->name); 317 dev->name);
@@ -165,6 +325,9 @@ static int dvb_register(struct cx23885_tsport *port)
165 /* Put the analog decoder in standby to keep it quiet */ 325 /* Put the analog decoder in standby to keep it quiet */
166 cx23885_call_i2c_clients(i2c_bus, TUNER_SET_STANDBY, NULL); 326 cx23885_call_i2c_clients(i2c_bus, TUNER_SET_STANDBY, NULL);
167 327
328 if (port->dvb.frontend->ops.analog_ops.standby)
329 port->dvb.frontend->ops.analog_ops.standby(port->dvb.frontend);
330
168 /* register everything */ 331 /* register everything */
169 return videobuf_dvb_register(&port->dvb, THIS_MODULE, port, 332 return videobuf_dvb_register(&port->dvb, THIS_MODULE, port,
170 &dev->pci->dev); 333 &dev->pci->dev);
diff --git a/drivers/media/video/cx23885/cx23885-i2c.c b/drivers/media/video/cx23885/cx23885-i2c.c
index 71da528932df..92fe0bd37c84 100644
--- a/drivers/media/video/cx23885/cx23885-i2c.c
+++ b/drivers/media/video/cx23885/cx23885-i2c.c
@@ -29,7 +29,7 @@
29 29
30#include <media/v4l2-common.h> 30#include <media/v4l2-common.h>
31 31
32static unsigned int i2c_debug = 0; 32static unsigned int i2c_debug;
33module_param(i2c_debug, int, 0644); 33module_param(i2c_debug, int, 0644);
34MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); 34MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
35 35
@@ -37,8 +37,10 @@ static unsigned int i2c_scan = 0;
37module_param(i2c_scan, int, 0444); 37module_param(i2c_scan, int, 0444);
38MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); 38MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
39 39
40#define dprintk(level,fmt, arg...) if (i2c_debug >= level) \ 40#define dprintk(level, fmt, arg...)\
41 printk(KERN_DEBUG "%s: " fmt, dev->name , ## arg) 41 do { if (i2c_debug >= level)\
42 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
43 } while (0)
42 44
43#define I2C_WAIT_DELAY 32 45#define I2C_WAIT_DELAY 32
44#define I2C_WAIT_RETRY 64 46#define I2C_WAIT_RETRY 64
@@ -77,14 +79,19 @@ static int i2c_wait_done(struct i2c_adapter *i2c_adap)
77} 79}
78 80
79static int i2c_sendbytes(struct i2c_adapter *i2c_adap, 81static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
80 const struct i2c_msg *msg, int last) 82 const struct i2c_msg *msg, int joined_rlen)
81{ 83{
82 struct cx23885_i2c *bus = i2c_adap->algo_data; 84 struct cx23885_i2c *bus = i2c_adap->algo_data;
83 struct cx23885_dev *dev = bus->dev; 85 struct cx23885_dev *dev = bus->dev;
84 u32 wdata, addr, ctrl; 86 u32 wdata, addr, ctrl;
85 int retval, cnt; 87 int retval, cnt;
86 88
87 dprintk(1, "%s()\n", __FUNCTION__); 89 if (joined_rlen)
90 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __FUNCTION__,
91 msg->len, joined_rlen);
92 else
93 dprintk(1, "%s(msg->len=%d)\n", __FUNCTION__, msg->len);
94
88 /* Deal with i2c probe functions with zero payload */ 95 /* Deal with i2c probe functions with zero payload */
89 if (msg->len == 0) { 96 if (msg->len == 0) {
90 cx_write(bus->reg_addr, msg->addr << 25); 97 cx_write(bus->reg_addr, msg->addr << 25);
@@ -106,6 +113,8 @@ static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
106 113
107 if (msg->len > 1) 114 if (msg->len > 1)
108 ctrl |= I2C_NOSTOP | I2C_EXTEND; 115 ctrl |= I2C_NOSTOP | I2C_EXTEND;
116 else if (joined_rlen)
117 ctrl |= I2C_NOSTOP;
109 118
110 cx_write(bus->reg_addr, addr); 119 cx_write(bus->reg_addr, addr);
111 cx_write(bus->reg_wdata, wdata); 120 cx_write(bus->reg_wdata, wdata);
@@ -127,8 +136,10 @@ static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
127 wdata = msg->buf[cnt]; 136 wdata = msg->buf[cnt];
128 ctrl = bus->i2c_period | (1 << 12) | (1 << 2); 137 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
129 138
130 if (cnt < msg->len-1 || !last) 139 if (cnt < msg->len - 1)
131 ctrl |= I2C_NOSTOP | I2C_EXTEND; 140 ctrl |= I2C_NOSTOP | I2C_EXTEND;
141 else if (joined_rlen)
142 ctrl |= I2C_NOSTOP;
132 143
133 cx_write(bus->reg_addr, addr); 144 cx_write(bus->reg_addr, addr);
134 cx_write(bus->reg_wdata, wdata); 145 cx_write(bus->reg_wdata, wdata);
@@ -150,19 +161,22 @@ static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
150 eio: 161 eio:
151 retval = -EIO; 162 retval = -EIO;
152 err: 163 err:
153 printk(" ERR: %d\n", retval); 164 if (i2c_debug)
165 printk(" ERR: %d\n", retval);
154 return retval; 166 return retval;
155} 167}
156 168
157static int i2c_readbytes(struct i2c_adapter *i2c_adap, 169static int i2c_readbytes(struct i2c_adapter *i2c_adap,
158 const struct i2c_msg *msg, int last) 170 const struct i2c_msg *msg, int joined)
159{ 171{
160 struct cx23885_i2c *bus = i2c_adap->algo_data; 172 struct cx23885_i2c *bus = i2c_adap->algo_data;
161 struct cx23885_dev *dev = bus->dev; 173 struct cx23885_dev *dev = bus->dev;
162 u32 ctrl, cnt; 174 u32 ctrl, cnt;
163 int retval; 175 int retval;
164 176
165 dprintk(1, "%s()\n", __FUNCTION__); 177
178 if (i2c_debug && !joined)
179 dprintk(1, "%s(msg->len=%d)\n", __FUNCTION__, msg->len);
166 180
167 /* Deal with i2c probe functions with zero payload */ 181 /* Deal with i2c probe functions with zero payload */
168 if (msg->len == 0) { 182 if (msg->len == 0) {
@@ -178,11 +192,18 @@ static int i2c_readbytes(struct i2c_adapter *i2c_adap,
178 return 0; 192 return 0;
179 } 193 }
180 194
195 if (i2c_debug) {
196 if (joined)
197 printk(" R");
198 else
199 printk(" <R %02x", (msg->addr << 1) + 1);
200 }
201
181 for(cnt = 0; cnt < msg->len; cnt++) { 202 for(cnt = 0; cnt < msg->len; cnt++) {
182 203
183 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1; 204 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
184 205
185 if (cnt < msg->len-1 || !last) 206 if (cnt < msg->len - 1)
186 ctrl |= I2C_NOSTOP | I2C_EXTEND; 207 ctrl |= I2C_NOSTOP | I2C_EXTEND;
187 208
188 cx_write(bus->reg_addr, msg->addr << 25); 209 cx_write(bus->reg_addr, msg->addr << 25);
@@ -195,9 +216,7 @@ static int i2c_readbytes(struct i2c_adapter *i2c_adap,
195 goto eio; 216 goto eio;
196 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff; 217 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
197 if (i2c_debug) { 218 if (i2c_debug) {
198 if (!(ctrl & I2C_NOSTOP)) 219 printk(" %02x", msg->buf[cnt]);
199 printk(" <R %02x", (msg->addr << 1) +1);
200 printk(" =%02x", msg->buf[cnt]);
201 if (!(ctrl & I2C_NOSTOP)) 220 if (!(ctrl & I2C_NOSTOP))
202 printk(" >\n"); 221 printk(" >\n");
203 } 222 }
@@ -207,7 +226,8 @@ static int i2c_readbytes(struct i2c_adapter *i2c_adap,
207 eio: 226 eio:
208 retval = -EIO; 227 retval = -EIO;
209 err: 228 err:
210 printk(" ERR: %d\n", retval); 229 if (i2c_debug)
230 printk(" ERR: %d\n", retval);
211 return retval; 231 return retval;
212} 232}
213 233
@@ -225,15 +245,22 @@ static int i2c_xfer(struct i2c_adapter *i2c_adap,
225 __FUNCTION__, num, msgs[i].addr, msgs[i].len); 245 __FUNCTION__, num, msgs[i].addr, msgs[i].len);
226 if (msgs[i].flags & I2C_M_RD) { 246 if (msgs[i].flags & I2C_M_RD) {
227 /* read */ 247 /* read */
228 retval = i2c_readbytes(i2c_adap, &msgs[i], i+1 == num); 248 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
249 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
250 msgs[i].addr == msgs[i + 1].addr) {
251 /* write then read from same address */
252 retval = i2c_sendbytes(i2c_adap, &msgs[i],
253 msgs[i + 1].len);
229 if (retval < 0) 254 if (retval < 0)
230 goto err; 255 goto err;
256 i++;
257 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
231 } else { 258 } else {
232 /* write */ 259 /* write */
233 retval = i2c_sendbytes(i2c_adap, &msgs[i], i+1 == num); 260 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
234 if (retval < 0)
235 goto err;
236 } 261 }
262 if (retval < 0)
263 goto err;
237 } 264 }
238 return num; 265 return num;
239 266
@@ -243,7 +270,9 @@ static int i2c_xfer(struct i2c_adapter *i2c_adap,
243 270
244static int attach_inform(struct i2c_client *client) 271static int attach_inform(struct i2c_client *client)
245{ 272{
246 struct cx23885_dev *dev = i2c_get_adapdata(client->adapter); 273 struct cx23885_i2c *bus = i2c_get_adapdata(client->adapter);
274 struct cx23885_dev *dev = bus->dev;
275 struct tuner_setup tun_setup;
247 276
248 dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n", 277 dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
249 client->driver->driver.name, client->addr, client->name); 278 client->driver->driver.name, client->addr, client->name);
@@ -251,6 +280,31 @@ static int attach_inform(struct i2c_client *client)
251 if (!client->driver->command) 280 if (!client->driver->command)
252 return 0; 281 return 0;
253 282
283 if (dev->tuner_type != UNSET) {
284
285 dprintk(1, "%s (tuner) i2c attach [addr=0x%x,client=%s]\n",
286 client->driver->driver.name, client->addr,
287 client->name);
288
289 if ((dev->tuner_addr == ADDR_UNSET) ||
290 (dev->tuner_addr == client->addr)) {
291
292 dprintk(1, "%s (tuner || addr UNSET)\n",
293 client->driver->driver.name);
294
295 dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
296 client->driver->driver.name,
297 client->addr, client->name);
298
299 tun_setup.mode_mask = T_ANALOG_TV;
300 tun_setup.type = dev->tuner_type;
301 tun_setup.addr = dev->tuner_addr;
302
303 client->driver->command(client, TUNER_SET_TYPE_ADDR,
304 &tun_setup);
305 }
306 }
307
254 return 0; 308 return 0;
255} 309}
256 310
@@ -289,6 +343,7 @@ static struct i2c_adapter cx23885_i2c_adap_template = {
289 .owner = THIS_MODULE, 343 .owner = THIS_MODULE,
290 .id = I2C_HW_B_CX23885, 344 .id = I2C_HW_B_CX23885,
291 .algo = &cx23885_i2c_algo_template, 345 .algo = &cx23885_i2c_algo_template,
346 .class = I2C_CLASS_TV_ANALOG,
292 .client_register = attach_inform, 347 .client_register = attach_inform,
293 .client_unregister = detach_inform, 348 .client_unregister = detach_inform,
294}; 349};
@@ -305,7 +360,7 @@ static char *i2c_devs[128] = {
305 [ 0x84 >> 1 ] = "tda8295", 360 [ 0x84 >> 1 ] = "tda8295",
306 [ 0xa0 >> 1 ] = "eeprom", 361 [ 0xa0 >> 1 ] = "eeprom",
307 [ 0xc0 >> 1 ] = "tuner/mt2131/tda8275", 362 [ 0xc0 >> 1 ] = "tuner/mt2131/tda8275",
308 [ 0xc2 >> 1 ] = "tuner/mt2131/tda8275", 363 [ 0xc2 >> 1 ] = "tuner/mt2131/tda8275/xc5000",
309}; 364};
310 365
311static void do_i2c_scan(char *name, struct i2c_client *c) 366static void do_i2c_scan(char *name, struct i2c_client *c)
@@ -344,6 +399,7 @@ int cx23885_i2c_register(struct cx23885_i2c *bus)
344 399
345 bus->i2c_algo.data = bus; 400 bus->i2c_algo.data = bus;
346 bus->i2c_adap.algo_data = bus; 401 bus->i2c_adap.algo_data = bus;
402 i2c_set_adapdata(&bus->i2c_adap, bus);
347 i2c_add_adapter(&bus->i2c_adap); 403 i2c_add_adapter(&bus->i2c_adap);
348 404
349 bus->i2c_client.adapter = &bus->i2c_adap; 405 bus->i2c_client.adapter = &bus->i2c_adap;
@@ -366,8 +422,6 @@ int cx23885_i2c_unregister(struct cx23885_i2c *bus)
366 422
367/* ----------------------------------------------------------------------- */ 423/* ----------------------------------------------------------------------- */
368 424
369EXPORT_SYMBOL(cx23885_call_i2c_clients);
370
371/* 425/*
372 * Local variables: 426 * Local variables:
373 * c-basic-offset: 8 427 * c-basic-offset: 8
diff --git a/drivers/media/video/cx23885/cx23885-reg.h b/drivers/media/video/cx23885/cx23885-reg.h
index 162169f9091b..bdd11bc513ad 100644
--- a/drivers/media/video/cx23885/cx23885-reg.h
+++ b/drivers/media/video/cx23885/cx23885-reg.h
@@ -233,6 +233,17 @@ Channel manager Data Structure entry = 20 DWORD
233#define VID_A_INT_SSTAT 0x0004002C 233#define VID_A_INT_SSTAT 0x0004002C
234 234
235#define VID_B_INT_MSK 0x00040030 235#define VID_B_INT_MSK 0x00040030
236#define VID_B_MSK_BAD_PKT (1 << 20)
237#define VID_B_MSK_VBI_OPC_ERR (1 << 17)
238#define VID_B_MSK_OPC_ERR (1 << 16)
239#define VID_B_MSK_VBI_SYNC (1 << 13)
240#define VID_B_MSK_SYNC (1 << 12)
241#define VID_B_MSK_VBI_OF (1 << 9)
242#define VID_B_MSK_OF (1 << 8)
243#define VID_B_MSK_VBI_RISCI2 (1 << 5)
244#define VID_B_MSK_RISCI2 (1 << 4)
245#define VID_B_MSK_VBI_RISCI1 (1 << 1)
246#define VID_B_MSK_RISCI1 1
236#define VID_B_INT_STAT 0x00040034 247#define VID_B_INT_STAT 0x00040034
237#define VID_B_INT_MSTAT 0x00040038 248#define VID_B_INT_MSTAT 0x00040038
238#define VID_B_INT_SSTAT 0x0004003C 249#define VID_B_INT_SSTAT 0x0004003C
@@ -276,6 +287,7 @@ Channel manager Data Structure entry = 20 DWORD
276 287
277#define RDR_CFG0 0x00050000 288#define RDR_CFG0 0x00050000
278#define RDR_CFG1 0x00050004 289#define RDR_CFG1 0x00050004
290#define RDR_CFG2 0x00050008
279#define RDR_TLCTL0 0x00050318 291#define RDR_TLCTL0 0x00050318
280 292
281/* APB DMAC Current Buffer Pointer */ 293/* APB DMAC Current Buffer Pointer */
@@ -335,6 +347,7 @@ Channel manager Data Structure entry = 20 DWORD
335/* GPIO (417 Microsoftcontroller) Output Enable, Low Active */ 347/* GPIO (417 Microsoftcontroller) Output Enable, Low Active */
336#define MC417_OEN 0x00110024 348#define MC417_OEN 0x00110024
337#define MC417_CTL 0x00110028 349#define MC417_CTL 0x00110028
350#define ALT_PIN_OUT_SEL 0x0011002C
338#define CLK_DELAY 0x00110048 351#define CLK_DELAY 0x00110048
339#define PAD_CTRL 0x0011004C 352#define PAD_CTRL 0x0011004C
340 353
diff --git a/drivers/media/video/cx23885/cx23885-vbi.c b/drivers/media/video/cx23885/cx23885-vbi.c
new file mode 100644
index 000000000000..e36e3fcae2fb
--- /dev/null
+++ b/drivers/media/video/cx23885/cx23885-vbi.c
@@ -0,0 +1,258 @@
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2007 Steven Toth <stoth@hauppauge.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/kernel.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27
28#include "cx23885.h"
29
30static unsigned int vbibufs = 4;
31module_param(vbibufs, int, 0644);
32MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
33
34static unsigned int vbi_debug;
35module_param(vbi_debug, int, 0644);
36MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
37
38#define dprintk(level, fmt, arg...)\
39 do { if (vbi_debug >= level)\
40 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
41 } while (0)
42
43/* ------------------------------------------------------------------ */
44
45int cx23885_vbi_fmt(struct file *file, void *priv,
46 struct v4l2_format *f)
47{
48 struct cx23885_fh *fh = priv;
49 struct cx23885_dev *dev = fh->dev;
50
51 if (dev->tvnorm & V4L2_STD_525_60) {
52 /* ntsc */
53 f->fmt.vbi.sampling_rate = 28636363;
54 f->fmt.vbi.start[0] = 10;
55 f->fmt.vbi.start[1] = 273;
56
57 } else if (dev->tvnorm & V4L2_STD_625_50) {
58 /* pal */
59 f->fmt.vbi.sampling_rate = 35468950;
60 f->fmt.vbi.start[0] = 7 - 1;
61 f->fmt.vbi.start[1] = 319 - 1;
62 }
63 return 0;
64}
65
66static int cx23885_start_vbi_dma(struct cx23885_dev *dev,
67 struct cx23885_dmaqueue *q,
68 struct cx23885_buffer *buf)
69{
70 /* setup fifo + format */
71 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
72 buf->vb.width, buf->risc.dma);
73
74 /* reset counter */
75 q->count = 1;
76
77 /* enable irqs */
78 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
79 cx_set(VID_A_INT_MSK, 0x000022);
80
81 /* start dma */
82 cx_set(DEV_CNTRL2, (1<<5));
83 cx_set(VID_A_DMA_CTL, 0x00000022);
84
85 return 0;
86}
87
88int cx23885_stop_vbi_dma(struct cx23885_dev *dev)
89{
90 /* stop dma */
91 cx_clear(VID_A_DMA_CTL, 0x00000022);
92
93 /* disable irqs */
94 cx_clear(PCI_INT_MSK, 0x000001);
95 cx_clear(VID_A_INT_MSK, 0x00000022);
96 return 0;
97}
98
99int cx23885_restart_vbi_queue(struct cx23885_dev *dev,
100 struct cx23885_dmaqueue *q)
101{
102 struct cx23885_buffer *buf;
103 struct list_head *item;
104
105 if (list_empty(&q->active))
106 return 0;
107
108 buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue);
109 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
110 buf, buf->vb.i);
111 cx23885_start_vbi_dma(dev, q, buf);
112 list_for_each(item, &q->active) {
113 buf = list_entry(item, struct cx23885_buffer, vb.queue);
114 buf->count = q->count++;
115 }
116 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
117 return 0;
118}
119
120void cx23885_vbi_timeout(unsigned long data)
121{
122 struct cx23885_dev *dev = (struct cx23885_dev *)data;
123 struct cx23885_dmaqueue *q = &dev->vbiq;
124 struct cx23885_buffer *buf;
125 unsigned long flags;
126
127 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH02]);
128
129 cx_clear(VID_A_DMA_CTL, 0x22);
130
131 spin_lock_irqsave(&dev->slock, flags);
132 while (!list_empty(&q->active)) {
133 buf = list_entry(q->active.next, struct cx23885_buffer,
134 vb.queue);
135 list_del(&buf->vb.queue);
136 buf->vb.state = VIDEOBUF_ERROR;
137 wake_up(&buf->vb.done);
138 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
139 buf, buf->vb.i, (unsigned long)buf->risc.dma);
140 }
141 cx23885_restart_vbi_queue(dev, q);
142 spin_unlock_irqrestore(&dev->slock, flags);
143}
144
145/* ------------------------------------------------------------------ */
146#define VBI_LINE_LENGTH 2048
147#define VBI_LINE_COUNT 17
148
149static int
150vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
151{
152 *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
153 if (0 == *count)
154 *count = vbibufs;
155 if (*count < 2)
156 *count = 2;
157 if (*count > 32)
158 *count = 32;
159 return 0;
160}
161
162static int
163vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
164 enum v4l2_field field)
165{
166 struct cx23885_fh *fh = q->priv_data;
167 struct cx23885_dev *dev = fh->dev;
168 struct cx23885_buffer *buf = container_of(vb,
169 struct cx23885_buffer, vb);
170 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
171 unsigned int size;
172 int rc;
173
174 size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
175 if (0 != buf->vb.baddr && buf->vb.bsize < size)
176 return -EINVAL;
177
178 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
179 buf->vb.width = VBI_LINE_LENGTH;
180 buf->vb.height = VBI_LINE_COUNT;
181 buf->vb.size = size;
182 buf->vb.field = V4L2_FIELD_SEQ_TB;
183
184 rc = videobuf_iolock(q, &buf->vb, NULL);
185 if (0 != rc)
186 goto fail;
187 cx23885_risc_buffer(dev->pci, &buf->risc,
188 dma->sglist,
189 0, buf->vb.width * buf->vb.height,
190 buf->vb.width, 0,
191 buf->vb.height);
192 }
193 buf->vb.state = VIDEOBUF_PREPARED;
194 return 0;
195
196 fail:
197 cx23885_free_buffer(q, buf);
198 return rc;
199}
200
201static void
202vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
203{
204 struct cx23885_buffer *buf =
205 container_of(vb, struct cx23885_buffer, vb);
206 struct cx23885_buffer *prev;
207 struct cx23885_fh *fh = vq->priv_data;
208 struct cx23885_dev *dev = fh->dev;
209 struct cx23885_dmaqueue *q = &dev->vbiq;
210
211 /* add jump to stopper */
212 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
213 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
214 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
215
216 if (list_empty(&q->active)) {
217 list_add_tail(&buf->vb.queue, &q->active);
218 cx23885_start_vbi_dma(dev, q, buf);
219 buf->vb.state = VIDEOBUF_ACTIVE;
220 buf->count = q->count++;
221 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
222 dprintk(2, "[%p/%d] vbi_queue - first active\n",
223 buf, buf->vb.i);
224
225 } else {
226 prev = list_entry(q->active.prev, struct cx23885_buffer,
227 vb.queue);
228 list_add_tail(&buf->vb.queue, &q->active);
229 buf->vb.state = VIDEOBUF_ACTIVE;
230 buf->count = q->count++;
231 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
232 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63-32 */
233 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
234 buf, buf->vb.i);
235 }
236}
237
238static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
239{
240 struct cx23885_buffer *buf =
241 container_of(vb, struct cx23885_buffer, vb);
242
243 cx23885_free_buffer(q, buf);
244}
245
246struct videobuf_queue_ops cx23885_vbi_qops = {
247 .buf_setup = vbi_setup,
248 .buf_prepare = vbi_prepare,
249 .buf_queue = vbi_queue,
250 .buf_release = vbi_release,
251};
252
253/* ------------------------------------------------------------------ */
254/*
255 * Local variables:
256 * c-basic-offset: 8
257 * End:
258 */
diff --git a/drivers/media/video/cx23885/cx23885-video.c b/drivers/media/video/cx23885/cx23885-video.c
new file mode 100644
index 000000000000..d3c4d2c5cbe0
--- /dev/null
+++ b/drivers/media/video/cx23885/cx23885-video.c
@@ -0,0 +1,1557 @@
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2007 Steven Toth <stoth@hauppauge.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 <linux/kthread.h>
32#include <asm/div64.h>
33
34#include "cx23885.h"
35#include <media/v4l2-common.h>
36
37#ifdef CONFIG_VIDEO_V4L1_COMPAT
38/* Include V4L1 specific functions. Should be removed soon */
39#include <linux/videodev.h>
40#endif
41
42MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
43MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
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/0: " fmt, dev->name, ## arg);\
75 } while (0)
76
77/* ------------------------------------------------------------------- */
78/* static data */
79
80#define FORMAT_FLAGS_PACKED 0x01
81
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
136static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
137{
138 unsigned int i;
139
140 for (i = 0; i < ARRAY_SIZE(formats); i++)
141 if (formats[i].fourcc == fourcc)
142 return formats+i;
143
144 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __FUNCTION__, fourcc);
145 return NULL;
146}
147
148/* ------------------------------------------------------------------- */
149
150static const struct v4l2_queryctrl no_ctl = {
151 .name = "42",
152 .flags = V4L2_CTRL_FLAG_DISABLED,
153};
154
155static struct cx23885_ctrl cx23885_ctls[] = {
156 /* --- video --- */
157 {
158 .v = {
159 .id = V4L2_CID_BRIGHTNESS,
160 .name = "Brightness",
161 .minimum = 0x00,
162 .maximum = 0xff,
163 .step = 1,
164 .default_value = 0x7f,
165 .type = V4L2_CTRL_TYPE_INTEGER,
166 },
167 .off = 128,
168 .reg = LUMA_CTRL,
169 .mask = 0x00ff,
170 .shift = 0,
171 }, {
172 .v = {
173 .id = V4L2_CID_CONTRAST,
174 .name = "Contrast",
175 .minimum = 0,
176 .maximum = 0xff,
177 .step = 1,
178 .default_value = 0x3f,
179 .type = V4L2_CTRL_TYPE_INTEGER,
180 },
181 .off = 0,
182 .reg = LUMA_CTRL,
183 .mask = 0xff00,
184 .shift = 8,
185 }, {
186 .v = {
187 .id = V4L2_CID_HUE,
188 .name = "Hue",
189 .minimum = 0,
190 .maximum = 0xff,
191 .step = 1,
192 .default_value = 0x7f,
193 .type = V4L2_CTRL_TYPE_INTEGER,
194 },
195 .off = 128,
196 .reg = CHROMA_CTRL,
197 .mask = 0xff0000,
198 .shift = 16,
199 }, {
200 /* strictly, this only describes only U saturation.
201 * V saturation is handled specially through code.
202 */
203 .v = {
204 .id = V4L2_CID_SATURATION,
205 .name = "Saturation",
206 .minimum = 0,
207 .maximum = 0xff,
208 .step = 1,
209 .default_value = 0x7f,
210 .type = V4L2_CTRL_TYPE_INTEGER,
211 },
212 .off = 0,
213 .reg = CHROMA_CTRL,
214 .mask = 0x00ff,
215 .shift = 0,
216 }, {
217 /* --- audio --- */
218 .v = {
219 .id = V4L2_CID_AUDIO_MUTE,
220 .name = "Mute",
221 .minimum = 0,
222 .maximum = 1,
223 .default_value = 1,
224 .type = V4L2_CTRL_TYPE_BOOLEAN,
225 },
226 .reg = PATH1_CTL1,
227 .mask = (0x1f << 24),
228 .shift = 24,
229 }, {
230 .v = {
231 .id = V4L2_CID_AUDIO_VOLUME,
232 .name = "Volume",
233 .minimum = 0,
234 .maximum = 0x3f,
235 .step = 1,
236 .default_value = 0x3f,
237 .type = V4L2_CTRL_TYPE_INTEGER,
238 },
239 .reg = PATH1_VOL_CTL,
240 .mask = 0xff,
241 .shift = 0,
242 }
243};
244static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
245
246const u32 cx23885_user_ctrls[] = {
247 V4L2_CID_USER_CLASS,
248 V4L2_CID_BRIGHTNESS,
249 V4L2_CID_CONTRAST,
250 V4L2_CID_SATURATION,
251 V4L2_CID_HUE,
252 V4L2_CID_AUDIO_VOLUME,
253 V4L2_CID_AUDIO_MUTE,
254 0
255};
256EXPORT_SYMBOL(cx23885_user_ctrls);
257
258static const u32 *ctrl_classes[] = {
259 cx23885_user_ctrls,
260 NULL
261};
262
263void cx23885_video_wakeup(struct cx23885_dev *dev,
264 struct cx23885_dmaqueue *q, u32 count)
265{
266 struct cx23885_buffer *buf;
267 int bc;
268
269 for (bc = 0;; bc++) {
270 if (list_empty(&q->active))
271 break;
272 buf = list_entry(q->active.next,
273 struct cx23885_buffer, vb.queue);
274
275 /* count comes from the hw and is is 16bit wide --
276 * this trick handles wrap-arounds correctly for
277 * up to 32767 buffers in flight... */
278 if ((s16) (count - buf->count) < 0)
279 break;
280
281 do_gettimeofday(&buf->vb.ts);
282 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
283 count, buf->count);
284 buf->vb.state = VIDEOBUF_DONE;
285 list_del(&buf->vb.queue);
286 wake_up(&buf->vb.done);
287 }
288 if (list_empty(&q->active)) {
289 del_timer(&q->timeout);
290 } else {
291 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
292 }
293 if (bc != 1)
294 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
295 __FUNCTION__, bc);
296}
297
298int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
299{
300 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
301 __FUNCTION__,
302 (unsigned int)norm,
303 v4l2_norm_to_name(norm));
304
305 dev->tvnorm = norm;
306
307 /* Tell the analog tuner/demods */
308 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_STD, &norm);
309
310 /* Tell the internal A/V decoder */
311 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_STD, &norm);
312
313 return 0;
314}
315
316struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
317 struct pci_dev *pci,
318 struct video_device *template,
319 char *type)
320{
321 struct video_device *vfd;
322 dprintk(1, "%s()\n", __FUNCTION__);
323
324 vfd = video_device_alloc();
325 if (NULL == vfd)
326 return NULL;
327 *vfd = *template;
328 vfd->minor = -1;
329 vfd->dev = &pci->dev;
330 vfd->release = video_device_release;
331 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
332 dev->name, type, cx23885_boards[dev->board].name);
333 return vfd;
334}
335
336int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
337{
338 int i;
339
340 if (qctrl->id < V4L2_CID_BASE ||
341 qctrl->id >= V4L2_CID_LASTP1)
342 return -EINVAL;
343 for (i = 0; i < CX23885_CTLS; i++)
344 if (cx23885_ctls[i].v.id == qctrl->id)
345 break;
346 if (i == CX23885_CTLS) {
347 *qctrl = no_ctl;
348 return 0;
349 }
350 *qctrl = cx23885_ctls[i].v;
351 return 0;
352}
353EXPORT_SYMBOL(cx23885_ctrl_query);
354
355/* ------------------------------------------------------------------- */
356/* resource management */
357
358static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
359 unsigned int bit)
360{
361 dprintk(1, "%s()\n", __FUNCTION__);
362 if (fh->resources & bit)
363 /* have it already allocated */
364 return 1;
365
366 /* is it free? */
367 mutex_lock(&dev->lock);
368 if (dev->resources & bit) {
369 /* no, someone else uses it */
370 mutex_unlock(&dev->lock);
371 return 0;
372 }
373 /* it's free, grab it */
374 fh->resources |= bit;
375 dev->resources |= bit;
376 dprintk(1, "res: get %d\n", bit);
377 mutex_unlock(&dev->lock);
378 return 1;
379}
380
381static int res_check(struct cx23885_fh *fh, unsigned int bit)
382{
383 return (fh->resources & bit);
384}
385
386static int res_locked(struct cx23885_dev *dev, unsigned int bit)
387{
388 return (dev->resources & bit);
389}
390
391static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
392 unsigned int bits)
393{
394 BUG_ON((fh->resources & bits) != bits);
395 dprintk(1, "%s()\n", __FUNCTION__);
396
397 mutex_lock(&dev->lock);
398 fh->resources &= ~bits;
399 dev->resources &= ~bits;
400 dprintk(1, "res: put %d\n", bits);
401 mutex_unlock(&dev->lock);
402}
403
404int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
405{
406 struct v4l2_routing route;
407 memset(&route, 0, sizeof(route));
408
409 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
410 __FUNCTION__,
411 input, INPUT(input)->vmux,
412 INPUT(input)->gpio0, INPUT(input)->gpio1,
413 INPUT(input)->gpio2, INPUT(input)->gpio3);
414 dev->input = input;
415
416 route.input = INPUT(input)->vmux;
417
418 /* Tell the internal A/V decoder */
419 cx23885_call_i2c_clients(&dev->i2c_bus[2],
420 VIDIOC_INT_S_VIDEO_ROUTING, &route);
421
422 return 0;
423}
424EXPORT_SYMBOL(cx23885_video_mux);
425
426/* ------------------------------------------------------------------ */
427int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
428 unsigned int height, enum v4l2_field field)
429{
430 dprintk(1, "%s()\n", __FUNCTION__);
431 return 0;
432}
433
434static int cx23885_start_video_dma(struct cx23885_dev *dev,
435 struct cx23885_dmaqueue *q,
436 struct cx23885_buffer *buf)
437{
438 dprintk(1, "%s()\n", __FUNCTION__);
439
440 /* setup fifo + format */
441 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
442 buf->bpl, buf->risc.dma);
443 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
444
445 /* reset counter */
446 cx_write(VID_A_GPCNT_CTL, 3);
447 q->count = 1;
448
449 /* enable irq */
450 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
451 cx_set(VID_A_INT_MSK, 0x000011);
452
453 /* start dma */
454 cx_set(DEV_CNTRL2, (1<<5));
455 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
456
457 return 0;
458}
459
460
461static int cx23885_restart_video_queue(struct cx23885_dev *dev,
462 struct cx23885_dmaqueue *q)
463{
464 struct cx23885_buffer *buf, *prev;
465 struct list_head *item;
466 dprintk(1, "%s()\n", __FUNCTION__);
467
468 if (!list_empty(&q->active)) {
469 buf = list_entry(q->active.next, struct cx23885_buffer,
470 vb.queue);
471 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
472 buf, buf->vb.i);
473 cx23885_start_video_dma(dev, q, buf);
474 list_for_each(item, &q->active) {
475 buf = list_entry(item, struct cx23885_buffer,
476 vb.queue);
477 buf->count = q->count++;
478 }
479 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
480 return 0;
481 }
482
483 prev = NULL;
484 for (;;) {
485 if (list_empty(&q->queued))
486 return 0;
487 buf = list_entry(q->queued.next, struct cx23885_buffer,
488 vb.queue);
489 if (NULL == prev) {
490 list_move_tail(&buf->vb.queue, &q->active);
491 cx23885_start_video_dma(dev, q, buf);
492 buf->vb.state = VIDEOBUF_ACTIVE;
493 buf->count = q->count++;
494 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
495 dprintk(2, "[%p/%d] restart_queue - first active\n",
496 buf, buf->vb.i);
497
498 } else if (prev->vb.width == buf->vb.width &&
499 prev->vb.height == buf->vb.height &&
500 prev->fmt == buf->fmt) {
501 list_move_tail(&buf->vb.queue, &q->active);
502 buf->vb.state = VIDEOBUF_ACTIVE;
503 buf->count = q->count++;
504 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
505 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
506 dprintk(2, "[%p/%d] restart_queue - move to active\n",
507 buf, buf->vb.i);
508 } else {
509 return 0;
510 }
511 prev = buf;
512 }
513}
514
515static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
516 unsigned int *size)
517{
518 struct cx23885_fh *fh = q->priv_data;
519
520 *size = fh->fmt->depth*fh->width*fh->height >> 3;
521 if (0 == *count)
522 *count = 32;
523 while (*size * *count > vid_limit * 1024 * 1024)
524 (*count)--;
525 return 0;
526}
527
528static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
529 enum v4l2_field field)
530{
531 struct cx23885_fh *fh = q->priv_data;
532 struct cx23885_dev *dev = fh->dev;
533 struct cx23885_buffer *buf =
534 container_of(vb, struct cx23885_buffer, vb);
535 int rc, init_buffer = 0;
536 u32 line0_offset, line1_offset;
537 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
538
539 BUG_ON(NULL == fh->fmt);
540 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
541 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
542 return -EINVAL;
543 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
544 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
545 return -EINVAL;
546
547 if (buf->fmt != fh->fmt ||
548 buf->vb.width != fh->width ||
549 buf->vb.height != fh->height ||
550 buf->vb.field != field) {
551 buf->fmt = fh->fmt;
552 buf->vb.width = fh->width;
553 buf->vb.height = fh->height;
554 buf->vb.field = field;
555 init_buffer = 1;
556 }
557
558 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
559 init_buffer = 1;
560 rc = videobuf_iolock(q, &buf->vb, NULL);
561 if (0 != rc)
562 goto fail;
563 }
564
565 if (init_buffer) {
566 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
567 switch (buf->vb.field) {
568 case V4L2_FIELD_TOP:
569 cx23885_risc_buffer(dev->pci, &buf->risc,
570 dma->sglist, 0, UNSET,
571 buf->bpl, 0, buf->vb.height);
572 break;
573 case V4L2_FIELD_BOTTOM:
574 cx23885_risc_buffer(dev->pci, &buf->risc,
575 dma->sglist, UNSET, 0,
576 buf->bpl, 0, buf->vb.height);
577 break;
578 case V4L2_FIELD_INTERLACED:
579 if (dev->tvnorm & V4L2_STD_NTSC) {
580 /* cx25840 transmits NTSC bottom field first */
581 dprintk(1, "%s() Creating NTSC risc\n",
582 __FUNCTION__);
583 line0_offset = buf->bpl;
584 line1_offset = 0;
585 } else {
586 /* All other formats are top field first */
587 dprintk(1, "%s() Creating PAL/SECAM risc\n",
588 __FUNCTION__);
589 line0_offset = 0;
590 line1_offset = buf->bpl;
591 }
592 cx23885_risc_buffer(dev->pci, &buf->risc,
593 dma->sglist, line0_offset,
594 line1_offset,
595 buf->bpl, buf->bpl,
596 buf->vb.height >> 1);
597 break;
598 case V4L2_FIELD_SEQ_TB:
599 cx23885_risc_buffer(dev->pci, &buf->risc,
600 dma->sglist,
601 0, buf->bpl * (buf->vb.height >> 1),
602 buf->bpl, 0,
603 buf->vb.height >> 1);
604 break;
605 case V4L2_FIELD_SEQ_BT:
606 cx23885_risc_buffer(dev->pci, &buf->risc,
607 dma->sglist,
608 buf->bpl * (buf->vb.height >> 1), 0,
609 buf->bpl, 0,
610 buf->vb.height >> 1);
611 break;
612 default:
613 BUG();
614 }
615 }
616 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
617 buf, buf->vb.i,
618 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
619 (unsigned long)buf->risc.dma);
620
621 buf->vb.state = VIDEOBUF_PREPARED;
622 return 0;
623
624 fail:
625 cx23885_free_buffer(q, buf);
626 return rc;
627}
628
629static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
630{
631 struct cx23885_buffer *buf = container_of(vb,
632 struct cx23885_buffer, vb);
633 struct cx23885_buffer *prev;
634 struct cx23885_fh *fh = vq->priv_data;
635 struct cx23885_dev *dev = fh->dev;
636 struct cx23885_dmaqueue *q = &dev->vidq;
637
638 /* add jump to stopper */
639 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
640 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
641 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
642
643 if (!list_empty(&q->queued)) {
644 list_add_tail(&buf->vb.queue, &q->queued);
645 buf->vb.state = VIDEOBUF_QUEUED;
646 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
647 buf, buf->vb.i);
648
649 } else if (list_empty(&q->active)) {
650 list_add_tail(&buf->vb.queue, &q->active);
651 cx23885_start_video_dma(dev, q, buf);
652 buf->vb.state = VIDEOBUF_ACTIVE;
653 buf->count = q->count++;
654 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
655 dprintk(2, "[%p/%d] buffer_queue - first active\n",
656 buf, buf->vb.i);
657
658 } else {
659 prev = list_entry(q->active.prev, struct cx23885_buffer,
660 vb.queue);
661 if (prev->vb.width == buf->vb.width &&
662 prev->vb.height == buf->vb.height &&
663 prev->fmt == buf->fmt) {
664 list_add_tail(&buf->vb.queue, &q->active);
665 buf->vb.state = VIDEOBUF_ACTIVE;
666 buf->count = q->count++;
667 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
668 /* 64 bit bits 63-32 */
669 prev->risc.jmp[2] = cpu_to_le32(0);
670 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
671 buf, buf->vb.i);
672
673 } else {
674 list_add_tail(&buf->vb.queue, &q->queued);
675 buf->vb.state = VIDEOBUF_QUEUED;
676 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
677 buf, buf->vb.i);
678 }
679 }
680}
681
682static void buffer_release(struct videobuf_queue *q,
683 struct videobuf_buffer *vb)
684{
685 struct cx23885_buffer *buf = container_of(vb,
686 struct cx23885_buffer, vb);
687
688 cx23885_free_buffer(q, buf);
689}
690
691static struct videobuf_queue_ops cx23885_video_qops = {
692 .buf_setup = buffer_setup,
693 .buf_prepare = buffer_prepare,
694 .buf_queue = buffer_queue,
695 .buf_release = buffer_release,
696};
697
698static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
699{
700 switch (fh->type) {
701 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
702 return &fh->vidq;
703 case V4L2_BUF_TYPE_VBI_CAPTURE:
704 return &fh->vbiq;
705 default:
706 BUG();
707 return NULL;
708 }
709}
710
711static int get_resource(struct cx23885_fh *fh)
712{
713 switch (fh->type) {
714 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
715 return RESOURCE_VIDEO;
716 case V4L2_BUF_TYPE_VBI_CAPTURE:
717 return RESOURCE_VBI;
718 default:
719 BUG();
720 return 0;
721 }
722}
723
724static int video_open(struct inode *inode, struct file *file)
725{
726 int minor = iminor(inode);
727 struct cx23885_dev *h, *dev = NULL;
728 struct cx23885_fh *fh;
729 struct list_head *list;
730 enum v4l2_buf_type type = 0;
731 int radio = 0;
732
733 list_for_each(list, &cx23885_devlist) {
734 h = list_entry(list, struct cx23885_dev, devlist);
735 if (h->video_dev->minor == minor) {
736 dev = h;
737 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
738 }
739 if (h->vbi_dev &&
740 h->vbi_dev->minor == minor) {
741 dev = h;
742 type = V4L2_BUF_TYPE_VBI_CAPTURE;
743 }
744 if (h->radio_dev &&
745 h->radio_dev->minor == minor) {
746 radio = 1;
747 dev = h;
748 }
749 }
750 if (NULL == dev)
751 return -ENODEV;
752
753 dprintk(1, "open minor=%d radio=%d type=%s\n",
754 minor, radio, v4l2_type_names[type]);
755
756 /* allocate + initialize per filehandle data */
757 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
758 if (NULL == fh)
759 return -ENOMEM;
760 file->private_data = fh;
761 fh->dev = dev;
762 fh->radio = radio;
763 fh->type = type;
764 fh->width = 320;
765 fh->height = 240;
766 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
767
768 videobuf_queue_pci_init(&fh->vidq, &cx23885_video_qops,
769 dev->pci, &dev->slock,
770 V4L2_BUF_TYPE_VIDEO_CAPTURE,
771 V4L2_FIELD_INTERLACED,
772 sizeof(struct cx23885_buffer),
773 fh);
774
775 dprintk(1, "post videobuf_queue_init()\n");
776
777
778 return 0;
779}
780
781static ssize_t video_read(struct file *file, char __user *data,
782 size_t count, loff_t *ppos)
783{
784 struct cx23885_fh *fh = file->private_data;
785
786 switch (fh->type) {
787 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
788 if (res_locked(fh->dev, RESOURCE_VIDEO))
789 return -EBUSY;
790 return videobuf_read_one(&fh->vidq, data, count, ppos,
791 file->f_flags & O_NONBLOCK);
792 case V4L2_BUF_TYPE_VBI_CAPTURE:
793 if (!res_get(fh->dev, fh, RESOURCE_VBI))
794 return -EBUSY;
795 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
796 file->f_flags & O_NONBLOCK);
797 default:
798 BUG();
799 return 0;
800 }
801}
802
803static unsigned int video_poll(struct file *file,
804 struct poll_table_struct *wait)
805{
806 struct cx23885_fh *fh = file->private_data;
807 struct cx23885_buffer *buf;
808
809 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
810 if (!res_get(fh->dev, fh, RESOURCE_VBI))
811 return POLLERR;
812 return videobuf_poll_stream(file, &fh->vbiq, wait);
813 }
814
815 if (res_check(fh, RESOURCE_VIDEO)) {
816 /* streaming capture */
817 if (list_empty(&fh->vidq.stream))
818 return POLLERR;
819 buf = list_entry(fh->vidq.stream.next,
820 struct cx23885_buffer, vb.stream);
821 } else {
822 /* read() capture */
823 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
824 if (NULL == buf)
825 return POLLERR;
826 }
827 poll_wait(file, &buf->vb.done, wait);
828 if (buf->vb.state == VIDEOBUF_DONE ||
829 buf->vb.state == VIDEOBUF_ERROR)
830 return POLLIN|POLLRDNORM;
831 return 0;
832}
833
834static int video_release(struct inode *inode, struct file *file)
835{
836 struct cx23885_fh *fh = file->private_data;
837 struct cx23885_dev *dev = fh->dev;
838
839 /* turn off overlay */
840 if (res_check(fh, RESOURCE_OVERLAY)) {
841 /* FIXME */
842 res_free(dev, fh, RESOURCE_OVERLAY);
843 }
844
845 /* stop video capture */
846 if (res_check(fh, RESOURCE_VIDEO)) {
847 videobuf_queue_cancel(&fh->vidq);
848 res_free(dev, fh, RESOURCE_VIDEO);
849 }
850 if (fh->vidq.read_buf) {
851 buffer_release(&fh->vidq, fh->vidq.read_buf);
852 kfree(fh->vidq.read_buf);
853 }
854
855 /* stop vbi capture */
856 if (res_check(fh, RESOURCE_VBI)) {
857 if (fh->vbiq.streaming)
858 videobuf_streamoff(&fh->vbiq);
859 if (fh->vbiq.reading)
860 videobuf_read_stop(&fh->vbiq);
861 res_free(dev, fh, RESOURCE_VBI);
862 }
863
864 videobuf_mmap_free(&fh->vidq);
865 file->private_data = NULL;
866 kfree(fh);
867
868 /* We are not putting the tuner to sleep here on exit, because
869 * we want to use the mpeg encoder in another session to capture
870 * tuner video. Closing this will result in no video to the encoder.
871 */
872
873 return 0;
874}
875
876static int video_mmap(struct file *file, struct vm_area_struct *vma)
877{
878 struct cx23885_fh *fh = file->private_data;
879
880 return videobuf_mmap_mapper(get_queue(fh), vma);
881}
882
883/* ------------------------------------------------------------------ */
884/* VIDEO CTRL IOCTLS */
885
886int cx23885_get_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
887{
888 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __FUNCTION__);
889 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_G_CTRL, ctl);
890 return 0;
891}
892EXPORT_SYMBOL(cx23885_get_control);
893
894int cx23885_set_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
895{
896 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
897 " (disabled - no action)\n", __FUNCTION__);
898 return 0;
899}
900EXPORT_SYMBOL(cx23885_set_control);
901
902static void init_controls(struct cx23885_dev *dev)
903{
904 struct v4l2_control ctrl;
905 int i;
906
907 for (i = 0; i < CX23885_CTLS; i++) {
908 ctrl.id = cx23885_ctls[i].v.id;
909 ctrl.value = cx23885_ctls[i].v.default_value;
910
911 cx23885_set_control(dev, &ctrl);
912 }
913}
914
915/* ------------------------------------------------------------------ */
916/* VIDEO IOCTLS */
917
918static int vidioc_g_fmt_cap(struct file *file, void *priv,
919 struct v4l2_format *f)
920{
921 struct cx23885_fh *fh = priv;
922
923 f->fmt.pix.width = fh->width;
924 f->fmt.pix.height = fh->height;
925 f->fmt.pix.field = fh->vidq.field;
926 f->fmt.pix.pixelformat = fh->fmt->fourcc;
927 f->fmt.pix.bytesperline =
928 (f->fmt.pix.width * fh->fmt->depth) >> 3;
929 f->fmt.pix.sizeimage =
930 f->fmt.pix.height * f->fmt.pix.bytesperline;
931
932 return 0;
933}
934
935static int vidioc_try_fmt_cap(struct file *file, void *priv,
936 struct v4l2_format *f)
937{
938 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
939 struct cx23885_fmt *fmt;
940 enum v4l2_field field;
941 unsigned int maxw, maxh;
942
943 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
944 if (NULL == fmt)
945 return -EINVAL;
946
947 field = f->fmt.pix.field;
948 maxw = norm_maxw(dev->tvnorm);
949 maxh = norm_maxh(dev->tvnorm);
950
951 if (V4L2_FIELD_ANY == field) {
952 field = (f->fmt.pix.height > maxh/2)
953 ? V4L2_FIELD_INTERLACED
954 : V4L2_FIELD_BOTTOM;
955 }
956
957 switch (field) {
958 case V4L2_FIELD_TOP:
959 case V4L2_FIELD_BOTTOM:
960 maxh = maxh / 2;
961 break;
962 case V4L2_FIELD_INTERLACED:
963 break;
964 default:
965 return -EINVAL;
966 }
967
968 f->fmt.pix.field = field;
969 if (f->fmt.pix.height < 32)
970 f->fmt.pix.height = 32;
971 if (f->fmt.pix.height > maxh)
972 f->fmt.pix.height = maxh;
973 if (f->fmt.pix.width < 48)
974 f->fmt.pix.width = 48;
975 if (f->fmt.pix.width > maxw)
976 f->fmt.pix.width = maxw;
977 f->fmt.pix.width &= ~0x03;
978 f->fmt.pix.bytesperline =
979 (f->fmt.pix.width * fmt->depth) >> 3;
980 f->fmt.pix.sizeimage =
981 f->fmt.pix.height * f->fmt.pix.bytesperline;
982
983 return 0;
984}
985
986static int vidioc_s_fmt_cap(struct file *file, void *priv,
987 struct v4l2_format *f)
988{
989 struct cx23885_fh *fh = priv;
990 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
991 int err;
992
993 dprintk(2, "%s()\n", __FUNCTION__);
994 err = vidioc_try_fmt_cap(file, priv, f);
995
996 if (0 != err)
997 return err;
998 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
999 fh->width = f->fmt.pix.width;
1000 fh->height = f->fmt.pix.height;
1001 fh->vidq.field = f->fmt.pix.field;
1002 dprintk(2, "%s() width=%d height=%d field=%d\n", __FUNCTION__,
1003 fh->width, fh->height, fh->vidq.field);
1004 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
1005 return 0;
1006}
1007
1008static int vidioc_querycap(struct file *file, void *priv,
1009 struct v4l2_capability *cap)
1010{
1011 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1012
1013 strcpy(cap->driver, "cx23885");
1014 strlcpy(cap->card, cx23885_boards[dev->board].name,
1015 sizeof(cap->card));
1016 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1017 cap->version = CX23885_VERSION_CODE;
1018 cap->capabilities =
1019 V4L2_CAP_VIDEO_CAPTURE |
1020 V4L2_CAP_READWRITE |
1021 V4L2_CAP_STREAMING |
1022 V4L2_CAP_VBI_CAPTURE;
1023 if (UNSET != dev->tuner_type)
1024 cap->capabilities |= V4L2_CAP_TUNER;
1025 return 0;
1026}
1027
1028static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1029 struct v4l2_fmtdesc *f)
1030{
1031 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1032 return -EINVAL;
1033
1034 strlcpy(f->description, formats[f->index].name,
1035 sizeof(f->description));
1036 f->pixelformat = formats[f->index].fourcc;
1037
1038 return 0;
1039}
1040
1041#ifdef CONFIG_VIDEO_V4L1_COMPAT
1042static int vidiocgmbuf(struct file *file, void *priv,
1043 struct video_mbuf *mbuf)
1044{
1045 struct cx23885_fh *fh = priv;
1046 struct videobuf_queue *q;
1047 struct v4l2_requestbuffers req;
1048 unsigned int i;
1049 int err;
1050
1051 q = get_queue(fh);
1052 memset(&req, 0, sizeof(req));
1053 req.type = q->type;
1054 req.count = 8;
1055 req.memory = V4L2_MEMORY_MMAP;
1056 err = videobuf_reqbufs(q, &req);
1057 if (err < 0)
1058 return err;
1059
1060 mbuf->frames = req.count;
1061 mbuf->size = 0;
1062 for (i = 0; i < mbuf->frames; i++) {
1063 mbuf->offsets[i] = q->bufs[i]->boff;
1064 mbuf->size += q->bufs[i]->bsize;
1065 }
1066 return 0;
1067}
1068#endif
1069
1070static int vidioc_reqbufs(struct file *file, void *priv,
1071 struct v4l2_requestbuffers *p)
1072{
1073 struct cx23885_fh *fh = priv;
1074 return (videobuf_reqbufs(get_queue(fh), p));
1075}
1076
1077static int vidioc_querybuf(struct file *file, void *priv,
1078 struct v4l2_buffer *p)
1079{
1080 struct cx23885_fh *fh = priv;
1081 return (videobuf_querybuf(get_queue(fh), p));
1082}
1083
1084static int vidioc_qbuf(struct file *file, void *priv,
1085 struct v4l2_buffer *p)
1086{
1087 struct cx23885_fh *fh = priv;
1088 return (videobuf_qbuf(get_queue(fh), p));
1089}
1090
1091static int vidioc_dqbuf(struct file *file, void *priv,
1092 struct v4l2_buffer *p)
1093{
1094 struct cx23885_fh *fh = priv;
1095 return (videobuf_dqbuf(get_queue(fh), p,
1096 file->f_flags & O_NONBLOCK));
1097}
1098
1099static int vidioc_streamon(struct file *file, void *priv,
1100 enum v4l2_buf_type i)
1101{
1102 struct cx23885_fh *fh = priv;
1103 struct cx23885_dev *dev = fh->dev;
1104 dprintk(1, "%s()\n", __FUNCTION__);
1105
1106 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1107 return -EINVAL;
1108 if (unlikely(i != fh->type))
1109 return -EINVAL;
1110
1111 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1112 return -EBUSY;
1113 return videobuf_streamon(get_queue(fh));
1114}
1115
1116static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1117{
1118 struct cx23885_fh *fh = priv;
1119 struct cx23885_dev *dev = fh->dev;
1120 int err, res;
1121 dprintk(1, "%s()\n", __FUNCTION__);
1122
1123 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1124 return -EINVAL;
1125 if (i != fh->type)
1126 return -EINVAL;
1127
1128 res = get_resource(fh);
1129 err = videobuf_streamoff(get_queue(fh));
1130 if (err < 0)
1131 return err;
1132 res_free(dev, fh, res);
1133 return 0;
1134}
1135
1136static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1137{
1138 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1139 dprintk(1, "%s()\n", __FUNCTION__);
1140
1141 mutex_lock(&dev->lock);
1142 cx23885_set_tvnorm(dev, *tvnorms);
1143 mutex_unlock(&dev->lock);
1144
1145 return 0;
1146}
1147
1148int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1149{
1150 static const char *iname[] = {
1151 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1152 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1153 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1154 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1155 [CX23885_VMUX_SVIDEO] = "S-Video",
1156 [CX23885_VMUX_TELEVISION] = "Television",
1157 [CX23885_VMUX_CABLE] = "Cable TV",
1158 [CX23885_VMUX_DVB] = "DVB",
1159 [CX23885_VMUX_DEBUG] = "for debug only",
1160 };
1161 unsigned int n;
1162 dprintk(1, "%s()\n", __FUNCTION__);
1163
1164 n = i->index;
1165 if (n >= 4)
1166 return -EINVAL;
1167
1168 if (0 == INPUT(n)->type)
1169 return -EINVAL;
1170
1171 memset(i, 0, sizeof(*i));
1172 i->index = n;
1173 i->type = V4L2_INPUT_TYPE_CAMERA;
1174 strcpy(i->name, iname[INPUT(n)->type]);
1175 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1176 (CX23885_VMUX_CABLE == INPUT(n)->type))
1177 i->type = V4L2_INPUT_TYPE_TUNER;
1178 i->std = CX23885_NORMS;
1179 return 0;
1180}
1181EXPORT_SYMBOL(cx23885_enum_input);
1182
1183static int vidioc_enum_input(struct file *file, void *priv,
1184 struct v4l2_input *i)
1185{
1186 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1187 dprintk(1, "%s()\n", __FUNCTION__);
1188 return cx23885_enum_input(dev, i);
1189}
1190
1191static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1192{
1193 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1194
1195 *i = dev->input;
1196 dprintk(1, "%s() returns %d\n", __FUNCTION__, *i);
1197 return 0;
1198}
1199
1200static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1201{
1202 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1203
1204 dprintk(1, "%s(%d)\n", __FUNCTION__, i);
1205
1206 if (i >= 4) {
1207 dprintk(1, "%s() -EINVAL\n", __FUNCTION__);
1208 return -EINVAL;
1209 }
1210
1211 mutex_lock(&dev->lock);
1212 cx23885_video_mux(dev, i);
1213 mutex_unlock(&dev->lock);
1214 return 0;
1215}
1216
1217static int vidioc_queryctrl(struct file *file, void *priv,
1218 struct v4l2_queryctrl *qctrl)
1219{
1220 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1221 if (unlikely(qctrl->id == 0))
1222 return -EINVAL;
1223 return cx23885_ctrl_query(qctrl);
1224}
1225
1226static int vidioc_g_ctrl(struct file *file, void *priv,
1227 struct v4l2_control *ctl)
1228{
1229 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1230
1231 return cx23885_get_control(dev, ctl);
1232}
1233
1234static int vidioc_s_ctrl(struct file *file, void *priv,
1235 struct v4l2_control *ctl)
1236{
1237 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1238
1239 return cx23885_set_control(dev, ctl);
1240}
1241
1242static int vidioc_g_tuner(struct file *file, void *priv,
1243 struct v4l2_tuner *t)
1244{
1245 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1246
1247 if (unlikely(UNSET == dev->tuner_type))
1248 return -EINVAL;
1249 if (0 != t->index)
1250 return -EINVAL;
1251
1252 strcpy(t->name, "Television");
1253 t->type = V4L2_TUNER_ANALOG_TV;
1254 t->capability = V4L2_TUNER_CAP_NORM;
1255 t->rangehigh = 0xffffffffUL;
1256 t->signal = 0xffff ; /* LOCKED */
1257 return 0;
1258}
1259
1260static int vidioc_s_tuner(struct file *file, void *priv,
1261 struct v4l2_tuner *t)
1262{
1263 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1264
1265 if (UNSET == dev->tuner_type)
1266 return -EINVAL;
1267 if (0 != t->index)
1268 return -EINVAL;
1269 return 0;
1270}
1271
1272static int vidioc_g_frequency(struct file *file, void *priv,
1273 struct v4l2_frequency *f)
1274{
1275 struct cx23885_fh *fh = priv;
1276 struct cx23885_dev *dev = fh->dev;
1277
1278 if (unlikely(UNSET == dev->tuner_type))
1279 return -EINVAL;
1280
1281 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1282 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1283 f->frequency = dev->freq;
1284
1285 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1286
1287 return 0;
1288}
1289
1290int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1291{
1292 if (unlikely(UNSET == dev->tuner_type))
1293 return -EINVAL;
1294 if (unlikely(f->tuner != 0))
1295 return -EINVAL;
1296
1297 mutex_lock(&dev->lock);
1298 dev->freq = f->frequency;
1299
1300 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
1301
1302 /* When changing channels it is required to reset TVAUDIO */
1303 msleep(10);
1304
1305 mutex_unlock(&dev->lock);
1306
1307 return 0;
1308}
1309EXPORT_SYMBOL(cx23885_set_freq);
1310
1311static int vidioc_s_frequency(struct file *file, void *priv,
1312 struct v4l2_frequency *f)
1313{
1314 struct cx23885_fh *fh = priv;
1315 struct cx23885_dev *dev = fh->dev;
1316
1317 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1318 return -EINVAL;
1319 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1320 return -EINVAL;
1321
1322 return
1323 cx23885_set_freq(dev, f);
1324}
1325
1326#ifdef CONFIG_VIDEO_ADV_DEBUG
1327static int vidioc_g_register(struct file *file, void *fh,
1328 struct v4l2_register *reg)
1329{
1330 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1331
1332 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1333 return -EINVAL;
1334
1335 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
1336
1337 return 0;
1338}
1339
1340static int vidioc_s_register(struct file *file, void *fh,
1341 struct v4l2_register *reg)
1342{
1343 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1344
1345 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1346 return -EINVAL;
1347
1348 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
1349
1350 return 0;
1351}
1352#endif
1353
1354/* ----------------------------------------------------------- */
1355
1356static void cx23885_vid_timeout(unsigned long data)
1357{
1358 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1359 struct cx23885_dmaqueue *q = &dev->vidq;
1360 struct cx23885_buffer *buf;
1361 unsigned long flags;
1362
1363 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1364
1365 cx_clear(VID_A_DMA_CTL, 0x11);
1366
1367 spin_lock_irqsave(&dev->slock, flags);
1368 while (!list_empty(&q->active)) {
1369 buf = list_entry(q->active.next,
1370 struct cx23885_buffer, vb.queue);
1371 list_del(&buf->vb.queue);
1372 buf->vb.state = VIDEOBUF_ERROR;
1373 wake_up(&buf->vb.done);
1374 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1375 dev->name, buf, buf->vb.i,
1376 (unsigned long)buf->risc.dma);
1377 }
1378 cx23885_restart_video_queue(dev, q);
1379 spin_unlock_irqrestore(&dev->slock, flags);
1380}
1381
1382int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1383{
1384 u32 mask, count;
1385 int handled = 0;
1386
1387 mask = cx_read(VID_A_INT_MSK);
1388 if (0 == (status & mask))
1389 return handled;
1390 cx_write(VID_A_INT_STAT, status);
1391
1392 dprintk(2, "%s() status = 0x%08x\n", __FUNCTION__, status);
1393 /* risc op code error */
1394 if (status & (1 << 16)) {
1395 printk(KERN_WARNING "%s/0: video risc op code error\n",
1396 dev->name);
1397 cx_clear(VID_A_DMA_CTL, 0x11);
1398 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1399 }
1400
1401 /* risc1 y */
1402 if (status & 0x01) {
1403 spin_lock(&dev->slock);
1404 count = cx_read(VID_A_GPCNT);
1405 cx23885_video_wakeup(dev, &dev->vidq, count);
1406 spin_unlock(&dev->slock);
1407 handled++;
1408 }
1409 /* risc2 y */
1410 if (status & 0x10) {
1411 dprintk(2, "stopper video\n");
1412 spin_lock(&dev->slock);
1413 cx23885_restart_video_queue(dev, &dev->vidq);
1414 spin_unlock(&dev->slock);
1415 handled++;
1416 }
1417
1418 return handled;
1419}
1420
1421/* ----------------------------------------------------------- */
1422/* exported stuff */
1423
1424static const struct file_operations video_fops = {
1425 .owner = THIS_MODULE,
1426 .open = video_open,
1427 .release = video_release,
1428 .read = video_read,
1429 .poll = video_poll,
1430 .mmap = video_mmap,
1431 .ioctl = video_ioctl2,
1432 .compat_ioctl = v4l_compat_ioctl32,
1433 .llseek = no_llseek,
1434};
1435
1436static struct video_device cx23885_vbi_template;
1437static struct video_device cx23885_video_template = {
1438 .name = "cx23885-video",
1439 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1440 .fops = &video_fops,
1441 .minor = -1,
1442 .vidioc_querycap = vidioc_querycap,
1443 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1444 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1445 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1446 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1447 .vidioc_g_fmt_vbi = cx23885_vbi_fmt,
1448 .vidioc_try_fmt_vbi = cx23885_vbi_fmt,
1449 .vidioc_s_fmt_vbi = cx23885_vbi_fmt,
1450 .vidioc_reqbufs = vidioc_reqbufs,
1451 .vidioc_querybuf = vidioc_querybuf,
1452 .vidioc_qbuf = vidioc_qbuf,
1453 .vidioc_dqbuf = vidioc_dqbuf,
1454 .vidioc_s_std = vidioc_s_std,
1455 .vidioc_enum_input = vidioc_enum_input,
1456 .vidioc_g_input = vidioc_g_input,
1457 .vidioc_s_input = vidioc_s_input,
1458 .vidioc_queryctrl = vidioc_queryctrl,
1459 .vidioc_g_ctrl = vidioc_g_ctrl,
1460 .vidioc_s_ctrl = vidioc_s_ctrl,
1461 .vidioc_streamon = vidioc_streamon,
1462 .vidioc_streamoff = vidioc_streamoff,
1463#ifdef CONFIG_VIDEO_V4L1_COMPAT
1464 .vidiocgmbuf = vidiocgmbuf,
1465#endif
1466 .vidioc_g_tuner = vidioc_g_tuner,
1467 .vidioc_s_tuner = vidioc_s_tuner,
1468 .vidioc_g_frequency = vidioc_g_frequency,
1469 .vidioc_s_frequency = vidioc_s_frequency,
1470#ifdef CONFIG_VIDEO_ADV_DEBUG
1471 .vidioc_g_register = vidioc_g_register,
1472 .vidioc_s_register = vidioc_s_register,
1473#endif
1474 .tvnorms = CX23885_NORMS,
1475 .current_norm = V4L2_STD_NTSC_M,
1476};
1477
1478static const struct file_operations radio_fops = {
1479 .owner = THIS_MODULE,
1480 .open = video_open,
1481 .release = video_release,
1482 .ioctl = video_ioctl2,
1483 .compat_ioctl = v4l_compat_ioctl32,
1484 .llseek = no_llseek,
1485};
1486
1487
1488void cx23885_video_unregister(struct cx23885_dev *dev)
1489{
1490 dprintk(1, "%s()\n", __FUNCTION__);
1491 cx_clear(PCI_INT_MSK, 1);
1492
1493 if (dev->video_dev) {
1494 if (-1 != dev->video_dev->minor)
1495 video_unregister_device(dev->video_dev);
1496 else
1497 video_device_release(dev->video_dev);
1498 dev->video_dev = NULL;
1499
1500 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1501 }
1502}
1503
1504int cx23885_video_register(struct cx23885_dev *dev)
1505{
1506 int err;
1507
1508 dprintk(1, "%s()\n", __FUNCTION__);
1509 spin_lock_init(&dev->slock);
1510
1511 /* Initialize VBI template */
1512 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1513 sizeof(cx23885_vbi_template));
1514 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1515 cx23885_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1516
1517 dev->tvnorm = cx23885_video_template.current_norm;
1518
1519 /* init video dma queues */
1520 INIT_LIST_HEAD(&dev->vidq.active);
1521 INIT_LIST_HEAD(&dev->vidq.queued);
1522 dev->vidq.timeout.function = cx23885_vid_timeout;
1523 dev->vidq.timeout.data = (unsigned long)dev;
1524 init_timer(&dev->vidq.timeout);
1525 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1526 VID_A_DMA_CTL, 0x11, 0x00);
1527
1528 /* Don't enable VBI yet */
1529 cx_set(PCI_INT_MSK, 1);
1530
1531
1532 /* register v4l devices */
1533 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1534 &cx23885_video_template, "video");
1535 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1536 video_nr[dev->nr]);
1537 if (err < 0) {
1538 printk(KERN_INFO "%s: can't register video device\n",
1539 dev->name);
1540 goto fail_unreg;
1541 }
1542 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1543 dev->name, dev->video_dev->minor & 0x1f);
1544 /* initial device configuration */
1545 mutex_lock(&dev->lock);
1546 cx23885_set_tvnorm(dev, dev->tvnorm);
1547 init_controls(dev);
1548 cx23885_video_mux(dev, 0);
1549 mutex_unlock(&dev->lock);
1550
1551 return 0;
1552
1553fail_unreg:
1554 cx23885_video_unregister(dev);
1555 return err;
1556}
1557
diff --git a/drivers/media/video/cx23885/cx23885.h b/drivers/media/video/cx23885/cx23885.h
index dec4dc2fcbb4..7cb2179f2622 100644
--- a/drivers/media/video/cx23885/cx23885.h
+++ b/drivers/media/video/cx23885/cx23885.h
@@ -44,6 +44,10 @@
44 44
45/* Max number of inputs by card */ 45/* Max number of inputs by card */
46#define MAX_CX23885_INPUT 8 46#define MAX_CX23885_INPUT 8
47#define INPUT(nr) (&cx23885_boards[dev->board].input[nr])
48#define RESOURCE_OVERLAY 1
49#define RESOURCE_VIDEO 2
50#define RESOURCE_VBI 4
47 51
48#define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */ 52#define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */
49 53
@@ -53,6 +57,62 @@
53#define CX23885_BOARD_HAUPPAUGE_HVR1800 2 57#define CX23885_BOARD_HAUPPAUGE_HVR1800 2
54#define CX23885_BOARD_HAUPPAUGE_HVR1250 3 58#define CX23885_BOARD_HAUPPAUGE_HVR1250 3
55#define CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP 4 59#define CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP 4
60#define CX23885_BOARD_HAUPPAUGE_HVR1500Q 5
61#define CX23885_BOARD_HAUPPAUGE_HVR1500 6
62
63/* Currently unsupported by the driver: PAL/H, NTSC/Kr, SECAM B/G/H/LC */
64#define CX23885_NORMS (\
65 V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_443 | \
66 V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_I | \
67 V4L2_STD_PAL_M | V4L2_STD_PAL_N | V4L2_STD_PAL_Nc | \
68 V4L2_STD_PAL_60 | V4L2_STD_SECAM_L | V4L2_STD_SECAM_DK)
69
70struct cx23885_fmt {
71 char *name;
72 u32 fourcc; /* v4l2 format id */
73 int depth;
74 int flags;
75 u32 cxformat;
76};
77
78struct cx23885_ctrl {
79 struct v4l2_queryctrl v;
80 u32 off;
81 u32 reg;
82 u32 mask;
83 u32 shift;
84};
85
86struct cx23885_tvnorm {
87 char *name;
88 v4l2_std_id id;
89 u32 cxiformat;
90 u32 cxoformat;
91};
92
93struct cx23885_fh {
94 struct cx23885_dev *dev;
95 enum v4l2_buf_type type;
96 int radio;
97 u32 resources;
98
99 /* video overlay */
100 struct v4l2_window win;
101 struct v4l2_clip *clips;
102 unsigned int nclips;
103
104 /* video capture */
105 struct cx23885_fmt *fmt;
106 unsigned int width, height;
107
108 /* vbi capture */
109 struct videobuf_queue vidq;
110 struct videobuf_queue vbiq;
111
112 /* MPEG Encoder specifics ONLY */
113 struct videobuf_queue mpegq;
114 atomic_t v4l_reading;
115};
56 116
57enum cx23885_itype { 117enum cx23885_itype {
58 CX23885_VMUX_COMPOSITE1 = 1, 118 CX23885_VMUX_COMPOSITE1 = 1,
@@ -92,12 +152,28 @@ struct cx23885_input {
92 152
93typedef enum { 153typedef enum {
94 CX23885_MPEG_UNDEFINED = 0, 154 CX23885_MPEG_UNDEFINED = 0,
95 CX23885_MPEG_DVB 155 CX23885_MPEG_DVB,
156 CX23885_ANALOG_VIDEO,
96} port_t; 157} port_t;
97 158
98struct cx23885_board { 159struct cx23885_board {
99 char *name; 160 char *name;
100 port_t portb, portc; 161 port_t porta, portb, portc;
162 unsigned int tuner_type;
163 unsigned int radio_type;
164 unsigned char tuner_addr;
165 unsigned char radio_addr;
166
167 /* Vendors can and do run the PCIe bridge at different
168 * clock rates, driven physically by crystals on the PCBs.
169 * The core has to accomodate this. This allows the user
170 * to add new boards with new frequencys. The value is
171 * expressed in Hz.
172 *
173 * The core framework will default this value based on
174 * current designs, but it can vary.
175 */
176 u32 clk_freq;
101 struct cx23885_input input[MAX_CX23885_INPUT]; 177 struct cx23885_input input[MAX_CX23885_INPUT];
102}; 178};
103 179
@@ -189,6 +265,11 @@ struct cx23885_dev {
189 u32 __iomem *lmmio; 265 u32 __iomem *lmmio;
190 u8 __iomem *bmmio; 266 u8 __iomem *bmmio;
191 int pci_irqmask; 267 int pci_irqmask;
268 int hwrevision;
269
270 /* This valud is board specific and is used to configure the
271 * AV core so we see nice clean and stable video and audio. */
272 u32 clk_freq;
192 273
193 /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */ 274 /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
194 struct cx23885_i2c i2c_bus[3]; 275 struct cx23885_i2c i2c_bus[3];
@@ -210,8 +291,31 @@ struct cx23885_dev {
210 CX23885_BRIDGE_885 = 885, 291 CX23885_BRIDGE_885 = 885,
211 CX23885_BRIDGE_887 = 887, 292 CX23885_BRIDGE_887 = 887,
212 } bridge; 293 } bridge;
294
295 /* Analog video */
296 u32 resources;
297 unsigned int input;
298 u32 tvaudio;
299 v4l2_std_id tvnorm;
300 unsigned int tuner_type;
301 unsigned char tuner_addr;
302 unsigned int radio_type;
303 unsigned char radio_addr;
304 unsigned int has_radio;
305
306 /* V4l */
307 u32 freq;
308 struct video_device *video_dev;
309 struct video_device *vbi_dev;
310 struct video_device *radio_dev;
311
312 struct cx23885_dmaqueue vidq;
313 struct cx23885_dmaqueue vbiq;
314 spinlock_t slock;
213}; 315};
214 316
317extern struct list_head cx23885_devlist;
318
215#define SRAM_CH01 0 /* Video A */ 319#define SRAM_CH01 0 /* Video A */
216#define SRAM_CH02 1 /* VBI A */ 320#define SRAM_CH02 1 /* VBI A */
217#define SRAM_CH03 2 /* Video B */ 321#define SRAM_CH03 2 /* Video B */
@@ -254,19 +358,42 @@ struct sram_channel {
254#define cx_set(reg,bit) cx_andor((reg),(bit),(bit)) 358#define cx_set(reg,bit) cx_andor((reg),(bit),(bit))
255#define cx_clear(reg,bit) cx_andor((reg),(bit),0) 359#define cx_clear(reg,bit) cx_andor((reg),(bit),0)
256 360
361/* ----------------------------------------------------------- */
362/* cx23885-core.c */
363
257extern int cx23885_sram_channel_setup(struct cx23885_dev *dev, 364extern int cx23885_sram_channel_setup(struct cx23885_dev *dev,
258 struct sram_channel *ch, 365 struct sram_channel *ch,
259 unsigned int bpl, u32 risc); 366 unsigned int bpl, u32 risc);
260 367
261/* ----------------------------------------------------------- */ 368extern void cx23885_sram_channel_dump(struct cx23885_dev *dev,
262/* cx23885-cards.c */ 369 struct sram_channel *ch);
263 370
371extern int cx23885_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
372 u32 reg, u32 mask, u32 value);
373
374extern int cx23885_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
375 struct scatterlist *sglist,
376 unsigned int top_offset, unsigned int bottom_offset,
377 unsigned int bpl, unsigned int padding, unsigned int lines);
378
379void cx23885_cancel_buffers(struct cx23885_tsport *port);
380
381extern int cx23885_restart_queue(struct cx23885_tsport *port,
382 struct cx23885_dmaqueue *q);
383
384extern void cx23885_wakeup(struct cx23885_tsport *port,
385 struct cx23885_dmaqueue *q, u32 count);
386
387
388/* ----------------------------------------------------------- */
389/* cx23885-cards.c */
264extern struct cx23885_board cx23885_boards[]; 390extern struct cx23885_board cx23885_boards[];
265extern const unsigned int cx23885_bcount; 391extern const unsigned int cx23885_bcount;
266 392
267extern struct cx23885_subid cx23885_subids[]; 393extern struct cx23885_subid cx23885_subids[];
268extern const unsigned int cx23885_idcount; 394extern const unsigned int cx23885_idcount;
269 395
396extern int cx23885_tuner_callback(void *priv, int command, int arg);
270extern void cx23885_card_list(struct cx23885_dev *dev); 397extern void cx23885_card_list(struct cx23885_dev *dev);
271extern int cx23885_ir_init(struct cx23885_dev *dev); 398extern int cx23885_ir_init(struct cx23885_dev *dev);
272extern void cx23885_gpio_setup(struct cx23885_dev *dev); 399extern void cx23885_gpio_setup(struct cx23885_dev *dev);
@@ -280,19 +407,50 @@ extern int cx23885_buf_prepare(struct videobuf_queue *q,
280 struct cx23885_tsport *port, 407 struct cx23885_tsport *port,
281 struct cx23885_buffer *buf, 408 struct cx23885_buffer *buf,
282 enum v4l2_field field); 409 enum v4l2_field field);
283
284extern void cx23885_buf_queue(struct cx23885_tsport *port, 410extern void cx23885_buf_queue(struct cx23885_tsport *port,
285 struct cx23885_buffer *buf); 411 struct cx23885_buffer *buf);
286extern void cx23885_free_buffer(struct videobuf_queue *q, 412extern void cx23885_free_buffer(struct videobuf_queue *q,
287 struct cx23885_buffer *buf); 413 struct cx23885_buffer *buf);
288 414
289/* ----------------------------------------------------------- */ 415/* ----------------------------------------------------------- */
416/* cx23885-video.c */
417/* Video */
418extern int cx23885_video_register(struct cx23885_dev *dev);
419extern void cx23885_video_unregister(struct cx23885_dev *dev);
420extern int cx23885_video_irq(struct cx23885_dev *dev, u32 status);
421
422/* ----------------------------------------------------------- */
423/* cx23885-vbi.c */
424extern int cx23885_vbi_fmt(struct file *file, void *priv,
425 struct v4l2_format *f);
426extern void cx23885_vbi_timeout(unsigned long data);
427extern struct videobuf_queue_ops cx23885_vbi_qops;
428
290/* cx23885-i2c.c */ 429/* cx23885-i2c.c */
291extern int cx23885_i2c_register(struct cx23885_i2c *bus); 430extern int cx23885_i2c_register(struct cx23885_i2c *bus);
292extern int cx23885_i2c_unregister(struct cx23885_i2c *bus); 431extern int cx23885_i2c_unregister(struct cx23885_i2c *bus);
293extern void cx23885_call_i2c_clients(struct cx23885_i2c *bus, unsigned int cmd, 432extern void cx23885_call_i2c_clients(struct cx23885_i2c *bus, unsigned int cmd,
294 void *arg); 433 void *arg);
295 434
435/* ----------------------------------------------------------- */
436/* tv norms */
437
438static inline unsigned int norm_maxw(v4l2_std_id norm)
439{
440 return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 720 : 768;
441}
442
443static inline unsigned int norm_maxh(v4l2_std_id norm)
444{
445 return (norm & V4L2_STD_625_50) ? 576 : 480;
446}
447
448static inline unsigned int norm_swidth(v4l2_std_id norm)
449{
450 return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 754 : 922;
451}
452
453
296/* 454/*
297 * Local variables: 455 * Local variables:
298 * c-basic-offset: 8 456 * c-basic-offset: 8
diff --git a/drivers/media/video/cx25840/cx25840-audio.c b/drivers/media/video/cx25840/cx25840-audio.c
index 3d46a776df36..d6421e1e8f6a 100644
--- a/drivers/media/video/cx25840/cx25840-audio.c
+++ b/drivers/media/video/cx25840/cx25840-audio.c
@@ -32,118 +32,156 @@ static int set_audclk_freq(struct i2c_client *client, u32 freq)
32 32
33 /* common for all inputs and rates */ 33 /* common for all inputs and rates */
34 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */ 34 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
35 cx25840_write(client, 0x127, 0x50); 35 if (!state->is_cx23885)
36 cx25840_write(client, 0x127, 0x50);
36 37
37 if (state->aud_input != CX25840_AUDIO_SERIAL) { 38 if (state->aud_input != CX25840_AUDIO_SERIAL) {
38 switch (freq) { 39 switch (freq) {
39 case 32000: 40 case 32000:
41 if (state->is_cx23885) {
42 /* We don't have register values
43 * so avoid destroying registers. */
44 break;
45 }
40 /* VID_PLL and AUX_PLL */ 46 /* VID_PLL and AUX_PLL */
41 cx25840_write4(client, 0x108, 0x0f040610); 47 cx25840_write4(client, 0x108, 0x1006040f);
42 48
43 /* AUX_PLL_FRAC */ 49 /* AUX_PLL_FRAC */
44 cx25840_write4(client, 0x110, 0xee39bb01); 50 cx25840_write4(client, 0x110, 0x01bb39ee);
45 51
46 if (state->is_cx25836) 52 if (state->is_cx25836)
47 break; 53 break;
48 54
49 /* src3/4/6_ctl = 0x0801f77f */ 55 /* src3/4/6_ctl = 0x0801f77f */
50 cx25840_write4(client, 0x900, 0x7ff70108); 56 cx25840_write4(client, 0x900, 0x0801f77f);
51 cx25840_write4(client, 0x904, 0x7ff70108); 57 cx25840_write4(client, 0x904, 0x0801f77f);
52 cx25840_write4(client, 0x90c, 0x7ff70108); 58 cx25840_write4(client, 0x90c, 0x0801f77f);
53 break; 59 break;
54 60
55 case 44100: 61 case 44100:
62 if (state->is_cx23885) {
63 /* We don't have register values
64 * so avoid destroying registers. */
65 break;
66 }
56 /* VID_PLL and AUX_PLL */ 67 /* VID_PLL and AUX_PLL */
57 cx25840_write4(client, 0x108, 0x0f040910); 68 cx25840_write4(client, 0x108, 0x1009040f);
58 69
59 /* AUX_PLL_FRAC */ 70 /* AUX_PLL_FRAC */
60 cx25840_write4(client, 0x110, 0xd66bec00); 71 cx25840_write4(client, 0x110, 0x00ec6bd6);
61 72
62 if (state->is_cx25836) 73 if (state->is_cx25836)
63 break; 74 break;
64 75
65 /* src3/4/6_ctl = 0x08016d59 */ 76 /* src3/4/6_ctl = 0x08016d59 */
66 cx25840_write4(client, 0x900, 0x596d0108); 77 cx25840_write4(client, 0x900, 0x08016d59);
67 cx25840_write4(client, 0x904, 0x596d0108); 78 cx25840_write4(client, 0x904, 0x08016d59);
68 cx25840_write4(client, 0x90c, 0x596d0108); 79 cx25840_write4(client, 0x90c, 0x08016d59);
69 break; 80 break;
70 81
71 case 48000: 82 case 48000:
83 if (state->is_cx23885) {
84 /* We don't have register values
85 * so avoid destroying registers. */
86 break;
87 }
72 /* VID_PLL and AUX_PLL */ 88 /* VID_PLL and AUX_PLL */
73 cx25840_write4(client, 0x108, 0x0f040a10); 89 cx25840_write4(client, 0x108, 0x100a040f);
74 90
75 /* AUX_PLL_FRAC */ 91 /* AUX_PLL_FRAC */
76 cx25840_write4(client, 0x110, 0xe5d69800); 92 cx25840_write4(client, 0x110, 0x0098d6e5);
77 93
78 if (state->is_cx25836) 94 if (state->is_cx25836)
79 break; 95 break;
80 96
81 /* src3/4/6_ctl = 0x08014faa */ 97 /* src3/4/6_ctl = 0x08014faa */
82 cx25840_write4(client, 0x900, 0xaa4f0108); 98 cx25840_write4(client, 0x900, 0x08014faa);
83 cx25840_write4(client, 0x904, 0xaa4f0108); 99 cx25840_write4(client, 0x904, 0x08014faa);
84 cx25840_write4(client, 0x90c, 0xaa4f0108); 100 cx25840_write4(client, 0x90c, 0x08014faa);
85 break; 101 break;
86 } 102 }
87 } else { 103 } else {
88 switch (freq) { 104 switch (freq) {
89 case 32000: 105 case 32000:
106 if (state->is_cx23885) {
107 /* We don't have register values
108 * so avoid destroying registers. */
109 break;
110 }
90 /* VID_PLL and AUX_PLL */ 111 /* VID_PLL and AUX_PLL */
91 cx25840_write4(client, 0x108, 0x0f04081e); 112 cx25840_write4(client, 0x108, 0x1e08040f);
92 113
93 /* AUX_PLL_FRAC */ 114 /* AUX_PLL_FRAC */
94 cx25840_write4(client, 0x110, 0x69082a01); 115 cx25840_write4(client, 0x110, 0x012a0869);
95 116
96 if (state->is_cx25836) 117 if (state->is_cx25836)
97 break; 118 break;
98 119
99 /* src1_ctl = 0x08010000 */ 120 /* src1_ctl = 0x08010000 */
100 cx25840_write4(client, 0x8f8, 0x00000108); 121 cx25840_write4(client, 0x8f8, 0x08010000);
101 122
102 /* src3/4/6_ctl = 0x08020000 */ 123 /* src3/4/6_ctl = 0x08020000 */
103 cx25840_write4(client, 0x900, 0x00000208); 124 cx25840_write4(client, 0x900, 0x08020000);
104 cx25840_write4(client, 0x904, 0x00000208); 125 cx25840_write4(client, 0x904, 0x08020000);
105 cx25840_write4(client, 0x90c, 0x00000208); 126 cx25840_write4(client, 0x90c, 0x08020000);
106 127
107 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */ 128 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
108 cx25840_write(client, 0x127, 0x54); 129 cx25840_write(client, 0x127, 0x54);
109 break; 130 break;
110 131
111 case 44100: 132 case 44100:
133 if (state->is_cx23885) {
134 /* We don't have register values
135 * so avoid destroying registers. */
136 break;
137 }
138
112 /* VID_PLL and AUX_PLL */ 139 /* VID_PLL and AUX_PLL */
113 cx25840_write4(client, 0x108, 0x0f040918); 140 cx25840_write4(client, 0x108, 0x1809040f);
114 141
115 /* AUX_PLL_FRAC */ 142 /* AUX_PLL_FRAC */
116 cx25840_write4(client, 0x110, 0xd66bec00); 143 cx25840_write4(client, 0x110, 0x00ec6bd6);
117 144
118 if (state->is_cx25836) 145 if (state->is_cx25836)
119 break; 146 break;
120 147
121 /* src1_ctl = 0x08010000 */ 148 /* src1_ctl = 0x08010000 */
122 cx25840_write4(client, 0x8f8, 0xcd600108); 149 cx25840_write4(client, 0x8f8, 0x080160cd);
123 150
124 /* src3/4/6_ctl = 0x08020000 */ 151 /* src3/4/6_ctl = 0x08020000 */
125 cx25840_write4(client, 0x900, 0x85730108); 152 cx25840_write4(client, 0x900, 0x08017385);
126 cx25840_write4(client, 0x904, 0x85730108); 153 cx25840_write4(client, 0x904, 0x08017385);
127 cx25840_write4(client, 0x90c, 0x85730108); 154 cx25840_write4(client, 0x90c, 0x08017385);
128 break; 155 break;
129 156
130 case 48000: 157 case 48000:
131 /* VID_PLL and AUX_PLL */ 158 if (!state->is_cx23885) {
132 cx25840_write4(client, 0x108, 0x0f040a18); 159 /* VID_PLL and AUX_PLL */
160 cx25840_write4(client, 0x108, 0x180a040f);
133 161
134 /* AUX_PLL_FRAC */ 162 /* AUX_PLL_FRAC */
135 cx25840_write4(client, 0x110, 0xe5d69800); 163 cx25840_write4(client, 0x110, 0x0098d6e5);
164 }
136 165
137 if (state->is_cx25836) 166 if (state->is_cx25836)
138 break; 167 break;
139 168
140 /* src1_ctl = 0x08010000 */ 169 if (!state->is_cx23885) {
141 cx25840_write4(client, 0x8f8, 0x00800108); 170 /* src1_ctl */
171 cx25840_write4(client, 0x8f8, 0x08018000);
142 172
143 /* src3/4/6_ctl = 0x08020000 */ 173 /* src3/4/6_ctl */
144 cx25840_write4(client, 0x900, 0x55550108); 174 cx25840_write4(client, 0x900, 0x08015555);
145 cx25840_write4(client, 0x904, 0x55550108); 175 cx25840_write4(client, 0x904, 0x08015555);
146 cx25840_write4(client, 0x90c, 0x55550108); 176 cx25840_write4(client, 0x90c, 0x08015555);
177 } else {
178
179 cx25840_write4(client, 0x8f8, 0x0801867c);
180
181 cx25840_write4(client, 0x900, 0x08014faa);
182 cx25840_write4(client, 0x904, 0x08014faa);
183 cx25840_write4(client, 0x90c, 0x08014faa);
184 }
147 break; 185 break;
148 } 186 }
149 } 187 }
@@ -168,14 +206,14 @@ void cx25840_audio_set_path(struct i2c_client *client)
168 206
169 if (state->aud_input == CX25840_AUDIO_SERIAL) { 207 if (state->aud_input == CX25840_AUDIO_SERIAL) {
170 /* Set Path1 to Serial Audio Input */ 208 /* Set Path1 to Serial Audio Input */
171 cx25840_write4(client, 0x8d0, 0x12100101); 209 cx25840_write4(client, 0x8d0, 0x01011012);
172 210
173 /* The microcontroller should not be started for the 211 /* The microcontroller should not be started for the
174 * non-tuner inputs: autodetection is specific for 212 * non-tuner inputs: autodetection is specific for
175 * TV audio. */ 213 * TV audio. */
176 } else { 214 } else {
177 /* Set Path1 to Analog Demod Main Channel */ 215 /* Set Path1 to Analog Demod Main Channel */
178 cx25840_write4(client, 0x8d0, 0x7038061f); 216 cx25840_write4(client, 0x8d0, 0x1f063870);
179 } 217 }
180 218
181 set_audclk_freq(client, state->audclk_freq); 219 set_audclk_freq(client, state->audclk_freq);
@@ -188,6 +226,11 @@ void cx25840_audio_set_path(struct i2c_client *client)
188 226
189 /* deassert soft reset */ 227 /* deassert soft reset */
190 cx25840_and_or(client, 0x810, ~0x1, 0x00); 228 cx25840_and_or(client, 0x810, ~0x1, 0x00);
229
230 if (state->is_cx23885) {
231 /* Ensure the controller is running when we exit */
232 cx25840_and_or(client, 0x803, ~0x10, 0x10);
233 }
191} 234}
192 235
193static int get_volume(struct i2c_client *client) 236static int get_volume(struct i2c_client *client)
diff --git a/drivers/media/video/cx25840/cx25840-core.c b/drivers/media/video/cx25840/cx25840-core.c
index 15f191e170d2..756a1eeb274e 100644
--- a/drivers/media/video/cx25840/cx25840-core.c
+++ b/drivers/media/video/cx25840/cx25840-core.c
@@ -13,6 +13,8 @@
13 * NTSC sliced VBI support by Christopher Neufeld <television@cneufeld.ca> 13 * NTSC sliced VBI support by Christopher Neufeld <television@cneufeld.ca>
14 * with additional fixes by Hans Verkuil <hverkuil@xs4all.nl>. 14 * with additional fixes by Hans Verkuil <hverkuil@xs4all.nl>.
15 * 15 *
16 * CX23885 support by Steven Toth <stoth@hauppauge.com>.
17 *
16 * This program is free software; you can redistribute it and/or 18 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License 19 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2 20 * as published by the Free Software Foundation; either version 2
@@ -37,6 +39,7 @@
37#include <linux/delay.h> 39#include <linux/delay.h>
38#include <media/v4l2-common.h> 40#include <media/v4l2-common.h>
39#include <media/v4l2-chip-ident.h> 41#include <media/v4l2-chip-ident.h>
42#include <media/v4l2-i2c-drv-legacy.h>
40#include <media/cx25840.h> 43#include <media/cx25840.h>
41 44
42#include "cx25840-core.h" 45#include "cx25840-core.h"
@@ -72,10 +75,10 @@ int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
72 u8 buffer[6]; 75 u8 buffer[6];
73 buffer[0] = addr >> 8; 76 buffer[0] = addr >> 8;
74 buffer[1] = addr & 0xff; 77 buffer[1] = addr & 0xff;
75 buffer[2] = value >> 24; 78 buffer[2] = value & 0xff;
76 buffer[3] = (value >> 16) & 0xff; 79 buffer[3] = (value >> 8) & 0xff;
77 buffer[4] = (value >> 8) & 0xff; 80 buffer[4] = (value >> 16) & 0xff;
78 buffer[5] = value & 0xff; 81 buffer[5] = value >> 24;
79 return i2c_master_send(client, buffer, 6); 82 return i2c_master_send(client, buffer, 6);
80} 83}
81 84
@@ -122,8 +125,6 @@ int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
122 125
123static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input, 126static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
124 enum cx25840_audio_input aud_input); 127 enum cx25840_audio_input aud_input);
125static void log_audio_status(struct i2c_client *client);
126static void log_video_status(struct i2c_client *client);
127 128
128/* ----------------------------------------------------------------------- */ 129/* ----------------------------------------------------------------------- */
129 130
@@ -256,6 +257,96 @@ static void cx25840_initialize(struct i2c_client *client)
256 cx25840_and_or(client, 0x803, ~0x10, 0x10); 257 cx25840_and_or(client, 0x803, ~0x10, 0x10);
257} 258}
258 259
260static void cx23885_initialize(struct i2c_client *client)
261{
262 DEFINE_WAIT(wait);
263 struct cx25840_state *state = i2c_get_clientdata(client);
264 struct workqueue_struct *q;
265
266 /* Internal Reset */
267 cx25840_and_or(client, 0x102, ~0x01, 0x01);
268 cx25840_and_or(client, 0x102, ~0x01, 0x00);
269
270 /* Stop microcontroller */
271 cx25840_and_or(client, 0x803, ~0x10, 0x00);
272
273 /* DIF in reset? */
274 cx25840_write(client, 0x398, 0);
275
276 /* Trust the default xtal, no division */
277 /* This changes for the cx23888 products */
278 cx25840_write(client, 0x2, 0x76);
279
280 /* Bring down the regulator for AUX clk */
281 cx25840_write(client, 0x1, 0x40);
282
283 /* Sys PLL frac */
284 cx25840_write4(client, 0x11c, 0x01d1744c);
285
286 /* Sys PLL int */
287 cx25840_write4(client, 0x118, 0x00000416);
288
289 /* Disable DIF bypass */
290 cx25840_write4(client, 0x33c, 0x00000001);
291
292 /* DIF Src phase inc */
293 cx25840_write4(client, 0x340, 0x0df7df83);
294
295 /* Vid PLL frac */
296 cx25840_write4(client, 0x10c, 0x01b6db7b);
297
298 /* Vid PLL int */
299 cx25840_write4(client, 0x108, 0x00000512);
300
301 /* Luma */
302 cx25840_write4(client, 0x414, 0x00107d12);
303
304 /* Chroma */
305 cx25840_write4(client, 0x420, 0x3d008282);
306
307 /* Aux PLL frac */
308 cx25840_write4(client, 0x114, 0x017dbf48);
309
310 /* Aux PLL int */
311 cx25840_write4(client, 0x110, 0x000a030e);
312
313 /* ADC2 input select */
314 cx25840_write(client, 0x102, 0x10);
315
316 /* VIN1 & VIN5 */
317 cx25840_write(client, 0x103, 0x11);
318
319 /* Enable format auto detect */
320 cx25840_write(client, 0x400, 0);
321 /* Fast subchroma lock */
322 /* White crush, Chroma AGC & Chroma Killer enabled */
323 cx25840_write(client, 0x401, 0xe8);
324
325 /* Select AFE clock pad output source */
326 cx25840_write(client, 0x144, 0x05);
327
328 /* Do the firmware load in a work handler to prevent.
329 Otherwise the kernel is blocked waiting for the
330 bit-banging i2c interface to finish uploading the
331 firmware. */
332 INIT_WORK(&state->fw_work, cx25840_work_handler);
333 init_waitqueue_head(&state->fw_wait);
334 q = create_singlethread_workqueue("cx25840_fw");
335 prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
336 queue_work(q, &state->fw_work);
337 schedule();
338 finish_wait(&state->fw_wait, &wait);
339 destroy_workqueue(q);
340
341 cx25840_vbi_setup(client);
342
343 /* (re)set input */
344 set_input(client, state->vid_input, state->aud_input);
345
346 /* start microcontroller */
347 cx25840_and_or(client, 0x803, ~0x10, 0x10);
348}
349
259/* ----------------------------------------------------------------------- */ 350/* ----------------------------------------------------------------------- */
260 351
261static void input_change(struct i2c_client *client) 352static void input_change(struct i2c_client *client)
@@ -319,9 +410,22 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
319 vid_input <= CX25840_COMPOSITE8); 410 vid_input <= CX25840_COMPOSITE8);
320 u8 reg; 411 u8 reg;
321 412
322 v4l_dbg(1, cx25840_debug, client, "decoder set video input %d, audio input %d\n", 413 v4l_dbg(1, cx25840_debug, client,
323 vid_input, aud_input); 414 "decoder set video input %d, audio input %d\n",
415 vid_input, aud_input);
416
417 if (vid_input >= CX25840_VIN1_CH1) {
418 v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",
419 vid_input);
420 reg = vid_input & 0xff;
421 if ((vid_input & CX25840_SVIDEO_ON) == CX25840_SVIDEO_ON)
422 is_composite = 0;
423 else
424 is_composite = 1;
324 425
426 v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
427 reg, is_composite);
428 } else
325 if (is_composite) { 429 if (is_composite) {
326 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1); 430 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
327 } else { 431 } else {
@@ -331,7 +435,8 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
331 if ((vid_input & ~0xff0) || 435 if ((vid_input & ~0xff0) ||
332 luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 || 436 luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
333 chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) { 437 chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
334 v4l_err(client, "0x%04x is not a valid video input!\n", vid_input); 438 v4l_err(client, "0x%04x is not a valid video input!\n",
439 vid_input);
335 return -EINVAL; 440 return -EINVAL;
336 } 441 }
337 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4); 442 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
@@ -344,31 +449,49 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
344 } 449 }
345 } 450 }
346 451
347 switch (aud_input) { 452 /* The caller has previously prepared the correct routing
348 case CX25840_AUDIO_SERIAL: 453 * configuration in reg (for the cx23885) so we have no
349 /* do nothing, use serial audio input */ 454 * need to attempt to flip bits for earlier av decoders.
350 break; 455 */
351 case CX25840_AUDIO4: reg &= ~0x30; break; 456 if (!state->is_cx23885) {
352 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break; 457 switch (aud_input) {
353 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break; 458 case CX25840_AUDIO_SERIAL:
354 case CX25840_AUDIO7: reg &= ~0xc0; break; 459 /* do nothing, use serial audio input */
355 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break; 460 break;
461 case CX25840_AUDIO4: reg &= ~0x30; break;
462 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
463 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
464 case CX25840_AUDIO7: reg &= ~0xc0; break;
465 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
356 466
357 default: 467 default:
358 v4l_err(client, "0x%04x is not a valid audio input!\n", aud_input); 468 v4l_err(client, "0x%04x is not a valid audio input!\n",
359 return -EINVAL; 469 aud_input);
470 return -EINVAL;
471 }
360 } 472 }
361 473
362 cx25840_write(client, 0x103, reg); 474 cx25840_write(client, 0x103, reg);
475
363 /* Set INPUT_MODE to Composite (0) or S-Video (1) */ 476 /* Set INPUT_MODE to Composite (0) or S-Video (1) */
364 cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02); 477 cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
365 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */ 478
366 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0); 479 if (!state->is_cx23885) {
367 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */ 480 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
368 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30) 481 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
369 cx25840_and_or(client, 0x102, ~0x4, 4); 482 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
370 else 483 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
371 cx25840_and_or(client, 0x102, ~0x4, 0); 484 cx25840_and_or(client, 0x102, ~0x4, 4);
485 else
486 cx25840_and_or(client, 0x102, ~0x4, 0);
487 } else {
488 if (is_composite)
489 /* ADC2 input select channel 2 */
490 cx25840_and_or(client, 0x102, ~0x2, 0);
491 else
492 /* ADC2 input select channel 3 */
493 cx25840_and_or(client, 0x102, ~0x2, 2);
494 }
372 495
373 state->vid_input = vid_input; 496 state->vid_input = vid_input;
374 state->aud_input = aud_input; 497 state->aud_input = aud_input;
@@ -376,6 +499,25 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
376 cx25840_audio_set_path(client); 499 cx25840_audio_set_path(client);
377 input_change(client); 500 input_change(client);
378 } 501 }
502
503 if (state->is_cx23885) {
504 /* Audio channel 1 src : Parallel 1 */
505 cx25840_write(client, 0x124, 0x03);
506
507 /* Select AFE clock pad output source */
508 cx25840_write(client, 0x144, 0x05);
509
510 /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
511 cx25840_write(client, 0x914, 0xa0);
512
513 /* I2S_OUT_CTL:
514 * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
515 * I2S_OUT_MASTER_MODE = Master
516 */
517 cx25840_write(client, 0x918, 0xa0);
518 cx25840_write(client, 0x919, 0x01);
519 }
520
379 return 0; 521 return 0;
380} 522}
381 523
@@ -641,6 +783,200 @@ static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
641 783
642/* ----------------------------------------------------------------------- */ 784/* ----------------------------------------------------------------------- */
643 785
786static void log_video_status(struct i2c_client *client)
787{
788 static const char *const fmt_strs[] = {
789 "0x0",
790 "NTSC-M", "NTSC-J", "NTSC-4.43",
791 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
792 "0x9", "0xA", "0xB",
793 "SECAM",
794 "0xD", "0xE", "0xF"
795 };
796
797 struct cx25840_state *state = i2c_get_clientdata(client);
798 u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
799 u8 gen_stat1 = cx25840_read(client, 0x40d);
800 u8 gen_stat2 = cx25840_read(client, 0x40e);
801 int vid_input = state->vid_input;
802
803 v4l_info(client, "Video signal: %spresent\n",
804 (gen_stat2 & 0x20) ? "" : "not ");
805 v4l_info(client, "Detected format: %s\n",
806 fmt_strs[gen_stat1 & 0xf]);
807
808 v4l_info(client, "Specified standard: %s\n",
809 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
810
811 if (vid_input >= CX25840_COMPOSITE1 &&
812 vid_input <= CX25840_COMPOSITE8) {
813 v4l_info(client, "Specified video input: Composite %d\n",
814 vid_input - CX25840_COMPOSITE1 + 1);
815 } else {
816 v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
817 (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
818 }
819
820 v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
821}
822
823/* ----------------------------------------------------------------------- */
824
825static void log_audio_status(struct i2c_client *client)
826{
827 struct cx25840_state *state = i2c_get_clientdata(client);
828 u8 download_ctl = cx25840_read(client, 0x803);
829 u8 mod_det_stat0 = cx25840_read(client, 0x804);
830 u8 mod_det_stat1 = cx25840_read(client, 0x805);
831 u8 audio_config = cx25840_read(client, 0x808);
832 u8 pref_mode = cx25840_read(client, 0x809);
833 u8 afc0 = cx25840_read(client, 0x80b);
834 u8 mute_ctl = cx25840_read(client, 0x8d3);
835 int aud_input = state->aud_input;
836 char *p;
837
838 switch (mod_det_stat0) {
839 case 0x00: p = "mono"; break;
840 case 0x01: p = "stereo"; break;
841 case 0x02: p = "dual"; break;
842 case 0x04: p = "tri"; break;
843 case 0x10: p = "mono with SAP"; break;
844 case 0x11: p = "stereo with SAP"; break;
845 case 0x12: p = "dual with SAP"; break;
846 case 0x14: p = "tri with SAP"; break;
847 case 0xfe: p = "forced mode"; break;
848 default: p = "not defined";
849 }
850 v4l_info(client, "Detected audio mode: %s\n", p);
851
852 switch (mod_det_stat1) {
853 case 0x00: p = "not defined"; break;
854 case 0x01: p = "EIAJ"; break;
855 case 0x02: p = "A2-M"; break;
856 case 0x03: p = "A2-BG"; break;
857 case 0x04: p = "A2-DK1"; break;
858 case 0x05: p = "A2-DK2"; break;
859 case 0x06: p = "A2-DK3"; break;
860 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
861 case 0x08: p = "AM-L"; break;
862 case 0x09: p = "NICAM-BG"; break;
863 case 0x0a: p = "NICAM-DK"; break;
864 case 0x0b: p = "NICAM-I"; break;
865 case 0x0c: p = "NICAM-L"; break;
866 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
867 case 0x0e: p = "IF FM Radio"; break;
868 case 0x0f: p = "BTSC"; break;
869 case 0x10: p = "high-deviation FM"; break;
870 case 0x11: p = "very high-deviation FM"; break;
871 case 0xfd: p = "unknown audio standard"; break;
872 case 0xfe: p = "forced audio standard"; break;
873 case 0xff: p = "no detected audio standard"; break;
874 default: p = "not defined";
875 }
876 v4l_info(client, "Detected audio standard: %s\n", p);
877 v4l_info(client, "Audio muted: %s\n",
878 (state->unmute_volume >= 0) ? "yes" : "no");
879 v4l_info(client, "Audio microcontroller: %s\n",
880 (download_ctl & 0x10) ?
881 ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
882
883 switch (audio_config >> 4) {
884 case 0x00: p = "undefined"; break;
885 case 0x01: p = "BTSC"; break;
886 case 0x02: p = "EIAJ"; break;
887 case 0x03: p = "A2-M"; break;
888 case 0x04: p = "A2-BG"; break;
889 case 0x05: p = "A2-DK1"; break;
890 case 0x06: p = "A2-DK2"; break;
891 case 0x07: p = "A2-DK3"; break;
892 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
893 case 0x09: p = "AM-L"; break;
894 case 0x0a: p = "NICAM-BG"; break;
895 case 0x0b: p = "NICAM-DK"; break;
896 case 0x0c: p = "NICAM-I"; break;
897 case 0x0d: p = "NICAM-L"; break;
898 case 0x0e: p = "FM radio"; break;
899 case 0x0f: p = "automatic detection"; break;
900 default: p = "undefined";
901 }
902 v4l_info(client, "Configured audio standard: %s\n", p);
903
904 if ((audio_config >> 4) < 0xF) {
905 switch (audio_config & 0xF) {
906 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
907 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
908 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
909 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
910 case 0x04: p = "STEREO"; break;
911 case 0x05: p = "DUAL1 (AB)"; break;
912 case 0x06: p = "DUAL2 (AC) (FM)"; break;
913 case 0x07: p = "DUAL3 (BC) (FM)"; break;
914 case 0x08: p = "DUAL4 (AC) (AM)"; break;
915 case 0x09: p = "DUAL5 (BC) (AM)"; break;
916 case 0x0a: p = "SAP"; break;
917 default: p = "undefined";
918 }
919 v4l_info(client, "Configured audio mode: %s\n", p);
920 } else {
921 switch (audio_config & 0xF) {
922 case 0x00: p = "BG"; break;
923 case 0x01: p = "DK1"; break;
924 case 0x02: p = "DK2"; break;
925 case 0x03: p = "DK3"; break;
926 case 0x04: p = "I"; break;
927 case 0x05: p = "L"; break;
928 case 0x06: p = "BTSC"; break;
929 case 0x07: p = "EIAJ"; break;
930 case 0x08: p = "A2-M"; break;
931 case 0x09: p = "FM Radio"; break;
932 case 0x0f: p = "automatic standard and mode detection"; break;
933 default: p = "undefined";
934 }
935 v4l_info(client, "Configured audio system: %s\n", p);
936 }
937
938 if (aud_input) {
939 v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
940 } else {
941 v4l_info(client, "Specified audio input: External\n");
942 }
943
944 switch (pref_mode & 0xf) {
945 case 0: p = "mono/language A"; break;
946 case 1: p = "language B"; break;
947 case 2: p = "language C"; break;
948 case 3: p = "analog fallback"; break;
949 case 4: p = "stereo"; break;
950 case 5: p = "language AC"; break;
951 case 6: p = "language BC"; break;
952 case 7: p = "language AB"; break;
953 default: p = "undefined";
954 }
955 v4l_info(client, "Preferred audio mode: %s\n", p);
956
957 if ((audio_config & 0xf) == 0xf) {
958 switch ((afc0 >> 3) & 0x3) {
959 case 0: p = "system DK"; break;
960 case 1: p = "system L"; break;
961 case 2: p = "autodetect"; break;
962 default: p = "undefined";
963 }
964 v4l_info(client, "Selected 65 MHz format: %s\n", p);
965
966 switch (afc0 & 0x7) {
967 case 0: p = "chroma"; break;
968 case 1: p = "BTSC"; break;
969 case 2: p = "EIAJ"; break;
970 case 3: p = "A2-M"; break;
971 case 4: p = "autodetect"; break;
972 default: p = "undefined";
973 }
974 v4l_info(client, "Selected 45 MHz format: %s\n", p);
975 }
976}
977
978/* ----------------------------------------------------------------------- */
979
644static int cx25840_command(struct i2c_client *client, unsigned int cmd, 980static int cx25840_command(struct i2c_client *client, unsigned int cmd,
645 void *arg) 981 void *arg)
646{ 982{
@@ -660,6 +996,8 @@ static int cx25840_command(struct i2c_client *client, unsigned int cmd,
660 state->is_initialized = 1; 996 state->is_initialized = 1;
661 if (state->is_cx25836) 997 if (state->is_cx25836)
662 cx25836_initialize(client); 998 cx25836_initialize(client);
999 else if (state->is_cx23885)
1000 cx23885_initialize(client);
663 else 1001 else
664 cx25840_initialize(client); 1002 cx25840_initialize(client);
665 } 1003 }
@@ -677,6 +1015,7 @@ static int cx25840_command(struct i2c_client *client, unsigned int cmd,
677 return -EINVAL; 1015 return -EINVAL;
678 if (!capable(CAP_SYS_ADMIN)) 1016 if (!capable(CAP_SYS_ADMIN))
679 return -EPERM; 1017 return -EPERM;
1018
680 if (cmd == VIDIOC_DBG_G_REGISTER) 1019 if (cmd == VIDIOC_DBG_G_REGISTER)
681 reg->val = cx25840_read(client, reg->reg & 0x0fff); 1020 reg->val = cx25840_read(client, reg->reg & 0x0fff);
682 else 1021 else
@@ -693,14 +1032,26 @@ static int cx25840_command(struct i2c_client *client, unsigned int cmd,
693 1032
694 case VIDIOC_STREAMON: 1033 case VIDIOC_STREAMON:
695 v4l_dbg(1, cx25840_debug, client, "enable output\n"); 1034 v4l_dbg(1, cx25840_debug, client, "enable output\n");
696 cx25840_write(client, 0x115, state->is_cx25836 ? 0x0c : 0x8c); 1035 if (state->is_cx23885) {
697 cx25840_write(client, 0x116, state->is_cx25836 ? 0x04 : 0x07); 1036 u8 v = (cx25840_read(client, 0x421) | 0x0b);
1037 cx25840_write(client, 0x421, v);
1038 } else {
1039 cx25840_write(client, 0x115,
1040 state->is_cx25836 ? 0x0c : 0x8c);
1041 cx25840_write(client, 0x116,
1042 state->is_cx25836 ? 0x04 : 0x07);
1043 }
698 break; 1044 break;
699 1045
700 case VIDIOC_STREAMOFF: 1046 case VIDIOC_STREAMOFF:
701 v4l_dbg(1, cx25840_debug, client, "disable output\n"); 1047 v4l_dbg(1, cx25840_debug, client, "disable output\n");
702 cx25840_write(client, 0x115, 0x00); 1048 if (state->is_cx23885) {
703 cx25840_write(client, 0x116, 0x00); 1049 u8 v = cx25840_read(client, 0x421) & ~(0x0b);
1050 cx25840_write(client, 0x421, v);
1051 } else {
1052 cx25840_write(client, 0x115, 0x00);
1053 cx25840_write(client, 0x116, 0x00);
1054 }
704 break; 1055 break;
705 1056
706 case VIDIOC_LOG_STATUS: 1057 case VIDIOC_LOG_STATUS:
@@ -863,6 +1214,8 @@ static int cx25840_command(struct i2c_client *client, unsigned int cmd,
863 case VIDIOC_INT_RESET: 1214 case VIDIOC_INT_RESET:
864 if (state->is_cx25836) 1215 if (state->is_cx25836)
865 cx25836_initialize(client); 1216 cx25836_initialize(client);
1217 else if (state->is_cx23885)
1218 cx23885_initialize(client);
866 else 1219 else
867 cx25840_initialize(client); 1220 cx25840_initialize(client);
868 break; 1221 break;
@@ -879,35 +1232,21 @@ static int cx25840_command(struct i2c_client *client, unsigned int cmd,
879 1232
880/* ----------------------------------------------------------------------- */ 1233/* ----------------------------------------------------------------------- */
881 1234
882static struct i2c_driver i2c_driver_cx25840; 1235static int cx25840_probe(struct i2c_client *client)
883
884static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
885 int kind)
886{ 1236{
887 struct i2c_client *client;
888 struct cx25840_state *state; 1237 struct cx25840_state *state;
889 u32 id; 1238 u32 id;
890 u16 device_id; 1239 u16 device_id;
891 1240
892 /* Check if the adapter supports the needed features 1241 /* Check if the adapter supports the needed features */
893 * Not until kernel version 2.6.11 did the bit-algo 1242 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
894 * correctly report that it would do an I2C-level xfer */ 1243 return -EIO;
895 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
896 return 0;
897
898 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
899 if (client == 0)
900 return -ENOMEM;
901
902 client->addr = address;
903 client->adapter = adapter;
904 client->driver = &i2c_driver_cx25840;
905 snprintf(client->name, sizeof(client->name) - 1, "cx25840");
906 1244
907 v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1); 1245 v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
908 1246
909 device_id = cx25840_read(client, 0x101) << 8; 1247 device_id = cx25840_read(client, 0x101) << 8;
910 device_id |= cx25840_read(client, 0x100); 1248 device_id |= cx25840_read(client, 0x100);
1249 v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
911 1250
912 /* The high byte of the device ID should be 1251 /* The high byte of the device ID should be
913 * 0x83 for the cx2583x and 0x84 for the cx2584x */ 1252 * 0x83 for the cx2583x and 0x84 for the cx2584x */
@@ -916,16 +1255,18 @@ static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
916 } 1255 }
917 else if ((device_id & 0xff00) == 0x8400) { 1256 else if ((device_id & 0xff00) == 0x8400) {
918 id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf); 1257 id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
1258 } else if (device_id == 0x0000) {
1259 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
1260 } else if (device_id == 0x1313) {
1261 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
919 } 1262 }
920 else { 1263 else {
921 v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n"); 1264 v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
922 kfree(client); 1265 return -ENODEV;
923 return 0;
924 } 1266 }
925 1267
926 state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL); 1268 state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
927 if (state == NULL) { 1269 if (state == NULL) {
928 kfree(client);
929 return -ENOMEM; 1270 return -ENOMEM;
930 } 1271 }
931 1272
@@ -939,6 +1280,7 @@ static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
939 i2c_set_clientdata(client, state); 1280 i2c_set_clientdata(client, state);
940 state->c = client; 1281 state->c = client;
941 state->is_cx25836 = ((device_id & 0xff00) == 0x8300); 1282 state->is_cx25836 = ((device_id & 0xff00) == 0x8300);
1283 state->is_cx23885 = (device_id == 0x0000) || (device_id == 0x1313);
942 state->vid_input = CX25840_COMPOSITE7; 1284 state->vid_input = CX25840_COMPOSITE7;
943 state->aud_input = CX25840_AUDIO8; 1285 state->aud_input = CX25840_AUDIO8;
944 state->audclk_freq = 48000; 1286 state->audclk_freq = 48000;
@@ -949,250 +1291,19 @@ static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
949 state->id = id; 1291 state->id = id;
950 state->rev = device_id; 1292 state->rev = device_id;
951 1293
952 i2c_attach_client(client);
953
954 return 0;
955}
956
957static int cx25840_attach_adapter(struct i2c_adapter *adapter)
958{
959 if (adapter->class & I2C_CLASS_TV_ANALOG)
960 return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
961 return 0; 1294 return 0;
962} 1295}
963 1296
964static int cx25840_detach_client(struct i2c_client *client) 1297static int cx25840_remove(struct i2c_client *client)
965{ 1298{
966 struct cx25840_state *state = i2c_get_clientdata(client); 1299 kfree(i2c_get_clientdata(client));
967 int err;
968
969 err = i2c_detach_client(client);
970 if (err) {
971 return err;
972 }
973
974 kfree(state);
975 kfree(client);
976
977 return 0; 1300 return 0;
978} 1301}
979 1302
980/* ----------------------------------------------------------------------- */ 1303static struct v4l2_i2c_driver_data v4l2_i2c_data = {
981 1304 .name = "cx25840",
982static struct i2c_driver i2c_driver_cx25840 = { 1305 .driverid = I2C_DRIVERID_CX25840,
983 .driver = {
984 .name = "cx25840",
985 },
986 .id = I2C_DRIVERID_CX25840,
987 .attach_adapter = cx25840_attach_adapter,
988 .detach_client = cx25840_detach_client,
989 .command = cx25840_command, 1306 .command = cx25840_command,
1307 .probe = cx25840_probe,
1308 .remove = cx25840_remove,
990}; 1309};
991
992
993static int __init m__init(void)
994{
995 return i2c_add_driver(&i2c_driver_cx25840);
996}
997
998static void __exit m__exit(void)
999{
1000 i2c_del_driver(&i2c_driver_cx25840);
1001}
1002
1003module_init(m__init);
1004module_exit(m__exit);
1005
1006/* ----------------------------------------------------------------------- */
1007
1008static void log_video_status(struct i2c_client *client)
1009{
1010 static const char *const fmt_strs[] = {
1011 "0x0",
1012 "NTSC-M", "NTSC-J", "NTSC-4.43",
1013 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
1014 "0x9", "0xA", "0xB",
1015 "SECAM",
1016 "0xD", "0xE", "0xF"
1017 };
1018
1019 struct cx25840_state *state = i2c_get_clientdata(client);
1020 u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
1021 u8 gen_stat1 = cx25840_read(client, 0x40d);
1022 u8 gen_stat2 = cx25840_read(client, 0x40e);
1023 int vid_input = state->vid_input;
1024
1025 v4l_info(client, "Video signal: %spresent\n",
1026 (gen_stat2 & 0x20) ? "" : "not ");
1027 v4l_info(client, "Detected format: %s\n",
1028 fmt_strs[gen_stat1 & 0xf]);
1029
1030 v4l_info(client, "Specified standard: %s\n",
1031 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
1032
1033 if (vid_input >= CX25840_COMPOSITE1 &&
1034 vid_input <= CX25840_COMPOSITE8) {
1035 v4l_info(client, "Specified video input: Composite %d\n",
1036 vid_input - CX25840_COMPOSITE1 + 1);
1037 } else {
1038 v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
1039 (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
1040 }
1041
1042 v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
1043}
1044
1045/* ----------------------------------------------------------------------- */
1046
1047static void log_audio_status(struct i2c_client *client)
1048{
1049 struct cx25840_state *state = i2c_get_clientdata(client);
1050 u8 download_ctl = cx25840_read(client, 0x803);
1051 u8 mod_det_stat0 = cx25840_read(client, 0x804);
1052 u8 mod_det_stat1 = cx25840_read(client, 0x805);
1053 u8 audio_config = cx25840_read(client, 0x808);
1054 u8 pref_mode = cx25840_read(client, 0x809);
1055 u8 afc0 = cx25840_read(client, 0x80b);
1056 u8 mute_ctl = cx25840_read(client, 0x8d3);
1057 int aud_input = state->aud_input;
1058 char *p;
1059
1060 switch (mod_det_stat0) {
1061 case 0x00: p = "mono"; break;
1062 case 0x01: p = "stereo"; break;
1063 case 0x02: p = "dual"; break;
1064 case 0x04: p = "tri"; break;
1065 case 0x10: p = "mono with SAP"; break;
1066 case 0x11: p = "stereo with SAP"; break;
1067 case 0x12: p = "dual with SAP"; break;
1068 case 0x14: p = "tri with SAP"; break;
1069 case 0xfe: p = "forced mode"; break;
1070 default: p = "not defined";
1071 }
1072 v4l_info(client, "Detected audio mode: %s\n", p);
1073
1074 switch (mod_det_stat1) {
1075 case 0x00: p = "not defined"; break;
1076 case 0x01: p = "EIAJ"; break;
1077 case 0x02: p = "A2-M"; break;
1078 case 0x03: p = "A2-BG"; break;
1079 case 0x04: p = "A2-DK1"; break;
1080 case 0x05: p = "A2-DK2"; break;
1081 case 0x06: p = "A2-DK3"; break;
1082 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
1083 case 0x08: p = "AM-L"; break;
1084 case 0x09: p = "NICAM-BG"; break;
1085 case 0x0a: p = "NICAM-DK"; break;
1086 case 0x0b: p = "NICAM-I"; break;
1087 case 0x0c: p = "NICAM-L"; break;
1088 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
1089 case 0x0e: p = "IF FM Radio"; break;
1090 case 0x0f: p = "BTSC"; break;
1091 case 0x10: p = "high-deviation FM"; break;
1092 case 0x11: p = "very high-deviation FM"; break;
1093 case 0xfd: p = "unknown audio standard"; break;
1094 case 0xfe: p = "forced audio standard"; break;
1095 case 0xff: p = "no detected audio standard"; break;
1096 default: p = "not defined";
1097 }
1098 v4l_info(client, "Detected audio standard: %s\n", p);
1099 v4l_info(client, "Audio muted: %s\n",
1100 (state->unmute_volume >= 0) ? "yes" : "no");
1101 v4l_info(client, "Audio microcontroller: %s\n",
1102 (download_ctl & 0x10) ?
1103 ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
1104
1105 switch (audio_config >> 4) {
1106 case 0x00: p = "undefined"; break;
1107 case 0x01: p = "BTSC"; break;
1108 case 0x02: p = "EIAJ"; break;
1109 case 0x03: p = "A2-M"; break;
1110 case 0x04: p = "A2-BG"; break;
1111 case 0x05: p = "A2-DK1"; break;
1112 case 0x06: p = "A2-DK2"; break;
1113 case 0x07: p = "A2-DK3"; break;
1114 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1115 case 0x09: p = "AM-L"; break;
1116 case 0x0a: p = "NICAM-BG"; break;
1117 case 0x0b: p = "NICAM-DK"; break;
1118 case 0x0c: p = "NICAM-I"; break;
1119 case 0x0d: p = "NICAM-L"; break;
1120 case 0x0e: p = "FM radio"; break;
1121 case 0x0f: p = "automatic detection"; break;
1122 default: p = "undefined";
1123 }
1124 v4l_info(client, "Configured audio standard: %s\n", p);
1125
1126 if ((audio_config >> 4) < 0xF) {
1127 switch (audio_config & 0xF) {
1128 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1129 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1130 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1131 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1132 case 0x04: p = "STEREO"; break;
1133 case 0x05: p = "DUAL1 (AB)"; break;
1134 case 0x06: p = "DUAL2 (AC) (FM)"; break;
1135 case 0x07: p = "DUAL3 (BC) (FM)"; break;
1136 case 0x08: p = "DUAL4 (AC) (AM)"; break;
1137 case 0x09: p = "DUAL5 (BC) (AM)"; break;
1138 case 0x0a: p = "SAP"; break;
1139 default: p = "undefined";
1140 }
1141 v4l_info(client, "Configured audio mode: %s\n", p);
1142 } else {
1143 switch (audio_config & 0xF) {
1144 case 0x00: p = "BG"; break;
1145 case 0x01: p = "DK1"; break;
1146 case 0x02: p = "DK2"; break;
1147 case 0x03: p = "DK3"; break;
1148 case 0x04: p = "I"; break;
1149 case 0x05: p = "L"; break;
1150 case 0x06: p = "BTSC"; break;
1151 case 0x07: p = "EIAJ"; break;
1152 case 0x08: p = "A2-M"; break;
1153 case 0x09: p = "FM Radio"; break;
1154 case 0x0f: p = "automatic standard and mode detection"; break;
1155 default: p = "undefined";
1156 }
1157 v4l_info(client, "Configured audio system: %s\n", p);
1158 }
1159
1160 if (aud_input) {
1161 v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
1162 } else {
1163 v4l_info(client, "Specified audio input: External\n");
1164 }
1165
1166 switch (pref_mode & 0xf) {
1167 case 0: p = "mono/language A"; break;
1168 case 1: p = "language B"; break;
1169 case 2: p = "language C"; break;
1170 case 3: p = "analog fallback"; break;
1171 case 4: p = "stereo"; break;
1172 case 5: p = "language AC"; break;
1173 case 6: p = "language BC"; break;
1174 case 7: p = "language AB"; break;
1175 default: p = "undefined";
1176 }
1177 v4l_info(client, "Preferred audio mode: %s\n", p);
1178
1179 if ((audio_config & 0xf) == 0xf) {
1180 switch ((afc0 >> 3) & 0x3) {
1181 case 0: p = "system DK"; break;
1182 case 1: p = "system L"; break;
1183 case 2: p = "autodetect"; break;
1184 default: p = "undefined";
1185 }
1186 v4l_info(client, "Selected 65 MHz format: %s\n", p);
1187
1188 switch (afc0 & 0x7) {
1189 case 0: p = "chroma"; break;
1190 case 1: p = "BTSC"; break;
1191 case 2: p = "EIAJ"; break;
1192 case 3: p = "A2-M"; break;
1193 case 4: p = "autodetect"; break;
1194 default: p = "undefined";
1195 }
1196 v4l_info(client, "Selected 45 MHz format: %s\n", p);
1197 }
1198}
diff --git a/drivers/media/video/cx25840/cx25840-core.h b/drivers/media/video/cx25840/cx25840-core.h
index ea669b1f084d..95093edc9186 100644
--- a/drivers/media/video/cx25840/cx25840-core.h
+++ b/drivers/media/video/cx25840/cx25840-core.h
@@ -47,6 +47,7 @@ struct cx25840_state {
47 u32 id; 47 u32 id;
48 u32 rev; 48 u32 rev;
49 int is_cx25836; 49 int is_cx25836;
50 int is_cx23885;
50 int is_initialized; 51 int is_initialized;
51 wait_queue_head_t fw_wait; /* wake up when the fw load is finished */ 52 wait_queue_head_t fw_wait; /* wake up when the fw load is finished */
52 struct work_struct fw_work; /* work entry for fw load */ 53 struct work_struct fw_work; /* work entry for fw load */
diff --git a/drivers/media/video/cx25840/cx25840-firmware.c b/drivers/media/video/cx25840/cx25840-firmware.c
index e852024a5ea3..1ddf724a2c74 100644
--- a/drivers/media/video/cx25840/cx25840-firmware.c
+++ b/drivers/media/video/cx25840/cx25840-firmware.c
@@ -24,6 +24,7 @@
24#include "cx25840-core.h" 24#include "cx25840-core.h"
25 25
26#define FWFILE "v4l-cx25840.fw" 26#define FWFILE "v4l-cx25840.fw"
27#define FWFILE_CX23885 "v4l-cx23885-avcore-01.fw"
27 28
28/* 29/*
29 * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the 30 * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the
@@ -92,10 +93,14 @@ static int fw_write(struct i2c_client *client, u8 * data, int size)
92 93
93int cx25840_loadfw(struct i2c_client *client) 94int cx25840_loadfw(struct i2c_client *client)
94{ 95{
96 struct cx25840_state *state = i2c_get_clientdata(client);
95 const struct firmware *fw = NULL; 97 const struct firmware *fw = NULL;
96 u8 buffer[4], *ptr; 98 u8 buffer[4], *ptr;
97 int size, send, retval; 99 int size, send, retval;
98 100
101 if (state->is_cx23885)
102 firmware = FWFILE_CX23885;
103
99 if (request_firmware(&fw, firmware, FWDEV(client)) != 0) { 104 if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
100 v4l_err(client, "unable to open firmware %s\n", firmware); 105 v4l_err(client, "unable to open firmware %s\n", firmware);
101 return -EINVAL; 106 return -EINVAL;
diff --git a/drivers/media/video/cx25840/cx25840-vbi.c b/drivers/media/video/cx25840/cx25840-vbi.c
index ced13febed89..6828f59b9d83 100644
--- a/drivers/media/video/cx25840/cx25840-vbi.c
+++ b/drivers/media/video/cx25840/cx25840-vbi.c
@@ -180,7 +180,7 @@ void cx25840_vbi_setup(struct i2c_client *client)
180 fsc/1000000,fsc%1000000); 180 fsc/1000000,fsc%1000000);
181 181
182 v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, " 182 v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, "
183 "vblank %i , vactive %i, vblank656 %i, src_dec %i," 183 "vblank %i, vactive %i, vblank656 %i, src_dec %i, "
184 "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x," 184 "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x,"
185 " sc 0x%06x\n", 185 " sc 0x%06x\n",
186 hblank, hactive, vblank, vactive, vblank656, 186 hblank, hactive, vblank, vactive, vblank656,
diff --git a/drivers/media/video/cx88/Kconfig b/drivers/media/video/cx88/Kconfig
index ceb31d4a2512..49d3813a9b48 100644
--- a/drivers/media/video/cx88/Kconfig
+++ b/drivers/media/video/cx88/Kconfig
@@ -8,6 +8,7 @@ config VIDEO_CX88
8 select VIDEO_TUNER 8 select VIDEO_TUNER
9 select VIDEO_TVEEPROM 9 select VIDEO_TVEEPROM
10 select VIDEO_IR 10 select VIDEO_IR
11 select VIDEO_WM8775 if VIDEO_HELPER_CHIPS_AUTO
11 ---help--- 12 ---help---
12 This is a video4linux driver for Conexant 2388x based 13 This is a video4linux driver for Conexant 2388x based
13 TV cards. 14 TV cards.
diff --git a/drivers/media/video/cx88/cx88-alsa.c b/drivers/media/video/cx88/cx88-alsa.c
index 40ffd7a5579a..8735227f7e47 100644
--- a/drivers/media/video/cx88/cx88-alsa.c
+++ b/drivers/media/video/cx88/cx88-alsa.c
@@ -417,7 +417,7 @@ static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
417 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC); 417 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
418 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 418 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
419 419
420 buf->vb.state = STATE_PREPARED; 420 buf->vb.state = VIDEOBUF_PREPARED;
421 421
422 chip->buf = buf; 422 chip->buf = buf;
423 chip->dma_risc = dma; 423 chip->dma_risc = dma;
diff --git a/drivers/media/video/cx88/cx88-blackbird.c b/drivers/media/video/cx88/cx88-blackbird.c
index f802b5653569..a99e9d5950aa 100644
--- a/drivers/media/video/cx88/cx88-blackbird.c
+++ b/drivers/media/video/cx88/cx88-blackbird.c
@@ -307,7 +307,7 @@ static int register_read(struct cx88_core *core, u32 address, u32 *value)
307 307
308/* ------------------------------------------------------------------ */ 308/* ------------------------------------------------------------------ */
309 309
310static int blackbird_mbox_func(void *priv, int command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA]) 310static int blackbird_mbox_func(void *priv, u32 command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
311{ 311{
312 struct cx8802_dev *dev = priv; 312 struct cx8802_dev *dev = priv;
313 unsigned long timeout; 313 unsigned long timeout;
@@ -536,11 +536,12 @@ static int blackbird_initialize_codec(struct cx8802_dev *dev)
536 dprintk(1,"Initialize codec\n"); 536 dprintk(1,"Initialize codec\n");
537 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */ 537 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
538 if (retval < 0) { 538 if (retval < 0) {
539
540 dev->mpeg_active = 0;
541
539 /* ping was not successful, reset and upload firmware */ 542 /* ping was not successful, reset and upload firmware */
540 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */ 543 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
541 msleep(1);
542 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */ 544 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
543 msleep(1);
544 retval = blackbird_load_firmware(dev); 545 retval = blackbird_load_firmware(dev);
545 if (retval < 0) 546 if (retval < 0)
546 return retval; 547 return retval;
@@ -562,7 +563,6 @@ static int blackbird_initialize_codec(struct cx8802_dev *dev)
562 } 563 }
563 dprintk(0, "Firmware version is 0x%08x\n", version); 564 dprintk(0, "Firmware version is 0x%08x\n", version);
564 } 565 }
565 msleep(1);
566 566
567 cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */ 567 cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
568 cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */ 568 cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
@@ -570,40 +570,68 @@ static int blackbird_initialize_codec(struct cx8802_dev *dev)
570 cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */ 570 cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
571 571
572 blackbird_codec_settings(dev); 572 blackbird_codec_settings(dev);
573 msleep(1);
574 573
575 /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
576 blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
577 blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */
578 blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0, 574 blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
579 BLACKBIRD_FIELD1_SAA7115, 575 BLACKBIRD_FIELD1_SAA7115,
580 BLACKBIRD_FIELD2_SAA7115 576 BLACKBIRD_FIELD2_SAA7115
581 ); 577 );
582 578
583 /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */
584 blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0, 579 blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
585 BLACKBIRD_CUSTOM_EXTENSION_USR_DATA, 580 BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
587 582
588 /* initialize the video input */ 583 return 0;
589 blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0); 584}
590 585
591 msleep(1); 586static int blackbird_start_codec(struct file *file, void *priv)
587{
588 struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev;
589 struct cx88_core *core = dev->core;
590 /* start capturing to the host interface */
591 u32 reg;
592 592
593 blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE); 593 int i;
594 msleep(1); 594 int lastchange = -1;
595 blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE); 595 int lastval = 0;
596 msleep(1); 596
597 for (i = 0; (i < 10) && (i < (lastchange + 4)); i++) {
598 reg = cx_read(AUD_STATUS);
599
600 dprintk(1, "AUD_STATUS:%dL: 0x%x\n", i, reg);
601 if ((reg & 0x0F) != lastval) {
602 lastval = reg & 0x0F;
603 lastchange = i;
604 }
605 msleep(100);
606 }
607
608 /* unmute audio source */
609 cx_clear(AUD_VOL_CTL, (1 << 6));
610
611 blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0, 0);
612
613 /* initialize the video input */
614 blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
597 615
598 /* start capturing to the host interface */ 616 /* start capturing to the host interface */
599 /* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */
600 blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 617 blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
601 BLACKBIRD_MPEG_CAPTURE, 618 BLACKBIRD_MPEG_CAPTURE,
602 BLACKBIRD_RAW_BITS_NONE 619 BLACKBIRD_RAW_BITS_NONE
603 ); 620 );
604 msleep(10);
605 621
606 blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0); 622 dev->mpeg_active = 1;
623 return 0;
624}
625
626static int blackbird_stop_codec(struct cx8802_dev *dev)
627{
628 blackbird_api_cmd(dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
629 BLACKBIRD_END_NOW,
630 BLACKBIRD_MPEG_CAPTURE,
631 BLACKBIRD_RAW_BITS_NONE
632 );
633
634 dev->mpeg_active = 0;
607 return 0; 635 return 0;
608} 636}
609 637
@@ -833,6 +861,10 @@ static int vidioc_s_ext_ctrls (struct file *file, void *priv,
833 861
834 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG) 862 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
835 return -EINVAL; 863 return -EINVAL;
864
865 if (dev->mpeg_active)
866 blackbird_stop_codec(dev);
867
836 p = dev->params; 868 p = dev->params;
837 err = cx2341x_ext_ctrls(&p, 0, f, VIDIOC_S_EXT_CTRLS); 869 err = cx2341x_ext_ctrls(&p, 0, f, VIDIOC_S_EXT_CTRLS);
838 if (!err) { 870 if (!err) {
@@ -864,10 +896,9 @@ static int vidioc_s_frequency (struct file *file, void *priv,
864 struct cx8802_dev *dev = fh->dev; 896 struct cx8802_dev *dev = fh->dev;
865 struct cx88_core *core = dev->core; 897 struct cx88_core *core = dev->core;
866 898
867 blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, 899 if (dev->mpeg_active)
868 BLACKBIRD_END_NOW, 900 blackbird_stop_codec(dev);
869 BLACKBIRD_MPEG_CAPTURE, 901
870 BLACKBIRD_RAW_BITS_NONE);
871 cx88_set_freq (core,f); 902 cx88_set_freq (core,f);
872 blackbird_initialize_codec(dev); 903 blackbird_initialize_codec(dev);
873 cx88_set_scale(dev->core, dev->width, dev->height, 904 cx88_set_scale(dev->core, dev->width, dev->height,
@@ -1073,15 +1104,11 @@ static int mpeg_open(struct inode *inode, struct file *file)
1073static int mpeg_release(struct inode *inode, struct file *file) 1104static int mpeg_release(struct inode *inode, struct file *file)
1074{ 1105{
1075 struct cx8802_fh *fh = file->private_data; 1106 struct cx8802_fh *fh = file->private_data;
1076 struct cx8802_dev *dev = NULL; 1107 struct cx8802_dev *dev = fh->dev;
1077 struct cx8802_driver *drv = NULL; 1108 struct cx8802_driver *drv = NULL;
1078 1109
1079 /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */ 1110 if (dev->mpeg_active)
1080 blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, 1111 blackbird_stop_codec(dev);
1081 BLACKBIRD_END_NOW,
1082 BLACKBIRD_MPEG_CAPTURE,
1083 BLACKBIRD_RAW_BITS_NONE
1084 );
1085 1112
1086 cx8802_cancel_buffers(fh->dev); 1113 cx8802_cancel_buffers(fh->dev);
1087 /* stop mpeg capture */ 1114 /* stop mpeg capture */
@@ -1107,6 +1134,10 @@ static ssize_t
1107mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos) 1134mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1108{ 1135{
1109 struct cx8802_fh *fh = file->private_data; 1136 struct cx8802_fh *fh = file->private_data;
1137 struct cx8802_dev *dev = fh->dev;
1138
1139 if (!dev->mpeg_active)
1140 blackbird_start_codec(file, fh);
1110 1141
1111 return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0, 1142 return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
1112 file->f_flags & O_NONBLOCK); 1143 file->f_flags & O_NONBLOCK);
@@ -1282,6 +1313,7 @@ static int cx8802_blackbird_probe(struct cx8802_driver *drv)
1282 core->name); 1313 core->name);
1283 host_setup(dev->core); 1314 host_setup(dev->core);
1284 1315
1316 blackbird_initialize_codec(dev);
1285 blackbird_register_video(dev); 1317 blackbird_register_video(dev);
1286 1318
1287 /* initial device configuration: needed ? */ 1319 /* initial device configuration: needed ? */
diff --git a/drivers/media/video/cx88/cx88-cards.c b/drivers/media/video/cx88/cx88-cards.c
index a4eb6a87a761..e6b7f518c56e 100644
--- a/drivers/media/video/cx88/cx88-cards.c
+++ b/drivers/media/video/cx88/cx88-cards.c
@@ -26,6 +26,7 @@
26#include <linux/delay.h> 26#include <linux/delay.h>
27 27
28#include "cx88.h" 28#include "cx88.h"
29#include "tea5767.h"
29 30
30static unsigned int tuner[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET }; 31static unsigned int tuner[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
31static unsigned int radio[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET }; 32static unsigned int radio[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
@@ -245,6 +246,10 @@ static const struct cx88_board cx88_boards[] = {
245 }}, 246 }},
246 .radio = { 247 .radio = {
247 .type = CX88_RADIO, 248 .type = CX88_RADIO,
249 .vmux = 3,
250 .gpio0 = 0x000040bf,
251 .gpio1 = 0x000080c0,
252 .gpio2 = 0x0000ff20,
248 }, 253 },
249 }, 254 },
250 [CX88_BOARD_WINFAST_DV2000] = { 255 [CX88_BOARD_WINFAST_DV2000] = {
@@ -297,22 +302,22 @@ static const struct cx88_board cx88_boards[] = {
297 .type = CX88_VMUX_TELEVISION, 302 .type = CX88_VMUX_TELEVISION,
298 .vmux = 0, 303 .vmux = 0,
299 .gpio0 = 0x0000bde2, 304 .gpio0 = 0x0000bde2,
300 .extadc = 1, 305 .audioroute = 1,
301 },{ 306 },{
302 .type = CX88_VMUX_COMPOSITE1, 307 .type = CX88_VMUX_COMPOSITE1,
303 .vmux = 1, 308 .vmux = 1,
304 .gpio0 = 0x0000bde6, 309 .gpio0 = 0x0000bde6,
305 .extadc = 1, 310 .audioroute = 1,
306 },{ 311 },{
307 .type = CX88_VMUX_SVIDEO, 312 .type = CX88_VMUX_SVIDEO,
308 .vmux = 2, 313 .vmux = 2,
309 .gpio0 = 0x0000bde6, 314 .gpio0 = 0x0000bde6,
310 .extadc = 1, 315 .audioroute = 1,
311 }}, 316 }},
312 .radio = { 317 .radio = {
313 .type = CX88_RADIO, 318 .type = CX88_RADIO,
314 .gpio0 = 0x0000bd62, 319 .gpio0 = 0x0000bd62,
315 .extadc = 1, 320 .audioroute = 1,
316 }, 321 },
317 .mpeg = CX88_MPEG_BLACKBIRD, 322 .mpeg = CX88_MPEG_BLACKBIRD,
318 }, 323 },
@@ -373,7 +378,7 @@ static const struct cx88_board cx88_boards[] = {
373 .type = CX88_VMUX_SVIDEO, 378 .type = CX88_VMUX_SVIDEO,
374 .vmux = 2, 379 .vmux = 2,
375 .gpio0 = 0x0000fde6, // 0x0000fda6 L,R RCA audio in? 380 .gpio0 = 0x0000fde6, // 0x0000fda6 L,R RCA audio in?
376 .extadc = 1, 381 .audioroute = 1,
377 }}, 382 }},
378 .radio = { 383 .radio = {
379 .type = CX88_RADIO, 384 .type = CX88_RADIO,
@@ -544,7 +549,7 @@ static const struct cx88_board cx88_boards[] = {
544 .input = {{ 549 .input = {{
545 .type = CX88_VMUX_TELEVISION, 550 .type = CX88_VMUX_TELEVISION,
546 .vmux = 0, 551 .vmux = 0,
547 .extadc = 1, 552 .audioroute = 1,
548 }}, 553 }},
549 .mpeg = CX88_MPEG_BLACKBIRD, 554 .mpeg = CX88_MPEG_BLACKBIRD,
550 }, 555 },
@@ -667,22 +672,22 @@ static const struct cx88_board cx88_boards[] = {
667 .type = CX88_VMUX_TELEVISION, 672 .type = CX88_VMUX_TELEVISION,
668 .vmux = 0, 673 .vmux = 0,
669 .gpio0 = 0x00009d80, 674 .gpio0 = 0x00009d80,
670 .extadc = 1, 675 .audioroute = 1,
671 },{ 676 },{
672 .type = CX88_VMUX_COMPOSITE1, 677 .type = CX88_VMUX_COMPOSITE1,
673 .vmux = 1, 678 .vmux = 1,
674 .gpio0 = 0x00009d76, 679 .gpio0 = 0x00009d76,
675 .extadc = 1, 680 .audioroute = 1,
676 },{ 681 },{
677 .type = CX88_VMUX_SVIDEO, 682 .type = CX88_VMUX_SVIDEO,
678 .vmux = 2, 683 .vmux = 2,
679 .gpio0 = 0x00009d76, 684 .gpio0 = 0x00009d76,
680 .extadc = 1, 685 .audioroute = 1,
681 }}, 686 }},
682 .radio = { 687 .radio = {
683 .type = CX88_RADIO, 688 .type = CX88_RADIO,
684 .gpio0 = 0x00009d00, 689 .gpio0 = 0x00009d00,
685 .extadc = 1, 690 .audioroute = 1,
686 }, 691 },
687 .mpeg = CX88_MPEG_BLACKBIRD, 692 .mpeg = CX88_MPEG_BLACKBIRD,
688 }, 693 },
@@ -821,23 +826,23 @@ static const struct cx88_board cx88_boards[] = {
821 .type = CX88_VMUX_COMPOSITE1, 826 .type = CX88_VMUX_COMPOSITE1,
822 .vmux = 0, 827 .vmux = 0,
823 .gpio0 = 0x0000cd73, 828 .gpio0 = 0x0000cd73,
824 .extadc = 1, 829 .audioroute = 1,
825 },{ 830 },{
826 .type = CX88_VMUX_SVIDEO, 831 .type = CX88_VMUX_SVIDEO,
827 .vmux = 1, 832 .vmux = 1,
828 .gpio0 = 0x0000cd73, 833 .gpio0 = 0x0000cd73,
829 .extadc = 1, 834 .audioroute = 1,
830 },{ 835 },{
831 .type = CX88_VMUX_TELEVISION, 836 .type = CX88_VMUX_TELEVISION,
832 .vmux = 3, 837 .vmux = 3,
833 .gpio0 = 0x0000cdb3, 838 .gpio0 = 0x0000cdb3,
834 .extadc = 1, 839 .audioroute = 1,
835 }}, 840 }},
836 .radio = { 841 .radio = {
837 .type = CX88_RADIO, 842 .type = CX88_RADIO,
838 .vmux = 2, 843 .vmux = 2,
839 .gpio0 = 0x0000cdf3, 844 .gpio0 = 0x0000cdf3,
840 .extadc = 1, 845 .audioroute = 1,
841 }, 846 },
842 .mpeg = CX88_MPEG_BLACKBIRD, 847 .mpeg = CX88_MPEG_BLACKBIRD,
843 }, 848 },
@@ -1105,12 +1110,12 @@ static const struct cx88_board cx88_boards[] = {
1105 .type = CX88_VMUX_COMPOSITE1, 1110 .type = CX88_VMUX_COMPOSITE1,
1106 .vmux = 1, 1111 .vmux = 1,
1107 .gpio0 = 0x3de6, 1112 .gpio0 = 0x3de6,
1108 .extadc = 1, 1113 .audioroute = 1,
1109 },{ 1114 },{
1110 .type = CX88_VMUX_SVIDEO, 1115 .type = CX88_VMUX_SVIDEO,
1111 .vmux = 2, 1116 .vmux = 2,
1112 .gpio0 = 0x3de6, 1117 .gpio0 = 0x3de6,
1113 .extadc = 1, 1118 .audioroute = 1,
1114 }}, 1119 }},
1115 .radio = { 1120 .radio = {
1116 .type = CX88_RADIO, 1121 .type = CX88_RADIO,
@@ -1335,17 +1340,17 @@ static const struct cx88_board cx88_boards[] = {
1335 .type = CX88_VMUX_TELEVISION, 1340 .type = CX88_VMUX_TELEVISION,
1336 .vmux = 0, 1341 .vmux = 0,
1337 .gpio0 = 0xe780, 1342 .gpio0 = 0xe780,
1338 .extadc = 1, 1343 .audioroute = 1,
1339 },{ 1344 },{
1340 .type = CX88_VMUX_COMPOSITE1, 1345 .type = CX88_VMUX_COMPOSITE1,
1341 .vmux = 1, 1346 .vmux = 1,
1342 .gpio0 = 0xe780, 1347 .gpio0 = 0xe780,
1343 .extadc = 1, 1348 .audioroute = 2,
1344 },{ 1349 },{
1345 .type = CX88_VMUX_SVIDEO, 1350 .type = CX88_VMUX_SVIDEO,
1346 .vmux = 2, 1351 .vmux = 2,
1347 .gpio0 = 0xe780, 1352 .gpio0 = 0xe780,
1348 .extadc = 1, 1353 .audioroute = 2,
1349 }}, 1354 }},
1350 /* fixme: Add radio support */ 1355 /* fixme: Add radio support */
1351 .mpeg = CX88_MPEG_DVB | CX88_MPEG_BLACKBIRD, 1356 .mpeg = CX88_MPEG_DVB | CX88_MPEG_BLACKBIRD,
@@ -1370,6 +1375,32 @@ static const struct cx88_board cx88_boards[] = {
1370 .gpio0 = 0x07fa, 1375 .gpio0 = 0x07fa,
1371 }}, 1376 }},
1372 }, 1377 },
1378 [CX88_BOARD_PINNACLE_PCTV_HD_800i] = {
1379 .name = "Pinnacle PCTV HD 800i",
1380 .tuner_type = TUNER_XC5000,
1381 .radio_type = UNSET,
1382 .tuner_addr = ADDR_UNSET,
1383 .radio_addr = ADDR_UNSET,
1384 .input = {{
1385 .type = CX88_VMUX_TELEVISION,
1386 .vmux = 0,
1387 .gpio0 = 0x04fb,
1388 .gpio1 = 0x10ff,
1389 },{
1390 .type = CX88_VMUX_COMPOSITE1,
1391 .vmux = 1,
1392 .gpio0 = 0x04fb,
1393 .gpio1 = 0x10ef,
1394 .audioroute = 1,
1395 },{
1396 .type = CX88_VMUX_SVIDEO,
1397 .vmux = 2,
1398 .gpio0 = 0x04fb,
1399 .gpio1 = 0x10ef,
1400 .audioroute = 1,
1401 }},
1402 .mpeg = CX88_MPEG_DVB,
1403 },
1373}; 1404};
1374 1405
1375/* ------------------------------------------------------------------ */ 1406/* ------------------------------------------------------------------ */
@@ -1679,6 +1710,10 @@ static const struct cx88_subid cx88_subids[] = {
1679 .subvendor = 0x1421, 1710 .subvendor = 0x1421,
1680 .subdevice = 0x0390, 1711 .subdevice = 0x0390,
1681 .card = CX88_BOARD_ADSTECH_PTV_390, 1712 .card = CX88_BOARD_ADSTECH_PTV_390,
1713 },{
1714 .subvendor = 0x11bd,
1715 .subdevice = 0x0051,
1716 .card = CX88_BOARD_PINNACLE_PCTV_HD_800i,
1682 }, 1717 },
1683}; 1718};
1684 1719
@@ -1846,6 +1881,36 @@ static void dvico_fusionhdtv_hybrid_init(struct cx88_core *core)
1846} 1881}
1847 1882
1848/* ----------------------------------------------------------------------- */ 1883/* ----------------------------------------------------------------------- */
1884/* Tuner callback function. Currently only needed for the Pinnacle *
1885 * PCTV HD 800i with an xc5000 sillicon tuner. This is used for both *
1886 * analog tuner attach (tuner-core.c) and dvb tuner attach (cx88-dvb.c) */
1887
1888int cx88_tuner_callback(void *priv, int command, int arg)
1889{
1890 struct i2c_algo_bit_data *i2c_algo = priv;
1891 struct cx88_core *core = i2c_algo->data;
1892
1893 switch(core->boardnr) {
1894 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
1895 if(command == 0) { /* This is the reset command from xc5000 */
1896 /* Reset XC5000 tuner via SYS_RSTO_pin */
1897 cx_write(MO_SRST_IO, 0);
1898 msleep(10);
1899 cx_write(MO_SRST_IO, 1);
1900 return 0;
1901 }
1902 else {
1903 printk(KERN_ERR
1904 "xc5000: unknown tuner callback command.\n");
1905 return -EINVAL;
1906 }
1907 break;
1908 }
1909 return 0; /* Should never be here */
1910}
1911EXPORT_SYMBOL(cx88_tuner_callback);
1912
1913/* ----------------------------------------------------------------------- */
1849 1914
1850static void cx88_card_list(struct cx88_core *core, struct pci_dev *pci) 1915static void cx88_card_list(struct cx88_core *core, struct pci_dev *pci)
1851{ 1916{
@@ -1979,6 +2044,23 @@ static void cx88_card_setup(struct cx88_core *core)
1979 core->name, i); 2044 core->name, i);
1980 } 2045 }
1981 break; 2046 break;
2047 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
2048 {
2049 struct v4l2_priv_tun_config tea5767_cfg;
2050 struct tea5767_ctrl ctl;
2051
2052 memset(&ctl, 0, sizeof(ctl));
2053
2054 ctl.high_cut = 1;
2055 ctl.st_noise = 1;
2056 ctl.deemph_75 = 1;
2057 ctl.xtal_freq = TEA5767_HIGH_LO_13MHz;
2058
2059 tea5767_cfg.tuner = TUNER_TEA5767;
2060 tea5767_cfg.priv = &ctl;
2061
2062 cx88_call_i2c_clients(core, TUNER_SET_CONFIG, &tea5767_cfg);
2063 }
1982 } 2064 }
1983} 2065}
1984 2066
diff --git a/drivers/media/video/cx88/cx88-core.c b/drivers/media/video/cx88/cx88-core.c
index 62e8dd24c5f5..01e2ac98970b 100644
--- a/drivers/media/video/cx88/cx88-core.c
+++ b/drivers/media/video/cx88/cx88-core.c
@@ -220,7 +220,7 @@ cx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf)
220 videobuf_dma_unmap(q, dma); 220 videobuf_dma_unmap(q, dma);
221 videobuf_dma_free(dma); 221 videobuf_dma_free(dma);
222 btcx_riscmem_free((struct pci_dev *)q->dev, &buf->risc); 222 btcx_riscmem_free((struct pci_dev *)q->dev, &buf->risc);
223 buf->vb.state = STATE_NEEDS_INIT; 223 buf->vb.state = VIDEOBUF_NEEDS_INIT;
224} 224}
225 225
226/* ------------------------------------------------------------------ */ 226/* ------------------------------------------------------------------ */
@@ -538,7 +538,7 @@ void cx88_wakeup(struct cx88_core *core,
538 do_gettimeofday(&buf->vb.ts); 538 do_gettimeofday(&buf->vb.ts);
539 dprintk(2,"[%p/%d] wakeup reg=%d buf=%d\n",buf,buf->vb.i, 539 dprintk(2,"[%p/%d] wakeup reg=%d buf=%d\n",buf,buf->vb.i,
540 count, buf->count); 540 count, buf->count);
541 buf->vb.state = STATE_DONE; 541 buf->vb.state = VIDEOBUF_DONE;
542 list_del(&buf->vb.queue); 542 list_del(&buf->vb.queue);
543 wake_up(&buf->vb.done); 543 wake_up(&buf->vb.done);
544 } 544 }
diff --git a/drivers/media/video/cx88/cx88-dvb.c b/drivers/media/video/cx88/cx88-dvb.c
index fce19caf9d04..f7b41eb1bb5a 100644
--- a/drivers/media/video/cx88/cx88-dvb.c
+++ b/drivers/media/video/cx88/cx88-dvb.c
@@ -40,6 +40,8 @@
40#include "cx22702.h" 40#include "cx22702.h"
41#include "or51132.h" 41#include "or51132.h"
42#include "lgdt330x.h" 42#include "lgdt330x.h"
43#include "s5h1409.h"
44#include "xc5000.h"
43#include "nxt200x.h" 45#include "nxt200x.h"
44#include "cx24123.h" 46#include "cx24123.h"
45#include "isl6421.h" 47#include "isl6421.h"
@@ -371,6 +373,22 @@ static struct cx24123_config kworld_dvbs_100_config = {
371 .lnb_polarity = 1, 373 .lnb_polarity = 1,
372}; 374};
373 375
376static struct s5h1409_config pinnacle_pctv_hd_800i_config = {
377 .demod_address = 0x32 >> 1,
378 .output_mode = S5H1409_PARALLEL_OUTPUT,
379 .gpio = S5H1409_GPIO_ON,
380 .qam_if = 44000,
381 .inversion = S5H1409_INVERSION_OFF,
382 .status_mode = S5H1409_DEMODLOCKING,
383 .mpeg_timing = S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK,
384};
385
386static struct xc5000_config pinnacle_pctv_hd_800i_tuner_config = {
387 .i2c_address = 0x64,
388 .if_khz = 5380,
389 .tuner_callback = cx88_tuner_callback,
390};
391
374static int dvb_register(struct cx8802_dev *dev) 392static int dvb_register(struct cx8802_dev *dev)
375{ 393{
376 /* init struct videobuf_dvb */ 394 /* init struct videobuf_dvb */
@@ -625,6 +643,21 @@ static int dvb_register(struct cx8802_dev *dev)
625 dev->dvb.frontend->ops.set_voltage = geniatech_dvbs_set_voltage; 643 dev->dvb.frontend->ops.set_voltage = geniatech_dvbs_set_voltage;
626 } 644 }
627 break; 645 break;
646 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
647 dev->dvb.frontend = dvb_attach(s5h1409_attach,
648 &pinnacle_pctv_hd_800i_config,
649 &dev->core->i2c_adap);
650 if (dev->dvb.frontend != NULL) {
651 /* tuner_config.video_dev must point to
652 * i2c_adap.algo_data
653 */
654 pinnacle_pctv_hd_800i_tuner_config.priv =
655 dev->core->i2c_adap.algo_data;
656 dvb_attach(xc5000_attach, dev->dvb.frontend,
657 &dev->core->i2c_adap,
658 &pinnacle_pctv_hd_800i_tuner_config);
659 }
660 break;
628 default: 661 default:
629 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card isn't supported yet\n", 662 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card isn't supported yet\n",
630 dev->core->name); 663 dev->core->name);
diff --git a/drivers/media/video/cx88/cx88-i2c.c b/drivers/media/video/cx88/cx88-i2c.c
index c8b1c50625f4..566b26af523e 100644
--- a/drivers/media/video/cx88/cx88-i2c.c
+++ b/drivers/media/video/cx88/cx88-i2c.c
@@ -109,26 +109,32 @@ static int attach_inform(struct i2c_client *client)
109 109
110 if (core->board.radio_type != UNSET) { 110 if (core->board.radio_type != UNSET) {
111 if ((core->board.radio_addr==ADDR_UNSET)||(core->board.radio_addr==client->addr)) { 111 if ((core->board.radio_addr==ADDR_UNSET)||(core->board.radio_addr==client->addr)) {
112 tun_setup.mode_mask = T_RADIO; 112 tun_setup.mode_mask = T_RADIO;
113 tun_setup.type = core->board.radio_type; 113 tun_setup.type = core->board.radio_type;
114 tun_setup.addr = core->board.radio_addr; 114 tun_setup.addr = core->board.radio_addr;
115 115 tun_setup.tuner_callback = cx88_tuner_callback;
116 client->driver->command (client, TUNER_SET_TYPE_ADDR, &tun_setup); 116 client->driver->command (client, TUNER_SET_TYPE_ADDR, &tun_setup);
117 } 117 }
118 } 118 }
119 if (core->board.tuner_type != UNSET) { 119 if (core->board.tuner_type != UNSET) {
120 if ((core->board.tuner_addr==ADDR_UNSET)||(core->board.tuner_addr==client->addr)) { 120 if ((core->board.tuner_addr==ADDR_UNSET)||(core->board.tuner_addr==client->addr)) {
121 121
122 tun_setup.mode_mask = T_ANALOG_TV; 122 tun_setup.mode_mask = T_ANALOG_TV;
123 tun_setup.type = core->board.tuner_type; 123 tun_setup.type = core->board.tuner_type;
124 tun_setup.addr = core->board.tuner_addr; 124 tun_setup.addr = core->board.tuner_addr;
125 125 tun_setup.tuner_callback = cx88_tuner_callback;
126 client->driver->command (client,TUNER_SET_TYPE_ADDR, &tun_setup); 126 client->driver->command (client,TUNER_SET_TYPE_ADDR, &tun_setup);
127 } 127 }
128 } 128 }
129 129
130 if (core->board.tda9887_conf) 130 if (core->board.tda9887_conf) {
131 client->driver->command(client, TDA9887_SET_CONFIG, &core->board.tda9887_conf); 131 struct v4l2_priv_tun_config tda9887_cfg;
132
133 tda9887_cfg.tuner = TUNER_TDA9887;
134 tda9887_cfg.priv = &core->board.tda9887_conf;
135
136 client->driver->command(client, TUNER_SET_CONFIG, &tda9887_cfg);
137 }
132 return 0; 138 return 0;
133} 139}
134 140
@@ -176,6 +182,7 @@ static char *i2c_devs[128] = {
176 [ 0xa0 >> 1 ] = "eeprom", 182 [ 0xa0 >> 1 ] = "eeprom",
177 [ 0xc0 >> 1 ] = "tuner (analog)", 183 [ 0xc0 >> 1 ] = "tuner (analog)",
178 [ 0xc2 >> 1 ] = "tuner (analog/dvb)", 184 [ 0xc2 >> 1 ] = "tuner (analog/dvb)",
185 [ 0xc8 >> 1 ] = "xc5000",
179}; 186};
180 187
181static void do_i2c_scan(char *name, struct i2c_client *c) 188static void do_i2c_scan(char *name, struct i2c_client *c)
diff --git a/drivers/media/video/cx88/cx88-input.c b/drivers/media/video/cx88/cx88-input.c
index e52de3968c63..bb0911b4d2f6 100644
--- a/drivers/media/video/cx88/cx88-input.c
+++ b/drivers/media/video/cx88/cx88-input.c
@@ -305,6 +305,11 @@ int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
305 ir->mask_keycode = 0xfa; 305 ir->mask_keycode = 0xfa;
306 ir->polling = 50; /* ms */ 306 ir->polling = 50; /* ms */
307 break; 307 break;
308 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
309 ir_codes = ir_codes_pinnacle_pctv_hd;
310 ir_type = IR_TYPE_RC5;
311 ir->sampling = 1;
312 break;
308 } 313 }
309 314
310 if (NULL == ir_codes) { 315 if (NULL == ir_codes) {
@@ -443,6 +448,7 @@ void cx88_ir_irq(struct cx88_core *core)
443 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1: 448 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
444 case CX88_BOARD_HAUPPAUGE_HVR1100: 449 case CX88_BOARD_HAUPPAUGE_HVR1100:
445 case CX88_BOARD_HAUPPAUGE_HVR3000: 450 case CX88_BOARD_HAUPPAUGE_HVR3000:
451 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
446 ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7); 452 ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
447 ir_dprintk("biphase decoded: %x\n", ircode); 453 ir_dprintk("biphase decoded: %x\n", ircode);
448 if ((ircode & 0xfffff000) != 0x3000) 454 if ((ircode & 0xfffff000) != 0x3000)
diff --git a/drivers/media/video/cx88/cx88-mpeg.c b/drivers/media/video/cx88/cx88-mpeg.c
index 448c67380945..0aedbeaf94cd 100644
--- a/drivers/media/video/cx88/cx88-mpeg.c
+++ b/drivers/media/video/cx88/cx88-mpeg.c
@@ -102,7 +102,7 @@ static int cx8802_start_dma(struct cx8802_dev *dev,
102 cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl); 102 cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
103 udelay(100); 103 udelay(100);
104 cx_write(MO_PINMUX_IO, 0x00); 104 cx_write(MO_PINMUX_IO, 0x00);
105 cx_write(TS_HW_SOP_CNTRL,0x47<<16|188<<4|0x01); 105 cx_write(TS_HW_SOP_CNTRL, 0x47<<16|188<<4|0x01);
106 switch (core->boardnr) { 106 switch (core->boardnr) {
107 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q: 107 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
108 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T: 108 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
@@ -117,6 +117,15 @@ static int cx8802_start_dma(struct cx8802_dev *dev,
117 break; 117 break;
118 case CX88_BOARD_HAUPPAUGE_HVR1300: 118 case CX88_BOARD_HAUPPAUGE_HVR1300:
119 break; 119 break;
120 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
121 /* Enable MPEG parallel IO and video signal pins */
122 cx_write(MO_PINMUX_IO, 0x88);
123 cx_write(TS_HW_SOP_CNTRL, (0x47 << 16) | (188 << 4));
124 dev->ts_gen_cntrl = 5;
125 cx_write(TS_SOP_STAT, 0);
126 cx_write(TS_VALERR_CNTRL, 0);
127 udelay(100);
128 break;
120 default: 129 default:
121 cx_write(TS_SOP_STAT, 0x00); 130 cx_write(TS_SOP_STAT, 0x00);
122 break; 131 break;
@@ -195,7 +204,7 @@ static int cx8802_restart_queue(struct cx8802_dev *dev,
195 list_del(&buf->vb.queue); 204 list_del(&buf->vb.queue);
196 list_add_tail(&buf->vb.queue,&q->active); 205 list_add_tail(&buf->vb.queue,&q->active);
197 cx8802_start_dma(dev, q, buf); 206 cx8802_start_dma(dev, q, buf);
198 buf->vb.state = STATE_ACTIVE; 207 buf->vb.state = VIDEOBUF_ACTIVE;
199 buf->count = q->count++; 208 buf->count = q->count++;
200 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); 209 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
201 dprintk(1,"[%p/%d] restart_queue - first active\n", 210 dprintk(1,"[%p/%d] restart_queue - first active\n",
@@ -206,7 +215,7 @@ static int cx8802_restart_queue(struct cx8802_dev *dev,
206 prev->fmt == buf->fmt) { 215 prev->fmt == buf->fmt) {
207 list_del(&buf->vb.queue); 216 list_del(&buf->vb.queue);
208 list_add_tail(&buf->vb.queue,&q->active); 217 list_add_tail(&buf->vb.queue,&q->active);
209 buf->vb.state = STATE_ACTIVE; 218 buf->vb.state = VIDEOBUF_ACTIVE;
210 buf->count = q->count++; 219 buf->count = q->count++;
211 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 220 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
212 dprintk(1,"[%p/%d] restart_queue - move to active\n", 221 dprintk(1,"[%p/%d] restart_queue - move to active\n",
@@ -242,7 +251,7 @@ int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
242 if (0 != buf->vb.baddr && buf->vb.bsize < size) 251 if (0 != buf->vb.baddr && buf->vb.bsize < size)
243 return -EINVAL; 252 return -EINVAL;
244 253
245 if (STATE_NEEDS_INIT == buf->vb.state) { 254 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
246 buf->vb.width = dev->ts_packet_size; 255 buf->vb.width = dev->ts_packet_size;
247 buf->vb.height = dev->ts_packet_count; 256 buf->vb.height = dev->ts_packet_count;
248 buf->vb.size = size; 257 buf->vb.size = size;
@@ -254,7 +263,7 @@ int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
254 dma->sglist, 263 dma->sglist,
255 buf->vb.width, buf->vb.height, 0); 264 buf->vb.width, buf->vb.height, 0);
256 } 265 }
257 buf->vb.state = STATE_PREPARED; 266 buf->vb.state = VIDEOBUF_PREPARED;
258 return 0; 267 return 0;
259 268
260 fail: 269 fail:
@@ -276,7 +285,7 @@ void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
276 dprintk( 1, "queue is empty - first active\n" ); 285 dprintk( 1, "queue is empty - first active\n" );
277 list_add_tail(&buf->vb.queue,&cx88q->active); 286 list_add_tail(&buf->vb.queue,&cx88q->active);
278 cx8802_start_dma(dev, cx88q, buf); 287 cx8802_start_dma(dev, cx88q, buf);
279 buf->vb.state = STATE_ACTIVE; 288 buf->vb.state = VIDEOBUF_ACTIVE;
280 buf->count = cx88q->count++; 289 buf->count = cx88q->count++;
281 mod_timer(&cx88q->timeout, jiffies+BUFFER_TIMEOUT); 290 mod_timer(&cx88q->timeout, jiffies+BUFFER_TIMEOUT);
282 dprintk(1,"[%p/%d] %s - first active\n", 291 dprintk(1,"[%p/%d] %s - first active\n",
@@ -286,7 +295,7 @@ void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
286 dprintk( 1, "queue is not empty - append to active\n" ); 295 dprintk( 1, "queue is not empty - append to active\n" );
287 prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue); 296 prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue);
288 list_add_tail(&buf->vb.queue,&cx88q->active); 297 list_add_tail(&buf->vb.queue,&cx88q->active);
289 buf->vb.state = STATE_ACTIVE; 298 buf->vb.state = VIDEOBUF_ACTIVE;
290 buf->count = cx88q->count++; 299 buf->count = cx88q->count++;
291 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 300 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
292 dprintk( 1, "[%p/%d] %s - append to active\n", 301 dprintk( 1, "[%p/%d] %s - append to active\n",
@@ -306,7 +315,7 @@ static void do_cancel_buffers(struct cx8802_dev *dev, char *reason, int restart)
306 while (!list_empty(&q->active)) { 315 while (!list_empty(&q->active)) {
307 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue); 316 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
308 list_del(&buf->vb.queue); 317 list_del(&buf->vb.queue);
309 buf->vb.state = STATE_ERROR; 318 buf->vb.state = VIDEOBUF_ERROR;
310 wake_up(&buf->vb.done); 319 wake_up(&buf->vb.done);
311 dprintk(1,"[%p/%d] %s - dma=0x%08lx\n", 320 dprintk(1,"[%p/%d] %s - dma=0x%08lx\n",
312 buf, buf->vb.i, reason, (unsigned long)buf->risc.dma); 321 buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
@@ -437,10 +446,7 @@ static irqreturn_t cx8802_irq(int irq, void *dev_id)
437 return IRQ_RETVAL(handled); 446 return IRQ_RETVAL(handled);
438} 447}
439 448
440/* ----------------------------------------------------------- */ 449static int cx8802_init_common(struct cx8802_dev *dev)
441/* exported stuff */
442
443int cx8802_init_common(struct cx8802_dev *dev)
444{ 450{
445 struct cx88_core *core = dev->core; 451 struct cx88_core *core = dev->core;
446 int err; 452 int err;
@@ -488,7 +494,7 @@ int cx8802_init_common(struct cx8802_dev *dev)
488 return 0; 494 return 0;
489} 495}
490 496
491void cx8802_fini_common(struct cx8802_dev *dev) 497static void cx8802_fini_common(struct cx8802_dev *dev)
492{ 498{
493 dprintk( 2, "cx8802_fini_common\n" ); 499 dprintk( 2, "cx8802_fini_common\n" );
494 cx8802_stop_dma(dev); 500 cx8802_stop_dma(dev);
@@ -504,7 +510,7 @@ void cx8802_fini_common(struct cx8802_dev *dev)
504 510
505/* ----------------------------------------------------------- */ 511/* ----------------------------------------------------------- */
506 512
507int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state) 513static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
508{ 514{
509 struct cx8802_dev *dev = pci_get_drvdata(pci_dev); 515 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
510 struct cx88_core *core = dev->core; 516 struct cx88_core *core = dev->core;
@@ -530,7 +536,7 @@ int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
530 return 0; 536 return 0;
531} 537}
532 538
533int cx8802_resume_common(struct pci_dev *pci_dev) 539static int cx8802_resume_common(struct pci_dev *pci_dev)
534{ 540{
535 struct cx8802_dev *dev = pci_get_drvdata(pci_dev); 541 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
536 struct cx88_core *core = dev->core; 542 struct cx88_core *core = dev->core;
@@ -874,9 +880,6 @@ EXPORT_SYMBOL(cx8802_buf_prepare);
874EXPORT_SYMBOL(cx8802_buf_queue); 880EXPORT_SYMBOL(cx8802_buf_queue);
875EXPORT_SYMBOL(cx8802_cancel_buffers); 881EXPORT_SYMBOL(cx8802_cancel_buffers);
876 882
877EXPORT_SYMBOL(cx8802_init_common);
878EXPORT_SYMBOL(cx8802_fini_common);
879
880EXPORT_SYMBOL(cx8802_register_driver); 883EXPORT_SYMBOL(cx8802_register_driver);
881EXPORT_SYMBOL(cx8802_unregister_driver); 884EXPORT_SYMBOL(cx8802_unregister_driver);
882EXPORT_SYMBOL(cx8802_get_driver); 885EXPORT_SYMBOL(cx8802_get_driver);
diff --git a/drivers/media/video/cx88/cx88-vbi.c b/drivers/media/video/cx88/cx88-vbi.c
index babb08556406..d96ecfcf393a 100644
--- a/drivers/media/video/cx88/cx88-vbi.c
+++ b/drivers/media/video/cx88/cx88-vbi.c
@@ -130,7 +130,7 @@ void cx8800_vbi_timeout(unsigned long data)
130 while (!list_empty(&q->active)) { 130 while (!list_empty(&q->active)) {
131 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue); 131 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
132 list_del(&buf->vb.queue); 132 list_del(&buf->vb.queue);
133 buf->vb.state = STATE_ERROR; 133 buf->vb.state = VIDEOBUF_ERROR;
134 wake_up(&buf->vb.done); 134 wake_up(&buf->vb.done);
135 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->core->name, 135 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->core->name,
136 buf, buf->vb.i, (unsigned long)buf->risc.dma); 136 buf, buf->vb.i, (unsigned long)buf->risc.dma);
@@ -168,7 +168,7 @@ vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
168 if (0 != buf->vb.baddr && buf->vb.bsize < size) 168 if (0 != buf->vb.baddr && buf->vb.bsize < size)
169 return -EINVAL; 169 return -EINVAL;
170 170
171 if (STATE_NEEDS_INIT == buf->vb.state) { 171 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
172 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); 172 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
173 buf->vb.width = VBI_LINE_LENGTH; 173 buf->vb.width = VBI_LINE_LENGTH;
174 buf->vb.height = VBI_LINE_COUNT; 174 buf->vb.height = VBI_LINE_COUNT;
@@ -183,7 +183,7 @@ vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
183 buf->vb.width, 0, 183 buf->vb.width, 0,
184 buf->vb.height); 184 buf->vb.height);
185 } 185 }
186 buf->vb.state = STATE_PREPARED; 186 buf->vb.state = VIDEOBUF_PREPARED;
187 return 0; 187 return 0;
188 188
189 fail: 189 fail:
@@ -207,7 +207,7 @@ vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
207 if (list_empty(&q->active)) { 207 if (list_empty(&q->active)) {
208 list_add_tail(&buf->vb.queue,&q->active); 208 list_add_tail(&buf->vb.queue,&q->active);
209 cx8800_start_vbi_dma(dev, q, buf); 209 cx8800_start_vbi_dma(dev, q, buf);
210 buf->vb.state = STATE_ACTIVE; 210 buf->vb.state = VIDEOBUF_ACTIVE;
211 buf->count = q->count++; 211 buf->count = q->count++;
212 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); 212 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
213 dprintk(2,"[%p/%d] vbi_queue - first active\n", 213 dprintk(2,"[%p/%d] vbi_queue - first active\n",
@@ -216,7 +216,7 @@ vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
216 } else { 216 } else {
217 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue); 217 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
218 list_add_tail(&buf->vb.queue,&q->active); 218 list_add_tail(&buf->vb.queue,&q->active);
219 buf->vb.state = STATE_ACTIVE; 219 buf->vb.state = VIDEOBUF_ACTIVE;
220 buf->count = q->count++; 220 buf->count = q->count++;
221 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 221 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
222 dprintk(2,"[%p/%d] buffer_queue - append to active\n", 222 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
diff --git a/drivers/media/video/cx88/cx88-video.c b/drivers/media/video/cx88/cx88-video.c
index c84dafbdb991..7f1931aed207 100644
--- a/drivers/media/video/cx88/cx88-video.c
+++ b/drivers/media/video/cx88/cx88-video.c
@@ -392,13 +392,41 @@ int cx88_video_mux(struct cx88_core *core, unsigned int input)
392 break; 392 break;
393 } 393 }
394 394
395 if (core->board.mpeg & CX88_MPEG_BLACKBIRD) { 395 /* if there are audioroutes defined, we have an external
396 /* sets sound input from external adc */ 396 ADC to deal with audio */
397 if (INPUT(input).extadc) 397
398 if (INPUT(input).audioroute) {
399
400 /* cx2388's C-ADC is connected to the tuner only.
401 When used with S-Video, that ADC is busy dealing with
402 chroma, so an external must be used for baseband audio */
403
404 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
405 INPUT(input).type != CX88_RADIO) {
406 /* "ADC mode" */
407 cx_write(AUD_I2SCNTL, 0x1);
398 cx_set(AUD_CTL, EN_I2SIN_ENABLE); 408 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
399 else 409 } else {
410 /* Normal mode */
411 cx_write(AUD_I2SCNTL, 0x0);
400 cx_clear(AUD_CTL, EN_I2SIN_ENABLE); 412 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
413 }
414
415 /* The wm8775 module has the "2" route hardwired into
416 the initialization. Some boards may use different
417 routes for different inputs. HVR-1300 surely does */
418 if (core->board.audio_chip &&
419 core->board.audio_chip == AUDIO_CHIP_WM8775) {
420 struct v4l2_routing route;
421
422 route.input = INPUT(input).audioroute;
423 cx88_call_i2c_clients(core,
424 VIDIOC_INT_S_AUDIO_ROUTING, &route);
425
426 }
427
401 } 428 }
429
402 return 0; 430 return 0;
403} 431}
404EXPORT_SYMBOL(cx88_video_mux); 432EXPORT_SYMBOL(cx88_video_mux);
@@ -486,7 +514,7 @@ static int restart_video_queue(struct cx8800_dev *dev,
486 if (NULL == prev) { 514 if (NULL == prev) {
487 list_move_tail(&buf->vb.queue, &q->active); 515 list_move_tail(&buf->vb.queue, &q->active);
488 start_video_dma(dev, q, buf); 516 start_video_dma(dev, q, buf);
489 buf->vb.state = STATE_ACTIVE; 517 buf->vb.state = VIDEOBUF_ACTIVE;
490 buf->count = q->count++; 518 buf->count = q->count++;
491 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); 519 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
492 dprintk(2,"[%p/%d] restart_queue - first active\n", 520 dprintk(2,"[%p/%d] restart_queue - first active\n",
@@ -496,7 +524,7 @@ static int restart_video_queue(struct cx8800_dev *dev,
496 prev->vb.height == buf->vb.height && 524 prev->vb.height == buf->vb.height &&
497 prev->fmt == buf->fmt) { 525 prev->fmt == buf->fmt) {
498 list_move_tail(&buf->vb.queue, &q->active); 526 list_move_tail(&buf->vb.queue, &q->active);
499 buf->vb.state = STATE_ACTIVE; 527 buf->vb.state = VIDEOBUF_ACTIVE;
500 buf->count = q->count++; 528 buf->count = q->count++;
501 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 529 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
502 dprintk(2,"[%p/%d] restart_queue - move to active\n", 530 dprintk(2,"[%p/%d] restart_queue - move to active\n",
@@ -553,7 +581,7 @@ buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
553 init_buffer = 1; 581 init_buffer = 1;
554 } 582 }
555 583
556 if (STATE_NEEDS_INIT == buf->vb.state) { 584 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
557 init_buffer = 1; 585 init_buffer = 1;
558 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL))) 586 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
559 goto fail; 587 goto fail;
@@ -601,7 +629,7 @@ buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
601 fh->width, fh->height, fh->fmt->depth, fh->fmt->name, 629 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
602 (unsigned long)buf->risc.dma); 630 (unsigned long)buf->risc.dma);
603 631
604 buf->vb.state = STATE_PREPARED; 632 buf->vb.state = VIDEOBUF_PREPARED;
605 return 0; 633 return 0;
606 634
607 fail: 635 fail:
@@ -625,14 +653,14 @@ buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
625 653
626 if (!list_empty(&q->queued)) { 654 if (!list_empty(&q->queued)) {
627 list_add_tail(&buf->vb.queue,&q->queued); 655 list_add_tail(&buf->vb.queue,&q->queued);
628 buf->vb.state = STATE_QUEUED; 656 buf->vb.state = VIDEOBUF_QUEUED;
629 dprintk(2,"[%p/%d] buffer_queue - append to queued\n", 657 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
630 buf, buf->vb.i); 658 buf, buf->vb.i);
631 659
632 } else if (list_empty(&q->active)) { 660 } else if (list_empty(&q->active)) {
633 list_add_tail(&buf->vb.queue,&q->active); 661 list_add_tail(&buf->vb.queue,&q->active);
634 start_video_dma(dev, q, buf); 662 start_video_dma(dev, q, buf);
635 buf->vb.state = STATE_ACTIVE; 663 buf->vb.state = VIDEOBUF_ACTIVE;
636 buf->count = q->count++; 664 buf->count = q->count++;
637 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); 665 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
638 dprintk(2,"[%p/%d] buffer_queue - first active\n", 666 dprintk(2,"[%p/%d] buffer_queue - first active\n",
@@ -644,7 +672,7 @@ buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
644 prev->vb.height == buf->vb.height && 672 prev->vb.height == buf->vb.height &&
645 prev->fmt == buf->fmt) { 673 prev->fmt == buf->fmt) {
646 list_add_tail(&buf->vb.queue,&q->active); 674 list_add_tail(&buf->vb.queue,&q->active);
647 buf->vb.state = STATE_ACTIVE; 675 buf->vb.state = VIDEOBUF_ACTIVE;
648 buf->count = q->count++; 676 buf->count = q->count++;
649 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 677 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
650 dprintk(2,"[%p/%d] buffer_queue - append to active\n", 678 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
@@ -652,7 +680,7 @@ buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
652 680
653 } else { 681 } else {
654 list_add_tail(&buf->vb.queue,&q->queued); 682 list_add_tail(&buf->vb.queue,&q->queued);
655 buf->vb.state = STATE_QUEUED; 683 buf->vb.state = VIDEOBUF_QUEUED;
656 dprintk(2,"[%p/%d] buffer_queue - first queued\n", 684 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
657 buf, buf->vb.i); 685 buf, buf->vb.i);
658 } 686 }
@@ -822,8 +850,8 @@ video_poll(struct file *file, struct poll_table_struct *wait)
822 return POLLERR; 850 return POLLERR;
823 } 851 }
824 poll_wait(file, &buf->vb.done, wait); 852 poll_wait(file, &buf->vb.done, wait);
825 if (buf->vb.state == STATE_DONE || 853 if (buf->vb.state == VIDEOBUF_DONE ||
826 buf->vb.state == STATE_ERROR) 854 buf->vb.state == VIDEOBUF_ERROR)
827 return POLLIN|POLLRDNORM; 855 return POLLIN|POLLRDNORM;
828 return 0; 856 return 0;
829} 857}
@@ -1496,7 +1524,7 @@ static void cx8800_vid_timeout(unsigned long data)
1496 while (!list_empty(&q->active)) { 1524 while (!list_empty(&q->active)) {
1497 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue); 1525 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1498 list_del(&buf->vb.queue); 1526 list_del(&buf->vb.queue);
1499 buf->vb.state = STATE_ERROR; 1527 buf->vb.state = VIDEOBUF_ERROR;
1500 wake_up(&buf->vb.done); 1528 wake_up(&buf->vb.done);
1501 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name, 1529 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1502 buf, buf->vb.i, (unsigned long)buf->risc.dma); 1530 buf, buf->vb.i, (unsigned long)buf->risc.dma);
diff --git a/drivers/media/video/cx88/cx88.h b/drivers/media/video/cx88/cx88.h
index eb296bdecb1e..4e823f2a539a 100644
--- a/drivers/media/video/cx88/cx88.h
+++ b/drivers/media/video/cx88/cx88.h
@@ -210,6 +210,7 @@ extern struct sram_channel cx88_sram_channels[];
210#define CX88_BOARD_TE_DTV_250_OEM_SWANN 55 210#define CX88_BOARD_TE_DTV_250_OEM_SWANN 55
211#define CX88_BOARD_HAUPPAUGE_HVR1300 56 211#define CX88_BOARD_HAUPPAUGE_HVR1300 56
212#define CX88_BOARD_ADSTECH_PTV_390 57 212#define CX88_BOARD_ADSTECH_PTV_390 57
213#define CX88_BOARD_PINNACLE_PCTV_HD_800i 58
213 214
214enum cx88_itype { 215enum cx88_itype {
215 CX88_VMUX_COMPOSITE1 = 1, 216 CX88_VMUX_COMPOSITE1 = 1,
@@ -228,7 +229,7 @@ struct cx88_input {
228 enum cx88_itype type; 229 enum cx88_itype type;
229 u32 gpio0, gpio1, gpio2, gpio3; 230 u32 gpio0, gpio1, gpio2, gpio3;
230 unsigned int vmux:2; 231 unsigned int vmux:2;
231 unsigned int extadc:1; 232 unsigned int audioroute:2;
232}; 233};
233 234
234struct cx88_board { 235struct cx88_board {
@@ -461,6 +462,7 @@ struct cx8802_dev {
461 u32 mailbox; 462 u32 mailbox;
462 int width; 463 int width;
463 int height; 464 int height;
465 unsigned char mpeg_active; /* nonzero if mpeg encoder is active */
464 466
465 /* mpeg params */ 467 /* mpeg params */
466 struct cx2341x_mpeg_params params; 468 struct cx2341x_mpeg_params params;
@@ -588,6 +590,7 @@ extern void cx88_call_i2c_clients(struct cx88_core *core,
588/* ----------------------------------------------------------- */ 590/* ----------------------------------------------------------- */
589/* cx88-cards.c */ 591/* cx88-cards.c */
590 592
593extern int cx88_tuner_callback(void *dev, int command, int arg);
591extern int cx88_get_resources(const struct cx88_core *core, 594extern int cx88_get_resources(const struct cx88_core *core,
592 struct pci_dev *pci); 595 struct pci_dev *pci);
593extern struct cx88_core *cx88_core_create(struct pci_dev *pci, int nr); 596extern struct cx88_core *cx88_core_create(struct pci_dev *pci, int nr);
@@ -633,12 +636,6 @@ int cx8802_buf_prepare(struct videobuf_queue *q,struct cx8802_dev *dev,
633void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf); 636void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf);
634void cx8802_cancel_buffers(struct cx8802_dev *dev); 637void cx8802_cancel_buffers(struct cx8802_dev *dev);
635 638
636int cx8802_init_common(struct cx8802_dev *dev);
637void cx8802_fini_common(struct cx8802_dev *dev);
638
639int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state);
640int cx8802_resume_common(struct pci_dev *pci_dev);
641
642/* ----------------------------------------------------------- */ 639/* ----------------------------------------------------------- */
643/* cx88-video.c*/ 640/* cx88-video.c*/
644extern const u32 cx88_user_ctrls[]; 641extern const u32 cx88_user_ctrls[];
diff --git a/drivers/media/video/em28xx/Kconfig b/drivers/media/video/em28xx/Kconfig
index c1127802ad9c..abbd38c1ebba 100644
--- a/drivers/media/video/em28xx/Kconfig
+++ b/drivers/media/video/em28xx/Kconfig
@@ -1,6 +1,6 @@
1config VIDEO_EM28XX 1config VIDEO_EM28XX
2 tristate "Empia EM2800/2820/2840 USB video capture support" 2 tristate "Empia EM2800/2820/2840 USB video capture support"
3 depends on VIDEO_V4L1 && I2C && INPUT 3 depends on VIDEO_DEV && I2C && INPUT
4 select VIDEO_TUNER 4 select VIDEO_TUNER
5 select VIDEO_TVEEPROM 5 select VIDEO_TVEEPROM
6 select VIDEO_IR 6 select VIDEO_IR
@@ -11,3 +11,18 @@ config VIDEO_EM28XX
11 11
12 To compile this driver as a module, choose M here: the 12 To compile this driver as a module, choose M here: the
13 module will be called em28xx 13 module will be called em28xx
14
15config VIDEO_EM28XX_ALSA
16 depends on VIDEO_EM28XX
17 tristate "Empia EM28xx ALSA audio module"
18 ---help---
19 This is an ALSA driver for some Empia 28xx based TV cards.
20
21 This is not required for em2800/em2820/em2821 boards. However,
22 newer em28xx devices uses Vendor Class for audio, instead of
23 implementing the USB Audio Class. For those chips, this module
24 will enable digital audio.
25
26 To compile this driver as a module, choose M here: the
27 module will be called em28xx-alsa
28
diff --git a/drivers/media/video/em28xx/Makefile b/drivers/media/video/em28xx/Makefile
index 826d0e340753..0924550992d0 100644
--- a/drivers/media/video/em28xx/Makefile
+++ b/drivers/media/video/em28xx/Makefile
@@ -1,6 +1,12 @@
1em28xx-objs := em28xx-video.o em28xx-i2c.o em28xx-cards.o em28xx-core.o \ 1em28xx-objs := em28xx-video.o em28xx-i2c.o em28xx-cards.o em28xx-core.o \
2 em28xx-input.o 2 em28xx-input.o
3 3
4em28xx-alsa-objs := em28xx-audio.o
5
4obj-$(CONFIG_VIDEO_EM28XX) += em28xx.o 6obj-$(CONFIG_VIDEO_EM28XX) += em28xx.o
7obj-$(CONFIG_VIDEO_EM28XX_ALSA) += em28xx-alsa.o
5 8
6EXTRA_CFLAGS += -Idrivers/media/video 9EXTRA_CFLAGS += -Idrivers/media/video
10EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core
11EXTRA_CFLAGS += -Idrivers/media/dvb/frontends
12
diff --git a/drivers/media/video/em28xx/em28xx-audio.c b/drivers/media/video/em28xx/em28xx-audio.c
new file mode 100644
index 000000000000..941357c4f3f5
--- /dev/null
+++ b/drivers/media/video/em28xx/em28xx-audio.c
@@ -0,0 +1,489 @@
1/*
2 * Empiatech em28x1 audio extension
3 *
4 * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
5 *
6 * Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
7 * - Port to work with the in-kernel driver
8 * - Several cleanups
9 *
10 * This driver is based on my previous au600 usb pstn audio driver
11 * and inherits all the copyrights
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/kernel.h>
29#include <linux/usb.h>
30#include <linux/init.h>
31#include <linux/sound.h>
32#include <linux/spinlock.h>
33#include <linux/soundcard.h>
34#include <linux/slab.h>
35#include <linux/vmalloc.h>
36#include <linux/proc_fs.h>
37#include <linux/module.h>
38#include <sound/driver.h>
39#include <sound/core.h>
40#include <sound/pcm.h>
41#include <sound/pcm_params.h>
42#include <sound/info.h>
43#include <sound/initval.h>
44#include <sound/control.h>
45#include <media/v4l2-common.h>
46#include "em28xx.h"
47
48static int debug;
49module_param(debug, int, 0644);
50MODULE_PARM_DESC(debug, "activates debug info");
51
52#define dprintk(fmt, arg...) do { \
53 if (debug) \
54 printk(KERN_INFO "em28xx-audio %s: " fmt, \
55 __FUNCTION__, ##arg); \
56 } while (0)
57
58static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
59
60static int em28xx_isoc_audio_deinit(struct em28xx *dev)
61{
62 int i;
63
64 dprintk("Stopping isoc\n");
65 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
66 usb_kill_urb(dev->adev->urb[i]);
67 usb_free_urb(dev->adev->urb[i]);
68 dev->adev->urb[i] = NULL;
69 }
70
71 return 0;
72}
73
74static void em28xx_audio_isocirq(struct urb *urb)
75{
76 struct em28xx *dev = urb->context;
77 int i;
78 unsigned int oldptr;
79 unsigned long flags;
80 int period_elapsed = 0;
81 int status;
82 unsigned char *cp;
83 unsigned int stride;
84 struct snd_pcm_substream *substream;
85 struct snd_pcm_runtime *runtime;
86 if (dev->adev->capture_pcm_substream) {
87 substream = dev->adev->capture_pcm_substream;
88 runtime = substream->runtime;
89 stride = runtime->frame_bits >> 3;
90
91 for (i = 0; i < urb->number_of_packets; i++) {
92 int length =
93 urb->iso_frame_desc[i].actual_length / stride;
94 cp = (unsigned char *)urb->transfer_buffer +
95 urb->iso_frame_desc[i].offset;
96
97 if (!length)
98 continue;
99
100 spin_lock_irqsave(&dev->adev->slock, flags);
101
102 oldptr = dev->adev->hwptr_done_capture;
103 dev->adev->hwptr_done_capture += length;
104 if (dev->adev->hwptr_done_capture >=
105 runtime->buffer_size)
106 dev->adev->hwptr_done_capture -=
107 runtime->buffer_size;
108
109 dev->adev->capture_transfer_done += length;
110 if (dev->adev->capture_transfer_done >=
111 runtime->period_size) {
112 dev->adev->capture_transfer_done -=
113 runtime->period_size;
114 period_elapsed = 1;
115 }
116
117 spin_unlock_irqrestore(&dev->adev->slock, flags);
118
119 if (oldptr + length >= runtime->buffer_size) {
120 unsigned int cnt =
121 runtime->buffer_size - oldptr - 1;
122 memcpy(runtime->dma_area + oldptr * stride, cp,
123 cnt * stride);
124 memcpy(runtime->dma_area, cp + cnt,
125 length * stride - cnt * stride);
126 } else {
127 memcpy(runtime->dma_area + oldptr * stride, cp,
128 length * stride);
129 }
130 }
131 if (period_elapsed)
132 snd_pcm_period_elapsed(substream);
133 }
134 urb->status = 0;
135
136 if (dev->adev->shutdown)
137 return;
138
139 status = usb_submit_urb(urb, GFP_ATOMIC);
140 if (status < 0) {
141 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
142 status);
143 }
144 return;
145}
146
147static int em28xx_init_audio_isoc(struct em28xx *dev)
148{
149 int i, errCode;
150 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
151 EM28XX_AUDIO_MAX_PACKET_SIZE;
152
153 dprintk("Starting isoc transfers\n");
154
155 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
156 struct urb *urb;
157 int j, k;
158
159 dev->adev->transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
160 if (!dev->adev->transfer_buffer[i])
161 return -ENOMEM;
162
163 memset(dev->adev->transfer_buffer[i], 0x80, sb_size);
164 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
165 if (!urb)
166 return -ENOMEM;
167
168 urb->dev = dev->udev;
169 urb->context = dev;
170 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
171 urb->transfer_flags = URB_ISO_ASAP;
172 urb->transfer_buffer = dev->adev->transfer_buffer[i];
173 urb->interval = 1;
174 urb->complete = em28xx_audio_isocirq;
175 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
176 urb->transfer_buffer_length = sb_size;
177
178 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
179 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
180 urb->iso_frame_desc[j].offset = k;
181 urb->iso_frame_desc[j].length =
182 EM28XX_AUDIO_MAX_PACKET_SIZE;
183 }
184 dev->adev->urb[i] = urb;
185 }
186
187 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
188 errCode = usb_submit_urb(dev->adev->urb[i], GFP_ATOMIC);
189 if (errCode) {
190 em28xx_isoc_audio_deinit(dev);
191
192 return errCode;
193 }
194 }
195
196 return 0;
197}
198
199static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
200{
201 dprintk("%s transfer\n", (dev->adev->capture_stream == STREAM_ON)?
202 "stop" : "start");
203
204 switch (cmd) {
205 case EM28XX_CAPTURE_STREAM_EN:
206 if (dev->adev->capture_stream == STREAM_OFF && arg == 1) {
207 dev->adev->capture_stream = STREAM_ON;
208 em28xx_init_audio_isoc(dev);
209 } else if (dev->adev->capture_stream == STREAM_ON && arg == 0) {
210 dev->adev->capture_stream = STREAM_OFF;
211 em28xx_isoc_audio_deinit(dev);
212 } else {
213 printk(KERN_ERR "An underrun very likely occurred. "
214 "Ignoring it.\n");
215 }
216 return 0;
217 default:
218 return -EINVAL;
219 }
220}
221
222static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
223 size_t size)
224{
225 struct snd_pcm_runtime *runtime = subs->runtime;
226
227 dprintk("Alocating vbuffer\n");
228 if (runtime->dma_area) {
229 if (runtime->dma_bytes > size)
230 return 0;
231
232 vfree(runtime->dma_area);
233 }
234 runtime->dma_area = vmalloc(size);
235 if (!runtime->dma_area)
236 return -ENOMEM;
237
238 runtime->dma_bytes = size;
239
240 return 0;
241}
242
243static struct snd_pcm_hardware snd_em28xx_hw_capture = {
244 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
245 SNDRV_PCM_INFO_MMAP |
246 SNDRV_PCM_INFO_INTERLEAVED |
247 SNDRV_PCM_INFO_MMAP_VALID,
248
249 .formats = SNDRV_PCM_FMTBIT_S16_LE,
250
251 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
252
253 .rate_min = 48000,
254 .rate_max = 48000,
255 .channels_min = 2,
256 .channels_max = 2,
257 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
258 .period_bytes_min = 64, /* 12544/2, */
259 .period_bytes_max = 12544,
260 .periods_min = 2,
261 .periods_max = 98, /* 12544, */
262};
263
264static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
265{
266 struct em28xx *dev = snd_pcm_substream_chip(substream);
267 struct snd_pcm_runtime *runtime = substream->runtime;
268 int ret = 0;
269
270 dprintk("opening device and trying to acquire exclusive lock\n");
271
272 /* Sets volume, mute, etc */
273 dev->mute = 0;
274 ret = em28xx_audio_analog_set(dev);
275 if (ret < 0)
276 goto err;
277
278 runtime->hw = snd_em28xx_hw_capture;
279 if (dev->alt == 0 && dev->adev->users == 0) {
280 int errCode;
281 dev->alt = 7;
282 errCode = usb_set_interface(dev->udev, 0, 7);
283 dprintk("changing alternate number to 7\n");
284 }
285
286 dev->adev->users++;
287
288 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
289 dev->adev->capture_pcm_substream = substream;
290 runtime->private_data = dev;
291
292 return 0;
293err:
294 printk(KERN_ERR "Error while configuring em28xx mixer\n");
295 return ret;
296}
297
298static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
299{
300 struct em28xx *dev = snd_pcm_substream_chip(substream);
301 dev->adev->users--;
302
303 dprintk("closing device\n");
304
305 dev->mute = 1;
306 em28xx_audio_analog_set(dev);
307
308 if (dev->adev->users == 0 && dev->adev->shutdown == 1) {
309 dprintk("audio users: %d\n", dev->adev->users);
310 dprintk("disabling audio stream!\n");
311 dev->adev->shutdown = 0;
312 dprintk("released lock\n");
313 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
314 }
315 return 0;
316}
317
318static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
319 struct snd_pcm_hw_params *hw_params)
320{
321 unsigned int channels, rate, format;
322 int ret;
323
324 dprintk("Setting capture parameters\n");
325
326 ret = snd_pcm_alloc_vmalloc_buffer(substream,
327 params_buffer_bytes(hw_params));
328 format = params_format(hw_params);
329 rate = params_rate(hw_params);
330 channels = params_channels(hw_params);
331
332 /* TODO: set up em28xx audio chip to deliver the correct audio format,
333 current default is 48000hz multiplexed => 96000hz mono
334 which shouldn't matter since analogue TV only supports mono */
335 return 0;
336}
337
338static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
339{
340 struct em28xx *dev = snd_pcm_substream_chip(substream);
341
342 dprintk("Stop capture, if needed\n");
343
344 if (dev->adev->capture_stream == STREAM_ON)
345 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
346
347 return 0;
348}
349
350static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
351{
352 return 0;
353}
354
355static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
356 int cmd)
357{
358 struct em28xx *dev = snd_pcm_substream_chip(substream);
359
360 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
361 "start": "stop");
362 switch (cmd) {
363 case SNDRV_PCM_TRIGGER_START:
364 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
365 return 0;
366 case SNDRV_PCM_TRIGGER_STOP:
367 dev->adev->shutdown = 1;
368 return 0;
369 default:
370 return -EINVAL;
371 }
372}
373
374static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
375 *substream)
376{
377 struct em28xx *dev;
378
379 snd_pcm_uframes_t hwptr_done;
380 dev = snd_pcm_substream_chip(substream);
381 hwptr_done = dev->adev->hwptr_done_capture;
382
383 return hwptr_done;
384}
385
386static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
387 unsigned long offset)
388{
389 void *pageptr = subs->runtime->dma_area + offset;
390
391 return vmalloc_to_page(pageptr);
392}
393
394static struct snd_pcm_ops snd_em28xx_pcm_capture = {
395 .open = snd_em28xx_capture_open,
396 .close = snd_em28xx_pcm_close,
397 .ioctl = snd_pcm_lib_ioctl,
398 .hw_params = snd_em28xx_hw_capture_params,
399 .hw_free = snd_em28xx_hw_capture_free,
400 .prepare = snd_em28xx_prepare,
401 .trigger = snd_em28xx_capture_trigger,
402 .pointer = snd_em28xx_capture_pointer,
403 .page = snd_pcm_get_vmalloc_page,
404};
405
406static int em28xx_audio_init(struct em28xx *dev)
407{
408 struct em28xx_audio *adev;
409 struct snd_pcm *pcm;
410 struct snd_card *card;
411 static int devnr;
412 int ret, err;
413
414 printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
415 "non standard usbaudio\n");
416 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
417 "Rechberger\n");
418
419 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
420 if (!adev) {
421 printk(KERN_ERR "em28xx-audio.c: out of memory\n");
422 return -1;
423 }
424 card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0);
425 if (card == NULL) {
426 kfree(adev);
427 return -ENOMEM;
428 }
429
430 spin_lock_init(&adev->slock);
431 ret = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
432 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
433 pcm->info_flags = 0;
434 pcm->private_data = dev;
435 strcpy(pcm->name, "Empia 28xx Capture");
436 strcpy(card->driver, "Empia Em28xx Audio");
437 strcpy(card->shortname, "Em28xx Audio");
438 strcpy(card->longname, "Empia Em28xx Audio");
439
440 err = snd_card_register(card);
441 if (err < 0) {
442 snd_card_free(card);
443 return -ENOMEM;
444 }
445 adev->sndcard = card;
446 adev->udev = dev->udev;
447 dev->adev = adev;
448
449 return 0;
450}
451
452static int em28xx_audio_fini(struct em28xx *dev)
453{
454 if (dev == NULL)
455 return 0;
456
457 if (dev->adev) {
458 snd_card_free(dev->adev->sndcard);
459 kfree(dev->adev);
460 dev->adev = NULL;
461 }
462
463 return 0;
464}
465
466static struct em28xx_ops audio_ops = {
467 .id = EM28XX_AUDIO,
468 .name = "Em28xx Audio Extension",
469 .init = em28xx_audio_init,
470 .fini = em28xx_audio_fini,
471};
472
473static int __init em28xx_alsa_register(void)
474{
475 return em28xx_register_extension(&audio_ops);
476}
477
478static void __exit em28xx_alsa_unregister(void)
479{
480 em28xx_unregister_extension(&audio_ops);
481}
482
483MODULE_LICENSE("GPL");
484MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
485MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
486MODULE_DESCRIPTION("Em28xx Audio driver");
487
488module_init(em28xx_alsa_register);
489module_exit(em28xx_alsa_unregister);
diff --git a/drivers/media/video/em28xx/em28xx-cards.c b/drivers/media/video/em28xx/em28xx-cards.c
index 418ea8b7f85a..2159d0160df2 100644
--- a/drivers/media/video/em28xx/em28xx-cards.c
+++ b/drivers/media/video/em28xx/em28xx-cards.c
@@ -1,5 +1,6 @@
1/* 1/*
2 em28xx-cards.c - driver for Empia EM2800/EM2820/2840 USB video capture devices 2 em28xx-cards.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
3 4
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> 5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com> 6 Markus Rechberger <mrechberger@gmail.com>
@@ -35,294 +36,735 @@
35#include <media/v4l2-common.h> 36#include <media/v4l2-common.h>
36 37
37#include "em28xx.h" 38#include "em28xx.h"
39#include "tuner-xc2028.h"
40
41static int tuner = -1;
42module_param(tuner, int, 0444);
43MODULE_PARM_DESC(tuner, "tuner type");
44
45static unsigned int disable_ir;
46module_param(disable_ir, int, 0444);
47MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
48
49struct em28xx_hash_table {
50 unsigned long hash;
51 unsigned int model;
52 unsigned int tuner;
53};
54
55/* Boards supported by driver */
56
57#define EM2800_BOARD_UNKNOWN 0
58#define EM2820_BOARD_UNKNOWN 1
59#define EM2820_BOARD_TERRATEC_CINERGY_250 2
60#define EM2820_BOARD_PINNACLE_USB_2 3
61#define EM2820_BOARD_HAUPPAUGE_WINTV_USB_2 4
62#define EM2820_BOARD_MSI_VOX_USB_2 5
63#define EM2800_BOARD_TERRATEC_CINERGY_200 6
64#define EM2800_BOARD_LEADTEK_WINFAST_USBII 7
65#define EM2800_BOARD_KWORLD_USB2800 8
66#define EM2820_BOARD_PINNACLE_DVC_90 9
67#define EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900 10
68#define EM2880_BOARD_TERRATEC_HYBRID_XS 11
69#define EM2820_BOARD_KWORLD_PVRTV2800RF 12
70#define EM2880_BOARD_TERRATEC_PRODIGY_XS 13
71#define EM2820_BOARD_PROLINK_PLAYTV_USB2 14
72#define EM2800_BOARD_VGEAR_POCKETTV 15
73#define EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950 16
38 74
39struct em28xx_board em28xx_boards[] = { 75struct em28xx_board em28xx_boards[] = {
40 [EM2800_BOARD_UNKNOWN] = { 76 [EM2800_BOARD_UNKNOWN] = {
41 .name = "Unknown EM2800 video grabber", 77 .name = "Unknown EM2800 video grabber",
42 .is_em2800 = 1, 78 .is_em2800 = 1,
43 .vchannels = 2, 79 .vchannels = 2,
44 .norm = VIDEO_MODE_PAL,
45 .tda9887_conf = TDA9887_PRESENT, 80 .tda9887_conf = TDA9887_PRESENT,
46 .has_tuner = 1,
47 .decoder = EM28XX_SAA7113, 81 .decoder = EM28XX_SAA7113,
48 .input = {{ 82 .input = { {
49 .type = EM28XX_VMUX_COMPOSITE1, 83 .type = EM28XX_VMUX_COMPOSITE1,
50 .vmux = SAA7115_COMPOSITE0, 84 .vmux = SAA7115_COMPOSITE0,
51 .amux = 1, 85 .amux = 1,
52 },{ 86 }, {
53 .type = EM28XX_VMUX_SVIDEO, 87 .type = EM28XX_VMUX_SVIDEO,
54 .vmux = SAA7115_SVIDEO3, 88 .vmux = SAA7115_SVIDEO3,
55 .amux = 1, 89 .amux = 1,
56 }}, 90 } },
57 }, 91 },
58 [EM2820_BOARD_UNKNOWN] = { 92 [EM2820_BOARD_UNKNOWN] = {
59 .name = "Unknown EM2820/2840 video grabber", 93 .name = "Unknown EM2750/28xx video grabber",
60 .is_em2800 = 0, 94 .is_em2800 = 0,
61 .vchannels = 2, 95 .tuner_type = TUNER_ABSENT,
62 .norm = VIDEO_MODE_PAL,
63 .tda9887_conf = TDA9887_PRESENT,
64 .has_tuner = 1,
65 .decoder = EM28XX_SAA7113,
66 .input = {{
67 .type = EM28XX_VMUX_COMPOSITE1,
68 .vmux = SAA7115_COMPOSITE0,
69 .amux = 1,
70 },{
71 .type = EM28XX_VMUX_SVIDEO,
72 .vmux = SAA7115_SVIDEO3,
73 .amux = 1,
74 }},
75 }, 96 },
76 [EM2820_BOARD_KWORLD_PVRTV2800RF] = { 97 [EM2820_BOARD_KWORLD_PVRTV2800RF] = {
77 .name = "Kworld PVR TV 2800 RF", 98 .name = "Kworld PVR TV 2800 RF",
78 .is_em2800 = 0, 99 .is_em2800 = 0,
79 .vchannels = 2, 100 .vchannels = 2,
80 .norm = VIDEO_MODE_PAL, 101 .tuner_type = TUNER_TEMIC_PAL,
81 .tda9887_conf = TDA9887_PRESENT, 102 .tda9887_conf = TDA9887_PRESENT,
82 .has_tuner = 1,
83 .decoder = EM28XX_SAA7113, 103 .decoder = EM28XX_SAA7113,
84 .input = {{ 104 .input = { {
85 .type = EM28XX_VMUX_COMPOSITE1, 105 .type = EM28XX_VMUX_COMPOSITE1,
86 .vmux = SAA7115_COMPOSITE0, 106 .vmux = SAA7115_COMPOSITE0,
87 .amux = 1, 107 .amux = 1,
88 },{ 108 }, {
89 .type = EM28XX_VMUX_SVIDEO, 109 .type = EM28XX_VMUX_SVIDEO,
90 .vmux = SAA7115_SVIDEO3, 110 .vmux = SAA7115_SVIDEO3,
91 .amux = 1, 111 .amux = 1,
92 }}, 112 } },
93 }, 113 },
94 [EM2820_BOARD_TERRATEC_CINERGY_250] = { 114 [EM2820_BOARD_TERRATEC_CINERGY_250] = {
95 .name = "Terratec Cinergy 250 USB", 115 .name = "Terratec Cinergy 250 USB",
96 .vchannels = 3, 116 .vchannels = 3,
97 .norm = VIDEO_MODE_PAL,
98 .tuner_type = TUNER_LG_PAL_NEW_TAPC, 117 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
99 .tda9887_conf = TDA9887_PRESENT, 118 .tda9887_conf = TDA9887_PRESENT,
100 .has_tuner = 1,
101 .decoder = EM28XX_SAA7113, 119 .decoder = EM28XX_SAA7113,
102 .input = {{ 120 .input = { {
103 .type = EM28XX_VMUX_TELEVISION, 121 .type = EM28XX_VMUX_TELEVISION,
104 .vmux = SAA7115_COMPOSITE2, 122 .vmux = SAA7115_COMPOSITE2,
105 .amux = 1, 123 .amux = 1,
106 },{ 124 }, {
107 .type = EM28XX_VMUX_COMPOSITE1, 125 .type = EM28XX_VMUX_COMPOSITE1,
108 .vmux = SAA7115_COMPOSITE0, 126 .vmux = SAA7115_COMPOSITE0,
109 .amux = 1, 127 .amux = 1,
110 },{ 128 }, {
111 .type = EM28XX_VMUX_SVIDEO, 129 .type = EM28XX_VMUX_SVIDEO,
112 .vmux = SAA7115_SVIDEO3, 130 .vmux = SAA7115_SVIDEO3,
113 .amux = 1, 131 .amux = 1,
114 }}, 132 } },
115 }, 133 },
116 [EM2820_BOARD_PINNACLE_USB_2] = { 134 [EM2820_BOARD_PINNACLE_USB_2] = {
117 .name = "Pinnacle PCTV USB 2", 135 .name = "Pinnacle PCTV USB 2",
118 .vchannels = 3, 136 .vchannels = 3,
119 .norm = VIDEO_MODE_PAL,
120 .tuner_type = TUNER_LG_PAL_NEW_TAPC, 137 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
121 .tda9887_conf = TDA9887_PRESENT, 138 .tda9887_conf = TDA9887_PRESENT,
122 .has_tuner = 1,
123 .decoder = EM28XX_SAA7113, 139 .decoder = EM28XX_SAA7113,
124 .input = {{ 140 .input = { {
125 .type = EM28XX_VMUX_TELEVISION, 141 .type = EM28XX_VMUX_TELEVISION,
126 .vmux = SAA7115_COMPOSITE2, 142 .vmux = SAA7115_COMPOSITE2,
127 .amux = 0, 143 .amux = 0,
128 },{ 144 }, {
129 .type = EM28XX_VMUX_COMPOSITE1, 145 .type = EM28XX_VMUX_COMPOSITE1,
130 .vmux = SAA7115_COMPOSITE0, 146 .vmux = SAA7115_COMPOSITE0,
131 .amux = 1, 147 .amux = 1,
132 },{ 148 }, {
133 .type = EM28XX_VMUX_SVIDEO, 149 .type = EM28XX_VMUX_SVIDEO,
134 .vmux = SAA7115_SVIDEO3, 150 .vmux = SAA7115_SVIDEO3,
135 .amux = 1, 151 .amux = 1,
136 }}, 152 } },
137 }, 153 },
138 [EM2820_BOARD_HAUPPAUGE_WINTV_USB_2] = { 154 [EM2820_BOARD_HAUPPAUGE_WINTV_USB_2] = {
139 .name = "Hauppauge WinTV USB 2", 155 .name = "Hauppauge WinTV USB 2",
140 .vchannels = 3, 156 .vchannels = 3,
141 .norm = VIDEO_MODE_NTSC,
142 .tuner_type = TUNER_PHILIPS_FM1236_MK3, 157 .tuner_type = TUNER_PHILIPS_FM1236_MK3,
143 .tda9887_conf = TDA9887_PRESENT|TDA9887_PORT1_ACTIVE|TDA9887_PORT2_ACTIVE, 158 .tda9887_conf = TDA9887_PRESENT |
144 .has_tuner = 1, 159 TDA9887_PORT1_ACTIVE|
160 TDA9887_PORT2_ACTIVE,
145 .decoder = EM28XX_TVP5150, 161 .decoder = EM28XX_TVP5150,
146 .has_msp34xx = 1, 162 .has_msp34xx = 1,
147 /*FIXME: S-Video not tested */ 163 /*FIXME: S-Video not tested */
148 .input = {{ 164 .input = { {
149 .type = EM28XX_VMUX_TELEVISION, 165 .type = EM28XX_VMUX_TELEVISION,
150 .vmux = TVP5150_COMPOSITE0, 166 .vmux = TVP5150_COMPOSITE0,
151 .amux = MSP_INPUT_DEFAULT, 167 .amux = MSP_INPUT_DEFAULT,
152 },{ 168 }, {
153 .type = EM28XX_VMUX_SVIDEO, 169 .type = EM28XX_VMUX_SVIDEO,
154 .vmux = TVP5150_SVIDEO, 170 .vmux = TVP5150_SVIDEO,
155 .amux = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1, 171 .amux = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
156 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART), 172 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART),
157 }}, 173 } },
158 }, 174 },
159 [EM2820_BOARD_MSI_VOX_USB_2] = { 175 [EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900] = {
160 .name = "MSI VOX USB 2.0", 176 .name = "Hauppauge WinTV HVR 900",
161 .vchannels = 3, 177 .vchannels = 3,
162 .norm = VIDEO_MODE_PAL, 178 .tda9887_conf = TDA9887_PRESENT,
163 .tuner_type = TUNER_LG_PAL_NEW_TAPC, 179 .tuner_type = TUNER_XC2028,
164 .tda9887_conf = TDA9887_PRESENT|TDA9887_PORT1_ACTIVE|TDA9887_PORT2_ACTIVE, 180 .mts_firmware = 1,
165 .has_tuner = 1, 181 .decoder = EM28XX_TVP5150,
166 .decoder = EM28XX_SAA7114, 182 .input = { {
167 .input = {{ 183 .type = EM28XX_VMUX_TELEVISION,
184 .vmux = TVP5150_COMPOSITE0,
185 .amux = 0,
186 }, {
187 .type = EM28XX_VMUX_COMPOSITE1,
188 .vmux = TVP5150_COMPOSITE1,
189 .amux = 1,
190 }, {
191 .type = EM28XX_VMUX_SVIDEO,
192 .vmux = TVP5150_SVIDEO,
193 .amux = 1,
194 } },
195 },
196 [EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950] = {
197 .name = "Hauppauge WinTV HVR 950",
198 .vchannels = 3,
199 .tda9887_conf = TDA9887_PRESENT,
200 .tuner_type = TUNER_XC2028,
201 .mts_firmware = 1,
202 .has_12mhz_i2s = 1,
203 .decoder = EM28XX_TVP5150,
204 .input = { {
168 .type = EM28XX_VMUX_TELEVISION, 205 .type = EM28XX_VMUX_TELEVISION,
169 .vmux = SAA7115_COMPOSITE4, 206 .vmux = TVP5150_COMPOSITE0,
170 .amux = 0, 207 .amux = 0,
171 },{ 208 }, {
172 .type = EM28XX_VMUX_COMPOSITE1, 209 .type = EM28XX_VMUX_COMPOSITE1,
173 .vmux = SAA7115_COMPOSITE0, 210 .vmux = TVP5150_COMPOSITE1,
174 .amux = 1, 211 .amux = 1,
175 },{ 212 }, {
176 .type = EM28XX_VMUX_SVIDEO, 213 .type = EM28XX_VMUX_SVIDEO,
177 .vmux = SAA7115_SVIDEO3, 214 .vmux = TVP5150_SVIDEO,
178 .amux = 1, 215 .amux = 1,
179 }}, 216 } },
217
218 /* gpio's 4, 1, 0 */
219 .analog_gpio = 0x003d2d,
220 },
221 [EM2880_BOARD_TERRATEC_HYBRID_XS] = {
222 .name = "Terratec Hybrid XS",
223 .vchannels = 3,
224 .tda9887_conf = TDA9887_PRESENT,
225 .tuner_type = TUNER_XC2028,
226 .decoder = EM28XX_TVP5150,
227 .input = { {
228 .type = EM28XX_VMUX_TELEVISION,
229 .vmux = TVP5150_COMPOSITE0,
230 .amux = 0,
231 }, {
232 .type = EM28XX_VMUX_COMPOSITE1,
233 .vmux = TVP5150_COMPOSITE1,
234 .amux = 1,
235 }, {
236 .type = EM28XX_VMUX_SVIDEO,
237 .vmux = TVP5150_SVIDEO,
238 .amux = 1,
239 } },
240 },
241 /* maybe there's a reason behind it why Terratec sells the Hybrid XS
242 as Prodigy XS with a different PID, let's keep it separated for now
243 maybe we'll need it lateron */
244 [EM2880_BOARD_TERRATEC_PRODIGY_XS] = {
245 .name = "Terratec Prodigy XS",
246 .vchannels = 3,
247 .tda9887_conf = TDA9887_PRESENT,
248 .tuner_type = TUNER_XC2028,
249 .decoder = EM28XX_TVP5150,
250 .input = { {
251 .type = EM28XX_VMUX_TELEVISION,
252 .vmux = TVP5150_COMPOSITE0,
253 .amux = 0,
254 }, {
255 .type = EM28XX_VMUX_COMPOSITE1,
256 .vmux = TVP5150_COMPOSITE1,
257 .amux = 1,
258 }, {
259 .type = EM28XX_VMUX_SVIDEO,
260 .vmux = TVP5150_SVIDEO,
261 .amux = 1,
262 } },
263 },
264 [EM2820_BOARD_MSI_VOX_USB_2] = {
265 .name = "MSI VOX USB 2.0",
266 .vchannels = 3,
267 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
268 .tda9887_conf = TDA9887_PRESENT |
269 TDA9887_PORT1_ACTIVE |
270 TDA9887_PORT2_ACTIVE,
271 .max_range_640_480 = 1,
272
273 .decoder = EM28XX_SAA7114,
274 .input = { {
275 .type = EM28XX_VMUX_TELEVISION,
276 .vmux = SAA7115_COMPOSITE4,
277 .amux = 0,
278 }, {
279 .type = EM28XX_VMUX_COMPOSITE1,
280 .vmux = SAA7115_COMPOSITE0,
281 .amux = 1,
282 }, {
283 .type = EM28XX_VMUX_SVIDEO,
284 .vmux = SAA7115_SVIDEO3,
285 .amux = 1,
286 } },
180 }, 287 },
181 [EM2800_BOARD_TERRATEC_CINERGY_200] = { 288 [EM2800_BOARD_TERRATEC_CINERGY_200] = {
182 .name = "Terratec Cinergy 200 USB", 289 .name = "Terratec Cinergy 200 USB",
183 .is_em2800 = 1, 290 .is_em2800 = 1,
184 .vchannels = 3, 291 .vchannels = 3,
185 .norm = VIDEO_MODE_PAL,
186 .tuner_type = TUNER_LG_PAL_NEW_TAPC, 292 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
187 .tda9887_conf = TDA9887_PRESENT, 293 .tda9887_conf = TDA9887_PRESENT,
188 .has_tuner = 1,
189 .decoder = EM28XX_SAA7113, 294 .decoder = EM28XX_SAA7113,
190 .input = {{ 295 .input = { {
191 .type = EM28XX_VMUX_TELEVISION, 296 .type = EM28XX_VMUX_TELEVISION,
192 .vmux = SAA7115_COMPOSITE2, 297 .vmux = SAA7115_COMPOSITE2,
193 .amux = 0, 298 .amux = 0,
194 },{ 299 }, {
195 .type = EM28XX_VMUX_COMPOSITE1, 300 .type = EM28XX_VMUX_COMPOSITE1,
196 .vmux = SAA7115_COMPOSITE0, 301 .vmux = SAA7115_COMPOSITE0,
197 .amux = 1, 302 .amux = 1,
198 },{ 303 }, {
199 .type = EM28XX_VMUX_SVIDEO, 304 .type = EM28XX_VMUX_SVIDEO,
200 .vmux = SAA7115_SVIDEO3, 305 .vmux = SAA7115_SVIDEO3,
201 .amux = 1, 306 .amux = 1,
202 }}, 307 } },
203 }, 308 },
204 [EM2800_BOARD_LEADTEK_WINFAST_USBII] = { 309 [EM2800_BOARD_LEADTEK_WINFAST_USBII] = {
205 .name = "Leadtek Winfast USB II", 310 .name = "Leadtek Winfast USB II",
206 .is_em2800 = 1, 311 .is_em2800 = 1,
207 .vchannels = 3, 312 .vchannels = 3,
208 .norm = VIDEO_MODE_PAL,
209 .tuner_type = TUNER_LG_PAL_NEW_TAPC, 313 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
210 .tda9887_conf = TDA9887_PRESENT, 314 .tda9887_conf = TDA9887_PRESENT,
211 .has_tuner = 1,
212 .decoder = EM28XX_SAA7113, 315 .decoder = EM28XX_SAA7113,
213 .input = {{ 316 .input = { {
214 .type = EM28XX_VMUX_TELEVISION, 317 .type = EM28XX_VMUX_TELEVISION,
215 .vmux = SAA7115_COMPOSITE2, 318 .vmux = SAA7115_COMPOSITE2,
216 .amux = 0, 319 .amux = 0,
217 },{ 320 }, {
218 .type = EM28XX_VMUX_COMPOSITE1, 321 .type = EM28XX_VMUX_COMPOSITE1,
219 .vmux = SAA7115_COMPOSITE0, 322 .vmux = SAA7115_COMPOSITE0,
220 .amux = 1, 323 .amux = 1,
221 },{ 324 }, {
222 .type = EM28XX_VMUX_SVIDEO, 325 .type = EM28XX_VMUX_SVIDEO,
223 .vmux = SAA7115_SVIDEO3, 326 .vmux = SAA7115_SVIDEO3,
224 .amux = 1, 327 .amux = 1,
225 }}, 328 } },
226 }, 329 },
227 [EM2800_BOARD_KWORLD_USB2800] = { 330 [EM2800_BOARD_KWORLD_USB2800] = {
228 .name = "Kworld USB2800", 331 .name = "Kworld USB2800",
229 .is_em2800 = 1, 332 .is_em2800 = 1,
230 .vchannels = 3, 333 .vchannels = 3,
231 .norm = VIDEO_MODE_PAL,
232 .tuner_type = TUNER_PHILIPS_ATSC, 334 .tuner_type = TUNER_PHILIPS_ATSC,
233 .tda9887_conf = TDA9887_PRESENT, 335 .tda9887_conf = TDA9887_PRESENT,
234 .has_tuner = 1,
235 .decoder = EM28XX_SAA7113, 336 .decoder = EM28XX_SAA7113,
236 .input = {{ 337 .input = { {
237 .type = EM28XX_VMUX_TELEVISION, 338 .type = EM28XX_VMUX_TELEVISION,
238 .vmux = SAA7115_COMPOSITE2, 339 .vmux = SAA7115_COMPOSITE2,
239 .amux = 0, 340 .amux = 0,
240 },{ 341 }, {
241 .type = EM28XX_VMUX_COMPOSITE1, 342 .type = EM28XX_VMUX_COMPOSITE1,
242 .vmux = SAA7115_COMPOSITE0, 343 .vmux = SAA7115_COMPOSITE0,
243 .amux = 1, 344 .amux = 1,
244 },{ 345 }, {
245 .type = EM28XX_VMUX_SVIDEO, 346 .type = EM28XX_VMUX_SVIDEO,
246 .vmux = SAA7115_SVIDEO3, 347 .vmux = SAA7115_SVIDEO3,
247 .amux = 1, 348 .amux = 1,
248 }}, 349 } },
249 }, 350 },
250 [EM2820_BOARD_PINNACLE_DVC_90] = { 351 [EM2820_BOARD_PINNACLE_DVC_90] = {
251 .name = "Pinnacle Dazzle DVC 90", 352 .name = "Pinnacle Dazzle DVC 90/DVC 100",
353 .vchannels = 3,
354 .tuner_type = TUNER_ABSENT,
355 .decoder = EM28XX_SAA7113,
356 .input = { {
357 .type = EM28XX_VMUX_COMPOSITE1,
358 .vmux = SAA7115_COMPOSITE0,
359 .amux = 1,
360 }, {
361 .type = EM28XX_VMUX_SVIDEO,
362 .vmux = SAA7115_SVIDEO3,
363 .amux = 1,
364 } },
365 },
366 [EM2800_BOARD_VGEAR_POCKETTV] = {
367 .name = "V-Gear PocketTV",
368 .is_em2800 = 1,
369 .vchannels = 3,
370 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
371 .tda9887_conf = TDA9887_PRESENT,
372 .decoder = EM28XX_SAA7113,
373 .input = { {
374 .type = EM28XX_VMUX_TELEVISION,
375 .vmux = SAA7115_COMPOSITE2,
376 .amux = 0,
377 }, {
378 .type = EM28XX_VMUX_COMPOSITE1,
379 .vmux = SAA7115_COMPOSITE0,
380 .amux = 1,
381 }, {
382 .type = EM28XX_VMUX_SVIDEO,
383 .vmux = SAA7115_SVIDEO3,
384 .amux = 1,
385 } },
386 },
387 [EM2820_BOARD_PROLINK_PLAYTV_USB2] = {
388 .name = "Pixelview Prolink PlayTV USB 2.0",
252 .vchannels = 3, 389 .vchannels = 3,
253 .norm = VIDEO_MODE_PAL, 390 .tda9887_conf = TDA9887_PRESENT,
254 .has_tuner = 0, 391 .tuner_type = TUNER_YMEC_TVF_5533MF,
255 .decoder = EM28XX_SAA7113, 392 .decoder = EM28XX_SAA7113,
256 .input = {{ 393 .input = { {
394 .type = EM28XX_VMUX_TELEVISION,
395 .vmux = SAA7115_COMPOSITE2,
396 .amux = 1,
397 }, {
257 .type = EM28XX_VMUX_COMPOSITE1, 398 .type = EM28XX_VMUX_COMPOSITE1,
258 .vmux = SAA7115_COMPOSITE0, 399 .vmux = SAA7115_COMPOSITE0,
259 .amux = 1, 400 .amux = 1,
260 },{ 401 }, {
261 .type = EM28XX_VMUX_SVIDEO, 402 .type = EM28XX_VMUX_SVIDEO,
262 .vmux = SAA7115_SVIDEO3, 403 .vmux = SAA7115_SVIDEO3,
263 .amux = 1, 404 .amux = 1,
264 }}, 405 } },
265 }, 406 },
266}; 407};
267const unsigned int em28xx_bcount = ARRAY_SIZE(em28xx_boards); 408const unsigned int em28xx_bcount = ARRAY_SIZE(em28xx_boards);
268 409
269/* table of devices that work with this driver */ 410/* table of devices that work with this driver */
270struct usb_device_id em28xx_id_table [] = { 411struct usb_device_id em28xx_id_table [] = {
271 { USB_DEVICE(0xeb1a, 0x2800), .driver_info = EM2800_BOARD_UNKNOWN }, 412 { USB_DEVICE(0xeb1a, 0x2750),
272 { USB_DEVICE(0xeb1a, 0x2820), .driver_info = EM2820_BOARD_MSI_VOX_USB_2 }, 413 .driver_info = EM2820_BOARD_UNKNOWN },
273 { USB_DEVICE(0x0ccd, 0x0036), .driver_info = EM2820_BOARD_TERRATEC_CINERGY_250 }, 414 { USB_DEVICE(0xeb1a, 0x2800),
274 { USB_DEVICE(0x2304, 0x0208), .driver_info = EM2820_BOARD_PINNACLE_USB_2 }, 415 .driver_info = EM2800_BOARD_UNKNOWN },
275 { USB_DEVICE(0x2040, 0x4200), .driver_info = EM2820_BOARD_HAUPPAUGE_WINTV_USB_2 }, 416 { USB_DEVICE(0xeb1a, 0x2820),
276 { USB_DEVICE(0x2304, 0x0207), .driver_info = EM2820_BOARD_PINNACLE_DVC_90 }, 417 .driver_info = EM2820_BOARD_UNKNOWN },
418 { USB_DEVICE(0xeb1a, 0x2821),
419 .driver_info = EM2820_BOARD_UNKNOWN },
420 { USB_DEVICE(0xeb1a, 0x2860),
421 .driver_info = EM2820_BOARD_UNKNOWN },
422 { USB_DEVICE(0xeb1a, 0x2861),
423 .driver_info = EM2820_BOARD_UNKNOWN },
424 { USB_DEVICE(0xeb1a, 0x2870),
425 .driver_info = EM2820_BOARD_UNKNOWN },
426 { USB_DEVICE(0xeb1a, 0x2881),
427 .driver_info = EM2820_BOARD_UNKNOWN },
428 { USB_DEVICE(0xeb1a, 0x2883),
429 .driver_info = EM2820_BOARD_UNKNOWN },
430 { USB_DEVICE(0x0ccd, 0x0036),
431 .driver_info = EM2820_BOARD_TERRATEC_CINERGY_250 },
432 { USB_DEVICE(0x2304, 0x0208),
433 .driver_info = EM2820_BOARD_PINNACLE_USB_2 },
434 { USB_DEVICE(0x2040, 0x4200),
435 .driver_info = EM2820_BOARD_HAUPPAUGE_WINTV_USB_2 },
436 { USB_DEVICE(0x2040, 0x4201),
437 .driver_info = EM2820_BOARD_HAUPPAUGE_WINTV_USB_2 },
438 { USB_DEVICE(0x2304, 0x0207),
439 .driver_info = EM2820_BOARD_PINNACLE_DVC_90 },
440 { USB_DEVICE(0x2304, 0x021a),
441 .driver_info = EM2820_BOARD_PINNACLE_DVC_90 },
442 { USB_DEVICE(0x2040, 0x6500),
443 .driver_info = EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900 },
444 { USB_DEVICE(0x2040, 0x6513),
445 .driver_info = EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950 },
446 { USB_DEVICE(0x0ccd, 0x0042),
447 .driver_info = EM2880_BOARD_TERRATEC_HYBRID_XS },
448 { USB_DEVICE(0x0ccd, 0x0047),
449 .driver_info = EM2880_BOARD_TERRATEC_PRODIGY_XS },
277 { }, 450 { },
278}; 451};
452MODULE_DEVICE_TABLE(usb, em28xx_id_table);
453
454/* EEPROM hash table for devices with generic USB IDs */
455static struct em28xx_hash_table em28xx_eeprom_hash [] = {
456 /* P/N: SA 60002070465 Tuner: TVF7533-MF */
457 {0x6ce05a8f, EM2820_BOARD_PROLINK_PLAYTV_USB2, TUNER_YMEC_TVF_5533MF},
458};
459
460/* I2C devicelist hash table for devices with generic USB IDs */
461static struct em28xx_hash_table em28xx_i2c_hash[] = {
462 {0xb06a32c3, EM2800_BOARD_TERRATEC_CINERGY_200, TUNER_LG_PAL_NEW_TAPC},
463 {0xf51200e3, EM2800_BOARD_VGEAR_POCKETTV, TUNER_LG_PAL_NEW_TAPC},
464};
279 465
466/* Since em28xx_pre_card_setup() requires a proper dev->model,
467 * this won't work for boards with generic PCI IDs
468 */
280void em28xx_pre_card_setup(struct em28xx *dev) 469void em28xx_pre_card_setup(struct em28xx *dev)
281{ 470{
282 /* request some modules */ 471 /* request some modules */
283 switch(dev->model){ 472 switch (dev->model) {
284 case EM2880_BOARD_TERRATEC_PRODIGY_XS: 473 case EM2880_BOARD_TERRATEC_PRODIGY_XS:
285 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900: 474 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
286 case EM2880_BOARD_TERRATEC_HYBRID_XS: 475 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
287 { 476 case EM2880_BOARD_TERRATEC_HYBRID_XS:
288 em28xx_write_regs_req(dev, 0x00, 0x08, "\x7d", 1); // reset through GPIO? 477 em28xx_write_regs(dev, XCLK_REG, "\x27", 1);
289 break; 478 em28xx_write_regs(dev, I2C_CLK_REG, "\x40", 1);
479 em28xx_write_regs(dev, 0x08, "\xff", 1);
480 em28xx_write_regs(dev, 0x04, "\x00", 1);
481 msleep(100);
482 em28xx_write_regs(dev, 0x04, "\x08", 1);
483 msleep(100);
484 em28xx_write_regs(dev, 0x08, "\xff", 1);
485 msleep(50);
486 em28xx_write_regs(dev, 0x08, "\x2d", 1);
487 msleep(50);
488 em28xx_write_regs(dev, 0x08, "\x3d", 1);
489 break;
490 }
491}
492
493static int em28xx_tuner_callback(void *ptr, int command, int arg)
494{
495 int rc = 0;
496 struct em28xx *dev = ptr;
497
498 if (dev->tuner_type != TUNER_XC2028)
499 return 0;
500
501 switch (command) {
502 case XC2028_TUNER_RESET:
503 {
504 /* GPIO and initialization codes for analog TV and radio
505 This code should be complemented for DTV, since reset
506 codes are different.
507 */
508
509 dev->em28xx_write_regs_req(dev, 0x00, 0x48, "\x00", 1);
510 dev->em28xx_write_regs_req(dev, 0x00, 0x12, "\x67", 1);
511
512 if (dev->analog_gpio) {
513 char gpio0 = dev->analog_gpio & 0xff;
514 char gpio1 = (dev->analog_gpio >> 8) & 0xff;
515 char gpio4 = dev->analog_gpio >> 24;
516
517 if (gpio4) {
518 dev->em28xx_write_regs(dev, 0x04, &gpio4, 1);
519 msleep(140);
290 } 520 }
521
522 msleep(6);
523 dev->em28xx_write_regs(dev, 0x08, &gpio0, 1);
524 msleep(10);
525 dev->em28xx_write_regs(dev, 0x08, &gpio1, 1);
526 msleep(5);
527 }
528
529 break;
530 }
531 }
532 return rc;
533}
534
535static void em28xx_config_tuner(struct em28xx *dev)
536{
537 struct v4l2_priv_tun_config xc2028_cfg;
538 struct xc2028_ctrl ctl;
539 struct tuner_setup tun_setup;
540 struct v4l2_frequency f;
541
542 if (dev->tuner_type == TUNER_ABSENT)
543 return;
544
545 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
546 tun_setup.type = dev->tuner_type;
547 tun_setup.addr = dev->tuner_addr;
548 tun_setup.tuner_callback = em28xx_tuner_callback;
549
550 em28xx_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup);
551
552 if (dev->tuner_type == TUNER_XC2028) {
553 memset(&ctl, 0, sizeof(ctl));
554
555 ctl.fname = XC2028_DEFAULT_FIRMWARE;
556 ctl.max_len = 64;
557 ctl.mts = em28xx_boards[dev->model].mts_firmware;
558
559 xc2028_cfg.tuner = TUNER_XC2028;
560 xc2028_cfg.priv = &ctl;
561
562 em28xx_i2c_call_clients(dev, TUNER_SET_CONFIG, &xc2028_cfg);
563 }
564
565 /* configure tuner */
566 f.tuner = 0;
567 f.type = V4L2_TUNER_ANALOG_TV;
568 f.frequency = 9076; /* just a magic number */
569 dev->ctl_freq = f.frequency;
570 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
571}
572
573static int em28xx_hint_board(struct em28xx *dev)
574{
575 int i;
576
577 /* HINT method: EEPROM
578 *
579 * This method works only for boards with eeprom.
580 * Uses a hash of all eeprom bytes. The hash should be
581 * unique for a vendor/tuner pair.
582 * There are a high chance that tuners for different
583 * video standards produce different hashes.
584 */
585 for (i = 0; i < ARRAY_SIZE(em28xx_eeprom_hash); i++) {
586 if (dev->hash == em28xx_eeprom_hash[i].hash) {
587 dev->model = em28xx_eeprom_hash[i].model;
588 dev->tuner_type = em28xx_eeprom_hash[i].tuner;
589
590 em28xx_errdev("Your board has no unique USB ID.\n");
591 em28xx_errdev("A hint were successfully done, "
592 "based on eeprom hash.\n");
593 em28xx_errdev("This method is not 100%% failproof.\n");
594 em28xx_errdev("If the board were missdetected, "
595 "please email this log to:\n");
596 em28xx_errdev("\tV4L Mailing List "
597 " <video4linux-list@redhat.com>\n");
598 em28xx_errdev("Board detected as %s\n",
599 em28xx_boards[dev->model].name);
600
601 return 0;
602 }
603 }
604
605 /* HINT method: I2C attached devices
606 *
607 * This method works for all boards.
608 * Uses a hash of i2c scanned devices.
609 * Devices with the same i2c attached chips will
610 * be considered equal.
611 * This method is less precise than the eeprom one.
612 */
613
614 /* user did not request i2c scanning => do it now */
615 if (!dev->i2c_hash)
616 em28xx_do_i2c_scan(dev);
617
618 for (i = 0; i < ARRAY_SIZE(em28xx_i2c_hash); i++) {
619 if (dev->i2c_hash == em28xx_i2c_hash[i].hash) {
620 dev->model = em28xx_i2c_hash[i].model;
621 dev->tuner_type = em28xx_i2c_hash[i].tuner;
622 em28xx_errdev("Your board has no unique USB ID.\n");
623 em28xx_errdev("A hint were successfully done, "
624 "based on i2c devicelist hash.\n");
625 em28xx_errdev("This method is not 100%% failproof.\n");
626 em28xx_errdev("If the board were missdetected, "
627 "please email this log to:\n");
628 em28xx_errdev("\tV4L Mailing List "
629 " <video4linux-list@redhat.com>\n");
630 em28xx_errdev("Board detected as %s\n",
631 em28xx_boards[dev->model].name);
632
633 return 0;
634 }
635 }
636
637 em28xx_errdev("Your board has no unique USB ID and thus need a "
638 "hint to be detected.\n");
639 em28xx_errdev("You may try to use card=<n> insmod option to "
640 "workaround that.\n");
641 em28xx_errdev("Please send an email with this log to:\n");
642 em28xx_errdev("\tV4L Mailing List <video4linux-list@redhat.com>\n");
643 em28xx_errdev("Board eeprom hash is 0x%08lx\n", dev->hash);
644 em28xx_errdev("Board i2c devicelist hash is 0x%08lx\n", dev->i2c_hash);
645
646 em28xx_errdev("Here is a list of valid choices for the card=<n>"
647 " insmod option:\n");
648 for (i = 0; i < em28xx_bcount; i++) {
649 em28xx_errdev(" card=%d -> %s\n",
650 i, em28xx_boards[i].name);
651 }
652 return -1;
653}
654
655
656static void em28xx_set_model(struct em28xx *dev)
657{
658 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
659 dev->has_msp34xx = em28xx_boards[dev->model].has_msp34xx;
660 dev->tda9887_conf = em28xx_boards[dev->model].tda9887_conf;
661 dev->decoder = em28xx_boards[dev->model].decoder;
662 dev->video_inputs = em28xx_boards[dev->model].vchannels;
663 dev->analog_gpio = em28xx_boards[dev->model].analog_gpio;
664 dev->has_12mhz_i2s = em28xx_boards[dev->model].has_12mhz_i2s;
665 dev->max_range_640_480 = em28xx_boards[dev->model].max_range_640_480;
666}
667
668/* ----------------------------------------------------------------------- */
669void em28xx_set_ir(struct em28xx *dev, struct IR_i2c *ir)
670{
671 if (disable_ir) {
672 ir->get_key = NULL;
673 return ;
674 }
675
676 /* detect & configure */
677 switch (dev->model) {
678 case (EM2800_BOARD_UNKNOWN):
679 break;
680 case (EM2820_BOARD_UNKNOWN):
681 break;
682 case (EM2800_BOARD_TERRATEC_CINERGY_200):
683 case (EM2820_BOARD_TERRATEC_CINERGY_250):
684 ir->ir_codes = ir_codes_em_terratec;
685 ir->get_key = em28xx_get_key_terratec;
686 snprintf(ir->c.name, sizeof(ir->c.name),
687 "i2c IR (EM28XX Terratec)");
688 break;
689 case (EM2820_BOARD_PINNACLE_USB_2):
690 ir->ir_codes = ir_codes_pinnacle_grey;
691 ir->get_key = em28xx_get_key_pinnacle_usb_grey;
692 snprintf(ir->c.name, sizeof(ir->c.name),
693 "i2c IR (EM28XX Pinnacle PCTV)");
694 break;
695 case (EM2820_BOARD_HAUPPAUGE_WINTV_USB_2):
696 ir->ir_codes = ir_codes_hauppauge_new;
697 ir->get_key = em28xx_get_key_em_haup;
698 snprintf(ir->c.name, sizeof(ir->c.name),
699 "i2c IR (EM2840 Hauppauge)");
700 break;
701 case (EM2820_BOARD_MSI_VOX_USB_2):
702 break;
703 case (EM2800_BOARD_LEADTEK_WINFAST_USBII):
704 break;
705 case (EM2800_BOARD_KWORLD_USB2800):
706 break;
291 } 707 }
292} 708}
293 709
294void em28xx_card_setup(struct em28xx *dev) 710void em28xx_card_setup(struct em28xx *dev)
295{ 711{
712 em28xx_set_model(dev);
713
714 dev->tuner_type = em28xx_boards[dev->model].tuner_type;
715
296 /* request some modules */ 716 /* request some modules */
297 switch(dev->model){ 717 switch (dev->model) {
298 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2: 718 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
299 { 719 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
300 struct tveeprom tv; 720 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
721 {
722 struct tveeprom tv;
301#ifdef CONFIG_MODULES 723#ifdef CONFIG_MODULES
302 request_module("tveeprom"); 724 request_module("tveeprom");
303 request_module("ir-kbd-i2c");
304 request_module("msp3400");
305#endif 725#endif
306 /* Call first TVeeprom */ 726 /* Call first TVeeprom */
307 727
308 dev->i2c_client.addr = 0xa0 >> 1; 728 dev->i2c_client.addr = 0xa0 >> 1;
309 tveeprom_hauppauge_analog(&dev->i2c_client, &tv, dev->eedata); 729 tveeprom_hauppauge_analog(&dev->i2c_client, &tv, dev->eedata);
310
311 dev->tuner_type= tv.tuner_type;
312 if (tv.audio_processor == AUDIO_CHIP_MSP34XX) {
313 dev->i2s_speed=2048000;
314 dev->has_msp34xx=1;
315 } else
316 dev->has_msp34xx=0;
317 break;
318 }
319 case EM2820_BOARD_KWORLD_PVRTV2800RF:
320 {
321 em28xx_write_regs_req(dev,0x00,0x08, "\xf9", 1); // GPIO enables sound on KWORLD PVR TV 2800RF
322 break;
323 }
324 730
731 dev->tuner_type = tv.tuner_type;
732
733 if (tv.audio_processor == AUDIO_CHIP_MSP34XX) {
734 dev->i2s_speed = 2048000;
735 dev->has_msp34xx = 1;
736 }
737#ifdef CONFIG_MODULES
738 if (tv.has_ir)
739 request_module("ir-kbd-i2c");
740#endif
741 break;
742 }
743 case EM2820_BOARD_KWORLD_PVRTV2800RF:
744 /* GPIO enables sound on KWORLD PVR TV 2800RF */
745 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf9", 1);
746 break;
747 case EM2820_BOARD_UNKNOWN:
748 case EM2800_BOARD_UNKNOWN:
749 if (!em28xx_hint_board(dev))
750 em28xx_set_model(dev);
325 } 751 }
326}
327 752
328MODULE_DEVICE_TABLE (usb, em28xx_id_table); 753 /* Allow override tuner type by a module parameter */
754 if (tuner >= 0)
755 dev->tuner_type = tuner;
756
757#ifdef CONFIG_MODULES
758 /* request some modules */
759 if (dev->has_msp34xx)
760 request_module("msp3400");
761 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
762 request_module("saa7115");
763 if (dev->decoder == EM28XX_TVP5150)
764 request_module("tvp5150");
765 if (dev->tuner_type != TUNER_ABSENT)
766 request_module("tuner");
767#endif
768
769 em28xx_config_tuner(dev);
770}
diff --git a/drivers/media/video/em28xx/em28xx-core.c b/drivers/media/video/em28xx/em28xx-core.c
index d56484f20467..f6b78357f0e5 100644
--- a/drivers/media/video/em28xx/em28xx-core.c
+++ b/drivers/media/video/em28xx/em28xx-core.c
@@ -252,7 +252,7 @@ int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
252 * em28xx_write_ac97() 252 * em28xx_write_ac97()
253 * write a 16 bit value to the specified AC97 address (LSB first!) 253 * write a 16 bit value to the specified AC97 address (LSB first!)
254 */ 254 */
255int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 * val) 255static int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 *val)
256{ 256{
257 int ret; 257 int ret;
258 u8 addr = reg & 0x7f; 258 u8 addr = reg & 0x7f;
@@ -268,16 +268,98 @@ int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 * val)
268 return 0; 268 return 0;
269} 269}
270 270
271int em28xx_set_audio_source(struct em28xx *dev)
272{
273 static char *enable = "\x08\x08";
274 static char *disable = "\x08\x88";
275 char *video = enable, *line = disable;
276 int ret, no_ac97;
277 u8 input;
278
279 if (dev->is_em2800) {
280 if (dev->ctl_ainput)
281 input = EM2800_AUDIO_SRC_LINE;
282 else
283 input = EM2800_AUDIO_SRC_TUNER;
284
285 ret = em28xx_write_regs(dev, EM2800_AUDIOSRC_REG, &input, 1);
286 if (ret < 0)
287 return ret;
288 }
289
290 if (dev->has_msp34xx)
291 input = EM28XX_AUDIO_SRC_TUNER;
292 else {
293 switch (dev->ctl_ainput) {
294 case EM28XX_AMUX_VIDEO:
295 input = EM28XX_AUDIO_SRC_TUNER;
296 no_ac97 = 1;
297 break;
298 case EM28XX_AMUX_LINE_IN:
299 input = EM28XX_AUDIO_SRC_LINE;
300 no_ac97 = 1;
301 break;
302 case EM28XX_AMUX_AC97_VIDEO:
303 input = EM28XX_AUDIO_SRC_LINE;
304 break;
305 case EM28XX_AMUX_AC97_LINE_IN:
306 input = EM28XX_AUDIO_SRC_LINE;
307 video = disable;
308 line = enable;
309 break;
310 }
311 }
312
313 ret = em28xx_write_reg_bits(dev, AUDIOSRC_REG, input, 0xc0);
314 if (ret < 0)
315 return ret;
316
317 if (no_ac97)
318 return 0;
319
320 /* Sets AC97 mixer registers */
321
322 ret = em28xx_write_ac97(dev, VIDEO_AC97, video);
323 if (ret < 0)
324 return ret;
325
326 ret = em28xx_write_ac97(dev, LINE_IN_AC97, line);
327
328 return ret;
329}
330
271int em28xx_audio_analog_set(struct em28xx *dev) 331int em28xx_audio_analog_set(struct em28xx *dev)
272{ 332{
333 int ret;
273 char s[2] = { 0x00, 0x00 }; 334 char s[2] = { 0x00, 0x00 };
335 u8 xclk = 0x07;
336
274 s[0] |= 0x1f - dev->volume; 337 s[0] |= 0x1f - dev->volume;
275 s[1] |= 0x1f - dev->volume; 338 s[1] |= 0x1f - dev->volume;
339
276 if (dev->mute) 340 if (dev->mute)
277 s[1] |= 0x80; 341 s[1] |= 0x80;
278 return em28xx_write_ac97(dev, MASTER_AC97, s); 342 ret = em28xx_write_ac97(dev, MASTER_AC97, s);
279} 343 if (ret < 0)
344 return ret;
345
346 if (dev->has_12mhz_i2s)
347 xclk |= 0x20;
348
349 if (!dev->mute)
350 xclk |= 0x80;
280 351
352 ret = em28xx_write_reg_bits(dev, XCLK_REG, xclk, 0xa7);
353 if (ret < 0)
354 return ret;
355 msleep(10);
356
357 /* Selects the proper audio input */
358 ret = em28xx_set_audio_source(dev);
359
360 return ret;
361}
362EXPORT_SYMBOL_GPL(em28xx_audio_analog_set);
281 363
282int em28xx_colorlevels_set_default(struct em28xx *dev) 364int em28xx_colorlevels_set_default(struct em28xx *dev)
283{ 365{
diff --git a/drivers/media/video/em28xx/em28xx-i2c.c b/drivers/media/video/em28xx/em28xx-i2c.c
index e3a4aa7a9df4..cacd04d46e99 100644
--- a/drivers/media/video/em28xx/em28xx-i2c.c
+++ b/drivers/media/video/em28xx/em28xx-i2c.c
@@ -25,9 +25,9 @@
25#include <linux/kernel.h> 25#include <linux/kernel.h>
26#include <linux/usb.h> 26#include <linux/usb.h>
27#include <linux/i2c.h> 27#include <linux/i2c.h>
28#include <linux/video_decoder.h>
29 28
30#include "em28xx.h" 29#include "em28xx.h"
30#include "tuner-xc2028.h"
31#include <media/v4l2-common.h> 31#include <media/v4l2-common.h>
32#include <media/tuner.h> 32#include <media/tuner.h>
33 33
@@ -291,6 +291,31 @@ static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
291 return rc; 291 return rc;
292} 292}
293 293
294/* based on linux/sunrpc/svcauth.h and linux/hash.h
295 * The original hash function returns a different value, if arch is x86_64
296 * or i386.
297 */
298static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
299{
300 unsigned long hash = 0;
301 unsigned long l = 0;
302 int len = 0;
303 unsigned char c;
304 do {
305 if (len == length) {
306 c = (char)len;
307 len = -1;
308 } else
309 c = *buf++;
310 l = (l << 8) | c;
311 len++;
312 if ((len & (32 / 8 - 1)) == 0)
313 hash = ((hash^l) * 0x9e370001UL);
314 } while (len);
315
316 return (hash >> (32 - bits)) & 0xffffffffUL;
317}
318
294static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len) 319static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len)
295{ 320{
296 unsigned char buf, *p = eedata; 321 unsigned char buf, *p = eedata;
@@ -334,7 +359,11 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len)
334 printk("\n"); 359 printk("\n");
335 } 360 }
336 361
337 printk(KERN_INFO "EEPROM ID= 0x%08x\n", em_eeprom->id); 362 if (em_eeprom->id == 0x9567eb1a)
363 dev->hash = em28xx_hash_mem(eedata, len, 32);
364
365 printk(KERN_INFO "EEPROM ID= 0x%08x, hash = 0x%08lx\n",
366 em_eeprom->id, dev->hash);
338 printk(KERN_INFO "Vendor/Product ID= %04x:%04x\n", em_eeprom->vendor_ID, 367 printk(KERN_INFO "Vendor/Product ID= %04x:%04x\n", em_eeprom->vendor_ID,
339 em_eeprom->product_ID); 368 em_eeprom->product_ID);
340 369
@@ -391,21 +420,6 @@ static u32 functionality(struct i2c_adapter *adap)
391} 420}
392 421
393 422
394static int em28xx_set_tuner(int check_eeprom, struct i2c_client *client)
395{
396 struct em28xx *dev = client->adapter->algo_data;
397 struct tuner_setup tun_setup;
398
399 if (dev->has_tuner) {
400 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
401 tun_setup.type = dev->tuner_type;
402 tun_setup.addr = dev->tuner_addr;
403 em28xx_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup);
404 }
405
406 return (0);
407}
408
409/* 423/*
410 * attach_inform() 424 * attach_inform()
411 * gets called when a device attaches to the i2c bus 425 * gets called when a device attaches to the i2c bus
@@ -421,6 +435,8 @@ static int attach_inform(struct i2c_client *client)
421 case 0x96: 435 case 0x96:
422 case 0x94: 436 case 0x94:
423 { 437 {
438 struct v4l2_priv_tun_config tda9887_cfg;
439
424 struct tuner_setup tun_setup; 440 struct tuner_setup tun_setup;
425 441
426 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO; 442 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
@@ -428,7 +444,11 @@ static int attach_inform(struct i2c_client *client)
428 tun_setup.addr = client->addr; 444 tun_setup.addr = client->addr;
429 445
430 em28xx_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup); 446 em28xx_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup);
431 em28xx_i2c_call_clients(dev, TDA9887_SET_CONFIG, &dev->tda9887_conf); 447
448 tda9887_cfg.tuner = TUNER_TDA9887;
449 tda9887_cfg.priv = &dev->tda9887_conf;
450 em28xx_i2c_call_clients(dev, TUNER_SET_CONFIG,
451 &tda9887_cfg);
432 break; 452 break;
433 } 453 }
434 case 0x42: 454 case 0x42:
@@ -458,9 +478,11 @@ static int attach_inform(struct i2c_client *client)
458 break; 478 break;
459 479
460 default: 480 default:
481 if (!dev->tuner_addr)
482 dev->tuner_addr = client->addr;
483
461 dprintk1(1,"attach inform: detected I2C address %x\n", client->addr << 1); 484 dprintk1(1,"attach inform: detected I2C address %x\n", client->addr << 1);
462 dev->tuner_addr = client->addr; 485
463 em28xx_set_tuner(-1, client);
464 } 486 }
465 487
466 return 0; 488 return 0;
@@ -510,19 +532,26 @@ static char *i2c_devs[128] = {
510 * do_i2c_scan() 532 * do_i2c_scan()
511 * check i2c address range for devices 533 * check i2c address range for devices
512 */ 534 */
513static void do_i2c_scan(char *name, struct i2c_client *c) 535void em28xx_do_i2c_scan(struct em28xx *dev)
514{ 536{
537 u8 i2c_devicelist[128];
515 unsigned char buf; 538 unsigned char buf;
516 int i, rc; 539 int i, rc;
517 540
541 memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
542
518 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { 543 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
519 c->addr = i; 544 dev->i2c_client.addr = i;
520 rc = i2c_master_recv(c, &buf, 0); 545 rc = i2c_master_recv(&dev->i2c_client, &buf, 0);
521 if (rc < 0) 546 if (rc < 0)
522 continue; 547 continue;
523 printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n", name, 548 i2c_devicelist[i] = i;
524 i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); 549 printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n",
550 dev->name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
525 } 551 }
552
553 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
554 ARRAY_SIZE(i2c_devicelist), 32);
526} 555}
527 556
528/* 557/*
@@ -555,7 +584,7 @@ int em28xx_i2c_register(struct em28xx *dev)
555 em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata)); 584 em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata));
556 585
557 if (i2c_scan) 586 if (i2c_scan)
558 do_i2c_scan(dev->name, &dev->i2c_client); 587 em28xx_do_i2c_scan(dev);
559 return 0; 588 return 0;
560} 589}
561 590
diff --git a/drivers/media/video/em28xx/em28xx-input.c b/drivers/media/video/em28xx/em28xx-input.c
index e3894b68c4ee..10da2fd8d987 100644
--- a/drivers/media/video/em28xx/em28xx-input.c
+++ b/drivers/media/video/em28xx/em28xx-input.c
@@ -30,11 +30,7 @@
30 30
31#include "em28xx.h" 31#include "em28xx.h"
32 32
33static unsigned int disable_ir = 0; 33static unsigned int ir_debug;
34module_param(disable_ir, int, 0444);
35MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
36
37static unsigned int ir_debug = 0;
38module_param(ir_debug, int, 0644); 34module_param(ir_debug, int, 0644);
39MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]"); 35MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
40 36
@@ -43,7 +39,7 @@ MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
43 39
44/* ----------------------------------------------------------------------- */ 40/* ----------------------------------------------------------------------- */
45 41
46static int get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 42int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
47{ 43{
48 unsigned char b; 44 unsigned char b;
49 45
@@ -72,7 +68,7 @@ static int get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
72} 68}
73 69
74 70
75static int get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 71int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
76{ 72{
77 unsigned char buf[2]; 73 unsigned char buf[2];
78 unsigned char code; 74 unsigned char code;
@@ -103,7 +99,8 @@ static int get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
103 return 1; 99 return 1;
104} 100}
105 101
106static int get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 102int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
103 u32 *ir_raw)
107{ 104{
108 unsigned char buf[3]; 105 unsigned char buf[3];
109 106
@@ -125,45 +122,6 @@ static int get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw
125 return 1; 122 return 1;
126} 123}
127 124
128/* ----------------------------------------------------------------------- */
129void em28xx_set_ir(struct em28xx * dev,struct IR_i2c *ir)
130{
131 if (disable_ir) {
132 ir->get_key=NULL;
133 return ;
134 }
135
136 /* detect & configure */
137 switch (dev->model) {
138 case (EM2800_BOARD_UNKNOWN):
139 break;
140 case (EM2820_BOARD_UNKNOWN):
141 break;
142 case (EM2800_BOARD_TERRATEC_CINERGY_200):
143 case (EM2820_BOARD_TERRATEC_CINERGY_250):
144 ir->ir_codes = ir_codes_em_terratec;
145 ir->get_key = get_key_terratec;
146 snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (EM28XX Terratec)");
147 break;
148 case (EM2820_BOARD_PINNACLE_USB_2):
149 ir->ir_codes = ir_codes_pinnacle_grey;
150 ir->get_key = get_key_pinnacle_usb_grey;
151 snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (EM28XX Pinnacle PCTV)");
152 break;
153 case (EM2820_BOARD_HAUPPAUGE_WINTV_USB_2):
154 ir->ir_codes = ir_codes_hauppauge_new;
155 ir->get_key = get_key_em_haup;
156 snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (EM2840 Hauppauge)");
157 break;
158 case (EM2820_BOARD_MSI_VOX_USB_2):
159 break;
160 case (EM2800_BOARD_LEADTEK_WINFAST_USBII):
161 break;
162 case (EM2800_BOARD_KWORLD_USB2800):
163 break;
164 }
165}
166
167/* ---------------------------------------------------------------------- 125/* ----------------------------------------------------------------------
168 * Local variables: 126 * Local variables:
169 * c-basic-offset: 8 127 * c-basic-offset: 8
diff --git a/drivers/media/video/em28xx/em28xx-video.c b/drivers/media/video/em28xx/em28xx-video.c
index 0906bc5766cc..a0c334672488 100644
--- a/drivers/media/video/em28xx/em28xx-video.c
+++ b/drivers/media/video/em28xx/em28xx-video.c
@@ -33,13 +33,12 @@
33#include <linux/i2c.h> 33#include <linux/i2c.h>
34#include <linux/version.h> 34#include <linux/version.h>
35#include <linux/mm.h> 35#include <linux/mm.h>
36#include <linux/video_decoder.h>
37#include <linux/mutex.h> 36#include <linux/mutex.h>
38 37
39#include "em28xx.h" 38#include "em28xx.h"
40#include <media/tuner.h>
41#include <media/v4l2-common.h> 39#include <media/v4l2-common.h>
42#include <media/msp3400.h> 40#include <media/msp3400.h>
41#include <media/tuner.h>
43 42
44#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \ 43#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
45 "Markus Rechberger <mrechberger@gmail.com>, " \ 44 "Markus Rechberger <mrechberger@gmail.com>, " \
@@ -48,7 +47,7 @@
48 47
49#define DRIVER_NAME "em28xx" 48#define DRIVER_NAME "em28xx"
50#define DRIVER_DESC "Empia em28xx based USB video device driver" 49#define DRIVER_DESC "Empia em28xx based USB video device driver"
51#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1) 50#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
52 51
53#define em28xx_videodbg(fmt, arg...) do {\ 52#define em28xx_videodbg(fmt, arg...) do {\
54 if (video_debug) \ 53 if (video_debug) \
@@ -63,17 +62,17 @@ static LIST_HEAD(em28xx_devlist);
63 62
64static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 63static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 64static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
66static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 65static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
66static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
67
67module_param_array(card, int, NULL, 0444); 68module_param_array(card, int, NULL, 0444);
68module_param_array(video_nr, int, NULL, 0444); 69module_param_array(video_nr, int, NULL, 0444);
69module_param_array(vbi_nr, int, NULL, 0444); 70module_param_array(vbi_nr, int, NULL, 0444);
70MODULE_PARM_DESC(card,"card type"); 71module_param_array(radio_nr, int, NULL, 0444);
71MODULE_PARM_DESC(video_nr,"video device numbers"); 72MODULE_PARM_DESC(card, "card type");
72MODULE_PARM_DESC(vbi_nr,"vbi device numbers"); 73MODULE_PARM_DESC(video_nr, "video device numbers");
73 74MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
74static int tuner = -1; 75MODULE_PARM_DESC(radio_nr, "radio device numbers");
75module_param(tuner, int, 0444);
76MODULE_PARM_DESC(tuner, "tuner type");
77 76
78static unsigned int video_debug = 0; 77static unsigned int video_debug = 0;
79module_param(video_debug,int,0644); 78module_param(video_debug,int,0644);
@@ -82,29 +81,6 @@ MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
82/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */ 81/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
83static unsigned long em28xx_devused; 82static unsigned long em28xx_devused;
84 83
85/* supported tv norms */
86static struct em28xx_tvnorm tvnorms[] = {
87 {
88 .name = "PAL",
89 .id = V4L2_STD_PAL,
90 .mode = VIDEO_MODE_PAL,
91 }, {
92 .name = "NTSC",
93 .id = V4L2_STD_NTSC,
94 .mode = VIDEO_MODE_NTSC,
95 }, {
96 .name = "SECAM",
97 .id = V4L2_STD_SECAM,
98 .mode = VIDEO_MODE_SECAM,
99 }, {
100 .name = "PAL-M",
101 .id = V4L2_STD_PAL_M,
102 .mode = VIDEO_MODE_PAL,
103 }
104};
105
106#define TVNORMS ARRAY_SIZE(tvnorms)
107
108/* supported controls */ 84/* supported controls */
109/* Common to all boards */ 85/* Common to all boards */
110static struct v4l2_queryctrl em28xx_qctrl[] = { 86static struct v4l2_queryctrl em28xx_qctrl[] = {
@@ -131,8 +107,6 @@ static struct v4l2_queryctrl em28xx_qctrl[] = {
131 107
132static struct usb_driver em28xx_usb_driver; 108static struct usb_driver em28xx_usb_driver;
133 109
134static DEFINE_MUTEX(em28xx_sysfs_lock);
135static DECLARE_RWSEM(em28xx_disconnect);
136 110
137/********************* v4l2 interface ******************************************/ 111/********************* v4l2 interface ******************************************/
138 112
@@ -153,11 +127,9 @@ static int em28xx_config(struct em28xx *dev)
153/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */ 127/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
154 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1); 128 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
155 129
156 em28xx_audio_usb_mute(dev, 1);
157 dev->mute = 1; /* maybe not the right place... */ 130 dev->mute = 1; /* maybe not the right place... */
158 dev->volume = 0x1f; 131 dev->volume = 0x1f;
159 em28xx_audio_analog_set(dev); 132
160 em28xx_audio_analog_setup(dev);
161 em28xx_outfmt_set_yuv422(dev); 133 em28xx_outfmt_set_yuv422(dev);
162 em28xx_colorlevels_set_default(dev); 134 em28xx_colorlevels_set_default(dev);
163 em28xx_compression_disable(dev); 135 em28xx_compression_disable(dev);
@@ -171,7 +143,6 @@ static int em28xx_config(struct em28xx *dev)
171 */ 143 */
172static void em28xx_config_i2c(struct em28xx *dev) 144static void em28xx_config_i2c(struct em28xx *dev)
173{ 145{
174 struct v4l2_frequency f;
175 struct v4l2_routing route; 146 struct v4l2_routing route;
176 147
177 route.input = INPUT(dev->ctl_input)->vmux; 148 route.input = INPUT(dev->ctl_input)->vmux;
@@ -179,18 +150,6 @@ static void em28xx_config_i2c(struct em28xx *dev)
179 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL); 150 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
180 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route); 151 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
181 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL); 152 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
182
183 /* configure tuner */
184 f.tuner = 0;
185 f.type = V4L2_TUNER_ANALOG_TV;
186 f.frequency = 9076; /* FIXME:remove magic number */
187 dev->ctl_freq = f.frequency;
188 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
189
190 /* configure tda9887 */
191
192
193/* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
194} 153}
195 154
196/* 155/*
@@ -212,7 +171,6 @@ static void em28xx_empty_framequeues(struct em28xx *dev)
212 171
213static void video_mux(struct em28xx *dev, int index) 172static void video_mux(struct em28xx *dev, int index)
214{ 173{
215 int ainput;
216 struct v4l2_routing route; 174 struct v4l2_routing route;
217 175
218 route.input = INPUT(index)->vmux; 176 route.input = INPUT(index)->vmux;
@@ -222,8 +180,6 @@ static void video_mux(struct em28xx *dev, int index)
222 180
223 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route); 181 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
224 182
225 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,route.input,dev->ctl_ainput);
226
227 if (dev->has_msp34xx) { 183 if (dev->has_msp34xx) {
228 if (dev->i2s_speed) 184 if (dev->i2s_speed)
229 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed); 185 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
@@ -231,18 +187,1068 @@ static void video_mux(struct em28xx *dev, int index)
231 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1); 187 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
232 /* Note: this is msp3400 specific */ 188 /* Note: this is msp3400 specific */
233 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route); 189 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
234 ainput = EM28XX_AUDIO_SRC_TUNER; 190 }
235 em28xx_audio_source(dev, ainput); 191
192 em28xx_set_audio_source(dev);
193}
194
195/* Usage lock check functions */
196static int res_get(struct em28xx_fh *fh)
197{
198 struct em28xx *dev = fh->dev;
199 int rc = 0;
200
201 /* This instance already has stream_on */
202 if (fh->stream_on)
203 return rc;
204
205 mutex_lock(&dev->lock);
206
207 if (dev->stream_on)
208 rc = -EINVAL;
209 else {
210 dev->stream_on = 1;
211 fh->stream_on = 1;
212 }
213
214 mutex_unlock(&dev->lock);
215 return rc;
216}
217
218static int res_check(struct em28xx_fh *fh)
219{
220 return (fh->stream_on);
221}
222
223static void res_free(struct em28xx_fh *fh)
224{
225 struct em28xx *dev = fh->dev;
226
227 mutex_lock(&dev->lock);
228 fh->stream_on = 0;
229 dev->stream_on = 0;
230 mutex_unlock(&dev->lock);
231}
232
233/*
234 * em28xx_vm_open()
235 */
236static void em28xx_vm_open(struct vm_area_struct *vma)
237{
238 struct em28xx_frame_t *f = vma->vm_private_data;
239 f->vma_use_count++;
240}
241
242/*
243 * em28xx_vm_close()
244 */
245static void em28xx_vm_close(struct vm_area_struct *vma)
246{
247 /* NOTE: buffers are not freed here */
248 struct em28xx_frame_t *f = vma->vm_private_data;
249
250 if (f->vma_use_count)
251 f->vma_use_count--;
252}
253
254static struct vm_operations_struct em28xx_vm_ops = {
255 .open = em28xx_vm_open,
256 .close = em28xx_vm_close,
257};
258
259
260/*
261 * em28xx_get_ctrl()
262 * return the current saturation, brightness or contrast, mute state
263 */
264static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
265{
266 switch (ctrl->id) {
267 case V4L2_CID_AUDIO_MUTE:
268 ctrl->value = dev->mute;
269 return 0;
270 case V4L2_CID_AUDIO_VOLUME:
271 ctrl->value = dev->volume;
272 return 0;
273 default:
274 return -EINVAL;
275 }
276}
277
278/*
279 * em28xx_set_ctrl()
280 * mute or set new saturation, brightness or contrast
281 */
282static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
283{
284 switch (ctrl->id) {
285 case V4L2_CID_AUDIO_MUTE:
286 if (ctrl->value != dev->mute) {
287 dev->mute = ctrl->value;
288 return em28xx_audio_analog_set(dev);
289 }
290 return 0;
291 case V4L2_CID_AUDIO_VOLUME:
292 dev->volume = ctrl->value;
293 return em28xx_audio_analog_set(dev);
294 default:
295 return -EINVAL;
296 }
297}
298
299/*
300 * em28xx_stream_interrupt()
301 * stops streaming
302 */
303static int em28xx_stream_interrupt(struct em28xx *dev)
304{
305 int rc = 0;
306
307 /* stop reading from the device */
308
309 dev->stream = STREAM_INTERRUPT;
310 rc = wait_event_timeout(dev->wait_stream,
311 (dev->stream == STREAM_OFF) ||
312 (dev->state & DEV_DISCONNECTED),
313 EM28XX_URB_TIMEOUT);
314
315 if (rc) {
316 dev->state |= DEV_MISCONFIGURED;
317 em28xx_videodbg("device is misconfigured; close and "
318 "open /dev/video%d again\n",
319 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
320 return rc;
321 }
322
323 return 0;
324}
325
326
327static int check_dev(struct em28xx *dev)
328{
329 if (dev->state & DEV_DISCONNECTED) {
330 em28xx_errdev("v4l2 ioctl: device not present\n");
331 return -ENODEV;
332 }
333
334 if (dev->state & DEV_MISCONFIGURED) {
335 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
336 "close and open it again\n");
337 return -EIO;
338 }
339 return 0;
340}
341
342static void get_scale(struct em28xx *dev,
343 unsigned int width, unsigned int height,
344 unsigned int *hscale, unsigned int *vscale)
345{
346 unsigned int maxw = norm_maxw(dev);
347 unsigned int maxh = norm_maxh(dev);
348
349 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
350 if (*hscale >= 0x4000)
351 *hscale = 0x3fff;
352
353 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
354 if (*vscale >= 0x4000)
355 *vscale = 0x3fff;
356}
357
358/* ------------------------------------------------------------------
359 IOCTL vidioc handling
360 ------------------------------------------------------------------*/
361
362static int vidioc_g_fmt_cap(struct file *file, void *priv,
363 struct v4l2_format *f)
364{
365 struct em28xx_fh *fh = priv;
366 struct em28xx *dev = fh->dev;
367
368 mutex_lock(&dev->lock);
369
370 f->fmt.pix.width = dev->width;
371 f->fmt.pix.height = dev->height;
372 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
373 f->fmt.pix.bytesperline = dev->bytesperline;
374 f->fmt.pix.sizeimage = dev->frame_size;
375 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
376
377 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
378 f->fmt.pix.field = dev->interlaced ?
379 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
380
381 mutex_unlock(&dev->lock);
382 return 0;
383}
384
385static int vidioc_try_fmt_cap(struct file *file, void *priv,
386 struct v4l2_format *f)
387{
388 struct em28xx_fh *fh = priv;
389 struct em28xx *dev = fh->dev;
390 int width = f->fmt.pix.width;
391 int height = f->fmt.pix.height;
392 unsigned int maxw = norm_maxw(dev);
393 unsigned int maxh = norm_maxh(dev);
394 unsigned int hscale, vscale;
395
396 /* width must even because of the YUYV format
397 height must be even because of interlacing */
398 height &= 0xfffe;
399 width &= 0xfffe;
400
401 if (height < 32)
402 height = 32;
403 if (height > maxh)
404 height = maxh;
405 if (width < 48)
406 width = 48;
407 if (width > maxw)
408 width = maxw;
409
410 mutex_lock(&dev->lock);
411
412 if (dev->is_em2800) {
413 /* the em2800 can only scale down to 50% */
414 if (height % (maxh / 2))
415 height = maxh;
416 if (width % (maxw / 2))
417 width = maxw;
418 /* according to empiatech support */
419 /* the MaxPacketSize is to small to support */
420 /* framesizes larger than 640x480 @ 30 fps */
421 /* or 640x576 @ 25 fps. As this would cut */
422 /* of a part of the image we prefer */
423 /* 360x576 or 360x480 for now */
424 if (width == maxw && height == maxh)
425 width /= 2;
426 }
427
428 get_scale(dev, width, height, &hscale, &vscale);
429
430 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
431 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
432
433 f->fmt.pix.width = width;
434 f->fmt.pix.height = height;
435 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
436 f->fmt.pix.bytesperline = width * 2;
437 f->fmt.pix.sizeimage = width * 2 * height;
438 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
439 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
440
441 mutex_unlock(&dev->lock);
442 return 0;
443}
444
445static int vidioc_s_fmt_cap(struct file *file, void *priv,
446 struct v4l2_format *f)
447{
448 struct em28xx_fh *fh = priv;
449 struct em28xx *dev = fh->dev;
450 int rc, i;
451
452 rc = check_dev(dev);
453 if (rc < 0)
454 return rc;
455
456 vidioc_try_fmt_cap(file, priv, f);
457
458 mutex_lock(&dev->lock);
459
460 for (i = 0; i < dev->num_frames; i++)
461 if (dev->frame[i].vma_use_count) {
462 em28xx_videodbg("VIDIOC_S_FMT failed. "
463 "Unmap the buffers first.\n");
464 rc = -EINVAL;
465 goto err;
466 }
467
468 /* stop io in case it is already in progress */
469 if (dev->stream == STREAM_ON) {
470 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
471 rc = em28xx_stream_interrupt(dev);
472 if (rc < 0)
473 goto err;
474 }
475
476 em28xx_release_buffers(dev);
477 dev->io = IO_NONE;
478
479 /* set new image size */
480 dev->width = f->fmt.pix.width;
481 dev->height = f->fmt.pix.height;
482 dev->frame_size = dev->width * dev->height * 2;
483 dev->field_size = dev->frame_size >> 1;
484 dev->bytesperline = dev->width * 2;
485 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
486
487 /* FIXME: This is really weird! Why capture is starting with
488 this ioctl ???
489 */
490 em28xx_uninit_isoc(dev);
491 em28xx_set_alternate(dev);
492 em28xx_capture_start(dev, 1);
493 em28xx_resolution_set(dev);
494 em28xx_init_isoc(dev);
495 rc = 0;
496
497err:
498 mutex_unlock(&dev->lock);
499 return rc;
500}
501
502static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
503{
504 struct em28xx_fh *fh = priv;
505 struct em28xx *dev = fh->dev;
506 struct v4l2_format f;
507 int rc;
508
509 rc = check_dev(dev);
510 if (rc < 0)
511 return rc;
512
513 mutex_lock(&dev->lock);
514 dev->norm = *norm;
515 mutex_unlock(&dev->lock);
516
517 /* Adjusts width/height, if needed */
518 f.fmt.pix.width = dev->width;
519 f.fmt.pix.height = dev->height;
520 vidioc_try_fmt_cap(file, priv, &f);
521
522 mutex_lock(&dev->lock);
523
524 /* set new image size */
525 dev->width = f.fmt.pix.width;
526 dev->height = f.fmt.pix.height;
527 dev->frame_size = dev->width * dev->height * 2;
528 dev->field_size = dev->frame_size >> 1;
529 dev->bytesperline = dev->width * 2;
530 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
531
532 em28xx_resolution_set(dev);
533 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
534
535 mutex_unlock(&dev->lock);
536 return 0;
537}
538
539static const char *iname[] = {
540 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
541 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
542 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
543 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
544 [EM28XX_VMUX_SVIDEO] = "S-Video",
545 [EM28XX_VMUX_TELEVISION] = "Television",
546 [EM28XX_VMUX_CABLE] = "Cable TV",
547 [EM28XX_VMUX_DVB] = "DVB",
548 [EM28XX_VMUX_DEBUG] = "for debug only",
549};
550
551static int vidioc_enum_input(struct file *file, void *priv,
552 struct v4l2_input *i)
553{
554 struct em28xx_fh *fh = priv;
555 struct em28xx *dev = fh->dev;
556 unsigned int n;
557
558 n = i->index;
559 if (n >= MAX_EM28XX_INPUT)
560 return -EINVAL;
561 if (0 == INPUT(n)->type)
562 return -EINVAL;
563
564 i->index = n;
565 i->type = V4L2_INPUT_TYPE_CAMERA;
566
567 strcpy(i->name, iname[INPUT(n)->type]);
568
569 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
570 (EM28XX_VMUX_CABLE == INPUT(n)->type))
571 i->type = V4L2_INPUT_TYPE_TUNER;
572
573 i->std = dev->vdev->tvnorms;
574
575 return 0;
576}
577
578static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
579{
580 struct em28xx_fh *fh = priv;
581 struct em28xx *dev = fh->dev;
582
583 *i = dev->ctl_input;
584
585 return 0;
586}
587
588static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
589{
590 struct em28xx_fh *fh = priv;
591 struct em28xx *dev = fh->dev;
592 int rc;
593
594 rc = check_dev(dev);
595 if (rc < 0)
596 return rc;
597
598 if (i >= MAX_EM28XX_INPUT)
599 return -EINVAL;
600 if (0 == INPUT(i)->type)
601 return -EINVAL;
602
603 mutex_lock(&dev->lock);
604
605 video_mux(dev, i);
606
607 mutex_unlock(&dev->lock);
608 return 0;
609}
610
611static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
612{
613 struct em28xx_fh *fh = priv;
614 struct em28xx *dev = fh->dev;
615 unsigned int index = a->index;
616
617 if (a->index > 1)
618 return -EINVAL;
619
620 index = dev->ctl_ainput;
621
622 if (index == 0) {
623 strcpy(a->name, "Television");
236 } else { 624 } else {
237 switch (dev->ctl_ainput) { 625 strcpy(a->name, "Line In");
238 case 0: 626 }
239 ainput = EM28XX_AUDIO_SRC_TUNER; 627 a->capability = V4L2_AUDCAP_STEREO;
628 a->index = index;
629
630 return 0;
631}
632
633static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
634{
635 struct em28xx_fh *fh = priv;
636 struct em28xx *dev = fh->dev;
637
638 if (a->index != dev->ctl_ainput)
639 return -EINVAL;
640
641 return 0;
642}
643
644static int vidioc_queryctrl(struct file *file, void *priv,
645 struct v4l2_queryctrl *qc)
646{
647 struct em28xx_fh *fh = priv;
648 struct em28xx *dev = fh->dev;
649 int id = qc->id;
650 int i;
651 int rc;
652
653 rc = check_dev(dev);
654 if (rc < 0)
655 return rc;
656
657 memset(qc, 0, sizeof(*qc));
658
659 qc->id = id;
660
661 if (!dev->has_msp34xx) {
662 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
663 if (qc->id && qc->id == em28xx_qctrl[i].id) {
664 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
665 return 0;
666 }
667 }
668 }
669 mutex_lock(&dev->lock);
670 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
671 mutex_unlock(&dev->lock);
672
673 if (qc->type)
674 return 0;
675 else
676 return -EINVAL;
677}
678
679static int vidioc_g_ctrl(struct file *file, void *priv,
680 struct v4l2_control *ctrl)
681{
682 struct em28xx_fh *fh = priv;
683 struct em28xx *dev = fh->dev;
684 int rc;
685
686 rc = check_dev(dev);
687 if (rc < 0)
688 return rc;
689 mutex_lock(&dev->lock);
690
691 if (!dev->has_msp34xx)
692 rc = em28xx_get_ctrl(dev, ctrl);
693 else
694 rc = -EINVAL;
695
696 if (rc == -EINVAL) {
697 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
698 rc = 0;
699 }
700
701 mutex_unlock(&dev->lock);
702 return rc;
703}
704
705static int vidioc_s_ctrl(struct file *file, void *priv,
706 struct v4l2_control *ctrl)
707{
708 struct em28xx_fh *fh = priv;
709 struct em28xx *dev = fh->dev;
710 u8 i;
711 int rc;
712
713 rc = check_dev(dev);
714 if (rc < 0)
715 return rc;
716
717 mutex_lock(&dev->lock);
718
719 if (dev->has_msp34xx)
720 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
721 else {
722 rc = 1;
723 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
724 if (ctrl->id == em28xx_qctrl[i].id) {
725 if (ctrl->value < em28xx_qctrl[i].minimum ||
726 ctrl->value > em28xx_qctrl[i].maximum) {
727 rc = -ERANGE;
728 break;
729 }
730
731 rc = em28xx_set_ctrl(dev, ctrl);
240 break; 732 break;
241 default: 733 }
242 ainput = EM28XX_AUDIO_SRC_LINE; 734 }
735 }
736
737 /* Control not found - try to send it to the attached devices */
738 if (rc == 1) {
739 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
740 rc = 0;
741 }
742
743 mutex_unlock(&dev->lock);
744 return rc;
745}
746
747static int vidioc_g_tuner(struct file *file, void *priv,
748 struct v4l2_tuner *t)
749{
750 struct em28xx_fh *fh = priv;
751 struct em28xx *dev = fh->dev;
752 int rc;
753
754 rc = check_dev(dev);
755 if (rc < 0)
756 return rc;
757
758 if (0 != t->index)
759 return -EINVAL;
760
761 strcpy(t->name, "Tuner");
762
763 mutex_lock(&dev->lock);
764
765 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
766
767 mutex_unlock(&dev->lock);
768 return 0;
769}
770
771static int vidioc_s_tuner(struct file *file, void *priv,
772 struct v4l2_tuner *t)
773{
774 struct em28xx_fh *fh = priv;
775 struct em28xx *dev = fh->dev;
776 int rc;
777
778 rc = check_dev(dev);
779 if (rc < 0)
780 return rc;
781
782 if (0 != t->index)
783 return -EINVAL;
784
785 mutex_lock(&dev->lock);
786
787 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
788
789 mutex_unlock(&dev->lock);
790 return 0;
791}
792
793static int vidioc_g_frequency(struct file *file, void *priv,
794 struct v4l2_frequency *f)
795{
796 struct em28xx_fh *fh = priv;
797 struct em28xx *dev = fh->dev;
798
799 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
800 f->frequency = dev->ctl_freq;
801
802 return 0;
803}
804
805static int vidioc_s_frequency(struct file *file, void *priv,
806 struct v4l2_frequency *f)
807{
808 struct em28xx_fh *fh = priv;
809 struct em28xx *dev = fh->dev;
810 int rc;
811
812 rc = check_dev(dev);
813 if (rc < 0)
814 return rc;
815
816 if (0 != f->tuner)
817 return -EINVAL;
818
819 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
820 return -EINVAL;
821 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
822 return -EINVAL;
823
824 mutex_lock(&dev->lock);
825
826 dev->ctl_freq = f->frequency;
827 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
828
829 mutex_unlock(&dev->lock);
830 return 0;
831}
832
833static int vidioc_cropcap(struct file *file, void *priv,
834 struct v4l2_cropcap *cc)
835{
836 struct em28xx_fh *fh = priv;
837 struct em28xx *dev = fh->dev;
838
839 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
840 return -EINVAL;
841
842 cc->bounds.left = 0;
843 cc->bounds.top = 0;
844 cc->bounds.width = dev->width;
845 cc->bounds.height = dev->height;
846 cc->defrect = cc->bounds;
847 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
848 cc->pixelaspect.denominator = 59;
849
850 return 0;
851}
852
853static int vidioc_streamon(struct file *file, void *priv,
854 enum v4l2_buf_type type)
855{
856 struct em28xx_fh *fh = priv;
857 struct em28xx *dev = fh->dev;
858 int rc;
859
860 rc = check_dev(dev);
861 if (rc < 0)
862 return rc;
863
864 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
865 return -EINVAL;
866
867 if (list_empty(&dev->inqueue))
868 return -EINVAL;
869
870 mutex_lock(&dev->lock);
871
872 if (unlikely(res_get(fh) < 0)) {
873 mutex_unlock(&dev->lock);
874 return -EBUSY;
875 }
876
877 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
878
879 mutex_unlock(&dev->lock);
880 return 0;
881}
882
883static int vidioc_streamoff(struct file *file, void *priv,
884 enum v4l2_buf_type type)
885{
886 struct em28xx_fh *fh = priv;
887 struct em28xx *dev = fh->dev;
888 int rc;
889
890 rc = check_dev(dev);
891 if (rc < 0)
892 return rc;
893
894 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
895 return -EINVAL;
896
897 mutex_lock(&dev->lock);
898
899 if (dev->stream == STREAM_ON) {
900 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
901 rc = em28xx_stream_interrupt(dev);
902 if (rc < 0) {
903 mutex_unlock(&dev->lock);
904 return rc;
905 }
906 }
907
908 em28xx_empty_framequeues(dev);
909
910 mutex_unlock(&dev->lock);
911 return 0;
912}
913
914static int vidioc_querycap(struct file *file, void *priv,
915 struct v4l2_capability *cap)
916{
917 struct em28xx_fh *fh = priv;
918 struct em28xx *dev = fh->dev;
919
920 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
921 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
922 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
923
924 cap->version = EM28XX_VERSION_CODE;
925
926 cap->capabilities =
927 V4L2_CAP_SLICED_VBI_CAPTURE |
928 V4L2_CAP_VIDEO_CAPTURE |
929 V4L2_CAP_AUDIO |
930 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
931
932 if (dev->tuner_type != TUNER_ABSENT)
933 cap->capabilities |= V4L2_CAP_TUNER;
934
935 return 0;
936}
937
938static int vidioc_enum_fmt_cap(struct file *file, void *priv,
939 struct v4l2_fmtdesc *fmtd)
940{
941 if (fmtd->index != 0)
942 return -EINVAL;
943
944 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
945 strcpy(fmtd->description, "Packed YUY2");
946 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
947 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
948
949 return 0;
950}
951
952/* Sliced VBI ioctls */
953static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
954 struct v4l2_format *f)
955{
956 struct em28xx_fh *fh = priv;
957 struct em28xx *dev = fh->dev;
958 int rc;
959
960 rc = check_dev(dev);
961 if (rc < 0)
962 return rc;
963
964 mutex_lock(&dev->lock);
965
966 f->fmt.sliced.service_set = 0;
967
968 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
969
970 if (f->fmt.sliced.service_set == 0)
971 rc = -EINVAL;
972
973 mutex_unlock(&dev->lock);
974 return rc;
975}
976
977static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
978 struct v4l2_format *f)
979{
980 struct em28xx_fh *fh = priv;
981 struct em28xx *dev = fh->dev;
982 int rc;
983
984 rc = check_dev(dev);
985 if (rc < 0)
986 return rc;
987
988 mutex_lock(&dev->lock);
989 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
990 mutex_unlock(&dev->lock);
991
992 if (f->fmt.sliced.service_set == 0)
993 return -EINVAL;
994
995 return 0;
996}
997
998
999static int vidioc_reqbufs(struct file *file, void *priv,
1000 struct v4l2_requestbuffers *rb)
1001{
1002 struct em28xx_fh *fh = priv;
1003 struct em28xx *dev = fh->dev;
1004 u32 i;
1005 int rc;
1006
1007 rc = check_dev(dev);
1008 if (rc < 0)
1009 return rc;
1010
1011 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1012 rb->memory != V4L2_MEMORY_MMAP)
1013 return -EINVAL;
1014
1015 if (dev->io == IO_READ) {
1016 em28xx_videodbg("method is set to read;"
1017 " close and open the device again to"
1018 " choose the mmap I/O method\n");
1019 return -EINVAL;
1020 }
1021
1022 for (i = 0; i < dev->num_frames; i++)
1023 if (dev->frame[i].vma_use_count) {
1024 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1025 "previous buffers are still mapped\n");
1026 return -EINVAL;
1027 }
1028
1029 mutex_lock(&dev->lock);
1030
1031 if (dev->stream == STREAM_ON) {
1032 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1033 rc = em28xx_stream_interrupt(dev);
1034 if (rc < 0) {
1035 mutex_unlock(&dev->lock);
1036 return rc;
243 } 1037 }
244 em28xx_audio_source(dev, ainput);
245 } 1038 }
1039
1040 em28xx_empty_framequeues(dev);
1041
1042 em28xx_release_buffers(dev);
1043 if (rb->count)
1044 rb->count = em28xx_request_buffers(dev, rb->count);
1045
1046 dev->frame_current = NULL;
1047 dev->io = rb->count ? IO_MMAP : IO_NONE;
1048
1049 mutex_unlock(&dev->lock);
1050 return 0;
1051}
1052
1053static int vidioc_querybuf(struct file *file, void *priv,
1054 struct v4l2_buffer *b)
1055{
1056 struct em28xx_fh *fh = priv;
1057 struct em28xx *dev = fh->dev;
1058 int rc;
1059
1060 rc = check_dev(dev);
1061 if (rc < 0)
1062 return rc;
1063
1064 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1065 b->index >= dev->num_frames || dev->io != IO_MMAP)
1066 return -EINVAL;
1067
1068 mutex_lock(&dev->lock);
1069
1070 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1071
1072 if (dev->frame[b->index].vma_use_count)
1073 b->flags |= V4L2_BUF_FLAG_MAPPED;
1074
1075 if (dev->frame[b->index].state == F_DONE)
1076 b->flags |= V4L2_BUF_FLAG_DONE;
1077 else if (dev->frame[b->index].state != F_UNUSED)
1078 b->flags |= V4L2_BUF_FLAG_QUEUED;
1079
1080 mutex_unlock(&dev->lock);
1081 return 0;
1082}
1083
1084static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1085{
1086 struct em28xx_fh *fh = priv;
1087 struct em28xx *dev = fh->dev;
1088 unsigned long lock_flags;
1089 int rc;
1090
1091 rc = check_dev(dev);
1092 if (rc < 0)
1093 return rc;
1094
1095 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP ||
1096 b->index >= dev->num_frames)
1097 return -EINVAL;
1098
1099 if (dev->frame[b->index].state != F_UNUSED)
1100 return -EAGAIN;
1101
1102 dev->frame[b->index].state = F_QUEUED;
1103
1104 /* add frame to fifo */
1105 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1106 list_add_tail(&dev->frame[b->index].frame, &dev->inqueue);
1107 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1108
1109 return 0;
1110}
1111
1112static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1113{
1114 struct em28xx_fh *fh = priv;
1115 struct em28xx *dev = fh->dev;
1116 int rc;
1117 struct em28xx_frame_t *f;
1118 unsigned long lock_flags;
1119
1120 rc = check_dev(dev);
1121 if (rc < 0)
1122 return rc;
1123
1124 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
1125 return -EINVAL;
1126
1127 if (list_empty(&dev->outqueue)) {
1128 if (dev->stream == STREAM_OFF)
1129 return -EINVAL;
1130
1131 if (file->f_flags & O_NONBLOCK)
1132 return -EAGAIN;
1133
1134 rc = wait_event_interruptible(dev->wait_frame,
1135 (!list_empty(&dev->outqueue)) ||
1136 (dev->state & DEV_DISCONNECTED));
1137 if (rc)
1138 return rc;
1139
1140 if (dev->state & DEV_DISCONNECTED)
1141 return -ENODEV;
1142 }
1143
1144 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1145 f = list_entry(dev->outqueue.next, struct em28xx_frame_t, frame);
1146 list_del(dev->outqueue.next);
1147 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1148
1149 f->state = F_UNUSED;
1150 memcpy(b, &f->buf, sizeof(*b));
1151
1152 if (f->vma_use_count)
1153 b->flags |= V4L2_BUF_FLAG_MAPPED;
1154
1155 return 0;
1156}
1157
1158/* ----------------------------------------------------------- */
1159/* RADIO ESPECIFIC IOCTLS */
1160/* ----------------------------------------------------------- */
1161
1162static int radio_querycap(struct file *file, void *priv,
1163 struct v4l2_capability *cap)
1164{
1165 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1166
1167 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1168 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1169 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1170
1171 cap->version = EM28XX_VERSION_CODE;
1172 cap->capabilities = V4L2_CAP_TUNER;
1173 return 0;
1174}
1175
1176static int radio_g_tuner(struct file *file, void *priv,
1177 struct v4l2_tuner *t)
1178{
1179 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1180
1181 if (unlikely(t->index > 0))
1182 return -EINVAL;
1183
1184 strcpy(t->name, "Radio");
1185 t->type = V4L2_TUNER_RADIO;
1186
1187 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1188 return 0;
1189}
1190
1191static int radio_enum_input(struct file *file, void *priv,
1192 struct v4l2_input *i)
1193{
1194 if (i->index != 0)
1195 return -EINVAL;
1196 strcpy(i->name, "Radio");
1197 i->type = V4L2_INPUT_TYPE_TUNER;
1198
1199 return 0;
1200}
1201
1202static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1203{
1204 if (unlikely(a->index))
1205 return -EINVAL;
1206
1207 strcpy(a->name, "Radio");
1208 return 0;
1209}
1210
1211static int radio_s_tuner(struct file *file, void *priv,
1212 struct v4l2_tuner *t)
1213{
1214 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1215
1216 if (0 != t->index)
1217 return -EINVAL;
1218
1219 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1220
1221 return 0;
1222}
1223
1224static int radio_s_audio(struct file *file, void *fh,
1225 struct v4l2_audio *a)
1226{
1227 return 0;
1228}
1229
1230static int radio_s_input(struct file *file, void *fh, unsigned int i)
1231{
1232 return 0;
1233}
1234
1235static int radio_queryctrl(struct file *file, void *priv,
1236 struct v4l2_queryctrl *qc)
1237{
1238 int i;
1239
1240 if (qc->id < V4L2_CID_BASE ||
1241 qc->id >= V4L2_CID_LASTP1)
1242 return -EINVAL;
1243
1244 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1245 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1246 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1247 return 0;
1248 }
1249 }
1250
1251 return -EINVAL;
246} 1252}
247 1253
248/* 1254/*
@@ -252,8 +1258,9 @@ static void video_mux(struct em28xx *dev, int index)
252static int em28xx_v4l2_open(struct inode *inode, struct file *filp) 1258static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
253{ 1259{
254 int minor = iminor(inode); 1260 int minor = iminor(inode);
255 int errCode = 0; 1261 int errCode = 0, radio = 0;
256 struct em28xx *h,*dev = NULL; 1262 struct em28xx *h,*dev = NULL;
1263 struct em28xx_fh *fh;
257 1264
258 list_for_each_entry(h, &em28xx_devlist, devlist) { 1265 list_for_each_entry(h, &em28xx_devlist, devlist) {
259 if (h->vdev->minor == minor) { 1266 if (h->vdev->minor == minor) {
@@ -264,6 +1271,11 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
264 dev = h; 1271 dev = h;
265 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE; 1272 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
266 } 1273 }
1274 if (h->radio_dev &&
1275 h->radio_dev->minor == minor) {
1276 radio = 1;
1277 dev = h;
1278 }
267 } 1279 }
268 if (NULL == dev) 1280 if (NULL == dev)
269 return -ENODEV; 1281 return -ENODEV;
@@ -271,23 +1283,18 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
271 em28xx_videodbg("open minor=%d type=%s users=%d\n", 1283 em28xx_videodbg("open minor=%d type=%s users=%d\n",
272 minor,v4l2_type_names[dev->type],dev->users); 1284 minor,v4l2_type_names[dev->type],dev->users);
273 1285
274 if (!down_read_trylock(&em28xx_disconnect)) 1286 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
275 return -ERESTARTSYS;
276 1287
277 if (dev->users) { 1288 if (!fh) {
278 em28xx_warn("this driver can be opened only once\n"); 1289 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
279 up_read(&em28xx_disconnect); 1290 return -ENOMEM;
280 return -EBUSY;
281 } 1291 }
282
283 mutex_init(&dev->fileop_lock); /* to 1 == available */
284 spin_lock_init(&dev->queue_lock);
285 init_waitqueue_head(&dev->wait_frame);
286 init_waitqueue_head(&dev->wait_stream);
287
288 mutex_lock(&dev->lock); 1292 mutex_lock(&dev->lock);
1293 fh->dev = dev;
1294 fh->radio = radio;
1295 filp->private_data = fh;
289 1296
290 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 1297 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
291 em28xx_set_alternate(dev); 1298 em28xx_set_alternate(dev);
292 1299
293 dev->width = norm_maxw(dev); 1300 dev->width = norm_maxw(dev);
@@ -301,30 +1308,23 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
301 em28xx_capture_start(dev, 1); 1308 em28xx_capture_start(dev, 1);
302 em28xx_resolution_set(dev); 1309 em28xx_resolution_set(dev);
303 1310
304 /* device needs to be initialized before isoc transfer */
305 video_mux(dev, 0);
306 1311
307 /* start the transfer */ 1312 /* start the transfer */
308 errCode = em28xx_init_isoc(dev); 1313 errCode = em28xx_init_isoc(dev);
309 if (errCode) 1314 if (errCode)
310 goto err; 1315 goto err;
311 1316
1317 em28xx_empty_framequeues(dev);
1318 }
1319 if (fh->radio) {
1320 em28xx_videodbg("video_open: setting radio device\n");
1321 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
312 } 1322 }
313 1323
314 dev->users++; 1324 dev->users++;
315 filp->private_data = dev;
316 dev->io = IO_NONE;
317 dev->stream = STREAM_OFF;
318 dev->num_frames = 0;
319
320 /* prepare queues */
321 em28xx_empty_framequeues(dev);
322
323 dev->state |= DEV_INITIALIZED;
324 1325
325err: 1326err:
326 mutex_unlock(&dev->lock); 1327 mutex_unlock(&dev->lock);
327 up_read(&em28xx_disconnect);
328 return errCode; 1328 return errCode;
329} 1329}
330 1330
@@ -335,7 +1335,6 @@ err:
335*/ 1335*/
336static void em28xx_release_resources(struct em28xx *dev) 1336static void em28xx_release_resources(struct em28xx *dev)
337{ 1337{
338 mutex_lock(&em28xx_sysfs_lock);
339 1338
340 /*FIXME: I2C IR should be disconnected */ 1339 /*FIXME: I2C IR should be disconnected */
341 1340
@@ -343,12 +1342,29 @@ static void em28xx_release_resources(struct em28xx *dev)
343 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN, 1342 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
344 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN); 1343 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
345 list_del(&dev->devlist); 1344 list_del(&dev->devlist);
346 video_unregister_device(dev->vdev); 1345 if (dev->radio_dev) {
347 video_unregister_device(dev->vbi_dev); 1346 if (-1 != dev->radio_dev->minor)
1347 video_unregister_device(dev->radio_dev);
1348 else
1349 video_device_release(dev->radio_dev);
1350 dev->radio_dev = NULL;
1351 }
1352 if (dev->vbi_dev) {
1353 if (-1 != dev->vbi_dev->minor)
1354 video_unregister_device(dev->vbi_dev);
1355 else
1356 video_device_release(dev->vbi_dev);
1357 dev->vbi_dev = NULL;
1358 }
1359 if (dev->vdev) {
1360 if (-1 != dev->vdev->minor)
1361 video_unregister_device(dev->vdev);
1362 else
1363 video_device_release(dev->vdev);
1364 dev->vdev = NULL;
1365 }
348 em28xx_i2c_unregister(dev); 1366 em28xx_i2c_unregister(dev);
349 usb_put_dev(dev->udev); 1367 usb_put_dev(dev->udev);
350 mutex_unlock(&em28xx_sysfs_lock);
351
352 1368
353 /* Mark device as unused */ 1369 /* Mark device as unused */
354 em28xx_devused&=~(1<<dev->devno); 1370 em28xx_devused&=~(1<<dev->devno);
@@ -360,34 +1376,42 @@ static void em28xx_release_resources(struct em28xx *dev)
360 */ 1376 */
361static int em28xx_v4l2_close(struct inode *inode, struct file *filp) 1377static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
362{ 1378{
363 int errCode; 1379 struct em28xx_fh *fh = filp->private_data;
364 struct em28xx *dev=filp->private_data; 1380 struct em28xx *dev = fh->dev;
1381 int errCode;
365 1382
366 em28xx_videodbg("users=%d\n", dev->users); 1383 em28xx_videodbg("users=%d\n", dev->users);
367 1384
368 mutex_lock(&dev->lock);
369 1385
370 em28xx_uninit_isoc(dev); 1386 if (res_check(fh))
1387 res_free(fh);
371 1388
372 em28xx_release_buffers(dev); 1389 mutex_lock(&dev->lock);
373 1390
374 /* the device is already disconnect, free the remaining resources */ 1391 if (dev->users == 1) {
375 if (dev->state & DEV_DISCONNECTED) { 1392 em28xx_uninit_isoc(dev);
376 em28xx_release_resources(dev); 1393 em28xx_release_buffers(dev);
377 mutex_unlock(&dev->lock); 1394 dev->io = IO_NONE;
378 kfree(dev);
379 return 0;
380 }
381 1395
382 /* set alternate 0 */ 1396 /* the device is already disconnect,
383 dev->alt = 0; 1397 free the remaining resources */
384 em28xx_videodbg("setting alternate 0\n"); 1398 if (dev->state & DEV_DISCONNECTED) {
385 errCode = usb_set_interface(dev->udev, 0, 0); 1399 em28xx_release_resources(dev);
386 if (errCode < 0) { 1400 mutex_unlock(&dev->lock);
387 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n", 1401 kfree(dev);
388 errCode); 1402 return 0;
389 } 1403 }
390 1404
1405 /* set alternate 0 */
1406 dev->alt = 0;
1407 em28xx_videodbg("setting alternate 0\n");
1408 errCode = usb_set_interface(dev->udev, 0, 0);
1409 if (errCode < 0) {
1410 em28xx_errdev("cannot change alternate number to "
1411 "0 (error=%i)\n", errCode);
1412 }
1413 }
1414 kfree(fh);
391 dev->users--; 1415 dev->users--;
392 wake_up_interruptible_nr(&dev->open, 1); 1416 wake_up_interruptible_nr(&dev->open, 1);
393 mutex_unlock(&dev->lock); 1417 mutex_unlock(&dev->lock);
@@ -405,56 +1429,65 @@ em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
405 struct em28xx_frame_t *f, *i; 1429 struct em28xx_frame_t *f, *i;
406 unsigned long lock_flags; 1430 unsigned long lock_flags;
407 int ret = 0; 1431 int ret = 0;
408 struct em28xx *dev = filp->private_data; 1432 struct em28xx_fh *fh = filp->private_data;
1433 struct em28xx *dev = fh->dev;
409 1434
410 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 1435 /* FIXME: read() is not prepared to allow changing the video
1436 resolution while streaming. Seems a bug at em28xx_set_fmt
1437 */
1438
1439 if (unlikely(res_get(fh) < 0))
1440 return -EBUSY;
1441
1442 mutex_lock(&dev->lock);
1443
1444 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
411 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n"); 1445 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
412 } 1446
413 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) { 1447 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
414 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n"); 1448 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
415 em28xx_videodbg("not supported yet! ...\n"); 1449 em28xx_videodbg("not supported yet! ...\n");
416 if (copy_to_user(buf, "", 1)) { 1450 if (copy_to_user(buf, "", 1)) {
417 mutex_unlock(&dev->fileop_lock); 1451 mutex_unlock(&dev->lock);
418 return -EFAULT; 1452 return -EFAULT;
419 } 1453 }
1454 mutex_unlock(&dev->lock);
420 return (1); 1455 return (1);
421 } 1456 }
422 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) { 1457 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
423 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n"); 1458 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
424 em28xx_videodbg("not supported yet! ...\n"); 1459 em28xx_videodbg("not supported yet! ...\n");
425 if (copy_to_user(buf, "", 1)) { 1460 if (copy_to_user(buf, "", 1)) {
426 mutex_unlock(&dev->fileop_lock); 1461 mutex_unlock(&dev->lock);
427 return -EFAULT; 1462 return -EFAULT;
428 } 1463 }
1464 mutex_unlock(&dev->lock);
429 return (1); 1465 return (1);
430 } 1466 }
431 1467
432 if (mutex_lock_interruptible(&dev->fileop_lock))
433 return -ERESTARTSYS;
434
435 if (dev->state & DEV_DISCONNECTED) { 1468 if (dev->state & DEV_DISCONNECTED) {
436 em28xx_videodbg("device not present\n"); 1469 em28xx_videodbg("device not present\n");
437 mutex_unlock(&dev->fileop_lock); 1470 mutex_unlock(&dev->lock);
438 return -ENODEV; 1471 return -ENODEV;
439 } 1472 }
440 1473
441 if (dev->state & DEV_MISCONFIGURED) { 1474 if (dev->state & DEV_MISCONFIGURED) {
442 em28xx_videodbg("device misconfigured; close and open it again\n"); 1475 em28xx_videodbg("device misconfigured; close and open it again\n");
443 mutex_unlock(&dev->fileop_lock); 1476 mutex_unlock(&dev->lock);
444 return -EIO; 1477 return -EIO;
445 } 1478 }
446 1479
447 if (dev->io == IO_MMAP) { 1480 if (dev->io == IO_MMAP) {
448 em28xx_videodbg ("IO method is set to mmap; close and open" 1481 em28xx_videodbg ("IO method is set to mmap; close and open"
449 " the device again to choose the read method\n"); 1482 " the device again to choose the read method\n");
450 mutex_unlock(&dev->fileop_lock); 1483 mutex_unlock(&dev->lock);
451 return -EINVAL; 1484 return -EINVAL;
452 } 1485 }
453 1486
454 if (dev->io == IO_NONE) { 1487 if (dev->io == IO_NONE) {
455 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) { 1488 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
456 em28xx_errdev("read failed, not enough memory\n"); 1489 em28xx_errdev("read failed, not enough memory\n");
457 mutex_unlock(&dev->fileop_lock); 1490 mutex_unlock(&dev->lock);
458 return -ENOMEM; 1491 return -ENOMEM;
459 } 1492 }
460 dev->io = IO_READ; 1493 dev->io = IO_READ;
@@ -463,13 +1496,13 @@ em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
463 } 1496 }
464 1497
465 if (!count) { 1498 if (!count) {
466 mutex_unlock(&dev->fileop_lock); 1499 mutex_unlock(&dev->lock);
467 return 0; 1500 return 0;
468 } 1501 }
469 1502
470 if (list_empty(&dev->outqueue)) { 1503 if (list_empty(&dev->outqueue)) {
471 if (filp->f_flags & O_NONBLOCK) { 1504 if (filp->f_flags & O_NONBLOCK) {
472 mutex_unlock(&dev->fileop_lock); 1505 mutex_unlock(&dev->lock);
473 return -EAGAIN; 1506 return -EAGAIN;
474 } 1507 }
475 ret = wait_event_interruptible 1508 ret = wait_event_interruptible
@@ -477,35 +1510,46 @@ em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
477 (!list_empty(&dev->outqueue)) || 1510 (!list_empty(&dev->outqueue)) ||
478 (dev->state & DEV_DISCONNECTED)); 1511 (dev->state & DEV_DISCONNECTED));
479 if (ret) { 1512 if (ret) {
480 mutex_unlock(&dev->fileop_lock); 1513 mutex_unlock(&dev->lock);
481 return ret; 1514 return ret;
482 } 1515 }
483 if (dev->state & DEV_DISCONNECTED) { 1516 if (dev->state & DEV_DISCONNECTED) {
484 mutex_unlock(&dev->fileop_lock); 1517 mutex_unlock(&dev->lock);
485 return -ENODEV; 1518 return -ENODEV;
486 } 1519 }
1520 dev->video_bytesread = 0;
487 } 1521 }
488 1522
489 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame); 1523 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
490 1524
491 spin_lock_irqsave(&dev->queue_lock, lock_flags);
492 list_for_each_entry(i, &dev->outqueue, frame)
493 i->state = F_UNUSED;
494 INIT_LIST_HEAD(&dev->outqueue);
495 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
496
497 em28xx_queue_unusedframes(dev); 1525 em28xx_queue_unusedframes(dev);
498 1526
499 if (count > f->buf.length) 1527 if (count > f->buf.length)
500 count = f->buf.length; 1528 count = f->buf.length;
501 1529
502 if (copy_to_user(buf, f->bufmem, count)) { 1530 if ((dev->video_bytesread + count) > dev->frame_size)
503 mutex_unlock(&dev->fileop_lock); 1531 count = dev->frame_size - dev->video_bytesread;
1532
1533 if (copy_to_user(buf, f->bufmem+dev->video_bytesread, count)) {
1534 em28xx_err("Error while copying to user\n");
504 return -EFAULT; 1535 return -EFAULT;
505 } 1536 }
1537 dev->video_bytesread += count;
1538
1539 if (dev->video_bytesread == dev->frame_size) {
1540 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1541 list_for_each_entry(i, &dev->outqueue, frame)
1542 i->state = F_UNUSED;
1543 INIT_LIST_HEAD(&dev->outqueue);
1544 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1545
1546 em28xx_queue_unusedframes(dev);
1547 dev->video_bytesread = 0;
1548 }
1549
506 *f_pos += count; 1550 *f_pos += count;
507 1551
508 mutex_unlock(&dev->fileop_lock); 1552 mutex_unlock(&dev->lock);
509 1553
510 return count; 1554 return count;
511} 1555}
@@ -517,11 +1561,14 @@ em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
517static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait) 1561static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
518{ 1562{
519 unsigned int mask = 0; 1563 unsigned int mask = 0;
520 struct em28xx *dev = filp->private_data; 1564 struct em28xx_fh *fh = filp->private_data;
1565 struct em28xx *dev = fh->dev;
521 1566
522 if (mutex_lock_interruptible(&dev->fileop_lock)) 1567 if (unlikely(res_get(fh) < 0))
523 return POLLERR; 1568 return POLLERR;
524 1569
1570 mutex_lock(&dev->lock);
1571
525 if (dev->state & DEV_DISCONNECTED) { 1572 if (dev->state & DEV_DISCONNECTED) {
526 em28xx_videodbg("device not present\n"); 1573 em28xx_videodbg("device not present\n");
527 } else if (dev->state & DEV_MISCONFIGURED) { 1574 } else if (dev->state & DEV_MISCONFIGURED) {
@@ -545,83 +1592,61 @@ static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
545 if (!list_empty(&dev->outqueue)) 1592 if (!list_empty(&dev->outqueue))
546 mask |= POLLIN | POLLRDNORM; 1593 mask |= POLLIN | POLLRDNORM;
547 1594
548 mutex_unlock(&dev->fileop_lock); 1595 mutex_unlock(&dev->lock);
549 1596
550 return mask; 1597 return mask;
551 } 1598 }
552 } 1599 }
553 1600
554 mutex_unlock(&dev->fileop_lock); 1601 mutex_unlock(&dev->lock);
555 return POLLERR; 1602 return POLLERR;
556} 1603}
557 1604
558/* 1605/*
559 * em28xx_vm_open()
560 */
561static void em28xx_vm_open(struct vm_area_struct *vma)
562{
563 struct em28xx_frame_t *f = vma->vm_private_data;
564 f->vma_use_count++;
565}
566
567/*
568 * em28xx_vm_close()
569 */
570static void em28xx_vm_close(struct vm_area_struct *vma)
571{
572 /* NOTE: buffers are not freed here */
573 struct em28xx_frame_t *f = vma->vm_private_data;
574
575 if (f->vma_use_count)
576 f->vma_use_count--;
577}
578
579static struct vm_operations_struct em28xx_vm_ops = {
580 .open = em28xx_vm_open,
581 .close = em28xx_vm_close,
582};
583
584/*
585 * em28xx_v4l2_mmap() 1606 * em28xx_v4l2_mmap()
586 */ 1607 */
587static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) 1608static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
588{ 1609{
589 unsigned long size = vma->vm_end - vma->vm_start, 1610 struct em28xx_fh *fh = filp->private_data;
590 start = vma->vm_start; 1611 struct em28xx *dev = fh->dev;
591 void *pos; 1612 unsigned long size = vma->vm_end - vma->vm_start;
592 u32 i; 1613 unsigned long start = vma->vm_start;
593 1614 void *pos;
594 struct em28xx *dev = filp->private_data; 1615 u32 i;
1616
1617 if (unlikely(res_get(fh) < 0))
1618 return -EBUSY;
595 1619
596 if (mutex_lock_interruptible(&dev->fileop_lock)) 1620 mutex_lock(&dev->lock);
597 return -ERESTARTSYS;
598 1621
599 if (dev->state & DEV_DISCONNECTED) { 1622 if (dev->state & DEV_DISCONNECTED) {
600 em28xx_videodbg("mmap: device not present\n"); 1623 em28xx_videodbg("mmap: device not present\n");
601 mutex_unlock(&dev->fileop_lock); 1624 mutex_unlock(&dev->lock);
602 return -ENODEV; 1625 return -ENODEV;
603 } 1626 }
604 1627
605 if (dev->state & DEV_MISCONFIGURED) { 1628 if (dev->state & DEV_MISCONFIGURED) {
606 em28xx_videodbg ("mmap: Device is misconfigured; close and " 1629 em28xx_videodbg ("mmap: Device is misconfigured; close and "
607 "open it again\n"); 1630 "open it again\n");
608 mutex_unlock(&dev->fileop_lock); 1631 mutex_unlock(&dev->lock);
609 return -EIO; 1632 return -EIO;
610 } 1633 }
611 1634
612 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) || 1635 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE)) {
613 size != PAGE_ALIGN(dev->frame[0].buf.length)) { 1636 mutex_unlock(&dev->lock);
614 mutex_unlock(&dev->fileop_lock);
615 return -EINVAL; 1637 return -EINVAL;
616 } 1638 }
617 1639
1640 if (size > PAGE_ALIGN(dev->frame[0].buf.length))
1641 size = PAGE_ALIGN(dev->frame[0].buf.length);
1642
618 for (i = 0; i < dev->num_frames; i++) { 1643 for (i = 0; i < dev->num_frames; i++) {
619 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff) 1644 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
620 break; 1645 break;
621 } 1646 }
622 if (i == dev->num_frames) { 1647 if (i == dev->num_frames) {
623 em28xx_videodbg("mmap: user supplied mapping address is out of range\n"); 1648 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
624 mutex_unlock(&dev->fileop_lock); 1649 mutex_unlock(&dev->lock);
625 return -EINVAL; 1650 return -EINVAL;
626 } 1651 }
627 1652
@@ -633,7 +1658,7 @@ static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
633 while (size > 0) { /* size is page-aligned */ 1658 while (size > 0) { /* size is page-aligned */
634 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { 1659 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
635 em28xx_videodbg("mmap: vm_insert_page failed\n"); 1660 em28xx_videodbg("mmap: vm_insert_page failed\n");
636 mutex_unlock(&dev->fileop_lock); 1661 mutex_unlock(&dev->lock);
637 return -EAGAIN; 1662 return -EAGAIN;
638 } 1663 }
639 start += PAGE_SIZE; 1664 start += PAGE_SIZE;
@@ -645,900 +1670,210 @@ static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
645 vma->vm_private_data = &dev->frame[i]; 1670 vma->vm_private_data = &dev->frame[i];
646 1671
647 em28xx_vm_open(vma); 1672 em28xx_vm_open(vma);
648 mutex_unlock(&dev->fileop_lock); 1673 mutex_unlock(&dev->lock);
649 return 0;
650}
651
652/*
653 * em28xx_get_ctrl()
654 * return the current saturation, brightness or contrast, mute state
655 */
656static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
657{
658 switch (ctrl->id) {
659 case V4L2_CID_AUDIO_MUTE:
660 ctrl->value = dev->mute;
661 return 0;
662 case V4L2_CID_AUDIO_VOLUME:
663 ctrl->value = dev->volume;
664 return 0;
665 default:
666 return -EINVAL;
667 }
668}
669
670/*
671 * em28xx_set_ctrl()
672 * mute or set new saturation, brightness or contrast
673 */
674static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
675{
676 switch (ctrl->id) {
677 case V4L2_CID_AUDIO_MUTE:
678 if (ctrl->value != dev->mute) {
679 dev->mute = ctrl->value;
680 em28xx_audio_usb_mute(dev, ctrl->value);
681 return em28xx_audio_analog_set(dev);
682 }
683 return 0;
684 case V4L2_CID_AUDIO_VOLUME:
685 dev->volume = ctrl->value;
686 return em28xx_audio_analog_set(dev);
687 default:
688 return -EINVAL;
689 }
690}
691
692/*
693 * em28xx_stream_interrupt()
694 * stops streaming
695 */
696static int em28xx_stream_interrupt(struct em28xx *dev)
697{
698 int ret = 0;
699
700 /* stop reading from the device */
701
702 dev->stream = STREAM_INTERRUPT;
703 ret = wait_event_timeout(dev->wait_stream,
704 (dev->stream == STREAM_OFF) ||
705 (dev->state & DEV_DISCONNECTED),
706 EM28XX_URB_TIMEOUT);
707 if (dev->state & DEV_DISCONNECTED)
708 return -ENODEV;
709 else if (ret) {
710 dev->state |= DEV_MISCONFIGURED;
711 em28xx_videodbg("device is misconfigured; close and "
712 "open /dev/video%d again\n",
713 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
714 return ret;
715 }
716
717 return 0;
718}
719
720static int em28xx_set_norm(struct em28xx *dev, int width, int height)
721{
722 unsigned int hscale, vscale;
723 unsigned int maxh, maxw;
724
725 maxw = norm_maxw(dev);
726 maxh = norm_maxh(dev);
727
728 /* width must even because of the YUYV format */
729 /* height must be even because of interlacing */
730 height &= 0xfffe;
731 width &= 0xfffe;
732
733 if (height < 32)
734 height = 32;
735 if (height > maxh)
736 height = maxh;
737 if (width < 48)
738 width = 48;
739 if (width > maxw)
740 width = maxw;
741
742 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
743 hscale = 0x3fff;
744 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
745
746 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
747 vscale = 0x3fff;
748 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
749
750 /* set new image size */
751 dev->width = width;
752 dev->height = height;
753 dev->frame_size = dev->width * dev->height * 2;
754 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
755 dev->bytesperline = dev->width * 2;
756 dev->hscale = hscale;
757 dev->vscale = vscale;
758
759 em28xx_resolution_set(dev);
760
761 return 0; 1674 return 0;
762} 1675}
763 1676
764static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format) 1677static const struct file_operations em28xx_v4l_fops = {
765{ 1678 .owner = THIS_MODULE,
766 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n", 1679 .open = em28xx_v4l2_open,
767 (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ? 1680 .release = em28xx_v4l2_close,
768 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : 1681 .read = em28xx_v4l2_read,
769 (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ? 1682 .poll = em28xx_v4l2_poll,
770 "V4L2_BUF_TYPE_VBI_CAPTURE" : 1683 .mmap = em28xx_v4l2_mmap,
771 (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ? 1684 .ioctl = video_ioctl2,
772 "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " : 1685 .llseek = no_llseek,
773 "not supported"); 1686 .compat_ioctl = v4l_compat_ioctl32,
774 1687};
775 switch (format->type) {
776 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
777 {
778 format->fmt.pix.width = dev->width;
779 format->fmt.pix.height = dev->height;
780 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
781 format->fmt.pix.bytesperline = dev->bytesperline;
782 format->fmt.pix.sizeimage = dev->frame_size;
783 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
784 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
785 1688
786 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width, 1689static const struct file_operations radio_fops = {
787 dev->height); 1690 .owner = THIS_MODULE,
788 break; 1691 .open = em28xx_v4l2_open,
789 } 1692 .release = em28xx_v4l2_close,
1693 .ioctl = video_ioctl2,
1694 .compat_ioctl = v4l_compat_ioctl32,
1695 .llseek = no_llseek,
1696};
790 1697
791 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE: 1698static const struct video_device em28xx_video_template = {
792 { 1699 .fops = &em28xx_v4l_fops,
793 format->fmt.sliced.service_set=0; 1700 .release = video_device_release,
1701
1702 .minor = -1,
1703 .vidioc_querycap = vidioc_querycap,
1704 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1705 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1706 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1707 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1708 .vidioc_g_audio = vidioc_g_audio,
1709 .vidioc_s_audio = vidioc_s_audio,
1710 .vidioc_cropcap = vidioc_cropcap,
1711
1712 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1713 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1714 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1715
1716 .vidioc_reqbufs = vidioc_reqbufs,
1717 .vidioc_querybuf = vidioc_querybuf,
1718 .vidioc_qbuf = vidioc_qbuf,
1719 .vidioc_dqbuf = vidioc_dqbuf,
1720 .vidioc_s_std = vidioc_s_std,
1721 .vidioc_enum_input = vidioc_enum_input,
1722 .vidioc_g_input = vidioc_g_input,
1723 .vidioc_s_input = vidioc_s_input,
1724 .vidioc_queryctrl = vidioc_queryctrl,
1725 .vidioc_g_ctrl = vidioc_g_ctrl,
1726 .vidioc_s_ctrl = vidioc_s_ctrl,
1727 .vidioc_streamon = vidioc_streamon,
1728 .vidioc_streamoff = vidioc_streamoff,
1729 .vidioc_g_tuner = vidioc_g_tuner,
1730 .vidioc_s_tuner = vidioc_s_tuner,
1731 .vidioc_g_frequency = vidioc_g_frequency,
1732 .vidioc_s_frequency = vidioc_s_frequency,
1733
1734 .tvnorms = V4L2_STD_ALL,
1735 .current_norm = V4L2_STD_PAL,
1736};
794 1737
795 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format); 1738static struct video_device em28xx_radio_template = {
1739 .name = "em28xx-radio",
1740 .type = VID_TYPE_TUNER,
1741 .fops = &radio_fops,
1742 .minor = -1,
1743 .vidioc_querycap = radio_querycap,
1744 .vidioc_g_tuner = radio_g_tuner,
1745 .vidioc_enum_input = radio_enum_input,
1746 .vidioc_g_audio = radio_g_audio,
1747 .vidioc_s_tuner = radio_s_tuner,
1748 .vidioc_s_audio = radio_s_audio,
1749 .vidioc_s_input = radio_s_input,
1750 .vidioc_queryctrl = radio_queryctrl,
1751 .vidioc_g_ctrl = vidioc_g_ctrl,
1752 .vidioc_s_ctrl = vidioc_s_ctrl,
1753 .vidioc_g_frequency = vidioc_g_frequency,
1754 .vidioc_s_frequency = vidioc_s_frequency,
1755};
796 1756
797 if (format->fmt.sliced.service_set==0) 1757/******************************** usb interface *****************************************/
798 return -EINVAL;
799 1758
800 break;
801 }
802 1759
803 default: 1760static LIST_HEAD(em28xx_extension_devlist);
804 return -EINVAL; 1761static DEFINE_MUTEX(em28xx_extension_devlist_lock);
805 }
806 return (0);
807}
808 1762
809static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format) 1763int em28xx_register_extension(struct em28xx_ops *ops)
810{ 1764{
811 u32 i; 1765 struct em28xx *h, *dev = NULL;
812 int ret = 0;
813 int width = format->fmt.pix.width;
814 int height = format->fmt.pix.height;
815 unsigned int hscale, vscale;
816 unsigned int maxh, maxw;
817 1766
818 maxw = norm_maxw(dev); 1767 list_for_each_entry(h, &em28xx_devlist, devlist)
819 maxh = norm_maxh(dev); 1768 dev = h;
820
821 em28xx_videodbg("%s: type=%s\n",
822 cmd == VIDIOC_TRY_FMT ?
823 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
824 format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ?
825 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
826 format->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
827 "V4L2_BUF_TYPE_VBI_CAPTURE " :
828 "not supported");
829
830 if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
831 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
832
833 if (format->fmt.sliced.service_set==0)
834 return -EINVAL;
835 1769
836 return 0; 1770 mutex_lock(&em28xx_extension_devlist_lock);
837 } 1771 list_add_tail(&ops->next, &em28xx_extension_devlist);
1772 if (dev)
1773 ops->init(dev);
838 1774
839 1775 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
840 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1776 mutex_unlock(&em28xx_extension_devlist_lock);
841 return -EINVAL;
842
843 em28xx_videodbg("%s: requested %dx%d\n",
844 cmd == VIDIOC_TRY_FMT ?
845 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
846 format->fmt.pix.width, format->fmt.pix.height);
847
848 /* FIXME: Move some code away from here */
849 /* width must even because of the YUYV format */
850 /* height must be even because of interlacing */
851 height &= 0xfffe;
852 width &= 0xfffe;
853
854 if (height < 32)
855 height = 32;
856 if (height > maxh)
857 height = maxh;
858 if (width < 48)
859 width = 48;
860 if (width > maxw)
861 width = maxw;
862
863 if(dev->is_em2800){
864 /* the em2800 can only scale down to 50% */
865 if(height % (maxh / 2))
866 height=maxh;
867 if(width % (maxw / 2))
868 width=maxw;
869 /* according to empiatech support */
870 /* the MaxPacketSize is to small to support */
871 /* framesizes larger than 640x480 @ 30 fps */
872 /* or 640x576 @ 25 fps. As this would cut */
873 /* of a part of the image we prefer */
874 /* 360x576 or 360x480 for now */
875 if(width == maxw && height == maxh)
876 width /= 2;
877 }
878
879 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
880 hscale = 0x3fff;
881
882 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
883
884 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
885 vscale = 0x3fff;
886
887 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
888
889 format->fmt.pix.width = width;
890 format->fmt.pix.height = height;
891 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
892 format->fmt.pix.bytesperline = width * 2;
893 format->fmt.pix.sizeimage = width * 2 * height;
894 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
895 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
896
897 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
898 cmd == VIDIOC_TRY_FMT ?
899 "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
900 format->fmt.pix.width, format->fmt.pix.height, hscale, vscale);
901
902 if (cmd == VIDIOC_TRY_FMT)
903 return 0;
904
905 for (i = 0; i < dev->num_frames; i++)
906 if (dev->frame[i].vma_use_count) {
907 em28xx_videodbg("VIDIOC_S_FMT failed. "
908 "Unmap the buffers first.\n");
909 return -EINVAL;
910 }
911
912 /* stop io in case it is already in progress */
913 if (dev->stream == STREAM_ON) {
914 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
915 if ((ret = em28xx_stream_interrupt(dev)))
916 return ret;
917 }
918
919 em28xx_release_buffers(dev);
920 dev->io = IO_NONE;
921
922 /* set new image size */
923 dev->width = width;
924 dev->height = height;
925 dev->frame_size = dev->width * dev->height * 2;
926 dev->field_size = dev->frame_size >> 1;
927 dev->bytesperline = dev->width * 2;
928 dev->hscale = hscale;
929 dev->vscale = vscale;
930 em28xx_uninit_isoc(dev);
931 em28xx_set_alternate(dev);
932 em28xx_capture_start(dev, 1);
933 em28xx_resolution_set(dev);
934 em28xx_init_isoc(dev);
935 1777
936 return 0; 1778 return 0;
937} 1779}
1780EXPORT_SYMBOL(em28xx_register_extension);
938 1781
939/* 1782void em28xx_unregister_extension(struct em28xx_ops *ops)
940 * em28xx_v4l2_do_ioctl()
941 * This function is _not_ called directly, but from
942 * em28xx_v4l2_ioctl. Userspace
943 * copying is done already, arg is a kernel pointer.
944 */
945static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
946 struct em28xx *dev, unsigned int cmd, void *arg,
947 v4l2_kioctl driver_ioctl)
948{ 1783{
949 int ret; 1784 struct em28xx *h, *dev = NULL;
950 1785
951 switch (cmd) { 1786 list_for_each_entry(h, &em28xx_devlist, devlist)
952 /* ---------- tv norms ---------- */ 1787 dev = h;
953 case VIDIOC_ENUMSTD:
954 {
955 struct v4l2_standard *e = arg;
956 unsigned int i;
957 1788
958 i = e->index; 1789 if (dev)
959 if (i >= TVNORMS) 1790 ops->fini(dev);
960 return -EINVAL;
961 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
962 tvnorms[e->index].name);
963 e->index = i;
964 if (ret < 0)
965 return ret;
966 return 0;
967 }
968 case VIDIOC_G_STD:
969 {
970 v4l2_std_id *id = arg;
971 1791
972 *id = dev->tvnorm->id; 1792 mutex_lock(&em28xx_extension_devlist_lock);
973 return 0; 1793 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
974 } 1794 list_del(&ops->next);
975 case VIDIOC_S_STD: 1795 mutex_unlock(&em28xx_extension_devlist_lock);
976 {
977 v4l2_std_id *id = arg;
978 unsigned int i;
979
980 for (i = 0; i < TVNORMS; i++)
981 if (*id == tvnorms[i].id)
982 break;
983 if (i == TVNORMS)
984 for (i = 0; i < TVNORMS; i++)
985 if (*id & tvnorms[i].id)
986 break;
987 if (i == TVNORMS)
988 return -EINVAL;
989
990 mutex_lock(&dev->lock);
991 dev->tvnorm = &tvnorms[i];
992
993 em28xx_set_norm(dev, dev->width, dev->height);
994
995 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
996 &dev->tvnorm->id);
997
998 mutex_unlock(&dev->lock);
999
1000 return 0;
1001 }
1002
1003 /* ------ input switching ---------- */
1004 case VIDIOC_ENUMINPUT:
1005 {
1006 struct v4l2_input *i = arg;
1007 unsigned int n;
1008 static const char *iname[] = {
1009 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1010 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1011 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1012 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1013 [EM28XX_VMUX_SVIDEO] = "S-Video",
1014 [EM28XX_VMUX_TELEVISION] = "Television",
1015 [EM28XX_VMUX_CABLE] = "Cable TV",
1016 [EM28XX_VMUX_DVB] = "DVB",
1017 [EM28XX_VMUX_DEBUG] = "for debug only",
1018 };
1019
1020 n = i->index;
1021 if (n >= MAX_EM28XX_INPUT)
1022 return -EINVAL;
1023 if (0 == INPUT(n)->type)
1024 return -EINVAL;
1025 memset(i, 0, sizeof(*i));
1026 i->index = n;
1027 i->type = V4L2_INPUT_TYPE_CAMERA;
1028 strcpy(i->name, iname[INPUT(n)->type]);
1029 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1030 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1031 i->type = V4L2_INPUT_TYPE_TUNER;
1032 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1033 i->std |= tvnorms[n].id;
1034 return 0;
1035 }
1036 case VIDIOC_G_INPUT:
1037 {
1038 int *i = arg;
1039 *i = dev->ctl_input;
1040
1041 return 0;
1042 }
1043 case VIDIOC_S_INPUT:
1044 {
1045 int *index = arg;
1046
1047 if (*index >= MAX_EM28XX_INPUT)
1048 return -EINVAL;
1049 if (0 == INPUT(*index)->type)
1050 return -EINVAL;
1051
1052 mutex_lock(&dev->lock);
1053 video_mux(dev, *index);
1054 mutex_unlock(&dev->lock);
1055
1056 return 0;
1057 }
1058 case VIDIOC_G_AUDIO:
1059 {
1060 struct v4l2_audio *a = arg;
1061 unsigned int index = a->index;
1062
1063 if (a->index > 1)
1064 return -EINVAL;
1065 memset(a, 0, sizeof(*a));
1066 index = dev->ctl_ainput;
1067
1068 if (index == 0) {
1069 strcpy(a->name, "Television");
1070 } else {
1071 strcpy(a->name, "Line In");
1072 }
1073 a->capability = V4L2_AUDCAP_STEREO;
1074 a->index = index;
1075 return 0;
1076 }
1077 case VIDIOC_S_AUDIO:
1078 {
1079 struct v4l2_audio *a = arg;
1080
1081 if (a->index != dev->ctl_ainput)
1082 return -EINVAL;
1083
1084 return 0;
1085 }
1086
1087 /* --- controls ---------------------------------------------- */
1088 case VIDIOC_QUERYCTRL:
1089 {
1090 struct v4l2_queryctrl *qc = arg;
1091 int i, id=qc->id;
1092
1093 memset(qc,0,sizeof(*qc));
1094 qc->id=id;
1095
1096 if (!dev->has_msp34xx) {
1097 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1098 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1099 memcpy(qc, &(em28xx_qctrl[i]),
1100 sizeof(*qc));
1101 return 0;
1102 }
1103 }
1104 }
1105 em28xx_i2c_call_clients(dev,cmd,qc);
1106 if (qc->type)
1107 return 0;
1108 else
1109 return -EINVAL;
1110 }
1111 case VIDIOC_G_CTRL:
1112 {
1113 struct v4l2_control *ctrl = arg;
1114 int retval=-EINVAL;
1115
1116 if (!dev->has_msp34xx)
1117 retval=em28xx_get_ctrl(dev, ctrl);
1118 if (retval==-EINVAL) {
1119 em28xx_i2c_call_clients(dev,cmd,arg);
1120 return 0;
1121 } else return retval;
1122 }
1123 case VIDIOC_S_CTRL:
1124 {
1125 struct v4l2_control *ctrl = arg;
1126 u8 i;
1127
1128 if (!dev->has_msp34xx){
1129 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1130 if (ctrl->id == em28xx_qctrl[i].id) {
1131 if (ctrl->value <
1132 em28xx_qctrl[i].minimum
1133 || ctrl->value >
1134 em28xx_qctrl[i].maximum)
1135 return -ERANGE;
1136 return em28xx_set_ctrl(dev, ctrl);
1137 }
1138 }
1139 }
1140
1141 em28xx_i2c_call_clients(dev,cmd,arg);
1142 return 0;
1143 }
1144 /* --- tuner ioctls ------------------------------------------ */
1145 case VIDIOC_G_TUNER:
1146 {
1147 struct v4l2_tuner *t = arg;
1148
1149 if (0 != t->index)
1150 return -EINVAL;
1151
1152 memset(t, 0, sizeof(*t));
1153 strcpy(t->name, "Tuner");
1154 mutex_lock(&dev->lock);
1155 /* let clients fill in the remainder of this struct */
1156 em28xx_i2c_call_clients(dev, cmd, t);
1157 mutex_unlock(&dev->lock);
1158 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1159 t->afc);
1160 return 0;
1161 }
1162 case VIDIOC_S_TUNER:
1163 {
1164 struct v4l2_tuner *t = arg;
1165
1166 if (0 != t->index)
1167 return -EINVAL;
1168 mutex_lock(&dev->lock);
1169 /* let clients handle this */
1170 em28xx_i2c_call_clients(dev, cmd, t);
1171 mutex_unlock(&dev->lock);
1172 return 0;
1173 }
1174 case VIDIOC_G_FREQUENCY:
1175 {
1176 struct v4l2_frequency *f = arg;
1177
1178 memset(f, 0, sizeof(*f));
1179 f->type = V4L2_TUNER_ANALOG_TV;
1180 f->frequency = dev->ctl_freq;
1181
1182 return 0;
1183 }
1184 case VIDIOC_S_FREQUENCY:
1185 {
1186 struct v4l2_frequency *f = arg;
1187
1188 if (0 != f->tuner)
1189 return -EINVAL;
1190
1191 if (V4L2_TUNER_ANALOG_TV != f->type)
1192 return -EINVAL;
1193
1194 mutex_lock(&dev->lock);
1195 dev->ctl_freq = f->frequency;
1196 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1197 mutex_unlock(&dev->lock);
1198 return 0;
1199 }
1200 case VIDIOC_CROPCAP:
1201 {
1202 struct v4l2_cropcap *cc = arg;
1203
1204 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1205 return -EINVAL;
1206 cc->bounds.left = 0;
1207 cc->bounds.top = 0;
1208 cc->bounds.width = dev->width;
1209 cc->bounds.height = dev->height;
1210 cc->defrect = cc->bounds;
1211 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1212 cc->pixelaspect.denominator = 59;
1213 return 0;
1214 }
1215 case VIDIOC_STREAMON:
1216 {
1217 int *type = arg;
1218
1219 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1220 || dev->io != IO_MMAP)
1221 return -EINVAL;
1222
1223 if (list_empty(&dev->inqueue))
1224 return -EINVAL;
1225
1226 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1227
1228 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
1229
1230 return 0;
1231 }
1232 case VIDIOC_STREAMOFF:
1233 {
1234 int *type = arg;
1235 int ret;
1236
1237 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1238 || dev->io != IO_MMAP)
1239 return -EINVAL;
1240
1241 if (dev->stream == STREAM_ON) {
1242 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1243 if ((ret = em28xx_stream_interrupt(dev)))
1244 return ret;
1245 }
1246 em28xx_empty_framequeues(dev);
1247
1248 return 0;
1249 }
1250 default:
1251 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1252 driver_ioctl);
1253 }
1254 return 0;
1255}
1256
1257/*
1258 * em28xx_v4l2_do_ioctl()
1259 * This function is _not_ called directly, but from
1260 * em28xx_v4l2_ioctl. Userspace
1261 * copying is done already, arg is a kernel pointer.
1262 */
1263static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
1264 unsigned int cmd, void *arg)
1265{
1266 struct em28xx *dev = filp->private_data;
1267
1268 if (!dev)
1269 return -ENODEV;
1270
1271 if (video_debug > 1)
1272 v4l_print_ioctl(dev->name,cmd);
1273
1274 switch (cmd) {
1275
1276 /* --- capabilities ------------------------------------------ */
1277 case VIDIOC_QUERYCAP:
1278 {
1279 struct v4l2_capability *cap = arg;
1280
1281 memset(cap, 0, sizeof(*cap));
1282 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1283 strlcpy(cap->card, em28xx_boards[dev->model].name,
1284 sizeof(cap->card));
1285 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1286 sizeof(cap->bus_info));
1287 cap->version = EM28XX_VERSION_CODE;
1288 cap->capabilities =
1289 V4L2_CAP_SLICED_VBI_CAPTURE |
1290 V4L2_CAP_VIDEO_CAPTURE |
1291 V4L2_CAP_AUDIO |
1292 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1293 if (dev->has_tuner)
1294 cap->capabilities |= V4L2_CAP_TUNER;
1295 return 0;
1296 }
1297 /* --- capture ioctls ---------------------------------------- */
1298 case VIDIOC_ENUM_FMT:
1299 {
1300 struct v4l2_fmtdesc *fmtd = arg;
1301
1302 if (fmtd->index != 0)
1303 return -EINVAL;
1304 memset(fmtd, 0, sizeof(*fmtd));
1305 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1306 strcpy(fmtd->description, "Packed YUY2");
1307 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1308 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1309 return 0;
1310 }
1311 case VIDIOC_G_FMT:
1312 return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
1313
1314 case VIDIOC_TRY_FMT:
1315 case VIDIOC_S_FMT:
1316 return em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
1317
1318 case VIDIOC_REQBUFS:
1319 {
1320 struct v4l2_requestbuffers *rb = arg;
1321 u32 i;
1322 int ret;
1323
1324 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1325 rb->memory != V4L2_MEMORY_MMAP)
1326 return -EINVAL;
1327
1328 if (dev->io == IO_READ) {
1329 em28xx_videodbg ("method is set to read;"
1330 " close and open the device again to"
1331 " choose the mmap I/O method\n");
1332 return -EINVAL;
1333 }
1334
1335 for (i = 0; i < dev->num_frames; i++)
1336 if (dev->frame[i].vma_use_count) {
1337 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1338 return -EINVAL;
1339 }
1340
1341 if (dev->stream == STREAM_ON) {
1342 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1343 if ((ret = em28xx_stream_interrupt(dev)))
1344 return ret;
1345 }
1346
1347 em28xx_empty_framequeues(dev);
1348
1349 em28xx_release_buffers(dev);
1350 if (rb->count)
1351 rb->count =
1352 em28xx_request_buffers(dev, rb->count);
1353
1354 dev->frame_current = NULL;
1355
1356 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1357 rb->count);
1358 dev->io = rb->count ? IO_MMAP : IO_NONE;
1359 return 0;
1360 }
1361 case VIDIOC_QUERYBUF:
1362 {
1363 struct v4l2_buffer *b = arg;
1364
1365 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1366 b->index >= dev->num_frames || dev->io != IO_MMAP)
1367 return -EINVAL;
1368
1369 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1370
1371 if (dev->frame[b->index].vma_use_count) {
1372 b->flags |= V4L2_BUF_FLAG_MAPPED;
1373 }
1374 if (dev->frame[b->index].state == F_DONE)
1375 b->flags |= V4L2_BUF_FLAG_DONE;
1376 else if (dev->frame[b->index].state != F_UNUSED)
1377 b->flags |= V4L2_BUF_FLAG_QUEUED;
1378 return 0;
1379 }
1380 case VIDIOC_QBUF:
1381 {
1382 struct v4l2_buffer *b = arg;
1383 unsigned long lock_flags;
1384
1385 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1386 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1387 return -EINVAL;
1388 }
1389
1390 if (dev->frame[b->index].state != F_UNUSED) {
1391 return -EAGAIN;
1392 }
1393 dev->frame[b->index].state = F_QUEUED;
1394
1395 /* add frame to fifo */
1396 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1397 list_add_tail(&dev->frame[b->index].frame,
1398 &dev->inqueue);
1399 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1400
1401 return 0;
1402 }
1403 case VIDIOC_DQBUF:
1404 {
1405 struct v4l2_buffer *b = arg;
1406 struct em28xx_frame_t *f;
1407 unsigned long lock_flags;
1408 int ret = 0;
1409
1410 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1411 || dev->io != IO_MMAP)
1412 return -EINVAL;
1413
1414 if (list_empty(&dev->outqueue)) {
1415 if (dev->stream == STREAM_OFF)
1416 return -EINVAL;
1417 if (filp->f_flags & O_NONBLOCK)
1418 return -EAGAIN;
1419 ret = wait_event_interruptible
1420 (dev->wait_frame,
1421 (!list_empty(&dev->outqueue)) ||
1422 (dev->state & DEV_DISCONNECTED));
1423 if (ret)
1424 return ret;
1425 if (dev->state & DEV_DISCONNECTED)
1426 return -ENODEV;
1427 }
1428
1429 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1430 f = list_entry(dev->outqueue.next,
1431 struct em28xx_frame_t, frame);
1432 list_del(dev->outqueue.next);
1433 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1434
1435 f->state = F_UNUSED;
1436 memcpy(b, &f->buf, sizeof(*b));
1437
1438 if (f->vma_use_count)
1439 b->flags |= V4L2_BUF_FLAG_MAPPED;
1440
1441 return 0;
1442 }
1443 default:
1444 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1445 em28xx_video_do_ioctl);
1446 }
1447 return 0;
1448} 1796}
1797EXPORT_SYMBOL(em28xx_unregister_extension);
1449 1798
1450/* 1799struct video_device *em28xx_vdev_init(struct em28xx *dev,
1451 * em28xx_v4l2_ioctl() 1800 const struct video_device *template,
1452 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl() 1801 const int type,
1453 */ 1802 const char *type_name)
1454static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
1455 unsigned int cmd, unsigned long arg)
1456{ 1803{
1457 int ret = 0; 1804 struct video_device *vfd;
1458 struct em28xx *dev = filp->private_data;
1459
1460 if (mutex_lock_interruptible(&dev->fileop_lock))
1461 return -ERESTARTSYS;
1462
1463 if (dev->state & DEV_DISCONNECTED) {
1464 em28xx_errdev("v4l2 ioctl: device not present\n");
1465 mutex_unlock(&dev->fileop_lock);
1466 return -ENODEV;
1467 }
1468
1469 if (dev->state & DEV_MISCONFIGURED) {
1470 em28xx_errdev
1471 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1472 mutex_unlock(&dev->fileop_lock);
1473 return -EIO;
1474 }
1475 1805
1476 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl); 1806 vfd = video_device_alloc();
1807 if (NULL == vfd)
1808 return NULL;
1809 *vfd = *template;
1810 vfd->minor = -1;
1811 vfd->dev = &dev->udev->dev;
1812 vfd->release = video_device_release;
1813 vfd->type = type;
1477 1814
1478 mutex_unlock(&dev->fileop_lock); 1815 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1816 dev->name, type_name);
1479 1817
1480 return ret; 1818 return vfd;
1481} 1819}
1482 1820
1483static const struct file_operations em28xx_v4l_fops = {
1484 .owner = THIS_MODULE,
1485 .open = em28xx_v4l2_open,
1486 .release = em28xx_v4l2_close,
1487 .ioctl = em28xx_v4l2_ioctl,
1488 .read = em28xx_v4l2_read,
1489 .poll = em28xx_v4l2_poll,
1490 .mmap = em28xx_v4l2_mmap,
1491 .llseek = no_llseek,
1492 .compat_ioctl = v4l_compat_ioctl32,
1493
1494};
1495
1496/******************************** usb interface *****************************************/
1497 1821
1498/* 1822/*
1499 * em28xx_init_dev() 1823 * em28xx_init_dev()
1500 * allocates and inits the device structs, registers i2c bus and v4l device 1824 * allocates and inits the device structs, registers i2c bus and v4l device
1501 */ 1825 */
1502static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev, 1826static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1503 int minor, int model) 1827 int minor)
1504{ 1828{
1829 struct em28xx_ops *ops = NULL;
1505 struct em28xx *dev = *devhandle; 1830 struct em28xx *dev = *devhandle;
1506 int retval = -ENOMEM; 1831 int retval = -ENOMEM;
1507 int errCode, i; 1832 int errCode;
1508 unsigned int maxh, maxw; 1833 unsigned int maxh, maxw;
1509 1834
1510 dev->udev = udev; 1835 dev->udev = udev;
1511 dev->model = model;
1512 mutex_init(&dev->lock); 1836 mutex_init(&dev->lock);
1837 spin_lock_init(&dev->queue_lock);
1513 init_waitqueue_head(&dev->open); 1838 init_waitqueue_head(&dev->open);
1839 init_waitqueue_head(&dev->wait_frame);
1840 init_waitqueue_head(&dev->wait_stream);
1514 1841
1515 dev->em28xx_write_regs = em28xx_write_regs; 1842 dev->em28xx_write_regs = em28xx_write_regs;
1516 dev->em28xx_read_reg = em28xx_read_reg; 1843 dev->em28xx_read_reg = em28xx_read_reg;
1517 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len; 1844 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1518 dev->em28xx_write_regs_req = em28xx_write_regs_req; 1845 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1519 dev->em28xx_read_reg_req = em28xx_read_reg_req; 1846 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1520 dev->is_em2800 = em28xx_boards[model].is_em2800; 1847 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
1521 dev->has_tuner = em28xx_boards[model].has_tuner;
1522 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1523 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1524 dev->decoder = em28xx_boards[model].decoder;
1525
1526 if (tuner >= 0)
1527 dev->tuner_type = tuner;
1528 else
1529 dev->tuner_type = em28xx_boards[model].tuner_type;
1530 1848
1531 dev->video_inputs = em28xx_boards[model].vchannels; 1849 errCode = em28xx_read_reg(dev, CHIPID_REG);
1850 if (errCode >= 0)
1851 em28xx_info("em28xx chip ID = %d\n", errCode);
1532 1852
1533 for (i = 0; i < TVNORMS; i++) 1853 em28xx_pre_card_setup(dev);
1534 if (em28xx_boards[model].norm == tvnorms[i].mode) 1854
1535 break; 1855 errCode = em28xx_config(dev);
1536 if (i == TVNORMS) 1856 if (errCode) {
1537 i = 0; 1857 em28xx_errdev("error configuring device\n");
1858 em28xx_devused &= ~(1<<dev->devno);
1859 kfree(dev);
1860 return -ENOMEM;
1861 }
1862
1863 /* register i2c bus */
1864 em28xx_i2c_register(dev);
1538 1865
1539 dev->tvnorm = &tvnorms[i]; /* set default norm */ 1866 /* Do board specific init and eeprom reading */
1867 em28xx_card_setup(dev);
1540 1868
1541 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name); 1869 /* Configure audio */
1870 em28xx_audio_analog_set(dev);
1871
1872 /* configure the device */
1873 em28xx_config_i2c(dev);
1874
1875 /* set default norm */
1876 dev->norm = em28xx_video_template.current_norm;
1542 1877
1543 maxw = norm_maxw(dev); 1878 maxw = norm_maxw(dev);
1544 maxh = norm_maxh(dev); 1879 maxh = norm_maxh(dev);
@@ -1555,138 +1890,110 @@ static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1555 dev->vscale = 0; 1890 dev->vscale = 0;
1556 dev->ctl_input = 2; 1891 dev->ctl_input = 2;
1557 1892
1558 /* setup video picture settings for saa7113h */
1559 memset(&dev->vpic, 0, sizeof(dev->vpic));
1560 dev->vpic.colour = 128 << 8;
1561 dev->vpic.hue = 128 << 8;
1562 dev->vpic.brightness = 128 << 8;
1563 dev->vpic.contrast = 192 << 8;
1564 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1565 dev->vpic.depth = 16;
1566 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1567
1568 em28xx_pre_card_setup(dev);
1569#ifdef CONFIG_MODULES
1570 /* request some modules */
1571 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
1572 request_module("saa7115");
1573 if (dev->decoder == EM28XX_TVP5150)
1574 request_module("tvp5150");
1575 if (dev->has_tuner)
1576 request_module("tuner");
1577#endif
1578 errCode = em28xx_config(dev); 1893 errCode = em28xx_config(dev);
1579 if (errCode) {
1580 em28xx_errdev("error configuring device\n");
1581 em28xx_devused&=~(1<<dev->devno);
1582 kfree(dev);
1583 return -ENOMEM;
1584 }
1585
1586 mutex_lock(&dev->lock);
1587 /* register i2c bus */
1588 em28xx_i2c_register(dev);
1589 1894
1590 /* Do board specific init and eeprom reading */ 1895 list_add_tail(&dev->devlist, &em28xx_devlist);
1591 em28xx_card_setup(dev);
1592 1896
1593 /* configure the device */ 1897 /* allocate and fill video video_device struct */
1594 em28xx_config_i2c(dev); 1898 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
1595 1899 VID_TYPE_CAPTURE, "video");
1596 mutex_unlock(&dev->lock);
1597
1598 errCode = em28xx_config(dev);
1599
1600#ifdef CONFIG_MODULES
1601 if (dev->has_msp34xx)
1602 request_module("msp3400");
1603#endif
1604 /* allocate and fill v4l2 device struct */
1605 dev->vdev = video_device_alloc();
1606 if (NULL == dev->vdev) { 1900 if (NULL == dev->vdev) {
1607 em28xx_errdev("cannot allocate video_device.\n"); 1901 em28xx_errdev("cannot allocate video_device.\n");
1608 em28xx_devused&=~(1<<dev->devno); 1902 goto fail_unreg;
1609 kfree(dev);
1610 return -ENOMEM;
1611 }
1612
1613 dev->vbi_dev = video_device_alloc();
1614 if (NULL == dev->vbi_dev) {
1615 em28xx_errdev("cannot allocate video_device.\n");
1616 kfree(dev->vdev);
1617 em28xx_devused&=~(1<<dev->devno);
1618 kfree(dev);
1619 return -ENOMEM;
1620 } 1903 }
1621 1904 if (dev->tuner_type != TUNER_ABSENT)
1622 /* Fills VBI device info */
1623 dev->vbi_dev->type = VFL_TYPE_VBI;
1624 dev->vbi_dev->fops = &em28xx_v4l_fops;
1625 dev->vbi_dev->minor = -1;
1626 dev->vbi_dev->dev = &dev->udev->dev;
1627 dev->vbi_dev->release = video_device_release;
1628 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1629 "em28xx",dev->devno,"vbi");
1630
1631 /* Fills CAPTURE device info */
1632 dev->vdev->type = VID_TYPE_CAPTURE;
1633 if (dev->has_tuner)
1634 dev->vdev->type |= VID_TYPE_TUNER; 1905 dev->vdev->type |= VID_TYPE_TUNER;
1635 dev->vdev->fops = &em28xx_v4l_fops;
1636 dev->vdev->minor = -1;
1637 dev->vdev->dev = &dev->udev->dev;
1638 dev->vdev->release = video_device_release;
1639 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1640 "em28xx",dev->devno,"video");
1641
1642 list_add_tail(&dev->devlist,&em28xx_devlist);
1643 1906
1644 /* register v4l2 device */ 1907 /* register v4l2 video video_device */
1645 mutex_lock(&dev->lock); 1908 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1646 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, 1909 video_nr[dev->devno]);
1647 video_nr[dev->devno]))) { 1910 if (retval) {
1648 em28xx_errdev("unable to register video device (error=%i).\n", 1911 em28xx_errdev("unable to register video device (error=%i).\n",
1649 retval); 1912 retval);
1650 mutex_unlock(&dev->lock); 1913 goto fail_unreg;
1651 list_del(&dev->devlist);
1652 video_device_release(dev->vdev);
1653 em28xx_devused&=~(1<<dev->devno);
1654 kfree(dev);
1655 return -ENODEV;
1656 } 1914 }
1657 1915
1916 /* Allocate and fill vbi video_device struct */
1917 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1918 VFL_TYPE_VBI, "vbi");
1919 /* register v4l2 vbi video_device */
1658 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI, 1920 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1659 vbi_nr[dev->devno]) < 0) { 1921 vbi_nr[dev->devno]) < 0) {
1660 printk("unable to register vbi device\n"); 1922 em28xx_errdev("unable to register vbi device\n");
1661 mutex_unlock(&dev->lock); 1923 retval = -ENODEV;
1662 list_del(&dev->devlist); 1924 goto fail_unreg;
1663 video_device_release(dev->vbi_dev);
1664 video_device_release(dev->vdev);
1665 em28xx_devused&=~(1<<dev->devno);
1666 kfree(dev);
1667 return -ENODEV;
1668 } else {
1669 printk("registered VBI\n");
1670 } 1925 }
1671 1926
1927 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
1928 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
1929 VFL_TYPE_RADIO, "radio");
1930 if (NULL == dev->radio_dev) {
1931 em28xx_errdev("cannot allocate video_device.\n");
1932 goto fail_unreg;
1933 }
1934 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1935 radio_nr[dev->devno]);
1936 if (retval < 0) {
1937 em28xx_errdev("can't register radio device\n");
1938 goto fail_unreg;
1939 }
1940 em28xx_info("Registered radio device as /dev/radio%d\n",
1941 dev->radio_dev->minor & 0x1f);
1942 }
1943
1944
1672 if (dev->has_msp34xx) { 1945 if (dev->has_msp34xx) {
1673 /* Send a reset to other chips via gpio */ 1946 /* Send a reset to other chips via gpio */
1674 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1); 1947 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1675 msleep(3); 1948 msleep(3);
1676 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1); 1949 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1677 msleep(3); 1950 msleep(3);
1678
1679 } 1951 }
1680 video_mux(dev, 0);
1681 1952
1682 mutex_unlock(&dev->lock); 1953 video_mux(dev, 0);
1683 1954
1684 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n", 1955 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1685 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN, 1956 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1686 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN); 1957 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1687 1958
1959 mutex_lock(&em28xx_extension_devlist_lock);
1960 if (!list_empty(&em28xx_extension_devlist)) {
1961 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1962 if (ops->id)
1963 ops->init(dev);
1964 }
1965 }
1966 mutex_unlock(&em28xx_extension_devlist_lock);
1967
1688 return 0; 1968 return 0;
1969
1970fail_unreg:
1971 em28xx_release_resources(dev);
1972 mutex_unlock(&dev->lock);
1973 kfree(dev);
1974 return retval;
1975}
1976
1977#if defined(CONFIG_MODULES) && defined(MODULE)
1978static void request_module_async(struct work_struct *work)
1979{
1980 struct em28xx *dev = container_of(work,
1981 struct em28xx, request_module_wk);
1982
1983 if (dev->has_audio_class)
1984 request_module("snd-usb-audio");
1985 else
1986 request_module("em28xx-alsa");
1987}
1988
1989static void request_modules(struct em28xx *dev)
1990{
1991 INIT_WORK(&dev->request_module_wk, request_module_async);
1992 schedule_work(&dev->request_module_wk);
1689} 1993}
1994#else
1995#define request_modules(dev)
1996#endif /* CONFIG_MODULES */
1690 1997
1691/* 1998/*
1692 * em28xx_usb_probe() 1999 * em28xx_usb_probe()
@@ -1700,7 +2007,7 @@ static int em28xx_usb_probe(struct usb_interface *interface,
1700 struct usb_interface *uif; 2007 struct usb_interface *uif;
1701 struct em28xx *dev = NULL; 2008 struct em28xx *dev = NULL;
1702 int retval = -ENODEV; 2009 int retval = -ENODEV;
1703 int model,i,nr,ifnum; 2010 int i, nr, ifnum;
1704 2011
1705 udev = usb_get_dev(interface_to_usbdev(interface)); 2012 udev = usb_get_dev(interface_to_usbdev(interface));
1706 ifnum = interface->altsetting[0].desc.bInterfaceNumber; 2013 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
@@ -1740,8 +2047,6 @@ static int em28xx_usb_probe(struct usb_interface *interface,
1740 return -ENODEV; 2047 return -ENODEV;
1741 } 2048 }
1742 2049
1743 model=id->driver_info;
1744
1745 if (nr >= EM28XX_MAXBOARDS) { 2050 if (nr >= EM28XX_MAXBOARDS) {
1746 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS); 2051 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
1747 em28xx_devused&=~(1<<nr); 2052 em28xx_devused&=~(1<<nr);
@@ -1757,7 +2062,20 @@ static int em28xx_usb_probe(struct usb_interface *interface,
1757 } 2062 }
1758 2063
1759 snprintf(dev->name, 29, "em28xx #%d", nr); 2064 snprintf(dev->name, 29, "em28xx #%d", nr);
1760 dev->devno=nr; 2065 dev->devno = nr;
2066 dev->model = id->driver_info;
2067
2068 /* Checks if audio is provided by some interface */
2069 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2070 uif = udev->config->interface[i];
2071 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2072 dev->has_audio_class = 1;
2073 break;
2074 }
2075 }
2076
2077 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2078 dev->has_audio_class ? "Has" : "Doesn't have");
1761 2079
1762 /* compute alternate max packet sizes */ 2080 /* compute alternate max packet sizes */
1763 uif = udev->actconfig->interface[0]; 2081 uif = udev->actconfig->interface[0];
@@ -1784,33 +2102,20 @@ static int em28xx_usb_probe(struct usb_interface *interface,
1784 } 2102 }
1785 2103
1786 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount)) 2104 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
1787 model=card[nr]; 2105 dev->model = card[nr];
1788
1789 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1790 em28xx_errdev( "Your board has no eeprom inside it and thus can't\n"
1791 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1792 "%s: workaround that. Redirect complaints to the vendor of\n"
1793 "%s: the TV card. Generic type will be used."
1794 "%s: Best regards,\n"
1795 "%s: -- tux\n",
1796 dev->name,dev->name,dev->name,dev->name,dev->name);
1797 em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1798 dev->name);
1799 for (i = 0; i < em28xx_bcount; i++) {
1800 em28xx_errdev(" card=%d -> %s\n", i,
1801 em28xx_boards[i].name);
1802 }
1803 }
1804 2106
1805 /* allocate device struct */ 2107 /* allocate device struct */
1806 retval = em28xx_init_dev(&dev, udev, nr, model); 2108 retval = em28xx_init_dev(&dev, udev, nr);
1807 if (retval) 2109 if (retval)
1808 return retval; 2110 return retval;
1809 2111
1810 em28xx_info("Found %s\n", em28xx_boards[model].name); 2112 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
1811 2113
1812 /* save our data pointer in this interface device */ 2114 /* save our data pointer in this interface device */
1813 usb_set_intfdata(interface, dev); 2115 usb_set_intfdata(interface, dev);
2116
2117 request_modules(dev);
2118
1814 return 0; 2119 return 0;
1815} 2120}
1816 2121
@@ -1821,18 +2126,20 @@ static int em28xx_usb_probe(struct usb_interface *interface,
1821 */ 2126 */
1822static void em28xx_usb_disconnect(struct usb_interface *interface) 2127static void em28xx_usb_disconnect(struct usb_interface *interface)
1823{ 2128{
1824 struct em28xx *dev = usb_get_intfdata(interface); 2129 struct em28xx *dev;
2130 struct em28xx_ops *ops = NULL;
2131
2132 dev = usb_get_intfdata(interface);
1825 usb_set_intfdata(interface, NULL); 2133 usb_set_intfdata(interface, NULL);
1826 2134
1827 if (!dev) 2135 if (!dev)
1828 return; 2136 return;
1829 2137
1830 down_write(&em28xx_disconnect); 2138 em28xx_info("disconnecting %s\n", dev->vdev->name);
1831 2139
2140 /* wait until all current v4l2 io is finished then deallocate resources */
1832 mutex_lock(&dev->lock); 2141 mutex_lock(&dev->lock);
1833 2142
1834 em28xx_info("disconnecting %s\n", dev->vdev->name);
1835
1836 wake_up_interruptible_all(&dev->open); 2143 wake_up_interruptible_all(&dev->open);
1837 2144
1838 if (dev->users) { 2145 if (dev->users) {
@@ -1850,15 +2157,20 @@ static void em28xx_usb_disconnect(struct usb_interface *interface)
1850 dev->state |= DEV_DISCONNECTED; 2157 dev->state |= DEV_DISCONNECTED;
1851 em28xx_release_resources(dev); 2158 em28xx_release_resources(dev);
1852 } 2159 }
1853
1854 mutex_unlock(&dev->lock); 2160 mutex_unlock(&dev->lock);
1855 2161
2162 mutex_lock(&em28xx_extension_devlist_lock);
2163 if (!list_empty(&em28xx_extension_devlist)) {
2164 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2165 ops->fini(dev);
2166 }
2167 }
2168 mutex_unlock(&em28xx_extension_devlist_lock);
2169
1856 if (!dev->users) { 2170 if (!dev->users) {
1857 kfree(dev->alt_max_pkt_size); 2171 kfree(dev->alt_max_pkt_size);
1858 kfree(dev); 2172 kfree(dev);
1859 } 2173 }
1860
1861 up_write(&em28xx_disconnect);
1862} 2174}
1863 2175
1864static struct usb_driver em28xx_usb_driver = { 2176static struct usb_driver em28xx_usb_driver = {
diff --git a/drivers/media/video/em28xx/em28xx.h b/drivers/media/video/em28xx/em28xx.h
index d8fcc9e17ac0..f3bad0c1c517 100644
--- a/drivers/media/video/em28xx/em28xx.h
+++ b/drivers/media/video/em28xx/em28xx.h
@@ -25,28 +25,11 @@
25#ifndef _EM28XX_H 25#ifndef _EM28XX_H
26#define _EM28XX_H 26#define _EM28XX_H
27 27
28#include <linux/videodev.h> 28#include <linux/videodev2.h>
29#include <linux/i2c.h> 29#include <linux/i2c.h>
30#include <linux/mutex.h> 30#include <linux/mutex.h>
31#include <media/ir-kbd-i2c.h> 31#include <media/ir-kbd-i2c.h>
32 32
33/* Boards supported by driver */
34
35#define EM2800_BOARD_UNKNOWN 0
36#define EM2820_BOARD_UNKNOWN 1
37#define EM2820_BOARD_TERRATEC_CINERGY_250 2
38#define EM2820_BOARD_PINNACLE_USB_2 3
39#define EM2820_BOARD_HAUPPAUGE_WINTV_USB_2 4
40#define EM2820_BOARD_MSI_VOX_USB_2 5
41#define EM2800_BOARD_TERRATEC_CINERGY_200 6
42#define EM2800_BOARD_LEADTEK_WINFAST_USBII 7
43#define EM2800_BOARD_KWORLD_USB2800 8
44#define EM2820_BOARD_PINNACLE_DVC_90 9
45#define EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900 10
46#define EM2880_BOARD_TERRATEC_HYBRID_XS 11
47#define EM2820_BOARD_KWORLD_PVRTV2800RF 12
48#define EM2880_BOARD_TERRATEC_PRODIGY_XS 13
49
50#define UNSET -1 33#define UNSET -1
51 34
52/* maximum number of em28xx boards */ 35/* maximum number of em28xx boards */
@@ -148,10 +131,17 @@ enum enum28xx_itype {
148 EM28XX_RADIO, 131 EM28XX_RADIO,
149}; 132};
150 133
134enum em28xx_amux {
135 EM28XX_AMUX_VIDEO,
136 EM28XX_AMUX_LINE_IN,
137 EM28XX_AMUX_AC97_VIDEO,
138 EM28XX_AMUX_AC97_LINE_IN,
139};
140
151struct em28xx_input { 141struct em28xx_input {
152 enum enum28xx_itype type; 142 enum enum28xx_itype type;
153 unsigned int vmux; 143 unsigned int vmux;
154 unsigned int amux; 144 enum em28xx_amux amux;
155}; 145};
156 146
157#define INPUT(nr) (&em28xx_boards[dev->model].input[nr]) 147#define INPUT(nr) (&em28xx_boards[dev->model].input[nr])
@@ -165,19 +155,23 @@ enum em28xx_decoder {
165struct em28xx_board { 155struct em28xx_board {
166 char *name; 156 char *name;
167 int vchannels; 157 int vchannels;
168 int norm;
169 int tuner_type; 158 int tuner_type;
170 159
171 /* i2c flags */ 160 /* i2c flags */
172 unsigned int is_em2800;
173 unsigned int tda9887_conf; 161 unsigned int tda9887_conf;
174 162
175 unsigned int has_tuner:1; 163 unsigned int is_em2800:1;
176 unsigned int has_msp34xx:1; 164 unsigned int has_msp34xx:1;
165 unsigned int mts_firmware:1;
166 unsigned int has_12mhz_i2s:1;
167 unsigned int max_range_640_480:1;
168
169 unsigned int analog_gpio;
177 170
178 enum em28xx_decoder decoder; 171 enum em28xx_decoder decoder;
179 172
180 struct em28xx_input input[MAX_EM28XX_INPUT]; 173 struct em28xx_input input[MAX_EM28XX_INPUT];
174 struct em28xx_input radio;
181}; 175};
182 176
183struct em28xx_eeprom { 177struct em28xx_eeprom {
@@ -201,12 +195,26 @@ enum em28xx_dev_state {
201 DEV_MISCONFIGURED = 0x04, 195 DEV_MISCONFIGURED = 0x04,
202}; 196};
203 197
204/* tvnorms */ 198#define EM28XX_AUDIO_BUFS 5
205struct em28xx_tvnorm { 199#define EM28XX_NUM_AUDIO_PACKETS 64
206 char *name; 200#define EM28XX_AUDIO_MAX_PACKET_SIZE 196 /* static value */
207 v4l2_std_id id; 201#define EM28XX_CAPTURE_STREAM_EN 1
208 /* mode for saa7113h */ 202#define EM28XX_AUDIO 0x10
209 int mode; 203
204struct em28xx_audio {
205 char name[50];
206 char *transfer_buffer[EM28XX_AUDIO_BUFS];
207 struct urb *urb[EM28XX_AUDIO_BUFS];
208 struct usb_device *udev;
209 unsigned int capture_transfer_done;
210 struct snd_pcm_substream *capture_pcm_substream;
211
212 unsigned int hwptr_done_capture;
213 struct snd_card *sndcard;
214
215 int users, shutdown;
216 enum em28xx_stream_state capture_stream;
217 spinlock_t slock;
210}; 218};
211 219
212/* main device struct */ 220/* main device struct */
@@ -215,12 +223,17 @@ struct em28xx {
215 char name[30]; /* name (including minor) of the device */ 223 char name[30]; /* name (including minor) of the device */
216 int model; /* index in the device_data struct */ 224 int model; /* index in the device_data struct */
217 int devno; /* marks the number of this device */ 225 int devno; /* marks the number of this device */
218 unsigned int is_em2800; 226 unsigned int analog_gpio;
219 int video_inputs; /* number of video inputs */ 227 unsigned int is_em2800:1;
220 struct list_head devlist;
221 unsigned int has_tuner:1;
222 unsigned int has_msp34xx:1; 228 unsigned int has_msp34xx:1;
223 unsigned int has_tda9887:1; 229 unsigned int has_tda9887:1;
230 unsigned int stream_on:1; /* Locks streams */
231 unsigned int has_audio_class:1;
232 unsigned int has_12mhz_i2s:1;
233 unsigned int max_range_640_480:1;
234
235 int video_inputs; /* number of video inputs */
236 struct list_head devlist;
224 237
225 u32 i2s_speed; /* I2S speed for audio digital stream */ 238 u32 i2s_speed; /* I2S speed for audio digital stream */
226 239
@@ -235,8 +248,7 @@ struct em28xx {
235 /* video for linux */ 248 /* video for linux */
236 int users; /* user count for exclusive use */ 249 int users; /* user count for exclusive use */
237 struct video_device *vdev; /* video for linux device struct */ 250 struct video_device *vdev; /* video for linux device struct */
238 struct video_picture vpic; /* picture settings only used to init saa7113h */ 251 v4l2_std_id norm; /* selected tv norm */
239 struct em28xx_tvnorm *tvnorm; /* selected tv norm */
240 int ctl_freq; /* selected frequency */ 252 int ctl_freq; /* selected frequency */
241 unsigned int ctl_input; /* selected input */ 253 unsigned int ctl_input; /* selected input */
242 unsigned int ctl_ainput; /* slected audio input */ 254 unsigned int ctl_ainput; /* slected audio input */
@@ -256,17 +268,27 @@ struct em28xx {
256 int vscale; /* vertical scale factor (see datasheet) */ 268 int vscale; /* vertical scale factor (see datasheet) */
257 int interlaced; /* 1=interlace fileds, 0=just top fileds */ 269 int interlaced; /* 1=interlace fileds, 0=just top fileds */
258 int type; 270 int type;
271 unsigned int video_bytesread; /* Number of bytes read */
272
273 unsigned long hash; /* eeprom hash - for boards with generic ID */
274 unsigned long i2c_hash; /* i2c devicelist hash - for boards with generic ID */
275
276 struct em28xx_audio *adev;
259 277
260 /* states */ 278 /* states */
261 enum em28xx_dev_state state; 279 enum em28xx_dev_state state;
262 enum em28xx_stream_state stream; 280 enum em28xx_stream_state stream;
263 enum em28xx_io_method io; 281 enum em28xx_io_method io;
282
283 struct work_struct request_module_wk;
284
264 /* locks */ 285 /* locks */
265 struct mutex lock, fileop_lock; 286 struct mutex lock;
266 spinlock_t queue_lock; 287 spinlock_t queue_lock;
267 struct list_head inqueue, outqueue; 288 struct list_head inqueue, outqueue;
268 wait_queue_head_t open, wait_frame, wait_stream; 289 wait_queue_head_t open, wait_frame, wait_stream;
269 struct video_device *vbi_dev; 290 struct video_device *vbi_dev;
291 struct video_device *radio_dev;
270 292
271 unsigned char eedata[256]; 293 unsigned char eedata[256];
272 294
@@ -289,16 +311,27 @@ struct em28xx {
289 int (*em28xx_read_reg_req) (struct em28xx * dev, u8 req, u16 reg); 311 int (*em28xx_read_reg_req) (struct em28xx * dev, u8 req, u16 reg);
290}; 312};
291 313
314struct em28xx_fh {
315 struct em28xx *dev;
316 unsigned int stream_on:1; /* Locks streams */
317 int radio;
318};
319
320struct em28xx_ops {
321 struct list_head next;
322 char *name;
323 int id;
324 int (*init)(struct em28xx *);
325 int (*fini)(struct em28xx *);
326};
327
292/* Provided by em28xx-i2c.c */ 328/* Provided by em28xx-i2c.c */
293 329
294void em28xx_i2c_call_clients(struct em28xx *dev, unsigned int cmd, void *arg); 330void em28xx_i2c_call_clients(struct em28xx *dev, unsigned int cmd, void *arg);
331void em28xx_do_i2c_scan(struct em28xx *dev);
295int em28xx_i2c_register(struct em28xx *dev); 332int em28xx_i2c_register(struct em28xx *dev);
296int em28xx_i2c_unregister(struct em28xx *dev); 333int em28xx_i2c_unregister(struct em28xx *dev);
297 334
298/* Provided by em28xx-input.c */
299
300void em28xx_set_ir(struct em28xx * dev,struct IR_i2c *ir);
301
302/* Provided by em28xx-core.c */ 335/* Provided by em28xx-core.c */
303 336
304u32 em28xx_request_buffers(struct em28xx *dev, u32 count); 337u32 em28xx_request_buffers(struct em28xx *dev, u32 count);
@@ -314,8 +347,9 @@ int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
314int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len); 347int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len);
315int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val, 348int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
316 u8 bitmask); 349 u8 bitmask);
317int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 * val); 350int em28xx_set_audio_source(struct em28xx *dev);
318int em28xx_audio_analog_set(struct em28xx *dev); 351int em28xx_audio_analog_set(struct em28xx *dev);
352
319int em28xx_colorlevels_set_default(struct em28xx *dev); 353int em28xx_colorlevels_set_default(struct em28xx *dev);
320int em28xx_capture_start(struct em28xx *dev, int start); 354int em28xx_capture_start(struct em28xx *dev, int start);
321int em28xx_outfmt_set_yuv422(struct em28xx *dev); 355int em28xx_outfmt_set_yuv422(struct em28xx *dev);
@@ -324,6 +358,10 @@ int em28xx_init_isoc(struct em28xx *dev);
324void em28xx_uninit_isoc(struct em28xx *dev); 358void em28xx_uninit_isoc(struct em28xx *dev);
325int em28xx_set_alternate(struct em28xx *dev); 359int em28xx_set_alternate(struct em28xx *dev);
326 360
361/* Provided by em28xx-video.c */
362int em28xx_register_extension(struct em28xx_ops *dev);
363void em28xx_unregister_extension(struct em28xx_ops *dev);
364
327/* Provided by em28xx-cards.c */ 365/* Provided by em28xx-cards.c */
328extern int em2800_variant_detect(struct usb_device* udev,int model); 366extern int em2800_variant_detect(struct usb_device* udev,int model);
329extern void em28xx_pre_card_setup(struct em28xx *dev); 367extern void em28xx_pre_card_setup(struct em28xx *dev);
@@ -331,8 +369,20 @@ extern void em28xx_card_setup(struct em28xx *dev);
331extern struct em28xx_board em28xx_boards[]; 369extern struct em28xx_board em28xx_boards[];
332extern struct usb_device_id em28xx_id_table[]; 370extern struct usb_device_id em28xx_id_table[];
333extern const unsigned int em28xx_bcount; 371extern const unsigned int em28xx_bcount;
372void em28xx_set_ir(struct em28xx *dev, struct IR_i2c *ir);
373
374/* Provided by em28xx-input.c */
375/* TODO: Check if the standard get_key handlers on ir-common can be used */
376int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw);
377int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw);
378int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
379 u32 *ir_raw);
380
381/* em2800 registers */
382#define EM2800_AUDIOSRC_REG 0x08
334 383
335/* em28xx registers */ 384/* em28xx registers */
385#define I2C_CLK_REG 0x06
336#define CHIPID_REG 0x0a 386#define CHIPID_REG 0x0a
337#define USBSUSP_REG 0x0c /* */ 387#define USBSUSP_REG 0x0c /* */
338 388
@@ -384,9 +434,12 @@ extern const unsigned int em28xx_bcount;
384 434
385/* em202 registers */ 435/* em202 registers */
386#define MASTER_AC97 0x02 436#define MASTER_AC97 0x02
437#define LINE_IN_AC97 0x10
387#define VIDEO_AC97 0x14 438#define VIDEO_AC97 0x14
388 439
389/* register settings */ 440/* register settings */
441#define EM2800_AUDIO_SRC_TUNER 0x0d
442#define EM2800_AUDIO_SRC_LINE 0x0c
390#define EM28XX_AUDIO_SRC_TUNER 0xc0 443#define EM28XX_AUDIO_SRC_TUNER 0xc0
391#define EM28XX_AUDIO_SRC_LINE 0x80 444#define EM28XX_AUDIO_SRC_LINE 0x80
392 445
@@ -406,22 +459,6 @@ extern const unsigned int em28xx_bcount;
406 printk(KERN_WARNING "%s: "fmt,\ 459 printk(KERN_WARNING "%s: "fmt,\
407 dev->name , ##arg); } while (0) 460 dev->name , ##arg); } while (0)
408 461
409inline static int em28xx_audio_source(struct em28xx *dev, int input)
410{
411 return em28xx_write_reg_bits(dev, AUDIOSRC_REG, input, 0xc0);
412}
413
414inline static int em28xx_audio_usb_mute(struct em28xx *dev, int mute)
415{
416 return em28xx_write_reg_bits(dev, XCLK_REG, mute ? 0x00 : 0x80, 0x80);
417}
418
419inline static int em28xx_audio_analog_setup(struct em28xx *dev)
420{
421 /* unmute video mixer with default volume level */
422 return em28xx_write_ac97(dev, VIDEO_AC97, "\x08\x08");
423}
424
425inline static int em28xx_compression_disable(struct em28xx *dev) 462inline static int em28xx_compression_disable(struct em28xx *dev)
426{ 463{
427 /* side effect of disabling scaler and mixer */ 464 /* side effect of disabling scaler and mixer */
@@ -497,18 +534,17 @@ inline static int em28xx_gamma_set(struct em28xx *dev, s32 val)
497/*FIXME: maxw should be dependent of alt mode */ 534/*FIXME: maxw should be dependent of alt mode */
498inline static unsigned int norm_maxw(struct em28xx *dev) 535inline static unsigned int norm_maxw(struct em28xx *dev)
499{ 536{
500 switch(dev->model){ 537 if (dev->max_range_640_480)
501 case (EM2820_BOARD_MSI_VOX_USB_2): return(640); 538 return 640;
502 default: return(720); 539 else
503 } 540 return 720;
504} 541}
505 542
506inline static unsigned int norm_maxh(struct em28xx *dev) 543inline static unsigned int norm_maxh(struct em28xx *dev)
507{ 544{
508 switch(dev->model){ 545 if (dev->max_range_640_480)
509 case (EM2820_BOARD_MSI_VOX_USB_2): return(480); 546 return 480;
510 default: return (dev->tvnorm->id & V4L2_STD_625_50) ? 576 : 480; 547 else
511 } 548 return (dev->norm & V4L2_STD_625_50) ? 576 : 480;
512} 549}
513
514#endif 550#endif
diff --git a/drivers/media/video/et61x251/et61x251_core.c b/drivers/media/video/et61x251/et61x251_core.c
index d19d73b81ede..06b6a3ae06c4 100644
--- a/drivers/media/video/et61x251/et61x251_core.c
+++ b/drivers/media/video/et61x251/et61x251_core.c
@@ -227,7 +227,7 @@ int et61x251_write_reg(struct et61x251_device* cam, u8 value, u16 index)
227} 227}
228 228
229 229
230int et61x251_read_reg(struct et61x251_device* cam, u16 index) 230static int et61x251_read_reg(struct et61x251_device* cam, u16 index)
231{ 231{
232 struct usb_device* udev = cam->usbdev; 232 struct usb_device* udev = cam->usbdev;
233 u8* buff = cam->control_buffer; 233 u8* buff = cam->control_buffer;
@@ -269,73 +269,6 @@ et61x251_i2c_wait(struct et61x251_device* cam,
269 269
270 270
271int 271int
272et61x251_i2c_try_read(struct et61x251_device* cam,
273 const struct et61x251_sensor* sensor, u8 address)
274{
275 struct usb_device* udev = cam->usbdev;
276 u8* data = cam->control_buffer;
277 int err = 0, res;
278
279 data[0] = address;
280 data[1] = cam->sensor.i2c_slave_id;
281 data[2] = cam->sensor.rsta | 0x10;
282 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
283 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
284 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
285 if (res < 0)
286 err += res;
287
288 err += et61x251_i2c_wait(cam, sensor);
289
290 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
291 0, 0x80, data, 8, ET61X251_CTRL_TIMEOUT);
292 if (res < 0)
293 err += res;
294
295 if (err)
296 DBG(3, "I2C read failed for %s image sensor", sensor->name);
297
298 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[0]);
299
300 return err ? -1 : (int)data[0];
301}
302
303
304int
305et61x251_i2c_try_write(struct et61x251_device* cam,
306 const struct et61x251_sensor* sensor, u8 address,
307 u8 value)
308{
309 struct usb_device* udev = cam->usbdev;
310 u8* data = cam->control_buffer;
311 int err = 0, res;
312
313 data[0] = address;
314 data[1] = cam->sensor.i2c_slave_id;
315 data[2] = cam->sensor.rsta | 0x12;
316 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
317 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
318 if (res < 0)
319 err += res;
320
321 data[0] = value;
322 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
323 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
324 if (res < 0)
325 err += res;
326
327 err += et61x251_i2c_wait(cam, sensor);
328
329 if (err)
330 DBG(3, "I2C write failed for %s image sensor", sensor->name);
331
332 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address, value);
333
334 return err ? -1 : 0;
335}
336
337
338int
339et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2, 272et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
340 u8 data3, u8 data4, u8 data5, u8 data6, u8 data7, 273 u8 data3, u8 data4, u8 data5, u8 data6, u8 data7,
341 u8 data8, u8 address) 274 u8 data8, u8 address)
@@ -387,17 +320,6 @@ et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
387} 320}
388 321
389 322
390int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
391{
392 return et61x251_i2c_try_read(cam, &cam->sensor, address);
393}
394
395
396int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value)
397{
398 return et61x251_i2c_try_write(cam, &cam->sensor, address, value);
399}
400
401/*****************************************************************************/ 323/*****************************************************************************/
402 324
403static void et61x251_urb_complete(struct urb *urb) 325static void et61x251_urb_complete(struct urb *urb)
@@ -675,6 +597,83 @@ static int et61x251_stream_interrupt(struct et61x251_device* cam)
675/*****************************************************************************/ 597/*****************************************************************************/
676 598
677#ifdef CONFIG_VIDEO_ADV_DEBUG 599#ifdef CONFIG_VIDEO_ADV_DEBUG
600
601static int et61x251_i2c_try_read(struct et61x251_device* cam,
602 const struct et61x251_sensor* sensor,
603 u8 address)
604{
605 struct usb_device* udev = cam->usbdev;
606 u8* data = cam->control_buffer;
607 int err = 0, res;
608
609 data[0] = address;
610 data[1] = cam->sensor.i2c_slave_id;
611 data[2] = cam->sensor.rsta | 0x10;
612 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
613 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
614 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
615 if (res < 0)
616 err += res;
617
618 err += et61x251_i2c_wait(cam, sensor);
619
620 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
621 0, 0x80, data, 8, ET61X251_CTRL_TIMEOUT);
622 if (res < 0)
623 err += res;
624
625 if (err)
626 DBG(3, "I2C read failed for %s image sensor", sensor->name);
627
628 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[0]);
629
630 return err ? -1 : (int)data[0];
631}
632
633
634static int et61x251_i2c_try_write(struct et61x251_device* cam,
635 const struct et61x251_sensor* sensor,
636 u8 address, u8 value)
637{
638 struct usb_device* udev = cam->usbdev;
639 u8* data = cam->control_buffer;
640 int err = 0, res;
641
642 data[0] = address;
643 data[1] = cam->sensor.i2c_slave_id;
644 data[2] = cam->sensor.rsta | 0x12;
645 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
646 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
647 if (res < 0)
648 err += res;
649
650 data[0] = value;
651 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
652 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
653 if (res < 0)
654 err += res;
655
656 err += et61x251_i2c_wait(cam, sensor);
657
658 if (err)
659 DBG(3, "I2C write failed for %s image sensor", sensor->name);
660
661 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address, value);
662
663 return err ? -1 : 0;
664}
665
666static int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
667{
668 return et61x251_i2c_try_read(cam, &cam->sensor, address);
669}
670
671static int et61x251_i2c_write(struct et61x251_device* cam,
672 u8 address, u8 value)
673{
674 return et61x251_i2c_try_write(cam, &cam->sensor, address, value);
675}
676
678static u8 et61x251_strtou8(const char* buff, size_t len, ssize_t* count) 677static u8 et61x251_strtou8(const char* buff, size_t len, ssize_t* count)
679{ 678{
680 char str[5]; 679 char str[5];
diff --git a/drivers/media/video/et61x251/et61x251_sensor.h b/drivers/media/video/et61x251/et61x251_sensor.h
index e14586330623..71a03148cb09 100644
--- a/drivers/media/video/et61x251/et61x251_sensor.h
+++ b/drivers/media/video/et61x251/et61x251_sensor.h
@@ -52,14 +52,6 @@ et61x251_attach_sensor(struct et61x251_device* cam,
52/*****************************************************************************/ 52/*****************************************************************************/
53 53
54extern int et61x251_write_reg(struct et61x251_device*, u8 value, u16 index); 54extern int et61x251_write_reg(struct et61x251_device*, u8 value, u16 index);
55extern int et61x251_read_reg(struct et61x251_device*, u16 index);
56extern int et61x251_i2c_write(struct et61x251_device*, u8 address, u8 value);
57extern int et61x251_i2c_read(struct et61x251_device*, u8 address);
58extern int et61x251_i2c_try_write(struct et61x251_device*,
59 const struct et61x251_sensor*, u8 address,
60 u8 value);
61extern int et61x251_i2c_try_read(struct et61x251_device*,
62 const struct et61x251_sensor*, u8 address);
63extern int et61x251_i2c_raw_write(struct et61x251_device*, u8 n, u8 data1, 55extern int et61x251_i2c_raw_write(struct et61x251_device*, u8 n, u8 data1,
64 u8 data2, u8 data3, u8 data4, u8 data5, 56 u8 data2, u8 data3, u8 data4, u8 data5,
65 u8 data6, u8 data7, u8 data8, u8 address); 57 u8 data6, u8 data7, u8 data8, u8 address);
diff --git a/drivers/media/video/ir-kbd-i2c.c b/drivers/media/video/ir-kbd-i2c.c
index 29779d8bf7fb..9851987b95fb 100644
--- a/drivers/media/video/ir-kbd-i2c.c
+++ b/drivers/media/video/ir-kbd-i2c.c
@@ -398,6 +398,7 @@ static int ir_attach(struct i2c_adapter *adap, int addr,
398 case 0x7a: 398 case 0x7a:
399 case 0x47: 399 case 0x47:
400 case 0x71: 400 case 0x71:
401 case 0x2d:
401 if (adap->id == I2C_HW_B_CX2388x) { 402 if (adap->id == I2C_HW_B_CX2388x) {
402 /* Handled by cx88-input */ 403 /* Handled by cx88-input */
403 name = "CX2388x remote"; 404 name = "CX2388x remote";
@@ -504,7 +505,7 @@ static int ir_probe(struct i2c_adapter *adap)
504 */ 505 */
505 506
506 static const int probe_bttv[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, -1}; 507 static const int probe_bttv[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, -1};
507 static const int probe_saa7134[] = { 0x7a, 0x47, 0x71, -1 }; 508 static const int probe_saa7134[] = { 0x7a, 0x47, 0x71, 0x2d, -1 };
508 static const int probe_em28XX[] = { 0x30, 0x47, -1 }; 509 static const int probe_em28XX[] = { 0x30, 0x47, -1 };
509 static const int probe_cx88[] = { 0x18, 0x6b, 0x71, -1 }; 510 static const int probe_cx88[] = { 0x18, 0x6b, 0x71, -1 };
510 static const int probe_cx23885[] = { 0x6b, -1 }; 511 static const int probe_cx23885[] = { 0x6b, -1 };
diff --git a/drivers/media/video/ivtv/Kconfig b/drivers/media/video/ivtv/Kconfig
index 854cc9c30ca9..270906fc3146 100644
--- a/drivers/media/video/ivtv/Kconfig
+++ b/drivers/media/video/ivtv/Kconfig
@@ -3,6 +3,7 @@ config VIDEO_IVTV
3 depends on VIDEO_V4L1 && VIDEO_V4L2 && PCI && I2C && EXPERIMENTAL 3 depends on VIDEO_V4L1 && VIDEO_V4L2 && PCI && I2C && EXPERIMENTAL
4 select I2C_ALGOBIT 4 select I2C_ALGOBIT
5 select FW_LOADER 5 select FW_LOADER
6 select VIDEO_IR
6 select VIDEO_TUNER 7 select VIDEO_TUNER
7 select VIDEO_TVEEPROM 8 select VIDEO_TVEEPROM
8 select VIDEO_CX2341X 9 select VIDEO_CX2341X
@@ -12,6 +13,7 @@ config VIDEO_IVTV
12 select VIDEO_SAA7127 13 select VIDEO_SAA7127
13 select VIDEO_TVAUDIO 14 select VIDEO_TVAUDIO
14 select VIDEO_CS53L32A 15 select VIDEO_CS53L32A
16 select VIDEO_M52790
15 select VIDEO_WM8775 17 select VIDEO_WM8775
16 select VIDEO_WM8739 18 select VIDEO_WM8739
17 select VIDEO_VP27SMPX 19 select VIDEO_VP27SMPX
diff --git a/drivers/media/video/ivtv/Makefile b/drivers/media/video/ivtv/Makefile
index e8eefd96d897..a0389014fa88 100644
--- a/drivers/media/video/ivtv/Makefile
+++ b/drivers/media/video/ivtv/Makefile
@@ -6,3 +6,8 @@ ivtv-objs := ivtv-routing.o ivtv-cards.o ivtv-controls.o \
6 6
7obj-$(CONFIG_VIDEO_IVTV) += ivtv.o 7obj-$(CONFIG_VIDEO_IVTV) += ivtv.o
8obj-$(CONFIG_VIDEO_FB_IVTV) += ivtvfb.o 8obj-$(CONFIG_VIDEO_FB_IVTV) += ivtvfb.o
9
10EXTRA_CFLAGS += -Idrivers/media/video
11EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core
12EXTRA_CFLAGS += -Idrivers/media/dvb/frontends
13
diff --git a/drivers/media/video/ivtv/ivtv-cards.c b/drivers/media/video/ivtv/ivtv-cards.c
index b6a8be622d3c..f23c6b8d6911 100644
--- a/drivers/media/video/ivtv/ivtv-cards.c
+++ b/drivers/media/video/ivtv/ivtv-cards.c
@@ -23,6 +23,7 @@
23#include "ivtv-i2c.h" 23#include "ivtv-i2c.h"
24 24
25#include <media/msp3400.h> 25#include <media/msp3400.h>
26#include <media/m52790.h>
26#include <media/wm8775.h> 27#include <media/wm8775.h>
27#include <media/cs53l32a.h> 28#include <media/cs53l32a.h>
28#include <media/cx25840.h> 29#include <media/cx25840.h>
@@ -39,6 +40,27 @@
39#define MSP_MONO MSP_INPUT(MSP_IN_MONO, MSP_IN_TUNER1, \ 40#define MSP_MONO MSP_INPUT(MSP_IN_MONO, MSP_IN_TUNER1, \
40 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART) 41 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART)
41 42
43/* usual i2c tuner addresses to probe */
44static struct ivtv_card_tuner_i2c ivtv_i2c_std = {
45 .radio = { I2C_CLIENT_END },
46 .demod = { 0x43, I2C_CLIENT_END },
47 .tv = { 0x61, 0x60, I2C_CLIENT_END },
48};
49
50/* as above, but with possible radio tuner */
51static struct ivtv_card_tuner_i2c ivtv_i2c_radio = {
52 .radio = { 0x60, I2C_CLIENT_END },
53 .demod = { 0x43, I2C_CLIENT_END },
54 .tv = { 0x61, I2C_CLIENT_END },
55};
56
57/* using the tda8290+75a combo */
58static struct ivtv_card_tuner_i2c ivtv_i2c_tda8290 = {
59 .radio = { I2C_CLIENT_END },
60 .demod = { I2C_CLIENT_END },
61 .tv = { 0x4b, I2C_CLIENT_END },
62};
63
42/********************** card configuration *******************************/ 64/********************** card configuration *******************************/
43 65
44/* Please add new PCI IDs to: http://pci-ids.ucw.cz/iii 66/* Please add new PCI IDs to: http://pci-ids.ucw.cz/iii
@@ -72,6 +94,7 @@ static const struct ivtv_card ivtv_card_pvr250 = {
72 { IVTV_CARD_INPUT_LINE_IN2, MSP_SCART3 }, 94 { IVTV_CARD_INPUT_LINE_IN2, MSP_SCART3 },
73 }, 95 },
74 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, MSP_SCART2 }, 96 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, MSP_SCART2 },
97 .i2c = &ivtv_i2c_std,
75}; 98};
76 99
77/* ------------------------------------------------------------------------- */ 100/* ------------------------------------------------------------------------- */
@@ -126,6 +149,7 @@ static const struct ivtv_card ivtv_card_pvr350 = {
126 { IVTV_CARD_INPUT_LINE_IN2, MSP_SCART3 }, 149 { IVTV_CARD_INPUT_LINE_IN2, MSP_SCART3 },
127 }, 150 },
128 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, MSP_SCART2 }, 151 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, MSP_SCART2 },
152 .i2c = &ivtv_i2c_std,
129}; 153};
130 154
131/* PVR-350 V1 boards have a different audio tuner input and use a 155/* PVR-350 V1 boards have a different audio tuner input and use a
@@ -157,6 +181,7 @@ static const struct ivtv_card ivtv_card_pvr350_v1 = {
157 { IVTV_CARD_INPUT_LINE_IN2, MSP_SCART3 }, 181 { IVTV_CARD_INPUT_LINE_IN2, MSP_SCART3 },
158 }, 182 },
159 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, MSP_SCART2 }, 183 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, MSP_SCART2 },
184 .i2c = &ivtv_i2c_std,
160}; 185};
161 186
162/* ------------------------------------------------------------------------- */ 187/* ------------------------------------------------------------------------- */
@@ -192,6 +217,7 @@ static const struct ivtv_card ivtv_card_pvr150 = {
192 CX25840_AUDIO_SERIAL, WM8775_AIN4 }, 217 CX25840_AUDIO_SERIAL, WM8775_AIN4 },
193 /* apparently needed for the IR blaster */ 218 /* apparently needed for the IR blaster */
194 .gpio_init = { .direction = 0x1f01, .initial_value = 0x26f3 }, 219 .gpio_init = { .direction = 0x1f01, .initial_value = 0x26f3 },
220 .i2c = &ivtv_i2c_std,
195}; 221};
196 222
197/* ------------------------------------------------------------------------- */ 223/* ------------------------------------------------------------------------- */
@@ -234,6 +260,7 @@ static const struct ivtv_card ivtv_card_m179 = {
234 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_NTSC }, 260 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_NTSC },
235 }, 261 },
236 .pci_list = ivtv_pci_m179, 262 .pci_list = ivtv_pci_m179,
263 .i2c = &ivtv_i2c_std,
237}; 264};
238 265
239/* ------------------------------------------------------------------------- */ 266/* ------------------------------------------------------------------------- */
@@ -275,6 +302,7 @@ static const struct ivtv_card ivtv_card_mpg600 = {
275 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FQ1286 }, 302 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FQ1286 },
276 }, 303 },
277 .pci_list = ivtv_pci_mpg600, 304 .pci_list = ivtv_pci_mpg600,
305 .i2c = &ivtv_i2c_std,
278}; 306};
279 307
280/* ------------------------------------------------------------------------- */ 308/* ------------------------------------------------------------------------- */
@@ -315,6 +343,7 @@ static const struct ivtv_card ivtv_card_mpg160 = {
315 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FQ1286 }, 343 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FQ1286 },
316 }, 344 },
317 .pci_list = ivtv_pci_mpg160, 345 .pci_list = ivtv_pci_mpg160,
346 .i2c = &ivtv_i2c_std,
318}; 347};
319 348
320/* ------------------------------------------------------------------------- */ 349/* ------------------------------------------------------------------------- */
@@ -350,6 +379,7 @@ static const struct ivtv_card ivtv_card_pg600 = {
350 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FQ1286 }, 379 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FQ1286 },
351 }, 380 },
352 .pci_list = ivtv_pci_pg600, 381 .pci_list = ivtv_pci_pg600,
382 .i2c = &ivtv_i2c_std,
353}; 383};
354 384
355/* ------------------------------------------------------------------------- */ 385/* ------------------------------------------------------------------------- */
@@ -393,6 +423,7 @@ static const struct ivtv_card ivtv_card_avc2410 = {
393 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 }, 423 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 },
394 }, 424 },
395 .pci_list = ivtv_pci_avc2410, 425 .pci_list = ivtv_pci_avc2410,
426 .i2c = &ivtv_i2c_std,
396}; 427};
397 428
398/* ------------------------------------------------------------------------- */ 429/* ------------------------------------------------------------------------- */
@@ -463,6 +494,7 @@ static const struct ivtv_card ivtv_card_tg5000tv = {
463 { .std = V4L2_STD_525_60, .tuner = TUNER_PHILIPS_FQ1286 }, 494 { .std = V4L2_STD_525_60, .tuner = TUNER_PHILIPS_FQ1286 },
464 }, 495 },
465 .pci_list = ivtv_pci_tg5000tv, 496 .pci_list = ivtv_pci_tg5000tv,
497 .i2c = &ivtv_i2c_std,
466}; 498};
467 499
468/* ------------------------------------------------------------------------- */ 500/* ------------------------------------------------------------------------- */
@@ -493,6 +525,7 @@ static const struct ivtv_card ivtv_card_va2000 = {
493 { .std = V4L2_STD_525_60, .tuner = TUNER_PHILIPS_FQ1286 }, 525 { .std = V4L2_STD_525_60, .tuner = TUNER_PHILIPS_FQ1286 },
494 }, 526 },
495 .pci_list = ivtv_pci_va2000, 527 .pci_list = ivtv_pci_va2000,
528 .i2c = &ivtv_i2c_std,
496}; 529};
497 530
498/* ------------------------------------------------------------------------- */ 531/* ------------------------------------------------------------------------- */
@@ -537,6 +570,7 @@ static const struct ivtv_card ivtv_card_cx23416gyc = {
537 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 }, 570 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 },
538 }, 571 },
539 .pci_list = ivtv_pci_cx23416gyc, 572 .pci_list = ivtv_pci_cx23416gyc,
573 .i2c = &ivtv_i2c_std,
540}; 574};
541 575
542static const struct ivtv_card ivtv_card_cx23416gyc_nogr = { 576static const struct ivtv_card ivtv_card_cx23416gyc_nogr = {
@@ -567,6 +601,7 @@ static const struct ivtv_card ivtv_card_cx23416gyc_nogr = {
567 { .std = V4L2_STD_625_50, .tuner = TUNER_PHILIPS_FM1216ME_MK3 }, 601 { .std = V4L2_STD_625_50, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
568 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 }, 602 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 },
569 }, 603 },
604 .i2c = &ivtv_i2c_std,
570}; 605};
571 606
572static const struct ivtv_card ivtv_card_cx23416gyc_nogrycs = { 607static const struct ivtv_card ivtv_card_cx23416gyc_nogrycs = {
@@ -596,6 +631,7 @@ static const struct ivtv_card ivtv_card_cx23416gyc_nogrycs = {
596 { .std = V4L2_STD_625_50, .tuner = TUNER_PHILIPS_FM1216ME_MK3 }, 631 { .std = V4L2_STD_625_50, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
597 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 }, 632 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 },
598 }, 633 },
634 .i2c = &ivtv_i2c_std,
599}; 635};
600 636
601/* ------------------------------------------------------------------------- */ 637/* ------------------------------------------------------------------------- */
@@ -635,6 +671,7 @@ static const struct ivtv_card ivtv_card_gv_mvprx = {
635 { .std = V4L2_STD_525_60, .tuner = TUNER_PANASONIC_VP27 }, 671 { .std = V4L2_STD_525_60, .tuner = TUNER_PANASONIC_VP27 },
636 }, 672 },
637 .pci_list = ivtv_pci_gv_mvprx, 673 .pci_list = ivtv_pci_gv_mvprx,
674 .i2c = &ivtv_i2c_std,
638}; 675};
639 676
640/* ------------------------------------------------------------------------- */ 677/* ------------------------------------------------------------------------- */
@@ -671,6 +708,7 @@ static const struct ivtv_card ivtv_card_gv_mvprx2e = {
671 { .std = V4L2_STD_525_60, .tuner = TUNER_PANASONIC_VP27 }, 708 { .std = V4L2_STD_525_60, .tuner = TUNER_PANASONIC_VP27 },
672 }, 709 },
673 .pci_list = ivtv_pci_gv_mvprx2e, 710 .pci_list = ivtv_pci_gv_mvprx2e,
711 .i2c = &ivtv_i2c_std,
674}; 712};
675 713
676/* ------------------------------------------------------------------------- */ 714/* ------------------------------------------------------------------------- */
@@ -705,6 +743,7 @@ static const struct ivtv_card ivtv_card_gotview_pci_dvd = {
705 { .std = V4L2_STD_625_50, .tuner = TUNER_PHILIPS_FM1216ME_MK3 }, 743 { .std = V4L2_STD_625_50, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
706 }, 744 },
707 .pci_list = ivtv_pci_gotview_pci_dvd, 745 .pci_list = ivtv_pci_gotview_pci_dvd,
746 .i2c = &ivtv_i2c_std,
708}; 747};
709 748
710/* ------------------------------------------------------------------------- */ 749/* ------------------------------------------------------------------------- */
@@ -743,6 +782,7 @@ static const struct ivtv_card ivtv_card_gotview_pci_dvd2 = {
743 { .std = V4L2_STD_625_50, .tuner = TUNER_PHILIPS_FM1216ME_MK3 }, 782 { .std = V4L2_STD_625_50, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
744 }, 783 },
745 .pci_list = ivtv_pci_gotview_pci_dvd2, 784 .pci_list = ivtv_pci_gotview_pci_dvd2,
785 .i2c = &ivtv_i2c_std,
746}; 786};
747 787
748/* ------------------------------------------------------------------------- */ 788/* ------------------------------------------------------------------------- */
@@ -778,6 +818,7 @@ static const struct ivtv_card ivtv_card_yuan_mpc622 = {
778 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_TDA8290 }, 818 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_TDA8290 },
779 }, 819 },
780 .pci_list = ivtv_pci_yuan_mpc622, 820 .pci_list = ivtv_pci_yuan_mpc622,
821 .i2c = &ivtv_i2c_tda8290,
781}; 822};
782 823
783/* ------------------------------------------------------------------------- */ 824/* ------------------------------------------------------------------------- */
@@ -819,6 +860,7 @@ static const struct ivtv_card ivtv_card_dctmvtvp1 = {
819 { .std = V4L2_STD_525_60, .tuner = TUNER_PHILIPS_FQ1286 }, 860 { .std = V4L2_STD_525_60, .tuner = TUNER_PHILIPS_FQ1286 },
820 }, 861 },
821 .pci_list = ivtv_pci_dctmvtvp1, 862 .pci_list = ivtv_pci_dctmvtvp1,
863 .i2c = &ivtv_i2c_std,
822}; 864};
823 865
824/* ------------------------------------------------------------------------- */ 866/* ------------------------------------------------------------------------- */
@@ -838,7 +880,7 @@ static const struct ivtv_card ivtv_card_pg600v2 = {
838 .hw_video = IVTV_HW_CX25840, 880 .hw_video = IVTV_HW_CX25840,
839 .hw_audio = IVTV_HW_CX25840, 881 .hw_audio = IVTV_HW_CX25840,
840 .hw_audio_ctrl = IVTV_HW_CX25840, 882 .hw_audio_ctrl = IVTV_HW_CX25840,
841 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER, 883 .hw_all = IVTV_HW_CX25840,
842 .video_inputs = { 884 .video_inputs = {
843 { IVTV_CARD_INPUT_SVIDEO1, 0, 885 { IVTV_CARD_INPUT_SVIDEO1, 0,
844 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 }, 886 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 },
@@ -847,10 +889,8 @@ static const struct ivtv_card ivtv_card_pg600v2 = {
847 .audio_inputs = { 889 .audio_inputs = {
848 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL }, 890 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL },
849 }, 891 },
850 .tuners = {
851 { .std = V4L2_STD_ALL, .tuner = TUNER_XCEIVE_XC3028 },
852 },
853 .pci_list = ivtv_pci_pg600v2, 892 .pci_list = ivtv_pci_pg600v2,
893 .i2c = &ivtv_i2c_std,
854}; 894};
855 895
856/* ------------------------------------------------------------------------- */ 896/* ------------------------------------------------------------------------- */
@@ -871,17 +911,22 @@ static const struct ivtv_card ivtv_card_club3d = {
871 .hw_audio_ctrl = IVTV_HW_CX25840, 911 .hw_audio_ctrl = IVTV_HW_CX25840,
872 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER, 912 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER,
873 .video_inputs = { 913 .video_inputs = {
874 { IVTV_CARD_INPUT_SVIDEO1, 0, 914 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
915 { IVTV_CARD_INPUT_SVIDEO1, 1,
875 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 }, 916 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 },
876 { IVTV_CARD_INPUT_COMPOSITE1, 0, CX25840_COMPOSITE3 }, 917 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE3 },
877 }, 918 },
878 .audio_inputs = { 919 .audio_inputs = {
920 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
879 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL }, 921 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL },
880 }, 922 },
923 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
924 .gpio_init = { .direction = 0x1000, .initial_value = 0x1000 }, /* tuner reset */
881 .tuners = { 925 .tuners = {
882 { .std = V4L2_STD_ALL, .tuner = TUNER_XCEIVE_XC3028 }, 926 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
883 }, 927 },
884 .pci_list = ivtv_pci_club3d, 928 .pci_list = ivtv_pci_club3d,
929 .i2c = &ivtv_i2c_std,
885}; 930};
886 931
887/* ------------------------------------------------------------------------- */ 932/* ------------------------------------------------------------------------- */
@@ -900,7 +945,7 @@ static const struct ivtv_card ivtv_card_avertv_mce116 = {
900 .hw_video = IVTV_HW_CX25840, 945 .hw_video = IVTV_HW_CX25840,
901 .hw_audio = IVTV_HW_CX25840, 946 .hw_audio = IVTV_HW_CX25840,
902 .hw_audio_ctrl = IVTV_HW_CX25840, 947 .hw_audio_ctrl = IVTV_HW_CX25840,
903 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER | IVTV_HW_WM8739, 948 .hw_all = IVTV_HW_CX25840 | IVTV_HW_WM8739,
904 .video_inputs = { 949 .video_inputs = {
905 { IVTV_CARD_INPUT_SVIDEO1, 0, CX25840_SVIDEO3 }, 950 { IVTV_CARD_INPUT_SVIDEO1, 0, CX25840_SVIDEO3 },
906 { IVTV_CARD_INPUT_COMPOSITE1, 0, CX25840_COMPOSITE1 }, 951 { IVTV_CARD_INPUT_COMPOSITE1, 0, CX25840_COMPOSITE1 },
@@ -909,10 +954,115 @@ static const struct ivtv_card ivtv_card_avertv_mce116 = {
909 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, 1 }, 954 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, 1 },
910 }, 955 },
911 .gpio_init = { .direction = 0xe000, .initial_value = 0x4000 }, /* enable line-in */ 956 .gpio_init = { .direction = 0xe000, .initial_value = 0x4000 }, /* enable line-in */
957 .pci_list = ivtv_pci_avertv_mce116,
958 .i2c = &ivtv_i2c_std,
959};
960
961/* ------------------------------------------------------------------------- */
962
963/* AVerMedia PVR-150 Plus (M113) card */
964
965static const struct ivtv_card_pci_info ivtv_pci_aver_pvr150[] = {
966 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xc035 },
967 { 0, 0, 0 }
968};
969
970static const struct ivtv_card ivtv_card_aver_pvr150 = {
971 .type = IVTV_CARD_AVER_PVR150PLUS,
972 .name = "AVerMedia PVR-150 Plus",
973 .v4l2_capabilities = IVTV_CAP_ENCODER,
974 .hw_video = IVTV_HW_CX25840,
975 .hw_audio = IVTV_HW_CX25840,
976 .hw_audio_ctrl = IVTV_HW_CX25840,
977 .hw_muxer = IVTV_HW_GPIO,
978 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER,
979 .video_inputs = {
980 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
981 { IVTV_CARD_INPUT_SVIDEO1, 1,
982 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 },
983 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE1 },
984 },
985 .audio_inputs = {
986 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5, 0 },
987 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, 1 },
988 },
989 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO_SERIAL, 2 },
990 .gpio_init = { .direction = 0x0800, .initial_value = 0 },
991 .gpio_audio_input = { .mask = 0x0800, .tuner = 0, .linein = 0, .radio = 0x0800 },
912 .tuners = { 992 .tuners = {
913 { .std = V4L2_STD_ALL, .tuner = TUNER_XCEIVE_XC3028 }, 993 /* This card has a Partsnic PTI-5NF05 tuner */
994 { .std = V4L2_STD_525_60, .tuner = TUNER_TCL_2002N },
914 }, 995 },
915 .pci_list = ivtv_pci_avertv_mce116, 996 .pci_list = ivtv_pci_aver_pvr150,
997 .i2c = &ivtv_i2c_radio,
998};
999
1000/* ------------------------------------------------------------------------- */
1001
1002/* AVerMedia EZMaker PCI Deluxe card */
1003
1004static const struct ivtv_card_pci_info ivtv_pci_aver_ezmaker[] = {
1005 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xc03f },
1006 { 0, 0, 0 }
1007};
1008
1009static const struct ivtv_card ivtv_card_aver_ezmaker = {
1010 .type = IVTV_CARD_AVER_EZMAKER,
1011 .name = "AVerMedia EZMaker PCI Deluxe",
1012 .v4l2_capabilities = IVTV_CAP_ENCODER,
1013 .hw_video = IVTV_HW_CX25840,
1014 .hw_audio = IVTV_HW_CX25840,
1015 .hw_audio_ctrl = IVTV_HW_CX25840,
1016 .hw_all = IVTV_HW_CX25840 | IVTV_HW_WM8739,
1017 .video_inputs = {
1018 { IVTV_CARD_INPUT_SVIDEO1, 0, CX25840_SVIDEO3 },
1019 { IVTV_CARD_INPUT_COMPOSITE1, 0, CX25840_COMPOSITE1 },
1020 },
1021 .audio_inputs = {
1022 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, 0 },
1023 },
1024 .gpio_init = { .direction = 0x4000, .initial_value = 0x4000 },
1025 /* Does not have a tuner */
1026 .pci_list = ivtv_pci_aver_ezmaker,
1027};
1028
1029/* ------------------------------------------------------------------------- */
1030
1031/* ASUS Falcon2 */
1032
1033static const struct ivtv_card_pci_info ivtv_pci_asus_falcon2[] = {
1034 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_ASUSTEK, 0x4b66 },
1035 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_ASUSTEK, 0x462e },
1036 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_ASUSTEK, 0x4b2e },
1037 { 0, 0, 0 }
1038};
1039
1040static const struct ivtv_card ivtv_card_asus_falcon2 = {
1041 .type = IVTV_CARD_ASUS_FALCON2,
1042 .name = "ASUS Falcon2",
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_M52790,
1048 .hw_all = IVTV_HW_CX25840 | IVTV_HW_M52790 | IVTV_HW_TUNER,
1049 .video_inputs = {
1050 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
1051 { IVTV_CARD_INPUT_SVIDEO1, 1, CX25840_SVIDEO3 },
1052 { IVTV_CARD_INPUT_COMPOSITE1, 2, CX25840_COMPOSITE2 },
1053 },
1054 .audio_inputs = {
1055 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5, M52790_IN_TUNER },
1056 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL,
1057 M52790_IN_V2 | M52790_SW1_YCMIX | M52790_SW2_YCMIX },
1058 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, M52790_IN_V2 },
1059 },
1060 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO_SERIAL, M52790_IN_TUNER },
1061 .tuners = {
1062 { .std = V4L2_STD_525_60, .tuner = TUNER_PHILIPS_FM1236_MK3 },
1063 },
1064 .pci_list = ivtv_pci_asus_falcon2,
1065 .i2c = &ivtv_i2c_std,
916}; 1066};
917 1067
918static const struct ivtv_card *ivtv_card_list[] = { 1068static const struct ivtv_card *ivtv_card_list[] = {
@@ -937,6 +1087,9 @@ static const struct ivtv_card *ivtv_card_list[] = {
937 &ivtv_card_pg600v2, 1087 &ivtv_card_pg600v2,
938 &ivtv_card_club3d, 1088 &ivtv_card_club3d,
939 &ivtv_card_avertv_mce116, 1089 &ivtv_card_avertv_mce116,
1090 &ivtv_card_asus_falcon2,
1091 &ivtv_card_aver_pvr150,
1092 &ivtv_card_aver_ezmaker,
940 1093
941 /* Variations of standard cards but with the same PCI IDs. 1094 /* Variations of standard cards but with the same PCI IDs.
942 These cards must come last in this list. */ 1095 These cards must come last in this list. */
diff --git a/drivers/media/video/ivtv/ivtv-cards.h b/drivers/media/video/ivtv/ivtv-cards.h
index ff46e5ae8653..191aafdd9968 100644
--- a/drivers/media/video/ivtv/ivtv-cards.h
+++ b/drivers/media/video/ivtv/ivtv-cards.h
@@ -45,7 +45,10 @@
45#define IVTV_CARD_PG600V2 18 /* Yuan PG600V2/GotView PCI DVD Lite */ 45#define IVTV_CARD_PG600V2 18 /* Yuan PG600V2/GotView PCI DVD Lite */
46#define IVTV_CARD_CLUB3D 19 /* Club3D ZAP-TV1x01 */ 46#define IVTV_CARD_CLUB3D 19 /* Club3D ZAP-TV1x01 */
47#define IVTV_CARD_AVERTV_MCE116 20 /* AVerTV MCE 116 Plus */ 47#define IVTV_CARD_AVERTV_MCE116 20 /* AVerTV MCE 116 Plus */
48#define IVTV_CARD_LAST 20 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_LAST 23
49 52
50/* Variants of existing cards but with the same PCI IDs. The driver 53/* Variants of existing cards but with the same PCI IDs. The driver
51 detects these based on other device information. 54 detects these based on other device information.
@@ -69,6 +72,7 @@
69#define IVTV_PCI_ID_HAUPPAUGE_ALT1 0x0270 72#define IVTV_PCI_ID_HAUPPAUGE_ALT1 0x0270
70#define IVTV_PCI_ID_HAUPPAUGE_ALT2 0x4070 73#define IVTV_PCI_ID_HAUPPAUGE_ALT2 0x4070
71#define IVTV_PCI_ID_ADAPTEC 0x9005 74#define IVTV_PCI_ID_ADAPTEC 0x9005
75#define IVTV_PCI_ID_ASUSTEK 0x1043
72#define IVTV_PCI_ID_AVERMEDIA 0x1461 76#define IVTV_PCI_ID_AVERMEDIA 0x1461
73#define IVTV_PCI_ID_YUAN1 0x12ab 77#define IVTV_PCI_ID_YUAN1 0x12ab
74#define IVTV_PCI_ID_YUAN2 0xff01 78#define IVTV_PCI_ID_YUAN2 0xff01
@@ -80,7 +84,7 @@
80#define IVTV_PCI_ID_GOTVIEW1 0xffac 84#define IVTV_PCI_ID_GOTVIEW1 0xffac
81#define IVTV_PCI_ID_GOTVIEW2 0xffad 85#define IVTV_PCI_ID_GOTVIEW2 0xffad
82 86
83/* hardware flags */ 87/* hardware flags, no gaps allowed, IVTV_HW_GPIO must always be last */
84#define IVTV_HW_CX25840 (1 << 0) 88#define IVTV_HW_CX25840 (1 << 0)
85#define IVTV_HW_SAA7115 (1 << 1) 89#define IVTV_HW_SAA7115 (1 << 1)
86#define IVTV_HW_SAA7127 (1 << 2) 90#define IVTV_HW_SAA7127 (1 << 2)
@@ -90,12 +94,12 @@
90#define IVTV_HW_CS53L32A (1 << 6) 94#define IVTV_HW_CS53L32A (1 << 6)
91#define IVTV_HW_TVEEPROM (1 << 7) 95#define IVTV_HW_TVEEPROM (1 << 7)
92#define IVTV_HW_SAA7114 (1 << 8) 96#define IVTV_HW_SAA7114 (1 << 8)
93#define IVTV_HW_TVAUDIO (1 << 9) 97#define IVTV_HW_UPD64031A (1 << 9)
94#define IVTV_HW_UPD64031A (1 << 10) 98#define IVTV_HW_UPD6408X (1 << 10)
95#define IVTV_HW_UPD6408X (1 << 11) 99#define IVTV_HW_SAA717X (1 << 11)
96#define IVTV_HW_SAA717X (1 << 12) 100#define IVTV_HW_WM8739 (1 << 12)
97#define IVTV_HW_WM8739 (1 << 13) 101#define IVTV_HW_VP27SMPX (1 << 13)
98#define IVTV_HW_VP27SMPX (1 << 14) 102#define IVTV_HW_M52790 (1 << 14)
99#define IVTV_HW_GPIO (1 << 15) 103#define IVTV_HW_GPIO (1 << 15)
100 104
101#define IVTV_HW_SAA711X (IVTV_HW_SAA7115 | IVTV_HW_SAA7114) 105#define IVTV_HW_SAA711X (IVTV_HW_SAA7115 | IVTV_HW_SAA7114)
@@ -230,6 +234,12 @@ struct ivtv_card_tuner {
230 int tuner; /* tuner ID (from tuner.h) */ 234 int tuner; /* tuner ID (from tuner.h) */
231}; 235};
232 236
237struct ivtv_card_tuner_i2c {
238 unsigned short radio[2];/* radio tuner i2c address to probe */
239 unsigned short demod[2];/* demodulator i2c address to probe */
240 unsigned short tv[4]; /* tv tuner i2c addresses to probe */
241};
242
233/* for card information/parameters */ 243/* for card information/parameters */
234struct ivtv_card { 244struct ivtv_card {
235 int type; 245 int type;
@@ -257,6 +267,7 @@ struct ivtv_card {
257 struct ivtv_gpio_audio_detect gpio_audio_detect; 267 struct ivtv_gpio_audio_detect gpio_audio_detect;
258 268
259 struct ivtv_card_tuner tuners[IVTV_CARD_MAX_TUNERS]; 269 struct ivtv_card_tuner tuners[IVTV_CARD_MAX_TUNERS];
270 struct ivtv_card_tuner_i2c *i2c;
260 271
261 /* list of device and subsystem vendor/devices that 272 /* list of device and subsystem vendor/devices that
262 correspond to this card type. */ 273 correspond to this card type. */
diff --git a/drivers/media/video/ivtv/ivtv-driver.c b/drivers/media/video/ivtv/ivtv-driver.c
index 6d2dd8764f81..d42f120354e5 100644
--- a/drivers/media/video/ivtv/ivtv-driver.c
+++ b/drivers/media/video/ivtv/ivtv-driver.c
@@ -59,6 +59,7 @@
59#include <media/tveeprom.h> 59#include <media/tveeprom.h>
60#include <media/saa7115.h> 60#include <media/saa7115.h>
61#include <media/v4l2-chip-ident.h> 61#include <media/v4l2-chip-ident.h>
62#include "tuner-xc2028.h"
62 63
63/* var to keep track of the number of array elements in use */ 64/* var to keep track of the number of array elements in use */
64int ivtv_cards_active = 0; 65int ivtv_cards_active = 0;
@@ -185,6 +186,9 @@ MODULE_PARM_DESC(cardtype,
185 "\t\t\t19 = Yuan PG600V2/GotView PCI DVD Lite\n" 186 "\t\t\t19 = Yuan PG600V2/GotView PCI DVD Lite\n"
186 "\t\t\t20 = Club3D ZAP-TV1x01\n" 187 "\t\t\t20 = Club3D ZAP-TV1x01\n"
187 "\t\t\t21 = AverTV MCE 116 Plus\n" 188 "\t\t\t21 = AverTV MCE 116 Plus\n"
189 "\t\t\t22 = ASUS Falcon2\n"
190 "\t\t\t23 = AverMedia PVR-150 Plus\n"
191 "\t\t\t24 = AverMedia EZMaker PCI Deluxe\n"
188 "\t\t\t 0 = Autodetect (default)\n" 192 "\t\t\t 0 = Autodetect (default)\n"
189 "\t\t\t-1 = Ignore this card\n\t\t"); 193 "\t\t\t-1 = Ignore this card\n\t\t");
190MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60"); 194MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
@@ -397,6 +401,7 @@ static void ivtv_process_eeprom(struct ivtv *itv)
397 401
398 itv->v4l2_cap = itv->card->v4l2_capabilities; 402 itv->v4l2_cap = itv->card->v4l2_capabilities;
399 itv->card_name = itv->card->name; 403 itv->card_name = itv->card->name;
404 itv->card_i2c = itv->card->i2c;
400 405
401 /* If this is a PVR500 then it should be possible to detect whether it is the 406 /* If this is a PVR500 then it should be possible to detect whether it is the
402 first or second unit by looking at the subsystem device ID: is bit 4 is 407 first or second unit by looking at the subsystem device ID: is bit 4 is
@@ -414,7 +419,14 @@ static void ivtv_process_eeprom(struct ivtv *itv)
414 This detection is needed since the eeprom reports incorrectly that a radio is 419 This detection is needed since the eeprom reports incorrectly that a radio is
415 present on the second unit. */ 420 present on the second unit. */
416 if (tv.model / 1000 == 23) { 421 if (tv.model / 1000 == 23) {
422 static const struct ivtv_card_tuner_i2c ivtv_i2c_radio = {
423 .radio = { 0x60, I2C_CLIENT_END },
424 .demod = { 0x43, I2C_CLIENT_END },
425 .tv = { 0x61, I2C_CLIENT_END },
426 };
427
417 itv->card_name = "WinTV PVR 500"; 428 itv->card_name = "WinTV PVR 500";
429 itv->card_i2c = &ivtv_i2c_radio;
418 if (pci_slot == 8 || pci_slot == 9) { 430 if (pci_slot == 8 || pci_slot == 9) {
419 int is_first = (pci_slot & 1) == 0; 431 int is_first = (pci_slot & 1) == 0;
420 432
@@ -628,10 +640,11 @@ done:
628 IVTV_ERR("Defaulting to %s card\n", itv->card->name); 640 IVTV_ERR("Defaulting to %s card\n", itv->card->name);
629 IVTV_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n"); 641 IVTV_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
630 IVTV_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n"); 642 IVTV_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
631 IVTV_ERR("Prefix your subject line with [UNKNOWN CARD].\n"); 643 IVTV_ERR("Prefix your subject line with [UNKNOWN IVTV CARD].\n");
632 } 644 }
633 itv->v4l2_cap = itv->card->v4l2_capabilities; 645 itv->v4l2_cap = itv->card->v4l2_capabilities;
634 itv->card_name = itv->card->name; 646 itv->card_name = itv->card->name;
647 itv->card_i2c = itv->card->i2c;
635} 648}
636 649
637/* Precondition: the ivtv structure has been memset to 0. Only 650/* Precondition: the ivtv structure has been memset to 0. Only
@@ -695,6 +708,7 @@ static int __devinit ivtv_init_struct1(struct ivtv *itv)
695 atomic_set(&itv->yuv_info.next_dma_frame, -1); 708 atomic_set(&itv->yuv_info.next_dma_frame, -1);
696 itv->yuv_info.lace_mode = ivtv_yuv_mode; 709 itv->yuv_info.lace_mode = ivtv_yuv_mode;
697 itv->yuv_info.lace_threshold = ivtv_yuv_threshold; 710 itv->yuv_info.lace_threshold = ivtv_yuv_threshold;
711 itv->yuv_info.max_frames_buffered = 3;
698 return 0; 712 return 0;
699} 713}
700 714
@@ -812,75 +826,61 @@ static int ivtv_setup_pci(struct ivtv *itv, struct pci_dev *dev,
812 return 0; 826 return 0;
813} 827}
814 828
815static void ivtv_request_module(struct ivtv *itv, const char *name) 829static u32 ivtv_request_module(struct ivtv *itv, u32 hw,
830 const char *name, u32 id)
816{ 831{
832 if ((hw & id) == 0)
833 return hw;
817 if (request_module(name) != 0) { 834 if (request_module(name) != 0) {
818 IVTV_ERR("Failed to load module %s\n", name); 835 IVTV_ERR("Failed to load module %s\n", name);
819 } else { 836 return hw & ~id;
820 IVTV_DEBUG_INFO("Loaded module %s\n", name);
821 } 837 }
838 IVTV_DEBUG_INFO("Loaded module %s\n", name);
839 return hw;
822} 840}
823 841
824static void ivtv_load_and_init_modules(struct ivtv *itv) 842static void ivtv_load_and_init_modules(struct ivtv *itv)
825{ 843{
826 u32 hw = itv->card->hw_all; 844 u32 hw = itv->card->hw_all;
827 int i; 845 unsigned i;
828 846
829 /* load modules */ 847 /* load modules */
830#ifndef CONFIG_VIDEO_TUNER 848#ifndef CONFIG_VIDEO_TUNER
831 if (hw & IVTV_HW_TUNER) { 849 hw = ivtv_request_module(itv, hw, "tuner", IVTV_HW_TUNER);
832 if (itv->options.tuner == TUNER_XCEIVE_XC3028) {
833 IVTV_INFO("Xceive tuner not yet supported, only composite and S-Video inputs will be available\n");
834 itv->tunerid = 1;
835 }
836 else {
837 ivtv_request_module(itv, "tuner");
838 }
839 }
840#endif 850#endif
841#ifndef CONFIG_VIDEO_CX25840 851#ifndef CONFIG_VIDEO_CX25840
842 if (hw & IVTV_HW_CX25840) 852 hw = ivtv_request_module(itv, hw, "cx25840", IVTV_HW_CX25840);
843 ivtv_request_module(itv, "cx25840");
844#endif 853#endif
845#ifndef CONFIG_VIDEO_SAA711X 854#ifndef CONFIG_VIDEO_SAA711X
846 if (hw & IVTV_HW_SAA711X) 855 hw = ivtv_request_module(itv, hw, "saa7115", IVTV_HW_SAA711X);
847 ivtv_request_module(itv, "saa7115");
848#endif 856#endif
849#ifndef CONFIG_VIDEO_SAA7127 857#ifndef CONFIG_VIDEO_SAA7127
850 if (hw & IVTV_HW_SAA7127) 858 hw = ivtv_request_module(itv, hw, "saa7127", IVTV_HW_SAA7127);
851 ivtv_request_module(itv, "saa7127");
852#endif 859#endif
853 if (hw & IVTV_HW_SAA717X) 860 hw = ivtv_request_module(itv, hw, "saa717x", IVTV_HW_SAA717X);
854 ivtv_request_module(itv, "saa717x");
855#ifndef CONFIG_VIDEO_UPD64031A 861#ifndef CONFIG_VIDEO_UPD64031A
856 if (hw & IVTV_HW_UPD64031A) 862 hw = ivtv_request_module(itv, hw, "upd64031a", IVTV_HW_UPD64031A);
857 ivtv_request_module(itv, "upd64031a");
858#endif 863#endif
859#ifndef CONFIG_VIDEO_UPD64083 864#ifndef CONFIG_VIDEO_UPD64083
860 if (hw & IVTV_HW_UPD6408X) 865 hw = ivtv_request_module(itv, hw, "upd64083", IVTV_HW_UPD6408X);
861 ivtv_request_module(itv, "upd64083");
862#endif 866#endif
863#ifndef CONFIG_VIDEO_MSP3400 867#ifndef CONFIG_VIDEO_MSP3400
864 if (hw & IVTV_HW_MSP34XX) 868 hw = ivtv_request_module(itv, hw, "msp3400", IVTV_HW_MSP34XX);
865 ivtv_request_module(itv, "msp3400");
866#endif 869#endif
867#ifndef CONFIG_VIDEO_VP27SMPX 870#ifndef CONFIG_VIDEO_VP27SMPX
868 if (hw & IVTV_HW_VP27SMPX) 871 hw = ivtv_request_module(itv, hw, "vp27smpx", IVTV_HW_VP27SMPX);
869 ivtv_request_module(itv, "vp27smpx");
870#endif 872#endif
871 if (hw & IVTV_HW_TVAUDIO)
872 ivtv_request_module(itv, "tvaudio");
873#ifndef CONFIG_VIDEO_WM8775 873#ifndef CONFIG_VIDEO_WM8775
874 if (hw & IVTV_HW_WM8775) 874 hw = ivtv_request_module(itv, hw, "wm8775", IVTV_HW_WM8775);
875 ivtv_request_module(itv, "wm8775");
876#endif 875#endif
877#ifndef CONFIG_VIDEO_WM8739 876#ifndef CONFIG_VIDEO_WM8739
878 if (hw & IVTV_HW_WM8739) 877 hw = ivtv_request_module(itv, hw, "wm8739", IVTV_HW_WM8739);
879 ivtv_request_module(itv, "wm8739");
880#endif 878#endif
881#ifndef CONFIG_VIDEO_CS53L32A 879#ifndef CONFIG_VIDEO_CS53L32A
882 if (hw & IVTV_HW_CS53L32A) 880 hw = ivtv_request_module(itv, hw, "cs53l32a", IVTV_HW_CS53L32A);
883 ivtv_request_module(itv, "cs53l32a"); 881#endif
882#ifndef CONFIG_VIDEO_M52790
883 hw = ivtv_request_module(itv, hw, "m52790", IVTV_HW_M52790);
884#endif 884#endif
885 885
886 /* check which i2c devices are actually found */ 886 /* check which i2c devices are actually found */
@@ -889,11 +889,12 @@ static void ivtv_load_and_init_modules(struct ivtv *itv)
889 889
890 if (!(device & hw)) 890 if (!(device & hw))
891 continue; 891 continue;
892 if (device == IVTV_HW_GPIO) { 892 if (device == IVTV_HW_GPIO || device == IVTV_HW_TVEEPROM) {
893 /* GPIO is always available */ 893 /* GPIO and TVEEPROM do not use i2c probing */
894 itv->hw_flags |= IVTV_HW_GPIO; 894 itv->hw_flags |= device;
895 continue; 895 continue;
896 } 896 }
897 ivtv_i2c_register(itv, i);
897 if (ivtv_i2c_hw_addr(itv, device) > 0) 898 if (ivtv_i2c_hw_addr(itv, device) > 0)
898 itv->hw_flags |= device; 899 itv->hw_flags |= device;
899 } 900 }
@@ -964,7 +965,6 @@ static int __devinit ivtv_probe(struct pci_dev *dev,
964 const struct pci_device_id *pci_id) 965 const struct pci_device_id *pci_id)
965{ 966{
966 int retval = 0; 967 int retval = 0;
967 int yuv_buf_size;
968 int vbi_buf_size; 968 int vbi_buf_size;
969 struct ivtv *itv; 969 struct ivtv *itv;
970 970
@@ -979,7 +979,7 @@ static int __devinit ivtv_probe(struct pci_dev *dev,
979 } 979 }
980 980
981 itv = kzalloc(sizeof(struct ivtv), GFP_ATOMIC); 981 itv = kzalloc(sizeof(struct ivtv), GFP_ATOMIC);
982 if (itv == 0) { 982 if (itv == NULL) {
983 spin_unlock(&ivtv_cards_lock); 983 spin_unlock(&ivtv_cards_lock);
984 return -ENOMEM; 984 return -ENOMEM;
985 } 985 }
@@ -1068,9 +1068,6 @@ static int __devinit ivtv_probe(struct pci_dev *dev,
1068 IVTV_DEBUG_INFO("Active card count: %d.\n", ivtv_cards_active); 1068 IVTV_DEBUG_INFO("Active card count: %d.\n", ivtv_cards_active);
1069 1069
1070 if (itv->card->hw_all & IVTV_HW_TVEEPROM) { 1070 if (itv->card->hw_all & IVTV_HW_TVEEPROM) {
1071#ifdef CONFIG_VIDEO_TVEEPROM_MODULE
1072 ivtv_request_module(itv, "tveeprom");
1073#endif
1074 /* Based on the model number the cardtype may be changed. 1071 /* Based on the model number the cardtype may be changed.
1075 The PCI IDs are not always reliable. */ 1072 The PCI IDs are not always reliable. */
1076 ivtv_process_eeprom(itv); 1073 ivtv_process_eeprom(itv);
@@ -1111,16 +1108,19 @@ static int __devinit ivtv_probe(struct pci_dev *dev,
1111 itv->is_50hz = 1; 1108 itv->is_50hz = 1;
1112 itv->is_out_50hz = 1; 1109 itv->is_out_50hz = 1;
1113 } 1110 }
1111
1112 itv->yuv_info.osd_full_w = 720;
1113 itv->yuv_info.osd_full_h = itv->is_out_50hz ? 576 : 480;
1114 itv->yuv_info.v4l2_src_w = itv->yuv_info.osd_full_w;
1115 itv->yuv_info.v4l2_src_h = itv->yuv_info.osd_full_h;
1116
1114 itv->params.video_gop_size = itv->is_60hz ? 15 : 12; 1117 itv->params.video_gop_size = itv->is_60hz ? 15 : 12;
1115 1118
1116 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_MPG] = 0x08000; 1119 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_MPG] = 0x08000;
1117 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_PCM] = 0x01200; 1120 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_PCM] = 0x01200;
1118 itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_MPG] = 0x10000; 1121 itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_MPG] = 0x10000;
1119 1122 itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_YUV] = 0x10000;
1120 /* 0x15180 == 720 * 480 / 4, 0x19500 == 720 * 576 / 4 */ 1123 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_YUV] = 0x08000;
1121 yuv_buf_size = itv->is_60hz ? 0x15180 : 0x19500;
1122 itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_YUV] = yuv_buf_size / 2;
1123 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_YUV] = yuv_buf_size / 8;
1124 1124
1125 /* Setup VBI Raw Size. Should be big enough to hold PAL. 1125 /* Setup VBI Raw Size. Should be big enough to hold PAL.
1126 It is possible to switch between PAL and NTSC, so we need to 1126 It is possible to switch between PAL and NTSC, so we need to
@@ -1140,13 +1140,26 @@ static int __devinit ivtv_probe(struct pci_dev *dev,
1140 if (itv->options.radio > 0) 1140 if (itv->options.radio > 0)
1141 itv->v4l2_cap |= V4L2_CAP_RADIO; 1141 itv->v4l2_cap |= V4L2_CAP_RADIO;
1142 1142
1143 if (itv->options.tuner > -1 && itv->tunerid == 0) { 1143 if (itv->options.tuner > -1) {
1144 struct tuner_setup setup; 1144 struct tuner_setup setup;
1145 1145
1146 setup.addr = ADDR_UNSET; 1146 setup.addr = ADDR_UNSET;
1147 setup.type = itv->options.tuner; 1147 setup.type = itv->options.tuner;
1148 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */ 1148 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
1149 setup.tuner_callback = (setup.type == TUNER_XC2028) ?
1150 ivtv_reset_tuner_gpio : NULL;
1149 ivtv_call_i2c_clients(itv, TUNER_SET_TYPE_ADDR, &setup); 1151 ivtv_call_i2c_clients(itv, TUNER_SET_TYPE_ADDR, &setup);
1152 if (setup.type == TUNER_XC2028) {
1153 static struct xc2028_ctrl ctrl = {
1154 .fname = XC2028_DEFAULT_FIRMWARE,
1155 .max_len = 64,
1156 };
1157 struct v4l2_priv_tun_config cfg = {
1158 .tuner = itv->options.tuner,
1159 .priv = &ctrl,
1160 };
1161 ivtv_call_i2c_clients(itv, TUNER_SET_CONFIG, &cfg);
1162 }
1150 } 1163 }
1151 1164
1152 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video) 1165 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
diff --git a/drivers/media/video/ivtv/ivtv-driver.h b/drivers/media/video/ivtv/ivtv-driver.h
index 49ce14d14a54..536140f0c19e 100644
--- a/drivers/media/video/ivtv/ivtv-driver.h
+++ b/drivers/media/video/ivtv/ivtv-driver.h
@@ -65,7 +65,6 @@
65 65
66#include <linux/ivtv.h> 66#include <linux/ivtv.h>
67 67
68
69/* Memory layout */ 68/* Memory layout */
70#define IVTV_ENCODER_OFFSET 0x00000000 69#define IVTV_ENCODER_OFFSET 0x00000000
71#define IVTV_ENCODER_SIZE 0x00800000 /* Total size is 0x01000000, but only first half is used */ 70#define IVTV_ENCODER_SIZE 0x00800000 /* Total size is 0x01000000, but only first half is used */
@@ -392,6 +391,9 @@ struct yuv_frame_info
392 u32 tru_h; 391 u32 tru_h;
393 u32 offset_y; 392 u32 offset_y;
394 s32 lace_mode; 393 s32 lace_mode;
394 u32 sync_field;
395 u32 delay;
396 u32 interlaced;
395}; 397};
396 398
397#define IVTV_YUV_MODE_INTERLACED 0x00 399#define IVTV_YUV_MODE_INTERLACED 0x00
@@ -403,6 +405,8 @@ struct yuv_frame_info
403#define IVTV_YUV_SYNC_ODD 0x04 405#define IVTV_YUV_SYNC_ODD 0x04
404#define IVTV_YUV_SYNC_MASK 0x04 406#define IVTV_YUV_SYNC_MASK 0x04
405 407
408#define IVTV_YUV_BUFFERS 8
409
406struct yuv_playback_info 410struct yuv_playback_info
407{ 411{
408 u32 reg_2834; 412 u32 reg_2834;
@@ -461,9 +465,10 @@ struct yuv_playback_info
461 u32 osd_vis_w; 465 u32 osd_vis_w;
462 u32 osd_vis_h; 466 u32 osd_vis_h;
463 467
464 int decode_height; 468 u32 osd_full_w;
469 u32 osd_full_h;
465 470
466 int frame_interlaced; 471 int decode_height;
467 472
468 int lace_mode; 473 int lace_mode;
469 int lace_threshold; 474 int lace_threshold;
@@ -475,16 +480,23 @@ struct yuv_playback_info
475 u32 yuv_forced_update; 480 u32 yuv_forced_update;
476 int update_frame; 481 int update_frame;
477 482
478 int sync_field[4]; /* Field to sync on */
479 int field_delay[4]; /* Flag to extend duration of previous frame */
480 u8 fields_lapsed; /* Counter used when delaying a frame */ 483 u8 fields_lapsed; /* Counter used when delaying a frame */
481 484
482 struct yuv_frame_info new_frame_info[4]; 485 struct yuv_frame_info new_frame_info[IVTV_YUV_BUFFERS];
483 struct yuv_frame_info old_frame_info; 486 struct yuv_frame_info old_frame_info;
484 struct yuv_frame_info old_frame_info_args; 487 struct yuv_frame_info old_frame_info_args;
485 488
486 void *blanking_ptr; 489 void *blanking_ptr;
487 dma_addr_t blanking_dmaptr; 490 dma_addr_t blanking_dmaptr;
491
492 int stream_size;
493
494 u8 draw_frame; /* PVR350 buffer to draw into */
495 u8 max_frames_buffered; /* Maximum number of frames to buffer */
496
497 struct v4l2_rect main_rect;
498 u32 v4l2_src_w;
499 u32 v4l2_src_h;
488}; 500};
489 501
490#define IVTV_VBI_FRAMES 32 502#define IVTV_VBI_FRAMES 32
@@ -577,13 +589,13 @@ struct ivtv {
577 struct pci_dev *dev; /* PCI device */ 589 struct pci_dev *dev; /* PCI device */
578 const struct ivtv_card *card; /* card information */ 590 const struct ivtv_card *card; /* card information */
579 const char *card_name; /* full name of the card */ 591 const char *card_name; /* full name of the card */
592 const struct ivtv_card_tuner_i2c *card_i2c; /* i2c addresses to probe for tuner */
580 u8 has_cx23415; /* 1 if it is a cx23415 based card, 0 for cx23416 */ 593 u8 has_cx23415; /* 1 if it is a cx23415 based card, 0 for cx23416 */
581 u8 pvr150_workaround; /* 1 if the cx25840 needs to workaround a PVR150 bug */ 594 u8 pvr150_workaround; /* 1 if the cx25840 needs to workaround a PVR150 bug */
582 u8 nof_inputs; /* number of video inputs */ 595 u8 nof_inputs; /* number of video inputs */
583 u8 nof_audio_inputs; /* number of audio inputs */ 596 u8 nof_audio_inputs; /* number of audio inputs */
584 u32 v4l2_cap; /* V4L2 capabilities of card */ 597 u32 v4l2_cap; /* V4L2 capabilities of card */
585 u32 hw_flags; /* hardware description of the board */ 598 u32 hw_flags; /* hardware description of the board */
586 int tunerid; /* userspace tuner ID for experimental Xceive tuner support */
587 v4l2_std_id tuner_std; /* the norm of the card's tuner (fixed) */ 599 v4l2_std_id tuner_std; /* the norm of the card's tuner (fixed) */
588 /* controlling video decoder function */ 600 /* controlling video decoder function */
589 int (*video_dec_func)(struct ivtv *, unsigned int, void *); 601 int (*video_dec_func)(struct ivtv *, unsigned int, void *);
diff --git a/drivers/media/video/ivtv/ivtv-fileops.c b/drivers/media/video/ivtv/ivtv-fileops.c
index a200a8a95a2d..6fb96f19a866 100644
--- a/drivers/media/video/ivtv/ivtv-fileops.c
+++ b/drivers/media/video/ivtv/ivtv-fileops.c
@@ -542,6 +542,7 @@ ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t c
542 struct ivtv_open_id *id = filp->private_data; 542 struct ivtv_open_id *id = filp->private_data;
543 struct ivtv *itv = id->itv; 543 struct ivtv *itv = id->itv;
544 struct ivtv_stream *s = &itv->streams[id->type]; 544 struct ivtv_stream *s = &itv->streams[id->type];
545 struct yuv_playback_info *yi = &itv->yuv_info;
545 struct ivtv_buffer *buf; 546 struct ivtv_buffer *buf;
546 struct ivtv_queue q; 547 struct ivtv_queue q;
547 int bytes_written = 0; 548 int bytes_written = 0;
@@ -580,6 +581,24 @@ ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t c
580 set_bit(IVTV_F_S_APPL_IO, &s->s_flags); 581 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
581 582
582retry: 583retry:
584 /* If possible, just DMA the entire frame - Check the data transfer size
585 since we may get here before the stream has been fully set-up */
586 if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) {
587 while (count >= itv->dma_data_req_size) {
588 if (!ivtv_yuv_udma_stream_frame (itv, (void *)user_buf)) {
589 bytes_written += itv->dma_data_req_size;
590 user_buf += itv->dma_data_req_size;
591 count -= itv->dma_data_req_size;
592 } else {
593 break;
594 }
595 }
596 if (count == 0) {
597 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
598 return bytes_written;
599 }
600 }
601
583 for (;;) { 602 for (;;) {
584 /* Gather buffers */ 603 /* Gather buffers */
585 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io))) 604 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
@@ -604,9 +623,16 @@ retry:
604 623
605 /* copy user data into buffers */ 624 /* copy user data into buffers */
606 while ((buf = ivtv_dequeue(s, &q))) { 625 while ((buf = ivtv_dequeue(s, &q))) {
607 /* Make sure we really got all the user data */ 626 /* yuv is a pain. Don't copy more data than needed for a single
608 rc = ivtv_buf_copy_from_user(s, buf, user_buf, count); 627 frame, otherwise we lose sync with the incoming stream */
628 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
629 yi->stream_size + count > itv->dma_data_req_size)
630 rc = ivtv_buf_copy_from_user(s, buf, user_buf,
631 itv->dma_data_req_size - yi->stream_size);
632 else
633 rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
609 634
635 /* Make sure we really got all the user data */
610 if (rc < 0) { 636 if (rc < 0) {
611 ivtv_queue_move(s, &q, NULL, &s->q_free, 0); 637 ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
612 return rc; 638 return rc;
@@ -615,6 +641,16 @@ retry:
615 count -= rc; 641 count -= rc;
616 bytes_written += rc; 642 bytes_written += rc;
617 643
644 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
645 yi->stream_size += rc;
646 /* If we have a complete yuv frame, break loop now */
647 if (yi->stream_size == itv->dma_data_req_size) {
648 ivtv_enqueue(s, buf, &s->q_full);
649 yi->stream_size = 0;
650 break;
651 }
652 }
653
618 if (buf->bytesused != s->buf_size) { 654 if (buf->bytesused != s->buf_size) {
619 /* incomplete, leave in q_io for next time */ 655 /* incomplete, leave in q_io for next time */
620 ivtv_enqueue(s, buf, &s->q_io); 656 ivtv_enqueue(s, buf, &s->q_io);
@@ -642,6 +678,9 @@ retry:
642 if (s->q_full.length >= itv->dma_data_req_size) { 678 if (s->q_full.length >= itv->dma_data_req_size) {
643 int got_sig; 679 int got_sig;
644 680
681 if (mode == OUT_YUV)
682 ivtv_yuv_setup_stream_frame(itv);
683
645 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE); 684 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
646 while (!(got_sig = signal_pending(current)) && 685 while (!(got_sig = signal_pending(current)) &&
647 test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) { 686 test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
@@ -922,10 +961,15 @@ static int ivtv_serialized_open(struct ivtv_stream *s, struct file *filp)
922 } 961 }
923 962
924 /* YUV or MPG Decoding Mode? */ 963 /* YUV or MPG Decoding Mode? */
925 if (s->type == IVTV_DEC_STREAM_TYPE_MPG) 964 if (s->type == IVTV_DEC_STREAM_TYPE_MPG) {
926 clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags); 965 clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
927 else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) 966 } else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
928 set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags); 967 set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
968 /* For yuv, we need to know the dma size before we start */
969 itv->dma_data_req_size =
970 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
971 itv->yuv_info.stream_size = 0;
972 }
929 return 0; 973 return 0;
930} 974}
931 975
diff --git a/drivers/media/video/ivtv/ivtv-gpio.c b/drivers/media/video/ivtv/ivtv-gpio.c
index 132fb5f71366..688cd3856685 100644
--- a/drivers/media/video/ivtv/ivtv-gpio.c
+++ b/drivers/media/video/ivtv/ivtv-gpio.c
@@ -22,6 +22,7 @@
22#include "ivtv-driver.h" 22#include "ivtv-driver.h"
23#include "ivtv-cards.h" 23#include "ivtv-cards.h"
24#include "ivtv-gpio.h" 24#include "ivtv-gpio.h"
25#include "tuner-xc2028.h"
25#include <media/tuner.h> 26#include <media/tuner.h>
26 27
27/* 28/*
@@ -122,6 +123,29 @@ void ivtv_reset_ir_gpio(struct ivtv *itv)
122 write_reg(curdir, IVTV_REG_GPIO_DIR); 123 write_reg(curdir, IVTV_REG_GPIO_DIR);
123} 124}
124 125
126/* Xceive tuner reset function */
127int ivtv_reset_tuner_gpio(void *dev, int cmd, int value)
128{
129 struct i2c_algo_bit_data *algo = dev;
130 struct ivtv *itv = algo->data;
131 int curdir, curout;
132
133 if (cmd != XC2028_TUNER_RESET)
134 return 0;
135 IVTV_DEBUG_INFO("Resetting tuner\n");
136 curout = read_reg(IVTV_REG_GPIO_OUT);
137 curdir = read_reg(IVTV_REG_GPIO_DIR);
138 curdir |= (1 << 12); /* GPIO bit 12 */
139
140 curout &= ~(1 << 12);
141 write_reg(curout, IVTV_REG_GPIO_OUT);
142 schedule_timeout_interruptible(msecs_to_jiffies(1));
143
144 curout |= (1 << 12);
145 write_reg(curout, IVTV_REG_GPIO_OUT);
146 schedule_timeout_interruptible(msecs_to_jiffies(1));
147 return 0;
148}
125 149
126void ivtv_gpio_init(struct ivtv *itv) 150void ivtv_gpio_init(struct ivtv *itv)
127{ 151{
diff --git a/drivers/media/video/ivtv/ivtv-i2c.c b/drivers/media/video/ivtv/ivtv-i2c.c
index 36e54f78aa2a..fa5ab1eb1800 100644
--- a/drivers/media/video/ivtv/ivtv-i2c.c
+++ b/drivers/media/video/ivtv/ivtv-i2c.c
@@ -80,6 +80,7 @@
80#endif /* I2C_ADAP_CLASS_TV_ANALOG */ 80#endif /* I2C_ADAP_CLASS_TV_ANALOG */
81 81
82#define IVTV_CS53L32A_I2C_ADDR 0x11 82#define IVTV_CS53L32A_I2C_ADDR 0x11
83#define IVTV_M52790_I2C_ADDR 0x48
83#define IVTV_CX25840_I2C_ADDR 0x44 84#define IVTV_CX25840_I2C_ADDR 0x44
84#define IVTV_SAA7115_I2C_ADDR 0x21 85#define IVTV_SAA7115_I2C_ADDR 0x21
85#define IVTV_SAA7127_I2C_ADDR 0x44 86#define IVTV_SAA7127_I2C_ADDR 0x44
@@ -91,7 +92,8 @@
91#define IVTV_TEA5767_I2C_ADDR 0x60 92#define IVTV_TEA5767_I2C_ADDR 0x60
92#define IVTV_UPD64031A_I2C_ADDR 0x12 93#define IVTV_UPD64031A_I2C_ADDR 0x12
93#define IVTV_UPD64083_I2C_ADDR 0x5c 94#define IVTV_UPD64083_I2C_ADDR 0x5c
94#define IVTV_TDA985X_I2C_ADDR 0x5b 95#define IVTV_VP27SMPX_I2C_ADDR 0x5b
96#define IVTV_M52790_I2C_ADDR 0x48
95 97
96/* This array should match the IVTV_HW_ defines */ 98/* This array should match the IVTV_HW_ defines */
97static const u8 hw_driverids[] = { 99static const u8 hw_driverids[] = {
@@ -104,18 +106,38 @@ static const u8 hw_driverids[] = {
104 I2C_DRIVERID_CS53L32A, 106 I2C_DRIVERID_CS53L32A,
105 I2C_DRIVERID_TVEEPROM, 107 I2C_DRIVERID_TVEEPROM,
106 I2C_DRIVERID_SAA711X, 108 I2C_DRIVERID_SAA711X,
107 I2C_DRIVERID_TVAUDIO,
108 I2C_DRIVERID_UPD64031A, 109 I2C_DRIVERID_UPD64031A,
109 I2C_DRIVERID_UPD64083, 110 I2C_DRIVERID_UPD64083,
110 I2C_DRIVERID_SAA717X, 111 I2C_DRIVERID_SAA717X,
111 I2C_DRIVERID_WM8739, 112 I2C_DRIVERID_WM8739,
112 I2C_DRIVERID_VP27SMPX, 113 I2C_DRIVERID_VP27SMPX,
114 I2C_DRIVERID_M52790,
115 0 /* IVTV_HW_GPIO dummy driver ID */
116};
117
118/* This array should match the IVTV_HW_ defines */
119static const u8 hw_addrs[] = {
120 IVTV_CX25840_I2C_ADDR,
121 IVTV_SAA7115_I2C_ADDR,
122 IVTV_SAA7127_I2C_ADDR,
123 IVTV_MSP3400_I2C_ADDR,
124 0,
125 IVTV_WM8775_I2C_ADDR,
126 IVTV_CS53L32A_I2C_ADDR,
127 0,
128 IVTV_SAA7115_I2C_ADDR,
129 IVTV_UPD64031A_I2C_ADDR,
130 IVTV_UPD64083_I2C_ADDR,
131 IVTV_SAA717x_I2C_ADDR,
132 IVTV_WM8739_I2C_ADDR,
133 IVTV_VP27SMPX_I2C_ADDR,
134 IVTV_M52790_I2C_ADDR,
113 0 /* IVTV_HW_GPIO dummy driver ID */ 135 0 /* IVTV_HW_GPIO dummy driver ID */
114}; 136};
115 137
116/* This array should match the IVTV_HW_ defines */ 138/* This array should match the IVTV_HW_ defines */
117static const char * const hw_drivernames[] = { 139static const char * const hw_drivernames[] = {
118 "cx2584x", 140 "cx25840",
119 "saa7115", 141 "saa7115",
120 "saa7127", 142 "saa7127",
121 "msp3400", 143 "msp3400",
@@ -123,31 +145,67 @@ static const char * const hw_drivernames[] = {
123 "wm8775", 145 "wm8775",
124 "cs53l32a", 146 "cs53l32a",
125 "tveeprom", 147 "tveeprom",
126 "saa7114", 148 "saa7115",
127 "tvaudio",
128 "upd64031a", 149 "upd64031a",
129 "upd64083", 150 "upd64083",
130 "saa717x", 151 "saa717x",
131 "wm8739", 152 "wm8739",
132 "vp27smpx", 153 "vp27smpx",
154 "m52790",
133 "gpio", 155 "gpio",
134}; 156};
135 157
136static int attach_inform(struct i2c_client *client) 158int ivtv_i2c_register(struct ivtv *itv, unsigned idx)
137{ 159{
138 struct ivtv *itv = (struct ivtv *)i2c_get_adapdata(client->adapter); 160 struct i2c_board_info info;
161 struct i2c_client *c;
162 u8 id;
139 int i; 163 int i;
140 164
141 IVTV_DEBUG_I2C("i2c client attach\n"); 165 IVTV_DEBUG_I2C("i2c client register\n");
142 for (i = 0; i < I2C_CLIENTS_MAX; i++) { 166 if (idx >= ARRAY_SIZE(hw_driverids) || hw_driverids[idx] == 0)
143 if (itv->i2c_clients[i] == NULL) { 167 return -1;
144 itv->i2c_clients[i] = client; 168 id = hw_driverids[idx];
145 break; 169 memset(&info, 0, sizeof(info));
146 } 170 strcpy(info.driver_name, hw_drivernames[idx]);
147 } 171 info.addr = hw_addrs[idx];
172 for (i = 0; itv->i2c_clients[i] && i < I2C_CLIENTS_MAX; i++) {}
173
148 if (i == I2C_CLIENTS_MAX) { 174 if (i == I2C_CLIENTS_MAX) {
149 IVTV_ERR("Insufficient room for new I2C client\n"); 175 IVTV_ERR("insufficient room for new I2C client!\n");
176 return -ENOMEM;
150 } 177 }
178
179 if (id != I2C_DRIVERID_TUNER) {
180 c = i2c_new_device(&itv->i2c_adap, &info);
181 if (c->driver == NULL)
182 i2c_unregister_device(c);
183 else
184 itv->i2c_clients[i] = c;
185 return itv->i2c_clients[i] ? 0 : -ENODEV;
186 }
187
188 /* special tuner handling */
189 c = i2c_new_probed_device(&itv->i2c_adap, &info, itv->card_i2c->radio);
190 if (c && c->driver == NULL)
191 i2c_unregister_device(c);
192 else if (c)
193 itv->i2c_clients[i++] = c;
194 c = i2c_new_probed_device(&itv->i2c_adap, &info, itv->card_i2c->demod);
195 if (c && c->driver == NULL)
196 i2c_unregister_device(c);
197 else if (c)
198 itv->i2c_clients[i++] = c;
199 c = i2c_new_probed_device(&itv->i2c_adap, &info, itv->card_i2c->tv);
200 if (c && c->driver == NULL)
201 i2c_unregister_device(c);
202 else if (c)
203 itv->i2c_clients[i++] = c;
204 return 0;
205}
206
207static int attach_inform(struct i2c_client *client)
208{
151 return 0; 209 return 0;
152} 210}
153 211
@@ -475,9 +533,6 @@ static struct i2c_adapter ivtv_i2c_adap_hw_template = {
475 .client_register = attach_inform, 533 .client_register = attach_inform,
476 .client_unregister = detach_inform, 534 .client_unregister = detach_inform,
477 .owner = THIS_MODULE, 535 .owner = THIS_MODULE,
478#ifdef I2C_ADAP_CLASS_TV_ANALOG
479 .class = I2C_ADAP_CLASS_TV_ANALOG,
480#endif
481}; 536};
482 537
483static void ivtv_setscl_old(void *data, int state) 538static void ivtv_setscl_old(void *data, int state)
@@ -525,15 +580,12 @@ static int ivtv_getsda_old(void *data)
525/* template for i2c-bit-algo */ 580/* template for i2c-bit-algo */
526static struct i2c_adapter ivtv_i2c_adap_template = { 581static struct i2c_adapter ivtv_i2c_adap_template = {
527 .name = "ivtv i2c driver", 582 .name = "ivtv i2c driver",
528 .id = I2C_HW_B_CX2341X, /* algo-bit is OR'd with this */ 583 .id = I2C_HW_B_CX2341X,
529 .algo = NULL, /* set by i2c-algo-bit */ 584 .algo = NULL, /* set by i2c-algo-bit */
530 .algo_data = NULL, /* filled from template */ 585 .algo_data = NULL, /* filled from template */
531 .client_register = attach_inform, 586 .client_register = attach_inform,
532 .client_unregister = detach_inform, 587 .client_unregister = detach_inform,
533 .owner = THIS_MODULE, 588 .owner = THIS_MODULE,
534#ifdef I2C_ADAP_CLASS_TV_ANALOG
535 .class = I2C_ADAP_CLASS_TV_ANALOG,
536#endif
537}; 589};
538 590
539static const struct i2c_algo_bit_data ivtv_i2c_algo_template = { 591static const struct i2c_algo_bit_data ivtv_i2c_algo_template = {
@@ -558,12 +610,9 @@ int ivtv_call_i2c_client(struct ivtv *itv, int addr, unsigned int cmd, void *arg
558 IVTV_DEBUG_I2C("call_i2c_client addr=%02x\n", addr); 610 IVTV_DEBUG_I2C("call_i2c_client addr=%02x\n", addr);
559 for (i = 0; i < I2C_CLIENTS_MAX; i++) { 611 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
560 client = itv->i2c_clients[i]; 612 client = itv->i2c_clients[i];
561 if (client == NULL) { 613 if (client == NULL || client->driver == NULL ||
562 continue; 614 client->driver->command == NULL)
563 }
564 if (client->driver->command == NULL) {
565 continue; 615 continue;
566 }
567 if (addr == client->addr) { 616 if (addr == client->addr) {
568 retval = client->driver->command(client, cmd, arg); 617 retval = client->driver->command(client, cmd, arg);
569 return retval; 618 return retval;
@@ -584,7 +633,7 @@ static int ivtv_i2c_id_addr(struct ivtv *itv, u32 id)
584 633
585 for (i = 0; i < I2C_CLIENTS_MAX; i++) { 634 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
586 client = itv->i2c_clients[i]; 635 client = itv->i2c_clients[i];
587 if (client == NULL) 636 if (client == NULL || client->driver == NULL)
588 continue; 637 continue;
589 if (id == client->driver->id) { 638 if (id == client->driver->id) {
590 retval = client->addr; 639 retval = client->addr;
@@ -710,6 +759,16 @@ int init_ivtv_i2c(struct ivtv *itv)
710{ 759{
711 IVTV_DEBUG_I2C("i2c init\n"); 760 IVTV_DEBUG_I2C("i2c init\n");
712 761
762 /* Sanity checks for the I2C hardware arrays. They must be the
763 * same size and GPIO must be the last entry.
764 */
765 if (ARRAY_SIZE(hw_driverids) != ARRAY_SIZE(hw_addrs) ||
766 ARRAY_SIZE(hw_drivernames) != ARRAY_SIZE(hw_addrs) ||
767 IVTV_HW_GPIO != (1 << (ARRAY_SIZE(hw_addrs) - 1)) ||
768 hw_driverids[ARRAY_SIZE(hw_addrs) - 1]) {
769 IVTV_ERR("Mismatched I2C hardware arrays\n");
770 return -ENODEV;
771 }
713 if (itv->options.newi2c > 0) { 772 if (itv->options.newi2c > 0) {
714 memcpy(&itv->i2c_adap, &ivtv_i2c_adap_hw_template, 773 memcpy(&itv->i2c_adap, &ivtv_i2c_adap_hw_template,
715 sizeof(struct i2c_adapter)); 774 sizeof(struct i2c_adapter));
@@ -718,9 +777,9 @@ int init_ivtv_i2c(struct ivtv *itv)
718 sizeof(struct i2c_adapter)); 777 sizeof(struct i2c_adapter));
719 memcpy(&itv->i2c_algo, &ivtv_i2c_algo_template, 778 memcpy(&itv->i2c_algo, &ivtv_i2c_algo_template,
720 sizeof(struct i2c_algo_bit_data)); 779 sizeof(struct i2c_algo_bit_data));
721 itv->i2c_algo.data = itv;
722 itv->i2c_adap.algo_data = &itv->i2c_algo;
723 } 780 }
781 itv->i2c_algo.data = itv;
782 itv->i2c_adap.algo_data = &itv->i2c_algo;
724 783
725 sprintf(itv->i2c_adap.name + strlen(itv->i2c_adap.name), " #%d", 784 sprintf(itv->i2c_adap.name + strlen(itv->i2c_adap.name), " #%d",
726 itv->num); 785 itv->num);
diff --git a/drivers/media/video/ivtv/ivtv-i2c.h b/drivers/media/video/ivtv/ivtv-i2c.h
index 987042c09b64..022978cf533d 100644
--- a/drivers/media/video/ivtv/ivtv-i2c.h
+++ b/drivers/media/video/ivtv/ivtv-i2c.h
@@ -33,6 +33,7 @@ int ivtv_i2c_hw(struct ivtv *itv, u32 hw, unsigned int cmd, void *arg);
33int ivtv_i2c_id(struct ivtv *itv, u32 id, unsigned int cmd, void *arg); 33int ivtv_i2c_id(struct ivtv *itv, u32 id, unsigned int cmd, void *arg);
34int ivtv_call_i2c_client(struct ivtv *itv, int addr, unsigned int cmd, void *arg); 34int ivtv_call_i2c_client(struct ivtv *itv, int addr, unsigned int cmd, void *arg);
35void ivtv_call_i2c_clients(struct ivtv *itv, unsigned int cmd, void *arg); 35void ivtv_call_i2c_clients(struct ivtv *itv, unsigned int cmd, void *arg);
36int ivtv_i2c_register(struct ivtv *itv, unsigned idx);
36 37
37/* init + register i2c algo-bit adapter */ 38/* init + register i2c algo-bit adapter */
38int init_ivtv_i2c(struct ivtv *itv); 39int init_ivtv_i2c(struct ivtv *itv);
diff --git a/drivers/media/video/ivtv/ivtv-ioctl.c b/drivers/media/video/ivtv/ivtv-ioctl.c
index fd6826f472e3..edef2a579617 100644
--- a/drivers/media/video/ivtv/ivtv-ioctl.c
+++ b/drivers/media/video/ivtv/ivtv-ioctl.c
@@ -372,7 +372,7 @@ static int ivtv_get_fmt(struct ivtv *itv, int streamtype, struct v4l2_format *fm
372 fmt->fmt.pix.height = itv->main_rect.height; 372 fmt->fmt.pix.height = itv->main_rect.height;
373 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 373 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
374 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED; 374 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
375 if (itv->output_mode == OUT_UDMA_YUV) { 375 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
376 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) { 376 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
377 case IVTV_YUV_MODE_INTERLACED: 377 case IVTV_YUV_MODE_INTERLACED:
378 fmt->fmt.pix.field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ? 378 fmt->fmt.pix.field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
@@ -386,14 +386,13 @@ static int ivtv_get_fmt(struct ivtv *itv, int streamtype, struct v4l2_format *fm
386 break; 386 break;
387 } 387 }
388 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12; 388 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
389 fmt->fmt.pix.bytesperline = 720;
390 fmt->fmt.pix.width = itv->yuv_info.v4l2_src_w;
391 fmt->fmt.pix.height = itv->yuv_info.v4l2_src_h;
389 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */ 392 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
390 fmt->fmt.pix.sizeimage = 393 fmt->fmt.pix.sizeimage =
391 fmt->fmt.pix.height * fmt->fmt.pix.width + 394 1080 * ((fmt->fmt.pix.height + 31) & ~31);
392 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2); 395 } else if (streamtype == IVTV_ENC_STREAM_TYPE_YUV) {
393 }
394 else if (itv->output_mode == OUT_YUV ||
395 streamtype == IVTV_ENC_STREAM_TYPE_YUV ||
396 streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
397 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12; 396 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
398 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */ 397 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
399 fmt->fmt.pix.sizeimage = 398 fmt->fmt.pix.sizeimage =
@@ -490,6 +489,7 @@ static int ivtv_get_fmt(struct ivtv *itv, int streamtype, struct v4l2_format *fm
490static int ivtv_try_or_set_fmt(struct ivtv *itv, int streamtype, 489static int ivtv_try_or_set_fmt(struct ivtv *itv, int streamtype,
491 struct v4l2_format *fmt, int set_fmt) 490 struct v4l2_format *fmt, int set_fmt)
492{ 491{
492 struct yuv_playback_info *yi = &itv->yuv_info;
493 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced; 493 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
494 u16 set; 494 u16 set;
495 495
@@ -505,39 +505,52 @@ static int ivtv_try_or_set_fmt(struct ivtv *itv, int streamtype,
505 r.width = fmt->fmt.pix.width; 505 r.width = fmt->fmt.pix.width;
506 r.height = fmt->fmt.pix.height; 506 r.height = fmt->fmt.pix.height;
507 ivtv_get_fmt(itv, streamtype, fmt); 507 ivtv_get_fmt(itv, streamtype, fmt);
508 if (itv->output_mode != OUT_UDMA_YUV) { 508 fmt->fmt.pix.width = r.width;
509 /* TODO: would setting the rect also be valid for this mode? */ 509 fmt->fmt.pix.height = r.height;
510 fmt->fmt.pix.width = r.width; 510 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
511 fmt->fmt.pix.height = r.height;
512 }
513 if (itv->output_mode == OUT_UDMA_YUV) {
514 /* TODO: add checks for validity */
515 fmt->fmt.pix.field = field; 511 fmt->fmt.pix.field = field;
512 if (fmt->fmt.pix.width < 2)
513 fmt->fmt.pix.width = 2;
514 if (fmt->fmt.pix.width > 720)
515 fmt->fmt.pix.width = 720;
516 if (fmt->fmt.pix.height < 2)
517 fmt->fmt.pix.height = 2;
518 if (fmt->fmt.pix.height > 576)
519 fmt->fmt.pix.height = 576;
516 } 520 }
517 if (set_fmt) { 521 if (set_fmt && streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
518 if (itv->output_mode == OUT_UDMA_YUV) { 522 /* Return now if we already have some frame data */
519 switch (field) { 523 if (yi->stream_size)
520 case V4L2_FIELD_NONE: 524 return -EBUSY;
521 itv->yuv_info.lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
522 break;
523 case V4L2_FIELD_ANY:
524 itv->yuv_info.lace_mode = IVTV_YUV_MODE_AUTO;
525 break;
526 case V4L2_FIELD_INTERLACED_BT:
527 itv->yuv_info.lace_mode =
528 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
529 break;
530 case V4L2_FIELD_INTERLACED_TB:
531 default:
532 itv->yuv_info.lace_mode = IVTV_YUV_MODE_INTERLACED;
533 break;
534 }
535 itv->yuv_info.lace_sync_field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
536 525
537 /* Force update of yuv registers */ 526 yi->v4l2_src_w = r.width;
538 itv->yuv_info.yuv_forced_update = 1; 527 yi->v4l2_src_h = r.height;
539 return 0; 528
529 switch (field) {
530 case V4L2_FIELD_NONE:
531 yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
532 break;
533 case V4L2_FIELD_ANY:
534 yi->lace_mode = IVTV_YUV_MODE_AUTO;
535 break;
536 case V4L2_FIELD_INTERLACED_BT:
537 yi->lace_mode =
538 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
539 break;
540 case V4L2_FIELD_INTERLACED_TB:
541 default:
542 yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
543 break;
540 } 544 }
545 yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
546
547 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
548 itv->dma_data_req_size =
549 1080 * ((yi->v4l2_src_h + 31) & ~31);
550
551 /* Force update of yuv registers */
552 yi->yuv_forced_update = 1;
553 return 0;
541 } 554 }
542 return 0; 555 return 0;
543 } 556 }
@@ -660,11 +673,8 @@ static int ivtv_debug_ioctls(struct file *filp, unsigned int cmd, void *arg)
660 chip->ident = V4L2_IDENT_NONE; 673 chip->ident = V4L2_IDENT_NONE;
661 chip->revision = 0; 674 chip->revision = 0;
662 if (reg->match_type == V4L2_CHIP_MATCH_HOST) { 675 if (reg->match_type == V4L2_CHIP_MATCH_HOST) {
663 if (v4l2_chip_match_host(reg->match_type, reg->match_chip)) { 676 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
664 struct v4l2_chip_ident *chip = arg;
665
666 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416; 677 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
667 }
668 return 0; 678 return 0;
669 } 679 }
670 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER) 680 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
@@ -688,7 +698,7 @@ static int ivtv_debug_ioctls(struct file *filp, unsigned int cmd, void *arg)
688 ivtv_reset_ir_gpio(itv); 698 ivtv_reset_ir_gpio(itv);
689 } 699 }
690 if (val & 0x02) { 700 if (val & 0x02) {
691 itv->video_dec_func(itv, cmd, 0); 701 itv->video_dec_func(itv, cmd, NULL);
692 } 702 }
693 break; 703 break;
694 } 704 }
@@ -703,8 +713,12 @@ int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void
703{ 713{
704 struct ivtv_open_id *id = NULL; 714 struct ivtv_open_id *id = NULL;
705 u32 data[CX2341X_MBOX_MAX_DATA]; 715 u32 data[CX2341X_MBOX_MAX_DATA];
716 int streamtype = 0;
706 717
707 if (filp) id = (struct ivtv_open_id *)filp->private_data; 718 if (filp) {
719 id = (struct ivtv_open_id *)filp->private_data;
720 streamtype = id->type;
721 }
708 722
709 switch (cmd) { 723 switch (cmd) {
710 case VIDIOC_G_PRIORITY: 724 case VIDIOC_G_PRIORITY:
@@ -822,6 +836,11 @@ int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void
822 cropcap->bounds.height = itv->is_50hz ? 576 : 480; 836 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
823 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10; 837 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
824 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11; 838 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
839 } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
840 cropcap->bounds.width = itv->yuv_info.osd_full_w;
841 cropcap->bounds.height = itv->yuv_info.osd_full_h;
842 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
843 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
825 } else { 844 } else {
826 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480; 845 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
827 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10; 846 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
@@ -836,10 +855,15 @@ int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void
836 855
837 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && 856 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
838 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) { 857 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
839 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4, 858 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
840 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) { 859 itv->yuv_info.main_rect = crop->c;
841 itv->main_rect = crop->c;
842 return 0; 860 return 0;
861 } else {
862 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
863 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
864 itv->main_rect = crop->c;
865 return 0;
866 }
843 } 867 }
844 return -EINVAL; 868 return -EINVAL;
845 } 869 }
@@ -853,7 +877,10 @@ int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void
853 877
854 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && 878 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
855 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) { 879 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
856 crop->c = itv->main_rect; 880 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
881 crop->c = itv->yuv_info.main_rect;
882 else
883 crop->c = itv->main_rect;
857 return 0; 884 return 0;
858 } 885 }
859 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 886 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
@@ -864,7 +891,7 @@ int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void
864 case VIDIOC_ENUM_FMT: { 891 case VIDIOC_ENUM_FMT: {
865 static struct v4l2_fmtdesc formats[] = { 892 static struct v4l2_fmtdesc formats[] = {
866 { 0, 0, 0, 893 { 0, 0, 0,
867 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, 894 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
868 { 0, 0, 0, 0 } 895 { 0, 0, 0, 0 }
869 }, 896 },
870 { 1, 0, V4L2_FMT_FLAG_COMPRESSED, 897 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
@@ -1043,6 +1070,12 @@ int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void
1043 itv->main_rect.height = itv->params.height; 1070 itv->main_rect.height = itv->params.height;
1044 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4, 1071 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1045 720, itv->main_rect.height, 0, 0); 1072 720, itv->main_rect.height, 0, 0);
1073 itv->yuv_info.main_rect = itv->main_rect;
1074 if (!itv->osd_info) {
1075 itv->yuv_info.osd_full_w = 720;
1076 itv->yuv_info.osd_full_h =
1077 itv->is_out_50hz ? 576 : 480;
1078 }
1046 } 1079 }
1047 break; 1080 break;
1048 } 1081 }
diff --git a/drivers/media/video/ivtv/ivtv-irq.c b/drivers/media/video/ivtv/ivtv-irq.c
index fd1688e4757d..65604dde9726 100644
--- a/drivers/media/video/ivtv/ivtv-irq.c
+++ b/drivers/media/video/ivtv/ivtv-irq.c
@@ -204,7 +204,7 @@ static int stream_enc_dma_append(struct ivtv_stream *s, u32 data[CX2341X_MBOX_MA
204 s->sg_pending[idx].dst = buf->dma_handle; 204 s->sg_pending[idx].dst = buf->dma_handle;
205 s->sg_pending[idx].src = offset; 205 s->sg_pending[idx].src = offset;
206 s->sg_pending[idx].size = s->buf_size; 206 s->sg_pending[idx].size = s->buf_size;
207 buf->bytesused = (size < s->buf_size) ? size : s->buf_size; 207 buf->bytesused = min(size, s->buf_size);
208 buf->dma_xfer_cnt = s->dma_xfer_cnt; 208 buf->dma_xfer_cnt = s->dma_xfer_cnt;
209 209
210 s->q_predma.bytesused += buf->bytesused; 210 s->q_predma.bytesused += buf->bytesused;
@@ -302,8 +302,11 @@ static void dma_post(struct ivtv_stream *s)
302void ivtv_dma_stream_dec_prepare(struct ivtv_stream *s, u32 offset, int lock) 302void ivtv_dma_stream_dec_prepare(struct ivtv_stream *s, u32 offset, int lock)
303{ 303{
304 struct ivtv *itv = s->itv; 304 struct ivtv *itv = s->itv;
305 struct yuv_playback_info *yi = &itv->yuv_info;
306 u8 frame = yi->draw_frame;
307 struct yuv_frame_info *f = &yi->new_frame_info[frame];
305 struct ivtv_buffer *buf; 308 struct ivtv_buffer *buf;
306 u32 y_size = itv->params.height * itv->params.width; 309 u32 y_size = 720 * ((f->src_h + 31) & ~31);
307 u32 uv_offset = offset + IVTV_YUV_BUFFER_UV_OFFSET; 310 u32 uv_offset = offset + IVTV_YUV_BUFFER_UV_OFFSET;
308 int y_done = 0; 311 int y_done = 0;
309 int bytes_written = 0; 312 int bytes_written = 0;
@@ -311,17 +314,42 @@ void ivtv_dma_stream_dec_prepare(struct ivtv_stream *s, u32 offset, int lock)
311 int idx = 0; 314 int idx = 0;
312 315
313 IVTV_DEBUG_HI_DMA("DEC PREPARE DMA %s: %08x %08x\n", s->name, s->q_predma.bytesused, offset); 316 IVTV_DEBUG_HI_DMA("DEC PREPARE DMA %s: %08x %08x\n", s->name, s->q_predma.bytesused, offset);
317
318 /* Insert buffer block for YUV if needed */
319 if (s->type == IVTV_DEC_STREAM_TYPE_YUV && f->offset_y) {
320 if (yi->blanking_dmaptr) {
321 s->sg_pending[idx].src = yi->blanking_dmaptr;
322 s->sg_pending[idx].dst = offset;
323 s->sg_pending[idx].size = 720 * 16;
324 }
325 offset += 720 * 16;
326 idx++;
327 }
328
314 list_for_each_entry(buf, &s->q_predma.list, list) { 329 list_for_each_entry(buf, &s->q_predma.list, list) {
315 /* YUV UV Offset from Y Buffer */ 330 /* YUV UV Offset from Y Buffer */
316 if (s->type == IVTV_DEC_STREAM_TYPE_YUV && !y_done && bytes_written >= y_size) { 331 if (s->type == IVTV_DEC_STREAM_TYPE_YUV && !y_done &&
332 (bytes_written + buf->bytesused) >= y_size) {
333 s->sg_pending[idx].src = buf->dma_handle;
334 s->sg_pending[idx].dst = offset;
335 s->sg_pending[idx].size = y_size - bytes_written;
317 offset = uv_offset; 336 offset = uv_offset;
337 if (s->sg_pending[idx].size != buf->bytesused) {
338 idx++;
339 s->sg_pending[idx].src =
340 buf->dma_handle + s->sg_pending[idx - 1].size;
341 s->sg_pending[idx].dst = offset;
342 s->sg_pending[idx].size =
343 buf->bytesused - s->sg_pending[idx - 1].size;
344 offset += s->sg_pending[idx].size;
345 }
318 y_done = 1; 346 y_done = 1;
347 } else {
348 s->sg_pending[idx].src = buf->dma_handle;
349 s->sg_pending[idx].dst = offset;
350 s->sg_pending[idx].size = buf->bytesused;
351 offset += buf->bytesused;
319 } 352 }
320 s->sg_pending[idx].src = buf->dma_handle;
321 s->sg_pending[idx].dst = offset;
322 s->sg_pending[idx].size = buf->bytesused;
323
324 offset += buf->bytesused;
325 bytes_written += buf->bytesused; 353 bytes_written += buf->bytesused;
326 354
327 /* Sync SG buffers */ 355 /* Sync SG buffers */
@@ -408,7 +436,7 @@ static void ivtv_dma_enc_start(struct ivtv_stream *s)
408 s_vbi->sg_pending_size = 0; 436 s_vbi->sg_pending_size = 0;
409 s_vbi->dma_xfer_cnt++; 437 s_vbi->dma_xfer_cnt++;
410 set_bit(IVTV_F_S_DMA_HAS_VBI, &s->s_flags); 438 set_bit(IVTV_F_S_DMA_HAS_VBI, &s->s_flags);
411 IVTV_DEBUG_HI_DMA("include DMA for %s\n", s->name); 439 IVTV_DEBUG_HI_DMA("include DMA for %s\n", s_vbi->name);
412 } 440 }
413 441
414 s->dma_xfer_cnt++; 442 s->dma_xfer_cnt++;
@@ -700,12 +728,15 @@ static void ivtv_irq_dec_data_req(struct ivtv *itv)
700 ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, data); 728 ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, data);
701 729
702 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags)) { 730 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags)) {
703 itv->dma_data_req_size = itv->params.width * itv->params.height * 3 / 2; 731 itv->dma_data_req_size =
704 itv->dma_data_req_offset = data[1] ? data[1] : yuv_offset[0]; 732 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
733 itv->dma_data_req_offset = data[1];
734 if (atomic_read(&itv->yuv_info.next_dma_frame) >= 0)
735 ivtv_yuv_frame_complete(itv);
705 s = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV]; 736 s = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
706 } 737 }
707 else { 738 else {
708 itv->dma_data_req_size = data[2] >= 0x10000 ? 0x10000 : data[2]; 739 itv->dma_data_req_size = min_t(u32, data[2], 0x10000);
709 itv->dma_data_req_offset = data[1]; 740 itv->dma_data_req_offset = data[1];
710 s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG]; 741 s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
711 } 742 }
@@ -715,6 +746,8 @@ static void ivtv_irq_dec_data_req(struct ivtv *itv)
715 set_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags); 746 set_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
716 } 747 }
717 else { 748 else {
749 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
750 ivtv_yuv_setup_stream_frame(itv);
718 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags); 751 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
719 ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size); 752 ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
720 ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 0); 753 ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 0);
@@ -731,24 +764,26 @@ static void ivtv_irq_vsync(struct ivtv *itv)
731 * one vsync per frame. 764 * one vsync per frame.
732 */ 765 */
733 unsigned int frame = read_reg(0x28c0) & 1; 766 unsigned int frame = read_reg(0x28c0) & 1;
767 struct yuv_playback_info *yi = &itv->yuv_info;
734 int last_dma_frame = atomic_read(&itv->yuv_info.next_dma_frame); 768 int last_dma_frame = atomic_read(&itv->yuv_info.next_dma_frame);
769 struct yuv_frame_info *f = &yi->new_frame_info[last_dma_frame];
735 770
736 if (0) IVTV_DEBUG_IRQ("DEC VSYNC\n"); 771 if (0) IVTV_DEBUG_IRQ("DEC VSYNC\n");
737 772
738 if (((frame ^ itv->yuv_info.sync_field[last_dma_frame]) == 0 && 773 if (((frame ^ f->sync_field) == 0 &&
739 ((itv->last_vsync_field & 1) ^ itv->yuv_info.sync_field[last_dma_frame])) || 774 ((itv->last_vsync_field & 1) ^ f->sync_field)) ||
740 (frame != (itv->last_vsync_field & 1) && !itv->yuv_info.frame_interlaced)) { 775 (frame != (itv->last_vsync_field & 1) && !f->interlaced)) {
741 int next_dma_frame = last_dma_frame; 776 int next_dma_frame = last_dma_frame;
742 777
743 if (!(itv->yuv_info.frame_interlaced && itv->yuv_info.field_delay[next_dma_frame] && itv->yuv_info.fields_lapsed < 1)) { 778 if (!(f->interlaced && f->delay && yi->fields_lapsed < 1)) {
744 if (next_dma_frame >= 0 && next_dma_frame != atomic_read(&itv->yuv_info.next_fill_frame)) { 779 if (next_dma_frame >= 0 && next_dma_frame != atomic_read(&yi->next_fill_frame)) {
745 write_reg(yuv_offset[next_dma_frame] >> 4, 0x82c); 780 write_reg(yuv_offset[next_dma_frame] >> 4, 0x82c);
746 write_reg((yuv_offset[next_dma_frame] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x830); 781 write_reg((yuv_offset[next_dma_frame] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x830);
747 write_reg(yuv_offset[next_dma_frame] >> 4, 0x834); 782 write_reg(yuv_offset[next_dma_frame] >> 4, 0x834);
748 write_reg((yuv_offset[next_dma_frame] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x838); 783 write_reg((yuv_offset[next_dma_frame] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x838);
749 next_dma_frame = (next_dma_frame + 1) & 0x3; 784 next_dma_frame = (next_dma_frame + 1) % IVTV_YUV_BUFFERS;
750 atomic_set(&itv->yuv_info.next_dma_frame, next_dma_frame); 785 atomic_set(&yi->next_dma_frame, next_dma_frame);
751 itv->yuv_info.fields_lapsed = -1; 786 yi->fields_lapsed = -1;
752 } 787 }
753 } 788 }
754 } 789 }
@@ -781,20 +816,22 @@ static void ivtv_irq_vsync(struct ivtv *itv)
781 } 816 }
782 817
783 /* Check if we need to update the yuv registers */ 818 /* Check if we need to update the yuv registers */
784 if ((itv->yuv_info.yuv_forced_update || itv->yuv_info.new_frame_info[last_dma_frame].update) && last_dma_frame != -1) { 819 if ((yi->yuv_forced_update || f->update) && last_dma_frame != -1) {
785 if (!itv->yuv_info.new_frame_info[last_dma_frame].update) 820 if (!f->update) {
786 last_dma_frame = (last_dma_frame - 1) & 3; 821 last_dma_frame = (u8)(last_dma_frame - 1) % IVTV_YUV_BUFFERS;
787 822 f = &yi->new_frame_info[last_dma_frame];
788 if (itv->yuv_info.new_frame_info[last_dma_frame].src_w) { 823 }
789 itv->yuv_info.update_frame = last_dma_frame; 824
790 itv->yuv_info.new_frame_info[last_dma_frame].update = 0; 825 if (f->src_w) {
791 itv->yuv_info.yuv_forced_update = 0; 826 yi->update_frame = last_dma_frame;
827 f->update = 0;
828 yi->yuv_forced_update = 0;
792 set_bit(IVTV_F_I_WORK_HANDLER_YUV, &itv->i_flags); 829 set_bit(IVTV_F_I_WORK_HANDLER_YUV, &itv->i_flags);
793 set_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags); 830 set_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags);
794 } 831 }
795 } 832 }
796 833
797 itv->yuv_info.fields_lapsed ++; 834 yi->fields_lapsed++;
798 } 835 }
799} 836}
800 837
diff --git a/drivers/media/video/ivtv/ivtv-mailbox.c b/drivers/media/video/ivtv/ivtv-mailbox.c
index b05436da7136..13a6c374d2db 100644
--- a/drivers/media/video/ivtv/ivtv-mailbox.c
+++ b/drivers/media/video/ivtv/ivtv-mailbox.c
@@ -333,7 +333,7 @@ int ivtv_api(struct ivtv *itv, int cmd, int args, u32 data[])
333 return (res == -EBUSY) ? ivtv_api_call(itv, cmd, args, data) : res; 333 return (res == -EBUSY) ? ivtv_api_call(itv, cmd, args, data) : res;
334} 334}
335 335
336int ivtv_api_func(void *priv, int cmd, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA]) 336int ivtv_api_func(void *priv, u32 cmd, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
337{ 337{
338 return ivtv_api(priv, cmd, in, data); 338 return ivtv_api(priv, cmd, in, data);
339} 339}
diff --git a/drivers/media/video/ivtv/ivtv-mailbox.h b/drivers/media/video/ivtv/ivtv-mailbox.h
index 71a54eef8fc7..6ef12091e3f3 100644
--- a/drivers/media/video/ivtv/ivtv-mailbox.h
+++ b/drivers/media/video/ivtv/ivtv-mailbox.h
@@ -28,6 +28,6 @@ void ivtv_api_get_data(struct ivtv_mailbox_data *mbox, int mb, u32 data[]);
28int ivtv_api(struct ivtv *itv, int cmd, int args, u32 data[]); 28int ivtv_api(struct ivtv *itv, int cmd, int args, u32 data[]);
29int ivtv_vapi_result(struct ivtv *itv, u32 data[CX2341X_MBOX_MAX_DATA], int cmd, int args, ...); 29int ivtv_vapi_result(struct ivtv *itv, u32 data[CX2341X_MBOX_MAX_DATA], int cmd, int args, ...);
30int ivtv_vapi(struct ivtv *itv, int cmd, int args, ...); 30int ivtv_vapi(struct ivtv *itv, int cmd, int args, ...);
31int ivtv_api_func(void *priv, int cmd, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA]); 31int ivtv_api_func(void *priv, u32 cmd, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA]);
32 32
33#endif 33#endif
diff --git a/drivers/media/video/ivtv/ivtv-routing.c b/drivers/media/video/ivtv/ivtv-routing.c
index 398bd33033ed..05564919b57f 100644
--- a/drivers/media/video/ivtv/ivtv-routing.c
+++ b/drivers/media/video/ivtv/ivtv-routing.c
@@ -25,6 +25,7 @@
25#include "ivtv-routing.h" 25#include "ivtv-routing.h"
26 26
27#include <media/msp3400.h> 27#include <media/msp3400.h>
28#include <media/m52790.h>
28#include <media/upd64031a.h> 29#include <media/upd64031a.h>
29#include <media/upd64083.h> 30#include <media/upd64083.h>
30 31
@@ -32,28 +33,26 @@
32 settings. */ 33 settings. */
33void ivtv_audio_set_io(struct ivtv *itv) 34void ivtv_audio_set_io(struct ivtv *itv)
34{ 35{
36 const struct ivtv_card_audio_input *in;
35 struct v4l2_routing route; 37 struct v4l2_routing route;
36 u32 audio_input;
37 int mux_input;
38 38
39 /* Determine which input to use */ 39 /* Determine which input to use */
40 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) { 40 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags))
41 audio_input = itv->card->radio_input.audio_input; 41 in = &itv->card->radio_input;
42 mux_input = itv->card->radio_input.muxer_input; 42 else
43 } else { 43 in = &itv->card->audio_inputs[itv->audio_input];
44 audio_input = itv->card->audio_inputs[itv->audio_input].audio_input;
45 mux_input = itv->card->audio_inputs[itv->audio_input].muxer_input;
46 }
47 44
48 /* handle muxer chips */ 45 /* handle muxer chips */
49 route.input = mux_input; 46 route.input = in->muxer_input;
50 route.output = 0; 47 route.output = 0;
48 if (itv->card->hw_muxer & IVTV_HW_M52790)
49 route.output = M52790_OUT_STEREO;
51 ivtv_i2c_hw(itv, itv->card->hw_muxer, VIDIOC_INT_S_AUDIO_ROUTING, &route); 50 ivtv_i2c_hw(itv, itv->card->hw_muxer, VIDIOC_INT_S_AUDIO_ROUTING, &route);
52 51
53 route.input = audio_input; 52 route.input = in->audio_input;
54 if (itv->card->hw_audio & IVTV_HW_MSP34XX) { 53 route.output = 0;
54 if (itv->card->hw_audio & IVTV_HW_MSP34XX)
55 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1); 55 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
56 }
57 ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, &route); 56 ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, &route);
58} 57}
59 58
diff --git a/drivers/media/video/ivtv/ivtv-streams.c b/drivers/media/video/ivtv/ivtv-streams.c
index 74fb0e021979..24d98ecf35ad 100644
--- a/drivers/media/video/ivtv/ivtv-streams.c
+++ b/drivers/media/video/ivtv/ivtv-streams.c
@@ -43,7 +43,7 @@
43#include "ivtv-cards.h" 43#include "ivtv-cards.h"
44#include "ivtv-streams.h" 44#include "ivtv-streams.h"
45 45
46static struct file_operations ivtv_v4l2_enc_fops = { 46static const struct file_operations ivtv_v4l2_enc_fops = {
47 .owner = THIS_MODULE, 47 .owner = THIS_MODULE,
48 .read = ivtv_v4l2_read, 48 .read = ivtv_v4l2_read,
49 .write = ivtv_v4l2_write, 49 .write = ivtv_v4l2_write,
@@ -53,7 +53,7 @@ static struct file_operations ivtv_v4l2_enc_fops = {
53 .poll = ivtv_v4l2_enc_poll, 53 .poll = ivtv_v4l2_enc_poll,
54}; 54};
55 55
56static struct file_operations ivtv_v4l2_dec_fops = { 56static const struct file_operations ivtv_v4l2_dec_fops = {
57 .owner = THIS_MODULE, 57 .owner = THIS_MODULE,
58 .read = ivtv_v4l2_read, 58 .read = ivtv_v4l2_read,
59 .write = ivtv_v4l2_write, 59 .write = ivtv_v4l2_write,
@@ -572,10 +572,10 @@ int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
572 clear_bit(IVTV_F_I_EOS, &itv->i_flags); 572 clear_bit(IVTV_F_I_EOS, &itv->i_flags);
573 573
574 /* Initialize Digitizer for Capture */ 574 /* Initialize Digitizer for Capture */
575 itv->video_dec_func(itv, VIDIOC_STREAMOFF, 0); 575 itv->video_dec_func(itv, VIDIOC_STREAMOFF, NULL);
576 ivtv_msleep_timeout(300, 1); 576 ivtv_msleep_timeout(300, 1);
577 ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0); 577 ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
578 itv->video_dec_func(itv, VIDIOC_STREAMON, 0); 578 itv->video_dec_func(itv, VIDIOC_STREAMON, NULL);
579 } 579 }
580 580
581 /* begin_capture */ 581 /* begin_capture */
@@ -661,27 +661,12 @@ int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset)
661 661
662 IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset); 662 IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset);
663 663
664 /* Clear Streamoff */
665 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
666 /* Initialize Decoder */
667 /* Reprogram Decoder YUV Buffers for YUV */
668 write_reg(yuv_offset[0] >> 4, 0x82c);
669 write_reg((yuv_offset[0] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x830);
670 write_reg(yuv_offset[0] >> 4, 0x834);
671 write_reg((yuv_offset[0] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x838);
672
673 write_reg_sync(0x00000000 | (0x0c << 16) | (0x0b << 8), 0x2d24);
674
675 write_reg_sync(0x00108080, 0x2898);
676 /* Enable YUV decoder output */
677 write_reg_sync(0x01, IVTV_REG_VDM);
678 }
679
680 ivtv_setup_v4l2_decode_stream(s); 664 ivtv_setup_v4l2_decode_stream(s);
681 665
682 /* set dma size to 65536 bytes */ 666 /* set dma size to 65536 bytes */
683 ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536); 667 ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536);
684 668
669 /* Clear Streamoff */
685 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); 670 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
686 671
687 /* Zero out decoder counters */ 672 /* Zero out decoder counters */
diff --git a/drivers/media/video/ivtv/ivtv-version.h b/drivers/media/video/ivtv/ivtv-version.h
index d050de2a7229..0f1d4cc4b4d9 100644
--- a/drivers/media/video/ivtv/ivtv-version.h
+++ b/drivers/media/video/ivtv/ivtv-version.h
@@ -22,7 +22,7 @@
22 22
23#define IVTV_DRIVER_NAME "ivtv" 23#define IVTV_DRIVER_NAME "ivtv"
24#define IVTV_DRIVER_VERSION_MAJOR 1 24#define IVTV_DRIVER_VERSION_MAJOR 1
25#define IVTV_DRIVER_VERSION_MINOR 1 25#define IVTV_DRIVER_VERSION_MINOR 2
26#define IVTV_DRIVER_VERSION_PATCHLEVEL 0 26#define IVTV_DRIVER_VERSION_PATCHLEVEL 0
27 27
28#define IVTV_VERSION __stringify(IVTV_DRIVER_VERSION_MAJOR) "." __stringify(IVTV_DRIVER_VERSION_MINOR) "." __stringify(IVTV_DRIVER_VERSION_PATCHLEVEL) 28#define IVTV_VERSION __stringify(IVTV_DRIVER_VERSION_MAJOR) "." __stringify(IVTV_DRIVER_VERSION_MINOR) "." __stringify(IVTV_DRIVER_VERSION_PATCHLEVEL)
diff --git a/drivers/media/video/ivtv/ivtv-yuv.c b/drivers/media/video/ivtv/ivtv-yuv.c
index 9091c4837bbc..85183480a225 100644
--- a/drivers/media/video/ivtv/ivtv-yuv.c
+++ b/drivers/media/video/ivtv/ivtv-yuv.c
@@ -22,32 +22,37 @@
22#include "ivtv-udma.h" 22#include "ivtv-udma.h"
23#include "ivtv-yuv.h" 23#include "ivtv-yuv.h"
24 24
25const u32 yuv_offset[4] = { 25/* YUV buffer offsets */
26 IVTV_YUV_BUFFER_OFFSET, 26const u32 yuv_offset[IVTV_YUV_BUFFERS] = {
27 IVTV_YUV_BUFFER_OFFSET_1, 27 0x001a8600,
28 IVTV_YUV_BUFFER_OFFSET_2, 28 0x00240400,
29 IVTV_YUV_BUFFER_OFFSET_3 29 0x002d8200,
30 0x00370000,
31 0x00029000,
32 0x000C0E00,
33 0x006B0400,
34 0x00748200
30}; 35};
31 36
32static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma, 37static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
33 struct ivtv_dma_frame *args) 38 struct ivtv_dma_frame *args)
34{ 39{
35 struct ivtv_dma_page_info y_dma; 40 struct ivtv_dma_page_info y_dma;
36 struct ivtv_dma_page_info uv_dma; 41 struct ivtv_dma_page_info uv_dma;
37 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];
38 int i; 45 int i;
39 int y_pages, uv_pages; 46 int y_pages, uv_pages;
40
41 unsigned long y_buffer_offset, uv_buffer_offset; 47 unsigned long y_buffer_offset, uv_buffer_offset;
42 int y_decode_height, uv_decode_height, y_size; 48 int y_decode_height, uv_decode_height, y_size;
43 int frame = atomic_read(&itv->yuv_info.next_fill_frame);
44 49
45 y_buffer_offset = IVTV_DECODER_OFFSET + yuv_offset[frame]; 50 y_buffer_offset = IVTV_DECODER_OFFSET + yuv_offset[frame];
46 uv_buffer_offset = y_buffer_offset + IVTV_YUV_BUFFER_UV_OFFSET; 51 uv_buffer_offset = y_buffer_offset + IVTV_YUV_BUFFER_UV_OFFSET;
47 52
48 y_decode_height = uv_decode_height = args->src.height + args->src.top; 53 y_decode_height = uv_decode_height = f->src_h + f->src_y;
49 54
50 if (y_decode_height < 512-16) 55 if (f->offset_y)
51 y_buffer_offset += 720 * 16; 56 y_buffer_offset += 720 * 16;
52 57
53 if (y_decode_height & 15) 58 if (y_decode_height & 15)
@@ -60,8 +65,9 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
60 65
61 /* Still in USE */ 66 /* Still in USE */
62 if (dma->SG_length || dma->page_count) { 67 if (dma->SG_length || dma->page_count) {
63 IVTV_DEBUG_WARN("prep_user_dma: SG_length %d page_count %d still full?\n", 68 IVTV_DEBUG_WARN
64 dma->SG_length, dma->page_count); 69 ("prep_user_dma: SG_length %d page_count %d still full?\n",
70 dma->SG_length, dma->page_count);
65 return -EBUSY; 71 return -EBUSY;
66 } 72 }
67 73
@@ -77,8 +83,9 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
77 dma->page_count = y_dma.page_count + uv_dma.page_count; 83 dma->page_count = y_dma.page_count + uv_dma.page_count;
78 84
79 if (y_pages + uv_pages != dma->page_count) { 85 if (y_pages + uv_pages != dma->page_count) {
80 IVTV_DEBUG_WARN("failed to map user pages, returned %d instead of %d\n", 86 IVTV_DEBUG_WARN
81 y_pages + uv_pages, dma->page_count); 87 ("failed to map user pages, returned %d instead of %d\n",
88 y_pages + uv_pages, dma->page_count);
82 89
83 for (i = 0; i < dma->page_count; i++) { 90 for (i = 0; i < dma->page_count; i++) {
84 put_page(dma->map[i]); 91 put_page(dma->map[i]);
@@ -99,16 +106,14 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
99 dma->SG_length = pci_map_sg(itv->dev, dma->SGlist, dma->page_count, PCI_DMA_TODEVICE); 106 dma->SG_length = pci_map_sg(itv->dev, dma->SGlist, dma->page_count, PCI_DMA_TODEVICE);
100 107
101 /* Fill SG Array with new values */ 108 /* Fill SG Array with new values */
102 ivtv_udma_fill_sg_array (dma, y_buffer_offset, uv_buffer_offset, y_size); 109 ivtv_udma_fill_sg_array(dma, y_buffer_offset, uv_buffer_offset, y_size);
103 110
104 /* If we've offset the y plane, ensure top area is blanked */ 111 /* If we've offset the y plane, ensure top area is blanked */
105 if (args->src.height + args->src.top < 512-16) { 112 if (f->offset_y && yi->blanking_dmaptr) {
106 if (itv->yuv_info.blanking_dmaptr) { 113 dma->SGarray[dma->SG_length].size = cpu_to_le32(720*16);
107 dma->SGarray[dma->SG_length].size = cpu_to_le32(720*16); 114 dma->SGarray[dma->SG_length].src = cpu_to_le32(yi->blanking_dmaptr);
108 dma->SGarray[dma->SG_length].src = cpu_to_le32(itv->yuv_info.blanking_dmaptr); 115 dma->SGarray[dma->SG_length].dst = cpu_to_le32(IVTV_DECODER_OFFSET + yuv_offset[frame]);
109 dma->SGarray[dma->SG_length].dst = cpu_to_le32(IVTV_DECODER_OFFSET + yuv_offset[frame]); 116 dma->SG_length++;
110 dma->SG_length++;
111 }
112 } 117 }
113 118
114 /* Tag SG Array with Interrupt Bit */ 119 /* Tag SG Array with Interrupt Bit */
@@ -121,11 +126,11 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
121/* We rely on a table held in the firmware - Quick check. */ 126/* We rely on a table held in the firmware - Quick check. */
122int ivtv_yuv_filter_check(struct ivtv *itv) 127int ivtv_yuv_filter_check(struct ivtv *itv)
123{ 128{
124 int i, offset_y, offset_uv; 129 int i, y, uv;
125 130
126 for (i=0, offset_y = 16, offset_uv = 4; i<16; i++, offset_y += 24, offset_uv += 12) { 131 for (i = 0, y = 16, uv = 4; i < 16; i++, y += 24, uv += 12) {
127 if ((read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + offset_y) != i << 16) || 132 if ((read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + y) != i << 16) ||
128 (read_dec(IVTV_YUV_VERTICAL_FILTER_OFFSET + offset_uv) != i << 16)) { 133 (read_dec(IVTV_YUV_VERTICAL_FILTER_OFFSET + uv) != i << 16)) {
129 IVTV_WARN ("YUV filter table not found in firmware.\n"); 134 IVTV_WARN ("YUV filter table not found in firmware.\n");
130 return -1; 135 return -1;
131 } 136 }
@@ -135,69 +140,67 @@ int ivtv_yuv_filter_check(struct ivtv *itv)
135 140
136static void ivtv_yuv_filter(struct ivtv *itv, int h_filter, int v_filter_1, int v_filter_2) 141static void ivtv_yuv_filter(struct ivtv *itv, int h_filter, int v_filter_1, int v_filter_2)
137{ 142{
138 int filter_index, filter_line; 143 u32 i, line;
139 144
140 /* If any filter is -1, then don't update it */ 145 /* If any filter is -1, then don't update it */
141 if (h_filter > -1) { 146 if (h_filter > -1) {
142 if (h_filter > 4) h_filter = 4; 147 if (h_filter > 4)
143 filter_index = h_filter * 384; 148 h_filter = 4;
144 filter_line = 0; 149 i = IVTV_YUV_HORIZONTAL_FILTER_OFFSET + (h_filter * 384);
145 while (filter_line < 16) { 150 for (line = 0; line < 16; line++) {
146 write_reg(read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + filter_index), 0x02804); 151 write_reg(read_dec(i), 0x02804);
147 write_reg(read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + filter_index), 0x0281c); 152 write_reg(read_dec(i), 0x0281c);
148 filter_index += 4; 153 i += 4;
149 write_reg(read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + filter_index), 0x02808); 154 write_reg(read_dec(i), 0x02808);
150 write_reg(read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + filter_index), 0x02820); 155 write_reg(read_dec(i), 0x02820);
151 filter_index += 4; 156 i += 4;
152 write_reg(read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + filter_index), 0x0280c); 157 write_reg(read_dec(i), 0x0280c);
153 write_reg(read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + filter_index), 0x02824); 158 write_reg(read_dec(i), 0x02824);
154 filter_index += 4; 159 i += 4;
155 write_reg(read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + filter_index), 0x02810); 160 write_reg(read_dec(i), 0x02810);
156 write_reg(read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + filter_index), 0x02828); 161 write_reg(read_dec(i), 0x02828);
157 filter_index += 4; 162 i += 4;
158 write_reg(read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + filter_index), 0x02814); 163 write_reg(read_dec(i), 0x02814);
159 write_reg(read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + filter_index), 0x0282c); 164 write_reg(read_dec(i), 0x0282c);
160 filter_index += 8; 165 i += 8;
161 write_reg(0, 0x02818); 166 write_reg(0, 0x02818);
162 write_reg(0, 0x02830); 167 write_reg(0, 0x02830);
163 filter_line ++;
164 } 168 }
165 IVTV_DEBUG_YUV("h_filter -> %d\n",h_filter); 169 IVTV_DEBUG_YUV("h_filter -> %d\n", h_filter);
166 } 170 }
167 171
168 if (v_filter_1 > -1) { 172 if (v_filter_1 > -1) {
169 if (v_filter_1 > 4) v_filter_1 = 4; 173 if (v_filter_1 > 4)
170 filter_index = v_filter_1 * 192; 174 v_filter_1 = 4;
171 filter_line = 0; 175 i = IVTV_YUV_VERTICAL_FILTER_OFFSET + (v_filter_1 * 192);
172 while (filter_line < 16) { 176 for (line = 0; line < 16; line++) {
173 write_reg(read_dec(IVTV_YUV_VERTICAL_FILTER_OFFSET + filter_index), 0x02900); 177 write_reg(read_dec(i), 0x02900);
174 filter_index += 4; 178 i += 4;
175 write_reg(read_dec(IVTV_YUV_VERTICAL_FILTER_OFFSET + filter_index), 0x02904); 179 write_reg(read_dec(i), 0x02904);
176 filter_index += 8; 180 i += 8;
177 write_reg(0, 0x02908); 181 write_reg(0, 0x02908);
178 filter_line ++;
179 } 182 }
180 IVTV_DEBUG_YUV("v_filter_1 -> %d\n",v_filter_1); 183 IVTV_DEBUG_YUV("v_filter_1 -> %d\n", v_filter_1);
181 } 184 }
182 185
183 if (v_filter_2 > -1) { 186 if (v_filter_2 > -1) {
184 if (v_filter_2 > 4) v_filter_2 = 4; 187 if (v_filter_2 > 4)
185 filter_index = v_filter_2 * 192; 188 v_filter_2 = 4;
186 filter_line = 0; 189 i = IVTV_YUV_VERTICAL_FILTER_OFFSET + (v_filter_2 * 192);
187 while (filter_line < 16) { 190 for (line = 0; line < 16; line++) {
188 write_reg(read_dec(IVTV_YUV_VERTICAL_FILTER_OFFSET + filter_index), 0x0290c); 191 write_reg(read_dec(i), 0x0290c);
189 filter_index += 4; 192 i += 4;
190 write_reg(read_dec(IVTV_YUV_VERTICAL_FILTER_OFFSET + filter_index), 0x02910); 193 write_reg(read_dec(i), 0x02910);
191 filter_index += 8; 194 i += 8;
192 write_reg(0, 0x02914); 195 write_reg(0, 0x02914);
193 filter_line ++;
194 } 196 }
195 IVTV_DEBUG_YUV("v_filter_2 -> %d\n",v_filter_2); 197 IVTV_DEBUG_YUV("v_filter_2 -> %d\n", v_filter_2);
196 } 198 }
197} 199}
198 200
199static void ivtv_yuv_handle_horizontal(struct ivtv *itv, struct yuv_frame_info *window) 201static void ivtv_yuv_handle_horizontal(struct ivtv *itv, struct yuv_frame_info *f)
200{ 202{
203 struct yuv_playback_info *yi = &itv->yuv_info;
201 u32 reg_2834, reg_2838, reg_283c; 204 u32 reg_2834, reg_2838, reg_283c;
202 u32 reg_2844, reg_2854, reg_285c; 205 u32 reg_2844, reg_2854, reg_285c;
203 u32 reg_2864, reg_2874, reg_2890; 206 u32 reg_2864, reg_2874, reg_2890;
@@ -206,18 +209,19 @@ static void ivtv_yuv_handle_horizontal(struct ivtv *itv, struct yuv_frame_info *
206 int h_filter; 209 int h_filter;
207 u32 master_width; 210 u32 master_width;
208 211
209 IVTV_DEBUG_WARN( "Need to adjust to width %d src_w %d dst_w %d src_x %d dst_x %d\n", 212 IVTV_DEBUG_WARN
210 window->tru_w, window->src_w, window->dst_w,window->src_x, window->dst_x); 213 ("Adjust to width %d src_w %d dst_w %d src_x %d dst_x %d\n",
214 f->tru_w, f->src_w, f->dst_w, f->src_x, f->dst_x);
211 215
212 /* How wide is the src image */ 216 /* How wide is the src image */
213 x_cutoff = window->src_w + window->src_x; 217 x_cutoff = f->src_w + f->src_x;
214 218
215 /* Set the display width */ 219 /* Set the display width */
216 reg_2834 = window->dst_w; 220 reg_2834 = f->dst_w;
217 reg_2838 = reg_2834; 221 reg_2838 = reg_2834;
218 222
219 /* Set the display position */ 223 /* Set the display position */
220 reg_2890 = window->dst_x; 224 reg_2890 = f->dst_x;
221 225
222 /* Index into the image horizontally */ 226 /* Index into the image horizontally */
223 reg_2870 = 0; 227 reg_2870 = 0;
@@ -228,32 +232,31 @@ static void ivtv_yuv_handle_horizontal(struct ivtv *itv, struct yuv_frame_info *
228 Gradually adjust the offset to avoid the video 'snapping' 232 Gradually adjust the offset to avoid the video 'snapping'
229 left/right if it gets dragged through this region. 233 left/right if it gets dragged through this region.
230 Only do this if osd is full width. */ 234 Only do this if osd is full width. */
231 if (window->vis_w == 720) { 235 if (f->vis_w == 720) {
232 if ((window->tru_x - window->pan_x > -1) && (window->tru_x - window->pan_x <= 40) && (window->dst_w >= 680)){ 236 if ((f->tru_x - f->pan_x > -1) && (f->tru_x - f->pan_x <= 40) && (f->dst_w >= 680))
233 reg_2870 = 10 - (window->tru_x - window->pan_x) / 4; 237 reg_2870 = 10 - (f->tru_x - f->pan_x) / 4;
234 } 238 else if ((f->tru_x - f->pan_x < 0) && (f->tru_x - f->pan_x >= -20) && (f->dst_w >= 660))
235 else if ((window->tru_x - window->pan_x < 0) && (window->tru_x - window->pan_x >= -20) && (window->dst_w >= 660)) { 239 reg_2870 = (10 + (f->tru_x - f->pan_x) / 2);
236 reg_2870 = (10 + (window->tru_x - window->pan_x) / 2);
237 }
238 240
239 if (window->dst_w >= window->src_w) 241 if (f->dst_w >= f->src_w)
240 reg_2870 = reg_2870 << 16 | reg_2870; 242 reg_2870 = reg_2870 << 16 | reg_2870;
241 else 243 else
242 reg_2870 = ((reg_2870 & ~1) << 15) | (reg_2870 & ~1); 244 reg_2870 = ((reg_2870 & ~1) << 15) | (reg_2870 & ~1);
243 } 245 }
244 246
245 if (window->dst_w < window->src_w) 247 if (f->dst_w < f->src_w)
246 reg_2870 = 0x000d000e - reg_2870; 248 reg_2870 = 0x000d000e - reg_2870;
247 else 249 else
248 reg_2870 = 0x0012000e - reg_2870; 250 reg_2870 = 0x0012000e - reg_2870;
249 251
250 /* We're also using 2870 to shift the image left (src_x & negative dst_x) */ 252 /* We're also using 2870 to shift the image left (src_x & negative dst_x) */
251 reg_2870_offset = (window->src_x*((window->dst_w << 21)/window->src_w))>>19; 253 reg_2870_offset = (f->src_x * ((f->dst_w << 21) / f->src_w)) >> 19;
252 254
253 if (window->dst_w >= window->src_w) { 255 if (f->dst_w >= f->src_w) {
254 x_cutoff &= ~1; 256 x_cutoff &= ~1;
255 master_width = (window->src_w * 0x00200000) / (window->dst_w); 257 master_width = (f->src_w * 0x00200000) / (f->dst_w);
256 if (master_width * window->dst_w != window->src_w * 0x00200000) master_width ++; 258 if (master_width * f->dst_w != f->src_w * 0x00200000)
259 master_width++;
257 reg_2834 = (reg_2834 << 16) | x_cutoff; 260 reg_2834 = (reg_2834 << 16) | x_cutoff;
258 reg_2838 = (reg_2838 << 16) | x_cutoff; 261 reg_2838 = (reg_2838 << 16) | x_cutoff;
259 reg_283c = master_width >> 2; 262 reg_283c = master_width >> 2;
@@ -264,17 +267,17 @@ static void ivtv_yuv_handle_horizontal(struct ivtv *itv, struct yuv_frame_info *
264 267
265 /* We also need to factor in the scaling 268 /* We also need to factor in the scaling
266 (src_w - dst_w) / (src_w / 4) */ 269 (src_w - dst_w) / (src_w / 4) */
267 if (window->dst_w > window->src_w) 270 if (f->dst_w > f->src_w)
268 reg_2870_base = ((window->dst_w - window->src_w)<<16) / (window->src_w <<14); 271 reg_2870_base = ((f->dst_w - f->src_w)<<16) / (f->src_w <<14);
269 else 272 else
270 reg_2870_base = 0; 273 reg_2870_base = 0;
271 274
272 reg_2870 += (((reg_2870_offset << 14) & 0xFFFF0000) | reg_2870_offset >> 2) + (reg_2870_base << 17 | reg_2870_base); 275 reg_2870 += (((reg_2870_offset << 14) & 0xFFFF0000) | reg_2870_offset >> 2) + (reg_2870_base << 17 | reg_2870_base);
273 reg_2874 = 0; 276 reg_2874 = 0;
274 } 277 } else if (f->dst_w < f->src_w / 2) {
275 else if (window->dst_w < window->src_w / 2) { 278 master_width = (f->src_w * 0x00080000) / f->dst_w;
276 master_width = (window->src_w * 0x00080000) / window->dst_w; 279 if (master_width * f->dst_w != f->src_w * 0x00080000)
277 if (master_width * window->dst_w != window->src_w * 0x00080000) master_width ++; 280 master_width++;
278 reg_2834 = (reg_2834 << 16) | x_cutoff; 281 reg_2834 = (reg_2834 << 16) | x_cutoff;
279 reg_2838 = (reg_2838 << 16) | x_cutoff; 282 reg_2838 = (reg_2838 << 16) | x_cutoff;
280 reg_283c = master_width >> 2; 283 reg_283c = master_width >> 2;
@@ -282,13 +285,13 @@ static void ivtv_yuv_handle_horizontal(struct ivtv *itv, struct yuv_frame_info *
282 reg_2854 = master_width; 285 reg_2854 = master_width;
283 reg_285c = master_width >> 1; 286 reg_285c = master_width >> 1;
284 reg_2864 = master_width >> 1; 287 reg_2864 = master_width >> 1;
285 reg_2870 += (((reg_2870_offset << 15) & 0xFFFF0000) | reg_2870_offset); 288 reg_2870 += ((reg_2870_offset << 15) & 0xFFFF0000) | reg_2870_offset;
286 reg_2870 += (5 - (((window->src_w + window->src_w / 2) - 1) / window->dst_w)) << 16; 289 reg_2870 += (5 - (((f->src_w + f->src_w / 2) - 1) / f->dst_w)) << 16;
287 reg_2874 = 0x00000012; 290 reg_2874 = 0x00000012;
288 } 291 } else {
289 else { 292 master_width = (f->src_w * 0x00100000) / f->dst_w;
290 master_width = (window->src_w * 0x00100000) / window->dst_w; 293 if (master_width * f->dst_w != f->src_w * 0x00100000)
291 if (master_width * window->dst_w != window->src_w * 0x00100000) master_width ++; 294 master_width++;
292 reg_2834 = (reg_2834 << 16) | x_cutoff; 295 reg_2834 = (reg_2834 << 16) | x_cutoff;
293 reg_2838 = (reg_2838 << 16) | x_cutoff; 296 reg_2838 = (reg_2838 << 16) | x_cutoff;
294 reg_283c = master_width >> 2; 297 reg_283c = master_width >> 2;
@@ -296,62 +299,70 @@ static void ivtv_yuv_handle_horizontal(struct ivtv *itv, struct yuv_frame_info *
296 reg_2854 = master_width; 299 reg_2854 = master_width;
297 reg_285c = master_width >> 1; 300 reg_285c = master_width >> 1;
298 reg_2864 = master_width >> 1; 301 reg_2864 = master_width >> 1;
299 reg_2870 += (((reg_2870_offset << 14) & 0xFFFF0000) | reg_2870_offset >> 1); 302 reg_2870 += ((reg_2870_offset << 14) & 0xFFFF0000) | reg_2870_offset >> 1;
300 reg_2870 += (5 - (((window->src_w * 3) - 1) / window->dst_w)) << 16; 303 reg_2870 += (5 - (((f->src_w * 3) - 1) / f->dst_w)) << 16;
301 reg_2874 = 0x00000001; 304 reg_2874 = 0x00000001;
302 } 305 }
303 306
304 /* Select the horizontal filter */ 307 /* Select the horizontal filter */
305 if (window->src_w == window->dst_w) { 308 if (f->src_w == f->dst_w) {
306 /* An exact size match uses filter 0 */ 309 /* An exact size match uses filter 0 */
307 h_filter = 0; 310 h_filter = 0;
308 } 311 } else {
309 else {
310 /* Figure out which filter to use */ 312 /* Figure out which filter to use */
311 h_filter = ((window->src_w << 16) / window->dst_w) >> 15; 313 h_filter = ((f->src_w << 16) / f->dst_w) >> 15;
312 h_filter = (h_filter >> 1) + (h_filter & 1); 314 h_filter = (h_filter >> 1) + (h_filter & 1);
313 /* Only an exact size match can use filter 0 */ 315 /* Only an exact size match can use filter 0 */
314 if (h_filter == 0) h_filter = 1; 316 h_filter += !h_filter;
315 } 317 }
316 318
317 write_reg(reg_2834, 0x02834); 319 write_reg(reg_2834, 0x02834);
318 write_reg(reg_2838, 0x02838); 320 write_reg(reg_2838, 0x02838);
319 IVTV_DEBUG_YUV("Update reg 0x2834 %08x->%08x 0x2838 %08x->%08x\n",itv->yuv_info.reg_2834, reg_2834, itv->yuv_info.reg_2838, reg_2838); 321 IVTV_DEBUG_YUV("Update reg 0x2834 %08x->%08x 0x2838 %08x->%08x\n",
322 yi->reg_2834, reg_2834, yi->reg_2838, reg_2838);
320 323
321 write_reg(reg_283c, 0x0283c); 324 write_reg(reg_283c, 0x0283c);
322 write_reg(reg_2844, 0x02844); 325 write_reg(reg_2844, 0x02844);
323 326
324 IVTV_DEBUG_YUV("Update reg 0x283c %08x->%08x 0x2844 %08x->%08x\n",itv->yuv_info.reg_283c, reg_283c, itv->yuv_info.reg_2844, reg_2844); 327 IVTV_DEBUG_YUV("Update reg 0x283c %08x->%08x 0x2844 %08x->%08x\n",
328 yi->reg_283c, reg_283c, yi->reg_2844, reg_2844);
325 329
326 write_reg(0x00080514, 0x02840); 330 write_reg(0x00080514, 0x02840);
327 write_reg(0x00100514, 0x02848); 331 write_reg(0x00100514, 0x02848);
328 IVTV_DEBUG_YUV("Update reg 0x2840 %08x->%08x 0x2848 %08x->%08x\n",itv->yuv_info.reg_2840, 0x00080514, itv->yuv_info.reg_2848, 0x00100514); 332 IVTV_DEBUG_YUV("Update reg 0x2840 %08x->%08x 0x2848 %08x->%08x\n",
333 yi->reg_2840, 0x00080514, yi->reg_2848, 0x00100514);
329 334
330 write_reg(reg_2854, 0x02854); 335 write_reg(reg_2854, 0x02854);
331 IVTV_DEBUG_YUV("Update reg 0x2854 %08x->%08x \n",itv->yuv_info.reg_2854, reg_2854); 336 IVTV_DEBUG_YUV("Update reg 0x2854 %08x->%08x \n",
337 yi->reg_2854, reg_2854);
332 338
333 write_reg(reg_285c, 0x0285c); 339 write_reg(reg_285c, 0x0285c);
334 write_reg(reg_2864, 0x02864); 340 write_reg(reg_2864, 0x02864);
335 IVTV_DEBUG_YUV("Update reg 0x285c %08x->%08x 0x2864 %08x->%08x\n",itv->yuv_info.reg_285c, reg_285c, itv->yuv_info.reg_2864, reg_2864); 341 IVTV_DEBUG_YUV("Update reg 0x285c %08x->%08x 0x2864 %08x->%08x\n",
342 yi->reg_285c, reg_285c, yi->reg_2864, reg_2864);
336 343
337 write_reg(reg_2874, 0x02874); 344 write_reg(reg_2874, 0x02874);
338 IVTV_DEBUG_YUV("Update reg 0x2874 %08x->%08x\n",itv->yuv_info.reg_2874, reg_2874); 345 IVTV_DEBUG_YUV("Update reg 0x2874 %08x->%08x\n",
346 yi->reg_2874, reg_2874);
339 347
340 write_reg(reg_2870, 0x02870); 348 write_reg(reg_2870, 0x02870);
341 IVTV_DEBUG_YUV("Update reg 0x2870 %08x->%08x\n",itv->yuv_info.reg_2870, reg_2870); 349 IVTV_DEBUG_YUV("Update reg 0x2870 %08x->%08x\n",
350 yi->reg_2870, reg_2870);
342 351
343 write_reg( reg_2890,0x02890); 352 write_reg(reg_2890, 0x02890);
344 IVTV_DEBUG_YUV("Update reg 0x2890 %08x->%08x\n",itv->yuv_info.reg_2890, reg_2890); 353 IVTV_DEBUG_YUV("Update reg 0x2890 %08x->%08x\n",
354 yi->reg_2890, reg_2890);
345 355
346 /* Only update the filter if we really need to */ 356 /* Only update the filter if we really need to */
347 if (h_filter != itv->yuv_info.h_filter) { 357 if (h_filter != yi->h_filter) {
348 ivtv_yuv_filter (itv,h_filter,-1,-1); 358 ivtv_yuv_filter(itv, h_filter, -1, -1);
349 itv->yuv_info.h_filter = h_filter; 359 yi->h_filter = h_filter;
350 } 360 }
351} 361}
352 362
353static void ivtv_yuv_handle_vertical(struct ivtv *itv, struct yuv_frame_info *window) 363static void ivtv_yuv_handle_vertical(struct ivtv *itv, struct yuv_frame_info *f)
354{ 364{
365 struct yuv_playback_info *yi = &itv->yuv_info;
355 u32 master_height; 366 u32 master_height;
356 u32 reg_2918, reg_291c, reg_2920, reg_2928; 367 u32 reg_2918, reg_291c, reg_2920, reg_2928;
357 u32 reg_2930, reg_2934, reg_293c; 368 u32 reg_2930, reg_2934, reg_293c;
@@ -359,69 +370,59 @@ static void ivtv_yuv_handle_vertical(struct ivtv *itv, struct yuv_frame_info *wi
359 u32 reg_2950, reg_2954, reg_2958, reg_295c; 370 u32 reg_2950, reg_2954, reg_2958, reg_295c;
360 u32 reg_2960, reg_2964, reg_2968, reg_296c; 371 u32 reg_2960, reg_2964, reg_2968, reg_296c;
361 u32 reg_289c; 372 u32 reg_289c;
362 u32 src_y_major_y, src_y_minor_y; 373 u32 src_major_y, src_minor_y;
363 u32 src_y_major_uv, src_y_minor_uv; 374 u32 src_major_uv, src_minor_uv;
364 u32 reg_2964_base, reg_2968_base; 375 u32 reg_2964_base, reg_2968_base;
365 int v_filter_1, v_filter_2; 376 int v_filter_1, v_filter_2;
366 377
367 IVTV_DEBUG_WARN("Need to adjust to height %d src_h %d dst_h %d src_y %d dst_y %d\n", 378 IVTV_DEBUG_WARN
368 window->tru_h, window->src_h, window->dst_h,window->src_y, window->dst_y); 379 ("Adjust to height %d src_h %d dst_h %d src_y %d dst_y %d\n",
380 f->tru_h, f->src_h, f->dst_h, f->src_y, f->dst_y);
369 381
370 /* What scaling mode is being used... */ 382 /* What scaling mode is being used... */
371 if (window->interlaced_y) { 383 IVTV_DEBUG_YUV("Scaling mode Y: %s\n",
372 IVTV_DEBUG_YUV("Scaling mode Y: Interlaced\n"); 384 f->interlaced_y ? "Interlaced" : "Progressive");
373 }
374 else {
375 IVTV_DEBUG_YUV("Scaling mode Y: Progressive\n");
376 }
377 385
378 if (window->interlaced_uv) { 386 IVTV_DEBUG_YUV("Scaling mode UV: %s\n",
379 IVTV_DEBUG_YUV("Scaling mode UV: Interlaced\n"); 387 f->interlaced_uv ? "Interlaced" : "Progressive");
380 }
381 else {
382 IVTV_DEBUG_YUV("Scaling mode UV: Progressive\n");
383 }
384 388
385 /* What is the source video being treated as... */ 389 /* What is the source video being treated as... */
386 if (itv->yuv_info.frame_interlaced) { 390 IVTV_DEBUG_WARN("Source video: %s\n",
387 IVTV_DEBUG_WARN("Source video: Interlaced\n"); 391 f->interlaced ? "Interlaced" : "Progressive");
388 }
389 else {
390 IVTV_DEBUG_WARN("Source video: Non-interlaced\n");
391 }
392 392
393 /* We offset into the image using two different index methods, so split 393 /* We offset into the image using two different index methods, so split
394 the y source coord into two parts. */ 394 the y source coord into two parts. */
395 if (window->src_y < 8) { 395 if (f->src_y < 8) {
396 src_y_minor_uv = window->src_y; 396 src_minor_uv = f->src_y;
397 src_y_major_uv = 0; 397 src_major_uv = 0;
398 } 398 } else {
399 else { 399 src_minor_uv = 8;
400 src_y_minor_uv = 8; 400 src_major_uv = f->src_y - 8;
401 src_y_major_uv = window->src_y - 8;
402 } 401 }
403 402
404 src_y_minor_y = src_y_minor_uv; 403 src_minor_y = src_minor_uv;
405 src_y_major_y = src_y_major_uv; 404 src_major_y = src_major_uv;
406 405
407 if (window->offset_y) src_y_minor_y += 16; 406 if (f->offset_y)
407 src_minor_y += 16;
408 408
409 if (window->interlaced_y) 409 if (f->interlaced_y)
410 reg_2918 = (window->dst_h << 16) | (window->src_h + src_y_minor_y); 410 reg_2918 = (f->dst_h << 16) | (f->src_h + src_minor_y);
411 else 411 else
412 reg_2918 = (window->dst_h << 16) | ((window->src_h + src_y_minor_y) << 1); 412 reg_2918 = (f->dst_h << 16) | ((f->src_h + src_minor_y) << 1);
413 413
414 if (window->interlaced_uv) 414 if (f->interlaced_uv)
415 reg_291c = (window->dst_h << 16) | ((window->src_h + src_y_minor_uv) >> 1); 415 reg_291c = (f->dst_h << 16) | ((f->src_h + src_minor_uv) >> 1);
416 else 416 else
417 reg_291c = (window->dst_h << 16) | (window->src_h + src_y_minor_uv); 417 reg_291c = (f->dst_h << 16) | (f->src_h + src_minor_uv);
418 418
419 reg_2964_base = (src_y_minor_y * ((window->dst_h << 16)/window->src_h)) >> 14; 419 reg_2964_base = (src_minor_y * ((f->dst_h << 16) / f->src_h)) >> 14;
420 reg_2968_base = (src_y_minor_uv * ((window->dst_h << 16)/window->src_h)) >> 14; 420 reg_2968_base = (src_minor_uv * ((f->dst_h << 16) / f->src_h)) >> 14;
421 421
422 if (window->dst_h / 2 >= window->src_h && !window->interlaced_y) { 422 if (f->dst_h / 2 >= f->src_h && !f->interlaced_y) {
423 master_height = (window->src_h * 0x00400000) / window->dst_h; 423 master_height = (f->src_h * 0x00400000) / f->dst_h;
424 if ((window->src_h * 0x00400000) - (master_height * window->dst_h) >= window->dst_h / 2) master_height ++; 424 if ((f->src_h * 0x00400000) - (master_height * f->dst_h) >= f->dst_h / 2)
425 master_height++;
425 reg_2920 = master_height >> 2; 426 reg_2920 = master_height >> 2;
426 reg_2928 = master_height >> 3; 427 reg_2928 = master_height >> 3;
427 reg_2930 = master_height; 428 reg_2930 = master_height;
@@ -429,45 +430,42 @@ static void ivtv_yuv_handle_vertical(struct ivtv *itv, struct yuv_frame_info *wi
429 reg_2964_base >>= 3; 430 reg_2964_base >>= 3;
430 reg_2968_base >>= 3; 431 reg_2968_base >>= 3;
431 reg_296c = 0x00000000; 432 reg_296c = 0x00000000;
432 } 433 } else if (f->dst_h >= f->src_h) {
433 else if (window->dst_h >= window->src_h) { 434 master_height = (f->src_h * 0x00400000) / f->dst_h;
434 master_height = (window->src_h * 0x00400000) / window->dst_h;
435 master_height = (master_height >> 1) + (master_height & 1); 435 master_height = (master_height >> 1) + (master_height & 1);
436 reg_2920 = master_height >> 2; 436 reg_2920 = master_height >> 2;
437 reg_2928 = master_height >> 2; 437 reg_2928 = master_height >> 2;
438 reg_2930 = master_height; 438 reg_2930 = master_height;
439 reg_2940 = master_height >> 1; 439 reg_2940 = master_height >> 1;
440 reg_296c = 0x00000000; 440 reg_296c = 0x00000000;
441 if (window->interlaced_y) { 441 if (f->interlaced_y) {
442 reg_2964_base >>= 3; 442 reg_2964_base >>= 3;
443 } 443 } else {
444 else { 444 reg_296c++;
445 reg_296c ++;
446 reg_2964_base >>= 2; 445 reg_2964_base >>= 2;
447 } 446 }
448 if (window->interlaced_uv) reg_2928 >>= 1; 447 if (f->interlaced_uv)
448 reg_2928 >>= 1;
449 reg_2968_base >>= 3; 449 reg_2968_base >>= 3;
450 } 450 } else if (f->dst_h >= f->src_h / 2) {
451 else if (window->dst_h >= window->src_h / 2) { 451 master_height = (f->src_h * 0x00200000) / f->dst_h;
452 master_height = (window->src_h * 0x00200000) / window->dst_h;
453 master_height = (master_height >> 1) + (master_height & 1); 452 master_height = (master_height >> 1) + (master_height & 1);
454 reg_2920 = master_height >> 2; 453 reg_2920 = master_height >> 2;
455 reg_2928 = master_height >> 2; 454 reg_2928 = master_height >> 2;
456 reg_2930 = master_height; 455 reg_2930 = master_height;
457 reg_2940 = master_height; 456 reg_2940 = master_height;
458 reg_296c = 0x00000101; 457 reg_296c = 0x00000101;
459 if (window->interlaced_y) { 458 if (f->interlaced_y) {
460 reg_2964_base >>= 2; 459 reg_2964_base >>= 2;
461 } 460 } else {
462 else { 461 reg_296c++;
463 reg_296c ++;
464 reg_2964_base >>= 1; 462 reg_2964_base >>= 1;
465 } 463 }
466 if (window->interlaced_uv) reg_2928 >>= 1; 464 if (f->interlaced_uv)
465 reg_2928 >>= 1;
467 reg_2968_base >>= 2; 466 reg_2968_base >>= 2;
468 } 467 } else {
469 else { 468 master_height = (f->src_h * 0x00100000) / f->dst_h;
470 master_height = (window->src_h * 0x00100000) / window->dst_h;
471 master_height = (master_height >> 1) + (master_height & 1); 469 master_height = (master_height >> 1) + (master_height & 1);
472 reg_2920 = master_height >> 2; 470 reg_2920 = master_height >> 2;
473 reg_2928 = master_height >> 2; 471 reg_2928 = master_height >> 2;
@@ -480,13 +478,12 @@ static void ivtv_yuv_handle_vertical(struct ivtv *itv, struct yuv_frame_info *wi
480 478
481 /* FIXME These registers change depending on scaled / unscaled output 479 /* FIXME These registers change depending on scaled / unscaled output
482 We really need to work out what they should be */ 480 We really need to work out what they should be */
483 if (window->src_h == window->dst_h){ 481 if (f->src_h == f->dst_h) {
484 reg_2934 = 0x00020000; 482 reg_2934 = 0x00020000;
485 reg_293c = 0x00100000; 483 reg_293c = 0x00100000;
486 reg_2944 = 0x00040000; 484 reg_2944 = 0x00040000;
487 reg_294c = 0x000b0000; 485 reg_294c = 0x000b0000;
488 } 486 } else {
489 else {
490 reg_2934 = 0x00000FF0; 487 reg_2934 = 0x00000FF0;
491 reg_293c = 0x00000FF0; 488 reg_293c = 0x00000FF0;
492 reg_2944 = 0x00000FF0; 489 reg_2944 = 0x00000FF0;
@@ -494,34 +491,36 @@ static void ivtv_yuv_handle_vertical(struct ivtv *itv, struct yuv_frame_info *wi
494 } 491 }
495 492
496 /* The first line to be displayed */ 493 /* The first line to be displayed */
497 reg_2950 = 0x00010000 + src_y_major_y; 494 reg_2950 = 0x00010000 + src_major_y;
498 if (window->interlaced_y) reg_2950 += 0x00010000; 495 if (f->interlaced_y)
496 reg_2950 += 0x00010000;
499 reg_2954 = reg_2950 + 1; 497 reg_2954 = reg_2950 + 1;
500 498
501 reg_2958 = 0x00010000 + (src_y_major_y >> 1); 499 reg_2958 = 0x00010000 + (src_major_y >> 1);
502 if (window->interlaced_uv) reg_2958 += 0x00010000; 500 if (f->interlaced_uv)
501 reg_2958 += 0x00010000;
503 reg_295c = reg_2958 + 1; 502 reg_295c = reg_2958 + 1;
504 503
505 if (itv->yuv_info.decode_height == 480) 504 if (yi->decode_height == 480)
506 reg_289c = 0x011e0017; 505 reg_289c = 0x011e0017;
507 else 506 else
508 reg_289c = 0x01500017; 507 reg_289c = 0x01500017;
509 508
510 if (window->dst_y < 0) 509 if (f->dst_y < 0)
511 reg_289c = (reg_289c - ((window->dst_y & ~1)<<15))-(window->dst_y >>1); 510 reg_289c = (reg_289c - ((f->dst_y & ~1)<<15))-(f->dst_y >>1);
512 else 511 else
513 reg_289c = (reg_289c + ((window->dst_y & ~1)<<15))+(window->dst_y >>1); 512 reg_289c = (reg_289c + ((f->dst_y & ~1)<<15))+(f->dst_y >>1);
514 513
515 /* How much of the source to decode. 514 /* How much of the source to decode.
516 Take into account the source offset */ 515 Take into account the source offset */
517 reg_2960 = ((src_y_minor_y + window->src_h + src_y_major_y) - 1 ) | 516 reg_2960 = ((src_minor_y + f->src_h + src_major_y) - 1) |
518 ((((src_y_minor_uv + window->src_h + src_y_major_uv) - 1) & ~1) << 15); 517 (((src_minor_uv + f->src_h + src_major_uv - 1) & ~1) << 15);
519 518
520 /* Calculate correct value for register 2964 */ 519 /* Calculate correct value for register 2964 */
521 if (window->src_h == window->dst_h) 520 if (f->src_h == f->dst_h) {
522 reg_2964 = 1; 521 reg_2964 = 1;
523 else { 522 } else {
524 reg_2964 = 2 + ((window->dst_h << 1) / window->src_h); 523 reg_2964 = 2 + ((f->dst_h << 1) / f->src_h);
525 reg_2964 = (reg_2964 >> 1) + (reg_2964 & 1); 524 reg_2964 = (reg_2964 >> 1) + (reg_2964 & 1);
526 } 525 }
527 reg_2968 = (reg_2964 << 16) + reg_2964 + (reg_2964 >> 1); 526 reg_2968 = (reg_2964 << 16) + reg_2964 + (reg_2964 >> 1);
@@ -536,283 +535,246 @@ static void ivtv_yuv_handle_vertical(struct ivtv *itv, struct yuv_frame_info *wi
536 /* Deviate further from what it should be. I find the flicker headache 535 /* Deviate further from what it should be. I find the flicker headache
537 inducing so try to reduce it slightly. Leave 2968 as-is otherwise 536 inducing so try to reduce it slightly. Leave 2968 as-is otherwise
538 colours foul. */ 537 colours foul. */
539 if ((reg_2964 != 0x00010001) && (window->dst_h / 2 <= window->src_h)) 538 if ((reg_2964 != 0x00010001) && (f->dst_h / 2 <= f->src_h))
540 reg_2964 = (reg_2964 & 0xFFFF0000) + ((reg_2964 & 0x0000FFFF)/2); 539 reg_2964 = (reg_2964 & 0xFFFF0000) + ((reg_2964 & 0x0000FFFF) / 2);
541 540
542 if (!window->interlaced_y) reg_2964 -= 0x00010001; 541 if (!f->interlaced_y)
543 if (!window->interlaced_uv) reg_2968 -= 0x00010001; 542 reg_2964 -= 0x00010001;
543 if (!f->interlaced_uv)
544 reg_2968 -= 0x00010001;
544 545
545 reg_2964 += ((reg_2964_base << 16) | reg_2964_base); 546 reg_2964 += ((reg_2964_base << 16) | reg_2964_base);
546 reg_2968 += ((reg_2968_base << 16) | reg_2968_base); 547 reg_2968 += ((reg_2968_base << 16) | reg_2968_base);
547 548
548 /* Select the vertical filter */ 549 /* Select the vertical filter */
549 if (window->src_h == window->dst_h) { 550 if (f->src_h == f->dst_h) {
550 /* An exact size match uses filter 0/1 */ 551 /* An exact size match uses filter 0/1 */
551 v_filter_1 = 0; 552 v_filter_1 = 0;
552 v_filter_2 = 1; 553 v_filter_2 = 1;
553 } 554 } else {
554 else {
555 /* Figure out which filter to use */ 555 /* Figure out which filter to use */
556 v_filter_1 = ((window->src_h << 16) / window->dst_h) >> 15; 556 v_filter_1 = ((f->src_h << 16) / f->dst_h) >> 15;
557 v_filter_1 = (v_filter_1 >> 1) + (v_filter_1 & 1); 557 v_filter_1 = (v_filter_1 >> 1) + (v_filter_1 & 1);
558 /* Only an exact size match can use filter 0 */ 558 /* Only an exact size match can use filter 0 */
559 if (v_filter_1 == 0) v_filter_1 = 1; 559 v_filter_1 += !v_filter_1;
560 v_filter_2 = v_filter_1; 560 v_filter_2 = v_filter_1;
561 } 561 }
562 562
563 write_reg(reg_2934, 0x02934); 563 write_reg(reg_2934, 0x02934);
564 write_reg(reg_293c, 0x0293c); 564 write_reg(reg_293c, 0x0293c);
565 IVTV_DEBUG_YUV("Update reg 0x2934 %08x->%08x 0x293c %08x->%08x\n",itv->yuv_info.reg_2934, reg_2934, itv->yuv_info.reg_293c, reg_293c); 565 IVTV_DEBUG_YUV("Update reg 0x2934 %08x->%08x 0x293c %08x->%08x\n",
566 yi->reg_2934, reg_2934, yi->reg_293c, reg_293c);
566 write_reg(reg_2944, 0x02944); 567 write_reg(reg_2944, 0x02944);
567 write_reg(reg_294c, 0x0294c); 568 write_reg(reg_294c, 0x0294c);
568 IVTV_DEBUG_YUV("Update reg 0x2944 %08x->%08x 0x294c %08x->%08x\n",itv->yuv_info.reg_2944, reg_2944, itv->yuv_info.reg_294c, reg_294c); 569 IVTV_DEBUG_YUV("Update reg 0x2944 %08x->%08x 0x294c %08x->%08x\n",
570 yi->reg_2944, reg_2944, yi->reg_294c, reg_294c);
569 571
570 /* Ensure 2970 is 0 (does it ever change ?) */ 572 /* Ensure 2970 is 0 (does it ever change ?) */
571/* write_reg(0,0x02970); */ 573/* write_reg(0,0x02970); */
572/* IVTV_DEBUG_YUV("Update reg 0x2970 %08x->%08x\n",itv->yuv_info.reg_2970, 0); */ 574/* IVTV_DEBUG_YUV("Update reg 0x2970 %08x->%08x\n", yi->reg_2970, 0); */
573 575
574 write_reg(reg_2930, 0x02938); 576 write_reg(reg_2930, 0x02938);
575 write_reg(reg_2930, 0x02930); 577 write_reg(reg_2930, 0x02930);
576 IVTV_DEBUG_YUV("Update reg 0x2930 %08x->%08x 0x2938 %08x->%08x\n",itv->yuv_info.reg_2930, reg_2930, itv->yuv_info.reg_2938, reg_2930); 578 IVTV_DEBUG_YUV("Update reg 0x2930 %08x->%08x 0x2938 %08x->%08x\n",
579 yi->reg_2930, reg_2930, yi->reg_2938, reg_2930);
577 580
578 write_reg(reg_2928, 0x02928); 581 write_reg(reg_2928, 0x02928);
579 write_reg(reg_2928+0x514, 0x0292C); 582 write_reg(reg_2928 + 0x514, 0x0292C);
580 IVTV_DEBUG_YUV("Update reg 0x2928 %08x->%08x 0x292c %08x->%08x\n",itv->yuv_info.reg_2928, reg_2928, itv->yuv_info.reg_292c, reg_2928+0x514); 583 IVTV_DEBUG_YUV("Update reg 0x2928 %08x->%08x 0x292c %08x->%08x\n",
584 yi->reg_2928, reg_2928, yi->reg_292c, reg_2928 + 0x514);
581 585
582 write_reg(reg_2920, 0x02920); 586 write_reg(reg_2920, 0x02920);
583 write_reg(reg_2920+0x514, 0x02924); 587 write_reg(reg_2920 + 0x514, 0x02924);
584 IVTV_DEBUG_YUV("Update reg 0x2920 %08x->%08x 0x2924 %08x->%08x\n",itv->yuv_info.reg_2920, reg_2920, itv->yuv_info.reg_2924, 0x514+reg_2920); 588 IVTV_DEBUG_YUV("Update reg 0x2920 %08x->%08x 0x2924 %08x->%08x\n",
589 yi->reg_2920, reg_2920, yi->reg_2924, reg_2920 + 0x514);
585 590
586 write_reg (reg_2918,0x02918); 591 write_reg(reg_2918, 0x02918);
587 write_reg (reg_291c,0x0291C); 592 write_reg(reg_291c, 0x0291C);
588 IVTV_DEBUG_YUV("Update reg 0x2918 %08x->%08x 0x291C %08x->%08x\n",itv->yuv_info.reg_2918,reg_2918,itv->yuv_info.reg_291c,reg_291c); 593 IVTV_DEBUG_YUV("Update reg 0x2918 %08x->%08x 0x291C %08x->%08x\n",
594 yi->reg_2918, reg_2918, yi->reg_291c, reg_291c);
589 595
590 write_reg(reg_296c, 0x0296c); 596 write_reg(reg_296c, 0x0296c);
591 IVTV_DEBUG_YUV("Update reg 0x296c %08x->%08x\n",itv->yuv_info.reg_296c, reg_296c); 597 IVTV_DEBUG_YUV("Update reg 0x296c %08x->%08x\n",
598 yi->reg_296c, reg_296c);
592 599
593 write_reg(reg_2940, 0x02948); 600 write_reg(reg_2940, 0x02948);
594 write_reg(reg_2940, 0x02940); 601 write_reg(reg_2940, 0x02940);
595 IVTV_DEBUG_YUV("Update reg 0x2940 %08x->%08x 0x2948 %08x->%08x\n",itv->yuv_info.reg_2940, reg_2940, itv->yuv_info.reg_2948, reg_2940); 602 IVTV_DEBUG_YUV("Update reg 0x2940 %08x->%08x 0x2948 %08x->%08x\n",
603 yi->reg_2940, reg_2940, yi->reg_2948, reg_2940);
596 604
597 write_reg(reg_2950, 0x02950); 605 write_reg(reg_2950, 0x02950);
598 write_reg(reg_2954, 0x02954); 606 write_reg(reg_2954, 0x02954);
599 IVTV_DEBUG_YUV("Update reg 0x2950 %08x->%08x 0x2954 %08x->%08x\n",itv->yuv_info.reg_2950, reg_2950, itv->yuv_info.reg_2954, reg_2954); 607 IVTV_DEBUG_YUV("Update reg 0x2950 %08x->%08x 0x2954 %08x->%08x\n",
608 yi->reg_2950, reg_2950, yi->reg_2954, reg_2954);
600 609
601 write_reg(reg_2958, 0x02958); 610 write_reg(reg_2958, 0x02958);
602 write_reg(reg_295c, 0x0295C); 611 write_reg(reg_295c, 0x0295C);
603 IVTV_DEBUG_YUV("Update reg 0x2958 %08x->%08x 0x295C %08x->%08x\n",itv->yuv_info.reg_2958, reg_2958, itv->yuv_info.reg_295c, reg_295c); 612 IVTV_DEBUG_YUV("Update reg 0x2958 %08x->%08x 0x295C %08x->%08x\n",
613 yi->reg_2958, reg_2958, yi->reg_295c, reg_295c);
604 614
605 write_reg(reg_2960, 0x02960); 615 write_reg(reg_2960, 0x02960);
606 IVTV_DEBUG_YUV("Update reg 0x2960 %08x->%08x \n",itv->yuv_info.reg_2960, reg_2960); 616 IVTV_DEBUG_YUV("Update reg 0x2960 %08x->%08x \n",
617 yi->reg_2960, reg_2960);
607 618
608 write_reg(reg_2964, 0x02964); 619 write_reg(reg_2964, 0x02964);
609 write_reg(reg_2968, 0x02968); 620 write_reg(reg_2968, 0x02968);
610 IVTV_DEBUG_YUV("Update reg 0x2964 %08x->%08x 0x2968 %08x->%08x\n",itv->yuv_info.reg_2964, reg_2964, itv->yuv_info.reg_2968, reg_2968); 621 IVTV_DEBUG_YUV("Update reg 0x2964 %08x->%08x 0x2968 %08x->%08x\n",
622 yi->reg_2964, reg_2964, yi->reg_2968, reg_2968);
611 623
612 write_reg( reg_289c,0x0289c); 624 write_reg(reg_289c, 0x0289c);
613 IVTV_DEBUG_YUV("Update reg 0x289c %08x->%08x\n",itv->yuv_info.reg_289c, reg_289c); 625 IVTV_DEBUG_YUV("Update reg 0x289c %08x->%08x\n",
626 yi->reg_289c, reg_289c);
614 627
615 /* Only update filter 1 if we really need to */ 628 /* Only update filter 1 if we really need to */
616 if (v_filter_1 != itv->yuv_info.v_filter_1) { 629 if (v_filter_1 != yi->v_filter_1) {
617 ivtv_yuv_filter (itv,-1,v_filter_1,-1); 630 ivtv_yuv_filter(itv, -1, v_filter_1, -1);
618 itv->yuv_info.v_filter_1 = v_filter_1; 631 yi->v_filter_1 = v_filter_1;
619 } 632 }
620 633
621 /* Only update filter 2 if we really need to */ 634 /* Only update filter 2 if we really need to */
622 if (v_filter_2 != itv->yuv_info.v_filter_2) { 635 if (v_filter_2 != yi->v_filter_2) {
623 ivtv_yuv_filter (itv,-1,-1,v_filter_2); 636 ivtv_yuv_filter(itv, -1, -1, v_filter_2);
624 itv->yuv_info.v_filter_2 = v_filter_2; 637 yi->v_filter_2 = v_filter_2;
625 } 638 }
626
627} 639}
628 640
629/* Modify the supplied coordinate information to fit the visible osd area */ 641/* Modify the supplied coordinate information to fit the visible osd area */
630static u32 ivtv_yuv_window_setup (struct ivtv *itv, struct yuv_frame_info *window) 642static u32 ivtv_yuv_window_setup(struct ivtv *itv, struct yuv_frame_info *f)
631{ 643{
632 int osd_crop, lace_threshold; 644 struct yuv_frame_info *of = &itv->yuv_info.old_frame_info;
645 int osd_crop;
633 u32 osd_scale; 646 u32 osd_scale;
634 u32 yuv_update = 0; 647 u32 yuv_update = 0;
635 648
636 lace_threshold = itv->yuv_info.lace_threshold;
637 if (lace_threshold < 0)
638 lace_threshold = itv->yuv_info.decode_height - 1;
639
640 /* Work out the lace settings */
641 switch (itv->yuv_info.lace_mode) {
642 case IVTV_YUV_MODE_PROGRESSIVE: /* Progressive mode */
643 itv->yuv_info.frame_interlaced = 0;
644 if (window->tru_h < 512 || (window->tru_h > 576 && window->tru_h < 1021))
645 window->interlaced_y = 0;
646 else
647 window->interlaced_y = 1;
648
649 if (window->tru_h < 1021 && (window->dst_h >= window->src_h /2))
650 window->interlaced_uv = 0;
651 else
652 window->interlaced_uv = 1;
653 break;
654
655 case IVTV_YUV_MODE_AUTO:
656 if (window->tru_h <= lace_threshold || window->tru_h > 576 || window->tru_w > 720){
657 itv->yuv_info.frame_interlaced = 0;
658 if ((window->tru_h < 512) ||
659 (window->tru_h > 576 && window->tru_h < 1021) ||
660 (window->tru_w > 720 && window->tru_h < 1021))
661 window->interlaced_y = 0;
662 else
663 window->interlaced_y = 1;
664
665 if (window->tru_h < 1021 && (window->dst_h >= window->src_h /2))
666 window->interlaced_uv = 0;
667 else
668 window->interlaced_uv = 1;
669 }
670 else {
671 itv->yuv_info.frame_interlaced = 1;
672 window->interlaced_y = 1;
673 window->interlaced_uv = 1;
674 }
675 break;
676
677 case IVTV_YUV_MODE_INTERLACED: /* Interlace mode */
678 default:
679 itv->yuv_info.frame_interlaced = 1;
680 window->interlaced_y = 1;
681 window->interlaced_uv = 1;
682 break;
683 }
684
685 /* Sorry, but no negative coords for src */ 649 /* Sorry, but no negative coords for src */
686 if (window->src_x < 0) window->src_x = 0; 650 if (f->src_x < 0)
687 if (window->src_y < 0) window->src_y = 0; 651 f->src_x = 0;
652 if (f->src_y < 0)
653 f->src_y = 0;
688 654
689 /* Can only reduce width down to 1/4 original size */ 655 /* Can only reduce width down to 1/4 original size */
690 if ((osd_crop = window->src_w - ( 4 * window->dst_w )) > 0) { 656 if ((osd_crop = f->src_w - 4 * f->dst_w) > 0) {
691 window->src_x += osd_crop / 2; 657 f->src_x += osd_crop / 2;
692 window->src_w = (window->src_w - osd_crop) & ~3; 658 f->src_w = (f->src_w - osd_crop) & ~3;
693 window->dst_w = window->src_w / 4; 659 f->dst_w = f->src_w / 4;
694 window->dst_w += window->dst_w & 1; 660 f->dst_w += f->dst_w & 1;
695 } 661 }
696 662
697 /* Can only reduce height down to 1/4 original size */ 663 /* Can only reduce height down to 1/4 original size */
698 if (window->src_h / window->dst_h >= 2) { 664 if (f->src_h / f->dst_h >= 2) {
699 /* Overflow may be because we're running progressive, so force mode switch */ 665 /* Overflow may be because we're running progressive,
700 window->interlaced_y = 1; 666 so force mode switch */
667 f->interlaced_y = 1;
701 /* Make sure we're still within limits for interlace */ 668 /* Make sure we're still within limits for interlace */
702 if ((osd_crop = window->src_h - ( 4 * window->dst_h )) > 0) { 669 if ((osd_crop = f->src_h - 4 * f->dst_h) > 0) {
703 /* If we reach here we'll have to force the height. */ 670 /* If we reach here we'll have to force the height. */
704 window->src_y += osd_crop / 2; 671 f->src_y += osd_crop / 2;
705 window->src_h = (window->src_h - osd_crop) & ~3; 672 f->src_h = (f->src_h - osd_crop) & ~3;
706 window->dst_h = window->src_h / 4; 673 f->dst_h = f->src_h / 4;
707 window->dst_h += window->dst_h & 1; 674 f->dst_h += f->dst_h & 1;
708 } 675 }
709 } 676 }
710 677
711 /* If there's nothing to safe to display, we may as well stop now */ 678 /* If there's nothing to safe to display, we may as well stop now */
712 if ((int)window->dst_w <= 2 || (int)window->dst_h <= 2 || (int)window->src_w <= 2 || (int)window->src_h <= 2) { 679 if ((int)f->dst_w <= 2 || (int)f->dst_h <= 2 ||
680 (int)f->src_w <= 2 || (int)f->src_h <= 2) {
713 return IVTV_YUV_UPDATE_INVALID; 681 return IVTV_YUV_UPDATE_INVALID;
714 } 682 }
715 683
716 /* Ensure video remains inside OSD area */ 684 /* Ensure video remains inside OSD area */
717 osd_scale = (window->src_h << 16) / window->dst_h; 685 osd_scale = (f->src_h << 16) / f->dst_h;
718 686
719 if ((osd_crop = window->pan_y - window->dst_y) > 0) { 687 if ((osd_crop = f->pan_y - f->dst_y) > 0) {
720 /* Falls off the upper edge - crop */ 688 /* Falls off the upper edge - crop */
721 window->src_y += (osd_scale * osd_crop) >> 16; 689 f->src_y += (osd_scale * osd_crop) >> 16;
722 window->src_h -= (osd_scale * osd_crop) >> 16; 690 f->src_h -= (osd_scale * osd_crop) >> 16;
723 window->dst_h -= osd_crop; 691 f->dst_h -= osd_crop;
724 window->dst_y = 0; 692 f->dst_y = 0;
725 } 693 } else {
726 else { 694 f->dst_y -= f->pan_y;
727 window->dst_y -= window->pan_y;
728 } 695 }
729 696
730 if ((osd_crop = window->dst_h + window->dst_y - window->vis_h) > 0) { 697 if ((osd_crop = f->dst_h + f->dst_y - f->vis_h) > 0) {
731 /* Falls off the lower edge - crop */ 698 /* Falls off the lower edge - crop */
732 window->dst_h -= osd_crop; 699 f->dst_h -= osd_crop;
733 window->src_h -= (osd_scale * osd_crop) >> 16; 700 f->src_h -= (osd_scale * osd_crop) >> 16;
734 } 701 }
735 702
736 osd_scale = (window->src_w << 16) / window->dst_w; 703 osd_scale = (f->src_w << 16) / f->dst_w;
737 704
738 if ((osd_crop = window->pan_x - window->dst_x) > 0) { 705 if ((osd_crop = f->pan_x - f->dst_x) > 0) {
739 /* Fall off the left edge - crop */ 706 /* Fall off the left edge - crop */
740 window->src_x += (osd_scale * osd_crop) >> 16; 707 f->src_x += (osd_scale * osd_crop) >> 16;
741 window->src_w -= (osd_scale * osd_crop) >> 16; 708 f->src_w -= (osd_scale * osd_crop) >> 16;
742 window->dst_w -= osd_crop; 709 f->dst_w -= osd_crop;
743 window->dst_x = 0; 710 f->dst_x = 0;
744 } 711 } else {
745 else { 712 f->dst_x -= f->pan_x;
746 window->dst_x -= window->pan_x;
747 } 713 }
748 714
749 if ((osd_crop = window->dst_w + window->dst_x - window->vis_w) > 0) { 715 if ((osd_crop = f->dst_w + f->dst_x - f->vis_w) > 0) {
750 /* Falls off the right edge - crop */ 716 /* Falls off the right edge - crop */
751 window->dst_w -= osd_crop; 717 f->dst_w -= osd_crop;
752 window->src_w -= (osd_scale * osd_crop) >> 16; 718 f->src_w -= (osd_scale * osd_crop) >> 16;
753 } 719 }
754 720
755 /* The OSD can be moved. Track to it */ 721 /* The OSD can be moved. Track to it */
756 window->dst_x += itv->yuv_info.osd_x_offset; 722 f->dst_x += itv->yuv_info.osd_x_offset;
757 window->dst_y += itv->yuv_info.osd_y_offset; 723 f->dst_y += itv->yuv_info.osd_y_offset;
758 724
759 /* Width & height for both src & dst must be even. 725 /* Width & height for both src & dst must be even.
760 Same for coordinates. */ 726 Same for coordinates. */
761 window->dst_w &= ~1; 727 f->dst_w &= ~1;
762 window->dst_x &= ~1; 728 f->dst_x &= ~1;
763 729
764 window->src_w += window->src_x & 1; 730 f->src_w += f->src_x & 1;
765 window->src_x &= ~1; 731 f->src_x &= ~1;
766 732
767 window->src_w &= ~1; 733 f->src_w &= ~1;
768 window->dst_w &= ~1; 734 f->dst_w &= ~1;
769 735
770 window->dst_h &= ~1; 736 f->dst_h &= ~1;
771 window->dst_y &= ~1; 737 f->dst_y &= ~1;
772 738
773 window->src_h += window->src_y & 1; 739 f->src_h += f->src_y & 1;
774 window->src_y &= ~1; 740 f->src_y &= ~1;
775 741
776 window->src_h &= ~1; 742 f->src_h &= ~1;
777 window->dst_h &= ~1; 743 f->dst_h &= ~1;
778 744
779 /* Due to rounding, we may have reduced the output size to <1/4 of the source 745 /* Due to rounding, we may have reduced the output size to <1/4 of
780 Check again, but this time just resize. Don't change source coordinates */ 746 the source. Check again, but this time just resize. Don't change
781 if (window->dst_w < window->src_w / 4) { 747 source coordinates */
782 window->src_w &= ~3; 748 if (f->dst_w < f->src_w / 4) {
783 window->dst_w = window->src_w / 4; 749 f->src_w &= ~3;
784 window->dst_w += window->dst_w & 1; 750 f->dst_w = f->src_w / 4;
751 f->dst_w += f->dst_w & 1;
785 } 752 }
786 if (window->dst_h < window->src_h / 4) { 753 if (f->dst_h < f->src_h / 4) {
787 window->src_h &= ~3; 754 f->src_h &= ~3;
788 window->dst_h = window->src_h / 4; 755 f->dst_h = f->src_h / 4;
789 window->dst_h += window->dst_h & 1; 756 f->dst_h += f->dst_h & 1;
790 } 757 }
791 758
792 /* Check again. If there's nothing to safe to display, stop now */ 759 /* Check again. If there's nothing to safe to display, stop now */
793 if ((int)window->dst_w <= 2 || (int)window->dst_h <= 2 || (int)window->src_w <= 2 || (int)window->src_h <= 2) { 760 if ((int)f->dst_w <= 2 || (int)f->dst_h <= 2 ||
761 (int)f->src_w <= 2 || (int)f->src_h <= 2) {
794 return IVTV_YUV_UPDATE_INVALID; 762 return IVTV_YUV_UPDATE_INVALID;
795 } 763 }
796 764
797 /* Both x offset & width are linked, so they have to be done together */ 765 /* Both x offset & width are linked, so they have to be done together */
798 if ((itv->yuv_info.old_frame_info.dst_w != window->dst_w) || 766 if ((of->dst_w != f->dst_w) || (of->src_w != f->src_w) ||
799 (itv->yuv_info.old_frame_info.src_w != window->src_w) || 767 (of->dst_x != f->dst_x) || (of->src_x != f->src_x) ||
800 (itv->yuv_info.old_frame_info.dst_x != window->dst_x) || 768 (of->pan_x != f->pan_x) || (of->vis_w != f->vis_w)) {
801 (itv->yuv_info.old_frame_info.src_x != window->src_x) ||
802 (itv->yuv_info.old_frame_info.pan_x != window->pan_x) ||
803 (itv->yuv_info.old_frame_info.vis_w != window->vis_w)) {
804 yuv_update |= IVTV_YUV_UPDATE_HORIZONTAL; 769 yuv_update |= IVTV_YUV_UPDATE_HORIZONTAL;
805 } 770 }
806 771
807 if ((itv->yuv_info.old_frame_info.src_h != window->src_h) || 772 if ((of->src_h != f->src_h) || (of->dst_h != f->dst_h) ||
808 (itv->yuv_info.old_frame_info.dst_h != window->dst_h) || 773 (of->dst_y != f->dst_y) || (of->src_y != f->src_y) ||
809 (itv->yuv_info.old_frame_info.dst_y != window->dst_y) || 774 (of->pan_y != f->pan_y) || (of->vis_h != f->vis_h) ||
810 (itv->yuv_info.old_frame_info.src_y != window->src_y) || 775 (of->lace_mode != f->lace_mode) ||
811 (itv->yuv_info.old_frame_info.pan_y != window->pan_y) || 776 (of->interlaced_y != f->interlaced_y) ||
812 (itv->yuv_info.old_frame_info.vis_h != window->vis_h) || 777 (of->interlaced_uv != f->interlaced_uv)) {
813 (itv->yuv_info.old_frame_info.lace_mode != window->lace_mode) ||
814 (itv->yuv_info.old_frame_info.interlaced_y != window->interlaced_y) ||
815 (itv->yuv_info.old_frame_info.interlaced_uv != window->interlaced_uv)) {
816 yuv_update |= IVTV_YUV_UPDATE_VERTICAL; 778 yuv_update |= IVTV_YUV_UPDATE_VERTICAL;
817 } 779 }
818 780
@@ -820,24 +782,24 @@ static u32 ivtv_yuv_window_setup (struct ivtv *itv, struct yuv_frame_info *windo
820} 782}
821 783
822/* Update the scaling register to the requested value */ 784/* Update the scaling register to the requested value */
823void ivtv_yuv_work_handler (struct ivtv *itv) 785void ivtv_yuv_work_handler(struct ivtv *itv)
824{ 786{
825 struct yuv_frame_info window; 787 struct yuv_playback_info *yi = &itv->yuv_info;
788 struct yuv_frame_info f;
789 int frame = yi->update_frame;
826 u32 yuv_update; 790 u32 yuv_update;
827 791
828 int frame = itv->yuv_info.update_frame; 792 IVTV_DEBUG_YUV("Update yuv registers for frame %d\n", frame);
829 793 f = yi->new_frame_info[frame];
830/* IVTV_DEBUG_YUV("Update yuv registers for frame %d\n",frame); */
831 memcpy(&window, &itv->yuv_info.new_frame_info[frame], sizeof (window));
832 794
833 /* Update the osd pan info */ 795 /* Update the osd pan info */
834 window.pan_x = itv->yuv_info.osd_x_pan; 796 f.pan_x = yi->osd_x_pan;
835 window.pan_y = itv->yuv_info.osd_y_pan; 797 f.pan_y = yi->osd_y_pan;
836 window.vis_w = itv->yuv_info.osd_vis_w; 798 f.vis_w = yi->osd_vis_w;
837 window.vis_h = itv->yuv_info.osd_vis_h; 799 f.vis_h = yi->osd_vis_h;
838 800
839 /* Calculate the display window coordinates. Exit if nothing left */ 801 /* Calculate the display window coordinates. Exit if nothing left */
840 if (!(yuv_update = ivtv_yuv_window_setup (itv, &window))) 802 if (!(yuv_update = ivtv_yuv_window_setup(itv, &f)))
841 return; 803 return;
842 804
843 if (yuv_update & IVTV_YUV_UPDATE_INVALID) { 805 if (yuv_update & IVTV_YUV_UPDATE_INVALID) {
@@ -846,16 +808,15 @@ void ivtv_yuv_work_handler (struct ivtv *itv)
846 write_reg(0x00108080, 0x2898); 808 write_reg(0x00108080, 0x2898);
847 809
848 if (yuv_update & IVTV_YUV_UPDATE_HORIZONTAL) 810 if (yuv_update & IVTV_YUV_UPDATE_HORIZONTAL)
849 ivtv_yuv_handle_horizontal(itv, &window); 811 ivtv_yuv_handle_horizontal(itv, &f);
850 812
851 if (yuv_update & IVTV_YUV_UPDATE_VERTICAL) 813 if (yuv_update & IVTV_YUV_UPDATE_VERTICAL)
852 ivtv_yuv_handle_vertical(itv, &window); 814 ivtv_yuv_handle_vertical(itv, &f);
853 } 815 }
854 816 yi->old_frame_info = f;
855 memcpy(&itv->yuv_info.old_frame_info, &window, sizeof (itv->yuv_info.old_frame_info));
856} 817}
857 818
858static void ivtv_yuv_init (struct ivtv *itv) 819static void ivtv_yuv_init(struct ivtv *itv)
859{ 820{
860 struct yuv_playback_info *yi = &itv->yuv_info; 821 struct yuv_playback_info *yi = &itv->yuv_info;
861 822
@@ -924,25 +885,23 @@ static void ivtv_yuv_init (struct ivtv *itv)
924 if (!yi->osd_vis_w) 885 if (!yi->osd_vis_w)
925 yi->osd_vis_w = 720 - yi->osd_x_offset; 886 yi->osd_vis_w = 720 - yi->osd_x_offset;
926 887
927 if (!yi->osd_vis_h) 888 if (!yi->osd_vis_h) {
928 yi->osd_vis_h = yi->decode_height - yi->osd_y_offset; 889 yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
929 else { 890 } else if (yi->osd_vis_h + yi->osd_y_offset > yi->decode_height) {
930 /* If output video standard has changed, requested height may 891 /* If output video standard has changed, requested height may
931 not be legal */ 892 not be legal */
932 if (yi->osd_vis_h + yi->osd_y_offset > yi->decode_height) { 893 IVTV_DEBUG_WARN("Clipping yuv output - fb size (%d) exceeds video standard limit (%d)\n",
933 IVTV_DEBUG_WARN("Clipping yuv output - fb size (%d) exceeds video standard limit (%d)\n", 894 yi->osd_vis_h + yi->osd_y_offset,
934 yi->osd_vis_h + yi->osd_y_offset, 895 yi->decode_height);
935 yi->decode_height); 896 yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
936 yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
937 }
938 } 897 }
939 } 898 }
940 899
941 /* We need a buffer for blanking when Y plane is offset - non-fatal if we can't get one */ 900 /* We need a buffer for blanking when Y plane is offset - non-fatal if we can't get one */
942 yi->blanking_ptr = kzalloc(720*16, GFP_KERNEL); 901 yi->blanking_ptr = kzalloc(720 * 16, GFP_KERNEL);
943 if (yi->blanking_ptr) 902 if (yi->blanking_ptr) {
944 yi->blanking_dmaptr = pci_map_single(itv->dev, yi->blanking_ptr, 720*16, PCI_DMA_TODEVICE); 903 yi->blanking_dmaptr = pci_map_single(itv->dev, yi->blanking_ptr, 720*16, PCI_DMA_TODEVICE);
945 else { 904 } else {
946 yi->blanking_dmaptr = 0; 905 yi->blanking_dmaptr = 0;
947 IVTV_DEBUG_WARN("Failed to allocate yuv blanking buffer\n"); 906 IVTV_DEBUG_WARN("Failed to allocate yuv blanking buffer\n");
948 } 907 }
@@ -954,77 +913,140 @@ static void ivtv_yuv_init (struct ivtv *itv)
954 atomic_set(&yi->next_dma_frame, 0); 913 atomic_set(&yi->next_dma_frame, 0);
955} 914}
956 915
957int ivtv_yuv_prep_frame(struct ivtv *itv, struct ivtv_dma_frame *args) 916/* Get next available yuv buffer on PVR350 */
917void ivtv_yuv_next_free(struct ivtv *itv)
958{ 918{
959 DEFINE_WAIT(wait); 919 int draw, display;
960 int rc = 0; 920 struct yuv_playback_info *yi = &itv->yuv_info;
961 int got_sig = 0;
962 int frame, next_fill_frame, last_fill_frame;
963 int register_update = 0;
964 921
965 IVTV_DEBUG_INFO("yuv_prep_frame\n"); 922 if (atomic_read(&yi->next_dma_frame) == -1)
923 ivtv_yuv_init(itv);
966 924
967 if (atomic_read(&itv->yuv_info.next_dma_frame) == -1) ivtv_yuv_init(itv); 925 draw = atomic_read(&yi->next_fill_frame);
926 display = atomic_read(&yi->next_dma_frame);
968 927
969 frame = atomic_read(&itv->yuv_info.next_fill_frame); 928 if (display > draw)
970 next_fill_frame = (frame + 1) & 0x3; 929 display -= IVTV_YUV_BUFFERS;
971 last_fill_frame = (atomic_read(&itv->yuv_info.next_dma_frame)+1) & 0x3;
972 930
973 if (next_fill_frame != last_fill_frame && last_fill_frame != frame) { 931 if (draw - display >= yi->max_frames_buffered)
974 /* Buffers are full - Overwrite the last frame */ 932 draw = (u8)(draw - 1) % IVTV_YUV_BUFFERS;
975 next_fill_frame = frame; 933 else
976 frame = (frame - 1) & 3; 934 yi->new_frame_info[draw].update = 0;
977 register_update = itv->yuv_info.new_frame_info[frame].update; 935
978 } 936 yi->draw_frame = draw;
937}
938
939/* Set up frame according to ivtv_dma_frame parameters */
940void ivtv_yuv_setup_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
941{
942 struct yuv_playback_info *yi = &itv->yuv_info;
943 u8 frame = yi->draw_frame;
944 u8 last_frame = (u8)(frame - 1) % IVTV_YUV_BUFFERS;
945 struct yuv_frame_info *nf = &yi->new_frame_info[frame];
946 struct yuv_frame_info *of = &yi->new_frame_info[last_frame];
947 int lace_threshold = yi->lace_threshold;
948
949 /* Preserve old update flag in case we're overwriting a queued frame */
950 int update = nf->update;
979 951
980 /* Take a snapshot of the yuv coordinate information */ 952 /* Take a snapshot of the yuv coordinate information */
981 itv->yuv_info.new_frame_info[frame].src_x = args->src.left; 953 nf->src_x = args->src.left;
982 itv->yuv_info.new_frame_info[frame].src_y = args->src.top; 954 nf->src_y = args->src.top;
983 itv->yuv_info.new_frame_info[frame].src_w = args->src.width; 955 nf->src_w = args->src.width;
984 itv->yuv_info.new_frame_info[frame].src_h = args->src.height; 956 nf->src_h = args->src.height;
985 itv->yuv_info.new_frame_info[frame].dst_x = args->dst.left; 957 nf->dst_x = args->dst.left;
986 itv->yuv_info.new_frame_info[frame].dst_y = args->dst.top; 958 nf->dst_y = args->dst.top;
987 itv->yuv_info.new_frame_info[frame].dst_w = args->dst.width; 959 nf->dst_w = args->dst.width;
988 itv->yuv_info.new_frame_info[frame].dst_h = args->dst.height; 960 nf->dst_h = args->dst.height;
989 itv->yuv_info.new_frame_info[frame].tru_x = args->dst.left; 961 nf->tru_x = args->dst.left;
990 itv->yuv_info.new_frame_info[frame].tru_w = args->src_width; 962 nf->tru_w = args->src_width;
991 itv->yuv_info.new_frame_info[frame].tru_h = args->src_height; 963 nf->tru_h = args->src_height;
992
993 /* Snapshot field order */
994 itv->yuv_info.sync_field[frame] = itv->yuv_info.lace_sync_field;
995 964
996 /* Are we going to offset the Y plane */ 965 /* Are we going to offset the Y plane */
997 if (args->src.height + args->src.top < 512-16) 966 nf->offset_y = (nf->tru_h + nf->src_x < 512 - 16) ? 1 : 0;
998 itv->yuv_info.new_frame_info[frame].offset_y = 1;
999 else
1000 itv->yuv_info.new_frame_info[frame].offset_y = 0;
1001 967
1002 /* Snapshot the osd pan info */ 968 /* Snapshot the osd pan info */
1003 itv->yuv_info.new_frame_info[frame].pan_x = itv->yuv_info.osd_x_pan; 969 nf->pan_x = yi->osd_x_pan;
1004 itv->yuv_info.new_frame_info[frame].pan_y = itv->yuv_info.osd_y_pan; 970 nf->pan_y = yi->osd_y_pan;
1005 itv->yuv_info.new_frame_info[frame].vis_w = itv->yuv_info.osd_vis_w; 971 nf->vis_w = yi->osd_vis_w;
1006 itv->yuv_info.new_frame_info[frame].vis_h = itv->yuv_info.osd_vis_h; 972 nf->vis_h = yi->osd_vis_h;
1007 973
1008 itv->yuv_info.new_frame_info[frame].update = 0; 974 nf->update = 0;
1009 itv->yuv_info.new_frame_info[frame].interlaced_y = 0; 975 nf->interlaced_y = 0;
1010 itv->yuv_info.new_frame_info[frame].interlaced_uv = 0; 976 nf->interlaced_uv = 0;
1011 itv->yuv_info.new_frame_info[frame].lace_mode = itv->yuv_info.lace_mode; 977 nf->delay = 0;
1012 978 nf->sync_field = 0;
1013 if (memcmp (&itv->yuv_info.old_frame_info_args, &itv->yuv_info.new_frame_info[frame], 979 nf->lace_mode = yi->lace_mode & IVTV_YUV_MODE_MASK;
1014 sizeof (itv->yuv_info.new_frame_info[frame]))) { 980
1015 memcpy(&itv->yuv_info.old_frame_info_args, &itv->yuv_info.new_frame_info[frame], sizeof (itv->yuv_info.old_frame_info_args)); 981 if (lace_threshold < 0)
1016 itv->yuv_info.new_frame_info[frame].update = 1; 982 lace_threshold = yi->decode_height - 1;
1017/* IVTV_DEBUG_YUV ("Requesting register update for frame %d\n",frame); */ 983
984 /* Work out the lace settings */
985 switch (nf->lace_mode) {
986 case IVTV_YUV_MODE_PROGRESSIVE: /* Progressive mode */
987 nf->interlaced = 0;
988 if (nf->tru_h < 512 || (nf->tru_h > 576 && nf->tru_h < 1021))
989 nf->interlaced_y = 0;
990 else
991 nf->interlaced_y = 1;
992
993 if (nf->tru_h < 1021 && (nf->dst_h >= nf->src_h / 2))
994 nf->interlaced_uv = 0;
995 else
996 nf->interlaced_uv = 1;
997 break;
998
999 case IVTV_YUV_MODE_AUTO:
1000 if (nf->tru_h <= lace_threshold || nf->tru_h > 576 || nf->tru_w > 720) {
1001 nf->interlaced = 0;
1002 if ((nf->tru_h < 512) ||
1003 (nf->tru_h > 576 && nf->tru_h < 1021) ||
1004 (nf->tru_w > 720 && nf->tru_h < 1021))
1005 nf->interlaced_y = 0;
1006 else
1007 nf->interlaced_y = 1;
1008 if (nf->tru_h < 1021 && (nf->dst_h >= nf->src_h / 2))
1009 nf->interlaced_uv = 0;
1010 else
1011 nf->interlaced_uv = 1;
1012 } else {
1013 nf->interlaced = 1;
1014 nf->interlaced_y = 1;
1015 nf->interlaced_uv = 1;
1016 }
1017 break;
1018
1019 case IVTV_YUV_MODE_INTERLACED: /* Interlace mode */
1020 default:
1021 nf->interlaced = 1;
1022 nf->interlaced_y = 1;
1023 nf->interlaced_uv = 1;
1024 break;
1018 } 1025 }
1019 1026
1020 itv->yuv_info.new_frame_info[frame].update |= register_update; 1027 if (memcmp(&yi->old_frame_info_args, nf, sizeof(*nf))) {
1028 yi->old_frame_info_args = *nf;
1029 nf->update = 1;
1030 IVTV_DEBUG_YUV("Requesting reg update for frame %d\n", frame);
1031 }
1021 1032
1022 /* Should this frame be delayed ? */ 1033 nf->update |= update;
1023 if (itv->yuv_info.sync_field[frame] != itv->yuv_info.sync_field[(frame - 1) & 3]) 1034 nf->sync_field = yi->lace_sync_field;
1024 itv->yuv_info.field_delay[frame] = 1; 1035 nf->delay = nf->sync_field != of->sync_field;
1025 else 1036}
1026 itv->yuv_info.field_delay[frame] = 0;
1027 1037
1038/* Frame is complete & ready for display */
1039void ivtv_yuv_frame_complete(struct ivtv *itv)
1040{
1041 atomic_set(&itv->yuv_info.next_fill_frame,
1042 (itv->yuv_info.draw_frame + 1) % IVTV_YUV_BUFFERS);
1043}
1044
1045int ivtv_yuv_udma_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
1046{
1047 DEFINE_WAIT(wait);
1048 int rc = 0;
1049 int got_sig = 0;
1028 /* DMA the frame */ 1050 /* DMA the frame */
1029 mutex_lock(&itv->udma.lock); 1051 mutex_lock(&itv->udma.lock);
1030 1052
@@ -1036,10 +1058,10 @@ int ivtv_yuv_prep_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
1036 ivtv_udma_prepare(itv); 1058 ivtv_udma_prepare(itv);
1037 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE); 1059 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
1038 /* if no UDMA is pending and no UDMA is in progress, then the DMA 1060 /* if no UDMA is pending and no UDMA is in progress, then the DMA
1039 is finished */ 1061 is finished */
1040 while (itv->i_flags & (IVTV_F_I_UDMA_PENDING | IVTV_F_I_UDMA)) { 1062 while (itv->i_flags & (IVTV_F_I_UDMA_PENDING | IVTV_F_I_UDMA)) {
1041 /* don't interrupt if the DMA is in progress but break off 1063 /* don't interrupt if the DMA is in progress but break off
1042 a still pending DMA. */ 1064 a still pending DMA. */
1043 got_sig = signal_pending(current); 1065 got_sig = signal_pending(current);
1044 if (got_sig && test_and_clear_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags)) 1066 if (got_sig && test_and_clear_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags))
1045 break; 1067 break;
@@ -1057,99 +1079,148 @@ int ivtv_yuv_prep_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
1057 return -EINTR; 1079 return -EINTR;
1058 } 1080 }
1059 1081
1060 atomic_set(&itv->yuv_info.next_fill_frame, next_fill_frame); 1082 ivtv_yuv_frame_complete(itv);
1061 1083
1062 mutex_unlock(&itv->udma.lock); 1084 mutex_unlock(&itv->udma.lock);
1063 return rc; 1085 return rc;
1064} 1086}
1065 1087
1088/* Setup frame according to V4L2 parameters */
1089void ivtv_yuv_setup_stream_frame(struct ivtv *itv)
1090{
1091 struct yuv_playback_info *yi = &itv->yuv_info;
1092 struct ivtv_dma_frame dma_args;
1093
1094 ivtv_yuv_next_free(itv);
1095
1096 /* Copy V4L2 parameters to an ivtv_dma_frame struct... */
1097 dma_args.y_source = 0L;
1098 dma_args.uv_source = 0L;
1099 dma_args.src.left = 0;
1100 dma_args.src.top = 0;
1101 dma_args.src.width = yi->v4l2_src_w;
1102 dma_args.src.height = yi->v4l2_src_h;
1103 dma_args.dst = yi->main_rect;
1104 dma_args.src_width = yi->v4l2_src_w;
1105 dma_args.src_height = yi->v4l2_src_h;
1106
1107 /* ... and use the same setup routine as ivtv_yuv_prep_frame */
1108 ivtv_yuv_setup_frame(itv, &dma_args);
1109
1110 if (!itv->dma_data_req_offset)
1111 itv->dma_data_req_offset = yuv_offset[yi->draw_frame];
1112}
1113
1114/* Attempt to dma a frame from a user buffer */
1115int ivtv_yuv_udma_stream_frame(struct ivtv *itv, void *src)
1116{
1117 struct yuv_playback_info *yi = &itv->yuv_info;
1118 struct ivtv_dma_frame dma_args;
1119
1120 ivtv_yuv_setup_stream_frame(itv);
1121
1122 /* We only need to supply source addresses for this */
1123 dma_args.y_source = src;
1124 dma_args.uv_source = src + 720 * ((yi->v4l2_src_h + 31) & ~31);
1125 return ivtv_yuv_udma_frame(itv, &dma_args);
1126}
1127
1128/* IVTV_IOC_DMA_FRAME ioctl handler */
1129int ivtv_yuv_prep_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
1130{
1131/* IVTV_DEBUG_INFO("yuv_prep_frame\n"); */
1132
1133 ivtv_yuv_next_free(itv);
1134 ivtv_yuv_setup_frame(itv, args);
1135 return ivtv_yuv_udma_frame(itv, args);
1136}
1137
1066void ivtv_yuv_close(struct ivtv *itv) 1138void ivtv_yuv_close(struct ivtv *itv)
1067{ 1139{
1140 struct yuv_playback_info *yi = &itv->yuv_info;
1068 int h_filter, v_filter_1, v_filter_2; 1141 int h_filter, v_filter_1, v_filter_2;
1069 1142
1070 IVTV_DEBUG_YUV("ivtv_yuv_close\n"); 1143 IVTV_DEBUG_YUV("ivtv_yuv_close\n");
1071 ivtv_waitq(&itv->vsync_waitq); 1144 ivtv_waitq(&itv->vsync_waitq);
1072 1145
1073 atomic_set(&itv->yuv_info.next_dma_frame, -1); 1146 atomic_set(&yi->next_dma_frame, -1);
1074 atomic_set(&itv->yuv_info.next_fill_frame, 0); 1147 atomic_set(&yi->next_fill_frame, 0);
1075 1148
1076 /* Reset registers we have changed so mpeg playback works */ 1149 /* Reset registers we have changed so mpeg playback works */
1077 1150
1078 /* If we fully restore this register, the display may remain active. 1151 /* If we fully restore this register, the display may remain active.
1079 Restore, but set one bit to blank the video. Firmware will always 1152 Restore, but set one bit to blank the video. Firmware will always
1080 clear this bit when needed, so not a problem. */ 1153 clear this bit when needed, so not a problem. */
1081 write_reg(itv->yuv_info.reg_2898 | 0x01000000, 0x2898); 1154 write_reg(yi->reg_2898 | 0x01000000, 0x2898);
1082 1155
1083 write_reg(itv->yuv_info.reg_2834, 0x02834); 1156 write_reg(yi->reg_2834, 0x02834);
1084 write_reg(itv->yuv_info.reg_2838, 0x02838); 1157 write_reg(yi->reg_2838, 0x02838);
1085 write_reg(itv->yuv_info.reg_283c, 0x0283c); 1158 write_reg(yi->reg_283c, 0x0283c);
1086 write_reg(itv->yuv_info.reg_2840, 0x02840); 1159 write_reg(yi->reg_2840, 0x02840);
1087 write_reg(itv->yuv_info.reg_2844, 0x02844); 1160 write_reg(yi->reg_2844, 0x02844);
1088 write_reg(itv->yuv_info.reg_2848, 0x02848); 1161 write_reg(yi->reg_2848, 0x02848);
1089 write_reg(itv->yuv_info.reg_2854, 0x02854); 1162 write_reg(yi->reg_2854, 0x02854);
1090 write_reg(itv->yuv_info.reg_285c, 0x0285c); 1163 write_reg(yi->reg_285c, 0x0285c);
1091 write_reg(itv->yuv_info.reg_2864, 0x02864); 1164 write_reg(yi->reg_2864, 0x02864);
1092 write_reg(itv->yuv_info.reg_2870, 0x02870); 1165 write_reg(yi->reg_2870, 0x02870);
1093 write_reg(itv->yuv_info.reg_2874, 0x02874); 1166 write_reg(yi->reg_2874, 0x02874);
1094 write_reg(itv->yuv_info.reg_2890, 0x02890); 1167 write_reg(yi->reg_2890, 0x02890);
1095 write_reg(itv->yuv_info.reg_289c, 0x0289c); 1168 write_reg(yi->reg_289c, 0x0289c);
1096 1169
1097 write_reg(itv->yuv_info.reg_2918, 0x02918); 1170 write_reg(yi->reg_2918, 0x02918);
1098 write_reg(itv->yuv_info.reg_291c, 0x0291c); 1171 write_reg(yi->reg_291c, 0x0291c);
1099 write_reg(itv->yuv_info.reg_2920, 0x02920); 1172 write_reg(yi->reg_2920, 0x02920);
1100 write_reg(itv->yuv_info.reg_2924, 0x02924); 1173 write_reg(yi->reg_2924, 0x02924);
1101 write_reg(itv->yuv_info.reg_2928, 0x02928); 1174 write_reg(yi->reg_2928, 0x02928);
1102 write_reg(itv->yuv_info.reg_292c, 0x0292c); 1175 write_reg(yi->reg_292c, 0x0292c);
1103 write_reg(itv->yuv_info.reg_2930, 0x02930); 1176 write_reg(yi->reg_2930, 0x02930);
1104 write_reg(itv->yuv_info.reg_2934, 0x02934); 1177 write_reg(yi->reg_2934, 0x02934);
1105 write_reg(itv->yuv_info.reg_2938, 0x02938); 1178 write_reg(yi->reg_2938, 0x02938);
1106 write_reg(itv->yuv_info.reg_293c, 0x0293c); 1179 write_reg(yi->reg_293c, 0x0293c);
1107 write_reg(itv->yuv_info.reg_2940, 0x02940); 1180 write_reg(yi->reg_2940, 0x02940);
1108 write_reg(itv->yuv_info.reg_2944, 0x02944); 1181 write_reg(yi->reg_2944, 0x02944);
1109 write_reg(itv->yuv_info.reg_2948, 0x02948); 1182 write_reg(yi->reg_2948, 0x02948);
1110 write_reg(itv->yuv_info.reg_294c, 0x0294c); 1183 write_reg(yi->reg_294c, 0x0294c);
1111 write_reg(itv->yuv_info.reg_2950, 0x02950); 1184 write_reg(yi->reg_2950, 0x02950);
1112 write_reg(itv->yuv_info.reg_2954, 0x02954); 1185 write_reg(yi->reg_2954, 0x02954);
1113 write_reg(itv->yuv_info.reg_2958, 0x02958); 1186 write_reg(yi->reg_2958, 0x02958);
1114 write_reg(itv->yuv_info.reg_295c, 0x0295c); 1187 write_reg(yi->reg_295c, 0x0295c);
1115 write_reg(itv->yuv_info.reg_2960, 0x02960); 1188 write_reg(yi->reg_2960, 0x02960);
1116 write_reg(itv->yuv_info.reg_2964, 0x02964); 1189 write_reg(yi->reg_2964, 0x02964);
1117 write_reg(itv->yuv_info.reg_2968, 0x02968); 1190 write_reg(yi->reg_2968, 0x02968);
1118 write_reg(itv->yuv_info.reg_296c, 0x0296c); 1191 write_reg(yi->reg_296c, 0x0296c);
1119 write_reg(itv->yuv_info.reg_2970, 0x02970); 1192 write_reg(yi->reg_2970, 0x02970);
1120 1193
1121 /* Prepare to restore filters */ 1194 /* Prepare to restore filters */
1122 1195
1123 /* First the horizontal filter */ 1196 /* First the horizontal filter */
1124 if ((itv->yuv_info.reg_2834 & 0x0000FFFF) == (itv->yuv_info.reg_2834 >> 16)) { 1197 if ((yi->reg_2834 & 0x0000FFFF) == (yi->reg_2834 >> 16)) {
1125 /* An exact size match uses filter 0 */ 1198 /* An exact size match uses filter 0 */
1126 h_filter = 0; 1199 h_filter = 0;
1127 } 1200 } else {
1128 else {
1129 /* Figure out which filter to use */ 1201 /* Figure out which filter to use */
1130 h_filter = ((itv->yuv_info.reg_2834 << 16) / (itv->yuv_info.reg_2834 >> 16)) >> 15; 1202 h_filter = ((yi->reg_2834 << 16) / (yi->reg_2834 >> 16)) >> 15;
1131 h_filter = (h_filter >> 1) + (h_filter & 1); 1203 h_filter = (h_filter >> 1) + (h_filter & 1);
1132 /* Only an exact size match can use filter 0. */ 1204 /* Only an exact size match can use filter 0. */
1133 if (h_filter < 1) h_filter = 1; 1205 h_filter += !h_filter;
1134 } 1206 }
1135 1207
1136 /* Now the vertical filter */ 1208 /* Now the vertical filter */
1137 if ((itv->yuv_info.reg_2918 & 0x0000FFFF) == (itv->yuv_info.reg_2918 >> 16)) { 1209 if ((yi->reg_2918 & 0x0000FFFF) == (yi->reg_2918 >> 16)) {
1138 /* An exact size match uses filter 0/1 */ 1210 /* An exact size match uses filter 0/1 */
1139 v_filter_1 = 0; 1211 v_filter_1 = 0;
1140 v_filter_2 = 1; 1212 v_filter_2 = 1;
1141 } 1213 } else {
1142 else {
1143 /* Figure out which filter to use */ 1214 /* Figure out which filter to use */
1144 v_filter_1 = ((itv->yuv_info.reg_2918 << 16) / (itv->yuv_info.reg_2918 >> 16)) >> 15; 1215 v_filter_1 = ((yi->reg_2918 << 16) / (yi->reg_2918 >> 16)) >> 15;
1145 v_filter_1 = (v_filter_1 >> 1) + (v_filter_1 & 1); 1216 v_filter_1 = (v_filter_1 >> 1) + (v_filter_1 & 1);
1146 /* Only an exact size match can use filter 0 */ 1217 /* Only an exact size match can use filter 0 */
1147 if (v_filter_1 == 0) v_filter_1 = 1; 1218 v_filter_1 += !v_filter_1;
1148 v_filter_2 = v_filter_1; 1219 v_filter_2 = v_filter_1;
1149 } 1220 }
1150 1221
1151 /* Now restore the filters */ 1222 /* Now restore the filters */
1152 ivtv_yuv_filter (itv,h_filter,v_filter_1,v_filter_2); 1223 ivtv_yuv_filter(itv, h_filter, v_filter_1, v_filter_2);
1153 1224
1154 /* and clear a few registers */ 1225 /* and clear a few registers */
1155 write_reg(0, 0x02814); 1226 write_reg(0, 0x02814);
@@ -1158,19 +1229,18 @@ void ivtv_yuv_close(struct ivtv *itv)
1158 write_reg(0, 0x02910); 1229 write_reg(0, 0x02910);
1159 1230
1160 /* Release the blanking buffer */ 1231 /* Release the blanking buffer */
1161 if (itv->yuv_info.blanking_ptr) { 1232 if (yi->blanking_ptr) {
1162 kfree (itv->yuv_info.blanking_ptr); 1233 kfree(yi->blanking_ptr);
1163 itv->yuv_info.blanking_ptr = NULL; 1234 yi->blanking_ptr = NULL;
1164 pci_unmap_single(itv->dev, itv->yuv_info.blanking_dmaptr, 720*16, PCI_DMA_TODEVICE); 1235 pci_unmap_single(itv->dev, yi->blanking_dmaptr, 720*16, PCI_DMA_TODEVICE);
1165 } 1236 }
1166 1237
1167 /* Invalidate the old dimension information */ 1238 /* Invalidate the old dimension information */
1168 itv->yuv_info.old_frame_info.src_w = 0; 1239 yi->old_frame_info.src_w = 0;
1169 itv->yuv_info.old_frame_info.src_h = 0; 1240 yi->old_frame_info.src_h = 0;
1170 itv->yuv_info.old_frame_info_args.src_w = 0; 1241 yi->old_frame_info_args.src_w = 0;
1171 itv->yuv_info.old_frame_info_args.src_h = 0; 1242 yi->old_frame_info_args.src_h = 0;
1172 1243
1173 /* All done. */ 1244 /* All done. */
1174 clear_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags); 1245 clear_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags);
1175} 1246}
1176
diff --git a/drivers/media/video/ivtv/ivtv-yuv.h b/drivers/media/video/ivtv/ivtv-yuv.h
index 3b966f0a204a..2fe5f1250762 100644
--- a/drivers/media/video/ivtv/ivtv-yuv.h
+++ b/drivers/media/video/ivtv/ivtv-yuv.h
@@ -21,11 +21,6 @@
21#ifndef IVTV_YUV_H 21#ifndef IVTV_YUV_H
22#define IVTV_YUV_H 22#define IVTV_YUV_H
23 23
24/* Buffers on hardware offsets */
25#define IVTV_YUV_BUFFER_OFFSET 0x001a8600 /* First YUV Buffer */
26#define IVTV_YUV_BUFFER_OFFSET_1 0x00240400 /* Second YUV Buffer */
27#define IVTV_YUV_BUFFER_OFFSET_2 0x002d8200 /* Third YUV Buffer */
28#define IVTV_YUV_BUFFER_OFFSET_3 0x00370000 /* Fourth YUV Buffer */
29#define IVTV_YUV_BUFFER_UV_OFFSET 0x65400 /* Offset to UV Buffer */ 24#define IVTV_YUV_BUFFER_UV_OFFSET 0x65400 /* Offset to UV Buffer */
30 25
31/* Offset to filter table in firmware */ 26/* Offset to filter table in firmware */
@@ -36,11 +31,14 @@
36#define IVTV_YUV_UPDATE_VERTICAL 0x02 31#define IVTV_YUV_UPDATE_VERTICAL 0x02
37#define IVTV_YUV_UPDATE_INVALID 0x04 32#define IVTV_YUV_UPDATE_INVALID 0x04
38 33
39extern const u32 yuv_offset[4]; 34extern const u32 yuv_offset[IVTV_YUV_BUFFERS];
40 35
41int ivtv_yuv_filter_check(struct ivtv *itv); 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 *src);
39void ivtv_yuv_frame_complete(struct ivtv *itv);
42int ivtv_yuv_prep_frame(struct ivtv *itv, struct ivtv_dma_frame *args); 40int ivtv_yuv_prep_frame(struct ivtv *itv, struct ivtv_dma_frame *args);
43void ivtv_yuv_close(struct ivtv *itv); 41void ivtv_yuv_close(struct ivtv *itv);
44void ivtv_yuv_work_handler (struct ivtv *itv); 42void ivtv_yuv_work_handler(struct ivtv *itv);
45 43
46#endif 44#endif
diff --git a/drivers/media/video/ivtv/ivtvfb.c b/drivers/media/video/ivtv/ivtvfb.c
index 52ffd154a3d8..3b23fc05f7c4 100644
--- a/drivers/media/video/ivtv/ivtvfb.c
+++ b/drivers/media/video/ivtv/ivtvfb.c
@@ -504,6 +504,10 @@ static int ivtvfb_set_var(struct ivtv *itv, struct fb_var_screeninfo *var)
504 504
505 ivtvfb_set_display_window(itv, &ivtv_window); 505 ivtvfb_set_display_window(itv, &ivtv_window);
506 506
507 /* Pass screen size back to yuv handler */
508 itv->yuv_info.osd_full_w = ivtv_osd.pixel_stride;
509 itv->yuv_info.osd_full_h = ivtv_osd.lines;
510
507 /* Force update of yuv registers */ 511 /* Force update of yuv registers */
508 itv->yuv_info.yuv_forced_update = 1; 512 itv->yuv_info.yuv_forced_update = 1;
509 513
@@ -1053,7 +1057,7 @@ static int ivtvfb_init_card(struct ivtv *itv)
1053 } 1057 }
1054 1058
1055 itv->osd_info = kzalloc(sizeof(struct osd_info), GFP_ATOMIC); 1059 itv->osd_info = kzalloc(sizeof(struct osd_info), GFP_ATOMIC);
1056 if (itv->osd_info == 0) { 1060 if (itv->osd_info == NULL) {
1057 IVTVFB_ERR("Failed to allocate memory for osd_info\n"); 1061 IVTVFB_ERR("Failed to allocate memory for osd_info\n");
1058 return -ENOMEM; 1062 return -ENOMEM;
1059 } 1063 }
diff --git a/drivers/media/video/m52790.c b/drivers/media/video/m52790.c
new file mode 100644
index 000000000000..d4bf14c284ef
--- /dev/null
+++ b/drivers/media/video/m52790.c
@@ -0,0 +1,168 @@
1/*
2 * m52790 i2c ivtv driver.
3 * Copyright (C) 2007 Hans Verkuil
4 *
5 * A/V source switching Mitsubishi M52790SP/FP
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
23#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/ioctl.h>
26#include <asm/uaccess.h>
27#include <linux/i2c.h>
28#include <linux/i2c-id.h>
29#include <linux/videodev.h>
30#include <media/m52790.h>
31#include <media/v4l2-common.h>
32#include <media/v4l2-chip-ident.h>
33#include <media/v4l2-i2c-drv.h>
34
35MODULE_DESCRIPTION("i2c device driver for m52790 A/V switch");
36MODULE_AUTHOR("Hans Verkuil");
37MODULE_LICENSE("GPL");
38
39
40struct m52790_state {
41 u16 input;
42 u16 output;
43};
44
45/* ----------------------------------------------------------------------- */
46
47static int m52790_write(struct i2c_client *client)
48{
49 struct m52790_state *state = i2c_get_clientdata(client);
50 u8 sw1 = (state->input | state->output) & 0xff;
51 u8 sw2 = (state->input | state->output) >> 8;
52
53 return i2c_smbus_write_byte_data(client, sw1, sw2);
54}
55
56static int m52790_command(struct i2c_client *client, unsigned int cmd,
57 void *arg)
58{
59 struct m52790_state *state = i2c_get_clientdata(client);
60 struct v4l2_routing *route = arg;
61
62 /* Note: audio and video are linked and cannot be switched separately.
63 So audio and video routing commands are identical for this chip.
64 In theory the video amplifier and audio modes could be handled
65 separately for the output, but that seems to be overkill right now.
66 The same holds for implementing an audio mute control, this is now
67 part of the audio output routing. The normal case is that another
68 chip takes care of the actual muting so making it part of the
69 output routing seems to be the right thing to do for now. */
70 switch (cmd) {
71 case VIDIOC_INT_G_AUDIO_ROUTING:
72 case VIDIOC_INT_G_VIDEO_ROUTING:
73 route->input = state->input;
74 route->output = state->output;
75 break;
76
77 case VIDIOC_INT_S_AUDIO_ROUTING:
78 case VIDIOC_INT_S_VIDEO_ROUTING:
79 state->input = route->input;
80 state->output = route->output;
81 m52790_write(client);
82 break;
83
84#ifdef CONFIG_VIDEO_ADV_DEBUG
85 case VIDIOC_DBG_G_REGISTER:
86 case VIDIOC_DBG_S_REGISTER:
87 {
88 struct v4l2_register *reg = arg;
89
90 if (!v4l2_chip_match_i2c_client(client,
91 reg->match_type, reg->match_chip))
92 return -EINVAL;
93 if (!capable(CAP_SYS_ADMIN))
94 return -EPERM;
95 if (reg->reg != 0)
96 return -EINVAL;
97 if (cmd == VIDIOC_DBG_G_REGISTER)
98 reg->val = state->input | state->output;
99 else {
100 state->input = reg->val & 0x0303;
101 state->output = reg->val & ~0x0303;
102 m52790_write(client);
103 }
104 break;
105 }
106#endif
107
108 case VIDIOC_G_CHIP_IDENT:
109 return v4l2_chip_ident_i2c_client(client, arg,
110 V4L2_IDENT_M52790, 0);
111
112 case VIDIOC_LOG_STATUS:
113 v4l_info(client, "Switch 1: %02x\n",
114 (state->input | state->output) & 0xff);
115 v4l_info(client, "Switch 2: %02x\n",
116 (state->input | state->output) >> 8);
117 break;
118
119 default:
120 return -EINVAL;
121 }
122 return 0;
123}
124
125/* ----------------------------------------------------------------------- */
126
127/* i2c implementation */
128
129static int m52790_probe(struct i2c_client *client)
130{
131 struct m52790_state *state;
132
133 /* Check if the adapter supports the needed features */
134 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
135 return -EIO;
136
137 snprintf(client->name, sizeof(client->name) - 1, "m52790");
138
139 v4l_info(client, "chip found @ 0x%x (%s)\n",
140 client->addr << 1, client->adapter->name);
141
142 state = kmalloc(sizeof(struct m52790_state), GFP_KERNEL);
143 if (state == NULL)
144 return -ENOMEM;
145
146 state->input = M52790_IN_TUNER;
147 state->output = M52790_OUT_STEREO;
148 i2c_set_clientdata(client, state);
149 m52790_write(client);
150 return 0;
151}
152
153static int m52790_remove(struct i2c_client *client)
154{
155 kfree(i2c_get_clientdata(client));
156 return 0;
157}
158
159/* ----------------------------------------------------------------------- */
160
161static struct v4l2_i2c_driver_data v4l2_i2c_data = {
162 .name = "m52790",
163 .driverid = I2C_DRIVERID_M52790,
164 .command = m52790_command,
165 .probe = m52790_probe,
166 .remove = m52790_remove,
167};
168
diff --git a/drivers/media/video/meye.c b/drivers/media/video/meye.c
index c31163290432..3d51fa0a52b6 100644
--- a/drivers/media/video/meye.c
+++ b/drivers/media/video/meye.c
@@ -2023,7 +2023,7 @@ static int __init meye_init(void)
2023 if (gbufsize < 0 || gbufsize > MEYE_MAX_BUFSIZE) 2023 if (gbufsize < 0 || gbufsize > MEYE_MAX_BUFSIZE)
2024 gbufsize = MEYE_MAX_BUFSIZE; 2024 gbufsize = MEYE_MAX_BUFSIZE;
2025 gbufsize = PAGE_ALIGN(gbufsize); 2025 gbufsize = PAGE_ALIGN(gbufsize);
2026 printk(KERN_INFO "meye: using %d buffers with %dk (%dk total)" 2026 printk(KERN_INFO "meye: using %d buffers with %dk (%dk total) "
2027 "for capture\n", 2027 "for capture\n",
2028 gbuffers, 2028 gbuffers,
2029 gbufsize / 1024, gbuffers * gbufsize / 1024); 2029 gbufsize / 1024, gbuffers * gbufsize / 1024);
diff --git a/drivers/media/video/msp3400-driver.c b/drivers/media/video/msp3400-driver.c
index c0c87e06259b..7a11f3159e32 100644
--- a/drivers/media/video/msp3400-driver.c
+++ b/drivers/media/video/msp3400-driver.c
@@ -42,7 +42,8 @@
42 * 42 *
43 * You should have received a copy of the GNU General Public License 43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software 44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 45 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
46 * 02110-1301, USA.
46 */ 47 */
47 48
48 49
@@ -53,6 +54,7 @@
53#include <linux/videodev.h> 54#include <linux/videodev.h>
54#include <linux/videodev2.h> 55#include <linux/videodev2.h>
55#include <media/v4l2-common.h> 56#include <media/v4l2-common.h>
57#include <media/v4l2-i2c-drv-legacy.h>
56#include <media/tvaudio.h> 58#include <media/tvaudio.h>
57#include <media/msp3400.h> 59#include <media/msp3400.h>
58#include <linux/kthread.h> 60#include <linux/kthread.h>
@@ -71,7 +73,8 @@ int msp_debug; /* msp_debug output */
71int msp_once; /* no continous stereo monitoring */ 73int msp_once; /* no continous stereo monitoring */
72int msp_amsound; /* hard-wire AM sound at 6.5 Hz (france), 74int msp_amsound; /* hard-wire AM sound at 6.5 Hz (france),
73 the autoscan seems work well only with FM... */ 75 the autoscan seems work well only with FM... */
74int msp_standard = 1; /* Override auto detect of audio msp_standard, if needed. */ 76int msp_standard = 1; /* Override auto detect of audio msp_standard,
77 if needed. */
75int msp_dolby; 78int msp_dolby;
76 79
77int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual 80int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
@@ -81,12 +84,12 @@ int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
81module_param(opmode, int, 0444); 84module_param(opmode, int, 0444);
82 85
83/* read-write */ 86/* read-write */
84module_param_named(once,msp_once, bool, 0644); 87module_param_named(once, msp_once, bool, 0644);
85module_param_named(debug,msp_debug, int, 0644); 88module_param_named(debug, msp_debug, int, 0644);
86module_param_named(stereo_threshold,msp_stereo_thresh, int, 0644); 89module_param_named(stereo_threshold, msp_stereo_thresh, int, 0644);
87module_param_named(standard,msp_standard, int, 0644); 90module_param_named(standard, msp_standard, int, 0644);
88module_param_named(amsound,msp_amsound, bool, 0644); 91module_param_named(amsound, msp_amsound, bool, 0644);
89module_param_named(dolby,msp_dolby, bool, 0644); 92module_param_named(dolby, msp_dolby, bool, 0644);
90 93
91MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect"); 94MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
92MODULE_PARM_DESC(once, "No continuous stereo monitoring"); 95MODULE_PARM_DESC(once, "No continuous stereo monitoring");
@@ -160,12 +163,13 @@ static int msp_read(struct i2c_client *client, int dev, int addr)
160 schedule_timeout_interruptible(msecs_to_jiffies(10)); 163 schedule_timeout_interruptible(msecs_to_jiffies(10));
161 } 164 }
162 if (err == 3) { 165 if (err == 3) {
163 v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n"); 166 v4l_warn(client, "resetting chip, sound will go off.\n");
164 msp_reset(client); 167 msp_reset(client);
165 return -1; 168 return -1;
166 } 169 }
167 retval = read[0] << 8 | read[1]; 170 retval = read[0] << 8 | read[1];
168 v4l_dbg(3, msp_debug, client, "msp_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval); 171 v4l_dbg(3, msp_debug, client, "msp_read(0x%x, 0x%x): 0x%x\n",
172 dev, addr, retval);
169 return retval; 173 return retval;
170} 174}
171 175
@@ -190,7 +194,8 @@ static int msp_write(struct i2c_client *client, int dev, int addr, int val)
190 buffer[3] = val >> 8; 194 buffer[3] = val >> 8;
191 buffer[4] = val & 0xff; 195 buffer[4] = val & 0xff;
192 196
193 v4l_dbg(3, msp_debug, client, "msp_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val); 197 v4l_dbg(3, msp_debug, client, "msp_write(0x%x, 0x%x, 0x%x)\n",
198 dev, addr, val);
194 for (err = 0; err < 3; err++) { 199 for (err = 0; err < 3; err++) {
195 if (i2c_master_send(client, buffer, 5) == 5) 200 if (i2c_master_send(client, buffer, 5) == 5)
196 break; 201 break;
@@ -199,7 +204,7 @@ static int msp_write(struct i2c_client *client, int dev, int addr, int val)
199 schedule_timeout_interruptible(msecs_to_jiffies(10)); 204 schedule_timeout_interruptible(msecs_to_jiffies(10));
200 } 205 }
201 if (err == 3) { 206 if (err == 3) {
202 v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n"); 207 v4l_warn(client, "resetting chip, sound will go off.\n");
203 msp_reset(client); 208 msp_reset(client);
204 return -1; 209 return -1;
205 } 210 }
@@ -273,7 +278,7 @@ void msp_set_scart(struct i2c_client *client, int in, int out)
273 state->acb = 0xf60; /* Mute Input and SCART 1 Output */ 278 state->acb = 0xf60; /* Mute Input and SCART 1 Output */
274 279
275 v4l_dbg(1, msp_debug, client, "scart switch: %s => %d (ACB=0x%04x)\n", 280 v4l_dbg(1, msp_debug, client, "scart switch: %s => %d (ACB=0x%04x)\n",
276 scart_names[in], out, state->acb); 281 scart_names[in], out, state->acb);
277 msp_write_dsp(client, 0x13, state->acb); 282 msp_write_dsp(client, 0x13, state->acb);
278 283
279 /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */ 284 /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
@@ -292,7 +297,8 @@ void msp_set_audio(struct i2c_client *client)
292 val = (state->volume * 0x7f / 65535) << 8; 297 val = (state->volume * 0x7f / 65535) << 8;
293 298
294 v4l_dbg(1, msp_debug, client, "mute=%s scanning=%s volume=%d\n", 299 v4l_dbg(1, msp_debug, client, "mute=%s scanning=%s volume=%d\n",
295 state->muted ? "on" : "off", state->scan_in_progress ? "yes" : "no", 300 state->muted ? "on" : "off",
301 state->scan_in_progress ? "yes" : "no",
296 state->volume); 302 state->volume);
297 303
298 msp_write_dsp(client, 0x0000, val); 304 msp_write_dsp(client, 0x0000, val);
@@ -681,14 +687,14 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
681 v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", *a); 687 v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", *a);
682 688
683 switch (*a) { 689 switch (*a) {
684 case 1024000: 690 case 1024000:
685 state->i2s_mode = 0; 691 state->i2s_mode = 0;
686 break; 692 break;
687 case 2048000: 693 case 2048000:
688 state->i2s_mode = 1; 694 state->i2s_mode = 1;
689 break; 695 break;
690 default: 696 default:
691 return -EINVAL; 697 return -EINVAL;
692 } 698 }
693 break; 699 break;
694 } 700 }
@@ -698,22 +704,22 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
698 struct v4l2_queryctrl *qc = arg; 704 struct v4l2_queryctrl *qc = arg;
699 705
700 switch (qc->id) { 706 switch (qc->id) {
701 case V4L2_CID_AUDIO_VOLUME: 707 case V4L2_CID_AUDIO_VOLUME:
702 case V4L2_CID_AUDIO_MUTE: 708 case V4L2_CID_AUDIO_MUTE:
703 return v4l2_ctrl_query_fill_std(qc); 709 return v4l2_ctrl_query_fill_std(qc);
704 default: 710 default:
705 break; 711 break;
706 } 712 }
707 if (!state->has_sound_processing) 713 if (!state->has_sound_processing)
708 return -EINVAL; 714 return -EINVAL;
709 switch (qc->id) { 715 switch (qc->id) {
710 case V4L2_CID_AUDIO_LOUDNESS: 716 case V4L2_CID_AUDIO_LOUDNESS:
711 case V4L2_CID_AUDIO_BALANCE: 717 case V4L2_CID_AUDIO_BALANCE:
712 case V4L2_CID_AUDIO_BASS: 718 case V4L2_CID_AUDIO_BASS:
713 case V4L2_CID_AUDIO_TREBLE: 719 case V4L2_CID_AUDIO_TREBLE:
714 return v4l2_ctrl_query_fill_std(qc); 720 return v4l2_ctrl_query_fill_std(qc);
715 default: 721 default:
716 return -EINVAL; 722 return -EINVAL;
717 } 723 }
718 } 724 }
719 725
@@ -735,13 +741,14 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
735 state->volume, state->muted ? " (muted)" : ""); 741 state->volume, state->muted ? " (muted)" : "");
736 if (state->has_sound_processing) { 742 if (state->has_sound_processing) {
737 v4l_info(client, "Audio: balance %d bass %d treble %d loudness %s\n", 743 v4l_info(client, "Audio: balance %d bass %d treble %d loudness %s\n",
738 state->balance, state->bass, state->treble, 744 state->balance, state->bass,
745 state->treble,
739 state->loudness ? "on" : "off"); 746 state->loudness ? "on" : "off");
740 } 747 }
741 switch (state->mode) { 748 switch (state->mode) {
742 case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break; 749 case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
743 case MSP_MODE_FM_RADIO: p = "FM Radio"; break; 750 case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
744 case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono + FM-stereo"; break; 751 case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono/stereo"; break;
745 case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break; 752 case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
746 case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break; 753 case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
747 case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break; 754 case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
@@ -772,7 +779,8 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
772 } 779 }
773 780
774 case VIDIOC_G_CHIP_IDENT: 781 case VIDIOC_G_CHIP_IDENT:
775 return v4l2_chip_ident_i2c_client(client, arg, state->ident, (state->rev1 << 16) | state->rev2); 782 return v4l2_chip_ident_i2c_client(client, arg, state->ident,
783 (state->rev1 << 16) | state->rev2);
776 784
777 default: 785 default:
778 /* unknown */ 786 /* unknown */
@@ -783,7 +791,6 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
783 791
784static int msp_suspend(struct i2c_client *client, pm_message_t state) 792static int msp_suspend(struct i2c_client *client, pm_message_t state)
785{ 793{
786
787 v4l_dbg(1, msp_debug, client, "suspend\n"); 794 v4l_dbg(1, msp_debug, client, "suspend\n");
788 msp_reset(client); 795 msp_reset(client);
789 return 0; 796 return 0;
@@ -791,7 +798,6 @@ static int msp_suspend(struct i2c_client *client, pm_message_t state)
791 798
792static int msp_resume(struct i2c_client *client) 799static int msp_resume(struct i2c_client *client)
793{ 800{
794
795 v4l_dbg(1, msp_debug, client, "resume\n"); 801 v4l_dbg(1, msp_debug, client, "resume\n");
796 msp_wake_thread(client); 802 msp_wake_thread(client);
797 return 0; 803 return 0;
@@ -799,11 +805,8 @@ static int msp_resume(struct i2c_client *client)
799 805
800/* ----------------------------------------------------------------------- */ 806/* ----------------------------------------------------------------------- */
801 807
802static struct i2c_driver i2c_driver; 808static int msp_probe(struct i2c_client *client)
803
804static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
805{ 809{
806 struct i2c_client *client;
807 struct msp_state *state; 810 struct msp_state *state;
808 int (*thread_func)(void *data) = NULL; 811 int (*thread_func)(void *data) = NULL;
809 int msp_hard; 812 int msp_hard;
@@ -812,26 +815,16 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
812 int msp_product, msp_prod_hi, msp_prod_lo; 815 int msp_product, msp_prod_hi, msp_prod_lo;
813 int msp_rom; 816 int msp_rom;
814 817
815 client = kzalloc(sizeof(*client), GFP_KERNEL);
816 if (!client)
817 return -ENOMEM;
818
819 client->addr = address;
820 client->adapter = adapter;
821 client->driver = &i2c_driver;
822 snprintf(client->name, sizeof(client->name) - 1, "msp3400"); 818 snprintf(client->name, sizeof(client->name) - 1, "msp3400");
823 819
824 if (msp_reset(client) == -1) { 820 if (msp_reset(client) == -1) {
825 v4l_dbg(1, msp_debug, client, "msp3400 not found\n"); 821 v4l_dbg(1, msp_debug, client, "msp3400 not found\n");
826 kfree(client); 822 return -ENODEV;
827 return 0;
828 } 823 }
829 824
830 state = kzalloc(sizeof(*state), GFP_KERNEL); 825 state = kzalloc(sizeof(*state), GFP_KERNEL);
831 if (!state) { 826 if (!state)
832 kfree(client);
833 return -ENOMEM; 827 return -ENOMEM;
834 }
835 828
836 i2c_set_clientdata(client, state); 829 i2c_set_clientdata(client, state);
837 830
@@ -853,12 +846,13 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
853 state->rev1 = msp_read_dsp(client, 0x1e); 846 state->rev1 = msp_read_dsp(client, 0x1e);
854 if (state->rev1 != -1) 847 if (state->rev1 != -1)
855 state->rev2 = msp_read_dsp(client, 0x1f); 848 state->rev2 = msp_read_dsp(client, 0x1f);
856 v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n", state->rev1, state->rev2); 849 v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n",
850 state->rev1, state->rev2);
857 if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) { 851 if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
858 v4l_dbg(1, msp_debug, client, "not an msp3400 (cannot read chip version)\n"); 852 v4l_dbg(1, msp_debug, client,
853 "not an msp3400 (cannot read chip version)\n");
859 kfree(state); 854 kfree(state);
860 kfree(client); 855 return -ENODEV;
861 return 0;
862 } 856 }
863 857
864 msp_set_audio(client); 858 msp_set_audio(client);
@@ -874,37 +868,55 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
874 msp_family, msp_product, 868 msp_family, msp_product,
875 msp_revision, msp_hard, msp_rom); 869 msp_revision, msp_hard, msp_rom);
876 /* Rev B=2, C=3, D=4, G=7 */ 870 /* Rev B=2, C=3, D=4, G=7 */
877 state->ident = msp_family * 10000 + 4000 + msp_product * 10 + msp_revision - '@'; 871 state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
872 msp_revision - '@';
878 873
879 /* Has NICAM support: all mspx41x and mspx45x products have NICAM */ 874 /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
880 state->has_nicam = msp_prod_hi == 1 || msp_prod_hi == 5; 875 state->has_nicam =
876 msp_prod_hi == 1 || msp_prod_hi == 5;
881 /* Has radio support: was added with revision G */ 877 /* Has radio support: was added with revision G */
882 state->has_radio = msp_revision >= 'G'; 878 state->has_radio =
879 msp_revision >= 'G';
883 /* Has headphones output: not for stripped down products */ 880 /* Has headphones output: not for stripped down products */
884 state->has_headphones = msp_prod_lo < 5; 881 state->has_headphones =
882 msp_prod_lo < 5;
885 /* Has scart2 input: not in stripped down products of the '3' family */ 883 /* Has scart2 input: not in stripped down products of the '3' family */
886 state->has_scart2 = msp_family >= 4 || msp_prod_lo < 7; 884 state->has_scart2 =
885 msp_family >= 4 || msp_prod_lo < 7;
887 /* Has scart3 input: not in stripped down products of the '3' family */ 886 /* Has scart3 input: not in stripped down products of the '3' family */
888 state->has_scart3 = msp_family >= 4 || msp_prod_lo < 5; 887 state->has_scart3 =
888 msp_family >= 4 || msp_prod_lo < 5;
889 /* Has scart4 input: not in pre D revisions, not in stripped D revs */ 889 /* Has scart4 input: not in pre D revisions, not in stripped D revs */
890 state->has_scart4 = msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5); 890 state->has_scart4 =
891 /* Has scart2 output: not in stripped down products of the '3' family */ 891 msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
892 state->has_scart2_out = msp_family >= 4 || msp_prod_lo < 5; 892 /* Has scart2 output: not in stripped down products of
893 * the '3' family */
894 state->has_scart2_out =
895 msp_family >= 4 || msp_prod_lo < 5;
893 /* Has scart2 a volume control? Not in pre-D revisions. */ 896 /* Has scart2 a volume control? Not in pre-D revisions. */
894 state->has_scart2_out_volume = msp_revision > 'C' && state->has_scart2_out; 897 state->has_scart2_out_volume =
898 msp_revision > 'C' && state->has_scart2_out;
895 /* Has a configurable i2s out? */ 899 /* Has a configurable i2s out? */
896 state->has_i2s_conf = msp_revision >= 'G' && msp_prod_lo < 7; 900 state->has_i2s_conf =
897 /* Has subwoofer output: not in pre-D revs and not in stripped down products */ 901 msp_revision >= 'G' && msp_prod_lo < 7;
898 state->has_subwoofer = msp_revision >= 'D' && msp_prod_lo < 5; 902 /* Has subwoofer output: not in pre-D revs and not in stripped down
899 /* Has soundprocessing (bass/treble/balance/loudness/equalizer): not in 903 * products */
900 stripped down products */ 904 state->has_subwoofer =
901 state->has_sound_processing = msp_prod_lo < 7; 905 msp_revision >= 'D' && msp_prod_lo < 5;
906 /* Has soundprocessing (bass/treble/balance/loudness/equalizer):
907 * not in stripped down products */
908 state->has_sound_processing =
909 msp_prod_lo < 7;
902 /* Has Virtual Dolby Surround: only in msp34x1 */ 910 /* Has Virtual Dolby Surround: only in msp34x1 */
903 state->has_virtual_dolby_surround = msp_revision == 'G' && msp_prod_lo == 1; 911 state->has_virtual_dolby_surround =
912 msp_revision == 'G' && msp_prod_lo == 1;
904 /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */ 913 /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
905 state->has_dolby_pro_logic = msp_revision == 'G' && msp_prod_lo == 2; 914 state->has_dolby_pro_logic =
906 /* The msp343xG supports BTSC only and cannot do Automatic Standard Detection. */ 915 msp_revision == 'G' && msp_prod_lo == 2;
907 state->force_btsc = msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3; 916 /* The msp343xG supports BTSC only and cannot do Automatic Standard
917 * Detection. */
918 state->force_btsc =
919 msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
908 920
909 state->opmode = opmode; 921 state->opmode = opmode;
910 if (state->opmode == OPMODE_AUTO) { 922 if (state->opmode == OPMODE_AUTO) {
@@ -919,32 +931,33 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
919 } 931 }
920 932
921 /* hello world :-) */ 933 /* hello world :-) */
922 v4l_info(client, "%s found @ 0x%x (%s)\n", client->name, address << 1, adapter->name); 934 v4l_info(client, "%s found @ 0x%x (%s)\n", client->name,
935 client->addr << 1, client->adapter->name);
923 v4l_info(client, "%s ", client->name); 936 v4l_info(client, "%s ", client->name);
924 if (state->has_nicam && state->has_radio) 937 if (state->has_nicam && state->has_radio)
925 printk("supports nicam and radio, "); 938 printk(KERN_CONT "supports nicam and radio, ");
926 else if (state->has_nicam) 939 else if (state->has_nicam)
927 printk("supports nicam, "); 940 printk(KERN_CONT "supports nicam, ");
928 else if (state->has_radio) 941 else if (state->has_radio)
929 printk("supports radio, "); 942 printk(KERN_CONT "supports radio, ");
930 printk("mode is "); 943 printk(KERN_CONT "mode is ");
931 944
932 /* version-specific initialization */ 945 /* version-specific initialization */
933 switch (state->opmode) { 946 switch (state->opmode) {
934 case OPMODE_MANUAL: 947 case OPMODE_MANUAL:
935 printk("manual"); 948 printk(KERN_CONT "manual");
936 thread_func = msp3400c_thread; 949 thread_func = msp3400c_thread;
937 break; 950 break;
938 case OPMODE_AUTODETECT: 951 case OPMODE_AUTODETECT:
939 printk("autodetect"); 952 printk(KERN_CONT "autodetect");
940 thread_func = msp3410d_thread; 953 thread_func = msp3410d_thread;
941 break; 954 break;
942 case OPMODE_AUTOSELECT: 955 case OPMODE_AUTOSELECT:
943 printk("autodetect and autoselect"); 956 printk(KERN_CONT "autodetect and autoselect");
944 thread_func = msp34xxg_thread; 957 thread_func = msp34xxg_thread;
945 break; 958 break;
946 } 959 }
947 printk("\n"); 960 printk(KERN_CONT "\n");
948 961
949 /* startup control thread if needed */ 962 /* startup control thread if needed */
950 if (thread_func) { 963 if (thread_func) {
@@ -954,24 +967,12 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
954 v4l_warn(client, "kernel_thread() failed\n"); 967 v4l_warn(client, "kernel_thread() failed\n");
955 msp_wake_thread(client); 968 msp_wake_thread(client);
956 } 969 }
957
958 /* done */
959 i2c_attach_client(client);
960
961 return 0; 970 return 0;
962} 971}
963 972
964static int msp_probe(struct i2c_adapter *adapter) 973static int msp_remove(struct i2c_client *client)
965{
966 if (adapter->class & I2C_CLASS_TV_ANALOG)
967 return i2c_probe(adapter, &addr_data, msp_attach);
968 return 0;
969}
970
971static int msp_detach(struct i2c_client *client)
972{ 974{
973 struct msp_state *state = i2c_get_clientdata(client); 975 struct msp_state *state = i2c_get_clientdata(client);
974 int err;
975 976
976 /* shutdown control thread */ 977 /* shutdown control thread */
977 if (state->kthread) { 978 if (state->kthread) {
@@ -980,43 +981,22 @@ static int msp_detach(struct i2c_client *client)
980 } 981 }
981 msp_reset(client); 982 msp_reset(client);
982 983
983 err = i2c_detach_client(client);
984 if (err) {
985 return err;
986 }
987
988 kfree(state); 984 kfree(state);
989 kfree(client);
990 return 0; 985 return 0;
991} 986}
992 987
993/* ----------------------------------------------------------------------- */ 988/* ----------------------------------------------------------------------- */
994 989
995/* i2c implementation */ 990static struct v4l2_i2c_driver_data v4l2_i2c_data = {
996static struct i2c_driver i2c_driver = { 991 .name = "msp3400",
997 .id = I2C_DRIVERID_MSP3400, 992 .driverid = I2C_DRIVERID_MSP3400,
998 .attach_adapter = msp_probe, 993 .command = msp_command,
999 .detach_client = msp_detach, 994 .probe = msp_probe,
995 .remove = msp_remove,
1000 .suspend = msp_suspend, 996 .suspend = msp_suspend,
1001 .resume = msp_resume, 997 .resume = msp_resume,
1002 .command = msp_command,
1003 .driver = {
1004 .name = "msp3400",
1005 },
1006}; 998};
1007 999
1008static int __init msp3400_init_module(void)
1009{
1010 return i2c_add_driver(&i2c_driver);
1011}
1012
1013static void __exit msp3400_cleanup_module(void)
1014{
1015 i2c_del_driver(&i2c_driver);
1016}
1017
1018module_init(msp3400_init_module);
1019module_exit(msp3400_cleanup_module);
1020 1000
1021/* 1001/*
1022 * Overrides for Emacs so that we follow Linus's tabbing style. 1002 * Overrides for Emacs so that we follow Linus's tabbing style.
diff --git a/drivers/media/video/msp3400-kthreads.c b/drivers/media/video/msp3400-kthreads.c
index d5ee2629121e..61ec794a737e 100644
--- a/drivers/media/video/msp3400-kthreads.c
+++ b/drivers/media/video/msp3400-kthreads.c
@@ -15,7 +15,8 @@
15 * 15 *
16 * You should have received a copy of the GNU General Public License 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 17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
19 */ 20 */
20 21
21 22
@@ -78,37 +79,37 @@ static struct msp3400c_init_data_dem {
78 {75, 19, 36, 35, 39, 40}, 79 {75, 19, 36, 35, 39, 40},
79 MSP_CARRIER(5.5), MSP_CARRIER(5.5), 80 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
80 0x00d0, 0x0500, 0x0020, 0x3000 81 0x00d0, 0x0500, 0x0020, 0x3000
81 },{ /* AM (for carrier detect / msp3410) */ 82 }, { /* AM (for carrier detect / msp3410) */
82 {-1, -1, -8, 2, 59, 126}, 83 {-1, -1, -8, 2, 59, 126},
83 {-1, -1, -8, 2, 59, 126}, 84 {-1, -1, -8, 2, 59, 126},
84 MSP_CARRIER(5.5), MSP_CARRIER(5.5), 85 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
85 0x00d0, 0x0100, 0x0020, 0x3000 86 0x00d0, 0x0100, 0x0020, 0x3000
86 },{ /* FM Radio */ 87 }, { /* FM Radio */
87 {-8, -8, 4, 6, 78, 107}, 88 {-8, -8, 4, 6, 78, 107},
88 {-8, -8, 4, 6, 78, 107}, 89 {-8, -8, 4, 6, 78, 107},
89 MSP_CARRIER(10.7), MSP_CARRIER(10.7), 90 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
90 0x00d0, 0x0480, 0x0020, 0x3000 91 0x00d0, 0x0480, 0x0020, 0x3000
91 },{ /* Terrestial FM-mono + FM-stereo */ 92 }, { /* Terrestial FM-mono + FM-stereo */
92 {3, 18, 27, 48, 66, 72}, 93 {3, 18, 27, 48, 66, 72},
93 {3, 18, 27, 48, 66, 72}, 94 {3, 18, 27, 48, 66, 72},
94 MSP_CARRIER(5.5), MSP_CARRIER(5.5), 95 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
95 0x00d0, 0x0480, 0x0030, 0x3000 96 0x00d0, 0x0480, 0x0030, 0x3000
96 },{ /* Sat FM-mono */ 97 }, { /* Sat FM-mono */
97 { 1, 9, 14, 24, 33, 37}, 98 { 1, 9, 14, 24, 33, 37},
98 { 3, 18, 27, 48, 66, 72}, 99 { 3, 18, 27, 48, 66, 72},
99 MSP_CARRIER(6.5), MSP_CARRIER(6.5), 100 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
100 0x00c6, 0x0480, 0x0000, 0x3000 101 0x00c6, 0x0480, 0x0000, 0x3000
101 },{ /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */ 102 }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
102 {-2, -8, -10, 10, 50, 86}, 103 {-2, -8, -10, 10, 50, 86},
103 {3, 18, 27, 48, 66, 72}, 104 {3, 18, 27, 48, 66, 72},
104 MSP_CARRIER(5.5), MSP_CARRIER(5.5), 105 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
105 0x00d0, 0x0040, 0x0120, 0x3000 106 0x00d0, 0x0040, 0x0120, 0x3000
106 },{ /* NICAM/FM -- I (6.0/6.552) */ 107 }, { /* NICAM/FM -- I (6.0/6.552) */
107 {2, 4, -6, -4, 40, 94}, 108 {2, 4, -6, -4, 40, 94},
108 {3, 18, 27, 48, 66, 72}, 109 {3, 18, 27, 48, 66, 72},
109 MSP_CARRIER(6.0), MSP_CARRIER(6.0), 110 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
110 0x00d0, 0x0040, 0x0120, 0x3000 111 0x00d0, 0x0040, 0x0120, 0x3000
111 },{ /* NICAM/AM -- L (6.5/5.85) */ 112 }, { /* NICAM/AM -- L (6.5/5.85) */
112 {-2, -8, -10, 10, 50, 86}, 113 {-2, -8, -10, 10, 50, 86},
113 {-4, -12, -9, 23, 79, 126}, 114 {-4, -12, -9, 23, 79, 126},
114 MSP_CARRIER(6.5), MSP_CARRIER(6.5), 115 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
@@ -224,7 +225,9 @@ void msp3400c_set_mode(struct i2c_client *client, int mode)
224 nor do they support stereo BTSC. */ 225 nor do they support stereo BTSC. */
225static void msp3400c_set_audmode(struct i2c_client *client) 226static void msp3400c_set_audmode(struct i2c_client *client)
226{ 227{
227 static char *strmode[] = { "mono", "stereo", "lang2", "lang1", "lang1+lang2" }; 228 static char *strmode[] = {
229 "mono", "stereo", "lang2", "lang1", "lang1+lang2"
230 };
228 struct msp_state *state = i2c_get_clientdata(client); 231 struct msp_state *state = i2c_get_clientdata(client);
229 char *modestr = (state->audmode >= 0 && state->audmode < 5) ? 232 char *modestr = (state->audmode >= 0 && state->audmode < 5) ?
230 strmode[state->audmode] : "unknown"; 233 strmode[state->audmode] : "unknown";
@@ -298,19 +301,23 @@ static void msp3400c_set_audmode(struct i2c_client *client)
298 case MSP_MODE_FM_NICAM1: 301 case MSP_MODE_FM_NICAM1:
299 case MSP_MODE_FM_NICAM2: 302 case MSP_MODE_FM_NICAM2:
300 case MSP_MODE_AM_NICAM: 303 case MSP_MODE_AM_NICAM:
301 v4l_dbg(1, msp_debug, client, "NICAM set_audmode: %s\n",modestr); 304 v4l_dbg(1, msp_debug, client,
305 "NICAM set_audmode: %s\n", modestr);
302 if (state->nicam_on) 306 if (state->nicam_on)
303 src = 0x0100; /* NICAM */ 307 src = 0x0100; /* NICAM */
304 break; 308 break;
305 case MSP_MODE_BTSC: 309 case MSP_MODE_BTSC:
306 v4l_dbg(1, msp_debug, client, "BTSC set_audmode: %s\n",modestr); 310 v4l_dbg(1, msp_debug, client,
311 "BTSC set_audmode: %s\n", modestr);
307 break; 312 break;
308 case MSP_MODE_EXTERN: 313 case MSP_MODE_EXTERN:
309 v4l_dbg(1, msp_debug, client, "extern set_audmode: %s\n",modestr); 314 v4l_dbg(1, msp_debug, client,
315 "extern set_audmode: %s\n", modestr);
310 src = 0x0200; /* SCART */ 316 src = 0x0200; /* SCART */
311 break; 317 break;
312 case MSP_MODE_FM_RADIO: 318 case MSP_MODE_FM_RADIO:
313 v4l_dbg(1, msp_debug, client, "FM-Radio set_audmode: %s\n",modestr); 319 v4l_dbg(1, msp_debug, client,
320 "FM-Radio set_audmode: %s\n", modestr);
314 break; 321 break;
315 default: 322 default:
316 v4l_dbg(1, msp_debug, client, "mono set_audmode\n"); 323 v4l_dbg(1, msp_debug, client, "mono set_audmode\n");
@@ -342,7 +349,8 @@ static void msp3400c_set_audmode(struct i2c_client *client)
342 src |= 0x0010; 349 src |= 0x0010;
343 break; 350 break;
344 } 351 }
345 v4l_dbg(1, msp_debug, client, "set_audmode final source/matrix = 0x%x\n", src); 352 v4l_dbg(1, msp_debug, client,
353 "set_audmode final source/matrix = 0x%x\n", src);
346 354
347 msp_set_source(client, src); 355 msp_set_source(client, src);
348} 356}
@@ -351,22 +359,26 @@ static void msp3400c_print_mode(struct i2c_client *client)
351{ 359{
352 struct msp_state *state = i2c_get_clientdata(client); 360 struct msp_state *state = i2c_get_clientdata(client);
353 361
354 if (state->main == state->second) { 362 if (state->main == state->second)
355 v4l_dbg(1, msp_debug, client, "mono sound carrier: %d.%03d MHz\n", 363 v4l_dbg(1, msp_debug, client,
356 state->main / 910000, (state->main / 910) % 1000); 364 "mono sound carrier: %d.%03d MHz\n",
357 } else { 365 state->main / 910000, (state->main / 910) % 1000);
358 v4l_dbg(1, msp_debug, client, "main sound carrier: %d.%03d MHz\n", 366 else
359 state->main / 910000, (state->main / 910) % 1000); 367 v4l_dbg(1, msp_debug, client,
360 } 368 "main sound carrier: %d.%03d MHz\n",
369 state->main / 910000, (state->main / 910) % 1000);
361 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2) 370 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
362 v4l_dbg(1, msp_debug, client, "NICAM/FM carrier : %d.%03d MHz\n", 371 v4l_dbg(1, msp_debug, client,
363 state->second / 910000, (state->second/910) % 1000); 372 "NICAM/FM carrier : %d.%03d MHz\n",
373 state->second / 910000, (state->second/910) % 1000);
364 if (state->mode == MSP_MODE_AM_NICAM) 374 if (state->mode == MSP_MODE_AM_NICAM)
365 v4l_dbg(1, msp_debug, client, "NICAM/AM carrier : %d.%03d MHz\n", 375 v4l_dbg(1, msp_debug, client,
366 state->second / 910000, (state->second / 910) % 1000); 376 "NICAM/AM carrier : %d.%03d MHz\n",
377 state->second / 910000, (state->second / 910) % 1000);
367 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) { 378 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
368 v4l_dbg(1, msp_debug, client, "FM-stereo carrier : %d.%03d MHz\n", 379 v4l_dbg(1, msp_debug, client,
369 state->second / 910000, (state->second / 910) % 1000); 380 "FM-stereo carrier : %d.%03d MHz\n",
381 state->second / 910000, (state->second / 910) % 1000);
370 } 382 }
371} 383}
372 384
@@ -385,7 +397,8 @@ static int msp3400c_detect_stereo(struct i2c_client *client)
385 val = msp_read_dsp(client, 0x18); 397 val = msp_read_dsp(client, 0x18);
386 if (val > 32767) 398 if (val > 32767)
387 val -= 65536; 399 val -= 65536;
388 v4l_dbg(2, msp_debug, client, "stereo detect register: %d\n", val); 400 v4l_dbg(2, msp_debug, client,
401 "stereo detect register: %d\n", val);
389 if (val > 8192) { 402 if (val > 8192) {
390 rxsubchans = V4L2_TUNER_SUB_STEREO; 403 rxsubchans = V4L2_TUNER_SUB_STEREO;
391 } else if (val < -4096) { 404 } else if (val < -4096) {
@@ -430,7 +443,8 @@ static int msp3400c_detect_stereo(struct i2c_client *client)
430 } 443 }
431 if (rxsubchans != state->rxsubchans) { 444 if (rxsubchans != state->rxsubchans) {
432 update = 1; 445 update = 1;
433 v4l_dbg(1, msp_debug, client, "watch: rxsubchans %02x => %02x\n", 446 v4l_dbg(1, msp_debug, client,
447 "watch: rxsubchans %02x => %02x\n",
434 state->rxsubchans, rxsubchans); 448 state->rxsubchans, rxsubchans);
435 state->rxsubchans = rxsubchans; 449 state->rxsubchans = rxsubchans;
436 } 450 }
@@ -452,9 +466,8 @@ static void watch_stereo(struct i2c_client *client)
452{ 466{
453 struct msp_state *state = i2c_get_clientdata(client); 467 struct msp_state *state = i2c_get_clientdata(client);
454 468
455 if (msp_detect_stereo(client)) { 469 if (msp_detect_stereo(client))
456 msp_set_audmode(client); 470 msp_set_audmode(client);
457 }
458 471
459 if (msp_once) 472 if (msp_once)
460 state->watch_stereo = 0; 473 state->watch_stereo = 0;
@@ -465,7 +478,7 @@ int msp3400c_thread(void *data)
465 struct i2c_client *client = data; 478 struct i2c_client *client = data;
466 struct msp_state *state = i2c_get_clientdata(client); 479 struct msp_state *state = i2c_get_clientdata(client);
467 struct msp3400c_carrier_detect *cd; 480 struct msp3400c_carrier_detect *cd;
468 int count, max1, max2, val1, val2, val, this; 481 int count, max1, max2, val1, val2, val, i;
469 482
470 483
471 v4l_dbg(1, msp_debug, client, "msp3400 daemon started\n"); 484 v4l_dbg(1, msp_debug, client, "msp3400 daemon started\n");
@@ -475,7 +488,7 @@ int msp3400c_thread(void *data)
475 msp_sleep(state, -1); 488 msp_sleep(state, -1);
476 v4l_dbg(2, msp_debug, client, "msp3400 thread: wakeup\n"); 489 v4l_dbg(2, msp_debug, client, "msp3400 thread: wakeup\n");
477 490
478 restart: 491restart:
479 v4l_dbg(2, msp_debug, client, "thread: restart scan\n"); 492 v4l_dbg(2, msp_debug, client, "thread: restart scan\n");
480 state->restart = 0; 493 state->restart = 0;
481 if (kthread_should_stop()) 494 if (kthread_should_stop())
@@ -483,7 +496,8 @@ int msp3400c_thread(void *data)
483 496
484 if (state->radio || MSP_MODE_EXTERN == state->mode) { 497 if (state->radio || MSP_MODE_EXTERN == state->mode) {
485 /* no carrier scan, just unmute */ 498 /* no carrier scan, just unmute */
486 v4l_dbg(1, msp_debug, client, "thread: no carrier scan\n"); 499 v4l_dbg(1, msp_debug, client,
500 "thread: no carrier scan\n");
487 state->scan_in_progress = 0; 501 state->scan_in_progress = 0;
488 msp_set_audio(client); 502 msp_set_audio(client);
489 continue; 503 continue;
@@ -514,16 +528,17 @@ int msp3400c_thread(void *data)
514 v4l_dbg(1, msp_debug, client, "AM sound override\n"); 528 v4l_dbg(1, msp_debug, client, "AM sound override\n");
515 } 529 }
516 530
517 for (this = 0; this < count; this++) { 531 for (i = 0; i < count; i++) {
518 msp3400c_set_carrier(client, cd[this].cdo, cd[this].cdo); 532 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
519 if (msp_sleep(state,100)) 533 if (msp_sleep(state, 100))
520 goto restart; 534 goto restart;
521 val = msp_read_dsp(client, 0x1b); 535 val = msp_read_dsp(client, 0x1b);
522 if (val > 32767) 536 if (val > 32767)
523 val -= 65536; 537 val -= 65536;
524 if (val1 < val) 538 if (val1 < val)
525 val1 = val, max1 = this; 539 val1 = val, max1 = i;
526 v4l_dbg(1, msp_debug, client, "carrier1 val: %5d / %s\n", val,cd[this].name); 540 v4l_dbg(1, msp_debug, client,
541 "carrier1 val: %5d / %s\n", val, cd[i].name);
527 } 542 }
528 543
529 /* carrier detect pass #2 -- second (stereo) carrier */ 544 /* carrier detect pass #2 -- second (stereo) carrier */
@@ -550,16 +565,17 @@ int msp3400c_thread(void *data)
550 count = 0; 565 count = 0;
551 max2 = 0; 566 max2 = 0;
552 } 567 }
553 for (this = 0; this < count; this++) { 568 for (i = 0; i < count; i++) {
554 msp3400c_set_carrier(client, cd[this].cdo, cd[this].cdo); 569 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
555 if (msp_sleep(state,100)) 570 if (msp_sleep(state, 100))
556 goto restart; 571 goto restart;
557 val = msp_read_dsp(client, 0x1b); 572 val = msp_read_dsp(client, 0x1b);
558 if (val > 32767) 573 if (val > 32767)
559 val -= 65536; 574 val -= 65536;
560 if (val2 < val) 575 if (val2 < val)
561 val2 = val, max2 = this; 576 val2 = val, max2 = i;
562 v4l_dbg(1, msp_debug, client, "carrier2 val: %5d / %s\n", val,cd[this].name); 577 v4l_dbg(1, msp_debug, client,
578 "carrier2 val: %5d / %s\n", val, cd[i].name);
563 } 579 }
564 580
565 /* program the msp3400 according to the results */ 581 /* program the msp3400 according to the results */
@@ -611,7 +627,7 @@ int msp3400c_thread(void *data)
611 break; 627 break;
612 case 0: /* 4.5 */ 628 case 0: /* 4.5 */
613 default: 629 default:
614 no_second: 630no_second:
615 state->second = msp3400c_carrier_detect_main[max1].cdo; 631 state->second = msp3400c_carrier_detect_main[max1].cdo;
616 msp3400c_set_mode(client, MSP_MODE_FM_TERRA); 632 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
617 break; 633 break;
@@ -632,7 +648,8 @@ int msp3400c_thread(void *data)
632 while (state->watch_stereo) { 648 while (state->watch_stereo) {
633 if (msp_sleep(state, count ? 1000 : 5000)) 649 if (msp_sleep(state, count ? 1000 : 5000))
634 goto restart; 650 goto restart;
635 if (count) count--; 651 if (count)
652 count--;
636 watch_stereo(client); 653 watch_stereo(client);
637 } 654 }
638 } 655 }
@@ -651,10 +668,10 @@ int msp3410d_thread(void *data)
651 set_freezable(); 668 set_freezable();
652 for (;;) { 669 for (;;) {
653 v4l_dbg(2, msp_debug, client, "msp3410 thread: sleep\n"); 670 v4l_dbg(2, msp_debug, client, "msp3410 thread: sleep\n");
654 msp_sleep(state,-1); 671 msp_sleep(state, -1);
655 v4l_dbg(2, msp_debug, client, "msp3410 thread: wakeup\n"); 672 v4l_dbg(2, msp_debug, client, "msp3410 thread: wakeup\n");
656 673
657 restart: 674restart:
658 v4l_dbg(2, msp_debug, client, "thread: restart scan\n"); 675 v4l_dbg(2, msp_debug, client, "thread: restart scan\n");
659 state->restart = 0; 676 state->restart = 0;
660 if (kthread_should_stop()) 677 if (kthread_should_stop())
@@ -662,7 +679,8 @@ int msp3410d_thread(void *data)
662 679
663 if (state->mode == MSP_MODE_EXTERN) { 680 if (state->mode == MSP_MODE_EXTERN) {
664 /* no carrier scan needed, just unmute */ 681 /* no carrier scan needed, just unmute */
665 v4l_dbg(1, msp_debug, client, "thread: no carrier scan\n"); 682 v4l_dbg(1, msp_debug, client,
683 "thread: no carrier scan\n");
666 state->scan_in_progress = 0; 684 state->scan_in_progress = 0;
667 msp_set_audio(client); 685 msp_set_audio(client);
668 continue; 686 continue;
@@ -673,7 +691,8 @@ int msp3410d_thread(void *data)
673 msp_set_audio(client); 691 msp_set_audio(client);
674 692
675 /* start autodetect. Note: autodetect is not supported for 693 /* start autodetect. Note: autodetect is not supported for
676 NTSC-M and radio, hence we force the standard in those cases. */ 694 NTSC-M and radio, hence we force the standard in those
695 cases. */
677 if (state->radio) 696 if (state->radio)
678 std = 0x40; 697 std = 0x40;
679 else 698 else
@@ -686,8 +705,9 @@ int msp3410d_thread(void *data)
686 goto restart; 705 goto restart;
687 706
688 if (msp_debug) 707 if (msp_debug)
689 v4l_dbg(2, msp_debug, client, "setting standard: %s (0x%04x)\n", 708 v4l_dbg(2, msp_debug, client,
690 msp_standard_std_name(std), std); 709 "setting standard: %s (0x%04x)\n",
710 msp_standard_std_name(std), std);
691 711
692 if (std != 1) { 712 if (std != 1) {
693 /* programmed some specific mode */ 713 /* programmed some specific mode */
@@ -703,7 +723,8 @@ int msp3410d_thread(void *data)
703 val = msp_read_dem(client, 0x7e); 723 val = msp_read_dem(client, 0x7e);
704 if (val < 0x07ff) 724 if (val < 0x07ff)
705 break; 725 break;
706 v4l_dbg(2, msp_debug, client, "detection still in progress\n"); 726 v4l_dbg(2, msp_debug, client,
727 "detection still in progress\n");
707 } 728 }
708 } 729 }
709 for (i = 0; msp_stdlist[i].name != NULL; i++) 730 for (i = 0; msp_stdlist[i].name != NULL; i++)
@@ -716,12 +737,13 @@ int msp3410d_thread(void *data)
716 state->std = val; 737 state->std = val;
717 state->rxsubchans = V4L2_TUNER_SUB_MONO; 738 state->rxsubchans = V4L2_TUNER_SUB_MONO;
718 739
719 if (msp_amsound && !state->radio && (state->v4l2_std & V4L2_STD_SECAM) && 740 if (msp_amsound && !state->radio &&
720 (val != 0x0009)) { 741 (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) {
721 /* autodetection has failed, let backup */ 742 /* autodetection has failed, let backup */
722 v4l_dbg(1, msp_debug, client, "autodetection failed," 743 v4l_dbg(1, msp_debug, client, "autodetection failed,"
723 " switching to backup standard: %s (0x%04x)\n", 744 " switching to backup standard: %s (0x%04x)\n",
724 msp_stdlist[8].name ? msp_stdlist[8].name : "unknown",val); 745 msp_stdlist[8].name ?
746 msp_stdlist[8].name : "unknown", val);
725 state->std = val = 0x0009; 747 state->std = val = 0x0009;
726 msp_write_dem(client, 0x20, val); 748 msp_write_dem(client, 0x20, val);
727 } 749 }
@@ -786,7 +808,8 @@ int msp3410d_thread(void *data)
786 while (state->watch_stereo) { 808 while (state->watch_stereo) {
787 if (msp_sleep(state, count ? 1000 : 5000)) 809 if (msp_sleep(state, count ? 1000 : 5000))
788 goto restart; 810 goto restart;
789 if (count) count--; 811 if (count)
812 count--;
790 watch_stereo(client); 813 watch_stereo(client);
791 } 814 }
792 } 815 }
@@ -872,8 +895,8 @@ static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
872 else 895 else
873 source = (in << 8) | matrix; 896 source = (in << 8) | matrix;
874 897
875 v4l_dbg(1, msp_debug, client, "set source to %d (0x%x) for output %02x\n", 898 v4l_dbg(1, msp_debug, client,
876 in, source, reg); 899 "set source to %d (0x%x) for output %02x\n", in, source, reg);
877 msp_write_dsp(client, reg, source); 900 msp_write_dsp(client, reg, source);
878} 901}
879 902
@@ -948,7 +971,7 @@ int msp34xxg_thread(void *data)
948 msp_sleep(state, -1); 971 msp_sleep(state, -1);
949 v4l_dbg(2, msp_debug, client, "msp34xxg thread: wakeup\n"); 972 v4l_dbg(2, msp_debug, client, "msp34xxg thread: wakeup\n");
950 973
951 restart: 974restart:
952 v4l_dbg(1, msp_debug, client, "thread: restart scan\n"); 975 v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
953 state->restart = 0; 976 state->restart = 0;
954 if (kthread_should_stop()) 977 if (kthread_should_stop())
@@ -956,7 +979,8 @@ int msp34xxg_thread(void *data)
956 979
957 if (state->mode == MSP_MODE_EXTERN) { 980 if (state->mode == MSP_MODE_EXTERN) {
958 /* no carrier scan needed, just unmute */ 981 /* no carrier scan needed, just unmute */
959 v4l_dbg(1, msp_debug, client, "thread: no carrier scan\n"); 982 v4l_dbg(1, msp_debug, client,
983 "thread: no carrier scan\n");
960 state->scan_in_progress = 0; 984 state->scan_in_progress = 0;
961 msp_set_audio(client); 985 msp_set_audio(client);
962 continue; 986 continue;
@@ -972,7 +996,8 @@ int msp34xxg_thread(void *data)
972 goto unmute; 996 goto unmute;
973 997
974 /* watch autodetect */ 998 /* watch autodetect */
975 v4l_dbg(1, msp_debug, client, "started autodetect, waiting for result\n"); 999 v4l_dbg(1, msp_debug, client,
1000 "started autodetect, waiting for result\n");
976 for (i = 0; i < 10; i++) { 1001 for (i = 0; i < 10; i++) {
977 if (msp_sleep(state, 100)) 1002 if (msp_sleep(state, 100))
978 goto restart; 1003 goto restart;
@@ -983,15 +1008,18 @@ int msp34xxg_thread(void *data)
983 state->std = val; 1008 state->std = val;
984 break; 1009 break;
985 } 1010 }
986 v4l_dbg(2, msp_debug, client, "detection still in progress\n"); 1011 v4l_dbg(2, msp_debug, client,
1012 "detection still in progress\n");
987 } 1013 }
988 if (state->std == 1) { 1014 if (state->std == 1) {
989 v4l_dbg(1, msp_debug, client, "detection still in progress after 10 tries. giving up.\n"); 1015 v4l_dbg(1, msp_debug, client,
1016 "detection still in progress after 10 tries. giving up.\n");
990 continue; 1017 continue;
991 } 1018 }
992 1019
993 unmute: 1020unmute:
994 v4l_dbg(1, msp_debug, client, "detected standard: %s (0x%04x)\n", 1021 v4l_dbg(1, msp_debug, client,
1022 "detected standard: %s (0x%04x)\n",
995 msp_standard_std_name(state->std), state->std); 1023 msp_standard_std_name(state->std), state->std);
996 1024
997 if (state->std == 9) { 1025 if (state->std == 9) {
@@ -1046,9 +1074,11 @@ static int msp34xxg_detect_stereo(struct i2c_client *client)
1046 if (state->std == 0x20) 1074 if (state->std == 0x20)
1047 state->rxsubchans |= V4L2_TUNER_SUB_SAP; 1075 state->rxsubchans |= V4L2_TUNER_SUB_SAP;
1048 else 1076 else
1049 state->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; 1077 state->rxsubchans =
1078 V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1050 } 1079 }
1051 v4l_dbg(1, msp_debug, client, "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n", 1080 v4l_dbg(1, msp_debug, client,
1081 "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1052 status, is_stereo, is_bilingual, state->rxsubchans); 1082 status, is_stereo, is_bilingual, state->rxsubchans);
1053 return (oldrx != state->rxsubchans); 1083 return (oldrx != state->rxsubchans);
1054} 1084}
diff --git a/drivers/media/video/mt20xx.c b/drivers/media/video/mt20xx.c
index f49d1f4c40db..b630c26cfe85 100644
--- a/drivers/media/video/mt20xx.c
+++ b/drivers/media/video/mt20xx.c
@@ -14,7 +14,7 @@ static int debug = 0;
14module_param(debug, int, 0644); 14module_param(debug, int, 0644);
15MODULE_PARM_DESC(debug, "enable verbose debug messages"); 15MODULE_PARM_DESC(debug, "enable verbose debug messages");
16 16
17#define PREFIX "mt20xx " 17#define PREFIX "mt20xx"
18 18
19/* ---------------------------------------------------------------------- */ 19/* ---------------------------------------------------------------------- */
20 20
diff --git a/drivers/media/video/pvrusb2/Kconfig b/drivers/media/video/pvrusb2/Kconfig
index d0c2cd785430..6fc1b8be1a1f 100644
--- a/drivers/media/video/pvrusb2/Kconfig
+++ b/drivers/media/video/pvrusb2/Kconfig
@@ -5,6 +5,10 @@ config VIDEO_PVRUSB2
5 select VIDEO_TUNER 5 select VIDEO_TUNER
6 select VIDEO_TVEEPROM 6 select VIDEO_TVEEPROM
7 select VIDEO_CX2341X 7 select VIDEO_CX2341X
8 select VIDEO_SAA711X
9 select VIDEO_CX25840
10 select VIDEO_MSP3400
11 select VIDEO_WM8775
8 ---help--- 12 ---help---
9 This is a video4linux driver for Conexant 23416 based 13 This is a video4linux driver for Conexant 23416 based
10 usb2 personal video recorder devices. 14 usb2 personal video recorder devices.
@@ -12,32 +16,29 @@ config VIDEO_PVRUSB2
12 To compile this driver as a module, choose M here: the 16 To compile this driver as a module, choose M here: the
13 module will be called pvrusb2 17 module will be called pvrusb2
14 18
15config VIDEO_PVRUSB2_29XXX 19config VIDEO_PVRUSB2_ONAIR_CREATOR
16 bool "Hauppauge WinTV-PVR USB2 support for 29xxx model series" 20 bool "pvrusb2 driver support for OnAir Creator model"
17 depends on VIDEO_PVRUSB2 && EXPERIMENTAL 21 depends on VIDEO_PVRUSB2 && EXPERIMENTAL
18 select VIDEO_SAA711X 22 select VIDEO_SAA711X
19 select VIDEO_MSP3400 23 select VIDEO_CS53L32A
20 ---help--- 24 ---help---
21 This option enables support for WinTV-PVR USB2 devices whose 25
22 model number is of the form "29xxx" (leading prefix of "29" 26 This option enables support for the OnAir Creator USB tuner
23 followed by 3 digits). 27 device. This is a hybrid device, however currently only
24 To see if you may need this option, examine the white 28 analog mode is supported.
25 sticker on the underside of your device.
26 29
27 If you are in doubt, say Y. 30 If you are in doubt, say Y.
28 31
29config VIDEO_PVRUSB2_24XXX 32config VIDEO_PVRUSB2_ONAIR_USB2
30 bool "Hauppauge WinTV-PVR USB2 support for 24xxx model series" 33 bool "pvrusb2 driver support for OnAir USB2 model"
31 depends on VIDEO_PVRUSB2 && EXPERIMENTAL 34 depends on VIDEO_PVRUSB2 && EXPERIMENTAL
32 select VIDEO_CX25840 35 select VIDEO_SAA711X
33 select VIDEO_WM8775 36 select VIDEO_CS53L32A
34 ---help--- 37 ---help---
35 This option enables inclusion of additional logic to operate 38
36 newer WinTV-PVR USB2 devices whose model number is of the 39 This option enables support for the OnAir USB2 tuner device
37 form "24xxx" (leading prefix of "24" followed by 3 digits). 40 (also known as the Sasem tuner). This is a hybrid device,
38 To see if you may need this option, examine the white 41 however currently only analog mode is supported.
39 sticker on the underside of your device. Enabling this
40 option will not harm support for older devices.
41 42
42 If you are in doubt, say Y. 43 If you are in doubt, say Y.
43 44
diff --git a/drivers/media/video/pvrusb2/Makefile b/drivers/media/video/pvrusb2/Makefile
index 69b3e43cd0eb..47284e558648 100644
--- a/drivers/media/video/pvrusb2/Makefile
+++ b/drivers/media/video/pvrusb2/Makefile
@@ -6,7 +6,7 @@ pvrusb2-objs := pvrusb2-i2c-core.o pvrusb2-i2c-cmd-v4l2.o \
6 pvrusb2-encoder.o pvrusb2-video-v4l.o \ 6 pvrusb2-encoder.o pvrusb2-video-v4l.o \
7 pvrusb2-eeprom.o pvrusb2-tuner.o \ 7 pvrusb2-eeprom.o pvrusb2-tuner.o \
8 pvrusb2-main.o pvrusb2-hdw.o pvrusb2-v4l2.o \ 8 pvrusb2-main.o pvrusb2-hdw.o pvrusb2-v4l2.o \
9 pvrusb2-ctrl.o pvrusb2-std.o \ 9 pvrusb2-ctrl.o pvrusb2-std.o pvrusb2-devattr.o \
10 pvrusb2-context.o pvrusb2-io.o pvrusb2-ioread.o \ 10 pvrusb2-context.o pvrusb2-io.o pvrusb2-ioread.o \
11 pvrusb2-cx2584x-v4l.o pvrusb2-wm8775.o \ 11 pvrusb2-cx2584x-v4l.o pvrusb2-wm8775.o \
12 $(obj-pvrusb2-sysfs-y) $(obj-pvrusb2-debugifc-y) 12 $(obj-pvrusb2-sysfs-y) $(obj-pvrusb2-debugifc-y)
diff --git a/drivers/media/video/pvrusb2/pvrusb2-audio.c b/drivers/media/video/pvrusb2/pvrusb2-audio.c
index 379645e481c6..9a7c8e9c3e8b 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-audio.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-audio.c
@@ -35,34 +35,58 @@ struct pvr2_msp3400_handler {
35}; 35};
36 36
37 37
38
39struct routing_scheme {
40 const int *def;
41 unsigned int cnt;
42};
43
44static const int routing_scheme0[] = {
45 [PVR2_CVAL_INPUT_TV] = MSP_INPUT_DEFAULT,
46 [PVR2_CVAL_INPUT_RADIO] = MSP_INPUT(MSP_IN_SCART2,
47 MSP_IN_TUNER1,
48 MSP_DSP_IN_SCART,
49 MSP_DSP_IN_SCART),
50 [PVR2_CVAL_INPUT_COMPOSITE] = MSP_INPUT(MSP_IN_SCART1,
51 MSP_IN_TUNER1,
52 MSP_DSP_IN_SCART,
53 MSP_DSP_IN_SCART),
54 [PVR2_CVAL_INPUT_SVIDEO] = MSP_INPUT(MSP_IN_SCART1,
55 MSP_IN_TUNER1,
56 MSP_DSP_IN_SCART,
57 MSP_DSP_IN_SCART),
58};
59
60static const struct routing_scheme routing_schemes[] = {
61 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = {
62 .def = routing_scheme0,
63 .cnt = ARRAY_SIZE(routing_scheme0),
64 },
65};
66
38/* This function selects the correct audio input source */ 67/* This function selects the correct audio input source */
39static void set_stereo(struct pvr2_msp3400_handler *ctxt) 68static void set_stereo(struct pvr2_msp3400_handler *ctxt)
40{ 69{
41 struct pvr2_hdw *hdw = ctxt->hdw; 70 struct pvr2_hdw *hdw = ctxt->hdw;
42 struct v4l2_routing route; 71 struct v4l2_routing route;
72 const struct routing_scheme *sp;
73 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
43 74
44 pvr2_trace(PVR2_TRACE_CHIPS,"i2c msp3400 v4l2 set_stereo"); 75 pvr2_trace(PVR2_TRACE_CHIPS,"i2c msp3400 v4l2 set_stereo");
45 76
46 route.input = MSP_INPUT_DEFAULT; 77 if ((sid < ARRAY_SIZE(routing_schemes)) &&
47 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1); 78 ((sp = routing_schemes + sid) != 0) &&
48 switch (hdw->input_val) { 79 (hdw->input_val >= 0) &&
49 case PVR2_CVAL_INPUT_TV: 80 (hdw->input_val < sp->cnt)) {
50 break; 81 route.input = sp->def[hdw->input_val];
51 case PVR2_CVAL_INPUT_RADIO: 82 } else {
52 /* Assume that msp34xx also handle FM decoding, in which case 83 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
53 we're still using the tuner. */ 84 "*** WARNING *** i2c msp3400 v4l2 set_stereo:"
54 /* HV: actually it is more likely to be the SCART2 input if 85 " Invalid routing scheme (%u) and/or input (%d)",
55 the ivtv experience is any indication. */ 86 sid,hdw->input_val);
56 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1, 87 return;
57 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
58 break;
59 case PVR2_CVAL_INPUT_SVIDEO:
60 case PVR2_CVAL_INPUT_COMPOSITE:
61 /* SCART 1 input */
62 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
63 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
64 break;
65 } 88 }
89 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
66 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route); 90 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route);
67} 91}
68 92
diff --git a/drivers/media/video/pvrusb2/pvrusb2-context.c b/drivers/media/video/pvrusb2/pvrusb2-context.c
index 22719ba861ac..9d94aed2e12d 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-context.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-context.c
@@ -31,52 +31,32 @@
31 31
32static void pvr2_context_destroy(struct pvr2_context *mp) 32static void pvr2_context_destroy(struct pvr2_context *mp)
33{ 33{
34 if (mp->hdw) pvr2_hdw_destroy(mp->hdw);
35 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr_main id=%p",mp); 34 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr_main id=%p",mp);
36 if (mp->workqueue) { 35 if (mp->hdw) pvr2_hdw_destroy(mp->hdw);
37 flush_workqueue(mp->workqueue);
38 destroy_workqueue(mp->workqueue);
39 }
40 kfree(mp); 36 kfree(mp);
41} 37}
42 38
43 39
44static void pvr2_context_trigger_poll(struct pvr2_context *mp) 40static void pvr2_context_state_check(struct pvr2_context *mp)
45{
46 queue_work(mp->workqueue,&mp->workpoll);
47}
48
49
50static void pvr2_context_poll(struct work_struct *work)
51{
52 struct pvr2_context *mp =
53 container_of(work, struct pvr2_context, workpoll);
54 pvr2_context_enter(mp); do {
55 pvr2_hdw_poll(mp->hdw);
56 } while (0); pvr2_context_exit(mp);
57}
58
59
60static void pvr2_context_setup(struct work_struct *work)
61{ 41{
62 struct pvr2_context *mp = 42 if (mp->init_flag) return;
63 container_of(work, struct pvr2_context, workinit); 43
44 switch (pvr2_hdw_get_state(mp->hdw)) {
45 case PVR2_STATE_WARM: break;
46 case PVR2_STATE_ERROR: break;
47 case PVR2_STATE_READY: break;
48 case PVR2_STATE_RUN: break;
49 default: return;
50 }
64 51
65 pvr2_context_enter(mp); do { 52 pvr2_context_enter(mp); do {
66 if (!pvr2_hdw_dev_ok(mp->hdw)) break; 53 mp->init_flag = !0;
67 pvr2_hdw_setup(mp->hdw);
68 pvr2_hdw_setup_poll_trigger(
69 mp->hdw,
70 (void (*)(void *))pvr2_context_trigger_poll,
71 mp);
72 if (!pvr2_hdw_dev_ok(mp->hdw)) break;
73 if (!pvr2_hdw_init_ok(mp->hdw)) break;
74 mp->video_stream.stream = pvr2_hdw_get_video_stream(mp->hdw); 54 mp->video_stream.stream = pvr2_hdw_get_video_stream(mp->hdw);
75 if (mp->setup_func) { 55 if (mp->setup_func) {
76 mp->setup_func(mp); 56 mp->setup_func(mp);
77 } 57 }
78 } while (0); pvr2_context_exit(mp); 58 } while (0); pvr2_context_exit(mp);
79} 59 }
80 60
81 61
82struct pvr2_context *pvr2_context_create( 62struct pvr2_context *pvr2_context_create(
@@ -96,11 +76,10 @@ struct pvr2_context *pvr2_context_create(
96 mp = NULL; 76 mp = NULL;
97 goto done; 77 goto done;
98 } 78 }
99 79 pvr2_hdw_set_state_callback(mp->hdw,
100 mp->workqueue = create_singlethread_workqueue("pvrusb2"); 80 (void (*)(void *))pvr2_context_state_check,
101 INIT_WORK(&mp->workinit, pvr2_context_setup); 81 mp);
102 INIT_WORK(&mp->workpoll, pvr2_context_poll); 82 pvr2_context_state_check(mp);
103 queue_work(mp->workqueue,&mp->workinit);
104 done: 83 done:
105 return mp; 84 return mp;
106} 85}
diff --git a/drivers/media/video/pvrusb2/pvrusb2-context.h b/drivers/media/video/pvrusb2/pvrusb2-context.h
index 6327fa1f7e4f..a04187a93225 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-context.h
+++ b/drivers/media/video/pvrusb2/pvrusb2-context.h
@@ -45,14 +45,11 @@ struct pvr2_context {
45 struct pvr2_context_stream video_stream; 45 struct pvr2_context_stream video_stream;
46 struct mutex mutex; 46 struct mutex mutex;
47 int disconnect_flag; 47 int disconnect_flag;
48 int init_flag;
48 49
49 /* Called after pvr2_context initialization is complete */ 50 /* Called after pvr2_context initialization is complete */
50 void (*setup_func)(struct pvr2_context *); 51 void (*setup_func)(struct pvr2_context *);
51 52
52 /* Work queue overhead for out-of-line processing */
53 struct workqueue_struct *workqueue;
54 struct work_struct workinit;
55 struct work_struct workpoll;
56}; 53};
57 54
58struct pvr2_channel { 55struct pvr2_channel {
diff --git a/drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.c b/drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.c
index e8a9252c7df6..ffdc45c324e5 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.c
@@ -49,34 +49,89 @@ struct pvr2_v4l_cx2584x {
49}; 49};
50 50
51 51
52struct routing_scheme_item {
53 int vid;
54 int aud;
55};
56
57struct routing_scheme {
58 const struct routing_scheme_item *def;
59 unsigned int cnt;
60};
61
62static const struct routing_scheme_item routing_scheme0[] = {
63 [PVR2_CVAL_INPUT_TV] = {
64 .vid = CX25840_COMPOSITE7,
65 .aud = CX25840_AUDIO8,
66 },
67 [PVR2_CVAL_INPUT_RADIO] = { /* Treat the same as composite */
68 .vid = CX25840_COMPOSITE3,
69 .aud = CX25840_AUDIO_SERIAL,
70 },
71 [PVR2_CVAL_INPUT_COMPOSITE] = {
72 .vid = CX25840_COMPOSITE3,
73 .aud = CX25840_AUDIO_SERIAL,
74 },
75 [PVR2_CVAL_INPUT_SVIDEO] = {
76 .vid = CX25840_SVIDEO1,
77 .aud = CX25840_AUDIO_SERIAL,
78 },
79};
80
81/* Specific to gotview device */
82static const struct routing_scheme_item routing_schemegv[] = {
83 [PVR2_CVAL_INPUT_TV] = {
84 .vid = CX25840_COMPOSITE2,
85 .aud = CX25840_AUDIO5,
86 },
87 [PVR2_CVAL_INPUT_RADIO] = { /* Treat the same as composite */
88 .vid = CX25840_COMPOSITE1,
89 .aud = CX25840_AUDIO_SERIAL,
90 },
91 [PVR2_CVAL_INPUT_COMPOSITE] = {
92 .vid = CX25840_COMPOSITE1,
93 .aud = CX25840_AUDIO_SERIAL,
94 },
95 [PVR2_CVAL_INPUT_SVIDEO] = {
96 .vid = (CX25840_SVIDEO_LUMA3|CX25840_SVIDEO_CHROMA4),
97 .aud = CX25840_AUDIO_SERIAL,
98 },
99};
100
101static const struct routing_scheme routing_schemes[] = {
102 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = {
103 .def = routing_scheme0,
104 .cnt = ARRAY_SIZE(routing_scheme0),
105 },
106 [PVR2_ROUTING_SCHEME_GOTVIEW] = {
107 .def = routing_schemegv,
108 .cnt = ARRAY_SIZE(routing_schemegv),
109 },
110};
111
52static void set_input(struct pvr2_v4l_cx2584x *ctxt) 112static void set_input(struct pvr2_v4l_cx2584x *ctxt)
53{ 113{
54 struct pvr2_hdw *hdw = ctxt->hdw; 114 struct pvr2_hdw *hdw = ctxt->hdw;
55 struct v4l2_routing route; 115 struct v4l2_routing route;
56 enum cx25840_video_input vid_input; 116 enum cx25840_video_input vid_input;
57 enum cx25840_audio_input aud_input; 117 enum cx25840_audio_input aud_input;
118 const struct routing_scheme *sp;
119 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
58 120
59 memset(&route,0,sizeof(route)); 121 memset(&route,0,sizeof(route));
60 122
61 switch(hdw->input_val) { 123 if ((sid < ARRAY_SIZE(routing_schemes)) &&
62 case PVR2_CVAL_INPUT_TV: 124 ((sp = routing_schemes + sid) != 0) &&
63 vid_input = CX25840_COMPOSITE7; 125 (hdw->input_val >= 0) &&
64 aud_input = CX25840_AUDIO8; 126 (hdw->input_val < sp->cnt)) {
65 break; 127 vid_input = sp->def[hdw->input_val].vid;
66 case PVR2_CVAL_INPUT_RADIO: // Treat same as composite 128 aud_input = sp->def[hdw->input_val].aud;
67 case PVR2_CVAL_INPUT_COMPOSITE: 129 } else {
68 vid_input = CX25840_COMPOSITE3; 130 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
69 aud_input = CX25840_AUDIO_SERIAL; 131 "*** WARNING *** i2c cx2584x set_input:"
70 break; 132 " Invalid routing scheme (%u) and/or input (%d)",
71 case PVR2_CVAL_INPUT_SVIDEO: 133 sid,hdw->input_val);
72 vid_input = CX25840_SVIDEO1; 134 return;
73 aud_input = CX25840_AUDIO_SERIAL;
74 break;
75 default:
76 // Just set it to be composite input for now...
77 vid_input = CX25840_COMPOSITE3;
78 aud_input = CX25840_AUDIO_SERIAL;
79 break;
80 } 135 }
81 136
82 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_input vid=0x%x aud=0x%x", 137 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_input vid=0x%x aud=0x%x",
@@ -140,7 +195,7 @@ static const struct pvr2_v4l_cx2584x_ops decoder_ops[] = {
140static void decoder_detach(struct pvr2_v4l_cx2584x *ctxt) 195static void decoder_detach(struct pvr2_v4l_cx2584x *ctxt)
141{ 196{
142 ctxt->client->handler = NULL; 197 ctxt->client->handler = NULL;
143 ctxt->hdw->decoder_ctrl = NULL; 198 pvr2_hdw_set_decoder(ctxt->hdw,NULL);
144 kfree(ctxt); 199 kfree(ctxt);
145} 200}
146 201
@@ -241,7 +296,7 @@ int pvr2_i2c_cx2584x_v4l_setup(struct pvr2_hdw *hdw,
241 ctxt->client = cp; 296 ctxt->client = cp;
242 ctxt->hdw = hdw; 297 ctxt->hdw = hdw;
243 ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1; 298 ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1;
244 hdw->decoder_ctrl = &ctxt->ctrl; 299 pvr2_hdw_set_decoder(hdw,&ctxt->ctrl);
245 cp->handler = &ctxt->handler; 300 cp->handler = &ctxt->handler;
246 { 301 {
247 /* 302 /*
diff --git a/drivers/media/video/pvrusb2/pvrusb2-debug.h b/drivers/media/video/pvrusb2/pvrusb2-debug.h
index da6441b88f31..fca49d8a9311 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-debug.h
+++ b/drivers/media/video/pvrusb2/pvrusb2-debug.h
@@ -34,25 +34,26 @@ extern int pvrusb2_debug;
34#define PVR2_TRACE_INIT (1 << 5) /* misc initialization steps */ 34#define PVR2_TRACE_INIT (1 << 5) /* misc initialization steps */
35#define PVR2_TRACE_START_STOP (1 << 6) /* Streaming start / stop */ 35#define PVR2_TRACE_START_STOP (1 << 6) /* Streaming start / stop */
36#define PVR2_TRACE_CTL (1 << 7) /* commit of control changes */ 36#define PVR2_TRACE_CTL (1 << 7) /* commit of control changes */
37#define PVR2_TRACE_DEBUG (1 << 8) /* Temporary debug code */ 37#define PVR2_TRACE_STATE (1 << 8) /* Device state changes */
38#define PVR2_TRACE_EEPROM (1 << 9) /* eeprom parsing / report */ 38#define PVR2_TRACE_STBITS (1 << 9) /* Individual bit state changes */
39#define PVR2_TRACE_STRUCT (1 << 10) /* internal struct creation */ 39#define PVR2_TRACE_EEPROM (1 << 10) /* eeprom parsing / report */
40#define PVR2_TRACE_OPEN_CLOSE (1 << 11) /* application open / close */ 40#define PVR2_TRACE_STRUCT (1 << 11) /* internal struct creation */
41#define PVR2_TRACE_CREG (1 << 12) /* Main critical region entry / exit */ 41#define PVR2_TRACE_OPEN_CLOSE (1 << 12) /* application open / close */
42#define PVR2_TRACE_SYSFS (1 << 13) /* Sysfs driven I/O */ 42#define PVR2_TRACE_CREG (1 << 13) /* Main critical region entry / exit */
43#define PVR2_TRACE_FIRMWARE (1 << 14) /* firmware upload actions */ 43#define PVR2_TRACE_SYSFS (1 << 14) /* Sysfs driven I/O */
44#define PVR2_TRACE_CHIPS (1 << 15) /* chip broadcast operation */ 44#define PVR2_TRACE_FIRMWARE (1 << 15) /* firmware upload actions */
45#define PVR2_TRACE_I2C (1 << 16) /* I2C related stuff */ 45#define PVR2_TRACE_CHIPS (1 << 16) /* chip broadcast operation */
46#define PVR2_TRACE_I2C_CMD (1 << 17) /* Software commands to I2C modules */ 46#define PVR2_TRACE_I2C (1 << 17) /* I2C related stuff */
47#define PVR2_TRACE_I2C_CORE (1 << 18) /* I2C core debugging */ 47#define PVR2_TRACE_I2C_CMD (1 << 18) /* Software commands to I2C modules */
48#define PVR2_TRACE_I2C_TRAF (1 << 19) /* I2C traffic through the adapter */ 48#define PVR2_TRACE_I2C_CORE (1 << 19) /* I2C core debugging */
49#define PVR2_TRACE_V4LIOCTL (1 << 20) /* v4l ioctl details */ 49#define PVR2_TRACE_I2C_TRAF (1 << 20) /* I2C traffic through the adapter */
50#define PVR2_TRACE_ENCODER (1 << 21) /* mpeg2 encoder operation */ 50#define PVR2_TRACE_V4LIOCTL (1 << 21) /* v4l ioctl details */
51#define PVR2_TRACE_BUF_POOL (1 << 22) /* Track buffer pool management */ 51#define PVR2_TRACE_ENCODER (1 << 22) /* mpeg2 encoder operation */
52#define PVR2_TRACE_BUF_FLOW (1 << 23) /* Track buffer flow in system */ 52#define PVR2_TRACE_BUF_POOL (1 << 23) /* Track buffer pool management */
53#define PVR2_TRACE_DATA_FLOW (1 << 24) /* Track data flow */ 53#define PVR2_TRACE_BUF_FLOW (1 << 24) /* Track buffer flow in system */
54#define PVR2_TRACE_DEBUGIFC (1 << 25) /* Debug interface actions */ 54#define PVR2_TRACE_DATA_FLOW (1 << 25) /* Track data flow */
55#define PVR2_TRACE_GPIO (1 << 26) /* GPIO state bit changes */ 55#define PVR2_TRACE_DEBUGIFC (1 << 26) /* Debug interface actions */
56#define PVR2_TRACE_GPIO (1 << 27) /* GPIO state bit changes */
56 57
57 58
58#endif /* __PVRUSB2_HDW_INTERNAL_H */ 59#endif /* __PVRUSB2_HDW_INTERNAL_H */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-debugifc.c b/drivers/media/video/pvrusb2/pvrusb2-debugifc.c
index 6f135f4a2497..b0687430fdd4 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-debugifc.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-debugifc.c
@@ -31,14 +31,6 @@ struct debugifc_mask_item {
31 unsigned long msk; 31 unsigned long msk;
32}; 32};
33 33
34static struct debugifc_mask_item mask_items[] = {
35 {"ENC_FIRMWARE",(1<<PVR2_SUBSYS_B_ENC_FIRMWARE)},
36 {"ENC_CFG",(1<<PVR2_SUBSYS_B_ENC_CFG)},
37 {"DIG_RUN",(1<<PVR2_SUBSYS_B_DIGITIZER_RUN)},
38 {"USB_RUN",(1<<PVR2_SUBSYS_B_USBSTREAM_RUN)},
39 {"ENC_RUN",(1<<PVR2_SUBSYS_B_ENC_RUN)},
40};
41
42 34
43static unsigned int debugifc_count_whitespace(const char *buf, 35static unsigned int debugifc_count_whitespace(const char *buf,
44 unsigned int count) 36 unsigned int count)
@@ -148,134 +140,14 @@ static int debugifc_match_keyword(const char *buf,unsigned int count,
148} 140}
149 141
150 142
151static unsigned long debugifc_find_mask(const char *buf,unsigned int count)
152{
153 struct debugifc_mask_item *mip;
154 unsigned int idx;
155 for (idx = 0; idx < ARRAY_SIZE(mask_items); idx++) {
156 mip = mask_items + idx;
157 if (debugifc_match_keyword(buf,count,mip->name)) {
158 return mip->msk;
159 }
160 }
161 return 0;
162}
163
164
165static int debugifc_print_mask(char *buf,unsigned int sz,
166 unsigned long msk,unsigned long val)
167{
168 struct debugifc_mask_item *mip;
169 unsigned int idx;
170 int bcnt = 0;
171 int ccnt;
172 for (idx = 0; idx < ARRAY_SIZE(mask_items); idx++) {
173 mip = mask_items + idx;
174 if (!(mip->msk & msk)) continue;
175 ccnt = scnprintf(buf,sz,"%s%c%s",
176 (bcnt ? " " : ""),
177 ((mip->msk & val) ? '+' : '-'),
178 mip->name);
179 sz -= ccnt;
180 buf += ccnt;
181 bcnt += ccnt;
182 }
183 return bcnt;
184}
185
186static unsigned int debugifc_parse_subsys_mask(const char *buf,
187 unsigned int count,
188 unsigned long *mskPtr,
189 unsigned long *valPtr)
190{
191 const char *wptr;
192 unsigned int consume_cnt = 0;
193 unsigned int scnt;
194 unsigned int wlen;
195 int mode;
196 unsigned long m1,msk,val;
197
198 msk = 0;
199 val = 0;
200
201 while (count) {
202 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
203 if (!scnt) break;
204 consume_cnt += scnt; count -= scnt; buf += scnt;
205 if (!wptr) break;
206
207 mode = 0;
208 if (wlen) switch (wptr[0]) {
209 case '+':
210 wptr++;
211 wlen--;
212 break;
213 case '-':
214 mode = 1;
215 wptr++;
216 wlen--;
217 break;
218 }
219 if (!wlen) continue;
220 m1 = debugifc_find_mask(wptr,wlen);
221 if (!m1) break;
222 msk |= m1;
223 if (!mode) val |= m1;
224 }
225 *mskPtr = msk;
226 *valPtr = val;
227 return consume_cnt;
228}
229
230
231int pvr2_debugifc_print_info(struct pvr2_hdw *hdw,char *buf,unsigned int acnt) 143int pvr2_debugifc_print_info(struct pvr2_hdw *hdw,char *buf,unsigned int acnt)
232{ 144{
233 int bcnt = 0; 145 int bcnt = 0;
234 int ccnt; 146 int ccnt;
235 struct pvr2_hdw_debug_info dbg; 147 ccnt = scnprintf(buf,acnt,"Driver state info:\n");
236
237 pvr2_hdw_get_debug_info(hdw,&dbg);
238
239 ccnt = scnprintf(buf,acnt,"big lock %s; ctl lock %s",
240 (dbg.big_lock_held ? "held" : "free"),
241 (dbg.ctl_lock_held ? "held" : "free"));
242 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
243 if (dbg.ctl_lock_held) {
244 ccnt = scnprintf(buf,acnt,"; cmd_state=%d cmd_code=%d"
245 " cmd_wlen=%d cmd_rlen=%d"
246 " wpend=%d rpend=%d tmout=%d rstatus=%d"
247 " wstatus=%d",
248 dbg.cmd_debug_state,dbg.cmd_code,
249 dbg.cmd_debug_write_len,
250 dbg.cmd_debug_read_len,
251 dbg.cmd_debug_write_pend,
252 dbg.cmd_debug_read_pend,
253 dbg.cmd_debug_timeout,
254 dbg.cmd_debug_rstatus,
255 dbg.cmd_debug_wstatus);
256 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
257 }
258 ccnt = scnprintf(buf,acnt,"\n");
259 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
260 ccnt = scnprintf(
261 buf,acnt,"driver flags: %s %s %s\n",
262 (dbg.flag_init_ok ? "initialized" : "uninitialized"),
263 (dbg.flag_ok ? "ok" : "fail"),
264 (dbg.flag_disconnected ? "disconnected" : "connected"));
265 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
266 ccnt = scnprintf(buf,acnt,"Subsystems enabled / configured: ");
267 bcnt += ccnt; acnt -= ccnt; buf += ccnt; 148 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
268 ccnt = debugifc_print_mask(buf,acnt,dbg.subsys_flags,dbg.subsys_flags); 149 ccnt = pvr2_hdw_state_report(hdw,buf,acnt);
269 bcnt += ccnt; acnt -= ccnt; buf += ccnt; 150 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
270 ccnt = scnprintf(buf,acnt,"\n");
271 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
272 ccnt = scnprintf(buf,acnt,"Subsystems disabled / unconfigured: ");
273 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
274 ccnt = debugifc_print_mask(buf,acnt,~dbg.subsys_flags,dbg.subsys_flags);
275 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
276 ccnt = scnprintf(buf,acnt,"\n");
277 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
278
279 ccnt = scnprintf(buf,acnt,"Attached I2C modules:\n"); 151 ccnt = scnprintf(buf,acnt,"Attached I2C modules:\n");
280 bcnt += ccnt; acnt -= ccnt; buf += ccnt; 152 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
281 ccnt = pvr2_i2c_report(hdw,buf,acnt); 153 ccnt = pvr2_i2c_report(hdw,buf,acnt);
@@ -290,7 +162,6 @@ int pvr2_debugifc_print_status(struct pvr2_hdw *hdw,
290{ 162{
291 int bcnt = 0; 163 int bcnt = 0;
292 int ccnt; 164 int ccnt;
293 unsigned long msk;
294 int ret; 165 int ret;
295 u32 gpio_dir,gpio_in,gpio_out; 166 u32 gpio_dir,gpio_in,gpio_out;
296 167
@@ -311,28 +182,6 @@ int pvr2_debugifc_print_status(struct pvr2_hdw *hdw,
311 pvr2_hdw_get_streaming(hdw) ? "on" : "off"); 182 pvr2_hdw_get_streaming(hdw) ? "on" : "off");
312 bcnt += ccnt; acnt -= ccnt; buf += ccnt; 183 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
313 184
314 msk = pvr2_hdw_subsys_get(hdw);
315 ccnt = scnprintf(buf,acnt,"Subsystems enabled / configured: ");
316 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
317 ccnt = debugifc_print_mask(buf,acnt,msk,msk);
318 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
319 ccnt = scnprintf(buf,acnt,"\n");
320 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
321 ccnt = scnprintf(buf,acnt,"Subsystems disabled / unconfigured: ");
322 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
323 ccnt = debugifc_print_mask(buf,acnt,~msk,msk);
324 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
325 ccnt = scnprintf(buf,acnt,"\n");
326 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
327
328 msk = pvr2_hdw_subsys_stream_get(hdw);
329 ccnt = scnprintf(buf,acnt,"Subsystems stopped on stream shutdown: ");
330 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
331 ccnt = debugifc_print_mask(buf,acnt,msk,msk);
332 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
333 ccnt = scnprintf(buf,acnt,"\n");
334 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
335
336 return bcnt; 185 return bcnt;
337} 186}
338 187
@@ -369,28 +218,10 @@ static int pvr2_debugifc_do1cmd(struct pvr2_hdw *hdw,const char *buf,
369 return pvr2_upload_firmware2(hdw); 218 return pvr2_upload_firmware2(hdw);
370 } else if (debugifc_match_keyword(wptr,wlen,"decoder")) { 219 } else if (debugifc_match_keyword(wptr,wlen,"decoder")) {
371 return pvr2_hdw_cmd_decoder_reset(hdw); 220 return pvr2_hdw_cmd_decoder_reset(hdw);
221 } else if (debugifc_match_keyword(wptr,wlen,"worker")) {
222 return pvr2_hdw_untrip(hdw);
372 } 223 }
373 return -EINVAL; 224 return -EINVAL;
374 } else if (debugifc_match_keyword(wptr,wlen,"subsys_flags")) {
375 unsigned long msk = 0;
376 unsigned long val = 0;
377 if (debugifc_parse_subsys_mask(buf,count,&msk,&val) != count) {
378 pvr2_trace(PVR2_TRACE_DEBUGIFC,
379 "debugifc parse error on subsys mask");
380 return -EINVAL;
381 }
382 pvr2_hdw_subsys_bit_chg(hdw,msk,val);
383 return 0;
384 } else if (debugifc_match_keyword(wptr,wlen,"stream_flags")) {
385 unsigned long msk = 0;
386 unsigned long val = 0;
387 if (debugifc_parse_subsys_mask(buf,count,&msk,&val) != count) {
388 pvr2_trace(PVR2_TRACE_DEBUGIFC,
389 "debugifc parse error on stream mask");
390 return -EINVAL;
391 }
392 pvr2_hdw_subsys_stream_bit_chg(hdw,msk,val);
393 return 0;
394 } else if (debugifc_match_keyword(wptr,wlen,"cpufw")) { 225 } else if (debugifc_match_keyword(wptr,wlen,"cpufw")) {
395 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen); 226 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
396 if (!scnt) return -EINVAL; 227 if (!scnt) return -EINVAL;
diff --git a/drivers/media/video/pvrusb2/pvrusb2-devattr.c b/drivers/media/video/pvrusb2/pvrusb2-devattr.c
new file mode 100644
index 000000000000..4df6d6d936fc
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-devattr.c
@@ -0,0 +1,217 @@
1/*
2 *
3 * $Id$
4 *
5 * Copyright (C) 2007 Mike Isely <isely@pobox.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
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/*
23
24This source file should encompass ALL per-device type information for the
25driver. To define a new device, add elements to the pvr2_device_table and
26pvr2_device_desc structures.
27
28*/
29
30#include "pvrusb2-devattr.h"
31#include <linux/usb.h>
32/* This is needed in order to pull in tuner type ids... */
33#include <linux/i2c.h>
34#include <media/tuner.h>
35
36
37
38/*------------------------------------------------------------------------*/
39/* Hauppauge PVR-USB2 Model 29xxx */
40
41static const char *pvr2_client_29xxx[] = {
42 "msp3400",
43 "saa7115",
44 "tuner",
45};
46
47static const char *pvr2_fw1_names_29xxx[] = {
48 "v4l-pvrusb2-29xxx-01.fw",
49};
50
51static const struct pvr2_device_desc pvr2_device_29xxx = {
52 .description = "WinTV PVR USB2 Model Category 29xxxx",
53 .shortname = "29xxx",
54 .client_modules.lst = pvr2_client_29xxx,
55 .client_modules.cnt = ARRAY_SIZE(pvr2_client_29xxx),
56 .fx2_firmware.lst = pvr2_fw1_names_29xxx,
57 .fx2_firmware.cnt = ARRAY_SIZE(pvr2_fw1_names_29xxx),
58 .flag_has_hauppauge_rom = !0,
59 .signal_routing_scheme = PVR2_ROUTING_SCHEME_HAUPPAUGE,
60};
61
62
63
64/*------------------------------------------------------------------------*/
65/* Hauppauge PVR-USB2 Model 24xxx */
66
67static const char *pvr2_client_24xxx[] = {
68 "cx25840",
69 "tuner",
70 "wm8775",
71};
72
73static const char *pvr2_fw1_names_24xxx[] = {
74 "v4l-pvrusb2-24xxx-01.fw",
75};
76
77static const struct pvr2_device_desc pvr2_device_24xxx = {
78 .description = "WinTV PVR USB2 Model Category 24xxxx",
79 .shortname = "24xxx",
80 .client_modules.lst = pvr2_client_24xxx,
81 .client_modules.cnt = ARRAY_SIZE(pvr2_client_24xxx),
82 .fx2_firmware.lst = pvr2_fw1_names_24xxx,
83 .fx2_firmware.cnt = ARRAY_SIZE(pvr2_fw1_names_24xxx),
84 .flag_has_cx25840 = !0,
85 .flag_has_wm8775 = !0,
86 .flag_has_hauppauge_rom = !0,
87 .flag_has_hauppauge_custom_ir = !0,
88 .signal_routing_scheme = PVR2_ROUTING_SCHEME_HAUPPAUGE,
89};
90
91
92
93/*------------------------------------------------------------------------*/
94/* GOTVIEW USB2.0 DVD2 */
95
96static const char *pvr2_client_gotview_2[] = {
97 "cx25840",
98 "tuner",
99};
100
101static const struct pvr2_device_desc pvr2_device_gotview_2 = {
102 .description = "Gotview USB 2.0 DVD 2",
103 .shortname = "gv2",
104 .client_modules.lst = pvr2_client_gotview_2,
105 .client_modules.cnt = ARRAY_SIZE(pvr2_client_gotview_2),
106 .flag_has_cx25840 = !0,
107 .default_tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
108 .signal_routing_scheme = PVR2_ROUTING_SCHEME_GOTVIEW,
109};
110
111
112
113#ifdef CONFIG_VIDEO_PVRUSB2_ONAIR_CREATOR
114/*------------------------------------------------------------------------*/
115/* OnAir Creator */
116
117static const char *pvr2_client_onair_creator[] = {
118 "saa7115",
119 "tuner",
120 "cs53l32a",
121};
122
123static const struct pvr2_device_desc pvr2_device_onair_creator = {
124 .description = "OnAir Creator Hybrid USB tuner",
125 .shortname = "oac",
126 .client_modules.lst = pvr2_client_onair_creator,
127 .client_modules.cnt = ARRAY_SIZE(pvr2_client_onair_creator),
128 .default_tuner_type = TUNER_LG_TDVS_H06XF,
129 .signal_routing_scheme = PVR2_ROUTING_SCHEME_HAUPPAUGE,
130};
131#endif
132
133
134
135#ifdef CONFIG_VIDEO_PVRUSB2_ONAIR_USB2
136/*------------------------------------------------------------------------*/
137/* OnAir USB 2.0 */
138
139static const char *pvr2_client_onair_usb2[] = {
140 "saa7115",
141 "tuner",
142 "cs53l32a",
143};
144
145static const struct pvr2_device_desc pvr2_device_onair_usb2 = {
146 .description = "OnAir USB2 Hybrid USB tuner",
147 .shortname = "oa2",
148 .client_modules.lst = pvr2_client_onair_usb2,
149 .client_modules.cnt = ARRAY_SIZE(pvr2_client_onair_usb2),
150 .default_tuner_type = TUNER_PHILIPS_ATSC,
151 .signal_routing_scheme = PVR2_ROUTING_SCHEME_HAUPPAUGE,
152};
153#endif
154
155
156
157/*------------------------------------------------------------------------*/
158/* Hauppauge PVR-USB2 Model 75xxx */
159
160static const char *pvr2_client_75xxx[] = {
161 "cx25840",
162 "tuner",
163};
164
165static const char *pvr2_fw1_names_75xxx[] = {
166 "v4l-pvrusb2-73xxx-01.fw",
167};
168
169static const struct pvr2_device_desc pvr2_device_75xxx = {
170 .description = "WinTV PVR USB2 Model Category 75xxxx",
171 .shortname = "75xxx",
172 .client_modules.lst = pvr2_client_75xxx,
173 .client_modules.cnt = ARRAY_SIZE(pvr2_client_75xxx),
174 .fx2_firmware.lst = pvr2_fw1_names_75xxx,
175 .fx2_firmware.cnt = ARRAY_SIZE(pvr2_fw1_names_75xxx),
176 .flag_has_cx25840 = !0,
177 .flag_has_hauppauge_rom = !0,
178 .signal_routing_scheme = PVR2_ROUTING_SCHEME_HAUPPAUGE,
179 .default_std_mask = V4L2_STD_NTSC_M,
180};
181
182
183
184/*------------------------------------------------------------------------*/
185
186struct usb_device_id pvr2_device_table[] = {
187 { USB_DEVICE(0x2040, 0x2900),
188 .driver_info = (kernel_ulong_t)&pvr2_device_29xxx},
189 { USB_DEVICE(0x2040, 0x2400),
190 .driver_info = (kernel_ulong_t)&pvr2_device_24xxx},
191 { USB_DEVICE(0x1164, 0x0622),
192 .driver_info = (kernel_ulong_t)&pvr2_device_gotview_2},
193#ifdef CONFIG_VIDEO_PVRUSB2_ONAIR_CREATOR
194 { USB_DEVICE(0x11ba, 0x1003),
195 .driver_info = (kernel_ulong_t)&pvr2_device_onair_creator},
196#endif
197#ifdef CONFIG_VIDEO_PVRUSB2_ONAIR_USB2
198 { USB_DEVICE(0x11ba, 0x1001),
199 .driver_info = (kernel_ulong_t)&pvr2_device_onair_usb2},
200#endif
201 { USB_DEVICE(0x2040, 0x7500),
202 .driver_info = (kernel_ulong_t)&pvr2_device_75xxx},
203 { }
204};
205
206MODULE_DEVICE_TABLE(usb, pvr2_device_table);
207
208
209/*
210 Stuff for Emacs to see, in order to encourage consistent editing style:
211 *** Local Variables: ***
212 *** mode: c ***
213 *** fill-column: 75 ***
214 *** tab-width: 8 ***
215 *** c-basic-offset: 8 ***
216 *** End: ***
217 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-devattr.h b/drivers/media/video/pvrusb2/pvrusb2-devattr.h
new file mode 100644
index 000000000000..64b467f0637f
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-devattr.h
@@ -0,0 +1,119 @@
1/*
2 *
3 * $Id$
4 *
5 * Copyright (C) 2005 Mike Isely <isely@pobox.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
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 __PVRUSB2_DEVATTR_H
22#define __PVRUSB2_DEVATTR_H
23
24#include <linux/mod_devicetable.h>
25#include <linux/videodev2.h>
26
27/*
28
29 This header defines structures used to describe attributes of a device.
30
31*/
32
33
34struct pvr2_string_table {
35 const char **lst;
36 unsigned int cnt;
37};
38
39#define PVR2_ROUTING_SCHEME_HAUPPAUGE 0
40#define PVR2_ROUTING_SCHEME_GOTVIEW 1
41
42/* This describes a particular hardware type (except for the USB device ID
43 which must live in a separate structure due to environmental
44 constraints). See the top of pvrusb2-hdw.c for where this is
45 instantiated. */
46struct pvr2_device_desc {
47 /* Single line text description of hardware */
48 const char *description;
49
50 /* Single token identifier for hardware */
51 const char *shortname;
52
53 /* List of additional client modules we need to load */
54 struct pvr2_string_table client_modules;
55
56 /* List of FX2 firmware file names we should search; if empty then
57 FX2 firmware check / load is skipped and we assume the device
58 was initialized from internal ROM. */
59 struct pvr2_string_table fx2_firmware;
60
61 /* Signal routing scheme used by device, contains one of
62 PVR2_ROUTING_SCHEME_XXX. Schemes have to be defined as we
63 encounter them. This is an arbitrary integer scheme id; its
64 meaning is contained entirely within the driver and is
65 interpreted by logic which must send commands to the chip-level
66 drivers (search for things which touch this field). */
67 unsigned int signal_routing_scheme;
68
69 /* V4L tuner type ID to use with this device (only used if the
70 driver could not discover the type any other way). */
71 int default_tuner_type;
72
73 /* Initial standard bits to use for this device, if not zero.
74 Anything set here is also implied as an available standard.
75 Note: This is ignored if overridden on the module load line via
76 the video_std module option. */
77 v4l2_std_id default_std_mask;
78
79 /* If set, we don't bother trying to load cx23416 firmware. */
80 char flag_skip_cx23416_firmware;
81
82 /* Device has a hauppauge eeprom which we can interrogate. */
83 char flag_has_hauppauge_rom;
84
85 /* Device does not require a powerup command to be issued. */
86 char flag_no_powerup;
87
88 /* Device has a cx25840 - this enables special additional logic to
89 handle it. */
90 char flag_has_cx25840;
91
92 /* Device has a wm8775 - this enables special additional logic to
93 ensure that it is found. */
94 char flag_has_wm8775;
95
96 /* Device has IR hardware that can be faked into looking like a
97 normal Hauppauge i2c IR receiver. This is currently very
98 specific to the 24xxx device, where Hauppauge had replaced their
99 'standard' I2C IR receiver with a bunch of FPGA logic controlled
100 directly via the FX2. Turning this on tells the pvrusb2 driver
101 to virtualize the presence of the non-existant IR receiver chip and
102 implement the virtual receiver in terms of appropriate FX2
103 commands. */
104 char flag_has_hauppauge_custom_ir;
105};
106
107extern struct usb_device_id pvr2_device_table[];
108
109#endif /* __PVRUSB2_HDW_INTERNAL_H */
110
111/*
112 Stuff for Emacs to see, in order to encourage consistent editing style:
113 *** Local Variables: ***
114 *** mode: c ***
115 *** fill-column: 75 ***
116 *** tab-width: 8 ***
117 *** c-basic-offset: 8 ***
118 *** End: ***
119 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-eeprom.c b/drivers/media/video/pvrusb2/pvrusb2-eeprom.c
index 45cbca0143ca..5ef005947b04 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-eeprom.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-eeprom.c
@@ -144,6 +144,7 @@ int pvr2_eeprom_analyze(struct pvr2_hdw *hdw)
144 trace_eeprom("serial_number=%d",tvdata.serial_number); 144 trace_eeprom("serial_number=%d",tvdata.serial_number);
145 trace_eeprom("rev_str=%s",tvdata.rev_str); 145 trace_eeprom("rev_str=%s",tvdata.rev_str);
146 hdw->tuner_type = tvdata.tuner_type; 146 hdw->tuner_type = tvdata.tuner_type;
147 hdw->tuner_updated = !0;
147 hdw->serial_number = tvdata.serial_number; 148 hdw->serial_number = tvdata.serial_number;
148 hdw->std_mask_eeprom = tvdata.tuner_formats; 149 hdw->std_mask_eeprom = tvdata.tuner_formats;
149 150
diff --git a/drivers/media/video/pvrusb2/pvrusb2-encoder.c b/drivers/media/video/pvrusb2/pvrusb2-encoder.c
index 205087a3e136..64062879981e 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-encoder.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-encoder.c
@@ -140,7 +140,7 @@ static int pvr2_encoder_read_words(struct pvr2_hdw *hdw,
140 cx2341x.ko to write to our encoder (by handing it a pointer to this 140 cx2341x.ko to write to our encoder (by handing it a pointer to this
141 function). For earlier kernels this doesn't really matter. */ 141 function). For earlier kernels this doesn't really matter. */
142static int pvr2_encoder_cmd(void *ctxt, 142static int pvr2_encoder_cmd(void *ctxt,
143 int cmd, 143 u32 cmd,
144 int arg_cnt_send, 144 int arg_cnt_send,
145 int arg_cnt_recv, 145 int arg_cnt_recv,
146 u32 *argp) 146 u32 *argp)
@@ -209,7 +209,7 @@ static int pvr2_encoder_cmd(void *ctxt,
209 209
210 LOCK_TAKE(hdw->ctl_lock); do { 210 LOCK_TAKE(hdw->ctl_lock); do {
211 211
212 if (!hdw->flag_encoder_ok) { 212 if (!hdw->state_encoder_ok) {
213 ret = -EIO; 213 ret = -EIO;
214 break; 214 break;
215 } 215 }
@@ -278,12 +278,15 @@ static int pvr2_encoder_cmd(void *ctxt,
278 ret = -EBUSY; 278 ret = -EBUSY;
279 } 279 }
280 if (ret) { 280 if (ret) {
281 hdw->flag_encoder_ok = 0; 281 hdw->state_encoder_ok = 0;
282 pvr2_trace(PVR2_TRACE_STBITS,
283 "State bit %s <-- %s",
284 "state_encoder_ok",
285 (hdw->state_encoder_ok ? "true" : "false"));
282 pvr2_trace( 286 pvr2_trace(
283 PVR2_TRACE_ERROR_LEGS, 287 PVR2_TRACE_ERROR_LEGS,
284 "Giving up on command." 288 "Giving up on command."
285 " It is likely that" 289 " This is normally recovered by the driver.");
286 " this is a bad idea...");
287 break; 290 break;
288 } 291 }
289 wrData[0] = 0x7; 292 wrData[0] = 0x7;
@@ -366,13 +369,13 @@ static int pvr2_encoder_prep_config(struct pvr2_hdw *hdw)
366 369
367 /* This ENC_MISC(3,encMisc3Arg) command is critical - without 370 /* This ENC_MISC(3,encMisc3Arg) command is critical - without
368 it there will eventually be video corruption. Also, the 371 it there will eventually be video corruption. Also, the
369 29xxx case is strange - the Windows driver is passing 1 372 saa7115 case is strange - the Windows driver is passing 1
370 regardless of device type but if we have 1 for 29xxx device 373 regardless of device type but if we have 1 for saa7115
371 the video turns sluggish. */ 374 devices the video turns sluggish. */
372 switch (hdw->hdw_type) { 375 if (hdw->hdw_desc->flag_has_cx25840) {
373 case PVR2_HDW_TYPE_24XXX: encMisc3Arg = 1; break; 376 encMisc3Arg = 1;
374 case PVR2_HDW_TYPE_29XXX: encMisc3Arg = 0; break; 377 } else {
375 default: break; 378 encMisc3Arg = 0;
376 } 379 }
377 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 3, 380 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 3,
378 encMisc3Arg,0,0); 381 encMisc3Arg,0,0);
@@ -394,6 +397,24 @@ static int pvr2_encoder_prep_config(struct pvr2_hdw *hdw)
394 return ret; 397 return ret;
395} 398}
396 399
400int pvr2_encoder_adjust(struct pvr2_hdw *hdw)
401{
402 int ret;
403 ret = cx2341x_update(hdw,pvr2_encoder_cmd,
404 (hdw->enc_cur_valid ? &hdw->enc_cur_state : NULL),
405 &hdw->enc_ctl_state);
406 if (ret) {
407 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
408 "Error from cx2341x module code=%d",ret);
409 } else {
410 memcpy(&hdw->enc_cur_state,&hdw->enc_ctl_state,
411 sizeof(struct cx2341x_mpeg_params));
412 hdw->enc_cur_valid = !0;
413 }
414 return ret;
415}
416
417
397int pvr2_encoder_configure(struct pvr2_hdw *hdw) 418int pvr2_encoder_configure(struct pvr2_hdw *hdw)
398{ 419{
399 int ret; 420 int ret;
@@ -412,7 +433,7 @@ int pvr2_encoder_configure(struct pvr2_hdw *hdw)
412 433
413 /* saa7115: 0xf0 */ 434 /* saa7115: 0xf0 */
414 val = 0xf0; 435 val = 0xf0;
415 if (hdw->hdw_type == PVR2_HDW_TYPE_24XXX) { 436 if (hdw->hdw_desc->flag_has_cx25840) {
416 /* ivtv cx25840: 0x140 */ 437 /* ivtv cx25840: 0x140 */
417 val = 0x140; 438 val = 0x140;
418 } 439 }
@@ -436,18 +457,10 @@ int pvr2_encoder_configure(struct pvr2_hdw *hdw)
436 return ret; 457 return ret;
437 } 458 }
438 459
439 ret = cx2341x_update(hdw,pvr2_encoder_cmd, 460 ret = pvr2_encoder_adjust(hdw);
440 (hdw->enc_cur_valid ? &hdw->enc_cur_state : NULL), 461 if (ret) return ret;
441 &hdw->enc_ctl_state);
442 if (ret) {
443 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
444 "Error from cx2341x module code=%d",ret);
445 return ret;
446 }
447
448 ret = 0;
449 462
450 if (!ret) ret = pvr2_encoder_vcmd( 463 ret = pvr2_encoder_vcmd(
451 hdw, CX2341X_ENC_INITIALIZE_INPUT, 0); 464 hdw, CX2341X_ENC_INITIALIZE_INPUT, 0);
452 465
453 if (ret) { 466 if (ret) {
@@ -456,10 +469,6 @@ int pvr2_encoder_configure(struct pvr2_hdw *hdw)
456 return ret; 469 return ret;
457 } 470 }
458 471
459 hdw->subsys_enabled_mask |= (1<<PVR2_SUBSYS_B_ENC_CFG);
460 memcpy(&hdw->enc_cur_state,&hdw->enc_ctl_state,
461 sizeof(struct cx2341x_mpeg_params));
462 hdw->enc_cur_valid = !0;
463 return 0; 472 return 0;
464} 473}
465 474
@@ -478,7 +487,7 @@ int pvr2_encoder_start(struct pvr2_hdw *hdw)
478 pvr2_encoder_vcmd(hdw,CX2341X_ENC_MUTE_VIDEO,1, 487 pvr2_encoder_vcmd(hdw,CX2341X_ENC_MUTE_VIDEO,1,
479 hdw->input_val == PVR2_CVAL_INPUT_RADIO ? 1 : 0); 488 hdw->input_val == PVR2_CVAL_INPUT_RADIO ? 1 : 0);
480 489
481 switch (hdw->config) { 490 switch (hdw->active_stream_type) {
482 case pvr2_config_vbi: 491 case pvr2_config_vbi:
483 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2, 492 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
484 0x01,0x14); 493 0x01,0x14);
@@ -492,9 +501,6 @@ int pvr2_encoder_start(struct pvr2_hdw *hdw)
492 0,0x13); 501 0,0x13);
493 break; 502 break;
494 } 503 }
495 if (!status) {
496 hdw->subsys_enabled_mask |= (1<<PVR2_SUBSYS_B_ENC_RUN);
497 }
498 return status; 504 return status;
499} 505}
500 506
@@ -505,7 +511,7 @@ int pvr2_encoder_stop(struct pvr2_hdw *hdw)
505 /* mask all interrupts */ 511 /* mask all interrupts */
506 pvr2_write_register(hdw, 0x0048, 0xffffffff); 512 pvr2_write_register(hdw, 0x0048, 0xffffffff);
507 513
508 switch (hdw->config) { 514 switch (hdw->active_stream_type) {
509 case pvr2_config_vbi: 515 case pvr2_config_vbi:
510 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3, 516 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
511 0x01,0x01,0x14); 517 0x01,0x01,0x14);
@@ -526,9 +532,6 @@ int pvr2_encoder_stop(struct pvr2_hdw *hdw)
526 pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000401); 532 pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000401);
527 pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000000); 533 pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000000);
528 534
529 if (!status) {
530 hdw->subsys_enabled_mask &= ~(1<<PVR2_SUBSYS_B_ENC_RUN);
531 }
532 return status; 535 return status;
533} 536}
534 537
diff --git a/drivers/media/video/pvrusb2/pvrusb2-encoder.h b/drivers/media/video/pvrusb2/pvrusb2-encoder.h
index 01b5a0b89c03..54caf2e3c428 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-encoder.h
+++ b/drivers/media/video/pvrusb2/pvrusb2-encoder.h
@@ -25,6 +25,7 @@
25 25
26struct pvr2_hdw; 26struct pvr2_hdw;
27 27
28int pvr2_encoder_adjust(struct pvr2_hdw *);
28int pvr2_encoder_configure(struct pvr2_hdw *); 29int pvr2_encoder_configure(struct pvr2_hdw *);
29int pvr2_encoder_start(struct pvr2_hdw *); 30int pvr2_encoder_start(struct pvr2_hdw *);
30int pvr2_encoder_stop(struct pvr2_hdw *); 31int pvr2_encoder_stop(struct pvr2_hdw *);
diff --git a/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h b/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h
index f873994b088c..d7a216b41b72 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h
+++ b/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h
@@ -35,10 +35,12 @@
35 35
36#include <linux/videodev2.h> 36#include <linux/videodev2.h>
37#include <linux/i2c.h> 37#include <linux/i2c.h>
38#include <linux/workqueue.h>
38#include <linux/mutex.h> 39#include <linux/mutex.h>
39#include "pvrusb2-hdw.h" 40#include "pvrusb2-hdw.h"
40#include "pvrusb2-io.h" 41#include "pvrusb2-io.h"
41#include <media/cx2341x.h> 42#include <media/cx2341x.h>
43#include "pvrusb2-devattr.h"
42 44
43/* Legal values for PVR2_CID_HSM */ 45/* Legal values for PVR2_CID_HSM */
44#define PVR2_CVAL_HSM_FAIL 0 46#define PVR2_CVAL_HSM_FAIL 0
@@ -161,10 +163,6 @@ struct pvr2_decoder_ctrl {
161#define FW1_STATE_RELOAD 3 163#define FW1_STATE_RELOAD 3
162#define FW1_STATE_OK 4 164#define FW1_STATE_OK 4
163 165
164/* Known major hardware variants, keyed from device ID */
165#define PVR2_HDW_TYPE_29XXX 0
166#define PVR2_HDW_TYPE_24XXX 1
167
168typedef int (*pvr2_i2c_func)(struct pvr2_hdw *,u8,u8 *,u16,u8 *, u16); 166typedef int (*pvr2_i2c_func)(struct pvr2_hdw *,u8,u8 *,u16,u8 *, u16);
169#define PVR2_I2C_FUNC_CNT 128 167#define PVR2_I2C_FUNC_CNT 128
170 168
@@ -176,8 +174,15 @@ struct pvr2_hdw {
176 struct usb_device *usb_dev; 174 struct usb_device *usb_dev;
177 struct usb_interface *usb_intf; 175 struct usb_interface *usb_intf;
178 176
179 /* Device type, one of PVR2_HDW_TYPE_xxxxx */ 177 /* Device description, anything that must adjust behavior based on
180 unsigned int hdw_type; 178 device specific info will use information held here. */
179 const struct pvr2_device_desc *hdw_desc;
180
181 /* Kernel worker thread handling */
182 struct workqueue_struct *workqueue;
183 struct work_struct workpoll; /* Update driver state */
184 struct work_struct worki2csync; /* Update i2c clients */
185 struct work_struct workinit; /* Driver initialization sequence */
181 186
182 /* Video spigot */ 187 /* Video spigot */
183 struct pvr2_stream *vid_stream; 188 struct pvr2_stream *vid_stream;
@@ -186,9 +191,6 @@ struct pvr2_hdw {
186 struct mutex big_lock_mutex; 191 struct mutex big_lock_mutex;
187 int big_lock_held; /* For debugging */ 192 int big_lock_held; /* For debugging */
188 193
189 void (*poll_trigger_func)(void *);
190 void *poll_trigger_data;
191
192 char name[32]; 194 char name[32];
193 195
194 /* I2C stuff */ 196 /* I2C stuff */
@@ -215,9 +217,9 @@ struct pvr2_hdw {
215 struct urb *ctl_read_urb; 217 struct urb *ctl_read_urb;
216 unsigned char *ctl_write_buffer; 218 unsigned char *ctl_write_buffer;
217 unsigned char *ctl_read_buffer; 219 unsigned char *ctl_read_buffer;
218 volatile int ctl_write_pend_flag; 220 int ctl_write_pend_flag;
219 volatile int ctl_read_pend_flag; 221 int ctl_read_pend_flag;
220 volatile int ctl_timeout_flag; 222 int ctl_timeout_flag;
221 struct completion ctl_done; 223 struct completion ctl_done;
222 unsigned char cmd_buffer[PVR2_CTL_BUFFSIZE]; 224 unsigned char cmd_buffer[PVR2_CTL_BUFFSIZE];
223 int cmd_debug_state; // Low level command debugging info 225 int cmd_debug_state; // Low level command debugging info
@@ -225,14 +227,48 @@ struct pvr2_hdw {
225 unsigned int cmd_debug_write_len; // 227 unsigned int cmd_debug_write_len; //
226 unsigned int cmd_debug_read_len; // 228 unsigned int cmd_debug_read_len; //
227 229
230 /* Bits of state that describe what is going on with various parts
231 of the driver. */
232 int state_encoder_ok; /* Encoder is operational */
233 int state_encoder_run; /* Encoder is running */
234 int state_encoder_config; /* Encoder is configured */
235 int state_encoder_waitok; /* Encoder pre-wait done */
236 int state_decoder_run; /* Decoder is running */
237 int state_usbstream_run; /* FX2 is streaming */
238 int state_decoder_quiescent; /* Decoder idle for > 50msec */
239 int state_pipeline_config; /* Pipeline is configured */
240 int state_pipeline_req; /* Somebody wants to stream */
241 int state_pipeline_pause; /* Pipeline must be paused */
242 int state_pipeline_idle; /* Pipeline not running */
243
244 /* This is the master state of the driver. It is the combined
245 result of other bits of state. Examining this will indicate the
246 overall state of the driver. Values here are one of
247 PVR2_STATE_xxxx */
248 unsigned int master_state;
249
250 /* True if states must be re-evaluated */
251 int state_stale;
252
253 void (*state_func)(void *);
254 void *state_data;
255
256 /* Timer for measuring decoder settling time */
257 struct timer_list quiescent_timer;
258
259 /* Timer for measuring encoder pre-wait time */
260 struct timer_list encoder_wait_timer;
261
262 /* Place to block while waiting for state changes */
263 wait_queue_head_t state_wait_data;
264
265
228 int flag_ok; /* device in known good state */ 266 int flag_ok; /* device in known good state */
229 int flag_disconnected; /* flag_ok == 0 due to disconnect */ 267 int flag_disconnected; /* flag_ok == 0 due to disconnect */
230 int flag_init_ok; /* true if structure is fully initialized */ 268 int flag_init_ok; /* true if structure is fully initialized */
231 int flag_streaming_enabled; /* true if streaming should be on */
232 int fw1_state; /* current situation with fw1 */ 269 int fw1_state; /* current situation with fw1 */
233 int flag_encoder_ok; /* True if encoder is healthy */ 270 int flag_decoder_missed;/* We've noticed missing decoder */
234 271 int flag_tripped; /* Indicates overall failure to start */
235 int flag_decoder_is_tuned;
236 272
237 struct pvr2_decoder_ctrl *decoder_ctrl; 273 struct pvr2_decoder_ctrl *decoder_ctrl;
238 274
@@ -241,12 +277,6 @@ struct pvr2_hdw {
241 unsigned int fw_size; 277 unsigned int fw_size;
242 int fw_cpu_flag; /* True if we are dealing with the CPU */ 278 int fw_cpu_flag; /* True if we are dealing with the CPU */
243 279
244 // Which subsystem pieces have been enabled / configured
245 unsigned long subsys_enabled_mask;
246
247 // Which subsystems are manipulated to enable streaming
248 unsigned long subsys_stream_mask;
249
250 // True if there is a request to trigger logging of state in each 280 // True if there is a request to trigger logging of state in each
251 // module. 281 // module.
252 int log_requested; 282 int log_requested;
@@ -296,13 +326,16 @@ struct pvr2_hdw {
296 /* Location of eeprom or a negative number if none */ 326 /* Location of eeprom or a negative number if none */
297 int eeprom_addr; 327 int eeprom_addr;
298 328
299 enum pvr2_config config; 329 enum pvr2_config active_stream_type;
330 enum pvr2_config desired_stream_type;
300 331
301 /* Control state needed for cx2341x module */ 332 /* Control state needed for cx2341x module */
302 struct cx2341x_mpeg_params enc_cur_state; 333 struct cx2341x_mpeg_params enc_cur_state;
303 struct cx2341x_mpeg_params enc_ctl_state; 334 struct cx2341x_mpeg_params enc_ctl_state;
304 /* True if an encoder attribute has changed */ 335 /* True if an encoder attribute has changed */
305 int enc_stale; 336 int enc_stale;
337 /* True if an unsafe encoder attribute has changed */
338 int enc_unsafe_stale;
306 /* True if enc_cur_state is valid */ 339 /* True if enc_cur_state is valid */
307 int enc_cur_valid; 340 int enc_cur_valid;
308 341
@@ -332,6 +365,7 @@ struct pvr2_hdw {
332 365
333/* This function gets the current frequency */ 366/* This function gets the current frequency */
334unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *); 367unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *);
368void pvr2_hdw_set_decoder(struct pvr2_hdw *,struct pvr2_decoder_ctrl *);
335 369
336#endif /* __PVRUSB2_HDW_INTERNAL_H */ 370#endif /* __PVRUSB2_HDW_INTERNAL_H */
337 371
diff --git a/drivers/media/video/pvrusb2/pvrusb2-hdw.c b/drivers/media/video/pvrusb2/pvrusb2-hdw.c
index 402c59488253..41ae980405ed 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-hdw.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-hdw.c
@@ -41,47 +41,6 @@
41#define TV_MIN_FREQ 55250000L 41#define TV_MIN_FREQ 55250000L
42#define TV_MAX_FREQ 850000000L 42#define TV_MAX_FREQ 850000000L
43 43
44struct usb_device_id pvr2_device_table[] = {
45 [PVR2_HDW_TYPE_29XXX] = { USB_DEVICE(0x2040, 0x2900) },
46 [PVR2_HDW_TYPE_24XXX] = { USB_DEVICE(0x2040, 0x2400) },
47 { }
48};
49
50MODULE_DEVICE_TABLE(usb, pvr2_device_table);
51
52static const char *pvr2_device_names[] = {
53 [PVR2_HDW_TYPE_29XXX] = "WinTV PVR USB2 Model Category 29xxxx",
54 [PVR2_HDW_TYPE_24XXX] = "WinTV PVR USB2 Model Category 24xxxx",
55};
56
57struct pvr2_string_table {
58 const char **lst;
59 unsigned int cnt;
60};
61
62// Names of other client modules to request for 24xxx model hardware
63static const char *pvr2_client_24xxx[] = {
64 "cx25840",
65 "tuner",
66 "wm8775",
67};
68
69// Names of other client modules to request for 29xxx model hardware
70static const char *pvr2_client_29xxx[] = {
71 "msp3400",
72 "saa7115",
73 "tuner",
74};
75
76static struct pvr2_string_table pvr2_client_lists[] = {
77 [PVR2_HDW_TYPE_29XXX] = {
78 pvr2_client_29xxx, ARRAY_SIZE(pvr2_client_29xxx)
79 },
80 [PVR2_HDW_TYPE_24XXX] = {
81 pvr2_client_24xxx, ARRAY_SIZE(pvr2_client_24xxx)
82 },
83};
84
85static struct pvr2_hdw *unit_pointers[PVR_NUM] = {[ 0 ... PVR_NUM-1 ] = NULL}; 44static struct pvr2_hdw *unit_pointers[PVR_NUM] = {[ 0 ... PVR_NUM-1 ] = NULL};
86static DEFINE_MUTEX(pvr2_unit_mtx); 45static DEFINE_MUTEX(pvr2_unit_mtx);
87 46
@@ -246,32 +205,46 @@ static const char *control_values_hsm[] = {
246}; 205};
247 206
248 207
249static const char *control_values_subsystem[] = { 208static const char *pvr2_state_names[] = {
250 [PVR2_SUBSYS_B_ENC_FIRMWARE] = "enc_firmware", 209 [PVR2_STATE_NONE] = "none",
251 [PVR2_SUBSYS_B_ENC_CFG] = "enc_config", 210 [PVR2_STATE_DEAD] = "dead",
252 [PVR2_SUBSYS_B_DIGITIZER_RUN] = "digitizer_run", 211 [PVR2_STATE_COLD] = "cold",
253 [PVR2_SUBSYS_B_USBSTREAM_RUN] = "usbstream_run", 212 [PVR2_STATE_WARM] = "warm",
254 [PVR2_SUBSYS_B_ENC_RUN] = "enc_run", 213 [PVR2_STATE_ERROR] = "error",
214 [PVR2_STATE_READY] = "ready",
215 [PVR2_STATE_RUN] = "run",
255}; 216};
256 217
218
219static void pvr2_hdw_state_sched(struct pvr2_hdw *);
220static int pvr2_hdw_state_eval(struct pvr2_hdw *);
257static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long); 221static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
222static void pvr2_hdw_worker_i2c(struct work_struct *work);
223static void pvr2_hdw_worker_poll(struct work_struct *work);
224static void pvr2_hdw_worker_init(struct work_struct *work);
225static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
226static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
227static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
258static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl); 228static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
259static int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw); 229static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
260static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw); 230static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
261static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw); 231static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
262static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw); 232static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
263static void pvr2_hdw_render_useless_unlocked(struct pvr2_hdw *hdw); 233static void pvr2_hdw_quiescent_timeout(unsigned long);
264static void pvr2_hdw_subsys_bit_chg_no_lock(struct pvr2_hdw *hdw, 234static void pvr2_hdw_encoder_wait_timeout(unsigned long);
265 unsigned long msk,
266 unsigned long val);
267static void pvr2_hdw_subsys_stream_bit_chg_no_lock(struct pvr2_hdw *hdw,
268 unsigned long msk,
269 unsigned long val);
270static int pvr2_send_request_ex(struct pvr2_hdw *hdw, 235static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
271 unsigned int timeout,int probe_fl, 236 unsigned int timeout,int probe_fl,
272 void *write_data,unsigned int write_len, 237 void *write_data,unsigned int write_len,
273 void *read_data,unsigned int read_len); 238 void *read_data,unsigned int read_len);
274 239
240
241static void trace_stbit(const char *name,int val)
242{
243 pvr2_trace(PVR2_TRACE_STBITS,
244 "State bit %s <-- %s",
245 name,(val ? "true" : "false"));
246}
247
275static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp) 248static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
276{ 249{
277 struct pvr2_hdw *hdw = cptr->hdw; 250 struct pvr2_hdw *hdw = cptr->hdw;
@@ -380,8 +353,8 @@ static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
380 353
381static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp) 354static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
382{ 355{
383 /* Actual minimum depends on device type. */ 356 /* Actual minimum depends on device digitizer type. */
384 if (cptr->hdw->hdw_type == PVR2_HDW_TYPE_24XXX) { 357 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
385 *vp = 75; 358 *vp = 75;
386 } else { 359 } else {
387 *vp = 17; 360 *vp = 17;
@@ -480,6 +453,7 @@ static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
480static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr) 453static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
481{ 454{
482 cptr->hdw->enc_stale = 0; 455 cptr->hdw->enc_stale = 0;
456 cptr->hdw->enc_unsafe_stale = 0;
483} 457}
484 458
485static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp) 459static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
@@ -502,6 +476,7 @@ static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
502static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v) 476static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
503{ 477{
504 int ret; 478 int ret;
479 struct pvr2_hdw *hdw = cptr->hdw;
505 struct v4l2_ext_controls cs; 480 struct v4l2_ext_controls cs;
506 struct v4l2_ext_control c1; 481 struct v4l2_ext_control c1;
507 memset(&cs,0,sizeof(cs)); 482 memset(&cs,0,sizeof(cs));
@@ -510,10 +485,22 @@ static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
510 cs.count = 1; 485 cs.count = 1;
511 c1.id = cptr->info->v4l_id; 486 c1.id = cptr->info->v4l_id;
512 c1.value = v; 487 c1.value = v;
513 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs, 488 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
489 hdw->state_encoder_run, &cs,
514 VIDIOC_S_EXT_CTRLS); 490 VIDIOC_S_EXT_CTRLS);
491 if (ret == -EBUSY) {
492 /* Oops. cx2341x is telling us it's not safe to change
493 this control while we're capturing. Make a note of this
494 fact so that the pipeline will be stopped the next time
495 controls are committed. Then go on ahead and store this
496 change anyway. */
497 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
498 0, &cs,
499 VIDIOC_S_EXT_CTRLS);
500 if (!ret) hdw->enc_unsafe_stale = !0;
501 }
515 if (ret) return ret; 502 if (ret) return ret;
516 cptr->hdw->enc_stale = !0; 503 hdw->enc_stale = !0;
517 return 0; 504 return 0;
518} 505}
519 506
@@ -544,7 +531,13 @@ static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
544 531
545static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp) 532static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
546{ 533{
547 *vp = cptr->hdw->flag_streaming_enabled; 534 *vp = cptr->hdw->state_pipeline_req;
535 return 0;
536}
537
538static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
539{
540 *vp = cptr->hdw->master_state;
548 return 0; 541 return 0;
549} 542}
550 543
@@ -657,29 +650,6 @@ static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
657 return 0; 650 return 0;
658} 651}
659 652
660static int ctrl_subsys_get(struct pvr2_ctrl *cptr,int *vp)
661{
662 *vp = cptr->hdw->subsys_enabled_mask;
663 return 0;
664}
665
666static int ctrl_subsys_set(struct pvr2_ctrl *cptr,int m,int v)
667{
668 pvr2_hdw_subsys_bit_chg_no_lock(cptr->hdw,m,v);
669 return 0;
670}
671
672static int ctrl_subsys_stream_get(struct pvr2_ctrl *cptr,int *vp)
673{
674 *vp = cptr->hdw->subsys_stream_mask;
675 return 0;
676}
677
678static int ctrl_subsys_stream_set(struct pvr2_ctrl *cptr,int m,int v)
679{
680 pvr2_hdw_subsys_stream_bit_chg_no_lock(cptr->hdw,m,v);
681 return 0;
682}
683 653
684static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v) 654static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
685{ 655{
@@ -915,6 +885,11 @@ static const struct pvr2_ctl_info control_defs[] = {
915 .get_value = ctrl_hsm_get, 885 .get_value = ctrl_hsm_get,
916 DEFENUM(control_values_hsm), 886 DEFENUM(control_values_hsm),
917 },{ 887 },{
888 .desc = "Master State",
889 .name = "master_state",
890 .get_value = ctrl_masterstate_get,
891 DEFENUM(pvr2_state_names),
892 },{
918 .desc = "Signal Present", 893 .desc = "Signal Present",
919 .name = "signal_present", 894 .name = "signal_present",
920 .get_value = ctrl_signal_get, 895 .get_value = ctrl_signal_get,
@@ -955,20 +930,6 @@ static const struct pvr2_ctl_info control_defs[] = {
955 .sym_to_val = ctrl_std_sym_to_val, 930 .sym_to_val = ctrl_std_sym_to_val,
956 .type = pvr2_ctl_bitmask, 931 .type = pvr2_ctl_bitmask,
957 },{ 932 },{
958 .desc = "Subsystem enabled mask",
959 .name = "debug_subsys_mask",
960 .skip_init = !0,
961 .get_value = ctrl_subsys_get,
962 .set_value = ctrl_subsys_set,
963 DEFMASK(PVR2_SUBSYS_ALL,control_values_subsystem),
964 },{
965 .desc = "Subsystem stream mask",
966 .name = "debug_subsys_stream_mask",
967 .skip_init = !0,
968 .get_value = ctrl_subsys_stream_get,
969 .set_value = ctrl_subsys_stream_set,
970 DEFMASK(PVR2_SUBSYS_ALL,control_values_subsystem),
971 },{
972 .desc = "Video Standard Name", 933 .desc = "Video Standard Name",
973 .name = "video_standard", 934 .name = "video_standard",
974 .internal_id = PVR2_CID_STDENUM, 935 .internal_id = PVR2_CID_STDENUM,
@@ -1129,25 +1090,13 @@ static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
1129 unsigned int pipe; 1090 unsigned int pipe;
1130 int ret; 1091 int ret;
1131 u16 address; 1092 u16 address;
1132 static const char *fw_files_29xxx[] = {
1133 "v4l-pvrusb2-29xxx-01.fw",
1134 };
1135 static const char *fw_files_24xxx[] = {
1136 "v4l-pvrusb2-24xxx-01.fw",
1137 };
1138 static const struct pvr2_string_table fw_file_defs[] = {
1139 [PVR2_HDW_TYPE_29XXX] = {
1140 fw_files_29xxx, ARRAY_SIZE(fw_files_29xxx)
1141 },
1142 [PVR2_HDW_TYPE_24XXX] = {
1143 fw_files_24xxx, ARRAY_SIZE(fw_files_24xxx)
1144 },
1145 };
1146 1093
1147 if ((hdw->hdw_type >= ARRAY_SIZE(fw_file_defs)) || 1094 if (!hdw->hdw_desc->fx2_firmware.cnt) {
1148 (!fw_file_defs[hdw->hdw_type].lst)) {
1149 hdw->fw1_state = FW1_STATE_OK; 1095 hdw->fw1_state = FW1_STATE_OK;
1150 return 0; 1096 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1097 "Connected device type defines"
1098 " no firmware to upload; ignoring firmware");
1099 return -ENOTTY;
1151 } 1100 }
1152 1101
1153 hdw->fw1_state = FW1_STATE_FAILED; // default result 1102 hdw->fw1_state = FW1_STATE_FAILED; // default result
@@ -1155,8 +1104,8 @@ static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
1155 trace_firmware("pvr2_upload_firmware1"); 1104 trace_firmware("pvr2_upload_firmware1");
1156 1105
1157 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller", 1106 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
1158 fw_file_defs[hdw->hdw_type].cnt, 1107 hdw->hdw_desc->fx2_firmware.cnt,
1159 fw_file_defs[hdw->hdw_type].lst); 1108 hdw->hdw_desc->fx2_firmware.lst);
1160 if (ret < 0) { 1109 if (ret < 0) {
1161 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING; 1110 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1162 return ret; 1111 return ret;
@@ -1231,8 +1180,7 @@ int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1231 CX2341X_FIRM_ENC_FILENAME, 1180 CX2341X_FIRM_ENC_FILENAME,
1232 }; 1181 };
1233 1182
1234 if ((hdw->hdw_type != PVR2_HDW_TYPE_29XXX) && 1183 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
1235 (hdw->hdw_type != PVR2_HDW_TYPE_24XXX)) {
1236 return 0; 1184 return 0;
1237 } 1185 }
1238 1186
@@ -1248,8 +1196,6 @@ int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1248 time we configure the encoder, then we'll fully configure it. */ 1196 time we configure the encoder, then we'll fully configure it. */
1249 hdw->enc_cur_valid = 0; 1197 hdw->enc_cur_valid = 0;
1250 1198
1251 hdw->flag_encoder_ok = 0;
1252
1253 /* First prepare firmware loading */ 1199 /* First prepare firmware loading */
1254 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/ 1200 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1255 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/ 1201 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
@@ -1347,293 +1293,129 @@ int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1347 if (ret) { 1293 if (ret) {
1348 pvr2_trace(PVR2_TRACE_ERROR_LEGS, 1294 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1349 "firmware2 upload post-proc failure"); 1295 "firmware2 upload post-proc failure");
1350 } else {
1351 hdw->flag_encoder_ok = !0;
1352 hdw->subsys_enabled_mask |= (1<<PVR2_SUBSYS_B_ENC_FIRMWARE);
1353 } 1296 }
1354 return ret; 1297 return ret;
1355} 1298}
1356 1299
1357 1300
1358#define FIRMWARE_RECOVERY_BITS \ 1301static const char *pvr2_get_state_name(unsigned int st)
1359 ((1<<PVR2_SUBSYS_B_ENC_CFG) | \ 1302{
1360 (1<<PVR2_SUBSYS_B_ENC_RUN) | \ 1303 if (st < ARRAY_SIZE(pvr2_state_names)) {
1361 (1<<PVR2_SUBSYS_B_ENC_FIRMWARE) | \ 1304 return pvr2_state_names[st];
1362 (1<<PVR2_SUBSYS_B_USBSTREAM_RUN))
1363
1364/*
1365
1366 This single function is key to pretty much everything. The pvrusb2
1367 device can logically be viewed as a series of subsystems which can be
1368 stopped / started or unconfigured / configured. To get things streaming,
1369 one must configure everything and start everything, but there may be
1370 various reasons over time to deconfigure something or stop something.
1371 This function handles all of this activity. Everything EVERYWHERE that
1372 must affect a subsystem eventually comes here to do the work.
1373
1374 The current state of all subsystems is represented by a single bit mask,
1375 known as subsys_enabled_mask. The bit positions are defined by the
1376 PVR2_SUBSYS_xxxx macros, with one subsystem per bit position. At any
1377 time the set of configured or active subsystems can be queried just by
1378 looking at that mask. To change bits in that mask, this function here
1379 must be called. The "msk" argument indicates which bit positions to
1380 change, and the "val" argument defines the new values for the positions
1381 defined by "msk".
1382
1383 There is a priority ordering of starting / stopping things, and for
1384 multiple requested changes, this function implements that ordering.
1385 (Thus we will act on a request to load encoder firmware before we
1386 configure the encoder.) In addition to priority ordering, there is a
1387 recovery strategy implemented here. If a particular step fails and we
1388 detect that failure, this function will clear the affected subsystem bits
1389 and restart. Thus we have a means for recovering from a dead encoder:
1390 Clear all bits that correspond to subsystems that we need to restart /
1391 reconfigure and start over.
1392
1393*/
1394static void pvr2_hdw_subsys_bit_chg_no_lock(struct pvr2_hdw *hdw,
1395 unsigned long msk,
1396 unsigned long val)
1397{
1398 unsigned long nmsk;
1399 unsigned long vmsk;
1400 int ret;
1401 unsigned int tryCount = 0;
1402
1403 if (!hdw->flag_ok) return;
1404
1405 msk &= PVR2_SUBSYS_ALL;
1406 nmsk = (hdw->subsys_enabled_mask & ~msk) | (val & msk);
1407 nmsk &= PVR2_SUBSYS_ALL;
1408
1409 for (;;) {
1410 tryCount++;
1411 if (!((nmsk ^ hdw->subsys_enabled_mask) &
1412 PVR2_SUBSYS_ALL)) break;
1413 if (tryCount > 4) {
1414 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1415 "Too many retries when configuring device;"
1416 " giving up");
1417 pvr2_hdw_render_useless(hdw);
1418 break;
1419 }
1420 if (tryCount > 1) {
1421 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1422 "Retrying device reconfiguration");
1423 }
1424 pvr2_trace(PVR2_TRACE_INIT,
1425 "subsys mask changing 0x%lx:0x%lx"
1426 " from 0x%lx to 0x%lx",
1427 msk,val,hdw->subsys_enabled_mask,nmsk);
1428
1429 vmsk = (nmsk ^ hdw->subsys_enabled_mask) &
1430 hdw->subsys_enabled_mask;
1431 if (vmsk) {
1432 if (vmsk & (1<<PVR2_SUBSYS_B_ENC_RUN)) {
1433 pvr2_trace(PVR2_TRACE_CTL,
1434 "/*---TRACE_CTL----*/"
1435 " pvr2_encoder_stop");
1436 ret = pvr2_encoder_stop(hdw);
1437 if (ret) {
1438 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1439 "Error recovery initiated");
1440 hdw->subsys_enabled_mask &=
1441 ~FIRMWARE_RECOVERY_BITS;
1442 continue;
1443 }
1444 }
1445 if (vmsk & (1<<PVR2_SUBSYS_B_USBSTREAM_RUN)) {
1446 pvr2_trace(PVR2_TRACE_CTL,
1447 "/*---TRACE_CTL----*/"
1448 " pvr2_hdw_cmd_usbstream(0)");
1449 pvr2_hdw_cmd_usbstream(hdw,0);
1450 }
1451 if (vmsk & (1<<PVR2_SUBSYS_B_DIGITIZER_RUN)) {
1452 pvr2_trace(PVR2_TRACE_CTL,
1453 "/*---TRACE_CTL----*/"
1454 " decoder disable");
1455 if (hdw->decoder_ctrl) {
1456 hdw->decoder_ctrl->enable(
1457 hdw->decoder_ctrl->ctxt,0);
1458 } else {
1459 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1460 "WARNING:"
1461 " No decoder present");
1462 }
1463 hdw->subsys_enabled_mask &=
1464 ~(1<<PVR2_SUBSYS_B_DIGITIZER_RUN);
1465 }
1466 if (vmsk & PVR2_SUBSYS_CFG_ALL) {
1467 hdw->subsys_enabled_mask &=
1468 ~(vmsk & PVR2_SUBSYS_CFG_ALL);
1469 }
1470 }
1471 vmsk = (nmsk ^ hdw->subsys_enabled_mask) & nmsk;
1472 if (vmsk) {
1473 if (vmsk & (1<<PVR2_SUBSYS_B_ENC_FIRMWARE)) {
1474 pvr2_trace(PVR2_TRACE_CTL,
1475 "/*---TRACE_CTL----*/"
1476 " pvr2_upload_firmware2");
1477 ret = pvr2_upload_firmware2(hdw);
1478 if (ret) {
1479 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1480 "Failure uploading encoder"
1481 " firmware");
1482 pvr2_hdw_render_useless(hdw);
1483 break;
1484 }
1485 }
1486 if (vmsk & (1<<PVR2_SUBSYS_B_ENC_CFG)) {
1487 pvr2_trace(PVR2_TRACE_CTL,
1488 "/*---TRACE_CTL----*/"
1489 " pvr2_encoder_configure");
1490 ret = pvr2_encoder_configure(hdw);
1491 if (ret) {
1492 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1493 "Error recovery initiated");
1494 hdw->subsys_enabled_mask &=
1495 ~FIRMWARE_RECOVERY_BITS;
1496 continue;
1497 }
1498 }
1499 if (vmsk & (1<<PVR2_SUBSYS_B_DIGITIZER_RUN)) {
1500 pvr2_trace(PVR2_TRACE_CTL,
1501 "/*---TRACE_CTL----*/"
1502 " decoder enable");
1503 if (hdw->decoder_ctrl) {
1504 hdw->decoder_ctrl->enable(
1505 hdw->decoder_ctrl->ctxt,!0);
1506 } else {
1507 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1508 "WARNING:"
1509 " No decoder present");
1510 }
1511 hdw->subsys_enabled_mask |=
1512 (1<<PVR2_SUBSYS_B_DIGITIZER_RUN);
1513 }
1514 if (vmsk & (1<<PVR2_SUBSYS_B_USBSTREAM_RUN)) {
1515 pvr2_trace(PVR2_TRACE_CTL,
1516 "/*---TRACE_CTL----*/"
1517 " pvr2_hdw_cmd_usbstream(1)");
1518 pvr2_hdw_cmd_usbstream(hdw,!0);
1519 }
1520 if (vmsk & (1<<PVR2_SUBSYS_B_ENC_RUN)) {
1521 pvr2_trace(PVR2_TRACE_CTL,
1522 "/*---TRACE_CTL----*/"
1523 " pvr2_encoder_start");
1524 ret = pvr2_encoder_start(hdw);
1525 if (ret) {
1526 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1527 "Error recovery initiated");
1528 hdw->subsys_enabled_mask &=
1529 ~FIRMWARE_RECOVERY_BITS;
1530 continue;
1531 }
1532 }
1533 }
1534 } 1305 }
1306 return "???";
1535} 1307}
1536 1308
1537 1309static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
1538void pvr2_hdw_subsys_bit_chg(struct pvr2_hdw *hdw,
1539 unsigned long msk,unsigned long val)
1540{ 1310{
1541 LOCK_TAKE(hdw->big_lock); do { 1311 if (!hdw->decoder_ctrl) {
1542 pvr2_hdw_subsys_bit_chg_no_lock(hdw,msk,val); 1312 if (!hdw->flag_decoder_missed) {
1543 } while (0); LOCK_GIVE(hdw->big_lock); 1313 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1314 "WARNING: No decoder present");
1315 hdw->flag_decoder_missed = !0;
1316 trace_stbit("flag_decoder_missed",
1317 hdw->flag_decoder_missed);
1318 }
1319 return -EIO;
1320 }
1321 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt,enablefl);
1322 return 0;
1544} 1323}
1545 1324
1546 1325
1547unsigned long pvr2_hdw_subsys_get(struct pvr2_hdw *hdw) 1326void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1548{ 1327{
1549 return hdw->subsys_enabled_mask; 1328 if (hdw->decoder_ctrl == ptr) return;
1329 hdw->decoder_ctrl = ptr;
1330 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1331 hdw->flag_decoder_missed = 0;
1332 trace_stbit("flag_decoder_missed",
1333 hdw->flag_decoder_missed);
1334 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1335 "Decoder has appeared");
1336 pvr2_hdw_state_sched(hdw);
1337 }
1550} 1338}
1551 1339
1552 1340
1553unsigned long pvr2_hdw_subsys_stream_get(struct pvr2_hdw *hdw) 1341int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1554{ 1342{
1555 return hdw->subsys_stream_mask; 1343 return hdw->master_state;
1556} 1344}
1557 1345
1558 1346
1559static void pvr2_hdw_subsys_stream_bit_chg_no_lock(struct pvr2_hdw *hdw, 1347static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1560 unsigned long msk,
1561 unsigned long val)
1562{ 1348{
1563 unsigned long val2; 1349 if (!hdw->flag_tripped) return 0;
1564 msk &= PVR2_SUBSYS_ALL; 1350 hdw->flag_tripped = 0;
1565 val2 = ((hdw->subsys_stream_mask & ~msk) | (val & msk)); 1351 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1566 pvr2_trace(PVR2_TRACE_INIT, 1352 "Clearing driver error statuss");
1567 "stream mask changing 0x%lx:0x%lx from 0x%lx to 0x%lx", 1353 return !0;
1568 msk,val,hdw->subsys_stream_mask,val2);
1569 hdw->subsys_stream_mask = val2;
1570} 1354}
1571 1355
1572 1356
1573void pvr2_hdw_subsys_stream_bit_chg(struct pvr2_hdw *hdw, 1357int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1574 unsigned long msk,
1575 unsigned long val)
1576{ 1358{
1359 int fl;
1577 LOCK_TAKE(hdw->big_lock); do { 1360 LOCK_TAKE(hdw->big_lock); do {
1578 pvr2_hdw_subsys_stream_bit_chg_no_lock(hdw,msk,val); 1361 fl = pvr2_hdw_untrip_unlocked(hdw);
1579 } while (0); LOCK_GIVE(hdw->big_lock); 1362 } while (0); LOCK_GIVE(hdw->big_lock);
1363 if (fl) pvr2_hdw_state_sched(hdw);
1364 return 0;
1580} 1365}
1581 1366
1582 1367
1583static int pvr2_hdw_set_streaming_no_lock(struct pvr2_hdw *hdw,int enableFl) 1368const char *pvr2_hdw_get_state_name(unsigned int id)
1584{ 1369{
1585 if ((!enableFl) == !(hdw->flag_streaming_enabled)) return 0; 1370 if (id >= ARRAY_SIZE(pvr2_state_names)) return NULL;
1586 if (enableFl) { 1371 return pvr2_state_names[id];
1587 pvr2_trace(PVR2_TRACE_START_STOP,
1588 "/*--TRACE_STREAM--*/ enable");
1589 pvr2_hdw_subsys_bit_chg_no_lock(hdw,~0,~0);
1590 } else {
1591 pvr2_trace(PVR2_TRACE_START_STOP,
1592 "/*--TRACE_STREAM--*/ disable");
1593 pvr2_hdw_subsys_bit_chg_no_lock(hdw,hdw->subsys_stream_mask,0);
1594 }
1595 if (!hdw->flag_ok) return -EIO;
1596 hdw->flag_streaming_enabled = enableFl != 0;
1597 return 0;
1598} 1372}
1599 1373
1600 1374
1601int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw) 1375int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1602{ 1376{
1603 return hdw->flag_streaming_enabled != 0; 1377 return hdw->state_pipeline_req != 0;
1604} 1378}
1605 1379
1606 1380
1607int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag) 1381int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1608{ 1382{
1609 int ret; 1383 int ret,st;
1610 LOCK_TAKE(hdw->big_lock); do { 1384 LOCK_TAKE(hdw->big_lock); do {
1611 ret = pvr2_hdw_set_streaming_no_lock(hdw,enable_flag); 1385 pvr2_hdw_untrip_unlocked(hdw);
1386 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1387 hdw->state_pipeline_req = enable_flag != 0;
1388 pvr2_trace(PVR2_TRACE_START_STOP,
1389 "/*--TRACE_STREAM--*/ %s",
1390 enable_flag ? "enable" : "disable");
1391 }
1392 pvr2_hdw_state_sched(hdw);
1612 } while (0); LOCK_GIVE(hdw->big_lock); 1393 } while (0); LOCK_GIVE(hdw->big_lock);
1613 return ret; 1394 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1614} 1395 if (enable_flag) {
1615 1396 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1616 1397 if (st != PVR2_STATE_READY) return -EIO;
1617static int pvr2_hdw_set_stream_type_no_lock(struct pvr2_hdw *hdw, 1398 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1618 enum pvr2_config config) 1399 }
1619{ 1400 }
1620 unsigned long sm = hdw->subsys_enabled_mask;
1621 if (!hdw->flag_ok) return -EIO;
1622 pvr2_hdw_subsys_bit_chg_no_lock(hdw,hdw->subsys_stream_mask,0);
1623 hdw->config = config;
1624 pvr2_hdw_subsys_bit_chg_no_lock(hdw,~0,sm);
1625 return 0; 1401 return 0;
1626} 1402}
1627 1403
1628 1404
1629int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config) 1405int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1630{ 1406{
1631 int ret; 1407 int fl;
1632 if (!hdw->flag_ok) return -EIO;
1633 LOCK_TAKE(hdw->big_lock); 1408 LOCK_TAKE(hdw->big_lock);
1634 ret = pvr2_hdw_set_stream_type_no_lock(hdw,config); 1409 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1410 hdw->desired_stream_type = config;
1411 hdw->state_pipeline_config = 0;
1412 trace_stbit("state_pipeline_config",
1413 hdw->state_pipeline_config);
1414 pvr2_hdw_state_sched(hdw);
1415 }
1635 LOCK_GIVE(hdw->big_lock); 1416 LOCK_GIVE(hdw->big_lock);
1636 return ret; 1417 if (fl) return 0;
1418 return pvr2_hdw_wait(hdw,0);
1637} 1419}
1638 1420
1639 1421
@@ -1646,6 +1428,7 @@ static int get_default_tuner_type(struct pvr2_hdw *hdw)
1646 } 1428 }
1647 if (tp < 0) return -EINVAL; 1429 if (tp < 0) return -EINVAL;
1648 hdw->tuner_type = tp; 1430 hdw->tuner_type = tp;
1431 hdw->tuner_updated = !0;
1649 return 0; 1432 return 0;
1650} 1433}
1651 1434
@@ -1656,8 +1439,9 @@ static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1656 int tp = 0; 1439 int tp = 0;
1657 if ((unit_number >= 0) && (unit_number < PVR_NUM)) { 1440 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1658 tp = video_std[unit_number]; 1441 tp = video_std[unit_number];
1442 if (tp) return tp;
1659 } 1443 }
1660 return tp; 1444 return 0;
1661} 1445}
1662 1446
1663 1447
@@ -1731,7 +1515,7 @@ const static struct pvr2_std_hack std_eeprom_maps[] = {
1731 }, 1515 },
1732 { /* PAL(D/D1/K) */ 1516 { /* PAL(D/D1/K) */
1733 .pat = V4L2_STD_DK, 1517 .pat = V4L2_STD_DK,
1734 .std = V4L2_STD_PAL_D/V4L2_STD_PAL_D1|V4L2_STD_PAL_K, 1518 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
1735 }, 1519 },
1736}; 1520};
1737 1521
@@ -1739,18 +1523,20 @@ static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1739{ 1523{
1740 char buf[40]; 1524 char buf[40];
1741 unsigned int bcnt; 1525 unsigned int bcnt;
1742 v4l2_std_id std1,std2; 1526 v4l2_std_id std1,std2,std3;
1743 1527
1744 std1 = get_default_standard(hdw); 1528 std1 = get_default_standard(hdw);
1529 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
1745 1530
1746 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom); 1531 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
1747 pvr2_trace(PVR2_TRACE_STD, 1532 pvr2_trace(PVR2_TRACE_STD,
1748 "Supported video standard(s) reported by eeprom: %.*s", 1533 "Supported video standard(s) reported available"
1534 " in hardware: %.*s",
1749 bcnt,buf); 1535 bcnt,buf);
1750 1536
1751 hdw->std_mask_avail = hdw->std_mask_eeprom; 1537 hdw->std_mask_avail = hdw->std_mask_eeprom;
1752 1538
1753 std2 = std1 & ~hdw->std_mask_avail; 1539 std2 = (std1|std3) & ~hdw->std_mask_avail;
1754 if (std2) { 1540 if (std2) {
1755 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2); 1541 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
1756 pvr2_trace(PVR2_TRACE_STD, 1542 pvr2_trace(PVR2_TRACE_STD,
@@ -1772,6 +1558,16 @@ static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1772 pvr2_hdw_internal_find_stdenum(hdw); 1558 pvr2_hdw_internal_find_stdenum(hdw);
1773 return; 1559 return;
1774 } 1560 }
1561 if (std3) {
1562 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1563 pvr2_trace(PVR2_TRACE_STD,
1564 "Initial video standard"
1565 " (determined by device type): %.*s",bcnt,buf);
1566 hdw->std_mask_cur = std3;
1567 hdw->std_dirty = !0;
1568 pvr2_hdw_internal_find_stdenum(hdw);
1569 return;
1570 }
1775 1571
1776 { 1572 {
1777 unsigned int idx; 1573 unsigned int idx;
@@ -1816,8 +1612,7 @@ static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1816 unsigned int idx; 1612 unsigned int idx;
1817 struct pvr2_ctrl *cptr; 1613 struct pvr2_ctrl *cptr;
1818 int reloadFl = 0; 1614 int reloadFl = 0;
1819 if ((hdw->hdw_type == PVR2_HDW_TYPE_29XXX) || 1615 if (hdw->hdw_desc->fx2_firmware.cnt) {
1820 (hdw->hdw_type == PVR2_HDW_TYPE_24XXX)) {
1821 if (!reloadFl) { 1616 if (!reloadFl) {
1822 reloadFl = 1617 reloadFl =
1823 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints 1618 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
@@ -1853,25 +1648,13 @@ static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1853 } 1648 }
1854 if (!pvr2_hdw_dev_ok(hdw)) return; 1649 if (!pvr2_hdw_dev_ok(hdw)) return;
1855 1650
1856 if (hdw->hdw_type < ARRAY_SIZE(pvr2_client_lists)) { 1651 for (idx = 0; idx < hdw->hdw_desc->client_modules.cnt; idx++) {
1857 for (idx = 0; 1652 request_module(hdw->hdw_desc->client_modules.lst[idx]);
1858 idx < pvr2_client_lists[hdw->hdw_type].cnt;
1859 idx++) {
1860 request_module(
1861 pvr2_client_lists[hdw->hdw_type].lst[idx]);
1862 }
1863 } 1653 }
1864 1654
1865 if ((hdw->hdw_type == PVR2_HDW_TYPE_29XXX) || 1655 if (!hdw->hdw_desc->flag_no_powerup) {
1866 (hdw->hdw_type == PVR2_HDW_TYPE_24XXX)) {
1867 pvr2_hdw_cmd_powerup(hdw); 1656 pvr2_hdw_cmd_powerup(hdw);
1868 if (!pvr2_hdw_dev_ok(hdw)) return; 1657 if (!pvr2_hdw_dev_ok(hdw)) return;
1869
1870 if (pvr2_upload_firmware2(hdw)){
1871 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"device unstable!!");
1872 pvr2_hdw_render_useless(hdw);
1873 return;
1874 }
1875 } 1658 }
1876 1659
1877 // This step MUST happen after the earlier powerup step. 1660 // This step MUST happen after the earlier powerup step.
@@ -1899,15 +1682,22 @@ static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1899 // thread-safe against the normal pvr2_send_request() mechanism. 1682 // thread-safe against the normal pvr2_send_request() mechanism.
1900 // (We should make it thread safe). 1683 // (We should make it thread safe).
1901 1684
1902 ret = pvr2_hdw_get_eeprom_addr(hdw); 1685 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
1903 if (!pvr2_hdw_dev_ok(hdw)) return; 1686 ret = pvr2_hdw_get_eeprom_addr(hdw);
1904 if (ret < 0) {
1905 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1906 "Unable to determine location of eeprom, skipping");
1907 } else {
1908 hdw->eeprom_addr = ret;
1909 pvr2_eeprom_analyze(hdw);
1910 if (!pvr2_hdw_dev_ok(hdw)) return; 1687 if (!pvr2_hdw_dev_ok(hdw)) return;
1688 if (ret < 0) {
1689 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1690 "Unable to determine location of eeprom,"
1691 " skipping");
1692 } else {
1693 hdw->eeprom_addr = ret;
1694 pvr2_eeprom_analyze(hdw);
1695 if (!pvr2_hdw_dev_ok(hdw)) return;
1696 }
1697 } else {
1698 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
1699 hdw->tuner_updated = !0;
1700 hdw->std_mask_eeprom = V4L2_STD_ALL;
1911 } 1701 }
1912 1702
1913 pvr2_hdw_setup_std(hdw); 1703 pvr2_hdw_setup_std(hdw);
@@ -1918,14 +1708,12 @@ static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1918 hdw->tuner_type); 1708 hdw->tuner_type);
1919 } 1709 }
1920 1710
1921 hdw->tuner_updated = !0;
1922 pvr2_i2c_core_check_stale(hdw); 1711 pvr2_i2c_core_check_stale(hdw);
1923 hdw->tuner_updated = 0; 1712 hdw->tuner_updated = 0;
1924 1713
1925 if (!pvr2_hdw_dev_ok(hdw)) return; 1714 if (!pvr2_hdw_dev_ok(hdw)) return;
1926 1715
1927 pvr2_hdw_commit_ctl_internal(hdw); 1716 pvr2_hdw_commit_setup(hdw);
1928 if (!pvr2_hdw_dev_ok(hdw)) return;
1929 1717
1930 hdw->vid_stream = pvr2_stream_create(); 1718 hdw->vid_stream = pvr2_stream_create();
1931 if (!pvr2_hdw_dev_ok(hdw)) return; 1719 if (!pvr2_hdw_dev_ok(hdw)) return;
@@ -1945,25 +1733,25 @@ static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1945 1733
1946 if (!pvr2_hdw_dev_ok(hdw)) return; 1734 if (!pvr2_hdw_dev_ok(hdw)) return;
1947 1735
1948 /* Make sure everything is up to date */
1949 pvr2_i2c_core_sync(hdw);
1950
1951 if (!pvr2_hdw_dev_ok(hdw)) return;
1952
1953 hdw->flag_init_ok = !0; 1736 hdw->flag_init_ok = !0;
1737
1738 pvr2_hdw_state_sched(hdw);
1954} 1739}
1955 1740
1956 1741
1957int pvr2_hdw_setup(struct pvr2_hdw *hdw) 1742/* Set up the structure and attempt to put the device into a usable state.
1743 This can be a time-consuming operation, which is why it is not done
1744 internally as part of the create() step. */
1745static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
1958{ 1746{
1959 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw); 1747 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
1960 LOCK_TAKE(hdw->big_lock); do { 1748 do {
1961 pvr2_hdw_setup_low(hdw); 1749 pvr2_hdw_setup_low(hdw);
1962 pvr2_trace(PVR2_TRACE_INIT, 1750 pvr2_trace(PVR2_TRACE_INIT,
1963 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d", 1751 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
1964 hdw,hdw->flag_ok,hdw->flag_init_ok); 1752 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
1965 if (pvr2_hdw_dev_ok(hdw)) { 1753 if (pvr2_hdw_dev_ok(hdw)) {
1966 if (pvr2_hdw_init_ok(hdw)) { 1754 if (hdw->flag_init_ok) {
1967 pvr2_trace( 1755 pvr2_trace(
1968 PVR2_TRACE_INFO, 1756 PVR2_TRACE_INFO,
1969 "Device initialization" 1757 "Device initialization"
@@ -2013,9 +1801,8 @@ int pvr2_hdw_setup(struct pvr2_hdw *hdw)
2013 " the pvrusb2 device" 1801 " the pvrusb2 device"
2014 " in order to recover."); 1802 " in order to recover.");
2015 } 1803 }
2016 } while (0); LOCK_GIVE(hdw->big_lock); 1804 } while (0);
2017 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw); 1805 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
2018 return hdw->flag_init_ok;
2019} 1806}
2020 1807
2021 1808
@@ -2026,24 +1813,32 @@ struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2026{ 1813{
2027 unsigned int idx,cnt1,cnt2; 1814 unsigned int idx,cnt1,cnt2;
2028 struct pvr2_hdw *hdw; 1815 struct pvr2_hdw *hdw;
2029 unsigned int hdw_type;
2030 int valid_std_mask; 1816 int valid_std_mask;
2031 struct pvr2_ctrl *cptr; 1817 struct pvr2_ctrl *cptr;
1818 const struct pvr2_device_desc *hdw_desc;
2032 __u8 ifnum; 1819 __u8 ifnum;
2033 struct v4l2_queryctrl qctrl; 1820 struct v4l2_queryctrl qctrl;
2034 struct pvr2_ctl_info *ciptr; 1821 struct pvr2_ctl_info *ciptr;
2035 1822
2036 hdw_type = devid - pvr2_device_table; 1823 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
2037 if (hdw_type >= ARRAY_SIZE(pvr2_device_names)) {
2038 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2039 "Bogus device type of %u reported",hdw_type);
2040 return NULL;
2041 }
2042 1824
2043 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL); 1825 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
2044 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"", 1826 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
2045 hdw,pvr2_device_names[hdw_type]); 1827 hdw,hdw_desc->description);
2046 if (!hdw) goto fail; 1828 if (!hdw) goto fail;
1829
1830 init_timer(&hdw->quiescent_timer);
1831 hdw->quiescent_timer.data = (unsigned long)hdw;
1832 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
1833
1834 init_timer(&hdw->encoder_wait_timer);
1835 hdw->encoder_wait_timer.data = (unsigned long)hdw;
1836 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
1837
1838 hdw->master_state = PVR2_STATE_DEAD;
1839
1840 init_waitqueue_head(&hdw->state_wait_data);
1841
2047 hdw->tuner_signal_stale = !0; 1842 hdw->tuner_signal_stale = !0;
2048 cx2341x_fill_defaults(&hdw->enc_ctl_state); 1843 cx2341x_fill_defaults(&hdw->enc_ctl_state);
2049 1844
@@ -2052,7 +1847,7 @@ struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2052 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt, 1847 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
2053 GFP_KERNEL); 1848 GFP_KERNEL);
2054 if (!hdw->controls) goto fail; 1849 if (!hdw->controls) goto fail;
2055 hdw->hdw_type = hdw_type; 1850 hdw->hdw_desc = hdw_desc;
2056 for (idx = 0; idx < hdw->control_cnt; idx++) { 1851 for (idx = 0; idx < hdw->control_cnt; idx++) {
2057 cptr = hdw->controls + idx; 1852 cptr = hdw->controls + idx;
2058 cptr->hdw = hdw; 1853 cptr->hdw = hdw;
@@ -2184,18 +1979,16 @@ struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2184 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1; 1979 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2185 hdw->name[cnt1] = 0; 1980 hdw->name[cnt1] = 0;
2186 1981
1982 hdw->workqueue = create_singlethread_workqueue(hdw->name);
1983 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
1984 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
1985 INIT_WORK(&hdw->workinit,pvr2_hdw_worker_init);
1986
2187 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s", 1987 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2188 hdw->unit_number,hdw->name); 1988 hdw->unit_number,hdw->name);
2189 1989
2190 hdw->tuner_type = -1; 1990 hdw->tuner_type = -1;
2191 hdw->flag_ok = !0; 1991 hdw->flag_ok = !0;
2192 /* Initialize the mask of subsystems that we will shut down when we
2193 stop streaming. */
2194 hdw->subsys_stream_mask = PVR2_SUBSYS_RUN_ALL;
2195 hdw->subsys_stream_mask |= (1<<PVR2_SUBSYS_B_ENC_CFG);
2196
2197 pvr2_trace(PVR2_TRACE_INIT,"subsys_stream_mask: 0x%lx",
2198 hdw->subsys_stream_mask);
2199 1992
2200 hdw->usb_intf = intf; 1993 hdw->usb_intf = intf;
2201 hdw->usb_dev = interface_to_usbdev(intf); 1994 hdw->usb_dev = interface_to_usbdev(intf);
@@ -2211,15 +2004,25 @@ struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2211 mutex_init(&hdw->ctl_lock_mutex); 2004 mutex_init(&hdw->ctl_lock_mutex);
2212 mutex_init(&hdw->big_lock_mutex); 2005 mutex_init(&hdw->big_lock_mutex);
2213 2006
2007 queue_work(hdw->workqueue,&hdw->workinit);
2214 return hdw; 2008 return hdw;
2215 fail: 2009 fail:
2216 if (hdw) { 2010 if (hdw) {
2011 del_timer_sync(&hdw->quiescent_timer);
2012 del_timer_sync(&hdw->encoder_wait_timer);
2013 if (hdw->workqueue) {
2014 flush_workqueue(hdw->workqueue);
2015 destroy_workqueue(hdw->workqueue);
2016 hdw->workqueue = NULL;
2017 }
2217 usb_free_urb(hdw->ctl_read_urb); 2018 usb_free_urb(hdw->ctl_read_urb);
2218 usb_free_urb(hdw->ctl_write_urb); 2019 usb_free_urb(hdw->ctl_write_urb);
2219 kfree(hdw->ctl_read_buffer); 2020 kfree(hdw->ctl_read_buffer);
2220 kfree(hdw->ctl_write_buffer); 2021 kfree(hdw->ctl_write_buffer);
2221 kfree(hdw->controls); 2022 kfree(hdw->controls);
2222 kfree(hdw->mpeg_ctrl_info); 2023 kfree(hdw->mpeg_ctrl_info);
2024 kfree(hdw->std_defs);
2025 kfree(hdw->std_enum_names);
2223 kfree(hdw); 2026 kfree(hdw);
2224 } 2027 }
2225 return NULL; 2028 return NULL;
@@ -2250,10 +2053,10 @@ static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
2250 kfree(hdw->ctl_write_buffer); 2053 kfree(hdw->ctl_write_buffer);
2251 hdw->ctl_write_buffer = NULL; 2054 hdw->ctl_write_buffer = NULL;
2252 } 2055 }
2253 pvr2_hdw_render_useless_unlocked(hdw);
2254 hdw->flag_disconnected = !0; 2056 hdw->flag_disconnected = !0;
2255 hdw->usb_dev = NULL; 2057 hdw->usb_dev = NULL;
2256 hdw->usb_intf = NULL; 2058 hdw->usb_intf = NULL;
2059 pvr2_hdw_render_useless(hdw);
2257} 2060}
2258 2061
2259 2062
@@ -2262,6 +2065,13 @@ void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2262{ 2065{
2263 if (!hdw) return; 2066 if (!hdw) return;
2264 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw); 2067 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
2068 del_timer_sync(&hdw->quiescent_timer);
2069 del_timer_sync(&hdw->encoder_wait_timer);
2070 if (hdw->workqueue) {
2071 flush_workqueue(hdw->workqueue);
2072 destroy_workqueue(hdw->workqueue);
2073 hdw->workqueue = NULL;
2074 }
2265 if (hdw->fw_buffer) { 2075 if (hdw->fw_buffer) {
2266 kfree(hdw->fw_buffer); 2076 kfree(hdw->fw_buffer);
2267 hdw->fw_buffer = NULL; 2077 hdw->fw_buffer = NULL;
@@ -2290,12 +2100,6 @@ void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2290} 2100}
2291 2101
2292 2102
2293int pvr2_hdw_init_ok(struct pvr2_hdw *hdw)
2294{
2295 return hdw->flag_init_ok;
2296}
2297
2298
2299int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw) 2103int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2300{ 2104{
2301 return (hdw && hdw->flag_ok); 2105 return (hdw && hdw->flag_ok);
@@ -2473,17 +2277,11 @@ static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2473} 2277}
2474 2278
2475 2279
2476/* Commit all control changes made up to this point. Subsystems can be 2280/* Figure out if we need to commit control changes. If so, mark internal
2477 indirectly affected by these changes. For a given set of things being 2281 state flags to indicate this fact and return true. Otherwise do nothing
2478 committed, we'll clear the affected subsystem bits and then once we're 2282 else and return false. */
2479 done committing everything we'll make a request to restore the subsystem 2283static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
2480 state(s) back to their previous value before this function was called.
2481 Thus we can automatically reconfigure affected pieces of the driver as
2482 controls are changed. */
2483static int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw)
2484{ 2284{
2485 unsigned long saved_subsys_mask = hdw->subsys_enabled_mask;
2486 unsigned long stale_subsys_mask = 0;
2487 unsigned int idx; 2285 unsigned int idx;
2488 struct pvr2_ctrl *cptr; 2286 struct pvr2_ctrl *cptr;
2489 int value; 2287 int value;
@@ -2518,6 +2316,25 @@ static int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw)
2518 return 0; 2316 return 0;
2519 } 2317 }
2520 2318
2319 hdw->state_pipeline_config = 0;
2320 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2321 pvr2_hdw_state_sched(hdw);
2322
2323 return !0;
2324}
2325
2326
2327/* Perform all operations needed to commit all control changes. This must
2328 be performed in synchronization with the pipeline state and is thus
2329 expected to be called as part of the driver's worker thread. Return
2330 true if commit successful, otherwise return false to indicate that
2331 commit isn't possible at this time. */
2332static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
2333{
2334 unsigned int idx;
2335 struct pvr2_ctrl *cptr;
2336 int disruptive_change;
2337
2521 /* When video standard changes, reset the hres and vres values - 2338 /* When video standard changes, reset the hres and vres values -
2522 but if the user has pending changes there, then let the changes 2339 but if the user has pending changes there, then let the changes
2523 take priority. */ 2340 take priority. */
@@ -2536,24 +2353,26 @@ static int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw)
2536 } 2353 }
2537 } 2354 }
2538 2355
2539 if (hdw->std_dirty || 2356 /* If any of the below has changed, then we can't do the update
2540 hdw->enc_stale || 2357 while the pipeline is running. Pipeline must be paused first
2541 hdw->srate_dirty || 2358 and decoder -> encoder connection be made quiescent before we
2542 hdw->res_ver_dirty || 2359 can proceed. */
2543 hdw->res_hor_dirty || 2360 disruptive_change =
2544 0) { 2361 (hdw->std_dirty ||
2545 /* If any of this changes, then the encoder needs to be 2362 hdw->enc_unsafe_stale ||
2546 reconfigured, and we need to reset the stream. */ 2363 hdw->srate_dirty ||
2547 stale_subsys_mask |= (1<<PVR2_SUBSYS_B_ENC_CFG); 2364 hdw->res_ver_dirty ||
2548 } 2365 hdw->res_hor_dirty ||
2549 2366 hdw->input_dirty ||
2550 if (hdw->input_dirty) { 2367 (hdw->active_stream_type != hdw->desired_stream_type));
2551 /* pk: If input changes to or from radio, then the encoder 2368 if (disruptive_change && !hdw->state_pipeline_idle) {
2552 needs to be restarted (for ENC_MUTE_VIDEO to work) */ 2369 /* Pipeline is not idle; we can't proceed. Arrange to
2553 stale_subsys_mask |= (1<<PVR2_SUBSYS_B_ENC_RUN); 2370 cause pipeline to stop so that we can try this again
2371 later.... */
2372 hdw->state_pipeline_pause = !0;
2373 return 0;
2554 } 2374 }
2555 2375
2556
2557 if (hdw->srate_dirty) { 2376 if (hdw->srate_dirty) {
2558 /* Write new sample rate into control structure since 2377 /* Write new sample rate into control structure since
2559 * the master copy is stale. We must track srate 2378 * the master copy is stale. We must track srate
@@ -2582,51 +2401,88 @@ static int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw)
2582 cptr->info->clear_dirty(cptr); 2401 cptr->info->clear_dirty(cptr);
2583 } 2402 }
2584 2403
2404 if (hdw->active_stream_type != hdw->desired_stream_type) {
2405 /* Handle any side effects of stream config here */
2406 hdw->active_stream_type = hdw->desired_stream_type;
2407 }
2408
2585 /* Now execute i2c core update */ 2409 /* Now execute i2c core update */
2586 pvr2_i2c_core_sync(hdw); 2410 pvr2_i2c_core_sync(hdw);
2587 2411
2588 pvr2_hdw_subsys_bit_chg_no_lock(hdw,stale_subsys_mask,0); 2412 if (hdw->state_encoder_run) {
2589 pvr2_hdw_subsys_bit_chg_no_lock(hdw,~0,saved_subsys_mask); 2413 /* If encoder isn't running, then this will get worked out
2414 later when we start the encoder. */
2415 if (pvr2_encoder_adjust(hdw) < 0) return !0;
2416 }
2590 2417
2591 return 0; 2418 hdw->state_pipeline_config = !0;
2419 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2420 return !0;
2592} 2421}
2593 2422
2594 2423
2595int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw) 2424int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
2596{ 2425{
2426 int fl;
2427 LOCK_TAKE(hdw->big_lock);
2428 fl = pvr2_hdw_commit_setup(hdw);
2429 LOCK_GIVE(hdw->big_lock);
2430 if (!fl) return 0;
2431 return pvr2_hdw_wait(hdw,0);
2432}
2433
2434
2435static void pvr2_hdw_worker_i2c(struct work_struct *work)
2436{
2437 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
2597 LOCK_TAKE(hdw->big_lock); do { 2438 LOCK_TAKE(hdw->big_lock); do {
2598 pvr2_hdw_commit_ctl_internal(hdw); 2439 pvr2_i2c_core_sync(hdw);
2599 } while (0); LOCK_GIVE(hdw->big_lock); 2440 } while (0); LOCK_GIVE(hdw->big_lock);
2600 return 0;
2601} 2441}
2602 2442
2603 2443
2604void pvr2_hdw_poll(struct pvr2_hdw *hdw) 2444static void pvr2_hdw_worker_poll(struct work_struct *work)
2605{ 2445{
2446 int fl = 0;
2447 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
2606 LOCK_TAKE(hdw->big_lock); do { 2448 LOCK_TAKE(hdw->big_lock); do {
2607 pvr2_i2c_core_sync(hdw); 2449 fl = pvr2_hdw_state_eval(hdw);
2608 } while (0); LOCK_GIVE(hdw->big_lock); 2450 } while (0); LOCK_GIVE(hdw->big_lock);
2451 if (fl && hdw->state_func) {
2452 hdw->state_func(hdw->state_data);
2453 }
2609} 2454}
2610 2455
2611 2456
2612void pvr2_hdw_setup_poll_trigger(struct pvr2_hdw *hdw, 2457static void pvr2_hdw_worker_init(struct work_struct *work)
2613 void (*func)(void *),
2614 void *data)
2615{ 2458{
2459 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workinit);
2616 LOCK_TAKE(hdw->big_lock); do { 2460 LOCK_TAKE(hdw->big_lock); do {
2617 hdw->poll_trigger_func = func; 2461 pvr2_hdw_setup(hdw);
2618 hdw->poll_trigger_data = data;
2619 } while (0); LOCK_GIVE(hdw->big_lock); 2462 } while (0); LOCK_GIVE(hdw->big_lock);
2620} 2463}
2621 2464
2622 2465
2623void pvr2_hdw_poll_trigger_unlocked(struct pvr2_hdw *hdw) 2466static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
2624{ 2467{
2625 if (hdw->poll_trigger_func) { 2468 return wait_event_interruptible(
2626 hdw->poll_trigger_func(hdw->poll_trigger_data); 2469 hdw->state_wait_data,
2627 } 2470 (hdw->state_stale == 0) &&
2471 (!state || (hdw->master_state != state)));
2628} 2472}
2629 2473
2474
2475void pvr2_hdw_set_state_callback(struct pvr2_hdw *hdw,
2476 void (*callback_func)(void *),
2477 void *callback_data)
2478{
2479 LOCK_TAKE(hdw->big_lock); do {
2480 hdw->state_data = callback_data;
2481 hdw->state_func = callback_func;
2482 } while (0); LOCK_GIVE(hdw->big_lock);
2483}
2484
2485
2630/* Return name for this driver instance */ 2486/* Return name for this driver instance */
2631const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw) 2487const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
2632{ 2488{
@@ -2634,6 +2490,18 @@ const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
2634} 2490}
2635 2491
2636 2492
2493const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
2494{
2495 return hdw->hdw_desc->description;
2496}
2497
2498
2499const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
2500{
2501 return hdw->hdw_desc->shortname;
2502}
2503
2504
2637int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw) 2505int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
2638{ 2506{
2639 int result; 2507 int result;
@@ -2689,6 +2557,7 @@ void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
2689 pvr2_i2c_core_sync(hdw); 2557 pvr2_i2c_core_sync(hdw);
2690 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:"); 2558 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
2691 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2"); 2559 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
2560 pvr2_hdw_state_log_state(hdw);
2692 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr); 2561 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
2693 } while (0); LOCK_GIVE(hdw->big_lock); 2562 } while (0); LOCK_GIVE(hdw->big_lock);
2694} 2563}
@@ -2959,7 +2828,7 @@ static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
2959 " without lock!!"); 2828 " without lock!!");
2960 return -EDEADLK; 2829 return -EDEADLK;
2961 } 2830 }
2962 if ((!hdw->flag_ok) && !probe_fl) { 2831 if (!hdw->flag_ok && !probe_fl) {
2963 pvr2_trace(PVR2_TRACE_ERROR_LEGS, 2832 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2964 "Attempted to execute control transfer" 2833 "Attempted to execute control transfer"
2965 " when device not ok"); 2834 " when device not ok");
@@ -3167,7 +3036,7 @@ static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
3167 3036
3168 hdw->cmd_debug_state = 0; 3037 hdw->cmd_debug_state = 0;
3169 if ((status < 0) && (!probe_fl)) { 3038 if ((status < 0) && (!probe_fl)) {
3170 pvr2_hdw_render_useless_unlocked(hdw); 3039 pvr2_hdw_render_useless(hdw);
3171 } 3040 }
3172 return status; 3041 return status;
3173} 3042}
@@ -3227,24 +3096,17 @@ static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
3227} 3096}
3228 3097
3229 3098
3230static void pvr2_hdw_render_useless_unlocked(struct pvr2_hdw *hdw) 3099void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
3231{ 3100{
3232 if (!hdw->flag_ok) return; 3101 if (!hdw->flag_ok) return;
3233 pvr2_trace(PVR2_TRACE_INIT,"render_useless"); 3102 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3234 hdw->flag_ok = 0; 3103 "Device being rendered inoperable");
3235 if (hdw->vid_stream) { 3104 if (hdw->vid_stream) {
3236 pvr2_stream_setup(hdw->vid_stream,NULL,0,0); 3105 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
3237 } 3106 }
3238 hdw->flag_streaming_enabled = 0; 3107 hdw->flag_ok = 0;
3239 hdw->subsys_enabled_mask = 0; 3108 trace_stbit("flag_ok",hdw->flag_ok);
3240} 3109 pvr2_hdw_state_sched(hdw);
3241
3242
3243void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
3244{
3245 LOCK_TAKE(hdw->ctl_lock);
3246 pvr2_hdw_render_useless_unlocked(hdw);
3247 LOCK_GIVE(hdw->ctl_lock);
3248} 3110}
3249 3111
3250 3112
@@ -3299,7 +3161,6 @@ int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
3299 int status; 3161 int status;
3300 LOCK_TAKE(hdw->ctl_lock); do { 3162 LOCK_TAKE(hdw->ctl_lock); do {
3301 pvr2_trace(PVR2_TRACE_INIT,"Requesting uproc hard reset"); 3163 pvr2_trace(PVR2_TRACE_INIT,"Requesting uproc hard reset");
3302 hdw->flag_ok = !0;
3303 hdw->cmd_buffer[0] = FX2CMD_DEEP_RESET; 3164 hdw->cmd_buffer[0] = FX2CMD_DEEP_RESET;
3304 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0); 3165 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
3305 } while (0); LOCK_GIVE(hdw->ctl_lock); 3166 } while (0); LOCK_GIVE(hdw->ctl_lock);
@@ -3349,26 +3210,473 @@ static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
3349 (runFl ? FX2CMD_STREAMING_ON : FX2CMD_STREAMING_OFF); 3210 (runFl ? FX2CMD_STREAMING_ON : FX2CMD_STREAMING_OFF);
3350 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0); 3211 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
3351 } while (0); LOCK_GIVE(hdw->ctl_lock); 3212 } while (0); LOCK_GIVE(hdw->ctl_lock);
3352 if (!status) {
3353 hdw->subsys_enabled_mask =
3354 ((hdw->subsys_enabled_mask &
3355 ~(1<<PVR2_SUBSYS_B_USBSTREAM_RUN)) |
3356 (runFl ? (1<<PVR2_SUBSYS_B_USBSTREAM_RUN) : 0));
3357 }
3358 return status; 3213 return status;
3359} 3214}
3360 3215
3361 3216
3362void pvr2_hdw_get_debug_info(const struct pvr2_hdw *hdw, 3217/* Evaluate whether or not state_encoder_ok can change */
3363 struct pvr2_hdw_debug_info *ptr) 3218static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
3219{
3220 if (hdw->state_encoder_ok) return 0;
3221 if (hdw->flag_tripped) return 0;
3222 if (hdw->state_encoder_run) return 0;
3223 if (hdw->state_encoder_config) return 0;
3224 if (hdw->state_decoder_run) return 0;
3225 if (hdw->state_usbstream_run) return 0;
3226 if (pvr2_upload_firmware2(hdw) < 0) {
3227 hdw->flag_tripped = !0;
3228 trace_stbit("flag_tripped",hdw->flag_tripped);
3229 return !0;
3230 }
3231 hdw->state_encoder_ok = !0;
3232 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
3233 return !0;
3234}
3235
3236
3237/* Evaluate whether or not state_encoder_config can change */
3238static int state_eval_encoder_config(struct pvr2_hdw *hdw)
3239{
3240 if (hdw->state_encoder_config) {
3241 if (hdw->state_encoder_ok) {
3242 if (hdw->state_pipeline_req &&
3243 !hdw->state_pipeline_pause) return 0;
3244 }
3245 hdw->state_encoder_config = 0;
3246 hdw->state_encoder_waitok = 0;
3247 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3248 /* paranoia - solve race if timer just completed */
3249 del_timer_sync(&hdw->encoder_wait_timer);
3250 } else {
3251 if (!hdw->state_encoder_ok ||
3252 !hdw->state_pipeline_idle ||
3253 hdw->state_pipeline_pause ||
3254 !hdw->state_pipeline_req ||
3255 !hdw->state_pipeline_config) {
3256 /* We must reset the enforced wait interval if
3257 anything has happened that might have disturbed
3258 the encoder. This should be a rare case. */
3259 if (timer_pending(&hdw->encoder_wait_timer)) {
3260 del_timer_sync(&hdw->encoder_wait_timer);
3261 }
3262 if (hdw->state_encoder_waitok) {
3263 /* Must clear the state - therefore we did
3264 something to a state bit and must also
3265 return true. */
3266 hdw->state_encoder_waitok = 0;
3267 trace_stbit("state_encoder_waitok",
3268 hdw->state_encoder_waitok);
3269 return !0;
3270 }
3271 return 0;
3272 }
3273 if (!hdw->state_encoder_waitok) {
3274 if (!timer_pending(&hdw->encoder_wait_timer)) {
3275 /* waitok flag wasn't set and timer isn't
3276 running. Check flag once more to avoid
3277 a race then start the timer. This is
3278 the point when we measure out a minimal
3279 quiet interval before doing something to
3280 the encoder. */
3281 if (!hdw->state_encoder_waitok) {
3282 hdw->encoder_wait_timer.expires =
3283 jiffies + (HZ*50/1000);
3284 add_timer(&hdw->encoder_wait_timer);
3285 }
3286 }
3287 /* We can't continue until we know we have been
3288 quiet for the interval measured by this
3289 timer. */
3290 return 0;
3291 }
3292 pvr2_encoder_configure(hdw);
3293 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
3294 }
3295 trace_stbit("state_encoder_config",hdw->state_encoder_config);
3296 return !0;
3297}
3298
3299
3300/* Evaluate whether or not state_encoder_run can change */
3301static int state_eval_encoder_run(struct pvr2_hdw *hdw)
3302{
3303 if (hdw->state_encoder_run) {
3304 if (hdw->state_encoder_ok) {
3305 if (hdw->state_decoder_run) return 0;
3306 if (pvr2_encoder_stop(hdw) < 0) return !0;
3307 }
3308 hdw->state_encoder_run = 0;
3309 } else {
3310 if (!hdw->state_encoder_ok) return 0;
3311 if (!hdw->state_decoder_run) return 0;
3312 if (pvr2_encoder_start(hdw) < 0) return !0;
3313 hdw->state_encoder_run = !0;
3314 }
3315 trace_stbit("state_encoder_run",hdw->state_encoder_run);
3316 return !0;
3317}
3318
3319
3320/* Timeout function for quiescent timer. */
3321static void pvr2_hdw_quiescent_timeout(unsigned long data)
3322{
3323 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3324 hdw->state_decoder_quiescent = !0;
3325 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3326 hdw->state_stale = !0;
3327 queue_work(hdw->workqueue,&hdw->workpoll);
3328}
3329
3330
3331/* Timeout function for encoder wait timer. */
3332static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
3333{
3334 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3335 hdw->state_encoder_waitok = !0;
3336 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3337 hdw->state_stale = !0;
3338 queue_work(hdw->workqueue,&hdw->workpoll);
3339}
3340
3341
3342/* Evaluate whether or not state_decoder_run can change */
3343static int state_eval_decoder_run(struct pvr2_hdw *hdw)
3344{
3345 if (hdw->state_decoder_run) {
3346 if (hdw->state_encoder_ok) {
3347 if (hdw->state_pipeline_req &&
3348 !hdw->state_pipeline_pause) return 0;
3349 }
3350 if (!hdw->flag_decoder_missed) {
3351 pvr2_decoder_enable(hdw,0);
3352 }
3353 hdw->state_decoder_quiescent = 0;
3354 hdw->state_decoder_run = 0;
3355 /* paranoia - solve race if timer just completed */
3356 del_timer_sync(&hdw->quiescent_timer);
3357 } else {
3358 if (!hdw->state_decoder_quiescent) {
3359 if (!timer_pending(&hdw->quiescent_timer)) {
3360 /* We don't do something about the
3361 quiescent timer until right here because
3362 we also want to catch cases where the
3363 decoder was already not running (like
3364 after initialization) as opposed to
3365 knowing that we had just stopped it.
3366 The second flag check is here to cover a
3367 race - the timer could have run and set
3368 this flag just after the previous check
3369 but before we did the pending check. */
3370 if (!hdw->state_decoder_quiescent) {
3371 hdw->quiescent_timer.expires =
3372 jiffies + (HZ*50/1000);
3373 add_timer(&hdw->quiescent_timer);
3374 }
3375 }
3376 /* Don't allow decoder to start again until it has
3377 been quiesced first. This little detail should
3378 hopefully further stabilize the encoder. */
3379 return 0;
3380 }
3381 if (!hdw->state_pipeline_req ||
3382 hdw->state_pipeline_pause ||
3383 !hdw->state_pipeline_config ||
3384 !hdw->state_encoder_config ||
3385 !hdw->state_encoder_ok) return 0;
3386 del_timer_sync(&hdw->quiescent_timer);
3387 if (hdw->flag_decoder_missed) return 0;
3388 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
3389 hdw->state_decoder_quiescent = 0;
3390 hdw->state_decoder_run = !0;
3391 }
3392 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3393 trace_stbit("state_decoder_run",hdw->state_decoder_run);
3394 return !0;
3395}
3396
3397
3398/* Evaluate whether or not state_usbstream_run can change */
3399static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
3400{
3401 if (hdw->state_usbstream_run) {
3402 if (hdw->state_encoder_ok) {
3403 if (hdw->state_encoder_run) return 0;
3404 }
3405 pvr2_hdw_cmd_usbstream(hdw,0);
3406 hdw->state_usbstream_run = 0;
3407 } else {
3408 if (!hdw->state_encoder_ok ||
3409 !hdw->state_encoder_run ||
3410 !hdw->state_pipeline_req ||
3411 hdw->state_pipeline_pause) return 0;
3412 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
3413 hdw->state_usbstream_run = !0;
3414 }
3415 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
3416 return !0;
3417}
3418
3419
3420/* Attempt to configure pipeline, if needed */
3421static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
3422{
3423 if (hdw->state_pipeline_config ||
3424 hdw->state_pipeline_pause) return 0;
3425 pvr2_hdw_commit_execute(hdw);
3426 return !0;
3427}
3428
3429
3430/* Update pipeline idle and pipeline pause tracking states based on other
3431 inputs. This must be called whenever the other relevant inputs have
3432 changed. */
3433static int state_update_pipeline_state(struct pvr2_hdw *hdw)
3434{
3435 unsigned int st;
3436 int updatedFl = 0;
3437 /* Update pipeline state */
3438 st = !(hdw->state_encoder_run ||
3439 hdw->state_decoder_run ||
3440 hdw->state_usbstream_run ||
3441 (!hdw->state_decoder_quiescent));
3442 if (!st != !hdw->state_pipeline_idle) {
3443 hdw->state_pipeline_idle = st;
3444 updatedFl = !0;
3445 }
3446 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
3447 hdw->state_pipeline_pause = 0;
3448 updatedFl = !0;
3449 }
3450 return updatedFl;
3451}
3452
3453
3454typedef int (*state_eval_func)(struct pvr2_hdw *);
3455
3456/* Set of functions to be run to evaluate various states in the driver. */
3457const static state_eval_func eval_funcs[] = {
3458 state_eval_pipeline_config,
3459 state_eval_encoder_ok,
3460 state_eval_encoder_config,
3461 state_eval_decoder_run,
3462 state_eval_encoder_run,
3463 state_eval_usbstream_run,
3464};
3465
3466
3467/* Process various states and return true if we did anything interesting. */
3468static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
3469{
3470 unsigned int i;
3471 int state_updated = 0;
3472 int check_flag;
3473
3474 if (!hdw->state_stale) return 0;
3475 if ((hdw->fw1_state != FW1_STATE_OK) ||
3476 !hdw->flag_ok) {
3477 hdw->state_stale = 0;
3478 return !0;
3479 }
3480 /* This loop is the heart of the entire driver. It keeps trying to
3481 evaluate various bits of driver state until nothing changes for
3482 one full iteration. Each "bit of state" tracks some global
3483 aspect of the driver, e.g. whether decoder should run, if
3484 pipeline is configured, usb streaming is on, etc. We separately
3485 evaluate each of those questions based on other driver state to
3486 arrive at the correct running configuration. */
3487 do {
3488 check_flag = 0;
3489 state_update_pipeline_state(hdw);
3490 /* Iterate over each bit of state */
3491 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
3492 if ((*eval_funcs[i])(hdw)) {
3493 check_flag = !0;
3494 state_updated = !0;
3495 state_update_pipeline_state(hdw);
3496 }
3497 }
3498 } while (check_flag && hdw->flag_ok);
3499 hdw->state_stale = 0;
3500 trace_stbit("state_stale",hdw->state_stale);
3501 return state_updated;
3502}
3503
3504
3505static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
3506 char *buf,unsigned int acnt)
3507{
3508 switch (which) {
3509 case 0:
3510 return scnprintf(
3511 buf,acnt,
3512 "driver:%s%s%s%s%s",
3513 (hdw->flag_ok ? " <ok>" : " <fail>"),
3514 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
3515 (hdw->flag_disconnected ? " <disconnected>" :
3516 " <connected>"),
3517 (hdw->flag_tripped ? " <tripped>" : ""),
3518 (hdw->flag_decoder_missed ? " <no decoder>" : ""));
3519 case 1:
3520 return scnprintf(
3521 buf,acnt,
3522 "pipeline:%s%s%s%s",
3523 (hdw->state_pipeline_idle ? " <idle>" : ""),
3524 (hdw->state_pipeline_config ?
3525 " <configok>" : " <stale>"),
3526 (hdw->state_pipeline_req ? " <req>" : ""),
3527 (hdw->state_pipeline_pause ? " <pause>" : ""));
3528 case 2:
3529 return scnprintf(
3530 buf,acnt,
3531 "worker:%s%s%s%s%s%s",
3532 (hdw->state_decoder_run ?
3533 " <decode:run>" :
3534 (hdw->state_decoder_quiescent ?
3535 "" : " <decode:stop>")),
3536 (hdw->state_decoder_quiescent ?
3537 " <decode:quiescent>" : ""),
3538 (hdw->state_encoder_ok ?
3539 "" : " <encode:init>"),
3540 (hdw->state_encoder_run ?
3541 " <encode:run>" : " <encode:stop>"),
3542 (hdw->state_encoder_config ?
3543 " <encode:configok>" :
3544 (hdw->state_encoder_waitok ?
3545 "" : " <encode:wait>")),
3546 (hdw->state_usbstream_run ?
3547 " <usb:run>" : " <usb:stop>"));
3548 break;
3549 case 3:
3550 return scnprintf(
3551 buf,acnt,
3552 "state: %s",
3553 pvr2_get_state_name(hdw->master_state));
3554 break;
3555 default: break;
3556 }
3557 return 0;
3558}
3559
3560
3561unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
3562 char *buf,unsigned int acnt)
3563{
3564 unsigned int bcnt,ccnt,idx;
3565 bcnt = 0;
3566 LOCK_TAKE(hdw->big_lock);
3567 for (idx = 0; ; idx++) {
3568 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
3569 if (!ccnt) break;
3570 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
3571 if (!acnt) break;
3572 buf[0] = '\n'; ccnt = 1;
3573 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
3574 }
3575 LOCK_GIVE(hdw->big_lock);
3576 return bcnt;
3577}
3578
3579
3580static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
3581{
3582 char buf[128];
3583 unsigned int idx,ccnt;
3584
3585 for (idx = 0; ; idx++) {
3586 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
3587 if (!ccnt) break;
3588 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
3589 }
3590}
3591
3592
3593/* Evaluate and update the driver's current state, taking various actions
3594 as appropriate for the update. */
3595static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
3596{
3597 unsigned int st;
3598 int state_updated = 0;
3599 int callback_flag = 0;
3600
3601 pvr2_trace(PVR2_TRACE_STBITS,
3602 "Drive state check START");
3603 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
3604 pvr2_hdw_state_log_state(hdw);
3605 }
3606
3607 /* Process all state and get back over disposition */
3608 state_updated = pvr2_hdw_state_update(hdw);
3609
3610 /* Update master state based upon all other states. */
3611 if (!hdw->flag_ok) {
3612 st = PVR2_STATE_DEAD;
3613 } else if (hdw->fw1_state != FW1_STATE_OK) {
3614 st = PVR2_STATE_COLD;
3615 } else if (!hdw->state_encoder_ok) {
3616 st = PVR2_STATE_WARM;
3617 } else if (hdw->flag_tripped || hdw->flag_decoder_missed) {
3618 st = PVR2_STATE_ERROR;
3619 } else if (hdw->state_encoder_run &&
3620 hdw->state_decoder_run &&
3621 hdw->state_usbstream_run) {
3622 st = PVR2_STATE_RUN;
3623 } else {
3624 st = PVR2_STATE_READY;
3625 }
3626 if (hdw->master_state != st) {
3627 pvr2_trace(PVR2_TRACE_STATE,
3628 "Device state change from %s to %s",
3629 pvr2_get_state_name(hdw->master_state),
3630 pvr2_get_state_name(st));
3631 hdw->master_state = st;
3632 state_updated = !0;
3633 callback_flag = !0;
3634 }
3635 if (state_updated) {
3636 /* Trigger anyone waiting on any state changes here. */
3637 wake_up(&hdw->state_wait_data);
3638 }
3639
3640 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
3641 pvr2_hdw_state_log_state(hdw);
3642 }
3643 pvr2_trace(PVR2_TRACE_STBITS,
3644 "Drive state check DONE callback=%d",callback_flag);
3645
3646 return callback_flag;
3647}
3648
3649
3650/* Cause kernel thread to check / update driver state */
3651static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
3652{
3653 if (hdw->state_stale) return;
3654 hdw->state_stale = !0;
3655 trace_stbit("state_stale",hdw->state_stale);
3656 queue_work(hdw->workqueue,&hdw->workpoll);
3657}
3658
3659
3660void pvr2_hdw_get_debug_info_unlocked(const struct pvr2_hdw *hdw,
3661 struct pvr2_hdw_debug_info *ptr)
3364{ 3662{
3365 ptr->big_lock_held = hdw->big_lock_held; 3663 ptr->big_lock_held = hdw->big_lock_held;
3366 ptr->ctl_lock_held = hdw->ctl_lock_held; 3664 ptr->ctl_lock_held = hdw->ctl_lock_held;
3367 ptr->flag_ok = hdw->flag_ok;
3368 ptr->flag_disconnected = hdw->flag_disconnected; 3665 ptr->flag_disconnected = hdw->flag_disconnected;
3369 ptr->flag_init_ok = hdw->flag_init_ok; 3666 ptr->flag_init_ok = hdw->flag_init_ok;
3370 ptr->flag_streaming_enabled = hdw->flag_streaming_enabled; 3667 ptr->flag_ok = hdw->flag_ok;
3371 ptr->subsys_flags = hdw->subsys_enabled_mask; 3668 ptr->fw1_state = hdw->fw1_state;
3669 ptr->flag_decoder_missed = hdw->flag_decoder_missed;
3670 ptr->flag_tripped = hdw->flag_tripped;
3671 ptr->state_encoder_ok = hdw->state_encoder_ok;
3672 ptr->state_encoder_run = hdw->state_encoder_run;
3673 ptr->state_decoder_run = hdw->state_decoder_run;
3674 ptr->state_usbstream_run = hdw->state_usbstream_run;
3675 ptr->state_decoder_quiescent = hdw->state_decoder_quiescent;
3676 ptr->state_pipeline_config = hdw->state_pipeline_config;
3677 ptr->state_pipeline_req = hdw->state_pipeline_req;
3678 ptr->state_pipeline_pause = hdw->state_pipeline_pause;
3679 ptr->state_pipeline_idle = hdw->state_pipeline_idle;
3372 ptr->cmd_debug_state = hdw->cmd_debug_state; 3680 ptr->cmd_debug_state = hdw->cmd_debug_state;
3373 ptr->cmd_code = hdw->cmd_debug_code; 3681 ptr->cmd_code = hdw->cmd_debug_code;
3374 ptr->cmd_debug_write_len = hdw->cmd_debug_write_len; 3682 ptr->cmd_debug_write_len = hdw->cmd_debug_write_len;
@@ -3381,6 +3689,15 @@ void pvr2_hdw_get_debug_info(const struct pvr2_hdw *hdw,
3381} 3689}
3382 3690
3383 3691
3692void pvr2_hdw_get_debug_info_locked(struct pvr2_hdw *hdw,
3693 struct pvr2_hdw_debug_info *ptr)
3694{
3695 LOCK_TAKE(hdw->ctl_lock); do {
3696 pvr2_hdw_get_debug_info_unlocked(hdw,ptr);
3697 } while(0); LOCK_GIVE(hdw->ctl_lock);
3698}
3699
3700
3384int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp) 3701int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
3385{ 3702{
3386 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp); 3703 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
diff --git a/drivers/media/video/pvrusb2/pvrusb2-hdw.h b/drivers/media/video/pvrusb2/pvrusb2-hdw.h
index e2f9d5e4cb65..3ad7a13d6c39 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-hdw.h
+++ b/drivers/media/video/pvrusb2/pvrusb2-hdw.h
@@ -44,27 +44,6 @@
44#define PVR2_CVAL_INPUT_COMPOSITE 2 44#define PVR2_CVAL_INPUT_COMPOSITE 2
45#define PVR2_CVAL_INPUT_RADIO 3 45#define PVR2_CVAL_INPUT_RADIO 3
46 46
47/* Subsystem definitions - these are various pieces that can be
48 independently stopped / started. Usually you don't want to mess with
49 this directly (let the driver handle things itself), but it is useful
50 for debugging. */
51#define PVR2_SUBSYS_B_ENC_FIRMWARE 0
52#define PVR2_SUBSYS_B_ENC_CFG 1
53#define PVR2_SUBSYS_B_DIGITIZER_RUN 2
54#define PVR2_SUBSYS_B_USBSTREAM_RUN 3
55#define PVR2_SUBSYS_B_ENC_RUN 4
56
57#define PVR2_SUBSYS_CFG_ALL ( \
58 (1 << PVR2_SUBSYS_B_ENC_FIRMWARE) | \
59 (1 << PVR2_SUBSYS_B_ENC_CFG) )
60#define PVR2_SUBSYS_RUN_ALL ( \
61 (1 << PVR2_SUBSYS_B_DIGITIZER_RUN) | \
62 (1 << PVR2_SUBSYS_B_USBSTREAM_RUN) | \
63 (1 << PVR2_SUBSYS_B_ENC_RUN) )
64#define PVR2_SUBSYS_ALL ( \
65 PVR2_SUBSYS_CFG_ALL | \
66 PVR2_SUBSYS_RUN_ALL )
67
68enum pvr2_config { 47enum pvr2_config {
69 pvr2_config_empty, /* No configuration */ 48 pvr2_config_empty, /* No configuration */
70 pvr2_config_mpeg, /* Encoded / compressed video */ 49 pvr2_config_mpeg, /* Encoded / compressed video */
@@ -79,8 +58,41 @@ enum pvr2_v4l_type {
79 pvr2_v4l_type_radio, 58 pvr2_v4l_type_radio,
80}; 59};
81 60
61/* Major states that we can be in:
62 *
63 * DEAD - Device is in an unusable state and cannot be recovered. This
64 * can happen if we completely lose the ability to communicate with it
65 * (but it might still on the bus). In this state there's nothing we can
66 * do; it must be replugged in order to recover.
67 *
68 * COLD - Device is in an unusuable state, needs microcontroller firmware.
69 *
70 * WARM - We can communicate with the device and the proper
71 * microcontroller firmware is running, but other device initialization is
72 * still needed (e.g. encoder firmware).
73 *
74 * ERROR - A problem prevents capture operation (e.g. encoder firmware
75 * missing).
76 *
77 * READY - Device is operational, but not streaming.
78 *
79 * RUN - Device is streaming.
80 *
81 */
82#define PVR2_STATE_NONE 0
83#define PVR2_STATE_DEAD 1
84#define PVR2_STATE_COLD 2
85#define PVR2_STATE_WARM 3
86#define PVR2_STATE_ERROR 4
87#define PVR2_STATE_READY 5
88#define PVR2_STATE_RUN 6
89
90/* Translate configuration enum to a string label */
82const char *pvr2_config_get_name(enum pvr2_config); 91const char *pvr2_config_get_name(enum pvr2_config);
83 92
93/* Translate a master state enum to a string label */
94const char *pvr2_hdw_get_state_name(unsigned int);
95
84struct pvr2_hdw; 96struct pvr2_hdw;
85 97
86/* Create and return a structure for interacting with the underlying 98/* Create and return a structure for interacting with the underlying
@@ -88,28 +100,13 @@ struct pvr2_hdw;
88struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf, 100struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
89 const struct usb_device_id *devid); 101 const struct usb_device_id *devid);
90 102
91/* Poll for background activity (if any) */
92void pvr2_hdw_poll(struct pvr2_hdw *);
93
94/* Trigger a poll to take place later at a convenient time */
95void pvr2_hdw_poll_trigger_unlocked(struct pvr2_hdw *);
96
97/* Register a callback used to trigger a future poll */
98void pvr2_hdw_setup_poll_trigger(struct pvr2_hdw *,
99 void (*func)(void *),
100 void *data);
101
102/* Destroy hardware interaction structure */ 103/* Destroy hardware interaction structure */
103void pvr2_hdw_destroy(struct pvr2_hdw *); 104void pvr2_hdw_destroy(struct pvr2_hdw *);
104 105
105/* Set up the structure and attempt to put the device into a usable state. 106/* Register a function to be called whenever the master state changes. */
106 This can be a time-consuming operation, which is why it is not done 107void pvr2_hdw_set_state_callback(struct pvr2_hdw *,
107 internally as part of the create() step. Return value is exactly the 108 void (*callback_func)(void *),
108 same as pvr2_hdw_init_ok(). */ 109 void *callback_data);
109int pvr2_hdw_setup(struct pvr2_hdw *);
110
111/* Initialization succeeded */
112int pvr2_hdw_init_ok(struct pvr2_hdw *);
113 110
114/* Return true if in the ready (normal) state */ 111/* Return true if in the ready (normal) state */
115int pvr2_hdw_dev_ok(struct pvr2_hdw *); 112int pvr2_hdw_dev_ok(struct pvr2_hdw *);
@@ -161,12 +158,21 @@ int pvr2_hdw_get_tuner_status(struct pvr2_hdw *,struct v4l2_tuner *);
161/* Query device and see if it thinks it is on a high-speed USB link */ 158/* Query device and see if it thinks it is on a high-speed USB link */
162int pvr2_hdw_is_hsm(struct pvr2_hdw *); 159int pvr2_hdw_is_hsm(struct pvr2_hdw *);
163 160
161/* Return a string token representative of the hardware type */
162const char *pvr2_hdw_get_type(struct pvr2_hdw *);
163
164/* Return a single line description of the hardware type */
165const char *pvr2_hdw_get_desc(struct pvr2_hdw *);
166
164/* Turn streaming on/off */ 167/* Turn streaming on/off */
165int pvr2_hdw_set_streaming(struct pvr2_hdw *,int); 168int pvr2_hdw_set_streaming(struct pvr2_hdw *,int);
166 169
167/* Find out if streaming is on */ 170/* Find out if streaming is on */
168int pvr2_hdw_get_streaming(struct pvr2_hdw *); 171int pvr2_hdw_get_streaming(struct pvr2_hdw *);
169 172
173/* Retrieve driver overall state */
174int pvr2_hdw_get_state(struct pvr2_hdw *);
175
170/* Configure the type of stream to generate */ 176/* Configure the type of stream to generate */
171int pvr2_hdw_set_stream_type(struct pvr2_hdw *, enum pvr2_config); 177int pvr2_hdw_set_stream_type(struct pvr2_hdw *, enum pvr2_config);
172 178
@@ -177,26 +183,6 @@ struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *);
177int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,struct v4l2_standard *std, 183int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,struct v4l2_standard *std,
178 unsigned int idx); 184 unsigned int idx);
179 185
180/* Enable / disable various pieces of hardware. Items to change are
181 identified by bit positions within msk, and new state for each item is
182 identified by corresponding bit positions within val. */
183void pvr2_hdw_subsys_bit_chg(struct pvr2_hdw *hdw,
184 unsigned long msk,unsigned long val);
185
186/* Retrieve mask indicating which pieces of hardware are currently enabled
187 / configured. */
188unsigned long pvr2_hdw_subsys_get(struct pvr2_hdw *);
189
190/* Adjust mask of what get shut down when streaming is stopped. This is a
191 debugging aid. */
192void pvr2_hdw_subsys_stream_bit_chg(struct pvr2_hdw *hdw,
193 unsigned long msk,unsigned long val);
194
195/* Retrieve mask indicating which pieces of hardware are disabled when
196 streaming is turned off. */
197unsigned long pvr2_hdw_subsys_stream_get(struct pvr2_hdw *);
198
199
200/* Enable / disable retrieval of CPU firmware or prom contents. This must 186/* Enable / disable retrieval of CPU firmware or prom contents. This must
201 be enabled before pvr2_hdw_cpufw_get() will function. Note that doing 187 be enabled before pvr2_hdw_cpufw_get() will function. Note that doing
202 this may prevent the device from running (and leaving this mode may 188 this may prevent the device from running (and leaving this mode may
@@ -253,6 +239,9 @@ void pvr2_hdw_cpureset_assert(struct pvr2_hdw *,int);
253/* Execute a USB-commanded device reset */ 239/* Execute a USB-commanded device reset */
254void pvr2_hdw_device_reset(struct pvr2_hdw *); 240void pvr2_hdw_device_reset(struct pvr2_hdw *);
255 241
242/* Reset worker's error trapping circuit breaker */
243int pvr2_hdw_untrip(struct pvr2_hdw *);
244
256/* Execute hard reset command (after this point it's likely that the 245/* Execute hard reset command (after this point it's likely that the
257 encoder will have to be reconfigured). This also clears the "useless" 246 encoder will have to be reconfigured). This also clears the "useless"
258 state. */ 247 state. */
@@ -275,11 +264,21 @@ int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val);
275struct pvr2_hdw_debug_info { 264struct pvr2_hdw_debug_info {
276 int big_lock_held; 265 int big_lock_held;
277 int ctl_lock_held; 266 int ctl_lock_held;
278 int flag_ok;
279 int flag_disconnected; 267 int flag_disconnected;
280 int flag_init_ok; 268 int flag_init_ok;
281 int flag_streaming_enabled; 269 int flag_ok;
282 unsigned long subsys_flags; 270 int fw1_state;
271 int flag_decoder_missed;
272 int flag_tripped;
273 int state_encoder_ok;
274 int state_encoder_run;
275 int state_decoder_run;
276 int state_usbstream_run;
277 int state_decoder_quiescent;
278 int state_pipeline_config;
279 int state_pipeline_req;
280 int state_pipeline_pause;
281 int state_pipeline_idle;
283 int cmd_debug_state; 282 int cmd_debug_state;
284 int cmd_debug_write_len; 283 int cmd_debug_write_len;
285 int cmd_debug_read_len; 284 int cmd_debug_read_len;
@@ -295,8 +294,20 @@ struct pvr2_hdw_debug_info {
295 diagnosing lockups. Note that this operation is completed without any 294 diagnosing lockups. Note that this operation is completed without any
296 kind of locking and so it is not atomic and may yield inconsistent 295 kind of locking and so it is not atomic and may yield inconsistent
297 results. This is *purely* a debugging aid. */ 296 results. This is *purely* a debugging aid. */
298void pvr2_hdw_get_debug_info(const struct pvr2_hdw *hdw, 297void pvr2_hdw_get_debug_info_unlocked(const struct pvr2_hdw *hdw,
299 struct pvr2_hdw_debug_info *); 298 struct pvr2_hdw_debug_info *);
299
300/* Intrusively retrieve internal state info - this is useful for
301 diagnosing overall driver state. This operation synchronizes against
302 the overall driver mutex - so if there are locking problems this will
303 likely hang! This is *purely* a debugging aid. */
304void pvr2_hdw_get_debug_info_locked(struct pvr2_hdw *hdw,
305 struct pvr2_hdw_debug_info *);
306
307/* Report out several lines of text that describes driver internal state.
308 Results are written into the passed-in buffer. */
309unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
310 char *buf_ptr,unsigned int buf_size);
300 311
301/* Cause modules to log their state once */ 312/* Cause modules to log their state once */
302void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw); 313void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw);
@@ -306,9 +317,6 @@ void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw);
306 a debugging aid. */ 317 a debugging aid. */
307int pvr2_upload_firmware2(struct pvr2_hdw *hdw); 318int pvr2_upload_firmware2(struct pvr2_hdw *hdw);
308 319
309/* List of device types that we can match */
310extern struct usb_device_id pvr2_device_table[];
311
312#endif /* __PVRUSB2_HDW_H */ 320#endif /* __PVRUSB2_HDW_H */
313 321
314/* 322/*
diff --git a/drivers/media/video/pvrusb2/pvrusb2-i2c-core.c b/drivers/media/video/pvrusb2/pvrusb2-i2c-core.c
index c817c864e6a0..62867fa3517a 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-i2c-core.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-i2c-core.c
@@ -895,7 +895,7 @@ static int pvr2_i2c_attach_inform(struct i2c_client *client)
895 list_add_tail(&cp->list,&hdw->i2c_clients); 895 list_add_tail(&cp->list,&hdw->i2c_clients);
896 hdw->i2c_pend_types |= PVR2_I2C_PEND_DETECT; 896 hdw->i2c_pend_types |= PVR2_I2C_PEND_DETECT;
897 } while (0); mutex_unlock(&hdw->i2c_list_lock); 897 } while (0); mutex_unlock(&hdw->i2c_list_lock);
898 if (fl) pvr2_hdw_poll_trigger_unlocked(hdw); 898 if (fl) queue_work(hdw->workqueue,&hdw->worki2csync);
899 return 0; 899 return 0;
900} 900}
901 901
@@ -980,14 +980,16 @@ void pvr2_i2c_core_init(struct pvr2_hdw *hdw)
980 printk(KERN_INFO "%s: IR disabled\n",hdw->name); 980 printk(KERN_INFO "%s: IR disabled\n",hdw->name);
981 hdw->i2c_func[0x18] = i2c_black_hole; 981 hdw->i2c_func[0x18] = i2c_black_hole;
982 } else if (ir_mode[hdw->unit_number] == 1) { 982 } else if (ir_mode[hdw->unit_number] == 1) {
983 if (hdw->hdw_type == PVR2_HDW_TYPE_24XXX) { 983 if (hdw->hdw_desc->flag_has_hauppauge_custom_ir) {
984 hdw->i2c_func[0x18] = i2c_24xxx_ir; 984 hdw->i2c_func[0x18] = i2c_24xxx_ir;
985 } 985 }
986 } 986 }
987 if (hdw->hdw_type == PVR2_HDW_TYPE_24XXX) { 987 if (hdw->hdw_desc->flag_has_cx25840) {
988 hdw->i2c_func[0x1b] = i2c_hack_wm8775;
989 hdw->i2c_func[0x44] = i2c_hack_cx25840; 988 hdw->i2c_func[0x44] = i2c_hack_cx25840;
990 } 989 }
990 if (hdw->hdw_desc->flag_has_wm8775) {
991 hdw->i2c_func[0x1b] = i2c_hack_wm8775;
992 }
991 993
992 // Configure the adapter and set up everything else related to it. 994 // Configure the adapter and set up everything else related to it.
993 memcpy(&hdw->i2c_adap,&pvr2_i2c_adap_template,sizeof(hdw->i2c_adap)); 995 memcpy(&hdw->i2c_adap,&pvr2_i2c_adap_template,sizeof(hdw->i2c_adap));
diff --git a/drivers/media/video/pvrusb2/pvrusb2-main.c b/drivers/media/video/pvrusb2/pvrusb2-main.c
index 11b3b2e84b90..b63b2265503a 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-main.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-main.c
@@ -28,6 +28,7 @@
28#include <linux/videodev2.h> 28#include <linux/videodev2.h>
29 29
30#include "pvrusb2-hdw.h" 30#include "pvrusb2-hdw.h"
31#include "pvrusb2-devattr.h"
31#include "pvrusb2-context.h" 32#include "pvrusb2-context.h"
32#include "pvrusb2-debug.h" 33#include "pvrusb2-debug.h"
33#include "pvrusb2-v4l2.h" 34#include "pvrusb2-v4l2.h"
@@ -148,11 +149,6 @@ static void __exit pvr_exit(void)
148module_init(pvr_init); 149module_init(pvr_init);
149module_exit(pvr_exit); 150module_exit(pvr_exit);
150 151
151/* Mike Isely <mcisely@pobox.com> 11-Mar-2006: See pvrusb2-hdw.c for
152 MODULE_DEVICE_TABLE(). We have to declare that attribute there
153 because that's where the device table actually is now and it seems
154 that certain gcc configurations get angry if MODULE_DEVICE_TABLE()
155 is used on what ends up being an external symbol. */
156MODULE_AUTHOR(DRIVER_AUTHOR); 152MODULE_AUTHOR(DRIVER_AUTHOR);
157MODULE_DESCRIPTION(DRIVER_DESC); 153MODULE_DESCRIPTION(DRIVER_DESC);
158MODULE_LICENSE("GPL"); 154MODULE_LICENSE("GPL");
diff --git a/drivers/media/video/pvrusb2/pvrusb2-std.c b/drivers/media/video/pvrusb2/pvrusb2-std.c
index 63e55bb59fcb..da309288daa4 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-std.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-std.c
@@ -50,6 +50,10 @@ struct std_name {
50 V4L2_STD_NTSC_M_KR| \ 50 V4L2_STD_NTSC_M_KR| \
51 V4L2_STD_NTSC_443) 51 V4L2_STD_NTSC_443)
52 52
53#define CSTD_ATSC \
54 (V4L2_STD_ATSC_8_VSB| \
55 V4L2_STD_ATSC_16_VSB)
56
53#define CSTD_SECAM \ 57#define CSTD_SECAM \
54 (V4L2_STD_SECAM_B| \ 58 (V4L2_STD_SECAM_B| \
55 V4L2_STD_SECAM_D| \ 59 V4L2_STD_SECAM_D| \
@@ -82,6 +86,7 @@ static const struct std_name std_groups[] = {
82 {"PAL",CSTD_PAL}, 86 {"PAL",CSTD_PAL},
83 {"NTSC",CSTD_NTSC}, 87 {"NTSC",CSTD_NTSC},
84 {"SECAM",CSTD_SECAM}, 88 {"SECAM",CSTD_SECAM},
89 {"ATSC",CSTD_ATSC},
85}; 90};
86 91
87/* Mapping of standard bits to modulation system */ 92/* Mapping of standard bits to modulation system */
@@ -104,6 +109,8 @@ static const struct std_name std_items[] = {
104 {"N",TSTD_N}, 109 {"N",TSTD_N},
105 {"Nc",TSTD_Nc}, 110 {"Nc",TSTD_Nc},
106 {"60",TSTD_60}, 111 {"60",TSTD_60},
112 {"8VSB",V4L2_STD_ATSC_8_VSB},
113 {"16VSB",V4L2_STD_ATSC_16_VSB},
107}; 114};
108 115
109 116
diff --git a/drivers/media/video/pvrusb2/pvrusb2-sysfs.c b/drivers/media/video/pvrusb2/pvrusb2-sysfs.c
index 3c57a7d8200b..7a1cd878e31a 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-sysfs.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-sysfs.c
@@ -43,10 +43,14 @@ struct pvr2_sysfs {
43 struct device_attribute attr_v4l_radio_minor_number; 43 struct device_attribute attr_v4l_radio_minor_number;
44 struct device_attribute attr_unit_number; 44 struct device_attribute attr_unit_number;
45 struct device_attribute attr_bus_info; 45 struct device_attribute attr_bus_info;
46 struct device_attribute attr_hdw_name;
47 struct device_attribute attr_hdw_desc;
46 int v4l_minor_number_created_ok; 48 int v4l_minor_number_created_ok;
47 int v4l_radio_minor_number_created_ok; 49 int v4l_radio_minor_number_created_ok;
48 int unit_number_created_ok; 50 int unit_number_created_ok;
49 int bus_info_created_ok; 51 int bus_info_created_ok;
52 int hdw_name_created_ok;
53 int hdw_desc_created_ok;
50}; 54};
51 55
52#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC 56#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
@@ -712,6 +716,14 @@ static void class_dev_destroy(struct pvr2_sysfs *sfp)
712 pvr2_sysfs_tear_down_debugifc(sfp); 716 pvr2_sysfs_tear_down_debugifc(sfp);
713#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */ 717#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
714 pvr2_sysfs_tear_down_controls(sfp); 718 pvr2_sysfs_tear_down_controls(sfp);
719 if (sfp->hdw_desc_created_ok) {
720 device_remove_file(sfp->class_dev,
721 &sfp->attr_hdw_desc);
722 }
723 if (sfp->hdw_name_created_ok) {
724 device_remove_file(sfp->class_dev,
725 &sfp->attr_hdw_name);
726 }
715 if (sfp->bus_info_created_ok) { 727 if (sfp->bus_info_created_ok) {
716 device_remove_file(sfp->class_dev, 728 device_remove_file(sfp->class_dev,
717 &sfp->attr_bus_info); 729 &sfp->attr_bus_info);
@@ -758,6 +770,28 @@ static ssize_t bus_info_show(struct device *class_dev,
758} 770}
759 771
760 772
773static ssize_t hdw_name_show(struct device *class_dev,
774 struct device_attribute *attr, char *buf)
775{
776 struct pvr2_sysfs *sfp;
777 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
778 if (!sfp) return -EINVAL;
779 return scnprintf(buf,PAGE_SIZE,"%s\n",
780 pvr2_hdw_get_type(sfp->channel.hdw));
781}
782
783
784static ssize_t hdw_desc_show(struct device *class_dev,
785 struct device_attribute *attr, char *buf)
786{
787 struct pvr2_sysfs *sfp;
788 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
789 if (!sfp) return -EINVAL;
790 return scnprintf(buf,PAGE_SIZE,"%s\n",
791 pvr2_hdw_get_desc(sfp->channel.hdw));
792}
793
794
761static ssize_t v4l_radio_minor_number_show(struct device *class_dev, 795static ssize_t v4l_radio_minor_number_show(struct device *class_dev,
762 struct device_attribute *attr, 796 struct device_attribute *attr,
763 char *buf) 797 char *buf)
@@ -871,6 +905,32 @@ static void class_dev_create(struct pvr2_sysfs *sfp,
871 sfp->bus_info_created_ok = !0; 905 sfp->bus_info_created_ok = !0;
872 } 906 }
873 907
908 sfp->attr_hdw_name.attr.name = "device_hardware_type";
909 sfp->attr_hdw_name.attr.mode = S_IRUGO;
910 sfp->attr_hdw_name.show = hdw_name_show;
911 sfp->attr_hdw_name.store = NULL;
912 ret = device_create_file(sfp->class_dev,
913 &sfp->attr_hdw_name);
914 if (ret < 0) {
915 printk(KERN_WARNING "%s: device_create_file error: %d\n",
916 __FUNCTION__, ret);
917 } else {
918 sfp->hdw_name_created_ok = !0;
919 }
920
921 sfp->attr_hdw_desc.attr.name = "device_hardware_description";
922 sfp->attr_hdw_desc.attr.mode = S_IRUGO;
923 sfp->attr_hdw_desc.show = hdw_desc_show;
924 sfp->attr_hdw_desc.store = NULL;
925 ret = device_create_file(sfp->class_dev,
926 &sfp->attr_hdw_desc);
927 if (ret < 0) {
928 printk(KERN_WARNING "%s: device_create_file error: %d\n",
929 __FUNCTION__, ret);
930 } else {
931 sfp->hdw_desc_created_ok = !0;
932 }
933
874 pvr2_sysfs_add_controls(sfp); 934 pvr2_sysfs_add_controls(sfp);
875#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC 935#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
876 pvr2_sysfs_add_debugifc(sfp); 936 pvr2_sysfs_add_debugifc(sfp);
diff --git a/drivers/media/video/pvrusb2/pvrusb2-v4l2.c b/drivers/media/video/pvrusb2/pvrusb2-v4l2.c
index 7a596ea7cfe6..8f0587ebd4bd 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-v4l2.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-v4l2.c
@@ -205,6 +205,7 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
205 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability)); 205 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
206 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw), 206 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
207 sizeof(cap->bus_info)); 207 sizeof(cap->bus_info));
208 strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
208 209
209 ret = 0; 210 ret = 0;
210 break; 211 break;
@@ -1015,10 +1016,8 @@ static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1015 sp = fh->dev_info->stream->stream; 1016 sp = fh->dev_info->stream->stream;
1016 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh); 1017 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1017 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config); 1018 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
1018 pvr2_hdw_set_streaming(hdw,!0); 1019 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1019 ret = pvr2_ioread_set_enabled(fh->rhp,!0); 1020 return pvr2_ioread_set_enabled(fh->rhp,!0);
1020
1021 return ret;
1022} 1021}
1023 1022
1024 1023
diff --git a/drivers/media/video/pvrusb2/pvrusb2-video-v4l.c b/drivers/media/video/pvrusb2/pvrusb2-video-v4l.c
index 61efa6f02200..7c47345501b6 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-video-v4l.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-video-v4l.c
@@ -49,29 +49,50 @@ struct pvr2_v4l_decoder {
49}; 49};
50 50
51 51
52struct routing_scheme {
53 const int *def;
54 unsigned int cnt;
55};
56
57
58static const int routing_scheme0[] = {
59 [PVR2_CVAL_INPUT_TV] = SAA7115_COMPOSITE4,
60 /* In radio mode, we mute the video, but point at one
61 spot just to stay consistent */
62 [PVR2_CVAL_INPUT_RADIO] = SAA7115_COMPOSITE5,
63 [PVR2_CVAL_INPUT_COMPOSITE] = SAA7115_COMPOSITE5,
64 [PVR2_CVAL_INPUT_SVIDEO] = SAA7115_SVIDEO2,
65};
66
67static const struct routing_scheme routing_schemes[] = {
68 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = {
69 .def = routing_scheme0,
70 .cnt = ARRAY_SIZE(routing_scheme0),
71 },
72};
73
52static void set_input(struct pvr2_v4l_decoder *ctxt) 74static void set_input(struct pvr2_v4l_decoder *ctxt)
53{ 75{
54 struct pvr2_hdw *hdw = ctxt->hdw; 76 struct pvr2_hdw *hdw = ctxt->hdw;
55 struct v4l2_routing route; 77 struct v4l2_routing route;
78 const struct routing_scheme *sp;
79 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
56 80
57 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_input(%d)",hdw->input_val); 81 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_input(%d)",hdw->input_val);
58 switch(hdw->input_val) { 82
59 case PVR2_CVAL_INPUT_TV: 83 if ((sid < ARRAY_SIZE(routing_schemes)) &&
60 route.input = SAA7115_COMPOSITE4; 84 ((sp = routing_schemes + sid) != 0) &&
61 break; 85 (hdw->input_val >= 0) &&
62 case PVR2_CVAL_INPUT_COMPOSITE: 86 (hdw->input_val < sp->cnt)) {
63 route.input = SAA7115_COMPOSITE5; 87 route.input = sp->def[hdw->input_val];
64 break; 88 } else {
65 case PVR2_CVAL_INPUT_SVIDEO: 89 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
66 route.input = SAA7115_SVIDEO2; 90 "*** WARNING *** i2c v4l2 set_input:"
67 break; 91 " Invalid routing scheme (%u) and/or input (%d)",
68 case PVR2_CVAL_INPUT_RADIO: 92 sid,hdw->input_val);
69 // In radio mode, we mute the video, but point at one
70 // spot just to stay consistent
71 route.input = SAA7115_COMPOSITE5;
72 default:
73 return; 93 return;
74 } 94 }
95
75 route.output = 0; 96 route.output = 0;
76 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route); 97 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
77} 98}
@@ -129,7 +150,7 @@ static const struct pvr2_v4l_decoder_ops decoder_ops[] = {
129static void decoder_detach(struct pvr2_v4l_decoder *ctxt) 150static void decoder_detach(struct pvr2_v4l_decoder *ctxt)
130{ 151{
131 ctxt->client->handler = NULL; 152 ctxt->client->handler = NULL;
132 ctxt->hdw->decoder_ctrl = NULL; 153 pvr2_hdw_set_decoder(ctxt->hdw,NULL);
133 kfree(ctxt); 154 kfree(ctxt);
134} 155}
135 156
@@ -217,7 +238,7 @@ int pvr2_i2c_decoder_v4l_setup(struct pvr2_hdw *hdw,
217 ctxt->client = cp; 238 ctxt->client = cp;
218 ctxt->hdw = hdw; 239 ctxt->hdw = hdw;
219 ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1; 240 ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1;
220 hdw->decoder_ctrl = &ctxt->ctrl; 241 pvr2_hdw_set_decoder(hdw,&ctxt->ctrl);
221 cp->handler = &ctxt->handler; 242 cp->handler = &ctxt->handler;
222 pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up", 243 pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up",
223 cp->client->addr); 244 cp->client->addr);
diff --git a/drivers/media/video/saa7115.c b/drivers/media/video/saa7115.c
index 2d18f0069821..41e5e518a47e 100644
--- a/drivers/media/video/saa7115.c
+++ b/drivers/media/video/saa7115.c
@@ -46,6 +46,7 @@
46#include <linux/videodev2.h> 46#include <linux/videodev2.h>
47#include <media/v4l2-common.h> 47#include <media/v4l2-common.h>
48#include <media/v4l2-chip-ident.h> 48#include <media/v4l2-chip-ident.h>
49#include <media/v4l2-i2c-drv-legacy.h>
49#include <media/saa7115.h> 50#include <media/saa7115.h>
50#include <asm/div64.h> 51#include <asm/div64.h>
51 52
@@ -1230,7 +1231,7 @@ static void saa711x_decode_vbi_line(struct i2c_client *client,
1230 1231
1231/* ============ SAA7115 AUDIO settings (end) ============= */ 1232/* ============ SAA7115 AUDIO settings (end) ============= */
1232 1233
1233static int saa711x_command(struct i2c_client *client, unsigned int cmd, void *arg) 1234static int saa7115_command(struct i2c_client *client, unsigned int cmd, void *arg)
1234{ 1235{
1235 struct saa711x_state *state = i2c_get_clientdata(client); 1236 struct saa711x_state *state = i2c_get_clientdata(client);
1236 1237
@@ -1449,26 +1450,17 @@ static int saa711x_command(struct i2c_client *client, unsigned int cmd, void *ar
1449 1450
1450/* ----------------------------------------------------------------------- */ 1451/* ----------------------------------------------------------------------- */
1451 1452
1452static struct i2c_driver i2c_driver_saa711x; 1453static int saa7115_probe(struct i2c_client *client)
1453
1454static int saa711x_attach(struct i2c_adapter *adapter, int address, int kind)
1455{ 1454{
1456 struct i2c_client *client;
1457 struct saa711x_state *state; 1455 struct saa711x_state *state;
1458 int i; 1456 int i;
1459 char name[17]; 1457 char name[17];
1460 u8 chip_id; 1458 u8 chip_id;
1461 1459
1462 /* Check if the adapter supports the needed features */ 1460 /* Check if the adapter supports the needed features */
1463 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 1461 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1464 return 0; 1462 return -EIO;
1465 1463
1466 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1467 if (client == 0)
1468 return -ENOMEM;
1469 client->addr = address;
1470 client->adapter = adapter;
1471 client->driver = &i2c_driver_saa711x;
1472 snprintf(client->name, sizeof(client->name) - 1, "saa7115"); 1464 snprintf(client->name, sizeof(client->name) - 1, "saa7115");
1473 1465
1474 for (i = 0; i < 0x0f; i++) { 1466 for (i = 0; i < 0x0f; i++) {
@@ -1485,18 +1477,16 @@ static int saa711x_attach(struct i2c_adapter *adapter, int address, int kind)
1485 /* Check whether this chip is part of the saa711x series */ 1477 /* Check whether this chip is part of the saa711x series */
1486 if (memcmp(name, "1f711", 5)) { 1478 if (memcmp(name, "1f711", 5)) {
1487 v4l_dbg(1, debug, client, "chip found @ 0x%x (ID %s) does not match a known saa711x chip.\n", 1479 v4l_dbg(1, debug, client, "chip found @ 0x%x (ID %s) does not match a known saa711x chip.\n",
1488 address << 1, name); 1480 client->addr << 1, name);
1489 kfree(client); 1481 return -ENODEV;
1490 return 0;
1491 } 1482 }
1492 1483
1493 snprintf(client->name, sizeof(client->name) - 1, "saa711%d",chip_id); 1484 snprintf(client->name, sizeof(client->name) - 1, "saa711%d",chip_id);
1494 v4l_info(client, "saa711%d found (%s) @ 0x%x (%s)\n", chip_id, name, address << 1, adapter->name); 1485 v4l_info(client, "saa711%d found (%s) @ 0x%x (%s)\n", chip_id, name, client->addr << 1, client->adapter->name);
1495 1486
1496 state = kzalloc(sizeof(struct saa711x_state), GFP_KERNEL); 1487 state = kzalloc(sizeof(struct saa711x_state), GFP_KERNEL);
1497 i2c_set_clientdata(client, state); 1488 i2c_set_clientdata(client, state);
1498 if (state == NULL) { 1489 if (state == NULL) {
1499 kfree(client);
1500 return -ENOMEM; 1490 return -ENOMEM;
1501 } 1491 }
1502 state->input = -1; 1492 state->input = -1;
@@ -1549,59 +1539,25 @@ static int saa711x_attach(struct i2c_adapter *adapter, int address, int kind)
1549 saa711x_writeregs(client, saa7115_init_misc); 1539 saa711x_writeregs(client, saa7115_init_misc);
1550 saa711x_set_v4lstd(client, V4L2_STD_NTSC); 1540 saa711x_set_v4lstd(client, V4L2_STD_NTSC);
1551 1541
1552 i2c_attach_client(client);
1553
1554 v4l_dbg(1, debug, client, "status: (1E) 0x%02x, (1F) 0x%02x\n", 1542 v4l_dbg(1, debug, client, "status: (1E) 0x%02x, (1F) 0x%02x\n",
1555 saa711x_read(client, R_1E_STATUS_BYTE_1_VD_DEC), saa711x_read(client, R_1F_STATUS_BYTE_2_VD_DEC)); 1543 saa711x_read(client, R_1E_STATUS_BYTE_1_VD_DEC), saa711x_read(client, R_1F_STATUS_BYTE_2_VD_DEC));
1556
1557 return 0; 1544 return 0;
1558} 1545}
1559 1546
1560static int saa711x_probe(struct i2c_adapter *adapter) 1547/* ----------------------------------------------------------------------- */
1561{
1562 if (adapter->class & I2C_CLASS_TV_ANALOG || adapter->class & I2C_CLASS_TV_DIGITAL)
1563 return i2c_probe(adapter, &addr_data, &saa711x_attach);
1564 return 0;
1565}
1566 1548
1567static int saa711x_detach(struct i2c_client *client) 1549static int saa7115_remove(struct i2c_client *client)
1568{ 1550{
1569 struct saa711x_state *state = i2c_get_clientdata(client); 1551 kfree(i2c_get_clientdata(client));
1570 int err;
1571
1572 err = i2c_detach_client(client);
1573 if (err) {
1574 return err;
1575 }
1576
1577 kfree(state);
1578 kfree(client);
1579 return 0; 1552 return 0;
1580} 1553}
1581 1554
1582/* ----------------------------------------------------------------------- */ 1555static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1583 1556 .name = "saa7115",
1584/* i2c implementation */ 1557 .driverid = I2C_DRIVERID_SAA711X,
1585static struct i2c_driver i2c_driver_saa711x = { 1558 .command = saa7115_command,
1586 .driver = { 1559 .probe = saa7115_probe,
1587 .name = "saa7115", 1560 .remove = saa7115_remove,
1588 }, 1561 .legacy_class = I2C_CLASS_TV_ANALOG | I2C_CLASS_TV_DIGITAL,
1589 .id = I2C_DRIVERID_SAA711X,
1590 .attach_adapter = saa711x_probe,
1591 .detach_client = saa711x_detach,
1592 .command = saa711x_command,
1593}; 1562};
1594 1563
1595
1596static int __init saa711x_init_module(void)
1597{
1598 return i2c_add_driver(&i2c_driver_saa711x);
1599}
1600
1601static void __exit saa711x_cleanup_module(void)
1602{
1603 i2c_del_driver(&i2c_driver_saa711x);
1604}
1605
1606module_init(saa711x_init_module);
1607module_exit(saa711x_cleanup_module);
diff --git a/drivers/media/video/saa7127.c b/drivers/media/video/saa7127.c
index e35ef321ec71..06c88db656b4 100644
--- a/drivers/media/video/saa7127.c
+++ b/drivers/media/video/saa7127.c
@@ -55,10 +55,11 @@
55#include <linux/videodev2.h> 55#include <linux/videodev2.h>
56#include <media/v4l2-common.h> 56#include <media/v4l2-common.h>
57#include <media/v4l2-chip-ident.h> 57#include <media/v4l2-chip-ident.h>
58#include <media/v4l2-i2c-drv.h>
58#include <media/saa7127.h> 59#include <media/saa7127.h>
59 60
60static int debug = 0; 61static int debug;
61static int test_image = 0; 62static int test_image;
62 63
63MODULE_DESCRIPTION("Philips SAA7127/9 video encoder driver"); 64MODULE_DESCRIPTION("Philips SAA7127/9 video encoder driver");
64MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil"); 65MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil");
@@ -68,10 +69,6 @@ module_param(test_image, int, 0644);
68MODULE_PARM_DESC(debug, "debug level (0-2)"); 69MODULE_PARM_DESC(debug, "debug level (0-2)");
69MODULE_PARM_DESC(test_image, "test_image (0-1)"); 70MODULE_PARM_DESC(test_image, "test_image (0-1)");
70 71
71static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
72
73
74I2C_CLIENT_INSMOD;
75 72
76/* 73/*
77 * SAA7127 registers 74 * SAA7127 registers
@@ -360,9 +357,10 @@ static int saa7127_set_cc(struct i2c_client *client, struct v4l2_sliced_vbi_data
360 if (enable && (data->field != 0 || data->line != 21)) 357 if (enable && (data->field != 0 || data->line != 21))
361 return -EINVAL; 358 return -EINVAL;
362 if (state->cc_enable != enable) { 359 if (state->cc_enable != enable) {
363 v4l_dbg(1, debug, client, "Turn CC %s\n", enable ? "on" : "off"); 360 v4l_dbg(1, debug, client,
361 "Turn CC %s\n", enable ? "on" : "off");
364 saa7127_write(client, SAA7127_REG_CLOSED_CAPTION, 362 saa7127_write(client, SAA7127_REG_CLOSED_CAPTION,
365 (state->xds_enable << 7) | (enable << 6) | 0x11); 363 (state->xds_enable << 7) | (enable << 6) | 0x11);
366 state->cc_enable = enable; 364 state->cc_enable = enable;
367 } 365 }
368 if (!enable) 366 if (!enable)
@@ -420,7 +418,8 @@ static int saa7127_set_wss(struct i2c_client *client, struct v4l2_sliced_vbi_dat
420 418
421 saa7127_write(client, 0x26, data->data[0]); 419 saa7127_write(client, 0x26, data->data[0]);
422 saa7127_write(client, 0x27, 0x80 | (data->data[1] & 0x3f)); 420 saa7127_write(client, 0x27, 0x80 | (data->data[1] & 0x3f));
423 v4l_dbg(1, debug, client, "WSS mode: %s\n", wss_strs[data->data[0] & 0xf]); 421 v4l_dbg(1, debug, client,
422 "WSS mode: %s\n", wss_strs[data->data[0] & 0xf]);
424 state->wss_mode = (data->data[1] & 0x3f) << 8 | data->data[0]; 423 state->wss_mode = (data->data[1] & 0x3f) << 8 | data->data[0];
425 return 0; 424 return 0;
426} 425}
@@ -507,7 +506,8 @@ static int saa7127_set_output_type(struct i2c_client *client, int output)
507 default: 506 default:
508 return -EINVAL; 507 return -EINVAL;
509 } 508 }
510 v4l_dbg(1, debug, client, "Selecting %s output type\n", output_strs[output]); 509 v4l_dbg(1, debug, client,
510 "Selecting %s output type\n", output_strs[output]);
511 511
512 /* Configure Encoder */ 512 /* Configure Encoder */
513 saa7127_write(client, 0x2d, state->reg_2d); 513 saa7127_write(client, 0x2d, state->reg_2d);
@@ -569,12 +569,10 @@ static int saa7127_command(struct i2c_client *client,
569 { 569 {
570 int rc = 0; 570 int rc = 0;
571 571
572 if (state->input_type != route->input) { 572 if (state->input_type != route->input)
573 rc = saa7127_set_input_type(client, route->input); 573 rc = saa7127_set_input_type(client, route->input);
574 } 574 if (rc == 0 && state->output_type != route->output)
575 if (rc == 0 && state->output_type != route->output) {
576 rc = saa7127_set_output_type(client, route->output); 575 rc = saa7127_set_output_type(client, route->output);
577 }
578 return rc; 576 return rc;
579 } 577 }
580 578
@@ -620,7 +618,8 @@ static int saa7127_command(struct i2c_client *client,
620 { 618 {
621 struct v4l2_register *reg = arg; 619 struct v4l2_register *reg = arg;
622 620
623 if (!v4l2_chip_match_i2c_client(client, reg->match_type, reg->match_chip)) 621 if (!v4l2_chip_match_i2c_client(client,
622 reg->match_type, reg->match_chip))
624 return -EINVAL; 623 return -EINVAL;
625 if (!capable(CAP_SYS_ADMIN)) 624 if (!capable(CAP_SYS_ADMIN))
626 return -EPERM; 625 return -EPERM;
@@ -637,16 +636,16 @@ static int saa7127_command(struct i2c_client *client,
637 struct v4l2_sliced_vbi_data *data = arg; 636 struct v4l2_sliced_vbi_data *data = arg;
638 637
639 switch (data->id) { 638 switch (data->id) {
640 case V4L2_SLICED_WSS_625: 639 case V4L2_SLICED_WSS_625:
641 return saa7127_set_wss(client, data); 640 return saa7127_set_wss(client, data);
642 case V4L2_SLICED_VPS: 641 case V4L2_SLICED_VPS:
643 return saa7127_set_vps(client, data); 642 return saa7127_set_vps(client, data);
644 case V4L2_SLICED_CAPTION_525: 643 case V4L2_SLICED_CAPTION_525:
645 if (data->field == 0) 644 if (data->field == 0)
646 return saa7127_set_cc(client, data); 645 return saa7127_set_cc(client, data);
647 return saa7127_set_xds(client, data); 646 return saa7127_set_xds(client, data);
648 default: 647 default:
649 return -EINVAL; 648 return -EINVAL;
650 } 649 }
651 break; 650 break;
652 } 651 }
@@ -662,31 +661,20 @@ static int saa7127_command(struct i2c_client *client,
662 661
663/* ----------------------------------------------------------------------- */ 662/* ----------------------------------------------------------------------- */
664 663
665static struct i2c_driver i2c_driver_saa7127; 664static int saa7127_probe(struct i2c_client *client)
666
667/* ----------------------------------------------------------------------- */
668
669static int saa7127_attach(struct i2c_adapter *adapter, int address, int kind)
670{ 665{
671 struct i2c_client *client;
672 struct saa7127_state *state; 666 struct saa7127_state *state;
673 struct v4l2_sliced_vbi_data vbi = { 0, 0, 0, 0 }; /* set to disabled */ 667 struct v4l2_sliced_vbi_data vbi = { 0, 0, 0, 0 }; /* set to disabled */
674 int read_result = 0; 668 int read_result = 0;
675 669
676 /* Check if the adapter supports the needed features */ 670 /* Check if the adapter supports the needed features */
677 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 671 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
678 return 0; 672 return -EIO;
679
680 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
681 if (client == 0)
682 return -ENOMEM;
683 673
684 client->addr = address;
685 client->adapter = adapter;
686 client->driver = &i2c_driver_saa7127;
687 snprintf(client->name, sizeof(client->name) - 1, "saa7127"); 674 snprintf(client->name, sizeof(client->name) - 1, "saa7127");
688 675
689 v4l_dbg(1, debug, client, "detecting saa7127 client on address 0x%x\n", address << 1); 676 v4l_dbg(1, debug, client, "detecting saa7127 client on address 0x%x\n",
677 client->addr << 1);
690 678
691 /* First test register 0: Bits 5-7 are a version ID (should be 0), 679 /* First test register 0: Bits 5-7 are a version ID (should be 0),
692 and bit 2 should also be 0. 680 and bit 2 should also be 0.
@@ -696,15 +684,12 @@ static int saa7127_attach(struct i2c_adapter *adapter, int address, int kind)
696 if ((saa7127_read(client, 0) & 0xe4) != 0 || 684 if ((saa7127_read(client, 0) & 0xe4) != 0 ||
697 (saa7127_read(client, 0x29) & 0x3f) != 0x1d) { 685 (saa7127_read(client, 0x29) & 0x3f) != 0x1d) {
698 v4l_dbg(1, debug, client, "saa7127 not found\n"); 686 v4l_dbg(1, debug, client, "saa7127 not found\n");
699 kfree(client); 687 return -ENODEV;
700 return 0;
701 } 688 }
702 state = kzalloc(sizeof(struct saa7127_state), GFP_KERNEL); 689 state = kzalloc(sizeof(struct saa7127_state), GFP_KERNEL);
703 690
704 if (state == NULL) { 691 if (state == NULL)
705 kfree(client); 692 return -ENOMEM;
706 return (-ENOMEM);
707 }
708 693
709 i2c_set_clientdata(client, state); 694 i2c_set_clientdata(client, state);
710 695
@@ -718,91 +703,48 @@ static int saa7127_attach(struct i2c_adapter *adapter, int address, int kind)
718 saa7127_set_wss(client, &vbi); 703 saa7127_set_wss(client, &vbi);
719 saa7127_set_cc(client, &vbi); 704 saa7127_set_cc(client, &vbi);
720 saa7127_set_xds(client, &vbi); 705 saa7127_set_xds(client, &vbi);
721 if (test_image == 1) { 706 if (test_image == 1)
722 /* The Encoder has an internal Colorbar generator */ 707 /* The Encoder has an internal Colorbar generator */
723 /* This can be used for debugging */ 708 /* This can be used for debugging */
724 saa7127_set_input_type(client, SAA7127_INPUT_TYPE_TEST_IMAGE); 709 saa7127_set_input_type(client, SAA7127_INPUT_TYPE_TEST_IMAGE);
725 } else { 710 else
726 saa7127_set_input_type(client, SAA7127_INPUT_TYPE_NORMAL); 711 saa7127_set_input_type(client, SAA7127_INPUT_TYPE_NORMAL);
727 }
728 saa7127_set_video_enable(client, 1); 712 saa7127_set_video_enable(client, 1);
729 713
730 /* Detect if it's an saa7129 */ 714 /* Detect if it's an saa7129 */
731 read_result = saa7127_read(client, SAA7129_REG_FADE_KEY_COL2); 715 read_result = saa7127_read(client, SAA7129_REG_FADE_KEY_COL2);
732 saa7127_write(client, SAA7129_REG_FADE_KEY_COL2, 0xaa); 716 saa7127_write(client, SAA7129_REG_FADE_KEY_COL2, 0xaa);
733 if (saa7127_read(client, SAA7129_REG_FADE_KEY_COL2) == 0xaa) { 717 if (saa7127_read(client, SAA7129_REG_FADE_KEY_COL2) == 0xaa) {
734 v4l_info(client, "saa7129 found @ 0x%x (%s)\n", address << 1, adapter->name); 718 v4l_info(client, "saa7129 found @ 0x%x (%s)\n",
719 client->addr << 1, client->adapter->name);
735 saa7127_write(client, SAA7129_REG_FADE_KEY_COL2, read_result); 720 saa7127_write(client, SAA7129_REG_FADE_KEY_COL2, read_result);
736 saa7127_write_inittab(client, saa7129_init_config_extra); 721 saa7127_write_inittab(client, saa7129_init_config_extra);
737 state->ident = V4L2_IDENT_SAA7129; 722 state->ident = V4L2_IDENT_SAA7129;
738 } else { 723 } else {
739 v4l_info(client, "saa7127 found @ 0x%x (%s)\n", address << 1, adapter->name); 724 v4l_info(client, "saa7127 found @ 0x%x (%s)\n",
725 client->addr << 1, client->adapter->name);
740 state->ident = V4L2_IDENT_SAA7127; 726 state->ident = V4L2_IDENT_SAA7127;
741 } 727 }
742
743 i2c_attach_client(client);
744
745 return 0; 728 return 0;
746} 729}
747 730
748/* ----------------------------------------------------------------------- */ 731/* ----------------------------------------------------------------------- */
749 732
750static int saa7127_probe(struct i2c_adapter *adapter) 733static int saa7127_remove(struct i2c_client *client)
751{ 734{
752 if (adapter->class & I2C_CLASS_TV_ANALOG)
753 return i2c_probe(adapter, &addr_data, saa7127_attach);
754 return 0;
755}
756
757/* ----------------------------------------------------------------------- */
758
759static int saa7127_detach(struct i2c_client *client)
760{
761 struct saa7127_state *state = i2c_get_clientdata(client);
762 int err;
763
764 /* Turn off TV output */ 735 /* Turn off TV output */
765 saa7127_set_video_enable(client, 0); 736 saa7127_set_video_enable(client, 0);
766 737 kfree(i2c_get_clientdata(client));
767 err = i2c_detach_client(client);
768
769 if (err) {
770 return err;
771 }
772
773 kfree(state);
774 kfree(client);
775 return 0; 738 return 0;
776} 739}
777 740
778/* ----------------------------------------------------------------------- */ 741/* ----------------------------------------------------------------------- */
779 742
780static struct i2c_driver i2c_driver_saa7127 = { 743static struct v4l2_i2c_driver_data v4l2_i2c_data = {
781 .driver = { 744 .name = "saa7127",
782 .name = "saa7127", 745 .driverid = I2C_DRIVERID_SAA7127,
783 },
784 .id = I2C_DRIVERID_SAA7127,
785 .attach_adapter = saa7127_probe,
786 .detach_client = saa7127_detach,
787 .command = saa7127_command, 746 .command = saa7127_command,
747 .probe = saa7127_probe,
748 .remove = saa7127_remove,
788}; 749};
789 750
790
791/* ----------------------------------------------------------------------- */
792
793static int __init saa7127_init_module(void)
794{
795 return i2c_add_driver(&i2c_driver_saa7127);
796}
797
798/* ----------------------------------------------------------------------- */
799
800static void __exit saa7127_cleanup_module(void)
801{
802 i2c_del_driver(&i2c_driver_saa7127);
803}
804
805/* ----------------------------------------------------------------------- */
806
807module_init(saa7127_init_module);
808module_exit(saa7127_cleanup_module);
diff --git a/drivers/media/video/saa7134/Kconfig b/drivers/media/video/saa7134/Kconfig
index 3aa8cb2b860a..96bc3b1298a2 100644
--- a/drivers/media/video/saa7134/Kconfig
+++ b/drivers/media/video/saa7134/Kconfig
@@ -4,6 +4,7 @@ config VIDEO_SAA7134
4 select VIDEOBUF_DMA_SG 4 select VIDEOBUF_DMA_SG
5 select VIDEO_IR 5 select VIDEO_IR
6 select VIDEO_TUNER 6 select VIDEO_TUNER
7 select VIDEO_TVEEPROM
7 select CRC32 8 select CRC32
8 ---help--- 9 ---help---
9 This is a video4linux driver for Philips SAA713x based 10 This is a video4linux driver for Philips SAA713x based
@@ -23,18 +24,6 @@ config VIDEO_SAA7134_ALSA
23 To compile this driver as a module, choose M here: the 24 To compile this driver as a module, choose M here: the
24 module will be called saa7134-alsa. 25 module will be called saa7134-alsa.
25 26
26config VIDEO_SAA7134_OSS
27 tristate "Philips SAA7134 DMA audio support (OSS, DEPRECATED)"
28 depends on VIDEO_SAA7134 && SOUND_PRIME && !VIDEO_SAA7134_ALSA
29 ---help---
30 This is a video4linux driver for direct (DMA) audio in
31 Philips SAA713x based TV cards using OSS
32
33 This is deprecated in favor of the ALSA module
34
35 To compile this driver as a module, choose M here: the
36 module will be called saa7134-oss.
37
38config VIDEO_SAA7134_DVB 27config VIDEO_SAA7134_DVB
39 tristate "DVB/ATSC Support for saa7134 based TV cards" 28 tristate "DVB/ATSC Support for saa7134 based TV cards"
40 depends on VIDEO_SAA7134 && DVB_CORE 29 depends on VIDEO_SAA7134 && DVB_CORE
diff --git a/drivers/media/video/saa7134/Makefile b/drivers/media/video/saa7134/Makefile
index c85c8a8ec361..9aff937ba7a5 100644
--- a/drivers/media/video/saa7134/Makefile
+++ b/drivers/media/video/saa7134/Makefile
@@ -7,7 +7,6 @@ obj-$(CONFIG_VIDEO_SAA7134) += saa7134.o saa7134-empress.o \
7 saa6752hs.o 7 saa6752hs.o
8 8
9obj-$(CONFIG_VIDEO_SAA7134_ALSA) += saa7134-alsa.o 9obj-$(CONFIG_VIDEO_SAA7134_ALSA) += saa7134-alsa.o
10obj-$(CONFIG_VIDEO_SAA7134_OSS) += saa7134-oss.o
11 10
12obj-$(CONFIG_VIDEO_SAA7134_DVB) += saa7134-dvb.o 11obj-$(CONFIG_VIDEO_SAA7134_DVB) += saa7134-dvb.o
13 12
diff --git a/drivers/media/video/saa7134/saa7134-alsa.c b/drivers/media/video/saa7134/saa7134-alsa.c
index 4878f3067787..ba2531034a91 100644
--- a/drivers/media/video/saa7134/saa7134-alsa.c
+++ b/drivers/media/video/saa7134/saa7134-alsa.c
@@ -1077,24 +1077,14 @@ static int saa7134_alsa_init(void)
1077 struct saa7134_dev *dev = NULL; 1077 struct saa7134_dev *dev = NULL;
1078 struct list_head *list; 1078 struct list_head *list;
1079 1079
1080 if (!saa7134_dmasound_init && !saa7134_dmasound_exit) { 1080 saa7134_dmasound_init = alsa_device_init;
1081 saa7134_dmasound_init = alsa_device_init; 1081 saa7134_dmasound_exit = alsa_device_exit;
1082 saa7134_dmasound_exit = alsa_device_exit;
1083 } else {
1084 printk(KERN_WARNING "saa7134 ALSA: can't load, DMA sound handler already assigned (probably to OSS)\n");
1085 return -EBUSY;
1086 }
1087 1082
1088 printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n"); 1083 printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
1089 1084
1090 list_for_each(list,&saa7134_devlist) { 1085 list_for_each(list,&saa7134_devlist) {
1091 dev = list_entry(list, struct saa7134_dev, devlist); 1086 dev = list_entry(list, struct saa7134_dev, devlist);
1092 if (dev->dmasound.priv_data == NULL) { 1087 alsa_device_init(dev);
1093 alsa_device_init(dev);
1094 } else {
1095 printk(KERN_ERR "saa7134 ALSA: DMA sound is being handled by OSS. ignoring %s\n",dev->name);
1096 return -EBUSY;
1097 }
1098 } 1088 }
1099 1089
1100 if (dev == NULL) 1090 if (dev == NULL)
diff --git a/drivers/media/video/saa7134/saa7134-cards.c b/drivers/media/video/saa7134/saa7134-cards.c
index 98c1b084a716..7d7f383b404f 100644
--- a/drivers/media/video/saa7134/saa7134-cards.c
+++ b/drivers/media/video/saa7134/saa7134-cards.c
@@ -26,6 +26,7 @@
26#include "saa7134-reg.h" 26#include "saa7134-reg.h"
27#include "saa7134.h" 27#include "saa7134.h"
28#include <media/v4l2-common.h> 28#include <media/v4l2-common.h>
29#include <media/tveeprom.h>
29 30
30/* commly used strings */ 31/* commly used strings */
31static char name_mute[] = "mute"; 32static char name_mute[] = "mute";
@@ -349,6 +350,10 @@ struct saa7134_board saa7134_boards[] = {
349 .name = name_radio, 350 .name = name_radio,
350 .amux = LINE2, 351 .amux = LINE2,
351 }, 352 },
353 .mute = {
354 .name = name_mute,
355 .amux = TV,
356 },
352 }, 357 },
353 [SAA7134_BOARD_TVSTATION_RDS] = { 358 [SAA7134_BOARD_TVSTATION_RDS] = {
354 /* Typhoon TV Tuner RDS: Art.Nr. 50694 */ 359 /* Typhoon TV Tuner RDS: Art.Nr. 50694 */
@@ -565,6 +570,10 @@ struct saa7134_board saa7134_boards[] = {
565 .radio = { 570 .radio = {
566 .name = name_radio, 571 .name = name_radio,
567 .amux = LINE2, 572 .amux = LINE2,
573 },
574 .mute = {
575 .name = name_mute,
576 .amux = TV,
568 }, 577 },
569 }, 578 },
570 [SAA7134_BOARD_TYPHOON_90031] = { 579 [SAA7134_BOARD_TYPHOON_90031] = {
@@ -3553,6 +3562,356 @@ struct saa7134_board saa7134_boards[] = {
3553 .tv = 1, 3562 .tv = 1,
3554 }}, 3563 }},
3555 }, 3564 },
3565 [SAA7134_BOARD_BEHOLD_401] = {
3566 .name = "Beholder BeholdTV 401",
3567 .audio_clock = 0x00187de7,
3568 .tuner_type = TUNER_PHILIPS_FQ1216ME,
3569 .radio_type = UNSET,
3570 .tuner_addr = ADDR_UNSET,
3571 .radio_addr = ADDR_UNSET,
3572 .inputs = {{
3573 .name = name_svideo,
3574 .vmux = 8,
3575 .amux = LINE1,
3576 },{
3577 .name = name_comp1,
3578 .vmux = 1,
3579 .amux = LINE1,
3580 },{
3581 .name = name_tv,
3582 .vmux = 3,
3583 .amux = LINE2,
3584 .tv = 1,
3585 }},
3586 .mute = {
3587 .name = name_mute,
3588 .amux = LINE1,
3589 },
3590 },
3591 [SAA7134_BOARD_BEHOLD_403] = {
3592 .name = "Beholder BeholdTV 403",
3593 .audio_clock = 0x00187de7,
3594 .tuner_type = TUNER_PHILIPS_FQ1216ME,
3595 .radio_type = UNSET,
3596 .tuner_addr = ADDR_UNSET,
3597 .radio_addr = ADDR_UNSET,
3598 .inputs = {{
3599 .name = name_svideo,
3600 .vmux = 8,
3601 .amux = LINE1,
3602 },{
3603 .name = name_comp1,
3604 .vmux = 1,
3605 .amux = LINE1,
3606 },{
3607 .name = name_tv,
3608 .vmux = 3,
3609 .amux = LINE2,
3610 .tv = 1,
3611 }},
3612 },
3613 [SAA7134_BOARD_BEHOLD_403FM] = {
3614 .name = "Beholder BeholdTV 403 FM",
3615 .audio_clock = 0x00187de7,
3616 .tuner_type = TUNER_PHILIPS_FQ1216ME,
3617 .radio_type = UNSET,
3618 .tuner_addr = ADDR_UNSET,
3619 .radio_addr = ADDR_UNSET,
3620 .inputs = {{
3621 .name = name_svideo,
3622 .vmux = 8,
3623 .amux = LINE1,
3624 },{
3625 .name = name_comp1,
3626 .vmux = 1,
3627 .amux = LINE1,
3628 },{
3629 .name = name_tv,
3630 .vmux = 3,
3631 .amux = LINE2,
3632 .tv = 1,
3633 }},
3634 .radio = {
3635 .name = name_radio,
3636 .amux = LINE2,
3637 },
3638 },
3639 [SAA7134_BOARD_BEHOLD_405] = {
3640 .name = "Beholder BeholdTV 405",
3641 .audio_clock = 0x00187de7,
3642 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
3643 .radio_type = UNSET,
3644 .tuner_addr = ADDR_UNSET,
3645 .radio_addr = ADDR_UNSET,
3646 .tda9887_conf = TDA9887_PRESENT,
3647 .inputs = {{
3648 .name = name_svideo,
3649 .vmux = 8,
3650 .amux = LINE1,
3651 },{
3652 .name = name_comp1,
3653 .vmux = 3,
3654 .amux = LINE1,
3655 },{
3656 .name = name_tv,
3657 .vmux = 3,
3658 .amux = LINE2,
3659 .tv = 1,
3660 }},
3661 },
3662 [SAA7134_BOARD_BEHOLD_405FM] = {
3663 /* Sergey <skiv@orel.ru> */
3664 .name = "Beholder BeholdTV 405 FM",
3665 .audio_clock = 0x00187de7,
3666 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
3667 .radio_type = UNSET,
3668 .tuner_addr = ADDR_UNSET,
3669 .radio_addr = ADDR_UNSET,
3670 .tda9887_conf = TDA9887_PRESENT,
3671 .inputs = {{
3672 .name = name_svideo,
3673 .vmux = 8,
3674 .amux = LINE1,
3675 },{
3676 .name = name_comp1,
3677 .vmux = 3,
3678 .amux = LINE1,
3679 },{
3680 .name = name_tv,
3681 .vmux = 3,
3682 .amux = LINE2,
3683 .tv = 1,
3684 }},
3685 .radio = {
3686 .name = name_radio,
3687 .amux = LINE2,
3688 },
3689 },
3690 [SAA7134_BOARD_BEHOLD_407] = {
3691 .name = "Beholder BeholdTV 407",
3692 .audio_clock = 0x00187de7,
3693 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
3694 .radio_type = UNSET,
3695 .tuner_addr = ADDR_UNSET,
3696 .radio_addr = ADDR_UNSET,
3697 .tda9887_conf = TDA9887_PRESENT,
3698 .gpiomask = 0xc0c000,
3699 .inputs = {{
3700 .name = name_svideo,
3701 .vmux = 8,
3702 .amux = LINE1,
3703 .gpio = 0xc0c000,
3704 },{
3705 .name = name_comp1,
3706 .vmux = 1,
3707 .amux = LINE1,
3708 .gpio = 0xc0c000,
3709 },{
3710 .name = name_tv,
3711 .vmux = 3,
3712 .amux = TV,
3713 .tv = 1,
3714 .gpio = 0xc0c000,
3715 }},
3716 },
3717 [SAA7134_BOARD_BEHOLD_407FM] = {
3718 .name = "Beholder BeholdTV 407 FM",
3719 .audio_clock = 0x00187de7,
3720 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
3721 .radio_type = UNSET,
3722 .tuner_addr = ADDR_UNSET,
3723 .radio_addr = ADDR_UNSET,
3724 .tda9887_conf = TDA9887_PRESENT,
3725 .gpiomask = 0xc0c000,
3726 .inputs = {{
3727 .name = name_svideo,
3728 .vmux = 8,
3729 .amux = LINE1,
3730 .gpio = 0xc0c000,
3731 },{
3732 .name = name_comp1,
3733 .vmux = 1,
3734 .amux = LINE1,
3735 .gpio = 0xc0c000,
3736 },{
3737 .name = name_tv,
3738 .vmux = 3,
3739 .amux = TV,
3740 .tv = 1,
3741 .gpio = 0xc0c000,
3742 }},
3743 .radio = {
3744 .name = name_radio,
3745 .amux = LINE2,
3746 .gpio = 0xc0c000,
3747 },
3748 },
3749 [SAA7134_BOARD_BEHOLD_409] = {
3750 .name = "Beholder BeholdTV 409",
3751 .audio_clock = 0x00187de7,
3752 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
3753 .radio_type = UNSET,
3754 .tuner_addr = ADDR_UNSET,
3755 .radio_addr = ADDR_UNSET,
3756 .tda9887_conf = TDA9887_PRESENT,
3757 .inputs = {{
3758 .name = name_tv,
3759 .vmux = 3,
3760 .amux = TV,
3761 .tv = 1,
3762 },{
3763 .name = name_comp1,
3764 .vmux = 1,
3765 .amux = LINE1,
3766 },{
3767 .name = name_svideo,
3768 .vmux = 8,
3769 .amux = LINE1,
3770 }},
3771 },
3772 [SAA7134_BOARD_BEHOLD_505FM] = {
3773 .name = "Beholder BeholdTV 505 FM/RDS",
3774 .audio_clock = 0x00200000,
3775 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
3776 .radio_type = UNSET,
3777 .tuner_addr = ADDR_UNSET,
3778 .radio_addr = ADDR_UNSET,
3779 .tda9887_conf = TDA9887_PRESENT,
3780 .inputs = {{
3781 .name = name_tv,
3782 .vmux = 3,
3783 .amux = LINE2,
3784 .tv = 1,
3785 },{
3786 .name = name_comp1,
3787 .vmux = 1,
3788 .amux = LINE1,
3789 },{
3790 .name = name_svideo,
3791 .vmux = 8,
3792 .amux = LINE1,
3793 }},
3794 .mute = {
3795 .name = name_mute,
3796 .amux = LINE1,
3797 },
3798 .radio = {
3799 .name = name_radio,
3800 .amux = LINE2,
3801 },
3802 },
3803 [SAA7134_BOARD_BEHOLD_507_9FM] = {
3804 .name = "Beholder BeholdTV 507 FM/RDS / BeholdTV 509 FM",
3805 .audio_clock = 0x00187de7,
3806 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
3807 .radio_type = UNSET,
3808 .tuner_addr = ADDR_UNSET,
3809 .radio_addr = ADDR_UNSET,
3810 .tda9887_conf = TDA9887_PRESENT,
3811 .inputs = {{
3812 .name = name_tv,
3813 .vmux = 3,
3814 .amux = TV,
3815 .tv = 1,
3816 },{
3817 .name = name_comp1,
3818 .vmux = 1,
3819 .amux = LINE1,
3820 },{
3821 .name = name_svideo,
3822 .vmux = 8,
3823 .amux = LINE1,
3824 }},
3825 .radio = {
3826 .name = name_radio,
3827 .amux = LINE2,
3828 },
3829 },
3830 [SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM] = {
3831 .name = "Beholder BeholdTV Columbus TVFM",
3832 .audio_clock = 0x00187de7,
3833 .tuner_type = TUNER_ALPS_TSBE5_PAL,
3834 .radio_type = UNSET,
3835 .tuner_addr = ADDR_UNSET,
3836 .radio_addr = ADDR_UNSET,
3837 .tda9887_conf = TDA9887_PRESENT,
3838 .inputs = {{
3839 .name = name_tv,
3840 .vmux = 3,
3841 .amux = TV,
3842 .tv = 1,
3843 },{
3844 .name = name_comp1,
3845 .vmux = 1,
3846 .amux = LINE1,
3847 },{
3848 .name = name_svideo,
3849 .vmux = 8,
3850 .amux = LINE1,
3851 }},
3852 .radio = {
3853 .name = name_radio,
3854 .amux = LINE2,
3855 },
3856 },
3857 [SAA7134_BOARD_BEHOLD_607_9FM] = {
3858 /* Andrey Melnikoff <temnota@kmv.ru> */
3859 .name = "Beholder BeholdTV 607 / BeholdTV 609",
3860 .audio_clock = 0x00187de7,
3861 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
3862 .radio_type = UNSET,
3863 .tuner_addr = ADDR_UNSET,
3864 .radio_addr = ADDR_UNSET,
3865 .tda9887_conf = TDA9887_PRESENT,
3866 .inputs = {{
3867 .name = name_tv,
3868 .vmux = 3,
3869 .amux = TV,
3870 .tv = 1,
3871 },{
3872 .name = name_comp1,
3873 .vmux = 1,
3874 .amux = LINE1,
3875 },{
3876 .name = name_svideo,
3877 .vmux = 8,
3878 .amux = LINE1,
3879 }},
3880 .radio = {
3881 .name = name_radio,
3882 .amux = LINE2,
3883 },
3884 },
3885 [SAA7134_BOARD_BEHOLD_M6] = {
3886 /* Igor Kuznetsov <igk@igk.ru> */
3887 /* Andrey Melnikoff <temnota@kmv.ru> */
3888 .name = "Beholder BeholdTV M6 / BeholdTV M6 Extra",
3889 .audio_clock = 0x00187de7,
3890 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
3891 .radio_type = UNSET,
3892 .tuner_addr = ADDR_UNSET,
3893 .radio_addr = ADDR_UNSET,
3894 .tda9887_conf = TDA9887_PRESENT,
3895 .inputs = {{
3896 .name = name_tv,
3897 .vmux = 3,
3898 .amux = TV,
3899 .tv = 1,
3900 },{
3901 .name = name_comp1,
3902 .vmux = 1,
3903 .amux = LINE1,
3904 },{
3905 .name = name_svideo,
3906 .vmux = 8,
3907 .amux = LINE1,
3908 }},
3909 .radio = {
3910 .name = name_radio,
3911 .amux = LINE2,
3912 },
3913 .mpeg = SAA7134_MPEG_EMPRESS,
3914 },
3556}; 3915};
3557 3916
3558const unsigned int saa7134_bcount = ARRAY_SIZE(saa7134_boards); 3917const unsigned int saa7134_bcount = ARRAY_SIZE(saa7134_boards);
@@ -4033,7 +4392,13 @@ struct pci_device_id saa7134_pci_tbl[] = {
4033 .vendor = PCI_VENDOR_ID_PHILIPS, 4392 .vendor = PCI_VENDOR_ID_PHILIPS,
4034 .device = PCI_DEVICE_ID_PHILIPS_SAA7133, 4393 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4035 .subvendor = 0x1462, 4394 .subvendor = 0x1462,
4036 .subdevice = 0x6231, 4395 .subdevice = 0x6231, /* tda8275a, ks003 IR */
4396 .driver_data = SAA7134_BOARD_MSI_TVATANYWHERE_PLUS,
4397 },{
4398 .vendor = PCI_VENDOR_ID_PHILIPS,
4399 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4400 .subvendor = 0x1462,
4401 .subdevice = 0x8624, /* tda8275, ks003 IR */
4037 .driver_data = SAA7134_BOARD_MSI_TVATANYWHERE_PLUS, 4402 .driver_data = SAA7134_BOARD_MSI_TVATANYWHERE_PLUS,
4038 },{ 4403 },{
4039 .vendor = PCI_VENDOR_ID_PHILIPS, 4404 .vendor = PCI_VENDOR_ID_PHILIPS,
@@ -4207,11 +4572,41 @@ struct pci_device_id saa7134_pci_tbl[] = {
4207 .vendor = PCI_VENDOR_ID_PHILIPS, 4572 .vendor = PCI_VENDOR_ID_PHILIPS,
4208 .device = PCI_DEVICE_ID_PHILIPS_SAA7133, 4573 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4209 .subvendor = 0x0070, 4574 .subvendor = 0x0070,
4575 .subdevice = 0x6700,
4576 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
4577 },{
4578 .vendor = PCI_VENDOR_ID_PHILIPS,
4579 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4580 .subvendor = 0x0070,
4210 .subdevice = 0x6701, 4581 .subdevice = 0x6701,
4211 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110, 4582 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
4212 },{ 4583 },{
4213 .vendor = PCI_VENDOR_ID_PHILIPS, 4584 .vendor = PCI_VENDOR_ID_PHILIPS,
4214 .device = PCI_DEVICE_ID_PHILIPS_SAA7133, 4585 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4586 .subvendor = 0x0070,
4587 .subdevice = 0x6702,
4588 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
4589 },{
4590 .vendor = PCI_VENDOR_ID_PHILIPS,
4591 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4592 .subvendor = 0x0070,
4593 .subdevice = 0x6703,
4594 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
4595 },{
4596 .vendor = PCI_VENDOR_ID_PHILIPS,
4597 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4598 .subvendor = 0x0070,
4599 .subdevice = 0x6704,
4600 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
4601 },{
4602 .vendor = PCI_VENDOR_ID_PHILIPS,
4603 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4604 .subvendor = 0x0070,
4605 .subdevice = 0x6705,
4606 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
4607 },{
4608 .vendor = PCI_VENDOR_ID_PHILIPS,
4609 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4215 .subvendor = 0x153b, 4610 .subvendor = 0x153b,
4216 .subdevice = 0x1172, 4611 .subdevice = 0x1172,
4217 .driver_data = SAA7134_BOARD_CINERGY_HT_PCMCIA, 4612 .driver_data = SAA7134_BOARD_CINERGY_HT_PCMCIA,
@@ -4289,6 +4684,162 @@ struct pci_device_id saa7134_pci_tbl[] = {
4289 .driver_data = SAA7134_BOARD_AVERMEDIA_SUPER_007, 4684 .driver_data = SAA7134_BOARD_AVERMEDIA_SUPER_007,
4290 },{ 4685 },{
4291 .vendor = PCI_VENDOR_ID_PHILIPS, 4686 .vendor = PCI_VENDOR_ID_PHILIPS,
4687 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
4688 .subvendor = 0x0000,
4689 .subdevice = 0x4016,
4690 .driver_data = SAA7134_BOARD_BEHOLD_401,
4691 },{
4692 .vendor = PCI_VENDOR_ID_PHILIPS,
4693 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
4694 .subvendor = 0x0000,
4695 .subdevice = 0x4036,
4696 .driver_data = SAA7134_BOARD_BEHOLD_403,
4697 },{
4698 .vendor = PCI_VENDOR_ID_PHILIPS,
4699 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
4700 .subvendor = 0x0000,
4701 .subdevice = 0x4037,
4702 .driver_data = SAA7134_BOARD_BEHOLD_403FM,
4703 },{
4704 .vendor = PCI_VENDOR_ID_PHILIPS,
4705 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
4706 .subvendor = 0x0000,
4707 .subdevice = 0x4050,
4708 .driver_data = SAA7134_BOARD_BEHOLD_405,
4709 },{
4710 .vendor = PCI_VENDOR_ID_PHILIPS,
4711 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
4712 .subvendor = 0x0000,
4713 .subdevice = 0x4051,
4714 .driver_data = SAA7134_BOARD_BEHOLD_405FM,
4715 },{
4716 .vendor = PCI_VENDOR_ID_PHILIPS,
4717 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
4718 .subvendor = 0x0000,
4719 .subdevice = 0x4070,
4720 .driver_data = SAA7134_BOARD_BEHOLD_407,
4721 },{
4722 .vendor = PCI_VENDOR_ID_PHILIPS,
4723 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
4724 .subvendor = 0x0000,
4725 .subdevice = 0x4071,
4726 .driver_data = SAA7134_BOARD_BEHOLD_407FM,
4727 },{
4728 .vendor = PCI_VENDOR_ID_PHILIPS,
4729 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4730 .subvendor = 0x0000,
4731 .subdevice = 0x4090,
4732 .driver_data = SAA7134_BOARD_BEHOLD_409,
4733 },{
4734 .vendor = PCI_VENDOR_ID_PHILIPS,
4735 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
4736 .subvendor = 0x0000,
4737 .subdevice = 0x5051,
4738 .driver_data = SAA7134_BOARD_BEHOLD_505FM,
4739 },{
4740 .vendor = PCI_VENDOR_ID_PHILIPS,
4741 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
4742 .subvendor = 0x0000,
4743 .subdevice = 0x505B,
4744 .driver_data = SAA7134_BOARD_BEHOLD_505FM,
4745 },{
4746 .vendor = PCI_VENDOR_ID_PHILIPS,
4747 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
4748 .subvendor = 0x5ace,
4749 .subdevice = 0x5050,
4750 .driver_data = SAA7134_BOARD_BEHOLD_505FM,
4751 },{
4752 .vendor = PCI_VENDOR_ID_PHILIPS,
4753 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4754 .subvendor = 0x0000,
4755 .subdevice = 0x5071,
4756 .driver_data = SAA7134_BOARD_BEHOLD_507_9FM,
4757 },{
4758 .vendor = PCI_VENDOR_ID_PHILIPS,
4759 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4760 .subvendor = 0x0000,
4761 .subdevice = 0x507B,
4762 .driver_data = SAA7134_BOARD_BEHOLD_507_9FM,
4763 },{
4764 .vendor = PCI_VENDOR_ID_PHILIPS,
4765 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
4766 .subvendor = 0x5ace,
4767 .subdevice = 0x5070,
4768 .driver_data = SAA7134_BOARD_BEHOLD_507_9FM,
4769 },{
4770 .vendor = PCI_VENDOR_ID_PHILIPS,
4771 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4772 .subvendor = 0x5ace,
4773 .subdevice = 0x5090,
4774 .driver_data = SAA7134_BOARD_BEHOLD_507_9FM,
4775 },{
4776 .vendor = PCI_VENDOR_ID_PHILIPS,
4777 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4778 .subvendor = 0x0000,
4779 .subdevice = 0x5201,
4780 .driver_data = SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM,
4781 },{
4782 .vendor = PCI_VENDOR_ID_PHILIPS,
4783 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
4784 .subvendor = 0x5ace,
4785 .subdevice = 0x6070,
4786 .driver_data = SAA7134_BOARD_BEHOLD_607_9FM,
4787 },{
4788 .vendor = PCI_VENDOR_ID_PHILIPS,
4789 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
4790 .subvendor = 0x5ace,
4791 .subdevice = 0x6071,
4792 .driver_data = SAA7134_BOARD_BEHOLD_607_9FM,
4793 },{
4794 .vendor = PCI_VENDOR_ID_PHILIPS,
4795 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
4796 .subvendor = 0x5ace,
4797 .subdevice = 0x6072,
4798 .driver_data = SAA7134_BOARD_BEHOLD_607_9FM,
4799 },{
4800 .vendor = PCI_VENDOR_ID_PHILIPS,
4801 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
4802 .subvendor = 0x5ace,
4803 .subdevice = 0x6073,
4804 .driver_data = SAA7134_BOARD_BEHOLD_607_9FM,
4805 },{
4806 .vendor = PCI_VENDOR_ID_PHILIPS,
4807 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4808 .subvendor = 0x5ace,
4809 .subdevice = 0x6090,
4810 .driver_data = SAA7134_BOARD_BEHOLD_607_9FM,
4811 },{
4812 .vendor = PCI_VENDOR_ID_PHILIPS,
4813 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4814 .subvendor = 0x5ace,
4815 .subdevice = 0x6091,
4816 .driver_data = SAA7134_BOARD_BEHOLD_607_9FM,
4817 },{
4818 .vendor = PCI_VENDOR_ID_PHILIPS,
4819 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4820 .subvendor = 0x5ace,
4821 .subdevice = 0x6092,
4822 .driver_data = SAA7134_BOARD_BEHOLD_607_9FM,
4823 },{
4824 .vendor = PCI_VENDOR_ID_PHILIPS,
4825 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4826 .subvendor = 0x5ace,
4827 .subdevice = 0x6093,
4828 .driver_data = SAA7134_BOARD_BEHOLD_607_9FM,
4829 },{
4830 .vendor = PCI_VENDOR_ID_PHILIPS,
4831 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4832 .subvendor = 0x5ace,
4833 .subdevice = 0x6190,
4834 .driver_data = SAA7134_BOARD_BEHOLD_M6,
4835 },{
4836 .vendor = PCI_VENDOR_ID_PHILIPS,
4837 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4838 .subvendor = 0x5ace,
4839 .subdevice = 0x6193,
4840 .driver_data = SAA7134_BOARD_BEHOLD_M6,
4841 },{
4842 .vendor = PCI_VENDOR_ID_PHILIPS,
4292 .device = PCI_DEVICE_ID_PHILIPS_SAA7133, 4843 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
4293 .subvendor = 0x4e42, 4844 .subvendor = 0x4e42,
4294 .subdevice = 0x3502, 4845 .subdevice = 0x3502,
@@ -4351,6 +4902,34 @@ static void board_flyvideo(struct saa7134_dev *dev)
4351 4902
4352/* ----------------------------------------------------------- */ 4903/* ----------------------------------------------------------- */
4353 4904
4905static void hauppauge_eeprom(struct saa7134_dev *dev, u8 *eeprom_data)
4906{
4907 struct tveeprom tv;
4908
4909 tveeprom_hauppauge_analog(&dev->i2c_client, &tv, eeprom_data);
4910
4911 /* Make sure we support the board model */
4912 switch (tv.model) {
4913 case 67019: /* WinTV-HVR1110 (Retail, IR Blaster, hybrid, FM, SVid/Comp, 3.5mm audio in) */
4914 case 67109: /* WinTV-HVR1000 (Retail, IR Receive, analog, no FM, SVid/Comp, 3.5mm audio in) */
4915 case 67559: /* WinTV-HVR1110 (OEM, no IR, hybrid, FM, SVid/Comp, RCA aud) */
4916 case 67569: /* WinTV-HVR1110 (OEM, no IR, hybrid, FM) */
4917 case 67579: /* WinTV-HVR1110 (OEM, no IR, hybrid, no FM) */
4918 case 67589: /* WinTV-HVR1110 (OEM, no IR, hybrid, no FM, SVid/Comp, RCA aud) */
4919 case 67599: /* WinTV-HVR1110 (OEM, no IR, hybrid, no FM, SVid/Comp, RCA aud) */
4920 break;
4921 default:
4922 printk(KERN_WARNING "%s: warning: "
4923 "unknown hauppauge model #%d\n", dev->name, tv.model);
4924 break;
4925 }
4926
4927 printk(KERN_INFO "%s: hauppauge eeprom: model=%d\n",
4928 dev->name, tv.model);
4929}
4930
4931/* ----------------------------------------------------------- */
4932
4354int saa7134_board_init1(struct saa7134_dev *dev) 4933int saa7134_board_init1(struct saa7134_dev *dev)
4355{ 4934{
4356 /* Always print gpio, often manufacturers encode tuner type and other info. */ 4935 /* Always print gpio, often manufacturers encode tuner type and other info. */
@@ -4406,6 +4985,16 @@ int saa7134_board_init1(struct saa7134_dev *dev)
4406 case SAA7134_BOARD_ENCORE_ENLTV: 4985 case SAA7134_BOARD_ENCORE_ENLTV:
4407 case SAA7134_BOARD_ENCORE_ENLTV_FM: 4986 case SAA7134_BOARD_ENCORE_ENLTV_FM:
4408 case SAA7134_BOARD_10MOONSTVMASTER3: 4987 case SAA7134_BOARD_10MOONSTVMASTER3:
4988 case SAA7134_BOARD_BEHOLD_401:
4989 case SAA7134_BOARD_BEHOLD_403:
4990 case SAA7134_BOARD_BEHOLD_403FM:
4991 case SAA7134_BOARD_BEHOLD_405:
4992 case SAA7134_BOARD_BEHOLD_405FM:
4993 case SAA7134_BOARD_BEHOLD_407:
4994 case SAA7134_BOARD_BEHOLD_407FM:
4995 case SAA7134_BOARD_BEHOLD_409:
4996 case SAA7134_BOARD_BEHOLD_505FM:
4997 case SAA7134_BOARD_BEHOLD_507_9FM:
4409 dev->has_remote = SAA7134_REMOTE_GPIO; 4998 dev->has_remote = SAA7134_REMOTE_GPIO;
4410 break; 4999 break;
4411 case SAA7134_BOARD_FLYDVBS_LR300: 5000 case SAA7134_BOARD_FLYDVBS_LR300:
@@ -4445,6 +5034,7 @@ int saa7134_board_init1(struct saa7134_dev *dev)
4445 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x08000000, 0x00000000); 5034 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x08000000, 0x00000000);
4446 break; 5035 break;
4447 case SAA7134_BOARD_AVERMEDIA_CARDBUS: 5036 case SAA7134_BOARD_AVERMEDIA_CARDBUS:
5037 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
4448 /* power-up tuner chip */ 5038 /* power-up tuner chip */
4449 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0xffffffff, 0xffffffff); 5039 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0xffffffff, 0xffffffff);
4450 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0xffffffff, 0xffffffff); 5040 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0xffffffff, 0xffffffff);
@@ -4466,6 +5056,8 @@ int saa7134_board_init1(struct saa7134_dev *dev)
4466 case SAA7134_BOARD_PINNACLE_PCTV_310i: 5056 case SAA7134_BOARD_PINNACLE_PCTV_310i:
4467 case SAA7134_BOARD_UPMOST_PURPLE_TV: 5057 case SAA7134_BOARD_UPMOST_PURPLE_TV:
4468 case SAA7134_BOARD_HAUPPAUGE_HVR1110: 5058 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
5059 case SAA7134_BOARD_BEHOLD_607_9FM:
5060 case SAA7134_BOARD_BEHOLD_M6:
4469 dev->has_remote = SAA7134_REMOTE_I2C; 5061 dev->has_remote = SAA7134_REMOTE_I2C;
4470 break; 5062 break;
4471 case SAA7134_BOARD_AVERMEDIA_A169_B: 5063 case SAA7134_BOARD_AVERMEDIA_A169_B:
@@ -4477,6 +5069,7 @@ int saa7134_board_init1(struct saa7134_dev *dev)
4477 break; 5069 break;
4478 case SAA7134_BOARD_AVERMEDIA_M102: 5070 case SAA7134_BOARD_AVERMEDIA_M102:
4479 /* enable tuner */ 5071 /* enable tuner */
5072 dev->has_remote = SAA7134_REMOTE_GPIO;
4480 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x8c040007, 0x8c040007); 5073 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x8c040007, 0x8c040007);
4481 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0c0007cd, 0x0c0007cd); 5074 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0c0007cd, 0x0c0007cd);
4482 break; 5075 break;
@@ -4570,8 +5163,17 @@ int saa7134_board_init2(struct saa7134_dev *dev)
4570 5163
4571 printk(KERN_INFO "%s Tuner type is %d\n", dev->name, dev->tuner_type); 5164 printk(KERN_INFO "%s Tuner type is %d\n", dev->name, dev->tuner_type);
4572 if (dev->tuner_type == TUNER_PHILIPS_FMD1216ME_MK3) { 5165 if (dev->tuner_type == TUNER_PHILIPS_FMD1216ME_MK3) {
4573 dev->tda9887_conf = TDA9887_PRESENT | TDA9887_PORT1_ACTIVE | TDA9887_PORT2_ACTIVE; 5166 struct v4l2_priv_tun_config tda9887_cfg;
4574 saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG, &dev->tda9887_conf); 5167
5168 tda9887_cfg.tuner = TUNER_TDA9887;
5169 tda9887_cfg.priv = &dev->tda9887_conf;
5170
5171 dev->tda9887_conf = TDA9887_PRESENT |
5172 TDA9887_PORT1_ACTIVE |
5173 TDA9887_PORT2_ACTIVE;
5174
5175 saa7134_i2c_call_clients(dev, TUNER_SET_CONFIG,
5176 &tda9887_cfg);
4575 } 5177 }
4576 5178
4577 tun_setup.mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV; 5179 tun_setup.mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
@@ -4601,7 +5203,6 @@ int saa7134_board_init2(struct saa7134_dev *dev)
4601 break; 5203 break;
4602 case SAA7134_BOARD_PHILIPS_TIGER: 5204 case SAA7134_BOARD_PHILIPS_TIGER:
4603 case SAA7134_BOARD_PHILIPS_TIGER_S: 5205 case SAA7134_BOARD_PHILIPS_TIGER_S:
4604 case SAA7134_BOARD_AVERMEDIA_SUPER_007:
4605 { 5206 {
4606 u8 data[] = { 0x3c, 0x33, 0x60}; 5207 u8 data[] = { 0x3c, 0x33, 0x60};
4607 struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)}; 5208 struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
@@ -4622,13 +5223,16 @@ int saa7134_board_init2(struct saa7134_dev *dev)
4622 i2c_transfer(&dev->i2c_adap, &msg, 1); 5223 i2c_transfer(&dev->i2c_adap, &msg, 1);
4623 } 5224 }
4624 break; 5225 break;
5226 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
5227 hauppauge_eeprom(dev, dev->eedata+0x80);
5228 /* break intentionally omitted */
4625 case SAA7134_BOARD_PINNACLE_PCTV_310i: 5229 case SAA7134_BOARD_PINNACLE_PCTV_310i:
4626 case SAA7134_BOARD_KWORLD_DVBT_210: 5230 case SAA7134_BOARD_KWORLD_DVBT_210:
4627 case SAA7134_BOARD_TEVION_DVBT_220RF: 5231 case SAA7134_BOARD_TEVION_DVBT_220RF:
4628 case SAA7134_BOARD_ASUSTeK_P7131_DUAL: 5232 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
4629 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA: 5233 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
4630 case SAA7134_BOARD_MEDION_MD8800_QUADRO: 5234 case SAA7134_BOARD_MEDION_MD8800_QUADRO:
4631 case SAA7134_BOARD_HAUPPAUGE_HVR1110: 5235 case SAA7134_BOARD_AVERMEDIA_SUPER_007:
4632 /* this is a hybrid board, initialize to analog mode 5236 /* this is a hybrid board, initialize to analog mode
4633 * and configure firmware eeprom address 5237 * and configure firmware eeprom address
4634 */ 5238 */
diff --git a/drivers/media/video/saa7134/saa7134-core.c b/drivers/media/video/saa7134/saa7134-core.c
index 4f0a9157ecb1..52baa4f7f7dd 100644
--- a/drivers/media/video/saa7134/saa7134-core.c
+++ b/drivers/media/video/saa7134/saa7134-core.c
@@ -294,7 +294,7 @@ void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf)
294 videobuf_waiton(&buf->vb,0,0); 294 videobuf_waiton(&buf->vb,0,0);
295 videobuf_dma_unmap(q, dma); 295 videobuf_dma_unmap(q, dma);
296 videobuf_dma_free(dma); 296 videobuf_dma_free(dma);
297 buf->vb.state = STATE_NEEDS_INIT; 297 buf->vb.state = VIDEOBUF_NEEDS_INIT;
298} 298}
299 299
300/* ------------------------------------------------------------------ */ 300/* ------------------------------------------------------------------ */
@@ -313,7 +313,7 @@ int saa7134_buffer_queue(struct saa7134_dev *dev,
313 buf->activate(dev,buf,NULL); 313 buf->activate(dev,buf,NULL);
314 } else if (list_empty(&q->queue)) { 314 } else if (list_empty(&q->queue)) {
315 list_add_tail(&buf->vb.queue,&q->queue); 315 list_add_tail(&buf->vb.queue,&q->queue);
316 buf->vb.state = STATE_QUEUED; 316 buf->vb.state = VIDEOBUF_QUEUED;
317 } else { 317 } else {
318 next = list_entry(q->queue.next,struct saa7134_buf, 318 next = list_entry(q->queue.next,struct saa7134_buf,
319 vb.queue); 319 vb.queue);
@@ -322,7 +322,7 @@ int saa7134_buffer_queue(struct saa7134_dev *dev,
322 } 322 }
323 } else { 323 } else {
324 list_add_tail(&buf->vb.queue,&q->queue); 324 list_add_tail(&buf->vb.queue,&q->queue);
325 buf->vb.state = STATE_QUEUED; 325 buf->vb.state = VIDEOBUF_QUEUED;
326 } 326 }
327 return 0; 327 return 0;
328} 328}
@@ -387,7 +387,7 @@ void saa7134_buffer_timeout(unsigned long data)
387 try to start over with the next one. */ 387 try to start over with the next one. */
388 if (q->curr) { 388 if (q->curr) {
389 dprintk("timeout on %p\n",q->curr); 389 dprintk("timeout on %p\n",q->curr);
390 saa7134_buffer_finish(dev,q,STATE_ERROR); 390 saa7134_buffer_finish(dev,q,VIDEOBUF_ERROR);
391 } 391 }
392 saa7134_buffer_next(dev,q); 392 saa7134_buffer_next(dev,q);
393 spin_unlock_irqrestore(&dev->slock,flags); 393 spin_unlock_irqrestore(&dev->slock,flags);
@@ -395,8 +395,8 @@ void saa7134_buffer_timeout(unsigned long data)
395 395
396/* resends a current buffer in queue after resume */ 396/* resends a current buffer in queue after resume */
397 397
398int saa7134_buffer_requeue(struct saa7134_dev *dev, 398static int saa7134_buffer_requeue(struct saa7134_dev *dev,
399 struct saa7134_dmaqueue *q) 399 struct saa7134_dmaqueue *q)
400{ 400{
401 struct saa7134_buf *buf, *next; 401 struct saa7134_buf *buf, *next;
402 402
@@ -834,6 +834,7 @@ static struct video_device *vdev_init(struct saa7134_dev *dev,
834 vfd->minor = -1; 834 vfd->minor = -1;
835 vfd->dev = &dev->pci->dev; 835 vfd->dev = &dev->pci->dev;
836 vfd->release = video_device_release; 836 vfd->release = video_device_release;
837 vfd->debug = video_debug;
837 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", 838 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
838 dev->name, type, saa7134_boards[dev->board].name); 839 dev->name, type, saa7134_boards[dev->board].name);
839 return vfd; 840 return vfd;
@@ -1052,7 +1053,9 @@ static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
1052 printk(KERN_INFO "%s: registered device video%d [v4l2]\n", 1053 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
1053 dev->name,dev->video_dev->minor & 0x1f); 1054 dev->name,dev->video_dev->minor & 0x1f);
1054 1055
1055 dev->vbi_dev = vdev_init(dev,&saa7134_vbi_template,"vbi"); 1056 dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
1057 dev->vbi_dev->type = VID_TYPE_TUNER | VID_TYPE_TELETEXT;
1058
1056 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI, 1059 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1057 vbi_nr[dev->nr]); 1060 vbi_nr[dev->nr]);
1058 if (err < 0) 1061 if (err < 0)
@@ -1181,8 +1184,13 @@ static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1181 saa_writel(SAA7134_IRQ2, 0); 1184 saa_writel(SAA7134_IRQ2, 0);
1182 saa_writel(SAA7134_MAIN_CTRL, 0); 1185 saa_writel(SAA7134_MAIN_CTRL, 0);
1183 1186
1184 synchronize_irq(pci_dev->irq);
1185 dev->insuspend = 1; 1187 dev->insuspend = 1;
1188 synchronize_irq(pci_dev->irq);
1189
1190 /* ACK interrupts once more, just in case,
1191 since the IRQ handler won't ack them anymore*/
1192
1193 saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1186 1194
1187 /* Disable timeout timers - if we have active buffers, we will 1195 /* Disable timeout timers - if we have active buffers, we will
1188 fill them on resume*/ 1196 fill them on resume*/
@@ -1194,10 +1202,10 @@ static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1194 if (dev->remote) 1202 if (dev->remote)
1195 saa7134_ir_stop(dev); 1203 saa7134_ir_stop(dev);
1196 1204
1197 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1198 pci_save_state(pci_dev); 1205 pci_save_state(pci_dev);
1206 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1199 1207
1200 return 0; 1208 return 0;
1201} 1209}
1202 1210
1203static int saa7134_resume(struct pci_dev *pci_dev) 1211static int saa7134_resume(struct pci_dev *pci_dev)
@@ -1205,8 +1213,8 @@ static int saa7134_resume(struct pci_dev *pci_dev)
1205 struct saa7134_dev *dev = pci_get_drvdata(pci_dev); 1213 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1206 unsigned long flags; 1214 unsigned long flags;
1207 1215
1208 pci_restore_state(pci_dev);
1209 pci_set_power_state(pci_dev, PCI_D0); 1216 pci_set_power_state(pci_dev, PCI_D0);
1217 pci_restore_state(pci_dev);
1210 1218
1211 /* Do things that are done in saa7134_initdev , 1219 /* Do things that are done in saa7134_initdev ,
1212 except of initializing memory structures.*/ 1220 except of initializing memory structures.*/
@@ -1222,6 +1230,7 @@ static int saa7134_resume(struct pci_dev *pci_dev)
1222 saa7134_ir_start(dev, dev->remote); 1230 saa7134_ir_start(dev, dev->remote);
1223 saa7134_hw_enable1(dev); 1231 saa7134_hw_enable1(dev);
1224 1232
1233 msleep(100);
1225 1234
1226 saa7134_board_init2(dev); 1235 saa7134_board_init2(dev);
1227 1236
@@ -1229,10 +1238,13 @@ static int saa7134_resume(struct pci_dev *pci_dev)
1229 saa7134_set_tvnorm_hw(dev); 1238 saa7134_set_tvnorm_hw(dev);
1230 saa7134_tvaudio_setmute(dev); 1239 saa7134_tvaudio_setmute(dev);
1231 saa7134_tvaudio_setvolume(dev, dev->ctl_volume); 1240 saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1241 saa7134_tvaudio_init(dev);
1232 saa7134_tvaudio_do_scan(dev); 1242 saa7134_tvaudio_do_scan(dev);
1233 saa7134_enable_i2s(dev); 1243 saa7134_enable_i2s(dev);
1234 saa7134_hw_enable2(dev); 1244 saa7134_hw_enable2(dev);
1235 1245
1246 saa7134_irq_video_signalchange(dev);
1247
1236 /*resume unfinished buffer(s)*/ 1248 /*resume unfinished buffer(s)*/
1237 spin_lock_irqsave(&dev->slock, flags); 1249 spin_lock_irqsave(&dev->slock, flags);
1238 saa7134_buffer_requeue(dev, &dev->video_q); 1250 saa7134_buffer_requeue(dev, &dev->video_q);
@@ -1246,6 +1258,7 @@ static int saa7134_resume(struct pci_dev *pci_dev)
1246 1258
1247 /* start DMA now*/ 1259 /* start DMA now*/
1248 dev->insuspend = 0; 1260 dev->insuspend = 0;
1261 smp_wmb();
1249 saa7134_set_dmabits(dev); 1262 saa7134_set_dmabits(dev);
1250 spin_unlock_irqrestore(&dev->slock, flags); 1263 spin_unlock_irqrestore(&dev->slock, flags);
1251 1264
diff --git a/drivers/media/video/saa7134/saa7134-dvb.c b/drivers/media/video/saa7134/saa7134-dvb.c
index e1ab099ec4c6..a9ca5730826f 100644
--- a/drivers/media/video/saa7134/saa7134-dvb.c
+++ b/drivers/media/video/saa7134/saa7134-dvb.c
@@ -1073,14 +1073,21 @@ static int dvb_init(struct saa7134_dev *dev)
1073 1073
1074static int dvb_fini(struct saa7134_dev *dev) 1074static int dvb_fini(struct saa7134_dev *dev)
1075{ 1075{
1076 static int on = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE; 1076 /* FIXME: I suspect that this code is bogus, since the entry for
1077 Pinnacle 300I DVB-T PAL already defines the proper init to allow
1078 the detection of mt2032 (TDA9887_PORT2_INACTIVE)
1079 */
1080 if (dev->board == SAA7134_BOARD_PINNACLE_300I_DVBT_PAL) {
1081 struct v4l2_priv_tun_config tda9887_cfg;
1082 static int on = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1083
1084 tda9887_cfg.tuner = TUNER_TDA9887;
1085 tda9887_cfg.priv = &on;
1077 1086
1078 switch (dev->board) {
1079 case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1080 /* otherwise we don't detect the tuner on next insmod */ 1087 /* otherwise we don't detect the tuner on next insmod */
1081 saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&on); 1088 saa7134_i2c_call_clients(dev, TUNER_SET_CONFIG, &tda9887_cfg);
1082 break; 1089 }
1083 }; 1090
1084 videobuf_dvb_unregister(&dev->dvb); 1091 videobuf_dvb_unregister(&dev->dvb);
1085 return 0; 1092 return 0;
1086} 1093}
diff --git a/drivers/media/video/saa7134/saa7134-empress.c b/drivers/media/video/saa7134/saa7134-empress.c
index 9322f44865b8..b1b01fa86720 100644
--- a/drivers/media/video/saa7134/saa7134-empress.c
+++ b/drivers/media/video/saa7134/saa7134-empress.c
@@ -161,152 +161,176 @@ ts_mmap(struct file *file, struct vm_area_struct * vma)
161 * video_generic_ioctl (and maybe others). userspace 161 * video_generic_ioctl (and maybe others). userspace
162 * copying is done already, arg is a kernel pointer. 162 * copying is done already, arg is a kernel pointer.
163 */ 163 */
164static int ts_do_ioctl(struct inode *inode, struct file *file, 164
165 unsigned int cmd, void *arg) 165static int empress_querycap(struct file *file, void *priv,
166 struct v4l2_capability *cap)
166{ 167{
167 struct saa7134_dev *dev = file->private_data; 168 struct saa7134_fh *fh = priv;
168 struct v4l2_ext_controls *ctrls = arg; 169 struct saa7134_dev *dev = fh->dev;
169 170
170 if (debug > 1) 171 strcpy(cap->driver, "saa7134");
171 v4l_print_ioctl(dev->name,cmd); 172 strlcpy(cap->card, saa7134_boards[dev->board].name,
172 switch (cmd) { 173 sizeof(cap->card));
173 case VIDIOC_QUERYCAP: 174 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
174 { 175 cap->version = SAA7134_VERSION_CODE;
175 struct v4l2_capability *cap = arg; 176 cap->capabilities =
176 177 V4L2_CAP_VIDEO_CAPTURE |
177 memset(cap,0,sizeof(*cap)); 178 V4L2_CAP_READWRITE |
178 strcpy(cap->driver, "saa7134"); 179 V4L2_CAP_STREAMING;
179 strlcpy(cap->card, saa7134_boards[dev->board].name, 180 return 0;
180 sizeof(cap->card)); 181}
181 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
182 cap->version = SAA7134_VERSION_CODE;
183 cap->capabilities =
184 V4L2_CAP_VIDEO_CAPTURE |
185 V4L2_CAP_READWRITE |
186 V4L2_CAP_STREAMING;
187 return 0;
188 }
189 182
190 /* --- input switching --------------------------------------- */ 183static int empress_enum_input(struct file *file, void *priv,
191 case VIDIOC_ENUMINPUT: 184 struct v4l2_input *i)
192 { 185{
193 struct v4l2_input *i = arg; 186 if (i->index != 0)
187 return -EINVAL;
194 188
195 if (i->index != 0) 189 i->type = V4L2_INPUT_TYPE_CAMERA;
196 return -EINVAL; 190 strcpy(i->name, "CCIR656");
197 i->type = V4L2_INPUT_TYPE_CAMERA;
198 strcpy(i->name,"CCIR656");
199 return 0;
200 }
201 case VIDIOC_G_INPUT:
202 {
203 int *i = arg;
204 *i = 0;
205 return 0;
206 }
207 case VIDIOC_S_INPUT:
208 {
209 int *i = arg;
210 191
211 if (*i != 0) 192 return 0;
212 return -EINVAL; 193}
213 return 0;
214 }
215 /* --- capture ioctls ---------------------------------------- */
216
217 case VIDIOC_ENUM_FMT:
218 {
219 struct v4l2_fmtdesc *f = arg;
220 int index;
221
222 index = f->index;
223 if (index != 0)
224 return -EINVAL;
225
226 memset(f,0,sizeof(*f));
227 f->index = index;
228 strlcpy(f->description, "MPEG TS", sizeof(f->description));
229 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
230 f->pixelformat = V4L2_PIX_FMT_MPEG;
231 return 0;
232 }
233 194
234 case VIDIOC_G_FMT: 195static int empress_g_input(struct file *file, void *priv, unsigned int *i)
235 { 196{
236 struct v4l2_format *f = arg; 197 *i = 0;
198 return 0;
199}
237 200
238 memset(f,0,sizeof(*f)); 201static int empress_s_input(struct file *file, void *priv, unsigned int i)
239 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 202{
203 if (i != 0)
204 return -EINVAL;
240 205
241 saa7134_i2c_call_clients(dev, cmd, arg); 206 return 0;
242 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 207}
243 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
244 return 0;
245 }
246 208
247 case VIDIOC_S_FMT: 209static int empress_enum_fmt_cap(struct file *file, void *priv,
248 { 210 struct v4l2_fmtdesc *f)
249 struct v4l2_format *f = arg; 211{
212 if (f->index != 0)
213 return -EINVAL;
250 214
251 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 215 strlcpy(f->description, "MPEG TS", sizeof(f->description));
252 return -EINVAL; 216 f->pixelformat = V4L2_PIX_FMT_MPEG;
253 217
254 saa7134_i2c_call_clients(dev, cmd, arg); 218 return 0;
255 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 219}
256 f->fmt.pix.sizeimage = TS_PACKET_SIZE* dev->ts.nr_packets;
257 return 0;
258 }
259 220
260 case VIDIOC_REQBUFS: 221static int empress_g_fmt_cap(struct file *file, void *priv,
261 return videobuf_reqbufs(&dev->empress_tsq,arg); 222 struct v4l2_format *f)
223{
224 struct saa7134_fh *fh = priv;
225 struct saa7134_dev *dev = fh->dev;
262 226
263 case VIDIOC_QUERYBUF: 227 saa7134_i2c_call_clients(dev, VIDIOC_G_FMT, f);
264 return videobuf_querybuf(&dev->empress_tsq,arg);
265 228
266 case VIDIOC_QBUF: 229 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
267 return videobuf_qbuf(&dev->empress_tsq,arg); 230 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
268 231
269 case VIDIOC_DQBUF: 232 return 0;
270 return videobuf_dqbuf(&dev->empress_tsq,arg, 233}
271 file->f_flags & O_NONBLOCK);
272 234
273 case VIDIOC_STREAMON: 235static int empress_s_fmt_cap(struct file *file, void *priv,
274 return videobuf_streamon(&dev->empress_tsq); 236 struct v4l2_format *f)
237{
238 struct saa7134_fh *fh = priv;
239 struct saa7134_dev *dev = fh->dev;
275 240
276 case VIDIOC_STREAMOFF: 241 saa7134_i2c_call_clients(dev, VIDIOC_S_FMT, f);
277 return videobuf_streamoff(&dev->empress_tsq);
278 242
279 case VIDIOC_QUERYCTRL: 243 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
280 case VIDIOC_G_CTRL: 244 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
281 case VIDIOC_S_CTRL:
282 return saa7134_common_ioctl(dev, cmd, arg);
283 245
284 case VIDIOC_S_EXT_CTRLS: 246 return 0;
285 /* count == 0 is abused in saa6752hs.c, so that special 247}
286 case is handled here explicitly. */ 248
287 if (ctrls->count == 0) 249
288 return 0; 250static int empress_reqbufs(struct file *file, void *priv,
289 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG) 251 struct v4l2_requestbuffers *p)
290 return -EINVAL; 252{
291 saa7134_i2c_call_clients(dev, VIDIOC_S_EXT_CTRLS, arg); 253 struct saa7134_fh *fh = priv;
292 ts_init_encoder(dev); 254 struct saa7134_dev *dev = fh->dev;
293 return 0; 255
294 case VIDIOC_G_EXT_CTRLS: 256 return videobuf_reqbufs(&dev->empress_tsq, p);
295 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG) 257}
296 return -EINVAL; 258
297 saa7134_i2c_call_clients(dev, VIDIOC_G_EXT_CTRLS, arg); 259static int empress_querybuf(struct file *file, void *priv,
260 struct v4l2_buffer *b)
261{
262 struct saa7134_fh *fh = priv;
263 struct saa7134_dev *dev = fh->dev;
264
265 return videobuf_querybuf(&dev->empress_tsq, b);
266}
267
268static int empress_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
269{
270 struct saa7134_fh *fh = priv;
271 struct saa7134_dev *dev = fh->dev;
272
273 return videobuf_qbuf(&dev->empress_tsq, b);
274}
275
276static int empress_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
277{
278 struct saa7134_fh *fh = priv;
279 struct saa7134_dev *dev = fh->dev;
280
281 return videobuf_dqbuf(&dev->empress_tsq, b,
282 file->f_flags & O_NONBLOCK);
283}
284
285static int empress_streamon(struct file *file, void *priv,
286 enum v4l2_buf_type type)
287{
288 struct saa7134_fh *fh = priv;
289 struct saa7134_dev *dev = fh->dev;
290
291 return videobuf_streamon(&dev->empress_tsq);
292}
293
294static int empress_streamoff(struct file *file, void *priv,
295 enum v4l2_buf_type type)
296{
297 struct saa7134_fh *fh = priv;
298 struct saa7134_dev *dev = fh->dev;
299
300 return videobuf_streamoff(&dev->empress_tsq);
301}
302
303static int empress_s_ext_ctrls(struct file *file, void *priv,
304 struct v4l2_ext_controls *ctrls)
305{
306 struct saa7134_fh *fh = priv;
307 struct saa7134_dev *dev = fh->dev;
308
309 /* count == 0 is abused in saa6752hs.c, so that special
310 case is handled here explicitly. */
311 if (ctrls->count == 0)
298 return 0; 312 return 0;
299 313
300 default: 314 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
301 return -ENOIOCTLCMD; 315 return -EINVAL;
302 } 316
317 saa7134_i2c_call_clients(dev, VIDIOC_S_EXT_CTRLS, ctrls);
318 ts_init_encoder(dev);
319
303 return 0; 320 return 0;
304} 321}
305 322
306static int ts_ioctl(struct inode *inode, struct file *file, 323static int empress_g_ext_ctrls(struct file *file, void *priv,
307 unsigned int cmd, unsigned long arg) 324 struct v4l2_ext_controls *ctrls)
308{ 325{
309 return video_usercopy(inode, file, cmd, arg, ts_do_ioctl); 326 struct saa7134_fh *fh = priv;
327 struct saa7134_dev *dev = fh->dev;
328
329 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
330 return -EINVAL;
331 saa7134_i2c_call_clients(dev, VIDIOC_G_EXT_CTRLS, ctrls);
332
333 return 0;
310} 334}
311 335
312static const struct file_operations ts_fops = 336static const struct file_operations ts_fops =
@@ -317,7 +341,7 @@ static const struct file_operations ts_fops =
317 .read = ts_read, 341 .read = ts_read,
318 .poll = ts_poll, 342 .poll = ts_poll,
319 .mmap = ts_mmap, 343 .mmap = ts_mmap,
320 .ioctl = ts_ioctl, 344 .ioctl = video_ioctl2,
321 .llseek = no_llseek, 345 .llseek = no_llseek,
322}; 346};
323 347
@@ -330,6 +354,29 @@ static struct video_device saa7134_empress_template =
330 .type2 = 0 /* FIXME */, 354 .type2 = 0 /* FIXME */,
331 .fops = &ts_fops, 355 .fops = &ts_fops,
332 .minor = -1, 356 .minor = -1,
357
358 .vidioc_querycap = empress_querycap,
359 .vidioc_enum_fmt_cap = empress_enum_fmt_cap,
360 .vidioc_s_fmt_cap = empress_s_fmt_cap,
361 .vidioc_g_fmt_cap = empress_g_fmt_cap,
362 .vidioc_reqbufs = empress_reqbufs,
363 .vidioc_querybuf = empress_querybuf,
364 .vidioc_qbuf = empress_qbuf,
365 .vidioc_dqbuf = empress_dqbuf,
366 .vidioc_streamon = empress_streamon,
367 .vidioc_streamoff = empress_streamoff,
368 .vidioc_s_ext_ctrls = empress_s_ext_ctrls,
369 .vidioc_g_ext_ctrls = empress_g_ext_ctrls,
370 .vidioc_enum_input = empress_enum_input,
371 .vidioc_g_input = empress_g_input,
372 .vidioc_s_input = empress_s_input,
373
374 .vidioc_queryctrl = saa7134_queryctrl,
375 .vidioc_g_ctrl = saa7134_g_ctrl,
376 .vidioc_s_ctrl = saa7134_s_ctrl,
377
378 .tvnorms = SAA7134_NORMS,
379 .current_norm = V4L2_STD_PAL,
333}; 380};
334 381
335static void empress_signal_update(struct work_struct *work) 382static void empress_signal_update(struct work_struct *work)
diff --git a/drivers/media/video/saa7134/saa7134-i2c.c b/drivers/media/video/saa7134/saa7134-i2c.c
index 6deaad1a5480..d3322c3018f2 100644
--- a/drivers/media/video/saa7134/saa7134-i2c.c
+++ b/drivers/media/video/saa7134/saa7134-i2c.c
@@ -323,7 +323,6 @@ static int attach_inform(struct i2c_client *client)
323{ 323{
324 struct saa7134_dev *dev = client->adapter->algo_data; 324 struct saa7134_dev *dev = client->adapter->algo_data;
325 int tuner = dev->tuner_type; 325 int tuner = dev->tuner_type;
326 int conf = dev->tda9887_conf;
327 struct tuner_setup tun_setup; 326 struct tuner_setup tun_setup;
328 327
329 d1printk( "%s i2c attach [addr=0x%x,client=%s]\n", 328 d1printk( "%s i2c attach [addr=0x%x,client=%s]\n",
@@ -335,6 +334,7 @@ static int attach_inform(struct i2c_client *client)
335 case 0x7a: 334 case 0x7a:
336 case 0x47: 335 case 0x47:
337 case 0x71: 336 case 0x71:
337 case 0x2d:
338 { 338 {
339 struct IR_i2c *ir = i2c_get_clientdata(client); 339 struct IR_i2c *ir = i2c_get_clientdata(client);
340 d1printk("%s i2c IR detected (%s).\n", 340 d1printk("%s i2c IR detected (%s).\n",
@@ -360,7 +360,6 @@ static int attach_inform(struct i2c_client *client)
360 } 360 }
361 361
362 if (tuner != UNSET) { 362 if (tuner != UNSET) {
363
364 tun_setup.type = tuner; 363 tun_setup.type = tuner;
365 tun_setup.addr = saa7134_boards[dev->board].tuner_addr; 364 tun_setup.addr = saa7134_boards[dev->board].tuner_addr;
366 tun_setup.config = saa7134_boards[dev->board].tuner_config; 365 tun_setup.config = saa7134_boards[dev->board].tuner_config;
@@ -372,9 +371,18 @@ static int attach_inform(struct i2c_client *client)
372 371
373 client->driver->command(client,TUNER_SET_TYPE_ADDR, &tun_setup); 372 client->driver->command(client,TUNER_SET_TYPE_ADDR, &tun_setup);
374 } 373 }
374
375 if (tuner == TUNER_TDA9887) {
376 struct v4l2_priv_tun_config tda9887_cfg;
377
378 tda9887_cfg.tuner = TUNER_TDA9887;
379 tda9887_cfg.priv = &dev->tda9887_conf;
380
381 client->driver->command(client, TUNER_SET_CONFIG,
382 &tda9887_cfg);
383 }
375 } 384 }
376 385
377 client->driver->command(client, TDA9887_SET_CONFIG, &conf);
378 386
379 return 0; 387 return 0;
380} 388}
@@ -432,6 +440,7 @@ static char *i2c_devs[128] = {
432 [ 0xa0 >> 1 ] = "eeprom", 440 [ 0xa0 >> 1 ] = "eeprom",
433 [ 0xc0 >> 1 ] = "tuner (analog)", 441 [ 0xc0 >> 1 ] = "tuner (analog)",
434 [ 0x86 >> 1 ] = "tda9887", 442 [ 0x86 >> 1 ] = "tda9887",
443 [ 0x5a >> 1 ] = "remote control",
435}; 444};
436 445
437static void do_i2c_scan(char *name, struct i2c_client *c) 446static void do_i2c_scan(char *name, struct i2c_client *c)
diff --git a/drivers/media/video/saa7134/saa7134-input.c b/drivers/media/video/saa7134/saa7134-input.c
index 3abaa1b8ac9d..0db955c2d9b9 100644
--- a/drivers/media/video/saa7134/saa7134-input.c
+++ b/drivers/media/video/saa7134/saa7134-input.c
@@ -49,9 +49,14 @@ module_param(repeat_delay, int, 0644);
49MODULE_PARM_DESC(repeat_delay, "delay before key repeat started"); 49MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
50static int repeat_period = 33; 50static int repeat_period = 33;
51module_param(repeat_period, int, 0644); 51module_param(repeat_period, int, 0644);
52MODULE_PARM_DESC(repeat_period, "repeat period between" 52MODULE_PARM_DESC(repeat_period, "repeat period between "
53 "keypresses when key is down"); 53 "keypresses when key is down");
54 54
55static unsigned int disable_other_ir;
56module_param(disable_other_ir, int, 0644);
57MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
58 "alternative remotes from other manufacturers");
59
55#define dprintk(fmt, arg...) if (ir_debug) \ 60#define dprintk(fmt, arg...) if (ir_debug) \
56 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg) 61 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
57#define i2cdprintk(fmt, arg...) if (ir_debug) \ 62#define i2cdprintk(fmt, arg...) if (ir_debug) \
@@ -154,6 +159,45 @@ static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
154 return 1; 159 return 1;
155} 160}
156 161
162
163static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
164{
165 unsigned char data[12];
166 u32 gpio;
167
168 struct saa7134_dev *dev = ir->c.adapter->algo_data;
169
170 /* rising SAA7134_GPIO_GPRESCAN reads the status */
171 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
172 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
173
174 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
175
176 if (0x400000 & ~gpio)
177 return 0; /* No button press */
178
179 ir->c.addr = 0x5a >> 1;
180
181 if (12 != i2c_master_recv(&ir->c, data, 12)) {
182 i2cdprintk("read error\n");
183 return -EIO;
184 }
185 /* IR of this card normally decode signals NEC-standard from
186 * - Sven IHOO MT 5.1R remote. xxyye718
187 * - Sven DVD HD-10xx remote. xxyyf708
188 * - BBK ...
189 * - mayby others
190 * So, skip not our, if disable full codes mode.
191 */
192 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
193 return 0;
194
195 *ir_key = data[9];
196 *ir_raw = data[9];
197
198 return 1;
199}
200
157void saa7134_input_irq(struct saa7134_dev *dev) 201void saa7134_input_irq(struct saa7134_dev *dev)
158{ 202{
159 struct card_ir *ir = dev->remote; 203 struct card_ir *ir = dev->remote;
@@ -260,6 +304,7 @@ int saa7134_input_init1(struct saa7134_dev *dev)
260 case SAA7134_BOARD_AVERMEDIA_STUDIO_307: 304 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
261 case SAA7134_BOARD_AVERMEDIA_STUDIO_507: 305 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
262 case SAA7134_BOARD_AVERMEDIA_GO_007_FM: 306 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
307 case SAA7134_BOARD_AVERMEDIA_M102:
263 ir_codes = ir_codes_avermedia; 308 ir_codes = ir_codes_avermedia;
264 mask_keycode = 0x0007C8; 309 mask_keycode = 0x0007C8;
265 mask_keydown = 0x000010; 310 mask_keydown = 0x000010;
@@ -287,6 +332,16 @@ int saa7134_input_init1(struct saa7134_dev *dev)
287 case SAA7134_BOARD_MANLI_MTV001: 332 case SAA7134_BOARD_MANLI_MTV001:
288 case SAA7134_BOARD_MANLI_MTV002: 333 case SAA7134_BOARD_MANLI_MTV002:
289 case SAA7134_BOARD_BEHOLD_409FM: 334 case SAA7134_BOARD_BEHOLD_409FM:
335 case SAA7134_BOARD_BEHOLD_401:
336 case SAA7134_BOARD_BEHOLD_403:
337 case SAA7134_BOARD_BEHOLD_403FM:
338 case SAA7134_BOARD_BEHOLD_405:
339 case SAA7134_BOARD_BEHOLD_405FM:
340 case SAA7134_BOARD_BEHOLD_407:
341 case SAA7134_BOARD_BEHOLD_407FM:
342 case SAA7134_BOARD_BEHOLD_409:
343 case SAA7134_BOARD_BEHOLD_505FM:
344 case SAA7134_BOARD_BEHOLD_507_9FM:
290 ir_codes = ir_codes_manli; 345 ir_codes = ir_codes_manli;
291 mask_keycode = 0x001f00; 346 mask_keycode = 0x001f00;
292 mask_keyup = 0x004000; 347 mask_keyup = 0x004000;
@@ -457,6 +512,12 @@ void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
457 ir->get_key = get_key_hvr1110; 512 ir->get_key = get_key_hvr1110;
458 ir->ir_codes = ir_codes_hauppauge_new; 513 ir->ir_codes = ir_codes_hauppauge_new;
459 break; 514 break;
515 case SAA7134_BOARD_BEHOLD_607_9FM:
516 case SAA7134_BOARD_BEHOLD_M6:
517 snprintf(ir->c.name, sizeof(ir->c.name), "BeholdTV");
518 ir->get_key = get_key_beholdm6xx;
519 ir->ir_codes = ir_codes_behold;
520 break;
460 default: 521 default:
461 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board); 522 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
462 break; 523 break;
diff --git a/drivers/media/video/saa7134/saa7134-oss.c b/drivers/media/video/saa7134/saa7134-oss.c
deleted file mode 100644
index aedf04653e0e..000000000000
--- a/drivers/media/video/saa7134/saa7134-oss.c
+++ /dev/null
@@ -1,1046 +0,0 @@
1/*
2 *
3 * device driver for philips saa7134 based TV cards
4 * oss dsp interface
5 *
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 * 2005 conversion to standalone module:
8 * Ricardo Cerqueira <v4l@cerqueira.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/init.h>
26#include <linux/list.h>
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/interrupt.h>
30#include <linux/slab.h>
31#include <linux/sound.h>
32#include <linux/soundcard.h>
33
34#include "saa7134-reg.h"
35#include "saa7134.h"
36
37/* ------------------------------------------------------------------ */
38
39static unsigned int debug = 0;
40module_param(debug, int, 0644);
41MODULE_PARM_DESC(debug,"enable debug messages [oss]");
42
43static unsigned int rate = 0;
44module_param(rate, int, 0444);
45MODULE_PARM_DESC(rate,"sample rate (valid are: 32000,48000)");
46
47static unsigned int dsp_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
48MODULE_PARM_DESC(dsp_nr, "device numbers for SAA7134 capture interface(s).");
49module_param_array(dsp_nr, int, NULL, 0444);
50
51static unsigned int mixer_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
52MODULE_PARM_DESC(mixer_nr, "mixer numbers for SAA7134 capture interface(s).");
53module_param_array(mixer_nr, int, NULL, 0444);
54
55#define dprintk(fmt, arg...) if (debug) \
56 printk(KERN_DEBUG "%s/oss: " fmt, dev->name , ## arg)
57
58
59/* ------------------------------------------------------------------ */
60
61static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
62{
63 if (blksize < 0x100)
64 blksize = 0x100;
65 if (blksize > 0x10000)
66 blksize = 0x10000;
67
68 if (blocks < 2)
69 blocks = 2;
70 if ((blksize * blocks) > 1024*1024)
71 blocks = 1024*1024 / blksize;
72
73 dev->dmasound.blocks = blocks;
74 dev->dmasound.blksize = blksize;
75 dev->dmasound.bufsize = blksize * blocks;
76
77 dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
78 blocks,blksize,blksize * blocks / 1024);
79 return 0;
80}
81
82static int dsp_buffer_init(struct saa7134_dev *dev)
83{
84 int err;
85
86 BUG_ON(!dev->dmasound.bufsize);
87 videobuf_dma_init(&dev->dmasound.dma);
88 err = videobuf_dma_init_kernel(&dev->dmasound.dma, PCI_DMA_FROMDEVICE,
89 (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
90 if (0 != err)
91 return err;
92 return 0;
93}
94
95static int dsp_buffer_free(struct saa7134_dev *dev)
96{
97 BUG_ON(!dev->dmasound.blksize);
98 videobuf_dma_free(&dev->dmasound.dma);
99 dev->dmasound.blocks = 0;
100 dev->dmasound.blksize = 0;
101 dev->dmasound.bufsize = 0;
102 return 0;
103}
104
105static void dsp_dma_start(struct saa7134_dev *dev)
106{
107 dev->dmasound.dma_blk = 0;
108 dev->dmasound.dma_running = 1;
109 saa7134_set_dmabits(dev);
110}
111
112static void dsp_dma_stop(struct saa7134_dev *dev)
113{
114 dev->dmasound.dma_blk = -1;
115 dev->dmasound.dma_running = 0;
116 saa7134_set_dmabits(dev);
117}
118
119static int dsp_rec_start(struct saa7134_dev *dev)
120{
121 int err, bswap, sign;
122 u32 fmt, control;
123 unsigned long flags;
124
125 /* prepare buffer */
126 if (0 != (err = videobuf_pci_dma_map(dev->pci,&dev->dmasound.dma)))
127 return err;
128 if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt)))
129 goto fail1;
130 if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
131 dev->dmasound.dma.sglist,
132 dev->dmasound.dma.sglen,
133 0)))
134 goto fail2;
135
136 /* sample format */
137 switch (dev->dmasound.afmt) {
138 case AFMT_U8:
139 case AFMT_S8: fmt = 0x00; break;
140 case AFMT_U16_LE:
141 case AFMT_U16_BE:
142 case AFMT_S16_LE:
143 case AFMT_S16_BE: fmt = 0x01; break;
144 default:
145 err = -EINVAL;
146 goto fail2;
147 }
148
149 switch (dev->dmasound.afmt) {
150 case AFMT_S8:
151 case AFMT_S16_LE:
152 case AFMT_S16_BE: sign = 1; break;
153 default: sign = 0; break;
154 }
155
156 switch (dev->dmasound.afmt) {
157 case AFMT_U16_BE:
158 case AFMT_S16_BE: bswap = 1; break;
159 default: bswap = 0; break;
160 }
161
162 switch (dev->pci->device) {
163 case PCI_DEVICE_ID_PHILIPS_SAA7134:
164 if (1 == dev->dmasound.channels)
165 fmt |= (1 << 3);
166 if (2 == dev->dmasound.channels)
167 fmt |= (3 << 3);
168 if (sign)
169 fmt |= 0x04;
170 fmt |= (TV == dev->dmasound.input) ? 0xc0 : 0x80;
171
172 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
173 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
174 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
175 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
176
177 break;
178 case PCI_DEVICE_ID_PHILIPS_SAA7133:
179 case PCI_DEVICE_ID_PHILIPS_SAA7135:
180 if (1 == dev->dmasound.channels)
181 fmt |= (1 << 4);
182 if (2 == dev->dmasound.channels)
183 fmt |= (2 << 4);
184 if (!sign)
185 fmt |= 0x04;
186 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -4);
187 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
188 break;
189 }
190 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
191 dev->dmasound.afmt, dev->dmasound.channels, fmt,
192 bswap ? 'b' : '-');
193
194 /* dma: setup channel 6 (= AUDIO) */
195 control = SAA7134_RS_CONTROL_BURST_16 |
196 SAA7134_RS_CONTROL_ME |
197 (dev->dmasound.pt.dma >> 12);
198 if (bswap)
199 control |= SAA7134_RS_CONTROL_BSWAP;
200 saa_writel(SAA7134_RS_BA1(6),0);
201 saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
202 saa_writel(SAA7134_RS_PITCH(6),0);
203 saa_writel(SAA7134_RS_CONTROL(6),control);
204
205 /* start dma */
206 dev->dmasound.recording_on = 1;
207 spin_lock_irqsave(&dev->slock,flags);
208 dsp_dma_start(dev);
209 spin_unlock_irqrestore(&dev->slock,flags);
210 return 0;
211
212 fail2:
213 saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
214 fail1:
215 videobuf_pci_dma_unmap(dev->pci,&dev->dmasound.dma);
216 return err;
217}
218
219static int dsp_rec_stop(struct saa7134_dev *dev)
220{
221 unsigned long flags;
222
223 dprintk("rec_stop dma_blk=%d\n",dev->dmasound.dma_blk);
224
225 /* stop dma */
226 dev->dmasound.recording_on = 0;
227 spin_lock_irqsave(&dev->slock,flags);
228 dsp_dma_stop(dev);
229 spin_unlock_irqrestore(&dev->slock,flags);
230
231 /* unlock buffer */
232 saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
233 videobuf_pci_dma_unmap(dev->pci,&dev->dmasound.dma);
234 return 0;
235}
236
237/* ------------------------------------------------------------------ */
238
239static int dsp_open(struct inode *inode, struct file *file)
240{
241 int minor = iminor(inode);
242 struct saa7134_dev *dev;
243 int err;
244
245 list_for_each_entry(dev, &saa7134_devlist, devlist)
246 if (dev->dmasound.minor_dsp == minor)
247 goto found;
248 return -ENODEV;
249 found:
250
251 mutex_lock(&dev->dmasound.lock);
252 err = -EBUSY;
253 if (dev->dmasound.users_dsp)
254 goto fail1;
255 dev->dmasound.users_dsp++;
256 file->private_data = dev;
257
258 dev->dmasound.afmt = AFMT_U8;
259 dev->dmasound.channels = 1;
260 dev->dmasound.read_count = 0;
261 dev->dmasound.read_offset = 0;
262 dsp_buffer_conf(dev,PAGE_SIZE,64);
263 err = dsp_buffer_init(dev);
264 if (0 != err)
265 goto fail2;
266
267 mutex_unlock(&dev->dmasound.lock);
268 return 0;
269
270 fail2:
271 dev->dmasound.users_dsp--;
272 fail1:
273 mutex_unlock(&dev->dmasound.lock);
274 return err;
275}
276
277static int dsp_release(struct inode *inode, struct file *file)
278{
279 struct saa7134_dev *dev = file->private_data;
280
281 mutex_lock(&dev->dmasound.lock);
282 if (dev->dmasound.recording_on)
283 dsp_rec_stop(dev);
284 dsp_buffer_free(dev);
285 dev->dmasound.users_dsp--;
286 file->private_data = NULL;
287 mutex_unlock(&dev->dmasound.lock);
288 return 0;
289}
290
291static ssize_t dsp_read(struct file *file, char __user *buffer,
292 size_t count, loff_t *ppos)
293{
294 struct saa7134_dev *dev = file->private_data;
295 DECLARE_WAITQUEUE(wait, current);
296 unsigned int bytes;
297 unsigned long flags;
298 int err,ret = 0;
299
300 add_wait_queue(&dev->dmasound.wq, &wait);
301 mutex_lock(&dev->dmasound.lock);
302 while (count > 0) {
303 /* wait for data if needed */
304 if (0 == dev->dmasound.read_count) {
305 if (!dev->dmasound.recording_on) {
306 err = dsp_rec_start(dev);
307 if (err < 0) {
308 if (0 == ret)
309 ret = err;
310 break;
311 }
312 }
313 if (dev->dmasound.recording_on &&
314 !dev->dmasound.dma_running) {
315 /* recover from overruns */
316 spin_lock_irqsave(&dev->slock,flags);
317 dsp_dma_start(dev);
318 spin_unlock_irqrestore(&dev->slock,flags);
319 }
320 if (file->f_flags & O_NONBLOCK) {
321 if (0 == ret)
322 ret = -EAGAIN;
323 break;
324 }
325 mutex_unlock(&dev->dmasound.lock);
326 set_current_state(TASK_INTERRUPTIBLE);
327 if (0 == dev->dmasound.read_count)
328 schedule();
329 set_current_state(TASK_RUNNING);
330 mutex_lock(&dev->dmasound.lock);
331 if (signal_pending(current)) {
332 if (0 == ret)
333 ret = -EINTR;
334 break;
335 }
336 }
337
338 /* copy data to userspace */
339 bytes = count;
340 if (bytes > dev->dmasound.read_count)
341 bytes = dev->dmasound.read_count;
342 if (bytes > dev->dmasound.bufsize - dev->dmasound.read_offset)
343 bytes = dev->dmasound.bufsize - dev->dmasound.read_offset;
344 if (copy_to_user(buffer + ret,
345 dev->dmasound.dma.vmalloc + dev->dmasound.read_offset,
346 bytes)) {
347 if (0 == ret)
348 ret = -EFAULT;
349 break;
350 }
351
352 ret += bytes;
353 count -= bytes;
354 dev->dmasound.read_count -= bytes;
355 dev->dmasound.read_offset += bytes;
356 if (dev->dmasound.read_offset == dev->dmasound.bufsize)
357 dev->dmasound.read_offset = 0;
358 }
359 mutex_unlock(&dev->dmasound.lock);
360 remove_wait_queue(&dev->dmasound.wq, &wait);
361 return ret;
362}
363
364static ssize_t dsp_write(struct file *file, const char __user *buffer,
365 size_t count, loff_t *ppos)
366{
367 return -EINVAL;
368}
369
370static const char *osspcm_ioctls[] = {
371 "RESET", "SYNC", "SPEED", "STEREO", "GETBLKSIZE", "SETFMT",
372 "CHANNELS", "?", "POST", "SUBDIVIDE", "SETFRAGMENT", "GETFMTS",
373 "GETOSPACE", "GETISPACE", "NONBLOCK", "GETCAPS", "GET/SETTRIGGER",
374 "GETIPTR", "GETOPTR", "MAPINBUF", "MAPOUTBUF", "SETSYNCRO",
375 "SETDUPLEX", "GETODELAY"
376};
377#define OSSPCM_IOCTLS ARRAY_SIZE(osspcm_ioctls)
378
379static void saa7134_oss_print_ioctl(char *name, unsigned int cmd)
380{
381 char *dir;
382
383 switch (_IOC_DIR(cmd)) {
384 case _IOC_NONE: dir = "--"; break;
385 case _IOC_READ: dir = "r-"; break;
386 case _IOC_WRITE: dir = "-w"; break;
387 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
388 default: dir = "??"; break;
389 }
390 switch (_IOC_TYPE(cmd)) {
391 case 'P':
392 printk(KERN_DEBUG "%s: ioctl 0x%08x (oss dsp, %s, SNDCTL_DSP_%s)\n",
393 name, cmd, dir, (_IOC_NR(cmd) < OSSPCM_IOCTLS) ?
394 osspcm_ioctls[_IOC_NR(cmd)] : "???");
395 break;
396 case 'M':
397 printk(KERN_DEBUG "%s: ioctl 0x%08x (oss mixer, %s, #%d)\n",
398 name, cmd, dir, _IOC_NR(cmd));
399 break;
400 default:
401 printk(KERN_DEBUG "%s: ioctl 0x%08x (???, %s, #%d)\n",
402 name, cmd, dir, _IOC_NR(cmd));
403 }
404}
405
406static int dsp_ioctl(struct inode *inode, struct file *file,
407 unsigned int cmd, unsigned long arg)
408{
409 struct saa7134_dev *dev = file->private_data;
410 void __user *argp = (void __user *) arg;
411 int __user *p = argp;
412 int val = 0;
413
414 if (debug > 1)
415 saa7134_oss_print_ioctl(dev->name,cmd);
416 switch (cmd) {
417 case OSS_GETVERSION:
418 return put_user(SOUND_VERSION, p);
419 case SNDCTL_DSP_GETCAPS:
420 return 0;
421
422 case SNDCTL_DSP_SPEED:
423 if (get_user(val, p))
424 return -EFAULT;
425 /* fall through */
426 case SOUND_PCM_READ_RATE:
427 return put_user(dev->dmasound.rate, p);
428
429 case SNDCTL_DSP_STEREO:
430 if (get_user(val, p))
431 return -EFAULT;
432 mutex_lock(&dev->dmasound.lock);
433 dev->dmasound.channels = val ? 2 : 1;
434 if (dev->dmasound.recording_on) {
435 dsp_rec_stop(dev);
436 dsp_rec_start(dev);
437 }
438 mutex_unlock(&dev->dmasound.lock);
439 return put_user(dev->dmasound.channels-1, p);
440
441 case SNDCTL_DSP_CHANNELS:
442 if (get_user(val, p))
443 return -EFAULT;
444 if (val != 1 && val != 2)
445 return -EINVAL;
446 mutex_lock(&dev->dmasound.lock);
447 dev->dmasound.channels = val;
448 if (dev->dmasound.recording_on) {
449 dsp_rec_stop(dev);
450 dsp_rec_start(dev);
451 }
452 mutex_unlock(&dev->dmasound.lock);
453 /* fall through */
454 case SOUND_PCM_READ_CHANNELS:
455 return put_user(dev->dmasound.channels, p);
456
457 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
458 return put_user(AFMT_U8 | AFMT_S8 |
459 AFMT_U16_LE | AFMT_U16_BE |
460 AFMT_S16_LE | AFMT_S16_BE, p);
461
462 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt */
463 if (get_user(val, p))
464 return -EFAULT;
465 switch (val) {
466 case AFMT_QUERY:
467 /* nothing to do */
468 break;
469 case AFMT_U8:
470 case AFMT_S8:
471 case AFMT_U16_LE:
472 case AFMT_U16_BE:
473 case AFMT_S16_LE:
474 case AFMT_S16_BE:
475 mutex_lock(&dev->dmasound.lock);
476 dev->dmasound.afmt = val;
477 if (dev->dmasound.recording_on) {
478 dsp_rec_stop(dev);
479 dsp_rec_start(dev);
480 }
481 mutex_unlock(&dev->dmasound.lock);
482 return put_user(dev->dmasound.afmt, p);
483 default:
484 return -EINVAL;
485 }
486
487 case SOUND_PCM_READ_BITS:
488 switch (dev->dmasound.afmt) {
489 case AFMT_U8:
490 case AFMT_S8:
491 return put_user(8, p);
492 case AFMT_U16_LE:
493 case AFMT_U16_BE:
494 case AFMT_S16_LE:
495 case AFMT_S16_BE:
496 return put_user(16, p);
497 default:
498 return -EINVAL;
499 }
500
501 case SNDCTL_DSP_NONBLOCK:
502 file->f_flags |= O_NONBLOCK;
503 return 0;
504
505 case SNDCTL_DSP_RESET:
506 mutex_lock(&dev->dmasound.lock);
507 if (dev->dmasound.recording_on)
508 dsp_rec_stop(dev);
509 mutex_unlock(&dev->dmasound.lock);
510 return 0;
511 case SNDCTL_DSP_GETBLKSIZE:
512 return put_user(dev->dmasound.blksize, p);
513
514 case SNDCTL_DSP_SETFRAGMENT:
515 if (get_user(val, p))
516 return -EFAULT;
517 if (dev->dmasound.recording_on)
518 return -EBUSY;
519 dsp_buffer_free(dev);
520 /* used to be arg >> 16 instead of val >> 16; fixed */
521 dsp_buffer_conf(dev,1 << (val & 0xffff), (val >> 16) & 0xffff);
522 dsp_buffer_init(dev);
523 return 0;
524
525 case SNDCTL_DSP_SYNC:
526 /* NOP */
527 return 0;
528
529 case SNDCTL_DSP_GETISPACE:
530 {
531 audio_buf_info info;
532 info.fragsize = dev->dmasound.blksize;
533 info.fragstotal = dev->dmasound.blocks;
534 info.bytes = dev->dmasound.read_count;
535 info.fragments = info.bytes / info.fragsize;
536 if (copy_to_user(argp, &info, sizeof(info)))
537 return -EFAULT;
538 return 0;
539 }
540 default:
541 return -EINVAL;
542 }
543}
544
545static unsigned int dsp_poll(struct file *file, struct poll_table_struct *wait)
546{
547 struct saa7134_dev *dev = file->private_data;
548 unsigned int mask = 0;
549
550 poll_wait(file, &dev->dmasound.wq, wait);
551
552 if (0 == dev->dmasound.read_count) {
553 mutex_lock(&dev->dmasound.lock);
554 if (!dev->dmasound.recording_on)
555 dsp_rec_start(dev);
556 mutex_unlock(&dev->dmasound.lock);
557 } else
558 mask |= (POLLIN | POLLRDNORM);
559 return mask;
560}
561
562const struct file_operations saa7134_dsp_fops = {
563 .owner = THIS_MODULE,
564 .open = dsp_open,
565 .release = dsp_release,
566 .read = dsp_read,
567 .write = dsp_write,
568 .ioctl = dsp_ioctl,
569 .poll = dsp_poll,
570 .llseek = no_llseek,
571};
572
573/* ------------------------------------------------------------------ */
574
575static int
576mixer_recsrc_7134(struct saa7134_dev *dev)
577{
578 int analog_io,rate;
579
580 switch (dev->dmasound.input) {
581 case TV:
582 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
583 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
584 break;
585 case LINE1:
586 case LINE2:
587 case LINE2_LEFT:
588 analog_io = (LINE1 == dev->dmasound.input) ? 0x00 : 0x08;
589 rate = (32000 == dev->dmasound.rate) ? 0x01 : 0x03;
590 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
591 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
592 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
593 break;
594 }
595 return 0;
596}
597
598static int
599mixer_recsrc_7133(struct saa7134_dev *dev)
600{
601 u32 anabar, xbarin;
602
603 xbarin = 0x03; // adc
604 anabar = 0;
605 switch (dev->dmasound.input) {
606 case TV:
607 xbarin = 0; // Demodulator
608 anabar = 2; // DACs
609 break;
610 case LINE1:
611 anabar = 0; // aux1, aux1
612 break;
613 case LINE2:
614 case LINE2_LEFT:
615 anabar = 9; // aux2, aux2
616 break;
617 }
618 /* output xbar always main channel */
619 saa_dsp_writel(dev, 0x46c >> 2, 0xbbbb10);
620 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
621 saa_writel(0x594 >> 2, anabar);
622
623 return 0;
624}
625
626static int
627mixer_recsrc(struct saa7134_dev *dev, enum saa7134_audio_in src)
628{
629 static const char *iname[] = { "Oops", "TV", "LINE1", "LINE2" };
630
631 dev->dmasound.count++;
632 dev->dmasound.input = src;
633 dprintk("mixer input = %s\n",iname[dev->dmasound.input]);
634
635 switch (dev->pci->device) {
636 case PCI_DEVICE_ID_PHILIPS_SAA7134:
637 mixer_recsrc_7134(dev);
638 break;
639 case PCI_DEVICE_ID_PHILIPS_SAA7133:
640 case PCI_DEVICE_ID_PHILIPS_SAA7135:
641 mixer_recsrc_7133(dev);
642 break;
643 }
644 return 0;
645}
646
647static int
648mixer_level(struct saa7134_dev *dev, enum saa7134_audio_in src, int level)
649{
650 switch (dev->pci->device) {
651 case PCI_DEVICE_ID_PHILIPS_SAA7134:
652 switch (src) {
653 case TV:
654 /* nothing */
655 break;
656 case LINE1:
657 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
658 (100 == level) ? 0x00 : 0x10);
659 break;
660 case LINE2:
661 case LINE2_LEFT:
662 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
663 (100 == level) ? 0x00 : 0x20);
664 break;
665 }
666 break;
667 case PCI_DEVICE_ID_PHILIPS_SAA7133:
668 case PCI_DEVICE_ID_PHILIPS_SAA7135:
669 /* nothing */
670 break;
671 }
672 return 0;
673}
674
675/* ------------------------------------------------------------------ */
676
677static int mixer_open(struct inode *inode, struct file *file)
678{
679 int minor = iminor(inode);
680 struct saa7134_dev *dev;
681
682 list_for_each_entry(dev, &saa7134_devlist, devlist)
683 if (dev->dmasound.minor_mixer == minor) {
684 file->private_data = dev;
685 return 0;
686 }
687 return -ENODEV;
688}
689
690static int mixer_release(struct inode *inode, struct file *file)
691{
692 file->private_data = NULL;
693 return 0;
694}
695
696static int mixer_ioctl(struct inode *inode, struct file *file,
697 unsigned int cmd, unsigned long arg)
698{
699 struct saa7134_dev *dev = file->private_data;
700 enum saa7134_audio_in input;
701 int val,ret;
702 void __user *argp = (void __user *) arg;
703 int __user *p = argp;
704
705 if (debug > 1)
706 saa7134_oss_print_ioctl(dev->name,cmd);
707 switch (cmd) {
708 case OSS_GETVERSION:
709 return put_user(SOUND_VERSION, p);
710 case SOUND_MIXER_INFO:
711 {
712 mixer_info info;
713 memset(&info,0,sizeof(info));
714 strlcpy(info.id, "TV audio", sizeof(info.id));
715 strlcpy(info.name, dev->name, sizeof(info.name));
716 info.modify_counter = dev->dmasound.count;
717 if (copy_to_user(argp, &info, sizeof(info)))
718 return -EFAULT;
719 return 0;
720 }
721 case SOUND_OLD_MIXER_INFO:
722 {
723 _old_mixer_info info;
724 memset(&info,0,sizeof(info));
725 strlcpy(info.id, "TV audio", sizeof(info.id));
726 strlcpy(info.name, dev->name, sizeof(info.name));
727 if (copy_to_user(argp, &info, sizeof(info)))
728 return -EFAULT;
729 return 0;
730 }
731 case MIXER_READ(SOUND_MIXER_CAPS):
732 return put_user(SOUND_CAP_EXCL_INPUT, p);
733 case MIXER_READ(SOUND_MIXER_STEREODEVS):
734 return put_user(0, p);
735 case MIXER_READ(SOUND_MIXER_RECMASK):
736 case MIXER_READ(SOUND_MIXER_DEVMASK):
737 val = SOUND_MASK_LINE1 | SOUND_MASK_LINE2;
738 if (32000 == dev->dmasound.rate)
739 val |= SOUND_MASK_VIDEO;
740 return put_user(val, p);
741
742 case MIXER_WRITE(SOUND_MIXER_RECSRC):
743 if (get_user(val, p))
744 return -EFAULT;
745 input = dev->dmasound.input;
746 if (32000 == dev->dmasound.rate &&
747 val & SOUND_MASK_VIDEO && dev->dmasound.input != TV)
748 input = TV;
749 if (val & SOUND_MASK_LINE1 && dev->dmasound.input != LINE1)
750 input = LINE1;
751 if (val & SOUND_MASK_LINE2 && dev->dmasound.input != LINE2)
752 input = LINE2;
753 if (input != dev->dmasound.input)
754 mixer_recsrc(dev,input);
755 /* fall throuth */
756 case MIXER_READ(SOUND_MIXER_RECSRC):
757 switch (dev->dmasound.input) {
758 case TV: ret = SOUND_MASK_VIDEO; break;
759 case LINE1: ret = SOUND_MASK_LINE1; break;
760 case LINE2: ret = SOUND_MASK_LINE2; break;
761 default: ret = 0;
762 }
763 return put_user(ret, p);
764
765 case MIXER_WRITE(SOUND_MIXER_VIDEO):
766 case MIXER_READ(SOUND_MIXER_VIDEO):
767 if (32000 != dev->dmasound.rate)
768 return -EINVAL;
769 return put_user(100 | 100 << 8, p);
770
771 case MIXER_WRITE(SOUND_MIXER_LINE1):
772 if (get_user(val, p))
773 return -EFAULT;
774 val &= 0xff;
775 val = (val <= 50) ? 50 : 100;
776 dev->dmasound.line1 = val;
777 mixer_level(dev,LINE1,dev->dmasound.line1);
778 /* fall throuth */
779 case MIXER_READ(SOUND_MIXER_LINE1):
780 return put_user(dev->dmasound.line1 | dev->dmasound.line1 << 8, p);
781
782 case MIXER_WRITE(SOUND_MIXER_LINE2):
783 if (get_user(val, p))
784 return -EFAULT;
785 val &= 0xff;
786 val = (val <= 50) ? 50 : 100;
787 dev->dmasound.line2 = val;
788 mixer_level(dev,LINE2,dev->dmasound.line2);
789 /* fall throuth */
790 case MIXER_READ(SOUND_MIXER_LINE2):
791 return put_user(dev->dmasound.line2 | dev->dmasound.line2 << 8, p);
792
793 default:
794 return -EINVAL;
795 }
796}
797
798const struct file_operations saa7134_mixer_fops = {
799 .owner = THIS_MODULE,
800 .open = mixer_open,
801 .release = mixer_release,
802 .ioctl = mixer_ioctl,
803 .llseek = no_llseek,
804};
805
806/* ------------------------------------------------------------------ */
807
808static irqreturn_t saa7134_oss_irq(int irq, void *dev_id)
809{
810 struct saa7134_dmasound *dmasound = dev_id;
811 struct saa7134_dev *dev = dmasound->priv_data;
812 unsigned long report, status;
813 int loop, handled = 0;
814
815 for (loop = 0; loop < 10; loop++) {
816 report = saa_readl(SAA7134_IRQ_REPORT);
817 status = saa_readl(SAA7134_IRQ_STATUS);
818
819 if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
820 handled = 1;
821 saa_writel(SAA7134_IRQ_REPORT,report);
822 saa7134_irq_oss_done(dev, status);
823 } else {
824 goto out;
825 }
826 }
827
828 if (loop == 10) {
829 dprintk("error! looping IRQ!");
830 }
831out:
832 return IRQ_RETVAL(handled);
833}
834
835int saa7134_oss_init1(struct saa7134_dev *dev)
836{
837
838 if ((request_irq(dev->pci->irq, saa7134_oss_irq,
839 IRQF_SHARED | IRQF_DISABLED, dev->name,
840 (void*) &dev->dmasound)) < 0)
841 return -1;
842
843 /* general */
844 mutex_init(&dev->dmasound.lock);
845 init_waitqueue_head(&dev->dmasound.wq);
846
847 switch (dev->pci->device) {
848 case PCI_DEVICE_ID_PHILIPS_SAA7133:
849 case PCI_DEVICE_ID_PHILIPS_SAA7135:
850 saa_writel(0x588 >> 2, 0x00000fff);
851 saa_writel(0x58c >> 2, 0x00543210);
852 saa_dsp_writel(dev, 0x46c >> 2, 0xbbbbbb);
853 break;
854 }
855
856 /* dsp */
857 dev->dmasound.rate = 32000;
858 if (rate)
859 dev->dmasound.rate = rate;
860 dev->dmasound.rate = (dev->dmasound.rate > 40000) ? 48000 : 32000;
861
862 /* mixer */
863 dev->dmasound.line1 = 50;
864 dev->dmasound.line2 = 50;
865 mixer_level(dev,LINE1,dev->dmasound.line1);
866 mixer_level(dev,LINE2,dev->dmasound.line2);
867 mixer_recsrc(dev, (dev->dmasound.rate == 32000) ? TV : LINE2);
868
869 return 0;
870}
871
872int saa7134_oss_fini(struct saa7134_dev *dev)
873{
874 /* nothing */
875 return 0;
876}
877
878void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status)
879{
880 int next_blk, reg = 0;
881
882 spin_lock(&dev->slock);
883 if (UNSET == dev->dmasound.dma_blk) {
884 dprintk("irq: recording stopped\n");
885 goto done;
886 }
887 if (0 != (status & 0x0f000000))
888 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
889 if (0 == (status & 0x10000000)) {
890 /* odd */
891 if (0 == (dev->dmasound.dma_blk & 0x01))
892 reg = SAA7134_RS_BA1(6);
893 } else {
894 /* even */
895 if (1 == (dev->dmasound.dma_blk & 0x01))
896 reg = SAA7134_RS_BA2(6);
897 }
898 if (0 == reg) {
899 dprintk("irq: field oops [%s]\n",
900 (status & 0x10000000) ? "even" : "odd");
901 goto done;
902 }
903 if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
904 dprintk("irq: overrun [full=%d/%d]\n",dev->dmasound.read_count,
905 dev->dmasound.bufsize);
906 dsp_dma_stop(dev);
907 goto done;
908 }
909
910 /* next block addr */
911 next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
912 saa_writel(reg,next_blk * dev->dmasound.blksize);
913 if (debug > 2)
914 dprintk("irq: ok, %s, next_blk=%d, addr=%x\n",
915 (status & 0x10000000) ? "even" : "odd ", next_blk,
916 next_blk * dev->dmasound.blksize);
917
918 /* update status & wake waiting readers */
919 dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
920 dev->dmasound.read_count += dev->dmasound.blksize;
921 wake_up(&dev->dmasound.wq);
922
923 done:
924 spin_unlock(&dev->slock);
925}
926
927static int saa7134_dsp_create(struct saa7134_dev *dev)
928{
929 int err;
930
931 err = dev->dmasound.minor_dsp =
932 register_sound_dsp(&saa7134_dsp_fops,
933 dsp_nr[dev->nr]);
934 if (err < 0) {
935 goto fail;
936 }
937 printk(KERN_INFO "%s: registered device dsp%d\n",
938 dev->name,dev->dmasound.minor_dsp >> 4);
939
940 err = dev->dmasound.minor_mixer =
941 register_sound_mixer(&saa7134_mixer_fops,
942 mixer_nr[dev->nr]);
943 if (err < 0)
944 goto fail;
945 printk(KERN_INFO "%s: registered device mixer%d\n",
946 dev->name,dev->dmasound.minor_mixer >> 4);
947
948 return 0;
949
950fail:
951 unregister_sound_dsp(dev->dmasound.minor_dsp);
952 return 0;
953
954
955}
956
957static int oss_device_init(struct saa7134_dev *dev)
958{
959 dev->dmasound.priv_data = dev;
960 saa7134_oss_init1(dev);
961 saa7134_dsp_create(dev);
962 return 1;
963}
964
965static int oss_device_exit(struct saa7134_dev *dev)
966{
967
968 unregister_sound_mixer(dev->dmasound.minor_mixer);
969 unregister_sound_dsp(dev->dmasound.minor_dsp);
970
971 saa7134_oss_fini(dev);
972
973 if (dev->pci->irq > 0) {
974 synchronize_irq(dev->pci->irq);
975 free_irq(dev->pci->irq,&dev->dmasound);
976 }
977
978 dev->dmasound.priv_data = NULL;
979 return 1;
980}
981
982static int saa7134_oss_init(void)
983{
984 struct saa7134_dev *dev = NULL;
985 struct list_head *list;
986
987 if (!saa7134_dmasound_init && !saa7134_dmasound_exit) {
988 saa7134_dmasound_init = oss_device_init;
989 saa7134_dmasound_exit = oss_device_exit;
990 } else {
991 printk(KERN_WARNING "saa7134 OSS: can't load, DMA sound handler already assigned (probably to ALSA)\n");
992 return -EBUSY;
993 }
994
995 printk(KERN_INFO "saa7134 OSS driver for DMA sound loaded\n");
996
997
998 list_for_each(list,&saa7134_devlist) {
999 dev = list_entry(list, struct saa7134_dev, devlist);
1000 if (dev->dmasound.priv_data == NULL) {
1001 oss_device_init(dev);
1002 } else {
1003 printk(KERN_ERR "saa7134 OSS: DMA sound is being handled by ALSA, ignoring %s\n",dev->name);
1004 return -EBUSY;
1005 }
1006 }
1007
1008 if (dev == NULL)
1009 printk(KERN_INFO "saa7134 OSS: no saa7134 cards found\n");
1010
1011 return 0;
1012
1013}
1014
1015static void saa7134_oss_exit(void)
1016{
1017 struct saa7134_dev *dev;
1018
1019 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1020 /* Device isn't registered by OSS, probably ALSA's */
1021 if (!dev->dmasound.minor_dsp)
1022 continue;
1023
1024 oss_device_exit(dev);
1025 }
1026
1027 saa7134_dmasound_init = NULL;
1028 saa7134_dmasound_exit = NULL;
1029
1030 printk(KERN_INFO "saa7134 OSS driver for DMA sound unloaded\n");
1031
1032 return;
1033}
1034
1035/* We initialize this late, to make sure the sound system is up and running */
1036late_initcall(saa7134_oss_init);
1037module_exit(saa7134_oss_exit);
1038MODULE_LICENSE("GPL");
1039MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
1040
1041/* ----------------------------------------------------------- */
1042/*
1043 * Local variables:
1044 * c-basic-offset: 8
1045 * End:
1046 */
diff --git a/drivers/media/video/saa7134/saa7134-ts.c b/drivers/media/video/saa7134/saa7134-ts.c
index 4b63ad3e8466..f1b8fcaeb43a 100644
--- a/drivers/media/video/saa7134/saa7134-ts.c
+++ b/drivers/media/video/saa7134/saa7134-ts.c
@@ -47,7 +47,7 @@ static int buffer_activate(struct saa7134_dev *dev,
47{ 47{
48 48
49 dprintk("buffer_activate [%p]",buf); 49 dprintk("buffer_activate [%p]",buf);
50 buf->vb.state = STATE_ACTIVE; 50 buf->vb.state = VIDEOBUF_ACTIVE;
51 buf->top_seen = 0; 51 buf->top_seen = 0;
52 52
53 if (NULL == next) 53 if (NULL == next)
@@ -91,7 +91,7 @@ static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
91 saa7134_dma_free(q,buf); 91 saa7134_dma_free(q,buf);
92 } 92 }
93 93
94 if (STATE_NEEDS_INIT == buf->vb.state) { 94 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
95 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); 95 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
96 96
97 buf->vb.width = llength; 97 buf->vb.width = llength;
@@ -121,7 +121,7 @@ static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
121 saa_writel(SAA7134_RS_PITCH(5),TS_PACKET_SIZE); 121 saa_writel(SAA7134_RS_PITCH(5),TS_PACKET_SIZE);
122 saa_writel(SAA7134_RS_CONTROL(5),control); 122 saa_writel(SAA7134_RS_CONTROL(5),control);
123 123
124 buf->vb.state = STATE_PREPARED; 124 buf->vb.state = VIDEOBUF_PREPARED;
125 buf->activate = buffer_activate; 125 buf->activate = buffer_activate;
126 buf->vb.field = field; 126 buf->vb.field = field;
127 return 0; 127 return 0;
@@ -242,7 +242,7 @@ void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status)
242 if ((status & 0x100000) != 0x100000) 242 if ((status & 0x100000) != 0x100000)
243 goto done; 243 goto done;
244 } 244 }
245 saa7134_buffer_finish(dev,&dev->ts_q,STATE_DONE); 245 saa7134_buffer_finish(dev,&dev->ts_q,VIDEOBUF_DONE);
246 } 246 }
247 saa7134_buffer_next(dev,&dev->ts_q); 247 saa7134_buffer_next(dev,&dev->ts_q);
248 248
diff --git a/drivers/media/video/saa7134/saa7134-tvaudio.c b/drivers/media/video/saa7134/saa7134-tvaudio.c
index f8e304c76232..4e9810469ae3 100644
--- a/drivers/media/video/saa7134/saa7134-tvaudio.c
+++ b/drivers/media/video/saa7134/saa7134-tvaudio.c
@@ -163,32 +163,6 @@ static struct saa7134_tvaudio tvaudio[] = {
163 163
164/* ------------------------------------------------------------------ */ 164/* ------------------------------------------------------------------ */
165 165
166static void tvaudio_init(struct saa7134_dev *dev)
167{
168 int clock = saa7134_boards[dev->board].audio_clock;
169
170 if (UNSET != audio_clock_override)
171 clock = audio_clock_override;
172
173 /* init all audio registers */
174 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x00);
175 if (need_resched())
176 schedule();
177 else
178 udelay(10);
179
180 saa_writeb(SAA7134_AUDIO_CLOCK0, clock & 0xff);
181 saa_writeb(SAA7134_AUDIO_CLOCK1, (clock >> 8) & 0xff);
182 saa_writeb(SAA7134_AUDIO_CLOCK2, (clock >> 16) & 0xff);
183 /* frame locked audio is mandatory for NICAM */
184 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x01);
185
186 saa_writeb(SAA7134_NICAM_ERROR_LOW, 0x14);
187 saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
188 saa_writeb(SAA7134_MONITOR_SELECT, 0xa0);
189 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
190}
191
192static u32 tvaudio_carr2reg(u32 carrier) 166static u32 tvaudio_carr2reg(u32 carrier)
193{ 167{
194 u64 a = carrier; 168 u64 a = carrier;
@@ -517,9 +491,13 @@ static int tvaudio_thread(void *data)
517 dev->thread.scan1 = dev->thread.scan2; 491 dev->thread.scan1 = dev->thread.scan2;
518 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1); 492 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
519 dev->tvaudio = NULL; 493 dev->tvaudio = NULL;
520 tvaudio_init(dev); 494
495 saa_writeb(SAA7134_MONITOR_SELECT, 0xa0);
496 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
497
521 if (dev->ctl_automute) 498 if (dev->ctl_automute)
522 dev->automute = 1; 499 dev->automute = 1;
500
523 mute_input_7134(dev); 501 mute_input_7134(dev);
524 502
525 /* give the tuner some time */ 503 /* give the tuner some time */
@@ -784,27 +762,15 @@ static int mute_input_7133(struct saa7134_dev *dev)
784static int tvaudio_thread_ddep(void *data) 762static int tvaudio_thread_ddep(void *data)
785{ 763{
786 struct saa7134_dev *dev = data; 764 struct saa7134_dev *dev = data;
787 u32 value, norms, clock; 765 u32 value, norms;
788 766
789 767
790 set_freezable(); 768 set_freezable();
791
792 clock = saa7134_boards[dev->board].audio_clock;
793 if (UNSET != audio_clock_override)
794 clock = audio_clock_override;
795 saa_writel(0x598 >> 2, clock);
796
797 /* unmute */
798 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
799 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
800
801 for (;;) { 769 for (;;) {
802 tvaudio_sleep(dev,-1); 770 tvaudio_sleep(dev,-1);
803 if (kthread_should_stop()) 771 if (kthread_should_stop())
804 goto done; 772 goto done;
805
806 restart: 773 restart:
807
808 try_to_freeze(); 774 try_to_freeze();
809 775
810 dev->thread.scan1 = dev->thread.scan2; 776 dev->thread.scan1 = dev->thread.scan2;
@@ -978,6 +944,38 @@ int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
978 return retval; 944 return retval;
979} 945}
980 946
947void saa7134_tvaudio_init(struct saa7134_dev *dev)
948{
949 int clock = saa7134_boards[dev->board].audio_clock;
950
951 if (UNSET != audio_clock_override)
952 clock = audio_clock_override;
953
954 switch (dev->pci->device) {
955 case PCI_DEVICE_ID_PHILIPS_SAA7134:
956 /* init all audio registers */
957 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x00);
958 if (need_resched())
959 schedule();
960 else
961 udelay(10);
962
963 saa_writeb(SAA7134_AUDIO_CLOCK0, clock & 0xff);
964 saa_writeb(SAA7134_AUDIO_CLOCK1, (clock >> 8) & 0xff);
965 saa_writeb(SAA7134_AUDIO_CLOCK2, (clock >> 16) & 0xff);
966 /* frame locked audio is mandatory for NICAM */
967 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x01);
968 saa_writeb(SAA7134_NICAM_ERROR_LOW, 0x14);
969 saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
970 break;
971 case PCI_DEVICE_ID_PHILIPS_SAA7133:
972 case PCI_DEVICE_ID_PHILIPS_SAA7135:
973 saa_writel(0x598 >> 2, clock);
974 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
975 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
976 }
977}
978
981int saa7134_tvaudio_init2(struct saa7134_dev *dev) 979int saa7134_tvaudio_init2(struct saa7134_dev *dev)
982{ 980{
983 int (*my_thread)(void *data) = NULL; 981 int (*my_thread)(void *data) = NULL;
@@ -994,6 +992,7 @@ int saa7134_tvaudio_init2(struct saa7134_dev *dev)
994 992
995 dev->thread.thread = NULL; 993 dev->thread.thread = NULL;
996 if (my_thread) { 994 if (my_thread) {
995 saa7134_tvaudio_init(dev);
997 /* start tvaudio thread */ 996 /* start tvaudio thread */
998 dev->thread.thread = kthread_run(my_thread, dev, "%s", dev->name); 997 dev->thread.thread = kthread_run(my_thread, dev, "%s", dev->name);
999 if (IS_ERR(dev->thread.thread)) { 998 if (IS_ERR(dev->thread.thread)) {
diff --git a/drivers/media/video/saa7134/saa7134-vbi.c b/drivers/media/video/saa7134/saa7134-vbi.c
index 81a2aedeff5c..f0d5ed9c2b06 100644
--- a/drivers/media/video/saa7134/saa7134-vbi.c
+++ b/drivers/media/video/saa7134/saa7134-vbi.c
@@ -85,7 +85,7 @@ static int buffer_activate(struct saa7134_dev *dev,
85 unsigned long control,base; 85 unsigned long control,base;
86 86
87 dprintk("buffer_activate [%p]\n",buf); 87 dprintk("buffer_activate [%p]\n",buf);
88 buf->vb.state = STATE_ACTIVE; 88 buf->vb.state = VIDEOBUF_ACTIVE;
89 buf->top_seen = 0; 89 buf->top_seen = 0;
90 90
91 task_init(dev,buf,TASK_A); 91 task_init(dev,buf,TASK_A);
@@ -136,7 +136,7 @@ static int buffer_prepare(struct videobuf_queue *q,
136 if (buf->vb.size != size) 136 if (buf->vb.size != size)
137 saa7134_dma_free(q,buf); 137 saa7134_dma_free(q,buf);
138 138
139 if (STATE_NEEDS_INIT == buf->vb.state) { 139 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
140 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); 140 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
141 141
142 buf->vb.width = llength; 142 buf->vb.width = llength;
@@ -154,7 +154,7 @@ static int buffer_prepare(struct videobuf_queue *q,
154 if (err) 154 if (err)
155 goto oops; 155 goto oops;
156 } 156 }
157 buf->vb.state = STATE_PREPARED; 157 buf->vb.state = VIDEOBUF_PREPARED;
158 buf->activate = buffer_activate; 158 buf->activate = buffer_activate;
159 buf->vb.field = field; 159 buf->vb.field = field;
160 return 0; 160 return 0;
@@ -240,7 +240,7 @@ void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status)
240 goto done; 240 goto done;
241 241
242 dev->vbi_q.curr->vb.field_count = dev->vbi_fieldcount; 242 dev->vbi_q.curr->vb.field_count = dev->vbi_fieldcount;
243 saa7134_buffer_finish(dev,&dev->vbi_q,STATE_DONE); 243 saa7134_buffer_finish(dev,&dev->vbi_q,VIDEOBUF_DONE);
244 } 244 }
245 saa7134_buffer_next(dev,&dev->vbi_q); 245 saa7134_buffer_next(dev,&dev->vbi_q);
246 246
diff --git a/drivers/media/video/saa7134/saa7134-video.c b/drivers/media/video/saa7134/saa7134-video.c
index 6396d9b5c063..1184d359e848 100644
--- a/drivers/media/video/saa7134/saa7134-video.c
+++ b/drivers/media/video/saa7134/saa7134-video.c
@@ -38,7 +38,7 @@
38 38
39/* ------------------------------------------------------------------ */ 39/* ------------------------------------------------------------------ */
40 40
41static unsigned int video_debug = 0; 41unsigned int video_debug;
42static unsigned int gbuffers = 8; 42static unsigned int gbuffers = 8;
43static unsigned int noninterlaced = 0; 43static unsigned int noninterlaced = 0;
44static unsigned int gbufsize = 720*576*4; 44static unsigned int gbufsize = 720*576*4;
@@ -54,7 +54,7 @@ module_param_string(secam, secam, sizeof(secam), 0644);
54MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc"); 54MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
55 55
56 56
57#define dprintk(fmt, arg...) if (video_debug) \ 57#define dprintk(fmt, arg...) if (video_debug&0x04) \
58 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg) 58 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
59 59
60/* ------------------------------------------------------------------ */ 60/* ------------------------------------------------------------------ */
@@ -540,9 +540,8 @@ void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
540 540
541/* ------------------------------------------------------------------ */ 541/* ------------------------------------------------------------------ */
542 542
543void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm) 543static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
544{ 544{
545
546 dprintk("set tv norm = %s\n",norm->name); 545 dprintk("set tv norm = %s\n",norm->name);
547 dev->tvnorm = norm; 546 dev->tvnorm = norm;
548 547
@@ -561,7 +560,6 @@ void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
561 dev->crop_current = dev->crop_defrect; 560 dev->crop_current = dev->crop_defrect;
562 561
563 saa7134_set_tvnorm_hw(dev); 562 saa7134_set_tvnorm_hw(dev);
564
565} 563}
566 564
567static void video_mux(struct saa7134_dev *dev, int input) 565static void video_mux(struct saa7134_dev *dev, int input)
@@ -945,7 +943,7 @@ static int buffer_activate(struct saa7134_dev *dev,
945 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */ 943 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
946 944
947 dprintk("buffer_activate buf=%p\n",buf); 945 dprintk("buffer_activate buf=%p\n",buf);
948 buf->vb.state = STATE_ACTIVE; 946 buf->vb.state = VIDEOBUF_ACTIVE;
949 buf->top_seen = 0; 947 buf->top_seen = 0;
950 948
951 set_size(dev,TASK_A,buf->vb.width,buf->vb.height, 949 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
@@ -1054,7 +1052,7 @@ static int buffer_prepare(struct videobuf_queue *q,
1054 saa7134_dma_free(q,buf); 1052 saa7134_dma_free(q,buf);
1055 } 1053 }
1056 1054
1057 if (STATE_NEEDS_INIT == buf->vb.state) { 1055 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1058 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); 1056 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1059 1057
1060 buf->vb.width = fh->width; 1058 buf->vb.width = fh->width;
@@ -1074,7 +1072,7 @@ static int buffer_prepare(struct videobuf_queue *q,
1074 if (err) 1072 if (err)
1075 goto oops; 1073 goto oops;
1076 } 1074 }
1077 buf->vb.state = STATE_PREPARED; 1075 buf->vb.state = VIDEOBUF_PREPARED;
1078 buf->activate = buffer_activate; 1076 buf->activate = buffer_activate;
1079 return 0; 1077 return 0;
1080 1078
@@ -1119,8 +1117,10 @@ static struct videobuf_queue_ops video_qops = {
1119 1117
1120/* ------------------------------------------------------------------ */ 1118/* ------------------------------------------------------------------ */
1121 1119
1122static int get_control(struct saa7134_dev *dev, struct v4l2_control *c) 1120int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
1123{ 1121{
1122 struct saa7134_fh *fh = priv;
1123 struct saa7134_dev *dev = fh->dev;
1124 const struct v4l2_queryctrl* ctrl; 1124 const struct v4l2_queryctrl* ctrl;
1125 1125
1126 ctrl = ctrl_by_id(c->id); 1126 ctrl = ctrl_by_id(c->id);
@@ -1165,17 +1165,27 @@ static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1165 } 1165 }
1166 return 0; 1166 return 0;
1167} 1167}
1168EXPORT_SYMBOL_GPL(saa7134_g_ctrl);
1168 1169
1169static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh, 1170int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
1170 struct v4l2_control *c)
1171{ 1171{
1172 const struct v4l2_queryctrl* ctrl; 1172 const struct v4l2_queryctrl* ctrl;
1173 struct saa7134_fh *fh = f;
1174 struct saa7134_dev *dev = fh->dev;
1173 unsigned long flags; 1175 unsigned long flags;
1174 int restart_overlay = 0; 1176 int restart_overlay = 0;
1177 int err = -EINVAL;
1178
1179 err = v4l2_prio_check(&dev->prio, &fh->prio);
1180 if (0 != err)
1181 return err;
1182
1183 mutex_lock(&dev->lock);
1175 1184
1176 ctrl = ctrl_by_id(c->id); 1185 ctrl = ctrl_by_id(c->id);
1177 if (NULL == ctrl) 1186 if (NULL == ctrl)
1178 return -EINVAL; 1187 goto error;
1188
1179 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value); 1189 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1180 switch (ctrl->type) { 1190 switch (ctrl->type) {
1181 case V4L2_CTRL_TYPE_BOOLEAN: 1191 case V4L2_CTRL_TYPE_BOOLEAN:
@@ -1236,18 +1246,26 @@ static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1236 restart_overlay = 1; 1246 restart_overlay = 1;
1237 break; 1247 break;
1238 case V4L2_CID_PRIVATE_AUTOMUTE: 1248 case V4L2_CID_PRIVATE_AUTOMUTE:
1249 {
1250 struct v4l2_priv_tun_config tda9887_cfg;
1251
1252 tda9887_cfg.tuner = TUNER_TDA9887;
1253 tda9887_cfg.priv = &dev->tda9887_conf;
1254
1239 dev->ctl_automute = c->value; 1255 dev->ctl_automute = c->value;
1240 if (dev->tda9887_conf) { 1256 if (dev->tda9887_conf) {
1241 if (dev->ctl_automute) 1257 if (dev->ctl_automute)
1242 dev->tda9887_conf |= TDA9887_AUTOMUTE; 1258 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1243 else 1259 else
1244 dev->tda9887_conf &= ~TDA9887_AUTOMUTE; 1260 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1245 saa7134_i2c_call_clients(dev, TDA9887_SET_CONFIG, 1261
1246 &dev->tda9887_conf); 1262 saa7134_i2c_call_clients(dev, TUNER_SET_CONFIG,
1263 &tda9887_cfg);
1247 } 1264 }
1248 break; 1265 break;
1266 }
1249 default: 1267 default:
1250 return -EINVAL; 1268 goto error;
1251 } 1269 }
1252 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) { 1270 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1253 spin_lock_irqsave(&dev->slock,flags); 1271 spin_lock_irqsave(&dev->slock,flags);
@@ -1255,8 +1273,13 @@ static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1255 start_preview(dev,fh); 1273 start_preview(dev,fh);
1256 spin_unlock_irqrestore(&dev->slock,flags); 1274 spin_unlock_irqrestore(&dev->slock,flags);
1257 } 1275 }
1258 return 0; 1276 err = 0;
1277
1278error:
1279 mutex_unlock(&dev->lock);
1280 return err;
1259} 1281}
1282EXPORT_SYMBOL_GPL(saa7134_s_ctrl);
1260 1283
1261/* ------------------------------------------------------------------ */ 1284/* ------------------------------------------------------------------ */
1262 1285
@@ -1413,8 +1436,8 @@ video_poll(struct file *file, struct poll_table_struct *wait)
1413 return POLLERR; 1436 return POLLERR;
1414 1437
1415 poll_wait(file, &buf->done, wait); 1438 poll_wait(file, &buf->done, wait);
1416 if (buf->state == STATE_DONE || 1439 if (buf->state == VIDEOBUF_DONE ||
1417 buf->state == STATE_ERROR) 1440 buf->state == VIDEOBUF_ERROR)
1418 return POLLIN|POLLRDNORM; 1441 return POLLIN|POLLRDNORM;
1419 return 0; 1442 return 0;
1420} 1443}
@@ -1478,8 +1501,11 @@ static int video_mmap(struct file *file, struct vm_area_struct * vma)
1478 1501
1479/* ------------------------------------------------------------------ */ 1502/* ------------------------------------------------------------------ */
1480 1503
1481static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f) 1504static int saa7134_try_get_set_fmt_vbi(struct file *file, void *priv,
1505 struct v4l2_format *f)
1482{ 1506{
1507 struct saa7134_fh *fh = priv;
1508 struct saa7134_dev *dev = fh->dev;
1483 struct saa7134_tvnorm *norm = dev->tvnorm; 1509 struct saa7134_tvnorm *norm = dev->tvnorm;
1484 1510
1485 f->fmt.vbi.sampling_rate = 6750000 * 4; 1511 f->fmt.vbi.sampling_rate = 6750000 * 4;
@@ -1492,837 +1518,805 @@ static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1492 f->fmt.vbi.count[1] = f->fmt.vbi.count[0]; 1518 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1493 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */ 1519 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1494 1520
1521 return 0;
1495} 1522}
1496 1523
1497static int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh, 1524static int saa7134_g_fmt_cap(struct file *file, void *priv,
1498 struct v4l2_format *f) 1525 struct v4l2_format *f)
1499{ 1526{
1500 switch (f->type) { 1527 struct saa7134_fh *fh = priv;
1501 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 1528
1502 memset(&f->fmt.pix,0,sizeof(f->fmt.pix)); 1529 f->fmt.pix.width = fh->width;
1503 f->fmt.pix.width = fh->width; 1530 f->fmt.pix.height = fh->height;
1504 f->fmt.pix.height = fh->height; 1531 f->fmt.pix.field = fh->cap.field;
1505 f->fmt.pix.field = fh->cap.field; 1532 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1506 f->fmt.pix.pixelformat = fh->fmt->fourcc; 1533 f->fmt.pix.bytesperline =
1507 f->fmt.pix.bytesperline = 1534 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1508 (f->fmt.pix.width * fh->fmt->depth) >> 3; 1535 f->fmt.pix.sizeimage =
1509 f->fmt.pix.sizeimage = 1536 f->fmt.pix.height * f->fmt.pix.bytesperline;
1510 f->fmt.pix.height * f->fmt.pix.bytesperline; 1537 return 0;
1511 return 0;
1512 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1513 if (saa7134_no_overlay > 0) {
1514 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1515 return -EINVAL;
1516 }
1517 f->fmt.win = fh->win;
1518 return 0;
1519 case V4L2_BUF_TYPE_VBI_CAPTURE:
1520 saa7134_vbi_fmt(dev,f);
1521 return 0;
1522 default:
1523 return -EINVAL;
1524 }
1525} 1538}
1526 1539
1527static int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh, 1540static int saa7134_g_fmt_overlay(struct file *file, void *priv,
1528 struct v4l2_format *f) 1541 struct v4l2_format *f)
1529{ 1542{
1530 int err; 1543 struct saa7134_fh *fh = priv;
1531 1544
1532 switch (f->type) { 1545 if (saa7134_no_overlay > 0) {
1533 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 1546 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1534 { 1547 return -EINVAL;
1535 struct saa7134_format *fmt; 1548 }
1536 enum v4l2_field field; 1549 f->fmt.win = fh->win;
1537 unsigned int maxw, maxh;
1538 1550
1539 fmt = format_by_fourcc(f->fmt.pix.pixelformat); 1551 return 0;
1540 if (NULL == fmt) 1552}
1541 return -EINVAL;
1542 1553
1543 field = f->fmt.pix.field; 1554static int saa7134_try_fmt_cap(struct file *file, void *priv,
1544 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width); 1555 struct v4l2_format *f)
1545 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height); 1556{
1557 struct saa7134_fh *fh = priv;
1558 struct saa7134_dev *dev = fh->dev;
1559 struct saa7134_format *fmt;
1560 enum v4l2_field field;
1561 unsigned int maxw, maxh;
1546 1562
1547 if (V4L2_FIELD_ANY == field) { 1563 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1548 field = (f->fmt.pix.height > maxh/2) 1564 if (NULL == fmt)
1549 ? V4L2_FIELD_INTERLACED 1565 return -EINVAL;
1550 : V4L2_FIELD_BOTTOM;
1551 }
1552 switch (field) {
1553 case V4L2_FIELD_TOP:
1554 case V4L2_FIELD_BOTTOM:
1555 maxh = maxh / 2;
1556 break;
1557 case V4L2_FIELD_INTERLACED:
1558 break;
1559 default:
1560 return -EINVAL;
1561 }
1562 1566
1563 f->fmt.pix.field = field; 1567 field = f->fmt.pix.field;
1564 if (f->fmt.pix.width < 48) 1568 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1565 f->fmt.pix.width = 48; 1569 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1566 if (f->fmt.pix.height < 32)
1567 f->fmt.pix.height = 32;
1568 if (f->fmt.pix.width > maxw)
1569 f->fmt.pix.width = maxw;
1570 if (f->fmt.pix.height > maxh)
1571 f->fmt.pix.height = maxh;
1572 f->fmt.pix.width &= ~0x03;
1573 f->fmt.pix.bytesperline =
1574 (f->fmt.pix.width * fmt->depth) >> 3;
1575 f->fmt.pix.sizeimage =
1576 f->fmt.pix.height * f->fmt.pix.bytesperline;
1577 1570
1578 return 0; 1571 if (V4L2_FIELD_ANY == field) {
1572 field = (f->fmt.pix.height > maxh/2)
1573 ? V4L2_FIELD_INTERLACED
1574 : V4L2_FIELD_BOTTOM;
1579 } 1575 }
1580 case V4L2_BUF_TYPE_VIDEO_OVERLAY: 1576 switch (field) {
1581 if (saa7134_no_overlay > 0) { 1577 case V4L2_FIELD_TOP:
1582 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n"); 1578 case V4L2_FIELD_BOTTOM:
1583 return -EINVAL; 1579 maxh = maxh / 2;
1584 } 1580 break;
1585 err = verify_preview(dev,&f->fmt.win); 1581 case V4L2_FIELD_INTERLACED:
1586 if (0 != err) 1582 break;
1587 return err;
1588 return 0;
1589 case V4L2_BUF_TYPE_VBI_CAPTURE:
1590 saa7134_vbi_fmt(dev,f);
1591 return 0;
1592 default: 1583 default:
1593 return -EINVAL; 1584 return -EINVAL;
1594 } 1585 }
1586
1587 f->fmt.pix.field = field;
1588 if (f->fmt.pix.width < 48)
1589 f->fmt.pix.width = 48;
1590 if (f->fmt.pix.height < 32)
1591 f->fmt.pix.height = 32;
1592 if (f->fmt.pix.width > maxw)
1593 f->fmt.pix.width = maxw;
1594 if (f->fmt.pix.height > maxh)
1595 f->fmt.pix.height = maxh;
1596 f->fmt.pix.width &= ~0x03;
1597 f->fmt.pix.bytesperline =
1598 (f->fmt.pix.width * fmt->depth) >> 3;
1599 f->fmt.pix.sizeimage =
1600 f->fmt.pix.height * f->fmt.pix.bytesperline;
1601
1602 return 0;
1595} 1603}
1596 1604
1597static int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh, 1605static int saa7134_try_fmt_overlay(struct file *file, void *priv,
1598 struct v4l2_format *f) 1606 struct v4l2_format *f)
1599{ 1607{
1600 unsigned long flags; 1608 struct saa7134_fh *fh = priv;
1601 int err; 1609 struct saa7134_dev *dev = fh->dev;
1602
1603 switch (f->type) {
1604 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1605 err = saa7134_try_fmt(dev,fh,f);
1606 if (0 != err)
1607 return err;
1608
1609 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1610 fh->width = f->fmt.pix.width;
1611 fh->height = f->fmt.pix.height;
1612 fh->cap.field = f->fmt.pix.field;
1613 return 0;
1614 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1615 if (saa7134_no_overlay > 0) {
1616 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1617 return -EINVAL;
1618 }
1619 err = verify_preview(dev,&f->fmt.win);
1620 if (0 != err)
1621 return err;
1622
1623 mutex_lock(&dev->lock);
1624 fh->win = f->fmt.win;
1625 fh->nclips = f->fmt.win.clipcount;
1626 if (fh->nclips > 8)
1627 fh->nclips = 8;
1628 if (copy_from_user(fh->clips,f->fmt.win.clips,
1629 sizeof(struct v4l2_clip)*fh->nclips)) {
1630 mutex_unlock(&dev->lock);
1631 return -EFAULT;
1632 }
1633 1610
1634 if (res_check(fh, RESOURCE_OVERLAY)) { 1611 if (saa7134_no_overlay > 0) {
1635 spin_lock_irqsave(&dev->slock,flags); 1612 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1636 stop_preview(dev,fh);
1637 start_preview(dev,fh);
1638 spin_unlock_irqrestore(&dev->slock,flags);
1639 }
1640 mutex_unlock(&dev->lock);
1641 return 0;
1642 case V4L2_BUF_TYPE_VBI_CAPTURE:
1643 saa7134_vbi_fmt(dev,f);
1644 return 0;
1645 default:
1646 return -EINVAL; 1613 return -EINVAL;
1647 } 1614 }
1615
1616 return verify_preview(dev, &f->fmt.win);
1648} 1617}
1649 1618
1650int saa7134_common_ioctl(struct saa7134_dev *dev, 1619static int saa7134_s_fmt_cap(struct file *file, void *priv,
1651 unsigned int cmd, void *arg) 1620 struct v4l2_format *f)
1652{ 1621{
1622 struct saa7134_fh *fh = priv;
1653 int err; 1623 int err;
1654 1624
1655 switch (cmd) { 1625 err = saa7134_try_fmt_cap(file, priv, f);
1656 case VIDIOC_QUERYCTRL: 1626 if (0 != err)
1657 { 1627 return err;
1658 const struct v4l2_queryctrl *ctrl;
1659 struct v4l2_queryctrl *c = arg;
1660 1628
1661 if ((c->id < V4L2_CID_BASE || 1629 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1662 c->id >= V4L2_CID_LASTP1) && 1630 fh->width = f->fmt.pix.width;
1663 (c->id < V4L2_CID_PRIVATE_BASE || 1631 fh->height = f->fmt.pix.height;
1664 c->id >= V4L2_CID_PRIVATE_LASTP1)) 1632 fh->cap.field = f->fmt.pix.field;
1665 return -EINVAL; 1633 return 0;
1666 ctrl = ctrl_by_id(c->id); 1634}
1667 *c = (NULL != ctrl) ? *ctrl : no_ctrl; 1635
1668 return 0; 1636static int saa7134_s_fmt_overlay(struct file *file, void *priv,
1637 struct v4l2_format *f)
1638{
1639 struct saa7134_fh *fh = priv;
1640 struct saa7134_dev *dev = fh->dev;
1641 int err;
1642 unsigned int flags;
1643
1644 if (saa7134_no_overlay > 0) {
1645 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1646 return -EINVAL;
1669 } 1647 }
1670 case VIDIOC_G_CTRL: 1648 err = verify_preview(dev, &f->fmt.win);
1671 return get_control(dev,arg); 1649 if (0 != err)
1672 case VIDIOC_S_CTRL:
1673 {
1674 mutex_lock(&dev->lock);
1675 err = set_control(dev,NULL,arg);
1676 mutex_unlock(&dev->lock);
1677 return err; 1650 return err;
1678 }
1679 /* --- input switching --------------------------------------- */
1680 case VIDIOC_ENUMINPUT:
1681 {
1682 struct v4l2_input *i = arg;
1683 unsigned int n;
1684 1651
1685 n = i->index; 1652 mutex_lock(&dev->lock);
1686 if (n >= SAA7134_INPUT_MAX)
1687 return -EINVAL;
1688 if (NULL == card_in(dev,i->index).name)
1689 return -EINVAL;
1690 memset(i,0,sizeof(*i));
1691 i->index = n;
1692 i->type = V4L2_INPUT_TYPE_CAMERA;
1693 strcpy(i->name,card_in(dev,n).name);
1694 if (card_in(dev,n).tv)
1695 i->type = V4L2_INPUT_TYPE_TUNER;
1696 i->audioset = 1;
1697 if (n == dev->ctl_input) {
1698 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1699 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1700
1701 if (0 != (v1 & 0x40))
1702 i->status |= V4L2_IN_ST_NO_H_LOCK;
1703 if (0 != (v2 & 0x40))
1704 i->status |= V4L2_IN_ST_NO_SYNC;
1705 if (0 != (v2 & 0x0e))
1706 i->status |= V4L2_IN_ST_MACROVISION;
1707 }
1708 for (n = 0; n < TVNORMS; n++)
1709 i->std |= tvnorms[n].id;
1710 return 0;
1711 }
1712 case VIDIOC_G_INPUT:
1713 {
1714 int *i = arg;
1715 *i = dev->ctl_input;
1716 return 0;
1717 }
1718 case VIDIOC_S_INPUT:
1719 {
1720 int *i = arg;
1721 1653
1722 if (*i < 0 || *i >= SAA7134_INPUT_MAX) 1654 fh->win = f->fmt.win;
1723 return -EINVAL; 1655 fh->nclips = f->fmt.win.clipcount;
1724 if (NULL == card_in(dev,*i).name) 1656
1725 return -EINVAL; 1657 if (fh->nclips > 8)
1726 mutex_lock(&dev->lock); 1658 fh->nclips = 8;
1727 video_mux(dev,*i); 1659
1660 if (copy_from_user(fh->clips, f->fmt.win.clips,
1661 sizeof(struct v4l2_clip)*fh->nclips)) {
1728 mutex_unlock(&dev->lock); 1662 mutex_unlock(&dev->lock);
1729 return 0; 1663 return -EFAULT;
1730 } 1664 }
1731 1665
1666 if (res_check(fh, RESOURCE_OVERLAY)) {
1667 spin_lock_irqsave(&dev->slock, flags);
1668 stop_preview(dev, fh);
1669 start_preview(dev, fh);
1670 spin_unlock_irqrestore(&dev->slock, flags);
1732 } 1671 }
1672
1673 mutex_unlock(&dev->lock);
1733 return 0; 1674 return 0;
1734} 1675}
1735EXPORT_SYMBOL(saa7134_common_ioctl);
1736 1676
1737/* 1677int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
1738 * This function is _not_ called directly, but from
1739 * video_generic_ioctl (and maybe others). userspace
1740 * copying is done already, arg is a kernel pointer.
1741 */
1742static int video_do_ioctl(struct inode *inode, struct file *file,
1743 unsigned int cmd, void *arg)
1744{ 1678{
1745 struct saa7134_fh *fh = file->private_data; 1679 const struct v4l2_queryctrl *ctrl;
1680
1681 if ((c->id < V4L2_CID_BASE ||
1682 c->id >= V4L2_CID_LASTP1) &&
1683 (c->id < V4L2_CID_PRIVATE_BASE ||
1684 c->id >= V4L2_CID_PRIVATE_LASTP1))
1685 return -EINVAL;
1686 ctrl = ctrl_by_id(c->id);
1687 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1688 return 0;
1689}
1690EXPORT_SYMBOL_GPL(saa7134_queryctrl);
1691
1692static int saa7134_enum_input(struct file *file, void *priv,
1693 struct v4l2_input *i)
1694{
1695 struct saa7134_fh *fh = priv;
1696 struct saa7134_dev *dev = fh->dev;
1697 unsigned int n;
1698
1699 n = i->index;
1700 if (n >= SAA7134_INPUT_MAX)
1701 return -EINVAL;
1702 if (NULL == card_in(dev, i->index).name)
1703 return -EINVAL;
1704 memset(i, 0, sizeof(*i));
1705 i->index = n;
1706 i->type = V4L2_INPUT_TYPE_CAMERA;
1707 strcpy(i->name, card_in(dev, n).name);
1708 if (card_in(dev, n).tv)
1709 i->type = V4L2_INPUT_TYPE_TUNER;
1710 i->audioset = 1;
1711 if (n == dev->ctl_input) {
1712 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1713 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1714
1715 if (0 != (v1 & 0x40))
1716 i->status |= V4L2_IN_ST_NO_H_LOCK;
1717 if (0 != (v2 & 0x40))
1718 i->status |= V4L2_IN_ST_NO_SYNC;
1719 if (0 != (v2 & 0x0e))
1720 i->status |= V4L2_IN_ST_MACROVISION;
1721 }
1722 i->std = SAA7134_NORMS;
1723 return 0;
1724}
1725
1726static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1727{
1728 struct saa7134_fh *fh = priv;
1729 struct saa7134_dev *dev = fh->dev;
1730
1731 *i = dev->ctl_input;
1732 return 0;
1733}
1734
1735static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1736{
1737 struct saa7134_fh *fh = priv;
1746 struct saa7134_dev *dev = fh->dev; 1738 struct saa7134_dev *dev = fh->dev;
1747 unsigned long flags;
1748 int err; 1739 int err;
1749 1740
1750 if (video_debug > 1) 1741 err = v4l2_prio_check(&dev->prio, &fh->prio);
1751 v4l_print_ioctl(dev->name,cmd); 1742 if (0 != err)
1752 1743 return err;
1753 switch (cmd) {
1754 case VIDIOC_S_CTRL:
1755 case VIDIOC_S_STD:
1756 case VIDIOC_S_INPUT:
1757 case VIDIOC_S_TUNER:
1758 case VIDIOC_S_FREQUENCY:
1759 err = v4l2_prio_check(&dev->prio,&fh->prio);
1760 if (0 != err)
1761 return err;
1762 }
1763 1744
1764 switch (cmd) { 1745 if (i < 0 || i >= SAA7134_INPUT_MAX)
1765 case VIDIOC_QUERYCAP: 1746 return -EINVAL;
1766 { 1747 if (NULL == card_in(dev, i).name)
1767 struct v4l2_capability *cap = arg; 1748 return -EINVAL;
1768 unsigned int tuner_type = dev->tuner_type; 1749 mutex_lock(&dev->lock);
1769 1750 video_mux(dev, i);
1770 memset(cap,0,sizeof(*cap)); 1751 mutex_unlock(&dev->lock);
1771 strcpy(cap->driver, "saa7134"); 1752 return 0;
1772 strlcpy(cap->card, saa7134_boards[dev->board].name, 1753}
1773 sizeof(cap->card));
1774 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1775 cap->version = SAA7134_VERSION_CODE;
1776 cap->capabilities =
1777 V4L2_CAP_VIDEO_CAPTURE |
1778 V4L2_CAP_VBI_CAPTURE |
1779 V4L2_CAP_READWRITE |
1780 V4L2_CAP_STREAMING |
1781 V4L2_CAP_TUNER;
1782 if (saa7134_no_overlay <= 0) {
1783 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1784 }
1785 1754
1786 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET)) 1755static int saa7134_querycap(struct file *file, void *priv,
1787 cap->capabilities &= ~V4L2_CAP_TUNER; 1756 struct v4l2_capability *cap)
1757{
1758 struct saa7134_fh *fh = priv;
1759 struct saa7134_dev *dev = fh->dev;
1788 1760
1761 unsigned int tuner_type = dev->tuner_type;
1762
1763 strcpy(cap->driver, "saa7134");
1764 strlcpy(cap->card, saa7134_boards[dev->board].name,
1765 sizeof(cap->card));
1766 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1767 cap->version = SAA7134_VERSION_CODE;
1768 cap->capabilities =
1769 V4L2_CAP_VIDEO_CAPTURE |
1770 V4L2_CAP_VBI_CAPTURE |
1771 V4L2_CAP_READWRITE |
1772 V4L2_CAP_STREAMING |
1773 V4L2_CAP_TUNER;
1774 if (saa7134_no_overlay <= 0)
1775 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1776
1777 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1778 cap->capabilities &= ~V4L2_CAP_TUNER;
1789 return 0; 1779 return 0;
1790 } 1780}
1791 1781
1792 /* --- tv standards ------------------------------------------ */ 1782static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
1793 case VIDIOC_ENUMSTD: 1783{
1794 { 1784 struct saa7134_fh *fh = priv;
1795 struct v4l2_standard *e = arg; 1785 struct saa7134_dev *dev = fh->dev;
1796 unsigned int i; 1786 unsigned long flags;
1787 unsigned int i;
1788 v4l2_std_id fixup;
1789 int err;
1797 1790
1798 i = e->index; 1791 err = v4l2_prio_check(&dev->prio, &fh->prio);
1799 if (i >= TVNORMS) 1792 if (0 != err)
1800 return -EINVAL; 1793 return err;
1801 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1802 tvnorms[e->index].name);
1803 e->index = i;
1804 if (err < 0)
1805 return err;
1806 return 0;
1807 }
1808 case VIDIOC_G_STD:
1809 {
1810 v4l2_std_id *id = arg;
1811 1794
1812 *id = dev->tvnorm->id; 1795 for (i = 0; i < TVNORMS; i++)
1813 return 0; 1796 if (*id == tvnorms[i].id)
1814 } 1797 break;
1815 case VIDIOC_S_STD:
1816 {
1817 v4l2_std_id *id = arg;
1818 unsigned int i;
1819 v4l2_std_id fixup;
1820 1798
1799 if (i == TVNORMS)
1821 for (i = 0; i < TVNORMS; i++) 1800 for (i = 0; i < TVNORMS; i++)
1822 if (*id == tvnorms[i].id) 1801 if (*id & tvnorms[i].id)
1823 break; 1802 break;
1824 if (i == TVNORMS) 1803 if (i == TVNORMS)
1825 for (i = 0; i < TVNORMS; i++) 1804 return -EINVAL;
1826 if (*id & tvnorms[i].id) 1805
1827 break; 1806 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1828 if (i == TVNORMS) 1807 if (secam[0] == 'L' || secam[0] == 'l') {
1829 return -EINVAL; 1808 if (secam[1] == 'C' || secam[1] == 'c')
1830 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) { 1809 fixup = V4L2_STD_SECAM_LC;
1831 if (secam[0] == 'L' || secam[0] == 'l') { 1810 else
1832 if (secam[1] == 'C' || secam[1] == 'c') 1811 fixup = V4L2_STD_SECAM_L;
1833 fixup = V4L2_STD_SECAM_LC; 1812 } else {
1834 else 1813 if (secam[0] == 'D' || secam[0] == 'd')
1835 fixup = V4L2_STD_SECAM_L; 1814 fixup = V4L2_STD_SECAM_DK;
1836 } else { 1815 else
1837 if (secam[0] == 'D' || secam[0] == 'd') 1816 fixup = V4L2_STD_SECAM;
1838 fixup = V4L2_STD_SECAM_DK;
1839 else
1840 fixup = V4L2_STD_SECAM;
1841 }
1842 for (i = 0; i < TVNORMS; i++)
1843 if (fixup == tvnorms[i].id)
1844 break;
1845 } 1817 }
1846 mutex_lock(&dev->lock); 1818 for (i = 0; i < TVNORMS; i++)
1847 if (res_check(fh, RESOURCE_OVERLAY)) { 1819 if (fixup == tvnorms[i].id)
1848 spin_lock_irqsave(&dev->slock,flags); 1820 break;
1849 stop_preview(dev,fh);
1850 spin_unlock_irqrestore(&dev->slock, flags);
1851
1852 set_tvnorm(dev,&tvnorms[i]);
1853
1854 spin_lock_irqsave(&dev->slock, flags);
1855 start_preview(dev,fh);
1856 spin_unlock_irqrestore(&dev->slock,flags);
1857 } else
1858 set_tvnorm(dev,&tvnorms[i]);
1859 saa7134_tvaudio_do_scan(dev);
1860 mutex_unlock(&dev->lock);
1861 return 0;
1862 } 1821 }
1863 1822
1864 case VIDIOC_CROPCAP: 1823 *id = tvnorms[i].id;
1865 {
1866 struct v4l2_cropcap *cap = arg;
1867 1824
1868 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 1825 mutex_lock(&dev->lock);
1869 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 1826 if (res_check(fh, RESOURCE_OVERLAY)) {
1870 return -EINVAL; 1827 spin_lock_irqsave(&dev->slock, flags);
1871 cap->bounds = dev->crop_bounds; 1828 stop_preview(dev, fh);
1872 cap->defrect = dev->crop_defrect; 1829 spin_unlock_irqrestore(&dev->slock, flags);
1873 cap->pixelaspect.numerator = 1;
1874 cap->pixelaspect.denominator = 1;
1875 if (dev->tvnorm->id & V4L2_STD_525_60) {
1876 cap->pixelaspect.numerator = 11;
1877 cap->pixelaspect.denominator = 10;
1878 }
1879 if (dev->tvnorm->id & V4L2_STD_625_50) {
1880 cap->pixelaspect.numerator = 54;
1881 cap->pixelaspect.denominator = 59;
1882 }
1883 return 0;
1884 }
1885 1830
1886 case VIDIOC_G_CROP: 1831 set_tvnorm(dev, &tvnorms[i]);
1887 {
1888 struct v4l2_crop * crop = arg;
1889 1832
1890 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 1833 spin_lock_irqsave(&dev->slock, flags);
1891 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 1834 start_preview(dev, fh);
1892 return -EINVAL; 1835 spin_unlock_irqrestore(&dev->slock, flags);
1893 crop->c = dev->crop_current; 1836 } else
1894 return 0; 1837 set_tvnorm(dev, &tvnorms[i]);
1895 }
1896 case VIDIOC_S_CROP:
1897 {
1898 struct v4l2_crop *crop = arg;
1899 struct v4l2_rect *b = &dev->crop_bounds;
1900 1838
1901 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 1839 saa7134_tvaudio_do_scan(dev);
1902 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 1840 mutex_unlock(&dev->lock);
1903 return -EINVAL; 1841 return 0;
1904 if (crop->c.height < 0) 1842}
1905 return -EINVAL;
1906 if (crop->c.width < 0)
1907 return -EINVAL;
1908 1843
1909 if (res_locked(fh->dev,RESOURCE_OVERLAY)) 1844static int saa7134_cropcap(struct file *file, void *priv,
1910 return -EBUSY; 1845 struct v4l2_cropcap *cap)
1911 if (res_locked(fh->dev,RESOURCE_VIDEO)) 1846{
1912 return -EBUSY; 1847 struct saa7134_fh *fh = priv;
1848 struct saa7134_dev *dev = fh->dev;
1913 1849
1914 if (crop->c.top < b->top) 1850 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1915 crop->c.top = b->top; 1851 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1916 if (crop->c.top > b->top + b->height) 1852 return -EINVAL;
1917 crop->c.top = b->top + b->height; 1853 cap->bounds = dev->crop_bounds;
1918 if (crop->c.height > b->top - crop->c.top + b->height) 1854 cap->defrect = dev->crop_defrect;
1919 crop->c.height = b->top - crop->c.top + b->height; 1855 cap->pixelaspect.numerator = 1;
1920 1856 cap->pixelaspect.denominator = 1;
1921 if (crop->c.left < b->left) 1857 if (dev->tvnorm->id & V4L2_STD_525_60) {
1922 crop->c.left = b->left; 1858 cap->pixelaspect.numerator = 11;
1923 if (crop->c.left > b->left + b->width) 1859 cap->pixelaspect.denominator = 10;
1924 crop->c.left = b->left + b->width; 1860 }
1925 if (crop->c.width > b->left - crop->c.left + b->width) 1861 if (dev->tvnorm->id & V4L2_STD_625_50) {
1926 crop->c.width = b->left - crop->c.left + b->width; 1862 cap->pixelaspect.numerator = 54;
1927 1863 cap->pixelaspect.denominator = 59;
1928 dev->crop_current = crop->c;
1929 return 0;
1930 } 1864 }
1865 return 0;
1866}
1931 1867
1932 /* --- tuner ioctls ------------------------------------------ */ 1868static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1933 case VIDIOC_G_TUNER: 1869{
1934 { 1870 struct saa7134_fh *fh = f;
1935 struct v4l2_tuner *t = arg; 1871 struct saa7134_dev *dev = fh->dev;
1936 int n;
1937 1872
1938 if (0 != t->index) 1873 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1939 return -EINVAL; 1874 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1940 memset(t,0,sizeof(*t)); 1875 return -EINVAL;
1941 for (n = 0; n < SAA7134_INPUT_MAX; n++) 1876 crop->c = dev->crop_current;
1942 if (card_in(dev,n).tv) 1877 return 0;
1943 break; 1878}
1944 if (NULL != card_in(dev,n).name) {
1945 strcpy(t->name, "Television");
1946 t->type = V4L2_TUNER_ANALOG_TV;
1947 t->capability = V4L2_TUNER_CAP_NORM |
1948 V4L2_TUNER_CAP_STEREO |
1949 V4L2_TUNER_CAP_LANG1 |
1950 V4L2_TUNER_CAP_LANG2;
1951 t->rangehigh = 0xffffffffUL;
1952 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1953 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1954 }
1955 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1956 t->signal = 0xffff;
1957 return 0;
1958 }
1959 case VIDIOC_S_TUNER:
1960 {
1961 struct v4l2_tuner *t = arg;
1962 int rx,mode;
1963 1879
1964 mode = dev->thread.mode; 1880static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
1965 if (UNSET == mode) { 1881{
1966 rx = saa7134_tvaudio_getstereo(dev); 1882 struct saa7134_fh *fh = f;
1967 mode = saa7134_tvaudio_rx2mode(t->rxsubchans); 1883 struct saa7134_dev *dev = fh->dev;
1968 } 1884 struct v4l2_rect *b = &dev->crop_bounds;
1969 if (mode != t->audmode) {
1970 dev->thread.mode = t->audmode;
1971 }
1972 return 0;
1973 }
1974 case VIDIOC_G_FREQUENCY:
1975 {
1976 struct v4l2_frequency *f = arg;
1977 1885
1978 memset(f,0,sizeof(*f)); 1886 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1979 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 1887 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1980 f->frequency = dev->ctl_freq; 1888 return -EINVAL;
1981 return 0; 1889 if (crop->c.height < 0)
1982 } 1890 return -EINVAL;
1983 case VIDIOC_S_FREQUENCY: 1891 if (crop->c.width < 0)
1984 { 1892 return -EINVAL;
1985 struct v4l2_frequency *f = arg;
1986 1893
1987 if (0 != f->tuner) 1894 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1988 return -EINVAL; 1895 return -EBUSY;
1989 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type) 1896 if (res_locked(fh->dev, RESOURCE_VIDEO))
1990 return -EINVAL; 1897 return -EBUSY;
1991 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type) 1898
1992 return -EINVAL; 1899 if (crop->c.top < b->top)
1993 mutex_lock(&dev->lock); 1900 crop->c.top = b->top;
1994 dev->ctl_freq = f->frequency; 1901 if (crop->c.top > b->top + b->height)
1902 crop->c.top = b->top + b->height;
1903 if (crop->c.height > b->top - crop->c.top + b->height)
1904 crop->c.height = b->top - crop->c.top + b->height;
1905
1906 if (crop->c.left < b->left)
1907 crop->c.left = b->left;
1908 if (crop->c.left > b->left + b->width)
1909 crop->c.left = b->left + b->width;
1910 if (crop->c.width > b->left - crop->c.left + b->width)
1911 crop->c.width = b->left - crop->c.left + b->width;
1912
1913 dev->crop_current = crop->c;
1914 return 0;
1915}
1995 1916
1996 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f); 1917static int saa7134_g_tuner(struct file *file, void *priv,
1918 struct v4l2_tuner *t)
1919{
1920 struct saa7134_fh *fh = priv;
1921 struct saa7134_dev *dev = fh->dev;
1922 int n;
1997 1923
1998 saa7134_tvaudio_do_scan(dev); 1924 if (0 != t->index)
1999 mutex_unlock(&dev->lock); 1925 return -EINVAL;
2000 return 0; 1926 memset(t, 0, sizeof(*t));
2001 } 1927 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1928 if (card_in(dev, n).tv)
1929 break;
1930 if (NULL != card_in(dev, n).name) {
1931 strcpy(t->name, "Television");
1932 t->type = V4L2_TUNER_ANALOG_TV;
1933 t->capability = V4L2_TUNER_CAP_NORM |
1934 V4L2_TUNER_CAP_STEREO |
1935 V4L2_TUNER_CAP_LANG1 |
1936 V4L2_TUNER_CAP_LANG2;
1937 t->rangehigh = 0xffffffffUL;
1938 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1939 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1940 }
1941 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1942 t->signal = 0xffff;
1943 return 0;
1944}
2002 1945
2003 /* --- control ioctls ---------------------------------------- */ 1946static int saa7134_s_tuner(struct file *file, void *priv,
2004 case VIDIOC_ENUMINPUT: 1947 struct v4l2_tuner *t)
2005 case VIDIOC_G_INPUT: 1948{
2006 case VIDIOC_S_INPUT: 1949 struct saa7134_fh *fh = priv;
2007 case VIDIOC_QUERYCTRL: 1950 struct saa7134_dev *dev = fh->dev;
2008 case VIDIOC_G_CTRL: 1951 int rx, mode, err;
2009 case VIDIOC_S_CTRL:
2010 return saa7134_common_ioctl(dev, cmd, arg);
2011 1952
2012 case VIDIOC_G_AUDIO: 1953 err = v4l2_prio_check(&dev->prio, &fh->prio);
2013 { 1954 if (0 != err)
2014 struct v4l2_audio *a = arg; 1955 return err;
2015 1956
2016 memset(a,0,sizeof(*a)); 1957 mode = dev->thread.mode;
2017 strcpy(a->name,"audio"); 1958 if (UNSET == mode) {
2018 return 0; 1959 rx = saa7134_tvaudio_getstereo(dev);
2019 } 1960 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
2020 case VIDIOC_S_AUDIO:
2021 return 0;
2022 case VIDIOC_G_PARM:
2023 {
2024 struct v4l2_captureparm *parm = arg;
2025 memset(parm,0,sizeof(*parm));
2026 return 0;
2027 } 1961 }
1962 if (mode != t->audmode)
1963 dev->thread.mode = t->audmode;
2028 1964
2029 case VIDIOC_G_PRIORITY: 1965 return 0;
2030 { 1966}
2031 enum v4l2_priority *p = arg;
2032 1967
2033 *p = v4l2_prio_max(&dev->prio); 1968static int saa7134_g_frequency(struct file *file, void *priv,
2034 return 0; 1969 struct v4l2_frequency *f)
2035 } 1970{
2036 case VIDIOC_S_PRIORITY: 1971 struct saa7134_fh *fh = priv;
2037 { 1972 struct saa7134_dev *dev = fh->dev;
2038 enum v4l2_priority *prio = arg;
2039 1973
2040 return v4l2_prio_change(&dev->prio, &fh->prio, *prio); 1974 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2041 } 1975 f->frequency = dev->ctl_freq;
2042 1976
2043 /* --- preview ioctls ---------------------------------------- */ 1977 return 0;
2044 case VIDIOC_ENUM_FMT: 1978}
2045 {
2046 struct v4l2_fmtdesc *f = arg;
2047 enum v4l2_buf_type type;
2048 unsigned int index;
2049
2050 index = f->index;
2051 type = f->type;
2052 switch (type) {
2053 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2054 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2055 if (saa7134_no_overlay > 0) {
2056 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2057 return -EINVAL;
2058 }
2059 if (index >= FORMATS)
2060 return -EINVAL;
2061 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
2062 formats[index].planar)
2063 return -EINVAL;
2064 memset(f,0,sizeof(*f));
2065 f->index = index;
2066 f->type = type;
2067 strlcpy(f->description,formats[index].name,sizeof(f->description));
2068 f->pixelformat = formats[index].fourcc;
2069 break;
2070 case V4L2_BUF_TYPE_VBI_CAPTURE:
2071 if (0 != index)
2072 return -EINVAL;
2073 memset(f,0,sizeof(*f));
2074 f->index = index;
2075 f->type = type;
2076 f->pixelformat = V4L2_PIX_FMT_GREY;
2077 strcpy(f->description,"vbi data");
2078 break;
2079 default:
2080 return -EINVAL;
2081 }
2082 return 0;
2083 }
2084 case VIDIOC_G_FBUF:
2085 {
2086 struct v4l2_framebuffer *fb = arg;
2087 1979
2088 *fb = dev->ovbuf; 1980static int saa7134_s_frequency(struct file *file, void *priv,
2089 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING; 1981 struct v4l2_frequency *f)
2090 return 0; 1982{
2091 } 1983 struct saa7134_fh *fh = priv;
2092 case VIDIOC_S_FBUF: 1984 struct saa7134_dev *dev = fh->dev;
2093 { 1985 int err;
2094 struct v4l2_framebuffer *fb = arg;
2095 struct saa7134_format *fmt;
2096 1986
2097 if(!capable(CAP_SYS_ADMIN) && 1987 err = v4l2_prio_check(&dev->prio, &fh->prio);
2098 !capable(CAP_SYS_RAWIO)) 1988 if (0 != err)
2099 return -EPERM; 1989 return err;
2100 1990
2101 /* check args */ 1991 if (0 != f->tuner)
2102 fmt = format_by_fourcc(fb->fmt.pixelformat); 1992 return -EINVAL;
2103 if (NULL == fmt) 1993 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2104 return -EINVAL; 1994 return -EINVAL;
1995 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1996 return -EINVAL;
1997 mutex_lock(&dev->lock);
1998 dev->ctl_freq = f->frequency;
2105 1999
2106 /* ok, accept it */ 2000 saa7134_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
2107 dev->ovbuf = *fb; 2001
2108 dev->ovfmt = fmt; 2002 saa7134_tvaudio_do_scan(dev);
2109 if (0 == dev->ovbuf.fmt.bytesperline) 2003 mutex_unlock(&dev->lock);
2110 dev->ovbuf.fmt.bytesperline = 2004 return 0;
2111 dev->ovbuf.fmt.width*fmt->depth/8; 2005}
2112 return 0; 2006
2007static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2008{
2009 strcpy(a->name, "audio");
2010 return 0;
2011}
2012
2013static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
2014{
2015 return 0;
2016}
2017
2018static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2019{
2020 struct saa7134_fh *fh = f;
2021 struct saa7134_dev *dev = fh->dev;
2022
2023 *p = v4l2_prio_max(&dev->prio);
2024 return 0;
2025}
2026
2027static int saa7134_s_priority(struct file *file, void *f,
2028 enum v4l2_priority prio)
2029{
2030 struct saa7134_fh *fh = f;
2031 struct saa7134_dev *dev = fh->dev;
2032
2033 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2034}
2035
2036static int saa7134_enum_fmt_cap(struct file *file, void *priv,
2037 struct v4l2_fmtdesc *f)
2038{
2039 if (f->index >= FORMATS)
2040 return -EINVAL;
2041
2042 strlcpy(f->description, formats[f->index].name,
2043 sizeof(f->description));
2044
2045 f->pixelformat = formats[f->index].fourcc;
2046
2047 return 0;
2048}
2049
2050static int saa7134_enum_fmt_overlay(struct file *file, void *priv,
2051 struct v4l2_fmtdesc *f)
2052{
2053 if (saa7134_no_overlay > 0) {
2054 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2055 return -EINVAL;
2113 } 2056 }
2114 case VIDIOC_OVERLAY:
2115 {
2116 int *on = arg;
2117 2057
2118 if (*on) { 2058 if ((f->index >= FORMATS) || formats[f->index].planar)
2119 if (saa7134_no_overlay > 0) { 2059 return -EINVAL;
2120 printk ("no_overlay\n");
2121 return -EINVAL;
2122 }
2123 2060
2124 if (!res_get(dev,fh,RESOURCE_OVERLAY)) 2061 strlcpy(f->description, formats[f->index].name,
2125 return -EBUSY; 2062 sizeof(f->description));
2126 spin_lock_irqsave(&dev->slock,flags); 2063
2127 start_preview(dev,fh); 2064 f->pixelformat = formats[f->index].fourcc;
2128 spin_unlock_irqrestore(&dev->slock,flags); 2065
2129 } 2066 return 0;
2130 if (!*on) { 2067}
2131 if (!res_check(fh, RESOURCE_OVERLAY)) 2068
2132 return -EINVAL; 2069static int saa7134_enum_fmt_vbi(struct file *file, void *priv,
2133 spin_lock_irqsave(&dev->slock,flags); 2070 struct v4l2_fmtdesc *f)
2134 stop_preview(dev,fh); 2071{
2135 spin_unlock_irqrestore(&dev->slock,flags); 2072 if (0 != f->index)
2136 res_free(dev,fh,RESOURCE_OVERLAY); 2073 return -EINVAL;
2074
2075 f->pixelformat = V4L2_PIX_FMT_GREY;
2076 strcpy(f->description, "vbi data");
2077
2078 return 0;
2079}
2080
2081static int saa7134_g_fbuf(struct file *file, void *f,
2082 struct v4l2_framebuffer *fb)
2083{
2084 struct saa7134_fh *fh = f;
2085 struct saa7134_dev *dev = fh->dev;
2086
2087 *fb = dev->ovbuf;
2088 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2089
2090 return 0;
2091}
2092
2093static int saa7134_s_fbuf(struct file *file, void *f,
2094 struct v4l2_framebuffer *fb)
2095{
2096 struct saa7134_fh *fh = f;
2097 struct saa7134_dev *dev = fh->dev;
2098 struct saa7134_format *fmt;
2099
2100 if (!capable(CAP_SYS_ADMIN) &&
2101 !capable(CAP_SYS_RAWIO))
2102 return -EPERM;
2103
2104 /* check args */
2105 fmt = format_by_fourcc(fb->fmt.pixelformat);
2106 if (NULL == fmt)
2107 return -EINVAL;
2108
2109 /* ok, accept it */
2110 dev->ovbuf = *fb;
2111 dev->ovfmt = fmt;
2112 if (0 == dev->ovbuf.fmt.bytesperline)
2113 dev->ovbuf.fmt.bytesperline =
2114 dev->ovbuf.fmt.width*fmt->depth/8;
2115 return 0;
2116}
2117
2118static int saa7134_overlay(struct file *file, void *f, unsigned int on)
2119{
2120 struct saa7134_fh *fh = f;
2121 struct saa7134_dev *dev = fh->dev;
2122 unsigned long flags;
2123
2124 if (on) {
2125 if (saa7134_no_overlay > 0) {
2126 dprintk("no_overlay\n");
2127 return -EINVAL;
2137 } 2128 }
2138 return 0;
2139 }
2140 2129
2141 /* --- capture ioctls ---------------------------------------- */ 2130 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2142 case VIDIOC_G_FMT: 2131 return -EBUSY;
2143 { 2132 spin_lock_irqsave(&dev->slock, flags);
2144 struct v4l2_format *f = arg; 2133 start_preview(dev, fh);
2145 return saa7134_g_fmt(dev,fh,f); 2134 spin_unlock_irqrestore(&dev->slock, flags);
2146 }
2147 case VIDIOC_S_FMT:
2148 {
2149 struct v4l2_format *f = arg;
2150 return saa7134_s_fmt(dev,fh,f);
2151 } 2135 }
2152 case VIDIOC_TRY_FMT: 2136 if (!on) {
2153 { 2137 if (!res_check(fh, RESOURCE_OVERLAY))
2154 struct v4l2_format *f = arg; 2138 return -EINVAL;
2155 return saa7134_try_fmt(dev,fh,f); 2139 spin_lock_irqsave(&dev->slock, flags);
2140 stop_preview(dev, fh);
2141 spin_unlock_irqrestore(&dev->slock, flags);
2142 res_free(dev, fh, RESOURCE_OVERLAY);
2156 } 2143 }
2144 return 0;
2145}
2146
2157#ifdef CONFIG_VIDEO_V4L1_COMPAT 2147#ifdef CONFIG_VIDEO_V4L1_COMPAT
2158 case VIDIOCGMBUF: 2148static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2159 return videobuf_cgmbuf(saa7134_queue(fh), arg, gbuffers); 2149{
2150 struct saa7134_fh *fh = file->private_data;
2151 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2152}
2160#endif 2153#endif
2161 case VIDIOC_REQBUFS:
2162 return videobuf_reqbufs(saa7134_queue(fh),arg);
2163 2154
2164 case VIDIOC_QUERYBUF: 2155static int saa7134_reqbufs(struct file *file, void *priv,
2165 return videobuf_querybuf(saa7134_queue(fh),arg); 2156 struct v4l2_requestbuffers *p)
2157{
2158 struct saa7134_fh *fh = priv;
2159 return videobuf_reqbufs(saa7134_queue(fh), p);
2160}
2166 2161
2167 case VIDIOC_QBUF: 2162static int saa7134_querybuf(struct file *file, void *priv,
2168 return videobuf_qbuf(saa7134_queue(fh),arg); 2163 struct v4l2_buffer *b)
2164{
2165 struct saa7134_fh *fh = priv;
2166 return videobuf_querybuf(saa7134_queue(fh), b);
2167}
2169 2168
2170 case VIDIOC_DQBUF: 2169static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2171 return videobuf_dqbuf(saa7134_queue(fh),arg, 2170{
2172 file->f_flags & O_NONBLOCK); 2171 struct saa7134_fh *fh = priv;
2172 return videobuf_qbuf(saa7134_queue(fh), b);
2173}
2173 2174
2174 case VIDIOC_STREAMON: 2175static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2175 { 2176{
2176 int res = saa7134_resource(fh); 2177 struct saa7134_fh *fh = priv;
2178 return videobuf_dqbuf(saa7134_queue(fh), b,
2179 file->f_flags & O_NONBLOCK);
2180}
2177 2181
2178 if (!res_get(dev,fh,res)) 2182static int saa7134_streamon(struct file *file, void *priv,
2179 return -EBUSY; 2183 enum v4l2_buf_type type)
2180 return videobuf_streamon(saa7134_queue(fh)); 2184{
2181 } 2185 struct saa7134_fh *fh = priv;
2182 case VIDIOC_STREAMOFF: 2186 struct saa7134_dev *dev = fh->dev;
2183 { 2187 int res = saa7134_resource(fh);
2184 int res = saa7134_resource(fh);
2185 2188
2186 err = videobuf_streamoff(saa7134_queue(fh)); 2189 if (!res_get(dev, fh, res))
2187 if (err < 0) 2190 return -EBUSY;
2188 return err;
2189 res_free(dev,fh,res);
2190 return 0;
2191 }
2192 2191
2193 default: 2192 return videobuf_streamon(saa7134_queue(fh));
2194 return v4l_compat_translate_ioctl(inode,file,cmd,arg, 2193}
2195 video_do_ioctl); 2194
2196 } 2195static int saa7134_streamoff(struct file *file, void *priv,
2196 enum v4l2_buf_type type)
2197{
2198 int err;
2199 struct saa7134_fh *fh = priv;
2200 struct saa7134_dev *dev = fh->dev;
2201 int res = saa7134_resource(fh);
2202
2203 err = videobuf_streamoff(saa7134_queue(fh));
2204 if (err < 0)
2205 return err;
2206 res_free(dev, fh, res);
2197 return 0; 2207 return 0;
2198} 2208}
2199 2209
2200static int video_ioctl(struct inode *inode, struct file *file, 2210static int saa7134_g_parm(struct file *file, void *fh,
2201 unsigned int cmd, unsigned long arg) 2211 struct v4l2_streamparm *parm)
2202{ 2212{
2203 return video_usercopy(inode, file, cmd, arg, video_do_ioctl); 2213 return 0;
2204} 2214}
2205 2215
2206static int radio_do_ioctl(struct inode *inode, struct file *file, 2216static int radio_querycap(struct file *file, void *priv,
2207 unsigned int cmd, void *arg) 2217 struct v4l2_capability *cap)
2208{ 2218{
2209 struct saa7134_fh *fh = file->private_data; 2219 struct saa7134_fh *fh = file->private_data;
2210 struct saa7134_dev *dev = fh->dev; 2220 struct saa7134_dev *dev = fh->dev;
2211 2221
2212 if (video_debug > 1) 2222 strcpy(cap->driver, "saa7134");
2213 v4l_print_ioctl(dev->name,cmd); 2223 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2214 switch (cmd) { 2224 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2215 case VIDIOC_QUERYCAP: 2225 cap->version = SAA7134_VERSION_CODE;
2216 { 2226 cap->capabilities = V4L2_CAP_TUNER;
2217 struct v4l2_capability *cap = arg; 2227 return 0;
2218 2228}
2219 memset(cap,0,sizeof(*cap));
2220 strcpy(cap->driver, "saa7134");
2221 strlcpy(cap->card, saa7134_boards[dev->board].name,
2222 sizeof(cap->card));
2223 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2224 cap->version = SAA7134_VERSION_CODE;
2225 cap->capabilities = V4L2_CAP_TUNER;
2226 return 0;
2227 }
2228 case VIDIOC_G_TUNER:
2229 {
2230 struct v4l2_tuner *t = arg;
2231 2229
2232 if (0 != t->index) 2230static int radio_g_tuner(struct file *file, void *priv,
2233 return -EINVAL; 2231 struct v4l2_tuner *t)
2232{
2233 struct saa7134_fh *fh = file->private_data;
2234 struct saa7134_dev *dev = fh->dev;
2234 2235
2235 memset(t,0,sizeof(*t)); 2236 if (0 != t->index)
2236 strcpy(t->name, "Radio"); 2237 return -EINVAL;
2237 t->type = V4L2_TUNER_RADIO;
2238 2238
2239 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t); 2239 memset(t, 0, sizeof(*t));
2240 if (dev->input->amux == TV) { 2240 strcpy(t->name, "Radio");
2241 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11); 2241 t->type = V4L2_TUNER_RADIO;
2242 t->rxsubchans = (saa_readb(0x529) & 0x08) ? 2242
2243 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; 2243 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2244 } 2244 if (dev->input->amux == TV) {
2245 return 0; 2245 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2246 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2247 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2246 } 2248 }
2247 case VIDIOC_S_TUNER: 2249 return 0;
2248 { 2250}
2249 struct v4l2_tuner *t = arg; 2251static int radio_s_tuner(struct file *file, void *priv,
2252 struct v4l2_tuner *t)
2253{
2254 struct saa7134_fh *fh = file->private_data;
2255 struct saa7134_dev *dev = fh->dev;
2250 2256
2251 if (0 != t->index) 2257 if (0 != t->index)
2252 return -EINVAL; 2258 return -EINVAL;
2253 2259
2254 saa7134_i2c_call_clients(dev,VIDIOC_S_TUNER,t); 2260 saa7134_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
2261 return 0;
2262}
2255 2263
2256 return 0; 2264static int radio_enum_input(struct file *file, void *priv,
2257 } 2265 struct v4l2_input *i)
2258 case VIDIOC_ENUMINPUT: 2266{
2259 { 2267 if (i->index != 0)
2260 struct v4l2_input *i = arg; 2268 return -EINVAL;
2261 2269
2262 if (i->index != 0) 2270 strcpy(i->name, "Radio");
2263 return -EINVAL; 2271 i->type = V4L2_INPUT_TYPE_TUNER;
2264 strcpy(i->name,"Radio");
2265 i->type = V4L2_INPUT_TYPE_TUNER;
2266 return 0;
2267 }
2268 case VIDIOC_G_INPUT:
2269 {
2270 int *i = arg;
2271 *i = 0;
2272 return 0;
2273 }
2274 case VIDIOC_G_AUDIO:
2275 {
2276 struct v4l2_audio *a = arg;
2277 2272
2278 memset(a,0,sizeof(*a)); 2273 return 0;
2279 strcpy(a->name,"Radio"); 2274}
2280 return 0;
2281 }
2282 case VIDIOC_G_STD:
2283 {
2284 v4l2_std_id *id = arg;
2285 *id = 0;
2286 return 0;
2287 }
2288 case VIDIOC_S_AUDIO:
2289 case VIDIOC_S_INPUT:
2290 case VIDIOC_S_STD:
2291 return 0;
2292 2275
2293 case VIDIOC_QUERYCTRL: 2276static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2294 { 2277{
2295 const struct v4l2_queryctrl *ctrl; 2278 *i = 0;
2296 struct v4l2_queryctrl *c = arg; 2279 return 0;
2280}
2297 2281
2298 if (c->id < V4L2_CID_BASE || 2282static int radio_g_audio(struct file *file, void *priv,
2299 c->id >= V4L2_CID_LASTP1) 2283 struct v4l2_audio *a)
2300 return -EINVAL; 2284{
2301 if (c->id == V4L2_CID_AUDIO_MUTE) { 2285 memset(a, 0, sizeof(*a));
2302 ctrl = ctrl_by_id(c->id); 2286 strcpy(a->name, "Radio");
2303 *c = *ctrl; 2287 return 0;
2304 } else 2288}
2305 *c = no_ctrl;
2306 return 0;
2307 }
2308 2289
2309 case VIDIOC_G_CTRL: 2290static int radio_s_audio(struct file *file, void *priv,
2310 case VIDIOC_S_CTRL: 2291 struct v4l2_audio *a)
2311 case VIDIOC_G_FREQUENCY: 2292{
2312 case VIDIOC_S_FREQUENCY: 2293 return 0;
2313 return video_do_ioctl(inode,file,cmd,arg); 2294}
2314 2295
2315 default: 2296static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2316 return v4l_compat_translate_ioctl(inode,file,cmd,arg, 2297{
2317 radio_do_ioctl);
2318 }
2319 return 0; 2298 return 0;
2320} 2299}
2321 2300
2322static int radio_ioctl(struct inode *inode, struct file *file, 2301static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2323 unsigned int cmd, unsigned long arg)
2324{ 2302{
2325 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl); 2303 return 0;
2304}
2305
2306static int radio_queryctrl(struct file *file, void *priv,
2307 struct v4l2_queryctrl *c)
2308{
2309 const struct v4l2_queryctrl *ctrl;
2310
2311 if (c->id < V4L2_CID_BASE ||
2312 c->id >= V4L2_CID_LASTP1)
2313 return -EINVAL;
2314 if (c->id == V4L2_CID_AUDIO_MUTE) {
2315 ctrl = ctrl_by_id(c->id);
2316 *c = *ctrl;
2317 } else
2318 *c = no_ctrl;
2319 return 0;
2326} 2320}
2327 2321
2328static const struct file_operations video_fops = 2322static const struct file_operations video_fops =
@@ -2333,7 +2327,7 @@ static const struct file_operations video_fops =
2333 .read = video_read, 2327 .read = video_read,
2334 .poll = video_poll, 2328 .poll = video_poll,
2335 .mmap = video_mmap, 2329 .mmap = video_mmap,
2336 .ioctl = video_ioctl, 2330 .ioctl = video_ioctl2,
2337 .compat_ioctl = v4l_compat_ioctl32, 2331 .compat_ioctl = v4l_compat_ioctl32,
2338 .llseek = no_llseek, 2332 .llseek = no_llseek,
2339}; 2333};
@@ -2343,7 +2337,7 @@ static const struct file_operations radio_fops =
2343 .owner = THIS_MODULE, 2337 .owner = THIS_MODULE,
2344 .open = video_open, 2338 .open = video_open,
2345 .release = video_release, 2339 .release = video_release,
2346 .ioctl = radio_ioctl, 2340 .ioctl = video_ioctl2,
2347 .compat_ioctl = v4l_compat_ioctl32, 2341 .compat_ioctl = v4l_compat_ioctl32,
2348 .llseek = no_llseek, 2342 .llseek = no_llseek,
2349}; 2343};
@@ -2353,27 +2347,79 @@ static const struct file_operations radio_fops =
2353 2347
2354struct video_device saa7134_video_template = 2348struct video_device saa7134_video_template =
2355{ 2349{
2356 .name = "saa7134-video", 2350 .name = "saa7134-video",
2357 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER| 2351 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER |
2358 VID_TYPE_CLIPPING|VID_TYPE_SCALES, 2352 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2359 .fops = &video_fops, 2353 .fops = &video_fops,
2360 .minor = -1, 2354 .minor = -1,
2361}; 2355 .vidioc_querycap = saa7134_querycap,
2362 2356 .vidioc_enum_fmt_cap = saa7134_enum_fmt_cap,
2363struct video_device saa7134_vbi_template = 2357 .vidioc_g_fmt_cap = saa7134_g_fmt_cap,
2364{ 2358 .vidioc_try_fmt_cap = saa7134_try_fmt_cap,
2365 .name = "saa7134-vbi", 2359 .vidioc_s_fmt_cap = saa7134_s_fmt_cap,
2366 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT, 2360 .vidioc_enum_fmt_overlay = saa7134_enum_fmt_overlay,
2367 .fops = &video_fops, 2361 .vidioc_g_fmt_overlay = saa7134_g_fmt_overlay,
2368 .minor = -1, 2362 .vidioc_try_fmt_overlay = saa7134_try_fmt_overlay,
2363 .vidioc_s_fmt_overlay = saa7134_s_fmt_overlay,
2364 .vidioc_enum_fmt_vbi = saa7134_enum_fmt_vbi,
2365 .vidioc_g_fmt_vbi = saa7134_try_get_set_fmt_vbi,
2366 .vidioc_try_fmt_vbi = saa7134_try_get_set_fmt_vbi,
2367 .vidioc_s_fmt_vbi = saa7134_try_get_set_fmt_vbi,
2368 .vidioc_g_audio = saa7134_g_audio,
2369 .vidioc_s_audio = saa7134_s_audio,
2370 .vidioc_cropcap = saa7134_cropcap,
2371 .vidioc_reqbufs = saa7134_reqbufs,
2372 .vidioc_querybuf = saa7134_querybuf,
2373 .vidioc_qbuf = saa7134_qbuf,
2374 .vidioc_dqbuf = saa7134_dqbuf,
2375 .vidioc_s_std = saa7134_s_std,
2376 .vidioc_enum_input = saa7134_enum_input,
2377 .vidioc_g_input = saa7134_g_input,
2378 .vidioc_s_input = saa7134_s_input,
2379 .vidioc_queryctrl = saa7134_queryctrl,
2380 .vidioc_g_ctrl = saa7134_g_ctrl,
2381 .vidioc_s_ctrl = saa7134_s_ctrl,
2382 .vidioc_streamon = saa7134_streamon,
2383 .vidioc_streamoff = saa7134_streamoff,
2384 .vidioc_g_tuner = saa7134_g_tuner,
2385 .vidioc_s_tuner = saa7134_s_tuner,
2386#ifdef CONFIG_VIDEO_V4L1_COMPAT
2387 .vidiocgmbuf = vidiocgmbuf,
2388#endif
2389 .vidioc_g_crop = saa7134_g_crop,
2390 .vidioc_s_crop = saa7134_s_crop,
2391 .vidioc_g_fbuf = saa7134_g_fbuf,
2392 .vidioc_s_fbuf = saa7134_s_fbuf,
2393 .vidioc_overlay = saa7134_overlay,
2394 .vidioc_g_priority = saa7134_g_priority,
2395 .vidioc_s_priority = saa7134_s_priority,
2396 .vidioc_g_parm = saa7134_g_parm,
2397 .vidioc_g_frequency = saa7134_g_frequency,
2398 .vidioc_s_frequency = saa7134_s_frequency,
2399 .tvnorms = SAA7134_NORMS,
2400 .current_norm = V4L2_STD_PAL,
2369}; 2401};
2370 2402
2371struct video_device saa7134_radio_template = 2403struct video_device saa7134_radio_template =
2372{ 2404{
2373 .name = "saa7134-radio", 2405 .name = "saa7134-radio",
2374 .type = VID_TYPE_TUNER, 2406 .type = VID_TYPE_TUNER,
2375 .fops = &radio_fops, 2407 .fops = &radio_fops,
2376 .minor = -1, 2408 .minor = -1,
2409 .vidioc_querycap = radio_querycap,
2410 .vidioc_g_tuner = radio_g_tuner,
2411 .vidioc_enum_input = radio_enum_input,
2412 .vidioc_g_audio = radio_g_audio,
2413 .vidioc_s_tuner = radio_s_tuner,
2414 .vidioc_s_audio = radio_s_audio,
2415 .vidioc_s_input = radio_s_input,
2416 .vidioc_s_std = radio_s_std,
2417 .vidioc_queryctrl = radio_queryctrl,
2418 .vidioc_g_input = radio_g_input,
2419 .vidioc_g_ctrl = saa7134_g_ctrl,
2420 .vidioc_s_ctrl = saa7134_s_ctrl,
2421 .vidioc_g_frequency = saa7134_g_frequency,
2422 .vidioc_s_frequency = saa7134_s_frequency,
2377}; 2423};
2378 2424
2379int saa7134_video_init1(struct saa7134_dev *dev) 2425int saa7134_video_init1(struct saa7134_dev *dev)
@@ -2511,7 +2557,7 @@ void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2511 goto done; 2557 goto done;
2512 } 2558 }
2513 dev->video_q.curr->vb.field_count = dev->video_fieldcount; 2559 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2514 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE); 2560 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
2515 } 2561 }
2516 saa7134_buffer_next(dev,&dev->video_q); 2562 saa7134_buffer_next(dev,&dev->video_q);
2517 2563
diff --git a/drivers/media/video/saa7134/saa7134.h b/drivers/media/video/saa7134/saa7134.h
index 66a390c321a7..ce450304fb53 100644
--- a/drivers/media/video/saa7134/saa7134.h
+++ b/drivers/media/video/saa7134/saa7134.h
@@ -240,6 +240,19 @@ struct saa7134_format {
240#define SAA7134_BOARD_SABRENT_TV_PCB05 115 240#define SAA7134_BOARD_SABRENT_TV_PCB05 115
241#define SAA7134_BOARD_10MOONSTVMASTER3 116 241#define SAA7134_BOARD_10MOONSTVMASTER3 116
242#define SAA7134_BOARD_AVERMEDIA_SUPER_007 117 242#define SAA7134_BOARD_AVERMEDIA_SUPER_007 117
243#define SAA7134_BOARD_BEHOLD_401 118
244#define SAA7134_BOARD_BEHOLD_403 119
245#define SAA7134_BOARD_BEHOLD_403FM 120
246#define SAA7134_BOARD_BEHOLD_405 121
247#define SAA7134_BOARD_BEHOLD_405FM 122
248#define SAA7134_BOARD_BEHOLD_407 123
249#define SAA7134_BOARD_BEHOLD_407FM 124
250#define SAA7134_BOARD_BEHOLD_409 125
251#define SAA7134_BOARD_BEHOLD_505FM 126
252#define SAA7134_BOARD_BEHOLD_507_9FM 127
253#define SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM 128
254#define SAA7134_BOARD_BEHOLD_607_9FM 129
255#define SAA7134_BOARD_BEHOLD_M6 130
243 256
244#define SAA7134_MAXBOARDS 8 257#define SAA7134_MAXBOARDS 8
245#define SAA7134_INPUT_MAX 8 258#define SAA7134_INPUT_MAX 8
@@ -481,7 +494,7 @@ struct saa7134_dev {
481 /* i2c i/o */ 494 /* i2c i/o */
482 struct i2c_adapter i2c_adap; 495 struct i2c_adapter i2c_adap;
483 struct i2c_client i2c_client; 496 struct i2c_client i2c_client;
484 unsigned char eedata[128]; 497 unsigned char eedata[256];
485 498
486 /* video overlay */ 499 /* video overlay */
487 struct v4l2_framebuffer ovbuf; 500 struct v4l2_framebuffer ovbuf;
@@ -566,6 +579,12 @@ struct saa7134_dev {
566 579
567#define saa_wait(us) { udelay(us); } 580#define saa_wait(us) { udelay(us); }
568 581
582#define SAA7134_NORMS (\
583 V4L2_STD_PAL | V4L2_STD_PAL_N | \
584 V4L2_STD_PAL_Nc | V4L2_STD_SECAM | \
585 V4L2_STD_NTSC | V4L2_STD_PAL_M | \
586 V4L2_STD_PAL_60)
587
569/* ----------------------------------------------------------- */ 588/* ----------------------------------------------------------- */
570/* saa7134-core.c */ 589/* saa7134-core.c */
571 590
@@ -596,9 +615,6 @@ void saa7134_buffer_next(struct saa7134_dev *dev, struct saa7134_dmaqueue *q);
596void saa7134_buffer_timeout(unsigned long data); 615void saa7134_buffer_timeout(unsigned long data);
597void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf); 616void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf);
598 617
599int saa7134_buffer_requeue(struct saa7134_dev *dev,
600 struct saa7134_dmaqueue *q);
601
602int saa7134_set_dmabits(struct saa7134_dev *dev); 618int saa7134_set_dmabits(struct saa7134_dev *dev);
603 619
604extern int (*saa7134_dmasound_init)(struct saa7134_dev *dev); 620extern int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
@@ -628,16 +644,17 @@ void saa7134_i2c_call_clients(struct saa7134_dev *dev,
628/* ----------------------------------------------------------- */ 644/* ----------------------------------------------------------- */
629/* saa7134-video.c */ 645/* saa7134-video.c */
630 646
647extern unsigned int video_debug;
631extern struct video_device saa7134_video_template; 648extern struct video_device saa7134_video_template;
632extern struct video_device saa7134_radio_template; 649extern struct video_device saa7134_radio_template;
633 650
634void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm); 651int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c);
652int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c);
653int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c);
654
635int saa7134_videoport_init(struct saa7134_dev *dev); 655int saa7134_videoport_init(struct saa7134_dev *dev);
636void saa7134_set_tvnorm_hw(struct saa7134_dev *dev); 656void saa7134_set_tvnorm_hw(struct saa7134_dev *dev);
637 657
638int saa7134_common_ioctl(struct saa7134_dev *dev,
639 unsigned int cmd, void *arg);
640
641int saa7134_video_init1(struct saa7134_dev *dev); 658int saa7134_video_init1(struct saa7134_dev *dev);
642int saa7134_video_init2(struct saa7134_dev *dev); 659int saa7134_video_init2(struct saa7134_dev *dev);
643void saa7134_irq_video_signalchange(struct saa7134_dev *dev); 660void saa7134_irq_video_signalchange(struct saa7134_dev *dev);
@@ -682,6 +699,7 @@ void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
682void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level); 699void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level);
683int saa7134_tvaudio_getstereo(struct saa7134_dev *dev); 700int saa7134_tvaudio_getstereo(struct saa7134_dev *dev);
684 701
702void saa7134_tvaudio_init(struct saa7134_dev *dev);
685int saa7134_tvaudio_init2(struct saa7134_dev *dev); 703int saa7134_tvaudio_init2(struct saa7134_dev *dev);
686int saa7134_tvaudio_fini(struct saa7134_dev *dev); 704int saa7134_tvaudio_fini(struct saa7134_dev *dev);
687int saa7134_tvaudio_do_scan(struct saa7134_dev *dev); 705int saa7134_tvaudio_do_scan(struct saa7134_dev *dev);
diff --git a/drivers/media/video/sn9c102/Makefile b/drivers/media/video/sn9c102/Makefile
index a56d16f69c71..7ecd5a90c7c9 100644
--- a/drivers/media/video/sn9c102/Makefile
+++ b/drivers/media/video/sn9c102/Makefile
@@ -3,6 +3,7 @@ sn9c102-objs := sn9c102_core.o \
3 sn9c102_hv7131r.o \ 3 sn9c102_hv7131r.o \
4 sn9c102_mi0343.o \ 4 sn9c102_mi0343.o \
5 sn9c102_mi0360.o \ 5 sn9c102_mi0360.o \
6 sn9c102_mt9v111.o \
6 sn9c102_ov7630.o \ 7 sn9c102_ov7630.o \
7 sn9c102_ov7660.o \ 8 sn9c102_ov7660.o \
8 sn9c102_pas106b.o \ 9 sn9c102_pas106b.o \
diff --git a/drivers/media/video/sn9c102/sn9c102_core.c b/drivers/media/video/sn9c102/sn9c102_core.c
index 511847912c48..c40ba3adab21 100644
--- a/drivers/media/video/sn9c102/sn9c102_core.c
+++ b/drivers/media/video/sn9c102/sn9c102_core.c
@@ -47,7 +47,7 @@
47#define SN9C102_MODULE_AUTHOR "(C) 2004-2007 Luca Risolia" 47#define SN9C102_MODULE_AUTHOR "(C) 2004-2007 Luca Risolia"
48#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>" 48#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
49#define SN9C102_MODULE_LICENSE "GPL" 49#define SN9C102_MODULE_LICENSE "GPL"
50#define SN9C102_MODULE_VERSION "1:1.47" 50#define SN9C102_MODULE_VERSION "1:1.47pre49"
51#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 47) 51#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 47)
52 52
53/*****************************************************************************/ 53/*****************************************************************************/
@@ -3322,7 +3322,6 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3322 cam->v4ldev->fops = &sn9c102_fops; 3322 cam->v4ldev->fops = &sn9c102_fops;
3323 cam->v4ldev->minor = video_nr[dev_nr]; 3323 cam->v4ldev->minor = video_nr[dev_nr];
3324 cam->v4ldev->release = video_device_release; 3324 cam->v4ldev->release = video_device_release;
3325 video_set_drvdata(cam->v4ldev, cam);
3326 3325
3327 init_completion(&cam->probe); 3326 init_completion(&cam->probe);
3328 3327
@@ -3340,6 +3339,7 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3340 3339
3341 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor); 3340 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
3342 3341
3342 video_set_drvdata(cam->v4ldev, cam);
3343 cam->module_param.force_munmap = force_munmap[dev_nr]; 3343 cam->module_param.force_munmap = force_munmap[dev_nr];
3344 cam->module_param.frame_timeout = frame_timeout[dev_nr]; 3344 cam->module_param.frame_timeout = frame_timeout[dev_nr];
3345 3345
diff --git a/drivers/media/video/sn9c102/sn9c102_devtable.h b/drivers/media/video/sn9c102/sn9c102_devtable.h
index 916054faf9be..35223e0d7e49 100644
--- a/drivers/media/video/sn9c102/sn9c102_devtable.h
+++ b/drivers/media/video/sn9c102/sn9c102_devtable.h
@@ -126,6 +126,7 @@ extern int sn9c102_probe_hv7131d(struct sn9c102_device* cam);
126extern int sn9c102_probe_hv7131r(struct sn9c102_device* cam); 126extern int sn9c102_probe_hv7131r(struct sn9c102_device* cam);
127extern int sn9c102_probe_mi0343(struct sn9c102_device* cam); 127extern int sn9c102_probe_mi0343(struct sn9c102_device* cam);
128extern int sn9c102_probe_mi0360(struct sn9c102_device* cam); 128extern int sn9c102_probe_mi0360(struct sn9c102_device* cam);
129extern int sn9c102_probe_mt9v111(struct sn9c102_device *cam);
129extern int sn9c102_probe_ov7630(struct sn9c102_device* cam); 130extern int sn9c102_probe_ov7630(struct sn9c102_device* cam);
130extern int sn9c102_probe_ov7660(struct sn9c102_device* cam); 131extern int sn9c102_probe_ov7660(struct sn9c102_device* cam);
131extern int sn9c102_probe_pas106b(struct sn9c102_device* cam); 132extern int sn9c102_probe_pas106b(struct sn9c102_device* cam);
@@ -144,6 +145,7 @@ static int (*sn9c102_sensor_table[])(struct sn9c102_device*) = {
144 &sn9c102_probe_hv7131r, /* strong detection based on SENSOR ids */ 145 &sn9c102_probe_hv7131r, /* strong detection based on SENSOR ids */
145 &sn9c102_probe_mi0343, /* strong detection based on SENSOR ids */ 146 &sn9c102_probe_mi0343, /* strong detection based on SENSOR ids */
146 &sn9c102_probe_mi0360, /* strong detection based on SENSOR ids */ 147 &sn9c102_probe_mi0360, /* strong detection based on SENSOR ids */
148 &sn9c102_probe_mt9v111, /* strong detection based on SENSOR ids */
147 &sn9c102_probe_pas106b, /* strong detection based on SENSOR ids */ 149 &sn9c102_probe_pas106b, /* strong detection based on SENSOR ids */
148 &sn9c102_probe_pas202bcb, /* strong detection based on SENSOR ids */ 150 &sn9c102_probe_pas202bcb, /* strong detection based on SENSOR ids */
149 &sn9c102_probe_ov7630, /* strong detection based on SENSOR ids */ 151 &sn9c102_probe_ov7630, /* strong detection based on SENSOR ids */
diff --git a/drivers/media/video/sn9c102/sn9c102_mt9v111.c b/drivers/media/video/sn9c102/sn9c102_mt9v111.c
new file mode 100644
index 000000000000..3b98ac3bbc38
--- /dev/null
+++ b/drivers/media/video/sn9c102/sn9c102_mt9v111.c
@@ -0,0 +1,259 @@
1/***************************************************************************
2 * Plug-in for MT9V111 image sensor connected to the SN9C1xx PC Camera *
3 * Controllers *
4 * *
5 * Copyright (C) 2007 by Luca Risolia <luca.risolia@studio.unibo.it> *
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 "sn9c102_sensor.h"
23
24
25static int mt9v111_init(struct sn9c102_device *cam)
26{
27 struct sn9c102_sensor *s = sn9c102_get_sensor(cam);
28 int err = 0;
29
30 err = sn9c102_write_const_regs(cam, {0x44, 0x01}, {0x40, 0x02},
31 {0x00, 0x03}, {0x1a, 0x04},
32 {0x1f, 0x05}, {0x20, 0x06},
33 {0x1f, 0x07}, {0x81, 0x08},
34 {0x5c, 0x09}, {0x00, 0x0a},
35 {0x00, 0x0b}, {0x00, 0x0c},
36 {0x00, 0x0d}, {0x00, 0x0e},
37 {0x00, 0x0f}, {0x03, 0x10},
38 {0x00, 0x11}, {0x00, 0x12},
39 {0x02, 0x13}, {0x14, 0x14},
40 {0x28, 0x15}, {0x1e, 0x16},
41 {0xe2, 0x17}, {0x06, 0x18},
42 {0x00, 0x19}, {0x00, 0x1a},
43 {0x00, 0x1b}, {0x08, 0x20},
44 {0x39, 0x21}, {0x51, 0x22},
45 {0x63, 0x23}, {0x73, 0x24},
46 {0x82, 0x25}, {0x8f, 0x26},
47 {0x9b, 0x27}, {0xa7, 0x28},
48 {0xb1, 0x29}, {0xbc, 0x2a},
49 {0xc6, 0x2b}, {0xcf, 0x2c},
50 {0xd8, 0x2d}, {0xe1, 0x2e},
51 {0xea, 0x2f}, {0xf2, 0x30},
52 {0x13, 0x84}, {0x00, 0x85},
53 {0x25, 0x86}, {0x00, 0x87},
54 {0x07, 0x88}, {0x00, 0x89},
55 {0xee, 0x8a}, {0x0f, 0x8b},
56 {0xe5, 0x8c}, {0x0f, 0x8d},
57 {0x2e, 0x8e}, {0x00, 0x8f},
58 {0x30, 0x90}, {0x00, 0x91},
59 {0xd4, 0x92}, {0x0f, 0x93},
60 {0xfc, 0x94}, {0x0f, 0x95},
61 {0x14, 0x96}, {0x00, 0x97},
62 {0x00, 0x98}, {0x60, 0x99},
63 {0x07, 0x9a}, {0x40, 0x9b},
64 {0x20, 0x9c}, {0x00, 0x9d},
65 {0x00, 0x9e}, {0x00, 0x9f},
66 {0x2d, 0xc0}, {0x2d, 0xc1},
67 {0x3a, 0xc2}, {0x05, 0xc3},
68 {0x04, 0xc4}, {0x3f, 0xc5},
69 {0x00, 0xc6}, {0x00, 0xc7},
70 {0x50, 0xc8}, {0x3c, 0xc9},
71 {0x28, 0xca}, {0xd8, 0xcb},
72 {0x14, 0xcc}, {0xec, 0xcd},
73 {0x32, 0xce}, {0xdd, 0xcf},
74 {0x2d, 0xd0}, {0xdd, 0xd1},
75 {0x6a, 0xd2}, {0x50, 0xd3},
76 {0x60, 0xd4}, {0x00, 0xd5},
77 {0x00, 0xd6});
78
79 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x01,
80 0x00, 0x01, 0, 0);
81 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
82 0x00, 0x01, 0, 0);
83 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
84 0x00, 0x00, 0, 0);
85 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x08,
86 0x04, 0x80, 0, 0);
87 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x01,
88 0x00, 0x04, 0, 0);
89 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x08,
90 0x00, 0x08, 0, 0);
91 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x02,
92 0x00, 0x16, 0, 0);
93 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03,
94 0x01, 0xe7, 0, 0);
95 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04,
96 0x02, 0x87, 0, 0);
97 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06,
98 0x00, 0x40, 0, 0);
99 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05,
100 0x00, 0x09, 0, 0);
101 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x07,
102 0x30, 0x02, 0, 0);
103 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0c,
104 0x00, 0x00, 0, 0);
105 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x12,
106 0x00, 0xb0, 0, 0);
107 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x13,
108 0x00, 0x7c, 0, 0);
109 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x1e,
110 0x00, 0x00, 0, 0);
111 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x20,
112 0x00, 0x00, 0, 0);
113 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x20,
114 0x00, 0x00, 0, 0);
115 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x01,
116 0x00, 0x04, 0, 0);
117
118 return err;
119}
120
121static int mt9v111_get_ctrl(struct sn9c102_device *cam,
122 struct v4l2_control *ctrl)
123{
124 struct sn9c102_sensor *s = sn9c102_get_sensor(cam);
125 u8 data[2];
126 int err = 0;
127
128 switch (ctrl->id) {
129 case V4L2_CID_VFLIP:
130 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
131 data) < 0)
132 return -EIO;
133 ctrl->value = data[1] & 0x80 ? 1 : 0;
134 return 0;
135 default:
136 return -EINVAL;
137 }
138
139 return err ? -EIO : 0;
140}
141
142static int mt9v111_set_ctrl(struct sn9c102_device *cam,
143 const struct v4l2_control *ctrl)
144{
145 struct sn9c102_sensor *s = sn9c102_get_sensor(cam);
146 int err = 0;
147
148 switch (ctrl->id) {
149 case V4L2_CID_VFLIP:
150 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
151 0x20,
152 ctrl->value ? 0x80 : 0x00,
153 ctrl->value ? 0x80 : 0x00, 0,
154 0);
155 break;
156 default:
157 return -EINVAL;
158 }
159
160 return err ? -EIO : 0;
161}
162
163static int mt9v111_set_crop(struct sn9c102_device *cam,
164 const struct v4l2_rect *rect)
165{
166 struct sn9c102_sensor *s = sn9c102_get_sensor(cam);
167 int err = 0;
168 u8 v_start = (u8) (rect->top - s->cropcap.bounds.top) + 2;
169
170 err += sn9c102_write_reg(cam, v_start, 0x13);
171
172 return err;
173}
174
175static int mt9v111_set_pix_format(struct sn9c102_device *cam,
176 const struct v4l2_pix_format *pix)
177{
178 int err = 0;
179
180 if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) {
181 err += sn9c102_write_reg(cam, 0xb4, 0x17);
182 } else {
183 err += sn9c102_write_reg(cam, 0xe2, 0x17);
184 }
185
186 return err;
187}
188
189
190static const struct sn9c102_sensor mt9v111 = {
191 .name = "MT9V111",
192 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
193 .supported_bridge = BRIDGE_SN9C105 | BRIDGE_SN9C120,
194 .frequency = SN9C102_I2C_100KHZ,
195 .interface = SN9C102_I2C_2WIRES,
196 .i2c_slave_id = 0x5c,
197 .init = &mt9v111_init,
198 .qctrl = {
199 {
200 .id = V4L2_CID_VFLIP,
201 .type = V4L2_CTRL_TYPE_BOOLEAN,
202 .name = "vertical mirror",
203 .minimum = 0,
204 .maximum = 1,
205 .step = 1,
206 .default_value = 0,
207 .flags = 0,
208 },
209 },
210 .get_ctrl = &mt9v111_get_ctrl,
211 .set_ctrl = &mt9v111_set_ctrl,
212 .cropcap = {
213 .bounds = {
214 .left = 0,
215 .top = 0,
216 .width = 640,
217 .height = 480,
218 },
219 .defrect = {
220 .left = 0,
221 .top = 0,
222 .width = 640,
223 .height = 480,
224 },
225 },
226 .set_crop = &mt9v111_set_crop,
227 .pix_format = {
228 .width = 640,
229 .height = 480,
230 .pixelformat = V4L2_PIX_FMT_SBGGR8,
231 .priv = 8,
232 },
233 .set_pix_format = &mt9v111_set_pix_format
234};
235
236
237int sn9c102_probe_mt9v111(struct sn9c102_device *cam)
238{
239 u8 data[2];
240 int err = 0;
241
242 err += sn9c102_write_const_regs(cam, {0x01, 0xf1}, {0x00, 0xf1},
243 {0x29, 0x01}, {0x42, 0x17},
244 {0x62, 0x17}, {0x08, 0x01});
245 err += sn9c102_i2c_try_raw_write(cam, &mt9v111, 4,
246 mt9v111.i2c_slave_id, 0x01, 0x00,
247 0x04, 0, 0);
248 if (err || sn9c102_i2c_try_raw_read(cam, &mt9v111,
249 mt9v111.i2c_slave_id, 0x36, 2,
250 data) < 0)
251 return -EIO;
252
253 if (data[0] != 0x82 || data[1] != 0x3a)
254 return -ENODEV;
255
256 sn9c102_attach_sensor(cam, &mt9v111);
257
258 return 0;
259}
diff --git a/drivers/media/video/stk-sensor.c b/drivers/media/video/stk-sensor.c
new file mode 100644
index 000000000000..4a9a0b62efa3
--- /dev/null
+++ b/drivers/media/video/stk-sensor.c
@@ -0,0 +1,578 @@
1/* stk-sensor.c: Driver for ov96xx sensor (used in some Syntek webcams)
2 *
3 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
4 *
5 * Some parts derived from ov7670.c:
6 * Copyright 2006 One Laptop Per Child Association, Inc. Written
7 * by Jonathan Corbet with substantial inspiration from Mark
8 * McClelland's ovcamchip code.
9 *
10 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
11 *
12 * This file may be distributed under the terms of the GNU General
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/* Controlling the sensor via the STK1125 vendor specific control interface:
29 * The camera uses an OmniVision sensor and the stk1125 provides an
30 * SCCB(i2c)-USB bridge which let us program the sensor.
31 * In my case the sensor id is 0x9652, it can be read from sensor's register
32 * 0x0A and 0x0B as follows:
33 * - read register #R:
34 * output #R to index 0x0208
35 * output 0x0070 to index 0x0200
36 * input 1 byte from index 0x0201 (some kind of status register)
37 * until its value is 0x01
38 * input 1 byte from index 0x0209. This is the value of #R
39 * - write value V to register #R
40 * output #R to index 0x0204
41 * output V to index 0x0205
42 * output 0x0005 to index 0x0200
43 * input 1 byte from index 0x0201 until its value becomes 0x04
44 */
45
46/* It seems the i2c bus is controlled with these registers */
47
48#include "stk-webcam.h"
49
50#define STK_IIC_BASE (0x0200)
51# define STK_IIC_OP (STK_IIC_BASE)
52# define STK_IIC_OP_TX (0x05)
53# define STK_IIC_OP_RX (0x70)
54# define STK_IIC_STAT (STK_IIC_BASE+1)
55# define STK_IIC_STAT_TX_OK (0x04)
56# define STK_IIC_STAT_RX_OK (0x01)
57/* I don't know what does this register.
58 * when it is 0x00 or 0x01, we cannot talk to the sensor,
59 * other values work */
60# define STK_IIC_ENABLE (STK_IIC_BASE+2)
61# define STK_IIC_ENABLE_NO (0x00)
62/* This is what the driver writes in windows */
63# define STK_IIC_ENABLE_YES (0x1e)
64/*
65 * Address of the slave. Seems like the binary driver look for the
66 * sensor in multiple places, attempting a reset sequence.
67 * We only know about the ov9650
68 */
69# define STK_IIC_ADDR (STK_IIC_BASE+3)
70# define STK_IIC_TX_INDEX (STK_IIC_BASE+4)
71# define STK_IIC_TX_VALUE (STK_IIC_BASE+5)
72# define STK_IIC_RX_INDEX (STK_IIC_BASE+8)
73# define STK_IIC_RX_VALUE (STK_IIC_BASE+9)
74
75#define MAX_RETRIES (50)
76
77#define SENSOR_ADDRESS (0x60)
78
79/* From ov7670.c (These registers aren't fully accurate) */
80
81/* Registers */
82#define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
83#define REG_BLUE 0x01 /* blue gain */
84#define REG_RED 0x02 /* red gain */
85#define REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
86#define REG_COM1 0x04 /* Control 1 */
87#define COM1_CCIR656 0x40 /* CCIR656 enable */
88#define COM1_QFMT 0x20 /* QVGA/QCIF format */
89#define COM1_SKIP_0 0x00 /* Do not skip any row */
90#define COM1_SKIP_2 0x04 /* Skip 2 rows of 4 */
91#define COM1_SKIP_3 0x08 /* Skip 3 rows of 4 */
92#define REG_BAVE 0x05 /* U/B Average level */
93#define REG_GbAVE 0x06 /* Y/Gb Average level */
94#define REG_AECHH 0x07 /* AEC MS 5 bits */
95#define REG_RAVE 0x08 /* V/R Average level */
96#define REG_COM2 0x09 /* Control 2 */
97#define COM2_SSLEEP 0x10 /* Soft sleep mode */
98#define REG_PID 0x0a /* Product ID MSB */
99#define REG_VER 0x0b /* Product ID LSB */
100#define REG_COM3 0x0c /* Control 3 */
101#define COM3_SWAP 0x40 /* Byte swap */
102#define COM3_SCALEEN 0x08 /* Enable scaling */
103#define COM3_DCWEN 0x04 /* Enable downsamp/crop/window */
104#define REG_COM4 0x0d /* Control 4 */
105#define REG_COM5 0x0e /* All "reserved" */
106#define REG_COM6 0x0f /* Control 6 */
107#define REG_AECH 0x10 /* More bits of AEC value */
108#define REG_CLKRC 0x11 /* Clock control */
109#define CLK_PLL 0x80 /* Enable internal PLL */
110#define CLK_EXT 0x40 /* Use external clock directly */
111#define CLK_SCALE 0x3f /* Mask for internal clock scale */
112#define REG_COM7 0x12 /* Control 7 */
113#define COM7_RESET 0x80 /* Register reset */
114#define COM7_FMT_MASK 0x38
115#define COM7_FMT_SXGA 0x00
116#define COM7_FMT_VGA 0x40
117#define COM7_FMT_CIF 0x20 /* CIF format */
118#define COM7_FMT_QVGA 0x10 /* QVGA format */
119#define COM7_FMT_QCIF 0x08 /* QCIF format */
120#define COM7_RGB 0x04 /* bits 0 and 2 - RGB format */
121#define COM7_YUV 0x00 /* YUV */
122#define COM7_BAYER 0x01 /* Bayer format */
123#define COM7_PBAYER 0x05 /* "Processed bayer" */
124#define REG_COM8 0x13 /* Control 8 */
125#define COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
126#define COM8_AECSTEP 0x40 /* Unlimited AEC step size */
127#define COM8_BFILT 0x20 /* Band filter enable */
128#define COM8_AGC 0x04 /* Auto gain enable */
129#define COM8_AWB 0x02 /* White balance enable */
130#define COM8_AEC 0x01 /* Auto exposure enable */
131#define REG_COM9 0x14 /* Control 9 - gain ceiling */
132#define REG_COM10 0x15 /* Control 10 */
133#define COM10_HSYNC 0x40 /* HSYNC instead of HREF */
134#define COM10_PCLK_HB 0x20 /* Suppress PCLK on horiz blank */
135#define COM10_HREF_REV 0x08 /* Reverse HREF */
136#define COM10_VS_LEAD 0x04 /* VSYNC on clock leading edge */
137#define COM10_VS_NEG 0x02 /* VSYNC negative */
138#define COM10_HS_NEG 0x01 /* HSYNC negative */
139#define REG_HSTART 0x17 /* Horiz start high bits */
140#define REG_HSTOP 0x18 /* Horiz stop high bits */
141#define REG_VSTART 0x19 /* Vert start high bits */
142#define REG_VSTOP 0x1a /* Vert stop high bits */
143#define REG_PSHFT 0x1b /* Pixel delay after HREF */
144#define REG_MIDH 0x1c /* Manuf. ID high */
145#define REG_MIDL 0x1d /* Manuf. ID low */
146#define REG_MVFP 0x1e /* Mirror / vflip */
147#define MVFP_MIRROR 0x20 /* Mirror image */
148#define MVFP_FLIP 0x10 /* Vertical flip */
149
150#define REG_AEW 0x24 /* AGC upper limit */
151#define REG_AEB 0x25 /* AGC lower limit */
152#define REG_VPT 0x26 /* AGC/AEC fast mode op region */
153#define REG_ADVFL 0x2d /* Insert dummy lines (LSB) */
154#define REG_ADVFH 0x2e /* Insert dummy lines (MSB) */
155#define REG_HSYST 0x30 /* HSYNC rising edge delay */
156#define REG_HSYEN 0x31 /* HSYNC falling edge delay */
157#define REG_HREF 0x32 /* HREF pieces */
158#define REG_TSLB 0x3a /* lots of stuff */
159#define TSLB_YLAST 0x04 /* UYVY or VYUY - see com13 */
160#define TSLB_BYTEORD 0x08 /* swap bytes in 16bit mode? */
161#define REG_COM11 0x3b /* Control 11 */
162#define COM11_NIGHT 0x80 /* NIght mode enable */
163#define COM11_NMFR 0x60 /* Two bit NM frame rate */
164#define COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
165#define COM11_50HZ 0x08 /* Manual 50Hz select */
166#define COM11_EXP 0x02
167#define REG_COM12 0x3c /* Control 12 */
168#define COM12_HREF 0x80 /* HREF always */
169#define REG_COM13 0x3d /* Control 13 */
170#define COM13_GAMMA 0x80 /* Gamma enable */
171#define COM13_UVSAT 0x40 /* UV saturation auto adjustment */
172#define COM13_CMATRIX 0x10 /* Enable color matrix for RGB or YUV */
173#define COM13_UVSWAP 0x01 /* V before U - w/TSLB */
174#define REG_COM14 0x3e /* Control 14 */
175#define COM14_DCWEN 0x10 /* DCW/PCLK-scale enable */
176#define REG_EDGE 0x3f /* Edge enhancement factor */
177#define REG_COM15 0x40 /* Control 15 */
178#define COM15_R10F0 0x00 /* Data range 10 to F0 */
179#define COM15_R01FE 0x80 /* 01 to FE */
180#define COM15_R00FF 0xc0 /* 00 to FF */
181#define COM15_RGB565 0x10 /* RGB565 output */
182#define COM15_RGBFIXME 0x20 /* FIXME */
183#define COM15_RGB555 0x30 /* RGB555 output */
184#define REG_COM16 0x41 /* Control 16 */
185#define COM16_AWBGAIN 0x08 /* AWB gain enable */
186#define REG_COM17 0x42 /* Control 17 */
187#define COM17_AECWIN 0xc0 /* AEC window - must match COM4 */
188#define COM17_CBAR 0x08 /* DSP Color bar */
189
190/*
191 * This matrix defines how the colors are generated, must be
192 * tweaked to adjust hue and saturation.
193 *
194 * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
195 *
196 * They are nine-bit signed quantities, with the sign bit
197 * stored in 0x58. Sign for v-red is bit 0, and up from there.
198 */
199#define REG_CMATRIX_BASE 0x4f
200#define CMATRIX_LEN 6
201#define REG_CMATRIX_SIGN 0x58
202
203
204#define REG_BRIGHT 0x55 /* Brightness */
205#define REG_CONTRAS 0x56 /* Contrast control */
206
207#define REG_GFIX 0x69 /* Fix gain control */
208
209#define REG_RGB444 0x8c /* RGB 444 control */
210#define R444_ENABLE 0x02 /* Turn on RGB444, overrides 5x5 */
211#define R444_RGBX 0x01 /* Empty nibble at end */
212
213#define REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
214#define REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
215
216#define REG_BD50MAX 0xa5 /* 50hz banding step limit */
217#define REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
218#define REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
219#define REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
220#define REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
221#define REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
222#define REG_BD60MAX 0xab /* 60hz banding step limit */
223
224
225
226
227/* Returns 0 if OK */
228int stk_sensor_outb(struct stk_camera *dev, u8 reg, u8 val)
229{
230 int i = 0;
231 int tmpval = 0;
232
233 if (stk_camera_write_reg(dev, STK_IIC_TX_INDEX, reg))
234 return 1;
235 if (stk_camera_write_reg(dev, STK_IIC_TX_VALUE, val))
236 return 1;
237 if (stk_camera_write_reg(dev, STK_IIC_OP, STK_IIC_OP_TX))
238 return 1;
239 do {
240 if (stk_camera_read_reg(dev, STK_IIC_STAT, &tmpval))
241 return 1;
242 i++;
243 } while (tmpval == 0 && i < MAX_RETRIES);
244 if (tmpval != STK_IIC_STAT_TX_OK) {
245 if (tmpval)
246 STK_ERROR("stk_sensor_outb failed, status=0x%02x\n",
247 tmpval);
248 return 1;
249 } else
250 return 0;
251}
252
253int stk_sensor_inb(struct stk_camera *dev, u8 reg, u8 *val)
254{
255 int i = 0;
256 int tmpval = 0;
257
258 if (stk_camera_write_reg(dev, STK_IIC_RX_INDEX, reg))
259 return 1;
260 if (stk_camera_write_reg(dev, STK_IIC_OP, STK_IIC_OP_RX))
261 return 1;
262 do {
263 if (stk_camera_read_reg(dev, STK_IIC_STAT, &tmpval))
264 return 1;
265 i++;
266 } while (tmpval == 0 && i < MAX_RETRIES);
267 if (tmpval != STK_IIC_STAT_RX_OK) {
268 if (tmpval)
269 STK_ERROR("stk_sensor_inb failed, status=0x%02x\n",
270 tmpval);
271 return 1;
272 }
273
274 if (stk_camera_read_reg(dev, STK_IIC_RX_VALUE, &tmpval))
275 return 1;
276
277 *val = (u8) tmpval;
278 return 0;
279}
280
281static int stk_sensor_write_regvals(struct stk_camera *dev,
282 struct regval *rv)
283{
284 int ret;
285 if (rv == NULL)
286 return 0;
287 while (rv->reg != 0xff || rv->val != 0xff) {
288 ret = stk_sensor_outb(dev, rv->reg, rv->val);
289 if (ret != 0)
290 return ret;
291 rv++;
292 }
293 return 0;
294}
295
296int stk_sensor_sleep(struct stk_camera *dev)
297{
298 u8 tmp;
299 return stk_sensor_inb(dev, REG_COM2, &tmp)
300 || stk_sensor_outb(dev, REG_COM2, tmp|COM2_SSLEEP);
301}
302
303int stk_sensor_wakeup(struct stk_camera *dev)
304{
305 u8 tmp;
306 return stk_sensor_inb(dev, REG_COM2, &tmp)
307 || stk_sensor_outb(dev, REG_COM2, tmp&~COM2_SSLEEP);
308}
309
310static struct regval ov_initvals[] = {
311 {REG_CLKRC, CLK_PLL},
312 {REG_COM11, 0x01},
313 {0x6a, 0x7d},
314 {REG_AECH, 0x40},
315 {REG_GAIN, 0x00},
316 {REG_BLUE, 0x80},
317 {REG_RED, 0x80},
318 /* Do not enable fast AEC for now */
319 /*{REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC},*/
320 {REG_COM8, COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC},
321 {0x39, 0x50}, {0x38, 0x93},
322 {0x37, 0x00}, {0x35, 0x81},
323 {REG_COM5, 0x20},
324 {REG_COM1, 0x00},
325 {REG_COM3, 0x00},
326 {REG_COM4, 0x00},
327 {REG_PSHFT, 0x00},
328 {0x16, 0x07},
329 {0x33, 0xe2}, {0x34, 0xbf},
330 {REG_COM16, 0x00},
331 {0x96, 0x04},
332 /* Gamma curve values */
333/* { 0x7a, 0x20 }, { 0x7b, 0x10 },
334 { 0x7c, 0x1e }, { 0x7d, 0x35 },
335 { 0x7e, 0x5a }, { 0x7f, 0x69 },
336 { 0x80, 0x76 }, { 0x81, 0x80 },
337 { 0x82, 0x88 }, { 0x83, 0x8f },
338 { 0x84, 0x96 }, { 0x85, 0xa3 },
339 { 0x86, 0xaf }, { 0x87, 0xc4 },
340 { 0x88, 0xd7 }, { 0x89, 0xe8 },
341*/
342 {REG_GFIX, 0x40},
343 {0x8e, 0x00},
344 {REG_COM12, 0x73},
345 {0x8f, 0xdf}, {0x8b, 0x06},
346 {0x8c, 0x20},
347 {0x94, 0x88}, {0x95, 0x88},
348/* {REG_COM15, 0xc1}, TODO */
349 {0x29, 0x3f},
350 {REG_COM6, 0x42},
351 {REG_BD50MAX, 0x80},
352 {REG_HAECC6, 0xb8}, {REG_HAECC7, 0x92},
353 {REG_BD60MAX, 0x0a},
354 {0x90, 0x00}, {0x91, 0x00},
355 {REG_HAECC1, 0x00}, {REG_HAECC2, 0x00},
356 {REG_AEW, 0x68}, {REG_AEB, 0x5c},
357 {REG_VPT, 0xc3},
358 {REG_COM9, 0x2e},
359 {0x2a, 0x00}, {0x2b, 0x00},
360
361 {0xff, 0xff}, /* END MARKER */
362};
363
364/* Probe the I2C bus and initialise the sensor chip */
365int stk_sensor_init(struct stk_camera *dev)
366{
367 u8 idl = 0;
368 u8 idh = 0;
369
370 if (stk_camera_write_reg(dev, STK_IIC_ENABLE, STK_IIC_ENABLE_YES)
371 || stk_camera_write_reg(dev, STK_IIC_ADDR, SENSOR_ADDRESS)
372 || stk_sensor_outb(dev, REG_COM7, COM7_RESET)) {
373 STK_ERROR("Sensor resetting failed\n");
374 return -ENODEV;
375 }
376 msleep(10);
377 /* Read the manufacturer ID: ov = 0x7FA2 */
378 if (stk_sensor_inb(dev, REG_MIDH, &idh)
379 || stk_sensor_inb(dev, REG_MIDL, &idl)) {
380 STK_ERROR("Strange error reading sensor ID\n");
381 return -ENODEV;
382 }
383 if (idh != 0x7F || idl != 0xA2) {
384 STK_ERROR("Huh? you don't have a sensor from ovt\n");
385 return -ENODEV;
386 }
387 if (stk_sensor_inb(dev, REG_PID, &idh)
388 || stk_sensor_inb(dev, REG_VER, &idl)) {
389 STK_ERROR("Could not read sensor model\n");
390 return -ENODEV;
391 }
392 stk_sensor_write_regvals(dev, ov_initvals);
393 msleep(10);
394 STK_INFO("OmniVision sensor detected, id %02X%02X"
395 " at address %x\n", idh, idl, SENSOR_ADDRESS);
396 return 0;
397}
398
399/* V4L2_PIX_FMT_UYVY */
400static struct regval ov_fmt_uyvy[] = {
401 {REG_TSLB, TSLB_YLAST|0x08 },
402 { 0x4f, 0x80 }, /* "matrix coefficient 1" */
403 { 0x50, 0x80 }, /* "matrix coefficient 2" */
404 { 0x51, 0 }, /* vb */
405 { 0x52, 0x22 }, /* "matrix coefficient 4" */
406 { 0x53, 0x5e }, /* "matrix coefficient 5" */
407 { 0x54, 0x80 }, /* "matrix coefficient 6" */
408 {REG_COM13, COM13_UVSAT|COM13_CMATRIX},
409 {REG_COM15, COM15_R00FF },
410 {0xff, 0xff}, /* END MARKER */
411};
412
413/* V4L2_PIX_FMT_RGB565X rrrrrggg gggbbbbb */
414static struct regval ov_fmt_rgbr[] = {
415 { REG_RGB444, 0 }, /* No RGB444 please */
416 {REG_TSLB, 0x00},
417 { REG_COM1, 0x0 },
418 { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
419 { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
420 { 0x50, 0xb3 }, /* "matrix coefficient 2" */
421 { 0x51, 0 }, /* vb */
422 { 0x52, 0x3d }, /* "matrix coefficient 4" */
423 { 0x53, 0xa7 }, /* "matrix coefficient 5" */
424 { 0x54, 0xe4 }, /* "matrix coefficient 6" */
425 { REG_COM13, COM13_GAMMA },
426 { REG_COM15, COM15_RGB565|COM15_R00FF },
427 { 0xff, 0xff },
428};
429
430/* V4L2_PIX_FMT_RGB565 gggbbbbb rrrrrggg */
431static struct regval ov_fmt_rgbp[] = {
432 { REG_RGB444, 0 }, /* No RGB444 please */
433 {REG_TSLB, TSLB_BYTEORD },
434 { REG_COM1, 0x0 },
435 { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
436 { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
437 { 0x50, 0xb3 }, /* "matrix coefficient 2" */
438 { 0x51, 0 }, /* vb */
439 { 0x52, 0x3d }, /* "matrix coefficient 4" */
440 { 0x53, 0xa7 }, /* "matrix coefficient 5" */
441 { 0x54, 0xe4 }, /* "matrix coefficient 6" */
442 { REG_COM13, COM13_GAMMA },
443 { REG_COM15, COM15_RGB565|COM15_R00FF },
444 { 0xff, 0xff },
445};
446
447/* V4L2_PIX_FMT_SRGGB8 */
448static struct regval ov_fmt_bayer[] = {
449 /* This changes color order */
450 {REG_TSLB, 0x40}, /* BGGR */
451 /* {REG_TSLB, 0x08}, */ /* BGGR with vertical image flipping */
452 {REG_COM15, COM15_R00FF },
453 {0xff, 0xff}, /* END MARKER */
454};
455/*
456 * Store a set of start/stop values into the camera.
457 */
458static int stk_sensor_set_hw(struct stk_camera *dev,
459 int hstart, int hstop, int vstart, int vstop)
460{
461 int ret;
462 unsigned char v;
463/*
464 * Horizontal: 11 bits, top 8 live in hstart and hstop. Bottom 3 of
465 * hstart are in href[2:0], bottom 3 of hstop in href[5:3]. There is
466 * a mystery "edge offset" value in the top two bits of href.
467 */
468 ret = stk_sensor_outb(dev, REG_HSTART, (hstart >> 3) & 0xff);
469 ret += stk_sensor_outb(dev, REG_HSTOP, (hstop >> 3) & 0xff);
470 ret += stk_sensor_inb(dev, REG_HREF, &v);
471 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
472 msleep(10);
473 ret += stk_sensor_outb(dev, REG_HREF, v);
474/*
475 * Vertical: similar arrangement (note: this is different from ov7670.c)
476 */
477 ret += stk_sensor_outb(dev, REG_VSTART, (vstart >> 3) & 0xff);
478 ret += stk_sensor_outb(dev, REG_VSTOP, (vstop >> 3) & 0xff);
479 ret += stk_sensor_inb(dev, REG_VREF, &v);
480 v = (v & 0xc0) | ((vstop & 0x7) << 3) | (vstart & 0x7);
481 msleep(10);
482 ret += stk_sensor_outb(dev, REG_VREF, v);
483 return ret;
484}
485
486
487int stk_sensor_configure(struct stk_camera *dev)
488{
489 int com7;
490 /*
491 * We setup the sensor to output dummy lines in low-res modes,
492 * so we don't get absurdly hight framerates.
493 */
494 unsigned dummylines;
495 int flip;
496 struct regval *rv;
497
498 switch (dev->vsettings.mode) {
499 case MODE_QCIF: com7 = COM7_FMT_QCIF;
500 dummylines = 604;
501 break;
502 case MODE_QVGA: com7 = COM7_FMT_QVGA;
503 dummylines = 267;
504 break;
505 case MODE_CIF: com7 = COM7_FMT_CIF;
506 dummylines = 412;
507 break;
508 case MODE_VGA: com7 = COM7_FMT_VGA;
509 dummylines = 11;
510 break;
511 case MODE_SXGA: com7 = COM7_FMT_SXGA;
512 dummylines = 0;
513 break;
514 default: STK_ERROR("Unsupported mode %d\n", dev->vsettings.mode);
515 return -EFAULT;
516 }
517 switch (dev->vsettings.palette) {
518 case V4L2_PIX_FMT_UYVY:
519 com7 |= COM7_YUV;
520 rv = ov_fmt_uyvy;
521 break;
522 case V4L2_PIX_FMT_RGB565:
523 com7 |= COM7_RGB;
524 rv = ov_fmt_rgbp;
525 break;
526 case V4L2_PIX_FMT_RGB565X:
527 com7 |= COM7_RGB;
528 rv = ov_fmt_rgbr;
529 break;
530 case V4L2_PIX_FMT_SBGGR8:
531 com7 |= COM7_PBAYER;
532 rv = ov_fmt_bayer;
533 break;
534 default: STK_ERROR("Unsupported colorspace\n");
535 return -EFAULT;
536 }
537 /*FIXME sometimes the sensor go to a bad state
538 stk_sensor_write_regvals(dev, ov_initvals); */
539 stk_sensor_outb(dev, REG_COM7, com7);
540 msleep(50);
541 stk_sensor_write_regvals(dev, rv);
542 flip = (dev->vsettings.vflip?MVFP_FLIP:0)
543 | (dev->vsettings.hflip?MVFP_MIRROR:0);
544 stk_sensor_outb(dev, REG_MVFP, flip);
545 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8
546 && !dev->vsettings.vflip)
547 stk_sensor_outb(dev, REG_TSLB, 0x08);
548 stk_sensor_outb(dev, REG_ADVFH, dummylines >> 8);
549 stk_sensor_outb(dev, REG_ADVFL, dummylines & 0xff);
550 msleep(50);
551 switch (dev->vsettings.mode) {
552 case MODE_VGA:
553 if (stk_sensor_set_hw(dev, 302, 1582, 6, 486))
554 STK_ERROR("stk_sensor_set_hw failed (VGA)\n");
555 break;
556 case MODE_SXGA:
557 case MODE_CIF:
558 case MODE_QVGA:
559 case MODE_QCIF:
560 /*FIXME These settings seem ignored by the sensor
561 if (stk_sensor_set_hw(dev, 220, 1500, 10, 1034))
562 STK_ERROR("stk_sensor_set_hw failed (SXGA)\n");
563 */
564 break;
565 }
566 msleep(10);
567 return 0;
568}
569
570int stk_sensor_set_brightness(struct stk_camera *dev, int br)
571{
572 if (br < 0 || br > 0xff)
573 return -EINVAL;
574 stk_sensor_outb(dev, REG_AEB, max(0x00, br - 6));
575 stk_sensor_outb(dev, REG_AEW, min(0xff, br + 6));
576 return 0;
577}
578
diff --git a/drivers/media/video/stk-webcam.c b/drivers/media/video/stk-webcam.c
new file mode 100644
index 000000000000..d37e5e2594b4
--- /dev/null
+++ b/drivers/media/video/stk-webcam.c
@@ -0,0 +1,1465 @@
1/*
2 * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
3 *
4 * Copyright (C) 2006 Nicolas VIVIEN
5 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
6 *
7 * Some parts are inspired from cafe_ccic.c
8 * Copyright 2006-2007 Jonathan Corbet
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 * 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/module.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/errno.h>
29#include <linux/slab.h>
30#include <linux/kref.h>
31
32#include <linux/usb.h>
33#include <linux/vmalloc.h>
34#include <linux/videodev2.h>
35#include <media/v4l2-common.h>
36
37#include "stk-webcam.h"
38
39
40static int hflip = 1;
41module_param(hflip, bool, 0444);
42MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
43
44static int vflip = 1;
45module_param(vflip, bool, 0444);
46MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
47
48static int debug;
49module_param(debug, int, 0444);
50MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
51
52MODULE_LICENSE("GPL");
53MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
54MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
55
56
57
58/* Some cameras have audio interfaces, we aren't interested in those */
59static struct usb_device_id stkwebcam_table[] = {
60 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
61 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
62 { }
63};
64MODULE_DEVICE_TABLE(usb, stkwebcam_table);
65
66void stk_camera_cleanup(struct kref *kref)
67{
68 struct stk_camera *dev = to_stk_camera(kref);
69
70 STK_INFO("Syntek USB2.0 Camera release resources"
71 " video device /dev/video%d\n", dev->vdev.minor);
72 video_unregister_device(&dev->vdev);
73 dev->vdev.priv = NULL;
74
75 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
76 STK_ERROR("We are leaking memory\n");
77 usb_put_intf(dev->interface);
78 kfree(dev);
79}
80
81
82/*
83 * Basic stuff
84 */
85int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
86{
87 struct usb_device *udev = dev->udev;
88 int ret;
89
90 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
91 0x01,
92 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
93 value,
94 index,
95 NULL,
96 0,
97 500);
98 if (ret < 0)
99 return ret;
100 else
101 return 0;
102}
103
104int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
105{
106 struct usb_device *udev = dev->udev;
107 int ret;
108
109 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
110 0x00,
111 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
112 0x00,
113 index,
114 (u8 *) value,
115 sizeof(u8),
116 500);
117 if (ret < 0)
118 return ret;
119 else
120 return 0;
121}
122
123static int stk_start_stream(struct stk_camera *dev)
124{
125 int value;
126 int i, ret;
127 int value_116, value_117;
128
129 if (!is_present(dev))
130 return -ENODEV;
131 if (!is_memallocd(dev) || !is_initialised(dev)) {
132 STK_ERROR("FIXME: Buffers are not allocated\n");
133 return -EFAULT;
134 }
135 ret = usb_set_interface(dev->udev, 0, 5);
136
137 if (ret < 0)
138 STK_ERROR("usb_set_interface failed !\n");
139 if (stk_sensor_wakeup(dev))
140 STK_ERROR("error awaking the sensor\n");
141
142 stk_camera_read_reg(dev, 0x0116, &value_116);
143 stk_camera_read_reg(dev, 0x0117, &value_117);
144
145 stk_camera_write_reg(dev, 0x0116, 0x0000);
146 stk_camera_write_reg(dev, 0x0117, 0x0000);
147
148 stk_camera_read_reg(dev, 0x0100, &value);
149 stk_camera_write_reg(dev, 0x0100, value | 0x80);
150
151 stk_camera_write_reg(dev, 0x0116, value_116);
152 stk_camera_write_reg(dev, 0x0117, value_117);
153 for (i = 0; i < MAX_ISO_BUFS; i++) {
154 if (dev->isobufs[i].urb) {
155 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
156 atomic_inc(&dev->urbs_used);
157 if (ret)
158 return ret;
159 }
160 }
161 set_streaming(dev);
162 return 0;
163}
164
165static int stk_stop_stream(struct stk_camera *dev)
166{
167 int value;
168 int i;
169 if (is_present(dev)) {
170 stk_camera_read_reg(dev, 0x0100, &value);
171 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
172 if (dev->isobufs != NULL) {
173 for (i = 0; i < MAX_ISO_BUFS; i++) {
174 if (dev->isobufs[i].urb)
175 usb_kill_urb(dev->isobufs[i].urb);
176 }
177 }
178 unset_streaming(dev);
179
180 if (usb_set_interface(dev->udev, 0, 0))
181 STK_ERROR("usb_set_interface failed !\n");
182 if (stk_sensor_sleep(dev))
183 STK_ERROR("error suspending the sensor\n");
184 }
185 return 0;
186}
187
188/*
189 * This seems to be the shortest init sequence we
190 * must do in order to find the sensor
191 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
192 * is also reset. Maybe powers down it?
193 * Rest of values don't make a difference
194 */
195
196static struct regval stk1125_initvals[] = {
197 /*TODO: What means this sequence? */
198 {0x0000, 0x24},
199 {0x0100, 0x21},
200 {0x0002, 0x68},
201 {0x0003, 0x80},
202 {0x0005, 0x00},
203 {0x0007, 0x03},
204 {0x000d, 0x00},
205 {0x000f, 0x02},
206 {0x0300, 0x12},
207 {0x0350, 0x41},
208 {0x0351, 0x00},
209 {0x0352, 0x00},
210 {0x0353, 0x00},
211 {0x0018, 0x10},
212 {0x0019, 0x00},
213 {0x001b, 0x0e},
214 {0x001c, 0x46},
215 {0x0300, 0x80},
216 {0x001a, 0x04},
217 {0x0110, 0x00},
218 {0x0111, 0x00},
219 {0x0112, 0x00},
220 {0x0113, 0x00},
221
222 {0xffff, 0xff},
223};
224
225
226static int stk_initialise(struct stk_camera *dev)
227{
228 struct regval *rv;
229 int ret;
230 if (!is_present(dev))
231 return -ENODEV;
232 if (is_initialised(dev))
233 return 0;
234 rv = stk1125_initvals;
235 while (rv->reg != 0xffff) {
236 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
237 if (ret)
238 return ret;
239 rv++;
240 }
241 if (stk_sensor_init(dev) == 0) {
242 set_initialised(dev);
243 return 0;
244 } else
245 return -1;
246}
247
248/* sysfs functions */
249/*FIXME cleanup this */
250
251static ssize_t show_brightness(struct device *class,
252 struct device_attribute *attr, char *buf)
253{
254 struct video_device *vdev = to_video_device(class);
255 struct stk_camera *dev = vdev_to_camera(vdev);
256
257 return sprintf(buf, "%X\n", dev->vsettings.brightness);
258}
259
260static ssize_t store_brightness(struct device *class,
261 struct device_attribute *attr, const char *buf, size_t count)
262{
263 char *endp;
264 unsigned long value;
265 int ret;
266
267 struct video_device *vdev = to_video_device(class);
268 struct stk_camera *dev = vdev_to_camera(vdev);
269
270 value = simple_strtoul(buf, &endp, 16);
271
272 dev->vsettings.brightness = (int) value;
273
274 ret = stk_sensor_set_brightness(dev, value >> 8);
275 if (ret)
276 return ret;
277 else
278 return count;
279}
280
281static ssize_t show_hflip(struct device *class,
282 struct device_attribute *attr, char *buf)
283{
284 struct video_device *vdev = to_video_device(class);
285 struct stk_camera *dev = vdev_to_camera(vdev);
286
287 return sprintf(buf, "%d\n", dev->vsettings.hflip);
288}
289
290static ssize_t store_hflip(struct device *class,
291 struct device_attribute *attr, const char *buf, size_t count)
292{
293 struct video_device *vdev = to_video_device(class);
294 struct stk_camera *dev = vdev_to_camera(vdev);
295
296 if (strncmp(buf, "1", 1) == 0)
297 dev->vsettings.hflip = 1;
298 else if (strncmp(buf, "0", 1) == 0)
299 dev->vsettings.hflip = 0;
300 else
301 return -EINVAL;
302
303 return strlen(buf);
304}
305
306static ssize_t show_vflip(struct device *class,
307 struct device_attribute *attr, char *buf)
308{
309 struct video_device *vdev = to_video_device(class);
310 struct stk_camera *dev = vdev_to_camera(vdev);
311
312 return sprintf(buf, "%d\n", dev->vsettings.vflip);
313}
314
315static ssize_t store_vflip(struct device *class,
316 struct device_attribute *attr, const char *buf, size_t count)
317{
318 struct video_device *vdev = to_video_device(class);
319 struct stk_camera *dev = vdev_to_camera(vdev);
320
321 if (strncmp(buf, "1", 1) == 0)
322 dev->vsettings.vflip = 1;
323 else if (strncmp(buf, "0", 1) == 0)
324 dev->vsettings.vflip = 0;
325 else
326 return -EINVAL;
327
328 return strlen(buf);
329}
330
331static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO,
332 show_brightness, store_brightness);
333static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip);
334static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip);
335
336static int stk_create_sysfs_files(struct video_device *vdev)
337{
338 int ret;
339
340 ret = video_device_create_file(vdev, &dev_attr_brightness);
341 ret += video_device_create_file(vdev, &dev_attr_hflip);
342 ret += video_device_create_file(vdev, &dev_attr_vflip);
343 return ret;
344}
345
346static void stk_remove_sysfs_files(struct video_device *vdev)
347{
348 video_device_remove_file(vdev, &dev_attr_brightness);
349 video_device_remove_file(vdev, &dev_attr_hflip);
350 video_device_remove_file(vdev, &dev_attr_vflip);
351}
352
353
354/* *********************************************** */
355/*
356 * This function is called as an URB transfert is complete (Isochronous pipe).
357 * So, the traitement is done in interrupt time, so it has be fast, not crash,
358 * and not stall. Neat.
359 */
360static void stk_isoc_handler(struct urb *urb)
361{
362 int i;
363 int ret;
364 int framelen;
365 unsigned long flags;
366
367 unsigned char *fill = NULL;
368 unsigned char *iso_buf = NULL;
369
370 struct stk_camera *dev;
371 struct stk_sio_buffer *fb;
372
373 dev = (struct stk_camera *) urb->context;
374
375 if (dev == NULL) {
376 STK_ERROR("isoc_handler called with NULL device !\n");
377 return;
378 }
379
380 if (urb->status == -ENOENT || urb->status == -ECONNRESET
381 || urb->status == -ESHUTDOWN) {
382 atomic_dec(&dev->urbs_used);
383 return;
384 }
385
386 spin_lock_irqsave(&dev->spinlock, flags);
387
388 if (urb->status != -EINPROGRESS && urb->status != 0) {
389 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
390 goto resubmit;
391 }
392
393 if (list_empty(&dev->sio_avail)) {
394 /*FIXME Stop streaming after a while */
395 (void) (printk_ratelimit() &&
396 STK_ERROR("isoc_handler without available buffer!\n"));
397 goto resubmit;
398 }
399 fb = list_first_entry(&dev->sio_avail,
400 struct stk_sio_buffer, list);
401 fill = fb->buffer + fb->v4lbuf.bytesused;
402
403 for (i = 0; i < urb->number_of_packets; i++) {
404 if (urb->iso_frame_desc[i].status != 0) {
405 if (urb->iso_frame_desc[i].status != -EXDEV)
406 STK_ERROR("Frame %d has error %d\n", i,
407 urb->iso_frame_desc[i].status);
408 continue;
409 }
410 framelen = urb->iso_frame_desc[i].actual_length;
411 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
412
413 if (framelen <= 4)
414 continue; /* no data */
415
416 /*
417 * we found something informational from there
418 * the isoc frames have to type of headers
419 * type1: 00 xx 00 00 or 20 xx 00 00
420 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
421 * xx is a sequencer which has never been seen over 0x3f
422 * imho data written down looks like bayer, i see similarities
423 * after every 640 bytes
424 */
425 if (*iso_buf & 0x80) {
426 framelen -= 8;
427 iso_buf += 8;
428 /* This marks a new frame */
429 if (fb->v4lbuf.bytesused != 0
430 && fb->v4lbuf.bytesused != dev->frame_size) {
431 (void) (printk_ratelimit() &&
432 STK_ERROR("frame %d, "
433 "bytesused=%d, skipping\n",
434 i, fb->v4lbuf.bytesused));
435 fb->v4lbuf.bytesused = 0;
436 fill = fb->buffer;
437 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
438 list_move_tail(dev->sio_avail.next,
439 &dev->sio_full);
440 wake_up(&dev->wait_frame);
441 if (list_empty(&dev->sio_avail)) {
442 (void) (printk_ratelimit() &&
443 STK_ERROR("No buffer available\n"));
444 goto resubmit;
445 }
446 fb = list_first_entry(&dev->sio_avail,
447 struct stk_sio_buffer, list);
448 fb->v4lbuf.bytesused = 0;
449 fill = fb->buffer;
450 }
451 } else {
452 framelen -= 4;
453 iso_buf += 4;
454 }
455
456 /* Our buffer is full !!! */
457 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
458 (void) (printk_ratelimit() &&
459 STK_ERROR("Frame buffer overflow, lost sync\n"));
460 /*FIXME Do something here? */
461 continue;
462 }
463 spin_unlock_irqrestore(&dev->spinlock, flags);
464 memcpy(fill, iso_buf, framelen);
465 spin_lock_irqsave(&dev->spinlock, flags);
466 fill += framelen;
467
468 /* New size of our buffer */
469 fb->v4lbuf.bytesused += framelen;
470 }
471
472resubmit:
473 spin_unlock_irqrestore(&dev->spinlock, flags);
474 urb->dev = dev->udev;
475 ret = usb_submit_urb(urb, GFP_ATOMIC);
476 if (ret != 0) {
477 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
478 ret);
479 }
480}
481
482/* -------------------------------------------- */
483
484static int stk_prepare_iso(struct stk_camera *dev)
485{
486 void *kbuf;
487 int i, j;
488 struct urb *urb;
489 struct usb_device *udev;
490
491 if (dev == NULL)
492 return -ENXIO;
493 udev = dev->udev;
494
495 if (dev->isobufs)
496 STK_ERROR("isobufs already allocated. Bad\n");
497 else
498 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
499 GFP_KERNEL);
500 if (dev->isobufs == NULL) {
501 STK_ERROR("Unable to allocate iso buffers\n");
502 return -ENOMEM;
503 }
504 for (i = 0; i < MAX_ISO_BUFS; i++) {
505 if (dev->isobufs[i].data == NULL) {
506 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
507 if (kbuf == NULL) {
508 STK_ERROR("Failed to allocate iso buffer %d\n",
509 i);
510 goto isobufs_out;
511 }
512 dev->isobufs[i].data = kbuf;
513 } else
514 STK_ERROR("isobuf data already allocated\n");
515 if (dev->isobufs[i].urb == NULL) {
516 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
517 if (urb == NULL) {
518 STK_ERROR("Failed to allocate URB %d\n", i);
519 goto isobufs_out;
520 }
521 dev->isobufs[i].urb = urb;
522 } else {
523 STK_ERROR("Killing URB\n");
524 usb_kill_urb(dev->isobufs[i].urb);
525 urb = dev->isobufs[i].urb;
526 }
527 urb->interval = 1;
528 urb->dev = udev;
529 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
530 urb->transfer_flags = URB_ISO_ASAP;
531 urb->transfer_buffer = dev->isobufs[i].data;
532 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
533 urb->complete = stk_isoc_handler;
534 urb->context = dev;
535 urb->start_frame = 0;
536 urb->number_of_packets = ISO_FRAMES_PER_DESC;
537
538 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
539 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
540 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
541 }
542 }
543 set_memallocd(dev);
544 return 0;
545
546isobufs_out:
547 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
548 kfree(dev->isobufs[i].data);
549 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
550 usb_free_urb(dev->isobufs[i].urb);
551 kfree(dev->isobufs);
552 dev->isobufs = NULL;
553 return -ENOMEM;
554}
555
556static void stk_clean_iso(struct stk_camera *dev)
557{
558 int i;
559
560 if (dev == NULL || dev->isobufs == NULL)
561 return;
562
563 for (i = 0; i < MAX_ISO_BUFS; i++) {
564 struct urb *urb;
565
566 urb = dev->isobufs[i].urb;
567 if (urb) {
568 if (atomic_read(&dev->urbs_used))
569 usb_kill_urb(urb);
570 usb_free_urb(urb);
571 }
572 kfree(dev->isobufs[i].data);
573 }
574 kfree(dev->isobufs);
575 dev->isobufs = NULL;
576 unset_memallocd(dev);
577}
578
579static int stk_setup_siobuf(struct stk_camera *dev, int index)
580{
581 struct stk_sio_buffer *buf = dev->sio_bufs + index;
582 INIT_LIST_HEAD(&buf->list);
583 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
584 buf->buffer = vmalloc_user(buf->v4lbuf.length);
585 if (buf->buffer == NULL)
586 return -ENOMEM;
587 buf->mapcount = 0;
588 buf->dev = dev;
589 buf->v4lbuf.index = index;
590 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
591 buf->v4lbuf.field = V4L2_FIELD_NONE;
592 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
593 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
594 return 0;
595}
596
597static int stk_free_sio_buffers(struct stk_camera *dev)
598{
599 int i;
600 int nbufs;
601 unsigned long flags;
602 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
603 return 0;
604 /*
605 * If any buffers are mapped, we cannot free them at all.
606 */
607 for (i = 0; i < dev->n_sbufs; i++) {
608 if (dev->sio_bufs[i].mapcount > 0)
609 return -EBUSY;
610 }
611 /*
612 * OK, let's do it.
613 */
614 spin_lock_irqsave(&dev->spinlock, flags);
615 INIT_LIST_HEAD(&dev->sio_avail);
616 INIT_LIST_HEAD(&dev->sio_full);
617 nbufs = dev->n_sbufs;
618 dev->n_sbufs = 0;
619 spin_unlock_irqrestore(&dev->spinlock, flags);
620 for (i = 0; i < nbufs; i++) {
621 if (dev->sio_bufs[i].buffer != NULL)
622 vfree(dev->sio_bufs[i].buffer);
623 }
624 kfree(dev->sio_bufs);
625 dev->sio_bufs = NULL;
626 return 0;
627}
628
629static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
630{
631 int i;
632 if (dev->sio_bufs != NULL)
633 STK_ERROR("sio_bufs already allocated\n");
634 else {
635 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
636 GFP_KERNEL);
637 if (dev->sio_bufs == NULL)
638 return -ENOMEM;
639 for (i = 0; i < n_sbufs; i++) {
640 if (stk_setup_siobuf(dev, i))
641 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
642 dev->n_sbufs = i+1;
643 }
644 }
645 return 0;
646}
647
648static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
649{
650 int err;
651 err = stk_prepare_iso(dev);
652 if (err) {
653 stk_clean_iso(dev);
654 return err;
655 }
656 err = stk_prepare_sio_buffers(dev, n_sbufs);
657 if (err) {
658 stk_free_sio_buffers(dev);
659 return err;
660 }
661 return 0;
662}
663
664static void stk_free_buffers(struct stk_camera *dev)
665{
666 stk_clean_iso(dev);
667 stk_free_sio_buffers(dev);
668}
669/* -------------------------------------------- */
670
671/* v4l file operations */
672
673static int v4l_stk_open(struct inode *inode, struct file *fp)
674{
675 struct stk_camera *dev;
676 struct video_device *vdev;
677
678 vdev = video_devdata(fp);
679 dev = vdev_to_camera(vdev);
680
681 if (dev == NULL || !is_present(dev))
682 return -ENXIO;
683 fp->private_data = vdev;
684 kref_get(&dev->kref);
685
686 return 0;
687}
688
689static int v4l_stk_release(struct inode *inode, struct file *fp)
690{
691 struct stk_camera *dev;
692 struct video_device *vdev;
693
694 vdev = video_devdata(fp);
695 if (vdev == NULL) {
696 STK_ERROR("v4l_release called w/o video devdata\n");
697 return -EFAULT;
698 }
699 dev = vdev_to_camera(vdev);
700 if (dev == NULL) {
701 STK_ERROR("v4l_release called on removed device\n");
702 return -ENODEV;
703 }
704
705 if (dev->owner != fp) {
706 kref_put(&dev->kref, stk_camera_cleanup);
707 return 0;
708 }
709
710 stk_stop_stream(dev);
711
712 stk_free_buffers(dev);
713
714 dev->owner = NULL;
715
716 kref_put(&dev->kref, stk_camera_cleanup);
717
718 return 0;
719}
720
721static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
722 size_t count, loff_t *f_pos)
723{
724 int i;
725 int ret;
726 unsigned long flags;
727 struct stk_camera *dev;
728 struct video_device *vdev;
729 struct stk_sio_buffer *sbuf;
730
731 vdev = video_devdata(fp);
732 if (vdev == NULL)
733 return -EFAULT;
734 dev = vdev_to_camera(vdev);
735
736 if (dev == NULL)
737 return -EIO;
738
739 if (!is_present(dev))
740 return -EIO;
741 if (dev->owner && dev->owner != fp)
742 return -EBUSY;
743 dev->owner = fp;
744 if (!is_streaming(dev)) {
745 if (stk_initialise(dev)
746 || stk_allocate_buffers(dev, 3)
747 || stk_start_stream(dev))
748 return -ENOMEM;
749 spin_lock_irqsave(&dev->spinlock, flags);
750 for (i = 0; i < dev->n_sbufs; i++) {
751 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
752 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
753 }
754 spin_unlock_irqrestore(&dev->spinlock, flags);
755 }
756 if (*f_pos == 0) {
757 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
758 return -EWOULDBLOCK;
759 ret = wait_event_interruptible(dev->wait_frame,
760 !list_empty(&dev->sio_full) || !is_present(dev));
761 if (ret)
762 return ret;
763 if (!is_present(dev))
764 return -EIO;
765 }
766 if (count + *f_pos > dev->frame_size)
767 count = dev->frame_size - *f_pos;
768 spin_lock_irqsave(&dev->spinlock, flags);
769 if (list_empty(&dev->sio_full)) {
770 spin_unlock_irqrestore(&dev->spinlock, flags);
771 STK_ERROR("BUG: No siobufs ready\n");
772 return 0;
773 }
774 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
775 spin_unlock_irqrestore(&dev->spinlock, flags);
776
777 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
778 return -EFAULT;
779
780 *f_pos += count;
781
782 if (*f_pos >= dev->frame_size) {
783 *f_pos = 0;
784 spin_lock_irqsave(&dev->spinlock, flags);
785 list_move_tail(&sbuf->list, &dev->sio_avail);
786 spin_unlock_irqrestore(&dev->spinlock, flags);
787 }
788 return count;
789}
790
791static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
792{
793 struct stk_camera *dev;
794 struct video_device *vdev;
795
796 vdev = video_devdata(fp);
797
798 if (vdev == NULL)
799 return -EFAULT;
800
801 dev = vdev_to_camera(vdev);
802 if (dev == NULL)
803 return -ENODEV;
804
805 poll_wait(fp, &dev->wait_frame, wait);
806
807 if (!is_present(dev))
808 return POLLERR;
809
810 if (!list_empty(&dev->sio_full))
811 return (POLLIN | POLLRDNORM);
812
813 return 0;
814}
815
816
817static void stk_v4l_vm_open(struct vm_area_struct *vma)
818{
819 struct stk_sio_buffer *sbuf = vma->vm_private_data;
820 sbuf->mapcount++;
821}
822static void stk_v4l_vm_close(struct vm_area_struct *vma)
823{
824 struct stk_sio_buffer *sbuf = vma->vm_private_data;
825 sbuf->mapcount--;
826 if (sbuf->mapcount == 0)
827 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
828}
829static struct vm_operations_struct stk_v4l_vm_ops = {
830 .open = stk_v4l_vm_open,
831 .close = stk_v4l_vm_close
832};
833
834static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
835{
836 unsigned int i;
837 int ret;
838 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
839 struct stk_camera *dev;
840 struct video_device *vdev;
841 struct stk_sio_buffer *sbuf = NULL;
842
843 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
844 return -EINVAL;
845
846 vdev = video_devdata(fp);
847 dev = vdev_to_camera(vdev);
848
849 for (i = 0; i < dev->n_sbufs; i++) {
850 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
851 sbuf = dev->sio_bufs + i;
852 break;
853 }
854 }
855 if (sbuf == NULL)
856 return -EINVAL;
857 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
858 if (ret)
859 return ret;
860 vma->vm_flags |= VM_DONTEXPAND;
861 vma->vm_private_data = sbuf;
862 vma->vm_ops = &stk_v4l_vm_ops;
863 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
864 stk_v4l_vm_open(vma);
865 return 0;
866}
867
868/* v4l ioctl handlers */
869
870static int stk_vidioc_querycap(struct file *filp,
871 void *priv, struct v4l2_capability *cap)
872{
873 strcpy(cap->driver, "stk");
874 strcpy(cap->card, "stk");
875 cap->version = DRIVER_VERSION_NUM;
876
877 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
878 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
879 return 0;
880}
881
882static int stk_vidioc_enum_input(struct file *filp,
883 void *priv, struct v4l2_input *input)
884{
885 if (input->index != 0)
886 return -EINVAL;
887
888 strcpy(input->name, "Syntek USB Camera");
889 input->type = V4L2_INPUT_TYPE_CAMERA;
890 return 0;
891}
892
893
894static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
895{
896 *i = 0;
897 return 0;
898}
899
900static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
901{
902 if (i != 0)
903 return -EINVAL;
904 else
905 return 0;
906}
907
908/* from vivi.c */
909static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
910{
911 return 0;
912}
913
914/* List of all V4Lv2 controls supported by the driver */
915static struct v4l2_queryctrl stk_controls[] = {
916 {
917 .id = V4L2_CID_BRIGHTNESS,
918 .type = V4L2_CTRL_TYPE_INTEGER,
919 .name = "Brightness",
920 .minimum = 0,
921 .maximum = 0xffff,
922 .step = 0x0100,
923 .default_value = 0x6000,
924 },
925 /*TODO: get more controls to work */
926};
927
928static int stk_vidioc_queryctrl(struct file *filp,
929 void *priv, struct v4l2_queryctrl *c)
930{
931 int i;
932 int nbr;
933 nbr = ARRAY_SIZE(stk_controls);
934
935 for (i = 0; i < nbr; i++) {
936 if (stk_controls[i].id == c->id) {
937 memcpy(c, &stk_controls[i],
938 sizeof(struct v4l2_queryctrl));
939 return 0;
940 }
941 }
942 return -EINVAL;
943}
944
945static int stk_vidioc_g_ctrl(struct file *filp,
946 void *priv, struct v4l2_control *c)
947{
948 struct stk_camera *dev = priv;
949 switch (c->id) {
950 case V4L2_CID_BRIGHTNESS:
951 c->value = dev->vsettings.brightness;
952 break;
953 default:
954 return -EINVAL;
955 }
956 return 0;
957}
958
959static int stk_vidioc_s_ctrl(struct file *filp,
960 void *priv, struct v4l2_control *c)
961{
962 struct stk_camera *dev = priv;
963 switch (c->id) {
964 case V4L2_CID_BRIGHTNESS:
965 dev->vsettings.brightness = c->value;
966 return stk_sensor_set_brightness(dev, c->value >> 8);
967 default:
968 return -EINVAL;
969 }
970 return 0;
971}
972
973
974static int stk_vidioc_enum_fmt_cap(struct file *filp,
975 void *priv, struct v4l2_fmtdesc *fmtd)
976{
977 fmtd->flags = 0;
978
979 switch (fmtd->index) {
980 case 0:
981 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
982 strcpy(fmtd->description, "r5g6b5");
983 break;
984 case 1:
985 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
986 strcpy(fmtd->description, "r5g6b5BE");
987 break;
988 case 2:
989 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
990 strcpy(fmtd->description, "yuv4:2:2");
991 break;
992 case 3:
993 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
994 strcpy(fmtd->description, "Raw bayer");
995 break;
996 default:
997 return -EINVAL;
998 }
999 return 0;
1000}
1001
1002static struct stk_size {
1003 unsigned w;
1004 unsigned h;
1005 enum stk_mode m;
1006} stk_sizes[] = {
1007 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1008 { .w = 640, .h = 480, .m = MODE_VGA, },
1009 { .w = 352, .h = 288, .m = MODE_CIF, },
1010 { .w = 320, .h = 240, .m = MODE_QVGA, },
1011 { .w = 176, .h = 144, .m = MODE_QCIF, },
1012};
1013
1014static int stk_vidioc_g_fmt_cap(struct file *filp,
1015 void *priv, struct v4l2_format *f)
1016{
1017 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1018 struct stk_camera *dev = priv;
1019 int i;
1020
1021 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1022 && stk_sizes[i].m != dev->vsettings.mode;
1023 i++);
1024 if (i == ARRAY_SIZE(stk_sizes)) {
1025 STK_ERROR("ERROR: mode invalid\n");
1026 return -EINVAL;
1027 }
1028 pix_format->width = stk_sizes[i].w;
1029 pix_format->height = stk_sizes[i].h;
1030 pix_format->field = V4L2_FIELD_NONE;
1031 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1032 pix_format->priv = 0;
1033 pix_format->pixelformat = dev->vsettings.palette;
1034 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1035 pix_format->bytesperline = pix_format->width;
1036 else
1037 pix_format->bytesperline = 2 * pix_format->width;
1038 pix_format->sizeimage = pix_format->bytesperline
1039 * pix_format->height;
1040 return 0;
1041}
1042
1043static int stk_vidioc_try_fmt_cap(struct file *filp,
1044 void *priv, struct v4l2_format *fmtd)
1045{
1046 int i;
1047 switch (fmtd->fmt.pix.pixelformat) {
1048 case V4L2_PIX_FMT_RGB565:
1049 case V4L2_PIX_FMT_RGB565X:
1050 case V4L2_PIX_FMT_UYVY:
1051 case V4L2_PIX_FMT_SBGGR8:
1052 break;
1053 default:
1054 return -EINVAL;
1055 }
1056 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1057 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1058 break;
1059 }
1060 if (i == ARRAY_SIZE(stk_sizes)
1061 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1062 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1063 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1064 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1065 fmtd->fmt.pix.priv = i - 1;
1066 } else {
1067 fmtd->fmt.pix.height = stk_sizes[i].h;
1068 fmtd->fmt.pix.width = stk_sizes[i].w;
1069 fmtd->fmt.pix.priv = i;
1070 }
1071
1072 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1073 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1074 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1075 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1076 else
1077 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1078 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1079 * fmtd->fmt.pix.height;
1080 return 0;
1081}
1082
1083static int stk_vidioc_s_fmt_cap(struct file *filp,
1084 void *priv, struct v4l2_format *fmtd)
1085{
1086 int ret;
1087 struct stk_camera *dev = priv;
1088
1089 if (dev == NULL)
1090 return -ENODEV;
1091 if (!is_present(dev))
1092 return -ENODEV;
1093 if (is_streaming(dev))
1094 return -EBUSY;
1095 if (dev->owner && dev->owner != filp)
1096 return -EBUSY;
1097 dev->owner = filp;
1098 ret = stk_vidioc_try_fmt_cap(filp, priv, fmtd);
1099 if (ret)
1100 return ret;
1101
1102 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1103 stk_free_buffers(dev);
1104 dev->frame_size = fmtd->fmt.pix.sizeimage;
1105 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1106
1107 stk_initialise(dev);
1108 /* This registers controls some timings, not sure of what. */
1109 stk_camera_write_reg(dev, 0x001b, 0x0e);
1110 if (dev->vsettings.mode == MODE_SXGA)
1111 stk_camera_write_reg(dev, 0x001c, 0x0e);
1112 else
1113 stk_camera_write_reg(dev, 0x001c, 0x46);
1114 /*
1115 * Registers 0x0115 0x0114 are the size of each line (bytes),
1116 * regs 0x0117 0x0116 are the heigth of the image.
1117 */
1118 stk_camera_write_reg(dev, 0x0115,
1119 (fmtd->fmt.pix.bytesperline >> 8) & 0xff);
1120 stk_camera_write_reg(dev, 0x0114,
1121 fmtd->fmt.pix.bytesperline & 0xff);
1122 stk_camera_write_reg(dev, 0x0117,
1123 (fmtd->fmt.pix.height >> 8) & 0xff);
1124 stk_camera_write_reg(dev, 0x0116,
1125 fmtd->fmt.pix.height & 0xff);
1126 return stk_sensor_configure(dev);
1127}
1128
1129static int stk_vidioc_reqbufs(struct file *filp,
1130 void *priv, struct v4l2_requestbuffers *rb)
1131{
1132 struct stk_camera *dev = priv;
1133
1134 if (dev == NULL)
1135 return -ENODEV;
1136 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1137 return -EINVAL;
1138 if (rb->memory != V4L2_MEMORY_MMAP)
1139 return -EINVAL;
1140 if (is_streaming(dev)
1141 || (dev->owner && dev->owner != filp))
1142 return -EBUSY;
1143 dev->owner = filp;
1144
1145 /*FIXME If they ask for zero, we must stop streaming and free */
1146 if (rb->count < 3)
1147 rb->count = 3;
1148 /* Arbitrary limit */
1149 else if (rb->count > 5)
1150 rb->count = 5;
1151
1152 stk_allocate_buffers(dev, rb->count);
1153 rb->count = dev->n_sbufs;
1154 return 0;
1155}
1156
1157static int stk_vidioc_querybuf(struct file *filp,
1158 void *priv, struct v4l2_buffer *buf)
1159{
1160 int index;
1161 struct stk_camera *dev = priv;
1162 struct stk_sio_buffer *sbuf;
1163
1164 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1165 return -EINVAL;
1166
1167 index = buf->index;
1168
1169 if (index < 0 || index >= dev->n_sbufs)
1170 return -EINVAL;
1171 sbuf = dev->sio_bufs + buf->index;
1172 *buf = sbuf->v4lbuf;
1173 return 0;
1174}
1175
1176static int stk_vidioc_qbuf(struct file *filp,
1177 void *priv, struct v4l2_buffer *buf)
1178{
1179 struct stk_camera *dev = priv;
1180 struct stk_sio_buffer *sbuf;
1181 unsigned long flags;
1182 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1183 return -EINVAL;
1184
1185 if (buf->memory != V4L2_MEMORY_MMAP)
1186 return -EINVAL;
1187
1188 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1189 return -EINVAL;
1190 sbuf = dev->sio_bufs + buf->index;
1191 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1192 return 0;
1193 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1194 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1195 spin_lock_irqsave(&dev->spinlock, flags);
1196 list_add_tail(&sbuf->list, &dev->sio_avail);
1197 *buf = sbuf->v4lbuf;
1198 spin_unlock_irqrestore(&dev->spinlock, flags);
1199 return 0;
1200}
1201
1202static int stk_vidioc_dqbuf(struct file *filp,
1203 void *priv, struct v4l2_buffer *buf)
1204{
1205 struct stk_camera *dev = priv;
1206 struct stk_sio_buffer *sbuf;
1207 unsigned long flags;
1208 int ret;
1209
1210 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1211 || !is_streaming(dev))
1212 return -EINVAL;
1213
1214 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1215 return -EWOULDBLOCK;
1216 ret = wait_event_interruptible(dev->wait_frame,
1217 !list_empty(&dev->sio_full) || !is_present(dev));
1218 if (ret)
1219 return ret;
1220 if (!is_present(dev))
1221 return -EIO;
1222
1223 spin_lock_irqsave(&dev->spinlock, flags);
1224 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1225 list_del_init(&sbuf->list);
1226 spin_unlock_irqrestore(&dev->spinlock, flags);
1227 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1228 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1229 sbuf->v4lbuf.sequence = ++dev->sequence;
1230 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1231
1232 *buf = sbuf->v4lbuf;
1233 return 0;
1234}
1235
1236static int stk_vidioc_streamon(struct file *filp,
1237 void *priv, enum v4l2_buf_type type)
1238{
1239 struct stk_camera *dev = priv;
1240 if (is_streaming(dev))
1241 return 0;
1242 if (dev->sio_bufs == NULL)
1243 return -EINVAL;
1244 dev->sequence = 0;
1245 return stk_start_stream(dev);
1246}
1247
1248static int stk_vidioc_streamoff(struct file *filp,
1249 void *priv, enum v4l2_buf_type type)
1250{
1251 struct stk_camera *dev = priv;
1252 unsigned long flags;
1253 int i;
1254 stk_stop_stream(dev);
1255 spin_lock_irqsave(&dev->spinlock, flags);
1256 INIT_LIST_HEAD(&dev->sio_avail);
1257 INIT_LIST_HEAD(&dev->sio_full);
1258 for (i = 0; i < dev->n_sbufs; i++) {
1259 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1260 dev->sio_bufs[i].v4lbuf.flags = 0;
1261 }
1262 spin_unlock_irqrestore(&dev->spinlock, flags);
1263 return 0;
1264}
1265
1266
1267static int stk_vidioc_g_parm(struct file *filp,
1268 void *priv, struct v4l2_streamparm *sp)
1269{
1270 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1271 return -EINVAL;
1272
1273 sp->parm.capture.capability = 0;
1274 sp->parm.capture.capturemode = 0;
1275 /*FIXME This is not correct */
1276 sp->parm.capture.timeperframe.numerator = 1;
1277 sp->parm.capture.timeperframe.denominator = 30;
1278 sp->parm.capture.readbuffers = 2;
1279 sp->parm.capture.extendedmode = 0;
1280 return 0;
1281}
1282
1283static struct file_operations v4l_stk_fops = {
1284 .owner = THIS_MODULE,
1285 .open = v4l_stk_open,
1286 .release = v4l_stk_release,
1287 .read = v4l_stk_read,
1288 .poll = v4l_stk_poll,
1289 .mmap = v4l_stk_mmap,
1290 .ioctl = video_ioctl2,
1291 .llseek = no_llseek
1292};
1293
1294static void stk_v4l_dev_release(struct video_device *vd)
1295{
1296}
1297
1298static struct video_device stk_v4l_data = {
1299 .name = "stkwebcam",
1300 .type = VFL_TYPE_GRABBER,
1301 .type2 = VID_TYPE_CAPTURE,
1302 .minor = -1,
1303 .tvnorms = V4L2_STD_UNKNOWN,
1304 .current_norm = V4L2_STD_UNKNOWN,
1305 .fops = &v4l_stk_fops,
1306 .release = stk_v4l_dev_release,
1307
1308 .vidioc_querycap = stk_vidioc_querycap,
1309 .vidioc_enum_fmt_cap = stk_vidioc_enum_fmt_cap,
1310 .vidioc_try_fmt_cap = stk_vidioc_try_fmt_cap,
1311 .vidioc_s_fmt_cap = stk_vidioc_s_fmt_cap,
1312 .vidioc_g_fmt_cap = stk_vidioc_g_fmt_cap,
1313 .vidioc_enum_input = stk_vidioc_enum_input,
1314 .vidioc_s_input = stk_vidioc_s_input,
1315 .vidioc_g_input = stk_vidioc_g_input,
1316 .vidioc_s_std = stk_vidioc_s_std,
1317 .vidioc_reqbufs = stk_vidioc_reqbufs,
1318 .vidioc_querybuf = stk_vidioc_querybuf,
1319 .vidioc_qbuf = stk_vidioc_qbuf,
1320 .vidioc_dqbuf = stk_vidioc_dqbuf,
1321 .vidioc_streamon = stk_vidioc_streamon,
1322 .vidioc_streamoff = stk_vidioc_streamoff,
1323 .vidioc_queryctrl = stk_vidioc_queryctrl,
1324 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1325 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1326 .vidioc_g_parm = stk_vidioc_g_parm,
1327};
1328
1329
1330static int stk_register_video_device(struct stk_camera *dev)
1331{
1332 int err;
1333
1334 dev->vdev = stk_v4l_data;
1335 dev->vdev.debug = debug;
1336 dev->vdev.dev = &dev->interface->dev;
1337 dev->vdev.priv = dev;
1338 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1339 if (err)
1340 STK_ERROR("v4l registration failed\n");
1341 else
1342 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1343 " /dev/video%d\n", dev->vdev.minor);
1344 return err;
1345}
1346
1347
1348/* USB Stuff */
1349
1350static int stk_camera_probe(struct usb_interface *interface,
1351 const struct usb_device_id *id)
1352{
1353 int i;
1354 int err;
1355
1356 struct stk_camera *dev = NULL;
1357 struct usb_device *udev = interface_to_usbdev(interface);
1358 struct usb_host_interface *iface_desc;
1359 struct usb_endpoint_descriptor *endpoint;
1360
1361 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1362 if (dev == NULL) {
1363 STK_ERROR("Out of memory !\n");
1364 return -ENOMEM;
1365 }
1366
1367 kref_init(&dev->kref);
1368 spin_lock_init(&dev->spinlock);
1369 init_waitqueue_head(&dev->wait_frame);
1370
1371 dev->udev = udev;
1372 dev->interface = interface;
1373 usb_get_intf(interface);
1374
1375 dev->vsettings.vflip = vflip;
1376 dev->vsettings.hflip = hflip;
1377 dev->n_sbufs = 0;
1378 set_present(dev);
1379
1380 /* Set up the endpoint information
1381 * use only the first isoc-in endpoint
1382 * for the current alternate setting */
1383 iface_desc = interface->cur_altsetting;
1384
1385 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1386 endpoint = &iface_desc->endpoint[i].desc;
1387
1388 if (!dev->isoc_ep
1389 && ((endpoint->bEndpointAddress
1390 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1391 && ((endpoint->bmAttributes
1392 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1393 /* we found an isoc in endpoint */
1394 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1395 break;
1396 }
1397 }
1398 if (!dev->isoc_ep) {
1399 STK_ERROR("Could not find isoc-in endpoint");
1400 kref_put(&dev->kref, stk_camera_cleanup);
1401 return -ENODEV;
1402 }
1403 dev->vsettings.brightness = 0x7fff;
1404 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1405 dev->vsettings.mode = MODE_VGA;
1406 dev->frame_size = 640*480*2;
1407
1408 INIT_LIST_HEAD(&dev->sio_avail);
1409 INIT_LIST_HEAD(&dev->sio_full);
1410
1411 usb_set_intfdata(interface, dev);
1412
1413 err = stk_register_video_device(dev);
1414 if (err) {
1415 kref_put(&dev->kref, stk_camera_cleanup);
1416 return err;
1417 }
1418
1419 stk_create_sysfs_files(&dev->vdev);
1420
1421 return 0;
1422}
1423
1424static void stk_camera_disconnect(struct usb_interface *interface)
1425{
1426 struct stk_camera *dev = usb_get_intfdata(interface);
1427
1428 usb_set_intfdata(interface, NULL);
1429 unset_present(dev);
1430
1431 wake_up_interruptible(&dev->wait_frame);
1432 stk_remove_sysfs_files(&dev->vdev);
1433
1434 kref_put(&dev->kref, stk_camera_cleanup);
1435}
1436
1437static struct usb_driver stk_camera_driver = {
1438 .name = "stkwebcam",
1439 .probe = stk_camera_probe,
1440 .disconnect = stk_camera_disconnect,
1441 .id_table = stkwebcam_table,
1442};
1443
1444
1445static int __init stk_camera_init(void)
1446{
1447 int result;
1448
1449 result = usb_register(&stk_camera_driver);
1450 if (result)
1451 STK_ERROR("usb_register failed ! Error number %d\n", result);
1452
1453
1454 return result;
1455}
1456
1457static void __exit stk_camera_exit(void)
1458{
1459 usb_deregister(&stk_camera_driver);
1460}
1461
1462module_init(stk_camera_init);
1463module_exit(stk_camera_exit);
1464
1465
diff --git a/drivers/media/video/stk-webcam.h b/drivers/media/video/stk-webcam.h
new file mode 100644
index 000000000000..7e989d1ac1e0
--- /dev/null
+++ b/drivers/media/video/stk-webcam.h
@@ -0,0 +1,138 @@
1/*
2 * stk-webcam.h : Driver for Syntek 1125 USB webcam controller
3 *
4 * Copyright (C) 2006 Nicolas VIVIEN
5 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.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 * 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 STKWEBCAM_H
23#define STKWEBCAM_H
24
25#include <linux/usb.h>
26#include <media/v4l2-common.h>
27
28#define DRIVER_VERSION "v0.0.1"
29#define DRIVER_VERSION_NUM 0x000001
30
31#define MAX_ISO_BUFS 3
32#define ISO_FRAMES_PER_DESC 16
33#define ISO_MAX_FRAME_SIZE 3 * 1024
34#define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
35
36
37#define PREFIX "stkwebcam: "
38#define STK_INFO(str, args...) printk(KERN_INFO PREFIX str, ##args)
39#define STK_ERROR(str, args...) printk(KERN_ERR PREFIX str, ##args)
40#define STK_WARNING(str, args...) printk(KERN_WARNING PREFIX str, ##args)
41
42struct stk_iso_buf {
43 void *data;
44 int length;
45 int read;
46 struct urb *urb;
47};
48
49/* Streaming IO buffers */
50struct stk_sio_buffer {
51 struct v4l2_buffer v4lbuf;
52 char *buffer;
53 int mapcount;
54 struct stk_camera *dev;
55 struct list_head list;
56};
57
58enum stk_mode {MODE_VGA, MODE_SXGA, MODE_CIF, MODE_QVGA, MODE_QCIF};
59
60struct stk_video {
61 enum stk_mode mode;
62 int brightness;
63 __u32 palette;
64 int hflip;
65 int vflip;
66};
67
68enum stk_status {
69 S_PRESENT = 1,
70 S_INITIALISED = 2,
71 S_MEMALLOCD = 4,
72 S_STREAMING = 8,
73};
74#define is_present(dev) ((dev)->status & S_PRESENT)
75#define is_initialised(dev) ((dev)->status & S_INITIALISED)
76#define is_streaming(dev) ((dev)->status & S_STREAMING)
77#define is_memallocd(dev) ((dev)->status & S_MEMALLOCD)
78#define set_present(dev) ((dev)->status = S_PRESENT)
79#define unset_present(dev) ((dev)->status &= \
80 ~(S_PRESENT|S_INITIALISED|S_STREAMING))
81#define set_initialised(dev) ((dev)->status |= S_INITIALISED)
82#define set_memallocd(dev) ((dev)->status |= S_MEMALLOCD)
83#define unset_memallocd(dev) ((dev)->status &= ~S_MEMALLOCD)
84#define set_streaming(dev) ((dev)->status |= S_STREAMING)
85#define unset_streaming(dev) ((dev)->status &= ~S_STREAMING)
86
87struct regval {
88 unsigned reg;
89 unsigned val;
90};
91
92struct stk_camera {
93 struct video_device vdev;
94 struct usb_device *udev;
95 struct usb_interface *interface;
96 int webcam_model;
97 struct file *owner;
98
99 u8 isoc_ep;
100
101 struct kref kref;
102 /* Not sure if this is right */
103 atomic_t urbs_used;
104
105 struct stk_video vsettings;
106
107 enum stk_status status;
108
109 spinlock_t spinlock;
110 wait_queue_head_t wait_frame;
111
112 struct stk_iso_buf *isobufs;
113
114 int frame_size;
115 /* Streaming buffers */
116 unsigned int n_sbufs;
117 struct stk_sio_buffer *sio_bufs;
118 struct list_head sio_avail;
119 struct list_head sio_full;
120 unsigned sequence;
121};
122
123#define to_stk_camera(d) container_of(d, struct stk_camera, kref)
124#define vdev_to_camera(d) container_of(d, struct stk_camera, vdev)
125
126void stk_camera_delete(struct kref *);
127int stk_camera_write_reg(struct stk_camera *, u16, u8);
128int stk_camera_read_reg(struct stk_camera *, u16, int *);
129
130int stk_sensor_outb(struct stk_camera *dev, u8 reg, u8 val);
131int stk_sensor_inb(struct stk_camera *dev, u8 reg, u8 *val);
132int stk_sensor_init(struct stk_camera *);
133int stk_sensor_configure(struct stk_camera *);
134int stk_sensor_sleep(struct stk_camera *dev);
135int stk_sensor_wakeup(struct stk_camera *dev);
136int stk_sensor_set_brightness(struct stk_camera *dev, int br);
137
138#endif
diff --git a/drivers/media/video/tda7432.c b/drivers/media/video/tda7432.c
index 43225802a551..b4d10f7a4e57 100644
--- a/drivers/media/video/tda7432.c
+++ b/drivers/media/video/tda7432.c
@@ -8,6 +8,7 @@
8 * Muting and tone control by Jonathan Isom <jisom@ematic.com> 8 * Muting and tone control by Jonathan Isom <jisom@ematic.com>
9 * 9 *
10 * Copyright (c) 2000 Eric Sandeen <eric_sandeen@bigfoot.com> 10 * Copyright (c) 2000 Eric Sandeen <eric_sandeen@bigfoot.com>
11 * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
11 * This code is placed under the terms of the GNU General Public License 12 * This code is placed under the terms of the GNU General Public License
12 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu) 13 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
13 * Which was based on tda8425.c by Greg Alexander (c) 1998 14 * Which was based on tda8425.c by Greg Alexander (c) 1998
@@ -276,7 +277,7 @@ static void do_tda7432_init(struct i2c_client *client)
276 t->volume = 0x3b ; /* -27dB Volume */ 277 t->volume = 0x3b ; /* -27dB Volume */
277 if (loudness) /* Turn loudness on? */ 278 if (loudness) /* Turn loudness on? */
278 t->volume |= TDA7432_LD_ON; 279 t->volume |= TDA7432_LD_ON;
279 t->muted = VIDEO_AUDIO_MUTE; 280 t->muted = 1;
280 t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */ 281 t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */
281 t->bass = TDA7432_BASS_0DB; /* 0dB Bass */ 282 t->bass = TDA7432_BASS_0DB; /* 0dB Bass */
282 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */ 283 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
@@ -332,151 +333,160 @@ static int tda7432_detach(struct i2c_client *client)
332 return 0; 333 return 0;
333} 334}
334 335
335static int tda7432_command(struct i2c_client *client, 336static int tda7432_get_ctrl(struct i2c_client *client,
336 unsigned int cmd, void *arg) 337 struct v4l2_control *ctrl)
337{ 338{
338 struct tda7432 *t = i2c_get_clientdata(client); 339 struct tda7432 *t = i2c_get_clientdata(client);
339 v4l_dbg(2, debug,client,"In tda7432_command\n");
340 if (debug>1)
341 v4l_i2c_print_ioctl(client,cmd);
342 340
343 switch (cmd) { 341 switch (ctrl->id) {
344 /* --- v4l ioctls --- */ 342 case V4L2_CID_AUDIO_MUTE:
345 /* take care: bttv does userspace copying, we'll get a 343 ctrl->value=t->muted;
346 kernel pointer here... */ 344 return 0;
347 345 case V4L2_CID_AUDIO_VOLUME:
348 /* Query card - scale from TDA7432 settings to V4L settings */
349 case VIDIOCGAUDIO:
350 {
351 struct video_audio *va = arg;
352
353 va->flags |= VIDEO_AUDIO_VOLUME |
354 VIDEO_AUDIO_BASS |
355 VIDEO_AUDIO_TREBLE |
356 VIDEO_AUDIO_MUTABLE;
357 if (t->muted)
358 va->flags |= VIDEO_AUDIO_MUTE;
359 va->mode |= VIDEO_SOUND_STEREO;
360 /* Master volume control
361 * V4L volume is min 0, max 65535
362 * TDA7432 Volume:
363 * Min (-79dB) is 0x6f
364 * Max (+20dB) is 0x07 (630)
365 * Max (0dB) is 0x20 (829)
366 * (Mask out bit 7 of vol - it's for the loudness setting)
367 */
368 if (!maxvol){ /* max +20db */ 346 if (!maxvol){ /* max +20db */
369 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 630; 347 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 630;
370 } else { /* max 0db */ 348 } else { /* max 0db */
371 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 829; 349 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 829;
372 } 350 }
373 351 return 0;
374 /* Balance depends on L,R attenuation 352 case V4L2_CID_AUDIO_BALANCE:
375 * V4L balance is 0 to 65535, middle is 32768 353 {
376 * TDA7432 attenuation: min (0dB) is 0, max (-37.5dB) is 0x1f
377 * to scale up to V4L numbers, mult by 1057
378 * attenuation exists for lf, lr, rf, rr
379 * we use only lf and rf (front channels)
380 */
381
382 if ( (t->lf) < (t->rf) ) 354 if ( (t->lf) < (t->rf) )
383 /* right is attenuated, balance shifted left */ 355 /* right is attenuated, balance shifted left */
384 va->balance = (32768 - 1057*(t->rf)); 356 ctrl->value = (32768 - 1057*(t->rf));
385 else 357 else
386 /* left is attenuated, balance shifted right */ 358 /* left is attenuated, balance shifted right */
387 va->balance = (32768 + 1057*(t->lf)); 359 ctrl->value = (32768 + 1057*(t->lf));
388 360 return 0;
361 }
362 case V4L2_CID_AUDIO_BASS:
363 {
389 /* Bass/treble 4 bits each */ 364 /* Bass/treble 4 bits each */
390 va->bass=t->bass; 365 int bass=t->bass;
391 if(va->bass >= 0x8) 366 if(bass >= 0x8)
392 va->bass = ~(va->bass - 0x8) & 0xf; 367 bass = ~(bass - 0x8) & 0xf;
393 va->bass = (va->bass << 12)+(va->bass << 8)+(va->bass << 4)+(va->bass); 368 ctrl->value = (bass << 12)+(bass << 8)+(bass << 4)+(bass);
394 va->treble=t->treble; 369 return 0;
395 if(va->treble >= 0x8)
396 va->treble = ~(va->treble - 0x8) & 0xf;
397 va->treble = (va->treble << 12)+(va->treble << 8)+(va->treble << 4)+(va->treble);
398
399 break; /* VIDIOCGAUDIO case */
400 } 370 }
401 371 case V4L2_CID_AUDIO_TREBLE:
402 /* Set card - scale from V4L settings to TDA7432 settings */
403 case VIDIOCSAUDIO:
404 { 372 {
405 struct video_audio *va = arg; 373 int treble=t->treble;
374 if(treble >= 0x8)
375 treble = ~(treble - 0x8) & 0xf;
376 ctrl->value = (treble << 12)+(treble << 8)+(treble << 4)+(treble);
377 return 0;
378 }
379 }
380 return -EINVAL;
381}
406 382
407 if(va->flags & VIDEO_AUDIO_VOLUME){ 383static int tda7432_set_ctrl(struct i2c_client *client,
408 if(!maxvol){ /* max +20db */ 384 struct v4l2_control *ctrl)
409 t->volume = 0x6f - ((va->volume)/630); 385{
410 } else { /* max 0db */ 386 struct tda7432 *t = i2c_get_clientdata(client);
411 t->volume = 0x6f - ((va->volume)/829);
412 }
413 387
388 switch (ctrl->id) {
389 case V4L2_CID_AUDIO_MUTE:
390 t->muted=ctrl->value;
391 break;
392 case V4L2_CID_AUDIO_VOLUME:
393 if(!maxvol){ /* max +20db */
394 t->volume = 0x6f - ((ctrl->value)/630);
395 } else { /* max 0db */
396 t->volume = 0x6f - ((ctrl->value)/829);
397 }
414 if (loudness) /* Turn on the loudness bit */ 398 if (loudness) /* Turn on the loudness bit */
415 t->volume |= TDA7432_LD_ON; 399 t->volume |= TDA7432_LD_ON;
416 400
417 tda7432_write(client,TDA7432_VL, t->volume); 401 tda7432_write(client,TDA7432_VL, t->volume);
418 } 402 return 0;
419 403 case V4L2_CID_AUDIO_BALANCE:
420 if(va->flags & VIDEO_AUDIO_BASS) 404 if (ctrl->value < 32768) {
421 {
422 t->bass = va->bass >> 12;
423 if(t->bass>= 0x8)
424 t->bass = (~t->bass & 0xf) + 0x8 ;
425 }
426 if(va->flags & VIDEO_AUDIO_TREBLE)
427 {
428 t->treble= va->treble >> 12;
429 if(t->treble>= 0x8)
430 t->treble = (~t->treble & 0xf) + 0x8 ;
431 }
432 if(va->flags & (VIDEO_AUDIO_TREBLE| VIDEO_AUDIO_BASS))
433 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
434
435 if(va->flags & VIDEO_AUDIO_BALANCE) {
436 if (va->balance < 32768)
437 {
438 /* shifted to left, attenuate right */ 405 /* shifted to left, attenuate right */
439 t->rr = (32768 - va->balance)/1057; 406 t->rr = (32768 - ctrl->value)/1057;
440 t->rf = t->rr; 407 t->rf = t->rr;
441 t->lr = TDA7432_ATTEN_0DB; 408 t->lr = TDA7432_ATTEN_0DB;
442 t->lf = TDA7432_ATTEN_0DB; 409 t->lf = TDA7432_ATTEN_0DB;
443 } 410 } else if(ctrl->value > 32769) {
444 else if(va->balance > 32769)
445 {
446 /* shifted to right, attenuate left */ 411 /* shifted to right, attenuate left */
447 t->lf = (va->balance - 32768)/1057; 412 t->lf = (ctrl->value - 32768)/1057;
448 t->lr = t->lf; 413 t->lr = t->lf;
449 t->rr = TDA7432_ATTEN_0DB; 414 t->rr = TDA7432_ATTEN_0DB;
450 t->rf = TDA7432_ATTEN_0DB; 415 t->rf = TDA7432_ATTEN_0DB;
451 } 416 } else {
452 else
453 {
454 /* centered */ 417 /* centered */
455 t->rr = TDA7432_ATTEN_0DB; 418 t->rr = TDA7432_ATTEN_0DB;
456 t->rf = TDA7432_ATTEN_0DB; 419 t->rf = TDA7432_ATTEN_0DB;
457 t->lf = TDA7432_ATTEN_0DB; 420 t->lf = TDA7432_ATTEN_0DB;
458 t->lr = TDA7432_ATTEN_0DB; 421 t->lr = TDA7432_ATTEN_0DB;
459 } 422 }
460 } 423 break;
424 case V4L2_CID_AUDIO_BASS:
425 t->bass = ctrl->value >> 12;
426 if(t->bass>= 0x8)
427 t->bass = (~t->bass & 0xf) + 0x8 ;
428
429 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
430 return 0;
431 case V4L2_CID_AUDIO_TREBLE:
432 t->treble= ctrl->value >> 12;
433 if(t->treble>= 0x8)
434 t->treble = (~t->treble & 0xf) + 0x8 ;
435
436 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
437 return 0;
438 default:
439 return -EINVAL;
440 }
461 441
462 t->muted=(va->flags & VIDEO_AUDIO_MUTE); 442 /* Used for both mute and balance changes */
463 if (t->muted) 443 if (t->muted)
464 { 444 {
465 /* Mute & update balance*/ 445 /* Mute & update balance*/
466 tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE); 446 tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE);
467 tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE); 447 tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE);
468 tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE); 448 tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE);
469 tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE); 449 tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE);
470 } else { 450 } else {
471 tda7432_write(client,TDA7432_LF, t->lf); 451 tda7432_write(client,TDA7432_LF, t->lf);
472 tda7432_write(client,TDA7432_LR, t->lr); 452 tda7432_write(client,TDA7432_LR, t->lr);
473 tda7432_write(client,TDA7432_RF, t->rf); 453 tda7432_write(client,TDA7432_RF, t->rf);
474 tda7432_write(client,TDA7432_RR, t->rr); 454 tda7432_write(client,TDA7432_RR, t->rr);
475 } 455 }
456 return 0;
457}
476 458
477 break; 459static int tda7432_command(struct i2c_client *client,
460 unsigned int cmd, void *arg)
461{
462 v4l_dbg(2, debug,client,"In tda7432_command\n");
463 if (debug>1)
464 v4l_i2c_print_ioctl(client,cmd);
465
466 switch (cmd) {
467 /* --- v4l ioctls --- */
468 /* take care: bttv does userspace copying, we'll get a
469 kernel pointer here... */
470 case VIDIOC_QUERYCTRL:
471 {
472 struct v4l2_queryctrl *qc = arg;
473
474 switch (qc->id) {
475 case V4L2_CID_AUDIO_MUTE:
476 case V4L2_CID_AUDIO_VOLUME:
477 case V4L2_CID_AUDIO_BALANCE:
478 case V4L2_CID_AUDIO_BASS:
479 case V4L2_CID_AUDIO_TREBLE:
480 default:
481 return -EINVAL;
482 }
483 return v4l2_ctrl_query_fill_std(qc);
484 }
485 case VIDIOC_S_CTRL:
486 return tda7432_set_ctrl(client, arg);
478 487
479 } /* end of VIDEOCSAUDIO case */ 488 case VIDIOC_G_CTRL:
489 return tda7432_get_ctrl(client, arg);
480 490
481 } /* end of (cmd) switch */ 491 } /* end of (cmd) switch */
482 492
diff --git a/drivers/media/video/tda8290.c b/drivers/media/video/tda8290.c
index 0e5cf459d3ed..55bc89a6f069 100644
--- a/drivers/media/video/tda8290.c
+++ b/drivers/media/video/tda8290.c
@@ -25,12 +25,14 @@
25#include <linux/videodev.h> 25#include <linux/videodev.h>
26#include "tuner-i2c.h" 26#include "tuner-i2c.h"
27#include "tda8290.h" 27#include "tda8290.h"
28#include "tda827x.h"
29#include "tda18271.h"
28 30
29static int debug = 0; 31static int debug;
30module_param(debug, int, 0644); 32module_param(debug, int, 0644);
31MODULE_PARM_DESC(debug, "enable verbose debug messages"); 33MODULE_PARM_DESC(debug, "enable verbose debug messages");
32 34
33#define PREFIX "tda8290 " 35#define PREFIX "tda8290"
34 36
35/* ---------------------------------------------------------------------- */ 37/* ---------------------------------------------------------------------- */
36 38
@@ -38,345 +40,71 @@ struct tda8290_priv {
38 struct tuner_i2c_props i2c_props; 40 struct tuner_i2c_props i2c_props;
39 41
40 unsigned char tda8290_easy_mode; 42 unsigned char tda8290_easy_mode;
41 unsigned char tda827x_lpsel;
42 unsigned char tda827x_addr;
43 unsigned char tda827x_ver;
44 unsigned int sgIF;
45
46 u32 frequency;
47
48 unsigned int *lna_cfg;
49 int (*tuner_callback) (void *dev, int command,int arg);
50};
51
52/* ---------------------------------------------------------------------- */
53
54struct tda827x_data {
55 u32 lomax;
56 u8 spd;
57 u8 bs;
58 u8 bp;
59 u8 cp;
60 u8 gc3;
61 u8 div1p5;
62};
63
64 /* Note lomax entry is lo / 62500 */
65
66static struct tda827x_data tda827x_analog[] = {
67 { .lomax = 992, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1}, /* 62 MHz */
68 { .lomax = 1056, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1}, /* 66 MHz */
69 { .lomax = 1216, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0}, /* 76 MHz */
70 { .lomax = 1344, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0}, /* 84 MHz */
71 { .lomax = 1488, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 93 MHz */
72 { .lomax = 1568, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 98 MHz */
73 { .lomax = 1744, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 109 MHz */
74 { .lomax = 1968, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 123 MHz */
75 { .lomax = 2128, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 133 MHz */
76 { .lomax = 2416, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 151 MHz */
77 { .lomax = 2464, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 154 MHz */
78 { .lomax = 2896, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 181 MHz */
79 { .lomax = 2960, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 185 MHz */
80 { .lomax = 3472, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 217 MHz */
81 { .lomax = 3904, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 244 MHz */
82 { .lomax = 4240, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 265 MHz */
83 { .lomax = 4832, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 302 MHz */
84 { .lomax = 5184, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 324 MHz */
85 { .lomax = 5920, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 370 MHz */
86 { .lomax = 7264, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 454 MHz */
87 { .lomax = 7888, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 493 MHz */
88 { .lomax = 8480, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 530 MHz */
89 { .lomax = 8864, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 554 MHz */
90 { .lomax = 9664, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 604 MHz */
91 { .lomax = 11088, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 696 MHz */
92 { .lomax = 11840, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0}, /* 740 MHz */
93 { .lomax = 13120, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 820 MHz */
94 { .lomax = 13840, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0}, /* 865 MHz */
95 { .lomax = 0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0} /* End */
96};
97
98static void tda827x_set_analog_params(struct dvb_frontend *fe,
99 struct analog_parameters *params)
100{
101 unsigned char tuner_reg[8];
102 unsigned char reg2[2];
103 u32 N;
104 int i;
105 struct tda8290_priv *priv = fe->tuner_priv;
106 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags = 0};
107 unsigned int freq = params->frequency;
108
109 if (params->mode == V4L2_TUNER_RADIO)
110 freq = freq / 1000;
111
112 N = freq + priv->sgIF;
113 i = 0;
114 while (tda827x_analog[i].lomax < N) {
115 if(tda827x_analog[i + 1].lomax == 0)
116 break;
117 i++;
118 }
119
120 N = N << tda827x_analog[i].spd;
121
122 tuner_reg[0] = 0;
123 tuner_reg[1] = (unsigned char)(N>>8);
124 tuner_reg[2] = (unsigned char) N;
125 tuner_reg[3] = 0x40;
126 tuner_reg[4] = 0x52 + (priv->tda827x_lpsel << 5);
127 tuner_reg[5] = (tda827x_analog[i].spd << 6) + (tda827x_analog[i].div1p5 <<5) +
128 (tda827x_analog[i].bs <<3) + tda827x_analog[i].bp;
129 tuner_reg[6] = 0x8f + (tda827x_analog[i].gc3 << 4);
130 tuner_reg[7] = 0x8f;
131
132 msg.buf = tuner_reg;
133 msg.len = 8;
134 i2c_transfer(priv->i2c_props.adap, &msg, 1);
135
136 msg.buf= reg2;
137 msg.len = 2;
138 reg2[0] = 0x80;
139 reg2[1] = 0;
140 i2c_transfer(priv->i2c_props.adap, &msg, 1);
141
142 reg2[0] = 0x60;
143 reg2[1] = 0xbf;
144 i2c_transfer(priv->i2c_props.adap, &msg, 1);
145
146 reg2[0] = 0x30;
147 reg2[1] = tuner_reg[4] + 0x80;
148 i2c_transfer(priv->i2c_props.adap, &msg, 1);
149
150 msleep(1);
151 reg2[0] = 0x30;
152 reg2[1] = tuner_reg[4] + 4;
153 i2c_transfer(priv->i2c_props.adap, &msg, 1);
154
155 msleep(1);
156 reg2[0] = 0x30;
157 reg2[1] = tuner_reg[4];
158 i2c_transfer(priv->i2c_props.adap, &msg, 1);
159
160 msleep(550);
161 reg2[0] = 0x30;
162 reg2[1] = (tuner_reg[4] & 0xfc) + tda827x_analog[i].cp ;
163 i2c_transfer(priv->i2c_props.adap, &msg, 1);
164
165 reg2[0] = 0x60;
166 reg2[1] = 0x3f;
167 i2c_transfer(priv->i2c_props.adap, &msg, 1);
168
169 reg2[0] = 0x80;
170 reg2[1] = 0x08; // Vsync en
171 i2c_transfer(priv->i2c_props.adap, &msg, 1);
172}
173 43
174static void tda827x_agcf(struct dvb_frontend *fe) 44 unsigned char tda827x_addr;
175{
176 struct tda8290_priv *priv = fe->tuner_priv;
177 unsigned char data[] = {0x80, 0x0c};
178 struct i2c_msg msg = {.addr = priv->tda827x_addr, .buf = data,
179 .flags = 0, .len = 2};
180 i2c_transfer(priv->i2c_props.adap, &msg, 1);
181}
182
183/* ---------------------------------------------------------------------- */
184 45
185struct tda827xa_data { 46 unsigned char ver;
186 u32 lomax; 47#define TDA8290 1
187 u8 svco; 48#define TDA8295 2
188 u8 spd; 49#define TDA8275 4
189 u8 scr; 50#define TDA8275A 8
190 u8 sbs; 51#define TDA18271 16
191 u8 gc3;
192};
193 52
194static struct tda827xa_data tda827xa_analog[] = { 53 struct tda827x_config cfg;
195 { .lomax = 910, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 3}, /* 56.875 MHz */
196 { .lomax = 1076, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3}, /* 67.25 MHz */
197 { .lomax = 1300, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3}, /* 81.25 MHz */
198 { .lomax = 1560, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3}, /* 97.5 MHz */
199 { .lomax = 1820, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1}, /* 113.75 MHz */
200 { .lomax = 2152, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 134.5 MHz */
201 { .lomax = 2464, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 154 MHz */
202 { .lomax = 2600, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 162.5 MHz */
203 { .lomax = 2928, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 183 MHz */
204 { .lomax = 3120, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1}, /* 195 MHz */
205 { .lomax = 3640, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 3}, /* 227.5 MHz */
206 { .lomax = 4304, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 3}, /* 269 MHz */
207 { .lomax = 5200, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1}, /* 325 MHz */
208 { .lomax = 6240, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3}, /* 390 MHz */
209 { .lomax = 7280, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3}, /* 455 MHz */
210 { .lomax = 8320, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1}, /* 520 MHz */
211 { .lomax = 8608, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1}, /* 538 MHz */
212 { .lomax = 8864, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1}, /* 554 MHz */
213 { .lomax = 9920, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0}, /* 620 MHz */
214 { .lomax = 10400, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0}, /* 650 MHz */
215 { .lomax = 11200, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0}, /* 700 MHz */
216 { .lomax = 12480, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0}, /* 780 MHz */
217 { .lomax = 13120, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0}, /* 820 MHz */
218 { .lomax = 13920, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0}, /* 870 MHz */
219 { .lomax = 14576, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0}, /* 911 MHz */
220 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0} /* End */
221}; 54};
222 55
223static void tda827xa_lna_gain(struct dvb_frontend *fe, int high, 56/*---------------------------------------------------------------------*/
224 struct analog_parameters *params)
225{
226 struct tda8290_priv *priv = fe->tuner_priv;
227 unsigned char buf[] = {0x22, 0x01};
228 int arg;
229 struct i2c_msg msg = {.addr = priv->i2c_props.addr, .flags = 0, .buf = buf, .len = sizeof(buf)};
230
231 if ((priv->lna_cfg == NULL) || (priv->tuner_callback == NULL))
232 return;
233
234 if (*priv->lna_cfg) {
235 if (high)
236 tuner_dbg("setting LNA to high gain\n");
237 else
238 tuner_dbg("setting LNA to low gain\n");
239 }
240 switch (*priv->lna_cfg) {
241 case 0: /* no LNA */
242 break;
243 case 1: /* switch is GPIO 0 of tda8290 */
244 case 2:
245 /* turn Vsync on */
246 if (params->std & V4L2_STD_MN)
247 arg = 1;
248 else
249 arg = 0;
250 if (priv->tuner_callback)
251 priv->tuner_callback(priv->i2c_props.adap->algo_data, 1, arg);
252 buf[1] = high ? 0 : 1;
253 if (*priv->lna_cfg == 2)
254 buf[1] = high ? 1 : 0;
255 i2c_transfer(priv->i2c_props.adap, &msg, 1);
256 break;
257 case 3: /* switch with GPIO of saa713x */
258 if (priv->tuner_callback)
259 priv->tuner_callback(priv->i2c_props.adap->algo_data, 0, high);
260 break;
261 }
262}
263 57
264static void tda827xa_set_analog_params(struct dvb_frontend *fe, 58static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
265 struct analog_parameters *params)
266{ 59{
267 unsigned char tuner_reg[11]; 60 struct tda8290_priv *priv = fe->analog_demod_priv;
268 u32 N;
269 int i;
270 struct tda8290_priv *priv = fe->tuner_priv;
271 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags = 0, .buf = tuner_reg};
272 unsigned int freq = params->frequency;
273 61
274 tda827xa_lna_gain(fe, 1, params); 62 unsigned char enable[2] = { 0x21, 0xC0 };
275 msleep(10); 63 unsigned char disable[2] = { 0x21, 0x00 };
276 64 unsigned char *msg;
277 if (params->mode == V4L2_TUNER_RADIO)
278 freq = freq / 1000;
279 65
280 N = freq + priv->sgIF; 66 if (close) {
281 i = 0; 67 msg = enable;
282 while (tda827xa_analog[i].lomax < N) { 68 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
283 if(tda827xa_analog[i + 1].lomax == 0) 69 /* let the bridge stabilize */
284 break; 70 msleep(20);
285 i++; 71 } else {
72 msg = disable;
73 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
286 } 74 }
287 75
288 N = N << tda827xa_analog[i].spd; 76 return 0;
289
290 tuner_reg[0] = 0;
291 tuner_reg[1] = (unsigned char)(N>>8);
292 tuner_reg[2] = (unsigned char) N;
293 tuner_reg[3] = 0;
294 tuner_reg[4] = 0x16;
295 tuner_reg[5] = (tda827xa_analog[i].spd << 5) + (tda827xa_analog[i].svco << 3) +
296 tda827xa_analog[i].sbs;
297 tuner_reg[6] = 0x8b + (tda827xa_analog[i].gc3 << 4);
298 tuner_reg[7] = 0x1c;
299 tuner_reg[8] = 4;
300 tuner_reg[9] = 0x20;
301 tuner_reg[10] = 0x00;
302 msg.len = 11;
303 i2c_transfer(priv->i2c_props.adap, &msg, 1);
304
305 tuner_reg[0] = 0x90;
306 tuner_reg[1] = 0xff;
307 tuner_reg[2] = 0xe0;
308 tuner_reg[3] = 0;
309 tuner_reg[4] = 0x99 + (priv->tda827x_lpsel << 1);
310 msg.len = 5;
311 i2c_transfer(priv->i2c_props.adap, &msg, 1);
312
313 tuner_reg[0] = 0xa0;
314 tuner_reg[1] = 0xc0;
315 msg.len = 2;
316 i2c_transfer(priv->i2c_props.adap, &msg, 1);
317
318 tuner_reg[0] = 0x30;
319 tuner_reg[1] = 0x10 + tda827xa_analog[i].scr;
320 i2c_transfer(priv->i2c_props.adap, &msg, 1);
321
322 msg.flags = I2C_M_RD;
323 i2c_transfer(priv->i2c_props.adap, &msg, 1);
324 msg.flags = 0;
325 tuner_reg[1] >>= 4;
326 tuner_dbg("AGC2 gain is: %d\n", tuner_reg[1]);
327 if (tuner_reg[1] < 1)
328 tda827xa_lna_gain(fe, 0, params);
329
330 msleep(100);
331 tuner_reg[0] = 0x60;
332 tuner_reg[1] = 0x3c;
333 i2c_transfer(priv->i2c_props.adap, &msg, 1);
334
335 msleep(163);
336 tuner_reg[0] = 0x50;
337 tuner_reg[1] = 0x8f + (tda827xa_analog[i].gc3 << 4);
338 i2c_transfer(priv->i2c_props.adap, &msg, 1);
339
340 tuner_reg[0] = 0x80;
341 tuner_reg[1] = 0x28;
342 i2c_transfer(priv->i2c_props.adap, &msg, 1);
343
344 tuner_reg[0] = 0xb0;
345 tuner_reg[1] = 0x01;
346 i2c_transfer(priv->i2c_props.adap, &msg, 1);
347
348 tuner_reg[0] = 0xc0;
349 tuner_reg[1] = 0x19 + (priv->tda827x_lpsel << 1);
350 i2c_transfer(priv->i2c_props.adap, &msg, 1);
351}
352
353static void tda827xa_agcf(struct dvb_frontend *fe)
354{
355 struct tda8290_priv *priv = fe->tuner_priv;
356 unsigned char data[] = {0x80, 0x2c};
357 struct i2c_msg msg = {.addr = priv->tda827x_addr, .buf = data,
358 .flags = 0, .len = 2};
359 i2c_transfer(priv->i2c_props.adap, &msg, 1);
360} 77}
361 78
362/*---------------------------------------------------------------------*/ 79static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
363
364static void tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
365{ 80{
366 struct tda8290_priv *priv = fe->tuner_priv; 81 struct tda8290_priv *priv = fe->analog_demod_priv;
367 82
368 unsigned char enable[2] = { 0x21, 0xC0 }; 83 unsigned char enable[2] = { 0x45, 0xc1 };
369 unsigned char disable[2] = { 0x21, 0x00 }; 84 unsigned char disable[2] = { 0x46, 0x00 };
85 unsigned char buf[3] = { 0x45, 0x01, 0x00 };
370 unsigned char *msg; 86 unsigned char *msg;
371 if(close) { 87
88 if (close) {
372 msg = enable; 89 msg = enable;
373 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2); 90 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
374 /* let the bridge stabilize */ 91 /* let the bridge stabilize */
375 msleep(20); 92 msleep(20);
376 } else { 93 } else {
377 msg = disable; 94 msg = disable;
95 tuner_i2c_xfer_send(&priv->i2c_props, msg, 1);
96 tuner_i2c_xfer_recv(&priv->i2c_props, &msg[1], 1);
97
98 buf[2] = msg[1];
99 buf[2] &= ~0x04;
100 tuner_i2c_xfer_send(&priv->i2c_props, buf, 3);
101 msleep(5);
102
103 msg[1] |= 0x04;
378 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2); 104 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
379 } 105 }
106
107 return 0;
380} 108}
381 109
382/*---------------------------------------------------------------------*/ 110/*---------------------------------------------------------------------*/
@@ -384,55 +112,43 @@ static void tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
384static void set_audio(struct dvb_frontend *fe, 112static void set_audio(struct dvb_frontend *fe,
385 struct analog_parameters *params) 113 struct analog_parameters *params)
386{ 114{
387 struct tda8290_priv *priv = fe->tuner_priv; 115 struct tda8290_priv *priv = fe->analog_demod_priv;
388 char* mode; 116 char* mode;
389 117
390 priv->tda827x_lpsel = 0;
391 if (params->std & V4L2_STD_MN) { 118 if (params->std & V4L2_STD_MN) {
392 priv->sgIF = 92;
393 priv->tda8290_easy_mode = 0x01; 119 priv->tda8290_easy_mode = 0x01;
394 priv->tda827x_lpsel = 1;
395 mode = "MN"; 120 mode = "MN";
396 } else if (params->std & V4L2_STD_B) { 121 } else if (params->std & V4L2_STD_B) {
397 priv->sgIF = 108;
398 priv->tda8290_easy_mode = 0x02; 122 priv->tda8290_easy_mode = 0x02;
399 mode = "B"; 123 mode = "B";
400 } else if (params->std & V4L2_STD_GH) { 124 } else if (params->std & V4L2_STD_GH) {
401 priv->sgIF = 124;
402 priv->tda8290_easy_mode = 0x04; 125 priv->tda8290_easy_mode = 0x04;
403 mode = "GH"; 126 mode = "GH";
404 } else if (params->std & V4L2_STD_PAL_I) { 127 } else if (params->std & V4L2_STD_PAL_I) {
405 priv->sgIF = 124;
406 priv->tda8290_easy_mode = 0x08; 128 priv->tda8290_easy_mode = 0x08;
407 mode = "I"; 129 mode = "I";
408 } else if (params->std & V4L2_STD_DK) { 130 } else if (params->std & V4L2_STD_DK) {
409 priv->sgIF = 124;
410 priv->tda8290_easy_mode = 0x10; 131 priv->tda8290_easy_mode = 0x10;
411 mode = "DK"; 132 mode = "DK";
412 } else if (params->std & V4L2_STD_SECAM_L) { 133 } else if (params->std & V4L2_STD_SECAM_L) {
413 priv->sgIF = 124;
414 priv->tda8290_easy_mode = 0x20; 134 priv->tda8290_easy_mode = 0x20;
415 mode = "L"; 135 mode = "L";
416 } else if (params->std & V4L2_STD_SECAM_LC) { 136 } else if (params->std & V4L2_STD_SECAM_LC) {
417 priv->sgIF = 20;
418 priv->tda8290_easy_mode = 0x40; 137 priv->tda8290_easy_mode = 0x40;
419 mode = "LC"; 138 mode = "LC";
420 } else { 139 } else {
421 priv->sgIF = 124;
422 priv->tda8290_easy_mode = 0x10; 140 priv->tda8290_easy_mode = 0x10;
423 mode = "xx"; 141 mode = "xx";
424 } 142 }
425 143
426 if (params->mode == V4L2_TUNER_RADIO) 144 tuner_dbg("setting tda829x to system %s\n", mode);
427 priv->sgIF = 88; /* if frequency is 5.5 MHz */
428
429 tuner_dbg("setting tda8290 to system %s\n", mode);
430} 145}
431 146
432static int tda8290_set_params(struct dvb_frontend *fe, 147static void tda8290_set_params(struct dvb_frontend *fe,
433 struct analog_parameters *params) 148 struct analog_parameters *params)
434{ 149{
435 struct tda8290_priv *priv = fe->tuner_priv; 150 struct tda8290_priv *priv = fe->analog_demod_priv;
151
436 unsigned char soft_reset[] = { 0x00, 0x00 }; 152 unsigned char soft_reset[] = { 0x00, 0x00 };
437 unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode }; 153 unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode };
438 unsigned char expert_mode[] = { 0x01, 0x80 }; 154 unsigned char expert_mode[] = { 0x01, 0x80 };
@@ -457,8 +173,8 @@ static int tda8290_set_params(struct dvb_frontend *fe,
457 173
458 set_audio(fe, params); 174 set_audio(fe, params);
459 175
460 if (priv->lna_cfg) 176 if (priv->cfg.config)
461 tuner_dbg("tda827xa config is 0x%02x\n", *priv->lna_cfg); 177 tuner_dbg("tda827xa config is 0x%02x\n", *priv->cfg.config);
462 tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2); 178 tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
463 tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2); 179 tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
464 tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2); 180 tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
@@ -475,10 +191,10 @@ static int tda8290_set_params(struct dvb_frontend *fe,
475 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2); 191 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2);
476 192
477 tda8290_i2c_bridge(fe, 1); 193 tda8290_i2c_bridge(fe, 1);
478 if (priv->tda827x_ver != 0) 194
479 tda827xa_set_analog_params(fe, params); 195 if (fe->ops.tuner_ops.set_analog_params)
480 else 196 fe->ops.tuner_ops.set_analog_params(fe, params);
481 tda827x_set_analog_params(fe, params); 197
482 for (i = 0; i < 3; i++) { 198 for (i = 0; i < 3; i++) {
483 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1); 199 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
484 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1); 200 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
@@ -507,10 +223,8 @@ static int tda8290_set_params(struct dvb_frontend *fe,
507 if ((agc_stat > 115) || !(pll_stat & 0x80)) { 223 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
508 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n", 224 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
509 agc_stat, pll_stat & 0x80); 225 agc_stat, pll_stat & 0x80);
510 if (priv->tda827x_ver != 0) 226 if (priv->cfg.agcf)
511 tda827xa_agcf(fe); 227 priv->cfg.agcf(fe);
512 else
513 tda827x_agcf(fe);
514 msleep(100); 228 msleep(100);
515 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1); 229 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
516 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1); 230 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
@@ -541,99 +255,242 @@ static int tda8290_set_params(struct dvb_frontend *fe,
541 255
542 tda8290_i2c_bridge(fe, 0); 256 tda8290_i2c_bridge(fe, 0);
543 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2); 257 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
258}
259
260/*---------------------------------------------------------------------*/
261
262static void tda8295_power(struct dvb_frontend *fe, int enable)
263{
264 struct tda8290_priv *priv = fe->analog_demod_priv;
265 unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
544 266
545 priv->frequency = (V4L2_TUNER_RADIO == params->mode) ? 267 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
546 params->frequency * 125 / 2 : params->frequency * 62500; 268 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
547 269
548 return 0; 270 if (enable)
271 buf[1] = 0x01;
272 else
273 buf[1] = 0x03;
274
275 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
276}
277
278static void tda8295_set_easy_mode(struct dvb_frontend *fe, int enable)
279{
280 struct tda8290_priv *priv = fe->analog_demod_priv;
281 unsigned char buf[] = { 0x01, 0x00 };
282
283 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
284 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
285
286 if (enable)
287 buf[1] = 0x01; /* rising edge sets regs 0x02 - 0x23 */
288 else
289 buf[1] = 0x00; /* reset active bit */
290
291 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
292}
293
294static void tda8295_set_video_std(struct dvb_frontend *fe)
295{
296 struct tda8290_priv *priv = fe->analog_demod_priv;
297 unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
298
299 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
300
301 tda8295_set_easy_mode(fe, 1);
302 msleep(20);
303 tda8295_set_easy_mode(fe, 0);
549} 304}
550 305
551/*---------------------------------------------------------------------*/ 306/*---------------------------------------------------------------------*/
552 307
553static int tda8290_has_signal(struct dvb_frontend *fe) 308static void tda8295_agc1_out(struct dvb_frontend *fe, int enable)
554{ 309{
555 struct tda8290_priv *priv = fe->tuner_priv; 310 struct tda8290_priv *priv = fe->analog_demod_priv;
556 int ret; 311 unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
557 312
558 unsigned char i2c_get_afc[1] = { 0x1B }; 313 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
559 unsigned char afc = 0; 314 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
560 315
561 /* for now, report based on afc status */ 316 if (enable)
562 tuner_i2c_xfer_send(&priv->i2c_props, i2c_get_afc, ARRAY_SIZE(i2c_get_afc)); 317 buf[1] &= ~0x40;
563 tuner_i2c_xfer_recv(&priv->i2c_props, &afc, 1); 318 else
319 buf[1] |= 0x40;
564 320
565 ret = (afc & 0x80) ? 65535 : 0; 321 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
322}
323
324static void tda8295_agc2_out(struct dvb_frontend *fe, int enable)
325{
326 struct tda8290_priv *priv = fe->analog_demod_priv;
327 unsigned char set_gpio_cf[] = { 0x44, 0x00 };
328 unsigned char set_gpio_val[] = { 0x46, 0x00 };
329
330 tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_cf[0], 1);
331 tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_cf[1], 1);
332 tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_val[0], 1);
333 tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_val[1], 1);
566 334
567 tuner_dbg("AFC status: %d\n", ret); 335 set_gpio_cf[1] &= 0xf0; /* clear GPIO_0 bits 3-0 */
568 336
569 return ret; 337 if (enable) {
338 set_gpio_cf[1] |= 0x01; /* config GPIO_0 as Open Drain Out */
339 set_gpio_val[1] &= 0xfe; /* set GPIO_0 pin low */
340 }
341 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_cf, 2);
342 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_val, 2);
570} 343}
571 344
572static int tda8290_get_status(struct dvb_frontend *fe, u32 *status) 345static int tda8295_has_signal(struct dvb_frontend *fe)
573{ 346{
574 *status = 0; 347 struct tda8290_priv *priv = fe->analog_demod_priv;
575 348
576 if (tda8290_has_signal(fe)) 349 unsigned char hvpll_stat = 0x26;
577 *status = TUNER_STATUS_LOCKED; 350 unsigned char ret;
578 351
579 return 0; 352 tuner_i2c_xfer_send(&priv->i2c_props, &hvpll_stat, 1);
353 tuner_i2c_xfer_recv(&priv->i2c_props, &ret, 1);
354 return (ret & 0x01) ? 65535 : 0;
580} 355}
581 356
582static int tda8290_get_rf_strength(struct dvb_frontend *fe, u16 *strength) 357/*---------------------------------------------------------------------*/
358
359static void tda8295_set_params(struct dvb_frontend *fe,
360 struct analog_parameters *params)
583{ 361{
584 *strength = tda8290_has_signal(fe); 362 struct tda8290_priv *priv = fe->analog_demod_priv;
585 363
586 return 0; 364 unsigned char blanking_mode[] = { 0x1d, 0x00 };
365
366 set_audio(fe, params);
367
368 tuner_dbg("%s: freq = %d\n", __FUNCTION__, params->frequency);
369
370 tda8295_power(fe, 1);
371 tda8295_agc1_out(fe, 1);
372
373 tuner_i2c_xfer_send(&priv->i2c_props, &blanking_mode[0], 1);
374 tuner_i2c_xfer_recv(&priv->i2c_props, &blanking_mode[1], 1);
375
376 tda8295_set_video_std(fe);
377
378 blanking_mode[1] = 0x03;
379 tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
380 msleep(20);
381
382 tda8295_i2c_bridge(fe, 1);
383
384 if (fe->ops.tuner_ops.set_analog_params)
385 fe->ops.tuner_ops.set_analog_params(fe, params);
386
387 if (priv->cfg.agcf)
388 priv->cfg.agcf(fe);
389
390 if (tda8295_has_signal(fe))
391 tuner_dbg("tda8295 is locked\n");
392 else
393 tuner_dbg("tda8295 not locked, no signal?\n");
394
395 tda8295_i2c_bridge(fe, 0);
396}
397
398/*---------------------------------------------------------------------*/
399
400static int tda8290_has_signal(struct dvb_frontend *fe)
401{
402 struct tda8290_priv *priv = fe->analog_demod_priv;
403
404 unsigned char i2c_get_afc[1] = { 0x1B };
405 unsigned char afc = 0;
406
407 tuner_i2c_xfer_send(&priv->i2c_props, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
408 tuner_i2c_xfer_recv(&priv->i2c_props, &afc, 1);
409 return (afc & 0x80)? 65535:0;
587} 410}
588 411
589/*---------------------------------------------------------------------*/ 412/*---------------------------------------------------------------------*/
590 413
591static int tda8290_standby(struct dvb_frontend *fe) 414static void tda8290_standby(struct dvb_frontend *fe)
592{ 415{
593 struct tda8290_priv *priv = fe->tuner_priv; 416 struct tda8290_priv *priv = fe->analog_demod_priv;
417
594 unsigned char cb1[] = { 0x30, 0xD0 }; 418 unsigned char cb1[] = { 0x30, 0xD0 };
595 unsigned char tda8290_standby[] = { 0x00, 0x02 }; 419 unsigned char tda8290_standby[] = { 0x00, 0x02 };
596 unsigned char tda8290_agc_tri[] = { 0x02, 0x20 }; 420 unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
597 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2}; 421 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
598 422
599 tda8290_i2c_bridge(fe, 1); 423 tda8290_i2c_bridge(fe, 1);
600 if (priv->tda827x_ver != 0) 424 if (priv->ver & TDA8275A)
601 cb1[1] = 0x90; 425 cb1[1] = 0x90;
602 i2c_transfer(priv->i2c_props.adap, &msg, 1); 426 i2c_transfer(priv->i2c_props.adap, &msg, 1);
603 tda8290_i2c_bridge(fe, 0); 427 tda8290_i2c_bridge(fe, 0);
604 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2); 428 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
605 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2); 429 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
606
607 return 0;
608} 430}
609 431
432static void tda8295_standby(struct dvb_frontend *fe)
433{
434 tda8295_agc1_out(fe, 0); /* Put AGC in tri-state */
435
436 tda8295_power(fe, 0);
437}
610 438
611static void tda8290_init_if(struct dvb_frontend *fe) 439static void tda8290_init_if(struct dvb_frontend *fe)
612{ 440{
613 struct tda8290_priv *priv = fe->tuner_priv; 441 struct tda8290_priv *priv = fe->analog_demod_priv;
614 442
615 unsigned char set_VS[] = { 0x30, 0x6F }; 443 unsigned char set_VS[] = { 0x30, 0x6F };
616 unsigned char set_GP00_CF[] = { 0x20, 0x01 }; 444 unsigned char set_GP00_CF[] = { 0x20, 0x01 };
617 unsigned char set_GP01_CF[] = { 0x20, 0x0B }; 445 unsigned char set_GP01_CF[] = { 0x20, 0x0B };
618 446
619 if ((priv->lna_cfg) && 447 if ((priv->cfg.config) &&
620 ((*priv->lna_cfg == 1) || (*priv->lna_cfg == 2))) 448 ((*priv->cfg.config == 1) || (*priv->cfg.config == 2)))
621 tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2); 449 tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
622 else 450 else
623 tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2); 451 tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
624 tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2); 452 tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
625} 453}
626 454
455static void tda8295_init_if(struct dvb_frontend *fe)
456{
457 struct tda8290_priv *priv = fe->analog_demod_priv;
458
459 static unsigned char set_adc_ctl[] = { 0x33, 0x14 };
460 static unsigned char set_adc_ctl2[] = { 0x34, 0x00 };
461 static unsigned char set_pll_reg6[] = { 0x3e, 0x63 };
462 static unsigned char set_pll_reg0[] = { 0x38, 0x23 };
463 static unsigned char set_pll_reg7[] = { 0x3f, 0x01 };
464 static unsigned char set_pll_reg10[] = { 0x42, 0x61 };
465 static unsigned char set_gpio_reg0[] = { 0x44, 0x0b };
466
467 tda8295_power(fe, 1);
468
469 tda8295_set_easy_mode(fe, 0);
470 tda8295_set_video_std(fe);
471
472 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl, 2);
473 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl2, 2);
474 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg6, 2);
475 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg0, 2);
476 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg7, 2);
477 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg10, 2);
478 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_reg0, 2);
479
480 tda8295_agc1_out(fe, 0);
481 tda8295_agc2_out(fe, 0);
482}
483
627static void tda8290_init_tuner(struct dvb_frontend *fe) 484static void tda8290_init_tuner(struct dvb_frontend *fe)
628{ 485{
629 struct tda8290_priv *priv = fe->tuner_priv; 486 struct tda8290_priv *priv = fe->analog_demod_priv;
630 unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf, 487 unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
631 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 }; 488 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
632 unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b, 489 unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
633 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b }; 490 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
634 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, 491 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
635 .buf=tda8275_init, .len = 14}; 492 .buf=tda8275_init, .len = 14};
636 if (priv->tda827x_ver != 0) 493 if (priv->ver & TDA8275A)
637 msg.buf = tda8275a_init; 494 msg.buf = tda8275a_init;
638 495
639 tda8290_i2c_bridge(fe, 1); 496 tda8290_i2c_bridge(fe, 1);
@@ -643,58 +500,42 @@ static void tda8290_init_tuner(struct dvb_frontend *fe)
643 500
644/*---------------------------------------------------------------------*/ 501/*---------------------------------------------------------------------*/
645 502
646static int tda8290_release(struct dvb_frontend *fe) 503static void tda829x_release(struct dvb_frontend *fe)
647{ 504{
648 kfree(fe->tuner_priv); 505 struct tda8290_priv *priv = fe->analog_demod_priv;
649 fe->tuner_priv = NULL;
650 506
651 return 0; 507 /* only try to release the tuner if we've
652} 508 * attached it from within this module */
509 if (priv->ver & (TDA18271 | TDA8275 | TDA8275A))
510 if (fe->ops.tuner_ops.release)
511 fe->ops.tuner_ops.release(fe);
653 512
654static int tda8290_get_frequency(struct dvb_frontend *fe, u32 *frequency) 513 kfree(fe->analog_demod_priv);
655{ 514 fe->analog_demod_priv = NULL;
656 struct tda8290_priv *priv = fe->tuner_priv;
657 *frequency = priv->frequency;
658 return 0;
659} 515}
660 516
661static struct dvb_tuner_ops tda8290_tuner_ops = { 517static struct tda18271_config tda829x_tda18271_config = {
662 .sleep = tda8290_standby, 518 .gate = TDA18271_GATE_ANALOG,
663 .set_analog_params = tda8290_set_params,
664 .release = tda8290_release,
665 .get_frequency = tda8290_get_frequency,
666 .get_status = tda8290_get_status,
667 .get_rf_strength = tda8290_get_rf_strength,
668}; 519};
669 520
670struct dvb_frontend *tda8290_attach(struct dvb_frontend *fe, 521static int tda829x_find_tuner(struct dvb_frontend *fe)
671 struct i2c_adapter* i2c_adap,
672 u8 i2c_addr,
673 struct tda8290_config *cfg)
674{ 522{
675 struct tda8290_priv *priv = NULL; 523 struct tda8290_priv *priv = fe->analog_demod_priv;
676 u8 data; 524 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
677 int i, ret, tuners_found; 525 int i, ret, tuners_found;
678 u32 tuner_addrs; 526 u32 tuner_addrs;
679 struct i2c_msg msg = {.flags=I2C_M_RD, .buf=&data, .len = 1}; 527 u8 data;
528 struct i2c_msg msg = { .flags = I2C_M_RD, .buf = &data, .len = 1 };
680 529
681 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL); 530 if (NULL == analog_ops->i2c_gate_ctrl)
682 if (priv == NULL) 531 return -EINVAL;
683 return NULL;
684 fe->tuner_priv = priv;
685 532
686 priv->i2c_props.addr = i2c_addr; 533 analog_ops->i2c_gate_ctrl(fe, 1);
687 priv->i2c_props.adap = i2c_adap;
688 if (cfg) {
689 priv->lna_cfg = cfg->lna_cfg;
690 priv->tuner_callback = cfg->tuner_callback;
691 }
692 534
693 tda8290_i2c_bridge(fe, 1);
694 /* probe for tuner chip */ 535 /* probe for tuner chip */
695 tuners_found = 0; 536 tuners_found = 0;
696 tuner_addrs = 0; 537 tuner_addrs = 0;
697 for (i=0x60; i<= 0x63; i++) { 538 for (i = 0x60; i <= 0x63; i++) {
698 msg.addr = i; 539 msg.addr = i;
699 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1); 540 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
700 if (ret == 1) { 541 if (ret == 1) {
@@ -706,20 +547,23 @@ struct dvb_frontend *tda8290_attach(struct dvb_frontend *fe,
706 behind the bridge and we choose the highest address that doesn't 547 behind the bridge and we choose the highest address that doesn't
707 give a response now 548 give a response now
708 */ 549 */
709 tda8290_i2c_bridge(fe, 0); 550
710 if(tuners_found > 1) 551 analog_ops->i2c_gate_ctrl(fe, 0);
552
553 if (tuners_found > 1)
711 for (i = 0; i < tuners_found; i++) { 554 for (i = 0; i < tuners_found; i++) {
712 msg.addr = tuner_addrs & 0xff; 555 msg.addr = tuner_addrs & 0xff;
713 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1); 556 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
714 if(ret == 1) 557 if (ret == 1)
715 tuner_addrs = tuner_addrs >> 8; 558 tuner_addrs = tuner_addrs >> 8;
716 else 559 else
717 break; 560 break;
718 } 561 }
562
719 if (tuner_addrs == 0) { 563 if (tuner_addrs == 0) {
720 tuner_addrs = 0x61; 564 tuner_addrs = 0x60;
721 tuner_info("could not clearly identify tuner address, defaulting to %x\n", 565 tuner_info("could not clearly identify tuner address, "
722 tuner_addrs); 566 "defaulting to %x\n", tuner_addrs);
723 } else { 567 } else {
724 tuner_addrs = tuner_addrs & 0xff; 568 tuner_addrs = tuner_addrs & 0xff;
725 tuner_info("setting tuner address to %x\n", tuner_addrs); 569 tuner_info("setting tuner address to %x\n", tuner_addrs);
@@ -727,42 +571,181 @@ struct dvb_frontend *tda8290_attach(struct dvb_frontend *fe,
727 priv->tda827x_addr = tuner_addrs; 571 priv->tda827x_addr = tuner_addrs;
728 msg.addr = tuner_addrs; 572 msg.addr = tuner_addrs;
729 573
730 tda8290_i2c_bridge(fe, 1); 574 analog_ops->i2c_gate_ctrl(fe, 1);
731 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1); 575 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
732 if( ret != 1) 576
733 tuner_warn("TDA827x access failed!\n"); 577 if (ret != 1) {
734 578 tuner_warn("tuner access failed!\n");
735 memcpy(&fe->ops.tuner_ops, &tda8290_tuner_ops, 579 return -EREMOTEIO;
736 sizeof(struct dvb_tuner_ops)); 580 }
737 581
738 if ((data & 0x3c) == 0) { 582 if ((data == 0x83) || (data == 0x84)) {
739 strlcpy(fe->ops.tuner_ops.info.name, "tda8290+75", 583 priv->ver |= TDA18271;
740 sizeof(fe->ops.tuner_ops.info.name)); 584 tda18271_attach(fe, priv->tda827x_addr,
741 fe->ops.tuner_ops.info.frequency_min = 55000000; 585 priv->i2c_props.adap,
742 fe->ops.tuner_ops.info.frequency_max = 860000000; 586 &tda829x_tda18271_config);
743 fe->ops.tuner_ops.info.frequency_step = 250000;
744 priv->tda827x_ver = 0;
745 } else { 587 } else {
746 strlcpy(fe->ops.tuner_ops.info.name, "tda8290+75a", 588 if ((data & 0x3c) == 0)
747 sizeof(fe->ops.tuner_ops.info.name)); 589 priv->ver |= TDA8275;
748 fe->ops.tuner_ops.info.frequency_min = 44000000; 590 else
749 fe->ops.tuner_ops.info.frequency_max = 906000000; 591 priv->ver |= TDA8275A;
750 fe->ops.tuner_ops.info.frequency_step = 62500; 592
751 priv->tda827x_ver = 2; 593 tda827x_attach(fe, priv->tda827x_addr,
594 priv->i2c_props.adap, &priv->cfg);
595 }
596 if (fe->ops.tuner_ops.init)
597 fe->ops.tuner_ops.init(fe);
598
599 if (fe->ops.tuner_ops.sleep)
600 fe->ops.tuner_ops.sleep(fe);
601
602 analog_ops->i2c_gate_ctrl(fe, 0);
603
604 return 0;
605}
606
607static int tda8290_probe(struct tuner_i2c_props *i2c_props)
608{
609#define TDA8290_ID 0x89
610 unsigned char tda8290_id[] = { 0x1f, 0x00 };
611
612 /* detect tda8290 */
613 tuner_i2c_xfer_send(i2c_props, &tda8290_id[0], 1);
614 tuner_i2c_xfer_recv(i2c_props, &tda8290_id[1], 1);
615
616 if (tda8290_id[1] == TDA8290_ID) {
617 if (debug)
618 printk(KERN_DEBUG "%s: tda8290 detected @ %d-%04x\n",
619 __FUNCTION__, i2c_adapter_id(i2c_props->adap),
620 i2c_props->addr);
621 return 0;
622 }
623
624 return -ENODEV;
625}
626
627static int tda8295_probe(struct tuner_i2c_props *i2c_props)
628{
629#define TDA8295_ID 0x8a
630 unsigned char tda8295_id[] = { 0x2f, 0x00 };
631
632 /* detect tda8295 */
633 tuner_i2c_xfer_send(i2c_props, &tda8295_id[0], 1);
634 tuner_i2c_xfer_recv(i2c_props, &tda8295_id[1], 1);
635
636 if (tda8295_id[1] == TDA8295_ID) {
637 if (debug)
638 printk(KERN_DEBUG "%s: tda8295 detected @ %d-%04x\n",
639 __FUNCTION__, i2c_adapter_id(i2c_props->adap),
640 i2c_props->addr);
641 return 0;
752 } 642 }
753 643
754 priv->tda827x_lpsel = 0; 644 return -ENODEV;
645}
646
647static struct analog_demod_ops tda8290_ops = {
648 .set_params = tda8290_set_params,
649 .has_signal = tda8290_has_signal,
650 .standby = tda8290_standby,
651 .release = tda829x_release,
652 .i2c_gate_ctrl = tda8290_i2c_bridge,
653};
654
655static struct analog_demod_ops tda8295_ops = {
656 .set_params = tda8295_set_params,
657 .has_signal = tda8295_has_signal,
658 .standby = tda8295_standby,
659 .release = tda829x_release,
660 .i2c_gate_ctrl = tda8295_i2c_bridge,
661};
662
663struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
664 struct i2c_adapter *i2c_adap, u8 i2c_addr,
665 struct tda829x_config *cfg)
666{
667 struct tda8290_priv *priv = NULL;
668 char *name;
669
670 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
671 if (priv == NULL)
672 return NULL;
673 fe->analog_demod_priv = priv;
674
675 priv->i2c_props.addr = i2c_addr;
676 priv->i2c_props.adap = i2c_adap;
677 if (cfg) {
678 priv->cfg.config = cfg->lna_cfg;
679 priv->cfg.tuner_callback = cfg->tuner_callback;
680 }
681
682 if (tda8290_probe(&priv->i2c_props) == 0) {
683 priv->ver = TDA8290;
684 memcpy(&fe->ops.analog_ops, &tda8290_ops,
685 sizeof(struct analog_demod_ops));
686 }
687
688 if (tda8295_probe(&priv->i2c_props) == 0) {
689 priv->ver = TDA8295;
690 memcpy(&fe->ops.analog_ops, &tda8295_ops,
691 sizeof(struct analog_demod_ops));
692 }
693
694 if ((!(cfg) || (TDA829X_PROBE_TUNER == cfg->probe_tuner)) &&
695 (tda829x_find_tuner(fe) < 0))
696 goto fail;
697
698 switch (priv->ver) {
699 case TDA8290:
700 name = "tda8290";
701 break;
702 case TDA8295:
703 name = "tda8295";
704 break;
705 case TDA8290 | TDA8275:
706 name = "tda8290+75";
707 break;
708 case TDA8295 | TDA8275:
709 name = "tda8295+75";
710 break;
711 case TDA8290 | TDA8275A:
712 name = "tda8290+75a";
713 break;
714 case TDA8295 | TDA8275A:
715 name = "tda8295+75a";
716 break;
717 case TDA8290 | TDA18271:
718 name = "tda8290+18271";
719 break;
720 case TDA8295 | TDA18271:
721 name = "tda8295+18271";
722 break;
723 default:
724 goto fail;
725 }
726 tuner_info("type set to %s\n", name);
727
728 fe->ops.analog_ops.info.name = name;
729
730 if (priv->ver & TDA8290) {
731 tda8290_init_tuner(fe);
732 tda8290_init_if(fe);
733 } else if (priv->ver & TDA8295)
734 tda8295_init_if(fe);
755 735
756 tda8290_init_tuner(fe);
757 tda8290_init_if(fe);
758 return fe; 736 return fe;
737
738fail:
739 tda829x_release(fe);
740 return NULL;
759} 741}
742EXPORT_SYMBOL_GPL(tda829x_attach);
760 743
761int tda8290_probe(struct i2c_adapter* i2c_adap, u8 i2c_addr) 744int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
762{ 745{
763 struct tuner_i2c_props i2c_props = { 746 struct tuner_i2c_props i2c_props = {
764 .adap = i2c_adap, 747 .adap = i2c_adap,
765 .addr = i2c_addr 748 .addr = i2c_addr,
766 }; 749 };
767 750
768 unsigned char soft_reset[] = { 0x00, 0x00 }; 751 unsigned char soft_reset[] = { 0x00, 0x00 };
@@ -771,7 +754,27 @@ int tda8290_probe(struct i2c_adapter* i2c_adap, u8 i2c_addr)
771 unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 }; 754 unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
772 unsigned char addr_dto_lsb = 0x07; 755 unsigned char addr_dto_lsb = 0x07;
773 unsigned char data; 756 unsigned char data;
757#define PROBE_BUFFER_SIZE 8
758 unsigned char buf[PROBE_BUFFER_SIZE];
759 int i;
760
761 /* rule out tda9887, which would return the same byte repeatedly */
762 tuner_i2c_xfer_send(&i2c_props, soft_reset, 1);
763 tuner_i2c_xfer_recv(&i2c_props, buf, PROBE_BUFFER_SIZE);
764 for (i = 1; i < PROBE_BUFFER_SIZE; i++) {
765 if (buf[i] != buf[0])
766 break;
767 }
774 768
769 /* all bytes are equal, not a tda829x - probably a tda9887 */
770 if (i == PROBE_BUFFER_SIZE)
771 return -ENODEV;
772
773 if ((tda8290_probe(&i2c_props) == 0) ||
774 (tda8295_probe(&i2c_props) == 0))
775 return 0;
776
777 /* fall back to old probing method */
775 tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2); 778 tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
776 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2); 779 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
777 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1); 780 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
@@ -786,14 +789,12 @@ int tda8290_probe(struct i2c_adapter* i2c_adap, u8 i2c_addr)
786 } 789 }
787 } 790 }
788 tuner_i2c_xfer_send(&i2c_props, restore_9886, 3); 791 tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
789 return -1; 792 return -ENODEV;
790} 793}
794EXPORT_SYMBOL_GPL(tda829x_probe);
791 795
792EXPORT_SYMBOL_GPL(tda8290_probe); 796MODULE_DESCRIPTION("Philips/NXP TDA8290/TDA8295 analog IF demodulator driver");
793EXPORT_SYMBOL_GPL(tda8290_attach); 797MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
794
795MODULE_DESCRIPTION("Philips TDA8290 + TDA8275 / TDA8275a tuner driver");
796MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann");
797MODULE_LICENSE("GPL"); 798MODULE_LICENSE("GPL");
798 799
799/* 800/*
diff --git a/drivers/media/video/tda8290.h b/drivers/media/video/tda8290.h
index 107b24b05aa1..dc8ef310b7b2 100644
--- a/drivers/media/video/tda8290.h
+++ b/drivers/media/video/tda8290.h
@@ -20,33 +20,36 @@
20#include <linux/i2c.h> 20#include <linux/i2c.h>
21#include "dvb_frontend.h" 21#include "dvb_frontend.h"
22 22
23struct tda8290_config 23struct tda829x_config {
24{
25 unsigned int *lna_cfg; 24 unsigned int *lna_cfg;
26 int (*tuner_callback) (void *dev, int command,int arg); 25 int (*tuner_callback) (void *dev, int command, int arg);
26
27 unsigned int probe_tuner:1;
28#define TDA829X_PROBE_TUNER 0
29#define TDA829X_DONT_PROBE 1
27}; 30};
28 31
29#if defined(CONFIG_TUNER_TDA8290) || (defined(CONFIG_TUNER_TDA8290_MODULE) && defined(MODULE)) 32#if defined(CONFIG_TUNER_TDA8290) || (defined(CONFIG_TUNER_TDA8290_MODULE) && defined(MODULE))
30extern int tda8290_probe(struct i2c_adapter* i2c_adap, u8 i2c_addr); 33extern int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr);
31 34
32extern struct dvb_frontend *tda8290_attach(struct dvb_frontend *fe, 35extern struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
33 struct i2c_adapter* i2c_adap, 36 struct i2c_adapter *i2c_adap,
34 u8 i2c_addr, 37 u8 i2c_addr,
35 struct tda8290_config *cfg); 38 struct tda829x_config *cfg);
36#else 39#else
37static inline int tda8290_probe(struct i2c_adapter* i2c_adap, u8 i2c_addr) 40static inline int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
38{ 41{
39 printk(KERN_INFO "%s: not probed - driver disabled by Kconfig\n", 42 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __FUNCTION__);
40 __FUNCTION__);
41 return -EINVAL; 43 return -EINVAL;
42} 44}
43 45
44static inline struct dvb_frontend *tda8290_attach(struct dvb_frontend *fe, 46static inline struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
45 struct i2c_adapter* i2c_adap, 47 struct i2c_adapter *i2c_adap,
46 u8 i2c_addr, 48 u8 i2c_addr,
47 struct tda8290_config *cfg) 49 struct tda829x_config *cfg)
48{ 50{
49 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __FUNCTION__); 51 printk(KERN_INFO "%s: not probed - driver disabled by Kconfig\n",
52 __FUNCTION__);
50 return NULL; 53 return NULL;
51} 54}
52#endif 55#endif
diff --git a/drivers/media/video/tda9875.c b/drivers/media/video/tda9875.c
index d11044170872..3c0557130a70 100644
--- a/drivers/media/video/tda9875.c
+++ b/drivers/media/video/tda9875.c
@@ -7,6 +7,7 @@
7 * 7 *
8 * Copyright (c) 2000 Guillaume Delvit based on Gerd Knorr source and 8 * Copyright (c) 2000 Guillaume Delvit based on Gerd Knorr source and
9 * Eric Sandeen 9 * Eric Sandeen
10 * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
10 * This code is placed under the terms of the GNU General Public License 11 * This code is placed under the terms of the GNU General Public License
11 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu) 12 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
12 * Which was based on tda8425.c by Greg Alexander (c) 1998 13 * Which was based on tda8425.c by Greg Alexander (c) 1998
@@ -268,87 +269,143 @@ static int tda9875_detach(struct i2c_client *client)
268 return 0; 269 return 0;
269} 270}
270 271
271static int tda9875_command(struct i2c_client *client, 272static int tda9875_get_ctrl(struct i2c_client *client,
272 unsigned int cmd, void *arg) 273 struct v4l2_control *ctrl)
273{ 274{
274 struct tda9875 *t = i2c_get_clientdata(client); 275 struct tda9875 *t = i2c_get_clientdata(client);
275 276
276 dprintk("In tda9875_command...\n"); 277 switch (ctrl->id) {
278 case V4L2_CID_AUDIO_VOLUME:
279 {
280 int left = (t->lvol+84)*606;
281 int right = (t->rvol+84)*606;
277 282
278 switch (cmd) { 283 ctrl->value=max(left,right);
279 /* --- v4l ioctls --- */ 284 return 0;
280 /* take care: bttv does userspace copying, we'll get a 285 }
281 kernel pointer here... */ 286 case V4L2_CID_AUDIO_BALANCE:
282 case VIDIOCGAUDIO:
283 { 287 {
284 struct video_audio *va = arg; 288 int left = (t->lvol+84)*606;
285 int left,right; 289 int right = (t->rvol+84)*606;
290 int volume = max(left,right);
291 int balance = (32768*min(left,right))/
292 (volume ? volume : 1);
293 ctrl->value=(left<right)?
294 (65535-balance) : balance;
295 return 0;
296 }
297 case V4L2_CID_AUDIO_BASS:
298 ctrl->value = (t->bass+12)*2427; /* min -12 max +15 */
299 return 0;
300 case V4L2_CID_AUDIO_TREBLE:
301 ctrl->value = (t->treble+12)*2730;/* min -12 max +12 */
302 return 0;
303 }
304 return -EINVAL;
305}
286 306
287 dprintk("VIDIOCGAUDIO\n"); 307static int tda9875_set_ctrl(struct i2c_client *client,
308 struct v4l2_control *ctrl)
309{
310 struct tda9875 *t = i2c_get_clientdata(client);
311 int chvol=0, volume, balance, left, right;
288 312
289 va->flags |= VIDEO_AUDIO_VOLUME | 313 switch (ctrl->id) {
290 VIDEO_AUDIO_BASS | 314 case V4L2_CID_AUDIO_VOLUME:
291 VIDEO_AUDIO_TREBLE; 315 left = (t->lvol+84)*606;
316 right = (t->rvol+84)*606;
317
318 volume = max(left,right);
319 balance = (32768*min(left,right))/
320 (volume ? volume : 1);
321 balance =(left<right)?
322 (65535-balance) : balance;
323
324 volume = ctrl->value;
292 325
293 /* min is -84 max is 24 */ 326 chvol=1;
327 break;
328 case V4L2_CID_AUDIO_BALANCE:
294 left = (t->lvol+84)*606; 329 left = (t->lvol+84)*606;
295 right = (t->rvol+84)*606; 330 right = (t->rvol+84)*606;
296 va->volume=max(left,right); 331
297 va->balance=(32768*min(left,right))/ 332 volume=max(left,right);
298 (va->volume ? va->volume : 1); 333
299 va->balance=(left<right)? 334 balance = ctrl->value;
300 (65535-va->balance) : va->balance; 335
301 va->bass = (t->bass+12)*2427; /* min -12 max +15 */ 336 chvol=1;
302 va->treble = (t->treble+12)*2730;/* min -12 max +12 */ 337 break;
303 va->mode |= VIDEO_SOUND_MONO; 338 case V4L2_CID_AUDIO_BASS:
304 339 t->bass = ((ctrl->value/2400)-12) & 0xff;
305 break; /* VIDIOCGAUDIO case */ 340 if (t->bass > 15)
341 t->bass = 15;
342 if (t->bass < -12)
343 t->bass = -12 & 0xff;
344 break;
345 case V4L2_CID_AUDIO_TREBLE:
346 t->treble = ((ctrl->value/2700)-12) & 0xff;
347 if (t->treble > 12)
348 t->treble = 12;
349 if (t->treble < -12)
350 t->treble = -12 & 0xff;
351 break;
352 default:
353 return -EINVAL;
306 } 354 }
307 355
308 case VIDIOCSAUDIO: 356 if (chvol) {
309 { 357 left = (min(65536 - balance,32768) *
310 struct video_audio *va = arg; 358 volume) / 32768;
311 int left,right; 359 right = (min(balance,32768) *
312 360 volume) / 32768;
313 dprintk("VIDEOCSAUDIO...\n");
314 left = (min(65536 - va->balance,32768) *
315 va->volume) / 32768;
316 right = (min(va->balance,(__u16)32768) *
317 va->volume) / 32768;
318 t->lvol = ((left/606)-84) & 0xff; 361 t->lvol = ((left/606)-84) & 0xff;
319 if (t->lvol > 24) 362 if (t->lvol > 24)
320 t->lvol = 24; 363 t->lvol = 24;
321 if (t->lvol < -84) 364 if (t->lvol < -84)
322 t->lvol = -84 & 0xff; 365 t->lvol = -84 & 0xff;
323 366
324 t->rvol = ((right/606)-84) & 0xff; 367 t->rvol = ((right/606)-84) & 0xff;
325 if (t->rvol > 24) 368 if (t->rvol > 24)
326 t->rvol = 24; 369 t->rvol = 24;
327 if (t->rvol < -84) 370 if (t->rvol < -84)
328 t->rvol = -84 & 0xff; 371 t->rvol = -84 & 0xff;
329 372 }
330 t->bass = ((va->bass/2400)-12) & 0xff;
331 if (t->bass > 15)
332 t->bass = 15;
333 if (t->bass < -12)
334 t->bass = -12 & 0xff;
335
336 t->treble = ((va->treble/2700)-12) & 0xff;
337 if (t->treble > 12)
338 t->treble = 12;
339 if (t->treble < -12)
340 t->treble = -12 & 0xff;
341 373
374//printk("tda9875 bal:%04x vol:%04x bass:%04x treble:%04x\n",va->balance,va->volume,va->bass,va->treble);
342 375
376 tda9875_set(client);
343 377
344//printk("tda9875 bal:%04x vol:%04x bass:%04x treble:%04x\n",va->balance,va->volume,va->bass,va->treble); 378 return 0;
379}
345 380
346 381
347 tda9875_set(client); 382static int tda9875_command(struct i2c_client *client,
383 unsigned int cmd, void *arg)
384{
385 dprintk("In tda9875_command...\n");
348 386
349 break; 387 switch (cmd) {
388 /* --- v4l ioctls --- */
389 /* take care: bttv does userspace copying, we'll get a
390 kernel pointer here... */
391 case VIDIOC_QUERYCTRL:
392 {
393 struct v4l2_queryctrl *qc = arg;
394
395 switch (qc->id) {
396 case V4L2_CID_AUDIO_VOLUME:
397 case V4L2_CID_AUDIO_BASS:
398 case V4L2_CID_AUDIO_TREBLE:
399 default:
400 return -EINVAL;
401 }
402 return v4l2_ctrl_query_fill_std(qc);
403 }
404 case VIDIOC_S_CTRL:
405 return tda9875_set_ctrl(client, arg);
350 406
351 } /* end of VIDEOCSAUDIO case */ 407 case VIDIOC_G_CTRL:
408 return tda9875_get_ctrl(client, arg);
352 409
353 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */ 410 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
354 411
diff --git a/drivers/media/video/tda9887.c b/drivers/media/video/tda9887.c
index be5387f11afb..106c93b8203f 100644
--- a/drivers/media/video/tda9887.c
+++ b/drivers/media/video/tda9887.c
@@ -9,7 +9,8 @@
9#include <linux/videodev.h> 9#include <linux/videodev.h>
10#include <media/v4l2-common.h> 10#include <media/v4l2-common.h>
11#include <media/tuner.h> 11#include <media/tuner.h>
12#include "tuner-driver.h" 12#include "tuner-i2c.h"
13#include "tda9887.h"
13 14
14 15
15/* Chips: 16/* Chips:
@@ -20,18 +21,20 @@
20 Used as part of several tuners 21 Used as part of several tuners
21*/ 22*/
22 23
23#define tda9887_info(fmt, arg...) do {\ 24static int debug;
24 printk(KERN_INFO "%s %d-%04x: " fmt, t->i2c.name, \ 25module_param(debug, int, 0644);
25 i2c_adapter_id(t->i2c.adapter), t->i2c.addr , ##arg); } while (0) 26MODULE_PARM_DESC(debug, "enable verbose debug messages");
26#define tda9887_dbg(fmt, arg...) do {\ 27
27 if (tuner_debug) \ 28#define PREFIX "tda9887"
28 printk(KERN_INFO "%s %d-%04x: " fmt, t->i2c.name, \
29 i2c_adapter_id(t->i2c.adapter), t->i2c.addr , ##arg); } while (0)
30 29
31struct tda9887_priv { 30struct tda9887_priv {
32 struct tuner_i2c_props i2c_props; 31 struct tuner_i2c_props i2c_props;
33 32
34 unsigned char data[4]; 33 unsigned char data[4];
34 unsigned int config;
35 unsigned int mode;
36 unsigned int audmode;
37 v4l2_std_id std;
35}; 38};
36 39
37/* ---------------------------------------------------------------------- */ 40/* ---------------------------------------------------------------------- */
@@ -262,8 +265,10 @@ static struct tvnorm radio_mono = {
262 265
263/* ---------------------------------------------------------------------- */ 266/* ---------------------------------------------------------------------- */
264 267
265static void dump_read_message(struct tuner *t, unsigned char *buf) 268static void dump_read_message(struct dvb_frontend *fe, unsigned char *buf)
266{ 269{
270 struct tda9887_priv *priv = fe->analog_demod_priv;
271
267 static char *afc[16] = { 272 static char *afc[16] = {
268 "- 12.5 kHz", 273 "- 12.5 kHz",
269 "- 37.5 kHz", 274 "- 37.5 kHz",
@@ -282,16 +287,18 @@ static void dump_read_message(struct tuner *t, unsigned char *buf)
282 "+ 37.5 kHz", 287 "+ 37.5 kHz",
283 "+ 12.5 kHz", 288 "+ 12.5 kHz",
284 }; 289 };
285 tda9887_info("read: 0x%2x\n", buf[0]); 290 tuner_info("read: 0x%2x\n", buf[0]);
286 tda9887_info(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no"); 291 tuner_info(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
287 tda9887_info(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]); 292 tuner_info(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
288 tda9887_info(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low"); 293 tuner_info(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low");
289 tda9887_info(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out"); 294 tuner_info(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
290 tda9887_info(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low"); 295 tuner_info(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
291} 296}
292 297
293static void dump_write_message(struct tuner *t, unsigned char *buf) 298static void dump_write_message(struct dvb_frontend *fe, unsigned char *buf)
294{ 299{
300 struct tda9887_priv *priv = fe->analog_demod_priv;
301
295 static char *sound[4] = { 302 static char *sound[4] = {
296 "AM/TV", 303 "AM/TV",
297 "FM/radio", 304 "FM/radio",
@@ -330,86 +337,90 @@ static void dump_write_message(struct tuner *t, unsigned char *buf)
330 "44 MHz", 337 "44 MHz",
331 }; 338 };
332 339
333 tda9887_info("write: byte B 0x%02x\n",buf[1]); 340 tuner_info("write: byte B 0x%02x\n", buf[1]);
334 tda9887_info(" B0 video mode : %s\n", 341 tuner_info(" B0 video mode : %s\n",
335 (buf[1] & 0x01) ? "video trap" : "sound trap"); 342 (buf[1] & 0x01) ? "video trap" : "sound trap");
336 tda9887_info(" B1 auto mute fm : %s\n", 343 tuner_info(" B1 auto mute fm : %s\n",
337 (buf[1] & 0x02) ? "yes" : "no"); 344 (buf[1] & 0x02) ? "yes" : "no");
338 tda9887_info(" B2 carrier mode : %s\n", 345 tuner_info(" B2 carrier mode : %s\n",
339 (buf[1] & 0x04) ? "QSS" : "Intercarrier"); 346 (buf[1] & 0x04) ? "QSS" : "Intercarrier");
340 tda9887_info(" B3-4 tv sound/radio : %s\n", 347 tuner_info(" B3-4 tv sound/radio : %s\n",
341 sound[(buf[1] & 0x18) >> 3]); 348 sound[(buf[1] & 0x18) >> 3]);
342 tda9887_info(" B5 force mute audio: %s\n", 349 tuner_info(" B5 force mute audio: %s\n",
343 (buf[1] & 0x20) ? "yes" : "no"); 350 (buf[1] & 0x20) ? "yes" : "no");
344 tda9887_info(" B6 output port 1 : %s\n", 351 tuner_info(" B6 output port 1 : %s\n",
345 (buf[1] & 0x40) ? "high (inactive)" : "low (active)"); 352 (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
346 tda9887_info(" B7 output port 2 : %s\n", 353 tuner_info(" B7 output port 2 : %s\n",
347 (buf[1] & 0x80) ? "high (inactive)" : "low (active)"); 354 (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
348 355
349 tda9887_info("write: byte C 0x%02x\n",buf[2]); 356 tuner_info("write: byte C 0x%02x\n", buf[2]);
350 tda9887_info(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]); 357 tuner_info(" C0-4 top adjustment : %s dB\n",
351 tda9887_info(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]); 358 adjust[buf[2] & 0x1f]);
352 tda9887_info(" C7 audio gain : %s\n", 359 tuner_info(" C5-6 de-emphasis : %s\n",
353 (buf[2] & 0x80) ? "-6" : "0"); 360 deemph[(buf[2] & 0x60) >> 5]);
354 361 tuner_info(" C7 audio gain : %s\n",
355 tda9887_info("write: byte E 0x%02x\n",buf[3]); 362 (buf[2] & 0x80) ? "-6" : "0");
356 tda9887_info(" E0-1 sound carrier : %s\n", 363
357 carrier[(buf[3] & 0x03)]); 364 tuner_info("write: byte E 0x%02x\n", buf[3]);
358 tda9887_info(" E6 l pll gating : %s\n", 365 tuner_info(" E0-1 sound carrier : %s\n",
359 (buf[3] & 0x40) ? "36" : "13"); 366 carrier[(buf[3] & 0x03)]);
367 tuner_info(" E6 l pll gating : %s\n",
368 (buf[3] & 0x40) ? "36" : "13");
360 369
361 if (buf[1] & 0x08) { 370 if (buf[1] & 0x08) {
362 /* radio */ 371 /* radio */
363 tda9887_info(" E2-4 video if : %s\n", 372 tuner_info(" E2-4 video if : %s\n",
364 rif[(buf[3] & 0x0c) >> 2]); 373 rif[(buf[3] & 0x0c) >> 2]);
365 tda9887_info(" E7 vif agc output : %s\n", 374 tuner_info(" E7 vif agc output : %s\n",
366 (buf[3] & 0x80) 375 (buf[3] & 0x80)
367 ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio") 376 ? ((buf[3] & 0x10) ? "fm-agc radio" :
368 : "fm radio carrier afc"); 377 "sif-agc radio")
378 : "fm radio carrier afc");
369 } else { 379 } else {
370 /* video */ 380 /* video */
371 tda9887_info(" E2-4 video if : %s\n", 381 tuner_info(" E2-4 video if : %s\n",
372 vif[(buf[3] & 0x1c) >> 2]); 382 vif[(buf[3] & 0x1c) >> 2]);
373 tda9887_info(" E5 tuner gain : %s\n", 383 tuner_info(" E5 tuner gain : %s\n",
374 (buf[3] & 0x80) 384 (buf[3] & 0x80)
375 ? ((buf[3] & 0x20) ? "external" : "normal") 385 ? ((buf[3] & 0x20) ? "external" : "normal")
376 : ((buf[3] & 0x20) ? "minimum" : "normal")); 386 : ((buf[3] & 0x20) ? "minimum" : "normal"));
377 tda9887_info(" E7 vif agc output : %s\n", 387 tuner_info(" E7 vif agc output : %s\n",
378 (buf[3] & 0x80) 388 (buf[3] & 0x80) ? ((buf[3] & 0x20)
379 ? ((buf[3] & 0x20) 389 ? "pin3 port, pin22 vif agc out"
380 ? "pin3 port, pin22 vif agc out" 390 : "pin22 port, pin3 vif acg ext in")
381 : "pin22 port, pin3 vif acg ext in") 391 : "pin3+pin22 port");
382 : "pin3+pin22 port");
383 } 392 }
384 tda9887_info("--\n"); 393 tuner_info("--\n");
385} 394}
386 395
387/* ---------------------------------------------------------------------- */ 396/* ---------------------------------------------------------------------- */
388 397
389static int tda9887_set_tvnorm(struct tuner *t, char *buf) 398static int tda9887_set_tvnorm(struct dvb_frontend *fe)
390{ 399{
400 struct tda9887_priv *priv = fe->analog_demod_priv;
391 struct tvnorm *norm = NULL; 401 struct tvnorm *norm = NULL;
402 char *buf = priv->data;
392 int i; 403 int i;
393 404
394 if (t->mode == V4L2_TUNER_RADIO) { 405 if (priv->mode == V4L2_TUNER_RADIO) {
395 if (t->audmode == V4L2_TUNER_MODE_MONO) 406 if (priv->audmode == V4L2_TUNER_MODE_MONO)
396 norm = &radio_mono; 407 norm = &radio_mono;
397 else 408 else
398 norm = &radio_stereo; 409 norm = &radio_stereo;
399 } else { 410 } else {
400 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) { 411 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
401 if (tvnorms[i].std & t->std) { 412 if (tvnorms[i].std & priv->std) {
402 norm = tvnorms+i; 413 norm = tvnorms+i;
403 break; 414 break;
404 } 415 }
405 } 416 }
406 } 417 }
407 if (NULL == norm) { 418 if (NULL == norm) {
408 tda9887_dbg("Unsupported tvnorm entry - audio muted\n"); 419 tuner_dbg("Unsupported tvnorm entry - audio muted\n");
409 return -1; 420 return -1;
410 } 421 }
411 422
412 tda9887_dbg("configure for: %s\n",norm->name); 423 tuner_dbg("configure for: %s\n", norm->name);
413 buf[1] = norm->b; 424 buf[1] = norm->b;
414 buf[2] = norm->c; 425 buf[2] = norm->c;
415 buf[3] = norm->e; 426 buf[3] = norm->e;
@@ -426,8 +437,11 @@ module_param(port2, int, 0644);
426module_param(qss, int, 0644); 437module_param(qss, int, 0644);
427module_param(adjust, int, 0644); 438module_param(adjust, int, 0644);
428 439
429static int tda9887_set_insmod(struct tuner *t, char *buf) 440static int tda9887_set_insmod(struct dvb_frontend *fe)
430{ 441{
442 struct tda9887_priv *priv = fe->analog_demod_priv;
443 char *buf = priv->data;
444
431 if (UNSET != port1) { 445 if (UNSET != port1) {
432 if (port1) 446 if (port1)
433 buf[1] |= cOutputPort1Inactive; 447 buf[1] |= cOutputPort1Inactive;
@@ -455,27 +469,30 @@ static int tda9887_set_insmod(struct tuner *t, char *buf)
455 return 0; 469 return 0;
456} 470}
457 471
458static int tda9887_set_config(struct tuner *t, char *buf) 472static int tda9887_do_config(struct dvb_frontend *fe)
459{ 473{
460 if (t->tda9887_config & TDA9887_PORT1_ACTIVE) 474 struct tda9887_priv *priv = fe->analog_demod_priv;
475 char *buf = priv->data;
476
477 if (priv->config & TDA9887_PORT1_ACTIVE)
461 buf[1] &= ~cOutputPort1Inactive; 478 buf[1] &= ~cOutputPort1Inactive;
462 if (t->tda9887_config & TDA9887_PORT1_INACTIVE) 479 if (priv->config & TDA9887_PORT1_INACTIVE)
463 buf[1] |= cOutputPort1Inactive; 480 buf[1] |= cOutputPort1Inactive;
464 if (t->tda9887_config & TDA9887_PORT2_ACTIVE) 481 if (priv->config & TDA9887_PORT2_ACTIVE)
465 buf[1] &= ~cOutputPort2Inactive; 482 buf[1] &= ~cOutputPort2Inactive;
466 if (t->tda9887_config & TDA9887_PORT2_INACTIVE) 483 if (priv->config & TDA9887_PORT2_INACTIVE)
467 buf[1] |= cOutputPort2Inactive; 484 buf[1] |= cOutputPort2Inactive;
468 485
469 if (t->tda9887_config & TDA9887_QSS) 486 if (priv->config & TDA9887_QSS)
470 buf[1] |= cQSS; 487 buf[1] |= cQSS;
471 if (t->tda9887_config & TDA9887_INTERCARRIER) 488 if (priv->config & TDA9887_INTERCARRIER)
472 buf[1] &= ~cQSS; 489 buf[1] &= ~cQSS;
473 490
474 if (t->tda9887_config & TDA9887_AUTOMUTE) 491 if (priv->config & TDA9887_AUTOMUTE)
475 buf[1] |= cAutoMuteFmActive; 492 buf[1] |= cAutoMuteFmActive;
476 if (t->tda9887_config & TDA9887_DEEMPHASIS_MASK) { 493 if (priv->config & TDA9887_DEEMPHASIS_MASK) {
477 buf[2] &= ~0x60; 494 buf[2] &= ~0x60;
478 switch (t->tda9887_config & TDA9887_DEEMPHASIS_MASK) { 495 switch (priv->config & TDA9887_DEEMPHASIS_MASK) {
479 case TDA9887_DEEMPHASIS_NONE: 496 case TDA9887_DEEMPHASIS_NONE:
480 buf[2] |= cDeemphasisOFF; 497 buf[2] |= cDeemphasisOFF;
481 break; 498 break;
@@ -487,21 +504,22 @@ static int tda9887_set_config(struct tuner *t, char *buf)
487 break; 504 break;
488 } 505 }
489 } 506 }
490 if (t->tda9887_config & TDA9887_TOP_SET) { 507 if (priv->config & TDA9887_TOP_SET) {
491 buf[2] &= ~cTopMask; 508 buf[2] &= ~cTopMask;
492 buf[2] |= (t->tda9887_config >> 8) & cTopMask; 509 buf[2] |= (priv->config >> 8) & cTopMask;
493 } 510 }
494 if ((t->tda9887_config & TDA9887_INTERCARRIER_NTSC) && (t->std & V4L2_STD_NTSC)) 511 if ((priv->config & TDA9887_INTERCARRIER_NTSC) &&
512 (priv->std & V4L2_STD_NTSC))
495 buf[1] &= ~cQSS; 513 buf[1] &= ~cQSS;
496 if (t->tda9887_config & TDA9887_GATING_18) 514 if (priv->config & TDA9887_GATING_18)
497 buf[3] &= ~cGating_36; 515 buf[3] &= ~cGating_36;
498 516
499 if (t->mode == V4L2_TUNER_RADIO) { 517 if (priv->mode == V4L2_TUNER_RADIO) {
500 if (t->tda9887_config & TDA9887_RIF_41_3) { 518 if (priv->config & TDA9887_RIF_41_3) {
501 buf[3] &= ~cVideoIFMask; 519 buf[3] &= ~cVideoIFMask;
502 buf[3] |= cRadioIF_41_30; 520 buf[3] |= cRadioIF_41_30;
503 } 521 }
504 if (t->tda9887_config & TDA9887_GAIN_NORMAL) 522 if (priv->config & TDA9887_GAIN_NORMAL)
505 buf[3] &= ~cTunerGainLow; 523 buf[3] &= ~cTunerGainLow;
506 } 524 }
507 525
@@ -510,26 +528,26 @@ static int tda9887_set_config(struct tuner *t, char *buf)
510 528
511/* ---------------------------------------------------------------------- */ 529/* ---------------------------------------------------------------------- */
512 530
513static int tda9887_status(struct tuner *t) 531static int tda9887_status(struct dvb_frontend *fe)
514{ 532{
515 struct tda9887_priv *priv = t->priv; 533 struct tda9887_priv *priv = fe->analog_demod_priv;
516 unsigned char buf[1]; 534 unsigned char buf[1];
517 int rc; 535 int rc;
518 536
519 memset(buf,0,sizeof(buf)); 537 memset(buf,0,sizeof(buf));
520 if (1 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props,buf,1))) 538 if (1 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props,buf,1)))
521 tda9887_info("i2c i/o error: rc == %d (should be 1)\n",rc); 539 tuner_info("i2c i/o error: rc == %d (should be 1)\n", rc);
522 dump_read_message(t, buf); 540 dump_read_message(fe, buf);
523 return 0; 541 return 0;
524} 542}
525 543
526static void tda9887_configure(struct tuner *t) 544static void tda9887_configure(struct dvb_frontend *fe)
527{ 545{
528 struct tda9887_priv *priv = t->priv; 546 struct tda9887_priv *priv = fe->analog_demod_priv;
529 int rc; 547 int rc;
530 548
531 memset(priv->data,0,sizeof(priv->data)); 549 memset(priv->data,0,sizeof(priv->data));
532 tda9887_set_tvnorm(t,priv->data); 550 tda9887_set_tvnorm(fe);
533 551
534 /* A note on the port settings: 552 /* A note on the port settings:
535 These settings tend to depend on the specifics of the board. 553 These settings tend to depend on the specifics of the board.
@@ -547,38 +565,38 @@ static void tda9887_configure(struct tuner *t)
547 priv->data[1] |= cOutputPort1Inactive; 565 priv->data[1] |= cOutputPort1Inactive;
548 priv->data[1] |= cOutputPort2Inactive; 566 priv->data[1] |= cOutputPort2Inactive;
549 567
550 tda9887_set_config(t,priv->data); 568 tda9887_do_config(fe);
551 tda9887_set_insmod(t,priv->data); 569 tda9887_set_insmod(fe);
552 570
553 if (t->mode == T_STANDBY) { 571 if (priv->mode == T_STANDBY)
554 priv->data[1] |= cForcedMuteAudioON; 572 priv->data[1] |= cForcedMuteAudioON;
555 }
556 573
557 tda9887_dbg("writing: b=0x%02x c=0x%02x e=0x%02x\n", 574 tuner_dbg("writing: b=0x%02x c=0x%02x e=0x%02x\n",
558 priv->data[1],priv->data[2],priv->data[3]); 575 priv->data[1], priv->data[2], priv->data[3]);
559 if (tuner_debug > 1) 576 if (debug > 1)
560 dump_write_message(t, priv->data); 577 dump_write_message(fe, priv->data);
561 578
562 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,priv->data,4))) 579 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,priv->data,4)))
563 tda9887_info("i2c i/o error: rc == %d (should be 4)\n",rc); 580 tuner_info("i2c i/o error: rc == %d (should be 4)\n", rc);
564 581
565 if (tuner_debug > 2) { 582 if (debug > 2) {
566 msleep_interruptible(1000); 583 msleep_interruptible(1000);
567 tda9887_status(t); 584 tda9887_status(fe);
568 } 585 }
569} 586}
570 587
571/* ---------------------------------------------------------------------- */ 588/* ---------------------------------------------------------------------- */
572 589
573static void tda9887_tuner_status(struct tuner *t) 590static void tda9887_tuner_status(struct dvb_frontend *fe)
574{ 591{
575 struct tda9887_priv *priv = t->priv; 592 struct tda9887_priv *priv = fe->analog_demod_priv;
576 tda9887_info("Data bytes: b=0x%02x c=0x%02x e=0x%02x\n", priv->data[1], priv->data[2], priv->data[3]); 593 tuner_info("Data bytes: b=0x%02x c=0x%02x e=0x%02x\n",
594 priv->data[1], priv->data[2], priv->data[3]);
577} 595}
578 596
579static int tda9887_get_afc(struct tuner *t) 597static int tda9887_get_afc(struct dvb_frontend *fe)
580{ 598{
581 struct tda9887_priv *priv = t->priv; 599 struct tda9887_priv *priv = fe->analog_demod_priv;
582 static int AFC_BITS_2_kHz[] = { 600 static int AFC_BITS_2_kHz[] = {
583 -12500, -37500, -62500, -97500, 601 -12500, -37500, -62500, -97500,
584 -112500, -137500, -162500, -187500, 602 -112500, -137500, -162500, -187500,
@@ -594,52 +612,79 @@ static int tda9887_get_afc(struct tuner *t)
594 return afc; 612 return afc;
595} 613}
596 614
597static void tda9887_standby(struct tuner *t) 615static void tda9887_standby(struct dvb_frontend *fe)
616{
617 struct tda9887_priv *priv = fe->analog_demod_priv;
618
619 priv->mode = T_STANDBY;
620
621 tda9887_configure(fe);
622}
623
624static void tda9887_set_params(struct dvb_frontend *fe,
625 struct analog_parameters *params)
598{ 626{
599 tda9887_configure(t); 627 struct tda9887_priv *priv = fe->analog_demod_priv;
628
629 priv->mode = params->mode;
630 priv->audmode = params->audmode;
631 priv->std = params->std;
632 tda9887_configure(fe);
600} 633}
601 634
602static void tda9887_set_freq(struct tuner *t, unsigned int freq) 635static int tda9887_set_config(struct dvb_frontend *fe, void *priv_cfg)
603{ 636{
604 tda9887_configure(t); 637 struct tda9887_priv *priv = fe->analog_demod_priv;
638
639 priv->config = *(unsigned int *)priv_cfg;
640 tda9887_configure(fe);
641
642 return 0;
605} 643}
606 644
607static void tda9887_release(struct tuner *t) 645static void tda9887_release(struct dvb_frontend *fe)
608{ 646{
609 kfree(t->priv); 647 kfree(fe->analog_demod_priv);
610 t->priv = NULL; 648 fe->analog_demod_priv = NULL;
611} 649}
612 650
613static struct tuner_operations tda9887_tuner_ops = { 651static struct analog_demod_ops tda9887_ops = {
614 .set_tv_freq = tda9887_set_freq, 652 .info = {
615 .set_radio_freq = tda9887_set_freq, 653 .name = "tda9887",
654 },
655 .set_params = tda9887_set_params,
616 .standby = tda9887_standby, 656 .standby = tda9887_standby,
617 .tuner_status = tda9887_tuner_status, 657 .tuner_status = tda9887_tuner_status,
618 .get_afc = tda9887_get_afc, 658 .get_afc = tda9887_get_afc,
619 .release = tda9887_release, 659 .release = tda9887_release,
660 .set_config = tda9887_set_config,
620}; 661};
621 662
622int tda9887_tuner_init(struct tuner *t) 663struct dvb_frontend *tda9887_attach(struct dvb_frontend *fe,
664 struct i2c_adapter *i2c_adap,
665 u8 i2c_addr)
623{ 666{
624 struct tda9887_priv *priv = NULL; 667 struct tda9887_priv *priv = NULL;
625 668
626 priv = kzalloc(sizeof(struct tda9887_priv), GFP_KERNEL); 669 priv = kzalloc(sizeof(struct tda9887_priv), GFP_KERNEL);
627 if (priv == NULL) 670 if (priv == NULL)
628 return -ENOMEM; 671 return NULL;
629 t->priv = priv; 672 fe->analog_demod_priv = priv;
630
631 priv->i2c_props.addr = t->i2c.addr;
632 priv->i2c_props.adap = t->i2c.adapter;
633 673
634 strlcpy(t->i2c.name, "tda9887", sizeof(t->i2c.name)); 674 priv->i2c_props.addr = i2c_addr;
675 priv->i2c_props.adap = i2c_adap;
676 priv->mode = T_STANDBY;
635 677
636 tda9887_info("tda988[5/6/7] found @ 0x%x (%s)\n", t->i2c.addr, 678 tuner_info("tda988[5/6/7] found\n");
637 t->i2c.driver->driver.name);
638 679
639 memcpy(&t->ops, &tda9887_tuner_ops, sizeof(struct tuner_operations)); 680 memcpy(&fe->ops.analog_ops, &tda9887_ops,
681 sizeof(struct analog_demod_ops));
640 682
641 return 0; 683 return fe;
642} 684}
685EXPORT_SYMBOL_GPL(tda9887_attach);
686
687MODULE_LICENSE("GPL");
643 688
644/* 689/*
645 * Overrides for Emacs so that we follow Linus's tabbing style. 690 * Overrides for Emacs so that we follow Linus's tabbing style.
diff --git a/drivers/media/video/tda9887.h b/drivers/media/video/tda9887.h
new file mode 100644
index 000000000000..8f873a8e6ed2
--- /dev/null
+++ b/drivers/media/video/tda9887.h
@@ -0,0 +1,38 @@
1/*
2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15*/
16
17#ifndef __TDA9887_H__
18#define __TDA9887_H__
19
20#include <linux/i2c.h>
21#include "dvb_frontend.h"
22
23/* ------------------------------------------------------------------------ */
24#if defined(CONFIG_TUNER_TDA9887) || (defined(CONFIG_TUNER_TDA9887_MODULE) && defined(MODULE))
25extern struct dvb_frontend *tda9887_attach(struct dvb_frontend *fe,
26 struct i2c_adapter *i2c_adap,
27 u8 i2c_addr);
28#else
29static inline struct dvb_frontend *tda9887_attach(struct dvb_frontend *fe,
30 struct i2c_adapter *i2c_adap,
31 u8 i2c_addr)
32{
33 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __FUNCTION__);
34 return NULL;
35}
36#endif
37
38#endif /* __TDA9887_H__ */
diff --git a/drivers/media/video/tea5761.c b/drivers/media/video/tea5761.c
index 2150222a3860..5326eeceaacd 100644
--- a/drivers/media/video/tea5761.c
+++ b/drivers/media/video/tea5761.c
@@ -18,7 +18,7 @@ static int debug = 0;
18module_param(debug, int, 0644); 18module_param(debug, int, 0644);
19MODULE_PARM_DESC(debug, "enable verbose debug messages"); 19MODULE_PARM_DESC(debug, "enable verbose debug messages");
20 20
21#define PREFIX "tea5761 " 21#define PREFIX "tea5761"
22 22
23struct tea5761_priv { 23struct tea5761_priv {
24 struct tuner_i2c_props i2c_props; 24 struct tuner_i2c_props i2c_props;
diff --git a/drivers/media/video/tea5767.c b/drivers/media/video/tea5767.c
index 71df419df7bc..e1b48d87e7b7 100644
--- a/drivers/media/video/tea5767.c
+++ b/drivers/media/video/tea5767.c
@@ -20,12 +20,14 @@ static int debug = 0;
20module_param(debug, int, 0644); 20module_param(debug, int, 0644);
21MODULE_PARM_DESC(debug, "enable verbose debug messages"); 21MODULE_PARM_DESC(debug, "enable verbose debug messages");
22 22
23#define PREFIX "tea5767 " 23#define PREFIX "tea5767"
24 24
25struct tea5767_priv { 25/*****************************************************************************/
26 struct tuner_i2c_props i2c_props;
27 26
28 u32 frequency; 27struct tea5767_priv {
28 struct tuner_i2c_props i2c_props;
29 u32 frequency;
30 struct tea5767_ctrl ctrl;
29}; 31};
30 32
31/*****************************************************************************/ 33/*****************************************************************************/
@@ -127,17 +129,10 @@ struct tea5767_priv {
127/* Reserved for future extensions */ 129/* Reserved for future extensions */
128#define TEA5767_RESERVED_MASK 0xff 130#define TEA5767_RESERVED_MASK 0xff
129 131
130enum tea5767_xtal_freq {
131 TEA5767_LOW_LO_32768 = 0,
132 TEA5767_HIGH_LO_32768 = 1,
133 TEA5767_LOW_LO_13MHz = 2,
134 TEA5767_HIGH_LO_13MHz = 3,
135};
136
137
138/*****************************************************************************/ 132/*****************************************************************************/
139 133
140static void tea5767_status_dump(unsigned char *buffer) 134static void tea5767_status_dump(struct tea5767_priv *priv,
135 unsigned char *buffer)
141{ 136{
142 unsigned int div, frq; 137 unsigned int div, frq;
143 138
@@ -153,7 +148,7 @@ static void tea5767_status_dump(unsigned char *buffer)
153 148
154 div = ((buffer[0] & 0x3f) << 8) | buffer[1]; 149 div = ((buffer[0] & 0x3f) << 8) | buffer[1];
155 150
156 switch (TEA5767_HIGH_LO_32768) { 151 switch (priv->ctrl.xtal_freq) {
157 case TEA5767_HIGH_LO_13MHz: 152 case TEA5767_HIGH_LO_13MHz:
158 frq = (div * 50000 - 700000 - 225000) / 4; /* Freq in KHz */ 153 frq = (div * 50000 - 700000 - 225000) / 4; /* Freq in KHz */
159 break; 154 break;
@@ -202,13 +197,10 @@ static int set_radio_freq(struct dvb_frontend *fe,
202 197
203 tuner_dbg("radio freq = %d.%03d MHz\n", frq/16000,(frq/16)%1000); 198 tuner_dbg("radio freq = %d.%03d MHz\n", frq/16000,(frq/16)%1000);
204 199
205 /* Rounds freq to next decimal value - for 62.5 KHz step */ 200 buffer[2] = 0;
206 /* frq = 20*(frq/16)+radio_frq[frq%16]; */
207 201
208 buffer[2] = TEA5767_PORT1_HIGH; 202 if (priv->ctrl.port1)
209 buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL | 203 buffer[2] |= TEA5767_PORT1_HIGH;
210 TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND;
211 buffer[4] = 0;
212 204
213 if (params->audmode == V4L2_TUNER_MODE_MONO) { 205 if (params->audmode == V4L2_TUNER_MODE_MONO) {
214 tuner_dbg("TEA5767 set to mono\n"); 206 tuner_dbg("TEA5767 set to mono\n");
@@ -217,18 +209,45 @@ static int set_radio_freq(struct dvb_frontend *fe,
217 tuner_dbg("TEA5767 set to stereo\n"); 209 tuner_dbg("TEA5767 set to stereo\n");
218 } 210 }
219 211
220 /* Should be replaced */ 212
221 switch (TEA5767_HIGH_LO_32768) { 213 buffer[3] = 0;
214
215 if (priv->ctrl.port2)
216 buffer[3] |= TEA5767_PORT2_HIGH;
217
218 if (priv->ctrl.high_cut)
219 buffer[3] |= TEA5767_HIGH_CUT_CTRL;
220
221 if (priv->ctrl.st_noise)
222 buffer[3] |= TEA5767_ST_NOISE_CTL;
223
224 if (priv->ctrl.soft_mute)
225 buffer[3] |= TEA5767_SOFT_MUTE;
226
227 if (priv->ctrl.japan_band)
228 buffer[3] |= TEA5767_JAPAN_BAND;
229
230 buffer[4] = 0;
231
232 if (priv->ctrl.deemph_75)
233 buffer[4] |= TEA5767_DEEMPH_75;
234
235 if (priv->ctrl.pllref)
236 buffer[4] |= TEA5767_PLLREF_ENABLE;
237
238
239 /* Rounds freq to next decimal value - for 62.5 KHz step */
240 /* frq = 20*(frq/16)+radio_frq[frq%16]; */
241
242 switch (priv->ctrl.xtal_freq) {
222 case TEA5767_HIGH_LO_13MHz: 243 case TEA5767_HIGH_LO_13MHz:
223 tuner_dbg("radio HIGH LO inject xtal @ 13 MHz\n"); 244 tuner_dbg("radio HIGH LO inject xtal @ 13 MHz\n");
224 buffer[2] |= TEA5767_HIGH_LO_INJECT; 245 buffer[2] |= TEA5767_HIGH_LO_INJECT;
225 buffer[4] |= TEA5767_PLLREF_ENABLE;
226 div = (frq * (4000 / 16) + 700000 + 225000 + 25000) / 50000; 246 div = (frq * (4000 / 16) + 700000 + 225000 + 25000) / 50000;
227 break; 247 break;
228 case TEA5767_LOW_LO_13MHz: 248 case TEA5767_LOW_LO_13MHz:
229 tuner_dbg("radio LOW LO inject xtal @ 13 MHz\n"); 249 tuner_dbg("radio LOW LO inject xtal @ 13 MHz\n");
230 250
231 buffer[4] |= TEA5767_PLLREF_ENABLE;
232 div = (frq * (4000 / 16) - 700000 - 225000 + 25000) / 50000; 251 div = (frq * (4000 / 16) - 700000 - 225000 + 25000) / 50000;
233 break; 252 break;
234 case TEA5767_LOW_LO_32768: 253 case TEA5767_LOW_LO_32768:
@@ -256,7 +275,7 @@ static int set_radio_freq(struct dvb_frontend *fe,
256 if (5 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 5))) 275 if (5 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 5)))
257 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc); 276 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
258 else 277 else
259 tea5767_status_dump(buffer); 278 tea5767_status_dump(priv, buffer);
260 } 279 }
261 280
262 priv->frequency = frq * 125 / 2; 281 priv->frequency = frq * 125 / 2;
@@ -382,7 +401,6 @@ int tea5767_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr)
382 return EINVAL; 401 return EINVAL;
383 } 402 }
384 403
385 printk(KERN_WARNING "TEA5767 detected.\n");
386 return 0; 404 return 0;
387} 405}
388 406
@@ -398,6 +416,16 @@ static int tea5767_get_frequency(struct dvb_frontend *fe, u32 *frequency)
398{ 416{
399 struct tea5767_priv *priv = fe->tuner_priv; 417 struct tea5767_priv *priv = fe->tuner_priv;
400 *frequency = priv->frequency; 418 *frequency = priv->frequency;
419
420 return 0;
421}
422
423static int tea5767_set_config (struct dvb_frontend *fe, void *priv_cfg)
424{
425 struct tea5767_priv *priv = fe->tuner_priv;
426
427 memcpy(&priv->ctrl, priv_cfg, sizeof(priv->ctrl));
428
401 return 0; 429 return 0;
402} 430}
403 431
@@ -407,6 +435,7 @@ static struct dvb_tuner_ops tea5767_tuner_ops = {
407 }, 435 },
408 436
409 .set_analog_params = set_radio_freq, 437 .set_analog_params = set_radio_freq,
438 .set_config = tea5767_set_config,
410 .sleep = tea5767_standby, 439 .sleep = tea5767_standby,
411 .release = tea5767_release, 440 .release = tea5767_release,
412 .get_frequency = tea5767_get_frequency, 441 .get_frequency = tea5767_get_frequency,
@@ -425,8 +454,14 @@ struct dvb_frontend *tea5767_attach(struct dvb_frontend *fe,
425 return NULL; 454 return NULL;
426 fe->tuner_priv = priv; 455 fe->tuner_priv = priv;
427 456
428 priv->i2c_props.addr = i2c_addr; 457 priv->i2c_props.addr = i2c_addr;
429 priv->i2c_props.adap = i2c_adap; 458 priv->i2c_props.adap = i2c_adap;
459 priv->ctrl.xtal_freq = TEA5767_HIGH_LO_32768;
460 priv->ctrl.port1 = 1;
461 priv->ctrl.port2 = 1;
462 priv->ctrl.high_cut = 1;
463 priv->ctrl.st_noise = 1;
464 priv->ctrl.japan_band = 1;
430 465
431 memcpy(&fe->ops.tuner_ops, &tea5767_tuner_ops, 466 memcpy(&fe->ops.tuner_ops, &tea5767_tuner_ops,
432 sizeof(struct dvb_tuner_ops)); 467 sizeof(struct dvb_tuner_ops));
@@ -436,7 +471,6 @@ struct dvb_frontend *tea5767_attach(struct dvb_frontend *fe,
436 return fe; 471 return fe;
437} 472}
438 473
439
440EXPORT_SYMBOL_GPL(tea5767_attach); 474EXPORT_SYMBOL_GPL(tea5767_attach);
441EXPORT_SYMBOL_GPL(tea5767_autodetection); 475EXPORT_SYMBOL_GPL(tea5767_autodetection);
442 476
diff --git a/drivers/media/video/tea5767.h b/drivers/media/video/tea5767.h
index 5d78281adcc2..a44451f61145 100644
--- a/drivers/media/video/tea5767.h
+++ b/drivers/media/video/tea5767.h
@@ -20,6 +20,25 @@
20#include <linux/i2c.h> 20#include <linux/i2c.h>
21#include "dvb_frontend.h" 21#include "dvb_frontend.h"
22 22
23enum tea5767_xtal {
24 TEA5767_LOW_LO_32768 = 0,
25 TEA5767_HIGH_LO_32768 = 1,
26 TEA5767_LOW_LO_13MHz = 2,
27 TEA5767_HIGH_LO_13MHz = 3,
28};
29
30struct tea5767_ctrl {
31 unsigned int port1:1;
32 unsigned int port2:1;
33 unsigned int high_cut:1;
34 unsigned int st_noise:1;
35 unsigned int soft_mute:1;
36 unsigned int japan_band:1;
37 unsigned int deemph_75:1;
38 unsigned int pllref:1;
39 enum tea5767_xtal xtal_freq;
40};
41
23#if defined(CONFIG_TUNER_TEA5767) || (defined(CONFIG_TUNER_TEA5767_MODULE) && defined(MODULE)) 42#if defined(CONFIG_TUNER_TEA5767) || (defined(CONFIG_TUNER_TEA5767_MODULE) && defined(MODULE))
24extern int tea5767_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr); 43extern int tea5767_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr);
25 44
diff --git a/drivers/media/video/tlv320aic23b.c b/drivers/media/video/tlv320aic23b.c
index 76b2e96429d9..dc7b9c220b90 100644
--- a/drivers/media/video/tlv320aic23b.c
+++ b/drivers/media/video/tlv320aic23b.c
@@ -31,6 +31,7 @@
31#include <linux/i2c-id.h> 31#include <linux/i2c-id.h>
32#include <linux/videodev.h> 32#include <linux/videodev.h>
33#include <media/v4l2-common.h> 33#include <media/v4l2-common.h>
34#include <media/v4l2-i2c-drv-legacy.h>
34 35
35MODULE_DESCRIPTION("tlv320aic23b driver"); 36MODULE_DESCRIPTION("tlv320aic23b driver");
36MODULE_AUTHOR("Scott Alfter, Ulf Eklund, Hans Verkuil"); 37MODULE_AUTHOR("Scott Alfter, Ulf Eklund, Hans Verkuil");
@@ -56,37 +57,35 @@ static int tlv320aic23b_write(struct i2c_client *client, int reg, u16 val)
56 return -1; 57 return -1;
57 } 58 }
58 59
59 for (i = 0; i < 3; i++) { 60 for (i = 0; i < 3; i++)
60 if (i2c_smbus_write_byte_data(client, (reg << 1) | 61 if (i2c_smbus_write_byte_data(client,
61 (val >> 8), val & 0xff) == 0) { 62 (reg << 1) | (val >> 8), val & 0xff) == 0)
62 return 0; 63 return 0;
63 }
64 }
65 v4l_err(client, "I2C: cannot write %03x to register R%d\n", val, reg); 64 v4l_err(client, "I2C: cannot write %03x to register R%d\n", val, reg);
66 return -1; 65 return -1;
67} 66}
68 67
69static int tlv320aic23b_command(struct i2c_client *client, unsigned int cmd, 68static int tlv320aic23b_command(struct i2c_client *client,
70 void *arg) 69 unsigned int cmd, void *arg)
71{ 70{
72 struct tlv320aic23b_state *state = i2c_get_clientdata(client); 71 struct tlv320aic23b_state *state = i2c_get_clientdata(client);
73 struct v4l2_control *ctrl = arg; 72 struct v4l2_control *ctrl = arg;
74 u32* freq = arg; 73 u32 *freq = arg;
75 74
76 switch (cmd) { 75 switch (cmd) {
77 case VIDIOC_INT_AUDIO_CLOCK_FREQ: 76 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
78 switch (*freq) { 77 switch (*freq) {
79 case 32000: /* set sample rate to 32 kHz */ 78 case 32000: /* set sample rate to 32 kHz */
80 tlv320aic23b_write(client, 8, 0x018); 79 tlv320aic23b_write(client, 8, 0x018);
81 break; 80 break;
82 case 44100: /* set sample rate to 44.1 kHz */ 81 case 44100: /* set sample rate to 44.1 kHz */
83 tlv320aic23b_write(client, 8, 0x022); 82 tlv320aic23b_write(client, 8, 0x022);
84 break; 83 break;
85 case 48000: /* set sample rate to 48 kHz */ 84 case 48000: /* set sample rate to 48 kHz */
86 tlv320aic23b_write(client, 8, 0x000); 85 tlv320aic23b_write(client, 8, 0x000);
87 break; 86 break;
88 default: 87 default:
89 return -EINVAL; 88 return -EINVAL;
90 } 89 }
91 break; 90 break;
92 91
@@ -126,92 +125,53 @@ static int tlv320aic23b_command(struct i2c_client *client, unsigned int cmd,
126 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1' 125 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
127 */ 126 */
128 127
129static struct i2c_driver i2c_driver; 128static int tlv320aic23b_probe(struct i2c_client *client)
130
131static int tlv320aic23b_attach(struct i2c_adapter *adapter, int address, int kind)
132{ 129{
133 struct i2c_client *client;
134 struct tlv320aic23b_state *state; 130 struct tlv320aic23b_state *state;
135 131
136 /* Check if the adapter supports the needed features */ 132 /* Check if the adapter supports the needed features */
137 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 133 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
138 return 0; 134 return -EIO;
139
140 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
141 if (client == 0)
142 return -ENOMEM;
143
144 client->addr = address;
145 client->adapter = adapter;
146 client->driver = &i2c_driver;
147 snprintf(client->name, sizeof(client->name) - 1, "tlv320aic23b");
148 135
149 v4l_info(client, "chip found @ 0x%x (%s)\n", address << 1, adapter->name); 136 v4l_info(client, "chip found @ 0x%x (%s)\n",
137 client->addr << 1, client->adapter->name);
150 138
151 state = kmalloc(sizeof(struct tlv320aic23b_state), GFP_KERNEL); 139 state = kmalloc(sizeof(struct tlv320aic23b_state), GFP_KERNEL);
152 if (state == NULL) { 140 if (state == NULL)
153 kfree(client);
154 return -ENOMEM; 141 return -ENOMEM;
155 }
156 state->muted = 0; 142 state->muted = 0;
157 i2c_set_clientdata(client, state); 143 i2c_set_clientdata(client, state);
158 144
159 /* initialize tlv320aic23b */ 145 /* Initialize tlv320aic23b */
160 tlv320aic23b_write(client, 15, 0x000); /* RESET */ 146
161 tlv320aic23b_write(client, 6, 0x00A); /* turn off DAC & mic input */ 147 /* RESET */
162 tlv320aic23b_write(client, 7, 0x049); /* left-justified, 24-bit, master mode */ 148 tlv320aic23b_write(client, 15, 0x000);
163 tlv320aic23b_write(client, 0, 0x119); /* set gain on both channels to +3.0 dB */ 149 /* turn off DAC & mic input */
164 tlv320aic23b_write(client, 8, 0x000); /* set sample rate to 48 kHz */ 150 tlv320aic23b_write(client, 6, 0x00A);
165 tlv320aic23b_write(client, 9, 0x001); /* activate digital interface */ 151 /* left-justified, 24-bit, master mode */
166 152 tlv320aic23b_write(client, 7, 0x049);
167 i2c_attach_client(client); 153 /* set gain on both channels to +3.0 dB */
168 154 tlv320aic23b_write(client, 0, 0x119);
155 /* set sample rate to 48 kHz */
156 tlv320aic23b_write(client, 8, 0x000);
157 /* activate digital interface */
158 tlv320aic23b_write(client, 9, 0x001);
169 return 0; 159 return 0;
170} 160}
171 161
172static int tlv320aic23b_probe(struct i2c_adapter *adapter) 162static int tlv320aic23b_remove(struct i2c_client *client)
173{ 163{
174 if (adapter->class & I2C_CLASS_TV_ANALOG) 164 kfree(i2c_get_clientdata(client));
175 return i2c_probe(adapter, &addr_data, tlv320aic23b_attach);
176 return 0;
177}
178
179static int tlv320aic23b_detach(struct i2c_client *client)
180{
181 int err;
182
183 err = i2c_detach_client(client);
184 if (err) {
185 return err;
186 }
187 kfree(client);
188
189 return 0; 165 return 0;
190} 166}
191 167
192/* ----------------------------------------------------------------------- */ 168/* ----------------------------------------------------------------------- */
193 169
194/* i2c implementation */
195static struct i2c_driver i2c_driver = {
196 .driver = {
197 .name = "tlv320aic23b",
198 },
199 .id = I2C_DRIVERID_TLV320AIC23B,
200 .attach_adapter = tlv320aic23b_probe,
201 .detach_client = tlv320aic23b_detach,
202 .command = tlv320aic23b_command,
203};
204
205 170
206static int __init tlv320aic23b_init_module(void) 171static struct v4l2_i2c_driver_data v4l2_i2c_data = {
207{ 172 .name = "tlv320aic23b",
208 return i2c_add_driver(&i2c_driver); 173 .driverid = I2C_DRIVERID_TLV320AIC23B,
209} 174 .command = tlv320aic23b_command,
210 175 .probe = tlv320aic23b_probe,
211static void __exit tlv320aic23b_cleanup_module(void) 176 .remove = tlv320aic23b_remove,
212{ 177};
213 i2c_del_driver(&i2c_driver);
214}
215
216module_init(tlv320aic23b_init_module);
217module_exit(tlv320aic23b_cleanup_module);
diff --git a/drivers/media/video/tuner-core.c b/drivers/media/video/tuner-core.c
index 9e99f3636d3d..ba538f6fbcc3 100644
--- a/drivers/media/video/tuner-core.c
+++ b/drivers/media/video/tuner-core.c
@@ -19,15 +19,41 @@
19#include <media/tuner.h> 19#include <media/tuner.h>
20#include <media/tuner-types.h> 20#include <media/tuner-types.h>
21#include <media/v4l2-common.h> 21#include <media/v4l2-common.h>
22#include "tuner-driver.h" 22#include <media/v4l2-i2c-drv-legacy.h>
23#include "mt20xx.h" 23#include "mt20xx.h"
24#include "tda8290.h" 24#include "tda8290.h"
25#include "tea5761.h" 25#include "tea5761.h"
26#include "tea5767.h" 26#include "tea5767.h"
27#include "tuner-xc2028.h"
27#include "tuner-simple.h" 28#include "tuner-simple.h"
29#include "tda9887.h"
30#include "xc5000.h"
28 31
29#define UNSET (-1U) 32#define UNSET (-1U)
30 33
34#define PREFIX t->i2c->driver->driver.name
35
36struct tuner {
37 /* device */
38 struct dvb_frontend fe;
39 struct i2c_client *i2c;
40 struct list_head list;
41 unsigned int using_v4l2:1;
42
43 /* keep track of the current settings */
44 v4l2_std_id std;
45 unsigned int tv_freq;
46 unsigned int radio_freq;
47 unsigned int audmode;
48
49 unsigned int mode;
50 unsigned int mode_mask; /* Combination of allowable modes */
51
52 unsigned int type; /* chip type id */
53 unsigned int config;
54 int (*tuner_callback) (void *dev, int command, int arg);
55};
56
31/* standard i2c insmod options */ 57/* standard i2c insmod options */
32static unsigned short normal_i2c[] = { 58static unsigned short normal_i2c[] = {
33#if defined(CONFIG_TUNER_TEA5761) || (defined(CONFIG_TUNER_TEA5761_MODULE) && defined(MODULE)) 59#if defined(CONFIG_TUNER_TEA5761) || (defined(CONFIG_TUNER_TEA5761_MODULE) && defined(MODULE))
@@ -47,7 +73,34 @@ static unsigned int no_autodetect = 0;
47static unsigned int show_i2c = 0; 73static unsigned int show_i2c = 0;
48 74
49/* insmod options used at runtime => read/write */ 75/* insmod options used at runtime => read/write */
50int tuner_debug = 0; 76static int tuner_debug;
77
78#define tuner_warn(fmt, arg...) do { \
79 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
80 i2c_adapter_id(t->i2c->adapter), \
81 t->i2c->addr, ##arg); \
82 } while (0)
83
84#define tuner_info(fmt, arg...) do { \
85 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
86 i2c_adapter_id(t->i2c->adapter), \
87 t->i2c->addr, ##arg); \
88 } while (0)
89
90#define tuner_err(fmt, arg...) do { \
91 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
92 i2c_adapter_id(t->i2c->adapter), \
93 t->i2c->addr, ##arg); \
94 } while (0)
95
96#define tuner_dbg(fmt, arg...) do { \
97 if (tuner_debug) \
98 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
99 i2c_adapter_id(t->i2c->adapter), \
100 t->i2c->addr, ##arg); \
101 } while (0)
102
103/* ------------------------------------------------------------------------ */
51 104
52static unsigned int tv_range[2] = { 44, 958 }; 105static unsigned int tv_range[2] = { 44, 958 };
53static unsigned int radio_range[2] = { 65, 108 }; 106static unsigned int radio_range[2] = { 65, 108 };
@@ -71,66 +124,96 @@ MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
71MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer"); 124MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
72MODULE_LICENSE("GPL"); 125MODULE_LICENSE("GPL");
73 126
74static struct i2c_driver driver;
75static struct i2c_client client_template;
76
77/* ---------------------------------------------------------------------- */ 127/* ---------------------------------------------------------------------- */
78 128
79static void fe_set_freq(struct tuner *t, unsigned int freq) 129static void fe_set_params(struct dvb_frontend *fe,
130 struct analog_parameters *params)
80{ 131{
81 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; 132 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
82 133 struct tuner *t = fe->analog_demod_priv;
83 struct analog_parameters params = {
84 .frequency = freq,
85 .mode = t->mode,
86 .audmode = t->audmode,
87 .std = t->std
88 };
89 134
90 if (NULL == fe_tuner_ops->set_analog_params) { 135 if (NULL == fe_tuner_ops->set_analog_params) {
91 tuner_warn("Tuner frontend module has no way to set freq\n"); 136 tuner_warn("Tuner frontend module has no way to set freq\n");
92 return; 137 return;
93 } 138 }
94 fe_tuner_ops->set_analog_params(&t->fe, &params); 139 fe_tuner_ops->set_analog_params(fe, params);
95} 140}
96 141
97static void fe_release(struct tuner *t) 142static void fe_release(struct dvb_frontend *fe)
98{ 143{
99 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; 144 if (fe->ops.tuner_ops.release)
100 145 fe->ops.tuner_ops.release(fe);
101 if (fe_tuner_ops->release) 146
102 fe_tuner_ops->release(&t->fe); 147 /* DO NOT kfree(fe->analog_demod_priv)
148 *
149 * If we are in this function, analog_demod_priv contains a pointer
150 * to struct tuner *t. This will be kfree'd in tuner_detach().
151 *
152 * Otherwise, fe->ops.analog_demod_ops->release will
153 * handle the cleanup for analog demodulator modules.
154 */
155 fe->analog_demod_priv = NULL;
103} 156}
104 157
105static void fe_standby(struct tuner *t) 158static void fe_standby(struct dvb_frontend *fe)
106{ 159{
107 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; 160 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
108 161
109 if (fe_tuner_ops->sleep) 162 if (fe_tuner_ops->sleep)
110 fe_tuner_ops->sleep(&t->fe); 163 fe_tuner_ops->sleep(fe);
111} 164}
112 165
113static int fe_has_signal(struct tuner *t) 166static int fe_has_signal(struct dvb_frontend *fe)
114{ 167{
115 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
116 u16 strength = 0; 168 u16 strength = 0;
117 169
118 if (fe_tuner_ops->get_rf_strength) 170 if (fe->ops.tuner_ops.get_rf_strength)
119 fe_tuner_ops->get_rf_strength(&t->fe, &strength); 171 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
120 172
121 return strength; 173 return strength;
122} 174}
123 175
176static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
177{
178 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
179 struct tuner *t = fe->analog_demod_priv;
180
181 if (fe_tuner_ops->set_config)
182 return fe_tuner_ops->set_config(fe, priv_cfg);
183
184 tuner_warn("Tuner frontend module has no way to set config\n");
185
186 return 0;
187}
188
189static void tuner_status(struct dvb_frontend *fe);
190
191static struct analog_demod_ops tuner_core_ops = {
192 .set_params = fe_set_params,
193 .standby = fe_standby,
194 .release = fe_release,
195 .has_signal = fe_has_signal,
196 .set_config = fe_set_config,
197 .tuner_status = tuner_status
198};
199
124/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */ 200/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
125static void set_tv_freq(struct i2c_client *c, unsigned int freq) 201static void set_tv_freq(struct i2c_client *c, unsigned int freq)
126{ 202{
127 struct tuner *t = i2c_get_clientdata(c); 203 struct tuner *t = i2c_get_clientdata(c);
204 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
205
206 struct analog_parameters params = {
207 .mode = t->mode,
208 .audmode = t->audmode,
209 .std = t->std
210 };
128 211
129 if (t->type == UNSET) { 212 if (t->type == UNSET) {
130 tuner_warn ("tuner type not set\n"); 213 tuner_warn ("tuner type not set\n");
131 return; 214 return;
132 } 215 }
133 if (NULL == t->ops.set_tv_freq) { 216 if (NULL == analog_ops->set_params) {
134 tuner_warn ("Tuner has no way to set tv freq\n"); 217 tuner_warn ("Tuner has no way to set tv freq\n");
135 return; 218 return;
136 } 219 }
@@ -145,18 +228,27 @@ static void set_tv_freq(struct i2c_client *c, unsigned int freq)
145 else 228 else
146 freq = tv_range[1] * 16; 229 freq = tv_range[1] * 16;
147 } 230 }
148 t->ops.set_tv_freq(t, freq); 231 params.frequency = freq;
232
233 analog_ops->set_params(&t->fe, &params);
149} 234}
150 235
151static void set_radio_freq(struct i2c_client *c, unsigned int freq) 236static void set_radio_freq(struct i2c_client *c, unsigned int freq)
152{ 237{
153 struct tuner *t = i2c_get_clientdata(c); 238 struct tuner *t = i2c_get_clientdata(c);
239 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
240
241 struct analog_parameters params = {
242 .mode = t->mode,
243 .audmode = t->audmode,
244 .std = t->std
245 };
154 246
155 if (t->type == UNSET) { 247 if (t->type == UNSET) {
156 tuner_warn ("tuner type not set\n"); 248 tuner_warn ("tuner type not set\n");
157 return; 249 return;
158 } 250 }
159 if (NULL == t->ops.set_radio_freq) { 251 if (NULL == analog_ops->set_params) {
160 tuner_warn ("tuner has no way to set radio frequency\n"); 252 tuner_warn ("tuner has no way to set radio frequency\n");
161 return; 253 return;
162 } 254 }
@@ -171,8 +263,9 @@ static void set_radio_freq(struct i2c_client *c, unsigned int freq)
171 else 263 else
172 freq = radio_range[1] * 16000; 264 freq = radio_range[1] * 16000;
173 } 265 }
266 params.frequency = freq;
174 267
175 t->ops.set_radio_freq(t, freq); 268 analog_ops->set_params(&t->fe, &params);
176} 269}
177 270
178static void set_freq(struct i2c_client *c, unsigned long freq) 271static void set_freq(struct i2c_client *c, unsigned long freq)
@@ -193,54 +286,65 @@ static void set_freq(struct i2c_client *c, unsigned long freq)
193 set_tv_freq(c, freq); 286 set_tv_freq(c, freq);
194 t->tv_freq = freq; 287 t->tv_freq = freq;
195 break; 288 break;
289 default:
290 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode);
196 } 291 }
197} 292}
198 293
199static void tuner_i2c_address_check(struct tuner *t) 294static void tuner_i2c_address_check(struct tuner *t)
200{ 295{
201 if ((t->type == UNSET || t->type == TUNER_ABSENT) || 296 if ((t->type == UNSET || t->type == TUNER_ABSENT) ||
202 ((t->i2c.addr < 0x64) || (t->i2c.addr > 0x6f))) 297 ((t->i2c->addr < 0x64) || (t->i2c->addr > 0x6f)))
298 return;
299
300 /* We already know that the XC5000 can only be located at
301 * i2c address 0x61, 0x62, 0x63 or 0x64 */
302 if ((t->type == TUNER_XC5000) &&
303 ((t->i2c->addr <= 0x64)) && (t->i2c->addr >= 0x61))
203 return; 304 return;
204 305
205 tuner_warn("====================== WARNING! ======================\n"); 306 tuner_warn("====================== WARNING! ======================\n");
206 tuner_warn("Support for tuners in i2c address range 0x64 thru 0x6f\n"); 307 tuner_warn("Support for tuners in i2c address range 0x64 thru 0x6f\n");
207 tuner_warn("will soon be dropped. This message indicates that your\n"); 308 tuner_warn("will soon be dropped. This message indicates that your\n");
208 tuner_warn("hardware has a %s tuner at i2c address 0x%02x.\n", 309 tuner_warn("hardware has a %s tuner at i2c address 0x%02x.\n",
209 t->i2c.name, t->i2c.addr); 310 t->i2c->name, t->i2c->addr);
210 tuner_warn("To ensure continued support for your device, please\n"); 311 tuner_warn("To ensure continued support for your device, please\n");
211 tuner_warn("send a copy of this message, along with full dmesg\n"); 312 tuner_warn("send a copy of this message, along with full dmesg\n");
212 tuner_warn("output to v4l-dvb-maintainer@linuxtv.org\n"); 313 tuner_warn("output to v4l-dvb-maintainer@linuxtv.org\n");
213 tuner_warn("Please use subject line: \"obsolete tuner i2c address.\"\n"); 314 tuner_warn("Please use subject line: \"obsolete tuner i2c address.\"\n");
214 tuner_warn("driver: %s, addr: 0x%02x, type: %d (%s)\n", 315 tuner_warn("driver: %s, addr: 0x%02x, type: %d (%s)\n",
215 t->i2c.adapter->name, t->i2c.addr, t->type, 316 t->i2c->adapter->name, t->i2c->addr, t->type,
216 tuners[t->type].name); 317 tuners[t->type].name);
217 tuner_warn("====================== WARNING! ======================\n"); 318 tuner_warn("====================== WARNING! ======================\n");
218} 319}
219 320
220static void attach_tda8290(struct tuner *t)
221{
222 struct tda8290_config cfg = {
223 .lna_cfg = &t->config,
224 .tuner_callback = t->tuner_callback
225 };
226 tda8290_attach(&t->fe, t->i2c.adapter, t->i2c.addr, &cfg);
227}
228
229static void attach_simple_tuner(struct tuner *t) 321static void attach_simple_tuner(struct tuner *t)
230{ 322{
231 struct simple_tuner_config cfg = { 323 struct simple_tuner_config cfg = {
232 .type = t->type, 324 .type = t->type,
233 .tun = &tuners[t->type] 325 .tun = &tuners[t->type]
234 }; 326 };
235 simple_tuner_attach(&t->fe, t->i2c.adapter, t->i2c.addr, &cfg); 327 simple_tuner_attach(&t->fe, t->i2c->adapter, t->i2c->addr, &cfg);
236} 328}
237 329
330static void attach_tda829x(struct tuner *t)
331{
332 struct tda829x_config cfg = {
333 .lna_cfg = &t->config,
334 .tuner_callback = t->tuner_callback,
335 };
336 tda829x_attach(&t->fe, t->i2c->adapter, t->i2c->addr, &cfg);
337}
338
339static struct xc5000_config xc5000_cfg;
340
238static void set_type(struct i2c_client *c, unsigned int type, 341static void set_type(struct i2c_client *c, unsigned int type,
239 unsigned int new_mode_mask, unsigned int new_config, 342 unsigned int new_mode_mask, unsigned int new_config,
240 int (*tuner_callback) (void *dev, int command,int arg)) 343 int (*tuner_callback) (void *dev, int command,int arg))
241{ 344{
242 struct tuner *t = i2c_get_clientdata(c); 345 struct tuner *t = i2c_get_clientdata(c);
243 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; 346 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
347 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
244 unsigned char buffer[4]; 348 unsigned char buffer[4];
245 349
246 if (type == UNSET || type == TUNER_ABSENT) { 350 if (type == UNSET || type == TUNER_ABSENT) {
@@ -260,32 +364,27 @@ static void set_type(struct i2c_client *c, unsigned int type,
260 t->tuner_callback = tuner_callback; 364 t->tuner_callback = tuner_callback;
261 } 365 }
262 366
263 /* This code detects calls by card attach_inform */ 367 if (t->mode == T_UNINITIALIZED) {
264 if (NULL == t->i2c.dev.driver) {
265 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr); 368 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
266 369
267 return; 370 return;
268 } 371 }
269 372
270 /* discard private data, in case set_type() was previously called */ 373 /* discard private data, in case set_type() was previously called */
271 if (t->ops.release) 374 if (analog_ops->release)
272 t->ops.release(t); 375 analog_ops->release(&t->fe);
273 else {
274 kfree(t->priv);
275 t->priv = NULL;
276 }
277 376
278 switch (t->type) { 377 switch (t->type) {
279 case TUNER_MT2032: 378 case TUNER_MT2032:
280 microtune_attach(&t->fe, t->i2c.adapter, t->i2c.addr); 379 microtune_attach(&t->fe, t->i2c->adapter, t->i2c->addr);
281 break; 380 break;
282 case TUNER_PHILIPS_TDA8290: 381 case TUNER_PHILIPS_TDA8290:
283 { 382 {
284 attach_tda8290(t); 383 attach_tda829x(t);
285 break; 384 break;
286 } 385 }
287 case TUNER_TEA5767: 386 case TUNER_TEA5767:
288 if (tea5767_attach(&t->fe, t->i2c.adapter, t->i2c.addr) == NULL) { 387 if (tea5767_attach(&t->fe, t->i2c->adapter, t->i2c->addr) == NULL) {
289 t->type = TUNER_ABSENT; 388 t->type = TUNER_ABSENT;
290 t->mode_mask = T_UNINITIALIZED; 389 t->mode_mask = T_UNINITIALIZED;
291 return; 390 return;
@@ -293,7 +392,7 @@ static void set_type(struct i2c_client *c, unsigned int type,
293 t->mode_mask = T_RADIO; 392 t->mode_mask = T_RADIO;
294 break; 393 break;
295 case TUNER_TEA5761: 394 case TUNER_TEA5761:
296 if (tea5761_attach(&t->fe, t->i2c.adapter, t->i2c.addr) == NULL) { 395 if (tea5761_attach(&t->fe, t->i2c->adapter, t->i2c->addr) == NULL) {
297 t->type = TUNER_ABSENT; 396 t->type = TUNER_ABSENT;
298 t->mode_mask = T_UNINITIALIZED; 397 t->mode_mask = T_UNINITIALIZED;
299 return; 398 return;
@@ -320,25 +419,60 @@ static void set_type(struct i2c_client *c, unsigned int type,
320 i2c_master_send(c,buffer,4); 419 i2c_master_send(c,buffer,4);
321 attach_simple_tuner(t); 420 attach_simple_tuner(t);
322 break; 421 break;
422 case TUNER_XC2028:
423 {
424 struct xc2028_config cfg = {
425 .i2c_adap = t->i2c->adapter,
426 .i2c_addr = t->i2c->addr,
427 .video_dev = c->adapter->algo_data,
428 .callback = t->tuner_callback,
429 };
430 if (!xc2028_attach(&t->fe, &cfg)) {
431 t->type = TUNER_ABSENT;
432 t->mode_mask = T_UNINITIALIZED;
433 return;
434 }
435 break;
436 }
323 case TUNER_TDA9887: 437 case TUNER_TDA9887:
324 tda9887_tuner_init(t); 438 tda9887_attach(&t->fe, t->i2c->adapter, t->i2c->addr);
439 break;
440 case TUNER_XC5000:
441 xc5000_cfg.i2c_address = t->i2c->addr;
442 xc5000_cfg.if_khz = 5380;
443 xc5000_cfg.priv = c->adapter->algo_data;
444 xc5000_cfg.tuner_callback = t->tuner_callback;
445 if (!xc5000_attach(&t->fe, t->i2c->adapter, &xc5000_cfg)) {
446 t->type = TUNER_ABSENT;
447 t->mode_mask = T_UNINITIALIZED;
448 return;
449 }
450 {
451 struct dvb_tuner_ops *xc_tuner_ops;
452 xc_tuner_ops = &t->fe.ops.tuner_ops;
453 if(xc_tuner_ops->init != NULL)
454 xc_tuner_ops->init(&t->fe);
455 }
325 break; 456 break;
326 default: 457 default:
327 attach_simple_tuner(t); 458 attach_simple_tuner(t);
328 break; 459 break;
329 } 460 }
330 461
331 if (fe_tuner_ops->set_analog_params) { 462 if ((NULL == analog_ops->set_params) &&
332 strlcpy(t->i2c.name, fe_tuner_ops->info.name, sizeof(t->i2c.name)); 463 (fe_tuner_ops->set_analog_params)) {
464 strlcpy(t->i2c->name, fe_tuner_ops->info.name,
465 sizeof(t->i2c->name));
333 466
334 t->ops.set_tv_freq = fe_set_freq; 467 t->fe.analog_demod_priv = t;
335 t->ops.set_radio_freq = fe_set_freq; 468 memcpy(analog_ops, &tuner_core_ops,
336 t->ops.standby = fe_standby; 469 sizeof(struct analog_demod_ops));
337 t->ops.release = fe_release; 470 } else {
338 t->ops.has_signal = fe_has_signal; 471 strlcpy(t->i2c->name, analog_ops->info.name,
472 sizeof(t->i2c->name));
339 } 473 }
340 474
341 tuner_info("type set to %s\n", t->i2c.name); 475 tuner_dbg("type set to %s\n", t->i2c->name);
342 476
343 if (t->mode_mask == T_UNINITIALIZED) 477 if (t->mode_mask == T_UNINITIALIZED)
344 t->mode_mask = new_mode_mask; 478 t->mode_mask = new_mode_mask;
@@ -508,10 +642,12 @@ static int tuner_fixup_std(struct tuner *t)
508 return 0; 642 return 0;
509} 643}
510 644
511static void tuner_status(struct tuner *t) 645static void tuner_status(struct dvb_frontend *fe)
512{ 646{
647 struct tuner *t = fe->analog_demod_priv;
513 unsigned long freq, freq_fraction; 648 unsigned long freq, freq_fraction;
514 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; 649 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
650 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
515 const char *p; 651 const char *p;
516 652
517 switch (t->mode) { 653 switch (t->mode) {
@@ -541,172 +677,16 @@ static void tuner_status(struct tuner *t)
541 if (tuner_status & TUNER_STATUS_STEREO) 677 if (tuner_status & TUNER_STATUS_STEREO)
542 tuner_info("Stereo: yes\n"); 678 tuner_info("Stereo: yes\n");
543 } 679 }
544 if (t->ops.has_signal) { 680 if (analog_ops->has_signal)
545 tuner_info("Signal strength: %d\n", t->ops.has_signal(t)); 681 tuner_info("Signal strength: %d\n",
546 } 682 analog_ops->has_signal(fe));
547 if (t->ops.is_stereo) { 683 if (analog_ops->is_stereo)
548 tuner_info("Stereo: %s\n", t->ops.is_stereo(t) ? "yes" : "no"); 684 tuner_info("Stereo: %s\n",
549 } 685 analog_ops->is_stereo(fe) ? "yes" : "no");
550} 686}
551 687
552/* ---------------------------------------------------------------------- */ 688/* ---------------------------------------------------------------------- */
553 689
554/* static vars: used only in tuner_attach and tuner_probe */
555static unsigned default_mode_mask;
556
557/* During client attach, set_type is called by adapter's attach_inform callback.
558 set_type must then be completed by tuner_attach.
559 */
560static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
561{
562 struct tuner *t;
563
564 client_template.adapter = adap;
565 client_template.addr = addr;
566
567 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
568 if (NULL == t)
569 return -ENOMEM;
570 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
571 i2c_set_clientdata(&t->i2c, t);
572 t->type = UNSET;
573 t->audmode = V4L2_TUNER_MODE_STEREO;
574 t->mode_mask = T_UNINITIALIZED;
575 t->ops.tuner_status = tuner_status;
576
577 if (show_i2c) {
578 unsigned char buffer[16];
579 int i,rc;
580
581 memset(buffer, 0, sizeof(buffer));
582 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
583 tuner_info("I2C RECV = ");
584 for (i=0;i<rc;i++)
585 printk("%02x ",buffer[i]);
586 printk("\n");
587 }
588 /* HACK: This test were added to avoid tuner to probe tda9840 and tea6415c on the MXB card */
589 if (adap->id == I2C_HW_SAA7146 && addr < 0x4a)
590 return -ENODEV;
591
592 /* autodetection code based on the i2c addr */
593 if (!no_autodetect) {
594 switch (addr) {
595 case 0x10:
596 if (tea5761_autodetection(t->i2c.adapter, t->i2c.addr) != EINVAL) {
597 t->type = TUNER_TEA5761;
598 t->mode_mask = T_RADIO;
599 t->mode = T_STANDBY;
600 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
601 default_mode_mask &= ~T_RADIO;
602
603 goto register_client;
604 }
605 break;
606 case 0x42:
607 case 0x43:
608 case 0x4a:
609 case 0x4b:
610 /* If chip is not tda8290, don't register.
611 since it can be tda9887*/
612 if (tda8290_probe(t->i2c.adapter, t->i2c.addr) == 0) {
613 tuner_dbg("chip at addr %x is a tda8290\n", addr);
614 } else {
615 /* Default is being tda9887 */
616 t->type = TUNER_TDA9887;
617 t->mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
618 t->mode = T_STANDBY;
619 goto register_client;
620 }
621 break;
622 case 0x60:
623 if (tea5767_autodetection(t->i2c.adapter, t->i2c.addr) != EINVAL) {
624 t->type = TUNER_TEA5767;
625 t->mode_mask = T_RADIO;
626 t->mode = T_STANDBY;
627 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
628 default_mode_mask &= ~T_RADIO;
629
630 goto register_client;
631 }
632 break;
633 }
634 }
635
636 /* Initializes only the first adapter found */
637 if (default_mode_mask != T_UNINITIALIZED) {
638 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
639 t->mode_mask = default_mode_mask;
640 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
641 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
642 default_mode_mask = T_UNINITIALIZED;
643 }
644
645 /* Should be just before return */
646register_client:
647 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
648 i2c_attach_client (&t->i2c);
649 set_type (&t->i2c,t->type, t->mode_mask, t->config, t->tuner_callback);
650 return 0;
651}
652
653static int tuner_probe(struct i2c_adapter *adap)
654{
655 if (0 != addr) {
656 normal_i2c[0] = addr;
657 normal_i2c[1] = I2C_CLIENT_END;
658 }
659
660 /* HACK: Ignore 0x6b and 0x6f on cx88 boards.
661 * FusionHDTV5 RT Gold has an ir receiver at 0x6b
662 * and an RTC at 0x6f which can get corrupted if probed.
663 */
664 if ((adap->id == I2C_HW_B_CX2388x) ||
665 (adap->id == I2C_HW_B_CX23885)) {
666 unsigned int i = 0;
667
668 while (i < I2C_CLIENT_MAX_OPTS && ignore[i] != I2C_CLIENT_END)
669 i += 2;
670 if (i + 4 < I2C_CLIENT_MAX_OPTS) {
671 ignore[i+0] = adap->nr;
672 ignore[i+1] = 0x6b;
673 ignore[i+2] = adap->nr;
674 ignore[i+3] = 0x6f;
675 ignore[i+4] = I2C_CLIENT_END;
676 } else
677 printk(KERN_WARNING "tuner: "
678 "too many options specified "
679 "in i2c probe ignore list!\n");
680 }
681
682 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
683
684 if (adap->class & I2C_CLASS_TV_ANALOG)
685 return i2c_probe(adap, &addr_data, tuner_attach);
686 return 0;
687}
688
689static int tuner_detach(struct i2c_client *client)
690{
691 struct tuner *t = i2c_get_clientdata(client);
692 int err;
693
694 err = i2c_detach_client(&t->i2c);
695 if (err) {
696 tuner_warn
697 ("Client deregistration failed, client not detached.\n");
698 return err;
699 }
700
701 if (t->ops.release)
702 t->ops.release(t);
703 else {
704 kfree(t->priv);
705 }
706 kfree(t);
707 return 0;
708}
709
710/* 690/*
711 * Switch tuner to other mode. If tuner support both tv and radio, 691 * Switch tuner to other mode. If tuner support both tv and radio,
712 * set another frequency to some value (This is needed for some pal 692 * set another frequency to some value (This is needed for some pal
@@ -716,6 +696,8 @@ static int tuner_detach(struct i2c_client *client)
716 696
717static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd) 697static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
718{ 698{
699 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
700
719 if (mode == t->mode) 701 if (mode == t->mode)
720 return 0; 702 return 0;
721 703
@@ -723,8 +705,8 @@ static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode,
723 705
724 if (check_mode(t, cmd) == EINVAL) { 706 if (check_mode(t, cmd) == EINVAL) {
725 t->mode = T_STANDBY; 707 t->mode = T_STANDBY;
726 if (t->ops.standby) 708 if (analog_ops->standby)
727 t->ops.standby(t); 709 analog_ops->standby(&t->fe);
728 return EINVAL; 710 return EINVAL;
729 } 711 }
730 return 0; 712 return 0;
@@ -747,9 +729,10 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
747{ 729{
748 struct tuner *t = i2c_get_clientdata(client); 730 struct tuner *t = i2c_get_clientdata(client);
749 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; 731 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
732 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
750 733
751 if (tuner_debug>1) 734 if (tuner_debug>1)
752 v4l_i2c_print_ioctl(&(t->i2c),cmd); 735 v4l_i2c_print_ioctl(client,cmd);
753 736
754 switch (cmd) { 737 switch (cmd) {
755 /* --- configuration --- */ 738 /* --- configuration --- */
@@ -773,8 +756,8 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
773 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL) 756 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
774 return 0; 757 return 0;
775 t->mode = T_STANDBY; 758 t->mode = T_STANDBY;
776 if (t->ops.standby) 759 if (analog_ops->standby)
777 t->ops.standby(t); 760 analog_ops->standby(&t->fe);
778 break; 761 break;
779#ifdef CONFIG_VIDEO_V4L1 762#ifdef CONFIG_VIDEO_V4L1
780 case VIDIOCSAUDIO: 763 case VIDIOCSAUDIO:
@@ -842,8 +825,8 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
842 else 825 else
843 vt->flags &= ~VIDEO_TUNER_STEREO_ON; 826 vt->flags &= ~VIDEO_TUNER_STEREO_ON;
844 } else { 827 } else {
845 if (t->ops.is_stereo) { 828 if (analog_ops->is_stereo) {
846 if (t->ops.is_stereo(t)) 829 if (analog_ops->is_stereo(&t->fe))
847 vt->flags |= 830 vt->flags |=
848 VIDEO_TUNER_STEREO_ON; 831 VIDEO_TUNER_STEREO_ON;
849 else 832 else
@@ -851,8 +834,9 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
851 ~VIDEO_TUNER_STEREO_ON; 834 ~VIDEO_TUNER_STEREO_ON;
852 } 835 }
853 } 836 }
854 if (t->ops.has_signal) 837 if (analog_ops->has_signal)
855 vt->signal = t->ops.has_signal(t); 838 vt->signal =
839 analog_ops->has_signal(&t->fe);
856 840
857 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */ 841 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
858 842
@@ -882,21 +866,28 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
882 fe_tuner_ops->get_status(&t->fe, &tuner_status); 866 fe_tuner_ops->get_status(&t->fe, &tuner_status);
883 va->mode = (tuner_status & TUNER_STATUS_STEREO) 867 va->mode = (tuner_status & TUNER_STATUS_STEREO)
884 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO; 868 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
885 } else if (t->ops.is_stereo) 869 } else if (analog_ops->is_stereo)
886 va->mode = t->ops.is_stereo(t) 870 va->mode = analog_ops->is_stereo(&t->fe)
887 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO; 871 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
888 } 872 }
889 return 0; 873 return 0;
890 } 874 }
891#endif 875#endif
892 case TDA9887_SET_CONFIG: 876 case TUNER_SET_CONFIG:
893 if (t->type == TUNER_TDA9887) { 877 {
894 int *i = arg; 878 struct v4l2_priv_tun_config *cfg = arg;
895 879
896 t->tda9887_config = *i; 880 if (t->type != cfg->tuner)
897 set_freq(client, t->tv_freq); 881 break;
882
883 if (analog_ops->set_config) {
884 analog_ops->set_config(&t->fe, cfg->priv);
885 break;
898 } 886 }
887
888 tuner_dbg("Tuner frontend module has no way to set config\n");
899 break; 889 break;
890 }
900 /* --- v4l ioctls --- */ 891 /* --- v4l ioctls --- */
901 /* take care: bttv does userspace copying, we'll get a 892 /* take care: bttv does userspace copying, we'll get a
902 kernel pointer here... */ 893 kernel pointer here... */
@@ -958,8 +949,8 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
958 switch_v4l2(); 949 switch_v4l2();
959 950
960 tuner->type = t->mode; 951 tuner->type = t->mode;
961 if (t->ops.get_afc) 952 if (analog_ops->get_afc)
962 tuner->afc=t->ops.get_afc(t); 953 tuner->afc = analog_ops->get_afc(&t->fe);
963 if (t->mode == V4L2_TUNER_ANALOG_TV) 954 if (t->mode == V4L2_TUNER_ANALOG_TV)
964 tuner->capability |= V4L2_TUNER_CAP_NORM; 955 tuner->capability |= V4L2_TUNER_CAP_NORM;
965 if (t->mode != V4L2_TUNER_RADIO) { 956 if (t->mode != V4L2_TUNER_RADIO) {
@@ -975,16 +966,20 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
975 u32 tuner_status; 966 u32 tuner_status;
976 967
977 fe_tuner_ops->get_status(&t->fe, &tuner_status); 968 fe_tuner_ops->get_status(&t->fe, &tuner_status);
978 tuner->rxsubchans = (tuner_status & TUNER_STATUS_STEREO) ? 969 tuner->rxsubchans =
979 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; 970 (tuner_status & TUNER_STATUS_STEREO) ?
971 V4L2_TUNER_SUB_STEREO :
972 V4L2_TUNER_SUB_MONO;
980 } else { 973 } else {
981 if (t->ops.is_stereo) { 974 if (analog_ops->is_stereo) {
982 tuner->rxsubchans = t->ops.is_stereo(t) ? 975 tuner->rxsubchans =
983 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; 976 analog_ops->is_stereo(&t->fe) ?
977 V4L2_TUNER_SUB_STEREO :
978 V4L2_TUNER_SUB_MONO;
984 } 979 }
985 } 980 }
986 if (t->ops.has_signal) 981 if (analog_ops->has_signal)
987 tuner->signal = t->ops.has_signal(t); 982 tuner->signal = analog_ops->has_signal(&t->fe);
988 tuner->capability |= 983 tuner->capability |=
989 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; 984 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
990 tuner->audmode = t->audmode; 985 tuner->audmode = t->audmode;
@@ -1009,8 +1004,8 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
1009 break; 1004 break;
1010 } 1005 }
1011 case VIDIOC_LOG_STATUS: 1006 case VIDIOC_LOG_STATUS:
1012 if (t->ops.tuner_status) 1007 if (analog_ops->tuner_status)
1013 t->ops.tuner_status(t); 1008 analog_ops->tuner_status(&t->fe);
1014 break; 1009 break;
1015 } 1010 }
1016 1011
@@ -1019,18 +1014,18 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
1019 1014
1020static int tuner_suspend(struct i2c_client *c, pm_message_t state) 1015static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1021{ 1016{
1022 struct tuner *t = i2c_get_clientdata (c); 1017 struct tuner *t = i2c_get_clientdata(c);
1023 1018
1024 tuner_dbg ("suspend\n"); 1019 tuner_dbg("suspend\n");
1025 /* FIXME: power down ??? */ 1020 /* FIXME: power down ??? */
1026 return 0; 1021 return 0;
1027} 1022}
1028 1023
1029static int tuner_resume(struct i2c_client *c) 1024static int tuner_resume(struct i2c_client *c)
1030{ 1025{
1031 struct tuner *t = i2c_get_clientdata (c); 1026 struct tuner *t = i2c_get_clientdata(c);
1032 1027
1033 tuner_dbg ("resume\n"); 1028 tuner_dbg("resume\n");
1034 if (V4L2_TUNER_RADIO == t->mode) { 1029 if (V4L2_TUNER_RADIO == t->mode) {
1035 if (t->radio_freq) 1030 if (t->radio_freq)
1036 set_freq(c, t->radio_freq); 1031 set_freq(c, t->radio_freq);
@@ -1041,36 +1036,227 @@ static int tuner_resume(struct i2c_client *c)
1041 return 0; 1036 return 0;
1042} 1037}
1043 1038
1044/* ----------------------------------------------------------------------- */ 1039/* ---------------------------------------------------------------------- */
1045 1040
1046static struct i2c_driver driver = { 1041LIST_HEAD(tuner_list);
1047 .id = I2C_DRIVERID_TUNER,
1048 .attach_adapter = tuner_probe,
1049 .detach_client = tuner_detach,
1050 .command = tuner_command,
1051 .suspend = tuner_suspend,
1052 .resume = tuner_resume,
1053 .driver = {
1054 .name = "tuner",
1055 },
1056};
1057static struct i2c_client client_template = {
1058 .name = "(tuner unset)",
1059 .driver = &driver,
1060};
1061 1042
1062static int __init tuner_init_module(void) 1043/* Search for existing radio and/or TV tuners on the given I2C adapter.
1044 Note that when this function is called from tuner_probe you can be
1045 certain no other devices will be added/deleted at the same time, I2C
1046 core protects against that. */
1047static void tuner_lookup(struct i2c_adapter *adap,
1048 struct tuner **radio, struct tuner **tv)
1063{ 1049{
1064 return i2c_add_driver(&driver); 1050 struct tuner *pos;
1051
1052 *radio = NULL;
1053 *tv = NULL;
1054
1055 list_for_each_entry(pos, &tuner_list, list) {
1056 int mode_mask;
1057
1058 if (pos->i2c->adapter != adap ||
1059 pos->i2c->driver->id != I2C_DRIVERID_TUNER)
1060 continue;
1061
1062 mode_mask = pos->mode_mask & ~T_STANDBY;
1063 if (*radio == NULL && mode_mask == T_RADIO)
1064 *radio = pos;
1065 /* Note: currently TDA9887 is the only demod-only
1066 device. If other devices appear then we need to
1067 make this test more general. */
1068 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
1069 (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV)))
1070 *tv = pos;
1071 }
1065} 1072}
1066 1073
1067static void __exit tuner_cleanup_module(void) 1074/* During client attach, set_type is called by adapter's attach_inform callback.
1075 set_type must then be completed by tuner_probe.
1076 */
1077static int tuner_probe(struct i2c_client *client)
1068{ 1078{
1069 i2c_del_driver(&driver); 1079 struct tuner *t;
1080 struct tuner *radio;
1081 struct tuner *tv;
1082
1083 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
1084 if (NULL == t)
1085 return -ENOMEM;
1086 t->i2c = client;
1087 strlcpy(client->name, "(tuner unset)", sizeof(client->name));
1088 i2c_set_clientdata(client, t);
1089 t->type = UNSET;
1090 t->audmode = V4L2_TUNER_MODE_STEREO;
1091 t->mode_mask = T_UNINITIALIZED;
1092
1093 if (show_i2c) {
1094 unsigned char buffer[16];
1095 int i, rc;
1096
1097 memset(buffer, 0, sizeof(buffer));
1098 rc = i2c_master_recv(client, buffer, sizeof(buffer));
1099 tuner_info("I2C RECV = ");
1100 for (i = 0; i < rc; i++)
1101 printk(KERN_CONT "%02x ", buffer[i]);
1102 printk("\n");
1103 }
1104 /* HACK: This test was added to avoid tuner to probe tda9840 and
1105 tea6415c on the MXB card */
1106 if (client->adapter->id == I2C_HW_SAA7146 && client->addr < 0x4a) {
1107 kfree(t);
1108 return -ENODEV;
1109 }
1110
1111 /* autodetection code based on the i2c addr */
1112 if (!no_autodetect) {
1113 switch (client->addr) {
1114 case 0x10:
1115 if (tea5761_autodetection(t->i2c->adapter, t->i2c->addr)
1116 != EINVAL) {
1117 t->type = TUNER_TEA5761;
1118 t->mode_mask = T_RADIO;
1119 t->mode = T_STANDBY;
1120 /* Sets freq to FM range */
1121 t->radio_freq = 87.5 * 16000;
1122 tuner_lookup(t->i2c->adapter, &radio, &tv);
1123 if (tv)
1124 tv->mode_mask &= ~T_RADIO;
1125
1126 goto register_client;
1127 }
1128 break;
1129 case 0x42:
1130 case 0x43:
1131 case 0x4a:
1132 case 0x4b:
1133 /* If chip is not tda8290, don't register.
1134 since it can be tda9887*/
1135 if (tda829x_probe(t->i2c->adapter,
1136 t->i2c->addr) == 0) {
1137 tuner_dbg("tda829x detected\n");
1138 } else {
1139 /* Default is being tda9887 */
1140 t->type = TUNER_TDA9887;
1141 t->mode_mask = T_RADIO | T_ANALOG_TV |
1142 T_DIGITAL_TV;
1143 t->mode = T_STANDBY;
1144 goto register_client;
1145 }
1146 break;
1147 case 0x60:
1148 if (tea5767_autodetection(t->i2c->adapter, t->i2c->addr)
1149 != EINVAL) {
1150 t->type = TUNER_TEA5767;
1151 t->mode_mask = T_RADIO;
1152 t->mode = T_STANDBY;
1153 /* Sets freq to FM range */
1154 t->radio_freq = 87.5 * 16000;
1155 tuner_lookup(t->i2c->adapter, &radio, &tv);
1156 if (tv)
1157 tv->mode_mask &= ~T_RADIO;
1158
1159 goto register_client;
1160 }
1161 break;
1162 }
1163 }
1164
1165 /* Initializes only the first TV tuner on this adapter. Why only the
1166 first? Because there are some devices (notably the ones with TI
1167 tuners) that have more than one i2c address for the *same* device.
1168 Experience shows that, except for just one case, the first
1169 address is the right one. The exception is a Russian tuner
1170 (ACORP_Y878F). So, the desired behavior is just to enable the
1171 first found TV tuner. */
1172 tuner_lookup(t->i2c->adapter, &radio, &tv);
1173 if (tv == NULL) {
1174 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
1175 if (radio == NULL)
1176 t->mode_mask |= T_RADIO;
1177 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
1178 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
1179 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
1180 }
1181
1182 /* Should be just before return */
1183register_client:
1184 tuner_info("chip found @ 0x%x (%s)\n", client->addr << 1,
1185 client->adapter->name);
1186
1187 /* Sets a default mode */
1188 if (t->mode_mask & T_ANALOG_TV) {
1189 t->mode = V4L2_TUNER_ANALOG_TV;
1190 } else if (t->mode_mask & T_RADIO) {
1191 t->mode = V4L2_TUNER_RADIO;
1192 } else {
1193 t->mode = V4L2_TUNER_DIGITAL_TV;
1194 }
1195 set_type(client, t->type, t->mode_mask, t->config, t->tuner_callback);
1196 list_add_tail(&t->list, &tuner_list);
1197 return 0;
1198}
1199
1200static int tuner_legacy_probe(struct i2c_adapter *adap)
1201{
1202 if (0 != addr) {
1203 normal_i2c[0] = addr;
1204 normal_i2c[1] = I2C_CLIENT_END;
1205 }
1206
1207 if ((adap->class & I2C_CLASS_TV_ANALOG) == 0)
1208 return 0;
1209
1210 /* HACK: Ignore 0x6b and 0x6f on cx88 boards.
1211 * FusionHDTV5 RT Gold has an ir receiver at 0x6b
1212 * and an RTC at 0x6f which can get corrupted if probed.
1213 */
1214 if ((adap->id == I2C_HW_B_CX2388x) ||
1215 (adap->id == I2C_HW_B_CX23885)) {
1216 unsigned int i = 0;
1217
1218 while (i < I2C_CLIENT_MAX_OPTS && ignore[i] != I2C_CLIENT_END)
1219 i += 2;
1220 if (i + 4 < I2C_CLIENT_MAX_OPTS) {
1221 ignore[i+0] = adap->nr;
1222 ignore[i+1] = 0x6b;
1223 ignore[i+2] = adap->nr;
1224 ignore[i+3] = 0x6f;
1225 ignore[i+4] = I2C_CLIENT_END;
1226 } else
1227 printk(KERN_WARNING "tuner: "
1228 "too many options specified "
1229 "in i2c probe ignore list!\n");
1230 }
1231 return 1;
1070} 1232}
1071 1233
1072module_init(tuner_init_module); 1234static int tuner_remove(struct i2c_client *client)
1073module_exit(tuner_cleanup_module); 1235{
1236 struct tuner *t = i2c_get_clientdata(client);
1237 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1238
1239 if (analog_ops->release)
1240 analog_ops->release(&t->fe);
1241
1242 list_del(&t->list);
1243 kfree(t);
1244 return 0;
1245}
1246
1247/* ----------------------------------------------------------------------- */
1248
1249static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1250 .name = "tuner",
1251 .driverid = I2C_DRIVERID_TUNER,
1252 .command = tuner_command,
1253 .probe = tuner_probe,
1254 .remove = tuner_remove,
1255 .suspend = tuner_suspend,
1256 .resume = tuner_resume,
1257 .legacy_probe = tuner_legacy_probe,
1258};
1259
1074 1260
1075/* 1261/*
1076 * Overrides for Emacs so that we follow Linus's tabbing style. 1262 * Overrides for Emacs so that we follow Linus's tabbing style.
diff --git a/drivers/media/video/tuner-driver.h b/drivers/media/video/tuner-driver.h
deleted file mode 100644
index 28a10da76d12..000000000000
--- a/drivers/media/video/tuner-driver.h
+++ /dev/null
@@ -1,99 +0,0 @@
1/*
2 tuner-driver.h - interface for different tuners
3
4 Copyright (C) 1997 Markus Schroeder (schroedm@uni-duesseldorf.de)
5 minor modifications by Ralph Metzler (rjkm@thp.uni-koeln.de)
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#ifndef __TUNER_DRIVER_H__
23#define __TUNER_DRIVER_H__
24
25#include <linux/videodev2.h>
26#include <linux/i2c.h>
27#include "tuner-i2c.h"
28#include "dvb_frontend.h"
29
30extern unsigned const int tuner_count;
31
32struct tuner;
33
34struct tuner_operations {
35 void (*set_tv_freq)(struct tuner *t, unsigned int freq);
36 void (*set_radio_freq)(struct tuner *t, unsigned int freq);
37 int (*has_signal)(struct tuner *t);
38 int (*is_stereo)(struct tuner *t);
39 int (*get_afc)(struct tuner *t);
40 void (*tuner_status)(struct tuner *t);
41 void (*standby)(struct tuner *t);
42 void (*release)(struct tuner *t);
43};
44
45struct tuner {
46 /* device */
47 struct i2c_client i2c;
48
49 unsigned int type; /* chip type */
50
51 unsigned int mode;
52 unsigned int mode_mask; /* Combination of allowable modes */
53
54 unsigned int tv_freq; /* keep track of the current settings */
55 unsigned int radio_freq;
56 unsigned int audmode;
57 v4l2_std_id std;
58
59 int using_v4l2;
60 void *priv;
61
62 struct dvb_frontend fe;
63
64 /* used by tda9887 */
65 unsigned int tda9887_config;
66
67 unsigned int config;
68 int (*tuner_callback) (void *dev, int command,int arg);
69
70 struct tuner_operations ops;
71};
72
73/* ------------------------------------------------------------------------ */
74
75extern int tda9887_tuner_init(struct tuner *t);
76
77/* ------------------------------------------------------------------------ */
78
79#define tuner_warn(fmt, arg...) do {\
80 printk(KERN_WARNING "%s %d-%04x: " fmt, t->i2c.driver->driver.name, \
81 i2c_adapter_id(t->i2c.adapter), t->i2c.addr , ##arg); } while (0)
82#define tuner_info(fmt, arg...) do {\
83 printk(KERN_INFO "%s %d-%04x: " fmt, t->i2c.driver->driver.name, \
84 i2c_adapter_id(t->i2c.adapter), t->i2c.addr , ##arg); } while (0)
85#define tuner_dbg(fmt, arg...) do {\
86 extern int tuner_debug; \
87 if (tuner_debug) \
88 printk(KERN_DEBUG "%s %d-%04x: " fmt, t->i2c.driver->driver.name, \
89 i2c_adapter_id(t->i2c.adapter), t->i2c.addr , ##arg); } while (0)
90
91#endif /* __TUNER_DRIVER_H__ */
92
93/*
94 * Overrides for Emacs so that we follow Linus's tabbing style.
95 * ---------------------------------------------------------------------------
96 * Local variables:
97 * c-basic-offset: 8
98 * End:
99 */
diff --git a/drivers/media/video/tuner-i2c.h b/drivers/media/video/tuner-i2c.h
index 159019ec3373..de52e8ffd347 100644
--- a/drivers/media/video/tuner-i2c.h
+++ b/drivers/media/video/tuner-i2c.h
@@ -46,25 +46,42 @@ static inline int tuner_i2c_xfer_recv(struct tuner_i2c_props *props, char *buf,
46 return (ret == 1) ? len : ret; 46 return (ret == 1) ? len : ret;
47} 47}
48 48
49#ifndef __TUNER_DRIVER_H__ 49static inline int tuner_i2c_xfer_send_recv(struct tuner_i2c_props *props,
50#define tuner_warn(fmt, arg...) do {\ 50 char *obuf, int olen,
51 printk(KERN_WARNING PREFIX "%d-%04x: " fmt, \ 51 char *ibuf, int ilen)
52 i2c_adapter_id(priv->i2c_props.adap), priv->i2c_props.addr , ##arg); } while (0) 52{
53#define tuner_info(fmt, arg...) do {\ 53 struct i2c_msg msg[2] = { { .addr = props->addr, .flags = 0,
54 printk(KERN_INFO PREFIX "%d-%04x: " fmt, \ 54 .buf = obuf, .len = olen },
55 i2c_adapter_id(priv->i2c_props.adap), priv->i2c_props.addr , ##arg); } while (0) 55 { .addr = props->addr, .flags = I2C_M_RD,
56#define tuner_dbg(fmt, arg...) do {\ 56 .buf = ibuf, .len = ilen } };
57 if ((debug)) \ 57 int ret = i2c_transfer(props->adap, msg, 2);
58 printk(KERN_DEBUG PREFIX "%d-%04x: " fmt, \
59 i2c_adapter_id(priv->i2c_props.adap), priv->i2c_props.addr , ##arg); } while (0)
60#endif /* __TUNER_DRIVER_H__ */
61 58
62#endif /* __TUNER_I2C_H__ */ 59 return (ret == 2) ? ilen : ret;
60}
63 61
64/* 62#define tuner_warn(fmt, arg...) do { \
65 * Overrides for Emacs so that we follow Linus's tabbing style. 63 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
66 * --------------------------------------------------------------------------- 64 i2c_adapter_id(priv->i2c_props.adap), \
67 * Local variables: 65 priv->i2c_props.addr, ##arg); \
68 * c-basic-offset: 8 66 } while (0)
69 * End: 67
70 */ 68#define tuner_info(fmt, arg...) do { \
69 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
70 i2c_adapter_id(priv->i2c_props.adap), \
71 priv->i2c_props.addr , ##arg); \
72 } while (0)
73
74#define tuner_err(fmt, arg...) do { \
75 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
76 i2c_adapter_id(priv->i2c_props.adap), \
77 priv->i2c_props.addr , ##arg); \
78 } while (0)
79
80#define tuner_dbg(fmt, arg...) do { \
81 if ((debug)) \
82 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
83 i2c_adapter_id(priv->i2c_props.adap), \
84 priv->i2c_props.addr , ##arg); \
85 } while (0)
86
87#endif /* __TUNER_I2C_H__ */
diff --git a/drivers/media/video/tuner-simple.c b/drivers/media/video/tuner-simple.c
index 7b93d3b1f4c6..c1db576696c6 100644
--- a/drivers/media/video/tuner-simple.c
+++ b/drivers/media/video/tuner-simple.c
@@ -17,7 +17,7 @@ static int debug = 0;
17module_param(debug, int, 0644); 17module_param(debug, int, 0644);
18MODULE_PARM_DESC(debug, "enable verbose debug messages"); 18MODULE_PARM_DESC(debug, "enable verbose debug messages");
19 19
20#define PREFIX "tuner-simple " 20#define PREFIX "tuner-simple"
21 21
22static int offset = 0; 22static int offset = 0;
23module_param(offset, int, 0664); 23module_param(offset, int, 0664);
@@ -355,10 +355,14 @@ static int simple_set_tv_freq(struct dvb_frontend *fe,
355 } 355 }
356 priv->last_div = div; 356 priv->last_div = div;
357 if (t_params->has_tda9887) { 357 if (t_params->has_tda9887) {
358 struct v4l2_priv_tun_config tda9887_cfg;
358 int config = 0; 359 int config = 0;
359 int is_secam_l = (params->std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) && 360 int is_secam_l = (params->std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) &&
360 !(params->std & ~(V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)); 361 !(params->std & ~(V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC));
361 362
363 tda9887_cfg.tuner = TUNER_TDA9887;
364 tda9887_cfg.priv = &config;
365
362 if (params->std == V4L2_STD_SECAM_LC) { 366 if (params->std == V4L2_STD_SECAM_LC) {
363 if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc) 367 if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
364 config |= TDA9887_PORT1_ACTIVE; 368 config |= TDA9887_PORT1_ACTIVE;
@@ -391,7 +395,8 @@ static int simple_set_tv_freq(struct dvb_frontend *fe,
391 } 395 }
392 if (t_params->default_pll_gating_18) 396 if (t_params->default_pll_gating_18)
393 config |= TDA9887_GATING_18; 397 config |= TDA9887_GATING_18;
394 i2c_clients_command(priv->i2c_props.adap, TDA9887_SET_CONFIG, &config); 398 i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
399 &tda9887_cfg);
395 } 400 }
396 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n", 401 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
397 buffer[0],buffer[1],buffer[2],buffer[3]); 402 buffer[0],buffer[1],buffer[2],buffer[3]);
@@ -534,6 +539,11 @@ static int simple_set_radio_freq(struct dvb_frontend *fe,
534 539
535 if (t_params->has_tda9887) { 540 if (t_params->has_tda9887) {
536 int config = 0; 541 int config = 0;
542 struct v4l2_priv_tun_config tda9887_cfg;
543
544 tda9887_cfg.tuner = TUNER_TDA9887;
545 tda9887_cfg.priv = &config;
546
537 if (t_params->port1_active && !t_params->port1_fm_high_sensitivity) 547 if (t_params->port1_active && !t_params->port1_fm_high_sensitivity)
538 config |= TDA9887_PORT1_ACTIVE; 548 config |= TDA9887_PORT1_ACTIVE;
539 if (t_params->port2_active && !t_params->port2_fm_high_sensitivity) 549 if (t_params->port2_active && !t_params->port2_fm_high_sensitivity)
@@ -546,7 +556,8 @@ static int simple_set_radio_freq(struct dvb_frontend *fe,
546 config |= TDA9887_GAIN_NORMAL; 556 config |= TDA9887_GAIN_NORMAL;
547 if (t_params->radio_if == 2) 557 if (t_params->radio_if == 2)
548 config |= TDA9887_RIF_41_3; 558 config |= TDA9887_RIF_41_3;
549 i2c_clients_command(priv->i2c_props.adap, TDA9887_SET_CONFIG, &config); 559 i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
560 &tda9887_cfg);
550 } 561 }
551 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,4))) 562 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,4)))
552 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc); 563 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
diff --git a/drivers/media/video/tuner-types.c b/drivers/media/video/tuner-types.c
index c6a7934bd5a6..883047f9c28c 100644
--- a/drivers/media/video/tuner-types.c
+++ b/drivers/media/video/tuner-types.c
@@ -1366,7 +1366,7 @@ struct tunertype tuners[] = {
1366 .count = ARRAY_SIZE(tuner_philips_fq1286_params), 1366 .count = ARRAY_SIZE(tuner_philips_fq1286_params),
1367 }, 1367 },
1368 [TUNER_PHILIPS_TDA8290] = { /* Philips PAL|NTSC */ 1368 [TUNER_PHILIPS_TDA8290] = { /* Philips PAL|NTSC */
1369 .name = "tda8290+75", 1369 .name = "Philips/NXP TDA 8290/8295 + 8275/8275A/18271",
1370 /* see tda8290.c for details */ }, 1370 /* see tda8290.c for details */ },
1371 [TUNER_TCL_2002MB] = { /* TCL PAL */ 1371 [TUNER_TCL_2002MB] = { /* TCL PAL */
1372 .name = "TCL 2002MB", 1372 .name = "TCL 2002MB",
@@ -1452,9 +1452,9 @@ struct tunertype tuners[] = {
1452 .params = tuner_samsung_tcpn_2121p30a_params, 1452 .params = tuner_samsung_tcpn_2121p30a_params,
1453 .count = ARRAY_SIZE(tuner_samsung_tcpn_2121p30a_params), 1453 .count = ARRAY_SIZE(tuner_samsung_tcpn_2121p30a_params),
1454 }, 1454 },
1455 [TUNER_XCEIVE_XC3028] = { /* Xceive 3028 */ 1455 [TUNER_XC2028] = { /* Xceive 2028 */
1456 .name = "Xceive xc3028", 1456 .name = "Xceive xc2028/xc3028 tuner",
1457 /* see xc3028.c for details */ 1457 /* see tuner-xc2028.c for details */
1458 }, 1458 },
1459 [TUNER_THOMSON_FE6600] = { /* Thomson PAL / DVB-T */ 1459 [TUNER_THOMSON_FE6600] = { /* Thomson PAL / DVB-T */
1460 .name = "Thomson FE6600", 1460 .name = "Thomson FE6600",
@@ -1475,6 +1475,10 @@ struct tunertype tuners[] = {
1475 .name = "Philips TEA5761 FM Radio", 1475 .name = "Philips TEA5761 FM Radio",
1476 /* see tea5767.c for details */ 1476 /* see tea5767.c for details */
1477 }, 1477 },
1478 [TUNER_XC5000] = { /* Xceive 5000 */
1479 .name = "Xceive 5000 tuner",
1480 /* see xc5000.c for details */
1481 },
1478}; 1482};
1479 1483
1480unsigned const int tuner_count = ARRAY_SIZE(tuners); 1484unsigned const int tuner_count = ARRAY_SIZE(tuners);
diff --git a/drivers/media/video/tuner-xc2028-types.h b/drivers/media/video/tuner-xc2028-types.h
new file mode 100644
index 000000000000..d0057fbf0ec7
--- /dev/null
+++ b/drivers/media/video/tuner-xc2028-types.h
@@ -0,0 +1,128 @@
1/* tuner-xc2028_types
2 *
3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
4 * This code is placed under the terms of the GNU General Public License v2
5 */
6
7/* xc3028 firmware types */
8
9/* BASE firmware should be loaded before any other firmware */
10#define BASE (1<<0)
11#define BASE_TYPES (BASE|F8MHZ|MTS|FM|INPUT1|INPUT2|INIT1)
12
13/* F8MHZ marks BASE firmwares for 8 MHz Bandwidth */
14#define F8MHZ (1<<1)
15
16/* Multichannel Television Sound (MTS)
17 Those firmwares are capable of using xc2038 DSP to decode audio and
18 produce a baseband audio output on some pins of the chip.
19 There are MTS firmwares for the most used video standards. It should be
20 required to use MTS firmwares, depending on the way audio is routed into
21 the bridge chip
22 */
23#define MTS (1<<2)
24
25/* FIXME: I have no idea what's the difference between
26 D2620 and D2633 firmwares
27 */
28#define D2620 (1<<3)
29#define D2633 (1<<4)
30
31/* DTV firmwares for 6, 7 and 8 MHz
32 DTV6 - 6MHz - ATSC/DVB-C/DVB-T/ISDB-T/DOCSIS
33 DTV8 - 8MHz - DVB-C/DVB-T
34 */
35#define DTV6 (1 << 5)
36#define QAM (1 << 6)
37#define DTV7 (1<<7)
38#define DTV78 (1<<8)
39#define DTV8 (1<<9)
40
41#define DTV_TYPES (D2620|D2633|DTV6|QAM|DTV7|DTV78|DTV8|ATSC)
42
43/* There's a FM | BASE firmware + FM specific firmware (std=0) */
44#define FM (1<<10)
45
46#define STD_SPECIFIC_TYPES (MTS|FM|LCD|NOGD)
47
48/* Applies only for FM firmware
49 Makes it use RF input 1 (pin #2) instead of input 2 (pin #4)
50 */
51#define INPUT1 (1<<11)
52
53
54/* LCD firmwares exist only for MTS STD/MN (PAL or NTSC/M)
55 and for non-MTS STD/MN (PAL, NTSC/M or NTSC/Kr)
56 There are variants both with and without NOGD
57 */
58#define LCD (1<<12)
59
60/* NOGD firmwares exist only for MTS STD/MN (PAL or NTSC/M)
61 and for non-MTS STD/MN (PAL, NTSC/M or NTSC/Kr)
62 */
63#define NOGD (1<<13)
64
65/* Old firmwares were broken into init0 and init1 */
66#define INIT1 (1<<14)
67
68/* SCODE firmware selects particular behaviours */
69#define MONO (1 << 15)
70#define ATSC (1 << 16)
71#define IF (1 << 17)
72#define LG60 (1 << 18)
73#define ATI638 (1 << 19)
74#define OREN538 (1 << 20)
75#define OREN36 (1 << 21)
76#define TOYOTA388 (1 << 22)
77#define TOYOTA794 (1 << 23)
78#define DIBCOM52 (1 << 24)
79#define ZARLINK456 (1 << 25)
80#define CHINA (1 << 26)
81#define F6MHZ (1 << 27)
82#define INPUT2 (1 << 28)
83#define SCODE (1 << 29)
84
85/* This flag identifies that the scode table has a new format */
86#define HAS_IF (1 << 30)
87
88#define SCODE_TYPES (MTS|DTV6|QAM|DTV7|DTV78|DTV8|LCD|NOGD|MONO|ATSC|IF| \
89 LG60|ATI638|OREN538|OREN36|TOYOTA388|TOYOTA794| \
90 DIBCOM52|ZARLINK456|CHINA|F6MHZ|SCODE)
91
92/* Newer types to be moved to videodev2.h */
93
94#define V4L2_STD_SECAM_K3 (0x04000000)
95
96/* Audio types */
97
98#define V4L2_STD_A2_A (1LL<<32)
99#define V4L2_STD_A2_B (1LL<<33)
100#define V4L2_STD_NICAM_A (1LL<<34)
101#define V4L2_STD_NICAM_B (1LL<<35)
102#define V4L2_STD_AM (1LL<<36)
103#define V4L2_STD_BTSC (1LL<<37)
104#define V4L2_STD_EIAJ (1LL<<38)
105
106#define V4L2_STD_A2 (V4L2_STD_A2_A | V4L2_STD_A2_B)
107#define V4L2_STD_NICAM (V4L2_STD_NICAM_A | V4L2_STD_NICAM_B)
108
109/* To preserve backward compatibilty,
110 (std & V4L2_STD_AUDIO) = 0 means that ALL audio stds are supported
111 */
112
113#define V4L2_STD_AUDIO (V4L2_STD_A2 | \
114 V4L2_STD_NICAM | \
115 V4L2_STD_AM | \
116 V4L2_STD_BTSC | \
117 V4L2_STD_EIAJ)
118
119/* Used standards with audio restrictions */
120
121#define V4L2_STD_PAL_BG_A2_A (V4L2_STD_PAL_BG | V4L2_STD_A2_A)
122#define V4L2_STD_PAL_BG_A2_B (V4L2_STD_PAL_BG | V4L2_STD_A2_B)
123#define V4L2_STD_PAL_BG_NICAM_A (V4L2_STD_PAL_BG | V4L2_STD_NICAM_A)
124#define V4L2_STD_PAL_BG_NICAM_B (V4L2_STD_PAL_BG | V4L2_STD_NICAM_B)
125#define V4L2_STD_PAL_DK_A2 (V4L2_STD_PAL_DK | V4L2_STD_A2)
126#define V4L2_STD_PAL_DK_NICAM (V4L2_STD_PAL_DK | V4L2_STD_NICAM)
127#define V4L2_STD_SECAM_L_NICAM (V4L2_STD_SECAM_L | V4L2_STD_NICAM)
128#define V4L2_STD_SECAM_L_AM (V4L2_STD_SECAM_L | V4L2_STD_AM)
diff --git a/drivers/media/video/tuner-xc2028.c b/drivers/media/video/tuner-xc2028.c
new file mode 100644
index 000000000000..f191f6a48070
--- /dev/null
+++ b/drivers/media/video/tuner-xc2028.c
@@ -0,0 +1,1213 @@
1/* tuner-xc2028
2 *
3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
4 *
5 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
7 *
8 * This code is placed under the terms of the GNU General Public License v2
9 */
10
11#include <linux/i2c.h>
12#include <asm/div64.h>
13#include <linux/firmware.h>
14#include <linux/videodev2.h>
15#include <linux/delay.h>
16#include <media/tuner.h>
17#include <linux/mutex.h>
18#include "tuner-i2c.h"
19#include "tuner-xc2028.h"
20#include "tuner-xc2028-types.h"
21
22#include <linux/dvb/frontend.h>
23#include "dvb_frontend.h"
24
25
26#define PREFIX "xc2028"
27
28static int debug;
29module_param(debug, int, 0644);
30MODULE_PARM_DESC(debug, "enable verbose debug messages");
31
32static char audio_std[8];
33module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
34MODULE_PARM_DESC(audio_std,
35 "Audio standard. XC3028 audio decoder explicitly "
36 "needs to know what audio\n"
37 "standard is needed for some video standards with audio A2 or NICAM.\n"
38 "The valid values are:\n"
39 "A2\n"
40 "A2/A\n"
41 "A2/B\n"
42 "NICAM\n"
43 "NICAM/A\n"
44 "NICAM/B\n");
45
46static LIST_HEAD(xc2028_list);
47static DEFINE_MUTEX(xc2028_list_mutex);
48
49/* struct for storing firmware table */
50struct firmware_description {
51 unsigned int type;
52 v4l2_std_id id;
53 __u16 int_freq;
54 unsigned char *ptr;
55 unsigned int size;
56};
57
58struct firmware_properties {
59 unsigned int type;
60 v4l2_std_id id;
61 v4l2_std_id std_req;
62 __u16 int_freq;
63 unsigned int scode_table;
64 int scode_nr;
65};
66
67struct xc2028_data {
68 struct list_head xc2028_list;
69 struct tuner_i2c_props i2c_props;
70 int (*tuner_callback) (void *dev,
71 int command, int arg);
72 void *video_dev;
73 int count;
74 __u32 frequency;
75
76 struct firmware_description *firm;
77 int firm_size;
78 __u16 firm_version;
79
80 __u16 hwmodel;
81 __u16 hwvers;
82
83 struct xc2028_ctrl ctrl;
84
85 struct firmware_properties cur_fw;
86
87 struct mutex lock;
88};
89
90#define i2c_send(priv, buf, size) ({ \
91 int _rc; \
92 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
93 if (size != _rc) \
94 tuner_info("i2c output error: rc = %d (should be %d)\n",\
95 _rc, (int)size); \
96 _rc; \
97})
98
99#define i2c_rcv(priv, buf, size) ({ \
100 int _rc; \
101 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
102 if (size != _rc) \
103 tuner_err("i2c input error: rc = %d (should be %d)\n", \
104 _rc, (int)size); \
105 _rc; \
106})
107
108#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
109 int _rc; \
110 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
111 ibuf, isize); \
112 if (isize != _rc) \
113 tuner_err("i2c input error: rc = %d (should be %d)\n", \
114 _rc, (int)isize); \
115 _rc; \
116})
117
118#define send_seq(priv, data...) ({ \
119 static u8 _val[] = data; \
120 int _rc; \
121 if (sizeof(_val) != \
122 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
123 _val, sizeof(_val)))) { \
124 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
125 } else \
126 msleep(10); \
127 _rc; \
128})
129
130static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
131{
132 unsigned char buf[2];
133 unsigned char ibuf[2];
134
135 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
136
137 buf[0] = reg >> 8;
138 buf[1] = (unsigned char) reg;
139
140 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
141 return -EIO;
142
143 *val = (ibuf[1]) | (ibuf[0] << 8);
144 return 0;
145}
146
147#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
148void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
149{
150 if (type & BASE)
151 printk("BASE ");
152 if (type & INIT1)
153 printk("INIT1 ");
154 if (type & F8MHZ)
155 printk("F8MHZ ");
156 if (type & MTS)
157 printk("MTS ");
158 if (type & D2620)
159 printk("D2620 ");
160 if (type & D2633)
161 printk("D2633 ");
162 if (type & DTV6)
163 printk("DTV6 ");
164 if (type & QAM)
165 printk("QAM ");
166 if (type & DTV7)
167 printk("DTV7 ");
168 if (type & DTV78)
169 printk("DTV78 ");
170 if (type & DTV8)
171 printk("DTV8 ");
172 if (type & FM)
173 printk("FM ");
174 if (type & INPUT1)
175 printk("INPUT1 ");
176 if (type & LCD)
177 printk("LCD ");
178 if (type & NOGD)
179 printk("NOGD ");
180 if (type & MONO)
181 printk("MONO ");
182 if (type & ATSC)
183 printk("ATSC ");
184 if (type & IF)
185 printk("IF ");
186 if (type & LG60)
187 printk("LG60 ");
188 if (type & ATI638)
189 printk("ATI638 ");
190 if (type & OREN538)
191 printk("OREN538 ");
192 if (type & OREN36)
193 printk("OREN36 ");
194 if (type & TOYOTA388)
195 printk("TOYOTA388 ");
196 if (type & TOYOTA794)
197 printk("TOYOTA794 ");
198 if (type & DIBCOM52)
199 printk("DIBCOM52 ");
200 if (type & ZARLINK456)
201 printk("ZARLINK456 ");
202 if (type & CHINA)
203 printk("CHINA ");
204 if (type & F6MHZ)
205 printk("F6MHZ ");
206 if (type & INPUT2)
207 printk("INPUT2 ");
208 if (type & SCODE)
209 printk("SCODE ");
210 if (type & HAS_IF)
211 printk("HAS_IF_%d ", int_freq);
212}
213
214static v4l2_std_id parse_audio_std_option(void)
215{
216 if (strcasecmp(audio_std, "A2") == 0)
217 return V4L2_STD_A2;
218 if (strcasecmp(audio_std, "A2/A") == 0)
219 return V4L2_STD_A2_A;
220 if (strcasecmp(audio_std, "A2/B") == 0)
221 return V4L2_STD_A2_B;
222 if (strcasecmp(audio_std, "NICAM") == 0)
223 return V4L2_STD_NICAM;
224 if (strcasecmp(audio_std, "NICAM/A") == 0)
225 return V4L2_STD_NICAM_A;
226 if (strcasecmp(audio_std, "NICAM/B") == 0)
227 return V4L2_STD_NICAM_B;
228
229 return 0;
230}
231
232static void free_firmware(struct xc2028_data *priv)
233{
234 int i;
235
236 if (!priv->firm)
237 return;
238
239 for (i = 0; i < priv->firm_size; i++)
240 kfree(priv->firm[i].ptr);
241
242 kfree(priv->firm);
243
244 priv->firm = NULL;
245 priv->firm_size = 0;
246
247 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
248}
249
250static int load_all_firmwares(struct dvb_frontend *fe)
251{
252 struct xc2028_data *priv = fe->tuner_priv;
253 const struct firmware *fw = NULL;
254 unsigned char *p, *endp;
255 int rc = 0;
256 int n, n_array;
257 char name[33];
258
259 tuner_dbg("%s called\n", __FUNCTION__);
260
261 tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
262 rc = request_firmware(&fw, priv->ctrl.fname,
263 &priv->i2c_props.adap->dev);
264 if (rc < 0) {
265 if (rc == -ENOENT)
266 tuner_err("Error: firmware %s not found.\n",
267 priv->ctrl.fname);
268 else
269 tuner_err("Error %d while requesting firmware %s \n",
270 rc, priv->ctrl.fname);
271
272 return rc;
273 }
274 p = fw->data;
275 endp = p + fw->size;
276
277 if (fw->size < sizeof(name) - 1 + 2 + 2) {
278 tuner_err("Error: firmware file %s has invalid size!\n",
279 priv->ctrl.fname);
280 goto corrupt;
281 }
282
283 memcpy(name, p, sizeof(name) - 1);
284 name[sizeof(name) - 1] = 0;
285 p += sizeof(name) - 1;
286
287 priv->firm_version = le16_to_cpu(*(__u16 *) p);
288 p += 2;
289
290 n_array = le16_to_cpu(*(__u16 *) p);
291 p += 2;
292
293 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
294 n_array, priv->ctrl.fname, name,
295 priv->firm_version >> 8, priv->firm_version & 0xff);
296
297 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
298 if (priv->firm == NULL) {
299 tuner_err("Not enough memory to load firmware file.\n");
300 rc = -ENOMEM;
301 goto err;
302 }
303 priv->firm_size = n_array;
304
305 n = -1;
306 while (p < endp) {
307 __u32 type, size;
308 v4l2_std_id id;
309 __u16 int_freq = 0;
310
311 n++;
312 if (n >= n_array) {
313 tuner_err("More firmware images in file than "
314 "were expected!\n");
315 goto corrupt;
316 }
317
318 /* Checks if there's enough bytes to read */
319 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
320 tuner_err("Firmware header is incomplete!\n");
321 goto corrupt;
322 }
323
324 type = le32_to_cpu(*(__u32 *) p);
325 p += sizeof(type);
326
327 id = le64_to_cpu(*(v4l2_std_id *) p);
328 p += sizeof(id);
329
330 if (type & HAS_IF) {
331 int_freq = le16_to_cpu(*(__u16 *) p);
332 p += sizeof(int_freq);
333 }
334
335 size = le32_to_cpu(*(__u32 *) p);
336 p += sizeof(size);
337
338 if ((!size) || (size + p > endp)) {
339 tuner_err("Firmware type ");
340 dump_firm_type(type);
341 printk("(%x), id %llx is corrupted "
342 "(size=%d, expected %d)\n",
343 type, (unsigned long long)id,
344 (unsigned)(endp - p), size);
345 goto corrupt;
346 }
347
348 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
349 if (priv->firm[n].ptr == NULL) {
350 tuner_err("Not enough memory to load firmware file.\n");
351 rc = -ENOMEM;
352 goto err;
353 }
354 tuner_dbg("Reading firmware type ");
355 if (debug) {
356 dump_firm_type_and_int_freq(type, int_freq);
357 printk("(%x), id %llx, size=%d.\n",
358 type, (unsigned long long)id, size);
359 }
360
361 memcpy(priv->firm[n].ptr, p, size);
362 priv->firm[n].type = type;
363 priv->firm[n].id = id;
364 priv->firm[n].size = size;
365 priv->firm[n].int_freq = int_freq;
366
367 p += size;
368 }
369
370 if (n + 1 != priv->firm_size) {
371 tuner_err("Firmware file is incomplete!\n");
372 goto corrupt;
373 }
374
375 goto done;
376
377corrupt:
378 rc = -EINVAL;
379 tuner_err("Error: firmware file is corrupted!\n");
380
381err:
382 tuner_info("Releasing partially loaded firmware file.\n");
383 free_firmware(priv);
384
385done:
386 release_firmware(fw);
387 if (rc == 0)
388 tuner_dbg("Firmware files loaded.\n");
389
390 return rc;
391}
392
393static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
394 v4l2_std_id *id)
395{
396 struct xc2028_data *priv = fe->tuner_priv;
397 int i, best_i = -1, best_nr_matches = 0;
398 unsigned int ign_firm_type_mask = 0;
399
400 tuner_dbg("%s called, want type=", __FUNCTION__);
401 if (debug) {
402 dump_firm_type(type);
403 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
404 }
405
406 if (!priv->firm) {
407 tuner_err("Error! firmware not loaded\n");
408 return -EINVAL;
409 }
410
411 if (((type & ~SCODE) == 0) && (*id == 0))
412 *id = V4L2_STD_PAL;
413
414 if (type & BASE)
415 type &= BASE_TYPES;
416 else if (type & SCODE) {
417 type &= SCODE_TYPES;
418 ign_firm_type_mask = HAS_IF;
419 } else if (type & DTV_TYPES)
420 type &= DTV_TYPES;
421 else if (type & STD_SPECIFIC_TYPES)
422 type &= STD_SPECIFIC_TYPES;
423
424 /* Seek for exact match */
425 for (i = 0; i < priv->firm_size; i++) {
426 if ((type == (priv->firm[i].type & ~ign_firm_type_mask)) &&
427 (*id == priv->firm[i].id))
428 goto found;
429 }
430
431 /* Seek for generic video standard match */
432 for (i = 0; i < priv->firm_size; i++) {
433 v4l2_std_id match_mask;
434 int nr_matches;
435
436 if (type != (priv->firm[i].type & ~ign_firm_type_mask))
437 continue;
438
439 match_mask = *id & priv->firm[i].id;
440 if (!match_mask)
441 continue;
442
443 if ((*id & match_mask) == *id)
444 goto found; /* Supports all the requested standards */
445
446 nr_matches = hweight64(match_mask);
447 if (nr_matches > best_nr_matches) {
448 best_nr_matches = nr_matches;
449 best_i = i;
450 }
451 }
452
453 if (best_nr_matches > 0) {
454 tuner_dbg("Selecting best matching firmware (%d bits) for "
455 "type=", best_nr_matches);
456 dump_firm_type(type);
457 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
458 i = best_i;
459 goto found;
460 }
461
462 /*FIXME: Would make sense to seek for type "hint" match ? */
463
464 i = -ENOENT;
465 goto ret;
466
467found:
468 *id = priv->firm[i].id;
469
470ret:
471 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
472 if (debug) {
473 dump_firm_type(type);
474 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
475 }
476 return i;
477}
478
479static int load_firmware(struct dvb_frontend *fe, unsigned int type,
480 v4l2_std_id *id)
481{
482 struct xc2028_data *priv = fe->tuner_priv;
483 int pos, rc;
484 unsigned char *p, *endp, buf[priv->ctrl.max_len];
485
486 tuner_dbg("%s called\n", __FUNCTION__);
487
488 pos = seek_firmware(fe, type, id);
489 if (pos < 0)
490 return pos;
491
492 tuner_info("Loading firmware for type=");
493 dump_firm_type(priv->firm[pos].type);
494 printk("(%x), id %016llx.\n", priv->firm[pos].type,
495 (unsigned long long)*id);
496
497 p = priv->firm[pos].ptr;
498 endp = p + priv->firm[pos].size;
499
500 while (p < endp) {
501 __u16 size;
502
503 /* Checks if there's enough bytes to read */
504 if (p + sizeof(size) > endp) {
505 tuner_err("Firmware chunk size is wrong\n");
506 return -EINVAL;
507 }
508
509 size = le16_to_cpu(*(__u16 *) p);
510 p += sizeof(size);
511
512 if (size == 0xffff)
513 return 0;
514
515 if (!size) {
516 /* Special callback command received */
517 rc = priv->tuner_callback(priv->video_dev,
518 XC2028_TUNER_RESET, 0);
519 if (rc < 0) {
520 tuner_err("Error at RESET code %d\n",
521 (*p) & 0x7f);
522 return -EINVAL;
523 }
524 continue;
525 }
526 if (size >= 0xff00) {
527 switch (size) {
528 case 0xff00:
529 rc = priv->tuner_callback(priv->video_dev,
530 XC2028_RESET_CLK, 0);
531 if (rc < 0) {
532 tuner_err("Error at RESET code %d\n",
533 (*p) & 0x7f);
534 return -EINVAL;
535 }
536 break;
537 default:
538 tuner_info("Invalid RESET code %d\n",
539 size & 0x7f);
540 return -EINVAL;
541
542 }
543 continue;
544 }
545
546 /* Checks for a sleep command */
547 if (size & 0x8000) {
548 msleep(size & 0x7fff);
549 continue;
550 }
551
552 if ((size + p > endp)) {
553 tuner_err("missing bytes: need %d, have %d\n",
554 size, (int)(endp - p));
555 return -EINVAL;
556 }
557
558 buf[0] = *p;
559 p++;
560 size--;
561
562 /* Sends message chunks */
563 while (size > 0) {
564 int len = (size < priv->ctrl.max_len - 1) ?
565 size : priv->ctrl.max_len - 1;
566
567 memcpy(buf + 1, p, len);
568
569 rc = i2c_send(priv, buf, len + 1);
570 if (rc < 0) {
571 tuner_err("%d returned from send\n", rc);
572 return -EINVAL;
573 }
574
575 p += len;
576 size -= len;
577 }
578 }
579 return 0;
580}
581
582static int load_scode(struct dvb_frontend *fe, unsigned int type,
583 v4l2_std_id *id, __u16 int_freq, int scode)
584{
585 struct xc2028_data *priv = fe->tuner_priv;
586 int pos, rc;
587 unsigned char *p;
588
589 tuner_dbg("%s called\n", __FUNCTION__);
590
591 if (!int_freq) {
592 pos = seek_firmware(fe, type, id);
593 if (pos < 0)
594 return pos;
595 } else {
596 for (pos = 0; pos < priv->firm_size; pos++) {
597 if ((priv->firm[pos].int_freq == int_freq) &&
598 (priv->firm[pos].type & HAS_IF))
599 break;
600 }
601 if (pos == priv->firm_size)
602 return -ENOENT;
603 }
604
605 p = priv->firm[pos].ptr;
606
607 if (priv->firm[pos].type & HAS_IF) {
608 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
609 return -EINVAL;
610 p += 12 * scode;
611 } else {
612 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
613 * has a 2-byte size header in the firmware format. */
614 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
615 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
616 return -EINVAL;
617 p += 14 * scode + 2;
618 }
619
620 tuner_info("Loading SCODE for type=");
621 dump_firm_type_and_int_freq(priv->firm[pos].type,
622 priv->firm[pos].int_freq);
623 printk("(%x), id %016llx.\n", priv->firm[pos].type,
624 (unsigned long long)*id);
625
626 if (priv->firm_version < 0x0202)
627 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
628 else
629 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
630 if (rc < 0)
631 return -EIO;
632
633 rc = i2c_send(priv, p, 12);
634 if (rc < 0)
635 return -EIO;
636
637 rc = send_seq(priv, {0x00, 0x8c});
638 if (rc < 0)
639 return -EIO;
640
641 return 0;
642}
643
644static int check_firmware(struct dvb_frontend *fe, unsigned int type,
645 v4l2_std_id std, __u16 int_freq)
646{
647 struct xc2028_data *priv = fe->tuner_priv;
648 struct firmware_properties new_fw;
649 int rc = 0, is_retry = 0;
650 u16 version, hwmodel;
651 v4l2_std_id std0;
652
653 tuner_dbg("%s called\n", __FUNCTION__);
654
655 if (!priv->firm) {
656 if (!priv->ctrl.fname) {
657 tuner_info("xc2028/3028 firmware name not set!\n");
658 return -EINVAL;
659 }
660
661 rc = load_all_firmwares(fe);
662 if (rc < 0)
663 return rc;
664 }
665
666 if (priv->ctrl.mts && !(type & FM))
667 type |= MTS;
668
669retry:
670 new_fw.type = type;
671 new_fw.id = std;
672 new_fw.std_req = std;
673 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
674 new_fw.scode_nr = 0;
675 new_fw.int_freq = int_freq;
676
677 tuner_dbg("checking firmware, user requested type=");
678 if (debug) {
679 dump_firm_type(new_fw.type);
680 printk("(%x), id %016llx, ", new_fw.type,
681 (unsigned long long)new_fw.std_req);
682 if (!int_freq) {
683 printk("scode_tbl ");
684 dump_firm_type(priv->ctrl.scode_table);
685 printk("(%x), ", priv->ctrl.scode_table);
686 } else
687 printk("int_freq %d, ", new_fw.int_freq);
688 printk("scode_nr %d\n", new_fw.scode_nr);
689 }
690
691 /* No need to reload base firmware if it matches */
692 if (((BASE | new_fw.type) & BASE_TYPES) ==
693 (priv->cur_fw.type & BASE_TYPES)) {
694 tuner_dbg("BASE firmware not changed.\n");
695 goto skip_base;
696 }
697
698 /* Updating BASE - forget about all currently loaded firmware */
699 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
700
701 /* Reset is needed before loading firmware */
702 rc = priv->tuner_callback(priv->video_dev,
703 XC2028_TUNER_RESET, 0);
704 if (rc < 0)
705 goto fail;
706
707 /* BASE firmwares are all std0 */
708 std0 = 0;
709 rc = load_firmware(fe, BASE | new_fw.type, &std0);
710 if (rc < 0) {
711 tuner_err("Error %d while loading base firmware\n",
712 rc);
713 goto fail;
714 }
715
716 /* Load INIT1, if needed */
717 tuner_dbg("Load init1 firmware, if exists\n");
718
719 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
720 if (rc == -ENOENT)
721 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
722 &std0);
723 if (rc < 0 && rc != -ENOENT) {
724 tuner_err("Error %d while loading init1 firmware\n",
725 rc);
726 goto fail;
727 }
728
729skip_base:
730 /*
731 * No need to reload standard specific firmware if base firmware
732 * was not reloaded and requested video standards have not changed.
733 */
734 if (priv->cur_fw.type == (BASE | new_fw.type) &&
735 priv->cur_fw.std_req == std) {
736 tuner_dbg("Std-specific firmware already loaded.\n");
737 goto skip_std_specific;
738 }
739
740 /* Reloading std-specific firmware forces a SCODE update */
741 priv->cur_fw.scode_table = 0;
742
743 rc = load_firmware(fe, new_fw.type, &new_fw.id);
744 if (rc == -ENOENT)
745 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
746
747 if (rc < 0)
748 goto fail;
749
750skip_std_specific:
751 if (priv->cur_fw.scode_table == new_fw.scode_table &&
752 priv->cur_fw.scode_nr == new_fw.scode_nr) {
753 tuner_dbg("SCODE firmware already loaded.\n");
754 goto check_device;
755 }
756
757 /* Load SCODE firmware, if exists */
758 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
759
760 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
761 new_fw.int_freq, new_fw.scode_nr);
762
763check_device:
764 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
765 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
766 tuner_err("Unable to read tuner registers.\n");
767 goto fail;
768 }
769
770 tuner_info("Device is Xceive %d version %d.%d, "
771 "firmware version %d.%d\n",
772 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
773 (version & 0xf0) >> 4, version & 0xf);
774
775 /* Check firmware version against what we downloaded. */
776 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
777 tuner_err("Incorrect readback of firmware version.\n");
778 goto fail;
779 }
780
781 /* Check that the tuner hardware model remains consistent over time. */
782 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
783 priv->hwmodel = hwmodel;
784 priv->hwvers = version & 0xff00;
785 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
786 priv->hwvers != (version & 0xff00)) {
787 tuner_err("Read invalid device hardware information - tuner "
788 "hung?\n");
789 goto fail;
790 }
791
792 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
793
794 /*
795 * By setting BASE in cur_fw.type only after successfully loading all
796 * firmwares, we can:
797 * 1. Identify that BASE firmware with type=0 has been loaded;
798 * 2. Tell whether BASE firmware was just changed the next time through.
799 */
800 priv->cur_fw.type |= BASE;
801
802 return 0;
803
804fail:
805 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
806 if (!is_retry) {
807 msleep(50);
808 is_retry = 1;
809 tuner_dbg("Retrying firmware load\n");
810 goto retry;
811 }
812
813 if (rc == -ENOENT)
814 rc = -EINVAL;
815 return rc;
816}
817
818static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
819{
820 struct xc2028_data *priv = fe->tuner_priv;
821 u16 frq_lock, signal = 0;
822 int rc;
823
824 tuner_dbg("%s called\n", __FUNCTION__);
825
826 mutex_lock(&priv->lock);
827
828 /* Sync Lock Indicator */
829 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
830 if (rc < 0 || frq_lock == 0)
831 goto ret;
832
833 /* Frequency is locked. Return signal quality */
834
835 /* Get SNR of the video signal */
836 rc = xc2028_get_reg(priv, 0x0040, &signal);
837 if (rc < 0)
838 signal = -frq_lock;
839
840ret:
841 mutex_unlock(&priv->lock);
842
843 *strength = signal;
844
845 return rc;
846}
847
848#define DIV 15625
849
850static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
851 enum tuner_mode new_mode,
852 unsigned int type,
853 v4l2_std_id std,
854 u16 int_freq)
855{
856 struct xc2028_data *priv = fe->tuner_priv;
857 int rc = -EINVAL;
858 unsigned char buf[4];
859 u32 div, offset = 0;
860
861 tuner_dbg("%s called\n", __FUNCTION__);
862
863 mutex_lock(&priv->lock);
864
865 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
866
867 if (check_firmware(fe, type, std, int_freq) < 0)
868 goto ret;
869
870 /* On some cases xc2028 can disable video output, if
871 * very weak signals are received. By sending a soft
872 * reset, this is re-enabled. So, it is better to always
873 * send a soft reset before changing channels, to be sure
874 * that xc2028 will be in a safe state.
875 * Maybe this might also be needed for DTV.
876 */
877 if (new_mode == T_ANALOG_TV) {
878 rc = send_seq(priv, {0x00, 0x00});
879 } else if (priv->cur_fw.type & ATSC) {
880 offset = 1750000;
881 } else {
882 offset = 2750000;
883 /*
884 * We must adjust the offset by 500kHz in two cases in order
885 * to correctly center the IF output:
886 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
887 * selected and a 7MHz channel is tuned;
888 * 2) When tuning a VHF channel with DTV78 firmware.
889 */
890 if (((priv->cur_fw.type & DTV7) &&
891 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
892 ((priv->cur_fw.type & DTV78) && freq < 470000000))
893 offset -= 500000;
894 }
895
896 div = (freq - offset + DIV / 2) / DIV;
897
898 /* CMD= Set frequency */
899 if (priv->firm_version < 0x0202)
900 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
901 else
902 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
903 if (rc < 0)
904 goto ret;
905
906 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
907 if (rc < 0)
908 goto ret;
909
910 msleep(10);
911
912 buf[0] = 0xff & (div >> 24);
913 buf[1] = 0xff & (div >> 16);
914 buf[2] = 0xff & (div >> 8);
915 buf[3] = 0xff & (div);
916
917 rc = i2c_send(priv, buf, sizeof(buf));
918 if (rc < 0)
919 goto ret;
920 msleep(100);
921
922 priv->frequency = freq;
923
924 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
925 buf[0], buf[1], buf[2], buf[3],
926 freq / 1000000, (freq % 1000000) / 1000);
927
928 rc = 0;
929
930ret:
931 mutex_unlock(&priv->lock);
932
933 return rc;
934}
935
936static int xc2028_set_analog_freq(struct dvb_frontend *fe,
937 struct analog_parameters *p)
938{
939 struct xc2028_data *priv = fe->tuner_priv;
940 unsigned int type=0;
941
942 tuner_dbg("%s called\n", __FUNCTION__);
943
944 if (p->mode == V4L2_TUNER_RADIO) {
945 type |= FM;
946 if (priv->ctrl.input1)
947 type |= INPUT1;
948 return generic_set_freq(fe, (625l * p->frequency) / 10,
949 T_ANALOG_TV, type, 0, 0);
950 }
951
952 /* if std is not defined, choose one */
953 if (!p->std)
954 p->std = V4L2_STD_MN;
955
956 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
957 if (!(p->std & V4L2_STD_MN))
958 type |= F8MHZ;
959
960 /* Add audio hack to std mask */
961 p->std |= parse_audio_std_option();
962
963 return generic_set_freq(fe, 62500l * p->frequency,
964 T_ANALOG_TV, type, p->std, 0);
965}
966
967static int xc2028_set_params(struct dvb_frontend *fe,
968 struct dvb_frontend_parameters *p)
969{
970 struct xc2028_data *priv = fe->tuner_priv;
971 unsigned int type=0;
972 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
973 u16 demod = 0;
974
975 tuner_dbg("%s called\n", __FUNCTION__);
976
977 if (priv->ctrl.d2633)
978 type |= D2633;
979 else
980 type |= D2620;
981
982 switch(fe->ops.info.type) {
983 case FE_OFDM:
984 bw = p->u.ofdm.bandwidth;
985 break;
986 case FE_QAM:
987 tuner_info("WARN: There are some reports that "
988 "QAM 6 MHz doesn't work.\n"
989 "If this works for you, please report by "
990 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
991 bw = BANDWIDTH_6_MHZ;
992 type |= QAM;
993 break;
994 case FE_ATSC:
995 bw = BANDWIDTH_6_MHZ;
996 /* The only ATSC firmware (at least on v2.7) is D2633,
997 so overrides ctrl->d2633 */
998 type |= ATSC| D2633;
999 type &= ~D2620;
1000 break;
1001 /* DVB-S is not supported */
1002 default:
1003 return -EINVAL;
1004 }
1005
1006 switch (bw) {
1007 case BANDWIDTH_8_MHZ:
1008 if (p->frequency < 470000000)
1009 priv->ctrl.vhfbw7 = 0;
1010 else
1011 priv->ctrl.uhfbw8 = 1;
1012 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1013 type |= F8MHZ;
1014 break;
1015 case BANDWIDTH_7_MHZ:
1016 if (p->frequency < 470000000)
1017 priv->ctrl.vhfbw7 = 1;
1018 else
1019 priv->ctrl.uhfbw8 = 0;
1020 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1021 type |= F8MHZ;
1022 break;
1023 case BANDWIDTH_6_MHZ:
1024 type |= DTV6;
1025 priv->ctrl.vhfbw7 = 0;
1026 priv->ctrl.uhfbw8 = 0;
1027 break;
1028 default:
1029 tuner_err("error: bandwidth not supported.\n");
1030 };
1031
1032 /* All S-code tables need a 200kHz shift */
1033 if (priv->ctrl.demod)
1034 demod = priv->ctrl.demod + 200;
1035
1036 return generic_set_freq(fe, p->frequency,
1037 T_DIGITAL_TV, type, 0, demod);
1038}
1039
1040static int xc2028_sleep(struct dvb_frontend *fe)
1041{
1042 struct xc2028_data *priv = fe->tuner_priv;
1043 int rc = 0;
1044
1045 tuner_dbg("%s called\n", __FUNCTION__);
1046
1047 mutex_lock(&priv->lock);
1048
1049 if (priv->firm_version < 0x0202)
1050 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1051 else
1052 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1053
1054 priv->cur_fw.type = 0; /* need firmware reload */
1055
1056 mutex_unlock(&priv->lock);
1057
1058 return rc;
1059}
1060
1061
1062static int xc2028_dvb_release(struct dvb_frontend *fe)
1063{
1064 struct xc2028_data *priv = fe->tuner_priv;
1065
1066 tuner_dbg("%s called\n", __FUNCTION__);
1067
1068 mutex_lock(&xc2028_list_mutex);
1069
1070 priv->count--;
1071
1072 if (!priv->count) {
1073 list_del(&priv->xc2028_list);
1074
1075 kfree(priv->ctrl.fname);
1076
1077 free_firmware(priv);
1078 kfree(priv);
1079 fe->tuner_priv = NULL;
1080 }
1081
1082 mutex_unlock(&xc2028_list_mutex);
1083
1084 return 0;
1085}
1086
1087static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1088{
1089 struct xc2028_data *priv = fe->tuner_priv;
1090
1091 tuner_dbg("%s called\n", __FUNCTION__);
1092
1093 *frequency = priv->frequency;
1094
1095 return 0;
1096}
1097
1098static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
1099{
1100 struct xc2028_data *priv = fe->tuner_priv;
1101 struct xc2028_ctrl *p = priv_cfg;
1102 int rc = 0;
1103
1104 tuner_dbg("%s called\n", __FUNCTION__);
1105
1106 mutex_lock(&priv->lock);
1107
1108 kfree(priv->ctrl.fname);
1109 free_firmware(priv);
1110
1111 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1112 priv->ctrl.fname = NULL;
1113
1114 if (p->fname) {
1115 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1116 if (priv->ctrl.fname == NULL)
1117 rc = -ENOMEM;
1118 }
1119
1120 if (priv->ctrl.max_len < 9)
1121 priv->ctrl.max_len = 13;
1122
1123 mutex_unlock(&priv->lock);
1124
1125 return rc;
1126}
1127
1128static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
1129 .info = {
1130 .name = "Xceive XC3028",
1131 .frequency_min = 42000000,
1132 .frequency_max = 864000000,
1133 .frequency_step = 50000,
1134 },
1135
1136 .set_config = xc2028_set_config,
1137 .set_analog_params = xc2028_set_analog_freq,
1138 .release = xc2028_dvb_release,
1139 .get_frequency = xc2028_get_frequency,
1140 .get_rf_strength = xc2028_signal,
1141 .set_params = xc2028_set_params,
1142 .sleep = xc2028_sleep,
1143
1144};
1145
1146struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1147 struct xc2028_config *cfg)
1148{
1149 struct xc2028_data *priv;
1150 void *video_dev;
1151
1152 if (debug)
1153 printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
1154
1155 if (NULL == cfg || NULL == cfg->video_dev)
1156 return NULL;
1157
1158 if (!fe) {
1159 printk(KERN_ERR PREFIX ": No frontend!\n");
1160 return NULL;
1161 }
1162
1163 video_dev = cfg->video_dev;
1164
1165 mutex_lock(&xc2028_list_mutex);
1166
1167 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
1168 if (priv->video_dev == cfg->video_dev) {
1169 video_dev = NULL;
1170 break;
1171 }
1172 }
1173
1174 if (video_dev) {
1175 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1176 if (priv == NULL) {
1177 mutex_unlock(&xc2028_list_mutex);
1178 return NULL;
1179 }
1180
1181 priv->i2c_props.addr = cfg->i2c_addr;
1182 priv->i2c_props.adap = cfg->i2c_adap;
1183 priv->video_dev = video_dev;
1184 priv->tuner_callback = cfg->callback;
1185 priv->ctrl.max_len = 13;
1186
1187 mutex_init(&priv->lock);
1188
1189 list_add_tail(&priv->xc2028_list, &xc2028_list);
1190 }
1191
1192 fe->tuner_priv = priv;
1193 priv->count++;
1194
1195 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1196 sizeof(xc2028_dvb_tuner_ops));
1197
1198 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1199
1200 if (cfg->ctrl)
1201 xc2028_set_config(fe, cfg->ctrl);
1202
1203 mutex_unlock(&xc2028_list_mutex);
1204
1205 return fe;
1206}
1207
1208EXPORT_SYMBOL(xc2028_attach);
1209
1210MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1211MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1212MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1213MODULE_LICENSE("GPL");
diff --git a/drivers/media/video/tuner-xc2028.h b/drivers/media/video/tuner-xc2028.h
new file mode 100644
index 000000000000..3eb8420379a4
--- /dev/null
+++ b/drivers/media/video/tuner-xc2028.h
@@ -0,0 +1,63 @@
1/* tuner-xc2028
2 *
3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
4 * This code is placed under the terms of the GNU General Public License v2
5 */
6
7#ifndef __TUNER_XC2028_H__
8#define __TUNER_XC2028_H__
9
10#include "dvb_frontend.h"
11
12#define XC2028_DEFAULT_FIRMWARE "xc3028-v27.fw"
13
14/* Dmoduler IF (kHz) */
15#define XC3028_FE_DEFAULT 0
16#define XC3028_FE_LG60 6000
17#define XC3028_FE_ATI638 6380
18#define XC3028_FE_OREN538 5380
19#define XC3028_FE_OREN36 3600
20#define XC3028_FE_TOYOTA388 3880
21#define XC3028_FE_TOYOTA794 7940
22#define XC3028_FE_DIBCOM52 5200
23#define XC3028_FE_ZARLINK456 4560
24#define XC3028_FE_CHINA 5200
25
26struct xc2028_ctrl {
27 char *fname;
28 int max_len;
29 unsigned int scode_table;
30 unsigned int mts :1;
31 unsigned int d2633 :1;
32 unsigned int input1:1;
33 unsigned int vhfbw7:1;
34 unsigned int uhfbw8:1;
35 unsigned int demod;
36};
37
38struct xc2028_config {
39 struct i2c_adapter *i2c_adap;
40 u8 i2c_addr;
41 void *video_dev;
42 struct xc2028_ctrl *ctrl;
43 int (*callback) (void *dev, int command, int arg);
44};
45
46/* xc2028 commands for callback */
47#define XC2028_TUNER_RESET 0
48#define XC2028_RESET_CLK 1
49
50#if defined(CONFIG_TUNER_XC2028) || (defined(CONFIG_TUNER_XC2028_MODULE) && defined(MODULE))
51extern struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
52 struct xc2028_config *cfg);
53#else
54static inline struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
55 struct xc2028_config *cfg)
56{
57 printk(KERN_INFO "%s: not probed - driver disabled by Kconfig\n",
58 __FUNCTION__);
59 return NULL;
60}
61#endif
62
63#endif /* __TUNER_XC2028_H__ */
diff --git a/drivers/media/video/tvaudio.c b/drivers/media/video/tvaudio.c
index a19cdcc17ef7..a75560540e79 100644
--- a/drivers/media/video/tvaudio.c
+++ b/drivers/media/video/tvaudio.c
@@ -31,6 +31,7 @@
31#include <media/tvaudio.h> 31#include <media/tvaudio.h>
32#include <media/v4l2-common.h> 32#include <media/v4l2-common.h>
33#include <media/v4l2-chip-ident.h> 33#include <media/v4l2-chip-ident.h>
34#include <media/v4l2-i2c-drv-legacy.h>
34 35
35#include <media/i2c-addr.h> 36#include <media/i2c-addr.h>
36 37
@@ -109,7 +110,7 @@ static struct CHIPDESC chiplist[];
109 110
110/* current state of the chip */ 111/* current state of the chip */
111struct CHIPSTATE { 112struct CHIPSTATE {
112 struct i2c_client c; 113 struct i2c_client *c;
113 114
114 /* index into CHIPDESC array */ 115 /* index into CHIPDESC array */
115 int type; 116 int type;
@@ -145,10 +146,6 @@ static unsigned short normal_i2c[] = {
145 I2C_CLIENT_END }; 146 I2C_CLIENT_END };
146I2C_CLIENT_INSMOD; 147I2C_CLIENT_INSMOD;
147 148
148static struct i2c_driver driver;
149static struct i2c_client client_template;
150
151
152/* ---------------------------------------------------------------------- */ 149/* ---------------------------------------------------------------------- */
153/* i2c I/O functions */ 150/* i2c I/O functions */
154 151
@@ -157,24 +154,24 @@ static int chip_write(struct CHIPSTATE *chip, int subaddr, int val)
157 unsigned char buffer[2]; 154 unsigned char buffer[2];
158 155
159 if (-1 == subaddr) { 156 if (-1 == subaddr) {
160 v4l_dbg(1, debug, &chip->c, "%s: chip_write: 0x%x\n", 157 v4l_dbg(1, debug, chip->c, "%s: chip_write: 0x%x\n",
161 chip->c.name, val); 158 chip->c->name, val);
162 chip->shadow.bytes[1] = val; 159 chip->shadow.bytes[1] = val;
163 buffer[0] = val; 160 buffer[0] = val;
164 if (1 != i2c_master_send(&chip->c,buffer,1)) { 161 if (1 != i2c_master_send(chip->c,buffer,1)) {
165 v4l_warn(&chip->c, "%s: I/O error (write 0x%x)\n", 162 v4l_warn(chip->c, "%s: I/O error (write 0x%x)\n",
166 chip->c.name, val); 163 chip->c->name, val);
167 return -1; 164 return -1;
168 } 165 }
169 } else { 166 } else {
170 v4l_dbg(1, debug, &chip->c, "%s: chip_write: reg%d=0x%x\n", 167 v4l_dbg(1, debug, chip->c, "%s: chip_write: reg%d=0x%x\n",
171 chip->c.name, subaddr, val); 168 chip->c->name, subaddr, val);
172 chip->shadow.bytes[subaddr+1] = val; 169 chip->shadow.bytes[subaddr+1] = val;
173 buffer[0] = subaddr; 170 buffer[0] = subaddr;
174 buffer[1] = val; 171 buffer[1] = val;
175 if (2 != i2c_master_send(&chip->c,buffer,2)) { 172 if (2 != i2c_master_send(chip->c,buffer,2)) {
176 v4l_warn(&chip->c, "%s: I/O error (write reg%d=0x%x)\n", 173 v4l_warn(chip->c, "%s: I/O error (write reg%d=0x%x)\n",
177 chip->c.name, subaddr, val); 174 chip->c->name, subaddr, val);
178 return -1; 175 return -1;
179 } 176 }
180 } 177 }
@@ -197,12 +194,12 @@ static int chip_read(struct CHIPSTATE *chip)
197{ 194{
198 unsigned char buffer; 195 unsigned char buffer;
199 196
200 if (1 != i2c_master_recv(&chip->c,&buffer,1)) { 197 if (1 != i2c_master_recv(chip->c,&buffer,1)) {
201 v4l_warn(&chip->c, "%s: I/O error (read)\n", 198 v4l_warn(chip->c, "%s: I/O error (read)\n",
202 chip->c.name); 199 chip->c->name);
203 return -1; 200 return -1;
204 } 201 }
205 v4l_dbg(1, debug, &chip->c, "%s: chip_read: 0x%x\n",chip->c.name, buffer); 202 v4l_dbg(1, debug, chip->c, "%s: chip_read: 0x%x\n",chip->c->name, buffer);
206 return buffer; 203 return buffer;
207} 204}
208 205
@@ -211,17 +208,17 @@ static int chip_read2(struct CHIPSTATE *chip, int subaddr)
211 unsigned char write[1]; 208 unsigned char write[1];
212 unsigned char read[1]; 209 unsigned char read[1];
213 struct i2c_msg msgs[2] = { 210 struct i2c_msg msgs[2] = {
214 { chip->c.addr, 0, 1, write }, 211 { chip->c->addr, 0, 1, write },
215 { chip->c.addr, I2C_M_RD, 1, read } 212 { chip->c->addr, I2C_M_RD, 1, read }
216 }; 213 };
217 write[0] = subaddr; 214 write[0] = subaddr;
218 215
219 if (2 != i2c_transfer(chip->c.adapter,msgs,2)) { 216 if (2 != i2c_transfer(chip->c->adapter,msgs,2)) {
220 v4l_warn(&chip->c, "%s: I/O error (read2)\n", chip->c.name); 217 v4l_warn(chip->c, "%s: I/O error (read2)\n", chip->c->name);
221 return -1; 218 return -1;
222 } 219 }
223 v4l_dbg(1, debug, &chip->c, "%s: chip_read2: reg%d=0x%x\n", 220 v4l_dbg(1, debug, chip->c, "%s: chip_read2: reg%d=0x%x\n",
224 chip->c.name, subaddr,read[0]); 221 chip->c->name, subaddr,read[0]);
225 return read[0]; 222 return read[0];
226} 223}
227 224
@@ -233,8 +230,8 @@ static int chip_cmd(struct CHIPSTATE *chip, char *name, audiocmd *cmd)
233 return 0; 230 return 0;
234 231
235 /* update our shadow register set; print bytes if (debug > 0) */ 232 /* update our shadow register set; print bytes if (debug > 0) */
236 v4l_dbg(1, debug, &chip->c, "%s: chip_cmd(%s): reg=%d, data:", 233 v4l_dbg(1, debug, chip->c, "%s: chip_cmd(%s): reg=%d, data:",
237 chip->c.name, name,cmd->bytes[0]); 234 chip->c->name, name,cmd->bytes[0]);
238 for (i = 1; i < cmd->count; i++) { 235 for (i = 1; i < cmd->count; i++) {
239 if (debug) 236 if (debug)
240 printk(" 0x%x",cmd->bytes[i]); 237 printk(" 0x%x",cmd->bytes[i]);
@@ -244,8 +241,8 @@ static int chip_cmd(struct CHIPSTATE *chip, char *name, audiocmd *cmd)
244 printk("\n"); 241 printk("\n");
245 242
246 /* send data to the chip */ 243 /* send data to the chip */
247 if (cmd->count != i2c_master_send(&chip->c,cmd->bytes,cmd->count)) { 244 if (cmd->count != i2c_master_send(chip->c,cmd->bytes,cmd->count)) {
248 v4l_warn(&chip->c, "%s: I/O error (%s)\n", chip->c.name, name); 245 v4l_warn(chip->c, "%s: I/O error (%s)\n", chip->c->name, name);
249 return -1; 246 return -1;
250 } 247 }
251 return 0; 248 return 0;
@@ -269,7 +266,7 @@ static int chip_thread(void *data)
269 struct CHIPSTATE *chip = data; 266 struct CHIPSTATE *chip = data;
270 struct CHIPDESC *desc = chiplist + chip->type; 267 struct CHIPDESC *desc = chiplist + chip->type;
271 268
272 v4l_dbg(1, debug, &chip->c, "%s: thread started\n", chip->c.name); 269 v4l_dbg(1, debug, chip->c, "%s: thread started\n", chip->c->name);
273 set_freezable(); 270 set_freezable();
274 for (;;) { 271 for (;;) {
275 set_current_state(TASK_INTERRUPTIBLE); 272 set_current_state(TASK_INTERRUPTIBLE);
@@ -279,7 +276,7 @@ static int chip_thread(void *data)
279 try_to_freeze(); 276 try_to_freeze();
280 if (kthread_should_stop()) 277 if (kthread_should_stop())
281 break; 278 break;
282 v4l_dbg(1, debug, &chip->c, "%s: thread wakeup\n", chip->c.name); 279 v4l_dbg(1, debug, chip->c, "%s: thread wakeup\n", chip->c->name);
283 280
284 /* don't do anything for radio or if mode != auto */ 281 /* don't do anything for radio or if mode != auto */
285 if (chip->radio || chip->mode != 0) 282 if (chip->radio || chip->mode != 0)
@@ -292,7 +289,7 @@ static int chip_thread(void *data)
292 mod_timer(&chip->wt, jiffies+msecs_to_jiffies(2000)); 289 mod_timer(&chip->wt, jiffies+msecs_to_jiffies(2000));
293 } 290 }
294 291
295 v4l_dbg(1, debug, &chip->c, "%s: thread exiting\n", chip->c.name); 292 v4l_dbg(1, debug, chip->c, "%s: thread exiting\n", chip->c->name);
296 return 0; 293 return 0;
297} 294}
298 295
@@ -304,17 +301,19 @@ static void generic_checkmode(struct CHIPSTATE *chip)
304 if (mode == chip->prevmode) 301 if (mode == chip->prevmode)
305 return; 302 return;
306 303
307 v4l_dbg(1, debug, &chip->c, "%s: thread checkmode\n", chip->c.name); 304 v4l_dbg(1, debug, chip->c, "%s: thread checkmode\n", chip->c->name);
308 chip->prevmode = mode; 305 chip->prevmode = mode;
309 306
310 if (mode & VIDEO_SOUND_STEREO) 307 if (mode & V4L2_TUNER_MODE_STEREO)
311 desc->setmode(chip,VIDEO_SOUND_STEREO); 308 desc->setmode(chip,V4L2_TUNER_MODE_STEREO);
312 else if (mode & VIDEO_SOUND_LANG1) 309 if (mode & V4L2_TUNER_MODE_LANG1_LANG2)
313 desc->setmode(chip,VIDEO_SOUND_LANG1); 310 desc->setmode(chip,V4L2_TUNER_MODE_STEREO);
314 else if (mode & VIDEO_SOUND_LANG2) 311 else if (mode & V4L2_TUNER_MODE_LANG1)
315 desc->setmode(chip,VIDEO_SOUND_LANG2); 312 desc->setmode(chip,V4L2_TUNER_MODE_LANG1);
313 else if (mode & V4L2_TUNER_MODE_LANG2)
314 desc->setmode(chip,V4L2_TUNER_MODE_LANG2);
316 else 315 else
317 desc->setmode(chip,VIDEO_SOUND_MONO); 316 desc->setmode(chip,V4L2_TUNER_MODE_MONO);
318} 317}
319 318
320/* ---------------------------------------------------------------------- */ 319/* ---------------------------------------------------------------------- */
@@ -345,13 +344,13 @@ static int tda9840_getmode(struct CHIPSTATE *chip)
345 int val, mode; 344 int val, mode;
346 345
347 val = chip_read(chip); 346 val = chip_read(chip);
348 mode = VIDEO_SOUND_MONO; 347 mode = V4L2_TUNER_MODE_MONO;
349 if (val & TDA9840_DS_DUAL) 348 if (val & TDA9840_DS_DUAL)
350 mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2; 349 mode |= V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
351 if (val & TDA9840_ST_STEREO) 350 if (val & TDA9840_ST_STEREO)
352 mode |= VIDEO_SOUND_STEREO; 351 mode |= V4L2_TUNER_MODE_STEREO;
353 352
354 v4l_dbg(1, debug, &chip->c, "tda9840_getmode(): raw chip read: %d, return: %d\n", 353 v4l_dbg(1, debug, chip->c, "tda9840_getmode(): raw chip read: %d, return: %d\n",
355 val, mode); 354 val, mode);
356 return mode; 355 return mode;
357} 356}
@@ -362,16 +361,16 @@ static void tda9840_setmode(struct CHIPSTATE *chip, int mode)
362 int t = chip->shadow.bytes[TDA9840_SW + 1] & ~0x7e; 361 int t = chip->shadow.bytes[TDA9840_SW + 1] & ~0x7e;
363 362
364 switch (mode) { 363 switch (mode) {
365 case VIDEO_SOUND_MONO: 364 case V4L2_TUNER_MODE_MONO:
366 t |= TDA9840_MONO; 365 t |= TDA9840_MONO;
367 break; 366 break;
368 case VIDEO_SOUND_STEREO: 367 case V4L2_TUNER_MODE_STEREO:
369 t |= TDA9840_STEREO; 368 t |= TDA9840_STEREO;
370 break; 369 break;
371 case VIDEO_SOUND_LANG1: 370 case V4L2_TUNER_MODE_LANG1:
372 t |= TDA9840_DUALA; 371 t |= TDA9840_DUALA;
373 break; 372 break;
374 case VIDEO_SOUND_LANG2: 373 case V4L2_TUNER_MODE_LANG2:
375 t |= TDA9840_DUALB; 374 t |= TDA9840_DUALB;
376 break; 375 break;
377 default: 376 default:
@@ -502,7 +501,7 @@ static int tda985x_getmode(struct CHIPSTATE *chip)
502 chip_read(chip)) >> 4; 501 chip_read(chip)) >> 4;
503 /* Add mono mode regardless of SAP and stereo */ 502 /* Add mono mode regardless of SAP and stereo */
504 /* Allows forced mono */ 503 /* Allows forced mono */
505 return mode | VIDEO_SOUND_MONO; 504 return mode | V4L2_TUNER_MODE_MONO;
506} 505}
507 506
508static void tda985x_setmode(struct CHIPSTATE *chip, int mode) 507static void tda985x_setmode(struct CHIPSTATE *chip, int mode)
@@ -511,13 +510,13 @@ static void tda985x_setmode(struct CHIPSTATE *chip, int mode)
511 int c6 = chip->shadow.bytes[TDA985x_C6+1] & 0x3f; 510 int c6 = chip->shadow.bytes[TDA985x_C6+1] & 0x3f;
512 511
513 switch (mode) { 512 switch (mode) {
514 case VIDEO_SOUND_MONO: 513 case V4L2_TUNER_MODE_MONO:
515 c6 |= TDA985x_MONO; 514 c6 |= TDA985x_MONO;
516 break; 515 break;
517 case VIDEO_SOUND_STEREO: 516 case V4L2_TUNER_MODE_STEREO:
518 c6 |= TDA985x_STEREO; 517 c6 |= TDA985x_STEREO;
519 break; 518 break;
520 case VIDEO_SOUND_LANG1: 519 case V4L2_TUNER_MODE_LANG1:
521 c6 |= TDA985x_SAP; 520 c6 |= TDA985x_SAP;
522 break; 521 break;
523 default: 522 default:
@@ -650,12 +649,12 @@ static int tda9873_getmode(struct CHIPSTATE *chip)
650 int val,mode; 649 int val,mode;
651 650
652 val = chip_read(chip); 651 val = chip_read(chip);
653 mode = VIDEO_SOUND_MONO; 652 mode = V4L2_TUNER_MODE_MONO;
654 if (val & TDA9873_STEREO) 653 if (val & TDA9873_STEREO)
655 mode |= VIDEO_SOUND_STEREO; 654 mode |= V4L2_TUNER_MODE_STEREO;
656 if (val & TDA9873_DUAL) 655 if (val & TDA9873_DUAL)
657 mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2; 656 mode |= V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
658 v4l_dbg(1, debug, &chip->c, "tda9873_getmode(): raw chip read: %d, return: %d\n", 657 v4l_dbg(1, debug, chip->c, "tda9873_getmode(): raw chip read: %d, return: %d\n",
659 val, mode); 658 val, mode);
660 return mode; 659 return mode;
661} 660}
@@ -666,24 +665,24 @@ static void tda9873_setmode(struct CHIPSTATE *chip, int mode)
666 /* int adj_data = chip->shadow.bytes[TDA9873_AD+1] ; */ 665 /* int adj_data = chip->shadow.bytes[TDA9873_AD+1] ; */
667 666
668 if ((sw_data & TDA9873_INP_MASK) != TDA9873_INTERNAL) { 667 if ((sw_data & TDA9873_INP_MASK) != TDA9873_INTERNAL) {
669 v4l_dbg(1, debug, &chip->c, "tda9873_setmode(): external input\n"); 668 v4l_dbg(1, debug, chip->c, "tda9873_setmode(): external input\n");
670 return; 669 return;
671 } 670 }
672 671
673 v4l_dbg(1, debug, &chip->c, "tda9873_setmode(): chip->shadow.bytes[%d] = %d\n", TDA9873_SW+1, chip->shadow.bytes[TDA9873_SW+1]); 672 v4l_dbg(1, debug, chip->c, "tda9873_setmode(): chip->shadow.bytes[%d] = %d\n", TDA9873_SW+1, chip->shadow.bytes[TDA9873_SW+1]);
674 v4l_dbg(1, debug, &chip->c, "tda9873_setmode(): sw_data = %d\n", sw_data); 673 v4l_dbg(1, debug, chip->c, "tda9873_setmode(): sw_data = %d\n", sw_data);
675 674
676 switch (mode) { 675 switch (mode) {
677 case VIDEO_SOUND_MONO: 676 case V4L2_TUNER_MODE_MONO:
678 sw_data |= TDA9873_TR_MONO; 677 sw_data |= TDA9873_TR_MONO;
679 break; 678 break;
680 case VIDEO_SOUND_STEREO: 679 case V4L2_TUNER_MODE_STEREO:
681 sw_data |= TDA9873_TR_STEREO; 680 sw_data |= TDA9873_TR_STEREO;
682 break; 681 break;
683 case VIDEO_SOUND_LANG1: 682 case V4L2_TUNER_MODE_LANG1:
684 sw_data |= TDA9873_TR_DUALA; 683 sw_data |= TDA9873_TR_DUALA;
685 break; 684 break;
686 case VIDEO_SOUND_LANG2: 685 case V4L2_TUNER_MODE_LANG2:
687 sw_data |= TDA9873_TR_DUALB; 686 sw_data |= TDA9873_TR_DUALB;
688 break; 687 break;
689 default: 688 default:
@@ -692,7 +691,7 @@ static void tda9873_setmode(struct CHIPSTATE *chip, int mode)
692 } 691 }
693 692
694 chip_write(chip, TDA9873_SW, sw_data); 693 chip_write(chip, TDA9873_SW, sw_data);
695 v4l_dbg(1, debug, &chip->c, "tda9873_setmode(): req. mode %d; chip_write: %d\n", 694 v4l_dbg(1, debug, chip->c, "tda9873_setmode(): req. mode %d; chip_write: %d\n",
696 mode, sw_data); 695 mode, sw_data);
697} 696}
698 697
@@ -831,7 +830,7 @@ static int tda9874a_setup(struct CHIPSTATE *chip)
831 chip_write(chip, TDA9874A_SDACOSR, (tda9874a_mode) ? 0x81:0x80); 830 chip_write(chip, TDA9874A_SDACOSR, (tda9874a_mode) ? 0x81:0x80);
832 chip_write(chip, TDA9874A_AOSR, 0x00); /* or 0x10 */ 831 chip_write(chip, TDA9874A_AOSR, 0x00); /* or 0x10 */
833 } 832 }
834 v4l_dbg(1, debug, &chip->c, "tda9874a_setup(): %s [0x%02X].\n", 833 v4l_dbg(1, debug, chip->c, "tda9874a_setup(): %s [0x%02X].\n",
835 tda9874a_modelist[tda9874a_STD].name,tda9874a_STD); 834 tda9874a_modelist[tda9874a_STD].name,tda9874a_STD);
836 return 1; 835 return 1;
837} 836}
@@ -841,7 +840,7 @@ static int tda9874a_getmode(struct CHIPSTATE *chip)
841 int dsr,nsr,mode; 840 int dsr,nsr,mode;
842 int necr; /* just for debugging */ 841 int necr; /* just for debugging */
843 842
844 mode = VIDEO_SOUND_MONO; 843 mode = V4L2_TUNER_MODE_MONO;
845 844
846 if(-1 == (dsr = chip_read2(chip,TDA9874A_DSR))) 845 if(-1 == (dsr = chip_read2(chip,TDA9874A_DSR)))
847 return mode; 846 return mode;
@@ -860,21 +859,21 @@ static int tda9874a_getmode(struct CHIPSTATE *chip)
860 * that sound has (temporarily) switched from NICAM to 859 * that sound has (temporarily) switched from NICAM to
861 * mono FM (or AM) on 1st sound carrier due to high NICAM bit 860 * mono FM (or AM) on 1st sound carrier due to high NICAM bit
862 * error count. So in fact there is no stereo in this case :-( 861 * error count. So in fact there is no stereo in this case :-(
863 * But changing the mode to VIDEO_SOUND_MONO would switch 862 * But changing the mode to V4L2_TUNER_MODE_MONO would switch
864 * external 4052 multiplexer in audio_hook(). 863 * external 4052 multiplexer in audio_hook().
865 */ 864 */
866 if(nsr & 0x02) /* NSR.S/MB=1 */ 865 if(nsr & 0x02) /* NSR.S/MB=1 */
867 mode |= VIDEO_SOUND_STEREO; 866 mode |= V4L2_TUNER_MODE_STEREO;
868 if(nsr & 0x01) /* NSR.D/SB=1 */ 867 if(nsr & 0x01) /* NSR.D/SB=1 */
869 mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2; 868 mode |= V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
870 } else { 869 } else {
871 if(dsr & 0x02) /* DSR.IDSTE=1 */ 870 if(dsr & 0x02) /* DSR.IDSTE=1 */
872 mode |= VIDEO_SOUND_STEREO; 871 mode |= V4L2_TUNER_MODE_STEREO;
873 if(dsr & 0x04) /* DSR.IDDUA=1 */ 872 if(dsr & 0x04) /* DSR.IDDUA=1 */
874 mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2; 873 mode |= V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
875 } 874 }
876 875
877 v4l_dbg(1, debug, &chip->c, "tda9874a_getmode(): DSR=0x%X, NSR=0x%X, NECR=0x%X, return: %d.\n", 876 v4l_dbg(1, debug, chip->c, "tda9874a_getmode(): DSR=0x%X, NSR=0x%X, NECR=0x%X, return: %d.\n",
878 dsr, nsr, necr, mode); 877 dsr, nsr, necr, mode);
879 return mode; 878 return mode;
880} 879}
@@ -902,14 +901,14 @@ static void tda9874a_setmode(struct CHIPSTATE *chip, int mode)
902 int mdacosr = (tda9874a_mode) ? 0x82:0x80; 901 int mdacosr = (tda9874a_mode) ? 0x82:0x80;
903 902
904 switch(mode) { 903 switch(mode) {
905 case VIDEO_SOUND_MONO: 904 case V4L2_TUNER_MODE_MONO:
906 case VIDEO_SOUND_STEREO: 905 case V4L2_TUNER_MODE_STEREO:
907 break; 906 break;
908 case VIDEO_SOUND_LANG1: 907 case V4L2_TUNER_MODE_LANG1:
909 aosr = 0x80; /* auto-select, dual A/A */ 908 aosr = 0x80; /* auto-select, dual A/A */
910 mdacosr = (tda9874a_mode) ? 0x82:0x80; 909 mdacosr = (tda9874a_mode) ? 0x82:0x80;
911 break; 910 break;
912 case VIDEO_SOUND_LANG2: 911 case V4L2_TUNER_MODE_LANG2:
913 aosr = 0xa0; /* auto-select, dual B/B */ 912 aosr = 0xa0; /* auto-select, dual B/B */
914 mdacosr = (tda9874a_mode) ? 0x83:0x81; 913 mdacosr = (tda9874a_mode) ? 0x83:0x81;
915 break; 914 break;
@@ -920,18 +919,18 @@ static void tda9874a_setmode(struct CHIPSTATE *chip, int mode)
920 chip_write(chip, TDA9874A_AOSR, aosr); 919 chip_write(chip, TDA9874A_AOSR, aosr);
921 chip_write(chip, TDA9874A_MDACOSR, mdacosr); 920 chip_write(chip, TDA9874A_MDACOSR, mdacosr);
922 921
923 v4l_dbg(1, debug, &chip->c, "tda9874a_setmode(): req. mode %d; AOSR=0x%X, MDACOSR=0x%X.\n", 922 v4l_dbg(1, debug, chip->c, "tda9874a_setmode(): req. mode %d; AOSR=0x%X, MDACOSR=0x%X.\n",
924 mode, aosr, mdacosr); 923 mode, aosr, mdacosr);
925 924
926 } else { /* dic == 0x07 */ 925 } else { /* dic == 0x07 */
927 int fmmr,aosr; 926 int fmmr,aosr;
928 927
929 switch(mode) { 928 switch(mode) {
930 case VIDEO_SOUND_MONO: 929 case V4L2_TUNER_MODE_MONO:
931 fmmr = 0x00; /* mono */ 930 fmmr = 0x00; /* mono */
932 aosr = 0x10; /* A/A */ 931 aosr = 0x10; /* A/A */
933 break; 932 break;
934 case VIDEO_SOUND_STEREO: 933 case V4L2_TUNER_MODE_STEREO:
935 if(tda9874a_mode) { 934 if(tda9874a_mode) {
936 fmmr = 0x00; 935 fmmr = 0x00;
937 aosr = 0x00; /* handled by NICAM auto-mute */ 936 aosr = 0x00; /* handled by NICAM auto-mute */
@@ -940,11 +939,11 @@ static void tda9874a_setmode(struct CHIPSTATE *chip, int mode)
940 aosr = 0x00; 939 aosr = 0x00;
941 } 940 }
942 break; 941 break;
943 case VIDEO_SOUND_LANG1: 942 case V4L2_TUNER_MODE_LANG1:
944 fmmr = 0x02; /* dual */ 943 fmmr = 0x02; /* dual */
945 aosr = 0x10; /* dual A/A */ 944 aosr = 0x10; /* dual A/A */
946 break; 945 break;
947 case VIDEO_SOUND_LANG2: 946 case V4L2_TUNER_MODE_LANG2:
948 fmmr = 0x02; /* dual */ 947 fmmr = 0x02; /* dual */
949 aosr = 0x20; /* dual B/B */ 948 aosr = 0x20; /* dual B/B */
950 break; 949 break;
@@ -955,7 +954,7 @@ static void tda9874a_setmode(struct CHIPSTATE *chip, int mode)
955 chip_write(chip, TDA9874A_FMMR, fmmr); 954 chip_write(chip, TDA9874A_FMMR, fmmr);
956 chip_write(chip, TDA9874A_AOSR, aosr); 955 chip_write(chip, TDA9874A_AOSR, aosr);
957 956
958 v4l_dbg(1, debug, &chip->c, "tda9874a_setmode(): req. mode %d; FMMR=0x%X, AOSR=0x%X.\n", 957 v4l_dbg(1, debug, chip->c, "tda9874a_setmode(): req. mode %d; FMMR=0x%X, AOSR=0x%X.\n",
959 mode, fmmr, aosr); 958 mode, fmmr, aosr);
960 } 959 }
961} 960}
@@ -969,10 +968,10 @@ static int tda9874a_checkit(struct CHIPSTATE *chip)
969 if(-1 == (sic = chip_read2(chip,TDA9874A_SIC))) 968 if(-1 == (sic = chip_read2(chip,TDA9874A_SIC)))
970 return 0; 969 return 0;
971 970
972 v4l_dbg(1, debug, &chip->c, "tda9874a_checkit(): DIC=0x%X, SIC=0x%X.\n", dic, sic); 971 v4l_dbg(1, debug, chip->c, "tda9874a_checkit(): DIC=0x%X, SIC=0x%X.\n", dic, sic);
973 972
974 if((dic == 0x11)||(dic == 0x07)) { 973 if((dic == 0x11)||(dic == 0x07)) {
975 v4l_info(&chip->c, "found tda9874%s.\n", (dic == 0x11) ? "a":"h"); 974 v4l_info(chip->c, "found tda9874%s.\n", (dic == 0x11) ? "a":"h");
976 tda9874a_dic = dic; /* remember device id. */ 975 tda9874a_dic = dic; /* remember device id. */
977 return 1; 976 return 1;
978 } 977 }
@@ -1095,7 +1094,7 @@ static int tda8425_initialize(struct CHIPSTATE *chip)
1095 int inputmap[4] = { /* tuner */ TDA8425_S1_CH2, /* radio */ TDA8425_S1_CH1, 1094 int inputmap[4] = { /* tuner */ TDA8425_S1_CH2, /* radio */ TDA8425_S1_CH1,
1096 /* extern */ TDA8425_S1_CH1, /* intern */ TDA8425_S1_OFF}; 1095 /* extern */ TDA8425_S1_CH1, /* intern */ TDA8425_S1_OFF};
1097 1096
1098 if (chip->c.adapter->id == I2C_HW_B_RIVA) { 1097 if (chip->c->adapter->id == I2C_HW_B_RIVA) {
1099 memcpy (desc->inputmap, inputmap, sizeof (inputmap)); 1098 memcpy (desc->inputmap, inputmap, sizeof (inputmap));
1100 } 1099 }
1101 return 0; 1100 return 0;
@@ -1105,20 +1104,20 @@ static void tda8425_setmode(struct CHIPSTATE *chip, int mode)
1105{ 1104{
1106 int s1 = chip->shadow.bytes[TDA8425_S1+1] & 0xe1; 1105 int s1 = chip->shadow.bytes[TDA8425_S1+1] & 0xe1;
1107 1106
1108 if (mode & VIDEO_SOUND_LANG1) { 1107 if (mode & V4L2_TUNER_MODE_LANG1) {
1109 s1 |= TDA8425_S1_ML_SOUND_A; 1108 s1 |= TDA8425_S1_ML_SOUND_A;
1110 s1 |= TDA8425_S1_STEREO_PSEUDO; 1109 s1 |= TDA8425_S1_STEREO_PSEUDO;
1111 1110
1112 } else if (mode & VIDEO_SOUND_LANG2) { 1111 } else if (mode & V4L2_TUNER_MODE_LANG2) {
1113 s1 |= TDA8425_S1_ML_SOUND_B; 1112 s1 |= TDA8425_S1_ML_SOUND_B;
1114 s1 |= TDA8425_S1_STEREO_PSEUDO; 1113 s1 |= TDA8425_S1_STEREO_PSEUDO;
1115 1114
1116 } else { 1115 } else {
1117 s1 |= TDA8425_S1_ML_STEREO; 1116 s1 |= TDA8425_S1_ML_STEREO;
1118 1117
1119 if (mode & VIDEO_SOUND_MONO) 1118 if (mode & V4L2_TUNER_MODE_MONO)
1120 s1 |= TDA8425_S1_STEREO_MONO; 1119 s1 |= TDA8425_S1_STEREO_MONO;
1121 if (mode & VIDEO_SOUND_STEREO) 1120 if (mode & V4L2_TUNER_MODE_STEREO)
1122 s1 |= TDA8425_S1_STEREO_SPATIAL; 1121 s1 |= TDA8425_S1_STEREO_SPATIAL;
1123 } 1122 }
1124 chip_write(chip,TDA8425_S1,s1); 1123 chip_write(chip,TDA8425_S1,s1);
@@ -1177,13 +1176,13 @@ static int ta8874z_getmode(struct CHIPSTATE *chip)
1177 int val, mode; 1176 int val, mode;
1178 1177
1179 val = chip_read(chip); 1178 val = chip_read(chip);
1180 mode = VIDEO_SOUND_MONO; 1179 mode = V4L2_TUNER_MODE_MONO;
1181 if (val & TA8874Z_B1){ 1180 if (val & TA8874Z_B1){
1182 mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2; 1181 mode |= V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
1183 }else if (!(val & TA8874Z_B0)){ 1182 }else if (!(val & TA8874Z_B0)){
1184 mode |= VIDEO_SOUND_STEREO; 1183 mode |= V4L2_TUNER_MODE_STEREO;
1185 } 1184 }
1186 /* v4l_dbg(1, debug, &chip->c, "ta8874z_getmode(): raw chip read: 0x%02x, return: 0x%02x\n", val, mode); */ 1185 /* v4l_dbg(1, debug, chip->c, "ta8874z_getmode(): raw chip read: 0x%02x, return: 0x%02x\n", val, mode); */
1187 return mode; 1186 return mode;
1188} 1187}
1189 1188
@@ -1196,19 +1195,19 @@ static void ta8874z_setmode(struct CHIPSTATE *chip, int mode)
1196{ 1195{
1197 int update = 1; 1196 int update = 1;
1198 audiocmd *t = NULL; 1197 audiocmd *t = NULL;
1199 v4l_dbg(1, debug, &chip->c, "ta8874z_setmode(): mode: 0x%02x\n", mode); 1198 v4l_dbg(1, debug, chip->c, "ta8874z_setmode(): mode: 0x%02x\n", mode);
1200 1199
1201 switch(mode){ 1200 switch(mode){
1202 case VIDEO_SOUND_MONO: 1201 case V4L2_TUNER_MODE_MONO:
1203 t = &ta8874z_mono; 1202 t = &ta8874z_mono;
1204 break; 1203 break;
1205 case VIDEO_SOUND_STEREO: 1204 case V4L2_TUNER_MODE_STEREO:
1206 t = &ta8874z_stereo; 1205 t = &ta8874z_stereo;
1207 break; 1206 break;
1208 case VIDEO_SOUND_LANG1: 1207 case V4L2_TUNER_MODE_LANG1:
1209 t = &ta8874z_main; 1208 t = &ta8874z_main;
1210 break; 1209 break;
1211 case VIDEO_SOUND_LANG2: 1210 case V4L2_TUNER_MODE_LANG2:
1212 t = &ta8874z_sub; 1211 t = &ta8874z_sub;
1213 break; 1212 break;
1214 default: 1213 default:
@@ -1462,51 +1461,55 @@ static struct CHIPDESC chiplist[] = {
1462/* ---------------------------------------------------------------------- */ 1461/* ---------------------------------------------------------------------- */
1463/* i2c registration */ 1462/* i2c registration */
1464 1463
1465static int chip_attach(struct i2c_adapter *adap, int addr, int kind) 1464static int chip_probe(struct i2c_client *client)
1466{ 1465{
1467 struct CHIPSTATE *chip; 1466 struct CHIPSTATE *chip;
1468 struct CHIPDESC *desc; 1467 struct CHIPDESC *desc;
1469 1468
1469 if (debug) {
1470 printk(KERN_INFO "tvaudio: TV audio decoder + audio/video mux driver\n");
1471 printk(KERN_INFO "tvaudio: known chips: ");
1472 for (desc = chiplist; desc->name != NULL; desc++)
1473 printk("%s%s", (desc == chiplist) ? "" : ", ", desc->name);
1474 printk("\n");
1475 }
1476
1470 chip = kzalloc(sizeof(*chip),GFP_KERNEL); 1477 chip = kzalloc(sizeof(*chip),GFP_KERNEL);
1471 if (!chip) 1478 if (!chip)
1472 return -ENOMEM; 1479 return -ENOMEM;
1473 memcpy(&chip->c,&client_template,sizeof(struct i2c_client)); 1480 chip->c = client;
1474 chip->c.adapter = adap; 1481 i2c_set_clientdata(client, chip);
1475 chip->c.addr = addr;
1476 i2c_set_clientdata(&chip->c, chip);
1477 1482
1478 /* find description for the chip */ 1483 /* find description for the chip */
1479 v4l_dbg(1, debug, &chip->c, "chip found @ 0x%x\n", addr<<1); 1484 v4l_dbg(1, debug, client, "chip found @ 0x%x\n", client->addr<<1);
1480 for (desc = chiplist; desc->name != NULL; desc++) { 1485 for (desc = chiplist; desc->name != NULL; desc++) {
1481 if (0 == *(desc->insmodopt)) 1486 if (0 == *(desc->insmodopt))
1482 continue; 1487 continue;
1483 if (addr < desc->addr_lo || 1488 if (client->addr < desc->addr_lo ||
1484 addr > desc->addr_hi) 1489 client->addr > desc->addr_hi)
1485 continue; 1490 continue;
1486 if (desc->checkit && !desc->checkit(chip)) 1491 if (desc->checkit && !desc->checkit(chip))
1487 continue; 1492 continue;
1488 break; 1493 break;
1489 } 1494 }
1490 if (desc->name == NULL) { 1495 if (desc->name == NULL) {
1491 v4l_dbg(1, debug, &chip->c, "no matching chip description found\n"); 1496 v4l_dbg(1, debug, client, "no matching chip description found\n");
1492 return -EIO; 1497 return -EIO;
1493 } 1498 }
1494 v4l_info(&chip->c, "%s found @ 0x%x (%s)\n", desc->name, addr<<1, adap->name); 1499 v4l_info(client, "%s found @ 0x%x (%s)\n", desc->name, client->addr<<1, client->adapter->name);
1495 if (desc->flags) { 1500 if (desc->flags) {
1496 v4l_dbg(1, debug, &chip->c, "matches:%s%s%s.\n", 1501 v4l_dbg(1, debug, client, "matches:%s%s%s.\n",
1497 (desc->flags & CHIP_HAS_VOLUME) ? " volume" : "", 1502 (desc->flags & CHIP_HAS_VOLUME) ? " volume" : "",
1498 (desc->flags & CHIP_HAS_BASSTREBLE) ? " bass/treble" : "", 1503 (desc->flags & CHIP_HAS_BASSTREBLE) ? " bass/treble" : "",
1499 (desc->flags & CHIP_HAS_INPUTSEL) ? " audiomux" : ""); 1504 (desc->flags & CHIP_HAS_INPUTSEL) ? " audiomux" : "");
1500 } 1505 }
1501 1506
1502 /* fill required data structures */ 1507 /* fill required data structures */
1503 strcpy(chip->c.name, desc->name); 1508 strcpy(client->name, desc->name);
1504 chip->type = desc-chiplist; 1509 chip->type = desc-chiplist;
1505 chip->shadow.count = desc->registers+1; 1510 chip->shadow.count = desc->registers+1;
1506 chip->prevmode = -1; 1511 chip->prevmode = -1;
1507 chip->audmode = V4L2_TUNER_MODE_LANG1; 1512 chip->audmode = V4L2_TUNER_MODE_LANG1;
1508 /* register */
1509 i2c_attach_client(&chip->c);
1510 1513
1511 /* initialization */ 1514 /* initialization */
1512 if (desc->initialize != NULL) 1515 if (desc->initialize != NULL)
@@ -1533,28 +1536,17 @@ static int chip_attach(struct i2c_adapter *adap, int addr, int kind)
1533 init_timer(&chip->wt); 1536 init_timer(&chip->wt);
1534 chip->wt.function = chip_thread_wake; 1537 chip->wt.function = chip_thread_wake;
1535 chip->wt.data = (unsigned long)chip; 1538 chip->wt.data = (unsigned long)chip;
1536 chip->thread = kthread_run(chip_thread, chip, chip->c.name); 1539 chip->thread = kthread_run(chip_thread, chip, chip->c->name);
1537 if (IS_ERR(chip->thread)) { 1540 if (IS_ERR(chip->thread)) {
1538 v4l_warn(&chip->c, "%s: failed to create kthread\n", 1541 v4l_warn(chip->c, "%s: failed to create kthread\n",
1539 chip->c.name); 1542 chip->c->name);
1540 chip->thread = NULL; 1543 chip->thread = NULL;
1541 } 1544 }
1542 } 1545 }
1543 return 0; 1546 return 0;
1544} 1547}
1545 1548
1546static int chip_probe(struct i2c_adapter *adap) 1549static int chip_remove(struct i2c_client *client)
1547{
1548 /* don't attach on saa7146 based cards,
1549 because dedicated drivers are used */
1550 if ((adap->id == I2C_HW_SAA7146))
1551 return 0;
1552 if (adap->class & I2C_CLASS_TV_ANALOG)
1553 return i2c_probe(adap, &addr_data, chip_attach);
1554 return 0;
1555}
1556
1557static int chip_detach(struct i2c_client *client)
1558{ 1550{
1559 struct CHIPSTATE *chip = i2c_get_clientdata(client); 1551 struct CHIPSTATE *chip = i2c_get_clientdata(client);
1560 1552
@@ -1565,12 +1557,52 @@ static int chip_detach(struct i2c_client *client)
1565 chip->thread = NULL; 1557 chip->thread = NULL;
1566 } 1558 }
1567 1559
1568 i2c_detach_client(&chip->c);
1569 kfree(chip); 1560 kfree(chip);
1570 return 0; 1561 return 0;
1571} 1562}
1572 1563
1573static int tvaudio_set_ctrl(struct CHIPSTATE *chip, struct v4l2_control *ctrl) 1564static int tvaudio_get_ctrl(struct CHIPSTATE *chip,
1565 struct v4l2_control *ctrl)
1566{
1567 struct CHIPDESC *desc = chiplist + chip->type;
1568
1569 switch (ctrl->id) {
1570 case V4L2_CID_AUDIO_MUTE:
1571 ctrl->value=chip->muted;
1572 return 0;
1573 case V4L2_CID_AUDIO_VOLUME:
1574 if (!desc->flags & CHIP_HAS_VOLUME)
1575 break;
1576 ctrl->value = max(chip->left,chip->right);
1577 return 0;
1578 case V4L2_CID_AUDIO_BALANCE:
1579 {
1580 int volume;
1581 if (!desc->flags & CHIP_HAS_VOLUME)
1582 break;
1583 volume = max(chip->left,chip->right);
1584 if (volume)
1585 ctrl->value=(32768*min(chip->left,chip->right))/volume;
1586 else
1587 ctrl->value=32768;
1588 return 0;
1589 }
1590 case V4L2_CID_AUDIO_BASS:
1591 if (desc->flags & CHIP_HAS_BASSTREBLE)
1592 break;
1593 ctrl->value = chip->bass;
1594 return 0;
1595 case V4L2_CID_AUDIO_TREBLE:
1596 if (desc->flags & CHIP_HAS_BASSTREBLE)
1597 return -EINVAL;
1598 ctrl->value = chip->treble;
1599 return 0;
1600 }
1601 return -EINVAL;
1602}
1603
1604static int tvaudio_set_ctrl(struct CHIPSTATE *chip,
1605 struct v4l2_control *ctrl)
1574{ 1606{
1575 struct CHIPDESC *desc = chiplist + chip->type; 1607 struct CHIPDESC *desc = chiplist + chip->type;
1576 1608
@@ -1584,11 +1616,60 @@ static int tvaudio_set_ctrl(struct CHIPSTATE *chip, struct v4l2_control *ctrl)
1584 else 1616 else
1585 chip_write_masked(chip,desc->inputreg, 1617 chip_write_masked(chip,desc->inputreg,
1586 desc->inputmap[chip->input],desc->inputmask); 1618 desc->inputmap[chip->input],desc->inputmask);
1587 break; 1619 return 0;
1588 default: 1620 case V4L2_CID_AUDIO_VOLUME:
1589 return -EINVAL; 1621 {
1622 int volume,balance;
1623
1624 if (!desc->flags & CHIP_HAS_VOLUME)
1625 break;
1626
1627 volume = max(chip->left,chip->right);
1628 if (volume)
1629 balance=(32768*min(chip->left,chip->right))/volume;
1630 else
1631 balance=32768;
1632
1633 volume=ctrl->value;
1634 chip->left = (min(65536 - balance,32768) * volume) / 32768;
1635 chip->right = (min(balance,volume *(__u16)32768)) / 32768;
1636
1637 chip_write(chip,desc->leftreg,desc->volfunc(chip->left));
1638 chip_write(chip,desc->rightreg,desc->volfunc(chip->right));
1639
1640 return 0;
1590 } 1641 }
1591 return 0; 1642 case V4L2_CID_AUDIO_BALANCE:
1643 {
1644 int volume, balance;
1645 if (!desc->flags & CHIP_HAS_VOLUME)
1646 break;
1647
1648 volume = max(chip->left,chip->right);
1649 balance = ctrl->value;
1650
1651 chip_write(chip,desc->leftreg,desc->volfunc(chip->left));
1652 chip_write(chip,desc->rightreg,desc->volfunc(chip->right));
1653
1654 return 0;
1655 }
1656 case V4L2_CID_AUDIO_BASS:
1657 if (desc->flags & CHIP_HAS_BASSTREBLE)
1658 break;
1659 chip->bass = ctrl->value;
1660 chip_write(chip,desc->bassreg,desc->bassfunc(chip->bass));
1661
1662 return 0;
1663 case V4L2_CID_AUDIO_TREBLE:
1664 if (desc->flags & CHIP_HAS_BASSTREBLE)
1665 return -EINVAL;
1666
1667 chip->treble = ctrl->value;
1668 chip_write(chip,desc->treblereg,desc->treblefunc(chip->treble));
1669
1670 return 0;
1671 }
1672 return -EINVAL;
1592} 1673}
1593 1674
1594 1675
@@ -1601,7 +1682,7 @@ static int chip_command(struct i2c_client *client,
1601 struct CHIPSTATE *chip = i2c_get_clientdata(client); 1682 struct CHIPSTATE *chip = i2c_get_clientdata(client);
1602 struct CHIPDESC *desc = chiplist + chip->type; 1683 struct CHIPDESC *desc = chiplist + chip->type;
1603 1684
1604 v4l_dbg(1, debug, &chip->c, "%s: chip_command 0x%x\n", chip->c.name, cmd); 1685 v4l_dbg(1, debug, chip->c, "%s: chip_command 0x%x\n", chip->c->name, cmd);
1605 1686
1606 switch (cmd) { 1687 switch (cmd) {
1607 case AUDC_SET_RADIO: 1688 case AUDC_SET_RADIO:
@@ -1609,67 +1690,36 @@ static int chip_command(struct i2c_client *client,
1609 chip->watch_stereo = 0; 1690 chip->watch_stereo = 0;
1610 /* del_timer(&chip->wt); */ 1691 /* del_timer(&chip->wt); */
1611 break; 1692 break;
1612
1613 /* --- v4l ioctls --- */ 1693 /* --- v4l ioctls --- */
1614 /* take care: bttv does userspace copying, we'll get a 1694 /* take care: bttv does userspace copying, we'll get a
1615 kernel pointer here... */ 1695 kernel pointer here... */
1616 case VIDIOCGAUDIO: 1696 case VIDIOC_QUERYCTRL:
1617 {
1618 struct video_audio *va = arg;
1619
1620 if (desc->flags & CHIP_HAS_VOLUME) {
1621 va->flags |= VIDEO_AUDIO_VOLUME;
1622 va->volume = max(chip->left,chip->right);
1623 if (va->volume)
1624 va->balance = (32768*min(chip->left,chip->right))/
1625 va->volume;
1626 else
1627 va->balance = 32768;
1628 }
1629 if (desc->flags & CHIP_HAS_BASSTREBLE) {
1630 va->flags |= VIDEO_AUDIO_BASS | VIDEO_AUDIO_TREBLE;
1631 va->bass = chip->bass;
1632 va->treble = chip->treble;
1633 }
1634 if (!chip->radio) {
1635 if (desc->getmode)
1636 va->mode = desc->getmode(chip);
1637 else
1638 va->mode = VIDEO_SOUND_MONO;
1639 }
1640 break;
1641 }
1642
1643 case VIDIOCSAUDIO:
1644 { 1697 {
1645 struct video_audio *va = arg; 1698 struct v4l2_queryctrl *qc = arg;
1646 1699
1647 if (desc->flags & CHIP_HAS_VOLUME) { 1700 switch (qc->id) {
1648 chip->left = (min(65536 - va->balance,32768) * 1701 case V4L2_CID_AUDIO_MUTE:
1649 va->volume) / 32768; 1702 break;
1650 chip->right = (min(va->balance,(__u16)32768) * 1703 case V4L2_CID_AUDIO_VOLUME:
1651 va->volume) / 32768; 1704 case V4L2_CID_AUDIO_BALANCE:
1652 chip_write(chip,desc->leftreg,desc->volfunc(chip->left)); 1705 if (!desc->flags & CHIP_HAS_VOLUME)
1653 chip_write(chip,desc->rightreg,desc->volfunc(chip->right)); 1706 return -EINVAL;
1654 } 1707 break;
1655 if (desc->flags & CHIP_HAS_BASSTREBLE) { 1708 case V4L2_CID_AUDIO_BASS:
1656 chip->bass = va->bass; 1709 case V4L2_CID_AUDIO_TREBLE:
1657 chip->treble = va->treble; 1710 if (desc->flags & CHIP_HAS_BASSTREBLE)
1658 chip_write(chip,desc->bassreg,desc->bassfunc(chip->bass)); 1711 return -EINVAL;
1659 chip_write(chip,desc->treblereg,desc->treblefunc(chip->treble)); 1712 break;
1660 } 1713 default:
1661 if (desc->setmode && va->mode) { 1714 return -EINVAL;
1662 chip->watch_stereo = 0;
1663 /* del_timer(&chip->wt); */
1664 chip->mode = va->mode;
1665 desc->setmode(chip,va->mode);
1666 } 1715 }
1667 break; 1716 return v4l2_ctrl_query_fill_std(qc);
1668 } 1717 }
1669
1670 case VIDIOC_S_CTRL: 1718 case VIDIOC_S_CTRL:
1671 return tvaudio_set_ctrl(chip, arg); 1719 return tvaudio_set_ctrl(chip, arg);
1672 1720
1721 case VIDIOC_G_CTRL:
1722 return tvaudio_get_ctrl(chip, arg);
1673 case VIDIOC_INT_G_AUDIO_ROUTING: 1723 case VIDIOC_INT_G_AUDIO_ROUTING:
1674 { 1724 {
1675 struct v4l2_routing *rt = arg; 1725 struct v4l2_routing *rt = arg;
@@ -1678,7 +1728,6 @@ static int chip_command(struct i2c_client *client,
1678 rt->output = 0; 1728 rt->output = 0;
1679 break; 1729 break;
1680 } 1730 }
1681
1682 case VIDIOC_INT_S_AUDIO_ROUTING: 1731 case VIDIOC_INT_S_AUDIO_ROUTING:
1683 { 1732 {
1684 struct v4l2_routing *rt = arg; 1733 struct v4l2_routing *rt = arg;
@@ -1693,7 +1742,6 @@ static int chip_command(struct i2c_client *client,
1693 desc->inputmap[chip->input], desc->inputmask); 1742 desc->inputmap[chip->input], desc->inputmask);
1694 break; 1743 break;
1695 } 1744 }
1696
1697 case VIDIOC_S_TUNER: 1745 case VIDIOC_S_TUNER:
1698 { 1746 {
1699 struct v4l2_tuner *vt = arg; 1747 struct v4l2_tuner *vt = arg;
@@ -1703,17 +1751,13 @@ static int chip_command(struct i2c_client *client,
1703 break; 1751 break;
1704 switch (vt->audmode) { 1752 switch (vt->audmode) {
1705 case V4L2_TUNER_MODE_MONO: 1753 case V4L2_TUNER_MODE_MONO:
1706 mode = VIDEO_SOUND_MONO;
1707 break;
1708 case V4L2_TUNER_MODE_STEREO: 1754 case V4L2_TUNER_MODE_STEREO:
1709 case V4L2_TUNER_MODE_LANG1_LANG2:
1710 mode = VIDEO_SOUND_STEREO;
1711 break;
1712 case V4L2_TUNER_MODE_LANG1: 1755 case V4L2_TUNER_MODE_LANG1:
1713 mode = VIDEO_SOUND_LANG1;
1714 break;
1715 case V4L2_TUNER_MODE_LANG2: 1756 case V4L2_TUNER_MODE_LANG2:
1716 mode = VIDEO_SOUND_LANG2; 1757 mode = vt->audmode;
1758 break;
1759 case V4L2_TUNER_MODE_LANG1_LANG2:
1760 mode = V4L2_TUNER_MODE_STEREO;
1717 break; 1761 break;
1718 default: 1762 default:
1719 return -EINVAL; 1763 return -EINVAL;
@@ -1728,11 +1772,10 @@ static int chip_command(struct i2c_client *client,
1728 } 1772 }
1729 break; 1773 break;
1730 } 1774 }
1731
1732 case VIDIOC_G_TUNER: 1775 case VIDIOC_G_TUNER:
1733 { 1776 {
1734 struct v4l2_tuner *vt = arg; 1777 struct v4l2_tuner *vt = arg;
1735 int mode = VIDEO_SOUND_MONO; 1778 int mode = V4L2_TUNER_MODE_MONO;
1736 1779
1737 if (chip->radio) 1780 if (chip->radio)
1738 break; 1781 break;
@@ -1744,30 +1787,26 @@ static int chip_command(struct i2c_client *client,
1744 if (desc->getmode) 1787 if (desc->getmode)
1745 mode = desc->getmode(chip); 1788 mode = desc->getmode(chip);
1746 1789
1747 if (mode & VIDEO_SOUND_MONO) 1790 if (mode & V4L2_TUNER_MODE_MONO)
1748 vt->rxsubchans |= V4L2_TUNER_SUB_MONO; 1791 vt->rxsubchans |= V4L2_TUNER_SUB_MONO;
1749 if (mode & VIDEO_SOUND_STEREO) 1792 if (mode & V4L2_TUNER_MODE_STEREO)
1750 vt->rxsubchans |= V4L2_TUNER_SUB_STEREO; 1793 vt->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1751 /* Note: for SAP it should be mono/lang2 or stereo/lang2. 1794 /* Note: for SAP it should be mono/lang2 or stereo/lang2.
1752 When this module is converted fully to v4l2, then this 1795 When this module is converted fully to v4l2, then this
1753 should change for those chips that can detect SAP. */ 1796 should change for those chips that can detect SAP. */
1754 if (mode & VIDEO_SOUND_LANG1) 1797 if (mode & V4L2_TUNER_MODE_LANG1)
1755 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | 1798 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 |
1756 V4L2_TUNER_SUB_LANG2; 1799 V4L2_TUNER_SUB_LANG2;
1757 break; 1800 break;
1758 } 1801 }
1759
1760 case VIDIOCSCHAN:
1761 case VIDIOC_S_STD: 1802 case VIDIOC_S_STD:
1762 chip->radio = 0; 1803 chip->radio = 0;
1763 break; 1804 break;
1764
1765 case VIDIOCSFREQ:
1766 case VIDIOC_S_FREQUENCY: 1805 case VIDIOC_S_FREQUENCY:
1767 chip->mode = 0; /* automatic */ 1806 chip->mode = 0; /* automatic */
1768 if (desc->checkmode) { 1807 if (desc->checkmode) {
1769 desc->setmode(chip,VIDEO_SOUND_MONO); 1808 desc->setmode(chip,V4L2_TUNER_MODE_MONO);
1770 if (chip->prevmode != VIDEO_SOUND_MONO) 1809 if (chip->prevmode != V4L2_TUNER_MODE_MONO)
1771 chip->prevmode = -1; /* reset previous mode */ 1810 chip->prevmode = -1; /* reset previous mode */
1772 mod_timer(&chip->wt, jiffies+msecs_to_jiffies(2000)); 1811 mod_timer(&chip->wt, jiffies+msecs_to_jiffies(2000));
1773 /* the thread will call checkmode() later */ 1812 /* the thread will call checkmode() later */
@@ -1780,44 +1819,25 @@ static int chip_command(struct i2c_client *client,
1780 return 0; 1819 return 0;
1781} 1820}
1782 1821
1783static struct i2c_driver driver = { 1822static int chip_legacy_probe(struct i2c_adapter *adap)
1784 .driver = {
1785 .name = "tvaudio",
1786 },
1787 .id = I2C_DRIVERID_TVAUDIO,
1788 .attach_adapter = chip_probe,
1789 .detach_client = chip_detach,
1790 .command = chip_command,
1791};
1792
1793static struct i2c_client client_template =
1794{ 1823{
1795 .name = "(unset)", 1824 /* don't attach on saa7146 based cards,
1796 .driver = &driver, 1825 because dedicated drivers are used */
1797}; 1826 if ((adap->id == I2C_HW_SAA7146))
1798 1827 return 0;
1799static int __init audiochip_init_module(void) 1828 if (adap->class & I2C_CLASS_TV_ANALOG)
1800{ 1829 return 1;
1801 struct CHIPDESC *desc; 1830 return 0;
1802
1803 if (debug) {
1804 printk(KERN_INFO "tvaudio: TV audio decoder + audio/video mux driver\n");
1805 printk(KERN_INFO "tvaudio: known chips: ");
1806 for (desc = chiplist; desc->name != NULL; desc++)
1807 printk("%s%s", (desc == chiplist) ? "" : ", ", desc->name);
1808 printk("\n");
1809 }
1810
1811 return i2c_add_driver(&driver);
1812}
1813
1814static void __exit audiochip_cleanup_module(void)
1815{
1816 i2c_del_driver(&driver);
1817} 1831}
1818 1832
1819module_init(audiochip_init_module); 1833static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1820module_exit(audiochip_cleanup_module); 1834 .name = "tvaudio",
1835 .driverid = I2C_DRIVERID_TVAUDIO,
1836 .command = chip_command,
1837 .probe = chip_probe,
1838 .remove = chip_remove,
1839 .legacy_probe = chip_legacy_probe,
1840};
1821 1841
1822/* 1842/*
1823 * Local variables: 1843 * Local variables:
diff --git a/drivers/media/video/tveeprom.c b/drivers/media/video/tveeprom.c
index 4b2c4034f5b3..0b8fbad3c721 100644
--- a/drivers/media/video/tveeprom.c
+++ b/drivers/media/video/tveeprom.c
@@ -46,11 +46,12 @@ MODULE_DESCRIPTION("i2c Hauppauge eeprom decoder driver");
46MODULE_AUTHOR("John Klar"); 46MODULE_AUTHOR("John Klar");
47MODULE_LICENSE("GPL"); 47MODULE_LICENSE("GPL");
48 48
49static int debug = 0; 49static int debug;
50module_param(debug, int, 0644); 50module_param(debug, int, 0644);
51MODULE_PARM_DESC(debug, "Debug level (0-1)"); 51MODULE_PARM_DESC(debug, "Debug level (0-1)");
52 52
53#define STRM(array,i) (i < sizeof(array)/sizeof(char*) ? array[i] : "unknown") 53#define STRM(array, i) \
54 (i < sizeof(array) / sizeof(char *) ? array[i] : "unknown")
54 55
55#define tveeprom_info(fmt, arg...) \ 56#define tveeprom_info(fmt, arg...) \
56 v4l_printk(KERN_INFO, "tveeprom", c->adapter, c->addr, fmt , ## arg) 57 v4l_printk(KERN_INFO, "tveeprom", c->adapter, c->addr, fmt , ## arg)
@@ -58,7 +59,8 @@ MODULE_PARM_DESC(debug, "Debug level (0-1)");
58 v4l_printk(KERN_WARNING, "tveeprom", c->adapter, c->addr, fmt , ## arg) 59 v4l_printk(KERN_WARNING, "tveeprom", c->adapter, c->addr, fmt , ## arg)
59#define tveeprom_dbg(fmt, arg...) do { \ 60#define tveeprom_dbg(fmt, arg...) do { \
60 if (debug) \ 61 if (debug) \
61 v4l_printk(KERN_DEBUG, "tveeprom", c->adapter, c->addr, fmt , ## arg); \ 62 v4l_printk(KERN_DEBUG, "tveeprom", \
63 c->adapter, c->addr, fmt , ## arg); \
62 } while (0) 64 } while (0)
63 65
64/* 66/*
@@ -94,170 +96,172 @@ static struct HAUPPAUGE_TUNER
94hauppauge_tuner[] = 96hauppauge_tuner[] =
95{ 97{
96 /* 0-9 */ 98 /* 0-9 */
97 { TUNER_ABSENT, "None" }, 99 { TUNER_ABSENT, "None" },
98 { TUNER_ABSENT, "External" }, 100 { TUNER_ABSENT, "External" },
99 { TUNER_ABSENT, "Unspecified" }, 101 { TUNER_ABSENT, "Unspecified" },
100 { TUNER_PHILIPS_PAL, "Philips FI1216" }, 102 { TUNER_PHILIPS_PAL, "Philips FI1216" },
101 { TUNER_PHILIPS_SECAM, "Philips FI1216MF" }, 103 { TUNER_PHILIPS_SECAM, "Philips FI1216MF" },
102 { TUNER_PHILIPS_NTSC, "Philips FI1236" }, 104 { TUNER_PHILIPS_NTSC, "Philips FI1236" },
103 { TUNER_PHILIPS_PAL_I, "Philips FI1246" }, 105 { TUNER_PHILIPS_PAL_I, "Philips FI1246" },
104 { TUNER_PHILIPS_PAL_DK,"Philips FI1256" }, 106 { TUNER_PHILIPS_PAL_DK, "Philips FI1256" },
105 { TUNER_PHILIPS_PAL, "Philips FI1216 MK2" }, 107 { TUNER_PHILIPS_PAL, "Philips FI1216 MK2" },
106 { TUNER_PHILIPS_SECAM, "Philips FI1216MF MK2" }, 108 { TUNER_PHILIPS_SECAM, "Philips FI1216MF MK2" },
107 /* 10-19 */ 109 /* 10-19 */
108 { TUNER_PHILIPS_NTSC, "Philips FI1236 MK2" }, 110 { TUNER_PHILIPS_NTSC, "Philips FI1236 MK2" },
109 { TUNER_PHILIPS_PAL_I, "Philips FI1246 MK2" }, 111 { TUNER_PHILIPS_PAL_I, "Philips FI1246 MK2" },
110 { TUNER_PHILIPS_PAL_DK,"Philips FI1256 MK2" }, 112 { TUNER_PHILIPS_PAL_DK, "Philips FI1256 MK2" },
111 { TUNER_TEMIC_NTSC, "Temic 4032FY5" }, 113 { TUNER_TEMIC_NTSC, "Temic 4032FY5" },
112 { TUNER_TEMIC_PAL, "Temic 4002FH5" }, 114 { TUNER_TEMIC_PAL, "Temic 4002FH5" },
113 { TUNER_TEMIC_PAL_I, "Temic 4062FY5" }, 115 { TUNER_TEMIC_PAL_I, "Temic 4062FY5" },
114 { TUNER_PHILIPS_PAL, "Philips FR1216 MK2" }, 116 { TUNER_PHILIPS_PAL, "Philips FR1216 MK2" },
115 { TUNER_PHILIPS_SECAM, "Philips FR1216MF MK2" }, 117 { TUNER_PHILIPS_SECAM, "Philips FR1216MF MK2" },
116 { TUNER_PHILIPS_NTSC, "Philips FR1236 MK2" }, 118 { TUNER_PHILIPS_NTSC, "Philips FR1236 MK2" },
117 { TUNER_PHILIPS_PAL_I, "Philips FR1246 MK2" }, 119 { TUNER_PHILIPS_PAL_I, "Philips FR1246 MK2" },
118 /* 20-29 */ 120 /* 20-29 */
119 { TUNER_PHILIPS_PAL_DK,"Philips FR1256 MK2" }, 121 { TUNER_PHILIPS_PAL_DK, "Philips FR1256 MK2" },
120 { TUNER_PHILIPS_PAL, "Philips FM1216" }, 122 { TUNER_PHILIPS_PAL, "Philips FM1216" },
121 { TUNER_PHILIPS_SECAM, "Philips FM1216MF" }, 123 { TUNER_PHILIPS_SECAM, "Philips FM1216MF" },
122 { TUNER_PHILIPS_NTSC, "Philips FM1236" }, 124 { TUNER_PHILIPS_NTSC, "Philips FM1236" },
123 { TUNER_PHILIPS_PAL_I, "Philips FM1246" }, 125 { TUNER_PHILIPS_PAL_I, "Philips FM1246" },
124 { TUNER_PHILIPS_PAL_DK,"Philips FM1256" }, 126 { TUNER_PHILIPS_PAL_DK, "Philips FM1256" },
125 { TUNER_TEMIC_4036FY5_NTSC, "Temic 4036FY5" }, 127 { TUNER_TEMIC_4036FY5_NTSC, "Temic 4036FY5" },
126 { TUNER_ABSENT, "Samsung TCPN9082D" }, 128 { TUNER_ABSENT, "Samsung TCPN9082D" },
127 { TUNER_ABSENT, "Samsung TCPM9092P" }, 129 { TUNER_ABSENT, "Samsung TCPM9092P" },
128 { TUNER_TEMIC_4006FH5_PAL, "Temic 4006FH5" }, 130 { TUNER_TEMIC_4006FH5_PAL, "Temic 4006FH5" },
129 /* 30-39 */ 131 /* 30-39 */
130 { TUNER_ABSENT, "Samsung TCPN9085D" }, 132 { TUNER_ABSENT, "Samsung TCPN9085D" },
131 { TUNER_ABSENT, "Samsung TCPB9085P" }, 133 { TUNER_ABSENT, "Samsung TCPB9085P" },
132 { TUNER_ABSENT, "Samsung TCPL9091P" }, 134 { TUNER_ABSENT, "Samsung TCPL9091P" },
133 { TUNER_TEMIC_4039FR5_NTSC, "Temic 4039FR5" }, 135 { TUNER_TEMIC_4039FR5_NTSC, "Temic 4039FR5" },
134 { TUNER_PHILIPS_FQ1216ME, "Philips FQ1216 ME" }, 136 { TUNER_PHILIPS_FQ1216ME, "Philips FQ1216 ME" },
135 { TUNER_TEMIC_4066FY5_PAL_I, "Temic 4066FY5" }, 137 { TUNER_TEMIC_4066FY5_PAL_I, "Temic 4066FY5" },
136 { TUNER_PHILIPS_NTSC, "Philips TD1536" }, 138 { TUNER_PHILIPS_NTSC, "Philips TD1536" },
137 { TUNER_PHILIPS_NTSC, "Philips TD1536D" }, 139 { TUNER_PHILIPS_NTSC, "Philips TD1536D" },
138 { TUNER_PHILIPS_NTSC, "Philips FMR1236" }, /* mono radio */ 140 { TUNER_PHILIPS_NTSC, "Philips FMR1236" }, /* mono radio */
139 { TUNER_ABSENT, "Philips FI1256MP" }, 141 { TUNER_ABSENT, "Philips FI1256MP" },
140 /* 40-49 */ 142 /* 40-49 */
141 { TUNER_ABSENT, "Samsung TCPQ9091P" }, 143 { TUNER_ABSENT, "Samsung TCPQ9091P" },
142 { TUNER_TEMIC_4006FN5_MULTI_PAL, "Temic 4006FN5" }, 144 { TUNER_TEMIC_4006FN5_MULTI_PAL, "Temic 4006FN5" },
143 { TUNER_TEMIC_4009FR5_PAL, "Temic 4009FR5" }, 145 { TUNER_TEMIC_4009FR5_PAL, "Temic 4009FR5" },
144 { TUNER_TEMIC_4046FM5, "Temic 4046FM5" }, 146 { TUNER_TEMIC_4046FM5, "Temic 4046FM5" },
145 { TUNER_TEMIC_4009FN5_MULTI_PAL_FM, "Temic 4009FN5" }, 147 { TUNER_TEMIC_4009FN5_MULTI_PAL_FM, "Temic 4009FN5" },
146 { TUNER_ABSENT, "Philips TD1536D FH 44"}, 148 { TUNER_ABSENT, "Philips TD1536D FH 44"},
147 { TUNER_LG_NTSC_FM, "LG TP18NSR01F"}, 149 { TUNER_LG_NTSC_FM, "LG TP18NSR01F"},
148 { TUNER_LG_PAL_FM, "LG TP18PSB01D"}, 150 { TUNER_LG_PAL_FM, "LG TP18PSB01D"},
149 { TUNER_LG_PAL, "LG TP18PSB11D"}, 151 { TUNER_LG_PAL, "LG TP18PSB11D"},
150 { TUNER_LG_PAL_I_FM, "LG TAPC-I001D"}, 152 { TUNER_LG_PAL_I_FM, "LG TAPC-I001D"},
151 /* 50-59 */ 153 /* 50-59 */
152 { TUNER_LG_PAL_I, "LG TAPC-I701D"}, 154 { TUNER_LG_PAL_I, "LG TAPC-I701D"},
153 { TUNER_ABSENT, "Temic 4042FI5"}, 155 { TUNER_ABSENT, "Temic 4042FI5"},
154 { TUNER_MICROTUNE_4049FM5, "Microtune 4049 FM5"}, 156 { TUNER_MICROTUNE_4049FM5, "Microtune 4049 FM5"},
155 { TUNER_ABSENT, "LG TPI8NSR11F"}, 157 { TUNER_ABSENT, "LG TPI8NSR11F"},
156 { TUNER_ABSENT, "Microtune 4049 FM5 Alt I2C"}, 158 { TUNER_ABSENT, "Microtune 4049 FM5 Alt I2C"},
157 { TUNER_PHILIPS_FM1216ME_MK3, "Philips FQ1216ME MK3"}, 159 { TUNER_PHILIPS_FM1216ME_MK3, "Philips FQ1216ME MK3"},
158 { TUNER_ABSENT, "Philips FI1236 MK3"}, 160 { TUNER_ABSENT, "Philips FI1236 MK3"},
159 { TUNER_PHILIPS_FM1216ME_MK3, "Philips FM1216 ME MK3"}, 161 { TUNER_PHILIPS_FM1216ME_MK3, "Philips FM1216 ME MK3"},
160 { TUNER_PHILIPS_FM1236_MK3, "Philips FM1236 MK3"}, 162 { TUNER_PHILIPS_FM1236_MK3, "Philips FM1236 MK3"},
161 { TUNER_ABSENT, "Philips FM1216MP MK3"}, 163 { TUNER_ABSENT, "Philips FM1216MP MK3"},
162 /* 60-69 */ 164 /* 60-69 */
163 { TUNER_PHILIPS_FM1216ME_MK3, "LG S001D MK3"}, 165 { TUNER_PHILIPS_FM1216ME_MK3, "LG S001D MK3"},
164 { TUNER_ABSENT, "LG M001D MK3"}, 166 { TUNER_ABSENT, "LG M001D MK3"},
165 { TUNER_PHILIPS_FM1216ME_MK3, "LG S701D MK3"}, 167 { TUNER_PHILIPS_FM1216ME_MK3, "LG S701D MK3"},
166 { TUNER_ABSENT, "LG M701D MK3"}, 168 { TUNER_ABSENT, "LG M701D MK3"},
167 { TUNER_ABSENT, "Temic 4146FM5"}, 169 { TUNER_ABSENT, "Temic 4146FM5"},
168 { TUNER_ABSENT, "Temic 4136FY5"}, 170 { TUNER_ABSENT, "Temic 4136FY5"},
169 { TUNER_ABSENT, "Temic 4106FH5"}, 171 { TUNER_ABSENT, "Temic 4106FH5"},
170 { TUNER_ABSENT, "Philips FQ1216LMP MK3"}, 172 { TUNER_ABSENT, "Philips FQ1216LMP MK3"},
171 { TUNER_LG_NTSC_TAPE, "LG TAPE H001F MK3"}, 173 { TUNER_LG_NTSC_TAPE, "LG TAPE H001F MK3"},
172 { TUNER_LG_NTSC_TAPE, "LG TAPE H701F MK3"}, 174 { TUNER_LG_NTSC_TAPE, "LG TAPE H701F MK3"},
173 /* 70-79 */ 175 /* 70-79 */
174 { TUNER_ABSENT, "LG TALN H200T"}, 176 { TUNER_ABSENT, "LG TALN H200T"},
175 { TUNER_ABSENT, "LG TALN H250T"}, 177 { TUNER_ABSENT, "LG TALN H250T"},
176 { TUNER_ABSENT, "LG TALN M200T"}, 178 { TUNER_ABSENT, "LG TALN M200T"},
177 { TUNER_ABSENT, "LG TALN Z200T"}, 179 { TUNER_ABSENT, "LG TALN Z200T"},
178 { TUNER_ABSENT, "LG TALN S200T"}, 180 { TUNER_ABSENT, "LG TALN S200T"},
179 { TUNER_ABSENT, "Thompson DTT7595"}, 181 { TUNER_ABSENT, "Thompson DTT7595"},
180 { TUNER_ABSENT, "Thompson DTT7592"}, 182 { TUNER_ABSENT, "Thompson DTT7592"},
181 { TUNER_ABSENT, "Silicon TDA8275C1 8290"}, 183 { TUNER_ABSENT, "Silicon TDA8275C1 8290"},
182 { TUNER_ABSENT, "Silicon TDA8275C1 8290 FM"}, 184 { TUNER_ABSENT, "Silicon TDA8275C1 8290 FM"},
183 { TUNER_ABSENT, "Thompson DTT757"}, 185 { TUNER_ABSENT, "Thompson DTT757"},
184 /* 80-89 */ 186 /* 80-89 */
185 { TUNER_PHILIPS_FM1216ME_MK3, "Philips FQ1216LME MK3"}, 187 { TUNER_PHILIPS_FM1216ME_MK3, "Philips FQ1216LME MK3"},
186 { TUNER_LG_PAL_NEW_TAPC, "LG TAPC G701D"}, 188 { TUNER_LG_PAL_NEW_TAPC, "LG TAPC G701D"},
187 { TUNER_LG_NTSC_NEW_TAPC, "LG TAPC H791F"}, 189 { TUNER_LG_NTSC_NEW_TAPC, "LG TAPC H791F"},
188 { TUNER_LG_PAL_NEW_TAPC, "TCL 2002MB 3"}, 190 { TUNER_LG_PAL_NEW_TAPC, "TCL 2002MB 3"},
189 { TUNER_LG_PAL_NEW_TAPC, "TCL 2002MI 3"}, 191 { TUNER_LG_PAL_NEW_TAPC, "TCL 2002MI 3"},
190 { TUNER_TCL_2002N, "TCL 2002N 6A"}, 192 { TUNER_TCL_2002N, "TCL 2002N 6A"},
191 { TUNER_PHILIPS_FM1236_MK3, "Philips FQ1236 MK3"}, 193 { TUNER_PHILIPS_FM1236_MK3, "Philips FQ1236 MK3"},
192 { TUNER_SAMSUNG_TCPN_2121P30A, "Samsung TCPN 2121P30A"}, 194 { TUNER_SAMSUNG_TCPN_2121P30A, "Samsung TCPN 2121P30A"},
193 { TUNER_ABSENT, "Samsung TCPE 4121P30A"}, 195 { TUNER_ABSENT, "Samsung TCPE 4121P30A"},
194 { TUNER_PHILIPS_FM1216ME_MK3, "TCL MFPE05 2"}, 196 { TUNER_PHILIPS_FM1216ME_MK3, "TCL MFPE05 2"},
195 /* 90-99 */ 197 /* 90-99 */
196 { TUNER_ABSENT, "LG TALN H202T"}, 198 { TUNER_ABSENT, "LG TALN H202T"},
197 { TUNER_PHILIPS_FQ1216AME_MK4, "Philips FQ1216AME MK4"}, 199 { TUNER_PHILIPS_FQ1216AME_MK4, "Philips FQ1216AME MK4"},
198 { TUNER_PHILIPS_FQ1236A_MK4, "Philips FQ1236A MK4"}, 200 { TUNER_PHILIPS_FQ1236A_MK4, "Philips FQ1236A MK4"},
199 { TUNER_ABSENT, "Philips FQ1286A MK4"}, 201 { TUNER_ABSENT, "Philips FQ1286A MK4"},
200 { TUNER_ABSENT, "Philips FQ1216ME MK5"}, 202 { TUNER_ABSENT, "Philips FQ1216ME MK5"},
201 { TUNER_ABSENT, "Philips FQ1236 MK5"}, 203 { TUNER_ABSENT, "Philips FQ1236 MK5"},
202 { TUNER_SAMSUNG_TCPG_6121P30A, "Samsung TCPG 6121P30A"}, 204 { TUNER_SAMSUNG_TCPG_6121P30A, "Samsung TCPG 6121P30A"},
203 { TUNER_TCL_2002MB, "TCL 2002MB_3H"}, 205 { TUNER_TCL_2002MB, "TCL 2002MB_3H"},
204 { TUNER_ABSENT, "TCL 2002MI_3H"}, 206 { TUNER_ABSENT, "TCL 2002MI_3H"},
205 { TUNER_TCL_2002N, "TCL 2002N 5H"}, 207 { TUNER_TCL_2002N, "TCL 2002N 5H"},
206 /* 100-109 */ 208 /* 100-109 */
207 { TUNER_PHILIPS_FMD1216ME_MK3, "Philips FMD1216ME"}, 209 { TUNER_PHILIPS_FMD1216ME_MK3, "Philips FMD1216ME"},
208 { TUNER_TEA5767, "Philips TEA5768HL FM Radio"}, 210 { TUNER_TEA5767, "Philips TEA5768HL FM Radio"},
209 { TUNER_ABSENT, "Panasonic ENV57H12D5"}, 211 { TUNER_ABSENT, "Panasonic ENV57H12D5"},
210 { TUNER_PHILIPS_FM1236_MK3, "TCL MFNM05-4"}, 212 { TUNER_PHILIPS_FM1236_MK3, "TCL MFNM05-4"},
211 { TUNER_ABSENT, "TCL MNM05-4"}, 213 { TUNER_ABSENT, "TCL MNM05-4"},
212 { TUNER_PHILIPS_FM1216ME_MK3, "TCL MPE05-2"}, 214 { TUNER_PHILIPS_FM1216ME_MK3, "TCL MPE05-2"},
213 { TUNER_ABSENT, "TCL MQNM05-4"}, 215 { TUNER_ABSENT, "TCL MQNM05-4"},
214 { TUNER_ABSENT, "LG TAPC-W701D"}, 216 { TUNER_ABSENT, "LG TAPC-W701D"},
215 { TUNER_ABSENT, "TCL 9886P-WM"}, 217 { TUNER_ABSENT, "TCL 9886P-WM"},
216 { TUNER_ABSENT, "TCL 1676NM-WM"}, 218 { TUNER_ABSENT, "TCL 1676NM-WM"},
217 /* 110-119 */ 219 /* 110-119 */
218 { TUNER_ABSENT, "Thompson DTT75105"}, 220 { TUNER_ABSENT, "Thompson DTT75105"},
219 { TUNER_ABSENT, "Conexant_CX24109"}, 221 { TUNER_ABSENT, "Conexant_CX24109"},
220 { TUNER_TCL_2002N, "TCL M2523_5N_E"}, 222 { TUNER_TCL_2002N, "TCL M2523_5N_E"},
221 { TUNER_TCL_2002MB, "TCL M2523_3DB_E"}, 223 { TUNER_TCL_2002MB, "TCL M2523_3DB_E"},
222 { TUNER_ABSENT, "Philips 8275A"}, 224 { TUNER_ABSENT, "Philips 8275A"},
223 { TUNER_ABSENT, "Microtune MT2060"}, 225 { TUNER_ABSENT, "Microtune MT2060"},
224 { TUNER_PHILIPS_FM1236_MK3, "Philips FM1236 MK5"}, 226 { TUNER_PHILIPS_FM1236_MK3, "Philips FM1236 MK5"},
225 { TUNER_PHILIPS_FM1216ME_MK3, "Philips FM1216ME MK5"}, 227 { TUNER_PHILIPS_FM1216ME_MK3, "Philips FM1216ME MK5"},
226 { TUNER_ABSENT, "TCL M2523_3DI_E"}, 228 { TUNER_ABSENT, "TCL M2523_3DI_E"},
227 { TUNER_ABSENT, "Samsung THPD5222FG30A"}, 229 { TUNER_ABSENT, "Samsung THPD5222FG30A"},
228 /* 120-129 */ 230 /* 120-129 */
229 { TUNER_ABSENT, "Xceive XC3028"}, 231 { TUNER_XC2028, "Xceive XC3028"},
230 { TUNER_ABSENT, "Philips FQ1216LME MK5"}, 232 { TUNER_ABSENT, "Philips FQ1216LME MK5"},
231 { TUNER_ABSENT, "Philips FQD1216LME"}, 233 { TUNER_ABSENT, "Philips FQD1216LME"},
232 { TUNER_ABSENT, "Conexant CX24118A"}, 234 { TUNER_ABSENT, "Conexant CX24118A"},
233 { TUNER_ABSENT, "TCL DMF11WIP"}, 235 { TUNER_ABSENT, "TCL DMF11WIP"},
234 { TUNER_ABSENT, "TCL MFNM05_4H_E"}, 236 { TUNER_ABSENT, "TCL MFNM05_4H_E"},
235 { TUNER_ABSENT, "TCL MNM05_4H_E"}, 237 { TUNER_ABSENT, "TCL MNM05_4H_E"},
236 { TUNER_ABSENT, "TCL MPE05_2H_E"}, 238 { TUNER_ABSENT, "TCL MPE05_2H_E"},
237 { TUNER_ABSENT, "TCL MQNM05_4_U"}, 239 { TUNER_ABSENT, "TCL MQNM05_4_U"},
238 { TUNER_ABSENT, "TCL M2523_5NH_E"}, 240 { TUNER_ABSENT, "TCL M2523_5NH_E"},
239 /* 130-139 */ 241 /* 130-139 */
240 { TUNER_ABSENT, "TCL M2523_3DBH_E"}, 242 { TUNER_ABSENT, "TCL M2523_3DBH_E"},
241 { TUNER_ABSENT, "TCL M2523_3DIH_E"}, 243 { TUNER_ABSENT, "TCL M2523_3DIH_E"},
242 { TUNER_ABSENT, "TCL MFPE05_2_U"}, 244 { TUNER_ABSENT, "TCL MFPE05_2_U"},
243 { TUNER_ABSENT, "Philips FMD1216MEX"}, 245 { TUNER_ABSENT, "Philips FMD1216MEX"},
244 { TUNER_ABSENT, "Philips FRH2036B"}, 246 { TUNER_ABSENT, "Philips FRH2036B"},
245 { TUNER_ABSENT, "Panasonic ENGF75_01GF"}, 247 { TUNER_ABSENT, "Panasonic ENGF75_01GF"},
246 { TUNER_ABSENT, "MaxLinear MXL5005"}, 248 { TUNER_ABSENT, "MaxLinear MXL5005"},
247 { TUNER_ABSENT, "MaxLinear MXL5003"}, 249 { TUNER_ABSENT, "MaxLinear MXL5003"},
248 { TUNER_ABSENT, "Xceive XC2028"}, 250 { TUNER_ABSENT, "Xceive XC2028"},
249 { TUNER_ABSENT, "Microtune MT2131"}, 251 { TUNER_ABSENT, "Microtune MT2131"},
250 /* 140-149 */ 252 /* 140-149 */
251 { TUNER_ABSENT, "Philips 8275A_8295"}, 253 { TUNER_ABSENT, "Philips 8275A_8295"},
252 { TUNER_ABSENT, "TCL MF02GIP_5N_E"}, 254 { TUNER_ABSENT, "TCL MF02GIP_5N_E"},
253 { TUNER_ABSENT, "TCL MF02GIP_3DB_E"}, 255 { TUNER_ABSENT, "TCL MF02GIP_3DB_E"},
254 { TUNER_ABSENT, "TCL MF02GIP_3DI_E"}, 256 { TUNER_ABSENT, "TCL MF02GIP_3DI_E"},
255 { TUNER_ABSENT, "Microtune MT2266"}, 257 { TUNER_ABSENT, "Microtune MT2266"},
256 { TUNER_ABSENT, "TCL MF10WPP_4N_E"}, 258 { TUNER_ABSENT, "TCL MF10WPP_4N_E"},
257 { TUNER_ABSENT, "LG TAPQ_H702F"}, 259 { TUNER_ABSENT, "LG TAPQ_H702F"},
258 { TUNER_ABSENT, "TCL M09WPP_4N_E"}, 260 { TUNER_ABSENT, "TCL M09WPP_4N_E"},
259 { TUNER_ABSENT, "MaxLinear MXL5005_v2"}, 261 { TUNER_ABSENT, "MaxLinear MXL5005_v2"},
260 { TUNER_ABSENT, "Philips 18271_8295"}, 262 { TUNER_PHILIPS_TDA8290, "Philips 18271_8295"},
263 /* 150-159 */
264 { TUNER_ABSENT, "Xceive XC5000"},
261}; 265};
262 266
263static struct HAUPPAUGE_AUDIOIC 267static struct HAUPPAUGE_AUDIOIC
@@ -344,37 +348,37 @@ static const char *decoderIC[] = {
344static int hasRadioTuner(int tunerType) 348static int hasRadioTuner(int tunerType)
345{ 349{
346 switch (tunerType) { 350 switch (tunerType) {
347 case 18: //PNPEnv_TUNER_FR1236_MK2: 351 case 18: /* PNPEnv_TUNER_FR1236_MK2 */
348 case 23: //PNPEnv_TUNER_FM1236: 352 case 23: /* PNPEnv_TUNER_FM1236 */
349 case 38: //PNPEnv_TUNER_FMR1236: 353 case 38: /* PNPEnv_TUNER_FMR1236 */
350 case 16: //PNPEnv_TUNER_FR1216_MK2: 354 case 16: /* PNPEnv_TUNER_FR1216_MK2 */
351 case 19: //PNPEnv_TUNER_FR1246_MK2: 355 case 19: /* PNPEnv_TUNER_FR1246_MK2 */
352 case 21: //PNPEnv_TUNER_FM1216: 356 case 21: /* PNPEnv_TUNER_FM1216 */
353 case 24: //PNPEnv_TUNER_FM1246: 357 case 24: /* PNPEnv_TUNER_FM1246 */
354 case 17: //PNPEnv_TUNER_FR1216MF_MK2: 358 case 17: /* PNPEnv_TUNER_FR1216MF_MK2 */
355 case 22: //PNPEnv_TUNER_FM1216MF: 359 case 22: /* PNPEnv_TUNER_FM1216MF */
356 case 20: //PNPEnv_TUNER_FR1256_MK2: 360 case 20: /* PNPEnv_TUNER_FR1256_MK2 */
357 case 25: //PNPEnv_TUNER_FM1256: 361 case 25: /* PNPEnv_TUNER_FM1256 */
358 case 33: //PNPEnv_TUNER_4039FR5: 362 case 33: /* PNPEnv_TUNER_4039FR5 */
359 case 42: //PNPEnv_TUNER_4009FR5: 363 case 42: /* PNPEnv_TUNER_4009FR5 */
360 case 52: //PNPEnv_TUNER_4049FM5: 364 case 52: /* PNPEnv_TUNER_4049FM5 */
361 case 54: //PNPEnv_TUNER_4049FM5_AltI2C: 365 case 54: /* PNPEnv_TUNER_4049FM5_AltI2C */
362 case 44: //PNPEnv_TUNER_4009FN5: 366 case 44: /* PNPEnv_TUNER_4009FN5 */
363 case 31: //PNPEnv_TUNER_TCPB9085P: 367 case 31: /* PNPEnv_TUNER_TCPB9085P */
364 case 30: //PNPEnv_TUNER_TCPN9085D: 368 case 30: /* PNPEnv_TUNER_TCPN9085D */
365 case 46: //PNPEnv_TUNER_TP18NSR01F: 369 case 46: /* PNPEnv_TUNER_TP18NSR01F */
366 case 47: //PNPEnv_TUNER_TP18PSB01D: 370 case 47: /* PNPEnv_TUNER_TP18PSB01D */
367 case 49: //PNPEnv_TUNER_TAPC_I001D: 371 case 49: /* PNPEnv_TUNER_TAPC_I001D */
368 case 60: //PNPEnv_TUNER_TAPE_S001D_MK3: 372 case 60: /* PNPEnv_TUNER_TAPE_S001D_MK3 */
369 case 57: //PNPEnv_TUNER_FM1216ME_MK3: 373 case 57: /* PNPEnv_TUNER_FM1216ME_MK3 */
370 case 59: //PNPEnv_TUNER_FM1216MP_MK3: 374 case 59: /* PNPEnv_TUNER_FM1216MP_MK3 */
371 case 58: //PNPEnv_TUNER_FM1236_MK3: 375 case 58: /* PNPEnv_TUNER_FM1236_MK3 */
372 case 68: //PNPEnv_TUNER_TAPE_H001F_MK3: 376 case 68: /* PNPEnv_TUNER_TAPE_H001F_MK3 */
373 case 61: //PNPEnv_TUNER_TAPE_M001D_MK3: 377 case 61: /* PNPEnv_TUNER_TAPE_M001D_MK3 */
374 case 78: //PNPEnv_TUNER_TDA8275C1_8290_FM: 378 case 78: /* PNPEnv_TUNER_TDA8275C1_8290_FM */
375 case 89: //PNPEnv_TUNER_TCL_MFPE05_2: 379 case 89: /* PNPEnv_TUNER_TCL_MFPE05_2 */
376 case 92: //PNPEnv_TUNER_PHILIPS_FQ1236A_MK4: 380 case 92: /* PNPEnv_TUNER_PHILIPS_FQ1236A_MK4 */
377 case 105: 381 case 105:
378 return 1; 382 return 1;
379 } 383 }
380 return 0; 384 return 0;
@@ -392,7 +396,8 @@ void tveeprom_hauppauge_analog(struct i2c_client *c, struct tveeprom *tvee,
392 ** 396 **
393 ** In our (ivtv) case we're interested in the following: 397 ** In our (ivtv) case we're interested in the following:
394 ** tuner type: tag [00].05 or [0a].01 (index into hauppauge_tuner) 398 ** tuner type: tag [00].05 or [0a].01 (index into hauppauge_tuner)
395 ** tuner fmts: tag [00].04 or [0a].00 (bitmask index into hauppauge_tuner_fmt) 399 ** tuner fmts: tag [00].04 or [0a].00 (bitmask index into
400 ** hauppauge_tuner_fmt)
396 ** radio: tag [00].{last} or [0e].00 (bitmask. bit2=FM) 401 ** radio: tag [00].{last} or [0e].00 (bitmask. bit2=FM)
397 ** audio proc: tag [02].01 or [05].00 (mask with 0x7f) 402 ** audio proc: tag [02].01 or [05].00 (mask with 0x7f)
398 ** decoder proc: tag [09].01) 403 ** decoder proc: tag [09].01)
@@ -405,9 +410,9 @@ void tveeprom_hauppauge_analog(struct i2c_client *c, struct tveeprom *tvee,
405 ** # of inputs/outputs ??? 410 ** # of inputs/outputs ???
406 */ 411 */
407 412
408 int i, j, len, done, beenhere, tag,start; 413 int i, j, len, done, beenhere, tag, start;
409 414
410 int tuner1 = 0, t_format1 = 0, audioic=-1; 415 int tuner1 = 0, t_format1 = 0, audioic = -1;
411 char *t_name1 = NULL; 416 char *t_name1 = NULL;
412 const char *t_fmt_name1[8] = { " none", "", "", "", "", "", "", "" }; 417 const char *t_fmt_name1[8] = { " none", "", "", "", "", "", "", "" };
413 418
@@ -418,17 +423,24 @@ void tveeprom_hauppauge_analog(struct i2c_client *c, struct tveeprom *tvee,
418 memset(tvee, 0, sizeof(*tvee)); 423 memset(tvee, 0, sizeof(*tvee));
419 done = len = beenhere = 0; 424 done = len = beenhere = 0;
420 425
421 /* Hack for processing eeprom for em28xx and cx 2388x*/ 426 /* Different eeprom start offsets for em28xx, cx2388x and cx23418 */
422 if ((eeprom_data[0] == 0x1a) && (eeprom_data[1] == 0xeb) && 427 if (eeprom_data[0] == 0x1a &&
423 (eeprom_data[2] == 0x67) && (eeprom_data[3] == 0x95)) 428 eeprom_data[1] == 0xeb &&
424 start=0xa0; /* Generic em28xx offset */ 429 eeprom_data[2] == 0x67 &&
425 else if (((eeprom_data[0] & 0xe1) == 0x01) && 430 eeprom_data[3] == 0x95)
426 (eeprom_data[1] == 0x00) && 431 start = 0xa0; /* Generic em28xx offset */
427 (eeprom_data[2] == 0x00) && 432 else if ((eeprom_data[0] & 0xe1) == 0x01 &&
428 (eeprom_data[8] == 0x84)) 433 eeprom_data[1] == 0x00 &&
429 start=8; /* Generic cx2388x offset */ 434 eeprom_data[2] == 0x00 &&
435 eeprom_data[8] == 0x84)
436 start = 8; /* Generic cx2388x offset */
437 else if (eeprom_data[1] == 0x70 &&
438 eeprom_data[2] == 0x00 &&
439 eeprom_data[4] == 0x74 &&
440 eeprom_data[8] == 0x84)
441 start = 8; /* Generic cx23418 offset (models 74xxx) */
430 else 442 else
431 start=0; 443 start = 0;
432 444
433 for (i = start; !done && i < 256; i += len) { 445 for (i = start; !done && i < 256; i += len) {
434 if (eeprom_data[i] == 0x84) { 446 if (eeprom_data[i] == 0x84) {
@@ -444,16 +456,17 @@ void tveeprom_hauppauge_analog(struct i2c_client *c, struct tveeprom *tvee,
444 ++i; 456 ++i;
445 } else { 457 } else {
446 tveeprom_warn("Encountered bad packet header [%02x]. " 458 tveeprom_warn("Encountered bad packet header [%02x]. "
447 "Corrupt or not a Hauppauge eeprom.\n", eeprom_data[i]); 459 "Corrupt or not a Hauppauge eeprom.\n",
460 eeprom_data[i]);
448 return; 461 return;
449 } 462 }
450 463
451 if (debug) { 464 if (debug) {
452 tveeprom_info("Tag [%02x] + %d bytes:", eeprom_data[i], len - 1); 465 tveeprom_info("Tag [%02x] + %d bytes:",
453 for(j = 1; j < len; j++) { 466 eeprom_data[i], len - 1);
454 printk(" %02x", eeprom_data[i + j]); 467 for (j = 1; j < len; j++)
455 } 468 printk(KERN_CONT " %02x", eeprom_data[i + j]);
456 printk("\n"); 469 printk(KERN_CONT "\n");
457 } 470 }
458 471
459 /* process by tag */ 472 /* process by tag */
@@ -504,16 +517,16 @@ void tveeprom_hauppauge_analog(struct i2c_client *c, struct tveeprom *tvee,
504 (eeprom_data[i+6] << 8) + 517 (eeprom_data[i+6] << 8) +
505 (eeprom_data[i+7] << 16); 518 (eeprom_data[i+7] << 16);
506 519
507 if ( (eeprom_data[i + 8] & 0xf0) && 520 if ((eeprom_data[i + 8] & 0xf0) &&
508 (tvee->serial_number < 0xffffff) ) { 521 (tvee->serial_number < 0xffffff)) {
509 tvee->MAC_address[0] = 0x00; 522 tvee->MAC_address[0] = 0x00;
510 tvee->MAC_address[1] = 0x0D; 523 tvee->MAC_address[1] = 0x0D;
511 tvee->MAC_address[2] = 0xFE; 524 tvee->MAC_address[2] = 0xFE;
512 tvee->MAC_address[3] = eeprom_data[i + 7]; 525 tvee->MAC_address[3] = eeprom_data[i + 7];
513 tvee->MAC_address[4] = eeprom_data[i + 6]; 526 tvee->MAC_address[4] = eeprom_data[i + 6];
514 tvee->MAC_address[5] = eeprom_data[i + 5]; 527 tvee->MAC_address[5] = eeprom_data[i + 5];
515 tvee->has_MAC_address = 1; 528 tvee->has_MAC_address = 1;
516 } 529 }
517 break; 530 break;
518 531
519 case 0x05: 532 case 0x05:
@@ -537,7 +550,7 @@ void tveeprom_hauppauge_analog(struct i2c_client *c, struct tveeprom *tvee,
537 (eeprom_data[i + 3] << 16) + 550 (eeprom_data[i + 3] << 16) +
538 (eeprom_data[i + 4] << 24); 551 (eeprom_data[i + 4] << 24);
539 tvee->revision = 552 tvee->revision =
540 eeprom_data[i +5 ] + 553 eeprom_data[i + 5] +
541 (eeprom_data[i + 6] << 8) + 554 (eeprom_data[i + 6] << 8) +
542 (eeprom_data[i + 7] << 16); 555 (eeprom_data[i + 7] << 16);
543 break; 556 break;
@@ -557,16 +570,16 @@ void tveeprom_hauppauge_analog(struct i2c_client *c, struct tveeprom *tvee,
557 case 0x0a: 570 case 0x0a:
558 /* tag 'Tuner' */ 571 /* tag 'Tuner' */
559 if (beenhere == 0) { 572 if (beenhere == 0) {
560 tuner1 = eeprom_data[i+2]; 573 tuner1 = eeprom_data[i + 2];
561 t_format1 = eeprom_data[i+1]; 574 t_format1 = eeprom_data[i + 1];
562 beenhere = 1; 575 beenhere = 1;
563 } else { 576 } else {
564 /* a second (radio) tuner may be present */ 577 /* a second (radio) tuner may be present */
565 tuner2 = eeprom_data[i+2]; 578 tuner2 = eeprom_data[i + 2];
566 t_format2 = eeprom_data[i+1]; 579 t_format2 = eeprom_data[i + 1];
567 if (t_format2 == 0) { /* not a TV tuner? */ 580 /* not a TV tuner? */
581 if (t_format2 == 0)
568 tvee->has_radio = 1; /* must be radio */ 582 tvee->has_radio = 1; /* must be radio */
569 }
570 } 583 }
571 break; 584 break;
572 585
@@ -594,7 +607,8 @@ void tveeprom_hauppauge_analog(struct i2c_client *c, struct tveeprom *tvee,
594 /* case 0x12: tag 'InfoBits' */ 607 /* case 0x12: tag 'InfoBits' */
595 608
596 default: 609 default:
597 tveeprom_dbg("Not sure what to do with tag [%02x]\n", tag); 610 tveeprom_dbg("Not sure what to do with tag [%02x]\n",
611 tag);
598 /* dump the rest of the packet? */ 612 /* dump the rest of the packet? */
599 } 613 }
600 } 614 }
@@ -608,7 +622,7 @@ void tveeprom_hauppauge_analog(struct i2c_client *c, struct tveeprom *tvee,
608 tvee->rev_str[0] = 32 + ((tvee->revision >> 18) & 0x3f); 622 tvee->rev_str[0] = 32 + ((tvee->revision >> 18) & 0x3f);
609 tvee->rev_str[1] = 32 + ((tvee->revision >> 12) & 0x3f); 623 tvee->rev_str[1] = 32 + ((tvee->revision >> 12) & 0x3f);
610 tvee->rev_str[2] = 32 + ((tvee->revision >> 6) & 0x3f); 624 tvee->rev_str[2] = 32 + ((tvee->revision >> 6) & 0x3f);
611 tvee->rev_str[3] = 32 + ( tvee->revision & 0x3f); 625 tvee->rev_str[3] = 32 + (tvee->revision & 0x3f);
612 tvee->rev_str[4] = 0; 626 tvee->rev_str[4] = 0;
613 } 627 }
614 628
@@ -651,44 +665,40 @@ void tveeprom_hauppauge_analog(struct i2c_client *c, struct tveeprom *tvee,
651 665
652 tveeprom_info("Hauppauge model %d, rev %s, serial# %d\n", 666 tveeprom_info("Hauppauge model %d, rev %s, serial# %d\n",
653 tvee->model, tvee->rev_str, tvee->serial_number); 667 tvee->model, tvee->rev_str, tvee->serial_number);
654 if (tvee->has_MAC_address == 1) { 668 if (tvee->has_MAC_address == 1)
655 tveeprom_info("MAC address is %02X-%02X-%02X-%02X-%02X-%02X\n", 669 tveeprom_info("MAC address is %02X-%02X-%02X-%02X-%02X-%02X\n",
656 tvee->MAC_address[0], tvee->MAC_address[1], 670 tvee->MAC_address[0], tvee->MAC_address[1],
657 tvee->MAC_address[2], tvee->MAC_address[3], 671 tvee->MAC_address[2], tvee->MAC_address[3],
658 tvee->MAC_address[4], tvee->MAC_address[5]); 672 tvee->MAC_address[4], tvee->MAC_address[5]);
659 }
660 tveeprom_info("tuner model is %s (idx %d, type %d)\n", 673 tveeprom_info("tuner model is %s (idx %d, type %d)\n",
661 t_name1, tuner1, tvee->tuner_type); 674 t_name1, tuner1, tvee->tuner_type);
662 tveeprom_info("TV standards%s%s%s%s%s%s%s%s (eeprom 0x%02x)\n", 675 tveeprom_info("TV standards%s%s%s%s%s%s%s%s (eeprom 0x%02x)\n",
663 t_fmt_name1[0], t_fmt_name1[1], t_fmt_name1[2], t_fmt_name1[3], 676 t_fmt_name1[0], t_fmt_name1[1], t_fmt_name1[2],
664 t_fmt_name1[4], t_fmt_name1[5], t_fmt_name1[6], t_fmt_name1[7], 677 t_fmt_name1[3], t_fmt_name1[4], t_fmt_name1[5],
665 t_format1); 678 t_fmt_name1[6], t_fmt_name1[7], t_format1);
666 if (tuner2) { 679 if (tuner2)
667 tveeprom_info("second tuner model is %s (idx %d, type %d)\n", 680 tveeprom_info("second tuner model is %s (idx %d, type %d)\n",
668 t_name2, tuner2, tvee->tuner2_type); 681 t_name2, tuner2, tvee->tuner2_type);
669 } 682 if (t_format2)
670 if (t_format2) {
671 tveeprom_info("TV standards%s%s%s%s%s%s%s%s (eeprom 0x%02x)\n", 683 tveeprom_info("TV standards%s%s%s%s%s%s%s%s (eeprom 0x%02x)\n",
672 t_fmt_name2[0], t_fmt_name2[1], t_fmt_name2[2], t_fmt_name2[3], 684 t_fmt_name2[0], t_fmt_name2[1], t_fmt_name2[2],
673 t_fmt_name2[4], t_fmt_name2[5], t_fmt_name2[6], t_fmt_name2[7], 685 t_fmt_name2[3], t_fmt_name2[4], t_fmt_name2[5],
674 t_format2); 686 t_fmt_name2[6], t_fmt_name2[7], t_format2);
675 } 687 if (audioic < 0) {
676 if (audioic<0) {
677 tveeprom_info("audio processor is unknown (no idx)\n"); 688 tveeprom_info("audio processor is unknown (no idx)\n");
678 tvee->audio_processor=AUDIO_CHIP_UNKNOWN; 689 tvee->audio_processor = AUDIO_CHIP_UNKNOWN;
679 } else { 690 } else {
680 if (audioic < ARRAY_SIZE(audioIC)) 691 if (audioic < ARRAY_SIZE(audioIC))
681 tveeprom_info("audio processor is %s (idx %d)\n", 692 tveeprom_info("audio processor is %s (idx %d)\n",
682 audioIC[audioic].name,audioic); 693 audioIC[audioic].name, audioic);
683 else 694 else
684 tveeprom_info("audio processor is unknown (idx %d)\n", 695 tveeprom_info("audio processor is unknown (idx %d)\n",
685 audioic); 696 audioic);
686 } 697 }
687 if (tvee->decoder_processor) { 698 if (tvee->decoder_processor)
688 tveeprom_info("decoder processor is %s (idx %d)\n", 699 tveeprom_info("decoder processor is %s (idx %d)\n",
689 STRM(decoderIC, tvee->decoder_processor), 700 STRM(decoderIC, tvee->decoder_processor),
690 tvee->decoder_processor); 701 tvee->decoder_processor);
691 }
692 if (tvee->has_ir == -1) 702 if (tvee->has_ir == -1)
693 tveeprom_info("has %sradio\n", 703 tveeprom_info("has %sradio\n",
694 tvee->has_radio ? "" : "no "); 704 tvee->has_radio ? "" : "no ");
@@ -709,11 +719,13 @@ int tveeprom_read(struct i2c_client *c, unsigned char *eedata, int len)
709 int err; 719 int err;
710 720
711 buf = 0; 721 buf = 0;
712 if (1 != (err = i2c_master_send(c, &buf, 1))) { 722 err = i2c_master_send(c, &buf, 1);
723 if (err != 1) {
713 tveeprom_info("Huh, no eeprom present (err=%d)?\n", err); 724 tveeprom_info("Huh, no eeprom present (err=%d)?\n", err);
714 return -1; 725 return -1;
715 } 726 }
716 if (len != (err = i2c_master_recv(c, eedata, len))) { 727 err = i2c_master_recv(c, eedata, len);
728 if (err != len) {
717 tveeprom_warn("i2c eeprom read error (err=%d)\n", err); 729 tveeprom_warn("i2c eeprom read error (err=%d)\n", err);
718 return -1; 730 return -1;
719 } 731 }
@@ -724,9 +736,9 @@ int tveeprom_read(struct i2c_client *c, unsigned char *eedata, int len)
724 for (i = 0; i < len; i++) { 736 for (i = 0; i < len; i++) {
725 if (0 == (i % 16)) 737 if (0 == (i % 16))
726 tveeprom_info("%02x:", i); 738 tveeprom_info("%02x:", i);
727 printk(" %02x", eedata[i]); 739 printk(KERN_CONT " %02x", eedata[i]);
728 if (15 == (i % 16)) 740 if (15 == (i % 16))
729 printk("\n"); 741 printk(KERN_CONT "\n");
730 } 742 }
731 } 743 }
732 return 0; 744 return 0;
@@ -758,9 +770,9 @@ tveeprom_command(struct i2c_client *client,
758 770
759 switch (cmd) { 771 switch (cmd) {
760 case 0: 772 case 0:
761 buf = kzalloc(256,GFP_KERNEL); 773 buf = kzalloc(256, GFP_KERNEL);
762 tveeprom_read(client,buf,256); 774 tveeprom_read(client, buf, 256);
763 tveeprom_hauppauge_analog(client, &eeprom,buf); 775 tveeprom_hauppauge_analog(client, &eeprom, buf);
764 kfree(buf); 776 kfree(buf);
765 eeprom_props[0] = eeprom.tuner_type; 777 eeprom_props[0] = eeprom.tuner_type;
766 eeprom_props[1] = eeprom.tuner_formats; 778 eeprom_props[1] = eeprom.tuner_formats;
@@ -794,7 +806,7 @@ tveeprom_detect_client(struct i2c_adapter *adapter,
794} 806}
795 807
796static int 808static int
797tveeprom_attach_adapter (struct i2c_adapter *adapter) 809tveeprom_attach_adapter(struct i2c_adapter *adapter)
798{ 810{
799 if (adapter->class & I2C_CLASS_TV_ANALOG) 811 if (adapter->class & I2C_CLASS_TV_ANALOG)
800 return i2c_probe(adapter, &addr_data, tveeprom_detect_client); 812 return i2c_probe(adapter, &addr_data, tveeprom_detect_client);
@@ -802,7 +814,7 @@ tveeprom_attach_adapter (struct i2c_adapter *adapter)
802} 814}
803 815
804static int 816static int
805tveeprom_detach_client (struct i2c_client *client) 817tveeprom_detach_client(struct i2c_client *client)
806{ 818{
807 int err; 819 int err;
808 820
diff --git a/drivers/media/video/upd64031a.c b/drivers/media/video/upd64031a.c
index 0b2a961efd22..bd201397a2ac 100644
--- a/drivers/media/video/upd64031a.c
+++ b/drivers/media/video/upd64031a.c
@@ -28,30 +28,27 @@
28#include <linux/videodev2.h> 28#include <linux/videodev2.h>
29#include <media/v4l2-common.h> 29#include <media/v4l2-common.h>
30#include <media/v4l2-chip-ident.h> 30#include <media/v4l2-chip-ident.h>
31#include <media/v4l2-i2c-drv.h>
31#include <media/upd64031a.h> 32#include <media/upd64031a.h>
32 33
33// --------------------- read registers functions define ----------------------- 34/* --------------------- read registers functions define -------------------- */
34 35
35/* bit masks */ 36/* bit masks */
36#define GR_MODE_MASK 0xc0 37#define GR_MODE_MASK 0xc0
37#define DIRECT_3DYCS_CONNECT_MASK 0xc0 38#define DIRECT_3DYCS_CONNECT_MASK 0xc0
38#define SYNC_CIRCUIT_MASK 0xa0 39#define SYNC_CIRCUIT_MASK 0xa0
39 40
40// ----------------------------------------------------------------------------- 41/* -------------------------------------------------------------------------- */
41 42
42MODULE_DESCRIPTION("uPD64031A driver"); 43MODULE_DESCRIPTION("uPD64031A driver");
43MODULE_AUTHOR("T. Adachi, Takeru KOMORIYA, Hans Verkuil"); 44MODULE_AUTHOR("T. Adachi, Takeru KOMORIYA, Hans Verkuil");
44MODULE_LICENSE("GPL"); 45MODULE_LICENSE("GPL");
45 46
46static int debug = 0; 47static int debug;
47module_param(debug, int, 0644); 48module_param(debug, int, 0644);
48 49
49MODULE_PARM_DESC(debug, "Debug level (0-1)"); 50MODULE_PARM_DESC(debug, "Debug level (0-1)");
50 51
51static unsigned short normal_i2c[] = { 0x24 >> 1, 0x26 >> 1, I2C_CLIENT_END };
52
53
54I2C_CLIENT_INSMOD;
55 52
56enum { 53enum {
57 R00 = 0, R01, R02, R03, R04, 54 R00 = 0, R01, R02, R03, R04,
@@ -99,7 +96,7 @@ static void upd64031a_write(struct i2c_client *client, u8 reg, u8 val)
99 96
100 buf[0] = reg; 97 buf[0] = reg;
101 buf[1] = val; 98 buf[1] = val;
102 v4l_dbg(1, debug, client, "writing reg addr: %02X val: %02X\n", reg, val); 99 v4l_dbg(1, debug, client, "write reg: %02X val: %02X\n", reg, val);
103 if (i2c_master_send(client, buf, 2) != 2) 100 if (i2c_master_send(client, buf, 2) != 2)
104 v4l_err(client, "I/O error write 0x%02x/0x%02x\n", reg, val); 101 v4l_err(client, "I/O error write 0x%02x/0x%02x\n", reg, val);
105} 102}
@@ -119,7 +116,7 @@ static void upd64031a_change(struct i2c_client *client)
119 116
120/* ------------------------------------------------------------------------ */ 117/* ------------------------------------------------------------------------ */
121 118
122static int upd64031a_command(struct i2c_client *client, unsigned int cmd, void *arg) 119static int upd64031a_command(struct i2c_client *client, unsigned cmd, void *arg)
123{ 120{
124 struct upd64031a_state *state = i2c_get_clientdata(client); 121 struct upd64031a_state *state = i2c_get_clientdata(client);
125 struct v4l2_routing *route = arg; 122 struct v4l2_routing *route = arg;
@@ -143,8 +140,10 @@ static int upd64031a_command(struct i2c_client *client, unsigned int cmd, void *
143 140
144 state->gr_mode = (route->input & 3) << 6; 141 state->gr_mode = (route->input & 3) << 6;
145 state->direct_3dycs_connect = (route->input & 0xc) << 4; 142 state->direct_3dycs_connect = (route->input & 0xc) << 4;
146 state->ext_comp_sync = (route->input & UPD64031A_COMPOSITE_EXTERNAL) << 1; 143 state->ext_comp_sync =
147 state->ext_vert_sync = (route->input & UPD64031A_VERTICAL_EXTERNAL) << 2; 144 (route->input & UPD64031A_COMPOSITE_EXTERNAL) << 1;
145 state->ext_vert_sync =
146 (route->input & UPD64031A_VERTICAL_EXTERNAL) << 2;
148 r00 = (state->regs[R00] & ~GR_MODE_MASK) | state->gr_mode; 147 r00 = (state->regs[R00] & ~GR_MODE_MASK) | state->gr_mode;
149 r05 = (state->regs[R00] & ~SYNC_CIRCUIT_MASK) | 148 r05 = (state->regs[R00] & ~SYNC_CIRCUIT_MASK) |
150 state->ext_comp_sync | state->ext_vert_sync; 149 state->ext_comp_sync | state->ext_vert_sync;
@@ -168,20 +167,23 @@ static int upd64031a_command(struct i2c_client *client, unsigned int cmd, void *
168 { 167 {
169 struct v4l2_register *reg = arg; 168 struct v4l2_register *reg = arg;
170 169
171 if (!v4l2_chip_match_i2c_client(client, reg->match_type, reg->match_chip)) 170 if (!v4l2_chip_match_i2c_client(client,
171 reg->match_type, reg->match_chip))
172 return -EINVAL; 172 return -EINVAL;
173 if (!capable(CAP_SYS_ADMIN)) 173 if (!capable(CAP_SYS_ADMIN))
174 return -EPERM; 174 return -EPERM;
175 if (cmd == VIDIOC_DBG_G_REGISTER) 175 if (cmd == VIDIOC_DBG_G_REGISTER) {
176 reg->val = upd64031a_read(client, reg->reg & 0xff); 176 reg->val = upd64031a_read(client, reg->reg & 0xff);
177 else 177 break;
178 upd64031a_write(client, reg->reg & 0xff, reg->val & 0xff); 178 }
179 upd64031a_write(client, reg->reg & 0xff, reg->val & 0xff);
179 break; 180 break;
180 } 181 }
181#endif 182#endif
182 183
183 case VIDIOC_G_CHIP_IDENT: 184 case VIDIOC_G_CHIP_IDENT:
184 return v4l2_chip_ident_i2c_client(client, arg, V4L2_IDENT_UPD64031A, 0); 185 return v4l2_chip_ident_i2c_client(client, arg,
186 V4L2_IDENT_UPD64031A, 0);
185 187
186 default: 188 default:
187 break; 189 break;
@@ -193,90 +195,43 @@ static int upd64031a_command(struct i2c_client *client, unsigned int cmd, void *
193 195
194/* i2c implementation */ 196/* i2c implementation */
195 197
196static struct i2c_driver i2c_driver; 198static int upd64031a_probe(struct i2c_client *client)
197
198static int upd64031a_attach(struct i2c_adapter *adapter, int address, int kind)
199{ 199{
200 struct i2c_client *client;
201 struct upd64031a_state *state; 200 struct upd64031a_state *state;
202 int i; 201 int i;
203 202
204 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 203 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
205 return 0; 204 return -EIO;
206
207 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
208 if (client == NULL) {
209 return -ENOMEM;
210 }
211
212 client->addr = address;
213 client->adapter = adapter;
214 client->driver = &i2c_driver;
215 snprintf(client->name, sizeof(client->name) - 1, "uPD64031A");
216 205
217 v4l_info(client, "chip found @ 0x%x (%s)\n", address << 1, adapter->name); 206 v4l_info(client, "chip found @ 0x%x (%s)\n",
207 client->addr << 1, client->adapter->name);
218 208
219 state = kmalloc(sizeof(struct upd64031a_state), GFP_KERNEL); 209 state = kmalloc(sizeof(struct upd64031a_state), GFP_KERNEL);
220 if (state == NULL) { 210 if (state == NULL)
221 kfree(client);
222 return -ENOMEM; 211 return -ENOMEM;
223 }
224 i2c_set_clientdata(client, state); 212 i2c_set_clientdata(client, state);
225 memcpy(state->regs, upd64031a_init, sizeof(state->regs)); 213 memcpy(state->regs, upd64031a_init, sizeof(state->regs));
226 state->gr_mode = UPD64031A_GR_ON << 6; 214 state->gr_mode = UPD64031A_GR_ON << 6;
227 state->direct_3dycs_connect = UPD64031A_3DYCS_COMPOSITE << 4; 215 state->direct_3dycs_connect = UPD64031A_3DYCS_COMPOSITE << 4;
228 state->ext_comp_sync = state->ext_vert_sync = 0; 216 state->ext_comp_sync = state->ext_vert_sync = 0;
229 for (i = 0; i < TOT_REGS; i++) { 217 for (i = 0; i < TOT_REGS; i++)
230 upd64031a_write(client, i, state->regs[i]); 218 upd64031a_write(client, i, state->regs[i]);
231 }
232
233 i2c_attach_client(client);
234
235 return 0; 219 return 0;
236} 220}
237 221
238static int upd64031a_probe(struct i2c_adapter *adapter) 222static int upd64031a_remove(struct i2c_client *client)
239{ 223{
240 if (adapter->class & I2C_CLASS_TV_ANALOG) 224 kfree(i2c_get_clientdata(client));
241 return i2c_probe(adapter, &addr_data, upd64031a_attach);
242 return 0;
243}
244
245static int upd64031a_detach(struct i2c_client *client)
246{
247 int err;
248
249 err = i2c_detach_client(client);
250 if (err)
251 return err;
252
253 kfree(client);
254 return 0; 225 return 0;
255} 226}
256 227
257/* ----------------------------------------------------------------------- */ 228/* ----------------------------------------------------------------------- */
258 229
259/* i2c implementation */ 230
260static struct i2c_driver i2c_driver = { 231static struct v4l2_i2c_driver_data v4l2_i2c_data = {
261 .driver = { 232 .name = "upd64031a",
262 .name = "upd64031a", 233 .driverid = I2C_DRIVERID_UPD64031A,
263 },
264 .id = I2C_DRIVERID_UPD64031A,
265 .attach_adapter = upd64031a_probe,
266 .detach_client = upd64031a_detach,
267 .command = upd64031a_command, 234 .command = upd64031a_command,
235 .probe = upd64031a_probe,
236 .remove = upd64031a_remove,
268}; 237};
269
270
271static int __init upd64031a_init_module(void)
272{
273 return i2c_add_driver(&i2c_driver);
274}
275
276static void __exit upd64031a_exit_module(void)
277{
278 i2c_del_driver(&i2c_driver);
279}
280
281module_init(upd64031a_init_module);
282module_exit(upd64031a_exit_module);
diff --git a/drivers/media/video/upd64083.c b/drivers/media/video/upd64083.c
index 401bd21f46eb..2d9a88f70c85 100644
--- a/drivers/media/video/upd64083.c
+++ b/drivers/media/video/upd64083.c
@@ -17,7 +17,8 @@
17 * 17 *
18 * You should have received a copy of the GNU General Public License 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 19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
21 */ 22 */
22 23
23#include <linux/version.h> 24#include <linux/version.h>
@@ -27,21 +28,18 @@
27#include <linux/videodev2.h> 28#include <linux/videodev2.h>
28#include <media/v4l2-common.h> 29#include <media/v4l2-common.h>
29#include <media/v4l2-chip-ident.h> 30#include <media/v4l2-chip-ident.h>
31#include <media/v4l2-i2c-drv.h>
30#include <media/upd64083.h> 32#include <media/upd64083.h>
31 33
32MODULE_DESCRIPTION("uPD64083 driver"); 34MODULE_DESCRIPTION("uPD64083 driver");
33MODULE_AUTHOR("T. Adachi, Takeru KOMORIYA, Hans Verkuil"); 35MODULE_AUTHOR("T. Adachi, Takeru KOMORIYA, Hans Verkuil");
34MODULE_LICENSE("GPL"); 36MODULE_LICENSE("GPL");
35 37
36static int debug = 0; 38static int debug;
37module_param(debug, bool, 0644); 39module_param(debug, bool, 0644);
38 40
39MODULE_PARM_DESC(debug, "Debug level (0-1)"); 41MODULE_PARM_DESC(debug, "Debug level (0-1)");
40 42
41static unsigned short normal_i2c[] = { 0xb8 >> 1, 0xba >> 1, I2C_CLIENT_END };
42
43
44I2C_CLIENT_INSMOD;
45 43
46enum { 44enum {
47 R00 = 0, R01, R02, R03, R04, 45 R00 = 0, R01, R02, R03, R04,
@@ -88,7 +86,7 @@ static void upd64083_write(struct i2c_client *client, u8 reg, u8 val)
88 86
89 buf[0] = reg; 87 buf[0] = reg;
90 buf[1] = val; 88 buf[1] = val;
91 v4l_dbg(1, debug, client, "writing reg addr: %02x val: %02x\n", reg, val); 89 v4l_dbg(1, debug, client, "write reg: %02x val: %02x\n", reg, val);
92 if (i2c_master_send(client, buf, 2) != 2) 90 if (i2c_master_send(client, buf, 2) != 2)
93 v4l_err(client, "I/O error write 0x%02x/0x%02x\n", reg, val); 91 v4l_err(client, "I/O error write 0x%02x/0x%02x\n", reg, val);
94} 92}
@@ -109,7 +107,7 @@ static u8 upd64083_read(struct i2c_client *client, u8 reg)
109 107
110/* ------------------------------------------------------------------------ */ 108/* ------------------------------------------------------------------------ */
111 109
112static int upd64083_command(struct i2c_client *client, unsigned int cmd, void *arg) 110static int upd64083_command(struct i2c_client *client, unsigned cmd, void *arg)
113{ 111{
114 struct upd64083_state *state = i2c_get_clientdata(client); 112 struct upd64083_state *state = i2c_get_clientdata(client);
115 struct v4l2_routing *route = arg; 113 struct v4l2_routing *route = arg;
@@ -145,20 +143,23 @@ static int upd64083_command(struct i2c_client *client, unsigned int cmd, void *a
145 { 143 {
146 struct v4l2_register *reg = arg; 144 struct v4l2_register *reg = arg;
147 145
148 if (!v4l2_chip_match_i2c_client(client, reg->match_type, reg->match_chip)) 146 if (!v4l2_chip_match_i2c_client(client,
147 reg->match_type, reg->match_chip))
149 return -EINVAL; 148 return -EINVAL;
150 if (!capable(CAP_SYS_ADMIN)) 149 if (!capable(CAP_SYS_ADMIN))
151 return -EPERM; 150 return -EPERM;
152 if (cmd == VIDIOC_DBG_G_REGISTER) 151 if (cmd == VIDIOC_DBG_G_REGISTER) {
153 reg->val = upd64083_read(client, reg->reg & 0xff); 152 reg->val = upd64083_read(client, reg->reg & 0xff);
154 else 153 break;
155 upd64083_write(client, reg->reg & 0xff, reg->val & 0xff); 154 }
155 upd64083_write(client, reg->reg & 0xff, reg->val & 0xff);
156 break; 156 break;
157 } 157 }
158#endif 158#endif
159 159
160 case VIDIOC_G_CHIP_IDENT: 160 case VIDIOC_G_CHIP_IDENT:
161 return v4l2_chip_ident_i2c_client(client, arg, V4L2_IDENT_UPD64083, 0); 161 return v4l2_chip_ident_i2c_client(client, arg,
162 V4L2_IDENT_UPD64083, 0);
162 163
163 default: 164 default:
164 break; 165 break;
@@ -171,89 +172,43 @@ static int upd64083_command(struct i2c_client *client, unsigned int cmd, void *a
171 172
172/* i2c implementation */ 173/* i2c implementation */
173 174
174static struct i2c_driver i2c_driver; 175static int upd64083_probe(struct i2c_client *client)
175
176static int upd64083_attach(struct i2c_adapter *adapter, int address, int kind)
177{ 176{
178 struct i2c_client *client;
179 struct upd64083_state *state; 177 struct upd64083_state *state;
180 int i; 178 int i;
181 179
182 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 180 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
183 return 0; 181 return -EIO;
184
185 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
186 if (client == NULL) {
187 return -ENOMEM;
188 }
189
190 client->addr = address;
191 client->adapter = adapter;
192 client->driver = &i2c_driver;
193 snprintf(client->name, sizeof(client->name) - 1, "uPD64083");
194 182
195 v4l_info(client, "chip found @ 0x%x (%s)\n", address << 1, adapter->name); 183 v4l_info(client, "chip found @ 0x%x (%s)\n",
184 client->addr << 1, client->adapter->name);
196 185
197 state = kmalloc(sizeof(struct upd64083_state), GFP_KERNEL); 186 state = kmalloc(sizeof(struct upd64083_state), GFP_KERNEL);
198 if (state == NULL) { 187 if (state == NULL)
199 kfree(client);
200 return -ENOMEM; 188 return -ENOMEM;
201 }
202 i2c_set_clientdata(client, state); 189 i2c_set_clientdata(client, state);
203 /* Initially assume that a ghost reduction chip is present */ 190 /* Initially assume that a ghost reduction chip is present */
204 state->mode = 0; /* YCS mode */ 191 state->mode = 0; /* YCS mode */
205 state->ext_y_adc = (1 << 5); 192 state->ext_y_adc = (1 << 5);
206 memcpy(state->regs, upd64083_init, TOT_REGS); 193 memcpy(state->regs, upd64083_init, TOT_REGS);
207 for (i = 0; i < TOT_REGS; i++) { 194 for (i = 0; i < TOT_REGS; i++)
208 upd64083_write(client, i, state->regs[i]); 195 upd64083_write(client, i, state->regs[i]);
209 }
210 i2c_attach_client(client);
211
212 return 0;
213}
214
215static int upd64083_probe(struct i2c_adapter *adapter)
216{
217 if (adapter->class & I2C_CLASS_TV_ANALOG)
218 return i2c_probe(adapter, &addr_data, upd64083_attach);
219 return 0; 196 return 0;
220} 197}
221 198
222static int upd64083_detach(struct i2c_client *client) 199static int upd64083_remove(struct i2c_client *client)
223{ 200{
224 int err; 201 kfree(i2c_get_clientdata(client));
225
226 err = i2c_detach_client(client);
227 if (err)
228 return err;
229
230 kfree(client);
231 return 0; 202 return 0;
232} 203}
233 204
234/* ----------------------------------------------------------------------- */ 205/* ----------------------------------------------------------------------- */
235 206
236/* i2c implementation */ 207
237static struct i2c_driver i2c_driver = { 208static struct v4l2_i2c_driver_data v4l2_i2c_data = {
238 .driver = { 209 .name = "upd64083",
239 .name = "upd64083", 210 .driverid = I2C_DRIVERID_UPD64083,
240 },
241 .id = I2C_DRIVERID_UPD64083,
242 .attach_adapter = upd64083_probe,
243 .detach_client = upd64083_detach,
244 .command = upd64083_command, 211 .command = upd64083_command,
212 .probe = upd64083_probe,
213 .remove = upd64083_remove,
245}; 214};
246
247
248static int __init upd64083_init_module(void)
249{
250 return i2c_add_driver(&i2c_driver);
251}
252
253static void __exit upd64083_exit_module(void)
254{
255 i2c_del_driver(&i2c_driver);
256}
257
258module_init(upd64083_init_module);
259module_exit(upd64083_exit_module);
diff --git a/drivers/media/video/usbvision/usbvision-cards.c b/drivers/media/video/usbvision/usbvision-cards.c
index f09eb102731b..503b13beb922 100644
--- a/drivers/media/video/usbvision/usbvision-cards.c
+++ b/drivers/media/video/usbvision/usbvision-cards.c
@@ -901,6 +901,20 @@ struct usbvision_device_data_st usbvision_device_data[] = {
901 .Y_Offset = -1, 901 .Y_Offset = -1,
902 .ModelString = "Pinnacle Studio PCTV USB (NTSC) FM", 902 .ModelString = "Pinnacle Studio PCTV USB (NTSC) FM",
903 }, 903 },
904 [PINNA_PCTV_USB_NTSC_FM_V3] = {
905 .Interface = -1,
906 .Codec = CODEC_SAA7111,
907 .VideoChannels = 3,
908 .VideoNorm = V4L2_STD_NTSC,
909 .AudioChannels = 1,
910 .Radio = 1,
911 .vbi = 1,
912 .Tuner = 1,
913 .TunerType = TUNER_PHILIPS_NTSC_M,
914 .X_Offset = -1,
915 .Y_Offset = -1,
916 .ModelString = "Pinnacle Studio PCTV USB (NTSC) FM V3",
917 },
904 [PINNA_PCTV_USB_PAL_FM_V2] = { 918 [PINNA_PCTV_USB_PAL_FM_V2] = {
905 .Interface = -1, 919 .Interface = -1,
906 .Codec = CODEC_SAA7113, 920 .Codec = CODEC_SAA7113,
@@ -1044,7 +1058,7 @@ struct usb_device_id usbvision_table [] = {
1044 { USB_DEVICE(0x0573, 0x4d2a), .driver_info=HPG_WINTV_PRO_NTSC_MN }, 1058 { USB_DEVICE(0x0573, 0x4d2a), .driver_info=HPG_WINTV_PRO_NTSC_MN },
1045 { USB_DEVICE(0x0573, 0x4d2b), .driver_info=HPG_WINTV_PRO_NTSC_MN_V2 }, 1059 { USB_DEVICE(0x0573, 0x4d2b), .driver_info=HPG_WINTV_PRO_NTSC_MN_V2 },
1046 { USB_DEVICE(0x0573, 0x4d2c), .driver_info=HPG_WINTV_PRO_PAL }, 1060 { USB_DEVICE(0x0573, 0x4d2c), .driver_info=HPG_WINTV_PRO_PAL },
1047 { USB_DEVICE(0x0573, 0x4d20), .driver_info=HPG_WINTV_PRO_NTSC_MN_V3 }, 1061 { USB_DEVICE(0x0573, 0x4d20), .driver_info = HPG_WINTV_PRO_NTSC_MN_V3 },
1048 { USB_DEVICE(0x0573, 0x4d21), .driver_info=HPG_WINTV_PRO_PAL_BG }, 1062 { USB_DEVICE(0x0573, 0x4d21), .driver_info=HPG_WINTV_PRO_PAL_BG },
1049 { USB_DEVICE(0x0573, 0x4d22), .driver_info=HPG_WINTV_PRO_PAL_I }, 1063 { USB_DEVICE(0x0573, 0x4d22), .driver_info=HPG_WINTV_PRO_PAL_I },
1050 { USB_DEVICE(0x0573, 0x4d23), .driver_info=HPG_WINTV_PRO_PAL_SECAM_L }, 1064 { USB_DEVICE(0x0573, 0x4d23), .driver_info=HPG_WINTV_PRO_PAL_SECAM_L },
@@ -1074,6 +1088,8 @@ struct usb_device_id usbvision_table [] = {
1074 { USB_DEVICE(0x2304, 0x0110), .driver_info=PINNA_PCTV_USB_PAL_FM }, 1088 { USB_DEVICE(0x2304, 0x0110), .driver_info=PINNA_PCTV_USB_PAL_FM },
1075 { USB_DEVICE(0x2304, 0x0111), .driver_info=MIRO_PCTV_USB }, 1089 { USB_DEVICE(0x2304, 0x0111), .driver_info=MIRO_PCTV_USB },
1076 { USB_DEVICE(0x2304, 0x0112), .driver_info=PINNA_PCTV_USB_NTSC_FM }, 1090 { USB_DEVICE(0x2304, 0x0112), .driver_info=PINNA_PCTV_USB_NTSC_FM },
1091 { USB_DEVICE(0x2304, 0x0113),
1092 .driver_info = PINNA_PCTV_USB_NTSC_FM_V3 },
1077 { USB_DEVICE(0x2304, 0x0210), .driver_info=PINNA_PCTV_USB_PAL_FM_V2 }, 1093 { USB_DEVICE(0x2304, 0x0210), .driver_info=PINNA_PCTV_USB_PAL_FM_V2 },
1078 { USB_DEVICE(0x2304, 0x0212), .driver_info=PINNA_PCTV_USB_NTSC_FM_V2 }, 1094 { USB_DEVICE(0x2304, 0x0212), .driver_info=PINNA_PCTV_USB_NTSC_FM_V2 },
1079 { USB_DEVICE(0x2304, 0x0214), .driver_info=PINNA_PCTV_USB_PAL_FM_V3 }, 1095 { USB_DEVICE(0x2304, 0x0214), .driver_info=PINNA_PCTV_USB_PAL_FM_V3 },
diff --git a/drivers/media/video/usbvision/usbvision-cards.h b/drivers/media/video/usbvision/usbvision-cards.h
index 512c5cee4145..9c6ad22960d8 100644
--- a/drivers/media/video/usbvision/usbvision-cards.h
+++ b/drivers/media/video/usbvision/usbvision-cards.h
@@ -62,5 +62,6 @@
62#define PINNA_LINX_VD_IN_CAB_PAL 61 62#define PINNA_LINX_VD_IN_CAB_PAL 61
63#define PINNA_PCTV_BUNGEE_PAL_FM 62 63#define PINNA_PCTV_BUNGEE_PAL_FM 62
64#define HPG_WINTV 63 64#define HPG_WINTV 63
65#define PINNA_PCTV_USB_NTSC_FM_V3 64
65 66
66extern const int usbvision_device_data_size; 67extern const int usbvision_device_data_size;
diff --git a/drivers/media/video/usbvision/usbvision-core.c b/drivers/media/video/usbvision/usbvision-core.c
index c7d5f9ed22d7..56775ab8b75d 100644
--- a/drivers/media/video/usbvision/usbvision-core.c
+++ b/drivers/media/video/usbvision/usbvision-core.c
@@ -69,6 +69,15 @@ static int SwitchSVideoInput = 0; // To help people with Black and White outpu
69module_param(SwitchSVideoInput, int, 0444); 69module_param(SwitchSVideoInput, int, 0444);
70MODULE_PARM_DESC(SwitchSVideoInput, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)"); 70MODULE_PARM_DESC(SwitchSVideoInput, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)");
71 71
72static unsigned int adjust_X_Offset = -1;
73module_param(adjust_X_Offset, int, 0644);
74MODULE_PARM_DESC(adjust_X_Offset, "adjust X offset display [core]");
75
76static unsigned int adjust_Y_Offset = -1;
77module_param(adjust_Y_Offset, int, 0644);
78MODULE_PARM_DESC(adjust_Y_Offset, "adjust Y offset display [core]");
79
80
72#define ENABLE_HEXDUMP 0 /* Enable if you need it */ 81#define ENABLE_HEXDUMP 0 /* Enable if you need it */
73 82
74 83
@@ -624,25 +633,29 @@ static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision
624 633
625 YUV_TO_RGB_BY_THE_BOOK(yuyv[0], yuyv[1], yuyv[3], rv, gv, bv); 634 YUV_TO_RGB_BY_THE_BOOK(yuyv[0], yuyv[1], yuyv[3], rv, gv, bv);
626 switch (frame->v4l2_format.format) { 635 switch (frame->v4l2_format.format) {
627 case V4L2_PIX_FMT_RGB565: 636 case V4L2_PIX_FMT_RGB565:
628 *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3)); 637 *f++ = (0x1F & rv) |
629 *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv); 638 (0xE0 & (gv << 5));
630 break; 639 *f++ = (0x07 & (gv >> 3)) |
631 case V4L2_PIX_FMT_RGB24: 640 (0xF8 & bv);
632 *f++ = bv; 641 break;
633 *f++ = gv; 642 case V4L2_PIX_FMT_RGB24:
634 *f++ = rv; 643 *f++ = rv;
635 break; 644 *f++ = gv;
636 case V4L2_PIX_FMT_RGB32: 645 *f++ = bv;
637 *f++ = bv; 646 break;
638 *f++ = gv; 647 case V4L2_PIX_FMT_RGB32:
639 *f++ = rv; 648 *f++ = rv;
640 f++; 649 *f++ = gv;
641 break; 650 *f++ = bv;
642 case V4L2_PIX_FMT_RGB555: 651 f++;
643 *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2)); 652 break;
644 *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1)); 653 case V4L2_PIX_FMT_RGB555:
645 break; 654 *f++ = (0x1F & rv) |
655 (0xE0 & (gv << 5));
656 *f++ = (0x03 & (gv >> 3)) |
657 (0x7C & (bv << 2));
658 break;
646 } 659 }
647 } 660 }
648 clipmask_index += clipmask_add; 661 clipmask_index += clipmask_add;
@@ -656,25 +669,29 @@ static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision
656 669
657 YUV_TO_RGB_BY_THE_BOOK(yuyv[2], yuyv[1], yuyv[3], rv, gv, bv); 670 YUV_TO_RGB_BY_THE_BOOK(yuyv[2], yuyv[1], yuyv[3], rv, gv, bv);
658 switch (frame->v4l2_format.format) { 671 switch (frame->v4l2_format.format) {
659 case V4L2_PIX_FMT_RGB565: 672 case V4L2_PIX_FMT_RGB565:
660 *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3)); 673 *f++ = (0x1F & rv) |
661 *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv); 674 (0xE0 & (gv << 5));
662 break; 675 *f++ = (0x07 & (gv >> 3)) |
663 case V4L2_PIX_FMT_RGB24: 676 (0xF8 & bv);
664 *f++ = bv; 677 break;
665 *f++ = gv; 678 case V4L2_PIX_FMT_RGB24:
666 *f++ = rv; 679 *f++ = rv;
667 break; 680 *f++ = gv;
668 case V4L2_PIX_FMT_RGB32: 681 *f++ = bv;
669 *f++ = bv; 682 break;
670 *f++ = gv; 683 case V4L2_PIX_FMT_RGB32:
671 *f++ = rv; 684 *f++ = rv;
672 f++; 685 *f++ = gv;
673 break; 686 *f++ = bv;
674 case V4L2_PIX_FMT_RGB555: 687 f++;
675 *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2)); 688 break;
676 *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1)); 689 case V4L2_PIX_FMT_RGB555:
677 break; 690 *f++ = (0x1F & rv) |
691 (0xE0 & (gv << 5));
692 *f++ = (0x03 & (gv >> 3)) |
693 (0x7C & (bv << 2));
694 break;
678 } 695 }
679 } 696 }
680 clipmask_index += clipmask_add; 697 clipmask_index += clipmask_add;
@@ -942,22 +959,26 @@ static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
942 *f++ = Y[Idx]; 959 *f++ = Y[Idx];
943 break; 960 break;
944 case V4L2_PIX_FMT_RGB555: 961 case V4L2_PIX_FMT_RGB555:
945 *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2)); 962 *f++ = (0x1F & rv) |
946 *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1)); 963 (0xE0 & (gv << 5));
964 *f++ = (0x03 & (gv >> 3)) |
965 (0x7C & (bv << 2));
947 break; 966 break;
948 case V4L2_PIX_FMT_RGB565: 967 case V4L2_PIX_FMT_RGB565:
949 *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3)); 968 *f++ = (0x1F & rv) |
950 *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv); 969 (0xE0 & (gv << 5));
970 *f++ = (0x07 & (gv >> 3)) |
971 (0xF8 & bv);
951 break; 972 break;
952 case V4L2_PIX_FMT_RGB24: 973 case V4L2_PIX_FMT_RGB24:
953 *f++ = bv;
954 *f++ = gv;
955 *f++ = rv; 974 *f++ = rv;
975 *f++ = gv;
976 *f++ = bv;
956 break; 977 break;
957 case V4L2_PIX_FMT_RGB32: 978 case V4L2_PIX_FMT_RGB32:
958 *f++ = bv;
959 *f++ = gv;
960 *f++ = rv; 979 *f++ = rv;
980 *f++ = gv;
981 *f++ = bv;
961 f++; 982 f++;
962 break; 983 break;
963 } 984 }
@@ -1071,28 +1092,33 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
1071 r_ = (y_ + ur) >> 16; 1092 r_ = (y_ + ur) >> 16;
1072 1093
1073 switch (frame->v4l2_format.format) { 1094 switch (frame->v4l2_format.format) {
1074 case V4L2_PIX_FMT_RGB565: 1095 case V4L2_PIX_FMT_RGB565:
1075 g = LIMIT_RGB(g_); 1096 g = LIMIT_RGB(g_);
1076 *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3)); 1097 *f_even++ =
1077 *f_even++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_)); 1098 (0x1F & LIMIT_RGB(r_)) |
1078 break; 1099 (0xE0 & (g << 5));
1079 case V4L2_PIX_FMT_RGB24: 1100 *f_even++ =
1080 *f_even++ = LIMIT_RGB(b_); 1101 (0x07 & (g >> 3)) |
1081 *f_even++ = LIMIT_RGB(g_); 1102 (0xF8 & LIMIT_RGB(b_));
1082 *f_even++ = LIMIT_RGB(r_); 1103 break;
1083 break; 1104 case V4L2_PIX_FMT_RGB24:
1084 case V4L2_PIX_FMT_RGB32: 1105 *f_even++ = LIMIT_RGB(r_);
1085 *f_even++ = LIMIT_RGB(b_); 1106 *f_even++ = LIMIT_RGB(g_);
1086 *f_even++ = LIMIT_RGB(g_); 1107 *f_even++ = LIMIT_RGB(b_);
1087 *f_even++ = LIMIT_RGB(r_); 1108 break;
1088 f_even++; 1109 case V4L2_PIX_FMT_RGB32:
1089 break; 1110 *f_even++ = LIMIT_RGB(r_);
1090 case V4L2_PIX_FMT_RGB555: 1111 *f_even++ = LIMIT_RGB(g_);
1091 g = LIMIT_RGB(g_); 1112 *f_even++ = LIMIT_RGB(b_);
1092 *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2)); 1113 f_even++;
1093 *f_even++ = (0x03 & ( g >> 6)) | 1114 break;
1094 (0x7C & (LIMIT_RGB(r_) >> 1)); 1115 case V4L2_PIX_FMT_RGB555:
1095 break; 1116 g = LIMIT_RGB(g_);
1117 *f_even++ = (0x1F & LIMIT_RGB(r_)) |
1118 (0xE0 & (g << 5));
1119 *f_even++ = (0x03 & (g >> 3)) |
1120 (0x7C & (LIMIT_RGB(b_) << 2));
1121 break;
1096 } 1122 }
1097 } 1123 }
1098 clipmask_even_index += clipmask_add; 1124 clipmask_even_index += clipmask_add;
@@ -1110,28 +1136,33 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
1110 r_ = (y_ + ur) >> 16; 1136 r_ = (y_ + ur) >> 16;
1111 1137
1112 switch (frame->v4l2_format.format) { 1138 switch (frame->v4l2_format.format) {
1113 case V4L2_PIX_FMT_RGB565: 1139 case V4L2_PIX_FMT_RGB565:
1114 g = LIMIT_RGB(g_); 1140 g = LIMIT_RGB(g_);
1115 *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3)); 1141 *f_even++ =
1116 *f_even++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_)); 1142 (0x1F & LIMIT_RGB(r_)) |
1117 break; 1143 (0xE0 & (g << 5));
1118 case V4L2_PIX_FMT_RGB24: 1144 *f_even++ =
1119 *f_even++ = LIMIT_RGB(b_); 1145 (0x07 & (g >> 3)) |
1120 *f_even++ = LIMIT_RGB(g_); 1146 (0xF8 & LIMIT_RGB(b_));
1121 *f_even++ = LIMIT_RGB(r_); 1147 break;
1122 break; 1148 case V4L2_PIX_FMT_RGB24:
1123 case V4L2_PIX_FMT_RGB32: 1149 *f_even++ = LIMIT_RGB(r_);
1124 *f_even++ = LIMIT_RGB(b_); 1150 *f_even++ = LIMIT_RGB(g_);
1125 *f_even++ = LIMIT_RGB(g_); 1151 *f_even++ = LIMIT_RGB(b_);
1126 *f_even++ = LIMIT_RGB(r_); 1152 break;
1127 f_even++; 1153 case V4L2_PIX_FMT_RGB32:
1128 break; 1154 *f_even++ = LIMIT_RGB(r_);
1129 case V4L2_PIX_FMT_RGB555: 1155 *f_even++ = LIMIT_RGB(g_);
1130 g = LIMIT_RGB(g_); 1156 *f_even++ = LIMIT_RGB(b_);
1131 *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2)); 1157 f_even++;
1132 *f_even++ = (0x03 & ( g >> 6)) | 1158 break;
1133 (0x7C & (LIMIT_RGB(r_) >> 1)); 1159 case V4L2_PIX_FMT_RGB555:
1134 break; 1160 g = LIMIT_RGB(g_);
1161 *f_even++ = (0x1F & LIMIT_RGB(r_)) |
1162 (0xE0 & (g << 5));
1163 *f_even++ = (0x03 & (g >> 3)) |
1164 (0x7C & (LIMIT_RGB(b_) << 2));
1165 break;
1135 } 1166 }
1136 } 1167 }
1137 clipmask_even_index += clipmask_add; 1168 clipmask_even_index += clipmask_add;
@@ -1151,28 +1182,33 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
1151 r_ = (y_ + ur) >> 16; 1182 r_ = (y_ + ur) >> 16;
1152 1183
1153 switch (frame->v4l2_format.format) { 1184 switch (frame->v4l2_format.format) {
1154 case V4L2_PIX_FMT_RGB565: 1185 case V4L2_PIX_FMT_RGB565:
1155 g = LIMIT_RGB(g_); 1186 g = LIMIT_RGB(g_);
1156 *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3)); 1187 *f_odd++ =
1157 *f_odd++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_)); 1188 (0x1F & LIMIT_RGB(r_)) |
1158 break; 1189 (0xE0 & (g << 5));
1159 case V4L2_PIX_FMT_RGB24: 1190 *f_odd++ =
1160 *f_odd++ = LIMIT_RGB(b_); 1191 (0x07 & (g >> 3)) |
1161 *f_odd++ = LIMIT_RGB(g_); 1192 (0xF8 & LIMIT_RGB(b_));
1162 *f_odd++ = LIMIT_RGB(r_); 1193 break;
1163 break; 1194 case V4L2_PIX_FMT_RGB24:
1164 case V4L2_PIX_FMT_RGB32: 1195 *f_odd++ = LIMIT_RGB(r_);
1165 *f_odd++ = LIMIT_RGB(b_); 1196 *f_odd++ = LIMIT_RGB(g_);
1166 *f_odd++ = LIMIT_RGB(g_); 1197 *f_odd++ = LIMIT_RGB(b_);
1167 *f_odd++ = LIMIT_RGB(r_); 1198 break;
1168 f_odd++; 1199 case V4L2_PIX_FMT_RGB32:
1169 break; 1200 *f_odd++ = LIMIT_RGB(r_);
1170 case V4L2_PIX_FMT_RGB555: 1201 *f_odd++ = LIMIT_RGB(g_);
1171 g = LIMIT_RGB(g_); 1202 *f_odd++ = LIMIT_RGB(b_);
1172 *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2)); 1203 f_odd++;
1173 *f_odd++ = (0x03 & ( g >> 6)) | 1204 break;
1174 (0x7C & (LIMIT_RGB(r_) >> 1)); 1205 case V4L2_PIX_FMT_RGB555:
1175 break; 1206 g = LIMIT_RGB(g_);
1207 *f_odd++ = (0x1F & LIMIT_RGB(r_)) |
1208 (0xE0 & (g << 5));
1209 *f_odd++ = (0x03 & (g >> 3)) |
1210 (0x7C & (LIMIT_RGB(b_) << 2));
1211 break;
1176 } 1212 }
1177 } 1213 }
1178 clipmask_odd_index += clipmask_add; 1214 clipmask_odd_index += clipmask_add;
@@ -1190,28 +1226,33 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
1190 r_ = (y_ + ur) >> 16; 1226 r_ = (y_ + ur) >> 16;
1191 1227
1192 switch (frame->v4l2_format.format) { 1228 switch (frame->v4l2_format.format) {
1193 case V4L2_PIX_FMT_RGB565: 1229 case V4L2_PIX_FMT_RGB565:
1194 g = LIMIT_RGB(g_); 1230 g = LIMIT_RGB(g_);
1195 *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3)); 1231 *f_odd++ =
1196 *f_odd++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_)); 1232 (0x1F & LIMIT_RGB(r_)) |
1197 break; 1233 (0xE0 & (g << 5));
1198 case V4L2_PIX_FMT_RGB24: 1234 *f_odd++ =
1199 *f_odd++ = LIMIT_RGB(b_); 1235 (0x07 & (g >> 3)) |
1200 *f_odd++ = LIMIT_RGB(g_); 1236 (0xF8 & LIMIT_RGB(b_));
1201 *f_odd++ = LIMIT_RGB(r_); 1237 break;
1202 break; 1238 case V4L2_PIX_FMT_RGB24:
1203 case V4L2_PIX_FMT_RGB32: 1239 *f_odd++ = LIMIT_RGB(r_);
1204 *f_odd++ = LIMIT_RGB(b_); 1240 *f_odd++ = LIMIT_RGB(g_);
1205 *f_odd++ = LIMIT_RGB(g_); 1241 *f_odd++ = LIMIT_RGB(b_);
1206 *f_odd++ = LIMIT_RGB(r_); 1242 break;
1207 f_odd++; 1243 case V4L2_PIX_FMT_RGB32:
1208 break; 1244 *f_odd++ = LIMIT_RGB(r_);
1209 case V4L2_PIX_FMT_RGB555: 1245 *f_odd++ = LIMIT_RGB(g_);
1210 g = LIMIT_RGB(g_); 1246 *f_odd++ = LIMIT_RGB(b_);
1211 *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2)); 1247 f_odd++;
1212 *f_odd++ = (0x03 & ( g >> 6)) | 1248 break;
1213 (0x7C & (LIMIT_RGB(r_) >> 1)); 1249 case V4L2_PIX_FMT_RGB555:
1214 break; 1250 g = LIMIT_RGB(g_);
1251 *f_odd++ = (0x1F & LIMIT_RGB(r_)) |
1252 (0xE0 & (g << 5));
1253 *f_odd++ = (0x03 & (g >> 3)) |
1254 (0x7C & (LIMIT_RGB(b_) << 2));
1255 break;
1215 } 1256 }
1216 } 1257 }
1217 clipmask_odd_index += clipmask_add; 1258 clipmask_odd_index += clipmask_add;
@@ -1561,13 +1602,10 @@ static int usbvision_write_reg_irq(struct usb_usbvision *usbvision,int address,
1561 if (len > 8) { 1602 if (len > 8) {
1562 return -EFAULT; 1603 return -EFAULT;
1563 } 1604 }
1564// down(&usbvision->ctrlUrbLock);
1565 if (usbvision->ctrlUrbBusy) { 1605 if (usbvision->ctrlUrbBusy) {
1566// up(&usbvision->ctrlUrbLock);
1567 return -EBUSY; 1606 return -EBUSY;
1568 } 1607 }
1569 usbvision->ctrlUrbBusy = 1; 1608 usbvision->ctrlUrbBusy = 1;
1570// up(&usbvision->ctrlUrbLock);
1571 1609
1572 usbvision->ctrlUrbSetup.bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT; 1610 usbvision->ctrlUrbSetup.bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
1573 usbvision->ctrlUrbSetup.bRequest = USBVISION_OP_CODE; 1611 usbvision->ctrlUrbSetup.bRequest = USBVISION_OP_CODE;
@@ -2100,11 +2138,21 @@ int usbvision_set_input(struct usb_usbvision *usbvision)
2100 value[5]=(usbvision_device_data[usbvision->DevModel].X_Offset & 0x0300) >> 8; 2138 value[5]=(usbvision_device_data[usbvision->DevModel].X_Offset & 0x0300) >> 8;
2101 } 2139 }
2102 2140
2141 if (adjust_X_Offset != -1) {
2142 value[4] = adjust_X_Offset & 0xff;
2143 value[5] = (adjust_X_Offset & 0x0300) >> 8;
2144 }
2145
2103 if (usbvision_device_data[usbvision->DevModel].Y_Offset >= 0) { 2146 if (usbvision_device_data[usbvision->DevModel].Y_Offset >= 0) {
2104 value[6]=usbvision_device_data[usbvision->DevModel].Y_Offset & 0xff; 2147 value[6]=usbvision_device_data[usbvision->DevModel].Y_Offset & 0xff;
2105 value[7]=(usbvision_device_data[usbvision->DevModel].Y_Offset & 0x0300) >> 8; 2148 value[7]=(usbvision_device_data[usbvision->DevModel].Y_Offset & 0x0300) >> 8;
2106 } 2149 }
2107 2150
2151 if (adjust_Y_Offset != -1) {
2152 value[6] = adjust_Y_Offset & 0xff;
2153 value[7] = (adjust_Y_Offset & 0x0300) >> 8;
2154 }
2155
2108 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), 2156 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2109 USBVISION_OP_CODE, /* USBVISION specific code */ 2157 USBVISION_OP_CODE, /* USBVISION specific code */
2110 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0, 2158 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0,
@@ -2242,14 +2290,18 @@ static void call_usbvision_power_off(struct work_struct *work)
2242 struct usb_usbvision *usbvision = container_of(work, struct usb_usbvision, powerOffWork); 2290 struct usb_usbvision *usbvision = container_of(work, struct usb_usbvision, powerOffWork);
2243 2291
2244 PDEBUG(DBG_FUNC, ""); 2292 PDEBUG(DBG_FUNC, "");
2245 down_interruptible(&usbvision->lock); 2293 if(mutex_lock_interruptible(&usbvision->lock)) {
2294 return;
2295 }
2296
2297
2246 if(usbvision->user == 0) { 2298 if(usbvision->user == 0) {
2247 usbvision_i2c_unregister(usbvision); 2299 usbvision_i2c_unregister(usbvision);
2248 2300
2249 usbvision_power_off(usbvision); 2301 usbvision_power_off(usbvision);
2250 usbvision->initialized = 0; 2302 usbvision->initialized = 0;
2251 } 2303 }
2252 up(&usbvision->lock); 2304 mutex_unlock(&usbvision->lock);
2253} 2305}
2254 2306
2255static void usbvision_powerOffTimer(unsigned long data) 2307static void usbvision_powerOffTimer(unsigned long data)
diff --git a/drivers/media/video/usbvision/usbvision-video.c b/drivers/media/video/usbvision/usbvision-video.c
index 36e689fa16c0..b52b826a30be 100644
--- a/drivers/media/video/usbvision/usbvision-video.c
+++ b/drivers/media/video/usbvision/usbvision-video.c
@@ -410,7 +410,7 @@ static int usbvision_v4l2_open(struct inode *inode, struct file *file)
410 410
411 /* If so far no errors then we shall start the camera */ 411 /* If so far no errors then we shall start the camera */
412 if (!errCode) { 412 if (!errCode) {
413 down(&usbvision->lock); 413 mutex_lock(&usbvision->lock);
414 if (usbvision->power == 0) { 414 if (usbvision->power == 0) {
415 usbvision_power_on(usbvision); 415 usbvision_power_on(usbvision);
416 usbvision_i2c_register(usbvision); 416 usbvision_i2c_register(usbvision);
@@ -439,7 +439,7 @@ static int usbvision_v4l2_open(struct inode *inode, struct file *file)
439 usbvision->initialized = 0; 439 usbvision->initialized = 0;
440 } 440 }
441 } 441 }
442 up(&usbvision->lock); 442 mutex_unlock(&usbvision->lock);
443 } 443 }
444 444
445 if (errCode) { 445 if (errCode) {
@@ -467,7 +467,7 @@ static int usbvision_v4l2_close(struct inode *inode, struct file *file)
467 (struct usb_usbvision *) video_get_drvdata(dev); 467 (struct usb_usbvision *) video_get_drvdata(dev);
468 468
469 PDEBUG(DBG_IO, "close"); 469 PDEBUG(DBG_IO, "close");
470 down(&usbvision->lock); 470 mutex_lock(&usbvision->lock);
471 471
472 usbvision_audio_off(usbvision); 472 usbvision_audio_off(usbvision);
473 usbvision_restart_isoc(usbvision); 473 usbvision_restart_isoc(usbvision);
@@ -487,7 +487,7 @@ static int usbvision_v4l2_close(struct inode *inode, struct file *file)
487 usbvision->initialized = 0; 487 usbvision->initialized = 0;
488 } 488 }
489 489
490 up(&usbvision->lock); 490 mutex_unlock(&usbvision->lock);
491 491
492 if (usbvision->remove_pending) { 492 if (usbvision->remove_pending) {
493 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__); 493 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
@@ -647,13 +647,13 @@ static int vidioc_s_input (struct file *file, void *priv, unsigned int input)
647 if ((input >= usbvision->video_inputs) || (input < 0) ) 647 if ((input >= usbvision->video_inputs) || (input < 0) )
648 return -EINVAL; 648 return -EINVAL;
649 649
650 down(&usbvision->lock); 650 mutex_lock(&usbvision->lock);
651 usbvision_muxsel(usbvision, input); 651 usbvision_muxsel(usbvision, input);
652 usbvision_set_input(usbvision); 652 usbvision_set_input(usbvision);
653 usbvision_set_output(usbvision, 653 usbvision_set_output(usbvision,
654 usbvision->curwidth, 654 usbvision->curwidth,
655 usbvision->curheight); 655 usbvision->curheight);
656 up(&usbvision->lock); 656 mutex_unlock(&usbvision->lock);
657 return 0; 657 return 0;
658} 658}
659 659
@@ -664,10 +664,10 @@ static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
664 (struct usb_usbvision *) video_get_drvdata(dev); 664 (struct usb_usbvision *) video_get_drvdata(dev);
665 usbvision->tvnormId=*id; 665 usbvision->tvnormId=*id;
666 666
667 down(&usbvision->lock); 667 mutex_lock(&usbvision->lock);
668 call_i2c_clients(usbvision, VIDIOC_S_STD, 668 call_i2c_clients(usbvision, VIDIOC_S_STD,
669 &usbvision->tvnormId); 669 &usbvision->tvnormId);
670 up(&usbvision->lock); 670 mutex_unlock(&usbvision->lock);
671 /* propagate the change to the decoder */ 671 /* propagate the change to the decoder */
672 usbvision_muxsel(usbvision, usbvision->ctl_input); 672 usbvision_muxsel(usbvision, usbvision->ctl_input);
673 673
@@ -1083,9 +1083,9 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv,
1083 usbvision->curFrame = NULL; 1083 usbvision->curFrame = NULL;
1084 1084
1085 /* by now we are committed to the new data... */ 1085 /* by now we are committed to the new data... */
1086 down(&usbvision->lock); 1086 mutex_lock(&usbvision->lock);
1087 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height); 1087 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1088 up(&usbvision->lock); 1088 mutex_unlock(&usbvision->lock);
1089 1089
1090 return 0; 1090 return 0;
1091} 1091}
@@ -1211,16 +1211,16 @@ static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1211 1211
1212 PDEBUG(DBG_MMAP, "mmap"); 1212 PDEBUG(DBG_MMAP, "mmap");
1213 1213
1214 down(&usbvision->lock); 1214 mutex_lock(&usbvision->lock);
1215 1215
1216 if (!USBVISION_IS_OPERATIONAL(usbvision)) { 1216 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1217 up(&usbvision->lock); 1217 mutex_unlock(&usbvision->lock);
1218 return -EFAULT; 1218 return -EFAULT;
1219 } 1219 }
1220 1220
1221 if (!(vma->vm_flags & VM_WRITE) || 1221 if (!(vma->vm_flags & VM_WRITE) ||
1222 size != PAGE_ALIGN(usbvision->max_frame_size)) { 1222 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1223 up(&usbvision->lock); 1223 mutex_unlock(&usbvision->lock);
1224 return -EINVAL; 1224 return -EINVAL;
1225 } 1225 }
1226 1226
@@ -1232,7 +1232,7 @@ static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1232 if (i == usbvision->num_frames) { 1232 if (i == usbvision->num_frames) {
1233 PDEBUG(DBG_MMAP, 1233 PDEBUG(DBG_MMAP,
1234 "mmap: user supplied mapping address is out of range"); 1234 "mmap: user supplied mapping address is out of range");
1235 up(&usbvision->lock); 1235 mutex_unlock(&usbvision->lock);
1236 return -EINVAL; 1236 return -EINVAL;
1237 } 1237 }
1238 1238
@@ -1245,7 +1245,7 @@ static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1245 1245
1246 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { 1246 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1247 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed"); 1247 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1248 up(&usbvision->lock); 1248 mutex_unlock(&usbvision->lock);
1249 return -EAGAIN; 1249 return -EAGAIN;
1250 } 1250 }
1251 start += PAGE_SIZE; 1251 start += PAGE_SIZE;
@@ -1253,7 +1253,7 @@ static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1253 size -= PAGE_SIZE; 1253 size -= PAGE_SIZE;
1254 } 1254 }
1255 1255
1256 up(&usbvision->lock); 1256 mutex_unlock(&usbvision->lock);
1257 return 0; 1257 return 0;
1258} 1258}
1259 1259
@@ -1271,7 +1271,7 @@ static int usbvision_radio_open(struct inode *inode, struct file *file)
1271 1271
1272 PDEBUG(DBG_IO, "%s:", __FUNCTION__); 1272 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1273 1273
1274 down(&usbvision->lock); 1274 mutex_lock(&usbvision->lock);
1275 1275
1276 if (usbvision->user) { 1276 if (usbvision->user) {
1277 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__); 1277 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
@@ -1290,7 +1290,8 @@ static int usbvision_radio_open(struct inode *inode, struct file *file)
1290 errCode = usbvision_set_alternate(usbvision); 1290 errCode = usbvision_set_alternate(usbvision);
1291 if (errCode < 0) { 1291 if (errCode < 0) {
1292 usbvision->last_error = errCode; 1292 usbvision->last_error = errCode;
1293 return -EBUSY; 1293 errCode = -EBUSY;
1294 goto out;
1294 } 1295 }
1295 1296
1296 // If so far no errors then we shall start the radio 1297 // If so far no errors then we shall start the radio
@@ -1307,7 +1308,8 @@ static int usbvision_radio_open(struct inode *inode, struct file *file)
1307 usbvision->initialized = 0; 1308 usbvision->initialized = 0;
1308 } 1309 }
1309 } 1310 }
1310 up(&usbvision->lock); 1311out:
1312 mutex_unlock(&usbvision->lock);
1311 return errCode; 1313 return errCode;
1312} 1314}
1313 1315
@@ -1321,7 +1323,7 @@ static int usbvision_radio_close(struct inode *inode, struct file *file)
1321 1323
1322 PDEBUG(DBG_IO, ""); 1324 PDEBUG(DBG_IO, "");
1323 1325
1324 down(&usbvision->lock); 1326 mutex_lock(&usbvision->lock);
1325 1327
1326 /* Set packet size to 0 */ 1328 /* Set packet size to 0 */
1327 usbvision->ifaceAlt=0; 1329 usbvision->ifaceAlt=0;
@@ -1337,7 +1339,7 @@ static int usbvision_radio_close(struct inode *inode, struct file *file)
1337 usbvision->initialized = 0; 1339 usbvision->initialized = 0;
1338 } 1340 }
1339 1341
1340 up(&usbvision->lock); 1342 mutex_unlock(&usbvision->lock);
1341 1343
1342 if (usbvision->remove_pending) { 1344 if (usbvision->remove_pending) {
1343 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__); 1345 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
@@ -1641,7 +1643,7 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1641 1643
1642 usbvision->dev = dev; 1644 usbvision->dev = dev;
1643 1645
1644 init_MUTEX(&usbvision->lock); /* to 1 == available */ 1646 mutex_init(&usbvision->lock); /* available */
1645 1647
1646 // prepare control urb for control messages during interrupts 1648 // prepare control urb for control messages during interrupts
1647 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL); 1649 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
@@ -1649,7 +1651,6 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1649 goto err_exit; 1651 goto err_exit;
1650 } 1652 }
1651 init_waitqueue_head(&usbvision->ctrlUrb_wq); 1653 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1652 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1653 1654
1654 usbvision_init_powerOffTimer(usbvision); 1655 usbvision_init_powerOffTimer(usbvision);
1655 1656
@@ -1676,13 +1677,13 @@ static void usbvision_release(struct usb_usbvision *usbvision)
1676{ 1677{
1677 PDEBUG(DBG_PROBE, ""); 1678 PDEBUG(DBG_PROBE, "");
1678 1679
1679 down(&usbvision->lock); 1680 mutex_lock(&usbvision->lock);
1680 1681
1681 usbvision_reset_powerOffTimer(usbvision); 1682 usbvision_reset_powerOffTimer(usbvision);
1682 1683
1683 usbvision->initialized = 0; 1684 usbvision->initialized = 0;
1684 1685
1685 up(&usbvision->lock); 1686 mutex_unlock(&usbvision->lock);
1686 1687
1687 usbvision_remove_sysfs(usbvision->vdev); 1688 usbvision_remove_sysfs(usbvision->vdev);
1688 usbvision_unregister_video(usbvision); 1689 usbvision_unregister_video(usbvision);
@@ -1796,7 +1797,7 @@ static int __devinit usbvision_probe(struct usb_interface *intf,
1796 } 1797 }
1797 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType); 1798 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1798 1799
1799 down(&usbvision->lock); 1800 mutex_lock(&usbvision->lock);
1800 1801
1801 /* compute alternate max packet sizes */ 1802 /* compute alternate max packet sizes */
1802 uif = dev->actconfig->interface[0]; 1803 uif = dev->actconfig->interface[0];
@@ -1807,6 +1808,7 @@ static int __devinit usbvision_probe(struct usb_interface *intf,
1807 usbvision->num_alt,GFP_KERNEL); 1808 usbvision->num_alt,GFP_KERNEL);
1808 if (usbvision->alt_max_pkt_size == NULL) { 1809 if (usbvision->alt_max_pkt_size == NULL) {
1809 err("usbvision: out of memory!\n"); 1810 err("usbvision: out of memory!\n");
1811 mutex_unlock(&usbvision->lock);
1810 return -ENOMEM; 1812 return -ENOMEM;
1811 } 1813 }
1812 1814
@@ -1840,7 +1842,7 @@ static int __devinit usbvision_probe(struct usb_interface *intf,
1840 usbvision->streaming = Stream_Off; 1842 usbvision->streaming = Stream_Off;
1841 usbvision_register_video(usbvision); 1843 usbvision_register_video(usbvision);
1842 usbvision_configure_video(usbvision); 1844 usbvision_configure_video(usbvision);
1843 up(&usbvision->lock); 1845 mutex_unlock(&usbvision->lock);
1844 1846
1845 1847
1846 usb_set_intfdata (intf, usbvision); 1848 usb_set_intfdata (intf, usbvision);
@@ -1871,7 +1873,7 @@ static void __devexit usbvision_disconnect(struct usb_interface *intf)
1871 } 1873 }
1872 usb_set_intfdata (intf, NULL); 1874 usb_set_intfdata (intf, NULL);
1873 1875
1874 down(&usbvision->lock); 1876 mutex_lock(&usbvision->lock);
1875 1877
1876 // At this time we ask to cancel outstanding URBs 1878 // At this time we ask to cancel outstanding URBs
1877 usbvision_stop_isoc(usbvision); 1879 usbvision_stop_isoc(usbvision);
@@ -1885,7 +1887,7 @@ static void __devexit usbvision_disconnect(struct usb_interface *intf)
1885 usb_put_dev(usbvision->dev); 1887 usb_put_dev(usbvision->dev);
1886 usbvision->dev = NULL; // USB device is no more 1888 usbvision->dev = NULL; // USB device is no more
1887 1889
1888 up(&usbvision->lock); 1890 mutex_unlock(&usbvision->lock);
1889 1891
1890 if (usbvision->user) { 1892 if (usbvision->user) {
1891 printk(KERN_INFO "%s: In use, disconnect pending\n", 1893 printk(KERN_INFO "%s: In use, disconnect pending\n",
diff --git a/drivers/media/video/usbvision/usbvision.h b/drivers/media/video/usbvision/usbvision.h
index c5b6c501c869..20d7ec624999 100644
--- a/drivers/media/video/usbvision/usbvision.h
+++ b/drivers/media/video/usbvision/usbvision.h
@@ -34,16 +34,13 @@
34#include <linux/list.h> 34#include <linux/list.h>
35#include <linux/usb.h> 35#include <linux/usb.h>
36#include <linux/i2c.h> 36#include <linux/i2c.h>
37#include <linux/mutex.h>
37#include <media/v4l2-common.h> 38#include <media/v4l2-common.h>
38#include <media/tuner.h> 39#include <media/tuner.h>
39#include <linux/videodev2.h> 40#include <linux/videodev2.h>
40 41
41#define USBVISION_DEBUG /* Turn on debug messages */ 42#define USBVISION_DEBUG /* Turn on debug messages */
42 43
43#ifndef VID_HARDWARE_USBVISION
44 #define VID_HARDWARE_USBVISION 34 /* USBVision Video Grabber */
45#endif
46
47#define USBVISION_PWR_REG 0x00 44#define USBVISION_PWR_REG 0x00
48 #define USBVISION_SSPND_EN (1 << 1) 45 #define USBVISION_SSPND_EN (1 << 1)
49 #define USBVISION_RES2 (1 << 2) 46 #define USBVISION_RES2 (1 << 2)
@@ -373,7 +370,6 @@ struct usb_usbvision {
373 int ctrlUrbBusy; 370 int ctrlUrbBusy;
374 struct usb_ctrlrequest ctrlUrbSetup; 371 struct usb_ctrlrequest ctrlUrbSetup;
375 wait_queue_head_t ctrlUrb_wq; // Processes waiting 372 wait_queue_head_t ctrlUrb_wq; // Processes waiting
376 struct semaphore ctrlUrbLock;
377 373
378 /* configuration part */ 374 /* configuration part */
379 int have_tuner; 375 int have_tuner;
@@ -396,7 +392,7 @@ struct usb_usbvision {
396 unsigned char iface; /* Video interface number */ 392 unsigned char iface; /* Video interface number */
397 unsigned char ifaceAlt; /* Alt settings */ 393 unsigned char ifaceAlt; /* Alt settings */
398 unsigned char Vin_Reg2_Preset; 394 unsigned char Vin_Reg2_Preset;
399 struct semaphore lock; 395 struct mutex lock;
400 struct timer_list powerOffTimer; 396 struct timer_list powerOffTimer;
401 struct work_struct powerOffWork; 397 struct work_struct powerOffWork;
402 int power; /* is the device powered on? */ 398 int power; /* is the device powered on? */
diff --git a/drivers/media/video/v4l2-common.c b/drivers/media/video/v4l2-common.c
index 1141b4bf41ce..c056ff6d810c 100644
--- a/drivers/media/video/v4l2-common.c
+++ b/drivers/media/video/v4l2-common.c
@@ -400,7 +400,7 @@ static const char *v4l2_int_ioctls[] = {
400 400
401 [_IOC_NR(TUNER_SET_TYPE_ADDR)] = "TUNER_SET_TYPE_ADDR", 401 [_IOC_NR(TUNER_SET_TYPE_ADDR)] = "TUNER_SET_TYPE_ADDR",
402 [_IOC_NR(TUNER_SET_STANDBY)] = "TUNER_SET_STANDBY", 402 [_IOC_NR(TUNER_SET_STANDBY)] = "TUNER_SET_STANDBY",
403 [_IOC_NR(TDA9887_SET_CONFIG)] = "TDA9887_SET_CONFIG", 403 [_IOC_NR(TUNER_SET_CONFIG)] = "TUNER_SET_CONFIG",
404 404
405 [_IOC_NR(VIDIOC_INT_S_TUNER_MODE)] = "VIDIOC_INT_S_TUNER_MODE", 405 [_IOC_NR(VIDIOC_INT_S_TUNER_MODE)] = "VIDIOC_INT_S_TUNER_MODE",
406 [_IOC_NR(VIDIOC_INT_RESET)] = "VIDIOC_INT_RESET", 406 [_IOC_NR(VIDIOC_INT_RESET)] = "VIDIOC_INT_RESET",
@@ -1013,6 +1013,34 @@ int v4l2_chip_match_host(u32 match_type, u32 match_chip)
1013 1013
1014/* ----------------------------------------------------------------- */ 1014/* ----------------------------------------------------------------- */
1015 1015
1016/* Helper function for I2C legacy drivers */
1017
1018int v4l2_i2c_attach(struct i2c_adapter *adapter, int address, struct i2c_driver *driver,
1019 const char *name, int (*probe)(struct i2c_client *))
1020{
1021 struct i2c_client *client;
1022 int err;
1023
1024 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1025 if (client == 0)
1026 return -ENOMEM;
1027
1028 client->addr = address;
1029 client->adapter = adapter;
1030 client->driver = driver;
1031 strlcpy(client->name, name, sizeof(client->name));
1032
1033 err = probe(client);
1034 if (err == 0) {
1035 i2c_attach_client(client);
1036 } else {
1037 kfree(client);
1038 }
1039 return err != -ENOMEM ? 0 : err;
1040}
1041
1042/* ----------------------------------------------------------------- */
1043
1016EXPORT_SYMBOL(v4l2_norm_to_name); 1044EXPORT_SYMBOL(v4l2_norm_to_name);
1017EXPORT_SYMBOL(v4l2_video_std_construct); 1045EXPORT_SYMBOL(v4l2_video_std_construct);
1018 1046
@@ -1038,6 +1066,8 @@ EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
1038EXPORT_SYMBOL(v4l2_chip_ident_i2c_client); 1066EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
1039EXPORT_SYMBOL(v4l2_chip_match_host); 1067EXPORT_SYMBOL(v4l2_chip_match_host);
1040 1068
1069EXPORT_SYMBOL(v4l2_i2c_attach);
1070
1041/* 1071/*
1042 * Local variables: 1072 * Local variables:
1043 * c-basic-offset: 8 1073 * c-basic-offset: 8
diff --git a/drivers/media/video/v4l2-int-device.c b/drivers/media/video/v4l2-int-device.c
index 8b4ef530a3a8..a545dcaf857f 100644
--- a/drivers/media/video/v4l2-int-device.c
+++ b/drivers/media/video/v4l2-int-device.c
@@ -57,12 +57,12 @@ static void v4l2_int_device_try_attach_all(void)
57 if (!try_module_get(m->module)) 57 if (!try_module_get(m->module))
58 continue; 58 continue;
59 59
60 if (m->u.master->attach(m, s)) { 60 s->u.slave->master = m;
61 if (m->u.master->attach(s)) {
62 s->u.slave->master = NULL;
61 module_put(m->module); 63 module_put(m->module);
62 continue; 64 continue;
63 } 65 }
64
65 s->u.slave->master = m;
66 } 66 }
67 } 67 }
68} 68}
diff --git a/drivers/media/video/videobuf-core.c b/drivers/media/video/videobuf-core.c
index c8a5cb57963b..80a14da9acef 100644
--- a/drivers/media/video/videobuf-core.c
+++ b/drivers/media/video/videobuf-core.c
@@ -22,29 +22,32 @@
22#include <media/videobuf-core.h> 22#include <media/videobuf-core.h>
23 23
24#define MAGIC_BUFFER 0x20070728 24#define MAGIC_BUFFER 0x20070728
25#define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \ 25#define MAGIC_CHECK(is, should) do { \
26 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); } 26 if (unlikely((is) != (should))) { \
27 printk(KERN_ERR "magic mismatch: %x (expected %x)\n", is, should); \
28 BUG(); } } while (0)
27 29
28static int debug = 0; 30static int debug;
29module_param(debug, int, 0644); 31module_param(debug, int, 0644);
30 32
31MODULE_DESCRIPTION("helper module to manage video4linux buffers"); 33MODULE_DESCRIPTION("helper module to manage video4linux buffers");
32MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); 34MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
33MODULE_LICENSE("GPL"); 35MODULE_LICENSE("GPL");
34 36
35#define dprintk(level, fmt, arg...) if (debug >= level) \ 37#define dprintk(level, fmt, arg...) do { \
36 printk(KERN_DEBUG "vbuf: " fmt , ## arg) 38 if (debug >= level) \
39 printk(KERN_DEBUG "vbuf: " fmt , ## arg); } while (0)
37 40
38/* --------------------------------------------------------------------- */ 41/* --------------------------------------------------------------------- */
39 42
40#define CALL(q, f, arg...) \ 43#define CALL(q, f, arg...) \
41 ( (q->int_ops->f)? q->int_ops->f(arg) : 0) 44 ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
42 45
43void* videobuf_alloc(struct videobuf_queue* q) 46void *videobuf_alloc(struct videobuf_queue *q)
44{ 47{
45 struct videobuf_buffer *vb; 48 struct videobuf_buffer *vb;
46 49
47 BUG_ON (q->msize<sizeof(*vb)); 50 BUG_ON(q->msize < sizeof(*vb));
48 51
49 if (!q->int_ops || !q->int_ops->alloc) { 52 if (!q->int_ops || !q->int_ops->alloc) {
50 printk(KERN_ERR "No specific ops defined!\n"); 53 printk(KERN_ERR "No specific ops defined!\n");
@@ -66,20 +69,21 @@ int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
66 int retval = 0; 69 int retval = 0;
67 DECLARE_WAITQUEUE(wait, current); 70 DECLARE_WAITQUEUE(wait, current);
68 71
69 MAGIC_CHECK(vb->magic,MAGIC_BUFFER); 72 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
70 add_wait_queue(&vb->done, &wait); 73 add_wait_queue(&vb->done, &wait);
71 while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) { 74 while (vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) {
72 if (non_blocking) { 75 if (non_blocking) {
73 retval = -EAGAIN; 76 retval = -EAGAIN;
74 break; 77 break;
75 } 78 }
76 set_current_state(intr ? TASK_INTERRUPTIBLE 79 set_current_state(intr ? TASK_INTERRUPTIBLE
77 : TASK_UNINTERRUPTIBLE); 80 : TASK_UNINTERRUPTIBLE);
78 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) 81 if (vb->state == VIDEOBUF_ACTIVE ||
82 vb->state == VIDEOBUF_QUEUED)
79 schedule(); 83 schedule();
80 set_current_state(TASK_RUNNING); 84 set_current_state(TASK_RUNNING);
81 if (intr && signal_pending(current)) { 85 if (intr && signal_pending(current)) {
82 dprintk(1,"buffer waiton: -EINTR\n"); 86 dprintk(1, "buffer waiton: -EINTR\n");
83 retval = -EINTR; 87 retval = -EINTR;
84 break; 88 break;
85 } 89 }
@@ -88,27 +92,33 @@ int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
88 return retval; 92 return retval;
89} 93}
90 94
91int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb, 95int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
92 struct v4l2_framebuffer *fbuf) 96 struct v4l2_framebuffer *fbuf)
93{ 97{
94 MAGIC_CHECK(vb->magic,MAGIC_BUFFER); 98 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
95 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 99 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
96 100
97 /* FIXME: This is required to avoid OOPS on some cases, since mmap_mapper() 101 /* This is required to avoid OOPS on some cases,
98 method should be called before _iolock. 102 since mmap_mapper() method should be called before _iolock.
99 On some cases, the mmap_mapper() is called only after scheduling. 103 On some cases, the mmap_mapper() is called only after scheduling.
100
101 However, this way is just too dirty! Better to wait for some event.
102 */ 104 */
103 schedule_timeout(HZ); 105 if (vb->memory == V4L2_MEMORY_MMAP) {
106 wait_event_timeout(vb->done, q->is_mmapped,
107 msecs_to_jiffies(100));
108 if (!q->is_mmapped) {
109 printk(KERN_ERR
110 "Error: mmap_mapper() never called!\n");
111 return -EINVAL;
112 }
113 }
104 114
105 return CALL(q,iolock,q,vb,fbuf); 115 return CALL(q, iolock, q, vb, fbuf);
106} 116}
107 117
108/* --------------------------------------------------------------------- */ 118/* --------------------------------------------------------------------- */
109 119
110 120
111void videobuf_queue_core_init(struct videobuf_queue* q, 121void videobuf_queue_core_init(struct videobuf_queue *q,
112 struct videobuf_queue_ops *ops, 122 struct videobuf_queue_ops *ops,
113 void *dev, 123 void *dev,
114 spinlock_t *irqlock, 124 spinlock_t *irqlock,
@@ -118,7 +128,7 @@ void videobuf_queue_core_init(struct videobuf_queue* q,
118 void *priv, 128 void *priv,
119 struct videobuf_qtype_ops *int_ops) 129 struct videobuf_qtype_ops *int_ops)
120{ 130{
121 memset(q,0,sizeof(*q)); 131 memset(q, 0, sizeof(*q));
122 q->irqlock = irqlock; 132 q->irqlock = irqlock;
123 q->dev = dev; 133 q->dev = dev;
124 q->type = type; 134 q->type = type;
@@ -129,13 +139,13 @@ void videobuf_queue_core_init(struct videobuf_queue* q,
129 q->int_ops = int_ops; 139 q->int_ops = int_ops;
130 140
131 /* All buffer operations are mandatory */ 141 /* All buffer operations are mandatory */
132 BUG_ON (!q->ops->buf_setup); 142 BUG_ON(!q->ops->buf_setup);
133 BUG_ON (!q->ops->buf_prepare); 143 BUG_ON(!q->ops->buf_prepare);
134 BUG_ON (!q->ops->buf_queue); 144 BUG_ON(!q->ops->buf_queue);
135 BUG_ON (!q->ops->buf_release); 145 BUG_ON(!q->ops->buf_release);
136 146
137 /* Having implementations for abstract methods are mandatory */ 147 /* Having implementations for abstract methods are mandatory */
138 BUG_ON (!q->int_ops); 148 BUG_ON(!q->int_ops);
139 149
140 mutex_init(&q->lock); 150 mutex_init(&q->lock);
141 INIT_LIST_HEAD(&q->stream); 151 INIT_LIST_HEAD(&q->stream);
@@ -146,33 +156,33 @@ int videobuf_queue_is_busy(struct videobuf_queue *q)
146{ 156{
147 int i; 157 int i;
148 158
149 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 159 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
150 160
151 if (q->streaming) { 161 if (q->streaming) {
152 dprintk(1,"busy: streaming active\n"); 162 dprintk(1, "busy: streaming active\n");
153 return 1; 163 return 1;
154 } 164 }
155 if (q->reading) { 165 if (q->reading) {
156 dprintk(1,"busy: pending read #1\n"); 166 dprintk(1, "busy: pending read #1\n");
157 return 1; 167 return 1;
158 } 168 }
159 if (q->read_buf) { 169 if (q->read_buf) {
160 dprintk(1,"busy: pending read #2\n"); 170 dprintk(1, "busy: pending read #2\n");
161 return 1; 171 return 1;
162 } 172 }
163 for (i = 0; i < VIDEO_MAX_FRAME; i++) { 173 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
164 if (NULL == q->bufs[i]) 174 if (NULL == q->bufs[i])
165 continue; 175 continue;
166 if (q->bufs[i]->map) { 176 if (q->bufs[i]->map) {
167 dprintk(1,"busy: buffer #%d mapped\n",i); 177 dprintk(1, "busy: buffer #%d mapped\n", i);
168 return 1; 178 return 1;
169 } 179 }
170 if (q->bufs[i]->state == STATE_QUEUED) { 180 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
171 dprintk(1,"busy: buffer #%d queued\n",i); 181 dprintk(1, "busy: buffer #%d queued\n", i);
172 return 1; 182 return 1;
173 } 183 }
174 if (q->bufs[i]->state == STATE_ACTIVE) { 184 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
175 dprintk(1,"busy: buffer #%d avtive\n",i); 185 dprintk(1, "busy: buffer #%d avtive\n", i);
176 return 1; 186 return 1;
177 } 187 }
178 } 188 }
@@ -182,28 +192,28 @@ int videobuf_queue_is_busy(struct videobuf_queue *q)
182/* Locking: Caller holds q->lock */ 192/* Locking: Caller holds q->lock */
183void videobuf_queue_cancel(struct videobuf_queue *q) 193void videobuf_queue_cancel(struct videobuf_queue *q)
184{ 194{
185 unsigned long flags=0; 195 unsigned long flags = 0;
186 int i; 196 int i;
187 197
188 /* remove queued buffers from list */ 198 /* remove queued buffers from list */
189 if (q->irqlock) 199 if (q->irqlock)
190 spin_lock_irqsave(q->irqlock,flags); 200 spin_lock_irqsave(q->irqlock, flags);
191 for (i = 0; i < VIDEO_MAX_FRAME; i++) { 201 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
192 if (NULL == q->bufs[i]) 202 if (NULL == q->bufs[i])
193 continue; 203 continue;
194 if (q->bufs[i]->state == STATE_QUEUED) { 204 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
195 list_del(&q->bufs[i]->queue); 205 list_del(&q->bufs[i]->queue);
196 q->bufs[i]->state = STATE_ERROR; 206 q->bufs[i]->state = VIDEOBUF_ERROR;
197 } 207 }
198 } 208 }
199 if (q->irqlock) 209 if (q->irqlock)
200 spin_unlock_irqrestore(q->irqlock,flags); 210 spin_unlock_irqrestore(q->irqlock, flags);
201 211
202 /* free all buffers + clear queue */ 212 /* free all buffers + clear queue */
203 for (i = 0; i < VIDEO_MAX_FRAME; i++) { 213 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
204 if (NULL == q->bufs[i]) 214 if (NULL == q->bufs[i])
205 continue; 215 continue;
206 q->ops->buf_release(q,q->bufs[i]); 216 q->ops->buf_release(q, q->bufs[i]);
207 } 217 }
208 INIT_LIST_HEAD(&q->stream); 218 INIT_LIST_HEAD(&q->stream);
209} 219}
@@ -233,8 +243,8 @@ enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
233static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b, 243static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
234 struct videobuf_buffer *vb, enum v4l2_buf_type type) 244 struct videobuf_buffer *vb, enum v4l2_buf_type type)
235{ 245{
236 MAGIC_CHECK(vb->magic,MAGIC_BUFFER); 246 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
237 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 247 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
238 248
239 b->index = vb->i; 249 b->index = vb->i;
240 b->type = type; 250 b->type = type;
@@ -259,17 +269,17 @@ static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
259 b->flags |= V4L2_BUF_FLAG_MAPPED; 269 b->flags |= V4L2_BUF_FLAG_MAPPED;
260 270
261 switch (vb->state) { 271 switch (vb->state) {
262 case STATE_PREPARED: 272 case VIDEOBUF_PREPARED:
263 case STATE_QUEUED: 273 case VIDEOBUF_QUEUED:
264 case STATE_ACTIVE: 274 case VIDEOBUF_ACTIVE:
265 b->flags |= V4L2_BUF_FLAG_QUEUED; 275 b->flags |= V4L2_BUF_FLAG_QUEUED;
266 break; 276 break;
267 case STATE_DONE: 277 case VIDEOBUF_DONE:
268 case STATE_ERROR: 278 case VIDEOBUF_ERROR:
269 b->flags |= V4L2_BUF_FLAG_DONE; 279 b->flags |= V4L2_BUF_FLAG_DONE;
270 break; 280 break;
271 case STATE_NEEDS_INIT: 281 case VIDEOBUF_NEEDS_INIT:
272 case STATE_IDLE: 282 case VIDEOBUF_IDLE:
273 /* nothing */ 283 /* nothing */
274 break; 284 break;
275 } 285 }
@@ -294,16 +304,20 @@ static int __videobuf_mmap_free(struct videobuf_queue *q)
294 if (!q) 304 if (!q)
295 return 0; 305 return 0;
296 306
297 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 307 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
308
309
310 rc = CALL(q, mmap_free, q);
311
312 q->is_mmapped = 0;
298 313
299 rc = CALL(q,mmap_free,q); 314 if (rc < 0)
300 if (rc<0)
301 return rc; 315 return rc;
302 316
303 for (i = 0; i < VIDEO_MAX_FRAME; i++) { 317 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
304 if (NULL == q->bufs[i]) 318 if (NULL == q->bufs[i])
305 continue; 319 continue;
306 q->ops->buf_release(q,q->bufs[i]); 320 q->ops->buf_release(q, q->bufs[i]);
307 kfree(q->bufs[i]); 321 kfree(q->bufs[i]);
308 q->bufs[i] = NULL; 322 q->bufs[i] = NULL;
309 } 323 }
@@ -328,7 +342,7 @@ static int __videobuf_mmap_setup(struct videobuf_queue *q,
328 unsigned int i; 342 unsigned int i;
329 int err; 343 int err;
330 344
331 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 345 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
332 346
333 err = __videobuf_mmap_free(q); 347 err = __videobuf_mmap_free(q);
334 if (0 != err) 348 if (0 != err)
@@ -359,7 +373,7 @@ static int __videobuf_mmap_setup(struct videobuf_queue *q,
359 if (!i) 373 if (!i)
360 return -ENOMEM; 374 return -ENOMEM;
361 375
362 dprintk(1,"mmap setup: %d buffers, %d bytes each\n", 376 dprintk(1, "mmap setup: %d buffers, %d bytes each\n",
363 i, bsize); 377 i, bsize);
364 378
365 return i; 379 return i;
@@ -379,35 +393,35 @@ int videobuf_mmap_setup(struct videobuf_queue *q,
379int videobuf_reqbufs(struct videobuf_queue *q, 393int videobuf_reqbufs(struct videobuf_queue *q,
380 struct v4l2_requestbuffers *req) 394 struct v4l2_requestbuffers *req)
381{ 395{
382 unsigned int size,count; 396 unsigned int size, count;
383 int retval; 397 int retval;
384 398
385 if (req->count < 1) { 399 if (req->count < 1) {
386 dprintk(1,"reqbufs: count invalid (%d)\n",req->count); 400 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
387 return -EINVAL; 401 return -EINVAL;
388 } 402 }
389 403
390 if (req->memory != V4L2_MEMORY_MMAP && 404 if (req->memory != V4L2_MEMORY_MMAP &&
391 req->memory != V4L2_MEMORY_USERPTR && 405 req->memory != V4L2_MEMORY_USERPTR &&
392 req->memory != V4L2_MEMORY_OVERLAY) { 406 req->memory != V4L2_MEMORY_OVERLAY) {
393 dprintk(1,"reqbufs: memory type invalid\n"); 407 dprintk(1, "reqbufs: memory type invalid\n");
394 return -EINVAL; 408 return -EINVAL;
395 } 409 }
396 410
397 mutex_lock(&q->lock); 411 mutex_lock(&q->lock);
398 if (req->type != q->type) { 412 if (req->type != q->type) {
399 dprintk(1,"reqbufs: queue type invalid\n"); 413 dprintk(1, "reqbufs: queue type invalid\n");
400 retval = -EINVAL; 414 retval = -EINVAL;
401 goto done; 415 goto done;
402 } 416 }
403 417
404 if (q->streaming) { 418 if (q->streaming) {
405 dprintk(1,"reqbufs: streaming already exists\n"); 419 dprintk(1, "reqbufs: streaming already exists\n");
406 retval = -EBUSY; 420 retval = -EBUSY;
407 goto done; 421 goto done;
408 } 422 }
409 if (!list_empty(&q->stream)) { 423 if (!list_empty(&q->stream)) {
410 dprintk(1,"reqbufs: stream running\n"); 424 dprintk(1, "reqbufs: stream running\n");
411 retval = -EBUSY; 425 retval = -EBUSY;
412 goto done; 426 goto done;
413 } 427 }
@@ -416,14 +430,14 @@ int videobuf_reqbufs(struct videobuf_queue *q,
416 if (count > VIDEO_MAX_FRAME) 430 if (count > VIDEO_MAX_FRAME)
417 count = VIDEO_MAX_FRAME; 431 count = VIDEO_MAX_FRAME;
418 size = 0; 432 size = 0;
419 q->ops->buf_setup(q,&count,&size); 433 q->ops->buf_setup(q, &count, &size);
420 size = PAGE_ALIGN(size); 434 size = PAGE_ALIGN(size);
421 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n", 435 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
422 count, size, (count*size)>>PAGE_SHIFT); 436 count, size, (count*size)>>PAGE_SHIFT);
423 437
424 retval = __videobuf_mmap_setup(q,count,size,req->memory); 438 retval = __videobuf_mmap_setup(q, count, size, req->memory);
425 if (retval < 0) { 439 if (retval < 0) {
426 dprintk(1,"reqbufs: mmap setup returned %d\n",retval); 440 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
427 goto done; 441 goto done;
428 } 442 }
429 443
@@ -440,19 +454,19 @@ int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
440 454
441 mutex_lock(&q->lock); 455 mutex_lock(&q->lock);
442 if (unlikely(b->type != q->type)) { 456 if (unlikely(b->type != q->type)) {
443 dprintk(1,"querybuf: Wrong type.\n"); 457 dprintk(1, "querybuf: Wrong type.\n");
444 goto done; 458 goto done;
445 } 459 }
446 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) { 460 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
447 dprintk(1,"querybuf: index out of range.\n"); 461 dprintk(1, "querybuf: index out of range.\n");
448 goto done; 462 goto done;
449 } 463 }
450 if (unlikely(NULL == q->bufs[b->index])) { 464 if (unlikely(NULL == q->bufs[b->index])) {
451 dprintk(1,"querybuf: buffer is null.\n"); 465 dprintk(1, "querybuf: buffer is null.\n");
452 goto done; 466 goto done;
453 } 467 }
454 468
455 videobuf_status(q,b,q->bufs[b->index],q->type); 469 videobuf_status(q, b, q->bufs[b->index], q->type);
456 470
457 ret = 0; 471 ret = 0;
458done: 472done:
@@ -465,10 +479,10 @@ int videobuf_qbuf(struct videobuf_queue *q,
465{ 479{
466 struct videobuf_buffer *buf; 480 struct videobuf_buffer *buf;
467 enum v4l2_field field; 481 enum v4l2_field field;
468 unsigned long flags=0; 482 unsigned long flags = 0;
469 int retval; 483 int retval;
470 484
471 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 485 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
472 486
473 if (b->memory == V4L2_MEMORY_MMAP) 487 if (b->memory == V4L2_MEMORY_MMAP)
474 down_read(&current->mm->mmap_sem); 488 down_read(&current->mm->mmap_sem);
@@ -476,36 +490,36 @@ int videobuf_qbuf(struct videobuf_queue *q,
476 mutex_lock(&q->lock); 490 mutex_lock(&q->lock);
477 retval = -EBUSY; 491 retval = -EBUSY;
478 if (q->reading) { 492 if (q->reading) {
479 dprintk(1,"qbuf: Reading running...\n"); 493 dprintk(1, "qbuf: Reading running...\n");
480 goto done; 494 goto done;
481 } 495 }
482 retval = -EINVAL; 496 retval = -EINVAL;
483 if (b->type != q->type) { 497 if (b->type != q->type) {
484 dprintk(1,"qbuf: Wrong type.\n"); 498 dprintk(1, "qbuf: Wrong type.\n");
485 goto done; 499 goto done;
486 } 500 }
487 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) { 501 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
488 dprintk(1,"qbuf: index out of range.\n"); 502 dprintk(1, "qbuf: index out of range.\n");
489 goto done; 503 goto done;
490 } 504 }
491 buf = q->bufs[b->index]; 505 buf = q->bufs[b->index];
492 if (NULL == buf) { 506 if (NULL == buf) {
493 dprintk(1,"qbuf: buffer is null.\n"); 507 dprintk(1, "qbuf: buffer is null.\n");
494 goto done; 508 goto done;
495 } 509 }
496 MAGIC_CHECK(buf->magic,MAGIC_BUFFER); 510 MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
497 if (buf->memory != b->memory) { 511 if (buf->memory != b->memory) {
498 dprintk(1,"qbuf: memory type is wrong.\n"); 512 dprintk(1, "qbuf: memory type is wrong.\n");
499 goto done; 513 goto done;
500 } 514 }
501 if (buf->state != STATE_NEEDS_INIT && buf->state != STATE_IDLE) { 515 if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
502 dprintk(1,"qbuf: buffer is already queued or active.\n"); 516 dprintk(1, "qbuf: buffer is already queued or active.\n");
503 goto done; 517 goto done;
504 } 518 }
505 519
506 if (b->flags & V4L2_BUF_FLAG_INPUT) { 520 if (b->flags & V4L2_BUF_FLAG_INPUT) {
507 if (b->input >= q->inputs) { 521 if (b->input >= q->inputs) {
508 dprintk(1,"qbuf: wrong input.\n"); 522 dprintk(1, "qbuf: wrong input.\n");
509 goto done; 523 goto done;
510 } 524 }
511 buf->input = b->input; 525 buf->input = b->input;
@@ -516,44 +530,46 @@ int videobuf_qbuf(struct videobuf_queue *q,
516 switch (b->memory) { 530 switch (b->memory) {
517 case V4L2_MEMORY_MMAP: 531 case V4L2_MEMORY_MMAP:
518 if (0 == buf->baddr) { 532 if (0 == buf->baddr) {
519 dprintk(1,"qbuf: mmap requested but buffer addr is zero!\n"); 533 dprintk(1, "qbuf: mmap requested "
534 "but buffer addr is zero!\n");
520 goto done; 535 goto done;
521 } 536 }
522 break; 537 break;
523 case V4L2_MEMORY_USERPTR: 538 case V4L2_MEMORY_USERPTR:
524 if (b->length < buf->bsize) { 539 if (b->length < buf->bsize) {
525 dprintk(1,"qbuf: buffer length is not enough\n"); 540 dprintk(1, "qbuf: buffer length is not enough\n");
526 goto done; 541 goto done;
527 } 542 }
528 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr) 543 if (VIDEOBUF_NEEDS_INIT != buf->state &&
529 q->ops->buf_release(q,buf); 544 buf->baddr != b->m.userptr)
545 q->ops->buf_release(q, buf);
530 buf->baddr = b->m.userptr; 546 buf->baddr = b->m.userptr;
531 break; 547 break;
532 case V4L2_MEMORY_OVERLAY: 548 case V4L2_MEMORY_OVERLAY:
533 buf->boff = b->m.offset; 549 buf->boff = b->m.offset;
534 break; 550 break;
535 default: 551 default:
536 dprintk(1,"qbuf: wrong memory type\n"); 552 dprintk(1, "qbuf: wrong memory type\n");
537 goto done; 553 goto done;
538 } 554 }
539 555
540 dprintk(1,"qbuf: requesting next field\n"); 556 dprintk(1, "qbuf: requesting next field\n");
541 field = videobuf_next_field(q); 557 field = videobuf_next_field(q);
542 retval = q->ops->buf_prepare(q,buf,field); 558 retval = q->ops->buf_prepare(q, buf, field);
543 if (0 != retval) { 559 if (0 != retval) {
544 dprintk(1,"qbuf: buffer_prepare returned %d\n",retval); 560 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
545 goto done; 561 goto done;
546 } 562 }
547 563
548 list_add_tail(&buf->stream,&q->stream); 564 list_add_tail(&buf->stream, &q->stream);
549 if (q->streaming) { 565 if (q->streaming) {
550 if (q->irqlock) 566 if (q->irqlock)
551 spin_lock_irqsave(q->irqlock,flags); 567 spin_lock_irqsave(q->irqlock, flags);
552 q->ops->buf_queue(q,buf); 568 q->ops->buf_queue(q, buf);
553 if (q->irqlock) 569 if (q->irqlock)
554 spin_unlock_irqrestore(q->irqlock,flags); 570 spin_unlock_irqrestore(q->irqlock, flags);
555 } 571 }
556 dprintk(1,"qbuf: succeded\n"); 572 dprintk(1, "qbuf: succeded\n");
557 retval = 0; 573 retval = 0;
558 574
559 done: 575 done:
@@ -571,49 +587,49 @@ int videobuf_dqbuf(struct videobuf_queue *q,
571 struct videobuf_buffer *buf; 587 struct videobuf_buffer *buf;
572 int retval; 588 int retval;
573 589
574 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 590 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
575 591
576 mutex_lock(&q->lock); 592 mutex_lock(&q->lock);
577 retval = -EBUSY; 593 retval = -EBUSY;
578 if (q->reading) { 594 if (q->reading) {
579 dprintk(1,"dqbuf: Reading running...\n"); 595 dprintk(1, "dqbuf: Reading running...\n");
580 goto done; 596 goto done;
581 } 597 }
582 retval = -EINVAL; 598 retval = -EINVAL;
583 if (b->type != q->type) { 599 if (b->type != q->type) {
584 dprintk(1,"dqbuf: Wrong type.\n"); 600 dprintk(1, "dqbuf: Wrong type.\n");
585 goto done; 601 goto done;
586 } 602 }
587 if (list_empty(&q->stream)) { 603 if (list_empty(&q->stream)) {
588 dprintk(1,"dqbuf: stream running\n"); 604 dprintk(1, "dqbuf: stream running\n");
589 goto done; 605 goto done;
590 } 606 }
591 buf = list_entry(q->stream.next, struct videobuf_buffer, stream); 607 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
592 retval = videobuf_waiton(buf, nonblocking, 1); 608 retval = videobuf_waiton(buf, nonblocking, 1);
593 if (retval < 0) { 609 if (retval < 0) {
594 dprintk(1,"dqbuf: waiton returned %d\n",retval); 610 dprintk(1, "dqbuf: waiton returned %d\n", retval);
595 goto done; 611 goto done;
596 } 612 }
597 switch (buf->state) { 613 switch (buf->state) {
598 case STATE_ERROR: 614 case VIDEOBUF_ERROR:
599 dprintk(1,"dqbuf: state is error\n"); 615 dprintk(1, "dqbuf: state is error\n");
600 retval = -EIO; 616 retval = -EIO;
601 CALL(q,sync,q, buf); 617 CALL(q, sync, q, buf);
602 buf->state = STATE_IDLE; 618 buf->state = VIDEOBUF_IDLE;
603 break; 619 break;
604 case STATE_DONE: 620 case VIDEOBUF_DONE:
605 dprintk(1,"dqbuf: state is done\n"); 621 dprintk(1, "dqbuf: state is done\n");
606 CALL(q,sync,q, buf); 622 CALL(q, sync, q, buf);
607 buf->state = STATE_IDLE; 623 buf->state = VIDEOBUF_IDLE;
608 break; 624 break;
609 default: 625 default:
610 dprintk(1,"dqbuf: state invalid\n"); 626 dprintk(1, "dqbuf: state invalid\n");
611 retval = -EINVAL; 627 retval = -EINVAL;
612 goto done; 628 goto done;
613 } 629 }
614 list_del(&buf->stream); 630 list_del(&buf->stream);
615 memset(b,0,sizeof(*b)); 631 memset(b, 0, sizeof(*b));
616 videobuf_status(q,b,buf,q->type); 632 videobuf_status(q, b, buf, q->type);
617 633
618 done: 634 done:
619 mutex_unlock(&q->lock); 635 mutex_unlock(&q->lock);
@@ -623,7 +639,7 @@ int videobuf_dqbuf(struct videobuf_queue *q,
623int videobuf_streamon(struct videobuf_queue *q) 639int videobuf_streamon(struct videobuf_queue *q)
624{ 640{
625 struct videobuf_buffer *buf; 641 struct videobuf_buffer *buf;
626 unsigned long flags=0; 642 unsigned long flags = 0;
627 int retval; 643 int retval;
628 644
629 mutex_lock(&q->lock); 645 mutex_lock(&q->lock);
@@ -635,12 +651,12 @@ int videobuf_streamon(struct videobuf_queue *q)
635 goto done; 651 goto done;
636 q->streaming = 1; 652 q->streaming = 1;
637 if (q->irqlock) 653 if (q->irqlock)
638 spin_lock_irqsave(q->irqlock,flags); 654 spin_lock_irqsave(q->irqlock, flags);
639 list_for_each_entry(buf, &q->stream, stream) 655 list_for_each_entry(buf, &q->stream, stream)
640 if (buf->state == STATE_PREPARED) 656 if (buf->state == VIDEOBUF_PREPARED)
641 q->ops->buf_queue(q,buf); 657 q->ops->buf_queue(q, buf);
642 if (q->irqlock) 658 if (q->irqlock)
643 spin_unlock_irqrestore(q->irqlock,flags); 659 spin_unlock_irqrestore(q->irqlock, flags);
644 660
645 done: 661 done:
646 mutex_unlock(&q->lock); 662 mutex_unlock(&q->lock);
@@ -676,10 +692,10 @@ static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
676 size_t count, loff_t *ppos) 692 size_t count, loff_t *ppos)
677{ 693{
678 enum v4l2_field field; 694 enum v4l2_field field;
679 unsigned long flags=0; 695 unsigned long flags = 0;
680 int retval; 696 int retval;
681 697
682 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 698 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
683 699
684 /* setup stuff */ 700 /* setup stuff */
685 q->read_buf = videobuf_alloc(q); 701 q->read_buf = videobuf_alloc(q);
@@ -691,20 +707,20 @@ static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
691 q->read_buf->bsize = count; 707 q->read_buf->bsize = count;
692 708
693 field = videobuf_next_field(q); 709 field = videobuf_next_field(q);
694 retval = q->ops->buf_prepare(q,q->read_buf,field); 710 retval = q->ops->buf_prepare(q, q->read_buf, field);
695 if (0 != retval) 711 if (0 != retval)
696 goto done; 712 goto done;
697 713
698 /* start capture & wait */ 714 /* start capture & wait */
699 if (q->irqlock) 715 if (q->irqlock)
700 spin_lock_irqsave(q->irqlock,flags); 716 spin_lock_irqsave(q->irqlock, flags);
701 q->ops->buf_queue(q,q->read_buf); 717 q->ops->buf_queue(q, q->read_buf);
702 if (q->irqlock) 718 if (q->irqlock)
703 spin_unlock_irqrestore(q->irqlock,flags); 719 spin_unlock_irqrestore(q->irqlock, flags);
704 retval = videobuf_waiton(q->read_buf,0,0); 720 retval = videobuf_waiton(q->read_buf, 0, 0);
705 if (0 == retval) { 721 if (0 == retval) {
706 CALL(q,sync,q,q->read_buf); 722 CALL(q, sync, q, q->read_buf);
707 if (STATE_ERROR == q->read_buf->state) 723 if (VIDEOBUF_ERROR == q->read_buf->state)
708 retval = -EIO; 724 retval = -EIO;
709 else 725 else
710 retval = q->read_buf->size; 726 retval = q->read_buf->size;
@@ -712,7 +728,7 @@ static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
712 728
713 done: 729 done:
714 /* cleanup */ 730 /* cleanup */
715 q->ops->buf_release(q,q->read_buf); 731 q->ops->buf_release(q, q->read_buf);
716 kfree(q->read_buf); 732 kfree(q->read_buf);
717 q->read_buf = NULL; 733 q->read_buf = NULL;
718 return retval; 734 return retval;
@@ -723,21 +739,21 @@ ssize_t videobuf_read_one(struct videobuf_queue *q,
723 int nonblocking) 739 int nonblocking)
724{ 740{
725 enum v4l2_field field; 741 enum v4l2_field field;
726 unsigned long flags=0; 742 unsigned long flags = 0;
727 unsigned size, nbufs; 743 unsigned size, nbufs;
728 int retval; 744 int retval;
729 745
730 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 746 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
731 747
732 mutex_lock(&q->lock); 748 mutex_lock(&q->lock);
733 749
734 nbufs = 1; size = 0; 750 nbufs = 1; size = 0;
735 q->ops->buf_setup(q,&nbufs,&size); 751 q->ops->buf_setup(q, &nbufs, &size);
736 752
737 if (NULL == q->read_buf && 753 if (NULL == q->read_buf &&
738 count >= size && 754 count >= size &&
739 !nonblocking) { 755 !nonblocking) {
740 retval = videobuf_read_zerocopy(q,data,count,ppos); 756 retval = videobuf_read_zerocopy(q, data, count, ppos);
741 if (retval >= 0 || retval == -EIO) 757 if (retval >= 0 || retval == -EIO)
742 /* ok, all done */ 758 /* ok, all done */
743 goto done; 759 goto done;
@@ -749,25 +765,25 @@ ssize_t videobuf_read_one(struct videobuf_queue *q,
749 retval = -ENOMEM; 765 retval = -ENOMEM;
750 q->read_buf = videobuf_alloc(q); 766 q->read_buf = videobuf_alloc(q);
751 767
752 dprintk(1,"video alloc=0x%p\n", q->read_buf); 768 dprintk(1, "video alloc=0x%p\n", q->read_buf);
753 if (NULL == q->read_buf) 769 if (NULL == q->read_buf)
754 goto done; 770 goto done;
755 q->read_buf->memory = V4L2_MEMORY_USERPTR; 771 q->read_buf->memory = V4L2_MEMORY_USERPTR;
756 q->read_buf->bsize = count; /* preferred size */ 772 q->read_buf->bsize = count; /* preferred size */
757 field = videobuf_next_field(q); 773 field = videobuf_next_field(q);
758 retval = q->ops->buf_prepare(q,q->read_buf,field); 774 retval = q->ops->buf_prepare(q, q->read_buf, field);
759 775
760 if (0 != retval) { 776 if (0 != retval) {
761 kfree (q->read_buf); 777 kfree(q->read_buf);
762 q->read_buf = NULL; 778 q->read_buf = NULL;
763 goto done; 779 goto done;
764 } 780 }
765 if (q->irqlock) 781 if (q->irqlock)
766 spin_lock_irqsave(q->irqlock,flags); 782 spin_lock_irqsave(q->irqlock, flags);
767 783
768 q->ops->buf_queue(q,q->read_buf); 784 q->ops->buf_queue(q, q->read_buf);
769 if (q->irqlock) 785 if (q->irqlock)
770 spin_unlock_irqrestore(q->irqlock,flags); 786 spin_unlock_irqrestore(q->irqlock, flags);
771 q->read_off = 0; 787 q->read_off = 0;
772 } 788 }
773 789
@@ -776,11 +792,11 @@ ssize_t videobuf_read_one(struct videobuf_queue *q,
776 if (0 != retval) 792 if (0 != retval)
777 goto done; 793 goto done;
778 794
779 CALL(q,sync,q,q->read_buf); 795 CALL(q, sync, q, q->read_buf);
780 796
781 if (STATE_ERROR == q->read_buf->state) { 797 if (VIDEOBUF_ERROR == q->read_buf->state) {
782 /* catch I/O errors */ 798 /* catch I/O errors */
783 q->ops->buf_release(q,q->read_buf); 799 q->ops->buf_release(q, q->read_buf);
784 kfree(q->read_buf); 800 kfree(q->read_buf);
785 q->read_buf = NULL; 801 q->read_buf = NULL;
786 retval = -EIO; 802 retval = -EIO;
@@ -788,14 +804,14 @@ ssize_t videobuf_read_one(struct videobuf_queue *q,
788 } 804 }
789 805
790 /* Copy to userspace */ 806 /* Copy to userspace */
791 retval=CALL(q,video_copy_to_user,q,data,count,nonblocking); 807 retval = CALL(q, video_copy_to_user, q, data, count, nonblocking);
792 if (retval<0) 808 if (retval < 0)
793 goto done; 809 goto done;
794 810
795 q->read_off += retval; 811 q->read_off += retval;
796 if (q->read_off == q->read_buf->size) { 812 if (q->read_off == q->read_buf->size) {
797 /* all data copied, cleanup */ 813 /* all data copied, cleanup */
798 q->ops->buf_release(q,q->read_buf); 814 q->ops->buf_release(q, q->read_buf);
799 kfree(q->read_buf); 815 kfree(q->read_buf);
800 q->read_buf = NULL; 816 q->read_buf = NULL;
801 } 817 }
@@ -806,14 +822,14 @@ ssize_t videobuf_read_one(struct videobuf_queue *q,
806} 822}
807 823
808/* Locking: Caller holds q->lock */ 824/* Locking: Caller holds q->lock */
809int __videobuf_read_start(struct videobuf_queue *q) 825static int __videobuf_read_start(struct videobuf_queue *q)
810{ 826{
811 enum v4l2_field field; 827 enum v4l2_field field;
812 unsigned long flags=0; 828 unsigned long flags = 0;
813 unsigned int count = 0, size = 0; 829 unsigned int count = 0, size = 0;
814 int err, i; 830 int err, i;
815 831
816 q->ops->buf_setup(q,&count,&size); 832 q->ops->buf_setup(q, &count, &size);
817 if (count < 2) 833 if (count < 2)
818 count = 2; 834 count = 2;
819 if (count > VIDEO_MAX_FRAME) 835 if (count > VIDEO_MAX_FRAME)
@@ -828,17 +844,17 @@ int __videobuf_read_start(struct videobuf_queue *q)
828 844
829 for (i = 0; i < count; i++) { 845 for (i = 0; i < count; i++) {
830 field = videobuf_next_field(q); 846 field = videobuf_next_field(q);
831 err = q->ops->buf_prepare(q,q->bufs[i],field); 847 err = q->ops->buf_prepare(q, q->bufs[i], field);
832 if (err) 848 if (err)
833 return err; 849 return err;
834 list_add_tail(&q->bufs[i]->stream, &q->stream); 850 list_add_tail(&q->bufs[i]->stream, &q->stream);
835 } 851 }
836 if (q->irqlock) 852 if (q->irqlock)
837 spin_lock_irqsave(q->irqlock,flags); 853 spin_lock_irqsave(q->irqlock, flags);
838 for (i = 0; i < count; i++) 854 for (i = 0; i < count; i++)
839 q->ops->buf_queue(q,q->bufs[i]); 855 q->ops->buf_queue(q, q->bufs[i]);
840 if (q->irqlock) 856 if (q->irqlock)
841 spin_unlock_irqrestore(q->irqlock,flags); 857 spin_unlock_irqrestore(q->irqlock, flags);
842 q->reading = 1; 858 q->reading = 1;
843 return 0; 859 return 0;
844} 860}
@@ -859,7 +875,7 @@ static void __videobuf_read_stop(struct videobuf_queue *q)
859 } 875 }
860 q->read_buf = NULL; 876 q->read_buf = NULL;
861 q->reading = 0; 877 q->reading = 0;
862 878
863} 879}
864 880
865int videobuf_read_start(struct videobuf_queue *q) 881int videobuf_read_start(struct videobuf_queue *q)
@@ -899,11 +915,11 @@ ssize_t videobuf_read_stream(struct videobuf_queue *q,
899 int vbihack, int nonblocking) 915 int vbihack, int nonblocking)
900{ 916{
901 int rc, retval; 917 int rc, retval;
902 unsigned long flags=0; 918 unsigned long flags = 0;
903 919
904 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 920 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
905 921
906 dprintk(2,"%s\n",__FUNCTION__); 922 dprintk(2, "%s\n", __FUNCTION__);
907 mutex_lock(&q->lock); 923 mutex_lock(&q->lock);
908 retval = -EBUSY; 924 retval = -EBUSY;
909 if (q->streaming) 925 if (q->streaming)
@@ -931,8 +947,8 @@ ssize_t videobuf_read_stream(struct videobuf_queue *q,
931 break; 947 break;
932 } 948 }
933 949
934 if (q->read_buf->state == STATE_DONE) { 950 if (q->read_buf->state == VIDEOBUF_DONE) {
935 rc = CALL (q,copy_stream, q, data + retval, count, 951 rc = CALL(q, copy_stream, q, data + retval, count,
936 retval, vbihack, nonblocking); 952 retval, vbihack, nonblocking);
937 if (rc < 0) { 953 if (rc < 0) {
938 retval = rc; 954 retval = rc;
@@ -953,10 +969,10 @@ ssize_t videobuf_read_stream(struct videobuf_queue *q,
953 list_add_tail(&q->read_buf->stream, 969 list_add_tail(&q->read_buf->stream,
954 &q->stream); 970 &q->stream);
955 if (q->irqlock) 971 if (q->irqlock)
956 spin_lock_irqsave(q->irqlock,flags); 972 spin_lock_irqsave(q->irqlock, flags);
957 q->ops->buf_queue(q,q->read_buf); 973 q->ops->buf_queue(q, q->read_buf);
958 if (q->irqlock) 974 if (q->irqlock)
959 spin_unlock_irqrestore(q->irqlock,flags); 975 spin_unlock_irqrestore(q->irqlock, flags);
960 q->read_buf = NULL; 976 q->read_buf = NULL;
961 } 977 }
962 if (retval < 0) 978 if (retval < 0)
@@ -999,8 +1015,8 @@ unsigned int videobuf_poll_stream(struct file *file,
999 1015
1000 if (0 == rc) { 1016 if (0 == rc) {
1001 poll_wait(file, &buf->done, wait); 1017 poll_wait(file, &buf->done, wait);
1002 if (buf->state == STATE_DONE || 1018 if (buf->state == VIDEOBUF_DONE ||
1003 buf->state == STATE_ERROR) 1019 buf->state == VIDEOBUF_ERROR)
1004 rc = POLLIN|POLLRDNORM; 1020 rc = POLLIN|POLLRDNORM;
1005 } 1021 }
1006 mutex_unlock(&q->lock); 1022 mutex_unlock(&q->lock);
@@ -1012,10 +1028,11 @@ int videobuf_mmap_mapper(struct videobuf_queue *q,
1012{ 1028{
1013 int retval; 1029 int retval;
1014 1030
1015 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 1031 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1016 1032
1017 mutex_lock(&q->lock); 1033 mutex_lock(&q->lock);
1018 retval=CALL(q,mmap_mapper,q,vma); 1034 retval = CALL(q, mmap_mapper, q, vma);
1035 q->is_mmapped = 1;
1019 mutex_unlock(&q->lock); 1036 mutex_unlock(&q->lock);
1020 1037
1021 return retval; 1038 return retval;
@@ -1026,15 +1043,15 @@ int videobuf_cgmbuf(struct videobuf_queue *q,
1026 struct video_mbuf *mbuf, int count) 1043 struct video_mbuf *mbuf, int count)
1027{ 1044{
1028 struct v4l2_requestbuffers req; 1045 struct v4l2_requestbuffers req;
1029 int rc,i; 1046 int rc, i;
1030 1047
1031 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS); 1048 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1032 1049
1033 memset(&req,0,sizeof(req)); 1050 memset(&req, 0, sizeof(req));
1034 req.type = q->type; 1051 req.type = q->type;
1035 req.count = count; 1052 req.count = count;
1036 req.memory = V4L2_MEMORY_MMAP; 1053 req.memory = V4L2_MEMORY_MMAP;
1037 rc = videobuf_reqbufs(q,&req); 1054 rc = videobuf_reqbufs(q, &req);
1038 if (rc < 0) 1055 if (rc < 0)
1039 return rc; 1056 return rc;
1040 1057
@@ -1079,9 +1096,3 @@ EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1079EXPORT_SYMBOL_GPL(videobuf_mmap_setup); 1096EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1080EXPORT_SYMBOL_GPL(videobuf_mmap_free); 1097EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1081EXPORT_SYMBOL_GPL(videobuf_mmap_mapper); 1098EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1082
1083/*
1084 * Local variables:
1085 * c-basic-offset: 8
1086 * End:
1087 */
diff --git a/drivers/media/video/videobuf-dma-sg.c b/drivers/media/video/videobuf-dma-sg.c
index 44ee408e145f..98efd7ab1f50 100644
--- a/drivers/media/video/videobuf-dma-sg.c
+++ b/drivers/media/video/videobuf-dma-sg.c
@@ -385,30 +385,27 @@ videobuf_vm_close(struct vm_area_struct *vma)
385 * now ...). Bounce buffers don't work very well for the data rates 385 * now ...). Bounce buffers don't work very well for the data rates
386 * video capture has. 386 * video capture has.
387 */ 387 */
388static struct page* 388static int
389videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr, 389videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
390 int *type)
391{ 390{
392 struct page *page; 391 struct page *page;
393 392
394 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n", 393 dprintk(3,"fault: fault @ %08lx [vma %08lx-%08lx]\n",
395 vaddr,vma->vm_start,vma->vm_end); 394 (unsigned long)vmf->virtual_address,vma->vm_start,vma->vm_end);
396 if (vaddr > vma->vm_end)
397 return NOPAGE_SIGBUS;
398 page = alloc_page(GFP_USER | __GFP_DMA32); 395 page = alloc_page(GFP_USER | __GFP_DMA32);
399 if (!page) 396 if (!page)
400 return NOPAGE_OOM; 397 return VM_FAULT_OOM;
401 clear_user_page(page_address(page), vaddr, page); 398 clear_user_page(page_address(page), (unsigned long)vmf->virtual_address,
402 if (type) 399 page);
403 *type = VM_FAULT_MINOR; 400 vmf->page = page;
404 return page; 401 return 0;
405} 402}
406 403
407static struct vm_operations_struct videobuf_vm_ops = 404static struct vm_operations_struct videobuf_vm_ops =
408{ 405{
409 .open = videobuf_vm_open, 406 .open = videobuf_vm_open,
410 .close = videobuf_vm_close, 407 .close = videobuf_vm_close,
411 .nopage = videobuf_vm_nopage, 408 .fault = videobuf_vm_fault,
412}; 409};
413 410
414/* --------------------------------------------------------------------- 411/* ---------------------------------------------------------------------
diff --git a/drivers/media/video/videobuf-dvb.c b/drivers/media/video/videobuf-dvb.c
index 880317e04a02..b73aba65d21d 100644
--- a/drivers/media/video/videobuf-dvb.c
+++ b/drivers/media/video/videobuf-dvb.c
@@ -67,7 +67,7 @@ static int videobuf_dvb_thread(void *data)
67 67
68 /* feed buffer data to demux */ 68 /* feed buffer data to demux */
69 dma=videobuf_to_dma(buf); 69 dma=videobuf_to_dma(buf);
70 if (buf->state == STATE_DONE) 70 if (buf->state == VIDEOBUF_DONE)
71 dvb_dmx_swfilter(&dvb->demux, dma->vmalloc, 71 dvb_dmx_swfilter(&dvb->demux, dma->vmalloc,
72 buf->size); 72 buf->size);
73 73
diff --git a/drivers/media/video/videobuf-vmalloc.c b/drivers/media/video/videobuf-vmalloc.c
index e01259438bb2..9b3898347ca5 100644
--- a/drivers/media/video/videobuf-vmalloc.c
+++ b/drivers/media/video/videobuf-vmalloc.c
@@ -41,7 +41,7 @@ MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
41MODULE_LICENSE("GPL"); 41MODULE_LICENSE("GPL");
42 42
43#define dprintk(level, fmt, arg...) if (debug >= level) \ 43#define dprintk(level, fmt, arg...) if (debug >= level) \
44 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg) 44 printk(KERN_DEBUG "vbuf-vmalloc: " fmt , ## arg)
45 45
46 46
47/***************************************************************************/ 47/***************************************************************************/
diff --git a/drivers/media/video/videodev.c b/drivers/media/video/videodev.c
index 9611c3990285..28655f8983c6 100644
--- a/drivers/media/video/videodev.c
+++ b/drivers/media/video/videodev.c
@@ -973,7 +973,7 @@ static int __video_do_ioctl(struct inode *inode, struct file *file,
973 973
974 *id = vfd->current_norm; 974 *id = vfd->current_norm;
975 975
976 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id); 976 dbgarg (cmd, "value=%08Lx\n", (long long unsigned) *id);
977 977
978 ret=0; 978 ret=0;
979 break; 979 break;
@@ -982,7 +982,7 @@ static int __video_do_ioctl(struct inode *inode, struct file *file,
982 { 982 {
983 v4l2_std_id *id = arg,norm; 983 v4l2_std_id *id = arg,norm;
984 984
985 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id); 985 dbgarg (cmd, "value=%08Lx\n", (long long unsigned) *id);
986 986
987 norm = (*id) & vfd->tvnorms; 987 norm = (*id) & vfd->tvnorms;
988 if ( vfd->tvnorms && !norm) /* Check if std is supported */ 988 if ( vfd->tvnorms && !norm) /* Check if std is supported */
@@ -1008,7 +1008,7 @@ static int __video_do_ioctl(struct inode *inode, struct file *file,
1008 break; 1008 break;
1009 ret=vfd->vidioc_querystd(file, fh, arg); 1009 ret=vfd->vidioc_querystd(file, fh, arg);
1010 if (!ret) 1010 if (!ret)
1011 dbgarg (cmd, "detected std=%Lu\n", 1011 dbgarg (cmd, "detected std=%08Lx\n",
1012 (unsigned long long)*p); 1012 (unsigned long long)*p);
1013 break; 1013 break;
1014 } 1014 }
@@ -1028,7 +1028,7 @@ static int __video_do_ioctl(struct inode *inode, struct file *file,
1028 if (!ret) 1028 if (!ret)
1029 dbgarg (cmd, "index=%d, name=%s, type=%d, " 1029 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1030 "audioset=%d, " 1030 "audioset=%d, "
1031 "tuner=%d, std=%Ld, status=%d\n", 1031 "tuner=%d, std=%08Lx, status=%d\n",
1032 p->index,p->name,p->type,p->audioset, 1032 p->index,p->name,p->type,p->audioset,
1033 p->tuner, 1033 p->tuner,
1034 (unsigned long long)p->std, 1034 (unsigned long long)p->std,
diff --git a/drivers/media/video/vivi.c b/drivers/media/video/vivi.c
index 9b54ff9d2e36..1db067c02815 100644
--- a/drivers/media/video/vivi.c
+++ b/drivers/media/video/vivi.c
@@ -44,21 +44,18 @@
44#define WAKE_DENOMINATOR 1001 44#define WAKE_DENOMINATOR 1001
45#define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */ 45#define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
46 46
47/* These timers are for 1 fps - used only for testing */
48//#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
49//#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
50
51#include "font.h" 47#include "font.h"
52 48
53#define VIVI_MAJOR_VERSION 0 49#define VIVI_MAJOR_VERSION 0
54#define VIVI_MINOR_VERSION 4 50#define VIVI_MINOR_VERSION 4
55#define VIVI_RELEASE 0 51#define VIVI_RELEASE 0
56#define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE) 52#define VIVI_VERSION \
53 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
57 54
58/* Declare static vars that will be used as parameters */ 55/* Declare static vars that will be used as parameters */
59static unsigned int vid_limit = 16; /* Video memory limit, in Mb */ 56static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
60static struct video_device vivi; /* Video device */
61static int video_nr = -1; /* /dev/videoN, -1 for autodetect */ 57static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
58static int n_devs = 1; /* Number of virtual devices */
62 59
63/* supported controls */ 60/* supported controls */
64static struct v4l2_queryctrl vivi_qctrl[] = { 61static struct v4l2_queryctrl vivi_qctrl[] = {
@@ -71,7 +68,7 @@ static struct v4l2_queryctrl vivi_qctrl[] = {
71 .default_value = 65535, 68 .default_value = 65535,
72 .flags = 0, 69 .flags = 0,
73 .type = V4L2_CTRL_TYPE_INTEGER, 70 .type = V4L2_CTRL_TYPE_INTEGER,
74 },{ 71 }, {
75 .id = V4L2_CID_BRIGHTNESS, 72 .id = V4L2_CID_BRIGHTNESS,
76 .type = V4L2_CTRL_TYPE_INTEGER, 73 .type = V4L2_CTRL_TYPE_INTEGER,
77 .name = "Brightness", 74 .name = "Brightness",
@@ -112,9 +109,9 @@ static struct v4l2_queryctrl vivi_qctrl[] = {
112 109
113static int qctl_regs[ARRAY_SIZE(vivi_qctrl)]; 110static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
114 111
115#define dprintk(level,fmt, arg...) \ 112#define dprintk(dev, level, fmt, arg...) \
116 do { \ 113 do { \
117 if (vivi.debug >= (level)) \ 114 if (dev->vfd->debug >= (level)) \
118 printk(KERN_DEBUG "vivi: " fmt , ## arg); \ 115 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
119 } while (0) 116 } while (0)
120 117
@@ -166,17 +163,21 @@ struct vivi_dev {
166 struct list_head vivi_devlist; 163 struct list_head vivi_devlist;
167 164
168 struct mutex lock; 165 struct mutex lock;
166 spinlock_t slock;
169 167
170 int users; 168 int users;
171 169
172 /* various device info */ 170 /* various device info */
173 struct video_device vfd; 171 struct video_device *vfd;
174 172
175 struct vivi_dmaqueue vidq; 173 struct vivi_dmaqueue vidq;
176 174
177 /* Several counters */ 175 /* Several counters */
178 int h,m,s,us,jiffies; 176 int h, m, s, ms;
177 unsigned long jiffies;
179 char timestr[13]; 178 char timestr[13];
179
180 int mv_count; /* Controls bars movement */
180}; 181};
181 182
182struct vivi_fh { 183struct vivi_fh {
@@ -184,7 +185,7 @@ struct vivi_fh {
184 185
185 /* video capture */ 186 /* video capture */
186 struct vivi_fmt *fmt; 187 struct vivi_fmt *fmt;
187 unsigned int width,height; 188 unsigned int width, height;
188 struct videobuf_queue vb_vidq; 189 struct videobuf_queue vb_vidq;
189 190
190 enum v4l2_buf_type type; 191 enum v4l2_buf_type type;
@@ -203,109 +204,113 @@ enum colors {
203 GREEN, 204 GREEN,
204 MAGENTA, 205 MAGENTA,
205 RED, 206 RED,
206 BLUE 207 BLUE,
208 BLACK,
207}; 209};
208 210
209static u8 bars[8][3] = { 211static u8 bars[8][3] = {
210 /* R G B */ 212 /* R G B */
211 {204,204,204}, /* white */ 213 {204, 204, 204}, /* white */
212 {208,208, 0}, /* ambar */ 214 {208, 208, 0}, /* ambar */
213 { 0,206,206}, /* cyan */ 215 { 0, 206, 206}, /* cyan */
214 { 0,239, 0}, /* green */ 216 { 0, 239, 0}, /* green */
215 {239, 0,239}, /* magenta */ 217 {239, 0, 239}, /* magenta */
216 {205, 0, 0}, /* red */ 218 {205, 0, 0}, /* red */
217 { 0, 0,255}, /* blue */ 219 { 0, 0, 255}, /* blue */
218 { 0, 0, 0} 220 { 0, 0, 0}, /* black */
219}; 221};
220 222
221#define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16) 223#define TO_Y(r, g, b) \
224 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
222/* RGB to V(Cr) Color transform */ 225/* RGB to V(Cr) Color transform */
223#define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128) 226#define TO_V(r, g, b) \
227 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
224/* RGB to U(Cb) Color transform */ 228/* RGB to U(Cb) Color transform */
225#define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128) 229#define TO_U(r, g, b) \
230 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
226 231
227#define TSTAMP_MIN_Y 24 232#define TSTAMP_MIN_Y 24
228#define TSTAMP_MAX_Y TSTAMP_MIN_Y+15 233#define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
229#define TSTAMP_MIN_X 64 234#define TSTAMP_MIN_X 64
230 235
231static void gen_line(char *basep,int inipos,int wmax, 236static void gen_line(char *basep, int inipos, int wmax,
232 int hmax, int line, int count, char *timestr) 237 int hmax, int line, int count, char *timestr)
233{ 238{
234 int w,i,j,pos=inipos,y; 239 int w, i, j, y;
235 char *p,*s; 240 int pos = inipos;
236 u8 chr,r,g,b,color; 241 char *p, *s;
242 u8 chr, r, g, b, color;
237 243
238 /* We will just duplicate the second pixel at the packet */ 244 /* We will just duplicate the second pixel at the packet */
239 wmax/=2; 245 wmax /= 2;
240 246
241 /* Generate a standard color bar pattern */ 247 /* Generate a standard color bar pattern */
242 for (w=0;w<wmax;w++) { 248 for (w = 0; w < wmax; w++) {
243 int colorpos=((w+count)*8/(wmax+1)) % 8; 249 int colorpos = ((w + count) * 8/(wmax + 1)) % 8;
244 r=bars[colorpos][0]; 250 r = bars[colorpos][0];
245 g=bars[colorpos][1]; 251 g = bars[colorpos][1];
246 b=bars[colorpos][2]; 252 b = bars[colorpos][2];
247 253
248 for (color=0;color<4;color++) { 254 for (color = 0; color < 4; color++) {
249 p=basep+pos; 255 p = basep + pos;
250 256
251 switch (color) { 257 switch (color) {
252 case 0: 258 case 0:
253 case 2: 259 case 2:
254 *p=TO_Y(r,g,b); /* Luminance */ 260 *p = TO_Y(r, g, b); /* Luma */
255 break; 261 break;
256 case 1: 262 case 1:
257 *p=TO_U(r,g,b); /* Cb */ 263 *p = TO_U(r, g, b); /* Cb */
258 break; 264 break;
259 case 3: 265 case 3:
260 *p=TO_V(r,g,b); /* Cr */ 266 *p = TO_V(r, g, b); /* Cr */
261 break; 267 break;
262 } 268 }
263 pos++; 269 pos++;
264 } 270 }
265 } 271 }
266 272
267 /* Checks if it is possible to show timestamp */ 273 /* Checks if it is possible to show timestamp */
268 if (TSTAMP_MAX_Y>=hmax) 274 if (TSTAMP_MAX_Y >= hmax)
269 goto end; 275 goto end;
270 if (TSTAMP_MIN_X+strlen(timestr)>=wmax) 276 if (TSTAMP_MIN_X + strlen(timestr) >= wmax)
271 goto end; 277 goto end;
272 278
273 /* Print stream time */ 279 /* Print stream time */
274 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) { 280 if (line >= TSTAMP_MIN_Y && line <= TSTAMP_MAX_Y) {
275 j=TSTAMP_MIN_X; 281 j = TSTAMP_MIN_X;
276 for (s=timestr;*s;s++) { 282 for (s = timestr; *s; s++) {
277 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y]; 283 chr = rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
278 for (i=0;i<7;i++) { 284 for (i = 0; i < 7; i++) {
279 if (chr&1<<(7-i)) { /* Font color*/ 285 if (chr & 1 << (7 - i)) {
280 r=bars[BLUE][0]; 286 /* Font color*/
281 g=bars[BLUE][1]; 287 r = 0;
282 b=bars[BLUE][2]; 288 g = 198;
283 r=g=b=0; 289 b = 0;
284 g=198; 290 } else {
285 } else { /* Background color */ 291 /* Background color */
286 r=bars[WHITE][0]; 292 r = bars[BLACK][0];
287 g=bars[WHITE][1]; 293 g = bars[BLACK][1];
288 b=bars[WHITE][2]; 294 b = bars[BLACK][2];
289 r=g=b=0;
290 } 295 }
291 296
292 pos=inipos+j*2; 297 pos = inipos + j * 2;
293 for (color=0;color<4;color++) { 298 for (color = 0; color < 4; color++) {
294 p=basep+pos; 299 p = basep + pos;
295 300
296 y=TO_Y(r,g,b); 301 y = TO_Y(r, g, b);
297 302
298 switch (color) { 303 switch (color) {
299 case 0: 304 case 0:
300 case 2: 305 case 2:
301 *p=TO_Y(r,g,b); /* Luminance */ 306 *p = TO_Y(r, g, b); /* Luma */
302 break; 307 break;
303 case 1: 308 case 1:
304 *p=TO_U(r,g,b); /* Cb */ 309 *p = TO_U(r, g, b); /* Cb */
305 break; 310 break;
306 case 3: 311 case 3:
307 *p=TO_V(r,g,b); /* Cr */ 312 *p = TO_V(r, g, b); /* Cr */
308 break; 313 break;
309 } 314 }
310 pos++; 315 pos++;
311 } 316 }
@@ -314,63 +319,60 @@ static void gen_line(char *basep,int inipos,int wmax,
314 } 319 }
315 } 320 }
316 321
317
318end: 322end:
319 return; 323 return;
320} 324}
321static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf) 325static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
322{ 326{
323 int h,pos=0; 327 int h , pos = 0;
324 int hmax = buf->vb.height; 328 int hmax = buf->vb.height;
325 int wmax = buf->vb.width; 329 int wmax = buf->vb.width;
326 struct timeval ts; 330 struct timeval ts;
327 char *tmpbuf = kmalloc(wmax*2,GFP_KERNEL); 331 char *tmpbuf = kmalloc(wmax * 2, GFP_KERNEL);
328 void *vbuf=videobuf_to_vmalloc (&buf->vb); 332 void *vbuf = videobuf_to_vmalloc(&buf->vb);
329 /* FIXME: move to dev struct */
330 static int mv_count=0;
331 333
332 if (!tmpbuf) 334 if (!tmpbuf)
333 return; 335 return;
334 336
335 for (h=0;h<hmax;h++) { 337 for (h = 0; h < hmax; h++) {
336 gen_line(tmpbuf,0,wmax,hmax,h,mv_count, 338 gen_line(tmpbuf, 0, wmax, hmax, h, dev->mv_count,
337 dev->timestr); 339 dev->timestr);
338 /* FIXME: replacing to __copy_to_user */ 340 /* FIXME: replacing to __copy_to_user */
339 if (copy_to_user(vbuf+pos,tmpbuf,wmax*2)!=0) 341 if (copy_to_user(vbuf + pos, tmpbuf, wmax * 2) != 0)
340 dprintk(2,"vivifill copy_to_user failed.\n"); 342 dprintk(dev, 2, "vivifill copy_to_user failed.\n");
341 pos += wmax*2; 343 pos += wmax*2;
342 } 344 }
343 345
344 mv_count++; 346 dev->mv_count++;
345 347
346 kfree(tmpbuf); 348 kfree(tmpbuf);
347 349
348 /* Updates stream time */ 350 /* Updates stream time */
349 351
350 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies); 352 dev->ms += jiffies_to_msecs(jiffies-dev->jiffies);
351 dev->jiffies=jiffies; 353 dev->jiffies = jiffies;
352 if (dev->us>=1000000) { 354 if (dev->ms >= 1000) {
353 dev->us-=1000000; 355 dev->ms -= 1000;
354 dev->s++; 356 dev->s++;
355 if (dev->s>=60) { 357 if (dev->s >= 60) {
356 dev->s-=60; 358 dev->s -= 60;
357 dev->m++; 359 dev->m++;
358 if (dev->m>60) { 360 if (dev->m > 60) {
359 dev->m-=60; 361 dev->m -= 60;
360 dev->h++; 362 dev->h++;
361 if (dev->h>24) 363 if (dev->h > 24)
362 dev->h-=24; 364 dev->h -= 24;
363 } 365 }
364 } 366 }
365 } 367 }
366 sprintf(dev->timestr,"%02d:%02d:%02d:%03d", 368 sprintf(dev->timestr, "%02d:%02d:%02d:%03d",
367 dev->h,dev->m,dev->s,(dev->us+500)/1000); 369 dev->h, dev->m, dev->s, dev->ms);
368 370
369 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr, 371 dprintk(dev, 2, "vivifill at %s: Buffer 0x%08lx size= %d\n",
370 (unsigned long)tmpbuf,pos); 372 dev->timestr, (unsigned long)tmpbuf, pos);
371 373
372 /* Advice that buffer was filled */ 374 /* Advice that buffer was filled */
373 buf->vb.state = STATE_DONE; 375 buf->vb.state = VIDEOBUF_DONE;
374 buf->vb.field_count++; 376 buf->vb.field_count++;
375 do_gettimeofday(&ts); 377 do_gettimeofday(&ts);
376 buf->vb.ts = ts; 378 buf->vb.ts = ts;
@@ -384,14 +386,15 @@ static int restart_video_queue(struct vivi_dmaqueue *dma_q);
384static void vivi_thread_tick(struct vivi_dmaqueue *dma_q) 386static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
385{ 387{
386 struct vivi_buffer *buf; 388 struct vivi_buffer *buf;
387 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq); 389 struct vivi_dev *dev = container_of(dma_q, struct vivi_dev, vidq);
388 390
389 int bc; 391 int bc;
390 392
393 spin_lock(&dev->slock);
391 /* Announces videobuf that all went ok */ 394 /* Announces videobuf that all went ok */
392 for (bc = 0;; bc++) { 395 for (bc = 0;; bc++) {
393 if (list_empty(&dma_q->active)) { 396 if (list_empty(&dma_q->active)) {
394 dprintk(1,"No active queue to serve\n"); 397 dprintk(dev, 1, "No active queue to serve\n");
395 break; 398 break;
396 } 399 }
397 400
@@ -401,65 +404,89 @@ static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
401 /* Nobody is waiting something to be done, just return */ 404 /* Nobody is waiting something to be done, just return */
402 if (!waitqueue_active(&buf->vb.done)) { 405 if (!waitqueue_active(&buf->vb.done)) {
403 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); 406 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
407 spin_unlock(&dev->slock);
404 return; 408 return;
405 } 409 }
406 410
407 do_gettimeofday(&buf->vb.ts); 411 do_gettimeofday(&buf->vb.ts);
408 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i); 412 dprintk(dev, 2, "[%p/%d] wakeup\n", buf, buf->vb. i);
409 413
410 /* Fill buffer */ 414 /* Fill buffer */
411 vivi_fillbuff(dev,buf); 415 vivi_fillbuff(dev, buf);
412 416
413 if (list_empty(&dma_q->active)) { 417 if (list_empty(&dma_q->active)) {
414 del_timer(&dma_q->timeout); 418 del_timer(&dma_q->timeout);
415 } else { 419 } else {
416 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); 420 mod_timer(&dma_q->timeout, jiffies + BUFFER_TIMEOUT);
417 } 421 }
418 } 422 }
419 if (bc != 1) 423 if (bc != 1)
420 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc); 424 dprintk(dev, 1, "%s: %d buffers handled (should be 1)\n",
425 __FUNCTION__, bc);
426 spin_unlock(&dev->slock);
421} 427}
422 428
429#define frames_to_ms(frames) \
430 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
431
423static void vivi_sleep(struct vivi_dmaqueue *dma_q) 432static void vivi_sleep(struct vivi_dmaqueue *dma_q)
424{ 433{
425 int timeout; 434 struct vivi_dev *dev = container_of(dma_q, struct vivi_dev, vidq);
435 int timeout, running_time;
426 DECLARE_WAITQUEUE(wait, current); 436 DECLARE_WAITQUEUE(wait, current);
427 437
428 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q); 438 dprintk(dev, 1, "%s dma_q=0x%08lx\n", __FUNCTION__,
439 (unsigned long)dma_q);
429 440
430 add_wait_queue(&dma_q->wq, &wait); 441 add_wait_queue(&dma_q->wq, &wait);
431 if (!kthread_should_stop()) { 442 if (kthread_should_stop())
432 dma_q->frame++; 443 goto stop_task;
433 444
434 /* Calculate time to wake up */ 445 running_time = jiffies - dma_q->ini_jiffies;
435 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies; 446 dma_q->frame++;
436 447
437 if (timeout <= 0) { 448 /* Calculate time to wake up */
438 int old=dma_q->frame; 449 timeout = msecs_to_jiffies(frames_to_ms(dma_q->frame)) - running_time;
439 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
440 450
441 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies; 451 if (timeout > msecs_to_jiffies(frames_to_ms(2)) || timeout <= 0) {
452 int old = dma_q->frame;
453 int nframes;
442 454
443 dprintk(1,"underrun, losed %d frames. " 455 dma_q->frame = (jiffies_to_msecs(running_time) /
444 "Now, frame is %d. Waking on %d jiffies\n", 456 frames_to_ms(1)) + 1;
445 dma_q->frame-old,dma_q->frame,timeout);
446 } else
447 dprintk(1,"will sleep for %i jiffies\n",timeout);
448 457
449 vivi_thread_tick(dma_q); 458 timeout = msecs_to_jiffies(frames_to_ms(dma_q->frame))
459 - running_time;
450 460
451 schedule_timeout_interruptible (timeout); 461 if (unlikely (timeout <= 0))
452 } 462 timeout = 1;
463
464 nframes = (dma_q->frame > old)?
465 dma_q->frame - old : old - dma_q->frame;
466
467 dprintk(dev, 1, "%ld: %s %d frames. "
468 "Current frame is %d. Will sleep for %d jiffies\n",
469 jiffies,
470 (dma_q->frame > old)? "Underrun, losed" : "Overrun of",
471 nframes, dma_q->frame, timeout);
472 } else
473 dprintk(dev, 1, "will sleep for %d jiffies\n", timeout);
453 474
475 vivi_thread_tick(dma_q);
476
477 schedule_timeout_interruptible(timeout);
478
479stop_task:
454 remove_wait_queue(&dma_q->wq, &wait); 480 remove_wait_queue(&dma_q->wq, &wait);
455 try_to_freeze(); 481 try_to_freeze();
456} 482}
457 483
458static int vivi_thread(void *data) 484static int vivi_thread(void *data)
459{ 485{
460 struct vivi_dmaqueue *dma_q=data; 486 struct vivi_dmaqueue *dma_q = data;
487 struct vivi_dev *dev = container_of(dma_q, struct vivi_dev, vidq);
461 488
462 dprintk(1,"thread started\n"); 489 dprintk(dev, 1, "thread started\n");
463 490
464 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); 491 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
465 set_freezable(); 492 set_freezable();
@@ -470,16 +497,18 @@ static int vivi_thread(void *data)
470 if (kthread_should_stop()) 497 if (kthread_should_stop())
471 break; 498 break;
472 } 499 }
473 dprintk(1, "thread: exit\n"); 500 dprintk(dev, 1, "thread: exit\n");
474 return 0; 501 return 0;
475} 502}
476 503
477static int vivi_start_thread(struct vivi_dmaqueue *dma_q) 504static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
478{ 505{
479 dma_q->frame=0; 506 struct vivi_dev *dev = container_of(dma_q, struct vivi_dev, vidq);
480 dma_q->ini_jiffies=jiffies;
481 507
482 dprintk(1,"%s\n",__FUNCTION__); 508 dma_q->frame = 0;
509 dma_q->ini_jiffies = jiffies;
510
511 dprintk(dev, 1, "%s\n", __FUNCTION__);
483 512
484 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi"); 513 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
485 514
@@ -490,39 +519,43 @@ static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
490 /* Wakes thread */ 519 /* Wakes thread */
491 wake_up_interruptible(&dma_q->wq); 520 wake_up_interruptible(&dma_q->wq);
492 521
493 dprintk(1,"returning from %s\n",__FUNCTION__); 522 dprintk(dev, 1, "returning from %s\n", __FUNCTION__);
494 return 0; 523 return 0;
495} 524}
496 525
497static void vivi_stop_thread(struct vivi_dmaqueue *dma_q) 526static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
498{ 527{
499 dprintk(1,"%s\n",__FUNCTION__); 528 struct vivi_dev *dev = container_of(dma_q, struct vivi_dev, vidq);
529
530 dprintk(dev, 1, "%s\n", __FUNCTION__);
500 /* shutdown control thread */ 531 /* shutdown control thread */
501 if (dma_q->kthread) { 532 if (dma_q->kthread) {
502 kthread_stop(dma_q->kthread); 533 kthread_stop(dma_q->kthread);
503 dma_q->kthread=NULL; 534 dma_q->kthread = NULL;
504 } 535 }
505} 536}
506 537
507static int restart_video_queue(struct vivi_dmaqueue *dma_q) 538static int restart_video_queue(struct vivi_dmaqueue *dma_q)
508{ 539{
540 struct vivi_dev *dev = container_of(dma_q, struct vivi_dev, vidq);
509 struct vivi_buffer *buf, *prev; 541 struct vivi_buffer *buf, *prev;
510 542
511 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q); 543 dprintk(dev, 1, "%s dma_q=0x%08lx\n", __FUNCTION__,
544 (unsigned long)dma_q);
512 545
513 if (!list_empty(&dma_q->active)) { 546 if (!list_empty(&dma_q->active)) {
514 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue); 547 buf = list_entry(dma_q->active.next,
515 dprintk(2,"restart_queue [%p/%d]: restart dma\n", 548 struct vivi_buffer, vb.queue);
549 dprintk(dev, 2, "restart_queue [%p/%d]: restart dma\n",
516 buf, buf->vb.i); 550 buf, buf->vb.i);
517 551
518 dprintk(1,"Restarting video dma\n"); 552 dprintk(dev, 1, "Restarting video dma\n");
519 vivi_stop_thread(dma_q); 553 vivi_stop_thread(dma_q);
520// vivi_start_thread(dma_q);
521 554
522 /* cancel all outstanding capture / vbi requests */ 555 /* cancel all outstanding capture / vbi requests */
523 list_for_each_entry_safe(buf, prev, &dma_q->active, vb.queue) { 556 list_for_each_entry_safe(buf, prev, &dma_q->active, vb.queue) {
524 list_del(&buf->vb.queue); 557 list_del(&buf->vb.queue);
525 buf->vb.state = STATE_ERROR; 558 buf->vb.state = VIDEOBUF_ERROR;
526 wake_up(&buf->vb.done); 559 wake_up(&buf->vb.done);
527 } 560 }
528 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); 561 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
@@ -534,28 +567,31 @@ static int restart_video_queue(struct vivi_dmaqueue *dma_q)
534 for (;;) { 567 for (;;) {
535 if (list_empty(&dma_q->queued)) 568 if (list_empty(&dma_q->queued))
536 return 0; 569 return 0;
537 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue); 570 buf = list_entry(dma_q->queued.next,
571 struct vivi_buffer, vb.queue);
538 if (NULL == prev) { 572 if (NULL == prev) {
539 list_del(&buf->vb.queue); 573 list_del(&buf->vb.queue);
540 list_add_tail(&buf->vb.queue,&dma_q->active); 574 list_add_tail(&buf->vb.queue, &dma_q->active);
541 575
542 dprintk(1,"Restarting video dma\n"); 576 dprintk(dev, 1, "Restarting video dma\n");
543 vivi_stop_thread(dma_q); 577 vivi_stop_thread(dma_q);
544 vivi_start_thread(dma_q); 578 vivi_start_thread(dma_q);
545 579
546 buf->vb.state = STATE_ACTIVE; 580 buf->vb.state = VIDEOBUF_ACTIVE;
547 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); 581 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
548 dprintk(2,"[%p/%d] restart_queue - first active\n", 582 dprintk(dev, 2,
549 buf,buf->vb.i); 583 "[%p/%d] restart_queue - first active\n",
584 buf, buf->vb.i);
550 585
551 } else if (prev->vb.width == buf->vb.width && 586 } else if (prev->vb.width == buf->vb.width &&
552 prev->vb.height == buf->vb.height && 587 prev->vb.height == buf->vb.height &&
553 prev->fmt == buf->fmt) { 588 prev->fmt == buf->fmt) {
554 list_del(&buf->vb.queue); 589 list_del(&buf->vb.queue);
555 list_add_tail(&buf->vb.queue,&dma_q->active); 590 list_add_tail(&buf->vb.queue, &dma_q->active);
556 buf->vb.state = STATE_ACTIVE; 591 buf->vb.state = VIDEOBUF_ACTIVE;
557 dprintk(2,"[%p/%d] restart_queue - move to active\n", 592 dprintk(dev, 2,
558 buf,buf->vb.i); 593 "[%p/%d] restart_queue - move to active\n",
594 buf, buf->vb.i);
559 } else { 595 } else {
560 return 0; 596 return 0;
561 } 597 }
@@ -565,19 +601,23 @@ static int restart_video_queue(struct vivi_dmaqueue *dma_q)
565 601
566static void vivi_vid_timeout(unsigned long data) 602static void vivi_vid_timeout(unsigned long data)
567{ 603{
568 struct vivi_dev *dev = (struct vivi_dev*)data; 604 struct vivi_dev *dev = (struct vivi_dev *)data;
569 struct vivi_dmaqueue *vidq = &dev->vidq; 605 struct vivi_dmaqueue *vidq = &dev->vidq;
570 struct vivi_buffer *buf; 606 struct vivi_buffer *buf;
571 607
608 spin_lock(&dev->slock);
609
572 while (!list_empty(&vidq->active)) { 610 while (!list_empty(&vidq->active)) {
573 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue); 611 buf = list_entry(vidq->active.next,
612 struct vivi_buffer, vb.queue);
574 list_del(&buf->vb.queue); 613 list_del(&buf->vb.queue);
575 buf->vb.state = STATE_ERROR; 614 buf->vb.state = VIDEOBUF_ERROR;
576 wake_up(&buf->vb.done); 615 wake_up(&buf->vb.done);
577 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i); 616 printk(KERN_INFO "vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
578 } 617 }
579
580 restart_video_queue(vidq); 618 restart_video_queue(vidq);
619
620 spin_unlock(&dev->slock);
581} 621}
582 622
583/* ------------------------------------------------------------------ 623/* ------------------------------------------------------------------
@@ -586,7 +626,8 @@ static void vivi_vid_timeout(unsigned long data)
586static int 626static int
587buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) 627buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
588{ 628{
589 struct vivi_fh *fh = vq->priv_data; 629 struct vivi_fh *fh = vq->priv_data;
630 struct vivi_dev *dev = fh->dev;
590 631
591 *size = fh->width*fh->height*2; 632 *size = fh->width*fh->height*2;
592 633
@@ -596,21 +637,25 @@ buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
596 while (*size * *count > vid_limit * 1024 * 1024) 637 while (*size * *count > vid_limit * 1024 * 1024)
597 (*count)--; 638 (*count)--;
598 639
599 dprintk(1,"%s, count=%d, size=%d\n",__FUNCTION__,*count, *size); 640 dprintk(dev, 1, "%s, count=%d, size=%d\n", __FUNCTION__,
641 *count, *size);
600 642
601 return 0; 643 return 0;
602} 644}
603 645
604static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf) 646static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
605{ 647{
606 dprintk(1,"%s\n",__FUNCTION__); 648 struct vivi_fh *fh = vq->priv_data;
649 struct vivi_dev *dev = fh->dev;
650
651 dprintk(dev, 1, "%s\n", __FUNCTION__);
607 652
608 if (in_interrupt()) 653 if (in_interrupt())
609 BUG(); 654 BUG();
610 655
611 videobuf_waiton(&buf->vb,0,0); 656 videobuf_waiton(&buf->vb, 0, 0);
612 videobuf_vmalloc_free(&buf->vb); 657 videobuf_vmalloc_free(&buf->vb);
613 buf->vb.state = STATE_NEEDS_INIT; 658 buf->vb.state = VIDEOBUF_NEEDS_INIT;
614} 659}
615 660
616#define norm_maxw() 1024 661#define norm_maxw() 1024
@@ -620,10 +665,11 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
620 enum v4l2_field field) 665 enum v4l2_field field)
621{ 666{
622 struct vivi_fh *fh = vq->priv_data; 667 struct vivi_fh *fh = vq->priv_data;
623 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb); 668 struct vivi_dev *dev = fh->dev;
669 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
624 int rc, init_buffer = 0; 670 int rc, init_buffer = 0;
625 671
626 dprintk(1,"%s, field=%d\n",__FUNCTION__,field); 672 dprintk(dev, 1, "%s, field=%d\n", __FUNCTION__, field);
627 673
628 BUG_ON(NULL == fh->fmt); 674 BUG_ON(NULL == fh->fmt);
629 if (fh->width < 48 || fh->width > norm_maxw() || 675 if (fh->width < 48 || fh->width > norm_maxw() ||
@@ -644,75 +690,81 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
644 init_buffer = 1; 690 init_buffer = 1;
645 } 691 }
646 692
647 if (STATE_NEEDS_INIT == buf->vb.state) { 693 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
648 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL))) 694 rc = videobuf_iolock(vq, &buf->vb, NULL);
695 if (rc < 0)
649 goto fail; 696 goto fail;
650 } 697 }
651 698
652 buf->vb.state = STATE_PREPARED; 699 buf->vb.state = VIDEOBUF_PREPARED;
653 700
654 return 0; 701 return 0;
655 702
656fail: 703fail:
657 free_buffer(vq,buf); 704 free_buffer(vq, buf);
658 return rc; 705 return rc;
659} 706}
660 707
661static void 708static void
662buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) 709buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
663{ 710{
664 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb); 711 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
665 struct vivi_fh *fh = vq->priv_data; 712 struct vivi_fh *fh = vq->priv_data;
666 struct vivi_dev *dev = fh->dev; 713 struct vivi_dev *dev = fh->dev;
667 struct vivi_dmaqueue *vidq = &dev->vidq; 714 struct vivi_dmaqueue *vidq = &dev->vidq;
668 struct vivi_buffer *prev; 715 struct vivi_buffer *prev;
669 716
670 if (!list_empty(&vidq->queued)) { 717 if (!list_empty(&vidq->queued)) {
671 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue); 718 dprintk(dev, 1, "adding vb queue=0x%08lx\n",
672 list_add_tail(&buf->vb.queue,&vidq->queued); 719 (unsigned long)&buf->vb.queue);
673 buf->vb.state = STATE_QUEUED; 720 list_add_tail(&buf->vb.queue, &vidq->queued);
674 dprintk(2,"[%p/%d] buffer_queue - append to queued\n", 721 buf->vb.state = VIDEOBUF_QUEUED;
722 dprintk(dev, 2, "[%p/%d] buffer_queue - append to queued\n",
675 buf, buf->vb.i); 723 buf, buf->vb.i);
676 } else if (list_empty(&vidq->active)) { 724 } else if (list_empty(&vidq->active)) {
677 list_add_tail(&buf->vb.queue,&vidq->active); 725 list_add_tail(&buf->vb.queue, &vidq->active);
678 726
679 buf->vb.state = STATE_ACTIVE; 727 buf->vb.state = VIDEOBUF_ACTIVE;
680 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT); 728 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
681 dprintk(2,"[%p/%d] buffer_queue - first active\n", 729 dprintk(dev, 2, "[%p/%d] buffer_queue - first active\n",
682 buf, buf->vb.i); 730 buf, buf->vb.i);
683 731
684 vivi_start_thread(vidq); 732 vivi_start_thread(vidq);
685 } else { 733 } else {
686 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue); 734 prev = list_entry(vidq->active.prev,
735 struct vivi_buffer, vb.queue);
687 if (prev->vb.width == buf->vb.width && 736 if (prev->vb.width == buf->vb.width &&
688 prev->vb.height == buf->vb.height && 737 prev->vb.height == buf->vb.height &&
689 prev->fmt == buf->fmt) { 738 prev->fmt == buf->fmt) {
690 list_add_tail(&buf->vb.queue,&vidq->active); 739 list_add_tail(&buf->vb.queue, &vidq->active);
691 buf->vb.state = STATE_ACTIVE; 740 buf->vb.state = VIDEOBUF_ACTIVE;
692 dprintk(2,"[%p/%d] buffer_queue - append to active\n", 741 dprintk(dev, 2,
742 "[%p/%d] buffer_queue - append to active\n",
693 buf, buf->vb.i); 743 buf, buf->vb.i);
694 744
695 } else { 745 } else {
696 list_add_tail(&buf->vb.queue,&vidq->queued); 746 list_add_tail(&buf->vb.queue, &vidq->queued);
697 buf->vb.state = STATE_QUEUED; 747 buf->vb.state = VIDEOBUF_QUEUED;
698 dprintk(2,"[%p/%d] buffer_queue - first queued\n", 748 dprintk(dev, 2,
749 "[%p/%d] buffer_queue - first queued\n",
699 buf, buf->vb.i); 750 buf, buf->vb.i);
700 } 751 }
701 } 752 }
702} 753}
703 754
704static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb) 755static void buffer_release(struct videobuf_queue *vq,
756 struct videobuf_buffer *vb)
705{ 757{
706 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb); 758 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
707 struct vivi_fh *fh = vq->priv_data; 759 struct vivi_fh *fh = vq->priv_data;
708 struct vivi_dev *dev = (struct vivi_dev*)fh->dev; 760 struct vivi_dev *dev = (struct vivi_dev *)fh->dev;
709 struct vivi_dmaqueue *vidq = &dev->vidq; 761 struct vivi_dmaqueue *vidq = &dev->vidq;
710 762
711 dprintk(1,"%s\n",__FUNCTION__); 763 dprintk(dev, 1, "%s\n", __FUNCTION__);
712 764
713 vivi_stop_thread(vidq); 765 vivi_stop_thread(vidq);
714 766
715 free_buffer(vq,buf); 767 free_buffer(vq, buf);
716} 768}
717 769
718static struct videobuf_queue_ops vivi_video_qops = { 770static struct videobuf_queue_ops vivi_video_qops = {
@@ -725,7 +777,7 @@ static struct videobuf_queue_ops vivi_video_qops = {
725/* ------------------------------------------------------------------ 777/* ------------------------------------------------------------------
726 IOCTL vidioc handling 778 IOCTL vidioc handling
727 ------------------------------------------------------------------*/ 779 ------------------------------------------------------------------*/
728static int vidioc_querycap (struct file *file, void *priv, 780static int vidioc_querycap(struct file *file, void *priv,
729 struct v4l2_capability *cap) 781 struct v4l2_capability *cap)
730{ 782{
731 strcpy(cap->driver, "vivi"); 783 strcpy(cap->driver, "vivi");
@@ -737,21 +789,21 @@ static int vidioc_querycap (struct file *file, void *priv,
737 return 0; 789 return 0;
738} 790}
739 791
740static int vidioc_enum_fmt_cap (struct file *file, void *priv, 792static int vidioc_enum_fmt_cap(struct file *file, void *priv,
741 struct v4l2_fmtdesc *f) 793 struct v4l2_fmtdesc *f)
742{ 794{
743 if (f->index > 0) 795 if (f->index > 0)
744 return -EINVAL; 796 return -EINVAL;
745 797
746 strlcpy(f->description,format.name,sizeof(f->description)); 798 strlcpy(f->description, format.name, sizeof(f->description));
747 f->pixelformat = format.fourcc; 799 f->pixelformat = format.fourcc;
748 return 0; 800 return 0;
749} 801}
750 802
751static int vidioc_g_fmt_cap (struct file *file, void *priv, 803static int vidioc_g_fmt_cap(struct file *file, void *priv,
752 struct v4l2_format *f) 804 struct v4l2_format *f)
753{ 805{
754 struct vivi_fh *fh=priv; 806 struct vivi_fh *fh = priv;
755 807
756 f->fmt.pix.width = fh->width; 808 f->fmt.pix.width = fh->width;
757 f->fmt.pix.height = fh->height; 809 f->fmt.pix.height = fh->height;
@@ -765,26 +817,29 @@ static int vidioc_g_fmt_cap (struct file *file, void *priv,
765 return (0); 817 return (0);
766} 818}
767 819
768static int vidioc_try_fmt_cap (struct file *file, void *priv, 820static int vidioc_try_fmt_cap(struct file *file, void *priv,
769 struct v4l2_format *f) 821 struct v4l2_format *f)
770{ 822{
823 struct vivi_fh *fh = priv;
824 struct vivi_dev *dev = fh->dev;
771 struct vivi_fmt *fmt; 825 struct vivi_fmt *fmt;
772 enum v4l2_field field; 826 enum v4l2_field field;
773 unsigned int maxw, maxh; 827 unsigned int maxw, maxh;
774 828
775 if (format.fourcc != f->fmt.pix.pixelformat) { 829 if (format.fourcc != f->fmt.pix.pixelformat) {
776 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts " 830 dprintk(dev, 1, "Fourcc format (0x%08x) invalid. "
777 "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc); 831 "Driver accepts only 0x%08x\n",
832 f->fmt.pix.pixelformat, format.fourcc);
778 return -EINVAL; 833 return -EINVAL;
779 } 834 }
780 fmt=&format; 835 fmt = &format;
781 836
782 field = f->fmt.pix.field; 837 field = f->fmt.pix.field;
783 838
784 if (field == V4L2_FIELD_ANY) { 839 if (field == V4L2_FIELD_ANY) {
785 field=V4L2_FIELD_INTERLACED; 840 field = V4L2_FIELD_INTERLACED;
786 } else if (V4L2_FIELD_INTERLACED != field) { 841 } else if (V4L2_FIELD_INTERLACED != field) {
787 dprintk(1,"Field type invalid.\n"); 842 dprintk(dev, 1, "Field type invalid.\n");
788 return -EINVAL; 843 return -EINVAL;
789 } 844 }
790 845
@@ -810,11 +865,11 @@ static int vidioc_try_fmt_cap (struct file *file, void *priv,
810} 865}
811 866
812/*FIXME: This seems to be generic enough to be at videodev2 */ 867/*FIXME: This seems to be generic enough to be at videodev2 */
813static int vidioc_s_fmt_cap (struct file *file, void *priv, 868static int vidioc_s_fmt_cap(struct file *file, void *priv,
814 struct v4l2_format *f) 869 struct v4l2_format *f)
815{ 870{
816 struct vivi_fh *fh=priv; 871 struct vivi_fh *fh = priv;
817 int ret = vidioc_try_fmt_cap(file,fh,f); 872 int ret = vidioc_try_fmt_cap(file, fh, f);
818 if (ret < 0) 873 if (ret < 0)
819 return (ret); 874 return (ret);
820 875
@@ -827,47 +882,48 @@ static int vidioc_s_fmt_cap (struct file *file, void *priv,
827 return (0); 882 return (0);
828} 883}
829 884
830static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p) 885static int vidioc_reqbufs(struct file *file, void *priv,
886 struct v4l2_requestbuffers *p)
831{ 887{
832 struct vivi_fh *fh=priv; 888 struct vivi_fh *fh = priv;
833 889
834 return (videobuf_reqbufs(&fh->vb_vidq, p)); 890 return (videobuf_reqbufs(&fh->vb_vidq, p));
835} 891}
836 892
837static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p) 893static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
838{ 894{
839 struct vivi_fh *fh=priv; 895 struct vivi_fh *fh = priv;
840 896
841 return (videobuf_querybuf(&fh->vb_vidq, p)); 897 return (videobuf_querybuf(&fh->vb_vidq, p));
842} 898}
843 899
844static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p) 900static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
845{ 901{
846 struct vivi_fh *fh=priv; 902 struct vivi_fh *fh = priv;
847 903
848 return (videobuf_qbuf(&fh->vb_vidq, p)); 904 return (videobuf_qbuf(&fh->vb_vidq, p));
849} 905}
850 906
851static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p) 907static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
852{ 908{
853 struct vivi_fh *fh=priv; 909 struct vivi_fh *fh = priv;
854 910
855 return (videobuf_dqbuf(&fh->vb_vidq, p, 911 return (videobuf_dqbuf(&fh->vb_vidq, p,
856 file->f_flags & O_NONBLOCK)); 912 file->f_flags & O_NONBLOCK));
857} 913}
858 914
859#ifdef CONFIG_VIDEO_V4L1_COMPAT 915#ifdef CONFIG_VIDEO_V4L1_COMPAT
860static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf) 916static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
861{ 917{
862 struct vivi_fh *fh=priv; 918 struct vivi_fh *fh = priv;
863 919
864 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8); 920 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
865} 921}
866#endif 922#endif
867 923
868static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i) 924static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
869{ 925{
870 struct vivi_fh *fh=priv; 926 struct vivi_fh *fh = priv;
871 927
872 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 928 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
873 return -EINVAL; 929 return -EINVAL;
@@ -879,7 +935,7 @@ static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
879 935
880static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) 936static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
881{ 937{
882 struct vivi_fh *fh=priv; 938 struct vivi_fh *fh = priv;
883 939
884 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 940 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
885 return -EINVAL; 941 return -EINVAL;
@@ -889,32 +945,32 @@ static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
889 return videobuf_streamoff(&fh->vb_vidq); 945 return videobuf_streamoff(&fh->vb_vidq);
890} 946}
891 947
892static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i) 948static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
893{ 949{
894 return 0; 950 return 0;
895} 951}
896 952
897/* only one input in this sample driver */ 953/* only one input in this sample driver */
898static int vidioc_enum_input (struct file *file, void *priv, 954static int vidioc_enum_input(struct file *file, void *priv,
899 struct v4l2_input *inp) 955 struct v4l2_input *inp)
900{ 956{
901 if (inp->index != 0) 957 if (inp->index != 0)
902 return -EINVAL; 958 return -EINVAL;
903 959
904 inp->type = V4L2_INPUT_TYPE_CAMERA; 960 inp->type = V4L2_INPUT_TYPE_CAMERA;
905 inp->std = V4L2_STD_NTSC_M; 961 inp->std = V4L2_STD_525_60;
906 strcpy(inp->name,"Camera"); 962 strcpy(inp->name, "Camera");
907 963
908 return (0); 964 return (0);
909} 965}
910 966
911static int vidioc_g_input (struct file *file, void *priv, unsigned int *i) 967static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
912{ 968{
913 *i = 0; 969 *i = 0;
914 970
915 return (0); 971 return (0);
916} 972}
917static int vidioc_s_input (struct file *file, void *priv, unsigned int i) 973static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
918{ 974{
919 if (i > 0) 975 if (i > 0)
920 return -EINVAL; 976 return -EINVAL;
@@ -923,8 +979,8 @@ static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
923} 979}
924 980
925 /* --- controls ---------------------------------------------- */ 981 /* --- controls ---------------------------------------------- */
926static int vidioc_queryctrl (struct file *file, void *priv, 982static int vidioc_queryctrl(struct file *file, void *priv,
927 struct v4l2_queryctrl *qc) 983 struct v4l2_queryctrl *qc)
928{ 984{
929 int i; 985 int i;
930 986
@@ -938,33 +994,31 @@ static int vidioc_queryctrl (struct file *file, void *priv,
938 return -EINVAL; 994 return -EINVAL;
939} 995}
940 996
941static int vidioc_g_ctrl (struct file *file, void *priv, 997static int vidioc_g_ctrl(struct file *file, void *priv,
942 struct v4l2_control *ctrl) 998 struct v4l2_control *ctrl)
943{ 999{
944 int i; 1000 int i;
945 1001
946 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++) 1002 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
947 if (ctrl->id == vivi_qctrl[i].id) { 1003 if (ctrl->id == vivi_qctrl[i].id) {
948 ctrl->value=qctl_regs[i]; 1004 ctrl->value = qctl_regs[i];
949 return (0); 1005 return (0);
950 } 1006 }
951 1007
952 return -EINVAL; 1008 return -EINVAL;
953} 1009}
954static int vidioc_s_ctrl (struct file *file, void *priv, 1010static int vidioc_s_ctrl(struct file *file, void *priv,
955 struct v4l2_control *ctrl) 1011 struct v4l2_control *ctrl)
956{ 1012{
957 int i; 1013 int i;
958 1014
959 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++) 1015 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
960 if (ctrl->id == vivi_qctrl[i].id) { 1016 if (ctrl->id == vivi_qctrl[i].id) {
961 if (ctrl->value < 1017 if (ctrl->value < vivi_qctrl[i].minimum
962 vivi_qctrl[i].minimum 1018 || ctrl->value > vivi_qctrl[i].maximum) {
963 || ctrl->value >
964 vivi_qctrl[i].maximum) {
965 return (-ERANGE); 1019 return (-ERANGE);
966 } 1020 }
967 qctl_regs[i]=ctrl->value; 1021 qctl_regs[i] = ctrl->value;
968 return (0); 1022 return (0);
969 } 1023 }
970 return -EINVAL; 1024 return -EINVAL;
@@ -983,24 +1037,22 @@ static int vivi_open(struct inode *inode, struct file *file)
983 struct vivi_fh *fh; 1037 struct vivi_fh *fh;
984 int i; 1038 int i;
985 1039
986 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor); 1040 printk(KERN_DEBUG "vivi: open called (minor=%d)\n", minor);
987 1041
988 list_for_each_entry(dev, &vivi_devlist, vivi_devlist) 1042 list_for_each_entry(dev, &vivi_devlist, vivi_devlist)
989 if (dev->vfd.minor == minor) 1043 if (dev->vfd->minor == minor)
990 goto found; 1044 goto found;
991 return -ENODEV; 1045 return -ENODEV;
992found:
993
994
995 1046
1047found:
996 /* If more than one user, mutex should be added */ 1048 /* If more than one user, mutex should be added */
997 dev->users++; 1049 dev->users++;
998 1050
999 dprintk(1, "open minor=%d type=%s users=%d\n", minor, 1051 dprintk(dev, 1, "open minor=%d type=%s users=%d\n", minor,
1000 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users); 1052 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1001 1053
1002 /* allocate + initialize per filehandle data */ 1054 /* allocate + initialize per filehandle data */
1003 fh = kzalloc(sizeof(*fh),GFP_KERNEL); 1055 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1004 if (NULL == fh) { 1056 if (NULL == fh) {
1005 dev->users--; 1057 dev->users--;
1006 return -ENOMEM; 1058 return -ENOMEM;
@@ -1016,27 +1068,21 @@ found:
1016 1068
1017 /* Put all controls at a sane state */ 1069 /* Put all controls at a sane state */
1018 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++) 1070 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1019 qctl_regs[i] =vivi_qctrl[i].default_value; 1071 qctl_regs[i] = vivi_qctrl[i].default_value;
1020
1021 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1022 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1023 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1024 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1025 1072
1026 /* Resets frame counters */ 1073 /* Resets frame counters */
1027 dev->h=0; 1074 dev->h = 0;
1028 dev->m=0; 1075 dev->m = 0;
1029 dev->s=0; 1076 dev->s = 0;
1030 dev->us=0; 1077 dev->ms = 0;
1031 dev->jiffies=jiffies; 1078 dev->mv_count = 0;
1032 sprintf(dev->timestr,"%02d:%02d:%02d:%03d", 1079 dev->jiffies = jiffies;
1033 dev->h,dev->m,dev->s,(dev->us+500)/1000); 1080 sprintf(dev->timestr, "%02d:%02d:%02d:%03d",
1081 dev->h, dev->m, dev->s, dev->ms);
1034 1082
1035 videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops, 1083 videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops,
1036 NULL, NULL, 1084 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1037 fh->type, 1085 sizeof(struct vivi_buffer), fh);
1038 V4L2_FIELD_INTERLACED,
1039 sizeof(struct vivi_buffer),fh);
1040 1086
1041 return 0; 1087 return 0;
1042} 1088}
@@ -1044,9 +1090,9 @@ found:
1044static ssize_t 1090static ssize_t
1045vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos) 1091vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1046{ 1092{
1047 struct vivi_fh *fh = file->private_data; 1093 struct vivi_fh *fh = file->private_data;
1048 1094
1049 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) { 1095 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1050 return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0, 1096 return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
1051 file->f_flags & O_NONBLOCK); 1097 file->f_flags & O_NONBLOCK);
1052 } 1098 }
@@ -1057,9 +1103,10 @@ static unsigned int
1057vivi_poll(struct file *file, struct poll_table_struct *wait) 1103vivi_poll(struct file *file, struct poll_table_struct *wait)
1058{ 1104{
1059 struct vivi_fh *fh = file->private_data; 1105 struct vivi_fh *fh = file->private_data;
1106 struct vivi_dev *dev = fh->dev;
1060 struct videobuf_queue *q = &fh->vb_vidq; 1107 struct videobuf_queue *q = &fh->vb_vidq;
1061 1108
1062 dprintk(1,"%s\n",__FUNCTION__); 1109 dprintk(dev, 1, "%s\n", __FUNCTION__);
1063 1110
1064 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type) 1111 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1065 return POLLERR; 1112 return POLLERR;
@@ -1067,7 +1114,7 @@ vivi_poll(struct file *file, struct poll_table_struct *wait)
1067 return videobuf_poll_stream(file, q, wait); 1114 return videobuf_poll_stream(file, q, wait);
1068} 1115}
1069 1116
1070static int vivi_release(struct inode *inode, struct file *file) 1117static int vivi_close(struct inode *inode, struct file *file)
1071{ 1118{
1072 struct vivi_fh *fh = file->private_data; 1119 struct vivi_fh *fh = file->private_data;
1073 struct vivi_dev *dev = fh->dev; 1120 struct vivi_dev *dev = fh->dev;
@@ -1079,26 +1126,48 @@ static int vivi_release(struct inode *inode, struct file *file)
1079 videobuf_stop(&fh->vb_vidq); 1126 videobuf_stop(&fh->vb_vidq);
1080 videobuf_mmap_free(&fh->vb_vidq); 1127 videobuf_mmap_free(&fh->vb_vidq);
1081 1128
1082 kfree (fh); 1129 kfree(fh);
1083 1130
1084 dev->users--; 1131 dev->users--;
1085 1132
1086 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users); 1133 dprintk(dev, 1, "close called (minor=%d, users=%d)\n",
1134 minor, dev->users);
1087 1135
1088 return 0; 1136 return 0;
1089} 1137}
1090 1138
1091static int 1139static int vivi_release(void)
1092vivi_mmap(struct file *file, struct vm_area_struct * vma)
1093{ 1140{
1094 struct vivi_fh *fh = file->private_data; 1141 struct vivi_dev *dev;
1142 struct list_head *list;
1143
1144 while (!list_empty(&vivi_devlist)) {
1145 list = vivi_devlist.next;
1146 list_del(list);
1147 dev = list_entry(list, struct vivi_dev, vivi_devlist);
1148
1149 if (-1 != dev->vfd->minor)
1150 video_unregister_device(dev->vfd);
1151 else
1152 video_device_release(dev->vfd);
1153
1154 kfree(dev);
1155 }
1156
1157 return 0;
1158}
1159
1160static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
1161{
1162 struct vivi_fh *fh = file->private_data;
1163 struct vivi_dev *dev = fh->dev;
1095 int ret; 1164 int ret;
1096 1165
1097 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma); 1166 dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1098 1167
1099 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma); 1168 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1100 1169
1101 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n", 1170 dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1102 (unsigned long)vma->vm_start, 1171 (unsigned long)vma->vm_start,
1103 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start, 1172 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1104 ret); 1173 ret);
@@ -1109,7 +1178,7 @@ vivi_mmap(struct file *file, struct vm_area_struct * vma)
1109static const struct file_operations vivi_fops = { 1178static const struct file_operations vivi_fops = {
1110 .owner = THIS_MODULE, 1179 .owner = THIS_MODULE,
1111 .open = vivi_open, 1180 .open = vivi_open,
1112 .release = vivi_release, 1181 .release = vivi_close,
1113 .read = vivi_read, 1182 .read = vivi_read,
1114 .poll = vivi_poll, 1183 .poll = vivi_poll,
1115 .ioctl = video_ioctl2, /* V4L2 ioctl handler */ 1184 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
@@ -1117,12 +1186,12 @@ static const struct file_operations vivi_fops = {
1117 .llseek = no_llseek, 1186 .llseek = no_llseek,
1118}; 1187};
1119 1188
1120static struct video_device vivi = { 1189static struct video_device vivi_template = {
1121 .name = "vivi", 1190 .name = "vivi",
1122 .type = VID_TYPE_CAPTURE, 1191 .type = VID_TYPE_CAPTURE,
1123 .fops = &vivi_fops, 1192 .fops = &vivi_fops,
1124 .minor = -1, 1193 .minor = -1,
1125// .release = video_device_release, 1194 .release = video_device_release,
1126 1195
1127 .vidioc_querycap = vidioc_querycap, 1196 .vidioc_querycap = vidioc_querycap,
1128 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap, 1197 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
@@ -1145,7 +1214,7 @@ static struct video_device vivi = {
1145#ifdef CONFIG_VIDEO_V4L1_COMPAT 1214#ifdef CONFIG_VIDEO_V4L1_COMPAT
1146 .vidiocgmbuf = vidiocgmbuf, 1215 .vidiocgmbuf = vidiocgmbuf,
1147#endif 1216#endif
1148 .tvnorms = V4L2_STD_NTSC_M, 1217 .tvnorms = V4L2_STD_525_60,
1149 .current_norm = V4L2_STD_NTSC_M, 1218 .current_norm = V4L2_STD_NTSC_M,
1150}; 1219};
1151/* ----------------------------------------------------------------- 1220/* -----------------------------------------------------------------
@@ -1154,43 +1223,61 @@ static struct video_device vivi = {
1154 1223
1155static int __init vivi_init(void) 1224static int __init vivi_init(void)
1156{ 1225{
1157 int ret; 1226 int ret = -ENOMEM, i;
1158 struct vivi_dev *dev; 1227 struct vivi_dev *dev;
1228 struct video_device *vfd;
1159 1229
1160 dev = kzalloc(sizeof(*dev),GFP_KERNEL); 1230 for (i = 0; i < n_devs; i++) {
1161 if (NULL == dev) 1231 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1162 return -ENOMEM; 1232 if (NULL == dev)
1163 list_add_tail(&dev->vivi_devlist,&vivi_devlist); 1233 break;
1164 1234
1165 /* init video dma queues */ 1235 list_add_tail(&dev->vivi_devlist, &vivi_devlist);
1166 INIT_LIST_HEAD(&dev->vidq.active);
1167 INIT_LIST_HEAD(&dev->vidq.queued);
1168 init_waitqueue_head(&dev->vidq.wq);
1169 1236
1170 /* initialize locks */ 1237 /* init video dma queues */
1171 mutex_init(&dev->lock); 1238 INIT_LIST_HEAD(&dev->vidq.active);
1239 INIT_LIST_HEAD(&dev->vidq.queued);
1240 init_waitqueue_head(&dev->vidq.wq);
1172 1241
1173 dev->vidq.timeout.function = vivi_vid_timeout; 1242 /* initialize locks */
1174 dev->vidq.timeout.data = (unsigned long)dev; 1243 mutex_init(&dev->lock);
1175 init_timer(&dev->vidq.timeout); 1244 spin_lock_init(&dev->slock);
1176 1245
1177 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr); 1246 dev->vidq.timeout.function = vivi_vid_timeout;
1178 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret); 1247 dev->vidq.timeout.data = (unsigned long)dev;
1248 init_timer(&dev->vidq.timeout);
1249
1250 vfd = video_device_alloc();
1251 if (NULL == vfd)
1252 break;
1253
1254 *vfd = vivi_template;
1255
1256 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
1257 if (ret < 0)
1258 break;
1259
1260 snprintf(vfd->name, sizeof(vfd->name), "%s (%i)",
1261 vivi_template.name, vfd->minor);
1262
1263 if (video_nr >= 0)
1264 video_nr++;
1265
1266 dev->vfd = vfd;
1267 }
1268
1269 if (ret < 0) {
1270 vivi_release();
1271 printk(KERN_INFO "Error %d while loading vivi driver\n", ret);
1272 } else
1273 printk(KERN_INFO "Video Technology Magazine Virtual Video "
1274 "Capture Board successfully loaded.\n");
1179 return ret; 1275 return ret;
1180} 1276}
1181 1277
1182static void __exit vivi_exit(void) 1278static void __exit vivi_exit(void)
1183{ 1279{
1184 struct vivi_dev *h; 1280 vivi_release();
1185 struct list_head *list;
1186
1187 while (!list_empty(&vivi_devlist)) {
1188 list = vivi_devlist.next;
1189 list_del(list);
1190 h = list_entry(list, struct vivi_dev, vivi_devlist);
1191 kfree (h);
1192 }
1193 video_unregister_device(&vivi);
1194} 1281}
1195 1282
1196module_init(vivi_init); 1283module_init(vivi_init);
@@ -1201,10 +1288,13 @@ MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1201MODULE_LICENSE("Dual BSD/GPL"); 1288MODULE_LICENSE("Dual BSD/GPL");
1202 1289
1203module_param(video_nr, int, 0); 1290module_param(video_nr, int, 0);
1291MODULE_PARM_DESC(video_nr, "video iminor start number");
1204 1292
1205module_param_named(debug,vivi.debug, int, 0644); 1293module_param(n_devs, int, 0);
1206MODULE_PARM_DESC(debug,"activates debug info"); 1294MODULE_PARM_DESC(n_devs, "number of video devices to create");
1207 1295
1208module_param(vid_limit,int,0644); 1296module_param_named(debug, vivi_template.debug, int, 0444);
1209MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes"); 1297MODULE_PARM_DESC(debug, "activates debug info");
1210 1298
1299module_param(vid_limit, int, 0644);
1300MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
diff --git a/drivers/media/video/vp27smpx.c b/drivers/media/video/vp27smpx.c
index 63002e0ac764..282c81403c97 100644
--- a/drivers/media/video/vp27smpx.c
+++ b/drivers/media/video/vp27smpx.c
@@ -30,15 +30,12 @@
30#include <linux/videodev.h> 30#include <linux/videodev.h>
31#include <media/v4l2-common.h> 31#include <media/v4l2-common.h>
32#include <media/v4l2-chip-ident.h> 32#include <media/v4l2-chip-ident.h>
33#include <media/v4l2-i2c-drv.h>
33 34
34MODULE_DESCRIPTION("vp27smpx driver"); 35MODULE_DESCRIPTION("vp27smpx driver");
35MODULE_AUTHOR("Hans Verkuil"); 36MODULE_AUTHOR("Hans Verkuil");
36MODULE_LICENSE("GPL"); 37MODULE_LICENSE("GPL");
37 38
38static unsigned short normal_i2c[] = { 0xb6 >> 1, I2C_CLIENT_END };
39
40
41I2C_CLIENT_INSMOD;
42 39
43/* ----------------------------------------------------------------------- */ 40/* ----------------------------------------------------------------------- */
44 41
@@ -53,28 +50,26 @@ static void vp27smpx_set_audmode(struct i2c_client *client, u32 audmode)
53 u8 data[3] = { 0x00, 0x00, 0x04 }; 50 u8 data[3] = { 0x00, 0x00, 0x04 };
54 51
55 switch (audmode) { 52 switch (audmode) {
56 case V4L2_TUNER_MODE_MONO: 53 case V4L2_TUNER_MODE_MONO:
57 case V4L2_TUNER_MODE_LANG1: 54 case V4L2_TUNER_MODE_LANG1:
58 break; 55 break;
59 case V4L2_TUNER_MODE_STEREO: 56 case V4L2_TUNER_MODE_STEREO:
60 case V4L2_TUNER_MODE_LANG1_LANG2: 57 case V4L2_TUNER_MODE_LANG1_LANG2:
61 data[1] = 0x01; 58 data[1] = 0x01;
62 break; 59 break;
63 case V4L2_TUNER_MODE_LANG2: 60 case V4L2_TUNER_MODE_LANG2:
64 data[1] = 0x02; 61 data[1] = 0x02;
65 break; 62 break;
66 } 63 }
67 64
68 if (i2c_master_send(client, data, sizeof(data)) != sizeof(data)) { 65 if (i2c_master_send(client, data, sizeof(data)) != sizeof(data))
69 v4l_err(client, "%s: I/O error setting audmode\n", client->name); 66 v4l_err(client, "%s: I/O error setting audmode\n",
70 } 67 client->name);
71 else { 68 else
72 state->audmode = audmode; 69 state->audmode = audmode;
73 }
74} 70}
75 71
76static int vp27smpx_command(struct i2c_client *client, unsigned int cmd, 72static int vp27smpx_command(struct i2c_client *client, unsigned cmd, void *arg)
77 void *arg)
78{ 73{
79 struct vp27smpx_state *state = i2c_get_clientdata(client); 74 struct vp27smpx_state *state = i2c_get_clientdata(client);
80 struct v4l2_tuner *vt = arg; 75 struct v4l2_tuner *vt = arg;
@@ -103,7 +98,8 @@ static int vp27smpx_command(struct i2c_client *client, unsigned int cmd,
103 break; 98 break;
104 99
105 case VIDIOC_G_CHIP_IDENT: 100 case VIDIOC_G_CHIP_IDENT:
106 return v4l2_chip_ident_i2c_client(client, arg, V4L2_IDENT_VP27SMPX, 0); 101 return v4l2_chip_ident_i2c_client(client, arg,
102 V4L2_IDENT_VP27SMPX, 0);
107 103
108 case VIDIOC_LOG_STATUS: 104 case VIDIOC_LOG_STATUS:
109 v4l_info(client, "Audio Mode: %u%s\n", state->audmode, 105 v4l_info(client, "Audio Mode: %u%s\n", state->audmode,
@@ -125,88 +121,43 @@ static int vp27smpx_command(struct i2c_client *client, unsigned int cmd,
125 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1' 121 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
126 */ 122 */
127 123
128static struct i2c_driver i2c_driver; 124static int vp27smpx_probe(struct i2c_client *client)
129
130static int vp27smpx_attach(struct i2c_adapter *adapter, int address, int kind)
131{ 125{
132 struct i2c_client *client;
133 struct vp27smpx_state *state; 126 struct vp27smpx_state *state;
134 127
135 /* Check if the adapter supports the needed features */ 128 /* Check if the adapter supports the needed features */
136 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 129 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
137 return 0; 130 return -EIO;
138
139 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
140 if (client == 0)
141 return -ENOMEM;
142 131
143 client->addr = address;
144 client->adapter = adapter;
145 client->driver = &i2c_driver;
146 snprintf(client->name, sizeof(client->name) - 1, "vp27smpx"); 132 snprintf(client->name, sizeof(client->name) - 1, "vp27smpx");
147 133
148 v4l_info(client, "chip found @ 0x%x (%s)\n", address << 1, adapter->name); 134 v4l_info(client, "chip found @ 0x%x (%s)\n",
135 client->addr << 1, client->adapter->name);
149 136
150 state = kzalloc(sizeof(struct vp27smpx_state), GFP_KERNEL); 137 state = kzalloc(sizeof(struct vp27smpx_state), GFP_KERNEL);
151 if (state == NULL) { 138 if (state == NULL)
152 kfree(client);
153 return -ENOMEM; 139 return -ENOMEM;
154 }
155 state->audmode = V4L2_TUNER_MODE_STEREO; 140 state->audmode = V4L2_TUNER_MODE_STEREO;
156 i2c_set_clientdata(client, state); 141 i2c_set_clientdata(client, state);
157 142
158 /* initialize vp27smpx */ 143 /* initialize vp27smpx */
159 vp27smpx_set_audmode(client, state->audmode); 144 vp27smpx_set_audmode(client, state->audmode);
160 i2c_attach_client(client);
161
162 return 0; 145 return 0;
163} 146}
164 147
165static int vp27smpx_probe(struct i2c_adapter *adapter) 148static int vp27smpx_remove(struct i2c_client *client)
166{ 149{
167 if (adapter->class & I2C_CLASS_TV_ANALOG) 150 kfree(i2c_get_clientdata(client));
168 return i2c_probe(adapter, &addr_data, vp27smpx_attach);
169 return 0;
170}
171
172static int vp27smpx_detach(struct i2c_client *client)
173{
174 struct vp27smpx_state *state = i2c_get_clientdata(client);
175 int err;
176
177 err = i2c_detach_client(client);
178 if (err) {
179 return err;
180 }
181 kfree(state);
182 kfree(client);
183
184 return 0; 151 return 0;
185} 152}
186 153
187/* ----------------------------------------------------------------------- */ 154/* ----------------------------------------------------------------------- */
188 155
189/* i2c implementation */ 156static struct v4l2_i2c_driver_data v4l2_i2c_data = {
190static struct i2c_driver i2c_driver = { 157 .name = "vp27smpx",
191 .driver = { 158 .driverid = I2C_DRIVERID_VP27SMPX,
192 .name = "vp27smpx", 159 .command = vp27smpx_command,
193 }, 160 .probe = vp27smpx_probe,
194 .id = I2C_DRIVERID_VP27SMPX, 161 .remove = vp27smpx_remove,
195 .attach_adapter = vp27smpx_probe,
196 .detach_client = vp27smpx_detach,
197 .command = vp27smpx_command,
198}; 162};
199 163
200
201static int __init vp27smpx_init_module(void)
202{
203 return i2c_add_driver(&i2c_driver);
204}
205
206static void __exit vp27smpx_cleanup_module(void)
207{
208 i2c_del_driver(&i2c_driver);
209}
210
211module_init(vp27smpx_init_module);
212module_exit(vp27smpx_cleanup_module);
diff --git a/drivers/media/video/wm8739.c b/drivers/media/video/wm8739.c
index 1bf4cbec6a87..31795b4f8b63 100644
--- a/drivers/media/video/wm8739.c
+++ b/drivers/media/video/wm8739.c
@@ -30,21 +30,19 @@
30#include <linux/videodev.h> 30#include <linux/videodev.h>
31#include <media/v4l2-common.h> 31#include <media/v4l2-common.h>
32#include <media/v4l2-chip-ident.h> 32#include <media/v4l2-chip-ident.h>
33#include <media/v4l2-i2c-drv.h>
33 34
34MODULE_DESCRIPTION("wm8739 driver"); 35MODULE_DESCRIPTION("wm8739 driver");
35MODULE_AUTHOR("T. Adachi, Hans Verkuil"); 36MODULE_AUTHOR("T. Adachi, Hans Verkuil");
36MODULE_LICENSE("GPL"); 37MODULE_LICENSE("GPL");
37 38
38static int debug = 0; 39static int debug;
39static unsigned short normal_i2c[] = { 0x34 >> 1, 0x36 >> 1, I2C_CLIENT_END };
40 40
41module_param(debug, int, 0644); 41module_param(debug, int, 0644);
42 42
43MODULE_PARM_DESC(debug, "Debug level (0-1)"); 43MODULE_PARM_DESC(debug, "Debug level (0-1)");
44 44
45 45
46I2C_CLIENT_INSMOD;
47
48/* ------------------------------------------------------------------------ */ 46/* ------------------------------------------------------------------------ */
49 47
50enum { 48enum {
@@ -75,12 +73,10 @@ static int wm8739_write(struct i2c_client *client, int reg, u16 val)
75 73
76 v4l_dbg(1, debug, client, "write: %02x %02x\n", reg, val); 74 v4l_dbg(1, debug, client, "write: %02x %02x\n", reg, val);
77 75
78 for (i = 0; i < 3; i++) { 76 for (i = 0; i < 3; i++)
79 if (i2c_smbus_write_byte_data(client, (reg << 1) | 77 if (i2c_smbus_write_byte_data(client,
80 (val >> 8), val & 0xff) == 0) { 78 (reg << 1) | (val >> 8), val & 0xff) == 0)
81 return 0; 79 return 0;
82 }
83 }
84 v4l_err(client, "I2C: cannot write %03x to register R%d\n", val, reg); 80 v4l_err(client, "I2C: cannot write %03x to register R%d\n", val, reg);
85 return -1; 81 return -1;
86} 82}
@@ -167,7 +163,7 @@ static struct v4l2_queryctrl wm8739_qctrl[] = {
167 .default_value = 58880, 163 .default_value = 58880,
168 .flags = 0, 164 .flags = 0,
169 .type = V4L2_CTRL_TYPE_INTEGER, 165 .type = V4L2_CTRL_TYPE_INTEGER,
170 },{ 166 }, {
171 .id = V4L2_CID_AUDIO_MUTE, 167 .id = V4L2_CID_AUDIO_MUTE,
172 .name = "Mute", 168 .name = "Mute",
173 .minimum = 0, 169 .minimum = 0,
@@ -176,7 +172,7 @@ static struct v4l2_queryctrl wm8739_qctrl[] = {
176 .default_value = 1, 172 .default_value = 1,
177 .flags = 0, 173 .flags = 0,
178 .type = V4L2_CTRL_TYPE_BOOLEAN, 174 .type = V4L2_CTRL_TYPE_BOOLEAN,
179 },{ 175 }, {
180 .id = V4L2_CID_AUDIO_BALANCE, 176 .id = V4L2_CID_AUDIO_BALANCE,
181 .name = "Balance", 177 .name = "Balance",
182 .minimum = 0, 178 .minimum = 0,
@@ -190,7 +186,7 @@ static struct v4l2_queryctrl wm8739_qctrl[] = {
190 186
191/* ------------------------------------------------------------------------ */ 187/* ------------------------------------------------------------------------ */
192 188
193static int wm8739_command(struct i2c_client *client, unsigned int cmd, void *arg) 189static int wm8739_command(struct i2c_client *client, unsigned cmd, void *arg)
194{ 190{
195 struct wm8739_state *state = i2c_get_clientdata(client); 191 struct wm8739_state *state = i2c_get_clientdata(client);
196 192
@@ -200,21 +196,26 @@ static int wm8739_command(struct i2c_client *client, unsigned int cmd, void *arg
200 u32 audiofreq = *(u32 *)arg; 196 u32 audiofreq = *(u32 *)arg;
201 197
202 state->clock_freq = audiofreq; 198 state->clock_freq = audiofreq;
203 wm8739_write(client, R9, 0x000); /* de-activate */ 199 /* de-activate */
200 wm8739_write(client, R9, 0x000);
204 switch (audiofreq) { 201 switch (audiofreq) {
205 case 44100: 202 case 44100:
206 wm8739_write(client, R8, 0x020); /* 256fps, fs=44.1k */ 203 /* 256fps, fs=44.1k */
204 wm8739_write(client, R8, 0x020);
207 break; 205 break;
208 case 48000: 206 case 48000:
209 wm8739_write(client, R8, 0x000); /* 256fps, fs=48k */ 207 /* 256fps, fs=48k */
208 wm8739_write(client, R8, 0x000);
210 break; 209 break;
211 case 32000: 210 case 32000:
212 wm8739_write(client, R8, 0x018); /* 256fps, fs=32k */ 211 /* 256fps, fs=32k */
212 wm8739_write(client, R8, 0x018);
213 break; 213 break;
214 default: 214 default:
215 break; 215 break;
216 } 216 }
217 wm8739_write(client, R9, 0x001); /* activate */ 217 /* activate */
218 wm8739_write(client, R9, 0x001);
218 break; 219 break;
219 } 220 }
220 221
@@ -238,7 +239,8 @@ static int wm8739_command(struct i2c_client *client, unsigned int cmd, void *arg
238 } 239 }
239 240
240 case VIDIOC_G_CHIP_IDENT: 241 case VIDIOC_G_CHIP_IDENT:
241 return v4l2_chip_ident_i2c_client(client, arg, V4L2_IDENT_WM8739, 0); 242 return v4l2_chip_ident_i2c_client(client,
243 arg, V4L2_IDENT_WM8739, 0);
242 244
243 case VIDIOC_LOG_STATUS: 245 case VIDIOC_LOG_STATUS:
244 v4l_info(client, "Frequency: %u Hz\n", state->clock_freq); 246 v4l_info(client, "Frequency: %u Hz\n", state->clock_freq);
@@ -259,27 +261,16 @@ static int wm8739_command(struct i2c_client *client, unsigned int cmd, void *arg
259 261
260/* i2c implementation */ 262/* i2c implementation */
261 263
262static struct i2c_driver i2c_driver; 264static int wm8739_probe(struct i2c_client *client)
263
264static int wm8739_attach(struct i2c_adapter *adapter, int address, int kind)
265{ 265{
266 struct i2c_client *client;
267 struct wm8739_state *state; 266 struct wm8739_state *state;
268 267
269 /* Check if the adapter supports the needed features */ 268 /* Check if the adapter supports the needed features */
270 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 269 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
271 return 0; 270 return -EIO;
272
273 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
274 if (client == NULL)
275 return -ENOMEM;
276
277 client->addr = address;
278 client->adapter = adapter;
279 client->driver = &i2c_driver;
280 snprintf(client->name, sizeof(client->name) - 1, "wm8739");
281 271
282 v4l_info(client, "chip found @ 0x%x (%s)\n", address << 1, adapter->name); 272 v4l_info(client, "chip found @ 0x%x (%s)\n",
273 client->addr << 1, client->adapter->name);
283 274
284 state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL); 275 state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL);
285 if (state == NULL) { 276 if (state == NULL) {
@@ -295,67 +286,37 @@ static int wm8739_attach(struct i2c_adapter *adapter, int address, int kind)
295 state->clock_freq = 48000; 286 state->clock_freq = 48000;
296 i2c_set_clientdata(client, state); 287 i2c_set_clientdata(client, state);
297 288
298 /* initialize wm8739 */ 289 /* Initialize wm8739 */
299 wm8739_write(client, R15, 0x00); /* reset */ 290
300 wm8739_write(client, R5, 0x000); /* filter setting, high path, offet clear */ 291 /* reset */
301 wm8739_write(client, R6, 0x000); /* ADC, OSC, Power Off mode Disable */ 292 wm8739_write(client, R15, 0x00);
302 wm8739_write(client, R7, 0x049); /* Digital Audio interface format */ 293 /* filter setting, high path, offet clear */
303 /* Enable Master mode */ 294 wm8739_write(client, R5, 0x000);
304 /* 24 bit, MSB first/left justified */ 295 /* ADC, OSC, Power Off mode Disable */
305 wm8739_write(client, R8, 0x000); /* sampling control */ 296 wm8739_write(client, R6, 0x000);
306 /* normal, 256fs, 48KHz sampling rate */ 297 /* Digital Audio interface format:
307 wm8739_write(client, R9, 0x001); /* activate */ 298 Enable Master mode, 24 bit, MSB first/left justified */
308 wm8739_set_audio(client); /* set volume/mute */ 299 wm8739_write(client, R7, 0x049);
309 300 /* sampling control: normal, 256fs, 48KHz sampling rate */
310 i2c_attach_client(client); 301 wm8739_write(client, R8, 0x000);
311 302 /* activate */
312 return 0; 303 wm8739_write(client, R9, 0x001);
313} 304 /* set volume/mute */
314 305 wm8739_set_audio(client);
315static int wm8739_probe(struct i2c_adapter *adapter)
316{
317 if (adapter->class & I2C_CLASS_TV_ANALOG)
318 return i2c_probe(adapter, &addr_data, wm8739_attach);
319 return 0; 306 return 0;
320} 307}
321 308
322static int wm8739_detach(struct i2c_client *client) 309static int wm8739_remove(struct i2c_client *client)
323{ 310{
324 struct wm8739_state *state = i2c_get_clientdata(client); 311 kfree(i2c_get_clientdata(client));
325 int err;
326
327 err = i2c_detach_client(client);
328 if (err)
329 return err;
330
331 kfree(state);
332 kfree(client);
333 return 0; 312 return 0;
334} 313}
335 314
336/* ----------------------------------------------------------------------- */ 315static struct v4l2_i2c_driver_data v4l2_i2c_data = {
337 316 .name = "wm8739",
338/* i2c implementation */ 317 .driverid = I2C_DRIVERID_WM8739,
339static struct i2c_driver i2c_driver = {
340 .driver = {
341 .name = "wm8739",
342 },
343 .id = I2C_DRIVERID_WM8739,
344 .attach_adapter = wm8739_probe,
345 .detach_client = wm8739_detach,
346 .command = wm8739_command, 318 .command = wm8739_command,
319 .probe = wm8739_probe,
320 .remove = wm8739_remove,
347}; 321};
348 322
349
350static int __init wm8739_init_module(void)
351{
352 return i2c_add_driver(&i2c_driver);
353}
354
355static void __exit wm8739_cleanup_module(void)
356{
357 i2c_del_driver(&i2c_driver);
358}
359
360module_init(wm8739_init_module);
361module_exit(wm8739_cleanup_module);
diff --git a/drivers/media/video/wm8775.c b/drivers/media/video/wm8775.c
index 9f7e894ef962..869f9e7946b6 100644
--- a/drivers/media/video/wm8775.c
+++ b/drivers/media/video/wm8775.c
@@ -34,6 +34,7 @@
34#include <linux/videodev.h> 34#include <linux/videodev.h>
35#include <media/v4l2-common.h> 35#include <media/v4l2-common.h>
36#include <media/v4l2-chip-ident.h> 36#include <media/v4l2-chip-ident.h>
37#include <media/v4l2-i2c-drv-legacy.h>
37 38
38MODULE_DESCRIPTION("wm8775 driver"); 39MODULE_DESCRIPTION("wm8775 driver");
39MODULE_AUTHOR("Ulf Eklund, Hans Verkuil"); 40MODULE_AUTHOR("Ulf Eklund, Hans Verkuil");
@@ -44,6 +45,7 @@ static unsigned short normal_i2c[] = { 0x36 >> 1, I2C_CLIENT_END };
44 45
45I2C_CLIENT_INSMOD; 46I2C_CLIENT_INSMOD;
46 47
48
47/* ----------------------------------------------------------------------- */ 49/* ----------------------------------------------------------------------- */
48 50
49enum { 51enum {
@@ -66,18 +68,15 @@ static int wm8775_write(struct i2c_client *client, int reg, u16 val)
66 return -1; 68 return -1;
67 } 69 }
68 70
69 for (i = 0; i < 3; i++) { 71 for (i = 0; i < 3; i++)
70 if (i2c_smbus_write_byte_data(client, (reg << 1) | 72 if (i2c_smbus_write_byte_data(client,
71 (val >> 8), val & 0xff) == 0) { 73 (reg << 1) | (val >> 8), val & 0xff) == 0)
72 return 0; 74 return 0;
73 }
74 }
75 v4l_err(client, "I2C: cannot write %03x to register R%d\n", val, reg); 75 v4l_err(client, "I2C: cannot write %03x to register R%d\n", val, reg);
76 return -1; 76 return -1;
77} 77}
78 78
79static int wm8775_command(struct i2c_client *client, unsigned int cmd, 79static int wm8775_command(struct i2c_client *client, unsigned cmd, void *arg)
80 void *arg)
81{ 80{
82 struct wm8775_state *state = i2c_get_clientdata(client); 81 struct wm8775_state *state = i2c_get_clientdata(client);
83 struct v4l2_routing *route = arg; 82 struct v4l2_routing *route = arg;
@@ -126,7 +125,8 @@ static int wm8775_command(struct i2c_client *client, unsigned int cmd,
126 break; 125 break;
127 126
128 case VIDIOC_G_CHIP_IDENT: 127 case VIDIOC_G_CHIP_IDENT:
129 return v4l2_chip_ident_i2c_client(client, arg, V4L2_IDENT_WM8775, 0); 128 return v4l2_chip_ident_i2c_client(client,
129 arg, V4L2_IDENT_WM8775, 0);
130 130
131 case VIDIOC_LOG_STATUS: 131 case VIDIOC_LOG_STATUS:
132 v4l_info(client, "Input: %d%s\n", state->input, 132 v4l_info(client, "Input: %d%s\n", state->input,
@@ -159,105 +159,67 @@ static int wm8775_command(struct i2c_client *client, unsigned int cmd,
159 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1' 159 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
160 */ 160 */
161 161
162static struct i2c_driver i2c_driver; 162static int wm8775_probe(struct i2c_client *client)
163
164static int wm8775_attach(struct i2c_adapter *adapter, int address, int kind)
165{ 163{
166 struct i2c_client *client;
167 struct wm8775_state *state; 164 struct wm8775_state *state;
168 165
169 /* Check if the adapter supports the needed features */ 166 /* Check if the adapter supports the needed features */
170 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 167 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
171 return 0; 168 return -EIO;
172
173 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
174 if (client == 0)
175 return -ENOMEM;
176
177 client->addr = address;
178 client->adapter = adapter;
179 client->driver = &i2c_driver;
180 snprintf(client->name, sizeof(client->name) - 1, "wm8775");
181 169
182 v4l_info(client, "chip found @ 0x%x (%s)\n", address << 1, adapter->name); 170 v4l_info(client, "chip found @ 0x%x (%s)\n",
171 client->addr << 1, client->adapter->name);
183 172
184 state = kmalloc(sizeof(struct wm8775_state), GFP_KERNEL); 173 state = kmalloc(sizeof(struct wm8775_state), GFP_KERNEL);
185 if (state == NULL) { 174 if (state == NULL)
186 kfree(client);
187 return -ENOMEM; 175 return -ENOMEM;
188 }
189 state->input = 2; 176 state->input = 2;
190 state->muted = 0; 177 state->muted = 0;
191 i2c_set_clientdata(client, state); 178 i2c_set_clientdata(client, state);
192 179
193 /* initialize wm8775 */ 180 /* Initialize wm8775 */
194 wm8775_write(client, R23, 0x000); /* RESET */ 181
195 wm8775_write(client, R7, 0x000); /* Disable zero cross detect timeout */ 182 /* RESET */
196 wm8775_write(client, R11, 0x021); /* Left justified, 24-bit mode */ 183 wm8775_write(client, R23, 0x000);
197 wm8775_write(client, R12, 0x102); /* Master mode, clock ratio 256fs */ 184 /* Disable zero cross detect timeout */
198 wm8775_write(client, R13, 0x000); /* Powered up */ 185 wm8775_write(client, R7, 0x000);
199 wm8775_write(client, R14, 0x1d4); /* ADC gain +2.5dB, enable zero cross */ 186 /* Left justified, 24-bit mode */
200 wm8775_write(client, R15, 0x1d4); /* ADC gain +2.5dB, enable zero cross */ 187 wm8775_write(client, R11, 0x021);
201 wm8775_write(client, R16, 0x1bf); /* ALC Stereo, ALC target level -1dB FS */ 188 /* Master mode, clock ratio 256fs */
202 /* max gain +8dB */ 189 wm8775_write(client, R12, 0x102);
203 wm8775_write(client, R17, 0x185); /* Enable gain control, use zero cross */ 190 /* Powered up */
204 /* detection, ALC hold time 42.6 ms */ 191 wm8775_write(client, R13, 0x000);
205 wm8775_write(client, R18, 0x0a2); /* ALC gain ramp up delay 34 s, */ 192 /* ADC gain +2.5dB, enable zero cross */
206 /* ALC gain ramp down delay 33 ms */ 193 wm8775_write(client, R14, 0x1d4);
207 wm8775_write(client, R19, 0x005); /* Enable noise gate, threshold -72dBfs */ 194 /* ADC gain +2.5dB, enable zero cross */
208 wm8775_write(client, R20, 0x07a); /* Transient window 4ms, lower PGA gain */ 195 wm8775_write(client, R15, 0x1d4);
209 /* limit -1dB */ 196 /* ALC Stereo, ALC target level -1dB FS max gain +8dB */
210 wm8775_write(client, R21, 0x102); /* LRBOTH = 1, use input 2. */ 197 wm8775_write(client, R16, 0x1bf);
211 i2c_attach_client(client); 198 /* Enable gain control, use zero cross detection,
212 199 ALC hold time 42.6 ms */
200 wm8775_write(client, R17, 0x185);
201 /* ALC gain ramp up delay 34 s, ALC gain ramp down delay 33 ms */
202 wm8775_write(client, R18, 0x0a2);
203 /* Enable noise gate, threshold -72dBfs */
204 wm8775_write(client, R19, 0x005);
205 /* Transient window 4ms, lower PGA gain limit -1dB */
206 wm8775_write(client, R20, 0x07a);
207 /* LRBOTH = 1, use input 2. */
208 wm8775_write(client, R21, 0x102);
213 return 0; 209 return 0;
214} 210}
215 211
216static int wm8775_probe(struct i2c_adapter *adapter) 212static int wm8775_remove(struct i2c_client *client)
217{ 213{
218 if (adapter->class & I2C_CLASS_TV_ANALOG) 214 kfree(i2c_get_clientdata(client));
219 return i2c_probe(adapter, &addr_data, wm8775_attach);
220 return 0; 215 return 0;
221} 216}
222 217
223static int wm8775_detach(struct i2c_client *client) 218static struct v4l2_i2c_driver_data v4l2_i2c_data = {
224{ 219 .name = "wm8775",
225 struct wm8775_state *state = i2c_get_clientdata(client); 220 .driverid = I2C_DRIVERID_WM8775,
226 int err; 221 .command = wm8775_command,
227 222 .probe = wm8775_probe,
228 err = i2c_detach_client(client); 223 .remove = wm8775_remove,
229 if (err) {
230 return err;
231 }
232 kfree(state);
233 kfree(client);
234
235 return 0;
236}
237
238/* ----------------------------------------------------------------------- */
239
240/* i2c implementation */
241static struct i2c_driver i2c_driver = {
242 .driver = {
243 .name = "wm8775",
244 },
245 .id = I2C_DRIVERID_WM8775,
246 .attach_adapter = wm8775_probe,
247 .detach_client = wm8775_detach,
248 .command = wm8775_command,
249}; 224};
250 225
251
252static int __init wm8775_init_module(void)
253{
254 return i2c_add_driver(&i2c_driver);
255}
256
257static void __exit wm8775_cleanup_module(void)
258{
259 i2c_del_driver(&i2c_driver);
260}
261
262module_init(wm8775_init_module);
263module_exit(wm8775_cleanup_module);
diff --git a/drivers/media/video/zr364xx.c b/drivers/media/video/zr364xx.c
index 6f1892585cbb..1fdbb46de7f3 100644
--- a/drivers/media/video/zr364xx.c
+++ b/drivers/media/video/zr364xx.c
@@ -749,7 +749,7 @@ static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
749} 749}
750 750
751 751
752static struct file_operations zr364xx_fops = { 752static const struct file_operations zr364xx_fops = {
753 .owner = THIS_MODULE, 753 .owner = THIS_MODULE,
754 .open = zr364xx_open, 754 .open = zr364xx_open,
755 .release = zr364xx_release, 755 .release = zr364xx_release,
diff --git a/drivers/net/mlx4/fw.c b/drivers/net/mlx4/fw.c
index 50648738d679..535a4461d88c 100644
--- a/drivers/net/mlx4/fw.c
+++ b/drivers/net/mlx4/fw.c
@@ -202,7 +202,7 @@ int mlx4_QUERY_DEV_CAP(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
202 MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_EQ_OFFSET); 202 MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_EQ_OFFSET);
203 dev_cap->reserved_eqs = 1 << (field & 0xf); 203 dev_cap->reserved_eqs = 1 << (field & 0xf);
204 MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_EQ_OFFSET); 204 MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_EQ_OFFSET);
205 dev_cap->max_eqs = 1 << (field & 0x7); 205 dev_cap->max_eqs = 1 << (field & 0xf);
206 MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_MTT_OFFSET); 206 MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_MTT_OFFSET);
207 dev_cap->reserved_mtts = 1 << (field >> 4); 207 dev_cap->reserved_mtts = 1 << (field >> 4);
208 MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_MRW_SZ_OFFSET); 208 MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_MRW_SZ_OFFSET);
diff --git a/drivers/s390/char/sclp_config.c b/drivers/s390/char/sclp_config.c
index 5322e5e54a98..9dc77f14fa52 100644
--- a/drivers/s390/char/sclp_config.c
+++ b/drivers/s390/char/sclp_config.c
@@ -29,12 +29,12 @@ static void sclp_cpu_capability_notify(struct work_struct *work)
29 struct sys_device *sysdev; 29 struct sys_device *sysdev;
30 30
31 printk(KERN_WARNING TAG "cpu capability changed.\n"); 31 printk(KERN_WARNING TAG "cpu capability changed.\n");
32 lock_cpu_hotplug(); 32 get_online_cpus();
33 for_each_online_cpu(cpu) { 33 for_each_online_cpu(cpu) {
34 sysdev = get_cpu_sysdev(cpu); 34 sysdev = get_cpu_sysdev(cpu);
35 kobject_uevent(&sysdev->kobj, KOBJ_CHANGE); 35 kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
36 } 36 }
37 unlock_cpu_hotplug(); 37 put_online_cpus();
38} 38}
39 39
40static void sclp_conf_receiver_fn(struct evbuf_header *evbuf) 40static void sclp_conf_receiver_fn(struct evbuf_header *evbuf)
diff --git a/drivers/scsi/ide-scsi.c b/drivers/scsi/ide-scsi.c
index 9706de9d98d5..02e91893064d 100644
--- a/drivers/scsi/ide-scsi.c
+++ b/drivers/scsi/ide-scsi.c
@@ -395,14 +395,12 @@ static int idescsi_expiry(ide_drive_t *drive)
395static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive) 395static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
396{ 396{
397 idescsi_scsi_t *scsi = drive_to_idescsi(drive); 397 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
398 idescsi_pc_t *pc=scsi->pc; 398 ide_hwif_t *hwif = drive->hwif;
399 idescsi_pc_t *pc = scsi->pc;
399 struct request *rq = pc->rq; 400 struct request *rq = pc->rq;
400 atapi_bcount_t bcount;
401 atapi_status_t status;
402 atapi_ireason_t ireason;
403 atapi_feature_t feature;
404
405 unsigned int temp; 401 unsigned int temp;
402 u16 bcount;
403 u8 stat, ireason;
406 404
407#if IDESCSI_DEBUG_LOG 405#if IDESCSI_DEBUG_LOG
408 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n"); 406 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
@@ -425,30 +423,29 @@ static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
425 (void) HWIF(drive)->ide_dma_end(drive); 423 (void) HWIF(drive)->ide_dma_end(drive);
426 } 424 }
427 425
428 feature.all = 0;
429 /* Clear the interrupt */ 426 /* Clear the interrupt */
430 status.all = HWIF(drive)->INB(IDE_STATUS_REG); 427 stat = drive->hwif->INB(IDE_STATUS_REG);
431 428
432 if (!status.b.drq) { 429 if ((stat & DRQ_STAT) == 0) {
433 /* No more interrupts */ 430 /* No more interrupts */
434 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) 431 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
435 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred); 432 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
436 local_irq_enable_in_hardirq(); 433 local_irq_enable_in_hardirq();
437 if (status.b.check) 434 if (stat & ERR_STAT)
438 rq->errors++; 435 rq->errors++;
439 idescsi_end_request (drive, 1, 0); 436 idescsi_end_request (drive, 1, 0);
440 return ide_stopped; 437 return ide_stopped;
441 } 438 }
442 bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG); 439 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
443 bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG); 440 hwif->INB(IDE_BCOUNTL_REG);
444 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG); 441 ireason = hwif->INB(IDE_IREASON_REG);
445 442
446 if (ireason.b.cod) { 443 if (ireason & CD) {
447 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n"); 444 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
448 return ide_do_reset (drive); 445 return ide_do_reset (drive);
449 } 446 }
450 if (ireason.b.io) { 447 if (ireason & IO) {
451 temp = pc->actually_transferred + bcount.all; 448 temp = pc->actually_transferred + bcount;
452 if (temp > pc->request_transfer) { 449 if (temp > pc->request_transfer) {
453 if (temp > pc->buffer_size) { 450 if (temp > pc->buffer_size) {
454 printk(KERN_ERR "ide-scsi: The scsi wants to " 451 printk(KERN_ERR "ide-scsi: The scsi wants to "
@@ -461,11 +458,13 @@ static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
461 idescsi_input_buffers(drive, pc, temp); 458 idescsi_input_buffers(drive, pc, temp);
462 else 459 else
463 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp); 460 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
464 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all); 461 printk(KERN_ERR "ide-scsi: transferred"
462 " %d of %d bytes\n",
463 temp, bcount);
465 } 464 }
466 pc->actually_transferred += temp; 465 pc->actually_transferred += temp;
467 pc->current_position += temp; 466 pc->current_position += temp;
468 idescsi_discard_data(drive, bcount.all - temp); 467 idescsi_discard_data(drive, bcount - temp);
469 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry); 468 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
470 return ide_started; 469 return ide_started;
471 } 470 }
@@ -474,22 +473,24 @@ static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
474#endif /* IDESCSI_DEBUG_LOG */ 473#endif /* IDESCSI_DEBUG_LOG */
475 } 474 }
476 } 475 }
477 if (ireason.b.io) { 476 if (ireason & IO) {
478 clear_bit(PC_WRITING, &pc->flags); 477 clear_bit(PC_WRITING, &pc->flags);
479 if (pc->sg) 478 if (pc->sg)
480 idescsi_input_buffers(drive, pc, bcount.all); 479 idescsi_input_buffers(drive, pc, bcount);
481 else 480 else
482 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all); 481 hwif->atapi_input_bytes(drive, pc->current_position,
482 bcount);
483 } else { 483 } else {
484 set_bit(PC_WRITING, &pc->flags); 484 set_bit(PC_WRITING, &pc->flags);
485 if (pc->sg) 485 if (pc->sg)
486 idescsi_output_buffers (drive, pc, bcount.all); 486 idescsi_output_buffers(drive, pc, bcount);
487 else 487 else
488 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all); 488 hwif->atapi_output_bytes(drive, pc->current_position,
489 bcount);
489 } 490 }
490 /* Update the current position */ 491 /* Update the current position */
491 pc->actually_transferred += bcount.all; 492 pc->actually_transferred += bcount;
492 pc->current_position += bcount.all; 493 pc->current_position += bcount;
493 494
494 /* And set the interrupt handler again */ 495 /* And set the interrupt handler again */
495 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry); 496 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
@@ -501,16 +502,16 @@ static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
501 ide_hwif_t *hwif = drive->hwif; 502 ide_hwif_t *hwif = drive->hwif;
502 idescsi_scsi_t *scsi = drive_to_idescsi(drive); 503 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
503 idescsi_pc_t *pc = scsi->pc; 504 idescsi_pc_t *pc = scsi->pc;
504 atapi_ireason_t ireason;
505 ide_startstop_t startstop; 505 ide_startstop_t startstop;
506 u8 ireason;
506 507
507 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) { 508 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
508 printk(KERN_ERR "ide-scsi: Strange, packet command " 509 printk(KERN_ERR "ide-scsi: Strange, packet command "
509 "initiated yet DRQ isn't asserted\n"); 510 "initiated yet DRQ isn't asserted\n");
510 return startstop; 511 return startstop;
511 } 512 }
512 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG); 513 ireason = hwif->INB(IDE_IREASON_REG);
513 if (!ireason.b.cod || ireason.b.io) { 514 if ((ireason & CD) == 0 || (ireason & IO)) {
514 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while " 515 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
515 "issuing a packet command\n"); 516 "issuing a packet command\n");
516 return ide_do_reset (drive); 517 return ide_do_reset (drive);
@@ -573,30 +574,26 @@ static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
573{ 574{
574 idescsi_scsi_t *scsi = drive_to_idescsi(drive); 575 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
575 ide_hwif_t *hwif = drive->hwif; 576 ide_hwif_t *hwif = drive->hwif;
576 atapi_feature_t feature; 577 u16 bcount;
577 atapi_bcount_t bcount; 578 u8 dma = 0;
578 579
579 scsi->pc=pc; /* Set the current packet command */ 580 scsi->pc=pc; /* Set the current packet command */
580 pc->actually_transferred=0; /* We haven't transferred any data yet */ 581 pc->actually_transferred=0; /* We haven't transferred any data yet */
581 pc->current_position=pc->buffer; 582 pc->current_position=pc->buffer;
582 bcount.all = min(pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */ 583 /* Request to transfer the entire buffer at once */
584 bcount = min(pc->request_transfer, 63 * 1024);
583 585
584 feature.all = 0;
585 if (drive->using_dma && !idescsi_map_sg(drive, pc)) { 586 if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
586 hwif->sg_mapped = 1; 587 hwif->sg_mapped = 1;
587 feature.b.dma = !hwif->dma_setup(drive); 588 dma = !hwif->dma_setup(drive);
588 hwif->sg_mapped = 0; 589 hwif->sg_mapped = 0;
589 } 590 }
590 591
591 SELECT_DRIVE(drive); 592 SELECT_DRIVE(drive);
592 if (IDE_CONTROL_REG)
593 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
594 593
595 HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG); 594 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK, bcount, dma);
596 HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
597 HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
598 595
599 if (feature.b.dma) 596 if (dma)
600 set_bit(PC_DMA_OK, &pc->flags); 597 set_bit(PC_DMA_OK, &pc->flags);
601 598
602 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) { 599 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
diff --git a/drivers/scsi/ipr.c b/drivers/scsi/ipr.c
index 0841df01bc19..aa0df0a4b22a 100644
--- a/drivers/scsi/ipr.c
+++ b/drivers/scsi/ipr.c
@@ -5142,6 +5142,7 @@ static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
5142 struct ipr_ioadl_desc *last_ioadl = NULL; 5142 struct ipr_ioadl_desc *last_ioadl = NULL;
5143 int len = qc->nbytes + qc->pad_len; 5143 int len = qc->nbytes + qc->pad_len;
5144 struct scatterlist *sg; 5144 struct scatterlist *sg;
5145 unsigned int si;
5145 5146
5146 if (len == 0) 5147 if (len == 0)
5147 return; 5148 return;
@@ -5159,7 +5160,7 @@ static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
5159 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg); 5160 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5160 } 5161 }
5161 5162
5162 ata_for_each_sg(sg, qc) { 5163 for_each_sg(qc->sg, sg, qc->n_elem, si) {
5163 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg)); 5164 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5164 ioadl->address = cpu_to_be32(sg_dma_address(sg)); 5165 ioadl->address = cpu_to_be32(sg_dma_address(sg));
5165 5166
@@ -5222,12 +5223,12 @@ static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
5222 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA; 5223 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5223 break; 5224 break;
5224 5225
5225 case ATA_PROT_ATAPI: 5226 case ATAPI_PROT_PIO:
5226 case ATA_PROT_ATAPI_NODATA: 5227 case ATAPI_PROT_NODATA:
5227 regs->flags |= IPR_ATA_FLAG_PACKET_CMD; 5228 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5228 break; 5229 break;
5229 5230
5230 case ATA_PROT_ATAPI_DMA: 5231 case ATAPI_PROT_DMA:
5231 regs->flags |= IPR_ATA_FLAG_PACKET_CMD; 5232 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5232 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA; 5233 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5233 break; 5234 break;
diff --git a/drivers/scsi/libsas/sas_ata.c b/drivers/scsi/libsas/sas_ata.c
index 0829b55c64d2..827cfb132f21 100644
--- a/drivers/scsi/libsas/sas_ata.c
+++ b/drivers/scsi/libsas/sas_ata.c
@@ -158,8 +158,8 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
158 struct Scsi_Host *host = sas_ha->core.shost; 158 struct Scsi_Host *host = sas_ha->core.shost;
159 struct sas_internal *i = to_sas_internal(host->transportt); 159 struct sas_internal *i = to_sas_internal(host->transportt);
160 struct scatterlist *sg; 160 struct scatterlist *sg;
161 unsigned int num = 0;
162 unsigned int xfer = 0; 161 unsigned int xfer = 0;
162 unsigned int si;
163 163
164 task = sas_alloc_task(GFP_ATOMIC); 164 task = sas_alloc_task(GFP_ATOMIC);
165 if (!task) 165 if (!task)
@@ -176,22 +176,20 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
176 176
177 ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis); 177 ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
178 task->uldd_task = qc; 178 task->uldd_task = qc;
179 if (is_atapi_taskfile(&qc->tf)) { 179 if (ata_is_atapi(qc->tf.protocol)) {
180 memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len); 180 memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
181 task->total_xfer_len = qc->nbytes + qc->pad_len; 181 task->total_xfer_len = qc->nbytes + qc->pad_len;
182 task->num_scatter = qc->pad_len ? qc->n_elem + 1 : qc->n_elem; 182 task->num_scatter = qc->pad_len ? qc->n_elem + 1 : qc->n_elem;
183 } else { 183 } else {
184 ata_for_each_sg(sg, qc) { 184 for_each_sg(qc->sg, sg, qc->n_elem, si)
185 num++;
186 xfer += sg->length; 185 xfer += sg->length;
187 }
188 186
189 task->total_xfer_len = xfer; 187 task->total_xfer_len = xfer;
190 task->num_scatter = num; 188 task->num_scatter = si;
191 } 189 }
192 190
193 task->data_dir = qc->dma_dir; 191 task->data_dir = qc->dma_dir;
194 task->scatter = qc->__sg; 192 task->scatter = qc->sg;
195 task->ata_task.retry_count = 1; 193 task->ata_task.retry_count = 1;
196 task->task_state_flags = SAS_TASK_STATE_PENDING; 194 task->task_state_flags = SAS_TASK_STATE_PENDING;
197 qc->lldd_task = task; 195 qc->lldd_task = task;
@@ -200,7 +198,7 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
200 case ATA_PROT_NCQ: 198 case ATA_PROT_NCQ:
201 task->ata_task.use_ncq = 1; 199 task->ata_task.use_ncq = 1;
202 /* fall through */ 200 /* fall through */
203 case ATA_PROT_ATAPI_DMA: 201 case ATAPI_PROT_DMA:
204 case ATA_PROT_DMA: 202 case ATA_PROT_DMA:
205 task->ata_task.dma_xfer = 1; 203 task->ata_task.dma_xfer = 1;
206 break; 204 break;
diff --git a/fs/Kconfig b/fs/Kconfig
index 781b47d2f9f2..b6df18f1f677 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -440,14 +440,8 @@ config OCFS2_FS
440 Tools web page: http://oss.oracle.com/projects/ocfs2-tools 440 Tools web page: http://oss.oracle.com/projects/ocfs2-tools
441 OCFS2 mailing lists: http://oss.oracle.com/projects/ocfs2/mailman/ 441 OCFS2 mailing lists: http://oss.oracle.com/projects/ocfs2/mailman/
442 442
443 Note: Features which OCFS2 does not support yet: 443 For more information on OCFS2, see the file
444 - extended attributes 444 <file:Documentation/filesystems/ocfs2.txt>.
445 - quotas
446 - cluster aware flock
447 - Directory change notification (F_NOTIFY)
448 - Distributed Caching (F_SETLEASE/F_GETLEASE/break_lease)
449 - POSIX ACLs
450 - readpages / writepages (not user visible)
451 445
452config OCFS2_DEBUG_MASKLOG 446config OCFS2_DEBUG_MASKLOG
453 bool "OCFS2 logging support" 447 bool "OCFS2 logging support"
@@ -1028,8 +1022,8 @@ config HUGETLB_PAGE
1028 def_bool HUGETLBFS 1022 def_bool HUGETLBFS
1029 1023
1030config CONFIGFS_FS 1024config CONFIGFS_FS
1031 tristate "Userspace-driven configuration filesystem (EXPERIMENTAL)" 1025 tristate "Userspace-driven configuration filesystem"
1032 depends on SYSFS && EXPERIMENTAL 1026 depends on SYSFS
1033 help 1027 help
1034 configfs is a ram-based filesystem that provides the converse 1028 configfs is a ram-based filesystem that provides the converse
1035 of sysfs's functionality. Where sysfs is a filesystem-based 1029 of sysfs's functionality. Where sysfs is a filesystem-based
@@ -2130,4 +2124,3 @@ source "fs/nls/Kconfig"
2130source "fs/dlm/Kconfig" 2124source "fs/dlm/Kconfig"
2131 2125
2132endmenu 2126endmenu
2133
diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
index 50ed691098bc..a48dc7dd8765 100644
--- a/fs/configfs/dir.c
+++ b/fs/configfs/dir.c
@@ -546,7 +546,7 @@ static int populate_groups(struct config_group *group)
546 * That said, taking our i_mutex is closer to mkdir 546 * That said, taking our i_mutex is closer to mkdir
547 * emulation, and shouldn't hurt. 547 * emulation, and shouldn't hurt.
548 */ 548 */
549 mutex_lock(&dentry->d_inode->i_mutex); 549 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
550 550
551 for (i = 0; group->default_groups[i]; i++) { 551 for (i = 0; group->default_groups[i]; i++) {
552 new_group = group->default_groups[i]; 552 new_group = group->default_groups[i];
@@ -1405,7 +1405,8 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys)
1405 sd = configfs_sb->s_root->d_fsdata; 1405 sd = configfs_sb->s_root->d_fsdata;
1406 link_group(to_config_group(sd->s_element), group); 1406 link_group(to_config_group(sd->s_element), group);
1407 1407
1408 mutex_lock(&configfs_sb->s_root->d_inode->i_mutex); 1408 mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1409 I_MUTEX_PARENT);
1409 1410
1410 name.name = group->cg_item.ci_name; 1411 name.name = group->cg_item.ci_name;
1411 name.len = strlen(name.name); 1412 name.len = strlen(name.name);
diff --git a/fs/configfs/file.c b/fs/configfs/file.c
index a3658f9a082c..397cb503a180 100644
--- a/fs/configfs/file.c
+++ b/fs/configfs/file.c
@@ -320,7 +320,7 @@ int configfs_add_file(struct dentry * dir, const struct configfs_attribute * att
320 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG; 320 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
321 int error = 0; 321 int error = 0;
322 322
323 mutex_lock(&dir->d_inode->i_mutex); 323 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_NORMAL);
324 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type); 324 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type);
325 mutex_unlock(&dir->d_inode->i_mutex); 325 mutex_unlock(&dir->d_inode->i_mutex);
326 326
diff --git a/fs/ocfs2/Makefile b/fs/ocfs2/Makefile
index 9fb8132f19b0..4d4ce48bb42c 100644
--- a/fs/ocfs2/Makefile
+++ b/fs/ocfs2/Makefile
@@ -19,16 +19,17 @@ ocfs2-objs := \
19 ioctl.o \ 19 ioctl.o \
20 journal.o \ 20 journal.o \
21 localalloc.o \ 21 localalloc.o \
22 locks.o \
22 mmap.o \ 23 mmap.o \
23 namei.o \ 24 namei.o \
25 resize.o \
24 slot_map.o \ 26 slot_map.o \
25 suballoc.o \ 27 suballoc.o \
26 super.o \ 28 super.o \
27 symlink.o \ 29 symlink.o \
28 sysfile.o \ 30 sysfile.o \
29 uptodate.o \ 31 uptodate.o \
30 ver.o \ 32 ver.o
31 vote.o
32 33
33obj-$(CONFIG_OCFS2_FS) += cluster/ 34obj-$(CONFIG_OCFS2_FS) += cluster/
34obj-$(CONFIG_OCFS2_FS) += dlm/ 35obj-$(CONFIG_OCFS2_FS) += dlm/
diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c
index 23c8cda43f19..e6df06ac6405 100644
--- a/fs/ocfs2/alloc.c
+++ b/fs/ocfs2/alloc.c
@@ -4731,7 +4731,7 @@ int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
4731 4731
4732 mutex_lock(&data_alloc_inode->i_mutex); 4732 mutex_lock(&data_alloc_inode->i_mutex);
4733 4733
4734 status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1); 4734 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
4735 if (status < 0) { 4735 if (status < 0) {
4736 mlog_errno(status); 4736 mlog_errno(status);
4737 goto out_mutex; 4737 goto out_mutex;
@@ -4753,7 +4753,7 @@ int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
4753 4753
4754out_unlock: 4754out_unlock:
4755 brelse(data_alloc_bh); 4755 brelse(data_alloc_bh);
4756 ocfs2_meta_unlock(data_alloc_inode, 1); 4756 ocfs2_inode_unlock(data_alloc_inode, 1);
4757 4757
4758out_mutex: 4758out_mutex:
4759 mutex_unlock(&data_alloc_inode->i_mutex); 4759 mutex_unlock(&data_alloc_inode->i_mutex);
@@ -5077,7 +5077,7 @@ static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5077 5077
5078 mutex_lock(&inode->i_mutex); 5078 mutex_lock(&inode->i_mutex);
5079 5079
5080 ret = ocfs2_meta_lock(inode, &di_bh, 1); 5080 ret = ocfs2_inode_lock(inode, &di_bh, 1);
5081 if (ret) { 5081 if (ret) {
5082 mlog_errno(ret); 5082 mlog_errno(ret);
5083 goto out_mutex; 5083 goto out_mutex;
@@ -5118,7 +5118,7 @@ out_journal:
5118 ocfs2_commit_trans(osb, handle); 5118 ocfs2_commit_trans(osb, handle);
5119 5119
5120out_unlock: 5120out_unlock:
5121 ocfs2_meta_unlock(inode, 1); 5121 ocfs2_inode_unlock(inode, 1);
5122 brelse(di_bh); 5122 brelse(di_bh);
5123out_mutex: 5123out_mutex:
5124 mutex_unlock(&inode->i_mutex); 5124 mutex_unlock(&inode->i_mutex);
diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
index 56f7790cad46..bc7b4cbbe8ec 100644
--- a/fs/ocfs2/aops.c
+++ b/fs/ocfs2/aops.c
@@ -26,6 +26,7 @@
26#include <asm/byteorder.h> 26#include <asm/byteorder.h>
27#include <linux/swap.h> 27#include <linux/swap.h>
28#include <linux/pipe_fs_i.h> 28#include <linux/pipe_fs_i.h>
29#include <linux/mpage.h>
29 30
30#define MLOG_MASK_PREFIX ML_FILE_IO 31#define MLOG_MASK_PREFIX ML_FILE_IO
31#include <cluster/masklog.h> 32#include <cluster/masklog.h>
@@ -139,7 +140,8 @@ static int ocfs2_get_block(struct inode *inode, sector_t iblock,
139{ 140{
140 int err = 0; 141 int err = 0;
141 unsigned int ext_flags; 142 unsigned int ext_flags;
142 u64 p_blkno, past_eof; 143 u64 max_blocks = bh_result->b_size >> inode->i_blkbits;
144 u64 p_blkno, count, past_eof;
143 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 145 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
144 146
145 mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode, 147 mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
@@ -155,7 +157,7 @@ static int ocfs2_get_block(struct inode *inode, sector_t iblock,
155 goto bail; 157 goto bail;
156 } 158 }
157 159
158 err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, NULL, 160 err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, &count,
159 &ext_flags); 161 &ext_flags);
160 if (err) { 162 if (err) {
161 mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, " 163 mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
@@ -164,6 +166,9 @@ static int ocfs2_get_block(struct inode *inode, sector_t iblock,
164 goto bail; 166 goto bail;
165 } 167 }
166 168
169 if (max_blocks < count)
170 count = max_blocks;
171
167 /* 172 /*
168 * ocfs2 never allocates in this function - the only time we 173 * ocfs2 never allocates in this function - the only time we
169 * need to use BH_New is when we're extending i_size on a file 174 * need to use BH_New is when we're extending i_size on a file
@@ -178,6 +183,8 @@ static int ocfs2_get_block(struct inode *inode, sector_t iblock,
178 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN)) 183 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
179 map_bh(bh_result, inode->i_sb, p_blkno); 184 map_bh(bh_result, inode->i_sb, p_blkno);
180 185
186 bh_result->b_size = count << inode->i_blkbits;
187
181 if (!ocfs2_sparse_alloc(osb)) { 188 if (!ocfs2_sparse_alloc(osb)) {
182 if (p_blkno == 0) { 189 if (p_blkno == 0) {
183 err = -EIO; 190 err = -EIO;
@@ -210,7 +217,7 @@ int ocfs2_read_inline_data(struct inode *inode, struct page *page,
210 struct buffer_head *di_bh) 217 struct buffer_head *di_bh)
211{ 218{
212 void *kaddr; 219 void *kaddr;
213 unsigned int size; 220 loff_t size;
214 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 221 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
215 222
216 if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)) { 223 if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)) {
@@ -224,8 +231,9 @@ int ocfs2_read_inline_data(struct inode *inode, struct page *page,
224 if (size > PAGE_CACHE_SIZE || 231 if (size > PAGE_CACHE_SIZE ||
225 size > ocfs2_max_inline_data(inode->i_sb)) { 232 size > ocfs2_max_inline_data(inode->i_sb)) {
226 ocfs2_error(inode->i_sb, 233 ocfs2_error(inode->i_sb,
227 "Inode %llu has with inline data has bad size: %u", 234 "Inode %llu has with inline data has bad size: %Lu",
228 (unsigned long long)OCFS2_I(inode)->ip_blkno, size); 235 (unsigned long long)OCFS2_I(inode)->ip_blkno,
236 (unsigned long long)size);
229 return -EROFS; 237 return -EROFS;
230 } 238 }
231 239
@@ -275,7 +283,7 @@ static int ocfs2_readpage(struct file *file, struct page *page)
275 283
276 mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0)); 284 mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
277 285
278 ret = ocfs2_meta_lock_with_page(inode, NULL, 0, page); 286 ret = ocfs2_inode_lock_with_page(inode, NULL, 0, page);
279 if (ret != 0) { 287 if (ret != 0) {
280 if (ret == AOP_TRUNCATED_PAGE) 288 if (ret == AOP_TRUNCATED_PAGE)
281 unlock = 0; 289 unlock = 0;
@@ -285,7 +293,7 @@ static int ocfs2_readpage(struct file *file, struct page *page)
285 293
286 if (down_read_trylock(&oi->ip_alloc_sem) == 0) { 294 if (down_read_trylock(&oi->ip_alloc_sem) == 0) {
287 ret = AOP_TRUNCATED_PAGE; 295 ret = AOP_TRUNCATED_PAGE;
288 goto out_meta_unlock; 296 goto out_inode_unlock;
289 } 297 }
290 298
291 /* 299 /*
@@ -305,25 +313,16 @@ static int ocfs2_readpage(struct file *file, struct page *page)
305 goto out_alloc; 313 goto out_alloc;
306 } 314 }
307 315
308 ret = ocfs2_data_lock_with_page(inode, 0, page);
309 if (ret != 0) {
310 if (ret == AOP_TRUNCATED_PAGE)
311 unlock = 0;
312 mlog_errno(ret);
313 goto out_alloc;
314 }
315
316 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) 316 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
317 ret = ocfs2_readpage_inline(inode, page); 317 ret = ocfs2_readpage_inline(inode, page);
318 else 318 else
319 ret = block_read_full_page(page, ocfs2_get_block); 319 ret = block_read_full_page(page, ocfs2_get_block);
320 unlock = 0; 320 unlock = 0;
321 321
322 ocfs2_data_unlock(inode, 0);
323out_alloc: 322out_alloc:
324 up_read(&OCFS2_I(inode)->ip_alloc_sem); 323 up_read(&OCFS2_I(inode)->ip_alloc_sem);
325out_meta_unlock: 324out_inode_unlock:
326 ocfs2_meta_unlock(inode, 0); 325 ocfs2_inode_unlock(inode, 0);
327out: 326out:
328 if (unlock) 327 if (unlock)
329 unlock_page(page); 328 unlock_page(page);
@@ -331,6 +330,62 @@ out:
331 return ret; 330 return ret;
332} 331}
333 332
333/*
334 * This is used only for read-ahead. Failures or difficult to handle
335 * situations are safe to ignore.
336 *
337 * Right now, we don't bother with BH_Boundary - in-inode extent lists
338 * are quite large (243 extents on 4k blocks), so most inodes don't
339 * grow out to a tree. If need be, detecting boundary extents could
340 * trivially be added in a future version of ocfs2_get_block().
341 */
342static int ocfs2_readpages(struct file *filp, struct address_space *mapping,
343 struct list_head *pages, unsigned nr_pages)
344{
345 int ret, err = -EIO;
346 struct inode *inode = mapping->host;
347 struct ocfs2_inode_info *oi = OCFS2_I(inode);
348 loff_t start;
349 struct page *last;
350
351 /*
352 * Use the nonblocking flag for the dlm code to avoid page
353 * lock inversion, but don't bother with retrying.
354 */
355 ret = ocfs2_inode_lock_full(inode, NULL, 0, OCFS2_LOCK_NONBLOCK);
356 if (ret)
357 return err;
358
359 if (down_read_trylock(&oi->ip_alloc_sem) == 0) {
360 ocfs2_inode_unlock(inode, 0);
361 return err;
362 }
363
364 /*
365 * Don't bother with inline-data. There isn't anything
366 * to read-ahead in that case anyway...
367 */
368 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
369 goto out_unlock;
370
371 /*
372 * Check whether a remote node truncated this file - we just
373 * drop out in that case as it's not worth handling here.
374 */
375 last = list_entry(pages->prev, struct page, lru);
376 start = (loff_t)last->index << PAGE_CACHE_SHIFT;
377 if (start >= i_size_read(inode))
378 goto out_unlock;
379
380 err = mpage_readpages(mapping, pages, nr_pages, ocfs2_get_block);
381
382out_unlock:
383 up_read(&oi->ip_alloc_sem);
384 ocfs2_inode_unlock(inode, 0);
385
386 return err;
387}
388
334/* Note: Because we don't support holes, our allocation has 389/* Note: Because we don't support holes, our allocation has
335 * already happened (allocation writes zeros to the file data) 390 * already happened (allocation writes zeros to the file data)
336 * so we don't have to worry about ordered writes in 391 * so we don't have to worry about ordered writes in
@@ -452,7 +507,7 @@ static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
452 * accessed concurrently from multiple nodes. 507 * accessed concurrently from multiple nodes.
453 */ 508 */
454 if (!INODE_JOURNAL(inode)) { 509 if (!INODE_JOURNAL(inode)) {
455 err = ocfs2_meta_lock(inode, NULL, 0); 510 err = ocfs2_inode_lock(inode, NULL, 0);
456 if (err) { 511 if (err) {
457 if (err != -ENOENT) 512 if (err != -ENOENT)
458 mlog_errno(err); 513 mlog_errno(err);
@@ -467,7 +522,7 @@ static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
467 522
468 if (!INODE_JOURNAL(inode)) { 523 if (!INODE_JOURNAL(inode)) {
469 up_read(&OCFS2_I(inode)->ip_alloc_sem); 524 up_read(&OCFS2_I(inode)->ip_alloc_sem);
470 ocfs2_meta_unlock(inode, 0); 525 ocfs2_inode_unlock(inode, 0);
471 } 526 }
472 527
473 if (err) { 528 if (err) {
@@ -638,34 +693,12 @@ static ssize_t ocfs2_direct_IO(int rw,
638 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) 693 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
639 return 0; 694 return 0;
640 695
641 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
642 /*
643 * We get PR data locks even for O_DIRECT. This
644 * allows concurrent O_DIRECT I/O but doesn't let
645 * O_DIRECT with extending and buffered zeroing writes
646 * race. If they did race then the buffered zeroing
647 * could be written back after the O_DIRECT I/O. It's
648 * one thing to tell people not to mix buffered and
649 * O_DIRECT writes, but expecting them to understand
650 * that file extension is also an implicit buffered
651 * write is too much. By getting the PR we force
652 * writeback of the buffered zeroing before
653 * proceeding.
654 */
655 ret = ocfs2_data_lock(inode, 0);
656 if (ret < 0) {
657 mlog_errno(ret);
658 goto out;
659 }
660 ocfs2_data_unlock(inode, 0);
661 }
662
663 ret = blockdev_direct_IO_no_locking(rw, iocb, inode, 696 ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
664 inode->i_sb->s_bdev, iov, offset, 697 inode->i_sb->s_bdev, iov, offset,
665 nr_segs, 698 nr_segs,
666 ocfs2_direct_IO_get_blocks, 699 ocfs2_direct_IO_get_blocks,
667 ocfs2_dio_end_io); 700 ocfs2_dio_end_io);
668out: 701
669 mlog_exit(ret); 702 mlog_exit(ret);
670 return ret; 703 return ret;
671} 704}
@@ -1754,7 +1787,7 @@ static int ocfs2_write_begin(struct file *file, struct address_space *mapping,
1754 struct buffer_head *di_bh = NULL; 1787 struct buffer_head *di_bh = NULL;
1755 struct inode *inode = mapping->host; 1788 struct inode *inode = mapping->host;
1756 1789
1757 ret = ocfs2_meta_lock(inode, &di_bh, 1); 1790 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1758 if (ret) { 1791 if (ret) {
1759 mlog_errno(ret); 1792 mlog_errno(ret);
1760 return ret; 1793 return ret;
@@ -1769,30 +1802,22 @@ static int ocfs2_write_begin(struct file *file, struct address_space *mapping,
1769 */ 1802 */
1770 down_write(&OCFS2_I(inode)->ip_alloc_sem); 1803 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1771 1804
1772 ret = ocfs2_data_lock(inode, 1);
1773 if (ret) {
1774 mlog_errno(ret);
1775 goto out_fail;
1776 }
1777
1778 ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep, 1805 ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
1779 fsdata, di_bh, NULL); 1806 fsdata, di_bh, NULL);
1780 if (ret) { 1807 if (ret) {
1781 mlog_errno(ret); 1808 mlog_errno(ret);
1782 goto out_fail_data; 1809 goto out_fail;
1783 } 1810 }
1784 1811
1785 brelse(di_bh); 1812 brelse(di_bh);
1786 1813
1787 return 0; 1814 return 0;
1788 1815
1789out_fail_data:
1790 ocfs2_data_unlock(inode, 1);
1791out_fail: 1816out_fail:
1792 up_write(&OCFS2_I(inode)->ip_alloc_sem); 1817 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1793 1818
1794 brelse(di_bh); 1819 brelse(di_bh);
1795 ocfs2_meta_unlock(inode, 1); 1820 ocfs2_inode_unlock(inode, 1);
1796 1821
1797 return ret; 1822 return ret;
1798} 1823}
@@ -1908,15 +1933,15 @@ static int ocfs2_write_end(struct file *file, struct address_space *mapping,
1908 1933
1909 ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata); 1934 ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
1910 1935
1911 ocfs2_data_unlock(inode, 1);
1912 up_write(&OCFS2_I(inode)->ip_alloc_sem); 1936 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1913 ocfs2_meta_unlock(inode, 1); 1937 ocfs2_inode_unlock(inode, 1);
1914 1938
1915 return ret; 1939 return ret;
1916} 1940}
1917 1941
1918const struct address_space_operations ocfs2_aops = { 1942const struct address_space_operations ocfs2_aops = {
1919 .readpage = ocfs2_readpage, 1943 .readpage = ocfs2_readpage,
1944 .readpages = ocfs2_readpages,
1920 .writepage = ocfs2_writepage, 1945 .writepage = ocfs2_writepage,
1921 .write_begin = ocfs2_write_begin, 1946 .write_begin = ocfs2_write_begin,
1922 .write_end = ocfs2_write_end, 1947 .write_end = ocfs2_write_end,
diff --git a/fs/ocfs2/buffer_head_io.c b/fs/ocfs2/buffer_head_io.c
index c9037414f4f6..f136639f5b41 100644
--- a/fs/ocfs2/buffer_head_io.c
+++ b/fs/ocfs2/buffer_head_io.c
@@ -79,7 +79,7 @@ int ocfs2_write_block(struct ocfs2_super *osb, struct buffer_head *bh,
79 * information for this bh as it's not marked locally 79 * information for this bh as it's not marked locally
80 * uptodate. */ 80 * uptodate. */
81 ret = -EIO; 81 ret = -EIO;
82 brelse(bh); 82 put_bh(bh);
83 } 83 }
84 84
85 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex); 85 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
@@ -256,7 +256,7 @@ int ocfs2_read_blocks(struct ocfs2_super *osb, u64 block, int nr,
256 * for this bh as it's not marked locally 256 * for this bh as it's not marked locally
257 * uptodate. */ 257 * uptodate. */
258 status = -EIO; 258 status = -EIO;
259 brelse(bh); 259 put_bh(bh);
260 bhs[i] = NULL; 260 bhs[i] = NULL;
261 continue; 261 continue;
262 } 262 }
@@ -280,3 +280,64 @@ bail:
280 mlog_exit(status); 280 mlog_exit(status);
281 return status; 281 return status;
282} 282}
283
284/* Check whether the blkno is the super block or one of the backups. */
285static void ocfs2_check_super_or_backup(struct super_block *sb,
286 sector_t blkno)
287{
288 int i;
289 u64 backup_blkno;
290
291 if (blkno == OCFS2_SUPER_BLOCK_BLKNO)
292 return;
293
294 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
295 backup_blkno = ocfs2_backup_super_blkno(sb, i);
296 if (backup_blkno == blkno)
297 return;
298 }
299
300 BUG();
301}
302
303/*
304 * Write super block and backups doesn't need to collaborate with journal,
305 * so we don't need to lock ip_io_mutex and inode doesn't need to bea passed
306 * into this function.
307 */
308int ocfs2_write_super_or_backup(struct ocfs2_super *osb,
309 struct buffer_head *bh)
310{
311 int ret = 0;
312
313 mlog_entry_void();
314
315 BUG_ON(buffer_jbd(bh));
316 ocfs2_check_super_or_backup(osb->sb, bh->b_blocknr);
317
318 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) {
319 ret = -EROFS;
320 goto out;
321 }
322
323 lock_buffer(bh);
324 set_buffer_uptodate(bh);
325
326 /* remove from dirty list before I/O. */
327 clear_buffer_dirty(bh);
328
329 get_bh(bh); /* for end_buffer_write_sync() */
330 bh->b_end_io = end_buffer_write_sync;
331 submit_bh(WRITE, bh);
332
333 wait_on_buffer(bh);
334
335 if (!buffer_uptodate(bh)) {
336 ret = -EIO;
337 put_bh(bh);
338 }
339
340out:
341 mlog_exit(ret);
342 return ret;
343}
diff --git a/fs/ocfs2/buffer_head_io.h b/fs/ocfs2/buffer_head_io.h
index 6cc20930fac3..c2e78614c3e5 100644
--- a/fs/ocfs2/buffer_head_io.h
+++ b/fs/ocfs2/buffer_head_io.h
@@ -47,6 +47,8 @@ int ocfs2_read_blocks(struct ocfs2_super *osb,
47 int flags, 47 int flags,
48 struct inode *inode); 48 struct inode *inode);
49 49
50int ocfs2_write_super_or_backup(struct ocfs2_super *osb,
51 struct buffer_head *bh);
50 52
51#define OCFS2_BH_CACHED 1 53#define OCFS2_BH_CACHED 1
52#define OCFS2_BH_READAHEAD 8 54#define OCFS2_BH_READAHEAD 8
diff --git a/fs/ocfs2/cluster/heartbeat.h b/fs/ocfs2/cluster/heartbeat.h
index 35397dd5ecdb..e511339886b3 100644
--- a/fs/ocfs2/cluster/heartbeat.h
+++ b/fs/ocfs2/cluster/heartbeat.h
@@ -35,7 +35,7 @@
35#define O2HB_LIVE_THRESHOLD 2 35#define O2HB_LIVE_THRESHOLD 2
36/* number of equal samples to be seen as dead */ 36/* number of equal samples to be seen as dead */
37extern unsigned int o2hb_dead_threshold; 37extern unsigned int o2hb_dead_threshold;
38#define O2HB_DEFAULT_DEAD_THRESHOLD 7 38#define O2HB_DEFAULT_DEAD_THRESHOLD 31
39/* Otherwise MAX_WRITE_TIMEOUT will be zero... */ 39/* Otherwise MAX_WRITE_TIMEOUT will be zero... */
40#define O2HB_MIN_DEAD_THRESHOLD 2 40#define O2HB_MIN_DEAD_THRESHOLD 2
41#define O2HB_MAX_WRITE_TIMEOUT_MS (O2HB_REGION_TIMEOUT_MS * (o2hb_dead_threshold - 1)) 41#define O2HB_MAX_WRITE_TIMEOUT_MS (O2HB_REGION_TIMEOUT_MS * (o2hb_dead_threshold - 1))
diff --git a/fs/ocfs2/cluster/tcp.h b/fs/ocfs2/cluster/tcp.h
index da880fc215f0..f36f66aab3dd 100644
--- a/fs/ocfs2/cluster/tcp.h
+++ b/fs/ocfs2/cluster/tcp.h
@@ -60,8 +60,8 @@ typedef void (o2net_post_msg_handler_func)(int status, void *data,
60/* same as hb delay, we're waiting for another node to recognize our hb */ 60/* same as hb delay, we're waiting for another node to recognize our hb */
61#define O2NET_RECONNECT_DELAY_MS_DEFAULT 2000 61#define O2NET_RECONNECT_DELAY_MS_DEFAULT 2000
62 62
63#define O2NET_KEEPALIVE_DELAY_MS_DEFAULT 5000 63#define O2NET_KEEPALIVE_DELAY_MS_DEFAULT 2000
64#define O2NET_IDLE_TIMEOUT_MS_DEFAULT 10000 64#define O2NET_IDLE_TIMEOUT_MS_DEFAULT 30000
65 65
66 66
67/* TODO: figure this out.... */ 67/* TODO: figure this out.... */
diff --git a/fs/ocfs2/cluster/tcp_internal.h b/fs/ocfs2/cluster/tcp_internal.h
index 9606111fe89d..b2e832aca567 100644
--- a/fs/ocfs2/cluster/tcp_internal.h
+++ b/fs/ocfs2/cluster/tcp_internal.h
@@ -38,6 +38,12 @@
38 * locking semantics of the file system using the protocol. It should 38 * locking semantics of the file system using the protocol. It should
39 * be somewhere else, I'm sure, but right now it isn't. 39 * be somewhere else, I'm sure, but right now it isn't.
40 * 40 *
41 * New in version 10:
42 * - Meta/data locks combined
43 *
44 * New in version 9:
45 * - All votes removed
46 *
41 * New in version 8: 47 * New in version 8:
42 * - Replace delete inode votes with a cluster lock 48 * - Replace delete inode votes with a cluster lock
43 * 49 *
@@ -60,7 +66,7 @@
60 * - full 64 bit i_size in the metadata lock lvbs 66 * - full 64 bit i_size in the metadata lock lvbs
61 * - introduction of "rw" lock and pushing meta/data locking down 67 * - introduction of "rw" lock and pushing meta/data locking down
62 */ 68 */
63#define O2NET_PROTOCOL_VERSION 8ULL 69#define O2NET_PROTOCOL_VERSION 10ULL
64struct o2net_handshake { 70struct o2net_handshake {
65 __be64 protocol_version; 71 __be64 protocol_version;
66 __be64 connector_id; 72 __be64 connector_id;
diff --git a/fs/ocfs2/cluster/ver.c b/fs/ocfs2/cluster/ver.c
index 7286c48bb30d..a56eee6abad3 100644
--- a/fs/ocfs2/cluster/ver.c
+++ b/fs/ocfs2/cluster/ver.c
@@ -28,7 +28,7 @@
28 28
29#include "ver.h" 29#include "ver.h"
30 30
31#define CLUSTER_BUILD_VERSION "1.3.3" 31#define CLUSTER_BUILD_VERSION "1.5.0"
32 32
33#define VERSION_STR "OCFS2 Node Manager " CLUSTER_BUILD_VERSION 33#define VERSION_STR "OCFS2 Node Manager " CLUSTER_BUILD_VERSION
34 34
diff --git a/fs/ocfs2/dcache.c b/fs/ocfs2/dcache.c
index 9923278ea6d4..b1cc7c381e88 100644
--- a/fs/ocfs2/dcache.c
+++ b/fs/ocfs2/dcache.c
@@ -128,9 +128,9 @@ static int ocfs2_match_dentry(struct dentry *dentry,
128/* 128/*
129 * Walk the inode alias list, and find a dentry which has a given 129 * Walk the inode alias list, and find a dentry which has a given
130 * parent. ocfs2_dentry_attach_lock() wants to find _any_ alias as it 130 * parent. ocfs2_dentry_attach_lock() wants to find _any_ alias as it
131 * is looking for a dentry_lock reference. The vote thread is looking 131 * is looking for a dentry_lock reference. The downconvert thread is
132 * to unhash aliases, so we allow it to skip any that already have 132 * looking to unhash aliases, so we allow it to skip any that already
133 * that property. 133 * have that property.
134 */ 134 */
135struct dentry *ocfs2_find_local_alias(struct inode *inode, 135struct dentry *ocfs2_find_local_alias(struct inode *inode,
136 u64 parent_blkno, 136 u64 parent_blkno,
@@ -266,7 +266,7 @@ int ocfs2_dentry_attach_lock(struct dentry *dentry,
266 dl->dl_count = 0; 266 dl->dl_count = 0;
267 /* 267 /*
268 * Does this have to happen below, for all attaches, in case 268 * Does this have to happen below, for all attaches, in case
269 * the struct inode gets blown away by votes? 269 * the struct inode gets blown away by the downconvert thread?
270 */ 270 */
271 dl->dl_inode = igrab(inode); 271 dl->dl_inode = igrab(inode);
272 dl->dl_parent_blkno = parent_blkno; 272 dl->dl_parent_blkno = parent_blkno;
diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c
index 63b28fdceb4a..6b0107f21344 100644
--- a/fs/ocfs2/dir.c
+++ b/fs/ocfs2/dir.c
@@ -846,14 +846,14 @@ int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
846 mlog_entry("dirino=%llu\n", 846 mlog_entry("dirino=%llu\n",
847 (unsigned long long)OCFS2_I(inode)->ip_blkno); 847 (unsigned long long)OCFS2_I(inode)->ip_blkno);
848 848
849 error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level); 849 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
850 if (lock_level && error >= 0) { 850 if (lock_level && error >= 0) {
851 /* We release EX lock which used to update atime 851 /* We release EX lock which used to update atime
852 * and get PR lock again to reduce contention 852 * and get PR lock again to reduce contention
853 * on commonly accessed directories. */ 853 * on commonly accessed directories. */
854 ocfs2_meta_unlock(inode, 1); 854 ocfs2_inode_unlock(inode, 1);
855 lock_level = 0; 855 lock_level = 0;
856 error = ocfs2_meta_lock(inode, NULL, 0); 856 error = ocfs2_inode_lock(inode, NULL, 0);
857 } 857 }
858 if (error < 0) { 858 if (error < 0) {
859 if (error != -ENOENT) 859 if (error != -ENOENT)
@@ -865,7 +865,7 @@ int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
865 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos, 865 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
866 dirent, filldir, NULL); 866 dirent, filldir, NULL);
867 867
868 ocfs2_meta_unlock(inode, lock_level); 868 ocfs2_inode_unlock(inode, lock_level);
869 869
870bail_nolock: 870bail_nolock:
871 mlog_exit(error); 871 mlog_exit(error);
diff --git a/fs/ocfs2/dlm/dlmfsver.c b/fs/ocfs2/dlm/dlmfsver.c
index d2be3ad841f9..a733b3321f83 100644
--- a/fs/ocfs2/dlm/dlmfsver.c
+++ b/fs/ocfs2/dlm/dlmfsver.c
@@ -28,7 +28,7 @@
28 28
29#include "dlmfsver.h" 29#include "dlmfsver.h"
30 30
31#define DLM_BUILD_VERSION "1.3.3" 31#define DLM_BUILD_VERSION "1.5.0"
32 32
33#define VERSION_STR "OCFS2 DLMFS " DLM_BUILD_VERSION 33#define VERSION_STR "OCFS2 DLMFS " DLM_BUILD_VERSION
34 34
diff --git a/fs/ocfs2/dlm/dlmrecovery.c b/fs/ocfs2/dlm/dlmrecovery.c
index 2fde7bf91434..91f747b8a538 100644
--- a/fs/ocfs2/dlm/dlmrecovery.c
+++ b/fs/ocfs2/dlm/dlmrecovery.c
@@ -2270,6 +2270,12 @@ static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
2270 } 2270 }
2271 } 2271 }
2272 2272
2273 /* Clean up join state on node death. */
2274 if (dlm->joining_node == idx) {
2275 mlog(0, "Clearing join state for node %u\n", idx);
2276 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
2277 }
2278
2273 /* check to see if the node is already considered dead */ 2279 /* check to see if the node is already considered dead */
2274 if (!test_bit(idx, dlm->live_nodes_map)) { 2280 if (!test_bit(idx, dlm->live_nodes_map)) {
2275 mlog(0, "for domain %s, node %d is already dead. " 2281 mlog(0, "for domain %s, node %d is already dead. "
@@ -2288,12 +2294,6 @@ static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
2288 2294
2289 clear_bit(idx, dlm->live_nodes_map); 2295 clear_bit(idx, dlm->live_nodes_map);
2290 2296
2291 /* Clean up join state on node death. */
2292 if (dlm->joining_node == idx) {
2293 mlog(0, "Clearing join state for node %u\n", idx);
2294 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
2295 }
2296
2297 /* make sure local cleanup occurs before the heartbeat events */ 2297 /* make sure local cleanup occurs before the heartbeat events */
2298 if (!test_bit(idx, dlm->recovery_map)) 2298 if (!test_bit(idx, dlm->recovery_map))
2299 dlm_do_local_recovery_cleanup(dlm, idx); 2299 dlm_do_local_recovery_cleanup(dlm, idx);
@@ -2321,6 +2321,13 @@ void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
2321 if (!dlm_grab(dlm)) 2321 if (!dlm_grab(dlm))
2322 return; 2322 return;
2323 2323
2324 /*
2325 * This will notify any dlm users that a node in our domain
2326 * went away without notifying us first.
2327 */
2328 if (test_bit(idx, dlm->domain_map))
2329 dlm_fire_domain_eviction_callbacks(dlm, idx);
2330
2324 spin_lock(&dlm->spinlock); 2331 spin_lock(&dlm->spinlock);
2325 __dlm_hb_node_down(dlm, idx); 2332 __dlm_hb_node_down(dlm, idx);
2326 spin_unlock(&dlm->spinlock); 2333 spin_unlock(&dlm->spinlock);
diff --git a/fs/ocfs2/dlm/dlmver.c b/fs/ocfs2/dlm/dlmver.c
index 7ef2653f8f41..dfc0da4d158d 100644
--- a/fs/ocfs2/dlm/dlmver.c
+++ b/fs/ocfs2/dlm/dlmver.c
@@ -28,7 +28,7 @@
28 28
29#include "dlmver.h" 29#include "dlmver.h"
30 30
31#define DLM_BUILD_VERSION "1.3.3" 31#define DLM_BUILD_VERSION "1.5.0"
32 32
33#define VERSION_STR "OCFS2 DLM " DLM_BUILD_VERSION 33#define VERSION_STR "OCFS2 DLM " DLM_BUILD_VERSION
34 34
diff --git a/fs/ocfs2/dlmglue.c b/fs/ocfs2/dlmglue.c
index 4e97dcceaf8f..3867244fb144 100644
--- a/fs/ocfs2/dlmglue.c
+++ b/fs/ocfs2/dlmglue.c
@@ -55,7 +55,6 @@
55#include "slot_map.h" 55#include "slot_map.h"
56#include "super.h" 56#include "super.h"
57#include "uptodate.h" 57#include "uptodate.h"
58#include "vote.h"
59 58
60#include "buffer_head_io.h" 59#include "buffer_head_io.h"
61 60
@@ -69,6 +68,7 @@ struct ocfs2_mask_waiter {
69 68
70static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres); 69static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
71static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres); 70static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
71static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
72 72
73/* 73/*
74 * Return value from ->downconvert_worker functions. 74 * Return value from ->downconvert_worker functions.
@@ -153,10 +153,10 @@ struct ocfs2_lock_res_ops {
153 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *); 153 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
154 154
155 /* 155 /*
156 * Optionally called in the downconvert (or "vote") thread 156 * Optionally called in the downconvert thread after a
157 * after a successful downconvert. The lockres will not be 157 * successful downconvert. The lockres will not be referenced
158 * referenced after this callback is called, so it is safe to 158 * after this callback is called, so it is safe to free
159 * free memory, etc. 159 * memory, etc.
160 * 160 *
161 * The exact semantics of when this is called are controlled 161 * The exact semantics of when this is called are controlled
162 * by ->downconvert_worker() 162 * by ->downconvert_worker()
@@ -225,17 +225,12 @@ static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
225 .flags = 0, 225 .flags = 0,
226}; 226};
227 227
228static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = { 228static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
229 .get_osb = ocfs2_get_inode_osb, 229 .get_osb = ocfs2_get_inode_osb,
230 .check_downconvert = ocfs2_check_meta_downconvert, 230 .check_downconvert = ocfs2_check_meta_downconvert,
231 .set_lvb = ocfs2_set_meta_lvb, 231 .set_lvb = ocfs2_set_meta_lvb,
232 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
233};
234
235static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
236 .get_osb = ocfs2_get_inode_osb,
237 .downconvert_worker = ocfs2_data_convert_worker, 232 .downconvert_worker = ocfs2_data_convert_worker,
238 .flags = 0, 233 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
239}; 234};
240 235
241static struct ocfs2_lock_res_ops ocfs2_super_lops = { 236static struct ocfs2_lock_res_ops ocfs2_super_lops = {
@@ -258,10 +253,14 @@ static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
258 .flags = 0, 253 .flags = 0,
259}; 254};
260 255
256static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
257 .get_osb = ocfs2_get_file_osb,
258 .flags = 0,
259};
260
261static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres) 261static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
262{ 262{
263 return lockres->l_type == OCFS2_LOCK_TYPE_META || 263 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
264 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
265 lockres->l_type == OCFS2_LOCK_TYPE_RW || 264 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
266 lockres->l_type == OCFS2_LOCK_TYPE_OPEN; 265 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
267} 266}
@@ -310,12 +309,24 @@ static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
310 "resource %s: %s\n", dlm_errname(_stat), _func, \ 309 "resource %s: %s\n", dlm_errname(_stat), _func, \
311 _lockres->l_name, dlm_errmsg(_stat)); \ 310 _lockres->l_name, dlm_errmsg(_stat)); \
312} while (0) 311} while (0)
313static void ocfs2_vote_on_unlock(struct ocfs2_super *osb, 312static int ocfs2_downconvert_thread(void *arg);
314 struct ocfs2_lock_res *lockres); 313static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
315static int ocfs2_meta_lock_update(struct inode *inode, 314 struct ocfs2_lock_res *lockres);
315static int ocfs2_inode_lock_update(struct inode *inode,
316 struct buffer_head **bh); 316 struct buffer_head **bh);
317static void ocfs2_drop_osb_locks(struct ocfs2_super *osb); 317static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
318static inline int ocfs2_highest_compat_lock_level(int level); 318static inline int ocfs2_highest_compat_lock_level(int level);
319static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
320 int new_level);
321static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
322 struct ocfs2_lock_res *lockres,
323 int new_level,
324 int lvb);
325static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
326 struct ocfs2_lock_res *lockres);
327static int ocfs2_cancel_convert(struct ocfs2_super *osb,
328 struct ocfs2_lock_res *lockres);
329
319 330
320static void ocfs2_build_lock_name(enum ocfs2_lock_type type, 331static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
321 u64 blkno, 332 u64 blkno,
@@ -402,10 +413,7 @@ void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
402 ops = &ocfs2_inode_rw_lops; 413 ops = &ocfs2_inode_rw_lops;
403 break; 414 break;
404 case OCFS2_LOCK_TYPE_META: 415 case OCFS2_LOCK_TYPE_META:
405 ops = &ocfs2_inode_meta_lops; 416 ops = &ocfs2_inode_inode_lops;
406 break;
407 case OCFS2_LOCK_TYPE_DATA:
408 ops = &ocfs2_inode_data_lops;
409 break; 417 break;
410 case OCFS2_LOCK_TYPE_OPEN: 418 case OCFS2_LOCK_TYPE_OPEN:
411 ops = &ocfs2_inode_open_lops; 419 ops = &ocfs2_inode_open_lops;
@@ -428,6 +436,13 @@ static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
428 return OCFS2_SB(inode->i_sb); 436 return OCFS2_SB(inode->i_sb);
429} 437}
430 438
439static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
440{
441 struct ocfs2_file_private *fp = lockres->l_priv;
442
443 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
444}
445
431static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres) 446static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
432{ 447{
433 __be64 inode_blkno_be; 448 __be64 inode_blkno_be;
@@ -508,6 +523,21 @@ static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
508 &ocfs2_rename_lops, osb); 523 &ocfs2_rename_lops, osb);
509} 524}
510 525
526void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
527 struct ocfs2_file_private *fp)
528{
529 struct inode *inode = fp->fp_file->f_mapping->host;
530 struct ocfs2_inode_info *oi = OCFS2_I(inode);
531
532 ocfs2_lock_res_init_once(lockres);
533 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
534 inode->i_generation, lockres->l_name);
535 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
536 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
537 fp);
538 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
539}
540
511void ocfs2_lock_res_free(struct ocfs2_lock_res *res) 541void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
512{ 542{
513 mlog_entry_void(); 543 mlog_entry_void();
@@ -724,6 +754,13 @@ static void ocfs2_blocking_ast(void *opaque, int level)
724 lockres->l_name, level, lockres->l_level, 754 lockres->l_name, level, lockres->l_level,
725 ocfs2_lock_type_string(lockres->l_type)); 755 ocfs2_lock_type_string(lockres->l_type));
726 756
757 /*
758 * We can skip the bast for locks which don't enable caching -
759 * they'll be dropped at the earliest possible time anyway.
760 */
761 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
762 return;
763
727 spin_lock_irqsave(&lockres->l_lock, flags); 764 spin_lock_irqsave(&lockres->l_lock, flags);
728 needs_downconvert = ocfs2_generic_handle_bast(lockres, level); 765 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
729 if (needs_downconvert) 766 if (needs_downconvert)
@@ -732,7 +769,7 @@ static void ocfs2_blocking_ast(void *opaque, int level)
732 769
733 wake_up(&lockres->l_event); 770 wake_up(&lockres->l_event);
734 771
735 ocfs2_kick_vote_thread(osb); 772 ocfs2_wake_downconvert_thread(osb);
736} 773}
737 774
738static void ocfs2_locking_ast(void *opaque) 775static void ocfs2_locking_ast(void *opaque)
@@ -935,6 +972,21 @@ static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
935 972
936} 973}
937 974
975static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
976 struct ocfs2_lock_res *lockres)
977{
978 int ret;
979
980 ret = wait_for_completion_interruptible(&mw->mw_complete);
981 if (ret)
982 lockres_remove_mask_waiter(lockres, mw);
983 else
984 ret = mw->mw_status;
985 /* Re-arm the completion in case we want to wait on it again */
986 INIT_COMPLETION(mw->mw_complete);
987 return ret;
988}
989
938static int ocfs2_cluster_lock(struct ocfs2_super *osb, 990static int ocfs2_cluster_lock(struct ocfs2_super *osb,
939 struct ocfs2_lock_res *lockres, 991 struct ocfs2_lock_res *lockres,
940 int level, 992 int level,
@@ -1089,7 +1141,7 @@ static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1089 mlog_entry_void(); 1141 mlog_entry_void();
1090 spin_lock_irqsave(&lockres->l_lock, flags); 1142 spin_lock_irqsave(&lockres->l_lock, flags);
1091 ocfs2_dec_holders(lockres, level); 1143 ocfs2_dec_holders(lockres, level);
1092 ocfs2_vote_on_unlock(osb, lockres); 1144 ocfs2_downconvert_on_unlock(osb, lockres);
1093 spin_unlock_irqrestore(&lockres->l_lock, flags); 1145 spin_unlock_irqrestore(&lockres->l_lock, flags);
1094 mlog_exit_void(); 1146 mlog_exit_void();
1095} 1147}
@@ -1147,13 +1199,7 @@ int ocfs2_create_new_inode_locks(struct inode *inode)
1147 * We don't want to use LKM_LOCAL on a meta data lock as they 1199 * We don't want to use LKM_LOCAL on a meta data lock as they
1148 * don't use a generation in their lock names. 1200 * don't use a generation in their lock names.
1149 */ 1201 */
1150 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0); 1202 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1151 if (ret) {
1152 mlog_errno(ret);
1153 goto bail;
1154 }
1155
1156 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
1157 if (ret) { 1203 if (ret) {
1158 mlog_errno(ret); 1204 mlog_errno(ret);
1159 goto bail; 1205 goto bail;
@@ -1311,76 +1357,221 @@ out:
1311 mlog_exit_void(); 1357 mlog_exit_void();
1312} 1358}
1313 1359
1314int ocfs2_data_lock_full(struct inode *inode, 1360static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1315 int write, 1361 int level)
1316 int arg_flags)
1317{ 1362{
1318 int status = 0, level; 1363 int ret;
1319 struct ocfs2_lock_res *lockres; 1364 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1320 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1365 unsigned long flags;
1366 struct ocfs2_mask_waiter mw;
1321 1367
1322 BUG_ON(!inode); 1368 ocfs2_init_mask_waiter(&mw);
1323 1369
1324 mlog_entry_void(); 1370retry_cancel:
1371 spin_lock_irqsave(&lockres->l_lock, flags);
1372 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1373 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1374 if (ret) {
1375 spin_unlock_irqrestore(&lockres->l_lock, flags);
1376 ret = ocfs2_cancel_convert(osb, lockres);
1377 if (ret < 0) {
1378 mlog_errno(ret);
1379 goto out;
1380 }
1381 goto retry_cancel;
1382 }
1383 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1384 spin_unlock_irqrestore(&lockres->l_lock, flags);
1325 1385
1326 mlog(0, "inode %llu take %s DATA lock\n", 1386 ocfs2_wait_for_mask(&mw);
1327 (unsigned long long)OCFS2_I(inode)->ip_blkno, 1387 goto retry_cancel;
1328 write ? "EXMODE" : "PRMODE"); 1388 }
1329 1389
1330 /* We'll allow faking a readonly data lock for 1390 ret = -ERESTARTSYS;
1331 * rodevices. */ 1391 /*
1332 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) { 1392 * We may still have gotten the lock, in which case there's no
1333 if (write) { 1393 * point to restarting the syscall.
1334 status = -EROFS; 1394 */
1335 mlog_errno(status); 1395 if (lockres->l_level == level)
1396 ret = 0;
1397
1398 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1399 lockres->l_flags, lockres->l_level, lockres->l_action);
1400
1401 spin_unlock_irqrestore(&lockres->l_lock, flags);
1402
1403out:
1404 return ret;
1405}
1406
1407/*
1408 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1409 * flock() calls. The locking approach this requires is sufficiently
1410 * different from all other cluster lock types that we implement a
1411 * seperate path to the "low-level" dlm calls. In particular:
1412 *
1413 * - No optimization of lock levels is done - we take at exactly
1414 * what's been requested.
1415 *
1416 * - No lock caching is employed. We immediately downconvert to
1417 * no-lock at unlock time. This also means flock locks never go on
1418 * the blocking list).
1419 *
1420 * - Since userspace can trivially deadlock itself with flock, we make
1421 * sure to allow cancellation of a misbehaving applications flock()
1422 * request.
1423 *
1424 * - Access to any flock lockres doesn't require concurrency, so we
1425 * can simplify the code by requiring the caller to guarantee
1426 * serialization of dlmglue flock calls.
1427 */
1428int ocfs2_file_lock(struct file *file, int ex, int trylock)
1429{
1430 int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
1431 unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
1432 unsigned long flags;
1433 struct ocfs2_file_private *fp = file->private_data;
1434 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1435 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1436 struct ocfs2_mask_waiter mw;
1437
1438 ocfs2_init_mask_waiter(&mw);
1439
1440 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1441 (lockres->l_level > LKM_NLMODE)) {
1442 mlog(ML_ERROR,
1443 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1444 "level: %u\n", lockres->l_name, lockres->l_flags,
1445 lockres->l_level);
1446 return -EINVAL;
1447 }
1448
1449 spin_lock_irqsave(&lockres->l_lock, flags);
1450 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1451 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1452 spin_unlock_irqrestore(&lockres->l_lock, flags);
1453
1454 /*
1455 * Get the lock at NLMODE to start - that way we
1456 * can cancel the upconvert request if need be.
1457 */
1458 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1459 if (ret < 0) {
1460 mlog_errno(ret);
1461 goto out;
1336 } 1462 }
1337 goto out; 1463
1464 ret = ocfs2_wait_for_mask(&mw);
1465 if (ret) {
1466 mlog_errno(ret);
1467 goto out;
1468 }
1469 spin_lock_irqsave(&lockres->l_lock, flags);
1338 } 1470 }
1339 1471
1340 if (ocfs2_mount_local(osb)) 1472 lockres->l_action = OCFS2_AST_CONVERT;
1341 goto out; 1473 lkm_flags |= LKM_CONVERT;
1474 lockres->l_requested = level;
1475 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1342 1476
1343 lockres = &OCFS2_I(inode)->ip_data_lockres; 1477 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1478 spin_unlock_irqrestore(&lockres->l_lock, flags);
1344 1479
1345 level = write ? LKM_EXMODE : LKM_PRMODE; 1480 ret = dlmlock(osb->dlm, level, &lockres->l_lksb, lkm_flags,
1481 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1482 ocfs2_locking_ast, lockres, ocfs2_blocking_ast);
1483 if (ret != DLM_NORMAL) {
1484 if (trylock && ret == DLM_NOTQUEUED)
1485 ret = -EAGAIN;
1486 else {
1487 ocfs2_log_dlm_error("dlmlock", ret, lockres);
1488 ret = -EINVAL;
1489 }
1346 1490
1347 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 1491 ocfs2_recover_from_dlm_error(lockres, 1);
1348 0, arg_flags); 1492 lockres_remove_mask_waiter(lockres, &mw);
1349 if (status < 0 && status != -EAGAIN) 1493 goto out;
1350 mlog_errno(status); 1494 }
1495
1496 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1497 if (ret == -ERESTARTSYS) {
1498 /*
1499 * Userspace can cause deadlock itself with
1500 * flock(). Current behavior locally is to allow the
1501 * deadlock, but abort the system call if a signal is
1502 * received. We follow this example, otherwise a
1503 * poorly written program could sit in kernel until
1504 * reboot.
1505 *
1506 * Handling this is a bit more complicated for Ocfs2
1507 * though. We can't exit this function with an
1508 * outstanding lock request, so a cancel convert is
1509 * required. We intentionally overwrite 'ret' - if the
1510 * cancel fails and the lock was granted, it's easier
1511 * to just bubble sucess back up to the user.
1512 */
1513 ret = ocfs2_flock_handle_signal(lockres, level);
1514 }
1351 1515
1352out: 1516out:
1353 mlog_exit(status); 1517
1354 return status; 1518 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1519 lockres->l_name, ex, trylock, ret);
1520 return ret;
1355} 1521}
1356 1522
1357/* see ocfs2_meta_lock_with_page() */ 1523void ocfs2_file_unlock(struct file *file)
1358int ocfs2_data_lock_with_page(struct inode *inode,
1359 int write,
1360 struct page *page)
1361{ 1524{
1362 int ret; 1525 int ret;
1526 unsigned long flags;
1527 struct ocfs2_file_private *fp = file->private_data;
1528 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1529 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1530 struct ocfs2_mask_waiter mw;
1363 1531
1364 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK); 1532 ocfs2_init_mask_waiter(&mw);
1365 if (ret == -EAGAIN) { 1533
1366 unlock_page(page); 1534 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1367 if (ocfs2_data_lock(inode, write) == 0) 1535 return;
1368 ocfs2_data_unlock(inode, write); 1536
1369 ret = AOP_TRUNCATED_PAGE; 1537 if (lockres->l_level == LKM_NLMODE)
1538 return;
1539
1540 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1541 lockres->l_name, lockres->l_flags, lockres->l_level,
1542 lockres->l_action);
1543
1544 spin_lock_irqsave(&lockres->l_lock, flags);
1545 /*
1546 * Fake a blocking ast for the downconvert code.
1547 */
1548 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1549 lockres->l_blocking = LKM_EXMODE;
1550
1551 ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
1552 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1553 spin_unlock_irqrestore(&lockres->l_lock, flags);
1554
1555 ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0);
1556 if (ret) {
1557 mlog_errno(ret);
1558 return;
1370 } 1559 }
1371 1560
1372 return ret; 1561 ret = ocfs2_wait_for_mask(&mw);
1562 if (ret)
1563 mlog_errno(ret);
1373} 1564}
1374 1565
1375static void ocfs2_vote_on_unlock(struct ocfs2_super *osb, 1566static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1376 struct ocfs2_lock_res *lockres) 1567 struct ocfs2_lock_res *lockres)
1377{ 1568{
1378 int kick = 0; 1569 int kick = 0;
1379 1570
1380 mlog_entry_void(); 1571 mlog_entry_void();
1381 1572
1382 /* If we know that another node is waiting on our lock, kick 1573 /* If we know that another node is waiting on our lock, kick
1383 * the vote thread * pre-emptively when we reach a release 1574 * the downconvert thread * pre-emptively when we reach a release
1384 * condition. */ 1575 * condition. */
1385 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) { 1576 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1386 switch(lockres->l_blocking) { 1577 switch(lockres->l_blocking) {
@@ -1398,27 +1589,7 @@ static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1398 } 1589 }
1399 1590
1400 if (kick) 1591 if (kick)
1401 ocfs2_kick_vote_thread(osb); 1592 ocfs2_wake_downconvert_thread(osb);
1402
1403 mlog_exit_void();
1404}
1405
1406void ocfs2_data_unlock(struct inode *inode,
1407 int write)
1408{
1409 int level = write ? LKM_EXMODE : LKM_PRMODE;
1410 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1411 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1412
1413 mlog_entry_void();
1414
1415 mlog(0, "inode %llu drop %s DATA lock\n",
1416 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1417 write ? "EXMODE" : "PRMODE");
1418
1419 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
1420 !ocfs2_mount_local(osb))
1421 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1422 1593
1423 mlog_exit_void(); 1594 mlog_exit_void();
1424} 1595}
@@ -1442,11 +1613,11 @@ static u64 ocfs2_pack_timespec(struct timespec *spec)
1442 1613
1443/* Call this with the lockres locked. I am reasonably sure we don't 1614/* Call this with the lockres locked. I am reasonably sure we don't
1444 * need ip_lock in this function as anyone who would be changing those 1615 * need ip_lock in this function as anyone who would be changing those
1445 * values is supposed to be blocked in ocfs2_meta_lock right now. */ 1616 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1446static void __ocfs2_stuff_meta_lvb(struct inode *inode) 1617static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1447{ 1618{
1448 struct ocfs2_inode_info *oi = OCFS2_I(inode); 1619 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1449 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres; 1620 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1450 struct ocfs2_meta_lvb *lvb; 1621 struct ocfs2_meta_lvb *lvb;
1451 1622
1452 mlog_entry_void(); 1623 mlog_entry_void();
@@ -1496,7 +1667,7 @@ static void ocfs2_unpack_timespec(struct timespec *spec,
1496static void ocfs2_refresh_inode_from_lvb(struct inode *inode) 1667static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1497{ 1668{
1498 struct ocfs2_inode_info *oi = OCFS2_I(inode); 1669 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1499 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres; 1670 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1500 struct ocfs2_meta_lvb *lvb; 1671 struct ocfs2_meta_lvb *lvb;
1501 1672
1502 mlog_entry_void(); 1673 mlog_entry_void();
@@ -1604,12 +1775,12 @@ static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockre
1604} 1775}
1605 1776
1606/* may or may not return a bh if it went to disk. */ 1777/* may or may not return a bh if it went to disk. */
1607static int ocfs2_meta_lock_update(struct inode *inode, 1778static int ocfs2_inode_lock_update(struct inode *inode,
1608 struct buffer_head **bh) 1779 struct buffer_head **bh)
1609{ 1780{
1610 int status = 0; 1781 int status = 0;
1611 struct ocfs2_inode_info *oi = OCFS2_I(inode); 1782 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1612 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres; 1783 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1613 struct ocfs2_dinode *fe; 1784 struct ocfs2_dinode *fe;
1614 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1785 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1615 1786
@@ -1721,7 +1892,7 @@ static int ocfs2_assign_bh(struct inode *inode,
1721 * returns < 0 error if the callback will never be called, otherwise 1892 * returns < 0 error if the callback will never be called, otherwise
1722 * the result of the lock will be communicated via the callback. 1893 * the result of the lock will be communicated via the callback.
1723 */ 1894 */
1724int ocfs2_meta_lock_full(struct inode *inode, 1895int ocfs2_inode_lock_full(struct inode *inode,
1725 struct buffer_head **ret_bh, 1896 struct buffer_head **ret_bh,
1726 int ex, 1897 int ex,
1727 int arg_flags) 1898 int arg_flags)
@@ -1756,7 +1927,7 @@ int ocfs2_meta_lock_full(struct inode *inode,
1756 wait_event(osb->recovery_event, 1927 wait_event(osb->recovery_event,
1757 ocfs2_node_map_is_empty(osb, &osb->recovery_map)); 1928 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1758 1929
1759 lockres = &OCFS2_I(inode)->ip_meta_lockres; 1930 lockres = &OCFS2_I(inode)->ip_inode_lockres;
1760 level = ex ? LKM_EXMODE : LKM_PRMODE; 1931 level = ex ? LKM_EXMODE : LKM_PRMODE;
1761 dlm_flags = 0; 1932 dlm_flags = 0;
1762 if (arg_flags & OCFS2_META_LOCK_NOQUEUE) 1933 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
@@ -1795,11 +1966,11 @@ local:
1795 } 1966 }
1796 1967
1797 /* This is fun. The caller may want a bh back, or it may 1968 /* This is fun. The caller may want a bh back, or it may
1798 * not. ocfs2_meta_lock_update definitely wants one in, but 1969 * not. ocfs2_inode_lock_update definitely wants one in, but
1799 * may or may not read one, depending on what's in the 1970 * may or may not read one, depending on what's in the
1800 * LVB. The result of all of this is that we've *only* gone to 1971 * LVB. The result of all of this is that we've *only* gone to
1801 * disk if we have to, so the complexity is worthwhile. */ 1972 * disk if we have to, so the complexity is worthwhile. */
1802 status = ocfs2_meta_lock_update(inode, &local_bh); 1973 status = ocfs2_inode_lock_update(inode, &local_bh);
1803 if (status < 0) { 1974 if (status < 0) {
1804 if (status != -ENOENT) 1975 if (status != -ENOENT)
1805 mlog_errno(status); 1976 mlog_errno(status);
@@ -1821,7 +1992,7 @@ bail:
1821 *ret_bh = NULL; 1992 *ret_bh = NULL;
1822 } 1993 }
1823 if (acquired) 1994 if (acquired)
1824 ocfs2_meta_unlock(inode, ex); 1995 ocfs2_inode_unlock(inode, ex);
1825 } 1996 }
1826 1997
1827 if (local_bh) 1998 if (local_bh)
@@ -1832,19 +2003,20 @@ bail:
1832} 2003}
1833 2004
1834/* 2005/*
1835 * This is working around a lock inversion between tasks acquiring DLM locks 2006 * This is working around a lock inversion between tasks acquiring DLM
1836 * while holding a page lock and the vote thread which blocks dlm lock acquiry 2007 * locks while holding a page lock and the downconvert thread which
1837 * while acquiring page locks. 2008 * blocks dlm lock acquiry while acquiring page locks.
1838 * 2009 *
1839 * ** These _with_page variantes are only intended to be called from aop 2010 * ** These _with_page variantes are only intended to be called from aop
1840 * methods that hold page locks and return a very specific *positive* error 2011 * methods that hold page locks and return a very specific *positive* error
1841 * code that aop methods pass up to the VFS -- test for errors with != 0. ** 2012 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1842 * 2013 *
1843 * The DLM is called such that it returns -EAGAIN if it would have blocked 2014 * The DLM is called such that it returns -EAGAIN if it would have
1844 * waiting for the vote thread. In that case we unlock our page so the vote 2015 * blocked waiting for the downconvert thread. In that case we unlock
1845 * thread can make progress. Once we've done this we have to return 2016 * our page so the downconvert thread can make progress. Once we've
1846 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up 2017 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
1847 * into the VFS who will then immediately retry the aop call. 2018 * that called us can bubble that back up into the VFS who will then
2019 * immediately retry the aop call.
1848 * 2020 *
1849 * We do a blocking lock and immediate unlock before returning, though, so that 2021 * We do a blocking lock and immediate unlock before returning, though, so that
1850 * the lock has a great chance of being cached on this node by the time the VFS 2022 * the lock has a great chance of being cached on this node by the time the VFS
@@ -1852,32 +2024,32 @@ bail:
1852 * ping locks back and forth, but that's a risk we're willing to take to avoid 2024 * ping locks back and forth, but that's a risk we're willing to take to avoid
1853 * the lock inversion simply. 2025 * the lock inversion simply.
1854 */ 2026 */
1855int ocfs2_meta_lock_with_page(struct inode *inode, 2027int ocfs2_inode_lock_with_page(struct inode *inode,
1856 struct buffer_head **ret_bh, 2028 struct buffer_head **ret_bh,
1857 int ex, 2029 int ex,
1858 struct page *page) 2030 struct page *page)
1859{ 2031{
1860 int ret; 2032 int ret;
1861 2033
1862 ret = ocfs2_meta_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK); 2034 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
1863 if (ret == -EAGAIN) { 2035 if (ret == -EAGAIN) {
1864 unlock_page(page); 2036 unlock_page(page);
1865 if (ocfs2_meta_lock(inode, ret_bh, ex) == 0) 2037 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
1866 ocfs2_meta_unlock(inode, ex); 2038 ocfs2_inode_unlock(inode, ex);
1867 ret = AOP_TRUNCATED_PAGE; 2039 ret = AOP_TRUNCATED_PAGE;
1868 } 2040 }
1869 2041
1870 return ret; 2042 return ret;
1871} 2043}
1872 2044
1873int ocfs2_meta_lock_atime(struct inode *inode, 2045int ocfs2_inode_lock_atime(struct inode *inode,
1874 struct vfsmount *vfsmnt, 2046 struct vfsmount *vfsmnt,
1875 int *level) 2047 int *level)
1876{ 2048{
1877 int ret; 2049 int ret;
1878 2050
1879 mlog_entry_void(); 2051 mlog_entry_void();
1880 ret = ocfs2_meta_lock(inode, NULL, 0); 2052 ret = ocfs2_inode_lock(inode, NULL, 0);
1881 if (ret < 0) { 2053 if (ret < 0) {
1882 mlog_errno(ret); 2054 mlog_errno(ret);
1883 return ret; 2055 return ret;
@@ -1890,8 +2062,8 @@ int ocfs2_meta_lock_atime(struct inode *inode,
1890 if (ocfs2_should_update_atime(inode, vfsmnt)) { 2062 if (ocfs2_should_update_atime(inode, vfsmnt)) {
1891 struct buffer_head *bh = NULL; 2063 struct buffer_head *bh = NULL;
1892 2064
1893 ocfs2_meta_unlock(inode, 0); 2065 ocfs2_inode_unlock(inode, 0);
1894 ret = ocfs2_meta_lock(inode, &bh, 1); 2066 ret = ocfs2_inode_lock(inode, &bh, 1);
1895 if (ret < 0) { 2067 if (ret < 0) {
1896 mlog_errno(ret); 2068 mlog_errno(ret);
1897 return ret; 2069 return ret;
@@ -1908,11 +2080,11 @@ int ocfs2_meta_lock_atime(struct inode *inode,
1908 return ret; 2080 return ret;
1909} 2081}
1910 2082
1911void ocfs2_meta_unlock(struct inode *inode, 2083void ocfs2_inode_unlock(struct inode *inode,
1912 int ex) 2084 int ex)
1913{ 2085{
1914 int level = ex ? LKM_EXMODE : LKM_PRMODE; 2086 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1915 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres; 2087 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
1916 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2088 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1917 2089
1918 mlog_entry_void(); 2090 mlog_entry_void();
@@ -2320,11 +2492,11 @@ int ocfs2_dlm_init(struct ocfs2_super *osb)
2320 goto bail; 2492 goto bail;
2321 } 2493 }
2322 2494
2323 /* launch vote thread */ 2495 /* launch downconvert thread */
2324 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote"); 2496 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2325 if (IS_ERR(osb->vote_task)) { 2497 if (IS_ERR(osb->dc_task)) {
2326 status = PTR_ERR(osb->vote_task); 2498 status = PTR_ERR(osb->dc_task);
2327 osb->vote_task = NULL; 2499 osb->dc_task = NULL;
2328 mlog_errno(status); 2500 mlog_errno(status);
2329 goto bail; 2501 goto bail;
2330 } 2502 }
@@ -2353,8 +2525,8 @@ local:
2353bail: 2525bail:
2354 if (status < 0) { 2526 if (status < 0) {
2355 ocfs2_dlm_shutdown_debug(osb); 2527 ocfs2_dlm_shutdown_debug(osb);
2356 if (osb->vote_task) 2528 if (osb->dc_task)
2357 kthread_stop(osb->vote_task); 2529 kthread_stop(osb->dc_task);
2358 } 2530 }
2359 2531
2360 mlog_exit(status); 2532 mlog_exit(status);
@@ -2369,9 +2541,9 @@ void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2369 2541
2370 ocfs2_drop_osb_locks(osb); 2542 ocfs2_drop_osb_locks(osb);
2371 2543
2372 if (osb->vote_task) { 2544 if (osb->dc_task) {
2373 kthread_stop(osb->vote_task); 2545 kthread_stop(osb->dc_task);
2374 osb->vote_task = NULL; 2546 osb->dc_task = NULL;
2375 } 2547 }
2376 2548
2377 ocfs2_lock_res_free(&osb->osb_super_lockres); 2549 ocfs2_lock_res_free(&osb->osb_super_lockres);
@@ -2527,7 +2699,7 @@ out:
2527 2699
2528/* Mark the lockres as being dropped. It will no longer be 2700/* Mark the lockres as being dropped. It will no longer be
2529 * queued if blocking, but we still may have to wait on it 2701 * queued if blocking, but we still may have to wait on it
2530 * being dequeued from the vote thread before we can consider 2702 * being dequeued from the downconvert thread before we can consider
2531 * it safe to drop. 2703 * it safe to drop.
2532 * 2704 *
2533 * You can *not* attempt to call cluster_lock on this lockres anymore. */ 2705 * You can *not* attempt to call cluster_lock on this lockres anymore. */
@@ -2590,14 +2762,7 @@ int ocfs2_drop_inode_locks(struct inode *inode)
2590 status = err; 2762 status = err;
2591 2763
2592 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb), 2764 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2593 &OCFS2_I(inode)->ip_data_lockres); 2765 &OCFS2_I(inode)->ip_inode_lockres);
2594 if (err < 0)
2595 mlog_errno(err);
2596 if (err < 0 && !status)
2597 status = err;
2598
2599 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2600 &OCFS2_I(inode)->ip_meta_lockres);
2601 if (err < 0) 2766 if (err < 0)
2602 mlog_errno(err); 2767 mlog_errno(err);
2603 if (err < 0 && !status) 2768 if (err < 0 && !status)
@@ -2850,6 +3015,9 @@ static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2850 inode = ocfs2_lock_res_inode(lockres); 3015 inode = ocfs2_lock_res_inode(lockres);
2851 mapping = inode->i_mapping; 3016 mapping = inode->i_mapping;
2852 3017
3018 if (S_ISREG(inode->i_mode))
3019 goto out;
3020
2853 /* 3021 /*
2854 * We need this before the filemap_fdatawrite() so that it can 3022 * We need this before the filemap_fdatawrite() so that it can
2855 * transfer the dirty bit from the PTE to the 3023 * transfer the dirty bit from the PTE to the
@@ -2875,6 +3043,7 @@ static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2875 filemap_fdatawait(mapping); 3043 filemap_fdatawait(mapping);
2876 } 3044 }
2877 3045
3046out:
2878 return UNBLOCK_CONTINUE; 3047 return UNBLOCK_CONTINUE;
2879} 3048}
2880 3049
@@ -2903,7 +3072,7 @@ static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
2903 3072
2904/* 3073/*
2905 * Does the final reference drop on our dentry lock. Right now this 3074 * Does the final reference drop on our dentry lock. Right now this
2906 * happens in the vote thread, but we could choose to simplify the 3075 * happens in the downconvert thread, but we could choose to simplify the
2907 * dlmglue API and push these off to the ocfs2_wq in the future. 3076 * dlmglue API and push these off to the ocfs2_wq in the future.
2908 */ 3077 */
2909static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb, 3078static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
@@ -3042,7 +3211,7 @@ void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3042 mlog(0, "lockres %s blocked.\n", lockres->l_name); 3211 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3043 3212
3044 /* Detect whether a lock has been marked as going away while 3213 /* Detect whether a lock has been marked as going away while
3045 * the vote thread was processing other things. A lock can 3214 * the downconvert thread was processing other things. A lock can
3046 * still be marked with OCFS2_LOCK_FREEING after this check, 3215 * still be marked with OCFS2_LOCK_FREEING after this check,
3047 * but short circuiting here will still save us some 3216 * but short circuiting here will still save us some
3048 * performance. */ 3217 * performance. */
@@ -3091,13 +3260,104 @@ static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3091 3260
3092 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED); 3261 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3093 3262
3094 spin_lock(&osb->vote_task_lock); 3263 spin_lock(&osb->dc_task_lock);
3095 if (list_empty(&lockres->l_blocked_list)) { 3264 if (list_empty(&lockres->l_blocked_list)) {
3096 list_add_tail(&lockres->l_blocked_list, 3265 list_add_tail(&lockres->l_blocked_list,
3097 &osb->blocked_lock_list); 3266 &osb->blocked_lock_list);
3098 osb->blocked_lock_count++; 3267 osb->blocked_lock_count++;
3099 } 3268 }
3100 spin_unlock(&osb->vote_task_lock); 3269 spin_unlock(&osb->dc_task_lock);
3270
3271 mlog_exit_void();
3272}
3273
3274static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3275{
3276 unsigned long processed;
3277 struct ocfs2_lock_res *lockres;
3278
3279 mlog_entry_void();
3280
3281 spin_lock(&osb->dc_task_lock);
3282 /* grab this early so we know to try again if a state change and
3283 * wake happens part-way through our work */
3284 osb->dc_work_sequence = osb->dc_wake_sequence;
3285
3286 processed = osb->blocked_lock_count;
3287 while (processed) {
3288 BUG_ON(list_empty(&osb->blocked_lock_list));
3289
3290 lockres = list_entry(osb->blocked_lock_list.next,
3291 struct ocfs2_lock_res, l_blocked_list);
3292 list_del_init(&lockres->l_blocked_list);
3293 osb->blocked_lock_count--;
3294 spin_unlock(&osb->dc_task_lock);
3295
3296 BUG_ON(!processed);
3297 processed--;
3298
3299 ocfs2_process_blocked_lock(osb, lockres);
3300
3301 spin_lock(&osb->dc_task_lock);
3302 }
3303 spin_unlock(&osb->dc_task_lock);
3101 3304
3102 mlog_exit_void(); 3305 mlog_exit_void();
3103} 3306}
3307
3308static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3309{
3310 int empty = 0;
3311
3312 spin_lock(&osb->dc_task_lock);
3313 if (list_empty(&osb->blocked_lock_list))
3314 empty = 1;
3315
3316 spin_unlock(&osb->dc_task_lock);
3317 return empty;
3318}
3319
3320static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3321{
3322 int should_wake = 0;
3323
3324 spin_lock(&osb->dc_task_lock);
3325 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3326 should_wake = 1;
3327 spin_unlock(&osb->dc_task_lock);
3328
3329 return should_wake;
3330}
3331
3332int ocfs2_downconvert_thread(void *arg)
3333{
3334 int status = 0;
3335 struct ocfs2_super *osb = arg;
3336
3337 /* only quit once we've been asked to stop and there is no more
3338 * work available */
3339 while (!(kthread_should_stop() &&
3340 ocfs2_downconvert_thread_lists_empty(osb))) {
3341
3342 wait_event_interruptible(osb->dc_event,
3343 ocfs2_downconvert_thread_should_wake(osb) ||
3344 kthread_should_stop());
3345
3346 mlog(0, "downconvert_thread: awoken\n");
3347
3348 ocfs2_downconvert_thread_do_work(osb);
3349 }
3350
3351 osb->dc_task = NULL;
3352 return status;
3353}
3354
3355void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3356{
3357 spin_lock(&osb->dc_task_lock);
3358 /* make sure the voting thread gets a swipe at whatever changes
3359 * the caller may have made to the voting state */
3360 osb->dc_wake_sequence++;
3361 spin_unlock(&osb->dc_task_lock);
3362 wake_up(&osb->dc_event);
3363}
diff --git a/fs/ocfs2/dlmglue.h b/fs/ocfs2/dlmglue.h
index 87a785e41205..5f17243ba501 100644
--- a/fs/ocfs2/dlmglue.h
+++ b/fs/ocfs2/dlmglue.h
@@ -49,12 +49,12 @@ struct ocfs2_meta_lvb {
49 __be32 lvb_reserved2; 49 __be32 lvb_reserved2;
50}; 50};
51 51
52/* ocfs2_meta_lock_full() and ocfs2_data_lock_full() 'arg_flags' flags */ 52/* ocfs2_inode_lock_full() 'arg_flags' flags */
53/* don't wait on recovery. */ 53/* don't wait on recovery. */
54#define OCFS2_META_LOCK_RECOVERY (0x01) 54#define OCFS2_META_LOCK_RECOVERY (0x01)
55/* Instruct the dlm not to queue ourselves on the other node. */ 55/* Instruct the dlm not to queue ourselves on the other node. */
56#define OCFS2_META_LOCK_NOQUEUE (0x02) 56#define OCFS2_META_LOCK_NOQUEUE (0x02)
57/* don't block waiting for the vote thread, instead return -EAGAIN */ 57/* don't block waiting for the downconvert thread, instead return -EAGAIN */
58#define OCFS2_LOCK_NONBLOCK (0x04) 58#define OCFS2_LOCK_NONBLOCK (0x04)
59 59
60int ocfs2_dlm_init(struct ocfs2_super *osb); 60int ocfs2_dlm_init(struct ocfs2_super *osb);
@@ -66,38 +66,32 @@ void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
66 struct inode *inode); 66 struct inode *inode);
67void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl, 67void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
68 u64 parent, struct inode *inode); 68 u64 parent, struct inode *inode);
69struct ocfs2_file_private;
70void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
71 struct ocfs2_file_private *fp);
69void ocfs2_lock_res_free(struct ocfs2_lock_res *res); 72void ocfs2_lock_res_free(struct ocfs2_lock_res *res);
70int ocfs2_create_new_inode_locks(struct inode *inode); 73int ocfs2_create_new_inode_locks(struct inode *inode);
71int ocfs2_drop_inode_locks(struct inode *inode); 74int ocfs2_drop_inode_locks(struct inode *inode);
72int ocfs2_data_lock_full(struct inode *inode,
73 int write,
74 int arg_flags);
75#define ocfs2_data_lock(inode, write) ocfs2_data_lock_full(inode, write, 0)
76int ocfs2_data_lock_with_page(struct inode *inode,
77 int write,
78 struct page *page);
79void ocfs2_data_unlock(struct inode *inode,
80 int write);
81int ocfs2_rw_lock(struct inode *inode, int write); 75int ocfs2_rw_lock(struct inode *inode, int write);
82void ocfs2_rw_unlock(struct inode *inode, int write); 76void ocfs2_rw_unlock(struct inode *inode, int write);
83int ocfs2_open_lock(struct inode *inode); 77int ocfs2_open_lock(struct inode *inode);
84int ocfs2_try_open_lock(struct inode *inode, int write); 78int ocfs2_try_open_lock(struct inode *inode, int write);
85void ocfs2_open_unlock(struct inode *inode); 79void ocfs2_open_unlock(struct inode *inode);
86int ocfs2_meta_lock_atime(struct inode *inode, 80int ocfs2_inode_lock_atime(struct inode *inode,
87 struct vfsmount *vfsmnt, 81 struct vfsmount *vfsmnt,
88 int *level); 82 int *level);
89int ocfs2_meta_lock_full(struct inode *inode, 83int ocfs2_inode_lock_full(struct inode *inode,
90 struct buffer_head **ret_bh, 84 struct buffer_head **ret_bh,
91 int ex, 85 int ex,
92 int arg_flags); 86 int arg_flags);
93int ocfs2_meta_lock_with_page(struct inode *inode, 87int ocfs2_inode_lock_with_page(struct inode *inode,
94 struct buffer_head **ret_bh, 88 struct buffer_head **ret_bh,
95 int ex, 89 int ex,
96 struct page *page); 90 struct page *page);
97/* 99% of the time we don't want to supply any additional flags -- 91/* 99% of the time we don't want to supply any additional flags --
98 * those are for very specific cases only. */ 92 * those are for very specific cases only. */
99#define ocfs2_meta_lock(i, b, e) ocfs2_meta_lock_full(i, b, e, 0) 93#define ocfs2_inode_lock(i, b, e) ocfs2_inode_lock_full(i, b, e, 0)
100void ocfs2_meta_unlock(struct inode *inode, 94void ocfs2_inode_unlock(struct inode *inode,
101 int ex); 95 int ex);
102int ocfs2_super_lock(struct ocfs2_super *osb, 96int ocfs2_super_lock(struct ocfs2_super *osb,
103 int ex); 97 int ex);
@@ -107,14 +101,17 @@ int ocfs2_rename_lock(struct ocfs2_super *osb);
107void ocfs2_rename_unlock(struct ocfs2_super *osb); 101void ocfs2_rename_unlock(struct ocfs2_super *osb);
108int ocfs2_dentry_lock(struct dentry *dentry, int ex); 102int ocfs2_dentry_lock(struct dentry *dentry, int ex);
109void ocfs2_dentry_unlock(struct dentry *dentry, int ex); 103void ocfs2_dentry_unlock(struct dentry *dentry, int ex);
104int ocfs2_file_lock(struct file *file, int ex, int trylock);
105void ocfs2_file_unlock(struct file *file);
110 106
111void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres); 107void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres);
112void ocfs2_simple_drop_lockres(struct ocfs2_super *osb, 108void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
113 struct ocfs2_lock_res *lockres); 109 struct ocfs2_lock_res *lockres);
114 110
115/* for the vote thread */ 111/* for the downconvert thread */
116void ocfs2_process_blocked_lock(struct ocfs2_super *osb, 112void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
117 struct ocfs2_lock_res *lockres); 113 struct ocfs2_lock_res *lockres);
114void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb);
118 115
119struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void); 116struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void);
120void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug); 117void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug);
diff --git a/fs/ocfs2/endian.h b/fs/ocfs2/endian.h
index ff257628af16..1942e09f6ee5 100644
--- a/fs/ocfs2/endian.h
+++ b/fs/ocfs2/endian.h
@@ -37,11 +37,6 @@ static inline void le64_add_cpu(__le64 *var, u64 val)
37 *var = cpu_to_le64(le64_to_cpu(*var) + val); 37 *var = cpu_to_le64(le64_to_cpu(*var) + val);
38} 38}
39 39
40static inline void le32_and_cpu(__le32 *var, u32 val)
41{
42 *var = cpu_to_le32(le32_to_cpu(*var) & val);
43}
44
45static inline void be32_add_cpu(__be32 *var, u32 val) 40static inline void be32_add_cpu(__be32 *var, u32 val)
46{ 41{
47 *var = cpu_to_be32(be32_to_cpu(*var) + val); 42 *var = cpu_to_be32(be32_to_cpu(*var) + val);
diff --git a/fs/ocfs2/export.c b/fs/ocfs2/export.c
index 535bfa9568a4..67527cebf214 100644
--- a/fs/ocfs2/export.c
+++ b/fs/ocfs2/export.c
@@ -58,7 +58,7 @@ static struct dentry *ocfs2_get_dentry(struct super_block *sb,
58 return ERR_PTR(-ESTALE); 58 return ERR_PTR(-ESTALE);
59 } 59 }
60 60
61 inode = ocfs2_iget(OCFS2_SB(sb), handle->ih_blkno, 0); 61 inode = ocfs2_iget(OCFS2_SB(sb), handle->ih_blkno, 0, 0);
62 62
63 if (IS_ERR(inode)) 63 if (IS_ERR(inode))
64 return (void *)inode; 64 return (void *)inode;
@@ -95,7 +95,7 @@ static struct dentry *ocfs2_get_parent(struct dentry *child)
95 mlog(0, "find parent of directory %llu\n", 95 mlog(0, "find parent of directory %llu\n",
96 (unsigned long long)OCFS2_I(dir)->ip_blkno); 96 (unsigned long long)OCFS2_I(dir)->ip_blkno);
97 97
98 status = ocfs2_meta_lock(dir, NULL, 0); 98 status = ocfs2_inode_lock(dir, NULL, 0);
99 if (status < 0) { 99 if (status < 0) {
100 if (status != -ENOENT) 100 if (status != -ENOENT)
101 mlog_errno(status); 101 mlog_errno(status);
@@ -109,7 +109,7 @@ static struct dentry *ocfs2_get_parent(struct dentry *child)
109 goto bail_unlock; 109 goto bail_unlock;
110 } 110 }
111 111
112 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0); 112 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
113 if (IS_ERR(inode)) { 113 if (IS_ERR(inode)) {
114 mlog(ML_ERROR, "Unable to create inode %llu\n", 114 mlog(ML_ERROR, "Unable to create inode %llu\n",
115 (unsigned long long)blkno); 115 (unsigned long long)blkno);
@@ -126,7 +126,7 @@ static struct dentry *ocfs2_get_parent(struct dentry *child)
126 parent->d_op = &ocfs2_dentry_ops; 126 parent->d_op = &ocfs2_dentry_ops;
127 127
128bail_unlock: 128bail_unlock:
129 ocfs2_meta_unlock(dir, 0); 129 ocfs2_inode_unlock(dir, 0);
130 130
131bail: 131bail:
132 mlog_exit_ptr(parent); 132 mlog_exit_ptr(parent);
diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c
index b75b2e1f0e42..ed5d5232e85d 100644
--- a/fs/ocfs2/file.c
+++ b/fs/ocfs2/file.c
@@ -51,6 +51,7 @@
51#include "inode.h" 51#include "inode.h"
52#include "ioctl.h" 52#include "ioctl.h"
53#include "journal.h" 53#include "journal.h"
54#include "locks.h"
54#include "mmap.h" 55#include "mmap.h"
55#include "suballoc.h" 56#include "suballoc.h"
56#include "super.h" 57#include "super.h"
@@ -63,6 +64,35 @@ static int ocfs2_sync_inode(struct inode *inode)
63 return sync_mapping_buffers(inode->i_mapping); 64 return sync_mapping_buffers(inode->i_mapping);
64} 65}
65 66
67static int ocfs2_init_file_private(struct inode *inode, struct file *file)
68{
69 struct ocfs2_file_private *fp;
70
71 fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL);
72 if (!fp)
73 return -ENOMEM;
74
75 fp->fp_file = file;
76 mutex_init(&fp->fp_mutex);
77 ocfs2_file_lock_res_init(&fp->fp_flock, fp);
78 file->private_data = fp;
79
80 return 0;
81}
82
83static void ocfs2_free_file_private(struct inode *inode, struct file *file)
84{
85 struct ocfs2_file_private *fp = file->private_data;
86 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
87
88 if (fp) {
89 ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
90 ocfs2_lock_res_free(&fp->fp_flock);
91 kfree(fp);
92 file->private_data = NULL;
93 }
94}
95
66static int ocfs2_file_open(struct inode *inode, struct file *file) 96static int ocfs2_file_open(struct inode *inode, struct file *file)
67{ 97{
68 int status; 98 int status;
@@ -89,7 +119,18 @@ static int ocfs2_file_open(struct inode *inode, struct file *file)
89 119
90 oi->ip_open_count++; 120 oi->ip_open_count++;
91 spin_unlock(&oi->ip_lock); 121 spin_unlock(&oi->ip_lock);
92 status = 0; 122
123 status = ocfs2_init_file_private(inode, file);
124 if (status) {
125 /*
126 * We want to set open count back if we're failing the
127 * open.
128 */
129 spin_lock(&oi->ip_lock);
130 oi->ip_open_count--;
131 spin_unlock(&oi->ip_lock);
132 }
133
93leave: 134leave:
94 mlog_exit(status); 135 mlog_exit(status);
95 return status; 136 return status;
@@ -108,11 +149,24 @@ static int ocfs2_file_release(struct inode *inode, struct file *file)
108 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT; 149 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
109 spin_unlock(&oi->ip_lock); 150 spin_unlock(&oi->ip_lock);
110 151
152 ocfs2_free_file_private(inode, file);
153
111 mlog_exit(0); 154 mlog_exit(0);
112 155
113 return 0; 156 return 0;
114} 157}
115 158
159static int ocfs2_dir_open(struct inode *inode, struct file *file)
160{
161 return ocfs2_init_file_private(inode, file);
162}
163
164static int ocfs2_dir_release(struct inode *inode, struct file *file)
165{
166 ocfs2_free_file_private(inode, file);
167 return 0;
168}
169
116static int ocfs2_sync_file(struct file *file, 170static int ocfs2_sync_file(struct file *file,
117 struct dentry *dentry, 171 struct dentry *dentry,
118 int datasync) 172 int datasync)
@@ -382,18 +436,13 @@ static int ocfs2_truncate_file(struct inode *inode,
382 436
383 down_write(&OCFS2_I(inode)->ip_alloc_sem); 437 down_write(&OCFS2_I(inode)->ip_alloc_sem);
384 438
385 /* This forces other nodes to sync and drop their pages. Do 439 /*
386 * this even if we have a truncate without allocation change - 440 * The inode lock forced other nodes to sync and drop their
387 * ocfs2 cluster sizes can be much greater than page size, so 441 * pages, which (correctly) happens even if we have a truncate
388 * we have to truncate them anyway. */ 442 * without allocation change - ocfs2 cluster sizes can be much
389 status = ocfs2_data_lock(inode, 1); 443 * greater than page size, so we have to truncate them
390 if (status < 0) { 444 * anyway.
391 up_write(&OCFS2_I(inode)->ip_alloc_sem); 445 */
392
393 mlog_errno(status);
394 goto bail;
395 }
396
397 unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1); 446 unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
398 truncate_inode_pages(inode->i_mapping, new_i_size); 447 truncate_inode_pages(inode->i_mapping, new_i_size);
399 448
@@ -403,7 +452,7 @@ static int ocfs2_truncate_file(struct inode *inode,
403 if (status) 452 if (status)
404 mlog_errno(status); 453 mlog_errno(status);
405 454
406 goto bail_unlock_data; 455 goto bail_unlock_sem;
407 } 456 }
408 457
409 /* alright, we're going to need to do a full blown alloc size 458 /* alright, we're going to need to do a full blown alloc size
@@ -413,25 +462,23 @@ static int ocfs2_truncate_file(struct inode *inode,
413 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size); 462 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
414 if (status < 0) { 463 if (status < 0) {
415 mlog_errno(status); 464 mlog_errno(status);
416 goto bail_unlock_data; 465 goto bail_unlock_sem;
417 } 466 }
418 467
419 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc); 468 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
420 if (status < 0) { 469 if (status < 0) {
421 mlog_errno(status); 470 mlog_errno(status);
422 goto bail_unlock_data; 471 goto bail_unlock_sem;
423 } 472 }
424 473
425 status = ocfs2_commit_truncate(osb, inode, di_bh, tc); 474 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
426 if (status < 0) { 475 if (status < 0) {
427 mlog_errno(status); 476 mlog_errno(status);
428 goto bail_unlock_data; 477 goto bail_unlock_sem;
429 } 478 }
430 479
431 /* TODO: orphan dir cleanup here. */ 480 /* TODO: orphan dir cleanup here. */
432bail_unlock_data: 481bail_unlock_sem:
433 ocfs2_data_unlock(inode, 1);
434
435 up_write(&OCFS2_I(inode)->ip_alloc_sem); 482 up_write(&OCFS2_I(inode)->ip_alloc_sem);
436 483
437bail: 484bail:
@@ -579,7 +626,7 @@ int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
579 626
580 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, " 627 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
581 "clusters_to_add = %u, extents_to_split = %u\n", 628 "clusters_to_add = %u, extents_to_split = %u\n",
582 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode), 629 (unsigned long long)OCFS2_I(inode)->ip_blkno, (long long)i_size_read(inode),
583 le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split); 630 le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split);
584 631
585 num_free_extents = ocfs2_num_free_extents(osb, inode, di); 632 num_free_extents = ocfs2_num_free_extents(osb, inode, di);
@@ -760,7 +807,7 @@ restarted_transaction:
760 le32_to_cpu(fe->i_clusters), 807 le32_to_cpu(fe->i_clusters),
761 (unsigned long long)le64_to_cpu(fe->i_size)); 808 (unsigned long long)le64_to_cpu(fe->i_size));
762 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n", 809 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
763 OCFS2_I(inode)->ip_clusters, i_size_read(inode)); 810 OCFS2_I(inode)->ip_clusters, (long long)i_size_read(inode));
764 811
765leave: 812leave:
766 if (handle) { 813 if (handle) {
@@ -917,7 +964,7 @@ static int ocfs2_extend_file(struct inode *inode,
917 struct buffer_head *di_bh, 964 struct buffer_head *di_bh,
918 u64 new_i_size) 965 u64 new_i_size)
919{ 966{
920 int ret = 0, data_locked = 0; 967 int ret = 0;
921 struct ocfs2_inode_info *oi = OCFS2_I(inode); 968 struct ocfs2_inode_info *oi = OCFS2_I(inode);
922 969
923 BUG_ON(!di_bh); 970 BUG_ON(!di_bh);
@@ -943,20 +990,6 @@ static int ocfs2_extend_file(struct inode *inode,
943 && ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) 990 && ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
944 goto out_update_size; 991 goto out_update_size;
945 992
946 /*
947 * protect the pages that ocfs2_zero_extend is going to be
948 * pulling into the page cache.. we do this before the
949 * metadata extend so that we don't get into the situation
950 * where we've extended the metadata but can't get the data
951 * lock to zero.
952 */
953 ret = ocfs2_data_lock(inode, 1);
954 if (ret < 0) {
955 mlog_errno(ret);
956 goto out;
957 }
958 data_locked = 1;
959
960 /* 993 /*
961 * The alloc sem blocks people in read/write from reading our 994 * The alloc sem blocks people in read/write from reading our
962 * allocation until we're done changing it. We depend on 995 * allocation until we're done changing it. We depend on
@@ -980,7 +1013,7 @@ static int ocfs2_extend_file(struct inode *inode,
980 up_write(&oi->ip_alloc_sem); 1013 up_write(&oi->ip_alloc_sem);
981 1014
982 mlog_errno(ret); 1015 mlog_errno(ret);
983 goto out_unlock; 1016 goto out;
984 } 1017 }
985 } 1018 }
986 1019
@@ -991,7 +1024,7 @@ static int ocfs2_extend_file(struct inode *inode,
991 1024
992 if (ret < 0) { 1025 if (ret < 0) {
993 mlog_errno(ret); 1026 mlog_errno(ret);
994 goto out_unlock; 1027 goto out;
995 } 1028 }
996 1029
997out_update_size: 1030out_update_size:
@@ -999,10 +1032,6 @@ out_update_size:
999 if (ret < 0) 1032 if (ret < 0)
1000 mlog_errno(ret); 1033 mlog_errno(ret);
1001 1034
1002out_unlock:
1003 if (data_locked)
1004 ocfs2_data_unlock(inode, 1);
1005
1006out: 1035out:
1007 return ret; 1036 return ret;
1008} 1037}
@@ -1050,7 +1079,7 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
1050 } 1079 }
1051 } 1080 }
1052 1081
1053 status = ocfs2_meta_lock(inode, &bh, 1); 1082 status = ocfs2_inode_lock(inode, &bh, 1);
1054 if (status < 0) { 1083 if (status < 0) {
1055 if (status != -ENOENT) 1084 if (status != -ENOENT)
1056 mlog_errno(status); 1085 mlog_errno(status);
@@ -1102,7 +1131,7 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
1102bail_commit: 1131bail_commit:
1103 ocfs2_commit_trans(osb, handle); 1132 ocfs2_commit_trans(osb, handle);
1104bail_unlock: 1133bail_unlock:
1105 ocfs2_meta_unlock(inode, 1); 1134 ocfs2_inode_unlock(inode, 1);
1106bail_unlock_rw: 1135bail_unlock_rw:
1107 if (size_change) 1136 if (size_change)
1108 ocfs2_rw_unlock(inode, 1); 1137 ocfs2_rw_unlock(inode, 1);
@@ -1149,7 +1178,7 @@ int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1149 1178
1150 mlog_entry_void(); 1179 mlog_entry_void();
1151 1180
1152 ret = ocfs2_meta_lock(inode, NULL, 0); 1181 ret = ocfs2_inode_lock(inode, NULL, 0);
1153 if (ret) { 1182 if (ret) {
1154 if (ret != -ENOENT) 1183 if (ret != -ENOENT)
1155 mlog_errno(ret); 1184 mlog_errno(ret);
@@ -1158,7 +1187,7 @@ int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1158 1187
1159 ret = generic_permission(inode, mask, NULL); 1188 ret = generic_permission(inode, mask, NULL);
1160 1189
1161 ocfs2_meta_unlock(inode, 0); 1190 ocfs2_inode_unlock(inode, 0);
1162out: 1191out:
1163 mlog_exit(ret); 1192 mlog_exit(ret);
1164 return ret; 1193 return ret;
@@ -1630,7 +1659,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1630 goto out; 1659 goto out;
1631 } 1660 }
1632 1661
1633 ret = ocfs2_meta_lock(inode, &di_bh, 1); 1662 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1634 if (ret) { 1663 if (ret) {
1635 mlog_errno(ret); 1664 mlog_errno(ret);
1636 goto out_rw_unlock; 1665 goto out_rw_unlock;
@@ -1638,7 +1667,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1638 1667
1639 if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) { 1668 if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
1640 ret = -EPERM; 1669 ret = -EPERM;
1641 goto out_meta_unlock; 1670 goto out_inode_unlock;
1642 } 1671 }
1643 1672
1644 switch (sr->l_whence) { 1673 switch (sr->l_whence) {
@@ -1652,7 +1681,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1652 break; 1681 break;
1653 default: 1682 default:
1654 ret = -EINVAL; 1683 ret = -EINVAL;
1655 goto out_meta_unlock; 1684 goto out_inode_unlock;
1656 } 1685 }
1657 sr->l_whence = 0; 1686 sr->l_whence = 0;
1658 1687
@@ -1663,14 +1692,14 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1663 || (sr->l_start + llen) < 0 1692 || (sr->l_start + llen) < 0
1664 || (sr->l_start + llen) > max_off) { 1693 || (sr->l_start + llen) > max_off) {
1665 ret = -EINVAL; 1694 ret = -EINVAL;
1666 goto out_meta_unlock; 1695 goto out_inode_unlock;
1667 } 1696 }
1668 size = sr->l_start + sr->l_len; 1697 size = sr->l_start + sr->l_len;
1669 1698
1670 if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) { 1699 if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
1671 if (sr->l_len <= 0) { 1700 if (sr->l_len <= 0) {
1672 ret = -EINVAL; 1701 ret = -EINVAL;
1673 goto out_meta_unlock; 1702 goto out_inode_unlock;
1674 } 1703 }
1675 } 1704 }
1676 1705
@@ -1678,7 +1707,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1678 ret = __ocfs2_write_remove_suid(inode, di_bh); 1707 ret = __ocfs2_write_remove_suid(inode, di_bh);
1679 if (ret) { 1708 if (ret) {
1680 mlog_errno(ret); 1709 mlog_errno(ret);
1681 goto out_meta_unlock; 1710 goto out_inode_unlock;
1682 } 1711 }
1683 } 1712 }
1684 1713
@@ -1704,7 +1733,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1704 up_write(&OCFS2_I(inode)->ip_alloc_sem); 1733 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1705 if (ret) { 1734 if (ret) {
1706 mlog_errno(ret); 1735 mlog_errno(ret);
1707 goto out_meta_unlock; 1736 goto out_inode_unlock;
1708 } 1737 }
1709 1738
1710 /* 1739 /*
@@ -1714,7 +1743,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1714 if (IS_ERR(handle)) { 1743 if (IS_ERR(handle)) {
1715 ret = PTR_ERR(handle); 1744 ret = PTR_ERR(handle);
1716 mlog_errno(ret); 1745 mlog_errno(ret);
1717 goto out_meta_unlock; 1746 goto out_inode_unlock;
1718 } 1747 }
1719 1748
1720 if (change_size && i_size_read(inode) < size) 1749 if (change_size && i_size_read(inode) < size)
@@ -1727,9 +1756,9 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1727 1756
1728 ocfs2_commit_trans(osb, handle); 1757 ocfs2_commit_trans(osb, handle);
1729 1758
1730out_meta_unlock: 1759out_inode_unlock:
1731 brelse(di_bh); 1760 brelse(di_bh);
1732 ocfs2_meta_unlock(inode, 1); 1761 ocfs2_inode_unlock(inode, 1);
1733out_rw_unlock: 1762out_rw_unlock:
1734 ocfs2_rw_unlock(inode, 1); 1763 ocfs2_rw_unlock(inode, 1);
1735 1764
@@ -1799,7 +1828,7 @@ static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1799 * if we need to make modifications here. 1828 * if we need to make modifications here.
1800 */ 1829 */
1801 for(;;) { 1830 for(;;) {
1802 ret = ocfs2_meta_lock(inode, NULL, meta_level); 1831 ret = ocfs2_inode_lock(inode, NULL, meta_level);
1803 if (ret < 0) { 1832 if (ret < 0) {
1804 meta_level = -1; 1833 meta_level = -1;
1805 mlog_errno(ret); 1834 mlog_errno(ret);
@@ -1817,7 +1846,7 @@ static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1817 * set inode->i_size at the end of a write. */ 1846 * set inode->i_size at the end of a write. */
1818 if (should_remove_suid(dentry)) { 1847 if (should_remove_suid(dentry)) {
1819 if (meta_level == 0) { 1848 if (meta_level == 0) {
1820 ocfs2_meta_unlock(inode, meta_level); 1849 ocfs2_inode_unlock(inode, meta_level);
1821 meta_level = 1; 1850 meta_level = 1;
1822 continue; 1851 continue;
1823 } 1852 }
@@ -1886,7 +1915,7 @@ static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1886 *ppos = saved_pos; 1915 *ppos = saved_pos;
1887 1916
1888out_unlock: 1917out_unlock:
1889 ocfs2_meta_unlock(inode, meta_level); 1918 ocfs2_inode_unlock(inode, meta_level);
1890 1919
1891out: 1920out:
1892 return ret; 1921 return ret;
@@ -2099,12 +2128,12 @@ static ssize_t ocfs2_file_splice_read(struct file *in,
2099 /* 2128 /*
2100 * See the comment in ocfs2_file_aio_read() 2129 * See the comment in ocfs2_file_aio_read()
2101 */ 2130 */
2102 ret = ocfs2_meta_lock(inode, NULL, 0); 2131 ret = ocfs2_inode_lock(inode, NULL, 0);
2103 if (ret < 0) { 2132 if (ret < 0) {
2104 mlog_errno(ret); 2133 mlog_errno(ret);
2105 goto bail; 2134 goto bail;
2106 } 2135 }
2107 ocfs2_meta_unlock(inode, 0); 2136 ocfs2_inode_unlock(inode, 0);
2108 2137
2109 ret = generic_file_splice_read(in, ppos, pipe, len, flags); 2138 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
2110 2139
@@ -2160,12 +2189,12 @@ static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
2160 * like i_size. This allows the checks down below 2189 * like i_size. This allows the checks down below
2161 * generic_file_aio_read() a chance of actually working. 2190 * generic_file_aio_read() a chance of actually working.
2162 */ 2191 */
2163 ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level); 2192 ret = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
2164 if (ret < 0) { 2193 if (ret < 0) {
2165 mlog_errno(ret); 2194 mlog_errno(ret);
2166 goto bail; 2195 goto bail;
2167 } 2196 }
2168 ocfs2_meta_unlock(inode, lock_level); 2197 ocfs2_inode_unlock(inode, lock_level);
2169 2198
2170 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos); 2199 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
2171 if (ret == -EINVAL) 2200 if (ret == -EINVAL)
@@ -2204,6 +2233,7 @@ const struct inode_operations ocfs2_special_file_iops = {
2204}; 2233};
2205 2234
2206const struct file_operations ocfs2_fops = { 2235const struct file_operations ocfs2_fops = {
2236 .llseek = generic_file_llseek,
2207 .read = do_sync_read, 2237 .read = do_sync_read,
2208 .write = do_sync_write, 2238 .write = do_sync_write,
2209 .mmap = ocfs2_mmap, 2239 .mmap = ocfs2_mmap,
@@ -2216,16 +2246,21 @@ const struct file_operations ocfs2_fops = {
2216#ifdef CONFIG_COMPAT 2246#ifdef CONFIG_COMPAT
2217 .compat_ioctl = ocfs2_compat_ioctl, 2247 .compat_ioctl = ocfs2_compat_ioctl,
2218#endif 2248#endif
2249 .flock = ocfs2_flock,
2219 .splice_read = ocfs2_file_splice_read, 2250 .splice_read = ocfs2_file_splice_read,
2220 .splice_write = ocfs2_file_splice_write, 2251 .splice_write = ocfs2_file_splice_write,
2221}; 2252};
2222 2253
2223const struct file_operations ocfs2_dops = { 2254const struct file_operations ocfs2_dops = {
2255 .llseek = generic_file_llseek,
2224 .read = generic_read_dir, 2256 .read = generic_read_dir,
2225 .readdir = ocfs2_readdir, 2257 .readdir = ocfs2_readdir,
2226 .fsync = ocfs2_sync_file, 2258 .fsync = ocfs2_sync_file,
2259 .release = ocfs2_dir_release,
2260 .open = ocfs2_dir_open,
2227 .ioctl = ocfs2_ioctl, 2261 .ioctl = ocfs2_ioctl,
2228#ifdef CONFIG_COMPAT 2262#ifdef CONFIG_COMPAT
2229 .compat_ioctl = ocfs2_compat_ioctl, 2263 .compat_ioctl = ocfs2_compat_ioctl,
2230#endif 2264#endif
2265 .flock = ocfs2_flock,
2231}; 2266};
diff --git a/fs/ocfs2/file.h b/fs/ocfs2/file.h
index 066f14add3a8..048ddcaf5c80 100644
--- a/fs/ocfs2/file.h
+++ b/fs/ocfs2/file.h
@@ -32,6 +32,12 @@ extern const struct inode_operations ocfs2_file_iops;
32extern const struct inode_operations ocfs2_special_file_iops; 32extern const struct inode_operations ocfs2_special_file_iops;
33struct ocfs2_alloc_context; 33struct ocfs2_alloc_context;
34 34
35struct ocfs2_file_private {
36 struct file *fp_file;
37 struct mutex fp_mutex;
38 struct ocfs2_lock_res fp_flock;
39};
40
35enum ocfs2_alloc_restarted { 41enum ocfs2_alloc_restarted {
36 RESTART_NONE = 0, 42 RESTART_NONE = 0,
37 RESTART_TRANS, 43 RESTART_TRANS,
diff --git a/fs/ocfs2/heartbeat.c b/fs/ocfs2/heartbeat.c
index c4c36171240d..c0efd9489fe8 100644
--- a/fs/ocfs2/heartbeat.c
+++ b/fs/ocfs2/heartbeat.c
@@ -30,9 +30,6 @@
30#include <linux/highmem.h> 30#include <linux/highmem.h>
31#include <linux/kmod.h> 31#include <linux/kmod.h>
32 32
33#include <cluster/heartbeat.h>
34#include <cluster/nodemanager.h>
35
36#include <dlm/dlmapi.h> 33#include <dlm/dlmapi.h>
37 34
38#define MLOG_MASK_PREFIX ML_SUPER 35#define MLOG_MASK_PREFIX ML_SUPER
@@ -44,13 +41,9 @@
44#include "heartbeat.h" 41#include "heartbeat.h"
45#include "inode.h" 42#include "inode.h"
46#include "journal.h" 43#include "journal.h"
47#include "vote.h"
48 44
49#include "buffer_head_io.h" 45#include "buffer_head_io.h"
50 46
51#define OCFS2_HB_NODE_DOWN_PRI (0x0000002)
52#define OCFS2_HB_NODE_UP_PRI OCFS2_HB_NODE_DOWN_PRI
53
54static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map, 47static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
55 int bit); 48 int bit);
56static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map, 49static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
@@ -64,9 +57,7 @@ static void __ocfs2_node_map_set(struct ocfs2_node_map *target,
64void ocfs2_init_node_maps(struct ocfs2_super *osb) 57void ocfs2_init_node_maps(struct ocfs2_super *osb)
65{ 58{
66 spin_lock_init(&osb->node_map_lock); 59 spin_lock_init(&osb->node_map_lock);
67 ocfs2_node_map_init(&osb->mounted_map);
68 ocfs2_node_map_init(&osb->recovery_map); 60 ocfs2_node_map_init(&osb->recovery_map);
69 ocfs2_node_map_init(&osb->umount_map);
70 ocfs2_node_map_init(&osb->osb_recovering_orphan_dirs); 61 ocfs2_node_map_init(&osb->osb_recovering_orphan_dirs);
71} 62}
72 63
@@ -87,24 +78,7 @@ static void ocfs2_do_node_down(int node_num,
87 return; 78 return;
88 } 79 }
89 80
90 if (ocfs2_node_map_test_bit(osb, &osb->umount_map, node_num)) {
91 /* If a node is in the umount map, then we've been
92 * expecting him to go down and we know ahead of time
93 * that recovery is not necessary. */
94 ocfs2_node_map_clear_bit(osb, &osb->umount_map, node_num);
95 return;
96 }
97
98 ocfs2_recovery_thread(osb, node_num); 81 ocfs2_recovery_thread(osb, node_num);
99
100 ocfs2_remove_node_from_vote_queues(osb, node_num);
101}
102
103static void ocfs2_hb_node_down_cb(struct o2nm_node *node,
104 int node_num,
105 void *data)
106{
107 ocfs2_do_node_down(node_num, (struct ocfs2_super *) data);
108} 82}
109 83
110/* Called from the dlm when it's about to evict a node. We may also 84/* Called from the dlm when it's about to evict a node. We may also
@@ -121,27 +95,8 @@ static void ocfs2_dlm_eviction_cb(int node_num,
121 ocfs2_do_node_down(node_num, osb); 95 ocfs2_do_node_down(node_num, osb);
122} 96}
123 97
124static void ocfs2_hb_node_up_cb(struct o2nm_node *node,
125 int node_num,
126 void *data)
127{
128 struct ocfs2_super *osb = data;
129
130 BUG_ON(osb->node_num == node_num);
131
132 mlog(0, "node up event for %d\n", node_num);
133 ocfs2_node_map_clear_bit(osb, &osb->umount_map, node_num);
134}
135
136void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb) 98void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb)
137{ 99{
138 o2hb_setup_callback(&osb->osb_hb_down, O2HB_NODE_DOWN_CB,
139 ocfs2_hb_node_down_cb, osb,
140 OCFS2_HB_NODE_DOWN_PRI);
141
142 o2hb_setup_callback(&osb->osb_hb_up, O2HB_NODE_UP_CB,
143 ocfs2_hb_node_up_cb, osb, OCFS2_HB_NODE_UP_PRI);
144
145 /* Not exactly a heartbeat callback, but leads to essentially 100 /* Not exactly a heartbeat callback, but leads to essentially
146 * the same path so we set it up here. */ 101 * the same path so we set it up here. */
147 dlm_setup_eviction_cb(&osb->osb_eviction_cb, 102 dlm_setup_eviction_cb(&osb->osb_eviction_cb,
@@ -149,39 +104,6 @@ void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb)
149 osb); 104 osb);
150} 105}
151 106
152/* Most functions here are just stubs for now... */
153int ocfs2_register_hb_callbacks(struct ocfs2_super *osb)
154{
155 int status;
156
157 if (ocfs2_mount_local(osb))
158 return 0;
159
160 status = o2hb_register_callback(osb->uuid_str, &osb->osb_hb_down);
161 if (status < 0) {
162 mlog_errno(status);
163 goto bail;
164 }
165
166 status = o2hb_register_callback(osb->uuid_str, &osb->osb_hb_up);
167 if (status < 0) {
168 mlog_errno(status);
169 o2hb_unregister_callback(osb->uuid_str, &osb->osb_hb_down);
170 }
171
172bail:
173 return status;
174}
175
176void ocfs2_clear_hb_callbacks(struct ocfs2_super *osb)
177{
178 if (ocfs2_mount_local(osb))
179 return;
180
181 o2hb_unregister_callback(osb->uuid_str, &osb->osb_hb_down);
182 o2hb_unregister_callback(osb->uuid_str, &osb->osb_hb_up);
183}
184
185void ocfs2_stop_heartbeat(struct ocfs2_super *osb) 107void ocfs2_stop_heartbeat(struct ocfs2_super *osb)
186{ 108{
187 int ret; 109 int ret;
@@ -341,8 +263,6 @@ int ocfs2_recovery_map_set(struct ocfs2_super *osb,
341 263
342 spin_lock(&osb->node_map_lock); 264 spin_lock(&osb->node_map_lock);
343 265
344 __ocfs2_node_map_clear_bit(&osb->mounted_map, num);
345
346 if (!test_bit(num, osb->recovery_map.map)) { 266 if (!test_bit(num, osb->recovery_map.map)) {
347 __ocfs2_node_map_set_bit(&osb->recovery_map, num); 267 __ocfs2_node_map_set_bit(&osb->recovery_map, num);
348 set = 1; 268 set = 1;
diff --git a/fs/ocfs2/heartbeat.h b/fs/ocfs2/heartbeat.h
index e8fb079122e4..56859211888a 100644
--- a/fs/ocfs2/heartbeat.h
+++ b/fs/ocfs2/heartbeat.h
@@ -29,8 +29,6 @@
29void ocfs2_init_node_maps(struct ocfs2_super *osb); 29void ocfs2_init_node_maps(struct ocfs2_super *osb);
30 30
31void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb); 31void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb);
32int ocfs2_register_hb_callbacks(struct ocfs2_super *osb);
33void ocfs2_clear_hb_callbacks(struct ocfs2_super *osb);
34void ocfs2_stop_heartbeat(struct ocfs2_super *osb); 32void ocfs2_stop_heartbeat(struct ocfs2_super *osb);
35 33
36/* node map functions - used to keep track of mounted and in-recovery 34/* node map functions - used to keep track of mounted and in-recovery
diff --git a/fs/ocfs2/inode.c b/fs/ocfs2/inode.c
index ebb2bbe30f35..7e9e4c79aec7 100644
--- a/fs/ocfs2/inode.c
+++ b/fs/ocfs2/inode.c
@@ -49,7 +49,6 @@
49#include "symlink.h" 49#include "symlink.h"
50#include "sysfile.h" 50#include "sysfile.h"
51#include "uptodate.h" 51#include "uptodate.h"
52#include "vote.h"
53 52
54#include "buffer_head_io.h" 53#include "buffer_head_io.h"
55 54
@@ -58,8 +57,11 @@ struct ocfs2_find_inode_args
58 u64 fi_blkno; 57 u64 fi_blkno;
59 unsigned long fi_ino; 58 unsigned long fi_ino;
60 unsigned int fi_flags; 59 unsigned int fi_flags;
60 unsigned int fi_sysfile_type;
61}; 61};
62 62
63static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
64
63static int ocfs2_read_locked_inode(struct inode *inode, 65static int ocfs2_read_locked_inode(struct inode *inode,
64 struct ocfs2_find_inode_args *args); 66 struct ocfs2_find_inode_args *args);
65static int ocfs2_init_locked_inode(struct inode *inode, void *opaque); 67static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
@@ -107,7 +109,8 @@ void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
107 oi->ip_attr |= OCFS2_DIRSYNC_FL; 109 oi->ip_attr |= OCFS2_DIRSYNC_FL;
108} 110}
109 111
110struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, int flags) 112struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
113 int sysfile_type)
111{ 114{
112 struct inode *inode = NULL; 115 struct inode *inode = NULL;
113 struct super_block *sb = osb->sb; 116 struct super_block *sb = osb->sb;
@@ -127,6 +130,7 @@ struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, int flags)
127 args.fi_blkno = blkno; 130 args.fi_blkno = blkno;
128 args.fi_flags = flags; 131 args.fi_flags = flags;
129 args.fi_ino = ino_from_blkno(sb, blkno); 132 args.fi_ino = ino_from_blkno(sb, blkno);
133 args.fi_sysfile_type = sysfile_type;
130 134
131 inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor, 135 inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
132 ocfs2_init_locked_inode, &args); 136 ocfs2_init_locked_inode, &args);
@@ -201,6 +205,9 @@ static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
201 205
202 inode->i_ino = args->fi_ino; 206 inode->i_ino = args->fi_ino;
203 OCFS2_I(inode)->ip_blkno = args->fi_blkno; 207 OCFS2_I(inode)->ip_blkno = args->fi_blkno;
208 if (args->fi_sysfile_type != 0)
209 lockdep_set_class(&inode->i_mutex,
210 &ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
204 211
205 mlog_exit(0); 212 mlog_exit(0);
206 return 0; 213 return 0;
@@ -322,7 +329,7 @@ int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
322 */ 329 */
323 BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL); 330 BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
324 331
325 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_meta_lockres, 332 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
326 OCFS2_LOCK_TYPE_META, 0, inode); 333 OCFS2_LOCK_TYPE_META, 0, inode);
327 334
328 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, 335 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
@@ -333,10 +340,6 @@ int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
333 OCFS2_LOCK_TYPE_RW, inode->i_generation, 340 OCFS2_LOCK_TYPE_RW, inode->i_generation,
334 inode); 341 inode);
335 342
336 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_data_lockres,
337 OCFS2_LOCK_TYPE_DATA, inode->i_generation,
338 inode);
339
340 ocfs2_set_inode_flags(inode); 343 ocfs2_set_inode_flags(inode);
341 344
342 status = 0; 345 status = 0;
@@ -414,7 +417,7 @@ static int ocfs2_read_locked_inode(struct inode *inode,
414 if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE) 417 if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
415 generation = osb->fs_generation; 418 generation = osb->fs_generation;
416 419
417 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_meta_lockres, 420 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
418 OCFS2_LOCK_TYPE_META, 421 OCFS2_LOCK_TYPE_META,
419 generation, inode); 422 generation, inode);
420 423
@@ -429,7 +432,7 @@ static int ocfs2_read_locked_inode(struct inode *inode,
429 mlog_errno(status); 432 mlog_errno(status);
430 return status; 433 return status;
431 } 434 }
432 status = ocfs2_meta_lock(inode, NULL, 0); 435 status = ocfs2_inode_lock(inode, NULL, 0);
433 if (status) { 436 if (status) {
434 make_bad_inode(inode); 437 make_bad_inode(inode);
435 mlog_errno(status); 438 mlog_errno(status);
@@ -484,7 +487,7 @@ static int ocfs2_read_locked_inode(struct inode *inode,
484 487
485bail: 488bail:
486 if (can_lock) 489 if (can_lock)
487 ocfs2_meta_unlock(inode, 0); 490 ocfs2_inode_unlock(inode, 0);
488 491
489 if (status < 0) 492 if (status < 0)
490 make_bad_inode(inode); 493 make_bad_inode(inode);
@@ -586,7 +589,7 @@ static int ocfs2_remove_inode(struct inode *inode,
586 } 589 }
587 590
588 mutex_lock(&inode_alloc_inode->i_mutex); 591 mutex_lock(&inode_alloc_inode->i_mutex);
589 status = ocfs2_meta_lock(inode_alloc_inode, &inode_alloc_bh, 1); 592 status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
590 if (status < 0) { 593 if (status < 0) {
591 mutex_unlock(&inode_alloc_inode->i_mutex); 594 mutex_unlock(&inode_alloc_inode->i_mutex);
592 595
@@ -617,7 +620,7 @@ static int ocfs2_remove_inode(struct inode *inode,
617 } 620 }
618 621
619 di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec); 622 di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
620 le32_and_cpu(&di->i_flags, ~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL)); 623 di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
621 624
622 status = ocfs2_journal_dirty(handle, di_bh); 625 status = ocfs2_journal_dirty(handle, di_bh);
623 if (status < 0) { 626 if (status < 0) {
@@ -635,7 +638,7 @@ static int ocfs2_remove_inode(struct inode *inode,
635bail_commit: 638bail_commit:
636 ocfs2_commit_trans(osb, handle); 639 ocfs2_commit_trans(osb, handle);
637bail_unlock: 640bail_unlock:
638 ocfs2_meta_unlock(inode_alloc_inode, 1); 641 ocfs2_inode_unlock(inode_alloc_inode, 1);
639 mutex_unlock(&inode_alloc_inode->i_mutex); 642 mutex_unlock(&inode_alloc_inode->i_mutex);
640 brelse(inode_alloc_bh); 643 brelse(inode_alloc_bh);
641bail: 644bail:
@@ -709,7 +712,7 @@ static int ocfs2_wipe_inode(struct inode *inode,
709 * delete_inode operation. We do this now to avoid races with 712 * delete_inode operation. We do this now to avoid races with
710 * recovery completion on other nodes. */ 713 * recovery completion on other nodes. */
711 mutex_lock(&orphan_dir_inode->i_mutex); 714 mutex_lock(&orphan_dir_inode->i_mutex);
712 status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1); 715 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
713 if (status < 0) { 716 if (status < 0) {
714 mutex_unlock(&orphan_dir_inode->i_mutex); 717 mutex_unlock(&orphan_dir_inode->i_mutex);
715 718
@@ -718,8 +721,8 @@ static int ocfs2_wipe_inode(struct inode *inode,
718 } 721 }
719 722
720 /* we do this while holding the orphan dir lock because we 723 /* we do this while holding the orphan dir lock because we
721 * don't want recovery being run from another node to vote for 724 * don't want recovery being run from another node to try an
722 * an inode delete on us -- this will result in two nodes 725 * inode delete underneath us -- this will result in two nodes
723 * truncating the same file! */ 726 * truncating the same file! */
724 status = ocfs2_truncate_for_delete(osb, inode, di_bh); 727 status = ocfs2_truncate_for_delete(osb, inode, di_bh);
725 if (status < 0) { 728 if (status < 0) {
@@ -733,7 +736,7 @@ static int ocfs2_wipe_inode(struct inode *inode,
733 mlog_errno(status); 736 mlog_errno(status);
734 737
735bail_unlock_dir: 738bail_unlock_dir:
736 ocfs2_meta_unlock(orphan_dir_inode, 1); 739 ocfs2_inode_unlock(orphan_dir_inode, 1);
737 mutex_unlock(&orphan_dir_inode->i_mutex); 740 mutex_unlock(&orphan_dir_inode->i_mutex);
738 brelse(orphan_dir_bh); 741 brelse(orphan_dir_bh);
739bail: 742bail:
@@ -744,7 +747,7 @@ bail:
744} 747}
745 748
746/* There is a series of simple checks that should be done before a 749/* There is a series of simple checks that should be done before a
747 * vote is even considered. Encapsulate those in this function. */ 750 * trylock is even considered. Encapsulate those in this function. */
748static int ocfs2_inode_is_valid_to_delete(struct inode *inode) 751static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
749{ 752{
750 int ret = 0; 753 int ret = 0;
@@ -758,14 +761,14 @@ static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
758 goto bail; 761 goto bail;
759 } 762 }
760 763
761 /* If we're coming from process_vote we can't go into our own 764 /* If we're coming from downconvert_thread we can't go into our own
762 * voting [hello, deadlock city!], so unforuntately we just 765 * voting [hello, deadlock city!], so unforuntately we just
763 * have to skip deleting this guy. That's OK though because 766 * have to skip deleting this guy. That's OK though because
764 * the node who's doing the actual deleting should handle it 767 * the node who's doing the actual deleting should handle it
765 * anyway. */ 768 * anyway. */
766 if (current == osb->vote_task) { 769 if (current == osb->dc_task) {
767 mlog(0, "Skipping delete of %lu because we're currently " 770 mlog(0, "Skipping delete of %lu because we're currently "
768 "in process_vote\n", inode->i_ino); 771 "in downconvert\n", inode->i_ino);
769 goto bail; 772 goto bail;
770 } 773 }
771 774
@@ -779,10 +782,9 @@ static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
779 goto bail_unlock; 782 goto bail_unlock;
780 } 783 }
781 784
782 /* If we have voted "yes" on the wipe of this inode for 785 /* If we have allowd wipe of this inode for another node, it
783 * another node, it will be marked here so we can safely skip 786 * will be marked here so we can safely skip it. Recovery will
784 * it. Recovery will cleanup any inodes we might inadvertantly 787 * cleanup any inodes we might inadvertantly skip here. */
785 * skip here. */
786 if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) { 788 if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
787 mlog(0, "Skipping delete of %lu because another node " 789 mlog(0, "Skipping delete of %lu because another node "
788 "has done this for us.\n", inode->i_ino); 790 "has done this for us.\n", inode->i_ino);
@@ -929,13 +931,13 @@ void ocfs2_delete_inode(struct inode *inode)
929 931
930 /* Lock down the inode. This gives us an up to date view of 932 /* Lock down the inode. This gives us an up to date view of
931 * it's metadata (for verification), and allows us to 933 * it's metadata (for verification), and allows us to
932 * serialize delete_inode votes. 934 * serialize delete_inode on multiple nodes.
933 * 935 *
934 * Even though we might be doing a truncate, we don't take the 936 * Even though we might be doing a truncate, we don't take the
935 * allocation lock here as it won't be needed - nobody will 937 * allocation lock here as it won't be needed - nobody will
936 * have the file open. 938 * have the file open.
937 */ 939 */
938 status = ocfs2_meta_lock(inode, &di_bh, 1); 940 status = ocfs2_inode_lock(inode, &di_bh, 1);
939 if (status < 0) { 941 if (status < 0) {
940 if (status != -ENOENT) 942 if (status != -ENOENT)
941 mlog_errno(status); 943 mlog_errno(status);
@@ -947,15 +949,15 @@ void ocfs2_delete_inode(struct inode *inode)
947 * before we go ahead and wipe the inode. */ 949 * before we go ahead and wipe the inode. */
948 status = ocfs2_query_inode_wipe(inode, di_bh, &wipe); 950 status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
949 if (!wipe || status < 0) { 951 if (!wipe || status < 0) {
950 /* Error and inode busy vote both mean we won't be 952 /* Error and remote inode busy both mean we won't be
951 * removing the inode, so they take almost the same 953 * removing the inode, so they take almost the same
952 * path. */ 954 * path. */
953 if (status < 0) 955 if (status < 0)
954 mlog_errno(status); 956 mlog_errno(status);
955 957
956 /* Someone in the cluster has voted to not wipe this 958 /* Someone in the cluster has disallowed a wipe of
957 * inode, or it was never completely orphaned. Write 959 * this inode, or it was never completely
958 * out the pages and exit now. */ 960 * orphaned. Write out the pages and exit now. */
959 ocfs2_cleanup_delete_inode(inode, 1); 961 ocfs2_cleanup_delete_inode(inode, 1);
960 goto bail_unlock_inode; 962 goto bail_unlock_inode;
961 } 963 }
@@ -981,7 +983,7 @@ void ocfs2_delete_inode(struct inode *inode)
981 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED; 983 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
982 984
983bail_unlock_inode: 985bail_unlock_inode:
984 ocfs2_meta_unlock(inode, 1); 986 ocfs2_inode_unlock(inode, 1);
985 brelse(di_bh); 987 brelse(di_bh);
986bail_unblock: 988bail_unblock:
987 status = sigprocmask(SIG_SETMASK, &oldset, NULL); 989 status = sigprocmask(SIG_SETMASK, &oldset, NULL);
@@ -1008,15 +1010,14 @@ void ocfs2_clear_inode(struct inode *inode)
1008 mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL, 1010 mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
1009 "Inode=%lu\n", inode->i_ino); 1011 "Inode=%lu\n", inode->i_ino);
1010 1012
1011 /* For remove delete_inode vote, we hold open lock before, 1013 /* To preven remote deletes we hold open lock before, now it
1012 * now it is time to unlock PR and EX open locks. */ 1014 * is time to unlock PR and EX open locks. */
1013 ocfs2_open_unlock(inode); 1015 ocfs2_open_unlock(inode);
1014 1016
1015 /* Do these before all the other work so that we don't bounce 1017 /* Do these before all the other work so that we don't bounce
1016 * the vote thread while waiting to destroy the locks. */ 1018 * the downconvert thread while waiting to destroy the locks. */
1017 ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres); 1019 ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
1018 ocfs2_mark_lockres_freeing(&oi->ip_meta_lockres); 1020 ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
1019 ocfs2_mark_lockres_freeing(&oi->ip_data_lockres);
1020 ocfs2_mark_lockres_freeing(&oi->ip_open_lockres); 1021 ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
1021 1022
1022 /* We very well may get a clear_inode before all an inodes 1023 /* We very well may get a clear_inode before all an inodes
@@ -1039,8 +1040,7 @@ void ocfs2_clear_inode(struct inode *inode)
1039 mlog_errno(status); 1040 mlog_errno(status);
1040 1041
1041 ocfs2_lock_res_free(&oi->ip_rw_lockres); 1042 ocfs2_lock_res_free(&oi->ip_rw_lockres);
1042 ocfs2_lock_res_free(&oi->ip_meta_lockres); 1043 ocfs2_lock_res_free(&oi->ip_inode_lockres);
1043 ocfs2_lock_res_free(&oi->ip_data_lockres);
1044 ocfs2_lock_res_free(&oi->ip_open_lockres); 1044 ocfs2_lock_res_free(&oi->ip_open_lockres);
1045 1045
1046 ocfs2_metadata_cache_purge(inode); 1046 ocfs2_metadata_cache_purge(inode);
@@ -1184,15 +1184,15 @@ int ocfs2_inode_revalidate(struct dentry *dentry)
1184 } 1184 }
1185 spin_unlock(&OCFS2_I(inode)->ip_lock); 1185 spin_unlock(&OCFS2_I(inode)->ip_lock);
1186 1186
1187 /* Let ocfs2_meta_lock do the work of updating our struct 1187 /* Let ocfs2_inode_lock do the work of updating our struct
1188 * inode for us. */ 1188 * inode for us. */
1189 status = ocfs2_meta_lock(inode, NULL, 0); 1189 status = ocfs2_inode_lock(inode, NULL, 0);
1190 if (status < 0) { 1190 if (status < 0) {
1191 if (status != -ENOENT) 1191 if (status != -ENOENT)
1192 mlog_errno(status); 1192 mlog_errno(status);
1193 goto bail; 1193 goto bail;
1194 } 1194 }
1195 ocfs2_meta_unlock(inode, 0); 1195 ocfs2_inode_unlock(inode, 0);
1196bail: 1196bail:
1197 mlog_exit(status); 1197 mlog_exit(status);
1198 1198
diff --git a/fs/ocfs2/inode.h b/fs/ocfs2/inode.h
index 70e881c55536..390a85596aa0 100644
--- a/fs/ocfs2/inode.h
+++ b/fs/ocfs2/inode.h
@@ -34,8 +34,7 @@ struct ocfs2_inode_info
34 u64 ip_blkno; 34 u64 ip_blkno;
35 35
36 struct ocfs2_lock_res ip_rw_lockres; 36 struct ocfs2_lock_res ip_rw_lockres;
37 struct ocfs2_lock_res ip_meta_lockres; 37 struct ocfs2_lock_res ip_inode_lockres;
38 struct ocfs2_lock_res ip_data_lockres;
39 struct ocfs2_lock_res ip_open_lockres; 38 struct ocfs2_lock_res ip_open_lockres;
40 39
41 /* protects allocation changes on this inode. */ 40 /* protects allocation changes on this inode. */
@@ -121,9 +120,10 @@ void ocfs2_delete_inode(struct inode *inode);
121void ocfs2_drop_inode(struct inode *inode); 120void ocfs2_drop_inode(struct inode *inode);
122 121
123/* Flags for ocfs2_iget() */ 122/* Flags for ocfs2_iget() */
124#define OCFS2_FI_FLAG_SYSFILE 0x4 123#define OCFS2_FI_FLAG_SYSFILE 0x1
125#define OCFS2_FI_FLAG_ORPHAN_RECOVERY 0x8 124#define OCFS2_FI_FLAG_ORPHAN_RECOVERY 0x2
126struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 feoff, int flags); 125struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 feoff, unsigned flags,
126 int sysfile_type);
127int ocfs2_inode_init_private(struct inode *inode); 127int ocfs2_inode_init_private(struct inode *inode);
128int ocfs2_inode_revalidate(struct dentry *dentry); 128int ocfs2_inode_revalidate(struct dentry *dentry);
129int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe, 129int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
diff --git a/fs/ocfs2/ioctl.c b/fs/ocfs2/ioctl.c
index 87dcece7e1b5..5177fba5162b 100644
--- a/fs/ocfs2/ioctl.c
+++ b/fs/ocfs2/ioctl.c
@@ -20,6 +20,7 @@
20 20
21#include "ocfs2_fs.h" 21#include "ocfs2_fs.h"
22#include "ioctl.h" 22#include "ioctl.h"
23#include "resize.h"
23 24
24#include <linux/ext2_fs.h> 25#include <linux/ext2_fs.h>
25 26
@@ -27,14 +28,14 @@ static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
27{ 28{
28 int status; 29 int status;
29 30
30 status = ocfs2_meta_lock(inode, NULL, 0); 31 status = ocfs2_inode_lock(inode, NULL, 0);
31 if (status < 0) { 32 if (status < 0) {
32 mlog_errno(status); 33 mlog_errno(status);
33 return status; 34 return status;
34 } 35 }
35 ocfs2_get_inode_flags(OCFS2_I(inode)); 36 ocfs2_get_inode_flags(OCFS2_I(inode));
36 *flags = OCFS2_I(inode)->ip_attr; 37 *flags = OCFS2_I(inode)->ip_attr;
37 ocfs2_meta_unlock(inode, 0); 38 ocfs2_inode_unlock(inode, 0);
38 39
39 mlog_exit(status); 40 mlog_exit(status);
40 return status; 41 return status;
@@ -52,7 +53,7 @@ static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
52 53
53 mutex_lock(&inode->i_mutex); 54 mutex_lock(&inode->i_mutex);
54 55
55 status = ocfs2_meta_lock(inode, &bh, 1); 56 status = ocfs2_inode_lock(inode, &bh, 1);
56 if (status < 0) { 57 if (status < 0) {
57 mlog_errno(status); 58 mlog_errno(status);
58 goto bail; 59 goto bail;
@@ -100,7 +101,7 @@ static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
100 101
101 ocfs2_commit_trans(osb, handle); 102 ocfs2_commit_trans(osb, handle);
102bail_unlock: 103bail_unlock:
103 ocfs2_meta_unlock(inode, 1); 104 ocfs2_inode_unlock(inode, 1);
104bail: 105bail:
105 mutex_unlock(&inode->i_mutex); 106 mutex_unlock(&inode->i_mutex);
106 107
@@ -115,8 +116,10 @@ int ocfs2_ioctl(struct inode * inode, struct file * filp,
115 unsigned int cmd, unsigned long arg) 116 unsigned int cmd, unsigned long arg)
116{ 117{
117 unsigned int flags; 118 unsigned int flags;
119 int new_clusters;
118 int status; 120 int status;
119 struct ocfs2_space_resv sr; 121 struct ocfs2_space_resv sr;
122 struct ocfs2_new_group_input input;
120 123
121 switch (cmd) { 124 switch (cmd) {
122 case OCFS2_IOC_GETFLAGS: 125 case OCFS2_IOC_GETFLAGS:
@@ -140,6 +143,23 @@ int ocfs2_ioctl(struct inode * inode, struct file * filp,
140 return -EFAULT; 143 return -EFAULT;
141 144
142 return ocfs2_change_file_space(filp, cmd, &sr); 145 return ocfs2_change_file_space(filp, cmd, &sr);
146 case OCFS2_IOC_GROUP_EXTEND:
147 if (!capable(CAP_SYS_RESOURCE))
148 return -EPERM;
149
150 if (get_user(new_clusters, (int __user *)arg))
151 return -EFAULT;
152
153 return ocfs2_group_extend(inode, new_clusters);
154 case OCFS2_IOC_GROUP_ADD:
155 case OCFS2_IOC_GROUP_ADD64:
156 if (!capable(CAP_SYS_RESOURCE))
157 return -EPERM;
158
159 if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
160 return -EFAULT;
161
162 return ocfs2_group_add(inode, &input);
143 default: 163 default:
144 return -ENOTTY; 164 return -ENOTTY;
145 } 165 }
@@ -162,6 +182,9 @@ long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
162 case OCFS2_IOC_RESVSP64: 182 case OCFS2_IOC_RESVSP64:
163 case OCFS2_IOC_UNRESVSP: 183 case OCFS2_IOC_UNRESVSP:
164 case OCFS2_IOC_UNRESVSP64: 184 case OCFS2_IOC_UNRESVSP64:
185 case OCFS2_IOC_GROUP_EXTEND:
186 case OCFS2_IOC_GROUP_ADD:
187 case OCFS2_IOC_GROUP_ADD64:
165 break; 188 break;
166 default: 189 default:
167 return -ENOIOCTLCMD; 190 return -ENOIOCTLCMD;
diff --git a/fs/ocfs2/journal.c b/fs/ocfs2/journal.c
index 8d81f6c1b877..f31c7e8c19c3 100644
--- a/fs/ocfs2/journal.c
+++ b/fs/ocfs2/journal.c
@@ -44,7 +44,6 @@
44#include "localalloc.h" 44#include "localalloc.h"
45#include "slot_map.h" 45#include "slot_map.h"
46#include "super.h" 46#include "super.h"
47#include "vote.h"
48#include "sysfile.h" 47#include "sysfile.h"
49 48
50#include "buffer_head_io.h" 49#include "buffer_head_io.h"
@@ -103,7 +102,7 @@ static int ocfs2_commit_cache(struct ocfs2_super *osb)
103 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n", 102 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
104 journal->j_trans_id, flushed); 103 journal->j_trans_id, flushed);
105 104
106 ocfs2_kick_vote_thread(osb); 105 ocfs2_wake_downconvert_thread(osb);
107 wake_up(&journal->j_checkpointed); 106 wake_up(&journal->j_checkpointed);
108finally: 107finally:
109 mlog_exit(status); 108 mlog_exit(status);
@@ -314,14 +313,18 @@ int ocfs2_journal_dirty_data(handle_t *handle,
314 return err; 313 return err;
315} 314}
316 315
317#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * 5) 316#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD_DEFAULT_MAX_COMMIT_AGE)
318 317
319void ocfs2_set_journal_params(struct ocfs2_super *osb) 318void ocfs2_set_journal_params(struct ocfs2_super *osb)
320{ 319{
321 journal_t *journal = osb->journal->j_journal; 320 journal_t *journal = osb->journal->j_journal;
321 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
322
323 if (osb->osb_commit_interval)
324 commit_interval = osb->osb_commit_interval;
322 325
323 spin_lock(&journal->j_state_lock); 326 spin_lock(&journal->j_state_lock);
324 journal->j_commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL; 327 journal->j_commit_interval = commit_interval;
325 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER) 328 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
326 journal->j_flags |= JFS_BARRIER; 329 journal->j_flags |= JFS_BARRIER;
327 else 330 else
@@ -337,7 +340,7 @@ int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
337 struct ocfs2_dinode *di = NULL; 340 struct ocfs2_dinode *di = NULL;
338 struct buffer_head *bh = NULL; 341 struct buffer_head *bh = NULL;
339 struct ocfs2_super *osb; 342 struct ocfs2_super *osb;
340 int meta_lock = 0; 343 int inode_lock = 0;
341 344
342 mlog_entry_void(); 345 mlog_entry_void();
343 346
@@ -367,14 +370,14 @@ int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
367 /* Skip recovery waits here - journal inode metadata never 370 /* Skip recovery waits here - journal inode metadata never
368 * changes in a live cluster so it can be considered an 371 * changes in a live cluster so it can be considered an
369 * exception to the rule. */ 372 * exception to the rule. */
370 status = ocfs2_meta_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY); 373 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
371 if (status < 0) { 374 if (status < 0) {
372 if (status != -ERESTARTSYS) 375 if (status != -ERESTARTSYS)
373 mlog(ML_ERROR, "Could not get lock on journal!\n"); 376 mlog(ML_ERROR, "Could not get lock on journal!\n");
374 goto done; 377 goto done;
375 } 378 }
376 379
377 meta_lock = 1; 380 inode_lock = 1;
378 di = (struct ocfs2_dinode *)bh->b_data; 381 di = (struct ocfs2_dinode *)bh->b_data;
379 382
380 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) { 383 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
@@ -414,8 +417,8 @@ int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
414 status = 0; 417 status = 0;
415done: 418done:
416 if (status < 0) { 419 if (status < 0) {
417 if (meta_lock) 420 if (inode_lock)
418 ocfs2_meta_unlock(inode, 1); 421 ocfs2_inode_unlock(inode, 1);
419 if (bh != NULL) 422 if (bh != NULL)
420 brelse(bh); 423 brelse(bh);
421 if (inode) { 424 if (inode) {
@@ -544,7 +547,7 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
544 OCFS2_I(inode)->ip_open_count--; 547 OCFS2_I(inode)->ip_open_count--;
545 548
546 /* unlock our journal */ 549 /* unlock our journal */
547 ocfs2_meta_unlock(inode, 1); 550 ocfs2_inode_unlock(inode, 1);
548 551
549 brelse(journal->j_bh); 552 brelse(journal->j_bh);
550 journal->j_bh = NULL; 553 journal->j_bh = NULL;
@@ -883,8 +886,8 @@ restart:
883 ocfs2_super_unlock(osb, 1); 886 ocfs2_super_unlock(osb, 1);
884 887
885 /* We always run recovery on our own orphan dir - the dead 888 /* We always run recovery on our own orphan dir - the dead
886 * node(s) may have voted "no" on an inode delete earlier. A 889 * node(s) may have disallowd a previos inode delete. Re-processing
887 * revote is therefore required. */ 890 * is therefore required. */
888 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL, 891 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
889 NULL); 892 NULL);
890 893
@@ -973,9 +976,9 @@ static int ocfs2_replay_journal(struct ocfs2_super *osb,
973 } 976 }
974 SET_INODE_JOURNAL(inode); 977 SET_INODE_JOURNAL(inode);
975 978
976 status = ocfs2_meta_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY); 979 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
977 if (status < 0) { 980 if (status < 0) {
978 mlog(0, "status returned from ocfs2_meta_lock=%d\n", status); 981 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
979 if (status != -ERESTARTSYS) 982 if (status != -ERESTARTSYS)
980 mlog(ML_ERROR, "Could not lock journal!\n"); 983 mlog(ML_ERROR, "Could not lock journal!\n");
981 goto done; 984 goto done;
@@ -1047,7 +1050,7 @@ static int ocfs2_replay_journal(struct ocfs2_super *osb,
1047done: 1050done:
1048 /* drop the lock on this nodes journal */ 1051 /* drop the lock on this nodes journal */
1049 if (got_lock) 1052 if (got_lock)
1050 ocfs2_meta_unlock(inode, 1); 1053 ocfs2_inode_unlock(inode, 1);
1051 1054
1052 if (inode) 1055 if (inode)
1053 iput(inode); 1056 iput(inode);
@@ -1162,14 +1165,14 @@ static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1162 SET_INODE_JOURNAL(inode); 1165 SET_INODE_JOURNAL(inode);
1163 1166
1164 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE; 1167 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
1165 status = ocfs2_meta_lock_full(inode, NULL, 1, flags); 1168 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
1166 if (status < 0) { 1169 if (status < 0) {
1167 if (status != -EAGAIN) 1170 if (status != -EAGAIN)
1168 mlog_errno(status); 1171 mlog_errno(status);
1169 goto bail; 1172 goto bail;
1170 } 1173 }
1171 1174
1172 ocfs2_meta_unlock(inode, 1); 1175 ocfs2_inode_unlock(inode, 1);
1173bail: 1176bail:
1174 if (inode) 1177 if (inode)
1175 iput(inode); 1178 iput(inode);
@@ -1241,7 +1244,7 @@ static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1241 1244
1242 /* Skip bad inodes so that recovery can continue */ 1245 /* Skip bad inodes so that recovery can continue */
1243 iter = ocfs2_iget(p->osb, ino, 1246 iter = ocfs2_iget(p->osb, ino,
1244 OCFS2_FI_FLAG_ORPHAN_RECOVERY); 1247 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
1245 if (IS_ERR(iter)) 1248 if (IS_ERR(iter))
1246 return 0; 1249 return 0;
1247 1250
@@ -1277,7 +1280,7 @@ static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1277 } 1280 }
1278 1281
1279 mutex_lock(&orphan_dir_inode->i_mutex); 1282 mutex_lock(&orphan_dir_inode->i_mutex);
1280 status = ocfs2_meta_lock(orphan_dir_inode, NULL, 0); 1283 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
1281 if (status < 0) { 1284 if (status < 0) {
1282 mlog_errno(status); 1285 mlog_errno(status);
1283 goto out; 1286 goto out;
@@ -1293,7 +1296,7 @@ static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1293 *head = priv.head; 1296 *head = priv.head;
1294 1297
1295out_cluster: 1298out_cluster:
1296 ocfs2_meta_unlock(orphan_dir_inode, 0); 1299 ocfs2_inode_unlock(orphan_dir_inode, 0);
1297out: 1300out:
1298 mutex_unlock(&orphan_dir_inode->i_mutex); 1301 mutex_unlock(&orphan_dir_inode->i_mutex);
1299 iput(orphan_dir_inode); 1302 iput(orphan_dir_inode);
@@ -1380,10 +1383,10 @@ static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1380 iter = oi->ip_next_orphan; 1383 iter = oi->ip_next_orphan;
1381 1384
1382 spin_lock(&oi->ip_lock); 1385 spin_lock(&oi->ip_lock);
1383 /* Delete voting may have set these on the assumption 1386 /* The remote delete code may have set these on the
1384 * that the other node would wipe them successfully. 1387 * assumption that the other node would wipe them
1385 * If they are still in the node's orphan dir, we need 1388 * successfully. If they are still in the node's
1386 * to reset that state. */ 1389 * orphan dir, we need to reset that state. */
1387 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE); 1390 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1388 1391
1389 /* Set the proper information to get us going into 1392 /* Set the proper information to get us going into
diff --git a/fs/ocfs2/journal.h b/fs/ocfs2/journal.h
index 4b32e0961568..220f3e818e78 100644
--- a/fs/ocfs2/journal.h
+++ b/fs/ocfs2/journal.h
@@ -278,6 +278,12 @@ int ocfs2_journal_dirty_data(handle_t *handle,
278/* simple file updates like chmod, etc. */ 278/* simple file updates like chmod, etc. */
279#define OCFS2_INODE_UPDATE_CREDITS 1 279#define OCFS2_INODE_UPDATE_CREDITS 1
280 280
281/* group extend. inode update and last group update. */
282#define OCFS2_GROUP_EXTEND_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
283
284/* group add. inode update and the new group update. */
285#define OCFS2_GROUP_ADD_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
286
281/* get one bit out of a suballocator: dinode + group descriptor + 287/* get one bit out of a suballocator: dinode + group descriptor +
282 * prev. group desc. if we relink. */ 288 * prev. group desc. if we relink. */
283#define OCFS2_SUBALLOC_ALLOC (3) 289#define OCFS2_SUBALLOC_ALLOC (3)
diff --git a/fs/ocfs2/localalloc.c b/fs/ocfs2/localalloc.c
index 58ea88b5af36..add1ffdc5c6c 100644
--- a/fs/ocfs2/localalloc.c
+++ b/fs/ocfs2/localalloc.c
@@ -75,18 +75,12 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
75static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, 75static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
76 struct inode *local_alloc_inode); 76 struct inode *local_alloc_inode);
77 77
78/*
79 * Determine how large our local alloc window should be, in bits.
80 *
81 * These values (and the behavior in ocfs2_alloc_should_use_local) have
82 * been chosen so that most allocations, including new block groups go
83 * through local alloc.
84 */
85static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb) 78static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb)
86{ 79{
87 BUG_ON(osb->s_clustersize_bits < 12); 80 BUG_ON(osb->s_clustersize_bits > 20);
88 81
89 return 2048 >> (osb->s_clustersize_bits - 12); 82 /* Size local alloc windows by the megabyte */
83 return osb->local_alloc_size << (20 - osb->s_clustersize_bits);
90} 84}
91 85
92/* 86/*
@@ -96,18 +90,23 @@ static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb)
96int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits) 90int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
97{ 91{
98 int la_bits = ocfs2_local_alloc_window_bits(osb); 92 int la_bits = ocfs2_local_alloc_window_bits(osb);
93 int ret = 0;
99 94
100 if (osb->local_alloc_state != OCFS2_LA_ENABLED) 95 if (osb->local_alloc_state != OCFS2_LA_ENABLED)
101 return 0; 96 goto bail;
102 97
103 /* la_bits should be at least twice the size (in clusters) of 98 /* la_bits should be at least twice the size (in clusters) of
104 * a new block group. We want to be sure block group 99 * a new block group. We want to be sure block group
105 * allocations go through the local alloc, so allow an 100 * allocations go through the local alloc, so allow an
106 * allocation to take up to half the bitmap. */ 101 * allocation to take up to half the bitmap. */
107 if (bits > (la_bits / 2)) 102 if (bits > (la_bits / 2))
108 return 0; 103 goto bail;
109 104
110 return 1; 105 ret = 1;
106bail:
107 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
108 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
109 return ret;
111} 110}
112 111
113int ocfs2_load_local_alloc(struct ocfs2_super *osb) 112int ocfs2_load_local_alloc(struct ocfs2_super *osb)
@@ -121,6 +120,19 @@ int ocfs2_load_local_alloc(struct ocfs2_super *osb)
121 120
122 mlog_entry_void(); 121 mlog_entry_void();
123 122
123 if (ocfs2_mount_local(osb))
124 goto bail;
125
126 if (osb->local_alloc_size == 0)
127 goto bail;
128
129 if (ocfs2_local_alloc_window_bits(osb) >= osb->bitmap_cpg) {
130 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
131 "than max possible %u. Using defaults.\n",
132 ocfs2_local_alloc_window_bits(osb), (osb->bitmap_cpg - 1));
133 osb->local_alloc_size = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
134 }
135
124 /* read the alloc off disk */ 136 /* read the alloc off disk */
125 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE, 137 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
126 osb->slot_num); 138 osb->slot_num);
@@ -181,6 +193,9 @@ bail:
181 if (inode) 193 if (inode)
182 iput(inode); 194 iput(inode);
183 195
196 mlog(0, "Local alloc window bits = %d\n",
197 ocfs2_local_alloc_window_bits(osb));
198
184 mlog_exit(status); 199 mlog_exit(status);
185 return status; 200 return status;
186} 201}
@@ -231,7 +246,7 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
231 246
232 mutex_lock(&main_bm_inode->i_mutex); 247 mutex_lock(&main_bm_inode->i_mutex);
233 248
234 status = ocfs2_meta_lock(main_bm_inode, &main_bm_bh, 1); 249 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
235 if (status < 0) { 250 if (status < 0) {
236 mlog_errno(status); 251 mlog_errno(status);
237 goto out_mutex; 252 goto out_mutex;
@@ -286,7 +301,7 @@ out_unlock:
286 if (main_bm_bh) 301 if (main_bm_bh)
287 brelse(main_bm_bh); 302 brelse(main_bm_bh);
288 303
289 ocfs2_meta_unlock(main_bm_inode, 1); 304 ocfs2_inode_unlock(main_bm_inode, 1);
290 305
291out_mutex: 306out_mutex:
292 mutex_unlock(&main_bm_inode->i_mutex); 307 mutex_unlock(&main_bm_inode->i_mutex);
@@ -399,7 +414,7 @@ int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
399 414
400 mutex_lock(&main_bm_inode->i_mutex); 415 mutex_lock(&main_bm_inode->i_mutex);
401 416
402 status = ocfs2_meta_lock(main_bm_inode, &main_bm_bh, 1); 417 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
403 if (status < 0) { 418 if (status < 0) {
404 mlog_errno(status); 419 mlog_errno(status);
405 goto out_mutex; 420 goto out_mutex;
@@ -424,7 +439,7 @@ int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
424 ocfs2_commit_trans(osb, handle); 439 ocfs2_commit_trans(osb, handle);
425 440
426out_unlock: 441out_unlock:
427 ocfs2_meta_unlock(main_bm_inode, 1); 442 ocfs2_inode_unlock(main_bm_inode, 1);
428 443
429out_mutex: 444out_mutex:
430 mutex_unlock(&main_bm_inode->i_mutex); 445 mutex_unlock(&main_bm_inode->i_mutex);
@@ -521,6 +536,9 @@ bail:
521 iput(local_alloc_inode); 536 iput(local_alloc_inode);
522 } 537 }
523 538
539 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
540 status);
541
524 mlog_exit(status); 542 mlog_exit(status);
525 return status; 543 return status;
526} 544}
diff --git a/fs/ocfs2/locks.c b/fs/ocfs2/locks.c
new file mode 100644
index 000000000000..203f87143877
--- /dev/null
+++ b/fs/ocfs2/locks.c
@@ -0,0 +1,125 @@
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * locks.c
5 *
6 * Userspace file locking support
7 *
8 * Copyright (C) 2007 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (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 GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27
28#define MLOG_MASK_PREFIX ML_INODE
29#include <cluster/masklog.h>
30
31#include "ocfs2.h"
32
33#include "dlmglue.h"
34#include "file.h"
35#include "locks.h"
36
37static int ocfs2_do_flock(struct file *file, struct inode *inode,
38 int cmd, struct file_lock *fl)
39{
40 int ret = 0, level = 0, trylock = 0;
41 struct ocfs2_file_private *fp = file->private_data;
42 struct ocfs2_lock_res *lockres = &fp->fp_flock;
43
44 if (fl->fl_type == F_WRLCK)
45 level = 1;
46 if (!IS_SETLKW(cmd))
47 trylock = 1;
48
49 mutex_lock(&fp->fp_mutex);
50
51 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
52 lockres->l_level > LKM_NLMODE) {
53 int old_level = 0;
54
55 if (lockres->l_level == LKM_EXMODE)
56 old_level = 1;
57
58 if (level == old_level)
59 goto out;
60
61 /*
62 * Converting an existing lock is not guaranteed to be
63 * atomic, so we can get away with simply unlocking
64 * here and allowing the lock code to try at the new
65 * level.
66 */
67
68 flock_lock_file_wait(file,
69 &(struct file_lock){.fl_type = F_UNLCK});
70
71 ocfs2_file_unlock(file);
72 }
73
74 ret = ocfs2_file_lock(file, level, trylock);
75 if (ret) {
76 if (ret == -EAGAIN && trylock)
77 ret = -EWOULDBLOCK;
78 else
79 mlog_errno(ret);
80 goto out;
81 }
82
83 ret = flock_lock_file_wait(file, fl);
84
85out:
86 mutex_unlock(&fp->fp_mutex);
87
88 return ret;
89}
90
91static int ocfs2_do_funlock(struct file *file, int cmd, struct file_lock *fl)
92{
93 int ret;
94 struct ocfs2_file_private *fp = file->private_data;
95
96 mutex_lock(&fp->fp_mutex);
97 ocfs2_file_unlock(file);
98 ret = flock_lock_file_wait(file, fl);
99 mutex_unlock(&fp->fp_mutex);
100
101 return ret;
102}
103
104/*
105 * Overall flow of ocfs2_flock() was influenced by gfs2_flock().
106 */
107int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl)
108{
109 struct inode *inode = file->f_mapping->host;
110 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
111
112 if (!(fl->fl_flags & FL_FLOCK))
113 return -ENOLCK;
114 if (__mandatory_lock(inode))
115 return -ENOLCK;
116
117 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
118 ocfs2_mount_local(osb))
119 return flock_lock_file_wait(file, fl);
120
121 if (fl->fl_type == F_UNLCK)
122 return ocfs2_do_funlock(file, cmd, fl);
123 else
124 return ocfs2_do_flock(file, inode, cmd, fl);
125}
diff --git a/fs/ocfs2/vote.h b/fs/ocfs2/locks.h
index 9ea46f62de31..9743ef2324ec 100644
--- a/fs/ocfs2/vote.h
+++ b/fs/ocfs2/locks.h
@@ -1,9 +1,9 @@
1/* -*- mode: c; c-basic-offset: 8; -*- 1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0: 2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 * 3 *
4 * vote.h 4 * locks.h
5 * 5 *
6 * description here 6 * Function prototypes for Userspace file locking support
7 * 7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved. 8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 * 9 *
@@ -23,26 +23,9 @@
23 * Boston, MA 021110-1307, USA. 23 * Boston, MA 021110-1307, USA.
24 */ 24 */
25 25
26#ifndef OCFS2_LOCKS_H
27#define OCFS2_LOCKS_H
26 28
27#ifndef VOTE_H 29int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl);
28#define VOTE_H
29 30
30int ocfs2_vote_thread(void *arg); 31#endif /* OCFS2_LOCKS_H */
31static inline void ocfs2_kick_vote_thread(struct ocfs2_super *osb)
32{
33 spin_lock(&osb->vote_task_lock);
34 /* make sure the voting thread gets a swipe at whatever changes
35 * the caller may have made to the voting state */
36 osb->vote_wake_sequence++;
37 spin_unlock(&osb->vote_task_lock);
38 wake_up(&osb->vote_event);
39}
40
41int ocfs2_request_mount_vote(struct ocfs2_super *osb);
42int ocfs2_request_umount_vote(struct ocfs2_super *osb);
43int ocfs2_register_net_handlers(struct ocfs2_super *osb);
44void ocfs2_unregister_net_handlers(struct ocfs2_super *osb);
45
46void ocfs2_remove_node_from_vote_queues(struct ocfs2_super *osb,
47 int node_num);
48#endif
diff --git a/fs/ocfs2/mmap.c b/fs/ocfs2/mmap.c
index 98756156d298..3dc18d67557c 100644
--- a/fs/ocfs2/mmap.c
+++ b/fs/ocfs2/mmap.c
@@ -168,7 +168,7 @@ static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct page *page)
168 * node. Taking the data lock will also ensure that we don't 168 * node. Taking the data lock will also ensure that we don't
169 * attempt page truncation as part of a downconvert. 169 * attempt page truncation as part of a downconvert.
170 */ 170 */
171 ret = ocfs2_meta_lock(inode, &di_bh, 1); 171 ret = ocfs2_inode_lock(inode, &di_bh, 1);
172 if (ret < 0) { 172 if (ret < 0) {
173 mlog_errno(ret); 173 mlog_errno(ret);
174 goto out; 174 goto out;
@@ -181,21 +181,12 @@ static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct page *page)
181 */ 181 */
182 down_write(&OCFS2_I(inode)->ip_alloc_sem); 182 down_write(&OCFS2_I(inode)->ip_alloc_sem);
183 183
184 ret = ocfs2_data_lock(inode, 1);
185 if (ret < 0) {
186 mlog_errno(ret);
187 goto out_meta_unlock;
188 }
189
190 ret = __ocfs2_page_mkwrite(inode, di_bh, page); 184 ret = __ocfs2_page_mkwrite(inode, di_bh, page);
191 185
192 ocfs2_data_unlock(inode, 1);
193
194out_meta_unlock:
195 up_write(&OCFS2_I(inode)->ip_alloc_sem); 186 up_write(&OCFS2_I(inode)->ip_alloc_sem);
196 187
197 brelse(di_bh); 188 brelse(di_bh);
198 ocfs2_meta_unlock(inode, 1); 189 ocfs2_inode_unlock(inode, 1);
199 190
200out: 191out:
201 ret2 = ocfs2_vm_op_unblock_sigs(&oldset); 192 ret2 = ocfs2_vm_op_unblock_sigs(&oldset);
@@ -214,13 +205,13 @@ int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
214{ 205{
215 int ret = 0, lock_level = 0; 206 int ret = 0, lock_level = 0;
216 207
217 ret = ocfs2_meta_lock_atime(file->f_dentry->d_inode, 208 ret = ocfs2_inode_lock_atime(file->f_dentry->d_inode,
218 file->f_vfsmnt, &lock_level); 209 file->f_vfsmnt, &lock_level);
219 if (ret < 0) { 210 if (ret < 0) {
220 mlog_errno(ret); 211 mlog_errno(ret);
221 goto out; 212 goto out;
222 } 213 }
223 ocfs2_meta_unlock(file->f_dentry->d_inode, lock_level); 214 ocfs2_inode_unlock(file->f_dentry->d_inode, lock_level);
224out: 215out:
225 vma->vm_ops = &ocfs2_file_vm_ops; 216 vma->vm_ops = &ocfs2_file_vm_ops;
226 vma->vm_flags |= VM_CAN_NONLINEAR; 217 vma->vm_flags |= VM_CAN_NONLINEAR;
diff --git a/fs/ocfs2/namei.c b/fs/ocfs2/namei.c
index 989ac2718587..ae9ad9587516 100644
--- a/fs/ocfs2/namei.c
+++ b/fs/ocfs2/namei.c
@@ -60,7 +60,6 @@
60#include "symlink.h" 60#include "symlink.h"
61#include "sysfile.h" 61#include "sysfile.h"
62#include "uptodate.h" 62#include "uptodate.h"
63#include "vote.h"
64 63
65#include "buffer_head_io.h" 64#include "buffer_head_io.h"
66 65
@@ -116,7 +115,7 @@ static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
116 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len, 115 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
117 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno); 116 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
118 117
119 status = ocfs2_meta_lock(dir, NULL, 0); 118 status = ocfs2_inode_lock(dir, NULL, 0);
120 if (status < 0) { 119 if (status < 0) {
121 if (status != -ENOENT) 120 if (status != -ENOENT)
122 mlog_errno(status); 121 mlog_errno(status);
@@ -129,7 +128,7 @@ static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
129 if (status < 0) 128 if (status < 0)
130 goto bail_add; 129 goto bail_add;
131 130
132 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0); 131 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
133 if (IS_ERR(inode)) { 132 if (IS_ERR(inode)) {
134 ret = ERR_PTR(-EACCES); 133 ret = ERR_PTR(-EACCES);
135 goto bail_unlock; 134 goto bail_unlock;
@@ -176,8 +175,8 @@ bail_unlock:
176 /* Don't drop the cluster lock until *after* the d_add -- 175 /* Don't drop the cluster lock until *after* the d_add --
177 * unlink on another node will message us to remove that 176 * unlink on another node will message us to remove that
178 * dentry under this lock so otherwise we can race this with 177 * dentry under this lock so otherwise we can race this with
179 * the vote thread and have a stale dentry. */ 178 * the downconvert thread and have a stale dentry. */
180 ocfs2_meta_unlock(dir, 0); 179 ocfs2_inode_unlock(dir, 0);
181 180
182bail: 181bail:
183 182
@@ -209,7 +208,7 @@ static int ocfs2_mknod(struct inode *dir,
209 /* get our super block */ 208 /* get our super block */
210 osb = OCFS2_SB(dir->i_sb); 209 osb = OCFS2_SB(dir->i_sb);
211 210
212 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1); 211 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
213 if (status < 0) { 212 if (status < 0) {
214 if (status != -ENOENT) 213 if (status != -ENOENT)
215 mlog_errno(status); 214 mlog_errno(status);
@@ -323,7 +322,7 @@ leave:
323 if (handle) 322 if (handle)
324 ocfs2_commit_trans(osb, handle); 323 ocfs2_commit_trans(osb, handle);
325 324
326 ocfs2_meta_unlock(dir, 1); 325 ocfs2_inode_unlock(dir, 1);
327 326
328 if (status == -ENOSPC) 327 if (status == -ENOSPC)
329 mlog(0, "Disk is full\n"); 328 mlog(0, "Disk is full\n");
@@ -553,7 +552,7 @@ static int ocfs2_link(struct dentry *old_dentry,
553 if (S_ISDIR(inode->i_mode)) 552 if (S_ISDIR(inode->i_mode))
554 return -EPERM; 553 return -EPERM;
555 554
556 err = ocfs2_meta_lock(dir, &parent_fe_bh, 1); 555 err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
557 if (err < 0) { 556 if (err < 0) {
558 if (err != -ENOENT) 557 if (err != -ENOENT)
559 mlog_errno(err); 558 mlog_errno(err);
@@ -578,7 +577,7 @@ static int ocfs2_link(struct dentry *old_dentry,
578 goto out; 577 goto out;
579 } 578 }
580 579
581 err = ocfs2_meta_lock(inode, &fe_bh, 1); 580 err = ocfs2_inode_lock(inode, &fe_bh, 1);
582 if (err < 0) { 581 if (err < 0) {
583 if (err != -ENOENT) 582 if (err != -ENOENT)
584 mlog_errno(err); 583 mlog_errno(err);
@@ -643,10 +642,10 @@ static int ocfs2_link(struct dentry *old_dentry,
643out_commit: 642out_commit:
644 ocfs2_commit_trans(osb, handle); 643 ocfs2_commit_trans(osb, handle);
645out_unlock_inode: 644out_unlock_inode:
646 ocfs2_meta_unlock(inode, 1); 645 ocfs2_inode_unlock(inode, 1);
647 646
648out: 647out:
649 ocfs2_meta_unlock(dir, 1); 648 ocfs2_inode_unlock(dir, 1);
650 649
651 if (de_bh) 650 if (de_bh)
652 brelse(de_bh); 651 brelse(de_bh);
@@ -720,7 +719,7 @@ static int ocfs2_unlink(struct inode *dir,
720 return -EPERM; 719 return -EPERM;
721 } 720 }
722 721
723 status = ocfs2_meta_lock(dir, &parent_node_bh, 1); 722 status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
724 if (status < 0) { 723 if (status < 0) {
725 if (status != -ENOENT) 724 if (status != -ENOENT)
726 mlog_errno(status); 725 mlog_errno(status);
@@ -745,7 +744,7 @@ static int ocfs2_unlink(struct inode *dir,
745 goto leave; 744 goto leave;
746 } 745 }
747 746
748 status = ocfs2_meta_lock(inode, &fe_bh, 1); 747 status = ocfs2_inode_lock(inode, &fe_bh, 1);
749 if (status < 0) { 748 if (status < 0) {
750 if (status != -ENOENT) 749 if (status != -ENOENT)
751 mlog_errno(status); 750 mlog_errno(status);
@@ -765,7 +764,7 @@ static int ocfs2_unlink(struct inode *dir,
765 764
766 status = ocfs2_remote_dentry_delete(dentry); 765 status = ocfs2_remote_dentry_delete(dentry);
767 if (status < 0) { 766 if (status < 0) {
768 /* This vote should succeed under all normal 767 /* This remote delete should succeed under all normal
769 * circumstances. */ 768 * circumstances. */
770 mlog_errno(status); 769 mlog_errno(status);
771 goto leave; 770 goto leave;
@@ -841,13 +840,13 @@ leave:
841 ocfs2_commit_trans(osb, handle); 840 ocfs2_commit_trans(osb, handle);
842 841
843 if (child_locked) 842 if (child_locked)
844 ocfs2_meta_unlock(inode, 1); 843 ocfs2_inode_unlock(inode, 1);
845 844
846 ocfs2_meta_unlock(dir, 1); 845 ocfs2_inode_unlock(dir, 1);
847 846
848 if (orphan_dir) { 847 if (orphan_dir) {
849 /* This was locked for us in ocfs2_prepare_orphan_dir() */ 848 /* This was locked for us in ocfs2_prepare_orphan_dir() */
850 ocfs2_meta_unlock(orphan_dir, 1); 849 ocfs2_inode_unlock(orphan_dir, 1);
851 mutex_unlock(&orphan_dir->i_mutex); 850 mutex_unlock(&orphan_dir->i_mutex);
852 iput(orphan_dir); 851 iput(orphan_dir);
853 } 852 }
@@ -908,7 +907,7 @@ static int ocfs2_double_lock(struct ocfs2_super *osb,
908 inode1 = tmpinode; 907 inode1 = tmpinode;
909 } 908 }
910 /* lock id2 */ 909 /* lock id2 */
911 status = ocfs2_meta_lock(inode2, bh2, 1); 910 status = ocfs2_inode_lock(inode2, bh2, 1);
912 if (status < 0) { 911 if (status < 0) {
913 if (status != -ENOENT) 912 if (status != -ENOENT)
914 mlog_errno(status); 913 mlog_errno(status);
@@ -917,14 +916,14 @@ static int ocfs2_double_lock(struct ocfs2_super *osb,
917 } 916 }
918 917
919 /* lock id1 */ 918 /* lock id1 */
920 status = ocfs2_meta_lock(inode1, bh1, 1); 919 status = ocfs2_inode_lock(inode1, bh1, 1);
921 if (status < 0) { 920 if (status < 0) {
922 /* 921 /*
923 * An error return must mean that no cluster locks 922 * An error return must mean that no cluster locks
924 * were held on function exit. 923 * were held on function exit.
925 */ 924 */
926 if (oi1->ip_blkno != oi2->ip_blkno) 925 if (oi1->ip_blkno != oi2->ip_blkno)
927 ocfs2_meta_unlock(inode2, 1); 926 ocfs2_inode_unlock(inode2, 1);
928 927
929 if (status != -ENOENT) 928 if (status != -ENOENT)
930 mlog_errno(status); 929 mlog_errno(status);
@@ -937,10 +936,10 @@ bail:
937 936
938static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2) 937static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
939{ 938{
940 ocfs2_meta_unlock(inode1, 1); 939 ocfs2_inode_unlock(inode1, 1);
941 940
942 if (inode1 != inode2) 941 if (inode1 != inode2)
943 ocfs2_meta_unlock(inode2, 1); 942 ocfs2_inode_unlock(inode2, 1);
944} 943}
945 944
946static int ocfs2_rename(struct inode *old_dir, 945static int ocfs2_rename(struct inode *old_dir,
@@ -1031,10 +1030,11 @@ static int ocfs2_rename(struct inode *old_dir,
1031 1030
1032 /* 1031 /*
1033 * Aside from allowing a meta data update, the locking here 1032 * Aside from allowing a meta data update, the locking here
1034 * also ensures that the vote thread on other nodes won't have 1033 * also ensures that the downconvert thread on other nodes
1035 * to concurrently downconvert the inode and the dentry locks. 1034 * won't have to concurrently downconvert the inode and the
1035 * dentry locks.
1036 */ 1036 */
1037 status = ocfs2_meta_lock(old_inode, &old_inode_bh, 1); 1037 status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1);
1038 if (status < 0) { 1038 if (status < 0) {
1039 if (status != -ENOENT) 1039 if (status != -ENOENT)
1040 mlog_errno(status); 1040 mlog_errno(status);
@@ -1143,7 +1143,7 @@ static int ocfs2_rename(struct inode *old_dir,
1143 goto bail; 1143 goto bail;
1144 } 1144 }
1145 1145
1146 status = ocfs2_meta_lock(new_inode, &newfe_bh, 1); 1146 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
1147 if (status < 0) { 1147 if (status < 0) {
1148 if (status != -ENOENT) 1148 if (status != -ENOENT)
1149 mlog_errno(status); 1149 mlog_errno(status);
@@ -1355,14 +1355,14 @@ bail:
1355 ocfs2_double_unlock(old_dir, new_dir); 1355 ocfs2_double_unlock(old_dir, new_dir);
1356 1356
1357 if (old_child_locked) 1357 if (old_child_locked)
1358 ocfs2_meta_unlock(old_inode, 1); 1358 ocfs2_inode_unlock(old_inode, 1);
1359 1359
1360 if (new_child_locked) 1360 if (new_child_locked)
1361 ocfs2_meta_unlock(new_inode, 1); 1361 ocfs2_inode_unlock(new_inode, 1);
1362 1362
1363 if (orphan_dir) { 1363 if (orphan_dir) {
1364 /* This was locked for us in ocfs2_prepare_orphan_dir() */ 1364 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1365 ocfs2_meta_unlock(orphan_dir, 1); 1365 ocfs2_inode_unlock(orphan_dir, 1);
1366 mutex_unlock(&orphan_dir->i_mutex); 1366 mutex_unlock(&orphan_dir->i_mutex);
1367 iput(orphan_dir); 1367 iput(orphan_dir);
1368 } 1368 }
@@ -1530,7 +1530,7 @@ static int ocfs2_symlink(struct inode *dir,
1530 credits = ocfs2_calc_symlink_credits(sb); 1530 credits = ocfs2_calc_symlink_credits(sb);
1531 1531
1532 /* lock the parent directory */ 1532 /* lock the parent directory */
1533 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1); 1533 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
1534 if (status < 0) { 1534 if (status < 0) {
1535 if (status != -ENOENT) 1535 if (status != -ENOENT)
1536 mlog_errno(status); 1536 mlog_errno(status);
@@ -1657,7 +1657,7 @@ bail:
1657 if (handle) 1657 if (handle)
1658 ocfs2_commit_trans(osb, handle); 1658 ocfs2_commit_trans(osb, handle);
1659 1659
1660 ocfs2_meta_unlock(dir, 1); 1660 ocfs2_inode_unlock(dir, 1);
1661 1661
1662 if (new_fe_bh) 1662 if (new_fe_bh)
1663 brelse(new_fe_bh); 1663 brelse(new_fe_bh);
@@ -1735,7 +1735,7 @@ static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
1735 1735
1736 mutex_lock(&orphan_dir_inode->i_mutex); 1736 mutex_lock(&orphan_dir_inode->i_mutex);
1737 1737
1738 status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1); 1738 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
1739 if (status < 0) { 1739 if (status < 0) {
1740 mlog_errno(status); 1740 mlog_errno(status);
1741 goto leave; 1741 goto leave;
@@ -1745,7 +1745,7 @@ static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
1745 orphan_dir_bh, name, 1745 orphan_dir_bh, name,
1746 OCFS2_ORPHAN_NAMELEN, de_bh); 1746 OCFS2_ORPHAN_NAMELEN, de_bh);
1747 if (status < 0) { 1747 if (status < 0) {
1748 ocfs2_meta_unlock(orphan_dir_inode, 1); 1748 ocfs2_inode_unlock(orphan_dir_inode, 1);
1749 1749
1750 mlog_errno(status); 1750 mlog_errno(status);
1751 goto leave; 1751 goto leave;
diff --git a/fs/ocfs2/ocfs2.h b/fs/ocfs2/ocfs2.h
index 60a23e1906b0..d08480580470 100644
--- a/fs/ocfs2/ocfs2.h
+++ b/fs/ocfs2/ocfs2.h
@@ -101,6 +101,7 @@ enum ocfs2_unlock_action {
101 * about to be 101 * about to be
102 * dropped. */ 102 * dropped. */
103#define OCFS2_LOCK_QUEUED (0x00000100) /* queued for downconvert */ 103#define OCFS2_LOCK_QUEUED (0x00000100) /* queued for downconvert */
104#define OCFS2_LOCK_NOCACHE (0x00000200) /* don't use a holder count */
104 105
105struct ocfs2_lock_res_ops; 106struct ocfs2_lock_res_ops;
106 107
@@ -170,6 +171,7 @@ enum ocfs2_mount_options
170 OCFS2_MOUNT_NOINTR = 1 << 2, /* Don't catch signals */ 171 OCFS2_MOUNT_NOINTR = 1 << 2, /* Don't catch signals */
171 OCFS2_MOUNT_ERRORS_PANIC = 1 << 3, /* Panic on errors */ 172 OCFS2_MOUNT_ERRORS_PANIC = 1 << 3, /* Panic on errors */
172 OCFS2_MOUNT_DATA_WRITEBACK = 1 << 4, /* No data ordering */ 173 OCFS2_MOUNT_DATA_WRITEBACK = 1 << 4, /* No data ordering */
174 OCFS2_MOUNT_LOCALFLOCKS = 1 << 5, /* No cluster aware user file locks */
173}; 175};
174 176
175#define OCFS2_OSB_SOFT_RO 0x0001 177#define OCFS2_OSB_SOFT_RO 0x0001
@@ -189,9 +191,7 @@ struct ocfs2_super
189 struct ocfs2_slot_info *slot_info; 191 struct ocfs2_slot_info *slot_info;
190 192
191 spinlock_t node_map_lock; 193 spinlock_t node_map_lock;
192 struct ocfs2_node_map mounted_map;
193 struct ocfs2_node_map recovery_map; 194 struct ocfs2_node_map recovery_map;
194 struct ocfs2_node_map umount_map;
195 195
196 u64 root_blkno; 196 u64 root_blkno;
197 u64 system_dir_blkno; 197 u64 system_dir_blkno;
@@ -231,7 +231,9 @@ struct ocfs2_super
231 wait_queue_head_t checkpoint_event; 231 wait_queue_head_t checkpoint_event;
232 atomic_t needs_checkpoint; 232 atomic_t needs_checkpoint;
233 struct ocfs2_journal *journal; 233 struct ocfs2_journal *journal;
234 unsigned long osb_commit_interval;
234 235
236 int local_alloc_size;
235 enum ocfs2_local_alloc_state local_alloc_state; 237 enum ocfs2_local_alloc_state local_alloc_state;
236 struct buffer_head *local_alloc_bh; 238 struct buffer_head *local_alloc_bh;
237 u64 la_last_gd; 239 u64 la_last_gd;
@@ -254,28 +256,21 @@ struct ocfs2_super
254 256
255 wait_queue_head_t recovery_event; 257 wait_queue_head_t recovery_event;
256 258
257 spinlock_t vote_task_lock; 259 spinlock_t dc_task_lock;
258 struct task_struct *vote_task; 260 struct task_struct *dc_task;
259 wait_queue_head_t vote_event; 261 wait_queue_head_t dc_event;
260 unsigned long vote_wake_sequence; 262 unsigned long dc_wake_sequence;
261 unsigned long vote_work_sequence; 263 unsigned long dc_work_sequence;
262 264
265 /*
266 * Any thread can add locks to the list, but the downconvert
267 * thread is the only one allowed to remove locks. Any change
268 * to this rule requires updating
269 * ocfs2_downconvert_thread_do_work().
270 */
263 struct list_head blocked_lock_list; 271 struct list_head blocked_lock_list;
264 unsigned long blocked_lock_count; 272 unsigned long blocked_lock_count;
265 273
266 struct list_head vote_list;
267 int vote_count;
268
269 u32 net_key;
270 spinlock_t net_response_lock;
271 unsigned int net_response_ids;
272 struct list_head net_response_list;
273
274 struct o2hb_callback_func osb_hb_up;
275 struct o2hb_callback_func osb_hb_down;
276
277 struct list_head osb_net_handlers;
278
279 wait_queue_head_t osb_mount_event; 274 wait_queue_head_t osb_mount_event;
280 275
281 /* Truncate log info */ 276 /* Truncate log info */
diff --git a/fs/ocfs2/ocfs2_fs.h b/fs/ocfs2/ocfs2_fs.h
index 6ef876759a73..3633edd3982f 100644
--- a/fs/ocfs2/ocfs2_fs.h
+++ b/fs/ocfs2/ocfs2_fs.h
@@ -231,6 +231,20 @@ struct ocfs2_space_resv {
231#define OCFS2_IOC_RESVSP64 _IOW ('X', 42, struct ocfs2_space_resv) 231#define OCFS2_IOC_RESVSP64 _IOW ('X', 42, struct ocfs2_space_resv)
232#define OCFS2_IOC_UNRESVSP64 _IOW ('X', 43, struct ocfs2_space_resv) 232#define OCFS2_IOC_UNRESVSP64 _IOW ('X', 43, struct ocfs2_space_resv)
233 233
234/* Used to pass group descriptor data when online resize is done */
235struct ocfs2_new_group_input {
236 __u64 group; /* Group descriptor's blkno. */
237 __u32 clusters; /* Total number of clusters in this group */
238 __u32 frees; /* Total free clusters in this group */
239 __u16 chain; /* Chain for this group */
240 __u16 reserved1;
241 __u32 reserved2;
242};
243
244#define OCFS2_IOC_GROUP_EXTEND _IOW('o', 1, int)
245#define OCFS2_IOC_GROUP_ADD _IOW('o', 2,struct ocfs2_new_group_input)
246#define OCFS2_IOC_GROUP_ADD64 _IOW('o', 3,struct ocfs2_new_group_input)
247
234/* 248/*
235 * Journal Flags (ocfs2_dinode.id1.journal1.i_flags) 249 * Journal Flags (ocfs2_dinode.id1.journal1.i_flags)
236 */ 250 */
@@ -256,6 +270,14 @@ struct ocfs2_space_resv {
256/* Journal limits (in bytes) */ 270/* Journal limits (in bytes) */
257#define OCFS2_MIN_JOURNAL_SIZE (4 * 1024 * 1024) 271#define OCFS2_MIN_JOURNAL_SIZE (4 * 1024 * 1024)
258 272
273/*
274 * Default local alloc size (in megabytes)
275 *
276 * The value chosen should be such that most allocations, including new
277 * block groups, use local alloc.
278 */
279#define OCFS2_DEFAULT_LOCAL_ALLOC_SIZE 8
280
259struct ocfs2_system_inode_info { 281struct ocfs2_system_inode_info {
260 char *si_name; 282 char *si_name;
261 int si_iflags; 283 int si_iflags;
diff --git a/fs/ocfs2/ocfs2_lockid.h b/fs/ocfs2/ocfs2_lockid.h
index 4ca02b1c38ac..86f3e3799c2b 100644
--- a/fs/ocfs2/ocfs2_lockid.h
+++ b/fs/ocfs2/ocfs2_lockid.h
@@ -45,6 +45,7 @@ enum ocfs2_lock_type {
45 OCFS2_LOCK_TYPE_RW, 45 OCFS2_LOCK_TYPE_RW,
46 OCFS2_LOCK_TYPE_DENTRY, 46 OCFS2_LOCK_TYPE_DENTRY,
47 OCFS2_LOCK_TYPE_OPEN, 47 OCFS2_LOCK_TYPE_OPEN,
48 OCFS2_LOCK_TYPE_FLOCK,
48 OCFS2_NUM_LOCK_TYPES 49 OCFS2_NUM_LOCK_TYPES
49}; 50};
50 51
@@ -73,6 +74,9 @@ static inline char ocfs2_lock_type_char(enum ocfs2_lock_type type)
73 case OCFS2_LOCK_TYPE_OPEN: 74 case OCFS2_LOCK_TYPE_OPEN:
74 c = 'O'; 75 c = 'O';
75 break; 76 break;
77 case OCFS2_LOCK_TYPE_FLOCK:
78 c = 'F';
79 break;
76 default: 80 default:
77 c = '\0'; 81 c = '\0';
78 } 82 }
@@ -90,6 +94,7 @@ static char *ocfs2_lock_type_strings[] = {
90 [OCFS2_LOCK_TYPE_RW] = "Write/Read", 94 [OCFS2_LOCK_TYPE_RW] = "Write/Read",
91 [OCFS2_LOCK_TYPE_DENTRY] = "Dentry", 95 [OCFS2_LOCK_TYPE_DENTRY] = "Dentry",
92 [OCFS2_LOCK_TYPE_OPEN] = "Open", 96 [OCFS2_LOCK_TYPE_OPEN] = "Open",
97 [OCFS2_LOCK_TYPE_FLOCK] = "Flock",
93}; 98};
94 99
95static inline const char *ocfs2_lock_type_string(enum ocfs2_lock_type type) 100static inline const char *ocfs2_lock_type_string(enum ocfs2_lock_type type)
diff --git a/fs/ocfs2/resize.c b/fs/ocfs2/resize.c
new file mode 100644
index 000000000000..37835ffcb039
--- /dev/null
+++ b/fs/ocfs2/resize.c
@@ -0,0 +1,634 @@
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * resize.c
5 *
6 * volume resize.
7 * Inspired by ext3/resize.c.
8 *
9 * Copyright (C) 2007 Oracle. All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (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 GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
25 */
26
27#include <linux/fs.h>
28#include <linux/types.h>
29
30#define MLOG_MASK_PREFIX ML_DISK_ALLOC
31#include <cluster/masklog.h>
32
33#include "ocfs2.h"
34
35#include "alloc.h"
36#include "dlmglue.h"
37#include "inode.h"
38#include "journal.h"
39#include "super.h"
40#include "sysfile.h"
41#include "uptodate.h"
42
43#include "buffer_head_io.h"
44#include "suballoc.h"
45#include "resize.h"
46
47/*
48 * Check whether there are new backup superblocks exist
49 * in the last group. If there are some, mark them or clear
50 * them in the bitmap.
51 *
52 * Return how many backups we find in the last group.
53 */
54static u16 ocfs2_calc_new_backup_super(struct inode *inode,
55 struct ocfs2_group_desc *gd,
56 int new_clusters,
57 u32 first_new_cluster,
58 u16 cl_cpg,
59 int set)
60{
61 int i;
62 u16 backups = 0;
63 u32 cluster;
64 u64 blkno, gd_blkno, lgd_blkno = le64_to_cpu(gd->bg_blkno);
65
66 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
67 blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
68 cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
69
70 gd_blkno = ocfs2_which_cluster_group(inode, cluster);
71 if (gd_blkno < lgd_blkno)
72 continue;
73 else if (gd_blkno > lgd_blkno)
74 break;
75
76 if (set)
77 ocfs2_set_bit(cluster % cl_cpg,
78 (unsigned long *)gd->bg_bitmap);
79 else
80 ocfs2_clear_bit(cluster % cl_cpg,
81 (unsigned long *)gd->bg_bitmap);
82 backups++;
83 }
84
85 mlog_exit_void();
86 return backups;
87}
88
89static int ocfs2_update_last_group_and_inode(handle_t *handle,
90 struct inode *bm_inode,
91 struct buffer_head *bm_bh,
92 struct buffer_head *group_bh,
93 u32 first_new_cluster,
94 int new_clusters)
95{
96 int ret = 0;
97 struct ocfs2_super *osb = OCFS2_SB(bm_inode->i_sb);
98 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bm_bh->b_data;
99 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
100 struct ocfs2_chain_rec *cr;
101 struct ocfs2_group_desc *group;
102 u16 chain, num_bits, backups = 0;
103 u16 cl_bpc = le16_to_cpu(cl->cl_bpc);
104 u16 cl_cpg = le16_to_cpu(cl->cl_cpg);
105
106 mlog_entry("(new_clusters=%d, first_new_cluster = %u)\n",
107 new_clusters, first_new_cluster);
108
109 ret = ocfs2_journal_access(handle, bm_inode, group_bh,
110 OCFS2_JOURNAL_ACCESS_WRITE);
111 if (ret < 0) {
112 mlog_errno(ret);
113 goto out;
114 }
115
116 group = (struct ocfs2_group_desc *)group_bh->b_data;
117
118 /* update the group first. */
119 num_bits = new_clusters * cl_bpc;
120 le16_add_cpu(&group->bg_bits, num_bits);
121 le16_add_cpu(&group->bg_free_bits_count, num_bits);
122
123 /*
124 * check whether there are some new backup superblocks exist in
125 * this group and update the group bitmap accordingly.
126 */
127 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb,
128 OCFS2_FEATURE_COMPAT_BACKUP_SB)) {
129 backups = ocfs2_calc_new_backup_super(bm_inode,
130 group,
131 new_clusters,
132 first_new_cluster,
133 cl_cpg, 1);
134 le16_add_cpu(&group->bg_free_bits_count, -1 * backups);
135 }
136
137 ret = ocfs2_journal_dirty(handle, group_bh);
138 if (ret < 0) {
139 mlog_errno(ret);
140 goto out_rollback;
141 }
142
143 /* update the inode accordingly. */
144 ret = ocfs2_journal_access(handle, bm_inode, bm_bh,
145 OCFS2_JOURNAL_ACCESS_WRITE);
146 if (ret < 0) {
147 mlog_errno(ret);
148 goto out_rollback;
149 }
150
151 chain = le16_to_cpu(group->bg_chain);
152 cr = (&cl->cl_recs[chain]);
153 le32_add_cpu(&cr->c_total, num_bits);
154 le32_add_cpu(&cr->c_free, num_bits);
155 le32_add_cpu(&fe->id1.bitmap1.i_total, num_bits);
156 le32_add_cpu(&fe->i_clusters, new_clusters);
157
158 if (backups) {
159 le32_add_cpu(&cr->c_free, -1 * backups);
160 le32_add_cpu(&fe->id1.bitmap1.i_used, backups);
161 }
162
163 spin_lock(&OCFS2_I(bm_inode)->ip_lock);
164 OCFS2_I(bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
165 le64_add_cpu(&fe->i_size, new_clusters << osb->s_clustersize_bits);
166 spin_unlock(&OCFS2_I(bm_inode)->ip_lock);
167 i_size_write(bm_inode, le64_to_cpu(fe->i_size));
168
169 ocfs2_journal_dirty(handle, bm_bh);
170
171out_rollback:
172 if (ret < 0) {
173 ocfs2_calc_new_backup_super(bm_inode,
174 group,
175 new_clusters,
176 first_new_cluster,
177 cl_cpg, 0);
178 le16_add_cpu(&group->bg_free_bits_count, backups);
179 le16_add_cpu(&group->bg_bits, -1 * num_bits);
180 le16_add_cpu(&group->bg_free_bits_count, -1 * num_bits);
181 }
182out:
183 mlog_exit(ret);
184 return ret;
185}
186
187static int update_backups(struct inode * inode, u32 clusters, char *data)
188{
189 int i, ret = 0;
190 u32 cluster;
191 u64 blkno;
192 struct buffer_head *backup = NULL;
193 struct ocfs2_dinode *backup_di = NULL;
194 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
195
196 /* calculate the real backups we need to update. */
197 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
198 blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
199 cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
200 if (cluster > clusters)
201 break;
202
203 ret = ocfs2_read_block(osb, blkno, &backup, 0, NULL);
204 if (ret < 0) {
205 mlog_errno(ret);
206 break;
207 }
208
209 memcpy(backup->b_data, data, inode->i_sb->s_blocksize);
210
211 backup_di = (struct ocfs2_dinode *)backup->b_data;
212 backup_di->i_blkno = cpu_to_le64(blkno);
213
214 ret = ocfs2_write_super_or_backup(osb, backup);
215 brelse(backup);
216 backup = NULL;
217 if (ret < 0) {
218 mlog_errno(ret);
219 break;
220 }
221 }
222
223 return ret;
224}
225
226static void ocfs2_update_super_and_backups(struct inode *inode,
227 int new_clusters)
228{
229 int ret;
230 u32 clusters = 0;
231 struct buffer_head *super_bh = NULL;
232 struct ocfs2_dinode *super_di = NULL;
233 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
234
235 /*
236 * update the superblock last.
237 * It doesn't matter if the write failed.
238 */
239 ret = ocfs2_read_block(osb, OCFS2_SUPER_BLOCK_BLKNO,
240 &super_bh, 0, NULL);
241 if (ret < 0) {
242 mlog_errno(ret);
243 goto out;
244 }
245
246 super_di = (struct ocfs2_dinode *)super_bh->b_data;
247 le32_add_cpu(&super_di->i_clusters, new_clusters);
248 clusters = le32_to_cpu(super_di->i_clusters);
249
250 ret = ocfs2_write_super_or_backup(osb, super_bh);
251 if (ret < 0) {
252 mlog_errno(ret);
253 goto out;
254 }
255
256 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB))
257 ret = update_backups(inode, clusters, super_bh->b_data);
258
259out:
260 brelse(super_bh);
261 if (ret)
262 printk(KERN_WARNING "ocfs2: Failed to update super blocks on %s"
263 " during fs resize. This condition is not fatal,"
264 " but fsck.ocfs2 should be run to fix it\n",
265 osb->dev_str);
266 return;
267}
268
269/*
270 * Extend the filesystem to the new number of clusters specified. This entry
271 * point is only used to extend the current filesystem to the end of the last
272 * existing group.
273 */
274int ocfs2_group_extend(struct inode * inode, int new_clusters)
275{
276 int ret;
277 handle_t *handle;
278 struct buffer_head *main_bm_bh = NULL;
279 struct buffer_head *group_bh = NULL;
280 struct inode *main_bm_inode = NULL;
281 struct ocfs2_dinode *fe = NULL;
282 struct ocfs2_group_desc *group = NULL;
283 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
284 u16 cl_bpc;
285 u32 first_new_cluster;
286 u64 lgd_blkno;
287
288 mlog_entry_void();
289
290 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
291 return -EROFS;
292
293 if (new_clusters < 0)
294 return -EINVAL;
295 else if (new_clusters == 0)
296 return 0;
297
298 main_bm_inode = ocfs2_get_system_file_inode(osb,
299 GLOBAL_BITMAP_SYSTEM_INODE,
300 OCFS2_INVALID_SLOT);
301 if (!main_bm_inode) {
302 ret = -EINVAL;
303 mlog_errno(ret);
304 goto out;
305 }
306
307 mutex_lock(&main_bm_inode->i_mutex);
308
309 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
310 if (ret < 0) {
311 mlog_errno(ret);
312 goto out_mutex;
313 }
314
315 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
316
317 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
318 ocfs2_group_bitmap_size(osb->sb) * 8) {
319 mlog(ML_ERROR, "The disk is too old and small. "
320 "Force to do offline resize.");
321 ret = -EINVAL;
322 goto out_unlock;
323 }
324
325 if (!OCFS2_IS_VALID_DINODE(fe)) {
326 OCFS2_RO_ON_INVALID_DINODE(main_bm_inode->i_sb, fe);
327 ret = -EIO;
328 goto out_unlock;
329 }
330
331 first_new_cluster = le32_to_cpu(fe->i_clusters);
332 lgd_blkno = ocfs2_which_cluster_group(main_bm_inode,
333 first_new_cluster - 1);
334
335 ret = ocfs2_read_block(osb, lgd_blkno, &group_bh, OCFS2_BH_CACHED,
336 main_bm_inode);
337 if (ret < 0) {
338 mlog_errno(ret);
339 goto out_unlock;
340 }
341
342 group = (struct ocfs2_group_desc *)group_bh->b_data;
343
344 ret = ocfs2_check_group_descriptor(inode->i_sb, fe, group);
345 if (ret) {
346 mlog_errno(ret);
347 goto out_unlock;
348 }
349
350 cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
351 if (le16_to_cpu(group->bg_bits) / cl_bpc + new_clusters >
352 le16_to_cpu(fe->id2.i_chain.cl_cpg)) {
353 ret = -EINVAL;
354 goto out_unlock;
355 }
356
357 mlog(0, "extend the last group at %llu, new clusters = %d\n",
358 (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters);
359
360 handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS);
361 if (IS_ERR(handle)) {
362 mlog_errno(PTR_ERR(handle));
363 ret = -EINVAL;
364 goto out_unlock;
365 }
366
367 /* update the last group descriptor and inode. */
368 ret = ocfs2_update_last_group_and_inode(handle, main_bm_inode,
369 main_bm_bh, group_bh,
370 first_new_cluster,
371 new_clusters);
372 if (ret) {
373 mlog_errno(ret);
374 goto out_commit;
375 }
376
377 ocfs2_update_super_and_backups(main_bm_inode, new_clusters);
378
379out_commit:
380 ocfs2_commit_trans(osb, handle);
381out_unlock:
382 brelse(group_bh);
383 brelse(main_bm_bh);
384
385 ocfs2_inode_unlock(main_bm_inode, 1);
386
387out_mutex:
388 mutex_unlock(&main_bm_inode->i_mutex);
389 iput(main_bm_inode);
390
391out:
392 mlog_exit_void();
393 return ret;
394}
395
396static int ocfs2_check_new_group(struct inode *inode,
397 struct ocfs2_dinode *di,
398 struct ocfs2_new_group_input *input,
399 struct buffer_head *group_bh)
400{
401 int ret;
402 struct ocfs2_group_desc *gd;
403 u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc);
404 unsigned int max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) *
405 le16_to_cpu(di->id2.i_chain.cl_bpc);
406
407
408 gd = (struct ocfs2_group_desc *)group_bh->b_data;
409
410 ret = -EIO;
411 if (!OCFS2_IS_VALID_GROUP_DESC(gd))
412 mlog(ML_ERROR, "Group descriptor # %llu isn't valid.\n",
413 (unsigned long long)le64_to_cpu(gd->bg_blkno));
414 else if (di->i_blkno != gd->bg_parent_dinode)
415 mlog(ML_ERROR, "Group descriptor # %llu has bad parent "
416 "pointer (%llu, expected %llu)\n",
417 (unsigned long long)le64_to_cpu(gd->bg_blkno),
418 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
419 (unsigned long long)le64_to_cpu(di->i_blkno));
420 else if (le16_to_cpu(gd->bg_bits) > max_bits)
421 mlog(ML_ERROR, "Group descriptor # %llu has bit count of %u\n",
422 (unsigned long long)le64_to_cpu(gd->bg_blkno),
423 le16_to_cpu(gd->bg_bits));
424 else if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits))
425 mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but "
426 "claims that %u are free\n",
427 (unsigned long long)le64_to_cpu(gd->bg_blkno),
428 le16_to_cpu(gd->bg_bits),
429 le16_to_cpu(gd->bg_free_bits_count));
430 else if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size)))
431 mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but "
432 "max bitmap bits of %u\n",
433 (unsigned long long)le64_to_cpu(gd->bg_blkno),
434 le16_to_cpu(gd->bg_bits),
435 8 * le16_to_cpu(gd->bg_size));
436 else if (le16_to_cpu(gd->bg_chain) != input->chain)
437 mlog(ML_ERROR, "Group descriptor # %llu has bad chain %u "
438 "while input has %u set.\n",
439 (unsigned long long)le64_to_cpu(gd->bg_blkno),
440 le16_to_cpu(gd->bg_chain), input->chain);
441 else if (le16_to_cpu(gd->bg_bits) != input->clusters * cl_bpc)
442 mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but "
443 "input has %u clusters set\n",
444 (unsigned long long)le64_to_cpu(gd->bg_blkno),
445 le16_to_cpu(gd->bg_bits), input->clusters);
446 else if (le16_to_cpu(gd->bg_free_bits_count) != input->frees * cl_bpc)
447 mlog(ML_ERROR, "Group descriptor # %llu has free bit count %u "
448 "but it should have %u set\n",
449 (unsigned long long)le64_to_cpu(gd->bg_blkno),
450 le16_to_cpu(gd->bg_bits),
451 input->frees * cl_bpc);
452 else
453 ret = 0;
454
455 return ret;
456}
457
458static int ocfs2_verify_group_and_input(struct inode *inode,
459 struct ocfs2_dinode *di,
460 struct ocfs2_new_group_input *input,
461 struct buffer_head *group_bh)
462{
463 u16 cl_count = le16_to_cpu(di->id2.i_chain.cl_count);
464 u16 cl_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg);
465 u16 next_free = le16_to_cpu(di->id2.i_chain.cl_next_free_rec);
466 u32 cluster = ocfs2_blocks_to_clusters(inode->i_sb, input->group);
467 u32 total_clusters = le32_to_cpu(di->i_clusters);
468 int ret = -EINVAL;
469
470 if (cluster < total_clusters)
471 mlog(ML_ERROR, "add a group which is in the current volume.\n");
472 else if (input->chain >= cl_count)
473 mlog(ML_ERROR, "input chain exceeds the limit.\n");
474 else if (next_free != cl_count && next_free != input->chain)
475 mlog(ML_ERROR,
476 "the add group should be in chain %u\n", next_free);
477 else if (total_clusters + input->clusters < total_clusters)
478 mlog(ML_ERROR, "add group's clusters overflow.\n");
479 else if (input->clusters > cl_cpg)
480 mlog(ML_ERROR, "the cluster exceeds the maximum of a group\n");
481 else if (input->frees > input->clusters)
482 mlog(ML_ERROR, "the free cluster exceeds the total clusters\n");
483 else if (total_clusters % cl_cpg != 0)
484 mlog(ML_ERROR,
485 "the last group isn't full. Use group extend first.\n");
486 else if (input->group != ocfs2_which_cluster_group(inode, cluster))
487 mlog(ML_ERROR, "group blkno is invalid\n");
488 else if ((ret = ocfs2_check_new_group(inode, di, input, group_bh)))
489 mlog(ML_ERROR, "group descriptor check failed.\n");
490 else
491 ret = 0;
492
493 return ret;
494}
495
496/* Add a new group descriptor to global_bitmap. */
497int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input)
498{
499 int ret;
500 handle_t *handle;
501 struct buffer_head *main_bm_bh = NULL;
502 struct inode *main_bm_inode = NULL;
503 struct ocfs2_dinode *fe = NULL;
504 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
505 struct buffer_head *group_bh = NULL;
506 struct ocfs2_group_desc *group = NULL;
507 struct ocfs2_chain_list *cl;
508 struct ocfs2_chain_rec *cr;
509 u16 cl_bpc;
510
511 mlog_entry_void();
512
513 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
514 return -EROFS;
515
516 main_bm_inode = ocfs2_get_system_file_inode(osb,
517 GLOBAL_BITMAP_SYSTEM_INODE,
518 OCFS2_INVALID_SLOT);
519 if (!main_bm_inode) {
520 ret = -EINVAL;
521 mlog_errno(ret);
522 goto out;
523 }
524
525 mutex_lock(&main_bm_inode->i_mutex);
526
527 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
528 if (ret < 0) {
529 mlog_errno(ret);
530 goto out_mutex;
531 }
532
533 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
534
535 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
536 ocfs2_group_bitmap_size(osb->sb) * 8) {
537 mlog(ML_ERROR, "The disk is too old and small."
538 " Force to do offline resize.");
539 ret = -EINVAL;
540 goto out_unlock;
541 }
542
543 ret = ocfs2_read_block(osb, input->group, &group_bh, 0, NULL);
544 if (ret < 0) {
545 mlog(ML_ERROR, "Can't read the group descriptor # %llu "
546 "from the device.", (unsigned long long)input->group);
547 goto out_unlock;
548 }
549
550 ocfs2_set_new_buffer_uptodate(inode, group_bh);
551
552 ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh);
553 if (ret) {
554 mlog_errno(ret);
555 goto out_unlock;
556 }
557
558 mlog(0, "Add a new group %llu in chain = %u, length = %u\n",
559 (unsigned long long)input->group, input->chain, input->clusters);
560
561 handle = ocfs2_start_trans(osb, OCFS2_GROUP_ADD_CREDITS);
562 if (IS_ERR(handle)) {
563 mlog_errno(PTR_ERR(handle));
564 ret = -EINVAL;
565 goto out_unlock;
566 }
567
568 cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
569 cl = &fe->id2.i_chain;
570 cr = &cl->cl_recs[input->chain];
571
572 ret = ocfs2_journal_access(handle, main_bm_inode, group_bh,
573 OCFS2_JOURNAL_ACCESS_WRITE);
574 if (ret < 0) {
575 mlog_errno(ret);
576 goto out_commit;
577 }
578
579 group = (struct ocfs2_group_desc *)group_bh->b_data;
580 group->bg_next_group = cr->c_blkno;
581
582 ret = ocfs2_journal_dirty(handle, group_bh);
583 if (ret < 0) {
584 mlog_errno(ret);
585 goto out_commit;
586 }
587
588 ret = ocfs2_journal_access(handle, main_bm_inode, main_bm_bh,
589 OCFS2_JOURNAL_ACCESS_WRITE);
590 if (ret < 0) {
591 mlog_errno(ret);
592 goto out_commit;
593 }
594
595 if (input->chain == le16_to_cpu(cl->cl_next_free_rec)) {
596 le16_add_cpu(&cl->cl_next_free_rec, 1);
597 memset(cr, 0, sizeof(struct ocfs2_chain_rec));
598 }
599
600 cr->c_blkno = le64_to_cpu(input->group);
601 le32_add_cpu(&cr->c_total, input->clusters * cl_bpc);
602 le32_add_cpu(&cr->c_free, input->frees * cl_bpc);
603
604 le32_add_cpu(&fe->id1.bitmap1.i_total, input->clusters *cl_bpc);
605 le32_add_cpu(&fe->id1.bitmap1.i_used,
606 (input->clusters - input->frees) * cl_bpc);
607 le32_add_cpu(&fe->i_clusters, input->clusters);
608
609 ocfs2_journal_dirty(handle, main_bm_bh);
610
611 spin_lock(&OCFS2_I(main_bm_inode)->ip_lock);
612 OCFS2_I(main_bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
613 le64_add_cpu(&fe->i_size, input->clusters << osb->s_clustersize_bits);
614 spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock);
615 i_size_write(main_bm_inode, le64_to_cpu(fe->i_size));
616
617 ocfs2_update_super_and_backups(main_bm_inode, input->clusters);
618
619out_commit:
620 ocfs2_commit_trans(osb, handle);
621out_unlock:
622 brelse(group_bh);
623 brelse(main_bm_bh);
624
625 ocfs2_inode_unlock(main_bm_inode, 1);
626
627out_mutex:
628 mutex_unlock(&main_bm_inode->i_mutex);
629 iput(main_bm_inode);
630
631out:
632 mlog_exit_void();
633 return ret;
634}
diff --git a/fs/ocfs2/resize.h b/fs/ocfs2/resize.h
new file mode 100644
index 000000000000..f38841abf10b
--- /dev/null
+++ b/fs/ocfs2/resize.h
@@ -0,0 +1,32 @@
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * resize.h
5 *
6 * Function prototypes
7 *
8 * Copyright (C) 2007 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (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 GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#ifndef OCFS2_RESIZE_H
27#define OCFS2_RESIZE_H
28
29int ocfs2_group_extend(struct inode * inode, int new_clusters);
30int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input);
31
32#endif /* OCFS2_RESIZE_H */
diff --git a/fs/ocfs2/slot_map.c b/fs/ocfs2/slot_map.c
index af4882b62cfa..3a50ce555e64 100644
--- a/fs/ocfs2/slot_map.c
+++ b/fs/ocfs2/slot_map.c
@@ -48,25 +48,6 @@ static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
48 s16 slot_num, 48 s16 slot_num,
49 s16 node_num); 49 s16 node_num);
50 50
51/* Use the slot information we've collected to create a map of mounted
52 * nodes. Should be holding an EX on super block. assumes slot info is
53 * up to date. Note that we call this *after* we find a slot, so our
54 * own node should be set in the map too... */
55void ocfs2_populate_mounted_map(struct ocfs2_super *osb)
56{
57 int i;
58 struct ocfs2_slot_info *si = osb->slot_info;
59
60 spin_lock(&si->si_lock);
61
62 for (i = 0; i < si->si_size; i++)
63 if (si->si_global_node_nums[i] != OCFS2_INVALID_SLOT)
64 ocfs2_node_map_set_bit(osb, &osb->mounted_map,
65 si->si_global_node_nums[i]);
66
67 spin_unlock(&si->si_lock);
68}
69
70/* post the slot information on disk into our slot_info struct. */ 51/* post the slot information on disk into our slot_info struct. */
71void ocfs2_update_slot_info(struct ocfs2_slot_info *si) 52void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
72{ 53{
diff --git a/fs/ocfs2/slot_map.h b/fs/ocfs2/slot_map.h
index d8c8ceed031b..1025872aaade 100644
--- a/fs/ocfs2/slot_map.h
+++ b/fs/ocfs2/slot_map.h
@@ -52,8 +52,6 @@ s16 ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
52void ocfs2_clear_slot(struct ocfs2_slot_info *si, 52void ocfs2_clear_slot(struct ocfs2_slot_info *si,
53 s16 slot_num); 53 s16 slot_num);
54 54
55void ocfs2_populate_mounted_map(struct ocfs2_super *osb);
56
57static inline int ocfs2_is_empty_slot(struct ocfs2_slot_info *si, 55static inline int ocfs2_is_empty_slot(struct ocfs2_slot_info *si,
58 int slot_num) 56 int slot_num)
59{ 57{
diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c
index 8f09f5235e3a..7e397e2c25dd 100644
--- a/fs/ocfs2/suballoc.c
+++ b/fs/ocfs2/suballoc.c
@@ -101,8 +101,6 @@ static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg
101static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, 101static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
102 u64 bg_blkno, 102 u64 bg_blkno,
103 u16 bg_bit_off); 103 u16 bg_bit_off);
104static inline u64 ocfs2_which_cluster_group(struct inode *inode,
105 u32 cluster);
106static inline void ocfs2_block_to_cluster_group(struct inode *inode, 104static inline void ocfs2_block_to_cluster_group(struct inode *inode,
107 u64 data_blkno, 105 u64 data_blkno,
108 u64 *bg_blkno, 106 u64 *bg_blkno,
@@ -114,7 +112,7 @@ void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
114 112
115 if (inode) { 113 if (inode) {
116 if (ac->ac_which != OCFS2_AC_USE_LOCAL) 114 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
117 ocfs2_meta_unlock(inode, 1); 115 ocfs2_inode_unlock(inode, 1);
118 116
119 mutex_unlock(&inode->i_mutex); 117 mutex_unlock(&inode->i_mutex);
120 118
@@ -131,9 +129,9 @@ static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
131} 129}
132 130
133/* somewhat more expensive than our other checks, so use sparingly. */ 131/* somewhat more expensive than our other checks, so use sparingly. */
134static int ocfs2_check_group_descriptor(struct super_block *sb, 132int ocfs2_check_group_descriptor(struct super_block *sb,
135 struct ocfs2_dinode *di, 133 struct ocfs2_dinode *di,
136 struct ocfs2_group_desc *gd) 134 struct ocfs2_group_desc *gd)
137{ 135{
138 unsigned int max_bits; 136 unsigned int max_bits;
139 137
@@ -412,7 +410,7 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
412 410
413 mutex_lock(&alloc_inode->i_mutex); 411 mutex_lock(&alloc_inode->i_mutex);
414 412
415 status = ocfs2_meta_lock(alloc_inode, &bh, 1); 413 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
416 if (status < 0) { 414 if (status < 0) {
417 mutex_unlock(&alloc_inode->i_mutex); 415 mutex_unlock(&alloc_inode->i_mutex);
418 iput(alloc_inode); 416 iput(alloc_inode);
@@ -1443,8 +1441,7 @@ static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1443 1441
1444/* given a cluster offset, calculate which block group it belongs to 1442/* given a cluster offset, calculate which block group it belongs to
1445 * and return that block offset. */ 1443 * and return that block offset. */
1446static inline u64 ocfs2_which_cluster_group(struct inode *inode, 1444u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1447 u32 cluster)
1448{ 1445{
1449 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1446 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1450 u32 group_no; 1447 u32 group_no;
@@ -1519,8 +1516,9 @@ int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1519 if (min_clusters > (osb->bitmap_cpg - 1)) { 1516 if (min_clusters > (osb->bitmap_cpg - 1)) {
1520 /* The only paths asking for contiguousness 1517 /* The only paths asking for contiguousness
1521 * should know about this already. */ 1518 * should know about this already. */
1522 mlog(ML_ERROR, "minimum allocation requested exceeds " 1519 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1523 "group bitmap size!"); 1520 "group bitmap size %u!\n", min_clusters,
1521 osb->bitmap_cpg);
1524 status = -ENOSPC; 1522 status = -ENOSPC;
1525 goto bail; 1523 goto bail;
1526 } 1524 }
diff --git a/fs/ocfs2/suballoc.h b/fs/ocfs2/suballoc.h
index cafe93703095..8799033bb459 100644
--- a/fs/ocfs2/suballoc.h
+++ b/fs/ocfs2/suballoc.h
@@ -147,4 +147,12 @@ static inline int ocfs2_is_cluster_bitmap(struct inode *inode)
147int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb, 147int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
148 struct ocfs2_alloc_context *ac); 148 struct ocfs2_alloc_context *ac);
149 149
150/* given a cluster offset, calculate which block group it belongs to
151 * and return that block offset. */
152u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster);
153
154/* somewhat more expensive than our other checks, so use sparingly. */
155int ocfs2_check_group_descriptor(struct super_block *sb,
156 struct ocfs2_dinode *di,
157 struct ocfs2_group_desc *gd);
150#endif /* _CHAINALLOC_H_ */ 158#endif /* _CHAINALLOC_H_ */
diff --git a/fs/ocfs2/super.c b/fs/ocfs2/super.c
index 5ee775420665..01fe40ee5ea9 100644
--- a/fs/ocfs2/super.c
+++ b/fs/ocfs2/super.c
@@ -65,7 +65,6 @@
65#include "sysfile.h" 65#include "sysfile.h"
66#include "uptodate.h" 66#include "uptodate.h"
67#include "ver.h" 67#include "ver.h"
68#include "vote.h"
69 68
70#include "buffer_head_io.h" 69#include "buffer_head_io.h"
71 70
@@ -84,9 +83,11 @@ MODULE_LICENSE("GPL");
84 83
85struct mount_options 84struct mount_options
86{ 85{
86 unsigned long commit_interval;
87 unsigned long mount_opt; 87 unsigned long mount_opt;
88 unsigned int atime_quantum; 88 unsigned int atime_quantum;
89 signed short slot; 89 signed short slot;
90 unsigned int localalloc_opt;
90}; 91};
91 92
92static int ocfs2_parse_options(struct super_block *sb, char *options, 93static int ocfs2_parse_options(struct super_block *sb, char *options,
@@ -150,6 +151,9 @@ enum {
150 Opt_data_writeback, 151 Opt_data_writeback,
151 Opt_atime_quantum, 152 Opt_atime_quantum,
152 Opt_slot, 153 Opt_slot,
154 Opt_commit,
155 Opt_localalloc,
156 Opt_localflocks,
153 Opt_err, 157 Opt_err,
154}; 158};
155 159
@@ -165,6 +169,9 @@ static match_table_t tokens = {
165 {Opt_data_writeback, "data=writeback"}, 169 {Opt_data_writeback, "data=writeback"},
166 {Opt_atime_quantum, "atime_quantum=%u"}, 170 {Opt_atime_quantum, "atime_quantum=%u"},
167 {Opt_slot, "preferred_slot=%u"}, 171 {Opt_slot, "preferred_slot=%u"},
172 {Opt_commit, "commit=%u"},
173 {Opt_localalloc, "localalloc=%d"},
174 {Opt_localflocks, "localflocks"},
168 {Opt_err, NULL} 175 {Opt_err, NULL}
169}; 176};
170 177
@@ -213,7 +220,7 @@ static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
213 220
214 mlog_entry_void(); 221 mlog_entry_void();
215 222
216 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE); 223 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
217 if (IS_ERR(new)) { 224 if (IS_ERR(new)) {
218 status = PTR_ERR(new); 225 status = PTR_ERR(new);
219 mlog_errno(status); 226 mlog_errno(status);
@@ -221,7 +228,7 @@ static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
221 } 228 }
222 osb->root_inode = new; 229 osb->root_inode = new;
223 230
224 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE); 231 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
225 if (IS_ERR(new)) { 232 if (IS_ERR(new)) {
226 status = PTR_ERR(new); 233 status = PTR_ERR(new);
227 mlog_errno(status); 234 mlog_errno(status);
@@ -443,6 +450,8 @@ unlock_osb:
443 osb->s_mount_opt = parsed_options.mount_opt; 450 osb->s_mount_opt = parsed_options.mount_opt;
444 osb->s_atime_quantum = parsed_options.atime_quantum; 451 osb->s_atime_quantum = parsed_options.atime_quantum;
445 osb->preferred_slot = parsed_options.slot; 452 osb->preferred_slot = parsed_options.slot;
453 if (parsed_options.commit_interval)
454 osb->osb_commit_interval = parsed_options.commit_interval;
446 455
447 if (!ocfs2_is_hard_readonly(osb)) 456 if (!ocfs2_is_hard_readonly(osb))
448 ocfs2_set_journal_params(osb); 457 ocfs2_set_journal_params(osb);
@@ -597,6 +606,8 @@ static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
597 osb->s_mount_opt = parsed_options.mount_opt; 606 osb->s_mount_opt = parsed_options.mount_opt;
598 osb->s_atime_quantum = parsed_options.atime_quantum; 607 osb->s_atime_quantum = parsed_options.atime_quantum;
599 osb->preferred_slot = parsed_options.slot; 608 osb->preferred_slot = parsed_options.slot;
609 osb->osb_commit_interval = parsed_options.commit_interval;
610 osb->local_alloc_size = parsed_options.localalloc_opt;
600 611
601 sb->s_magic = OCFS2_SUPER_MAGIC; 612 sb->s_magic = OCFS2_SUPER_MAGIC;
602 613
@@ -747,9 +758,11 @@ static int ocfs2_parse_options(struct super_block *sb,
747 mlog_entry("remount: %d, options: \"%s\"\n", is_remount, 758 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
748 options ? options : "(none)"); 759 options ? options : "(none)");
749 760
761 mopt->commit_interval = 0;
750 mopt->mount_opt = 0; 762 mopt->mount_opt = 0;
751 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM; 763 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
752 mopt->slot = OCFS2_INVALID_SLOT; 764 mopt->slot = OCFS2_INVALID_SLOT;
765 mopt->localalloc_opt = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
753 766
754 if (!options) { 767 if (!options) {
755 status = 1; 768 status = 1;
@@ -816,6 +829,41 @@ static int ocfs2_parse_options(struct super_block *sb,
816 if (option) 829 if (option)
817 mopt->slot = (s16)option; 830 mopt->slot = (s16)option;
818 break; 831 break;
832 case Opt_commit:
833 option = 0;
834 if (match_int(&args[0], &option)) {
835 status = 0;
836 goto bail;
837 }
838 if (option < 0)
839 return 0;
840 if (option == 0)
841 option = JBD_DEFAULT_MAX_COMMIT_AGE;
842 mopt->commit_interval = HZ * option;
843 break;
844 case Opt_localalloc:
845 option = 0;
846 if (match_int(&args[0], &option)) {
847 status = 0;
848 goto bail;
849 }
850 if (option >= 0 && (option <= ocfs2_local_alloc_size(sb) * 8))
851 mopt->localalloc_opt = option;
852 break;
853 case Opt_localflocks:
854 /*
855 * Changing this during remount could race
856 * flock() requests, or "unbalance" existing
857 * ones (e.g., a lock is taken in one mode but
858 * dropped in the other). If users care enough
859 * to flip locking modes during remount, we
860 * could add a "local" flag to individual
861 * flock structures for proper tracking of
862 * state.
863 */
864 if (!is_remount)
865 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
866 break;
819 default: 867 default:
820 mlog(ML_ERROR, 868 mlog(ML_ERROR,
821 "Unrecognized mount option \"%s\" " 869 "Unrecognized mount option \"%s\" "
@@ -864,6 +912,16 @@ static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
864 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM) 912 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
865 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum); 913 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
866 914
915 if (osb->osb_commit_interval)
916 seq_printf(s, ",commit=%u",
917 (unsigned) (osb->osb_commit_interval / HZ));
918
919 if (osb->local_alloc_size != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
920 seq_printf(s, ",localalloc=%d", osb->local_alloc_size);
921
922 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
923 seq_printf(s, ",localflocks,");
924
867 return 0; 925 return 0;
868} 926}
869 927
@@ -965,7 +1023,7 @@ static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
965 goto bail; 1023 goto bail;
966 } 1024 }
967 1025
968 status = ocfs2_meta_lock(inode, &bh, 0); 1026 status = ocfs2_inode_lock(inode, &bh, 0);
969 if (status < 0) { 1027 if (status < 0) {
970 mlog_errno(status); 1028 mlog_errno(status);
971 goto bail; 1029 goto bail;
@@ -989,7 +1047,7 @@ static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
989 1047
990 brelse(bh); 1048 brelse(bh);
991 1049
992 ocfs2_meta_unlock(inode, 0); 1050 ocfs2_inode_unlock(inode, 0);
993 status = 0; 1051 status = 0;
994bail: 1052bail:
995 if (inode) 1053 if (inode)
@@ -1020,8 +1078,7 @@ static void ocfs2_inode_init_once(struct kmem_cache *cachep, void *data)
1020 oi->ip_clusters = 0; 1078 oi->ip_clusters = 0;
1021 1079
1022 ocfs2_lock_res_init_once(&oi->ip_rw_lockres); 1080 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
1023 ocfs2_lock_res_init_once(&oi->ip_meta_lockres); 1081 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
1024 ocfs2_lock_res_init_once(&oi->ip_data_lockres);
1025 ocfs2_lock_res_init_once(&oi->ip_open_lockres); 1082 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
1026 1083
1027 ocfs2_metadata_cache_init(&oi->vfs_inode); 1084 ocfs2_metadata_cache_init(&oi->vfs_inode);
@@ -1117,25 +1174,12 @@ static int ocfs2_mount_volume(struct super_block *sb)
1117 goto leave; 1174 goto leave;
1118 } 1175 }
1119 1176
1120 status = ocfs2_register_hb_callbacks(osb);
1121 if (status < 0) {
1122 mlog_errno(status);
1123 goto leave;
1124 }
1125
1126 status = ocfs2_dlm_init(osb); 1177 status = ocfs2_dlm_init(osb);
1127 if (status < 0) { 1178 if (status < 0) {
1128 mlog_errno(status); 1179 mlog_errno(status);
1129 goto leave; 1180 goto leave;
1130 } 1181 }
1131 1182
1132 /* requires vote_thread to be running. */
1133 status = ocfs2_register_net_handlers(osb);
1134 if (status < 0) {
1135 mlog_errno(status);
1136 goto leave;
1137 }
1138
1139 status = ocfs2_super_lock(osb, 1); 1183 status = ocfs2_super_lock(osb, 1);
1140 if (status < 0) { 1184 if (status < 0) {
1141 mlog_errno(status); 1185 mlog_errno(status);
@@ -1150,8 +1194,6 @@ static int ocfs2_mount_volume(struct super_block *sb)
1150 goto leave; 1194 goto leave;
1151 } 1195 }
1152 1196
1153 ocfs2_populate_mounted_map(osb);
1154
1155 /* load all node-local system inodes */ 1197 /* load all node-local system inodes */
1156 status = ocfs2_init_local_system_inodes(osb); 1198 status = ocfs2_init_local_system_inodes(osb);
1157 if (status < 0) { 1199 if (status < 0) {
@@ -1174,15 +1216,6 @@ static int ocfs2_mount_volume(struct super_block *sb)
1174 if (ocfs2_mount_local(osb)) 1216 if (ocfs2_mount_local(osb))
1175 goto leave; 1217 goto leave;
1176 1218
1177 /* This should be sent *after* we recovered our journal as it
1178 * will cause other nodes to unmark us as needing
1179 * recovery. However, we need to send it *before* dropping the
1180 * super block lock as otherwise their recovery threads might
1181 * try to clean us up while we're live! */
1182 status = ocfs2_request_mount_vote(osb);
1183 if (status < 0)
1184 mlog_errno(status);
1185
1186leave: 1219leave:
1187 if (unlock_super) 1220 if (unlock_super)
1188 ocfs2_super_unlock(osb, 1); 1221 ocfs2_super_unlock(osb, 1);
@@ -1240,10 +1273,6 @@ static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1240 mlog_errno(tmp); 1273 mlog_errno(tmp);
1241 return; 1274 return;
1242 } 1275 }
1243
1244 tmp = ocfs2_request_umount_vote(osb);
1245 if (tmp < 0)
1246 mlog_errno(tmp);
1247 } 1276 }
1248 1277
1249 if (osb->slot_num != OCFS2_INVALID_SLOT) 1278 if (osb->slot_num != OCFS2_INVALID_SLOT)
@@ -1254,13 +1283,8 @@ static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1254 1283
1255 ocfs2_release_system_inodes(osb); 1284 ocfs2_release_system_inodes(osb);
1256 1285
1257 if (osb->dlm) { 1286 if (osb->dlm)
1258 ocfs2_unregister_net_handlers(osb);
1259
1260 ocfs2_dlm_shutdown(osb); 1287 ocfs2_dlm_shutdown(osb);
1261 }
1262
1263 ocfs2_clear_hb_callbacks(osb);
1264 1288
1265 debugfs_remove(osb->osb_debug_root); 1289 debugfs_remove(osb->osb_debug_root);
1266 1290
@@ -1315,7 +1339,6 @@ static int ocfs2_initialize_super(struct super_block *sb,
1315 int i, cbits, bbits; 1339 int i, cbits, bbits;
1316 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; 1340 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1317 struct inode *inode = NULL; 1341 struct inode *inode = NULL;
1318 struct buffer_head *bitmap_bh = NULL;
1319 struct ocfs2_journal *journal; 1342 struct ocfs2_journal *journal;
1320 __le32 uuid_net_key; 1343 __le32 uuid_net_key;
1321 struct ocfs2_super *osb; 1344 struct ocfs2_super *osb;
@@ -1344,19 +1367,13 @@ static int ocfs2_initialize_super(struct super_block *sb,
1344 osb->s_sectsize_bits = blksize_bits(sector_size); 1367 osb->s_sectsize_bits = blksize_bits(sector_size);
1345 BUG_ON(!osb->s_sectsize_bits); 1368 BUG_ON(!osb->s_sectsize_bits);
1346 1369
1347 osb->net_response_ids = 0;
1348 spin_lock_init(&osb->net_response_lock);
1349 INIT_LIST_HEAD(&osb->net_response_list);
1350
1351 INIT_LIST_HEAD(&osb->osb_net_handlers);
1352 init_waitqueue_head(&osb->recovery_event); 1370 init_waitqueue_head(&osb->recovery_event);
1353 spin_lock_init(&osb->vote_task_lock); 1371 spin_lock_init(&osb->dc_task_lock);
1354 init_waitqueue_head(&osb->vote_event); 1372 init_waitqueue_head(&osb->dc_event);
1355 osb->vote_work_sequence = 0; 1373 osb->dc_work_sequence = 0;
1356 osb->vote_wake_sequence = 0; 1374 osb->dc_wake_sequence = 0;
1357 INIT_LIST_HEAD(&osb->blocked_lock_list); 1375 INIT_LIST_HEAD(&osb->blocked_lock_list);
1358 osb->blocked_lock_count = 0; 1376 osb->blocked_lock_count = 0;
1359 INIT_LIST_HEAD(&osb->vote_list);
1360 spin_lock_init(&osb->osb_lock); 1377 spin_lock_init(&osb->osb_lock);
1361 1378
1362 atomic_set(&osb->alloc_stats.moves, 0); 1379 atomic_set(&osb->alloc_stats.moves, 0);
@@ -1496,7 +1513,6 @@ static int ocfs2_initialize_super(struct super_block *sb,
1496 } 1513 }
1497 1514
1498 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key)); 1515 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
1499 osb->net_key = le32_to_cpu(uuid_net_key);
1500 1516
1501 strncpy(osb->vol_label, di->id2.i_super.s_label, 63); 1517 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
1502 osb->vol_label[63] = '\0'; 1518 osb->vol_label[63] = '\0';
@@ -1539,25 +1555,9 @@ static int ocfs2_initialize_super(struct super_block *sb,
1539 } 1555 }
1540 1556
1541 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno; 1557 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
1542
1543 /* We don't have a cluster lock on the bitmap here because
1544 * we're only interested in static information and the extra
1545 * complexity at mount time isn't worht it. Don't pass the
1546 * inode in to the read function though as we don't want it to
1547 * be put in the cache. */
1548 status = ocfs2_read_block(osb, osb->bitmap_blkno, &bitmap_bh, 0,
1549 NULL);
1550 iput(inode); 1558 iput(inode);
1551 if (status < 0) {
1552 mlog_errno(status);
1553 goto bail;
1554 }
1555 1559
1556 di = (struct ocfs2_dinode *) bitmap_bh->b_data; 1560 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
1557 osb->bitmap_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg);
1558 brelse(bitmap_bh);
1559 mlog(0, "cluster bitmap inode: %llu, clusters per group: %u\n",
1560 (unsigned long long)osb->bitmap_blkno, osb->bitmap_cpg);
1561 1561
1562 status = ocfs2_init_slot_info(osb); 1562 status = ocfs2_init_slot_info(osb);
1563 if (status < 0) { 1563 if (status < 0) {
diff --git a/fs/ocfs2/sysfile.c b/fs/ocfs2/sysfile.c
index fd2e846e3e6f..ab713ebdd546 100644
--- a/fs/ocfs2/sysfile.c
+++ b/fs/ocfs2/sysfile.c
@@ -112,7 +112,7 @@ static struct inode * _ocfs2_get_system_file_inode(struct ocfs2_super *osb,
112 goto bail; 112 goto bail;
113 } 113 }
114 114
115 inode = ocfs2_iget(osb, blkno, OCFS2_FI_FLAG_SYSFILE); 115 inode = ocfs2_iget(osb, blkno, OCFS2_FI_FLAG_SYSFILE, type);
116 if (IS_ERR(inode)) { 116 if (IS_ERR(inode)) {
117 mlog_errno(PTR_ERR(inode)); 117 mlog_errno(PTR_ERR(inode));
118 inode = NULL; 118 inode = NULL;
diff --git a/fs/ocfs2/ver.c b/fs/ocfs2/ver.c
index 5405ce121c99..e2488f4128a2 100644
--- a/fs/ocfs2/ver.c
+++ b/fs/ocfs2/ver.c
@@ -29,7 +29,7 @@
29 29
30#include "ver.h" 30#include "ver.h"
31 31
32#define OCFS2_BUILD_VERSION "1.3.3" 32#define OCFS2_BUILD_VERSION "1.5.0"
33 33
34#define VERSION_STR "OCFS2 " OCFS2_BUILD_VERSION 34#define VERSION_STR "OCFS2 " OCFS2_BUILD_VERSION
35 35
diff --git a/fs/ocfs2/vote.c b/fs/ocfs2/vote.c
deleted file mode 100644
index c05358538f2b..000000000000
--- a/fs/ocfs2/vote.c
+++ /dev/null
@@ -1,756 +0,0 @@
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * vote.c
5 *
6 * description here
7 *
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (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 GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/types.h>
27#include <linux/slab.h>
28#include <linux/highmem.h>
29#include <linux/kthread.h>
30
31#include <cluster/heartbeat.h>
32#include <cluster/nodemanager.h>
33#include <cluster/tcp.h>
34
35#include <dlm/dlmapi.h>
36
37#define MLOG_MASK_PREFIX ML_VOTE
38#include <cluster/masklog.h>
39
40#include "ocfs2.h"
41
42#include "alloc.h"
43#include "dlmglue.h"
44#include "extent_map.h"
45#include "heartbeat.h"
46#include "inode.h"
47#include "journal.h"
48#include "slot_map.h"
49#include "vote.h"
50
51#include "buffer_head_io.h"
52
53#define OCFS2_MESSAGE_TYPE_VOTE (0x1)
54#define OCFS2_MESSAGE_TYPE_RESPONSE (0x2)
55struct ocfs2_msg_hdr
56{
57 __be32 h_response_id; /* used to lookup message handle on sending
58 * node. */
59 __be32 h_request;
60 __be64 h_blkno;
61 __be32 h_generation;
62 __be32 h_node_num; /* node sending this particular message. */
63};
64
65struct ocfs2_vote_msg
66{
67 struct ocfs2_msg_hdr v_hdr;
68 __be32 v_reserved1;
69} __attribute__ ((packed));
70
71/* Responses are given these values to maintain backwards
72 * compatibility with older ocfs2 versions */
73#define OCFS2_RESPONSE_OK (0)
74#define OCFS2_RESPONSE_BUSY (-16)
75#define OCFS2_RESPONSE_BAD_MSG (-22)
76
77struct ocfs2_response_msg
78{
79 struct ocfs2_msg_hdr r_hdr;
80 __be32 r_response;
81} __attribute__ ((packed));
82
83struct ocfs2_vote_work {
84 struct list_head w_list;
85 struct ocfs2_vote_msg w_msg;
86};
87
88enum ocfs2_vote_request {
89 OCFS2_VOTE_REQ_INVALID = 0,
90 OCFS2_VOTE_REQ_MOUNT,
91 OCFS2_VOTE_REQ_UMOUNT,
92 OCFS2_VOTE_REQ_LAST
93};
94
95static inline int ocfs2_is_valid_vote_request(int request)
96{
97 return OCFS2_VOTE_REQ_INVALID < request &&
98 request < OCFS2_VOTE_REQ_LAST;
99}
100
101typedef void (*ocfs2_net_response_callback)(void *priv,
102 struct ocfs2_response_msg *resp);
103struct ocfs2_net_response_cb {
104 ocfs2_net_response_callback rc_cb;
105 void *rc_priv;
106};
107
108struct ocfs2_net_wait_ctxt {
109 struct list_head n_list;
110 u32 n_response_id;
111 wait_queue_head_t n_event;
112 struct ocfs2_node_map n_node_map;
113 int n_response; /* an agreggate response. 0 if
114 * all nodes are go, < 0 on any
115 * negative response from any
116 * node or network error. */
117 struct ocfs2_net_response_cb *n_callback;
118};
119
120static void ocfs2_process_mount_request(struct ocfs2_super *osb,
121 unsigned int node_num)
122{
123 mlog(0, "MOUNT vote from node %u\n", node_num);
124 /* The other node only sends us this message when he has an EX
125 * on the superblock, so our recovery threads (if having been
126 * launched) are waiting on it.*/
127 ocfs2_recovery_map_clear(osb, node_num);
128 ocfs2_node_map_set_bit(osb, &osb->mounted_map, node_num);
129
130 /* We clear the umount map here because a node may have been
131 * previously mounted, safely unmounted but never stopped
132 * heartbeating - in which case we'd have a stale entry. */
133 ocfs2_node_map_clear_bit(osb, &osb->umount_map, node_num);
134}
135
136static void ocfs2_process_umount_request(struct ocfs2_super *osb,
137 unsigned int node_num)
138{
139 mlog(0, "UMOUNT vote from node %u\n", node_num);
140 ocfs2_node_map_clear_bit(osb, &osb->mounted_map, node_num);
141 ocfs2_node_map_set_bit(osb, &osb->umount_map, node_num);
142}
143
144static void ocfs2_process_vote(struct ocfs2_super *osb,
145 struct ocfs2_vote_msg *msg)
146{
147 int net_status, vote_response;
148 unsigned int node_num;
149 u64 blkno;
150 enum ocfs2_vote_request request;
151 struct ocfs2_msg_hdr *hdr = &msg->v_hdr;
152 struct ocfs2_response_msg response;
153
154 /* decode the network mumbo jumbo into local variables. */
155 request = be32_to_cpu(hdr->h_request);
156 blkno = be64_to_cpu(hdr->h_blkno);
157 node_num = be32_to_cpu(hdr->h_node_num);
158
159 mlog(0, "processing vote: request = %u, blkno = %llu, node_num = %u\n",
160 request, (unsigned long long)blkno, node_num);
161
162 if (!ocfs2_is_valid_vote_request(request)) {
163 mlog(ML_ERROR, "Invalid vote request %d from node %u\n",
164 request, node_num);
165 vote_response = OCFS2_RESPONSE_BAD_MSG;
166 goto respond;
167 }
168
169 vote_response = OCFS2_RESPONSE_OK;
170
171 switch (request) {
172 case OCFS2_VOTE_REQ_UMOUNT:
173 ocfs2_process_umount_request(osb, node_num);
174 goto respond;
175 case OCFS2_VOTE_REQ_MOUNT:
176 ocfs2_process_mount_request(osb, node_num);
177 goto respond;
178 default:
179 /* avoids a gcc warning */
180 break;
181 }
182
183respond:
184 /* Response struture is small so we just put it on the stack
185 * and stuff it inline. */
186 memset(&response, 0, sizeof(struct ocfs2_response_msg));
187 response.r_hdr.h_response_id = hdr->h_response_id;
188 response.r_hdr.h_blkno = hdr->h_blkno;
189 response.r_hdr.h_generation = hdr->h_generation;
190 response.r_hdr.h_node_num = cpu_to_be32(osb->node_num);
191 response.r_response = cpu_to_be32(vote_response);
192
193 net_status = o2net_send_message(OCFS2_MESSAGE_TYPE_RESPONSE,
194 osb->net_key,
195 &response,
196 sizeof(struct ocfs2_response_msg),
197 node_num,
198 NULL);
199 /* We still want to error print for ENOPROTOOPT here. The
200 * sending node shouldn't have unregistered his net handler
201 * without sending an unmount vote 1st */
202 if (net_status < 0
203 && net_status != -ETIMEDOUT
204 && net_status != -ENOTCONN)
205 mlog(ML_ERROR, "message to node %u fails with error %d!\n",
206 node_num, net_status);
207}
208
209static void ocfs2_vote_thread_do_work(struct ocfs2_super *osb)
210{
211 unsigned long processed;
212 struct ocfs2_lock_res *lockres;
213 struct ocfs2_vote_work *work;
214
215 mlog_entry_void();
216
217 spin_lock(&osb->vote_task_lock);
218 /* grab this early so we know to try again if a state change and
219 * wake happens part-way through our work */
220 osb->vote_work_sequence = osb->vote_wake_sequence;
221
222 processed = osb->blocked_lock_count;
223 while (processed) {
224 BUG_ON(list_empty(&osb->blocked_lock_list));
225
226 lockres = list_entry(osb->blocked_lock_list.next,
227 struct ocfs2_lock_res, l_blocked_list);
228 list_del_init(&lockres->l_blocked_list);
229 osb->blocked_lock_count--;
230 spin_unlock(&osb->vote_task_lock);
231
232 BUG_ON(!processed);
233 processed--;
234
235 ocfs2_process_blocked_lock(osb, lockres);
236
237 spin_lock(&osb->vote_task_lock);
238 }
239
240 while (osb->vote_count) {
241 BUG_ON(list_empty(&osb->vote_list));
242 work = list_entry(osb->vote_list.next,
243 struct ocfs2_vote_work, w_list);
244 list_del(&work->w_list);
245 osb->vote_count--;
246 spin_unlock(&osb->vote_task_lock);
247
248 ocfs2_process_vote(osb, &work->w_msg);
249 kfree(work);
250
251 spin_lock(&osb->vote_task_lock);
252 }
253 spin_unlock(&osb->vote_task_lock);
254
255 mlog_exit_void();
256}
257
258static int ocfs2_vote_thread_lists_empty(struct ocfs2_super *osb)
259{
260 int empty = 0;
261
262 spin_lock(&osb->vote_task_lock);
263 if (list_empty(&osb->blocked_lock_list) &&
264 list_empty(&osb->vote_list))
265 empty = 1;
266
267 spin_unlock(&osb->vote_task_lock);
268 return empty;
269}
270
271static int ocfs2_vote_thread_should_wake(struct ocfs2_super *osb)
272{
273 int should_wake = 0;
274
275 spin_lock(&osb->vote_task_lock);
276 if (osb->vote_work_sequence != osb->vote_wake_sequence)
277 should_wake = 1;
278 spin_unlock(&osb->vote_task_lock);
279
280 return should_wake;
281}
282
283int ocfs2_vote_thread(void *arg)
284{
285 int status = 0;
286 struct ocfs2_super *osb = arg;
287
288 /* only quit once we've been asked to stop and there is no more
289 * work available */
290 while (!(kthread_should_stop() &&
291 ocfs2_vote_thread_lists_empty(osb))) {
292
293 wait_event_interruptible(osb->vote_event,
294 ocfs2_vote_thread_should_wake(osb) ||
295 kthread_should_stop());
296
297 mlog(0, "vote_thread: awoken\n");
298
299 ocfs2_vote_thread_do_work(osb);
300 }
301
302 osb->vote_task = NULL;
303 return status;
304}
305
306static struct ocfs2_net_wait_ctxt *ocfs2_new_net_wait_ctxt(unsigned int response_id)
307{
308 struct ocfs2_net_wait_ctxt *w;
309
310 w = kzalloc(sizeof(*w), GFP_NOFS);
311 if (!w) {
312 mlog_errno(-ENOMEM);
313 goto bail;
314 }
315
316 INIT_LIST_HEAD(&w->n_list);
317 init_waitqueue_head(&w->n_event);
318 ocfs2_node_map_init(&w->n_node_map);
319 w->n_response_id = response_id;
320 w->n_callback = NULL;
321bail:
322 return w;
323}
324
325static unsigned int ocfs2_new_response_id(struct ocfs2_super *osb)
326{
327 unsigned int ret;
328
329 spin_lock(&osb->net_response_lock);
330 ret = ++osb->net_response_ids;
331 spin_unlock(&osb->net_response_lock);
332
333 return ret;
334}
335
336static void ocfs2_dequeue_net_wait_ctxt(struct ocfs2_super *osb,
337 struct ocfs2_net_wait_ctxt *w)
338{
339 spin_lock(&osb->net_response_lock);
340 list_del(&w->n_list);
341 spin_unlock(&osb->net_response_lock);
342}
343
344static void ocfs2_queue_net_wait_ctxt(struct ocfs2_super *osb,
345 struct ocfs2_net_wait_ctxt *w)
346{
347 spin_lock(&osb->net_response_lock);
348 list_add_tail(&w->n_list,
349 &osb->net_response_list);
350 spin_unlock(&osb->net_response_lock);
351}
352
353static void __ocfs2_mark_node_responded(struct ocfs2_super *osb,
354 struct ocfs2_net_wait_ctxt *w,
355 int node_num)
356{
357 assert_spin_locked(&osb->net_response_lock);
358
359 ocfs2_node_map_clear_bit(osb, &w->n_node_map, node_num);
360 if (ocfs2_node_map_is_empty(osb, &w->n_node_map))
361 wake_up(&w->n_event);
362}
363
364/* Intended to be called from the node down callback, we fake remove
365 * the node from all our response contexts */
366void ocfs2_remove_node_from_vote_queues(struct ocfs2_super *osb,
367 int node_num)
368{
369 struct list_head *p;
370 struct ocfs2_net_wait_ctxt *w = NULL;
371
372 spin_lock(&osb->net_response_lock);
373
374 list_for_each(p, &osb->net_response_list) {
375 w = list_entry(p, struct ocfs2_net_wait_ctxt, n_list);
376
377 __ocfs2_mark_node_responded(osb, w, node_num);
378 }
379
380 spin_unlock(&osb->net_response_lock);
381}
382
383static int ocfs2_broadcast_vote(struct ocfs2_super *osb,
384 struct ocfs2_vote_msg *request,
385 unsigned int response_id,
386 int *response,
387 struct ocfs2_net_response_cb *callback)
388{
389 int status, i, remote_err;
390 struct ocfs2_net_wait_ctxt *w = NULL;
391 int dequeued = 0;
392
393 mlog_entry_void();
394
395 w = ocfs2_new_net_wait_ctxt(response_id);
396 if (!w) {
397 status = -ENOMEM;
398 mlog_errno(status);
399 goto bail;
400 }
401 w->n_callback = callback;
402
403 /* we're pretty much ready to go at this point, and this fills
404 * in n_response which we need anyway... */
405 ocfs2_queue_net_wait_ctxt(osb, w);
406
407 i = ocfs2_node_map_iterate(osb, &osb->mounted_map, 0);
408
409 while (i != O2NM_INVALID_NODE_NUM) {
410 if (i != osb->node_num) {
411 mlog(0, "trying to send request to node %i\n", i);
412 ocfs2_node_map_set_bit(osb, &w->n_node_map, i);
413
414 remote_err = 0;
415 status = o2net_send_message(OCFS2_MESSAGE_TYPE_VOTE,
416 osb->net_key,
417 request,
418 sizeof(*request),
419 i,
420 &remote_err);
421 if (status == -ETIMEDOUT) {
422 mlog(0, "remote node %d timed out!\n", i);
423 status = -EAGAIN;
424 goto bail;
425 }
426 if (remote_err < 0) {
427 status = remote_err;
428 mlog(0, "remote error %d on node %d!\n",
429 remote_err, i);
430 mlog_errno(status);
431 goto bail;
432 }
433 if (status < 0) {
434 mlog_errno(status);
435 goto bail;
436 }
437 }
438 i++;
439 i = ocfs2_node_map_iterate(osb, &osb->mounted_map, i);
440 mlog(0, "next is %d, i am %d\n", i, osb->node_num);
441 }
442 mlog(0, "done sending, now waiting on responses...\n");
443
444 wait_event(w->n_event, ocfs2_node_map_is_empty(osb, &w->n_node_map));
445
446 ocfs2_dequeue_net_wait_ctxt(osb, w);
447 dequeued = 1;
448
449 *response = w->n_response;
450 status = 0;
451bail:
452 if (w) {
453 if (!dequeued)
454 ocfs2_dequeue_net_wait_ctxt(osb, w);
455 kfree(w);
456 }
457
458 mlog_exit(status);
459 return status;
460}
461
462static struct ocfs2_vote_msg * ocfs2_new_vote_request(struct ocfs2_super *osb,
463 u64 blkno,
464 unsigned int generation,
465 enum ocfs2_vote_request type)
466{
467 struct ocfs2_vote_msg *request;
468 struct ocfs2_msg_hdr *hdr;
469
470 BUG_ON(!ocfs2_is_valid_vote_request(type));
471
472 request = kzalloc(sizeof(*request), GFP_NOFS);
473 if (!request) {
474 mlog_errno(-ENOMEM);
475 } else {
476 hdr = &request->v_hdr;
477 hdr->h_node_num = cpu_to_be32(osb->node_num);
478 hdr->h_request = cpu_to_be32(type);
479 hdr->h_blkno = cpu_to_be64(blkno);
480 hdr->h_generation = cpu_to_be32(generation);
481 }
482
483 return request;
484}
485
486/* Complete the buildup of a new vote request and process the
487 * broadcast return value. */
488static int ocfs2_do_request_vote(struct ocfs2_super *osb,
489 struct ocfs2_vote_msg *request,
490 struct ocfs2_net_response_cb *callback)
491{
492 int status, response = -EBUSY;
493 unsigned int response_id;
494 struct ocfs2_msg_hdr *hdr;
495
496 response_id = ocfs2_new_response_id(osb);
497
498 hdr = &request->v_hdr;
499 hdr->h_response_id = cpu_to_be32(response_id);
500
501 status = ocfs2_broadcast_vote(osb, request, response_id, &response,
502 callback);
503 if (status < 0) {
504 mlog_errno(status);
505 goto bail;
506 }
507
508 status = response;
509bail:
510
511 return status;
512}
513
514int ocfs2_request_mount_vote(struct ocfs2_super *osb)
515{
516 int status;
517 struct ocfs2_vote_msg *request = NULL;
518
519 request = ocfs2_new_vote_request(osb, 0ULL, 0, OCFS2_VOTE_REQ_MOUNT);
520 if (!request) {
521 status = -ENOMEM;
522 goto bail;
523 }
524
525 status = -EAGAIN;
526 while (status == -EAGAIN) {
527 if (!(osb->s_mount_opt & OCFS2_MOUNT_NOINTR) &&
528 signal_pending(current)) {
529 status = -ERESTARTSYS;
530 goto bail;
531 }
532
533 if (ocfs2_node_map_is_only(osb, &osb->mounted_map,
534 osb->node_num)) {
535 status = 0;
536 goto bail;
537 }
538
539 status = ocfs2_do_request_vote(osb, request, NULL);
540 }
541
542bail:
543 kfree(request);
544 return status;
545}
546
547int ocfs2_request_umount_vote(struct ocfs2_super *osb)
548{
549 int status;
550 struct ocfs2_vote_msg *request = NULL;
551
552 request = ocfs2_new_vote_request(osb, 0ULL, 0, OCFS2_VOTE_REQ_UMOUNT);
553 if (!request) {
554 status = -ENOMEM;
555 goto bail;
556 }
557
558 status = -EAGAIN;
559 while (status == -EAGAIN) {
560 /* Do not check signals on this vote... We really want
561 * this one to go all the way through. */
562
563 if (ocfs2_node_map_is_only(osb, &osb->mounted_map,
564 osb->node_num)) {
565 status = 0;
566 goto bail;
567 }
568
569 status = ocfs2_do_request_vote(osb, request, NULL);
570 }
571
572bail:
573 kfree(request);
574 return status;
575}
576
577/* TODO: This should eventually be a hash table! */
578static struct ocfs2_net_wait_ctxt * __ocfs2_find_net_wait_ctxt(struct ocfs2_super *osb,
579 u32 response_id)
580{
581 struct list_head *p;
582 struct ocfs2_net_wait_ctxt *w = NULL;
583
584 list_for_each(p, &osb->net_response_list) {
585 w = list_entry(p, struct ocfs2_net_wait_ctxt, n_list);
586 if (response_id == w->n_response_id)
587 break;
588 w = NULL;
589 }
590
591 return w;
592}
593
594/* Translate response codes into local node errno values */
595static inline int ocfs2_translate_response(int response)
596{
597 int ret;
598
599 switch (response) {
600 case OCFS2_RESPONSE_OK:
601 ret = 0;
602 break;
603
604 case OCFS2_RESPONSE_BUSY:
605 ret = -EBUSY;
606 break;
607
608 default:
609 ret = -EINVAL;
610 }
611
612 return ret;
613}
614
615static int ocfs2_handle_response_message(struct o2net_msg *msg,
616 u32 len,
617 void *data, void **ret_data)
618{
619 unsigned int response_id, node_num;
620 int response_status;
621 struct ocfs2_super *osb = data;
622 struct ocfs2_response_msg *resp;
623 struct ocfs2_net_wait_ctxt * w;
624 struct ocfs2_net_response_cb *resp_cb;
625
626 resp = (struct ocfs2_response_msg *) msg->buf;
627
628 response_id = be32_to_cpu(resp->r_hdr.h_response_id);
629 node_num = be32_to_cpu(resp->r_hdr.h_node_num);
630 response_status =
631 ocfs2_translate_response(be32_to_cpu(resp->r_response));
632
633 mlog(0, "received response message:\n");
634 mlog(0, "h_response_id = %u\n", response_id);
635 mlog(0, "h_request = %u\n", be32_to_cpu(resp->r_hdr.h_request));
636 mlog(0, "h_blkno = %llu\n",
637 (unsigned long long)be64_to_cpu(resp->r_hdr.h_blkno));
638 mlog(0, "h_generation = %u\n", be32_to_cpu(resp->r_hdr.h_generation));
639 mlog(0, "h_node_num = %u\n", node_num);
640 mlog(0, "r_response = %d\n", response_status);
641
642 spin_lock(&osb->net_response_lock);
643 w = __ocfs2_find_net_wait_ctxt(osb, response_id);
644 if (!w) {
645 mlog(0, "request not found!\n");
646 goto bail;
647 }
648 resp_cb = w->n_callback;
649
650 if (response_status && (!w->n_response)) {
651 /* we only really need one negative response so don't
652 * set it twice. */
653 w->n_response = response_status;
654 }
655
656 if (resp_cb) {
657 spin_unlock(&osb->net_response_lock);
658
659 resp_cb->rc_cb(resp_cb->rc_priv, resp);
660
661 spin_lock(&osb->net_response_lock);
662 }
663
664 __ocfs2_mark_node_responded(osb, w, node_num);
665bail:
666 spin_unlock(&osb->net_response_lock);
667
668 return 0;
669}
670
671static int ocfs2_handle_vote_message(struct o2net_msg *msg,
672 u32 len,
673 void *data, void **ret_data)
674{
675 int status;
676 struct ocfs2_super *osb = data;
677 struct ocfs2_vote_work *work;
678
679 work = kmalloc(sizeof(struct ocfs2_vote_work), GFP_NOFS);
680 if (!work) {
681 status = -ENOMEM;
682 mlog_errno(status);
683 goto bail;
684 }
685
686 INIT_LIST_HEAD(&work->w_list);
687 memcpy(&work->w_msg, msg->buf, sizeof(struct ocfs2_vote_msg));
688
689 mlog(0, "scheduling vote request:\n");
690 mlog(0, "h_response_id = %u\n",
691 be32_to_cpu(work->w_msg.v_hdr.h_response_id));
692 mlog(0, "h_request = %u\n", be32_to_cpu(work->w_msg.v_hdr.h_request));
693 mlog(0, "h_blkno = %llu\n",
694 (unsigned long long)be64_to_cpu(work->w_msg.v_hdr.h_blkno));
695 mlog(0, "h_generation = %u\n",
696 be32_to_cpu(work->w_msg.v_hdr.h_generation));
697 mlog(0, "h_node_num = %u\n",
698 be32_to_cpu(work->w_msg.v_hdr.h_node_num));
699
700 spin_lock(&osb->vote_task_lock);
701 list_add_tail(&work->w_list, &osb->vote_list);
702 osb->vote_count++;
703 spin_unlock(&osb->vote_task_lock);
704
705 ocfs2_kick_vote_thread(osb);
706
707 status = 0;
708bail:
709 return status;
710}
711
712void ocfs2_unregister_net_handlers(struct ocfs2_super *osb)
713{
714 if (!osb->net_key)
715 return;
716
717 o2net_unregister_handler_list(&osb->osb_net_handlers);
718
719 if (!list_empty(&osb->net_response_list))
720 mlog(ML_ERROR, "net response list not empty!\n");
721
722 osb->net_key = 0;
723}
724
725int ocfs2_register_net_handlers(struct ocfs2_super *osb)
726{
727 int status = 0;
728
729 if (ocfs2_mount_local(osb))
730 return 0;
731
732 status = o2net_register_handler(OCFS2_MESSAGE_TYPE_RESPONSE,
733 osb->net_key,
734 sizeof(struct ocfs2_response_msg),
735 ocfs2_handle_response_message,
736 osb, NULL, &osb->osb_net_handlers);
737 if (status) {
738 mlog_errno(status);
739 goto bail;
740 }
741
742 status = o2net_register_handler(OCFS2_MESSAGE_TYPE_VOTE,
743 osb->net_key,
744 sizeof(struct ocfs2_vote_msg),
745 ocfs2_handle_vote_message,
746 osb, NULL, &osb->osb_net_handlers);
747 if (status) {
748 mlog_errno(status);
749 goto bail;
750 }
751bail:
752 if (status < 0)
753 ocfs2_unregister_net_handlers(osb);
754
755 return status;
756}
diff --git a/fs/proc/base.c b/fs/proc/base.c
index 7411bfb0b7cc..91fa8e6ce8ad 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -310,6 +310,77 @@ static int proc_pid_schedstat(struct task_struct *task, char *buffer)
310} 310}
311#endif 311#endif
312 312
313#ifdef CONFIG_LATENCYTOP
314static int lstats_show_proc(struct seq_file *m, void *v)
315{
316 int i;
317 struct task_struct *task = m->private;
318 seq_puts(m, "Latency Top version : v0.1\n");
319
320 for (i = 0; i < 32; i++) {
321 if (task->latency_record[i].backtrace[0]) {
322 int q;
323 seq_printf(m, "%i %li %li ",
324 task->latency_record[i].count,
325 task->latency_record[i].time,
326 task->latency_record[i].max);
327 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
328 char sym[KSYM_NAME_LEN];
329 char *c;
330 if (!task->latency_record[i].backtrace[q])
331 break;
332 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
333 break;
334 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
335 c = strchr(sym, '+');
336 if (c)
337 *c = 0;
338 seq_printf(m, "%s ", sym);
339 }
340 seq_printf(m, "\n");
341 }
342
343 }
344 return 0;
345}
346
347static int lstats_open(struct inode *inode, struct file *file)
348{
349 int ret;
350 struct seq_file *m;
351 struct task_struct *task = get_proc_task(inode);
352
353 ret = single_open(file, lstats_show_proc, NULL);
354 if (!ret) {
355 m = file->private_data;
356 m->private = task;
357 }
358 return ret;
359}
360
361static ssize_t lstats_write(struct file *file, const char __user *buf,
362 size_t count, loff_t *offs)
363{
364 struct seq_file *m;
365 struct task_struct *task;
366
367 m = file->private_data;
368 task = m->private;
369 clear_all_latency_tracing(task);
370
371 return count;
372}
373
374static const struct file_operations proc_lstats_operations = {
375 .open = lstats_open,
376 .read = seq_read,
377 .write = lstats_write,
378 .llseek = seq_lseek,
379 .release = single_release,
380};
381
382#endif
383
313/* The badness from the OOM killer */ 384/* The badness from the OOM killer */
314unsigned long badness(struct task_struct *p, unsigned long uptime); 385unsigned long badness(struct task_struct *p, unsigned long uptime);
315static int proc_oom_score(struct task_struct *task, char *buffer) 386static int proc_oom_score(struct task_struct *task, char *buffer)
@@ -1020,6 +1091,7 @@ static const struct file_operations proc_fault_inject_operations = {
1020}; 1091};
1021#endif 1092#endif
1022 1093
1094
1023#ifdef CONFIG_SCHED_DEBUG 1095#ifdef CONFIG_SCHED_DEBUG
1024/* 1096/*
1025 * Print out various scheduling related per-task fields: 1097 * Print out various scheduling related per-task fields:
@@ -2230,6 +2302,9 @@ static const struct pid_entry tgid_base_stuff[] = {
2230#ifdef CONFIG_SCHEDSTATS 2302#ifdef CONFIG_SCHEDSTATS
2231 INF("schedstat", S_IRUGO, pid_schedstat), 2303 INF("schedstat", S_IRUGO, pid_schedstat),
2232#endif 2304#endif
2305#ifdef CONFIG_LATENCYTOP
2306 REG("latency", S_IRUGO, lstats),
2307#endif
2233#ifdef CONFIG_PROC_PID_CPUSET 2308#ifdef CONFIG_PROC_PID_CPUSET
2234 REG("cpuset", S_IRUGO, cpuset), 2309 REG("cpuset", S_IRUGO, cpuset),
2235#endif 2310#endif
@@ -2555,6 +2630,9 @@ static const struct pid_entry tid_base_stuff[] = {
2555#ifdef CONFIG_SCHEDSTATS 2630#ifdef CONFIG_SCHEDSTATS
2556 INF("schedstat", S_IRUGO, pid_schedstat), 2631 INF("schedstat", S_IRUGO, pid_schedstat),
2557#endif 2632#endif
2633#ifdef CONFIG_LATENCYTOP
2634 REG("latency", S_IRUGO, lstats),
2635#endif
2558#ifdef CONFIG_PROC_PID_CPUSET 2636#ifdef CONFIG_PROC_PID_CPUSET
2559 REG("cpuset", S_IRUGO, cpuset), 2637 REG("cpuset", S_IRUGO, cpuset),
2560#endif 2638#endif
diff --git a/include/asm-cris/arch-v10/ide.h b/include/asm-cris/arch-v10/ide.h
index 78b301ed7b12..ea34e0d0a388 100644
--- a/include/asm-cris/arch-v10/ide.h
+++ b/include/asm-cris/arch-v10/ide.h
@@ -89,11 +89,6 @@ static inline void ide_init_default_hwifs(void)
89 } 89 }
90} 90}
91 91
92/* some configuration options we don't need */
93
94#undef SUPPORT_VLB_SYNC
95#define SUPPORT_VLB_SYNC 0
96
97#endif /* __KERNEL__ */ 92#endif /* __KERNEL__ */
98 93
99#endif /* __ASMCRIS_IDE_H */ 94#endif /* __ASMCRIS_IDE_H */
diff --git a/include/asm-cris/arch-v32/ide.h b/include/asm-cris/arch-v32/ide.h
index 11296170d057..fb9c3627a5b4 100644
--- a/include/asm-cris/arch-v32/ide.h
+++ b/include/asm-cris/arch-v32/ide.h
@@ -48,11 +48,6 @@ static inline unsigned long ide_default_io_base(int index)
48 return REG_TYPE_CONV(unsigned long, reg_ata_rw_ctrl2, ctrl2); 48 return REG_TYPE_CONV(unsigned long, reg_ata_rw_ctrl2, ctrl2);
49} 49}
50 50
51/* some configuration options we don't need */
52
53#undef SUPPORT_VLB_SYNC
54#define SUPPORT_VLB_SYNC 0
55
56#define IDE_ARCH_ACK_INTR 51#define IDE_ARCH_ACK_INTR
57#define ide_ack_intr(hwif) ((hwif)->ack_intr(hwif)) 52#define ide_ack_intr(hwif) ((hwif)->ack_intr(hwif))
58 53
diff --git a/include/asm-frv/ide.h b/include/asm-frv/ide.h
index f0bd2cb250c1..8c9a540d4344 100644
--- a/include/asm-frv/ide.h
+++ b/include/asm-frv/ide.h
@@ -18,12 +18,6 @@
18#include <asm/io.h> 18#include <asm/io.h>
19#include <asm/irq.h> 19#include <asm/irq.h>
20 20
21#undef SUPPORT_SLOW_DATA_PORTS
22#define SUPPORT_SLOW_DATA_PORTS 0
23
24#undef SUPPORT_VLB_SYNC
25#define SUPPORT_VLB_SYNC 0
26
27#ifndef MAX_HWIFS 21#ifndef MAX_HWIFS
28#define MAX_HWIFS 8 22#define MAX_HWIFS 8
29#endif 23#endif
diff --git a/include/asm-generic/resource.h b/include/asm-generic/resource.h
index a4a22cc35898..587566f95f6c 100644
--- a/include/asm-generic/resource.h
+++ b/include/asm-generic/resource.h
@@ -44,8 +44,8 @@
44#define RLIMIT_NICE 13 /* max nice prio allowed to raise to 44#define RLIMIT_NICE 13 /* max nice prio allowed to raise to
45 0-39 for nice level 19 .. -20 */ 45 0-39 for nice level 19 .. -20 */
46#define RLIMIT_RTPRIO 14 /* maximum realtime priority */ 46#define RLIMIT_RTPRIO 14 /* maximum realtime priority */
47 47#define RLIMIT_RTTIME 15 /* timeout for RT tasks in us */
48#define RLIM_NLIMITS 15 48#define RLIM_NLIMITS 16
49 49
50/* 50/*
51 * SuS says limits have to be unsigned. 51 * SuS says limits have to be unsigned.
@@ -86,6 +86,7 @@
86 [RLIMIT_MSGQUEUE] = { MQ_BYTES_MAX, MQ_BYTES_MAX }, \ 86 [RLIMIT_MSGQUEUE] = { MQ_BYTES_MAX, MQ_BYTES_MAX }, \
87 [RLIMIT_NICE] = { 0, 0 }, \ 87 [RLIMIT_NICE] = { 0, 0 }, \
88 [RLIMIT_RTPRIO] = { 0, 0 }, \ 88 [RLIMIT_RTPRIO] = { 0, 0 }, \
89 [RLIMIT_RTTIME] = { RLIM_INFINITY, RLIM_INFINITY }, \
89} 90}
90 91
91#endif /* __KERNEL__ */ 92#endif /* __KERNEL__ */
diff --git a/include/asm-powerpc/ide.h b/include/asm-powerpc/ide.h
index fd7f5a430f0a..6d50310ecaea 100644
--- a/include/asm-powerpc/ide.h
+++ b/include/asm-powerpc/ide.h
@@ -42,9 +42,6 @@ struct ide_machdep_calls {
42 42
43extern struct ide_machdep_calls ppc_ide_md; 43extern struct ide_machdep_calls ppc_ide_md;
44 44
45#undef SUPPORT_SLOW_DATA_PORTS
46#define SUPPORT_SLOW_DATA_PORTS 0
47
48#define IDE_ARCH_OBSOLETE_DEFAULTS 45#define IDE_ARCH_OBSOLETE_DEFAULTS
49 46
50static __inline__ int ide_default_irq(unsigned long base) 47static __inline__ int ide_default_irq(unsigned long base)
diff --git a/include/asm-x86/thread_info_32.h b/include/asm-x86/thread_info_32.h
index 22a8cbcd35e2..ef58fd2a6eb0 100644
--- a/include/asm-x86/thread_info_32.h
+++ b/include/asm-x86/thread_info_32.h
@@ -132,6 +132,7 @@ static inline struct thread_info *current_thread_info(void)
132#define TIF_SYSCALL_AUDIT 6 /* syscall auditing active */ 132#define TIF_SYSCALL_AUDIT 6 /* syscall auditing active */
133#define TIF_SECCOMP 7 /* secure computing */ 133#define TIF_SECCOMP 7 /* secure computing */
134#define TIF_RESTORE_SIGMASK 8 /* restore signal mask in do_signal() */ 134#define TIF_RESTORE_SIGMASK 8 /* restore signal mask in do_signal() */
135#define TIF_HRTICK_RESCHED 9 /* reprogram hrtick timer */
135#define TIF_MEMDIE 16 136#define TIF_MEMDIE 16
136#define TIF_DEBUG 17 /* uses debug registers */ 137#define TIF_DEBUG 17 /* uses debug registers */
137#define TIF_IO_BITMAP 18 /* uses I/O bitmap */ 138#define TIF_IO_BITMAP 18 /* uses I/O bitmap */
@@ -147,6 +148,7 @@ static inline struct thread_info *current_thread_info(void)
147#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT) 148#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT)
148#define _TIF_SECCOMP (1<<TIF_SECCOMP) 149#define _TIF_SECCOMP (1<<TIF_SECCOMP)
149#define _TIF_RESTORE_SIGMASK (1<<TIF_RESTORE_SIGMASK) 150#define _TIF_RESTORE_SIGMASK (1<<TIF_RESTORE_SIGMASK)
151#define _TIF_HRTICK_RESCHED (1<<TIF_HRTICK_RESCHED)
150#define _TIF_DEBUG (1<<TIF_DEBUG) 152#define _TIF_DEBUG (1<<TIF_DEBUG)
151#define _TIF_IO_BITMAP (1<<TIF_IO_BITMAP) 153#define _TIF_IO_BITMAP (1<<TIF_IO_BITMAP)
152#define _TIF_FREEZE (1<<TIF_FREEZE) 154#define _TIF_FREEZE (1<<TIF_FREEZE)
diff --git a/include/asm-x86/thread_info_64.h b/include/asm-x86/thread_info_64.h
index beae2bfb62ca..7f6ee68f0002 100644
--- a/include/asm-x86/thread_info_64.h
+++ b/include/asm-x86/thread_info_64.h
@@ -115,6 +115,7 @@ static inline struct thread_info *stack_thread_info(void)
115#define TIF_SECCOMP 8 /* secure computing */ 115#define TIF_SECCOMP 8 /* secure computing */
116#define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal */ 116#define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal */
117#define TIF_MCE_NOTIFY 10 /* notify userspace of an MCE */ 117#define TIF_MCE_NOTIFY 10 /* notify userspace of an MCE */
118#define TIF_HRTICK_RESCHED 11 /* reprogram hrtick timer */
118/* 16 free */ 119/* 16 free */
119#define TIF_IA32 17 /* 32bit process */ 120#define TIF_IA32 17 /* 32bit process */
120#define TIF_FORK 18 /* ret_from_fork */ 121#define TIF_FORK 18 /* ret_from_fork */
@@ -133,6 +134,7 @@ static inline struct thread_info *stack_thread_info(void)
133#define _TIF_SECCOMP (1<<TIF_SECCOMP) 134#define _TIF_SECCOMP (1<<TIF_SECCOMP)
134#define _TIF_RESTORE_SIGMASK (1<<TIF_RESTORE_SIGMASK) 135#define _TIF_RESTORE_SIGMASK (1<<TIF_RESTORE_SIGMASK)
135#define _TIF_MCE_NOTIFY (1<<TIF_MCE_NOTIFY) 136#define _TIF_MCE_NOTIFY (1<<TIF_MCE_NOTIFY)
137#define _TIF_HRTICK_RESCHED (1<<TIF_HRTICK_RESCHED)
136#define _TIF_IA32 (1<<TIF_IA32) 138#define _TIF_IA32 (1<<TIF_IA32)
137#define _TIF_FORK (1<<TIF_FORK) 139#define _TIF_FORK (1<<TIF_FORK)
138#define _TIF_ABI_PENDING (1<<TIF_ABI_PENDING) 140#define _TIF_ABI_PENDING (1<<TIF_ABI_PENDING)
@@ -146,6 +148,9 @@ static inline struct thread_info *stack_thread_info(void)
146/* work to do on any return to user space */ 148/* work to do on any return to user space */
147#define _TIF_ALLWORK_MASK (0x0000FFFF & ~_TIF_SECCOMP) 149#define _TIF_ALLWORK_MASK (0x0000FFFF & ~_TIF_SECCOMP)
148 150
151#define _TIF_DO_NOTIFY_MASK \
152 (_TIF_SIGPENDING|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY|_TIF_HRTICK_RESCHED)
153
149/* flags to check in __switch_to() */ 154/* flags to check in __switch_to() */
150#define _TIF_WORK_CTXSW (_TIF_DEBUG|_TIF_IO_BITMAP) 155#define _TIF_WORK_CTXSW (_TIF_DEBUG|_TIF_IO_BITMAP)
151 156
diff --git a/include/linux/Kbuild b/include/linux/Kbuild
index f30fa92a44a1..bd694f779346 100644
--- a/include/linux/Kbuild
+++ b/include/linux/Kbuild
@@ -49,6 +49,7 @@ header-y += comstats.h
49header-y += const.h 49header-y += const.h
50header-y += cgroupstats.h 50header-y += cgroupstats.h
51header-y += cycx_cfm.h 51header-y += cycx_cfm.h
52header-y += dlmconstants.h
52header-y += dlm_device.h 53header-y += dlm_device.h
53header-y += dlm_netlink.h 54header-y += dlm_netlink.h
54header-y += dm-ioctl.h 55header-y += dm-ioctl.h
diff --git a/include/linux/ata.h b/include/linux/ata.h
index e672e80202a8..78bbacaed8c4 100644
--- a/include/linux/ata.h
+++ b/include/linux/ata.h
@@ -286,9 +286,10 @@ enum {
286 ATA_CBL_NONE = 0, 286 ATA_CBL_NONE = 0,
287 ATA_CBL_PATA40 = 1, 287 ATA_CBL_PATA40 = 1,
288 ATA_CBL_PATA80 = 2, 288 ATA_CBL_PATA80 = 2,
289 ATA_CBL_PATA40_SHORT = 3, /* 40 wire cable to high UDMA spec */ 289 ATA_CBL_PATA40_SHORT = 3, /* 40 wire cable to high UDMA spec */
290 ATA_CBL_PATA_UNK = 4, 290 ATA_CBL_PATA_UNK = 4, /* don't know, maybe 80c? */
291 ATA_CBL_SATA = 5, 291 ATA_CBL_PATA_IGN = 5, /* don't know, ignore cable handling */
292 ATA_CBL_SATA = 6,
292 293
293 /* SATA Status and Control Registers */ 294 /* SATA Status and Control Registers */
294 SCR_STATUS = 0, 295 SCR_STATUS = 0,
@@ -324,6 +325,13 @@ enum {
324 ATA_TFLAG_LBA = (1 << 4), /* enable LBA */ 325 ATA_TFLAG_LBA = (1 << 4), /* enable LBA */
325 ATA_TFLAG_FUA = (1 << 5), /* enable FUA */ 326 ATA_TFLAG_FUA = (1 << 5), /* enable FUA */
326 ATA_TFLAG_POLLING = (1 << 6), /* set nIEN to 1 and use polling */ 327 ATA_TFLAG_POLLING = (1 << 6), /* set nIEN to 1 and use polling */
328
329 /* protocol flags */
330 ATA_PROT_FLAG_PIO = (1 << 0), /* is PIO */
331 ATA_PROT_FLAG_DMA = (1 << 1), /* is DMA */
332 ATA_PROT_FLAG_DATA = ATA_PROT_FLAG_PIO | ATA_PROT_FLAG_DMA,
333 ATA_PROT_FLAG_NCQ = (1 << 2), /* is NCQ */
334 ATA_PROT_FLAG_ATAPI = (1 << 3), /* is ATAPI */
327}; 335};
328 336
329enum ata_tf_protocols { 337enum ata_tf_protocols {
@@ -333,9 +341,9 @@ enum ata_tf_protocols {
333 ATA_PROT_PIO, /* PIO data xfer */ 341 ATA_PROT_PIO, /* PIO data xfer */
334 ATA_PROT_DMA, /* DMA */ 342 ATA_PROT_DMA, /* DMA */
335 ATA_PROT_NCQ, /* NCQ */ 343 ATA_PROT_NCQ, /* NCQ */
336 ATA_PROT_ATAPI, /* packet command, PIO data xfer*/ 344 ATAPI_PROT_NODATA, /* packet command, no data */
337 ATA_PROT_ATAPI_NODATA, /* packet command, no data */ 345 ATAPI_PROT_PIO, /* packet command, PIO data xfer*/
338 ATA_PROT_ATAPI_DMA, /* packet command with special DMA sauce */ 346 ATAPI_PROT_DMA, /* packet command with special DMA sauce */
339}; 347};
340 348
341enum ata_ioctls { 349enum ata_ioctls {
@@ -346,8 +354,8 @@ enum ata_ioctls {
346/* core structures */ 354/* core structures */
347 355
348struct ata_prd { 356struct ata_prd {
349 u32 addr; 357 __le32 addr;
350 u32 flags_len; 358 __le32 flags_len;
351}; 359};
352 360
353struct ata_taskfile { 361struct ata_taskfile {
@@ -373,13 +381,69 @@ struct ata_taskfile {
373 u8 command; /* IO operation */ 381 u8 command; /* IO operation */
374}; 382};
375 383
384/*
385 * protocol tests
386 */
387static inline unsigned int ata_prot_flags(u8 prot)
388{
389 switch (prot) {
390 case ATA_PROT_NODATA:
391 return 0;
392 case ATA_PROT_PIO:
393 return ATA_PROT_FLAG_PIO;
394 case ATA_PROT_DMA:
395 return ATA_PROT_FLAG_DMA;
396 case ATA_PROT_NCQ:
397 return ATA_PROT_FLAG_DMA | ATA_PROT_FLAG_NCQ;
398 case ATAPI_PROT_NODATA:
399 return ATA_PROT_FLAG_ATAPI;
400 case ATAPI_PROT_PIO:
401 return ATA_PROT_FLAG_ATAPI | ATA_PROT_FLAG_PIO;
402 case ATAPI_PROT_DMA:
403 return ATA_PROT_FLAG_ATAPI | ATA_PROT_FLAG_DMA;
404 }
405 return 0;
406}
407
408static inline int ata_is_atapi(u8 prot)
409{
410 return ata_prot_flags(prot) & ATA_PROT_FLAG_ATAPI;
411}
412
413static inline int ata_is_nodata(u8 prot)
414{
415 return !(ata_prot_flags(prot) & ATA_PROT_FLAG_DATA);
416}
417
418static inline int ata_is_pio(u8 prot)
419{
420 return ata_prot_flags(prot) & ATA_PROT_FLAG_PIO;
421}
422
423static inline int ata_is_dma(u8 prot)
424{
425 return ata_prot_flags(prot) & ATA_PROT_FLAG_DMA;
426}
427
428static inline int ata_is_ncq(u8 prot)
429{
430 return ata_prot_flags(prot) & ATA_PROT_FLAG_NCQ;
431}
432
433static inline int ata_is_data(u8 prot)
434{
435 return ata_prot_flags(prot) & ATA_PROT_FLAG_DATA;
436}
437
438/*
439 * id tests
440 */
376#define ata_id_is_ata(id) (((id)[0] & (1 << 15)) == 0) 441#define ata_id_is_ata(id) (((id)[0] & (1 << 15)) == 0)
377#define ata_id_has_lba(id) ((id)[49] & (1 << 9)) 442#define ata_id_has_lba(id) ((id)[49] & (1 << 9))
378#define ata_id_has_dma(id) ((id)[49] & (1 << 8)) 443#define ata_id_has_dma(id) ((id)[49] & (1 << 8))
379#define ata_id_has_ncq(id) ((id)[76] & (1 << 8)) 444#define ata_id_has_ncq(id) ((id)[76] & (1 << 8))
380#define ata_id_queue_depth(id) (((id)[75] & 0x1f) + 1) 445#define ata_id_queue_depth(id) (((id)[75] & 0x1f) + 1)
381#define ata_id_removeable(id) ((id)[0] & (1 << 7)) 446#define ata_id_removeable(id) ((id)[0] & (1 << 7))
382#define ata_id_has_dword_io(id) ((id)[48] & (1 << 0))
383#define ata_id_has_atapi_AN(id) \ 447#define ata_id_has_atapi_AN(id) \
384 ( (((id)[76] != 0x0000) && ((id)[76] != 0xffff)) && \ 448 ( (((id)[76] != 0x0000) && ((id)[76] != 0xffff)) && \
385 ((id)[78] & (1 << 5)) ) 449 ((id)[78] & (1 << 5)) )
@@ -415,6 +479,7 @@ static inline bool ata_id_has_dipm(const u16 *id)
415 return val & (1 << 3); 479 return val & (1 << 3);
416} 480}
417 481
482
418static inline int ata_id_has_fua(const u16 *id) 483static inline int ata_id_has_fua(const u16 *id)
419{ 484{
420 if ((id[84] & 0xC000) != 0x4000) 485 if ((id[84] & 0xC000) != 0x4000)
@@ -519,6 +584,26 @@ static inline int ata_id_is_sata(const u16 *id)
519 return ata_id_major_version(id) >= 5 && id[93] == 0; 584 return ata_id_major_version(id) >= 5 && id[93] == 0;
520} 585}
521 586
587static inline int ata_id_has_tpm(const u16 *id)
588{
589 /* The TPM bits are only valid on ATA8 */
590 if (ata_id_major_version(id) < 8)
591 return 0;
592 if ((id[48] & 0xC000) != 0x4000)
593 return 0;
594 return id[48] & (1 << 0);
595}
596
597static inline int ata_id_has_dword_io(const u16 *id)
598{
599 /* ATA 8 reuses this flag for "trusted" computing */
600 if (ata_id_major_version(id) > 7)
601 return 0;
602 if (id[48] & (1 << 0))
603 return 1;
604 return 0;
605}
606
522static inline int ata_id_current_chs_valid(const u16 *id) 607static inline int ata_id_current_chs_valid(const u16 *id)
523{ 608{
524 /* For ATA-1 devices, if the INITIALIZE DEVICE PARAMETERS command 609 /* For ATA-1 devices, if the INITIALIZE DEVICE PARAMETERS command
@@ -574,13 +659,6 @@ static inline int atapi_command_packet_set(const u16 *dev_id)
574 return (dev_id[0] >> 8) & 0x1f; 659 return (dev_id[0] >> 8) & 0x1f;
575} 660}
576 661
577static inline int is_atapi_taskfile(const struct ata_taskfile *tf)
578{
579 return (tf->protocol == ATA_PROT_ATAPI) ||
580 (tf->protocol == ATA_PROT_ATAPI_NODATA) ||
581 (tf->protocol == ATA_PROT_ATAPI_DMA);
582}
583
584static inline int is_multi_taskfile(struct ata_taskfile *tf) 662static inline int is_multi_taskfile(struct ata_taskfile *tf)
585{ 663{
586 return (tf->command == ATA_CMD_READ_MULTI) || 664 return (tf->command == ATA_CMD_READ_MULTI) ||
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index d18ee67b40f8..40ee1706caa3 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -144,7 +144,6 @@ enum rq_cmd_type_bits {
144 * private REQ_LB opcodes to differentiate what type of request this is 144 * private REQ_LB opcodes to differentiate what type of request this is
145 */ 145 */
146 REQ_TYPE_ATA_CMD, 146 REQ_TYPE_ATA_CMD,
147 REQ_TYPE_ATA_TASK,
148 REQ_TYPE_ATA_TASKFILE, 147 REQ_TYPE_ATA_TASKFILE,
149 REQ_TYPE_ATA_PC, 148 REQ_TYPE_ATA_PC,
150}; 149};
diff --git a/include/linux/cdrom.h b/include/linux/cdrom.h
index c6d3e22c0624..fcdc11b9609b 100644
--- a/include/linux/cdrom.h
+++ b/include/linux/cdrom.h
@@ -451,6 +451,7 @@ struct cdrom_generic_command
451#define GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e 451#define GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
452#define GPCMD_READ_10 0x28 452#define GPCMD_READ_10 0x28
453#define GPCMD_READ_12 0xa8 453#define GPCMD_READ_12 0xa8
454#define GPCMD_READ_BUFFER 0x3c
454#define GPCMD_READ_BUFFER_CAPACITY 0x5c 455#define GPCMD_READ_BUFFER_CAPACITY 0x5c
455#define GPCMD_READ_CDVD_CAPACITY 0x25 456#define GPCMD_READ_CDVD_CAPACITY 0x25
456#define GPCMD_READ_CD 0xbe 457#define GPCMD_READ_CD 0xbe
@@ -480,7 +481,9 @@ struct cdrom_generic_command
480#define GPCMD_TEST_UNIT_READY 0x00 481#define GPCMD_TEST_UNIT_READY 0x00
481#define GPCMD_VERIFY_10 0x2f 482#define GPCMD_VERIFY_10 0x2f
482#define GPCMD_WRITE_10 0x2a 483#define GPCMD_WRITE_10 0x2a
484#define GPCMD_WRITE_12 0xaa
483#define GPCMD_WRITE_AND_VERIFY_10 0x2e 485#define GPCMD_WRITE_AND_VERIFY_10 0x2e
486#define GPCMD_WRITE_BUFFER 0x3b
484/* This is listed as optional in ATAPI 2.6, but is (curiously) 487/* This is listed as optional in ATAPI 2.6, but is (curiously)
485 * missing from Mt. Fuji, Table 57. It _is_ mentioned in Mt. Fuji 488 * missing from Mt. Fuji, Table 57. It _is_ mentioned in Mt. Fuji
486 * Table 377 as an MMC command for SCSi devices though... Most ATAPI 489 * Table 377 as an MMC command for SCSi devices though... Most ATAPI
diff --git a/include/linux/cpu.h b/include/linux/cpu.h
index 92f2029a34f3..0be8d65bc3c8 100644
--- a/include/linux/cpu.h
+++ b/include/linux/cpu.h
@@ -71,18 +71,27 @@ static inline void unregister_cpu_notifier(struct notifier_block *nb)
71 71
72int cpu_up(unsigned int cpu); 72int cpu_up(unsigned int cpu);
73 73
74extern void cpu_hotplug_init(void);
75
74#else 76#else
75 77
76static inline int register_cpu_notifier(struct notifier_block *nb) 78static inline int register_cpu_notifier(struct notifier_block *nb)
77{ 79{
78 return 0; 80 return 0;
79} 81}
82
80static inline void unregister_cpu_notifier(struct notifier_block *nb) 83static inline void unregister_cpu_notifier(struct notifier_block *nb)
81{ 84{
82} 85}
83 86
87static inline void cpu_hotplug_init(void)
88{
89}
90
84#endif /* CONFIG_SMP */ 91#endif /* CONFIG_SMP */
85extern struct sysdev_class cpu_sysdev_class; 92extern struct sysdev_class cpu_sysdev_class;
93extern void cpu_maps_update_begin(void);
94extern void cpu_maps_update_done(void);
86 95
87#ifdef CONFIG_HOTPLUG_CPU 96#ifdef CONFIG_HOTPLUG_CPU
88/* Stop CPUs going up and down. */ 97/* Stop CPUs going up and down. */
@@ -97,8 +106,8 @@ static inline void cpuhotplug_mutex_unlock(struct mutex *cpu_hp_mutex)
97 mutex_unlock(cpu_hp_mutex); 106 mutex_unlock(cpu_hp_mutex);
98} 107}
99 108
100extern void lock_cpu_hotplug(void); 109extern void get_online_cpus(void);
101extern void unlock_cpu_hotplug(void); 110extern void put_online_cpus(void);
102#define hotcpu_notifier(fn, pri) { \ 111#define hotcpu_notifier(fn, pri) { \
103 static struct notifier_block fn##_nb = \ 112 static struct notifier_block fn##_nb = \
104 { .notifier_call = fn, .priority = pri }; \ 113 { .notifier_call = fn, .priority = pri }; \
@@ -115,8 +124,8 @@ static inline void cpuhotplug_mutex_lock(struct mutex *cpu_hp_mutex)
115static inline void cpuhotplug_mutex_unlock(struct mutex *cpu_hp_mutex) 124static inline void cpuhotplug_mutex_unlock(struct mutex *cpu_hp_mutex)
116{ } 125{ }
117 126
118#define lock_cpu_hotplug() do { } while (0) 127#define get_online_cpus() do { } while (0)
119#define unlock_cpu_hotplug() do { } while (0) 128#define put_online_cpus() do { } while (0)
120#define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0) 129#define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
121/* These aren't inline functions due to a GCC bug. */ 130/* These aren't inline functions due to a GCC bug. */
122#define register_hotcpu_notifier(nb) ({ (void)(nb); 0; }) 131#define register_hotcpu_notifier(nb) ({ (void)(nb); 0; })
diff --git a/include/linux/debug_locks.h b/include/linux/debug_locks.h
index 1678a5de7013..f4a5871767f5 100644
--- a/include/linux/debug_locks.h
+++ b/include/linux/debug_locks.h
@@ -47,6 +47,7 @@ struct task_struct;
47 47
48#ifdef CONFIG_LOCKDEP 48#ifdef CONFIG_LOCKDEP
49extern void debug_show_all_locks(void); 49extern void debug_show_all_locks(void);
50extern void __debug_show_held_locks(struct task_struct *task);
50extern void debug_show_held_locks(struct task_struct *task); 51extern void debug_show_held_locks(struct task_struct *task);
51extern void debug_check_no_locks_freed(const void *from, unsigned long len); 52extern void debug_check_no_locks_freed(const void *from, unsigned long len);
52extern void debug_check_no_locks_held(struct task_struct *task); 53extern void debug_check_no_locks_held(struct task_struct *task);
@@ -55,6 +56,10 @@ static inline void debug_show_all_locks(void)
55{ 56{
56} 57}
57 58
59static inline void __debug_show_held_locks(struct task_struct *task)
60{
61}
62
58static inline void debug_show_held_locks(struct task_struct *task) 63static inline void debug_show_held_locks(struct task_struct *task)
59{ 64{
60} 65}
diff --git a/include/linux/dlm.h b/include/linux/dlm.h
index be9d278761e0..c743fbc769db 100644
--- a/include/linux/dlm.h
+++ b/include/linux/dlm.h
@@ -19,148 +19,12 @@
19 * routines and structures to use DLM lockspaces 19 * routines and structures to use DLM lockspaces
20 */ 20 */
21 21
22/* 22/* Lock levels and flags are here */
23 * Lock Modes 23#include <linux/dlmconstants.h>
24 */
25 24
26#define DLM_LOCK_IV -1 /* invalid */
27#define DLM_LOCK_NL 0 /* null */
28#define DLM_LOCK_CR 1 /* concurrent read */
29#define DLM_LOCK_CW 2 /* concurrent write */
30#define DLM_LOCK_PR 3 /* protected read */
31#define DLM_LOCK_PW 4 /* protected write */
32#define DLM_LOCK_EX 5 /* exclusive */
33
34/*
35 * Maximum size in bytes of a dlm_lock name
36 */
37 25
38#define DLM_RESNAME_MAXLEN 64 26#define DLM_RESNAME_MAXLEN 64
39 27
40/*
41 * Flags to dlm_lock
42 *
43 * DLM_LKF_NOQUEUE
44 *
45 * Do not queue the lock request on the wait queue if it cannot be granted
46 * immediately. If the lock cannot be granted because of this flag, DLM will
47 * either return -EAGAIN from the dlm_lock call or will return 0 from
48 * dlm_lock and -EAGAIN in the lock status block when the AST is executed.
49 *
50 * DLM_LKF_CANCEL
51 *
52 * Used to cancel a pending lock request or conversion. A converting lock is
53 * returned to its previously granted mode.
54 *
55 * DLM_LKF_CONVERT
56 *
57 * Indicates a lock conversion request. For conversions the name and namelen
58 * are ignored and the lock ID in the LKSB is used to identify the lock.
59 *
60 * DLM_LKF_VALBLK
61 *
62 * Requests DLM to return the current contents of the lock value block in the
63 * lock status block. When this flag is set in a lock conversion from PW or EX
64 * modes, DLM assigns the value specified in the lock status block to the lock
65 * value block of the lock resource. The LVB is a DLM_LVB_LEN size array
66 * containing application-specific information.
67 *
68 * DLM_LKF_QUECVT
69 *
70 * Force a conversion request to be queued, even if it is compatible with
71 * the granted modes of other locks on the same resource.
72 *
73 * DLM_LKF_IVVALBLK
74 *
75 * Invalidate the lock value block.
76 *
77 * DLM_LKF_CONVDEADLK
78 *
79 * Allows the dlm to resolve conversion deadlocks internally by demoting the
80 * granted mode of a converting lock to NL. The DLM_SBF_DEMOTED flag is
81 * returned for a conversion that's been effected by this.
82 *
83 * DLM_LKF_PERSISTENT
84 *
85 * Only relevant to locks originating in userspace. A persistent lock will not
86 * be removed if the process holding the lock exits.
87 *
88 * DLM_LKF_NODLCKWT
89 *
90 * Do not cancel the lock if it gets into conversion deadlock.
91 * Exclude this lock from being monitored due to DLM_LSFL_TIMEWARN.
92 *
93 * DLM_LKF_NODLCKBLK
94 *
95 * net yet implemented
96 *
97 * DLM_LKF_EXPEDITE
98 *
99 * Used only with new requests for NL mode locks. Tells the lock manager
100 * to grant the lock, ignoring other locks in convert and wait queues.
101 *
102 * DLM_LKF_NOQUEUEBAST
103 *
104 * Send blocking AST's before returning -EAGAIN to the caller. It is only
105 * used along with the NOQUEUE flag. Blocking AST's are not sent for failed
106 * NOQUEUE requests otherwise.
107 *
108 * DLM_LKF_HEADQUE
109 *
110 * Add a lock to the head of the convert or wait queue rather than the tail.
111 *
112 * DLM_LKF_NOORDER
113 *
114 * Disregard the standard grant order rules and grant a lock as soon as it
115 * is compatible with other granted locks.
116 *
117 * DLM_LKF_ORPHAN
118 *
119 * not yet implemented
120 *
121 * DLM_LKF_ALTPR
122 *
123 * If the requested mode cannot be granted immediately, try to grant the lock
124 * in PR mode instead. If this alternate mode is granted instead of the
125 * requested mode, DLM_SBF_ALTMODE is returned in the lksb.
126 *
127 * DLM_LKF_ALTCW
128 *
129 * The same as ALTPR, but the alternate mode is CW.
130 *
131 * DLM_LKF_FORCEUNLOCK
132 *
133 * Unlock the lock even if it is converting or waiting or has sublocks.
134 * Only really for use by the userland device.c code.
135 *
136 */
137
138#define DLM_LKF_NOQUEUE 0x00000001
139#define DLM_LKF_CANCEL 0x00000002
140#define DLM_LKF_CONVERT 0x00000004
141#define DLM_LKF_VALBLK 0x00000008
142#define DLM_LKF_QUECVT 0x00000010
143#define DLM_LKF_IVVALBLK 0x00000020
144#define DLM_LKF_CONVDEADLK 0x00000040
145#define DLM_LKF_PERSISTENT 0x00000080
146#define DLM_LKF_NODLCKWT 0x00000100
147#define DLM_LKF_NODLCKBLK 0x00000200
148#define DLM_LKF_EXPEDITE 0x00000400
149#define DLM_LKF_NOQUEUEBAST 0x00000800
150#define DLM_LKF_HEADQUE 0x00001000
151#define DLM_LKF_NOORDER 0x00002000
152#define DLM_LKF_ORPHAN 0x00004000
153#define DLM_LKF_ALTPR 0x00008000
154#define DLM_LKF_ALTCW 0x00010000
155#define DLM_LKF_FORCEUNLOCK 0x00020000
156#define DLM_LKF_TIMEOUT 0x00040000
157
158/*
159 * Some return codes that are not in errno.h
160 */
161
162#define DLM_ECANCEL 0x10001
163#define DLM_EUNLOCK 0x10002
164 28
165typedef void dlm_lockspace_t; 29typedef void dlm_lockspace_t;
166 30
diff --git a/include/linux/dlmconstants.h b/include/linux/dlmconstants.h
new file mode 100644
index 000000000000..fddb3d3ff321
--- /dev/null
+++ b/include/linux/dlmconstants.h
@@ -0,0 +1,159 @@
1/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5** Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6**
7** This copyrighted material is made available to anyone wishing to use,
8** modify, copy, or redistribute it subject to the terms and conditions
9** of the GNU General Public License v.2.
10**
11*******************************************************************************
12******************************************************************************/
13
14#ifndef __DLMCONSTANTS_DOT_H__
15#define __DLMCONSTANTS_DOT_H__
16
17/*
18 * Constants used by DLM interface.
19 */
20
21/*
22 * Lock Modes
23 */
24
25#define DLM_LOCK_IV (-1) /* invalid */
26#define DLM_LOCK_NL 0 /* null */
27#define DLM_LOCK_CR 1 /* concurrent read */
28#define DLM_LOCK_CW 2 /* concurrent write */
29#define DLM_LOCK_PR 3 /* protected read */
30#define DLM_LOCK_PW 4 /* protected write */
31#define DLM_LOCK_EX 5 /* exclusive */
32
33
34/*
35 * Flags to dlm_lock
36 *
37 * DLM_LKF_NOQUEUE
38 *
39 * Do not queue the lock request on the wait queue if it cannot be granted
40 * immediately. If the lock cannot be granted because of this flag, DLM will
41 * either return -EAGAIN from the dlm_lock call or will return 0 from
42 * dlm_lock and -EAGAIN in the lock status block when the AST is executed.
43 *
44 * DLM_LKF_CANCEL
45 *
46 * Used to cancel a pending lock request or conversion. A converting lock is
47 * returned to its previously granted mode.
48 *
49 * DLM_LKF_CONVERT
50 *
51 * Indicates a lock conversion request. For conversions the name and namelen
52 * are ignored and the lock ID in the LKSB is used to identify the lock.
53 *
54 * DLM_LKF_VALBLK
55 *
56 * Requests DLM to return the current contents of the lock value block in the
57 * lock status block. When this flag is set in a lock conversion from PW or EX
58 * modes, DLM assigns the value specified in the lock status block to the lock
59 * value block of the lock resource. The LVB is a DLM_LVB_LEN size array
60 * containing application-specific information.
61 *
62 * DLM_LKF_QUECVT
63 *
64 * Force a conversion request to be queued, even if it is compatible with
65 * the granted modes of other locks on the same resource.
66 *
67 * DLM_LKF_IVVALBLK
68 *
69 * Invalidate the lock value block.
70 *
71 * DLM_LKF_CONVDEADLK
72 *
73 * Allows the dlm to resolve conversion deadlocks internally by demoting the
74 * granted mode of a converting lock to NL. The DLM_SBF_DEMOTED flag is
75 * returned for a conversion that's been effected by this.
76 *
77 * DLM_LKF_PERSISTENT
78 *
79 * Only relevant to locks originating in userspace. A persistent lock will not
80 * be removed if the process holding the lock exits.
81 *
82 * DLM_LKF_NODLCKWT
83 *
84 * Do not cancel the lock if it gets into conversion deadlock.
85 * Exclude this lock from being monitored due to DLM_LSFL_TIMEWARN.
86 *
87 * DLM_LKF_NODLCKBLK
88 *
89 * net yet implemented
90 *
91 * DLM_LKF_EXPEDITE
92 *
93 * Used only with new requests for NL mode locks. Tells the lock manager
94 * to grant the lock, ignoring other locks in convert and wait queues.
95 *
96 * DLM_LKF_NOQUEUEBAST
97 *
98 * Send blocking AST's before returning -EAGAIN to the caller. It is only
99 * used along with the NOQUEUE flag. Blocking AST's are not sent for failed
100 * NOQUEUE requests otherwise.
101 *
102 * DLM_LKF_HEADQUE
103 *
104 * Add a lock to the head of the convert or wait queue rather than the tail.
105 *
106 * DLM_LKF_NOORDER
107 *
108 * Disregard the standard grant order rules and grant a lock as soon as it
109 * is compatible with other granted locks.
110 *
111 * DLM_LKF_ORPHAN
112 *
113 * not yet implemented
114 *
115 * DLM_LKF_ALTPR
116 *
117 * If the requested mode cannot be granted immediately, try to grant the lock
118 * in PR mode instead. If this alternate mode is granted instead of the
119 * requested mode, DLM_SBF_ALTMODE is returned in the lksb.
120 *
121 * DLM_LKF_ALTCW
122 *
123 * The same as ALTPR, but the alternate mode is CW.
124 *
125 * DLM_LKF_FORCEUNLOCK
126 *
127 * Unlock the lock even if it is converting or waiting or has sublocks.
128 * Only really for use by the userland device.c code.
129 *
130 */
131
132#define DLM_LKF_NOQUEUE 0x00000001
133#define DLM_LKF_CANCEL 0x00000002
134#define DLM_LKF_CONVERT 0x00000004
135#define DLM_LKF_VALBLK 0x00000008
136#define DLM_LKF_QUECVT 0x00000010
137#define DLM_LKF_IVVALBLK 0x00000020
138#define DLM_LKF_CONVDEADLK 0x00000040
139#define DLM_LKF_PERSISTENT 0x00000080
140#define DLM_LKF_NODLCKWT 0x00000100
141#define DLM_LKF_NODLCKBLK 0x00000200
142#define DLM_LKF_EXPEDITE 0x00000400
143#define DLM_LKF_NOQUEUEBAST 0x00000800
144#define DLM_LKF_HEADQUE 0x00001000
145#define DLM_LKF_NOORDER 0x00002000
146#define DLM_LKF_ORPHAN 0x00004000
147#define DLM_LKF_ALTPR 0x00008000
148#define DLM_LKF_ALTCW 0x00010000
149#define DLM_LKF_FORCEUNLOCK 0x00020000
150#define DLM_LKF_TIMEOUT 0x00040000
151
152/*
153 * Some return codes that are not in errno.h
154 */
155
156#define DLM_ECANCEL 0x10001
157#define DLM_EUNLOCK 0x10002
158
159#endif /* __DLMCONSTANTS_DOT_H__ */
diff --git a/include/linux/futex.h b/include/linux/futex.h
index 92d420fe03f8..1a15f8e237a7 100644
--- a/include/linux/futex.h
+++ b/include/linux/futex.h
@@ -1,8 +1,12 @@
1#ifndef _LINUX_FUTEX_H 1#ifndef _LINUX_FUTEX_H
2#define _LINUX_FUTEX_H 2#define _LINUX_FUTEX_H
3 3
4#include <linux/sched.h> 4#include <linux/compiler.h>
5#include <linux/types.h>
5 6
7struct inode;
8struct mm_struct;
9struct task_struct;
6union ktime; 10union ktime;
7 11
8/* Second argument to futex syscall */ 12/* Second argument to futex syscall */
diff --git a/include/linux/hardirq.h b/include/linux/hardirq.h
index 8d302298a161..2961ec788046 100644
--- a/include/linux/hardirq.h
+++ b/include/linux/hardirq.h
@@ -72,11 +72,7 @@
72#define in_softirq() (softirq_count()) 72#define in_softirq() (softirq_count())
73#define in_interrupt() (irq_count()) 73#define in_interrupt() (irq_count())
74 74
75#if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL) 75#define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != 0)
76# define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != kernel_locked())
77#else
78# define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != 0)
79#endif
80 76
81#ifdef CONFIG_PREEMPT 77#ifdef CONFIG_PREEMPT
82# define PREEMPT_CHECK_OFFSET 1 78# define PREEMPT_CHECK_OFFSET 1
diff --git a/include/linux/hdreg.h b/include/linux/hdreg.h
index 818c6afc1091..ff43f8d6b5b3 100644
--- a/include/linux/hdreg.h
+++ b/include/linux/hdreg.h
@@ -44,7 +44,9 @@
44 44
45/* Bits for HD_ERROR */ 45/* Bits for HD_ERROR */
46#define MARK_ERR 0x01 /* Bad address mark */ 46#define MARK_ERR 0x01 /* Bad address mark */
47#define ILI_ERR 0x01 /* Illegal Length Indication (ATAPI) */
47#define TRK0_ERR 0x02 /* couldn't find track 0 */ 48#define TRK0_ERR 0x02 /* couldn't find track 0 */
49#define EOM_ERR 0x02 /* End Of Media (ATAPI) */
48#define ABRT_ERR 0x04 /* Command aborted */ 50#define ABRT_ERR 0x04 /* Command aborted */
49#define MCR_ERR 0x08 /* media change request */ 51#define MCR_ERR 0x08 /* media change request */
50#define ID_ERR 0x10 /* ID field not found */ 52#define ID_ERR 0x10 /* ID field not found */
@@ -52,6 +54,7 @@
52#define ECC_ERR 0x40 /* Uncorrectable ECC error */ 54#define ECC_ERR 0x40 /* Uncorrectable ECC error */
53#define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */ 55#define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */
54#define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */ 56#define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
57#define LFS_ERR 0xf0 /* Last Failed Sense (ATAPI) */
55 58
56/* Bits of HD_NSECTOR */ 59/* Bits of HD_NSECTOR */
57#define CD 0x01 60#define CD 0x01
@@ -70,13 +73,13 @@
70#define HDIO_DRIVE_HOB_HDR_SIZE (8 * sizeof(__u8)) 73#define HDIO_DRIVE_HOB_HDR_SIZE (8 * sizeof(__u8))
71#define HDIO_DRIVE_TASK_HDR_SIZE (8 * sizeof(__u8)) 74#define HDIO_DRIVE_TASK_HDR_SIZE (8 * sizeof(__u8))
72 75
73#define IDE_DRIVE_TASK_INVALID -1
74#define IDE_DRIVE_TASK_NO_DATA 0 76#define IDE_DRIVE_TASK_NO_DATA 0
77#ifndef __KERNEL__
78#define IDE_DRIVE_TASK_INVALID -1
75#define IDE_DRIVE_TASK_SET_XFER 1 79#define IDE_DRIVE_TASK_SET_XFER 1
76
77#define IDE_DRIVE_TASK_IN 2 80#define IDE_DRIVE_TASK_IN 2
78
79#define IDE_DRIVE_TASK_OUT 3 81#define IDE_DRIVE_TASK_OUT 3
82#endif
80#define IDE_DRIVE_TASK_RAW_WRITE 4 83#define IDE_DRIVE_TASK_RAW_WRITE 4
81 84
82/* 85/*
@@ -87,10 +90,10 @@
87#ifndef __KERNEL__ 90#ifndef __KERNEL__
88#define IDE_TASKFILE_STD_OUT_FLAGS 0xFE 91#define IDE_TASKFILE_STD_OUT_FLAGS 0xFE
89#define IDE_HOB_STD_OUT_FLAGS 0x3C 92#define IDE_HOB_STD_OUT_FLAGS 0x3C
90#endif
91 93
92typedef unsigned char task_ioreg_t; 94typedef unsigned char task_ioreg_t;
93typedef unsigned long sata_ioreg_t; 95typedef unsigned long sata_ioreg_t;
96#endif
94 97
95typedef union ide_reg_valid_s { 98typedef union ide_reg_valid_s {
96 unsigned all : 16; 99 unsigned all : 16;
@@ -116,8 +119,8 @@ typedef union ide_reg_valid_s {
116} ide_reg_valid_t; 119} ide_reg_valid_t;
117 120
118typedef struct ide_task_request_s { 121typedef struct ide_task_request_s {
119 task_ioreg_t io_ports[8]; 122 __u8 io_ports[8];
120 task_ioreg_t hob_ports[8]; 123 __u8 hob_ports[8]; /* bytes 6 and 7 are unused */
121 ide_reg_valid_t out_flags; 124 ide_reg_valid_t out_flags;
122 ide_reg_valid_t in_flags; 125 ide_reg_valid_t in_flags;
123 int data_phase; 126 int data_phase;
@@ -133,36 +136,35 @@ typedef struct ide_ioctl_request_s {
133} ide_ioctl_request_t; 136} ide_ioctl_request_t;
134 137
135struct hd_drive_cmd_hdr { 138struct hd_drive_cmd_hdr {
136 task_ioreg_t command; 139 __u8 command;
137 task_ioreg_t sector_number; 140 __u8 sector_number;
138 task_ioreg_t feature; 141 __u8 feature;
139 task_ioreg_t sector_count; 142 __u8 sector_count;
140}; 143};
141 144
145#ifndef __KERNEL__
142typedef struct hd_drive_task_hdr { 146typedef struct hd_drive_task_hdr {
143 task_ioreg_t data; 147 __u8 data;
144 task_ioreg_t feature; 148 __u8 feature;
145 task_ioreg_t sector_count; 149 __u8 sector_count;
146 task_ioreg_t sector_number; 150 __u8 sector_number;
147 task_ioreg_t low_cylinder; 151 __u8 low_cylinder;
148 task_ioreg_t high_cylinder; 152 __u8 high_cylinder;
149 task_ioreg_t device_head; 153 __u8 device_head;
150 task_ioreg_t command; 154 __u8 command;
151} task_struct_t; 155} task_struct_t;
152 156
153typedef struct hd_drive_hob_hdr { 157typedef struct hd_drive_hob_hdr {
154 task_ioreg_t data; 158 __u8 data;
155 task_ioreg_t feature; 159 __u8 feature;
156 task_ioreg_t sector_count; 160 __u8 sector_count;
157 task_ioreg_t sector_number; 161 __u8 sector_number;
158 task_ioreg_t low_cylinder; 162 __u8 low_cylinder;
159 task_ioreg_t high_cylinder; 163 __u8 high_cylinder;
160 task_ioreg_t device_head; 164 __u8 device_head;
161 task_ioreg_t control; 165 __u8 control;
162} hob_struct_t; 166} hob_struct_t;
163 167#endif
164#define TASKFILE_INVALID 0x7fff
165#define TASKFILE_48 0x8000
166 168
167#define TASKFILE_NO_DATA 0x0000 169#define TASKFILE_NO_DATA 0x0000
168 170
@@ -178,12 +180,16 @@ typedef struct hd_drive_hob_hdr {
178#define TASKFILE_IN_DMAQ 0x0080 180#define TASKFILE_IN_DMAQ 0x0080
179#define TASKFILE_OUT_DMAQ 0x0100 181#define TASKFILE_OUT_DMAQ 0x0100
180 182
183#ifndef __KERNEL__
181#define TASKFILE_P_IN 0x0200 184#define TASKFILE_P_IN 0x0200
182#define TASKFILE_P_OUT 0x0400 185#define TASKFILE_P_OUT 0x0400
183#define TASKFILE_P_IN_DMA 0x0800 186#define TASKFILE_P_IN_DMA 0x0800
184#define TASKFILE_P_OUT_DMA 0x1000 187#define TASKFILE_P_OUT_DMA 0x1000
185#define TASKFILE_P_IN_DMAQ 0x2000 188#define TASKFILE_P_IN_DMAQ 0x2000
186#define TASKFILE_P_OUT_DMAQ 0x4000 189#define TASKFILE_P_OUT_DMAQ 0x4000
190#define TASKFILE_48 0x8000
191#define TASKFILE_INVALID 0x7fff
192#endif
187 193
188/* ATA/ATAPI Commands pre T13 Spec */ 194/* ATA/ATAPI Commands pre T13 Spec */
189#define WIN_NOP 0x00 195#define WIN_NOP 0x00
diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
index 7a9398e19704..49067f14fac1 100644
--- a/include/linux/hrtimer.h
+++ b/include/linux/hrtimer.h
@@ -115,10 +115,8 @@ struct hrtimer {
115 enum hrtimer_restart (*function)(struct hrtimer *); 115 enum hrtimer_restart (*function)(struct hrtimer *);
116 struct hrtimer_clock_base *base; 116 struct hrtimer_clock_base *base;
117 unsigned long state; 117 unsigned long state;
118#ifdef CONFIG_HIGH_RES_TIMERS
119 enum hrtimer_cb_mode cb_mode; 118 enum hrtimer_cb_mode cb_mode;
120 struct list_head cb_entry; 119 struct list_head cb_entry;
121#endif
122#ifdef CONFIG_TIMER_STATS 120#ifdef CONFIG_TIMER_STATS
123 void *start_site; 121 void *start_site;
124 char start_comm[16]; 122 char start_comm[16];
@@ -194,10 +192,10 @@ struct hrtimer_cpu_base {
194 spinlock_t lock; 192 spinlock_t lock;
195 struct lock_class_key lock_key; 193 struct lock_class_key lock_key;
196 struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES]; 194 struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
195 struct list_head cb_pending;
197#ifdef CONFIG_HIGH_RES_TIMERS 196#ifdef CONFIG_HIGH_RES_TIMERS
198 ktime_t expires_next; 197 ktime_t expires_next;
199 int hres_active; 198 int hres_active;
200 struct list_head cb_pending;
201 unsigned long nr_events; 199 unsigned long nr_events;
202#endif 200#endif
203}; 201};
@@ -217,6 +215,11 @@ static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
217 return timer->base->get_time(); 215 return timer->base->get_time();
218} 216}
219 217
218static inline int hrtimer_is_hres_active(struct hrtimer *timer)
219{
220 return timer->base->cpu_base->hres_active;
221}
222
220/* 223/*
221 * The resolution of the clocks. The resolution value is returned in 224 * The resolution of the clocks. The resolution value is returned in
222 * the clock_getres() system call to give application programmers an 225 * the clock_getres() system call to give application programmers an
@@ -248,6 +251,10 @@ static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
248 return timer->base->softirq_time; 251 return timer->base->softirq_time;
249} 252}
250 253
254static inline int hrtimer_is_hres_active(struct hrtimer *timer)
255{
256 return 0;
257}
251#endif 258#endif
252 259
253extern ktime_t ktime_get(void); 260extern ktime_t ktime_get(void);
@@ -310,6 +317,7 @@ extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
310 317
311/* Soft interrupt function to run the hrtimer queues: */ 318/* Soft interrupt function to run the hrtimer queues: */
312extern void hrtimer_run_queues(void); 319extern void hrtimer_run_queues(void);
320extern void hrtimer_run_pending(void);
313 321
314/* Bootup initialization: */ 322/* Bootup initialization: */
315extern void __init hrtimers_init(void); 323extern void __init hrtimers_init(void);
diff --git a/include/linux/i2c-id.h b/include/linux/i2c-id.h
index e18017d45758..c7a51a196f51 100644
--- a/include/linux/i2c-id.h
+++ b/include/linux/i2c-id.h
@@ -125,6 +125,8 @@
125#define I2C_DRIVERID_LM4857 92 /* LM4857 Audio Amplifier */ 125#define I2C_DRIVERID_LM4857 92 /* LM4857 Audio Amplifier */
126#define I2C_DRIVERID_VP27SMPX 93 /* Panasonic VP27s tuner internal MPX */ 126#define I2C_DRIVERID_VP27SMPX 93 /* Panasonic VP27s tuner internal MPX */
127#define I2C_DRIVERID_CS4270 94 /* Cirrus Logic 4270 audio codec */ 127#define I2C_DRIVERID_CS4270 94 /* Cirrus Logic 4270 audio codec */
128#define I2C_DRIVERID_M52790 95 /* Mitsubishi M52790SP/FP AV switch */
129#define I2C_DRIVERID_CS5345 96 /* cs5345 audio processor */
128 130
129#define I2C_DRIVERID_I2CDEV 900 131#define I2C_DRIVERID_I2CDEV 900
130#define I2C_DRIVERID_ARP 902 /* SMBus ARP Client */ 132#define I2C_DRIVERID_ARP 902 /* SMBus ARP Client */
diff --git a/include/linux/ide.h b/include/linux/ide.h
index 9a6a41e7079f..1e4409937ec3 100644
--- a/include/linux/ide.h
+++ b/include/linux/ide.h
@@ -27,25 +27,10 @@
27#include <asm/semaphore.h> 27#include <asm/semaphore.h>
28#include <asm/mutex.h> 28#include <asm/mutex.h>
29 29
30/****************************************************************************** 30#if defined(CRIS) || defined(FRV)
31 * IDE driver configuration options (play with these as desired): 31# define SUPPORT_VLB_SYNC 0
32 * 32#else
33 * REALLY_SLOW_IO can be defined in ide.c and ide-cd.c, if necessary 33# define SUPPORT_VLB_SYNC 1
34 */
35#define INITIAL_MULT_COUNT 0 /* off=0; on=2,4,8,16,32, etc.. */
36
37#ifndef SUPPORT_SLOW_DATA_PORTS /* 1 to support slow data ports */
38#define SUPPORT_SLOW_DATA_PORTS 1 /* 0 to reduce kernel size */
39#endif
40#ifndef SUPPORT_VLB_SYNC /* 1 to support weird 32-bit chips */
41#define SUPPORT_VLB_SYNC 1 /* 0 to reduce kernel size */
42#endif
43#ifndef OK_TO_RESET_CONTROLLER /* 1 needed for good error recovery */
44#define OK_TO_RESET_CONTROLLER 1 /* 0 for use with AH2372A/B interface */
45#endif
46
47#ifndef DISABLE_IRQ_NOSYNC
48#define DISABLE_IRQ_NOSYNC 0
49#endif 34#endif
50 35
51/* 36/*
@@ -55,10 +40,6 @@
55 40
56#define IDE_NO_IRQ (-1) 41#define IDE_NO_IRQ (-1)
57 42
58/*
59 * "No user-serviceable parts" beyond this point :)
60 *****************************************************************************/
61
62typedef unsigned char byte; /* used everywhere */ 43typedef unsigned char byte; /* used everywhere */
63 44
64/* 45/*
@@ -103,8 +84,6 @@ typedef unsigned char byte; /* used everywhere */
103#define IDE_FEATURE_OFFSET IDE_ERROR_OFFSET 84#define IDE_FEATURE_OFFSET IDE_ERROR_OFFSET
104#define IDE_COMMAND_OFFSET IDE_STATUS_OFFSET 85#define IDE_COMMAND_OFFSET IDE_STATUS_OFFSET
105 86
106#define IDE_CONTROL_OFFSET_HOB (7)
107
108#define IDE_DATA_REG (HWIF(drive)->io_ports[IDE_DATA_OFFSET]) 87#define IDE_DATA_REG (HWIF(drive)->io_ports[IDE_DATA_OFFSET])
109#define IDE_ERROR_REG (HWIF(drive)->io_ports[IDE_ERROR_OFFSET]) 88#define IDE_ERROR_REG (HWIF(drive)->io_ports[IDE_ERROR_OFFSET])
110#define IDE_NSECTOR_REG (HWIF(drive)->io_ports[IDE_NSECTOR_OFFSET]) 89#define IDE_NSECTOR_REG (HWIF(drive)->io_ports[IDE_NSECTOR_OFFSET])
@@ -327,47 +306,16 @@ static inline void ide_init_hwif_ports(hw_regs_t *hw,
327typedef union { 306typedef union {
328 unsigned all : 8; 307 unsigned all : 8;
329 struct { 308 struct {
330#if defined(__LITTLE_ENDIAN_BITFIELD)
331 unsigned set_geometry : 1; 309 unsigned set_geometry : 1;
332 unsigned recalibrate : 1; 310 unsigned recalibrate : 1;
333 unsigned set_multmode : 1; 311 unsigned set_multmode : 1;
334 unsigned set_tune : 1; 312 unsigned set_tune : 1;
335 unsigned serviced : 1; 313 unsigned serviced : 1;
336 unsigned reserved : 3; 314 unsigned reserved : 3;
337#elif defined(__BIG_ENDIAN_BITFIELD)
338 unsigned reserved : 3;
339 unsigned serviced : 1;
340 unsigned set_tune : 1;
341 unsigned set_multmode : 1;
342 unsigned recalibrate : 1;
343 unsigned set_geometry : 1;
344#else
345#error "Please fix <asm/byteorder.h>"
346#endif
347 } b; 315 } b;
348} special_t; 316} special_t;
349 317
350/* 318/*
351 * ATA DATA Register Special.
352 * ATA NSECTOR Count Register().
353 * ATAPI Byte Count Register.
354 */
355typedef union {
356 unsigned all :16;
357 struct {
358#if defined(__LITTLE_ENDIAN_BITFIELD)
359 unsigned low :8; /* LSB */
360 unsigned high :8; /* MSB */
361#elif defined(__BIG_ENDIAN_BITFIELD)
362 unsigned high :8; /* MSB */
363 unsigned low :8; /* LSB */
364#else
365#error "Please fix <asm/byteorder.h>"
366#endif
367 } b;
368} ata_nsector_t, ata_data_t, atapi_bcount_t;
369
370/*
371 * ATA-IDE Select Register, aka Device-Head 319 * ATA-IDE Select Register, aka Device-Head
372 * 320 *
373 * head : always zeros here 321 * head : always zeros here
@@ -398,131 +346,6 @@ typedef union {
398} select_t, ata_select_t; 346} select_t, ata_select_t;
399 347
400/* 348/*
401 * The ATA-IDE Status Register.
402 * The ATAPI Status Register.
403 *
404 * check : Error occurred
405 * idx : Index Error
406 * corr : Correctable error occurred
407 * drq : Data is request by the device
408 * dsc : Disk Seek Complete : ata
409 * : Media access command finished : atapi
410 * df : Device Fault : ata
411 * : Reserved : atapi
412 * drdy : Ready, Command Mode Capable : ata
413 * : Ignored for ATAPI commands : atapi
414 * bsy : Disk is Busy
415 * : The device has access to the command block
416 */
417typedef union {
418 unsigned all :8;
419 struct {
420#if defined(__LITTLE_ENDIAN_BITFIELD)
421 unsigned check :1;
422 unsigned idx :1;
423 unsigned corr :1;
424 unsigned drq :1;
425 unsigned dsc :1;
426 unsigned df :1;
427 unsigned drdy :1;
428 unsigned bsy :1;
429#elif defined(__BIG_ENDIAN_BITFIELD)
430 unsigned bsy :1;
431 unsigned drdy :1;
432 unsigned df :1;
433 unsigned dsc :1;
434 unsigned drq :1;
435 unsigned corr :1;
436 unsigned idx :1;
437 unsigned check :1;
438#else
439#error "Please fix <asm/byteorder.h>"
440#endif
441 } b;
442} ata_status_t, atapi_status_t;
443
444/*
445 * ATAPI Feature Register
446 *
447 * dma : Using DMA or PIO
448 * reserved321 : Reserved
449 * reserved654 : Reserved (Tag Type)
450 * reserved7 : Reserved
451 */
452typedef union {
453 unsigned all :8;
454 struct {
455#if defined(__LITTLE_ENDIAN_BITFIELD)
456 unsigned dma :1;
457 unsigned reserved321 :3;
458 unsigned reserved654 :3;
459 unsigned reserved7 :1;
460#elif defined(__BIG_ENDIAN_BITFIELD)
461 unsigned reserved7 :1;
462 unsigned reserved654 :3;
463 unsigned reserved321 :3;
464 unsigned dma :1;
465#else
466#error "Please fix <asm/byteorder.h>"
467#endif
468 } b;
469} atapi_feature_t;
470
471/*
472 * ATAPI Interrupt Reason Register.
473 *
474 * cod : Information transferred is command (1) or data (0)
475 * io : The device requests us to read (1) or write (0)
476 * reserved : Reserved
477 */
478typedef union {
479 unsigned all :8;
480 struct {
481#if defined(__LITTLE_ENDIAN_BITFIELD)
482 unsigned cod :1;
483 unsigned io :1;
484 unsigned reserved :6;
485#elif defined(__BIG_ENDIAN_BITFIELD)
486 unsigned reserved :6;
487 unsigned io :1;
488 unsigned cod :1;
489#else
490#error "Please fix <asm/byteorder.h>"
491#endif
492 } b;
493} atapi_ireason_t;
494
495/*
496 * The ATAPI error register.
497 *
498 * ili : Illegal Length Indication
499 * eom : End Of Media Detected
500 * abrt : Aborted command - As defined by ATA
501 * mcr : Media Change Requested - As defined by ATA
502 * sense_key : Sense key of the last failed packet command
503 */
504typedef union {
505 unsigned all :8;
506 struct {
507#if defined(__LITTLE_ENDIAN_BITFIELD)
508 unsigned ili :1;
509 unsigned eom :1;
510 unsigned abrt :1;
511 unsigned mcr :1;
512 unsigned sense_key :4;
513#elif defined(__BIG_ENDIAN_BITFIELD)
514 unsigned sense_key :4;
515 unsigned mcr :1;
516 unsigned abrt :1;
517 unsigned eom :1;
518 unsigned ili :1;
519#else
520#error "Please fix <asm/byteorder.h>"
521#endif
522 } b;
523} atapi_error_t;
524
525/*
526 * Status returned from various ide_ functions 349 * Status returned from various ide_ functions
527 */ 350 */
528typedef enum { 351typedef enum {
@@ -701,8 +524,6 @@ typedef struct hwif_s {
701 void (*pre_reset)(ide_drive_t *); 524 void (*pre_reset)(ide_drive_t *);
702 /* routine to reset controller after a disk reset */ 525 /* routine to reset controller after a disk reset */
703 void (*resetproc)(ide_drive_t *); 526 void (*resetproc)(ide_drive_t *);
704 /* special interrupt handling for shared pci interrupts */
705 void (*intrproc)(ide_drive_t *);
706 /* special host masking for drive selection */ 527 /* special host masking for drive selection */
707 void (*maskproc)(ide_drive_t *, int); 528 void (*maskproc)(ide_drive_t *, int);
708 /* check host's drive quirk list */ 529 /* check host's drive quirk list */
@@ -766,7 +587,6 @@ typedef struct hwif_s {
766 int rqsize; /* max sectors per request */ 587 int rqsize; /* max sectors per request */
767 int irq; /* our irq number */ 588 int irq; /* our irq number */
768 589
769 unsigned long dma_master; /* reference base addr dmabase */
770 unsigned long dma_base; /* base addr for dma ports */ 590 unsigned long dma_base; /* base addr for dma ports */
771 unsigned long dma_command; /* dma command register */ 591 unsigned long dma_command; /* dma command register */
772 unsigned long dma_vendor1; /* dma vendor 1 register */ 592 unsigned long dma_vendor1; /* dma vendor 1 register */
@@ -806,7 +626,6 @@ typedef struct hwif_s {
806/* 626/*
807 * internal ide interrupt handler type 627 * internal ide interrupt handler type
808 */ 628 */
809typedef ide_startstop_t (ide_pre_handler_t)(ide_drive_t *, struct request *);
810typedef ide_startstop_t (ide_handler_t)(ide_drive_t *); 629typedef ide_startstop_t (ide_handler_t)(ide_drive_t *);
811typedef int (ide_expiry_t)(ide_drive_t *); 630typedef int (ide_expiry_t)(ide_drive_t *);
812 631
@@ -1020,7 +839,8 @@ int ide_end_dequeued_request(ide_drive_t *drive, struct request *rq,
1020 839
1021extern void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, unsigned int timeout, ide_expiry_t *expiry); 840extern void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, unsigned int timeout, ide_expiry_t *expiry);
1022 841
1023extern void ide_execute_command(ide_drive_t *, task_ioreg_t cmd, ide_handler_t *, unsigned int, ide_expiry_t *); 842void ide_execute_command(ide_drive_t *, u8, ide_handler_t *, unsigned int,
843 ide_expiry_t *);
1024 844
1025ide_startstop_t __ide_error(ide_drive_t *, struct request *, u8, u8); 845ide_startstop_t __ide_error(ide_drive_t *, struct request *, u8, u8);
1026 846
@@ -1062,52 +882,114 @@ extern void ide_end_drive_cmd(ide_drive_t *, u8, u8);
1062 */ 882 */
1063extern int ide_wait_cmd(ide_drive_t *, u8, u8, u8, u8, u8 *); 883extern int ide_wait_cmd(ide_drive_t *, u8, u8, u8, u8, u8 *);
1064 884
885enum {
886 IDE_TFLAG_LBA48 = (1 << 0),
887 IDE_TFLAG_NO_SELECT_MASK = (1 << 1),
888 IDE_TFLAG_FLAGGED = (1 << 2),
889 IDE_TFLAG_OUT_DATA = (1 << 3),
890 IDE_TFLAG_OUT_HOB_FEATURE = (1 << 4),
891 IDE_TFLAG_OUT_HOB_NSECT = (1 << 5),
892 IDE_TFLAG_OUT_HOB_LBAL = (1 << 6),
893 IDE_TFLAG_OUT_HOB_LBAM = (1 << 7),
894 IDE_TFLAG_OUT_HOB_LBAH = (1 << 8),
895 IDE_TFLAG_OUT_HOB = IDE_TFLAG_OUT_HOB_FEATURE |
896 IDE_TFLAG_OUT_HOB_NSECT |
897 IDE_TFLAG_OUT_HOB_LBAL |
898 IDE_TFLAG_OUT_HOB_LBAM |
899 IDE_TFLAG_OUT_HOB_LBAH,
900 IDE_TFLAG_OUT_FEATURE = (1 << 9),
901 IDE_TFLAG_OUT_NSECT = (1 << 10),
902 IDE_TFLAG_OUT_LBAL = (1 << 11),
903 IDE_TFLAG_OUT_LBAM = (1 << 12),
904 IDE_TFLAG_OUT_LBAH = (1 << 13),
905 IDE_TFLAG_OUT_TF = IDE_TFLAG_OUT_FEATURE |
906 IDE_TFLAG_OUT_NSECT |
907 IDE_TFLAG_OUT_LBAL |
908 IDE_TFLAG_OUT_LBAM |
909 IDE_TFLAG_OUT_LBAH,
910 IDE_TFLAG_OUT_DEVICE = (1 << 14),
911 IDE_TFLAG_WRITE = (1 << 15),
912 IDE_TFLAG_FLAGGED_SET_IN_FLAGS = (1 << 16),
913 IDE_TFLAG_IN_DATA = (1 << 17),
914 IDE_TFLAG_CUSTOM_HANDLER = (1 << 18),
915 IDE_TFLAG_DMA_PIO_FALLBACK = (1 << 19),
916 IDE_TFLAG_IN_HOB_FEATURE = (1 << 20),
917 IDE_TFLAG_IN_HOB_NSECT = (1 << 21),
918 IDE_TFLAG_IN_HOB_LBAL = (1 << 22),
919 IDE_TFLAG_IN_HOB_LBAM = (1 << 23),
920 IDE_TFLAG_IN_HOB_LBAH = (1 << 24),
921 IDE_TFLAG_IN_HOB_LBA = IDE_TFLAG_IN_HOB_LBAL |
922 IDE_TFLAG_IN_HOB_LBAM |
923 IDE_TFLAG_IN_HOB_LBAH,
924 IDE_TFLAG_IN_HOB = IDE_TFLAG_IN_HOB_FEATURE |
925 IDE_TFLAG_IN_HOB_NSECT |
926 IDE_TFLAG_IN_HOB_LBA,
927 IDE_TFLAG_IN_NSECT = (1 << 25),
928 IDE_TFLAG_IN_LBAL = (1 << 26),
929 IDE_TFLAG_IN_LBAM = (1 << 27),
930 IDE_TFLAG_IN_LBAH = (1 << 28),
931 IDE_TFLAG_IN_LBA = IDE_TFLAG_IN_LBAL |
932 IDE_TFLAG_IN_LBAM |
933 IDE_TFLAG_IN_LBAH,
934 IDE_TFLAG_IN_TF = IDE_TFLAG_IN_NSECT |
935 IDE_TFLAG_IN_LBA,
936 IDE_TFLAG_IN_DEVICE = (1 << 29),
937};
938
939struct ide_taskfile {
940 u8 hob_data; /* 0: high data byte (for TASKFILE IOCTL) */
941
942 u8 hob_feature; /* 1-5: additional data to support LBA48 */
943 u8 hob_nsect;
944 u8 hob_lbal;
945 u8 hob_lbam;
946 u8 hob_lbah;
947
948 u8 data; /* 6: low data byte (for TASKFILE IOCTL) */
949
950 union { /*  7: */
951 u8 error; /* read: error */
952 u8 feature; /* write: feature */
953 };
954
955 u8 nsect; /* 8: number of sectors */
956 u8 lbal; /* 9: LBA low */
957 u8 lbam; /* 10: LBA mid */
958 u8 lbah; /* 11: LBA high */
959
960 u8 device; /* 12: device select */
961
962 union { /* 13: */
963 u8 status; /*  read: status  */
964 u8 command; /* write: command */
965 };
966};
967
1065typedef struct ide_task_s { 968typedef struct ide_task_s {
1066/* 969 union {
1067 * struct hd_drive_task_hdr tf; 970 struct ide_taskfile tf;
1068 * task_struct_t tf; 971 u8 tf_array[14];
1069 * struct hd_drive_hob_hdr hobf; 972 };
1070 * hob_struct_t hobf; 973 u32 tf_flags;
1071 */
1072 task_ioreg_t tfRegister[8];
1073 task_ioreg_t hobRegister[8];
1074 ide_reg_valid_t tf_out_flags;
1075 ide_reg_valid_t tf_in_flags;
1076 int data_phase; 974 int data_phase;
1077 int command_type;
1078 ide_pre_handler_t *prehandler;
1079 ide_handler_t *handler;
1080 struct request *rq; /* copy of request */ 975 struct request *rq; /* copy of request */
1081 void *special; /* valid_t generally */ 976 void *special; /* valid_t generally */
1082} ide_task_t; 977} ide_task_t;
1083 978
1084extern u32 ide_read_24(ide_drive_t *); 979void ide_tf_load(ide_drive_t *, ide_task_t *);
980void ide_tf_read(ide_drive_t *, ide_task_t *);
1085 981
1086extern void SELECT_DRIVE(ide_drive_t *); 982extern void SELECT_DRIVE(ide_drive_t *);
1087extern void SELECT_INTERRUPT(ide_drive_t *);
1088extern void SELECT_MASK(ide_drive_t *, int); 983extern void SELECT_MASK(ide_drive_t *, int);
1089extern void QUIRK_LIST(ide_drive_t *);
1090 984
1091extern int drive_is_ready(ide_drive_t *); 985extern int drive_is_ready(ide_drive_t *);
1092 986
1093/* 987void ide_pktcmd_tf_load(ide_drive_t *, u32, u16, u8);
1094 * taskfile io for disks for now...and builds request from ide_ioctl
1095 */
1096extern ide_startstop_t do_rw_taskfile(ide_drive_t *, ide_task_t *);
1097
1098/*
1099 * Special Flagged Register Validation Caller
1100 */
1101extern ide_startstop_t flagged_taskfile(ide_drive_t *, ide_task_t *);
1102 988
1103extern ide_startstop_t set_multmode_intr(ide_drive_t *); 989ide_startstop_t do_rw_taskfile(ide_drive_t *, ide_task_t *);
1104extern ide_startstop_t set_geometry_intr(ide_drive_t *);
1105extern ide_startstop_t recal_intr(ide_drive_t *);
1106extern ide_startstop_t task_no_data_intr(ide_drive_t *);
1107extern ide_startstop_t task_in_intr(ide_drive_t *);
1108extern ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
1109 990
1110extern int ide_raw_taskfile(ide_drive_t *, ide_task_t *, u8 *); 991int ide_raw_taskfile(ide_drive_t *, ide_task_t *, u8 *, u16);
992int ide_no_data_taskfile(ide_drive_t *, ide_task_t *);
1111 993
1112int ide_taskfile_ioctl(ide_drive_t *, unsigned int, unsigned long); 994int ide_taskfile_ioctl(ide_drive_t *, unsigned int, unsigned long);
1113int ide_cmd_ioctl(ide_drive_t *, unsigned int, unsigned long); 995int ide_cmd_ioctl(ide_drive_t *, unsigned int, unsigned long);
@@ -1212,6 +1094,7 @@ enum {
1212 IDE_HFLAG_IO_32BIT = (1 << 24), 1094 IDE_HFLAG_IO_32BIT = (1 << 24),
1213 /* unmask IRQs */ 1095 /* unmask IRQs */
1214 IDE_HFLAG_UNMASK_IRQS = (1 << 25), 1096 IDE_HFLAG_UNMASK_IRQS = (1 << 25),
1097 IDE_HFLAG_ABUSE_SET_DMA_MODE = (1 << 26),
1215}; 1098};
1216 1099
1217#ifdef CONFIG_BLK_DEV_OFFBOARD 1100#ifdef CONFIG_BLK_DEV_OFFBOARD
@@ -1229,7 +1112,7 @@ struct ide_port_info {
1229 void (*fixup)(ide_hwif_t *); 1112 void (*fixup)(ide_hwif_t *);
1230 ide_pci_enablebit_t enablebits[2]; 1113 ide_pci_enablebit_t enablebits[2];
1231 hwif_chipset_t chipset; 1114 hwif_chipset_t chipset;
1232 unsigned int extra; 1115 u8 extra;
1233 u32 host_flags; 1116 u32 host_flags;
1234 u8 pio_mask; 1117 u8 pio_mask;
1235 u8 swdma_mask; 1118 u8 swdma_mask;
@@ -1356,6 +1239,7 @@ static inline int ide_dev_is_sata(struct hd_driveid *id)
1356 return 0; 1239 return 0;
1357} 1240}
1358 1241
1242u64 ide_get_lba_addr(struct ide_taskfile *, int);
1359u8 ide_dump_status(ide_drive_t *, const char *, u8); 1243u8 ide_dump_status(ide_drive_t *, const char *, u8);
1360 1244
1361typedef struct ide_pio_timings_s { 1245typedef struct ide_pio_timings_s {
diff --git a/include/linux/init_task.h b/include/linux/init_task.h
index cae35b6b9aec..796019b22b6f 100644
--- a/include/linux/init_task.h
+++ b/include/linux/init_task.h
@@ -132,9 +132,12 @@ extern struct group_info init_groups;
132 .cpus_allowed = CPU_MASK_ALL, \ 132 .cpus_allowed = CPU_MASK_ALL, \
133 .mm = NULL, \ 133 .mm = NULL, \
134 .active_mm = &init_mm, \ 134 .active_mm = &init_mm, \
135 .run_list = LIST_HEAD_INIT(tsk.run_list), \ 135 .rt = { \
136 .run_list = LIST_HEAD_INIT(tsk.rt.run_list), \
137 .time_slice = HZ, \
138 .nr_cpus_allowed = NR_CPUS, \
139 }, \
136 .ioprio = 0, \ 140 .ioprio = 0, \
137 .time_slice = HZ, \
138 .tasks = LIST_HEAD_INIT(tsk.tasks), \ 141 .tasks = LIST_HEAD_INIT(tsk.tasks), \
139 .ptrace_children= LIST_HEAD_INIT(tsk.ptrace_children), \ 142 .ptrace_children= LIST_HEAD_INIT(tsk.ptrace_children), \
140 .ptrace_list = LIST_HEAD_INIT(tsk.ptrace_list), \ 143 .ptrace_list = LIST_HEAD_INIT(tsk.ptrace_list), \
diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
index 2306920fa388..c3db4a00f1fa 100644
--- a/include/linux/interrupt.h
+++ b/include/linux/interrupt.h
@@ -256,6 +256,7 @@ enum
256#ifdef CONFIG_HIGH_RES_TIMERS 256#ifdef CONFIG_HIGH_RES_TIMERS
257 HRTIMER_SOFTIRQ, 257 HRTIMER_SOFTIRQ,
258#endif 258#endif
259 RCU_SOFTIRQ, /* Preferable RCU should always be the last softirq */
259}; 260};
260 261
261/* softirq mask and active fields moved to irq_cpustat_t in 262/* softirq mask and active fields moved to irq_cpustat_t in
diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h
index 8b080024bbc1..7ba9e47bf061 100644
--- a/include/linux/jiffies.h
+++ b/include/linux/jiffies.h
@@ -29,6 +29,12 @@
29# define SHIFT_HZ 9 29# define SHIFT_HZ 9
30#elif HZ >= 768 && HZ < 1536 30#elif HZ >= 768 && HZ < 1536
31# define SHIFT_HZ 10 31# define SHIFT_HZ 10
32#elif HZ >= 1536 && HZ < 3072
33# define SHIFT_HZ 11
34#elif HZ >= 3072 && HZ < 6144
35# define SHIFT_HZ 12
36#elif HZ >= 6144 && HZ < 12288
37# define SHIFT_HZ 13
32#else 38#else
33# error You lose. 39# error You lose.
34#endif 40#endif
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 94bc99656963..a7283c9beadf 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -105,8 +105,8 @@ struct user;
105 * supposed to. 105 * supposed to.
106 */ 106 */
107#ifdef CONFIG_PREEMPT_VOLUNTARY 107#ifdef CONFIG_PREEMPT_VOLUNTARY
108extern int cond_resched(void); 108extern int _cond_resched(void);
109# define might_resched() cond_resched() 109# define might_resched() _cond_resched()
110#else 110#else
111# define might_resched() do { } while (0) 111# define might_resched() do { } while (0)
112#endif 112#endif
diff --git a/include/linux/latencytop.h b/include/linux/latencytop.h
new file mode 100644
index 000000000000..901c2d6377a8
--- /dev/null
+++ b/include/linux/latencytop.h
@@ -0,0 +1,44 @@
1/*
2 * latencytop.h: Infrastructure for displaying latency
3 *
4 * (C) Copyright 2008 Intel Corporation
5 * Author: Arjan van de Ven <arjan@linux.intel.com>
6 *
7 */
8
9#ifndef _INCLUDE_GUARD_LATENCYTOP_H_
10#define _INCLUDE_GUARD_LATENCYTOP_H_
11
12#ifdef CONFIG_LATENCYTOP
13
14#define LT_SAVECOUNT 32
15#define LT_BACKTRACEDEPTH 12
16
17struct latency_record {
18 unsigned long backtrace[LT_BACKTRACEDEPTH];
19 unsigned int count;
20 unsigned long time;
21 unsigned long max;
22};
23
24
25struct task_struct;
26
27void account_scheduler_latency(struct task_struct *task, int usecs, int inter);
28
29void clear_all_latency_tracing(struct task_struct *p);
30
31#else
32
33static inline void
34account_scheduler_latency(struct task_struct *task, int usecs, int inter)
35{
36}
37
38static inline void clear_all_latency_tracing(struct task_struct *p)
39{
40}
41
42#endif
43
44#endif
diff --git a/include/linux/libata.h b/include/linux/libata.h
index 124033cb5e9b..4374c4277780 100644
--- a/include/linux/libata.h
+++ b/include/linux/libata.h
@@ -35,6 +35,7 @@
35#include <linux/workqueue.h> 35#include <linux/workqueue.h>
36#include <scsi/scsi_host.h> 36#include <scsi/scsi_host.h>
37#include <linux/acpi.h> 37#include <linux/acpi.h>
38#include <linux/cdrom.h>
38 39
39/* 40/*
40 * Define if arch has non-standard setup. This is a _PCI_ standard 41 * Define if arch has non-standard setup. This is a _PCI_ standard
@@ -143,10 +144,11 @@ enum {
143 ATA_DFLAG_NCQ_OFF = (1 << 13), /* device limited to non-NCQ mode */ 144 ATA_DFLAG_NCQ_OFF = (1 << 13), /* device limited to non-NCQ mode */
144 ATA_DFLAG_SPUNDOWN = (1 << 14), /* XXX: for spindown_compat */ 145 ATA_DFLAG_SPUNDOWN = (1 << 14), /* XXX: for spindown_compat */
145 ATA_DFLAG_SLEEPING = (1 << 15), /* device is sleeping */ 146 ATA_DFLAG_SLEEPING = (1 << 15), /* device is sleeping */
146 ATA_DFLAG_INIT_MASK = (1 << 16) - 1, 147 ATA_DFLAG_DUBIOUS_XFER = (1 << 16), /* data transfer not verified */
148 ATA_DFLAG_INIT_MASK = (1 << 24) - 1,
147 149
148 ATA_DFLAG_DETACH = (1 << 16), 150 ATA_DFLAG_DETACH = (1 << 24),
149 ATA_DFLAG_DETACHED = (1 << 17), 151 ATA_DFLAG_DETACHED = (1 << 25),
150 152
151 ATA_DEV_UNKNOWN = 0, /* unknown device */ 153 ATA_DEV_UNKNOWN = 0, /* unknown device */
152 ATA_DEV_ATA = 1, /* ATA device */ 154 ATA_DEV_ATA = 1, /* ATA device */
@@ -217,9 +219,7 @@ enum {
217 219
218 /* struct ata_queued_cmd flags */ 220 /* struct ata_queued_cmd flags */
219 ATA_QCFLAG_ACTIVE = (1 << 0), /* cmd not yet ack'd to scsi lyer */ 221 ATA_QCFLAG_ACTIVE = (1 << 0), /* cmd not yet ack'd to scsi lyer */
220 ATA_QCFLAG_SG = (1 << 1), /* have s/g table? */ 222 ATA_QCFLAG_DMAMAP = (1 << 1), /* SG table is DMA mapped */
221 ATA_QCFLAG_SINGLE = (1 << 2), /* no s/g, just a single buffer */
222 ATA_QCFLAG_DMAMAP = ATA_QCFLAG_SG | ATA_QCFLAG_SINGLE,
223 ATA_QCFLAG_IO = (1 << 3), /* standard IO command */ 223 ATA_QCFLAG_IO = (1 << 3), /* standard IO command */
224 ATA_QCFLAG_RESULT_TF = (1 << 4), /* result TF requested */ 224 ATA_QCFLAG_RESULT_TF = (1 << 4), /* result TF requested */
225 ATA_QCFLAG_CLEAR_EXCL = (1 << 5), /* clear excl_link on completion */ 225 ATA_QCFLAG_CLEAR_EXCL = (1 << 5), /* clear excl_link on completion */
@@ -266,19 +266,15 @@ enum {
266 PORT_DISABLED = 2, 266 PORT_DISABLED = 2,
267 267
268 /* encoding various smaller bitmaps into a single 268 /* encoding various smaller bitmaps into a single
269 * unsigned int bitmap 269 * unsigned long bitmap
270 */ 270 */
271 ATA_BITS_PIO = 7, 271 ATA_NR_PIO_MODES = 7,
272 ATA_BITS_MWDMA = 5, 272 ATA_NR_MWDMA_MODES = 5,
273 ATA_BITS_UDMA = 8, 273 ATA_NR_UDMA_MODES = 8,
274 274
275 ATA_SHIFT_PIO = 0, 275 ATA_SHIFT_PIO = 0,
276 ATA_SHIFT_MWDMA = ATA_SHIFT_PIO + ATA_BITS_PIO, 276 ATA_SHIFT_MWDMA = ATA_SHIFT_PIO + ATA_NR_PIO_MODES,
277 ATA_SHIFT_UDMA = ATA_SHIFT_MWDMA + ATA_BITS_MWDMA, 277 ATA_SHIFT_UDMA = ATA_SHIFT_MWDMA + ATA_NR_MWDMA_MODES,
278
279 ATA_MASK_PIO = ((1 << ATA_BITS_PIO) - 1) << ATA_SHIFT_PIO,
280 ATA_MASK_MWDMA = ((1 << ATA_BITS_MWDMA) - 1) << ATA_SHIFT_MWDMA,
281 ATA_MASK_UDMA = ((1 << ATA_BITS_UDMA) - 1) << ATA_SHIFT_UDMA,
282 278
283 /* size of buffer to pad xfers ending on unaligned boundaries */ 279 /* size of buffer to pad xfers ending on unaligned boundaries */
284 ATA_DMA_PAD_SZ = 4, 280 ATA_DMA_PAD_SZ = 4,
@@ -349,6 +345,21 @@ enum {
349 ATA_DMA_MASK_ATA = (1 << 0), /* DMA on ATA Disk */ 345 ATA_DMA_MASK_ATA = (1 << 0), /* DMA on ATA Disk */
350 ATA_DMA_MASK_ATAPI = (1 << 1), /* DMA on ATAPI */ 346 ATA_DMA_MASK_ATAPI = (1 << 1), /* DMA on ATAPI */
351 ATA_DMA_MASK_CFA = (1 << 2), /* DMA on CF Card */ 347 ATA_DMA_MASK_CFA = (1 << 2), /* DMA on CF Card */
348
349 /* ATAPI command types */
350 ATAPI_READ = 0, /* READs */
351 ATAPI_WRITE = 1, /* WRITEs */
352 ATAPI_READ_CD = 2, /* READ CD [MSF] */
353 ATAPI_MISC = 3, /* the rest */
354};
355
356enum ata_xfer_mask {
357 ATA_MASK_PIO = ((1LU << ATA_NR_PIO_MODES) - 1)
358 << ATA_SHIFT_PIO,
359 ATA_MASK_MWDMA = ((1LU << ATA_NR_MWDMA_MODES) - 1)
360 << ATA_SHIFT_MWDMA,
361 ATA_MASK_UDMA = ((1LU << ATA_NR_UDMA_MODES) - 1)
362 << ATA_SHIFT_UDMA,
352}; 363};
353 364
354enum hsm_task_states { 365enum hsm_task_states {
@@ -447,7 +458,7 @@ struct ata_queued_cmd {
447 unsigned int tag; 458 unsigned int tag;
448 unsigned int n_elem; 459 unsigned int n_elem;
449 unsigned int n_iter; 460 unsigned int n_iter;
450 unsigned int orig_n_elem; 461 unsigned int mapped_n_elem;
451 462
452 int dma_dir; 463 int dma_dir;
453 464
@@ -455,17 +466,18 @@ struct ata_queued_cmd {
455 unsigned int sect_size; 466 unsigned int sect_size;
456 467
457 unsigned int nbytes; 468 unsigned int nbytes;
469 unsigned int raw_nbytes;
458 unsigned int curbytes; 470 unsigned int curbytes;
459 471
460 struct scatterlist *cursg; 472 struct scatterlist *cursg;
461 unsigned int cursg_ofs; 473 unsigned int cursg_ofs;
462 474
475 struct scatterlist *last_sg;
476 struct scatterlist saved_last_sg;
463 struct scatterlist sgent; 477 struct scatterlist sgent;
464 struct scatterlist pad_sgent; 478 struct scatterlist extra_sg[2];
465 void *buf_virt;
466 479
467 /* DO NOT iterate over __sg manually, use ata_for_each_sg() */ 480 struct scatterlist *sg;
468 struct scatterlist *__sg;
469 481
470 unsigned int err_mask; 482 unsigned int err_mask;
471 struct ata_taskfile result_tf; 483 struct ata_taskfile result_tf;
@@ -482,7 +494,7 @@ struct ata_port_stats {
482}; 494};
483 495
484struct ata_ering_entry { 496struct ata_ering_entry {
485 int is_io; 497 unsigned int eflags;
486 unsigned int err_mask; 498 unsigned int err_mask;
487 u64 timestamp; 499 u64 timestamp;
488}; 500};
@@ -522,9 +534,9 @@ struct ata_device {
522 unsigned int cdb_len; 534 unsigned int cdb_len;
523 535
524 /* per-dev xfer mask */ 536 /* per-dev xfer mask */
525 unsigned int pio_mask; 537 unsigned long pio_mask;
526 unsigned int mwdma_mask; 538 unsigned long mwdma_mask;
527 unsigned int udma_mask; 539 unsigned long udma_mask;
528 540
529 /* for CHS addressing */ 541 /* for CHS addressing */
530 u16 cylinders; /* Number of cylinders */ 542 u16 cylinders; /* Number of cylinders */
@@ -560,6 +572,8 @@ struct ata_eh_context {
560 int tries[ATA_MAX_DEVICES]; 572 int tries[ATA_MAX_DEVICES];
561 unsigned int classes[ATA_MAX_DEVICES]; 573 unsigned int classes[ATA_MAX_DEVICES];
562 unsigned int did_probe_mask; 574 unsigned int did_probe_mask;
575 unsigned int saved_ncq_enabled;
576 u8 saved_xfer_mode[ATA_MAX_DEVICES];
563}; 577};
564 578
565struct ata_acpi_drive 579struct ata_acpi_drive
@@ -686,7 +700,8 @@ struct ata_port_operations {
686 void (*bmdma_setup) (struct ata_queued_cmd *qc); 700 void (*bmdma_setup) (struct ata_queued_cmd *qc);
687 void (*bmdma_start) (struct ata_queued_cmd *qc); 701 void (*bmdma_start) (struct ata_queued_cmd *qc);
688 702
689 void (*data_xfer) (struct ata_device *, unsigned char *, unsigned int, int); 703 unsigned int (*data_xfer) (struct ata_device *dev, unsigned char *buf,
704 unsigned int buflen, int rw);
690 705
691 int (*qc_defer) (struct ata_queued_cmd *qc); 706 int (*qc_defer) (struct ata_queued_cmd *qc);
692 void (*qc_prep) (struct ata_queued_cmd *qc); 707 void (*qc_prep) (struct ata_queued_cmd *qc);
@@ -832,8 +847,6 @@ extern int ata_busy_sleep(struct ata_port *ap,
832 unsigned long timeout_pat, unsigned long timeout); 847 unsigned long timeout_pat, unsigned long timeout);
833extern void ata_wait_after_reset(struct ata_port *ap, unsigned long deadline); 848extern void ata_wait_after_reset(struct ata_port *ap, unsigned long deadline);
834extern int ata_wait_ready(struct ata_port *ap, unsigned long deadline); 849extern int ata_wait_ready(struct ata_port *ap, unsigned long deadline);
835extern void ata_port_queue_task(struct ata_port *ap, work_func_t fn,
836 void *data, unsigned long delay);
837extern u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val, 850extern u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
838 unsigned long interval_msec, 851 unsigned long interval_msec,
839 unsigned long timeout_msec); 852 unsigned long timeout_msec);
@@ -848,6 +861,16 @@ extern void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
848extern void ata_tf_to_fis(const struct ata_taskfile *tf, 861extern void ata_tf_to_fis(const struct ata_taskfile *tf,
849 u8 pmp, int is_cmd, u8 *fis); 862 u8 pmp, int is_cmd, u8 *fis);
850extern void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf); 863extern void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf);
864extern unsigned long ata_pack_xfermask(unsigned long pio_mask,
865 unsigned long mwdma_mask, unsigned long udma_mask);
866extern void ata_unpack_xfermask(unsigned long xfer_mask,
867 unsigned long *pio_mask, unsigned long *mwdma_mask,
868 unsigned long *udma_mask);
869extern u8 ata_xfer_mask2mode(unsigned long xfer_mask);
870extern unsigned long ata_xfer_mode2mask(u8 xfer_mode);
871extern int ata_xfer_mode2shift(unsigned long xfer_mode);
872extern const char *ata_mode_string(unsigned long xfer_mask);
873extern unsigned long ata_id_xfermask(const u16 *id);
851extern void ata_noop_dev_select(struct ata_port *ap, unsigned int device); 874extern void ata_noop_dev_select(struct ata_port *ap, unsigned int device);
852extern void ata_std_dev_select(struct ata_port *ap, unsigned int device); 875extern void ata_std_dev_select(struct ata_port *ap, unsigned int device);
853extern u8 ata_check_status(struct ata_port *ap); 876extern u8 ata_check_status(struct ata_port *ap);
@@ -856,17 +879,15 @@ extern void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
856extern int ata_port_start(struct ata_port *ap); 879extern int ata_port_start(struct ata_port *ap);
857extern int ata_sff_port_start(struct ata_port *ap); 880extern int ata_sff_port_start(struct ata_port *ap);
858extern irqreturn_t ata_interrupt(int irq, void *dev_instance); 881extern irqreturn_t ata_interrupt(int irq, void *dev_instance);
859extern void ata_data_xfer(struct ata_device *adev, unsigned char *buf, 882extern unsigned int ata_data_xfer(struct ata_device *dev,
860 unsigned int buflen, int write_data); 883 unsigned char *buf, unsigned int buflen, int rw);
861extern void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf, 884extern unsigned int ata_data_xfer_noirq(struct ata_device *dev,
862 unsigned int buflen, int write_data); 885 unsigned char *buf, unsigned int buflen, int rw);
863extern int ata_std_qc_defer(struct ata_queued_cmd *qc); 886extern int ata_std_qc_defer(struct ata_queued_cmd *qc);
864extern void ata_dumb_qc_prep(struct ata_queued_cmd *qc); 887extern void ata_dumb_qc_prep(struct ata_queued_cmd *qc);
865extern void ata_qc_prep(struct ata_queued_cmd *qc); 888extern void ata_qc_prep(struct ata_queued_cmd *qc);
866extern void ata_noop_qc_prep(struct ata_queued_cmd *qc); 889extern void ata_noop_qc_prep(struct ata_queued_cmd *qc);
867extern unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc); 890extern unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc);
868extern void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf,
869 unsigned int buflen);
870extern void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg, 891extern void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
871 unsigned int n_elem); 892 unsigned int n_elem);
872extern unsigned int ata_dev_classify(const struct ata_taskfile *tf); 893extern unsigned int ata_dev_classify(const struct ata_taskfile *tf);
@@ -875,7 +896,6 @@ extern void ata_id_string(const u16 *id, unsigned char *s,
875 unsigned int ofs, unsigned int len); 896 unsigned int ofs, unsigned int len);
876extern void ata_id_c_string(const u16 *id, unsigned char *s, 897extern void ata_id_c_string(const u16 *id, unsigned char *s,
877 unsigned int ofs, unsigned int len); 898 unsigned int ofs, unsigned int len);
878extern void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown);
879extern void ata_bmdma_setup(struct ata_queued_cmd *qc); 899extern void ata_bmdma_setup(struct ata_queued_cmd *qc);
880extern void ata_bmdma_start(struct ata_queued_cmd *qc); 900extern void ata_bmdma_start(struct ata_queued_cmd *qc);
881extern void ata_bmdma_stop(struct ata_queued_cmd *qc); 901extern void ata_bmdma_stop(struct ata_queued_cmd *qc);
@@ -910,6 +930,7 @@ extern u8 ata_irq_on(struct ata_port *ap);
910extern int ata_cable_40wire(struct ata_port *ap); 930extern int ata_cable_40wire(struct ata_port *ap);
911extern int ata_cable_80wire(struct ata_port *ap); 931extern int ata_cable_80wire(struct ata_port *ap);
912extern int ata_cable_sata(struct ata_port *ap); 932extern int ata_cable_sata(struct ata_port *ap);
933extern int ata_cable_ignore(struct ata_port *ap);
913extern int ata_cable_unknown(struct ata_port *ap); 934extern int ata_cable_unknown(struct ata_port *ap);
914 935
915/* 936/*
@@ -917,11 +938,13 @@ extern int ata_cable_unknown(struct ata_port *ap);
917 */ 938 */
918 939
919extern unsigned int ata_pio_need_iordy(const struct ata_device *); 940extern unsigned int ata_pio_need_iordy(const struct ata_device *);
941extern const struct ata_timing *ata_timing_find_mode(u8 xfer_mode);
920extern int ata_timing_compute(struct ata_device *, unsigned short, 942extern int ata_timing_compute(struct ata_device *, unsigned short,
921 struct ata_timing *, int, int); 943 struct ata_timing *, int, int);
922extern void ata_timing_merge(const struct ata_timing *, 944extern void ata_timing_merge(const struct ata_timing *,
923 const struct ata_timing *, struct ata_timing *, 945 const struct ata_timing *, struct ata_timing *,
924 unsigned int); 946 unsigned int);
947extern u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle);
925 948
926enum { 949enum {
927 ATA_TIMING_SETUP = (1 << 0), 950 ATA_TIMING_SETUP = (1 << 0),
@@ -948,15 +971,40 @@ static inline const struct ata_acpi_gtm *ata_acpi_init_gtm(struct ata_port *ap)
948 return &ap->__acpi_init_gtm; 971 return &ap->__acpi_init_gtm;
949 return NULL; 972 return NULL;
950} 973}
951extern int ata_acpi_cbl_80wire(struct ata_port *ap);
952int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm); 974int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm);
953int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *stm); 975int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *stm);
976unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
977 const struct ata_acpi_gtm *gtm);
978int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm);
954#else 979#else
955static inline const struct ata_acpi_gtm *ata_acpi_init_gtm(struct ata_port *ap) 980static inline const struct ata_acpi_gtm *ata_acpi_init_gtm(struct ata_port *ap)
956{ 981{
957 return NULL; 982 return NULL;
958} 983}
959static inline int ata_acpi_cbl_80wire(struct ata_port *ap) { return 0; } 984
985static inline int ata_acpi_stm(const struct ata_port *ap,
986 struct ata_acpi_gtm *stm)
987{
988 return -ENOSYS;
989}
990
991static inline int ata_acpi_gtm(const struct ata_port *ap,
992 struct ata_acpi_gtm *stm)
993{
994 return -ENOSYS;
995}
996
997static inline unsigned int ata_acpi_gtm_xfermask(struct ata_device *dev,
998 const struct ata_acpi_gtm *gtm)
999{
1000 return 0;
1001}
1002
1003static inline int ata_acpi_cbl_80wire(struct ata_port *ap,
1004 const struct ata_acpi_gtm *gtm)
1005{
1006 return 0;
1007}
960#endif 1008#endif
961 1009
962#ifdef CONFIG_PCI 1010#ifdef CONFIG_PCI
@@ -985,8 +1033,12 @@ extern int ata_pci_init_bmdma(struct ata_host *host);
985extern int ata_pci_prepare_sff_host(struct pci_dev *pdev, 1033extern int ata_pci_prepare_sff_host(struct pci_dev *pdev,
986 const struct ata_port_info * const * ppi, 1034 const struct ata_port_info * const * ppi,
987 struct ata_host **r_host); 1035 struct ata_host **r_host);
1036extern int ata_pci_activate_sff_host(struct ata_host *host,
1037 irq_handler_t irq_handler,
1038 struct scsi_host_template *sht);
988extern int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits); 1039extern int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits);
989extern unsigned long ata_pci_default_filter(struct ata_device *, unsigned long); 1040extern unsigned long ata_pci_default_filter(struct ata_device *dev,
1041 unsigned long xfer_mask);
990#endif /* CONFIG_PCI */ 1042#endif /* CONFIG_PCI */
991 1043
992/* 1044/*
@@ -1074,35 +1126,6 @@ extern void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
1074 const char *name); 1126 const char *name);
1075#endif 1127#endif
1076 1128
1077/*
1078 * qc helpers
1079 */
1080static inline struct scatterlist *
1081ata_qc_first_sg(struct ata_queued_cmd *qc)
1082{
1083 qc->n_iter = 0;
1084 if (qc->n_elem)
1085 return qc->__sg;
1086 if (qc->pad_len)
1087 return &qc->pad_sgent;
1088 return NULL;
1089}
1090
1091static inline struct scatterlist *
1092ata_qc_next_sg(struct scatterlist *sg, struct ata_queued_cmd *qc)
1093{
1094 if (sg == &qc->pad_sgent)
1095 return NULL;
1096 if (++qc->n_iter < qc->n_elem)
1097 return sg_next(sg);
1098 if (qc->pad_len)
1099 return &qc->pad_sgent;
1100 return NULL;
1101}
1102
1103#define ata_for_each_sg(sg, qc) \
1104 for (sg = ata_qc_first_sg(qc); sg; sg = ata_qc_next_sg(sg, qc))
1105
1106static inline unsigned int ata_tag_valid(unsigned int tag) 1129static inline unsigned int ata_tag_valid(unsigned int tag)
1107{ 1130{
1108 return (tag < ATA_MAX_QUEUE) ? 1 : 0; 1131 return (tag < ATA_MAX_QUEUE) ? 1 : 0;
@@ -1337,15 +1360,17 @@ static inline void ata_tf_init(struct ata_device *dev, struct ata_taskfile *tf)
1337static inline void ata_qc_reinit(struct ata_queued_cmd *qc) 1360static inline void ata_qc_reinit(struct ata_queued_cmd *qc)
1338{ 1361{
1339 qc->dma_dir = DMA_NONE; 1362 qc->dma_dir = DMA_NONE;
1340 qc->__sg = NULL; 1363 qc->sg = NULL;
1341 qc->flags = 0; 1364 qc->flags = 0;
1342 qc->cursg = NULL; 1365 qc->cursg = NULL;
1343 qc->cursg_ofs = 0; 1366 qc->cursg_ofs = 0;
1344 qc->nbytes = qc->curbytes = 0; 1367 qc->nbytes = qc->raw_nbytes = qc->curbytes = 0;
1345 qc->n_elem = 0; 1368 qc->n_elem = 0;
1369 qc->mapped_n_elem = 0;
1346 qc->n_iter = 0; 1370 qc->n_iter = 0;
1347 qc->err_mask = 0; 1371 qc->err_mask = 0;
1348 qc->pad_len = 0; 1372 qc->pad_len = 0;
1373 qc->last_sg = NULL;
1349 qc->sect_size = ATA_SECT_SIZE; 1374 qc->sect_size = ATA_SECT_SIZE;
1350 1375
1351 ata_tf_init(qc->dev, &qc->tf); 1376 ata_tf_init(qc->dev, &qc->tf);
@@ -1362,6 +1387,27 @@ static inline int ata_try_flush_cache(const struct ata_device *dev)
1362 ata_id_has_flush_ext(dev->id); 1387 ata_id_has_flush_ext(dev->id);
1363} 1388}
1364 1389
1390static inline int atapi_cmd_type(u8 opcode)
1391{
1392 switch (opcode) {
1393 case GPCMD_READ_10:
1394 case GPCMD_READ_12:
1395 return ATAPI_READ;
1396
1397 case GPCMD_WRITE_10:
1398 case GPCMD_WRITE_12:
1399 case GPCMD_WRITE_AND_VERIFY_10:
1400 return ATAPI_WRITE;
1401
1402 case GPCMD_READ_CD:
1403 case GPCMD_READ_CD_MSF:
1404 return ATAPI_READ_CD;
1405
1406 default:
1407 return ATAPI_MISC;
1408 }
1409}
1410
1365static inline unsigned int ac_err_mask(u8 status) 1411static inline unsigned int ac_err_mask(u8 status)
1366{ 1412{
1367 if (status & (ATA_BUSY | ATA_DRQ)) 1413 if (status & (ATA_BUSY | ATA_DRQ))
diff --git a/include/linux/notifier.h b/include/linux/notifier.h
index 0c40cc0b4a36..5dfbc684ce7d 100644
--- a/include/linux/notifier.h
+++ b/include/linux/notifier.h
@@ -207,9 +207,7 @@ static inline int notifier_to_errno(int ret)
207#define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */ 207#define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
208#define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */ 208#define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
209#define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */ 209#define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
210#define CPU_LOCK_ACQUIRE 0x0008 /* Acquire all hotcpu locks */ 210#define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
211#define CPU_LOCK_RELEASE 0x0009 /* Release all hotcpu locks */
212#define CPU_DYING 0x000A /* CPU (unsigned)v not running any task,
213 * not handling interrupts, soon dead */ 211 * not handling interrupts, soon dead */
214 212
215/* Used for CPU hotplug events occuring while tasks are frozen due to a suspend 213/* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
diff --git a/include/linux/pci_ids.h b/include/linux/pci_ids.h
index 7f2215139e9a..1fbd0256e86b 100644
--- a/include/linux/pci_ids.h
+++ b/include/linux/pci_ids.h
@@ -2066,6 +2066,9 @@
2066#define PCI_VENDOR_ID_NETCELL 0x169c 2066#define PCI_VENDOR_ID_NETCELL 0x169c
2067#define PCI_DEVICE_ID_REVOLUTION 0x0044 2067#define PCI_DEVICE_ID_REVOLUTION 0x0044
2068 2068
2069#define PCI_VENDOR_ID_CENATEK 0x16CA
2070#define PCI_DEVICE_ID_CENATEK_IDE 0x0001
2071
2069#define PCI_VENDOR_ID_VITESSE 0x1725 2072#define PCI_VENDOR_ID_VITESSE 0x1725
2070#define PCI_DEVICE_ID_VITESSE_VSC7174 0x7174 2073#define PCI_DEVICE_ID_VITESSE_VSC7174 0x7174
2071 2074
diff --git a/include/linux/rcuclassic.h b/include/linux/rcuclassic.h
new file mode 100644
index 000000000000..4d6624260b4c
--- /dev/null
+++ b/include/linux/rcuclassic.h
@@ -0,0 +1,164 @@
1/*
2 * Read-Copy Update mechanism for mutual exclusion (classic version)
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; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright IBM Corporation, 2001
19 *
20 * Author: Dipankar Sarma <dipankar@in.ibm.com>
21 *
22 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
23 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
24 * Papers:
25 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
26 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
27 *
28 * For detailed explanation of Read-Copy Update mechanism see -
29 * Documentation/RCU
30 *
31 */
32
33#ifndef __LINUX_RCUCLASSIC_H
34#define __LINUX_RCUCLASSIC_H
35
36#ifdef __KERNEL__
37
38#include <linux/cache.h>
39#include <linux/spinlock.h>
40#include <linux/threads.h>
41#include <linux/percpu.h>
42#include <linux/cpumask.h>
43#include <linux/seqlock.h>
44
45
46/* Global control variables for rcupdate callback mechanism. */
47struct rcu_ctrlblk {
48 long cur; /* Current batch number. */
49 long completed; /* Number of the last completed batch */
50 int next_pending; /* Is the next batch already waiting? */
51
52 int signaled;
53
54 spinlock_t lock ____cacheline_internodealigned_in_smp;
55 cpumask_t cpumask; /* CPUs that need to switch in order */
56 /* for current batch to proceed. */
57} ____cacheline_internodealigned_in_smp;
58
59/* Is batch a before batch b ? */
60static inline int rcu_batch_before(long a, long b)
61{
62 return (a - b) < 0;
63}
64
65/* Is batch a after batch b ? */
66static inline int rcu_batch_after(long a, long b)
67{
68 return (a - b) > 0;
69}
70
71/*
72 * Per-CPU data for Read-Copy UPdate.
73 * nxtlist - new callbacks are added here
74 * curlist - current batch for which quiescent cycle started if any
75 */
76struct rcu_data {
77 /* 1) quiescent state handling : */
78 long quiescbatch; /* Batch # for grace period */
79 int passed_quiesc; /* User-mode/idle loop etc. */
80 int qs_pending; /* core waits for quiesc state */
81
82 /* 2) batch handling */
83 long batch; /* Batch # for current RCU batch */
84 struct rcu_head *nxtlist;
85 struct rcu_head **nxttail;
86 long qlen; /* # of queued callbacks */
87 struct rcu_head *curlist;
88 struct rcu_head **curtail;
89 struct rcu_head *donelist;
90 struct rcu_head **donetail;
91 long blimit; /* Upper limit on a processed batch */
92 int cpu;
93 struct rcu_head barrier;
94};
95
96DECLARE_PER_CPU(struct rcu_data, rcu_data);
97DECLARE_PER_CPU(struct rcu_data, rcu_bh_data);
98
99/*
100 * Increment the quiescent state counter.
101 * The counter is a bit degenerated: We do not need to know
102 * how many quiescent states passed, just if there was at least
103 * one since the start of the grace period. Thus just a flag.
104 */
105static inline void rcu_qsctr_inc(int cpu)
106{
107 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
108 rdp->passed_quiesc = 1;
109}
110static inline void rcu_bh_qsctr_inc(int cpu)
111{
112 struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
113 rdp->passed_quiesc = 1;
114}
115
116extern int rcu_pending(int cpu);
117extern int rcu_needs_cpu(int cpu);
118
119#ifdef CONFIG_DEBUG_LOCK_ALLOC
120extern struct lockdep_map rcu_lock_map;
121# define rcu_read_acquire() \
122 lock_acquire(&rcu_lock_map, 0, 0, 2, 1, _THIS_IP_)
123# define rcu_read_release() lock_release(&rcu_lock_map, 1, _THIS_IP_)
124#else
125# define rcu_read_acquire() do { } while (0)
126# define rcu_read_release() do { } while (0)
127#endif
128
129#define __rcu_read_lock() \
130 do { \
131 preempt_disable(); \
132 __acquire(RCU); \
133 rcu_read_acquire(); \
134 } while (0)
135#define __rcu_read_unlock() \
136 do { \
137 rcu_read_release(); \
138 __release(RCU); \
139 preempt_enable(); \
140 } while (0)
141#define __rcu_read_lock_bh() \
142 do { \
143 local_bh_disable(); \
144 __acquire(RCU_BH); \
145 rcu_read_acquire(); \
146 } while (0)
147#define __rcu_read_unlock_bh() \
148 do { \
149 rcu_read_release(); \
150 __release(RCU_BH); \
151 local_bh_enable(); \
152 } while (0)
153
154#define __synchronize_sched() synchronize_rcu()
155
156extern void __rcu_init(void);
157extern void rcu_check_callbacks(int cpu, int user);
158extern void rcu_restart_cpu(int cpu);
159
160extern long rcu_batches_completed(void);
161extern long rcu_batches_completed_bh(void);
162
163#endif /* __KERNEL__ */
164#endif /* __LINUX_RCUCLASSIC_H */
diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
index cc24a01df940..d32c14de270e 100644
--- a/include/linux/rcupdate.h
+++ b/include/linux/rcupdate.h
@@ -15,7 +15,7 @@
15 * along with this program; if not, write to the Free Software 15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * 17 *
18 * Copyright (C) IBM Corporation, 2001 18 * Copyright IBM Corporation, 2001
19 * 19 *
20 * Author: Dipankar Sarma <dipankar@in.ibm.com> 20 * Author: Dipankar Sarma <dipankar@in.ibm.com>
21 * 21 *
@@ -53,96 +53,18 @@ struct rcu_head {
53 void (*func)(struct rcu_head *head); 53 void (*func)(struct rcu_head *head);
54}; 54};
55 55
56#ifdef CONFIG_CLASSIC_RCU
57#include <linux/rcuclassic.h>
58#else /* #ifdef CONFIG_CLASSIC_RCU */
59#include <linux/rcupreempt.h>
60#endif /* #else #ifdef CONFIG_CLASSIC_RCU */
61
56#define RCU_HEAD_INIT { .next = NULL, .func = NULL } 62#define RCU_HEAD_INIT { .next = NULL, .func = NULL }
57#define RCU_HEAD(head) struct rcu_head head = RCU_HEAD_INIT 63#define RCU_HEAD(head) struct rcu_head head = RCU_HEAD_INIT
58#define INIT_RCU_HEAD(ptr) do { \ 64#define INIT_RCU_HEAD(ptr) do { \
59 (ptr)->next = NULL; (ptr)->func = NULL; \ 65 (ptr)->next = NULL; (ptr)->func = NULL; \
60} while (0) 66} while (0)
61 67
62
63
64/* Global control variables for rcupdate callback mechanism. */
65struct rcu_ctrlblk {
66 long cur; /* Current batch number. */
67 long completed; /* Number of the last completed batch */
68 int next_pending; /* Is the next batch already waiting? */
69
70 int signaled;
71
72 spinlock_t lock ____cacheline_internodealigned_in_smp;
73 cpumask_t cpumask; /* CPUs that need to switch in order */
74 /* for current batch to proceed. */
75} ____cacheline_internodealigned_in_smp;
76
77/* Is batch a before batch b ? */
78static inline int rcu_batch_before(long a, long b)
79{
80 return (a - b) < 0;
81}
82
83/* Is batch a after batch b ? */
84static inline int rcu_batch_after(long a, long b)
85{
86 return (a - b) > 0;
87}
88
89/*
90 * Per-CPU data for Read-Copy UPdate.
91 * nxtlist - new callbacks are added here
92 * curlist - current batch for which quiescent cycle started if any
93 */
94struct rcu_data {
95 /* 1) quiescent state handling : */
96 long quiescbatch; /* Batch # for grace period */
97 int passed_quiesc; /* User-mode/idle loop etc. */
98 int qs_pending; /* core waits for quiesc state */
99
100 /* 2) batch handling */
101 long batch; /* Batch # for current RCU batch */
102 struct rcu_head *nxtlist;
103 struct rcu_head **nxttail;
104 long qlen; /* # of queued callbacks */
105 struct rcu_head *curlist;
106 struct rcu_head **curtail;
107 struct rcu_head *donelist;
108 struct rcu_head **donetail;
109 long blimit; /* Upper limit on a processed batch */
110 int cpu;
111 struct rcu_head barrier;
112};
113
114DECLARE_PER_CPU(struct rcu_data, rcu_data);
115DECLARE_PER_CPU(struct rcu_data, rcu_bh_data);
116
117/*
118 * Increment the quiescent state counter.
119 * The counter is a bit degenerated: We do not need to know
120 * how many quiescent states passed, just if there was at least
121 * one since the start of the grace period. Thus just a flag.
122 */
123static inline void rcu_qsctr_inc(int cpu)
124{
125 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
126 rdp->passed_quiesc = 1;
127}
128static inline void rcu_bh_qsctr_inc(int cpu)
129{
130 struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
131 rdp->passed_quiesc = 1;
132}
133
134extern int rcu_pending(int cpu);
135extern int rcu_needs_cpu(int cpu);
136
137#ifdef CONFIG_DEBUG_LOCK_ALLOC
138extern struct lockdep_map rcu_lock_map;
139# define rcu_read_acquire() lock_acquire(&rcu_lock_map, 0, 0, 2, 1, _THIS_IP_)
140# define rcu_read_release() lock_release(&rcu_lock_map, 1, _THIS_IP_)
141#else
142# define rcu_read_acquire() do { } while (0)
143# define rcu_read_release() do { } while (0)
144#endif
145
146/** 68/**
147 * rcu_read_lock - mark the beginning of an RCU read-side critical section. 69 * rcu_read_lock - mark the beginning of an RCU read-side critical section.
148 * 70 *
@@ -172,24 +94,13 @@ extern struct lockdep_map rcu_lock_map;
172 * 94 *
173 * It is illegal to block while in an RCU read-side critical section. 95 * It is illegal to block while in an RCU read-side critical section.
174 */ 96 */
175#define rcu_read_lock() \ 97#define rcu_read_lock() __rcu_read_lock()
176 do { \
177 preempt_disable(); \
178 __acquire(RCU); \
179 rcu_read_acquire(); \
180 } while(0)
181 98
182/** 99/**
183 * rcu_read_unlock - marks the end of an RCU read-side critical section. 100 * rcu_read_unlock - marks the end of an RCU read-side critical section.
184 * 101 *
185 * See rcu_read_lock() for more information. 102 * See rcu_read_lock() for more information.
186 */ 103 */
187#define rcu_read_unlock() \
188 do { \
189 rcu_read_release(); \
190 __release(RCU); \
191 preempt_enable(); \
192 } while(0)
193 104
194/* 105/*
195 * So where is rcu_write_lock()? It does not exist, as there is no 106 * So where is rcu_write_lock()? It does not exist, as there is no
@@ -200,6 +111,7 @@ extern struct lockdep_map rcu_lock_map;
200 * used as well. RCU does not care how the writers keep out of each 111 * used as well. RCU does not care how the writers keep out of each
201 * others' way, as long as they do so. 112 * others' way, as long as they do so.
202 */ 113 */
114#define rcu_read_unlock() __rcu_read_unlock()
203 115
204/** 116/**
205 * rcu_read_lock_bh - mark the beginning of a softirq-only RCU critical section 117 * rcu_read_lock_bh - mark the beginning of a softirq-only RCU critical section
@@ -212,24 +124,14 @@ extern struct lockdep_map rcu_lock_map;
212 * can use just rcu_read_lock(). 124 * can use just rcu_read_lock().
213 * 125 *
214 */ 126 */
215#define rcu_read_lock_bh() \ 127#define rcu_read_lock_bh() __rcu_read_lock_bh()
216 do { \
217 local_bh_disable(); \
218 __acquire(RCU_BH); \
219 rcu_read_acquire(); \
220 } while(0)
221 128
222/* 129/*
223 * rcu_read_unlock_bh - marks the end of a softirq-only RCU critical section 130 * rcu_read_unlock_bh - marks the end of a softirq-only RCU critical section
224 * 131 *
225 * See rcu_read_lock_bh() for more information. 132 * See rcu_read_lock_bh() for more information.
226 */ 133 */
227#define rcu_read_unlock_bh() \ 134#define rcu_read_unlock_bh() __rcu_read_unlock_bh()
228 do { \
229 rcu_read_release(); \
230 __release(RCU_BH); \
231 local_bh_enable(); \
232 } while(0)
233 135
234/* 136/*
235 * Prevent the compiler from merging or refetching accesses. The compiler 137 * Prevent the compiler from merging or refetching accesses. The compiler
@@ -293,21 +195,52 @@ extern struct lockdep_map rcu_lock_map;
293 * In "classic RCU", these two guarantees happen to be one and 195 * In "classic RCU", these two guarantees happen to be one and
294 * the same, but can differ in realtime RCU implementations. 196 * the same, but can differ in realtime RCU implementations.
295 */ 197 */
296#define synchronize_sched() synchronize_rcu() 198#define synchronize_sched() __synchronize_sched()
297 199
298extern void rcu_init(void); 200/**
299extern void rcu_check_callbacks(int cpu, int user); 201 * call_rcu - Queue an RCU callback for invocation after a grace period.
300extern void rcu_restart_cpu(int cpu); 202 * @head: structure to be used for queueing the RCU updates.
301extern long rcu_batches_completed(void); 203 * @func: actual update function to be invoked after the grace period
302extern long rcu_batches_completed_bh(void); 204 *
205 * The update function will be invoked some time after a full grace
206 * period elapses, in other words after all currently executing RCU
207 * read-side critical sections have completed. RCU read-side critical
208 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
209 * and may be nested.
210 */
211extern void call_rcu(struct rcu_head *head,
212 void (*func)(struct rcu_head *head));
303 213
304/* Exported interfaces */ 214/**
305extern void FASTCALL(call_rcu(struct rcu_head *head, 215 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
306 void (*func)(struct rcu_head *head))); 216 * @head: structure to be used for queueing the RCU updates.
307extern void FASTCALL(call_rcu_bh(struct rcu_head *head, 217 * @func: actual update function to be invoked after the grace period
308 void (*func)(struct rcu_head *head))); 218 *
219 * The update function will be invoked some time after a full grace
220 * period elapses, in other words after all currently executing RCU
221 * read-side critical sections have completed. call_rcu_bh() assumes
222 * that the read-side critical sections end on completion of a softirq
223 * handler. This means that read-side critical sections in process
224 * context must not be interrupted by softirqs. This interface is to be
225 * used when most of the read-side critical sections are in softirq context.
226 * RCU read-side critical sections are delimited by :
227 * - rcu_read_lock() and rcu_read_unlock(), if in interrupt context.
228 * OR
229 * - rcu_read_lock_bh() and rcu_read_unlock_bh(), if in process context.
230 * These may be nested.
231 */
232extern void call_rcu_bh(struct rcu_head *head,
233 void (*func)(struct rcu_head *head));
234
235/* Exported common interfaces */
309extern void synchronize_rcu(void); 236extern void synchronize_rcu(void);
310extern void rcu_barrier(void); 237extern void rcu_barrier(void);
238extern long rcu_batches_completed(void);
239extern long rcu_batches_completed_bh(void);
240
241/* Internal to kernel */
242extern void rcu_init(void);
243extern int rcu_needs_cpu(int cpu);
311 244
312#endif /* __KERNEL__ */ 245#endif /* __KERNEL__ */
313#endif /* __LINUX_RCUPDATE_H */ 246#endif /* __LINUX_RCUPDATE_H */
diff --git a/include/linux/rcupreempt.h b/include/linux/rcupreempt.h
new file mode 100644
index 000000000000..ece8eb3e4151
--- /dev/null
+++ b/include/linux/rcupreempt.h
@@ -0,0 +1,86 @@
1/*
2 * Read-Copy Update mechanism for mutual exclusion (RT implementation)
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; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) IBM Corporation, 2006
19 *
20 * Author: Paul McKenney <paulmck@us.ibm.com>
21 *
22 * Based on the original work by Paul McKenney <paul.mckenney@us.ibm.com>
23 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
24 * Papers:
25 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
26 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
27 *
28 * For detailed explanation of Read-Copy Update mechanism see -
29 * Documentation/RCU
30 *
31 */
32
33#ifndef __LINUX_RCUPREEMPT_H
34#define __LINUX_RCUPREEMPT_H
35
36#ifdef __KERNEL__
37
38#include <linux/cache.h>
39#include <linux/spinlock.h>
40#include <linux/threads.h>
41#include <linux/percpu.h>
42#include <linux/cpumask.h>
43#include <linux/seqlock.h>
44
45#define rcu_qsctr_inc(cpu)
46#define rcu_bh_qsctr_inc(cpu)
47#define call_rcu_bh(head, rcu) call_rcu(head, rcu)
48
49extern void __rcu_read_lock(void);
50extern void __rcu_read_unlock(void);
51extern int rcu_pending(int cpu);
52extern int rcu_needs_cpu(int cpu);
53
54#define __rcu_read_lock_bh() { rcu_read_lock(); local_bh_disable(); }
55#define __rcu_read_unlock_bh() { local_bh_enable(); rcu_read_unlock(); }
56
57extern void __synchronize_sched(void);
58
59extern void __rcu_init(void);
60extern void rcu_check_callbacks(int cpu, int user);
61extern void rcu_restart_cpu(int cpu);
62extern long rcu_batches_completed(void);
63
64/*
65 * Return the number of RCU batches processed thus far. Useful for debug
66 * and statistic. The _bh variant is identifcal to straight RCU
67 */
68static inline long rcu_batches_completed_bh(void)
69{
70 return rcu_batches_completed();
71}
72
73#ifdef CONFIG_RCU_TRACE
74struct rcupreempt_trace;
75extern long *rcupreempt_flipctr(int cpu);
76extern long rcupreempt_data_completed(void);
77extern int rcupreempt_flip_flag(int cpu);
78extern int rcupreempt_mb_flag(int cpu);
79extern char *rcupreempt_try_flip_state_name(void);
80extern struct rcupreempt_trace *rcupreempt_trace_cpu(int cpu);
81#endif
82
83struct softirq_action;
84
85#endif /* __KERNEL__ */
86#endif /* __LINUX_RCUPREEMPT_H */
diff --git a/include/linux/rcupreempt_trace.h b/include/linux/rcupreempt_trace.h
new file mode 100644
index 000000000000..21cd6b2a5c42
--- /dev/null
+++ b/include/linux/rcupreempt_trace.h
@@ -0,0 +1,99 @@
1/*
2 * Read-Copy Update mechanism for mutual exclusion (RT implementation)
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; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) IBM Corporation, 2006
19 *
20 * Author: Paul McKenney <paulmck@us.ibm.com>
21 *
22 * Based on the original work by Paul McKenney <paul.mckenney@us.ibm.com>
23 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
24 * Papers:
25 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
26 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
27 *
28 * For detailed explanation of the Preemptible Read-Copy Update mechanism see -
29 * http://lwn.net/Articles/253651/
30 */
31
32#ifndef __LINUX_RCUPREEMPT_TRACE_H
33#define __LINUX_RCUPREEMPT_TRACE_H
34
35#ifdef __KERNEL__
36#include <linux/types.h>
37#include <linux/kernel.h>
38
39#include <asm/atomic.h>
40
41/*
42 * PREEMPT_RCU data structures.
43 */
44
45struct rcupreempt_trace {
46 long next_length;
47 long next_add;
48 long wait_length;
49 long wait_add;
50 long done_length;
51 long done_add;
52 long done_remove;
53 atomic_t done_invoked;
54 long rcu_check_callbacks;
55 atomic_t rcu_try_flip_1;
56 atomic_t rcu_try_flip_e1;
57 long rcu_try_flip_i1;
58 long rcu_try_flip_ie1;
59 long rcu_try_flip_g1;
60 long rcu_try_flip_a1;
61 long rcu_try_flip_ae1;
62 long rcu_try_flip_a2;
63 long rcu_try_flip_z1;
64 long rcu_try_flip_ze1;
65 long rcu_try_flip_z2;
66 long rcu_try_flip_m1;
67 long rcu_try_flip_me1;
68 long rcu_try_flip_m2;
69};
70
71#ifdef CONFIG_RCU_TRACE
72#define RCU_TRACE(fn, arg) fn(arg);
73#else
74#define RCU_TRACE(fn, arg)
75#endif
76
77extern void rcupreempt_trace_move2done(struct rcupreempt_trace *trace);
78extern void rcupreempt_trace_move2wait(struct rcupreempt_trace *trace);
79extern void rcupreempt_trace_try_flip_1(struct rcupreempt_trace *trace);
80extern void rcupreempt_trace_try_flip_e1(struct rcupreempt_trace *trace);
81extern void rcupreempt_trace_try_flip_i1(struct rcupreempt_trace *trace);
82extern void rcupreempt_trace_try_flip_ie1(struct rcupreempt_trace *trace);
83extern void rcupreempt_trace_try_flip_g1(struct rcupreempt_trace *trace);
84extern void rcupreempt_trace_try_flip_a1(struct rcupreempt_trace *trace);
85extern void rcupreempt_trace_try_flip_ae1(struct rcupreempt_trace *trace);
86extern void rcupreempt_trace_try_flip_a2(struct rcupreempt_trace *trace);
87extern void rcupreempt_trace_try_flip_z1(struct rcupreempt_trace *trace);
88extern void rcupreempt_trace_try_flip_ze1(struct rcupreempt_trace *trace);
89extern void rcupreempt_trace_try_flip_z2(struct rcupreempt_trace *trace);
90extern void rcupreempt_trace_try_flip_m1(struct rcupreempt_trace *trace);
91extern void rcupreempt_trace_try_flip_me1(struct rcupreempt_trace *trace);
92extern void rcupreempt_trace_try_flip_m2(struct rcupreempt_trace *trace);
93extern void rcupreempt_trace_check_callbacks(struct rcupreempt_trace *trace);
94extern void rcupreempt_trace_done_remove(struct rcupreempt_trace *trace);
95extern void rcupreempt_trace_invoke(struct rcupreempt_trace *trace);
96extern void rcupreempt_trace_next_add(struct rcupreempt_trace *trace);
97
98#endif /* __KERNEL__ */
99#endif /* __LINUX_RCUPREEMPT_TRACE_H */
diff --git a/include/linux/sched.h b/include/linux/sched.h
index d6eacda765ca..df5b24ee80b3 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -78,7 +78,6 @@ struct sched_param {
78#include <linux/proportions.h> 78#include <linux/proportions.h>
79#include <linux/seccomp.h> 79#include <linux/seccomp.h>
80#include <linux/rcupdate.h> 80#include <linux/rcupdate.h>
81#include <linux/futex.h>
82#include <linux/rtmutex.h> 81#include <linux/rtmutex.h>
83 82
84#include <linux/time.h> 83#include <linux/time.h>
@@ -88,11 +87,13 @@ struct sched_param {
88#include <linux/hrtimer.h> 87#include <linux/hrtimer.h>
89#include <linux/task_io_accounting.h> 88#include <linux/task_io_accounting.h>
90#include <linux/kobject.h> 89#include <linux/kobject.h>
90#include <linux/latencytop.h>
91 91
92#include <asm/processor.h> 92#include <asm/processor.h>
93 93
94struct exec_domain; 94struct exec_domain;
95struct futex_pi_state; 95struct futex_pi_state;
96struct robust_list_head;
96struct bio; 97struct bio;
97 98
98/* 99/*
@@ -230,6 +231,8 @@ static inline int select_nohz_load_balancer(int cpu)
230} 231}
231#endif 232#endif
232 233
234extern unsigned long rt_needs_cpu(int cpu);
235
233/* 236/*
234 * Only dump TASK_* tasks. (0 for all tasks) 237 * Only dump TASK_* tasks. (0 for all tasks)
235 */ 238 */
@@ -257,13 +260,19 @@ extern void trap_init(void);
257extern void account_process_tick(struct task_struct *task, int user); 260extern void account_process_tick(struct task_struct *task, int user);
258extern void update_process_times(int user); 261extern void update_process_times(int user);
259extern void scheduler_tick(void); 262extern void scheduler_tick(void);
263extern void hrtick_resched(void);
264
265extern void sched_show_task(struct task_struct *p);
260 266
261#ifdef CONFIG_DETECT_SOFTLOCKUP 267#ifdef CONFIG_DETECT_SOFTLOCKUP
262extern void softlockup_tick(void); 268extern void softlockup_tick(void);
263extern void spawn_softlockup_task(void); 269extern void spawn_softlockup_task(void);
264extern void touch_softlockup_watchdog(void); 270extern void touch_softlockup_watchdog(void);
265extern void touch_all_softlockup_watchdogs(void); 271extern void touch_all_softlockup_watchdogs(void);
266extern int softlockup_thresh; 272extern unsigned long softlockup_thresh;
273extern unsigned long sysctl_hung_task_check_count;
274extern unsigned long sysctl_hung_task_timeout_secs;
275extern unsigned long sysctl_hung_task_warnings;
267#else 276#else
268static inline void softlockup_tick(void) 277static inline void softlockup_tick(void)
269{ 278{
@@ -822,6 +831,7 @@ struct sched_class {
822 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int wakeup); 831 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int wakeup);
823 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int sleep); 832 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int sleep);
824 void (*yield_task) (struct rq *rq); 833 void (*yield_task) (struct rq *rq);
834 int (*select_task_rq)(struct task_struct *p, int sync);
825 835
826 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p); 836 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p);
827 837
@@ -837,11 +847,25 @@ struct sched_class {
837 int (*move_one_task) (struct rq *this_rq, int this_cpu, 847 int (*move_one_task) (struct rq *this_rq, int this_cpu,
838 struct rq *busiest, struct sched_domain *sd, 848 struct rq *busiest, struct sched_domain *sd,
839 enum cpu_idle_type idle); 849 enum cpu_idle_type idle);
850 void (*pre_schedule) (struct rq *this_rq, struct task_struct *task);
851 void (*post_schedule) (struct rq *this_rq);
852 void (*task_wake_up) (struct rq *this_rq, struct task_struct *task);
840#endif 853#endif
841 854
842 void (*set_curr_task) (struct rq *rq); 855 void (*set_curr_task) (struct rq *rq);
843 void (*task_tick) (struct rq *rq, struct task_struct *p); 856 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
844 void (*task_new) (struct rq *rq, struct task_struct *p); 857 void (*task_new) (struct rq *rq, struct task_struct *p);
858 void (*set_cpus_allowed)(struct task_struct *p, cpumask_t *newmask);
859
860 void (*join_domain)(struct rq *rq);
861 void (*leave_domain)(struct rq *rq);
862
863 void (*switched_from) (struct rq *this_rq, struct task_struct *task,
864 int running);
865 void (*switched_to) (struct rq *this_rq, struct task_struct *task,
866 int running);
867 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
868 int oldprio, int running);
845}; 869};
846 870
847struct load_weight { 871struct load_weight {
@@ -871,6 +895,8 @@ struct sched_entity {
871#ifdef CONFIG_SCHEDSTATS 895#ifdef CONFIG_SCHEDSTATS
872 u64 wait_start; 896 u64 wait_start;
873 u64 wait_max; 897 u64 wait_max;
898 u64 wait_count;
899 u64 wait_sum;
874 900
875 u64 sleep_start; 901 u64 sleep_start;
876 u64 sleep_max; 902 u64 sleep_max;
@@ -909,6 +935,21 @@ struct sched_entity {
909#endif 935#endif
910}; 936};
911 937
938struct sched_rt_entity {
939 struct list_head run_list;
940 unsigned int time_slice;
941 unsigned long timeout;
942 int nr_cpus_allowed;
943
944#ifdef CONFIG_FAIR_GROUP_SCHED
945 struct sched_rt_entity *parent;
946 /* rq on which this entity is (to be) queued: */
947 struct rt_rq *rt_rq;
948 /* rq "owned" by this entity/group: */
949 struct rt_rq *my_q;
950#endif
951};
952
912struct task_struct { 953struct task_struct {
913 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ 954 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
914 void *stack; 955 void *stack;
@@ -925,9 +966,9 @@ struct task_struct {
925#endif 966#endif
926 967
927 int prio, static_prio, normal_prio; 968 int prio, static_prio, normal_prio;
928 struct list_head run_list;
929 const struct sched_class *sched_class; 969 const struct sched_class *sched_class;
930 struct sched_entity se; 970 struct sched_entity se;
971 struct sched_rt_entity rt;
931 972
932#ifdef CONFIG_PREEMPT_NOTIFIERS 973#ifdef CONFIG_PREEMPT_NOTIFIERS
933 /* list of struct preempt_notifier: */ 974 /* list of struct preempt_notifier: */
@@ -951,7 +992,11 @@ struct task_struct {
951 992
952 unsigned int policy; 993 unsigned int policy;
953 cpumask_t cpus_allowed; 994 cpumask_t cpus_allowed;
954 unsigned int time_slice; 995
996#ifdef CONFIG_PREEMPT_RCU
997 int rcu_read_lock_nesting;
998 int rcu_flipctr_idx;
999#endif /* #ifdef CONFIG_PREEMPT_RCU */
955 1000
956#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) 1001#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
957 struct sched_info sched_info; 1002 struct sched_info sched_info;
@@ -1041,6 +1086,11 @@ struct task_struct {
1041/* ipc stuff */ 1086/* ipc stuff */
1042 struct sysv_sem sysvsem; 1087 struct sysv_sem sysvsem;
1043#endif 1088#endif
1089#ifdef CONFIG_DETECT_SOFTLOCKUP
1090/* hung task detection */
1091 unsigned long last_switch_timestamp;
1092 unsigned long last_switch_count;
1093#endif
1044/* CPU-specific state of this task */ 1094/* CPU-specific state of this task */
1045 struct thread_struct thread; 1095 struct thread_struct thread;
1046/* filesystem information */ 1096/* filesystem information */
@@ -1173,6 +1223,10 @@ struct task_struct {
1173 int make_it_fail; 1223 int make_it_fail;
1174#endif 1224#endif
1175 struct prop_local_single dirties; 1225 struct prop_local_single dirties;
1226#ifdef CONFIG_LATENCYTOP
1227 int latency_record_count;
1228 struct latency_record latency_record[LT_SAVECOUNT];
1229#endif
1176}; 1230};
1177 1231
1178/* 1232/*
@@ -1453,6 +1507,12 @@ extern unsigned int sysctl_sched_child_runs_first;
1453extern unsigned int sysctl_sched_features; 1507extern unsigned int sysctl_sched_features;
1454extern unsigned int sysctl_sched_migration_cost; 1508extern unsigned int sysctl_sched_migration_cost;
1455extern unsigned int sysctl_sched_nr_migrate; 1509extern unsigned int sysctl_sched_nr_migrate;
1510extern unsigned int sysctl_sched_rt_period;
1511extern unsigned int sysctl_sched_rt_ratio;
1512#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
1513extern unsigned int sysctl_sched_min_bal_int_shares;
1514extern unsigned int sysctl_sched_max_bal_int_shares;
1515#endif
1456 1516
1457int sched_nr_latency_handler(struct ctl_table *table, int write, 1517int sched_nr_latency_handler(struct ctl_table *table, int write,
1458 struct file *file, void __user *buffer, size_t *length, 1518 struct file *file, void __user *buffer, size_t *length,
@@ -1845,7 +1905,18 @@ static inline int need_resched(void)
1845 * cond_resched_lock() will drop the spinlock before scheduling, 1905 * cond_resched_lock() will drop the spinlock before scheduling,
1846 * cond_resched_softirq() will enable bhs before scheduling. 1906 * cond_resched_softirq() will enable bhs before scheduling.
1847 */ 1907 */
1848extern int cond_resched(void); 1908#ifdef CONFIG_PREEMPT
1909static inline int cond_resched(void)
1910{
1911 return 0;
1912}
1913#else
1914extern int _cond_resched(void);
1915static inline int cond_resched(void)
1916{
1917 return _cond_resched();
1918}
1919#endif
1849extern int cond_resched_lock(spinlock_t * lock); 1920extern int cond_resched_lock(spinlock_t * lock);
1850extern int cond_resched_softirq(void); 1921extern int cond_resched_softirq(void);
1851 1922
diff --git a/include/linux/smp_lock.h b/include/linux/smp_lock.h
index 58962c51dee1..aab3a4cff4e1 100644
--- a/include/linux/smp_lock.h
+++ b/include/linux/smp_lock.h
@@ -17,22 +17,10 @@ extern void __lockfunc __release_kernel_lock(void);
17 __release_kernel_lock(); \ 17 __release_kernel_lock(); \
18} while (0) 18} while (0)
19 19
20/*
21 * Non-SMP kernels will never block on the kernel lock,
22 * so we are better off returning a constant zero from
23 * reacquire_kernel_lock() so that the compiler can see
24 * it at compile-time.
25 */
26#if defined(CONFIG_SMP) && !defined(CONFIG_PREEMPT_BKL)
27# define return_value_on_smp return
28#else
29# define return_value_on_smp
30#endif
31
32static inline int reacquire_kernel_lock(struct task_struct *task) 20static inline int reacquire_kernel_lock(struct task_struct *task)
33{ 21{
34 if (unlikely(task->lock_depth >= 0)) 22 if (unlikely(task->lock_depth >= 0))
35 return_value_on_smp __reacquire_kernel_lock(); 23 return __reacquire_kernel_lock();
36 return 0; 24 return 0;
37} 25}
38 26
diff --git a/include/linux/stacktrace.h b/include/linux/stacktrace.h
index e7fa657d0c49..5da9794b2d78 100644
--- a/include/linux/stacktrace.h
+++ b/include/linux/stacktrace.h
@@ -9,10 +9,13 @@ struct stack_trace {
9}; 9};
10 10
11extern void save_stack_trace(struct stack_trace *trace); 11extern void save_stack_trace(struct stack_trace *trace);
12extern void save_stack_trace_tsk(struct task_struct *tsk,
13 struct stack_trace *trace);
12 14
13extern void print_stack_trace(struct stack_trace *trace, int spaces); 15extern void print_stack_trace(struct stack_trace *trace, int spaces);
14#else 16#else
15# define save_stack_trace(trace) do { } while (0) 17# define save_stack_trace(trace) do { } while (0)
18# define save_stack_trace_tsk(tsk, trace) do { } while (0)
16# define print_stack_trace(trace, spaces) do { } while (0) 19# define print_stack_trace(trace, spaces) do { } while (0)
17#endif 20#endif
18 21
diff --git a/include/linux/topology.h b/include/linux/topology.h
index 47729f18bfdf..2352f46160d3 100644
--- a/include/linux/topology.h
+++ b/include/linux/topology.h
@@ -5,7 +5,7 @@
5 * 5 *
6 * Copyright (C) 2002, IBM Corp. 6 * Copyright (C) 2002, IBM Corp.
7 * 7 *
8 * All rights reserved. 8 * All rights reserved.
9 * 9 *
10 * This program is free software; you can redistribute it and/or modify 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 11 * it under the terms of the GNU General Public License as published by
@@ -103,6 +103,7 @@
103 .forkexec_idx = 0, \ 103 .forkexec_idx = 0, \
104 .flags = SD_LOAD_BALANCE \ 104 .flags = SD_LOAD_BALANCE \
105 | SD_BALANCE_NEWIDLE \ 105 | SD_BALANCE_NEWIDLE \
106 | SD_BALANCE_FORK \
106 | SD_BALANCE_EXEC \ 107 | SD_BALANCE_EXEC \
107 | SD_WAKE_AFFINE \ 108 | SD_WAKE_AFFINE \
108 | SD_WAKE_IDLE \ 109 | SD_WAKE_IDLE \
@@ -134,6 +135,7 @@
134 .forkexec_idx = 1, \ 135 .forkexec_idx = 1, \
135 .flags = SD_LOAD_BALANCE \ 136 .flags = SD_LOAD_BALANCE \
136 | SD_BALANCE_NEWIDLE \ 137 | SD_BALANCE_NEWIDLE \
138 | SD_BALANCE_FORK \
137 | SD_BALANCE_EXEC \ 139 | SD_BALANCE_EXEC \
138 | SD_WAKE_AFFINE \ 140 | SD_WAKE_AFFINE \
139 | SD_WAKE_IDLE \ 141 | SD_WAKE_IDLE \
@@ -165,6 +167,7 @@
165 .forkexec_idx = 1, \ 167 .forkexec_idx = 1, \
166 .flags = SD_LOAD_BALANCE \ 168 .flags = SD_LOAD_BALANCE \
167 | SD_BALANCE_NEWIDLE \ 169 | SD_BALANCE_NEWIDLE \
170 | SD_BALANCE_FORK \
168 | SD_BALANCE_EXEC \ 171 | SD_BALANCE_EXEC \
169 | SD_WAKE_AFFINE \ 172 | SD_WAKE_AFFINE \
170 | BALANCE_FOR_PKG_POWER,\ 173 | BALANCE_FOR_PKG_POWER,\
diff --git a/include/media/cs5345.h b/include/media/cs5345.h
new file mode 100644
index 000000000000..6ccae24e65ed
--- /dev/null
+++ b/include/media/cs5345.h
@@ -0,0 +1,39 @@
1/*
2 cs5345.h - definition for cs5345 inputs and outputs
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#ifndef _CS5345_H_
22#define _CS5345_H_
23
24/* CS5345 HW inputs */
25#define CS5345_IN_MIC 0
26#define CS5345_IN_1 1
27#define CS5345_IN_2 2
28#define CS5345_IN_3 3
29#define CS5345_IN_4 4
30#define CS5345_IN_5 5
31#define CS5345_IN_6 6
32
33#define CS5345_MCLK_1 0x00
34#define CS5345_MCLK_1_5 0x10
35#define CS5345_MCLK_2 0x20
36#define CS5345_MCLK_3 0x30
37#define CS5345_MCLK_4 0x40
38
39#endif
diff --git a/include/media/cx2341x.h b/include/media/cx2341x.h
index af8071d7620d..5f4608e88476 100644
--- a/include/media/cx2341x.h
+++ b/include/media/cx2341x.h
@@ -83,7 +83,7 @@ struct cx2341x_mpeg_params {
83#define CX2341X_MBOX_MAX_DATA 16 83#define CX2341X_MBOX_MAX_DATA 16
84 84
85extern const u32 cx2341x_mpeg_ctrls[]; 85extern const u32 cx2341x_mpeg_ctrls[];
86typedef int (*cx2341x_mbox_func)(void *priv, int cmd, int in, int out, 86typedef int (*cx2341x_mbox_func)(void *priv, u32 cmd, int in, int out,
87 u32 data[CX2341X_MBOX_MAX_DATA]); 87 u32 data[CX2341X_MBOX_MAX_DATA]);
88int cx2341x_update(void *priv, cx2341x_mbox_func func, 88int cx2341x_update(void *priv, cx2341x_mbox_func func,
89 const struct cx2341x_mpeg_params *old, 89 const struct cx2341x_mpeg_params *old,
diff --git a/include/media/cx25840.h b/include/media/cx25840.h
index 8e7e52d659a0..cd599ad29fb2 100644
--- a/include/media/cx25840.h
+++ b/include/media/cx25840.h
@@ -49,6 +49,25 @@ enum cx25840_video_input {
49 CX25840_SVIDEO2 = 0x620, 49 CX25840_SVIDEO2 = 0x620,
50 CX25840_SVIDEO3 = 0x730, 50 CX25840_SVIDEO3 = 0x730,
51 CX25840_SVIDEO4 = 0x840, 51 CX25840_SVIDEO4 = 0x840,
52
53 /* Allow frames to specify specific input configurations */
54 CX25840_VIN1_CH1 = 0x80000000,
55 CX25840_VIN2_CH1 = 0x80000001,
56 CX25840_VIN3_CH1 = 0x80000002,
57 CX25840_VIN4_CH1 = 0x80000003,
58 CX25840_VIN5_CH1 = 0x80000004,
59 CX25840_VIN6_CH1 = 0x80000005,
60 CX25840_VIN7_CH1 = 0x80000006,
61 CX25840_VIN8_CH1 = 0x80000007,
62 CX25840_VIN4_CH2 = 0x80000000,
63 CX25840_VIN5_CH2 = 0x80000010,
64 CX25840_VIN6_CH2 = 0x80000020,
65 CX25840_NONE_CH2 = 0x80000030,
66 CX25840_VIN7_CH3 = 0x80000000,
67 CX25840_VIN8_CH3 = 0x80000040,
68 CX25840_NONE0_CH3 = 0x80000080,
69 CX25840_NONE1_CH3 = 0x800000c0,
70 CX25840_SVIDEO_ON = 0x80000100,
52}; 71};
53 72
54enum cx25840_audio_input { 73enum cx25840_audio_input {
diff --git a/include/media/ir-common.h b/include/media/ir-common.h
index 7a785fa77212..831547d79683 100644
--- a/include/media/ir-common.h
+++ b/include/media/ir-common.h
@@ -97,7 +97,6 @@ int ir_dump_samples(u32 *samples, int count);
97int ir_decode_biphase(u32 *samples, int count, int low, int high); 97int ir_decode_biphase(u32 *samples, int count, int low, int high);
98int ir_decode_pulsedistance(u32 *samples, int count, int low, int high); 98int ir_decode_pulsedistance(u32 *samples, int count, int low, int high);
99 99
100u32 ir_rc5_decode(unsigned int code);
101void ir_rc5_timer_end(unsigned long data); 100void ir_rc5_timer_end(unsigned long data);
102void ir_rc5_timer_keyup(unsigned long data); 101void ir_rc5_timer_keyup(unsigned long data);
103 102
@@ -141,6 +140,8 @@ extern IR_KEYTAB_TYPE ir_codes_asus_pc39[IR_KEYTAB_SIZE];
141extern IR_KEYTAB_TYPE ir_codes_encore_enltv[IR_KEYTAB_SIZE]; 140extern IR_KEYTAB_TYPE ir_codes_encore_enltv[IR_KEYTAB_SIZE];
142extern IR_KEYTAB_TYPE ir_codes_tt_1500[IR_KEYTAB_SIZE]; 141extern IR_KEYTAB_TYPE ir_codes_tt_1500[IR_KEYTAB_SIZE];
143extern IR_KEYTAB_TYPE ir_codes_fusionhdtv_mce[IR_KEYTAB_SIZE]; 142extern IR_KEYTAB_TYPE ir_codes_fusionhdtv_mce[IR_KEYTAB_SIZE];
143extern IR_KEYTAB_TYPE ir_codes_behold[IR_KEYTAB_SIZE];
144extern IR_KEYTAB_TYPE ir_codes_pinnacle_pctv_hd[IR_KEYTAB_SIZE];
144 145
145#endif 146#endif
146 147
diff --git a/include/media/m52790.h b/include/media/m52790.h
new file mode 100644
index 000000000000..7ddffae31a67
--- /dev/null
+++ b/include/media/m52790.h
@@ -0,0 +1,93 @@
1/*
2 m52790.h - definition for m52790 inputs and outputs
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#ifndef _M52790_H_
22#define _M52790_H_
23
24/* Input routing switch 1 */
25
26#define M52790_SW1_IN_MASK 0x0003
27#define M52790_SW1_IN_TUNER 0x0000
28#define M52790_SW1_IN_V2 0x0001
29#define M52790_SW1_IN_V3 0x0002
30#define M52790_SW1_IN_V4 0x0003
31
32/* Selects component input instead of composite */
33#define M52790_SW1_YCMIX 0x0004
34
35
36/* Input routing switch 2 */
37
38#define M52790_SW2_IN_MASK 0x0300
39#define M52790_SW2_IN_TUNER 0x0000
40#define M52790_SW2_IN_V2 0x0100
41#define M52790_SW2_IN_V3 0x0200
42#define M52790_SW2_IN_V4 0x0300
43
44/* Selects component input instead of composite */
45#define M52790_SW2_YCMIX 0x0400
46
47
48/* Output routing switch 1 */
49
50/* Enable 6dB amplifier for composite out */
51#define M52790_SW1_V_AMP 0x0008
52
53/* Enable 6dB amplifier for component out */
54#define M52790_SW1_YC_AMP 0x0010
55
56/* Audio output mode */
57#define M52790_SW1_AUDIO_MASK 0x00c0
58#define M52790_SW1_AUDIO_MUTE 0x0000
59#define M52790_SW1_AUDIO_R 0x0040
60#define M52790_SW1_AUDIO_L 0x0080
61#define M52790_SW1_AUDIO_STEREO 0x00c0
62
63
64/* Output routing switch 2 */
65
66/* Enable 6dB amplifier for composite out */
67#define M52790_SW2_V_AMP 0x0800
68
69/* Enable 6dB amplifier for component out */
70#define M52790_SW2_YC_AMP 0x1000
71
72/* Audio output mode */
73#define M52790_SW2_AUDIO_MASK 0xc000
74#define M52790_SW2_AUDIO_MUTE 0x0000
75#define M52790_SW2_AUDIO_R 0x4000
76#define M52790_SW2_AUDIO_L 0x8000
77#define M52790_SW2_AUDIO_STEREO 0xc000
78
79
80/* Common values */
81#define M52790_IN_TUNER (M52790_SW1_IN_TUNER | M52790_SW2_IN_TUNER)
82#define M52790_IN_V2 (M52790_SW1_IN_V2 | M52790_SW2_IN_V2)
83#define M52790_IN_V3 (M52790_SW1_IN_V3 | M52790_SW2_IN_V3)
84#define M52790_IN_V4 (M52790_SW1_IN_V4 | M52790_SW2_IN_V4)
85
86#define M52790_OUT_STEREO (M52790_SW1_AUDIO_STEREO | \
87 M52790_SW2_AUDIO_STEREO)
88#define M52790_OUT_AMP_STEREO (M52790_SW1_AUDIO_STEREO | \
89 M52790_SW1_V_AMP | \
90 M52790_SW2_AUDIO_STEREO | \
91 M52790_SW2_V_AMP)
92
93#endif
diff --git a/include/media/saa7146_vv.h b/include/media/saa7146_vv.h
index e49f7e156061..89c442eb8849 100644
--- a/include/media/saa7146_vv.h
+++ b/include/media/saa7146_vv.h
@@ -1,7 +1,6 @@
1#ifndef __SAA7146_VV__ 1#ifndef __SAA7146_VV__
2#define __SAA7146_VV__ 2#define __SAA7146_VV__
3 3
4#include <linux/videodev.h>
5#include <media/v4l2-common.h> 4#include <media/v4l2-common.h>
6#include <media/saa7146.h> 5#include <media/saa7146.h>
7#include <media/videobuf-dma-sg.h> 6#include <media/videobuf-dma-sg.h>
diff --git a/include/media/tuner.h b/include/media/tuner.h
index c03dceb92605..1bf24a6ed8f1 100644
--- a/include/media/tuner.h
+++ b/include/media/tuner.h
@@ -24,8 +24,6 @@
24 24
25#include <linux/videodev2.h> 25#include <linux/videodev2.h>
26 26
27extern int tuner_debug;
28
29#define ADDR_UNSET (255) 27#define ADDR_UNSET (255)
30 28
31#define TUNER_TEMIC_PAL 0 /* 4002 FH5 (3X 7756, 9483) */ 29#define TUNER_TEMIC_PAL 0 /* 4002 FH5 (3X 7756, 9483) */
@@ -117,12 +115,13 @@ extern int tuner_debug;
117#define TUNER_PHILIPS_TUV1236D 68 /* ATI HDTV Wonder */ 115#define TUNER_PHILIPS_TUV1236D 68 /* ATI HDTV Wonder */
118#define TUNER_TNF_5335MF 69 /* Sabrent Bt848 */ 116#define TUNER_TNF_5335MF 69 /* Sabrent Bt848 */
119#define TUNER_SAMSUNG_TCPN_2121P30A 70 /* Hauppauge PVR-500MCE NTSC */ 117#define TUNER_SAMSUNG_TCPN_2121P30A 70 /* Hauppauge PVR-500MCE NTSC */
120#define TUNER_XCEIVE_XC3028 71 118#define TUNER_XC2028 71
121 119
122#define TUNER_THOMSON_FE6600 72 /* DViCO FusionHDTV DVB-T Hybrid */ 120#define TUNER_THOMSON_FE6600 72 /* DViCO FusionHDTV DVB-T Hybrid */
123#define TUNER_SAMSUNG_TCPG_6121P30A 73 /* Hauppauge PVR-500 PAL */ 121#define TUNER_SAMSUNG_TCPG_6121P30A 73 /* Hauppauge PVR-500 PAL */
124#define TUNER_TDA9887 74 /* This tuner should be used only internally */ 122#define TUNER_TDA9887 74 /* This tuner should be used only internally */
125#define TUNER_TEA5761 75 /* Only FM Radio Tuner */ 123#define TUNER_TEA5761 75 /* Only FM Radio Tuner */
124#define TUNER_XC5000 76 /* Xceive Silicon Tuner */
126 125
127/* tv card specific */ 126/* tv card specific */
128#define TDA9887_PRESENT (1<<0) 127#define TDA9887_PRESENT (1<<0)
diff --git a/include/media/v4l2-chip-ident.h b/include/media/v4l2-chip-ident.h
index 8ae42c41dd08..032bb75f69c2 100644
--- a/include/media/v4l2-chip-ident.h
+++ b/include/media/v4l2-chip-ident.h
@@ -68,6 +68,9 @@ enum {
68 /* module vp27smpx: just ident 2700 */ 68 /* module vp27smpx: just ident 2700 */
69 V4L2_IDENT_VP27SMPX = 2700, 69 V4L2_IDENT_VP27SMPX = 2700,
70 70
71 /* module cs5345: just ident 5345 */
72 V4L2_IDENT_CS5345 = 5345,
73
71 /* module wm8739: just ident 8739 */ 74 /* module wm8739: just ident 8739 */
72 V4L2_IDENT_WM8739 = 8739, 75 V4L2_IDENT_WM8739 = 8739,
73 76
@@ -83,6 +86,9 @@ enum {
83 /* module upd64083: just ident 64083 */ 86 /* module upd64083: just ident 64083 */
84 V4L2_IDENT_UPD64083 = 64083, 87 V4L2_IDENT_UPD64083 = 64083,
85 88
89 /* module m52790: just ident 52790 */
90 V4L2_IDENT_M52790 = 52790,
91
86 /* module msp34xx: reserved range 34000-34999 */ 92 /* module msp34xx: reserved range 34000-34999 */
87 V4L2_IDENT_MSP3400B = 34002, 93 V4L2_IDENT_MSP3400B = 34002,
88 V4L2_IDENT_MSP3410B = 34102, 94 V4L2_IDENT_MSP3410B = 34102,
diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
index 181a40c46a52..475d0d8275e0 100644
--- a/include/media/v4l2-common.h
+++ b/include/media/v4l2-common.h
@@ -104,6 +104,17 @@ int v4l2_chip_match_host(u32 id_type, u32 chip_id);
104 104
105/* ------------------------------------------------------------------------- */ 105/* ------------------------------------------------------------------------- */
106 106
107/* Helper function for I2C legacy drivers */
108
109struct i2c_driver;
110struct i2c_adapter;
111struct i2c_client;
112
113int v4l2_i2c_attach(struct i2c_adapter *adapter, int address, struct i2c_driver *driver,
114 const char *name, int (*probe)(struct i2c_client *));
115
116/* ------------------------------------------------------------------------- */
117
107/* Internal ioctls */ 118/* Internal ioctls */
108 119
109/* VIDIOC_INT_DECODE_VBI_LINE */ 120/* VIDIOC_INT_DECODE_VBI_LINE */
@@ -116,6 +127,11 @@ struct v4l2_decode_vbi_line {
116 u32 type; /* VBI service type (V4L2_SLICED_*). 0 if no service found */ 127 u32 type; /* VBI service type (V4L2_SLICED_*). 0 if no service found */
117}; 128};
118 129
130struct v4l2_priv_tun_config {
131 int tuner;
132 void *priv;
133};
134
119/* audio ioctls */ 135/* audio ioctls */
120 136
121/* v4l device was opened in Radio mode, to be replaced by VIDIOC_INT_S_TUNER_MODE */ 137/* v4l device was opened in Radio mode, to be replaced by VIDIOC_INT_S_TUNER_MODE */
@@ -131,7 +147,7 @@ struct v4l2_decode_vbi_line {
131#define TUNER_SET_STANDBY _IOW('d', 91, int) 147#define TUNER_SET_STANDBY _IOW('d', 91, int)
132 148
133/* Sets tda9887 specific stuff, like port1, port2 and qss */ 149/* Sets tda9887 specific stuff, like port1, port2 and qss */
134#define TDA9887_SET_CONFIG _IOW('d', 92, int) 150#define TUNER_SET_CONFIG _IOW('d', 92, struct v4l2_priv_tun_config)
135 151
136/* Switch the tuner to a specific tuner mode. Replacement of AUDC_SET_RADIO */ 152/* Switch the tuner to a specific tuner mode. Replacement of AUDC_SET_RADIO */
137#define VIDIOC_INT_S_TUNER_MODE _IOW('d', 93, enum v4l2_tuner_type) 153#define VIDIOC_INT_S_TUNER_MODE _IOW('d', 93, enum v4l2_tuner_type)
diff --git a/include/media/v4l2-i2c-drv-legacy.h b/include/media/v4l2-i2c-drv-legacy.h
new file mode 100644
index 000000000000..241854229d6f
--- /dev/null
+++ b/include/media/v4l2-i2c-drv-legacy.h
@@ -0,0 +1,140 @@
1/*
2 * v4l2-i2c-drv-legacy.h - contains I2C handling code that's identical
3 * for all V4L2 I2C drivers. Use this header if the
4 * I2C driver is used by both legacy drivers and
5 * drivers converted to the bus-based I2C API.
6 *
7 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
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
24struct v4l2_i2c_driver_data {
25 const char * const name;
26 int driverid;
27 int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
28 int (*probe)(struct i2c_client *client);
29 int (*remove)(struct i2c_client *client);
30 int (*suspend)(struct i2c_client *client, pm_message_t state);
31 int (*resume)(struct i2c_client *client);
32 int (*legacy_probe)(struct i2c_adapter *adapter);
33 int legacy_class;
34};
35
36static struct v4l2_i2c_driver_data v4l2_i2c_data;
37static struct i2c_client_address_data addr_data;
38static struct i2c_driver v4l2_i2c_driver_legacy;
39static char v4l2_i2c_drv_name_legacy[32];
40
41static int v4l2_i2c_drv_attach_legacy(struct i2c_adapter *adapter, int address, int kind)
42{
43 return v4l2_i2c_attach(adapter, address, &v4l2_i2c_driver_legacy,
44 v4l2_i2c_drv_name_legacy, v4l2_i2c_data.probe);
45}
46
47static int v4l2_i2c_drv_probe_legacy(struct i2c_adapter *adapter)
48{
49 if (v4l2_i2c_data.legacy_probe) {
50 if (v4l2_i2c_data.legacy_probe(adapter))
51 return i2c_probe(adapter, &addr_data, v4l2_i2c_drv_attach_legacy);
52 return 0;
53 }
54 if (adapter->class & v4l2_i2c_data.legacy_class)
55 return i2c_probe(adapter, &addr_data, v4l2_i2c_drv_attach_legacy);
56 return 0;
57}
58
59static int v4l2_i2c_drv_detach_legacy(struct i2c_client *client)
60{
61 int err;
62
63 if (v4l2_i2c_data.remove)
64 v4l2_i2c_data.remove(client);
65
66 err = i2c_detach_client(client);
67 if (err)
68 return err;
69 kfree(client);
70
71 return 0;
72}
73
74static int v4l2_i2c_drv_suspend_helper(struct i2c_client *client, pm_message_t state)
75{
76 return v4l2_i2c_data.suspend ? v4l2_i2c_data.suspend(client, state) : 0;
77}
78
79static int v4l2_i2c_drv_resume_helper(struct i2c_client *client)
80{
81 return v4l2_i2c_data.resume ? v4l2_i2c_data.resume(client) : 0;
82}
83
84/* ----------------------------------------------------------------------- */
85
86/* i2c implementation */
87static struct i2c_driver v4l2_i2c_driver_legacy = {
88 .driver = {
89 .owner = THIS_MODULE,
90 },
91 .attach_adapter = v4l2_i2c_drv_probe_legacy,
92 .detach_client = v4l2_i2c_drv_detach_legacy,
93 .suspend = v4l2_i2c_drv_suspend_helper,
94 .resume = v4l2_i2c_drv_resume_helper,
95};
96
97/* ----------------------------------------------------------------------- */
98
99/* i2c implementation */
100static struct i2c_driver v4l2_i2c_driver = {
101 .suspend = v4l2_i2c_drv_suspend_helper,
102 .resume = v4l2_i2c_drv_resume_helper,
103};
104
105static int __init v4l2_i2c_drv_init(void)
106{
107 int err;
108
109 strlcpy(v4l2_i2c_drv_name_legacy, v4l2_i2c_data.name, sizeof(v4l2_i2c_drv_name_legacy));
110 strlcat(v4l2_i2c_drv_name_legacy, "'", sizeof(v4l2_i2c_drv_name_legacy));
111
112 if (v4l2_i2c_data.legacy_class == 0)
113 v4l2_i2c_data.legacy_class = I2C_CLASS_TV_ANALOG;
114
115 v4l2_i2c_driver_legacy.driver.name = v4l2_i2c_drv_name_legacy;
116 v4l2_i2c_driver_legacy.id = v4l2_i2c_data.driverid;
117 v4l2_i2c_driver_legacy.command = v4l2_i2c_data.command;
118 err = i2c_add_driver(&v4l2_i2c_driver_legacy);
119
120 if (err)
121 return err;
122 v4l2_i2c_driver.driver.name = v4l2_i2c_data.name;
123 v4l2_i2c_driver.id = v4l2_i2c_data.driverid;
124 v4l2_i2c_driver.command = v4l2_i2c_data.command;
125 v4l2_i2c_driver.probe = v4l2_i2c_data.probe;
126 v4l2_i2c_driver.remove = v4l2_i2c_data.remove;
127 err = i2c_add_driver(&v4l2_i2c_driver);
128 if (err)
129 i2c_del_driver(&v4l2_i2c_driver_legacy);
130 return err;
131}
132
133static void __exit v4l2_i2c_drv_cleanup(void)
134{
135 i2c_del_driver(&v4l2_i2c_driver_legacy);
136 i2c_del_driver(&v4l2_i2c_driver);
137}
138
139module_init(v4l2_i2c_drv_init);
140module_exit(v4l2_i2c_drv_cleanup);
diff --git a/include/media/v4l2-i2c-drv.h b/include/media/v4l2-i2c-drv.h
new file mode 100644
index 000000000000..9e4bab276915
--- /dev/null
+++ b/include/media/v4l2-i2c-drv.h
@@ -0,0 +1,68 @@
1/*
2 * v4l2-i2c-drv.h - contains I2C handling code that's identical for
3 * all V4L2 I2C drivers. Use this header if the
4 * I2C driver is only used by drivers converted
5 * to the bus-based I2C API.
6 *
7 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
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#ifndef __V4L2_I2C_DRV_H__
25#define __V4L2_I2C_DRV_H__
26
27#include <media/v4l2-common.h>
28
29struct v4l2_i2c_driver_data {
30 const char * const name;
31 int driverid;
32 int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
33 int (*probe)(struct i2c_client *client);
34 int (*remove)(struct i2c_client *client);
35 int (*suspend)(struct i2c_client *client, pm_message_t state);
36 int (*resume)(struct i2c_client *client);
37 int (*legacy_probe)(struct i2c_adapter *adapter);
38 int legacy_class;
39};
40
41static struct v4l2_i2c_driver_data v4l2_i2c_data;
42static struct i2c_driver v4l2_i2c_driver;
43
44
45/* Bus-based I2C implementation for kernels >= 2.6.22 */
46
47static int __init v4l2_i2c_drv_init(void)
48{
49 v4l2_i2c_driver.driver.name = v4l2_i2c_data.name;
50 v4l2_i2c_driver.id = v4l2_i2c_data.driverid;
51 v4l2_i2c_driver.command = v4l2_i2c_data.command;
52 v4l2_i2c_driver.probe = v4l2_i2c_data.probe;
53 v4l2_i2c_driver.remove = v4l2_i2c_data.remove;
54 v4l2_i2c_driver.suspend = v4l2_i2c_data.suspend;
55 v4l2_i2c_driver.resume = v4l2_i2c_data.resume;
56 return i2c_add_driver(&v4l2_i2c_driver);
57}
58
59
60static void __exit v4l2_i2c_drv_cleanup(void)
61{
62 i2c_del_driver(&v4l2_i2c_driver);
63}
64
65module_init(v4l2_i2c_drv_init);
66module_exit(v4l2_i2c_drv_cleanup);
67
68#endif /* __V4L2_I2C_DRV_H__ */
diff --git a/include/media/v4l2-int-device.h b/include/media/v4l2-int-device.h
index 066ebfc4f983..c8b80e0f0651 100644
--- a/include/media/v4l2-int-device.h
+++ b/include/media/v4l2-int-device.h
@@ -44,9 +44,8 @@ enum v4l2_int_type {
44struct v4l2_int_device; 44struct v4l2_int_device;
45 45
46struct v4l2_int_master { 46struct v4l2_int_master {
47 int (*attach)(struct v4l2_int_device *master, 47 int (*attach)(struct v4l2_int_device *slave);
48 struct v4l2_int_device *slave); 48 void (*detach)(struct v4l2_int_device *slave);
49 void (*detach)(struct v4l2_int_device *master);
50}; 49};
51 50
52typedef int (v4l2_int_ioctl_func)(struct v4l2_int_device *); 51typedef int (v4l2_int_ioctl_func)(struct v4l2_int_device *);
diff --git a/include/media/videobuf-core.h b/include/media/videobuf-core.h
index 4fd5d0eaa935..97f14d469595 100644
--- a/include/media/videobuf-core.h
+++ b/include/media/videobuf-core.h
@@ -56,13 +56,13 @@ struct videobuf_mapping {
56}; 56};
57 57
58enum videobuf_state { 58enum videobuf_state {
59 STATE_NEEDS_INIT = 0, 59 VIDEOBUF_NEEDS_INIT = 0,
60 STATE_PREPARED = 1, 60 VIDEOBUF_PREPARED = 1,
61 STATE_QUEUED = 2, 61 VIDEOBUF_QUEUED = 2,
62 STATE_ACTIVE = 3, 62 VIDEOBUF_ACTIVE = 3,
63 STATE_DONE = 4, 63 VIDEOBUF_DONE = 4,
64 STATE_ERROR = 5, 64 VIDEOBUF_ERROR = 5,
65 STATE_IDLE = 6, 65 VIDEOBUF_IDLE = 6,
66}; 66};
67 67
68struct videobuf_buffer { 68struct videobuf_buffer {
@@ -162,12 +162,14 @@ struct videobuf_queue {
162 struct videobuf_queue_ops *ops; 162 struct videobuf_queue_ops *ops;
163 struct videobuf_qtype_ops *int_ops; 163 struct videobuf_qtype_ops *int_ops;
164 164
165 unsigned int streaming:1;
166 unsigned int reading:1;
167 unsigned int is_mmapped:1;
168
165 /* capture via mmap() + ioctl(QBUF/DQBUF) */ 169 /* capture via mmap() + ioctl(QBUF/DQBUF) */
166 unsigned int streaming;
167 struct list_head stream; 170 struct list_head stream;
168 171
169 /* capture via read() */ 172 /* capture via read() */
170 unsigned int reading;
171 unsigned int read_off; 173 unsigned int read_off;
172 struct videobuf_buffer *read_buf; 174 struct videobuf_buffer *read_buf;
173 175
diff --git a/include/net/if_inet6.h b/include/net/if_inet6.h
index 448eccb20638..b24508abb850 100644
--- a/include/net/if_inet6.h
+++ b/include/net/if_inet6.h
@@ -269,18 +269,21 @@ static inline void ipv6_arcnet_mc_map(const struct in6_addr *addr, char *buf)
269 buf[0] = 0x00; 269 buf[0] = 0x00;
270} 270}
271 271
272static inline void ipv6_ib_mc_map(struct in6_addr *addr, char *buf) 272static inline void ipv6_ib_mc_map(const struct in6_addr *addr,
273 const unsigned char *broadcast, char *buf)
273{ 274{
275 unsigned char scope = broadcast[5] & 0xF;
276
274 buf[0] = 0; /* Reserved */ 277 buf[0] = 0; /* Reserved */
275 buf[1] = 0xff; /* Multicast QPN */ 278 buf[1] = 0xff; /* Multicast QPN */
276 buf[2] = 0xff; 279 buf[2] = 0xff;
277 buf[3] = 0xff; 280 buf[3] = 0xff;
278 buf[4] = 0xff; 281 buf[4] = 0xff;
279 buf[5] = 0x12; /* link local scope */ 282 buf[5] = 0x10 | scope; /* scope from broadcast address */
280 buf[6] = 0x60; /* IPv6 signature */ 283 buf[6] = 0x60; /* IPv6 signature */
281 buf[7] = 0x1b; 284 buf[7] = 0x1b;
282 buf[8] = 0; /* P_Key */ 285 buf[8] = broadcast[8]; /* P_Key */
283 buf[9] = 0; 286 buf[9] = broadcast[9];
284 memcpy(buf + 10, addr->s6_addr + 6, 10); 287 memcpy(buf + 10, addr->s6_addr + 6, 10);
285} 288}
286#endif 289#endif
diff --git a/include/net/ip.h b/include/net/ip.h
index 840dd91b513b..50c8889b1b8d 100644
--- a/include/net/ip.h
+++ b/include/net/ip.h
@@ -266,20 +266,22 @@ static inline void ip_eth_mc_map(__be32 naddr, char *buf)
266 * Leave P_Key as 0 to be filled in by driver. 266 * Leave P_Key as 0 to be filled in by driver.
267 */ 267 */
268 268
269static inline void ip_ib_mc_map(__be32 naddr, char *buf) 269static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
270{ 270{
271 __u32 addr; 271 __u32 addr;
272 unsigned char scope = broadcast[5] & 0xF;
273
272 buf[0] = 0; /* Reserved */ 274 buf[0] = 0; /* Reserved */
273 buf[1] = 0xff; /* Multicast QPN */ 275 buf[1] = 0xff; /* Multicast QPN */
274 buf[2] = 0xff; 276 buf[2] = 0xff;
275 buf[3] = 0xff; 277 buf[3] = 0xff;
276 addr = ntohl(naddr); 278 addr = ntohl(naddr);
277 buf[4] = 0xff; 279 buf[4] = 0xff;
278 buf[5] = 0x12; /* link local scope */ 280 buf[5] = 0x10 | scope; /* scope from broadcast address */
279 buf[6] = 0x40; /* IPv4 signature */ 281 buf[6] = 0x40; /* IPv4 signature */
280 buf[7] = 0x1b; 282 buf[7] = 0x1b;
281 buf[8] = 0; /* P_Key */ 283 buf[8] = broadcast[8]; /* P_Key */
282 buf[9] = 0; 284 buf[9] = broadcast[9];
283 buf[10] = 0; 285 buf[10] = 0;
284 buf[11] = 0; 286 buf[11] = 0;
285 buf[12] = 0; 287 buf[12] = 0;
diff --git a/include/rdma/ib_mad.h b/include/rdma/ib_mad.h
index 8ec3799e42e1..7228c056b9e9 100644
--- a/include/rdma/ib_mad.h
+++ b/include/rdma/ib_mad.h
@@ -230,7 +230,9 @@ struct ib_class_port_info
230 * @seg_count: The number of RMPP segments allocated for this send. 230 * @seg_count: The number of RMPP segments allocated for this send.
231 * @seg_size: Size of each RMPP segment. 231 * @seg_size: Size of each RMPP segment.
232 * @timeout_ms: Time to wait for a response. 232 * @timeout_ms: Time to wait for a response.
233 * @retries: Number of times to retry a request for a response. 233 * @retries: Number of times to retry a request for a response. For MADs
234 * using RMPP, this applies per window. On completion, returns the number
235 * of retries needed to complete the transfer.
234 * 236 *
235 * Users are responsible for initializing the MAD buffer itself, with the 237 * Users are responsible for initializing the MAD buffer itself, with the
236 * exception of any RMPP header. Additional segment buffer space allocated 238 * exception of any RMPP header. Additional segment buffer space allocated
diff --git a/include/rdma/rdma_user_cm.h b/include/rdma/rdma_user_cm.h
index 9749c1b34d00..c55705460b87 100644
--- a/include/rdma/rdma_user_cm.h
+++ b/include/rdma/rdma_user_cm.h
@@ -60,7 +60,8 @@ enum {
60 RDMA_USER_CM_CMD_SET_OPTION, 60 RDMA_USER_CM_CMD_SET_OPTION,
61 RDMA_USER_CM_CMD_NOTIFY, 61 RDMA_USER_CM_CMD_NOTIFY,
62 RDMA_USER_CM_CMD_JOIN_MCAST, 62 RDMA_USER_CM_CMD_JOIN_MCAST,
63 RDMA_USER_CM_CMD_LEAVE_MCAST 63 RDMA_USER_CM_CMD_LEAVE_MCAST,
64 RDMA_USER_CM_CMD_MIGRATE_ID
64}; 65};
65 66
66/* 67/*
@@ -230,4 +231,14 @@ struct rdma_ucm_set_option {
230 __u32 optlen; 231 __u32 optlen;
231}; 232};
232 233
234struct rdma_ucm_migrate_id {
235 __u64 response;
236 __u32 id;
237 __u32 fd;
238};
239
240struct rdma_ucm_migrate_resp {
241 __u32 events_reported;
242};
243
233#endif /* RDMA_USER_CM_H */ 244#endif /* RDMA_USER_CM_H */
diff --git a/init/Kconfig b/init/Kconfig
index f5becd2a12f6..0eda68f0ad54 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -763,3 +763,31 @@ source "block/Kconfig"
763 763
764config PREEMPT_NOTIFIERS 764config PREEMPT_NOTIFIERS
765 bool 765 bool
766
767choice
768 prompt "RCU implementation type:"
769 default CLASSIC_RCU
770
771config CLASSIC_RCU
772 bool "Classic RCU"
773 help
774 This option selects the classic RCU implementation that is
775 designed for best read-side performance on non-realtime
776 systems.
777
778 Say Y if you are unsure.
779
780config PREEMPT_RCU
781 bool "Preemptible RCU"
782 depends on PREEMPT
783 help
784 This option reduces the latency of the kernel by making certain
785 RCU sections preemptible. Normally RCU code is non-preemptible, if
786 this option is selected then read-only RCU sections become
787 preemptible. This helps latency, but may expose bugs due to
788 now-naive assumptions about each RCU read-side critical section
789 remaining on a given CPU through its execution.
790
791 Say N if you are unsure.
792
793endchoice
diff --git a/init/main.c b/init/main.c
index 80b04b6c5157..f287ca5862b9 100644
--- a/init/main.c
+++ b/init/main.c
@@ -607,6 +607,7 @@ asmlinkage void __init start_kernel(void)
607 vfs_caches_init_early(); 607 vfs_caches_init_early();
608 cpuset_init_early(); 608 cpuset_init_early();
609 mem_init(); 609 mem_init();
610 cpu_hotplug_init();
610 kmem_cache_init(); 611 kmem_cache_init();
611 setup_per_cpu_pageset(); 612 setup_per_cpu_pageset();
612 numa_policy_init(); 613 numa_policy_init();
diff --git a/kernel/Kconfig.hz b/kernel/Kconfig.hz
index 4af15802ccd4..526128a2e622 100644
--- a/kernel/Kconfig.hz
+++ b/kernel/Kconfig.hz
@@ -54,3 +54,5 @@ config HZ
54 default 300 if HZ_300 54 default 300 if HZ_300
55 default 1000 if HZ_1000 55 default 1000 if HZ_1000
56 56
57config SCHED_HRTICK
58 def_bool HIGH_RES_TIMERS && X86
diff --git a/kernel/Kconfig.preempt b/kernel/Kconfig.preempt
index c64ce9c14207..0669b70fa6a3 100644
--- a/kernel/Kconfig.preempt
+++ b/kernel/Kconfig.preempt
@@ -52,14 +52,13 @@ config PREEMPT
52 52
53endchoice 53endchoice
54 54
55config PREEMPT_BKL 55config RCU_TRACE
56 bool "Preempt The Big Kernel Lock" 56 bool "Enable tracing for RCU - currently stats in debugfs"
57 depends on SMP || PREEMPT 57 select DEBUG_FS
58 default y 58 default y
59 help 59 help
60 This option reduces the latency of the kernel by making the 60 This option provides tracing in RCU which presents stats
61 big kernel lock preemptible. 61 in debugfs for debugging RCU implementation.
62 62
63 Say Y here if you are building a kernel for a desktop system. 63 Say Y here if you want to enable RCU tracing
64 Say N if you are unsure. 64 Say N if you are unsure.
65
diff --git a/kernel/Makefile b/kernel/Makefile
index dfa96956dae0..390d42146267 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -52,11 +52,17 @@ obj-$(CONFIG_DETECT_SOFTLOCKUP) += softlockup.o
52obj-$(CONFIG_GENERIC_HARDIRQS) += irq/ 52obj-$(CONFIG_GENERIC_HARDIRQS) += irq/
53obj-$(CONFIG_SECCOMP) += seccomp.o 53obj-$(CONFIG_SECCOMP) += seccomp.o
54obj-$(CONFIG_RCU_TORTURE_TEST) += rcutorture.o 54obj-$(CONFIG_RCU_TORTURE_TEST) += rcutorture.o
55obj-$(CONFIG_CLASSIC_RCU) += rcuclassic.o
56obj-$(CONFIG_PREEMPT_RCU) += rcupreempt.o
57ifeq ($(CONFIG_PREEMPT_RCU),y)
58obj-$(CONFIG_RCU_TRACE) += rcupreempt_trace.o
59endif
55obj-$(CONFIG_RELAY) += relay.o 60obj-$(CONFIG_RELAY) += relay.o
56obj-$(CONFIG_SYSCTL) += utsname_sysctl.o 61obj-$(CONFIG_SYSCTL) += utsname_sysctl.o
57obj-$(CONFIG_TASK_DELAY_ACCT) += delayacct.o 62obj-$(CONFIG_TASK_DELAY_ACCT) += delayacct.o
58obj-$(CONFIG_TASKSTATS) += taskstats.o tsacct.o 63obj-$(CONFIG_TASKSTATS) += taskstats.o tsacct.o
59obj-$(CONFIG_MARKERS) += marker.o 64obj-$(CONFIG_MARKERS) += marker.o
65obj-$(CONFIG_LATENCYTOP) += latencytop.o
60 66
61ifneq ($(CONFIG_SCHED_NO_NO_OMIT_FRAME_POINTER),y) 67ifneq ($(CONFIG_SCHED_NO_NO_OMIT_FRAME_POINTER),y)
62# According to Alan Modra <alan@linuxcare.com.au>, the -fno-omit-frame-pointer is 68# According to Alan Modra <alan@linuxcare.com.au>, the -fno-omit-frame-pointer is
diff --git a/kernel/cpu.c b/kernel/cpu.c
index 6b3a0c15144f..e0d3a4f56ecb 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -15,9 +15,8 @@
15#include <linux/stop_machine.h> 15#include <linux/stop_machine.h>
16#include <linux/mutex.h> 16#include <linux/mutex.h>
17 17
18/* This protects CPUs going up and down... */ 18/* Serializes the updates to cpu_online_map, cpu_present_map */
19static DEFINE_MUTEX(cpu_add_remove_lock); 19static DEFINE_MUTEX(cpu_add_remove_lock);
20static DEFINE_MUTEX(cpu_bitmask_lock);
21 20
22static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain); 21static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain);
23 22
@@ -26,52 +25,123 @@ static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain);
26 */ 25 */
27static int cpu_hotplug_disabled; 26static int cpu_hotplug_disabled;
28 27
29#ifdef CONFIG_HOTPLUG_CPU 28static struct {
29 struct task_struct *active_writer;
30 struct mutex lock; /* Synchronizes accesses to refcount, */
31 /*
32 * Also blocks the new readers during
33 * an ongoing cpu hotplug operation.
34 */
35 int refcount;
36 wait_queue_head_t writer_queue;
37} cpu_hotplug;
30 38
31/* Crappy recursive lock-takers in cpufreq! Complain loudly about idiots */ 39#define writer_exists() (cpu_hotplug.active_writer != NULL)
32static struct task_struct *recursive;
33static int recursive_depth;
34 40
35void lock_cpu_hotplug(void) 41void __init cpu_hotplug_init(void)
36{ 42{
37 struct task_struct *tsk = current; 43 cpu_hotplug.active_writer = NULL;
38 44 mutex_init(&cpu_hotplug.lock);
39 if (tsk == recursive) { 45 cpu_hotplug.refcount = 0;
40 static int warnings = 10; 46 init_waitqueue_head(&cpu_hotplug.writer_queue);
41 if (warnings) { 47}
42 printk(KERN_ERR "Lukewarm IQ detected in hotplug locking\n"); 48
43 WARN_ON(1); 49#ifdef CONFIG_HOTPLUG_CPU
44 warnings--; 50
45 } 51void get_online_cpus(void)
46 recursive_depth++; 52{
53 might_sleep();
54 if (cpu_hotplug.active_writer == current)
47 return; 55 return;
48 } 56 mutex_lock(&cpu_hotplug.lock);
49 mutex_lock(&cpu_bitmask_lock); 57 cpu_hotplug.refcount++;
50 recursive = tsk; 58 mutex_unlock(&cpu_hotplug.lock);
59
51} 60}
52EXPORT_SYMBOL_GPL(lock_cpu_hotplug); 61EXPORT_SYMBOL_GPL(get_online_cpus);
53 62
54void unlock_cpu_hotplug(void) 63void put_online_cpus(void)
55{ 64{
56 WARN_ON(recursive != current); 65 if (cpu_hotplug.active_writer == current)
57 if (recursive_depth) {
58 recursive_depth--;
59 return; 66 return;
60 } 67 mutex_lock(&cpu_hotplug.lock);
61 recursive = NULL; 68 cpu_hotplug.refcount--;
62 mutex_unlock(&cpu_bitmask_lock); 69
70 if (unlikely(writer_exists()) && !cpu_hotplug.refcount)
71 wake_up(&cpu_hotplug.writer_queue);
72
73 mutex_unlock(&cpu_hotplug.lock);
74
63} 75}
64EXPORT_SYMBOL_GPL(unlock_cpu_hotplug); 76EXPORT_SYMBOL_GPL(put_online_cpus);
65 77
66#endif /* CONFIG_HOTPLUG_CPU */ 78#endif /* CONFIG_HOTPLUG_CPU */
67 79
80/*
81 * The following two API's must be used when attempting
82 * to serialize the updates to cpu_online_map, cpu_present_map.
83 */
84void cpu_maps_update_begin(void)
85{
86 mutex_lock(&cpu_add_remove_lock);
87}
88
89void cpu_maps_update_done(void)
90{
91 mutex_unlock(&cpu_add_remove_lock);
92}
93
94/*
95 * This ensures that the hotplug operation can begin only when the
96 * refcount goes to zero.
97 *
98 * Note that during a cpu-hotplug operation, the new readers, if any,
99 * will be blocked by the cpu_hotplug.lock
100 *
101 * Since cpu_maps_update_begin is always called after invoking
102 * cpu_maps_update_begin, we can be sure that only one writer is active.
103 *
104 * Note that theoretically, there is a possibility of a livelock:
105 * - Refcount goes to zero, last reader wakes up the sleeping
106 * writer.
107 * - Last reader unlocks the cpu_hotplug.lock.
108 * - A new reader arrives at this moment, bumps up the refcount.
109 * - The writer acquires the cpu_hotplug.lock finds the refcount
110 * non zero and goes to sleep again.
111 *
112 * However, this is very difficult to achieve in practice since
113 * get_online_cpus() not an api which is called all that often.
114 *
115 */
116static void cpu_hotplug_begin(void)
117{
118 DECLARE_WAITQUEUE(wait, current);
119
120 mutex_lock(&cpu_hotplug.lock);
121
122 cpu_hotplug.active_writer = current;
123 add_wait_queue_exclusive(&cpu_hotplug.writer_queue, &wait);
124 while (cpu_hotplug.refcount) {
125 set_current_state(TASK_UNINTERRUPTIBLE);
126 mutex_unlock(&cpu_hotplug.lock);
127 schedule();
128 mutex_lock(&cpu_hotplug.lock);
129 }
130 remove_wait_queue_locked(&cpu_hotplug.writer_queue, &wait);
131}
132
133static void cpu_hotplug_done(void)
134{
135 cpu_hotplug.active_writer = NULL;
136 mutex_unlock(&cpu_hotplug.lock);
137}
68/* Need to know about CPUs going up/down? */ 138/* Need to know about CPUs going up/down? */
69int __cpuinit register_cpu_notifier(struct notifier_block *nb) 139int __cpuinit register_cpu_notifier(struct notifier_block *nb)
70{ 140{
71 int ret; 141 int ret;
72 mutex_lock(&cpu_add_remove_lock); 142 cpu_maps_update_begin();
73 ret = raw_notifier_chain_register(&cpu_chain, nb); 143 ret = raw_notifier_chain_register(&cpu_chain, nb);
74 mutex_unlock(&cpu_add_remove_lock); 144 cpu_maps_update_done();
75 return ret; 145 return ret;
76} 146}
77 147
@@ -81,9 +151,9 @@ EXPORT_SYMBOL(register_cpu_notifier);
81 151
82void unregister_cpu_notifier(struct notifier_block *nb) 152void unregister_cpu_notifier(struct notifier_block *nb)
83{ 153{
84 mutex_lock(&cpu_add_remove_lock); 154 cpu_maps_update_begin();
85 raw_notifier_chain_unregister(&cpu_chain, nb); 155 raw_notifier_chain_unregister(&cpu_chain, nb);
86 mutex_unlock(&cpu_add_remove_lock); 156 cpu_maps_update_done();
87} 157}
88EXPORT_SYMBOL(unregister_cpu_notifier); 158EXPORT_SYMBOL(unregister_cpu_notifier);
89 159
@@ -147,7 +217,7 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
147 if (!cpu_online(cpu)) 217 if (!cpu_online(cpu))
148 return -EINVAL; 218 return -EINVAL;
149 219
150 raw_notifier_call_chain(&cpu_chain, CPU_LOCK_ACQUIRE, hcpu); 220 cpu_hotplug_begin();
151 err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod, 221 err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
152 hcpu, -1, &nr_calls); 222 hcpu, -1, &nr_calls);
153 if (err == NOTIFY_BAD) { 223 if (err == NOTIFY_BAD) {
@@ -166,9 +236,7 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
166 cpu_clear(cpu, tmp); 236 cpu_clear(cpu, tmp);
167 set_cpus_allowed(current, tmp); 237 set_cpus_allowed(current, tmp);
168 238
169 mutex_lock(&cpu_bitmask_lock);
170 p = __stop_machine_run(take_cpu_down, &tcd_param, cpu); 239 p = __stop_machine_run(take_cpu_down, &tcd_param, cpu);
171 mutex_unlock(&cpu_bitmask_lock);
172 240
173 if (IS_ERR(p) || cpu_online(cpu)) { 241 if (IS_ERR(p) || cpu_online(cpu)) {
174 /* CPU didn't die: tell everyone. Can't complain. */ 242 /* CPU didn't die: tell everyone. Can't complain. */
@@ -202,7 +270,7 @@ out_thread:
202out_allowed: 270out_allowed:
203 set_cpus_allowed(current, old_allowed); 271 set_cpus_allowed(current, old_allowed);
204out_release: 272out_release:
205 raw_notifier_call_chain(&cpu_chain, CPU_LOCK_RELEASE, hcpu); 273 cpu_hotplug_done();
206 return err; 274 return err;
207} 275}
208 276
@@ -210,13 +278,13 @@ int cpu_down(unsigned int cpu)
210{ 278{
211 int err = 0; 279 int err = 0;
212 280
213 mutex_lock(&cpu_add_remove_lock); 281 cpu_maps_update_begin();
214 if (cpu_hotplug_disabled) 282 if (cpu_hotplug_disabled)
215 err = -EBUSY; 283 err = -EBUSY;
216 else 284 else
217 err = _cpu_down(cpu, 0); 285 err = _cpu_down(cpu, 0);
218 286
219 mutex_unlock(&cpu_add_remove_lock); 287 cpu_maps_update_done();
220 return err; 288 return err;
221} 289}
222#endif /*CONFIG_HOTPLUG_CPU*/ 290#endif /*CONFIG_HOTPLUG_CPU*/
@@ -231,7 +299,7 @@ static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
231 if (cpu_online(cpu) || !cpu_present(cpu)) 299 if (cpu_online(cpu) || !cpu_present(cpu))
232 return -EINVAL; 300 return -EINVAL;
233 301
234 raw_notifier_call_chain(&cpu_chain, CPU_LOCK_ACQUIRE, hcpu); 302 cpu_hotplug_begin();
235 ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu, 303 ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
236 -1, &nr_calls); 304 -1, &nr_calls);
237 if (ret == NOTIFY_BAD) { 305 if (ret == NOTIFY_BAD) {
@@ -243,9 +311,7 @@ static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
243 } 311 }
244 312
245 /* Arch-specific enabling code. */ 313 /* Arch-specific enabling code. */
246 mutex_lock(&cpu_bitmask_lock);
247 ret = __cpu_up(cpu); 314 ret = __cpu_up(cpu);
248 mutex_unlock(&cpu_bitmask_lock);
249 if (ret != 0) 315 if (ret != 0)
250 goto out_notify; 316 goto out_notify;
251 BUG_ON(!cpu_online(cpu)); 317 BUG_ON(!cpu_online(cpu));
@@ -257,7 +323,7 @@ out_notify:
257 if (ret != 0) 323 if (ret != 0)
258 __raw_notifier_call_chain(&cpu_chain, 324 __raw_notifier_call_chain(&cpu_chain,
259 CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL); 325 CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
260 raw_notifier_call_chain(&cpu_chain, CPU_LOCK_RELEASE, hcpu); 326 cpu_hotplug_done();
261 327
262 return ret; 328 return ret;
263} 329}
@@ -275,13 +341,13 @@ int __cpuinit cpu_up(unsigned int cpu)
275 return -EINVAL; 341 return -EINVAL;
276 } 342 }
277 343
278 mutex_lock(&cpu_add_remove_lock); 344 cpu_maps_update_begin();
279 if (cpu_hotplug_disabled) 345 if (cpu_hotplug_disabled)
280 err = -EBUSY; 346 err = -EBUSY;
281 else 347 else
282 err = _cpu_up(cpu, 0); 348 err = _cpu_up(cpu, 0);
283 349
284 mutex_unlock(&cpu_add_remove_lock); 350 cpu_maps_update_done();
285 return err; 351 return err;
286} 352}
287 353
@@ -292,7 +358,7 @@ int disable_nonboot_cpus(void)
292{ 358{
293 int cpu, first_cpu, error = 0; 359 int cpu, first_cpu, error = 0;
294 360
295 mutex_lock(&cpu_add_remove_lock); 361 cpu_maps_update_begin();
296 first_cpu = first_cpu(cpu_online_map); 362 first_cpu = first_cpu(cpu_online_map);
297 /* We take down all of the non-boot CPUs in one shot to avoid races 363 /* We take down all of the non-boot CPUs in one shot to avoid races
298 * with the userspace trying to use the CPU hotplug at the same time 364 * with the userspace trying to use the CPU hotplug at the same time
@@ -319,7 +385,7 @@ int disable_nonboot_cpus(void)
319 } else { 385 } else {
320 printk(KERN_ERR "Non-boot CPUs are not disabled\n"); 386 printk(KERN_ERR "Non-boot CPUs are not disabled\n");
321 } 387 }
322 mutex_unlock(&cpu_add_remove_lock); 388 cpu_maps_update_done();
323 return error; 389 return error;
324} 390}
325 391
@@ -328,7 +394,7 @@ void enable_nonboot_cpus(void)
328 int cpu, error; 394 int cpu, error;
329 395
330 /* Allow everyone to use the CPU hotplug again */ 396 /* Allow everyone to use the CPU hotplug again */
331 mutex_lock(&cpu_add_remove_lock); 397 cpu_maps_update_begin();
332 cpu_hotplug_disabled = 0; 398 cpu_hotplug_disabled = 0;
333 if (cpus_empty(frozen_cpus)) 399 if (cpus_empty(frozen_cpus))
334 goto out; 400 goto out;
@@ -344,6 +410,6 @@ void enable_nonboot_cpus(void)
344 } 410 }
345 cpus_clear(frozen_cpus); 411 cpus_clear(frozen_cpus);
346out: 412out:
347 mutex_unlock(&cpu_add_remove_lock); 413 cpu_maps_update_done();
348} 414}
349#endif /* CONFIG_PM_SLEEP_SMP */ 415#endif /* CONFIG_PM_SLEEP_SMP */
diff --git a/kernel/cpuset.c b/kernel/cpuset.c
index 50f5dc463688..cfaf6419d817 100644
--- a/kernel/cpuset.c
+++ b/kernel/cpuset.c
@@ -537,10 +537,10 @@ static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
537 * 537 *
538 * Call with cgroup_mutex held. May take callback_mutex during 538 * Call with cgroup_mutex held. May take callback_mutex during
539 * call due to the kfifo_alloc() and kmalloc() calls. May nest 539 * call due to the kfifo_alloc() and kmalloc() calls. May nest
540 * a call to the lock_cpu_hotplug()/unlock_cpu_hotplug() pair. 540 * a call to the get_online_cpus()/put_online_cpus() pair.
541 * Must not be called holding callback_mutex, because we must not 541 * Must not be called holding callback_mutex, because we must not
542 * call lock_cpu_hotplug() while holding callback_mutex. Elsewhere 542 * call get_online_cpus() while holding callback_mutex. Elsewhere
543 * the kernel nests callback_mutex inside lock_cpu_hotplug() calls. 543 * the kernel nests callback_mutex inside get_online_cpus() calls.
544 * So the reverse nesting would risk an ABBA deadlock. 544 * So the reverse nesting would risk an ABBA deadlock.
545 * 545 *
546 * The three key local variables below are: 546 * The three key local variables below are:
@@ -691,9 +691,9 @@ restart:
691 691
692rebuild: 692rebuild:
693 /* Have scheduler rebuild sched domains */ 693 /* Have scheduler rebuild sched domains */
694 lock_cpu_hotplug(); 694 get_online_cpus();
695 partition_sched_domains(ndoms, doms); 695 partition_sched_domains(ndoms, doms);
696 unlock_cpu_hotplug(); 696 put_online_cpus();
697 697
698done: 698done:
699 if (q && !IS_ERR(q)) 699 if (q && !IS_ERR(q))
@@ -1617,10 +1617,10 @@ static struct cgroup_subsys_state *cpuset_create(
1617 * 1617 *
1618 * If the cpuset being removed has its flag 'sched_load_balance' 1618 * If the cpuset being removed has its flag 'sched_load_balance'
1619 * enabled, then simulate turning sched_load_balance off, which 1619 * enabled, then simulate turning sched_load_balance off, which
1620 * will call rebuild_sched_domains(). The lock_cpu_hotplug() 1620 * will call rebuild_sched_domains(). The get_online_cpus()
1621 * call in rebuild_sched_domains() must not be made while holding 1621 * call in rebuild_sched_domains() must not be made while holding
1622 * callback_mutex. Elsewhere the kernel nests callback_mutex inside 1622 * callback_mutex. Elsewhere the kernel nests callback_mutex inside
1623 * lock_cpu_hotplug() calls. So the reverse nesting would risk an 1623 * get_online_cpus() calls. So the reverse nesting would risk an
1624 * ABBA deadlock. 1624 * ABBA deadlock.
1625 */ 1625 */
1626 1626
diff --git a/kernel/fork.c b/kernel/fork.c
index 8dd8ff281009..39d22b3357de 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1045,6 +1045,10 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1045 copy_flags(clone_flags, p); 1045 copy_flags(clone_flags, p);
1046 INIT_LIST_HEAD(&p->children); 1046 INIT_LIST_HEAD(&p->children);
1047 INIT_LIST_HEAD(&p->sibling); 1047 INIT_LIST_HEAD(&p->sibling);
1048#ifdef CONFIG_PREEMPT_RCU
1049 p->rcu_read_lock_nesting = 0;
1050 p->rcu_flipctr_idx = 0;
1051#endif /* #ifdef CONFIG_PREEMPT_RCU */
1048 p->vfork_done = NULL; 1052 p->vfork_done = NULL;
1049 spin_lock_init(&p->alloc_lock); 1053 spin_lock_init(&p->alloc_lock);
1050 1054
@@ -1059,6 +1063,11 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1059 p->prev_utime = cputime_zero; 1063 p->prev_utime = cputime_zero;
1060 p->prev_stime = cputime_zero; 1064 p->prev_stime = cputime_zero;
1061 1065
1066#ifdef CONFIG_DETECT_SOFTLOCKUP
1067 p->last_switch_count = 0;
1068 p->last_switch_timestamp = 0;
1069#endif
1070
1062#ifdef CONFIG_TASK_XACCT 1071#ifdef CONFIG_TASK_XACCT
1063 p->rchar = 0; /* I/O counter: bytes read */ 1072 p->rchar = 0; /* I/O counter: bytes read */
1064 p->wchar = 0; /* I/O counter: bytes written */ 1073 p->wchar = 0; /* I/O counter: bytes written */
@@ -1196,6 +1205,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1196#ifdef TIF_SYSCALL_EMU 1205#ifdef TIF_SYSCALL_EMU
1197 clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1206 clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1198#endif 1207#endif
1208 clear_all_latency_tracing(p);
1199 1209
1200 /* Our parent execution domain becomes current domain 1210 /* Our parent execution domain becomes current domain
1201 These must match for thread signalling to apply */ 1211 These must match for thread signalling to apply */
@@ -1237,6 +1247,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1237 * parent's CPU). This avoids alot of nasty races. 1247 * parent's CPU). This avoids alot of nasty races.
1238 */ 1248 */
1239 p->cpus_allowed = current->cpus_allowed; 1249 p->cpus_allowed = current->cpus_allowed;
1250 p->rt.nr_cpus_allowed = current->rt.nr_cpus_allowed;
1240 if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) || 1251 if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) ||
1241 !cpu_online(task_cpu(p)))) 1252 !cpu_online(task_cpu(p))))
1242 set_task_cpu(p, smp_processor_id()); 1253 set_task_cpu(p, smp_processor_id());
diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c
index f994bb8065e6..bd5d6b5060bc 100644
--- a/kernel/hrtimer.c
+++ b/kernel/hrtimer.c
@@ -325,6 +325,22 @@ unsigned long ktime_divns(const ktime_t kt, s64 div)
325} 325}
326#endif /* BITS_PER_LONG >= 64 */ 326#endif /* BITS_PER_LONG >= 64 */
327 327
328/*
329 * Check, whether the timer is on the callback pending list
330 */
331static inline int hrtimer_cb_pending(const struct hrtimer *timer)
332{
333 return timer->state & HRTIMER_STATE_PENDING;
334}
335
336/*
337 * Remove a timer from the callback pending list
338 */
339static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
340{
341 list_del_init(&timer->cb_entry);
342}
343
328/* High resolution timer related functions */ 344/* High resolution timer related functions */
329#ifdef CONFIG_HIGH_RES_TIMERS 345#ifdef CONFIG_HIGH_RES_TIMERS
330 346
@@ -494,29 +510,12 @@ void hres_timers_resume(void)
494} 510}
495 511
496/* 512/*
497 * Check, whether the timer is on the callback pending list
498 */
499static inline int hrtimer_cb_pending(const struct hrtimer *timer)
500{
501 return timer->state & HRTIMER_STATE_PENDING;
502}
503
504/*
505 * Remove a timer from the callback pending list
506 */
507static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
508{
509 list_del_init(&timer->cb_entry);
510}
511
512/*
513 * Initialize the high resolution related parts of cpu_base 513 * Initialize the high resolution related parts of cpu_base
514 */ 514 */
515static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) 515static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
516{ 516{
517 base->expires_next.tv64 = KTIME_MAX; 517 base->expires_next.tv64 = KTIME_MAX;
518 base->hres_active = 0; 518 base->hres_active = 0;
519 INIT_LIST_HEAD(&base->cb_pending);
520} 519}
521 520
522/* 521/*
@@ -524,7 +523,6 @@ static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
524 */ 523 */
525static inline void hrtimer_init_timer_hres(struct hrtimer *timer) 524static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
526{ 525{
527 INIT_LIST_HEAD(&timer->cb_entry);
528} 526}
529 527
530/* 528/*
@@ -618,10 +616,13 @@ static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
618{ 616{
619 return 0; 617 return 0;
620} 618}
621static inline int hrtimer_cb_pending(struct hrtimer *timer) { return 0; }
622static inline void hrtimer_remove_cb_pending(struct hrtimer *timer) { }
623static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { } 619static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
624static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { } 620static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
621static inline int hrtimer_reprogram(struct hrtimer *timer,
622 struct hrtimer_clock_base *base)
623{
624 return 0;
625}
625 626
626#endif /* CONFIG_HIGH_RES_TIMERS */ 627#endif /* CONFIG_HIGH_RES_TIMERS */
627 628
@@ -1001,6 +1002,7 @@ void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1001 clock_id = CLOCK_MONOTONIC; 1002 clock_id = CLOCK_MONOTONIC;
1002 1003
1003 timer->base = &cpu_base->clock_base[clock_id]; 1004 timer->base = &cpu_base->clock_base[clock_id];
1005 INIT_LIST_HEAD(&timer->cb_entry);
1004 hrtimer_init_timer_hres(timer); 1006 hrtimer_init_timer_hres(timer);
1005 1007
1006#ifdef CONFIG_TIMER_STATS 1008#ifdef CONFIG_TIMER_STATS
@@ -1030,6 +1032,85 @@ int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1030} 1032}
1031EXPORT_SYMBOL_GPL(hrtimer_get_res); 1033EXPORT_SYMBOL_GPL(hrtimer_get_res);
1032 1034
1035static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
1036{
1037 spin_lock_irq(&cpu_base->lock);
1038
1039 while (!list_empty(&cpu_base->cb_pending)) {
1040 enum hrtimer_restart (*fn)(struct hrtimer *);
1041 struct hrtimer *timer;
1042 int restart;
1043
1044 timer = list_entry(cpu_base->cb_pending.next,
1045 struct hrtimer, cb_entry);
1046
1047 timer_stats_account_hrtimer(timer);
1048
1049 fn = timer->function;
1050 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
1051 spin_unlock_irq(&cpu_base->lock);
1052
1053 restart = fn(timer);
1054
1055 spin_lock_irq(&cpu_base->lock);
1056
1057 timer->state &= ~HRTIMER_STATE_CALLBACK;
1058 if (restart == HRTIMER_RESTART) {
1059 BUG_ON(hrtimer_active(timer));
1060 /*
1061 * Enqueue the timer, allow reprogramming of the event
1062 * device
1063 */
1064 enqueue_hrtimer(timer, timer->base, 1);
1065 } else if (hrtimer_active(timer)) {
1066 /*
1067 * If the timer was rearmed on another CPU, reprogram
1068 * the event device.
1069 */
1070 if (timer->base->first == &timer->node)
1071 hrtimer_reprogram(timer, timer->base);
1072 }
1073 }
1074 spin_unlock_irq(&cpu_base->lock);
1075}
1076
1077static void __run_hrtimer(struct hrtimer *timer)
1078{
1079 struct hrtimer_clock_base *base = timer->base;
1080 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1081 enum hrtimer_restart (*fn)(struct hrtimer *);
1082 int restart;
1083
1084 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1085 timer_stats_account_hrtimer(timer);
1086
1087 fn = timer->function;
1088 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
1089 /*
1090 * Used for scheduler timers, avoid lock inversion with
1091 * rq->lock and tasklist_lock.
1092 *
1093 * These timers are required to deal with enqueue expiry
1094 * themselves and are not allowed to migrate.
1095 */
1096 spin_unlock(&cpu_base->lock);
1097 restart = fn(timer);
1098 spin_lock(&cpu_base->lock);
1099 } else
1100 restart = fn(timer);
1101
1102 /*
1103 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1104 * reprogramming of the event hardware. This happens at the end of this
1105 * function anyway.
1106 */
1107 if (restart != HRTIMER_NORESTART) {
1108 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1109 enqueue_hrtimer(timer, base, 0);
1110 }
1111 timer->state &= ~HRTIMER_STATE_CALLBACK;
1112}
1113
1033#ifdef CONFIG_HIGH_RES_TIMERS 1114#ifdef CONFIG_HIGH_RES_TIMERS
1034 1115
1035/* 1116/*
@@ -1087,21 +1168,7 @@ void hrtimer_interrupt(struct clock_event_device *dev)
1087 continue; 1168 continue;
1088 } 1169 }
1089 1170
1090 __remove_hrtimer(timer, base, 1171 __run_hrtimer(timer);
1091 HRTIMER_STATE_CALLBACK, 0);
1092 timer_stats_account_hrtimer(timer);
1093
1094 /*
1095 * Note: We clear the CALLBACK bit after
1096 * enqueue_hrtimer to avoid reprogramming of
1097 * the event hardware. This happens at the end
1098 * of this function anyway.
1099 */
1100 if (timer->function(timer) != HRTIMER_NORESTART) {
1101 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1102 enqueue_hrtimer(timer, base, 0);
1103 }
1104 timer->state &= ~HRTIMER_STATE_CALLBACK;
1105 } 1172 }
1106 spin_unlock(&cpu_base->lock); 1173 spin_unlock(&cpu_base->lock);
1107 base++; 1174 base++;
@@ -1122,52 +1189,41 @@ void hrtimer_interrupt(struct clock_event_device *dev)
1122 1189
1123static void run_hrtimer_softirq(struct softirq_action *h) 1190static void run_hrtimer_softirq(struct softirq_action *h)
1124{ 1191{
1125 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); 1192 run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
1126 1193}
1127 spin_lock_irq(&cpu_base->lock);
1128
1129 while (!list_empty(&cpu_base->cb_pending)) {
1130 enum hrtimer_restart (*fn)(struct hrtimer *);
1131 struct hrtimer *timer;
1132 int restart;
1133
1134 timer = list_entry(cpu_base->cb_pending.next,
1135 struct hrtimer, cb_entry);
1136 1194
1137 timer_stats_account_hrtimer(timer); 1195#endif /* CONFIG_HIGH_RES_TIMERS */
1138 1196
1139 fn = timer->function; 1197/*
1140 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0); 1198 * Called from timer softirq every jiffy, expire hrtimers:
1141 spin_unlock_irq(&cpu_base->lock); 1199 *
1200 * For HRT its the fall back code to run the softirq in the timer
1201 * softirq context in case the hrtimer initialization failed or has
1202 * not been done yet.
1203 */
1204void hrtimer_run_pending(void)
1205{
1206 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1142 1207
1143 restart = fn(timer); 1208 if (hrtimer_hres_active())
1209 return;
1144 1210
1145 spin_lock_irq(&cpu_base->lock); 1211 /*
1212 * This _is_ ugly: We have to check in the softirq context,
1213 * whether we can switch to highres and / or nohz mode. The
1214 * clocksource switch happens in the timer interrupt with
1215 * xtime_lock held. Notification from there only sets the
1216 * check bit in the tick_oneshot code, otherwise we might
1217 * deadlock vs. xtime_lock.
1218 */
1219 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1220 hrtimer_switch_to_hres();
1146 1221
1147 timer->state &= ~HRTIMER_STATE_CALLBACK; 1222 run_hrtimer_pending(cpu_base);
1148 if (restart == HRTIMER_RESTART) {
1149 BUG_ON(hrtimer_active(timer));
1150 /*
1151 * Enqueue the timer, allow reprogramming of the event
1152 * device
1153 */
1154 enqueue_hrtimer(timer, timer->base, 1);
1155 } else if (hrtimer_active(timer)) {
1156 /*
1157 * If the timer was rearmed on another CPU, reprogram
1158 * the event device.
1159 */
1160 if (timer->base->first == &timer->node)
1161 hrtimer_reprogram(timer, timer->base);
1162 }
1163 }
1164 spin_unlock_irq(&cpu_base->lock);
1165} 1223}
1166 1224
1167#endif /* CONFIG_HIGH_RES_TIMERS */
1168
1169/* 1225/*
1170 * Expire the per base hrtimer-queue: 1226 * Called from hardirq context every jiffy
1171 */ 1227 */
1172static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base, 1228static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
1173 int index) 1229 int index)
@@ -1181,46 +1237,27 @@ static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
1181 if (base->get_softirq_time) 1237 if (base->get_softirq_time)
1182 base->softirq_time = base->get_softirq_time(); 1238 base->softirq_time = base->get_softirq_time();
1183 1239
1184 spin_lock_irq(&cpu_base->lock); 1240 spin_lock(&cpu_base->lock);
1185 1241
1186 while ((node = base->first)) { 1242 while ((node = base->first)) {
1187 struct hrtimer *timer; 1243 struct hrtimer *timer;
1188 enum hrtimer_restart (*fn)(struct hrtimer *);
1189 int restart;
1190 1244
1191 timer = rb_entry(node, struct hrtimer, node); 1245 timer = rb_entry(node, struct hrtimer, node);
1192 if (base->softirq_time.tv64 <= timer->expires.tv64) 1246 if (base->softirq_time.tv64 <= timer->expires.tv64)
1193 break; 1247 break;
1194 1248
1195#ifdef CONFIG_HIGH_RES_TIMERS 1249 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1196 WARN_ON_ONCE(timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ); 1250 __remove_hrtimer(timer, base, HRTIMER_STATE_PENDING, 0);
1197#endif 1251 list_add_tail(&timer->cb_entry,
1198 timer_stats_account_hrtimer(timer); 1252 &base->cpu_base->cb_pending);
1199 1253 continue;
1200 fn = timer->function;
1201 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1202 spin_unlock_irq(&cpu_base->lock);
1203
1204 restart = fn(timer);
1205
1206 spin_lock_irq(&cpu_base->lock);
1207
1208 timer->state &= ~HRTIMER_STATE_CALLBACK;
1209 if (restart != HRTIMER_NORESTART) {
1210 BUG_ON(hrtimer_active(timer));
1211 enqueue_hrtimer(timer, base, 0);
1212 } 1254 }
1255
1256 __run_hrtimer(timer);
1213 } 1257 }
1214 spin_unlock_irq(&cpu_base->lock); 1258 spin_unlock(&cpu_base->lock);
1215} 1259}
1216 1260
1217/*
1218 * Called from timer softirq every jiffy, expire hrtimers:
1219 *
1220 * For HRT its the fall back code to run the softirq in the timer
1221 * softirq context in case the hrtimer initialization failed or has
1222 * not been done yet.
1223 */
1224void hrtimer_run_queues(void) 1261void hrtimer_run_queues(void)
1225{ 1262{
1226 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); 1263 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
@@ -1229,18 +1266,6 @@ void hrtimer_run_queues(void)
1229 if (hrtimer_hres_active()) 1266 if (hrtimer_hres_active())
1230 return; 1267 return;
1231 1268
1232 /*
1233 * This _is_ ugly: We have to check in the softirq context,
1234 * whether we can switch to highres and / or nohz mode. The
1235 * clocksource switch happens in the timer interrupt with
1236 * xtime_lock held. Notification from there only sets the
1237 * check bit in the tick_oneshot code, otherwise we might
1238 * deadlock vs. xtime_lock.
1239 */
1240 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1241 if (hrtimer_switch_to_hres())
1242 return;
1243
1244 hrtimer_get_softirq_time(cpu_base); 1269 hrtimer_get_softirq_time(cpu_base);
1245 1270
1246 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) 1271 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
@@ -1268,7 +1293,7 @@ void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1268 sl->timer.function = hrtimer_wakeup; 1293 sl->timer.function = hrtimer_wakeup;
1269 sl->task = task; 1294 sl->task = task;
1270#ifdef CONFIG_HIGH_RES_TIMERS 1295#ifdef CONFIG_HIGH_RES_TIMERS
1271 sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_RESTART; 1296 sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1272#endif 1297#endif
1273} 1298}
1274 1299
@@ -1279,6 +1304,8 @@ static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mod
1279 do { 1304 do {
1280 set_current_state(TASK_INTERRUPTIBLE); 1305 set_current_state(TASK_INTERRUPTIBLE);
1281 hrtimer_start(&t->timer, t->timer.expires, mode); 1306 hrtimer_start(&t->timer, t->timer.expires, mode);
1307 if (!hrtimer_active(&t->timer))
1308 t->task = NULL;
1282 1309
1283 if (likely(t->task)) 1310 if (likely(t->task))
1284 schedule(); 1311 schedule();
@@ -1389,6 +1416,7 @@ static void __cpuinit init_hrtimers_cpu(int cpu)
1389 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) 1416 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1390 cpu_base->clock_base[i].cpu_base = cpu_base; 1417 cpu_base->clock_base[i].cpu_base = cpu_base;
1391 1418
1419 INIT_LIST_HEAD(&cpu_base->cb_pending);
1392 hrtimer_init_hres(cpu_base); 1420 hrtimer_init_hres(cpu_base);
1393} 1421}
1394 1422
diff --git a/kernel/kthread.c b/kernel/kthread.c
index dcfe724300eb..0ac887882f90 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -15,6 +15,8 @@
15#include <linux/mutex.h> 15#include <linux/mutex.h>
16#include <asm/semaphore.h> 16#include <asm/semaphore.h>
17 17
18#define KTHREAD_NICE_LEVEL (-5)
19
18static DEFINE_SPINLOCK(kthread_create_lock); 20static DEFINE_SPINLOCK(kthread_create_lock);
19static LIST_HEAD(kthread_create_list); 21static LIST_HEAD(kthread_create_list);
20struct task_struct *kthreadd_task; 22struct task_struct *kthreadd_task;
@@ -94,10 +96,18 @@ static void create_kthread(struct kthread_create_info *create)
94 if (pid < 0) { 96 if (pid < 0) {
95 create->result = ERR_PTR(pid); 97 create->result = ERR_PTR(pid);
96 } else { 98 } else {
99 struct sched_param param = { .sched_priority = 0 };
97 wait_for_completion(&create->started); 100 wait_for_completion(&create->started);
98 read_lock(&tasklist_lock); 101 read_lock(&tasklist_lock);
99 create->result = find_task_by_pid(pid); 102 create->result = find_task_by_pid(pid);
100 read_unlock(&tasklist_lock); 103 read_unlock(&tasklist_lock);
104 /*
105 * root may have changed our (kthreadd's) priority or CPU mask.
106 * The kernel thread should not inherit these properties.
107 */
108 sched_setscheduler(create->result, SCHED_NORMAL, &param);
109 set_user_nice(create->result, KTHREAD_NICE_LEVEL);
110 set_cpus_allowed(create->result, CPU_MASK_ALL);
101 } 111 }
102 complete(&create->done); 112 complete(&create->done);
103} 113}
@@ -221,7 +231,7 @@ int kthreadd(void *unused)
221 /* Setup a clean context for our children to inherit. */ 231 /* Setup a clean context for our children to inherit. */
222 set_task_comm(tsk, "kthreadd"); 232 set_task_comm(tsk, "kthreadd");
223 ignore_signals(tsk); 233 ignore_signals(tsk);
224 set_user_nice(tsk, -5); 234 set_user_nice(tsk, KTHREAD_NICE_LEVEL);
225 set_cpus_allowed(tsk, CPU_MASK_ALL); 235 set_cpus_allowed(tsk, CPU_MASK_ALL);
226 236
227 current->flags |= PF_NOFREEZE; 237 current->flags |= PF_NOFREEZE;
diff --git a/kernel/latencytop.c b/kernel/latencytop.c
new file mode 100644
index 000000000000..b4e3c85abe74
--- /dev/null
+++ b/kernel/latencytop.c
@@ -0,0 +1,239 @@
1/*
2 * latencytop.c: Latency display infrastructure
3 *
4 * (C) Copyright 2008 Intel Corporation
5 * Author: Arjan van de Ven <arjan@linux.intel.com>
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; version 2
10 * of the License.
11 */
12#include <linux/latencytop.h>
13#include <linux/kallsyms.h>
14#include <linux/seq_file.h>
15#include <linux/notifier.h>
16#include <linux/spinlock.h>
17#include <linux/proc_fs.h>
18#include <linux/module.h>
19#include <linux/sched.h>
20#include <linux/list.h>
21#include <linux/slab.h>
22#include <linux/stacktrace.h>
23
24static DEFINE_SPINLOCK(latency_lock);
25
26#define MAXLR 128
27static struct latency_record latency_record[MAXLR];
28
29int latencytop_enabled;
30
31void clear_all_latency_tracing(struct task_struct *p)
32{
33 unsigned long flags;
34
35 if (!latencytop_enabled)
36 return;
37
38 spin_lock_irqsave(&latency_lock, flags);
39 memset(&p->latency_record, 0, sizeof(p->latency_record));
40 p->latency_record_count = 0;
41 spin_unlock_irqrestore(&latency_lock, flags);
42}
43
44static void clear_global_latency_tracing(void)
45{
46 unsigned long flags;
47
48 spin_lock_irqsave(&latency_lock, flags);
49 memset(&latency_record, 0, sizeof(latency_record));
50 spin_unlock_irqrestore(&latency_lock, flags);
51}
52
53static void __sched
54account_global_scheduler_latency(struct task_struct *tsk, struct latency_record *lat)
55{
56 int firstnonnull = MAXLR + 1;
57 int i;
58
59 if (!latencytop_enabled)
60 return;
61
62 /* skip kernel threads for now */
63 if (!tsk->mm)
64 return;
65
66 for (i = 0; i < MAXLR; i++) {
67 int q;
68 int same = 1;
69 /* Nothing stored: */
70 if (!latency_record[i].backtrace[0]) {
71 if (firstnonnull > i)
72 firstnonnull = i;
73 continue;
74 }
75 for (q = 0 ; q < LT_BACKTRACEDEPTH ; q++) {
76 if (latency_record[i].backtrace[q] !=
77 lat->backtrace[q])
78 same = 0;
79 if (same && lat->backtrace[q] == 0)
80 break;
81 if (same && lat->backtrace[q] == ULONG_MAX)
82 break;
83 }
84 if (same) {
85 latency_record[i].count++;
86 latency_record[i].time += lat->time;
87 if (lat->time > latency_record[i].max)
88 latency_record[i].max = lat->time;
89 return;
90 }
91 }
92
93 i = firstnonnull;
94 if (i >= MAXLR - 1)
95 return;
96
97 /* Allocted a new one: */
98 memcpy(&latency_record[i], lat, sizeof(struct latency_record));
99}
100
101static inline void store_stacktrace(struct task_struct *tsk, struct latency_record *lat)
102{
103 struct stack_trace trace;
104
105 memset(&trace, 0, sizeof(trace));
106 trace.max_entries = LT_BACKTRACEDEPTH;
107 trace.entries = &lat->backtrace[0];
108 trace.skip = 0;
109 save_stack_trace_tsk(tsk, &trace);
110}
111
112void __sched
113account_scheduler_latency(struct task_struct *tsk, int usecs, int inter)
114{
115 unsigned long flags;
116 int i, q;
117 struct latency_record lat;
118
119 if (!latencytop_enabled)
120 return;
121
122 /* Long interruptible waits are generally user requested... */
123 if (inter && usecs > 5000)
124 return;
125
126 memset(&lat, 0, sizeof(lat));
127 lat.count = 1;
128 lat.time = usecs;
129 lat.max = usecs;
130 store_stacktrace(tsk, &lat);
131
132 spin_lock_irqsave(&latency_lock, flags);
133
134 account_global_scheduler_latency(tsk, &lat);
135
136 /*
137 * short term hack; if we're > 32 we stop; future we recycle:
138 */
139 tsk->latency_record_count++;
140 if (tsk->latency_record_count >= LT_SAVECOUNT)
141 goto out_unlock;
142
143 for (i = 0; i < LT_SAVECOUNT ; i++) {
144 struct latency_record *mylat;
145 int same = 1;
146 mylat = &tsk->latency_record[i];
147 for (q = 0 ; q < LT_BACKTRACEDEPTH ; q++) {
148 if (mylat->backtrace[q] !=
149 lat.backtrace[q])
150 same = 0;
151 if (same && lat.backtrace[q] == 0)
152 break;
153 if (same && lat.backtrace[q] == ULONG_MAX)
154 break;
155 }
156 if (same) {
157 mylat->count++;
158 mylat->time += lat.time;
159 if (lat.time > mylat->max)
160 mylat->max = lat.time;
161 goto out_unlock;
162 }
163 }
164
165 /* Allocated a new one: */
166 i = tsk->latency_record_count;
167 memcpy(&tsk->latency_record[i], &lat, sizeof(struct latency_record));
168
169out_unlock:
170 spin_unlock_irqrestore(&latency_lock, flags);
171}
172
173static int lstats_show(struct seq_file *m, void *v)
174{
175 int i;
176
177 seq_puts(m, "Latency Top version : v0.1\n");
178
179 for (i = 0; i < MAXLR; i++) {
180 if (latency_record[i].backtrace[0]) {
181 int q;
182 seq_printf(m, "%i %li %li ",
183 latency_record[i].count,
184 latency_record[i].time,
185 latency_record[i].max);
186 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
187 char sym[KSYM_NAME_LEN];
188 char *c;
189 if (!latency_record[i].backtrace[q])
190 break;
191 if (latency_record[i].backtrace[q] == ULONG_MAX)
192 break;
193 sprint_symbol(sym, latency_record[i].backtrace[q]);
194 c = strchr(sym, '+');
195 if (c)
196 *c = 0;
197 seq_printf(m, "%s ", sym);
198 }
199 seq_printf(m, "\n");
200 }
201 }
202 return 0;
203}
204
205static ssize_t
206lstats_write(struct file *file, const char __user *buf, size_t count,
207 loff_t *offs)
208{
209 clear_global_latency_tracing();
210
211 return count;
212}
213
214static int lstats_open(struct inode *inode, struct file *filp)
215{
216 return single_open(filp, lstats_show, NULL);
217}
218
219static struct file_operations lstats_fops = {
220 .open = lstats_open,
221 .read = seq_read,
222 .write = lstats_write,
223 .llseek = seq_lseek,
224 .release = single_release,
225};
226
227static int __init init_lstats_procfs(void)
228{
229 struct proc_dir_entry *pe;
230
231 pe = create_proc_entry("latency_stats", 0644, NULL);
232 if (!pe)
233 return -ENOMEM;
234
235 pe->proc_fops = &lstats_fops;
236
237 return 0;
238}
239__initcall(init_lstats_procfs);
diff --git a/kernel/lockdep.c b/kernel/lockdep.c
index e2c07ece367d..3574379f4d62 100644
--- a/kernel/lockdep.c
+++ b/kernel/lockdep.c
@@ -3206,7 +3206,11 @@ retry:
3206 3206
3207EXPORT_SYMBOL_GPL(debug_show_all_locks); 3207EXPORT_SYMBOL_GPL(debug_show_all_locks);
3208 3208
3209void debug_show_held_locks(struct task_struct *task) 3209/*
3210 * Careful: only use this function if you are sure that
3211 * the task cannot run in parallel!
3212 */
3213void __debug_show_held_locks(struct task_struct *task)
3210{ 3214{
3211 if (unlikely(!debug_locks)) { 3215 if (unlikely(!debug_locks)) {
3212 printk("INFO: lockdep is turned off.\n"); 3216 printk("INFO: lockdep is turned off.\n");
@@ -3214,6 +3218,12 @@ void debug_show_held_locks(struct task_struct *task)
3214 } 3218 }
3215 lockdep_print_held_locks(task); 3219 lockdep_print_held_locks(task);
3216} 3220}
3221EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3222
3223void debug_show_held_locks(struct task_struct *task)
3224{
3225 __debug_show_held_locks(task);
3226}
3217 3227
3218EXPORT_SYMBOL_GPL(debug_show_held_locks); 3228EXPORT_SYMBOL_GPL(debug_show_held_locks);
3219 3229
diff --git a/kernel/module.c b/kernel/module.c
index dcb8a2cbf75e..1bb4c5e0d56e 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -496,6 +496,8 @@ static struct module_attribute modinfo_##field = { \
496MODINFO_ATTR(version); 496MODINFO_ATTR(version);
497MODINFO_ATTR(srcversion); 497MODINFO_ATTR(srcversion);
498 498
499static char last_unloaded_module[MODULE_NAME_LEN+1];
500
499#ifdef CONFIG_MODULE_UNLOAD 501#ifdef CONFIG_MODULE_UNLOAD
500/* Init the unload section of the module. */ 502/* Init the unload section of the module. */
501static void module_unload_init(struct module *mod) 503static void module_unload_init(struct module *mod)
@@ -719,6 +721,8 @@ sys_delete_module(const char __user *name_user, unsigned int flags)
719 mod->exit(); 721 mod->exit();
720 mutex_lock(&module_mutex); 722 mutex_lock(&module_mutex);
721 } 723 }
724 /* Store the name of the last unloaded module for diagnostic purposes */
725 sprintf(last_unloaded_module, mod->name);
722 free_module(mod); 726 free_module(mod);
723 727
724 out: 728 out:
@@ -2357,21 +2361,30 @@ static void m_stop(struct seq_file *m, void *p)
2357 mutex_unlock(&module_mutex); 2361 mutex_unlock(&module_mutex);
2358} 2362}
2359 2363
2360static char *taint_flags(unsigned int taints, char *buf) 2364static char *module_flags(struct module *mod, char *buf)
2361{ 2365{
2362 int bx = 0; 2366 int bx = 0;
2363 2367
2364 if (taints) { 2368 if (mod->taints ||
2369 mod->state == MODULE_STATE_GOING ||
2370 mod->state == MODULE_STATE_COMING) {
2365 buf[bx++] = '('; 2371 buf[bx++] = '(';
2366 if (taints & TAINT_PROPRIETARY_MODULE) 2372 if (mod->taints & TAINT_PROPRIETARY_MODULE)
2367 buf[bx++] = 'P'; 2373 buf[bx++] = 'P';
2368 if (taints & TAINT_FORCED_MODULE) 2374 if (mod->taints & TAINT_FORCED_MODULE)
2369 buf[bx++] = 'F'; 2375 buf[bx++] = 'F';
2370 /* 2376 /*
2371 * TAINT_FORCED_RMMOD: could be added. 2377 * TAINT_FORCED_RMMOD: could be added.
2372 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't 2378 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2373 * apply to modules. 2379 * apply to modules.
2374 */ 2380 */
2381
2382 /* Show a - for module-is-being-unloaded */
2383 if (mod->state == MODULE_STATE_GOING)
2384 buf[bx++] = '-';
2385 /* Show a + for module-is-being-loaded */
2386 if (mod->state == MODULE_STATE_COMING)
2387 buf[bx++] = '+';
2375 buf[bx++] = ')'; 2388 buf[bx++] = ')';
2376 } 2389 }
2377 buf[bx] = '\0'; 2390 buf[bx] = '\0';
@@ -2398,7 +2411,7 @@ static int m_show(struct seq_file *m, void *p)
2398 2411
2399 /* Taints info */ 2412 /* Taints info */
2400 if (mod->taints) 2413 if (mod->taints)
2401 seq_printf(m, " %s", taint_flags(mod->taints, buf)); 2414 seq_printf(m, " %s", module_flags(mod, buf));
2402 2415
2403 seq_printf(m, "\n"); 2416 seq_printf(m, "\n");
2404 return 0; 2417 return 0;
@@ -2493,7 +2506,9 @@ void print_modules(void)
2493 2506
2494 printk("Modules linked in:"); 2507 printk("Modules linked in:");
2495 list_for_each_entry(mod, &modules, list) 2508 list_for_each_entry(mod, &modules, list)
2496 printk(" %s%s", mod->name, taint_flags(mod->taints, buf)); 2509 printk(" %s%s", mod->name, module_flags(mod, buf));
2510 if (last_unloaded_module[0])
2511 printk(" [last unloaded: %s]", last_unloaded_module);
2497 printk("\n"); 2512 printk("\n");
2498} 2513}
2499 2514
diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c
index 68c96376e84a..0b7c82ac467e 100644
--- a/kernel/posix-cpu-timers.c
+++ b/kernel/posix-cpu-timers.c
@@ -967,6 +967,7 @@ static void check_thread_timers(struct task_struct *tsk,
967{ 967{
968 int maxfire; 968 int maxfire;
969 struct list_head *timers = tsk->cpu_timers; 969 struct list_head *timers = tsk->cpu_timers;
970 struct signal_struct *const sig = tsk->signal;
970 971
971 maxfire = 20; 972 maxfire = 20;
972 tsk->it_prof_expires = cputime_zero; 973 tsk->it_prof_expires = cputime_zero;
@@ -1011,6 +1012,35 @@ static void check_thread_timers(struct task_struct *tsk,
1011 t->firing = 1; 1012 t->firing = 1;
1012 list_move_tail(&t->entry, firing); 1013 list_move_tail(&t->entry, firing);
1013 } 1014 }
1015
1016 /*
1017 * Check for the special case thread timers.
1018 */
1019 if (sig->rlim[RLIMIT_RTTIME].rlim_cur != RLIM_INFINITY) {
1020 unsigned long hard = sig->rlim[RLIMIT_RTTIME].rlim_max;
1021 unsigned long *soft = &sig->rlim[RLIMIT_RTTIME].rlim_cur;
1022
1023 if (hard != RLIM_INFINITY &&
1024 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
1025 /*
1026 * At the hard limit, we just die.
1027 * No need to calculate anything else now.
1028 */
1029 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1030 return;
1031 }
1032 if (tsk->rt.timeout > DIV_ROUND_UP(*soft, USEC_PER_SEC/HZ)) {
1033 /*
1034 * At the soft limit, send a SIGXCPU every second.
1035 */
1036 if (sig->rlim[RLIMIT_RTTIME].rlim_cur
1037 < sig->rlim[RLIMIT_RTTIME].rlim_max) {
1038 sig->rlim[RLIMIT_RTTIME].rlim_cur +=
1039 USEC_PER_SEC;
1040 }
1041 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1042 }
1043 }
1014} 1044}
1015 1045
1016/* 1046/*
diff --git a/kernel/printk.c b/kernel/printk.c
index 89011bf8c106..423a8c765a57 100644
--- a/kernel/printk.c
+++ b/kernel/printk.c
@@ -573,11 +573,6 @@ static int __init printk_time_setup(char *str)
573 573
574__setup("time", printk_time_setup); 574__setup("time", printk_time_setup);
575 575
576__attribute__((weak)) unsigned long long printk_clock(void)
577{
578 return sched_clock();
579}
580
581/* Check if we have any console registered that can be called early in boot. */ 576/* Check if we have any console registered that can be called early in boot. */
582static int have_callable_console(void) 577static int have_callable_console(void)
583{ 578{
@@ -628,30 +623,57 @@ asmlinkage int printk(const char *fmt, ...)
628/* cpu currently holding logbuf_lock */ 623/* cpu currently holding logbuf_lock */
629static volatile unsigned int printk_cpu = UINT_MAX; 624static volatile unsigned int printk_cpu = UINT_MAX;
630 625
626const char printk_recursion_bug_msg [] =
627 KERN_CRIT "BUG: recent printk recursion!\n";
628static int printk_recursion_bug;
629
631asmlinkage int vprintk(const char *fmt, va_list args) 630asmlinkage int vprintk(const char *fmt, va_list args)
632{ 631{
632 static int log_level_unknown = 1;
633 static char printk_buf[1024];
634
633 unsigned long flags; 635 unsigned long flags;
634 int printed_len; 636 int printed_len = 0;
637 int this_cpu;
635 char *p; 638 char *p;
636 static char printk_buf[1024];
637 static int log_level_unknown = 1;
638 639
639 boot_delay_msec(); 640 boot_delay_msec();
640 641
641 preempt_disable(); 642 preempt_disable();
642 if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id())
643 /* If a crash is occurring during printk() on this CPU,
644 * make sure we can't deadlock */
645 zap_locks();
646
647 /* This stops the holder of console_sem just where we want him */ 643 /* This stops the holder of console_sem just where we want him */
648 raw_local_irq_save(flags); 644 raw_local_irq_save(flags);
645 this_cpu = smp_processor_id();
646
647 /*
648 * Ouch, printk recursed into itself!
649 */
650 if (unlikely(printk_cpu == this_cpu)) {
651 /*
652 * If a crash is occurring during printk() on this CPU,
653 * then try to get the crash message out but make sure
654 * we can't deadlock. Otherwise just return to avoid the
655 * recursion and return - but flag the recursion so that
656 * it can be printed at the next appropriate moment:
657 */
658 if (!oops_in_progress) {
659 printk_recursion_bug = 1;
660 goto out_restore_irqs;
661 }
662 zap_locks();
663 }
664
649 lockdep_off(); 665 lockdep_off();
650 spin_lock(&logbuf_lock); 666 spin_lock(&logbuf_lock);
651 printk_cpu = smp_processor_id(); 667 printk_cpu = this_cpu;
652 668
669 if (printk_recursion_bug) {
670 printk_recursion_bug = 0;
671 strcpy(printk_buf, printk_recursion_bug_msg);
672 printed_len = sizeof(printk_recursion_bug_msg);
673 }
653 /* Emit the output into the temporary buffer */ 674 /* Emit the output into the temporary buffer */
654 printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args); 675 printed_len += vscnprintf(printk_buf + printed_len,
676 sizeof(printk_buf), fmt, args);
655 677
656 /* 678 /*
657 * Copy the output into log_buf. If the caller didn't provide 679 * Copy the output into log_buf. If the caller didn't provide
@@ -680,7 +702,9 @@ asmlinkage int vprintk(const char *fmt, va_list args)
680 loglev_char = default_message_loglevel 702 loglev_char = default_message_loglevel
681 + '0'; 703 + '0';
682 } 704 }
683 t = printk_clock(); 705 t = 0;
706 if (system_state != SYSTEM_BOOTING)
707 t = ktime_to_ns(ktime_get());
684 nanosec_rem = do_div(t, 1000000000); 708 nanosec_rem = do_div(t, 1000000000);
685 tlen = sprintf(tbuf, 709 tlen = sprintf(tbuf,
686 "<%c>[%5lu.%06lu] ", 710 "<%c>[%5lu.%06lu] ",
@@ -744,6 +768,7 @@ asmlinkage int vprintk(const char *fmt, va_list args)
744 printk_cpu = UINT_MAX; 768 printk_cpu = UINT_MAX;
745 spin_unlock(&logbuf_lock); 769 spin_unlock(&logbuf_lock);
746 lockdep_on(); 770 lockdep_on();
771out_restore_irqs:
747 raw_local_irq_restore(flags); 772 raw_local_irq_restore(flags);
748 } 773 }
749 774
diff --git a/kernel/profile.c b/kernel/profile.c
index 5e95330e5120..e64c2da11c0f 100644
--- a/kernel/profile.c
+++ b/kernel/profile.c
@@ -52,7 +52,7 @@ static DEFINE_PER_CPU(int, cpu_profile_flip);
52static DEFINE_MUTEX(profile_flip_mutex); 52static DEFINE_MUTEX(profile_flip_mutex);
53#endif /* CONFIG_SMP */ 53#endif /* CONFIG_SMP */
54 54
55static int __init profile_setup(char * str) 55static int __init profile_setup(char *str)
56{ 56{
57 static char __initdata schedstr[] = "schedule"; 57 static char __initdata schedstr[] = "schedule";
58 static char __initdata sleepstr[] = "sleep"; 58 static char __initdata sleepstr[] = "sleep";
@@ -104,28 +104,28 @@ __setup("profile=", profile_setup);
104 104
105void __init profile_init(void) 105void __init profile_init(void)
106{ 106{
107 if (!prof_on) 107 if (!prof_on)
108 return; 108 return;
109 109
110 /* only text is profiled */ 110 /* only text is profiled */
111 prof_len = (_etext - _stext) >> prof_shift; 111 prof_len = (_etext - _stext) >> prof_shift;
112 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t)); 112 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
113} 113}
114 114
115/* Profile event notifications */ 115/* Profile event notifications */
116 116
117#ifdef CONFIG_PROFILING 117#ifdef CONFIG_PROFILING
118 118
119static BLOCKING_NOTIFIER_HEAD(task_exit_notifier); 119static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
120static ATOMIC_NOTIFIER_HEAD(task_free_notifier); 120static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
121static BLOCKING_NOTIFIER_HEAD(munmap_notifier); 121static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
122 122
123void profile_task_exit(struct task_struct * task) 123void profile_task_exit(struct task_struct *task)
124{ 124{
125 blocking_notifier_call_chain(&task_exit_notifier, 0, task); 125 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
126} 126}
127 127
128int profile_handoff_task(struct task_struct * task) 128int profile_handoff_task(struct task_struct *task)
129{ 129{
130 int ret; 130 int ret;
131 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task); 131 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
@@ -137,52 +137,55 @@ void profile_munmap(unsigned long addr)
137 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr); 137 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
138} 138}
139 139
140int task_handoff_register(struct notifier_block * n) 140int task_handoff_register(struct notifier_block *n)
141{ 141{
142 return atomic_notifier_chain_register(&task_free_notifier, n); 142 return atomic_notifier_chain_register(&task_free_notifier, n);
143} 143}
144EXPORT_SYMBOL_GPL(task_handoff_register);
144 145
145int task_handoff_unregister(struct notifier_block * n) 146int task_handoff_unregister(struct notifier_block *n)
146{ 147{
147 return atomic_notifier_chain_unregister(&task_free_notifier, n); 148 return atomic_notifier_chain_unregister(&task_free_notifier, n);
148} 149}
150EXPORT_SYMBOL_GPL(task_handoff_unregister);
149 151
150int profile_event_register(enum profile_type type, struct notifier_block * n) 152int profile_event_register(enum profile_type type, struct notifier_block *n)
151{ 153{
152 int err = -EINVAL; 154 int err = -EINVAL;
153 155
154 switch (type) { 156 switch (type) {
155 case PROFILE_TASK_EXIT: 157 case PROFILE_TASK_EXIT:
156 err = blocking_notifier_chain_register( 158 err = blocking_notifier_chain_register(
157 &task_exit_notifier, n); 159 &task_exit_notifier, n);
158 break; 160 break;
159 case PROFILE_MUNMAP: 161 case PROFILE_MUNMAP:
160 err = blocking_notifier_chain_register( 162 err = blocking_notifier_chain_register(
161 &munmap_notifier, n); 163 &munmap_notifier, n);
162 break; 164 break;
163 } 165 }
164 166
165 return err; 167 return err;
166} 168}
169EXPORT_SYMBOL_GPL(profile_event_register);
167 170
168 171int profile_event_unregister(enum profile_type type, struct notifier_block *n)
169int profile_event_unregister(enum profile_type type, struct notifier_block * n)
170{ 172{
171 int err = -EINVAL; 173 int err = -EINVAL;
172 174
173 switch (type) { 175 switch (type) {
174 case PROFILE_TASK_EXIT: 176 case PROFILE_TASK_EXIT:
175 err = blocking_notifier_chain_unregister( 177 err = blocking_notifier_chain_unregister(
176 &task_exit_notifier, n); 178 &task_exit_notifier, n);
177 break; 179 break;
178 case PROFILE_MUNMAP: 180 case PROFILE_MUNMAP:
179 err = blocking_notifier_chain_unregister( 181 err = blocking_notifier_chain_unregister(
180 &munmap_notifier, n); 182 &munmap_notifier, n);
181 break; 183 break;
182 } 184 }
183 185
184 return err; 186 return err;
185} 187}
188EXPORT_SYMBOL_GPL(profile_event_unregister);
186 189
187int register_timer_hook(int (*hook)(struct pt_regs *)) 190int register_timer_hook(int (*hook)(struct pt_regs *))
188{ 191{
@@ -191,6 +194,7 @@ int register_timer_hook(int (*hook)(struct pt_regs *))
191 timer_hook = hook; 194 timer_hook = hook;
192 return 0; 195 return 0;
193} 196}
197EXPORT_SYMBOL_GPL(register_timer_hook);
194 198
195void unregister_timer_hook(int (*hook)(struct pt_regs *)) 199void unregister_timer_hook(int (*hook)(struct pt_regs *))
196{ 200{
@@ -199,13 +203,7 @@ void unregister_timer_hook(int (*hook)(struct pt_regs *))
199 /* make sure all CPUs see the NULL hook */ 203 /* make sure all CPUs see the NULL hook */
200 synchronize_sched(); /* Allow ongoing interrupts to complete. */ 204 synchronize_sched(); /* Allow ongoing interrupts to complete. */
201} 205}
202
203EXPORT_SYMBOL_GPL(register_timer_hook);
204EXPORT_SYMBOL_GPL(unregister_timer_hook); 206EXPORT_SYMBOL_GPL(unregister_timer_hook);
205EXPORT_SYMBOL_GPL(task_handoff_register);
206EXPORT_SYMBOL_GPL(task_handoff_unregister);
207EXPORT_SYMBOL_GPL(profile_event_register);
208EXPORT_SYMBOL_GPL(profile_event_unregister);
209 207
210#endif /* CONFIG_PROFILING */ 208#endif /* CONFIG_PROFILING */
211 209
@@ -366,7 +364,7 @@ static int __devinit profile_cpu_callback(struct notifier_block *info,
366 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page); 364 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
367 } 365 }
368 break; 366 break;
369 out_free: 367out_free:
370 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]); 368 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
371 per_cpu(cpu_profile_hits, cpu)[1] = NULL; 369 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
372 __free_page(page); 370 __free_page(page);
@@ -409,7 +407,6 @@ void profile_hits(int type, void *__pc, unsigned int nr_hits)
409 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]); 407 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
410} 408}
411#endif /* !CONFIG_SMP */ 409#endif /* !CONFIG_SMP */
412
413EXPORT_SYMBOL_GPL(profile_hits); 410EXPORT_SYMBOL_GPL(profile_hits);
414 411
415void profile_tick(int type) 412void profile_tick(int type)
@@ -427,7 +424,7 @@ void profile_tick(int type)
427#include <asm/uaccess.h> 424#include <asm/uaccess.h>
428#include <asm/ptrace.h> 425#include <asm/ptrace.h>
429 426
430static int prof_cpu_mask_read_proc (char *page, char **start, off_t off, 427static int prof_cpu_mask_read_proc(char *page, char **start, off_t off,
431 int count, int *eof, void *data) 428 int count, int *eof, void *data)
432{ 429{
433 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data); 430 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
@@ -437,8 +434,8 @@ static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
437 return len; 434 return len;
438} 435}
439 436
440static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer, 437static int prof_cpu_mask_write_proc(struct file *file,
441 unsigned long count, void *data) 438 const char __user *buffer, unsigned long count, void *data)
442{ 439{
443 cpumask_t *mask = (cpumask_t *)data; 440 cpumask_t *mask = (cpumask_t *)data;
444 unsigned long full_count = count, err; 441 unsigned long full_count = count, err;
@@ -457,7 +454,8 @@ void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
457 struct proc_dir_entry *entry; 454 struct proc_dir_entry *entry;
458 455
459 /* create /proc/irq/prof_cpu_mask */ 456 /* create /proc/irq/prof_cpu_mask */
460 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir))) 457 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
458 if (!entry)
461 return; 459 return;
462 entry->data = (void *)&prof_cpu_mask; 460 entry->data = (void *)&prof_cpu_mask;
463 entry->read_proc = prof_cpu_mask_read_proc; 461 entry->read_proc = prof_cpu_mask_read_proc;
@@ -475,7 +473,7 @@ read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
475{ 473{
476 unsigned long p = *ppos; 474 unsigned long p = *ppos;
477 ssize_t read; 475 ssize_t read;
478 char * pnt; 476 char *pnt;
479 unsigned int sample_step = 1 << prof_shift; 477 unsigned int sample_step = 1 << prof_shift;
480 478
481 profile_flip_buffers(); 479 profile_flip_buffers();
@@ -486,12 +484,12 @@ read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
486 read = 0; 484 read = 0;
487 485
488 while (p < sizeof(unsigned int) && count > 0) { 486 while (p < sizeof(unsigned int) && count > 0) {
489 if (put_user(*((char *)(&sample_step)+p),buf)) 487 if (put_user(*((char *)(&sample_step)+p), buf))
490 return -EFAULT; 488 return -EFAULT;
491 buf++; p++; count--; read++; 489 buf++; p++; count--; read++;
492 } 490 }
493 pnt = (char *)prof_buffer + p - sizeof(atomic_t); 491 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
494 if (copy_to_user(buf,(void *)pnt,count)) 492 if (copy_to_user(buf, (void *)pnt, count))
495 return -EFAULT; 493 return -EFAULT;
496 read += count; 494 read += count;
497 *ppos += read; 495 *ppos += read;
@@ -508,7 +506,7 @@ static ssize_t write_profile(struct file *file, const char __user *buf,
508 size_t count, loff_t *ppos) 506 size_t count, loff_t *ppos)
509{ 507{
510#ifdef CONFIG_SMP 508#ifdef CONFIG_SMP
511 extern int setup_profiling_timer (unsigned int multiplier); 509 extern int setup_profiling_timer(unsigned int multiplier);
512 510
513 if (count == sizeof(int)) { 511 if (count == sizeof(int)) {
514 unsigned int multiplier; 512 unsigned int multiplier;
@@ -591,7 +589,8 @@ static int __init create_proc_profile(void)
591 return 0; 589 return 0;
592 if (create_hash_tables()) 590 if (create_hash_tables())
593 return -1; 591 return -1;
594 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL))) 592 entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL);
593 if (!entry)
595 return 0; 594 return 0;
596 entry->proc_fops = &proc_profile_operations; 595 entry->proc_fops = &proc_profile_operations;
597 entry->size = (1+prof_len) * sizeof(atomic_t); 596 entry->size = (1+prof_len) * sizeof(atomic_t);
diff --git a/kernel/rcuclassic.c b/kernel/rcuclassic.c
new file mode 100644
index 000000000000..f4ffbd0f306f
--- /dev/null
+++ b/kernel/rcuclassic.c
@@ -0,0 +1,575 @@
1/*
2 * Read-Copy Update mechanism for mutual exclusion
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; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright IBM Corporation, 2001
19 *
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
22 *
23 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
24 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
25 * Papers:
26 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
27 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
28 *
29 * For detailed explanation of Read-Copy Update mechanism see -
30 * Documentation/RCU
31 *
32 */
33#include <linux/types.h>
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/spinlock.h>
37#include <linux/smp.h>
38#include <linux/rcupdate.h>
39#include <linux/interrupt.h>
40#include <linux/sched.h>
41#include <asm/atomic.h>
42#include <linux/bitops.h>
43#include <linux/module.h>
44#include <linux/completion.h>
45#include <linux/moduleparam.h>
46#include <linux/percpu.h>
47#include <linux/notifier.h>
48#include <linux/cpu.h>
49#include <linux/mutex.h>
50
51#ifdef CONFIG_DEBUG_LOCK_ALLOC
52static struct lock_class_key rcu_lock_key;
53struct lockdep_map rcu_lock_map =
54 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
55EXPORT_SYMBOL_GPL(rcu_lock_map);
56#endif
57
58
59/* Definition for rcupdate control block. */
60static struct rcu_ctrlblk rcu_ctrlblk = {
61 .cur = -300,
62 .completed = -300,
63 .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock),
64 .cpumask = CPU_MASK_NONE,
65};
66static struct rcu_ctrlblk rcu_bh_ctrlblk = {
67 .cur = -300,
68 .completed = -300,
69 .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock),
70 .cpumask = CPU_MASK_NONE,
71};
72
73DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
74DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
75
76static int blimit = 10;
77static int qhimark = 10000;
78static int qlowmark = 100;
79
80#ifdef CONFIG_SMP
81static void force_quiescent_state(struct rcu_data *rdp,
82 struct rcu_ctrlblk *rcp)
83{
84 int cpu;
85 cpumask_t cpumask;
86 set_need_resched();
87 if (unlikely(!rcp->signaled)) {
88 rcp->signaled = 1;
89 /*
90 * Don't send IPI to itself. With irqs disabled,
91 * rdp->cpu is the current cpu.
92 */
93 cpumask = rcp->cpumask;
94 cpu_clear(rdp->cpu, cpumask);
95 for_each_cpu_mask(cpu, cpumask)
96 smp_send_reschedule(cpu);
97 }
98}
99#else
100static inline void force_quiescent_state(struct rcu_data *rdp,
101 struct rcu_ctrlblk *rcp)
102{
103 set_need_resched();
104}
105#endif
106
107/**
108 * call_rcu - Queue an RCU callback for invocation after a grace period.
109 * @head: structure to be used for queueing the RCU updates.
110 * @func: actual update function to be invoked after the grace period
111 *
112 * The update function will be invoked some time after a full grace
113 * period elapses, in other words after all currently executing RCU
114 * read-side critical sections have completed. RCU read-side critical
115 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
116 * and may be nested.
117 */
118void call_rcu(struct rcu_head *head,
119 void (*func)(struct rcu_head *rcu))
120{
121 unsigned long flags;
122 struct rcu_data *rdp;
123
124 head->func = func;
125 head->next = NULL;
126 local_irq_save(flags);
127 rdp = &__get_cpu_var(rcu_data);
128 *rdp->nxttail = head;
129 rdp->nxttail = &head->next;
130 if (unlikely(++rdp->qlen > qhimark)) {
131 rdp->blimit = INT_MAX;
132 force_quiescent_state(rdp, &rcu_ctrlblk);
133 }
134 local_irq_restore(flags);
135}
136EXPORT_SYMBOL_GPL(call_rcu);
137
138/**
139 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
140 * @head: structure to be used for queueing the RCU updates.
141 * @func: actual update function to be invoked after the grace period
142 *
143 * The update function will be invoked some time after a full grace
144 * period elapses, in other words after all currently executing RCU
145 * read-side critical sections have completed. call_rcu_bh() assumes
146 * that the read-side critical sections end on completion of a softirq
147 * handler. This means that read-side critical sections in process
148 * context must not be interrupted by softirqs. This interface is to be
149 * used when most of the read-side critical sections are in softirq context.
150 * RCU read-side critical sections are delimited by rcu_read_lock() and
151 * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
152 * and rcu_read_unlock_bh(), if in process context. These may be nested.
153 */
154void call_rcu_bh(struct rcu_head *head,
155 void (*func)(struct rcu_head *rcu))
156{
157 unsigned long flags;
158 struct rcu_data *rdp;
159
160 head->func = func;
161 head->next = NULL;
162 local_irq_save(flags);
163 rdp = &__get_cpu_var(rcu_bh_data);
164 *rdp->nxttail = head;
165 rdp->nxttail = &head->next;
166
167 if (unlikely(++rdp->qlen > qhimark)) {
168 rdp->blimit = INT_MAX;
169 force_quiescent_state(rdp, &rcu_bh_ctrlblk);
170 }
171
172 local_irq_restore(flags);
173}
174EXPORT_SYMBOL_GPL(call_rcu_bh);
175
176/*
177 * Return the number of RCU batches processed thus far. Useful
178 * for debug and statistics.
179 */
180long rcu_batches_completed(void)
181{
182 return rcu_ctrlblk.completed;
183}
184EXPORT_SYMBOL_GPL(rcu_batches_completed);
185
186/*
187 * Return the number of RCU batches processed thus far. Useful
188 * for debug and statistics.
189 */
190long rcu_batches_completed_bh(void)
191{
192 return rcu_bh_ctrlblk.completed;
193}
194EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
195
196/* Raises the softirq for processing rcu_callbacks. */
197static inline void raise_rcu_softirq(void)
198{
199 raise_softirq(RCU_SOFTIRQ);
200 /*
201 * The smp_mb() here is required to ensure that this cpu's
202 * __rcu_process_callbacks() reads the most recently updated
203 * value of rcu->cur.
204 */
205 smp_mb();
206}
207
208/*
209 * Invoke the completed RCU callbacks. They are expected to be in
210 * a per-cpu list.
211 */
212static void rcu_do_batch(struct rcu_data *rdp)
213{
214 struct rcu_head *next, *list;
215 int count = 0;
216
217 list = rdp->donelist;
218 while (list) {
219 next = list->next;
220 prefetch(next);
221 list->func(list);
222 list = next;
223 if (++count >= rdp->blimit)
224 break;
225 }
226 rdp->donelist = list;
227
228 local_irq_disable();
229 rdp->qlen -= count;
230 local_irq_enable();
231 if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark)
232 rdp->blimit = blimit;
233
234 if (!rdp->donelist)
235 rdp->donetail = &rdp->donelist;
236 else
237 raise_rcu_softirq();
238}
239
240/*
241 * Grace period handling:
242 * The grace period handling consists out of two steps:
243 * - A new grace period is started.
244 * This is done by rcu_start_batch. The start is not broadcasted to
245 * all cpus, they must pick this up by comparing rcp->cur with
246 * rdp->quiescbatch. All cpus are recorded in the
247 * rcu_ctrlblk.cpumask bitmap.
248 * - All cpus must go through a quiescent state.
249 * Since the start of the grace period is not broadcasted, at least two
250 * calls to rcu_check_quiescent_state are required:
251 * The first call just notices that a new grace period is running. The
252 * following calls check if there was a quiescent state since the beginning
253 * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If
254 * the bitmap is empty, then the grace period is completed.
255 * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
256 * period (if necessary).
257 */
258/*
259 * Register a new batch of callbacks, and start it up if there is currently no
260 * active batch and the batch to be registered has not already occurred.
261 * Caller must hold rcu_ctrlblk.lock.
262 */
263static void rcu_start_batch(struct rcu_ctrlblk *rcp)
264{
265 if (rcp->next_pending &&
266 rcp->completed == rcp->cur) {
267 rcp->next_pending = 0;
268 /*
269 * next_pending == 0 must be visible in
270 * __rcu_process_callbacks() before it can see new value of cur.
271 */
272 smp_wmb();
273 rcp->cur++;
274
275 /*
276 * Accessing nohz_cpu_mask before incrementing rcp->cur needs a
277 * Barrier Otherwise it can cause tickless idle CPUs to be
278 * included in rcp->cpumask, which will extend graceperiods
279 * unnecessarily.
280 */
281 smp_mb();
282 cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask);
283
284 rcp->signaled = 0;
285 }
286}
287
288/*
289 * cpu went through a quiescent state since the beginning of the grace period.
290 * Clear it from the cpu mask and complete the grace period if it was the last
291 * cpu. Start another grace period if someone has further entries pending
292 */
293static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
294{
295 cpu_clear(cpu, rcp->cpumask);
296 if (cpus_empty(rcp->cpumask)) {
297 /* batch completed ! */
298 rcp->completed = rcp->cur;
299 rcu_start_batch(rcp);
300 }
301}
302
303/*
304 * Check if the cpu has gone through a quiescent state (say context
305 * switch). If so and if it already hasn't done so in this RCU
306 * quiescent cycle, then indicate that it has done so.
307 */
308static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
309 struct rcu_data *rdp)
310{
311 if (rdp->quiescbatch != rcp->cur) {
312 /* start new grace period: */
313 rdp->qs_pending = 1;
314 rdp->passed_quiesc = 0;
315 rdp->quiescbatch = rcp->cur;
316 return;
317 }
318
319 /* Grace period already completed for this cpu?
320 * qs_pending is checked instead of the actual bitmap to avoid
321 * cacheline trashing.
322 */
323 if (!rdp->qs_pending)
324 return;
325
326 /*
327 * Was there a quiescent state since the beginning of the grace
328 * period? If no, then exit and wait for the next call.
329 */
330 if (!rdp->passed_quiesc)
331 return;
332 rdp->qs_pending = 0;
333
334 spin_lock(&rcp->lock);
335 /*
336 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
337 * during cpu startup. Ignore the quiescent state.
338 */
339 if (likely(rdp->quiescbatch == rcp->cur))
340 cpu_quiet(rdp->cpu, rcp);
341
342 spin_unlock(&rcp->lock);
343}
344
345
346#ifdef CONFIG_HOTPLUG_CPU
347
348/* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
349 * locking requirements, the list it's pulling from has to belong to a cpu
350 * which is dead and hence not processing interrupts.
351 */
352static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
353 struct rcu_head **tail)
354{
355 local_irq_disable();
356 *this_rdp->nxttail = list;
357 if (list)
358 this_rdp->nxttail = tail;
359 local_irq_enable();
360}
361
362static void __rcu_offline_cpu(struct rcu_data *this_rdp,
363 struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
364{
365 /* if the cpu going offline owns the grace period
366 * we can block indefinitely waiting for it, so flush
367 * it here
368 */
369 spin_lock_bh(&rcp->lock);
370 if (rcp->cur != rcp->completed)
371 cpu_quiet(rdp->cpu, rcp);
372 spin_unlock_bh(&rcp->lock);
373 rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail);
374 rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
375 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
376}
377
378static void rcu_offline_cpu(int cpu)
379{
380 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
381 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
382
383 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk,
384 &per_cpu(rcu_data, cpu));
385 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk,
386 &per_cpu(rcu_bh_data, cpu));
387 put_cpu_var(rcu_data);
388 put_cpu_var(rcu_bh_data);
389}
390
391#else
392
393static void rcu_offline_cpu(int cpu)
394{
395}
396
397#endif
398
399/*
400 * This does the RCU processing work from softirq context.
401 */
402static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
403 struct rcu_data *rdp)
404{
405 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
406 *rdp->donetail = rdp->curlist;
407 rdp->donetail = rdp->curtail;
408 rdp->curlist = NULL;
409 rdp->curtail = &rdp->curlist;
410 }
411
412 if (rdp->nxtlist && !rdp->curlist) {
413 local_irq_disable();
414 rdp->curlist = rdp->nxtlist;
415 rdp->curtail = rdp->nxttail;
416 rdp->nxtlist = NULL;
417 rdp->nxttail = &rdp->nxtlist;
418 local_irq_enable();
419
420 /*
421 * start the next batch of callbacks
422 */
423
424 /* determine batch number */
425 rdp->batch = rcp->cur + 1;
426 /* see the comment and corresponding wmb() in
427 * the rcu_start_batch()
428 */
429 smp_rmb();
430
431 if (!rcp->next_pending) {
432 /* and start it/schedule start if it's a new batch */
433 spin_lock(&rcp->lock);
434 rcp->next_pending = 1;
435 rcu_start_batch(rcp);
436 spin_unlock(&rcp->lock);
437 }
438 }
439
440 rcu_check_quiescent_state(rcp, rdp);
441 if (rdp->donelist)
442 rcu_do_batch(rdp);
443}
444
445static void rcu_process_callbacks(struct softirq_action *unused)
446{
447 __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
448 __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
449}
450
451static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
452{
453 /* This cpu has pending rcu entries and the grace period
454 * for them has completed.
455 */
456 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch))
457 return 1;
458
459 /* This cpu has no pending entries, but there are new entries */
460 if (!rdp->curlist && rdp->nxtlist)
461 return 1;
462
463 /* This cpu has finished callbacks to invoke */
464 if (rdp->donelist)
465 return 1;
466
467 /* The rcu core waits for a quiescent state from the cpu */
468 if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
469 return 1;
470
471 /* nothing to do */
472 return 0;
473}
474
475/*
476 * Check to see if there is any immediate RCU-related work to be done
477 * by the current CPU, returning 1 if so. This function is part of the
478 * RCU implementation; it is -not- an exported member of the RCU API.
479 */
480int rcu_pending(int cpu)
481{
482 return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) ||
483 __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu));
484}
485
486/*
487 * Check to see if any future RCU-related work will need to be done
488 * by the current CPU, even if none need be done immediately, returning
489 * 1 if so. This function is part of the RCU implementation; it is -not-
490 * an exported member of the RCU API.
491 */
492int rcu_needs_cpu(int cpu)
493{
494 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
495 struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu);
496
497 return (!!rdp->curlist || !!rdp_bh->curlist || rcu_pending(cpu));
498}
499
500void rcu_check_callbacks(int cpu, int user)
501{
502 if (user ||
503 (idle_cpu(cpu) && !in_softirq() &&
504 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
505 rcu_qsctr_inc(cpu);
506 rcu_bh_qsctr_inc(cpu);
507 } else if (!in_softirq())
508 rcu_bh_qsctr_inc(cpu);
509 raise_rcu_softirq();
510}
511
512static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
513 struct rcu_data *rdp)
514{
515 memset(rdp, 0, sizeof(*rdp));
516 rdp->curtail = &rdp->curlist;
517 rdp->nxttail = &rdp->nxtlist;
518 rdp->donetail = &rdp->donelist;
519 rdp->quiescbatch = rcp->completed;
520 rdp->qs_pending = 0;
521 rdp->cpu = cpu;
522 rdp->blimit = blimit;
523}
524
525static void __cpuinit rcu_online_cpu(int cpu)
526{
527 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
528 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
529
530 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
531 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
532 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks, NULL);
533}
534
535static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
536 unsigned long action, void *hcpu)
537{
538 long cpu = (long)hcpu;
539
540 switch (action) {
541 case CPU_UP_PREPARE:
542 case CPU_UP_PREPARE_FROZEN:
543 rcu_online_cpu(cpu);
544 break;
545 case CPU_DEAD:
546 case CPU_DEAD_FROZEN:
547 rcu_offline_cpu(cpu);
548 break;
549 default:
550 break;
551 }
552 return NOTIFY_OK;
553}
554
555static struct notifier_block __cpuinitdata rcu_nb = {
556 .notifier_call = rcu_cpu_notify,
557};
558
559/*
560 * Initializes rcu mechanism. Assumed to be called early.
561 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
562 * Note that rcu_qsctr and friends are implicitly
563 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
564 */
565void __init __rcu_init(void)
566{
567 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
568 (void *)(long)smp_processor_id());
569 /* Register notifier for non-boot CPUs */
570 register_cpu_notifier(&rcu_nb);
571}
572
573module_param(blimit, int, 0);
574module_param(qhimark, int, 0);
575module_param(qlowmark, int, 0);
diff --git a/kernel/rcupdate.c b/kernel/rcupdate.c
index f2c1a04e9b18..760dfc233a00 100644
--- a/kernel/rcupdate.c
+++ b/kernel/rcupdate.c
@@ -15,7 +15,7 @@
15 * along with this program; if not, write to the Free Software 15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * 17 *
18 * Copyright (C) IBM Corporation, 2001 18 * Copyright IBM Corporation, 2001
19 * 19 *
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com> 20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com> 21 * Manfred Spraul <manfred@colorfullife.com>
@@ -35,165 +35,57 @@
35#include <linux/init.h> 35#include <linux/init.h>
36#include <linux/spinlock.h> 36#include <linux/spinlock.h>
37#include <linux/smp.h> 37#include <linux/smp.h>
38#include <linux/rcupdate.h>
39#include <linux/interrupt.h> 38#include <linux/interrupt.h>
40#include <linux/sched.h> 39#include <linux/sched.h>
41#include <asm/atomic.h> 40#include <asm/atomic.h>
42#include <linux/bitops.h> 41#include <linux/bitops.h>
43#include <linux/module.h>
44#include <linux/completion.h> 42#include <linux/completion.h>
45#include <linux/moduleparam.h>
46#include <linux/percpu.h> 43#include <linux/percpu.h>
47#include <linux/notifier.h> 44#include <linux/notifier.h>
48#include <linux/cpu.h> 45#include <linux/cpu.h>
49#include <linux/mutex.h> 46#include <linux/mutex.h>
47#include <linux/module.h>
50 48
51#ifdef CONFIG_DEBUG_LOCK_ALLOC 49struct rcu_synchronize {
52static struct lock_class_key rcu_lock_key; 50 struct rcu_head head;
53struct lockdep_map rcu_lock_map = 51 struct completion completion;
54 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
55
56EXPORT_SYMBOL_GPL(rcu_lock_map);
57#endif
58
59/* Definition for rcupdate control block. */
60static struct rcu_ctrlblk rcu_ctrlblk = {
61 .cur = -300,
62 .completed = -300,
63 .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock),
64 .cpumask = CPU_MASK_NONE,
65};
66static struct rcu_ctrlblk rcu_bh_ctrlblk = {
67 .cur = -300,
68 .completed = -300,
69 .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock),
70 .cpumask = CPU_MASK_NONE,
71}; 52};
72 53
73DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L }; 54static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};
74DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
75
76/* Fake initialization required by compiler */
77static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL};
78static int blimit = 10;
79static int qhimark = 10000;
80static int qlowmark = 100;
81
82static atomic_t rcu_barrier_cpu_count; 55static atomic_t rcu_barrier_cpu_count;
83static DEFINE_MUTEX(rcu_barrier_mutex); 56static DEFINE_MUTEX(rcu_barrier_mutex);
84static struct completion rcu_barrier_completion; 57static struct completion rcu_barrier_completion;
85 58
86#ifdef CONFIG_SMP 59/* Because of FASTCALL declaration of complete, we use this wrapper */
87static void force_quiescent_state(struct rcu_data *rdp, 60static void wakeme_after_rcu(struct rcu_head *head)
88 struct rcu_ctrlblk *rcp)
89{
90 int cpu;
91 cpumask_t cpumask;
92 set_need_resched();
93 if (unlikely(!rcp->signaled)) {
94 rcp->signaled = 1;
95 /*
96 * Don't send IPI to itself. With irqs disabled,
97 * rdp->cpu is the current cpu.
98 */
99 cpumask = rcp->cpumask;
100 cpu_clear(rdp->cpu, cpumask);
101 for_each_cpu_mask(cpu, cpumask)
102 smp_send_reschedule(cpu);
103 }
104}
105#else
106static inline void force_quiescent_state(struct rcu_data *rdp,
107 struct rcu_ctrlblk *rcp)
108{ 61{
109 set_need_resched(); 62 struct rcu_synchronize *rcu;
63
64 rcu = container_of(head, struct rcu_synchronize, head);
65 complete(&rcu->completion);
110} 66}
111#endif
112 67
113/** 68/**
114 * call_rcu - Queue an RCU callback for invocation after a grace period. 69 * synchronize_rcu - wait until a grace period has elapsed.
115 * @head: structure to be used for queueing the RCU updates.
116 * @func: actual update function to be invoked after the grace period
117 * 70 *
118 * The update function will be invoked some time after a full grace 71 * Control will return to the caller some time after a full grace
119 * period elapses, in other words after all currently executing RCU 72 * period has elapsed, in other words after all currently executing RCU
120 * read-side critical sections have completed. RCU read-side critical 73 * read-side critical sections have completed. RCU read-side critical
121 * sections are delimited by rcu_read_lock() and rcu_read_unlock(), 74 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
122 * and may be nested. 75 * and may be nested.
123 */ 76 */
124void fastcall call_rcu(struct rcu_head *head, 77void synchronize_rcu(void)
125 void (*func)(struct rcu_head *rcu))
126{
127 unsigned long flags;
128 struct rcu_data *rdp;
129
130 head->func = func;
131 head->next = NULL;
132 local_irq_save(flags);
133 rdp = &__get_cpu_var(rcu_data);
134 *rdp->nxttail = head;
135 rdp->nxttail = &head->next;
136 if (unlikely(++rdp->qlen > qhimark)) {
137 rdp->blimit = INT_MAX;
138 force_quiescent_state(rdp, &rcu_ctrlblk);
139 }
140 local_irq_restore(flags);
141}
142
143/**
144 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
145 * @head: structure to be used for queueing the RCU updates.
146 * @func: actual update function to be invoked after the grace period
147 *
148 * The update function will be invoked some time after a full grace
149 * period elapses, in other words after all currently executing RCU
150 * read-side critical sections have completed. call_rcu_bh() assumes
151 * that the read-side critical sections end on completion of a softirq
152 * handler. This means that read-side critical sections in process
153 * context must not be interrupted by softirqs. This interface is to be
154 * used when most of the read-side critical sections are in softirq context.
155 * RCU read-side critical sections are delimited by rcu_read_lock() and
156 * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
157 * and rcu_read_unlock_bh(), if in process context. These may be nested.
158 */
159void fastcall call_rcu_bh(struct rcu_head *head,
160 void (*func)(struct rcu_head *rcu))
161{ 78{
162 unsigned long flags; 79 struct rcu_synchronize rcu;
163 struct rcu_data *rdp;
164
165 head->func = func;
166 head->next = NULL;
167 local_irq_save(flags);
168 rdp = &__get_cpu_var(rcu_bh_data);
169 *rdp->nxttail = head;
170 rdp->nxttail = &head->next;
171
172 if (unlikely(++rdp->qlen > qhimark)) {
173 rdp->blimit = INT_MAX;
174 force_quiescent_state(rdp, &rcu_bh_ctrlblk);
175 }
176
177 local_irq_restore(flags);
178}
179 80
180/* 81 init_completion(&rcu.completion);
181 * Return the number of RCU batches processed thus far. Useful 82 /* Will wake me after RCU finished */
182 * for debug and statistics. 83 call_rcu(&rcu.head, wakeme_after_rcu);
183 */
184long rcu_batches_completed(void)
185{
186 return rcu_ctrlblk.completed;
187}
188 84
189/* 85 /* Wait for it */
190 * Return the number of RCU batches processed thus far. Useful 86 wait_for_completion(&rcu.completion);
191 * for debug and statistics.
192 */
193long rcu_batches_completed_bh(void)
194{
195 return rcu_bh_ctrlblk.completed;
196} 87}
88EXPORT_SYMBOL_GPL(synchronize_rcu);
197 89
198static void rcu_barrier_callback(struct rcu_head *notused) 90static void rcu_barrier_callback(struct rcu_head *notused)
199{ 91{
@@ -207,10 +99,8 @@ static void rcu_barrier_callback(struct rcu_head *notused)
207static void rcu_barrier_func(void *notused) 99static void rcu_barrier_func(void *notused)
208{ 100{
209 int cpu = smp_processor_id(); 101 int cpu = smp_processor_id();
210 struct rcu_data *rdp = &per_cpu(rcu_data, cpu); 102 struct rcu_head *head = &per_cpu(rcu_barrier_head, cpu);
211 struct rcu_head *head;
212 103
213 head = &rdp->barrier;
214 atomic_inc(&rcu_barrier_cpu_count); 104 atomic_inc(&rcu_barrier_cpu_count);
215 call_rcu(head, rcu_barrier_callback); 105 call_rcu(head, rcu_barrier_callback);
216} 106}
@@ -225,420 +115,24 @@ void rcu_barrier(void)
225 mutex_lock(&rcu_barrier_mutex); 115 mutex_lock(&rcu_barrier_mutex);
226 init_completion(&rcu_barrier_completion); 116 init_completion(&rcu_barrier_completion);
227 atomic_set(&rcu_barrier_cpu_count, 0); 117 atomic_set(&rcu_barrier_cpu_count, 0);
118 /*
119 * The queueing of callbacks in all CPUs must be atomic with
120 * respect to RCU, otherwise one CPU may queue a callback,
121 * wait for a grace period, decrement barrier count and call
122 * complete(), while other CPUs have not yet queued anything.
123 * So, we need to make sure that grace periods cannot complete
124 * until all the callbacks are queued.
125 */
126 rcu_read_lock();
228 on_each_cpu(rcu_barrier_func, NULL, 0, 1); 127 on_each_cpu(rcu_barrier_func, NULL, 0, 1);
128 rcu_read_unlock();
229 wait_for_completion(&rcu_barrier_completion); 129 wait_for_completion(&rcu_barrier_completion);
230 mutex_unlock(&rcu_barrier_mutex); 130 mutex_unlock(&rcu_barrier_mutex);
231} 131}
232EXPORT_SYMBOL_GPL(rcu_barrier); 132EXPORT_SYMBOL_GPL(rcu_barrier);
233 133
234/*
235 * Invoke the completed RCU callbacks. They are expected to be in
236 * a per-cpu list.
237 */
238static void rcu_do_batch(struct rcu_data *rdp)
239{
240 struct rcu_head *next, *list;
241 int count = 0;
242
243 list = rdp->donelist;
244 while (list) {
245 next = list->next;
246 prefetch(next);
247 list->func(list);
248 list = next;
249 if (++count >= rdp->blimit)
250 break;
251 }
252 rdp->donelist = list;
253
254 local_irq_disable();
255 rdp->qlen -= count;
256 local_irq_enable();
257 if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark)
258 rdp->blimit = blimit;
259
260 if (!rdp->donelist)
261 rdp->donetail = &rdp->donelist;
262 else
263 tasklet_schedule(&per_cpu(rcu_tasklet, rdp->cpu));
264}
265
266/*
267 * Grace period handling:
268 * The grace period handling consists out of two steps:
269 * - A new grace period is started.
270 * This is done by rcu_start_batch. The start is not broadcasted to
271 * all cpus, they must pick this up by comparing rcp->cur with
272 * rdp->quiescbatch. All cpus are recorded in the
273 * rcu_ctrlblk.cpumask bitmap.
274 * - All cpus must go through a quiescent state.
275 * Since the start of the grace period is not broadcasted, at least two
276 * calls to rcu_check_quiescent_state are required:
277 * The first call just notices that a new grace period is running. The
278 * following calls check if there was a quiescent state since the beginning
279 * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If
280 * the bitmap is empty, then the grace period is completed.
281 * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
282 * period (if necessary).
283 */
284/*
285 * Register a new batch of callbacks, and start it up if there is currently no
286 * active batch and the batch to be registered has not already occurred.
287 * Caller must hold rcu_ctrlblk.lock.
288 */
289static void rcu_start_batch(struct rcu_ctrlblk *rcp)
290{
291 if (rcp->next_pending &&
292 rcp->completed == rcp->cur) {
293 rcp->next_pending = 0;
294 /*
295 * next_pending == 0 must be visible in
296 * __rcu_process_callbacks() before it can see new value of cur.
297 */
298 smp_wmb();
299 rcp->cur++;
300
301 /*
302 * Accessing nohz_cpu_mask before incrementing rcp->cur needs a
303 * Barrier Otherwise it can cause tickless idle CPUs to be
304 * included in rcp->cpumask, which will extend graceperiods
305 * unnecessarily.
306 */
307 smp_mb();
308 cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask);
309
310 rcp->signaled = 0;
311 }
312}
313
314/*
315 * cpu went through a quiescent state since the beginning of the grace period.
316 * Clear it from the cpu mask and complete the grace period if it was the last
317 * cpu. Start another grace period if someone has further entries pending
318 */
319static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
320{
321 cpu_clear(cpu, rcp->cpumask);
322 if (cpus_empty(rcp->cpumask)) {
323 /* batch completed ! */
324 rcp->completed = rcp->cur;
325 rcu_start_batch(rcp);
326 }
327}
328
329/*
330 * Check if the cpu has gone through a quiescent state (say context
331 * switch). If so and if it already hasn't done so in this RCU
332 * quiescent cycle, then indicate that it has done so.
333 */
334static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
335 struct rcu_data *rdp)
336{
337 if (rdp->quiescbatch != rcp->cur) {
338 /* start new grace period: */
339 rdp->qs_pending = 1;
340 rdp->passed_quiesc = 0;
341 rdp->quiescbatch = rcp->cur;
342 return;
343 }
344
345 /* Grace period already completed for this cpu?
346 * qs_pending is checked instead of the actual bitmap to avoid
347 * cacheline trashing.
348 */
349 if (!rdp->qs_pending)
350 return;
351
352 /*
353 * Was there a quiescent state since the beginning of the grace
354 * period? If no, then exit and wait for the next call.
355 */
356 if (!rdp->passed_quiesc)
357 return;
358 rdp->qs_pending = 0;
359
360 spin_lock(&rcp->lock);
361 /*
362 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
363 * during cpu startup. Ignore the quiescent state.
364 */
365 if (likely(rdp->quiescbatch == rcp->cur))
366 cpu_quiet(rdp->cpu, rcp);
367
368 spin_unlock(&rcp->lock);
369}
370
371
372#ifdef CONFIG_HOTPLUG_CPU
373
374/* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
375 * locking requirements, the list it's pulling from has to belong to a cpu
376 * which is dead and hence not processing interrupts.
377 */
378static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
379 struct rcu_head **tail)
380{
381 local_irq_disable();
382 *this_rdp->nxttail = list;
383 if (list)
384 this_rdp->nxttail = tail;
385 local_irq_enable();
386}
387
388static void __rcu_offline_cpu(struct rcu_data *this_rdp,
389 struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
390{
391 /* if the cpu going offline owns the grace period
392 * we can block indefinitely waiting for it, so flush
393 * it here
394 */
395 spin_lock_bh(&rcp->lock);
396 if (rcp->cur != rcp->completed)
397 cpu_quiet(rdp->cpu, rcp);
398 spin_unlock_bh(&rcp->lock);
399 rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
400 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
401 rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail);
402}
403
404static void rcu_offline_cpu(int cpu)
405{
406 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
407 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
408
409 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk,
410 &per_cpu(rcu_data, cpu));
411 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk,
412 &per_cpu(rcu_bh_data, cpu));
413 put_cpu_var(rcu_data);
414 put_cpu_var(rcu_bh_data);
415 tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu);
416}
417
418#else
419
420static void rcu_offline_cpu(int cpu)
421{
422}
423
424#endif
425
426/*
427 * This does the RCU processing work from tasklet context.
428 */
429static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
430 struct rcu_data *rdp)
431{
432 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
433 *rdp->donetail = rdp->curlist;
434 rdp->donetail = rdp->curtail;
435 rdp->curlist = NULL;
436 rdp->curtail = &rdp->curlist;
437 }
438
439 if (rdp->nxtlist && !rdp->curlist) {
440 local_irq_disable();
441 rdp->curlist = rdp->nxtlist;
442 rdp->curtail = rdp->nxttail;
443 rdp->nxtlist = NULL;
444 rdp->nxttail = &rdp->nxtlist;
445 local_irq_enable();
446
447 /*
448 * start the next batch of callbacks
449 */
450
451 /* determine batch number */
452 rdp->batch = rcp->cur + 1;
453 /* see the comment and corresponding wmb() in
454 * the rcu_start_batch()
455 */
456 smp_rmb();
457
458 if (!rcp->next_pending) {
459 /* and start it/schedule start if it's a new batch */
460 spin_lock(&rcp->lock);
461 rcp->next_pending = 1;
462 rcu_start_batch(rcp);
463 spin_unlock(&rcp->lock);
464 }
465 }
466
467 rcu_check_quiescent_state(rcp, rdp);
468 if (rdp->donelist)
469 rcu_do_batch(rdp);
470}
471
472static void rcu_process_callbacks(unsigned long unused)
473{
474 __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
475 __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
476}
477
478static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
479{
480 /* This cpu has pending rcu entries and the grace period
481 * for them has completed.
482 */
483 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch))
484 return 1;
485
486 /* This cpu has no pending entries, but there are new entries */
487 if (!rdp->curlist && rdp->nxtlist)
488 return 1;
489
490 /* This cpu has finished callbacks to invoke */
491 if (rdp->donelist)
492 return 1;
493
494 /* The rcu core waits for a quiescent state from the cpu */
495 if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
496 return 1;
497
498 /* nothing to do */
499 return 0;
500}
501
502/*
503 * Check to see if there is any immediate RCU-related work to be done
504 * by the current CPU, returning 1 if so. This function is part of the
505 * RCU implementation; it is -not- an exported member of the RCU API.
506 */
507int rcu_pending(int cpu)
508{
509 return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) ||
510 __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu));
511}
512
513/*
514 * Check to see if any future RCU-related work will need to be done
515 * by the current CPU, even if none need be done immediately, returning
516 * 1 if so. This function is part of the RCU implementation; it is -not-
517 * an exported member of the RCU API.
518 */
519int rcu_needs_cpu(int cpu)
520{
521 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
522 struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu);
523
524 return (!!rdp->curlist || !!rdp_bh->curlist || rcu_pending(cpu));
525}
526
527void rcu_check_callbacks(int cpu, int user)
528{
529 if (user ||
530 (idle_cpu(cpu) && !in_softirq() &&
531 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
532 rcu_qsctr_inc(cpu);
533 rcu_bh_qsctr_inc(cpu);
534 } else if (!in_softirq())
535 rcu_bh_qsctr_inc(cpu);
536 tasklet_schedule(&per_cpu(rcu_tasklet, cpu));
537}
538
539static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
540 struct rcu_data *rdp)
541{
542 memset(rdp, 0, sizeof(*rdp));
543 rdp->curtail = &rdp->curlist;
544 rdp->nxttail = &rdp->nxtlist;
545 rdp->donetail = &rdp->donelist;
546 rdp->quiescbatch = rcp->completed;
547 rdp->qs_pending = 0;
548 rdp->cpu = cpu;
549 rdp->blimit = blimit;
550}
551
552static void __cpuinit rcu_online_cpu(int cpu)
553{
554 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
555 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
556
557 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
558 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
559 tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL);
560}
561
562static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
563 unsigned long action, void *hcpu)
564{
565 long cpu = (long)hcpu;
566 switch (action) {
567 case CPU_UP_PREPARE:
568 case CPU_UP_PREPARE_FROZEN:
569 rcu_online_cpu(cpu);
570 break;
571 case CPU_DEAD:
572 case CPU_DEAD_FROZEN:
573 rcu_offline_cpu(cpu);
574 break;
575 default:
576 break;
577 }
578 return NOTIFY_OK;
579}
580
581static struct notifier_block __cpuinitdata rcu_nb = {
582 .notifier_call = rcu_cpu_notify,
583};
584
585/*
586 * Initializes rcu mechanism. Assumed to be called early.
587 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
588 * Note that rcu_qsctr and friends are implicitly
589 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
590 */
591void __init rcu_init(void) 134void __init rcu_init(void)
592{ 135{
593 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, 136 __rcu_init();
594 (void *)(long)smp_processor_id());
595 /* Register notifier for non-boot CPUs */
596 register_cpu_notifier(&rcu_nb);
597}
598
599struct rcu_synchronize {
600 struct rcu_head head;
601 struct completion completion;
602};
603
604/* Because of FASTCALL declaration of complete, we use this wrapper */
605static void wakeme_after_rcu(struct rcu_head *head)
606{
607 struct rcu_synchronize *rcu;
608
609 rcu = container_of(head, struct rcu_synchronize, head);
610 complete(&rcu->completion);
611} 137}
612 138
613/**
614 * synchronize_rcu - wait until a grace period has elapsed.
615 *
616 * Control will return to the caller some time after a full grace
617 * period has elapsed, in other words after all currently executing RCU
618 * read-side critical sections have completed. RCU read-side critical
619 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
620 * and may be nested.
621 *
622 * If your read-side code is not protected by rcu_read_lock(), do -not-
623 * use synchronize_rcu().
624 */
625void synchronize_rcu(void)
626{
627 struct rcu_synchronize rcu;
628
629 init_completion(&rcu.completion);
630 /* Will wake me after RCU finished */
631 call_rcu(&rcu.head, wakeme_after_rcu);
632
633 /* Wait for it */
634 wait_for_completion(&rcu.completion);
635}
636
637module_param(blimit, int, 0);
638module_param(qhimark, int, 0);
639module_param(qlowmark, int, 0);
640EXPORT_SYMBOL_GPL(rcu_batches_completed);
641EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
642EXPORT_SYMBOL_GPL(call_rcu);
643EXPORT_SYMBOL_GPL(call_rcu_bh);
644EXPORT_SYMBOL_GPL(synchronize_rcu);
diff --git a/kernel/rcupreempt.c b/kernel/rcupreempt.c
new file mode 100644
index 000000000000..987cfb7ade89
--- /dev/null
+++ b/kernel/rcupreempt.c
@@ -0,0 +1,953 @@
1/*
2 * Read-Copy Update mechanism for mutual exclusion, realtime implementation
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; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright IBM Corporation, 2006
19 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21 * With thanks to Esben Nielsen, Bill Huey, and Ingo Molnar
22 * for pushing me away from locks and towards counters, and
23 * to Suparna Bhattacharya for pushing me completely away
24 * from atomic instructions on the read side.
25 *
26 * Papers: http://www.rdrop.com/users/paulmck/RCU
27 *
28 * Design Document: http://lwn.net/Articles/253651/
29 *
30 * For detailed explanation of Read-Copy Update mechanism see -
31 * Documentation/RCU/ *.txt
32 *
33 */
34#include <linux/types.h>
35#include <linux/kernel.h>
36#include <linux/init.h>
37#include <linux/spinlock.h>
38#include <linux/smp.h>
39#include <linux/rcupdate.h>
40#include <linux/interrupt.h>
41#include <linux/sched.h>
42#include <asm/atomic.h>
43#include <linux/bitops.h>
44#include <linux/module.h>
45#include <linux/completion.h>
46#include <linux/moduleparam.h>
47#include <linux/percpu.h>
48#include <linux/notifier.h>
49#include <linux/rcupdate.h>
50#include <linux/cpu.h>
51#include <linux/random.h>
52#include <linux/delay.h>
53#include <linux/byteorder/swabb.h>
54#include <linux/cpumask.h>
55#include <linux/rcupreempt_trace.h>
56
57/*
58 * Macro that prevents the compiler from reordering accesses, but does
59 * absolutely -nothing- to prevent CPUs from reordering. This is used
60 * only to mediate communication between mainline code and hardware
61 * interrupt and NMI handlers.
62 */
63#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
64
65/*
66 * PREEMPT_RCU data structures.
67 */
68
69/*
70 * GP_STAGES specifies the number of times the state machine has
71 * to go through the all the rcu_try_flip_states (see below)
72 * in a single Grace Period.
73 *
74 * GP in GP_STAGES stands for Grace Period ;)
75 */
76#define GP_STAGES 2
77struct rcu_data {
78 spinlock_t lock; /* Protect rcu_data fields. */
79 long completed; /* Number of last completed batch. */
80 int waitlistcount;
81 struct tasklet_struct rcu_tasklet;
82 struct rcu_head *nextlist;
83 struct rcu_head **nexttail;
84 struct rcu_head *waitlist[GP_STAGES];
85 struct rcu_head **waittail[GP_STAGES];
86 struct rcu_head *donelist;
87 struct rcu_head **donetail;
88 long rcu_flipctr[2];
89#ifdef CONFIG_RCU_TRACE
90 struct rcupreempt_trace trace;
91#endif /* #ifdef CONFIG_RCU_TRACE */
92};
93
94/*
95 * States for rcu_try_flip() and friends.
96 */
97
98enum rcu_try_flip_states {
99
100 /*
101 * Stay here if nothing is happening. Flip the counter if somthing
102 * starts happening. Denoted by "I"
103 */
104 rcu_try_flip_idle_state,
105
106 /*
107 * Wait here for all CPUs to notice that the counter has flipped. This
108 * prevents the old set of counters from ever being incremented once
109 * we leave this state, which in turn is necessary because we cannot
110 * test any individual counter for zero -- we can only check the sum.
111 * Denoted by "A".
112 */
113 rcu_try_flip_waitack_state,
114
115 /*
116 * Wait here for the sum of the old per-CPU counters to reach zero.
117 * Denoted by "Z".
118 */
119 rcu_try_flip_waitzero_state,
120
121 /*
122 * Wait here for each of the other CPUs to execute a memory barrier.
123 * This is necessary to ensure that these other CPUs really have
124 * completed executing their RCU read-side critical sections, despite
125 * their CPUs wildly reordering memory. Denoted by "M".
126 */
127 rcu_try_flip_waitmb_state,
128};
129
130struct rcu_ctrlblk {
131 spinlock_t fliplock; /* Protect state-machine transitions. */
132 long completed; /* Number of last completed batch. */
133 enum rcu_try_flip_states rcu_try_flip_state; /* The current state of
134 the rcu state machine */
135};
136
137static DEFINE_PER_CPU(struct rcu_data, rcu_data);
138static struct rcu_ctrlblk rcu_ctrlblk = {
139 .fliplock = __SPIN_LOCK_UNLOCKED(rcu_ctrlblk.fliplock),
140 .completed = 0,
141 .rcu_try_flip_state = rcu_try_flip_idle_state,
142};
143
144
145#ifdef CONFIG_RCU_TRACE
146static char *rcu_try_flip_state_names[] =
147 { "idle", "waitack", "waitzero", "waitmb" };
148#endif /* #ifdef CONFIG_RCU_TRACE */
149
150static cpumask_t rcu_cpu_online_map __read_mostly = CPU_MASK_NONE;
151
152/*
153 * Enum and per-CPU flag to determine when each CPU has seen
154 * the most recent counter flip.
155 */
156
157enum rcu_flip_flag_values {
158 rcu_flip_seen, /* Steady/initial state, last flip seen. */
159 /* Only GP detector can update. */
160 rcu_flipped /* Flip just completed, need confirmation. */
161 /* Only corresponding CPU can update. */
162};
163static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_flip_flag_values, rcu_flip_flag)
164 = rcu_flip_seen;
165
166/*
167 * Enum and per-CPU flag to determine when each CPU has executed the
168 * needed memory barrier to fence in memory references from its last RCU
169 * read-side critical section in the just-completed grace period.
170 */
171
172enum rcu_mb_flag_values {
173 rcu_mb_done, /* Steady/initial state, no mb()s required. */
174 /* Only GP detector can update. */
175 rcu_mb_needed /* Flip just completed, need an mb(). */
176 /* Only corresponding CPU can update. */
177};
178static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_mb_flag_values, rcu_mb_flag)
179 = rcu_mb_done;
180
181/*
182 * RCU_DATA_ME: find the current CPU's rcu_data structure.
183 * RCU_DATA_CPU: find the specified CPU's rcu_data structure.
184 */
185#define RCU_DATA_ME() (&__get_cpu_var(rcu_data))
186#define RCU_DATA_CPU(cpu) (&per_cpu(rcu_data, cpu))
187
188/*
189 * Helper macro for tracing when the appropriate rcu_data is not
190 * cached in a local variable, but where the CPU number is so cached.
191 */
192#define RCU_TRACE_CPU(f, cpu) RCU_TRACE(f, &(RCU_DATA_CPU(cpu)->trace));
193
194/*
195 * Helper macro for tracing when the appropriate rcu_data is not
196 * cached in a local variable.
197 */
198#define RCU_TRACE_ME(f) RCU_TRACE(f, &(RCU_DATA_ME()->trace));
199
200/*
201 * Helper macro for tracing when the appropriate rcu_data is pointed
202 * to by a local variable.
203 */
204#define RCU_TRACE_RDP(f, rdp) RCU_TRACE(f, &((rdp)->trace));
205
206/*
207 * Return the number of RCU batches processed thus far. Useful
208 * for debug and statistics.
209 */
210long rcu_batches_completed(void)
211{
212 return rcu_ctrlblk.completed;
213}
214EXPORT_SYMBOL_GPL(rcu_batches_completed);
215
216EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
217
218void __rcu_read_lock(void)
219{
220 int idx;
221 struct task_struct *t = current;
222 int nesting;
223
224 nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
225 if (nesting != 0) {
226
227 /* An earlier rcu_read_lock() covers us, just count it. */
228
229 t->rcu_read_lock_nesting = nesting + 1;
230
231 } else {
232 unsigned long flags;
233
234 /*
235 * We disable interrupts for the following reasons:
236 * - If we get scheduling clock interrupt here, and we
237 * end up acking the counter flip, it's like a promise
238 * that we will never increment the old counter again.
239 * Thus we will break that promise if that
240 * scheduling clock interrupt happens between the time
241 * we pick the .completed field and the time that we
242 * increment our counter.
243 *
244 * - We don't want to be preempted out here.
245 *
246 * NMIs can still occur, of course, and might themselves
247 * contain rcu_read_lock().
248 */
249
250 local_irq_save(flags);
251
252 /*
253 * Outermost nesting of rcu_read_lock(), so increment
254 * the current counter for the current CPU. Use volatile
255 * casts to prevent the compiler from reordering.
256 */
257
258 idx = ACCESS_ONCE(rcu_ctrlblk.completed) & 0x1;
259 ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])++;
260
261 /*
262 * Now that the per-CPU counter has been incremented, we
263 * are protected from races with rcu_read_lock() invoked
264 * from NMI handlers on this CPU. We can therefore safely
265 * increment the nesting counter, relieving further NMIs
266 * of the need to increment the per-CPU counter.
267 */
268
269 ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting + 1;
270
271 /*
272 * Now that we have preventing any NMIs from storing
273 * to the ->rcu_flipctr_idx, we can safely use it to
274 * remember which counter to decrement in the matching
275 * rcu_read_unlock().
276 */
277
278 ACCESS_ONCE(t->rcu_flipctr_idx) = idx;
279 local_irq_restore(flags);
280 }
281}
282EXPORT_SYMBOL_GPL(__rcu_read_lock);
283
284void __rcu_read_unlock(void)
285{
286 int idx;
287 struct task_struct *t = current;
288 int nesting;
289
290 nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
291 if (nesting > 1) {
292
293 /*
294 * We are still protected by the enclosing rcu_read_lock(),
295 * so simply decrement the counter.
296 */
297
298 t->rcu_read_lock_nesting = nesting - 1;
299
300 } else {
301 unsigned long flags;
302
303 /*
304 * Disable local interrupts to prevent the grace-period
305 * detection state machine from seeing us half-done.
306 * NMIs can still occur, of course, and might themselves
307 * contain rcu_read_lock() and rcu_read_unlock().
308 */
309
310 local_irq_save(flags);
311
312 /*
313 * Outermost nesting of rcu_read_unlock(), so we must
314 * decrement the current counter for the current CPU.
315 * This must be done carefully, because NMIs can
316 * occur at any point in this code, and any rcu_read_lock()
317 * and rcu_read_unlock() pairs in the NMI handlers
318 * must interact non-destructively with this code.
319 * Lots of volatile casts, and -very- careful ordering.
320 *
321 * Changes to this code, including this one, must be
322 * inspected, validated, and tested extremely carefully!!!
323 */
324
325 /*
326 * First, pick up the index.
327 */
328
329 idx = ACCESS_ONCE(t->rcu_flipctr_idx);
330
331 /*
332 * Now that we have fetched the counter index, it is
333 * safe to decrement the per-task RCU nesting counter.
334 * After this, any interrupts or NMIs will increment and
335 * decrement the per-CPU counters.
336 */
337 ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting - 1;
338
339 /*
340 * It is now safe to decrement this task's nesting count.
341 * NMIs that occur after this statement will route their
342 * rcu_read_lock() calls through this "else" clause, and
343 * will thus start incrementing the per-CPU counter on
344 * their own. They will also clobber ->rcu_flipctr_idx,
345 * but that is OK, since we have already fetched it.
346 */
347
348 ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])--;
349 local_irq_restore(flags);
350 }
351}
352EXPORT_SYMBOL_GPL(__rcu_read_unlock);
353
354/*
355 * If a global counter flip has occurred since the last time that we
356 * advanced callbacks, advance them. Hardware interrupts must be
357 * disabled when calling this function.
358 */
359static void __rcu_advance_callbacks(struct rcu_data *rdp)
360{
361 int cpu;
362 int i;
363 int wlc = 0;
364
365 if (rdp->completed != rcu_ctrlblk.completed) {
366 if (rdp->waitlist[GP_STAGES - 1] != NULL) {
367 *rdp->donetail = rdp->waitlist[GP_STAGES - 1];
368 rdp->donetail = rdp->waittail[GP_STAGES - 1];
369 RCU_TRACE_RDP(rcupreempt_trace_move2done, rdp);
370 }
371 for (i = GP_STAGES - 2; i >= 0; i--) {
372 if (rdp->waitlist[i] != NULL) {
373 rdp->waitlist[i + 1] = rdp->waitlist[i];
374 rdp->waittail[i + 1] = rdp->waittail[i];
375 wlc++;
376 } else {
377 rdp->waitlist[i + 1] = NULL;
378 rdp->waittail[i + 1] =
379 &rdp->waitlist[i + 1];
380 }
381 }
382 if (rdp->nextlist != NULL) {
383 rdp->waitlist[0] = rdp->nextlist;
384 rdp->waittail[0] = rdp->nexttail;
385 wlc++;
386 rdp->nextlist = NULL;
387 rdp->nexttail = &rdp->nextlist;
388 RCU_TRACE_RDP(rcupreempt_trace_move2wait, rdp);
389 } else {
390 rdp->waitlist[0] = NULL;
391 rdp->waittail[0] = &rdp->waitlist[0];
392 }
393 rdp->waitlistcount = wlc;
394 rdp->completed = rcu_ctrlblk.completed;
395 }
396
397 /*
398 * Check to see if this CPU needs to report that it has seen
399 * the most recent counter flip, thereby declaring that all
400 * subsequent rcu_read_lock() invocations will respect this flip.
401 */
402
403 cpu = raw_smp_processor_id();
404 if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
405 smp_mb(); /* Subsequent counter accesses must see new value */
406 per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
407 smp_mb(); /* Subsequent RCU read-side critical sections */
408 /* seen -after- acknowledgement. */
409 }
410}
411
412/*
413 * Get here when RCU is idle. Decide whether we need to
414 * move out of idle state, and return non-zero if so.
415 * "Straightforward" approach for the moment, might later
416 * use callback-list lengths, grace-period duration, or
417 * some such to determine when to exit idle state.
418 * Might also need a pre-idle test that does not acquire
419 * the lock, but let's get the simple case working first...
420 */
421
422static int
423rcu_try_flip_idle(void)
424{
425 int cpu;
426
427 RCU_TRACE_ME(rcupreempt_trace_try_flip_i1);
428 if (!rcu_pending(smp_processor_id())) {
429 RCU_TRACE_ME(rcupreempt_trace_try_flip_ie1);
430 return 0;
431 }
432
433 /*
434 * Do the flip.
435 */
436
437 RCU_TRACE_ME(rcupreempt_trace_try_flip_g1);
438 rcu_ctrlblk.completed++; /* stands in for rcu_try_flip_g2 */
439
440 /*
441 * Need a memory barrier so that other CPUs see the new
442 * counter value before they see the subsequent change of all
443 * the rcu_flip_flag instances to rcu_flipped.
444 */
445
446 smp_mb(); /* see above block comment. */
447
448 /* Now ask each CPU for acknowledgement of the flip. */
449
450 for_each_cpu_mask(cpu, rcu_cpu_online_map)
451 per_cpu(rcu_flip_flag, cpu) = rcu_flipped;
452
453 return 1;
454}
455
456/*
457 * Wait for CPUs to acknowledge the flip.
458 */
459
460static int
461rcu_try_flip_waitack(void)
462{
463 int cpu;
464
465 RCU_TRACE_ME(rcupreempt_trace_try_flip_a1);
466 for_each_cpu_mask(cpu, rcu_cpu_online_map)
467 if (per_cpu(rcu_flip_flag, cpu) != rcu_flip_seen) {
468 RCU_TRACE_ME(rcupreempt_trace_try_flip_ae1);
469 return 0;
470 }
471
472 /*
473 * Make sure our checks above don't bleed into subsequent
474 * waiting for the sum of the counters to reach zero.
475 */
476
477 smp_mb(); /* see above block comment. */
478 RCU_TRACE_ME(rcupreempt_trace_try_flip_a2);
479 return 1;
480}
481
482/*
483 * Wait for collective ``last'' counter to reach zero,
484 * then tell all CPUs to do an end-of-grace-period memory barrier.
485 */
486
487static int
488rcu_try_flip_waitzero(void)
489{
490 int cpu;
491 int lastidx = !(rcu_ctrlblk.completed & 0x1);
492 int sum = 0;
493
494 /* Check to see if the sum of the "last" counters is zero. */
495
496 RCU_TRACE_ME(rcupreempt_trace_try_flip_z1);
497 for_each_cpu_mask(cpu, rcu_cpu_online_map)
498 sum += RCU_DATA_CPU(cpu)->rcu_flipctr[lastidx];
499 if (sum != 0) {
500 RCU_TRACE_ME(rcupreempt_trace_try_flip_ze1);
501 return 0;
502 }
503
504 /*
505 * This ensures that the other CPUs see the call for
506 * memory barriers -after- the sum to zero has been
507 * detected here
508 */
509 smp_mb(); /* ^^^^^^^^^^^^ */
510
511 /* Call for a memory barrier from each CPU. */
512 for_each_cpu_mask(cpu, rcu_cpu_online_map)
513 per_cpu(rcu_mb_flag, cpu) = rcu_mb_needed;
514
515 RCU_TRACE_ME(rcupreempt_trace_try_flip_z2);
516 return 1;
517}
518
519/*
520 * Wait for all CPUs to do their end-of-grace-period memory barrier.
521 * Return 0 once all CPUs have done so.
522 */
523
524static int
525rcu_try_flip_waitmb(void)
526{
527 int cpu;
528
529 RCU_TRACE_ME(rcupreempt_trace_try_flip_m1);
530 for_each_cpu_mask(cpu, rcu_cpu_online_map)
531 if (per_cpu(rcu_mb_flag, cpu) != rcu_mb_done) {
532 RCU_TRACE_ME(rcupreempt_trace_try_flip_me1);
533 return 0;
534 }
535
536 smp_mb(); /* Ensure that the above checks precede any following flip. */
537 RCU_TRACE_ME(rcupreempt_trace_try_flip_m2);
538 return 1;
539}
540
541/*
542 * Attempt a single flip of the counters. Remember, a single flip does
543 * -not- constitute a grace period. Instead, the interval between
544 * at least GP_STAGES consecutive flips is a grace period.
545 *
546 * If anyone is nuts enough to run this CONFIG_PREEMPT_RCU implementation
547 * on a large SMP, they might want to use a hierarchical organization of
548 * the per-CPU-counter pairs.
549 */
550static void rcu_try_flip(void)
551{
552 unsigned long flags;
553
554 RCU_TRACE_ME(rcupreempt_trace_try_flip_1);
555 if (unlikely(!spin_trylock_irqsave(&rcu_ctrlblk.fliplock, flags))) {
556 RCU_TRACE_ME(rcupreempt_trace_try_flip_e1);
557 return;
558 }
559
560 /*
561 * Take the next transition(s) through the RCU grace-period
562 * flip-counter state machine.
563 */
564
565 switch (rcu_ctrlblk.rcu_try_flip_state) {
566 case rcu_try_flip_idle_state:
567 if (rcu_try_flip_idle())
568 rcu_ctrlblk.rcu_try_flip_state =
569 rcu_try_flip_waitack_state;
570 break;
571 case rcu_try_flip_waitack_state:
572 if (rcu_try_flip_waitack())
573 rcu_ctrlblk.rcu_try_flip_state =
574 rcu_try_flip_waitzero_state;
575 break;
576 case rcu_try_flip_waitzero_state:
577 if (rcu_try_flip_waitzero())
578 rcu_ctrlblk.rcu_try_flip_state =
579 rcu_try_flip_waitmb_state;
580 break;
581 case rcu_try_flip_waitmb_state:
582 if (rcu_try_flip_waitmb())
583 rcu_ctrlblk.rcu_try_flip_state =
584 rcu_try_flip_idle_state;
585 }
586 spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
587}
588
589/*
590 * Check to see if this CPU needs to do a memory barrier in order to
591 * ensure that any prior RCU read-side critical sections have committed
592 * their counter manipulations and critical-section memory references
593 * before declaring the grace period to be completed.
594 */
595static void rcu_check_mb(int cpu)
596{
597 if (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed) {
598 smp_mb(); /* Ensure RCU read-side accesses are visible. */
599 per_cpu(rcu_mb_flag, cpu) = rcu_mb_done;
600 }
601}
602
603void rcu_check_callbacks(int cpu, int user)
604{
605 unsigned long flags;
606 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
607
608 rcu_check_mb(cpu);
609 if (rcu_ctrlblk.completed == rdp->completed)
610 rcu_try_flip();
611 spin_lock_irqsave(&rdp->lock, flags);
612 RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
613 __rcu_advance_callbacks(rdp);
614 if (rdp->donelist == NULL) {
615 spin_unlock_irqrestore(&rdp->lock, flags);
616 } else {
617 spin_unlock_irqrestore(&rdp->lock, flags);
618 raise_softirq(RCU_SOFTIRQ);
619 }
620}
621
622/*
623 * Needed by dynticks, to make sure all RCU processing has finished
624 * when we go idle:
625 */
626void rcu_advance_callbacks(int cpu, int user)
627{
628 unsigned long flags;
629 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
630
631 if (rcu_ctrlblk.completed == rdp->completed) {
632 rcu_try_flip();
633 if (rcu_ctrlblk.completed == rdp->completed)
634 return;
635 }
636 spin_lock_irqsave(&rdp->lock, flags);
637 RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
638 __rcu_advance_callbacks(rdp);
639 spin_unlock_irqrestore(&rdp->lock, flags);
640}
641
642#ifdef CONFIG_HOTPLUG_CPU
643#define rcu_offline_cpu_enqueue(srclist, srctail, dstlist, dsttail) do { \
644 *dsttail = srclist; \
645 if (srclist != NULL) { \
646 dsttail = srctail; \
647 srclist = NULL; \
648 srctail = &srclist;\
649 } \
650 } while (0)
651
652void rcu_offline_cpu(int cpu)
653{
654 int i;
655 struct rcu_head *list = NULL;
656 unsigned long flags;
657 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
658 struct rcu_head **tail = &list;
659
660 /*
661 * Remove all callbacks from the newly dead CPU, retaining order.
662 * Otherwise rcu_barrier() will fail
663 */
664
665 spin_lock_irqsave(&rdp->lock, flags);
666 rcu_offline_cpu_enqueue(rdp->donelist, rdp->donetail, list, tail);
667 for (i = GP_STAGES - 1; i >= 0; i--)
668 rcu_offline_cpu_enqueue(rdp->waitlist[i], rdp->waittail[i],
669 list, tail);
670 rcu_offline_cpu_enqueue(rdp->nextlist, rdp->nexttail, list, tail);
671 spin_unlock_irqrestore(&rdp->lock, flags);
672 rdp->waitlistcount = 0;
673
674 /* Disengage the newly dead CPU from the grace-period computation. */
675
676 spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
677 rcu_check_mb(cpu);
678 if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
679 smp_mb(); /* Subsequent counter accesses must see new value */
680 per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
681 smp_mb(); /* Subsequent RCU read-side critical sections */
682 /* seen -after- acknowledgement. */
683 }
684
685 RCU_DATA_ME()->rcu_flipctr[0] += RCU_DATA_CPU(cpu)->rcu_flipctr[0];
686 RCU_DATA_ME()->rcu_flipctr[1] += RCU_DATA_CPU(cpu)->rcu_flipctr[1];
687
688 RCU_DATA_CPU(cpu)->rcu_flipctr[0] = 0;
689 RCU_DATA_CPU(cpu)->rcu_flipctr[1] = 0;
690
691 cpu_clear(cpu, rcu_cpu_online_map);
692
693 spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
694
695 /*
696 * Place the removed callbacks on the current CPU's queue.
697 * Make them all start a new grace period: simple approach,
698 * in theory could starve a given set of callbacks, but
699 * you would need to be doing some serious CPU hotplugging
700 * to make this happen. If this becomes a problem, adding
701 * a synchronize_rcu() to the hotplug path would be a simple
702 * fix.
703 */
704
705 rdp = RCU_DATA_ME();
706 spin_lock_irqsave(&rdp->lock, flags);
707 *rdp->nexttail = list;
708 if (list)
709 rdp->nexttail = tail;
710 spin_unlock_irqrestore(&rdp->lock, flags);
711}
712
713void __devinit rcu_online_cpu(int cpu)
714{
715 unsigned long flags;
716
717 spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
718 cpu_set(cpu, rcu_cpu_online_map);
719 spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
720}
721
722#else /* #ifdef CONFIG_HOTPLUG_CPU */
723
724void rcu_offline_cpu(int cpu)
725{
726}
727
728void __devinit rcu_online_cpu(int cpu)
729{
730}
731
732#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
733
734static void rcu_process_callbacks(struct softirq_action *unused)
735{
736 unsigned long flags;
737 struct rcu_head *next, *list;
738 struct rcu_data *rdp = RCU_DATA_ME();
739
740 spin_lock_irqsave(&rdp->lock, flags);
741 list = rdp->donelist;
742 if (list == NULL) {
743 spin_unlock_irqrestore(&rdp->lock, flags);
744 return;
745 }
746 rdp->donelist = NULL;
747 rdp->donetail = &rdp->donelist;
748 RCU_TRACE_RDP(rcupreempt_trace_done_remove, rdp);
749 spin_unlock_irqrestore(&rdp->lock, flags);
750 while (list) {
751 next = list->next;
752 list->func(list);
753 list = next;
754 RCU_TRACE_ME(rcupreempt_trace_invoke);
755 }
756}
757
758void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
759{
760 unsigned long flags;
761 struct rcu_data *rdp;
762
763 head->func = func;
764 head->next = NULL;
765 local_irq_save(flags);
766 rdp = RCU_DATA_ME();
767 spin_lock(&rdp->lock);
768 __rcu_advance_callbacks(rdp);
769 *rdp->nexttail = head;
770 rdp->nexttail = &head->next;
771 RCU_TRACE_RDP(rcupreempt_trace_next_add, rdp);
772 spin_unlock(&rdp->lock);
773 local_irq_restore(flags);
774}
775EXPORT_SYMBOL_GPL(call_rcu);
776
777/*
778 * Wait until all currently running preempt_disable() code segments
779 * (including hardware-irq-disable segments) complete. Note that
780 * in -rt this does -not- necessarily result in all currently executing
781 * interrupt -handlers- having completed.
782 */
783void __synchronize_sched(void)
784{
785 cpumask_t oldmask;
786 int cpu;
787
788 if (sched_getaffinity(0, &oldmask) < 0)
789 oldmask = cpu_possible_map;
790 for_each_online_cpu(cpu) {
791 sched_setaffinity(0, cpumask_of_cpu(cpu));
792 schedule();
793 }
794 sched_setaffinity(0, oldmask);
795}
796EXPORT_SYMBOL_GPL(__synchronize_sched);
797
798/*
799 * Check to see if any future RCU-related work will need to be done
800 * by the current CPU, even if none need be done immediately, returning
801 * 1 if so. Assumes that notifiers would take care of handling any
802 * outstanding requests from the RCU core.
803 *
804 * This function is part of the RCU implementation; it is -not-
805 * an exported member of the RCU API.
806 */
807int rcu_needs_cpu(int cpu)
808{
809 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
810
811 return (rdp->donelist != NULL ||
812 !!rdp->waitlistcount ||
813 rdp->nextlist != NULL);
814}
815
816int rcu_pending(int cpu)
817{
818 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
819
820 /* The CPU has at least one callback queued somewhere. */
821
822 if (rdp->donelist != NULL ||
823 !!rdp->waitlistcount ||
824 rdp->nextlist != NULL)
825 return 1;
826
827 /* The RCU core needs an acknowledgement from this CPU. */
828
829 if ((per_cpu(rcu_flip_flag, cpu) == rcu_flipped) ||
830 (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed))
831 return 1;
832
833 /* This CPU has fallen behind the global grace-period number. */
834
835 if (rdp->completed != rcu_ctrlblk.completed)
836 return 1;
837
838 /* Nothing needed from this CPU. */
839
840 return 0;
841}
842
843static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
844 unsigned long action, void *hcpu)
845{
846 long cpu = (long)hcpu;
847
848 switch (action) {
849 case CPU_UP_PREPARE:
850 case CPU_UP_PREPARE_FROZEN:
851 rcu_online_cpu(cpu);
852 break;
853 case CPU_UP_CANCELED:
854 case CPU_UP_CANCELED_FROZEN:
855 case CPU_DEAD:
856 case CPU_DEAD_FROZEN:
857 rcu_offline_cpu(cpu);
858 break;
859 default:
860 break;
861 }
862 return NOTIFY_OK;
863}
864
865static struct notifier_block __cpuinitdata rcu_nb = {
866 .notifier_call = rcu_cpu_notify,
867};
868
869void __init __rcu_init(void)
870{
871 int cpu;
872 int i;
873 struct rcu_data *rdp;
874
875 printk(KERN_NOTICE "Preemptible RCU implementation.\n");
876 for_each_possible_cpu(cpu) {
877 rdp = RCU_DATA_CPU(cpu);
878 spin_lock_init(&rdp->lock);
879 rdp->completed = 0;
880 rdp->waitlistcount = 0;
881 rdp->nextlist = NULL;
882 rdp->nexttail = &rdp->nextlist;
883 for (i = 0; i < GP_STAGES; i++) {
884 rdp->waitlist[i] = NULL;
885 rdp->waittail[i] = &rdp->waitlist[i];
886 }
887 rdp->donelist = NULL;
888 rdp->donetail = &rdp->donelist;
889 rdp->rcu_flipctr[0] = 0;
890 rdp->rcu_flipctr[1] = 0;
891 }
892 register_cpu_notifier(&rcu_nb);
893
894 /*
895 * We don't need protection against CPU-Hotplug here
896 * since
897 * a) If a CPU comes online while we are iterating over the
898 * cpu_online_map below, we would only end up making a
899 * duplicate call to rcu_online_cpu() which sets the corresponding
900 * CPU's mask in the rcu_cpu_online_map.
901 *
902 * b) A CPU cannot go offline at this point in time since the user
903 * does not have access to the sysfs interface, nor do we
904 * suspend the system.
905 */
906 for_each_online_cpu(cpu)
907 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, (void *)(long) cpu);
908
909 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks, NULL);
910}
911
912/*
913 * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
914 */
915void synchronize_kernel(void)
916{
917 synchronize_rcu();
918}
919
920#ifdef CONFIG_RCU_TRACE
921long *rcupreempt_flipctr(int cpu)
922{
923 return &RCU_DATA_CPU(cpu)->rcu_flipctr[0];
924}
925EXPORT_SYMBOL_GPL(rcupreempt_flipctr);
926
927int rcupreempt_flip_flag(int cpu)
928{
929 return per_cpu(rcu_flip_flag, cpu);
930}
931EXPORT_SYMBOL_GPL(rcupreempt_flip_flag);
932
933int rcupreempt_mb_flag(int cpu)
934{
935 return per_cpu(rcu_mb_flag, cpu);
936}
937EXPORT_SYMBOL_GPL(rcupreempt_mb_flag);
938
939char *rcupreempt_try_flip_state_name(void)
940{
941 return rcu_try_flip_state_names[rcu_ctrlblk.rcu_try_flip_state];
942}
943EXPORT_SYMBOL_GPL(rcupreempt_try_flip_state_name);
944
945struct rcupreempt_trace *rcupreempt_trace_cpu(int cpu)
946{
947 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
948
949 return &rdp->trace;
950}
951EXPORT_SYMBOL_GPL(rcupreempt_trace_cpu);
952
953#endif /* #ifdef RCU_TRACE */
diff --git a/kernel/rcupreempt_trace.c b/kernel/rcupreempt_trace.c
new file mode 100644
index 000000000000..49ac4947af24
--- /dev/null
+++ b/kernel/rcupreempt_trace.c
@@ -0,0 +1,330 @@
1/*
2 * Read-Copy Update tracing for realtime implementation
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; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright IBM Corporation, 2006
19 *
20 * Papers: http://www.rdrop.com/users/paulmck/RCU
21 *
22 * For detailed explanation of Read-Copy Update mechanism see -
23 * Documentation/RCU/ *.txt
24 *
25 */
26#include <linux/types.h>
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/spinlock.h>
30#include <linux/smp.h>
31#include <linux/rcupdate.h>
32#include <linux/interrupt.h>
33#include <linux/sched.h>
34#include <asm/atomic.h>
35#include <linux/bitops.h>
36#include <linux/module.h>
37#include <linux/completion.h>
38#include <linux/moduleparam.h>
39#include <linux/percpu.h>
40#include <linux/notifier.h>
41#include <linux/rcupdate.h>
42#include <linux/cpu.h>
43#include <linux/mutex.h>
44#include <linux/rcupreempt_trace.h>
45#include <linux/debugfs.h>
46
47static struct mutex rcupreempt_trace_mutex;
48static char *rcupreempt_trace_buf;
49#define RCUPREEMPT_TRACE_BUF_SIZE 4096
50
51void rcupreempt_trace_move2done(struct rcupreempt_trace *trace)
52{
53 trace->done_length += trace->wait_length;
54 trace->done_add += trace->wait_length;
55 trace->wait_length = 0;
56}
57void rcupreempt_trace_move2wait(struct rcupreempt_trace *trace)
58{
59 trace->wait_length += trace->next_length;
60 trace->wait_add += trace->next_length;
61 trace->next_length = 0;
62}
63void rcupreempt_trace_try_flip_1(struct rcupreempt_trace *trace)
64{
65 atomic_inc(&trace->rcu_try_flip_1);
66}
67void rcupreempt_trace_try_flip_e1(struct rcupreempt_trace *trace)
68{
69 atomic_inc(&trace->rcu_try_flip_e1);
70}
71void rcupreempt_trace_try_flip_i1(struct rcupreempt_trace *trace)
72{
73 trace->rcu_try_flip_i1++;
74}
75void rcupreempt_trace_try_flip_ie1(struct rcupreempt_trace *trace)
76{
77 trace->rcu_try_flip_ie1++;
78}
79void rcupreempt_trace_try_flip_g1(struct rcupreempt_trace *trace)
80{
81 trace->rcu_try_flip_g1++;
82}
83void rcupreempt_trace_try_flip_a1(struct rcupreempt_trace *trace)
84{
85 trace->rcu_try_flip_a1++;
86}
87void rcupreempt_trace_try_flip_ae1(struct rcupreempt_trace *trace)
88{
89 trace->rcu_try_flip_ae1++;
90}
91void rcupreempt_trace_try_flip_a2(struct rcupreempt_trace *trace)
92{
93 trace->rcu_try_flip_a2++;
94}
95void rcupreempt_trace_try_flip_z1(struct rcupreempt_trace *trace)
96{
97 trace->rcu_try_flip_z1++;
98}
99void rcupreempt_trace_try_flip_ze1(struct rcupreempt_trace *trace)
100{
101 trace->rcu_try_flip_ze1++;
102}
103void rcupreempt_trace_try_flip_z2(struct rcupreempt_trace *trace)
104{
105 trace->rcu_try_flip_z2++;
106}
107void rcupreempt_trace_try_flip_m1(struct rcupreempt_trace *trace)
108{
109 trace->rcu_try_flip_m1++;
110}
111void rcupreempt_trace_try_flip_me1(struct rcupreempt_trace *trace)
112{
113 trace->rcu_try_flip_me1++;
114}
115void rcupreempt_trace_try_flip_m2(struct rcupreempt_trace *trace)
116{
117 trace->rcu_try_flip_m2++;
118}
119void rcupreempt_trace_check_callbacks(struct rcupreempt_trace *trace)
120{
121 trace->rcu_check_callbacks++;
122}
123void rcupreempt_trace_done_remove(struct rcupreempt_trace *trace)
124{
125 trace->done_remove += trace->done_length;
126 trace->done_length = 0;
127}
128void rcupreempt_trace_invoke(struct rcupreempt_trace *trace)
129{
130 atomic_inc(&trace->done_invoked);
131}
132void rcupreempt_trace_next_add(struct rcupreempt_trace *trace)
133{
134 trace->next_add++;
135 trace->next_length++;
136}
137
138static void rcupreempt_trace_sum(struct rcupreempt_trace *sp)
139{
140 struct rcupreempt_trace *cp;
141 int cpu;
142
143 memset(sp, 0, sizeof(*sp));
144 for_each_possible_cpu(cpu) {
145 cp = rcupreempt_trace_cpu(cpu);
146 sp->next_length += cp->next_length;
147 sp->next_add += cp->next_add;
148 sp->wait_length += cp->wait_length;
149 sp->wait_add += cp->wait_add;
150 sp->done_length += cp->done_length;
151 sp->done_add += cp->done_add;
152 sp->done_remove += cp->done_remove;
153 atomic_set(&sp->done_invoked, atomic_read(&cp->done_invoked));
154 sp->rcu_check_callbacks += cp->rcu_check_callbacks;
155 atomic_set(&sp->rcu_try_flip_1,
156 atomic_read(&cp->rcu_try_flip_1));
157 atomic_set(&sp->rcu_try_flip_e1,
158 atomic_read(&cp->rcu_try_flip_e1));
159 sp->rcu_try_flip_i1 += cp->rcu_try_flip_i1;
160 sp->rcu_try_flip_ie1 += cp->rcu_try_flip_ie1;
161 sp->rcu_try_flip_g1 += cp->rcu_try_flip_g1;
162 sp->rcu_try_flip_a1 += cp->rcu_try_flip_a1;
163 sp->rcu_try_flip_ae1 += cp->rcu_try_flip_ae1;
164 sp->rcu_try_flip_a2 += cp->rcu_try_flip_a2;
165 sp->rcu_try_flip_z1 += cp->rcu_try_flip_z1;
166 sp->rcu_try_flip_ze1 += cp->rcu_try_flip_ze1;
167 sp->rcu_try_flip_z2 += cp->rcu_try_flip_z2;
168 sp->rcu_try_flip_m1 += cp->rcu_try_flip_m1;
169 sp->rcu_try_flip_me1 += cp->rcu_try_flip_me1;
170 sp->rcu_try_flip_m2 += cp->rcu_try_flip_m2;
171 }
172}
173
174static ssize_t rcustats_read(struct file *filp, char __user *buffer,
175 size_t count, loff_t *ppos)
176{
177 struct rcupreempt_trace trace;
178 ssize_t bcount;
179 int cnt = 0;
180
181 rcupreempt_trace_sum(&trace);
182 mutex_lock(&rcupreempt_trace_mutex);
183 snprintf(&rcupreempt_trace_buf[cnt], RCUPREEMPT_TRACE_BUF_SIZE - cnt,
184 "ggp=%ld rcc=%ld\n",
185 rcu_batches_completed(),
186 trace.rcu_check_callbacks);
187 snprintf(&rcupreempt_trace_buf[cnt], RCUPREEMPT_TRACE_BUF_SIZE - cnt,
188 "na=%ld nl=%ld wa=%ld wl=%ld da=%ld dl=%ld dr=%ld di=%d\n"
189 "1=%d e1=%d i1=%ld ie1=%ld g1=%ld a1=%ld ae1=%ld a2=%ld\n"
190 "z1=%ld ze1=%ld z2=%ld m1=%ld me1=%ld m2=%ld\n",
191
192 trace.next_add, trace.next_length,
193 trace.wait_add, trace.wait_length,
194 trace.done_add, trace.done_length,
195 trace.done_remove, atomic_read(&trace.done_invoked),
196 atomic_read(&trace.rcu_try_flip_1),
197 atomic_read(&trace.rcu_try_flip_e1),
198 trace.rcu_try_flip_i1, trace.rcu_try_flip_ie1,
199 trace.rcu_try_flip_g1,
200 trace.rcu_try_flip_a1, trace.rcu_try_flip_ae1,
201 trace.rcu_try_flip_a2,
202 trace.rcu_try_flip_z1, trace.rcu_try_flip_ze1,
203 trace.rcu_try_flip_z2,
204 trace.rcu_try_flip_m1, trace.rcu_try_flip_me1,
205 trace.rcu_try_flip_m2);
206 bcount = simple_read_from_buffer(buffer, count, ppos,
207 rcupreempt_trace_buf, strlen(rcupreempt_trace_buf));
208 mutex_unlock(&rcupreempt_trace_mutex);
209 return bcount;
210}
211
212static ssize_t rcugp_read(struct file *filp, char __user *buffer,
213 size_t count, loff_t *ppos)
214{
215 long oldgp = rcu_batches_completed();
216 ssize_t bcount;
217
218 mutex_lock(&rcupreempt_trace_mutex);
219 synchronize_rcu();
220 snprintf(rcupreempt_trace_buf, RCUPREEMPT_TRACE_BUF_SIZE,
221 "oldggp=%ld newggp=%ld\n", oldgp, rcu_batches_completed());
222 bcount = simple_read_from_buffer(buffer, count, ppos,
223 rcupreempt_trace_buf, strlen(rcupreempt_trace_buf));
224 mutex_unlock(&rcupreempt_trace_mutex);
225 return bcount;
226}
227
228static ssize_t rcuctrs_read(struct file *filp, char __user *buffer,
229 size_t count, loff_t *ppos)
230{
231 int cnt = 0;
232 int cpu;
233 int f = rcu_batches_completed() & 0x1;
234 ssize_t bcount;
235
236 mutex_lock(&rcupreempt_trace_mutex);
237
238 cnt += snprintf(&rcupreempt_trace_buf[cnt], RCUPREEMPT_TRACE_BUF_SIZE,
239 "CPU last cur F M\n");
240 for_each_online_cpu(cpu) {
241 long *flipctr = rcupreempt_flipctr(cpu);
242 cnt += snprintf(&rcupreempt_trace_buf[cnt],
243 RCUPREEMPT_TRACE_BUF_SIZE - cnt,
244 "%3d %4ld %3ld %d %d\n",
245 cpu,
246 flipctr[!f],
247 flipctr[f],
248 rcupreempt_flip_flag(cpu),
249 rcupreempt_mb_flag(cpu));
250 }
251 cnt += snprintf(&rcupreempt_trace_buf[cnt],
252 RCUPREEMPT_TRACE_BUF_SIZE - cnt,
253 "ggp = %ld, state = %s\n",
254 rcu_batches_completed(),
255 rcupreempt_try_flip_state_name());
256 cnt += snprintf(&rcupreempt_trace_buf[cnt],
257 RCUPREEMPT_TRACE_BUF_SIZE - cnt,
258 "\n");
259 bcount = simple_read_from_buffer(buffer, count, ppos,
260 rcupreempt_trace_buf, strlen(rcupreempt_trace_buf));
261 mutex_unlock(&rcupreempt_trace_mutex);
262 return bcount;
263}
264
265static struct file_operations rcustats_fops = {
266 .owner = THIS_MODULE,
267 .read = rcustats_read,
268};
269
270static struct file_operations rcugp_fops = {
271 .owner = THIS_MODULE,
272 .read = rcugp_read,
273};
274
275static struct file_operations rcuctrs_fops = {
276 .owner = THIS_MODULE,
277 .read = rcuctrs_read,
278};
279
280static struct dentry *rcudir, *statdir, *ctrsdir, *gpdir;
281static int rcupreempt_debugfs_init(void)
282{
283 rcudir = debugfs_create_dir("rcu", NULL);
284 if (!rcudir)
285 goto out;
286 statdir = debugfs_create_file("rcustats", 0444, rcudir,
287 NULL, &rcustats_fops);
288 if (!statdir)
289 goto free_out;
290
291 gpdir = debugfs_create_file("rcugp", 0444, rcudir, NULL, &rcugp_fops);
292 if (!gpdir)
293 goto free_out;
294
295 ctrsdir = debugfs_create_file("rcuctrs", 0444, rcudir,
296 NULL, &rcuctrs_fops);
297 if (!ctrsdir)
298 goto free_out;
299 return 0;
300free_out:
301 if (statdir)
302 debugfs_remove(statdir);
303 if (gpdir)
304 debugfs_remove(gpdir);
305 debugfs_remove(rcudir);
306out:
307 return 1;
308}
309
310static int __init rcupreempt_trace_init(void)
311{
312 mutex_init(&rcupreempt_trace_mutex);
313 rcupreempt_trace_buf = kmalloc(RCUPREEMPT_TRACE_BUF_SIZE, GFP_KERNEL);
314 if (!rcupreempt_trace_buf)
315 return 1;
316 return rcupreempt_debugfs_init();
317}
318
319static void __exit rcupreempt_trace_cleanup(void)
320{
321 debugfs_remove(statdir);
322 debugfs_remove(gpdir);
323 debugfs_remove(ctrsdir);
324 debugfs_remove(rcudir);
325 kfree(rcupreempt_trace_buf);
326}
327
328
329module_init(rcupreempt_trace_init);
330module_exit(rcupreempt_trace_cleanup);
diff --git a/kernel/rcutorture.c b/kernel/rcutorture.c
index c3e165c2318f..fd599829e72a 100644
--- a/kernel/rcutorture.c
+++ b/kernel/rcutorture.c
@@ -726,11 +726,11 @@ static void rcu_torture_shuffle_tasks(void)
726 cpumask_t tmp_mask = CPU_MASK_ALL; 726 cpumask_t tmp_mask = CPU_MASK_ALL;
727 int i; 727 int i;
728 728
729 lock_cpu_hotplug(); 729 get_online_cpus();
730 730
731 /* No point in shuffling if there is only one online CPU (ex: UP) */ 731 /* No point in shuffling if there is only one online CPU (ex: UP) */
732 if (num_online_cpus() == 1) { 732 if (num_online_cpus() == 1) {
733 unlock_cpu_hotplug(); 733 put_online_cpus();
734 return; 734 return;
735 } 735 }
736 736
@@ -762,7 +762,7 @@ static void rcu_torture_shuffle_tasks(void)
762 else 762 else
763 rcu_idle_cpu--; 763 rcu_idle_cpu--;
764 764
765 unlock_cpu_hotplug(); 765 put_online_cpus();
766} 766}
767 767
768/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the 768/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
diff --git a/kernel/sched.c b/kernel/sched.c
index e76b11ca6df3..524285e46fa7 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -22,6 +22,8 @@
22 * by Peter Williams 22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith 23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri 24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
25 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
25 */ 27 */
26 28
27#include <linux/mm.h> 29#include <linux/mm.h>
@@ -63,6 +65,7 @@
63#include <linux/reciprocal_div.h> 65#include <linux/reciprocal_div.h>
64#include <linux/unistd.h> 66#include <linux/unistd.h>
65#include <linux/pagemap.h> 67#include <linux/pagemap.h>
68#include <linux/hrtimer.h>
66 69
67#include <asm/tlb.h> 70#include <asm/tlb.h>
68#include <asm/irq_regs.h> 71#include <asm/irq_regs.h>
@@ -96,10 +99,9 @@ unsigned long long __attribute__((weak)) sched_clock(void)
96#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) 99#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
97 100
98/* 101/*
99 * Some helpers for converting nanosecond timing to jiffy resolution 102 * Helpers for converting nanosecond timing to jiffy resolution
100 */ 103 */
101#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) 104#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
102#define JIFFIES_TO_NS(TIME) ((TIME) * (NSEC_PER_SEC / HZ))
103 105
104#define NICE_0_LOAD SCHED_LOAD_SCALE 106#define NICE_0_LOAD SCHED_LOAD_SCALE
105#define NICE_0_SHIFT SCHED_LOAD_SHIFT 107#define NICE_0_SHIFT SCHED_LOAD_SHIFT
@@ -159,6 +161,8 @@ struct rt_prio_array {
159 161
160struct cfs_rq; 162struct cfs_rq;
161 163
164static LIST_HEAD(task_groups);
165
162/* task group related information */ 166/* task group related information */
163struct task_group { 167struct task_group {
164#ifdef CONFIG_FAIR_CGROUP_SCHED 168#ifdef CONFIG_FAIR_CGROUP_SCHED
@@ -168,10 +172,50 @@ struct task_group {
168 struct sched_entity **se; 172 struct sched_entity **se;
169 /* runqueue "owned" by this group on each cpu */ 173 /* runqueue "owned" by this group on each cpu */
170 struct cfs_rq **cfs_rq; 174 struct cfs_rq **cfs_rq;
175
176 struct sched_rt_entity **rt_se;
177 struct rt_rq **rt_rq;
178
179 unsigned int rt_ratio;
180
181 /*
182 * shares assigned to a task group governs how much of cpu bandwidth
183 * is allocated to the group. The more shares a group has, the more is
184 * the cpu bandwidth allocated to it.
185 *
186 * For ex, lets say that there are three task groups, A, B and C which
187 * have been assigned shares 1000, 2000 and 3000 respectively. Then,
188 * cpu bandwidth allocated by the scheduler to task groups A, B and C
189 * should be:
190 *
191 * Bw(A) = 1000/(1000+2000+3000) * 100 = 16.66%
192 * Bw(B) = 2000/(1000+2000+3000) * 100 = 33.33%
193 * Bw(C) = 3000/(1000+2000+3000) * 100 = 50%
194 *
195 * The weight assigned to a task group's schedulable entities on every
196 * cpu (task_group.se[a_cpu]->load.weight) is derived from the task
197 * group's shares. For ex: lets say that task group A has been
198 * assigned shares of 1000 and there are two CPUs in a system. Then,
199 *
200 * tg_A->se[0]->load.weight = tg_A->se[1]->load.weight = 1000;
201 *
202 * Note: It's not necessary that each of a task's group schedulable
203 * entity have the same weight on all CPUs. If the group
204 * has 2 of its tasks on CPU0 and 1 task on CPU1, then a
205 * better distribution of weight could be:
206 *
207 * tg_A->se[0]->load.weight = 2/3 * 2000 = 1333
208 * tg_A->se[1]->load.weight = 1/2 * 2000 = 667
209 *
210 * rebalance_shares() is responsible for distributing the shares of a
211 * task groups like this among the group's schedulable entities across
212 * cpus.
213 *
214 */
171 unsigned long shares; 215 unsigned long shares;
172 /* spinlock to serialize modification to shares */ 216
173 spinlock_t lock;
174 struct rcu_head rcu; 217 struct rcu_head rcu;
218 struct list_head list;
175}; 219};
176 220
177/* Default task group's sched entity on each cpu */ 221/* Default task group's sched entity on each cpu */
@@ -179,24 +223,51 @@ static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
179/* Default task group's cfs_rq on each cpu */ 223/* Default task group's cfs_rq on each cpu */
180static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp; 224static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
181 225
226static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
227static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
228
182static struct sched_entity *init_sched_entity_p[NR_CPUS]; 229static struct sched_entity *init_sched_entity_p[NR_CPUS];
183static struct cfs_rq *init_cfs_rq_p[NR_CPUS]; 230static struct cfs_rq *init_cfs_rq_p[NR_CPUS];
184 231
232static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS];
233static struct rt_rq *init_rt_rq_p[NR_CPUS];
234
235/* task_group_mutex serializes add/remove of task groups and also changes to
236 * a task group's cpu shares.
237 */
238static DEFINE_MUTEX(task_group_mutex);
239
240/* doms_cur_mutex serializes access to doms_cur[] array */
241static DEFINE_MUTEX(doms_cur_mutex);
242
243#ifdef CONFIG_SMP
244/* kernel thread that runs rebalance_shares() periodically */
245static struct task_struct *lb_monitor_task;
246static int load_balance_monitor(void *unused);
247#endif
248
249static void set_se_shares(struct sched_entity *se, unsigned long shares);
250
185/* Default task group. 251/* Default task group.
186 * Every task in system belong to this group at bootup. 252 * Every task in system belong to this group at bootup.
187 */ 253 */
188struct task_group init_task_group = { 254struct task_group init_task_group = {
189 .se = init_sched_entity_p, 255 .se = init_sched_entity_p,
190 .cfs_rq = init_cfs_rq_p, 256 .cfs_rq = init_cfs_rq_p,
257
258 .rt_se = init_sched_rt_entity_p,
259 .rt_rq = init_rt_rq_p,
191}; 260};
192 261
193#ifdef CONFIG_FAIR_USER_SCHED 262#ifdef CONFIG_FAIR_USER_SCHED
194# define INIT_TASK_GRP_LOAD 2*NICE_0_LOAD 263# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
195#else 264#else
196# define INIT_TASK_GRP_LOAD NICE_0_LOAD 265# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
197#endif 266#endif
198 267
199static int init_task_group_load = INIT_TASK_GRP_LOAD; 268#define MIN_GROUP_SHARES 2
269
270static int init_task_group_load = INIT_TASK_GROUP_LOAD;
200 271
201/* return group to which a task belongs */ 272/* return group to which a task belongs */
202static inline struct task_group *task_group(struct task_struct *p) 273static inline struct task_group *task_group(struct task_struct *p)
@@ -215,15 +286,42 @@ static inline struct task_group *task_group(struct task_struct *p)
215} 286}
216 287
217/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ 288/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
218static inline void set_task_cfs_rq(struct task_struct *p, unsigned int cpu) 289static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
219{ 290{
220 p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; 291 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
221 p->se.parent = task_group(p)->se[cpu]; 292 p->se.parent = task_group(p)->se[cpu];
293
294 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
295 p->rt.parent = task_group(p)->rt_se[cpu];
296}
297
298static inline void lock_task_group_list(void)
299{
300 mutex_lock(&task_group_mutex);
301}
302
303static inline void unlock_task_group_list(void)
304{
305 mutex_unlock(&task_group_mutex);
306}
307
308static inline void lock_doms_cur(void)
309{
310 mutex_lock(&doms_cur_mutex);
311}
312
313static inline void unlock_doms_cur(void)
314{
315 mutex_unlock(&doms_cur_mutex);
222} 316}
223 317
224#else 318#else
225 319
226static inline void set_task_cfs_rq(struct task_struct *p, unsigned int cpu) { } 320static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
321static inline void lock_task_group_list(void) { }
322static inline void unlock_task_group_list(void) { }
323static inline void lock_doms_cur(void) { }
324static inline void unlock_doms_cur(void) { }
227 325
228#endif /* CONFIG_FAIR_GROUP_SCHED */ 326#endif /* CONFIG_FAIR_GROUP_SCHED */
229 327
@@ -264,10 +362,56 @@ struct cfs_rq {
264/* Real-Time classes' related field in a runqueue: */ 362/* Real-Time classes' related field in a runqueue: */
265struct rt_rq { 363struct rt_rq {
266 struct rt_prio_array active; 364 struct rt_prio_array active;
267 int rt_load_balance_idx; 365 unsigned long rt_nr_running;
268 struct list_head *rt_load_balance_head, *rt_load_balance_curr; 366#if defined CONFIG_SMP || defined CONFIG_FAIR_GROUP_SCHED
367 int highest_prio; /* highest queued rt task prio */
368#endif
369#ifdef CONFIG_SMP
370 unsigned long rt_nr_migratory;
371 int overloaded;
372#endif
373 int rt_throttled;
374 u64 rt_time;
375
376#ifdef CONFIG_FAIR_GROUP_SCHED
377 struct rq *rq;
378 struct list_head leaf_rt_rq_list;
379 struct task_group *tg;
380 struct sched_rt_entity *rt_se;
381#endif
269}; 382};
270 383
384#ifdef CONFIG_SMP
385
386/*
387 * We add the notion of a root-domain which will be used to define per-domain
388 * variables. Each exclusive cpuset essentially defines an island domain by
389 * fully partitioning the member cpus from any other cpuset. Whenever a new
390 * exclusive cpuset is created, we also create and attach a new root-domain
391 * object.
392 *
393 */
394struct root_domain {
395 atomic_t refcount;
396 cpumask_t span;
397 cpumask_t online;
398
399 /*
400 * The "RT overload" flag: it gets set if a CPU has more than
401 * one runnable RT task.
402 */
403 cpumask_t rto_mask;
404 atomic_t rto_count;
405};
406
407/*
408 * By default the system creates a single root-domain with all cpus as
409 * members (mimicking the global state we have today).
410 */
411static struct root_domain def_root_domain;
412
413#endif
414
271/* 415/*
272 * This is the main, per-CPU runqueue data structure. 416 * This is the main, per-CPU runqueue data structure.
273 * 417 *
@@ -296,11 +440,15 @@ struct rq {
296 u64 nr_switches; 440 u64 nr_switches;
297 441
298 struct cfs_rq cfs; 442 struct cfs_rq cfs;
443 struct rt_rq rt;
444 u64 rt_period_expire;
445 int rt_throttled;
446
299#ifdef CONFIG_FAIR_GROUP_SCHED 447#ifdef CONFIG_FAIR_GROUP_SCHED
300 /* list of leaf cfs_rq on this cpu: */ 448 /* list of leaf cfs_rq on this cpu: */
301 struct list_head leaf_cfs_rq_list; 449 struct list_head leaf_cfs_rq_list;
450 struct list_head leaf_rt_rq_list;
302#endif 451#endif
303 struct rt_rq rt;
304 452
305 /* 453 /*
306 * This is part of a global counter where only the total sum 454 * This is part of a global counter where only the total sum
@@ -317,7 +465,7 @@ struct rq {
317 u64 clock, prev_clock_raw; 465 u64 clock, prev_clock_raw;
318 s64 clock_max_delta; 466 s64 clock_max_delta;
319 467
320 unsigned int clock_warps, clock_overflows; 468 unsigned int clock_warps, clock_overflows, clock_underflows;
321 u64 idle_clock; 469 u64 idle_clock;
322 unsigned int clock_deep_idle_events; 470 unsigned int clock_deep_idle_events;
323 u64 tick_timestamp; 471 u64 tick_timestamp;
@@ -325,6 +473,7 @@ struct rq {
325 atomic_t nr_iowait; 473 atomic_t nr_iowait;
326 474
327#ifdef CONFIG_SMP 475#ifdef CONFIG_SMP
476 struct root_domain *rd;
328 struct sched_domain *sd; 477 struct sched_domain *sd;
329 478
330 /* For active balancing */ 479 /* For active balancing */
@@ -337,6 +486,12 @@ struct rq {
337 struct list_head migration_queue; 486 struct list_head migration_queue;
338#endif 487#endif
339 488
489#ifdef CONFIG_SCHED_HRTICK
490 unsigned long hrtick_flags;
491 ktime_t hrtick_expire;
492 struct hrtimer hrtick_timer;
493#endif
494
340#ifdef CONFIG_SCHEDSTATS 495#ifdef CONFIG_SCHEDSTATS
341 /* latency stats */ 496 /* latency stats */
342 struct sched_info rq_sched_info; 497 struct sched_info rq_sched_info;
@@ -363,7 +518,6 @@ struct rq {
363}; 518};
364 519
365static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); 520static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
366static DEFINE_MUTEX(sched_hotcpu_mutex);
367 521
368static inline void check_preempt_curr(struct rq *rq, struct task_struct *p) 522static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
369{ 523{
@@ -441,6 +595,23 @@ static void update_rq_clock(struct rq *rq)
441#define task_rq(p) cpu_rq(task_cpu(p)) 595#define task_rq(p) cpu_rq(task_cpu(p))
442#define cpu_curr(cpu) (cpu_rq(cpu)->curr) 596#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
443 597
598unsigned long rt_needs_cpu(int cpu)
599{
600 struct rq *rq = cpu_rq(cpu);
601 u64 delta;
602
603 if (!rq->rt_throttled)
604 return 0;
605
606 if (rq->clock > rq->rt_period_expire)
607 return 1;
608
609 delta = rq->rt_period_expire - rq->clock;
610 do_div(delta, NSEC_PER_SEC / HZ);
611
612 return (unsigned long)delta;
613}
614
444/* 615/*
445 * Tunables that become constants when CONFIG_SCHED_DEBUG is off: 616 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
446 */ 617 */
@@ -459,6 +630,8 @@ enum {
459 SCHED_FEAT_START_DEBIT = 4, 630 SCHED_FEAT_START_DEBIT = 4,
460 SCHED_FEAT_TREE_AVG = 8, 631 SCHED_FEAT_TREE_AVG = 8,
461 SCHED_FEAT_APPROX_AVG = 16, 632 SCHED_FEAT_APPROX_AVG = 16,
633 SCHED_FEAT_HRTICK = 32,
634 SCHED_FEAT_DOUBLE_TICK = 64,
462}; 635};
463 636
464const_debug unsigned int sysctl_sched_features = 637const_debug unsigned int sysctl_sched_features =
@@ -466,7 +639,9 @@ const_debug unsigned int sysctl_sched_features =
466 SCHED_FEAT_WAKEUP_PREEMPT * 1 | 639 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
467 SCHED_FEAT_START_DEBIT * 1 | 640 SCHED_FEAT_START_DEBIT * 1 |
468 SCHED_FEAT_TREE_AVG * 0 | 641 SCHED_FEAT_TREE_AVG * 0 |
469 SCHED_FEAT_APPROX_AVG * 0; 642 SCHED_FEAT_APPROX_AVG * 0 |
643 SCHED_FEAT_HRTICK * 1 |
644 SCHED_FEAT_DOUBLE_TICK * 0;
470 645
471#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x) 646#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
472 647
@@ -477,6 +652,21 @@ const_debug unsigned int sysctl_sched_features =
477const_debug unsigned int sysctl_sched_nr_migrate = 32; 652const_debug unsigned int sysctl_sched_nr_migrate = 32;
478 653
479/* 654/*
655 * period over which we measure -rt task cpu usage in ms.
656 * default: 1s
657 */
658const_debug unsigned int sysctl_sched_rt_period = 1000;
659
660#define SCHED_RT_FRAC_SHIFT 16
661#define SCHED_RT_FRAC (1UL << SCHED_RT_FRAC_SHIFT)
662
663/*
664 * ratio of time -rt tasks may consume.
665 * default: 95%
666 */
667const_debug unsigned int sysctl_sched_rt_ratio = 62259;
668
669/*
480 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu 670 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
481 * clock constructed from sched_clock(): 671 * clock constructed from sched_clock():
482 */ 672 */
@@ -668,7 +858,6 @@ void sched_clock_idle_wakeup_event(u64 delta_ns)
668 struct rq *rq = cpu_rq(smp_processor_id()); 858 struct rq *rq = cpu_rq(smp_processor_id());
669 u64 now = sched_clock(); 859 u64 now = sched_clock();
670 860
671 touch_softlockup_watchdog();
672 rq->idle_clock += delta_ns; 861 rq->idle_clock += delta_ns;
673 /* 862 /*
674 * Override the previous timestamp and ignore all 863 * Override the previous timestamp and ignore all
@@ -680,9 +869,177 @@ void sched_clock_idle_wakeup_event(u64 delta_ns)
680 rq->prev_clock_raw = now; 869 rq->prev_clock_raw = now;
681 rq->clock += delta_ns; 870 rq->clock += delta_ns;
682 spin_unlock(&rq->lock); 871 spin_unlock(&rq->lock);
872 touch_softlockup_watchdog();
683} 873}
684EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event); 874EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
685 875
876static void __resched_task(struct task_struct *p, int tif_bit);
877
878static inline void resched_task(struct task_struct *p)
879{
880 __resched_task(p, TIF_NEED_RESCHED);
881}
882
883#ifdef CONFIG_SCHED_HRTICK
884/*
885 * Use HR-timers to deliver accurate preemption points.
886 *
887 * Its all a bit involved since we cannot program an hrt while holding the
888 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
889 * reschedule event.
890 *
891 * When we get rescheduled we reprogram the hrtick_timer outside of the
892 * rq->lock.
893 */
894static inline void resched_hrt(struct task_struct *p)
895{
896 __resched_task(p, TIF_HRTICK_RESCHED);
897}
898
899static inline void resched_rq(struct rq *rq)
900{
901 unsigned long flags;
902
903 spin_lock_irqsave(&rq->lock, flags);
904 resched_task(rq->curr);
905 spin_unlock_irqrestore(&rq->lock, flags);
906}
907
908enum {
909 HRTICK_SET, /* re-programm hrtick_timer */
910 HRTICK_RESET, /* not a new slice */
911};
912
913/*
914 * Use hrtick when:
915 * - enabled by features
916 * - hrtimer is actually high res
917 */
918static inline int hrtick_enabled(struct rq *rq)
919{
920 if (!sched_feat(HRTICK))
921 return 0;
922 return hrtimer_is_hres_active(&rq->hrtick_timer);
923}
924
925/*
926 * Called to set the hrtick timer state.
927 *
928 * called with rq->lock held and irqs disabled
929 */
930static void hrtick_start(struct rq *rq, u64 delay, int reset)
931{
932 assert_spin_locked(&rq->lock);
933
934 /*
935 * preempt at: now + delay
936 */
937 rq->hrtick_expire =
938 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
939 /*
940 * indicate we need to program the timer
941 */
942 __set_bit(HRTICK_SET, &rq->hrtick_flags);
943 if (reset)
944 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
945
946 /*
947 * New slices are called from the schedule path and don't need a
948 * forced reschedule.
949 */
950 if (reset)
951 resched_hrt(rq->curr);
952}
953
954static void hrtick_clear(struct rq *rq)
955{
956 if (hrtimer_active(&rq->hrtick_timer))
957 hrtimer_cancel(&rq->hrtick_timer);
958}
959
960/*
961 * Update the timer from the possible pending state.
962 */
963static void hrtick_set(struct rq *rq)
964{
965 ktime_t time;
966 int set, reset;
967 unsigned long flags;
968
969 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
970
971 spin_lock_irqsave(&rq->lock, flags);
972 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
973 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
974 time = rq->hrtick_expire;
975 clear_thread_flag(TIF_HRTICK_RESCHED);
976 spin_unlock_irqrestore(&rq->lock, flags);
977
978 if (set) {
979 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
980 if (reset && !hrtimer_active(&rq->hrtick_timer))
981 resched_rq(rq);
982 } else
983 hrtick_clear(rq);
984}
985
986/*
987 * High-resolution timer tick.
988 * Runs from hardirq context with interrupts disabled.
989 */
990static enum hrtimer_restart hrtick(struct hrtimer *timer)
991{
992 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
993
994 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
995
996 spin_lock(&rq->lock);
997 __update_rq_clock(rq);
998 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
999 spin_unlock(&rq->lock);
1000
1001 return HRTIMER_NORESTART;
1002}
1003
1004static inline void init_rq_hrtick(struct rq *rq)
1005{
1006 rq->hrtick_flags = 0;
1007 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1008 rq->hrtick_timer.function = hrtick;
1009 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1010}
1011
1012void hrtick_resched(void)
1013{
1014 struct rq *rq;
1015 unsigned long flags;
1016
1017 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1018 return;
1019
1020 local_irq_save(flags);
1021 rq = cpu_rq(smp_processor_id());
1022 hrtick_set(rq);
1023 local_irq_restore(flags);
1024}
1025#else
1026static inline void hrtick_clear(struct rq *rq)
1027{
1028}
1029
1030static inline void hrtick_set(struct rq *rq)
1031{
1032}
1033
1034static inline void init_rq_hrtick(struct rq *rq)
1035{
1036}
1037
1038void hrtick_resched(void)
1039{
1040}
1041#endif
1042
686/* 1043/*
687 * resched_task - mark a task 'to be rescheduled now'. 1044 * resched_task - mark a task 'to be rescheduled now'.
688 * 1045 *
@@ -696,16 +1053,16 @@ EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
696#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) 1053#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
697#endif 1054#endif
698 1055
699static void resched_task(struct task_struct *p) 1056static void __resched_task(struct task_struct *p, int tif_bit)
700{ 1057{
701 int cpu; 1058 int cpu;
702 1059
703 assert_spin_locked(&task_rq(p)->lock); 1060 assert_spin_locked(&task_rq(p)->lock);
704 1061
705 if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED))) 1062 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
706 return; 1063 return;
707 1064
708 set_tsk_thread_flag(p, TIF_NEED_RESCHED); 1065 set_tsk_thread_flag(p, tif_bit);
709 1066
710 cpu = task_cpu(p); 1067 cpu = task_cpu(p);
711 if (cpu == smp_processor_id()) 1068 if (cpu == smp_processor_id())
@@ -728,10 +1085,10 @@ static void resched_cpu(int cpu)
728 spin_unlock_irqrestore(&rq->lock, flags); 1085 spin_unlock_irqrestore(&rq->lock, flags);
729} 1086}
730#else 1087#else
731static inline void resched_task(struct task_struct *p) 1088static void __resched_task(struct task_struct *p, int tif_bit)
732{ 1089{
733 assert_spin_locked(&task_rq(p)->lock); 1090 assert_spin_locked(&task_rq(p)->lock);
734 set_tsk_need_resched(p); 1091 set_tsk_thread_flag(p, tif_bit);
735} 1092}
736#endif 1093#endif
737 1094
@@ -871,6 +1228,23 @@ static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
871static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} 1228static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
872#endif 1229#endif
873 1230
1231static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1232{
1233 update_load_add(&rq->load, load);
1234}
1235
1236static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1237{
1238 update_load_sub(&rq->load, load);
1239}
1240
1241#ifdef CONFIG_SMP
1242static unsigned long source_load(int cpu, int type);
1243static unsigned long target_load(int cpu, int type);
1244static unsigned long cpu_avg_load_per_task(int cpu);
1245static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1246#endif /* CONFIG_SMP */
1247
874#include "sched_stats.h" 1248#include "sched_stats.h"
875#include "sched_idletask.c" 1249#include "sched_idletask.c"
876#include "sched_fair.c" 1250#include "sched_fair.c"
@@ -881,41 +1255,14 @@ static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
881 1255
882#define sched_class_highest (&rt_sched_class) 1256#define sched_class_highest (&rt_sched_class)
883 1257
884/*
885 * Update delta_exec, delta_fair fields for rq.
886 *
887 * delta_fair clock advances at a rate inversely proportional to
888 * total load (rq->load.weight) on the runqueue, while
889 * delta_exec advances at the same rate as wall-clock (provided
890 * cpu is not idle).
891 *
892 * delta_exec / delta_fair is a measure of the (smoothened) load on this
893 * runqueue over any given interval. This (smoothened) load is used
894 * during load balance.
895 *
896 * This function is called /before/ updating rq->load
897 * and when switching tasks.
898 */
899static inline void inc_load(struct rq *rq, const struct task_struct *p)
900{
901 update_load_add(&rq->load, p->se.load.weight);
902}
903
904static inline void dec_load(struct rq *rq, const struct task_struct *p)
905{
906 update_load_sub(&rq->load, p->se.load.weight);
907}
908
909static void inc_nr_running(struct task_struct *p, struct rq *rq) 1258static void inc_nr_running(struct task_struct *p, struct rq *rq)
910{ 1259{
911 rq->nr_running++; 1260 rq->nr_running++;
912 inc_load(rq, p);
913} 1261}
914 1262
915static void dec_nr_running(struct task_struct *p, struct rq *rq) 1263static void dec_nr_running(struct task_struct *p, struct rq *rq)
916{ 1264{
917 rq->nr_running--; 1265 rq->nr_running--;
918 dec_load(rq, p);
919} 1266}
920 1267
921static void set_load_weight(struct task_struct *p) 1268static void set_load_weight(struct task_struct *p)
@@ -1039,7 +1386,7 @@ unsigned long weighted_cpuload(const int cpu)
1039 1386
1040static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) 1387static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1041{ 1388{
1042 set_task_cfs_rq(p, cpu); 1389 set_task_rq(p, cpu);
1043#ifdef CONFIG_SMP 1390#ifdef CONFIG_SMP
1044 /* 1391 /*
1045 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be 1392 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
@@ -1051,12 +1398,24 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1051#endif 1398#endif
1052} 1399}
1053 1400
1401static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1402 const struct sched_class *prev_class,
1403 int oldprio, int running)
1404{
1405 if (prev_class != p->sched_class) {
1406 if (prev_class->switched_from)
1407 prev_class->switched_from(rq, p, running);
1408 p->sched_class->switched_to(rq, p, running);
1409 } else
1410 p->sched_class->prio_changed(rq, p, oldprio, running);
1411}
1412
1054#ifdef CONFIG_SMP 1413#ifdef CONFIG_SMP
1055 1414
1056/* 1415/*
1057 * Is this task likely cache-hot: 1416 * Is this task likely cache-hot:
1058 */ 1417 */
1059static inline int 1418static int
1060task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) 1419task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1061{ 1420{
1062 s64 delta; 1421 s64 delta;
@@ -1281,7 +1640,7 @@ static unsigned long target_load(int cpu, int type)
1281/* 1640/*
1282 * Return the average load per task on the cpu's run queue 1641 * Return the average load per task on the cpu's run queue
1283 */ 1642 */
1284static inline unsigned long cpu_avg_load_per_task(int cpu) 1643static unsigned long cpu_avg_load_per_task(int cpu)
1285{ 1644{
1286 struct rq *rq = cpu_rq(cpu); 1645 struct rq *rq = cpu_rq(cpu);
1287 unsigned long total = weighted_cpuload(cpu); 1646 unsigned long total = weighted_cpuload(cpu);
@@ -1438,58 +1797,6 @@ static int sched_balance_self(int cpu, int flag)
1438 1797
1439#endif /* CONFIG_SMP */ 1798#endif /* CONFIG_SMP */
1440 1799
1441/*
1442 * wake_idle() will wake a task on an idle cpu if task->cpu is
1443 * not idle and an idle cpu is available. The span of cpus to
1444 * search starts with cpus closest then further out as needed,
1445 * so we always favor a closer, idle cpu.
1446 *
1447 * Returns the CPU we should wake onto.
1448 */
1449#if defined(ARCH_HAS_SCHED_WAKE_IDLE)
1450static int wake_idle(int cpu, struct task_struct *p)
1451{
1452 cpumask_t tmp;
1453 struct sched_domain *sd;
1454 int i;
1455
1456 /*
1457 * If it is idle, then it is the best cpu to run this task.
1458 *
1459 * This cpu is also the best, if it has more than one task already.
1460 * Siblings must be also busy(in most cases) as they didn't already
1461 * pickup the extra load from this cpu and hence we need not check
1462 * sibling runqueue info. This will avoid the checks and cache miss
1463 * penalities associated with that.
1464 */
1465 if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1)
1466 return cpu;
1467
1468 for_each_domain(cpu, sd) {
1469 if (sd->flags & SD_WAKE_IDLE) {
1470 cpus_and(tmp, sd->span, p->cpus_allowed);
1471 for_each_cpu_mask(i, tmp) {
1472 if (idle_cpu(i)) {
1473 if (i != task_cpu(p)) {
1474 schedstat_inc(p,
1475 se.nr_wakeups_idle);
1476 }
1477 return i;
1478 }
1479 }
1480 } else {
1481 break;
1482 }
1483 }
1484 return cpu;
1485}
1486#else
1487static inline int wake_idle(int cpu, struct task_struct *p)
1488{
1489 return cpu;
1490}
1491#endif
1492
1493/*** 1800/***
1494 * try_to_wake_up - wake up a thread 1801 * try_to_wake_up - wake up a thread
1495 * @p: the to-be-woken-up thread 1802 * @p: the to-be-woken-up thread
@@ -1510,11 +1817,6 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
1510 unsigned long flags; 1817 unsigned long flags;
1511 long old_state; 1818 long old_state;
1512 struct rq *rq; 1819 struct rq *rq;
1513#ifdef CONFIG_SMP
1514 struct sched_domain *sd, *this_sd = NULL;
1515 unsigned long load, this_load;
1516 int new_cpu;
1517#endif
1518 1820
1519 rq = task_rq_lock(p, &flags); 1821 rq = task_rq_lock(p, &flags);
1520 old_state = p->state; 1822 old_state = p->state;
@@ -1532,92 +1834,9 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
1532 if (unlikely(task_running(rq, p))) 1834 if (unlikely(task_running(rq, p)))
1533 goto out_activate; 1835 goto out_activate;
1534 1836
1535 new_cpu = cpu; 1837 cpu = p->sched_class->select_task_rq(p, sync);
1536 1838 if (cpu != orig_cpu) {
1537 schedstat_inc(rq, ttwu_count); 1839 set_task_cpu(p, cpu);
1538 if (cpu == this_cpu) {
1539 schedstat_inc(rq, ttwu_local);
1540 goto out_set_cpu;
1541 }
1542
1543 for_each_domain(this_cpu, sd) {
1544 if (cpu_isset(cpu, sd->span)) {
1545 schedstat_inc(sd, ttwu_wake_remote);
1546 this_sd = sd;
1547 break;
1548 }
1549 }
1550
1551 if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
1552 goto out_set_cpu;
1553
1554 /*
1555 * Check for affine wakeup and passive balancing possibilities.
1556 */
1557 if (this_sd) {
1558 int idx = this_sd->wake_idx;
1559 unsigned int imbalance;
1560
1561 imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;
1562
1563 load = source_load(cpu, idx);
1564 this_load = target_load(this_cpu, idx);
1565
1566 new_cpu = this_cpu; /* Wake to this CPU if we can */
1567
1568 if (this_sd->flags & SD_WAKE_AFFINE) {
1569 unsigned long tl = this_load;
1570 unsigned long tl_per_task;
1571
1572 /*
1573 * Attract cache-cold tasks on sync wakeups:
1574 */
1575 if (sync && !task_hot(p, rq->clock, this_sd))
1576 goto out_set_cpu;
1577
1578 schedstat_inc(p, se.nr_wakeups_affine_attempts);
1579 tl_per_task = cpu_avg_load_per_task(this_cpu);
1580
1581 /*
1582 * If sync wakeup then subtract the (maximum possible)
1583 * effect of the currently running task from the load
1584 * of the current CPU:
1585 */
1586 if (sync)
1587 tl -= current->se.load.weight;
1588
1589 if ((tl <= load &&
1590 tl + target_load(cpu, idx) <= tl_per_task) ||
1591 100*(tl + p->se.load.weight) <= imbalance*load) {
1592 /*
1593 * This domain has SD_WAKE_AFFINE and
1594 * p is cache cold in this domain, and
1595 * there is no bad imbalance.
1596 */
1597 schedstat_inc(this_sd, ttwu_move_affine);
1598 schedstat_inc(p, se.nr_wakeups_affine);
1599 goto out_set_cpu;
1600 }
1601 }
1602
1603 /*
1604 * Start passive balancing when half the imbalance_pct
1605 * limit is reached.
1606 */
1607 if (this_sd->flags & SD_WAKE_BALANCE) {
1608 if (imbalance*this_load <= 100*load) {
1609 schedstat_inc(this_sd, ttwu_move_balance);
1610 schedstat_inc(p, se.nr_wakeups_passive);
1611 goto out_set_cpu;
1612 }
1613 }
1614 }
1615
1616 new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */
1617out_set_cpu:
1618 new_cpu = wake_idle(new_cpu, p);
1619 if (new_cpu != cpu) {
1620 set_task_cpu(p, new_cpu);
1621 task_rq_unlock(rq, &flags); 1840 task_rq_unlock(rq, &flags);
1622 /* might preempt at this point */ 1841 /* might preempt at this point */
1623 rq = task_rq_lock(p, &flags); 1842 rq = task_rq_lock(p, &flags);
@@ -1631,6 +1850,21 @@ out_set_cpu:
1631 cpu = task_cpu(p); 1850 cpu = task_cpu(p);
1632 } 1851 }
1633 1852
1853#ifdef CONFIG_SCHEDSTATS
1854 schedstat_inc(rq, ttwu_count);
1855 if (cpu == this_cpu)
1856 schedstat_inc(rq, ttwu_local);
1857 else {
1858 struct sched_domain *sd;
1859 for_each_domain(this_cpu, sd) {
1860 if (cpu_isset(cpu, sd->span)) {
1861 schedstat_inc(sd, ttwu_wake_remote);
1862 break;
1863 }
1864 }
1865 }
1866#endif
1867
1634out_activate: 1868out_activate:
1635#endif /* CONFIG_SMP */ 1869#endif /* CONFIG_SMP */
1636 schedstat_inc(p, se.nr_wakeups); 1870 schedstat_inc(p, se.nr_wakeups);
@@ -1649,6 +1883,10 @@ out_activate:
1649 1883
1650out_running: 1884out_running:
1651 p->state = TASK_RUNNING; 1885 p->state = TASK_RUNNING;
1886#ifdef CONFIG_SMP
1887 if (p->sched_class->task_wake_up)
1888 p->sched_class->task_wake_up(rq, p);
1889#endif
1652out: 1890out:
1653 task_rq_unlock(rq, &flags); 1891 task_rq_unlock(rq, &flags);
1654 1892
@@ -1691,7 +1929,7 @@ static void __sched_fork(struct task_struct *p)
1691 p->se.wait_max = 0; 1929 p->se.wait_max = 0;
1692#endif 1930#endif
1693 1931
1694 INIT_LIST_HEAD(&p->run_list); 1932 INIT_LIST_HEAD(&p->rt.run_list);
1695 p->se.on_rq = 0; 1933 p->se.on_rq = 0;
1696 1934
1697#ifdef CONFIG_PREEMPT_NOTIFIERS 1935#ifdef CONFIG_PREEMPT_NOTIFIERS
@@ -1771,6 +2009,10 @@ void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
1771 inc_nr_running(p, rq); 2009 inc_nr_running(p, rq);
1772 } 2010 }
1773 check_preempt_curr(rq, p); 2011 check_preempt_curr(rq, p);
2012#ifdef CONFIG_SMP
2013 if (p->sched_class->task_wake_up)
2014 p->sched_class->task_wake_up(rq, p);
2015#endif
1774 task_rq_unlock(rq, &flags); 2016 task_rq_unlock(rq, &flags);
1775} 2017}
1776 2018
@@ -1891,6 +2133,11 @@ static void finish_task_switch(struct rq *rq, struct task_struct *prev)
1891 prev_state = prev->state; 2133 prev_state = prev->state;
1892 finish_arch_switch(prev); 2134 finish_arch_switch(prev);
1893 finish_lock_switch(rq, prev); 2135 finish_lock_switch(rq, prev);
2136#ifdef CONFIG_SMP
2137 if (current->sched_class->post_schedule)
2138 current->sched_class->post_schedule(rq);
2139#endif
2140
1894 fire_sched_in_preempt_notifiers(current); 2141 fire_sched_in_preempt_notifiers(current);
1895 if (mm) 2142 if (mm)
1896 mmdrop(mm); 2143 mmdrop(mm);
@@ -2124,11 +2371,13 @@ static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2124/* 2371/*
2125 * double_lock_balance - lock the busiest runqueue, this_rq is locked already. 2372 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2126 */ 2373 */
2127static void double_lock_balance(struct rq *this_rq, struct rq *busiest) 2374static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
2128 __releases(this_rq->lock) 2375 __releases(this_rq->lock)
2129 __acquires(busiest->lock) 2376 __acquires(busiest->lock)
2130 __acquires(this_rq->lock) 2377 __acquires(this_rq->lock)
2131{ 2378{
2379 int ret = 0;
2380
2132 if (unlikely(!irqs_disabled())) { 2381 if (unlikely(!irqs_disabled())) {
2133 /* printk() doesn't work good under rq->lock */ 2382 /* printk() doesn't work good under rq->lock */
2134 spin_unlock(&this_rq->lock); 2383 spin_unlock(&this_rq->lock);
@@ -2139,9 +2388,11 @@ static void double_lock_balance(struct rq *this_rq, struct rq *busiest)
2139 spin_unlock(&this_rq->lock); 2388 spin_unlock(&this_rq->lock);
2140 spin_lock(&busiest->lock); 2389 spin_lock(&busiest->lock);
2141 spin_lock(&this_rq->lock); 2390 spin_lock(&this_rq->lock);
2391 ret = 1;
2142 } else 2392 } else
2143 spin_lock(&busiest->lock); 2393 spin_lock(&busiest->lock);
2144 } 2394 }
2395 return ret;
2145} 2396}
2146 2397
2147/* 2398/*
@@ -3485,12 +3736,14 @@ void scheduler_tick(void)
3485 /* 3736 /*
3486 * Let rq->clock advance by at least TICK_NSEC: 3737 * Let rq->clock advance by at least TICK_NSEC:
3487 */ 3738 */
3488 if (unlikely(rq->clock < next_tick)) 3739 if (unlikely(rq->clock < next_tick)) {
3489 rq->clock = next_tick; 3740 rq->clock = next_tick;
3741 rq->clock_underflows++;
3742 }
3490 rq->tick_timestamp = rq->clock; 3743 rq->tick_timestamp = rq->clock;
3491 update_cpu_load(rq); 3744 update_cpu_load(rq);
3492 if (curr != rq->idle) /* FIXME: needed? */ 3745 curr->sched_class->task_tick(rq, curr, 0);
3493 curr->sched_class->task_tick(rq, curr); 3746 update_sched_rt_period(rq);
3494 spin_unlock(&rq->lock); 3747 spin_unlock(&rq->lock);
3495 3748
3496#ifdef CONFIG_SMP 3749#ifdef CONFIG_SMP
@@ -3636,6 +3889,8 @@ need_resched_nonpreemptible:
3636 3889
3637 schedule_debug(prev); 3890 schedule_debug(prev);
3638 3891
3892 hrtick_clear(rq);
3893
3639 /* 3894 /*
3640 * Do the rq-clock update outside the rq lock: 3895 * Do the rq-clock update outside the rq lock:
3641 */ 3896 */
@@ -3654,6 +3909,11 @@ need_resched_nonpreemptible:
3654 switch_count = &prev->nvcsw; 3909 switch_count = &prev->nvcsw;
3655 } 3910 }
3656 3911
3912#ifdef CONFIG_SMP
3913 if (prev->sched_class->pre_schedule)
3914 prev->sched_class->pre_schedule(rq, prev);
3915#endif
3916
3657 if (unlikely(!rq->nr_running)) 3917 if (unlikely(!rq->nr_running))
3658 idle_balance(cpu, rq); 3918 idle_balance(cpu, rq);
3659 3919
@@ -3668,14 +3928,20 @@ need_resched_nonpreemptible:
3668 ++*switch_count; 3928 ++*switch_count;
3669 3929
3670 context_switch(rq, prev, next); /* unlocks the rq */ 3930 context_switch(rq, prev, next); /* unlocks the rq */
3931 /*
3932 * the context switch might have flipped the stack from under
3933 * us, hence refresh the local variables.
3934 */
3935 cpu = smp_processor_id();
3936 rq = cpu_rq(cpu);
3671 } else 3937 } else
3672 spin_unlock_irq(&rq->lock); 3938 spin_unlock_irq(&rq->lock);
3673 3939
3674 if (unlikely(reacquire_kernel_lock(current) < 0)) { 3940 hrtick_set(rq);
3675 cpu = smp_processor_id(); 3941
3676 rq = cpu_rq(cpu); 3942 if (unlikely(reacquire_kernel_lock(current) < 0))
3677 goto need_resched_nonpreemptible; 3943 goto need_resched_nonpreemptible;
3678 } 3944
3679 preempt_enable_no_resched(); 3945 preempt_enable_no_resched();
3680 if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) 3946 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3681 goto need_resched; 3947 goto need_resched;
@@ -3691,10 +3957,9 @@ EXPORT_SYMBOL(schedule);
3691asmlinkage void __sched preempt_schedule(void) 3957asmlinkage void __sched preempt_schedule(void)
3692{ 3958{
3693 struct thread_info *ti = current_thread_info(); 3959 struct thread_info *ti = current_thread_info();
3694#ifdef CONFIG_PREEMPT_BKL
3695 struct task_struct *task = current; 3960 struct task_struct *task = current;
3696 int saved_lock_depth; 3961 int saved_lock_depth;
3697#endif 3962
3698 /* 3963 /*
3699 * If there is a non-zero preempt_count or interrupts are disabled, 3964 * If there is a non-zero preempt_count or interrupts are disabled,
3700 * we do not want to preempt the current task. Just return.. 3965 * we do not want to preempt the current task. Just return..
@@ -3710,14 +3975,10 @@ asmlinkage void __sched preempt_schedule(void)
3710 * clear ->lock_depth so that schedule() doesnt 3975 * clear ->lock_depth so that schedule() doesnt
3711 * auto-release the semaphore: 3976 * auto-release the semaphore:
3712 */ 3977 */
3713#ifdef CONFIG_PREEMPT_BKL
3714 saved_lock_depth = task->lock_depth; 3978 saved_lock_depth = task->lock_depth;
3715 task->lock_depth = -1; 3979 task->lock_depth = -1;
3716#endif
3717 schedule(); 3980 schedule();
3718#ifdef CONFIG_PREEMPT_BKL
3719 task->lock_depth = saved_lock_depth; 3981 task->lock_depth = saved_lock_depth;
3720#endif
3721 sub_preempt_count(PREEMPT_ACTIVE); 3982 sub_preempt_count(PREEMPT_ACTIVE);
3722 3983
3723 /* 3984 /*
@@ -3738,10 +3999,9 @@ EXPORT_SYMBOL(preempt_schedule);
3738asmlinkage void __sched preempt_schedule_irq(void) 3999asmlinkage void __sched preempt_schedule_irq(void)
3739{ 4000{
3740 struct thread_info *ti = current_thread_info(); 4001 struct thread_info *ti = current_thread_info();
3741#ifdef CONFIG_PREEMPT_BKL
3742 struct task_struct *task = current; 4002 struct task_struct *task = current;
3743 int saved_lock_depth; 4003 int saved_lock_depth;
3744#endif 4004
3745 /* Catch callers which need to be fixed */ 4005 /* Catch callers which need to be fixed */
3746 BUG_ON(ti->preempt_count || !irqs_disabled()); 4006 BUG_ON(ti->preempt_count || !irqs_disabled());
3747 4007
@@ -3753,16 +4013,12 @@ asmlinkage void __sched preempt_schedule_irq(void)
3753 * clear ->lock_depth so that schedule() doesnt 4013 * clear ->lock_depth so that schedule() doesnt
3754 * auto-release the semaphore: 4014 * auto-release the semaphore:
3755 */ 4015 */
3756#ifdef CONFIG_PREEMPT_BKL
3757 saved_lock_depth = task->lock_depth; 4016 saved_lock_depth = task->lock_depth;
3758 task->lock_depth = -1; 4017 task->lock_depth = -1;
3759#endif
3760 local_irq_enable(); 4018 local_irq_enable();
3761 schedule(); 4019 schedule();
3762 local_irq_disable(); 4020 local_irq_disable();
3763#ifdef CONFIG_PREEMPT_BKL
3764 task->lock_depth = saved_lock_depth; 4021 task->lock_depth = saved_lock_depth;
3765#endif
3766 sub_preempt_count(PREEMPT_ACTIVE); 4022 sub_preempt_count(PREEMPT_ACTIVE);
3767 4023
3768 /* 4024 /*
@@ -4019,6 +4275,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4019 unsigned long flags; 4275 unsigned long flags;
4020 int oldprio, on_rq, running; 4276 int oldprio, on_rq, running;
4021 struct rq *rq; 4277 struct rq *rq;
4278 const struct sched_class *prev_class = p->sched_class;
4022 4279
4023 BUG_ON(prio < 0 || prio > MAX_PRIO); 4280 BUG_ON(prio < 0 || prio > MAX_PRIO);
4024 4281
@@ -4044,18 +4301,10 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4044 if (on_rq) { 4301 if (on_rq) {
4045 if (running) 4302 if (running)
4046 p->sched_class->set_curr_task(rq); 4303 p->sched_class->set_curr_task(rq);
4304
4047 enqueue_task(rq, p, 0); 4305 enqueue_task(rq, p, 0);
4048 /* 4306
4049 * Reschedule if we are currently running on this runqueue and 4307 check_class_changed(rq, p, prev_class, oldprio, running);
4050 * our priority decreased, or if we are not currently running on
4051 * this runqueue and our priority is higher than the current's
4052 */
4053 if (running) {
4054 if (p->prio > oldprio)
4055 resched_task(rq->curr);
4056 } else {
4057 check_preempt_curr(rq, p);
4058 }
4059 } 4308 }
4060 task_rq_unlock(rq, &flags); 4309 task_rq_unlock(rq, &flags);
4061} 4310}
@@ -4087,10 +4336,8 @@ void set_user_nice(struct task_struct *p, long nice)
4087 goto out_unlock; 4336 goto out_unlock;
4088 } 4337 }
4089 on_rq = p->se.on_rq; 4338 on_rq = p->se.on_rq;
4090 if (on_rq) { 4339 if (on_rq)
4091 dequeue_task(rq, p, 0); 4340 dequeue_task(rq, p, 0);
4092 dec_load(rq, p);
4093 }
4094 4341
4095 p->static_prio = NICE_TO_PRIO(nice); 4342 p->static_prio = NICE_TO_PRIO(nice);
4096 set_load_weight(p); 4343 set_load_weight(p);
@@ -4100,7 +4347,6 @@ void set_user_nice(struct task_struct *p, long nice)
4100 4347
4101 if (on_rq) { 4348 if (on_rq) {
4102 enqueue_task(rq, p, 0); 4349 enqueue_task(rq, p, 0);
4103 inc_load(rq, p);
4104 /* 4350 /*
4105 * If the task increased its priority or is running and 4351 * If the task increased its priority or is running and
4106 * lowered its priority, then reschedule its CPU: 4352 * lowered its priority, then reschedule its CPU:
@@ -4258,6 +4504,7 @@ int sched_setscheduler(struct task_struct *p, int policy,
4258{ 4504{
4259 int retval, oldprio, oldpolicy = -1, on_rq, running; 4505 int retval, oldprio, oldpolicy = -1, on_rq, running;
4260 unsigned long flags; 4506 unsigned long flags;
4507 const struct sched_class *prev_class = p->sched_class;
4261 struct rq *rq; 4508 struct rq *rq;
4262 4509
4263 /* may grab non-irq protected spin_locks */ 4510 /* may grab non-irq protected spin_locks */
@@ -4351,18 +4598,10 @@ recheck:
4351 if (on_rq) { 4598 if (on_rq) {
4352 if (running) 4599 if (running)
4353 p->sched_class->set_curr_task(rq); 4600 p->sched_class->set_curr_task(rq);
4601
4354 activate_task(rq, p, 0); 4602 activate_task(rq, p, 0);
4355 /* 4603
4356 * Reschedule if we are currently running on this runqueue and 4604 check_class_changed(rq, p, prev_class, oldprio, running);
4357 * our priority decreased, or if we are not currently running on
4358 * this runqueue and our priority is higher than the current's
4359 */
4360 if (running) {
4361 if (p->prio > oldprio)
4362 resched_task(rq->curr);
4363 } else {
4364 check_preempt_curr(rq, p);
4365 }
4366 } 4605 }
4367 __task_rq_unlock(rq); 4606 __task_rq_unlock(rq);
4368 spin_unlock_irqrestore(&p->pi_lock, flags); 4607 spin_unlock_irqrestore(&p->pi_lock, flags);
@@ -4490,13 +4729,13 @@ long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4490 struct task_struct *p; 4729 struct task_struct *p;
4491 int retval; 4730 int retval;
4492 4731
4493 mutex_lock(&sched_hotcpu_mutex); 4732 get_online_cpus();
4494 read_lock(&tasklist_lock); 4733 read_lock(&tasklist_lock);
4495 4734
4496 p = find_process_by_pid(pid); 4735 p = find_process_by_pid(pid);
4497 if (!p) { 4736 if (!p) {
4498 read_unlock(&tasklist_lock); 4737 read_unlock(&tasklist_lock);
4499 mutex_unlock(&sched_hotcpu_mutex); 4738 put_online_cpus();
4500 return -ESRCH; 4739 return -ESRCH;
4501 } 4740 }
4502 4741
@@ -4536,7 +4775,7 @@ long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4536 } 4775 }
4537out_unlock: 4776out_unlock:
4538 put_task_struct(p); 4777 put_task_struct(p);
4539 mutex_unlock(&sched_hotcpu_mutex); 4778 put_online_cpus();
4540 return retval; 4779 return retval;
4541} 4780}
4542 4781
@@ -4593,7 +4832,7 @@ long sched_getaffinity(pid_t pid, cpumask_t *mask)
4593 struct task_struct *p; 4832 struct task_struct *p;
4594 int retval; 4833 int retval;
4595 4834
4596 mutex_lock(&sched_hotcpu_mutex); 4835 get_online_cpus();
4597 read_lock(&tasklist_lock); 4836 read_lock(&tasklist_lock);
4598 4837
4599 retval = -ESRCH; 4838 retval = -ESRCH;
@@ -4609,7 +4848,7 @@ long sched_getaffinity(pid_t pid, cpumask_t *mask)
4609 4848
4610out_unlock: 4849out_unlock:
4611 read_unlock(&tasklist_lock); 4850 read_unlock(&tasklist_lock);
4612 mutex_unlock(&sched_hotcpu_mutex); 4851 put_online_cpus();
4613 4852
4614 return retval; 4853 return retval;
4615} 4854}
@@ -4683,7 +4922,8 @@ static void __cond_resched(void)
4683 } while (need_resched()); 4922 } while (need_resched());
4684} 4923}
4685 4924
4686int __sched cond_resched(void) 4925#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
4926int __sched _cond_resched(void)
4687{ 4927{
4688 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) && 4928 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
4689 system_state == SYSTEM_RUNNING) { 4929 system_state == SYSTEM_RUNNING) {
@@ -4692,7 +4932,8 @@ int __sched cond_resched(void)
4692 } 4932 }
4693 return 0; 4933 return 0;
4694} 4934}
4695EXPORT_SYMBOL(cond_resched); 4935EXPORT_SYMBOL(_cond_resched);
4936#endif
4696 4937
4697/* 4938/*
4698 * cond_resched_lock() - if a reschedule is pending, drop the given lock, 4939 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
@@ -4890,7 +5131,7 @@ out_unlock:
4890 5131
4891static const char stat_nam[] = "RSDTtZX"; 5132static const char stat_nam[] = "RSDTtZX";
4892 5133
4893static void show_task(struct task_struct *p) 5134void sched_show_task(struct task_struct *p)
4894{ 5135{
4895 unsigned long free = 0; 5136 unsigned long free = 0;
4896 unsigned state; 5137 unsigned state;
@@ -4920,8 +5161,7 @@ static void show_task(struct task_struct *p)
4920 printk(KERN_CONT "%5lu %5d %6d\n", free, 5161 printk(KERN_CONT "%5lu %5d %6d\n", free,
4921 task_pid_nr(p), task_pid_nr(p->real_parent)); 5162 task_pid_nr(p), task_pid_nr(p->real_parent));
4922 5163
4923 if (state != TASK_RUNNING) 5164 show_stack(p, NULL);
4924 show_stack(p, NULL);
4925} 5165}
4926 5166
4927void show_state_filter(unsigned long state_filter) 5167void show_state_filter(unsigned long state_filter)
@@ -4943,7 +5183,7 @@ void show_state_filter(unsigned long state_filter)
4943 */ 5183 */
4944 touch_nmi_watchdog(); 5184 touch_nmi_watchdog();
4945 if (!state_filter || (p->state & state_filter)) 5185 if (!state_filter || (p->state & state_filter))
4946 show_task(p); 5186 sched_show_task(p);
4947 } while_each_thread(g, p); 5187 } while_each_thread(g, p);
4948 5188
4949 touch_all_softlockup_watchdogs(); 5189 touch_all_softlockup_watchdogs();
@@ -4992,11 +5232,8 @@ void __cpuinit init_idle(struct task_struct *idle, int cpu)
4992 spin_unlock_irqrestore(&rq->lock, flags); 5232 spin_unlock_irqrestore(&rq->lock, flags);
4993 5233
4994 /* Set the preempt count _outside_ the spinlocks! */ 5234 /* Set the preempt count _outside_ the spinlocks! */
4995#if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL)
4996 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
4997#else
4998 task_thread_info(idle)->preempt_count = 0; 5235 task_thread_info(idle)->preempt_count = 0;
4999#endif 5236
5000 /* 5237 /*
5001 * The idle tasks have their own, simple scheduling class: 5238 * The idle tasks have their own, simple scheduling class:
5002 */ 5239 */
@@ -5077,7 +5314,13 @@ int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
5077 goto out; 5314 goto out;
5078 } 5315 }
5079 5316
5080 p->cpus_allowed = new_mask; 5317 if (p->sched_class->set_cpus_allowed)
5318 p->sched_class->set_cpus_allowed(p, &new_mask);
5319 else {
5320 p->cpus_allowed = new_mask;
5321 p->rt.nr_cpus_allowed = cpus_weight(new_mask);
5322 }
5323
5081 /* Can the task run on the task's current CPU? If so, we're done */ 5324 /* Can the task run on the task's current CPU? If so, we're done */
5082 if (cpu_isset(task_cpu(p), new_mask)) 5325 if (cpu_isset(task_cpu(p), new_mask))
5083 goto out; 5326 goto out;
@@ -5569,9 +5812,6 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5569 struct rq *rq; 5812 struct rq *rq;
5570 5813
5571 switch (action) { 5814 switch (action) {
5572 case CPU_LOCK_ACQUIRE:
5573 mutex_lock(&sched_hotcpu_mutex);
5574 break;
5575 5815
5576 case CPU_UP_PREPARE: 5816 case CPU_UP_PREPARE:
5577 case CPU_UP_PREPARE_FROZEN: 5817 case CPU_UP_PREPARE_FROZEN:
@@ -5590,6 +5830,15 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5590 case CPU_ONLINE_FROZEN: 5830 case CPU_ONLINE_FROZEN:
5591 /* Strictly unnecessary, as first user will wake it. */ 5831 /* Strictly unnecessary, as first user will wake it. */
5592 wake_up_process(cpu_rq(cpu)->migration_thread); 5832 wake_up_process(cpu_rq(cpu)->migration_thread);
5833
5834 /* Update our root-domain */
5835 rq = cpu_rq(cpu);
5836 spin_lock_irqsave(&rq->lock, flags);
5837 if (rq->rd) {
5838 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5839 cpu_set(cpu, rq->rd->online);
5840 }
5841 spin_unlock_irqrestore(&rq->lock, flags);
5593 break; 5842 break;
5594 5843
5595#ifdef CONFIG_HOTPLUG_CPU 5844#ifdef CONFIG_HOTPLUG_CPU
@@ -5640,10 +5889,18 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5640 } 5889 }
5641 spin_unlock_irq(&rq->lock); 5890 spin_unlock_irq(&rq->lock);
5642 break; 5891 break;
5643#endif 5892
5644 case CPU_LOCK_RELEASE: 5893 case CPU_DOWN_PREPARE:
5645 mutex_unlock(&sched_hotcpu_mutex); 5894 /* Update our root-domain */
5895 rq = cpu_rq(cpu);
5896 spin_lock_irqsave(&rq->lock, flags);
5897 if (rq->rd) {
5898 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5899 cpu_clear(cpu, rq->rd->online);
5900 }
5901 spin_unlock_irqrestore(&rq->lock, flags);
5646 break; 5902 break;
5903#endif
5647 } 5904 }
5648 return NOTIFY_OK; 5905 return NOTIFY_OK;
5649} 5906}
@@ -5831,11 +6088,76 @@ sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
5831 return 1; 6088 return 1;
5832} 6089}
5833 6090
6091static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6092{
6093 unsigned long flags;
6094 const struct sched_class *class;
6095
6096 spin_lock_irqsave(&rq->lock, flags);
6097
6098 if (rq->rd) {
6099 struct root_domain *old_rd = rq->rd;
6100
6101 for (class = sched_class_highest; class; class = class->next) {
6102 if (class->leave_domain)
6103 class->leave_domain(rq);
6104 }
6105
6106 cpu_clear(rq->cpu, old_rd->span);
6107 cpu_clear(rq->cpu, old_rd->online);
6108
6109 if (atomic_dec_and_test(&old_rd->refcount))
6110 kfree(old_rd);
6111 }
6112
6113 atomic_inc(&rd->refcount);
6114 rq->rd = rd;
6115
6116 cpu_set(rq->cpu, rd->span);
6117 if (cpu_isset(rq->cpu, cpu_online_map))
6118 cpu_set(rq->cpu, rd->online);
6119
6120 for (class = sched_class_highest; class; class = class->next) {
6121 if (class->join_domain)
6122 class->join_domain(rq);
6123 }
6124
6125 spin_unlock_irqrestore(&rq->lock, flags);
6126}
6127
6128static void init_rootdomain(struct root_domain *rd)
6129{
6130 memset(rd, 0, sizeof(*rd));
6131
6132 cpus_clear(rd->span);
6133 cpus_clear(rd->online);
6134}
6135
6136static void init_defrootdomain(void)
6137{
6138 init_rootdomain(&def_root_domain);
6139 atomic_set(&def_root_domain.refcount, 1);
6140}
6141
6142static struct root_domain *alloc_rootdomain(void)
6143{
6144 struct root_domain *rd;
6145
6146 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6147 if (!rd)
6148 return NULL;
6149
6150 init_rootdomain(rd);
6151
6152 return rd;
6153}
6154
5834/* 6155/*
5835 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must 6156 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
5836 * hold the hotplug lock. 6157 * hold the hotplug lock.
5837 */ 6158 */
5838static void cpu_attach_domain(struct sched_domain *sd, int cpu) 6159static void
6160cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
5839{ 6161{
5840 struct rq *rq = cpu_rq(cpu); 6162 struct rq *rq = cpu_rq(cpu);
5841 struct sched_domain *tmp; 6163 struct sched_domain *tmp;
@@ -5860,6 +6182,7 @@ static void cpu_attach_domain(struct sched_domain *sd, int cpu)
5860 6182
5861 sched_domain_debug(sd, cpu); 6183 sched_domain_debug(sd, cpu);
5862 6184
6185 rq_attach_root(rq, rd);
5863 rcu_assign_pointer(rq->sd, sd); 6186 rcu_assign_pointer(rq->sd, sd);
5864} 6187}
5865 6188
@@ -6228,6 +6551,7 @@ static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6228static int build_sched_domains(const cpumask_t *cpu_map) 6551static int build_sched_domains(const cpumask_t *cpu_map)
6229{ 6552{
6230 int i; 6553 int i;
6554 struct root_domain *rd;
6231#ifdef CONFIG_NUMA 6555#ifdef CONFIG_NUMA
6232 struct sched_group **sched_group_nodes = NULL; 6556 struct sched_group **sched_group_nodes = NULL;
6233 int sd_allnodes = 0; 6557 int sd_allnodes = 0;
@@ -6244,6 +6568,12 @@ static int build_sched_domains(const cpumask_t *cpu_map)
6244 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; 6568 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6245#endif 6569#endif
6246 6570
6571 rd = alloc_rootdomain();
6572 if (!rd) {
6573 printk(KERN_WARNING "Cannot alloc root domain\n");
6574 return -ENOMEM;
6575 }
6576
6247 /* 6577 /*
6248 * Set up domains for cpus specified by the cpu_map. 6578 * Set up domains for cpus specified by the cpu_map.
6249 */ 6579 */
@@ -6460,7 +6790,7 @@ static int build_sched_domains(const cpumask_t *cpu_map)
6460#else 6790#else
6461 sd = &per_cpu(phys_domains, i); 6791 sd = &per_cpu(phys_domains, i);
6462#endif 6792#endif
6463 cpu_attach_domain(sd, i); 6793 cpu_attach_domain(sd, rd, i);
6464 } 6794 }
6465 6795
6466 return 0; 6796 return 0;
@@ -6518,7 +6848,7 @@ static void detach_destroy_domains(const cpumask_t *cpu_map)
6518 unregister_sched_domain_sysctl(); 6848 unregister_sched_domain_sysctl();
6519 6849
6520 for_each_cpu_mask(i, *cpu_map) 6850 for_each_cpu_mask(i, *cpu_map)
6521 cpu_attach_domain(NULL, i); 6851 cpu_attach_domain(NULL, &def_root_domain, i);
6522 synchronize_sched(); 6852 synchronize_sched();
6523 arch_destroy_sched_domains(cpu_map); 6853 arch_destroy_sched_domains(cpu_map);
6524} 6854}
@@ -6548,6 +6878,8 @@ void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
6548{ 6878{
6549 int i, j; 6879 int i, j;
6550 6880
6881 lock_doms_cur();
6882
6551 /* always unregister in case we don't destroy any domains */ 6883 /* always unregister in case we don't destroy any domains */
6552 unregister_sched_domain_sysctl(); 6884 unregister_sched_domain_sysctl();
6553 6885
@@ -6588,6 +6920,8 @@ match2:
6588 ndoms_cur = ndoms_new; 6920 ndoms_cur = ndoms_new;
6589 6921
6590 register_sched_domain_sysctl(); 6922 register_sched_domain_sysctl();
6923
6924 unlock_doms_cur();
6591} 6925}
6592 6926
6593#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) 6927#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
@@ -6595,10 +6929,10 @@ static int arch_reinit_sched_domains(void)
6595{ 6929{
6596 int err; 6930 int err;
6597 6931
6598 mutex_lock(&sched_hotcpu_mutex); 6932 get_online_cpus();
6599 detach_destroy_domains(&cpu_online_map); 6933 detach_destroy_domains(&cpu_online_map);
6600 err = arch_init_sched_domains(&cpu_online_map); 6934 err = arch_init_sched_domains(&cpu_online_map);
6601 mutex_unlock(&sched_hotcpu_mutex); 6935 put_online_cpus();
6602 6936
6603 return err; 6937 return err;
6604} 6938}
@@ -6709,12 +7043,12 @@ void __init sched_init_smp(void)
6709{ 7043{
6710 cpumask_t non_isolated_cpus; 7044 cpumask_t non_isolated_cpus;
6711 7045
6712 mutex_lock(&sched_hotcpu_mutex); 7046 get_online_cpus();
6713 arch_init_sched_domains(&cpu_online_map); 7047 arch_init_sched_domains(&cpu_online_map);
6714 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); 7048 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
6715 if (cpus_empty(non_isolated_cpus)) 7049 if (cpus_empty(non_isolated_cpus))
6716 cpu_set(smp_processor_id(), non_isolated_cpus); 7050 cpu_set(smp_processor_id(), non_isolated_cpus);
6717 mutex_unlock(&sched_hotcpu_mutex); 7051 put_online_cpus();
6718 /* XXX: Theoretical race here - CPU may be hotplugged now */ 7052 /* XXX: Theoretical race here - CPU may be hotplugged now */
6719 hotcpu_notifier(update_sched_domains, 0); 7053 hotcpu_notifier(update_sched_domains, 0);
6720 7054
@@ -6722,6 +7056,21 @@ void __init sched_init_smp(void)
6722 if (set_cpus_allowed(current, non_isolated_cpus) < 0) 7056 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
6723 BUG(); 7057 BUG();
6724 sched_init_granularity(); 7058 sched_init_granularity();
7059
7060#ifdef CONFIG_FAIR_GROUP_SCHED
7061 if (nr_cpu_ids == 1)
7062 return;
7063
7064 lb_monitor_task = kthread_create(load_balance_monitor, NULL,
7065 "group_balance");
7066 if (!IS_ERR(lb_monitor_task)) {
7067 lb_monitor_task->flags |= PF_NOFREEZE;
7068 wake_up_process(lb_monitor_task);
7069 } else {
7070 printk(KERN_ERR "Could not create load balance monitor thread"
7071 "(error = %ld) \n", PTR_ERR(lb_monitor_task));
7072 }
7073#endif
6725} 7074}
6726#else 7075#else
6727void __init sched_init_smp(void) 7076void __init sched_init_smp(void)
@@ -6746,13 +7095,87 @@ static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
6746 cfs_rq->min_vruntime = (u64)(-(1LL << 20)); 7095 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
6747} 7096}
6748 7097
7098static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7099{
7100 struct rt_prio_array *array;
7101 int i;
7102
7103 array = &rt_rq->active;
7104 for (i = 0; i < MAX_RT_PRIO; i++) {
7105 INIT_LIST_HEAD(array->queue + i);
7106 __clear_bit(i, array->bitmap);
7107 }
7108 /* delimiter for bitsearch: */
7109 __set_bit(MAX_RT_PRIO, array->bitmap);
7110
7111#if defined CONFIG_SMP || defined CONFIG_FAIR_GROUP_SCHED
7112 rt_rq->highest_prio = MAX_RT_PRIO;
7113#endif
7114#ifdef CONFIG_SMP
7115 rt_rq->rt_nr_migratory = 0;
7116 rt_rq->overloaded = 0;
7117#endif
7118
7119 rt_rq->rt_time = 0;
7120 rt_rq->rt_throttled = 0;
7121
7122#ifdef CONFIG_FAIR_GROUP_SCHED
7123 rt_rq->rq = rq;
7124#endif
7125}
7126
7127#ifdef CONFIG_FAIR_GROUP_SCHED
7128static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg,
7129 struct cfs_rq *cfs_rq, struct sched_entity *se,
7130 int cpu, int add)
7131{
7132 tg->cfs_rq[cpu] = cfs_rq;
7133 init_cfs_rq(cfs_rq, rq);
7134 cfs_rq->tg = tg;
7135 if (add)
7136 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7137
7138 tg->se[cpu] = se;
7139 se->cfs_rq = &rq->cfs;
7140 se->my_q = cfs_rq;
7141 se->load.weight = tg->shares;
7142 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
7143 se->parent = NULL;
7144}
7145
7146static void init_tg_rt_entry(struct rq *rq, struct task_group *tg,
7147 struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
7148 int cpu, int add)
7149{
7150 tg->rt_rq[cpu] = rt_rq;
7151 init_rt_rq(rt_rq, rq);
7152 rt_rq->tg = tg;
7153 rt_rq->rt_se = rt_se;
7154 if (add)
7155 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7156
7157 tg->rt_se[cpu] = rt_se;
7158 rt_se->rt_rq = &rq->rt;
7159 rt_se->my_q = rt_rq;
7160 rt_se->parent = NULL;
7161 INIT_LIST_HEAD(&rt_se->run_list);
7162}
7163#endif
7164
6749void __init sched_init(void) 7165void __init sched_init(void)
6750{ 7166{
6751 int highest_cpu = 0; 7167 int highest_cpu = 0;
6752 int i, j; 7168 int i, j;
6753 7169
7170#ifdef CONFIG_SMP
7171 init_defrootdomain();
7172#endif
7173
7174#ifdef CONFIG_FAIR_GROUP_SCHED
7175 list_add(&init_task_group.list, &task_groups);
7176#endif
7177
6754 for_each_possible_cpu(i) { 7178 for_each_possible_cpu(i) {
6755 struct rt_prio_array *array;
6756 struct rq *rq; 7179 struct rq *rq;
6757 7180
6758 rq = cpu_rq(i); 7181 rq = cpu_rq(i);
@@ -6761,52 +7184,39 @@ void __init sched_init(void)
6761 rq->nr_running = 0; 7184 rq->nr_running = 0;
6762 rq->clock = 1; 7185 rq->clock = 1;
6763 init_cfs_rq(&rq->cfs, rq); 7186 init_cfs_rq(&rq->cfs, rq);
7187 init_rt_rq(&rq->rt, rq);
6764#ifdef CONFIG_FAIR_GROUP_SCHED 7188#ifdef CONFIG_FAIR_GROUP_SCHED
6765 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
6766 {
6767 struct cfs_rq *cfs_rq = &per_cpu(init_cfs_rq, i);
6768 struct sched_entity *se =
6769 &per_cpu(init_sched_entity, i);
6770
6771 init_cfs_rq_p[i] = cfs_rq;
6772 init_cfs_rq(cfs_rq, rq);
6773 cfs_rq->tg = &init_task_group;
6774 list_add(&cfs_rq->leaf_cfs_rq_list,
6775 &rq->leaf_cfs_rq_list);
6776
6777 init_sched_entity_p[i] = se;
6778 se->cfs_rq = &rq->cfs;
6779 se->my_q = cfs_rq;
6780 se->load.weight = init_task_group_load;
6781 se->load.inv_weight =
6782 div64_64(1ULL<<32, init_task_group_load);
6783 se->parent = NULL;
6784 }
6785 init_task_group.shares = init_task_group_load; 7189 init_task_group.shares = init_task_group_load;
6786 spin_lock_init(&init_task_group.lock); 7190 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
7191 init_tg_cfs_entry(rq, &init_task_group,
7192 &per_cpu(init_cfs_rq, i),
7193 &per_cpu(init_sched_entity, i), i, 1);
7194
7195 init_task_group.rt_ratio = sysctl_sched_rt_ratio; /* XXX */
7196 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
7197 init_tg_rt_entry(rq, &init_task_group,
7198 &per_cpu(init_rt_rq, i),
7199 &per_cpu(init_sched_rt_entity, i), i, 1);
6787#endif 7200#endif
7201 rq->rt_period_expire = 0;
7202 rq->rt_throttled = 0;
6788 7203
6789 for (j = 0; j < CPU_LOAD_IDX_MAX; j++) 7204 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
6790 rq->cpu_load[j] = 0; 7205 rq->cpu_load[j] = 0;
6791#ifdef CONFIG_SMP 7206#ifdef CONFIG_SMP
6792 rq->sd = NULL; 7207 rq->sd = NULL;
7208 rq->rd = NULL;
6793 rq->active_balance = 0; 7209 rq->active_balance = 0;
6794 rq->next_balance = jiffies; 7210 rq->next_balance = jiffies;
6795 rq->push_cpu = 0; 7211 rq->push_cpu = 0;
6796 rq->cpu = i; 7212 rq->cpu = i;
6797 rq->migration_thread = NULL; 7213 rq->migration_thread = NULL;
6798 INIT_LIST_HEAD(&rq->migration_queue); 7214 INIT_LIST_HEAD(&rq->migration_queue);
7215 rq_attach_root(rq, &def_root_domain);
6799#endif 7216#endif
7217 init_rq_hrtick(rq);
6800 atomic_set(&rq->nr_iowait, 0); 7218 atomic_set(&rq->nr_iowait, 0);
6801
6802 array = &rq->rt.active;
6803 for (j = 0; j < MAX_RT_PRIO; j++) {
6804 INIT_LIST_HEAD(array->queue + j);
6805 __clear_bit(j, array->bitmap);
6806 }
6807 highest_cpu = i; 7219 highest_cpu = i;
6808 /* delimiter for bitsearch: */
6809 __set_bit(MAX_RT_PRIO, array->bitmap);
6810 } 7220 }
6811 7221
6812 set_load_weight(&init_task); 7222 set_load_weight(&init_task);
@@ -6975,12 +7385,187 @@ void set_curr_task(int cpu, struct task_struct *p)
6975 7385
6976#ifdef CONFIG_FAIR_GROUP_SCHED 7386#ifdef CONFIG_FAIR_GROUP_SCHED
6977 7387
7388#ifdef CONFIG_SMP
7389/*
7390 * distribute shares of all task groups among their schedulable entities,
7391 * to reflect load distribution across cpus.
7392 */
7393static int rebalance_shares(struct sched_domain *sd, int this_cpu)
7394{
7395 struct cfs_rq *cfs_rq;
7396 struct rq *rq = cpu_rq(this_cpu);
7397 cpumask_t sdspan = sd->span;
7398 int balanced = 1;
7399
7400 /* Walk thr' all the task groups that we have */
7401 for_each_leaf_cfs_rq(rq, cfs_rq) {
7402 int i;
7403 unsigned long total_load = 0, total_shares;
7404 struct task_group *tg = cfs_rq->tg;
7405
7406 /* Gather total task load of this group across cpus */
7407 for_each_cpu_mask(i, sdspan)
7408 total_load += tg->cfs_rq[i]->load.weight;
7409
7410 /* Nothing to do if this group has no load */
7411 if (!total_load)
7412 continue;
7413
7414 /*
7415 * tg->shares represents the number of cpu shares the task group
7416 * is eligible to hold on a single cpu. On N cpus, it is
7417 * eligible to hold (N * tg->shares) number of cpu shares.
7418 */
7419 total_shares = tg->shares * cpus_weight(sdspan);
7420
7421 /*
7422 * redistribute total_shares across cpus as per the task load
7423 * distribution.
7424 */
7425 for_each_cpu_mask(i, sdspan) {
7426 unsigned long local_load, local_shares;
7427
7428 local_load = tg->cfs_rq[i]->load.weight;
7429 local_shares = (local_load * total_shares) / total_load;
7430 if (!local_shares)
7431 local_shares = MIN_GROUP_SHARES;
7432 if (local_shares == tg->se[i]->load.weight)
7433 continue;
7434
7435 spin_lock_irq(&cpu_rq(i)->lock);
7436 set_se_shares(tg->se[i], local_shares);
7437 spin_unlock_irq(&cpu_rq(i)->lock);
7438 balanced = 0;
7439 }
7440 }
7441
7442 return balanced;
7443}
7444
7445/*
7446 * How frequently should we rebalance_shares() across cpus?
7447 *
7448 * The more frequently we rebalance shares, the more accurate is the fairness
7449 * of cpu bandwidth distribution between task groups. However higher frequency
7450 * also implies increased scheduling overhead.
7451 *
7452 * sysctl_sched_min_bal_int_shares represents the minimum interval between
7453 * consecutive calls to rebalance_shares() in the same sched domain.
7454 *
7455 * sysctl_sched_max_bal_int_shares represents the maximum interval between
7456 * consecutive calls to rebalance_shares() in the same sched domain.
7457 *
7458 * These settings allows for the appropriate trade-off between accuracy of
7459 * fairness and the associated overhead.
7460 *
7461 */
7462
7463/* default: 8ms, units: milliseconds */
7464const_debug unsigned int sysctl_sched_min_bal_int_shares = 8;
7465
7466/* default: 128ms, units: milliseconds */
7467const_debug unsigned int sysctl_sched_max_bal_int_shares = 128;
7468
7469/* kernel thread that runs rebalance_shares() periodically */
7470static int load_balance_monitor(void *unused)
7471{
7472 unsigned int timeout = sysctl_sched_min_bal_int_shares;
7473 struct sched_param schedparm;
7474 int ret;
7475
7476 /*
7477 * We don't want this thread's execution to be limited by the shares
7478 * assigned to default group (init_task_group). Hence make it run
7479 * as a SCHED_RR RT task at the lowest priority.
7480 */
7481 schedparm.sched_priority = 1;
7482 ret = sched_setscheduler(current, SCHED_RR, &schedparm);
7483 if (ret)
7484 printk(KERN_ERR "Couldn't set SCHED_RR policy for load balance"
7485 " monitor thread (error = %d) \n", ret);
7486
7487 while (!kthread_should_stop()) {
7488 int i, cpu, balanced = 1;
7489
7490 /* Prevent cpus going down or coming up */
7491 get_online_cpus();
7492 /* lockout changes to doms_cur[] array */
7493 lock_doms_cur();
7494 /*
7495 * Enter a rcu read-side critical section to safely walk rq->sd
7496 * chain on various cpus and to walk task group list
7497 * (rq->leaf_cfs_rq_list) in rebalance_shares().
7498 */
7499 rcu_read_lock();
7500
7501 for (i = 0; i < ndoms_cur; i++) {
7502 cpumask_t cpumap = doms_cur[i];
7503 struct sched_domain *sd = NULL, *sd_prev = NULL;
7504
7505 cpu = first_cpu(cpumap);
7506
7507 /* Find the highest domain at which to balance shares */
7508 for_each_domain(cpu, sd) {
7509 if (!(sd->flags & SD_LOAD_BALANCE))
7510 continue;
7511 sd_prev = sd;
7512 }
7513
7514 sd = sd_prev;
7515 /* sd == NULL? No load balance reqd in this domain */
7516 if (!sd)
7517 continue;
7518
7519 balanced &= rebalance_shares(sd, cpu);
7520 }
7521
7522 rcu_read_unlock();
7523
7524 unlock_doms_cur();
7525 put_online_cpus();
7526
7527 if (!balanced)
7528 timeout = sysctl_sched_min_bal_int_shares;
7529 else if (timeout < sysctl_sched_max_bal_int_shares)
7530 timeout *= 2;
7531
7532 msleep_interruptible(timeout);
7533 }
7534
7535 return 0;
7536}
7537#endif /* CONFIG_SMP */
7538
7539static void free_sched_group(struct task_group *tg)
7540{
7541 int i;
7542
7543 for_each_possible_cpu(i) {
7544 if (tg->cfs_rq)
7545 kfree(tg->cfs_rq[i]);
7546 if (tg->se)
7547 kfree(tg->se[i]);
7548 if (tg->rt_rq)
7549 kfree(tg->rt_rq[i]);
7550 if (tg->rt_se)
7551 kfree(tg->rt_se[i]);
7552 }
7553
7554 kfree(tg->cfs_rq);
7555 kfree(tg->se);
7556 kfree(tg->rt_rq);
7557 kfree(tg->rt_se);
7558 kfree(tg);
7559}
7560
6978/* allocate runqueue etc for a new task group */ 7561/* allocate runqueue etc for a new task group */
6979struct task_group *sched_create_group(void) 7562struct task_group *sched_create_group(void)
6980{ 7563{
6981 struct task_group *tg; 7564 struct task_group *tg;
6982 struct cfs_rq *cfs_rq; 7565 struct cfs_rq *cfs_rq;
6983 struct sched_entity *se; 7566 struct sched_entity *se;
7567 struct rt_rq *rt_rq;
7568 struct sched_rt_entity *rt_se;
6984 struct rq *rq; 7569 struct rq *rq;
6985 int i; 7570 int i;
6986 7571
@@ -6994,97 +7579,89 @@ struct task_group *sched_create_group(void)
6994 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL); 7579 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL);
6995 if (!tg->se) 7580 if (!tg->se)
6996 goto err; 7581 goto err;
7582 tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL);
7583 if (!tg->rt_rq)
7584 goto err;
7585 tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL);
7586 if (!tg->rt_se)
7587 goto err;
7588
7589 tg->shares = NICE_0_LOAD;
7590 tg->rt_ratio = 0; /* XXX */
6997 7591
6998 for_each_possible_cpu(i) { 7592 for_each_possible_cpu(i) {
6999 rq = cpu_rq(i); 7593 rq = cpu_rq(i);
7000 7594
7001 cfs_rq = kmalloc_node(sizeof(struct cfs_rq), GFP_KERNEL, 7595 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7002 cpu_to_node(i)); 7596 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7003 if (!cfs_rq) 7597 if (!cfs_rq)
7004 goto err; 7598 goto err;
7005 7599
7006 se = kmalloc_node(sizeof(struct sched_entity), GFP_KERNEL, 7600 se = kmalloc_node(sizeof(struct sched_entity),
7007 cpu_to_node(i)); 7601 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7008 if (!se) 7602 if (!se)
7009 goto err; 7603 goto err;
7010 7604
7011 memset(cfs_rq, 0, sizeof(struct cfs_rq)); 7605 rt_rq = kmalloc_node(sizeof(struct rt_rq),
7012 memset(se, 0, sizeof(struct sched_entity)); 7606 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7607 if (!rt_rq)
7608 goto err;
7013 7609
7014 tg->cfs_rq[i] = cfs_rq; 7610 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
7015 init_cfs_rq(cfs_rq, rq); 7611 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7016 cfs_rq->tg = tg; 7612 if (!rt_se)
7613 goto err;
7017 7614
7018 tg->se[i] = se; 7615 init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0);
7019 se->cfs_rq = &rq->cfs; 7616 init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0);
7020 se->my_q = cfs_rq;
7021 se->load.weight = NICE_0_LOAD;
7022 se->load.inv_weight = div64_64(1ULL<<32, NICE_0_LOAD);
7023 se->parent = NULL;
7024 } 7617 }
7025 7618
7619 lock_task_group_list();
7026 for_each_possible_cpu(i) { 7620 for_each_possible_cpu(i) {
7027 rq = cpu_rq(i); 7621 rq = cpu_rq(i);
7028 cfs_rq = tg->cfs_rq[i]; 7622 cfs_rq = tg->cfs_rq[i];
7029 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list); 7623 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7624 rt_rq = tg->rt_rq[i];
7625 list_add_rcu(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7030 } 7626 }
7031 7627 list_add_rcu(&tg->list, &task_groups);
7032 tg->shares = NICE_0_LOAD; 7628 unlock_task_group_list();
7033 spin_lock_init(&tg->lock);
7034 7629
7035 return tg; 7630 return tg;
7036 7631
7037err: 7632err:
7038 for_each_possible_cpu(i) { 7633 free_sched_group(tg);
7039 if (tg->cfs_rq)
7040 kfree(tg->cfs_rq[i]);
7041 if (tg->se)
7042 kfree(tg->se[i]);
7043 }
7044 kfree(tg->cfs_rq);
7045 kfree(tg->se);
7046 kfree(tg);
7047
7048 return ERR_PTR(-ENOMEM); 7634 return ERR_PTR(-ENOMEM);
7049} 7635}
7050 7636
7051/* rcu callback to free various structures associated with a task group */ 7637/* rcu callback to free various structures associated with a task group */
7052static void free_sched_group(struct rcu_head *rhp) 7638static void free_sched_group_rcu(struct rcu_head *rhp)
7053{ 7639{
7054 struct task_group *tg = container_of(rhp, struct task_group, rcu);
7055 struct cfs_rq *cfs_rq;
7056 struct sched_entity *se;
7057 int i;
7058
7059 /* now it should be safe to free those cfs_rqs */ 7640 /* now it should be safe to free those cfs_rqs */
7060 for_each_possible_cpu(i) { 7641 free_sched_group(container_of(rhp, struct task_group, rcu));
7061 cfs_rq = tg->cfs_rq[i];
7062 kfree(cfs_rq);
7063
7064 se = tg->se[i];
7065 kfree(se);
7066 }
7067
7068 kfree(tg->cfs_rq);
7069 kfree(tg->se);
7070 kfree(tg);
7071} 7642}
7072 7643
7073/* Destroy runqueue etc associated with a task group */ 7644/* Destroy runqueue etc associated with a task group */
7074void sched_destroy_group(struct task_group *tg) 7645void sched_destroy_group(struct task_group *tg)
7075{ 7646{
7076 struct cfs_rq *cfs_rq = NULL; 7647 struct cfs_rq *cfs_rq = NULL;
7648 struct rt_rq *rt_rq = NULL;
7077 int i; 7649 int i;
7078 7650
7651 lock_task_group_list();
7079 for_each_possible_cpu(i) { 7652 for_each_possible_cpu(i) {
7080 cfs_rq = tg->cfs_rq[i]; 7653 cfs_rq = tg->cfs_rq[i];
7081 list_del_rcu(&cfs_rq->leaf_cfs_rq_list); 7654 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
7655 rt_rq = tg->rt_rq[i];
7656 list_del_rcu(&rt_rq->leaf_rt_rq_list);
7082 } 7657 }
7658 list_del_rcu(&tg->list);
7659 unlock_task_group_list();
7083 7660
7084 BUG_ON(!cfs_rq); 7661 BUG_ON(!cfs_rq);
7085 7662
7086 /* wait for possible concurrent references to cfs_rqs complete */ 7663 /* wait for possible concurrent references to cfs_rqs complete */
7087 call_rcu(&tg->rcu, free_sched_group); 7664 call_rcu(&tg->rcu, free_sched_group_rcu);
7088} 7665}
7089 7666
7090/* change task's runqueue when it moves between groups. 7667/* change task's runqueue when it moves between groups.
@@ -7100,11 +7677,6 @@ void sched_move_task(struct task_struct *tsk)
7100 7677
7101 rq = task_rq_lock(tsk, &flags); 7678 rq = task_rq_lock(tsk, &flags);
7102 7679
7103 if (tsk->sched_class != &fair_sched_class) {
7104 set_task_cfs_rq(tsk, task_cpu(tsk));
7105 goto done;
7106 }
7107
7108 update_rq_clock(rq); 7680 update_rq_clock(rq);
7109 7681
7110 running = task_current(rq, tsk); 7682 running = task_current(rq, tsk);
@@ -7116,7 +7688,7 @@ void sched_move_task(struct task_struct *tsk)
7116 tsk->sched_class->put_prev_task(rq, tsk); 7688 tsk->sched_class->put_prev_task(rq, tsk);
7117 } 7689 }
7118 7690
7119 set_task_cfs_rq(tsk, task_cpu(tsk)); 7691 set_task_rq(tsk, task_cpu(tsk));
7120 7692
7121 if (on_rq) { 7693 if (on_rq) {
7122 if (unlikely(running)) 7694 if (unlikely(running))
@@ -7124,53 +7696,82 @@ void sched_move_task(struct task_struct *tsk)
7124 enqueue_task(rq, tsk, 0); 7696 enqueue_task(rq, tsk, 0);
7125 } 7697 }
7126 7698
7127done:
7128 task_rq_unlock(rq, &flags); 7699 task_rq_unlock(rq, &flags);
7129} 7700}
7130 7701
7702/* rq->lock to be locked by caller */
7131static void set_se_shares(struct sched_entity *se, unsigned long shares) 7703static void set_se_shares(struct sched_entity *se, unsigned long shares)
7132{ 7704{
7133 struct cfs_rq *cfs_rq = se->cfs_rq; 7705 struct cfs_rq *cfs_rq = se->cfs_rq;
7134 struct rq *rq = cfs_rq->rq; 7706 struct rq *rq = cfs_rq->rq;
7135 int on_rq; 7707 int on_rq;
7136 7708
7137 spin_lock_irq(&rq->lock); 7709 if (!shares)
7710 shares = MIN_GROUP_SHARES;
7138 7711
7139 on_rq = se->on_rq; 7712 on_rq = se->on_rq;
7140 if (on_rq) 7713 if (on_rq) {
7141 dequeue_entity(cfs_rq, se, 0); 7714 dequeue_entity(cfs_rq, se, 0);
7715 dec_cpu_load(rq, se->load.weight);
7716 }
7142 7717
7143 se->load.weight = shares; 7718 se->load.weight = shares;
7144 se->load.inv_weight = div64_64((1ULL<<32), shares); 7719 se->load.inv_weight = div64_64((1ULL<<32), shares);
7145 7720
7146 if (on_rq) 7721 if (on_rq) {
7147 enqueue_entity(cfs_rq, se, 0); 7722 enqueue_entity(cfs_rq, se, 0);
7148 7723 inc_cpu_load(rq, se->load.weight);
7149 spin_unlock_irq(&rq->lock); 7724 }
7150} 7725}
7151 7726
7152int sched_group_set_shares(struct task_group *tg, unsigned long shares) 7727int sched_group_set_shares(struct task_group *tg, unsigned long shares)
7153{ 7728{
7154 int i; 7729 int i;
7730 struct cfs_rq *cfs_rq;
7731 struct rq *rq;
7732
7733 lock_task_group_list();
7734 if (tg->shares == shares)
7735 goto done;
7736
7737 if (shares < MIN_GROUP_SHARES)
7738 shares = MIN_GROUP_SHARES;
7155 7739
7156 /* 7740 /*
7157 * A weight of 0 or 1 can cause arithmetics problems. 7741 * Prevent any load balance activity (rebalance_shares,
7158 * (The default weight is 1024 - so there's no practical 7742 * load_balance_fair) from referring to this group first,
7159 * limitation from this.) 7743 * by taking it off the rq->leaf_cfs_rq_list on each cpu.
7160 */ 7744 */
7161 if (shares < 2) 7745 for_each_possible_cpu(i) {
7162 shares = 2; 7746 cfs_rq = tg->cfs_rq[i];
7747 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
7748 }
7163 7749
7164 spin_lock(&tg->lock); 7750 /* wait for any ongoing reference to this group to finish */
7165 if (tg->shares == shares) 7751 synchronize_sched();
7166 goto done;
7167 7752
7753 /*
7754 * Now we are free to modify the group's share on each cpu
7755 * w/o tripping rebalance_share or load_balance_fair.
7756 */
7168 tg->shares = shares; 7757 tg->shares = shares;
7169 for_each_possible_cpu(i) 7758 for_each_possible_cpu(i) {
7759 spin_lock_irq(&cpu_rq(i)->lock);
7170 set_se_shares(tg->se[i], shares); 7760 set_se_shares(tg->se[i], shares);
7761 spin_unlock_irq(&cpu_rq(i)->lock);
7762 }
7171 7763
7764 /*
7765 * Enable load balance activity on this group, by inserting it back on
7766 * each cpu's rq->leaf_cfs_rq_list.
7767 */
7768 for_each_possible_cpu(i) {
7769 rq = cpu_rq(i);
7770 cfs_rq = tg->cfs_rq[i];
7771 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7772 }
7172done: 7773done:
7173 spin_unlock(&tg->lock); 7774 unlock_task_group_list();
7174 return 0; 7775 return 0;
7175} 7776}
7176 7777
@@ -7179,6 +7780,31 @@ unsigned long sched_group_shares(struct task_group *tg)
7179 return tg->shares; 7780 return tg->shares;
7180} 7781}
7181 7782
7783/*
7784 * Ensure the total rt_ratio <= sysctl_sched_rt_ratio
7785 */
7786int sched_group_set_rt_ratio(struct task_group *tg, unsigned long rt_ratio)
7787{
7788 struct task_group *tgi;
7789 unsigned long total = 0;
7790
7791 rcu_read_lock();
7792 list_for_each_entry_rcu(tgi, &task_groups, list)
7793 total += tgi->rt_ratio;
7794 rcu_read_unlock();
7795
7796 if (total + rt_ratio - tg->rt_ratio > sysctl_sched_rt_ratio)
7797 return -EINVAL;
7798
7799 tg->rt_ratio = rt_ratio;
7800 return 0;
7801}
7802
7803unsigned long sched_group_rt_ratio(struct task_group *tg)
7804{
7805 return tg->rt_ratio;
7806}
7807
7182#endif /* CONFIG_FAIR_GROUP_SCHED */ 7808#endif /* CONFIG_FAIR_GROUP_SCHED */
7183 7809
7184#ifdef CONFIG_FAIR_CGROUP_SCHED 7810#ifdef CONFIG_FAIR_CGROUP_SCHED
@@ -7254,12 +7880,30 @@ static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
7254 return (u64) tg->shares; 7880 return (u64) tg->shares;
7255} 7881}
7256 7882
7883static int cpu_rt_ratio_write_uint(struct cgroup *cgrp, struct cftype *cftype,
7884 u64 rt_ratio_val)
7885{
7886 return sched_group_set_rt_ratio(cgroup_tg(cgrp), rt_ratio_val);
7887}
7888
7889static u64 cpu_rt_ratio_read_uint(struct cgroup *cgrp, struct cftype *cft)
7890{
7891 struct task_group *tg = cgroup_tg(cgrp);
7892
7893 return (u64) tg->rt_ratio;
7894}
7895
7257static struct cftype cpu_files[] = { 7896static struct cftype cpu_files[] = {
7258 { 7897 {
7259 .name = "shares", 7898 .name = "shares",
7260 .read_uint = cpu_shares_read_uint, 7899 .read_uint = cpu_shares_read_uint,
7261 .write_uint = cpu_shares_write_uint, 7900 .write_uint = cpu_shares_write_uint,
7262 }, 7901 },
7902 {
7903 .name = "rt_ratio",
7904 .read_uint = cpu_rt_ratio_read_uint,
7905 .write_uint = cpu_rt_ratio_write_uint,
7906 },
7263}; 7907};
7264 7908
7265static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont) 7909static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
diff --git a/kernel/sched_debug.c b/kernel/sched_debug.c
index 80fbbfc04290..4b5e24cf2f4a 100644
--- a/kernel/sched_debug.c
+++ b/kernel/sched_debug.c
@@ -179,6 +179,7 @@ static void print_cpu(struct seq_file *m, int cpu)
179 PN(prev_clock_raw); 179 PN(prev_clock_raw);
180 P(clock_warps); 180 P(clock_warps);
181 P(clock_overflows); 181 P(clock_overflows);
182 P(clock_underflows);
182 P(clock_deep_idle_events); 183 P(clock_deep_idle_events);
183 PN(clock_max_delta); 184 PN(clock_max_delta);
184 P(cpu_load[0]); 185 P(cpu_load[0]);
@@ -299,6 +300,8 @@ void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
299 PN(se.exec_max); 300 PN(se.exec_max);
300 PN(se.slice_max); 301 PN(se.slice_max);
301 PN(se.wait_max); 302 PN(se.wait_max);
303 PN(se.wait_sum);
304 P(se.wait_count);
302 P(sched_info.bkl_count); 305 P(sched_info.bkl_count);
303 P(se.nr_migrations); 306 P(se.nr_migrations);
304 P(se.nr_migrations_cold); 307 P(se.nr_migrations_cold);
@@ -366,6 +369,8 @@ void proc_sched_set_task(struct task_struct *p)
366{ 369{
367#ifdef CONFIG_SCHEDSTATS 370#ifdef CONFIG_SCHEDSTATS
368 p->se.wait_max = 0; 371 p->se.wait_max = 0;
372 p->se.wait_sum = 0;
373 p->se.wait_count = 0;
369 p->se.sleep_max = 0; 374 p->se.sleep_max = 0;
370 p->se.sum_sleep_runtime = 0; 375 p->se.sum_sleep_runtime = 0;
371 p->se.block_max = 0; 376 p->se.block_max = 0;
diff --git a/kernel/sched_fair.c b/kernel/sched_fair.c
index da7c061e7206..72e25c7a3a18 100644
--- a/kernel/sched_fair.c
+++ b/kernel/sched_fair.c
@@ -20,6 +20,8 @@
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> 20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
21 */ 21 */
22 22
23#include <linux/latencytop.h>
24
23/* 25/*
24 * Targeted preemption latency for CPU-bound tasks: 26 * Targeted preemption latency for CPU-bound tasks:
25 * (default: 20ms * (1 + ilog(ncpus)), units: nanoseconds) 27 * (default: 20ms * (1 + ilog(ncpus)), units: nanoseconds)
@@ -248,8 +250,8 @@ static u64 __sched_period(unsigned long nr_running)
248 unsigned long nr_latency = sched_nr_latency; 250 unsigned long nr_latency = sched_nr_latency;
249 251
250 if (unlikely(nr_running > nr_latency)) { 252 if (unlikely(nr_running > nr_latency)) {
253 period = sysctl_sched_min_granularity;
251 period *= nr_running; 254 period *= nr_running;
252 do_div(period, nr_latency);
253 } 255 }
254 256
255 return period; 257 return period;
@@ -383,6 +385,9 @@ update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
383{ 385{
384 schedstat_set(se->wait_max, max(se->wait_max, 386 schedstat_set(se->wait_max, max(se->wait_max,
385 rq_of(cfs_rq)->clock - se->wait_start)); 387 rq_of(cfs_rq)->clock - se->wait_start));
388 schedstat_set(se->wait_count, se->wait_count + 1);
389 schedstat_set(se->wait_sum, se->wait_sum +
390 rq_of(cfs_rq)->clock - se->wait_start);
386 schedstat_set(se->wait_start, 0); 391 schedstat_set(se->wait_start, 0);
387} 392}
388 393
@@ -434,6 +439,7 @@ static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
434#ifdef CONFIG_SCHEDSTATS 439#ifdef CONFIG_SCHEDSTATS
435 if (se->sleep_start) { 440 if (se->sleep_start) {
436 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start; 441 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
442 struct task_struct *tsk = task_of(se);
437 443
438 if ((s64)delta < 0) 444 if ((s64)delta < 0)
439 delta = 0; 445 delta = 0;
@@ -443,9 +449,12 @@ static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
443 449
444 se->sleep_start = 0; 450 se->sleep_start = 0;
445 se->sum_sleep_runtime += delta; 451 se->sum_sleep_runtime += delta;
452
453 account_scheduler_latency(tsk, delta >> 10, 1);
446 } 454 }
447 if (se->block_start) { 455 if (se->block_start) {
448 u64 delta = rq_of(cfs_rq)->clock - se->block_start; 456 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
457 struct task_struct *tsk = task_of(se);
449 458
450 if ((s64)delta < 0) 459 if ((s64)delta < 0)
451 delta = 0; 460 delta = 0;
@@ -462,11 +471,11 @@ static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
462 * time that the task spent sleeping: 471 * time that the task spent sleeping:
463 */ 472 */
464 if (unlikely(prof_on == SLEEP_PROFILING)) { 473 if (unlikely(prof_on == SLEEP_PROFILING)) {
465 struct task_struct *tsk = task_of(se);
466 474
467 profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk), 475 profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
468 delta >> 20); 476 delta >> 20);
469 } 477 }
478 account_scheduler_latency(tsk, delta >> 10, 0);
470 } 479 }
471#endif 480#endif
472} 481}
@@ -642,13 +651,29 @@ static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
642 cfs_rq->curr = NULL; 651 cfs_rq->curr = NULL;
643} 652}
644 653
645static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) 654static void
655entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
646{ 656{
647 /* 657 /*
648 * Update run-time statistics of the 'current'. 658 * Update run-time statistics of the 'current'.
649 */ 659 */
650 update_curr(cfs_rq); 660 update_curr(cfs_rq);
651 661
662#ifdef CONFIG_SCHED_HRTICK
663 /*
664 * queued ticks are scheduled to match the slice, so don't bother
665 * validating it and just reschedule.
666 */
667 if (queued)
668 return resched_task(rq_of(cfs_rq)->curr);
669 /*
670 * don't let the period tick interfere with the hrtick preemption
671 */
672 if (!sched_feat(DOUBLE_TICK) &&
673 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
674 return;
675#endif
676
652 if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT)) 677 if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
653 check_preempt_tick(cfs_rq, curr); 678 check_preempt_tick(cfs_rq, curr);
654} 679}
@@ -690,7 +715,7 @@ static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
690 715
691/* Iterate thr' all leaf cfs_rq's on a runqueue */ 716/* Iterate thr' all leaf cfs_rq's on a runqueue */
692#define for_each_leaf_cfs_rq(rq, cfs_rq) \ 717#define for_each_leaf_cfs_rq(rq, cfs_rq) \
693 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list) 718 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
694 719
695/* Do the two (enqueued) entities belong to the same group ? */ 720/* Do the two (enqueued) entities belong to the same group ? */
696static inline int 721static inline int
@@ -707,6 +732,8 @@ static inline struct sched_entity *parent_entity(struct sched_entity *se)
707 return se->parent; 732 return se->parent;
708} 733}
709 734
735#define GROUP_IMBALANCE_PCT 20
736
710#else /* CONFIG_FAIR_GROUP_SCHED */ 737#else /* CONFIG_FAIR_GROUP_SCHED */
711 738
712#define for_each_sched_entity(se) \ 739#define for_each_sched_entity(se) \
@@ -752,6 +779,43 @@ static inline struct sched_entity *parent_entity(struct sched_entity *se)
752 779
753#endif /* CONFIG_FAIR_GROUP_SCHED */ 780#endif /* CONFIG_FAIR_GROUP_SCHED */
754 781
782#ifdef CONFIG_SCHED_HRTICK
783static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
784{
785 int requeue = rq->curr == p;
786 struct sched_entity *se = &p->se;
787 struct cfs_rq *cfs_rq = cfs_rq_of(se);
788
789 WARN_ON(task_rq(p) != rq);
790
791 if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
792 u64 slice = sched_slice(cfs_rq, se);
793 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
794 s64 delta = slice - ran;
795
796 if (delta < 0) {
797 if (rq->curr == p)
798 resched_task(p);
799 return;
800 }
801
802 /*
803 * Don't schedule slices shorter than 10000ns, that just
804 * doesn't make sense. Rely on vruntime for fairness.
805 */
806 if (!requeue)
807 delta = max(10000LL, delta);
808
809 hrtick_start(rq, delta, requeue);
810 }
811}
812#else
813static inline void
814hrtick_start_fair(struct rq *rq, struct task_struct *p)
815{
816}
817#endif
818
755/* 819/*
756 * The enqueue_task method is called before nr_running is 820 * The enqueue_task method is called before nr_running is
757 * increased. Here we update the fair scheduling stats and 821 * increased. Here we update the fair scheduling stats and
@@ -760,15 +824,28 @@ static inline struct sched_entity *parent_entity(struct sched_entity *se)
760static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup) 824static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
761{ 825{
762 struct cfs_rq *cfs_rq; 826 struct cfs_rq *cfs_rq;
763 struct sched_entity *se = &p->se; 827 struct sched_entity *se = &p->se,
828 *topse = NULL; /* Highest schedulable entity */
829 int incload = 1;
764 830
765 for_each_sched_entity(se) { 831 for_each_sched_entity(se) {
766 if (se->on_rq) 832 topse = se;
833 if (se->on_rq) {
834 incload = 0;
767 break; 835 break;
836 }
768 cfs_rq = cfs_rq_of(se); 837 cfs_rq = cfs_rq_of(se);
769 enqueue_entity(cfs_rq, se, wakeup); 838 enqueue_entity(cfs_rq, se, wakeup);
770 wakeup = 1; 839 wakeup = 1;
771 } 840 }
841 /* Increment cpu load if we just enqueued the first task of a group on
842 * 'rq->cpu'. 'topse' represents the group to which task 'p' belongs
843 * at the highest grouping level.
844 */
845 if (incload)
846 inc_cpu_load(rq, topse->load.weight);
847
848 hrtick_start_fair(rq, rq->curr);
772} 849}
773 850
774/* 851/*
@@ -779,16 +856,30 @@ static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
779static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep) 856static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
780{ 857{
781 struct cfs_rq *cfs_rq; 858 struct cfs_rq *cfs_rq;
782 struct sched_entity *se = &p->se; 859 struct sched_entity *se = &p->se,
860 *topse = NULL; /* Highest schedulable entity */
861 int decload = 1;
783 862
784 for_each_sched_entity(se) { 863 for_each_sched_entity(se) {
864 topse = se;
785 cfs_rq = cfs_rq_of(se); 865 cfs_rq = cfs_rq_of(se);
786 dequeue_entity(cfs_rq, se, sleep); 866 dequeue_entity(cfs_rq, se, sleep);
787 /* Don't dequeue parent if it has other entities besides us */ 867 /* Don't dequeue parent if it has other entities besides us */
788 if (cfs_rq->load.weight) 868 if (cfs_rq->load.weight) {
869 if (parent_entity(se))
870 decload = 0;
789 break; 871 break;
872 }
790 sleep = 1; 873 sleep = 1;
791 } 874 }
875 /* Decrement cpu load if we just dequeued the last task of a group on
876 * 'rq->cpu'. 'topse' represents the group to which task 'p' belongs
877 * at the highest grouping level.
878 */
879 if (decload)
880 dec_cpu_load(rq, topse->load.weight);
881
882 hrtick_start_fair(rq, rq->curr);
792} 883}
793 884
794/* 885/*
@@ -836,6 +927,154 @@ static void yield_task_fair(struct rq *rq)
836} 927}
837 928
838/* 929/*
930 * wake_idle() will wake a task on an idle cpu if task->cpu is
931 * not idle and an idle cpu is available. The span of cpus to
932 * search starts with cpus closest then further out as needed,
933 * so we always favor a closer, idle cpu.
934 *
935 * Returns the CPU we should wake onto.
936 */
937#if defined(ARCH_HAS_SCHED_WAKE_IDLE)
938static int wake_idle(int cpu, struct task_struct *p)
939{
940 cpumask_t tmp;
941 struct sched_domain *sd;
942 int i;
943
944 /*
945 * If it is idle, then it is the best cpu to run this task.
946 *
947 * This cpu is also the best, if it has more than one task already.
948 * Siblings must be also busy(in most cases) as they didn't already
949 * pickup the extra load from this cpu and hence we need not check
950 * sibling runqueue info. This will avoid the checks and cache miss
951 * penalities associated with that.
952 */
953 if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1)
954 return cpu;
955
956 for_each_domain(cpu, sd) {
957 if (sd->flags & SD_WAKE_IDLE) {
958 cpus_and(tmp, sd->span, p->cpus_allowed);
959 for_each_cpu_mask(i, tmp) {
960 if (idle_cpu(i)) {
961 if (i != task_cpu(p)) {
962 schedstat_inc(p,
963 se.nr_wakeups_idle);
964 }
965 return i;
966 }
967 }
968 } else {
969 break;
970 }
971 }
972 return cpu;
973}
974#else
975static inline int wake_idle(int cpu, struct task_struct *p)
976{
977 return cpu;
978}
979#endif
980
981#ifdef CONFIG_SMP
982static int select_task_rq_fair(struct task_struct *p, int sync)
983{
984 int cpu, this_cpu;
985 struct rq *rq;
986 struct sched_domain *sd, *this_sd = NULL;
987 int new_cpu;
988
989 cpu = task_cpu(p);
990 rq = task_rq(p);
991 this_cpu = smp_processor_id();
992 new_cpu = cpu;
993
994 if (cpu == this_cpu)
995 goto out_set_cpu;
996
997 for_each_domain(this_cpu, sd) {
998 if (cpu_isset(cpu, sd->span)) {
999 this_sd = sd;
1000 break;
1001 }
1002 }
1003
1004 if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
1005 goto out_set_cpu;
1006
1007 /*
1008 * Check for affine wakeup and passive balancing possibilities.
1009 */
1010 if (this_sd) {
1011 int idx = this_sd->wake_idx;
1012 unsigned int imbalance;
1013 unsigned long load, this_load;
1014
1015 imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;
1016
1017 load = source_load(cpu, idx);
1018 this_load = target_load(this_cpu, idx);
1019
1020 new_cpu = this_cpu; /* Wake to this CPU if we can */
1021
1022 if (this_sd->flags & SD_WAKE_AFFINE) {
1023 unsigned long tl = this_load;
1024 unsigned long tl_per_task;
1025
1026 /*
1027 * Attract cache-cold tasks on sync wakeups:
1028 */
1029 if (sync && !task_hot(p, rq->clock, this_sd))
1030 goto out_set_cpu;
1031
1032 schedstat_inc(p, se.nr_wakeups_affine_attempts);
1033 tl_per_task = cpu_avg_load_per_task(this_cpu);
1034
1035 /*
1036 * If sync wakeup then subtract the (maximum possible)
1037 * effect of the currently running task from the load
1038 * of the current CPU:
1039 */
1040 if (sync)
1041 tl -= current->se.load.weight;
1042
1043 if ((tl <= load &&
1044 tl + target_load(cpu, idx) <= tl_per_task) ||
1045 100*(tl + p->se.load.weight) <= imbalance*load) {
1046 /*
1047 * This domain has SD_WAKE_AFFINE and
1048 * p is cache cold in this domain, and
1049 * there is no bad imbalance.
1050 */
1051 schedstat_inc(this_sd, ttwu_move_affine);
1052 schedstat_inc(p, se.nr_wakeups_affine);
1053 goto out_set_cpu;
1054 }
1055 }
1056
1057 /*
1058 * Start passive balancing when half the imbalance_pct
1059 * limit is reached.
1060 */
1061 if (this_sd->flags & SD_WAKE_BALANCE) {
1062 if (imbalance*this_load <= 100*load) {
1063 schedstat_inc(this_sd, ttwu_move_balance);
1064 schedstat_inc(p, se.nr_wakeups_passive);
1065 goto out_set_cpu;
1066 }
1067 }
1068 }
1069
1070 new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */
1071out_set_cpu:
1072 return wake_idle(new_cpu, p);
1073}
1074#endif /* CONFIG_SMP */
1075
1076
1077/*
839 * Preempt the current task with a newly woken task if needed: 1078 * Preempt the current task with a newly woken task if needed:
840 */ 1079 */
841static void check_preempt_wakeup(struct rq *rq, struct task_struct *p) 1080static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
@@ -876,6 +1115,7 @@ static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
876 1115
877static struct task_struct *pick_next_task_fair(struct rq *rq) 1116static struct task_struct *pick_next_task_fair(struct rq *rq)
878{ 1117{
1118 struct task_struct *p;
879 struct cfs_rq *cfs_rq = &rq->cfs; 1119 struct cfs_rq *cfs_rq = &rq->cfs;
880 struct sched_entity *se; 1120 struct sched_entity *se;
881 1121
@@ -887,7 +1127,10 @@ static struct task_struct *pick_next_task_fair(struct rq *rq)
887 cfs_rq = group_cfs_rq(se); 1127 cfs_rq = group_cfs_rq(se);
888 } while (cfs_rq); 1128 } while (cfs_rq);
889 1129
890 return task_of(se); 1130 p = task_of(se);
1131 hrtick_start_fair(rq, p);
1132
1133 return p;
891} 1134}
892 1135
893/* 1136/*
@@ -944,25 +1187,6 @@ static struct task_struct *load_balance_next_fair(void *arg)
944 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr); 1187 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
945} 1188}
946 1189
947#ifdef CONFIG_FAIR_GROUP_SCHED
948static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
949{
950 struct sched_entity *curr;
951 struct task_struct *p;
952
953 if (!cfs_rq->nr_running)
954 return MAX_PRIO;
955
956 curr = cfs_rq->curr;
957 if (!curr)
958 curr = __pick_next_entity(cfs_rq);
959
960 p = task_of(curr);
961
962 return p->prio;
963}
964#endif
965
966static unsigned long 1190static unsigned long
967load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest, 1191load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
968 unsigned long max_load_move, 1192 unsigned long max_load_move,
@@ -972,28 +1196,45 @@ load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
972 struct cfs_rq *busy_cfs_rq; 1196 struct cfs_rq *busy_cfs_rq;
973 long rem_load_move = max_load_move; 1197 long rem_load_move = max_load_move;
974 struct rq_iterator cfs_rq_iterator; 1198 struct rq_iterator cfs_rq_iterator;
1199 unsigned long load_moved;
975 1200
976 cfs_rq_iterator.start = load_balance_start_fair; 1201 cfs_rq_iterator.start = load_balance_start_fair;
977 cfs_rq_iterator.next = load_balance_next_fair; 1202 cfs_rq_iterator.next = load_balance_next_fair;
978 1203
979 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) { 1204 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
980#ifdef CONFIG_FAIR_GROUP_SCHED 1205#ifdef CONFIG_FAIR_GROUP_SCHED
981 struct cfs_rq *this_cfs_rq; 1206 struct cfs_rq *this_cfs_rq = busy_cfs_rq->tg->cfs_rq[this_cpu];
982 long imbalance; 1207 unsigned long maxload, task_load, group_weight;
983 unsigned long maxload; 1208 unsigned long thisload, per_task_load;
1209 struct sched_entity *se = busy_cfs_rq->tg->se[busiest->cpu];
1210
1211 task_load = busy_cfs_rq->load.weight;
1212 group_weight = se->load.weight;
984 1213
985 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu); 1214 /*
1215 * 'group_weight' is contributed by tasks of total weight
1216 * 'task_load'. To move 'rem_load_move' worth of weight only,
1217 * we need to move a maximum task load of:
1218 *
1219 * maxload = (remload / group_weight) * task_load;
1220 */
1221 maxload = (rem_load_move * task_load) / group_weight;
986 1222
987 imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight; 1223 if (!maxload || !task_load)
988 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
989 if (imbalance <= 0)
990 continue; 1224 continue;
991 1225
992 /* Don't pull more than imbalance/2 */ 1226 per_task_load = task_load / busy_cfs_rq->nr_running;
993 imbalance /= 2; 1227 /*
994 maxload = min(rem_load_move, imbalance); 1228 * balance_tasks will try to forcibly move atleast one task if
1229 * possible (because of SCHED_LOAD_SCALE_FUZZ). Avoid that if
1230 * maxload is less than GROUP_IMBALANCE_FUZZ% the per_task_load.
1231 */
1232 if (100 * maxload < GROUP_IMBALANCE_PCT * per_task_load)
1233 continue;
995 1234
996 *this_best_prio = cfs_rq_best_prio(this_cfs_rq); 1235 /* Disable priority-based load balance */
1236 *this_best_prio = 0;
1237 thisload = this_cfs_rq->load.weight;
997#else 1238#else
998# define maxload rem_load_move 1239# define maxload rem_load_move
999#endif 1240#endif
@@ -1002,11 +1243,33 @@ load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
1002 * load_balance_[start|next]_fair iterators 1243 * load_balance_[start|next]_fair iterators
1003 */ 1244 */
1004 cfs_rq_iterator.arg = busy_cfs_rq; 1245 cfs_rq_iterator.arg = busy_cfs_rq;
1005 rem_load_move -= balance_tasks(this_rq, this_cpu, busiest, 1246 load_moved = balance_tasks(this_rq, this_cpu, busiest,
1006 maxload, sd, idle, all_pinned, 1247 maxload, sd, idle, all_pinned,
1007 this_best_prio, 1248 this_best_prio,
1008 &cfs_rq_iterator); 1249 &cfs_rq_iterator);
1009 1250
1251#ifdef CONFIG_FAIR_GROUP_SCHED
1252 /*
1253 * load_moved holds the task load that was moved. The
1254 * effective (group) weight moved would be:
1255 * load_moved_eff = load_moved/task_load * group_weight;
1256 */
1257 load_moved = (group_weight * load_moved) / task_load;
1258
1259 /* Adjust shares on both cpus to reflect load_moved */
1260 group_weight -= load_moved;
1261 set_se_shares(se, group_weight);
1262
1263 se = busy_cfs_rq->tg->se[this_cpu];
1264 if (!thisload)
1265 group_weight = load_moved;
1266 else
1267 group_weight = se->load.weight + load_moved;
1268 set_se_shares(se, group_weight);
1269#endif
1270
1271 rem_load_move -= load_moved;
1272
1010 if (rem_load_move <= 0) 1273 if (rem_load_move <= 0)
1011 break; 1274 break;
1012 } 1275 }
@@ -1042,14 +1305,14 @@ move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
1042/* 1305/*
1043 * scheduler tick hitting a task of our scheduling class: 1306 * scheduler tick hitting a task of our scheduling class:
1044 */ 1307 */
1045static void task_tick_fair(struct rq *rq, struct task_struct *curr) 1308static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
1046{ 1309{
1047 struct cfs_rq *cfs_rq; 1310 struct cfs_rq *cfs_rq;
1048 struct sched_entity *se = &curr->se; 1311 struct sched_entity *se = &curr->se;
1049 1312
1050 for_each_sched_entity(se) { 1313 for_each_sched_entity(se) {
1051 cfs_rq = cfs_rq_of(se); 1314 cfs_rq = cfs_rq_of(se);
1052 entity_tick(cfs_rq, se); 1315 entity_tick(cfs_rq, se, queued);
1053 } 1316 }
1054} 1317}
1055 1318
@@ -1087,6 +1350,42 @@ static void task_new_fair(struct rq *rq, struct task_struct *p)
1087 resched_task(rq->curr); 1350 resched_task(rq->curr);
1088} 1351}
1089 1352
1353/*
1354 * Priority of the task has changed. Check to see if we preempt
1355 * the current task.
1356 */
1357static void prio_changed_fair(struct rq *rq, struct task_struct *p,
1358 int oldprio, int running)
1359{
1360 /*
1361 * Reschedule if we are currently running on this runqueue and
1362 * our priority decreased, or if we are not currently running on
1363 * this runqueue and our priority is higher than the current's
1364 */
1365 if (running) {
1366 if (p->prio > oldprio)
1367 resched_task(rq->curr);
1368 } else
1369 check_preempt_curr(rq, p);
1370}
1371
1372/*
1373 * We switched to the sched_fair class.
1374 */
1375static void switched_to_fair(struct rq *rq, struct task_struct *p,
1376 int running)
1377{
1378 /*
1379 * We were most likely switched from sched_rt, so
1380 * kick off the schedule if running, otherwise just see
1381 * if we can still preempt the current task.
1382 */
1383 if (running)
1384 resched_task(rq->curr);
1385 else
1386 check_preempt_curr(rq, p);
1387}
1388
1090/* Account for a task changing its policy or group. 1389/* Account for a task changing its policy or group.
1091 * 1390 *
1092 * This routine is mostly called to set cfs_rq->curr field when a task 1391 * This routine is mostly called to set cfs_rq->curr field when a task
@@ -1108,6 +1407,9 @@ static const struct sched_class fair_sched_class = {
1108 .enqueue_task = enqueue_task_fair, 1407 .enqueue_task = enqueue_task_fair,
1109 .dequeue_task = dequeue_task_fair, 1408 .dequeue_task = dequeue_task_fair,
1110 .yield_task = yield_task_fair, 1409 .yield_task = yield_task_fair,
1410#ifdef CONFIG_SMP
1411 .select_task_rq = select_task_rq_fair,
1412#endif /* CONFIG_SMP */
1111 1413
1112 .check_preempt_curr = check_preempt_wakeup, 1414 .check_preempt_curr = check_preempt_wakeup,
1113 1415
@@ -1122,6 +1424,9 @@ static const struct sched_class fair_sched_class = {
1122 .set_curr_task = set_curr_task_fair, 1424 .set_curr_task = set_curr_task_fair,
1123 .task_tick = task_tick_fair, 1425 .task_tick = task_tick_fair,
1124 .task_new = task_new_fair, 1426 .task_new = task_new_fair,
1427
1428 .prio_changed = prio_changed_fair,
1429 .switched_to = switched_to_fair,
1125}; 1430};
1126 1431
1127#ifdef CONFIG_SCHED_DEBUG 1432#ifdef CONFIG_SCHED_DEBUG
@@ -1132,7 +1437,9 @@ static void print_cfs_stats(struct seq_file *m, int cpu)
1132#ifdef CONFIG_FAIR_GROUP_SCHED 1437#ifdef CONFIG_FAIR_GROUP_SCHED
1133 print_cfs_rq(m, cpu, &cpu_rq(cpu)->cfs); 1438 print_cfs_rq(m, cpu, &cpu_rq(cpu)->cfs);
1134#endif 1439#endif
1440 rcu_read_lock();
1135 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq) 1441 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
1136 print_cfs_rq(m, cpu, cfs_rq); 1442 print_cfs_rq(m, cpu, cfs_rq);
1443 rcu_read_unlock();
1137} 1444}
1138#endif 1445#endif
diff --git a/kernel/sched_idletask.c b/kernel/sched_idletask.c
index bf9c25c15b8b..2bcafa375633 100644
--- a/kernel/sched_idletask.c
+++ b/kernel/sched_idletask.c
@@ -5,6 +5,12 @@
5 * handled in sched_fair.c) 5 * handled in sched_fair.c)
6 */ 6 */
7 7
8#ifdef CONFIG_SMP
9static int select_task_rq_idle(struct task_struct *p, int sync)
10{
11 return task_cpu(p); /* IDLE tasks as never migrated */
12}
13#endif /* CONFIG_SMP */
8/* 14/*
9 * Idle tasks are unconditionally rescheduled: 15 * Idle tasks are unconditionally rescheduled:
10 */ 16 */
@@ -55,7 +61,7 @@ move_one_task_idle(struct rq *this_rq, int this_cpu, struct rq *busiest,
55} 61}
56#endif 62#endif
57 63
58static void task_tick_idle(struct rq *rq, struct task_struct *curr) 64static void task_tick_idle(struct rq *rq, struct task_struct *curr, int queued)
59{ 65{
60} 66}
61 67
@@ -63,6 +69,33 @@ static void set_curr_task_idle(struct rq *rq)
63{ 69{
64} 70}
65 71
72static void switched_to_idle(struct rq *rq, struct task_struct *p,
73 int running)
74{
75 /* Can this actually happen?? */
76 if (running)
77 resched_task(rq->curr);
78 else
79 check_preempt_curr(rq, p);
80}
81
82static void prio_changed_idle(struct rq *rq, struct task_struct *p,
83 int oldprio, int running)
84{
85 /* This can happen for hot plug CPUS */
86
87 /*
88 * Reschedule if we are currently running on this runqueue and
89 * our priority decreased, or if we are not currently running on
90 * this runqueue and our priority is higher than the current's
91 */
92 if (running) {
93 if (p->prio > oldprio)
94 resched_task(rq->curr);
95 } else
96 check_preempt_curr(rq, p);
97}
98
66/* 99/*
67 * Simple, special scheduling class for the per-CPU idle tasks: 100 * Simple, special scheduling class for the per-CPU idle tasks:
68 */ 101 */
@@ -72,6 +105,9 @@ const struct sched_class idle_sched_class = {
72 105
73 /* dequeue is not valid, we print a debug message there: */ 106 /* dequeue is not valid, we print a debug message there: */
74 .dequeue_task = dequeue_task_idle, 107 .dequeue_task = dequeue_task_idle,
108#ifdef CONFIG_SMP
109 .select_task_rq = select_task_rq_idle,
110#endif /* CONFIG_SMP */
75 111
76 .check_preempt_curr = check_preempt_curr_idle, 112 .check_preempt_curr = check_preempt_curr_idle,
77 113
@@ -85,5 +121,9 @@ const struct sched_class idle_sched_class = {
85 121
86 .set_curr_task = set_curr_task_idle, 122 .set_curr_task = set_curr_task_idle,
87 .task_tick = task_tick_idle, 123 .task_tick = task_tick_idle,
124
125 .prio_changed = prio_changed_idle,
126 .switched_to = switched_to_idle,
127
88 /* no .task_new for idle tasks */ 128 /* no .task_new for idle tasks */
89}; 129};
diff --git a/kernel/sched_rt.c b/kernel/sched_rt.c
index 9ba3daa03475..274b40d7bef2 100644
--- a/kernel/sched_rt.c
+++ b/kernel/sched_rt.c
@@ -3,6 +3,217 @@
3 * policies) 3 * policies)
4 */ 4 */
5 5
6#ifdef CONFIG_SMP
7
8static inline int rt_overloaded(struct rq *rq)
9{
10 return atomic_read(&rq->rd->rto_count);
11}
12
13static inline void rt_set_overload(struct rq *rq)
14{
15 cpu_set(rq->cpu, rq->rd->rto_mask);
16 /*
17 * Make sure the mask is visible before we set
18 * the overload count. That is checked to determine
19 * if we should look at the mask. It would be a shame
20 * if we looked at the mask, but the mask was not
21 * updated yet.
22 */
23 wmb();
24 atomic_inc(&rq->rd->rto_count);
25}
26
27static inline void rt_clear_overload(struct rq *rq)
28{
29 /* the order here really doesn't matter */
30 atomic_dec(&rq->rd->rto_count);
31 cpu_clear(rq->cpu, rq->rd->rto_mask);
32}
33
34static void update_rt_migration(struct rq *rq)
35{
36 if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) {
37 if (!rq->rt.overloaded) {
38 rt_set_overload(rq);
39 rq->rt.overloaded = 1;
40 }
41 } else if (rq->rt.overloaded) {
42 rt_clear_overload(rq);
43 rq->rt.overloaded = 0;
44 }
45}
46#endif /* CONFIG_SMP */
47
48static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
49{
50 return container_of(rt_se, struct task_struct, rt);
51}
52
53static inline int on_rt_rq(struct sched_rt_entity *rt_se)
54{
55 return !list_empty(&rt_se->run_list);
56}
57
58#ifdef CONFIG_FAIR_GROUP_SCHED
59
60static inline unsigned int sched_rt_ratio(struct rt_rq *rt_rq)
61{
62 if (!rt_rq->tg)
63 return SCHED_RT_FRAC;
64
65 return rt_rq->tg->rt_ratio;
66}
67
68#define for_each_leaf_rt_rq(rt_rq, rq) \
69 list_for_each_entry(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
70
71static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
72{
73 return rt_rq->rq;
74}
75
76static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
77{
78 return rt_se->rt_rq;
79}
80
81#define for_each_sched_rt_entity(rt_se) \
82 for (; rt_se; rt_se = rt_se->parent)
83
84static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
85{
86 return rt_se->my_q;
87}
88
89static void enqueue_rt_entity(struct sched_rt_entity *rt_se);
90static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
91
92static void sched_rt_ratio_enqueue(struct rt_rq *rt_rq)
93{
94 struct sched_rt_entity *rt_se = rt_rq->rt_se;
95
96 if (rt_se && !on_rt_rq(rt_se) && rt_rq->rt_nr_running) {
97 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
98
99 enqueue_rt_entity(rt_se);
100 if (rt_rq->highest_prio < curr->prio)
101 resched_task(curr);
102 }
103}
104
105static void sched_rt_ratio_dequeue(struct rt_rq *rt_rq)
106{
107 struct sched_rt_entity *rt_se = rt_rq->rt_se;
108
109 if (rt_se && on_rt_rq(rt_se))
110 dequeue_rt_entity(rt_se);
111}
112
113#else
114
115static inline unsigned int sched_rt_ratio(struct rt_rq *rt_rq)
116{
117 return sysctl_sched_rt_ratio;
118}
119
120#define for_each_leaf_rt_rq(rt_rq, rq) \
121 for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
122
123static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
124{
125 return container_of(rt_rq, struct rq, rt);
126}
127
128static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
129{
130 struct task_struct *p = rt_task_of(rt_se);
131 struct rq *rq = task_rq(p);
132
133 return &rq->rt;
134}
135
136#define for_each_sched_rt_entity(rt_se) \
137 for (; rt_se; rt_se = NULL)
138
139static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
140{
141 return NULL;
142}
143
144static inline void sched_rt_ratio_enqueue(struct rt_rq *rt_rq)
145{
146}
147
148static inline void sched_rt_ratio_dequeue(struct rt_rq *rt_rq)
149{
150}
151
152#endif
153
154static inline int rt_se_prio(struct sched_rt_entity *rt_se)
155{
156#ifdef CONFIG_FAIR_GROUP_SCHED
157 struct rt_rq *rt_rq = group_rt_rq(rt_se);
158
159 if (rt_rq)
160 return rt_rq->highest_prio;
161#endif
162
163 return rt_task_of(rt_se)->prio;
164}
165
166static int sched_rt_ratio_exceeded(struct rt_rq *rt_rq)
167{
168 unsigned int rt_ratio = sched_rt_ratio(rt_rq);
169 u64 period, ratio;
170
171 if (rt_ratio == SCHED_RT_FRAC)
172 return 0;
173
174 if (rt_rq->rt_throttled)
175 return 1;
176
177 period = (u64)sysctl_sched_rt_period * NSEC_PER_MSEC;
178 ratio = (period * rt_ratio) >> SCHED_RT_FRAC_SHIFT;
179
180 if (rt_rq->rt_time > ratio) {
181 struct rq *rq = rq_of_rt_rq(rt_rq);
182
183 rq->rt_throttled = 1;
184 rt_rq->rt_throttled = 1;
185
186 sched_rt_ratio_dequeue(rt_rq);
187 return 1;
188 }
189
190 return 0;
191}
192
193static void update_sched_rt_period(struct rq *rq)
194{
195 struct rt_rq *rt_rq;
196 u64 period;
197
198 while (rq->clock > rq->rt_period_expire) {
199 period = (u64)sysctl_sched_rt_period * NSEC_PER_MSEC;
200 rq->rt_period_expire += period;
201
202 for_each_leaf_rt_rq(rt_rq, rq) {
203 unsigned long rt_ratio = sched_rt_ratio(rt_rq);
204 u64 ratio = (period * rt_ratio) >> SCHED_RT_FRAC_SHIFT;
205
206 rt_rq->rt_time -= min(rt_rq->rt_time, ratio);
207 if (rt_rq->rt_throttled) {
208 rt_rq->rt_throttled = 0;
209 sched_rt_ratio_enqueue(rt_rq);
210 }
211 }
212
213 rq->rt_throttled = 0;
214 }
215}
216
6/* 217/*
7 * Update the current task's runtime statistics. Skip current tasks that 218 * Update the current task's runtime statistics. Skip current tasks that
8 * are not in our scheduling class. 219 * are not in our scheduling class.
@@ -10,6 +221,8 @@
10static void update_curr_rt(struct rq *rq) 221static void update_curr_rt(struct rq *rq)
11{ 222{
12 struct task_struct *curr = rq->curr; 223 struct task_struct *curr = rq->curr;
224 struct sched_rt_entity *rt_se = &curr->rt;
225 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
13 u64 delta_exec; 226 u64 delta_exec;
14 227
15 if (!task_has_rt_policy(curr)) 228 if (!task_has_rt_policy(curr))
@@ -24,47 +237,228 @@ static void update_curr_rt(struct rq *rq)
24 curr->se.sum_exec_runtime += delta_exec; 237 curr->se.sum_exec_runtime += delta_exec;
25 curr->se.exec_start = rq->clock; 238 curr->se.exec_start = rq->clock;
26 cpuacct_charge(curr, delta_exec); 239 cpuacct_charge(curr, delta_exec);
240
241 rt_rq->rt_time += delta_exec;
242 /*
243 * might make it a tad more accurate:
244 *
245 * update_sched_rt_period(rq);
246 */
247 if (sched_rt_ratio_exceeded(rt_rq))
248 resched_task(curr);
27} 249}
28 250
29static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup) 251static inline
252void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
253{
254 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
255 rt_rq->rt_nr_running++;
256#if defined CONFIG_SMP || defined CONFIG_FAIR_GROUP_SCHED
257 if (rt_se_prio(rt_se) < rt_rq->highest_prio)
258 rt_rq->highest_prio = rt_se_prio(rt_se);
259#endif
260#ifdef CONFIG_SMP
261 if (rt_se->nr_cpus_allowed > 1) {
262 struct rq *rq = rq_of_rt_rq(rt_rq);
263 rq->rt.rt_nr_migratory++;
264 }
265
266 update_rt_migration(rq_of_rt_rq(rt_rq));
267#endif
268}
269
270static inline
271void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
272{
273 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
274 WARN_ON(!rt_rq->rt_nr_running);
275 rt_rq->rt_nr_running--;
276#if defined CONFIG_SMP || defined CONFIG_FAIR_GROUP_SCHED
277 if (rt_rq->rt_nr_running) {
278 struct rt_prio_array *array;
279
280 WARN_ON(rt_se_prio(rt_se) < rt_rq->highest_prio);
281 if (rt_se_prio(rt_se) == rt_rq->highest_prio) {
282 /* recalculate */
283 array = &rt_rq->active;
284 rt_rq->highest_prio =
285 sched_find_first_bit(array->bitmap);
286 } /* otherwise leave rq->highest prio alone */
287 } else
288 rt_rq->highest_prio = MAX_RT_PRIO;
289#endif
290#ifdef CONFIG_SMP
291 if (rt_se->nr_cpus_allowed > 1) {
292 struct rq *rq = rq_of_rt_rq(rt_rq);
293 rq->rt.rt_nr_migratory--;
294 }
295
296 update_rt_migration(rq_of_rt_rq(rt_rq));
297#endif /* CONFIG_SMP */
298}
299
300static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
301{
302 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
303 struct rt_prio_array *array = &rt_rq->active;
304 struct rt_rq *group_rq = group_rt_rq(rt_se);
305
306 if (group_rq && group_rq->rt_throttled)
307 return;
308
309 list_add_tail(&rt_se->run_list, array->queue + rt_se_prio(rt_se));
310 __set_bit(rt_se_prio(rt_se), array->bitmap);
311
312 inc_rt_tasks(rt_se, rt_rq);
313}
314
315static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
30{ 316{
31 struct rt_prio_array *array = &rq->rt.active; 317 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
318 struct rt_prio_array *array = &rt_rq->active;
319
320 list_del_init(&rt_se->run_list);
321 if (list_empty(array->queue + rt_se_prio(rt_se)))
322 __clear_bit(rt_se_prio(rt_se), array->bitmap);
32 323
33 list_add_tail(&p->run_list, array->queue + p->prio); 324 dec_rt_tasks(rt_se, rt_rq);
34 __set_bit(p->prio, array->bitmap); 325}
326
327/*
328 * Because the prio of an upper entry depends on the lower
329 * entries, we must remove entries top - down.
330 *
331 * XXX: O(1/2 h^2) because we can only walk up, not down the chain.
332 * doesn't matter much for now, as h=2 for GROUP_SCHED.
333 */
334static void dequeue_rt_stack(struct task_struct *p)
335{
336 struct sched_rt_entity *rt_se, *top_se;
337
338 /*
339 * dequeue all, top - down.
340 */
341 do {
342 rt_se = &p->rt;
343 top_se = NULL;
344 for_each_sched_rt_entity(rt_se) {
345 if (on_rt_rq(rt_se))
346 top_se = rt_se;
347 }
348 if (top_se)
349 dequeue_rt_entity(top_se);
350 } while (top_se);
35} 351}
36 352
37/* 353/*
38 * Adding/removing a task to/from a priority array: 354 * Adding/removing a task to/from a priority array:
39 */ 355 */
356static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
357{
358 struct sched_rt_entity *rt_se = &p->rt;
359
360 if (wakeup)
361 rt_se->timeout = 0;
362
363 dequeue_rt_stack(p);
364
365 /*
366 * enqueue everybody, bottom - up.
367 */
368 for_each_sched_rt_entity(rt_se)
369 enqueue_rt_entity(rt_se);
370
371 inc_cpu_load(rq, p->se.load.weight);
372}
373
40static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep) 374static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
41{ 375{
42 struct rt_prio_array *array = &rq->rt.active; 376 struct sched_rt_entity *rt_se = &p->rt;
377 struct rt_rq *rt_rq;
43 378
44 update_curr_rt(rq); 379 update_curr_rt(rq);
45 380
46 list_del(&p->run_list); 381 dequeue_rt_stack(p);
47 if (list_empty(array->queue + p->prio)) 382
48 __clear_bit(p->prio, array->bitmap); 383 /*
384 * re-enqueue all non-empty rt_rq entities.
385 */
386 for_each_sched_rt_entity(rt_se) {
387 rt_rq = group_rt_rq(rt_se);
388 if (rt_rq && rt_rq->rt_nr_running)
389 enqueue_rt_entity(rt_se);
390 }
391
392 dec_cpu_load(rq, p->se.load.weight);
49} 393}
50 394
51/* 395/*
52 * Put task to the end of the run list without the overhead of dequeue 396 * Put task to the end of the run list without the overhead of dequeue
53 * followed by enqueue. 397 * followed by enqueue.
54 */ 398 */
399static
400void requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
401{
402 struct rt_prio_array *array = &rt_rq->active;
403
404 list_move_tail(&rt_se->run_list, array->queue + rt_se_prio(rt_se));
405}
406
55static void requeue_task_rt(struct rq *rq, struct task_struct *p) 407static void requeue_task_rt(struct rq *rq, struct task_struct *p)
56{ 408{
57 struct rt_prio_array *array = &rq->rt.active; 409 struct sched_rt_entity *rt_se = &p->rt;
410 struct rt_rq *rt_rq;
58 411
59 list_move_tail(&p->run_list, array->queue + p->prio); 412 for_each_sched_rt_entity(rt_se) {
413 rt_rq = rt_rq_of_se(rt_se);
414 requeue_rt_entity(rt_rq, rt_se);
415 }
60} 416}
61 417
62static void 418static void yield_task_rt(struct rq *rq)
63yield_task_rt(struct rq *rq)
64{ 419{
65 requeue_task_rt(rq, rq->curr); 420 requeue_task_rt(rq, rq->curr);
66} 421}
67 422
423#ifdef CONFIG_SMP
424static int find_lowest_rq(struct task_struct *task);
425
426static int select_task_rq_rt(struct task_struct *p, int sync)
427{
428 struct rq *rq = task_rq(p);
429
430 /*
431 * If the current task is an RT task, then
432 * try to see if we can wake this RT task up on another
433 * runqueue. Otherwise simply start this RT task
434 * on its current runqueue.
435 *
436 * We want to avoid overloading runqueues. Even if
437 * the RT task is of higher priority than the current RT task.
438 * RT tasks behave differently than other tasks. If
439 * one gets preempted, we try to push it off to another queue.
440 * So trying to keep a preempting RT task on the same
441 * cache hot CPU will force the running RT task to
442 * a cold CPU. So we waste all the cache for the lower
443 * RT task in hopes of saving some of a RT task
444 * that is just being woken and probably will have
445 * cold cache anyway.
446 */
447 if (unlikely(rt_task(rq->curr)) &&
448 (p->rt.nr_cpus_allowed > 1)) {
449 int cpu = find_lowest_rq(p);
450
451 return (cpu == -1) ? task_cpu(p) : cpu;
452 }
453
454 /*
455 * Otherwise, just let it ride on the affined RQ and the
456 * post-schedule router will push the preempted task away
457 */
458 return task_cpu(p);
459}
460#endif /* CONFIG_SMP */
461
68/* 462/*
69 * Preempt the current task with a newly woken task if needed: 463 * Preempt the current task with a newly woken task if needed:
70 */ 464 */
@@ -74,25 +468,48 @@ static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
74 resched_task(rq->curr); 468 resched_task(rq->curr);
75} 469}
76 470
77static struct task_struct *pick_next_task_rt(struct rq *rq) 471static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
472 struct rt_rq *rt_rq)
78{ 473{
79 struct rt_prio_array *array = &rq->rt.active; 474 struct rt_prio_array *array = &rt_rq->active;
80 struct task_struct *next; 475 struct sched_rt_entity *next = NULL;
81 struct list_head *queue; 476 struct list_head *queue;
82 int idx; 477 int idx;
83 478
84 idx = sched_find_first_bit(array->bitmap); 479 idx = sched_find_first_bit(array->bitmap);
85 if (idx >= MAX_RT_PRIO) 480 BUG_ON(idx >= MAX_RT_PRIO);
86 return NULL;
87 481
88 queue = array->queue + idx; 482 queue = array->queue + idx;
89 next = list_entry(queue->next, struct task_struct, run_list); 483 next = list_entry(queue->next, struct sched_rt_entity, run_list);
90
91 next->se.exec_start = rq->clock;
92 484
93 return next; 485 return next;
94} 486}
95 487
488static struct task_struct *pick_next_task_rt(struct rq *rq)
489{
490 struct sched_rt_entity *rt_se;
491 struct task_struct *p;
492 struct rt_rq *rt_rq;
493
494 rt_rq = &rq->rt;
495
496 if (unlikely(!rt_rq->rt_nr_running))
497 return NULL;
498
499 if (sched_rt_ratio_exceeded(rt_rq))
500 return NULL;
501
502 do {
503 rt_se = pick_next_rt_entity(rq, rt_rq);
504 BUG_ON(!rt_se);
505 rt_rq = group_rt_rq(rt_se);
506 } while (rt_rq);
507
508 p = rt_task_of(rt_se);
509 p->se.exec_start = rq->clock;
510 return p;
511}
512
96static void put_prev_task_rt(struct rq *rq, struct task_struct *p) 513static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
97{ 514{
98 update_curr_rt(rq); 515 update_curr_rt(rq);
@@ -100,76 +517,448 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
100} 517}
101 518
102#ifdef CONFIG_SMP 519#ifdef CONFIG_SMP
103/* 520
104 * Load-balancing iterator. Note: while the runqueue stays locked 521/* Only try algorithms three times */
105 * during the whole iteration, the current task might be 522#define RT_MAX_TRIES 3
106 * dequeued so the iterator has to be dequeue-safe. Here we 523
107 * achieve that by always pre-iterating before returning 524static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
108 * the current task: 525static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
109 */ 526
110static struct task_struct *load_balance_start_rt(void *arg) 527static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
111{ 528{
112 struct rq *rq = arg; 529 if (!task_running(rq, p) &&
113 struct rt_prio_array *array = &rq->rt.active; 530 (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
114 struct list_head *head, *curr; 531 (p->rt.nr_cpus_allowed > 1))
115 struct task_struct *p; 532 return 1;
533 return 0;
534}
535
536/* Return the second highest RT task, NULL otherwise */
537static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
538{
539 struct task_struct *next = NULL;
540 struct sched_rt_entity *rt_se;
541 struct rt_prio_array *array;
542 struct rt_rq *rt_rq;
116 int idx; 543 int idx;
117 544
118 idx = sched_find_first_bit(array->bitmap); 545 for_each_leaf_rt_rq(rt_rq, rq) {
119 if (idx >= MAX_RT_PRIO) 546 array = &rt_rq->active;
120 return NULL; 547 idx = sched_find_first_bit(array->bitmap);
548 next_idx:
549 if (idx >= MAX_RT_PRIO)
550 continue;
551 if (next && next->prio < idx)
552 continue;
553 list_for_each_entry(rt_se, array->queue + idx, run_list) {
554 struct task_struct *p = rt_task_of(rt_se);
555 if (pick_rt_task(rq, p, cpu)) {
556 next = p;
557 break;
558 }
559 }
560 if (!next) {
561 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
562 goto next_idx;
563 }
564 }
121 565
122 head = array->queue + idx; 566 return next;
123 curr = head->prev; 567}
124 568
125 p = list_entry(curr, struct task_struct, run_list); 569static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
126 570
127 curr = curr->prev; 571static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
572{
573 int lowest_prio = -1;
574 int lowest_cpu = -1;
575 int count = 0;
576 int cpu;
128 577
129 rq->rt.rt_load_balance_idx = idx; 578 cpus_and(*lowest_mask, task_rq(task)->rd->online, task->cpus_allowed);
130 rq->rt.rt_load_balance_head = head;
131 rq->rt.rt_load_balance_curr = curr;
132 579
133 return p; 580 /*
581 * Scan each rq for the lowest prio.
582 */
583 for_each_cpu_mask(cpu, *lowest_mask) {
584 struct rq *rq = cpu_rq(cpu);
585
586 /* We look for lowest RT prio or non-rt CPU */
587 if (rq->rt.highest_prio >= MAX_RT_PRIO) {
588 /*
589 * if we already found a low RT queue
590 * and now we found this non-rt queue
591 * clear the mask and set our bit.
592 * Otherwise just return the queue as is
593 * and the count==1 will cause the algorithm
594 * to use the first bit found.
595 */
596 if (lowest_cpu != -1) {
597 cpus_clear(*lowest_mask);
598 cpu_set(rq->cpu, *lowest_mask);
599 }
600 return 1;
601 }
602
603 /* no locking for now */
604 if ((rq->rt.highest_prio > task->prio)
605 && (rq->rt.highest_prio >= lowest_prio)) {
606 if (rq->rt.highest_prio > lowest_prio) {
607 /* new low - clear old data */
608 lowest_prio = rq->rt.highest_prio;
609 lowest_cpu = cpu;
610 count = 0;
611 }
612 count++;
613 } else
614 cpu_clear(cpu, *lowest_mask);
615 }
616
617 /*
618 * Clear out all the set bits that represent
619 * runqueues that were of higher prio than
620 * the lowest_prio.
621 */
622 if (lowest_cpu > 0) {
623 /*
624 * Perhaps we could add another cpumask op to
625 * zero out bits. Like cpu_zero_bits(cpumask, nrbits);
626 * Then that could be optimized to use memset and such.
627 */
628 for_each_cpu_mask(cpu, *lowest_mask) {
629 if (cpu >= lowest_cpu)
630 break;
631 cpu_clear(cpu, *lowest_mask);
632 }
633 }
634
635 return count;
134} 636}
135 637
136static struct task_struct *load_balance_next_rt(void *arg) 638static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
137{ 639{
138 struct rq *rq = arg; 640 int first;
139 struct rt_prio_array *array = &rq->rt.active; 641
140 struct list_head *head, *curr; 642 /* "this_cpu" is cheaper to preempt than a remote processor */
141 struct task_struct *p; 643 if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
142 int idx; 644 return this_cpu;
645
646 first = first_cpu(*mask);
647 if (first != NR_CPUS)
648 return first;
649
650 return -1;
651}
652
653static int find_lowest_rq(struct task_struct *task)
654{
655 struct sched_domain *sd;
656 cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
657 int this_cpu = smp_processor_id();
658 int cpu = task_cpu(task);
659 int count = find_lowest_cpus(task, lowest_mask);
143 660
144 idx = rq->rt.rt_load_balance_idx; 661 if (!count)
145 head = rq->rt.rt_load_balance_head; 662 return -1; /* No targets found */
146 curr = rq->rt.rt_load_balance_curr;
147 663
148 /* 664 /*
149 * If we arrived back to the head again then 665 * There is no sense in performing an optimal search if only one
150 * iterate to the next queue (if any): 666 * target is found.
151 */ 667 */
152 if (unlikely(head == curr)) { 668 if (count == 1)
153 int next_idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1); 669 return first_cpu(*lowest_mask);
154 670
155 if (next_idx >= MAX_RT_PRIO) 671 /*
156 return NULL; 672 * At this point we have built a mask of cpus representing the
673 * lowest priority tasks in the system. Now we want to elect
674 * the best one based on our affinity and topology.
675 *
676 * We prioritize the last cpu that the task executed on since
677 * it is most likely cache-hot in that location.
678 */
679 if (cpu_isset(cpu, *lowest_mask))
680 return cpu;
681
682 /*
683 * Otherwise, we consult the sched_domains span maps to figure
684 * out which cpu is logically closest to our hot cache data.
685 */
686 if (this_cpu == cpu)
687 this_cpu = -1; /* Skip this_cpu opt if the same */
688
689 for_each_domain(cpu, sd) {
690 if (sd->flags & SD_WAKE_AFFINE) {
691 cpumask_t domain_mask;
692 int best_cpu;
157 693
158 idx = next_idx; 694 cpus_and(domain_mask, sd->span, *lowest_mask);
159 head = array->queue + idx;
160 curr = head->prev;
161 695
162 rq->rt.rt_load_balance_idx = idx; 696 best_cpu = pick_optimal_cpu(this_cpu,
163 rq->rt.rt_load_balance_head = head; 697 &domain_mask);
698 if (best_cpu != -1)
699 return best_cpu;
700 }
164 } 701 }
165 702
166 p = list_entry(curr, struct task_struct, run_list); 703 /*
704 * And finally, if there were no matches within the domains
705 * just give the caller *something* to work with from the compatible
706 * locations.
707 */
708 return pick_optimal_cpu(this_cpu, lowest_mask);
709}
167 710
168 curr = curr->prev; 711/* Will lock the rq it finds */
712static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
713{
714 struct rq *lowest_rq = NULL;
715 int tries;
716 int cpu;
169 717
170 rq->rt.rt_load_balance_curr = curr; 718 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
719 cpu = find_lowest_rq(task);
171 720
172 return p; 721 if ((cpu == -1) || (cpu == rq->cpu))
722 break;
723
724 lowest_rq = cpu_rq(cpu);
725
726 /* if the prio of this runqueue changed, try again */
727 if (double_lock_balance(rq, lowest_rq)) {
728 /*
729 * We had to unlock the run queue. In
730 * the mean time, task could have
731 * migrated already or had its affinity changed.
732 * Also make sure that it wasn't scheduled on its rq.
733 */
734 if (unlikely(task_rq(task) != rq ||
735 !cpu_isset(lowest_rq->cpu,
736 task->cpus_allowed) ||
737 task_running(rq, task) ||
738 !task->se.on_rq)) {
739
740 spin_unlock(&lowest_rq->lock);
741 lowest_rq = NULL;
742 break;
743 }
744 }
745
746 /* If this rq is still suitable use it. */
747 if (lowest_rq->rt.highest_prio > task->prio)
748 break;
749
750 /* try again */
751 spin_unlock(&lowest_rq->lock);
752 lowest_rq = NULL;
753 }
754
755 return lowest_rq;
756}
757
758/*
759 * If the current CPU has more than one RT task, see if the non
760 * running task can migrate over to a CPU that is running a task
761 * of lesser priority.
762 */
763static int push_rt_task(struct rq *rq)
764{
765 struct task_struct *next_task;
766 struct rq *lowest_rq;
767 int ret = 0;
768 int paranoid = RT_MAX_TRIES;
769
770 if (!rq->rt.overloaded)
771 return 0;
772
773 next_task = pick_next_highest_task_rt(rq, -1);
774 if (!next_task)
775 return 0;
776
777 retry:
778 if (unlikely(next_task == rq->curr)) {
779 WARN_ON(1);
780 return 0;
781 }
782
783 /*
784 * It's possible that the next_task slipped in of
785 * higher priority than current. If that's the case
786 * just reschedule current.
787 */
788 if (unlikely(next_task->prio < rq->curr->prio)) {
789 resched_task(rq->curr);
790 return 0;
791 }
792
793 /* We might release rq lock */
794 get_task_struct(next_task);
795
796 /* find_lock_lowest_rq locks the rq if found */
797 lowest_rq = find_lock_lowest_rq(next_task, rq);
798 if (!lowest_rq) {
799 struct task_struct *task;
800 /*
801 * find lock_lowest_rq releases rq->lock
802 * so it is possible that next_task has changed.
803 * If it has, then try again.
804 */
805 task = pick_next_highest_task_rt(rq, -1);
806 if (unlikely(task != next_task) && task && paranoid--) {
807 put_task_struct(next_task);
808 next_task = task;
809 goto retry;
810 }
811 goto out;
812 }
813
814 deactivate_task(rq, next_task, 0);
815 set_task_cpu(next_task, lowest_rq->cpu);
816 activate_task(lowest_rq, next_task, 0);
817
818 resched_task(lowest_rq->curr);
819
820 spin_unlock(&lowest_rq->lock);
821
822 ret = 1;
823out:
824 put_task_struct(next_task);
825
826 return ret;
827}
828
829/*
830 * TODO: Currently we just use the second highest prio task on
831 * the queue, and stop when it can't migrate (or there's
832 * no more RT tasks). There may be a case where a lower
833 * priority RT task has a different affinity than the
834 * higher RT task. In this case the lower RT task could
835 * possibly be able to migrate where as the higher priority
836 * RT task could not. We currently ignore this issue.
837 * Enhancements are welcome!
838 */
839static void push_rt_tasks(struct rq *rq)
840{
841 /* push_rt_task will return true if it moved an RT */
842 while (push_rt_task(rq))
843 ;
844}
845
846static int pull_rt_task(struct rq *this_rq)
847{
848 int this_cpu = this_rq->cpu, ret = 0, cpu;
849 struct task_struct *p, *next;
850 struct rq *src_rq;
851
852 if (likely(!rt_overloaded(this_rq)))
853 return 0;
854
855 next = pick_next_task_rt(this_rq);
856
857 for_each_cpu_mask(cpu, this_rq->rd->rto_mask) {
858 if (this_cpu == cpu)
859 continue;
860
861 src_rq = cpu_rq(cpu);
862 /*
863 * We can potentially drop this_rq's lock in
864 * double_lock_balance, and another CPU could
865 * steal our next task - hence we must cause
866 * the caller to recalculate the next task
867 * in that case:
868 */
869 if (double_lock_balance(this_rq, src_rq)) {
870 struct task_struct *old_next = next;
871
872 next = pick_next_task_rt(this_rq);
873 if (next != old_next)
874 ret = 1;
875 }
876
877 /*
878 * Are there still pullable RT tasks?
879 */
880 if (src_rq->rt.rt_nr_running <= 1)
881 goto skip;
882
883 p = pick_next_highest_task_rt(src_rq, this_cpu);
884
885 /*
886 * Do we have an RT task that preempts
887 * the to-be-scheduled task?
888 */
889 if (p && (!next || (p->prio < next->prio))) {
890 WARN_ON(p == src_rq->curr);
891 WARN_ON(!p->se.on_rq);
892
893 /*
894 * There's a chance that p is higher in priority
895 * than what's currently running on its cpu.
896 * This is just that p is wakeing up and hasn't
897 * had a chance to schedule. We only pull
898 * p if it is lower in priority than the
899 * current task on the run queue or
900 * this_rq next task is lower in prio than
901 * the current task on that rq.
902 */
903 if (p->prio < src_rq->curr->prio ||
904 (next && next->prio < src_rq->curr->prio))
905 goto skip;
906
907 ret = 1;
908
909 deactivate_task(src_rq, p, 0);
910 set_task_cpu(p, this_cpu);
911 activate_task(this_rq, p, 0);
912 /*
913 * We continue with the search, just in
914 * case there's an even higher prio task
915 * in another runqueue. (low likelyhood
916 * but possible)
917 *
918 * Update next so that we won't pick a task
919 * on another cpu with a priority lower (or equal)
920 * than the one we just picked.
921 */
922 next = p;
923
924 }
925 skip:
926 spin_unlock(&src_rq->lock);
927 }
928
929 return ret;
930}
931
932static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
933{
934 /* Try to pull RT tasks here if we lower this rq's prio */
935 if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio)
936 pull_rt_task(rq);
937}
938
939static void post_schedule_rt(struct rq *rq)
940{
941 /*
942 * If we have more than one rt_task queued, then
943 * see if we can push the other rt_tasks off to other CPUS.
944 * Note we may release the rq lock, and since
945 * the lock was owned by prev, we need to release it
946 * first via finish_lock_switch and then reaquire it here.
947 */
948 if (unlikely(rq->rt.overloaded)) {
949 spin_lock_irq(&rq->lock);
950 push_rt_tasks(rq);
951 spin_unlock_irq(&rq->lock);
952 }
953}
954
955
956static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
957{
958 if (!task_running(rq, p) &&
959 (p->prio >= rq->rt.highest_prio) &&
960 rq->rt.overloaded)
961 push_rt_tasks(rq);
173} 962}
174 963
175static unsigned long 964static unsigned long
@@ -178,38 +967,170 @@ load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
178 struct sched_domain *sd, enum cpu_idle_type idle, 967 struct sched_domain *sd, enum cpu_idle_type idle,
179 int *all_pinned, int *this_best_prio) 968 int *all_pinned, int *this_best_prio)
180{ 969{
181 struct rq_iterator rt_rq_iterator; 970 /* don't touch RT tasks */
182 971 return 0;
183 rt_rq_iterator.start = load_balance_start_rt;
184 rt_rq_iterator.next = load_balance_next_rt;
185 /* pass 'busiest' rq argument into
186 * load_balance_[start|next]_rt iterators
187 */
188 rt_rq_iterator.arg = busiest;
189
190 return balance_tasks(this_rq, this_cpu, busiest, max_load_move, sd,
191 idle, all_pinned, this_best_prio, &rt_rq_iterator);
192} 972}
193 973
194static int 974static int
195move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest, 975move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
196 struct sched_domain *sd, enum cpu_idle_type idle) 976 struct sched_domain *sd, enum cpu_idle_type idle)
197{ 977{
198 struct rq_iterator rt_rq_iterator; 978 /* don't touch RT tasks */
979 return 0;
980}
981
982static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
983{
984 int weight = cpus_weight(*new_mask);
985
986 BUG_ON(!rt_task(p));
199 987
200 rt_rq_iterator.start = load_balance_start_rt; 988 /*
201 rt_rq_iterator.next = load_balance_next_rt; 989 * Update the migration status of the RQ if we have an RT task
202 rt_rq_iterator.arg = busiest; 990 * which is running AND changing its weight value.
991 */
992 if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
993 struct rq *rq = task_rq(p);
994
995 if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
996 rq->rt.rt_nr_migratory++;
997 } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
998 BUG_ON(!rq->rt.rt_nr_migratory);
999 rq->rt.rt_nr_migratory--;
1000 }
1001
1002 update_rt_migration(rq);
1003 }
203 1004
204 return iter_move_one_task(this_rq, this_cpu, busiest, sd, idle, 1005 p->cpus_allowed = *new_mask;
205 &rt_rq_iterator); 1006 p->rt.nr_cpus_allowed = weight;
206} 1007}
207#endif
208 1008
209static void task_tick_rt(struct rq *rq, struct task_struct *p) 1009/* Assumes rq->lock is held */
1010static void join_domain_rt(struct rq *rq)
1011{
1012 if (rq->rt.overloaded)
1013 rt_set_overload(rq);
1014}
1015
1016/* Assumes rq->lock is held */
1017static void leave_domain_rt(struct rq *rq)
1018{
1019 if (rq->rt.overloaded)
1020 rt_clear_overload(rq);
1021}
1022
1023/*
1024 * When switch from the rt queue, we bring ourselves to a position
1025 * that we might want to pull RT tasks from other runqueues.
1026 */
1027static void switched_from_rt(struct rq *rq, struct task_struct *p,
1028 int running)
1029{
1030 /*
1031 * If there are other RT tasks then we will reschedule
1032 * and the scheduling of the other RT tasks will handle
1033 * the balancing. But if we are the last RT task
1034 * we may need to handle the pulling of RT tasks
1035 * now.
1036 */
1037 if (!rq->rt.rt_nr_running)
1038 pull_rt_task(rq);
1039}
1040#endif /* CONFIG_SMP */
1041
1042/*
1043 * When switching a task to RT, we may overload the runqueue
1044 * with RT tasks. In this case we try to push them off to
1045 * other runqueues.
1046 */
1047static void switched_to_rt(struct rq *rq, struct task_struct *p,
1048 int running)
1049{
1050 int check_resched = 1;
1051
1052 /*
1053 * If we are already running, then there's nothing
1054 * that needs to be done. But if we are not running
1055 * we may need to preempt the current running task.
1056 * If that current running task is also an RT task
1057 * then see if we can move to another run queue.
1058 */
1059 if (!running) {
1060#ifdef CONFIG_SMP
1061 if (rq->rt.overloaded && push_rt_task(rq) &&
1062 /* Don't resched if we changed runqueues */
1063 rq != task_rq(p))
1064 check_resched = 0;
1065#endif /* CONFIG_SMP */
1066 if (check_resched && p->prio < rq->curr->prio)
1067 resched_task(rq->curr);
1068 }
1069}
1070
1071/*
1072 * Priority of the task has changed. This may cause
1073 * us to initiate a push or pull.
1074 */
1075static void prio_changed_rt(struct rq *rq, struct task_struct *p,
1076 int oldprio, int running)
1077{
1078 if (running) {
1079#ifdef CONFIG_SMP
1080 /*
1081 * If our priority decreases while running, we
1082 * may need to pull tasks to this runqueue.
1083 */
1084 if (oldprio < p->prio)
1085 pull_rt_task(rq);
1086 /*
1087 * If there's a higher priority task waiting to run
1088 * then reschedule.
1089 */
1090 if (p->prio > rq->rt.highest_prio)
1091 resched_task(p);
1092#else
1093 /* For UP simply resched on drop of prio */
1094 if (oldprio < p->prio)
1095 resched_task(p);
1096#endif /* CONFIG_SMP */
1097 } else {
1098 /*
1099 * This task is not running, but if it is
1100 * greater than the current running task
1101 * then reschedule.
1102 */
1103 if (p->prio < rq->curr->prio)
1104 resched_task(rq->curr);
1105 }
1106}
1107
1108static void watchdog(struct rq *rq, struct task_struct *p)
1109{
1110 unsigned long soft, hard;
1111
1112 if (!p->signal)
1113 return;
1114
1115 soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
1116 hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
1117
1118 if (soft != RLIM_INFINITY) {
1119 unsigned long next;
1120
1121 p->rt.timeout++;
1122 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
1123 if (p->rt.timeout > next)
1124 p->it_sched_expires = p->se.sum_exec_runtime;
1125 }
1126}
1127
1128static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
210{ 1129{
211 update_curr_rt(rq); 1130 update_curr_rt(rq);
212 1131
1132 watchdog(rq, p);
1133
213 /* 1134 /*
214 * RR tasks need a special form of timeslice management. 1135 * RR tasks need a special form of timeslice management.
215 * FIFO tasks have no timeslices. 1136 * FIFO tasks have no timeslices.
@@ -217,16 +1138,16 @@ static void task_tick_rt(struct rq *rq, struct task_struct *p)
217 if (p->policy != SCHED_RR) 1138 if (p->policy != SCHED_RR)
218 return; 1139 return;
219 1140
220 if (--p->time_slice) 1141 if (--p->rt.time_slice)
221 return; 1142 return;
222 1143
223 p->time_slice = DEF_TIMESLICE; 1144 p->rt.time_slice = DEF_TIMESLICE;
224 1145
225 /* 1146 /*
226 * Requeue to the end of queue if we are not the only element 1147 * Requeue to the end of queue if we are not the only element
227 * on the queue: 1148 * on the queue:
228 */ 1149 */
229 if (p->run_list.prev != p->run_list.next) { 1150 if (p->rt.run_list.prev != p->rt.run_list.next) {
230 requeue_task_rt(rq, p); 1151 requeue_task_rt(rq, p);
231 set_tsk_need_resched(p); 1152 set_tsk_need_resched(p);
232 } 1153 }
@@ -244,6 +1165,9 @@ const struct sched_class rt_sched_class = {
244 .enqueue_task = enqueue_task_rt, 1165 .enqueue_task = enqueue_task_rt,
245 .dequeue_task = dequeue_task_rt, 1166 .dequeue_task = dequeue_task_rt,
246 .yield_task = yield_task_rt, 1167 .yield_task = yield_task_rt,
1168#ifdef CONFIG_SMP
1169 .select_task_rq = select_task_rq_rt,
1170#endif /* CONFIG_SMP */
247 1171
248 .check_preempt_curr = check_preempt_curr_rt, 1172 .check_preempt_curr = check_preempt_curr_rt,
249 1173
@@ -253,8 +1177,18 @@ const struct sched_class rt_sched_class = {
253#ifdef CONFIG_SMP 1177#ifdef CONFIG_SMP
254 .load_balance = load_balance_rt, 1178 .load_balance = load_balance_rt,
255 .move_one_task = move_one_task_rt, 1179 .move_one_task = move_one_task_rt,
1180 .set_cpus_allowed = set_cpus_allowed_rt,
1181 .join_domain = join_domain_rt,
1182 .leave_domain = leave_domain_rt,
1183 .pre_schedule = pre_schedule_rt,
1184 .post_schedule = post_schedule_rt,
1185 .task_wake_up = task_wake_up_rt,
1186 .switched_from = switched_from_rt,
256#endif 1187#endif
257 1188
258 .set_curr_task = set_curr_task_rt, 1189 .set_curr_task = set_curr_task_rt,
259 .task_tick = task_tick_rt, 1190 .task_tick = task_tick_rt,
1191
1192 .prio_changed = prio_changed_rt,
1193 .switched_to = switched_to_rt,
260}; 1194};
diff --git a/kernel/softlockup.c b/kernel/softlockup.c
index 11df812263c8..c1d76552446e 100644
--- a/kernel/softlockup.c
+++ b/kernel/softlockup.c
@@ -8,6 +8,7 @@
8 */ 8 */
9#include <linux/mm.h> 9#include <linux/mm.h>
10#include <linux/cpu.h> 10#include <linux/cpu.h>
11#include <linux/nmi.h>
11#include <linux/init.h> 12#include <linux/init.h>
12#include <linux/delay.h> 13#include <linux/delay.h>
13#include <linux/freezer.h> 14#include <linux/freezer.h>
@@ -23,8 +24,8 @@ static DEFINE_PER_CPU(unsigned long, touch_timestamp);
23static DEFINE_PER_CPU(unsigned long, print_timestamp); 24static DEFINE_PER_CPU(unsigned long, print_timestamp);
24static DEFINE_PER_CPU(struct task_struct *, watchdog_task); 25static DEFINE_PER_CPU(struct task_struct *, watchdog_task);
25 26
26static int did_panic; 27static int __read_mostly did_panic;
27int softlockup_thresh = 10; 28unsigned long __read_mostly softlockup_thresh = 60;
28 29
29static int 30static int
30softlock_panic(struct notifier_block *this, unsigned long event, void *ptr) 31softlock_panic(struct notifier_block *this, unsigned long event, void *ptr)
@@ -45,7 +46,7 @@ static struct notifier_block panic_block = {
45 */ 46 */
46static unsigned long get_timestamp(int this_cpu) 47static unsigned long get_timestamp(int this_cpu)
47{ 48{
48 return cpu_clock(this_cpu) >> 30; /* 2^30 ~= 10^9 */ 49 return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */
49} 50}
50 51
51void touch_softlockup_watchdog(void) 52void touch_softlockup_watchdog(void)
@@ -100,11 +101,7 @@ void softlockup_tick(void)
100 101
101 now = get_timestamp(this_cpu); 102 now = get_timestamp(this_cpu);
102 103
103 /* Wake up the high-prio watchdog task every second: */ 104 /* Warn about unreasonable delays: */
104 if (now > (touch_timestamp + 1))
105 wake_up_process(per_cpu(watchdog_task, this_cpu));
106
107 /* Warn about unreasonable 10+ seconds delays: */
108 if (now <= (touch_timestamp + softlockup_thresh)) 105 if (now <= (touch_timestamp + softlockup_thresh))
109 return; 106 return;
110 107
@@ -122,11 +119,93 @@ void softlockup_tick(void)
122} 119}
123 120
124/* 121/*
122 * Have a reasonable limit on the number of tasks checked:
123 */
124unsigned long __read_mostly sysctl_hung_task_check_count = 1024;
125
126/*
127 * Zero means infinite timeout - no checking done:
128 */
129unsigned long __read_mostly sysctl_hung_task_timeout_secs = 120;
130
131unsigned long __read_mostly sysctl_hung_task_warnings = 10;
132
133/*
134 * Only do the hung-tasks check on one CPU:
135 */
136static int check_cpu __read_mostly = -1;
137
138static void check_hung_task(struct task_struct *t, unsigned long now)
139{
140 unsigned long switch_count = t->nvcsw + t->nivcsw;
141
142 if (t->flags & PF_FROZEN)
143 return;
144
145 if (switch_count != t->last_switch_count || !t->last_switch_timestamp) {
146 t->last_switch_count = switch_count;
147 t->last_switch_timestamp = now;
148 return;
149 }
150 if ((long)(now - t->last_switch_timestamp) <
151 sysctl_hung_task_timeout_secs)
152 return;
153 if (sysctl_hung_task_warnings < 0)
154 return;
155 sysctl_hung_task_warnings--;
156
157 /*
158 * Ok, the task did not get scheduled for more than 2 minutes,
159 * complain:
160 */
161 printk(KERN_ERR "INFO: task %s:%d blocked for more than "
162 "%ld seconds.\n", t->comm, t->pid,
163 sysctl_hung_task_timeout_secs);
164 printk(KERN_ERR "\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
165 " disables this message.\n");
166 sched_show_task(t);
167 __debug_show_held_locks(t);
168
169 t->last_switch_timestamp = now;
170 touch_nmi_watchdog();
171}
172
173/*
174 * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
175 * a really long time (120 seconds). If that happens, print out
176 * a warning.
177 */
178static void check_hung_uninterruptible_tasks(int this_cpu)
179{
180 int max_count = sysctl_hung_task_check_count;
181 unsigned long now = get_timestamp(this_cpu);
182 struct task_struct *g, *t;
183
184 /*
185 * If the system crashed already then all bets are off,
186 * do not report extra hung tasks:
187 */
188 if ((tainted & TAINT_DIE) || did_panic)
189 return;
190
191 read_lock(&tasklist_lock);
192 do_each_thread(g, t) {
193 if (!--max_count)
194 break;
195 if (t->state & TASK_UNINTERRUPTIBLE)
196 check_hung_task(t, now);
197 } while_each_thread(g, t);
198
199 read_unlock(&tasklist_lock);
200}
201
202/*
125 * The watchdog thread - runs every second and touches the timestamp. 203 * The watchdog thread - runs every second and touches the timestamp.
126 */ 204 */
127static int watchdog(void *__bind_cpu) 205static int watchdog(void *__bind_cpu)
128{ 206{
129 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; 207 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
208 int this_cpu = (long)__bind_cpu;
130 209
131 sched_setscheduler(current, SCHED_FIFO, &param); 210 sched_setscheduler(current, SCHED_FIFO, &param);
132 211
@@ -135,13 +214,18 @@ static int watchdog(void *__bind_cpu)
135 214
136 /* 215 /*
137 * Run briefly once per second to reset the softlockup timestamp. 216 * Run briefly once per second to reset the softlockup timestamp.
138 * If this gets delayed for more than 10 seconds then the 217 * If this gets delayed for more than 60 seconds then the
139 * debug-printout triggers in softlockup_tick(). 218 * debug-printout triggers in softlockup_tick().
140 */ 219 */
141 while (!kthread_should_stop()) { 220 while (!kthread_should_stop()) {
142 set_current_state(TASK_INTERRUPTIBLE);
143 touch_softlockup_watchdog(); 221 touch_softlockup_watchdog();
144 schedule(); 222 msleep_interruptible(10000);
223
224 if (this_cpu != check_cpu)
225 continue;
226
227 if (sysctl_hung_task_timeout_secs)
228 check_hung_uninterruptible_tasks(this_cpu);
145 } 229 }
146 230
147 return 0; 231 return 0;
@@ -171,6 +255,7 @@ cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
171 break; 255 break;
172 case CPU_ONLINE: 256 case CPU_ONLINE:
173 case CPU_ONLINE_FROZEN: 257 case CPU_ONLINE_FROZEN:
258 check_cpu = any_online_cpu(cpu_online_map);
174 wake_up_process(per_cpu(watchdog_task, hotcpu)); 259 wake_up_process(per_cpu(watchdog_task, hotcpu));
175 break; 260 break;
176#ifdef CONFIG_HOTPLUG_CPU 261#ifdef CONFIG_HOTPLUG_CPU
@@ -181,6 +266,15 @@ cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
181 /* Unbind so it can run. Fall thru. */ 266 /* Unbind so it can run. Fall thru. */
182 kthread_bind(per_cpu(watchdog_task, hotcpu), 267 kthread_bind(per_cpu(watchdog_task, hotcpu),
183 any_online_cpu(cpu_online_map)); 268 any_online_cpu(cpu_online_map));
269 case CPU_DOWN_PREPARE:
270 case CPU_DOWN_PREPARE_FROZEN:
271 if (hotcpu == check_cpu) {
272 cpumask_t temp_cpu_online_map = cpu_online_map;
273
274 cpu_clear(hotcpu, temp_cpu_online_map);
275 check_cpu = any_online_cpu(temp_cpu_online_map);
276 }
277 break;
184 case CPU_DEAD: 278 case CPU_DEAD:
185 case CPU_DEAD_FROZEN: 279 case CPU_DEAD_FROZEN:
186 p = per_cpu(watchdog_task, hotcpu); 280 p = per_cpu(watchdog_task, hotcpu);
diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
index 319821ef78af..51b5ee53571a 100644
--- a/kernel/stop_machine.c
+++ b/kernel/stop_machine.c
@@ -203,13 +203,13 @@ int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
203 int ret; 203 int ret;
204 204
205 /* No CPUs can come up or down during this. */ 205 /* No CPUs can come up or down during this. */
206 lock_cpu_hotplug(); 206 get_online_cpus();
207 p = __stop_machine_run(fn, data, cpu); 207 p = __stop_machine_run(fn, data, cpu);
208 if (!IS_ERR(p)) 208 if (!IS_ERR(p))
209 ret = kthread_stop(p); 209 ret = kthread_stop(p);
210 else 210 else
211 ret = PTR_ERR(p); 211 ret = PTR_ERR(p);
212 unlock_cpu_hotplug(); 212 put_online_cpus();
213 213
214 return ret; 214 return ret;
215} 215}
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index c68f68dcc605..8e96558cb8f3 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -81,6 +81,7 @@ extern int compat_log;
81extern int maps_protect; 81extern int maps_protect;
82extern int sysctl_stat_interval; 82extern int sysctl_stat_interval;
83extern int audit_argv_kb; 83extern int audit_argv_kb;
84extern int latencytop_enabled;
84 85
85/* Constants used for minimum and maximum */ 86/* Constants used for minimum and maximum */
86#ifdef CONFIG_DETECT_SOFTLOCKUP 87#ifdef CONFIG_DETECT_SOFTLOCKUP
@@ -306,9 +307,43 @@ static struct ctl_table kern_table[] = {
306 .procname = "sched_nr_migrate", 307 .procname = "sched_nr_migrate",
307 .data = &sysctl_sched_nr_migrate, 308 .data = &sysctl_sched_nr_migrate,
308 .maxlen = sizeof(unsigned int), 309 .maxlen = sizeof(unsigned int),
309 .mode = 644, 310 .mode = 0644,
311 .proc_handler = &proc_dointvec,
312 },
313 {
314 .ctl_name = CTL_UNNUMBERED,
315 .procname = "sched_rt_period_ms",
316 .data = &sysctl_sched_rt_period,
317 .maxlen = sizeof(unsigned int),
318 .mode = 0644,
310 .proc_handler = &proc_dointvec, 319 .proc_handler = &proc_dointvec,
311 }, 320 },
321 {
322 .ctl_name = CTL_UNNUMBERED,
323 .procname = "sched_rt_ratio",
324 .data = &sysctl_sched_rt_ratio,
325 .maxlen = sizeof(unsigned int),
326 .mode = 0644,
327 .proc_handler = &proc_dointvec,
328 },
329#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
330 {
331 .ctl_name = CTL_UNNUMBERED,
332 .procname = "sched_min_bal_int_shares",
333 .data = &sysctl_sched_min_bal_int_shares,
334 .maxlen = sizeof(unsigned int),
335 .mode = 0644,
336 .proc_handler = &proc_dointvec,
337 },
338 {
339 .ctl_name = CTL_UNNUMBERED,
340 .procname = "sched_max_bal_int_shares",
341 .data = &sysctl_sched_max_bal_int_shares,
342 .maxlen = sizeof(unsigned int),
343 .mode = 0644,
344 .proc_handler = &proc_dointvec,
345 },
346#endif
312#endif 347#endif
313 { 348 {
314 .ctl_name = CTL_UNNUMBERED, 349 .ctl_name = CTL_UNNUMBERED,
@@ -382,6 +417,15 @@ static struct ctl_table kern_table[] = {
382 .proc_handler = &proc_dointvec_taint, 417 .proc_handler = &proc_dointvec_taint,
383 }, 418 },
384#endif 419#endif
420#ifdef CONFIG_LATENCYTOP
421 {
422 .procname = "latencytop",
423 .data = &latencytop_enabled,
424 .maxlen = sizeof(int),
425 .mode = 0644,
426 .proc_handler = &proc_dointvec,
427 },
428#endif
385#ifdef CONFIG_SECURITY_CAPABILITIES 429#ifdef CONFIG_SECURITY_CAPABILITIES
386 { 430 {
387 .procname = "cap-bound", 431 .procname = "cap-bound",
@@ -728,13 +772,40 @@ static struct ctl_table kern_table[] = {
728 .ctl_name = CTL_UNNUMBERED, 772 .ctl_name = CTL_UNNUMBERED,
729 .procname = "softlockup_thresh", 773 .procname = "softlockup_thresh",
730 .data = &softlockup_thresh, 774 .data = &softlockup_thresh,
731 .maxlen = sizeof(int), 775 .maxlen = sizeof(unsigned long),
732 .mode = 0644, 776 .mode = 0644,
733 .proc_handler = &proc_dointvec_minmax, 777 .proc_handler = &proc_doulongvec_minmax,
734 .strategy = &sysctl_intvec, 778 .strategy = &sysctl_intvec,
735 .extra1 = &one, 779 .extra1 = &one,
736 .extra2 = &sixty, 780 .extra2 = &sixty,
737 }, 781 },
782 {
783 .ctl_name = CTL_UNNUMBERED,
784 .procname = "hung_task_check_count",
785 .data = &sysctl_hung_task_check_count,
786 .maxlen = sizeof(unsigned long),
787 .mode = 0644,
788 .proc_handler = &proc_doulongvec_minmax,
789 .strategy = &sysctl_intvec,
790 },
791 {
792 .ctl_name = CTL_UNNUMBERED,
793 .procname = "hung_task_timeout_secs",
794 .data = &sysctl_hung_task_timeout_secs,
795 .maxlen = sizeof(unsigned long),
796 .mode = 0644,
797 .proc_handler = &proc_doulongvec_minmax,
798 .strategy = &sysctl_intvec,
799 },
800 {
801 .ctl_name = CTL_UNNUMBERED,
802 .procname = "hung_task_warnings",
803 .data = &sysctl_hung_task_warnings,
804 .maxlen = sizeof(unsigned long),
805 .mode = 0644,
806 .proc_handler = &proc_doulongvec_minmax,
807 .strategy = &sysctl_intvec,
808 },
738#endif 809#endif
739#ifdef CONFIG_COMPAT 810#ifdef CONFIG_COMPAT
740 { 811 {
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index cb89fa8db110..1a21b6fdb674 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -153,6 +153,7 @@ void tick_nohz_update_jiffies(void)
153void tick_nohz_stop_sched_tick(void) 153void tick_nohz_stop_sched_tick(void)
154{ 154{
155 unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags; 155 unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags;
156 unsigned long rt_jiffies;
156 struct tick_sched *ts; 157 struct tick_sched *ts;
157 ktime_t last_update, expires, now, delta; 158 ktime_t last_update, expires, now, delta;
158 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev; 159 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
@@ -216,6 +217,10 @@ void tick_nohz_stop_sched_tick(void)
216 next_jiffies = get_next_timer_interrupt(last_jiffies); 217 next_jiffies = get_next_timer_interrupt(last_jiffies);
217 delta_jiffies = next_jiffies - last_jiffies; 218 delta_jiffies = next_jiffies - last_jiffies;
218 219
220 rt_jiffies = rt_needs_cpu(cpu);
221 if (rt_jiffies && rt_jiffies < delta_jiffies)
222 delta_jiffies = rt_jiffies;
223
219 if (rcu_needs_cpu(cpu)) 224 if (rcu_needs_cpu(cpu))
220 delta_jiffies = 1; 225 delta_jiffies = 1;
221 /* 226 /*
@@ -509,7 +514,6 @@ static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
509{ 514{
510 struct tick_sched *ts = 515 struct tick_sched *ts =
511 container_of(timer, struct tick_sched, sched_timer); 516 container_of(timer, struct tick_sched, sched_timer);
512 struct hrtimer_cpu_base *base = timer->base->cpu_base;
513 struct pt_regs *regs = get_irq_regs(); 517 struct pt_regs *regs = get_irq_regs();
514 ktime_t now = ktime_get(); 518 ktime_t now = ktime_get();
515 int cpu = smp_processor_id(); 519 int cpu = smp_processor_id();
@@ -547,15 +551,8 @@ static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
547 touch_softlockup_watchdog(); 551 touch_softlockup_watchdog();
548 ts->idle_jiffies++; 552 ts->idle_jiffies++;
549 } 553 }
550 /*
551 * update_process_times() might take tasklist_lock, hence
552 * drop the base lock. sched-tick hrtimers are per-CPU and
553 * never accessible by userspace APIs, so this is safe to do.
554 */
555 spin_unlock(&base->lock);
556 update_process_times(user_mode(regs)); 554 update_process_times(user_mode(regs));
557 profile_tick(CPU_PROFILING); 555 profile_tick(CPU_PROFILING);
558 spin_lock(&base->lock);
559 } 556 }
560 557
561 /* Do not restart, when we are in the idle loop */ 558 /* Do not restart, when we are in the idle loop */
diff --git a/kernel/timer.c b/kernel/timer.c
index 2a00c22203f3..f739dfb539ce 100644
--- a/kernel/timer.c
+++ b/kernel/timer.c
@@ -896,7 +896,7 @@ static void run_timer_softirq(struct softirq_action *h)
896{ 896{
897 tvec_base_t *base = __get_cpu_var(tvec_bases); 897 tvec_base_t *base = __get_cpu_var(tvec_bases);
898 898
899 hrtimer_run_queues(); 899 hrtimer_run_pending();
900 900
901 if (time_after_eq(jiffies, base->timer_jiffies)) 901 if (time_after_eq(jiffies, base->timer_jiffies))
902 __run_timers(base); 902 __run_timers(base);
@@ -907,6 +907,7 @@ static void run_timer_softirq(struct softirq_action *h)
907 */ 907 */
908void run_local_timers(void) 908void run_local_timers(void)
909{ 909{
910 hrtimer_run_queues();
910 raise_softirq(TIMER_SOFTIRQ); 911 raise_softirq(TIMER_SOFTIRQ);
911 softlockup_tick(); 912 softlockup_tick();
912} 913}
diff --git a/kernel/user.c b/kernel/user.c
index ab4fd706993b..bc1c48d35cb3 100644
--- a/kernel/user.c
+++ b/kernel/user.c
@@ -319,7 +319,7 @@ void free_uid(struct user_struct *up)
319struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid) 319struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
320{ 320{
321 struct hlist_head *hashent = uidhashentry(ns, uid); 321 struct hlist_head *hashent = uidhashentry(ns, uid);
322 struct user_struct *up; 322 struct user_struct *up, *new;
323 323
324 /* Make uid_hash_find() + uids_user_create() + uid_hash_insert() 324 /* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
325 * atomic. 325 * atomic.
@@ -331,13 +331,9 @@ struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
331 spin_unlock_irq(&uidhash_lock); 331 spin_unlock_irq(&uidhash_lock);
332 332
333 if (!up) { 333 if (!up) {
334 struct user_struct *new;
335
336 new = kmem_cache_alloc(uid_cachep, GFP_KERNEL); 334 new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
337 if (!new) { 335 if (!new)
338 uids_mutex_unlock(); 336 goto out_unlock;
339 return NULL;
340 }
341 337
342 new->uid = uid; 338 new->uid = uid;
343 atomic_set(&new->__count, 1); 339 atomic_set(&new->__count, 1);
@@ -353,28 +349,14 @@ struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
353#endif 349#endif
354 new->locked_shm = 0; 350 new->locked_shm = 0;
355 351
356 if (alloc_uid_keyring(new, current) < 0) { 352 if (alloc_uid_keyring(new, current) < 0)
357 kmem_cache_free(uid_cachep, new); 353 goto out_free_user;
358 uids_mutex_unlock();
359 return NULL;
360 }
361 354
362 if (sched_create_user(new) < 0) { 355 if (sched_create_user(new) < 0)
363 key_put(new->uid_keyring); 356 goto out_put_keys;
364 key_put(new->session_keyring);
365 kmem_cache_free(uid_cachep, new);
366 uids_mutex_unlock();
367 return NULL;
368 }
369 357
370 if (uids_user_create(new)) { 358 if (uids_user_create(new))
371 sched_destroy_user(new); 359 goto out_destoy_sched;
372 key_put(new->uid_keyring);
373 key_put(new->session_keyring);
374 kmem_cache_free(uid_cachep, new);
375 uids_mutex_unlock();
376 return NULL;
377 }
378 360
379 /* 361 /*
380 * Before adding this, check whether we raced 362 * Before adding this, check whether we raced
@@ -402,6 +384,17 @@ struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
402 uids_mutex_unlock(); 384 uids_mutex_unlock();
403 385
404 return up; 386 return up;
387
388out_destoy_sched:
389 sched_destroy_user(new);
390out_put_keys:
391 key_put(new->uid_keyring);
392 key_put(new->session_keyring);
393out_free_user:
394 kmem_cache_free(uid_cachep, new);
395out_unlock:
396 uids_mutex_unlock();
397 return NULL;
405} 398}
406 399
407void switch_uid(struct user_struct *new_user) 400void switch_uid(struct user_struct *new_user)
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 8db0b597509e..52db48e7f6e7 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -67,9 +67,8 @@ struct workqueue_struct {
67#endif 67#endif
68}; 68};
69 69
70/* All the per-cpu workqueues on the system, for hotplug cpu to add/remove 70/* Serializes the accesses to the list of workqueues. */
71 threads to each one as cpus come/go. */ 71static DEFINE_SPINLOCK(workqueue_lock);
72static DEFINE_MUTEX(workqueue_mutex);
73static LIST_HEAD(workqueues); 72static LIST_HEAD(workqueues);
74 73
75static int singlethread_cpu __read_mostly; 74static int singlethread_cpu __read_mostly;
@@ -592,8 +591,6 @@ EXPORT_SYMBOL(schedule_delayed_work_on);
592 * Returns zero on success. 591 * Returns zero on success.
593 * Returns -ve errno on failure. 592 * Returns -ve errno on failure.
594 * 593 *
595 * Appears to be racy against CPU hotplug.
596 *
597 * schedule_on_each_cpu() is very slow. 594 * schedule_on_each_cpu() is very slow.
598 */ 595 */
599int schedule_on_each_cpu(work_func_t func) 596int schedule_on_each_cpu(work_func_t func)
@@ -605,7 +602,7 @@ int schedule_on_each_cpu(work_func_t func)
605 if (!works) 602 if (!works)
606 return -ENOMEM; 603 return -ENOMEM;
607 604
608 preempt_disable(); /* CPU hotplug */ 605 get_online_cpus();
609 for_each_online_cpu(cpu) { 606 for_each_online_cpu(cpu) {
610 struct work_struct *work = per_cpu_ptr(works, cpu); 607 struct work_struct *work = per_cpu_ptr(works, cpu);
611 608
@@ -613,8 +610,8 @@ int schedule_on_each_cpu(work_func_t func)
613 set_bit(WORK_STRUCT_PENDING, work_data_bits(work)); 610 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
614 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work); 611 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
615 } 612 }
616 preempt_enable();
617 flush_workqueue(keventd_wq); 613 flush_workqueue(keventd_wq);
614 put_online_cpus();
618 free_percpu(works); 615 free_percpu(works);
619 return 0; 616 return 0;
620} 617}
@@ -750,8 +747,10 @@ struct workqueue_struct *__create_workqueue_key(const char *name,
750 err = create_workqueue_thread(cwq, singlethread_cpu); 747 err = create_workqueue_thread(cwq, singlethread_cpu);
751 start_workqueue_thread(cwq, -1); 748 start_workqueue_thread(cwq, -1);
752 } else { 749 } else {
753 mutex_lock(&workqueue_mutex); 750 get_online_cpus();
751 spin_lock(&workqueue_lock);
754 list_add(&wq->list, &workqueues); 752 list_add(&wq->list, &workqueues);
753 spin_unlock(&workqueue_lock);
755 754
756 for_each_possible_cpu(cpu) { 755 for_each_possible_cpu(cpu) {
757 cwq = init_cpu_workqueue(wq, cpu); 756 cwq = init_cpu_workqueue(wq, cpu);
@@ -760,7 +759,7 @@ struct workqueue_struct *__create_workqueue_key(const char *name,
760 err = create_workqueue_thread(cwq, cpu); 759 err = create_workqueue_thread(cwq, cpu);
761 start_workqueue_thread(cwq, cpu); 760 start_workqueue_thread(cwq, cpu);
762 } 761 }
763 mutex_unlock(&workqueue_mutex); 762 put_online_cpus();
764 } 763 }
765 764
766 if (err) { 765 if (err) {
@@ -775,7 +774,7 @@ static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
775{ 774{
776 /* 775 /*
777 * Our caller is either destroy_workqueue() or CPU_DEAD, 776 * Our caller is either destroy_workqueue() or CPU_DEAD,
778 * workqueue_mutex protects cwq->thread 777 * get_online_cpus() protects cwq->thread.
779 */ 778 */
780 if (cwq->thread == NULL) 779 if (cwq->thread == NULL)
781 return; 780 return;
@@ -810,9 +809,11 @@ void destroy_workqueue(struct workqueue_struct *wq)
810 struct cpu_workqueue_struct *cwq; 809 struct cpu_workqueue_struct *cwq;
811 int cpu; 810 int cpu;
812 811
813 mutex_lock(&workqueue_mutex); 812 get_online_cpus();
813 spin_lock(&workqueue_lock);
814 list_del(&wq->list); 814 list_del(&wq->list);
815 mutex_unlock(&workqueue_mutex); 815 spin_unlock(&workqueue_lock);
816 put_online_cpus();
816 817
817 for_each_cpu_mask(cpu, *cpu_map) { 818 for_each_cpu_mask(cpu, *cpu_map) {
818 cwq = per_cpu_ptr(wq->cpu_wq, cpu); 819 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
@@ -835,13 +836,6 @@ static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
835 action &= ~CPU_TASKS_FROZEN; 836 action &= ~CPU_TASKS_FROZEN;
836 837
837 switch (action) { 838 switch (action) {
838 case CPU_LOCK_ACQUIRE:
839 mutex_lock(&workqueue_mutex);
840 return NOTIFY_OK;
841
842 case CPU_LOCK_RELEASE:
843 mutex_unlock(&workqueue_mutex);
844 return NOTIFY_OK;
845 839
846 case CPU_UP_PREPARE: 840 case CPU_UP_PREPARE:
847 cpu_set(cpu, cpu_populated_map); 841 cpu_set(cpu, cpu_populated_map);
@@ -854,7 +848,8 @@ static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
854 case CPU_UP_PREPARE: 848 case CPU_UP_PREPARE:
855 if (!create_workqueue_thread(cwq, cpu)) 849 if (!create_workqueue_thread(cwq, cpu))
856 break; 850 break;
857 printk(KERN_ERR "workqueue for %i failed\n", cpu); 851 printk(KERN_ERR "workqueue [%s] for %i failed\n",
852 wq->name, cpu);
858 return NOTIFY_BAD; 853 return NOTIFY_BAD;
859 854
860 case CPU_ONLINE: 855 case CPU_ONLINE:
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index a60109307d32..14fb355e3caa 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -517,4 +517,18 @@ config FAULT_INJECTION_STACKTRACE_FILTER
517 help 517 help
518 Provide stacktrace filter for fault-injection capabilities 518 Provide stacktrace filter for fault-injection capabilities
519 519
520config LATENCYTOP
521 bool "Latency measuring infrastructure"
522 select FRAME_POINTER if !MIPS
523 select KALLSYMS
524 select KALLSYMS_ALL
525 select STACKTRACE
526 select SCHEDSTATS
527 select SCHED_DEBUG
528 depends on X86 || X86_64
529 help
530 Enable this option if you want to use the LatencyTOP tool
531 to find out which userspace is blocking on what kernel operations.
532
533
520source "samples/Kconfig" 534source "samples/Kconfig"
diff --git a/lib/kernel_lock.c b/lib/kernel_lock.c
index f73e2f8c308f..812dbf00844b 100644
--- a/lib/kernel_lock.c
+++ b/lib/kernel_lock.c
@@ -9,7 +9,6 @@
9#include <linux/module.h> 9#include <linux/module.h>
10#include <linux/kallsyms.h> 10#include <linux/kallsyms.h>
11 11
12#ifdef CONFIG_PREEMPT_BKL
13/* 12/*
14 * The 'big kernel semaphore' 13 * The 'big kernel semaphore'
15 * 14 *
@@ -86,128 +85,6 @@ void __lockfunc unlock_kernel(void)
86 up(&kernel_sem); 85 up(&kernel_sem);
87} 86}
88 87
89#else
90
91/*
92 * The 'big kernel lock'
93 *
94 * This spinlock is taken and released recursively by lock_kernel()
95 * and unlock_kernel(). It is transparently dropped and reacquired
96 * over schedule(). It is used to protect legacy code that hasn't
97 * been migrated to a proper locking design yet.
98 *
99 * Don't use in new code.
100 */
101static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kernel_flag);
102
103
104/*
105 * Acquire/release the underlying lock from the scheduler.
106 *
107 * This is called with preemption disabled, and should
108 * return an error value if it cannot get the lock and
109 * TIF_NEED_RESCHED gets set.
110 *
111 * If it successfully gets the lock, it should increment
112 * the preemption count like any spinlock does.
113 *
114 * (This works on UP too - _raw_spin_trylock will never
115 * return false in that case)
116 */
117int __lockfunc __reacquire_kernel_lock(void)
118{
119 while (!_raw_spin_trylock(&kernel_flag)) {
120 if (test_thread_flag(TIF_NEED_RESCHED))
121 return -EAGAIN;
122 cpu_relax();
123 }
124 preempt_disable();
125 return 0;
126}
127
128void __lockfunc __release_kernel_lock(void)
129{
130 _raw_spin_unlock(&kernel_flag);
131 preempt_enable_no_resched();
132}
133
134/*
135 * These are the BKL spinlocks - we try to be polite about preemption.
136 * If SMP is not on (ie UP preemption), this all goes away because the
137 * _raw_spin_trylock() will always succeed.
138 */
139#ifdef CONFIG_PREEMPT
140static inline void __lock_kernel(void)
141{
142 preempt_disable();
143 if (unlikely(!_raw_spin_trylock(&kernel_flag))) {
144 /*
145 * If preemption was disabled even before this
146 * was called, there's nothing we can be polite
147 * about - just spin.
148 */
149 if (preempt_count() > 1) {
150 _raw_spin_lock(&kernel_flag);
151 return;
152 }
153
154 /*
155 * Otherwise, let's wait for the kernel lock
156 * with preemption enabled..
157 */
158 do {
159 preempt_enable();
160 while (spin_is_locked(&kernel_flag))
161 cpu_relax();
162 preempt_disable();
163 } while (!_raw_spin_trylock(&kernel_flag));
164 }
165}
166
167#else
168
169/*
170 * Non-preemption case - just get the spinlock
171 */
172static inline void __lock_kernel(void)
173{
174 _raw_spin_lock(&kernel_flag);
175}
176#endif
177
178static inline void __unlock_kernel(void)
179{
180 /*
181 * the BKL is not covered by lockdep, so we open-code the
182 * unlocking sequence (and thus avoid the dep-chain ops):
183 */
184 _raw_spin_unlock(&kernel_flag);
185 preempt_enable();
186}
187
188/*
189 * Getting the big kernel lock.
190 *
191 * This cannot happen asynchronously, so we only need to
192 * worry about other CPU's.
193 */
194void __lockfunc lock_kernel(void)
195{
196 int depth = current->lock_depth+1;
197 if (likely(!depth))
198 __lock_kernel();
199 current->lock_depth = depth;
200}
201
202void __lockfunc unlock_kernel(void)
203{
204 BUG_ON(current->lock_depth < 0);
205 if (likely(--current->lock_depth < 0))
206 __unlock_kernel();
207}
208
209#endif
210
211EXPORT_SYMBOL(lock_kernel); 88EXPORT_SYMBOL(lock_kernel);
212EXPORT_SYMBOL(unlock_kernel); 89EXPORT_SYMBOL(unlock_kernel);
213 90
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 91a081a82f55..96473b482099 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -286,7 +286,7 @@ static void __oom_kill_task(struct task_struct *p, int verbose)
286 * all the memory it needs. That way it should be able to 286 * all the memory it needs. That way it should be able to
287 * exit() and clear out its resources quickly... 287 * exit() and clear out its resources quickly...
288 */ 288 */
289 p->time_slice = HZ; 289 p->rt.time_slice = HZ;
290 set_tsk_thread_flag(p, TIF_MEMDIE); 290 set_tsk_thread_flag(p, TIF_MEMDIE);
291 291
292 force_sig(SIGKILL, p); 292 force_sig(SIGKILL, p);
diff --git a/mm/slab.c b/mm/slab.c
index ff31261fd24f..40c00dacbe4b 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -730,8 +730,7 @@ static inline void init_lock_keys(void)
730#endif 730#endif
731 731
732/* 732/*
733 * 1. Guard access to the cache-chain. 733 * Guard access to the cache-chain.
734 * 2. Protect sanity of cpu_online_map against cpu hotplug events
735 */ 734 */
736static DEFINE_MUTEX(cache_chain_mutex); 735static DEFINE_MUTEX(cache_chain_mutex);
737static struct list_head cache_chain; 736static struct list_head cache_chain;
@@ -1331,12 +1330,11 @@ static int __cpuinit cpuup_callback(struct notifier_block *nfb,
1331 int err = 0; 1330 int err = 0;
1332 1331
1333 switch (action) { 1332 switch (action) {
1334 case CPU_LOCK_ACQUIRE:
1335 mutex_lock(&cache_chain_mutex);
1336 break;
1337 case CPU_UP_PREPARE: 1333 case CPU_UP_PREPARE:
1338 case CPU_UP_PREPARE_FROZEN: 1334 case CPU_UP_PREPARE_FROZEN:
1335 mutex_lock(&cache_chain_mutex);
1339 err = cpuup_prepare(cpu); 1336 err = cpuup_prepare(cpu);
1337 mutex_unlock(&cache_chain_mutex);
1340 break; 1338 break;
1341 case CPU_ONLINE: 1339 case CPU_ONLINE:
1342 case CPU_ONLINE_FROZEN: 1340 case CPU_ONLINE_FROZEN:
@@ -1373,9 +1371,8 @@ static int __cpuinit cpuup_callback(struct notifier_block *nfb,
1373#endif 1371#endif
1374 case CPU_UP_CANCELED: 1372 case CPU_UP_CANCELED:
1375 case CPU_UP_CANCELED_FROZEN: 1373 case CPU_UP_CANCELED_FROZEN:
1374 mutex_lock(&cache_chain_mutex);
1376 cpuup_canceled(cpu); 1375 cpuup_canceled(cpu);
1377 break;
1378 case CPU_LOCK_RELEASE:
1379 mutex_unlock(&cache_chain_mutex); 1376 mutex_unlock(&cache_chain_mutex);
1380 break; 1377 break;
1381 } 1378 }
@@ -2170,6 +2167,7 @@ kmem_cache_create (const char *name, size_t size, size_t align,
2170 * We use cache_chain_mutex to ensure a consistent view of 2167 * We use cache_chain_mutex to ensure a consistent view of
2171 * cpu_online_map as well. Please see cpuup_callback 2168 * cpu_online_map as well. Please see cpuup_callback
2172 */ 2169 */
2170 get_online_cpus();
2173 mutex_lock(&cache_chain_mutex); 2171 mutex_lock(&cache_chain_mutex);
2174 2172
2175 list_for_each_entry(pc, &cache_chain, next) { 2173 list_for_each_entry(pc, &cache_chain, next) {
@@ -2396,6 +2394,7 @@ oops:
2396 panic("kmem_cache_create(): failed to create slab `%s'\n", 2394 panic("kmem_cache_create(): failed to create slab `%s'\n",
2397 name); 2395 name);
2398 mutex_unlock(&cache_chain_mutex); 2396 mutex_unlock(&cache_chain_mutex);
2397 put_online_cpus();
2399 return cachep; 2398 return cachep;
2400} 2399}
2401EXPORT_SYMBOL(kmem_cache_create); 2400EXPORT_SYMBOL(kmem_cache_create);
@@ -2547,9 +2546,11 @@ int kmem_cache_shrink(struct kmem_cache *cachep)
2547 int ret; 2546 int ret;
2548 BUG_ON(!cachep || in_interrupt()); 2547 BUG_ON(!cachep || in_interrupt());
2549 2548
2549 get_online_cpus();
2550 mutex_lock(&cache_chain_mutex); 2550 mutex_lock(&cache_chain_mutex);
2551 ret = __cache_shrink(cachep); 2551 ret = __cache_shrink(cachep);
2552 mutex_unlock(&cache_chain_mutex); 2552 mutex_unlock(&cache_chain_mutex);
2553 put_online_cpus();
2553 return ret; 2554 return ret;
2554} 2555}
2555EXPORT_SYMBOL(kmem_cache_shrink); 2556EXPORT_SYMBOL(kmem_cache_shrink);
@@ -2575,6 +2576,7 @@ void kmem_cache_destroy(struct kmem_cache *cachep)
2575 BUG_ON(!cachep || in_interrupt()); 2576 BUG_ON(!cachep || in_interrupt());
2576 2577
2577 /* Find the cache in the chain of caches. */ 2578 /* Find the cache in the chain of caches. */
2579 get_online_cpus();
2578 mutex_lock(&cache_chain_mutex); 2580 mutex_lock(&cache_chain_mutex);
2579 /* 2581 /*
2580 * the chain is never empty, cache_cache is never destroyed 2582 * the chain is never empty, cache_cache is never destroyed
@@ -2584,6 +2586,7 @@ void kmem_cache_destroy(struct kmem_cache *cachep)
2584 slab_error(cachep, "Can't free all objects"); 2586 slab_error(cachep, "Can't free all objects");
2585 list_add(&cachep->next, &cache_chain); 2587 list_add(&cachep->next, &cache_chain);
2586 mutex_unlock(&cache_chain_mutex); 2588 mutex_unlock(&cache_chain_mutex);
2589 put_online_cpus();
2587 return; 2590 return;
2588 } 2591 }
2589 2592
@@ -2592,6 +2595,7 @@ void kmem_cache_destroy(struct kmem_cache *cachep)
2592 2595
2593 __kmem_cache_destroy(cachep); 2596 __kmem_cache_destroy(cachep);
2594 mutex_unlock(&cache_chain_mutex); 2597 mutex_unlock(&cache_chain_mutex);
2598 put_online_cpus();
2595} 2599}
2596EXPORT_SYMBOL(kmem_cache_destroy); 2600EXPORT_SYMBOL(kmem_cache_destroy);
2597 2601
diff --git a/net/core/flow.c b/net/core/flow.c
index 3ed2b4b1d6d4..6489f4e24ecf 100644
--- a/net/core/flow.c
+++ b/net/core/flow.c
@@ -293,7 +293,7 @@ void flow_cache_flush(void)
293 static DEFINE_MUTEX(flow_flush_sem); 293 static DEFINE_MUTEX(flow_flush_sem);
294 294
295 /* Don't want cpus going down or up during this. */ 295 /* Don't want cpus going down or up during this. */
296 lock_cpu_hotplug(); 296 get_online_cpus();
297 mutex_lock(&flow_flush_sem); 297 mutex_lock(&flow_flush_sem);
298 atomic_set(&info.cpuleft, num_online_cpus()); 298 atomic_set(&info.cpuleft, num_online_cpus());
299 init_completion(&info.completion); 299 init_completion(&info.completion);
@@ -305,7 +305,7 @@ void flow_cache_flush(void)
305 305
306 wait_for_completion(&info.completion); 306 wait_for_completion(&info.completion);
307 mutex_unlock(&flow_flush_sem); 307 mutex_unlock(&flow_flush_sem);
308 unlock_cpu_hotplug(); 308 put_online_cpus();
309} 309}
310 310
311static void __devinit flow_cache_cpu_prepare(int cpu) 311static void __devinit flow_cache_cpu_prepare(int cpu)
diff --git a/net/ipv4/arp.c b/net/ipv4/arp.c
index 08174a2aa878..54a76b8b803a 100644
--- a/net/ipv4/arp.c
+++ b/net/ipv4/arp.c
@@ -211,7 +211,7 @@ int arp_mc_map(__be32 addr, u8 *haddr, struct net_device *dev, int dir)
211 ip_tr_mc_map(addr, haddr); 211 ip_tr_mc_map(addr, haddr);
212 return 0; 212 return 0;
213 case ARPHRD_INFINIBAND: 213 case ARPHRD_INFINIBAND:
214 ip_ib_mc_map(addr, haddr); 214 ip_ib_mc_map(addr, dev->broadcast, haddr);
215 return 0; 215 return 0;
216 default: 216 default:
217 if (dir) { 217 if (dir) {
diff --git a/net/ipv6/ndisc.c b/net/ipv6/ndisc.c
index 777ed733b2d7..85947eae5bf7 100644
--- a/net/ipv6/ndisc.c
+++ b/net/ipv6/ndisc.c
@@ -337,7 +337,7 @@ int ndisc_mc_map(struct in6_addr *addr, char *buf, struct net_device *dev, int d
337 ipv6_arcnet_mc_map(addr, buf); 337 ipv6_arcnet_mc_map(addr, buf);
338 return 0; 338 return 0;
339 case ARPHRD_INFINIBAND: 339 case ARPHRD_INFINIBAND:
340 ipv6_ib_mc_map(addr, buf); 340 ipv6_ib_mc_map(addr, dev->broadcast, buf);
341 return 0; 341 return 0;
342 default: 342 default:
343 if (dir) { 343 if (dir) {