aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorJiri Kosina <jkosina@suse.cz>2010-08-11 03:36:51 -0400
committerJiri Kosina <jkosina@suse.cz>2010-08-11 03:36:51 -0400
commit6396fc3b3ff3f6b942992b653a62df11dcef9bea (patch)
treedb3c7cbe833b43c653adc99f70941431c5ff7c4e /drivers
parent4785879e4d340e24e54f6de2ccfc42728b912808 (diff)
parent3d30701b58970425e1d45994d6cb82f828924fdd (diff)
Merge branch 'master' into for-next
Conflicts: fs/exofs/inode.c
Diffstat (limited to 'drivers')
-rw-r--r--drivers/ata/libata-core.c2
-rw-r--r--drivers/ata/libata-scsi.c4
-rw-r--r--drivers/block/DAC960.c13
-rw-r--r--drivers/block/amiflop.c29
-rw-r--r--drivers/block/aoe/aoeblk.c6
-rw-r--r--drivers/block/ataflop.c32
-rw-r--r--drivers/block/brd.c9
-rw-r--r--drivers/block/cciss.c2165
-rw-r--r--drivers/block/cciss.h135
-rw-r--r--drivers/block/cciss_cmd.h36
-rw-r--r--drivers/block/cciss_scsi.c670
-rw-r--r--drivers/block/cpqarray.c78
-rw-r--r--drivers/block/drbd/drbd_actlog.c8
-rw-r--r--drivers/block/drbd/drbd_int.h16
-rw-r--r--drivers/block/drbd/drbd_main.c102
-rw-r--r--drivers/block/drbd/drbd_nl.c4
-rw-r--r--drivers/block/drbd/drbd_proc.c19
-rw-r--r--drivers/block/drbd/drbd_receiver.c135
-rw-r--r--drivers/block/drbd/drbd_req.c2
-rw-r--r--drivers/block/drbd/drbd_worker.c15
-rw-r--r--drivers/block/floppy.c182
-rw-r--r--drivers/block/hd.c2
-rw-r--r--drivers/block/loop.c9
-rw-r--r--drivers/block/mg_disk.c4
-rw-r--r--drivers/block/nbd.c7
-rw-r--r--drivers/block/osdblk.c15
-rw-r--r--drivers/block/paride/pcd.c21
-rw-r--r--drivers/block/paride/pd.c11
-rw-r--r--drivers/block/paride/pf.c26
-rw-r--r--drivers/block/pktcdvd.c20
-rw-r--r--drivers/block/ps3disk.c25
-rw-r--r--drivers/block/swim.c20
-rw-r--r--drivers/block/swim3.c32
-rw-r--r--drivers/block/ub.c35
-rw-r--r--drivers/block/umem.c2
-rw-r--r--drivers/block/viodasd.c21
-rw-r--r--drivers/block/virtio_blk.c88
-rw-r--r--drivers/block/xd.c19
-rw-r--r--drivers/block/xen-blkfront.c403
-rw-r--r--drivers/block/xsysace.c8
-rw-r--r--drivers/block/z2ram.c13
-rw-r--r--drivers/cdrom/cdrom.c46
-rw-r--r--drivers/cdrom/gdrom.c48
-rw-r--r--drivers/cdrom/viocd.c106
-rw-r--r--drivers/char/Makefile1
-rw-r--r--drivers/char/amiserial.c25
-rw-r--r--drivers/char/briq_panel.c6
-rw-r--r--drivers/char/cyclades.c22
-rw-r--r--drivers/char/epca.c4
-rw-r--r--drivers/char/ip2/ip2main.c4
-rw-r--r--drivers/char/isicom.c13
-rw-r--r--drivers/char/istallion.c68
-rw-r--r--drivers/char/keyboard.c10
-rw-r--r--drivers/char/mxser.c2
-rw-r--r--drivers/char/n_gsm.c1
-rw-r--r--drivers/char/n_hdlc.c16
-rw-r--r--drivers/char/n_r3964.c10
-rw-r--r--drivers/char/n_tty.c17
-rw-r--r--drivers/char/nozomi.c4
-rw-r--r--drivers/char/pty.c47
-rw-r--r--drivers/char/riscom8.c14
-rw-r--r--drivers/char/rocket.c28
-rw-r--r--drivers/char/selection.c13
-rw-r--r--drivers/char/serial167.c8
-rw-r--r--drivers/char/specialix.c13
-rw-r--r--drivers/char/stallion.c20
-rw-r--r--drivers/char/sx.c12
-rw-r--r--drivers/char/synclink.c21
-rw-r--r--drivers/char/synclink_gt.c92
-rw-r--r--drivers/char/synclinkmp.c43
-rw-r--r--drivers/char/tty_io.c150
-rw-r--r--drivers/char/tty_ioctl.c18
-rw-r--r--drivers/char/tty_ldisc.c43
-rw-r--r--drivers/char/tty_mutex.c47
-rw-r--r--drivers/char/tty_port.c4
-rw-r--r--drivers/char/vc_screen.c4
-rw-r--r--drivers/char/vt.c37
-rw-r--r--drivers/char/vt_ioctl.c17
-rw-r--r--drivers/gpu/drm/i915/intel_fb.c4
-rw-r--r--drivers/gpu/drm/nouveau/nouveau_fbcon.c1
-rw-r--r--drivers/gpu/drm/radeon/radeon_fb.c2
-rw-r--r--drivers/hid/hid-wacom.c49
-rw-r--r--drivers/ide/ide-atapi.c17
-rw-r--r--drivers/ide/ide-cd.c98
-rw-r--r--drivers/ide/ide-cd_ioctl.c2
-rw-r--r--drivers/ide/ide-disk.c18
-rw-r--r--drivers/ide/ide-disk_ioctl.c9
-rw-r--r--drivers/ide/ide-eh.c5
-rw-r--r--drivers/ide/ide-floppy.c27
-rw-r--r--drivers/ide/ide-floppy_ioctl.c12
-rw-r--r--drivers/ide/ide-gd.c19
-rw-r--r--drivers/ide/ide-io.c8
-rw-r--r--drivers/ide/ide-pm.c8
-rw-r--r--drivers/ide/ide-tape.c22
-rw-r--r--drivers/infiniband/hw/cxgb4/device.c9
-rw-r--r--drivers/infiniband/hw/cxgb4/resource.c7
-rw-r--r--drivers/infiniband/hw/cxgb4/t4.h2
-rw-r--r--drivers/input/evdev.c152
-rw-r--r--drivers/input/input.c46
-rw-r--r--drivers/input/joydev.c31
-rw-r--r--drivers/input/joystick/a3d.c3
-rw-r--r--drivers/input/joystick/adi.c2
-rw-r--r--drivers/input/joystick/amijoy.c4
-rw-r--r--drivers/input/joystick/gf2k.c20
-rw-r--r--drivers/input/joystick/interact.c14
-rw-r--r--drivers/input/joystick/sidewinder.c18
-rw-r--r--drivers/input/joystick/xpad.c1
-rw-r--r--drivers/input/keyboard/gpio_keys.c22
-rw-r--r--drivers/input/keyboard/hil_kbd.c21
-rw-r--r--drivers/input/misc/adxl34x.c1
-rw-r--r--drivers/input/misc/uinput.c29
-rw-r--r--drivers/input/mouse/elantech.c31
-rw-r--r--drivers/input/mouse/elantech.h7
-rw-r--r--drivers/input/mouse/pc110pad.c4
-rw-r--r--drivers/input/mouse/synaptics.c4
-rw-r--r--drivers/input/mousedev.c44
-rw-r--r--drivers/input/tablet/aiptek.c15
-rw-r--r--drivers/input/tablet/wacom_wac.c4
-rw-r--r--drivers/input/touchscreen/cy8ctmg110_ts.c6
-rw-r--r--drivers/md/Kconfig18
-rw-r--r--drivers/md/Makefile77
-rw-r--r--drivers/md/bitmap.c508
-rw-r--r--drivers/md/bitmap.h6
-rw-r--r--drivers/md/dm-io.c12
-rw-r--r--drivers/md/dm-kcopyd.c2
-rw-r--r--drivers/md/dm-raid1.c2
-rw-r--r--drivers/md/dm-stripe.c2
-rw-r--r--drivers/md/dm.c47
-rw-r--r--drivers/md/linear.c2
-rw-r--r--drivers/md/md.c302
-rw-r--r--drivers/md/md.h59
-rw-r--r--drivers/md/mktables.c132
-rw-r--r--drivers/md/multipath.c8
-rw-r--r--drivers/md/raid0.c2
-rw-r--r--drivers/md/raid1.c22
-rw-r--r--drivers/md/raid10.c30
-rw-r--r--drivers/md/raid5.c170
-rw-r--r--drivers/md/raid5.h9
-rw-r--r--drivers/md/raid6algos.c154
-rw-r--r--drivers/md/raid6altivec.uc130
-rw-r--r--drivers/md/raid6int.uc117
-rw-r--r--drivers/md/raid6mmx.c142
-rw-r--r--drivers/md/raid6recov.c132
-rw-r--r--drivers/md/raid6sse1.c162
-rw-r--r--drivers/md/raid6sse2.c262
-rw-r--r--drivers/md/raid6test/Makefile75
-rw-r--r--drivers/md/raid6test/test.c124
-rw-r--r--drivers/md/raid6x86.h61
-rw-r--r--drivers/md/unroll.awk20
-rw-r--r--drivers/media/IR/Kconfig36
-rw-r--r--drivers/media/IR/Makefile2
-rw-r--r--drivers/media/IR/ene_ir.c1023
-rw-r--r--drivers/media/IR/ene_ir.h235
-rw-r--r--drivers/media/IR/imon.c20
-rw-r--r--drivers/media/IR/ir-core-priv.h13
-rw-r--r--drivers/media/IR/ir-jvc-decoder.c14
-rw-r--r--drivers/media/IR/ir-keytable.c13
-rw-r--r--drivers/media/IR/ir-lirc-codec.c124
-rw-r--r--drivers/media/IR/ir-nec-decoder.c25
-rw-r--r--drivers/media/IR/ir-raw-event.c159
-rw-r--r--drivers/media/IR/ir-sysfs.c2
-rw-r--r--drivers/media/IR/keymaps/Makefile2
-rw-r--r--drivers/media/IR/keymaps/rc-empty.c44
-rw-r--r--drivers/media/IR/keymaps/rc-rc5-streamzap.c81
-rw-r--r--drivers/media/IR/keymaps/rc-rc6-mce.c2
-rw-r--r--drivers/media/IR/mceusb.c21
-rw-r--r--drivers/media/IR/rc-map.c23
-rw-r--r--drivers/media/IR/streamzap.c741
-rw-r--r--drivers/media/common/tuners/Kconfig2
-rw-r--r--drivers/media/dvb/bt8xx/dst.c10
-rw-r--r--drivers/media/dvb/frontends/Kconfig2
-rw-r--r--drivers/media/dvb/siano/smscoreapi.c6
-rw-r--r--drivers/media/video/Kconfig16
-rw-r--r--drivers/media/video/Makefile3
-rw-r--r--drivers/media/video/cs53l32a.c107
-rw-r--r--drivers/media/video/cx2341x.c747
-rw-r--r--drivers/media/video/cx23885/Kconfig2
-rw-r--r--drivers/media/video/cx23885/Makefile5
-rw-r--r--drivers/media/video/cx23885/cx23885-av.c35
-rw-r--r--drivers/media/video/cx23885/cx23885-av.h27
-rw-r--r--drivers/media/video/cx23885/cx23885-cards.c114
-rw-r--r--drivers/media/video/cx23885/cx23885-core.c124
-rw-r--r--drivers/media/video/cx23885/cx23885-i2c.c27
-rw-r--r--drivers/media/video/cx23885/cx23885-input.c72
-rw-r--r--drivers/media/video/cx23885/cx23885-ir.c24
-rw-r--r--drivers/media/video/cx23885/cx23885-reg.h1
-rw-r--r--drivers/media/video/cx23885/cx23885-vbi.c2
-rw-r--r--drivers/media/video/cx23885/cx23885-video.c23
-rw-r--r--drivers/media/video/cx23885/cx23885.h9
-rw-r--r--drivers/media/video/cx23885/cx23888-ir.c142
-rw-r--r--drivers/media/video/cx25840/Makefile2
-rw-r--r--drivers/media/video/cx25840/cx25840-audio.c144
-rw-r--r--drivers/media/video/cx25840/cx25840-core.c540
-rw-r--r--drivers/media/video/cx25840/cx25840-core.h52
-rw-r--r--drivers/media/video/cx25840/cx25840-ir.c1279
-rw-r--r--drivers/media/video/gspca/gspca.c21
-rw-r--r--drivers/media/video/gspca/sonixj.c10
-rw-r--r--drivers/media/video/gspca/sq930x.c347
-rw-r--r--drivers/media/video/gspca/t613.c4
-rw-r--r--drivers/media/video/gspca/vc032x.c360
-rw-r--r--drivers/media/video/gspca/zc3xx.c1715
-rw-r--r--drivers/media/video/ivtv/ivtv-controls.c276
-rw-r--r--drivers/media/video/ivtv/ivtv-controls.h6
-rw-r--r--drivers/media/video/ivtv/ivtv-driver.c26
-rw-r--r--drivers/media/video/ivtv/ivtv-driver.h4
-rw-r--r--drivers/media/video/ivtv/ivtv-fileops.c23
-rw-r--r--drivers/media/video/ivtv/ivtv-firmware.c6
-rw-r--r--drivers/media/video/ivtv/ivtv-gpio.c77
-rw-r--r--drivers/media/video/ivtv/ivtv-i2c.c7
-rw-r--r--drivers/media/video/ivtv/ivtv-ioctl.c31
-rw-r--r--drivers/media/video/ivtv/ivtv-streams.c24
-rw-r--r--drivers/media/video/msp3400-driver.c248
-rw-r--r--drivers/media/video/msp3400-driver.h18
-rw-r--r--drivers/media/video/msp3400-kthreads.c16
-rw-r--r--drivers/media/video/mt9m111.c40
-rw-r--r--drivers/media/video/mx2_camera.c4
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-debugifc.c14
-rw-r--r--drivers/media/video/s5p-fimc/Makefile3
-rw-r--r--drivers/media/video/s5p-fimc/fimc-core.c1586
-rw-r--r--drivers/media/video/s5p-fimc/fimc-core.h471
-rw-r--r--drivers/media/video/s5p-fimc/fimc-reg.c527
-rw-r--r--drivers/media/video/s5p-fimc/regs-fimc.h293
-rw-r--r--drivers/media/video/saa7115.c183
-rw-r--r--drivers/media/video/saa717x.c323
-rw-r--r--drivers/media/video/soc_camera.c9
-rw-r--r--drivers/media/video/tvp7002.c10
-rw-r--r--drivers/media/video/usbvideo/usbvideo.c12
-rw-r--r--drivers/media/video/uvc/uvc_driver.c9
-rw-r--r--drivers/media/video/uvc/uvc_queue.c13
-rw-r--r--drivers/media/video/uvc/uvc_video.c19
-rw-r--r--drivers/media/video/uvc/uvcvideo.h5
-rw-r--r--drivers/media/video/v4l2-common.c479
-rw-r--r--drivers/media/video/v4l2-ctrls.c1851
-rw-r--r--drivers/media/video/v4l2-dev.c60
-rw-r--r--drivers/media/video/v4l2-device.c7
-rw-r--r--drivers/media/video/v4l2-ioctl.c46
-rw-r--r--drivers/media/video/wm8739.c179
-rw-r--r--drivers/media/video/wm8775.c79
-rw-r--r--drivers/memstick/core/mspro_block.c12
-rw-r--r--drivers/message/i2o/i2o_block.c30
-rw-r--r--drivers/mmc/card/block.c5
-rw-r--r--drivers/mmc/card/queue.c4
-rw-r--r--drivers/mtd/Kconfig12
-rw-r--r--drivers/mtd/afs.c2
-rw-r--r--drivers/mtd/chips/cfi_cmdset_0001.c31
-rw-r--r--drivers/mtd/chips/cfi_cmdset_0002.c17
-rw-r--r--drivers/mtd/chips/cfi_cmdset_0020.c1
-rw-r--r--drivers/mtd/chips/cfi_probe.c4
-rw-r--r--drivers/mtd/chips/cfi_util.c1
-rw-r--r--drivers/mtd/chips/chipreg.c1
-rw-r--r--drivers/mtd/chips/map_absent.c1
-rw-r--r--drivers/mtd/chips/map_ram.c1
-rw-r--r--drivers/mtd/chips/map_rom.c1
-rw-r--r--drivers/mtd/cmdlinepart.c17
-rw-r--r--drivers/mtd/devices/docecc.c1
-rw-r--r--drivers/mtd/devices/docprobe.c1
-rw-r--r--drivers/mtd/devices/m25p80.c43
-rw-r--r--drivers/mtd/devices/mtd_dataflash.c12
-rw-r--r--drivers/mtd/devices/mtdram.c1
-rw-r--r--drivers/mtd/devices/pmc551.c1
-rw-r--r--drivers/mtd/devices/sst25l.c2
-rw-r--r--drivers/mtd/ftl.c2
-rw-r--r--drivers/mtd/inftlcore.c6
-rw-r--r--drivers/mtd/inftlmount.c5
-rw-r--r--drivers/mtd/lpddr/lpddr_cmds.c20
-rw-r--r--drivers/mtd/maps/Kconfig8
-rw-r--r--drivers/mtd/maps/Makefile1
-rw-r--r--drivers/mtd/maps/ixp4xx.c35
-rw-r--r--drivers/mtd/maps/physmap.c14
-rw-r--r--drivers/mtd/maps/physmap_of.c6
-rw-r--r--drivers/mtd/maps/redwood.c131
-rw-r--r--drivers/mtd/mtd_blkdevs.c41
-rw-r--r--drivers/mtd/mtdblock.c19
-rw-r--r--drivers/mtd/mtdblock_ro.c19
-rw-r--r--drivers/mtd/mtdchar.c59
-rw-r--r--drivers/mtd/mtdconcat.c38
-rw-r--r--drivers/mtd/mtdcore.c21
-rw-r--r--drivers/mtd/mtdoops.c2
-rw-r--r--drivers/mtd/mtdpart.c31
-rw-r--r--drivers/mtd/mtdsuper.c2
-rw-r--r--drivers/mtd/nand/Kconfig33
-rw-r--r--drivers/mtd/nand/atmel_nand.c2
-rw-r--r--drivers/mtd/nand/bf5xx_nand.c117
-rw-r--r--drivers/mtd/nand/davinci_nand.c17
-rw-r--r--drivers/mtd/nand/denali.c1240
-rw-r--r--drivers/mtd/nand/denali.h140
-rw-r--r--drivers/mtd/nand/diskonchip.c6
-rw-r--r--drivers/mtd/nand/mxc_nand.c600
-rw-r--r--drivers/mtd/nand/nand_base.c79
-rw-r--r--drivers/mtd/nand/nand_bbt.c103
-rw-r--r--drivers/mtd/nand/nand_ids.c4
-rw-r--r--drivers/mtd/nand/nandsim.c14
-rw-r--r--drivers/mtd/nand/plat_nand.c2
-rw-r--r--drivers/mtd/nand/r852.c6
-rw-r--r--drivers/mtd/nand/rtc_from4.c1
-rw-r--r--drivers/mtd/nand/s3c2410.c15
-rw-r--r--drivers/mtd/nand/sm_common.c2
-rw-r--r--drivers/mtd/nftlcore.c25
-rw-r--r--drivers/mtd/nftlmount.c3
-rw-r--r--drivers/mtd/ofpart.c4
-rw-r--r--drivers/mtd/onenand/Kconfig4
-rw-r--r--drivers/mtd/onenand/onenand_base.c49
-rw-r--r--drivers/mtd/onenand/onenand_bbt.c1
-rw-r--r--drivers/mtd/onenand/samsung.c21
-rw-r--r--drivers/mtd/redboot.c18
-rw-r--r--drivers/mtd/rfd_ftl.c2
-rw-r--r--drivers/mtd/ssfdc.c2
-rw-r--r--drivers/mtd/tests/mtd_pagetest.c9
-rw-r--r--drivers/power/olpc_battery.c1
-rw-r--r--drivers/s390/block/dasd.c8
-rw-r--r--drivers/s390/block/dasd_devmap.c44
-rw-r--r--drivers/s390/block/dasd_diag.c6
-rw-r--r--drivers/s390/block/dasd_eckd.c94
-rw-r--r--drivers/s390/block/dasd_eckd.h7
-rw-r--r--drivers/s390/block/dasd_fba.c4
-rw-r--r--drivers/s390/block/dasd_int.h8
-rw-r--r--drivers/s390/block/dcssblk.c5
-rw-r--r--drivers/s390/char/tape_block.c8
-rw-r--r--drivers/s390/cio/ccwreq.c16
-rw-r--r--drivers/s390/cio/chsc.c48
-rw-r--r--drivers/s390/cio/chsc.h2
-rw-r--r--drivers/s390/cio/device.c47
-rw-r--r--drivers/s390/cio/device_pgid.c3
-rw-r--r--drivers/s390/cio/io_sch.h10
-rw-r--r--drivers/s390/net/smsgiucv_app.c7
-rw-r--r--drivers/scsi/aha1542.c25
-rw-r--r--drivers/scsi/osd/osd_initiator.c8
-rw-r--r--drivers/scsi/scsi_error.c12
-rw-r--r--drivers/scsi/scsi_lib.c14
-rw-r--r--drivers/scsi/sd.c126
-rw-r--r--drivers/scsi/sd.h2
-rw-r--r--drivers/scsi/sr.c25
-rw-r--r--drivers/scsi/sun3_NCR5380.c2
-rw-r--r--drivers/scsi/sun3_scsi.c2
-rw-r--r--drivers/scsi/sun3_scsi_vme.c2
-rw-r--r--drivers/serial/21285.c10
-rw-r--r--drivers/serial/68328serial.c26
-rw-r--r--drivers/serial/68360serial.c4
-rw-r--r--drivers/serial/8250.c46
-rw-r--r--drivers/serial/8250_early.c57
-rw-r--r--drivers/serial/8250_pci.c13
-rw-r--r--drivers/serial/Kconfig44
-rw-r--r--drivers/serial/Makefile4
-rw-r--r--drivers/serial/altera_uart.c2
-rw-r--r--drivers/serial/atmel_serial.c11
-rw-r--r--drivers/serial/bfin_5xx.c7
-rw-r--r--drivers/serial/crisv10.c12
-rw-r--r--drivers/serial/imx.c10
-rw-r--r--drivers/serial/ioc3_serial.c9
-rw-r--r--drivers/serial/ioc4_serial.c9
-rw-r--r--drivers/serial/max3100.c7
-rw-r--r--drivers/serial/max3107-aava.c344
-rw-r--r--drivers/serial/max3107.c1197
-rw-r--r--drivers/serial/max3107.h441
-rw-r--r--drivers/serial/mcf.c31
-rw-r--r--drivers/serial/mfd.c1498
-rw-r--r--drivers/serial/mrst_max3110.c844
-rw-r--r--drivers/serial/mrst_max3110.h59
-rw-r--r--drivers/serial/serial_core.c288
-rw-r--r--drivers/serial/timbuart.c6
-rw-r--r--drivers/staging/easycap/easycap.h8
-rw-r--r--drivers/staging/easycap/easycap_ioctl.c52
-rw-r--r--drivers/staging/easycap/easycap_main.c38
-rw-r--r--drivers/staging/hv/blkvsc_drv.c13
-rw-r--r--drivers/staging/lirc/Kconfig29
-rw-r--r--drivers/staging/lirc/Makefile2
-rw-r--r--drivers/staging/lirc/lirc_ene0100.c646
-rw-r--r--drivers/staging/lirc/lirc_it87.c9
-rw-r--r--drivers/staging/lirc/lirc_parallel.c4
-rw-r--r--drivers/staging/lirc/lirc_streamzap.c821
-rw-r--r--drivers/staging/pohmelfs/inode.c18
-rw-r--r--drivers/staging/usbip/vhci_hcd.c6
-rw-r--r--drivers/usb/Makefile2
-rw-r--r--drivers/usb/atm/cxacru.c11
-rw-r--r--drivers/usb/atm/speedtch.c10
-rw-r--r--drivers/usb/atm/ueagle-atm.c5
-rw-r--r--drivers/usb/atm/usbatm.c23
-rw-r--r--drivers/usb/atm/usbatm.h22
-rw-r--r--drivers/usb/atm/xusbatm.c10
-rw-r--r--drivers/usb/c67x00/c67x00-hcd.c4
-rw-r--r--drivers/usb/class/cdc-acm.c12
-rw-r--r--drivers/usb/class/usblp.c371
-rw-r--r--drivers/usb/core/devio.c7
-rw-r--r--drivers/usb/core/driver.c11
-rw-r--r--drivers/usb/core/endpoint.c9
-rw-r--r--drivers/usb/core/generic.c4
-rw-r--r--drivers/usb/core/hcd-pci.c202
-rw-r--r--drivers/usb/core/hcd.c79
-rw-r--r--drivers/usb/core/hub.c13
-rw-r--r--drivers/usb/core/inode.c4
-rw-r--r--drivers/usb/core/quirks.c3
-rw-r--r--drivers/usb/core/urb.c50
-rw-r--r--drivers/usb/core/usb.c6
-rw-r--r--drivers/usb/gadget/Kconfig52
-rw-r--r--drivers/usb/gadget/Makefile3
-rw-r--r--drivers/usb/gadget/audio.c4
-rw-r--r--drivers/usb/gadget/cdc2.c4
-rw-r--r--drivers/usb/gadget/composite.c73
-rw-r--r--drivers/usb/gadget/dbgp.c434
-rw-r--r--drivers/usb/gadget/dummy_hcd.c6
-rw-r--r--drivers/usb/gadget/ether.c6
-rw-r--r--drivers/usb/gadget/f_fs.c38
-rw-r--r--drivers/usb/gadget/f_hid.c6
-rw-r--r--drivers/usb/gadget/f_loopback.c4
-rw-r--r--drivers/usb/gadget/f_mass_storage.c125
-rw-r--r--drivers/usb/gadget/f_sourcesink.c2
-rw-r--r--drivers/usb/gadget/file_storage.c104
-rw-r--r--drivers/usb/gadget/g_ffs.c176
-rw-r--r--drivers/usb/gadget/gmidi.c2
-rw-r--r--drivers/usb/gadget/hid.c4
-rw-r--r--drivers/usb/gadget/inode.c16
-rw-r--r--drivers/usb/gadget/langwell_udc.c6
-rw-r--r--drivers/usb/gadget/mass_storage.c24
-rw-r--r--drivers/usb/gadget/multi.c262
-rw-r--r--drivers/usb/gadget/printer.c9
-rw-r--r--drivers/usb/gadget/s3c-hsotg.c153
-rw-r--r--drivers/usb/gadget/serial.c4
-rw-r--r--drivers/usb/gadget/storage_common.c105
-rw-r--r--drivers/usb/gadget/u_ether.c15
-rw-r--r--drivers/usb/gadget/u_serial.c1
-rw-r--r--drivers/usb/gadget/webcam.c4
-rw-r--r--drivers/usb/gadget/zero.c2
-rw-r--r--drivers/usb/host/Kconfig11
-rw-r--r--drivers/usb/host/ehci-au1xxx.c2
-rw-r--r--drivers/usb/host/ehci-dbg.c196
-rw-r--r--drivers/usb/host/ehci-fsl.c3
-rw-r--r--drivers/usb/host/ehci-hcd.c49
-rw-r--r--drivers/usb/host/ehci-hub.c25
-rw-r--r--drivers/usb/host/ehci-lpm.c83
-rw-r--r--drivers/usb/host/ehci-omap.c36
-rw-r--r--drivers/usb/host/ehci-pci.c26
-rw-r--r--drivers/usb/host/ehci-q.c3
-rw-r--r--drivers/usb/host/ehci-sched.c182
-rw-r--r--drivers/usb/host/ehci.h18
-rw-r--r--drivers/usb/host/hwa-hc.c4
-rw-r--r--drivers/usb/host/imx21-hcd.c2
-rw-r--r--drivers/usb/host/isp1362.h24
-rw-r--r--drivers/usb/host/isp1760-hcd.c3
-rw-r--r--drivers/usb/host/ohci-dbg.c4
-rw-r--r--drivers/usb/host/ohci-hcd.c6
-rw-r--r--drivers/usb/host/ohci-hub.c23
-rw-r--r--drivers/usb/host/ohci-pci.c2
-rw-r--r--drivers/usb/host/ohci-ssb.c52
-rw-r--r--drivers/usb/host/oxu210hp-hcd.c7
-rw-r--r--drivers/usb/host/sl811-hcd.c5
-rw-r--r--drivers/usb/host/uhci-debug.c23
-rw-r--r--drivers/usb/host/uhci-hcd.c87
-rw-r--r--drivers/usb/host/uhci-hcd.h7
-rw-r--r--drivers/usb/host/uhci-hub.c6
-rw-r--r--drivers/usb/host/uhci-q.c4
-rw-r--r--drivers/usb/host/whci/hcd.c2
-rw-r--r--drivers/usb/host/whci/qset.c2
-rw-r--r--drivers/usb/host/xhci-mem.c101
-rw-r--r--drivers/usb/host/xhci-pci.c9
-rw-r--r--drivers/usb/host/xhci-ring.c1332
-rw-r--r--drivers/usb/host/xhci.c344
-rw-r--r--drivers/usb/host/xhci.h30
-rw-r--r--drivers/usb/misc/ftdi-elan.c4
-rw-r--r--drivers/usb/misc/iowarrior.c23
-rw-r--r--drivers/usb/misc/legousbtower.c6
-rw-r--r--drivers/usb/misc/rio500.c15
-rw-r--r--drivers/usb/misc/sisusbvga/sisusb.c10
-rw-r--r--drivers/usb/misc/usblcd.c24
-rw-r--r--drivers/usb/misc/usbtest.c14
-rw-r--r--drivers/usb/mon/mon_bin.c24
-rw-r--r--drivers/usb/musb/musb_core.c7
-rw-r--r--drivers/usb/musb/musb_debugfs.c32
-rw-r--r--drivers/usb/musb/musb_gadget_ep0.c3
-rw-r--r--drivers/usb/musb/musb_virthub.c2
-rw-r--r--drivers/usb/musb/musbhsdma.c5
-rw-r--r--drivers/usb/musb/omap2430.c6
-rw-r--r--drivers/usb/otg/Kconfig2
-rw-r--r--drivers/usb/otg/ulpi.c134
-rw-r--r--drivers/usb/serial/Kconfig9
-rw-r--r--drivers/usb/serial/Makefile1
-rw-r--r--drivers/usb/serial/cp210x.c4
-rw-r--r--drivers/usb/serial/digi_acceleport.c14
-rw-r--r--drivers/usb/serial/ftdi_sio.c4
-rw-r--r--drivers/usb/serial/ftdi_sio_ids.h10
-rw-r--r--drivers/usb/serial/generic.c15
-rw-r--r--drivers/usb/serial/io_ti.c4
-rw-r--r--drivers/usb/serial/ipaq.c1
-rw-r--r--drivers/usb/serial/iuu_phoenix.c54
-rw-r--r--drivers/usb/serial/option.c17
-rw-r--r--drivers/usb/serial/ssu100.c698
-rw-r--r--drivers/usb/serial/usb-serial.c32
-rw-r--r--drivers/usb/storage/freecom.c23
-rw-r--r--drivers/usb/storage/isd200.c3
-rw-r--r--drivers/usb/storage/usb.c4
-rw-r--r--drivers/usb/usb-skeleton.c8
-rw-r--r--drivers/video/console/fbcon.c4
-rw-r--r--drivers/video/console/vgacon.c2
-rw-r--r--drivers/watchdog/Kconfig18
-rw-r--r--drivers/watchdog/Makefile2
-rw-r--r--drivers/watchdog/f71808e_wdt.c768
-rw-r--r--drivers/watchdog/hpwdt.c4
-rw-r--r--drivers/watchdog/s3c2410_wdt.c17
-rw-r--r--drivers/watchdog/sch311x_wdt.c4
-rw-r--r--drivers/watchdog/sp805_wdt.c387
-rw-r--r--drivers/watchdog/wdt_pci.c15
-rw-r--r--drivers/xen/xenbus/xenbus_client.c90
-rw-r--r--drivers/zorro/proc.c17
502 files changed, 29103 insertions, 13595 deletions
diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index 4972fdf4bd3..7ef7c4f216f 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -4281,7 +4281,7 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
4281 * The special characters ?, [, -, or *, can be matched using a set, eg. [*] 4281 * The special characters ?, [, -, or *, can be matched using a set, eg. [*]
4282 * Behaviour with malformed patterns is undefined, though generally reasonable. 4282 * Behaviour with malformed patterns is undefined, though generally reasonable.
4283 * 4283 *
4284 * Example patterns: "SD1?", "SD1[0-5]", "*R0", SD*1?[012]*xx" 4284 * Sample patterns: "SD1?", "SD1[0-5]", "*R0", "SD*1?[012]*xx"
4285 * 4285 *
4286 * This function uses one level of recursion per '*' in pattern. 4286 * This function uses one level of recursion per '*' in pattern.
4287 * Since it calls _nothing_ else, and has _no_ explicit local variables, 4287 * Since it calls _nothing_ else, and has _no_ explicit local variables,
diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
index d75c9c479d1..a89172c100f 100644
--- a/drivers/ata/libata-scsi.c
+++ b/drivers/ata/libata-scsi.c
@@ -1111,10 +1111,10 @@ static void ata_scsi_sdev_config(struct scsi_device *sdev)
1111 */ 1111 */
1112static int atapi_drain_needed(struct request *rq) 1112static int atapi_drain_needed(struct request *rq)
1113{ 1113{
1114 if (likely(!blk_pc_request(rq))) 1114 if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
1115 return 0; 1115 return 0;
1116 1116
1117 if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_RW)) 1117 if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE))
1118 return 0; 1118 return 0;
1119 1119
1120 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC; 1120 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
diff --git a/drivers/block/DAC960.c b/drivers/block/DAC960.c
index c5f22bb0a48..4e2c367fec1 100644
--- a/drivers/block/DAC960.c
+++ b/drivers/block/DAC960.c
@@ -79,23 +79,28 @@ static int DAC960_open(struct block_device *bdev, fmode_t mode)
79 struct gendisk *disk = bdev->bd_disk; 79 struct gendisk *disk = bdev->bd_disk;
80 DAC960_Controller_T *p = disk->queue->queuedata; 80 DAC960_Controller_T *p = disk->queue->queuedata;
81 int drive_nr = (long)disk->private_data; 81 int drive_nr = (long)disk->private_data;
82 int ret = -ENXIO;
82 83
84 lock_kernel();
83 if (p->FirmwareType == DAC960_V1_Controller) { 85 if (p->FirmwareType == DAC960_V1_Controller) {
84 if (p->V1.LogicalDriveInformation[drive_nr]. 86 if (p->V1.LogicalDriveInformation[drive_nr].
85 LogicalDriveState == DAC960_V1_LogicalDrive_Offline) 87 LogicalDriveState == DAC960_V1_LogicalDrive_Offline)
86 return -ENXIO; 88 goto out;
87 } else { 89 } else {
88 DAC960_V2_LogicalDeviceInfo_T *i = 90 DAC960_V2_LogicalDeviceInfo_T *i =
89 p->V2.LogicalDeviceInformation[drive_nr]; 91 p->V2.LogicalDeviceInformation[drive_nr];
90 if (!i || i->LogicalDeviceState == DAC960_V2_LogicalDevice_Offline) 92 if (!i || i->LogicalDeviceState == DAC960_V2_LogicalDevice_Offline)
91 return -ENXIO; 93 goto out;
92 } 94 }
93 95
94 check_disk_change(bdev); 96 check_disk_change(bdev);
95 97
96 if (!get_capacity(p->disks[drive_nr])) 98 if (!get_capacity(p->disks[drive_nr]))
97 return -ENXIO; 99 goto out;
98 return 0; 100 ret = 0;
101out:
102 unlock_kernel();
103 return ret;
99} 104}
100 105
101static int DAC960_getgeo(struct block_device *bdev, struct hd_geometry *geo) 106static int DAC960_getgeo(struct block_device *bdev, struct hd_geometry *geo)
diff --git a/drivers/block/amiflop.c b/drivers/block/amiflop.c
index 832798aa14f..76f114f0bba 100644
--- a/drivers/block/amiflop.c
+++ b/drivers/block/amiflop.c
@@ -60,6 +60,7 @@
60#include <linux/hdreg.h> 60#include <linux/hdreg.h>
61#include <linux/delay.h> 61#include <linux/delay.h>
62#include <linux/init.h> 62#include <linux/init.h>
63#include <linux/smp_lock.h>
63#include <linux/amifdreg.h> 64#include <linux/amifdreg.h>
64#include <linux/amifd.h> 65#include <linux/amifd.h>
65#include <linux/buffer_head.h> 66#include <linux/buffer_head.h>
@@ -1423,7 +1424,7 @@ static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1423 return 0; 1424 return 0;
1424} 1425}
1425 1426
1426static int fd_ioctl(struct block_device *bdev, fmode_t mode, 1427static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1427 unsigned int cmd, unsigned long param) 1428 unsigned int cmd, unsigned long param)
1428{ 1429{
1429 struct amiga_floppy_struct *p = bdev->bd_disk->private_data; 1430 struct amiga_floppy_struct *p = bdev->bd_disk->private_data;
@@ -1500,6 +1501,18 @@ static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1500 return 0; 1501 return 0;
1501} 1502}
1502 1503
1504static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1505 unsigned int cmd, unsigned long param)
1506{
1507 int ret;
1508
1509 lock_kernel();
1510 ret = fd_locked_ioctl(bdev, mode, cmd, param);
1511 unlock_kernel();
1512
1513 return ret;
1514}
1515
1503static void fd_probe(int dev) 1516static void fd_probe(int dev)
1504{ 1517{
1505 unsigned long code; 1518 unsigned long code;
@@ -1542,10 +1555,13 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
1542 int old_dev; 1555 int old_dev;
1543 unsigned long flags; 1556 unsigned long flags;
1544 1557
1558 lock_kernel();
1545 old_dev = fd_device[drive]; 1559 old_dev = fd_device[drive];
1546 1560
1547 if (fd_ref[drive] && old_dev != system) 1561 if (fd_ref[drive] && old_dev != system) {
1562 unlock_kernel();
1548 return -EBUSY; 1563 return -EBUSY;
1564 }
1549 1565
1550 if (mode & (FMODE_READ|FMODE_WRITE)) { 1566 if (mode & (FMODE_READ|FMODE_WRITE)) {
1551 check_disk_change(bdev); 1567 check_disk_change(bdev);
@@ -1558,8 +1574,10 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
1558 fd_deselect (drive); 1574 fd_deselect (drive);
1559 rel_fdc(); 1575 rel_fdc();
1560 1576
1561 if (wrprot) 1577 if (wrprot) {
1578 unlock_kernel();
1562 return -EROFS; 1579 return -EROFS;
1580 }
1563 } 1581 }
1564 } 1582 }
1565 1583
@@ -1576,6 +1594,7 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
1576 printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive, 1594 printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
1577 unit[drive].type->name, data_types[system].name); 1595 unit[drive].type->name, data_types[system].name);
1578 1596
1597 unlock_kernel();
1579 return 0; 1598 return 0;
1580} 1599}
1581 1600
@@ -1584,6 +1603,7 @@ static int floppy_release(struct gendisk *disk, fmode_t mode)
1584 struct amiga_floppy_struct *p = disk->private_data; 1603 struct amiga_floppy_struct *p = disk->private_data;
1585 int drive = p - unit; 1604 int drive = p - unit;
1586 1605
1606 lock_kernel();
1587 if (unit[drive].dirty == 1) { 1607 if (unit[drive].dirty == 1) {
1588 del_timer (flush_track_timer + drive); 1608 del_timer (flush_track_timer + drive);
1589 non_int_flush_track (drive); 1609 non_int_flush_track (drive);
@@ -1597,6 +1617,7 @@ static int floppy_release(struct gendisk *disk, fmode_t mode)
1597/* the mod_use counter is handled this way */ 1617/* the mod_use counter is handled this way */
1598 floppy_off (drive | 0x40000000); 1618 floppy_off (drive | 0x40000000);
1599#endif 1619#endif
1620 unlock_kernel();
1600 return 0; 1621 return 0;
1601} 1622}
1602 1623
@@ -1638,7 +1659,7 @@ static const struct block_device_operations floppy_fops = {
1638 .owner = THIS_MODULE, 1659 .owner = THIS_MODULE,
1639 .open = floppy_open, 1660 .open = floppy_open,
1640 .release = floppy_release, 1661 .release = floppy_release,
1641 .locked_ioctl = fd_ioctl, 1662 .ioctl = fd_ioctl,
1642 .getgeo = fd_getgeo, 1663 .getgeo = fd_getgeo,
1643 .media_changed = amiga_floppy_change, 1664 .media_changed = amiga_floppy_change,
1644}; 1665};
diff --git a/drivers/block/aoe/aoeblk.c b/drivers/block/aoe/aoeblk.c
index 035cefe4045..a946929735a 100644
--- a/drivers/block/aoe/aoeblk.c
+++ b/drivers/block/aoe/aoeblk.c
@@ -12,6 +12,7 @@
12#include <linux/slab.h> 12#include <linux/slab.h>
13#include <linux/genhd.h> 13#include <linux/genhd.h>
14#include <linux/netdevice.h> 14#include <linux/netdevice.h>
15#include <linux/smp_lock.h>
15#include "aoe.h" 16#include "aoe.h"
16 17
17static struct kmem_cache *buf_pool_cache; 18static struct kmem_cache *buf_pool_cache;
@@ -124,13 +125,16 @@ aoeblk_open(struct block_device *bdev, fmode_t mode)
124 struct aoedev *d = bdev->bd_disk->private_data; 125 struct aoedev *d = bdev->bd_disk->private_data;
125 ulong flags; 126 ulong flags;
126 127
128 lock_kernel();
127 spin_lock_irqsave(&d->lock, flags); 129 spin_lock_irqsave(&d->lock, flags);
128 if (d->flags & DEVFL_UP) { 130 if (d->flags & DEVFL_UP) {
129 d->nopen++; 131 d->nopen++;
130 spin_unlock_irqrestore(&d->lock, flags); 132 spin_unlock_irqrestore(&d->lock, flags);
133 unlock_kernel();
131 return 0; 134 return 0;
132 } 135 }
133 spin_unlock_irqrestore(&d->lock, flags); 136 spin_unlock_irqrestore(&d->lock, flags);
137 unlock_kernel();
134 return -ENODEV; 138 return -ENODEV;
135} 139}
136 140
@@ -173,7 +177,7 @@ aoeblk_make_request(struct request_queue *q, struct bio *bio)
173 BUG(); 177 BUG();
174 bio_endio(bio, -ENXIO); 178 bio_endio(bio, -ENXIO);
175 return 0; 179 return 0;
176 } else if (bio_rw_flagged(bio, BIO_RW_BARRIER)) { 180 } else if (bio->bi_rw & REQ_HARDBARRIER) {
177 bio_endio(bio, -EOPNOTSUPP); 181 bio_endio(bio, -EOPNOTSUPP);
178 return 0; 182 return 0;
179 } else if (bio->bi_io_vec == NULL) { 183 } else if (bio->bi_io_vec == NULL) {
diff --git a/drivers/block/ataflop.c b/drivers/block/ataflop.c
index e35cf59cbfd..aceb9647652 100644
--- a/drivers/block/ataflop.c
+++ b/drivers/block/ataflop.c
@@ -67,6 +67,7 @@
67#include <linux/delay.h> 67#include <linux/delay.h>
68#include <linux/init.h> 68#include <linux/init.h>
69#include <linux/blkdev.h> 69#include <linux/blkdev.h>
70#include <linux/smp_lock.h>
70 71
71#include <asm/atafd.h> 72#include <asm/atafd.h>
72#include <asm/atafdreg.h> 73#include <asm/atafdreg.h>
@@ -359,7 +360,7 @@ static void finish_fdc( void );
359static void finish_fdc_done( int dummy ); 360static void finish_fdc_done( int dummy );
360static void setup_req_params( int drive ); 361static void setup_req_params( int drive );
361static void redo_fd_request( void); 362static void redo_fd_request( void);
362static int fd_ioctl(struct block_device *bdev, fmode_t mode, unsigned int 363static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
363 cmd, unsigned long param); 364 cmd, unsigned long param);
364static void fd_probe( int drive ); 365static void fd_probe( int drive );
365static int fd_test_drive_present( int drive ); 366static int fd_test_drive_present( int drive );
@@ -1480,7 +1481,7 @@ void do_fd_request(struct request_queue * q)
1480 atari_enable_irq( IRQ_MFP_FDC ); 1481 atari_enable_irq( IRQ_MFP_FDC );
1481} 1482}
1482 1483
1483static int fd_ioctl(struct block_device *bdev, fmode_t mode, 1484static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1484 unsigned int cmd, unsigned long param) 1485 unsigned int cmd, unsigned long param)
1485{ 1486{
1486 struct gendisk *disk = bdev->bd_disk; 1487 struct gendisk *disk = bdev->bd_disk;
@@ -1665,6 +1666,17 @@ static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1665 } 1666 }
1666} 1667}
1667 1668
1669static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1670 unsigned int cmd, unsigned long arg)
1671{
1672 int ret;
1673
1674 lock_kernel();
1675 ret = fd_locked_ioctl(bdev, mode, cmd, arg);
1676 unlock_kernel();
1677
1678 return ret;
1679}
1668 1680
1669/* Initialize the 'unit' variable for drive 'drive' */ 1681/* Initialize the 'unit' variable for drive 'drive' */
1670 1682
@@ -1838,24 +1850,36 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
1838 return 0; 1850 return 0;
1839} 1851}
1840 1852
1853static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
1854{
1855 int ret;
1856
1857 lock_kernel();
1858 ret = floppy_open(bdev, mode);
1859 unlock_kernel();
1860
1861 return ret;
1862}
1841 1863
1842static int floppy_release(struct gendisk *disk, fmode_t mode) 1864static int floppy_release(struct gendisk *disk, fmode_t mode)
1843{ 1865{
1844 struct atari_floppy_struct *p = disk->private_data; 1866 struct atari_floppy_struct *p = disk->private_data;
1867 lock_kernel();
1845 if (p->ref < 0) 1868 if (p->ref < 0)
1846 p->ref = 0; 1869 p->ref = 0;
1847 else if (!p->ref--) { 1870 else if (!p->ref--) {
1848 printk(KERN_ERR "floppy_release with fd_ref == 0"); 1871 printk(KERN_ERR "floppy_release with fd_ref == 0");
1849 p->ref = 0; 1872 p->ref = 0;
1850 } 1873 }
1874 unlock_kernel();
1851 return 0; 1875 return 0;
1852} 1876}
1853 1877
1854static const struct block_device_operations floppy_fops = { 1878static const struct block_device_operations floppy_fops = {
1855 .owner = THIS_MODULE, 1879 .owner = THIS_MODULE,
1856 .open = floppy_open, 1880 .open = floppy_unlocked_open,
1857 .release = floppy_release, 1881 .release = floppy_release,
1858 .locked_ioctl = fd_ioctl, 1882 .ioctl = fd_ioctl,
1859 .media_changed = check_floppy_change, 1883 .media_changed = check_floppy_change,
1860 .revalidate_disk= floppy_revalidate, 1884 .revalidate_disk= floppy_revalidate,
1861}; 1885};
diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index f1bf79d9bc0..1c7f63792ff 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -15,6 +15,7 @@
15#include <linux/blkdev.h> 15#include <linux/blkdev.h>
16#include <linux/bio.h> 16#include <linux/bio.h>
17#include <linux/highmem.h> 17#include <linux/highmem.h>
18#include <linux/smp_lock.h>
18#include <linux/radix-tree.h> 19#include <linux/radix-tree.h>
19#include <linux/buffer_head.h> /* invalidate_bh_lrus() */ 20#include <linux/buffer_head.h> /* invalidate_bh_lrus() */
20#include <linux/slab.h> 21#include <linux/slab.h>
@@ -340,7 +341,7 @@ static int brd_make_request(struct request_queue *q, struct bio *bio)
340 get_capacity(bdev->bd_disk)) 341 get_capacity(bdev->bd_disk))
341 goto out; 342 goto out;
342 343
343 if (unlikely(bio_rw_flagged(bio, BIO_RW_DISCARD))) { 344 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
344 err = 0; 345 err = 0;
345 discard_from_brd(brd, sector, bio->bi_size); 346 discard_from_brd(brd, sector, bio->bi_size);
346 goto out; 347 goto out;
@@ -401,6 +402,7 @@ static int brd_ioctl(struct block_device *bdev, fmode_t mode,
401 * ram device BLKFLSBUF has special semantics, we want to actually 402 * ram device BLKFLSBUF has special semantics, we want to actually
402 * release and destroy the ramdisk data. 403 * release and destroy the ramdisk data.
403 */ 404 */
405 lock_kernel();
404 mutex_lock(&bdev->bd_mutex); 406 mutex_lock(&bdev->bd_mutex);
405 error = -EBUSY; 407 error = -EBUSY;
406 if (bdev->bd_openers <= 1) { 408 if (bdev->bd_openers <= 1) {
@@ -417,13 +419,14 @@ static int brd_ioctl(struct block_device *bdev, fmode_t mode,
417 error = 0; 419 error = 0;
418 } 420 }
419 mutex_unlock(&bdev->bd_mutex); 421 mutex_unlock(&bdev->bd_mutex);
422 unlock_kernel();
420 423
421 return error; 424 return error;
422} 425}
423 426
424static const struct block_device_operations brd_fops = { 427static const struct block_device_operations brd_fops = {
425 .owner = THIS_MODULE, 428 .owner = THIS_MODULE,
426 .locked_ioctl = brd_ioctl, 429 .ioctl = brd_ioctl,
427#ifdef CONFIG_BLK_DEV_XIP 430#ifdef CONFIG_BLK_DEV_XIP
428 .direct_access = brd_direct_access, 431 .direct_access = brd_direct_access,
429#endif 432#endif
@@ -479,7 +482,7 @@ static struct brd_device *brd_alloc(int i)
479 if (!brd->brd_queue) 482 if (!brd->brd_queue)
480 goto out_free_dev; 483 goto out_free_dev;
481 blk_queue_make_request(brd->brd_queue, brd_make_request); 484 blk_queue_make_request(brd->brd_queue, brd_make_request);
482 blk_queue_ordered(brd->brd_queue, QUEUE_ORDERED_TAG, NULL); 485 blk_queue_ordered(brd->brd_queue, QUEUE_ORDERED_TAG);
483 blk_queue_max_hw_sectors(brd->brd_queue, 1024); 486 blk_queue_max_hw_sectors(brd->brd_queue, 1024);
484 blk_queue_bounce_limit(brd->brd_queue, BLK_BOUNCE_ANY); 487 blk_queue_bounce_limit(brd->brd_queue, BLK_BOUNCE_ANY);
485 488
diff --git a/drivers/block/cciss.c b/drivers/block/cciss.c
index e1e7143ca1e..31064df1370 100644
--- a/drivers/block/cciss.c
+++ b/drivers/block/cciss.c
@@ -56,16 +56,14 @@
56#include <linux/kthread.h> 56#include <linux/kthread.h>
57 57
58#define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin)) 58#define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
59#define DRIVER_NAME "HP CISS Driver (v 3.6.20)" 59#define DRIVER_NAME "HP CISS Driver (v 3.6.26)"
60#define DRIVER_VERSION CCISS_DRIVER_VERSION(3, 6, 20) 60#define DRIVER_VERSION CCISS_DRIVER_VERSION(3, 6, 26)
61 61
62/* Embedded module documentation macros - see modules.h */ 62/* Embedded module documentation macros - see modules.h */
63MODULE_AUTHOR("Hewlett-Packard Company"); 63MODULE_AUTHOR("Hewlett-Packard Company");
64MODULE_DESCRIPTION("Driver for HP Smart Array Controllers"); 64MODULE_DESCRIPTION("Driver for HP Smart Array Controllers");
65MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400" 65MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
66 " SA6i P600 P800 P400 P400i E200 E200i E500 P700m" 66MODULE_VERSION("3.6.26");
67 " Smart Array G2 Series SAS/SATA Controllers");
68MODULE_VERSION("3.6.20");
69MODULE_LICENSE("GPL"); 67MODULE_LICENSE("GPL");
70 68
71static int cciss_allow_hpsa; 69static int cciss_allow_hpsa;
@@ -107,6 +105,11 @@ static const struct pci_device_id cciss_pci_device_id[] = {
107 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249}, 105 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
108 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A}, 106 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
109 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B}, 107 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
108 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3250},
109 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3251},
110 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3252},
111 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3253},
112 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3254},
110 {0,} 113 {0,}
111}; 114};
112 115
@@ -146,6 +149,11 @@ static struct board_type products[] = {
146 {0x3249103C, "Smart Array P812", &SA5_access}, 149 {0x3249103C, "Smart Array P812", &SA5_access},
147 {0x324A103C, "Smart Array P712m", &SA5_access}, 150 {0x324A103C, "Smart Array P712m", &SA5_access},
148 {0x324B103C, "Smart Array P711m", &SA5_access}, 151 {0x324B103C, "Smart Array P711m", &SA5_access},
152 {0x3250103C, "Smart Array", &SA5_access},
153 {0x3251103C, "Smart Array", &SA5_access},
154 {0x3252103C, "Smart Array", &SA5_access},
155 {0x3253103C, "Smart Array", &SA5_access},
156 {0x3254103C, "Smart Array", &SA5_access},
149}; 157};
150 158
151/* How long to wait (in milliseconds) for board to go into simple mode */ 159/* How long to wait (in milliseconds) for board to go into simple mode */
@@ -167,9 +175,13 @@ static DEFINE_MUTEX(scan_mutex);
167static LIST_HEAD(scan_q); 175static LIST_HEAD(scan_q);
168 176
169static void do_cciss_request(struct request_queue *q); 177static void do_cciss_request(struct request_queue *q);
170static irqreturn_t do_cciss_intr(int irq, void *dev_id); 178static irqreturn_t do_cciss_intx(int irq, void *dev_id);
179static irqreturn_t do_cciss_msix_intr(int irq, void *dev_id);
171static int cciss_open(struct block_device *bdev, fmode_t mode); 180static int cciss_open(struct block_device *bdev, fmode_t mode);
181static int cciss_unlocked_open(struct block_device *bdev, fmode_t mode);
172static int cciss_release(struct gendisk *disk, fmode_t mode); 182static int cciss_release(struct gendisk *disk, fmode_t mode);
183static int do_ioctl(struct block_device *bdev, fmode_t mode,
184 unsigned int cmd, unsigned long arg);
173static int cciss_ioctl(struct block_device *bdev, fmode_t mode, 185static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
174 unsigned int cmd, unsigned long arg); 186 unsigned int cmd, unsigned long arg);
175static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo); 187static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
@@ -179,25 +191,23 @@ static int rebuild_lun_table(ctlr_info_t *h, int first_time, int via_ioctl);
179static int deregister_disk(ctlr_info_t *h, int drv_index, 191static int deregister_disk(ctlr_info_t *h, int drv_index,
180 int clear_all, int via_ioctl); 192 int clear_all, int via_ioctl);
181 193
182static void cciss_read_capacity(int ctlr, int logvol, 194static void cciss_read_capacity(ctlr_info_t *h, int logvol,
183 sector_t *total_size, unsigned int *block_size); 195 sector_t *total_size, unsigned int *block_size);
184static void cciss_read_capacity_16(int ctlr, int logvol, 196static void cciss_read_capacity_16(ctlr_info_t *h, int logvol,
185 sector_t *total_size, unsigned int *block_size); 197 sector_t *total_size, unsigned int *block_size);
186static void cciss_geometry_inquiry(int ctlr, int logvol, 198static void cciss_geometry_inquiry(ctlr_info_t *h, int logvol,
187 sector_t total_size, 199 sector_t total_size,
188 unsigned int block_size, InquiryData_struct *inq_buff, 200 unsigned int block_size, InquiryData_struct *inq_buff,
189 drive_info_struct *drv); 201 drive_info_struct *drv);
190static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *, 202static void __devinit cciss_interrupt_mode(ctlr_info_t *);
191 __u32);
192static void start_io(ctlr_info_t *h); 203static void start_io(ctlr_info_t *h);
193static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size, 204static int sendcmd_withirq(ctlr_info_t *h, __u8 cmd, void *buff, size_t size,
194 __u8 page_code, unsigned char scsi3addr[], 205 __u8 page_code, unsigned char scsi3addr[],
195 int cmd_type); 206 int cmd_type);
196static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c, 207static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
197 int attempt_retry); 208 int attempt_retry);
198static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c); 209static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c);
199 210
200static void fail_all_cmds(unsigned long ctlr);
201static int add_to_scan_list(struct ctlr_info *h); 211static int add_to_scan_list(struct ctlr_info *h);
202static int scan_thread(void *data); 212static int scan_thread(void *data);
203static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c); 213static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c);
@@ -205,11 +215,23 @@ static void cciss_hba_release(struct device *dev);
205static void cciss_device_release(struct device *dev); 215static void cciss_device_release(struct device *dev);
206static void cciss_free_gendisk(ctlr_info_t *h, int drv_index); 216static void cciss_free_gendisk(ctlr_info_t *h, int drv_index);
207static void cciss_free_drive_info(ctlr_info_t *h, int drv_index); 217static void cciss_free_drive_info(ctlr_info_t *h, int drv_index);
218static inline u32 next_command(ctlr_info_t *h);
219static int __devinit cciss_find_cfg_addrs(struct pci_dev *pdev,
220 void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index,
221 u64 *cfg_offset);
222static int __devinit cciss_pci_find_memory_BAR(struct pci_dev *pdev,
223 unsigned long *memory_bar);
224
225
226/* performant mode helper functions */
227static void calc_bucket_map(int *bucket, int num_buckets, int nsgs,
228 int *bucket_map);
229static void cciss_put_controller_into_performant_mode(ctlr_info_t *h);
208 230
209#ifdef CONFIG_PROC_FS 231#ifdef CONFIG_PROC_FS
210static void cciss_procinit(int i); 232static void cciss_procinit(ctlr_info_t *h);
211#else 233#else
212static void cciss_procinit(int i) 234static void cciss_procinit(ctlr_info_t *h)
213{ 235{
214} 236}
215#endif /* CONFIG_PROC_FS */ 237#endif /* CONFIG_PROC_FS */
@@ -221,9 +243,9 @@ static int cciss_compat_ioctl(struct block_device *, fmode_t,
221 243
222static const struct block_device_operations cciss_fops = { 244static const struct block_device_operations cciss_fops = {
223 .owner = THIS_MODULE, 245 .owner = THIS_MODULE,
224 .open = cciss_open, 246 .open = cciss_unlocked_open,
225 .release = cciss_release, 247 .release = cciss_release,
226 .locked_ioctl = cciss_ioctl, 248 .ioctl = do_ioctl,
227 .getgeo = cciss_getgeo, 249 .getgeo = cciss_getgeo,
228#ifdef CONFIG_COMPAT 250#ifdef CONFIG_COMPAT
229 .compat_ioctl = cciss_compat_ioctl, 251 .compat_ioctl = cciss_compat_ioctl,
@@ -231,6 +253,16 @@ static const struct block_device_operations cciss_fops = {
231 .revalidate_disk = cciss_revalidate, 253 .revalidate_disk = cciss_revalidate,
232}; 254};
233 255
256/* set_performant_mode: Modify the tag for cciss performant
257 * set bit 0 for pull model, bits 3-1 for block fetch
258 * register number
259 */
260static void set_performant_mode(ctlr_info_t *h, CommandList_struct *c)
261{
262 if (likely(h->transMethod == CFGTBL_Trans_Performant))
263 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
264}
265
234/* 266/*
235 * Enqueuing and dequeuing functions for cmdlists. 267 * Enqueuing and dequeuing functions for cmdlists.
236 */ 268 */
@@ -257,6 +289,18 @@ static inline void removeQ(CommandList_struct *c)
257 hlist_del_init(&c->list); 289 hlist_del_init(&c->list);
258} 290}
259 291
292static void enqueue_cmd_and_start_io(ctlr_info_t *h,
293 CommandList_struct *c)
294{
295 unsigned long flags;
296 set_performant_mode(h, c);
297 spin_lock_irqsave(&h->lock, flags);
298 addQ(&h->reqQ, c);
299 h->Qdepth++;
300 start_io(h);
301 spin_unlock_irqrestore(&h->lock, flags);
302}
303
260static void cciss_free_sg_chain_blocks(SGDescriptor_struct **cmd_sg_list, 304static void cciss_free_sg_chain_blocks(SGDescriptor_struct **cmd_sg_list,
261 int nr_cmds) 305 int nr_cmds)
262{ 306{
@@ -366,32 +410,31 @@ static void cciss_seq_show_header(struct seq_file *seq)
366 h->product_name, 410 h->product_name,
367 (unsigned long)h->board_id, 411 (unsigned long)h->board_id,
368 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], 412 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
369 h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT], 413 h->firm_ver[3], (unsigned int)h->intr[PERF_MODE_INT],
370 h->num_luns, 414 h->num_luns,
371 h->Qdepth, h->commands_outstanding, 415 h->Qdepth, h->commands_outstanding,
372 h->maxQsinceinit, h->max_outstanding, h->maxSG); 416 h->maxQsinceinit, h->max_outstanding, h->maxSG);
373 417
374#ifdef CONFIG_CISS_SCSI_TAPE 418#ifdef CONFIG_CISS_SCSI_TAPE
375 cciss_seq_tape_report(seq, h->ctlr); 419 cciss_seq_tape_report(seq, h);
376#endif /* CONFIG_CISS_SCSI_TAPE */ 420#endif /* CONFIG_CISS_SCSI_TAPE */
377} 421}
378 422
379static void *cciss_seq_start(struct seq_file *seq, loff_t *pos) 423static void *cciss_seq_start(struct seq_file *seq, loff_t *pos)
380{ 424{
381 ctlr_info_t *h = seq->private; 425 ctlr_info_t *h = seq->private;
382 unsigned ctlr = h->ctlr;
383 unsigned long flags; 426 unsigned long flags;
384 427
385 /* prevent displaying bogus info during configuration 428 /* prevent displaying bogus info during configuration
386 * or deconfiguration of a logical volume 429 * or deconfiguration of a logical volume
387 */ 430 */
388 spin_lock_irqsave(CCISS_LOCK(ctlr), flags); 431 spin_lock_irqsave(&h->lock, flags);
389 if (h->busy_configuring) { 432 if (h->busy_configuring) {
390 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); 433 spin_unlock_irqrestore(&h->lock, flags);
391 return ERR_PTR(-EBUSY); 434 return ERR_PTR(-EBUSY);
392 } 435 }
393 h->busy_configuring = 1; 436 h->busy_configuring = 1;
394 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); 437 spin_unlock_irqrestore(&h->lock, flags);
395 438
396 if (*pos == 0) 439 if (*pos == 0)
397 cciss_seq_show_header(seq); 440 cciss_seq_show_header(seq);
@@ -499,7 +542,7 @@ cciss_proc_write(struct file *file, const char __user *buf,
499 struct seq_file *seq = file->private_data; 542 struct seq_file *seq = file->private_data;
500 ctlr_info_t *h = seq->private; 543 ctlr_info_t *h = seq->private;
501 544
502 err = cciss_engage_scsi(h->ctlr); 545 err = cciss_engage_scsi(h);
503 if (err == 0) 546 if (err == 0)
504 err = length; 547 err = length;
505 } else 548 } else
@@ -522,7 +565,7 @@ static const struct file_operations cciss_proc_fops = {
522 .write = cciss_proc_write, 565 .write = cciss_proc_write,
523}; 566};
524 567
525static void __devinit cciss_procinit(int i) 568static void __devinit cciss_procinit(ctlr_info_t *h)
526{ 569{
527 struct proc_dir_entry *pde; 570 struct proc_dir_entry *pde;
528 571
@@ -530,9 +573,9 @@ static void __devinit cciss_procinit(int i)
530 proc_cciss = proc_mkdir("driver/cciss", NULL); 573 proc_cciss = proc_mkdir("driver/cciss", NULL);
531 if (!proc_cciss) 574 if (!proc_cciss)
532 return; 575 return;
533 pde = proc_create_data(hba[i]->devname, S_IWUSR | S_IRUSR | S_IRGRP | 576 pde = proc_create_data(h->devname, S_IWUSR | S_IRUSR | S_IRGRP |
534 S_IROTH, proc_cciss, 577 S_IROTH, proc_cciss,
535 &cciss_proc_fops, hba[i]); 578 &cciss_proc_fops, h);
536} 579}
537#endif /* CONFIG_PROC_FS */ 580#endif /* CONFIG_PROC_FS */
538 581
@@ -565,12 +608,12 @@ static ssize_t dev_show_unique_id(struct device *dev,
565 unsigned long flags; 608 unsigned long flags;
566 int ret = 0; 609 int ret = 0;
567 610
568 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags); 611 spin_lock_irqsave(&h->lock, flags);
569 if (h->busy_configuring) 612 if (h->busy_configuring)
570 ret = -EBUSY; 613 ret = -EBUSY;
571 else 614 else
572 memcpy(sn, drv->serial_no, sizeof(sn)); 615 memcpy(sn, drv->serial_no, sizeof(sn));
573 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 616 spin_unlock_irqrestore(&h->lock, flags);
574 617
575 if (ret) 618 if (ret)
576 return ret; 619 return ret;
@@ -595,12 +638,12 @@ static ssize_t dev_show_vendor(struct device *dev,
595 unsigned long flags; 638 unsigned long flags;
596 int ret = 0; 639 int ret = 0;
597 640
598 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags); 641 spin_lock_irqsave(&h->lock, flags);
599 if (h->busy_configuring) 642 if (h->busy_configuring)
600 ret = -EBUSY; 643 ret = -EBUSY;
601 else 644 else
602 memcpy(vendor, drv->vendor, VENDOR_LEN + 1); 645 memcpy(vendor, drv->vendor, VENDOR_LEN + 1);
603 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 646 spin_unlock_irqrestore(&h->lock, flags);
604 647
605 if (ret) 648 if (ret)
606 return ret; 649 return ret;
@@ -619,12 +662,12 @@ static ssize_t dev_show_model(struct device *dev,
619 unsigned long flags; 662 unsigned long flags;
620 int ret = 0; 663 int ret = 0;
621 664
622 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags); 665 spin_lock_irqsave(&h->lock, flags);
623 if (h->busy_configuring) 666 if (h->busy_configuring)
624 ret = -EBUSY; 667 ret = -EBUSY;
625 else 668 else
626 memcpy(model, drv->model, MODEL_LEN + 1); 669 memcpy(model, drv->model, MODEL_LEN + 1);
627 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 670 spin_unlock_irqrestore(&h->lock, flags);
628 671
629 if (ret) 672 if (ret)
630 return ret; 673 return ret;
@@ -643,12 +686,12 @@ static ssize_t dev_show_rev(struct device *dev,
643 unsigned long flags; 686 unsigned long flags;
644 int ret = 0; 687 int ret = 0;
645 688
646 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags); 689 spin_lock_irqsave(&h->lock, flags);
647 if (h->busy_configuring) 690 if (h->busy_configuring)
648 ret = -EBUSY; 691 ret = -EBUSY;
649 else 692 else
650 memcpy(rev, drv->rev, REV_LEN + 1); 693 memcpy(rev, drv->rev, REV_LEN + 1);
651 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 694 spin_unlock_irqrestore(&h->lock, flags);
652 695
653 if (ret) 696 if (ret)
654 return ret; 697 return ret;
@@ -665,17 +708,17 @@ static ssize_t cciss_show_lunid(struct device *dev,
665 unsigned long flags; 708 unsigned long flags;
666 unsigned char lunid[8]; 709 unsigned char lunid[8];
667 710
668 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags); 711 spin_lock_irqsave(&h->lock, flags);
669 if (h->busy_configuring) { 712 if (h->busy_configuring) {
670 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 713 spin_unlock_irqrestore(&h->lock, flags);
671 return -EBUSY; 714 return -EBUSY;
672 } 715 }
673 if (!drv->heads) { 716 if (!drv->heads) {
674 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 717 spin_unlock_irqrestore(&h->lock, flags);
675 return -ENOTTY; 718 return -ENOTTY;
676 } 719 }
677 memcpy(lunid, drv->LunID, sizeof(lunid)); 720 memcpy(lunid, drv->LunID, sizeof(lunid));
678 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 721 spin_unlock_irqrestore(&h->lock, flags);
679 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n", 722 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
680 lunid[0], lunid[1], lunid[2], lunid[3], 723 lunid[0], lunid[1], lunid[2], lunid[3],
681 lunid[4], lunid[5], lunid[6], lunid[7]); 724 lunid[4], lunid[5], lunid[6], lunid[7]);
@@ -690,13 +733,13 @@ static ssize_t cciss_show_raid_level(struct device *dev,
690 int raid; 733 int raid;
691 unsigned long flags; 734 unsigned long flags;
692 735
693 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags); 736 spin_lock_irqsave(&h->lock, flags);
694 if (h->busy_configuring) { 737 if (h->busy_configuring) {
695 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 738 spin_unlock_irqrestore(&h->lock, flags);
696 return -EBUSY; 739 return -EBUSY;
697 } 740 }
698 raid = drv->raid_level; 741 raid = drv->raid_level;
699 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 742 spin_unlock_irqrestore(&h->lock, flags);
700 if (raid < 0 || raid > RAID_UNKNOWN) 743 if (raid < 0 || raid > RAID_UNKNOWN)
701 raid = RAID_UNKNOWN; 744 raid = RAID_UNKNOWN;
702 745
@@ -713,13 +756,13 @@ static ssize_t cciss_show_usage_count(struct device *dev,
713 unsigned long flags; 756 unsigned long flags;
714 int count; 757 int count;
715 758
716 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags); 759 spin_lock_irqsave(&h->lock, flags);
717 if (h->busy_configuring) { 760 if (h->busy_configuring) {
718 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 761 spin_unlock_irqrestore(&h->lock, flags);
719 return -EBUSY; 762 return -EBUSY;
720 } 763 }
721 count = drv->usage_count; 764 count = drv->usage_count;
722 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 765 spin_unlock_irqrestore(&h->lock, flags);
723 return snprintf(buf, 20, "%d\n", count); 766 return snprintf(buf, 20, "%d\n", count);
724} 767}
725static DEVICE_ATTR(usage_count, S_IRUGO, cciss_show_usage_count, NULL); 768static DEVICE_ATTR(usage_count, S_IRUGO, cciss_show_usage_count, NULL);
@@ -864,60 +907,70 @@ static void cciss_destroy_ld_sysfs_entry(struct ctlr_info *h, int drv_index,
864/* 907/*
865 * For operations that cannot sleep, a command block is allocated at init, 908 * For operations that cannot sleep, a command block is allocated at init,
866 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track 909 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
867 * which ones are free or in use. For operations that can wait for kmalloc 910 * which ones are free or in use.
868 * to possible sleep, this routine can be called with get_from_pool set to 0.
869 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
870 */ 911 */
871static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool) 912static CommandList_struct *cmd_alloc(ctlr_info_t *h)
872{ 913{
873 CommandList_struct *c; 914 CommandList_struct *c;
874 int i; 915 int i;
875 u64bit temp64; 916 u64bit temp64;
876 dma_addr_t cmd_dma_handle, err_dma_handle; 917 dma_addr_t cmd_dma_handle, err_dma_handle;
877 918
878 if (!get_from_pool) { 919 do {
879 c = (CommandList_struct *) pci_alloc_consistent(h->pdev, 920 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
880 sizeof(CommandList_struct), &cmd_dma_handle); 921 if (i == h->nr_cmds)
881 if (c == NULL)
882 return NULL; 922 return NULL;
883 memset(c, 0, sizeof(CommandList_struct)); 923 } while (test_and_set_bit(i & (BITS_PER_LONG - 1),
924 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
925 c = h->cmd_pool + i;
926 memset(c, 0, sizeof(CommandList_struct));
927 cmd_dma_handle = h->cmd_pool_dhandle + i * sizeof(CommandList_struct);
928 c->err_info = h->errinfo_pool + i;
929 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
930 err_dma_handle = h->errinfo_pool_dhandle
931 + i * sizeof(ErrorInfo_struct);
932 h->nr_allocs++;
884 933
885 c->cmdindex = -1; 934 c->cmdindex = i;
886 935
887 c->err_info = (ErrorInfo_struct *) 936 INIT_HLIST_NODE(&c->list);
888 pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct), 937 c->busaddr = (__u32) cmd_dma_handle;
889 &err_dma_handle); 938 temp64.val = (__u64) err_dma_handle;
939 c->ErrDesc.Addr.lower = temp64.val32.lower;
940 c->ErrDesc.Addr.upper = temp64.val32.upper;
941 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
890 942
891 if (c->err_info == NULL) { 943 c->ctlr = h->ctlr;
892 pci_free_consistent(h->pdev, 944 return c;
893 sizeof(CommandList_struct), c, cmd_dma_handle); 945}
894 return NULL;
895 }
896 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
897 } else { /* get it out of the controllers pool */
898
899 do {
900 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
901 if (i == h->nr_cmds)
902 return NULL;
903 } while (test_and_set_bit
904 (i & (BITS_PER_LONG - 1),
905 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
906#ifdef CCISS_DEBUG
907 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
908#endif
909 c = h->cmd_pool + i;
910 memset(c, 0, sizeof(CommandList_struct));
911 cmd_dma_handle = h->cmd_pool_dhandle
912 + i * sizeof(CommandList_struct);
913 c->err_info = h->errinfo_pool + i;
914 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
915 err_dma_handle = h->errinfo_pool_dhandle
916 + i * sizeof(ErrorInfo_struct);
917 h->nr_allocs++;
918 946
919 c->cmdindex = i; 947/* allocate a command using pci_alloc_consistent, used for ioctls,
948 * etc., not for the main i/o path.
949 */
950static CommandList_struct *cmd_special_alloc(ctlr_info_t *h)
951{
952 CommandList_struct *c;
953 u64bit temp64;
954 dma_addr_t cmd_dma_handle, err_dma_handle;
955
956 c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
957 sizeof(CommandList_struct), &cmd_dma_handle);
958 if (c == NULL)
959 return NULL;
960 memset(c, 0, sizeof(CommandList_struct));
961
962 c->cmdindex = -1;
963
964 c->err_info = (ErrorInfo_struct *)
965 pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
966 &err_dma_handle);
967
968 if (c->err_info == NULL) {
969 pci_free_consistent(h->pdev,
970 sizeof(CommandList_struct), c, cmd_dma_handle);
971 return NULL;
920 } 972 }
973 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
921 974
922 INIT_HLIST_NODE(&c->list); 975 INIT_HLIST_NODE(&c->list);
923 c->busaddr = (__u32) cmd_dma_handle; 976 c->busaddr = (__u32) cmd_dma_handle;
@@ -930,27 +983,26 @@ static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool)
930 return c; 983 return c;
931} 984}
932 985
933/* 986static void cmd_free(ctlr_info_t *h, CommandList_struct *c)
934 * Frees a command block that was previously allocated with cmd_alloc().
935 */
936static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
937{ 987{
938 int i; 988 int i;
989
990 i = c - h->cmd_pool;
991 clear_bit(i & (BITS_PER_LONG - 1),
992 h->cmd_pool_bits + (i / BITS_PER_LONG));
993 h->nr_frees++;
994}
995
996static void cmd_special_free(ctlr_info_t *h, CommandList_struct *c)
997{
939 u64bit temp64; 998 u64bit temp64;
940 999
941 if (!got_from_pool) { 1000 temp64.val32.lower = c->ErrDesc.Addr.lower;
942 temp64.val32.lower = c->ErrDesc.Addr.lower; 1001 temp64.val32.upper = c->ErrDesc.Addr.upper;
943 temp64.val32.upper = c->ErrDesc.Addr.upper; 1002 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
944 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct), 1003 c->err_info, (dma_addr_t) temp64.val);
945 c->err_info, (dma_addr_t) temp64.val); 1004 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
946 pci_free_consistent(h->pdev, sizeof(CommandList_struct), 1005 c, (dma_addr_t) c->busaddr);
947 c, (dma_addr_t) c->busaddr);
948 } else {
949 i = c - h->cmd_pool;
950 clear_bit(i & (BITS_PER_LONG - 1),
951 h->cmd_pool_bits + (i / BITS_PER_LONG));
952 h->nr_frees++;
953 }
954} 1006}
955 1007
956static inline ctlr_info_t *get_host(struct gendisk *disk) 1008static inline ctlr_info_t *get_host(struct gendisk *disk)
@@ -968,13 +1020,10 @@ static inline drive_info_struct *get_drv(struct gendisk *disk)
968 */ 1020 */
969static int cciss_open(struct block_device *bdev, fmode_t mode) 1021static int cciss_open(struct block_device *bdev, fmode_t mode)
970{ 1022{
971 ctlr_info_t *host = get_host(bdev->bd_disk); 1023 ctlr_info_t *h = get_host(bdev->bd_disk);
972 drive_info_struct *drv = get_drv(bdev->bd_disk); 1024 drive_info_struct *drv = get_drv(bdev->bd_disk);
973 1025
974#ifdef CCISS_DEBUG 1026 dev_dbg(&h->pdev->dev, "cciss_open %s\n", bdev->bd_disk->disk_name);
975 printk(KERN_DEBUG "cciss_open %s\n", bdev->bd_disk->disk_name);
976#endif /* CCISS_DEBUG */
977
978 if (drv->busy_configuring) 1027 if (drv->busy_configuring)
979 return -EBUSY; 1028 return -EBUSY;
980 /* 1029 /*
@@ -1000,29 +1049,39 @@ static int cciss_open(struct block_device *bdev, fmode_t mode)
1000 return -EPERM; 1049 return -EPERM;
1001 } 1050 }
1002 drv->usage_count++; 1051 drv->usage_count++;
1003 host->usage_count++; 1052 h->usage_count++;
1004 return 0; 1053 return 0;
1005} 1054}
1006 1055
1056static int cciss_unlocked_open(struct block_device *bdev, fmode_t mode)
1057{
1058 int ret;
1059
1060 lock_kernel();
1061 ret = cciss_open(bdev, mode);
1062 unlock_kernel();
1063
1064 return ret;
1065}
1066
1007/* 1067/*
1008 * Close. Sync first. 1068 * Close. Sync first.
1009 */ 1069 */
1010static int cciss_release(struct gendisk *disk, fmode_t mode) 1070static int cciss_release(struct gendisk *disk, fmode_t mode)
1011{ 1071{
1012 ctlr_info_t *host = get_host(disk); 1072 ctlr_info_t *h;
1013 drive_info_struct *drv = get_drv(disk); 1073 drive_info_struct *drv;
1014
1015#ifdef CCISS_DEBUG
1016 printk(KERN_DEBUG "cciss_release %s\n", disk->disk_name);
1017#endif /* CCISS_DEBUG */
1018 1074
1075 lock_kernel();
1076 h = get_host(disk);
1077 drv = get_drv(disk);
1078 dev_dbg(&h->pdev->dev, "cciss_release %s\n", disk->disk_name);
1019 drv->usage_count--; 1079 drv->usage_count--;
1020 host->usage_count--; 1080 h->usage_count--;
1081 unlock_kernel();
1021 return 0; 1082 return 0;
1022} 1083}
1023 1084
1024#ifdef CONFIG_COMPAT
1025
1026static int do_ioctl(struct block_device *bdev, fmode_t mode, 1085static int do_ioctl(struct block_device *bdev, fmode_t mode,
1027 unsigned cmd, unsigned long arg) 1086 unsigned cmd, unsigned long arg)
1028{ 1087{
@@ -1033,6 +1092,8 @@ static int do_ioctl(struct block_device *bdev, fmode_t mode,
1033 return ret; 1092 return ret;
1034} 1093}
1035 1094
1095#ifdef CONFIG_COMPAT
1096
1036static int cciss_ioctl32_passthru(struct block_device *bdev, fmode_t mode, 1097static int cciss_ioctl32_passthru(struct block_device *bdev, fmode_t mode,
1037 unsigned cmd, unsigned long arg); 1098 unsigned cmd, unsigned long arg);
1038static int cciss_ioctl32_big_passthru(struct block_device *bdev, fmode_t mode, 1099static int cciss_ioctl32_big_passthru(struct block_device *bdev, fmode_t mode,
@@ -1163,11 +1224,11 @@ static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1163 return 0; 1224 return 0;
1164} 1225}
1165 1226
1166static void check_ioctl_unit_attention(ctlr_info_t *host, CommandList_struct *c) 1227static void check_ioctl_unit_attention(ctlr_info_t *h, CommandList_struct *c)
1167{ 1228{
1168 if (c->err_info->CommandStatus == CMD_TARGET_STATUS && 1229 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
1169 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) 1230 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
1170 (void)check_for_unit_attention(host, c); 1231 (void)check_for_unit_attention(h, c);
1171} 1232}
1172/* 1233/*
1173 * ioctl 1234 * ioctl
@@ -1176,15 +1237,12 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1176 unsigned int cmd, unsigned long arg) 1237 unsigned int cmd, unsigned long arg)
1177{ 1238{
1178 struct gendisk *disk = bdev->bd_disk; 1239 struct gendisk *disk = bdev->bd_disk;
1179 ctlr_info_t *host = get_host(disk); 1240 ctlr_info_t *h = get_host(disk);
1180 drive_info_struct *drv = get_drv(disk); 1241 drive_info_struct *drv = get_drv(disk);
1181 int ctlr = host->ctlr;
1182 void __user *argp = (void __user *)arg; 1242 void __user *argp = (void __user *)arg;
1183 1243
1184#ifdef CCISS_DEBUG 1244 dev_dbg(&h->pdev->dev, "cciss_ioctl: Called with cmd=%x %lx\n",
1185 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg); 1245 cmd, arg);
1186#endif /* CCISS_DEBUG */
1187
1188 switch (cmd) { 1246 switch (cmd) {
1189 case CCISS_GETPCIINFO: 1247 case CCISS_GETPCIINFO:
1190 { 1248 {
@@ -1192,10 +1250,10 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1192 1250
1193 if (!arg) 1251 if (!arg)
1194 return -EINVAL; 1252 return -EINVAL;
1195 pciinfo.domain = pci_domain_nr(host->pdev->bus); 1253 pciinfo.domain = pci_domain_nr(h->pdev->bus);
1196 pciinfo.bus = host->pdev->bus->number; 1254 pciinfo.bus = h->pdev->bus->number;
1197 pciinfo.dev_fn = host->pdev->devfn; 1255 pciinfo.dev_fn = h->pdev->devfn;
1198 pciinfo.board_id = host->board_id; 1256 pciinfo.board_id = h->board_id;
1199 if (copy_to_user 1257 if (copy_to_user
1200 (argp, &pciinfo, sizeof(cciss_pci_info_struct))) 1258 (argp, &pciinfo, sizeof(cciss_pci_info_struct)))
1201 return -EFAULT; 1259 return -EFAULT;
@@ -1207,9 +1265,9 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1207 if (!arg) 1265 if (!arg)
1208 return -EINVAL; 1266 return -EINVAL;
1209 intinfo.delay = 1267 intinfo.delay =
1210 readl(&host->cfgtable->HostWrite.CoalIntDelay); 1268 readl(&h->cfgtable->HostWrite.CoalIntDelay);
1211 intinfo.count = 1269 intinfo.count =
1212 readl(&host->cfgtable->HostWrite.CoalIntCount); 1270 readl(&h->cfgtable->HostWrite.CoalIntCount);
1213 if (copy_to_user 1271 if (copy_to_user
1214 (argp, &intinfo, sizeof(cciss_coalint_struct))) 1272 (argp, &intinfo, sizeof(cciss_coalint_struct)))
1215 return -EFAULT; 1273 return -EFAULT;
@@ -1229,26 +1287,23 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1229 (&intinfo, argp, sizeof(cciss_coalint_struct))) 1287 (&intinfo, argp, sizeof(cciss_coalint_struct)))
1230 return -EFAULT; 1288 return -EFAULT;
1231 if ((intinfo.delay == 0) && (intinfo.count == 0)) 1289 if ((intinfo.delay == 0) && (intinfo.count == 0))
1232 {
1233// printk("cciss_ioctl: delay and count cannot be 0\n");
1234 return -EINVAL; 1290 return -EINVAL;
1235 } 1291 spin_lock_irqsave(&h->lock, flags);
1236 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1237 /* Update the field, and then ring the doorbell */ 1292 /* Update the field, and then ring the doorbell */
1238 writel(intinfo.delay, 1293 writel(intinfo.delay,
1239 &(host->cfgtable->HostWrite.CoalIntDelay)); 1294 &(h->cfgtable->HostWrite.CoalIntDelay));
1240 writel(intinfo.count, 1295 writel(intinfo.count,
1241 &(host->cfgtable->HostWrite.CoalIntCount)); 1296 &(h->cfgtable->HostWrite.CoalIntCount));
1242 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL); 1297 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
1243 1298
1244 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) { 1299 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
1245 if (!(readl(host->vaddr + SA5_DOORBELL) 1300 if (!(readl(h->vaddr + SA5_DOORBELL)
1246 & CFGTBL_ChangeReq)) 1301 & CFGTBL_ChangeReq))
1247 break; 1302 break;
1248 /* delay and try again */ 1303 /* delay and try again */
1249 udelay(1000); 1304 udelay(1000);
1250 } 1305 }
1251 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); 1306 spin_unlock_irqrestore(&h->lock, flags);
1252 if (i >= MAX_IOCTL_CONFIG_WAIT) 1307 if (i >= MAX_IOCTL_CONFIG_WAIT)
1253 return -EAGAIN; 1308 return -EAGAIN;
1254 return 0; 1309 return 0;
@@ -1262,7 +1317,7 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1262 return -EINVAL; 1317 return -EINVAL;
1263 for (i = 0; i < 16; i++) 1318 for (i = 0; i < 16; i++)
1264 NodeName[i] = 1319 NodeName[i] =
1265 readb(&host->cfgtable->ServerName[i]); 1320 readb(&h->cfgtable->ServerName[i]);
1266 if (copy_to_user(argp, NodeName, sizeof(NodeName_type))) 1321 if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
1267 return -EFAULT; 1322 return -EFAULT;
1268 return 0; 1323 return 0;
@@ -1282,23 +1337,23 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1282 (NodeName, argp, sizeof(NodeName_type))) 1337 (NodeName, argp, sizeof(NodeName_type)))
1283 return -EFAULT; 1338 return -EFAULT;
1284 1339
1285 spin_lock_irqsave(CCISS_LOCK(ctlr), flags); 1340 spin_lock_irqsave(&h->lock, flags);
1286 1341
1287 /* Update the field, and then ring the doorbell */ 1342 /* Update the field, and then ring the doorbell */
1288 for (i = 0; i < 16; i++) 1343 for (i = 0; i < 16; i++)
1289 writeb(NodeName[i], 1344 writeb(NodeName[i],
1290 &host->cfgtable->ServerName[i]); 1345 &h->cfgtable->ServerName[i]);
1291 1346
1292 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL); 1347 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
1293 1348
1294 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) { 1349 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
1295 if (!(readl(host->vaddr + SA5_DOORBELL) 1350 if (!(readl(h->vaddr + SA5_DOORBELL)
1296 & CFGTBL_ChangeReq)) 1351 & CFGTBL_ChangeReq))
1297 break; 1352 break;
1298 /* delay and try again */ 1353 /* delay and try again */
1299 udelay(1000); 1354 udelay(1000);
1300 } 1355 }
1301 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); 1356 spin_unlock_irqrestore(&h->lock, flags);
1302 if (i >= MAX_IOCTL_CONFIG_WAIT) 1357 if (i >= MAX_IOCTL_CONFIG_WAIT)
1303 return -EAGAIN; 1358 return -EAGAIN;
1304 return 0; 1359 return 0;
@@ -1310,7 +1365,7 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1310 1365
1311 if (!arg) 1366 if (!arg)
1312 return -EINVAL; 1367 return -EINVAL;
1313 heartbeat = readl(&host->cfgtable->HeartBeat); 1368 heartbeat = readl(&h->cfgtable->HeartBeat);
1314 if (copy_to_user 1369 if (copy_to_user
1315 (argp, &heartbeat, sizeof(Heartbeat_type))) 1370 (argp, &heartbeat, sizeof(Heartbeat_type)))
1316 return -EFAULT; 1371 return -EFAULT;
@@ -1322,7 +1377,7 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1322 1377
1323 if (!arg) 1378 if (!arg)
1324 return -EINVAL; 1379 return -EINVAL;
1325 BusTypes = readl(&host->cfgtable->BusTypes); 1380 BusTypes = readl(&h->cfgtable->BusTypes);
1326 if (copy_to_user 1381 if (copy_to_user
1327 (argp, &BusTypes, sizeof(BusTypes_type))) 1382 (argp, &BusTypes, sizeof(BusTypes_type)))
1328 return -EFAULT; 1383 return -EFAULT;
@@ -1334,7 +1389,7 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1334 1389
1335 if (!arg) 1390 if (!arg)
1336 return -EINVAL; 1391 return -EINVAL;
1337 memcpy(firmware, host->firm_ver, 4); 1392 memcpy(firmware, h->firm_ver, 4);
1338 1393
1339 if (copy_to_user 1394 if (copy_to_user
1340 (argp, firmware, sizeof(FirmwareVer_type))) 1395 (argp, firmware, sizeof(FirmwareVer_type)))
@@ -1357,7 +1412,7 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1357 case CCISS_DEREGDISK: 1412 case CCISS_DEREGDISK:
1358 case CCISS_REGNEWD: 1413 case CCISS_REGNEWD:
1359 case CCISS_REVALIDVOLS: 1414 case CCISS_REVALIDVOLS:
1360 return rebuild_lun_table(host, 0, 1); 1415 return rebuild_lun_table(h, 0, 1);
1361 1416
1362 case CCISS_GETLUNINFO:{ 1417 case CCISS_GETLUNINFO:{
1363 LogvolInfo_struct luninfo; 1418 LogvolInfo_struct luninfo;
@@ -1377,7 +1432,6 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1377 CommandList_struct *c; 1432 CommandList_struct *c;
1378 char *buff = NULL; 1433 char *buff = NULL;
1379 u64bit temp64; 1434 u64bit temp64;
1380 unsigned long flags;
1381 DECLARE_COMPLETION_ONSTACK(wait); 1435 DECLARE_COMPLETION_ONSTACK(wait);
1382 1436
1383 if (!arg) 1437 if (!arg)
@@ -1413,7 +1467,8 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1413 } else { 1467 } else {
1414 memset(buff, 0, iocommand.buf_size); 1468 memset(buff, 0, iocommand.buf_size);
1415 } 1469 }
1416 if ((c = cmd_alloc(host, 0)) == NULL) { 1470 c = cmd_special_alloc(h);
1471 if (!c) {
1417 kfree(buff); 1472 kfree(buff);
1418 return -ENOMEM; 1473 return -ENOMEM;
1419 } 1474 }
@@ -1439,7 +1494,7 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1439 1494
1440 /* Fill in the scatter gather information */ 1495 /* Fill in the scatter gather information */
1441 if (iocommand.buf_size > 0) { 1496 if (iocommand.buf_size > 0) {
1442 temp64.val = pci_map_single(host->pdev, buff, 1497 temp64.val = pci_map_single(h->pdev, buff,
1443 iocommand.buf_size, 1498 iocommand.buf_size,
1444 PCI_DMA_BIDIRECTIONAL); 1499 PCI_DMA_BIDIRECTIONAL);
1445 c->SG[0].Addr.lower = temp64.val32.lower; 1500 c->SG[0].Addr.lower = temp64.val32.lower;
@@ -1449,30 +1504,24 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1449 } 1504 }
1450 c->waiting = &wait; 1505 c->waiting = &wait;
1451 1506
1452 /* Put the request on the tail of the request queue */ 1507 enqueue_cmd_and_start_io(h, c);
1453 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1454 addQ(&host->reqQ, c);
1455 host->Qdepth++;
1456 start_io(host);
1457 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1458
1459 wait_for_completion(&wait); 1508 wait_for_completion(&wait);
1460 1509
1461 /* unlock the buffers from DMA */ 1510 /* unlock the buffers from DMA */
1462 temp64.val32.lower = c->SG[0].Addr.lower; 1511 temp64.val32.lower = c->SG[0].Addr.lower;
1463 temp64.val32.upper = c->SG[0].Addr.upper; 1512 temp64.val32.upper = c->SG[0].Addr.upper;
1464 pci_unmap_single(host->pdev, (dma_addr_t) temp64.val, 1513 pci_unmap_single(h->pdev, (dma_addr_t) temp64.val,
1465 iocommand.buf_size, 1514 iocommand.buf_size,
1466 PCI_DMA_BIDIRECTIONAL); 1515 PCI_DMA_BIDIRECTIONAL);
1467 1516
1468 check_ioctl_unit_attention(host, c); 1517 check_ioctl_unit_attention(h, c);
1469 1518
1470 /* Copy the error information out */ 1519 /* Copy the error information out */
1471 iocommand.error_info = *(c->err_info); 1520 iocommand.error_info = *(c->err_info);
1472 if (copy_to_user 1521 if (copy_to_user
1473 (argp, &iocommand, sizeof(IOCTL_Command_struct))) { 1522 (argp, &iocommand, sizeof(IOCTL_Command_struct))) {
1474 kfree(buff); 1523 kfree(buff);
1475 cmd_free(host, c, 0); 1524 cmd_special_free(h, c);
1476 return -EFAULT; 1525 return -EFAULT;
1477 } 1526 }
1478 1527
@@ -1481,12 +1530,12 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1481 if (copy_to_user 1530 if (copy_to_user
1482 (iocommand.buf, buff, iocommand.buf_size)) { 1531 (iocommand.buf, buff, iocommand.buf_size)) {
1483 kfree(buff); 1532 kfree(buff);
1484 cmd_free(host, c, 0); 1533 cmd_special_free(h, c);
1485 return -EFAULT; 1534 return -EFAULT;
1486 } 1535 }
1487 } 1536 }
1488 kfree(buff); 1537 kfree(buff);
1489 cmd_free(host, c, 0); 1538 cmd_special_free(h, c);
1490 return 0; 1539 return 0;
1491 } 1540 }
1492 case CCISS_BIG_PASSTHRU:{ 1541 case CCISS_BIG_PASSTHRU:{
@@ -1495,7 +1544,6 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1495 unsigned char **buff = NULL; 1544 unsigned char **buff = NULL;
1496 int *buff_size = NULL; 1545 int *buff_size = NULL;
1497 u64bit temp64; 1546 u64bit temp64;
1498 unsigned long flags;
1499 BYTE sg_used = 0; 1547 BYTE sg_used = 0;
1500 int status = 0; 1548 int status = 0;
1501 int i; 1549 int i;
@@ -1569,7 +1617,8 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1569 data_ptr += sz; 1617 data_ptr += sz;
1570 sg_used++; 1618 sg_used++;
1571 } 1619 }
1572 if ((c = cmd_alloc(host, 0)) == NULL) { 1620 c = cmd_special_alloc(h);
1621 if (!c) {
1573 status = -ENOMEM; 1622 status = -ENOMEM;
1574 goto cleanup1; 1623 goto cleanup1;
1575 } 1624 }
@@ -1590,7 +1639,7 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1590 if (ioc->buf_size > 0) { 1639 if (ioc->buf_size > 0) {
1591 for (i = 0; i < sg_used; i++) { 1640 for (i = 0; i < sg_used; i++) {
1592 temp64.val = 1641 temp64.val =
1593 pci_map_single(host->pdev, buff[i], 1642 pci_map_single(h->pdev, buff[i],
1594 buff_size[i], 1643 buff_size[i],
1595 PCI_DMA_BIDIRECTIONAL); 1644 PCI_DMA_BIDIRECTIONAL);
1596 c->SG[i].Addr.lower = 1645 c->SG[i].Addr.lower =
@@ -1602,26 +1651,21 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1602 } 1651 }
1603 } 1652 }
1604 c->waiting = &wait; 1653 c->waiting = &wait;
1605 /* Put the request on the tail of the request queue */ 1654 enqueue_cmd_and_start_io(h, c);
1606 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1607 addQ(&host->reqQ, c);
1608 host->Qdepth++;
1609 start_io(host);
1610 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1611 wait_for_completion(&wait); 1655 wait_for_completion(&wait);
1612 /* unlock the buffers from DMA */ 1656 /* unlock the buffers from DMA */
1613 for (i = 0; i < sg_used; i++) { 1657 for (i = 0; i < sg_used; i++) {
1614 temp64.val32.lower = c->SG[i].Addr.lower; 1658 temp64.val32.lower = c->SG[i].Addr.lower;
1615 temp64.val32.upper = c->SG[i].Addr.upper; 1659 temp64.val32.upper = c->SG[i].Addr.upper;
1616 pci_unmap_single(host->pdev, 1660 pci_unmap_single(h->pdev,
1617 (dma_addr_t) temp64.val, buff_size[i], 1661 (dma_addr_t) temp64.val, buff_size[i],
1618 PCI_DMA_BIDIRECTIONAL); 1662 PCI_DMA_BIDIRECTIONAL);
1619 } 1663 }
1620 check_ioctl_unit_attention(host, c); 1664 check_ioctl_unit_attention(h, c);
1621 /* Copy the error information out */ 1665 /* Copy the error information out */
1622 ioc->error_info = *(c->err_info); 1666 ioc->error_info = *(c->err_info);
1623 if (copy_to_user(argp, ioc, sizeof(*ioc))) { 1667 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1624 cmd_free(host, c, 0); 1668 cmd_special_free(h, c);
1625 status = -EFAULT; 1669 status = -EFAULT;
1626 goto cleanup1; 1670 goto cleanup1;
1627 } 1671 }
@@ -1631,14 +1675,14 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1631 for (i = 0; i < sg_used; i++) { 1675 for (i = 0; i < sg_used; i++) {
1632 if (copy_to_user 1676 if (copy_to_user
1633 (ptr, buff[i], buff_size[i])) { 1677 (ptr, buff[i], buff_size[i])) {
1634 cmd_free(host, c, 0); 1678 cmd_special_free(h, c);
1635 status = -EFAULT; 1679 status = -EFAULT;
1636 goto cleanup1; 1680 goto cleanup1;
1637 } 1681 }
1638 ptr += buff_size[i]; 1682 ptr += buff_size[i];
1639 } 1683 }
1640 } 1684 }
1641 cmd_free(host, c, 0); 1685 cmd_special_free(h, c);
1642 status = 0; 1686 status = 0;
1643 cleanup1: 1687 cleanup1:
1644 if (buff) { 1688 if (buff) {
@@ -1726,26 +1770,26 @@ static void cciss_check_queues(ctlr_info_t *h)
1726 1770
1727static void cciss_softirq_done(struct request *rq) 1771static void cciss_softirq_done(struct request *rq)
1728{ 1772{
1729 CommandList_struct *cmd = rq->completion_data; 1773 CommandList_struct *c = rq->completion_data;
1730 ctlr_info_t *h = hba[cmd->ctlr]; 1774 ctlr_info_t *h = hba[c->ctlr];
1731 SGDescriptor_struct *curr_sg = cmd->SG; 1775 SGDescriptor_struct *curr_sg = c->SG;
1732 unsigned long flags;
1733 u64bit temp64; 1776 u64bit temp64;
1777 unsigned long flags;
1734 int i, ddir; 1778 int i, ddir;
1735 int sg_index = 0; 1779 int sg_index = 0;
1736 1780
1737 if (cmd->Request.Type.Direction == XFER_READ) 1781 if (c->Request.Type.Direction == XFER_READ)
1738 ddir = PCI_DMA_FROMDEVICE; 1782 ddir = PCI_DMA_FROMDEVICE;
1739 else 1783 else
1740 ddir = PCI_DMA_TODEVICE; 1784 ddir = PCI_DMA_TODEVICE;
1741 1785
1742 /* command did not need to be retried */ 1786 /* command did not need to be retried */
1743 /* unmap the DMA mapping for all the scatter gather elements */ 1787 /* unmap the DMA mapping for all the scatter gather elements */
1744 for (i = 0; i < cmd->Header.SGList; i++) { 1788 for (i = 0; i < c->Header.SGList; i++) {
1745 if (curr_sg[sg_index].Ext == CCISS_SG_CHAIN) { 1789 if (curr_sg[sg_index].Ext == CCISS_SG_CHAIN) {
1746 cciss_unmap_sg_chain_block(h, cmd); 1790 cciss_unmap_sg_chain_block(h, c);
1747 /* Point to the next block */ 1791 /* Point to the next block */
1748 curr_sg = h->cmd_sg_list[cmd->cmdindex]; 1792 curr_sg = h->cmd_sg_list[c->cmdindex];
1749 sg_index = 0; 1793 sg_index = 0;
1750 } 1794 }
1751 temp64.val32.lower = curr_sg[sg_index].Addr.lower; 1795 temp64.val32.lower = curr_sg[sg_index].Addr.lower;
@@ -1755,18 +1799,16 @@ static void cciss_softirq_done(struct request *rq)
1755 ++sg_index; 1799 ++sg_index;
1756 } 1800 }
1757 1801
1758#ifdef CCISS_DEBUG 1802 dev_dbg(&h->pdev->dev, "Done with %p\n", rq);
1759 printk("Done with %p\n", rq);
1760#endif /* CCISS_DEBUG */
1761 1803
1762 /* set the residual count for pc requests */ 1804 /* set the residual count for pc requests */
1763 if (blk_pc_request(rq)) 1805 if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
1764 rq->resid_len = cmd->err_info->ResidualCnt; 1806 rq->resid_len = c->err_info->ResidualCnt;
1765 1807
1766 blk_end_request_all(rq, (rq->errors == 0) ? 0 : -EIO); 1808 blk_end_request_all(rq, (rq->errors == 0) ? 0 : -EIO);
1767 1809
1768 spin_lock_irqsave(&h->lock, flags); 1810 spin_lock_irqsave(&h->lock, flags);
1769 cmd_free(h, cmd, 1); 1811 cmd_free(h, c);
1770 cciss_check_queues(h); 1812 cciss_check_queues(h);
1771 spin_unlock_irqrestore(&h->lock, flags); 1813 spin_unlock_irqrestore(&h->lock, flags);
1772} 1814}
@@ -1782,7 +1824,7 @@ static inline void log_unit_to_scsi3addr(ctlr_info_t *h,
1782 * via the inquiry page 0. Model, vendor, and rev are set to empty strings if 1824 * via the inquiry page 0. Model, vendor, and rev are set to empty strings if
1783 * they cannot be read. 1825 * they cannot be read.
1784 */ 1826 */
1785static void cciss_get_device_descr(int ctlr, int logvol, 1827static void cciss_get_device_descr(ctlr_info_t *h, int logvol,
1786 char *vendor, char *model, char *rev) 1828 char *vendor, char *model, char *rev)
1787{ 1829{
1788 int rc; 1830 int rc;
@@ -1797,8 +1839,8 @@ static void cciss_get_device_descr(int ctlr, int logvol,
1797 if (!inq_buf) 1839 if (!inq_buf)
1798 return; 1840 return;
1799 1841
1800 log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol); 1842 log_unit_to_scsi3addr(h, scsi3addr, logvol);
1801 rc = sendcmd_withirq(CISS_INQUIRY, ctlr, inq_buf, sizeof(*inq_buf), 0, 1843 rc = sendcmd_withirq(h, CISS_INQUIRY, inq_buf, sizeof(*inq_buf), 0,
1802 scsi3addr, TYPE_CMD); 1844 scsi3addr, TYPE_CMD);
1803 if (rc == IO_OK) { 1845 if (rc == IO_OK) {
1804 memcpy(vendor, &inq_buf->data_byte[8], VENDOR_LEN); 1846 memcpy(vendor, &inq_buf->data_byte[8], VENDOR_LEN);
@@ -1818,7 +1860,7 @@ static void cciss_get_device_descr(int ctlr, int logvol,
1818 * number cannot be had, for whatever reason, 16 bytes of 0xff 1860 * number cannot be had, for whatever reason, 16 bytes of 0xff
1819 * are returned instead. 1861 * are returned instead.
1820 */ 1862 */
1821static void cciss_get_serial_no(int ctlr, int logvol, 1863static void cciss_get_serial_no(ctlr_info_t *h, int logvol,
1822 unsigned char *serial_no, int buflen) 1864 unsigned char *serial_no, int buflen)
1823{ 1865{
1824#define PAGE_83_INQ_BYTES 64 1866#define PAGE_83_INQ_BYTES 64
@@ -1833,8 +1875,8 @@ static void cciss_get_serial_no(int ctlr, int logvol,
1833 if (!buf) 1875 if (!buf)
1834 return; 1876 return;
1835 memset(serial_no, 0, buflen); 1877 memset(serial_no, 0, buflen);
1836 log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol); 1878 log_unit_to_scsi3addr(h, scsi3addr, logvol);
1837 rc = sendcmd_withirq(CISS_INQUIRY, ctlr, buf, 1879 rc = sendcmd_withirq(h, CISS_INQUIRY, buf,
1838 PAGE_83_INQ_BYTES, 0x83, scsi3addr, TYPE_CMD); 1880 PAGE_83_INQ_BYTES, 0x83, scsi3addr, TYPE_CMD);
1839 if (rc == IO_OK) 1881 if (rc == IO_OK)
1840 memcpy(serial_no, &buf[8], buflen); 1882 memcpy(serial_no, &buf[8], buflen);
@@ -1900,10 +1942,9 @@ init_queue_failure:
1900 * is also the controller node. Any changes to disk 0 will show up on 1942 * is also the controller node. Any changes to disk 0 will show up on
1901 * the next reboot. 1943 * the next reboot.
1902 */ 1944 */
1903static void cciss_update_drive_info(int ctlr, int drv_index, int first_time, 1945static void cciss_update_drive_info(ctlr_info_t *h, int drv_index,
1904 int via_ioctl) 1946 int first_time, int via_ioctl)
1905{ 1947{
1906 ctlr_info_t *h = hba[ctlr];
1907 struct gendisk *disk; 1948 struct gendisk *disk;
1908 InquiryData_struct *inq_buff = NULL; 1949 InquiryData_struct *inq_buff = NULL;
1909 unsigned int block_size; 1950 unsigned int block_size;
@@ -1920,16 +1961,16 @@ static void cciss_update_drive_info(int ctlr, int drv_index, int first_time,
1920 1961
1921 /* testing to see if 16-byte CDBs are already being used */ 1962 /* testing to see if 16-byte CDBs are already being used */
1922 if (h->cciss_read == CCISS_READ_16) { 1963 if (h->cciss_read == CCISS_READ_16) {
1923 cciss_read_capacity_16(h->ctlr, drv_index, 1964 cciss_read_capacity_16(h, drv_index,
1924 &total_size, &block_size); 1965 &total_size, &block_size);
1925 1966
1926 } else { 1967 } else {
1927 cciss_read_capacity(ctlr, drv_index, &total_size, &block_size); 1968 cciss_read_capacity(h, drv_index, &total_size, &block_size);
1928 /* if read_capacity returns all F's this volume is >2TB */ 1969 /* if read_capacity returns all F's this volume is >2TB */
1929 /* in size so we switch to 16-byte CDB's for all */ 1970 /* in size so we switch to 16-byte CDB's for all */
1930 /* read/write ops */ 1971 /* read/write ops */
1931 if (total_size == 0xFFFFFFFFULL) { 1972 if (total_size == 0xFFFFFFFFULL) {
1932 cciss_read_capacity_16(ctlr, drv_index, 1973 cciss_read_capacity_16(h, drv_index,
1933 &total_size, &block_size); 1974 &total_size, &block_size);
1934 h->cciss_read = CCISS_READ_16; 1975 h->cciss_read = CCISS_READ_16;
1935 h->cciss_write = CCISS_WRITE_16; 1976 h->cciss_write = CCISS_WRITE_16;
@@ -1939,14 +1980,14 @@ static void cciss_update_drive_info(int ctlr, int drv_index, int first_time,
1939 } 1980 }
1940 } 1981 }
1941 1982
1942 cciss_geometry_inquiry(ctlr, drv_index, total_size, block_size, 1983 cciss_geometry_inquiry(h, drv_index, total_size, block_size,
1943 inq_buff, drvinfo); 1984 inq_buff, drvinfo);
1944 drvinfo->block_size = block_size; 1985 drvinfo->block_size = block_size;
1945 drvinfo->nr_blocks = total_size + 1; 1986 drvinfo->nr_blocks = total_size + 1;
1946 1987
1947 cciss_get_device_descr(ctlr, drv_index, drvinfo->vendor, 1988 cciss_get_device_descr(h, drv_index, drvinfo->vendor,
1948 drvinfo->model, drvinfo->rev); 1989 drvinfo->model, drvinfo->rev);
1949 cciss_get_serial_no(ctlr, drv_index, drvinfo->serial_no, 1990 cciss_get_serial_no(h, drv_index, drvinfo->serial_no,
1950 sizeof(drvinfo->serial_no)); 1991 sizeof(drvinfo->serial_no));
1951 /* Save the lunid in case we deregister the disk, below. */ 1992 /* Save the lunid in case we deregister the disk, below. */
1952 memcpy(drvinfo->LunID, h->drv[drv_index]->LunID, 1993 memcpy(drvinfo->LunID, h->drv[drv_index]->LunID,
@@ -1971,10 +2012,10 @@ static void cciss_update_drive_info(int ctlr, int drv_index, int first_time,
1971 * (unless it's the first disk (for the controller node). 2012 * (unless it's the first disk (for the controller node).
1972 */ 2013 */
1973 if (h->drv[drv_index]->raid_level != -1 && drv_index != 0) { 2014 if (h->drv[drv_index]->raid_level != -1 && drv_index != 0) {
1974 printk(KERN_WARNING "disk %d has changed.\n", drv_index); 2015 dev_warn(&h->pdev->dev, "disk %d has changed.\n", drv_index);
1975 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags); 2016 spin_lock_irqsave(&h->lock, flags);
1976 h->drv[drv_index]->busy_configuring = 1; 2017 h->drv[drv_index]->busy_configuring = 1;
1977 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 2018 spin_unlock_irqrestore(&h->lock, flags);
1978 2019
1979 /* deregister_disk sets h->drv[drv_index]->queue = NULL 2020 /* deregister_disk sets h->drv[drv_index]->queue = NULL
1980 * which keeps the interrupt handler from starting 2021 * which keeps the interrupt handler from starting
@@ -2024,8 +2065,8 @@ static void cciss_update_drive_info(int ctlr, int drv_index, int first_time,
2024 if (cciss_add_disk(h, disk, drv_index) != 0) { 2065 if (cciss_add_disk(h, disk, drv_index) != 0) {
2025 cciss_free_gendisk(h, drv_index); 2066 cciss_free_gendisk(h, drv_index);
2026 cciss_free_drive_info(h, drv_index); 2067 cciss_free_drive_info(h, drv_index);
2027 printk(KERN_WARNING "cciss:%d could not update " 2068 dev_warn(&h->pdev->dev, "could not update disk %d\n",
2028 "disk %d\n", h->ctlr, drv_index); 2069 drv_index);
2029 --h->num_luns; 2070 --h->num_luns;
2030 } 2071 }
2031 } 2072 }
@@ -2035,7 +2076,7 @@ freeret:
2035 kfree(drvinfo); 2076 kfree(drvinfo);
2036 return; 2077 return;
2037mem_msg: 2078mem_msg:
2038 printk(KERN_ERR "cciss: out of memory\n"); 2079 dev_err(&h->pdev->dev, "out of memory\n");
2039 goto freeret; 2080 goto freeret;
2040} 2081}
2041 2082
@@ -2127,9 +2168,9 @@ static int cciss_add_gendisk(ctlr_info_t *h, unsigned char lunid[],
2127 h->gendisk[drv_index] = 2168 h->gendisk[drv_index] =
2128 alloc_disk(1 << NWD_SHIFT); 2169 alloc_disk(1 << NWD_SHIFT);
2129 if (!h->gendisk[drv_index]) { 2170 if (!h->gendisk[drv_index]) {
2130 printk(KERN_ERR "cciss%d: could not " 2171 dev_err(&h->pdev->dev,
2131 "allocate a new disk %d\n", 2172 "could not allocate a new disk %d\n",
2132 h->ctlr, drv_index); 2173 drv_index);
2133 goto err_free_drive_info; 2174 goto err_free_drive_info;
2134 } 2175 }
2135 } 2176 }
@@ -2180,8 +2221,7 @@ static void cciss_add_controller_node(ctlr_info_t *h)
2180 cciss_free_gendisk(h, drv_index); 2221 cciss_free_gendisk(h, drv_index);
2181 cciss_free_drive_info(h, drv_index); 2222 cciss_free_drive_info(h, drv_index);
2182error: 2223error:
2183 printk(KERN_WARNING "cciss%d: could not " 2224 dev_warn(&h->pdev->dev, "could not add disk 0.\n");
2184 "add disk 0.\n", h->ctlr);
2185 return; 2225 return;
2186} 2226}
2187 2227
@@ -2196,7 +2236,6 @@ error:
2196static int rebuild_lun_table(ctlr_info_t *h, int first_time, 2236static int rebuild_lun_table(ctlr_info_t *h, int first_time,
2197 int via_ioctl) 2237 int via_ioctl)
2198{ 2238{
2199 int ctlr = h->ctlr;
2200 int num_luns; 2239 int num_luns;
2201 ReportLunData_struct *ld_buff = NULL; 2240 ReportLunData_struct *ld_buff = NULL;
2202 int return_code; 2241 int return_code;
@@ -2211,27 +2250,27 @@ static int rebuild_lun_table(ctlr_info_t *h, int first_time,
2211 return -EPERM; 2250 return -EPERM;
2212 2251
2213 /* Set busy_configuring flag for this operation */ 2252 /* Set busy_configuring flag for this operation */
2214 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags); 2253 spin_lock_irqsave(&h->lock, flags);
2215 if (h->busy_configuring) { 2254 if (h->busy_configuring) {
2216 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 2255 spin_unlock_irqrestore(&h->lock, flags);
2217 return -EBUSY; 2256 return -EBUSY;
2218 } 2257 }
2219 h->busy_configuring = 1; 2258 h->busy_configuring = 1;
2220 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 2259 spin_unlock_irqrestore(&h->lock, flags);
2221 2260
2222 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL); 2261 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
2223 if (ld_buff == NULL) 2262 if (ld_buff == NULL)
2224 goto mem_msg; 2263 goto mem_msg;
2225 2264
2226 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff, 2265 return_code = sendcmd_withirq(h, CISS_REPORT_LOG, ld_buff,
2227 sizeof(ReportLunData_struct), 2266 sizeof(ReportLunData_struct),
2228 0, CTLR_LUNID, TYPE_CMD); 2267 0, CTLR_LUNID, TYPE_CMD);
2229 2268
2230 if (return_code == IO_OK) 2269 if (return_code == IO_OK)
2231 listlength = be32_to_cpu(*(__be32 *) ld_buff->LUNListLength); 2270 listlength = be32_to_cpu(*(__be32 *) ld_buff->LUNListLength);
2232 else { /* reading number of logical volumes failed */ 2271 else { /* reading number of logical volumes failed */
2233 printk(KERN_WARNING "cciss: report logical volume" 2272 dev_warn(&h->pdev->dev,
2234 " command failed\n"); 2273 "report logical volume command failed\n");
2235 listlength = 0; 2274 listlength = 0;
2236 goto freeret; 2275 goto freeret;
2237 } 2276 }
@@ -2239,7 +2278,7 @@ static int rebuild_lun_table(ctlr_info_t *h, int first_time,
2239 num_luns = listlength / 8; /* 8 bytes per entry */ 2278 num_luns = listlength / 8; /* 8 bytes per entry */
2240 if (num_luns > CISS_MAX_LUN) { 2279 if (num_luns > CISS_MAX_LUN) {
2241 num_luns = CISS_MAX_LUN; 2280 num_luns = CISS_MAX_LUN;
2242 printk(KERN_WARNING "cciss: more luns configured" 2281 dev_warn(&h->pdev->dev, "more luns configured"
2243 " on controller than can be handled by" 2282 " on controller than can be handled by"
2244 " this driver.\n"); 2283 " this driver.\n");
2245 } 2284 }
@@ -2270,9 +2309,9 @@ static int rebuild_lun_table(ctlr_info_t *h, int first_time,
2270 } 2309 }
2271 if (!drv_found) { 2310 if (!drv_found) {
2272 /* Deregister it from the OS, it's gone. */ 2311 /* Deregister it from the OS, it's gone. */
2273 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags); 2312 spin_lock_irqsave(&h->lock, flags);
2274 h->drv[i]->busy_configuring = 1; 2313 h->drv[i]->busy_configuring = 1;
2275 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 2314 spin_unlock_irqrestore(&h->lock, flags);
2276 return_code = deregister_disk(h, i, 1, via_ioctl); 2315 return_code = deregister_disk(h, i, 1, via_ioctl);
2277 if (h->drv[i] != NULL) 2316 if (h->drv[i] != NULL)
2278 h->drv[i]->busy_configuring = 0; 2317 h->drv[i]->busy_configuring = 0;
@@ -2311,8 +2350,7 @@ static int rebuild_lun_table(ctlr_info_t *h, int first_time,
2311 if (drv_index == -1) 2350 if (drv_index == -1)
2312 goto freeret; 2351 goto freeret;
2313 } 2352 }
2314 cciss_update_drive_info(ctlr, drv_index, first_time, 2353 cciss_update_drive_info(h, drv_index, first_time, via_ioctl);
2315 via_ioctl);
2316 } /* end for */ 2354 } /* end for */
2317 2355
2318freeret: 2356freeret:
@@ -2324,7 +2362,7 @@ freeret:
2324 */ 2362 */
2325 return -1; 2363 return -1;
2326mem_msg: 2364mem_msg:
2327 printk(KERN_ERR "cciss: out of memory\n"); 2365 dev_err(&h->pdev->dev, "out of memory\n");
2328 h->busy_configuring = 0; 2366 h->busy_configuring = 0;
2329 goto freeret; 2367 goto freeret;
2330} 2368}
@@ -2444,11 +2482,10 @@ static int deregister_disk(ctlr_info_t *h, int drv_index,
2444 return 0; 2482 return 0;
2445} 2483}
2446 2484
2447static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff, 2485static int fill_cmd(ctlr_info_t *h, CommandList_struct *c, __u8 cmd, void *buff,
2448 size_t size, __u8 page_code, unsigned char *scsi3addr, 2486 size_t size, __u8 page_code, unsigned char *scsi3addr,
2449 int cmd_type) 2487 int cmd_type)
2450{ 2488{
2451 ctlr_info_t *h = hba[ctlr];
2452 u64bit buff_dma_handle; 2489 u64bit buff_dma_handle;
2453 int status = IO_OK; 2490 int status = IO_OK;
2454 2491
@@ -2532,8 +2569,7 @@ static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
2532 c->Request.Timeout = 0; 2569 c->Request.Timeout = 0;
2533 break; 2570 break;
2534 default: 2571 default:
2535 printk(KERN_WARNING 2572 dev_warn(&h->pdev->dev, "Unknown Command 0x%c\n", cmd);
2536 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
2537 return IO_ERROR; 2573 return IO_ERROR;
2538 } 2574 }
2539 } else if (cmd_type == TYPE_MSG) { 2575 } else if (cmd_type == TYPE_MSG) {
@@ -2565,13 +2601,12 @@ static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
2565 c->Request.CDB[0] = cmd; 2601 c->Request.CDB[0] = cmd;
2566 break; 2602 break;
2567 default: 2603 default:
2568 printk(KERN_WARNING 2604 dev_warn(&h->pdev->dev,
2569 "cciss%d: unknown message type %d\n", ctlr, cmd); 2605 "unknown message type %d\n", cmd);
2570 return IO_ERROR; 2606 return IO_ERROR;
2571 } 2607 }
2572 } else { 2608 } else {
2573 printk(KERN_WARNING 2609 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
2574 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
2575 return IO_ERROR; 2610 return IO_ERROR;
2576 } 2611 }
2577 /* Fill in the scatter gather information */ 2612 /* Fill in the scatter gather information */
@@ -2599,15 +2634,14 @@ static int check_target_status(ctlr_info_t *h, CommandList_struct *c)
2599 default: 2634 default:
2600 if (check_for_unit_attention(h, c)) 2635 if (check_for_unit_attention(h, c))
2601 return IO_NEEDS_RETRY; 2636 return IO_NEEDS_RETRY;
2602 printk(KERN_WARNING "cciss%d: cmd 0x%02x " 2637 dev_warn(&h->pdev->dev, "cmd 0x%02x "
2603 "check condition, sense key = 0x%02x\n", 2638 "check condition, sense key = 0x%02x\n",
2604 h->ctlr, c->Request.CDB[0], 2639 c->Request.CDB[0], c->err_info->SenseInfo[2]);
2605 c->err_info->SenseInfo[2]);
2606 } 2640 }
2607 break; 2641 break;
2608 default: 2642 default:
2609 printk(KERN_WARNING "cciss%d: cmd 0x%02x" 2643 dev_warn(&h->pdev->dev, "cmd 0x%02x"
2610 "scsi status = 0x%02x\n", h->ctlr, 2644 "scsi status = 0x%02x\n",
2611 c->Request.CDB[0], c->err_info->ScsiStatus); 2645 c->Request.CDB[0], c->err_info->ScsiStatus);
2612 break; 2646 break;
2613 } 2647 }
@@ -2630,43 +2664,42 @@ static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c)
2630 /* expected for inquiry and report lun commands */ 2664 /* expected for inquiry and report lun commands */
2631 break; 2665 break;
2632 case CMD_INVALID: 2666 case CMD_INVALID:
2633 printk(KERN_WARNING "cciss: cmd 0x%02x is " 2667 dev_warn(&h->pdev->dev, "cmd 0x%02x is "
2634 "reported invalid\n", c->Request.CDB[0]); 2668 "reported invalid\n", c->Request.CDB[0]);
2635 return_status = IO_ERROR; 2669 return_status = IO_ERROR;
2636 break; 2670 break;
2637 case CMD_PROTOCOL_ERR: 2671 case CMD_PROTOCOL_ERR:
2638 printk(KERN_WARNING "cciss: cmd 0x%02x has " 2672 dev_warn(&h->pdev->dev, "cmd 0x%02x has "
2639 "protocol error \n", c->Request.CDB[0]); 2673 "protocol error\n", c->Request.CDB[0]);
2640 return_status = IO_ERROR; 2674 return_status = IO_ERROR;
2641 break; 2675 break;
2642 case CMD_HARDWARE_ERR: 2676 case CMD_HARDWARE_ERR:
2643 printk(KERN_WARNING "cciss: cmd 0x%02x had " 2677 dev_warn(&h->pdev->dev, "cmd 0x%02x had "
2644 " hardware error\n", c->Request.CDB[0]); 2678 " hardware error\n", c->Request.CDB[0]);
2645 return_status = IO_ERROR; 2679 return_status = IO_ERROR;
2646 break; 2680 break;
2647 case CMD_CONNECTION_LOST: 2681 case CMD_CONNECTION_LOST:
2648 printk(KERN_WARNING "cciss: cmd 0x%02x had " 2682 dev_warn(&h->pdev->dev, "cmd 0x%02x had "
2649 "connection lost\n", c->Request.CDB[0]); 2683 "connection lost\n", c->Request.CDB[0]);
2650 return_status = IO_ERROR; 2684 return_status = IO_ERROR;
2651 break; 2685 break;
2652 case CMD_ABORTED: 2686 case CMD_ABORTED:
2653 printk(KERN_WARNING "cciss: cmd 0x%02x was " 2687 dev_warn(&h->pdev->dev, "cmd 0x%02x was "
2654 "aborted\n", c->Request.CDB[0]); 2688 "aborted\n", c->Request.CDB[0]);
2655 return_status = IO_ERROR; 2689 return_status = IO_ERROR;
2656 break; 2690 break;
2657 case CMD_ABORT_FAILED: 2691 case CMD_ABORT_FAILED:
2658 printk(KERN_WARNING "cciss: cmd 0x%02x reports " 2692 dev_warn(&h->pdev->dev, "cmd 0x%02x reports "
2659 "abort failed\n", c->Request.CDB[0]); 2693 "abort failed\n", c->Request.CDB[0]);
2660 return_status = IO_ERROR; 2694 return_status = IO_ERROR;
2661 break; 2695 break;
2662 case CMD_UNSOLICITED_ABORT: 2696 case CMD_UNSOLICITED_ABORT:
2663 printk(KERN_WARNING 2697 dev_warn(&h->pdev->dev, "unsolicited abort 0x%02x\n",
2664 "cciss%d: unsolicited abort 0x%02x\n", h->ctlr,
2665 c->Request.CDB[0]); 2698 c->Request.CDB[0]);
2666 return_status = IO_NEEDS_RETRY; 2699 return_status = IO_NEEDS_RETRY;
2667 break; 2700 break;
2668 default: 2701 default:
2669 printk(KERN_WARNING "cciss: cmd 0x%02x returned " 2702 dev_warn(&h->pdev->dev, "cmd 0x%02x returned "
2670 "unknown status %x\n", c->Request.CDB[0], 2703 "unknown status %x\n", c->Request.CDB[0],
2671 c->err_info->CommandStatus); 2704 c->err_info->CommandStatus);
2672 return_status = IO_ERROR; 2705 return_status = IO_ERROR;
@@ -2679,17 +2712,11 @@ static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
2679{ 2712{
2680 DECLARE_COMPLETION_ONSTACK(wait); 2713 DECLARE_COMPLETION_ONSTACK(wait);
2681 u64bit buff_dma_handle; 2714 u64bit buff_dma_handle;
2682 unsigned long flags;
2683 int return_status = IO_OK; 2715 int return_status = IO_OK;
2684 2716
2685resend_cmd2: 2717resend_cmd2:
2686 c->waiting = &wait; 2718 c->waiting = &wait;
2687 /* Put the request on the tail of the queue and send it */ 2719 enqueue_cmd_and_start_io(h, c);
2688 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2689 addQ(&h->reqQ, c);
2690 h->Qdepth++;
2691 start_io(h);
2692 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2693 2720
2694 wait_for_completion(&wait); 2721 wait_for_completion(&wait);
2695 2722
@@ -2700,7 +2727,7 @@ resend_cmd2:
2700 2727
2701 if (return_status == IO_NEEDS_RETRY && 2728 if (return_status == IO_NEEDS_RETRY &&
2702 c->retry_count < MAX_CMD_RETRIES) { 2729 c->retry_count < MAX_CMD_RETRIES) {
2703 printk(KERN_WARNING "cciss%d: retrying 0x%02x\n", h->ctlr, 2730 dev_warn(&h->pdev->dev, "retrying 0x%02x\n",
2704 c->Request.CDB[0]); 2731 c->Request.CDB[0]);
2705 c->retry_count++; 2732 c->retry_count++;
2706 /* erase the old error information */ 2733 /* erase the old error information */
@@ -2719,27 +2746,26 @@ command_done:
2719 return return_status; 2746 return return_status;
2720} 2747}
2721 2748
2722static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size, 2749static int sendcmd_withirq(ctlr_info_t *h, __u8 cmd, void *buff, size_t size,
2723 __u8 page_code, unsigned char scsi3addr[], 2750 __u8 page_code, unsigned char scsi3addr[],
2724 int cmd_type) 2751 int cmd_type)
2725{ 2752{
2726 ctlr_info_t *h = hba[ctlr];
2727 CommandList_struct *c; 2753 CommandList_struct *c;
2728 int return_status; 2754 int return_status;
2729 2755
2730 c = cmd_alloc(h, 0); 2756 c = cmd_special_alloc(h);
2731 if (!c) 2757 if (!c)
2732 return -ENOMEM; 2758 return -ENOMEM;
2733 return_status = fill_cmd(c, cmd, ctlr, buff, size, page_code, 2759 return_status = fill_cmd(h, c, cmd, buff, size, page_code,
2734 scsi3addr, cmd_type); 2760 scsi3addr, cmd_type);
2735 if (return_status == IO_OK) 2761 if (return_status == IO_OK)
2736 return_status = sendcmd_withirq_core(h, c, 1); 2762 return_status = sendcmd_withirq_core(h, c, 1);
2737 2763
2738 cmd_free(h, c, 0); 2764 cmd_special_free(h, c);
2739 return return_status; 2765 return return_status;
2740} 2766}
2741 2767
2742static void cciss_geometry_inquiry(int ctlr, int logvol, 2768static void cciss_geometry_inquiry(ctlr_info_t *h, int logvol,
2743 sector_t total_size, 2769 sector_t total_size,
2744 unsigned int block_size, 2770 unsigned int block_size,
2745 InquiryData_struct *inq_buff, 2771 InquiryData_struct *inq_buff,
@@ -2750,13 +2776,13 @@ static void cciss_geometry_inquiry(int ctlr, int logvol,
2750 unsigned char scsi3addr[8]; 2776 unsigned char scsi3addr[8];
2751 2777
2752 memset(inq_buff, 0, sizeof(InquiryData_struct)); 2778 memset(inq_buff, 0, sizeof(InquiryData_struct));
2753 log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol); 2779 log_unit_to_scsi3addr(h, scsi3addr, logvol);
2754 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr, inq_buff, 2780 return_code = sendcmd_withirq(h, CISS_INQUIRY, inq_buff,
2755 sizeof(*inq_buff), 0xC1, scsi3addr, TYPE_CMD); 2781 sizeof(*inq_buff), 0xC1, scsi3addr, TYPE_CMD);
2756 if (return_code == IO_OK) { 2782 if (return_code == IO_OK) {
2757 if (inq_buff->data_byte[8] == 0xFF) { 2783 if (inq_buff->data_byte[8] == 0xFF) {
2758 printk(KERN_WARNING 2784 dev_warn(&h->pdev->dev,
2759 "cciss: reading geometry failed, volume " 2785 "reading geometry failed, volume "
2760 "does not support reading geometry\n"); 2786 "does not support reading geometry\n");
2761 drv->heads = 255; 2787 drv->heads = 255;
2762 drv->sectors = 32; /* Sectors per track */ 2788 drv->sectors = 32; /* Sectors per track */
@@ -2780,12 +2806,12 @@ static void cciss_geometry_inquiry(int ctlr, int logvol,
2780 drv->cylinders = real_size; 2806 drv->cylinders = real_size;
2781 } 2807 }
2782 } else { /* Get geometry failed */ 2808 } else { /* Get geometry failed */
2783 printk(KERN_WARNING "cciss: reading geometry failed\n"); 2809 dev_warn(&h->pdev->dev, "reading geometry failed\n");
2784 } 2810 }
2785} 2811}
2786 2812
2787static void 2813static void
2788cciss_read_capacity(int ctlr, int logvol, sector_t *total_size, 2814cciss_read_capacity(ctlr_info_t *h, int logvol, sector_t *total_size,
2789 unsigned int *block_size) 2815 unsigned int *block_size)
2790{ 2816{
2791 ReadCapdata_struct *buf; 2817 ReadCapdata_struct *buf;
@@ -2794,25 +2820,25 @@ cciss_read_capacity(int ctlr, int logvol, sector_t *total_size,
2794 2820
2795 buf = kzalloc(sizeof(ReadCapdata_struct), GFP_KERNEL); 2821 buf = kzalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
2796 if (!buf) { 2822 if (!buf) {
2797 printk(KERN_WARNING "cciss: out of memory\n"); 2823 dev_warn(&h->pdev->dev, "out of memory\n");
2798 return; 2824 return;
2799 } 2825 }
2800 2826
2801 log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol); 2827 log_unit_to_scsi3addr(h, scsi3addr, logvol);
2802 return_code = sendcmd_withirq(CCISS_READ_CAPACITY, ctlr, buf, 2828 return_code = sendcmd_withirq(h, CCISS_READ_CAPACITY, buf,
2803 sizeof(ReadCapdata_struct), 0, scsi3addr, TYPE_CMD); 2829 sizeof(ReadCapdata_struct), 0, scsi3addr, TYPE_CMD);
2804 if (return_code == IO_OK) { 2830 if (return_code == IO_OK) {
2805 *total_size = be32_to_cpu(*(__be32 *) buf->total_size); 2831 *total_size = be32_to_cpu(*(__be32 *) buf->total_size);
2806 *block_size = be32_to_cpu(*(__be32 *) buf->block_size); 2832 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
2807 } else { /* read capacity command failed */ 2833 } else { /* read capacity command failed */
2808 printk(KERN_WARNING "cciss: read capacity failed\n"); 2834 dev_warn(&h->pdev->dev, "read capacity failed\n");
2809 *total_size = 0; 2835 *total_size = 0;
2810 *block_size = BLOCK_SIZE; 2836 *block_size = BLOCK_SIZE;
2811 } 2837 }
2812 kfree(buf); 2838 kfree(buf);
2813} 2839}
2814 2840
2815static void cciss_read_capacity_16(int ctlr, int logvol, 2841static void cciss_read_capacity_16(ctlr_info_t *h, int logvol,
2816 sector_t *total_size, unsigned int *block_size) 2842 sector_t *total_size, unsigned int *block_size)
2817{ 2843{
2818 ReadCapdata_struct_16 *buf; 2844 ReadCapdata_struct_16 *buf;
@@ -2821,23 +2847,23 @@ static void cciss_read_capacity_16(int ctlr, int logvol,
2821 2847
2822 buf = kzalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL); 2848 buf = kzalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
2823 if (!buf) { 2849 if (!buf) {
2824 printk(KERN_WARNING "cciss: out of memory\n"); 2850 dev_warn(&h->pdev->dev, "out of memory\n");
2825 return; 2851 return;
2826 } 2852 }
2827 2853
2828 log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol); 2854 log_unit_to_scsi3addr(h, scsi3addr, logvol);
2829 return_code = sendcmd_withirq(CCISS_READ_CAPACITY_16, 2855 return_code = sendcmd_withirq(h, CCISS_READ_CAPACITY_16,
2830 ctlr, buf, sizeof(ReadCapdata_struct_16), 2856 buf, sizeof(ReadCapdata_struct_16),
2831 0, scsi3addr, TYPE_CMD); 2857 0, scsi3addr, TYPE_CMD);
2832 if (return_code == IO_OK) { 2858 if (return_code == IO_OK) {
2833 *total_size = be64_to_cpu(*(__be64 *) buf->total_size); 2859 *total_size = be64_to_cpu(*(__be64 *) buf->total_size);
2834 *block_size = be32_to_cpu(*(__be32 *) buf->block_size); 2860 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
2835 } else { /* read capacity command failed */ 2861 } else { /* read capacity command failed */
2836 printk(KERN_WARNING "cciss: read capacity failed\n"); 2862 dev_warn(&h->pdev->dev, "read capacity failed\n");
2837 *total_size = 0; 2863 *total_size = 0;
2838 *block_size = BLOCK_SIZE; 2864 *block_size = BLOCK_SIZE;
2839 } 2865 }
2840 printk(KERN_INFO " blocks= %llu block_size= %d\n", 2866 dev_info(&h->pdev->dev, " blocks= %llu block_size= %d\n",
2841 (unsigned long long)*total_size+1, *block_size); 2867 (unsigned long long)*total_size+1, *block_size);
2842 kfree(buf); 2868 kfree(buf);
2843} 2869}
@@ -2865,17 +2891,17 @@ static int cciss_revalidate(struct gendisk *disk)
2865 2891
2866 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL); 2892 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
2867 if (inq_buff == NULL) { 2893 if (inq_buff == NULL) {
2868 printk(KERN_WARNING "cciss: out of memory\n"); 2894 dev_warn(&h->pdev->dev, "out of memory\n");
2869 return 1; 2895 return 1;
2870 } 2896 }
2871 if (h->cciss_read == CCISS_READ_10) { 2897 if (h->cciss_read == CCISS_READ_10) {
2872 cciss_read_capacity(h->ctlr, logvol, 2898 cciss_read_capacity(h, logvol,
2873 &total_size, &block_size); 2899 &total_size, &block_size);
2874 } else { 2900 } else {
2875 cciss_read_capacity_16(h->ctlr, logvol, 2901 cciss_read_capacity_16(h, logvol,
2876 &total_size, &block_size); 2902 &total_size, &block_size);
2877 } 2903 }
2878 cciss_geometry_inquiry(h->ctlr, logvol, total_size, block_size, 2904 cciss_geometry_inquiry(h, logvol, total_size, block_size,
2879 inq_buff, drv); 2905 inq_buff, drv);
2880 2906
2881 blk_queue_logical_block_size(drv->queue, drv->block_size); 2907 blk_queue_logical_block_size(drv->queue, drv->block_size);
@@ -2909,7 +2935,7 @@ static void start_io(ctlr_info_t *h)
2909 c = hlist_entry(h->reqQ.first, CommandList_struct, list); 2935 c = hlist_entry(h->reqQ.first, CommandList_struct, list);
2910 /* can't do anything if fifo is full */ 2936 /* can't do anything if fifo is full */
2911 if ((h->access.fifo_full(h))) { 2937 if ((h->access.fifo_full(h))) {
2912 printk(KERN_WARNING "cciss: fifo full\n"); 2938 dev_warn(&h->pdev->dev, "fifo full\n");
2913 break; 2939 break;
2914 } 2940 }
2915 2941
@@ -2925,7 +2951,7 @@ static void start_io(ctlr_info_t *h)
2925 } 2951 }
2926} 2952}
2927 2953
2928/* Assumes that CCISS_LOCK(h->ctlr) is held. */ 2954/* Assumes that h->lock is held. */
2929/* Zeros out the error record and then resends the command back */ 2955/* Zeros out the error record and then resends the command back */
2930/* to the controller */ 2956/* to the controller */
2931static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c) 2957static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
@@ -2966,7 +2992,7 @@ static inline int evaluate_target_status(ctlr_info_t *h,
2966 driver_byte = DRIVER_OK; 2992 driver_byte = DRIVER_OK;
2967 msg_byte = cmd->err_info->CommandStatus; /* correct? seems too device specific */ 2993 msg_byte = cmd->err_info->CommandStatus; /* correct? seems too device specific */
2968 2994
2969 if (blk_pc_request(cmd->rq)) 2995 if (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC)
2970 host_byte = DID_PASSTHROUGH; 2996 host_byte = DID_PASSTHROUGH;
2971 else 2997 else
2972 host_byte = DID_OK; 2998 host_byte = DID_OK;
@@ -2975,8 +3001,8 @@ static inline int evaluate_target_status(ctlr_info_t *h,
2975 host_byte, driver_byte); 3001 host_byte, driver_byte);
2976 3002
2977 if (cmd->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) { 3003 if (cmd->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) {
2978 if (!blk_pc_request(cmd->rq)) 3004 if (cmd->rq->cmd_type != REQ_TYPE_BLOCK_PC)
2979 printk(KERN_WARNING "cciss: cmd %p " 3005 dev_warn(&h->pdev->dev, "cmd %p "
2980 "has SCSI Status 0x%x\n", 3006 "has SCSI Status 0x%x\n",
2981 cmd, cmd->err_info->ScsiStatus); 3007 cmd, cmd->err_info->ScsiStatus);
2982 return error_value; 3008 return error_value;
@@ -2985,17 +3011,19 @@ static inline int evaluate_target_status(ctlr_info_t *h,
2985 /* check the sense key */ 3011 /* check the sense key */
2986 sense_key = 0xf & cmd->err_info->SenseInfo[2]; 3012 sense_key = 0xf & cmd->err_info->SenseInfo[2];
2987 /* no status or recovered error */ 3013 /* no status or recovered error */
2988 if (((sense_key == 0x0) || (sense_key == 0x1)) && !blk_pc_request(cmd->rq)) 3014 if (((sense_key == 0x0) || (sense_key == 0x1)) &&
3015 (cmd->rq->cmd_type != REQ_TYPE_BLOCK_PC))
2989 error_value = 0; 3016 error_value = 0;
2990 3017
2991 if (check_for_unit_attention(h, cmd)) { 3018 if (check_for_unit_attention(h, cmd)) {
2992 *retry_cmd = !blk_pc_request(cmd->rq); 3019 *retry_cmd = !(cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC);
2993 return 0; 3020 return 0;
2994 } 3021 }
2995 3022
2996 if (!blk_pc_request(cmd->rq)) { /* Not SG_IO or similar? */ 3023 /* Not SG_IO or similar? */
3024 if (cmd->rq->cmd_type != REQ_TYPE_BLOCK_PC) {
2997 if (error_value != 0) 3025 if (error_value != 0)
2998 printk(KERN_WARNING "cciss: cmd %p has CHECK CONDITION" 3026 dev_warn(&h->pdev->dev, "cmd %p has CHECK CONDITION"
2999 " sense key = 0x%x\n", cmd, sense_key); 3027 " sense key = 0x%x\n", cmd, sense_key);
3000 return error_value; 3028 return error_value;
3001 } 3029 }
@@ -3035,90 +3063,97 @@ static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
3035 rq->errors = evaluate_target_status(h, cmd, &retry_cmd); 3063 rq->errors = evaluate_target_status(h, cmd, &retry_cmd);
3036 break; 3064 break;
3037 case CMD_DATA_UNDERRUN: 3065 case CMD_DATA_UNDERRUN:
3038 if (blk_fs_request(cmd->rq)) { 3066 if (cmd->rq->cmd_type == REQ_TYPE_FS) {
3039 printk(KERN_WARNING "cciss: cmd %p has" 3067 dev_warn(&h->pdev->dev, "cmd %p has"
3040 " completed with data underrun " 3068 " completed with data underrun "
3041 "reported\n", cmd); 3069 "reported\n", cmd);
3042 cmd->rq->resid_len = cmd->err_info->ResidualCnt; 3070 cmd->rq->resid_len = cmd->err_info->ResidualCnt;
3043 } 3071 }
3044 break; 3072 break;
3045 case CMD_DATA_OVERRUN: 3073 case CMD_DATA_OVERRUN:
3046 if (blk_fs_request(cmd->rq)) 3074 if (cmd->rq->cmd_type == REQ_TYPE_FS)
3047 printk(KERN_WARNING "cciss: cmd %p has" 3075 dev_warn(&h->pdev->dev, "cciss: cmd %p has"
3048 " completed with data overrun " 3076 " completed with data overrun "
3049 "reported\n", cmd); 3077 "reported\n", cmd);
3050 break; 3078 break;
3051 case CMD_INVALID: 3079 case CMD_INVALID:
3052 printk(KERN_WARNING "cciss: cmd %p is " 3080 dev_warn(&h->pdev->dev, "cciss: cmd %p is "
3053 "reported invalid\n", cmd); 3081 "reported invalid\n", cmd);
3054 rq->errors = make_status_bytes(SAM_STAT_GOOD, 3082 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3055 cmd->err_info->CommandStatus, DRIVER_OK, 3083 cmd->err_info->CommandStatus, DRIVER_OK,
3056 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR); 3084 (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
3085 DID_PASSTHROUGH : DID_ERROR);
3057 break; 3086 break;
3058 case CMD_PROTOCOL_ERR: 3087 case CMD_PROTOCOL_ERR:
3059 printk(KERN_WARNING "cciss: cmd %p has " 3088 dev_warn(&h->pdev->dev, "cciss: cmd %p has "
3060 "protocol error \n", cmd); 3089 "protocol error\n", cmd);
3061 rq->errors = make_status_bytes(SAM_STAT_GOOD, 3090 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3062 cmd->err_info->CommandStatus, DRIVER_OK, 3091 cmd->err_info->CommandStatus, DRIVER_OK,
3063 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR); 3092 (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
3093 DID_PASSTHROUGH : DID_ERROR);
3064 break; 3094 break;
3065 case CMD_HARDWARE_ERR: 3095 case CMD_HARDWARE_ERR:
3066 printk(KERN_WARNING "cciss: cmd %p had " 3096 dev_warn(&h->pdev->dev, "cciss: cmd %p had "
3067 " hardware error\n", cmd); 3097 " hardware error\n", cmd);
3068 rq->errors = make_status_bytes(SAM_STAT_GOOD, 3098 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3069 cmd->err_info->CommandStatus, DRIVER_OK, 3099 cmd->err_info->CommandStatus, DRIVER_OK,
3070 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR); 3100 (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
3101 DID_PASSTHROUGH : DID_ERROR);
3071 break; 3102 break;
3072 case CMD_CONNECTION_LOST: 3103 case CMD_CONNECTION_LOST:
3073 printk(KERN_WARNING "cciss: cmd %p had " 3104 dev_warn(&h->pdev->dev, "cciss: cmd %p had "
3074 "connection lost\n", cmd); 3105 "connection lost\n", cmd);
3075 rq->errors = make_status_bytes(SAM_STAT_GOOD, 3106 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3076 cmd->err_info->CommandStatus, DRIVER_OK, 3107 cmd->err_info->CommandStatus, DRIVER_OK,
3077 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR); 3108 (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
3109 DID_PASSTHROUGH : DID_ERROR);
3078 break; 3110 break;
3079 case CMD_ABORTED: 3111 case CMD_ABORTED:
3080 printk(KERN_WARNING "cciss: cmd %p was " 3112 dev_warn(&h->pdev->dev, "cciss: cmd %p was "
3081 "aborted\n", cmd); 3113 "aborted\n", cmd);
3082 rq->errors = make_status_bytes(SAM_STAT_GOOD, 3114 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3083 cmd->err_info->CommandStatus, DRIVER_OK, 3115 cmd->err_info->CommandStatus, DRIVER_OK,
3084 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT); 3116 (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
3117 DID_PASSTHROUGH : DID_ABORT);
3085 break; 3118 break;
3086 case CMD_ABORT_FAILED: 3119 case CMD_ABORT_FAILED:
3087 printk(KERN_WARNING "cciss: cmd %p reports " 3120 dev_warn(&h->pdev->dev, "cciss: cmd %p reports "
3088 "abort failed\n", cmd); 3121 "abort failed\n", cmd);
3089 rq->errors = make_status_bytes(SAM_STAT_GOOD, 3122 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3090 cmd->err_info->CommandStatus, DRIVER_OK, 3123 cmd->err_info->CommandStatus, DRIVER_OK,
3091 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR); 3124 (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
3125 DID_PASSTHROUGH : DID_ERROR);
3092 break; 3126 break;
3093 case CMD_UNSOLICITED_ABORT: 3127 case CMD_UNSOLICITED_ABORT:
3094 printk(KERN_WARNING "cciss%d: unsolicited " 3128 dev_warn(&h->pdev->dev, "cciss%d: unsolicited "
3095 "abort %p\n", h->ctlr, cmd); 3129 "abort %p\n", h->ctlr, cmd);
3096 if (cmd->retry_count < MAX_CMD_RETRIES) { 3130 if (cmd->retry_count < MAX_CMD_RETRIES) {
3097 retry_cmd = 1; 3131 retry_cmd = 1;
3098 printk(KERN_WARNING 3132 dev_warn(&h->pdev->dev, "retrying %p\n", cmd);
3099 "cciss%d: retrying %p\n", h->ctlr, cmd);
3100 cmd->retry_count++; 3133 cmd->retry_count++;
3101 } else 3134 } else
3102 printk(KERN_WARNING 3135 dev_warn(&h->pdev->dev,
3103 "cciss%d: %p retried too " 3136 "%p retried too many times\n", cmd);
3104 "many times\n", h->ctlr, cmd);
3105 rq->errors = make_status_bytes(SAM_STAT_GOOD, 3137 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3106 cmd->err_info->CommandStatus, DRIVER_OK, 3138 cmd->err_info->CommandStatus, DRIVER_OK,
3107 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT); 3139 (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
3140 DID_PASSTHROUGH : DID_ABORT);
3108 break; 3141 break;
3109 case CMD_TIMEOUT: 3142 case CMD_TIMEOUT:
3110 printk(KERN_WARNING "cciss: cmd %p timedout\n", cmd); 3143 dev_warn(&h->pdev->dev, "cmd %p timedout\n", cmd);
3111 rq->errors = make_status_bytes(SAM_STAT_GOOD, 3144 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3112 cmd->err_info->CommandStatus, DRIVER_OK, 3145 cmd->err_info->CommandStatus, DRIVER_OK,
3113 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR); 3146 (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
3147 DID_PASSTHROUGH : DID_ERROR);
3114 break; 3148 break;
3115 default: 3149 default:
3116 printk(KERN_WARNING "cciss: cmd %p returned " 3150 dev_warn(&h->pdev->dev, "cmd %p returned "
3117 "unknown status %x\n", cmd, 3151 "unknown status %x\n", cmd,
3118 cmd->err_info->CommandStatus); 3152 cmd->err_info->CommandStatus);
3119 rq->errors = make_status_bytes(SAM_STAT_GOOD, 3153 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3120 cmd->err_info->CommandStatus, DRIVER_OK, 3154 cmd->err_info->CommandStatus, DRIVER_OK,
3121 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR); 3155 (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
3156 DID_PASSTHROUGH : DID_ERROR);
3122 } 3157 }
3123 3158
3124after_error_processing: 3159after_error_processing:
@@ -3132,6 +3167,34 @@ after_error_processing:
3132 blk_complete_request(cmd->rq); 3167 blk_complete_request(cmd->rq);
3133} 3168}
3134 3169
3170static inline u32 cciss_tag_contains_index(u32 tag)
3171{
3172#define DIRECT_LOOKUP_BIT 0x10
3173 return tag & DIRECT_LOOKUP_BIT;
3174}
3175
3176static inline u32 cciss_tag_to_index(u32 tag)
3177{
3178#define DIRECT_LOOKUP_SHIFT 5
3179 return tag >> DIRECT_LOOKUP_SHIFT;
3180}
3181
3182static inline u32 cciss_tag_discard_error_bits(u32 tag)
3183{
3184#define CCISS_ERROR_BITS 0x03
3185 return tag & ~CCISS_ERROR_BITS;
3186}
3187
3188static inline void cciss_mark_tag_indexed(u32 *tag)
3189{
3190 *tag |= DIRECT_LOOKUP_BIT;
3191}
3192
3193static inline void cciss_set_tag_index(u32 *tag, u32 index)
3194{
3195 *tag |= (index << DIRECT_LOOKUP_SHIFT);
3196}
3197
3135/* 3198/*
3136 * Get a request and submit it to the controller. 3199 * Get a request and submit it to the controller.
3137 */ 3200 */
@@ -3163,7 +3226,8 @@ static void do_cciss_request(struct request_queue *q)
3163 3226
3164 BUG_ON(creq->nr_phys_segments > h->maxsgentries); 3227 BUG_ON(creq->nr_phys_segments > h->maxsgentries);
3165 3228
3166 if ((c = cmd_alloc(h, 1)) == NULL) 3229 c = cmd_alloc(h);
3230 if (!c)
3167 goto full; 3231 goto full;
3168 3232
3169 blk_start_request(creq); 3233 blk_start_request(creq);
@@ -3180,8 +3244,8 @@ static void do_cciss_request(struct request_queue *q)
3180 /* got command from pool, so use the command block index instead */ 3244 /* got command from pool, so use the command block index instead */
3181 /* for direct lookups. */ 3245 /* for direct lookups. */
3182 /* The first 2 bits are reserved for controller error reporting. */ 3246 /* The first 2 bits are reserved for controller error reporting. */
3183 c->Header.Tag.lower = (c->cmdindex << 3); 3247 cciss_set_tag_index(&c->Header.Tag.lower, c->cmdindex);
3184 c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */ 3248 cciss_mark_tag_indexed(&c->Header.Tag.lower);
3185 memcpy(&c->Header.LUN, drv->LunID, sizeof(drv->LunID)); 3249 memcpy(&c->Header.LUN, drv->LunID, sizeof(drv->LunID));
3186 c->Request.CDBLen = 10; /* 12 byte commands not in FW yet; */ 3250 c->Request.CDBLen = 10; /* 12 byte commands not in FW yet; */
3187 c->Request.Type.Type = TYPE_CMD; /* It is a command. */ 3251 c->Request.Type.Type = TYPE_CMD; /* It is a command. */
@@ -3192,11 +3256,8 @@ static void do_cciss_request(struct request_queue *q)
3192 c->Request.CDB[0] = 3256 c->Request.CDB[0] =
3193 (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write; 3257 (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
3194 start_blk = blk_rq_pos(creq); 3258 start_blk = blk_rq_pos(creq);
3195#ifdef CCISS_DEBUG 3259 dev_dbg(&h->pdev->dev, "sector =%d nr_sectors=%d\n",
3196 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",
3197 (int)blk_rq_pos(creq), (int)blk_rq_sectors(creq)); 3260 (int)blk_rq_pos(creq), (int)blk_rq_sectors(creq));
3198#endif /* CCISS_DEBUG */
3199
3200 sg_init_table(tmp_sg, h->maxsgentries); 3261 sg_init_table(tmp_sg, h->maxsgentries);
3201 seg = blk_rq_map_sg(q, creq, tmp_sg); 3262 seg = blk_rq_map_sg(q, creq, tmp_sg);
3202 3263
@@ -3236,17 +3297,18 @@ static void do_cciss_request(struct request_queue *q)
3236 if (seg > h->maxSG) 3297 if (seg > h->maxSG)
3237 h->maxSG = seg; 3298 h->maxSG = seg;
3238 3299
3239#ifdef CCISS_DEBUG 3300 dev_dbg(&h->pdev->dev, "Submitting %u sectors in %d segments "
3240 printk(KERN_DEBUG "cciss: Submitting %ld sectors in %d segments "
3241 "chained[%d]\n", 3301 "chained[%d]\n",
3242 blk_rq_sectors(creq), seg, chained); 3302 blk_rq_sectors(creq), seg, chained);
3243#endif /* CCISS_DEBUG */
3244 3303
3245 c->Header.SGList = c->Header.SGTotal = seg + chained; 3304 c->Header.SGTotal = seg + chained;
3246 if (seg > h->max_cmd_sgentries) 3305 if (seg <= h->max_cmd_sgentries)
3306 c->Header.SGList = c->Header.SGTotal;
3307 else
3247 c->Header.SGList = h->max_cmd_sgentries; 3308 c->Header.SGList = h->max_cmd_sgentries;
3309 set_performant_mode(h, c);
3248 3310
3249 if (likely(blk_fs_request(creq))) { 3311 if (likely(creq->cmd_type == REQ_TYPE_FS)) {
3250 if(h->cciss_read == CCISS_READ_10) { 3312 if(h->cciss_read == CCISS_READ_10) {
3251 c->Request.CDB[1] = 0; 3313 c->Request.CDB[1] = 0;
3252 c->Request.CDB[2] = (start_blk >> 24) & 0xff; /* MSB */ 3314 c->Request.CDB[2] = (start_blk >> 24) & 0xff; /* MSB */
@@ -3276,11 +3338,12 @@ static void do_cciss_request(struct request_queue *q)
3276 c->Request.CDB[13]= blk_rq_sectors(creq) & 0xff; 3338 c->Request.CDB[13]= blk_rq_sectors(creq) & 0xff;
3277 c->Request.CDB[14] = c->Request.CDB[15] = 0; 3339 c->Request.CDB[14] = c->Request.CDB[15] = 0;
3278 } 3340 }
3279 } else if (blk_pc_request(creq)) { 3341 } else if (creq->cmd_type == REQ_TYPE_BLOCK_PC) {
3280 c->Request.CDBLen = creq->cmd_len; 3342 c->Request.CDBLen = creq->cmd_len;
3281 memcpy(c->Request.CDB, creq->cmd, BLK_MAX_CDB); 3343 memcpy(c->Request.CDB, creq->cmd, BLK_MAX_CDB);
3282 } else { 3344 } else {
3283 printk(KERN_WARNING "cciss%d: bad request type %d\n", h->ctlr, creq->cmd_type); 3345 dev_warn(&h->pdev->dev, "bad request type %d\n",
3346 creq->cmd_type);
3284 BUG(); 3347 BUG();
3285 } 3348 }
3286 3349
@@ -3313,72 +3376,131 @@ static inline int interrupt_pending(ctlr_info_t *h)
3313 3376
3314static inline long interrupt_not_for_us(ctlr_info_t *h) 3377static inline long interrupt_not_for_us(ctlr_info_t *h)
3315{ 3378{
3316 return (((h->access.intr_pending(h) == 0) || 3379 return ((h->access.intr_pending(h) == 0) ||
3317 (h->interrupts_enabled == 0))); 3380 (h->interrupts_enabled == 0));
3318} 3381}
3319 3382
3320static irqreturn_t do_cciss_intr(int irq, void *dev_id) 3383static inline int bad_tag(ctlr_info_t *h, u32 tag_index,
3384 u32 raw_tag)
3321{ 3385{
3322 ctlr_info_t *h = dev_id; 3386 if (unlikely(tag_index >= h->nr_cmds)) {
3387 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
3388 return 1;
3389 }
3390 return 0;
3391}
3392
3393static inline void finish_cmd(ctlr_info_t *h, CommandList_struct *c,
3394 u32 raw_tag)
3395{
3396 removeQ(c);
3397 if (likely(c->cmd_type == CMD_RWREQ))
3398 complete_command(h, c, 0);
3399 else if (c->cmd_type == CMD_IOCTL_PEND)
3400 complete(c->waiting);
3401#ifdef CONFIG_CISS_SCSI_TAPE
3402 else if (c->cmd_type == CMD_SCSI)
3403 complete_scsi_command(c, 0, raw_tag);
3404#endif
3405}
3406
3407static inline u32 next_command(ctlr_info_t *h)
3408{
3409 u32 a;
3410
3411 if (unlikely(h->transMethod != CFGTBL_Trans_Performant))
3412 return h->access.command_completed(h);
3413
3414 if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
3415 a = *(h->reply_pool_head); /* Next cmd in ring buffer */
3416 (h->reply_pool_head)++;
3417 h->commands_outstanding--;
3418 } else {
3419 a = FIFO_EMPTY;
3420 }
3421 /* Check for wraparound */
3422 if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
3423 h->reply_pool_head = h->reply_pool;
3424 h->reply_pool_wraparound ^= 1;
3425 }
3426 return a;
3427}
3428
3429/* process completion of an indexed ("direct lookup") command */
3430static inline u32 process_indexed_cmd(ctlr_info_t *h, u32 raw_tag)
3431{
3432 u32 tag_index;
3323 CommandList_struct *c; 3433 CommandList_struct *c;
3434
3435 tag_index = cciss_tag_to_index(raw_tag);
3436 if (bad_tag(h, tag_index, raw_tag))
3437 return next_command(h);
3438 c = h->cmd_pool + tag_index;
3439 finish_cmd(h, c, raw_tag);
3440 return next_command(h);
3441}
3442
3443/* process completion of a non-indexed command */
3444static inline u32 process_nonindexed_cmd(ctlr_info_t *h, u32 raw_tag)
3445{
3446 u32 tag;
3447 CommandList_struct *c = NULL;
3448 struct hlist_node *tmp;
3449 __u32 busaddr_masked, tag_masked;
3450
3451 tag = cciss_tag_discard_error_bits(raw_tag);
3452 hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
3453 busaddr_masked = cciss_tag_discard_error_bits(c->busaddr);
3454 tag_masked = cciss_tag_discard_error_bits(tag);
3455 if (busaddr_masked == tag_masked) {
3456 finish_cmd(h, c, raw_tag);
3457 return next_command(h);
3458 }
3459 }
3460 bad_tag(h, h->nr_cmds + 1, raw_tag);
3461 return next_command(h);
3462}
3463
3464static irqreturn_t do_cciss_intx(int irq, void *dev_id)
3465{
3466 ctlr_info_t *h = dev_id;
3324 unsigned long flags; 3467 unsigned long flags;
3325 __u32 a, a1, a2; 3468 u32 raw_tag;
3326 3469
3327 if (interrupt_not_for_us(h)) 3470 if (interrupt_not_for_us(h))
3328 return IRQ_NONE; 3471 return IRQ_NONE;
3329 /* 3472 spin_lock_irqsave(&h->lock, flags);
3330 * If there are completed commands in the completion queue,
3331 * we had better do something about it.
3332 */
3333 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
3334 while (interrupt_pending(h)) { 3473 while (interrupt_pending(h)) {
3335 while ((a = get_next_completion(h)) != FIFO_EMPTY) { 3474 raw_tag = get_next_completion(h);
3336 a1 = a; 3475 while (raw_tag != FIFO_EMPTY) {
3337 if ((a & 0x04)) { 3476 if (cciss_tag_contains_index(raw_tag))
3338 a2 = (a >> 3); 3477 raw_tag = process_indexed_cmd(h, raw_tag);
3339 if (a2 >= h->nr_cmds) { 3478 else
3340 printk(KERN_WARNING 3479 raw_tag = process_nonindexed_cmd(h, raw_tag);
3341 "cciss: controller cciss%d failed, stopping.\n",
3342 h->ctlr);
3343 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
3344 fail_all_cmds(h->ctlr);
3345 return IRQ_HANDLED;
3346 }
3347
3348 c = h->cmd_pool + a2;
3349 a = c->busaddr;
3350
3351 } else {
3352 struct hlist_node *tmp;
3353
3354 a &= ~3;
3355 c = NULL;
3356 hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
3357 if (c->busaddr == a)
3358 break;
3359 }
3360 }
3361 /*
3362 * If we've found the command, take it off the
3363 * completion Q and free it
3364 */
3365 if (c && c->busaddr == a) {
3366 removeQ(c);
3367 if (c->cmd_type == CMD_RWREQ) {
3368 complete_command(h, c, 0);
3369 } else if (c->cmd_type == CMD_IOCTL_PEND) {
3370 complete(c->waiting);
3371 }
3372# ifdef CONFIG_CISS_SCSI_TAPE
3373 else if (c->cmd_type == CMD_SCSI)
3374 complete_scsi_command(c, 0, a1);
3375# endif
3376 continue;
3377 }
3378 } 3480 }
3379 } 3481 }
3482 spin_unlock_irqrestore(&h->lock, flags);
3483 return IRQ_HANDLED;
3484}
3380 3485
3381 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags); 3486/* Add a second interrupt handler for MSI/MSI-X mode. In this mode we never
3487 * check the interrupt pending register because it is not set.
3488 */
3489static irqreturn_t do_cciss_msix_intr(int irq, void *dev_id)
3490{
3491 ctlr_info_t *h = dev_id;
3492 unsigned long flags;
3493 u32 raw_tag;
3494
3495 spin_lock_irqsave(&h->lock, flags);
3496 raw_tag = get_next_completion(h);
3497 while (raw_tag != FIFO_EMPTY) {
3498 if (cciss_tag_contains_index(raw_tag))
3499 raw_tag = process_indexed_cmd(h, raw_tag);
3500 else
3501 raw_tag = process_nonindexed_cmd(h, raw_tag);
3502 }
3503 spin_unlock_irqrestore(&h->lock, flags);
3382 return IRQ_HANDLED; 3504 return IRQ_HANDLED;
3383} 3505}
3384 3506
@@ -3510,18 +3632,17 @@ static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c)
3510 3632
3511 switch (c->err_info->SenseInfo[12]) { 3633 switch (c->err_info->SenseInfo[12]) {
3512 case STATE_CHANGED: 3634 case STATE_CHANGED:
3513 printk(KERN_WARNING "cciss%d: a state change " 3635 dev_warn(&h->pdev->dev, "a state change "
3514 "detected, command retried\n", h->ctlr); 3636 "detected, command retried\n");
3515 return 1; 3637 return 1;
3516 break; 3638 break;
3517 case LUN_FAILED: 3639 case LUN_FAILED:
3518 printk(KERN_WARNING "cciss%d: LUN failure " 3640 dev_warn(&h->pdev->dev, "LUN failure "
3519 "detected, action required\n", h->ctlr); 3641 "detected, action required\n");
3520 return 1; 3642 return 1;
3521 break; 3643 break;
3522 case REPORT_LUNS_CHANGED: 3644 case REPORT_LUNS_CHANGED:
3523 printk(KERN_WARNING "cciss%d: report LUN data " 3645 dev_warn(&h->pdev->dev, "report LUN data changed\n");
3524 "changed\n", h->ctlr);
3525 /* 3646 /*
3526 * Here, we could call add_to_scan_list and wake up the scan thread, 3647 * Here, we could call add_to_scan_list and wake up the scan thread,
3527 * except that it's quite likely that we will get more than one 3648 * except that it's quite likely that we will get more than one
@@ -3541,19 +3662,18 @@ static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c)
3541 return 1; 3662 return 1;
3542 break; 3663 break;
3543 case POWER_OR_RESET: 3664 case POWER_OR_RESET:
3544 printk(KERN_WARNING "cciss%d: a power on " 3665 dev_warn(&h->pdev->dev,
3545 "or device reset detected\n", h->ctlr); 3666 "a power on or device reset detected\n");
3546 return 1; 3667 return 1;
3547 break; 3668 break;
3548 case UNIT_ATTENTION_CLEARED: 3669 case UNIT_ATTENTION_CLEARED:
3549 printk(KERN_WARNING "cciss%d: unit attention " 3670 dev_warn(&h->pdev->dev,
3550 "cleared by another initiator\n", h->ctlr); 3671 "unit attention cleared by another initiator\n");
3551 return 1; 3672 return 1;
3552 break; 3673 break;
3553 default: 3674 default:
3554 printk(KERN_WARNING "cciss%d: unknown " 3675 dev_warn(&h->pdev->dev, "unknown unit attention detected\n");
3555 "unit attention detected\n", h->ctlr); 3676 return 1;
3556 return 1;
3557 } 3677 }
3558} 3678}
3559 3679
@@ -3562,39 +3682,41 @@ static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c)
3562 * the io functions. 3682 * the io functions.
3563 * This is for debug only. 3683 * This is for debug only.
3564 */ 3684 */
3565#ifdef CCISS_DEBUG 3685static void print_cfg_table(ctlr_info_t *h)
3566static void print_cfg_table(CfgTable_struct *tb)
3567{ 3686{
3568 int i; 3687 int i;
3569 char temp_name[17]; 3688 char temp_name[17];
3689 CfgTable_struct *tb = h->cfgtable;
3570 3690
3571 printk("Controller Configuration information\n"); 3691 dev_dbg(&h->pdev->dev, "Controller Configuration information\n");
3572 printk("------------------------------------\n"); 3692 dev_dbg(&h->pdev->dev, "------------------------------------\n");
3573 for (i = 0; i < 4; i++) 3693 for (i = 0; i < 4; i++)
3574 temp_name[i] = readb(&(tb->Signature[i])); 3694 temp_name[i] = readb(&(tb->Signature[i]));
3575 temp_name[4] = '\0'; 3695 temp_name[4] = '\0';
3576 printk(" Signature = %s\n", temp_name); 3696 dev_dbg(&h->pdev->dev, " Signature = %s\n", temp_name);
3577 printk(" Spec Number = %d\n", readl(&(tb->SpecValence))); 3697 dev_dbg(&h->pdev->dev, " Spec Number = %d\n",
3578 printk(" Transport methods supported = 0x%x\n", 3698 readl(&(tb->SpecValence)));
3699 dev_dbg(&h->pdev->dev, " Transport methods supported = 0x%x\n",
3579 readl(&(tb->TransportSupport))); 3700 readl(&(tb->TransportSupport)));
3580 printk(" Transport methods active = 0x%x\n", 3701 dev_dbg(&h->pdev->dev, " Transport methods active = 0x%x\n",
3581 readl(&(tb->TransportActive))); 3702 readl(&(tb->TransportActive)));
3582 printk(" Requested transport Method = 0x%x\n", 3703 dev_dbg(&h->pdev->dev, " Requested transport Method = 0x%x\n",
3583 readl(&(tb->HostWrite.TransportRequest))); 3704 readl(&(tb->HostWrite.TransportRequest)));
3584 printk(" Coalesce Interrupt Delay = 0x%x\n", 3705 dev_dbg(&h->pdev->dev, " Coalesce Interrupt Delay = 0x%x\n",
3585 readl(&(tb->HostWrite.CoalIntDelay))); 3706 readl(&(tb->HostWrite.CoalIntDelay)));
3586 printk(" Coalesce Interrupt Count = 0x%x\n", 3707 dev_dbg(&h->pdev->dev, " Coalesce Interrupt Count = 0x%x\n",
3587 readl(&(tb->HostWrite.CoalIntCount))); 3708 readl(&(tb->HostWrite.CoalIntCount)));
3588 printk(" Max outstanding commands = 0x%d\n", 3709 dev_dbg(&h->pdev->dev, " Max outstanding commands = 0x%d\n",
3589 readl(&(tb->CmdsOutMax))); 3710 readl(&(tb->CmdsOutMax)));
3590 printk(" Bus Types = 0x%x\n", readl(&(tb->BusTypes))); 3711 dev_dbg(&h->pdev->dev, " Bus Types = 0x%x\n",
3712 readl(&(tb->BusTypes)));
3591 for (i = 0; i < 16; i++) 3713 for (i = 0; i < 16; i++)
3592 temp_name[i] = readb(&(tb->ServerName[i])); 3714 temp_name[i] = readb(&(tb->ServerName[i]));
3593 temp_name[16] = '\0'; 3715 temp_name[16] = '\0';
3594 printk(" Server Name = %s\n", temp_name); 3716 dev_dbg(&h->pdev->dev, " Server Name = %s\n", temp_name);
3595 printk(" Heartbeat Counter = 0x%x\n\n\n", readl(&(tb->HeartBeat))); 3717 dev_dbg(&h->pdev->dev, " Heartbeat Counter = 0x%x\n\n\n",
3718 readl(&(tb->HeartBeat)));
3596} 3719}
3597#endif /* CCISS_DEBUG */
3598 3720
3599static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr) 3721static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3600{ 3722{
@@ -3618,7 +3740,7 @@ static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3618 offset += 8; 3740 offset += 8;
3619 break; 3741 break;
3620 default: /* reserved in PCI 2.2 */ 3742 default: /* reserved in PCI 2.2 */
3621 printk(KERN_WARNING 3743 dev_warn(&pdev->dev,
3622 "Base address is invalid\n"); 3744 "Base address is invalid\n");
3623 return -1; 3745 return -1;
3624 break; 3746 break;
@@ -3630,12 +3752,182 @@ static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3630 return -1; 3752 return -1;
3631} 3753}
3632 3754
3755/* Fill in bucket_map[], given nsgs (the max number of
3756 * scatter gather elements supported) and bucket[],
3757 * which is an array of 8 integers. The bucket[] array
3758 * contains 8 different DMA transfer sizes (in 16
3759 * byte increments) which the controller uses to fetch
3760 * commands. This function fills in bucket_map[], which
3761 * maps a given number of scatter gather elements to one of
3762 * the 8 DMA transfer sizes. The point of it is to allow the
3763 * controller to only do as much DMA as needed to fetch the
3764 * command, with the DMA transfer size encoded in the lower
3765 * bits of the command address.
3766 */
3767static void calc_bucket_map(int bucket[], int num_buckets,
3768 int nsgs, int *bucket_map)
3769{
3770 int i, j, b, size;
3771
3772 /* even a command with 0 SGs requires 4 blocks */
3773#define MINIMUM_TRANSFER_BLOCKS 4
3774#define NUM_BUCKETS 8
3775 /* Note, bucket_map must have nsgs+1 entries. */
3776 for (i = 0; i <= nsgs; i++) {
3777 /* Compute size of a command with i SG entries */
3778 size = i + MINIMUM_TRANSFER_BLOCKS;
3779 b = num_buckets; /* Assume the biggest bucket */
3780 /* Find the bucket that is just big enough */
3781 for (j = 0; j < 8; j++) {
3782 if (bucket[j] >= size) {
3783 b = j;
3784 break;
3785 }
3786 }
3787 /* for a command with i SG entries, use bucket b. */
3788 bucket_map[i] = b;
3789 }
3790}
3791
3792static void __devinit cciss_wait_for_mode_change_ack(ctlr_info_t *h)
3793{
3794 int i;
3795
3796 /* under certain very rare conditions, this can take awhile.
3797 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3798 * as we enter this code.) */
3799 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
3800 if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3801 break;
3802 msleep(10);
3803 }
3804}
3805
3806static __devinit void cciss_enter_performant_mode(ctlr_info_t *h)
3807{
3808 /* This is a bit complicated. There are 8 registers on
3809 * the controller which we write to to tell it 8 different
3810 * sizes of commands which there may be. It's a way of
3811 * reducing the DMA done to fetch each command. Encoded into
3812 * each command's tag are 3 bits which communicate to the controller
3813 * which of the eight sizes that command fits within. The size of
3814 * each command depends on how many scatter gather entries there are.
3815 * Each SG entry requires 16 bytes. The eight registers are programmed
3816 * with the number of 16-byte blocks a command of that size requires.
3817 * The smallest command possible requires 5 such 16 byte blocks.
3818 * the largest command possible requires MAXSGENTRIES + 4 16-byte
3819 * blocks. Note, this only extends to the SG entries contained
3820 * within the command block, and does not extend to chained blocks
3821 * of SG elements. bft[] contains the eight values we write to
3822 * the registers. They are not evenly distributed, but have more
3823 * sizes for small commands, and fewer sizes for larger commands.
3824 */
3825 __u32 trans_offset;
3826 int bft[8] = { 5, 6, 8, 10, 12, 20, 28, MAXSGENTRIES + 4};
3827 /*
3828 * 5 = 1 s/g entry or 4k
3829 * 6 = 2 s/g entry or 8k
3830 * 8 = 4 s/g entry or 16k
3831 * 10 = 6 s/g entry or 24k
3832 */
3833 unsigned long register_value;
3834 BUILD_BUG_ON(28 > MAXSGENTRIES + 4);
3835
3836 h->reply_pool_wraparound = 1; /* spec: init to 1 */
3837
3838 /* Controller spec: zero out this buffer. */
3839 memset(h->reply_pool, 0, h->max_commands * sizeof(__u64));
3840 h->reply_pool_head = h->reply_pool;
3841
3842 trans_offset = readl(&(h->cfgtable->TransMethodOffset));
3843 calc_bucket_map(bft, ARRAY_SIZE(bft), h->maxsgentries,
3844 h->blockFetchTable);
3845 writel(bft[0], &h->transtable->BlockFetch0);
3846 writel(bft[1], &h->transtable->BlockFetch1);
3847 writel(bft[2], &h->transtable->BlockFetch2);
3848 writel(bft[3], &h->transtable->BlockFetch3);
3849 writel(bft[4], &h->transtable->BlockFetch4);
3850 writel(bft[5], &h->transtable->BlockFetch5);
3851 writel(bft[6], &h->transtable->BlockFetch6);
3852 writel(bft[7], &h->transtable->BlockFetch7);
3853
3854 /* size of controller ring buffer */
3855 writel(h->max_commands, &h->transtable->RepQSize);
3856 writel(1, &h->transtable->RepQCount);
3857 writel(0, &h->transtable->RepQCtrAddrLow32);
3858 writel(0, &h->transtable->RepQCtrAddrHigh32);
3859 writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
3860 writel(0, &h->transtable->RepQAddr0High32);
3861 writel(CFGTBL_Trans_Performant,
3862 &(h->cfgtable->HostWrite.TransportRequest));
3863
3864 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3865 cciss_wait_for_mode_change_ack(h);
3866 register_value = readl(&(h->cfgtable->TransportActive));
3867 if (!(register_value & CFGTBL_Trans_Performant))
3868 dev_warn(&h->pdev->dev, "cciss: unable to get board into"
3869 " performant mode\n");
3870}
3871
3872static void __devinit cciss_put_controller_into_performant_mode(ctlr_info_t *h)
3873{
3874 __u32 trans_support;
3875
3876 dev_dbg(&h->pdev->dev, "Trying to put board into Performant mode\n");
3877 /* Attempt to put controller into performant mode if supported */
3878 /* Does board support performant mode? */
3879 trans_support = readl(&(h->cfgtable->TransportSupport));
3880 if (!(trans_support & PERFORMANT_MODE))
3881 return;
3882
3883 dev_dbg(&h->pdev->dev, "Placing controller into performant mode\n");
3884 /* Performant mode demands commands on a 32 byte boundary
3885 * pci_alloc_consistent aligns on page boundarys already.
3886 * Just need to check if divisible by 32
3887 */
3888 if ((sizeof(CommandList_struct) % 32) != 0) {
3889 dev_warn(&h->pdev->dev, "%s %d %s\n",
3890 "cciss info: command size[",
3891 (int)sizeof(CommandList_struct),
3892 "] not divisible by 32, no performant mode..\n");
3893 return;
3894 }
3895
3896 /* Performant mode ring buffer and supporting data structures */
3897 h->reply_pool = (__u64 *)pci_alloc_consistent(
3898 h->pdev, h->max_commands * sizeof(__u64),
3899 &(h->reply_pool_dhandle));
3900
3901 /* Need a block fetch table for performant mode */
3902 h->blockFetchTable = kmalloc(((h->maxsgentries+1) *
3903 sizeof(__u32)), GFP_KERNEL);
3904
3905 if ((h->reply_pool == NULL) || (h->blockFetchTable == NULL))
3906 goto clean_up;
3907
3908 cciss_enter_performant_mode(h);
3909
3910 /* Change the access methods to the performant access methods */
3911 h->access = SA5_performant_access;
3912 h->transMethod = CFGTBL_Trans_Performant;
3913
3914 return;
3915clean_up:
3916 kfree(h->blockFetchTable);
3917 if (h->reply_pool)
3918 pci_free_consistent(h->pdev,
3919 h->max_commands * sizeof(__u64),
3920 h->reply_pool,
3921 h->reply_pool_dhandle);
3922 return;
3923
3924} /* cciss_put_controller_into_performant_mode */
3925
3633/* If MSI/MSI-X is supported by the kernel we will try to enable it on 3926/* If MSI/MSI-X is supported by the kernel we will try to enable it on
3634 * controllers that are capable. If not, we use IO-APIC mode. 3927 * controllers that are capable. If not, we use IO-APIC mode.
3635 */ 3928 */
3636 3929
3637static void __devinit cciss_interrupt_mode(ctlr_info_t *c, 3930static void __devinit cciss_interrupt_mode(ctlr_info_t *h)
3638 struct pci_dev *pdev, __u32 board_id)
3639{ 3931{
3640#ifdef CONFIG_PCI_MSI 3932#ifdef CONFIG_PCI_MSI
3641 int err; 3933 int err;
@@ -3644,268 +3936,283 @@ static void __devinit cciss_interrupt_mode(ctlr_info_t *c,
3644 }; 3936 };
3645 3937
3646 /* Some boards advertise MSI but don't really support it */ 3938 /* Some boards advertise MSI but don't really support it */
3647 if ((board_id == 0x40700E11) || 3939 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
3648 (board_id == 0x40800E11) || 3940 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
3649 (board_id == 0x40820E11) || (board_id == 0x40830E11))
3650 goto default_int_mode; 3941 goto default_int_mode;
3651 3942
3652 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) { 3943 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
3653 err = pci_enable_msix(pdev, cciss_msix_entries, 4); 3944 err = pci_enable_msix(h->pdev, cciss_msix_entries, 4);
3654 if (!err) { 3945 if (!err) {
3655 c->intr[0] = cciss_msix_entries[0].vector; 3946 h->intr[0] = cciss_msix_entries[0].vector;
3656 c->intr[1] = cciss_msix_entries[1].vector; 3947 h->intr[1] = cciss_msix_entries[1].vector;
3657 c->intr[2] = cciss_msix_entries[2].vector; 3948 h->intr[2] = cciss_msix_entries[2].vector;
3658 c->intr[3] = cciss_msix_entries[3].vector; 3949 h->intr[3] = cciss_msix_entries[3].vector;
3659 c->msix_vector = 1; 3950 h->msix_vector = 1;
3660 return; 3951 return;
3661 } 3952 }
3662 if (err > 0) { 3953 if (err > 0) {
3663 printk(KERN_WARNING "cciss: only %d MSI-X vectors " 3954 dev_warn(&h->pdev->dev,
3664 "available\n", err); 3955 "only %d MSI-X vectors available\n", err);
3665 goto default_int_mode; 3956 goto default_int_mode;
3666 } else { 3957 } else {
3667 printk(KERN_WARNING "cciss: MSI-X init failed %d\n", 3958 dev_warn(&h->pdev->dev,
3668 err); 3959 "MSI-X init failed %d\n", err);
3669 goto default_int_mode; 3960 goto default_int_mode;
3670 } 3961 }
3671 } 3962 }
3672 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) { 3963 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
3673 if (!pci_enable_msi(pdev)) { 3964 if (!pci_enable_msi(h->pdev))
3674 c->msi_vector = 1; 3965 h->msi_vector = 1;
3675 } else { 3966 else
3676 printk(KERN_WARNING "cciss: MSI init failed\n"); 3967 dev_warn(&h->pdev->dev, "MSI init failed\n");
3677 }
3678 } 3968 }
3679default_int_mode: 3969default_int_mode:
3680#endif /* CONFIG_PCI_MSI */ 3970#endif /* CONFIG_PCI_MSI */
3681 /* if we get here we're going to use the default interrupt mode */ 3971 /* if we get here we're going to use the default interrupt mode */
3682 c->intr[SIMPLE_MODE_INT] = pdev->irq; 3972 h->intr[PERF_MODE_INT] = h->pdev->irq;
3683 return; 3973 return;
3684} 3974}
3685 3975
3686static int __devinit cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev) 3976static int __devinit cciss_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
3687{ 3977{
3688 ushort subsystem_vendor_id, subsystem_device_id, command; 3978 int i;
3689 __u32 board_id, scratchpad = 0; 3979 u32 subsystem_vendor_id, subsystem_device_id;
3690 __u64 cfg_offset;
3691 __u32 cfg_base_addr;
3692 __u64 cfg_base_addr_index;
3693 int i, prod_index, err;
3694 3980
3695 subsystem_vendor_id = pdev->subsystem_vendor; 3981 subsystem_vendor_id = pdev->subsystem_vendor;
3696 subsystem_device_id = pdev->subsystem_device; 3982 subsystem_device_id = pdev->subsystem_device;
3697 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) | 3983 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
3698 subsystem_vendor_id); 3984 subsystem_vendor_id;
3699 3985
3700 for (i = 0; i < ARRAY_SIZE(products); i++) { 3986 for (i = 0; i < ARRAY_SIZE(products); i++) {
3701 /* Stand aside for hpsa driver on request */ 3987 /* Stand aside for hpsa driver on request */
3702 if (cciss_allow_hpsa && products[i].board_id == HPSA_BOUNDARY) 3988 if (cciss_allow_hpsa && products[i].board_id == HPSA_BOUNDARY)
3703 return -ENODEV; 3989 return -ENODEV;
3704 if (board_id == products[i].board_id) 3990 if (*board_id == products[i].board_id)
3705 break; 3991 return i;
3706 }
3707 prod_index = i;
3708 if (prod_index == ARRAY_SIZE(products)) {
3709 dev_warn(&pdev->dev,
3710 "unrecognized board ID: 0x%08lx, ignoring.\n",
3711 (unsigned long) board_id);
3712 return -ENODEV;
3713 } 3992 }
3993 dev_warn(&pdev->dev, "unrecognized board ID: 0x%08x, ignoring.\n",
3994 *board_id);
3995 return -ENODEV;
3996}
3714 3997
3715 /* check to see if controller has been disabled */ 3998static inline bool cciss_board_disabled(ctlr_info_t *h)
3716 /* BEFORE trying to enable it */ 3999{
3717 (void)pci_read_config_word(pdev, PCI_COMMAND, &command); 4000 u16 command;
3718 if (!(command & 0x02)) {
3719 printk(KERN_WARNING
3720 "cciss: controller appears to be disabled\n");
3721 return -ENODEV;
3722 }
3723 4001
3724 err = pci_enable_device(pdev); 4002 (void) pci_read_config_word(h->pdev, PCI_COMMAND, &command);
3725 if (err) { 4003 return ((command & PCI_COMMAND_MEMORY) == 0);
3726 printk(KERN_ERR "cciss: Unable to Enable PCI device\n"); 4004}
3727 return err;
3728 }
3729 4005
3730 err = pci_request_regions(pdev, "cciss"); 4006static int __devinit cciss_pci_find_memory_BAR(struct pci_dev *pdev,
3731 if (err) { 4007 unsigned long *memory_bar)
3732 printk(KERN_ERR "cciss: Cannot obtain PCI resources, " 4008{
3733 "aborting\n"); 4009 int i;
3734 return err;
3735 }
3736 4010
3737#ifdef CCISS_DEBUG 4011 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
3738 printk("command = %x\n", command); 4012 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
3739 printk("irq = %x\n", pdev->irq); 4013 /* addressing mode bits already removed */
3740 printk("board_id = %x\n", board_id); 4014 *memory_bar = pci_resource_start(pdev, i);
3741#endif /* CCISS_DEBUG */ 4015 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
4016 *memory_bar);
4017 return 0;
4018 }
4019 dev_warn(&pdev->dev, "no memory BAR found\n");
4020 return -ENODEV;
4021}
3742 4022
3743/* If the kernel supports MSI/MSI-X we will try to enable that functionality, 4023static int __devinit cciss_wait_for_board_ready(ctlr_info_t *h)
3744 * else we use the IO-APIC interrupt assigned to us by system ROM. 4024{
3745 */ 4025 int i;
3746 cciss_interrupt_mode(c, pdev, board_id); 4026 u32 scratchpad;
3747 4027
3748 /* find the memory BAR */ 4028 for (i = 0; i < CCISS_BOARD_READY_ITERATIONS; i++) {
3749 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 4029 scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
3750 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) 4030 if (scratchpad == CCISS_FIRMWARE_READY)
3751 break; 4031 return 0;
3752 } 4032 msleep(CCISS_BOARD_READY_POLL_INTERVAL_MSECS);
3753 if (i == DEVICE_COUNT_RESOURCE) {
3754 printk(KERN_WARNING "cciss: No memory BAR found\n");
3755 err = -ENODEV;
3756 goto err_out_free_res;
3757 } 4033 }
4034 dev_warn(&h->pdev->dev, "board not ready, timed out.\n");
4035 return -ENODEV;
4036}
3758 4037
3759 c->paddr = pci_resource_start(pdev, i); /* addressing mode bits 4038static int __devinit cciss_find_cfg_addrs(struct pci_dev *pdev,
3760 * already removed 4039 void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index,
3761 */ 4040 u64 *cfg_offset)
4041{
4042 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
4043 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
4044 *cfg_base_addr &= (u32) 0x0000ffff;
4045 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
4046 if (*cfg_base_addr_index == -1) {
4047 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index, "
4048 "*cfg_base_addr = 0x%08x\n", *cfg_base_addr);
4049 return -ENODEV;
4050 }
4051 return 0;
4052}
3762 4053
3763#ifdef CCISS_DEBUG 4054static int __devinit cciss_find_cfgtables(ctlr_info_t *h)
3764 printk("address 0 = %lx\n", c->paddr); 4055{
3765#endif /* CCISS_DEBUG */ 4056 u64 cfg_offset;
3766 c->vaddr = remap_pci_mem(c->paddr, 0x250); 4057 u32 cfg_base_addr;
4058 u64 cfg_base_addr_index;
4059 u32 trans_offset;
4060 int rc;
3767 4061
3768 /* Wait for the board to become ready. (PCI hotplug needs this.) 4062 rc = cciss_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
3769 * We poll for up to 120 secs, once per 100ms. */ 4063 &cfg_base_addr_index, &cfg_offset);
3770 for (i = 0; i < 1200; i++) { 4064 if (rc)
3771 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET); 4065 return rc;
3772 if (scratchpad == CCISS_FIRMWARE_READY) 4066 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
3773 break; 4067 cfg_base_addr_index) + cfg_offset, sizeof(h->cfgtable));
3774 set_current_state(TASK_INTERRUPTIBLE); 4068 if (!h->cfgtable)
3775 schedule_timeout(msecs_to_jiffies(100)); /* wait 100ms */ 4069 return -ENOMEM;
3776 } 4070 /* Find performant mode table. */
3777 if (scratchpad != CCISS_FIRMWARE_READY) { 4071 trans_offset = readl(&h->cfgtable->TransMethodOffset);
3778 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n"); 4072 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
3779 err = -ENODEV; 4073 cfg_base_addr_index)+cfg_offset+trans_offset,
3780 goto err_out_free_res; 4074 sizeof(*h->transtable));
3781 } 4075 if (!h->transtable)
4076 return -ENOMEM;
4077 return 0;
4078}
3782 4079
3783 /* get the address index number */ 4080static void __devinit cciss_get_max_perf_mode_cmds(struct ctlr_info *h)
3784 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET); 4081{
3785 cfg_base_addr &= (__u32) 0x0000ffff; 4082 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
3786#ifdef CCISS_DEBUG 4083 if (h->max_commands < 16) {
3787 printk("cfg base address = %x\n", cfg_base_addr); 4084 dev_warn(&h->pdev->dev, "Controller reports "
3788#endif /* CCISS_DEBUG */ 4085 "max supported commands of %d, an obvious lie. "
3789 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr); 4086 "Using 16. Ensure that firmware is up to date.\n",
3790#ifdef CCISS_DEBUG 4087 h->max_commands);
3791 printk("cfg base address index = %llx\n", 4088 h->max_commands = 16;
3792 (unsigned long long)cfg_base_addr_index);
3793#endif /* CCISS_DEBUG */
3794 if (cfg_base_addr_index == -1) {
3795 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
3796 err = -ENODEV;
3797 goto err_out_free_res;
3798 } 4089 }
4090}
3799 4091
3800 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET); 4092/* Interrogate the hardware for some limits:
3801#ifdef CCISS_DEBUG 4093 * max commands, max SG elements without chaining, and with chaining,
3802 printk("cfg offset = %llx\n", (unsigned long long)cfg_offset); 4094 * SG chain block size, etc.
3803#endif /* CCISS_DEBUG */ 4095 */
3804 c->cfgtable = remap_pci_mem(pci_resource_start(pdev, 4096static void __devinit cciss_find_board_params(ctlr_info_t *h)
3805 cfg_base_addr_index) + 4097{
3806 cfg_offset, sizeof(CfgTable_struct)); 4098 cciss_get_max_perf_mode_cmds(h);
3807 c->board_id = board_id; 4099 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
3808 4100 h->maxsgentries = readl(&(h->cfgtable->MaxSGElements));
3809#ifdef CCISS_DEBUG
3810 print_cfg_table(c->cfgtable);
3811#endif /* CCISS_DEBUG */
3812
3813 /* Some controllers support Zero Memory Raid (ZMR).
3814 * When configured in ZMR mode the number of supported
3815 * commands drops to 64. So instead of just setting an
3816 * arbitrary value we make the driver a little smarter.
3817 * We read the config table to tell us how many commands
3818 * are supported on the controller then subtract 4 to
3819 * leave a little room for ioctl calls.
3820 */
3821 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
3822 c->maxsgentries = readl(&(c->cfgtable->MaxSGElements));
3823
3824 /* 4101 /*
3825 * Limit native command to 32 s/g elements to save dma'able memory. 4102 * Limit in-command s/g elements to 32 save dma'able memory.
3826 * Howvever spec says if 0, use 31 4103 * Howvever spec says if 0, use 31
3827 */ 4104 */
3828 4105 h->max_cmd_sgentries = 31;
3829 c->max_cmd_sgentries = 31; 4106 if (h->maxsgentries > 512) {
3830 if (c->maxsgentries > 512) { 4107 h->max_cmd_sgentries = 32;
3831 c->max_cmd_sgentries = 32; 4108 h->chainsize = h->maxsgentries - h->max_cmd_sgentries + 1;
3832 c->chainsize = c->maxsgentries - c->max_cmd_sgentries + 1; 4109 h->maxsgentries--; /* save one for chain pointer */
3833 c->maxsgentries -= 1; /* account for chain pointer */
3834 } else { 4110 } else {
3835 c->maxsgentries = 31; /* Default to traditional value */ 4111 h->maxsgentries = 31; /* default to traditional values */
3836 c->chainsize = 0; /* traditional */ 4112 h->chainsize = 0;
3837 } 4113 }
4114}
3838 4115
3839 c->product_name = products[prod_index].product_name; 4116static inline bool CISS_signature_present(ctlr_info_t *h)
3840 c->access = *(products[prod_index].access); 4117{
3841 c->nr_cmds = c->max_commands - 4; 4118 if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
3842 if ((readb(&c->cfgtable->Signature[0]) != 'C') || 4119 (readb(&h->cfgtable->Signature[1]) != 'I') ||
3843 (readb(&c->cfgtable->Signature[1]) != 'I') || 4120 (readb(&h->cfgtable->Signature[2]) != 'S') ||
3844 (readb(&c->cfgtable->Signature[2]) != 'S') || 4121 (readb(&h->cfgtable->Signature[3]) != 'S')) {
3845 (readb(&c->cfgtable->Signature[3]) != 'S')) { 4122 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
3846 printk("Does not appear to be a valid CISS config table\n"); 4123 return false;
3847 err = -ENODEV;
3848 goto err_out_free_res;
3849 } 4124 }
4125 return true;
4126}
4127
4128/* Need to enable prefetch in the SCSI core for 6400 in x86 */
4129static inline void cciss_enable_scsi_prefetch(ctlr_info_t *h)
4130{
3850#ifdef CONFIG_X86 4131#ifdef CONFIG_X86
3851 { 4132 u32 prefetch;
3852 /* Need to enable prefetch in the SCSI core for 6400 in x86 */ 4133
3853 __u32 prefetch; 4134 prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
3854 prefetch = readl(&(c->cfgtable->SCSI_Prefetch)); 4135 prefetch |= 0x100;
3855 prefetch |= 0x100; 4136 writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
3856 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
3857 }
3858#endif 4137#endif
4138}
3859 4139
3860 /* Disabling DMA prefetch and refetch for the P600. 4140/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
3861 * An ASIC bug may result in accesses to invalid memory addresses. 4141 * in a prefetch beyond physical memory.
3862 * We've disabled prefetch for some time now. Testing with XEN 4142 */
3863 * kernels revealed a bug in the refetch if dom0 resides on a P600. 4143static inline void cciss_p600_dma_prefetch_quirk(ctlr_info_t *h)
3864 */ 4144{
3865 if(board_id == 0x3225103C) { 4145 u32 dma_prefetch;
3866 __u32 dma_prefetch; 4146 __u32 dma_refetch;
3867 __u32 dma_refetch; 4147
3868 dma_prefetch = readl(c->vaddr + I2O_DMA1_CFG); 4148 if (h->board_id != 0x3225103C)
3869 dma_prefetch |= 0x8000; 4149 return;
3870 writel(dma_prefetch, c->vaddr + I2O_DMA1_CFG); 4150 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
3871 pci_read_config_dword(pdev, PCI_COMMAND_PARITY, &dma_refetch); 4151 dma_prefetch |= 0x8000;
3872 dma_refetch |= 0x1; 4152 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
3873 pci_write_config_dword(pdev, PCI_COMMAND_PARITY, dma_refetch); 4153 pci_read_config_dword(h->pdev, PCI_COMMAND_PARITY, &dma_refetch);
4154 dma_refetch |= 0x1;
4155 pci_write_config_dword(h->pdev, PCI_COMMAND_PARITY, dma_refetch);
4156}
4157
4158static int __devinit cciss_pci_init(ctlr_info_t *h)
4159{
4160 int prod_index, err;
4161
4162 prod_index = cciss_lookup_board_id(h->pdev, &h->board_id);
4163 if (prod_index < 0)
4164 return -ENODEV;
4165 h->product_name = products[prod_index].product_name;
4166 h->access = *(products[prod_index].access);
4167
4168 if (cciss_board_disabled(h)) {
4169 dev_warn(&h->pdev->dev, "controller appears to be disabled\n");
4170 return -ENODEV;
4171 }
4172 err = pci_enable_device(h->pdev);
4173 if (err) {
4174 dev_warn(&h->pdev->dev, "Unable to Enable PCI device\n");
4175 return err;
3874 } 4176 }
3875 4177
3876#ifdef CCISS_DEBUG 4178 err = pci_request_regions(h->pdev, "cciss");
3877 printk("Trying to put board into Simple mode\n"); 4179 if (err) {
3878#endif /* CCISS_DEBUG */ 4180 dev_warn(&h->pdev->dev,
3879 c->max_commands = readl(&(c->cfgtable->CmdsOutMax)); 4181 "Cannot obtain PCI resources, aborting\n");
3880 /* Update the field, and then ring the doorbell */ 4182 return err;
3881 writel(CFGTBL_Trans_Simple, &(c->cfgtable->HostWrite.TransportRequest)); 4183 }
3882 writel(CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
3883 4184
3884 /* under certain very rare conditions, this can take awhile. 4185 dev_dbg(&h->pdev->dev, "irq = %x\n", h->pdev->irq);
3885 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right 4186 dev_dbg(&h->pdev->dev, "board_id = %x\n", h->board_id);
3886 * as we enter this code.) */ 4187
3887 for (i = 0; i < MAX_CONFIG_WAIT; i++) { 4188/* If the kernel supports MSI/MSI-X we will try to enable that functionality,
3888 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq)) 4189 * else we use the IO-APIC interrupt assigned to us by system ROM.
3889 break; 4190 */
3890 /* delay and try again */ 4191 cciss_interrupt_mode(h);
3891 set_current_state(TASK_INTERRUPTIBLE); 4192 err = cciss_pci_find_memory_BAR(h->pdev, &h->paddr);
3892 schedule_timeout(msecs_to_jiffies(1)); 4193 if (err)
4194 goto err_out_free_res;
4195 h->vaddr = remap_pci_mem(h->paddr, 0x250);
4196 if (!h->vaddr) {
4197 err = -ENOMEM;
4198 goto err_out_free_res;
3893 } 4199 }
4200 err = cciss_wait_for_board_ready(h);
4201 if (err)
4202 goto err_out_free_res;
4203 err = cciss_find_cfgtables(h);
4204 if (err)
4205 goto err_out_free_res;
4206 print_cfg_table(h);
4207 cciss_find_board_params(h);
3894 4208
3895#ifdef CCISS_DEBUG 4209 if (!CISS_signature_present(h)) {
3896 printk(KERN_DEBUG "I counter got to %d %x\n", i,
3897 readl(c->vaddr + SA5_DOORBELL));
3898#endif /* CCISS_DEBUG */
3899#ifdef CCISS_DEBUG
3900 print_cfg_table(c->cfgtable);
3901#endif /* CCISS_DEBUG */
3902
3903 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3904 printk(KERN_WARNING "cciss: unable to get board into"
3905 " simple mode\n");
3906 err = -ENODEV; 4210 err = -ENODEV;
3907 goto err_out_free_res; 4211 goto err_out_free_res;
3908 } 4212 }
4213 cciss_enable_scsi_prefetch(h);
4214 cciss_p600_dma_prefetch_quirk(h);
4215 cciss_put_controller_into_performant_mode(h);
3909 return 0; 4216 return 0;
3910 4217
3911err_out_free_res: 4218err_out_free_res:
@@ -3913,42 +4220,47 @@ err_out_free_res:
3913 * Deliberately omit pci_disable_device(): it does something nasty to 4220 * Deliberately omit pci_disable_device(): it does something nasty to
3914 * Smart Array controllers that pci_enable_device does not undo 4221 * Smart Array controllers that pci_enable_device does not undo
3915 */ 4222 */
3916 pci_release_regions(pdev); 4223 if (h->transtable)
4224 iounmap(h->transtable);
4225 if (h->cfgtable)
4226 iounmap(h->cfgtable);
4227 if (h->vaddr)
4228 iounmap(h->vaddr);
4229 pci_release_regions(h->pdev);
3917 return err; 4230 return err;
3918} 4231}
3919 4232
3920/* Function to find the first free pointer into our hba[] array 4233/* Function to find the first free pointer into our hba[] array
3921 * Returns -1 if no free entries are left. 4234 * Returns -1 if no free entries are left.
3922 */ 4235 */
3923static int alloc_cciss_hba(void) 4236static int alloc_cciss_hba(struct pci_dev *pdev)
3924{ 4237{
3925 int i; 4238 int i;
3926 4239
3927 for (i = 0; i < MAX_CTLR; i++) { 4240 for (i = 0; i < MAX_CTLR; i++) {
3928 if (!hba[i]) { 4241 if (!hba[i]) {
3929 ctlr_info_t *p; 4242 ctlr_info_t *h;
3930 4243
3931 p = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL); 4244 h = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
3932 if (!p) 4245 if (!h)
3933 goto Enomem; 4246 goto Enomem;
3934 hba[i] = p; 4247 hba[i] = h;
3935 return i; 4248 return i;
3936 } 4249 }
3937 } 4250 }
3938 printk(KERN_WARNING "cciss: This driver supports a maximum" 4251 dev_warn(&pdev->dev, "This driver supports a maximum"
3939 " of %d controllers.\n", MAX_CTLR); 4252 " of %d controllers.\n", MAX_CTLR);
3940 return -1; 4253 return -1;
3941Enomem: 4254Enomem:
3942 printk(KERN_ERR "cciss: out of memory.\n"); 4255 dev_warn(&pdev->dev, "out of memory.\n");
3943 return -1; 4256 return -1;
3944} 4257}
3945 4258
3946static void free_hba(int n) 4259static void free_hba(ctlr_info_t *h)
3947{ 4260{
3948 ctlr_info_t *h = hba[n];
3949 int i; 4261 int i;
3950 4262
3951 hba[n] = NULL; 4263 hba[h->ctlr] = NULL;
3952 for (i = 0; i < h->highest_lun + 1; i++) 4264 for (i = 0; i < h->highest_lun + 1; i++)
3953 if (h->gendisk[i] != NULL) 4265 if (h->gendisk[i] != NULL)
3954 put_disk(h->gendisk[i]); 4266 put_disk(h->gendisk[i]);
@@ -4028,7 +4340,8 @@ static __devinit int cciss_message(struct pci_dev *pdev, unsigned char opcode, u
4028 /* we leak the DMA buffer here ... no choice since the controller could 4340 /* we leak the DMA buffer here ... no choice since the controller could
4029 still complete the command. */ 4341 still complete the command. */
4030 if (i == 10) { 4342 if (i == 10) {
4031 printk(KERN_ERR "cciss: controller message %02x:%02x timed out\n", 4343 dev_err(&pdev->dev,
4344 "controller message %02x:%02x timed out\n",
4032 opcode, type); 4345 opcode, type);
4033 return -ETIMEDOUT; 4346 return -ETIMEDOUT;
4034 } 4347 }
@@ -4036,12 +4349,12 @@ static __devinit int cciss_message(struct pci_dev *pdev, unsigned char opcode, u
4036 pci_free_consistent(pdev, cmd_sz, cmd, paddr64); 4349 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
4037 4350
4038 if (tag & 2) { 4351 if (tag & 2) {
4039 printk(KERN_ERR "cciss: controller message %02x:%02x failed\n", 4352 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
4040 opcode, type); 4353 opcode, type);
4041 return -EIO; 4354 return -EIO;
4042 } 4355 }
4043 4356
4044 printk(KERN_INFO "cciss: controller message %02x:%02x succeeded\n", 4357 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
4045 opcode, type); 4358 opcode, type);
4046 return 0; 4359 return 0;
4047} 4360}
@@ -4062,7 +4375,7 @@ static __devinit int cciss_reset_msi(struct pci_dev *pdev)
4062 if (pos) { 4375 if (pos) {
4063 pci_read_config_word(pdev, msi_control_reg(pos), &control); 4376 pci_read_config_word(pdev, msi_control_reg(pos), &control);
4064 if (control & PCI_MSI_FLAGS_ENABLE) { 4377 if (control & PCI_MSI_FLAGS_ENABLE) {
4065 printk(KERN_INFO "cciss: resetting MSI\n"); 4378 dev_info(&pdev->dev, "resetting MSI\n");
4066 pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSI_FLAGS_ENABLE); 4379 pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSI_FLAGS_ENABLE);
4067 } 4380 }
4068 } 4381 }
@@ -4071,7 +4384,7 @@ static __devinit int cciss_reset_msi(struct pci_dev *pdev)
4071 if (pos) { 4384 if (pos) {
4072 pci_read_config_word(pdev, msi_control_reg(pos), &control); 4385 pci_read_config_word(pdev, msi_control_reg(pos), &control);
4073 if (control & PCI_MSIX_FLAGS_ENABLE) { 4386 if (control & PCI_MSIX_FLAGS_ENABLE) {
4074 printk(KERN_INFO "cciss: resetting MSI-X\n"); 4387 dev_info(&pdev->dev, "resetting MSI-X\n");
4075 pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSIX_FLAGS_ENABLE); 4388 pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSIX_FLAGS_ENABLE);
4076 } 4389 }
4077 } 4390 }
@@ -4079,68 +4392,144 @@ static __devinit int cciss_reset_msi(struct pci_dev *pdev)
4079 return 0; 4392 return 0;
4080} 4393}
4081 4394
4082/* This does a hard reset of the controller using PCI power management 4395static int cciss_controller_hard_reset(struct pci_dev *pdev,
4083 * states. */ 4396 void * __iomem vaddr, bool use_doorbell)
4084static __devinit int cciss_hard_reset_controller(struct pci_dev *pdev)
4085{ 4397{
4086 u16 pmcsr, saved_config_space[32]; 4398 u16 pmcsr;
4087 int i, pos; 4399 int pos;
4088 4400
4089 printk(KERN_INFO "cciss: using PCI PM to reset controller\n"); 4401 if (use_doorbell) {
4402 /* For everything after the P600, the PCI power state method
4403 * of resetting the controller doesn't work, so we have this
4404 * other way using the doorbell register.
4405 */
4406 dev_info(&pdev->dev, "using doorbell to reset controller\n");
4407 writel(DOORBELL_CTLR_RESET, vaddr + SA5_DOORBELL);
4408 msleep(1000);
4409 } else { /* Try to do it the PCI power state way */
4410
4411 /* Quoting from the Open CISS Specification: "The Power
4412 * Management Control/Status Register (CSR) controls the power
4413 * state of the device. The normal operating state is D0,
4414 * CSR=00h. The software off state is D3, CSR=03h. To reset
4415 * the controller, place the interface device in D3 then to D0,
4416 * this causes a secondary PCI reset which will reset the
4417 * controller." */
4418
4419 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
4420 if (pos == 0) {
4421 dev_err(&pdev->dev,
4422 "cciss_controller_hard_reset: "
4423 "PCI PM not supported\n");
4424 return -ENODEV;
4425 }
4426 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
4427 /* enter the D3hot power management state */
4428 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
4429 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4430 pmcsr |= PCI_D3hot;
4431 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
4090 4432
4091 /* This is very nearly the same thing as 4433 msleep(500);
4092 4434
4093 pci_save_state(pci_dev); 4435 /* enter the D0 power management state */
4094 pci_set_power_state(pci_dev, PCI_D3hot); 4436 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4095 pci_set_power_state(pci_dev, PCI_D0); 4437 pmcsr |= PCI_D0;
4096 pci_restore_state(pci_dev); 4438 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
4097 4439
4098 but we can't use these nice canned kernel routines on 4440 msleep(500);
4099 kexec, because they also check the MSI/MSI-X state in PCI 4441 }
4100 configuration space and do the wrong thing when it is 4442 return 0;
4101 set/cleared. Also, the pci_save/restore_state functions 4443}
4102 violate the ordering requirements for restoring the
4103 configuration space from the CCISS document (see the
4104 comment below). So we roll our own .... */
4105 4444
4106 for (i = 0; i < 32; i++) 4445/* This does a hard reset of the controller using PCI power management
4107 pci_read_config_word(pdev, 2*i, &saved_config_space[i]); 4446 * states or using the doorbell register. */
4447static __devinit int cciss_kdump_hard_reset_controller(struct pci_dev *pdev)
4448{
4449 u16 saved_config_space[32];
4450 u64 cfg_offset;
4451 u32 cfg_base_addr;
4452 u64 cfg_base_addr_index;
4453 void __iomem *vaddr;
4454 unsigned long paddr;
4455 u32 misc_fw_support, active_transport;
4456 int rc, i;
4457 CfgTable_struct __iomem *cfgtable;
4458 bool use_doorbell;
4459 u32 board_id;
4460
4461 /* For controllers as old a the p600, this is very nearly
4462 * the same thing as
4463 *
4464 * pci_save_state(pci_dev);
4465 * pci_set_power_state(pci_dev, PCI_D3hot);
4466 * pci_set_power_state(pci_dev, PCI_D0);
4467 * pci_restore_state(pci_dev);
4468 *
4469 * but we can't use these nice canned kernel routines on
4470 * kexec, because they also check the MSI/MSI-X state in PCI
4471 * configuration space and do the wrong thing when it is
4472 * set/cleared. Also, the pci_save/restore_state functions
4473 * violate the ordering requirements for restoring the
4474 * configuration space from the CCISS document (see the
4475 * comment below). So we roll our own ....
4476 *
4477 * For controllers newer than the P600, the pci power state
4478 * method of resetting doesn't work so we have another way
4479 * using the doorbell register.
4480 */
4108 4481
4109 pos = pci_find_capability(pdev, PCI_CAP_ID_PM); 4482 /* Exclude 640x boards. These are two pci devices in one slot
4110 if (pos == 0) { 4483 * which share a battery backed cache module. One controls the
4111 printk(KERN_ERR "cciss_reset_controller: PCI PM not supported\n"); 4484 * cache, the other accesses the cache through the one that controls
4485 * it. If we reset the one controlling the cache, the other will
4486 * likely not be happy. Just forbid resetting this conjoined mess.
4487 */
4488 cciss_lookup_board_id(pdev, &board_id);
4489 if (board_id == 0x409C0E11 || board_id == 0x409D0E11) {
4490 dev_warn(&pdev->dev, "Cannot reset Smart Array 640x "
4491 "due to shared cache module.");
4112 return -ENODEV; 4492 return -ENODEV;
4113 } 4493 }
4114 4494
4115 /* Quoting from the Open CISS Specification: "The Power 4495 for (i = 0; i < 32; i++)
4116 * Management Control/Status Register (CSR) controls the power 4496 pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
4117 * state of the device. The normal operating state is D0,
4118 * CSR=00h. The software off state is D3, CSR=03h. To reset
4119 * the controller, place the interface device in D3 then to
4120 * D0, this causes a secondary PCI reset which will reset the
4121 * controller." */
4122 4497
4123 /* enter the D3hot power management state */ 4498 /* find the first memory BAR, so we can find the cfg table */
4124 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr); 4499 rc = cciss_pci_find_memory_BAR(pdev, &paddr);
4125 pmcsr &= ~PCI_PM_CTRL_STATE_MASK; 4500 if (rc)
4126 pmcsr |= PCI_D3hot; 4501 return rc;
4127 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr); 4502 vaddr = remap_pci_mem(paddr, 0x250);
4503 if (!vaddr)
4504 return -ENOMEM;
4128 4505
4129 schedule_timeout_uninterruptible(HZ >> 1); 4506 /* find cfgtable in order to check if reset via doorbell is supported */
4507 rc = cciss_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
4508 &cfg_base_addr_index, &cfg_offset);
4509 if (rc)
4510 goto unmap_vaddr;
4511 cfgtable = remap_pci_mem(pci_resource_start(pdev,
4512 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
4513 if (!cfgtable) {
4514 rc = -ENOMEM;
4515 goto unmap_vaddr;
4516 }
4130 4517
4131 /* enter the D0 power management state */ 4518 /* If reset via doorbell register is supported, use that. */
4132 pmcsr &= ~PCI_PM_CTRL_STATE_MASK; 4519 misc_fw_support = readl(&cfgtable->misc_fw_support);
4133 pmcsr |= PCI_D0; 4520 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
4134 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
4135 4521
4136 schedule_timeout_uninterruptible(HZ >> 1); 4522 rc = cciss_controller_hard_reset(pdev, vaddr, use_doorbell);
4523 if (rc)
4524 goto unmap_cfgtable;
4137 4525
4138 /* Restore the PCI configuration space. The Open CISS 4526 /* Restore the PCI configuration space. The Open CISS
4139 * Specification says, "Restore the PCI Configuration 4527 * Specification says, "Restore the PCI Configuration
4140 * Registers, offsets 00h through 60h. It is important to 4528 * Registers, offsets 00h through 60h. It is important to
4141 * restore the command register, 16-bits at offset 04h, 4529 * restore the command register, 16-bits at offset 04h,
4142 * last. Do not restore the configuration status register, 4530 * last. Do not restore the configuration status register,
4143 * 16-bits at offset 06h." Note that the offset is 2*i. */ 4531 * 16-bits at offset 06h." Note that the offset is 2*i.
4532 */
4144 for (i = 0; i < 32; i++) { 4533 for (i = 0; i < 32; i++) {
4145 if (i == 2 || i == 3) 4534 if (i == 2 || i == 3)
4146 continue; 4535 continue;
@@ -4149,6 +4538,63 @@ static __devinit int cciss_hard_reset_controller(struct pci_dev *pdev)
4149 wmb(); 4538 wmb();
4150 pci_write_config_word(pdev, 4, saved_config_space[2]); 4539 pci_write_config_word(pdev, 4, saved_config_space[2]);
4151 4540
4541 /* Some devices (notably the HP Smart Array 5i Controller)
4542 need a little pause here */
4543 msleep(CCISS_POST_RESET_PAUSE_MSECS);
4544
4545 /* Controller should be in simple mode at this point. If it's not,
4546 * It means we're on one of those controllers which doesn't support
4547 * the doorbell reset method and on which the PCI power management reset
4548 * method doesn't work (P800, for example.)
4549 * In those cases, don't try to proceed, as it generally doesn't work.
4550 */
4551 active_transport = readl(&cfgtable->TransportActive);
4552 if (active_transport & PERFORMANT_MODE) {
4553 dev_warn(&pdev->dev, "Unable to successfully reset controller,"
4554 " Ignoring controller.\n");
4555 rc = -ENODEV;
4556 }
4557
4558unmap_cfgtable:
4559 iounmap(cfgtable);
4560
4561unmap_vaddr:
4562 iounmap(vaddr);
4563 return rc;
4564}
4565
4566static __devinit int cciss_init_reset_devices(struct pci_dev *pdev)
4567{
4568 int rc, i;
4569
4570 if (!reset_devices)
4571 return 0;
4572
4573 /* Reset the controller with a PCI power-cycle or via doorbell */
4574 rc = cciss_kdump_hard_reset_controller(pdev);
4575
4576 /* -ENOTSUPP here means we cannot reset the controller
4577 * but it's already (and still) up and running in
4578 * "performant mode". Or, it might be 640x, which can't reset
4579 * due to concerns about shared bbwc between 6402/6404 pair.
4580 */
4581 if (rc == -ENOTSUPP)
4582 return 0; /* just try to do the kdump anyhow. */
4583 if (rc)
4584 return -ENODEV;
4585 if (cciss_reset_msi(pdev))
4586 return -ENODEV;
4587
4588 /* Now try to get the controller to respond to a no-op */
4589 for (i = 0; i < CCISS_POST_RESET_NOOP_RETRIES; i++) {
4590 if (cciss_noop(pdev) == 0)
4591 break;
4592 else
4593 dev_warn(&pdev->dev, "no-op failed%s\n",
4594 (i < CCISS_POST_RESET_NOOP_RETRIES - 1 ?
4595 "; re-trying" : ""));
4596 msleep(CCISS_POST_RESET_NOOP_INTERVAL_MSECS);
4597 }
4152 return 0; 4598 return 0;
4153} 4599}
4154 4600
@@ -4166,46 +4612,31 @@ static int __devinit cciss_init_one(struct pci_dev *pdev,
4166 int rc; 4612 int rc;
4167 int dac, return_code; 4613 int dac, return_code;
4168 InquiryData_struct *inq_buff; 4614 InquiryData_struct *inq_buff;
4615 ctlr_info_t *h;
4169 4616
4170 if (reset_devices) { 4617 rc = cciss_init_reset_devices(pdev);
4171 /* Reset the controller with a PCI power-cycle */ 4618 if (rc)
4172 if (cciss_hard_reset_controller(pdev) || cciss_reset_msi(pdev)) 4619 return rc;
4173 return -ENODEV; 4620 i = alloc_cciss_hba(pdev);
4174
4175 /* Now try to get the controller to respond to a no-op. Some
4176 devices (notably the HP Smart Array 5i Controller) need
4177 up to 30 seconds to respond. */
4178 for (i=0; i<30; i++) {
4179 if (cciss_noop(pdev) == 0)
4180 break;
4181
4182 schedule_timeout_uninterruptible(HZ);
4183 }
4184 if (i == 30) {
4185 printk(KERN_ERR "cciss: controller seems dead\n");
4186 return -EBUSY;
4187 }
4188 }
4189
4190 i = alloc_cciss_hba();
4191 if (i < 0) 4621 if (i < 0)
4192 return -1; 4622 return -1;
4193 4623
4194 hba[i]->busy_initializing = 1; 4624 h = hba[i];
4195 INIT_HLIST_HEAD(&hba[i]->cmpQ); 4625 h->pdev = pdev;
4196 INIT_HLIST_HEAD(&hba[i]->reqQ); 4626 h->busy_initializing = 1;
4197 mutex_init(&hba[i]->busy_shutting_down); 4627 INIT_HLIST_HEAD(&h->cmpQ);
4628 INIT_HLIST_HEAD(&h->reqQ);
4629 mutex_init(&h->busy_shutting_down);
4198 4630
4199 if (cciss_pci_init(hba[i], pdev) != 0) 4631 if (cciss_pci_init(h) != 0)
4200 goto clean_no_release_regions; 4632 goto clean_no_release_regions;
4201 4633
4202 sprintf(hba[i]->devname, "cciss%d", i); 4634 sprintf(h->devname, "cciss%d", i);
4203 hba[i]->ctlr = i; 4635 h->ctlr = i;
4204 hba[i]->pdev = pdev;
4205 4636
4206 init_completion(&hba[i]->scan_wait); 4637 init_completion(&h->scan_wait);
4207 4638
4208 if (cciss_create_hba_sysfs_entry(hba[i])) 4639 if (cciss_create_hba_sysfs_entry(h))
4209 goto clean0; 4640 goto clean0;
4210 4641
4211 /* configure PCI DMA stuff */ 4642 /* configure PCI DMA stuff */
@@ -4214,7 +4645,7 @@ static int __devinit cciss_init_one(struct pci_dev *pdev,
4214 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) 4645 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
4215 dac = 0; 4646 dac = 0;
4216 else { 4647 else {
4217 printk(KERN_ERR "cciss: no suitable DMA available\n"); 4648 dev_err(&h->pdev->dev, "no suitable DMA available\n");
4218 goto clean1; 4649 goto clean1;
4219 } 4650 }
4220 4651
@@ -4224,151 +4655,161 @@ static int __devinit cciss_init_one(struct pci_dev *pdev,
4224 * 8 controller support. 4655 * 8 controller support.
4225 */ 4656 */
4226 if (i < MAX_CTLR_ORIG) 4657 if (i < MAX_CTLR_ORIG)
4227 hba[i]->major = COMPAQ_CISS_MAJOR + i; 4658 h->major = COMPAQ_CISS_MAJOR + i;
4228 rc = register_blkdev(hba[i]->major, hba[i]->devname); 4659 rc = register_blkdev(h->major, h->devname);
4229 if (rc == -EBUSY || rc == -EINVAL) { 4660 if (rc == -EBUSY || rc == -EINVAL) {
4230 printk(KERN_ERR 4661 dev_err(&h->pdev->dev,
4231 "cciss: Unable to get major number %d for %s " 4662 "Unable to get major number %d for %s "
4232 "on hba %d\n", hba[i]->major, hba[i]->devname, i); 4663 "on hba %d\n", h->major, h->devname, i);
4233 goto clean1; 4664 goto clean1;
4234 } else { 4665 } else {
4235 if (i >= MAX_CTLR_ORIG) 4666 if (i >= MAX_CTLR_ORIG)
4236 hba[i]->major = rc; 4667 h->major = rc;
4237 } 4668 }
4238 4669
4239 /* make sure the board interrupts are off */ 4670 /* make sure the board interrupts are off */
4240 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF); 4671 h->access.set_intr_mask(h, CCISS_INTR_OFF);
4241 if (request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr, 4672 if (h->msi_vector || h->msix_vector) {
4242 IRQF_DISABLED | IRQF_SHARED, hba[i]->devname, hba[i])) { 4673 if (request_irq(h->intr[PERF_MODE_INT],
4243 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n", 4674 do_cciss_msix_intr,
4244 hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname); 4675 IRQF_DISABLED, h->devname, h)) {
4245 goto clean2; 4676 dev_err(&h->pdev->dev, "Unable to get irq %d for %s\n",
4677 h->intr[PERF_MODE_INT], h->devname);
4678 goto clean2;
4679 }
4680 } else {
4681 if (request_irq(h->intr[PERF_MODE_INT], do_cciss_intx,
4682 IRQF_DISABLED, h->devname, h)) {
4683 dev_err(&h->pdev->dev, "Unable to get irq %d for %s\n",
4684 h->intr[PERF_MODE_INT], h->devname);
4685 goto clean2;
4686 }
4246 } 4687 }
4247 4688
4248 printk(KERN_INFO "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n", 4689 dev_info(&h->pdev->dev, "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
4249 hba[i]->devname, pdev->device, pci_name(pdev), 4690 h->devname, pdev->device, pci_name(pdev),
4250 hba[i]->intr[SIMPLE_MODE_INT], dac ? "" : " not"); 4691 h->intr[PERF_MODE_INT], dac ? "" : " not");
4251 4692
4252 hba[i]->cmd_pool_bits = 4693 h->cmd_pool_bits =
4253 kmalloc(DIV_ROUND_UP(hba[i]->nr_cmds, BITS_PER_LONG) 4694 kmalloc(DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG)
4254 * sizeof(unsigned long), GFP_KERNEL); 4695 * sizeof(unsigned long), GFP_KERNEL);
4255 hba[i]->cmd_pool = (CommandList_struct *) 4696 h->cmd_pool = (CommandList_struct *)
4256 pci_alloc_consistent(hba[i]->pdev, 4697 pci_alloc_consistent(h->pdev,
4257 hba[i]->nr_cmds * sizeof(CommandList_struct), 4698 h->nr_cmds * sizeof(CommandList_struct),
4258 &(hba[i]->cmd_pool_dhandle)); 4699 &(h->cmd_pool_dhandle));
4259 hba[i]->errinfo_pool = (ErrorInfo_struct *) 4700 h->errinfo_pool = (ErrorInfo_struct *)
4260 pci_alloc_consistent(hba[i]->pdev, 4701 pci_alloc_consistent(h->pdev,
4261 hba[i]->nr_cmds * sizeof(ErrorInfo_struct), 4702 h->nr_cmds * sizeof(ErrorInfo_struct),
4262 &(hba[i]->errinfo_pool_dhandle)); 4703 &(h->errinfo_pool_dhandle));
4263 if ((hba[i]->cmd_pool_bits == NULL) 4704 if ((h->cmd_pool_bits == NULL)
4264 || (hba[i]->cmd_pool == NULL) 4705 || (h->cmd_pool == NULL)
4265 || (hba[i]->errinfo_pool == NULL)) { 4706 || (h->errinfo_pool == NULL)) {
4266 printk(KERN_ERR "cciss: out of memory"); 4707 dev_err(&h->pdev->dev, "out of memory");
4267 goto clean4; 4708 goto clean4;
4268 } 4709 }
4269 4710
4270 /* Need space for temp scatter list */ 4711 /* Need space for temp scatter list */
4271 hba[i]->scatter_list = kmalloc(hba[i]->max_commands * 4712 h->scatter_list = kmalloc(h->max_commands *
4272 sizeof(struct scatterlist *), 4713 sizeof(struct scatterlist *),
4273 GFP_KERNEL); 4714 GFP_KERNEL);
4274 for (k = 0; k < hba[i]->nr_cmds; k++) { 4715 for (k = 0; k < h->nr_cmds; k++) {
4275 hba[i]->scatter_list[k] = kmalloc(sizeof(struct scatterlist) * 4716 h->scatter_list[k] = kmalloc(sizeof(struct scatterlist) *
4276 hba[i]->maxsgentries, 4717 h->maxsgentries,
4277 GFP_KERNEL); 4718 GFP_KERNEL);
4278 if (hba[i]->scatter_list[k] == NULL) { 4719 if (h->scatter_list[k] == NULL) {
4279 printk(KERN_ERR "cciss%d: could not allocate " 4720 dev_err(&h->pdev->dev,
4280 "s/g lists\n", i); 4721 "could not allocate s/g lists\n");
4281 goto clean4; 4722 goto clean4;
4282 } 4723 }
4283 } 4724 }
4284 hba[i]->cmd_sg_list = cciss_allocate_sg_chain_blocks(hba[i], 4725 h->cmd_sg_list = cciss_allocate_sg_chain_blocks(h,
4285 hba[i]->chainsize, hba[i]->nr_cmds); 4726 h->chainsize, h->nr_cmds);
4286 if (!hba[i]->cmd_sg_list && hba[i]->chainsize > 0) 4727 if (!h->cmd_sg_list && h->chainsize > 0)
4287 goto clean4; 4728 goto clean4;
4288 4729
4289 spin_lock_init(&hba[i]->lock); 4730 spin_lock_init(&h->lock);
4290 4731
4291 /* Initialize the pdev driver private data. 4732 /* Initialize the pdev driver private data.
4292 have it point to hba[i]. */ 4733 have it point to h. */
4293 pci_set_drvdata(pdev, hba[i]); 4734 pci_set_drvdata(pdev, h);
4294 /* command and error info recs zeroed out before 4735 /* command and error info recs zeroed out before
4295 they are used */ 4736 they are used */
4296 memset(hba[i]->cmd_pool_bits, 0, 4737 memset(h->cmd_pool_bits, 0,
4297 DIV_ROUND_UP(hba[i]->nr_cmds, BITS_PER_LONG) 4738 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG)
4298 * sizeof(unsigned long)); 4739 * sizeof(unsigned long));
4299 4740
4300 hba[i]->num_luns = 0; 4741 h->num_luns = 0;
4301 hba[i]->highest_lun = -1; 4742 h->highest_lun = -1;
4302 for (j = 0; j < CISS_MAX_LUN; j++) { 4743 for (j = 0; j < CISS_MAX_LUN; j++) {
4303 hba[i]->drv[j] = NULL; 4744 h->drv[j] = NULL;
4304 hba[i]->gendisk[j] = NULL; 4745 h->gendisk[j] = NULL;
4305 } 4746 }
4306 4747
4307 cciss_scsi_setup(i); 4748 cciss_scsi_setup(h);
4308 4749
4309 /* Turn the interrupts on so we can service requests */ 4750 /* Turn the interrupts on so we can service requests */
4310 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON); 4751 h->access.set_intr_mask(h, CCISS_INTR_ON);
4311 4752
4312 /* Get the firmware version */ 4753 /* Get the firmware version */
4313 inq_buff = kzalloc(sizeof(InquiryData_struct), GFP_KERNEL); 4754 inq_buff = kzalloc(sizeof(InquiryData_struct), GFP_KERNEL);
4314 if (inq_buff == NULL) { 4755 if (inq_buff == NULL) {
4315 printk(KERN_ERR "cciss: out of memory\n"); 4756 dev_err(&h->pdev->dev, "out of memory\n");
4316 goto clean4; 4757 goto clean4;
4317 } 4758 }
4318 4759
4319 return_code = sendcmd_withirq(CISS_INQUIRY, i, inq_buff, 4760 return_code = sendcmd_withirq(h, CISS_INQUIRY, inq_buff,
4320 sizeof(InquiryData_struct), 0, CTLR_LUNID, TYPE_CMD); 4761 sizeof(InquiryData_struct), 0, CTLR_LUNID, TYPE_CMD);
4321 if (return_code == IO_OK) { 4762 if (return_code == IO_OK) {
4322 hba[i]->firm_ver[0] = inq_buff->data_byte[32]; 4763 h->firm_ver[0] = inq_buff->data_byte[32];
4323 hba[i]->firm_ver[1] = inq_buff->data_byte[33]; 4764 h->firm_ver[1] = inq_buff->data_byte[33];
4324 hba[i]->firm_ver[2] = inq_buff->data_byte[34]; 4765 h->firm_ver[2] = inq_buff->data_byte[34];
4325 hba[i]->firm_ver[3] = inq_buff->data_byte[35]; 4766 h->firm_ver[3] = inq_buff->data_byte[35];
4326 } else { /* send command failed */ 4767 } else { /* send command failed */
4327 printk(KERN_WARNING "cciss: unable to determine firmware" 4768 dev_warn(&h->pdev->dev, "unable to determine firmware"
4328 " version of controller\n"); 4769 " version of controller\n");
4329 } 4770 }
4330 kfree(inq_buff); 4771 kfree(inq_buff);
4331 4772
4332 cciss_procinit(i); 4773 cciss_procinit(h);
4333 4774
4334 hba[i]->cciss_max_sectors = 8192; 4775 h->cciss_max_sectors = 8192;
4335 4776
4336 rebuild_lun_table(hba[i], 1, 0); 4777 rebuild_lun_table(h, 1, 0);
4337 hba[i]->busy_initializing = 0; 4778 h->busy_initializing = 0;
4338 return 1; 4779 return 1;
4339 4780
4340clean4: 4781clean4:
4341 kfree(hba[i]->cmd_pool_bits); 4782 kfree(h->cmd_pool_bits);
4342 /* Free up sg elements */ 4783 /* Free up sg elements */
4343 for (k = 0; k < hba[i]->nr_cmds; k++) 4784 for (k = 0; k < h->nr_cmds; k++)
4344 kfree(hba[i]->scatter_list[k]); 4785 kfree(h->scatter_list[k]);
4345 kfree(hba[i]->scatter_list); 4786 kfree(h->scatter_list);
4346 cciss_free_sg_chain_blocks(hba[i]->cmd_sg_list, hba[i]->nr_cmds); 4787 cciss_free_sg_chain_blocks(h->cmd_sg_list, h->nr_cmds);
4347 if (hba[i]->cmd_pool) 4788 if (h->cmd_pool)
4348 pci_free_consistent(hba[i]->pdev, 4789 pci_free_consistent(h->pdev,
4349 hba[i]->nr_cmds * sizeof(CommandList_struct), 4790 h->nr_cmds * sizeof(CommandList_struct),
4350 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle); 4791 h->cmd_pool, h->cmd_pool_dhandle);
4351 if (hba[i]->errinfo_pool) 4792 if (h->errinfo_pool)
4352 pci_free_consistent(hba[i]->pdev, 4793 pci_free_consistent(h->pdev,
4353 hba[i]->nr_cmds * sizeof(ErrorInfo_struct), 4794 h->nr_cmds * sizeof(ErrorInfo_struct),
4354 hba[i]->errinfo_pool, 4795 h->errinfo_pool,
4355 hba[i]->errinfo_pool_dhandle); 4796 h->errinfo_pool_dhandle);
4356 free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]); 4797 free_irq(h->intr[PERF_MODE_INT], h);
4357clean2: 4798clean2:
4358 unregister_blkdev(hba[i]->major, hba[i]->devname); 4799 unregister_blkdev(h->major, h->devname);
4359clean1: 4800clean1:
4360 cciss_destroy_hba_sysfs_entry(hba[i]); 4801 cciss_destroy_hba_sysfs_entry(h);
4361clean0: 4802clean0:
4362 pci_release_regions(pdev); 4803 pci_release_regions(pdev);
4363clean_no_release_regions: 4804clean_no_release_regions:
4364 hba[i]->busy_initializing = 0; 4805 h->busy_initializing = 0;
4365 4806
4366 /* 4807 /*
4367 * Deliberately omit pci_disable_device(): it does something nasty to 4808 * Deliberately omit pci_disable_device(): it does something nasty to
4368 * Smart Array controllers that pci_enable_device does not undo 4809 * Smart Array controllers that pci_enable_device does not undo
4369 */ 4810 */
4370 pci_set_drvdata(pdev, NULL); 4811 pci_set_drvdata(pdev, NULL);
4371 free_hba(i); 4812 free_hba(h);
4372 return -1; 4813 return -1;
4373} 4814}
4374 4815
@@ -4381,55 +4822,51 @@ static void cciss_shutdown(struct pci_dev *pdev)
4381 h = pci_get_drvdata(pdev); 4822 h = pci_get_drvdata(pdev);
4382 flush_buf = kzalloc(4, GFP_KERNEL); 4823 flush_buf = kzalloc(4, GFP_KERNEL);
4383 if (!flush_buf) { 4824 if (!flush_buf) {
4384 printk(KERN_WARNING 4825 dev_warn(&h->pdev->dev, "cache not flushed, out of memory.\n");
4385 "cciss:%d cache not flushed, out of memory.\n",
4386 h->ctlr);
4387 return; 4826 return;
4388 } 4827 }
4389 /* write all data in the battery backed cache to disk */ 4828 /* write all data in the battery backed cache to disk */
4390 memset(flush_buf, 0, 4); 4829 memset(flush_buf, 0, 4);
4391 return_code = sendcmd_withirq(CCISS_CACHE_FLUSH, h->ctlr, flush_buf, 4830 return_code = sendcmd_withirq(h, CCISS_CACHE_FLUSH, flush_buf,
4392 4, 0, CTLR_LUNID, TYPE_CMD); 4831 4, 0, CTLR_LUNID, TYPE_CMD);
4393 kfree(flush_buf); 4832 kfree(flush_buf);
4394 if (return_code != IO_OK) 4833 if (return_code != IO_OK)
4395 printk(KERN_WARNING "cciss%d: Error flushing cache\n", 4834 dev_warn(&h->pdev->dev, "Error flushing cache\n");
4396 h->ctlr);
4397 h->access.set_intr_mask(h, CCISS_INTR_OFF); 4835 h->access.set_intr_mask(h, CCISS_INTR_OFF);
4398 free_irq(h->intr[2], h); 4836 free_irq(h->intr[PERF_MODE_INT], h);
4399} 4837}
4400 4838
4401static void __devexit cciss_remove_one(struct pci_dev *pdev) 4839static void __devexit cciss_remove_one(struct pci_dev *pdev)
4402{ 4840{
4403 ctlr_info_t *tmp_ptr; 4841 ctlr_info_t *h;
4404 int i, j; 4842 int i, j;
4405 4843
4406 if (pci_get_drvdata(pdev) == NULL) { 4844 if (pci_get_drvdata(pdev) == NULL) {
4407 printk(KERN_ERR "cciss: Unable to remove device \n"); 4845 dev_err(&pdev->dev, "Unable to remove device\n");
4408 return; 4846 return;
4409 } 4847 }
4410 4848
4411 tmp_ptr = pci_get_drvdata(pdev); 4849 h = pci_get_drvdata(pdev);
4412 i = tmp_ptr->ctlr; 4850 i = h->ctlr;
4413 if (hba[i] == NULL) { 4851 if (hba[i] == NULL) {
4414 printk(KERN_ERR "cciss: device appears to " 4852 dev_err(&pdev->dev, "device appears to already be removed\n");
4415 "already be removed \n");
4416 return; 4853 return;
4417 } 4854 }
4418 4855
4419 mutex_lock(&hba[i]->busy_shutting_down); 4856 mutex_lock(&h->busy_shutting_down);
4420 4857
4421 remove_from_scan_list(hba[i]); 4858 remove_from_scan_list(h);
4422 remove_proc_entry(hba[i]->devname, proc_cciss); 4859 remove_proc_entry(h->devname, proc_cciss);
4423 unregister_blkdev(hba[i]->major, hba[i]->devname); 4860 unregister_blkdev(h->major, h->devname);
4424 4861
4425 /* remove it from the disk list */ 4862 /* remove it from the disk list */
4426 for (j = 0; j < CISS_MAX_LUN; j++) { 4863 for (j = 0; j < CISS_MAX_LUN; j++) {
4427 struct gendisk *disk = hba[i]->gendisk[j]; 4864 struct gendisk *disk = h->gendisk[j];
4428 if (disk) { 4865 if (disk) {
4429 struct request_queue *q = disk->queue; 4866 struct request_queue *q = disk->queue;
4430 4867
4431 if (disk->flags & GENHD_FL_UP) { 4868 if (disk->flags & GENHD_FL_UP) {
4432 cciss_destroy_ld_sysfs_entry(hba[i], j, 1); 4869 cciss_destroy_ld_sysfs_entry(h, j, 1);
4433 del_gendisk(disk); 4870 del_gendisk(disk);
4434 } 4871 }
4435 if (q) 4872 if (q)
@@ -4438,39 +4875,41 @@ static void __devexit cciss_remove_one(struct pci_dev *pdev)
4438 } 4875 }
4439 4876
4440#ifdef CONFIG_CISS_SCSI_TAPE 4877#ifdef CONFIG_CISS_SCSI_TAPE
4441 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */ 4878 cciss_unregister_scsi(h); /* unhook from SCSI subsystem */
4442#endif 4879#endif
4443 4880
4444 cciss_shutdown(pdev); 4881 cciss_shutdown(pdev);
4445 4882
4446#ifdef CONFIG_PCI_MSI 4883#ifdef CONFIG_PCI_MSI
4447 if (hba[i]->msix_vector) 4884 if (h->msix_vector)
4448 pci_disable_msix(hba[i]->pdev); 4885 pci_disable_msix(h->pdev);
4449 else if (hba[i]->msi_vector) 4886 else if (h->msi_vector)
4450 pci_disable_msi(hba[i]->pdev); 4887 pci_disable_msi(h->pdev);
4451#endif /* CONFIG_PCI_MSI */ 4888#endif /* CONFIG_PCI_MSI */
4452 4889
4453 iounmap(hba[i]->vaddr); 4890 iounmap(h->transtable);
4891 iounmap(h->cfgtable);
4892 iounmap(h->vaddr);
4454 4893
4455 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(CommandList_struct), 4894 pci_free_consistent(h->pdev, h->nr_cmds * sizeof(CommandList_struct),
4456 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle); 4895 h->cmd_pool, h->cmd_pool_dhandle);
4457 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(ErrorInfo_struct), 4896 pci_free_consistent(h->pdev, h->nr_cmds * sizeof(ErrorInfo_struct),
4458 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle); 4897 h->errinfo_pool, h->errinfo_pool_dhandle);
4459 kfree(hba[i]->cmd_pool_bits); 4898 kfree(h->cmd_pool_bits);
4460 /* Free up sg elements */ 4899 /* Free up sg elements */
4461 for (j = 0; j < hba[i]->nr_cmds; j++) 4900 for (j = 0; j < h->nr_cmds; j++)
4462 kfree(hba[i]->scatter_list[j]); 4901 kfree(h->scatter_list[j]);
4463 kfree(hba[i]->scatter_list); 4902 kfree(h->scatter_list);
4464 cciss_free_sg_chain_blocks(hba[i]->cmd_sg_list, hba[i]->nr_cmds); 4903 cciss_free_sg_chain_blocks(h->cmd_sg_list, h->nr_cmds);
4465 /* 4904 /*
4466 * Deliberately omit pci_disable_device(): it does something nasty to 4905 * Deliberately omit pci_disable_device(): it does something nasty to
4467 * Smart Array controllers that pci_enable_device does not undo 4906 * Smart Array controllers that pci_enable_device does not undo
4468 */ 4907 */
4469 pci_release_regions(pdev); 4908 pci_release_regions(pdev);
4470 pci_set_drvdata(pdev, NULL); 4909 pci_set_drvdata(pdev, NULL);
4471 cciss_destroy_hba_sysfs_entry(hba[i]); 4910 cciss_destroy_hba_sysfs_entry(h);
4472 mutex_unlock(&hba[i]->busy_shutting_down); 4911 mutex_unlock(&h->busy_shutting_down);
4473 free_hba(i); 4912 free_hba(h);
4474} 4913}
4475 4914
4476static struct pci_driver cciss_pci_driver = { 4915static struct pci_driver cciss_pci_driver = {
@@ -4495,7 +4934,6 @@ static int __init cciss_init(void)
4495 * array of them, the size must be a multiple of 8 bytes. 4934 * array of them, the size must be a multiple of 8 bytes.
4496 */ 4935 */
4497 BUILD_BUG_ON(sizeof(CommandList_struct) % COMMANDLIST_ALIGNMENT); 4936 BUILD_BUG_ON(sizeof(CommandList_struct) % COMMANDLIST_ALIGNMENT);
4498
4499 printk(KERN_INFO DRIVER_NAME "\n"); 4937 printk(KERN_INFO DRIVER_NAME "\n");
4500 4938
4501 err = bus_register(&cciss_bus_type); 4939 err = bus_register(&cciss_bus_type);
@@ -4532,8 +4970,8 @@ static void __exit cciss_cleanup(void)
4532 /* double check that all controller entrys have been removed */ 4970 /* double check that all controller entrys have been removed */
4533 for (i = 0; i < MAX_CTLR; i++) { 4971 for (i = 0; i < MAX_CTLR; i++) {
4534 if (hba[i] != NULL) { 4972 if (hba[i] != NULL) {
4535 printk(KERN_WARNING "cciss: had to remove" 4973 dev_warn(&hba[i]->pdev->dev,
4536 " controller %d\n", i); 4974 "had to remove controller\n");
4537 cciss_remove_one(hba[i]->pdev); 4975 cciss_remove_one(hba[i]->pdev);
4538 } 4976 }
4539 } 4977 }
@@ -4542,46 +4980,5 @@ static void __exit cciss_cleanup(void)
4542 bus_unregister(&cciss_bus_type); 4980 bus_unregister(&cciss_bus_type);
4543} 4981}
4544 4982
4545static void fail_all_cmds(unsigned long ctlr)
4546{
4547 /* If we get here, the board is apparently dead. */
4548 ctlr_info_t *h = hba[ctlr];
4549 CommandList_struct *c;
4550 unsigned long flags;
4551
4552 printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
4553 h->alive = 0; /* the controller apparently died... */
4554
4555 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
4556
4557 pci_disable_device(h->pdev); /* Make sure it is really dead. */
4558
4559 /* move everything off the request queue onto the completed queue */
4560 while (!hlist_empty(&h->reqQ)) {
4561 c = hlist_entry(h->reqQ.first, CommandList_struct, list);
4562 removeQ(c);
4563 h->Qdepth--;
4564 addQ(&h->cmpQ, c);
4565 }
4566
4567 /* Now, fail everything on the completed queue with a HW error */
4568 while (!hlist_empty(&h->cmpQ)) {
4569 c = hlist_entry(h->cmpQ.first, CommandList_struct, list);
4570 removeQ(c);
4571 if (c->cmd_type != CMD_MSG_STALE)
4572 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
4573 if (c->cmd_type == CMD_RWREQ) {
4574 complete_command(h, c, 0);
4575 } else if (c->cmd_type == CMD_IOCTL_PEND)
4576 complete(c->waiting);
4577#ifdef CONFIG_CISS_SCSI_TAPE
4578 else if (c->cmd_type == CMD_SCSI)
4579 complete_scsi_command(c, 0, 0);
4580#endif
4581 }
4582 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
4583 return;
4584}
4585
4586module_init(cciss_init); 4983module_init(cciss_init);
4587module_exit(cciss_cleanup); 4984module_exit(cciss_cleanup);
diff --git a/drivers/block/cciss.h b/drivers/block/cciss.h
index c5d411174db..ae340ffc8f8 100644
--- a/drivers/block/cciss.h
+++ b/drivers/block/cciss.h
@@ -25,7 +25,7 @@ struct access_method {
25 void (*submit_command)(ctlr_info_t *h, CommandList_struct *c); 25 void (*submit_command)(ctlr_info_t *h, CommandList_struct *c);
26 void (*set_intr_mask)(ctlr_info_t *h, unsigned long val); 26 void (*set_intr_mask)(ctlr_info_t *h, unsigned long val);
27 unsigned long (*fifo_full)(ctlr_info_t *h); 27 unsigned long (*fifo_full)(ctlr_info_t *h);
28 unsigned long (*intr_pending)(ctlr_info_t *h); 28 bool (*intr_pending)(ctlr_info_t *h);
29 unsigned long (*command_completed)(ctlr_info_t *h); 29 unsigned long (*command_completed)(ctlr_info_t *h);
30}; 30};
31typedef struct _drive_info_struct 31typedef struct _drive_info_struct
@@ -85,8 +85,8 @@ struct ctlr_info
85 int max_cmd_sgentries; 85 int max_cmd_sgentries;
86 SGDescriptor_struct **cmd_sg_list; 86 SGDescriptor_struct **cmd_sg_list;
87 87
88# define DOORBELL_INT 0 88# define PERF_MODE_INT 0
89# define PERF_MODE_INT 1 89# define DOORBELL_INT 1
90# define SIMPLE_MODE_INT 2 90# define SIMPLE_MODE_INT 2
91# define MEMQ_MODE_INT 3 91# define MEMQ_MODE_INT 3
92 unsigned int intr[4]; 92 unsigned int intr[4];
@@ -137,10 +137,27 @@ struct ctlr_info
137 struct list_head scan_list; 137 struct list_head scan_list;
138 struct completion scan_wait; 138 struct completion scan_wait;
139 struct device dev; 139 struct device dev;
140 /*
141 * Performant mode tables.
142 */
143 u32 trans_support;
144 u32 trans_offset;
145 struct TransTable_struct *transtable;
146 unsigned long transMethod;
147
148 /*
149 * Performant mode completion buffer
150 */
151 u64 *reply_pool;
152 dma_addr_t reply_pool_dhandle;
153 u64 *reply_pool_head;
154 size_t reply_pool_size;
155 unsigned char reply_pool_wraparound;
156 u32 *blockFetchTable;
140}; 157};
141 158
142/* Defining the diffent access_menthods */ 159/* Defining the diffent access_methods
143/* 160 *
144 * Memory mapped FIFO interface (SMART 53xx cards) 161 * Memory mapped FIFO interface (SMART 53xx cards)
145 */ 162 */
146#define SA5_DOORBELL 0x20 163#define SA5_DOORBELL 0x20
@@ -159,19 +176,47 @@ struct ctlr_info
159#define SA5B_INTR_PENDING 0x04 176#define SA5B_INTR_PENDING 0x04
160#define FIFO_EMPTY 0xffffffff 177#define FIFO_EMPTY 0xffffffff
161#define CCISS_FIRMWARE_READY 0xffff0000 /* value in scratchpad register */ 178#define CCISS_FIRMWARE_READY 0xffff0000 /* value in scratchpad register */
179/* Perf. mode flags */
180#define SA5_PERF_INTR_PENDING 0x04
181#define SA5_PERF_INTR_OFF 0x05
182#define SA5_OUTDB_STATUS_PERF_BIT 0x01
183#define SA5_OUTDB_CLEAR_PERF_BIT 0x01
184#define SA5_OUTDB_CLEAR 0xA0
185#define SA5_OUTDB_CLEAR_PERF_BIT 0x01
186#define SA5_OUTDB_STATUS 0x9C
187
162 188
163#define CISS_ERROR_BIT 0x02 189#define CISS_ERROR_BIT 0x02
164 190
165#define CCISS_INTR_ON 1 191#define CCISS_INTR_ON 1
166#define CCISS_INTR_OFF 0 192#define CCISS_INTR_OFF 0
193
194
195/* CCISS_BOARD_READY_WAIT_SECS is how long to wait for a board
196 * to become ready, in seconds, before giving up on it.
197 * CCISS_BOARD_READY_POLL_INTERVAL_MSECS * is how long to wait
198 * between polling the board to see if it is ready, in
199 * milliseconds. CCISS_BOARD_READY_ITERATIONS is derived
200 * the above.
201 */
202#define CCISS_BOARD_READY_WAIT_SECS (120)
203#define CCISS_BOARD_READY_POLL_INTERVAL_MSECS (100)
204#define CCISS_BOARD_READY_ITERATIONS \
205 ((CCISS_BOARD_READY_WAIT_SECS * 1000) / \
206 CCISS_BOARD_READY_POLL_INTERVAL_MSECS)
207#define CCISS_POST_RESET_PAUSE_MSECS (3000)
208#define CCISS_POST_RESET_NOOP_INTERVAL_MSECS (1000)
209#define CCISS_POST_RESET_NOOP_RETRIES (12)
210
167/* 211/*
168 Send the command to the hardware 212 Send the command to the hardware
169*/ 213*/
170static void SA5_submit_command( ctlr_info_t *h, CommandList_struct *c) 214static void SA5_submit_command( ctlr_info_t *h, CommandList_struct *c)
171{ 215{
172#ifdef CCISS_DEBUG 216#ifdef CCISS_DEBUG
173 printk("Sending %x - down to controller\n", c->busaddr ); 217 printk(KERN_WARNING "cciss%d: Sending %08x - down to controller\n",
174#endif /* CCISS_DEBUG */ 218 h->ctlr, c->busaddr);
219#endif /* CCISS_DEBUG */
175 writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET); 220 writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
176 h->commands_outstanding++; 221 h->commands_outstanding++;
177 if ( h->commands_outstanding > h->max_outstanding) 222 if ( h->commands_outstanding > h->max_outstanding)
@@ -214,6 +259,20 @@ static void SA5B_intr_mask(ctlr_info_t *h, unsigned long val)
214 h->vaddr + SA5_REPLY_INTR_MASK_OFFSET); 259 h->vaddr + SA5_REPLY_INTR_MASK_OFFSET);
215 } 260 }
216} 261}
262
263/* Performant mode intr_mask */
264static void SA5_performant_intr_mask(ctlr_info_t *h, unsigned long val)
265{
266 if (val) { /* turn on interrupts */
267 h->interrupts_enabled = 1;
268 writel(0, h->vaddr + SA5_REPLY_INTR_MASK_OFFSET);
269 } else {
270 h->interrupts_enabled = 0;
271 writel(SA5_PERF_INTR_OFF,
272 h->vaddr + SA5_REPLY_INTR_MASK_OFFSET);
273 }
274}
275
217/* 276/*
218 * Returns true if fifo is full. 277 * Returns true if fifo is full.
219 * 278 *
@@ -250,10 +309,44 @@ static unsigned long SA5_completed(ctlr_info_t *h)
250 return ( register_value); 309 return ( register_value);
251 310
252} 311}
312
313/* Performant mode command completed */
314static unsigned long SA5_performant_completed(ctlr_info_t *h)
315{
316 unsigned long register_value = FIFO_EMPTY;
317
318 /* flush the controller write of the reply queue by reading
319 * outbound doorbell status register.
320 */
321 register_value = readl(h->vaddr + SA5_OUTDB_STATUS);
322 /* msi auto clears the interrupt pending bit. */
323 if (!(h->msi_vector || h->msix_vector)) {
324 writel(SA5_OUTDB_CLEAR_PERF_BIT, h->vaddr + SA5_OUTDB_CLEAR);
325 /* Do a read in order to flush the write to the controller
326 * (as per spec.)
327 */
328 register_value = readl(h->vaddr + SA5_OUTDB_STATUS);
329 }
330
331 if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
332 register_value = *(h->reply_pool_head);
333 (h->reply_pool_head)++;
334 h->commands_outstanding--;
335 } else {
336 register_value = FIFO_EMPTY;
337 }
338 /* Check for wraparound */
339 if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
340 h->reply_pool_head = h->reply_pool;
341 h->reply_pool_wraparound ^= 1;
342 }
343
344 return register_value;
345}
253/* 346/*
254 * Returns true if an interrupt is pending.. 347 * Returns true if an interrupt is pending..
255 */ 348 */
256static unsigned long SA5_intr_pending(ctlr_info_t *h) 349static bool SA5_intr_pending(ctlr_info_t *h)
257{ 350{
258 unsigned long register_value = 351 unsigned long register_value =
259 readl(h->vaddr + SA5_INTR_STATUS); 352 readl(h->vaddr + SA5_INTR_STATUS);
@@ -268,7 +361,7 @@ static unsigned long SA5_intr_pending(ctlr_info_t *h)
268/* 361/*
269 * Returns true if an interrupt is pending.. 362 * Returns true if an interrupt is pending..
270 */ 363 */
271static unsigned long SA5B_intr_pending(ctlr_info_t *h) 364static bool SA5B_intr_pending(ctlr_info_t *h)
272{ 365{
273 unsigned long register_value = 366 unsigned long register_value =
274 readl(h->vaddr + SA5_INTR_STATUS); 367 readl(h->vaddr + SA5_INTR_STATUS);
@@ -280,6 +373,20 @@ static unsigned long SA5B_intr_pending(ctlr_info_t *h)
280 return 0 ; 373 return 0 ;
281} 374}
282 375
376static bool SA5_performant_intr_pending(ctlr_info_t *h)
377{
378 unsigned long register_value = readl(h->vaddr + SA5_INTR_STATUS);
379
380 if (!register_value)
381 return false;
382
383 if (h->msi_vector || h->msix_vector)
384 return true;
385
386 /* Read outbound doorbell to flush */
387 register_value = readl(h->vaddr + SA5_OUTDB_STATUS);
388 return register_value & SA5_OUTDB_STATUS_PERF_BIT;
389}
283 390
284static struct access_method SA5_access = { 391static struct access_method SA5_access = {
285 SA5_submit_command, 392 SA5_submit_command,
@@ -297,6 +404,14 @@ static struct access_method SA5B_access = {
297 SA5_completed, 404 SA5_completed,
298}; 405};
299 406
407static struct access_method SA5_performant_access = {
408 SA5_submit_command,
409 SA5_performant_intr_mask,
410 SA5_fifo_full,
411 SA5_performant_intr_pending,
412 SA5_performant_completed,
413};
414
300struct board_type { 415struct board_type {
301 __u32 board_id; 416 __u32 board_id;
302 char *product_name; 417 char *product_name;
@@ -304,6 +419,4 @@ struct board_type {
304 int nr_cmds; /* Max cmds this kind of ctlr can handle. */ 419 int nr_cmds; /* Max cmds this kind of ctlr can handle. */
305}; 420};
306 421
307#define CCISS_LOCK(i) (&hba[i]->lock)
308
309#endif /* CCISS_H */ 422#endif /* CCISS_H */
diff --git a/drivers/block/cciss_cmd.h b/drivers/block/cciss_cmd.h
index e624ff959cb..eb060f1b00b 100644
--- a/drivers/block/cciss_cmd.h
+++ b/drivers/block/cciss_cmd.h
@@ -52,8 +52,10 @@
52/* Configuration Table */ 52/* Configuration Table */
53#define CFGTBL_ChangeReq 0x00000001l 53#define CFGTBL_ChangeReq 0x00000001l
54#define CFGTBL_AccCmds 0x00000001l 54#define CFGTBL_AccCmds 0x00000001l
55#define DOORBELL_CTLR_RESET 0x00000004l
55 56
56#define CFGTBL_Trans_Simple 0x00000002l 57#define CFGTBL_Trans_Simple 0x00000002l
58#define CFGTBL_Trans_Performant 0x00000004l
57 59
58#define CFGTBL_BusType_Ultra2 0x00000001l 60#define CFGTBL_BusType_Ultra2 0x00000001l
59#define CFGTBL_BusType_Ultra3 0x00000002l 61#define CFGTBL_BusType_Ultra3 0x00000002l
@@ -173,12 +175,15 @@ typedef struct _SGDescriptor_struct {
173 * PAD_64 can be adjusted independently as needed for 32-bit 175 * PAD_64 can be adjusted independently as needed for 32-bit
174 * and 64-bits systems. 176 * and 64-bits systems.
175 */ 177 */
176#define COMMANDLIST_ALIGNMENT (8) 178#define COMMANDLIST_ALIGNMENT (32)
177#define IS_64_BIT ((sizeof(long) - 4)/4) 179#define IS_64_BIT ((sizeof(long) - 4)/4)
178#define IS_32_BIT (!IS_64_BIT) 180#define IS_32_BIT (!IS_64_BIT)
179#define PAD_32 (0) 181#define PAD_32 (0)
180#define PAD_64 (4) 182#define PAD_64 (4)
181#define PADSIZE (IS_32_BIT * PAD_32 + IS_64_BIT * PAD_64) 183#define PADSIZE (IS_32_BIT * PAD_32 + IS_64_BIT * PAD_64)
184#define DIRECT_LOOKUP_BIT 0x10
185#define DIRECT_LOOKUP_SHIFT 5
186
182typedef struct _CommandList_struct { 187typedef struct _CommandList_struct {
183 CommandListHeader_struct Header; 188 CommandListHeader_struct Header;
184 RequestBlock_struct Request; 189 RequestBlock_struct Request;
@@ -195,7 +200,7 @@ typedef struct _CommandList_struct {
195 struct completion *waiting; 200 struct completion *waiting;
196 int retry_count; 201 int retry_count;
197 void * scsi_cmd; 202 void * scsi_cmd;
198 char pad[PADSIZE]; 203 char pad[PADSIZE];
199} CommandList_struct; 204} CommandList_struct;
200 205
201/* Configuration Table Structure */ 206/* Configuration Table Structure */
@@ -209,12 +214,15 @@ typedef struct _HostWrite_struct {
209typedef struct _CfgTable_struct { 214typedef struct _CfgTable_struct {
210 BYTE Signature[4]; 215 BYTE Signature[4];
211 DWORD SpecValence; 216 DWORD SpecValence;
217#define SIMPLE_MODE 0x02
218#define PERFORMANT_MODE 0x04
219#define MEMQ_MODE 0x08
212 DWORD TransportSupport; 220 DWORD TransportSupport;
213 DWORD TransportActive; 221 DWORD TransportActive;
214 HostWrite_struct HostWrite; 222 HostWrite_struct HostWrite;
215 DWORD CmdsOutMax; 223 DWORD CmdsOutMax;
216 DWORD BusTypes; 224 DWORD BusTypes;
217 DWORD Reserved; 225 DWORD TransMethodOffset;
218 BYTE ServerName[16]; 226 BYTE ServerName[16];
219 DWORD HeartBeat; 227 DWORD HeartBeat;
220 DWORD SCSI_Prefetch; 228 DWORD SCSI_Prefetch;
@@ -222,6 +230,28 @@ typedef struct _CfgTable_struct {
222 DWORD MaxLogicalUnits; 230 DWORD MaxLogicalUnits;
223 DWORD MaxPhysicalDrives; 231 DWORD MaxPhysicalDrives;
224 DWORD MaxPhysicalDrivesPerLogicalUnit; 232 DWORD MaxPhysicalDrivesPerLogicalUnit;
233 DWORD MaxPerformantModeCommands;
234 u8 reserved[0x78 - 0x58];
235 u32 misc_fw_support; /* offset 0x78 */
236#define MISC_FW_DOORBELL_RESET (0x02)
225} CfgTable_struct; 237} CfgTable_struct;
238
239struct TransTable_struct {
240 u32 BlockFetch0;
241 u32 BlockFetch1;
242 u32 BlockFetch2;
243 u32 BlockFetch3;
244 u32 BlockFetch4;
245 u32 BlockFetch5;
246 u32 BlockFetch6;
247 u32 BlockFetch7;
248 u32 RepQSize;
249 u32 RepQCount;
250 u32 RepQCtrAddrLow32;
251 u32 RepQCtrAddrHigh32;
252 u32 RepQAddr0Low32;
253 u32 RepQAddr0High32;
254};
255
226#pragma pack() 256#pragma pack()
227#endif /* CCISS_CMD_H */ 257#endif /* CCISS_CMD_H */
diff --git a/drivers/block/cciss_scsi.c b/drivers/block/cciss_scsi.c
index 72dae92f3ca..575495f3c4b 100644
--- a/drivers/block/cciss_scsi.c
+++ b/drivers/block/cciss_scsi.c
@@ -44,13 +44,15 @@
44#define CCISS_ABORT_MSG 0x00 44#define CCISS_ABORT_MSG 0x00
45#define CCISS_RESET_MSG 0x01 45#define CCISS_RESET_MSG 0x01
46 46
47static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff, 47static int fill_cmd(ctlr_info_t *h, CommandList_struct *c, __u8 cmd, void *buff,
48 size_t size, 48 size_t size,
49 __u8 page_code, unsigned char *scsi3addr, 49 __u8 page_code, unsigned char *scsi3addr,
50 int cmd_type); 50 int cmd_type);
51 51
52static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool); 52static CommandList_struct *cmd_alloc(ctlr_info_t *h);
53static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool); 53static CommandList_struct *cmd_special_alloc(ctlr_info_t *h);
54static void cmd_free(ctlr_info_t *h, CommandList_struct *c);
55static void cmd_special_free(ctlr_info_t *h, CommandList_struct *c);
54 56
55static int cciss_scsi_proc_info( 57static int cciss_scsi_proc_info(
56 struct Scsi_Host *sh, 58 struct Scsi_Host *sh,
@@ -93,8 +95,8 @@ static struct scsi_host_template cciss_driver_template = {
93 95
94#pragma pack(1) 96#pragma pack(1)
95 97
96#define SCSI_PAD_32 0 98#define SCSI_PAD_32 8
97#define SCSI_PAD_64 0 99#define SCSI_PAD_64 8
98 100
99struct cciss_scsi_cmd_stack_elem_t { 101struct cciss_scsi_cmd_stack_elem_t {
100 CommandList_struct cmd; 102 CommandList_struct cmd;
@@ -127,16 +129,16 @@ struct cciss_scsi_adapter_data_t {
127 spinlock_t lock; // to protect ccissscsi[ctlr]; 129 spinlock_t lock; // to protect ccissscsi[ctlr];
128}; 130};
129 131
130#define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( \ 132#define CPQ_TAPE_LOCK(h, flags) spin_lock_irqsave( \
131 &hba[ctlr]->scsi_ctlr->lock, flags); 133 &h->scsi_ctlr->lock, flags);
132#define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( \ 134#define CPQ_TAPE_UNLOCK(h, flags) spin_unlock_irqrestore( \
133 &hba[ctlr]->scsi_ctlr->lock, flags); 135 &h->scsi_ctlr->lock, flags);
134 136
135static CommandList_struct * 137static CommandList_struct *
136scsi_cmd_alloc(ctlr_info_t *h) 138scsi_cmd_alloc(ctlr_info_t *h)
137{ 139{
138 /* assume only one process in here at a time, locking done by caller. */ 140 /* assume only one process in here at a time, locking done by caller. */
139 /* use CCISS_LOCK(ctlr) */ 141 /* use h->lock */
140 /* might be better to rewrite how we allocate scsi commands in a way that */ 142 /* might be better to rewrite how we allocate scsi commands in a way that */
141 /* needs no locking at all. */ 143 /* needs no locking at all. */
142 144
@@ -177,10 +179,10 @@ scsi_cmd_alloc(ctlr_info_t *h)
177} 179}
178 180
179static void 181static void
180scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd) 182scsi_cmd_free(ctlr_info_t *h, CommandList_struct *c)
181{ 183{
182 /* assume only one process in here at a time, locking done by caller. */ 184 /* assume only one process in here at a time, locking done by caller. */
183 /* use CCISS_LOCK(ctlr) */ 185 /* use h->lock */
184 /* drop the free memory chunk on top of the stack. */ 186 /* drop the free memory chunk on top of the stack. */
185 187
186 struct cciss_scsi_adapter_data_t *sa; 188 struct cciss_scsi_adapter_data_t *sa;
@@ -190,22 +192,23 @@ scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
190 stk = &sa->cmd_stack; 192 stk = &sa->cmd_stack;
191 stk->top++; 193 stk->top++;
192 if (stk->top >= CMD_STACK_SIZE) { 194 if (stk->top >= CMD_STACK_SIZE) {
193 printk("cciss: scsi_cmd_free called too many times.\n"); 195 dev_err(&h->pdev->dev,
196 "scsi_cmd_free called too many times.\n");
194 BUG(); 197 BUG();
195 } 198 }
196 stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd; 199 stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) c;
197} 200}
198 201
199static int 202static int
200scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa) 203scsi_cmd_stack_setup(ctlr_info_t *h, struct cciss_scsi_adapter_data_t *sa)
201{ 204{
202 int i; 205 int i;
203 struct cciss_scsi_cmd_stack_t *stk; 206 struct cciss_scsi_cmd_stack_t *stk;
204 size_t size; 207 size_t size;
205 208
206 sa->cmd_sg_list = cciss_allocate_sg_chain_blocks(hba[ctlr], 209 sa->cmd_sg_list = cciss_allocate_sg_chain_blocks(h,
207 hba[ctlr]->chainsize, CMD_STACK_SIZE); 210 h->chainsize, CMD_STACK_SIZE);
208 if (!sa->cmd_sg_list && hba[ctlr]->chainsize > 0) 211 if (!sa->cmd_sg_list && h->chainsize > 0)
209 return -ENOMEM; 212 return -ENOMEM;
210 213
211 stk = &sa->cmd_stack; 214 stk = &sa->cmd_stack;
@@ -215,7 +218,7 @@ scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
215 BUILD_BUG_ON((sizeof(*stk->pool) % COMMANDLIST_ALIGNMENT) != 0); 218 BUILD_BUG_ON((sizeof(*stk->pool) % COMMANDLIST_ALIGNMENT) != 0);
216 /* pci_alloc_consistent guarantees 32-bit DMA address will be used */ 219 /* pci_alloc_consistent guarantees 32-bit DMA address will be used */
217 stk->pool = (struct cciss_scsi_cmd_stack_elem_t *) 220 stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
218 pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle); 221 pci_alloc_consistent(h->pdev, size, &stk->cmd_pool_handle);
219 222
220 if (stk->pool == NULL) { 223 if (stk->pool == NULL) {
221 cciss_free_sg_chain_blocks(sa->cmd_sg_list, CMD_STACK_SIZE); 224 cciss_free_sg_chain_blocks(sa->cmd_sg_list, CMD_STACK_SIZE);
@@ -234,23 +237,22 @@ scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
234} 237}
235 238
236static void 239static void
237scsi_cmd_stack_free(int ctlr) 240scsi_cmd_stack_free(ctlr_info_t *h)
238{ 241{
239 struct cciss_scsi_adapter_data_t *sa; 242 struct cciss_scsi_adapter_data_t *sa;
240 struct cciss_scsi_cmd_stack_t *stk; 243 struct cciss_scsi_cmd_stack_t *stk;
241 size_t size; 244 size_t size;
242 245
243 sa = hba[ctlr]->scsi_ctlr; 246 sa = h->scsi_ctlr;
244 stk = &sa->cmd_stack; 247 stk = &sa->cmd_stack;
245 if (stk->top != CMD_STACK_SIZE-1) { 248 if (stk->top != CMD_STACK_SIZE-1) {
246 printk( "cciss: %d scsi commands are still outstanding.\n", 249 dev_warn(&h->pdev->dev,
250 "bug: %d scsi commands are still outstanding.\n",
247 CMD_STACK_SIZE - stk->top); 251 CMD_STACK_SIZE - stk->top);
248 // BUG();
249 printk("WE HAVE A BUG HERE!!! stk=0x%p\n", stk);
250 } 252 }
251 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE; 253 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
252 254
253 pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle); 255 pci_free_consistent(h->pdev, size, stk->pool, stk->cmd_pool_handle);
254 stk->pool = NULL; 256 stk->pool = NULL;
255 cciss_free_sg_chain_blocks(sa->cmd_sg_list, CMD_STACK_SIZE); 257 cciss_free_sg_chain_blocks(sa->cmd_sg_list, CMD_STACK_SIZE);
256} 258}
@@ -342,20 +344,20 @@ print_cmd(CommandList_struct *cp)
342#endif 344#endif
343 345
344static int 346static int
345find_bus_target_lun(int ctlr, int *bus, int *target, int *lun) 347find_bus_target_lun(ctlr_info_t *h, int *bus, int *target, int *lun)
346{ 348{
347 /* finds an unused bus, target, lun for a new device */ 349 /* finds an unused bus, target, lun for a new device */
348 /* assumes hba[ctlr]->scsi_ctlr->lock is held */ 350 /* assumes h->scsi_ctlr->lock is held */
349 int i, found=0; 351 int i, found=0;
350 unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA]; 352 unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
351 353
352 memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA); 354 memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
353 355
354 target_taken[SELF_SCSI_ID] = 1; 356 target_taken[SELF_SCSI_ID] = 1;
355 for (i=0;i<ccissscsi[ctlr].ndevices;i++) 357 for (i = 0; i < ccissscsi[h->ctlr].ndevices; i++)
356 target_taken[ccissscsi[ctlr].dev[i].target] = 1; 358 target_taken[ccissscsi[h->ctlr].dev[i].target] = 1;
357 359
358 for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) { 360 for (i = 0; i < CCISS_MAX_SCSI_DEVS_PER_HBA; i++) {
359 if (!target_taken[i]) { 361 if (!target_taken[i]) {
360 *bus = 0; *target=i; *lun = 0; found=1; 362 *bus = 0; *target=i; *lun = 0; found=1;
361 break; 363 break;
@@ -369,19 +371,19 @@ struct scsi2map {
369}; 371};
370 372
371static int 373static int
372cciss_scsi_add_entry(int ctlr, int hostno, 374cciss_scsi_add_entry(ctlr_info_t *h, int hostno,
373 struct cciss_scsi_dev_t *device, 375 struct cciss_scsi_dev_t *device,
374 struct scsi2map *added, int *nadded) 376 struct scsi2map *added, int *nadded)
375{ 377{
376 /* assumes hba[ctlr]->scsi_ctlr->lock is held */ 378 /* assumes h->scsi_ctlr->lock is held */
377 int n = ccissscsi[ctlr].ndevices; 379 int n = ccissscsi[h->ctlr].ndevices;
378 struct cciss_scsi_dev_t *sd; 380 struct cciss_scsi_dev_t *sd;
379 int i, bus, target, lun; 381 int i, bus, target, lun;
380 unsigned char addr1[8], addr2[8]; 382 unsigned char addr1[8], addr2[8];
381 383
382 if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) { 384 if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
383 printk("cciss%d: Too many devices, " 385 dev_warn(&h->pdev->dev, "Too many devices, "
384 "some will be inaccessible.\n", ctlr); 386 "some will be inaccessible.\n");
385 return -1; 387 return -1;
386 } 388 }
387 389
@@ -397,7 +399,7 @@ cciss_scsi_add_entry(int ctlr, int hostno,
397 memcpy(addr1, device->scsi3addr, 8); 399 memcpy(addr1, device->scsi3addr, 8);
398 addr1[4] = 0; 400 addr1[4] = 0;
399 for (i = 0; i < n; i++) { 401 for (i = 0; i < n; i++) {
400 sd = &ccissscsi[ctlr].dev[i]; 402 sd = &ccissscsi[h->ctlr].dev[i];
401 memcpy(addr2, sd->scsi3addr, 8); 403 memcpy(addr2, sd->scsi3addr, 8);
402 addr2[4] = 0; 404 addr2[4] = 0;
403 /* differ only in byte 4? */ 405 /* differ only in byte 4? */
@@ -410,9 +412,9 @@ cciss_scsi_add_entry(int ctlr, int hostno,
410 } 412 }
411 } 413 }
412 414
413 sd = &ccissscsi[ctlr].dev[n]; 415 sd = &ccissscsi[h->ctlr].dev[n];
414 if (lun == 0) { 416 if (lun == 0) {
415 if (find_bus_target_lun(ctlr, 417 if (find_bus_target_lun(h,
416 &sd->bus, &sd->target, &sd->lun) != 0) 418 &sd->bus, &sd->target, &sd->lun) != 0)
417 return -1; 419 return -1;
418 } else { 420 } else {
@@ -431,37 +433,37 @@ cciss_scsi_add_entry(int ctlr, int hostno,
431 memcpy(sd->device_id, device->device_id, sizeof(sd->device_id)); 433 memcpy(sd->device_id, device->device_id, sizeof(sd->device_id));
432 sd->devtype = device->devtype; 434 sd->devtype = device->devtype;
433 435
434 ccissscsi[ctlr].ndevices++; 436 ccissscsi[h->ctlr].ndevices++;
435 437
436 /* initially, (before registering with scsi layer) we don't 438 /* initially, (before registering with scsi layer) we don't
437 know our hostno and we don't want to print anything first 439 know our hostno and we don't want to print anything first
438 time anyway (the scsi layer's inquiries will show that info) */ 440 time anyway (the scsi layer's inquiries will show that info) */
439 if (hostno != -1) 441 if (hostno != -1)
440 printk("cciss%d: %s device c%db%dt%dl%d added.\n", 442 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
441 ctlr, scsi_device_type(sd->devtype), hostno, 443 scsi_device_type(sd->devtype), hostno,
442 sd->bus, sd->target, sd->lun); 444 sd->bus, sd->target, sd->lun);
443 return 0; 445 return 0;
444} 446}
445 447
446static void 448static void
447cciss_scsi_remove_entry(int ctlr, int hostno, int entry, 449cciss_scsi_remove_entry(ctlr_info_t *h, int hostno, int entry,
448 struct scsi2map *removed, int *nremoved) 450 struct scsi2map *removed, int *nremoved)
449{ 451{
450 /* assumes hba[ctlr]->scsi_ctlr->lock is held */ 452 /* assumes h->ctlr]->scsi_ctlr->lock is held */
451 int i; 453 int i;
452 struct cciss_scsi_dev_t sd; 454 struct cciss_scsi_dev_t sd;
453 455
454 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return; 456 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
455 sd = ccissscsi[ctlr].dev[entry]; 457 sd = ccissscsi[h->ctlr].dev[entry];
456 removed[*nremoved].bus = sd.bus; 458 removed[*nremoved].bus = sd.bus;
457 removed[*nremoved].target = sd.target; 459 removed[*nremoved].target = sd.target;
458 removed[*nremoved].lun = sd.lun; 460 removed[*nremoved].lun = sd.lun;
459 (*nremoved)++; 461 (*nremoved)++;
460 for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++) 462 for (i = entry; i < ccissscsi[h->ctlr].ndevices-1; i++)
461 ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1]; 463 ccissscsi[h->ctlr].dev[i] = ccissscsi[h->ctlr].dev[i+1];
462 ccissscsi[ctlr].ndevices--; 464 ccissscsi[h->ctlr].ndevices--;
463 printk("cciss%d: %s device c%db%dt%dl%d removed.\n", 465 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
464 ctlr, scsi_device_type(sd.devtype), hostno, 466 scsi_device_type(sd.devtype), hostno,
465 sd.bus, sd.target, sd.lun); 467 sd.bus, sd.target, sd.lun);
466} 468}
467 469
@@ -476,24 +478,24 @@ cciss_scsi_remove_entry(int ctlr, int hostno, int entry,
476 (a)[1] == (b)[1] && \ 478 (a)[1] == (b)[1] && \
477 (a)[0] == (b)[0]) 479 (a)[0] == (b)[0])
478 480
479static void fixup_botched_add(int ctlr, char *scsi3addr) 481static void fixup_botched_add(ctlr_info_t *h, char *scsi3addr)
480{ 482{
481 /* called when scsi_add_device fails in order to re-adjust */ 483 /* called when scsi_add_device fails in order to re-adjust */
482 /* ccissscsi[] to match the mid layer's view. */ 484 /* ccissscsi[] to match the mid layer's view. */
483 unsigned long flags; 485 unsigned long flags;
484 int i, j; 486 int i, j;
485 CPQ_TAPE_LOCK(ctlr, flags); 487 CPQ_TAPE_LOCK(h, flags);
486 for (i = 0; i < ccissscsi[ctlr].ndevices; i++) { 488 for (i = 0; i < ccissscsi[h->ctlr].ndevices; i++) {
487 if (memcmp(scsi3addr, 489 if (memcmp(scsi3addr,
488 ccissscsi[ctlr].dev[i].scsi3addr, 8) == 0) { 490 ccissscsi[h->ctlr].dev[i].scsi3addr, 8) == 0) {
489 for (j = i; j < ccissscsi[ctlr].ndevices-1; j++) 491 for (j = i; j < ccissscsi[h->ctlr].ndevices-1; j++)
490 ccissscsi[ctlr].dev[j] = 492 ccissscsi[h->ctlr].dev[j] =
491 ccissscsi[ctlr].dev[j+1]; 493 ccissscsi[h->ctlr].dev[j+1];
492 ccissscsi[ctlr].ndevices--; 494 ccissscsi[h->ctlr].ndevices--;
493 break; 495 break;
494 } 496 }
495 } 497 }
496 CPQ_TAPE_UNLOCK(ctlr, flags); 498 CPQ_TAPE_UNLOCK(h, flags);
497} 499}
498 500
499static int device_is_the_same(struct cciss_scsi_dev_t *dev1, 501static int device_is_the_same(struct cciss_scsi_dev_t *dev1,
@@ -513,7 +515,7 @@ static int device_is_the_same(struct cciss_scsi_dev_t *dev1,
513} 515}
514 516
515static int 517static int
516adjust_cciss_scsi_table(int ctlr, int hostno, 518adjust_cciss_scsi_table(ctlr_info_t *h, int hostno,
517 struct cciss_scsi_dev_t sd[], int nsds) 519 struct cciss_scsi_dev_t sd[], int nsds)
518{ 520{
519 /* sd contains scsi3 addresses and devtypes, but 521 /* sd contains scsi3 addresses and devtypes, but
@@ -534,15 +536,15 @@ adjust_cciss_scsi_table(int ctlr, int hostno,
534 GFP_KERNEL); 536 GFP_KERNEL);
535 537
536 if (!added || !removed) { 538 if (!added || !removed) {
537 printk(KERN_WARNING "cciss%d: Out of memory in " 539 dev_warn(&h->pdev->dev,
538 "adjust_cciss_scsi_table\n", ctlr); 540 "Out of memory in adjust_cciss_scsi_table\n");
539 goto free_and_out; 541 goto free_and_out;
540 } 542 }
541 543
542 CPQ_TAPE_LOCK(ctlr, flags); 544 CPQ_TAPE_LOCK(h, flags);
543 545
544 if (hostno != -1) /* if it's not the first time... */ 546 if (hostno != -1) /* if it's not the first time... */
545 sh = hba[ctlr]->scsi_ctlr->scsi_host; 547 sh = h->scsi_ctlr->scsi_host;
546 548
547 /* find any devices in ccissscsi[] that are not in 549 /* find any devices in ccissscsi[] that are not in
548 sd[] and remove them from ccissscsi[] */ 550 sd[] and remove them from ccissscsi[] */
@@ -550,8 +552,8 @@ adjust_cciss_scsi_table(int ctlr, int hostno,
550 i = 0; 552 i = 0;
551 nremoved = 0; 553 nremoved = 0;
552 nadded = 0; 554 nadded = 0;
553 while(i<ccissscsi[ctlr].ndevices) { 555 while (i < ccissscsi[h->ctlr].ndevices) {
554 csd = &ccissscsi[ctlr].dev[i]; 556 csd = &ccissscsi[h->ctlr].dev[i];
555 found=0; 557 found=0;
556 for (j=0;j<nsds;j++) { 558 for (j=0;j<nsds;j++) {
557 if (SCSI3ADDR_EQ(sd[j].scsi3addr, 559 if (SCSI3ADDR_EQ(sd[j].scsi3addr,
@@ -566,20 +568,18 @@ adjust_cciss_scsi_table(int ctlr, int hostno,
566 568
567 if (found == 0) { /* device no longer present. */ 569 if (found == 0) { /* device no longer present. */
568 changes++; 570 changes++;
569 /* printk("cciss%d: %s device c%db%dt%dl%d removed.\n", 571 cciss_scsi_remove_entry(h, hostno, i,
570 ctlr, scsi_device_type(csd->devtype), hostno,
571 csd->bus, csd->target, csd->lun); */
572 cciss_scsi_remove_entry(ctlr, hostno, i,
573 removed, &nremoved); 572 removed, &nremoved);
574 /* remove ^^^, hence i not incremented */ 573 /* remove ^^^, hence i not incremented */
575 } else if (found == 1) { /* device is different in some way */ 574 } else if (found == 1) { /* device is different in some way */
576 changes++; 575 changes++;
577 printk("cciss%d: device c%db%dt%dl%d has changed.\n", 576 dev_info(&h->pdev->dev,
578 ctlr, hostno, csd->bus, csd->target, csd->lun); 577 "device c%db%dt%dl%d has changed.\n",
579 cciss_scsi_remove_entry(ctlr, hostno, i, 578 hostno, csd->bus, csd->target, csd->lun);
579 cciss_scsi_remove_entry(h, hostno, i,
580 removed, &nremoved); 580 removed, &nremoved);
581 /* remove ^^^, hence i not incremented */ 581 /* remove ^^^, hence i not incremented */
582 if (cciss_scsi_add_entry(ctlr, hostno, &sd[j], 582 if (cciss_scsi_add_entry(h, hostno, &sd[j],
583 added, &nadded) != 0) 583 added, &nadded) != 0)
584 /* we just removed one, so add can't fail. */ 584 /* we just removed one, so add can't fail. */
585 BUG(); 585 BUG();
@@ -601,8 +601,8 @@ adjust_cciss_scsi_table(int ctlr, int hostno,
601 601
602 for (i=0;i<nsds;i++) { 602 for (i=0;i<nsds;i++) {
603 found=0; 603 found=0;
604 for (j=0;j<ccissscsi[ctlr].ndevices;j++) { 604 for (j = 0; j < ccissscsi[h->ctlr].ndevices; j++) {
605 csd = &ccissscsi[ctlr].dev[j]; 605 csd = &ccissscsi[h->ctlr].dev[j];
606 if (SCSI3ADDR_EQ(sd[i].scsi3addr, 606 if (SCSI3ADDR_EQ(sd[i].scsi3addr,
607 csd->scsi3addr)) { 607 csd->scsi3addr)) {
608 if (device_is_the_same(&sd[i], csd)) 608 if (device_is_the_same(&sd[i], csd))
@@ -614,18 +614,18 @@ adjust_cciss_scsi_table(int ctlr, int hostno,
614 } 614 }
615 if (!found) { 615 if (!found) {
616 changes++; 616 changes++;
617 if (cciss_scsi_add_entry(ctlr, hostno, &sd[i], 617 if (cciss_scsi_add_entry(h, hostno, &sd[i],
618 added, &nadded) != 0) 618 added, &nadded) != 0)
619 break; 619 break;
620 } else if (found == 1) { 620 } else if (found == 1) {
621 /* should never happen... */ 621 /* should never happen... */
622 changes++; 622 changes++;
623 printk(KERN_WARNING "cciss%d: device " 623 dev_warn(&h->pdev->dev,
624 "unexpectedly changed\n", ctlr); 624 "device unexpectedly changed\n");
625 /* but if it does happen, we just ignore that device */ 625 /* but if it does happen, we just ignore that device */
626 } 626 }
627 } 627 }
628 CPQ_TAPE_UNLOCK(ctlr, flags); 628 CPQ_TAPE_UNLOCK(h, flags);
629 629
630 /* Don't notify scsi mid layer of any changes the first time through */ 630 /* Don't notify scsi mid layer of any changes the first time through */
631 /* (or if there are no changes) scsi_scan_host will do it later the */ 631 /* (or if there are no changes) scsi_scan_host will do it later the */
@@ -645,9 +645,9 @@ adjust_cciss_scsi_table(int ctlr, int hostno,
645 /* We don't expect to get here. */ 645 /* We don't expect to get here. */
646 /* future cmds to this device will get selection */ 646 /* future cmds to this device will get selection */
647 /* timeout as if the device was gone. */ 647 /* timeout as if the device was gone. */
648 printk(KERN_WARNING "cciss%d: didn't find " 648 dev_warn(&h->pdev->dev, "didn't find "
649 "c%db%dt%dl%d\n for removal.", 649 "c%db%dt%dl%d\n for removal.",
650 ctlr, hostno, removed[i].bus, 650 hostno, removed[i].bus,
651 removed[i].target, removed[i].lun); 651 removed[i].target, removed[i].lun);
652 } 652 }
653 } 653 }
@@ -659,13 +659,12 @@ adjust_cciss_scsi_table(int ctlr, int hostno,
659 added[i].target, added[i].lun); 659 added[i].target, added[i].lun);
660 if (rc == 0) 660 if (rc == 0)
661 continue; 661 continue;
662 printk(KERN_WARNING "cciss%d: scsi_add_device " 662 dev_warn(&h->pdev->dev, "scsi_add_device "
663 "c%db%dt%dl%d failed, device not added.\n", 663 "c%db%dt%dl%d failed, device not added.\n",
664 ctlr, hostno, 664 hostno, added[i].bus, added[i].target, added[i].lun);
665 added[i].bus, added[i].target, added[i].lun);
666 /* now we have to remove it from ccissscsi, */ 665 /* now we have to remove it from ccissscsi, */
667 /* since it didn't get added to scsi mid layer */ 666 /* since it didn't get added to scsi mid layer */
668 fixup_botched_add(ctlr, added[i].scsi3addr); 667 fixup_botched_add(h, added[i].scsi3addr);
669 } 668 }
670 669
671free_and_out: 670free_and_out:
@@ -675,33 +674,33 @@ free_and_out:
675} 674}
676 675
677static int 676static int
678lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr) 677lookup_scsi3addr(ctlr_info_t *h, int bus, int target, int lun, char *scsi3addr)
679{ 678{
680 int i; 679 int i;
681 struct cciss_scsi_dev_t *sd; 680 struct cciss_scsi_dev_t *sd;
682 unsigned long flags; 681 unsigned long flags;
683 682
684 CPQ_TAPE_LOCK(ctlr, flags); 683 CPQ_TAPE_LOCK(h, flags);
685 for (i=0;i<ccissscsi[ctlr].ndevices;i++) { 684 for (i = 0; i < ccissscsi[h->ctlr].ndevices; i++) {
686 sd = &ccissscsi[ctlr].dev[i]; 685 sd = &ccissscsi[h->ctlr].dev[i];
687 if (sd->bus == bus && 686 if (sd->bus == bus &&
688 sd->target == target && 687 sd->target == target &&
689 sd->lun == lun) { 688 sd->lun == lun) {
690 memcpy(scsi3addr, &sd->scsi3addr[0], 8); 689 memcpy(scsi3addr, &sd->scsi3addr[0], 8);
691 CPQ_TAPE_UNLOCK(ctlr, flags); 690 CPQ_TAPE_UNLOCK(h, flags);
692 return 0; 691 return 0;
693 } 692 }
694 } 693 }
695 CPQ_TAPE_UNLOCK(ctlr, flags); 694 CPQ_TAPE_UNLOCK(h, flags);
696 return -1; 695 return -1;
697} 696}
698 697
699static void 698static void
700cciss_scsi_setup(int cntl_num) 699cciss_scsi_setup(ctlr_info_t *h)
701{ 700{
702 struct cciss_scsi_adapter_data_t * shba; 701 struct cciss_scsi_adapter_data_t * shba;
703 702
704 ccissscsi[cntl_num].ndevices = 0; 703 ccissscsi[h->ctlr].ndevices = 0;
705 shba = (struct cciss_scsi_adapter_data_t *) 704 shba = (struct cciss_scsi_adapter_data_t *)
706 kmalloc(sizeof(*shba), GFP_KERNEL); 705 kmalloc(sizeof(*shba), GFP_KERNEL);
707 if (shba == NULL) 706 if (shba == NULL)
@@ -709,35 +708,35 @@ cciss_scsi_setup(int cntl_num)
709 shba->scsi_host = NULL; 708 shba->scsi_host = NULL;
710 spin_lock_init(&shba->lock); 709 spin_lock_init(&shba->lock);
711 shba->registered = 0; 710 shba->registered = 0;
712 if (scsi_cmd_stack_setup(cntl_num, shba) != 0) { 711 if (scsi_cmd_stack_setup(h, shba) != 0) {
713 kfree(shba); 712 kfree(shba);
714 shba = NULL; 713 shba = NULL;
715 } 714 }
716 hba[cntl_num]->scsi_ctlr = shba; 715 h->scsi_ctlr = shba;
717 return; 716 return;
718} 717}
719 718
720static void 719static void complete_scsi_command(CommandList_struct *c, int timeout,
721complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag) 720 __u32 tag)
722{ 721{
723 struct scsi_cmnd *cmd; 722 struct scsi_cmnd *cmd;
724 ctlr_info_t *ctlr; 723 ctlr_info_t *h;
725 ErrorInfo_struct *ei; 724 ErrorInfo_struct *ei;
726 725
727 ei = cp->err_info; 726 ei = c->err_info;
728 727
729 /* First, see if it was a message rather than a command */ 728 /* First, see if it was a message rather than a command */
730 if (cp->Request.Type.Type == TYPE_MSG) { 729 if (c->Request.Type.Type == TYPE_MSG) {
731 cp->cmd_type = CMD_MSG_DONE; 730 c->cmd_type = CMD_MSG_DONE;
732 return; 731 return;
733 } 732 }
734 733
735 cmd = (struct scsi_cmnd *) cp->scsi_cmd; 734 cmd = (struct scsi_cmnd *) c->scsi_cmd;
736 ctlr = hba[cp->ctlr]; 735 h = hba[c->ctlr];
737 736
738 scsi_dma_unmap(cmd); 737 scsi_dma_unmap(cmd);
739 if (cp->Header.SGTotal > ctlr->max_cmd_sgentries) 738 if (c->Header.SGTotal > h->max_cmd_sgentries)
740 cciss_unmap_sg_chain_block(ctlr, cp); 739 cciss_unmap_sg_chain_block(h, c);
741 740
742 cmd->result = (DID_OK << 16); /* host byte */ 741 cmd->result = (DID_OK << 16); /* host byte */
743 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */ 742 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
@@ -764,9 +763,8 @@ complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
764 { 763 {
765#if 0 764#if 0
766 printk(KERN_WARNING "cciss: cmd %p " 765 printk(KERN_WARNING "cciss: cmd %p "
767 "has SCSI Status = %x\n", 766 "has SCSI Status = %x\n",
768 cp, 767 c, ei->ScsiStatus);
769 ei->ScsiStatus);
770#endif 768#endif
771 cmd->result |= (ei->ScsiStatus << 1); 769 cmd->result |= (ei->ScsiStatus << 1);
772 } 770 }
@@ -786,13 +784,13 @@ complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
786 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */ 784 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
787 break; 785 break;
788 case CMD_DATA_OVERRUN: 786 case CMD_DATA_OVERRUN:
789 printk(KERN_WARNING "cciss: cp %p has" 787 dev_warn(&h->pdev->dev, "%p has"
790 " completed with data overrun " 788 " completed with data overrun "
791 "reported\n", cp); 789 "reported\n", c);
792 break; 790 break;
793 case CMD_INVALID: { 791 case CMD_INVALID: {
794 /* print_bytes(cp, sizeof(*cp), 1, 0); 792 /* print_bytes(c, sizeof(*c), 1, 0);
795 print_cmd(cp); */ 793 print_cmd(c); */
796 /* We get CMD_INVALID if you address a non-existent tape drive instead 794 /* We get CMD_INVALID if you address a non-existent tape drive instead
797 of a selection timeout (no response). You will see this if you yank 795 of a selection timeout (no response). You will see this if you yank
798 out a tape drive, then try to access it. This is kind of a shame 796 out a tape drive, then try to access it. This is kind of a shame
@@ -802,54 +800,50 @@ complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
802 } 800 }
803 break; 801 break;
804 case CMD_PROTOCOL_ERR: 802 case CMD_PROTOCOL_ERR:
805 printk(KERN_WARNING "cciss: cp %p has " 803 dev_warn(&h->pdev->dev,
806 "protocol error \n", cp); 804 "%p has protocol error\n", c);
807 break; 805 break;
808 case CMD_HARDWARE_ERR: 806 case CMD_HARDWARE_ERR:
809 cmd->result = DID_ERROR << 16; 807 cmd->result = DID_ERROR << 16;
810 printk(KERN_WARNING "cciss: cp %p had " 808 dev_warn(&h->pdev->dev,
811 " hardware error\n", cp); 809 "%p had hardware error\n", c);
812 break; 810 break;
813 case CMD_CONNECTION_LOST: 811 case CMD_CONNECTION_LOST:
814 cmd->result = DID_ERROR << 16; 812 cmd->result = DID_ERROR << 16;
815 printk(KERN_WARNING "cciss: cp %p had " 813 dev_warn(&h->pdev->dev,
816 "connection lost\n", cp); 814 "%p had connection lost\n", c);
817 break; 815 break;
818 case CMD_ABORTED: 816 case CMD_ABORTED:
819 cmd->result = DID_ABORT << 16; 817 cmd->result = DID_ABORT << 16;
820 printk(KERN_WARNING "cciss: cp %p was " 818 dev_warn(&h->pdev->dev, "%p was aborted\n", c);
821 "aborted\n", cp);
822 break; 819 break;
823 case CMD_ABORT_FAILED: 820 case CMD_ABORT_FAILED:
824 cmd->result = DID_ERROR << 16; 821 cmd->result = DID_ERROR << 16;
825 printk(KERN_WARNING "cciss: cp %p reports " 822 dev_warn(&h->pdev->dev,
826 "abort failed\n", cp); 823 "%p reports abort failed\n", c);
827 break; 824 break;
828 case CMD_UNSOLICITED_ABORT: 825 case CMD_UNSOLICITED_ABORT:
829 cmd->result = DID_ABORT << 16; 826 cmd->result = DID_ABORT << 16;
830 printk(KERN_WARNING "cciss: cp %p aborted " 827 dev_warn(&h->pdev->dev, "%p aborted do to an "
831 "do to an unsolicited abort\n", cp); 828 "unsolicited abort\n", c);
832 break; 829 break;
833 case CMD_TIMEOUT: 830 case CMD_TIMEOUT:
834 cmd->result = DID_TIME_OUT << 16; 831 cmd->result = DID_TIME_OUT << 16;
835 printk(KERN_WARNING "cciss: cp %p timedout\n", 832 dev_warn(&h->pdev->dev, "%p timedout\n", c);
836 cp);
837 break; 833 break;
838 default: 834 default:
839 cmd->result = DID_ERROR << 16; 835 cmd->result = DID_ERROR << 16;
840 printk(KERN_WARNING "cciss: cp %p returned " 836 dev_warn(&h->pdev->dev,
841 "unknown status %x\n", cp, 837 "%p returned unknown status %x\n", c,
842 ei->CommandStatus); 838 ei->CommandStatus);
843 } 839 }
844 } 840 }
845 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
846 // cmd->target, cmd->lun);
847 cmd->scsi_done(cmd); 841 cmd->scsi_done(cmd);
848 scsi_cmd_free(ctlr, cp); 842 scsi_cmd_free(h, c);
849} 843}
850 844
851static int 845static int
852cciss_scsi_detect(int ctlr) 846cciss_scsi_detect(ctlr_info_t *h)
853{ 847{
854 struct Scsi_Host *sh; 848 struct Scsi_Host *sh;
855 int error; 849 int error;
@@ -860,15 +854,15 @@ cciss_scsi_detect(int ctlr)
860 sh->io_port = 0; // good enough? FIXME, 854 sh->io_port = 0; // good enough? FIXME,
861 sh->n_io_port = 0; // I don't think we use these two... 855 sh->n_io_port = 0; // I don't think we use these two...
862 sh->this_id = SELF_SCSI_ID; 856 sh->this_id = SELF_SCSI_ID;
863 sh->sg_tablesize = hba[ctlr]->maxsgentries; 857 sh->sg_tablesize = h->maxsgentries;
864 sh->max_cmd_len = MAX_COMMAND_SIZE; 858 sh->max_cmd_len = MAX_COMMAND_SIZE;
865 859
866 ((struct cciss_scsi_adapter_data_t *) 860 ((struct cciss_scsi_adapter_data_t *)
867 hba[ctlr]->scsi_ctlr)->scsi_host = sh; 861 h->scsi_ctlr)->scsi_host = sh;
868 sh->hostdata[0] = (unsigned long) hba[ctlr]; 862 sh->hostdata[0] = (unsigned long) h;
869 sh->irq = hba[ctlr]->intr[SIMPLE_MODE_INT]; 863 sh->irq = h->intr[SIMPLE_MODE_INT];
870 sh->unique_id = sh->irq; 864 sh->unique_id = sh->irq;
871 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev); 865 error = scsi_add_host(sh, &h->pdev->dev);
872 if (error) 866 if (error)
873 goto fail_host_put; 867 goto fail_host_put;
874 scsi_scan_host(sh); 868 scsi_scan_host(sh);
@@ -882,20 +876,20 @@ cciss_scsi_detect(int ctlr)
882 876
883static void 877static void
884cciss_unmap_one(struct pci_dev *pdev, 878cciss_unmap_one(struct pci_dev *pdev,
885 CommandList_struct *cp, 879 CommandList_struct *c,
886 size_t buflen, 880 size_t buflen,
887 int data_direction) 881 int data_direction)
888{ 882{
889 u64bit addr64; 883 u64bit addr64;
890 884
891 addr64.val32.lower = cp->SG[0].Addr.lower; 885 addr64.val32.lower = c->SG[0].Addr.lower;
892 addr64.val32.upper = cp->SG[0].Addr.upper; 886 addr64.val32.upper = c->SG[0].Addr.upper;
893 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction); 887 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
894} 888}
895 889
896static void 890static void
897cciss_map_one(struct pci_dev *pdev, 891cciss_map_one(struct pci_dev *pdev,
898 CommandList_struct *cp, 892 CommandList_struct *c,
899 unsigned char *buf, 893 unsigned char *buf,
900 size_t buflen, 894 size_t buflen,
901 int data_direction) 895 int data_direction)
@@ -903,164 +897,149 @@ cciss_map_one(struct pci_dev *pdev,
903 __u64 addr64; 897 __u64 addr64;
904 898
905 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction); 899 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
906 cp->SG[0].Addr.lower = 900 c->SG[0].Addr.lower =
907 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF); 901 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
908 cp->SG[0].Addr.upper = 902 c->SG[0].Addr.upper =
909 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF); 903 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
910 cp->SG[0].Len = buflen; 904 c->SG[0].Len = buflen;
911 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */ 905 c->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
912 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */ 906 c->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
913} 907}
914 908
915static int 909static int
916cciss_scsi_do_simple_cmd(ctlr_info_t *c, 910cciss_scsi_do_simple_cmd(ctlr_info_t *h,
917 CommandList_struct *cp, 911 CommandList_struct *c,
918 unsigned char *scsi3addr, 912 unsigned char *scsi3addr,
919 unsigned char *cdb, 913 unsigned char *cdb,
920 unsigned char cdblen, 914 unsigned char cdblen,
921 unsigned char *buf, int bufsize, 915 unsigned char *buf, int bufsize,
922 int direction) 916 int direction)
923{ 917{
924 unsigned long flags;
925 DECLARE_COMPLETION_ONSTACK(wait); 918 DECLARE_COMPLETION_ONSTACK(wait);
926 919
927 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl 920 c->cmd_type = CMD_IOCTL_PEND; /* treat this like an ioctl */
928 cp->scsi_cmd = NULL; 921 c->scsi_cmd = NULL;
929 cp->Header.ReplyQueue = 0; // unused in simple mode 922 c->Header.ReplyQueue = 0; /* unused in simple mode */
930 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN)); 923 memcpy(&c->Header.LUN, scsi3addr, sizeof(c->Header.LUN));
931 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag 924 c->Header.Tag.lower = c->busaddr; /* Use k. address of cmd as tag */
932 // Fill in the request block... 925 // Fill in the request block...
933 926
934 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n", 927 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
935 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3], 928 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
936 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */ 929 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
937 930
938 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB)); 931 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
939 memcpy(cp->Request.CDB, cdb, cdblen); 932 memcpy(c->Request.CDB, cdb, cdblen);
940 cp->Request.Timeout = 0; 933 c->Request.Timeout = 0;
941 cp->Request.CDBLen = cdblen; 934 c->Request.CDBLen = cdblen;
942 cp->Request.Type.Type = TYPE_CMD; 935 c->Request.Type.Type = TYPE_CMD;
943 cp->Request.Type.Attribute = ATTR_SIMPLE; 936 c->Request.Type.Attribute = ATTR_SIMPLE;
944 cp->Request.Type.Direction = direction; 937 c->Request.Type.Direction = direction;
945 938
946 /* Fill in the SG list and do dma mapping */ 939 /* Fill in the SG list and do dma mapping */
947 cciss_map_one(c->pdev, cp, (unsigned char *) buf, 940 cciss_map_one(h->pdev, c, (unsigned char *) buf,
948 bufsize, DMA_FROM_DEVICE); 941 bufsize, DMA_FROM_DEVICE);
949 942
950 cp->waiting = &wait; 943 c->waiting = &wait;
951 944 enqueue_cmd_and_start_io(h, c);
952 /* Put the request on the tail of the request queue */
953 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
954 addQ(&c->reqQ, cp);
955 c->Qdepth++;
956 start_io(c);
957 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
958
959 wait_for_completion(&wait); 945 wait_for_completion(&wait);
960 946
961 /* undo the dma mapping */ 947 /* undo the dma mapping */
962 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE); 948 cciss_unmap_one(h->pdev, c, bufsize, DMA_FROM_DEVICE);
963 return(0); 949 return(0);
964} 950}
965 951
966static void 952static void
967cciss_scsi_interpret_error(CommandList_struct *cp) 953cciss_scsi_interpret_error(ctlr_info_t *h, CommandList_struct *c)
968{ 954{
969 ErrorInfo_struct *ei; 955 ErrorInfo_struct *ei;
970 956
971 ei = cp->err_info; 957 ei = c->err_info;
972 switch(ei->CommandStatus) 958 switch(ei->CommandStatus)
973 { 959 {
974 case CMD_TARGET_STATUS: 960 case CMD_TARGET_STATUS:
975 printk(KERN_WARNING "cciss: cmd %p has " 961 dev_warn(&h->pdev->dev,
976 "completed with errors\n", cp); 962 "cmd %p has completed with errors\n", c);
977 printk(KERN_WARNING "cciss: cmd %p " 963 dev_warn(&h->pdev->dev,
978 "has SCSI Status = %x\n", 964 "cmd %p has SCSI Status = %x\n",
979 cp, 965 c, ei->ScsiStatus);
980 ei->ScsiStatus);
981 if (ei->ScsiStatus == 0) 966 if (ei->ScsiStatus == 0)
982 printk(KERN_WARNING 967 dev_warn(&h->pdev->dev,
983 "cciss:SCSI status is abnormally zero. " 968 "SCSI status is abnormally zero. "
984 "(probably indicates selection timeout " 969 "(probably indicates selection timeout "
985 "reported incorrectly due to a known " 970 "reported incorrectly due to a known "
986 "firmware bug, circa July, 2001.)\n"); 971 "firmware bug, circa July, 2001.)\n");
987 break; 972 break;
988 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */ 973 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
989 printk("UNDERRUN\n"); 974 dev_info(&h->pdev->dev, "UNDERRUN\n");
990 break; 975 break;
991 case CMD_DATA_OVERRUN: 976 case CMD_DATA_OVERRUN:
992 printk(KERN_WARNING "cciss: cp %p has" 977 dev_warn(&h->pdev->dev, "%p has"
993 " completed with data overrun " 978 " completed with data overrun "
994 "reported\n", cp); 979 "reported\n", c);
995 break; 980 break;
996 case CMD_INVALID: { 981 case CMD_INVALID: {
997 /* controller unfortunately reports SCSI passthru's */ 982 /* controller unfortunately reports SCSI passthru's */
998 /* to non-existent targets as invalid commands. */ 983 /* to non-existent targets as invalid commands. */
999 printk(KERN_WARNING "cciss: cp %p is " 984 dev_warn(&h->pdev->dev,
1000 "reported invalid (probably means " 985 "%p is reported invalid (probably means "
1001 "target device no longer present)\n", 986 "target device no longer present)\n", c);
1002 cp); 987 /* print_bytes((unsigned char *) c, sizeof(*c), 1, 0);
1003 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0); 988 print_cmd(c); */
1004 print_cmd(cp); */
1005 } 989 }
1006 break; 990 break;
1007 case CMD_PROTOCOL_ERR: 991 case CMD_PROTOCOL_ERR:
1008 printk(KERN_WARNING "cciss: cp %p has " 992 dev_warn(&h->pdev->dev, "%p has protocol error\n", c);
1009 "protocol error \n", cp);
1010 break; 993 break;
1011 case CMD_HARDWARE_ERR: 994 case CMD_HARDWARE_ERR:
1012 /* cmd->result = DID_ERROR << 16; */ 995 /* cmd->result = DID_ERROR << 16; */
1013 printk(KERN_WARNING "cciss: cp %p had " 996 dev_warn(&h->pdev->dev, "%p had hardware error\n", c);
1014 " hardware error\n", cp);
1015 break; 997 break;
1016 case CMD_CONNECTION_LOST: 998 case CMD_CONNECTION_LOST:
1017 printk(KERN_WARNING "cciss: cp %p had " 999 dev_warn(&h->pdev->dev, "%p had connection lost\n", c);
1018 "connection lost\n", cp);
1019 break; 1000 break;
1020 case CMD_ABORTED: 1001 case CMD_ABORTED:
1021 printk(KERN_WARNING "cciss: cp %p was " 1002 dev_warn(&h->pdev->dev, "%p was aborted\n", c);
1022 "aborted\n", cp);
1023 break; 1003 break;
1024 case CMD_ABORT_FAILED: 1004 case CMD_ABORT_FAILED:
1025 printk(KERN_WARNING "cciss: cp %p reports " 1005 dev_warn(&h->pdev->dev,
1026 "abort failed\n", cp); 1006 "%p reports abort failed\n", c);
1027 break; 1007 break;
1028 case CMD_UNSOLICITED_ABORT: 1008 case CMD_UNSOLICITED_ABORT:
1029 printk(KERN_WARNING "cciss: cp %p aborted " 1009 dev_warn(&h->pdev->dev,
1030 "do to an unsolicited abort\n", cp); 1010 "%p aborted do to an unsolicited abort\n", c);
1031 break; 1011 break;
1032 case CMD_TIMEOUT: 1012 case CMD_TIMEOUT:
1033 printk(KERN_WARNING "cciss: cp %p timedout\n", 1013 dev_warn(&h->pdev->dev, "%p timedout\n", c);
1034 cp);
1035 break; 1014 break;
1036 default: 1015 default:
1037 printk(KERN_WARNING "cciss: cp %p returned " 1016 dev_warn(&h->pdev->dev,
1038 "unknown status %x\n", cp, 1017 "%p returned unknown status %x\n",
1039 ei->CommandStatus); 1018 c, ei->CommandStatus);
1040 } 1019 }
1041} 1020}
1042 1021
1043static int 1022static int
1044cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr, 1023cciss_scsi_do_inquiry(ctlr_info_t *h, unsigned char *scsi3addr,
1045 unsigned char page, unsigned char *buf, 1024 unsigned char page, unsigned char *buf,
1046 unsigned char bufsize) 1025 unsigned char bufsize)
1047{ 1026{
1048 int rc; 1027 int rc;
1049 CommandList_struct *cp; 1028 CommandList_struct *c;
1050 char cdb[6]; 1029 char cdb[6];
1051 ErrorInfo_struct *ei; 1030 ErrorInfo_struct *ei;
1052 unsigned long flags; 1031 unsigned long flags;
1053 1032
1054 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags); 1033 spin_lock_irqsave(&h->lock, flags);
1055 cp = scsi_cmd_alloc(c); 1034 c = scsi_cmd_alloc(h);
1056 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags); 1035 spin_unlock_irqrestore(&h->lock, flags);
1057 1036
1058 if (cp == NULL) { /* trouble... */ 1037 if (c == NULL) { /* trouble... */
1059 printk("cmd_alloc returned NULL!\n"); 1038 printk("cmd_alloc returned NULL!\n");
1060 return -1; 1039 return -1;
1061 } 1040 }
1062 1041
1063 ei = cp->err_info; 1042 ei = c->err_info;
1064 1043
1065 cdb[0] = CISS_INQUIRY; 1044 cdb[0] = CISS_INQUIRY;
1066 cdb[1] = (page != 0); 1045 cdb[1] = (page != 0);
@@ -1068,24 +1047,24 @@ cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
1068 cdb[3] = 0; 1047 cdb[3] = 0;
1069 cdb[4] = bufsize; 1048 cdb[4] = bufsize;
1070 cdb[5] = 0; 1049 cdb[5] = 0;
1071 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb, 1050 rc = cciss_scsi_do_simple_cmd(h, c, scsi3addr, cdb,
1072 6, buf, bufsize, XFER_READ); 1051 6, buf, bufsize, XFER_READ);
1073 1052
1074 if (rc != 0) return rc; /* something went wrong */ 1053 if (rc != 0) return rc; /* something went wrong */
1075 1054
1076 if (ei->CommandStatus != 0 && 1055 if (ei->CommandStatus != 0 &&
1077 ei->CommandStatus != CMD_DATA_UNDERRUN) { 1056 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1078 cciss_scsi_interpret_error(cp); 1057 cciss_scsi_interpret_error(h, c);
1079 rc = -1; 1058 rc = -1;
1080 } 1059 }
1081 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags); 1060 spin_lock_irqsave(&h->lock, flags);
1082 scsi_cmd_free(c, cp); 1061 scsi_cmd_free(h, c);
1083 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags); 1062 spin_unlock_irqrestore(&h->lock, flags);
1084 return rc; 1063 return rc;
1085} 1064}
1086 1065
1087/* Get the device id from inquiry page 0x83 */ 1066/* Get the device id from inquiry page 0x83 */
1088static int cciss_scsi_get_device_id(ctlr_info_t *c, unsigned char *scsi3addr, 1067static int cciss_scsi_get_device_id(ctlr_info_t *h, unsigned char *scsi3addr,
1089 unsigned char *device_id, int buflen) 1068 unsigned char *device_id, int buflen)
1090{ 1069{
1091 int rc; 1070 int rc;
@@ -1096,7 +1075,7 @@ static int cciss_scsi_get_device_id(ctlr_info_t *c, unsigned char *scsi3addr,
1096 buf = kzalloc(64, GFP_KERNEL); 1075 buf = kzalloc(64, GFP_KERNEL);
1097 if (!buf) 1076 if (!buf)
1098 return -1; 1077 return -1;
1099 rc = cciss_scsi_do_inquiry(c, scsi3addr, 0x83, buf, 64); 1078 rc = cciss_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1100 if (rc == 0) 1079 if (rc == 0)
1101 memcpy(device_id, &buf[8], buflen); 1080 memcpy(device_id, &buf[8], buflen);
1102 kfree(buf); 1081 kfree(buf);
@@ -1104,20 +1083,20 @@ static int cciss_scsi_get_device_id(ctlr_info_t *c, unsigned char *scsi3addr,
1104} 1083}
1105 1084
1106static int 1085static int
1107cciss_scsi_do_report_phys_luns(ctlr_info_t *c, 1086cciss_scsi_do_report_phys_luns(ctlr_info_t *h,
1108 ReportLunData_struct *buf, int bufsize) 1087 ReportLunData_struct *buf, int bufsize)
1109{ 1088{
1110 int rc; 1089 int rc;
1111 CommandList_struct *cp; 1090 CommandList_struct *c;
1112 unsigned char cdb[12]; 1091 unsigned char cdb[12];
1113 unsigned char scsi3addr[8]; 1092 unsigned char scsi3addr[8];
1114 ErrorInfo_struct *ei; 1093 ErrorInfo_struct *ei;
1115 unsigned long flags; 1094 unsigned long flags;
1116 1095
1117 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags); 1096 spin_lock_irqsave(&h->lock, flags);
1118 cp = scsi_cmd_alloc(c); 1097 c = scsi_cmd_alloc(h);
1119 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags); 1098 spin_unlock_irqrestore(&h->lock, flags);
1120 if (cp == NULL) { /* trouble... */ 1099 if (c == NULL) { /* trouble... */
1121 printk("cmd_alloc returned NULL!\n"); 1100 printk("cmd_alloc returned NULL!\n");
1122 return -1; 1101 return -1;
1123 } 1102 }
@@ -1136,27 +1115,27 @@ cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
1136 cdb[10] = 0; 1115 cdb[10] = 0;
1137 cdb[11] = 0; 1116 cdb[11] = 0;
1138 1117
1139 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, 1118 rc = cciss_scsi_do_simple_cmd(h, c, scsi3addr,
1140 cdb, 12, 1119 cdb, 12,
1141 (unsigned char *) buf, 1120 (unsigned char *) buf,
1142 bufsize, XFER_READ); 1121 bufsize, XFER_READ);
1143 1122
1144 if (rc != 0) return rc; /* something went wrong */ 1123 if (rc != 0) return rc; /* something went wrong */
1145 1124
1146 ei = cp->err_info; 1125 ei = c->err_info;
1147 if (ei->CommandStatus != 0 && 1126 if (ei->CommandStatus != 0 &&
1148 ei->CommandStatus != CMD_DATA_UNDERRUN) { 1127 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1149 cciss_scsi_interpret_error(cp); 1128 cciss_scsi_interpret_error(h, c);
1150 rc = -1; 1129 rc = -1;
1151 } 1130 }
1152 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags); 1131 spin_lock_irqsave(&h->lock, flags);
1153 scsi_cmd_free(c, cp); 1132 scsi_cmd_free(h, c);
1154 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags); 1133 spin_unlock_irqrestore(&h->lock, flags);
1155 return rc; 1134 return rc;
1156} 1135}
1157 1136
1158static void 1137static void
1159cciss_update_non_disk_devices(int cntl_num, int hostno) 1138cciss_update_non_disk_devices(ctlr_info_t *h, int hostno)
1160{ 1139{
1161 /* the idea here is we could get notified from /proc 1140 /* the idea here is we could get notified from /proc
1162 that some devices have changed, so we do a report 1141 that some devices have changed, so we do a report
@@ -1189,7 +1168,6 @@ cciss_update_non_disk_devices(int cntl_num, int hostno)
1189 ReportLunData_struct *ld_buff; 1168 ReportLunData_struct *ld_buff;
1190 unsigned char *inq_buff; 1169 unsigned char *inq_buff;
1191 unsigned char scsi3addr[8]; 1170 unsigned char scsi3addr[8];
1192 ctlr_info_t *c;
1193 __u32 num_luns=0; 1171 __u32 num_luns=0;
1194 unsigned char *ch; 1172 unsigned char *ch;
1195 struct cciss_scsi_dev_t *currentsd, *this_device; 1173 struct cciss_scsi_dev_t *currentsd, *this_device;
@@ -1197,7 +1175,6 @@ cciss_update_non_disk_devices(int cntl_num, int hostno)
1197 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8; 1175 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1198 int i; 1176 int i;
1199 1177
1200 c = (ctlr_info_t *) hba[cntl_num];
1201 ld_buff = kzalloc(reportlunsize, GFP_KERNEL); 1178 ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
1202 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL); 1179 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1203 currentsd = kzalloc(sizeof(*currentsd) * 1180 currentsd = kzalloc(sizeof(*currentsd) *
@@ -1207,7 +1184,7 @@ cciss_update_non_disk_devices(int cntl_num, int hostno)
1207 goto out; 1184 goto out;
1208 } 1185 }
1209 this_device = &currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA]; 1186 this_device = &currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1210 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) { 1187 if (cciss_scsi_do_report_phys_luns(h, ld_buff, reportlunsize) == 0) {
1211 ch = &ld_buff->LUNListLength[0]; 1188 ch = &ld_buff->LUNListLength[0];
1212 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8; 1189 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1213 if (num_luns > CISS_MAX_PHYS_LUN) { 1190 if (num_luns > CISS_MAX_PHYS_LUN) {
@@ -1231,7 +1208,7 @@ cciss_update_non_disk_devices(int cntl_num, int hostno)
1231 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE); 1208 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1232 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8); 1209 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1233 1210
1234 if (cciss_scsi_do_inquiry(hba[cntl_num], scsi3addr, 0, inq_buff, 1211 if (cciss_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1235 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) 1212 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0)
1236 /* Inquiry failed (msg printed already) */ 1213 /* Inquiry failed (msg printed already) */
1237 continue; /* so we will skip this device. */ 1214 continue; /* so we will skip this device. */
@@ -1249,7 +1226,7 @@ cciss_update_non_disk_devices(int cntl_num, int hostno)
1249 sizeof(this_device->revision)); 1226 sizeof(this_device->revision));
1250 memset(this_device->device_id, 0, 1227 memset(this_device->device_id, 0,
1251 sizeof(this_device->device_id)); 1228 sizeof(this_device->device_id));
1252 cciss_scsi_get_device_id(hba[cntl_num], scsi3addr, 1229 cciss_scsi_get_device_id(h, scsi3addr,
1253 this_device->device_id, sizeof(this_device->device_id)); 1230 this_device->device_id, sizeof(this_device->device_id));
1254 1231
1255 switch (this_device->devtype) 1232 switch (this_device->devtype)
@@ -1276,7 +1253,7 @@ cciss_update_non_disk_devices(int cntl_num, int hostno)
1276 case 0x08: /* medium changer */ 1253 case 0x08: /* medium changer */
1277 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) { 1254 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1278 printk(KERN_INFO "cciss%d: %s ignored, " 1255 printk(KERN_INFO "cciss%d: %s ignored, "
1279 "too many devices.\n", cntl_num, 1256 "too many devices.\n", h->ctlr,
1280 scsi_device_type(this_device->devtype)); 1257 scsi_device_type(this_device->devtype));
1281 break; 1258 break;
1282 } 1259 }
@@ -1288,7 +1265,7 @@ cciss_update_non_disk_devices(int cntl_num, int hostno)
1288 } 1265 }
1289 } 1266 }
1290 1267
1291 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent); 1268 adjust_cciss_scsi_table(h, hostno, currentsd, ncurrent);
1292out: 1269out:
1293 kfree(inq_buff); 1270 kfree(inq_buff);
1294 kfree(ld_buff); 1271 kfree(ld_buff);
@@ -1307,12 +1284,12 @@ is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1307} 1284}
1308 1285
1309static int 1286static int
1310cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length) 1287cciss_scsi_user_command(ctlr_info_t *h, int hostno, char *buffer, int length)
1311{ 1288{
1312 int arg_len; 1289 int arg_len;
1313 1290
1314 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0) 1291 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1315 cciss_update_non_disk_devices(ctlr, hostno); 1292 cciss_update_non_disk_devices(h, hostno);
1316 else 1293 else
1317 return -EINVAL; 1294 return -EINVAL;
1318 return length; 1295 return length;
@@ -1329,20 +1306,16 @@ cciss_scsi_proc_info(struct Scsi_Host *sh,
1329{ 1306{
1330 1307
1331 int buflen, datalen; 1308 int buflen, datalen;
1332 ctlr_info_t *ci; 1309 ctlr_info_t *h;
1333 int i; 1310 int i;
1334 int cntl_num;
1335
1336 1311
1337 ci = (ctlr_info_t *) sh->hostdata[0]; 1312 h = (ctlr_info_t *) sh->hostdata[0];
1338 if (ci == NULL) /* This really shouldn't ever happen. */ 1313 if (h == NULL) /* This really shouldn't ever happen. */
1339 return -EINVAL; 1314 return -EINVAL;
1340 1315
1341 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1342
1343 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */ 1316 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
1344 buflen = sprintf(buffer, "cciss%d: SCSI host: %d\n", 1317 buflen = sprintf(buffer, "cciss%d: SCSI host: %d\n",
1345 cntl_num, sh->host_no); 1318 h->ctlr, sh->host_no);
1346 1319
1347 /* this information is needed by apps to know which cciss 1320 /* this information is needed by apps to know which cciss
1348 device corresponds to which scsi host number without 1321 device corresponds to which scsi host number without
@@ -1352,8 +1325,9 @@ cciss_scsi_proc_info(struct Scsi_Host *sh,
1352 this info is for an app to be able to use to know how to 1325 this info is for an app to be able to use to know how to
1353 get them back in sync. */ 1326 get them back in sync. */
1354 1327
1355 for (i=0;i<ccissscsi[cntl_num].ndevices;i++) { 1328 for (i = 0; i < ccissscsi[h->ctlr].ndevices; i++) {
1356 struct cciss_scsi_dev_t *sd = &ccissscsi[cntl_num].dev[i]; 1329 struct cciss_scsi_dev_t *sd =
1330 &ccissscsi[h->ctlr].dev[i];
1357 buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d " 1331 buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d "
1358 "0x%02x%02x%02x%02x%02x%02x%02x%02x\n", 1332 "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1359 sh->host_no, sd->bus, sd->target, sd->lun, 1333 sh->host_no, sd->bus, sd->target, sd->lun,
@@ -1371,15 +1345,15 @@ cciss_scsi_proc_info(struct Scsi_Host *sh,
1371 *start = buffer + offset; 1345 *start = buffer + offset;
1372 return(datalen); 1346 return(datalen);
1373 } else /* User is writing to /proc/scsi/cciss*?/?* ... */ 1347 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1374 return cciss_scsi_user_command(cntl_num, sh->host_no, 1348 return cciss_scsi_user_command(h, sh->host_no,
1375 buffer, length); 1349 buffer, length);
1376} 1350}
1377 1351
1378/* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci 1352/* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1379 dma mapping and fills in the scatter gather entries of the 1353 dma mapping and fills in the scatter gather entries of the
1380 cciss command, cp. */ 1354 cciss command, c. */
1381 1355
1382static void cciss_scatter_gather(ctlr_info_t *h, CommandList_struct *cp, 1356static void cciss_scatter_gather(ctlr_info_t *h, CommandList_struct *c,
1383 struct scsi_cmnd *cmd) 1357 struct scsi_cmnd *cmd)
1384{ 1358{
1385 unsigned int len; 1359 unsigned int len;
@@ -1393,7 +1367,7 @@ static void cciss_scatter_gather(ctlr_info_t *h, CommandList_struct *cp,
1393 1367
1394 chained = 0; 1368 chained = 0;
1395 sg_index = 0; 1369 sg_index = 0;
1396 curr_sg = cp->SG; 1370 curr_sg = c->SG;
1397 request_nsgs = scsi_dma_map(cmd); 1371 request_nsgs = scsi_dma_map(cmd);
1398 if (request_nsgs) { 1372 if (request_nsgs) {
1399 scsi_for_each_sg(cmd, sg, request_nsgs, i) { 1373 scsi_for_each_sg(cmd, sg, request_nsgs, i) {
@@ -1401,7 +1375,7 @@ static void cciss_scatter_gather(ctlr_info_t *h, CommandList_struct *cp,
1401 !chained && request_nsgs - i > 1) { 1375 !chained && request_nsgs - i > 1) {
1402 chained = 1; 1376 chained = 1;
1403 sg_index = 0; 1377 sg_index = 0;
1404 curr_sg = sa->cmd_sg_list[cp->cmdindex]; 1378 curr_sg = sa->cmd_sg_list[c->cmdindex];
1405 } 1379 }
1406 addr64 = (__u64) sg_dma_address(sg); 1380 addr64 = (__u64) sg_dma_address(sg);
1407 len = sg_dma_len(sg); 1381 len = sg_dma_len(sg);
@@ -1414,19 +1388,19 @@ static void cciss_scatter_gather(ctlr_info_t *h, CommandList_struct *cp,
1414 ++sg_index; 1388 ++sg_index;
1415 } 1389 }
1416 if (chained) 1390 if (chained)
1417 cciss_map_sg_chain_block(h, cp, 1391 cciss_map_sg_chain_block(h, c,
1418 sa->cmd_sg_list[cp->cmdindex], 1392 sa->cmd_sg_list[c->cmdindex],
1419 (request_nsgs - (h->max_cmd_sgentries - 1)) * 1393 (request_nsgs - (h->max_cmd_sgentries - 1)) *
1420 sizeof(SGDescriptor_struct)); 1394 sizeof(SGDescriptor_struct));
1421 } 1395 }
1422 /* track how many SG entries we are using */ 1396 /* track how many SG entries we are using */
1423 if (request_nsgs > h->maxSG) 1397 if (request_nsgs > h->maxSG)
1424 h->maxSG = request_nsgs; 1398 h->maxSG = request_nsgs;
1425 cp->Header.SGTotal = (__u8) request_nsgs + chained; 1399 c->Header.SGTotal = (__u8) request_nsgs + chained;
1426 if (request_nsgs > h->max_cmd_sgentries) 1400 if (request_nsgs > h->max_cmd_sgentries)
1427 cp->Header.SGList = h->max_cmd_sgentries; 1401 c->Header.SGList = h->max_cmd_sgentries;
1428 else 1402 else
1429 cp->Header.SGList = cp->Header.SGTotal; 1403 c->Header.SGList = c->Header.SGTotal;
1430 return; 1404 return;
1431} 1405}
1432 1406
@@ -1434,18 +1408,17 @@ static void cciss_scatter_gather(ctlr_info_t *h, CommandList_struct *cp,
1434static int 1408static int
1435cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *)) 1409cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1436{ 1410{
1437 ctlr_info_t *c; 1411 ctlr_info_t *h;
1438 int ctlr, rc; 1412 int rc;
1439 unsigned char scsi3addr[8]; 1413 unsigned char scsi3addr[8];
1440 CommandList_struct *cp; 1414 CommandList_struct *c;
1441 unsigned long flags; 1415 unsigned long flags;
1442 1416
1443 // Get the ptr to our adapter structure (hba[i]) out of cmd->host. 1417 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1444 // We violate cmd->host privacy here. (Is there another way?) 1418 // We violate cmd->host privacy here. (Is there another way?)
1445 c = (ctlr_info_t *) cmd->device->host->hostdata[0]; 1419 h = (ctlr_info_t *) cmd->device->host->hostdata[0];
1446 ctlr = c->ctlr;
1447 1420
1448 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id, 1421 rc = lookup_scsi3addr(h, cmd->device->channel, cmd->device->id,
1449 cmd->device->lun, scsi3addr); 1422 cmd->device->lun, scsi3addr);
1450 if (rc != 0) { 1423 if (rc != 0) {
1451 /* the scsi nexus does not match any that we presented... */ 1424 /* the scsi nexus does not match any that we presented... */
@@ -1457,19 +1430,14 @@ cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd
1457 return 0; 1430 return 0;
1458 } 1431 }
1459 1432
1460 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1461 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1462 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1463 // cmd->target, cmd->lun);
1464
1465 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and 1433 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1466 see what the device thinks of it. */ 1434 see what the device thinks of it. */
1467 1435
1468 spin_lock_irqsave(CCISS_LOCK(ctlr), flags); 1436 spin_lock_irqsave(&h->lock, flags);
1469 cp = scsi_cmd_alloc(c); 1437 c = scsi_cmd_alloc(h);
1470 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); 1438 spin_unlock_irqrestore(&h->lock, flags);
1471 if (cp == NULL) { /* trouble... */ 1439 if (c == NULL) { /* trouble... */
1472 printk("scsi_cmd_alloc returned NULL!\n"); 1440 dev_warn(&h->pdev->dev, "scsi_cmd_alloc returned NULL!\n");
1473 /* FIXME: next 3 lines are -> BAD! <- */ 1441 /* FIXME: next 3 lines are -> BAD! <- */
1474 cmd->result = DID_NO_CONNECT << 16; 1442 cmd->result = DID_NO_CONNECT << 16;
1475 done(cmd); 1443 done(cmd);
@@ -1480,35 +1448,41 @@ cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd
1480 1448
1481 cmd->scsi_done = done; // save this for use by completion code 1449 cmd->scsi_done = done; // save this for use by completion code
1482 1450
1483 // save cp in case we have to abort it 1451 /* save c in case we have to abort it */
1484 cmd->host_scribble = (unsigned char *) cp; 1452 cmd->host_scribble = (unsigned char *) c;
1485 1453
1486 cp->cmd_type = CMD_SCSI; 1454 c->cmd_type = CMD_SCSI;
1487 cp->scsi_cmd = cmd; 1455 c->scsi_cmd = cmd;
1488 cp->Header.ReplyQueue = 0; // unused in simple mode 1456 c->Header.ReplyQueue = 0; /* unused in simple mode */
1489 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8); 1457 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1490 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag 1458 c->Header.Tag.lower = c->busaddr; /* Use k. address of cmd as tag */
1491 1459
1492 // Fill in the request block... 1460 // Fill in the request block...
1493 1461
1494 cp->Request.Timeout = 0; 1462 c->Request.Timeout = 0;
1495 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB)); 1463 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
1496 BUG_ON(cmd->cmd_len > sizeof(cp->Request.CDB)); 1464 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
1497 cp->Request.CDBLen = cmd->cmd_len; 1465 c->Request.CDBLen = cmd->cmd_len;
1498 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len); 1466 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
1499 cp->Request.Type.Type = TYPE_CMD; 1467 c->Request.Type.Type = TYPE_CMD;
1500 cp->Request.Type.Attribute = ATTR_SIMPLE; 1468 c->Request.Type.Attribute = ATTR_SIMPLE;
1501 switch(cmd->sc_data_direction) 1469 switch(cmd->sc_data_direction)
1502 { 1470 {
1503 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break; 1471 case DMA_TO_DEVICE:
1504 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break; 1472 c->Request.Type.Direction = XFER_WRITE;
1505 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break; 1473 break;
1474 case DMA_FROM_DEVICE:
1475 c->Request.Type.Direction = XFER_READ;
1476 break;
1477 case DMA_NONE:
1478 c->Request.Type.Direction = XFER_NONE;
1479 break;
1506 case DMA_BIDIRECTIONAL: 1480 case DMA_BIDIRECTIONAL:
1507 // This can happen if a buggy application does a scsi passthru 1481 // This can happen if a buggy application does a scsi passthru
1508 // and sets both inlen and outlen to non-zero. ( see 1482 // and sets both inlen and outlen to non-zero. ( see
1509 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() ) 1483 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1510 1484
1511 cp->Request.Type.Direction = XFER_RSVD; 1485 c->Request.Type.Direction = XFER_RSVD;
1512 // This is technically wrong, and cciss controllers should 1486 // This is technically wrong, and cciss controllers should
1513 // reject it with CMD_INVALID, which is the most correct 1487 // reject it with CMD_INVALID, which is the most correct
1514 // response, but non-fibre backends appear to let it 1488 // response, but non-fibre backends appear to let it
@@ -1519,27 +1493,18 @@ cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd
1519 break; 1493 break;
1520 1494
1521 default: 1495 default:
1522 printk("cciss: unknown data direction: %d\n", 1496 dev_warn(&h->pdev->dev, "unknown data direction: %d\n",
1523 cmd->sc_data_direction); 1497 cmd->sc_data_direction);
1524 BUG(); 1498 BUG();
1525 break; 1499 break;
1526 } 1500 }
1527 cciss_scatter_gather(c, cp, cmd); 1501 cciss_scatter_gather(h, c, cmd);
1528 1502 enqueue_cmd_and_start_io(h, c);
1529 /* Put the request on the tail of the request queue */
1530
1531 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1532 addQ(&c->reqQ, cp);
1533 c->Qdepth++;
1534 start_io(c);
1535 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1536
1537 /* the cmd'll come back via intr handler in complete_scsi_command() */ 1503 /* the cmd'll come back via intr handler in complete_scsi_command() */
1538 return 0; 1504 return 0;
1539} 1505}
1540 1506
1541static void 1507static void cciss_unregister_scsi(ctlr_info_t *h)
1542cciss_unregister_scsi(int ctlr)
1543{ 1508{
1544 struct cciss_scsi_adapter_data_t *sa; 1509 struct cciss_scsi_adapter_data_t *sa;
1545 struct cciss_scsi_cmd_stack_t *stk; 1510 struct cciss_scsi_cmd_stack_t *stk;
@@ -1547,59 +1512,58 @@ cciss_unregister_scsi(int ctlr)
1547 1512
1548 /* we are being forcibly unloaded, and may not refuse. */ 1513 /* we are being forcibly unloaded, and may not refuse. */
1549 1514
1550 spin_lock_irqsave(CCISS_LOCK(ctlr), flags); 1515 spin_lock_irqsave(&h->lock, flags);
1551 sa = hba[ctlr]->scsi_ctlr; 1516 sa = h->scsi_ctlr;
1552 stk = &sa->cmd_stack; 1517 stk = &sa->cmd_stack;
1553 1518
1554 /* if we weren't ever actually registered, don't unregister */ 1519 /* if we weren't ever actually registered, don't unregister */
1555 if (sa->registered) { 1520 if (sa->registered) {
1556 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); 1521 spin_unlock_irqrestore(&h->lock, flags);
1557 scsi_remove_host(sa->scsi_host); 1522 scsi_remove_host(sa->scsi_host);
1558 scsi_host_put(sa->scsi_host); 1523 scsi_host_put(sa->scsi_host);
1559 spin_lock_irqsave(CCISS_LOCK(ctlr), flags); 1524 spin_lock_irqsave(&h->lock, flags);
1560 } 1525 }
1561 1526
1562 /* set scsi_host to NULL so our detect routine will 1527 /* set scsi_host to NULL so our detect routine will
1563 find us on register */ 1528 find us on register */
1564 sa->scsi_host = NULL; 1529 sa->scsi_host = NULL;
1565 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); 1530 spin_unlock_irqrestore(&h->lock, flags);
1566 scsi_cmd_stack_free(ctlr); 1531 scsi_cmd_stack_free(h);
1567 kfree(sa); 1532 kfree(sa);
1568} 1533}
1569 1534
1570static int 1535static int cciss_engage_scsi(ctlr_info_t *h)
1571cciss_engage_scsi(int ctlr)
1572{ 1536{
1573 struct cciss_scsi_adapter_data_t *sa; 1537 struct cciss_scsi_adapter_data_t *sa;
1574 struct cciss_scsi_cmd_stack_t *stk; 1538 struct cciss_scsi_cmd_stack_t *stk;
1575 unsigned long flags; 1539 unsigned long flags;
1576 1540
1577 spin_lock_irqsave(CCISS_LOCK(ctlr), flags); 1541 spin_lock_irqsave(&h->lock, flags);
1578 sa = hba[ctlr]->scsi_ctlr; 1542 sa = h->scsi_ctlr;
1579 stk = &sa->cmd_stack; 1543 stk = &sa->cmd_stack;
1580 1544
1581 if (sa->registered) { 1545 if (sa->registered) {
1582 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr); 1546 dev_info(&h->pdev->dev, "SCSI subsystem already engaged.\n");
1583 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); 1547 spin_unlock_irqrestore(&h->lock, flags);
1584 return -ENXIO; 1548 return -ENXIO;
1585 } 1549 }
1586 sa->registered = 1; 1550 sa->registered = 1;
1587 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); 1551 spin_unlock_irqrestore(&h->lock, flags);
1588 cciss_update_non_disk_devices(ctlr, -1); 1552 cciss_update_non_disk_devices(h, -1);
1589 cciss_scsi_detect(ctlr); 1553 cciss_scsi_detect(h);
1590 return 0; 1554 return 0;
1591} 1555}
1592 1556
1593static void 1557static void
1594cciss_seq_tape_report(struct seq_file *seq, int ctlr) 1558cciss_seq_tape_report(struct seq_file *seq, ctlr_info_t *h)
1595{ 1559{
1596 unsigned long flags; 1560 unsigned long flags;
1597 1561
1598 CPQ_TAPE_LOCK(ctlr, flags); 1562 CPQ_TAPE_LOCK(h, flags);
1599 seq_printf(seq, 1563 seq_printf(seq,
1600 "Sequential access devices: %d\n\n", 1564 "Sequential access devices: %d\n\n",
1601 ccissscsi[ctlr].ndevices); 1565 ccissscsi[h->ctlr].ndevices);
1602 CPQ_TAPE_UNLOCK(ctlr, flags); 1566 CPQ_TAPE_UNLOCK(h, flags);
1603} 1567}
1604 1568
1605static int wait_for_device_to_become_ready(ctlr_info_t *h, 1569static int wait_for_device_to_become_ready(ctlr_info_t *h,
@@ -1610,10 +1574,10 @@ static int wait_for_device_to_become_ready(ctlr_info_t *h,
1610 int waittime = HZ; 1574 int waittime = HZ;
1611 CommandList_struct *c; 1575 CommandList_struct *c;
1612 1576
1613 c = cmd_alloc(h, 1); 1577 c = cmd_alloc(h);
1614 if (!c) { 1578 if (!c) {
1615 printk(KERN_WARNING "cciss%d: out of memory in " 1579 dev_warn(&h->pdev->dev, "out of memory in "
1616 "wait_for_device_to_become_ready.\n", h->ctlr); 1580 "wait_for_device_to_become_ready.\n");
1617 return IO_ERROR; 1581 return IO_ERROR;
1618 } 1582 }
1619 1583
@@ -1631,7 +1595,7 @@ static int wait_for_device_to_become_ready(ctlr_info_t *h,
1631 waittime = waittime * 2; 1595 waittime = waittime * 2;
1632 1596
1633 /* Send the Test Unit Ready */ 1597 /* Send the Test Unit Ready */
1634 rc = fill_cmd(c, TEST_UNIT_READY, h->ctlr, NULL, 0, 0, 1598 rc = fill_cmd(h, c, TEST_UNIT_READY, NULL, 0, 0,
1635 lunaddr, TYPE_CMD); 1599 lunaddr, TYPE_CMD);
1636 if (rc == 0) 1600 if (rc == 0)
1637 rc = sendcmd_withirq_core(h, c, 0); 1601 rc = sendcmd_withirq_core(h, c, 0);
@@ -1657,18 +1621,18 @@ static int wait_for_device_to_become_ready(ctlr_info_t *h,
1657 } 1621 }
1658 } 1622 }
1659retry_tur: 1623retry_tur:
1660 printk(KERN_WARNING "cciss%d: Waiting %d secs " 1624 dev_warn(&h->pdev->dev, "Waiting %d secs "
1661 "for device to become ready.\n", 1625 "for device to become ready.\n",
1662 h->ctlr, waittime / HZ); 1626 waittime / HZ);
1663 rc = 1; /* device not ready. */ 1627 rc = 1; /* device not ready. */
1664 } 1628 }
1665 1629
1666 if (rc) 1630 if (rc)
1667 printk("cciss%d: giving up on device.\n", h->ctlr); 1631 dev_warn(&h->pdev->dev, "giving up on device.\n");
1668 else 1632 else
1669 printk(KERN_WARNING "cciss%d: device is ready.\n", h->ctlr); 1633 dev_warn(&h->pdev->dev, "device is ready.\n");
1670 1634
1671 cmd_free(h, c, 1); 1635 cmd_free(h, c);
1672 return rc; 1636 return rc;
1673} 1637}
1674 1638
@@ -1688,26 +1652,24 @@ static int cciss_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
1688 int rc; 1652 int rc;
1689 CommandList_struct *cmd_in_trouble; 1653 CommandList_struct *cmd_in_trouble;
1690 unsigned char lunaddr[8]; 1654 unsigned char lunaddr[8];
1691 ctlr_info_t *c; 1655 ctlr_info_t *h;
1692 int ctlr;
1693 1656
1694 /* find the controller to which the command to be aborted was sent */ 1657 /* find the controller to which the command to be aborted was sent */
1695 c = (ctlr_info_t *) scsicmd->device->host->hostdata[0]; 1658 h = (ctlr_info_t *) scsicmd->device->host->hostdata[0];
1696 if (c == NULL) /* paranoia */ 1659 if (h == NULL) /* paranoia */
1697 return FAILED; 1660 return FAILED;
1698 ctlr = c->ctlr; 1661 dev_warn(&h->pdev->dev, "resetting tape drive or medium changer.\n");
1699 printk(KERN_WARNING "cciss%d: resetting tape drive or medium changer.\n", ctlr);
1700 /* find the command that's giving us trouble */ 1662 /* find the command that's giving us trouble */
1701 cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble; 1663 cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
1702 if (cmd_in_trouble == NULL) /* paranoia */ 1664 if (cmd_in_trouble == NULL) /* paranoia */
1703 return FAILED; 1665 return FAILED;
1704 memcpy(lunaddr, &cmd_in_trouble->Header.LUN.LunAddrBytes[0], 8); 1666 memcpy(lunaddr, &cmd_in_trouble->Header.LUN.LunAddrBytes[0], 8);
1705 /* send a reset to the SCSI LUN which the command was sent to */ 1667 /* send a reset to the SCSI LUN which the command was sent to */
1706 rc = sendcmd_withirq(CCISS_RESET_MSG, ctlr, NULL, 0, 0, lunaddr, 1668 rc = sendcmd_withirq(h, CCISS_RESET_MSG, NULL, 0, 0, lunaddr,
1707 TYPE_MSG); 1669 TYPE_MSG);
1708 if (rc == 0 && wait_for_device_to_become_ready(c, lunaddr) == 0) 1670 if (rc == 0 && wait_for_device_to_become_ready(h, lunaddr) == 0)
1709 return SUCCESS; 1671 return SUCCESS;
1710 printk(KERN_WARNING "cciss%d: resetting device failed.\n", ctlr); 1672 dev_warn(&h->pdev->dev, "resetting device failed.\n");
1711 return FAILED; 1673 return FAILED;
1712} 1674}
1713 1675
@@ -1716,22 +1678,20 @@ static int cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
1716 int rc; 1678 int rc;
1717 CommandList_struct *cmd_to_abort; 1679 CommandList_struct *cmd_to_abort;
1718 unsigned char lunaddr[8]; 1680 unsigned char lunaddr[8];
1719 ctlr_info_t *c; 1681 ctlr_info_t *h;
1720 int ctlr;
1721 1682
1722 /* find the controller to which the command to be aborted was sent */ 1683 /* find the controller to which the command to be aborted was sent */
1723 c = (ctlr_info_t *) scsicmd->device->host->hostdata[0]; 1684 h = (ctlr_info_t *) scsicmd->device->host->hostdata[0];
1724 if (c == NULL) /* paranoia */ 1685 if (h == NULL) /* paranoia */
1725 return FAILED; 1686 return FAILED;
1726 ctlr = c->ctlr; 1687 dev_warn(&h->pdev->dev, "aborting tardy SCSI cmd\n");
1727 printk(KERN_WARNING "cciss%d: aborting tardy SCSI cmd\n", ctlr);
1728 1688
1729 /* find the command to be aborted */ 1689 /* find the command to be aborted */
1730 cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble; 1690 cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
1731 if (cmd_to_abort == NULL) /* paranoia */ 1691 if (cmd_to_abort == NULL) /* paranoia */
1732 return FAILED; 1692 return FAILED;
1733 memcpy(lunaddr, &cmd_to_abort->Header.LUN.LunAddrBytes[0], 8); 1693 memcpy(lunaddr, &cmd_to_abort->Header.LUN.LunAddrBytes[0], 8);
1734 rc = sendcmd_withirq(CCISS_ABORT_MSG, ctlr, &cmd_to_abort->Header.Tag, 1694 rc = sendcmd_withirq(h, CCISS_ABORT_MSG, &cmd_to_abort->Header.Tag,
1735 0, 0, lunaddr, TYPE_MSG); 1695 0, 0, lunaddr, TYPE_MSG);
1736 if (rc == 0) 1696 if (rc == 0)
1737 return SUCCESS; 1697 return SUCCESS;
diff --git a/drivers/block/cpqarray.c b/drivers/block/cpqarray.c
index abb4ec6690f..d53b0291c44 100644
--- a/drivers/block/cpqarray.c
+++ b/drivers/block/cpqarray.c
@@ -35,6 +35,7 @@
35#include <linux/seq_file.h> 35#include <linux/seq_file.h>
36#include <linux/init.h> 36#include <linux/init.h>
37#include <linux/hdreg.h> 37#include <linux/hdreg.h>
38#include <linux/smp_lock.h>
38#include <linux/spinlock.h> 39#include <linux/spinlock.h>
39#include <linux/blkdev.h> 40#include <linux/blkdev.h>
40#include <linux/genhd.h> 41#include <linux/genhd.h>
@@ -157,7 +158,7 @@ static int sendcmd(
157 unsigned int blkcnt, 158 unsigned int blkcnt,
158 unsigned int log_unit ); 159 unsigned int log_unit );
159 160
160static int ida_open(struct block_device *bdev, fmode_t mode); 161static int ida_unlocked_open(struct block_device *bdev, fmode_t mode);
161static int ida_release(struct gendisk *disk, fmode_t mode); 162static int ida_release(struct gendisk *disk, fmode_t mode);
162static int ida_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg); 163static int ida_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg);
163static int ida_getgeo(struct block_device *bdev, struct hd_geometry *geo); 164static int ida_getgeo(struct block_device *bdev, struct hd_geometry *geo);
@@ -195,9 +196,9 @@ static inline ctlr_info_t *get_host(struct gendisk *disk)
195 196
196static const struct block_device_operations ida_fops = { 197static const struct block_device_operations ida_fops = {
197 .owner = THIS_MODULE, 198 .owner = THIS_MODULE,
198 .open = ida_open, 199 .open = ida_unlocked_open,
199 .release = ida_release, 200 .release = ida_release,
200 .locked_ioctl = ida_ioctl, 201 .ioctl = ida_ioctl,
201 .getgeo = ida_getgeo, 202 .getgeo = ida_getgeo,
202 .revalidate_disk= ida_revalidate, 203 .revalidate_disk= ida_revalidate,
203}; 204};
@@ -840,13 +841,29 @@ static int ida_open(struct block_device *bdev, fmode_t mode)
840 return 0; 841 return 0;
841} 842}
842 843
844static int ida_unlocked_open(struct block_device *bdev, fmode_t mode)
845{
846 int ret;
847
848 lock_kernel();
849 ret = ida_open(bdev, mode);
850 unlock_kernel();
851
852 return ret;
853}
854
843/* 855/*
844 * Close. Sync first. 856 * Close. Sync first.
845 */ 857 */
846static int ida_release(struct gendisk *disk, fmode_t mode) 858static int ida_release(struct gendisk *disk, fmode_t mode)
847{ 859{
848 ctlr_info_t *host = get_host(disk); 860 ctlr_info_t *host;
861
862 lock_kernel();
863 host = get_host(disk);
849 host->usage_count--; 864 host->usage_count--;
865 unlock_kernel();
866
850 return 0; 867 return 0;
851} 868}
852 869
@@ -1128,7 +1145,7 @@ static int ida_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1128 * ida_ioctl does some miscellaneous stuff like reporting drive geometry, 1145 * ida_ioctl does some miscellaneous stuff like reporting drive geometry,
1129 * setting readahead and submitting commands from userspace to the controller. 1146 * setting readahead and submitting commands from userspace to the controller.
1130 */ 1147 */
1131static int ida_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg) 1148static int ida_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
1132{ 1149{
1133 drv_info_t *drv = get_drv(bdev->bd_disk); 1150 drv_info_t *drv = get_drv(bdev->bd_disk);
1134 ctlr_info_t *host = get_host(bdev->bd_disk); 1151 ctlr_info_t *host = get_host(bdev->bd_disk);
@@ -1162,7 +1179,8 @@ out_passthru:
1162 return error; 1179 return error;
1163 case IDAGETCTLRSIG: 1180 case IDAGETCTLRSIG:
1164 if (!arg) return -EINVAL; 1181 if (!arg) return -EINVAL;
1165 put_user(host->ctlr_sig, (int __user *)arg); 1182 if (put_user(host->ctlr_sig, (int __user *)arg))
1183 return -EFAULT;
1166 return 0; 1184 return 0;
1167 case IDAREVALIDATEVOLS: 1185 case IDAREVALIDATEVOLS:
1168 if (MINOR(bdev->bd_dev) != 0) 1186 if (MINOR(bdev->bd_dev) != 0)
@@ -1170,7 +1188,8 @@ out_passthru:
1170 return revalidate_allvol(host); 1188 return revalidate_allvol(host);
1171 case IDADRIVERVERSION: 1189 case IDADRIVERVERSION:
1172 if (!arg) return -EINVAL; 1190 if (!arg) return -EINVAL;
1173 put_user(DRIVER_VERSION, (unsigned long __user *)arg); 1191 if (put_user(DRIVER_VERSION, (unsigned long __user *)arg))
1192 return -EFAULT;
1174 return 0; 1193 return 0;
1175 case IDAGETPCIINFO: 1194 case IDAGETPCIINFO:
1176 { 1195 {
@@ -1192,6 +1211,19 @@ out_passthru:
1192 } 1211 }
1193 1212
1194} 1213}
1214
1215static int ida_ioctl(struct block_device *bdev, fmode_t mode,
1216 unsigned int cmd, unsigned long param)
1217{
1218 int ret;
1219
1220 lock_kernel();
1221 ret = ida_locked_ioctl(bdev, mode, cmd, param);
1222 unlock_kernel();
1223
1224 return ret;
1225}
1226
1195/* 1227/*
1196 * ida_ctlr_ioctl is for passing commands to the controller from userspace. 1228 * ida_ctlr_ioctl is for passing commands to the controller from userspace.
1197 * The command block (io) has already been copied to kernel space for us, 1229 * The command block (io) has already been copied to kernel space for us,
@@ -1225,17 +1257,11 @@ static int ida_ctlr_ioctl(ctlr_info_t *h, int dsk, ida_ioctl_t *io)
1225 /* Pre submit processing */ 1257 /* Pre submit processing */
1226 switch(io->cmd) { 1258 switch(io->cmd) {
1227 case PASSTHRU_A: 1259 case PASSTHRU_A:
1228 p = kmalloc(io->sg[0].size, GFP_KERNEL); 1260 p = memdup_user(io->sg[0].addr, io->sg[0].size);
1229 if (!p) 1261 if (IS_ERR(p)) {
1230 { 1262 error = PTR_ERR(p);
1231 error = -ENOMEM; 1263 cmd_free(h, c, 0);
1232 cmd_free(h, c, 0); 1264 return error;
1233 return(error);
1234 }
1235 if (copy_from_user(p, io->sg[0].addr, io->sg[0].size)) {
1236 kfree(p);
1237 cmd_free(h, c, 0);
1238 return -EFAULT;
1239 } 1265 }
1240 c->req.hdr.blk = pci_map_single(h->pci_dev, &(io->c), 1266 c->req.hdr.blk = pci_map_single(h->pci_dev, &(io->c),
1241 sizeof(ida_ioctl_t), 1267 sizeof(ida_ioctl_t),
@@ -1266,18 +1292,12 @@ static int ida_ctlr_ioctl(ctlr_info_t *h, int dsk, ida_ioctl_t *io)
1266 case DIAG_PASS_THRU: 1292 case DIAG_PASS_THRU:
1267 case COLLECT_BUFFER: 1293 case COLLECT_BUFFER:
1268 case WRITE_FLASH_ROM: 1294 case WRITE_FLASH_ROM:
1269 p = kmalloc(io->sg[0].size, GFP_KERNEL); 1295 p = memdup_user(io->sg[0].addr, io->sg[0].size);
1270 if (!p) 1296 if (IS_ERR(p)) {
1271 { 1297 error = PTR_ERR(p);
1272 error = -ENOMEM; 1298 cmd_free(h, c, 0);
1273 cmd_free(h, c, 0); 1299 return error;
1274 return(error);
1275 } 1300 }
1276 if (copy_from_user(p, io->sg[0].addr, io->sg[0].size)) {
1277 kfree(p);
1278 cmd_free(h, c, 0);
1279 return -EFAULT;
1280 }
1281 c->req.sg[0].size = io->sg[0].size; 1301 c->req.sg[0].size = io->sg[0].size;
1282 c->req.sg[0].addr = pci_map_single(h->pci_dev, p, 1302 c->req.sg[0].addr = pci_map_single(h->pci_dev, p,
1283 c->req.sg[0].size, PCI_DMA_BIDIRECTIONAL); 1303 c->req.sg[0].size, PCI_DMA_BIDIRECTIONAL);
diff --git a/drivers/block/drbd/drbd_actlog.c b/drivers/block/drbd/drbd_actlog.c
index df018990c42..9400845d602 100644
--- a/drivers/block/drbd/drbd_actlog.c
+++ b/drivers/block/drbd/drbd_actlog.c
@@ -79,8 +79,8 @@ static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
79 md_io.error = 0; 79 md_io.error = 0;
80 80
81 if ((rw & WRITE) && !test_bit(MD_NO_BARRIER, &mdev->flags)) 81 if ((rw & WRITE) && !test_bit(MD_NO_BARRIER, &mdev->flags))
82 rw |= (1 << BIO_RW_BARRIER); 82 rw |= REQ_HARDBARRIER;
83 rw |= ((1<<BIO_RW_UNPLUG) | (1<<BIO_RW_SYNCIO)); 83 rw |= REQ_UNPLUG | REQ_SYNC;
84 84
85 retry: 85 retry:
86 bio = bio_alloc(GFP_NOIO, 1); 86 bio = bio_alloc(GFP_NOIO, 1);
@@ -103,11 +103,11 @@ static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
103 /* check for unsupported barrier op. 103 /* check for unsupported barrier op.
104 * would rather check on EOPNOTSUPP, but that is not reliable. 104 * would rather check on EOPNOTSUPP, but that is not reliable.
105 * don't try again for ANY return value != 0 */ 105 * don't try again for ANY return value != 0 */
106 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER) && !ok)) { 106 if (unlikely((bio->bi_rw & REQ_HARDBARRIER) && !ok)) {
107 /* Try again with no barrier */ 107 /* Try again with no barrier */
108 dev_warn(DEV, "Barriers not supported on meta data device - disabling\n"); 108 dev_warn(DEV, "Barriers not supported on meta data device - disabling\n");
109 set_bit(MD_NO_BARRIER, &mdev->flags); 109 set_bit(MD_NO_BARRIER, &mdev->flags);
110 rw &= ~(1 << BIO_RW_BARRIER); 110 rw &= ~REQ_HARDBARRIER;
111 bio_put(bio); 111 bio_put(bio);
112 goto retry; 112 goto retry;
113 } 113 }
diff --git a/drivers/block/drbd/drbd_int.h b/drivers/block/drbd/drbd_int.h
index 485ed8c7d62..352441b0f92 100644
--- a/drivers/block/drbd/drbd_int.h
+++ b/drivers/block/drbd/drbd_int.h
@@ -550,12 +550,6 @@ struct p_delay_probe {
550 u32 offset; /* usecs the probe got sent after the reference time point */ 550 u32 offset; /* usecs the probe got sent after the reference time point */
551} __packed; 551} __packed;
552 552
553struct delay_probe {
554 struct list_head list;
555 unsigned int seq_num;
556 struct timeval time;
557};
558
559/* DCBP: Drbd Compressed Bitmap Packet ... */ 553/* DCBP: Drbd Compressed Bitmap Packet ... */
560static inline enum drbd_bitmap_code 554static inline enum drbd_bitmap_code
561DCBP_get_code(struct p_compressed_bm *p) 555DCBP_get_code(struct p_compressed_bm *p)
@@ -942,11 +936,9 @@ struct drbd_conf {
942 unsigned int ko_count; 936 unsigned int ko_count;
943 struct drbd_work resync_work, 937 struct drbd_work resync_work,
944 unplug_work, 938 unplug_work,
945 md_sync_work, 939 md_sync_work;
946 delay_probe_work;
947 struct timer_list resync_timer; 940 struct timer_list resync_timer;
948 struct timer_list md_sync_timer; 941 struct timer_list md_sync_timer;
949 struct timer_list delay_probe_timer;
950 942
951 /* Used after attach while negotiating new disk state. */ 943 /* Used after attach while negotiating new disk state. */
952 union drbd_state new_state_tmp; 944 union drbd_state new_state_tmp;
@@ -1062,12 +1054,6 @@ struct drbd_conf {
1062 u64 ed_uuid; /* UUID of the exposed data */ 1054 u64 ed_uuid; /* UUID of the exposed data */
1063 struct mutex state_mutex; 1055 struct mutex state_mutex;
1064 char congestion_reason; /* Why we where congested... */ 1056 char congestion_reason; /* Why we where congested... */
1065 struct list_head delay_probes; /* protected by peer_seq_lock */
1066 int data_delay; /* Delay of packets on the data-sock behind meta-sock */
1067 unsigned int delay_seq; /* To generate sequence numbers of delay probes */
1068 struct timeval dps_time; /* delay-probes-start-time */
1069 unsigned int dp_volume_last; /* send_cnt of last delay probe */
1070 int c_sync_rate; /* current resync rate after delay_probe magic */
1071}; 1057};
1072 1058
1073static inline struct drbd_conf *minor_to_mdev(unsigned int minor) 1059static inline struct drbd_conf *minor_to_mdev(unsigned int minor)
diff --git a/drivers/block/drbd/drbd_main.c b/drivers/block/drbd/drbd_main.c
index 7258c95e895..fa650dd85b9 100644
--- a/drivers/block/drbd/drbd_main.c
+++ b/drivers/block/drbd/drbd_main.c
@@ -2184,43 +2184,6 @@ int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
2184 return ok; 2184 return ok;
2185} 2185}
2186 2186
2187static int drbd_send_delay_probe(struct drbd_conf *mdev, struct drbd_socket *ds)
2188{
2189 struct p_delay_probe dp;
2190 int offset, ok = 0;
2191 struct timeval now;
2192
2193 mutex_lock(&ds->mutex);
2194 if (likely(ds->socket)) {
2195 do_gettimeofday(&now);
2196 offset = now.tv_usec - mdev->dps_time.tv_usec +
2197 (now.tv_sec - mdev->dps_time.tv_sec) * 1000000;
2198 dp.seq_num = cpu_to_be32(mdev->delay_seq);
2199 dp.offset = cpu_to_be32(offset);
2200
2201 ok = _drbd_send_cmd(mdev, ds->socket, P_DELAY_PROBE,
2202 (struct p_header *)&dp, sizeof(dp), 0);
2203 }
2204 mutex_unlock(&ds->mutex);
2205
2206 return ok;
2207}
2208
2209static int drbd_send_delay_probes(struct drbd_conf *mdev)
2210{
2211 int ok;
2212
2213 mdev->delay_seq++;
2214 do_gettimeofday(&mdev->dps_time);
2215 ok = drbd_send_delay_probe(mdev, &mdev->meta);
2216 ok = ok && drbd_send_delay_probe(mdev, &mdev->data);
2217
2218 mdev->dp_volume_last = mdev->send_cnt;
2219 mod_timer(&mdev->delay_probe_timer, jiffies + mdev->sync_conf.dp_interval * HZ / 10);
2220
2221 return ok;
2222}
2223
2224/* called on sndtimeo 2187/* called on sndtimeo
2225 * returns FALSE if we should retry, 2188 * returns FALSE if we should retry,
2226 * TRUE if we think connection is dead 2189 * TRUE if we think connection is dead
@@ -2369,31 +2332,6 @@ static int _drbd_send_zc_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e)
2369 return 1; 2332 return 1;
2370} 2333}
2371 2334
2372static void consider_delay_probes(struct drbd_conf *mdev)
2373{
2374 if (mdev->state.conn != C_SYNC_SOURCE || mdev->agreed_pro_version < 93)
2375 return;
2376
2377 if (mdev->dp_volume_last + mdev->sync_conf.dp_volume * 2 < mdev->send_cnt)
2378 drbd_send_delay_probes(mdev);
2379}
2380
2381static int w_delay_probes(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
2382{
2383 if (!cancel && mdev->state.conn == C_SYNC_SOURCE)
2384 drbd_send_delay_probes(mdev);
2385
2386 return 1;
2387}
2388
2389static void delay_probe_timer_fn(unsigned long data)
2390{
2391 struct drbd_conf *mdev = (struct drbd_conf *) data;
2392
2393 if (list_empty(&mdev->delay_probe_work.list))
2394 drbd_queue_work(&mdev->data.work, &mdev->delay_probe_work);
2395}
2396
2397/* Used to send write requests 2335/* Used to send write requests
2398 * R_PRIMARY -> Peer (P_DATA) 2336 * R_PRIMARY -> Peer (P_DATA)
2399 */ 2337 */
@@ -2425,15 +2363,15 @@ int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
2425 /* NOTE: no need to check if barriers supported here as we would 2363 /* NOTE: no need to check if barriers supported here as we would
2426 * not pass the test in make_request_common in that case 2364 * not pass the test in make_request_common in that case
2427 */ 2365 */
2428 if (bio_rw_flagged(req->master_bio, BIO_RW_BARRIER)) { 2366 if (req->master_bio->bi_rw & REQ_HARDBARRIER) {
2429 dev_err(DEV, "ASSERT FAILED would have set DP_HARDBARRIER\n"); 2367 dev_err(DEV, "ASSERT FAILED would have set DP_HARDBARRIER\n");
2430 /* dp_flags |= DP_HARDBARRIER; */ 2368 /* dp_flags |= DP_HARDBARRIER; */
2431 } 2369 }
2432 if (bio_rw_flagged(req->master_bio, BIO_RW_SYNCIO)) 2370 if (req->master_bio->bi_rw & REQ_SYNC)
2433 dp_flags |= DP_RW_SYNC; 2371 dp_flags |= DP_RW_SYNC;
2434 /* for now handle SYNCIO and UNPLUG 2372 /* for now handle SYNCIO and UNPLUG
2435 * as if they still were one and the same flag */ 2373 * as if they still were one and the same flag */
2436 if (bio_rw_flagged(req->master_bio, BIO_RW_UNPLUG)) 2374 if (req->master_bio->bi_rw & REQ_UNPLUG)
2437 dp_flags |= DP_RW_SYNC; 2375 dp_flags |= DP_RW_SYNC;
2438 if (mdev->state.conn >= C_SYNC_SOURCE && 2376 if (mdev->state.conn >= C_SYNC_SOURCE &&
2439 mdev->state.conn <= C_PAUSED_SYNC_T) 2377 mdev->state.conn <= C_PAUSED_SYNC_T)
@@ -2457,9 +2395,6 @@ int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
2457 2395
2458 drbd_put_data_sock(mdev); 2396 drbd_put_data_sock(mdev);
2459 2397
2460 if (ok)
2461 consider_delay_probes(mdev);
2462
2463 return ok; 2398 return ok;
2464} 2399}
2465 2400
@@ -2506,9 +2441,6 @@ int drbd_send_block(struct drbd_conf *mdev, enum drbd_packets cmd,
2506 2441
2507 drbd_put_data_sock(mdev); 2442 drbd_put_data_sock(mdev);
2508 2443
2509 if (ok)
2510 consider_delay_probes(mdev);
2511
2512 return ok; 2444 return ok;
2513} 2445}
2514 2446
@@ -2604,6 +2536,7 @@ static int drbd_open(struct block_device *bdev, fmode_t mode)
2604 unsigned long flags; 2536 unsigned long flags;
2605 int rv = 0; 2537 int rv = 0;
2606 2538
2539 lock_kernel();
2607 spin_lock_irqsave(&mdev->req_lock, flags); 2540 spin_lock_irqsave(&mdev->req_lock, flags);
2608 /* to have a stable mdev->state.role 2541 /* to have a stable mdev->state.role
2609 * and no race with updating open_cnt */ 2542 * and no race with updating open_cnt */
@@ -2618,6 +2551,7 @@ static int drbd_open(struct block_device *bdev, fmode_t mode)
2618 if (!rv) 2551 if (!rv)
2619 mdev->open_cnt++; 2552 mdev->open_cnt++;
2620 spin_unlock_irqrestore(&mdev->req_lock, flags); 2553 spin_unlock_irqrestore(&mdev->req_lock, flags);
2554 unlock_kernel();
2621 2555
2622 return rv; 2556 return rv;
2623} 2557}
@@ -2625,7 +2559,9 @@ static int drbd_open(struct block_device *bdev, fmode_t mode)
2625static int drbd_release(struct gendisk *gd, fmode_t mode) 2559static int drbd_release(struct gendisk *gd, fmode_t mode)
2626{ 2560{
2627 struct drbd_conf *mdev = gd->private_data; 2561 struct drbd_conf *mdev = gd->private_data;
2562 lock_kernel();
2628 mdev->open_cnt--; 2563 mdev->open_cnt--;
2564 unlock_kernel();
2629 return 0; 2565 return 0;
2630} 2566}
2631 2567
@@ -2660,9 +2596,20 @@ static void drbd_unplug_fn(struct request_queue *q)
2660 2596
2661static void drbd_set_defaults(struct drbd_conf *mdev) 2597static void drbd_set_defaults(struct drbd_conf *mdev)
2662{ 2598{
2663 mdev->sync_conf.after = DRBD_AFTER_DEF; 2599 /* This way we get a compile error when sync_conf grows,
2664 mdev->sync_conf.rate = DRBD_RATE_DEF; 2600 and we forgot to initialize it here */
2665 mdev->sync_conf.al_extents = DRBD_AL_EXTENTS_DEF; 2601 mdev->sync_conf = (struct syncer_conf) {
2602 /* .rate = */ DRBD_RATE_DEF,
2603 /* .after = */ DRBD_AFTER_DEF,
2604 /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
2605 /* .verify_alg = */ {}, 0,
2606 /* .cpu_mask = */ {}, 0,
2607 /* .csums_alg = */ {}, 0,
2608 /* .use_rle = */ 0
2609 };
2610
2611 /* Have to use that way, because the layout differs between
2612 big endian and little endian */
2666 mdev->state = (union drbd_state) { 2613 mdev->state = (union drbd_state) {
2667 { .role = R_SECONDARY, 2614 { .role = R_SECONDARY,
2668 .peer = R_UNKNOWN, 2615 .peer = R_UNKNOWN,
@@ -2721,24 +2668,17 @@ void drbd_init_set_defaults(struct drbd_conf *mdev)
2721 INIT_LIST_HEAD(&mdev->unplug_work.list); 2668 INIT_LIST_HEAD(&mdev->unplug_work.list);
2722 INIT_LIST_HEAD(&mdev->md_sync_work.list); 2669 INIT_LIST_HEAD(&mdev->md_sync_work.list);
2723 INIT_LIST_HEAD(&mdev->bm_io_work.w.list); 2670 INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
2724 INIT_LIST_HEAD(&mdev->delay_probes);
2725 INIT_LIST_HEAD(&mdev->delay_probe_work.list);
2726 2671
2727 mdev->resync_work.cb = w_resync_inactive; 2672 mdev->resync_work.cb = w_resync_inactive;
2728 mdev->unplug_work.cb = w_send_write_hint; 2673 mdev->unplug_work.cb = w_send_write_hint;
2729 mdev->md_sync_work.cb = w_md_sync; 2674 mdev->md_sync_work.cb = w_md_sync;
2730 mdev->bm_io_work.w.cb = w_bitmap_io; 2675 mdev->bm_io_work.w.cb = w_bitmap_io;
2731 mdev->delay_probe_work.cb = w_delay_probes;
2732 init_timer(&mdev->resync_timer); 2676 init_timer(&mdev->resync_timer);
2733 init_timer(&mdev->md_sync_timer); 2677 init_timer(&mdev->md_sync_timer);
2734 init_timer(&mdev->delay_probe_timer);
2735 mdev->resync_timer.function = resync_timer_fn; 2678 mdev->resync_timer.function = resync_timer_fn;
2736 mdev->resync_timer.data = (unsigned long) mdev; 2679 mdev->resync_timer.data = (unsigned long) mdev;
2737 mdev->md_sync_timer.function = md_sync_timer_fn; 2680 mdev->md_sync_timer.function = md_sync_timer_fn;
2738 mdev->md_sync_timer.data = (unsigned long) mdev; 2681 mdev->md_sync_timer.data = (unsigned long) mdev;
2739 mdev->delay_probe_timer.function = delay_probe_timer_fn;
2740 mdev->delay_probe_timer.data = (unsigned long) mdev;
2741
2742 2682
2743 init_waitqueue_head(&mdev->misc_wait); 2683 init_waitqueue_head(&mdev->misc_wait);
2744 init_waitqueue_head(&mdev->state_wait); 2684 init_waitqueue_head(&mdev->state_wait);
diff --git a/drivers/block/drbd/drbd_nl.c b/drivers/block/drbd/drbd_nl.c
index 2151f18b21d..73131c5ae33 100644
--- a/drivers/block/drbd/drbd_nl.c
+++ b/drivers/block/drbd/drbd_nl.c
@@ -1557,10 +1557,6 @@ static int drbd_nl_syncer_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *n
1557 sc.rate = DRBD_RATE_DEF; 1557 sc.rate = DRBD_RATE_DEF;
1558 sc.after = DRBD_AFTER_DEF; 1558 sc.after = DRBD_AFTER_DEF;
1559 sc.al_extents = DRBD_AL_EXTENTS_DEF; 1559 sc.al_extents = DRBD_AL_EXTENTS_DEF;
1560 sc.dp_volume = DRBD_DP_VOLUME_DEF;
1561 sc.dp_interval = DRBD_DP_INTERVAL_DEF;
1562 sc.throttle_th = DRBD_RS_THROTTLE_TH_DEF;
1563 sc.hold_off_th = DRBD_RS_HOLD_OFF_TH_DEF;
1564 } else 1560 } else
1565 memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf)); 1561 memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf));
1566 1562
diff --git a/drivers/block/drbd/drbd_proc.c b/drivers/block/drbd/drbd_proc.c
index d0f1767ea4c..be3374b6846 100644
--- a/drivers/block/drbd/drbd_proc.c
+++ b/drivers/block/drbd/drbd_proc.c
@@ -73,21 +73,14 @@ static void drbd_syncer_progress(struct drbd_conf *mdev, struct seq_file *seq)
73 seq_printf(seq, "sync'ed:%3u.%u%% ", res / 10, res % 10); 73 seq_printf(seq, "sync'ed:%3u.%u%% ", res / 10, res % 10);
74 /* if more than 1 GB display in MB */ 74 /* if more than 1 GB display in MB */
75 if (mdev->rs_total > 0x100000L) 75 if (mdev->rs_total > 0x100000L)
76 seq_printf(seq, "(%lu/%lu)M", 76 seq_printf(seq, "(%lu/%lu)M\n\t",
77 (unsigned long) Bit2KB(rs_left >> 10), 77 (unsigned long) Bit2KB(rs_left >> 10),
78 (unsigned long) Bit2KB(mdev->rs_total >> 10)); 78 (unsigned long) Bit2KB(mdev->rs_total >> 10));
79 else 79 else
80 seq_printf(seq, "(%lu/%lu)K", 80 seq_printf(seq, "(%lu/%lu)K\n\t",
81 (unsigned long) Bit2KB(rs_left), 81 (unsigned long) Bit2KB(rs_left),
82 (unsigned long) Bit2KB(mdev->rs_total)); 82 (unsigned long) Bit2KB(mdev->rs_total));
83 83
84 if (mdev->state.conn == C_SYNC_TARGET)
85 seq_printf(seq, " queue_delay: %d.%d ms\n\t",
86 mdev->data_delay / 1000,
87 (mdev->data_delay % 1000) / 100);
88 else if (mdev->state.conn == C_SYNC_SOURCE)
89 seq_printf(seq, " delay_probe: %u\n\t", mdev->delay_seq);
90
91 /* see drivers/md/md.c 84 /* see drivers/md/md.c
92 * We do not want to overflow, so the order of operands and 85 * We do not want to overflow, so the order of operands and
93 * the * 100 / 100 trick are important. We do a +1 to be 86 * the * 100 / 100 trick are important. We do a +1 to be
@@ -135,14 +128,6 @@ static void drbd_syncer_progress(struct drbd_conf *mdev, struct seq_file *seq)
135 else 128 else
136 seq_printf(seq, " (%ld)", dbdt); 129 seq_printf(seq, " (%ld)", dbdt);
137 130
138 if (mdev->state.conn == C_SYNC_TARGET) {
139 if (mdev->c_sync_rate > 1000)
140 seq_printf(seq, " want: %d,%03d",
141 mdev->c_sync_rate / 1000, mdev->c_sync_rate % 1000);
142 else
143 seq_printf(seq, " want: %d", mdev->c_sync_rate);
144 }
145
146 seq_printf(seq, " K/sec\n"); 131 seq_printf(seq, " K/sec\n");
147} 132}
148 133
diff --git a/drivers/block/drbd/drbd_receiver.c b/drivers/block/drbd/drbd_receiver.c
index ec1711f7c5c..081522d3c74 100644
--- a/drivers/block/drbd/drbd_receiver.c
+++ b/drivers/block/drbd/drbd_receiver.c
@@ -1180,7 +1180,7 @@ next_bio:
1180 bio->bi_sector = sector; 1180 bio->bi_sector = sector;
1181 bio->bi_bdev = mdev->ldev->backing_bdev; 1181 bio->bi_bdev = mdev->ldev->backing_bdev;
1182 /* we special case some flags in the multi-bio case, see below 1182 /* we special case some flags in the multi-bio case, see below
1183 * (BIO_RW_UNPLUG, BIO_RW_BARRIER) */ 1183 * (REQ_UNPLUG, REQ_HARDBARRIER) */
1184 bio->bi_rw = rw; 1184 bio->bi_rw = rw;
1185 bio->bi_private = e; 1185 bio->bi_private = e;
1186 bio->bi_end_io = drbd_endio_sec; 1186 bio->bi_end_io = drbd_endio_sec;
@@ -1209,16 +1209,16 @@ next_bio:
1209 bios = bios->bi_next; 1209 bios = bios->bi_next;
1210 bio->bi_next = NULL; 1210 bio->bi_next = NULL;
1211 1211
1212 /* strip off BIO_RW_UNPLUG unless it is the last bio */ 1212 /* strip off REQ_UNPLUG unless it is the last bio */
1213 if (bios) 1213 if (bios)
1214 bio->bi_rw &= ~(1<<BIO_RW_UNPLUG); 1214 bio->bi_rw &= ~REQ_UNPLUG;
1215 1215
1216 drbd_generic_make_request(mdev, fault_type, bio); 1216 drbd_generic_make_request(mdev, fault_type, bio);
1217 1217
1218 /* strip off BIO_RW_BARRIER, 1218 /* strip off REQ_HARDBARRIER,
1219 * unless it is the first or last bio */ 1219 * unless it is the first or last bio */
1220 if (bios && bios->bi_next) 1220 if (bios && bios->bi_next)
1221 bios->bi_rw &= ~(1<<BIO_RW_BARRIER); 1221 bios->bi_rw &= ~REQ_HARDBARRIER;
1222 } while (bios); 1222 } while (bios);
1223 maybe_kick_lo(mdev); 1223 maybe_kick_lo(mdev);
1224 return 0; 1224 return 0;
@@ -1233,7 +1233,7 @@ fail:
1233} 1233}
1234 1234
1235/** 1235/**
1236 * w_e_reissue() - Worker callback; Resubmit a bio, without BIO_RW_BARRIER set 1236 * w_e_reissue() - Worker callback; Resubmit a bio, without REQ_HARDBARRIER set
1237 * @mdev: DRBD device. 1237 * @mdev: DRBD device.
1238 * @w: work object. 1238 * @w: work object.
1239 * @cancel: The connection will be closed anyways (unused in this callback) 1239 * @cancel: The connection will be closed anyways (unused in this callback)
@@ -1245,7 +1245,7 @@ int w_e_reissue(struct drbd_conf *mdev, struct drbd_work *w, int cancel) __relea
1245 (and DE_BARRIER_IN_NEXT_EPOCH_ISSUED in the previous Epoch) 1245 (and DE_BARRIER_IN_NEXT_EPOCH_ISSUED in the previous Epoch)
1246 so that we can finish that epoch in drbd_may_finish_epoch(). 1246 so that we can finish that epoch in drbd_may_finish_epoch().
1247 That is necessary if we already have a long chain of Epochs, before 1247 That is necessary if we already have a long chain of Epochs, before
1248 we realize that BIO_RW_BARRIER is actually not supported */ 1248 we realize that REQ_HARDBARRIER is actually not supported */
1249 1249
1250 /* As long as the -ENOTSUPP on the barrier is reported immediately 1250 /* As long as the -ENOTSUPP on the barrier is reported immediately
1251 that will never trigger. If it is reported late, we will just 1251 that will never trigger. If it is reported late, we will just
@@ -1824,14 +1824,14 @@ static int receive_Data(struct drbd_conf *mdev, struct p_header *h)
1824 epoch = list_entry(e->epoch->list.prev, struct drbd_epoch, list); 1824 epoch = list_entry(e->epoch->list.prev, struct drbd_epoch, list);
1825 if (epoch == e->epoch) { 1825 if (epoch == e->epoch) {
1826 set_bit(DE_CONTAINS_A_BARRIER, &e->epoch->flags); 1826 set_bit(DE_CONTAINS_A_BARRIER, &e->epoch->flags);
1827 rw |= (1<<BIO_RW_BARRIER); 1827 rw |= REQ_HARDBARRIER;
1828 e->flags |= EE_IS_BARRIER; 1828 e->flags |= EE_IS_BARRIER;
1829 } else { 1829 } else {
1830 if (atomic_read(&epoch->epoch_size) > 1 || 1830 if (atomic_read(&epoch->epoch_size) > 1 ||
1831 !test_bit(DE_CONTAINS_A_BARRIER, &epoch->flags)) { 1831 !test_bit(DE_CONTAINS_A_BARRIER, &epoch->flags)) {
1832 set_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &epoch->flags); 1832 set_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &epoch->flags);
1833 set_bit(DE_CONTAINS_A_BARRIER, &e->epoch->flags); 1833 set_bit(DE_CONTAINS_A_BARRIER, &e->epoch->flags);
1834 rw |= (1<<BIO_RW_BARRIER); 1834 rw |= REQ_HARDBARRIER;
1835 e->flags |= EE_IS_BARRIER; 1835 e->flags |= EE_IS_BARRIER;
1836 } 1836 }
1837 } 1837 }
@@ -1841,10 +1841,10 @@ static int receive_Data(struct drbd_conf *mdev, struct p_header *h)
1841 dp_flags = be32_to_cpu(p->dp_flags); 1841 dp_flags = be32_to_cpu(p->dp_flags);
1842 if (dp_flags & DP_HARDBARRIER) { 1842 if (dp_flags & DP_HARDBARRIER) {
1843 dev_err(DEV, "ASSERT FAILED would have submitted barrier request\n"); 1843 dev_err(DEV, "ASSERT FAILED would have submitted barrier request\n");
1844 /* rw |= (1<<BIO_RW_BARRIER); */ 1844 /* rw |= REQ_HARDBARRIER; */
1845 } 1845 }
1846 if (dp_flags & DP_RW_SYNC) 1846 if (dp_flags & DP_RW_SYNC)
1847 rw |= (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); 1847 rw |= REQ_SYNC | REQ_UNPLUG;
1848 if (dp_flags & DP_MAY_SET_IN_SYNC) 1848 if (dp_flags & DP_MAY_SET_IN_SYNC)
1849 e->flags |= EE_MAY_SET_IN_SYNC; 1849 e->flags |= EE_MAY_SET_IN_SYNC;
1850 1850
@@ -3555,14 +3555,15 @@ static int receive_bitmap(struct drbd_conf *mdev, struct p_header *h)
3555 return ok; 3555 return ok;
3556} 3556}
3557 3557
3558static int receive_skip(struct drbd_conf *mdev, struct p_header *h) 3558static int receive_skip_(struct drbd_conf *mdev, struct p_header *h, int silent)
3559{ 3559{
3560 /* TODO zero copy sink :) */ 3560 /* TODO zero copy sink :) */
3561 static char sink[128]; 3561 static char sink[128];
3562 int size, want, r; 3562 int size, want, r;
3563 3563
3564 dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n", 3564 if (!silent)
3565 h->command, h->length); 3565 dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n",
3566 h->command, h->length);
3566 3567
3567 size = h->length; 3568 size = h->length;
3568 while (size > 0) { 3569 while (size > 0) {
@@ -3574,101 +3575,25 @@ static int receive_skip(struct drbd_conf *mdev, struct p_header *h)
3574 return size == 0; 3575 return size == 0;
3575} 3576}
3576 3577
3577static int receive_UnplugRemote(struct drbd_conf *mdev, struct p_header *h) 3578static int receive_skip(struct drbd_conf *mdev, struct p_header *h)
3578{
3579 if (mdev->state.disk >= D_INCONSISTENT)
3580 drbd_kick_lo(mdev);
3581
3582 /* Make sure we've acked all the TCP data associated
3583 * with the data requests being unplugged */
3584 drbd_tcp_quickack(mdev->data.socket);
3585
3586 return TRUE;
3587}
3588
3589static void timeval_sub_us(struct timeval* tv, unsigned int us)
3590{ 3579{
3591 tv->tv_sec -= us / 1000000; 3580 return receive_skip_(mdev, h, 0);
3592 us = us % 1000000;
3593 if (tv->tv_usec > us) {
3594 tv->tv_usec += 1000000;
3595 tv->tv_sec--;
3596 }
3597 tv->tv_usec -= us;
3598} 3581}
3599 3582
3600static void got_delay_probe(struct drbd_conf *mdev, int from, struct p_delay_probe *p) 3583static int receive_skip_silent(struct drbd_conf *mdev, struct p_header *h)
3601{ 3584{
3602 struct delay_probe *dp; 3585 return receive_skip_(mdev, h, 1);
3603 struct list_head *le;
3604 struct timeval now;
3605 int seq_num;
3606 int offset;
3607 int data_delay;
3608
3609 seq_num = be32_to_cpu(p->seq_num);
3610 offset = be32_to_cpu(p->offset);
3611
3612 spin_lock(&mdev->peer_seq_lock);
3613 if (!list_empty(&mdev->delay_probes)) {
3614 if (from == USE_DATA_SOCKET)
3615 le = mdev->delay_probes.next;
3616 else
3617 le = mdev->delay_probes.prev;
3618
3619 dp = list_entry(le, struct delay_probe, list);
3620
3621 if (dp->seq_num == seq_num) {
3622 list_del(le);
3623 spin_unlock(&mdev->peer_seq_lock);
3624 do_gettimeofday(&now);
3625 timeval_sub_us(&now, offset);
3626 data_delay =
3627 now.tv_usec - dp->time.tv_usec +
3628 (now.tv_sec - dp->time.tv_sec) * 1000000;
3629
3630 if (data_delay > 0)
3631 mdev->data_delay = data_delay;
3632
3633 kfree(dp);
3634 return;
3635 }
3636
3637 if (dp->seq_num > seq_num) {
3638 spin_unlock(&mdev->peer_seq_lock);
3639 dev_warn(DEV, "Previous allocation failure of struct delay_probe?\n");
3640 return; /* Do not alloca a struct delay_probe.... */
3641 }
3642 }
3643 spin_unlock(&mdev->peer_seq_lock);
3644
3645 dp = kmalloc(sizeof(struct delay_probe), GFP_NOIO);
3646 if (!dp) {
3647 dev_warn(DEV, "Failed to allocate a struct delay_probe, do not worry.\n");
3648 return;
3649 }
3650
3651 dp->seq_num = seq_num;
3652 do_gettimeofday(&dp->time);
3653 timeval_sub_us(&dp->time, offset);
3654
3655 spin_lock(&mdev->peer_seq_lock);
3656 if (from == USE_DATA_SOCKET)
3657 list_add(&dp->list, &mdev->delay_probes);
3658 else
3659 list_add_tail(&dp->list, &mdev->delay_probes);
3660 spin_unlock(&mdev->peer_seq_lock);
3661} 3586}
3662 3587
3663static int receive_delay_probe(struct drbd_conf *mdev, struct p_header *h) 3588static int receive_UnplugRemote(struct drbd_conf *mdev, struct p_header *h)
3664{ 3589{
3665 struct p_delay_probe *p = (struct p_delay_probe *)h; 3590 if (mdev->state.disk >= D_INCONSISTENT)
3591 drbd_kick_lo(mdev);
3666 3592
3667 ERR_IF(h->length != (sizeof(*p)-sizeof(*h))) return FALSE; 3593 /* Make sure we've acked all the TCP data associated
3668 if (drbd_recv(mdev, h->payload, h->length) != h->length) 3594 * with the data requests being unplugged */
3669 return FALSE; 3595 drbd_tcp_quickack(mdev->data.socket);
3670 3596
3671 got_delay_probe(mdev, USE_DATA_SOCKET, p);
3672 return TRUE; 3597 return TRUE;
3673} 3598}
3674 3599
@@ -3695,7 +3620,7 @@ static drbd_cmd_handler_f drbd_default_handler[] = {
3695 [P_OV_REQUEST] = receive_DataRequest, 3620 [P_OV_REQUEST] = receive_DataRequest,
3696 [P_OV_REPLY] = receive_DataRequest, 3621 [P_OV_REPLY] = receive_DataRequest,
3697 [P_CSUM_RS_REQUEST] = receive_DataRequest, 3622 [P_CSUM_RS_REQUEST] = receive_DataRequest,
3698 [P_DELAY_PROBE] = receive_delay_probe, 3623 [P_DELAY_PROBE] = receive_skip_silent,
3699 /* anything missing from this table is in 3624 /* anything missing from this table is in
3700 * the asender_tbl, see get_asender_cmd */ 3625 * the asender_tbl, see get_asender_cmd */
3701 [P_MAX_CMD] = NULL, 3626 [P_MAX_CMD] = NULL,
@@ -4472,11 +4397,9 @@ static int got_OVResult(struct drbd_conf *mdev, struct p_header *h)
4472 return TRUE; 4397 return TRUE;
4473} 4398}
4474 4399
4475static int got_delay_probe_m(struct drbd_conf *mdev, struct p_header *h) 4400static int got_something_to_ignore_m(struct drbd_conf *mdev, struct p_header *h)
4476{ 4401{
4477 struct p_delay_probe *p = (struct p_delay_probe *)h; 4402 /* IGNORE */
4478
4479 got_delay_probe(mdev, USE_META_SOCKET, p);
4480 return TRUE; 4403 return TRUE;
4481} 4404}
4482 4405
@@ -4504,7 +4427,7 @@ static struct asender_cmd *get_asender_cmd(int cmd)
4504 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck }, 4427 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4505 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply }, 4428 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4506 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync }, 4429 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
4507 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe), got_delay_probe_m }, 4430 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe), got_something_to_ignore_m },
4508 [P_MAX_CMD] = { 0, NULL }, 4431 [P_MAX_CMD] = { 0, NULL },
4509 }; 4432 };
4510 if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL) 4433 if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL)
diff --git a/drivers/block/drbd/drbd_req.c b/drivers/block/drbd/drbd_req.c
index 654f1ef5cbb..f761d98a4e9 100644
--- a/drivers/block/drbd/drbd_req.c
+++ b/drivers/block/drbd/drbd_req.c
@@ -997,7 +997,7 @@ int drbd_make_request_26(struct request_queue *q, struct bio *bio)
997 * because of those XXX, this is not yet enabled, 997 * because of those XXX, this is not yet enabled,
998 * i.e. in drbd_init_set_defaults we set the NO_BARRIER_SUPP bit. 998 * i.e. in drbd_init_set_defaults we set the NO_BARRIER_SUPP bit.
999 */ 999 */
1000 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER) && test_bit(NO_BARRIER_SUPP, &mdev->flags))) { 1000 if (unlikely(bio->bi_rw & REQ_HARDBARRIER) && test_bit(NO_BARRIER_SUPP, &mdev->flags)) {
1001 /* dev_warn(DEV, "Rejecting barrier request as underlying device does not support\n"); */ 1001 /* dev_warn(DEV, "Rejecting barrier request as underlying device does not support\n"); */
1002 bio_endio(bio, -EOPNOTSUPP); 1002 bio_endio(bio, -EOPNOTSUPP);
1003 return 0; 1003 return 0;
diff --git a/drivers/block/drbd/drbd_worker.c b/drivers/block/drbd/drbd_worker.c
index b623ceee2a4..ca4a16cea2d 100644
--- a/drivers/block/drbd/drbd_worker.c
+++ b/drivers/block/drbd/drbd_worker.c
@@ -424,18 +424,6 @@ void resync_timer_fn(unsigned long data)
424 drbd_queue_work(&mdev->data.work, &mdev->resync_work); 424 drbd_queue_work(&mdev->data.work, &mdev->resync_work);
425} 425}
426 426
427static int calc_resync_rate(struct drbd_conf *mdev)
428{
429 int d = mdev->data_delay / 1000; /* us -> ms */
430 int td = mdev->sync_conf.throttle_th * 100; /* 0.1s -> ms */
431 int hd = mdev->sync_conf.hold_off_th * 100; /* 0.1s -> ms */
432 int cr = mdev->sync_conf.rate;
433
434 return d <= td ? cr :
435 d >= hd ? 0 :
436 cr + (cr * (td - d) / (hd - td));
437}
438
439int w_make_resync_request(struct drbd_conf *mdev, 427int w_make_resync_request(struct drbd_conf *mdev,
440 struct drbd_work *w, int cancel) 428 struct drbd_work *w, int cancel)
441{ 429{
@@ -473,8 +461,7 @@ int w_make_resync_request(struct drbd_conf *mdev,
473 max_segment_size = mdev->agreed_pro_version < 94 ? 461 max_segment_size = mdev->agreed_pro_version < 94 ?
474 queue_max_segment_size(mdev->rq_queue) : DRBD_MAX_SEGMENT_SIZE; 462 queue_max_segment_size(mdev->rq_queue) : DRBD_MAX_SEGMENT_SIZE;
475 463
476 mdev->c_sync_rate = calc_resync_rate(mdev); 464 number = SLEEP_TIME * mdev->sync_conf.rate / ((BM_BLOCK_SIZE / 1024) * HZ);
477 number = SLEEP_TIME * mdev->c_sync_rate / ((BM_BLOCK_SIZE / 1024) * HZ);
478 pe = atomic_read(&mdev->rs_pending_cnt); 465 pe = atomic_read(&mdev->rs_pending_cnt);
479 466
480 mutex_lock(&mdev->data.mutex); 467 mutex_lock(&mdev->data.mutex);
diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
index 90c4038702d..cf04c1b234e 100644
--- a/drivers/block/floppy.c
+++ b/drivers/block/floppy.c
@@ -178,6 +178,7 @@ static int print_unex = 1;
178#include <linux/slab.h> 178#include <linux/slab.h>
179#include <linux/mm.h> 179#include <linux/mm.h>
180#include <linux/bio.h> 180#include <linux/bio.h>
181#include <linux/smp_lock.h>
181#include <linux/string.h> 182#include <linux/string.h>
182#include <linux/jiffies.h> 183#include <linux/jiffies.h>
183#include <linux/fcntl.h> 184#include <linux/fcntl.h>
@@ -514,8 +515,6 @@ static unsigned long fdc_busy;
514static DECLARE_WAIT_QUEUE_HEAD(fdc_wait); 515static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
515static DECLARE_WAIT_QUEUE_HEAD(command_done); 516static DECLARE_WAIT_QUEUE_HEAD(command_done);
516 517
517#define NO_SIGNAL (!interruptible || !signal_pending(current))
518
519/* Errors during formatting are counted here. */ 518/* Errors during formatting are counted here. */
520static int format_errors; 519static int format_errors;
521 520
@@ -539,7 +538,7 @@ static int max_buffer_sectors;
539 538
540static int *errors; 539static int *errors;
541typedef void (*done_f)(int); 540typedef void (*done_f)(int);
542static struct cont_t { 541static const struct cont_t {
543 void (*interrupt)(void); 542 void (*interrupt)(void);
544 /* this is called after the interrupt of the 543 /* this is called after the interrupt of the
545 * main command */ 544 * main command */
@@ -578,7 +577,7 @@ static void reset_fdc(void);
578#define NEED_1_RECAL -2 577#define NEED_1_RECAL -2
579#define NEED_2_RECAL -3 578#define NEED_2_RECAL -3
580 579
581static int usage_count; 580static atomic_t usage_count = ATOMIC_INIT(0);
582 581
583/* buffer related variables */ 582/* buffer related variables */
584static int buffer_track = -1; 583static int buffer_track = -1;
@@ -858,36 +857,15 @@ static void set_fdc(int drive)
858} 857}
859 858
860/* locks the driver */ 859/* locks the driver */
861static int _lock_fdc(int drive, bool interruptible, int line) 860static int lock_fdc(int drive, bool interruptible)
862{ 861{
863 if (!usage_count) { 862 if (WARN(atomic_read(&usage_count) == 0,
864 pr_err("Trying to lock fdc while usage count=0 at line %d\n", 863 "Trying to lock fdc while usage count=0\n"))
865 line);
866 return -1; 864 return -1;
867 }
868
869 if (test_and_set_bit(0, &fdc_busy)) {
870 DECLARE_WAITQUEUE(wait, current);
871 add_wait_queue(&fdc_wait, &wait);
872
873 for (;;) {
874 set_current_state(TASK_INTERRUPTIBLE);
875
876 if (!test_and_set_bit(0, &fdc_busy))
877 break;
878 865
879 schedule(); 866 if (wait_event_interruptible(fdc_wait, !test_and_set_bit(0, &fdc_busy)))
880 867 return -EINTR;
881 if (!NO_SIGNAL) {
882 remove_wait_queue(&fdc_wait, &wait);
883 return -EINTR;
884 }
885 }
886 868
887 set_current_state(TASK_RUNNING);
888 remove_wait_queue(&fdc_wait, &wait);
889 flush_scheduled_work();
890 }
891 command_status = FD_COMMAND_NONE; 869 command_status = FD_COMMAND_NONE;
892 870
893 __reschedule_timeout(drive, "lock fdc"); 871 __reschedule_timeout(drive, "lock fdc");
@@ -895,11 +873,8 @@ static int _lock_fdc(int drive, bool interruptible, int line)
895 return 0; 873 return 0;
896} 874}
897 875
898#define lock_fdc(drive, interruptible) \
899 _lock_fdc(drive, interruptible, __LINE__)
900
901/* unlocks the driver */ 876/* unlocks the driver */
902static inline void unlock_fdc(void) 877static void unlock_fdc(void)
903{ 878{
904 unsigned long flags; 879 unsigned long flags;
905 880
@@ -1224,7 +1199,7 @@ static int need_more_output(void)
1224/* Set perpendicular mode as required, based on data rate, if supported. 1199/* Set perpendicular mode as required, based on data rate, if supported.
1225 * 82077 Now tested. 1Mbps data rate only possible with 82077-1. 1200 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1226 */ 1201 */
1227static inline void perpendicular_mode(void) 1202static void perpendicular_mode(void)
1228{ 1203{
1229 unsigned char perp_mode; 1204 unsigned char perp_mode;
1230 1205
@@ -1995,14 +1970,14 @@ static void do_wakeup(void)
1995 wake_up(&command_done); 1970 wake_up(&command_done);
1996} 1971}
1997 1972
1998static struct cont_t wakeup_cont = { 1973static const struct cont_t wakeup_cont = {
1999 .interrupt = empty, 1974 .interrupt = empty,
2000 .redo = do_wakeup, 1975 .redo = do_wakeup,
2001 .error = empty, 1976 .error = empty,
2002 .done = (done_f)empty 1977 .done = (done_f)empty
2003}; 1978};
2004 1979
2005static struct cont_t intr_cont = { 1980static const struct cont_t intr_cont = {
2006 .interrupt = empty, 1981 .interrupt = empty,
2007 .redo = process_fd_request, 1982 .redo = process_fd_request,
2008 .error = empty, 1983 .error = empty,
@@ -2015,25 +1990,10 @@ static int wait_til_done(void (*handler)(void), bool interruptible)
2015 1990
2016 schedule_bh(handler); 1991 schedule_bh(handler);
2017 1992
2018 if (command_status < 2 && NO_SIGNAL) { 1993 if (interruptible)
2019 DECLARE_WAITQUEUE(wait, current); 1994 wait_event_interruptible(command_done, command_status >= 2);
2020 1995 else
2021 add_wait_queue(&command_done, &wait); 1996 wait_event(command_done, command_status >= 2);
2022 for (;;) {
2023 set_current_state(interruptible ?
2024 TASK_INTERRUPTIBLE :
2025 TASK_UNINTERRUPTIBLE);
2026
2027 if (command_status >= 2 || !NO_SIGNAL)
2028 break;
2029
2030 is_alive(__func__, "");
2031 schedule();
2032 }
2033
2034 set_current_state(TASK_RUNNING);
2035 remove_wait_queue(&command_done, &wait);
2036 }
2037 1997
2038 if (command_status < 2) { 1998 if (command_status < 2) {
2039 cancel_activity(); 1999 cancel_activity();
@@ -2223,7 +2183,7 @@ static void redo_format(void)
2223 debugt(__func__, "queue format request"); 2183 debugt(__func__, "queue format request");
2224} 2184}
2225 2185
2226static struct cont_t format_cont = { 2186static const struct cont_t format_cont = {
2227 .interrupt = format_interrupt, 2187 .interrupt = format_interrupt,
2228 .redo = redo_format, 2188 .redo = redo_format,
2229 .error = bad_flp_intr, 2189 .error = bad_flp_intr,
@@ -2583,10 +2543,8 @@ static int make_raw_rw_request(void)
2583 int tracksize; 2543 int tracksize;
2584 int ssize; 2544 int ssize;
2585 2545
2586 if (max_buffer_sectors == 0) { 2546 if (WARN(max_buffer_sectors == 0, "VFS: Block I/O scheduled on unopened device\n"))
2587 pr_info("VFS: Block I/O scheduled on unopened device\n");
2588 return 0; 2547 return 0;
2589 }
2590 2548
2591 set_fdc((long)current_req->rq_disk->private_data); 2549 set_fdc((long)current_req->rq_disk->private_data);
2592 2550
@@ -2921,7 +2879,7 @@ do_request:
2921 return; 2879 return;
2922} 2880}
2923 2881
2924static struct cont_t rw_cont = { 2882static const struct cont_t rw_cont = {
2925 .interrupt = rw_interrupt, 2883 .interrupt = rw_interrupt,
2926 .redo = redo_fd_request, 2884 .redo = redo_fd_request,
2927 .error = bad_flp_intr, 2885 .error = bad_flp_intr,
@@ -2936,19 +2894,16 @@ static void process_fd_request(void)
2936 2894
2937static void do_fd_request(struct request_queue *q) 2895static void do_fd_request(struct request_queue *q)
2938{ 2896{
2939 if (max_buffer_sectors == 0) { 2897 if (WARN(max_buffer_sectors == 0,
2940 pr_info("VFS: %s called on non-open device\n", __func__); 2898 "VFS: %s called on non-open device\n", __func__))
2941 return; 2899 return;
2942 }
2943 2900
2944 if (usage_count == 0) { 2901 if (WARN(atomic_read(&usage_count) == 0,
2945 pr_info("warning: usage count=0, current_req=%p exiting\n", 2902 "warning: usage count=0, current_req=%p sect=%ld type=%x flags=%x\n",
2946 current_req); 2903 current_req, (long)blk_rq_pos(current_req), current_req->cmd_type,
2947 pr_info("sect=%ld type=%x flags=%x\n", 2904 current_req->cmd_flags))
2948 (long)blk_rq_pos(current_req), current_req->cmd_type,
2949 current_req->cmd_flags);
2950 return; 2905 return;
2951 } 2906
2952 if (test_bit(0, &fdc_busy)) { 2907 if (test_bit(0, &fdc_busy)) {
2953 /* fdc busy, this new request will be treated when the 2908 /* fdc busy, this new request will be treated when the
2954 current one is done */ 2909 current one is done */
@@ -2960,7 +2915,7 @@ static void do_fd_request(struct request_queue *q)
2960 is_alive(__func__, ""); 2915 is_alive(__func__, "");
2961} 2916}
2962 2917
2963static struct cont_t poll_cont = { 2918static const struct cont_t poll_cont = {
2964 .interrupt = success_and_wakeup, 2919 .interrupt = success_and_wakeup,
2965 .redo = floppy_ready, 2920 .redo = floppy_ready,
2966 .error = generic_failure, 2921 .error = generic_failure,
@@ -2991,7 +2946,7 @@ static void reset_intr(void)
2991 pr_info("weird, reset interrupt called\n"); 2946 pr_info("weird, reset interrupt called\n");
2992} 2947}
2993 2948
2994static struct cont_t reset_cont = { 2949static const struct cont_t reset_cont = {
2995 .interrupt = reset_intr, 2950 .interrupt = reset_intr,
2996 .redo = success_and_wakeup, 2951 .redo = success_and_wakeup,
2997 .error = generic_failure, 2952 .error = generic_failure,
@@ -3033,7 +2988,7 @@ static inline int fd_copyin(void __user *param, void *address,
3033 return copy_from_user(address, param, size) ? -EFAULT : 0; 2988 return copy_from_user(address, param, size) ? -EFAULT : 0;
3034} 2989}
3035 2990
3036static inline const char *drive_name(int type, int drive) 2991static const char *drive_name(int type, int drive)
3037{ 2992{
3038 struct floppy_struct *floppy; 2993 struct floppy_struct *floppy;
3039 2994
@@ -3096,14 +3051,14 @@ static void raw_cmd_done(int flag)
3096 generic_done(flag); 3051 generic_done(flag);
3097} 3052}
3098 3053
3099static struct cont_t raw_cmd_cont = { 3054static const struct cont_t raw_cmd_cont = {
3100 .interrupt = success_and_wakeup, 3055 .interrupt = success_and_wakeup,
3101 .redo = floppy_start, 3056 .redo = floppy_start,
3102 .error = generic_failure, 3057 .error = generic_failure,
3103 .done = raw_cmd_done 3058 .done = raw_cmd_done
3104}; 3059};
3105 3060
3106static inline int raw_cmd_copyout(int cmd, void __user *param, 3061static int raw_cmd_copyout(int cmd, void __user *param,
3107 struct floppy_raw_cmd *ptr) 3062 struct floppy_raw_cmd *ptr)
3108{ 3063{
3109 int ret; 3064 int ret;
@@ -3148,7 +3103,7 @@ static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3148 } 3103 }
3149} 3104}
3150 3105
3151static inline int raw_cmd_copyin(int cmd, void __user *param, 3106static int raw_cmd_copyin(int cmd, void __user *param,
3152 struct floppy_raw_cmd **rcmd) 3107 struct floppy_raw_cmd **rcmd)
3153{ 3108{
3154 struct floppy_raw_cmd *ptr; 3109 struct floppy_raw_cmd *ptr;
@@ -3266,7 +3221,7 @@ static int invalidate_drive(struct block_device *bdev)
3266 return 0; 3221 return 0;
3267} 3222}
3268 3223
3269static inline int set_geometry(unsigned int cmd, struct floppy_struct *g, 3224static int set_geometry(unsigned int cmd, struct floppy_struct *g,
3270 int drive, int type, struct block_device *bdev) 3225 int drive, int type, struct block_device *bdev)
3271{ 3226{
3272 int cnt; 3227 int cnt;
@@ -3337,7 +3292,7 @@ static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3337} 3292}
3338 3293
3339/* handle obsolete ioctl's */ 3294/* handle obsolete ioctl's */
3340static int ioctl_table[] = { 3295static unsigned int ioctl_table[] = {
3341 FDCLRPRM, 3296 FDCLRPRM,
3342 FDSETPRM, 3297 FDSETPRM,
3343 FDDEFPRM, 3298 FDDEFPRM,
@@ -3365,7 +3320,7 @@ static int ioctl_table[] = {
3365 FDTWADDLE 3320 FDTWADDLE
3366}; 3321};
3367 3322
3368static inline int normalize_ioctl(int *cmd, int *size) 3323static int normalize_ioctl(unsigned int *cmd, int *size)
3369{ 3324{
3370 int i; 3325 int i;
3371 3326
@@ -3417,7 +3372,7 @@ static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3417 return 0; 3372 return 0;
3418} 3373}
3419 3374
3420static int fd_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, 3375static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3421 unsigned long param) 3376 unsigned long param)
3422{ 3377{
3423 int drive = (long)bdev->bd_disk->private_data; 3378 int drive = (long)bdev->bd_disk->private_data;
@@ -3593,6 +3548,18 @@ static int fd_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3593 return 0; 3548 return 0;
3594} 3549}
3595 3550
3551static int fd_ioctl(struct block_device *bdev, fmode_t mode,
3552 unsigned int cmd, unsigned long param)
3553{
3554 int ret;
3555
3556 lock_kernel();
3557 ret = fd_locked_ioctl(bdev, mode, cmd, param);
3558 unlock_kernel();
3559
3560 return ret;
3561}
3562
3596static void __init config_types(void) 3563static void __init config_types(void)
3597{ 3564{
3598 bool has_drive = false; 3565 bool has_drive = false;
@@ -3649,6 +3616,7 @@ static int floppy_release(struct gendisk *disk, fmode_t mode)
3649{ 3616{
3650 int drive = (long)disk->private_data; 3617 int drive = (long)disk->private_data;
3651 3618
3619 lock_kernel();
3652 mutex_lock(&open_lock); 3620 mutex_lock(&open_lock);
3653 if (UDRS->fd_ref < 0) 3621 if (UDRS->fd_ref < 0)
3654 UDRS->fd_ref = 0; 3622 UDRS->fd_ref = 0;
@@ -3659,6 +3627,7 @@ static int floppy_release(struct gendisk *disk, fmode_t mode)
3659 if (!UDRS->fd_ref) 3627 if (!UDRS->fd_ref)
3660 opened_bdev[drive] = NULL; 3628 opened_bdev[drive] = NULL;
3661 mutex_unlock(&open_lock); 3629 mutex_unlock(&open_lock);
3630 unlock_kernel();
3662 3631
3663 return 0; 3632 return 0;
3664} 3633}
@@ -3676,6 +3645,7 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
3676 int res = -EBUSY; 3645 int res = -EBUSY;
3677 char *tmp; 3646 char *tmp;
3678 3647
3648 lock_kernel();
3679 mutex_lock(&open_lock); 3649 mutex_lock(&open_lock);
3680 old_dev = UDRS->fd_device; 3650 old_dev = UDRS->fd_device;
3681 if (opened_bdev[drive] && opened_bdev[drive] != bdev) 3651 if (opened_bdev[drive] && opened_bdev[drive] != bdev)
@@ -3752,6 +3722,7 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
3752 goto out; 3722 goto out;
3753 } 3723 }
3754 mutex_unlock(&open_lock); 3724 mutex_unlock(&open_lock);
3725 unlock_kernel();
3755 return 0; 3726 return 0;
3756out: 3727out:
3757 if (UDRS->fd_ref < 0) 3728 if (UDRS->fd_ref < 0)
@@ -3762,6 +3733,7 @@ out:
3762 opened_bdev[drive] = NULL; 3733 opened_bdev[drive] = NULL;
3763out2: 3734out2:
3764 mutex_unlock(&open_lock); 3735 mutex_unlock(&open_lock);
3736 unlock_kernel();
3765 return res; 3737 return res;
3766} 3738}
3767 3739
@@ -3829,6 +3801,7 @@ static int __floppy_read_block_0(struct block_device *bdev)
3829 bio.bi_size = size; 3801 bio.bi_size = size;
3830 bio.bi_bdev = bdev; 3802 bio.bi_bdev = bdev;
3831 bio.bi_sector = 0; 3803 bio.bi_sector = 0;
3804 bio.bi_flags = BIO_QUIET;
3832 init_completion(&complete); 3805 init_completion(&complete);
3833 bio.bi_private = &complete; 3806 bio.bi_private = &complete;
3834 bio.bi_end_io = floppy_rb0_complete; 3807 bio.bi_end_io = floppy_rb0_complete;
@@ -3857,10 +3830,10 @@ static int floppy_revalidate(struct gendisk *disk)
3857 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) || 3830 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3858 test_bit(FD_VERIFY_BIT, &UDRS->flags) || 3831 test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
3859 test_bit(drive, &fake_change) || NO_GEOM) { 3832 test_bit(drive, &fake_change) || NO_GEOM) {
3860 if (usage_count == 0) { 3833 if (WARN(atomic_read(&usage_count) == 0,
3861 pr_info("VFS: revalidate called on non-open device.\n"); 3834 "VFS: revalidate called on non-open device.\n"))
3862 return -EFAULT; 3835 return -EFAULT;
3863 } 3836
3864 lock_fdc(drive, false); 3837 lock_fdc(drive, false);
3865 cf = (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) || 3838 cf = (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3866 test_bit(FD_VERIFY_BIT, &UDRS->flags)); 3839 test_bit(FD_VERIFY_BIT, &UDRS->flags));
@@ -3893,7 +3866,7 @@ static const struct block_device_operations floppy_fops = {
3893 .owner = THIS_MODULE, 3866 .owner = THIS_MODULE,
3894 .open = floppy_open, 3867 .open = floppy_open,
3895 .release = floppy_release, 3868 .release = floppy_release,
3896 .locked_ioctl = fd_ioctl, 3869 .ioctl = fd_ioctl,
3897 .getgeo = fd_getgeo, 3870 .getgeo = fd_getgeo,
3898 .media_changed = check_floppy_change, 3871 .media_changed = check_floppy_change,
3899 .revalidate_disk = floppy_revalidate, 3872 .revalidate_disk = floppy_revalidate,
@@ -4126,7 +4099,7 @@ static ssize_t floppy_cmos_show(struct device *dev,
4126 return sprintf(buf, "%X\n", UDP->cmos); 4099 return sprintf(buf, "%X\n", UDP->cmos);
4127} 4100}
4128 4101
4129DEVICE_ATTR(cmos, S_IRUGO, floppy_cmos_show, NULL); 4102static DEVICE_ATTR(cmos, S_IRUGO, floppy_cmos_show, NULL);
4130 4103
4131static void floppy_device_release(struct device *dev) 4104static void floppy_device_release(struct device *dev)
4132{ 4105{
@@ -4175,6 +4148,9 @@ static int __init floppy_init(void)
4175 int i, unit, drive; 4148 int i, unit, drive;
4176 int err, dr; 4149 int err, dr;
4177 4150
4151 set_debugt();
4152 interruptjiffies = resultjiffies = jiffies;
4153
4178#if defined(CONFIG_PPC) 4154#if defined(CONFIG_PPC)
4179 if (check_legacy_ioport(FDC1)) 4155 if (check_legacy_ioport(FDC1))
4180 return -ENODEV; 4156 return -ENODEV;
@@ -4353,7 +4329,7 @@ out_unreg_platform_dev:
4353 platform_device_unregister(&floppy_device[drive]); 4329 platform_device_unregister(&floppy_device[drive]);
4354out_flush_work: 4330out_flush_work:
4355 flush_scheduled_work(); 4331 flush_scheduled_work();
4356 if (usage_count) 4332 if (atomic_read(&usage_count))
4357 floppy_release_irq_and_dma(); 4333 floppy_release_irq_and_dma();
4358out_unreg_region: 4334out_unreg_region:
4359 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256); 4335 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
@@ -4370,8 +4346,6 @@ out_put_disk:
4370 return err; 4346 return err;
4371} 4347}
4372 4348
4373static DEFINE_SPINLOCK(floppy_usage_lock);
4374
4375static const struct io_region { 4349static const struct io_region {
4376 int offset; 4350 int offset;
4377 int size; 4351 int size;
@@ -4417,14 +4391,8 @@ static void floppy_release_regions(int fdc)
4417 4391
4418static int floppy_grab_irq_and_dma(void) 4392static int floppy_grab_irq_and_dma(void)
4419{ 4393{
4420 unsigned long flags; 4394 if (atomic_inc_return(&usage_count) > 1)
4421
4422 spin_lock_irqsave(&floppy_usage_lock, flags);
4423 if (usage_count++) {
4424 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4425 return 0; 4395 return 0;
4426 }
4427 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4428 4396
4429 /* 4397 /*
4430 * We might have scheduled a free_irq(), wait it to 4398 * We might have scheduled a free_irq(), wait it to
@@ -4435,9 +4403,7 @@ static int floppy_grab_irq_and_dma(void)
4435 if (fd_request_irq()) { 4403 if (fd_request_irq()) {
4436 DPRINT("Unable to grab IRQ%d for the floppy driver\n", 4404 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4437 FLOPPY_IRQ); 4405 FLOPPY_IRQ);
4438 spin_lock_irqsave(&floppy_usage_lock, flags); 4406 atomic_dec(&usage_count);
4439 usage_count--;
4440 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4441 return -1; 4407 return -1;
4442 } 4408 }
4443 if (fd_request_dma()) { 4409 if (fd_request_dma()) {
@@ -4447,9 +4413,7 @@ static int floppy_grab_irq_and_dma(void)
4447 use_virtual_dma = can_use_virtual_dma = 1; 4413 use_virtual_dma = can_use_virtual_dma = 1;
4448 if (!(can_use_virtual_dma & 1)) { 4414 if (!(can_use_virtual_dma & 1)) {
4449 fd_free_irq(); 4415 fd_free_irq();
4450 spin_lock_irqsave(&floppy_usage_lock, flags); 4416 atomic_dec(&usage_count);
4451 usage_count--;
4452 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4453 return -1; 4417 return -1;
4454 } 4418 }
4455 } 4419 }
@@ -4484,9 +4448,7 @@ cleanup:
4484 fd_free_dma(); 4448 fd_free_dma();
4485 while (--fdc >= 0) 4449 while (--fdc >= 0)
4486 floppy_release_regions(fdc); 4450 floppy_release_regions(fdc);
4487 spin_lock_irqsave(&floppy_usage_lock, flags); 4451 atomic_dec(&usage_count);
4488 usage_count--;
4489 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4490 return -1; 4452 return -1;
4491} 4453}
4492 4454
@@ -4498,14 +4460,10 @@ static void floppy_release_irq_and_dma(void)
4498#endif 4460#endif
4499 long tmpsize; 4461 long tmpsize;
4500 unsigned long tmpaddr; 4462 unsigned long tmpaddr;
4501 unsigned long flags;
4502 4463
4503 spin_lock_irqsave(&floppy_usage_lock, flags); 4464 if (!atomic_dec_and_test(&usage_count))
4504 if (--usage_count) {
4505 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4506 return; 4465 return;
4507 } 4466
4508 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4509 if (irqdma_allocated) { 4467 if (irqdma_allocated) {
4510 fd_disable_dma(); 4468 fd_disable_dma();
4511 fd_free_dma(); 4469 fd_free_dma();
@@ -4598,7 +4556,7 @@ static void __exit floppy_module_exit(void)
4598 del_timer_sync(&fd_timer); 4556 del_timer_sync(&fd_timer);
4599 blk_cleanup_queue(floppy_queue); 4557 blk_cleanup_queue(floppy_queue);
4600 4558
4601 if (usage_count) 4559 if (atomic_read(&usage_count))
4602 floppy_release_irq_and_dma(); 4560 floppy_release_irq_and_dma();
4603 4561
4604 /* eject disk, if any */ 4562 /* eject disk, if any */
diff --git a/drivers/block/hd.c b/drivers/block/hd.c
index 81c78b3ce2d..30ec6b37424 100644
--- a/drivers/block/hd.c
+++ b/drivers/block/hd.c
@@ -627,7 +627,7 @@ repeat:
627 req_data_dir(req) == READ ? "read" : "writ", 627 req_data_dir(req) == READ ? "read" : "writ",
628 cyl, head, sec, nsect, req->buffer); 628 cyl, head, sec, nsect, req->buffer);
629#endif 629#endif
630 if (blk_fs_request(req)) { 630 if (req->cmd_type == REQ_TYPE_FS) {
631 switch (rq_data_dir(req)) { 631 switch (rq_data_dir(req)) {
632 case READ: 632 case READ:
633 hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_READ, 633 hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_READ,
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 6120922f459..f3c636d2371 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -67,6 +67,7 @@
67#include <linux/compat.h> 67#include <linux/compat.h>
68#include <linux/suspend.h> 68#include <linux/suspend.h>
69#include <linux/freezer.h> 69#include <linux/freezer.h>
70#include <linux/smp_lock.h>
70#include <linux/writeback.h> 71#include <linux/writeback.h>
71#include <linux/buffer_head.h> /* for invalidate_bdev() */ 72#include <linux/buffer_head.h> /* for invalidate_bdev() */
72#include <linux/completion.h> 73#include <linux/completion.h>
@@ -476,7 +477,7 @@ static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
476 pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset; 477 pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
477 478
478 if (bio_rw(bio) == WRITE) { 479 if (bio_rw(bio) == WRITE) {
479 bool barrier = bio_rw_flagged(bio, BIO_RW_BARRIER); 480 bool barrier = (bio->bi_rw & REQ_HARDBARRIER);
480 struct file *file = lo->lo_backing_file; 481 struct file *file = lo->lo_backing_file;
481 482
482 if (barrier) { 483 if (barrier) {
@@ -831,7 +832,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
831 lo->lo_queue->unplug_fn = loop_unplug; 832 lo->lo_queue->unplug_fn = loop_unplug;
832 833
833 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 834 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
834 blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_DRAIN, NULL); 835 blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_DRAIN);
835 836
836 set_capacity(lo->lo_disk, size); 837 set_capacity(lo->lo_disk, size);
837 bd_set_size(bdev, size << 9); 838 bd_set_size(bdev, size << 9);
@@ -1408,9 +1409,11 @@ static int lo_open(struct block_device *bdev, fmode_t mode)
1408{ 1409{
1409 struct loop_device *lo = bdev->bd_disk->private_data; 1410 struct loop_device *lo = bdev->bd_disk->private_data;
1410 1411
1412 lock_kernel();
1411 mutex_lock(&lo->lo_ctl_mutex); 1413 mutex_lock(&lo->lo_ctl_mutex);
1412 lo->lo_refcnt++; 1414 lo->lo_refcnt++;
1413 mutex_unlock(&lo->lo_ctl_mutex); 1415 mutex_unlock(&lo->lo_ctl_mutex);
1416 unlock_kernel();
1414 1417
1415 return 0; 1418 return 0;
1416} 1419}
@@ -1420,6 +1423,7 @@ static int lo_release(struct gendisk *disk, fmode_t mode)
1420 struct loop_device *lo = disk->private_data; 1423 struct loop_device *lo = disk->private_data;
1421 int err; 1424 int err;
1422 1425
1426 lock_kernel();
1423 mutex_lock(&lo->lo_ctl_mutex); 1427 mutex_lock(&lo->lo_ctl_mutex);
1424 1428
1425 if (--lo->lo_refcnt) 1429 if (--lo->lo_refcnt)
@@ -1444,6 +1448,7 @@ static int lo_release(struct gendisk *disk, fmode_t mode)
1444out: 1448out:
1445 mutex_unlock(&lo->lo_ctl_mutex); 1449 mutex_unlock(&lo->lo_ctl_mutex);
1446out_unlocked: 1450out_unlocked:
1451 lock_kernel();
1447 return 0; 1452 return 0;
1448} 1453}
1449 1454
diff --git a/drivers/block/mg_disk.c b/drivers/block/mg_disk.c
index 28db925dbda..b82c5ce5e9d 100644
--- a/drivers/block/mg_disk.c
+++ b/drivers/block/mg_disk.c
@@ -670,7 +670,7 @@ static void mg_request_poll(struct request_queue *q)
670 break; 670 break;
671 } 671 }
672 672
673 if (unlikely(!blk_fs_request(host->req))) { 673 if (unlikely(host->req->cmd_type != REQ_TYPE_FS)) {
674 mg_end_request_cur(host, -EIO); 674 mg_end_request_cur(host, -EIO);
675 continue; 675 continue;
676 } 676 }
@@ -756,7 +756,7 @@ static void mg_request(struct request_queue *q)
756 continue; 756 continue;
757 } 757 }
758 758
759 if (unlikely(!blk_fs_request(req))) { 759 if (unlikely(req->cmd_type != REQ_TYPE_FS)) {
760 mg_end_request_cur(host, -EIO); 760 mg_end_request_cur(host, -EIO);
761 continue; 761 continue;
762 } 762 }
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 16c3c8613cd..0daa422aa28 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -24,6 +24,7 @@
24#include <linux/errno.h> 24#include <linux/errno.h>
25#include <linux/file.h> 25#include <linux/file.h>
26#include <linux/ioctl.h> 26#include <linux/ioctl.h>
27#include <linux/smp_lock.h>
27#include <linux/compiler.h> 28#include <linux/compiler.h>
28#include <linux/err.h> 29#include <linux/err.h>
29#include <linux/kernel.h> 30#include <linux/kernel.h>
@@ -448,7 +449,7 @@ static void nbd_clear_que(struct nbd_device *lo)
448 449
449static void nbd_handle_req(struct nbd_device *lo, struct request *req) 450static void nbd_handle_req(struct nbd_device *lo, struct request *req)
450{ 451{
451 if (!blk_fs_request(req)) 452 if (req->cmd_type != REQ_TYPE_FS)
452 goto error_out; 453 goto error_out;
453 454
454 nbd_cmd(req) = NBD_CMD_READ; 455 nbd_cmd(req) = NBD_CMD_READ;
@@ -716,9 +717,11 @@ static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
716 dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n", 717 dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
717 lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg); 718 lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
718 719
720 lock_kernel();
719 mutex_lock(&lo->tx_lock); 721 mutex_lock(&lo->tx_lock);
720 error = __nbd_ioctl(bdev, lo, cmd, arg); 722 error = __nbd_ioctl(bdev, lo, cmd, arg);
721 mutex_unlock(&lo->tx_lock); 723 mutex_unlock(&lo->tx_lock);
724 unlock_kernel();
722 725
723 return error; 726 return error;
724} 727}
@@ -726,7 +729,7 @@ static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
726static const struct block_device_operations nbd_fops = 729static const struct block_device_operations nbd_fops =
727{ 730{
728 .owner = THIS_MODULE, 731 .owner = THIS_MODULE,
729 .locked_ioctl = nbd_ioctl, 732 .ioctl = nbd_ioctl,
730}; 733};
731 734
732/* 735/*
diff --git a/drivers/block/osdblk.c b/drivers/block/osdblk.c
index 6cd8b705b11..2284b4f05c6 100644
--- a/drivers/block/osdblk.c
+++ b/drivers/block/osdblk.c
@@ -310,7 +310,8 @@ static void osdblk_rq_fn(struct request_queue *q)
310 break; 310 break;
311 311
312 /* filter out block requests we don't understand */ 312 /* filter out block requests we don't understand */
313 if (!blk_fs_request(rq) && !blk_barrier_rq(rq)) { 313 if (rq->cmd_type != REQ_TYPE_FS &&
314 !(rq->cmd_flags & REQ_HARDBARRIER)) {
314 blk_end_request_all(rq, 0); 315 blk_end_request_all(rq, 0);
315 continue; 316 continue;
316 } 317 }
@@ -322,7 +323,7 @@ static void osdblk_rq_fn(struct request_queue *q)
322 * driver-specific, etc. 323 * driver-specific, etc.
323 */ 324 */
324 325
325 do_flush = (rq->special == (void *) 0xdeadbeefUL); 326 do_flush = rq->cmd_flags & REQ_FLUSH;
326 do_write = (rq_data_dir(rq) == WRITE); 327 do_write = (rq_data_dir(rq) == WRITE);
327 328
328 if (!do_flush) { /* osd_flush does not use a bio */ 329 if (!do_flush) { /* osd_flush does not use a bio */
@@ -379,14 +380,6 @@ static void osdblk_rq_fn(struct request_queue *q)
379 } 380 }
380} 381}
381 382
382static void osdblk_prepare_flush(struct request_queue *q, struct request *rq)
383{
384 /* add driver-specific marker, to indicate that this request
385 * is a flush command
386 */
387 rq->special = (void *) 0xdeadbeefUL;
388}
389
390static void osdblk_free_disk(struct osdblk_device *osdev) 383static void osdblk_free_disk(struct osdblk_device *osdev)
391{ 384{
392 struct gendisk *disk = osdev->disk; 385 struct gendisk *disk = osdev->disk;
@@ -446,7 +439,7 @@ static int osdblk_init_disk(struct osdblk_device *osdev)
446 blk_queue_stack_limits(q, osd_request_queue(osdev->osd)); 439 blk_queue_stack_limits(q, osd_request_queue(osdev->osd));
447 440
448 blk_queue_prep_rq(q, blk_queue_start_tag); 441 blk_queue_prep_rq(q, blk_queue_start_tag);
449 blk_queue_ordered(q, QUEUE_ORDERED_DRAIN_FLUSH, osdblk_prepare_flush); 442 blk_queue_ordered(q, QUEUE_ORDERED_DRAIN_FLUSH);
450 443
451 disk->queue = q; 444 disk->queue = q;
452 445
diff --git a/drivers/block/paride/pcd.c b/drivers/block/paride/pcd.c
index 71acf4e5335..76f8565e1e8 100644
--- a/drivers/block/paride/pcd.c
+++ b/drivers/block/paride/pcd.c
@@ -138,6 +138,7 @@ enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY};
138#include <linux/cdrom.h> 138#include <linux/cdrom.h>
139#include <linux/spinlock.h> 139#include <linux/spinlock.h>
140#include <linux/blkdev.h> 140#include <linux/blkdev.h>
141#include <linux/smp_lock.h>
141#include <asm/uaccess.h> 142#include <asm/uaccess.h>
142 143
143static DEFINE_SPINLOCK(pcd_lock); 144static DEFINE_SPINLOCK(pcd_lock);
@@ -224,13 +225,21 @@ static char *pcd_buf; /* buffer for request in progress */
224static int pcd_block_open(struct block_device *bdev, fmode_t mode) 225static int pcd_block_open(struct block_device *bdev, fmode_t mode)
225{ 226{
226 struct pcd_unit *cd = bdev->bd_disk->private_data; 227 struct pcd_unit *cd = bdev->bd_disk->private_data;
227 return cdrom_open(&cd->info, bdev, mode); 228 int ret;
229
230 lock_kernel();
231 ret = cdrom_open(&cd->info, bdev, mode);
232 unlock_kernel();
233
234 return ret;
228} 235}
229 236
230static int pcd_block_release(struct gendisk *disk, fmode_t mode) 237static int pcd_block_release(struct gendisk *disk, fmode_t mode)
231{ 238{
232 struct pcd_unit *cd = disk->private_data; 239 struct pcd_unit *cd = disk->private_data;
240 lock_kernel();
233 cdrom_release(&cd->info, mode); 241 cdrom_release(&cd->info, mode);
242 unlock_kernel();
234 return 0; 243 return 0;
235} 244}
236 245
@@ -238,7 +247,13 @@ static int pcd_block_ioctl(struct block_device *bdev, fmode_t mode,
238 unsigned cmd, unsigned long arg) 247 unsigned cmd, unsigned long arg)
239{ 248{
240 struct pcd_unit *cd = bdev->bd_disk->private_data; 249 struct pcd_unit *cd = bdev->bd_disk->private_data;
241 return cdrom_ioctl(&cd->info, bdev, mode, cmd, arg); 250 int ret;
251
252 lock_kernel();
253 ret = cdrom_ioctl(&cd->info, bdev, mode, cmd, arg);
254 unlock_kernel();
255
256 return ret;
242} 257}
243 258
244static int pcd_block_media_changed(struct gendisk *disk) 259static int pcd_block_media_changed(struct gendisk *disk)
@@ -251,7 +266,7 @@ static const struct block_device_operations pcd_bdops = {
251 .owner = THIS_MODULE, 266 .owner = THIS_MODULE,
252 .open = pcd_block_open, 267 .open = pcd_block_open,
253 .release = pcd_block_release, 268 .release = pcd_block_release,
254 .locked_ioctl = pcd_block_ioctl, 269 .ioctl = pcd_block_ioctl,
255 .media_changed = pcd_block_media_changed, 270 .media_changed = pcd_block_media_changed,
256}; 271};
257 272
diff --git a/drivers/block/paride/pd.c b/drivers/block/paride/pd.c
index c1e5cd029b2..985f0d4f1d1 100644
--- a/drivers/block/paride/pd.c
+++ b/drivers/block/paride/pd.c
@@ -153,6 +153,7 @@ enum {D_PRT, D_PRO, D_UNI, D_MOD, D_GEO, D_SBY, D_DLY, D_SLV};
153#include <linux/blkdev.h> 153#include <linux/blkdev.h>
154#include <linux/blkpg.h> 154#include <linux/blkpg.h>
155#include <linux/kernel.h> 155#include <linux/kernel.h>
156#include <linux/smp_lock.h>
156#include <asm/uaccess.h> 157#include <asm/uaccess.h>
157#include <linux/workqueue.h> 158#include <linux/workqueue.h>
158 159
@@ -439,7 +440,7 @@ static char *pd_buf; /* buffer for request in progress */
439 440
440static enum action do_pd_io_start(void) 441static enum action do_pd_io_start(void)
441{ 442{
442 if (blk_special_request(pd_req)) { 443 if (pd_req->cmd_type == REQ_TYPE_SPECIAL) {
443 phase = pd_special; 444 phase = pd_special;
444 return pd_special(); 445 return pd_special();
445 } 446 }
@@ -735,12 +736,14 @@ static int pd_open(struct block_device *bdev, fmode_t mode)
735{ 736{
736 struct pd_unit *disk = bdev->bd_disk->private_data; 737 struct pd_unit *disk = bdev->bd_disk->private_data;
737 738
739 lock_kernel();
738 disk->access++; 740 disk->access++;
739 741
740 if (disk->removable) { 742 if (disk->removable) {
741 pd_special_command(disk, pd_media_check); 743 pd_special_command(disk, pd_media_check);
742 pd_special_command(disk, pd_door_lock); 744 pd_special_command(disk, pd_door_lock);
743 } 745 }
746 unlock_kernel();
744 return 0; 747 return 0;
745} 748}
746 749
@@ -768,8 +771,10 @@ static int pd_ioctl(struct block_device *bdev, fmode_t mode,
768 771
769 switch (cmd) { 772 switch (cmd) {
770 case CDROMEJECT: 773 case CDROMEJECT:
774 lock_kernel();
771 if (disk->access == 1) 775 if (disk->access == 1)
772 pd_special_command(disk, pd_eject); 776 pd_special_command(disk, pd_eject);
777 unlock_kernel();
773 return 0; 778 return 0;
774 default: 779 default:
775 return -EINVAL; 780 return -EINVAL;
@@ -780,8 +785,10 @@ static int pd_release(struct gendisk *p, fmode_t mode)
780{ 785{
781 struct pd_unit *disk = p->private_data; 786 struct pd_unit *disk = p->private_data;
782 787
788 lock_kernel();
783 if (!--disk->access && disk->removable) 789 if (!--disk->access && disk->removable)
784 pd_special_command(disk, pd_door_unlock); 790 pd_special_command(disk, pd_door_unlock);
791 unlock_kernel();
785 792
786 return 0; 793 return 0;
787} 794}
@@ -812,7 +819,7 @@ static const struct block_device_operations pd_fops = {
812 .owner = THIS_MODULE, 819 .owner = THIS_MODULE,
813 .open = pd_open, 820 .open = pd_open,
814 .release = pd_release, 821 .release = pd_release,
815 .locked_ioctl = pd_ioctl, 822 .ioctl = pd_ioctl,
816 .getgeo = pd_getgeo, 823 .getgeo = pd_getgeo,
817 .media_changed = pd_check_media, 824 .media_changed = pd_check_media,
818 .revalidate_disk= pd_revalidate 825 .revalidate_disk= pd_revalidate
diff --git a/drivers/block/paride/pf.c b/drivers/block/paride/pf.c
index c059aab3006..4457b494882 100644
--- a/drivers/block/paride/pf.c
+++ b/drivers/block/paride/pf.c
@@ -152,6 +152,7 @@ enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_LUN, D_DLY};
152#include <linux/spinlock.h> 152#include <linux/spinlock.h>
153#include <linux/blkdev.h> 153#include <linux/blkdev.h>
154#include <linux/blkpg.h> 154#include <linux/blkpg.h>
155#include <linux/smp_lock.h>
155#include <asm/uaccess.h> 156#include <asm/uaccess.h>
156 157
157static DEFINE_SPINLOCK(pf_spin_lock); 158static DEFINE_SPINLOCK(pf_spin_lock);
@@ -266,7 +267,7 @@ static const struct block_device_operations pf_fops = {
266 .owner = THIS_MODULE, 267 .owner = THIS_MODULE,
267 .open = pf_open, 268 .open = pf_open,
268 .release = pf_release, 269 .release = pf_release,
269 .locked_ioctl = pf_ioctl, 270 .ioctl = pf_ioctl,
270 .getgeo = pf_getgeo, 271 .getgeo = pf_getgeo,
271 .media_changed = pf_check_media, 272 .media_changed = pf_check_media,
272}; 273};
@@ -299,20 +300,26 @@ static void __init pf_init_units(void)
299static int pf_open(struct block_device *bdev, fmode_t mode) 300static int pf_open(struct block_device *bdev, fmode_t mode)
300{ 301{
301 struct pf_unit *pf = bdev->bd_disk->private_data; 302 struct pf_unit *pf = bdev->bd_disk->private_data;
303 int ret;
302 304
305 lock_kernel();
303 pf_identify(pf); 306 pf_identify(pf);
304 307
308 ret = -ENODEV;
305 if (pf->media_status == PF_NM) 309 if (pf->media_status == PF_NM)
306 return -ENODEV; 310 goto out;
307 311
312 ret = -EROFS;
308 if ((pf->media_status == PF_RO) && (mode & FMODE_WRITE)) 313 if ((pf->media_status == PF_RO) && (mode & FMODE_WRITE))
309 return -EROFS; 314 goto out;
310 315
316 ret = 0;
311 pf->access++; 317 pf->access++;
312 if (pf->removable) 318 if (pf->removable)
313 pf_lock(pf, 1); 319 pf_lock(pf, 1);
314 320out:
315 return 0; 321 unlock_kernel();
322 return ret;
316} 323}
317 324
318static int pf_getgeo(struct block_device *bdev, struct hd_geometry *geo) 325static int pf_getgeo(struct block_device *bdev, struct hd_geometry *geo)
@@ -342,7 +349,10 @@ static int pf_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, u
342 349
343 if (pf->access != 1) 350 if (pf->access != 1)
344 return -EBUSY; 351 return -EBUSY;
352 lock_kernel();
345 pf_eject(pf); 353 pf_eject(pf);
354 unlock_kernel();
355
346 return 0; 356 return 0;
347} 357}
348 358
@@ -350,14 +360,18 @@ static int pf_release(struct gendisk *disk, fmode_t mode)
350{ 360{
351 struct pf_unit *pf = disk->private_data; 361 struct pf_unit *pf = disk->private_data;
352 362
353 if (pf->access <= 0) 363 lock_kernel();
364 if (pf->access <= 0) {
365 unlock_kernel();
354 return -EINVAL; 366 return -EINVAL;
367 }
355 368
356 pf->access--; 369 pf->access--;
357 370
358 if (!pf->access && pf->removable) 371 if (!pf->access && pf->removable)
359 pf_lock(pf, 0); 372 pf_lock(pf, 0);
360 373
374 unlock_kernel();
361 return 0; 375 return 0;
362 376
363} 377}
diff --git a/drivers/block/pktcdvd.c b/drivers/block/pktcdvd.c
index 8a549db2aa7..b1cbeb59bb7 100644
--- a/drivers/block/pktcdvd.c
+++ b/drivers/block/pktcdvd.c
@@ -57,6 +57,7 @@
57#include <linux/seq_file.h> 57#include <linux/seq_file.h>
58#include <linux/miscdevice.h> 58#include <linux/miscdevice.h>
59#include <linux/freezer.h> 59#include <linux/freezer.h>
60#include <linux/smp_lock.h>
60#include <linux/mutex.h> 61#include <linux/mutex.h>
61#include <linux/slab.h> 62#include <linux/slab.h>
62#include <scsi/scsi_cmnd.h> 63#include <scsi/scsi_cmnd.h>
@@ -1221,7 +1222,7 @@ static int pkt_start_recovery(struct packet_data *pkt)
1221 pkt->bio->bi_flags = 1 << BIO_UPTODATE; 1222 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1222 pkt->bio->bi_idx = 0; 1223 pkt->bio->bi_idx = 0;
1223 1224
1224 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW)); 1225 BUG_ON(pkt->bio->bi_rw != REQ_WRITE);
1225 BUG_ON(pkt->bio->bi_vcnt != pkt->frames); 1226 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1226 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE); 1227 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1227 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write); 1228 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
@@ -2382,6 +2383,7 @@ static int pkt_open(struct block_device *bdev, fmode_t mode)
2382 2383
2383 VPRINTK(DRIVER_NAME": entering open\n"); 2384 VPRINTK(DRIVER_NAME": entering open\n");
2384 2385
2386 lock_kernel();
2385 mutex_lock(&ctl_mutex); 2387 mutex_lock(&ctl_mutex);
2386 pd = pkt_find_dev_from_minor(MINOR(bdev->bd_dev)); 2388 pd = pkt_find_dev_from_minor(MINOR(bdev->bd_dev));
2387 if (!pd) { 2389 if (!pd) {
@@ -2409,6 +2411,7 @@ static int pkt_open(struct block_device *bdev, fmode_t mode)
2409 } 2411 }
2410 2412
2411 mutex_unlock(&ctl_mutex); 2413 mutex_unlock(&ctl_mutex);
2414 unlock_kernel();
2412 return 0; 2415 return 0;
2413 2416
2414out_dec: 2417out_dec:
@@ -2416,6 +2419,7 @@ out_dec:
2416out: 2419out:
2417 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret); 2420 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
2418 mutex_unlock(&ctl_mutex); 2421 mutex_unlock(&ctl_mutex);
2422 unlock_kernel();
2419 return ret; 2423 return ret;
2420} 2424}
2421 2425
@@ -2424,6 +2428,7 @@ static int pkt_close(struct gendisk *disk, fmode_t mode)
2424 struct pktcdvd_device *pd = disk->private_data; 2428 struct pktcdvd_device *pd = disk->private_data;
2425 int ret = 0; 2429 int ret = 0;
2426 2430
2431 lock_kernel();
2427 mutex_lock(&ctl_mutex); 2432 mutex_lock(&ctl_mutex);
2428 pd->refcnt--; 2433 pd->refcnt--;
2429 BUG_ON(pd->refcnt < 0); 2434 BUG_ON(pd->refcnt < 0);
@@ -2432,6 +2437,7 @@ static int pkt_close(struct gendisk *disk, fmode_t mode)
2432 pkt_release_dev(pd, flush); 2437 pkt_release_dev(pd, flush);
2433 } 2438 }
2434 mutex_unlock(&ctl_mutex); 2439 mutex_unlock(&ctl_mutex);
2440 unlock_kernel();
2435 return ret; 2441 return ret;
2436} 2442}
2437 2443
@@ -2762,10 +2768,12 @@ out_mem:
2762static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg) 2768static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
2763{ 2769{
2764 struct pktcdvd_device *pd = bdev->bd_disk->private_data; 2770 struct pktcdvd_device *pd = bdev->bd_disk->private_data;
2771 int ret;
2765 2772
2766 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, 2773 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd,
2767 MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev)); 2774 MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
2768 2775
2776 lock_kernel();
2769 switch (cmd) { 2777 switch (cmd) {
2770 case CDROMEJECT: 2778 case CDROMEJECT:
2771 /* 2779 /*
@@ -2783,14 +2791,16 @@ static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
2783 case CDROM_LAST_WRITTEN: 2791 case CDROM_LAST_WRITTEN:
2784 case CDROM_SEND_PACKET: 2792 case CDROM_SEND_PACKET:
2785 case SCSI_IOCTL_SEND_COMMAND: 2793 case SCSI_IOCTL_SEND_COMMAND:
2786 return __blkdev_driver_ioctl(pd->bdev, mode, cmd, arg); 2794 ret = __blkdev_driver_ioctl(pd->bdev, mode, cmd, arg);
2795 break;
2787 2796
2788 default: 2797 default:
2789 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd); 2798 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
2790 return -ENOTTY; 2799 ret = -ENOTTY;
2791 } 2800 }
2801 unlock_kernel();
2792 2802
2793 return 0; 2803 return ret;
2794} 2804}
2795 2805
2796static int pkt_media_changed(struct gendisk *disk) 2806static int pkt_media_changed(struct gendisk *disk)
@@ -2812,7 +2822,7 @@ static const struct block_device_operations pktcdvd_ops = {
2812 .owner = THIS_MODULE, 2822 .owner = THIS_MODULE,
2813 .open = pkt_open, 2823 .open = pkt_open,
2814 .release = pkt_close, 2824 .release = pkt_close,
2815 .locked_ioctl = pkt_ioctl, 2825 .ioctl = pkt_ioctl,
2816 .media_changed = pkt_media_changed, 2826 .media_changed = pkt_media_changed,
2817}; 2827};
2818 2828
diff --git a/drivers/block/ps3disk.c b/drivers/block/ps3disk.c
index 3b419e3fffa..e9da874d041 100644
--- a/drivers/block/ps3disk.c
+++ b/drivers/block/ps3disk.c
@@ -196,13 +196,12 @@ static void ps3disk_do_request(struct ps3_storage_device *dev,
196 dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__); 196 dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
197 197
198 while ((req = blk_fetch_request(q))) { 198 while ((req = blk_fetch_request(q))) {
199 if (blk_fs_request(req)) { 199 if (req->cmd_flags & REQ_FLUSH) {
200 if (ps3disk_submit_request_sg(dev, req))
201 break;
202 } else if (req->cmd_type == REQ_TYPE_LINUX_BLOCK &&
203 req->cmd[0] == REQ_LB_OP_FLUSH) {
204 if (ps3disk_submit_flush_request(dev, req)) 200 if (ps3disk_submit_flush_request(dev, req))
205 break; 201 break;
202 } else if (req->cmd_type == REQ_TYPE_FS) {
203 if (ps3disk_submit_request_sg(dev, req))
204 break;
206 } else { 205 } else {
207 blk_dump_rq_flags(req, DEVICE_NAME " bad request"); 206 blk_dump_rq_flags(req, DEVICE_NAME " bad request");
208 __blk_end_request_all(req, -EIO); 207 __blk_end_request_all(req, -EIO);
@@ -257,8 +256,7 @@ static irqreturn_t ps3disk_interrupt(int irq, void *data)
257 return IRQ_HANDLED; 256 return IRQ_HANDLED;
258 } 257 }
259 258
260 if (req->cmd_type == REQ_TYPE_LINUX_BLOCK && 259 if (req->cmd_flags & REQ_FLUSH) {
261 req->cmd[0] == REQ_LB_OP_FLUSH) {
262 read = 0; 260 read = 0;
263 op = "flush"; 261 op = "flush";
264 } else { 262 } else {
@@ -398,16 +396,6 @@ static int ps3disk_identify(struct ps3_storage_device *dev)
398 return 0; 396 return 0;
399} 397}
400 398
401static void ps3disk_prepare_flush(struct request_queue *q, struct request *req)
402{
403 struct ps3_storage_device *dev = q->queuedata;
404
405 dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
406
407 req->cmd_type = REQ_TYPE_LINUX_BLOCK;
408 req->cmd[0] = REQ_LB_OP_FLUSH;
409}
410
411static unsigned long ps3disk_mask; 399static unsigned long ps3disk_mask;
412 400
413static DEFINE_MUTEX(ps3disk_mask_mutex); 401static DEFINE_MUTEX(ps3disk_mask_mutex);
@@ -480,8 +468,7 @@ static int __devinit ps3disk_probe(struct ps3_system_bus_device *_dev)
480 blk_queue_dma_alignment(queue, dev->blk_size-1); 468 blk_queue_dma_alignment(queue, dev->blk_size-1);
481 blk_queue_logical_block_size(queue, dev->blk_size); 469 blk_queue_logical_block_size(queue, dev->blk_size);
482 470
483 blk_queue_ordered(queue, QUEUE_ORDERED_DRAIN_FLUSH, 471 blk_queue_ordered(queue, QUEUE_ORDERED_DRAIN_FLUSH);
484 ps3disk_prepare_flush);
485 472
486 blk_queue_max_segments(queue, -1); 473 blk_queue_max_segments(queue, -1);
487 blk_queue_max_segment_size(queue, dev->bounce_size); 474 blk_queue_max_segment_size(queue, dev->bounce_size);
diff --git a/drivers/block/swim.c b/drivers/block/swim.c
index e463657569f..2e46815876d 100644
--- a/drivers/block/swim.c
+++ b/drivers/block/swim.c
@@ -20,6 +20,7 @@
20#include <linux/fd.h> 20#include <linux/fd.h>
21#include <linux/slab.h> 21#include <linux/slab.h>
22#include <linux/blkdev.h> 22#include <linux/blkdev.h>
23#include <linux/smp_lock.h>
23#include <linux/hdreg.h> 24#include <linux/hdreg.h>
24#include <linux/kernel.h> 25#include <linux/kernel.h>
25#include <linux/delay.h> 26#include <linux/delay.h>
@@ -661,11 +662,23 @@ out:
661 return err; 662 return err;
662} 663}
663 664
665static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
666{
667 int ret;
668
669 lock_kernel();
670 ret = floppy_open(bdev, mode);
671 unlock_kernel();
672
673 return ret;
674}
675
664static int floppy_release(struct gendisk *disk, fmode_t mode) 676static int floppy_release(struct gendisk *disk, fmode_t mode)
665{ 677{
666 struct floppy_state *fs = disk->private_data; 678 struct floppy_state *fs = disk->private_data;
667 struct swim __iomem *base = fs->swd->base; 679 struct swim __iomem *base = fs->swd->base;
668 680
681 lock_kernel();
669 if (fs->ref_count < 0) 682 if (fs->ref_count < 0)
670 fs->ref_count = 0; 683 fs->ref_count = 0;
671 else if (fs->ref_count > 0) 684 else if (fs->ref_count > 0)
@@ -673,6 +686,7 @@ static int floppy_release(struct gendisk *disk, fmode_t mode)
673 686
674 if (fs->ref_count == 0) 687 if (fs->ref_count == 0)
675 swim_motor(base, OFF); 688 swim_motor(base, OFF);
689 unlock_kernel();
676 690
677 return 0; 691 return 0;
678} 692}
@@ -690,7 +704,9 @@ static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
690 case FDEJECT: 704 case FDEJECT:
691 if (fs->ref_count != 1) 705 if (fs->ref_count != 1)
692 return -EBUSY; 706 return -EBUSY;
707 lock_kernel();
693 err = floppy_eject(fs); 708 err = floppy_eject(fs);
709 unlock_kernel();
694 return err; 710 return err;
695 711
696 case FDGETPRM: 712 case FDGETPRM:
@@ -751,9 +767,9 @@ static int floppy_revalidate(struct gendisk *disk)
751 767
752static const struct block_device_operations floppy_fops = { 768static const struct block_device_operations floppy_fops = {
753 .owner = THIS_MODULE, 769 .owner = THIS_MODULE,
754 .open = floppy_open, 770 .open = floppy_unlocked_open,
755 .release = floppy_release, 771 .release = floppy_release,
756 .locked_ioctl = floppy_ioctl, 772 .ioctl = floppy_ioctl,
757 .getgeo = floppy_getgeo, 773 .getgeo = floppy_getgeo,
758 .media_changed = floppy_check_change, 774 .media_changed = floppy_check_change,
759 .revalidate_disk = floppy_revalidate, 775 .revalidate_disk = floppy_revalidate,
diff --git a/drivers/block/swim3.c b/drivers/block/swim3.c
index ed6fb91123a..cc6a3864822 100644
--- a/drivers/block/swim3.c
+++ b/drivers/block/swim3.c
@@ -25,6 +25,7 @@
25#include <linux/ioctl.h> 25#include <linux/ioctl.h>
26#include <linux/blkdev.h> 26#include <linux/blkdev.h>
27#include <linux/interrupt.h> 27#include <linux/interrupt.h>
28#include <linux/smp_lock.h>
28#include <linux/module.h> 29#include <linux/module.h>
29#include <linux/spinlock.h> 30#include <linux/spinlock.h>
30#include <asm/io.h> 31#include <asm/io.h>
@@ -839,7 +840,7 @@ static int fd_eject(struct floppy_state *fs)
839static struct floppy_struct floppy_type = 840static struct floppy_struct floppy_type =
840 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */ 841 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */
841 842
842static int floppy_ioctl(struct block_device *bdev, fmode_t mode, 843static int floppy_locked_ioctl(struct block_device *bdev, fmode_t mode,
843 unsigned int cmd, unsigned long param) 844 unsigned int cmd, unsigned long param)
844{ 845{
845 struct floppy_state *fs = bdev->bd_disk->private_data; 846 struct floppy_state *fs = bdev->bd_disk->private_data;
@@ -867,6 +868,18 @@ static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
867 return -ENOTTY; 868 return -ENOTTY;
868} 869}
869 870
871static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
872 unsigned int cmd, unsigned long param)
873{
874 int ret;
875
876 lock_kernel();
877 ret = floppy_locked_ioctl(bdev, mode, cmd, param);
878 unlock_kernel();
879
880 return ret;
881}
882
870static int floppy_open(struct block_device *bdev, fmode_t mode) 883static int floppy_open(struct block_device *bdev, fmode_t mode)
871{ 884{
872 struct floppy_state *fs = bdev->bd_disk->private_data; 885 struct floppy_state *fs = bdev->bd_disk->private_data;
@@ -936,15 +949,28 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
936 return 0; 949 return 0;
937} 950}
938 951
952static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
953{
954 int ret;
955
956 lock_kernel();
957 ret = floppy_open(bdev, mode);
958 unlock_kernel();
959
960 return ret;
961}
962
939static int floppy_release(struct gendisk *disk, fmode_t mode) 963static int floppy_release(struct gendisk *disk, fmode_t mode)
940{ 964{
941 struct floppy_state *fs = disk->private_data; 965 struct floppy_state *fs = disk->private_data;
942 struct swim3 __iomem *sw = fs->swim3; 966 struct swim3 __iomem *sw = fs->swim3;
967 lock_kernel();
943 if (fs->ref_count > 0 && --fs->ref_count == 0) { 968 if (fs->ref_count > 0 && --fs->ref_count == 0) {
944 swim3_action(fs, MOTOR_OFF); 969 swim3_action(fs, MOTOR_OFF);
945 out_8(&sw->control_bic, 0xff); 970 out_8(&sw->control_bic, 0xff);
946 swim3_select(fs, RELAX); 971 swim3_select(fs, RELAX);
947 } 972 }
973 unlock_kernel();
948 return 0; 974 return 0;
949} 975}
950 976
@@ -995,9 +1021,9 @@ static int floppy_revalidate(struct gendisk *disk)
995} 1021}
996 1022
997static const struct block_device_operations floppy_fops = { 1023static const struct block_device_operations floppy_fops = {
998 .open = floppy_open, 1024 .open = floppy_unlocked_open,
999 .release = floppy_release, 1025 .release = floppy_release,
1000 .locked_ioctl = floppy_ioctl, 1026 .ioctl = floppy_ioctl,
1001 .media_changed = floppy_check_change, 1027 .media_changed = floppy_check_change,
1002 .revalidate_disk= floppy_revalidate, 1028 .revalidate_disk= floppy_revalidate,
1003}; 1029};
diff --git a/drivers/block/ub.c b/drivers/block/ub.c
index 0536b5b29ad..c48e1487858 100644
--- a/drivers/block/ub.c
+++ b/drivers/block/ub.c
@@ -28,6 +28,7 @@
28#include <linux/timer.h> 28#include <linux/timer.h>
29#include <linux/scatterlist.h> 29#include <linux/scatterlist.h>
30#include <linux/slab.h> 30#include <linux/slab.h>
31#include <linux/smp_lock.h>
31#include <scsi/scsi.h> 32#include <scsi/scsi.h>
32 33
33#define DRV_NAME "ub" 34#define DRV_NAME "ub"
@@ -648,7 +649,7 @@ static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
648 return 0; 649 return 0;
649 } 650 }
650 651
651 if (lun->changed && !blk_pc_request(rq)) { 652 if (lun->changed && rq->cmd_type != REQ_TYPE_BLOCK_PC) {
652 blk_start_request(rq); 653 blk_start_request(rq);
653 ub_end_rq(rq, SAM_STAT_CHECK_CONDITION); 654 ub_end_rq(rq, SAM_STAT_CHECK_CONDITION);
654 return 0; 655 return 0;
@@ -684,7 +685,7 @@ static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
684 } 685 }
685 urq->nsg = n_elem; 686 urq->nsg = n_elem;
686 687
687 if (blk_pc_request(rq)) { 688 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
688 ub_cmd_build_packet(sc, lun, cmd, urq); 689 ub_cmd_build_packet(sc, lun, cmd, urq);
689 } else { 690 } else {
690 ub_cmd_build_block(sc, lun, cmd, urq); 691 ub_cmd_build_block(sc, lun, cmd, urq);
@@ -781,7 +782,7 @@ static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
781 rq = urq->rq; 782 rq = urq->rq;
782 783
783 if (cmd->error == 0) { 784 if (cmd->error == 0) {
784 if (blk_pc_request(rq)) { 785 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
785 if (cmd->act_len >= rq->resid_len) 786 if (cmd->act_len >= rq->resid_len)
786 rq->resid_len = 0; 787 rq->resid_len = 0;
787 else 788 else
@@ -795,7 +796,7 @@ static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
795 } 796 }
796 } 797 }
797 } else { 798 } else {
798 if (blk_pc_request(rq)) { 799 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
799 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */ 800 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
800 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE); 801 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
801 rq->sense_len = UB_SENSE_SIZE; 802 rq->sense_len = UB_SENSE_SIZE;
@@ -1710,6 +1711,18 @@ err_open:
1710 return rc; 1711 return rc;
1711} 1712}
1712 1713
1714static int ub_bd_unlocked_open(struct block_device *bdev, fmode_t mode)
1715{
1716 int ret;
1717
1718 lock_kernel();
1719 ret = ub_bd_open(bdev, mode);
1720 unlock_kernel();
1721
1722 return ret;
1723}
1724
1725
1713/* 1726/*
1714 */ 1727 */
1715static int ub_bd_release(struct gendisk *disk, fmode_t mode) 1728static int ub_bd_release(struct gendisk *disk, fmode_t mode)
@@ -1717,7 +1730,10 @@ static int ub_bd_release(struct gendisk *disk, fmode_t mode)
1717 struct ub_lun *lun = disk->private_data; 1730 struct ub_lun *lun = disk->private_data;
1718 struct ub_dev *sc = lun->udev; 1731 struct ub_dev *sc = lun->udev;
1719 1732
1733 lock_kernel();
1720 ub_put(sc); 1734 ub_put(sc);
1735 unlock_kernel();
1736
1721 return 0; 1737 return 0;
1722} 1738}
1723 1739
@@ -1729,8 +1745,13 @@ static int ub_bd_ioctl(struct block_device *bdev, fmode_t mode,
1729{ 1745{
1730 struct gendisk *disk = bdev->bd_disk; 1746 struct gendisk *disk = bdev->bd_disk;
1731 void __user *usermem = (void __user *) arg; 1747 void __user *usermem = (void __user *) arg;
1748 int ret;
1749
1750 lock_kernel();
1751 ret = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, usermem);
1752 unlock_kernel();
1732 1753
1733 return scsi_cmd_ioctl(disk->queue, disk, mode, cmd, usermem); 1754 return ret;
1734} 1755}
1735 1756
1736/* 1757/*
@@ -1792,9 +1813,9 @@ static int ub_bd_media_changed(struct gendisk *disk)
1792 1813
1793static const struct block_device_operations ub_bd_fops = { 1814static const struct block_device_operations ub_bd_fops = {
1794 .owner = THIS_MODULE, 1815 .owner = THIS_MODULE,
1795 .open = ub_bd_open, 1816 .open = ub_bd_unlocked_open,
1796 .release = ub_bd_release, 1817 .release = ub_bd_release,
1797 .locked_ioctl = ub_bd_ioctl, 1818 .ioctl = ub_bd_ioctl,
1798 .media_changed = ub_bd_media_changed, 1819 .media_changed = ub_bd_media_changed,
1799 .revalidate_disk = ub_bd_revalidate, 1820 .revalidate_disk = ub_bd_revalidate,
1800}; 1821};
diff --git a/drivers/block/umem.c b/drivers/block/umem.c
index 2f9470ff8f7..8be57151f5d 100644
--- a/drivers/block/umem.c
+++ b/drivers/block/umem.c
@@ -478,7 +478,7 @@ static void process_page(unsigned long data)
478 le32_to_cpu(desc->local_addr)>>9, 478 le32_to_cpu(desc->local_addr)>>9,
479 le32_to_cpu(desc->transfer_size)); 479 le32_to_cpu(desc->transfer_size));
480 dump_dmastat(card, control); 480 dump_dmastat(card, control);
481 } else if (test_bit(BIO_RW, &bio->bi_rw) && 481 } else if ((bio->bi_rw & REQ_WRITE) &&
482 le32_to_cpu(desc->local_addr) >> 9 == 482 le32_to_cpu(desc->local_addr) >> 9 ==
483 card->init_size) { 483 card->init_size) {
484 card->init_size += le32_to_cpu(desc->transfer_size) >> 9; 484 card->init_size += le32_to_cpu(desc->transfer_size) >> 9;
diff --git a/drivers/block/viodasd.c b/drivers/block/viodasd.c
index 788d93882ab..f651e51a331 100644
--- a/drivers/block/viodasd.c
+++ b/drivers/block/viodasd.c
@@ -41,6 +41,7 @@
41#include <linux/errno.h> 41#include <linux/errno.h>
42#include <linux/init.h> 42#include <linux/init.h>
43#include <linux/string.h> 43#include <linux/string.h>
44#include <linux/smp_lock.h>
44#include <linux/dma-mapping.h> 45#include <linux/dma-mapping.h>
45#include <linux/completion.h> 46#include <linux/completion.h>
46#include <linux/device.h> 47#include <linux/device.h>
@@ -175,6 +176,18 @@ static int viodasd_open(struct block_device *bdev, fmode_t mode)
175 return 0; 176 return 0;
176} 177}
177 178
179static int viodasd_unlocked_open(struct block_device *bdev, fmode_t mode)
180{
181 int ret;
182
183 lock_kernel();
184 ret = viodasd_open(bdev, mode);
185 unlock_kernel();
186
187 return ret;
188}
189
190
178/* 191/*
179 * External release entry point. 192 * External release entry point.
180 */ 193 */
@@ -183,6 +196,7 @@ static int viodasd_release(struct gendisk *disk, fmode_t mode)
183 struct viodasd_device *d = disk->private_data; 196 struct viodasd_device *d = disk->private_data;
184 HvLpEvent_Rc hvrc; 197 HvLpEvent_Rc hvrc;
185 198
199 lock_kernel();
186 /* Send the event to OS/400. We DON'T expect a response */ 200 /* Send the event to OS/400. We DON'T expect a response */
187 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp, 201 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
188 HvLpEvent_Type_VirtualIo, 202 HvLpEvent_Type_VirtualIo,
@@ -195,6 +209,9 @@ static int viodasd_release(struct gendisk *disk, fmode_t mode)
195 0, 0, 0); 209 0, 0, 0);
196 if (hvrc != 0) 210 if (hvrc != 0)
197 pr_warning("HV close call failed %d\n", (int)hvrc); 211 pr_warning("HV close call failed %d\n", (int)hvrc);
212
213 unlock_kernel();
214
198 return 0; 215 return 0;
199} 216}
200 217
@@ -219,7 +236,7 @@ static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
219 */ 236 */
220static const struct block_device_operations viodasd_fops = { 237static const struct block_device_operations viodasd_fops = {
221 .owner = THIS_MODULE, 238 .owner = THIS_MODULE,
222 .open = viodasd_open, 239 .open = viodasd_unlocked_open,
223 .release = viodasd_release, 240 .release = viodasd_release,
224 .getgeo = viodasd_getgeo, 241 .getgeo = viodasd_getgeo,
225}; 242};
@@ -361,7 +378,7 @@ static void do_viodasd_request(struct request_queue *q)
361 if (req == NULL) 378 if (req == NULL)
362 return; 379 return;
363 /* check that request contains a valid command */ 380 /* check that request contains a valid command */
364 if (!blk_fs_request(req)) { 381 if (req->cmd_type != REQ_TYPE_FS) {
365 viodasd_end_request(req, -EIO, blk_rq_sectors(req)); 382 viodasd_end_request(req, -EIO, blk_rq_sectors(req));
366 continue; 383 continue;
367 } 384 }
diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 23b7c48df84..2aafafca2b1 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -2,6 +2,7 @@
2#include <linux/spinlock.h> 2#include <linux/spinlock.h>
3#include <linux/slab.h> 3#include <linux/slab.h>
4#include <linux/blkdev.h> 4#include <linux/blkdev.h>
5#include <linux/smp_lock.h>
5#include <linux/hdreg.h> 6#include <linux/hdreg.h>
6#include <linux/virtio.h> 7#include <linux/virtio.h>
7#include <linux/virtio_blk.h> 8#include <linux/virtio_blk.h>
@@ -65,13 +66,18 @@ static void blk_done(struct virtqueue *vq)
65 break; 66 break;
66 } 67 }
67 68
68 if (blk_pc_request(vbr->req)) { 69 switch (vbr->req->cmd_type) {
70 case REQ_TYPE_BLOCK_PC:
69 vbr->req->resid_len = vbr->in_hdr.residual; 71 vbr->req->resid_len = vbr->in_hdr.residual;
70 vbr->req->sense_len = vbr->in_hdr.sense_len; 72 vbr->req->sense_len = vbr->in_hdr.sense_len;
71 vbr->req->errors = vbr->in_hdr.errors; 73 vbr->req->errors = vbr->in_hdr.errors;
72 } 74 break;
73 if (blk_special_request(vbr->req)) 75 case REQ_TYPE_SPECIAL:
74 vbr->req->errors = (error != 0); 76 vbr->req->errors = (error != 0);
77 break;
78 default:
79 break;
80 }
75 81
76 __blk_end_request_all(vbr->req, error); 82 __blk_end_request_all(vbr->req, error);
77 list_del(&vbr->list); 83 list_del(&vbr->list);
@@ -94,36 +100,35 @@ static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
94 return false; 100 return false;
95 101
96 vbr->req = req; 102 vbr->req = req;
97 switch (req->cmd_type) { 103
98 case REQ_TYPE_FS: 104 if (req->cmd_flags & REQ_FLUSH) {
99 vbr->out_hdr.type = 0; 105 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
100 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
101 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
102 break;
103 case REQ_TYPE_BLOCK_PC:
104 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
105 vbr->out_hdr.sector = 0;
106 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
107 break;
108 case REQ_TYPE_SPECIAL:
109 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
110 vbr->out_hdr.sector = 0; 106 vbr->out_hdr.sector = 0;
111 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); 107 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
112 break; 108 } else {
113 case REQ_TYPE_LINUX_BLOCK: 109 switch (req->cmd_type) {
114 if (req->cmd[0] == REQ_LB_OP_FLUSH) { 110 case REQ_TYPE_FS:
115 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH; 111 vbr->out_hdr.type = 0;
112 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
113 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
114 break;
115 case REQ_TYPE_BLOCK_PC:
116 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
116 vbr->out_hdr.sector = 0; 117 vbr->out_hdr.sector = 0;
117 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); 118 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
118 break; 119 break;
120 case REQ_TYPE_SPECIAL:
121 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
122 vbr->out_hdr.sector = 0;
123 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
124 break;
125 default:
126 /* We don't put anything else in the queue. */
127 BUG();
119 } 128 }
120 /*FALLTHRU*/
121 default:
122 /* We don't put anything else in the queue. */
123 BUG();
124 } 129 }
125 130
126 if (blk_barrier_rq(vbr->req)) 131 if (vbr->req->cmd_flags & REQ_HARDBARRIER)
127 vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER; 132 vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
128 133
129 sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr)); 134 sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
@@ -134,12 +139,12 @@ static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
134 * block, and before the normal inhdr we put the sense data and the 139 * block, and before the normal inhdr we put the sense data and the
135 * inhdr with additional status information before the normal inhdr. 140 * inhdr with additional status information before the normal inhdr.
136 */ 141 */
137 if (blk_pc_request(vbr->req)) 142 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
138 sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len); 143 sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
139 144
140 num = blk_rq_map_sg(q, vbr->req, vblk->sg + out); 145 num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
141 146
142 if (blk_pc_request(vbr->req)) { 147 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
143 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, 96); 148 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, 96);
144 sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr, 149 sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
145 sizeof(vbr->in_hdr)); 150 sizeof(vbr->in_hdr));
@@ -190,12 +195,6 @@ static void do_virtblk_request(struct request_queue *q)
190 virtqueue_kick(vblk->vq); 195 virtqueue_kick(vblk->vq);
191} 196}
192 197
193static void virtblk_prepare_flush(struct request_queue *q, struct request *req)
194{
195 req->cmd_type = REQ_TYPE_LINUX_BLOCK;
196 req->cmd[0] = REQ_LB_OP_FLUSH;
197}
198
199/* return id (s/n) string for *disk to *id_str 198/* return id (s/n) string for *disk to *id_str
200 */ 199 */
201static int virtblk_get_id(struct gendisk *disk, char *id_str) 200static int virtblk_get_id(struct gendisk *disk, char *id_str)
@@ -219,7 +218,7 @@ static int virtblk_get_id(struct gendisk *disk, char *id_str)
219 return blk_execute_rq(vblk->disk->queue, vblk->disk, req, false); 218 return blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
220} 219}
221 220
222static int virtblk_ioctl(struct block_device *bdev, fmode_t mode, 221static int virtblk_locked_ioctl(struct block_device *bdev, fmode_t mode,
223 unsigned cmd, unsigned long data) 222 unsigned cmd, unsigned long data)
224{ 223{
225 struct gendisk *disk = bdev->bd_disk; 224 struct gendisk *disk = bdev->bd_disk;
@@ -235,6 +234,18 @@ static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
235 (void __user *)data); 234 (void __user *)data);
236} 235}
237 236
237static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
238 unsigned int cmd, unsigned long param)
239{
240 int ret;
241
242 lock_kernel();
243 ret = virtblk_locked_ioctl(bdev, mode, cmd, param);
244 unlock_kernel();
245
246 return ret;
247}
248
238/* We provide getgeo only to please some old bootloader/partitioning tools */ 249/* We provide getgeo only to please some old bootloader/partitioning tools */
239static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) 250static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
240{ 251{
@@ -261,7 +272,7 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
261} 272}
262 273
263static const struct block_device_operations virtblk_fops = { 274static const struct block_device_operations virtblk_fops = {
264 .locked_ioctl = virtblk_ioctl, 275 .ioctl = virtblk_ioctl,
265 .owner = THIS_MODULE, 276 .owner = THIS_MODULE,
266 .getgeo = virtblk_getgeo, 277 .getgeo = virtblk_getgeo,
267}; 278};
@@ -383,8 +394,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
383 * flushing a volatile write cache on the host. Use that 394 * flushing a volatile write cache on the host. Use that
384 * to implement write barrier support. 395 * to implement write barrier support.
385 */ 396 */
386 blk_queue_ordered(q, QUEUE_ORDERED_DRAIN_FLUSH, 397 blk_queue_ordered(q, QUEUE_ORDERED_DRAIN_FLUSH);
387 virtblk_prepare_flush);
388 } else if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER)) { 398 } else if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER)) {
389 /* 399 /*
390 * If the BARRIER feature is supported the host expects us 400 * If the BARRIER feature is supported the host expects us
@@ -393,7 +403,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
393 * never re-orders outstanding I/O. This feature is not 403 * never re-orders outstanding I/O. This feature is not
394 * useful for real life scenarious and deprecated. 404 * useful for real life scenarious and deprecated.
395 */ 405 */
396 blk_queue_ordered(q, QUEUE_ORDERED_TAG, NULL); 406 blk_queue_ordered(q, QUEUE_ORDERED_TAG);
397 } else { 407 } else {
398 /* 408 /*
399 * If the FLUSH feature is not supported we must assume that 409 * If the FLUSH feature is not supported we must assume that
@@ -401,7 +411,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
401 * caching. We still need to drain the queue to provider 411 * caching. We still need to drain the queue to provider
402 * proper barrier semantics. 412 * proper barrier semantics.
403 */ 413 */
404 blk_queue_ordered(q, QUEUE_ORDERED_DRAIN, NULL); 414 blk_queue_ordered(q, QUEUE_ORDERED_DRAIN);
405 } 415 }
406 416
407 /* If disk is read-only in the host, the guest should obey */ 417 /* If disk is read-only in the host, the guest should obey */
diff --git a/drivers/block/xd.c b/drivers/block/xd.c
index 18a80ff57ce..d5a3cd75056 100644
--- a/drivers/block/xd.c
+++ b/drivers/block/xd.c
@@ -46,6 +46,7 @@
46#include <linux/init.h> 46#include <linux/init.h>
47#include <linux/wait.h> 47#include <linux/wait.h>
48#include <linux/blkdev.h> 48#include <linux/blkdev.h>
49#include <linux/smp_lock.h>
49#include <linux/blkpg.h> 50#include <linux/blkpg.h>
50#include <linux/delay.h> 51#include <linux/delay.h>
51#include <linux/io.h> 52#include <linux/io.h>
@@ -133,7 +134,7 @@ static int xd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
133 134
134static const struct block_device_operations xd_fops = { 135static const struct block_device_operations xd_fops = {
135 .owner = THIS_MODULE, 136 .owner = THIS_MODULE,
136 .locked_ioctl = xd_ioctl, 137 .ioctl = xd_ioctl,
137 .getgeo = xd_getgeo, 138 .getgeo = xd_getgeo,
138}; 139};
139static DECLARE_WAIT_QUEUE_HEAD(xd_wait_int); 140static DECLARE_WAIT_QUEUE_HEAD(xd_wait_int);
@@ -322,7 +323,7 @@ static void do_xd_request (struct request_queue * q)
322 int res = -EIO; 323 int res = -EIO;
323 int retry; 324 int retry;
324 325
325 if (!blk_fs_request(req)) 326 if (req->cmd_type != REQ_TYPE_FS)
326 goto done; 327 goto done;
327 if (block + count > get_capacity(req->rq_disk)) 328 if (block + count > get_capacity(req->rq_disk))
328 goto done; 329 goto done;
@@ -347,7 +348,7 @@ static int xd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
347} 348}
348 349
349/* xd_ioctl: handle device ioctl's */ 350/* xd_ioctl: handle device ioctl's */
350static int xd_ioctl(struct block_device *bdev, fmode_t mode, u_int cmd, u_long arg) 351static int xd_locked_ioctl(struct block_device *bdev, fmode_t mode, u_int cmd, u_long arg)
351{ 352{
352 switch (cmd) { 353 switch (cmd) {
353 case HDIO_SET_DMA: 354 case HDIO_SET_DMA:
@@ -375,6 +376,18 @@ static int xd_ioctl(struct block_device *bdev, fmode_t mode, u_int cmd, u_long a
375 } 376 }
376} 377}
377 378
379static int xd_ioctl(struct block_device *bdev, fmode_t mode,
380 unsigned int cmd, unsigned long param)
381{
382 int ret;
383
384 lock_kernel();
385 ret = xd_locked_ioctl(bdev, mode, cmd, param);
386 unlock_kernel();
387
388 return ret;
389}
390
378/* xd_readwrite: handle a read/write request */ 391/* xd_readwrite: handle a read/write request */
379static int xd_readwrite (u_char operation,XD_INFO *p,char *buffer,u_int block,u_int count) 392static int xd_readwrite (u_char operation,XD_INFO *p,char *buffer,u_int block,u_int count)
380{ 393{
diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c
index f63ac3d1f8a..ac1b682edec 100644
--- a/drivers/block/xen-blkfront.c
+++ b/drivers/block/xen-blkfront.c
@@ -41,6 +41,7 @@
41#include <linux/cdrom.h> 41#include <linux/cdrom.h>
42#include <linux/module.h> 42#include <linux/module.h>
43#include <linux/slab.h> 43#include <linux/slab.h>
44#include <linux/smp_lock.h>
44#include <linux/scatterlist.h> 45#include <linux/scatterlist.h>
45 46
46#include <xen/xen.h> 47#include <xen/xen.h>
@@ -79,6 +80,7 @@ static const struct block_device_operations xlvbd_block_fops;
79 */ 80 */
80struct blkfront_info 81struct blkfront_info
81{ 82{
83 struct mutex mutex;
82 struct xenbus_device *xbdev; 84 struct xenbus_device *xbdev;
83 struct gendisk *gd; 85 struct gendisk *gd;
84 int vdevice; 86 int vdevice;
@@ -95,16 +97,14 @@ struct blkfront_info
95 unsigned long shadow_free; 97 unsigned long shadow_free;
96 int feature_barrier; 98 int feature_barrier;
97 int is_ready; 99 int is_ready;
98
99 /**
100 * The number of people holding this device open. We won't allow a
101 * hot-unplug unless this is 0.
102 */
103 int users;
104}; 100};
105 101
106static DEFINE_SPINLOCK(blkif_io_lock); 102static DEFINE_SPINLOCK(blkif_io_lock);
107 103
104static unsigned int nr_minors;
105static unsigned long *minors;
106static DEFINE_SPINLOCK(minor_lock);
107
108#define MAXIMUM_OUTSTANDING_BLOCK_REQS \ 108#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
109 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE) 109 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
110#define GRANT_INVALID_REF 0 110#define GRANT_INVALID_REF 0
@@ -139,6 +139,55 @@ static void add_id_to_freelist(struct blkfront_info *info,
139 info->shadow_free = id; 139 info->shadow_free = id;
140} 140}
141 141
142static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
143{
144 unsigned int end = minor + nr;
145 int rc;
146
147 if (end > nr_minors) {
148 unsigned long *bitmap, *old;
149
150 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
151 GFP_KERNEL);
152 if (bitmap == NULL)
153 return -ENOMEM;
154
155 spin_lock(&minor_lock);
156 if (end > nr_minors) {
157 old = minors;
158 memcpy(bitmap, minors,
159 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
160 minors = bitmap;
161 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
162 } else
163 old = bitmap;
164 spin_unlock(&minor_lock);
165 kfree(old);
166 }
167
168 spin_lock(&minor_lock);
169 if (find_next_bit(minors, end, minor) >= end) {
170 for (; minor < end; ++minor)
171 __set_bit(minor, minors);
172 rc = 0;
173 } else
174 rc = -EBUSY;
175 spin_unlock(&minor_lock);
176
177 return rc;
178}
179
180static void xlbd_release_minors(unsigned int minor, unsigned int nr)
181{
182 unsigned int end = minor + nr;
183
184 BUG_ON(end > nr_minors);
185 spin_lock(&minor_lock);
186 for (; minor < end; ++minor)
187 __clear_bit(minor, minors);
188 spin_unlock(&minor_lock);
189}
190
142static void blkif_restart_queue_callback(void *arg) 191static void blkif_restart_queue_callback(void *arg)
143{ 192{
144 struct blkfront_info *info = (struct blkfront_info *)arg; 193 struct blkfront_info *info = (struct blkfront_info *)arg;
@@ -239,7 +288,7 @@ static int blkif_queue_request(struct request *req)
239 288
240 ring_req->operation = rq_data_dir(req) ? 289 ring_req->operation = rq_data_dir(req) ?
241 BLKIF_OP_WRITE : BLKIF_OP_READ; 290 BLKIF_OP_WRITE : BLKIF_OP_READ;
242 if (blk_barrier_rq(req)) 291 if (req->cmd_flags & REQ_HARDBARRIER)
243 ring_req->operation = BLKIF_OP_WRITE_BARRIER; 292 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
244 293
245 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg); 294 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
@@ -310,7 +359,7 @@ static void do_blkif_request(struct request_queue *rq)
310 359
311 blk_start_request(req); 360 blk_start_request(req);
312 361
313 if (!blk_fs_request(req)) { 362 if (req->cmd_type != REQ_TYPE_FS) {
314 __blk_end_request_all(req, -EIO); 363 __blk_end_request_all(req, -EIO);
315 continue; 364 continue;
316 } 365 }
@@ -372,17 +421,22 @@ static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
372static int xlvbd_barrier(struct blkfront_info *info) 421static int xlvbd_barrier(struct blkfront_info *info)
373{ 422{
374 int err; 423 int err;
424 const char *barrier;
375 425
376 err = blk_queue_ordered(info->rq, 426 switch (info->feature_barrier) {
377 info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE, 427 case QUEUE_ORDERED_DRAIN: barrier = "enabled (drain)"; break;
378 NULL); 428 case QUEUE_ORDERED_TAG: barrier = "enabled (tag)"; break;
429 case QUEUE_ORDERED_NONE: barrier = "disabled"; break;
430 default: return -EINVAL;
431 }
432
433 err = blk_queue_ordered(info->rq, info->feature_barrier);
379 434
380 if (err) 435 if (err)
381 return err; 436 return err;
382 437
383 printk(KERN_INFO "blkfront: %s: barriers %s\n", 438 printk(KERN_INFO "blkfront: %s: barriers %s\n",
384 info->gd->disk_name, 439 info->gd->disk_name, barrier);
385 info->feature_barrier ? "enabled" : "disabled");
386 return 0; 440 return 0;
387} 441}
388 442
@@ -418,9 +472,14 @@ static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
418 if ((minor % nr_parts) == 0) 472 if ((minor % nr_parts) == 0)
419 nr_minors = nr_parts; 473 nr_minors = nr_parts;
420 474
475 err = xlbd_reserve_minors(minor, nr_minors);
476 if (err)
477 goto out;
478 err = -ENODEV;
479
421 gd = alloc_disk(nr_minors); 480 gd = alloc_disk(nr_minors);
422 if (gd == NULL) 481 if (gd == NULL)
423 goto out; 482 goto release;
424 483
425 offset = minor / nr_parts; 484 offset = minor / nr_parts;
426 485
@@ -451,14 +510,13 @@ static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
451 510
452 if (xlvbd_init_blk_queue(gd, sector_size)) { 511 if (xlvbd_init_blk_queue(gd, sector_size)) {
453 del_gendisk(gd); 512 del_gendisk(gd);
454 goto out; 513 goto release;
455 } 514 }
456 515
457 info->rq = gd->queue; 516 info->rq = gd->queue;
458 info->gd = gd; 517 info->gd = gd;
459 518
460 if (info->feature_barrier) 519 xlvbd_barrier(info);
461 xlvbd_barrier(info);
462 520
463 if (vdisk_info & VDISK_READONLY) 521 if (vdisk_info & VDISK_READONLY)
464 set_disk_ro(gd, 1); 522 set_disk_ro(gd, 1);
@@ -471,10 +529,45 @@ static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
471 529
472 return 0; 530 return 0;
473 531
532 release:
533 xlbd_release_minors(minor, nr_minors);
474 out: 534 out:
475 return err; 535 return err;
476} 536}
477 537
538static void xlvbd_release_gendisk(struct blkfront_info *info)
539{
540 unsigned int minor, nr_minors;
541 unsigned long flags;
542
543 if (info->rq == NULL)
544 return;
545
546 spin_lock_irqsave(&blkif_io_lock, flags);
547
548 /* No more blkif_request(). */
549 blk_stop_queue(info->rq);
550
551 /* No more gnttab callback work. */
552 gnttab_cancel_free_callback(&info->callback);
553 spin_unlock_irqrestore(&blkif_io_lock, flags);
554
555 /* Flush gnttab callback work. Must be done with no locks held. */
556 flush_scheduled_work();
557
558 del_gendisk(info->gd);
559
560 minor = info->gd->first_minor;
561 nr_minors = info->gd->minors;
562 xlbd_release_minors(minor, nr_minors);
563
564 blk_cleanup_queue(info->rq);
565 info->rq = NULL;
566
567 put_disk(info->gd);
568 info->gd = NULL;
569}
570
478static void kick_pending_request_queues(struct blkfront_info *info) 571static void kick_pending_request_queues(struct blkfront_info *info)
479{ 572{
480 if (!RING_FULL(&info->ring)) { 573 if (!RING_FULL(&info->ring)) {
@@ -569,7 +662,7 @@ static irqreturn_t blkif_interrupt(int irq, void *dev_id)
569 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n", 662 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
570 info->gd->disk_name); 663 info->gd->disk_name);
571 error = -EOPNOTSUPP; 664 error = -EOPNOTSUPP;
572 info->feature_barrier = 0; 665 info->feature_barrier = QUEUE_ORDERED_NONE;
573 xlvbd_barrier(info); 666 xlvbd_barrier(info);
574 } 667 }
575 /* fall through */ 668 /* fall through */
@@ -652,7 +745,7 @@ fail:
652 745
653 746
654/* Common code used when first setting up, and when resuming. */ 747/* Common code used when first setting up, and when resuming. */
655static int talk_to_backend(struct xenbus_device *dev, 748static int talk_to_blkback(struct xenbus_device *dev,
656 struct blkfront_info *info) 749 struct blkfront_info *info)
657{ 750{
658 const char *message = NULL; 751 const char *message = NULL;
@@ -712,7 +805,6 @@ again:
712 return err; 805 return err;
713} 806}
714 807
715
716/** 808/**
717 * Entry point to this code when a new device is created. Allocate the basic 809 * Entry point to this code when a new device is created. Allocate the basic
718 * structures and the ring buffer for communication with the backend, and 810 * structures and the ring buffer for communication with the backend, and
@@ -773,6 +865,7 @@ static int blkfront_probe(struct xenbus_device *dev,
773 return -ENOMEM; 865 return -ENOMEM;
774 } 866 }
775 867
868 mutex_init(&info->mutex);
776 info->xbdev = dev; 869 info->xbdev = dev;
777 info->vdevice = vdevice; 870 info->vdevice = vdevice;
778 info->connected = BLKIF_STATE_DISCONNECTED; 871 info->connected = BLKIF_STATE_DISCONNECTED;
@@ -786,7 +879,7 @@ static int blkfront_probe(struct xenbus_device *dev,
786 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); 879 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
787 dev_set_drvdata(&dev->dev, info); 880 dev_set_drvdata(&dev->dev, info);
788 881
789 err = talk_to_backend(dev, info); 882 err = talk_to_blkback(dev, info);
790 if (err) { 883 if (err) {
791 kfree(info); 884 kfree(info);
792 dev_set_drvdata(&dev->dev, NULL); 885 dev_set_drvdata(&dev->dev, NULL);
@@ -881,13 +974,50 @@ static int blkfront_resume(struct xenbus_device *dev)
881 974
882 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); 975 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
883 976
884 err = talk_to_backend(dev, info); 977 err = talk_to_blkback(dev, info);
885 if (info->connected == BLKIF_STATE_SUSPENDED && !err) 978 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
886 err = blkif_recover(info); 979 err = blkif_recover(info);
887 980
888 return err; 981 return err;
889} 982}
890 983
984static void
985blkfront_closing(struct blkfront_info *info)
986{
987 struct xenbus_device *xbdev = info->xbdev;
988 struct block_device *bdev = NULL;
989
990 mutex_lock(&info->mutex);
991
992 if (xbdev->state == XenbusStateClosing) {
993 mutex_unlock(&info->mutex);
994 return;
995 }
996
997 if (info->gd)
998 bdev = bdget_disk(info->gd, 0);
999
1000 mutex_unlock(&info->mutex);
1001
1002 if (!bdev) {
1003 xenbus_frontend_closed(xbdev);
1004 return;
1005 }
1006
1007 mutex_lock(&bdev->bd_mutex);
1008
1009 if (bdev->bd_openers) {
1010 xenbus_dev_error(xbdev, -EBUSY,
1011 "Device in use; refusing to close");
1012 xenbus_switch_state(xbdev, XenbusStateClosing);
1013 } else {
1014 xlvbd_release_gendisk(info);
1015 xenbus_frontend_closed(xbdev);
1016 }
1017
1018 mutex_unlock(&bdev->bd_mutex);
1019 bdput(bdev);
1020}
891 1021
892/* 1022/*
893 * Invoked when the backend is finally 'ready' (and has told produced 1023 * Invoked when the backend is finally 'ready' (and has told produced
@@ -899,11 +1029,31 @@ static void blkfront_connect(struct blkfront_info *info)
899 unsigned long sector_size; 1029 unsigned long sector_size;
900 unsigned int binfo; 1030 unsigned int binfo;
901 int err; 1031 int err;
902 1032 int barrier;
903 if ((info->connected == BLKIF_STATE_CONNECTED) || 1033
904 (info->connected == BLKIF_STATE_SUSPENDED) ) 1034 switch (info->connected) {
1035 case BLKIF_STATE_CONNECTED:
1036 /*
1037 * Potentially, the back-end may be signalling
1038 * a capacity change; update the capacity.
1039 */
1040 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1041 "sectors", "%Lu", &sectors);
1042 if (XENBUS_EXIST_ERR(err))
1043 return;
1044 printk(KERN_INFO "Setting capacity to %Lu\n",
1045 sectors);
1046 set_capacity(info->gd, sectors);
1047 revalidate_disk(info->gd);
1048
1049 /* fall through */
1050 case BLKIF_STATE_SUSPENDED:
905 return; 1051 return;
906 1052
1053 default:
1054 break;
1055 }
1056
907 dev_dbg(&info->xbdev->dev, "%s:%s.\n", 1057 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
908 __func__, info->xbdev->otherend); 1058 __func__, info->xbdev->otherend);
909 1059
@@ -920,10 +1070,26 @@ static void blkfront_connect(struct blkfront_info *info)
920 } 1070 }
921 1071
922 err = xenbus_gather(XBT_NIL, info->xbdev->otherend, 1072 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
923 "feature-barrier", "%lu", &info->feature_barrier, 1073 "feature-barrier", "%lu", &barrier,
924 NULL); 1074 NULL);
1075
1076 /*
1077 * If there's no "feature-barrier" defined, then it means
1078 * we're dealing with a very old backend which writes
1079 * synchronously; draining will do what needs to get done.
1080 *
1081 * If there are barriers, then we can do full queued writes
1082 * with tagged barriers.
1083 *
1084 * If barriers are not supported, then there's no much we can
1085 * do, so just set ordering to NONE.
1086 */
925 if (err) 1087 if (err)
926 info->feature_barrier = 0; 1088 info->feature_barrier = QUEUE_ORDERED_DRAIN;
1089 else if (barrier)
1090 info->feature_barrier = QUEUE_ORDERED_TAG;
1091 else
1092 info->feature_barrier = QUEUE_ORDERED_NONE;
927 1093
928 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size); 1094 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
929 if (err) { 1095 if (err) {
@@ -946,52 +1112,14 @@ static void blkfront_connect(struct blkfront_info *info)
946} 1112}
947 1113
948/** 1114/**
949 * Handle the change of state of the backend to Closing. We must delete our
950 * device-layer structures now, to ensure that writes are flushed through to
951 * the backend. Once is this done, we can switch to Closed in
952 * acknowledgement.
953 */
954static void blkfront_closing(struct xenbus_device *dev)
955{
956 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
957 unsigned long flags;
958
959 dev_dbg(&dev->dev, "blkfront_closing: %s removed\n", dev->nodename);
960
961 if (info->rq == NULL)
962 goto out;
963
964 spin_lock_irqsave(&blkif_io_lock, flags);
965
966 /* No more blkif_request(). */
967 blk_stop_queue(info->rq);
968
969 /* No more gnttab callback work. */
970 gnttab_cancel_free_callback(&info->callback);
971 spin_unlock_irqrestore(&blkif_io_lock, flags);
972
973 /* Flush gnttab callback work. Must be done with no locks held. */
974 flush_scheduled_work();
975
976 blk_cleanup_queue(info->rq);
977 info->rq = NULL;
978
979 del_gendisk(info->gd);
980
981 out:
982 xenbus_frontend_closed(dev);
983}
984
985/**
986 * Callback received when the backend's state changes. 1115 * Callback received when the backend's state changes.
987 */ 1116 */
988static void backend_changed(struct xenbus_device *dev, 1117static void blkback_changed(struct xenbus_device *dev,
989 enum xenbus_state backend_state) 1118 enum xenbus_state backend_state)
990{ 1119{
991 struct blkfront_info *info = dev_get_drvdata(&dev->dev); 1120 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
992 struct block_device *bd;
993 1121
994 dev_dbg(&dev->dev, "blkfront:backend_changed.\n"); 1122 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
995 1123
996 switch (backend_state) { 1124 switch (backend_state) {
997 case XenbusStateInitialising: 1125 case XenbusStateInitialising:
@@ -1006,35 +1134,56 @@ static void backend_changed(struct xenbus_device *dev,
1006 break; 1134 break;
1007 1135
1008 case XenbusStateClosing: 1136 case XenbusStateClosing:
1009 if (info->gd == NULL) { 1137 blkfront_closing(info);
1010 xenbus_frontend_closed(dev);
1011 break;
1012 }
1013 bd = bdget_disk(info->gd, 0);
1014 if (bd == NULL)
1015 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
1016
1017 mutex_lock(&bd->bd_mutex);
1018 if (info->users > 0)
1019 xenbus_dev_error(dev, -EBUSY,
1020 "Device in use; refusing to close");
1021 else
1022 blkfront_closing(dev);
1023 mutex_unlock(&bd->bd_mutex);
1024 bdput(bd);
1025 break; 1138 break;
1026 } 1139 }
1027} 1140}
1028 1141
1029static int blkfront_remove(struct xenbus_device *dev) 1142static int blkfront_remove(struct xenbus_device *xbdev)
1030{ 1143{
1031 struct blkfront_info *info = dev_get_drvdata(&dev->dev); 1144 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1145 struct block_device *bdev = NULL;
1146 struct gendisk *disk;
1032 1147
1033 dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename); 1148 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
1034 1149
1035 blkif_free(info, 0); 1150 blkif_free(info, 0);
1036 1151
1037 kfree(info); 1152 mutex_lock(&info->mutex);
1153
1154 disk = info->gd;
1155 if (disk)
1156 bdev = bdget_disk(disk, 0);
1157
1158 info->xbdev = NULL;
1159 mutex_unlock(&info->mutex);
1160
1161 if (!bdev) {
1162 kfree(info);
1163 return 0;
1164 }
1165
1166 /*
1167 * The xbdev was removed before we reached the Closed
1168 * state. See if it's safe to remove the disk. If the bdev
1169 * isn't closed yet, we let release take care of it.
1170 */
1171
1172 mutex_lock(&bdev->bd_mutex);
1173 info = disk->private_data;
1174
1175 dev_warn(disk_to_dev(disk),
1176 "%s was hot-unplugged, %d stale handles\n",
1177 xbdev->nodename, bdev->bd_openers);
1178
1179 if (info && !bdev->bd_openers) {
1180 xlvbd_release_gendisk(info);
1181 disk->private_data = NULL;
1182 kfree(info);
1183 }
1184
1185 mutex_unlock(&bdev->bd_mutex);
1186 bdput(bdev);
1038 1187
1039 return 0; 1188 return 0;
1040} 1189}
@@ -1043,30 +1192,78 @@ static int blkfront_is_ready(struct xenbus_device *dev)
1043{ 1192{
1044 struct blkfront_info *info = dev_get_drvdata(&dev->dev); 1193 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1045 1194
1046 return info->is_ready; 1195 return info->is_ready && info->xbdev;
1047} 1196}
1048 1197
1049static int blkif_open(struct block_device *bdev, fmode_t mode) 1198static int blkif_open(struct block_device *bdev, fmode_t mode)
1050{ 1199{
1051 struct blkfront_info *info = bdev->bd_disk->private_data; 1200 struct gendisk *disk = bdev->bd_disk;
1052 info->users++; 1201 struct blkfront_info *info;
1053 return 0; 1202 int err = 0;
1203
1204 lock_kernel();
1205
1206 info = disk->private_data;
1207 if (!info) {
1208 /* xbdev gone */
1209 err = -ERESTARTSYS;
1210 goto out;
1211 }
1212
1213 mutex_lock(&info->mutex);
1214
1215 if (!info->gd)
1216 /* xbdev is closed */
1217 err = -ERESTARTSYS;
1218
1219 mutex_unlock(&info->mutex);
1220
1221out:
1222 unlock_kernel();
1223 return err;
1054} 1224}
1055 1225
1056static int blkif_release(struct gendisk *disk, fmode_t mode) 1226static int blkif_release(struct gendisk *disk, fmode_t mode)
1057{ 1227{
1058 struct blkfront_info *info = disk->private_data; 1228 struct blkfront_info *info = disk->private_data;
1059 info->users--; 1229 struct block_device *bdev;
1060 if (info->users == 0) { 1230 struct xenbus_device *xbdev;
1061 /* Check whether we have been instructed to close. We will 1231
1062 have ignored this request initially, as the device was 1232 lock_kernel();
1063 still mounted. */ 1233
1064 struct xenbus_device *dev = info->xbdev; 1234 bdev = bdget_disk(disk, 0);
1065 enum xenbus_state state = xenbus_read_driver_state(dev->otherend); 1235 bdput(bdev);
1066 1236
1067 if (state == XenbusStateClosing && info->is_ready) 1237 if (bdev->bd_openers)
1068 blkfront_closing(dev); 1238 goto out;
1239
1240 /*
1241 * Check if we have been instructed to close. We will have
1242 * deferred this request, because the bdev was still open.
1243 */
1244
1245 mutex_lock(&info->mutex);
1246 xbdev = info->xbdev;
1247
1248 if (xbdev && xbdev->state == XenbusStateClosing) {
1249 /* pending switch to state closed */
1250 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
1251 xlvbd_release_gendisk(info);
1252 xenbus_frontend_closed(info->xbdev);
1253 }
1254
1255 mutex_unlock(&info->mutex);
1256
1257 if (!xbdev) {
1258 /* sudden device removal */
1259 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
1260 xlvbd_release_gendisk(info);
1261 disk->private_data = NULL;
1262 kfree(info);
1069 } 1263 }
1264
1265out:
1266 unlock_kernel();
1070 return 0; 1267 return 0;
1071} 1268}
1072 1269
@@ -1076,7 +1273,7 @@ static const struct block_device_operations xlvbd_block_fops =
1076 .open = blkif_open, 1273 .open = blkif_open,
1077 .release = blkif_release, 1274 .release = blkif_release,
1078 .getgeo = blkif_getgeo, 1275 .getgeo = blkif_getgeo,
1079 .locked_ioctl = blkif_ioctl, 1276 .ioctl = blkif_ioctl,
1080}; 1277};
1081 1278
1082 1279
@@ -1092,7 +1289,7 @@ static struct xenbus_driver blkfront = {
1092 .probe = blkfront_probe, 1289 .probe = blkfront_probe,
1093 .remove = blkfront_remove, 1290 .remove = blkfront_remove,
1094 .resume = blkfront_resume, 1291 .resume = blkfront_resume,
1095 .otherend_changed = backend_changed, 1292 .otherend_changed = blkback_changed,
1096 .is_ready = blkfront_is_ready, 1293 .is_ready = blkfront_is_ready,
1097}; 1294};
1098 1295
diff --git a/drivers/block/xsysace.c b/drivers/block/xsysace.c
index a7b83c0a7eb..b71888b909a 100644
--- a/drivers/block/xsysace.c
+++ b/drivers/block/xsysace.c
@@ -89,6 +89,7 @@
89#include <linux/delay.h> 89#include <linux/delay.h>
90#include <linux/slab.h> 90#include <linux/slab.h>
91#include <linux/blkdev.h> 91#include <linux/blkdev.h>
92#include <linux/smp_lock.h>
92#include <linux/ata.h> 93#include <linux/ata.h>
93#include <linux/hdreg.h> 94#include <linux/hdreg.h>
94#include <linux/platform_device.h> 95#include <linux/platform_device.h>
@@ -465,7 +466,7 @@ struct request *ace_get_next_request(struct request_queue * q)
465 struct request *req; 466 struct request *req;
466 467
467 while ((req = blk_peek_request(q)) != NULL) { 468 while ((req = blk_peek_request(q)) != NULL) {
468 if (blk_fs_request(req)) 469 if (req->cmd_type == REQ_TYPE_FS)
469 break; 470 break;
470 blk_start_request(req); 471 blk_start_request(req);
471 __blk_end_request_all(req, -EIO); 472 __blk_end_request_all(req, -EIO);
@@ -901,11 +902,14 @@ static int ace_open(struct block_device *bdev, fmode_t mode)
901 902
902 dev_dbg(ace->dev, "ace_open() users=%i\n", ace->users + 1); 903 dev_dbg(ace->dev, "ace_open() users=%i\n", ace->users + 1);
903 904
905 lock_kernel();
904 spin_lock_irqsave(&ace->lock, flags); 906 spin_lock_irqsave(&ace->lock, flags);
905 ace->users++; 907 ace->users++;
906 spin_unlock_irqrestore(&ace->lock, flags); 908 spin_unlock_irqrestore(&ace->lock, flags);
907 909
908 check_disk_change(bdev); 910 check_disk_change(bdev);
911 unlock_kernel();
912
909 return 0; 913 return 0;
910} 914}
911 915
@@ -917,6 +921,7 @@ static int ace_release(struct gendisk *disk, fmode_t mode)
917 921
918 dev_dbg(ace->dev, "ace_release() users=%i\n", ace->users - 1); 922 dev_dbg(ace->dev, "ace_release() users=%i\n", ace->users - 1);
919 923
924 lock_kernel();
920 spin_lock_irqsave(&ace->lock, flags); 925 spin_lock_irqsave(&ace->lock, flags);
921 ace->users--; 926 ace->users--;
922 if (ace->users == 0) { 927 if (ace->users == 0) {
@@ -924,6 +929,7 @@ static int ace_release(struct gendisk *disk, fmode_t mode)
924 ace_out(ace, ACE_CTRL, val & ~ACE_CTRL_LOCKREQ); 929 ace_out(ace, ACE_CTRL, val & ~ACE_CTRL_LOCKREQ);
925 } 930 }
926 spin_unlock_irqrestore(&ace->lock, flags); 931 spin_unlock_irqrestore(&ace->lock, flags);
932 unlock_kernel();
927 return 0; 933 return 0;
928} 934}
929 935
diff --git a/drivers/block/z2ram.c b/drivers/block/z2ram.c
index 9114654b54d..d75b2bb601a 100644
--- a/drivers/block/z2ram.c
+++ b/drivers/block/z2ram.c
@@ -33,6 +33,7 @@
33#include <linux/module.h> 33#include <linux/module.h>
34#include <linux/blkdev.h> 34#include <linux/blkdev.h>
35#include <linux/bitops.h> 35#include <linux/bitops.h>
36#include <linux/smp_lock.h>
36#include <linux/slab.h> 37#include <linux/slab.h>
37 38
38#include <asm/setup.h> 39#include <asm/setup.h>
@@ -153,6 +154,7 @@ static int z2_open(struct block_device *bdev, fmode_t mode)
153 154
154 device = MINOR(bdev->bd_dev); 155 device = MINOR(bdev->bd_dev);
155 156
157 lock_kernel();
156 if ( current_device != -1 && current_device != device ) 158 if ( current_device != -1 && current_device != device )
157 { 159 {
158 rc = -EBUSY; 160 rc = -EBUSY;
@@ -294,20 +296,25 @@ static int z2_open(struct block_device *bdev, fmode_t mode)
294 set_capacity(z2ram_gendisk, z2ram_size >> 9); 296 set_capacity(z2ram_gendisk, z2ram_size >> 9);
295 } 297 }
296 298
299 unlock_kernel();
297 return 0; 300 return 0;
298 301
299err_out_kfree: 302err_out_kfree:
300 kfree(z2ram_map); 303 kfree(z2ram_map);
301err_out: 304err_out:
305 unlock_kernel();
302 return rc; 306 return rc;
303} 307}
304 308
305static int 309static int
306z2_release(struct gendisk *disk, fmode_t mode) 310z2_release(struct gendisk *disk, fmode_t mode)
307{ 311{
308 if ( current_device == -1 ) 312 lock_kernel();
309 return 0; 313 if ( current_device == -1 ) {
310 314 unlock_kernel();
315 return 0;
316 }
317 unlock_kernel();
311 /* 318 /*
312 * FIXME: unmap memory 319 * FIXME: unmap memory
313 */ 320 */
diff --git a/drivers/cdrom/cdrom.c b/drivers/cdrom/cdrom.c
index e3749d0ba68..af13c62dc47 100644
--- a/drivers/cdrom/cdrom.c
+++ b/drivers/cdrom/cdrom.c
@@ -242,6 +242,8 @@
242 242
243-------------------------------------------------------------------------*/ 243-------------------------------------------------------------------------*/
244 244
245#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
246
245#define REVISION "Revision: 3.20" 247#define REVISION "Revision: 3.20"
246#define VERSION "Id: cdrom.c 3.20 2003/12/17" 248#define VERSION "Id: cdrom.c 3.20 2003/12/17"
247 249
@@ -314,11 +316,17 @@ static const char *mrw_format_status[] = {
314static const char *mrw_address_space[] = { "DMA", "GAA" }; 316static const char *mrw_address_space[] = { "DMA", "GAA" };
315 317
316#if (ERRLOGMASK!=CD_NOTHING) 318#if (ERRLOGMASK!=CD_NOTHING)
317#define cdinfo(type, fmt, args...) \ 319#define cdinfo(type, fmt, args...) \
318 if ((ERRLOGMASK & type) || debug==1 ) \ 320do { \
319 printk(KERN_INFO "cdrom: " fmt, ## args) 321 if ((ERRLOGMASK & type) || debug == 1) \
322 pr_info(fmt, ##args); \
323} while (0)
320#else 324#else
321#define cdinfo(type, fmt, args...) 325#define cdinfo(type, fmt, args...) \
326do { \
327 if (0 && (ERRLOGMASK & type) || debug == 1) \
328 pr_info(fmt, ##args); \
329} while (0)
322#endif 330#endif
323 331
324/* These are used to simplify getting data in from and back to user land */ 332/* These are used to simplify getting data in from and back to user land */
@@ -395,7 +403,7 @@ int register_cdrom(struct cdrom_device_info *cdi)
395 if (cdo->open == NULL || cdo->release == NULL) 403 if (cdo->open == NULL || cdo->release == NULL)
396 return -EINVAL; 404 return -EINVAL;
397 if (!banner_printed) { 405 if (!banner_printed) {
398 printk(KERN_INFO "Uniform CD-ROM driver " REVISION "\n"); 406 pr_info("Uniform CD-ROM driver " REVISION "\n");
399 banner_printed = 1; 407 banner_printed = 1;
400 cdrom_sysctl_register(); 408 cdrom_sysctl_register();
401 } 409 }
@@ -546,7 +554,7 @@ static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
546 unsigned char buffer[12]; 554 unsigned char buffer[12];
547 int ret; 555 int ret;
548 556
549 printk(KERN_INFO "cdrom: %sstarting format\n", cont ? "Re" : ""); 557 pr_info("%sstarting format\n", cont ? "Re" : "");
550 558
551 /* 559 /*
552 * FmtData bit set (bit 4), format type is 1 560 * FmtData bit set (bit 4), format type is 1
@@ -576,7 +584,7 @@ static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
576 584
577 ret = cdi->ops->generic_packet(cdi, &cgc); 585 ret = cdi->ops->generic_packet(cdi, &cgc);
578 if (ret) 586 if (ret)
579 printk(KERN_INFO "cdrom: bgformat failed\n"); 587 pr_info("bgformat failed\n");
580 588
581 return ret; 589 return ret;
582} 590}
@@ -622,8 +630,7 @@ static int cdrom_mrw_exit(struct cdrom_device_info *cdi)
622 630
623 ret = 0; 631 ret = 0;
624 if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) { 632 if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
625 printk(KERN_INFO "cdrom: issuing MRW back ground " 633 pr_info("issuing MRW background format suspend\n");
626 "format suspend\n");
627 ret = cdrom_mrw_bgformat_susp(cdi, 0); 634 ret = cdrom_mrw_bgformat_susp(cdi, 0);
628 } 635 }
629 636
@@ -658,7 +665,8 @@ static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
658 if ((ret = cdrom_mode_select(cdi, &cgc))) 665 if ((ret = cdrom_mode_select(cdi, &cgc)))
659 return ret; 666 return ret;
660 667
661 printk(KERN_INFO "cdrom: %s: mrw address space %s selected\n", cdi->name, mrw_address_space[space]); 668 pr_info("%s: mrw address space %s selected\n",
669 cdi->name, mrw_address_space[space]);
662 return 0; 670 return 0;
663} 671}
664 672
@@ -762,7 +770,7 @@ static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
762 * always reset to DMA lba space on open 770 * always reset to DMA lba space on open
763 */ 771 */
764 if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) { 772 if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
765 printk(KERN_ERR "cdrom: failed setting lba address space\n"); 773 pr_err("failed setting lba address space\n");
766 return 1; 774 return 1;
767 } 775 }
768 776
@@ -781,8 +789,7 @@ static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
781 * 3 - MRW formatting complete 789 * 3 - MRW formatting complete
782 */ 790 */
783 ret = 0; 791 ret = 0;
784 printk(KERN_INFO "cdrom open: mrw_status '%s'\n", 792 pr_info("open: mrw_status '%s'\n", mrw_format_status[di.mrw_status]);
785 mrw_format_status[di.mrw_status]);
786 if (!di.mrw_status) 793 if (!di.mrw_status)
787 ret = 1; 794 ret = 1;
788 else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE && 795 else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE &&
@@ -932,8 +939,7 @@ static void cdrom_dvd_rw_close_write(struct cdrom_device_info *cdi)
932 return; 939 return;
933 } 940 }
934 941
935 printk(KERN_INFO "cdrom: %s: dirty DVD+RW media, \"finalizing\"\n", 942 pr_info("%s: dirty DVD+RW media, \"finalizing\"\n", cdi->name);
936 cdi->name);
937 943
938 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE); 944 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
939 cgc.cmd[0] = GPCMD_FLUSH_CACHE; 945 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
@@ -2176,7 +2182,7 @@ retry:
2176 * frame dma, so drop to single frame dma if we need to 2182 * frame dma, so drop to single frame dma if we need to
2177 */ 2183 */
2178 if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) { 2184 if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
2179 printk("cdrom: dropping to single frame dma\n"); 2185 pr_info("dropping to single frame dma\n");
2180 cdi->cdda_method = CDDA_BPC_SINGLE; 2186 cdi->cdda_method = CDDA_BPC_SINGLE;
2181 goto retry; 2187 goto retry;
2182 } 2188 }
@@ -2189,7 +2195,7 @@ retry:
2189 if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b) 2195 if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
2190 return ret; 2196 return ret;
2191 2197
2192 printk("cdrom: dropping to old style cdda (sense=%x)\n", cdi->last_sense); 2198 pr_info("dropping to old style cdda (sense=%x)\n", cdi->last_sense);
2193 cdi->cdda_method = CDDA_OLD; 2199 cdi->cdda_method = CDDA_OLD;
2194 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes); 2200 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2195} 2201}
@@ -3401,7 +3407,7 @@ static int cdrom_print_info(const char *header, int val, char *info,
3401 "\t%d", CDROM_CAN(val) != 0); 3407 "\t%d", CDROM_CAN(val) != 0);
3402 break; 3408 break;
3403 default: 3409 default:
3404 printk(KERN_INFO "cdrom: invalid option%d\n", option); 3410 pr_info("invalid option%d\n", option);
3405 return 1; 3411 return 1;
3406 } 3412 }
3407 if (!ret) 3413 if (!ret)
@@ -3491,7 +3497,7 @@ doit:
3491 mutex_unlock(&cdrom_mutex); 3497 mutex_unlock(&cdrom_mutex);
3492 return proc_dostring(ctl, write, buffer, lenp, ppos); 3498 return proc_dostring(ctl, write, buffer, lenp, ppos);
3493done: 3499done:
3494 printk(KERN_INFO "cdrom: info buffer too small\n"); 3500 pr_info("info buffer too small\n");
3495 goto doit; 3501 goto doit;
3496} 3502}
3497 3503
@@ -3665,7 +3671,7 @@ static int __init cdrom_init(void)
3665 3671
3666static void __exit cdrom_exit(void) 3672static void __exit cdrom_exit(void)
3667{ 3673{
3668 printk(KERN_INFO "Uniform CD-ROM driver unloaded\n"); 3674 pr_info("Uniform CD-ROM driver unloaded\n");
3669 cdrom_sysctl_unregister(); 3675 cdrom_sysctl_unregister();
3670} 3676}
3671 3677
diff --git a/drivers/cdrom/gdrom.c b/drivers/cdrom/gdrom.c
index 03c71f7698c..261107d1457 100644
--- a/drivers/cdrom/gdrom.c
+++ b/drivers/cdrom/gdrom.c
@@ -19,6 +19,8 @@
19 * 19 *
20 */ 20 */
21 21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
22#include <linux/init.h> 24#include <linux/init.h>
23#include <linux/module.h> 25#include <linux/module.h>
24#include <linux/fs.h> 26#include <linux/fs.h>
@@ -32,6 +34,7 @@
32#include <linux/blkdev.h> 34#include <linux/blkdev.h>
33#include <linux/interrupt.h> 35#include <linux/interrupt.h>
34#include <linux/device.h> 36#include <linux/device.h>
37#include <linux/smp_lock.h>
35#include <linux/wait.h> 38#include <linux/wait.h>
36#include <linux/workqueue.h> 39#include <linux/workqueue.h>
37#include <linux/platform_device.h> 40#include <linux/platform_device.h>
@@ -339,8 +342,7 @@ static int gdrom_get_last_session(struct cdrom_device_info *cd_info,
339 tocuse = 0; 342 tocuse = 0;
340 err = gdrom_readtoc_cmd(gd.toc, 0); 343 err = gdrom_readtoc_cmd(gd.toc, 0);
341 if (err) { 344 if (err) {
342 printk(KERN_INFO "GDROM: Could not get CD " 345 pr_info("Could not get CD table of contents\n");
343 "table of contents\n");
344 return -ENXIO; 346 return -ENXIO;
345 } 347 }
346 } 348 }
@@ -357,8 +359,7 @@ static int gdrom_get_last_session(struct cdrom_device_info *cd_info,
357 } while (track >= fentry); 359 } while (track >= fentry);
358 360
359 if ((track > 100) || (track < get_entry_track(gd.toc->first))) { 361 if ((track > 100) || (track < get_entry_track(gd.toc->first))) {
360 printk(KERN_INFO "GDROM: No data on the last " 362 pr_info("No data on the last session of the CD\n");
361 "session of the CD\n");
362 gdrom_getsense(NULL); 363 gdrom_getsense(NULL);
363 return -ENXIO; 364 return -ENXIO;
364 } 365 }
@@ -451,14 +452,14 @@ static int gdrom_getsense(short *bufstring)
451 goto cleanup_sense; 452 goto cleanup_sense;
452 insw(GDROM_DATA_REG, &sense, sense_command->buflen/2); 453 insw(GDROM_DATA_REG, &sense, sense_command->buflen/2);
453 if (sense[1] & 40) { 454 if (sense[1] & 40) {
454 printk(KERN_INFO "GDROM: Drive not ready - command aborted\n"); 455 pr_info("Drive not ready - command aborted\n");
455 goto cleanup_sense; 456 goto cleanup_sense;
456 } 457 }
457 sense_key = sense[1] & 0x0F; 458 sense_key = sense[1] & 0x0F;
458 if (sense_key < ARRAY_SIZE(sense_texts)) 459 if (sense_key < ARRAY_SIZE(sense_texts))
459 printk(KERN_INFO "GDROM: %s\n", sense_texts[sense_key].text); 460 pr_info("%s\n", sense_texts[sense_key].text);
460 else 461 else
461 printk(KERN_ERR "GDROM: Unknown sense key: %d\n", sense_key); 462 pr_err("Unknown sense key: %d\n", sense_key);
462 if (bufstring) /* return addional sense data */ 463 if (bufstring) /* return addional sense data */
463 memcpy(bufstring, &sense[4], 2); 464 memcpy(bufstring, &sense[4], 2);
464 if (sense_key < 2) 465 if (sense_key < 2)
@@ -492,12 +493,18 @@ static struct cdrom_device_ops gdrom_ops = {
492 493
493static int gdrom_bdops_open(struct block_device *bdev, fmode_t mode) 494static int gdrom_bdops_open(struct block_device *bdev, fmode_t mode)
494{ 495{
495 return cdrom_open(gd.cd_info, bdev, mode); 496 int ret;
497 lock_kernel();
498 ret = cdrom_open(gd.cd_info, bdev, mode);
499 unlock_kernel();
500 return ret;
496} 501}
497 502
498static int gdrom_bdops_release(struct gendisk *disk, fmode_t mode) 503static int gdrom_bdops_release(struct gendisk *disk, fmode_t mode)
499{ 504{
505 lock_kernel();
500 cdrom_release(gd.cd_info, mode); 506 cdrom_release(gd.cd_info, mode);
507 unlock_kernel();
501 return 0; 508 return 0;
502} 509}
503 510
@@ -509,7 +516,13 @@ static int gdrom_bdops_mediachanged(struct gendisk *disk)
509static int gdrom_bdops_ioctl(struct block_device *bdev, fmode_t mode, 516static int gdrom_bdops_ioctl(struct block_device *bdev, fmode_t mode,
510 unsigned cmd, unsigned long arg) 517 unsigned cmd, unsigned long arg)
511{ 518{
512 return cdrom_ioctl(gd.cd_info, bdev, mode, cmd, arg); 519 int ret;
520
521 lock_kernel();
522 ret = cdrom_ioctl(gd.cd_info, bdev, mode, cmd, arg);
523 unlock_kernel();
524
525 return ret;
513} 526}
514 527
515static const struct block_device_operations gdrom_bdops = { 528static const struct block_device_operations gdrom_bdops = {
@@ -517,7 +530,7 @@ static const struct block_device_operations gdrom_bdops = {
517 .open = gdrom_bdops_open, 530 .open = gdrom_bdops_open,
518 .release = gdrom_bdops_release, 531 .release = gdrom_bdops_release,
519 .media_changed = gdrom_bdops_mediachanged, 532 .media_changed = gdrom_bdops_mediachanged,
520 .locked_ioctl = gdrom_bdops_ioctl, 533 .ioctl = gdrom_bdops_ioctl,
521}; 534};
522 535
523static irqreturn_t gdrom_command_interrupt(int irq, void *dev_id) 536static irqreturn_t gdrom_command_interrupt(int irq, void *dev_id)
@@ -643,14 +656,13 @@ static void gdrom_request(struct request_queue *rq)
643 struct request *req; 656 struct request *req;
644 657
645 while ((req = blk_fetch_request(rq)) != NULL) { 658 while ((req = blk_fetch_request(rq)) != NULL) {
646 if (!blk_fs_request(req)) { 659 if (req->cmd_type != REQ_TYPE_FS) {
647 printk(KERN_DEBUG "GDROM: Non-fs request ignored\n"); 660 printk(KERN_DEBUG "gdrom: Non-fs request ignored\n");
648 __blk_end_request_all(req, -EIO); 661 __blk_end_request_all(req, -EIO);
649 continue; 662 continue;
650 } 663 }
651 if (rq_data_dir(req) != READ) { 664 if (rq_data_dir(req) != READ) {
652 printk(KERN_NOTICE "GDROM: Read only device -"); 665 pr_notice("Read only device - write request ignored\n");
653 printk(" write request ignored\n");
654 __blk_end_request_all(req, -EIO); 666 __blk_end_request_all(req, -EIO);
655 continue; 667 continue;
656 } 668 }
@@ -685,7 +697,7 @@ static int __devinit gdrom_outputversion(void)
685 firmw_ver = kstrndup(id->firmver, 16, GFP_KERNEL); 697 firmw_ver = kstrndup(id->firmver, 16, GFP_KERNEL);
686 if (!firmw_ver) 698 if (!firmw_ver)
687 goto free_manuf_name; 699 goto free_manuf_name;
688 printk(KERN_INFO "GDROM: %s from %s with firmware %s\n", 700 pr_info("%s from %s with firmware %s\n",
689 model_name, manuf_name, firmw_ver); 701 model_name, manuf_name, firmw_ver);
690 err = 0; 702 err = 0;
691 kfree(firmw_ver); 703 kfree(firmw_ver);
@@ -757,7 +769,7 @@ static int __devinit probe_gdrom(struct platform_device *devptr)
757 int err; 769 int err;
758 /* Start the device */ 770 /* Start the device */
759 if (gdrom_execute_diagnostic() != 1) { 771 if (gdrom_execute_diagnostic() != 1) {
760 printk(KERN_WARNING "GDROM: ATA Probe for GDROM failed.\n"); 772 pr_warning("ATA Probe for GDROM failed\n");
761 return -ENODEV; 773 return -ENODEV;
762 } 774 }
763 /* Print out firmware ID */ 775 /* Print out firmware ID */
@@ -767,7 +779,7 @@ static int __devinit probe_gdrom(struct platform_device *devptr)
767 gdrom_major = register_blkdev(0, GDROM_DEV_NAME); 779 gdrom_major = register_blkdev(0, GDROM_DEV_NAME);
768 if (gdrom_major <= 0) 780 if (gdrom_major <= 0)
769 return gdrom_major; 781 return gdrom_major;
770 printk(KERN_INFO "GDROM: Registered with major number %d\n", 782 pr_info("Registered with major number %d\n",
771 gdrom_major); 783 gdrom_major);
772 /* Specify basic properties of drive */ 784 /* Specify basic properties of drive */
773 gd.cd_info = kzalloc(sizeof(struct cdrom_device_info), GFP_KERNEL); 785 gd.cd_info = kzalloc(sizeof(struct cdrom_device_info), GFP_KERNEL);
@@ -818,7 +830,7 @@ probe_fail_no_disk:
818 unregister_blkdev(gdrom_major, GDROM_DEV_NAME); 830 unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
819 gdrom_major = 0; 831 gdrom_major = 0;
820probe_fail_no_mem: 832probe_fail_no_mem:
821 printk(KERN_WARNING "GDROM: Probe failed - error is 0x%X\n", err); 833 pr_warning("Probe failed - error is 0x%X\n", err);
822 return err; 834 return err;
823} 835}
824 836
diff --git a/drivers/cdrom/viocd.c b/drivers/cdrom/viocd.c
index 451cd7071b1..56bf9f44700 100644
--- a/drivers/cdrom/viocd.c
+++ b/drivers/cdrom/viocd.c
@@ -31,6 +31,8 @@
31 * the OS/400 partition. 31 * the OS/400 partition.
32 */ 32 */
33 33
34#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
34#include <linux/major.h> 36#include <linux/major.h>
35#include <linux/blkdev.h> 37#include <linux/blkdev.h>
36#include <linux/cdrom.h> 38#include <linux/cdrom.h>
@@ -40,6 +42,7 @@
40#include <linux/module.h> 42#include <linux/module.h>
41#include <linux/completion.h> 43#include <linux/completion.h>
42#include <linux/proc_fs.h> 44#include <linux/proc_fs.h>
45#include <linux/smp_lock.h>
43#include <linux/seq_file.h> 46#include <linux/seq_file.h>
44#include <linux/scatterlist.h> 47#include <linux/scatterlist.h>
45 48
@@ -53,9 +56,6 @@
53 56
54#define VIOCD_VERS "1.06" 57#define VIOCD_VERS "1.06"
55 58
56#define VIOCD_KERN_WARNING KERN_WARNING "viocd: "
57#define VIOCD_KERN_INFO KERN_INFO "viocd: "
58
59/* 59/*
60 * Should probably make this a module parameter....sigh 60 * Should probably make this a module parameter....sigh
61 */ 61 */
@@ -154,13 +154,21 @@ static const struct file_operations proc_viocd_operations = {
154static int viocd_blk_open(struct block_device *bdev, fmode_t mode) 154static int viocd_blk_open(struct block_device *bdev, fmode_t mode)
155{ 155{
156 struct disk_info *di = bdev->bd_disk->private_data; 156 struct disk_info *di = bdev->bd_disk->private_data;
157 return cdrom_open(&di->viocd_info, bdev, mode); 157 int ret;
158
159 lock_kernel();
160 ret = cdrom_open(&di->viocd_info, bdev, mode);
161 unlock_kernel();
162
163 return ret;
158} 164}
159 165
160static int viocd_blk_release(struct gendisk *disk, fmode_t mode) 166static int viocd_blk_release(struct gendisk *disk, fmode_t mode)
161{ 167{
162 struct disk_info *di = disk->private_data; 168 struct disk_info *di = disk->private_data;
169 lock_kernel();
163 cdrom_release(&di->viocd_info, mode); 170 cdrom_release(&di->viocd_info, mode);
171 unlock_kernel();
164 return 0; 172 return 0;
165} 173}
166 174
@@ -168,7 +176,13 @@ static int viocd_blk_ioctl(struct block_device *bdev, fmode_t mode,
168 unsigned cmd, unsigned long arg) 176 unsigned cmd, unsigned long arg)
169{ 177{
170 struct disk_info *di = bdev->bd_disk->private_data; 178 struct disk_info *di = bdev->bd_disk->private_data;
171 return cdrom_ioctl(&di->viocd_info, bdev, mode, cmd, arg); 179 int ret;
180
181 lock_kernel();
182 ret = cdrom_ioctl(&di->viocd_info, bdev, mode, cmd, arg);
183 unlock_kernel();
184
185 return ret;
172} 186}
173 187
174static int viocd_blk_media_changed(struct gendisk *disk) 188static int viocd_blk_media_changed(struct gendisk *disk)
@@ -181,7 +195,7 @@ static const struct block_device_operations viocd_fops = {
181 .owner = THIS_MODULE, 195 .owner = THIS_MODULE,
182 .open = viocd_blk_open, 196 .open = viocd_blk_open,
183 .release = viocd_blk_release, 197 .release = viocd_blk_release,
184 .locked_ioctl = viocd_blk_ioctl, 198 .ioctl = viocd_blk_ioctl,
185 .media_changed = viocd_blk_media_changed, 199 .media_changed = viocd_blk_media_changed,
186}; 200};
187 201
@@ -202,9 +216,8 @@ static int viocd_open(struct cdrom_device_info *cdi, int purpose)
202 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48), 216 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
203 0, 0, 0); 217 0, 0, 0);
204 if (hvrc != 0) { 218 if (hvrc != 0) {
205 printk(VIOCD_KERN_WARNING 219 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
206 "bad rc on HvCallEvent_signalLpEventFast %d\n", 220 (int)hvrc);
207 (int)hvrc);
208 return -EIO; 221 return -EIO;
209 } 222 }
210 223
@@ -213,8 +226,8 @@ static int viocd_open(struct cdrom_device_info *cdi, int purpose)
213 if (we.rc) { 226 if (we.rc) {
214 const struct vio_error_entry *err = 227 const struct vio_error_entry *err =
215 vio_lookup_rc(viocd_err_table, we.sub_result); 228 vio_lookup_rc(viocd_err_table, we.sub_result);
216 printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on open: %s\n", 229 pr_warning("bad rc %d:0x%04X on open: %s\n",
217 we.rc, we.sub_result, err->msg); 230 we.rc, we.sub_result, err->msg);
218 return -err->errno; 231 return -err->errno;
219 } 232 }
220 233
@@ -234,9 +247,8 @@ static void viocd_release(struct cdrom_device_info *cdi)
234 viopath_targetinst(viopath_hostLp), 0, 247 viopath_targetinst(viopath_hostLp), 0,
235 VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0); 248 VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0);
236 if (hvrc != 0) 249 if (hvrc != 0)
237 printk(VIOCD_KERN_WARNING 250 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
238 "bad rc on HvCallEvent_signalLpEventFast %d\n", 251 (int)hvrc);
239 (int)hvrc);
240} 252}
241 253
242/* Send a read or write request to OS/400 */ 254/* Send a read or write request to OS/400 */
@@ -262,13 +274,12 @@ static int send_request(struct request *req)
262 274
263 sg_init_table(&sg, 1); 275 sg_init_table(&sg, 1);
264 if (blk_rq_map_sg(req->q, req, &sg) == 0) { 276 if (blk_rq_map_sg(req->q, req, &sg) == 0) {
265 printk(VIOCD_KERN_WARNING 277 pr_warning("error setting up scatter/gather list\n");
266 "error setting up scatter/gather list\n");
267 return -1; 278 return -1;
268 } 279 }
269 280
270 if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) { 281 if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) {
271 printk(VIOCD_KERN_WARNING "error allocating sg tce\n"); 282 pr_warning("error allocating sg tce\n");
272 return -1; 283 return -1;
273 } 284 }
274 dmaaddr = sg_dma_address(&sg); 285 dmaaddr = sg_dma_address(&sg);
@@ -284,7 +295,7 @@ static int send_request(struct request *req)
284 ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr, 295 ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
285 (u64)blk_rq_pos(req) * 512, len, 0); 296 (u64)blk_rq_pos(req) * 512, len, 0);
286 if (hvrc != HvLpEvent_Rc_Good) { 297 if (hvrc != HvLpEvent_Rc_Good) {
287 printk(VIOCD_KERN_WARNING "hv error on op %d\n", (int)hvrc); 298 pr_warning("hv error on op %d\n", (int)hvrc);
288 return -1; 299 return -1;
289 } 300 }
290 301
@@ -298,11 +309,10 @@ static void do_viocd_request(struct request_queue *q)
298 struct request *req; 309 struct request *req;
299 310
300 while ((rwreq == 0) && ((req = blk_fetch_request(q)) != NULL)) { 311 while ((rwreq == 0) && ((req = blk_fetch_request(q)) != NULL)) {
301 if (!blk_fs_request(req)) 312 if (req->cmd_type != REQ_TYPE_FS)
302 __blk_end_request_all(req, -EIO); 313 __blk_end_request_all(req, -EIO);
303 else if (send_request(req) < 0) { 314 else if (send_request(req) < 0) {
304 printk(VIOCD_KERN_WARNING 315 pr_warning("unable to send message to OS/400!\n");
305 "unable to send message to OS/400!");
306 __blk_end_request_all(req, -EIO); 316 __blk_end_request_all(req, -EIO);
307 } else 317 } else
308 rwreq++; 318 rwreq++;
@@ -327,8 +337,8 @@ static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
327 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48), 337 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
328 0, 0, 0); 338 0, 0, 0);
329 if (hvrc != 0) { 339 if (hvrc != 0) {
330 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n", 340 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
331 (int)hvrc); 341 (int)hvrc);
332 return -EIO; 342 return -EIO;
333 } 343 }
334 344
@@ -338,9 +348,8 @@ static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
338 if (we.rc) { 348 if (we.rc) {
339 const struct vio_error_entry *err = 349 const struct vio_error_entry *err =
340 vio_lookup_rc(viocd_err_table, we.sub_result); 350 vio_lookup_rc(viocd_err_table, we.sub_result);
341 printk(VIOCD_KERN_WARNING 351 pr_warning("bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
342 "bad rc %d:0x%04X on check_change: %s; Assuming no change\n", 352 we.rc, we.sub_result, err->msg);
343 we.rc, we.sub_result, err->msg);
344 return 0; 353 return 0;
345 } 354 }
346 355
@@ -367,8 +376,8 @@ static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
367 (u64)&we, VIOVERSION << 16, 376 (u64)&we, VIOVERSION << 16,
368 (device_no << 48) | (flags << 32), 0, 0, 0); 377 (device_no << 48) | (flags << 32), 0, 0, 0);
369 if (hvrc != 0) { 378 if (hvrc != 0) {
370 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n", 379 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
371 (int)hvrc); 380 (int)hvrc);
372 return -EIO; 381 return -EIO;
373 } 382 }
374 383
@@ -455,8 +464,7 @@ static void vio_handle_cd_event(struct HvLpEvent *event)
455 return; 464 return;
456 /* First, we should NEVER get an int here...only acks */ 465 /* First, we should NEVER get an int here...only acks */
457 if (hvlpevent_is_int(event)) { 466 if (hvlpevent_is_int(event)) {
458 printk(VIOCD_KERN_WARNING 467 pr_warning("Yikes! got an int in viocd event handler!\n");
459 "Yikes! got an int in viocd event handler!\n");
460 if (hvlpevent_need_ack(event)) { 468 if (hvlpevent_need_ack(event)) {
461 event->xRc = HvLpEvent_Rc_InvalidSubtype; 469 event->xRc = HvLpEvent_Rc_InvalidSubtype;
462 HvCallEvent_ackLpEvent(event); 470 HvCallEvent_ackLpEvent(event);
@@ -510,10 +518,9 @@ return_complete:
510 const struct vio_error_entry *err = 518 const struct vio_error_entry *err =
511 vio_lookup_rc(viocd_err_table, 519 vio_lookup_rc(viocd_err_table,
512 bevent->sub_result); 520 bevent->sub_result);
513 printk(VIOCD_KERN_WARNING "request %p failed " 521 pr_warning("request %p failed with rc %d:0x%04X: %s\n",
514 "with rc %d:0x%04X: %s\n", 522 req, event->xRc,
515 req, event->xRc, 523 bevent->sub_result, err->msg);
516 bevent->sub_result, err->msg);
517 __blk_end_request_all(req, -EIO); 524 __blk_end_request_all(req, -EIO);
518 } else 525 } else
519 __blk_end_request_all(req, 0); 526 __blk_end_request_all(req, 0);
@@ -524,9 +531,8 @@ return_complete:
524 break; 531 break;
525 532
526 default: 533 default:
527 printk(VIOCD_KERN_WARNING 534 pr_warning("message with invalid subtype %0x04X!\n",
528 "message with invalid subtype %0x04X!\n", 535 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
529 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
530 if (hvlpevent_need_ack(event)) { 536 if (hvlpevent_need_ack(event)) {
531 event->xRc = HvLpEvent_Rc_InvalidSubtype; 537 event->xRc = HvLpEvent_Rc_InvalidSubtype;
532 HvCallEvent_ackLpEvent(event); 538 HvCallEvent_ackLpEvent(event);
@@ -593,23 +599,19 @@ static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
593 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno); 599 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
594 600
595 if (register_cdrom(c) != 0) { 601 if (register_cdrom(c) != 0) {
596 printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n", 602 pr_warning("Cannot register viocd CD-ROM %s!\n", c->name);
597 c->name);
598 goto out; 603 goto out;
599 } 604 }
600 printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s " 605 pr_info("cd %s is iSeries resource %10.10s type %4.4s, model %3.3s\n",
601 "type %4.4s, model %3.3s\n", 606 c->name, d->rsrcname, d->type, d->model);
602 c->name, d->rsrcname, d->type, d->model);
603 q = blk_init_queue(do_viocd_request, &viocd_reqlock); 607 q = blk_init_queue(do_viocd_request, &viocd_reqlock);
604 if (q == NULL) { 608 if (q == NULL) {
605 printk(VIOCD_KERN_WARNING "Cannot allocate queue for %s!\n", 609 pr_warning("Cannot allocate queue for %s!\n", c->name);
606 c->name);
607 goto out_unregister_cdrom; 610 goto out_unregister_cdrom;
608 } 611 }
609 gendisk = alloc_disk(1); 612 gendisk = alloc_disk(1);
610 if (gendisk == NULL) { 613 if (gendisk == NULL) {
611 printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n", 614 pr_warning("Cannot create gendisk for %s!\n", c->name);
612 c->name);
613 goto out_cleanup_queue; 615 goto out_cleanup_queue;
614 } 616 }
615 gendisk->major = VIOCD_MAJOR; 617 gendisk->major = VIOCD_MAJOR;
@@ -682,21 +684,19 @@ static int __init viocd_init(void)
682 return -ENODEV; 684 return -ENODEV;
683 } 685 }
684 686
685 printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n", 687 pr_info("vers " VIOCD_VERS ", hosting partition %d\n", viopath_hostLp);
686 viopath_hostLp);
687 688
688 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) { 689 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
689 printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n", 690 pr_warning("Unable to get major %d for %s\n",
690 VIOCD_MAJOR, VIOCD_DEVICE); 691 VIOCD_MAJOR, VIOCD_DEVICE);
691 return -EIO; 692 return -EIO;
692 } 693 }
693 694
694 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio, 695 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
695 MAX_CD_REQ + 2); 696 MAX_CD_REQ + 2);
696 if (ret) { 697 if (ret) {
697 printk(VIOCD_KERN_WARNING 698 pr_warning("error opening path to host partition %d\n",
698 "error opening path to host partition %d\n", 699 viopath_hostLp);
699 viopath_hostLp);
700 goto out_unregister; 700 goto out_unregister;
701 } 701 }
702 702
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index 273cee1cc77..dc964166060 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -9,6 +9,7 @@ FONTMAPFILE = cp437.uni
9 9
10obj-y += mem.o random.o tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o tty_buffer.o tty_port.o 10obj-y += mem.o random.o tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o tty_buffer.o tty_port.o
11 11
12obj-y += tty_mutex.o
12obj-$(CONFIG_LEGACY_PTYS) += pty.o 13obj-$(CONFIG_LEGACY_PTYS) += pty.o
13obj-$(CONFIG_UNIX98_PTYS) += pty.o 14obj-$(CONFIG_UNIX98_PTYS) += pty.o
14obj-y += misc.o 15obj-y += misc.o
diff --git a/drivers/char/amiserial.c b/drivers/char/amiserial.c
index 4f8d60c25a9..a11c8c9ca3d 100644
--- a/drivers/char/amiserial.c
+++ b/drivers/char/amiserial.c
@@ -1072,7 +1072,7 @@ static int get_serial_info(struct async_struct * info,
1072 if (!retinfo) 1072 if (!retinfo)
1073 return -EFAULT; 1073 return -EFAULT;
1074 memset(&tmp, 0, sizeof(tmp)); 1074 memset(&tmp, 0, sizeof(tmp));
1075 lock_kernel(); 1075 tty_lock();
1076 tmp.type = state->type; 1076 tmp.type = state->type;
1077 tmp.line = state->line; 1077 tmp.line = state->line;
1078 tmp.port = state->port; 1078 tmp.port = state->port;
@@ -1083,7 +1083,7 @@ static int get_serial_info(struct async_struct * info,
1083 tmp.close_delay = state->close_delay; 1083 tmp.close_delay = state->close_delay;
1084 tmp.closing_wait = state->closing_wait; 1084 tmp.closing_wait = state->closing_wait;
1085 tmp.custom_divisor = state->custom_divisor; 1085 tmp.custom_divisor = state->custom_divisor;
1086 unlock_kernel(); 1086 tty_unlock();
1087 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) 1087 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1088 return -EFAULT; 1088 return -EFAULT;
1089 return 0; 1089 return 0;
@@ -1100,14 +1100,14 @@ static int set_serial_info(struct async_struct * info,
1100 if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) 1100 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1101 return -EFAULT; 1101 return -EFAULT;
1102 1102
1103 lock_kernel(); 1103 tty_lock();
1104 state = info->state; 1104 state = info->state;
1105 old_state = *state; 1105 old_state = *state;
1106 1106
1107 change_irq = new_serial.irq != state->irq; 1107 change_irq = new_serial.irq != state->irq;
1108 change_port = (new_serial.port != state->port); 1108 change_port = (new_serial.port != state->port);
1109 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) { 1109 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
1110 unlock_kernel(); 1110 tty_unlock();
1111 return -EINVAL; 1111 return -EINVAL;
1112 } 1112 }
1113 1113
@@ -1127,7 +1127,7 @@ static int set_serial_info(struct async_struct * info,
1127 } 1127 }
1128 1128
1129 if (new_serial.baud_base < 9600) { 1129 if (new_serial.baud_base < 9600) {
1130 unlock_kernel(); 1130 tty_unlock();
1131 return -EINVAL; 1131 return -EINVAL;
1132 } 1132 }
1133 1133
@@ -1163,7 +1163,7 @@ check_and_exit:
1163 } 1163 }
1164 } else 1164 } else
1165 retval = startup(info); 1165 retval = startup(info);
1166 unlock_kernel(); 1166 tty_unlock();
1167 return retval; 1167 return retval;
1168} 1168}
1169 1169
@@ -1528,6 +1528,7 @@ static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1528{ 1528{
1529 struct async_struct * info = tty->driver_data; 1529 struct async_struct * info = tty->driver_data;
1530 unsigned long orig_jiffies, char_time; 1530 unsigned long orig_jiffies, char_time;
1531 int tty_was_locked = tty_locked();
1531 int lsr; 1532 int lsr;
1532 1533
1533 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent")) 1534 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
@@ -1538,7 +1539,12 @@ static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1538 1539
1539 orig_jiffies = jiffies; 1540 orig_jiffies = jiffies;
1540 1541
1541 lock_kernel(); 1542 /*
1543 * tty_wait_until_sent is called from lots of places,
1544 * with or without the BTM.
1545 */
1546 if (!tty_was_locked)
1547 tty_lock();
1542 /* 1548 /*
1543 * Set the check interval to be 1/5 of the estimated time to 1549 * Set the check interval to be 1/5 of the estimated time to
1544 * send a single character, and make it at least 1. The check 1550 * send a single character, and make it at least 1. The check
@@ -1579,7 +1585,8 @@ static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1579 break; 1585 break;
1580 } 1586 }
1581 __set_current_state(TASK_RUNNING); 1587 __set_current_state(TASK_RUNNING);
1582 unlock_kernel(); 1588 if (!tty_was_locked)
1589 tty_unlock();
1583#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT 1590#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1584 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies); 1591 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1585#endif 1592#endif
@@ -1703,7 +1710,9 @@ static int block_til_ready(struct tty_struct *tty, struct file * filp,
1703 printk("block_til_ready blocking: ttys%d, count = %d\n", 1710 printk("block_til_ready blocking: ttys%d, count = %d\n",
1704 info->line, state->count); 1711 info->line, state->count);
1705#endif 1712#endif
1713 tty_unlock();
1706 schedule(); 1714 schedule();
1715 tty_lock();
1707 } 1716 }
1708 __set_current_state(TASK_RUNNING); 1717 __set_current_state(TASK_RUNNING);
1709 remove_wait_queue(&info->open_wait, &wait); 1718 remove_wait_queue(&info->open_wait, &wait);
diff --git a/drivers/char/briq_panel.c b/drivers/char/briq_panel.c
index 555cd93c2ee..d5fa113afe3 100644
--- a/drivers/char/briq_panel.c
+++ b/drivers/char/briq_panel.c
@@ -67,15 +67,15 @@ static void set_led(char state)
67 67
68static int briq_panel_open(struct inode *ino, struct file *filep) 68static int briq_panel_open(struct inode *ino, struct file *filep)
69{ 69{
70 lock_kernel(); 70 tty_lock();
71 /* enforce single access, vfd_is_open is protected by BKL */ 71 /* enforce single access, vfd_is_open is protected by BKL */
72 if (vfd_is_open) { 72 if (vfd_is_open) {
73 unlock_kernel(); 73 tty_unlock();
74 return -EBUSY; 74 return -EBUSY;
75 } 75 }
76 vfd_is_open = 1; 76 vfd_is_open = 1;
77 77
78 unlock_kernel(); 78 tty_unlock();
79 return 0; 79 return 0;
80} 80}
81 81
diff --git a/drivers/char/cyclades.c b/drivers/char/cyclades.c
index 9824b416290..27aad942233 100644
--- a/drivers/char/cyclades.c
+++ b/drivers/char/cyclades.c
@@ -65,7 +65,6 @@
65#include <linux/tty.h> 65#include <linux/tty.h>
66#include <linux/tty_flip.h> 66#include <linux/tty_flip.h>
67#include <linux/serial.h> 67#include <linux/serial.h>
68#include <linux/smp_lock.h>
69#include <linux/major.h> 68#include <linux/major.h>
70#include <linux/string.h> 69#include <linux/string.h>
71#include <linux/fcntl.h> 70#include <linux/fcntl.h>
@@ -1608,7 +1607,7 @@ static int cy_open(struct tty_struct *tty, struct file *filp)
1608 * If the port is the middle of closing, bail out now 1607 * If the port is the middle of closing, bail out now
1609 */ 1608 */
1610 if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) { 1609 if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) {
1611 wait_event_interruptible(info->port.close_wait, 1610 wait_event_interruptible_tty(info->port.close_wait,
1612 !(info->port.flags & ASYNC_CLOSING)); 1611 !(info->port.flags & ASYNC_CLOSING));
1613 return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS; 1612 return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
1614 } 1613 }
@@ -1655,7 +1654,6 @@ static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
1655 return; /* Just in case.... */ 1654 return; /* Just in case.... */
1656 1655
1657 orig_jiffies = jiffies; 1656 orig_jiffies = jiffies;
1658 lock_kernel();
1659 /* 1657 /*
1660 * Set the check interval to be 1/5 of the estimated time to 1658 * Set the check interval to be 1/5 of the estimated time to
1661 * send a single character, and make it at least 1. The check 1659 * send a single character, and make it at least 1. The check
@@ -1702,7 +1700,6 @@ static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
1702 } 1700 }
1703 /* Run one more char cycle */ 1701 /* Run one more char cycle */
1704 msleep_interruptible(jiffies_to_msecs(char_time * 5)); 1702 msleep_interruptible(jiffies_to_msecs(char_time * 5));
1705 unlock_kernel();
1706#ifdef CY_DEBUG_WAIT_UNTIL_SENT 1703#ifdef CY_DEBUG_WAIT_UNTIL_SENT
1707 printk(KERN_DEBUG "Clean (jiff=%lu)...done\n", jiffies); 1704 printk(KERN_DEBUG "Clean (jiff=%lu)...done\n", jiffies);
1708#endif 1705#endif
@@ -1959,7 +1956,6 @@ static int cy_chars_in_buffer(struct tty_struct *tty)
1959 int char_count; 1956 int char_count;
1960 __u32 tx_put, tx_get, tx_bufsize; 1957 __u32 tx_put, tx_get, tx_bufsize;
1961 1958
1962 lock_kernel();
1963 tx_get = readl(&buf_ctrl->tx_get); 1959 tx_get = readl(&buf_ctrl->tx_get);
1964 tx_put = readl(&buf_ctrl->tx_put); 1960 tx_put = readl(&buf_ctrl->tx_put);
1965 tx_bufsize = readl(&buf_ctrl->tx_bufsize); 1961 tx_bufsize = readl(&buf_ctrl->tx_bufsize);
@@ -1971,7 +1967,6 @@ static int cy_chars_in_buffer(struct tty_struct *tty)
1971 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n", 1967 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
1972 info->line, info->xmit_cnt + char_count); 1968 info->line, info->xmit_cnt + char_count);
1973#endif 1969#endif
1974 unlock_kernel();
1975 return info->xmit_cnt + char_count; 1970 return info->xmit_cnt + char_count;
1976 } 1971 }
1977#endif /* Z_EXT_CHARS_IN_BUFFER */ 1972#endif /* Z_EXT_CHARS_IN_BUFFER */
@@ -2359,17 +2354,22 @@ cy_set_serial_info(struct cyclades_port *info, struct tty_struct *tty,
2359 struct serial_struct __user *new_info) 2354 struct serial_struct __user *new_info)
2360{ 2355{
2361 struct serial_struct new_serial; 2356 struct serial_struct new_serial;
2357 int ret;
2362 2358
2363 if (copy_from_user(&new_serial, new_info, sizeof(new_serial))) 2359 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2364 return -EFAULT; 2360 return -EFAULT;
2365 2361
2362 mutex_lock(&info->port.mutex);
2366 if (!capable(CAP_SYS_ADMIN)) { 2363 if (!capable(CAP_SYS_ADMIN)) {
2367 if (new_serial.close_delay != info->port.close_delay || 2364 if (new_serial.close_delay != info->port.close_delay ||
2368 new_serial.baud_base != info->baud || 2365 new_serial.baud_base != info->baud ||
2369 (new_serial.flags & ASYNC_FLAGS & 2366 (new_serial.flags & ASYNC_FLAGS &
2370 ~ASYNC_USR_MASK) != 2367 ~ASYNC_USR_MASK) !=
2371 (info->port.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK)) 2368 (info->port.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK))
2369 {
2370 mutex_unlock(&info->port.mutex);
2372 return -EPERM; 2371 return -EPERM;
2372 }
2373 info->port.flags = (info->port.flags & ~ASYNC_USR_MASK) | 2373 info->port.flags = (info->port.flags & ~ASYNC_USR_MASK) |
2374 (new_serial.flags & ASYNC_USR_MASK); 2374 (new_serial.flags & ASYNC_USR_MASK);
2375 info->baud = new_serial.baud_base; 2375 info->baud = new_serial.baud_base;
@@ -2392,10 +2392,12 @@ cy_set_serial_info(struct cyclades_port *info, struct tty_struct *tty,
2392check_and_exit: 2392check_and_exit:
2393 if (info->port.flags & ASYNC_INITIALIZED) { 2393 if (info->port.flags & ASYNC_INITIALIZED) {
2394 cy_set_line_char(info, tty); 2394 cy_set_line_char(info, tty);
2395 return 0; 2395 ret = 0;
2396 } else { 2396 } else {
2397 return cy_startup(info, tty); 2397 ret = cy_startup(info, tty);
2398 } 2398 }
2399 mutex_unlock(&info->port.mutex);
2400 return ret;
2399} /* set_serial_info */ 2401} /* set_serial_info */
2400 2402
2401/* 2403/*
@@ -2438,7 +2440,6 @@ static int cy_tiocmget(struct tty_struct *tty, struct file *file)
2438 2440
2439 card = info->card; 2441 card = info->card;
2440 2442
2441 lock_kernel();
2442 if (!cy_is_Z(card)) { 2443 if (!cy_is_Z(card)) {
2443 unsigned long flags; 2444 unsigned long flags;
2444 int channel = info->line - card->first_line; 2445 int channel = info->line - card->first_line;
@@ -2478,7 +2479,6 @@ static int cy_tiocmget(struct tty_struct *tty, struct file *file)
2478 ((lstatus & C_RS_CTS) ? TIOCM_CTS : 0); 2479 ((lstatus & C_RS_CTS) ? TIOCM_CTS : 0);
2479 } 2480 }
2480end: 2481end:
2481 unlock_kernel();
2482 return result; 2482 return result;
2483} /* cy_tiomget */ 2483} /* cy_tiomget */
2484 2484
@@ -2696,7 +2696,6 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
2696 printk(KERN_DEBUG "cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n", 2696 printk(KERN_DEBUG "cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n",
2697 info->line, cmd, arg); 2697 info->line, cmd, arg);
2698#endif 2698#endif
2699 lock_kernel();
2700 2699
2701 switch (cmd) { 2700 switch (cmd) {
2702 case CYGETMON: 2701 case CYGETMON:
@@ -2817,7 +2816,6 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
2817 default: 2816 default:
2818 ret_val = -ENOIOCTLCMD; 2817 ret_val = -ENOIOCTLCMD;
2819 } 2818 }
2820 unlock_kernel();
2821 2819
2822#ifdef CY_DEBUG_OTHER 2820#ifdef CY_DEBUG_OTHER
2823 printk(KERN_DEBUG "cyc:cy_ioctl done\n"); 2821 printk(KERN_DEBUG "cyc:cy_ioctl done\n");
diff --git a/drivers/char/epca.c b/drivers/char/epca.c
index 6f5ffe1320f..d9df46aa0fb 100644
--- a/drivers/char/epca.c
+++ b/drivers/char/epca.c
@@ -36,7 +36,7 @@
36#include <linux/ctype.h> 36#include <linux/ctype.h>
37#include <linux/tty.h> 37#include <linux/tty.h>
38#include <linux/tty_flip.h> 38#include <linux/tty_flip.h>
39#include <linux/smp_lock.h> 39#include <linux/slab.h>
40#include <linux/ioport.h> 40#include <linux/ioport.h>
41#include <linux/interrupt.h> 41#include <linux/interrupt.h>
42#include <linux/uaccess.h> 42#include <linux/uaccess.h>
@@ -2105,7 +2105,6 @@ static int pc_ioctl(struct tty_struct *tty, struct file *file,
2105 break; 2105 break;
2106 case DIGI_SETAW: 2106 case DIGI_SETAW:
2107 case DIGI_SETAF: 2107 case DIGI_SETAF:
2108 lock_kernel();
2109 if (cmd == DIGI_SETAW) { 2108 if (cmd == DIGI_SETAW) {
2110 /* Setup an event to indicate when the transmit 2109 /* Setup an event to indicate when the transmit
2111 buffer empties */ 2110 buffer empties */
@@ -2118,7 +2117,6 @@ static int pc_ioctl(struct tty_struct *tty, struct file *file,
2118 if (tty->ldisc->ops->flush_buffer) 2117 if (tty->ldisc->ops->flush_buffer)
2119 tty->ldisc->ops->flush_buffer(tty); 2118 tty->ldisc->ops->flush_buffer(tty);
2120 } 2119 }
2121 unlock_kernel();
2122 /* Fall Thru */ 2120 /* Fall Thru */
2123 case DIGI_SETA: 2121 case DIGI_SETA:
2124 if (copy_from_user(&ch->digiext, argp, sizeof(digi_t))) 2122 if (copy_from_user(&ch->digiext, argp, sizeof(digi_t)))
diff --git a/drivers/char/ip2/ip2main.c b/drivers/char/ip2/ip2main.c
index 911e1da6def..07f3ea38b58 100644
--- a/drivers/char/ip2/ip2main.c
+++ b/drivers/char/ip2/ip2main.c
@@ -1486,7 +1486,9 @@ ip2_open( PTTY tty, struct file *pFile )
1486 1486
1487 if ( tty_hung_up_p(pFile) || ( pCh->flags & ASYNC_CLOSING )) { 1487 if ( tty_hung_up_p(pFile) || ( pCh->flags & ASYNC_CLOSING )) {
1488 if ( pCh->flags & ASYNC_CLOSING ) { 1488 if ( pCh->flags & ASYNC_CLOSING ) {
1489 tty_unlock();
1489 schedule(); 1490 schedule();
1491 tty_lock();
1490 } 1492 }
1491 if ( tty_hung_up_p(pFile) ) { 1493 if ( tty_hung_up_p(pFile) ) {
1492 set_current_state( TASK_RUNNING ); 1494 set_current_state( TASK_RUNNING );
@@ -1548,7 +1550,9 @@ ip2_open( PTTY tty, struct file *pFile )
1548 rc = (( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EAGAIN : -ERESTARTSYS); 1550 rc = (( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EAGAIN : -ERESTARTSYS);
1549 break; 1551 break;
1550 } 1552 }
1553 tty_unlock();
1551 schedule(); 1554 schedule();
1555 tty_lock();
1552 } 1556 }
1553 set_current_state( TASK_RUNNING ); 1557 set_current_state( TASK_RUNNING );
1554 remove_wait_queue(&pCh->open_wait, &wait); 1558 remove_wait_queue(&pCh->open_wait, &wait);
diff --git a/drivers/char/isicom.c b/drivers/char/isicom.c
index 98310e1aae3..c27e9d21fea 100644
--- a/drivers/char/isicom.c
+++ b/drivers/char/isicom.c
@@ -124,7 +124,6 @@
124#include <linux/fs.h> 124#include <linux/fs.h>
125#include <linux/sched.h> 125#include <linux/sched.h>
126#include <linux/serial.h> 126#include <linux/serial.h>
127#include <linux/smp_lock.h>
128#include <linux/mm.h> 127#include <linux/mm.h>
129#include <linux/interrupt.h> 128#include <linux/interrupt.h>
130#include <linux/timer.h> 129#include <linux/timer.h>
@@ -872,7 +871,6 @@ static struct tty_port *isicom_find_port(struct tty_struct *tty)
872static int isicom_open(struct tty_struct *tty, struct file *filp) 871static int isicom_open(struct tty_struct *tty, struct file *filp)
873{ 872{
874 struct isi_port *port; 873 struct isi_port *port;
875 struct isi_board *card;
876 struct tty_port *tport; 874 struct tty_port *tport;
877 875
878 tport = isicom_find_port(tty); 876 tport = isicom_find_port(tty);
@@ -1118,8 +1116,7 @@ static int isicom_set_serial_info(struct tty_struct *tty,
1118 if (copy_from_user(&newinfo, info, sizeof(newinfo))) 1116 if (copy_from_user(&newinfo, info, sizeof(newinfo)))
1119 return -EFAULT; 1117 return -EFAULT;
1120 1118
1121 lock_kernel(); 1119 mutex_lock(&port->port.mutex);
1122
1123 reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) != 1120 reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
1124 (newinfo.flags & ASYNC_SPD_MASK)); 1121 (newinfo.flags & ASYNC_SPD_MASK));
1125 1122
@@ -1128,7 +1125,7 @@ static int isicom_set_serial_info(struct tty_struct *tty,
1128 (newinfo.closing_wait != port->port.closing_wait) || 1125 (newinfo.closing_wait != port->port.closing_wait) ||
1129 ((newinfo.flags & ~ASYNC_USR_MASK) != 1126 ((newinfo.flags & ~ASYNC_USR_MASK) !=
1130 (port->port.flags & ~ASYNC_USR_MASK))) { 1127 (port->port.flags & ~ASYNC_USR_MASK))) {
1131 unlock_kernel(); 1128 mutex_unlock(&port->port.mutex);
1132 return -EPERM; 1129 return -EPERM;
1133 } 1130 }
1134 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) | 1131 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
@@ -1145,7 +1142,7 @@ static int isicom_set_serial_info(struct tty_struct *tty,
1145 isicom_config_port(tty); 1142 isicom_config_port(tty);
1146 spin_unlock_irqrestore(&port->card->card_lock, flags); 1143 spin_unlock_irqrestore(&port->card->card_lock, flags);
1147 } 1144 }
1148 unlock_kernel(); 1145 mutex_unlock(&port->port.mutex);
1149 return 0; 1146 return 0;
1150} 1147}
1151 1148
@@ -1154,7 +1151,7 @@ static int isicom_get_serial_info(struct isi_port *port,
1154{ 1151{
1155 struct serial_struct out_info; 1152 struct serial_struct out_info;
1156 1153
1157 lock_kernel(); 1154 mutex_lock(&port->port.mutex);
1158 memset(&out_info, 0, sizeof(out_info)); 1155 memset(&out_info, 0, sizeof(out_info));
1159/* out_info.type = ? */ 1156/* out_info.type = ? */
1160 out_info.line = port - isi_ports; 1157 out_info.line = port - isi_ports;
@@ -1164,7 +1161,7 @@ static int isicom_get_serial_info(struct isi_port *port,
1164/* out_info.baud_base = ? */ 1161/* out_info.baud_base = ? */
1165 out_info.close_delay = port->port.close_delay; 1162 out_info.close_delay = port->port.close_delay;
1166 out_info.closing_wait = port->port.closing_wait; 1163 out_info.closing_wait = port->port.closing_wait;
1167 unlock_kernel(); 1164 mutex_unlock(&port->port.mutex);
1168 if (copy_to_user(info, &out_info, sizeof(out_info))) 1165 if (copy_to_user(info, &out_info, sizeof(out_info)))
1169 return -EFAULT; 1166 return -EFAULT;
1170 return 0; 1167 return 0;
diff --git a/drivers/char/istallion.c b/drivers/char/istallion.c
index 4e395c956a0..be28391adb7 100644
--- a/drivers/char/istallion.c
+++ b/drivers/char/istallion.c
@@ -203,9 +203,9 @@ static int stli_shared;
203 * the board has been detected, and whether it is actually running a slave 203 * the board has been detected, and whether it is actually running a slave
204 * or not. 204 * or not.
205 */ 205 */
206#define BST_FOUND 0x1 206#define BST_FOUND 0
207#define BST_STARTED 0x2 207#define BST_STARTED 1
208#define BST_PROBED 0x4 208#define BST_PROBED 2
209 209
210/* 210/*
211 * Define the set of port state flags. These are marked for internal 211 * Define the set of port state flags. These are marked for internal
@@ -816,7 +816,7 @@ static int stli_open(struct tty_struct *tty, struct file *filp)
816 brdp = stli_brds[brdnr]; 816 brdp = stli_brds[brdnr];
817 if (brdp == NULL) 817 if (brdp == NULL)
818 return -ENODEV; 818 return -ENODEV;
819 if ((brdp->state & BST_STARTED) == 0) 819 if (!test_bit(BST_STARTED, &brdp->state))
820 return -ENODEV; 820 return -ENODEV;
821 portnr = MINOR2PORT(minordev); 821 portnr = MINOR2PORT(minordev);
822 if (portnr > brdp->nrports) 822 if (portnr > brdp->nrports)
@@ -954,7 +954,7 @@ static int stli_rawopen(struct stlibrd *brdp, struct stliport *portp, unsigned l
954 * order of opens and closes may not be preserved across shared 954 * order of opens and closes may not be preserved across shared
955 * memory, so we must wait until it is complete. 955 * memory, so we must wait until it is complete.
956 */ 956 */
957 wait_event_interruptible(portp->raw_wait, 957 wait_event_interruptible_tty(portp->raw_wait,
958 !test_bit(ST_CLOSING, &portp->state)); 958 !test_bit(ST_CLOSING, &portp->state));
959 if (signal_pending(current)) { 959 if (signal_pending(current)) {
960 return -ERESTARTSYS; 960 return -ERESTARTSYS;
@@ -989,7 +989,7 @@ static int stli_rawopen(struct stlibrd *brdp, struct stliport *portp, unsigned l
989 set_bit(ST_OPENING, &portp->state); 989 set_bit(ST_OPENING, &portp->state);
990 spin_unlock_irqrestore(&brd_lock, flags); 990 spin_unlock_irqrestore(&brd_lock, flags);
991 991
992 wait_event_interruptible(portp->raw_wait, 992 wait_event_interruptible_tty(portp->raw_wait,
993 !test_bit(ST_OPENING, &portp->state)); 993 !test_bit(ST_OPENING, &portp->state));
994 if (signal_pending(current)) 994 if (signal_pending(current))
995 rc = -ERESTARTSYS; 995 rc = -ERESTARTSYS;
@@ -1020,7 +1020,7 @@ static int stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned
1020 * occurs on this port. 1020 * occurs on this port.
1021 */ 1021 */
1022 if (wait) { 1022 if (wait) {
1023 wait_event_interruptible(portp->raw_wait, 1023 wait_event_interruptible_tty(portp->raw_wait,
1024 !test_bit(ST_CLOSING, &portp->state)); 1024 !test_bit(ST_CLOSING, &portp->state));
1025 if (signal_pending(current)) { 1025 if (signal_pending(current)) {
1026 return -ERESTARTSYS; 1026 return -ERESTARTSYS;
@@ -1052,7 +1052,7 @@ static int stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned
1052 * to come back. 1052 * to come back.
1053 */ 1053 */
1054 rc = 0; 1054 rc = 0;
1055 wait_event_interruptible(portp->raw_wait, 1055 wait_event_interruptible_tty(portp->raw_wait,
1056 !test_bit(ST_CLOSING, &portp->state)); 1056 !test_bit(ST_CLOSING, &portp->state));
1057 if (signal_pending(current)) 1057 if (signal_pending(current))
1058 rc = -ERESTARTSYS; 1058 rc = -ERESTARTSYS;
@@ -1073,6 +1073,10 @@ static int stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned
1073 1073
1074static int stli_cmdwait(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback) 1074static int stli_cmdwait(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback)
1075{ 1075{
1076 /*
1077 * no need for wait_event_tty because clearing ST_CMDING cannot block
1078 * on BTM
1079 */
1076 wait_event_interruptible(portp->raw_wait, 1080 wait_event_interruptible(portp->raw_wait,
1077 !test_bit(ST_CMDING, &portp->state)); 1081 !test_bit(ST_CMDING, &portp->state));
1078 if (signal_pending(current)) 1082 if (signal_pending(current))
@@ -1846,7 +1850,7 @@ static void stli_portinfo(struct seq_file *m, struct stlibrd *brdp, struct stlip
1846 rc = stli_portcmdstats(NULL, portp); 1850 rc = stli_portcmdstats(NULL, portp);
1847 1851
1848 uart = "UNKNOWN"; 1852 uart = "UNKNOWN";
1849 if (brdp->state & BST_STARTED) { 1853 if (test_bit(BST_STARTED, &brdp->state)) {
1850 switch (stli_comstats.hwid) { 1854 switch (stli_comstats.hwid) {
1851 case 0: uart = "2681"; break; 1855 case 0: uart = "2681"; break;
1852 case 1: uart = "SC26198"; break; 1856 case 1: uart = "SC26198"; break;
@@ -1855,7 +1859,7 @@ static void stli_portinfo(struct seq_file *m, struct stlibrd *brdp, struct stlip
1855 } 1859 }
1856 seq_printf(m, "%d: uart:%s ", portnr, uart); 1860 seq_printf(m, "%d: uart:%s ", portnr, uart);
1857 1861
1858 if ((brdp->state & BST_STARTED) && (rc >= 0)) { 1862 if (test_bit(BST_STARTED, &brdp->state) && rc >= 0) {
1859 char sep; 1863 char sep;
1860 1864
1861 seq_printf(m, "tx:%d rx:%d", (int) stli_comstats.txtotal, 1865 seq_printf(m, "tx:%d rx:%d", (int) stli_comstats.txtotal,
@@ -2355,7 +2359,7 @@ static void stli_poll(unsigned long arg)
2355 brdp = stli_brds[brdnr]; 2359 brdp = stli_brds[brdnr];
2356 if (brdp == NULL) 2360 if (brdp == NULL)
2357 continue; 2361 continue;
2358 if ((brdp->state & BST_STARTED) == 0) 2362 if (!test_bit(BST_STARTED, &brdp->state))
2359 continue; 2363 continue;
2360 2364
2361 spin_lock(&brd_lock); 2365 spin_lock(&brd_lock);
@@ -3140,7 +3144,7 @@ static int stli_initecp(struct stlibrd *brdp)
3140 } 3144 }
3141 3145
3142 3146
3143 brdp->state |= BST_FOUND; 3147 set_bit(BST_FOUND, &brdp->state);
3144 return 0; 3148 return 0;
3145err_unmap: 3149err_unmap:
3146 iounmap(brdp->membase); 3150 iounmap(brdp->membase);
@@ -3297,7 +3301,7 @@ static int stli_initonb(struct stlibrd *brdp)
3297 brdp->panels[0] = brdp->nrports; 3301 brdp->panels[0] = brdp->nrports;
3298 3302
3299 3303
3300 brdp->state |= BST_FOUND; 3304 set_bit(BST_FOUND, &brdp->state);
3301 return 0; 3305 return 0;
3302err_unmap: 3306err_unmap:
3303 iounmap(brdp->membase); 3307 iounmap(brdp->membase);
@@ -3407,7 +3411,7 @@ stli_donestartup:
3407 spin_unlock_irqrestore(&brd_lock, flags); 3411 spin_unlock_irqrestore(&brd_lock, flags);
3408 3412
3409 if (rc == 0) 3413 if (rc == 0)
3410 brdp->state |= BST_STARTED; 3414 set_bit(BST_STARTED, &brdp->state);
3411 3415
3412 if (! stli_timeron) { 3416 if (! stli_timeron) {
3413 stli_timeron++; 3417 stli_timeron++;
@@ -3710,7 +3714,7 @@ static int __devinit stli_pciprobe(struct pci_dev *pdev,
3710 if (retval) 3714 if (retval)
3711 goto err_null; 3715 goto err_null;
3712 3716
3713 brdp->state |= BST_PROBED; 3717 set_bit(BST_PROBED, &brdp->state);
3714 pci_set_drvdata(pdev, brdp); 3718 pci_set_drvdata(pdev, brdp);
3715 3719
3716 EBRDENABLE(brdp); 3720 EBRDENABLE(brdp);
@@ -3841,7 +3845,7 @@ static int __init stli_initbrds(void)
3841 brdp = stli_brds[i]; 3845 brdp = stli_brds[i];
3842 if (brdp == NULL) 3846 if (brdp == NULL)
3843 continue; 3847 continue;
3844 if (brdp->state & BST_FOUND) { 3848 if (test_bit(BST_FOUND, &brdp->state)) {
3845 EBRDENABLE(brdp); 3849 EBRDENABLE(brdp);
3846 brdp->enable = NULL; 3850 brdp->enable = NULL;
3847 brdp->disable = NULL; 3851 brdp->disable = NULL;
@@ -4011,6 +4015,7 @@ static int stli_getbrdstats(combrd_t __user *bp)
4011 return -ENODEV; 4015 return -ENODEV;
4012 4016
4013 memset(&stli_brdstats, 0, sizeof(combrd_t)); 4017 memset(&stli_brdstats, 0, sizeof(combrd_t));
4018
4014 stli_brdstats.brd = brdp->brdnr; 4019 stli_brdstats.brd = brdp->brdnr;
4015 stli_brdstats.type = brdp->brdtype; 4020 stli_brdstats.type = brdp->brdtype;
4016 stli_brdstats.hwid = 0; 4021 stli_brdstats.hwid = 0;
@@ -4076,10 +4081,13 @@ static int stli_portcmdstats(struct tty_struct *tty, struct stliport *portp)
4076 if (brdp == NULL) 4081 if (brdp == NULL)
4077 return -ENODEV; 4082 return -ENODEV;
4078 4083
4079 if (brdp->state & BST_STARTED) { 4084 mutex_lock(&portp->port.mutex);
4085 if (test_bit(BST_STARTED, &brdp->state)) {
4080 if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS, 4086 if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
4081 &stli_cdkstats, sizeof(asystats_t), 1)) < 0) 4087 &stli_cdkstats, sizeof(asystats_t), 1)) < 0) {
4088 mutex_unlock(&portp->port.mutex);
4082 return rc; 4089 return rc;
4090 }
4083 } else { 4091 } else {
4084 memset(&stli_cdkstats, 0, sizeof(asystats_t)); 4092 memset(&stli_cdkstats, 0, sizeof(asystats_t));
4085 } 4093 }
@@ -4124,6 +4132,7 @@ static int stli_portcmdstats(struct tty_struct *tty, struct stliport *portp)
4124 stli_comstats.modem = stli_cdkstats.dcdcnt; 4132 stli_comstats.modem = stli_cdkstats.dcdcnt;
4125 stli_comstats.hwid = stli_cdkstats.hwid; 4133 stli_comstats.hwid = stli_cdkstats.hwid;
4126 stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals); 4134 stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
4135 mutex_unlock(&portp->port.mutex);
4127 4136
4128 return 0; 4137 return 0;
4129} 4138}
@@ -4186,15 +4195,20 @@ static int stli_clrportstats(struct stliport *portp, comstats_t __user *cp)
4186 if (!brdp) 4195 if (!brdp)
4187 return -ENODEV; 4196 return -ENODEV;
4188 4197
4189 if (brdp->state & BST_STARTED) { 4198 mutex_lock(&portp->port.mutex);
4190 if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, NULL, 0, 0)) < 0) 4199
4200 if (test_bit(BST_STARTED, &brdp->state)) {
4201 if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, NULL, 0, 0)) < 0) {
4202 mutex_unlock(&portp->port.mutex);
4191 return rc; 4203 return rc;
4204 }
4192 } 4205 }
4193 4206
4194 memset(&stli_comstats, 0, sizeof(comstats_t)); 4207 memset(&stli_comstats, 0, sizeof(comstats_t));
4195 stli_comstats.brd = portp->brdnr; 4208 stli_comstats.brd = portp->brdnr;
4196 stli_comstats.panel = portp->panelnr; 4209 stli_comstats.panel = portp->panelnr;
4197 stli_comstats.port = portp->portnr; 4210 stli_comstats.port = portp->portnr;
4211 mutex_unlock(&portp->port.mutex);
4198 4212
4199 if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t))) 4213 if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t)))
4200 return -EFAULT; 4214 return -EFAULT;
@@ -4266,8 +4280,6 @@ static long stli_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
4266 done = 0; 4280 done = 0;
4267 rc = 0; 4281 rc = 0;
4268 4282
4269 lock_kernel();
4270
4271 switch (cmd) { 4283 switch (cmd) {
4272 case COM_GETPORTSTATS: 4284 case COM_GETPORTSTATS:
4273 rc = stli_getportstats(NULL, NULL, argp); 4285 rc = stli_getportstats(NULL, NULL, argp);
@@ -4290,8 +4302,6 @@ static long stli_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
4290 done++; 4302 done++;
4291 break; 4303 break;
4292 } 4304 }
4293 unlock_kernel();
4294
4295 if (done) 4305 if (done)
4296 return rc; 4306 return rc;
4297 4307
@@ -4308,8 +4318,6 @@ static long stli_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
4308 if (brdp->state == 0) 4318 if (brdp->state == 0)
4309 return -ENODEV; 4319 return -ENODEV;
4310 4320
4311 lock_kernel();
4312
4313 switch (cmd) { 4321 switch (cmd) {
4314 case STL_BINTR: 4322 case STL_BINTR:
4315 EBRDINTR(brdp); 4323 EBRDINTR(brdp);
@@ -4318,10 +4326,10 @@ static long stli_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
4318 rc = stli_startbrd(brdp); 4326 rc = stli_startbrd(brdp);
4319 break; 4327 break;
4320 case STL_BSTOP: 4328 case STL_BSTOP:
4321 brdp->state &= ~BST_STARTED; 4329 clear_bit(BST_STARTED, &brdp->state);
4322 break; 4330 break;
4323 case STL_BRESET: 4331 case STL_BRESET:
4324 brdp->state &= ~BST_STARTED; 4332 clear_bit(BST_STARTED, &brdp->state);
4325 EBRDRESET(brdp); 4333 EBRDRESET(brdp);
4326 if (stli_shared == 0) { 4334 if (stli_shared == 0) {
4327 if (brdp->reenable != NULL) 4335 if (brdp->reenable != NULL)
@@ -4332,7 +4340,6 @@ static long stli_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
4332 rc = -ENOIOCTLCMD; 4340 rc = -ENOIOCTLCMD;
4333 break; 4341 break;
4334 } 4342 }
4335 unlock_kernel();
4336 return rc; 4343 return rc;
4337} 4344}
4338 4345
@@ -4378,7 +4385,8 @@ static void istallion_cleanup_isa(void)
4378 unsigned int j; 4385 unsigned int j;
4379 4386
4380 for (j = 0; (j < stli_nrbrds); j++) { 4387 for (j = 0; (j < stli_nrbrds); j++) {
4381 if ((brdp = stli_brds[j]) == NULL || (brdp->state & BST_PROBED)) 4388 if ((brdp = stli_brds[j]) == NULL ||
4389 test_bit(BST_PROBED, &brdp->state))
4382 continue; 4390 continue;
4383 4391
4384 stli_cleanup_ports(brdp); 4392 stli_cleanup_ports(brdp);
diff --git a/drivers/char/keyboard.c b/drivers/char/keyboard.c
index 25be2102a60..a7ca75212bf 100644
--- a/drivers/char/keyboard.c
+++ b/drivers/char/keyboard.c
@@ -299,7 +299,7 @@ int kbd_rate(struct kbd_repeat *rep)
299 */ 299 */
300static void put_queue(struct vc_data *vc, int ch) 300static void put_queue(struct vc_data *vc, int ch)
301{ 301{
302 struct tty_struct *tty = vc->vc_tty; 302 struct tty_struct *tty = vc->port.tty;
303 303
304 if (tty) { 304 if (tty) {
305 tty_insert_flip_char(tty, ch, 0); 305 tty_insert_flip_char(tty, ch, 0);
@@ -309,7 +309,7 @@ static void put_queue(struct vc_data *vc, int ch)
309 309
310static void puts_queue(struct vc_data *vc, char *cp) 310static void puts_queue(struct vc_data *vc, char *cp)
311{ 311{
312 struct tty_struct *tty = vc->vc_tty; 312 struct tty_struct *tty = vc->port.tty;
313 313
314 if (!tty) 314 if (!tty)
315 return; 315 return;
@@ -485,7 +485,7 @@ static void fn_show_ptregs(struct vc_data *vc)
485 485
486static void fn_hold(struct vc_data *vc) 486static void fn_hold(struct vc_data *vc)
487{ 487{
488 struct tty_struct *tty = vc->vc_tty; 488 struct tty_struct *tty = vc->port.tty;
489 489
490 if (rep || !tty) 490 if (rep || !tty)
491 return; 491 return;
@@ -563,7 +563,7 @@ static void fn_inc_console(struct vc_data *vc)
563 563
564static void fn_send_intr(struct vc_data *vc) 564static void fn_send_intr(struct vc_data *vc)
565{ 565{
566 struct tty_struct *tty = vc->vc_tty; 566 struct tty_struct *tty = vc->port.tty;
567 567
568 if (!tty) 568 if (!tty)
569 return; 569 return;
@@ -1162,7 +1162,7 @@ static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
1162 struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down }; 1162 struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down };
1163 int rc; 1163 int rc;
1164 1164
1165 tty = vc->vc_tty; 1165 tty = vc->port.tty;
1166 1166
1167 if (tty && (!tty->driver_data)) { 1167 if (tty && (!tty->driver_data)) {
1168 /* No driver data? Strange. Okay we fix it then. */ 1168 /* No driver data? Strange. Okay we fix it then. */
diff --git a/drivers/char/mxser.c b/drivers/char/mxser.c
index d2692d443f7..3fc89da856a 100644
--- a/drivers/char/mxser.c
+++ b/drivers/char/mxser.c
@@ -2193,7 +2193,7 @@ static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port
2193 port->mon_data.up_txcnt += (cnt - port->xmit_cnt); 2193 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2194 port->icount.tx += (cnt - port->xmit_cnt); 2194 port->icount.tx += (cnt - port->xmit_cnt);
2195 2195
2196 if (port->xmit_cnt < WAKEUP_CHARS && tty) 2196 if (port->xmit_cnt < WAKEUP_CHARS)
2197 tty_wakeup(tty); 2197 tty_wakeup(tty);
2198 2198
2199 if (port->xmit_cnt <= 0) { 2199 if (port->xmit_cnt <= 0) {
diff --git a/drivers/char/n_gsm.c b/drivers/char/n_gsm.c
index e4089c432f1..099105e0894 100644
--- a/drivers/char/n_gsm.c
+++ b/drivers/char/n_gsm.c
@@ -43,7 +43,6 @@
43#include <linux/sched.h> 43#include <linux/sched.h>
44#include <linux/interrupt.h> 44#include <linux/interrupt.h>
45#include <linux/tty.h> 45#include <linux/tty.h>
46#include <linux/timer.h>
47#include <linux/ctype.h> 46#include <linux/ctype.h>
48#include <linux/mm.h> 47#include <linux/mm.h>
49#include <linux/string.h> 48#include <linux/string.h>
diff --git a/drivers/char/n_hdlc.c b/drivers/char/n_hdlc.c
index c68118efad8..47d32281032 100644
--- a/drivers/char/n_hdlc.c
+++ b/drivers/char/n_hdlc.c
@@ -598,18 +598,18 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
598 return -EFAULT; 598 return -EFAULT;
599 } 599 }
600 600
601 lock_kernel(); 601 tty_lock();
602 602
603 for (;;) { 603 for (;;) {
604 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { 604 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
605 unlock_kernel(); 605 tty_unlock();
606 return -EIO; 606 return -EIO;
607 } 607 }
608 608
609 n_hdlc = tty2n_hdlc (tty); 609 n_hdlc = tty2n_hdlc (tty);
610 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC || 610 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
611 tty != n_hdlc->tty) { 611 tty != n_hdlc->tty) {
612 unlock_kernel(); 612 tty_unlock();
613 return 0; 613 return 0;
614 } 614 }
615 615
@@ -619,13 +619,13 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
619 619
620 /* no data */ 620 /* no data */
621 if (file->f_flags & O_NONBLOCK) { 621 if (file->f_flags & O_NONBLOCK) {
622 unlock_kernel(); 622 tty_unlock();
623 return -EAGAIN; 623 return -EAGAIN;
624 } 624 }
625 625
626 interruptible_sleep_on (&tty->read_wait); 626 interruptible_sleep_on (&tty->read_wait);
627 if (signal_pending(current)) { 627 if (signal_pending(current)) {
628 unlock_kernel(); 628 tty_unlock();
629 return -EINTR; 629 return -EINTR;
630 } 630 }
631 } 631 }
@@ -648,7 +648,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
648 kfree(rbuf); 648 kfree(rbuf);
649 else 649 else
650 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf); 650 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf);
651 unlock_kernel(); 651 tty_unlock();
652 return ret; 652 return ret;
653 653
654} /* end of n_hdlc_tty_read() */ 654} /* end of n_hdlc_tty_read() */
@@ -691,7 +691,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
691 count = maxframe; 691 count = maxframe;
692 } 692 }
693 693
694 lock_kernel(); 694 tty_lock();
695 695
696 add_wait_queue(&tty->write_wait, &wait); 696 add_wait_queue(&tty->write_wait, &wait);
697 set_current_state(TASK_INTERRUPTIBLE); 697 set_current_state(TASK_INTERRUPTIBLE);
@@ -731,7 +731,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
731 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf); 731 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
732 n_hdlc_send_frames(n_hdlc,tty); 732 n_hdlc_send_frames(n_hdlc,tty);
733 } 733 }
734 unlock_kernel(); 734 tty_unlock();
735 return error; 735 return error;
736 736
737} /* end of n_hdlc_tty_write() */ 737} /* end of n_hdlc_tty_write() */
diff --git a/drivers/char/n_r3964.c b/drivers/char/n_r3964.c
index c1d8b54c816..a98290d7a2c 100644
--- a/drivers/char/n_r3964.c
+++ b/drivers/char/n_r3964.c
@@ -1067,7 +1067,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1067 1067
1068 TRACE_L("read()"); 1068 TRACE_L("read()");
1069 1069
1070 lock_kernel(); 1070 tty_lock();
1071 1071
1072 pClient = findClient(pInfo, task_pid(current)); 1072 pClient = findClient(pInfo, task_pid(current));
1073 if (pClient) { 1073 if (pClient) {
@@ -1079,7 +1079,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1079 goto unlock; 1079 goto unlock;
1080 } 1080 }
1081 /* block until there is a message: */ 1081 /* block until there is a message: */
1082 wait_event_interruptible(pInfo->read_wait, 1082 wait_event_interruptible_tty(pInfo->read_wait,
1083 (pMsg = remove_msg(pInfo, pClient))); 1083 (pMsg = remove_msg(pInfo, pClient)));
1084 } 1084 }
1085 1085
@@ -1109,7 +1109,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1109 } 1109 }
1110 ret = -EPERM; 1110 ret = -EPERM;
1111unlock: 1111unlock:
1112 unlock_kernel(); 1112 tty_unlock();
1113 return ret; 1113 return ret;
1114} 1114}
1115 1115
@@ -1158,7 +1158,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1158 pHeader->locks = 0; 1158 pHeader->locks = 0;
1159 pHeader->owner = NULL; 1159 pHeader->owner = NULL;
1160 1160
1161 lock_kernel(); 1161 tty_lock();
1162 1162
1163 pClient = findClient(pInfo, task_pid(current)); 1163 pClient = findClient(pInfo, task_pid(current));
1164 if (pClient) { 1164 if (pClient) {
@@ -1177,7 +1177,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1177 add_tx_queue(pInfo, pHeader); 1177 add_tx_queue(pInfo, pHeader);
1178 trigger_transmit(pInfo); 1178 trigger_transmit(pInfo);
1179 1179
1180 unlock_kernel(); 1180 tty_unlock();
1181 1181
1182 return 0; 1182 return 0;
1183} 1183}
diff --git a/drivers/char/n_tty.c b/drivers/char/n_tty.c
index bdae8327143..428f4fe0b5f 100644
--- a/drivers/char/n_tty.c
+++ b/drivers/char/n_tty.c
@@ -1102,6 +1102,11 @@ static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1102 if (I_IUCLC(tty) && L_IEXTEN(tty)) 1102 if (I_IUCLC(tty) && L_IEXTEN(tty))
1103 c = tolower(c); 1103 c = tolower(c);
1104 1104
1105 if (L_EXTPROC(tty)) {
1106 put_tty_queue(c, tty);
1107 return;
1108 }
1109
1105 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && 1110 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1106 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) && 1111 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1107 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) { 1112 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
@@ -1409,7 +1414,8 @@ static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1409 1414
1410 n_tty_set_room(tty); 1415 n_tty_set_room(tty);
1411 1416
1412 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) { 1417 if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
1418 L_EXTPROC(tty)) {
1413 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 1419 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1414 if (waitqueue_active(&tty->read_wait)) 1420 if (waitqueue_active(&tty->read_wait))
1415 wake_up_interruptible(&tty->read_wait); 1421 wake_up_interruptible(&tty->read_wait);
@@ -1585,7 +1591,7 @@ static int n_tty_open(struct tty_struct *tty)
1585static inline int input_available_p(struct tty_struct *tty, int amt) 1591static inline int input_available_p(struct tty_struct *tty, int amt)
1586{ 1592{
1587 tty_flush_to_ldisc(tty); 1593 tty_flush_to_ldisc(tty);
1588 if (tty->icanon) { 1594 if (tty->icanon && !L_EXTPROC(tty)) {
1589 if (tty->canon_data) 1595 if (tty->canon_data)
1590 return 1; 1596 return 1;
1591 } else if (tty->read_cnt >= (amt ? amt : 1)) 1597 } else if (tty->read_cnt >= (amt ? amt : 1))
@@ -1632,6 +1638,11 @@ static int copy_from_read_buf(struct tty_struct *tty,
1632 spin_lock_irqsave(&tty->read_lock, flags); 1638 spin_lock_irqsave(&tty->read_lock, flags);
1633 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1); 1639 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1634 tty->read_cnt -= n; 1640 tty->read_cnt -= n;
1641 /* Turn single EOF into zero-length read */
1642 if (L_EXTPROC(tty) && tty->icanon && n == 1) {
1643 if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty))
1644 n--;
1645 }
1635 spin_unlock_irqrestore(&tty->read_lock, flags); 1646 spin_unlock_irqrestore(&tty->read_lock, flags);
1636 *b += n; 1647 *b += n;
1637 *nr -= n; 1648 *nr -= n;
@@ -1812,7 +1823,7 @@ do_it_again:
1812 nr--; 1823 nr--;
1813 } 1824 }
1814 1825
1815 if (tty->icanon) { 1826 if (tty->icanon && !L_EXTPROC(tty)) {
1816 /* N.B. avoid overrun if nr == 0 */ 1827 /* N.B. avoid overrun if nr == 0 */
1817 while (nr && tty->read_cnt) { 1828 while (nr && tty->read_cnt) {
1818 int eol; 1829 int eol;
diff --git a/drivers/char/nozomi.c b/drivers/char/nozomi.c
index a6638003f53..18af923093c 100644
--- a/drivers/char/nozomi.c
+++ b/drivers/char/nozomi.c
@@ -1611,6 +1611,8 @@ static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
1611 ret = tty_init_termios(tty); 1611 ret = tty_init_termios(tty);
1612 if (ret == 0) { 1612 if (ret == 0) {
1613 tty_driver_kref_get(driver); 1613 tty_driver_kref_get(driver);
1614 tty->count++;
1615 tty->driver_data = port;
1614 driver->ttys[tty->index] = tty; 1616 driver->ttys[tty->index] = tty;
1615 } 1617 }
1616 return ret; 1618 return ret;
@@ -1639,7 +1641,7 @@ static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
1639 1641
1640static int ntty_open(struct tty_struct *tty, struct file *filp) 1642static int ntty_open(struct tty_struct *tty, struct file *filp)
1641{ 1643{
1642 struct port *port = get_port_by_tty(tty); 1644 struct port *port = tty->driver_data;
1643 return tty_port_open(&port->port, tty, filp); 1645 return tty_port_open(&port->port, tty, filp);
1644} 1646}
1645 1647
diff --git a/drivers/char/pty.c b/drivers/char/pty.c
index d83a43130df..ad46eae1f9b 100644
--- a/drivers/char/pty.c
+++ b/drivers/char/pty.c
@@ -62,7 +62,9 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
62 if (tty->driver == ptm_driver) 62 if (tty->driver == ptm_driver)
63 devpts_pty_kill(tty->link); 63 devpts_pty_kill(tty->link);
64#endif 64#endif
65 tty_unlock();
65 tty_vhangup(tty->link); 66 tty_vhangup(tty->link);
67 tty_lock();
66 } 68 }
67} 69}
68 70
@@ -171,6 +173,23 @@ static int pty_set_lock(struct tty_struct *tty, int __user *arg)
171 return 0; 173 return 0;
172} 174}
173 175
176/* Send a signal to the slave */
177static int pty_signal(struct tty_struct *tty, int sig)
178{
179 unsigned long flags;
180 struct pid *pgrp;
181
182 if (tty->link) {
183 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
184 pgrp = get_pid(tty->link->pgrp);
185 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
186
187 kill_pgrp(pgrp, sig, 1);
188 put_pid(pgrp);
189 }
190 return 0;
191}
192
174static void pty_flush_buffer(struct tty_struct *tty) 193static void pty_flush_buffer(struct tty_struct *tty)
175{ 194{
176 struct tty_struct *to = tty->link; 195 struct tty_struct *to = tty->link;
@@ -321,6 +340,8 @@ static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
321 switch (cmd) { 340 switch (cmd) {
322 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ 341 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
323 return pty_set_lock(tty, (int __user *) arg); 342 return pty_set_lock(tty, (int __user *) arg);
343 case TIOCSIG: /* Send signal to other side of pty */
344 return pty_signal(tty, (int) arg);
324 } 345 }
325 return -ENOIOCTLCMD; 346 return -ENOIOCTLCMD;
326} 347}
@@ -476,6 +497,8 @@ static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
476 return pty_set_lock(tty, (int __user *)arg); 497 return pty_set_lock(tty, (int __user *)arg);
477 case TIOCGPTN: /* Get PT Number */ 498 case TIOCGPTN: /* Get PT Number */
478 return put_user(tty->index, (unsigned int __user *)arg); 499 return put_user(tty->index, (unsigned int __user *)arg);
500 case TIOCSIG: /* Send signal to other side of pty */
501 return pty_signal(tty, (int) arg);
479 } 502 }
480 503
481 return -ENOIOCTLCMD; 504 return -ENOIOCTLCMD;
@@ -626,7 +649,7 @@ static const struct tty_operations pty_unix98_ops = {
626 * allocated_ptys_lock handles the list of free pty numbers 649 * allocated_ptys_lock handles the list of free pty numbers
627 */ 650 */
628 651
629static int __ptmx_open(struct inode *inode, struct file *filp) 652static int ptmx_open(struct inode *inode, struct file *filp)
630{ 653{
631 struct tty_struct *tty; 654 struct tty_struct *tty;
632 int retval; 655 int retval;
@@ -635,11 +658,14 @@ static int __ptmx_open(struct inode *inode, struct file *filp)
635 nonseekable_open(inode, filp); 658 nonseekable_open(inode, filp);
636 659
637 /* find a device that is not in use. */ 660 /* find a device that is not in use. */
661 tty_lock();
638 index = devpts_new_index(inode); 662 index = devpts_new_index(inode);
663 tty_unlock();
639 if (index < 0) 664 if (index < 0)
640 return index; 665 return index;
641 666
642 mutex_lock(&tty_mutex); 667 mutex_lock(&tty_mutex);
668 tty_lock();
643 tty = tty_init_dev(ptm_driver, index, 1); 669 tty = tty_init_dev(ptm_driver, index, 1);
644 mutex_unlock(&tty_mutex); 670 mutex_unlock(&tty_mutex);
645 671
@@ -657,26 +683,21 @@ static int __ptmx_open(struct inode *inode, struct file *filp)
657 goto out1; 683 goto out1;
658 684
659 retval = ptm_driver->ops->open(tty, filp); 685 retval = ptm_driver->ops->open(tty, filp);
660 if (!retval) 686 if (retval)
661 return 0; 687 goto out2;
662out1: 688out1:
689 tty_unlock();
690 return retval;
691out2:
692 tty_unlock();
663 tty_release(inode, filp); 693 tty_release(inode, filp);
664 return retval; 694 return retval;
665out: 695out:
666 devpts_kill_index(inode, index); 696 devpts_kill_index(inode, index);
697 tty_unlock();
667 return retval; 698 return retval;
668} 699}
669 700
670static int ptmx_open(struct inode *inode, struct file *filp)
671{
672 int ret;
673
674 lock_kernel();
675 ret = __ptmx_open(inode, filp);
676 unlock_kernel();
677 return ret;
678}
679
680static struct file_operations ptmx_fops; 701static struct file_operations ptmx_fops;
681 702
682static void __init unix98_pty_init(void) 703static void __init unix98_pty_init(void)
diff --git a/drivers/char/riscom8.c b/drivers/char/riscom8.c
index b02332a5412..af4de1fe844 100644
--- a/drivers/char/riscom8.c
+++ b/drivers/char/riscom8.c
@@ -47,7 +47,6 @@
47#include <linux/init.h> 47#include <linux/init.h>
48#include <linux/delay.h> 48#include <linux/delay.h>
49#include <linux/tty_flip.h> 49#include <linux/tty_flip.h>
50#include <linux/smp_lock.h>
51#include <linux/spinlock.h> 50#include <linux/spinlock.h>
52#include <linux/device.h> 51#include <linux/device.h>
53 52
@@ -1184,6 +1183,7 @@ static int rc_set_serial_info(struct tty_struct *tty, struct riscom_port *port,
1184 if (copy_from_user(&tmp, newinfo, sizeof(tmp))) 1183 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1185 return -EFAULT; 1184 return -EFAULT;
1186 1185
1186 mutex_lock(&port->port.mutex);
1187 change_speed = ((port->port.flags & ASYNC_SPD_MASK) != 1187 change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
1188 (tmp.flags & ASYNC_SPD_MASK)); 1188 (tmp.flags & ASYNC_SPD_MASK));
1189 1189
@@ -1191,8 +1191,10 @@ static int rc_set_serial_info(struct tty_struct *tty, struct riscom_port *port,
1191 if ((tmp.close_delay != port->port.close_delay) || 1191 if ((tmp.close_delay != port->port.close_delay) ||
1192 (tmp.closing_wait != port->port.closing_wait) || 1192 (tmp.closing_wait != port->port.closing_wait) ||
1193 ((tmp.flags & ~ASYNC_USR_MASK) != 1193 ((tmp.flags & ~ASYNC_USR_MASK) !=
1194 (port->port.flags & ~ASYNC_USR_MASK))) 1194 (port->port.flags & ~ASYNC_USR_MASK))) {
1195 mutex_unlock(&port->port.mutex);
1195 return -EPERM; 1196 return -EPERM;
1197 }
1196 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) | 1198 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
1197 (tmp.flags & ASYNC_USR_MASK)); 1199 (tmp.flags & ASYNC_USR_MASK));
1198 } else { 1200 } else {
@@ -1208,6 +1210,7 @@ static int rc_set_serial_info(struct tty_struct *tty, struct riscom_port *port,
1208 rc_change_speed(tty, bp, port); 1210 rc_change_speed(tty, bp, port);
1209 spin_unlock_irqrestore(&riscom_lock, flags); 1211 spin_unlock_irqrestore(&riscom_lock, flags);
1210 } 1212 }
1213 mutex_unlock(&port->port.mutex);
1211 return 0; 1214 return 0;
1212} 1215}
1213 1216
@@ -1220,12 +1223,15 @@ static int rc_get_serial_info(struct riscom_port *port,
1220 memset(&tmp, 0, sizeof(tmp)); 1223 memset(&tmp, 0, sizeof(tmp));
1221 tmp.type = PORT_CIRRUS; 1224 tmp.type = PORT_CIRRUS;
1222 tmp.line = port - rc_port; 1225 tmp.line = port - rc_port;
1226
1227 mutex_lock(&port->port.mutex);
1223 tmp.port = bp->base; 1228 tmp.port = bp->base;
1224 tmp.irq = bp->irq; 1229 tmp.irq = bp->irq;
1225 tmp.flags = port->port.flags; 1230 tmp.flags = port->port.flags;
1226 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC; 1231 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1227 tmp.close_delay = port->port.close_delay * HZ/100; 1232 tmp.close_delay = port->port.close_delay * HZ/100;
1228 tmp.closing_wait = port->port.closing_wait * HZ/100; 1233 tmp.closing_wait = port->port.closing_wait * HZ/100;
1234 mutex_unlock(&port->port.mutex);
1229 tmp.xmit_fifo_size = CD180_NFIFO; 1235 tmp.xmit_fifo_size = CD180_NFIFO;
1230 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0; 1236 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1231} 1237}
@@ -1242,14 +1248,10 @@ static int rc_ioctl(struct tty_struct *tty, struct file *filp,
1242 1248
1243 switch (cmd) { 1249 switch (cmd) {
1244 case TIOCGSERIAL: 1250 case TIOCGSERIAL:
1245 lock_kernel();
1246 retval = rc_get_serial_info(port, argp); 1251 retval = rc_get_serial_info(port, argp);
1247 unlock_kernel();
1248 break; 1252 break;
1249 case TIOCSSERIAL: 1253 case TIOCSSERIAL:
1250 lock_kernel();
1251 retval = rc_set_serial_info(tty, port, argp); 1254 retval = rc_set_serial_info(tty, port, argp);
1252 unlock_kernel();
1253 break; 1255 break;
1254 default: 1256 default:
1255 retval = -ENOIOCTLCMD; 1257 retval = -ENOIOCTLCMD;
diff --git a/drivers/char/rocket.c b/drivers/char/rocket.c
index 0e29a23ec4c..79c3bc69165 100644
--- a/drivers/char/rocket.c
+++ b/drivers/char/rocket.c
@@ -73,7 +73,6 @@
73#include <linux/tty_driver.h> 73#include <linux/tty_driver.h>
74#include <linux/tty_flip.h> 74#include <linux/tty_flip.h>
75#include <linux/serial.h> 75#include <linux/serial.h>
76#include <linux/smp_lock.h>
77#include <linux/string.h> 76#include <linux/string.h>
78#include <linux/fcntl.h> 77#include <linux/fcntl.h>
79#include <linux/ptrace.h> 78#include <linux/ptrace.h>
@@ -1017,6 +1016,7 @@ static void rp_close(struct tty_struct *tty, struct file *filp)
1017 if (tty_port_close_start(port, tty, filp) == 0) 1016 if (tty_port_close_start(port, tty, filp) == 0)
1018 return; 1017 return;
1019 1018
1019 mutex_lock(&port->mutex);
1020 cp = &info->channel; 1020 cp = &info->channel;
1021 /* 1021 /*
1022 * Before we drop DTR, make sure the UART transmitter 1022 * Before we drop DTR, make sure the UART transmitter
@@ -1060,9 +1060,13 @@ static void rp_close(struct tty_struct *tty, struct file *filp)
1060 info->xmit_buf = NULL; 1060 info->xmit_buf = NULL;
1061 } 1061 }
1062 } 1062 }
1063 spin_lock_irq(&port->lock);
1063 info->port.flags &= ~(ASYNC_INITIALIZED | ASYNC_CLOSING | ASYNC_NORMAL_ACTIVE); 1064 info->port.flags &= ~(ASYNC_INITIALIZED | ASYNC_CLOSING | ASYNC_NORMAL_ACTIVE);
1064 tty->closing = 0; 1065 tty->closing = 0;
1066 spin_unlock_irq(&port->lock);
1067 mutex_unlock(&port->mutex);
1065 tty_port_tty_set(port, NULL); 1068 tty_port_tty_set(port, NULL);
1069
1066 wake_up_interruptible(&port->close_wait); 1070 wake_up_interruptible(&port->close_wait);
1067 complete_all(&info->close_wait); 1071 complete_all(&info->close_wait);
1068 atomic_dec(&rp_num_ports_open); 1072 atomic_dec(&rp_num_ports_open);
@@ -1210,11 +1214,13 @@ static int get_config(struct r_port *info, struct rocket_config __user *retinfo)
1210 if (!retinfo) 1214 if (!retinfo)
1211 return -EFAULT; 1215 return -EFAULT;
1212 memset(&tmp, 0, sizeof (tmp)); 1216 memset(&tmp, 0, sizeof (tmp));
1217 mutex_lock(&info->port.mutex);
1213 tmp.line = info->line; 1218 tmp.line = info->line;
1214 tmp.flags = info->flags; 1219 tmp.flags = info->flags;
1215 tmp.close_delay = info->port.close_delay; 1220 tmp.close_delay = info->port.close_delay;
1216 tmp.closing_wait = info->port.closing_wait; 1221 tmp.closing_wait = info->port.closing_wait;
1217 tmp.port = rcktpt_io_addr[(info->line >> 5) & 3]; 1222 tmp.port = rcktpt_io_addr[(info->line >> 5) & 3];
1223 mutex_unlock(&info->port.mutex);
1218 1224
1219 if (copy_to_user(retinfo, &tmp, sizeof (*retinfo))) 1225 if (copy_to_user(retinfo, &tmp, sizeof (*retinfo)))
1220 return -EFAULT; 1226 return -EFAULT;
@@ -1229,10 +1235,13 @@ static int set_config(struct tty_struct *tty, struct r_port *info,
1229 if (copy_from_user(&new_serial, new_info, sizeof (new_serial))) 1235 if (copy_from_user(&new_serial, new_info, sizeof (new_serial)))
1230 return -EFAULT; 1236 return -EFAULT;
1231 1237
1238 mutex_lock(&info->port.mutex);
1232 if (!capable(CAP_SYS_ADMIN)) 1239 if (!capable(CAP_SYS_ADMIN))
1233 { 1240 {
1234 if ((new_serial.flags & ~ROCKET_USR_MASK) != (info->flags & ~ROCKET_USR_MASK)) 1241 if ((new_serial.flags & ~ROCKET_USR_MASK) != (info->flags & ~ROCKET_USR_MASK)) {
1242 mutex_unlock(&info->port.mutex);
1235 return -EPERM; 1243 return -EPERM;
1244 }
1236 info->flags = ((info->flags & ~ROCKET_USR_MASK) | (new_serial.flags & ROCKET_USR_MASK)); 1245 info->flags = ((info->flags & ~ROCKET_USR_MASK) | (new_serial.flags & ROCKET_USR_MASK));
1237 configure_r_port(tty, info, NULL); 1246 configure_r_port(tty, info, NULL);
1238 return 0; 1247 return 0;
@@ -1250,6 +1259,7 @@ static int set_config(struct tty_struct *tty, struct r_port *info,
1250 tty->alt_speed = 230400; 1259 tty->alt_speed = 230400;
1251 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_WARP) 1260 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_WARP)
1252 tty->alt_speed = 460800; 1261 tty->alt_speed = 460800;
1262 mutex_unlock(&info->port.mutex);
1253 1263
1254 configure_r_port(tty, info, NULL); 1264 configure_r_port(tty, info, NULL);
1255 return 0; 1265 return 0;
@@ -1325,8 +1335,6 @@ static int rp_ioctl(struct tty_struct *tty, struct file *file,
1325 if (cmd != RCKP_GET_PORTS && rocket_paranoia_check(info, "rp_ioctl")) 1335 if (cmd != RCKP_GET_PORTS && rocket_paranoia_check(info, "rp_ioctl"))
1326 return -ENXIO; 1336 return -ENXIO;
1327 1337
1328 lock_kernel();
1329
1330 switch (cmd) { 1338 switch (cmd) {
1331 case RCKP_GET_STRUCT: 1339 case RCKP_GET_STRUCT:
1332 if (copy_to_user(argp, info, sizeof (struct r_port))) 1340 if (copy_to_user(argp, info, sizeof (struct r_port)))
@@ -1350,7 +1358,6 @@ static int rp_ioctl(struct tty_struct *tty, struct file *file,
1350 default: 1358 default:
1351 ret = -ENOIOCTLCMD; 1359 ret = -ENOIOCTLCMD;
1352 } 1360 }
1353 unlock_kernel();
1354 return ret; 1361 return ret;
1355} 1362}
1356 1363
@@ -1471,7 +1478,6 @@ static void rp_wait_until_sent(struct tty_struct *tty, int timeout)
1471 jiffies); 1478 jiffies);
1472 printk(KERN_INFO "cps=%d...\n", info->cps); 1479 printk(KERN_INFO "cps=%d...\n", info->cps);
1473#endif 1480#endif
1474 lock_kernel();
1475 while (1) { 1481 while (1) {
1476 txcnt = sGetTxCnt(cp); 1482 txcnt = sGetTxCnt(cp);
1477 if (!txcnt) { 1483 if (!txcnt) {
@@ -1499,7 +1505,6 @@ static void rp_wait_until_sent(struct tty_struct *tty, int timeout)
1499 break; 1505 break;
1500 } 1506 }
1501 __set_current_state(TASK_RUNNING); 1507 __set_current_state(TASK_RUNNING);
1502 unlock_kernel();
1503#ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT 1508#ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
1504 printk(KERN_INFO "txcnt = %d (jiff=%lu)...done\n", txcnt, jiffies); 1509 printk(KERN_INFO "txcnt = %d (jiff=%lu)...done\n", txcnt, jiffies);
1505#endif 1510#endif
@@ -1512,6 +1517,7 @@ static void rp_hangup(struct tty_struct *tty)
1512{ 1517{
1513 CHANNEL_t *cp; 1518 CHANNEL_t *cp;
1514 struct r_port *info = tty->driver_data; 1519 struct r_port *info = tty->driver_data;
1520 unsigned long flags;
1515 1521
1516 if (rocket_paranoia_check(info, "rp_hangup")) 1522 if (rocket_paranoia_check(info, "rp_hangup"))
1517 return; 1523 return;
@@ -1520,11 +1526,15 @@ static void rp_hangup(struct tty_struct *tty)
1520 printk(KERN_INFO "rp_hangup of ttyR%d...\n", info->line); 1526 printk(KERN_INFO "rp_hangup of ttyR%d...\n", info->line);
1521#endif 1527#endif
1522 rp_flush_buffer(tty); 1528 rp_flush_buffer(tty);
1523 if (info->port.flags & ASYNC_CLOSING) 1529 spin_lock_irqsave(&info->port.lock, flags);
1530 if (info->port.flags & ASYNC_CLOSING) {
1531 spin_unlock_irqrestore(&info->port.lock, flags);
1524 return; 1532 return;
1533 }
1525 if (info->port.count) 1534 if (info->port.count)
1526 atomic_dec(&rp_num_ports_open); 1535 atomic_dec(&rp_num_ports_open);
1527 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]); 1536 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1537 spin_unlock_irqrestore(&info->port.lock, flags);
1528 1538
1529 tty_port_hangup(&info->port); 1539 tty_port_hangup(&info->port);
1530 1540
@@ -1535,7 +1545,7 @@ static void rp_hangup(struct tty_struct *tty)
1535 sDisCTSFlowCtl(cp); 1545 sDisCTSFlowCtl(cp);
1536 sDisTxSoftFlowCtl(cp); 1546 sDisTxSoftFlowCtl(cp);
1537 sClrTxXOFF(cp); 1547 sClrTxXOFF(cp);
1538 info->port.flags &= ~ASYNC_INITIALIZED; 1548 clear_bit(ASYNCB_INITIALIZED, &info->port.flags);
1539 1549
1540 wake_up_interruptible(&info->port.open_wait); 1550 wake_up_interruptible(&info->port.open_wait);
1541} 1551}
diff --git a/drivers/char/selection.c b/drivers/char/selection.c
index f97b9e84806..ebae344ce91 100644
--- a/drivers/char/selection.c
+++ b/drivers/char/selection.c
@@ -26,6 +26,7 @@
26#include <linux/selection.h> 26#include <linux/selection.h>
27#include <linux/tiocl.h> 27#include <linux/tiocl.h>
28#include <linux/console.h> 28#include <linux/console.h>
29#include <linux/smp_lock.h>
29 30
30/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */ 31/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
31#define isspace(c) ((c) == ' ') 32#define isspace(c) ((c) == ' ')
@@ -312,12 +313,20 @@ int paste_selection(struct tty_struct *tty)
312 struct tty_ldisc *ld; 313 struct tty_ldisc *ld;
313 DECLARE_WAITQUEUE(wait, current); 314 DECLARE_WAITQUEUE(wait, current);
314 315
316 /* always called with BTM from vt_ioctl */
317 WARN_ON(!tty_locked());
318
315 acquire_console_sem(); 319 acquire_console_sem();
316 poke_blanked_console(); 320 poke_blanked_console();
317 release_console_sem(); 321 release_console_sem();
318 322
319 ld = tty_ldisc_ref_wait(tty); 323 ld = tty_ldisc_ref(tty);
320 324 if (!ld) {
325 tty_unlock();
326 ld = tty_ldisc_ref_wait(tty);
327 tty_lock();
328 }
329
321 add_wait_queue(&vc->paste_wait, &wait); 330 add_wait_queue(&vc->paste_wait, &wait);
322 while (sel_buffer && sel_buffer_lth > pasted) { 331 while (sel_buffer && sel_buffer_lth > pasted) {
323 set_current_state(TASK_INTERRUPTIBLE); 332 set_current_state(TASK_INTERRUPTIBLE);
diff --git a/drivers/char/serial167.c b/drivers/char/serial167.c
index ecbe479c7d6..f646725bd56 100644
--- a/drivers/char/serial167.c
+++ b/drivers/char/serial167.c
@@ -1505,7 +1505,7 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
1505 printk("cy_ioctl %s, cmd = %x arg = %lx\n", tty->name, cmd, arg); /* */ 1505 printk("cy_ioctl %s, cmd = %x arg = %lx\n", tty->name, cmd, arg); /* */
1506#endif 1506#endif
1507 1507
1508 lock_kernel(); 1508 tty_lock();
1509 1509
1510 switch (cmd) { 1510 switch (cmd) {
1511 case CYGETMON: 1511 case CYGETMON:
@@ -1561,7 +1561,7 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
1561 default: 1561 default:
1562 ret_val = -ENOIOCTLCMD; 1562 ret_val = -ENOIOCTLCMD;
1563 } 1563 }
1564 unlock_kernel(); 1564 tty_unlock();
1565 1565
1566#ifdef SERIAL_DEBUG_OTHER 1566#ifdef SERIAL_DEBUG_OTHER
1567 printk("cy_ioctl done\n"); 1567 printk("cy_ioctl done\n");
@@ -1786,7 +1786,9 @@ block_til_ready(struct tty_struct *tty, struct file *filp,
1786 tty->name, info->count); 1786 tty->name, info->count);
1787 /**/ 1787 /**/
1788#endif 1788#endif
1789 schedule(); 1789 tty_unlock();
1790 schedule();
1791 tty_lock();
1790 } 1792 }
1791 __set_current_state(TASK_RUNNING); 1793 __set_current_state(TASK_RUNNING);
1792 remove_wait_queue(&info->open_wait, &wait); 1794 remove_wait_queue(&info->open_wait, &wait);
diff --git a/drivers/char/specialix.c b/drivers/char/specialix.c
index 2c24fcdc722..9f8495b4fc8 100644
--- a/drivers/char/specialix.c
+++ b/drivers/char/specialix.c
@@ -1365,7 +1365,9 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
1365 retval = -ERESTARTSYS; 1365 retval = -ERESTARTSYS;
1366 break; 1366 break;
1367 } 1367 }
1368 tty_unlock();
1368 schedule(); 1369 schedule();
1370 tty_lock();
1369 } 1371 }
1370 1372
1371 set_current_state(TASK_RUNNING); 1373 set_current_state(TASK_RUNNING);
@@ -1863,8 +1865,7 @@ static int sx_set_serial_info(struct specialix_port *port,
1863 return -EFAULT; 1865 return -EFAULT;
1864 } 1866 }
1865 1867
1866 lock_kernel(); 1868 mutex_lock(&port->port.mutex);
1867
1868 change_speed = ((port->port.flags & ASYNC_SPD_MASK) != 1869 change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
1869 (tmp.flags & ASYNC_SPD_MASK)); 1870 (tmp.flags & ASYNC_SPD_MASK));
1870 change_speed |= (tmp.custom_divisor != port->custom_divisor); 1871 change_speed |= (tmp.custom_divisor != port->custom_divisor);
@@ -1875,7 +1876,7 @@ static int sx_set_serial_info(struct specialix_port *port,
1875 ((tmp.flags & ~ASYNC_USR_MASK) != 1876 ((tmp.flags & ~ASYNC_USR_MASK) !=
1876 (port->port.flags & ~ASYNC_USR_MASK))) { 1877 (port->port.flags & ~ASYNC_USR_MASK))) {
1877 func_exit(); 1878 func_exit();
1878 unlock_kernel(); 1879 mutex_unlock(&port->port.mutex);
1879 return -EPERM; 1880 return -EPERM;
1880 } 1881 }
1881 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) | 1882 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
@@ -1892,7 +1893,7 @@ static int sx_set_serial_info(struct specialix_port *port,
1892 sx_change_speed(bp, port); 1893 sx_change_speed(bp, port);
1893 1894
1894 func_exit(); 1895 func_exit();
1895 unlock_kernel(); 1896 mutex_unlock(&port->port.mutex);
1896 return 0; 1897 return 0;
1897} 1898}
1898 1899
@@ -1906,7 +1907,7 @@ static int sx_get_serial_info(struct specialix_port *port,
1906 func_enter(); 1907 func_enter();
1907 1908
1908 memset(&tmp, 0, sizeof(tmp)); 1909 memset(&tmp, 0, sizeof(tmp));
1909 lock_kernel(); 1910 mutex_lock(&port->port.mutex);
1910 tmp.type = PORT_CIRRUS; 1911 tmp.type = PORT_CIRRUS;
1911 tmp.line = port - sx_port; 1912 tmp.line = port - sx_port;
1912 tmp.port = bp->base; 1913 tmp.port = bp->base;
@@ -1917,7 +1918,7 @@ static int sx_get_serial_info(struct specialix_port *port,
1917 tmp.closing_wait = port->port.closing_wait * HZ/100; 1918 tmp.closing_wait = port->port.closing_wait * HZ/100;
1918 tmp.custom_divisor = port->custom_divisor; 1919 tmp.custom_divisor = port->custom_divisor;
1919 tmp.xmit_fifo_size = CD186x_NFIFO; 1920 tmp.xmit_fifo_size = CD186x_NFIFO;
1920 unlock_kernel(); 1921 mutex_unlock(&port->port.mutex);
1921 if (copy_to_user(retinfo, &tmp, sizeof(tmp))) { 1922 if (copy_to_user(retinfo, &tmp, sizeof(tmp))) {
1922 func_exit(); 1923 func_exit();
1923 return -EFAULT; 1924 return -EFAULT;
diff --git a/drivers/char/stallion.c b/drivers/char/stallion.c
index 6049fd73192..f2167f8e5aa 100644
--- a/drivers/char/stallion.c
+++ b/drivers/char/stallion.c
@@ -807,7 +807,6 @@ static void stl_waituntilsent(struct tty_struct *tty, int timeout)
807 timeout = HZ; 807 timeout = HZ;
808 tend = jiffies + timeout; 808 tend = jiffies + timeout;
809 809
810 lock_kernel();
811 while (stl_datastate(portp)) { 810 while (stl_datastate(portp)) {
812 if (signal_pending(current)) 811 if (signal_pending(current))
813 break; 812 break;
@@ -815,7 +814,6 @@ static void stl_waituntilsent(struct tty_struct *tty, int timeout)
815 if (time_after_eq(jiffies, tend)) 814 if (time_after_eq(jiffies, tend))
816 break; 815 break;
817 } 816 }
818 unlock_kernel();
819} 817}
820 818
821/*****************************************************************************/ 819/*****************************************************************************/
@@ -1029,6 +1027,8 @@ static int stl_getserial(struct stlport *portp, struct serial_struct __user *sp)
1029 pr_debug("stl_getserial(portp=%p,sp=%p)\n", portp, sp); 1027 pr_debug("stl_getserial(portp=%p,sp=%p)\n", portp, sp);
1030 1028
1031 memset(&sio, 0, sizeof(struct serial_struct)); 1029 memset(&sio, 0, sizeof(struct serial_struct));
1030
1031 mutex_lock(&portp->port.mutex);
1032 sio.line = portp->portnr; 1032 sio.line = portp->portnr;
1033 sio.port = portp->ioaddr; 1033 sio.port = portp->ioaddr;
1034 sio.flags = portp->port.flags; 1034 sio.flags = portp->port.flags;
@@ -1048,6 +1048,7 @@ static int stl_getserial(struct stlport *portp, struct serial_struct __user *sp)
1048 brdp = stl_brds[portp->brdnr]; 1048 brdp = stl_brds[portp->brdnr];
1049 if (brdp != NULL) 1049 if (brdp != NULL)
1050 sio.irq = brdp->irq; 1050 sio.irq = brdp->irq;
1051 mutex_unlock(&portp->port.mutex);
1051 1052
1052 return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0; 1053 return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0;
1053} 1054}
@@ -1069,12 +1070,15 @@ static int stl_setserial(struct tty_struct *tty, struct serial_struct __user *sp
1069 1070
1070 if (copy_from_user(&sio, sp, sizeof(struct serial_struct))) 1071 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1071 return -EFAULT; 1072 return -EFAULT;
1073 mutex_lock(&portp->port.mutex);
1072 if (!capable(CAP_SYS_ADMIN)) { 1074 if (!capable(CAP_SYS_ADMIN)) {
1073 if ((sio.baud_base != portp->baud_base) || 1075 if ((sio.baud_base != portp->baud_base) ||
1074 (sio.close_delay != portp->close_delay) || 1076 (sio.close_delay != portp->close_delay) ||
1075 ((sio.flags & ~ASYNC_USR_MASK) != 1077 ((sio.flags & ~ASYNC_USR_MASK) !=
1076 (portp->port.flags & ~ASYNC_USR_MASK))) 1078 (portp->port.flags & ~ASYNC_USR_MASK))) {
1079 mutex_unlock(&portp->port.mutex);
1077 return -EPERM; 1080 return -EPERM;
1081 }
1078 } 1082 }
1079 1083
1080 portp->port.flags = (portp->port.flags & ~ASYNC_USR_MASK) | 1084 portp->port.flags = (portp->port.flags & ~ASYNC_USR_MASK) |
@@ -1083,6 +1087,7 @@ static int stl_setserial(struct tty_struct *tty, struct serial_struct __user *sp
1083 portp->close_delay = sio.close_delay; 1087 portp->close_delay = sio.close_delay;
1084 portp->closing_wait = sio.closing_wait; 1088 portp->closing_wait = sio.closing_wait;
1085 portp->custom_divisor = sio.custom_divisor; 1089 portp->custom_divisor = sio.custom_divisor;
1090 mutex_unlock(&portp->port.mutex);
1086 stl_setport(portp, tty->termios); 1091 stl_setport(portp, tty->termios);
1087 return 0; 1092 return 0;
1088} 1093}
@@ -1147,8 +1152,6 @@ static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd
1147 1152
1148 rc = 0; 1153 rc = 0;
1149 1154
1150 lock_kernel();
1151
1152 switch (cmd) { 1155 switch (cmd) {
1153 case TIOCGSERIAL: 1156 case TIOCGSERIAL:
1154 rc = stl_getserial(portp, argp); 1157 rc = stl_getserial(portp, argp);
@@ -1173,7 +1176,6 @@ static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd
1173 rc = -ENOIOCTLCMD; 1176 rc = -ENOIOCTLCMD;
1174 break; 1177 break;
1175 } 1178 }
1176 unlock_kernel();
1177 return rc; 1179 return rc;
1178} 1180}
1179 1181
@@ -2327,6 +2329,7 @@ static int stl_getportstats(struct tty_struct *tty, struct stlport *portp, comst
2327 return -ENODEV; 2329 return -ENODEV;
2328 } 2330 }
2329 2331
2332 mutex_lock(&portp->port.mutex);
2330 portp->stats.state = portp->istate; 2333 portp->stats.state = portp->istate;
2331 portp->stats.flags = portp->port.flags; 2334 portp->stats.flags = portp->port.flags;
2332 portp->stats.hwid = portp->hwid; 2335 portp->stats.hwid = portp->hwid;
@@ -2358,6 +2361,7 @@ static int stl_getportstats(struct tty_struct *tty, struct stlport *portp, comst
2358 (STL_TXBUFSIZE - (tail - head)); 2361 (STL_TXBUFSIZE - (tail - head));
2359 2362
2360 portp->stats.signals = (unsigned long) stl_getsignals(portp); 2363 portp->stats.signals = (unsigned long) stl_getsignals(portp);
2364 mutex_unlock(&portp->port.mutex);
2361 2365
2362 return copy_to_user(cp, &portp->stats, 2366 return copy_to_user(cp, &portp->stats,
2363 sizeof(comstats_t)) ? -EFAULT : 0; 2367 sizeof(comstats_t)) ? -EFAULT : 0;
@@ -2382,10 +2386,12 @@ static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp)
2382 return -ENODEV; 2386 return -ENODEV;
2383 } 2387 }
2384 2388
2389 mutex_lock(&portp->port.mutex);
2385 memset(&portp->stats, 0, sizeof(comstats_t)); 2390 memset(&portp->stats, 0, sizeof(comstats_t));
2386 portp->stats.brd = portp->brdnr; 2391 portp->stats.brd = portp->brdnr;
2387 portp->stats.panel = portp->panelnr; 2392 portp->stats.panel = portp->panelnr;
2388 portp->stats.port = portp->portnr; 2393 portp->stats.port = portp->portnr;
2394 mutex_unlock(&portp->port.mutex);
2389 return copy_to_user(cp, &portp->stats, 2395 return copy_to_user(cp, &portp->stats,
2390 sizeof(comstats_t)) ? -EFAULT : 0; 2396 sizeof(comstats_t)) ? -EFAULT : 0;
2391} 2397}
@@ -2451,7 +2457,6 @@ static long stl_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
2451 return -ENODEV; 2457 return -ENODEV;
2452 rc = 0; 2458 rc = 0;
2453 2459
2454 lock_kernel();
2455 switch (cmd) { 2460 switch (cmd) {
2456 case COM_GETPORTSTATS: 2461 case COM_GETPORTSTATS:
2457 rc = stl_getportstats(NULL, NULL, argp); 2462 rc = stl_getportstats(NULL, NULL, argp);
@@ -2472,7 +2477,6 @@ static long stl_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
2472 rc = -ENOIOCTLCMD; 2477 rc = -ENOIOCTLCMD;
2473 break; 2478 break;
2474 } 2479 }
2475 unlock_kernel();
2476 return rc; 2480 return rc;
2477} 2481}
2478 2482
diff --git a/drivers/char/sx.c b/drivers/char/sx.c
index a81ec4fcf6f..5b24db4ff7f 100644
--- a/drivers/char/sx.c
+++ b/drivers/char/sx.c
@@ -1699,7 +1699,7 @@ static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
1699 if (!capable(CAP_SYS_RAWIO)) 1699 if (!capable(CAP_SYS_RAWIO))
1700 return -EPERM; 1700 return -EPERM;
1701 1701
1702 lock_kernel(); 1702 tty_lock();
1703 1703
1704 sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg); 1704 sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
1705 1705
@@ -1848,7 +1848,7 @@ static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
1848 break; 1848 break;
1849 } 1849 }
1850out: 1850out:
1851 unlock_kernel(); 1851 tty_unlock();
1852 func_exit(); 1852 func_exit();
1853 return rc; 1853 return rc;
1854} 1854}
@@ -1859,7 +1859,7 @@ static int sx_break(struct tty_struct *tty, int flag)
1859 int rv; 1859 int rv;
1860 1860
1861 func_enter(); 1861 func_enter();
1862 lock_kernel(); 1862 tty_lock();
1863 1863
1864 if (flag) 1864 if (flag)
1865 rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK); 1865 rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK);
@@ -1868,7 +1868,7 @@ static int sx_break(struct tty_struct *tty, int flag)
1868 if (rv != 1) 1868 if (rv != 1)
1869 printk(KERN_ERR "sx: couldn't send break (%x).\n", 1869 printk(KERN_ERR "sx: couldn't send break (%x).\n",
1870 read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat))); 1870 read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat)));
1871 unlock_kernel(); 1871 tty_unlock();
1872 func_exit(); 1872 func_exit();
1873 return 0; 1873 return 0;
1874} 1874}
@@ -1909,7 +1909,7 @@ static int sx_ioctl(struct tty_struct *tty, struct file *filp,
1909 /* func_enter2(); */ 1909 /* func_enter2(); */
1910 1910
1911 rc = 0; 1911 rc = 0;
1912 lock_kernel(); 1912 tty_lock();
1913 switch (cmd) { 1913 switch (cmd) {
1914 case TIOCGSERIAL: 1914 case TIOCGSERIAL:
1915 rc = gs_getserial(&port->gs, argp); 1915 rc = gs_getserial(&port->gs, argp);
@@ -1921,7 +1921,7 @@ static int sx_ioctl(struct tty_struct *tty, struct file *filp,
1921 rc = -ENOIOCTLCMD; 1921 rc = -ENOIOCTLCMD;
1922 break; 1922 break;
1923 } 1923 }
1924 unlock_kernel(); 1924 tty_unlock();
1925 1925
1926 /* func_exit(); */ 1926 /* func_exit(); */
1927 return rc; 1927 return rc;
diff --git a/drivers/char/synclink.c b/drivers/char/synclink.c
index 0658fc54822..a2a58004e18 100644
--- a/drivers/char/synclink.c
+++ b/drivers/char/synclink.c
@@ -81,7 +81,6 @@
81#include <linux/mm.h> 81#include <linux/mm.h>
82#include <linux/seq_file.h> 82#include <linux/seq_file.h>
83#include <linux/slab.h> 83#include <linux/slab.h>
84#include <linux/smp_lock.h>
85#include <linux/delay.h> 84#include <linux/delay.h>
86#include <linux/netdevice.h> 85#include <linux/netdevice.h>
87#include <linux/vmalloc.h> 86#include <linux/vmalloc.h>
@@ -2436,7 +2435,9 @@ static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount __user *
2436 if (!user_icount) { 2435 if (!user_icount) {
2437 memset(&info->icount, 0, sizeof(info->icount)); 2436 memset(&info->icount, 0, sizeof(info->icount));
2438 } else { 2437 } else {
2438 mutex_lock(&info->port.mutex);
2439 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount)); 2439 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
2440 mutex_unlock(&info->port.mutex);
2440 if (err) 2441 if (err)
2441 return -EFAULT; 2442 return -EFAULT;
2442 } 2443 }
@@ -2461,7 +2462,9 @@ static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_p
2461 printk("%s(%d):mgsl_get_params(%s)\n", 2462 printk("%s(%d):mgsl_get_params(%s)\n",
2462 __FILE__,__LINE__, info->device_name); 2463 __FILE__,__LINE__, info->device_name);
2463 2464
2465 mutex_lock(&info->port.mutex);
2464 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS)); 2466 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
2467 mutex_unlock(&info->port.mutex);
2465 if (err) { 2468 if (err) {
2466 if ( debug_level >= DEBUG_LEVEL_INFO ) 2469 if ( debug_level >= DEBUG_LEVEL_INFO )
2467 printk( "%s(%d):mgsl_get_params(%s) user buffer copy failed\n", 2470 printk( "%s(%d):mgsl_get_params(%s) user buffer copy failed\n",
@@ -2501,11 +2504,13 @@ static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_pa
2501 return -EFAULT; 2504 return -EFAULT;
2502 } 2505 }
2503 2506
2507 mutex_lock(&info->port.mutex);
2504 spin_lock_irqsave(&info->irq_spinlock,flags); 2508 spin_lock_irqsave(&info->irq_spinlock,flags);
2505 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS)); 2509 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2506 spin_unlock_irqrestore(&info->irq_spinlock,flags); 2510 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2507 2511
2508 mgsl_change_params(info); 2512 mgsl_change_params(info);
2513 mutex_unlock(&info->port.mutex);
2509 2514
2510 return 0; 2515 return 0;
2511 2516
@@ -2935,7 +2940,6 @@ static int mgsl_ioctl(struct tty_struct *tty, struct file * file,
2935 unsigned int cmd, unsigned long arg) 2940 unsigned int cmd, unsigned long arg)
2936{ 2941{
2937 struct mgsl_struct * info = tty->driver_data; 2942 struct mgsl_struct * info = tty->driver_data;
2938 int ret;
2939 2943
2940 if (debug_level >= DEBUG_LEVEL_INFO) 2944 if (debug_level >= DEBUG_LEVEL_INFO)
2941 printk("%s(%d):mgsl_ioctl %s cmd=%08X\n", __FILE__,__LINE__, 2945 printk("%s(%d):mgsl_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
@@ -2950,10 +2954,7 @@ static int mgsl_ioctl(struct tty_struct *tty, struct file * file,
2950 return -EIO; 2954 return -EIO;
2951 } 2955 }
2952 2956
2953 lock_kernel(); 2957 return mgsl_ioctl_common(info, cmd, arg);
2954 ret = mgsl_ioctl_common(info, cmd, arg);
2955 unlock_kernel();
2956 return ret;
2957} 2958}
2958 2959
2959static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg) 2960static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg)
@@ -3109,12 +3110,14 @@ static void mgsl_close(struct tty_struct *tty, struct file * filp)
3109 3110
3110 if (tty_port_close_start(&info->port, tty, filp) == 0) 3111 if (tty_port_close_start(&info->port, tty, filp) == 0)
3111 goto cleanup; 3112 goto cleanup;
3112 3113
3114 mutex_lock(&info->port.mutex);
3113 if (info->port.flags & ASYNC_INITIALIZED) 3115 if (info->port.flags & ASYNC_INITIALIZED)
3114 mgsl_wait_until_sent(tty, info->timeout); 3116 mgsl_wait_until_sent(tty, info->timeout);
3115 mgsl_flush_buffer(tty); 3117 mgsl_flush_buffer(tty);
3116 tty_ldisc_flush(tty); 3118 tty_ldisc_flush(tty);
3117 shutdown(info); 3119 shutdown(info);
3120 mutex_unlock(&info->port.mutex);
3118 3121
3119 tty_port_close_end(&info->port, tty); 3122 tty_port_close_end(&info->port, tty);
3120 info->port.tty = NULL; 3123 info->port.tty = NULL;
@@ -3162,7 +3165,6 @@ static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout)
3162 * Note: use tight timings here to satisfy the NIST-PCTS. 3165 * Note: use tight timings here to satisfy the NIST-PCTS.
3163 */ 3166 */
3164 3167
3165 lock_kernel();
3166 if ( info->params.data_rate ) { 3168 if ( info->params.data_rate ) {
3167 char_time = info->timeout/(32 * 5); 3169 char_time = info->timeout/(32 * 5);
3168 if (!char_time) 3170 if (!char_time)
@@ -3192,7 +3194,6 @@ static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout)
3192 break; 3194 break;
3193 } 3195 }
3194 } 3196 }
3195 unlock_kernel();
3196 3197
3197exit: 3198exit:
3198 if (debug_level >= DEBUG_LEVEL_INFO) 3199 if (debug_level >= DEBUG_LEVEL_INFO)
@@ -3348,7 +3349,9 @@ static int block_til_ready(struct tty_struct *tty, struct file * filp,
3348 printk("%s(%d):block_til_ready blocking on %s count=%d\n", 3349 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
3349 __FILE__,__LINE__, tty->driver->name, port->count ); 3350 __FILE__,__LINE__, tty->driver->name, port->count );
3350 3351
3352 tty_unlock();
3351 schedule(); 3353 schedule();
3354 tty_lock();
3352 } 3355 }
3353 3356
3354 set_current_state(TASK_RUNNING); 3357 set_current_state(TASK_RUNNING);
diff --git a/drivers/char/synclink_gt.c b/drivers/char/synclink_gt.c
index 334cf5c8c8b..fef80cfcab5 100644
--- a/drivers/char/synclink_gt.c
+++ b/drivers/char/synclink_gt.c
@@ -40,8 +40,8 @@
40#define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt 40#define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
41#define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt 41#define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
42#define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label)) 42#define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
43//#define DBGTBUF(info) dump_tbufs(info) 43/*#define DBGTBUF(info) dump_tbufs(info)*/
44//#define DBGRBUF(info) dump_rbufs(info) 44/*#define DBGRBUF(info) dump_rbufs(info)*/
45 45
46 46
47#include <linux/module.h> 47#include <linux/module.h>
@@ -62,7 +62,6 @@
62#include <linux/mm.h> 62#include <linux/mm.h>
63#include <linux/seq_file.h> 63#include <linux/seq_file.h>
64#include <linux/slab.h> 64#include <linux/slab.h>
65#include <linux/smp_lock.h>
66#include <linux/netdevice.h> 65#include <linux/netdevice.h>
67#include <linux/vmalloc.h> 66#include <linux/vmalloc.h>
68#include <linux/init.h> 67#include <linux/init.h>
@@ -676,12 +675,14 @@ static int open(struct tty_struct *tty, struct file *filp)
676 goto cleanup; 675 goto cleanup;
677 } 676 }
678 677
678 mutex_lock(&info->port.mutex);
679 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0; 679 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
680 680
681 spin_lock_irqsave(&info->netlock, flags); 681 spin_lock_irqsave(&info->netlock, flags);
682 if (info->netcount) { 682 if (info->netcount) {
683 retval = -EBUSY; 683 retval = -EBUSY;
684 spin_unlock_irqrestore(&info->netlock, flags); 684 spin_unlock_irqrestore(&info->netlock, flags);
685 mutex_unlock(&info->port.mutex);
685 goto cleanup; 686 goto cleanup;
686 } 687 }
687 info->port.count++; 688 info->port.count++;
@@ -693,7 +694,7 @@ static int open(struct tty_struct *tty, struct file *filp)
693 if (retval < 0) 694 if (retval < 0)
694 goto cleanup; 695 goto cleanup;
695 } 696 }
696 697 mutex_unlock(&info->port.mutex);
697 retval = block_til_ready(tty, filp, info); 698 retval = block_til_ready(tty, filp, info);
698 if (retval) { 699 if (retval) {
699 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval)); 700 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
@@ -725,12 +726,14 @@ static void close(struct tty_struct *tty, struct file *filp)
725 if (tty_port_close_start(&info->port, tty, filp) == 0) 726 if (tty_port_close_start(&info->port, tty, filp) == 0)
726 goto cleanup; 727 goto cleanup;
727 728
729 mutex_lock(&info->port.mutex);
728 if (info->port.flags & ASYNC_INITIALIZED) 730 if (info->port.flags & ASYNC_INITIALIZED)
729 wait_until_sent(tty, info->timeout); 731 wait_until_sent(tty, info->timeout);
730 flush_buffer(tty); 732 flush_buffer(tty);
731 tty_ldisc_flush(tty); 733 tty_ldisc_flush(tty);
732 734
733 shutdown(info); 735 shutdown(info);
736 mutex_unlock(&info->port.mutex);
734 737
735 tty_port_close_end(&info->port, tty); 738 tty_port_close_end(&info->port, tty);
736 info->port.tty = NULL; 739 info->port.tty = NULL;
@@ -741,17 +744,23 @@ cleanup:
741static void hangup(struct tty_struct *tty) 744static void hangup(struct tty_struct *tty)
742{ 745{
743 struct slgt_info *info = tty->driver_data; 746 struct slgt_info *info = tty->driver_data;
747 unsigned long flags;
744 748
745 if (sanity_check(info, tty->name, "hangup")) 749 if (sanity_check(info, tty->name, "hangup"))
746 return; 750 return;
747 DBGINFO(("%s hangup\n", info->device_name)); 751 DBGINFO(("%s hangup\n", info->device_name));
748 752
749 flush_buffer(tty); 753 flush_buffer(tty);
754
755 mutex_lock(&info->port.mutex);
750 shutdown(info); 756 shutdown(info);
751 757
758 spin_lock_irqsave(&info->port.lock, flags);
752 info->port.count = 0; 759 info->port.count = 0;
753 info->port.flags &= ~ASYNC_NORMAL_ACTIVE; 760 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
754 info->port.tty = NULL; 761 info->port.tty = NULL;
762 spin_unlock_irqrestore(&info->port.lock, flags);
763 mutex_unlock(&info->port.mutex);
755 764
756 wake_up_interruptible(&info->port.open_wait); 765 wake_up_interruptible(&info->port.open_wait);
757} 766}
@@ -901,8 +910,6 @@ static void wait_until_sent(struct tty_struct *tty, int timeout)
901 * Note: use tight timings here to satisfy the NIST-PCTS. 910 * Note: use tight timings here to satisfy the NIST-PCTS.
902 */ 911 */
903 912
904 lock_kernel();
905
906 if (info->params.data_rate) { 913 if (info->params.data_rate) {
907 char_time = info->timeout/(32 * 5); 914 char_time = info->timeout/(32 * 5);
908 if (!char_time) 915 if (!char_time)
@@ -920,8 +927,6 @@ static void wait_until_sent(struct tty_struct *tty, int timeout)
920 if (timeout && time_after(jiffies, orig_jiffies + timeout)) 927 if (timeout && time_after(jiffies, orig_jiffies + timeout))
921 break; 928 break;
922 } 929 }
923 unlock_kernel();
924
925exit: 930exit:
926 DBGINFO(("%s wait_until_sent exit\n", info->device_name)); 931 DBGINFO(("%s wait_until_sent exit\n", info->device_name));
927} 932}
@@ -1041,8 +1046,37 @@ static int ioctl(struct tty_struct *tty, struct file *file,
1041 return -EIO; 1046 return -EIO;
1042 } 1047 }
1043 1048
1044 lock_kernel(); 1049 switch (cmd) {
1045 1050 case MGSL_IOCWAITEVENT:
1051 return wait_mgsl_event(info, argp);
1052 case TIOCMIWAIT:
1053 return modem_input_wait(info,(int)arg);
1054 case TIOCGICOUNT:
1055 spin_lock_irqsave(&info->lock,flags);
1056 cnow = info->icount;
1057 spin_unlock_irqrestore(&info->lock,flags);
1058 p_cuser = argp;
1059 if (put_user(cnow.cts, &p_cuser->cts) ||
1060 put_user(cnow.dsr, &p_cuser->dsr) ||
1061 put_user(cnow.rng, &p_cuser->rng) ||
1062 put_user(cnow.dcd, &p_cuser->dcd) ||
1063 put_user(cnow.rx, &p_cuser->rx) ||
1064 put_user(cnow.tx, &p_cuser->tx) ||
1065 put_user(cnow.frame, &p_cuser->frame) ||
1066 put_user(cnow.overrun, &p_cuser->overrun) ||
1067 put_user(cnow.parity, &p_cuser->parity) ||
1068 put_user(cnow.brk, &p_cuser->brk) ||
1069 put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1070 return -EFAULT;
1071 return 0;
1072 case MGSL_IOCSGPIO:
1073 return set_gpio(info, argp);
1074 case MGSL_IOCGGPIO:
1075 return get_gpio(info, argp);
1076 case MGSL_IOCWAITGPIO:
1077 return wait_gpio(info, argp);
1078 }
1079 mutex_lock(&info->port.mutex);
1046 switch (cmd) { 1080 switch (cmd) {
1047 case MGSL_IOCGPARAMS: 1081 case MGSL_IOCGPARAMS:
1048 ret = get_params(info, argp); 1082 ret = get_params(info, argp);
@@ -1068,50 +1102,16 @@ static int ioctl(struct tty_struct *tty, struct file *file,
1068 case MGSL_IOCGSTATS: 1102 case MGSL_IOCGSTATS:
1069 ret = get_stats(info, argp); 1103 ret = get_stats(info, argp);
1070 break; 1104 break;
1071 case MGSL_IOCWAITEVENT:
1072 ret = wait_mgsl_event(info, argp);
1073 break;
1074 case TIOCMIWAIT:
1075 ret = modem_input_wait(info,(int)arg);
1076 break;
1077 case MGSL_IOCGIF: 1105 case MGSL_IOCGIF:
1078 ret = get_interface(info, argp); 1106 ret = get_interface(info, argp);
1079 break; 1107 break;
1080 case MGSL_IOCSIF: 1108 case MGSL_IOCSIF:
1081 ret = set_interface(info,(int)arg); 1109 ret = set_interface(info,(int)arg);
1082 break; 1110 break;
1083 case MGSL_IOCSGPIO:
1084 ret = set_gpio(info, argp);
1085 break;
1086 case MGSL_IOCGGPIO:
1087 ret = get_gpio(info, argp);
1088 break;
1089 case MGSL_IOCWAITGPIO:
1090 ret = wait_gpio(info, argp);
1091 break;
1092 case TIOCGICOUNT:
1093 spin_lock_irqsave(&info->lock,flags);
1094 cnow = info->icount;
1095 spin_unlock_irqrestore(&info->lock,flags);
1096 p_cuser = argp;
1097 if (put_user(cnow.cts, &p_cuser->cts) ||
1098 put_user(cnow.dsr, &p_cuser->dsr) ||
1099 put_user(cnow.rng, &p_cuser->rng) ||
1100 put_user(cnow.dcd, &p_cuser->dcd) ||
1101 put_user(cnow.rx, &p_cuser->rx) ||
1102 put_user(cnow.tx, &p_cuser->tx) ||
1103 put_user(cnow.frame, &p_cuser->frame) ||
1104 put_user(cnow.overrun, &p_cuser->overrun) ||
1105 put_user(cnow.parity, &p_cuser->parity) ||
1106 put_user(cnow.brk, &p_cuser->brk) ||
1107 put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1108 ret = -EFAULT;
1109 ret = 0;
1110 break;
1111 default: 1111 default:
1112 ret = -ENOIOCTLCMD; 1112 ret = -ENOIOCTLCMD;
1113 } 1113 }
1114 unlock_kernel(); 1114 mutex_unlock(&info->port.mutex);
1115 return ret; 1115 return ret;
1116} 1116}
1117 1117
@@ -3244,7 +3244,9 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
3244 } 3244 }
3245 3245
3246 DBGINFO(("%s block_til_ready wait\n", tty->driver->name)); 3246 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3247 tty_unlock();
3247 schedule(); 3248 schedule();
3249 tty_lock();
3248 } 3250 }
3249 3251
3250 set_current_state(TASK_RUNNING); 3252 set_current_state(TASK_RUNNING);
diff --git a/drivers/char/synclinkmp.c b/drivers/char/synclinkmp.c
index 2b18adc4ee1..e56caf7d82a 100644
--- a/drivers/char/synclinkmp.c
+++ b/drivers/char/synclinkmp.c
@@ -52,7 +52,6 @@
52#include <linux/mm.h> 52#include <linux/mm.h>
53#include <linux/seq_file.h> 53#include <linux/seq_file.h>
54#include <linux/slab.h> 54#include <linux/slab.h>
55#include <linux/smp_lock.h>
56#include <linux/netdevice.h> 55#include <linux/netdevice.h>
57#include <linux/vmalloc.h> 56#include <linux/vmalloc.h>
58#include <linux/init.h> 57#include <linux/init.h>
@@ -813,13 +812,15 @@ static void close(struct tty_struct *tty, struct file *filp)
813 812
814 if (tty_port_close_start(&info->port, tty, filp) == 0) 813 if (tty_port_close_start(&info->port, tty, filp) == 0)
815 goto cleanup; 814 goto cleanup;
816 815
816 mutex_lock(&info->port.mutex);
817 if (info->port.flags & ASYNC_INITIALIZED) 817 if (info->port.flags & ASYNC_INITIALIZED)
818 wait_until_sent(tty, info->timeout); 818 wait_until_sent(tty, info->timeout);
819 819
820 flush_buffer(tty); 820 flush_buffer(tty);
821 tty_ldisc_flush(tty); 821 tty_ldisc_flush(tty);
822 shutdown(info); 822 shutdown(info);
823 mutex_unlock(&info->port.mutex);
823 824
824 tty_port_close_end(&info->port, tty); 825 tty_port_close_end(&info->port, tty);
825 info->port.tty = NULL; 826 info->port.tty = NULL;
@@ -835,6 +836,7 @@ cleanup:
835static void hangup(struct tty_struct *tty) 836static void hangup(struct tty_struct *tty)
836{ 837{
837 SLMP_INFO *info = tty->driver_data; 838 SLMP_INFO *info = tty->driver_data;
839 unsigned long flags;
838 840
839 if (debug_level >= DEBUG_LEVEL_INFO) 841 if (debug_level >= DEBUG_LEVEL_INFO)
840 printk("%s(%d):%s hangup()\n", 842 printk("%s(%d):%s hangup()\n",
@@ -843,12 +845,16 @@ static void hangup(struct tty_struct *tty)
843 if (sanity_check(info, tty->name, "hangup")) 845 if (sanity_check(info, tty->name, "hangup"))
844 return; 846 return;
845 847
848 mutex_lock(&info->port.mutex);
846 flush_buffer(tty); 849 flush_buffer(tty);
847 shutdown(info); 850 shutdown(info);
848 851
852 spin_lock_irqsave(&info->port.lock, flags);
849 info->port.count = 0; 853 info->port.count = 0;
850 info->port.flags &= ~ASYNC_NORMAL_ACTIVE; 854 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
851 info->port.tty = NULL; 855 info->port.tty = NULL;
856 spin_unlock_irqrestore(&info->port.lock, flags);
857 mutex_unlock(&info->port.mutex);
852 858
853 wake_up_interruptible(&info->port.open_wait); 859 wake_up_interruptible(&info->port.open_wait);
854} 860}
@@ -1062,9 +1068,7 @@ static void wait_until_sent(struct tty_struct *tty, int timeout)
1062 if (sanity_check(info, tty->name, "wait_until_sent")) 1068 if (sanity_check(info, tty->name, "wait_until_sent"))
1063 return; 1069 return;
1064 1070
1065 lock_kernel(); 1071 if (!test_bit(ASYNCB_INITIALIZED, &info->port.flags))
1066
1067 if (!(info->port.flags & ASYNC_INITIALIZED))
1068 goto exit; 1072 goto exit;
1069 1073
1070 orig_jiffies = jiffies; 1074 orig_jiffies = jiffies;
@@ -1094,8 +1098,10 @@ static void wait_until_sent(struct tty_struct *tty, int timeout)
1094 break; 1098 break;
1095 } 1099 }
1096 } else { 1100 } else {
1097 //TODO: determine if there is something similar to USC16C32 1101 /*
1098 // TXSTATUS_ALL_SENT status 1102 * TODO: determine if there is something similar to USC16C32
1103 * TXSTATUS_ALL_SENT status
1104 */
1099 while ( info->tx_active && info->tx_enabled) { 1105 while ( info->tx_active && info->tx_enabled) {
1100 msleep_interruptible(jiffies_to_msecs(char_time)); 1106 msleep_interruptible(jiffies_to_msecs(char_time));
1101 if (signal_pending(current)) 1107 if (signal_pending(current))
@@ -1106,7 +1112,6 @@ static void wait_until_sent(struct tty_struct *tty, int timeout)
1106 } 1112 }
1107 1113
1108exit: 1114exit:
1109 unlock_kernel();
1110 if (debug_level >= DEBUG_LEVEL_INFO) 1115 if (debug_level >= DEBUG_LEVEL_INFO)
1111 printk("%s(%d):%s wait_until_sent() exit\n", 1116 printk("%s(%d):%s wait_until_sent() exit\n",
1112 __FILE__,__LINE__, info->device_name ); 1117 __FILE__,__LINE__, info->device_name );
@@ -1122,7 +1127,6 @@ static int write_room(struct tty_struct *tty)
1122 if (sanity_check(info, tty->name, "write_room")) 1127 if (sanity_check(info, tty->name, "write_room"))
1123 return 0; 1128 return 0;
1124 1129
1125 lock_kernel();
1126 if (info->params.mode == MGSL_MODE_HDLC) { 1130 if (info->params.mode == MGSL_MODE_HDLC) {
1127 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE; 1131 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
1128 } else { 1132 } else {
@@ -1130,7 +1134,6 @@ static int write_room(struct tty_struct *tty)
1130 if (ret < 0) 1134 if (ret < 0)
1131 ret = 0; 1135 ret = 0;
1132 } 1136 }
1133 unlock_kernel();
1134 1137
1135 if (debug_level >= DEBUG_LEVEL_INFO) 1138 if (debug_level >= DEBUG_LEVEL_INFO)
1136 printk("%s(%d):%s write_room()=%d\n", 1139 printk("%s(%d):%s write_room()=%d\n",
@@ -1251,7 +1254,7 @@ static void tx_release(struct tty_struct *tty)
1251 * 1254 *
1252 * Return Value: 0 if success, otherwise error code 1255 * Return Value: 0 if success, otherwise error code
1253 */ 1256 */
1254static int do_ioctl(struct tty_struct *tty, struct file *file, 1257static int ioctl(struct tty_struct *tty, struct file *file,
1255 unsigned int cmd, unsigned long arg) 1258 unsigned int cmd, unsigned long arg)
1256{ 1259{
1257 SLMP_INFO *info = tty->driver_data; 1260 SLMP_INFO *info = tty->driver_data;
@@ -1341,16 +1344,6 @@ static int do_ioctl(struct tty_struct *tty, struct file *file,
1341 return 0; 1344 return 0;
1342} 1345}
1343 1346
1344static int ioctl(struct tty_struct *tty, struct file *file,
1345 unsigned int cmd, unsigned long arg)
1346{
1347 int ret;
1348 lock_kernel();
1349 ret = do_ioctl(tty, file, cmd, arg);
1350 unlock_kernel();
1351 return ret;
1352}
1353
1354/* 1347/*
1355 * /proc fs routines.... 1348 * /proc fs routines....
1356 */ 1349 */
@@ -2883,7 +2876,9 @@ static int get_stats(SLMP_INFO * info, struct mgsl_icount __user *user_icount)
2883 if (!user_icount) { 2876 if (!user_icount) {
2884 memset(&info->icount, 0, sizeof(info->icount)); 2877 memset(&info->icount, 0, sizeof(info->icount));
2885 } else { 2878 } else {
2879 mutex_lock(&info->port.mutex);
2886 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount)); 2880 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
2881 mutex_unlock(&info->port.mutex);
2887 if (err) 2882 if (err)
2888 return -EFAULT; 2883 return -EFAULT;
2889 } 2884 }
@@ -2898,7 +2893,9 @@ static int get_params(SLMP_INFO * info, MGSL_PARAMS __user *user_params)
2898 printk("%s(%d):%s get_params()\n", 2893 printk("%s(%d):%s get_params()\n",
2899 __FILE__,__LINE__, info->device_name); 2894 __FILE__,__LINE__, info->device_name);
2900 2895
2896 mutex_lock(&info->port.mutex);
2901 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS)); 2897 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
2898 mutex_unlock(&info->port.mutex);
2902 if (err) { 2899 if (err) {
2903 if ( debug_level >= DEBUG_LEVEL_INFO ) 2900 if ( debug_level >= DEBUG_LEVEL_INFO )
2904 printk( "%s(%d):%s get_params() user buffer copy failed\n", 2901 printk( "%s(%d):%s get_params() user buffer copy failed\n",
@@ -2926,11 +2923,13 @@ static int set_params(SLMP_INFO * info, MGSL_PARAMS __user *new_params)
2926 return -EFAULT; 2923 return -EFAULT;
2927 } 2924 }
2928 2925
2926 mutex_lock(&info->port.mutex);
2929 spin_lock_irqsave(&info->lock,flags); 2927 spin_lock_irqsave(&info->lock,flags);
2930 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS)); 2928 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2931 spin_unlock_irqrestore(&info->lock,flags); 2929 spin_unlock_irqrestore(&info->lock,flags);
2932 2930
2933 change_params(info); 2931 change_params(info);
2932 mutex_unlock(&info->port.mutex);
2934 2933
2935 return 0; 2934 return 0;
2936} 2935}
@@ -3366,7 +3365,9 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
3366 printk("%s(%d):%s block_til_ready() count=%d\n", 3365 printk("%s(%d):%s block_til_ready() count=%d\n",
3367 __FILE__,__LINE__, tty->driver->name, port->count ); 3366 __FILE__,__LINE__, tty->driver->name, port->count );
3368 3367
3368 tty_unlock();
3369 schedule(); 3369 schedule();
3370 tty_lock();
3370 } 3371 }
3371 3372
3372 set_current_state(TASK_RUNNING); 3373 set_current_state(TASK_RUNNING);
diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
index 507441ac6ed..0350c42375a 100644
--- a/drivers/char/tty_io.c
+++ b/drivers/char/tty_io.c
@@ -149,6 +149,7 @@ static long tty_compat_ioctl(struct file *file, unsigned int cmd,
149#else 149#else
150#define tty_compat_ioctl NULL 150#define tty_compat_ioctl NULL
151#endif 151#endif
152static int __tty_fasync(int fd, struct file *filp, int on);
152static int tty_fasync(int fd, struct file *filp, int on); 153static int tty_fasync(int fd, struct file *filp, int on);
153static void release_tty(struct tty_struct *tty, int idx); 154static void release_tty(struct tty_struct *tty, int idx);
154static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); 155static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
@@ -470,7 +471,7 @@ void tty_wakeup(struct tty_struct *tty)
470EXPORT_SYMBOL_GPL(tty_wakeup); 471EXPORT_SYMBOL_GPL(tty_wakeup);
471 472
472/** 473/**
473 * do_tty_hangup - actual handler for hangup events 474 * __tty_hangup - actual handler for hangup events
474 * @work: tty device 475 * @work: tty device
475 * 476 *
476 * This can be called by the "eventd" kernel thread. That is process 477 * This can be called by the "eventd" kernel thread. That is process
@@ -483,7 +484,7 @@ EXPORT_SYMBOL_GPL(tty_wakeup);
483 * remains intact. 484 * remains intact.
484 * 485 *
485 * Locking: 486 * Locking:
486 * BKL 487 * BTM
487 * redirect lock for undoing redirection 488 * redirect lock for undoing redirection
488 * file list lock for manipulating list of ttys 489 * file list lock for manipulating list of ttys
489 * tty_ldisc_lock from called functions 490 * tty_ldisc_lock from called functions
@@ -491,10 +492,8 @@ EXPORT_SYMBOL_GPL(tty_wakeup);
491 * tasklist_lock to walk task list for hangup event 492 * tasklist_lock to walk task list for hangup event
492 * ->siglock to protect ->signal/->sighand 493 * ->siglock to protect ->signal/->sighand
493 */ 494 */
494static void do_tty_hangup(struct work_struct *work) 495void __tty_hangup(struct tty_struct *tty)
495{ 496{
496 struct tty_struct *tty =
497 container_of(work, struct tty_struct, hangup_work);
498 struct file *cons_filp = NULL; 497 struct file *cons_filp = NULL;
499 struct file *filp, *f = NULL; 498 struct file *filp, *f = NULL;
500 struct task_struct *p; 499 struct task_struct *p;
@@ -513,9 +512,12 @@ static void do_tty_hangup(struct work_struct *work)
513 } 512 }
514 spin_unlock(&redirect_lock); 513 spin_unlock(&redirect_lock);
515 514
516 /* inuse_filps is protected by the single kernel lock */ 515 tty_lock();
517 lock_kernel(); 516
518 check_tty_count(tty, "do_tty_hangup"); 517 /* inuse_filps is protected by the single tty lock,
518 this really needs to change if we want to flush the
519 workqueue with the lock held */
520 check_tty_count(tty, "tty_hangup");
519 521
520 file_list_lock(); 522 file_list_lock();
521 /* This breaks for file handles being sent over AF_UNIX sockets ? */ 523 /* This breaks for file handles being sent over AF_UNIX sockets ? */
@@ -525,7 +527,7 @@ static void do_tty_hangup(struct work_struct *work)
525 if (filp->f_op->write != tty_write) 527 if (filp->f_op->write != tty_write)
526 continue; 528 continue;
527 closecount++; 529 closecount++;
528 tty_fasync(-1, filp, 0); /* can't block */ 530 __tty_fasync(-1, filp, 0); /* can't block */
529 filp->f_op = &hung_up_tty_fops; 531 filp->f_op = &hung_up_tty_fops;
530 } 532 }
531 file_list_unlock(); 533 file_list_unlock();
@@ -594,11 +596,21 @@ static void do_tty_hangup(struct work_struct *work)
594 */ 596 */
595 set_bit(TTY_HUPPED, &tty->flags); 597 set_bit(TTY_HUPPED, &tty->flags);
596 tty_ldisc_enable(tty); 598 tty_ldisc_enable(tty);
597 unlock_kernel(); 599
600 tty_unlock();
601
598 if (f) 602 if (f)
599 fput(f); 603 fput(f);
600} 604}
601 605
606static void do_tty_hangup(struct work_struct *work)
607{
608 struct tty_struct *tty =
609 container_of(work, struct tty_struct, hangup_work);
610
611 __tty_hangup(tty);
612}
613
602/** 614/**
603 * tty_hangup - trigger a hangup event 615 * tty_hangup - trigger a hangup event
604 * @tty: tty to hangup 616 * @tty: tty to hangup
@@ -634,11 +646,12 @@ void tty_vhangup(struct tty_struct *tty)
634 646
635 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf)); 647 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
636#endif 648#endif
637 do_tty_hangup(&tty->hangup_work); 649 __tty_hangup(tty);
638} 650}
639 651
640EXPORT_SYMBOL(tty_vhangup); 652EXPORT_SYMBOL(tty_vhangup);
641 653
654
642/** 655/**
643 * tty_vhangup_self - process vhangup for own ctty 656 * tty_vhangup_self - process vhangup for own ctty
644 * 657 *
@@ -696,7 +709,8 @@ static void session_clear_tty(struct pid *session)
696 * exiting; it is 0 if called by the ioctl TIOCNOTTY. 709 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
697 * 710 *
698 * Locking: 711 * Locking:
699 * BKL is taken for hysterical raisins 712 * BTM is taken for hysterical raisins, and held when
713 * called from no_tty().
700 * tty_mutex is taken to protect tty 714 * tty_mutex is taken to protect tty
701 * ->siglock is taken to protect ->signal/->sighand 715 * ->siglock is taken to protect ->signal/->sighand
702 * tasklist_lock is taken to walk process list for sessions 716 * tasklist_lock is taken to walk process list for sessions
@@ -714,10 +728,10 @@ void disassociate_ctty(int on_exit)
714 tty = get_current_tty(); 728 tty = get_current_tty();
715 if (tty) { 729 if (tty) {
716 tty_pgrp = get_pid(tty->pgrp); 730 tty_pgrp = get_pid(tty->pgrp);
717 lock_kernel(); 731 if (on_exit) {
718 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) 732 if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
719 tty_vhangup(tty); 733 tty_vhangup(tty);
720 unlock_kernel(); 734 }
721 tty_kref_put(tty); 735 tty_kref_put(tty);
722 } else if (on_exit) { 736 } else if (on_exit) {
723 struct pid *old_pgrp; 737 struct pid *old_pgrp;
@@ -774,9 +788,9 @@ void disassociate_ctty(int on_exit)
774void no_tty(void) 788void no_tty(void)
775{ 789{
776 struct task_struct *tsk = current; 790 struct task_struct *tsk = current;
777 lock_kernel(); 791 tty_lock();
778 disassociate_ctty(0); 792 disassociate_ctty(0);
779 unlock_kernel(); 793 tty_unlock();
780 proc_clear_tty(tsk); 794 proc_clear_tty(tsk);
781} 795}
782 796
@@ -879,7 +893,7 @@ static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
879 struct inode *inode; 893 struct inode *inode;
880 struct tty_ldisc *ld; 894 struct tty_ldisc *ld;
881 895
882 tty = (struct tty_struct *)file->private_data; 896 tty = file->private_data;
883 inode = file->f_path.dentry->d_inode; 897 inode = file->f_path.dentry->d_inode;
884 if (tty_paranoia_check(tty, inode, "tty_read")) 898 if (tty_paranoia_check(tty, inode, "tty_read"))
885 return -EIO; 899 return -EIO;
@@ -1013,19 +1027,19 @@ out:
1013 * We don't put it into the syslog queue right now maybe in the future if 1027 * We don't put it into the syslog queue right now maybe in the future if
1014 * really needed. 1028 * really needed.
1015 * 1029 *
1016 * We must still hold the BKL and test the CLOSING flag for the moment. 1030 * We must still hold the BTM and test the CLOSING flag for the moment.
1017 */ 1031 */
1018 1032
1019void tty_write_message(struct tty_struct *tty, char *msg) 1033void tty_write_message(struct tty_struct *tty, char *msg)
1020{ 1034{
1021 if (tty) { 1035 if (tty) {
1022 mutex_lock(&tty->atomic_write_lock); 1036 mutex_lock(&tty->atomic_write_lock);
1023 lock_kernel(); 1037 tty_lock();
1024 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { 1038 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
1025 unlock_kernel(); 1039 tty_unlock();
1026 tty->ops->write(tty, msg, strlen(msg)); 1040 tty->ops->write(tty, msg, strlen(msg));
1027 } else 1041 } else
1028 unlock_kernel(); 1042 tty_unlock();
1029 tty_write_unlock(tty); 1043 tty_write_unlock(tty);
1030 } 1044 }
1031 return; 1045 return;
@@ -1056,7 +1070,7 @@ static ssize_t tty_write(struct file *file, const char __user *buf,
1056 ssize_t ret; 1070 ssize_t ret;
1057 struct tty_ldisc *ld; 1071 struct tty_ldisc *ld;
1058 1072
1059 tty = (struct tty_struct *)file->private_data; 1073 tty = file->private_data;
1060 if (tty_paranoia_check(tty, inode, "tty_write")) 1074 if (tty_paranoia_check(tty, inode, "tty_write"))
1061 return -EIO; 1075 return -EIO;
1062 if (!tty || !tty->ops->write || 1076 if (!tty || !tty->ops->write ||
@@ -1208,18 +1222,14 @@ static int tty_driver_install_tty(struct tty_driver *driver,
1208 int ret; 1222 int ret;
1209 1223
1210 if (driver->ops->install) { 1224 if (driver->ops->install) {
1211 lock_kernel();
1212 ret = driver->ops->install(driver, tty); 1225 ret = driver->ops->install(driver, tty);
1213 unlock_kernel();
1214 return ret; 1226 return ret;
1215 } 1227 }
1216 1228
1217 if (tty_init_termios(tty) == 0) { 1229 if (tty_init_termios(tty) == 0) {
1218 lock_kernel();
1219 tty_driver_kref_get(driver); 1230 tty_driver_kref_get(driver);
1220 tty->count++; 1231 tty->count++;
1221 driver->ttys[idx] = tty; 1232 driver->ttys[idx] = tty;
1222 unlock_kernel();
1223 return 0; 1233 return 0;
1224 } 1234 }
1225 return -ENOMEM; 1235 return -ENOMEM;
@@ -1312,14 +1322,11 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1312 struct tty_struct *tty; 1322 struct tty_struct *tty;
1313 int retval; 1323 int retval;
1314 1324
1315 lock_kernel();
1316 /* Check if pty master is being opened multiple times */ 1325 /* Check if pty master is being opened multiple times */
1317 if (driver->subtype == PTY_TYPE_MASTER && 1326 if (driver->subtype == PTY_TYPE_MASTER &&
1318 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) { 1327 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
1319 unlock_kernel();
1320 return ERR_PTR(-EIO); 1328 return ERR_PTR(-EIO);
1321 } 1329 }
1322 unlock_kernel();
1323 1330
1324 /* 1331 /*
1325 * First time open is complex, especially for PTY devices. 1332 * First time open is complex, especially for PTY devices.
@@ -1363,9 +1370,7 @@ release_mem_out:
1363 if (printk_ratelimit()) 1370 if (printk_ratelimit())
1364 printk(KERN_INFO "tty_init_dev: ldisc open failed, " 1371 printk(KERN_INFO "tty_init_dev: ldisc open failed, "
1365 "clearing slot %d\n", idx); 1372 "clearing slot %d\n", idx);
1366 lock_kernel();
1367 release_tty(tty, idx); 1373 release_tty(tty, idx);
1368 unlock_kernel();
1369 return ERR_PTR(retval); 1374 return ERR_PTR(retval);
1370} 1375}
1371 1376
@@ -1508,14 +1513,14 @@ int tty_release(struct inode *inode, struct file *filp)
1508 int idx; 1513 int idx;
1509 char buf[64]; 1514 char buf[64];
1510 1515
1511 tty = (struct tty_struct *)filp->private_data; 1516 tty = filp->private_data;
1512 if (tty_paranoia_check(tty, inode, "tty_release_dev")) 1517 if (tty_paranoia_check(tty, inode, "tty_release_dev"))
1513 return 0; 1518 return 0;
1514 1519
1515 lock_kernel(); 1520 tty_lock();
1516 check_tty_count(tty, "tty_release_dev"); 1521 check_tty_count(tty, "tty_release_dev");
1517 1522
1518 tty_fasync(-1, filp, 0); 1523 __tty_fasync(-1, filp, 0);
1519 1524
1520 idx = tty->index; 1525 idx = tty->index;
1521 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && 1526 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
@@ -1527,18 +1532,18 @@ int tty_release(struct inode *inode, struct file *filp)
1527 if (idx < 0 || idx >= tty->driver->num) { 1532 if (idx < 0 || idx >= tty->driver->num) {
1528 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to " 1533 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
1529 "free (%s)\n", tty->name); 1534 "free (%s)\n", tty->name);
1530 unlock_kernel(); 1535 tty_unlock();
1531 return 0; 1536 return 0;
1532 } 1537 }
1533 if (!devpts) { 1538 if (!devpts) {
1534 if (tty != tty->driver->ttys[idx]) { 1539 if (tty != tty->driver->ttys[idx]) {
1535 unlock_kernel(); 1540 tty_unlock();
1536 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty " 1541 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
1537 "for (%s)\n", idx, tty->name); 1542 "for (%s)\n", idx, tty->name);
1538 return 0; 1543 return 0;
1539 } 1544 }
1540 if (tty->termios != tty->driver->termios[idx]) { 1545 if (tty->termios != tty->driver->termios[idx]) {
1541 unlock_kernel(); 1546 tty_unlock();
1542 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios " 1547 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
1543 "for (%s)\n", 1548 "for (%s)\n",
1544 idx, tty->name); 1549 idx, tty->name);
@@ -1556,21 +1561,21 @@ int tty_release(struct inode *inode, struct file *filp)
1556 if (tty->driver->other && 1561 if (tty->driver->other &&
1557 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { 1562 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1558 if (o_tty != tty->driver->other->ttys[idx]) { 1563 if (o_tty != tty->driver->other->ttys[idx]) {
1559 unlock_kernel(); 1564 tty_unlock();
1560 printk(KERN_DEBUG "tty_release_dev: other->table[%d] " 1565 printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
1561 "not o_tty for (%s)\n", 1566 "not o_tty for (%s)\n",
1562 idx, tty->name); 1567 idx, tty->name);
1563 return 0 ; 1568 return 0 ;
1564 } 1569 }
1565 if (o_tty->termios != tty->driver->other->termios[idx]) { 1570 if (o_tty->termios != tty->driver->other->termios[idx]) {
1566 unlock_kernel(); 1571 tty_unlock();
1567 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] " 1572 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
1568 "not o_termios for (%s)\n", 1573 "not o_termios for (%s)\n",
1569 idx, tty->name); 1574 idx, tty->name);
1570 return 0; 1575 return 0;
1571 } 1576 }
1572 if (o_tty->link != tty) { 1577 if (o_tty->link != tty) {
1573 unlock_kernel(); 1578 tty_unlock();
1574 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n"); 1579 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
1575 return 0; 1580 return 0;
1576 } 1581 }
@@ -1579,7 +1584,7 @@ int tty_release(struct inode *inode, struct file *filp)
1579 if (tty->ops->close) 1584 if (tty->ops->close)
1580 tty->ops->close(tty, filp); 1585 tty->ops->close(tty, filp);
1581 1586
1582 unlock_kernel(); 1587 tty_unlock();
1583 /* 1588 /*
1584 * Sanity check: if tty->count is going to zero, there shouldn't be 1589 * Sanity check: if tty->count is going to zero, there shouldn't be
1585 * any waiters on tty->read_wait or tty->write_wait. We test the 1590 * any waiters on tty->read_wait or tty->write_wait. We test the
@@ -1602,7 +1607,7 @@ int tty_release(struct inode *inode, struct file *filp)
1602 opens on /dev/tty */ 1607 opens on /dev/tty */
1603 1608
1604 mutex_lock(&tty_mutex); 1609 mutex_lock(&tty_mutex);
1605 lock_kernel(); 1610 tty_lock();
1606 tty_closing = tty->count <= 1; 1611 tty_closing = tty->count <= 1;
1607 o_tty_closing = o_tty && 1612 o_tty_closing = o_tty &&
1608 (o_tty->count <= (pty_master ? 1 : 0)); 1613 (o_tty->count <= (pty_master ? 1 : 0));
@@ -1633,7 +1638,7 @@ int tty_release(struct inode *inode, struct file *filp)
1633 1638
1634 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue " 1639 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
1635 "active!\n", tty_name(tty, buf)); 1640 "active!\n", tty_name(tty, buf));
1636 unlock_kernel(); 1641 tty_unlock();
1637 mutex_unlock(&tty_mutex); 1642 mutex_unlock(&tty_mutex);
1638 schedule(); 1643 schedule();
1639 } 1644 }
@@ -1698,7 +1703,7 @@ int tty_release(struct inode *inode, struct file *filp)
1698 1703
1699 /* check whether both sides are closing ... */ 1704 /* check whether both sides are closing ... */
1700 if (!tty_closing || (o_tty && !o_tty_closing)) { 1705 if (!tty_closing || (o_tty && !o_tty_closing)) {
1701 unlock_kernel(); 1706 tty_unlock();
1702 return 0; 1707 return 0;
1703 } 1708 }
1704 1709
@@ -1718,7 +1723,7 @@ int tty_release(struct inode *inode, struct file *filp)
1718 /* Make this pty number available for reallocation */ 1723 /* Make this pty number available for reallocation */
1719 if (devpts) 1724 if (devpts)
1720 devpts_kill_index(inode, idx); 1725 devpts_kill_index(inode, idx);
1721 unlock_kernel(); 1726 tty_unlock();
1722 return 0; 1727 return 0;
1723} 1728}
1724 1729
@@ -1760,12 +1765,12 @@ retry_open:
1760 retval = 0; 1765 retval = 0;
1761 1766
1762 mutex_lock(&tty_mutex); 1767 mutex_lock(&tty_mutex);
1763 lock_kernel(); 1768 tty_lock();
1764 1769
1765 if (device == MKDEV(TTYAUX_MAJOR, 0)) { 1770 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
1766 tty = get_current_tty(); 1771 tty = get_current_tty();
1767 if (!tty) { 1772 if (!tty) {
1768 unlock_kernel(); 1773 tty_unlock();
1769 mutex_unlock(&tty_mutex); 1774 mutex_unlock(&tty_mutex);
1770 return -ENXIO; 1775 return -ENXIO;
1771 } 1776 }
@@ -1797,14 +1802,14 @@ retry_open:
1797 goto got_driver; 1802 goto got_driver;
1798 } 1803 }
1799 } 1804 }
1800 unlock_kernel(); 1805 tty_unlock();
1801 mutex_unlock(&tty_mutex); 1806 mutex_unlock(&tty_mutex);
1802 return -ENODEV; 1807 return -ENODEV;
1803 } 1808 }
1804 1809
1805 driver = get_tty_driver(device, &index); 1810 driver = get_tty_driver(device, &index);
1806 if (!driver) { 1811 if (!driver) {
1807 unlock_kernel(); 1812 tty_unlock();
1808 mutex_unlock(&tty_mutex); 1813 mutex_unlock(&tty_mutex);
1809 return -ENODEV; 1814 return -ENODEV;
1810 } 1815 }
@@ -1814,7 +1819,7 @@ got_driver:
1814 tty = tty_driver_lookup_tty(driver, inode, index); 1819 tty = tty_driver_lookup_tty(driver, inode, index);
1815 1820
1816 if (IS_ERR(tty)) { 1821 if (IS_ERR(tty)) {
1817 unlock_kernel(); 1822 tty_unlock();
1818 mutex_unlock(&tty_mutex); 1823 mutex_unlock(&tty_mutex);
1819 return PTR_ERR(tty); 1824 return PTR_ERR(tty);
1820 } 1825 }
@@ -1830,7 +1835,7 @@ got_driver:
1830 mutex_unlock(&tty_mutex); 1835 mutex_unlock(&tty_mutex);
1831 tty_driver_kref_put(driver); 1836 tty_driver_kref_put(driver);
1832 if (IS_ERR(tty)) { 1837 if (IS_ERR(tty)) {
1833 unlock_kernel(); 1838 tty_unlock();
1834 return PTR_ERR(tty); 1839 return PTR_ERR(tty);
1835 } 1840 }
1836 1841
@@ -1860,29 +1865,29 @@ got_driver:
1860 printk(KERN_DEBUG "error %d in opening %s...", retval, 1865 printk(KERN_DEBUG "error %d in opening %s...", retval,
1861 tty->name); 1866 tty->name);
1862#endif 1867#endif
1868 tty_unlock(); /* need to call tty_release without BTM */
1863 tty_release(inode, filp); 1869 tty_release(inode, filp);
1864 if (retval != -ERESTARTSYS) { 1870 if (retval != -ERESTARTSYS)
1865 unlock_kernel();
1866 return retval; 1871 return retval;
1867 } 1872
1868 if (signal_pending(current)) { 1873 if (signal_pending(current))
1869 unlock_kernel();
1870 return retval; 1874 return retval;
1871 } 1875
1872 schedule(); 1876 schedule();
1873 /* 1877 /*
1874 * Need to reset f_op in case a hangup happened. 1878 * Need to reset f_op in case a hangup happened.
1875 */ 1879 */
1880 tty_lock();
1876 if (filp->f_op == &hung_up_tty_fops) 1881 if (filp->f_op == &hung_up_tty_fops)
1877 filp->f_op = &tty_fops; 1882 filp->f_op = &tty_fops;
1878 unlock_kernel(); 1883 tty_unlock();
1879 goto retry_open; 1884 goto retry_open;
1880 } 1885 }
1881 unlock_kernel(); 1886 tty_unlock();
1882 1887
1883 1888
1884 mutex_lock(&tty_mutex); 1889 mutex_lock(&tty_mutex);
1885 lock_kernel(); 1890 tty_lock();
1886 spin_lock_irq(&current->sighand->siglock); 1891 spin_lock_irq(&current->sighand->siglock);
1887 if (!noctty && 1892 if (!noctty &&
1888 current->signal->leader && 1893 current->signal->leader &&
@@ -1890,7 +1895,7 @@ got_driver:
1890 tty->session == NULL) 1895 tty->session == NULL)
1891 __proc_set_tty(current, tty); 1896 __proc_set_tty(current, tty);
1892 spin_unlock_irq(&current->sighand->siglock); 1897 spin_unlock_irq(&current->sighand->siglock);
1893 unlock_kernel(); 1898 tty_unlock();
1894 mutex_unlock(&tty_mutex); 1899 mutex_unlock(&tty_mutex);
1895 return 0; 1900 return 0;
1896} 1901}
@@ -1915,7 +1920,7 @@ static unsigned int tty_poll(struct file *filp, poll_table *wait)
1915 struct tty_ldisc *ld; 1920 struct tty_ldisc *ld;
1916 int ret = 0; 1921 int ret = 0;
1917 1922
1918 tty = (struct tty_struct *)filp->private_data; 1923 tty = filp->private_data;
1919 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll")) 1924 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
1920 return 0; 1925 return 0;
1921 1926
@@ -1926,14 +1931,13 @@ static unsigned int tty_poll(struct file *filp, poll_table *wait)
1926 return ret; 1931 return ret;
1927} 1932}
1928 1933
1929static int tty_fasync(int fd, struct file *filp, int on) 1934static int __tty_fasync(int fd, struct file *filp, int on)
1930{ 1935{
1931 struct tty_struct *tty; 1936 struct tty_struct *tty;
1932 unsigned long flags; 1937 unsigned long flags;
1933 int retval = 0; 1938 int retval = 0;
1934 1939
1935 lock_kernel(); 1940 tty = filp->private_data;
1936 tty = (struct tty_struct *)filp->private_data;
1937 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync")) 1941 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
1938 goto out; 1942 goto out;
1939 1943
@@ -1966,7 +1970,15 @@ static int tty_fasync(int fd, struct file *filp, int on)
1966 } 1970 }
1967 retval = 0; 1971 retval = 0;
1968out: 1972out:
1969 unlock_kernel(); 1973 return retval;
1974}
1975
1976static int tty_fasync(int fd, struct file *filp, int on)
1977{
1978 int retval;
1979 tty_lock();
1980 retval = __tty_fasync(fd, filp, on);
1981 tty_unlock();
1970 return retval; 1982 return retval;
1971} 1983}
1972 1984
@@ -2485,7 +2497,7 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2485 struct tty_ldisc *ld; 2497 struct tty_ldisc *ld;
2486 struct inode *inode = file->f_dentry->d_inode; 2498 struct inode *inode = file->f_dentry->d_inode;
2487 2499
2488 tty = (struct tty_struct *)file->private_data; 2500 tty = file->private_data;
2489 if (tty_paranoia_check(tty, inode, "tty_ioctl")) 2501 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2490 return -EINVAL; 2502 return -EINVAL;
2491 2503
diff --git a/drivers/char/tty_ioctl.c b/drivers/char/tty_ioctl.c
index 6bd5f8866c7..0c188997145 100644
--- a/drivers/char/tty_ioctl.c
+++ b/drivers/char/tty_ioctl.c
@@ -517,19 +517,25 @@ static void change_termios(struct tty_struct *tty, struct ktermios *new_termios)
517 517
518 /* See if packet mode change of state. */ 518 /* See if packet mode change of state. */
519 if (tty->link && tty->link->packet) { 519 if (tty->link && tty->link->packet) {
520 int extproc = (old_termios.c_lflag & EXTPROC) |
521 (tty->termios->c_lflag & EXTPROC);
520 int old_flow = ((old_termios.c_iflag & IXON) && 522 int old_flow = ((old_termios.c_iflag & IXON) &&
521 (old_termios.c_cc[VSTOP] == '\023') && 523 (old_termios.c_cc[VSTOP] == '\023') &&
522 (old_termios.c_cc[VSTART] == '\021')); 524 (old_termios.c_cc[VSTART] == '\021'));
523 int new_flow = (I_IXON(tty) && 525 int new_flow = (I_IXON(tty) &&
524 STOP_CHAR(tty) == '\023' && 526 STOP_CHAR(tty) == '\023' &&
525 START_CHAR(tty) == '\021'); 527 START_CHAR(tty) == '\021');
526 if (old_flow != new_flow) { 528 if ((old_flow != new_flow) || extproc) {
527 spin_lock_irqsave(&tty->ctrl_lock, flags); 529 spin_lock_irqsave(&tty->ctrl_lock, flags);
528 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP); 530 if (old_flow != new_flow) {
529 if (new_flow) 531 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
530 tty->ctrl_status |= TIOCPKT_DOSTOP; 532 if (new_flow)
531 else 533 tty->ctrl_status |= TIOCPKT_DOSTOP;
532 tty->ctrl_status |= TIOCPKT_NOSTOP; 534 else
535 tty->ctrl_status |= TIOCPKT_NOSTOP;
536 }
537 if (extproc)
538 tty->ctrl_status |= TIOCPKT_IOCTL;
533 spin_unlock_irqrestore(&tty->ctrl_lock, flags); 539 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
534 wake_up_interruptible(&tty->link->read_wait); 540 wake_up_interruptible(&tty->link->read_wait);
535 } 541 }
diff --git a/drivers/char/tty_ldisc.c b/drivers/char/tty_ldisc.c
index 500e740ec5e..412f9775d19 100644
--- a/drivers/char/tty_ldisc.c
+++ b/drivers/char/tty_ldisc.c
@@ -440,6 +440,8 @@ static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
440 * 440 *
441 * A helper opening method. Also a convenient debugging and check 441 * A helper opening method. Also a convenient debugging and check
442 * point. 442 * point.
443 *
444 * Locking: always called with BTM already held.
443 */ 445 */
444 446
445static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) 447static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
@@ -447,10 +449,9 @@ static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
447 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); 449 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
448 if (ld->ops->open) { 450 if (ld->ops->open) {
449 int ret; 451 int ret;
450 /* BKL here locks verus a hangup event */ 452 /* BTM here locks versus a hangup event */
451 lock_kernel(); 453 WARN_ON(!tty_locked());
452 ret = ld->ops->open(tty); 454 ret = ld->ops->open(tty);
453 unlock_kernel();
454 return ret; 455 return ret;
455 } 456 }
456 return 0; 457 return 0;
@@ -553,7 +554,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
553 if (IS_ERR(new_ldisc)) 554 if (IS_ERR(new_ldisc))
554 return PTR_ERR(new_ldisc); 555 return PTR_ERR(new_ldisc);
555 556
556 lock_kernel(); 557 tty_lock();
557 /* 558 /*
558 * We need to look at the tty locking here for pty/tty pairs 559 * We need to look at the tty locking here for pty/tty pairs
559 * when both sides try to change in parallel. 560 * when both sides try to change in parallel.
@@ -567,12 +568,12 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
567 */ 568 */
568 569
569 if (tty->ldisc->ops->num == ldisc) { 570 if (tty->ldisc->ops->num == ldisc) {
570 unlock_kernel(); 571 tty_unlock();
571 tty_ldisc_put(new_ldisc); 572 tty_ldisc_put(new_ldisc);
572 return 0; 573 return 0;
573 } 574 }
574 575
575 unlock_kernel(); 576 tty_unlock();
576 /* 577 /*
577 * Problem: What do we do if this blocks ? 578 * Problem: What do we do if this blocks ?
578 * We could deadlock here 579 * We could deadlock here
@@ -580,6 +581,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
580 581
581 tty_wait_until_sent(tty, 0); 582 tty_wait_until_sent(tty, 0);
582 583
584 tty_lock();
583 mutex_lock(&tty->ldisc_mutex); 585 mutex_lock(&tty->ldisc_mutex);
584 586
585 /* 587 /*
@@ -589,13 +591,13 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
589 591
590 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { 592 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
591 mutex_unlock(&tty->ldisc_mutex); 593 mutex_unlock(&tty->ldisc_mutex);
594 tty_unlock();
592 wait_event(tty_ldisc_wait, 595 wait_event(tty_ldisc_wait,
593 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0); 596 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
597 tty_lock();
594 mutex_lock(&tty->ldisc_mutex); 598 mutex_lock(&tty->ldisc_mutex);
595 } 599 }
596 600
597 lock_kernel();
598
599 set_bit(TTY_LDISC_CHANGING, &tty->flags); 601 set_bit(TTY_LDISC_CHANGING, &tty->flags);
600 602
601 /* 603 /*
@@ -607,7 +609,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
607 609
608 o_ldisc = tty->ldisc; 610 o_ldisc = tty->ldisc;
609 611
610 unlock_kernel(); 612 tty_unlock();
611 /* 613 /*
612 * Make sure we don't change while someone holds a 614 * Make sure we don't change while someone holds a
613 * reference to the line discipline. The TTY_LDISC bit 615 * reference to the line discipline. The TTY_LDISC bit
@@ -632,15 +634,15 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
632 634
633 flush_scheduled_work(); 635 flush_scheduled_work();
634 636
637 tty_lock();
635 mutex_lock(&tty->ldisc_mutex); 638 mutex_lock(&tty->ldisc_mutex);
636 lock_kernel();
637 if (test_bit(TTY_HUPPED, &tty->flags)) { 639 if (test_bit(TTY_HUPPED, &tty->flags)) {
638 /* We were raced by the hangup method. It will have stomped 640 /* We were raced by the hangup method. It will have stomped
639 the ldisc data and closed the ldisc down */ 641 the ldisc data and closed the ldisc down */
640 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 642 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
641 mutex_unlock(&tty->ldisc_mutex); 643 mutex_unlock(&tty->ldisc_mutex);
642 tty_ldisc_put(new_ldisc); 644 tty_ldisc_put(new_ldisc);
643 unlock_kernel(); 645 tty_unlock();
644 return -EIO; 646 return -EIO;
645 } 647 }
646 648
@@ -682,7 +684,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
682 if (o_work) 684 if (o_work)
683 schedule_delayed_work(&o_tty->buf.work, 1); 685 schedule_delayed_work(&o_tty->buf.work, 1);
684 mutex_unlock(&tty->ldisc_mutex); 686 mutex_unlock(&tty->ldisc_mutex);
685 unlock_kernel(); 687 tty_unlock();
686 return retval; 688 return retval;
687} 689}
688 690
@@ -780,7 +782,20 @@ void tty_ldisc_hangup(struct tty_struct *tty)
780 * Avoid racing set_ldisc or tty_ldisc_release 782 * Avoid racing set_ldisc or tty_ldisc_release
781 */ 783 */
782 mutex_lock(&tty->ldisc_mutex); 784 mutex_lock(&tty->ldisc_mutex);
783 tty_ldisc_halt(tty); 785
786 /*
787 * this is like tty_ldisc_halt, but we need to give up
788 * the BTM before calling cancel_delayed_work_sync,
789 * which may need to wait for another function taking the BTM
790 */
791 clear_bit(TTY_LDISC, &tty->flags);
792 tty_unlock();
793 cancel_delayed_work_sync(&tty->buf.work);
794 mutex_unlock(&tty->ldisc_mutex);
795
796 tty_lock();
797 mutex_lock(&tty->ldisc_mutex);
798
784 /* At this point we have a closed ldisc and we want to 799 /* At this point we have a closed ldisc and we want to
785 reopen it. We could defer this to the next open but 800 reopen it. We could defer this to the next open but
786 it means auditing a lot of other paths so this is 801 it means auditing a lot of other paths so this is
@@ -851,8 +866,10 @@ void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
851 * race with the set_ldisc code path. 866 * race with the set_ldisc code path.
852 */ 867 */
853 868
869 tty_unlock();
854 tty_ldisc_halt(tty); 870 tty_ldisc_halt(tty);
855 flush_scheduled_work(); 871 flush_scheduled_work();
872 tty_lock();
856 873
857 mutex_lock(&tty->ldisc_mutex); 874 mutex_lock(&tty->ldisc_mutex);
858 /* 875 /*
diff --git a/drivers/char/tty_mutex.c b/drivers/char/tty_mutex.c
new file mode 100644
index 00000000000..133697540c7
--- /dev/null
+++ b/drivers/char/tty_mutex.c
@@ -0,0 +1,47 @@
1/*
2 * drivers/char/tty_lock.c
3 */
4#include <linux/tty.h>
5#include <linux/module.h>
6#include <linux/kallsyms.h>
7#include <linux/semaphore.h>
8#include <linux/sched.h>
9
10/*
11 * The 'big tty mutex'
12 *
13 * This mutex is taken and released by tty_lock() and tty_unlock(),
14 * replacing the older big kernel lock.
15 * It can no longer be taken recursively, and does not get
16 * released implicitly while sleeping.
17 *
18 * Don't use in new code.
19 */
20static DEFINE_MUTEX(big_tty_mutex);
21struct task_struct *__big_tty_mutex_owner;
22EXPORT_SYMBOL_GPL(__big_tty_mutex_owner);
23
24/*
25 * Getting the big tty mutex.
26 */
27void __lockfunc tty_lock(void)
28{
29 struct task_struct *task = current;
30
31 WARN_ON(__big_tty_mutex_owner == task);
32
33 mutex_lock(&big_tty_mutex);
34 __big_tty_mutex_owner = task;
35}
36EXPORT_SYMBOL(tty_lock);
37
38void __lockfunc tty_unlock(void)
39{
40 struct task_struct *task = current;
41
42 WARN_ON(__big_tty_mutex_owner != task);
43 __big_tty_mutex_owner = NULL;
44
45 mutex_unlock(&big_tty_mutex);
46}
47EXPORT_SYMBOL(tty_unlock);
diff --git a/drivers/char/tty_port.c b/drivers/char/tty_port.c
index a3bd1d0b66c..33d37d230f8 100644
--- a/drivers/char/tty_port.c
+++ b/drivers/char/tty_port.c
@@ -231,7 +231,7 @@ int tty_port_block_til_ready(struct tty_port *port,
231 231
232 /* block if port is in the process of being closed */ 232 /* block if port is in the process of being closed */
233 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) { 233 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
234 wait_event_interruptible(port->close_wait, 234 wait_event_interruptible_tty(port->close_wait,
235 !(port->flags & ASYNC_CLOSING)); 235 !(port->flags & ASYNC_CLOSING));
236 if (port->flags & ASYNC_HUP_NOTIFY) 236 if (port->flags & ASYNC_HUP_NOTIFY)
237 return -EAGAIN; 237 return -EAGAIN;
@@ -294,7 +294,9 @@ int tty_port_block_til_ready(struct tty_port *port,
294 retval = -ERESTARTSYS; 294 retval = -ERESTARTSYS;
295 break; 295 break;
296 } 296 }
297 tty_unlock();
297 schedule(); 298 schedule();
299 tty_lock();
298 } 300 }
299 finish_wait(&port->open_wait, &wait); 301 finish_wait(&port->open_wait, &wait);
300 302
diff --git a/drivers/char/vc_screen.c b/drivers/char/vc_screen.c
index c1791a63d99..bcce46c96b8 100644
--- a/drivers/char/vc_screen.c
+++ b/drivers/char/vc_screen.c
@@ -463,10 +463,10 @@ vcs_open(struct inode *inode, struct file *filp)
463 unsigned int currcons = iminor(inode) & 127; 463 unsigned int currcons = iminor(inode) & 127;
464 int ret = 0; 464 int ret = 0;
465 465
466 lock_kernel(); 466 tty_lock();
467 if(currcons && !vc_cons_allocated(currcons-1)) 467 if(currcons && !vc_cons_allocated(currcons-1))
468 ret = -ENXIO; 468 ret = -ENXIO;
469 unlock_kernel(); 469 tty_unlock();
470 return ret; 470 return ret;
471} 471}
472 472
diff --git a/drivers/char/vt.c b/drivers/char/vt.c
index 44f03ddd887..c734f9b1263 100644
--- a/drivers/char/vt.c
+++ b/drivers/char/vt.c
@@ -105,6 +105,7 @@
105#include <asm/system.h> 105#include <asm/system.h>
106#include <linux/uaccess.h> 106#include <linux/uaccess.h>
107#include <linux/kdb.h> 107#include <linux/kdb.h>
108#include <linux/ctype.h>
108 109
109#define MAX_NR_CON_DRIVER 16 110#define MAX_NR_CON_DRIVER 16
110 111
@@ -286,8 +287,12 @@ static inline unsigned short *screenpos(struct vc_data *vc, int offset, int view
286 return p; 287 return p;
287} 288}
288 289
290/* Called from the keyboard irq path.. */
289static inline void scrolldelta(int lines) 291static inline void scrolldelta(int lines)
290{ 292{
293 /* FIXME */
294 /* scrolldelta needs some kind of consistency lock, but the BKL was
295 and still is not protecting versus the scheduled back end */
291 scrollback_delta += lines; 296 scrollback_delta += lines;
292 schedule_console_callback(); 297 schedule_console_callback();
293} 298}
@@ -704,7 +709,10 @@ void redraw_screen(struct vc_data *vc, int is_switch)
704 update_attr(vc); 709 update_attr(vc);
705 clear_buffer_attributes(vc); 710 clear_buffer_attributes(vc);
706 } 711 }
707 if (update && vc->vc_mode != KD_GRAPHICS) 712
713 /* Forcibly update if we're panicing */
714 if ((update && vc->vc_mode != KD_GRAPHICS) ||
715 vt_force_oops_output(vc))
708 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); 716 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
709 } 717 }
710 set_cursor(vc); 718 set_cursor(vc);
@@ -742,6 +750,7 @@ static void visual_init(struct vc_data *vc, int num, int init)
742 vc->vc_hi_font_mask = 0; 750 vc->vc_hi_font_mask = 0;
743 vc->vc_complement_mask = 0; 751 vc->vc_complement_mask = 0;
744 vc->vc_can_do_color = 0; 752 vc->vc_can_do_color = 0;
753 vc->vc_panic_force_write = false;
745 vc->vc_sw->con_init(vc, init); 754 vc->vc_sw->con_init(vc, init);
746 if (!vc->vc_complement_mask) 755 if (!vc->vc_complement_mask)
747 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800; 756 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
@@ -774,6 +783,7 @@ int vc_allocate(unsigned int currcons) /* return 0 on success */
774 if (!vc) 783 if (!vc)
775 return -ENOMEM; 784 return -ENOMEM;
776 vc_cons[currcons].d = vc; 785 vc_cons[currcons].d = vc;
786 tty_port_init(&vc->port);
777 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); 787 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
778 visual_init(vc, currcons, 1); 788 visual_init(vc, currcons, 1);
779 if (!*vc->vc_uni_pagedir_loc) 789 if (!*vc->vc_uni_pagedir_loc)
@@ -963,12 +973,12 @@ static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
963 * Resize a virtual console as seen from the console end of things. We 973 * Resize a virtual console as seen from the console end of things. We
964 * use the common vc_do_resize methods to update the structures. The 974 * use the common vc_do_resize methods to update the structures. The
965 * caller must hold the console sem to protect console internals and 975 * caller must hold the console sem to protect console internals and
966 * vc->vc_tty 976 * vc->port.tty
967 */ 977 */
968 978
969int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows) 979int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
970{ 980{
971 return vc_do_resize(vc->vc_tty, vc, cols, rows); 981 return vc_do_resize(vc->port.tty, vc, cols, rows);
972} 982}
973 983
974/** 984/**
@@ -1796,8 +1806,8 @@ static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1796 vc->vc_state = ESnormal; 1806 vc->vc_state = ESnormal;
1797 return; 1807 return;
1798 case ESpalette: 1808 case ESpalette:
1799 if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) { 1809 if (isxdigit(c)) {
1800 vc->vc_par[vc->vc_npar++] = (c > '9' ? (c & 0xDF) - 'A' + 10 : c - '0'); 1810 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
1801 if (vc->vc_npar == 7) { 1811 if (vc->vc_npar == 7) {
1802 int i = vc->vc_par[0] * 3, j = 1; 1812 int i = vc->vc_par[0] * 3, j = 1;
1803 vc->vc_palette[i] = 16 * vc->vc_par[j++]; 1813 vc->vc_palette[i] = 16 * vc->vc_par[j++];
@@ -2505,7 +2515,7 @@ static void vt_console_print(struct console *co, const char *b, unsigned count)
2505 goto quit; 2515 goto quit;
2506 } 2516 }
2507 2517
2508 if (vc->vc_mode != KD_TEXT) 2518 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
2509 goto quit; 2519 goto quit;
2510 2520
2511 /* undraw cursor first */ 2521 /* undraw cursor first */
@@ -2611,8 +2621,6 @@ int tioclinux(struct tty_struct *tty, unsigned long arg)
2611 return -EFAULT; 2621 return -EFAULT;
2612 ret = 0; 2622 ret = 0;
2613 2623
2614 lock_kernel();
2615
2616 switch (type) 2624 switch (type)
2617 { 2625 {
2618 case TIOCL_SETSEL: 2626 case TIOCL_SETSEL:
@@ -2687,7 +2695,6 @@ int tioclinux(struct tty_struct *tty, unsigned long arg)
2687 ret = -EINVAL; 2695 ret = -EINVAL;
2688 break; 2696 break;
2689 } 2697 }
2690 unlock_kernel();
2691 return ret; 2698 return ret;
2692} 2699}
2693 2700
@@ -2800,12 +2807,12 @@ static int con_open(struct tty_struct *tty, struct file *filp)
2800 struct vc_data *vc = vc_cons[currcons].d; 2807 struct vc_data *vc = vc_cons[currcons].d;
2801 2808
2802 /* Still being freed */ 2809 /* Still being freed */
2803 if (vc->vc_tty) { 2810 if (vc->port.tty) {
2804 release_console_sem(); 2811 release_console_sem();
2805 return -ERESTARTSYS; 2812 return -ERESTARTSYS;
2806 } 2813 }
2807 tty->driver_data = vc; 2814 tty->driver_data = vc;
2808 vc->vc_tty = tty; 2815 vc->port.tty = tty;
2809 2816
2810 if (!tty->winsize.ws_row && !tty->winsize.ws_col) { 2817 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2811 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows; 2818 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
@@ -2833,7 +2840,7 @@ static void con_shutdown(struct tty_struct *tty)
2833 struct vc_data *vc = tty->driver_data; 2840 struct vc_data *vc = tty->driver_data;
2834 BUG_ON(vc == NULL); 2841 BUG_ON(vc == NULL);
2835 acquire_console_sem(); 2842 acquire_console_sem();
2836 vc->vc_tty = NULL; 2843 vc->port.tty = NULL;
2837 release_console_sem(); 2844 release_console_sem();
2838 tty_shutdown(tty); 2845 tty_shutdown(tty);
2839} 2846}
@@ -2915,6 +2922,7 @@ static int __init con_init(void)
2915 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) { 2922 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2916 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT); 2923 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
2917 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); 2924 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
2925 tty_port_init(&vc->port);
2918 visual_init(vc, currcons, 1); 2926 visual_init(vc, currcons, 1);
2919 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT); 2927 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
2920 vc_init(vc, vc->vc_rows, vc->vc_cols, 2928 vc_init(vc, vc->vc_rows, vc->vc_cols,
@@ -3783,7 +3791,8 @@ void do_unblank_screen(int leaving_gfx)
3783 return; 3791 return;
3784 } 3792 }
3785 vc = vc_cons[fg_console].d; 3793 vc = vc_cons[fg_console].d;
3786 if (vc->vc_mode != KD_TEXT) 3794 /* Try to unblank in oops case too */
3795 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
3787 return; /* but leave console_blanked != 0 */ 3796 return; /* but leave console_blanked != 0 */
3788 3797
3789 if (blankinterval) { 3798 if (blankinterval) {
@@ -3792,7 +3801,7 @@ void do_unblank_screen(int leaving_gfx)
3792 } 3801 }
3793 3802
3794 console_blanked = 0; 3803 console_blanked = 0;
3795 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx)) 3804 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
3796 /* Low-level driver cannot restore -> do it ourselves */ 3805 /* Low-level driver cannot restore -> do it ourselves */
3797 update_screen(vc); 3806 update_screen(vc);
3798 if (console_blank_hook) 3807 if (console_blank_hook)
diff --git a/drivers/char/vt_ioctl.c b/drivers/char/vt_ioctl.c
index cb19dbc5213..2bbeaaea46e 100644
--- a/drivers/char/vt_ioctl.c
+++ b/drivers/char/vt_ioctl.c
@@ -133,7 +133,7 @@ static void vt_event_wait(struct vt_event_wait *vw)
133 list_add(&vw->list, &vt_events); 133 list_add(&vw->list, &vt_events);
134 spin_unlock_irqrestore(&vt_event_lock, flags); 134 spin_unlock_irqrestore(&vt_event_lock, flags);
135 /* Wait for it to pass */ 135 /* Wait for it to pass */
136 wait_event_interruptible(vt_event_waitqueue, vw->done); 136 wait_event_interruptible_tty(vt_event_waitqueue, vw->done);
137 /* Dequeue it */ 137 /* Dequeue it */
138 spin_lock_irqsave(&vt_event_lock, flags); 138 spin_lock_irqsave(&vt_event_lock, flags);
139 list_del(&vw->list); 139 list_del(&vw->list);
@@ -509,7 +509,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
509 509
510 console = vc->vc_num; 510 console = vc->vc_num;
511 511
512 lock_kernel(); 512 tty_lock();
513 513
514 if (!vc_cons_allocated(console)) { /* impossible? */ 514 if (!vc_cons_allocated(console)) { /* impossible? */
515 ret = -ENOIOCTLCMD; 515 ret = -ENOIOCTLCMD;
@@ -1336,7 +1336,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
1336 ret = -ENOIOCTLCMD; 1336 ret = -ENOIOCTLCMD;
1337 } 1337 }
1338out: 1338out:
1339 unlock_kernel(); 1339 tty_unlock();
1340 return ret; 1340 return ret;
1341eperm: 1341eperm:
1342 ret = -EPERM; 1342 ret = -EPERM;
@@ -1369,7 +1369,7 @@ void vc_SAK(struct work_struct *work)
1369 acquire_console_sem(); 1369 acquire_console_sem();
1370 vc = vc_con->d; 1370 vc = vc_con->d;
1371 if (vc) { 1371 if (vc) {
1372 tty = vc->vc_tty; 1372 tty = vc->port.tty;
1373 /* 1373 /*
1374 * SAK should also work in all raw modes and reset 1374 * SAK should also work in all raw modes and reset
1375 * them properly. 1375 * them properly.
@@ -1503,7 +1503,7 @@ long vt_compat_ioctl(struct tty_struct *tty, struct file * file,
1503 1503
1504 console = vc->vc_num; 1504 console = vc->vc_num;
1505 1505
1506 lock_kernel(); 1506 tty_lock();
1507 1507
1508 if (!vc_cons_allocated(console)) { /* impossible? */ 1508 if (!vc_cons_allocated(console)) { /* impossible? */
1509 ret = -ENOIOCTLCMD; 1509 ret = -ENOIOCTLCMD;
@@ -1571,11 +1571,11 @@ long vt_compat_ioctl(struct tty_struct *tty, struct file * file,
1571 goto fallback; 1571 goto fallback;
1572 } 1572 }
1573out: 1573out:
1574 unlock_kernel(); 1574 tty_unlock();
1575 return ret; 1575 return ret;
1576 1576
1577fallback: 1577fallback:
1578 unlock_kernel(); 1578 tty_unlock();
1579 return vt_ioctl(tty, file, cmd, arg); 1579 return vt_ioctl(tty, file, cmd, arg);
1580} 1580}
1581 1581
@@ -1761,10 +1761,13 @@ int vt_move_to_console(unsigned int vt, int alloc)
1761 return -EIO; 1761 return -EIO;
1762 } 1762 }
1763 release_console_sem(); 1763 release_console_sem();
1764 tty_lock();
1764 if (vt_waitactive(vt + 1)) { 1765 if (vt_waitactive(vt + 1)) {
1765 pr_debug("Suspend: Can't switch VCs."); 1766 pr_debug("Suspend: Can't switch VCs.");
1767 tty_unlock();
1766 return -EINTR; 1768 return -EINTR;
1767 } 1769 }
1770 tty_unlock();
1768 return prev; 1771 return prev;
1769} 1772}
1770 1773
diff --git a/drivers/gpu/drm/i915/intel_fb.c b/drivers/gpu/drm/i915/intel_fb.c
index 54acd8b534d..a79525f434a 100644
--- a/drivers/gpu/drm/i915/intel_fb.c
+++ b/drivers/gpu/drm/i915/intel_fb.c
@@ -130,7 +130,7 @@ static int intelfb_create(struct intel_fbdev *ifbdev,
130 130
131 strcpy(info->fix.id, "inteldrmfb"); 131 strcpy(info->fix.id, "inteldrmfb");
132 132
133 info->flags = FBINFO_DEFAULT; 133 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
134 info->fbops = &intelfb_ops; 134 info->fbops = &intelfb_ops;
135 135
136 /* setup aperture base/size for vesafb takeover */ 136 /* setup aperture base/size for vesafb takeover */
@@ -148,8 +148,6 @@ static int intelfb_create(struct intel_fbdev *ifbdev,
148 info->fix.smem_start = dev->mode_config.fb_base + obj_priv->gtt_offset; 148 info->fix.smem_start = dev->mode_config.fb_base + obj_priv->gtt_offset;
149 info->fix.smem_len = size; 149 info->fix.smem_len = size;
150 150
151 info->flags = FBINFO_DEFAULT;
152
153 info->screen_base = ioremap_wc(dev->agp->base + obj_priv->gtt_offset, 151 info->screen_base = ioremap_wc(dev->agp->base + obj_priv->gtt_offset,
154 size); 152 size);
155 if (!info->screen_base) { 153 if (!info->screen_base) {
diff --git a/drivers/gpu/drm/nouveau/nouveau_fbcon.c b/drivers/gpu/drm/nouveau/nouveau_fbcon.c
index 2fb2444d232..099f637264a 100644
--- a/drivers/gpu/drm/nouveau/nouveau_fbcon.c
+++ b/drivers/gpu/drm/nouveau/nouveau_fbcon.c
@@ -250,6 +250,7 @@ nouveau_fbcon_create(struct nouveau_fbdev *nfbdev,
250 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | 250 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
251 FBINFO_HWACCEL_FILLRECT | 251 FBINFO_HWACCEL_FILLRECT |
252 FBINFO_HWACCEL_IMAGEBLIT; 252 FBINFO_HWACCEL_IMAGEBLIT;
253 info->flags |= FBINFO_CAN_FORCE_OUTPUT;
253 info->fbops = &nouveau_fbcon_ops; 254 info->fbops = &nouveau_fbcon_ops;
254 info->fix.smem_start = dev->mode_config.fb_base + nvbo->bo.offset - 255 info->fix.smem_start = dev->mode_config.fb_base + nvbo->bo.offset -
255 dev_priv->vm_vram_base; 256 dev_priv->vm_vram_base;
diff --git a/drivers/gpu/drm/radeon/radeon_fb.c b/drivers/gpu/drm/radeon/radeon_fb.c
index dc1634bb0c1..dbf86962bdd 100644
--- a/drivers/gpu/drm/radeon/radeon_fb.c
+++ b/drivers/gpu/drm/radeon/radeon_fb.c
@@ -224,7 +224,7 @@ static int radeonfb_create(struct radeon_fbdev *rfbdev,
224 224
225 drm_fb_helper_fill_fix(info, fb->pitch, fb->depth); 225 drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
226 226
227 info->flags = FBINFO_DEFAULT; 227 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
228 info->fbops = &radeonfb_ops; 228 info->fbops = &radeonfb_ops;
229 229
230 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start; 230 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
diff --git a/drivers/hid/hid-wacom.c b/drivers/hid/hid-wacom.c
index 807dcd1555a..724f46ed612 100644
--- a/drivers/hid/hid-wacom.c
+++ b/drivers/hid/hid-wacom.c
@@ -230,7 +230,7 @@ static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
230 input_report_key(input, BTN_RIGHT, 0); 230 input_report_key(input, BTN_RIGHT, 0);
231 input_report_key(input, BTN_MIDDLE, 0); 231 input_report_key(input, BTN_MIDDLE, 0);
232 input_report_abs(input, ABS_DISTANCE, 232 input_report_abs(input, ABS_DISTANCE,
233 input->absmax[ABS_DISTANCE]); 233 input_abs_get_max(input, ABS_DISTANCE));
234 } else { 234 } else {
235 input_report_key(input, BTN_TOUCH, 0); 235 input_report_key(input, BTN_TOUCH, 0);
236 input_report_key(input, BTN_STYLUS, 0); 236 input_report_key(input, BTN_STYLUS, 0);
@@ -383,38 +383,37 @@ move_on:
383 383
384 /* Basics */ 384 /* Basics */
385 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL); 385 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
386 input->absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) | 386
387 BIT(ABS_PRESSURE) | BIT(ABS_DISTANCE); 387 __set_bit(REL_WHEEL, input->relbit);
388 input->relbit[0] |= BIT(REL_WHEEL); 388
389 set_bit(BTN_TOOL_PEN, input->keybit); 389 __set_bit(BTN_TOOL_PEN, input->keybit);
390 set_bit(BTN_TOUCH, input->keybit); 390 __set_bit(BTN_TOUCH, input->keybit);
391 set_bit(BTN_STYLUS, input->keybit); 391 __set_bit(BTN_STYLUS, input->keybit);
392 set_bit(BTN_STYLUS2, input->keybit); 392 __set_bit(BTN_STYLUS2, input->keybit);
393 set_bit(BTN_LEFT, input->keybit); 393 __set_bit(BTN_LEFT, input->keybit);
394 set_bit(BTN_RIGHT, input->keybit); 394 __set_bit(BTN_RIGHT, input->keybit);
395 set_bit(BTN_MIDDLE, input->keybit); 395 __set_bit(BTN_MIDDLE, input->keybit);
396 396
397 /* Pad */ 397 /* Pad */
398 input->evbit[0] |= BIT(EV_MSC); 398 input->evbit[0] |= BIT(EV_MSC);
399 input->mscbit[0] |= BIT(MSC_SERIAL);
400 set_bit(BTN_0, input->keybit);
401 set_bit(BTN_1, input->keybit);
402 set_bit(BTN_TOOL_FINGER, input->keybit);
403 399
404 /* Distance, rubber and mouse */ 400 __set_bit(MSC_SERIAL, input->mscbit);
405 input->absbit[0] |= BIT(ABS_DISTANCE);
406 set_bit(BTN_TOOL_RUBBER, input->keybit);
407 set_bit(BTN_TOOL_MOUSE, input->keybit);
408 401
409 input->absmax[ABS_PRESSURE] = 511; 402 __set_bit(BTN_0, input->keybit);
410 input->absmax[ABS_DISTANCE] = 32; 403 __set_bit(BTN_1, input->keybit);
404 __set_bit(BTN_TOOL_FINGER, input->keybit);
411 405
412 input->absmax[ABS_X] = 16704; 406 /* Distance, rubber and mouse */
413 input->absmax[ABS_Y] = 12064; 407 __set_bit(BTN_TOOL_RUBBER, input->keybit);
414 input->absfuzz[ABS_X] = 4; 408 __set_bit(BTN_TOOL_MOUSE, input->keybit);
415 input->absfuzz[ABS_Y] = 4; 409
410 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
411 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
412 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
413 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
416 414
417 return 0; 415 return 0;
416
418err_free: 417err_free:
419 kfree(wdata); 418 kfree(wdata);
420 return ret; 419 return ret;
diff --git a/drivers/ide/ide-atapi.c b/drivers/ide/ide-atapi.c
index f9daffd7d0e..e88a2cf1771 100644
--- a/drivers/ide/ide-atapi.c
+++ b/drivers/ide/ide-atapi.c
@@ -190,7 +190,7 @@ void ide_prep_sense(ide_drive_t *drive, struct request *rq)
190 190
191 BUG_ON(sense_len > sizeof(*sense)); 191 BUG_ON(sense_len > sizeof(*sense));
192 192
193 if (blk_sense_request(rq) || drive->sense_rq_armed) 193 if (rq->cmd_type == REQ_TYPE_SENSE || drive->sense_rq_armed)
194 return; 194 return;
195 195
196 memset(sense, 0, sizeof(*sense)); 196 memset(sense, 0, sizeof(*sense));
@@ -307,13 +307,16 @@ EXPORT_SYMBOL_GPL(ide_cd_expiry);
307 307
308int ide_cd_get_xferlen(struct request *rq) 308int ide_cd_get_xferlen(struct request *rq)
309{ 309{
310 if (blk_fs_request(rq)) 310 switch (rq->cmd_type) {
311 case REQ_TYPE_FS:
311 return 32768; 312 return 32768;
312 else if (blk_sense_request(rq) || blk_pc_request(rq) || 313 case REQ_TYPE_SENSE:
313 rq->cmd_type == REQ_TYPE_ATA_PC) 314 case REQ_TYPE_BLOCK_PC:
315 case REQ_TYPE_ATA_PC:
314 return blk_rq_bytes(rq); 316 return blk_rq_bytes(rq);
315 else 317 default:
316 return 0; 318 return 0;
319 }
317} 320}
318EXPORT_SYMBOL_GPL(ide_cd_get_xferlen); 321EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
319 322
@@ -474,12 +477,12 @@ static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
474 if (uptodate == 0) 477 if (uptodate == 0)
475 drive->failed_pc = NULL; 478 drive->failed_pc = NULL;
476 479
477 if (blk_special_request(rq)) { 480 if (rq->cmd_type == REQ_TYPE_SPECIAL) {
478 rq->errors = 0; 481 rq->errors = 0;
479 error = 0; 482 error = 0;
480 } else { 483 } else {
481 484
482 if (blk_fs_request(rq) == 0 && uptodate <= 0) { 485 if (rq->cmd_type != REQ_TYPE_FS && uptodate <= 0) {
483 if (rq->errors == 0) 486 if (rq->errors == 0)
484 rq->errors = -EIO; 487 rq->errors = -EIO;
485 } 488 }
diff --git a/drivers/ide/ide-cd.c b/drivers/ide/ide-cd.c
index 2de76cc08f6..31fc76960a8 100644
--- a/drivers/ide/ide-cd.c
+++ b/drivers/ide/ide-cd.c
@@ -31,6 +31,7 @@
31#include <linux/delay.h> 31#include <linux/delay.h>
32#include <linux/timer.h> 32#include <linux/timer.h>
33#include <linux/seq_file.h> 33#include <linux/seq_file.h>
34#include <linux/smp_lock.h>
34#include <linux/slab.h> 35#include <linux/slab.h>
35#include <linux/interrupt.h> 36#include <linux/interrupt.h>
36#include <linux/errno.h> 37#include <linux/errno.h>
@@ -176,7 +177,7 @@ static void cdrom_analyze_sense_data(ide_drive_t *drive,
176 if (!sense->valid) 177 if (!sense->valid)
177 break; 178 break;
178 if (failed_command == NULL || 179 if (failed_command == NULL ||
179 !blk_fs_request(failed_command)) 180 failed_command->cmd_type != REQ_TYPE_FS)
180 break; 181 break;
181 sector = (sense->information[0] << 24) | 182 sector = (sense->information[0] << 24) |
182 (sense->information[1] << 16) | 183 (sense->information[1] << 16) |
@@ -292,7 +293,7 @@ static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
292 "stat 0x%x", 293 "stat 0x%x",
293 rq->cmd[0], rq->cmd_type, err, stat); 294 rq->cmd[0], rq->cmd_type, err, stat);
294 295
295 if (blk_sense_request(rq)) { 296 if (rq->cmd_type == REQ_TYPE_SENSE) {
296 /* 297 /*
297 * We got an error trying to get sense info from the drive 298 * We got an error trying to get sense info from the drive
298 * (probably while trying to recover from a former error). 299 * (probably while trying to recover from a former error).
@@ -303,7 +304,7 @@ static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
303 } 304 }
304 305
305 /* if we have an error, pass CHECK_CONDITION as the SCSI status byte */ 306 /* if we have an error, pass CHECK_CONDITION as the SCSI status byte */
306 if (blk_pc_request(rq) && !rq->errors) 307 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !rq->errors)
307 rq->errors = SAM_STAT_CHECK_CONDITION; 308 rq->errors = SAM_STAT_CHECK_CONDITION;
308 309
309 if (blk_noretry_request(rq)) 310 if (blk_noretry_request(rq))
@@ -311,13 +312,14 @@ static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
311 312
312 switch (sense_key) { 313 switch (sense_key) {
313 case NOT_READY: 314 case NOT_READY:
314 if (blk_fs_request(rq) && rq_data_dir(rq) == WRITE) { 315 if (rq->cmd_type == REQ_TYPE_FS && rq_data_dir(rq) == WRITE) {
315 if (ide_cd_breathe(drive, rq)) 316 if (ide_cd_breathe(drive, rq))
316 return 1; 317 return 1;
317 } else { 318 } else {
318 cdrom_saw_media_change(drive); 319 cdrom_saw_media_change(drive);
319 320
320 if (blk_fs_request(rq) && !blk_rq_quiet(rq)) 321 if (rq->cmd_type == REQ_TYPE_FS &&
322 !(rq->cmd_flags & REQ_QUIET))
321 printk(KERN_ERR PFX "%s: tray open\n", 323 printk(KERN_ERR PFX "%s: tray open\n",
322 drive->name); 324 drive->name);
323 } 325 }
@@ -326,7 +328,7 @@ static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
326 case UNIT_ATTENTION: 328 case UNIT_ATTENTION:
327 cdrom_saw_media_change(drive); 329 cdrom_saw_media_change(drive);
328 330
329 if (blk_fs_request(rq) == 0) 331 if (rq->cmd_type != REQ_TYPE_FS)
330 return 0; 332 return 0;
331 333
332 /* 334 /*
@@ -352,7 +354,7 @@ static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
352 * No point in retrying after an illegal request or data 354 * No point in retrying after an illegal request or data
353 * protect error. 355 * protect error.
354 */ 356 */
355 if (!blk_rq_quiet(rq)) 357 if (!(rq->cmd_flags & REQ_QUIET))
356 ide_dump_status(drive, "command error", stat); 358 ide_dump_status(drive, "command error", stat);
357 do_end_request = 1; 359 do_end_request = 1;
358 break; 360 break;
@@ -361,20 +363,20 @@ static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
361 * No point in re-trying a zillion times on a bad sector. 363 * No point in re-trying a zillion times on a bad sector.
362 * If we got here the error is not correctable. 364 * If we got here the error is not correctable.
363 */ 365 */
364 if (!blk_rq_quiet(rq)) 366 if (!(rq->cmd_flags & REQ_QUIET))
365 ide_dump_status(drive, "media error " 367 ide_dump_status(drive, "media error "
366 "(bad sector)", stat); 368 "(bad sector)", stat);
367 do_end_request = 1; 369 do_end_request = 1;
368 break; 370 break;
369 case BLANK_CHECK: 371 case BLANK_CHECK:
370 /* disk appears blank? */ 372 /* disk appears blank? */
371 if (!blk_rq_quiet(rq)) 373 if (!(rq->cmd_flags & REQ_QUIET))
372 ide_dump_status(drive, "media error (blank)", 374 ide_dump_status(drive, "media error (blank)",
373 stat); 375 stat);
374 do_end_request = 1; 376 do_end_request = 1;
375 break; 377 break;
376 default: 378 default:
377 if (blk_fs_request(rq) == 0) 379 if (rq->cmd_type != REQ_TYPE_FS)
378 break; 380 break;
379 if (err & ~ATA_ABORTED) { 381 if (err & ~ATA_ABORTED) {
380 /* go to the default handler for other errors */ 382 /* go to the default handler for other errors */
@@ -385,7 +387,7 @@ static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
385 do_end_request = 1; 387 do_end_request = 1;
386 } 388 }
387 389
388 if (blk_fs_request(rq) == 0) { 390 if (rq->cmd_type != REQ_TYPE_FS) {
389 rq->cmd_flags |= REQ_FAILED; 391 rq->cmd_flags |= REQ_FAILED;
390 do_end_request = 1; 392 do_end_request = 1;
391 } 393 }
@@ -532,7 +534,7 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
532 ide_expiry_t *expiry = NULL; 534 ide_expiry_t *expiry = NULL;
533 int dma_error = 0, dma, thislen, uptodate = 0; 535 int dma_error = 0, dma, thislen, uptodate = 0;
534 int write = (rq_data_dir(rq) == WRITE) ? 1 : 0, rc = 0; 536 int write = (rq_data_dir(rq) == WRITE) ? 1 : 0, rc = 0;
535 int sense = blk_sense_request(rq); 537 int sense = (rq->cmd_type == REQ_TYPE_SENSE);
536 unsigned int timeout; 538 unsigned int timeout;
537 u16 len; 539 u16 len;
538 u8 ireason, stat; 540 u8 ireason, stat;
@@ -575,7 +577,7 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
575 577
576 ide_read_bcount_and_ireason(drive, &len, &ireason); 578 ide_read_bcount_and_ireason(drive, &len, &ireason);
577 579
578 thislen = blk_fs_request(rq) ? len : cmd->nleft; 580 thislen = (rq->cmd_type == REQ_TYPE_FS) ? len : cmd->nleft;
579 if (thislen > len) 581 if (thislen > len)
580 thislen = len; 582 thislen = len;
581 583
@@ -584,7 +586,7 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
584 586
585 /* If DRQ is clear, the command has completed. */ 587 /* If DRQ is clear, the command has completed. */
586 if ((stat & ATA_DRQ) == 0) { 588 if ((stat & ATA_DRQ) == 0) {
587 if (blk_fs_request(rq)) { 589 if (rq->cmd_type == REQ_TYPE_FS) {
588 /* 590 /*
589 * If we're not done reading/writing, complain. 591 * If we're not done reading/writing, complain.
590 * Otherwise, complete the command normally. 592 * Otherwise, complete the command normally.
@@ -598,7 +600,7 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
598 rq->cmd_flags |= REQ_FAILED; 600 rq->cmd_flags |= REQ_FAILED;
599 uptodate = 0; 601 uptodate = 0;
600 } 602 }
601 } else if (!blk_pc_request(rq)) { 603 } else if (rq->cmd_type != REQ_TYPE_BLOCK_PC) {
602 ide_cd_request_sense_fixup(drive, cmd); 604 ide_cd_request_sense_fixup(drive, cmd);
603 605
604 uptodate = cmd->nleft ? 0 : 1; 606 uptodate = cmd->nleft ? 0 : 1;
@@ -647,7 +649,7 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
647 649
648 /* pad, if necessary */ 650 /* pad, if necessary */
649 if (len > 0) { 651 if (len > 0) {
650 if (blk_fs_request(rq) == 0 || write == 0) 652 if (rq->cmd_type != REQ_TYPE_FS || write == 0)
651 ide_pad_transfer(drive, write, len); 653 ide_pad_transfer(drive, write, len);
652 else { 654 else {
653 printk(KERN_ERR PFX "%s: confused, missing data\n", 655 printk(KERN_ERR PFX "%s: confused, missing data\n",
@@ -656,11 +658,11 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
656 } 658 }
657 } 659 }
658 660
659 if (blk_pc_request(rq)) { 661 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
660 timeout = rq->timeout; 662 timeout = rq->timeout;
661 } else { 663 } else {
662 timeout = ATAPI_WAIT_PC; 664 timeout = ATAPI_WAIT_PC;
663 if (!blk_fs_request(rq)) 665 if (rq->cmd_type != REQ_TYPE_FS)
664 expiry = ide_cd_expiry; 666 expiry = ide_cd_expiry;
665 } 667 }
666 668
@@ -669,7 +671,7 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
669 return ide_started; 671 return ide_started;
670 672
671out_end: 673out_end:
672 if (blk_pc_request(rq) && rc == 0) { 674 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && rc == 0) {
673 rq->resid_len = 0; 675 rq->resid_len = 0;
674 blk_end_request_all(rq, 0); 676 blk_end_request_all(rq, 0);
675 hwif->rq = NULL; 677 hwif->rq = NULL;
@@ -677,7 +679,7 @@ out_end:
677 if (sense && uptodate) 679 if (sense && uptodate)
678 ide_cd_complete_failed_rq(drive, rq); 680 ide_cd_complete_failed_rq(drive, rq);
679 681
680 if (blk_fs_request(rq)) { 682 if (rq->cmd_type == REQ_TYPE_FS) {
681 if (cmd->nleft == 0) 683 if (cmd->nleft == 0)
682 uptodate = 1; 684 uptodate = 1;
683 } else { 685 } else {
@@ -690,7 +692,7 @@ out_end:
690 return ide_stopped; 692 return ide_stopped;
691 693
692 /* make sure it's fully ended */ 694 /* make sure it's fully ended */
693 if (blk_fs_request(rq) == 0) { 695 if (rq->cmd_type != REQ_TYPE_FS) {
694 rq->resid_len -= cmd->nbytes - cmd->nleft; 696 rq->resid_len -= cmd->nbytes - cmd->nleft;
695 if (uptodate == 0 && (cmd->tf_flags & IDE_TFLAG_WRITE)) 697 if (uptodate == 0 && (cmd->tf_flags & IDE_TFLAG_WRITE))
696 rq->resid_len += cmd->last_xfer_len; 698 rq->resid_len += cmd->last_xfer_len;
@@ -750,7 +752,7 @@ static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
750 ide_debug_log(IDE_DBG_PC, "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x", 752 ide_debug_log(IDE_DBG_PC, "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x",
751 rq->cmd[0], rq->cmd_type); 753 rq->cmd[0], rq->cmd_type);
752 754
753 if (blk_pc_request(rq)) 755 if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
754 rq->cmd_flags |= REQ_QUIET; 756 rq->cmd_flags |= REQ_QUIET;
755 else 757 else
756 rq->cmd_flags &= ~REQ_FAILED; 758 rq->cmd_flags &= ~REQ_FAILED;
@@ -791,21 +793,26 @@ static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
791 if (drive->debug_mask & IDE_DBG_RQ) 793 if (drive->debug_mask & IDE_DBG_RQ)
792 blk_dump_rq_flags(rq, "ide_cd_do_request"); 794 blk_dump_rq_flags(rq, "ide_cd_do_request");
793 795
794 if (blk_fs_request(rq)) { 796 switch (rq->cmd_type) {
797 case REQ_TYPE_FS:
795 if (cdrom_start_rw(drive, rq) == ide_stopped) 798 if (cdrom_start_rw(drive, rq) == ide_stopped)
796 goto out_end; 799 goto out_end;
797 } else if (blk_sense_request(rq) || blk_pc_request(rq) || 800 break;
798 rq->cmd_type == REQ_TYPE_ATA_PC) { 801 case REQ_TYPE_SENSE:
802 case REQ_TYPE_BLOCK_PC:
803 case REQ_TYPE_ATA_PC:
799 if (!rq->timeout) 804 if (!rq->timeout)
800 rq->timeout = ATAPI_WAIT_PC; 805 rq->timeout = ATAPI_WAIT_PC;
801 806
802 cdrom_do_block_pc(drive, rq); 807 cdrom_do_block_pc(drive, rq);
803 } else if (blk_special_request(rq)) { 808 break;
809 case REQ_TYPE_SPECIAL:
804 /* right now this can only be a reset... */ 810 /* right now this can only be a reset... */
805 uptodate = 1; 811 uptodate = 1;
806 goto out_end; 812 goto out_end;
807 } else 813 default:
808 BUG(); 814 BUG();
815 }
809 816
810 /* prepare sense request for this command */ 817 /* prepare sense request for this command */
811 ide_prep_sense(drive, rq); 818 ide_prep_sense(drive, rq);
@@ -817,7 +824,7 @@ static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
817 824
818 cmd.rq = rq; 825 cmd.rq = rq;
819 826
820 if (blk_fs_request(rq) || blk_rq_bytes(rq)) { 827 if (rq->cmd_type == REQ_TYPE_FS || blk_rq_bytes(rq)) {
821 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq)); 828 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
822 ide_map_sg(drive, &cmd); 829 ide_map_sg(drive, &cmd);
823 } 830 }
@@ -1373,9 +1380,9 @@ static int ide_cdrom_prep_pc(struct request *rq)
1373 1380
1374static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq) 1381static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
1375{ 1382{
1376 if (blk_fs_request(rq)) 1383 if (rq->cmd_type == REQ_TYPE_FS)
1377 return ide_cdrom_prep_fs(q, rq); 1384 return ide_cdrom_prep_fs(q, rq);
1378 else if (blk_pc_request(rq)) 1385 else if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
1379 return ide_cdrom_prep_pc(rq); 1386 return ide_cdrom_prep_pc(rq);
1380 1387
1381 return 0; 1388 return 0;
@@ -1592,17 +1599,19 @@ static struct ide_driver ide_cdrom_driver = {
1592 1599
1593static int idecd_open(struct block_device *bdev, fmode_t mode) 1600static int idecd_open(struct block_device *bdev, fmode_t mode)
1594{ 1601{
1595 struct cdrom_info *info = ide_cd_get(bdev->bd_disk); 1602 struct cdrom_info *info;
1596 int rc = -ENOMEM; 1603 int rc = -ENXIO;
1597 1604
1605 lock_kernel();
1606 info = ide_cd_get(bdev->bd_disk);
1598 if (!info) 1607 if (!info)
1599 return -ENXIO; 1608 goto out;
1600 1609
1601 rc = cdrom_open(&info->devinfo, bdev, mode); 1610 rc = cdrom_open(&info->devinfo, bdev, mode);
1602
1603 if (rc < 0) 1611 if (rc < 0)
1604 ide_cd_put(info); 1612 ide_cd_put(info);
1605 1613out:
1614 unlock_kernel();
1606 return rc; 1615 return rc;
1607} 1616}
1608 1617
@@ -1610,9 +1619,11 @@ static int idecd_release(struct gendisk *disk, fmode_t mode)
1610{ 1619{
1611 struct cdrom_info *info = ide_drv_g(disk, cdrom_info); 1620 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
1612 1621
1622 lock_kernel();
1613 cdrom_release(&info->devinfo, mode); 1623 cdrom_release(&info->devinfo, mode);
1614 1624
1615 ide_cd_put(info); 1625 ide_cd_put(info);
1626 unlock_kernel();
1616 1627
1617 return 0; 1628 return 0;
1618} 1629}
@@ -1656,7 +1667,7 @@ static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
1656 return 0; 1667 return 0;
1657} 1668}
1658 1669
1659static int idecd_ioctl(struct block_device *bdev, fmode_t mode, 1670static int idecd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1660 unsigned int cmd, unsigned long arg) 1671 unsigned int cmd, unsigned long arg)
1661{ 1672{
1662 struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info); 1673 struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
@@ -1678,6 +1689,19 @@ static int idecd_ioctl(struct block_device *bdev, fmode_t mode,
1678 return err; 1689 return err;
1679} 1690}
1680 1691
1692static int idecd_ioctl(struct block_device *bdev, fmode_t mode,
1693 unsigned int cmd, unsigned long arg)
1694{
1695 int ret;
1696
1697 lock_kernel();
1698 ret = idecd_locked_ioctl(bdev, mode, cmd, arg);
1699 unlock_kernel();
1700
1701 return ret;
1702}
1703
1704
1681static int idecd_media_changed(struct gendisk *disk) 1705static int idecd_media_changed(struct gendisk *disk)
1682{ 1706{
1683 struct cdrom_info *info = ide_drv_g(disk, cdrom_info); 1707 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
@@ -1698,7 +1722,7 @@ static const struct block_device_operations idecd_ops = {
1698 .owner = THIS_MODULE, 1722 .owner = THIS_MODULE,
1699 .open = idecd_open, 1723 .open = idecd_open,
1700 .release = idecd_release, 1724 .release = idecd_release,
1701 .locked_ioctl = idecd_ioctl, 1725 .ioctl = idecd_ioctl,
1702 .media_changed = idecd_media_changed, 1726 .media_changed = idecd_media_changed,
1703 .revalidate_disk = idecd_revalidate_disk 1727 .revalidate_disk = idecd_revalidate_disk
1704}; 1728};
diff --git a/drivers/ide/ide-cd_ioctl.c b/drivers/ide/ide-cd_ioctl.c
index 02712bf045c..766b3deeb23 100644
--- a/drivers/ide/ide-cd_ioctl.c
+++ b/drivers/ide/ide-cd_ioctl.c
@@ -454,7 +454,7 @@ int ide_cdrom_packet(struct cdrom_device_info *cdi,
454 touch it at all. */ 454 touch it at all. */
455 455
456 if (cgc->data_direction == CGC_DATA_WRITE) 456 if (cgc->data_direction == CGC_DATA_WRITE)
457 flags |= REQ_RW; 457 flags |= REQ_WRITE;
458 458
459 if (cgc->sense) 459 if (cgc->sense)
460 memset(cgc->sense, 0, sizeof(struct request_sense)); 460 memset(cgc->sense, 0, sizeof(struct request_sense));
diff --git a/drivers/ide/ide-disk.c b/drivers/ide/ide-disk.c
index 33d65039cce..7433e07de30 100644
--- a/drivers/ide/ide-disk.c
+++ b/drivers/ide/ide-disk.c
@@ -184,7 +184,7 @@ static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
184 ide_hwif_t *hwif = drive->hwif; 184 ide_hwif_t *hwif = drive->hwif;
185 185
186 BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED); 186 BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED);
187 BUG_ON(!blk_fs_request(rq)); 187 BUG_ON(rq->cmd_type != REQ_TYPE_FS);
188 188
189 ledtrig_ide_activity(); 189 ledtrig_ide_activity();
190 190
@@ -427,10 +427,15 @@ static void ide_disk_unlock_native_capacity(ide_drive_t *drive)
427 drive->dev_flags |= IDE_DFLAG_NOHPA; /* disable HPA on resume */ 427 drive->dev_flags |= IDE_DFLAG_NOHPA; /* disable HPA on resume */
428} 428}
429 429
430static void idedisk_prepare_flush(struct request_queue *q, struct request *rq) 430static int idedisk_prep_fn(struct request_queue *q, struct request *rq)
431{ 431{
432 ide_drive_t *drive = q->queuedata; 432 ide_drive_t *drive = q->queuedata;
433 struct ide_cmd *cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC); 433 struct ide_cmd *cmd;
434
435 if (!(rq->cmd_flags & REQ_FLUSH))
436 return BLKPREP_OK;
437
438 cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
434 439
435 /* FIXME: map struct ide_taskfile on rq->cmd[] */ 440 /* FIXME: map struct ide_taskfile on rq->cmd[] */
436 BUG_ON(cmd == NULL); 441 BUG_ON(cmd == NULL);
@@ -448,6 +453,8 @@ static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
448 rq->cmd_type = REQ_TYPE_ATA_TASKFILE; 453 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
449 rq->special = cmd; 454 rq->special = cmd;
450 cmd->rq = rq; 455 cmd->rq = rq;
456
457 return BLKPREP_OK;
451} 458}
452 459
453ide_devset_get(multcount, mult_count); 460ide_devset_get(multcount, mult_count);
@@ -513,7 +520,6 @@ static void update_ordered(ide_drive_t *drive)
513{ 520{
514 u16 *id = drive->id; 521 u16 *id = drive->id;
515 unsigned ordered = QUEUE_ORDERED_NONE; 522 unsigned ordered = QUEUE_ORDERED_NONE;
516 prepare_flush_fn *prep_fn = NULL;
517 523
518 if (drive->dev_flags & IDE_DFLAG_WCACHE) { 524 if (drive->dev_flags & IDE_DFLAG_WCACHE) {
519 unsigned long long capacity; 525 unsigned long long capacity;
@@ -538,12 +544,12 @@ static void update_ordered(ide_drive_t *drive)
538 544
539 if (barrier) { 545 if (barrier) {
540 ordered = QUEUE_ORDERED_DRAIN_FLUSH; 546 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
541 prep_fn = idedisk_prepare_flush; 547 blk_queue_prep_rq(drive->queue, idedisk_prep_fn);
542 } 548 }
543 } else 549 } else
544 ordered = QUEUE_ORDERED_DRAIN; 550 ordered = QUEUE_ORDERED_DRAIN;
545 551
546 blk_queue_ordered(drive->queue, ordered, prep_fn); 552 blk_queue_ordered(drive->queue, ordered);
547} 553}
548 554
549ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE); 555ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE);
diff --git a/drivers/ide/ide-disk_ioctl.c b/drivers/ide/ide-disk_ioctl.c
index 7b783dd7c0b..ec94c66918f 100644
--- a/drivers/ide/ide-disk_ioctl.c
+++ b/drivers/ide/ide-disk_ioctl.c
@@ -1,6 +1,7 @@
1#include <linux/kernel.h> 1#include <linux/kernel.h>
2#include <linux/ide.h> 2#include <linux/ide.h>
3#include <linux/hdreg.h> 3#include <linux/hdreg.h>
4#include <linux/smp_lock.h>
4 5
5#include "ide-disk.h" 6#include "ide-disk.h"
6 7
@@ -18,9 +19,13 @@ int ide_disk_ioctl(ide_drive_t *drive, struct block_device *bdev, fmode_t mode,
18{ 19{
19 int err; 20 int err;
20 21
22 lock_kernel();
21 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_disk_ioctl_settings); 23 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_disk_ioctl_settings);
22 if (err != -EOPNOTSUPP) 24 if (err != -EOPNOTSUPP)
23 return err; 25 goto out;
24 26
25 return generic_ide_ioctl(drive, bdev, cmd, arg); 27 err = generic_ide_ioctl(drive, bdev, cmd, arg);
28out:
29 unlock_kernel();
30 return err;
26} 31}
diff --git a/drivers/ide/ide-eh.c b/drivers/ide/ide-eh.c
index e9abf2c3c33..c0aa93fb7a6 100644
--- a/drivers/ide/ide-eh.c
+++ b/drivers/ide/ide-eh.c
@@ -122,7 +122,7 @@ ide_startstop_t ide_error(ide_drive_t *drive, const char *msg, u8 stat)
122 return ide_stopped; 122 return ide_stopped;
123 123
124 /* retry only "normal" I/O: */ 124 /* retry only "normal" I/O: */
125 if (!blk_fs_request(rq)) { 125 if (rq->cmd_type != REQ_TYPE_FS) {
126 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) { 126 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
127 struct ide_cmd *cmd = rq->special; 127 struct ide_cmd *cmd = rq->special;
128 128
@@ -146,7 +146,8 @@ static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
146{ 146{
147 struct request *rq = drive->hwif->rq; 147 struct request *rq = drive->hwif->rq;
148 148
149 if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET) { 149 if (rq && rq->cmd_type == REQ_TYPE_SPECIAL &&
150 rq->cmd[0] == REQ_DRIVE_RESET) {
150 if (err <= 0 && rq->errors == 0) 151 if (err <= 0 && rq->errors == 0)
151 rq->errors = -EIO; 152 rq->errors = -EIO;
152 ide_complete_rq(drive, err ? err : 0, blk_rq_bytes(rq)); 153 ide_complete_rq(drive, err ? err : 0, blk_rq_bytes(rq));
diff --git a/drivers/ide/ide-floppy.c b/drivers/ide/ide-floppy.c
index 4713bdca20b..5406b6ea3ad 100644
--- a/drivers/ide/ide-floppy.c
+++ b/drivers/ide/ide-floppy.c
@@ -73,7 +73,7 @@ static int ide_floppy_callback(ide_drive_t *drive, int dsc)
73 drive->failed_pc = NULL; 73 drive->failed_pc = NULL;
74 74
75 if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 || 75 if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
76 (rq && blk_pc_request(rq))) 76 (rq && rq->cmd_type == REQ_TYPE_BLOCK_PC))
77 uptodate = 1; /* FIXME */ 77 uptodate = 1; /* FIXME */
78 else if (pc->c[0] == GPCMD_REQUEST_SENSE) { 78 else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
79 79
@@ -98,7 +98,7 @@ static int ide_floppy_callback(ide_drive_t *drive, int dsc)
98 "Aborting request!\n"); 98 "Aborting request!\n");
99 } 99 }
100 100
101 if (blk_special_request(rq)) 101 if (rq->cmd_type == REQ_TYPE_SPECIAL)
102 rq->errors = uptodate ? 0 : IDE_DRV_ERROR_GENERAL; 102 rq->errors = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
103 103
104 return uptodate; 104 return uptodate;
@@ -207,7 +207,7 @@ static void idefloppy_create_rw_cmd(ide_drive_t *drive,
207 memcpy(rq->cmd, pc->c, 12); 207 memcpy(rq->cmd, pc->c, 12);
208 208
209 pc->rq = rq; 209 pc->rq = rq;
210 if (rq->cmd_flags & REQ_RW) 210 if (rq->cmd_flags & REQ_WRITE)
211 pc->flags |= PC_FLAG_WRITING; 211 pc->flags |= PC_FLAG_WRITING;
212 212
213 pc->flags |= PC_FLAG_DMA_OK; 213 pc->flags |= PC_FLAG_DMA_OK;
@@ -247,14 +247,16 @@ static ide_startstop_t ide_floppy_do_request(ide_drive_t *drive,
247 } else 247 } else
248 printk(KERN_ERR PFX "%s: I/O error\n", drive->name); 248 printk(KERN_ERR PFX "%s: I/O error\n", drive->name);
249 249
250 if (blk_special_request(rq)) { 250 if (rq->cmd_type == REQ_TYPE_SPECIAL) {
251 rq->errors = 0; 251 rq->errors = 0;
252 ide_complete_rq(drive, 0, blk_rq_bytes(rq)); 252 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
253 return ide_stopped; 253 return ide_stopped;
254 } else 254 } else
255 goto out_end; 255 goto out_end;
256 } 256 }
257 if (blk_fs_request(rq)) { 257
258 switch (rq->cmd_type) {
259 case REQ_TYPE_FS:
258 if (((long)blk_rq_pos(rq) % floppy->bs_factor) || 260 if (((long)blk_rq_pos(rq) % floppy->bs_factor) ||
259 (blk_rq_sectors(rq) % floppy->bs_factor)) { 261 (blk_rq_sectors(rq) % floppy->bs_factor)) {
260 printk(KERN_ERR PFX "%s: unsupported r/w rq size\n", 262 printk(KERN_ERR PFX "%s: unsupported r/w rq size\n",
@@ -263,13 +265,18 @@ static ide_startstop_t ide_floppy_do_request(ide_drive_t *drive,
263 } 265 }
264 pc = &floppy->queued_pc; 266 pc = &floppy->queued_pc;
265 idefloppy_create_rw_cmd(drive, pc, rq, (unsigned long)block); 267 idefloppy_create_rw_cmd(drive, pc, rq, (unsigned long)block);
266 } else if (blk_special_request(rq) || blk_sense_request(rq)) { 268 break;
269 case REQ_TYPE_SPECIAL:
270 case REQ_TYPE_SENSE:
267 pc = (struct ide_atapi_pc *)rq->special; 271 pc = (struct ide_atapi_pc *)rq->special;
268 } else if (blk_pc_request(rq)) { 272 break;
273 case REQ_TYPE_BLOCK_PC:
269 pc = &floppy->queued_pc; 274 pc = &floppy->queued_pc;
270 idefloppy_blockpc_cmd(floppy, pc, rq); 275 idefloppy_blockpc_cmd(floppy, pc, rq);
271 } else 276 break;
277 default:
272 BUG(); 278 BUG();
279 }
273 280
274 ide_prep_sense(drive, rq); 281 ide_prep_sense(drive, rq);
275 282
@@ -280,7 +287,7 @@ static ide_startstop_t ide_floppy_do_request(ide_drive_t *drive,
280 287
281 cmd.rq = rq; 288 cmd.rq = rq;
282 289
283 if (blk_fs_request(rq) || blk_rq_bytes(rq)) { 290 if (rq->cmd_type == REQ_TYPE_FS || blk_rq_bytes(rq)) {
284 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq)); 291 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
285 ide_map_sg(drive, &cmd); 292 ide_map_sg(drive, &cmd);
286 } 293 }
@@ -290,7 +297,7 @@ static ide_startstop_t ide_floppy_do_request(ide_drive_t *drive,
290 return ide_floppy_issue_pc(drive, &cmd, pc); 297 return ide_floppy_issue_pc(drive, &cmd, pc);
291out_end: 298out_end:
292 drive->failed_pc = NULL; 299 drive->failed_pc = NULL;
293 if (blk_fs_request(rq) == 0 && rq->errors == 0) 300 if (rq->cmd_type != REQ_TYPE_FS && rq->errors == 0)
294 rq->errors = -EIO; 301 rq->errors = -EIO;
295 ide_complete_rq(drive, -EIO, blk_rq_bytes(rq)); 302 ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
296 return ide_stopped; 303 return ide_stopped;
diff --git a/drivers/ide/ide-floppy_ioctl.c b/drivers/ide/ide-floppy_ioctl.c
index 9c2288234de..fd3d05ab341 100644
--- a/drivers/ide/ide-floppy_ioctl.c
+++ b/drivers/ide/ide-floppy_ioctl.c
@@ -5,6 +5,7 @@
5#include <linux/kernel.h> 5#include <linux/kernel.h>
6#include <linux/ide.h> 6#include <linux/ide.h>
7#include <linux/cdrom.h> 7#include <linux/cdrom.h>
8#include <linux/smp_lock.h>
8 9
9#include <asm/unaligned.h> 10#include <asm/unaligned.h>
10 11
@@ -275,12 +276,15 @@ int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,
275 void __user *argp = (void __user *)arg; 276 void __user *argp = (void __user *)arg;
276 int err; 277 int err;
277 278
278 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR) 279 lock_kernel();
279 return ide_floppy_lockdoor(drive, &pc, arg, cmd); 280 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR) {
281 err = ide_floppy_lockdoor(drive, &pc, arg, cmd);
282 goto out;
283 }
280 284
281 err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp); 285 err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp);
282 if (err != -ENOTTY) 286 if (err != -ENOTTY)
283 return err; 287 goto out;
284 288
285 /* 289 /*
286 * skip SCSI_IOCTL_SEND_COMMAND (deprecated) 290 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
@@ -293,5 +297,7 @@ int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,
293 if (err == -ENOTTY) 297 if (err == -ENOTTY)
294 err = generic_ide_ioctl(drive, bdev, cmd, arg); 298 err = generic_ide_ioctl(drive, bdev, cmd, arg);
295 299
300out:
301 unlock_kernel();
296 return err; 302 return err;
297} 303}
diff --git a/drivers/ide/ide-gd.c b/drivers/ide/ide-gd.c
index 79399534782..70aeeb18833 100644
--- a/drivers/ide/ide-gd.c
+++ b/drivers/ide/ide-gd.c
@@ -1,3 +1,4 @@
1#include <linux/smp_lock.h>
1#include <linux/module.h> 2#include <linux/module.h>
2#include <linux/types.h> 3#include <linux/types.h>
3#include <linux/string.h> 4#include <linux/string.h>
@@ -237,6 +238,18 @@ out_put_idkp:
237 return ret; 238 return ret;
238} 239}
239 240
241static int ide_gd_unlocked_open(struct block_device *bdev, fmode_t mode)
242{
243 int ret;
244
245 lock_kernel();
246 ret = ide_gd_open(bdev, mode);
247 unlock_kernel();
248
249 return ret;
250}
251
252
240static int ide_gd_release(struct gendisk *disk, fmode_t mode) 253static int ide_gd_release(struct gendisk *disk, fmode_t mode)
241{ 254{
242 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj); 255 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
@@ -244,6 +257,7 @@ static int ide_gd_release(struct gendisk *disk, fmode_t mode)
244 257
245 ide_debug_log(IDE_DBG_FUNC, "enter"); 258 ide_debug_log(IDE_DBG_FUNC, "enter");
246 259
260 lock_kernel();
247 if (idkp->openers == 1) 261 if (idkp->openers == 1)
248 drive->disk_ops->flush(drive); 262 drive->disk_ops->flush(drive);
249 263
@@ -255,6 +269,7 @@ static int ide_gd_release(struct gendisk *disk, fmode_t mode)
255 idkp->openers--; 269 idkp->openers--;
256 270
257 ide_disk_put(idkp); 271 ide_disk_put(idkp);
272 unlock_kernel();
258 273
259 return 0; 274 return 0;
260} 275}
@@ -321,9 +336,9 @@ static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,
321 336
322static const struct block_device_operations ide_gd_ops = { 337static const struct block_device_operations ide_gd_ops = {
323 .owner = THIS_MODULE, 338 .owner = THIS_MODULE,
324 .open = ide_gd_open, 339 .open = ide_gd_unlocked_open,
325 .release = ide_gd_release, 340 .release = ide_gd_release,
326 .locked_ioctl = ide_gd_ioctl, 341 .ioctl = ide_gd_ioctl,
327 .getgeo = ide_gd_getgeo, 342 .getgeo = ide_gd_getgeo,
328 .media_changed = ide_gd_media_changed, 343 .media_changed = ide_gd_media_changed,
329 .unlock_native_capacity = ide_gd_unlock_native_capacity, 344 .unlock_native_capacity = ide_gd_unlock_native_capacity,
diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
index 172ac921815..a381be81407 100644
--- a/drivers/ide/ide-io.c
+++ b/drivers/ide/ide-io.c
@@ -135,7 +135,7 @@ EXPORT_SYMBOL(ide_complete_rq);
135 135
136void ide_kill_rq(ide_drive_t *drive, struct request *rq) 136void ide_kill_rq(ide_drive_t *drive, struct request *rq)
137{ 137{
138 u8 drv_req = blk_special_request(rq) && rq->rq_disk; 138 u8 drv_req = (rq->cmd_type == REQ_TYPE_SPECIAL) && rq->rq_disk;
139 u8 media = drive->media; 139 u8 media = drive->media;
140 140
141 drive->failed_pc = NULL; 141 drive->failed_pc = NULL;
@@ -145,7 +145,7 @@ void ide_kill_rq(ide_drive_t *drive, struct request *rq)
145 } else { 145 } else {
146 if (media == ide_tape) 146 if (media == ide_tape)
147 rq->errors = IDE_DRV_ERROR_GENERAL; 147 rq->errors = IDE_DRV_ERROR_GENERAL;
148 else if (blk_fs_request(rq) == 0 && rq->errors == 0) 148 else if (rq->cmd_type != REQ_TYPE_FS && rq->errors == 0)
149 rq->errors = -EIO; 149 rq->errors = -EIO;
150 } 150 }
151 151
@@ -307,7 +307,7 @@ static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
307{ 307{
308 ide_startstop_t startstop; 308 ide_startstop_t startstop;
309 309
310 BUG_ON(!blk_rq_started(rq)); 310 BUG_ON(!(rq->cmd_flags & REQ_STARTED));
311 311
312#ifdef DEBUG 312#ifdef DEBUG
313 printk("%s: start_request: current=0x%08lx\n", 313 printk("%s: start_request: current=0x%08lx\n",
@@ -353,7 +353,7 @@ static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
353 pm->pm_step == IDE_PM_COMPLETED) 353 pm->pm_step == IDE_PM_COMPLETED)
354 ide_complete_pm_rq(drive, rq); 354 ide_complete_pm_rq(drive, rq);
355 return startstop; 355 return startstop;
356 } else if (!rq->rq_disk && blk_special_request(rq)) 356 } else if (!rq->rq_disk && rq->cmd_type == REQ_TYPE_SPECIAL)
357 /* 357 /*
358 * TODO: Once all ULDs have been modified to 358 * TODO: Once all ULDs have been modified to
359 * check for specific op codes rather than 359 * check for specific op codes rather than
diff --git a/drivers/ide/ide-pm.c b/drivers/ide/ide-pm.c
index 1c08311b0a0..92406097efe 100644
--- a/drivers/ide/ide-pm.c
+++ b/drivers/ide/ide-pm.c
@@ -191,10 +191,10 @@ void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
191 191
192#ifdef DEBUG_PM 192#ifdef DEBUG_PM
193 printk("%s: completing PM request, %s\n", drive->name, 193 printk("%s: completing PM request, %s\n", drive->name,
194 blk_pm_suspend_request(rq) ? "suspend" : "resume"); 194 (rq->cmd_type == REQ_TYPE_PM_SUSPEND) ? "suspend" : "resume");
195#endif 195#endif
196 spin_lock_irqsave(q->queue_lock, flags); 196 spin_lock_irqsave(q->queue_lock, flags);
197 if (blk_pm_suspend_request(rq)) 197 if (rq->cmd_type == REQ_TYPE_PM_SUSPEND)
198 blk_stop_queue(q); 198 blk_stop_queue(q);
199 else 199 else
200 drive->dev_flags &= ~IDE_DFLAG_BLOCKED; 200 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
@@ -210,11 +210,11 @@ void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
210{ 210{
211 struct request_pm_state *pm = rq->special; 211 struct request_pm_state *pm = rq->special;
212 212
213 if (blk_pm_suspend_request(rq) && 213 if (rq->cmd_type == REQ_TYPE_PM_SUSPEND &&
214 pm->pm_step == IDE_PM_START_SUSPEND) 214 pm->pm_step == IDE_PM_START_SUSPEND)
215 /* Mark drive blocked when starting the suspend sequence. */ 215 /* Mark drive blocked when starting the suspend sequence. */
216 drive->dev_flags |= IDE_DFLAG_BLOCKED; 216 drive->dev_flags |= IDE_DFLAG_BLOCKED;
217 else if (blk_pm_resume_request(rq) && 217 else if (rq->cmd_type == REQ_TYPE_PM_RESUME &&
218 pm->pm_step == IDE_PM_START_RESUME) { 218 pm->pm_step == IDE_PM_START_RESUME) {
219 /* 219 /*
220 * The first thing we do on wakeup is to wait for BSY bit to 220 * The first thing we do on wakeup is to wait for BSY bit to
diff --git a/drivers/ide/ide-tape.c b/drivers/ide/ide-tape.c
index b07232880ec..6d622cb5ac8 100644
--- a/drivers/ide/ide-tape.c
+++ b/drivers/ide/ide-tape.c
@@ -32,6 +32,7 @@
32#include <linux/errno.h> 32#include <linux/errno.h>
33#include <linux/genhd.h> 33#include <linux/genhd.h>
34#include <linux/seq_file.h> 34#include <linux/seq_file.h>
35#include <linux/smp_lock.h>
35#include <linux/slab.h> 36#include <linux/slab.h>
36#include <linux/pci.h> 37#include <linux/pci.h>
37#include <linux/ide.h> 38#include <linux/ide.h>
@@ -577,7 +578,8 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
577 rq->cmd[0], (unsigned long long)blk_rq_pos(rq), 578 rq->cmd[0], (unsigned long long)blk_rq_pos(rq),
578 blk_rq_sectors(rq)); 579 blk_rq_sectors(rq));
579 580
580 BUG_ON(!(blk_special_request(rq) || blk_sense_request(rq))); 581 BUG_ON(!(rq->cmd_type == REQ_TYPE_SPECIAL ||
582 rq->cmd_type == REQ_TYPE_SENSE));
581 583
582 /* Retry a failed packet command */ 584 /* Retry a failed packet command */
583 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) { 585 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
@@ -1905,7 +1907,11 @@ static const struct file_operations idetape_fops = {
1905 1907
1906static int idetape_open(struct block_device *bdev, fmode_t mode) 1908static int idetape_open(struct block_device *bdev, fmode_t mode)
1907{ 1909{
1908 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0); 1910 struct ide_tape_obj *tape;
1911
1912 lock_kernel();
1913 tape = ide_tape_get(bdev->bd_disk, false, 0);
1914 unlock_kernel();
1909 1915
1910 if (!tape) 1916 if (!tape)
1911 return -ENXIO; 1917 return -ENXIO;
@@ -1917,7 +1923,10 @@ static int idetape_release(struct gendisk *disk, fmode_t mode)
1917{ 1923{
1918 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj); 1924 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
1919 1925
1926 lock_kernel();
1920 ide_tape_put(tape); 1927 ide_tape_put(tape);
1928 unlock_kernel();
1929
1921 return 0; 1930 return 0;
1922} 1931}
1923 1932
@@ -1926,9 +1935,14 @@ static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
1926{ 1935{
1927 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj); 1936 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
1928 ide_drive_t *drive = tape->drive; 1937 ide_drive_t *drive = tape->drive;
1929 int err = generic_ide_ioctl(drive, bdev, cmd, arg); 1938 int err;
1939
1940 lock_kernel();
1941 err = generic_ide_ioctl(drive, bdev, cmd, arg);
1930 if (err == -EINVAL) 1942 if (err == -EINVAL)
1931 err = idetape_blkdev_ioctl(drive, cmd, arg); 1943 err = idetape_blkdev_ioctl(drive, cmd, arg);
1944 unlock_kernel();
1945
1932 return err; 1946 return err;
1933} 1947}
1934 1948
@@ -1936,7 +1950,7 @@ static const struct block_device_operations idetape_block_ops = {
1936 .owner = THIS_MODULE, 1950 .owner = THIS_MODULE,
1937 .open = idetape_open, 1951 .open = idetape_open,
1938 .release = idetape_release, 1952 .release = idetape_release,
1939 .locked_ioctl = idetape_ioctl, 1953 .ioctl = idetape_ioctl,
1940}; 1954};
1941 1955
1942static int ide_tape_probe(ide_drive_t *drive) 1956static int ide_tape_probe(ide_drive_t *drive)
diff --git a/drivers/infiniband/hw/cxgb4/device.c b/drivers/infiniband/hw/cxgb4/device.c
index d870f9c17c1..9bbf491d5d9 100644
--- a/drivers/infiniband/hw/cxgb4/device.c
+++ b/drivers/infiniband/hw/cxgb4/device.c
@@ -250,12 +250,17 @@ static int c4iw_rdev_open(struct c4iw_rdev *rdev)
250 rdev->cqshift = PAGE_SHIFT - ilog2(rdev->lldi.ucq_density); 250 rdev->cqshift = PAGE_SHIFT - ilog2(rdev->lldi.ucq_density);
251 rdev->cqmask = rdev->lldi.ucq_density - 1; 251 rdev->cqmask = rdev->lldi.ucq_density - 1;
252 PDBG("%s dev %s stag start 0x%0x size 0x%0x num stags %d " 252 PDBG("%s dev %s stag start 0x%0x size 0x%0x num stags %d "
253 "pbl start 0x%0x size 0x%0x rq start 0x%0x size 0x%0x\n", 253 "pbl start 0x%0x size 0x%0x rq start 0x%0x size 0x%0x "
254 "qp qid start %u size %u cq qid start %u size %u\n",
254 __func__, pci_name(rdev->lldi.pdev), rdev->lldi.vr->stag.start, 255 __func__, pci_name(rdev->lldi.pdev), rdev->lldi.vr->stag.start,
255 rdev->lldi.vr->stag.size, c4iw_num_stags(rdev), 256 rdev->lldi.vr->stag.size, c4iw_num_stags(rdev),
256 rdev->lldi.vr->pbl.start, 257 rdev->lldi.vr->pbl.start,
257 rdev->lldi.vr->pbl.size, rdev->lldi.vr->rq.start, 258 rdev->lldi.vr->pbl.size, rdev->lldi.vr->rq.start,
258 rdev->lldi.vr->rq.size); 259 rdev->lldi.vr->rq.size,
260 rdev->lldi.vr->qp.start,
261 rdev->lldi.vr->qp.size,
262 rdev->lldi.vr->cq.start,
263 rdev->lldi.vr->cq.size);
259 PDBG("udb len 0x%x udb base %p db_reg %p gts_reg %p qpshift %lu " 264 PDBG("udb len 0x%x udb base %p db_reg %p gts_reg %p qpshift %lu "
260 "qpmask 0x%x cqshift %lu cqmask 0x%x\n", 265 "qpmask 0x%x cqshift %lu cqmask 0x%x\n",
261 (unsigned)pci_resource_len(rdev->lldi.pdev, 2), 266 (unsigned)pci_resource_len(rdev->lldi.pdev, 2),
diff --git a/drivers/infiniband/hw/cxgb4/resource.c b/drivers/infiniband/hw/cxgb4/resource.c
index fb195d1d901..83b23dfa250 100644
--- a/drivers/infiniband/hw/cxgb4/resource.c
+++ b/drivers/infiniband/hw/cxgb4/resource.c
@@ -110,11 +110,12 @@ static int c4iw_init_qid_fifo(struct c4iw_rdev *rdev)
110 110
111 spin_lock_init(&rdev->resource.qid_fifo_lock); 111 spin_lock_init(&rdev->resource.qid_fifo_lock);
112 112
113 if (kfifo_alloc(&rdev->resource.qid_fifo, T4_MAX_QIDS * sizeof(u32), 113 if (kfifo_alloc(&rdev->resource.qid_fifo, rdev->lldi.vr->qp.size *
114 GFP_KERNEL)) 114 sizeof(u32), GFP_KERNEL))
115 return -ENOMEM; 115 return -ENOMEM;
116 116
117 for (i = T4_QID_BASE; i < T4_QID_BASE + T4_MAX_QIDS; i++) 117 for (i = rdev->lldi.vr->qp.start;
118 i < rdev->lldi.vr->qp.start + rdev->lldi.vr->qp.size; i++)
118 if (!(i & rdev->qpmask)) 119 if (!(i & rdev->qpmask))
119 kfifo_in(&rdev->resource.qid_fifo, 120 kfifo_in(&rdev->resource.qid_fifo,
120 (unsigned char *) &i, sizeof(u32)); 121 (unsigned char *) &i, sizeof(u32));
diff --git a/drivers/infiniband/hw/cxgb4/t4.h b/drivers/infiniband/hw/cxgb4/t4.h
index aef55f42bea..24f369046ef 100644
--- a/drivers/infiniband/hw/cxgb4/t4.h
+++ b/drivers/infiniband/hw/cxgb4/t4.h
@@ -36,8 +36,6 @@
36#include "t4_msg.h" 36#include "t4_msg.h"
37#include "t4fw_ri_api.h" 37#include "t4fw_ri_api.h"
38 38
39#define T4_QID_BASE 1024
40#define T4_MAX_QIDS 256
41#define T4_MAX_NUM_QP (1<<16) 39#define T4_MAX_NUM_QP (1<<16)
42#define T4_MAX_NUM_CQ (1<<15) 40#define T4_MAX_NUM_CQ (1<<15)
43#define T4_MAX_NUM_PD (1<<15) 41#define T4_MAX_NUM_PD (1<<15)
diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
index 054edf346e0..c908c5f8364 100644
--- a/drivers/input/evdev.c
+++ b/drivers/input/evdev.c
@@ -492,13 +492,15 @@ static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
492} 492}
493 493
494#define OLD_KEY_MAX 0x1ff 494#define OLD_KEY_MAX 0x1ff
495static int handle_eviocgbit(struct input_dev *dev, unsigned int cmd, void __user *p, int compat_mode) 495static int handle_eviocgbit(struct input_dev *dev,
496 unsigned int type, unsigned int size,
497 void __user *p, int compat_mode)
496{ 498{
497 static unsigned long keymax_warn_time; 499 static unsigned long keymax_warn_time;
498 unsigned long *bits; 500 unsigned long *bits;
499 int len; 501 int len;
500 502
501 switch (_IOC_NR(cmd) & EV_MAX) { 503 switch (type) {
502 504
503 case 0: bits = dev->evbit; len = EV_MAX; break; 505 case 0: bits = dev->evbit; len = EV_MAX; break;
504 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break; 506 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
@@ -517,7 +519,7 @@ static int handle_eviocgbit(struct input_dev *dev, unsigned int cmd, void __user
517 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len' 519 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
518 * should be in bytes, not in bits. 520 * should be in bytes, not in bits.
519 */ 521 */
520 if ((_IOC_NR(cmd) & EV_MAX) == EV_KEY && _IOC_SIZE(cmd) == OLD_KEY_MAX) { 522 if (type == EV_KEY && size == OLD_KEY_MAX) {
521 len = OLD_KEY_MAX; 523 len = OLD_KEY_MAX;
522 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000)) 524 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
523 printk(KERN_WARNING 525 printk(KERN_WARNING
@@ -528,7 +530,7 @@ static int handle_eviocgbit(struct input_dev *dev, unsigned int cmd, void __user
528 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long)); 530 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
529 } 531 }
530 532
531 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode); 533 return bits_to_user(bits, len, size, p, compat_mode);
532} 534}
533#undef OLD_KEY_MAX 535#undef OLD_KEY_MAX
534 536
@@ -542,8 +544,10 @@ static long evdev_do_ioctl(struct file *file, unsigned int cmd,
542 struct ff_effect effect; 544 struct ff_effect effect;
543 int __user *ip = (int __user *)p; 545 int __user *ip = (int __user *)p;
544 unsigned int i, t, u, v; 546 unsigned int i, t, u, v;
547 unsigned int size;
545 int error; 548 int error;
546 549
550 /* First we check for fixed-length commands */
547 switch (cmd) { 551 switch (cmd) {
548 552
549 case EVIOCGVERSION: 553 case EVIOCGVERSION:
@@ -610,112 +614,102 @@ static long evdev_do_ioctl(struct file *file, unsigned int cmd,
610 return evdev_grab(evdev, client); 614 return evdev_grab(evdev, client);
611 else 615 else
612 return evdev_ungrab(evdev, client); 616 return evdev_ungrab(evdev, client);
617 }
613 618
614 default: 619 size = _IOC_SIZE(cmd);
615
616 if (_IOC_TYPE(cmd) != 'E')
617 return -EINVAL;
618
619 if (_IOC_DIR(cmd) == _IOC_READ) {
620 620
621 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0))) 621 /* Now check variable-length commands */
622 return handle_eviocgbit(dev, cmd, p, compat_mode); 622#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
623 623
624 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) 624 switch (EVIOC_MASK_SIZE(cmd)) {
625 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
626 p, compat_mode);
627 625
628 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) 626 case EVIOCGKEY(0):
629 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd), 627 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
630 p, compat_mode);
631 628
632 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) 629 case EVIOCGLED(0):
633 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd), 630 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
634 p, compat_mode);
635 631
636 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) 632 case EVIOCGSND(0):
637 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd), 633 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
638 p, compat_mode);
639 634
640 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) 635 case EVIOCGSW(0):
641 return str_to_user(dev->name, _IOC_SIZE(cmd), p); 636 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
642 637
643 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) 638 case EVIOCGNAME(0):
644 return str_to_user(dev->phys, _IOC_SIZE(cmd), p); 639 return str_to_user(dev->name, size, p);
645 640
646 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) 641 case EVIOCGPHYS(0):
647 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p); 642 return str_to_user(dev->phys, size, p);
648 643
649 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { 644 case EVIOCGUNIQ(0):
645 return str_to_user(dev->uniq, size, p);
650 646
651 t = _IOC_NR(cmd) & ABS_MAX; 647 case EVIOC_MASK_SIZE(EVIOCSFF):
648 if (input_ff_effect_from_user(p, size, &effect))
649 return -EFAULT;
652 650
653 abs.value = dev->abs[t]; 651 error = input_ff_upload(dev, &effect, file);
654 abs.minimum = dev->absmin[t];
655 abs.maximum = dev->absmax[t];
656 abs.fuzz = dev->absfuzz[t];
657 abs.flat = dev->absflat[t];
658 abs.resolution = dev->absres[t];
659 652
660 if (copy_to_user(p, &abs, min_t(size_t, 653 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
661 _IOC_SIZE(cmd), 654 return -EFAULT;
662 sizeof(struct input_absinfo))))
663 return -EFAULT;
664 655
665 return 0; 656 return error;
666 } 657 }
667 658
668 } 659 /* Multi-number variable-length handlers */
660 if (_IOC_TYPE(cmd) != 'E')
661 return -EINVAL;
669 662
670 if (_IOC_DIR(cmd) == _IOC_WRITE) { 663 if (_IOC_DIR(cmd) == _IOC_READ) {
671 664
672 if (_IOC_NR(cmd) == _IOC_NR(EVIOCSFF)) { 665 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
666 return handle_eviocgbit(dev,
667 _IOC_NR(cmd) & EV_MAX, size,
668 p, compat_mode);
673 669
674 if (input_ff_effect_from_user(p, _IOC_SIZE(cmd), &effect)) 670 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
675 return -EFAULT;
676 671
677 error = input_ff_upload(dev, &effect, file); 672 t = _IOC_NR(cmd) & ABS_MAX;
673 abs = dev->absinfo[t];
678 674
679 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id))) 675 if (copy_to_user(p, &abs, min_t(size_t,
680 return -EFAULT; 676 size, sizeof(struct input_absinfo))))
677 return -EFAULT;
681 678
682 return error; 679 return 0;
683 } 680 }
681 }
684 682
685 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) { 683 if (_IOC_DIR(cmd) == _IOC_READ) {
686 684
687 t = _IOC_NR(cmd) & ABS_MAX; 685 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
688 686
689 if (copy_from_user(&abs, p, min_t(size_t, 687 t = _IOC_NR(cmd) & ABS_MAX;
690 _IOC_SIZE(cmd),
691 sizeof(struct input_absinfo))))
692 return -EFAULT;
693 688
694 /* We can't change number of reserved MT slots */ 689 if (copy_from_user(&abs, p, min_t(size_t,
695 if (t == ABS_MT_SLOT) 690 size, sizeof(struct input_absinfo))))
696 return -EINVAL; 691 return -EFAULT;
697 692
698 /* 693 if (size < sizeof(struct input_absinfo))
699 * Take event lock to ensure that we are not 694 abs.resolution = 0;
700 * changing device parameters in the middle
701 * of event.
702 */
703 spin_lock_irq(&dev->event_lock);
704 695
705 dev->abs[t] = abs.value; 696 /* We can't change number of reserved MT slots */
706 dev->absmin[t] = abs.minimum; 697 if (t == ABS_MT_SLOT)
707 dev->absmax[t] = abs.maximum; 698 return -EINVAL;
708 dev->absfuzz[t] = abs.fuzz;
709 dev->absflat[t] = abs.flat;
710 dev->absres[t] = _IOC_SIZE(cmd) < sizeof(struct input_absinfo) ?
711 0 : abs.resolution;
712 699
713 spin_unlock_irq(&dev->event_lock); 700 /*
701 * Take event lock to ensure that we are not
702 * changing device parameters in the middle
703 * of event.
704 */
705 spin_lock_irq(&dev->event_lock);
706 dev->absinfo[t] = abs;
707 spin_unlock_irq(&dev->event_lock);
714 708
715 return 0; 709 return 0;
716 }
717 } 710 }
718 } 711 }
712
719 return -EINVAL; 713 return -EINVAL;
720} 714}
721 715
diff --git a/drivers/input/input.c b/drivers/input/input.c
index e1243b4b32a..a9b025f4147 100644
--- a/drivers/input/input.c
+++ b/drivers/input/input.c
@@ -182,7 +182,7 @@ static int input_handle_abs_event(struct input_dev *dev,
182 is_mt_event = code >= ABS_MT_FIRST && code <= ABS_MT_LAST; 182 is_mt_event = code >= ABS_MT_FIRST && code <= ABS_MT_LAST;
183 183
184 if (!is_mt_event) { 184 if (!is_mt_event) {
185 pold = &dev->abs[code]; 185 pold = &dev->absinfo[code].value;
186 } else if (dev->mt) { 186 } else if (dev->mt) {
187 struct input_mt_slot *mtslot = &dev->mt[dev->slot]; 187 struct input_mt_slot *mtslot = &dev->mt[dev->slot];
188 pold = &mtslot->abs[code - ABS_MT_FIRST]; 188 pold = &mtslot->abs[code - ABS_MT_FIRST];
@@ -196,7 +196,7 @@ static int input_handle_abs_event(struct input_dev *dev,
196 196
197 if (pold) { 197 if (pold) {
198 *pval = input_defuzz_abs_event(*pval, *pold, 198 *pval = input_defuzz_abs_event(*pval, *pold,
199 dev->absfuzz[code]); 199 dev->absinfo[code].fuzz);
200 if (*pold == *pval) 200 if (*pold == *pval)
201 return INPUT_IGNORE_EVENT; 201 return INPUT_IGNORE_EVENT;
202 202
@@ -204,8 +204,8 @@ static int input_handle_abs_event(struct input_dev *dev,
204 } 204 }
205 205
206 /* Flush pending "slot" event */ 206 /* Flush pending "slot" event */
207 if (is_mt_event && dev->slot != dev->abs[ABS_MT_SLOT]) { 207 if (is_mt_event && dev->slot != input_abs_get_val(dev, ABS_MT_SLOT)) {
208 dev->abs[ABS_MT_SLOT] = dev->slot; 208 input_abs_set_val(dev, ABS_MT_SLOT, dev->slot);
209 input_pass_event(dev, EV_ABS, ABS_MT_SLOT, dev->slot); 209 input_pass_event(dev, EV_ABS, ABS_MT_SLOT, dev->slot);
210 } 210 }
211 211
@@ -391,6 +391,43 @@ void input_inject_event(struct input_handle *handle,
391EXPORT_SYMBOL(input_inject_event); 391EXPORT_SYMBOL(input_inject_event);
392 392
393/** 393/**
394 * input_alloc_absinfo - allocates array of input_absinfo structs
395 * @dev: the input device emitting absolute events
396 *
397 * If the absinfo struct the caller asked for is already allocated, this
398 * functions will not do anything.
399 */
400void input_alloc_absinfo(struct input_dev *dev)
401{
402 if (!dev->absinfo)
403 dev->absinfo = kcalloc(ABS_CNT, sizeof(struct input_absinfo),
404 GFP_KERNEL);
405
406 WARN(!dev->absinfo, "%s(): kcalloc() failed?\n", __func__);
407}
408EXPORT_SYMBOL(input_alloc_absinfo);
409
410void input_set_abs_params(struct input_dev *dev, unsigned int axis,
411 int min, int max, int fuzz, int flat)
412{
413 struct input_absinfo *absinfo;
414
415 input_alloc_absinfo(dev);
416 if (!dev->absinfo)
417 return;
418
419 absinfo = &dev->absinfo[axis];
420 absinfo->minimum = min;
421 absinfo->maximum = max;
422 absinfo->fuzz = fuzz;
423 absinfo->flat = flat;
424
425 dev->absbit[BIT_WORD(axis)] |= BIT_MASK(axis);
426}
427EXPORT_SYMBOL(input_set_abs_params);
428
429
430/**
394 * input_grab_device - grabs device for exclusive use 431 * input_grab_device - grabs device for exclusive use
395 * @handle: input handle that wants to own the device 432 * @handle: input handle that wants to own the device
396 * 433 *
@@ -1308,6 +1345,7 @@ static void input_dev_release(struct device *device)
1308 1345
1309 input_ff_destroy(dev); 1346 input_ff_destroy(dev);
1310 input_mt_destroy_slots(dev); 1347 input_mt_destroy_slots(dev);
1348 kfree(dev->absinfo);
1311 kfree(dev); 1349 kfree(dev);
1312 1350
1313 module_put(THIS_MODULE); 1351 module_put(THIS_MODULE);
diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c
index 63834585c28..d85bd8a7967 100644
--- a/drivers/input/joydev.c
+++ b/drivers/input/joydev.c
@@ -530,7 +530,7 @@ static int joydev_ioctl_common(struct joydev *joydev,
530{ 530{
531 struct input_dev *dev = joydev->handle.dev; 531 struct input_dev *dev = joydev->handle.dev;
532 size_t len; 532 size_t len;
533 int i, j; 533 int i;
534 const char *name; 534 const char *name;
535 535
536 /* Process fixed-sized commands. */ 536 /* Process fixed-sized commands. */
@@ -562,12 +562,11 @@ static int joydev_ioctl_common(struct joydev *joydev,
562 case JSIOCSCORR: 562 case JSIOCSCORR:
563 if (copy_from_user(joydev->corr, argp, 563 if (copy_from_user(joydev->corr, argp,
564 sizeof(joydev->corr[0]) * joydev->nabs)) 564 sizeof(joydev->corr[0]) * joydev->nabs))
565 return -EFAULT; 565 return -EFAULT;
566 566
567 for (i = 0; i < joydev->nabs; i++) { 567 for (i = 0; i < joydev->nabs; i++) {
568 j = joydev->abspam[i]; 568 int val = input_abs_get_val(dev, joydev->abspam[i]);
569 joydev->abs[i] = joydev_correct(dev->abs[j], 569 joydev->abs[i] = joydev_correct(val, &joydev->corr[i]);
570 &joydev->corr[i]);
571 } 570 }
572 return 0; 571 return 0;
573 572
@@ -848,25 +847,27 @@ static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
848 847
849 for (i = 0; i < joydev->nabs; i++) { 848 for (i = 0; i < joydev->nabs; i++) {
850 j = joydev->abspam[i]; 849 j = joydev->abspam[i];
851 if (dev->absmax[j] == dev->absmin[j]) { 850 if (input_abs_get_max(dev, j) == input_abs_get_min(dev, j)) {
852 joydev->corr[i].type = JS_CORR_NONE; 851 joydev->corr[i].type = JS_CORR_NONE;
853 joydev->abs[i] = dev->abs[j]; 852 joydev->abs[i] = input_abs_get_val(dev, j);
854 continue; 853 continue;
855 } 854 }
856 joydev->corr[i].type = JS_CORR_BROKEN; 855 joydev->corr[i].type = JS_CORR_BROKEN;
857 joydev->corr[i].prec = dev->absfuzz[j]; 856 joydev->corr[i].prec = input_abs_get_fuzz(dev, j);
858 joydev->corr[i].coef[0] = 857
859 (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j]; 858 t = (input_abs_get_max(dev, j) + input_abs_get_min(dev, j)) / 2;
860 joydev->corr[i].coef[1] = 859 joydev->corr[i].coef[0] = t - input_abs_get_flat(dev, j);
861 (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j]; 860 joydev->corr[i].coef[1] = t + input_abs_get_flat(dev, j);
862 861
863 t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j]; 862 t = (input_abs_get_max(dev, j) - input_abs_get_min(dev, j)) / 2
863 - 2 * input_abs_get_flat(dev, j);
864 if (t) { 864 if (t) {
865 joydev->corr[i].coef[2] = (1 << 29) / t; 865 joydev->corr[i].coef[2] = (1 << 29) / t;
866 joydev->corr[i].coef[3] = (1 << 29) / t; 866 joydev->corr[i].coef[3] = (1 << 29) / t;
867 867
868 joydev->abs[i] = joydev_correct(dev->abs[j], 868 joydev->abs[i] =
869 joydev->corr + i); 869 joydev_correct(input_abs_get_val(dev, j),
870 joydev->corr + i);
870 } 871 }
871 } 872 }
872 873
diff --git a/drivers/input/joystick/a3d.c b/drivers/input/joystick/a3d.c
index 6489f4010c4..d259b41354b 100644
--- a/drivers/input/joystick/a3d.c
+++ b/drivers/input/joystick/a3d.c
@@ -342,7 +342,8 @@ static int a3d_connect(struct gameport *gameport, struct gameport_driver *drv)
342 342
343 for (i = 0; i < 4; i++) { 343 for (i = 0; i < 4; i++) {
344 if (i < 2) 344 if (i < 2)
345 input_set_abs_params(input_dev, axes[i], 48, input_dev->abs[axes[i]] * 2 - 48, 0, 8); 345 input_set_abs_params(input_dev, axes[i],
346 48, input_abs_get_val(input_dev, axes[i]) * 2 - 48, 0, 8);
346 else 347 else
347 input_set_abs_params(input_dev, axes[i], 2, 253, 0, 0); 348 input_set_abs_params(input_dev, axes[i], 2, 253, 0, 0);
348 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0); 349 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
diff --git a/drivers/input/joystick/adi.c b/drivers/input/joystick/adi.c
index 89c4c084d4a..b992fbf91f2 100644
--- a/drivers/input/joystick/adi.c
+++ b/drivers/input/joystick/adi.c
@@ -452,7 +452,7 @@ static void adi_init_center(struct adi *adi)
452 for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) { 452 for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) {
453 453
454 t = adi->abs[i]; 454 t = adi->abs[i];
455 x = adi->dev->abs[t]; 455 x = input_abs_get_val(adi->dev, t);
456 456
457 if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE) 457 if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE)
458 x = i < adi->axes10 ? 512 : 128; 458 x = i < adi->axes10 ? 512 : 128;
diff --git a/drivers/input/joystick/amijoy.c b/drivers/input/joystick/amijoy.c
index 05022f07ec7..e90694fe0d5 100644
--- a/drivers/input/joystick/amijoy.c
+++ b/drivers/input/joystick/amijoy.c
@@ -139,8 +139,8 @@ static int __init amijoy_init(void)
139 amijoy_dev[i]->keybit[BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) | 139 amijoy_dev[i]->keybit[BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) |
140 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT); 140 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
141 for (j = 0; j < 2; j++) { 141 for (j = 0; j < 2; j++) {
142 amijoy_dev[i]->absmin[ABS_X + j] = -1; 142 XXinput_set_abs_params(amijoy_dev[i], ABS_X + j,
143 amijoy_dev[i]->absmax[ABS_X + j] = 1; 143 -1, 1, 0, 0);
144 } 144 }
145 145
146 err = input_register_device(amijoy_dev[i]); 146 err = input_register_device(amijoy_dev[i]);
diff --git a/drivers/input/joystick/gf2k.c b/drivers/input/joystick/gf2k.c
index 45ac70eae0a..0536b1b2f01 100644
--- a/drivers/input/joystick/gf2k.c
+++ b/drivers/input/joystick/gf2k.c
@@ -318,11 +318,8 @@ static int gf2k_connect(struct gameport *gameport, struct gameport_driver *drv)
318 for (i = 0; i < gf2k_axes[gf2k->id]; i++) 318 for (i = 0; i < gf2k_axes[gf2k->id]; i++)
319 set_bit(gf2k_abs[i], input_dev->absbit); 319 set_bit(gf2k_abs[i], input_dev->absbit);
320 320
321 for (i = 0; i < gf2k_hats[gf2k->id]; i++) { 321 for (i = 0; i < gf2k_hats[gf2k->id]; i++)
322 set_bit(ABS_HAT0X + i, input_dev->absbit); 322 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
323 input_dev->absmin[ABS_HAT0X + i] = -1;
324 input_dev->absmax[ABS_HAT0X + i] = 1;
325 }
326 323
327 for (i = 0; i < gf2k_joys[gf2k->id]; i++) 324 for (i = 0; i < gf2k_joys[gf2k->id]; i++)
328 set_bit(gf2k_btn_joy[i], input_dev->keybit); 325 set_bit(gf2k_btn_joy[i], input_dev->keybit);
@@ -334,11 +331,14 @@ static int gf2k_connect(struct gameport *gameport, struct gameport_driver *drv)
334 gf2k_read(gf2k, data); 331 gf2k_read(gf2k, data);
335 332
336 for (i = 0; i < gf2k_axes[gf2k->id]; i++) { 333 for (i = 0; i < gf2k_axes[gf2k->id]; i++) {
337 input_dev->absmax[gf2k_abs[i]] = (i < 2) ? input_dev->abs[gf2k_abs[i]] * 2 - 32 : 334 int max = i < 2 ?
338 input_dev->abs[gf2k_abs[0]] + input_dev->abs[gf2k_abs[1]] - 32; 335 input_abs_get_val(input_dev, gf2k_abs[i]) * 2 :
339 input_dev->absmin[gf2k_abs[i]] = 32; 336 input_abs_get_val(input_dev, gf2k_abs[0]) +
340 input_dev->absfuzz[gf2k_abs[i]] = 8; 337 input_abs_get_val(input_dev, gf2k_abs[1]);
341 input_dev->absflat[gf2k_abs[i]] = (i < 2) ? 24 : 0; 338 int flat = i < 2 ? 24 : 0;
339
340 input_set_abs_params(input_dev, gf2k_abs[i],
341 32, max - 32, 8, flat);
342 } 342 }
343 343
344 err = input_register_device(gf2k->dev); 344 err = input_register_device(gf2k->dev);
diff --git a/drivers/input/joystick/interact.c b/drivers/input/joystick/interact.c
index 2478289aeee..16fb19d1ca2 100644
--- a/drivers/input/joystick/interact.c
+++ b/drivers/input/joystick/interact.c
@@ -270,18 +270,14 @@ static int interact_connect(struct gameport *gameport, struct gameport_driver *d
270 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 270 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
271 271
272 for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) { 272 for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
273 set_bit(t, input_dev->absbit); 273 if (i < interact_type[interact->type].b8)
274 if (i < interact_type[interact->type].b8) { 274 input_set_abs_params(input_dev, t, 0, 255, 0, 0);
275 input_dev->absmin[t] = 0; 275 else
276 input_dev->absmax[t] = 255; 276 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
277 } else {
278 input_dev->absmin[t] = -1;
279 input_dev->absmax[t] = 1;
280 }
281 } 277 }
282 278
283 for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++) 279 for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
284 set_bit(t, input_dev->keybit); 280 __set_bit(t, input_dev->keybit);
285 281
286 err = input_register_device(interact->dev); 282 err = input_register_device(interact->dev);
287 if (err) 283 if (err)
diff --git a/drivers/input/joystick/sidewinder.c b/drivers/input/joystick/sidewinder.c
index ca13a6bec33..b8d86115644 100644
--- a/drivers/input/joystick/sidewinder.c
+++ b/drivers/input/joystick/sidewinder.c
@@ -761,17 +761,21 @@ static int sw_connect(struct gameport *gameport, struct gameport_driver *drv)
761 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 761 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
762 762
763 for (j = 0; (bits = sw_bit[sw->type][j]); j++) { 763 for (j = 0; (bits = sw_bit[sw->type][j]); j++) {
764 int min, max, fuzz, flat;
765
764 code = sw_abs[sw->type][j]; 766 code = sw_abs[sw->type][j];
765 set_bit(code, input_dev->absbit); 767 min = bits == 1 ? -1 : 0;
766 input_dev->absmax[code] = (1 << bits) - 1; 768 max = (1 << bits) - 1;
767 input_dev->absmin[code] = (bits == 1) ? -1 : 0; 769 fuzz = (bits >> 1) >= 2 ? 1 << ((bits >> 1) - 2) : 0;
768 input_dev->absfuzz[code] = ((bits >> 1) >= 2) ? (1 << ((bits >> 1) - 2)) : 0; 770 flat = code == ABS_THROTTLE || bits < 5 ?
769 if (code != ABS_THROTTLE) 771 0 : 1 << (bits - 5);
770 input_dev->absflat[code] = (bits >= 5) ? (1 << (bits - 5)) : 0; 772
773 input_set_abs_params(input_dev, code,
774 min, max, fuzz, flat);
771 } 775 }
772 776
773 for (j = 0; (code = sw_btn[sw->type][j]); j++) 777 for (j = 0; (code = sw_btn[sw->type][j]); j++)
774 set_bit(code, input_dev->keybit); 778 __set_bit(code, input_dev->keybit);
775 779
776 dbg("%s%s [%d-bit id %d data %d]\n", sw->name, comment, m, l, k); 780 dbg("%s%s [%d-bit id %d data %d]\n", sw->name, comment, m, l, k);
777 781
diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
index 269a846f369..f9fb7fa10af 100644
--- a/drivers/input/joystick/xpad.c
+++ b/drivers/input/joystick/xpad.c
@@ -148,6 +148,7 @@ static const struct xpad_device {
148 { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX }, 148 { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
149 { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX }, 149 { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
150 { 0x0e6f, 0x0006, "Pelican 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 }, 150 { 0x0e6f, 0x0006, "Pelican 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
151 { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
151 { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX }, 152 { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
152 { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX }, 153 { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
153 { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX }, 154 { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index a9fd147f2ba..6069abe31e4 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -39,6 +39,8 @@ struct gpio_keys_drvdata {
39 struct input_dev *input; 39 struct input_dev *input;
40 struct mutex disable_lock; 40 struct mutex disable_lock;
41 unsigned int n_buttons; 41 unsigned int n_buttons;
42 int (*enable)(struct device *dev);
43 void (*disable)(struct device *dev);
42 struct gpio_button_data data[0]; 44 struct gpio_button_data data[0];
43}; 45};
44 46
@@ -423,6 +425,21 @@ fail2:
423 return error; 425 return error;
424} 426}
425 427
428static int gpio_keys_open(struct input_dev *input)
429{
430 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
431
432 return ddata->enable ? ddata->enable(input->dev.parent) : 0;
433}
434
435static void gpio_keys_close(struct input_dev *input)
436{
437 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
438
439 if (ddata->disable)
440 ddata->disable(input->dev.parent);
441}
442
426static int __devinit gpio_keys_probe(struct platform_device *pdev) 443static int __devinit gpio_keys_probe(struct platform_device *pdev)
427{ 444{
428 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 445 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
@@ -444,13 +461,18 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
444 461
445 ddata->input = input; 462 ddata->input = input;
446 ddata->n_buttons = pdata->nbuttons; 463 ddata->n_buttons = pdata->nbuttons;
464 ddata->enable = pdata->enable;
465 ddata->disable = pdata->disable;
447 mutex_init(&ddata->disable_lock); 466 mutex_init(&ddata->disable_lock);
448 467
449 platform_set_drvdata(pdev, ddata); 468 platform_set_drvdata(pdev, ddata);
469 input_set_drvdata(input, ddata);
450 470
451 input->name = pdev->name; 471 input->name = pdev->name;
452 input->phys = "gpio-keys/input0"; 472 input->phys = "gpio-keys/input0";
453 input->dev.parent = &pdev->dev; 473 input->dev.parent = &pdev->dev;
474 input->open = gpio_keys_open;
475 input->close = gpio_keys_close;
454 476
455 input->id.bustype = BUS_HOST; 477 input->id.bustype = BUS_HOST;
456 input->id.vendor = 0x0001; 478 input->id.vendor = 0x0001;
diff --git a/drivers/input/keyboard/hil_kbd.c b/drivers/input/keyboard/hil_kbd.c
index c83f4b2ec7d..ddd5afd301d 100644
--- a/drivers/input/keyboard/hil_kbd.c
+++ b/drivers/input/keyboard/hil_kbd.c
@@ -232,15 +232,16 @@ static void hil_dev_handle_ptr_events(struct hil_dev *ptr)
232 if (absdev) { 232 if (absdev) {
233 val = lo + (hi << 8); 233 val = lo + (hi << 8);
234#ifdef TABLET_AUTOADJUST 234#ifdef TABLET_AUTOADJUST
235 if (val < dev->absmin[ABS_X + i]) 235 if (val < input_abs_min(dev, ABS_X + i))
236 dev->absmin[ABS_X + i] = val; 236 input_abs_set_min(dev, ABS_X + i, val);
237 if (val > dev->absmax[ABS_X + i]) 237 if (val > input_abs_max(dev, ABS_X + i))
238 dev->absmax[ABS_X + i] = val; 238 XXinput_abs_set_max(dev, ABS_X + i, val);
239#endif 239#endif
240 if (i%3) val = dev->absmax[ABS_X + i] - val; 240 if (i % 3)
241 val = input_abs_max(dev, ABS_X + i) - val;
241 input_report_abs(dev, ABS_X + i, val); 242 input_report_abs(dev, ABS_X + i, val);
242 } else { 243 } else {
243 val = (int) (((int8_t)lo) | ((int8_t)hi << 8)); 244 val = (int) (((int8_t) lo) | ((int8_t) hi << 8));
244 if (i % 3) 245 if (i % 3)
245 val *= -1; 246 val *= -1;
246 input_report_rel(dev, REL_X + i, val); 247 input_report_rel(dev, REL_X + i, val);
@@ -387,9 +388,11 @@ static void hil_dev_pointer_setup(struct hil_dev *ptr)
387 388
388#ifdef TABLET_AUTOADJUST 389#ifdef TABLET_AUTOADJUST
389 for (i = 0; i < ABS_MAX; i++) { 390 for (i = 0; i < ABS_MAX; i++) {
390 int diff = input_dev->absmax[ABS_X + i] / 10; 391 int diff = input_abs_max(input_dev, ABS_X + i) / 10;
391 input_dev->absmin[ABS_X + i] += diff; 392 input_abs_set_min(input_dev, ABS_X + i,
392 input_dev->absmax[ABS_X + i] -= diff; 393 input_abs_min(input_dev, ABS_X + i) + diff)
394 XXinput_abs_set_max(input_dev, ABS_X + i,
395 input_abs_max(input_dev, ABS_X + i) - diff)
393 } 396 }
394#endif 397#endif
395 398
diff --git a/drivers/input/misc/adxl34x.c b/drivers/input/misc/adxl34x.c
index e2ca0170808..de5900d5078 100644
--- a/drivers/input/misc/adxl34x.c
+++ b/drivers/input/misc/adxl34x.c
@@ -724,7 +724,6 @@ struct adxl34x *adxl34x_probe(struct device *dev, int irq,
724 pdata = &ac->pdata; 724 pdata = &ac->pdata;
725 725
726 ac->input = input_dev; 726 ac->input = input_dev;
727 ac->disabled = true;
728 ac->dev = dev; 727 ac->dev = dev;
729 ac->irq = irq; 728 ac->irq = irq;
730 ac->bops = bops; 729 ac->bops = bops;
diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c
index b71eb55f2db..bb53fd33cd1 100644
--- a/drivers/input/misc/uinput.c
+++ b/drivers/input/misc/uinput.c
@@ -304,21 +304,25 @@ static int uinput_validate_absbits(struct input_dev *dev)
304 if (!test_bit(cnt, dev->absbit)) 304 if (!test_bit(cnt, dev->absbit))
305 continue; 305 continue;
306 306
307 if ((dev->absmax[cnt] <= dev->absmin[cnt])) { 307 if (input_abs_get_max(dev, cnt) <= input_abs_get_min(dev, cnt)) {
308 printk(KERN_DEBUG 308 printk(KERN_DEBUG
309 "%s: invalid abs[%02x] min:%d max:%d\n", 309 "%s: invalid abs[%02x] min:%d max:%d\n",
310 UINPUT_NAME, cnt, 310 UINPUT_NAME, cnt,
311 dev->absmin[cnt], dev->absmax[cnt]); 311 input_abs_get_min(dev, cnt),
312 input_abs_get_max(dev, cnt));
312 retval = -EINVAL; 313 retval = -EINVAL;
313 break; 314 break;
314 } 315 }
315 316
316 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) { 317 if (input_abs_get_flat(dev, cnt) >
318 input_abs_get_max(dev, cnt) - input_abs_get_min(dev, cnt)) {
317 printk(KERN_DEBUG 319 printk(KERN_DEBUG
318 "%s: absflat[%02x] out of range: %d " 320 "%s: abs_flat #%02x out of range: %d "
319 "(min:%d/max:%d)\n", 321 "(min:%d/max:%d)\n",
320 UINPUT_NAME, cnt, dev->absflat[cnt], 322 UINPUT_NAME, cnt,
321 dev->absmin[cnt], dev->absmax[cnt]); 323 input_abs_get_flat(dev, cnt),
324 input_abs_get_min(dev, cnt),
325 input_abs_get_max(dev, cnt));
322 retval = -EINVAL; 326 retval = -EINVAL;
323 break; 327 break;
324 } 328 }
@@ -343,7 +347,7 @@ static int uinput_setup_device(struct uinput_device *udev, const char __user *bu
343 struct uinput_user_dev *user_dev; 347 struct uinput_user_dev *user_dev;
344 struct input_dev *dev; 348 struct input_dev *dev;
345 char *name; 349 char *name;
346 int size; 350 int i, size;
347 int retval; 351 int retval;
348 352
349 if (count != sizeof(struct uinput_user_dev)) 353 if (count != sizeof(struct uinput_user_dev))
@@ -387,11 +391,12 @@ static int uinput_setup_device(struct uinput_device *udev, const char __user *bu
387 dev->id.product = user_dev->id.product; 391 dev->id.product = user_dev->id.product;
388 dev->id.version = user_dev->id.version; 392 dev->id.version = user_dev->id.version;
389 393
390 size = sizeof(int) * ABS_CNT; 394 for (i = 0; i < ABS_CNT; i++) {
391 memcpy(dev->absmax, user_dev->absmax, size); 395 input_abs_set_max(dev, i, user_dev->absmax[i]);
392 memcpy(dev->absmin, user_dev->absmin, size); 396 input_abs_set_min(dev, i, user_dev->absmin[i]);
393 memcpy(dev->absfuzz, user_dev->absfuzz, size); 397 input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
394 memcpy(dev->absflat, user_dev->absflat, size); 398 input_abs_set_flat(dev, i, user_dev->absflat[i]);
399 }
395 400
396 /* check if absmin/absmax/absfuzz/absflat are filled as 401 /* check if absmin/absmax/absfuzz/absflat are filled as
397 * told in Documentation/input/input-programming.txt */ 402 * told in Documentation/input/input-programming.txt */
diff --git a/drivers/input/mouse/elantech.c b/drivers/input/mouse/elantech.c
index b18862b2a70..48311204ba5 100644
--- a/drivers/input/mouse/elantech.c
+++ b/drivers/input/mouse/elantech.c
@@ -185,7 +185,6 @@ static void elantech_report_absolute_v1(struct psmouse *psmouse)
185 struct elantech_data *etd = psmouse->private; 185 struct elantech_data *etd = psmouse->private;
186 unsigned char *packet = psmouse->packet; 186 unsigned char *packet = psmouse->packet;
187 int fingers; 187 int fingers;
188 static int old_fingers;
189 188
190 if (etd->fw_version < 0x020000) { 189 if (etd->fw_version < 0x020000) {
191 /* 190 /*
@@ -203,10 +202,13 @@ static void elantech_report_absolute_v1(struct psmouse *psmouse)
203 } 202 }
204 203
205 if (etd->jumpy_cursor) { 204 if (etd->jumpy_cursor) {
206 /* Discard packets that are likely to have bogus coordinates */ 205 if (fingers != 1) {
207 if (fingers > old_fingers) { 206 etd->single_finger_reports = 0;
207 } else if (etd->single_finger_reports < 2) {
208 /* Discard first 2 reports of one finger, bogus */
209 etd->single_finger_reports++;
208 elantech_debug("discarding packet\n"); 210 elantech_debug("discarding packet\n");
209 goto discard_packet_v1; 211 return;
210 } 212 }
211 } 213 }
212 214
@@ -238,9 +240,6 @@ static void elantech_report_absolute_v1(struct psmouse *psmouse)
238 } 240 }
239 241
240 input_sync(dev); 242 input_sync(dev);
241
242 discard_packet_v1:
243 old_fingers = fingers;
244} 243}
245 244
246/* 245/*
@@ -258,6 +257,14 @@ static void elantech_report_absolute_v2(struct psmouse *psmouse)
258 input_report_key(dev, BTN_TOUCH, fingers != 0); 257 input_report_key(dev, BTN_TOUCH, fingers != 0);
259 258
260 switch (fingers) { 259 switch (fingers) {
260 case 3:
261 /*
262 * Same as one finger, except report of more than 3 fingers:
263 * byte 3: n4 . w1 w0 . . . .
264 */
265 if (packet[3] & 0x80)
266 fingers = 4;
267 /* pass through... */
261 case 1: 268 case 1:
262 /* 269 /*
263 * byte 1: . . . . . x10 x9 x8 270 * byte 1: . . . . . x10 x9 x8
@@ -310,6 +317,7 @@ static void elantech_report_absolute_v2(struct psmouse *psmouse)
310 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1); 317 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
311 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2); 318 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
312 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3); 319 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
320 input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
313 input_report_key(dev, BTN_LEFT, packet[0] & 0x01); 321 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
314 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02); 322 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
315 323
@@ -467,6 +475,7 @@ static void elantech_set_input_params(struct psmouse *psmouse)
467 break; 475 break;
468 476
469 case 2: 477 case 2:
478 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
470 input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0); 479 input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
471 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0); 480 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
472 input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0); 481 input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
@@ -733,13 +742,13 @@ int elantech_init(struct psmouse *psmouse)
733 etd->capabilities = param[0]; 742 etd->capabilities = param[0];
734 743
735 /* 744 /*
736 * This firmware seems to suffer from misreporting coordinates when 745 * This firmware suffers from misreporting coordinates when
737 * a touch action starts causing the mouse cursor or scrolled page 746 * a touch action starts causing the mouse cursor or scrolled page
738 * to jump. Enable a workaround. 747 * to jump. Enable a workaround.
739 */ 748 */
740 if (etd->fw_version == 0x020022) { 749 if (etd->fw_version == 0x020022 || etd->fw_version == 0x020600) {
741 pr_info("firmware version 2.0.34 detected, enabling jumpy cursor workaround\n"); 750 pr_info("firmware version 2.0.34/2.6.0 detected, enabling jumpy cursor workaround\n");
742 etd->jumpy_cursor = 1; 751 etd->jumpy_cursor = true;
743 } 752 }
744 753
745 if (elantech_set_absolute_mode(psmouse)) { 754 if (elantech_set_absolute_mode(psmouse)) {
diff --git a/drivers/input/mouse/elantech.h b/drivers/input/mouse/elantech.h
index ac57bde1bb9..aa4aac5d219 100644
--- a/drivers/input/mouse/elantech.h
+++ b/drivers/input/mouse/elantech.h
@@ -100,10 +100,11 @@ struct elantech_data {
100 unsigned char reg_26; 100 unsigned char reg_26;
101 unsigned char debug; 101 unsigned char debug;
102 unsigned char capabilities; 102 unsigned char capabilities;
103 unsigned char paritycheck; 103 bool paritycheck;
104 unsigned char jumpy_cursor; 104 bool jumpy_cursor;
105 unsigned char hw_version; 105 unsigned char hw_version;
106 unsigned int fw_version; 106 unsigned int fw_version;
107 unsigned int single_finger_reports;
107 unsigned char parity[256]; 108 unsigned char parity[256];
108}; 109};
109 110
diff --git a/drivers/input/mouse/pc110pad.c b/drivers/input/mouse/pc110pad.c
index 3941f97cfa6..7b02b652e26 100644
--- a/drivers/input/mouse/pc110pad.c
+++ b/drivers/input/mouse/pc110pad.c
@@ -145,8 +145,8 @@ static int __init pc110pad_init(void)
145 pc110pad_dev->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y); 145 pc110pad_dev->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y);
146 pc110pad_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); 146 pc110pad_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
147 147
148 pc110pad_dev->absmax[ABS_X] = 0x1ff; 148 input_abs_set_max(pc110pad_dev, ABS_X, 0x1ff);
149 pc110pad_dev->absmax[ABS_Y] = 0x0ff; 149 input_abs_set_max(pc110pad_dev, ABS_Y, 0x0ff);
150 150
151 pc110pad_dev->open = pc110pad_open; 151 pc110pad_dev->open = pc110pad_open;
152 pc110pad_dev->close = pc110pad_close; 152 pc110pad_dev->close = pc110pad_close;
diff --git a/drivers/input/mouse/synaptics.c b/drivers/input/mouse/synaptics.c
index 8c324403b9f..96b70a43515 100644
--- a/drivers/input/mouse/synaptics.c
+++ b/drivers/input/mouse/synaptics.c
@@ -635,8 +635,8 @@ static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
635 __clear_bit(REL_X, dev->relbit); 635 __clear_bit(REL_X, dev->relbit);
636 __clear_bit(REL_Y, dev->relbit); 636 __clear_bit(REL_Y, dev->relbit);
637 637
638 dev->absres[ABS_X] = priv->x_res; 638 input_abs_set_res(dev, ABS_X, priv->x_res);
639 dev->absres[ABS_Y] = priv->y_res; 639 input_abs_set_res(dev, ABS_Y, priv->y_res);
640 640
641 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) { 641 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
642 /* Clickpads report only left button */ 642 /* Clickpads report only left button */
diff --git a/drivers/input/mousedev.c b/drivers/input/mousedev.c
index d8f68f77007..83c24cca234 100644
--- a/drivers/input/mousedev.c
+++ b/drivers/input/mousedev.c
@@ -22,6 +22,7 @@
22#include <linux/random.h> 22#include <linux/random.h>
23#include <linux/major.h> 23#include <linux/major.h>
24#include <linux/device.h> 24#include <linux/device.h>
25#include <linux/kernel.h>
25#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX 26#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
26#include <linux/miscdevice.h> 27#include <linux/miscdevice.h>
27#endif 28#endif
@@ -134,11 +135,14 @@ static void mousedev_touchpad_event(struct input_dev *dev,
134 switch (code) { 135 switch (code) {
135 136
136 case ABS_X: 137 case ABS_X:
138
137 fx(0) = value; 139 fx(0) = value;
138 if (mousedev->touch && mousedev->pkt_count >= 2) { 140 if (mousedev->touch && mousedev->pkt_count >= 2) {
139 size = dev->absmax[ABS_X] - dev->absmin[ABS_X]; 141 size = input_abs_get_min(dev, ABS_X) -
142 input_abs_get_max(dev, ABS_X);
140 if (size == 0) 143 if (size == 0)
141 size = 256 * 2; 144 size = 256 * 2;
145
142 tmp = ((value - fx(2)) * 256 * FRACTION_DENOM) / size; 146 tmp = ((value - fx(2)) * 256 * FRACTION_DENOM) / size;
143 tmp += mousedev->frac_dx; 147 tmp += mousedev->frac_dx;
144 mousedev->packet.dx = tmp / FRACTION_DENOM; 148 mousedev->packet.dx = tmp / FRACTION_DENOM;
@@ -150,10 +154,12 @@ static void mousedev_touchpad_event(struct input_dev *dev,
150 case ABS_Y: 154 case ABS_Y:
151 fy(0) = value; 155 fy(0) = value;
152 if (mousedev->touch && mousedev->pkt_count >= 2) { 156 if (mousedev->touch && mousedev->pkt_count >= 2) {
153 /* use X size to keep the same scale */ 157 /* use X size for ABS_Y to keep the same scale */
154 size = dev->absmax[ABS_X] - dev->absmin[ABS_X]; 158 size = input_abs_get_min(dev, ABS_X) -
159 input_abs_get_max(dev, ABS_X);
155 if (size == 0) 160 if (size == 0)
156 size = 256 * 2; 161 size = 256 * 2;
162
157 tmp = -((value - fy(2)) * 256 * FRACTION_DENOM) / size; 163 tmp = -((value - fy(2)) * 256 * FRACTION_DENOM) / size;
158 tmp += mousedev->frac_dy; 164 tmp += mousedev->frac_dy;
159 mousedev->packet.dy = tmp / FRACTION_DENOM; 165 mousedev->packet.dy = tmp / FRACTION_DENOM;
@@ -167,33 +173,35 @@ static void mousedev_touchpad_event(struct input_dev *dev,
167static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, 173static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev,
168 unsigned int code, int value) 174 unsigned int code, int value)
169{ 175{
170 int size; 176 int min, max, size;
171 177
172 switch (code) { 178 switch (code) {
173 179
174 case ABS_X: 180 case ABS_X:
175 size = dev->absmax[ABS_X] - dev->absmin[ABS_X]; 181 min = input_abs_get_min(dev, ABS_X);
182 max = input_abs_get_max(dev, ABS_X);
183
184 size = max - min;
176 if (size == 0) 185 if (size == 0)
177 size = xres ? : 1; 186 size = xres ? : 1;
178 if (value > dev->absmax[ABS_X]) 187
179 value = dev->absmax[ABS_X]; 188 clamp(value, min, max);
180 if (value < dev->absmin[ABS_X]) 189
181 value = dev->absmin[ABS_X]; 190 mousedev->packet.x = ((value - min) * xres) / size;
182 mousedev->packet.x =
183 ((value - dev->absmin[ABS_X]) * xres) / size;
184 mousedev->packet.abs_event = 1; 191 mousedev->packet.abs_event = 1;
185 break; 192 break;
186 193
187 case ABS_Y: 194 case ABS_Y:
188 size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y]; 195 min = input_abs_get_min(dev, ABS_Y);
196 max = input_abs_get_max(dev, ABS_Y);
197
198 size = max - min;
189 if (size == 0) 199 if (size == 0)
190 size = yres ? : 1; 200 size = yres ? : 1;
191 if (value > dev->absmax[ABS_Y]) 201
192 value = dev->absmax[ABS_Y]; 202 clamp(value, min, max);
193 if (value < dev->absmin[ABS_Y]) 203
194 value = dev->absmin[ABS_Y]; 204 mousedev->packet.y = yres - ((value - min) * yres) / size;
195 mousedev->packet.y = yres -
196 ((value - dev->absmin[ABS_Y]) * yres) / size;
197 mousedev->packet.abs_event = 1; 205 mousedev->packet.abs_event = 1;
198 break; 206 break;
199 } 207 }
diff --git a/drivers/input/tablet/aiptek.c b/drivers/input/tablet/aiptek.c
index 51b80b08d46..57b25b84d1f 100644
--- a/drivers/input/tablet/aiptek.c
+++ b/drivers/input/tablet/aiptek.c
@@ -987,20 +987,17 @@ static int aiptek_program_tablet(struct aiptek *aiptek)
987 /* Query getXextension */ 987 /* Query getXextension */
988 if ((ret = aiptek_query(aiptek, 0x01, 0x00)) < 0) 988 if ((ret = aiptek_query(aiptek, 0x01, 0x00)) < 0)
989 return ret; 989 return ret;
990 aiptek->inputdev->absmin[ABS_X] = 0; 990 input_set_abs_params(aiptek->inputdev, ABS_X, 0, ret - 1, 0, 0);
991 aiptek->inputdev->absmax[ABS_X] = ret - 1;
992 991
993 /* Query getYextension */ 992 /* Query getYextension */
994 if ((ret = aiptek_query(aiptek, 0x01, 0x01)) < 0) 993 if ((ret = aiptek_query(aiptek, 0x01, 0x01)) < 0)
995 return ret; 994 return ret;
996 aiptek->inputdev->absmin[ABS_Y] = 0; 995 input_set_abs_params(aiptek->inputdev, ABS_Y, 0, ret - 1, 0, 0);
997 aiptek->inputdev->absmax[ABS_Y] = ret - 1;
998 996
999 /* Query getPressureLevels */ 997 /* Query getPressureLevels */
1000 if ((ret = aiptek_query(aiptek, 0x08, 0x00)) < 0) 998 if ((ret = aiptek_query(aiptek, 0x08, 0x00)) < 0)
1001 return ret; 999 return ret;
1002 aiptek->inputdev->absmin[ABS_PRESSURE] = 0; 1000 input_set_abs_params(aiptek->inputdev, ABS_PRESSURE, 0, ret - 1, 0, 0);
1003 aiptek->inputdev->absmax[ABS_PRESSURE] = ret - 1;
1004 1001
1005 /* Depending on whether we are in absolute or relative mode, we will 1002 /* Depending on whether we are in absolute or relative mode, we will
1006 * do a switchToTablet(absolute) or switchToMouse(relative) command. 1003 * do a switchToTablet(absolute) or switchToMouse(relative) command.
@@ -1054,8 +1051,8 @@ static ssize_t show_tabletSize(struct device *dev, struct device_attribute *attr
1054 struct aiptek *aiptek = dev_get_drvdata(dev); 1051 struct aiptek *aiptek = dev_get_drvdata(dev);
1055 1052
1056 return snprintf(buf, PAGE_SIZE, "%dx%d\n", 1053 return snprintf(buf, PAGE_SIZE, "%dx%d\n",
1057 aiptek->inputdev->absmax[ABS_X] + 1, 1054 input_abs_get_max(aiptek->inputdev, ABS_X) + 1,
1058 aiptek->inputdev->absmax[ABS_Y] + 1); 1055 input_abs_get_max(aiptek->inputdev, ABS_Y) + 1);
1059} 1056}
1060 1057
1061/* These structs define the sysfs files, param #1 is the name of the 1058/* These structs define the sysfs files, param #1 is the name of the
@@ -1843,7 +1840,7 @@ aiptek_probe(struct usb_interface *intf, const struct usb_device_id *id)
1843 for (i = 0; i < ARRAY_SIZE(speeds); ++i) { 1840 for (i = 0; i < ARRAY_SIZE(speeds); ++i) {
1844 aiptek->curSetting.programmableDelay = speeds[i]; 1841 aiptek->curSetting.programmableDelay = speeds[i];
1845 (void)aiptek_program_tablet(aiptek); 1842 (void)aiptek_program_tablet(aiptek);
1846 if (aiptek->inputdev->absmax[ABS_X] > 0) { 1843 if (input_abs_get_max(aiptek->inputdev, ABS_X) > 0) {
1847 dev_info(&intf->dev, 1844 dev_info(&intf->dev,
1848 "Aiptek using %d ms programming speed\n", 1845 "Aiptek using %d ms programming speed\n",
1849 aiptek->curSetting.programmableDelay); 1846 aiptek->curSetting.programmableDelay);
diff --git a/drivers/input/tablet/wacom_wac.c b/drivers/input/tablet/wacom_wac.c
index ce0b4608dad..40d77ba8fdc 100644
--- a/drivers/input/tablet/wacom_wac.c
+++ b/drivers/input/tablet/wacom_wac.c
@@ -687,10 +687,10 @@ static void wacom_tpc_finger_in(struct wacom_wac *wacom, char *data, int idx)
687 * protocol. 687 * protocol.
688 */ 688 */
689 if (wacom->last_finger != finger) { 689 if (wacom->last_finger != finger) {
690 if (x == input->abs[ABS_X]) 690 if (x == input_abs_get_val(input, ABS_X))
691 x++; 691 x++;
692 692
693 if (y == input->abs[ABS_Y]) 693 if (y == input_abs_get_val(input, ABS_Y))
694 y++; 694 y++;
695 } 695 }
696 696
diff --git a/drivers/input/touchscreen/cy8ctmg110_ts.c b/drivers/input/touchscreen/cy8ctmg110_ts.c
index 4eb7df0b7f8..5ec0946938f 100644
--- a/drivers/input/touchscreen/cy8ctmg110_ts.c
+++ b/drivers/input/touchscreen/cy8ctmg110_ts.c
@@ -75,7 +75,7 @@ static int cy8ctmg110_write_regs(struct cy8ctmg110 *tsc, unsigned char reg,
75 unsigned char len, unsigned char *value) 75 unsigned char len, unsigned char *value)
76{ 76{
77 struct i2c_client *client = tsc->client; 77 struct i2c_client *client = tsc->client;
78 unsigned int ret; 78 int ret;
79 unsigned char i2c_data[6]; 79 unsigned char i2c_data[6];
80 80
81 BUG_ON(len > 5); 81 BUG_ON(len > 5);
@@ -86,7 +86,7 @@ static int cy8ctmg110_write_regs(struct cy8ctmg110 *tsc, unsigned char reg,
86 ret = i2c_master_send(client, i2c_data, len + 1); 86 ret = i2c_master_send(client, i2c_data, len + 1);
87 if (ret != 1) { 87 if (ret != 1) {
88 dev_err(&client->dev, "i2c write data cmd failed\n"); 88 dev_err(&client->dev, "i2c write data cmd failed\n");
89 return ret; 89 return ret ? ret : -EIO;
90 } 90 }
91 91
92 return 0; 92 return 0;
@@ -96,7 +96,7 @@ static int cy8ctmg110_read_regs(struct cy8ctmg110 *tsc,
96 unsigned char *data, unsigned char len, unsigned char cmd) 96 unsigned char *data, unsigned char len, unsigned char cmd)
97{ 97{
98 struct i2c_client *client = tsc->client; 98 struct i2c_client *client = tsc->client;
99 unsigned int ret; 99 int ret;
100 struct i2c_msg msg[2] = { 100 struct i2c_msg msg[2] = {
101 /* first write slave position to i2c devices */ 101 /* first write slave position to i2c devices */
102 { client->addr, 0, 1, &cmd }, 102 { client->addr, 0, 1, &cmd },
diff --git a/drivers/md/Kconfig b/drivers/md/Kconfig
index 4a6feac8c94..bf1a95e3155 100644
--- a/drivers/md/Kconfig
+++ b/drivers/md/Kconfig
@@ -121,7 +121,7 @@ config MD_RAID10
121config MD_RAID456 121config MD_RAID456
122 tristate "RAID-4/RAID-5/RAID-6 mode" 122 tristate "RAID-4/RAID-5/RAID-6 mode"
123 depends on BLK_DEV_MD 123 depends on BLK_DEV_MD
124 select MD_RAID6_PQ 124 select RAID6_PQ
125 select ASYNC_MEMCPY 125 select ASYNC_MEMCPY
126 select ASYNC_XOR 126 select ASYNC_XOR
127 select ASYNC_PQ 127 select ASYNC_PQ
@@ -165,22 +165,6 @@ config MULTICORE_RAID456
165 165
166 If unsure, say N. 166 If unsure, say N.
167 167
168config MD_RAID6_PQ
169 tristate
170
171config ASYNC_RAID6_TEST
172 tristate "Self test for hardware accelerated raid6 recovery"
173 depends on MD_RAID6_PQ
174 select ASYNC_RAID6_RECOV
175 ---help---
176 This is a one-shot self test that permutes through the
177 recovery of all the possible two disk failure scenarios for a
178 N-disk array. Recovery is performed with the asynchronous
179 raid6 recovery routines, and will optionally use an offload
180 engine if one is available.
181
182 If unsure, say N.
183
184config MD_MULTIPATH 168config MD_MULTIPATH
185 tristate "Multipath I/O support" 169 tristate "Multipath I/O support"
186 depends on BLK_DEV_MD 170 depends on BLK_DEV_MD
diff --git a/drivers/md/Makefile b/drivers/md/Makefile
index e355e7f6a53..5e3aac41919 100644
--- a/drivers/md/Makefile
+++ b/drivers/md/Makefile
@@ -12,13 +12,6 @@ dm-log-userspace-y \
12 += dm-log-userspace-base.o dm-log-userspace-transfer.o 12 += dm-log-userspace-base.o dm-log-userspace-transfer.o
13md-mod-y += md.o bitmap.o 13md-mod-y += md.o bitmap.o
14raid456-y += raid5.o 14raid456-y += raid5.o
15raid6_pq-y += raid6algos.o raid6recov.o raid6tables.o \
16 raid6int1.o raid6int2.o raid6int4.o \
17 raid6int8.o raid6int16.o raid6int32.o \
18 raid6altivec1.o raid6altivec2.o raid6altivec4.o \
19 raid6altivec8.o \
20 raid6mmx.o raid6sse1.o raid6sse2.o
21hostprogs-y += mktables
22 15
23# Note: link order is important. All raid personalities 16# Note: link order is important. All raid personalities
24# and must come before md.o, as they each initialise 17# and must come before md.o, as they each initialise
@@ -29,7 +22,6 @@ obj-$(CONFIG_MD_LINEAR) += linear.o
29obj-$(CONFIG_MD_RAID0) += raid0.o 22obj-$(CONFIG_MD_RAID0) += raid0.o
30obj-$(CONFIG_MD_RAID1) += raid1.o 23obj-$(CONFIG_MD_RAID1) += raid1.o
31obj-$(CONFIG_MD_RAID10) += raid10.o 24obj-$(CONFIG_MD_RAID10) += raid10.o
32obj-$(CONFIG_MD_RAID6_PQ) += raid6_pq.o
33obj-$(CONFIG_MD_RAID456) += raid456.o 25obj-$(CONFIG_MD_RAID456) += raid456.o
34obj-$(CONFIG_MD_MULTIPATH) += multipath.o 26obj-$(CONFIG_MD_MULTIPATH) += multipath.o
35obj-$(CONFIG_MD_FAULTY) += faulty.o 27obj-$(CONFIG_MD_FAULTY) += faulty.o
@@ -45,75 +37,6 @@ obj-$(CONFIG_DM_MIRROR) += dm-mirror.o dm-log.o dm-region-hash.o
45obj-$(CONFIG_DM_LOG_USERSPACE) += dm-log-userspace.o 37obj-$(CONFIG_DM_LOG_USERSPACE) += dm-log-userspace.o
46obj-$(CONFIG_DM_ZERO) += dm-zero.o 38obj-$(CONFIG_DM_ZERO) += dm-zero.o
47 39
48quiet_cmd_unroll = UNROLL $@
49 cmd_unroll = $(AWK) -f$(srctree)/$(src)/unroll.awk -vN=$(UNROLL) \
50 < $< > $@ || ( rm -f $@ && exit 1 )
51
52ifeq ($(CONFIG_ALTIVEC),y)
53altivec_flags := -maltivec -mabi=altivec
54endif
55
56ifeq ($(CONFIG_DM_UEVENT),y) 40ifeq ($(CONFIG_DM_UEVENT),y)
57dm-mod-objs += dm-uevent.o 41dm-mod-objs += dm-uevent.o
58endif 42endif
59
60targets += raid6int1.c
61$(obj)/raid6int1.c: UNROLL := 1
62$(obj)/raid6int1.c: $(src)/raid6int.uc $(src)/unroll.awk FORCE
63 $(call if_changed,unroll)
64
65targets += raid6int2.c
66$(obj)/raid6int2.c: UNROLL := 2
67$(obj)/raid6int2.c: $(src)/raid6int.uc $(src)/unroll.awk FORCE
68 $(call if_changed,unroll)
69
70targets += raid6int4.c
71$(obj)/raid6int4.c: UNROLL := 4
72$(obj)/raid6int4.c: $(src)/raid6int.uc $(src)/unroll.awk FORCE
73 $(call if_changed,unroll)
74
75targets += raid6int8.c
76$(obj)/raid6int8.c: UNROLL := 8
77$(obj)/raid6int8.c: $(src)/raid6int.uc $(src)/unroll.awk FORCE
78 $(call if_changed,unroll)
79
80targets += raid6int16.c
81$(obj)/raid6int16.c: UNROLL := 16
82$(obj)/raid6int16.c: $(src)/raid6int.uc $(src)/unroll.awk FORCE
83 $(call if_changed,unroll)
84
85targets += raid6int32.c
86$(obj)/raid6int32.c: UNROLL := 32
87$(obj)/raid6int32.c: $(src)/raid6int.uc $(src)/unroll.awk FORCE
88 $(call if_changed,unroll)
89
90CFLAGS_raid6altivec1.o += $(altivec_flags)
91targets += raid6altivec1.c
92$(obj)/raid6altivec1.c: UNROLL := 1
93$(obj)/raid6altivec1.c: $(src)/raid6altivec.uc $(src)/unroll.awk FORCE
94 $(call if_changed,unroll)
95
96CFLAGS_raid6altivec2.o += $(altivec_flags)
97targets += raid6altivec2.c
98$(obj)/raid6altivec2.c: UNROLL := 2
99$(obj)/raid6altivec2.c: $(src)/raid6altivec.uc $(src)/unroll.awk FORCE
100 $(call if_changed,unroll)
101
102CFLAGS_raid6altivec4.o += $(altivec_flags)
103targets += raid6altivec4.c
104$(obj)/raid6altivec4.c: UNROLL := 4
105$(obj)/raid6altivec4.c: $(src)/raid6altivec.uc $(src)/unroll.awk FORCE
106 $(call if_changed,unroll)
107
108CFLAGS_raid6altivec8.o += $(altivec_flags)
109targets += raid6altivec8.c
110$(obj)/raid6altivec8.c: UNROLL := 8
111$(obj)/raid6altivec8.c: $(src)/raid6altivec.uc $(src)/unroll.awk FORCE
112 $(call if_changed,unroll)
113
114quiet_cmd_mktable = TABLE $@
115 cmd_mktable = $(obj)/mktables > $@ || ( rm -f $@ && exit 1 )
116
117targets += raid6tables.c
118$(obj)/raid6tables.c: $(obj)/mktables FORCE
119 $(call if_changed,mktable)
diff --git a/drivers/md/bitmap.c b/drivers/md/bitmap.c
index 1742435ce3a..1ba1e122e94 100644
--- a/drivers/md/bitmap.c
+++ b/drivers/md/bitmap.c
@@ -13,7 +13,6 @@
13 * Still to do: 13 * Still to do:
14 * 14 *
15 * flush after percent set rather than just time based. (maybe both). 15 * flush after percent set rather than just time based. (maybe both).
16 * wait if count gets too high, wake when it drops to half.
17 */ 16 */
18 17
19#include <linux/blkdev.h> 18#include <linux/blkdev.h>
@@ -30,6 +29,7 @@
30#include "md.h" 29#include "md.h"
31#include "bitmap.h" 30#include "bitmap.h"
32 31
32#include <linux/dm-dirty-log.h>
33/* debug macros */ 33/* debug macros */
34 34
35#define DEBUG 0 35#define DEBUG 0
@@ -51,9 +51,6 @@
51#define INJECT_FATAL_FAULT_3 0 /* undef */ 51#define INJECT_FATAL_FAULT_3 0 /* undef */
52#endif 52#endif
53 53
54//#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
55#define DPRINTK(x...) do { } while(0)
56
57#ifndef PRINTK 54#ifndef PRINTK
58# if DEBUG > 0 55# if DEBUG > 0
59# define PRINTK(x...) printk(KERN_DEBUG x) 56# define PRINTK(x...) printk(KERN_DEBUG x)
@@ -62,12 +59,11 @@
62# endif 59# endif
63#endif 60#endif
64 61
65static inline char * bmname(struct bitmap *bitmap) 62static inline char *bmname(struct bitmap *bitmap)
66{ 63{
67 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX"; 64 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
68} 65}
69 66
70
71/* 67/*
72 * just a placeholder - calls kmalloc for bitmap pages 68 * just a placeholder - calls kmalloc for bitmap pages
73 */ 69 */
@@ -78,7 +74,7 @@ static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
78#ifdef INJECT_FAULTS_1 74#ifdef INJECT_FAULTS_1
79 page = NULL; 75 page = NULL;
80#else 76#else
81 page = kmalloc(PAGE_SIZE, GFP_NOIO); 77 page = kzalloc(PAGE_SIZE, GFP_NOIO);
82#endif 78#endif
83 if (!page) 79 if (!page)
84 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap)); 80 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
@@ -107,7 +103,8 @@ static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
107 * if we find our page, we increment the page's refcount so that it stays 103 * if we find our page, we increment the page's refcount so that it stays
108 * allocated while we're using it 104 * allocated while we're using it
109 */ 105 */
110static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create) 106static int bitmap_checkpage(struct bitmap *bitmap,
107 unsigned long page, int create)
111__releases(bitmap->lock) 108__releases(bitmap->lock)
112__acquires(bitmap->lock) 109__acquires(bitmap->lock)
113{ 110{
@@ -121,7 +118,6 @@ __acquires(bitmap->lock)
121 return -EINVAL; 118 return -EINVAL;
122 } 119 }
123 120
124
125 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */ 121 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
126 return 0; 122 return 0;
127 123
@@ -131,43 +127,34 @@ __acquires(bitmap->lock)
131 if (!create) 127 if (!create)
132 return -ENOENT; 128 return -ENOENT;
133 129
134 spin_unlock_irq(&bitmap->lock);
135
136 /* this page has not been allocated yet */ 130 /* this page has not been allocated yet */
137 131
138 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) { 132 spin_unlock_irq(&bitmap->lock);
133 mappage = bitmap_alloc_page(bitmap);
134 spin_lock_irq(&bitmap->lock);
135
136 if (mappage == NULL) {
139 PRINTK("%s: bitmap map page allocation failed, hijacking\n", 137 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
140 bmname(bitmap)); 138 bmname(bitmap));
141 /* failed - set the hijacked flag so that we can use the 139 /* failed - set the hijacked flag so that we can use the
142 * pointer as a counter */ 140 * pointer as a counter */
143 spin_lock_irq(&bitmap->lock);
144 if (!bitmap->bp[page].map) 141 if (!bitmap->bp[page].map)
145 bitmap->bp[page].hijacked = 1; 142 bitmap->bp[page].hijacked = 1;
146 goto out; 143 } else if (bitmap->bp[page].map ||
147 } 144 bitmap->bp[page].hijacked) {
148
149 /* got a page */
150
151 spin_lock_irq(&bitmap->lock);
152
153 /* recheck the page */
154
155 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
156 /* somebody beat us to getting the page */ 145 /* somebody beat us to getting the page */
157 bitmap_free_page(bitmap, mappage); 146 bitmap_free_page(bitmap, mappage);
158 return 0; 147 return 0;
159 } 148 } else {
160 149
161 /* no page was in place and we have one, so install it */ 150 /* no page was in place and we have one, so install it */
162 151
163 memset(mappage, 0, PAGE_SIZE); 152 bitmap->bp[page].map = mappage;
164 bitmap->bp[page].map = mappage; 153 bitmap->missing_pages--;
165 bitmap->missing_pages--; 154 }
166out:
167 return 0; 155 return 0;
168} 156}
169 157
170
171/* if page is completely empty, put it back on the free list, or dealloc it */ 158/* if page is completely empty, put it back on the free list, or dealloc it */
172/* if page was hijacked, unmark the flag so it might get alloced next time */ 159/* if page was hijacked, unmark the flag so it might get alloced next time */
173/* Note: lock should be held when calling this */ 160/* Note: lock should be held when calling this */
@@ -183,26 +170,15 @@ static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
183 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */ 170 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
184 bitmap->bp[page].hijacked = 0; 171 bitmap->bp[page].hijacked = 0;
185 bitmap->bp[page].map = NULL; 172 bitmap->bp[page].map = NULL;
186 return; 173 } else {
174 /* normal case, free the page */
175 ptr = bitmap->bp[page].map;
176 bitmap->bp[page].map = NULL;
177 bitmap->missing_pages++;
178 bitmap_free_page(bitmap, ptr);
187 } 179 }
188
189 /* normal case, free the page */
190
191#if 0
192/* actually ... let's not. We will probably need the page again exactly when
193 * memory is tight and we are flusing to disk
194 */
195 return;
196#else
197 ptr = bitmap->bp[page].map;
198 bitmap->bp[page].map = NULL;
199 bitmap->missing_pages++;
200 bitmap_free_page(bitmap, ptr);
201 return;
202#endif
203} 180}
204 181
205
206/* 182/*
207 * bitmap file handling - read and write the bitmap file and its superblock 183 * bitmap file handling - read and write the bitmap file and its superblock
208 */ 184 */
@@ -220,11 +196,14 @@ static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
220 196
221 mdk_rdev_t *rdev; 197 mdk_rdev_t *rdev;
222 sector_t target; 198 sector_t target;
199 int did_alloc = 0;
223 200
224 if (!page) 201 if (!page) {
225 page = alloc_page(GFP_KERNEL); 202 page = alloc_page(GFP_KERNEL);
226 if (!page) 203 if (!page)
227 return ERR_PTR(-ENOMEM); 204 return ERR_PTR(-ENOMEM);
205 did_alloc = 1;
206 }
228 207
229 list_for_each_entry(rdev, &mddev->disks, same_set) { 208 list_for_each_entry(rdev, &mddev->disks, same_set) {
230 if (! test_bit(In_sync, &rdev->flags) 209 if (! test_bit(In_sync, &rdev->flags)
@@ -242,6 +221,8 @@ static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
242 return page; 221 return page;
243 } 222 }
244 } 223 }
224 if (did_alloc)
225 put_page(page);
245 return ERR_PTR(-EIO); 226 return ERR_PTR(-EIO);
246 227
247} 228}
@@ -286,49 +267,51 @@ static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
286 mddev_t *mddev = bitmap->mddev; 267 mddev_t *mddev = bitmap->mddev;
287 268
288 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) { 269 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
289 int size = PAGE_SIZE; 270 int size = PAGE_SIZE;
290 loff_t offset = mddev->bitmap_info.offset; 271 loff_t offset = mddev->bitmap_info.offset;
291 if (page->index == bitmap->file_pages-1) 272 if (page->index == bitmap->file_pages-1)
292 size = roundup(bitmap->last_page_size, 273 size = roundup(bitmap->last_page_size,
293 bdev_logical_block_size(rdev->bdev)); 274 bdev_logical_block_size(rdev->bdev));
294 /* Just make sure we aren't corrupting data or 275 /* Just make sure we aren't corrupting data or
295 * metadata 276 * metadata
296 */ 277 */
297 if (mddev->external) { 278 if (mddev->external) {
298 /* Bitmap could be anywhere. */ 279 /* Bitmap could be anywhere. */
299 if (rdev->sb_start + offset + (page->index *(PAGE_SIZE/512)) > 280 if (rdev->sb_start + offset + (page->index
300 rdev->data_offset && 281 * (PAGE_SIZE/512))
301 rdev->sb_start + offset < 282 > rdev->data_offset
302 rdev->data_offset + mddev->dev_sectors + 283 &&
303 (PAGE_SIZE/512)) 284 rdev->sb_start + offset
304 goto bad_alignment; 285 < (rdev->data_offset + mddev->dev_sectors
305 } else if (offset < 0) { 286 + (PAGE_SIZE/512)))
306 /* DATA BITMAP METADATA */ 287 goto bad_alignment;
307 if (offset 288 } else if (offset < 0) {
308 + (long)(page->index * (PAGE_SIZE/512)) 289 /* DATA BITMAP METADATA */
309 + size/512 > 0) 290 if (offset
310 /* bitmap runs in to metadata */ 291 + (long)(page->index * (PAGE_SIZE/512))
311 goto bad_alignment; 292 + size/512 > 0)
312 if (rdev->data_offset + mddev->dev_sectors 293 /* bitmap runs in to metadata */
313 > rdev->sb_start + offset) 294 goto bad_alignment;
314 /* data runs in to bitmap */ 295 if (rdev->data_offset + mddev->dev_sectors
315 goto bad_alignment; 296 > rdev->sb_start + offset)
316 } else if (rdev->sb_start < rdev->data_offset) { 297 /* data runs in to bitmap */
317 /* METADATA BITMAP DATA */ 298 goto bad_alignment;
318 if (rdev->sb_start 299 } else if (rdev->sb_start < rdev->data_offset) {
319 + offset 300 /* METADATA BITMAP DATA */
320 + page->index*(PAGE_SIZE/512) + size/512 301 if (rdev->sb_start
321 > rdev->data_offset) 302 + offset
322 /* bitmap runs in to data */ 303 + page->index*(PAGE_SIZE/512) + size/512
323 goto bad_alignment; 304 > rdev->data_offset)
324 } else { 305 /* bitmap runs in to data */
325 /* DATA METADATA BITMAP - no problems */ 306 goto bad_alignment;
326 } 307 } else {
327 md_super_write(mddev, rdev, 308 /* DATA METADATA BITMAP - no problems */
328 rdev->sb_start + offset 309 }
329 + page->index * (PAGE_SIZE/512), 310 md_super_write(mddev, rdev,
330 size, 311 rdev->sb_start + offset
331 page); 312 + page->index * (PAGE_SIZE/512),
313 size,
314 page);
332 } 315 }
333 316
334 if (wait) 317 if (wait)
@@ -364,10 +347,9 @@ static void write_page(struct bitmap *bitmap, struct page *page, int wait)
364 bh = bh->b_this_page; 347 bh = bh->b_this_page;
365 } 348 }
366 349
367 if (wait) { 350 if (wait)
368 wait_event(bitmap->write_wait, 351 wait_event(bitmap->write_wait,
369 atomic_read(&bitmap->pending_writes)==0); 352 atomic_read(&bitmap->pending_writes)==0);
370 }
371 } 353 }
372 if (bitmap->flags & BITMAP_WRITE_ERROR) 354 if (bitmap->flags & BITMAP_WRITE_ERROR)
373 bitmap_file_kick(bitmap); 355 bitmap_file_kick(bitmap);
@@ -424,7 +406,7 @@ static struct page *read_page(struct file *file, unsigned long index,
424 struct buffer_head *bh; 406 struct buffer_head *bh;
425 sector_t block; 407 sector_t block;
426 408
427 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE, 409 PRINTK("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
428 (unsigned long long)index << PAGE_SHIFT); 410 (unsigned long long)index << PAGE_SHIFT);
429 411
430 page = alloc_page(GFP_KERNEL); 412 page = alloc_page(GFP_KERNEL);
@@ -478,7 +460,7 @@ static struct page *read_page(struct file *file, unsigned long index,
478 } 460 }
479out: 461out:
480 if (IS_ERR(page)) 462 if (IS_ERR(page))
481 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n", 463 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
482 (int)PAGE_SIZE, 464 (int)PAGE_SIZE,
483 (unsigned long long)index << PAGE_SHIFT, 465 (unsigned long long)index << PAGE_SHIFT,
484 PTR_ERR(page)); 466 PTR_ERR(page));
@@ -664,11 +646,14 @@ static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
664 sb = kmap_atomic(bitmap->sb_page, KM_USER0); 646 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
665 old = le32_to_cpu(sb->state) & bits; 647 old = le32_to_cpu(sb->state) & bits;
666 switch (op) { 648 switch (op) {
667 case MASK_SET: sb->state |= cpu_to_le32(bits); 649 case MASK_SET:
668 break; 650 sb->state |= cpu_to_le32(bits);
669 case MASK_UNSET: sb->state &= cpu_to_le32(~bits); 651 break;
670 break; 652 case MASK_UNSET:
671 default: BUG(); 653 sb->state &= cpu_to_le32(~bits);
654 break;
655 default:
656 BUG();
672 } 657 }
673 kunmap_atomic(sb, KM_USER0); 658 kunmap_atomic(sb, KM_USER0);
674 return old; 659 return old;
@@ -710,12 +695,14 @@ static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned lon
710static inline struct page *filemap_get_page(struct bitmap *bitmap, 695static inline struct page *filemap_get_page(struct bitmap *bitmap,
711 unsigned long chunk) 696 unsigned long chunk)
712{ 697{
713 if (file_page_index(bitmap, chunk) >= bitmap->file_pages) return NULL; 698 if (bitmap->filemap == NULL)
699 return NULL;
700 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
701 return NULL;
714 return bitmap->filemap[file_page_index(bitmap, chunk) 702 return bitmap->filemap[file_page_index(bitmap, chunk)
715 - file_page_index(bitmap, 0)]; 703 - file_page_index(bitmap, 0)];
716} 704}
717 705
718
719static void bitmap_file_unmap(struct bitmap *bitmap) 706static void bitmap_file_unmap(struct bitmap *bitmap)
720{ 707{
721 struct page **map, *sb_page; 708 struct page **map, *sb_page;
@@ -766,7 +753,6 @@ static void bitmap_file_put(struct bitmap *bitmap)
766 } 753 }
767} 754}
768 755
769
770/* 756/*
771 * bitmap_file_kick - if an error occurs while manipulating the bitmap file 757 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
772 * then it is no longer reliable, so we stop using it and we mark the file 758 * then it is no longer reliable, so we stop using it and we mark the file
@@ -785,7 +771,6 @@ static void bitmap_file_kick(struct bitmap *bitmap)
785 ptr = d_path(&bitmap->file->f_path, path, 771 ptr = d_path(&bitmap->file->f_path, path,
786 PAGE_SIZE); 772 PAGE_SIZE);
787 773
788
789 printk(KERN_ALERT 774 printk(KERN_ALERT
790 "%s: kicking failed bitmap file %s from array!\n", 775 "%s: kicking failed bitmap file %s from array!\n",
791 bmname(bitmap), IS_ERR(ptr) ? "" : ptr); 776 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
@@ -803,27 +788,36 @@ static void bitmap_file_kick(struct bitmap *bitmap)
803} 788}
804 789
805enum bitmap_page_attr { 790enum bitmap_page_attr {
806 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced 791 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
807 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared 792 BITMAP_PAGE_CLEAN = 1, /* there are bits that might need to be cleared */
808 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced 793 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
809}; 794};
810 795
811static inline void set_page_attr(struct bitmap *bitmap, struct page *page, 796static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
812 enum bitmap_page_attr attr) 797 enum bitmap_page_attr attr)
813{ 798{
814 __set_bit((page->index<<2) + attr, bitmap->filemap_attr); 799 if (page)
800 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
801 else
802 __set_bit(attr, &bitmap->logattrs);
815} 803}
816 804
817static inline void clear_page_attr(struct bitmap *bitmap, struct page *page, 805static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
818 enum bitmap_page_attr attr) 806 enum bitmap_page_attr attr)
819{ 807{
820 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr); 808 if (page)
809 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
810 else
811 __clear_bit(attr, &bitmap->logattrs);
821} 812}
822 813
823static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page, 814static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
824 enum bitmap_page_attr attr) 815 enum bitmap_page_attr attr)
825{ 816{
826 return test_bit((page->index<<2) + attr, bitmap->filemap_attr); 817 if (page)
818 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
819 else
820 return test_bit(attr, &bitmap->logattrs);
827} 821}
828 822
829/* 823/*
@@ -836,30 +830,32 @@ static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *p
836static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block) 830static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
837{ 831{
838 unsigned long bit; 832 unsigned long bit;
839 struct page *page; 833 struct page *page = NULL;
840 void *kaddr; 834 void *kaddr;
841 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap); 835 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
842 836
843 if (!bitmap->filemap) { 837 if (!bitmap->filemap) {
844 return; 838 struct dm_dirty_log *log = bitmap->mddev->bitmap_info.log;
845 } 839 if (log)
846 840 log->type->mark_region(log, chunk);
847 page = filemap_get_page(bitmap, chunk); 841 } else {
848 if (!page) return;
849 bit = file_page_offset(bitmap, chunk);
850 842
851 /* set the bit */ 843 page = filemap_get_page(bitmap, chunk);
852 kaddr = kmap_atomic(page, KM_USER0); 844 if (!page)
853 if (bitmap->flags & BITMAP_HOSTENDIAN) 845 return;
854 set_bit(bit, kaddr); 846 bit = file_page_offset(bitmap, chunk);
855 else
856 ext2_set_bit(bit, kaddr);
857 kunmap_atomic(kaddr, KM_USER0);
858 PRINTK("set file bit %lu page %lu\n", bit, page->index);
859 847
848 /* set the bit */
849 kaddr = kmap_atomic(page, KM_USER0);
850 if (bitmap->flags & BITMAP_HOSTENDIAN)
851 set_bit(bit, kaddr);
852 else
853 ext2_set_bit(bit, kaddr);
854 kunmap_atomic(kaddr, KM_USER0);
855 PRINTK("set file bit %lu page %lu\n", bit, page->index);
856 }
860 /* record page number so it gets flushed to disk when unplug occurs */ 857 /* record page number so it gets flushed to disk when unplug occurs */
861 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); 858 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
862
863} 859}
864 860
865/* this gets called when the md device is ready to unplug its underlying 861/* this gets called when the md device is ready to unplug its underlying
@@ -874,6 +870,16 @@ void bitmap_unplug(struct bitmap *bitmap)
874 870
875 if (!bitmap) 871 if (!bitmap)
876 return; 872 return;
873 if (!bitmap->filemap) {
874 /* Must be using a dirty_log */
875 struct dm_dirty_log *log = bitmap->mddev->bitmap_info.log;
876 dirty = test_and_clear_bit(BITMAP_PAGE_DIRTY, &bitmap->logattrs);
877 need_write = test_and_clear_bit(BITMAP_PAGE_NEEDWRITE, &bitmap->logattrs);
878 if (dirty || need_write)
879 if (log->type->flush(log))
880 bitmap->flags |= BITMAP_WRITE_ERROR;
881 goto out;
882 }
877 883
878 /* look at each page to see if there are any set bits that need to be 884 /* look at each page to see if there are any set bits that need to be
879 * flushed out to disk */ 885 * flushed out to disk */
@@ -892,7 +898,7 @@ void bitmap_unplug(struct bitmap *bitmap)
892 wait = 1; 898 wait = 1;
893 spin_unlock_irqrestore(&bitmap->lock, flags); 899 spin_unlock_irqrestore(&bitmap->lock, flags);
894 900
895 if (dirty | need_write) 901 if (dirty || need_write)
896 write_page(bitmap, page, 0); 902 write_page(bitmap, page, 0);
897 } 903 }
898 if (wait) { /* if any writes were performed, we need to wait on them */ 904 if (wait) { /* if any writes were performed, we need to wait on them */
@@ -902,9 +908,11 @@ void bitmap_unplug(struct bitmap *bitmap)
902 else 908 else
903 md_super_wait(bitmap->mddev); 909 md_super_wait(bitmap->mddev);
904 } 910 }
911out:
905 if (bitmap->flags & BITMAP_WRITE_ERROR) 912 if (bitmap->flags & BITMAP_WRITE_ERROR)
906 bitmap_file_kick(bitmap); 913 bitmap_file_kick(bitmap);
907} 914}
915EXPORT_SYMBOL(bitmap_unplug);
908 916
909static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed); 917static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
910/* * bitmap_init_from_disk -- called at bitmap_create time to initialize 918/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
@@ -943,12 +951,11 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
943 printk(KERN_INFO "%s: bitmap file is out of date, doing full " 951 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
944 "recovery\n", bmname(bitmap)); 952 "recovery\n", bmname(bitmap));
945 953
946 bytes = (chunks + 7) / 8; 954 bytes = DIV_ROUND_UP(bitmap->chunks, 8);
947 if (!bitmap->mddev->bitmap_info.external) 955 if (!bitmap->mddev->bitmap_info.external)
948 bytes += sizeof(bitmap_super_t); 956 bytes += sizeof(bitmap_super_t);
949 957
950 958 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
951 num_pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
952 959
953 if (file && i_size_read(file->f_mapping->host) < bytes) { 960 if (file && i_size_read(file->f_mapping->host) < bytes) {
954 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n", 961 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
@@ -966,7 +973,7 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
966 973
967 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */ 974 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
968 bitmap->filemap_attr = kzalloc( 975 bitmap->filemap_attr = kzalloc(
969 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)), 976 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
970 GFP_KERNEL); 977 GFP_KERNEL);
971 if (!bitmap->filemap_attr) 978 if (!bitmap->filemap_attr)
972 goto err; 979 goto err;
@@ -1021,7 +1028,7 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
1021 if (outofdate) { 1028 if (outofdate) {
1022 /* 1029 /*
1023 * if bitmap is out of date, dirty the 1030 * if bitmap is out of date, dirty the
1024 * whole page and write it out 1031 * whole page and write it out
1025 */ 1032 */
1026 paddr = kmap_atomic(page, KM_USER0); 1033 paddr = kmap_atomic(page, KM_USER0);
1027 memset(paddr + offset, 0xff, 1034 memset(paddr + offset, 0xff,
@@ -1052,7 +1059,7 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
1052 } 1059 }
1053 } 1060 }
1054 1061
1055 /* everything went OK */ 1062 /* everything went OK */
1056 ret = 0; 1063 ret = 0;
1057 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET); 1064 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1058 1065
@@ -1080,21 +1087,16 @@ void bitmap_write_all(struct bitmap *bitmap)
1080 */ 1087 */
1081 int i; 1088 int i;
1082 1089
1083 for (i=0; i < bitmap->file_pages; i++) 1090 for (i = 0; i < bitmap->file_pages; i++)
1084 set_page_attr(bitmap, bitmap->filemap[i], 1091 set_page_attr(bitmap, bitmap->filemap[i],
1085 BITMAP_PAGE_NEEDWRITE); 1092 BITMAP_PAGE_NEEDWRITE);
1086} 1093}
1087 1094
1088
1089static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc) 1095static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1090{ 1096{
1091 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap); 1097 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1092 unsigned long page = chunk >> PAGE_COUNTER_SHIFT; 1098 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1093 bitmap->bp[page].count += inc; 1099 bitmap->bp[page].count += inc;
1094/*
1095 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1096 (unsigned long long)offset, inc, bitmap->bp[page].count);
1097*/
1098 bitmap_checkfree(bitmap, page); 1100 bitmap_checkfree(bitmap, page);
1099} 1101}
1100static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap, 1102static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
@@ -1114,6 +1116,7 @@ void bitmap_daemon_work(mddev_t *mddev)
1114 struct page *page = NULL, *lastpage = NULL; 1116 struct page *page = NULL, *lastpage = NULL;
1115 int blocks; 1117 int blocks;
1116 void *paddr; 1118 void *paddr;
1119 struct dm_dirty_log *log = mddev->bitmap_info.log;
1117 1120
1118 /* Use a mutex to guard daemon_work against 1121 /* Use a mutex to guard daemon_work against
1119 * bitmap_destroy. 1122 * bitmap_destroy.
@@ -1138,11 +1141,12 @@ void bitmap_daemon_work(mddev_t *mddev)
1138 spin_lock_irqsave(&bitmap->lock, flags); 1141 spin_lock_irqsave(&bitmap->lock, flags);
1139 for (j = 0; j < bitmap->chunks; j++) { 1142 for (j = 0; j < bitmap->chunks; j++) {
1140 bitmap_counter_t *bmc; 1143 bitmap_counter_t *bmc;
1141 if (!bitmap->filemap) 1144 if (!bitmap->filemap) {
1142 /* error or shutdown */ 1145 if (!log)
1143 break; 1146 /* error or shutdown */
1144 1147 break;
1145 page = filemap_get_page(bitmap, j); 1148 } else
1149 page = filemap_get_page(bitmap, j);
1146 1150
1147 if (page != lastpage) { 1151 if (page != lastpage) {
1148 /* skip this page unless it's marked as needing cleaning */ 1152 /* skip this page unless it's marked as needing cleaning */
@@ -1197,14 +1201,11 @@ void bitmap_daemon_work(mddev_t *mddev)
1197 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap), 1201 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1198 &blocks, 0); 1202 &blocks, 0);
1199 if (bmc) { 1203 if (bmc) {
1200/*
1201 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1202*/
1203 if (*bmc) 1204 if (*bmc)
1204 bitmap->allclean = 0; 1205 bitmap->allclean = 0;
1205 1206
1206 if (*bmc == 2) { 1207 if (*bmc == 2) {
1207 *bmc=1; /* maybe clear the bit next time */ 1208 *bmc = 1; /* maybe clear the bit next time */
1208 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN); 1209 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1209 } else if (*bmc == 1 && !bitmap->need_sync) { 1210 } else if (*bmc == 1 && !bitmap->need_sync) {
1210 /* we can clear the bit */ 1211 /* we can clear the bit */
@@ -1214,14 +1215,17 @@ void bitmap_daemon_work(mddev_t *mddev)
1214 -1); 1215 -1);
1215 1216
1216 /* clear the bit */ 1217 /* clear the bit */
1217 paddr = kmap_atomic(page, KM_USER0); 1218 if (page) {
1218 if (bitmap->flags & BITMAP_HOSTENDIAN) 1219 paddr = kmap_atomic(page, KM_USER0);
1219 clear_bit(file_page_offset(bitmap, j), 1220 if (bitmap->flags & BITMAP_HOSTENDIAN)
1220 paddr); 1221 clear_bit(file_page_offset(bitmap, j),
1221 else 1222 paddr);
1222 ext2_clear_bit(file_page_offset(bitmap, j), 1223 else
1223 paddr); 1224 ext2_clear_bit(file_page_offset(bitmap, j),
1224 kunmap_atomic(paddr, KM_USER0); 1225 paddr);
1226 kunmap_atomic(paddr, KM_USER0);
1227 } else
1228 log->type->clear_region(log, j);
1225 } 1229 }
1226 } else 1230 } else
1227 j |= PAGE_COUNTER_MASK; 1231 j |= PAGE_COUNTER_MASK;
@@ -1229,12 +1233,16 @@ void bitmap_daemon_work(mddev_t *mddev)
1229 spin_unlock_irqrestore(&bitmap->lock, flags); 1233 spin_unlock_irqrestore(&bitmap->lock, flags);
1230 1234
1231 /* now sync the final page */ 1235 /* now sync the final page */
1232 if (lastpage != NULL) { 1236 if (lastpage != NULL || log != NULL) {
1233 spin_lock_irqsave(&bitmap->lock, flags); 1237 spin_lock_irqsave(&bitmap->lock, flags);
1234 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) { 1238 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1235 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); 1239 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1236 spin_unlock_irqrestore(&bitmap->lock, flags); 1240 spin_unlock_irqrestore(&bitmap->lock, flags);
1237 write_page(bitmap, lastpage, 0); 1241 if (lastpage)
1242 write_page(bitmap, lastpage, 0);
1243 else
1244 if (log->type->flush(log))
1245 bitmap->flags |= BITMAP_WRITE_ERROR;
1238 } else { 1246 } else {
1239 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); 1247 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1240 spin_unlock_irqrestore(&bitmap->lock, flags); 1248 spin_unlock_irqrestore(&bitmap->lock, flags);
@@ -1243,7 +1251,7 @@ void bitmap_daemon_work(mddev_t *mddev)
1243 1251
1244 done: 1252 done:
1245 if (bitmap->allclean == 0) 1253 if (bitmap->allclean == 0)
1246 bitmap->mddev->thread->timeout = 1254 bitmap->mddev->thread->timeout =
1247 bitmap->mddev->bitmap_info.daemon_sleep; 1255 bitmap->mddev->bitmap_info.daemon_sleep;
1248 mutex_unlock(&mddev->bitmap_info.mutex); 1256 mutex_unlock(&mddev->bitmap_info.mutex);
1249} 1257}
@@ -1262,34 +1270,38 @@ __acquires(bitmap->lock)
1262 unsigned long page = chunk >> PAGE_COUNTER_SHIFT; 1270 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1263 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT; 1271 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1264 sector_t csize; 1272 sector_t csize;
1273 int err;
1265 1274
1266 if (bitmap_checkpage(bitmap, page, create) < 0) { 1275 err = bitmap_checkpage(bitmap, page, create);
1276
1277 if (bitmap->bp[page].hijacked ||
1278 bitmap->bp[page].map == NULL)
1279 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1280 PAGE_COUNTER_SHIFT - 1);
1281 else
1267 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap)); 1282 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1268 *blocks = csize - (offset & (csize- 1)); 1283 *blocks = csize - (offset & (csize - 1));
1284
1285 if (err < 0)
1269 return NULL; 1286 return NULL;
1270 } 1287
1271 /* now locked ... */ 1288 /* now locked ... */
1272 1289
1273 if (bitmap->bp[page].hijacked) { /* hijacked pointer */ 1290 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1274 /* should we use the first or second counter field 1291 /* should we use the first or second counter field
1275 * of the hijacked pointer? */ 1292 * of the hijacked pointer? */
1276 int hi = (pageoff > PAGE_COUNTER_MASK); 1293 int hi = (pageoff > PAGE_COUNTER_MASK);
1277 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1278 PAGE_COUNTER_SHIFT - 1);
1279 *blocks = csize - (offset & (csize- 1));
1280 return &((bitmap_counter_t *) 1294 return &((bitmap_counter_t *)
1281 &bitmap->bp[page].map)[hi]; 1295 &bitmap->bp[page].map)[hi];
1282 } else { /* page is allocated */ 1296 } else /* page is allocated */
1283 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1284 *blocks = csize - (offset & (csize- 1));
1285 return (bitmap_counter_t *) 1297 return (bitmap_counter_t *)
1286 &(bitmap->bp[page].map[pageoff]); 1298 &(bitmap->bp[page].map[pageoff]);
1287 }
1288} 1299}
1289 1300
1290int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind) 1301int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1291{ 1302{
1292 if (!bitmap) return 0; 1303 if (!bitmap)
1304 return 0;
1293 1305
1294 if (behind) { 1306 if (behind) {
1295 int bw; 1307 int bw;
@@ -1322,17 +1334,16 @@ int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sect
1322 prepare_to_wait(&bitmap->overflow_wait, &__wait, 1334 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1323 TASK_UNINTERRUPTIBLE); 1335 TASK_UNINTERRUPTIBLE);
1324 spin_unlock_irq(&bitmap->lock); 1336 spin_unlock_irq(&bitmap->lock);
1325 blk_unplug(bitmap->mddev->queue); 1337 md_unplug(bitmap->mddev);
1326 schedule(); 1338 schedule();
1327 finish_wait(&bitmap->overflow_wait, &__wait); 1339 finish_wait(&bitmap->overflow_wait, &__wait);
1328 continue; 1340 continue;
1329 } 1341 }
1330 1342
1331 switch(*bmc) { 1343 switch (*bmc) {
1332 case 0: 1344 case 0:
1333 bitmap_file_set_bit(bitmap, offset); 1345 bitmap_file_set_bit(bitmap, offset);
1334 bitmap_count_page(bitmap,offset, 1); 1346 bitmap_count_page(bitmap, offset, 1);
1335 blk_plug_device_unlocked(bitmap->mddev->queue);
1336 /* fall through */ 1347 /* fall through */
1337 case 1: 1348 case 1:
1338 *bmc = 2; 1349 *bmc = 2;
@@ -1345,16 +1356,19 @@ int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sect
1345 offset += blocks; 1356 offset += blocks;
1346 if (sectors > blocks) 1357 if (sectors > blocks)
1347 sectors -= blocks; 1358 sectors -= blocks;
1348 else sectors = 0; 1359 else
1360 sectors = 0;
1349 } 1361 }
1350 bitmap->allclean = 0; 1362 bitmap->allclean = 0;
1351 return 0; 1363 return 0;
1352} 1364}
1365EXPORT_SYMBOL(bitmap_startwrite);
1353 1366
1354void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, 1367void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1355 int success, int behind) 1368 int success, int behind)
1356{ 1369{
1357 if (!bitmap) return; 1370 if (!bitmap)
1371 return;
1358 if (behind) { 1372 if (behind) {
1359 if (atomic_dec_and_test(&bitmap->behind_writes)) 1373 if (atomic_dec_and_test(&bitmap->behind_writes))
1360 wake_up(&bitmap->behind_wait); 1374 wake_up(&bitmap->behind_wait);
@@ -1381,7 +1395,7 @@ void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long secto
1381 bitmap->events_cleared < bitmap->mddev->events) { 1395 bitmap->events_cleared < bitmap->mddev->events) {
1382 bitmap->events_cleared = bitmap->mddev->events; 1396 bitmap->events_cleared = bitmap->mddev->events;
1383 bitmap->need_sync = 1; 1397 bitmap->need_sync = 1;
1384 sysfs_notify_dirent(bitmap->sysfs_can_clear); 1398 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
1385 } 1399 }
1386 1400
1387 if (!success && ! (*bmc & NEEDED_MASK)) 1401 if (!success && ! (*bmc & NEEDED_MASK))
@@ -1391,18 +1405,22 @@ void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long secto
1391 wake_up(&bitmap->overflow_wait); 1405 wake_up(&bitmap->overflow_wait);
1392 1406
1393 (*bmc)--; 1407 (*bmc)--;
1394 if (*bmc <= 2) { 1408 if (*bmc <= 2)
1395 set_page_attr(bitmap, 1409 set_page_attr(bitmap,
1396 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)), 1410 filemap_get_page(
1411 bitmap,
1412 offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1397 BITMAP_PAGE_CLEAN); 1413 BITMAP_PAGE_CLEAN);
1398 } 1414
1399 spin_unlock_irqrestore(&bitmap->lock, flags); 1415 spin_unlock_irqrestore(&bitmap->lock, flags);
1400 offset += blocks; 1416 offset += blocks;
1401 if (sectors > blocks) 1417 if (sectors > blocks)
1402 sectors -= blocks; 1418 sectors -= blocks;
1403 else sectors = 0; 1419 else
1420 sectors = 0;
1404 } 1421 }
1405} 1422}
1423EXPORT_SYMBOL(bitmap_endwrite);
1406 1424
1407static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks, 1425static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1408 int degraded) 1426 int degraded)
@@ -1455,14 +1473,14 @@ int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1455 } 1473 }
1456 return rv; 1474 return rv;
1457} 1475}
1476EXPORT_SYMBOL(bitmap_start_sync);
1458 1477
1459void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted) 1478void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1460{ 1479{
1461 bitmap_counter_t *bmc; 1480 bitmap_counter_t *bmc;
1462 unsigned long flags; 1481 unsigned long flags;
1463/* 1482
1464 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted); 1483 if (bitmap == NULL) {
1465*/ if (bitmap == NULL) {
1466 *blocks = 1024; 1484 *blocks = 1024;
1467 return; 1485 return;
1468 } 1486 }
@@ -1471,26 +1489,23 @@ void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int ab
1471 if (bmc == NULL) 1489 if (bmc == NULL)
1472 goto unlock; 1490 goto unlock;
1473 /* locked */ 1491 /* locked */
1474/*
1475 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1476*/
1477 if (RESYNC(*bmc)) { 1492 if (RESYNC(*bmc)) {
1478 *bmc &= ~RESYNC_MASK; 1493 *bmc &= ~RESYNC_MASK;
1479 1494
1480 if (!NEEDED(*bmc) && aborted) 1495 if (!NEEDED(*bmc) && aborted)
1481 *bmc |= NEEDED_MASK; 1496 *bmc |= NEEDED_MASK;
1482 else { 1497 else {
1483 if (*bmc <= 2) { 1498 if (*bmc <= 2)
1484 set_page_attr(bitmap, 1499 set_page_attr(bitmap,
1485 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)), 1500 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1486 BITMAP_PAGE_CLEAN); 1501 BITMAP_PAGE_CLEAN);
1487 }
1488 } 1502 }
1489 } 1503 }
1490 unlock: 1504 unlock:
1491 spin_unlock_irqrestore(&bitmap->lock, flags); 1505 spin_unlock_irqrestore(&bitmap->lock, flags);
1492 bitmap->allclean = 0; 1506 bitmap->allclean = 0;
1493} 1507}
1508EXPORT_SYMBOL(bitmap_end_sync);
1494 1509
1495void bitmap_close_sync(struct bitmap *bitmap) 1510void bitmap_close_sync(struct bitmap *bitmap)
1496{ 1511{
@@ -1507,6 +1522,7 @@ void bitmap_close_sync(struct bitmap *bitmap)
1507 sector += blocks; 1522 sector += blocks;
1508 } 1523 }
1509} 1524}
1525EXPORT_SYMBOL(bitmap_close_sync);
1510 1526
1511void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector) 1527void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1512{ 1528{
@@ -1526,7 +1542,8 @@ void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1526 atomic_read(&bitmap->mddev->recovery_active) == 0); 1542 atomic_read(&bitmap->mddev->recovery_active) == 0);
1527 1543
1528 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync; 1544 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
1529 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags); 1545 if (bitmap->mddev->persistent)
1546 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
1530 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1); 1547 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1531 s = 0; 1548 s = 0;
1532 while (s < sector && s < bitmap->mddev->resync_max_sectors) { 1549 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
@@ -1536,6 +1553,7 @@ void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1536 bitmap->last_end_sync = jiffies; 1553 bitmap->last_end_sync = jiffies;
1537 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed"); 1554 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
1538} 1555}
1556EXPORT_SYMBOL(bitmap_cond_end_sync);
1539 1557
1540static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed) 1558static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1541{ 1559{
@@ -1552,9 +1570,9 @@ static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int n
1552 spin_unlock_irq(&bitmap->lock); 1570 spin_unlock_irq(&bitmap->lock);
1553 return; 1571 return;
1554 } 1572 }
1555 if (! *bmc) { 1573 if (!*bmc) {
1556 struct page *page; 1574 struct page *page;
1557 *bmc = 1 | (needed?NEEDED_MASK:0); 1575 *bmc = 1 | (needed ? NEEDED_MASK : 0);
1558 bitmap_count_page(bitmap, offset, 1); 1576 bitmap_count_page(bitmap, offset, 1);
1559 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)); 1577 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1560 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN); 1578 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
@@ -1663,15 +1681,17 @@ int bitmap_create(mddev_t *mddev)
1663 unsigned long pages; 1681 unsigned long pages;
1664 struct file *file = mddev->bitmap_info.file; 1682 struct file *file = mddev->bitmap_info.file;
1665 int err; 1683 int err;
1666 sector_t start; 1684 struct sysfs_dirent *bm = NULL;
1667 struct sysfs_dirent *bm;
1668 1685
1669 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256); 1686 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
1670 1687
1671 if (!file && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */ 1688 if (!file
1689 && !mddev->bitmap_info.offset
1690 && !mddev->bitmap_info.log) /* bitmap disabled, nothing to do */
1672 return 0; 1691 return 0;
1673 1692
1674 BUG_ON(file && mddev->bitmap_info.offset); 1693 BUG_ON(file && mddev->bitmap_info.offset);
1694 BUG_ON(mddev->bitmap_info.offset && mddev->bitmap_info.log);
1675 1695
1676 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL); 1696 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
1677 if (!bitmap) 1697 if (!bitmap)
@@ -1685,7 +1705,8 @@ int bitmap_create(mddev_t *mddev)
1685 1705
1686 bitmap->mddev = mddev; 1706 bitmap->mddev = mddev;
1687 1707
1688 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap"); 1708 if (mddev->kobj.sd)
1709 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
1689 if (bm) { 1710 if (bm) {
1690 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear"); 1711 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
1691 sysfs_put(bm); 1712 sysfs_put(bm);
@@ -1719,9 +1740,9 @@ int bitmap_create(mddev_t *mddev)
1719 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize); 1740 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
1720 1741
1721 /* now that chunksize and chunkshift are set, we can use these macros */ 1742 /* now that chunksize and chunkshift are set, we can use these macros */
1722 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >> 1743 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
1723 CHUNK_BLOCK_SHIFT(bitmap); 1744 CHUNK_BLOCK_SHIFT(bitmap);
1724 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO; 1745 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1725 1746
1726 BUG_ON(!pages); 1747 BUG_ON(!pages);
1727 1748
@@ -1741,27 +1762,11 @@ int bitmap_create(mddev_t *mddev)
1741 if (!bitmap->bp) 1762 if (!bitmap->bp)
1742 goto error; 1763 goto error;
1743 1764
1744 /* now that we have some pages available, initialize the in-memory
1745 * bitmap from the on-disk bitmap */
1746 start = 0;
1747 if (mddev->degraded == 0
1748 || bitmap->events_cleared == mddev->events)
1749 /* no need to keep dirty bits to optimise a re-add of a missing device */
1750 start = mddev->recovery_cp;
1751 err = bitmap_init_from_disk(bitmap, start);
1752
1753 if (err)
1754 goto error;
1755
1756 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n", 1765 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1757 pages, bmname(bitmap)); 1766 pages, bmname(bitmap));
1758 1767
1759 mddev->bitmap = bitmap; 1768 mddev->bitmap = bitmap;
1760 1769
1761 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
1762 md_wakeup_thread(mddev->thread);
1763
1764 bitmap_update_sb(bitmap);
1765 1770
1766 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0; 1771 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1767 1772
@@ -1770,15 +1775,69 @@ int bitmap_create(mddev_t *mddev)
1770 return err; 1775 return err;
1771} 1776}
1772 1777
1778int bitmap_load(mddev_t *mddev)
1779{
1780 int err = 0;
1781 sector_t sector = 0;
1782 struct bitmap *bitmap = mddev->bitmap;
1783
1784 if (!bitmap)
1785 goto out;
1786
1787 /* Clear out old bitmap info first: Either there is none, or we
1788 * are resuming after someone else has possibly changed things,
1789 * so we should forget old cached info.
1790 * All chunks should be clean, but some might need_sync.
1791 */
1792 while (sector < mddev->resync_max_sectors) {
1793 int blocks;
1794 bitmap_start_sync(bitmap, sector, &blocks, 0);
1795 sector += blocks;
1796 }
1797 bitmap_close_sync(bitmap);
1798
1799 if (mddev->bitmap_info.log) {
1800 unsigned long i;
1801 struct dm_dirty_log *log = mddev->bitmap_info.log;
1802 for (i = 0; i < bitmap->chunks; i++)
1803 if (!log->type->in_sync(log, i, 1))
1804 bitmap_set_memory_bits(bitmap,
1805 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1806 1);
1807 } else {
1808 sector_t start = 0;
1809 if (mddev->degraded == 0
1810 || bitmap->events_cleared == mddev->events)
1811 /* no need to keep dirty bits to optimise a
1812 * re-add of a missing device */
1813 start = mddev->recovery_cp;
1814
1815 err = bitmap_init_from_disk(bitmap, start);
1816 }
1817 if (err)
1818 goto out;
1819
1820 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
1821 md_wakeup_thread(mddev->thread);
1822
1823 bitmap_update_sb(bitmap);
1824
1825 if (bitmap->flags & BITMAP_WRITE_ERROR)
1826 err = -EIO;
1827out:
1828 return err;
1829}
1830EXPORT_SYMBOL_GPL(bitmap_load);
1831
1773static ssize_t 1832static ssize_t
1774location_show(mddev_t *mddev, char *page) 1833location_show(mddev_t *mddev, char *page)
1775{ 1834{
1776 ssize_t len; 1835 ssize_t len;
1777 if (mddev->bitmap_info.file) { 1836 if (mddev->bitmap_info.file)
1778 len = sprintf(page, "file"); 1837 len = sprintf(page, "file");
1779 } else if (mddev->bitmap_info.offset) { 1838 else if (mddev->bitmap_info.offset)
1780 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset); 1839 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
1781 } else 1840 else
1782 len = sprintf(page, "none"); 1841 len = sprintf(page, "none");
1783 len += sprintf(page+len, "\n"); 1842 len += sprintf(page+len, "\n");
1784 return len; 1843 return len;
@@ -1867,7 +1926,7 @@ timeout_show(mddev_t *mddev, char *page)
1867 ssize_t len; 1926 ssize_t len;
1868 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ; 1927 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1869 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ; 1928 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
1870 1929
1871 len = sprintf(page, "%lu", secs); 1930 len = sprintf(page, "%lu", secs);
1872 if (jifs) 1931 if (jifs)
1873 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs)); 1932 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
@@ -2049,12 +2108,3 @@ struct attribute_group md_bitmap_group = {
2049 .attrs = md_bitmap_attrs, 2108 .attrs = md_bitmap_attrs,
2050}; 2109};
2051 2110
2052
2053/* the bitmap API -- for raid personalities */
2054EXPORT_SYMBOL(bitmap_startwrite);
2055EXPORT_SYMBOL(bitmap_endwrite);
2056EXPORT_SYMBOL(bitmap_start_sync);
2057EXPORT_SYMBOL(bitmap_end_sync);
2058EXPORT_SYMBOL(bitmap_unplug);
2059EXPORT_SYMBOL(bitmap_close_sync);
2060EXPORT_SYMBOL(bitmap_cond_end_sync);
diff --git a/drivers/md/bitmap.h b/drivers/md/bitmap.h
index 3797dea4723..e872a7bad6b 100644
--- a/drivers/md/bitmap.h
+++ b/drivers/md/bitmap.h
@@ -222,6 +222,10 @@ struct bitmap {
222 unsigned long file_pages; /* number of pages in the file */ 222 unsigned long file_pages; /* number of pages in the file */
223 int last_page_size; /* bytes in the last page */ 223 int last_page_size; /* bytes in the last page */
224 224
225 unsigned long logattrs; /* used when filemap_attr doesn't exist
226 * because we are working with a dirty_log
227 */
228
225 unsigned long flags; 229 unsigned long flags;
226 230
227 int allclean; 231 int allclean;
@@ -243,12 +247,14 @@ struct bitmap {
243 wait_queue_head_t behind_wait; 247 wait_queue_head_t behind_wait;
244 248
245 struct sysfs_dirent *sysfs_can_clear; 249 struct sysfs_dirent *sysfs_can_clear;
250
246}; 251};
247 252
248/* the bitmap API */ 253/* the bitmap API */
249 254
250/* these are used only by md/bitmap */ 255/* these are used only by md/bitmap */
251int bitmap_create(mddev_t *mddev); 256int bitmap_create(mddev_t *mddev);
257int bitmap_load(mddev_t *mddev);
252void bitmap_flush(mddev_t *mddev); 258void bitmap_flush(mddev_t *mddev);
253void bitmap_destroy(mddev_t *mddev); 259void bitmap_destroy(mddev_t *mddev);
254 260
diff --git a/drivers/md/dm-io.c b/drivers/md/dm-io.c
index 10f457ca6af..0590c75b0ab 100644
--- a/drivers/md/dm-io.c
+++ b/drivers/md/dm-io.c
@@ -356,7 +356,7 @@ static void dispatch_io(int rw, unsigned int num_regions,
356 BUG_ON(num_regions > DM_IO_MAX_REGIONS); 356 BUG_ON(num_regions > DM_IO_MAX_REGIONS);
357 357
358 if (sync) 358 if (sync)
359 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); 359 rw |= REQ_SYNC | REQ_UNPLUG;
360 360
361 /* 361 /*
362 * For multiple regions we need to be careful to rewind 362 * For multiple regions we need to be careful to rewind
@@ -364,7 +364,7 @@ static void dispatch_io(int rw, unsigned int num_regions,
364 */ 364 */
365 for (i = 0; i < num_regions; i++) { 365 for (i = 0; i < num_regions; i++) {
366 *dp = old_pages; 366 *dp = old_pages;
367 if (where[i].count || (rw & (1 << BIO_RW_BARRIER))) 367 if (where[i].count || (rw & REQ_HARDBARRIER))
368 do_region(rw, i, where + i, dp, io); 368 do_region(rw, i, where + i, dp, io);
369 } 369 }
370 370
@@ -412,8 +412,8 @@ retry:
412 } 412 }
413 set_current_state(TASK_RUNNING); 413 set_current_state(TASK_RUNNING);
414 414
415 if (io->eopnotsupp_bits && (rw & (1 << BIO_RW_BARRIER))) { 415 if (io->eopnotsupp_bits && (rw & REQ_HARDBARRIER)) {
416 rw &= ~(1 << BIO_RW_BARRIER); 416 rw &= ~REQ_HARDBARRIER;
417 goto retry; 417 goto retry;
418 } 418 }
419 419
@@ -479,8 +479,8 @@ static int dp_init(struct dm_io_request *io_req, struct dpages *dp)
479 * New collapsed (a)synchronous interface. 479 * New collapsed (a)synchronous interface.
480 * 480 *
481 * If the IO is asynchronous (i.e. it has notify.fn), you must either unplug 481 * If the IO is asynchronous (i.e. it has notify.fn), you must either unplug
482 * the queue with blk_unplug() some time later or set the BIO_RW_SYNC bit in 482 * the queue with blk_unplug() some time later or set REQ_SYNC in
483 * io_req->bi_rw. If you fail to do one of these, the IO will be submitted to 483io_req->bi_rw. If you fail to do one of these, the IO will be submitted to
484 * the disk after q->unplug_delay, which defaults to 3ms in blk-settings.c. 484 * the disk after q->unplug_delay, which defaults to 3ms in blk-settings.c.
485 */ 485 */
486int dm_io(struct dm_io_request *io_req, unsigned num_regions, 486int dm_io(struct dm_io_request *io_req, unsigned num_regions,
diff --git a/drivers/md/dm-kcopyd.c b/drivers/md/dm-kcopyd.c
index addf8347504..d8587bac568 100644
--- a/drivers/md/dm-kcopyd.c
+++ b/drivers/md/dm-kcopyd.c
@@ -345,7 +345,7 @@ static int run_io_job(struct kcopyd_job *job)
345{ 345{
346 int r; 346 int r;
347 struct dm_io_request io_req = { 347 struct dm_io_request io_req = {
348 .bi_rw = job->rw | (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG), 348 .bi_rw = job->rw | REQ_SYNC | REQ_UNPLUG,
349 .mem.type = DM_IO_PAGE_LIST, 349 .mem.type = DM_IO_PAGE_LIST,
350 .mem.ptr.pl = job->pages, 350 .mem.ptr.pl = job->pages,
351 .mem.offset = job->offset, 351 .mem.offset = job->offset,
diff --git a/drivers/md/dm-raid1.c b/drivers/md/dm-raid1.c
index ddda531723d..74136262d65 100644
--- a/drivers/md/dm-raid1.c
+++ b/drivers/md/dm-raid1.c
@@ -1211,7 +1211,7 @@ static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1211 if (error == -EOPNOTSUPP) 1211 if (error == -EOPNOTSUPP)
1212 goto out; 1212 goto out;
1213 1213
1214 if ((error == -EWOULDBLOCK) && bio_rw_flagged(bio, BIO_RW_AHEAD)) 1214 if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
1215 goto out; 1215 goto out;
1216 1216
1217 if (unlikely(error)) { 1217 if (unlikely(error)) {
diff --git a/drivers/md/dm-stripe.c b/drivers/md/dm-stripe.c
index e610725db76..d6e28d732b4 100644
--- a/drivers/md/dm-stripe.c
+++ b/drivers/md/dm-stripe.c
@@ -284,7 +284,7 @@ static int stripe_end_io(struct dm_target *ti, struct bio *bio,
284 if (!error) 284 if (!error)
285 return 0; /* I/O complete */ 285 return 0; /* I/O complete */
286 286
287 if ((error == -EWOULDBLOCK) && bio_rw_flagged(bio, BIO_RW_AHEAD)) 287 if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
288 return error; 288 return error;
289 289
290 if (error == -EOPNOTSUPP) 290 if (error == -EOPNOTSUPP)
diff --git a/drivers/md/dm.c b/drivers/md/dm.c
index d21e1284604..a3f21dc02bd 100644
--- a/drivers/md/dm.c
+++ b/drivers/md/dm.c
@@ -15,6 +15,7 @@
15#include <linux/blkpg.h> 15#include <linux/blkpg.h>
16#include <linux/bio.h> 16#include <linux/bio.h>
17#include <linux/buffer_head.h> 17#include <linux/buffer_head.h>
18#include <linux/smp_lock.h>
18#include <linux/mempool.h> 19#include <linux/mempool.h>
19#include <linux/slab.h> 20#include <linux/slab.h>
20#include <linux/idr.h> 21#include <linux/idr.h>
@@ -338,6 +339,7 @@ static int dm_blk_open(struct block_device *bdev, fmode_t mode)
338{ 339{
339 struct mapped_device *md; 340 struct mapped_device *md;
340 341
342 lock_kernel();
341 spin_lock(&_minor_lock); 343 spin_lock(&_minor_lock);
342 344
343 md = bdev->bd_disk->private_data; 345 md = bdev->bd_disk->private_data;
@@ -355,6 +357,7 @@ static int dm_blk_open(struct block_device *bdev, fmode_t mode)
355 357
356out: 358out:
357 spin_unlock(&_minor_lock); 359 spin_unlock(&_minor_lock);
360 unlock_kernel();
358 361
359 return md ? 0 : -ENXIO; 362 return md ? 0 : -ENXIO;
360} 363}
@@ -362,8 +365,12 @@ out:
362static int dm_blk_close(struct gendisk *disk, fmode_t mode) 365static int dm_blk_close(struct gendisk *disk, fmode_t mode)
363{ 366{
364 struct mapped_device *md = disk->private_data; 367 struct mapped_device *md = disk->private_data;
368
369 lock_kernel();
365 atomic_dec(&md->open_count); 370 atomic_dec(&md->open_count);
366 dm_put(md); 371 dm_put(md);
372 unlock_kernel();
373
367 return 0; 374 return 0;
368} 375}
369 376
@@ -614,7 +621,7 @@ static void dec_pending(struct dm_io *io, int error)
614 */ 621 */
615 spin_lock_irqsave(&md->deferred_lock, flags); 622 spin_lock_irqsave(&md->deferred_lock, flags);
616 if (__noflush_suspending(md)) { 623 if (__noflush_suspending(md)) {
617 if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER)) 624 if (!(io->bio->bi_rw & REQ_HARDBARRIER))
618 bio_list_add_head(&md->deferred, 625 bio_list_add_head(&md->deferred,
619 io->bio); 626 io->bio);
620 } else 627 } else
@@ -626,7 +633,7 @@ static void dec_pending(struct dm_io *io, int error)
626 io_error = io->error; 633 io_error = io->error;
627 bio = io->bio; 634 bio = io->bio;
628 635
629 if (bio_rw_flagged(bio, BIO_RW_BARRIER)) { 636 if (bio->bi_rw & REQ_HARDBARRIER) {
630 /* 637 /*
631 * There can be just one barrier request so we use 638 * There can be just one barrier request so we use
632 * a per-device variable for error reporting. 639 * a per-device variable for error reporting.
@@ -792,12 +799,12 @@ static void dm_end_request(struct request *clone, int error)
792{ 799{
793 int rw = rq_data_dir(clone); 800 int rw = rq_data_dir(clone);
794 int run_queue = 1; 801 int run_queue = 1;
795 bool is_barrier = blk_barrier_rq(clone); 802 bool is_barrier = clone->cmd_flags & REQ_HARDBARRIER;
796 struct dm_rq_target_io *tio = clone->end_io_data; 803 struct dm_rq_target_io *tio = clone->end_io_data;
797 struct mapped_device *md = tio->md; 804 struct mapped_device *md = tio->md;
798 struct request *rq = tio->orig; 805 struct request *rq = tio->orig;
799 806
800 if (blk_pc_request(rq) && !is_barrier) { 807 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !is_barrier) {
801 rq->errors = clone->errors; 808 rq->errors = clone->errors;
802 rq->resid_len = clone->resid_len; 809 rq->resid_len = clone->resid_len;
803 810
@@ -844,7 +851,7 @@ void dm_requeue_unmapped_request(struct request *clone)
844 struct request_queue *q = rq->q; 851 struct request_queue *q = rq->q;
845 unsigned long flags; 852 unsigned long flags;
846 853
847 if (unlikely(blk_barrier_rq(clone))) { 854 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
848 /* 855 /*
849 * Barrier clones share an original request. 856 * Barrier clones share an original request.
850 * Leave it to dm_end_request(), which handles this special 857 * Leave it to dm_end_request(), which handles this special
@@ -943,7 +950,7 @@ static void dm_complete_request(struct request *clone, int error)
943 struct dm_rq_target_io *tio = clone->end_io_data; 950 struct dm_rq_target_io *tio = clone->end_io_data;
944 struct request *rq = tio->orig; 951 struct request *rq = tio->orig;
945 952
946 if (unlikely(blk_barrier_rq(clone))) { 953 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
947 /* 954 /*
948 * Barrier clones share an original request. So can't use 955 * Barrier clones share an original request. So can't use
949 * softirq_done with the original. 956 * softirq_done with the original.
@@ -972,7 +979,7 @@ void dm_kill_unmapped_request(struct request *clone, int error)
972 struct dm_rq_target_io *tio = clone->end_io_data; 979 struct dm_rq_target_io *tio = clone->end_io_data;
973 struct request *rq = tio->orig; 980 struct request *rq = tio->orig;
974 981
975 if (unlikely(blk_barrier_rq(clone))) { 982 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
976 /* 983 /*
977 * Barrier clones share an original request. 984 * Barrier clones share an original request.
978 * Leave it to dm_end_request(), which handles this special 985 * Leave it to dm_end_request(), which handles this special
@@ -1106,7 +1113,7 @@ static struct bio *split_bvec(struct bio *bio, sector_t sector,
1106 1113
1107 clone->bi_sector = sector; 1114 clone->bi_sector = sector;
1108 clone->bi_bdev = bio->bi_bdev; 1115 clone->bi_bdev = bio->bi_bdev;
1109 clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER); 1116 clone->bi_rw = bio->bi_rw & ~REQ_HARDBARRIER;
1110 clone->bi_vcnt = 1; 1117 clone->bi_vcnt = 1;
1111 clone->bi_size = to_bytes(len); 1118 clone->bi_size = to_bytes(len);
1112 clone->bi_io_vec->bv_offset = offset; 1119 clone->bi_io_vec->bv_offset = offset;
@@ -1133,7 +1140,7 @@ static struct bio *clone_bio(struct bio *bio, sector_t sector,
1133 1140
1134 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); 1141 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1135 __bio_clone(clone, bio); 1142 __bio_clone(clone, bio);
1136 clone->bi_rw &= ~(1 << BIO_RW_BARRIER); 1143 clone->bi_rw &= ~REQ_HARDBARRIER;
1137 clone->bi_destructor = dm_bio_destructor; 1144 clone->bi_destructor = dm_bio_destructor;
1138 clone->bi_sector = sector; 1145 clone->bi_sector = sector;
1139 clone->bi_idx = idx; 1146 clone->bi_idx = idx;
@@ -1301,7 +1308,7 @@ static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
1301 1308
1302 ci.map = dm_get_live_table(md); 1309 ci.map = dm_get_live_table(md);
1303 if (unlikely(!ci.map)) { 1310 if (unlikely(!ci.map)) {
1304 if (!bio_rw_flagged(bio, BIO_RW_BARRIER)) 1311 if (!(bio->bi_rw & REQ_HARDBARRIER))
1305 bio_io_error(bio); 1312 bio_io_error(bio);
1306 else 1313 else
1307 if (!md->barrier_error) 1314 if (!md->barrier_error)
@@ -1414,7 +1421,7 @@ static int _dm_request(struct request_queue *q, struct bio *bio)
1414 * we have to queue this io for later. 1421 * we have to queue this io for later.
1415 */ 1422 */
1416 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) || 1423 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
1417 unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) { 1424 unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
1418 up_read(&md->io_lock); 1425 up_read(&md->io_lock);
1419 1426
1420 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) && 1427 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
@@ -1455,20 +1462,9 @@ static int dm_request(struct request_queue *q, struct bio *bio)
1455 return _dm_request(q, bio); 1462 return _dm_request(q, bio);
1456} 1463}
1457 1464
1458/*
1459 * Mark this request as flush request, so that dm_request_fn() can
1460 * recognize.
1461 */
1462static void dm_rq_prepare_flush(struct request_queue *q, struct request *rq)
1463{
1464 rq->cmd_type = REQ_TYPE_LINUX_BLOCK;
1465 rq->cmd[0] = REQ_LB_OP_FLUSH;
1466}
1467
1468static bool dm_rq_is_flush_request(struct request *rq) 1465static bool dm_rq_is_flush_request(struct request *rq)
1469{ 1466{
1470 if (rq->cmd_type == REQ_TYPE_LINUX_BLOCK && 1467 if (rq->cmd_flags & REQ_FLUSH)
1471 rq->cmd[0] == REQ_LB_OP_FLUSH)
1472 return true; 1468 return true;
1473 else 1469 else
1474 return false; 1470 return false;
@@ -1912,8 +1908,7 @@ static struct mapped_device *alloc_dev(int minor)
1912 blk_queue_softirq_done(md->queue, dm_softirq_done); 1908 blk_queue_softirq_done(md->queue, dm_softirq_done);
1913 blk_queue_prep_rq(md->queue, dm_prep_fn); 1909 blk_queue_prep_rq(md->queue, dm_prep_fn);
1914 blk_queue_lld_busy(md->queue, dm_lld_busy); 1910 blk_queue_lld_busy(md->queue, dm_lld_busy);
1915 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH, 1911 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH);
1916 dm_rq_prepare_flush);
1917 1912
1918 md->disk = alloc_disk(1); 1913 md->disk = alloc_disk(1);
1919 if (!md->disk) 1914 if (!md->disk)
@@ -2296,7 +2291,7 @@ static void dm_wq_work(struct work_struct *work)
2296 if (dm_request_based(md)) 2291 if (dm_request_based(md))
2297 generic_make_request(c); 2292 generic_make_request(c);
2298 else { 2293 else {
2299 if (bio_rw_flagged(c, BIO_RW_BARRIER)) 2294 if (c->bi_rw & REQ_HARDBARRIER)
2300 process_barrier(md, c); 2295 process_barrier(md, c);
2301 else 2296 else
2302 __split_and_process_bio(md, c); 2297 __split_and_process_bio(md, c);
diff --git a/drivers/md/linear.c b/drivers/md/linear.c
index 7e0e057db9a..ba19060bcf3 100644
--- a/drivers/md/linear.c
+++ b/drivers/md/linear.c
@@ -294,7 +294,7 @@ static int linear_make_request (mddev_t *mddev, struct bio *bio)
294 dev_info_t *tmp_dev; 294 dev_info_t *tmp_dev;
295 sector_t start_sector; 295 sector_t start_sector;
296 296
297 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) { 297 if (unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
298 md_barrier_request(mddev, bio); 298 md_barrier_request(mddev, bio);
299 return 0; 299 return 0;
300 } 300 }
diff --git a/drivers/md/md.c b/drivers/md/md.c
index cb20d0b0555..11567c7999a 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -36,6 +36,7 @@
36#include <linux/blkdev.h> 36#include <linux/blkdev.h>
37#include <linux/sysctl.h> 37#include <linux/sysctl.h>
38#include <linux/seq_file.h> 38#include <linux/seq_file.h>
39#include <linux/smp_lock.h>
39#include <linux/buffer_head.h> /* for invalidate_bdev */ 40#include <linux/buffer_head.h> /* for invalidate_bdev */
40#include <linux/poll.h> 41#include <linux/poll.h>
41#include <linux/ctype.h> 42#include <linux/ctype.h>
@@ -261,7 +262,7 @@ static int md_make_request(struct request_queue *q, struct bio *bio)
261 * Once ->stop is called and completes, the module will be completely 262 * Once ->stop is called and completes, the module will be completely
262 * unused. 263 * unused.
263 */ 264 */
264static void mddev_suspend(mddev_t *mddev) 265void mddev_suspend(mddev_t *mddev)
265{ 266{
266 BUG_ON(mddev->suspended); 267 BUG_ON(mddev->suspended);
267 mddev->suspended = 1; 268 mddev->suspended = 1;
@@ -269,13 +270,15 @@ static void mddev_suspend(mddev_t *mddev)
269 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); 270 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
270 mddev->pers->quiesce(mddev, 1); 271 mddev->pers->quiesce(mddev, 1);
271} 272}
273EXPORT_SYMBOL_GPL(mddev_suspend);
272 274
273static void mddev_resume(mddev_t *mddev) 275void mddev_resume(mddev_t *mddev)
274{ 276{
275 mddev->suspended = 0; 277 mddev->suspended = 0;
276 wake_up(&mddev->sb_wait); 278 wake_up(&mddev->sb_wait);
277 mddev->pers->quiesce(mddev, 0); 279 mddev->pers->quiesce(mddev, 0);
278} 280}
281EXPORT_SYMBOL_GPL(mddev_resume);
279 282
280int mddev_congested(mddev_t *mddev, int bits) 283int mddev_congested(mddev_t *mddev, int bits)
281{ 284{
@@ -353,7 +356,7 @@ static void md_submit_barrier(struct work_struct *ws)
353 /* an empty barrier - all done */ 356 /* an empty barrier - all done */
354 bio_endio(bio, 0); 357 bio_endio(bio, 0);
355 else { 358 else {
356 bio->bi_rw &= ~(1<<BIO_RW_BARRIER); 359 bio->bi_rw &= ~REQ_HARDBARRIER;
357 if (mddev->pers->make_request(mddev, bio)) 360 if (mddev->pers->make_request(mddev, bio))
358 generic_make_request(bio); 361 generic_make_request(bio);
359 mddev->barrier = POST_REQUEST_BARRIER; 362 mddev->barrier = POST_REQUEST_BARRIER;
@@ -384,6 +387,51 @@ void md_barrier_request(mddev_t *mddev, struct bio *bio)
384} 387}
385EXPORT_SYMBOL(md_barrier_request); 388EXPORT_SYMBOL(md_barrier_request);
386 389
390/* Support for plugging.
391 * This mirrors the plugging support in request_queue, but does not
392 * require having a whole queue
393 */
394static void plugger_work(struct work_struct *work)
395{
396 struct plug_handle *plug =
397 container_of(work, struct plug_handle, unplug_work);
398 plug->unplug_fn(plug);
399}
400static void plugger_timeout(unsigned long data)
401{
402 struct plug_handle *plug = (void *)data;
403 kblockd_schedule_work(NULL, &plug->unplug_work);
404}
405void plugger_init(struct plug_handle *plug,
406 void (*unplug_fn)(struct plug_handle *))
407{
408 plug->unplug_flag = 0;
409 plug->unplug_fn = unplug_fn;
410 init_timer(&plug->unplug_timer);
411 plug->unplug_timer.function = plugger_timeout;
412 plug->unplug_timer.data = (unsigned long)plug;
413 INIT_WORK(&plug->unplug_work, plugger_work);
414}
415EXPORT_SYMBOL_GPL(plugger_init);
416
417void plugger_set_plug(struct plug_handle *plug)
418{
419 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
420 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
421}
422EXPORT_SYMBOL_GPL(plugger_set_plug);
423
424int plugger_remove_plug(struct plug_handle *plug)
425{
426 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
427 del_timer(&plug->unplug_timer);
428 return 1;
429 } else
430 return 0;
431}
432EXPORT_SYMBOL_GPL(plugger_remove_plug);
433
434
387static inline mddev_t *mddev_get(mddev_t *mddev) 435static inline mddev_t *mddev_get(mddev_t *mddev)
388{ 436{
389 atomic_inc(&mddev->active); 437 atomic_inc(&mddev->active);
@@ -416,7 +464,7 @@ static void mddev_put(mddev_t *mddev)
416 spin_unlock(&all_mddevs_lock); 464 spin_unlock(&all_mddevs_lock);
417} 465}
418 466
419static void mddev_init(mddev_t *mddev) 467void mddev_init(mddev_t *mddev)
420{ 468{
421 mutex_init(&mddev->open_mutex); 469 mutex_init(&mddev->open_mutex);
422 mutex_init(&mddev->reconfig_mutex); 470 mutex_init(&mddev->reconfig_mutex);
@@ -436,6 +484,7 @@ static void mddev_init(mddev_t *mddev)
436 mddev->resync_max = MaxSector; 484 mddev->resync_max = MaxSector;
437 mddev->level = LEVEL_NONE; 485 mddev->level = LEVEL_NONE;
438} 486}
487EXPORT_SYMBOL_GPL(mddev_init);
439 488
440static mddev_t * mddev_find(dev_t unit) 489static mddev_t * mddev_find(dev_t unit)
441{ 490{
@@ -532,25 +581,31 @@ static void mddev_unlock(mddev_t * mddev)
532 * an access to the files will try to take reconfig_mutex 581 * an access to the files will try to take reconfig_mutex
533 * while holding the file unremovable, which leads to 582 * while holding the file unremovable, which leads to
534 * a deadlock. 583 * a deadlock.
535 * So hold open_mutex instead - we are allowed to take 584 * So hold set sysfs_active while the remove in happeing,
536 * it while holding reconfig_mutex, and md_run can 585 * and anything else which might set ->to_remove or my
537 * use it to wait for the remove to complete. 586 * otherwise change the sysfs namespace will fail with
587 * -EBUSY if sysfs_active is still set.
588 * We set sysfs_active under reconfig_mutex and elsewhere
589 * test it under the same mutex to ensure its correct value
590 * is seen.
538 */ 591 */
539 struct attribute_group *to_remove = mddev->to_remove; 592 struct attribute_group *to_remove = mddev->to_remove;
540 mddev->to_remove = NULL; 593 mddev->to_remove = NULL;
541 mutex_lock(&mddev->open_mutex); 594 mddev->sysfs_active = 1;
542 mutex_unlock(&mddev->reconfig_mutex); 595 mutex_unlock(&mddev->reconfig_mutex);
543 596
544 if (to_remove != &md_redundancy_group) 597 if (mddev->kobj.sd) {
545 sysfs_remove_group(&mddev->kobj, to_remove); 598 if (to_remove != &md_redundancy_group)
546 if (mddev->pers == NULL || 599 sysfs_remove_group(&mddev->kobj, to_remove);
547 mddev->pers->sync_request == NULL) { 600 if (mddev->pers == NULL ||
548 sysfs_remove_group(&mddev->kobj, &md_redundancy_group); 601 mddev->pers->sync_request == NULL) {
549 if (mddev->sysfs_action) 602 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
550 sysfs_put(mddev->sysfs_action); 603 if (mddev->sysfs_action)
551 mddev->sysfs_action = NULL; 604 sysfs_put(mddev->sysfs_action);
605 mddev->sysfs_action = NULL;
606 }
552 } 607 }
553 mutex_unlock(&mddev->open_mutex); 608 mddev->sysfs_active = 0;
554 } else 609 } else
555 mutex_unlock(&mddev->reconfig_mutex); 610 mutex_unlock(&mddev->reconfig_mutex);
556 611
@@ -675,11 +730,11 @@ void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
675 * if zero is reached. 730 * if zero is reached.
676 * If an error occurred, call md_error 731 * If an error occurred, call md_error
677 * 732 *
678 * As we might need to resubmit the request if BIO_RW_BARRIER 733 * As we might need to resubmit the request if REQ_HARDBARRIER
679 * causes ENOTSUPP, we allocate a spare bio... 734 * causes ENOTSUPP, we allocate a spare bio...
680 */ 735 */
681 struct bio *bio = bio_alloc(GFP_NOIO, 1); 736 struct bio *bio = bio_alloc(GFP_NOIO, 1);
682 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); 737 int rw = REQ_WRITE | REQ_SYNC | REQ_UNPLUG;
683 738
684 bio->bi_bdev = rdev->bdev; 739 bio->bi_bdev = rdev->bdev;
685 bio->bi_sector = sector; 740 bio->bi_sector = sector;
@@ -691,7 +746,7 @@ void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
691 atomic_inc(&mddev->pending_writes); 746 atomic_inc(&mddev->pending_writes);
692 if (!test_bit(BarriersNotsupp, &rdev->flags)) { 747 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
693 struct bio *rbio; 748 struct bio *rbio;
694 rw |= (1<<BIO_RW_BARRIER); 749 rw |= REQ_HARDBARRIER;
695 rbio = bio_clone(bio, GFP_NOIO); 750 rbio = bio_clone(bio, GFP_NOIO);
696 rbio->bi_private = bio; 751 rbio->bi_private = bio;
697 rbio->bi_end_io = super_written_barrier; 752 rbio->bi_end_io = super_written_barrier;
@@ -736,7 +791,7 @@ int sync_page_io(struct block_device *bdev, sector_t sector, int size,
736 struct completion event; 791 struct completion event;
737 int ret; 792 int ret;
738 793
739 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); 794 rw |= REQ_SYNC | REQ_UNPLUG;
740 795
741 bio->bi_bdev = bdev; 796 bio->bi_bdev = bdev;
742 bio->bi_sector = sector; 797 bio->bi_sector = sector;
@@ -1811,11 +1866,9 @@ static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1811 goto fail; 1866 goto fail;
1812 1867
1813 ko = &part_to_dev(rdev->bdev->bd_part)->kobj; 1868 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1814 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { 1869 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1815 kobject_del(&rdev->kobj); 1870 /* failure here is OK */;
1816 goto fail; 1871 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
1817 }
1818 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, NULL, "state");
1819 1872
1820 list_add_rcu(&rdev->same_set, &mddev->disks); 1873 list_add_rcu(&rdev->same_set, &mddev->disks);
1821 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); 1874 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
@@ -2334,8 +2387,8 @@ state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2334 set_bit(In_sync, &rdev->flags); 2387 set_bit(In_sync, &rdev->flags);
2335 err = 0; 2388 err = 0;
2336 } 2389 }
2337 if (!err && rdev->sysfs_state) 2390 if (!err)
2338 sysfs_notify_dirent(rdev->sysfs_state); 2391 sysfs_notify_dirent_safe(rdev->sysfs_state);
2339 return err ? err : len; 2392 return err ? err : len;
2340} 2393}
2341static struct rdev_sysfs_entry rdev_state = 2394static struct rdev_sysfs_entry rdev_state =
@@ -2430,14 +2483,10 @@ slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2430 rdev->raid_disk = -1; 2483 rdev->raid_disk = -1;
2431 return err; 2484 return err;
2432 } else 2485 } else
2433 sysfs_notify_dirent(rdev->sysfs_state); 2486 sysfs_notify_dirent_safe(rdev->sysfs_state);
2434 sprintf(nm, "rd%d", rdev->raid_disk); 2487 sprintf(nm, "rd%d", rdev->raid_disk);
2435 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) 2488 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2436 printk(KERN_WARNING 2489 /* failure here is OK */;
2437 "md: cannot register "
2438 "%s for %s\n",
2439 nm, mdname(rdev->mddev));
2440
2441 /* don't wakeup anyone, leave that to userspace. */ 2490 /* don't wakeup anyone, leave that to userspace. */
2442 } else { 2491 } else {
2443 if (slot >= rdev->mddev->raid_disks) 2492 if (slot >= rdev->mddev->raid_disks)
@@ -2447,7 +2496,7 @@ slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2447 clear_bit(Faulty, &rdev->flags); 2496 clear_bit(Faulty, &rdev->flags);
2448 clear_bit(WriteMostly, &rdev->flags); 2497 clear_bit(WriteMostly, &rdev->flags);
2449 set_bit(In_sync, &rdev->flags); 2498 set_bit(In_sync, &rdev->flags);
2450 sysfs_notify_dirent(rdev->sysfs_state); 2499 sysfs_notify_dirent_safe(rdev->sysfs_state);
2451 } 2500 }
2452 return len; 2501 return len;
2453} 2502}
@@ -2695,6 +2744,24 @@ static struct kobj_type rdev_ktype = {
2695 .default_attrs = rdev_default_attrs, 2744 .default_attrs = rdev_default_attrs,
2696}; 2745};
2697 2746
2747void md_rdev_init(mdk_rdev_t *rdev)
2748{
2749 rdev->desc_nr = -1;
2750 rdev->saved_raid_disk = -1;
2751 rdev->raid_disk = -1;
2752 rdev->flags = 0;
2753 rdev->data_offset = 0;
2754 rdev->sb_events = 0;
2755 rdev->last_read_error.tv_sec = 0;
2756 rdev->last_read_error.tv_nsec = 0;
2757 atomic_set(&rdev->nr_pending, 0);
2758 atomic_set(&rdev->read_errors, 0);
2759 atomic_set(&rdev->corrected_errors, 0);
2760
2761 INIT_LIST_HEAD(&rdev->same_set);
2762 init_waitqueue_head(&rdev->blocked_wait);
2763}
2764EXPORT_SYMBOL_GPL(md_rdev_init);
2698/* 2765/*
2699 * Import a device. If 'super_format' >= 0, then sanity check the superblock 2766 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2700 * 2767 *
@@ -2718,6 +2785,7 @@ static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_mi
2718 return ERR_PTR(-ENOMEM); 2785 return ERR_PTR(-ENOMEM);
2719 } 2786 }
2720 2787
2788 md_rdev_init(rdev);
2721 if ((err = alloc_disk_sb(rdev))) 2789 if ((err = alloc_disk_sb(rdev)))
2722 goto abort_free; 2790 goto abort_free;
2723 2791
@@ -2727,18 +2795,6 @@ static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_mi
2727 2795
2728 kobject_init(&rdev->kobj, &rdev_ktype); 2796 kobject_init(&rdev->kobj, &rdev_ktype);
2729 2797
2730 rdev->desc_nr = -1;
2731 rdev->saved_raid_disk = -1;
2732 rdev->raid_disk = -1;
2733 rdev->flags = 0;
2734 rdev->data_offset = 0;
2735 rdev->sb_events = 0;
2736 rdev->last_read_error.tv_sec = 0;
2737 rdev->last_read_error.tv_nsec = 0;
2738 atomic_set(&rdev->nr_pending, 0);
2739 atomic_set(&rdev->read_errors, 0);
2740 atomic_set(&rdev->corrected_errors, 0);
2741
2742 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; 2798 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2743 if (!size) { 2799 if (!size) {
2744 printk(KERN_WARNING 2800 printk(KERN_WARNING
@@ -2767,9 +2823,6 @@ static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_mi
2767 } 2823 }
2768 } 2824 }
2769 2825
2770 INIT_LIST_HEAD(&rdev->same_set);
2771 init_waitqueue_head(&rdev->blocked_wait);
2772
2773 return rdev; 2826 return rdev;
2774 2827
2775abort_free: 2828abort_free:
@@ -2960,7 +3013,9 @@ level_store(mddev_t *mddev, const char *buf, size_t len)
2960 * - new personality will access other array. 3013 * - new personality will access other array.
2961 */ 3014 */
2962 3015
2963 if (mddev->sync_thread || mddev->reshape_position != MaxSector) 3016 if (mddev->sync_thread ||
3017 mddev->reshape_position != MaxSector ||
3018 mddev->sysfs_active)
2964 return -EBUSY; 3019 return -EBUSY;
2965 3020
2966 if (!mddev->pers->quiesce) { 3021 if (!mddev->pers->quiesce) {
@@ -3437,7 +3492,7 @@ array_state_store(mddev_t *mddev, const char *buf, size_t len)
3437 if (err) 3492 if (err)
3438 return err; 3493 return err;
3439 else { 3494 else {
3440 sysfs_notify_dirent(mddev->sysfs_state); 3495 sysfs_notify_dirent_safe(mddev->sysfs_state);
3441 return len; 3496 return len;
3442 } 3497 }
3443} 3498}
@@ -3735,7 +3790,7 @@ action_store(mddev_t *mddev, const char *page, size_t len)
3735 } 3790 }
3736 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 3791 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3737 md_wakeup_thread(mddev->thread); 3792 md_wakeup_thread(mddev->thread);
3738 sysfs_notify_dirent(mddev->sysfs_action); 3793 sysfs_notify_dirent_safe(mddev->sysfs_action);
3739 return len; 3794 return len;
3740} 3795}
3741 3796
@@ -4281,13 +4336,14 @@ static int md_alloc(dev_t dev, char *name)
4281 disk->disk_name); 4336 disk->disk_name);
4282 error = 0; 4337 error = 0;
4283 } 4338 }
4284 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) 4339 if (mddev->kobj.sd &&
4340 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4285 printk(KERN_DEBUG "pointless warning\n"); 4341 printk(KERN_DEBUG "pointless warning\n");
4286 abort: 4342 abort:
4287 mutex_unlock(&disks_mutex); 4343 mutex_unlock(&disks_mutex);
4288 if (!error) { 4344 if (!error && mddev->kobj.sd) {
4289 kobject_uevent(&mddev->kobj, KOBJ_ADD); 4345 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4290 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, NULL, "array_state"); 4346 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4291 } 4347 }
4292 mddev_put(mddev); 4348 mddev_put(mddev);
4293 return error; 4349 return error;
@@ -4325,14 +4381,14 @@ static void md_safemode_timeout(unsigned long data)
4325 if (!atomic_read(&mddev->writes_pending)) { 4381 if (!atomic_read(&mddev->writes_pending)) {
4326 mddev->safemode = 1; 4382 mddev->safemode = 1;
4327 if (mddev->external) 4383 if (mddev->external)
4328 sysfs_notify_dirent(mddev->sysfs_state); 4384 sysfs_notify_dirent_safe(mddev->sysfs_state);
4329 } 4385 }
4330 md_wakeup_thread(mddev->thread); 4386 md_wakeup_thread(mddev->thread);
4331} 4387}
4332 4388
4333static int start_dirty_degraded; 4389static int start_dirty_degraded;
4334 4390
4335static int md_run(mddev_t *mddev) 4391int md_run(mddev_t *mddev)
4336{ 4392{
4337 int err; 4393 int err;
4338 mdk_rdev_t *rdev; 4394 mdk_rdev_t *rdev;
@@ -4344,13 +4400,9 @@ static int md_run(mddev_t *mddev)
4344 4400
4345 if (mddev->pers) 4401 if (mddev->pers)
4346 return -EBUSY; 4402 return -EBUSY;
4347 4403 /* Cannot run until previous stop completes properly */
4348 /* These two calls synchronise us with the 4404 if (mddev->sysfs_active)
4349 * sysfs_remove_group calls in mddev_unlock, 4405 return -EBUSY;
4350 * so they must have completed.
4351 */
4352 mutex_lock(&mddev->open_mutex);
4353 mutex_unlock(&mddev->open_mutex);
4354 4406
4355 /* 4407 /*
4356 * Analyze all RAID superblock(s) 4408 * Analyze all RAID superblock(s)
@@ -4397,7 +4449,7 @@ static int md_run(mddev_t *mddev)
4397 return -EINVAL; 4449 return -EINVAL;
4398 } 4450 }
4399 } 4451 }
4400 sysfs_notify_dirent(rdev->sysfs_state); 4452 sysfs_notify_dirent_safe(rdev->sysfs_state);
4401 } 4453 }
4402 4454
4403 spin_lock(&pers_lock); 4455 spin_lock(&pers_lock);
@@ -4496,11 +4548,12 @@ static int md_run(mddev_t *mddev)
4496 return err; 4548 return err;
4497 } 4549 }
4498 if (mddev->pers->sync_request) { 4550 if (mddev->pers->sync_request) {
4499 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) 4551 if (mddev->kobj.sd &&
4552 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4500 printk(KERN_WARNING 4553 printk(KERN_WARNING
4501 "md: cannot register extra attributes for %s\n", 4554 "md: cannot register extra attributes for %s\n",
4502 mdname(mddev)); 4555 mdname(mddev));
4503 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); 4556 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
4504 } else if (mddev->ro == 2) /* auto-readonly not meaningful */ 4557 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4505 mddev->ro = 0; 4558 mddev->ro = 0;
4506 4559
@@ -4518,8 +4571,7 @@ static int md_run(mddev_t *mddev)
4518 char nm[20]; 4571 char nm[20];
4519 sprintf(nm, "rd%d", rdev->raid_disk); 4572 sprintf(nm, "rd%d", rdev->raid_disk);
4520 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) 4573 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4521 printk("md: cannot register %s for %s\n", 4574 /* failure here is OK */;
4522 nm, mdname(mddev));
4523 } 4575 }
4524 4576
4525 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 4577 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
@@ -4531,12 +4583,12 @@ static int md_run(mddev_t *mddev)
4531 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ 4583 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4532 4584
4533 md_new_event(mddev); 4585 md_new_event(mddev);
4534 sysfs_notify_dirent(mddev->sysfs_state); 4586 sysfs_notify_dirent_safe(mddev->sysfs_state);
4535 if (mddev->sysfs_action) 4587 sysfs_notify_dirent_safe(mddev->sysfs_action);
4536 sysfs_notify_dirent(mddev->sysfs_action);
4537 sysfs_notify(&mddev->kobj, NULL, "degraded"); 4588 sysfs_notify(&mddev->kobj, NULL, "degraded");
4538 return 0; 4589 return 0;
4539} 4590}
4591EXPORT_SYMBOL_GPL(md_run);
4540 4592
4541static int do_md_run(mddev_t *mddev) 4593static int do_md_run(mddev_t *mddev)
4542{ 4594{
@@ -4545,7 +4597,11 @@ static int do_md_run(mddev_t *mddev)
4545 err = md_run(mddev); 4597 err = md_run(mddev);
4546 if (err) 4598 if (err)
4547 goto out; 4599 goto out;
4548 4600 err = bitmap_load(mddev);
4601 if (err) {
4602 bitmap_destroy(mddev);
4603 goto out;
4604 }
4549 set_capacity(mddev->gendisk, mddev->array_sectors); 4605 set_capacity(mddev->gendisk, mddev->array_sectors);
4550 revalidate_disk(mddev->gendisk); 4606 revalidate_disk(mddev->gendisk);
4551 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); 4607 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
@@ -4573,7 +4629,7 @@ static int restart_array(mddev_t *mddev)
4573 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 4629 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4574 md_wakeup_thread(mddev->thread); 4630 md_wakeup_thread(mddev->thread);
4575 md_wakeup_thread(mddev->sync_thread); 4631 md_wakeup_thread(mddev->sync_thread);
4576 sysfs_notify_dirent(mddev->sysfs_state); 4632 sysfs_notify_dirent_safe(mddev->sysfs_state);
4577 return 0; 4633 return 0;
4578} 4634}
4579 4635
@@ -4644,9 +4700,10 @@ static void md_clean(mddev_t *mddev)
4644 mddev->bitmap_info.chunksize = 0; 4700 mddev->bitmap_info.chunksize = 0;
4645 mddev->bitmap_info.daemon_sleep = 0; 4701 mddev->bitmap_info.daemon_sleep = 0;
4646 mddev->bitmap_info.max_write_behind = 0; 4702 mddev->bitmap_info.max_write_behind = 0;
4703 mddev->plug = NULL;
4647} 4704}
4648 4705
4649static void md_stop_writes(mddev_t *mddev) 4706void md_stop_writes(mddev_t *mddev)
4650{ 4707{
4651 if (mddev->sync_thread) { 4708 if (mddev->sync_thread) {
4652 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 4709 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
@@ -4666,11 +4723,10 @@ static void md_stop_writes(mddev_t *mddev)
4666 md_update_sb(mddev, 1); 4723 md_update_sb(mddev, 1);
4667 } 4724 }
4668} 4725}
4726EXPORT_SYMBOL_GPL(md_stop_writes);
4669 4727
4670static void md_stop(mddev_t *mddev) 4728void md_stop(mddev_t *mddev)
4671{ 4729{
4672 md_stop_writes(mddev);
4673
4674 mddev->pers->stop(mddev); 4730 mddev->pers->stop(mddev);
4675 if (mddev->pers->sync_request && mddev->to_remove == NULL) 4731 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4676 mddev->to_remove = &md_redundancy_group; 4732 mddev->to_remove = &md_redundancy_group;
@@ -4678,6 +4734,7 @@ static void md_stop(mddev_t *mddev)
4678 mddev->pers = NULL; 4734 mddev->pers = NULL;
4679 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 4735 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4680} 4736}
4737EXPORT_SYMBOL_GPL(md_stop);
4681 4738
4682static int md_set_readonly(mddev_t *mddev, int is_open) 4739static int md_set_readonly(mddev_t *mddev, int is_open)
4683{ 4740{
@@ -4697,7 +4754,7 @@ static int md_set_readonly(mddev_t *mddev, int is_open)
4697 mddev->ro = 1; 4754 mddev->ro = 1;
4698 set_disk_ro(mddev->gendisk, 1); 4755 set_disk_ro(mddev->gendisk, 1);
4699 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 4756 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4700 sysfs_notify_dirent(mddev->sysfs_state); 4757 sysfs_notify_dirent_safe(mddev->sysfs_state);
4701 err = 0; 4758 err = 0;
4702 } 4759 }
4703out: 4760out:
@@ -4711,26 +4768,29 @@ out:
4711 */ 4768 */
4712static int do_md_stop(mddev_t * mddev, int mode, int is_open) 4769static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4713{ 4770{
4714 int err = 0;
4715 struct gendisk *disk = mddev->gendisk; 4771 struct gendisk *disk = mddev->gendisk;
4716 mdk_rdev_t *rdev; 4772 mdk_rdev_t *rdev;
4717 4773
4718 mutex_lock(&mddev->open_mutex); 4774 mutex_lock(&mddev->open_mutex);
4719 if (atomic_read(&mddev->openers) > is_open) { 4775 if (atomic_read(&mddev->openers) > is_open ||
4776 mddev->sysfs_active) {
4720 printk("md: %s still in use.\n",mdname(mddev)); 4777 printk("md: %s still in use.\n",mdname(mddev));
4721 err = -EBUSY; 4778 mutex_unlock(&mddev->open_mutex);
4722 } else if (mddev->pers) { 4779 return -EBUSY;
4780 }
4723 4781
4782 if (mddev->pers) {
4724 if (mddev->ro) 4783 if (mddev->ro)
4725 set_disk_ro(disk, 0); 4784 set_disk_ro(disk, 0);
4726 4785
4786 md_stop_writes(mddev);
4727 md_stop(mddev); 4787 md_stop(mddev);
4728 mddev->queue->merge_bvec_fn = NULL; 4788 mddev->queue->merge_bvec_fn = NULL;
4729 mddev->queue->unplug_fn = NULL; 4789 mddev->queue->unplug_fn = NULL;
4730 mddev->queue->backing_dev_info.congested_fn = NULL; 4790 mddev->queue->backing_dev_info.congested_fn = NULL;
4731 4791
4732 /* tell userspace to handle 'inactive' */ 4792 /* tell userspace to handle 'inactive' */
4733 sysfs_notify_dirent(mddev->sysfs_state); 4793 sysfs_notify_dirent_safe(mddev->sysfs_state);
4734 4794
4735 list_for_each_entry(rdev, &mddev->disks, same_set) 4795 list_for_each_entry(rdev, &mddev->disks, same_set)
4736 if (rdev->raid_disk >= 0) { 4796 if (rdev->raid_disk >= 0) {
@@ -4740,21 +4800,17 @@ static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4740 } 4800 }
4741 4801
4742 set_capacity(disk, 0); 4802 set_capacity(disk, 0);
4803 mutex_unlock(&mddev->open_mutex);
4743 revalidate_disk(disk); 4804 revalidate_disk(disk);
4744 4805
4745 if (mddev->ro) 4806 if (mddev->ro)
4746 mddev->ro = 0; 4807 mddev->ro = 0;
4747 4808 } else
4748 err = 0; 4809 mutex_unlock(&mddev->open_mutex);
4749 }
4750 mutex_unlock(&mddev->open_mutex);
4751 if (err)
4752 return err;
4753 /* 4810 /*
4754 * Free resources if final stop 4811 * Free resources if final stop
4755 */ 4812 */
4756 if (mode == 0) { 4813 if (mode == 0) {
4757
4758 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); 4814 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4759 4815
4760 bitmap_destroy(mddev); 4816 bitmap_destroy(mddev);
@@ -4771,13 +4827,11 @@ static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4771 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); 4827 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4772 if (mddev->hold_active == UNTIL_STOP) 4828 if (mddev->hold_active == UNTIL_STOP)
4773 mddev->hold_active = 0; 4829 mddev->hold_active = 0;
4774
4775 } 4830 }
4776 err = 0;
4777 blk_integrity_unregister(disk); 4831 blk_integrity_unregister(disk);
4778 md_new_event(mddev); 4832 md_new_event(mddev);
4779 sysfs_notify_dirent(mddev->sysfs_state); 4833 sysfs_notify_dirent_safe(mddev->sysfs_state);
4780 return err; 4834 return 0;
4781} 4835}
4782 4836
4783#ifndef MODULE 4837#ifndef MODULE
@@ -5138,7 +5192,7 @@ static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5138 if (err) 5192 if (err)
5139 export_rdev(rdev); 5193 export_rdev(rdev);
5140 else 5194 else
5141 sysfs_notify_dirent(rdev->sysfs_state); 5195 sysfs_notify_dirent_safe(rdev->sysfs_state);
5142 5196
5143 md_update_sb(mddev, 1); 5197 md_update_sb(mddev, 1);
5144 if (mddev->degraded) 5198 if (mddev->degraded)
@@ -5331,8 +5385,11 @@ static int set_bitmap_file(mddev_t *mddev, int fd)
5331 err = 0; 5385 err = 0;
5332 if (mddev->pers) { 5386 if (mddev->pers) {
5333 mddev->pers->quiesce(mddev, 1); 5387 mddev->pers->quiesce(mddev, 1);
5334 if (fd >= 0) 5388 if (fd >= 0) {
5335 err = bitmap_create(mddev); 5389 err = bitmap_create(mddev);
5390 if (!err)
5391 err = bitmap_load(mddev);
5392 }
5336 if (fd < 0 || err) { 5393 if (fd < 0 || err) {
5337 bitmap_destroy(mddev); 5394 bitmap_destroy(mddev);
5338 fd = -1; /* make sure to put the file */ 5395 fd = -1; /* make sure to put the file */
@@ -5581,6 +5638,8 @@ static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5581 mddev->bitmap_info.default_offset; 5638 mddev->bitmap_info.default_offset;
5582 mddev->pers->quiesce(mddev, 1); 5639 mddev->pers->quiesce(mddev, 1);
5583 rv = bitmap_create(mddev); 5640 rv = bitmap_create(mddev);
5641 if (!rv)
5642 rv = bitmap_load(mddev);
5584 if (rv) 5643 if (rv)
5585 bitmap_destroy(mddev); 5644 bitmap_destroy(mddev);
5586 mddev->pers->quiesce(mddev, 0); 5645 mddev->pers->quiesce(mddev, 0);
@@ -5813,7 +5872,7 @@ static int md_ioctl(struct block_device *bdev, fmode_t mode,
5813 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { 5872 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5814 if (mddev->ro == 2) { 5873 if (mddev->ro == 2) {
5815 mddev->ro = 0; 5874 mddev->ro = 0;
5816 sysfs_notify_dirent(mddev->sysfs_state); 5875 sysfs_notify_dirent_safe(mddev->sysfs_state);
5817 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 5876 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5818 md_wakeup_thread(mddev->thread); 5877 md_wakeup_thread(mddev->thread);
5819 } else { 5878 } else {
@@ -5902,6 +5961,7 @@ static int md_open(struct block_device *bdev, fmode_t mode)
5902 mddev_t *mddev = mddev_find(bdev->bd_dev); 5961 mddev_t *mddev = mddev_find(bdev->bd_dev);
5903 int err; 5962 int err;
5904 5963
5964 lock_kernel();
5905 if (mddev->gendisk != bdev->bd_disk) { 5965 if (mddev->gendisk != bdev->bd_disk) {
5906 /* we are racing with mddev_put which is discarding this 5966 /* we are racing with mddev_put which is discarding this
5907 * bd_disk. 5967 * bd_disk.
@@ -5910,6 +5970,7 @@ static int md_open(struct block_device *bdev, fmode_t mode)
5910 /* Wait until bdev->bd_disk is definitely gone */ 5970 /* Wait until bdev->bd_disk is definitely gone */
5911 flush_scheduled_work(); 5971 flush_scheduled_work();
5912 /* Then retry the open from the top */ 5972 /* Then retry the open from the top */
5973 unlock_kernel();
5913 return -ERESTARTSYS; 5974 return -ERESTARTSYS;
5914 } 5975 }
5915 BUG_ON(mddev != bdev->bd_disk->private_data); 5976 BUG_ON(mddev != bdev->bd_disk->private_data);
@@ -5923,6 +5984,7 @@ static int md_open(struct block_device *bdev, fmode_t mode)
5923 5984
5924 check_disk_size_change(mddev->gendisk, bdev); 5985 check_disk_size_change(mddev->gendisk, bdev);
5925 out: 5986 out:
5987 unlock_kernel();
5926 return err; 5988 return err;
5927} 5989}
5928 5990
@@ -5931,8 +5993,10 @@ static int md_release(struct gendisk *disk, fmode_t mode)
5931 mddev_t *mddev = disk->private_data; 5993 mddev_t *mddev = disk->private_data;
5932 5994
5933 BUG_ON(!mddev); 5995 BUG_ON(!mddev);
5996 lock_kernel();
5934 atomic_dec(&mddev->openers); 5997 atomic_dec(&mddev->openers);
5935 mddev_put(mddev); 5998 mddev_put(mddev);
5999 unlock_kernel();
5936 6000
5937 return 0; 6001 return 0;
5938} 6002}
@@ -6059,10 +6123,12 @@ void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6059 mddev->pers->error_handler(mddev,rdev); 6123 mddev->pers->error_handler(mddev,rdev);
6060 if (mddev->degraded) 6124 if (mddev->degraded)
6061 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); 6125 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6062 sysfs_notify_dirent(rdev->sysfs_state); 6126 sysfs_notify_dirent_safe(rdev->sysfs_state);
6063 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 6127 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6064 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 6128 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6065 md_wakeup_thread(mddev->thread); 6129 md_wakeup_thread(mddev->thread);
6130 if (mddev->event_work.func)
6131 schedule_work(&mddev->event_work);
6066 md_new_event_inintr(mddev); 6132 md_new_event_inintr(mddev);
6067} 6133}
6068 6134
@@ -6520,7 +6586,7 @@ void md_write_start(mddev_t *mddev, struct bio *bi)
6520 spin_unlock_irq(&mddev->write_lock); 6586 spin_unlock_irq(&mddev->write_lock);
6521 } 6587 }
6522 if (did_change) 6588 if (did_change)
6523 sysfs_notify_dirent(mddev->sysfs_state); 6589 sysfs_notify_dirent_safe(mddev->sysfs_state);
6524 wait_event(mddev->sb_wait, 6590 wait_event(mddev->sb_wait,
6525 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && 6591 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6526 !test_bit(MD_CHANGE_PENDING, &mddev->flags)); 6592 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
@@ -6563,7 +6629,7 @@ int md_allow_write(mddev_t *mddev)
6563 mddev->safemode = 1; 6629 mddev->safemode = 1;
6564 spin_unlock_irq(&mddev->write_lock); 6630 spin_unlock_irq(&mddev->write_lock);
6565 md_update_sb(mddev, 0); 6631 md_update_sb(mddev, 0);
6566 sysfs_notify_dirent(mddev->sysfs_state); 6632 sysfs_notify_dirent_safe(mddev->sysfs_state);
6567 } else 6633 } else
6568 spin_unlock_irq(&mddev->write_lock); 6634 spin_unlock_irq(&mddev->write_lock);
6569 6635
@@ -6574,6 +6640,14 @@ int md_allow_write(mddev_t *mddev)
6574} 6640}
6575EXPORT_SYMBOL_GPL(md_allow_write); 6641EXPORT_SYMBOL_GPL(md_allow_write);
6576 6642
6643void md_unplug(mddev_t *mddev)
6644{
6645 if (mddev->queue)
6646 blk_unplug(mddev->queue);
6647 if (mddev->plug)
6648 mddev->plug->unplug_fn(mddev->plug);
6649}
6650
6577#define SYNC_MARKS 10 6651#define SYNC_MARKS 10
6578#define SYNC_MARK_STEP (3*HZ) 6652#define SYNC_MARK_STEP (3*HZ)
6579void md_do_sync(mddev_t *mddev) 6653void md_do_sync(mddev_t *mddev)
@@ -6752,12 +6826,13 @@ void md_do_sync(mddev_t *mddev)
6752 >= mddev->resync_max - mddev->curr_resync_completed 6826 >= mddev->resync_max - mddev->curr_resync_completed
6753 )) { 6827 )) {
6754 /* time to update curr_resync_completed */ 6828 /* time to update curr_resync_completed */
6755 blk_unplug(mddev->queue); 6829 md_unplug(mddev);
6756 wait_event(mddev->recovery_wait, 6830 wait_event(mddev->recovery_wait,
6757 atomic_read(&mddev->recovery_active) == 0); 6831 atomic_read(&mddev->recovery_active) == 0);
6758 mddev->curr_resync_completed = 6832 mddev->curr_resync_completed =
6759 mddev->curr_resync; 6833 mddev->curr_resync;
6760 set_bit(MD_CHANGE_CLEAN, &mddev->flags); 6834 if (mddev->persistent)
6835 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6761 sysfs_notify(&mddev->kobj, NULL, "sync_completed"); 6836 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6762 } 6837 }
6763 6838
@@ -6829,7 +6904,7 @@ void md_do_sync(mddev_t *mddev)
6829 * about not overloading the IO subsystem. (things like an 6904 * about not overloading the IO subsystem. (things like an
6830 * e2fsck being done on the RAID array should execute fast) 6905 * e2fsck being done on the RAID array should execute fast)
6831 */ 6906 */
6832 blk_unplug(mddev->queue); 6907 md_unplug(mddev);
6833 cond_resched(); 6908 cond_resched();
6834 6909
6835 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 6910 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
@@ -6848,7 +6923,7 @@ void md_do_sync(mddev_t *mddev)
6848 * this also signals 'finished resyncing' to md_stop 6923 * this also signals 'finished resyncing' to md_stop
6849 */ 6924 */
6850 out: 6925 out:
6851 blk_unplug(mddev->queue); 6926 md_unplug(mddev);
6852 6927
6853 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); 6928 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6854 6929
@@ -6950,10 +7025,7 @@ static int remove_and_add_spares(mddev_t *mddev)
6950 sprintf(nm, "rd%d", rdev->raid_disk); 7025 sprintf(nm, "rd%d", rdev->raid_disk);
6951 if (sysfs_create_link(&mddev->kobj, 7026 if (sysfs_create_link(&mddev->kobj,
6952 &rdev->kobj, nm)) 7027 &rdev->kobj, nm))
6953 printk(KERN_WARNING 7028 /* failure here is OK */;
6954 "md: cannot register "
6955 "%s for %s\n",
6956 nm, mdname(mddev));
6957 spares++; 7029 spares++;
6958 md_new_event(mddev); 7030 md_new_event(mddev);
6959 set_bit(MD_CHANGE_DEVS, &mddev->flags); 7031 set_bit(MD_CHANGE_DEVS, &mddev->flags);
@@ -7046,7 +7118,7 @@ void md_check_recovery(mddev_t *mddev)
7046 mddev->safemode = 0; 7118 mddev->safemode = 0;
7047 spin_unlock_irq(&mddev->write_lock); 7119 spin_unlock_irq(&mddev->write_lock);
7048 if (did_change) 7120 if (did_change)
7049 sysfs_notify_dirent(mddev->sysfs_state); 7121 sysfs_notify_dirent_safe(mddev->sysfs_state);
7050 } 7122 }
7051 7123
7052 if (mddev->flags) 7124 if (mddev->flags)
@@ -7085,7 +7157,7 @@ void md_check_recovery(mddev_t *mddev)
7085 mddev->recovery = 0; 7157 mddev->recovery = 0;
7086 /* flag recovery needed just to double check */ 7158 /* flag recovery needed just to double check */
7087 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 7159 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7088 sysfs_notify_dirent(mddev->sysfs_action); 7160 sysfs_notify_dirent_safe(mddev->sysfs_action);
7089 md_new_event(mddev); 7161 md_new_event(mddev);
7090 goto unlock; 7162 goto unlock;
7091 } 7163 }
@@ -7147,7 +7219,7 @@ void md_check_recovery(mddev_t *mddev)
7147 mddev->recovery = 0; 7219 mddev->recovery = 0;
7148 } else 7220 } else
7149 md_wakeup_thread(mddev->sync_thread); 7221 md_wakeup_thread(mddev->sync_thread);
7150 sysfs_notify_dirent(mddev->sysfs_action); 7222 sysfs_notify_dirent_safe(mddev->sysfs_action);
7151 md_new_event(mddev); 7223 md_new_event(mddev);
7152 } 7224 }
7153 unlock: 7225 unlock:
@@ -7156,7 +7228,7 @@ void md_check_recovery(mddev_t *mddev)
7156 if (test_and_clear_bit(MD_RECOVERY_RECOVER, 7228 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7157 &mddev->recovery)) 7229 &mddev->recovery))
7158 if (mddev->sysfs_action) 7230 if (mddev->sysfs_action)
7159 sysfs_notify_dirent(mddev->sysfs_action); 7231 sysfs_notify_dirent_safe(mddev->sysfs_action);
7160 } 7232 }
7161 mddev_unlock(mddev); 7233 mddev_unlock(mddev);
7162 } 7234 }
@@ -7164,7 +7236,7 @@ void md_check_recovery(mddev_t *mddev)
7164 7236
7165void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) 7237void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7166{ 7238{
7167 sysfs_notify_dirent(rdev->sysfs_state); 7239 sysfs_notify_dirent_safe(rdev->sysfs_state);
7168 wait_event_timeout(rdev->blocked_wait, 7240 wait_event_timeout(rdev->blocked_wait,
7169 !test_bit(Blocked, &rdev->flags), 7241 !test_bit(Blocked, &rdev->flags),
7170 msecs_to_jiffies(5000)); 7242 msecs_to_jiffies(5000));
diff --git a/drivers/md/md.h b/drivers/md/md.h
index 10597bfec00..a953fe2808a 100644
--- a/drivers/md/md.h
+++ b/drivers/md/md.h
@@ -29,6 +29,26 @@
29typedef struct mddev_s mddev_t; 29typedef struct mddev_s mddev_t;
30typedef struct mdk_rdev_s mdk_rdev_t; 30typedef struct mdk_rdev_s mdk_rdev_t;
31 31
32/* generic plugging support - like that provided with request_queue,
33 * but does not require a request_queue
34 */
35struct plug_handle {
36 void (*unplug_fn)(struct plug_handle *);
37 struct timer_list unplug_timer;
38 struct work_struct unplug_work;
39 unsigned long unplug_flag;
40};
41#define PLUGGED_FLAG 1
42void plugger_init(struct plug_handle *plug,
43 void (*unplug_fn)(struct plug_handle *));
44void plugger_set_plug(struct plug_handle *plug);
45int plugger_remove_plug(struct plug_handle *plug);
46static inline void plugger_flush(struct plug_handle *plug)
47{
48 del_timer_sync(&plug->unplug_timer);
49 cancel_work_sync(&plug->unplug_work);
50}
51
32/* 52/*
33 * MD's 'extended' device 53 * MD's 'extended' device
34 */ 54 */
@@ -67,7 +87,7 @@ struct mdk_rdev_s
67#define Faulty 1 /* device is known to have a fault */ 87#define Faulty 1 /* device is known to have a fault */
68#define In_sync 2 /* device is in_sync with rest of array */ 88#define In_sync 2 /* device is in_sync with rest of array */
69#define WriteMostly 4 /* Avoid reading if at all possible */ 89#define WriteMostly 4 /* Avoid reading if at all possible */
70#define BarriersNotsupp 5 /* BIO_RW_BARRIER is not supported */ 90#define BarriersNotsupp 5 /* REQ_HARDBARRIER is not supported */
71#define AllReserved 6 /* If whole device is reserved for 91#define AllReserved 6 /* If whole device is reserved for
72 * one array */ 92 * one array */
73#define AutoDetected 7 /* added by auto-detect */ 93#define AutoDetected 7 /* added by auto-detect */
@@ -125,6 +145,10 @@ struct mddev_s
125 int suspended; 145 int suspended;
126 atomic_t active_io; 146 atomic_t active_io;
127 int ro; 147 int ro;
148 int sysfs_active; /* set when sysfs deletes
149 * are happening, so run/
150 * takeover/stop are not safe
151 */
128 152
129 struct gendisk *gendisk; 153 struct gendisk *gendisk;
130 154
@@ -254,7 +278,7 @@ struct mddev_s
254 * fails. Only supported 278 * fails. Only supported
255 */ 279 */
256 struct bio *biolist; /* bios that need to be retried 280 struct bio *biolist; /* bios that need to be retried
257 * because BIO_RW_BARRIER is not supported 281 * because REQ_HARDBARRIER is not supported
258 */ 282 */
259 283
260 atomic_t recovery_active; /* blocks scheduled, but not written */ 284 atomic_t recovery_active; /* blocks scheduled, but not written */
@@ -297,9 +321,14 @@ struct mddev_s
297 * hot-adding a bitmap. It should 321 * hot-adding a bitmap. It should
298 * eventually be settable by sysfs. 322 * eventually be settable by sysfs.
299 */ 323 */
324 /* When md is serving under dm, it might use a
325 * dirty_log to store the bits.
326 */
327 struct dm_dirty_log *log;
328
300 struct mutex mutex; 329 struct mutex mutex;
301 unsigned long chunksize; 330 unsigned long chunksize;
302 unsigned long daemon_sleep; /* how many seconds between updates? */ 331 unsigned long daemon_sleep; /* how many jiffies between updates? */
303 unsigned long max_write_behind; /* write-behind mode */ 332 unsigned long max_write_behind; /* write-behind mode */
304 int external; 333 int external;
305 } bitmap_info; 334 } bitmap_info;
@@ -308,6 +337,8 @@ struct mddev_s
308 struct list_head all_mddevs; 337 struct list_head all_mddevs;
309 338
310 struct attribute_group *to_remove; 339 struct attribute_group *to_remove;
340 struct plug_handle *plug; /* if used by personality */
341
311 /* Generic barrier handling. 342 /* Generic barrier handling.
312 * If there is a pending barrier request, all other 343 * If there is a pending barrier request, all other
313 * writes are blocked while the devices are flushed. 344 * writes are blocked while the devices are flushed.
@@ -318,6 +349,7 @@ struct mddev_s
318 struct bio *barrier; 349 struct bio *barrier;
319 atomic_t flush_pending; 350 atomic_t flush_pending;
320 struct work_struct barrier_work; 351 struct work_struct barrier_work;
352 struct work_struct event_work; /* used by dm to report failure event */
321}; 353};
322 354
323 355
@@ -382,6 +414,18 @@ struct md_sysfs_entry {
382}; 414};
383extern struct attribute_group md_bitmap_group; 415extern struct attribute_group md_bitmap_group;
384 416
417static inline struct sysfs_dirent *sysfs_get_dirent_safe(struct sysfs_dirent *sd, char *name)
418{
419 if (sd)
420 return sysfs_get_dirent(sd, NULL, name);
421 return sd;
422}
423static inline void sysfs_notify_dirent_safe(struct sysfs_dirent *sd)
424{
425 if (sd)
426 sysfs_notify_dirent(sd);
427}
428
385static inline char * mdname (mddev_t * mddev) 429static inline char * mdname (mddev_t * mddev)
386{ 430{
387 return mddev->gendisk ? mddev->gendisk->disk_name : "mdX"; 431 return mddev->gendisk ? mddev->gendisk->disk_name : "mdX";
@@ -474,5 +518,14 @@ extern int md_integrity_register(mddev_t *mddev);
474extern void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev); 518extern void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev);
475extern int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale); 519extern int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale);
476extern void restore_bitmap_write_access(struct file *file); 520extern void restore_bitmap_write_access(struct file *file);
521extern void md_unplug(mddev_t *mddev);
522
523extern void mddev_init(mddev_t *mddev);
524extern int md_run(mddev_t *mddev);
525extern void md_stop(mddev_t *mddev);
526extern void md_stop_writes(mddev_t *mddev);
527extern void md_rdev_init(mdk_rdev_t *rdev);
477 528
529extern void mddev_suspend(mddev_t *mddev);
530extern void mddev_resume(mddev_t *mddev);
478#endif /* _MD_MD_H */ 531#endif /* _MD_MD_H */
diff --git a/drivers/md/mktables.c b/drivers/md/mktables.c
deleted file mode 100644
index 3b1500843bb..00000000000
--- a/drivers/md/mktables.c
+++ /dev/null
@@ -1,132 +0,0 @@
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright 2002-2007 H. Peter Anvin - All Rights Reserved
4 *
5 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2 or (at your
7 * option) any later version; incorporated herein by reference.
8 *
9 * ----------------------------------------------------------------------- */
10
11/*
12 * mktables.c
13 *
14 * Make RAID-6 tables. This is a host user space program to be run at
15 * compile time.
16 */
17
18#include <stdio.h>
19#include <string.h>
20#include <inttypes.h>
21#include <stdlib.h>
22#include <time.h>
23
24static uint8_t gfmul(uint8_t a, uint8_t b)
25{
26 uint8_t v = 0;
27
28 while (b) {
29 if (b & 1)
30 v ^= a;
31 a = (a << 1) ^ (a & 0x80 ? 0x1d : 0);
32 b >>= 1;
33 }
34
35 return v;
36}
37
38static uint8_t gfpow(uint8_t a, int b)
39{
40 uint8_t v = 1;
41
42 b %= 255;
43 if (b < 0)
44 b += 255;
45
46 while (b) {
47 if (b & 1)
48 v = gfmul(v, a);
49 a = gfmul(a, a);
50 b >>= 1;
51 }
52
53 return v;
54}
55
56int main(int argc, char *argv[])
57{
58 int i, j, k;
59 uint8_t v;
60 uint8_t exptbl[256], invtbl[256];
61
62 printf("#include <linux/raid/pq.h>\n");
63
64 /* Compute multiplication table */
65 printf("\nconst u8 __attribute__((aligned(256)))\n"
66 "raid6_gfmul[256][256] =\n"
67 "{\n");
68 for (i = 0; i < 256; i++) {
69 printf("\t{\n");
70 for (j = 0; j < 256; j += 8) {
71 printf("\t\t");
72 for (k = 0; k < 8; k++)
73 printf("0x%02x,%c", gfmul(i, j + k),
74 (k == 7) ? '\n' : ' ');
75 }
76 printf("\t},\n");
77 }
78 printf("};\n");
79 printf("#ifdef __KERNEL__\n");
80 printf("EXPORT_SYMBOL(raid6_gfmul);\n");
81 printf("#endif\n");
82
83 /* Compute power-of-2 table (exponent) */
84 v = 1;
85 printf("\nconst u8 __attribute__((aligned(256)))\n"
86 "raid6_gfexp[256] =\n" "{\n");
87 for (i = 0; i < 256; i += 8) {
88 printf("\t");
89 for (j = 0; j < 8; j++) {
90 exptbl[i + j] = v;
91 printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
92 v = gfmul(v, 2);
93 if (v == 1)
94 v = 0; /* For entry 255, not a real entry */
95 }
96 }
97 printf("};\n");
98 printf("#ifdef __KERNEL__\n");
99 printf("EXPORT_SYMBOL(raid6_gfexp);\n");
100 printf("#endif\n");
101
102 /* Compute inverse table x^-1 == x^254 */
103 printf("\nconst u8 __attribute__((aligned(256)))\n"
104 "raid6_gfinv[256] =\n" "{\n");
105 for (i = 0; i < 256; i += 8) {
106 printf("\t");
107 for (j = 0; j < 8; j++) {
108 invtbl[i + j] = v = gfpow(i + j, 254);
109 printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
110 }
111 }
112 printf("};\n");
113 printf("#ifdef __KERNEL__\n");
114 printf("EXPORT_SYMBOL(raid6_gfinv);\n");
115 printf("#endif\n");
116
117 /* Compute inv(2^x + 1) (exponent-xor-inverse) table */
118 printf("\nconst u8 __attribute__((aligned(256)))\n"
119 "raid6_gfexi[256] =\n" "{\n");
120 for (i = 0; i < 256; i += 8) {
121 printf("\t");
122 for (j = 0; j < 8; j++)
123 printf("0x%02x,%c", invtbl[exptbl[i + j] ^ 1],
124 (j == 7) ? '\n' : ' ');
125 }
126 printf("};\n");
127 printf("#ifdef __KERNEL__\n");
128 printf("EXPORT_SYMBOL(raid6_gfexi);\n");
129 printf("#endif\n");
130
131 return 0;
132}
diff --git a/drivers/md/multipath.c b/drivers/md/multipath.c
index 410fb60699a..0307d217e7a 100644
--- a/drivers/md/multipath.c
+++ b/drivers/md/multipath.c
@@ -91,7 +91,7 @@ static void multipath_end_request(struct bio *bio, int error)
91 91
92 if (uptodate) 92 if (uptodate)
93 multipath_end_bh_io(mp_bh, 0); 93 multipath_end_bh_io(mp_bh, 0);
94 else if (!bio_rw_flagged(bio, BIO_RW_AHEAD)) { 94 else if (!(bio->bi_rw & REQ_RAHEAD)) {
95 /* 95 /*
96 * oops, IO error: 96 * oops, IO error:
97 */ 97 */
@@ -142,7 +142,7 @@ static int multipath_make_request(mddev_t *mddev, struct bio * bio)
142 struct multipath_bh * mp_bh; 142 struct multipath_bh * mp_bh;
143 struct multipath_info *multipath; 143 struct multipath_info *multipath;
144 144
145 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) { 145 if (unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
146 md_barrier_request(mddev, bio); 146 md_barrier_request(mddev, bio);
147 return 0; 147 return 0;
148 } 148 }
@@ -163,7 +163,7 @@ static int multipath_make_request(mddev_t *mddev, struct bio * bio)
163 mp_bh->bio = *bio; 163 mp_bh->bio = *bio;
164 mp_bh->bio.bi_sector += multipath->rdev->data_offset; 164 mp_bh->bio.bi_sector += multipath->rdev->data_offset;
165 mp_bh->bio.bi_bdev = multipath->rdev->bdev; 165 mp_bh->bio.bi_bdev = multipath->rdev->bdev;
166 mp_bh->bio.bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT); 166 mp_bh->bio.bi_rw |= REQ_FAILFAST_TRANSPORT;
167 mp_bh->bio.bi_end_io = multipath_end_request; 167 mp_bh->bio.bi_end_io = multipath_end_request;
168 mp_bh->bio.bi_private = mp_bh; 168 mp_bh->bio.bi_private = mp_bh;
169 generic_make_request(&mp_bh->bio); 169 generic_make_request(&mp_bh->bio);
@@ -398,7 +398,7 @@ static void multipathd (mddev_t *mddev)
398 *bio = *(mp_bh->master_bio); 398 *bio = *(mp_bh->master_bio);
399 bio->bi_sector += conf->multipaths[mp_bh->path].rdev->data_offset; 399 bio->bi_sector += conf->multipaths[mp_bh->path].rdev->data_offset;
400 bio->bi_bdev = conf->multipaths[mp_bh->path].rdev->bdev; 400 bio->bi_bdev = conf->multipaths[mp_bh->path].rdev->bdev;
401 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT); 401 bio->bi_rw |= REQ_FAILFAST_TRANSPORT;
402 bio->bi_end_io = multipath_end_request; 402 bio->bi_end_io = multipath_end_request;
403 bio->bi_private = mp_bh; 403 bio->bi_private = mp_bh;
404 generic_make_request(bio); 404 generic_make_request(bio);
diff --git a/drivers/md/raid0.c b/drivers/md/raid0.c
index 563abed5a2c..6f7af46d623 100644
--- a/drivers/md/raid0.c
+++ b/drivers/md/raid0.c
@@ -483,7 +483,7 @@ static int raid0_make_request(mddev_t *mddev, struct bio *bio)
483 struct strip_zone *zone; 483 struct strip_zone *zone;
484 mdk_rdev_t *tmp_dev; 484 mdk_rdev_t *tmp_dev;
485 485
486 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) { 486 if (unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
487 md_barrier_request(mddev, bio); 487 md_barrier_request(mddev, bio);
488 return 0; 488 return 0;
489 } 489 }
diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index a948da8012d..73cc74ffc26 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -787,7 +787,7 @@ static int make_request(mddev_t *mddev, struct bio * bio)
787 struct bio_list bl; 787 struct bio_list bl;
788 struct page **behind_pages = NULL; 788 struct page **behind_pages = NULL;
789 const int rw = bio_data_dir(bio); 789 const int rw = bio_data_dir(bio);
790 const bool do_sync = bio_rw_flagged(bio, BIO_RW_SYNCIO); 790 const bool do_sync = (bio->bi_rw & REQ_SYNC);
791 bool do_barriers; 791 bool do_barriers;
792 mdk_rdev_t *blocked_rdev; 792 mdk_rdev_t *blocked_rdev;
793 793
@@ -822,7 +822,7 @@ static int make_request(mddev_t *mddev, struct bio * bio)
822 finish_wait(&conf->wait_barrier, &w); 822 finish_wait(&conf->wait_barrier, &w);
823 } 823 }
824 if (unlikely(!mddev->barriers_work && 824 if (unlikely(!mddev->barriers_work &&
825 bio_rw_flagged(bio, BIO_RW_BARRIER))) { 825 (bio->bi_rw & REQ_HARDBARRIER))) {
826 if (rw == WRITE) 826 if (rw == WRITE)
827 md_write_end(mddev); 827 md_write_end(mddev);
828 bio_endio(bio, -EOPNOTSUPP); 828 bio_endio(bio, -EOPNOTSUPP);
@@ -877,7 +877,7 @@ static int make_request(mddev_t *mddev, struct bio * bio)
877 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset; 877 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
878 read_bio->bi_bdev = mirror->rdev->bdev; 878 read_bio->bi_bdev = mirror->rdev->bdev;
879 read_bio->bi_end_io = raid1_end_read_request; 879 read_bio->bi_end_io = raid1_end_read_request;
880 read_bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO); 880 read_bio->bi_rw = READ | do_sync;
881 read_bio->bi_private = r1_bio; 881 read_bio->bi_private = r1_bio;
882 882
883 generic_make_request(read_bio); 883 generic_make_request(read_bio);
@@ -959,7 +959,7 @@ static int make_request(mddev_t *mddev, struct bio * bio)
959 atomic_set(&r1_bio->remaining, 0); 959 atomic_set(&r1_bio->remaining, 0);
960 atomic_set(&r1_bio->behind_remaining, 0); 960 atomic_set(&r1_bio->behind_remaining, 0);
961 961
962 do_barriers = bio_rw_flagged(bio, BIO_RW_BARRIER); 962 do_barriers = bio->bi_rw & REQ_HARDBARRIER;
963 if (do_barriers) 963 if (do_barriers)
964 set_bit(R1BIO_Barrier, &r1_bio->state); 964 set_bit(R1BIO_Barrier, &r1_bio->state);
965 965
@@ -975,8 +975,7 @@ static int make_request(mddev_t *mddev, struct bio * bio)
975 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset; 975 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
976 mbio->bi_bdev = conf->mirrors[i].rdev->bdev; 976 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
977 mbio->bi_end_io = raid1_end_write_request; 977 mbio->bi_end_io = raid1_end_write_request;
978 mbio->bi_rw = WRITE | (do_barriers << BIO_RW_BARRIER) | 978 mbio->bi_rw = WRITE | do_barriers | do_sync;
979 (do_sync << BIO_RW_SYNCIO);
980 mbio->bi_private = r1_bio; 979 mbio->bi_private = r1_bio;
981 980
982 if (behind_pages) { 981 if (behind_pages) {
@@ -1633,7 +1632,7 @@ static void raid1d(mddev_t *mddev)
1633 sync_request_write(mddev, r1_bio); 1632 sync_request_write(mddev, r1_bio);
1634 unplug = 1; 1633 unplug = 1;
1635 } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) { 1634 } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
1636 /* some requests in the r1bio were BIO_RW_BARRIER 1635 /* some requests in the r1bio were REQ_HARDBARRIER
1637 * requests which failed with -EOPNOTSUPP. Hohumm.. 1636 * requests which failed with -EOPNOTSUPP. Hohumm..
1638 * Better resubmit without the barrier. 1637 * Better resubmit without the barrier.
1639 * We know which devices to resubmit for, because 1638 * We know which devices to resubmit for, because
@@ -1641,7 +1640,7 @@ static void raid1d(mddev_t *mddev)
1641 * We already have a nr_pending reference on these rdevs. 1640 * We already have a nr_pending reference on these rdevs.
1642 */ 1641 */
1643 int i; 1642 int i;
1644 const bool do_sync = bio_rw_flagged(r1_bio->master_bio, BIO_RW_SYNCIO); 1643 const bool do_sync = (r1_bio->master_bio->bi_rw & REQ_SYNC);
1645 clear_bit(R1BIO_BarrierRetry, &r1_bio->state); 1644 clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
1646 clear_bit(R1BIO_Barrier, &r1_bio->state); 1645 clear_bit(R1BIO_Barrier, &r1_bio->state);
1647 for (i=0; i < conf->raid_disks; i++) 1646 for (i=0; i < conf->raid_disks; i++)
@@ -1662,8 +1661,7 @@ static void raid1d(mddev_t *mddev)
1662 conf->mirrors[i].rdev->data_offset; 1661 conf->mirrors[i].rdev->data_offset;
1663 bio->bi_bdev = conf->mirrors[i].rdev->bdev; 1662 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1664 bio->bi_end_io = raid1_end_write_request; 1663 bio->bi_end_io = raid1_end_write_request;
1665 bio->bi_rw = WRITE | 1664 bio->bi_rw = WRITE | do_sync;
1666 (do_sync << BIO_RW_SYNCIO);
1667 bio->bi_private = r1_bio; 1665 bio->bi_private = r1_bio;
1668 r1_bio->bios[i] = bio; 1666 r1_bio->bios[i] = bio;
1669 generic_make_request(bio); 1667 generic_make_request(bio);
@@ -1698,7 +1696,7 @@ static void raid1d(mddev_t *mddev)
1698 (unsigned long long)r1_bio->sector); 1696 (unsigned long long)r1_bio->sector);
1699 raid_end_bio_io(r1_bio); 1697 raid_end_bio_io(r1_bio);
1700 } else { 1698 } else {
1701 const bool do_sync = bio_rw_flagged(r1_bio->master_bio, BIO_RW_SYNCIO); 1699 const bool do_sync = r1_bio->master_bio->bi_rw & REQ_SYNC;
1702 r1_bio->bios[r1_bio->read_disk] = 1700 r1_bio->bios[r1_bio->read_disk] =
1703 mddev->ro ? IO_BLOCKED : NULL; 1701 mddev->ro ? IO_BLOCKED : NULL;
1704 r1_bio->read_disk = disk; 1702 r1_bio->read_disk = disk;
@@ -1715,7 +1713,7 @@ static void raid1d(mddev_t *mddev)
1715 bio->bi_sector = r1_bio->sector + rdev->data_offset; 1713 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1716 bio->bi_bdev = rdev->bdev; 1714 bio->bi_bdev = rdev->bdev;
1717 bio->bi_end_io = raid1_end_read_request; 1715 bio->bi_end_io = raid1_end_read_request;
1718 bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO); 1716 bio->bi_rw = READ | do_sync;
1719 bio->bi_private = r1_bio; 1717 bio->bi_private = r1_bio;
1720 unplug = 1; 1718 unplug = 1;
1721 generic_make_request(bio); 1719 generic_make_request(bio);
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index 42e64e4e5e2..a88aeb5198c 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -799,12 +799,12 @@ static int make_request(mddev_t *mddev, struct bio * bio)
799 int i; 799 int i;
800 int chunk_sects = conf->chunk_mask + 1; 800 int chunk_sects = conf->chunk_mask + 1;
801 const int rw = bio_data_dir(bio); 801 const int rw = bio_data_dir(bio);
802 const bool do_sync = bio_rw_flagged(bio, BIO_RW_SYNCIO); 802 const bool do_sync = (bio->bi_rw & REQ_SYNC);
803 struct bio_list bl; 803 struct bio_list bl;
804 unsigned long flags; 804 unsigned long flags;
805 mdk_rdev_t *blocked_rdev; 805 mdk_rdev_t *blocked_rdev;
806 806
807 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) { 807 if (unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
808 md_barrier_request(mddev, bio); 808 md_barrier_request(mddev, bio);
809 return 0; 809 return 0;
810 } 810 }
@@ -825,11 +825,29 @@ static int make_request(mddev_t *mddev, struct bio * bio)
825 */ 825 */
826 bp = bio_split(bio, 826 bp = bio_split(bio,
827 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) ); 827 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
828
829 /* Each of these 'make_request' calls will call 'wait_barrier'.
830 * If the first succeeds but the second blocks due to the resync
831 * thread raising the barrier, we will deadlock because the
832 * IO to the underlying device will be queued in generic_make_request
833 * and will never complete, so will never reduce nr_pending.
834 * So increment nr_waiting here so no new raise_barriers will
835 * succeed, and so the second wait_barrier cannot block.
836 */
837 spin_lock_irq(&conf->resync_lock);
838 conf->nr_waiting++;
839 spin_unlock_irq(&conf->resync_lock);
840
828 if (make_request(mddev, &bp->bio1)) 841 if (make_request(mddev, &bp->bio1))
829 generic_make_request(&bp->bio1); 842 generic_make_request(&bp->bio1);
830 if (make_request(mddev, &bp->bio2)) 843 if (make_request(mddev, &bp->bio2))
831 generic_make_request(&bp->bio2); 844 generic_make_request(&bp->bio2);
832 845
846 spin_lock_irq(&conf->resync_lock);
847 conf->nr_waiting--;
848 wake_up(&conf->wait_barrier);
849 spin_unlock_irq(&conf->resync_lock);
850
833 bio_pair_release(bp); 851 bio_pair_release(bp);
834 return 0; 852 return 0;
835 bad_map: 853 bad_map:
@@ -879,7 +897,7 @@ static int make_request(mddev_t *mddev, struct bio * bio)
879 mirror->rdev->data_offset; 897 mirror->rdev->data_offset;
880 read_bio->bi_bdev = mirror->rdev->bdev; 898 read_bio->bi_bdev = mirror->rdev->bdev;
881 read_bio->bi_end_io = raid10_end_read_request; 899 read_bio->bi_end_io = raid10_end_read_request;
882 read_bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO); 900 read_bio->bi_rw = READ | do_sync;
883 read_bio->bi_private = r10_bio; 901 read_bio->bi_private = r10_bio;
884 902
885 generic_make_request(read_bio); 903 generic_make_request(read_bio);
@@ -947,7 +965,7 @@ static int make_request(mddev_t *mddev, struct bio * bio)
947 conf->mirrors[d].rdev->data_offset; 965 conf->mirrors[d].rdev->data_offset;
948 mbio->bi_bdev = conf->mirrors[d].rdev->bdev; 966 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
949 mbio->bi_end_io = raid10_end_write_request; 967 mbio->bi_end_io = raid10_end_write_request;
950 mbio->bi_rw = WRITE | (do_sync << BIO_RW_SYNCIO); 968 mbio->bi_rw = WRITE | do_sync;
951 mbio->bi_private = r10_bio; 969 mbio->bi_private = r10_bio;
952 970
953 atomic_inc(&r10_bio->remaining); 971 atomic_inc(&r10_bio->remaining);
@@ -1716,7 +1734,7 @@ static void raid10d(mddev_t *mddev)
1716 raid_end_bio_io(r10_bio); 1734 raid_end_bio_io(r10_bio);
1717 bio_put(bio); 1735 bio_put(bio);
1718 } else { 1736 } else {
1719 const bool do_sync = bio_rw_flagged(r10_bio->master_bio, BIO_RW_SYNCIO); 1737 const bool do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
1720 bio_put(bio); 1738 bio_put(bio);
1721 rdev = conf->mirrors[mirror].rdev; 1739 rdev = conf->mirrors[mirror].rdev;
1722 if (printk_ratelimit()) 1740 if (printk_ratelimit())
@@ -1730,7 +1748,7 @@ static void raid10d(mddev_t *mddev)
1730 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr 1748 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1731 + rdev->data_offset; 1749 + rdev->data_offset;
1732 bio->bi_bdev = rdev->bdev; 1750 bio->bi_bdev = rdev->bdev;
1733 bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO); 1751 bio->bi_rw = READ | do_sync;
1734 bio->bi_private = r10_bio; 1752 bio->bi_private = r10_bio;
1735 bio->bi_end_io = raid10_end_read_request; 1753 bio->bi_end_io = raid10_end_read_request;
1736 unplug = 1; 1754 unplug = 1;
diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
index 96c690279fc..866d4b5a144 100644
--- a/drivers/md/raid5.c
+++ b/drivers/md/raid5.c
@@ -201,11 +201,11 @@ static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
201 if (test_bit(STRIPE_HANDLE, &sh->state)) { 201 if (test_bit(STRIPE_HANDLE, &sh->state)) {
202 if (test_bit(STRIPE_DELAYED, &sh->state)) { 202 if (test_bit(STRIPE_DELAYED, &sh->state)) {
203 list_add_tail(&sh->lru, &conf->delayed_list); 203 list_add_tail(&sh->lru, &conf->delayed_list);
204 blk_plug_device(conf->mddev->queue); 204 plugger_set_plug(&conf->plug);
205 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && 205 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
206 sh->bm_seq - conf->seq_write > 0) { 206 sh->bm_seq - conf->seq_write > 0) {
207 list_add_tail(&sh->lru, &conf->bitmap_list); 207 list_add_tail(&sh->lru, &conf->bitmap_list);
208 blk_plug_device(conf->mddev->queue); 208 plugger_set_plug(&conf->plug);
209 } else { 209 } else {
210 clear_bit(STRIPE_BIT_DELAY, &sh->state); 210 clear_bit(STRIPE_BIT_DELAY, &sh->state);
211 list_add_tail(&sh->lru, &conf->handle_list); 211 list_add_tail(&sh->lru, &conf->handle_list);
@@ -434,7 +434,6 @@ static int has_failed(raid5_conf_t *conf)
434} 434}
435 435
436static void unplug_slaves(mddev_t *mddev); 436static void unplug_slaves(mddev_t *mddev);
437static void raid5_unplug_device(struct request_queue *q);
438 437
439static struct stripe_head * 438static struct stripe_head *
440get_active_stripe(raid5_conf_t *conf, sector_t sector, 439get_active_stripe(raid5_conf_t *conf, sector_t sector,
@@ -464,7 +463,7 @@ get_active_stripe(raid5_conf_t *conf, sector_t sector,
464 < (conf->max_nr_stripes *3/4) 463 < (conf->max_nr_stripes *3/4)
465 || !conf->inactive_blocked), 464 || !conf->inactive_blocked),
466 conf->device_lock, 465 conf->device_lock,
467 raid5_unplug_device(conf->mddev->queue) 466 md_raid5_unplug_device(conf)
468 ); 467 );
469 conf->inactive_blocked = 0; 468 conf->inactive_blocked = 0;
470 } else 469 } else
@@ -1337,10 +1336,14 @@ static int grow_stripes(raid5_conf_t *conf, int num)
1337 struct kmem_cache *sc; 1336 struct kmem_cache *sc;
1338 int devs = max(conf->raid_disks, conf->previous_raid_disks); 1337 int devs = max(conf->raid_disks, conf->previous_raid_disks);
1339 1338
1340 sprintf(conf->cache_name[0], 1339 if (conf->mddev->gendisk)
1341 "raid%d-%s", conf->level, mdname(conf->mddev)); 1340 sprintf(conf->cache_name[0],
1342 sprintf(conf->cache_name[1], 1341 "raid%d-%s", conf->level, mdname(conf->mddev));
1343 "raid%d-%s-alt", conf->level, mdname(conf->mddev)); 1342 else
1343 sprintf(conf->cache_name[0],
1344 "raid%d-%p", conf->level, conf->mddev);
1345 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1346
1344 conf->active_name = 0; 1347 conf->active_name = 0;
1345 sc = kmem_cache_create(conf->cache_name[conf->active_name], 1348 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1346 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), 1349 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
@@ -3614,7 +3617,7 @@ static void raid5_activate_delayed(raid5_conf_t *conf)
3614 list_add_tail(&sh->lru, &conf->hold_list); 3617 list_add_tail(&sh->lru, &conf->hold_list);
3615 } 3618 }
3616 } else 3619 } else
3617 blk_plug_device(conf->mddev->queue); 3620 plugger_set_plug(&conf->plug);
3618} 3621}
3619 3622
3620static void activate_bit_delay(raid5_conf_t *conf) 3623static void activate_bit_delay(raid5_conf_t *conf)
@@ -3655,36 +3658,44 @@ static void unplug_slaves(mddev_t *mddev)
3655 rcu_read_unlock(); 3658 rcu_read_unlock();
3656} 3659}
3657 3660
3658static void raid5_unplug_device(struct request_queue *q) 3661void md_raid5_unplug_device(raid5_conf_t *conf)
3659{ 3662{
3660 mddev_t *mddev = q->queuedata;
3661 raid5_conf_t *conf = mddev->private;
3662 unsigned long flags; 3663 unsigned long flags;
3663 3664
3664 spin_lock_irqsave(&conf->device_lock, flags); 3665 spin_lock_irqsave(&conf->device_lock, flags);
3665 3666
3666 if (blk_remove_plug(q)) { 3667 if (plugger_remove_plug(&conf->plug)) {
3667 conf->seq_flush++; 3668 conf->seq_flush++;
3668 raid5_activate_delayed(conf); 3669 raid5_activate_delayed(conf);
3669 } 3670 }
3670 md_wakeup_thread(mddev->thread); 3671 md_wakeup_thread(conf->mddev->thread);
3671 3672
3672 spin_unlock_irqrestore(&conf->device_lock, flags); 3673 spin_unlock_irqrestore(&conf->device_lock, flags);
3673 3674
3674 unplug_slaves(mddev); 3675 unplug_slaves(conf->mddev);
3675} 3676}
3677EXPORT_SYMBOL_GPL(md_raid5_unplug_device);
3676 3678
3677static int raid5_congested(void *data, int bits) 3679static void raid5_unplug(struct plug_handle *plug)
3680{
3681 raid5_conf_t *conf = container_of(plug, raid5_conf_t, plug);
3682 md_raid5_unplug_device(conf);
3683}
3684
3685static void raid5_unplug_queue(struct request_queue *q)
3686{
3687 mddev_t *mddev = q->queuedata;
3688 md_raid5_unplug_device(mddev->private);
3689}
3690
3691int md_raid5_congested(mddev_t *mddev, int bits)
3678{ 3692{
3679 mddev_t *mddev = data;
3680 raid5_conf_t *conf = mddev->private; 3693 raid5_conf_t *conf = mddev->private;
3681 3694
3682 /* No difference between reads and writes. Just check 3695 /* No difference between reads and writes. Just check
3683 * how busy the stripe_cache is 3696 * how busy the stripe_cache is
3684 */ 3697 */
3685 3698
3686 if (mddev_congested(mddev, bits))
3687 return 1;
3688 if (conf->inactive_blocked) 3699 if (conf->inactive_blocked)
3689 return 1; 3700 return 1;
3690 if (conf->quiesce) 3701 if (conf->quiesce)
@@ -3694,6 +3705,15 @@ static int raid5_congested(void *data, int bits)
3694 3705
3695 return 0; 3706 return 0;
3696} 3707}
3708EXPORT_SYMBOL_GPL(md_raid5_congested);
3709
3710static int raid5_congested(void *data, int bits)
3711{
3712 mddev_t *mddev = data;
3713
3714 return mddev_congested(mddev, bits) ||
3715 md_raid5_congested(mddev, bits);
3716}
3697 3717
3698/* We want read requests to align with chunks where possible, 3718/* We want read requests to align with chunks where possible,
3699 * but write requests don't need to. 3719 * but write requests don't need to.
@@ -3958,7 +3978,7 @@ static int make_request(mddev_t *mddev, struct bio * bi)
3958 const int rw = bio_data_dir(bi); 3978 const int rw = bio_data_dir(bi);
3959 int remaining; 3979 int remaining;
3960 3980
3961 if (unlikely(bio_rw_flagged(bi, BIO_RW_BARRIER))) { 3981 if (unlikely(bi->bi_rw & REQ_HARDBARRIER)) {
3962 /* Drain all pending writes. We only really need 3982 /* Drain all pending writes. We only really need
3963 * to ensure they have been submitted, but this is 3983 * to ensure they have been submitted, but this is
3964 * easier. 3984 * easier.
@@ -4075,7 +4095,7 @@ static int make_request(mddev_t *mddev, struct bio * bi)
4075 * add failed due to overlap. Flush everything 4095 * add failed due to overlap. Flush everything
4076 * and wait a while 4096 * and wait a while
4077 */ 4097 */
4078 raid5_unplug_device(mddev->queue); 4098 md_raid5_unplug_device(conf);
4079 release_stripe(sh); 4099 release_stripe(sh);
4080 schedule(); 4100 schedule();
4081 goto retry; 4101 goto retry;
@@ -4566,23 +4586,15 @@ raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
4566 return 0; 4586 return 0;
4567} 4587}
4568 4588
4569static ssize_t 4589int
4570raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len) 4590raid5_set_cache_size(mddev_t *mddev, int size)
4571{ 4591{
4572 raid5_conf_t *conf = mddev->private; 4592 raid5_conf_t *conf = mddev->private;
4573 unsigned long new;
4574 int err; 4593 int err;
4575 4594
4576 if (len >= PAGE_SIZE) 4595 if (size <= 16 || size > 32768)
4577 return -EINVAL; 4596 return -EINVAL;
4578 if (!conf) 4597 while (size < conf->max_nr_stripes) {
4579 return -ENODEV;
4580
4581 if (strict_strtoul(page, 10, &new))
4582 return -EINVAL;
4583 if (new <= 16 || new > 32768)
4584 return -EINVAL;
4585 while (new < conf->max_nr_stripes) {
4586 if (drop_one_stripe(conf)) 4598 if (drop_one_stripe(conf))
4587 conf->max_nr_stripes--; 4599 conf->max_nr_stripes--;
4588 else 4600 else
@@ -4591,11 +4603,32 @@ raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
4591 err = md_allow_write(mddev); 4603 err = md_allow_write(mddev);
4592 if (err) 4604 if (err)
4593 return err; 4605 return err;
4594 while (new > conf->max_nr_stripes) { 4606 while (size > conf->max_nr_stripes) {
4595 if (grow_one_stripe(conf)) 4607 if (grow_one_stripe(conf))
4596 conf->max_nr_stripes++; 4608 conf->max_nr_stripes++;
4597 else break; 4609 else break;
4598 } 4610 }
4611 return 0;
4612}
4613EXPORT_SYMBOL(raid5_set_cache_size);
4614
4615static ssize_t
4616raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
4617{
4618 raid5_conf_t *conf = mddev->private;
4619 unsigned long new;
4620 int err;
4621
4622 if (len >= PAGE_SIZE)
4623 return -EINVAL;
4624 if (!conf)
4625 return -ENODEV;
4626
4627 if (strict_strtoul(page, 10, &new))
4628 return -EINVAL;
4629 err = raid5_set_cache_size(mddev, new);
4630 if (err)
4631 return err;
4599 return len; 4632 return len;
4600} 4633}
4601 4634
@@ -4958,7 +4991,7 @@ static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded
4958static int run(mddev_t *mddev) 4991static int run(mddev_t *mddev)
4959{ 4992{
4960 raid5_conf_t *conf; 4993 raid5_conf_t *conf;
4961 int working_disks = 0, chunk_size; 4994 int working_disks = 0;
4962 int dirty_parity_disks = 0; 4995 int dirty_parity_disks = 0;
4963 mdk_rdev_t *rdev; 4996 mdk_rdev_t *rdev;
4964 sector_t reshape_offset = 0; 4997 sector_t reshape_offset = 0;
@@ -5144,42 +5177,47 @@ static int run(mddev_t *mddev)
5144 "reshape"); 5177 "reshape");
5145 } 5178 }
5146 5179
5147 /* read-ahead size must cover two whole stripes, which is
5148 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5149 */
5150 {
5151 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5152 int stripe = data_disks *
5153 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5154 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5155 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5156 }
5157 5180
5158 /* Ok, everything is just fine now */ 5181 /* Ok, everything is just fine now */
5159 if (mddev->to_remove == &raid5_attrs_group) 5182 if (mddev->to_remove == &raid5_attrs_group)
5160 mddev->to_remove = NULL; 5183 mddev->to_remove = NULL;
5161 else if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) 5184 else if (mddev->kobj.sd &&
5185 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
5162 printk(KERN_WARNING 5186 printk(KERN_WARNING
5163 "md/raid:%s: failed to create sysfs attributes.\n", 5187 "raid5: failed to create sysfs attributes for %s\n",
5164 mdname(mddev)); 5188 mdname(mddev));
5189 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5165 5190
5166 mddev->queue->queue_lock = &conf->device_lock; 5191 plugger_init(&conf->plug, raid5_unplug);
5192 mddev->plug = &conf->plug;
5193 if (mddev->queue) {
5194 int chunk_size;
5195 /* read-ahead size must cover two whole stripes, which
5196 * is 2 * (datadisks) * chunksize where 'n' is the
5197 * number of raid devices
5198 */
5199 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5200 int stripe = data_disks *
5201 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5202 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5203 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5167 5204
5168 mddev->queue->unplug_fn = raid5_unplug_device; 5205 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
5169 mddev->queue->backing_dev_info.congested_data = mddev;
5170 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
5171 5206
5172 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); 5207 mddev->queue->backing_dev_info.congested_data = mddev;
5208 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
5209 mddev->queue->queue_lock = &conf->device_lock;
5210 mddev->queue->unplug_fn = raid5_unplug_queue;
5173 5211
5174 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); 5212 chunk_size = mddev->chunk_sectors << 9;
5175 chunk_size = mddev->chunk_sectors << 9; 5213 blk_queue_io_min(mddev->queue, chunk_size);
5176 blk_queue_io_min(mddev->queue, chunk_size); 5214 blk_queue_io_opt(mddev->queue, chunk_size *
5177 blk_queue_io_opt(mddev->queue, chunk_size * 5215 (conf->raid_disks - conf->max_degraded));
5178 (conf->raid_disks - conf->max_degraded));
5179 5216
5180 list_for_each_entry(rdev, &mddev->disks, same_set) 5217 list_for_each_entry(rdev, &mddev->disks, same_set)
5181 disk_stack_limits(mddev->gendisk, rdev->bdev, 5218 disk_stack_limits(mddev->gendisk, rdev->bdev,
5182 rdev->data_offset << 9); 5219 rdev->data_offset << 9);
5220 }
5183 5221
5184 return 0; 5222 return 0;
5185abort: 5223abort:
@@ -5200,8 +5238,9 @@ static int stop(mddev_t *mddev)
5200 5238
5201 md_unregister_thread(mddev->thread); 5239 md_unregister_thread(mddev->thread);
5202 mddev->thread = NULL; 5240 mddev->thread = NULL;
5203 mddev->queue->backing_dev_info.congested_fn = NULL; 5241 if (mddev->queue)
5204 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ 5242 mddev->queue->backing_dev_info.congested_fn = NULL;
5243 plugger_flush(&conf->plug); /* the unplug fn references 'conf'*/
5205 free_conf(conf); 5244 free_conf(conf);
5206 mddev->private = NULL; 5245 mddev->private = NULL;
5207 mddev->to_remove = &raid5_attrs_group; 5246 mddev->to_remove = &raid5_attrs_group;
@@ -5545,10 +5584,7 @@ static int raid5_start_reshape(mddev_t *mddev)
5545 sprintf(nm, "rd%d", rdev->raid_disk); 5584 sprintf(nm, "rd%d", rdev->raid_disk);
5546 if (sysfs_create_link(&mddev->kobj, 5585 if (sysfs_create_link(&mddev->kobj,
5547 &rdev->kobj, nm)) 5586 &rdev->kobj, nm))
5548 printk(KERN_WARNING 5587 /* Failure here is OK */;
5549 "md/raid:%s: failed to create "
5550 " link %s\n",
5551 mdname(mddev), nm);
5552 } else 5588 } else
5553 break; 5589 break;
5554 } 5590 }
@@ -5603,7 +5639,7 @@ static void end_reshape(raid5_conf_t *conf)
5603 /* read-ahead size must cover two whole stripes, which is 5639 /* read-ahead size must cover two whole stripes, which is
5604 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices 5640 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5605 */ 5641 */
5606 { 5642 if (conf->mddev->queue) {
5607 int data_disks = conf->raid_disks - conf->max_degraded; 5643 int data_disks = conf->raid_disks - conf->max_degraded;
5608 int stripe = data_disks * ((conf->chunk_sectors << 9) 5644 int stripe = data_disks * ((conf->chunk_sectors << 9)
5609 / PAGE_SIZE); 5645 / PAGE_SIZE);
diff --git a/drivers/md/raid5.h b/drivers/md/raid5.h
index 0f86f5e3672..36eaed5dfd6 100644
--- a/drivers/md/raid5.h
+++ b/drivers/md/raid5.h
@@ -388,7 +388,7 @@ struct raid5_private_data {
388 * two caches. 388 * two caches.
389 */ 389 */
390 int active_name; 390 int active_name;
391 char cache_name[2][20]; 391 char cache_name[2][32];
392 struct kmem_cache *slab_cache; /* for allocating stripes */ 392 struct kmem_cache *slab_cache; /* for allocating stripes */
393 393
394 int seq_flush, seq_write; 394 int seq_flush, seq_write;
@@ -398,6 +398,9 @@ struct raid5_private_data {
398 * (fresh device added). 398 * (fresh device added).
399 * Cleared when a sync completes. 399 * Cleared when a sync completes.
400 */ 400 */
401
402 struct plug_handle plug;
403
401 /* per cpu variables */ 404 /* per cpu variables */
402 struct raid5_percpu { 405 struct raid5_percpu {
403 struct page *spare_page; /* Used when checking P/Q in raid6 */ 406 struct page *spare_page; /* Used when checking P/Q in raid6 */
@@ -497,4 +500,8 @@ static inline int algorithm_is_DDF(int layout)
497{ 500{
498 return layout >= 8 && layout <= 10; 501 return layout >= 8 && layout <= 10;
499} 502}
503
504extern int md_raid5_congested(mddev_t *mddev, int bits);
505extern void md_raid5_unplug_device(raid5_conf_t *conf);
506extern int raid5_set_cache_size(mddev_t *mddev, int size);
500#endif 507#endif
diff --git a/drivers/md/raid6algos.c b/drivers/md/raid6algos.c
deleted file mode 100644
index 1f8784bfd44..00000000000
--- a/drivers/md/raid6algos.c
+++ /dev/null
@@ -1,154 +0,0 @@
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright 2002 H. Peter Anvin - All Rights Reserved
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, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
14 * raid6algos.c
15 *
16 * Algorithm list and algorithm selection for RAID-6
17 */
18
19#include <linux/raid/pq.h>
20#include <linux/gfp.h>
21#ifndef __KERNEL__
22#include <sys/mman.h>
23#include <stdio.h>
24#else
25#if !RAID6_USE_EMPTY_ZERO_PAGE
26/* In .bss so it's zeroed */
27const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
28EXPORT_SYMBOL(raid6_empty_zero_page);
29#endif
30#endif
31
32struct raid6_calls raid6_call;
33EXPORT_SYMBOL_GPL(raid6_call);
34
35const struct raid6_calls * const raid6_algos[] = {
36 &raid6_intx1,
37 &raid6_intx2,
38 &raid6_intx4,
39 &raid6_intx8,
40#if defined(__ia64__)
41 &raid6_intx16,
42 &raid6_intx32,
43#endif
44#if defined(__i386__) && !defined(__arch_um__)
45 &raid6_mmxx1,
46 &raid6_mmxx2,
47 &raid6_sse1x1,
48 &raid6_sse1x2,
49 &raid6_sse2x1,
50 &raid6_sse2x2,
51#endif
52#if defined(__x86_64__) && !defined(__arch_um__)
53 &raid6_sse2x1,
54 &raid6_sse2x2,
55 &raid6_sse2x4,
56#endif
57#ifdef CONFIG_ALTIVEC
58 &raid6_altivec1,
59 &raid6_altivec2,
60 &raid6_altivec4,
61 &raid6_altivec8,
62#endif
63 NULL
64};
65
66#ifdef __KERNEL__
67#define RAID6_TIME_JIFFIES_LG2 4
68#else
69/* Need more time to be stable in userspace */
70#define RAID6_TIME_JIFFIES_LG2 9
71#define time_before(x, y) ((x) < (y))
72#endif
73
74/* Try to pick the best algorithm */
75/* This code uses the gfmul table as convenient data set to abuse */
76
77int __init raid6_select_algo(void)
78{
79 const struct raid6_calls * const * algo;
80 const struct raid6_calls * best;
81 char *syndromes;
82 void *dptrs[(65536/PAGE_SIZE)+2];
83 int i, disks;
84 unsigned long perf, bestperf;
85 int bestprefer;
86 unsigned long j0, j1;
87
88 disks = (65536/PAGE_SIZE)+2;
89 for ( i = 0 ; i < disks-2 ; i++ ) {
90 dptrs[i] = ((char *)raid6_gfmul) + PAGE_SIZE*i;
91 }
92
93 /* Normal code - use a 2-page allocation to avoid D$ conflict */
94 syndromes = (void *) __get_free_pages(GFP_KERNEL, 1);
95
96 if ( !syndromes ) {
97 printk("raid6: Yikes! No memory available.\n");
98 return -ENOMEM;
99 }
100
101 dptrs[disks-2] = syndromes;
102 dptrs[disks-1] = syndromes + PAGE_SIZE;
103
104 bestperf = 0; bestprefer = 0; best = NULL;
105
106 for ( algo = raid6_algos ; *algo ; algo++ ) {
107 if ( !(*algo)->valid || (*algo)->valid() ) {
108 perf = 0;
109
110 preempt_disable();
111 j0 = jiffies;
112 while ( (j1 = jiffies) == j0 )
113 cpu_relax();
114 while (time_before(jiffies,
115 j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
116 (*algo)->gen_syndrome(disks, PAGE_SIZE, dptrs);
117 perf++;
118 }
119 preempt_enable();
120
121 if ( (*algo)->prefer > bestprefer ||
122 ((*algo)->prefer == bestprefer &&
123 perf > bestperf) ) {
124 best = *algo;
125 bestprefer = best->prefer;
126 bestperf = perf;
127 }
128 printk("raid6: %-8s %5ld MB/s\n", (*algo)->name,
129 (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
130 }
131 }
132
133 if (best) {
134 printk("raid6: using algorithm %s (%ld MB/s)\n",
135 best->name,
136 (bestperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
137 raid6_call = *best;
138 } else
139 printk("raid6: Yikes! No algorithm found!\n");
140
141 free_pages((unsigned long)syndromes, 1);
142
143 return best ? 0 : -EINVAL;
144}
145
146static void raid6_exit(void)
147{
148 do { } while (0);
149}
150
151subsys_initcall(raid6_select_algo);
152module_exit(raid6_exit);
153MODULE_LICENSE("GPL");
154MODULE_DESCRIPTION("RAID6 Q-syndrome calculations");
diff --git a/drivers/md/raid6altivec.uc b/drivers/md/raid6altivec.uc
deleted file mode 100644
index 2654d5c854b..00000000000
--- a/drivers/md/raid6altivec.uc
+++ /dev/null
@@ -1,130 +0,0 @@
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright 2002-2004 H. Peter Anvin - All Rights Reserved
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, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
14 * raid6altivec$#.c
15 *
16 * $#-way unrolled portable integer math RAID-6 instruction set
17 *
18 * This file is postprocessed using unroll.awk
19 *
20 * <benh> hpa: in process,
21 * you can just "steal" the vec unit with enable_kernel_altivec() (but
22 * bracked this with preempt_disable/enable or in a lock)
23 */
24
25#include <linux/raid/pq.h>
26
27#ifdef CONFIG_ALTIVEC
28
29#include <altivec.h>
30#ifdef __KERNEL__
31# include <asm/system.h>
32# include <asm/cputable.h>
33#endif
34
35/*
36 * This is the C data type to use. We use a vector of
37 * signed char so vec_cmpgt() will generate the right
38 * instruction.
39 */
40
41typedef vector signed char unative_t;
42
43#define NBYTES(x) ((vector signed char) {x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x})
44#define NSIZE sizeof(unative_t)
45
46/*
47 * The SHLBYTE() operation shifts each byte left by 1, *not*
48 * rolling over into the next byte
49 */
50static inline __attribute_const__ unative_t SHLBYTE(unative_t v)
51{
52 return vec_add(v,v);
53}
54
55/*
56 * The MASK() operation returns 0xFF in any byte for which the high
57 * bit is 1, 0x00 for any byte for which the high bit is 0.
58 */
59static inline __attribute_const__ unative_t MASK(unative_t v)
60{
61 unative_t zv = NBYTES(0);
62
63 /* vec_cmpgt returns a vector bool char; thus the need for the cast */
64 return (unative_t)vec_cmpgt(zv, v);
65}
66
67
68/* This is noinline to make damned sure that gcc doesn't move any of the
69 Altivec code around the enable/disable code */
70static void noinline
71raid6_altivec$#_gen_syndrome_real(int disks, size_t bytes, void **ptrs)
72{
73 u8 **dptr = (u8 **)ptrs;
74 u8 *p, *q;
75 int d, z, z0;
76
77 unative_t wd$$, wq$$, wp$$, w1$$, w2$$;
78 unative_t x1d = NBYTES(0x1d);
79
80 z0 = disks - 3; /* Highest data disk */
81 p = dptr[z0+1]; /* XOR parity */
82 q = dptr[z0+2]; /* RS syndrome */
83
84 for ( d = 0 ; d < bytes ; d += NSIZE*$# ) {
85 wq$$ = wp$$ = *(unative_t *)&dptr[z0][d+$$*NSIZE];
86 for ( z = z0-1 ; z >= 0 ; z-- ) {
87 wd$$ = *(unative_t *)&dptr[z][d+$$*NSIZE];
88 wp$$ = vec_xor(wp$$, wd$$);
89 w2$$ = MASK(wq$$);
90 w1$$ = SHLBYTE(wq$$);
91 w2$$ = vec_and(w2$$, x1d);
92 w1$$ = vec_xor(w1$$, w2$$);
93 wq$$ = vec_xor(w1$$, wd$$);
94 }
95 *(unative_t *)&p[d+NSIZE*$$] = wp$$;
96 *(unative_t *)&q[d+NSIZE*$$] = wq$$;
97 }
98}
99
100static void raid6_altivec$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
101{
102 preempt_disable();
103 enable_kernel_altivec();
104
105 raid6_altivec$#_gen_syndrome_real(disks, bytes, ptrs);
106
107 preempt_enable();
108}
109
110int raid6_have_altivec(void);
111#if $# == 1
112int raid6_have_altivec(void)
113{
114 /* This assumes either all CPUs have Altivec or none does */
115# ifdef __KERNEL__
116 return cpu_has_feature(CPU_FTR_ALTIVEC);
117# else
118 return 1;
119# endif
120}
121#endif
122
123const struct raid6_calls raid6_altivec$# = {
124 raid6_altivec$#_gen_syndrome,
125 raid6_have_altivec,
126 "altivecx$#",
127 0
128};
129
130#endif /* CONFIG_ALTIVEC */
diff --git a/drivers/md/raid6int.uc b/drivers/md/raid6int.uc
deleted file mode 100644
index d1e276a14fa..00000000000
--- a/drivers/md/raid6int.uc
+++ /dev/null
@@ -1,117 +0,0 @@
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright 2002-2004 H. Peter Anvin - All Rights Reserved
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, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
14 * raid6int$#.c
15 *
16 * $#-way unrolled portable integer math RAID-6 instruction set
17 *
18 * This file is postprocessed using unroll.awk
19 */
20
21#include <linux/raid/pq.h>
22
23/*
24 * This is the C data type to use
25 */
26
27/* Change this from BITS_PER_LONG if there is something better... */
28#if BITS_PER_LONG == 64
29# define NBYTES(x) ((x) * 0x0101010101010101UL)
30# define NSIZE 8
31# define NSHIFT 3
32# define NSTRING "64"
33typedef u64 unative_t;
34#else
35# define NBYTES(x) ((x) * 0x01010101U)
36# define NSIZE 4
37# define NSHIFT 2
38# define NSTRING "32"
39typedef u32 unative_t;
40#endif
41
42
43
44/*
45 * IA-64 wants insane amounts of unrolling. On other architectures that
46 * is just a waste of space.
47 */
48#if ($# <= 8) || defined(__ia64__)
49
50
51/*
52 * These sub-operations are separate inlines since they can sometimes be
53 * specially optimized using architecture-specific hacks.
54 */
55
56/*
57 * The SHLBYTE() operation shifts each byte left by 1, *not*
58 * rolling over into the next byte
59 */
60static inline __attribute_const__ unative_t SHLBYTE(unative_t v)
61{
62 unative_t vv;
63
64 vv = (v << 1) & NBYTES(0xfe);
65 return vv;
66}
67
68/*
69 * The MASK() operation returns 0xFF in any byte for which the high
70 * bit is 1, 0x00 for any byte for which the high bit is 0.
71 */
72static inline __attribute_const__ unative_t MASK(unative_t v)
73{
74 unative_t vv;
75
76 vv = v & NBYTES(0x80);
77 vv = (vv << 1) - (vv >> 7); /* Overflow on the top bit is OK */
78 return vv;
79}
80
81
82static void raid6_int$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
83{
84 u8 **dptr = (u8 **)ptrs;
85 u8 *p, *q;
86 int d, z, z0;
87
88 unative_t wd$$, wq$$, wp$$, w1$$, w2$$;
89
90 z0 = disks - 3; /* Highest data disk */
91 p = dptr[z0+1]; /* XOR parity */
92 q = dptr[z0+2]; /* RS syndrome */
93
94 for ( d = 0 ; d < bytes ; d += NSIZE*$# ) {
95 wq$$ = wp$$ = *(unative_t *)&dptr[z0][d+$$*NSIZE];
96 for ( z = z0-1 ; z >= 0 ; z-- ) {
97 wd$$ = *(unative_t *)&dptr[z][d+$$*NSIZE];
98 wp$$ ^= wd$$;
99 w2$$ = MASK(wq$$);
100 w1$$ = SHLBYTE(wq$$);
101 w2$$ &= NBYTES(0x1d);
102 w1$$ ^= w2$$;
103 wq$$ = w1$$ ^ wd$$;
104 }
105 *(unative_t *)&p[d+NSIZE*$$] = wp$$;
106 *(unative_t *)&q[d+NSIZE*$$] = wq$$;
107 }
108}
109
110const struct raid6_calls raid6_intx$# = {
111 raid6_int$#_gen_syndrome,
112 NULL, /* always valid */
113 "int" NSTRING "x$#",
114 0
115};
116
117#endif
diff --git a/drivers/md/raid6mmx.c b/drivers/md/raid6mmx.c
deleted file mode 100644
index e7f6c13132b..00000000000
--- a/drivers/md/raid6mmx.c
+++ /dev/null
@@ -1,142 +0,0 @@
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright 2002 H. Peter Anvin - All Rights Reserved
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, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
14 * raid6mmx.c
15 *
16 * MMX implementation of RAID-6 syndrome functions
17 */
18
19#if defined(__i386__) && !defined(__arch_um__)
20
21#include <linux/raid/pq.h>
22#include "raid6x86.h"
23
24/* Shared with raid6sse1.c */
25const struct raid6_mmx_constants {
26 u64 x1d;
27} raid6_mmx_constants = {
28 0x1d1d1d1d1d1d1d1dULL,
29};
30
31static int raid6_have_mmx(void)
32{
33 /* Not really "boot_cpu" but "all_cpus" */
34 return boot_cpu_has(X86_FEATURE_MMX);
35}
36
37/*
38 * Plain MMX implementation
39 */
40static void raid6_mmx1_gen_syndrome(int disks, size_t bytes, void **ptrs)
41{
42 u8 **dptr = (u8 **)ptrs;
43 u8 *p, *q;
44 int d, z, z0;
45
46 z0 = disks - 3; /* Highest data disk */
47 p = dptr[z0+1]; /* XOR parity */
48 q = dptr[z0+2]; /* RS syndrome */
49
50 kernel_fpu_begin();
51
52 asm volatile("movq %0,%%mm0" : : "m" (raid6_mmx_constants.x1d));
53 asm volatile("pxor %mm5,%mm5"); /* Zero temp */
54
55 for ( d = 0 ; d < bytes ; d += 8 ) {
56 asm volatile("movq %0,%%mm2" : : "m" (dptr[z0][d])); /* P[0] */
57 asm volatile("movq %mm2,%mm4"); /* Q[0] */
58 for ( z = z0-1 ; z >= 0 ; z-- ) {
59 asm volatile("movq %0,%%mm6" : : "m" (dptr[z][d]));
60 asm volatile("pcmpgtb %mm4,%mm5");
61 asm volatile("paddb %mm4,%mm4");
62 asm volatile("pand %mm0,%mm5");
63 asm volatile("pxor %mm5,%mm4");
64 asm volatile("pxor %mm5,%mm5");
65 asm volatile("pxor %mm6,%mm2");
66 asm volatile("pxor %mm6,%mm4");
67 }
68 asm volatile("movq %%mm2,%0" : "=m" (p[d]));
69 asm volatile("pxor %mm2,%mm2");
70 asm volatile("movq %%mm4,%0" : "=m" (q[d]));
71 asm volatile("pxor %mm4,%mm4");
72 }
73
74 kernel_fpu_end();
75}
76
77const struct raid6_calls raid6_mmxx1 = {
78 raid6_mmx1_gen_syndrome,
79 raid6_have_mmx,
80 "mmxx1",
81 0
82};
83
84/*
85 * Unrolled-by-2 MMX implementation
86 */
87static void raid6_mmx2_gen_syndrome(int disks, size_t bytes, void **ptrs)
88{
89 u8 **dptr = (u8 **)ptrs;
90 u8 *p, *q;
91 int d, z, z0;
92
93 z0 = disks - 3; /* Highest data disk */
94 p = dptr[z0+1]; /* XOR parity */
95 q = dptr[z0+2]; /* RS syndrome */
96
97 kernel_fpu_begin();
98
99 asm volatile("movq %0,%%mm0" : : "m" (raid6_mmx_constants.x1d));
100 asm volatile("pxor %mm5,%mm5"); /* Zero temp */
101 asm volatile("pxor %mm7,%mm7"); /* Zero temp */
102
103 for ( d = 0 ; d < bytes ; d += 16 ) {
104 asm volatile("movq %0,%%mm2" : : "m" (dptr[z0][d])); /* P[0] */
105 asm volatile("movq %0,%%mm3" : : "m" (dptr[z0][d+8]));
106 asm volatile("movq %mm2,%mm4"); /* Q[0] */
107 asm volatile("movq %mm3,%mm6"); /* Q[1] */
108 for ( z = z0-1 ; z >= 0 ; z-- ) {
109 asm volatile("pcmpgtb %mm4,%mm5");
110 asm volatile("pcmpgtb %mm6,%mm7");
111 asm volatile("paddb %mm4,%mm4");
112 asm volatile("paddb %mm6,%mm6");
113 asm volatile("pand %mm0,%mm5");
114 asm volatile("pand %mm0,%mm7");
115 asm volatile("pxor %mm5,%mm4");
116 asm volatile("pxor %mm7,%mm6");
117 asm volatile("movq %0,%%mm5" : : "m" (dptr[z][d]));
118 asm volatile("movq %0,%%mm7" : : "m" (dptr[z][d+8]));
119 asm volatile("pxor %mm5,%mm2");
120 asm volatile("pxor %mm7,%mm3");
121 asm volatile("pxor %mm5,%mm4");
122 asm volatile("pxor %mm7,%mm6");
123 asm volatile("pxor %mm5,%mm5");
124 asm volatile("pxor %mm7,%mm7");
125 }
126 asm volatile("movq %%mm2,%0" : "=m" (p[d]));
127 asm volatile("movq %%mm3,%0" : "=m" (p[d+8]));
128 asm volatile("movq %%mm4,%0" : "=m" (q[d]));
129 asm volatile("movq %%mm6,%0" : "=m" (q[d+8]));
130 }
131
132 kernel_fpu_end();
133}
134
135const struct raid6_calls raid6_mmxx2 = {
136 raid6_mmx2_gen_syndrome,
137 raid6_have_mmx,
138 "mmxx2",
139 0
140};
141
142#endif
diff --git a/drivers/md/raid6recov.c b/drivers/md/raid6recov.c
deleted file mode 100644
index 2609f00e0d6..00000000000
--- a/drivers/md/raid6recov.c
+++ /dev/null
@@ -1,132 +0,0 @@
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright 2002 H. Peter Anvin - All Rights Reserved
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, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
14 * raid6recov.c
15 *
16 * RAID-6 data recovery in dual failure mode. In single failure mode,
17 * use the RAID-5 algorithm (or, in the case of Q failure, just reconstruct
18 * the syndrome.)
19 */
20
21#include <linux/raid/pq.h>
22
23/* Recover two failed data blocks. */
24void raid6_2data_recov(int disks, size_t bytes, int faila, int failb,
25 void **ptrs)
26{
27 u8 *p, *q, *dp, *dq;
28 u8 px, qx, db;
29 const u8 *pbmul; /* P multiplier table for B data */
30 const u8 *qmul; /* Q multiplier table (for both) */
31
32 p = (u8 *)ptrs[disks-2];
33 q = (u8 *)ptrs[disks-1];
34
35 /* Compute syndrome with zero for the missing data pages
36 Use the dead data pages as temporary storage for
37 delta p and delta q */
38 dp = (u8 *)ptrs[faila];
39 ptrs[faila] = (void *)raid6_empty_zero_page;
40 ptrs[disks-2] = dp;
41 dq = (u8 *)ptrs[failb];
42 ptrs[failb] = (void *)raid6_empty_zero_page;
43 ptrs[disks-1] = dq;
44
45 raid6_call.gen_syndrome(disks, bytes, ptrs);
46
47 /* Restore pointer table */
48 ptrs[faila] = dp;
49 ptrs[failb] = dq;
50 ptrs[disks-2] = p;
51 ptrs[disks-1] = q;
52
53 /* Now, pick the proper data tables */
54 pbmul = raid6_gfmul[raid6_gfexi[failb-faila]];
55 qmul = raid6_gfmul[raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]]];
56
57 /* Now do it... */
58 while ( bytes-- ) {
59 px = *p ^ *dp;
60 qx = qmul[*q ^ *dq];
61 *dq++ = db = pbmul[px] ^ qx; /* Reconstructed B */
62 *dp++ = db ^ px; /* Reconstructed A */
63 p++; q++;
64 }
65}
66EXPORT_SYMBOL_GPL(raid6_2data_recov);
67
68/* Recover failure of one data block plus the P block */
69void raid6_datap_recov(int disks, size_t bytes, int faila, void **ptrs)
70{
71 u8 *p, *q, *dq;
72 const u8 *qmul; /* Q multiplier table */
73
74 p = (u8 *)ptrs[disks-2];
75 q = (u8 *)ptrs[disks-1];
76
77 /* Compute syndrome with zero for the missing data page
78 Use the dead data page as temporary storage for delta q */
79 dq = (u8 *)ptrs[faila];
80 ptrs[faila] = (void *)raid6_empty_zero_page;
81 ptrs[disks-1] = dq;
82
83 raid6_call.gen_syndrome(disks, bytes, ptrs);
84
85 /* Restore pointer table */
86 ptrs[faila] = dq;
87 ptrs[disks-1] = q;
88
89 /* Now, pick the proper data tables */
90 qmul = raid6_gfmul[raid6_gfinv[raid6_gfexp[faila]]];
91
92 /* Now do it... */
93 while ( bytes-- ) {
94 *p++ ^= *dq = qmul[*q ^ *dq];
95 q++; dq++;
96 }
97}
98EXPORT_SYMBOL_GPL(raid6_datap_recov);
99
100#ifndef __KERNEL__
101/* Testing only */
102
103/* Recover two failed blocks. */
104void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, void **ptrs)
105{
106 if ( faila > failb ) {
107 int tmp = faila;
108 faila = failb;
109 failb = tmp;
110 }
111
112 if ( failb == disks-1 ) {
113 if ( faila == disks-2 ) {
114 /* P+Q failure. Just rebuild the syndrome. */
115 raid6_call.gen_syndrome(disks, bytes, ptrs);
116 } else {
117 /* data+Q failure. Reconstruct data from P,
118 then rebuild syndrome. */
119 /* NOT IMPLEMENTED - equivalent to RAID-5 */
120 }
121 } else {
122 if ( failb == disks-2 ) {
123 /* data+P failure. */
124 raid6_datap_recov(disks, bytes, faila, ptrs);
125 } else {
126 /* data+data failure. */
127 raid6_2data_recov(disks, bytes, faila, failb, ptrs);
128 }
129 }
130}
131
132#endif
diff --git a/drivers/md/raid6sse1.c b/drivers/md/raid6sse1.c
deleted file mode 100644
index b274dd5eab8..00000000000
--- a/drivers/md/raid6sse1.c
+++ /dev/null
@@ -1,162 +0,0 @@
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright 2002 H. Peter Anvin - All Rights Reserved
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, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
14 * raid6sse1.c
15 *
16 * SSE-1/MMXEXT implementation of RAID-6 syndrome functions
17 *
18 * This is really an MMX implementation, but it requires SSE-1 or
19 * AMD MMXEXT for prefetch support and a few other features. The
20 * support for nontemporal memory accesses is enough to make this
21 * worthwhile as a separate implementation.
22 */
23
24#if defined(__i386__) && !defined(__arch_um__)
25
26#include <linux/raid/pq.h>
27#include "raid6x86.h"
28
29/* Defined in raid6mmx.c */
30extern const struct raid6_mmx_constants {
31 u64 x1d;
32} raid6_mmx_constants;
33
34static int raid6_have_sse1_or_mmxext(void)
35{
36 /* Not really boot_cpu but "all_cpus" */
37 return boot_cpu_has(X86_FEATURE_MMX) &&
38 (boot_cpu_has(X86_FEATURE_XMM) ||
39 boot_cpu_has(X86_FEATURE_MMXEXT));
40}
41
42/*
43 * Plain SSE1 implementation
44 */
45static void raid6_sse11_gen_syndrome(int disks, size_t bytes, void **ptrs)
46{
47 u8 **dptr = (u8 **)ptrs;
48 u8 *p, *q;
49 int d, z, z0;
50
51 z0 = disks - 3; /* Highest data disk */
52 p = dptr[z0+1]; /* XOR parity */
53 q = dptr[z0+2]; /* RS syndrome */
54
55 kernel_fpu_begin();
56
57 asm volatile("movq %0,%%mm0" : : "m" (raid6_mmx_constants.x1d));
58 asm volatile("pxor %mm5,%mm5"); /* Zero temp */
59
60 for ( d = 0 ; d < bytes ; d += 8 ) {
61 asm volatile("prefetchnta %0" : : "m" (dptr[z0][d]));
62 asm volatile("movq %0,%%mm2" : : "m" (dptr[z0][d])); /* P[0] */
63 asm volatile("prefetchnta %0" : : "m" (dptr[z0-1][d]));
64 asm volatile("movq %mm2,%mm4"); /* Q[0] */
65 asm volatile("movq %0,%%mm6" : : "m" (dptr[z0-1][d]));
66 for ( z = z0-2 ; z >= 0 ; z-- ) {
67 asm volatile("prefetchnta %0" : : "m" (dptr[z][d]));
68 asm volatile("pcmpgtb %mm4,%mm5");
69 asm volatile("paddb %mm4,%mm4");
70 asm volatile("pand %mm0,%mm5");
71 asm volatile("pxor %mm5,%mm4");
72 asm volatile("pxor %mm5,%mm5");
73 asm volatile("pxor %mm6,%mm2");
74 asm volatile("pxor %mm6,%mm4");
75 asm volatile("movq %0,%%mm6" : : "m" (dptr[z][d]));
76 }
77 asm volatile("pcmpgtb %mm4,%mm5");
78 asm volatile("paddb %mm4,%mm4");
79 asm volatile("pand %mm0,%mm5");
80 asm volatile("pxor %mm5,%mm4");
81 asm volatile("pxor %mm5,%mm5");
82 asm volatile("pxor %mm6,%mm2");
83 asm volatile("pxor %mm6,%mm4");
84
85 asm volatile("movntq %%mm2,%0" : "=m" (p[d]));
86 asm volatile("movntq %%mm4,%0" : "=m" (q[d]));
87 }
88
89 asm volatile("sfence" : : : "memory");
90 kernel_fpu_end();
91}
92
93const struct raid6_calls raid6_sse1x1 = {
94 raid6_sse11_gen_syndrome,
95 raid6_have_sse1_or_mmxext,
96 "sse1x1",
97 1 /* Has cache hints */
98};
99
100/*
101 * Unrolled-by-2 SSE1 implementation
102 */
103static void raid6_sse12_gen_syndrome(int disks, size_t bytes, void **ptrs)
104{
105 u8 **dptr = (u8 **)ptrs;
106 u8 *p, *q;
107 int d, z, z0;
108
109 z0 = disks - 3; /* Highest data disk */
110 p = dptr[z0+1]; /* XOR parity */
111 q = dptr[z0+2]; /* RS syndrome */
112
113 kernel_fpu_begin();
114
115 asm volatile("movq %0,%%mm0" : : "m" (raid6_mmx_constants.x1d));
116 asm volatile("pxor %mm5,%mm5"); /* Zero temp */
117 asm volatile("pxor %mm7,%mm7"); /* Zero temp */
118
119 /* We uniformly assume a single prefetch covers at least 16 bytes */
120 for ( d = 0 ; d < bytes ; d += 16 ) {
121 asm volatile("prefetchnta %0" : : "m" (dptr[z0][d]));
122 asm volatile("movq %0,%%mm2" : : "m" (dptr[z0][d])); /* P[0] */
123 asm volatile("movq %0,%%mm3" : : "m" (dptr[z0][d+8])); /* P[1] */
124 asm volatile("movq %mm2,%mm4"); /* Q[0] */
125 asm volatile("movq %mm3,%mm6"); /* Q[1] */
126 for ( z = z0-1 ; z >= 0 ; z-- ) {
127 asm volatile("prefetchnta %0" : : "m" (dptr[z][d]));
128 asm volatile("pcmpgtb %mm4,%mm5");
129 asm volatile("pcmpgtb %mm6,%mm7");
130 asm volatile("paddb %mm4,%mm4");
131 asm volatile("paddb %mm6,%mm6");
132 asm volatile("pand %mm0,%mm5");
133 asm volatile("pand %mm0,%mm7");
134 asm volatile("pxor %mm5,%mm4");
135 asm volatile("pxor %mm7,%mm6");
136 asm volatile("movq %0,%%mm5" : : "m" (dptr[z][d]));
137 asm volatile("movq %0,%%mm7" : : "m" (dptr[z][d+8]));
138 asm volatile("pxor %mm5,%mm2");
139 asm volatile("pxor %mm7,%mm3");
140 asm volatile("pxor %mm5,%mm4");
141 asm volatile("pxor %mm7,%mm6");
142 asm volatile("pxor %mm5,%mm5");
143 asm volatile("pxor %mm7,%mm7");
144 }
145 asm volatile("movntq %%mm2,%0" : "=m" (p[d]));
146 asm volatile("movntq %%mm3,%0" : "=m" (p[d+8]));
147 asm volatile("movntq %%mm4,%0" : "=m" (q[d]));
148 asm volatile("movntq %%mm6,%0" : "=m" (q[d+8]));
149 }
150
151 asm volatile("sfence" : :: "memory");
152 kernel_fpu_end();
153}
154
155const struct raid6_calls raid6_sse1x2 = {
156 raid6_sse12_gen_syndrome,
157 raid6_have_sse1_or_mmxext,
158 "sse1x2",
159 1 /* Has cache hints */
160};
161
162#endif
diff --git a/drivers/md/raid6sse2.c b/drivers/md/raid6sse2.c
deleted file mode 100644
index 6ed6c6c0389..00000000000
--- a/drivers/md/raid6sse2.c
+++ /dev/null
@@ -1,262 +0,0 @@
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright 2002 H. Peter Anvin - All Rights Reserved
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, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
14 * raid6sse2.c
15 *
16 * SSE-2 implementation of RAID-6 syndrome functions
17 *
18 */
19
20#if (defined(__i386__) || defined(__x86_64__)) && !defined(__arch_um__)
21
22#include <linux/raid/pq.h>
23#include "raid6x86.h"
24
25static const struct raid6_sse_constants {
26 u64 x1d[2];
27} raid6_sse_constants __attribute__((aligned(16))) = {
28 { 0x1d1d1d1d1d1d1d1dULL, 0x1d1d1d1d1d1d1d1dULL },
29};
30
31static int raid6_have_sse2(void)
32{
33 /* Not really boot_cpu but "all_cpus" */
34 return boot_cpu_has(X86_FEATURE_MMX) &&
35 boot_cpu_has(X86_FEATURE_FXSR) &&
36 boot_cpu_has(X86_FEATURE_XMM) &&
37 boot_cpu_has(X86_FEATURE_XMM2);
38}
39
40/*
41 * Plain SSE2 implementation
42 */
43static void raid6_sse21_gen_syndrome(int disks, size_t bytes, void **ptrs)
44{
45 u8 **dptr = (u8 **)ptrs;
46 u8 *p, *q;
47 int d, z, z0;
48
49 z0 = disks - 3; /* Highest data disk */
50 p = dptr[z0+1]; /* XOR parity */
51 q = dptr[z0+2]; /* RS syndrome */
52
53 kernel_fpu_begin();
54
55 asm volatile("movdqa %0,%%xmm0" : : "m" (raid6_sse_constants.x1d[0]));
56 asm volatile("pxor %xmm5,%xmm5"); /* Zero temp */
57
58 for ( d = 0 ; d < bytes ; d += 16 ) {
59 asm volatile("prefetchnta %0" : : "m" (dptr[z0][d]));
60 asm volatile("movdqa %0,%%xmm2" : : "m" (dptr[z0][d])); /* P[0] */
61 asm volatile("prefetchnta %0" : : "m" (dptr[z0-1][d]));
62 asm volatile("movdqa %xmm2,%xmm4"); /* Q[0] */
63 asm volatile("movdqa %0,%%xmm6" : : "m" (dptr[z0-1][d]));
64 for ( z = z0-2 ; z >= 0 ; z-- ) {
65 asm volatile("prefetchnta %0" : : "m" (dptr[z][d]));
66 asm volatile("pcmpgtb %xmm4,%xmm5");
67 asm volatile("paddb %xmm4,%xmm4");
68 asm volatile("pand %xmm0,%xmm5");
69 asm volatile("pxor %xmm5,%xmm4");
70 asm volatile("pxor %xmm5,%xmm5");
71 asm volatile("pxor %xmm6,%xmm2");
72 asm volatile("pxor %xmm6,%xmm4");
73 asm volatile("movdqa %0,%%xmm6" : : "m" (dptr[z][d]));
74 }
75 asm volatile("pcmpgtb %xmm4,%xmm5");
76 asm volatile("paddb %xmm4,%xmm4");
77 asm volatile("pand %xmm0,%xmm5");
78 asm volatile("pxor %xmm5,%xmm4");
79 asm volatile("pxor %xmm5,%xmm5");
80 asm volatile("pxor %xmm6,%xmm2");
81 asm volatile("pxor %xmm6,%xmm4");
82
83 asm volatile("movntdq %%xmm2,%0" : "=m" (p[d]));
84 asm volatile("pxor %xmm2,%xmm2");
85 asm volatile("movntdq %%xmm4,%0" : "=m" (q[d]));
86 asm volatile("pxor %xmm4,%xmm4");
87 }
88
89 asm volatile("sfence" : : : "memory");
90 kernel_fpu_end();
91}
92
93const struct raid6_calls raid6_sse2x1 = {
94 raid6_sse21_gen_syndrome,
95 raid6_have_sse2,
96 "sse2x1",
97 1 /* Has cache hints */
98};
99
100/*
101 * Unrolled-by-2 SSE2 implementation
102 */
103static void raid6_sse22_gen_syndrome(int disks, size_t bytes, void **ptrs)
104{
105 u8 **dptr = (u8 **)ptrs;
106 u8 *p, *q;
107 int d, z, z0;
108
109 z0 = disks - 3; /* Highest data disk */
110 p = dptr[z0+1]; /* XOR parity */
111 q = dptr[z0+2]; /* RS syndrome */
112
113 kernel_fpu_begin();
114
115 asm volatile("movdqa %0,%%xmm0" : : "m" (raid6_sse_constants.x1d[0]));
116 asm volatile("pxor %xmm5,%xmm5"); /* Zero temp */
117 asm volatile("pxor %xmm7,%xmm7"); /* Zero temp */
118
119 /* We uniformly assume a single prefetch covers at least 32 bytes */
120 for ( d = 0 ; d < bytes ; d += 32 ) {
121 asm volatile("prefetchnta %0" : : "m" (dptr[z0][d]));
122 asm volatile("movdqa %0,%%xmm2" : : "m" (dptr[z0][d])); /* P[0] */
123 asm volatile("movdqa %0,%%xmm3" : : "m" (dptr[z0][d+16])); /* P[1] */
124 asm volatile("movdqa %xmm2,%xmm4"); /* Q[0] */
125 asm volatile("movdqa %xmm3,%xmm6"); /* Q[1] */
126 for ( z = z0-1 ; z >= 0 ; z-- ) {
127 asm volatile("prefetchnta %0" : : "m" (dptr[z][d]));
128 asm volatile("pcmpgtb %xmm4,%xmm5");
129 asm volatile("pcmpgtb %xmm6,%xmm7");
130 asm volatile("paddb %xmm4,%xmm4");
131 asm volatile("paddb %xmm6,%xmm6");
132 asm volatile("pand %xmm0,%xmm5");
133 asm volatile("pand %xmm0,%xmm7");
134 asm volatile("pxor %xmm5,%xmm4");
135 asm volatile("pxor %xmm7,%xmm6");
136 asm volatile("movdqa %0,%%xmm5" : : "m" (dptr[z][d]));
137 asm volatile("movdqa %0,%%xmm7" : : "m" (dptr[z][d+16]));
138 asm volatile("pxor %xmm5,%xmm2");
139 asm volatile("pxor %xmm7,%xmm3");
140 asm volatile("pxor %xmm5,%xmm4");
141 asm volatile("pxor %xmm7,%xmm6");
142 asm volatile("pxor %xmm5,%xmm5");
143 asm volatile("pxor %xmm7,%xmm7");
144 }
145 asm volatile("movntdq %%xmm2,%0" : "=m" (p[d]));
146 asm volatile("movntdq %%xmm3,%0" : "=m" (p[d+16]));
147 asm volatile("movntdq %%xmm4,%0" : "=m" (q[d]));
148 asm volatile("movntdq %%xmm6,%0" : "=m" (q[d+16]));
149 }
150
151 asm volatile("sfence" : : : "memory");
152 kernel_fpu_end();
153}
154
155const struct raid6_calls raid6_sse2x2 = {
156 raid6_sse22_gen_syndrome,
157 raid6_have_sse2,
158 "sse2x2",
159 1 /* Has cache hints */
160};
161
162#endif
163
164#if defined(__x86_64__) && !defined(__arch_um__)
165
166/*
167 * Unrolled-by-4 SSE2 implementation
168 */
169static void raid6_sse24_gen_syndrome(int disks, size_t bytes, void **ptrs)
170{
171 u8 **dptr = (u8 **)ptrs;
172 u8 *p, *q;
173 int d, z, z0;
174
175 z0 = disks - 3; /* Highest data disk */
176 p = dptr[z0+1]; /* XOR parity */
177 q = dptr[z0+2]; /* RS syndrome */
178
179 kernel_fpu_begin();
180
181 asm volatile("movdqa %0,%%xmm0" :: "m" (raid6_sse_constants.x1d[0]));
182 asm volatile("pxor %xmm2,%xmm2"); /* P[0] */
183 asm volatile("pxor %xmm3,%xmm3"); /* P[1] */
184 asm volatile("pxor %xmm4,%xmm4"); /* Q[0] */
185 asm volatile("pxor %xmm5,%xmm5"); /* Zero temp */
186 asm volatile("pxor %xmm6,%xmm6"); /* Q[1] */
187 asm volatile("pxor %xmm7,%xmm7"); /* Zero temp */
188 asm volatile("pxor %xmm10,%xmm10"); /* P[2] */
189 asm volatile("pxor %xmm11,%xmm11"); /* P[3] */
190 asm volatile("pxor %xmm12,%xmm12"); /* Q[2] */
191 asm volatile("pxor %xmm13,%xmm13"); /* Zero temp */
192 asm volatile("pxor %xmm14,%xmm14"); /* Q[3] */
193 asm volatile("pxor %xmm15,%xmm15"); /* Zero temp */
194
195 for ( d = 0 ; d < bytes ; d += 64 ) {
196 for ( z = z0 ; z >= 0 ; z-- ) {
197 /* The second prefetch seems to improve performance... */
198 asm volatile("prefetchnta %0" :: "m" (dptr[z][d]));
199 asm volatile("prefetchnta %0" :: "m" (dptr[z][d+32]));
200 asm volatile("pcmpgtb %xmm4,%xmm5");
201 asm volatile("pcmpgtb %xmm6,%xmm7");
202 asm volatile("pcmpgtb %xmm12,%xmm13");
203 asm volatile("pcmpgtb %xmm14,%xmm15");
204 asm volatile("paddb %xmm4,%xmm4");
205 asm volatile("paddb %xmm6,%xmm6");
206 asm volatile("paddb %xmm12,%xmm12");
207 asm volatile("paddb %xmm14,%xmm14");
208 asm volatile("pand %xmm0,%xmm5");
209 asm volatile("pand %xmm0,%xmm7");
210 asm volatile("pand %xmm0,%xmm13");
211 asm volatile("pand %xmm0,%xmm15");
212 asm volatile("pxor %xmm5,%xmm4");
213 asm volatile("pxor %xmm7,%xmm6");
214 asm volatile("pxor %xmm13,%xmm12");
215 asm volatile("pxor %xmm15,%xmm14");
216 asm volatile("movdqa %0,%%xmm5" :: "m" (dptr[z][d]));
217 asm volatile("movdqa %0,%%xmm7" :: "m" (dptr[z][d+16]));
218 asm volatile("movdqa %0,%%xmm13" :: "m" (dptr[z][d+32]));
219 asm volatile("movdqa %0,%%xmm15" :: "m" (dptr[z][d+48]));
220 asm volatile("pxor %xmm5,%xmm2");
221 asm volatile("pxor %xmm7,%xmm3");
222 asm volatile("pxor %xmm13,%xmm10");
223 asm volatile("pxor %xmm15,%xmm11");
224 asm volatile("pxor %xmm5,%xmm4");
225 asm volatile("pxor %xmm7,%xmm6");
226 asm volatile("pxor %xmm13,%xmm12");
227 asm volatile("pxor %xmm15,%xmm14");
228 asm volatile("pxor %xmm5,%xmm5");
229 asm volatile("pxor %xmm7,%xmm7");
230 asm volatile("pxor %xmm13,%xmm13");
231 asm volatile("pxor %xmm15,%xmm15");
232 }
233 asm volatile("movntdq %%xmm2,%0" : "=m" (p[d]));
234 asm volatile("pxor %xmm2,%xmm2");
235 asm volatile("movntdq %%xmm3,%0" : "=m" (p[d+16]));
236 asm volatile("pxor %xmm3,%xmm3");
237 asm volatile("movntdq %%xmm10,%0" : "=m" (p[d+32]));
238 asm volatile("pxor %xmm10,%xmm10");
239 asm volatile("movntdq %%xmm11,%0" : "=m" (p[d+48]));
240 asm volatile("pxor %xmm11,%xmm11");
241 asm volatile("movntdq %%xmm4,%0" : "=m" (q[d]));
242 asm volatile("pxor %xmm4,%xmm4");
243 asm volatile("movntdq %%xmm6,%0" : "=m" (q[d+16]));
244 asm volatile("pxor %xmm6,%xmm6");
245 asm volatile("movntdq %%xmm12,%0" : "=m" (q[d+32]));
246 asm volatile("pxor %xmm12,%xmm12");
247 asm volatile("movntdq %%xmm14,%0" : "=m" (q[d+48]));
248 asm volatile("pxor %xmm14,%xmm14");
249 }
250
251 asm volatile("sfence" : : : "memory");
252 kernel_fpu_end();
253}
254
255const struct raid6_calls raid6_sse2x4 = {
256 raid6_sse24_gen_syndrome,
257 raid6_have_sse2,
258 "sse2x4",
259 1 /* Has cache hints */
260};
261
262#endif
diff --git a/drivers/md/raid6test/Makefile b/drivers/md/raid6test/Makefile
deleted file mode 100644
index 2874cbef529..00000000000
--- a/drivers/md/raid6test/Makefile
+++ /dev/null
@@ -1,75 +0,0 @@
1#
2# This is a simple Makefile to test some of the RAID-6 code
3# from userspace.
4#
5
6CC = gcc
7OPTFLAGS = -O2 # Adjust as desired
8CFLAGS = -I.. -I ../../../include -g $(OPTFLAGS)
9LD = ld
10AWK = awk
11AR = ar
12RANLIB = ranlib
13
14.c.o:
15 $(CC) $(CFLAGS) -c -o $@ $<
16
17%.c: ../%.c
18 cp -f $< $@
19
20%.uc: ../%.uc
21 cp -f $< $@
22
23all: raid6.a raid6test
24
25raid6.a: raid6int1.o raid6int2.o raid6int4.o raid6int8.o raid6int16.o \
26 raid6int32.o \
27 raid6mmx.o raid6sse1.o raid6sse2.o \
28 raid6altivec1.o raid6altivec2.o raid6altivec4.o raid6altivec8.o \
29 raid6recov.o raid6algos.o \
30 raid6tables.o
31 rm -f $@
32 $(AR) cq $@ $^
33 $(RANLIB) $@
34
35raid6test: test.c raid6.a
36 $(CC) $(CFLAGS) -o raid6test $^
37
38raid6altivec1.c: raid6altivec.uc ../unroll.awk
39 $(AWK) ../unroll.awk -vN=1 < raid6altivec.uc > $@
40
41raid6altivec2.c: raid6altivec.uc ../unroll.awk
42 $(AWK) ../unroll.awk -vN=2 < raid6altivec.uc > $@
43
44raid6altivec4.c: raid6altivec.uc ../unroll.awk
45 $(AWK) ../unroll.awk -vN=4 < raid6altivec.uc > $@
46
47raid6altivec8.c: raid6altivec.uc ../unroll.awk
48 $(AWK) ../unroll.awk -vN=8 < raid6altivec.uc > $@
49
50raid6int1.c: raid6int.uc ../unroll.awk
51 $(AWK) ../unroll.awk -vN=1 < raid6int.uc > $@
52
53raid6int2.c: raid6int.uc ../unroll.awk
54 $(AWK) ../unroll.awk -vN=2 < raid6int.uc > $@
55
56raid6int4.c: raid6int.uc ../unroll.awk
57 $(AWK) ../unroll.awk -vN=4 < raid6int.uc > $@
58
59raid6int8.c: raid6int.uc ../unroll.awk
60 $(AWK) ../unroll.awk -vN=8 < raid6int.uc > $@
61
62raid6int16.c: raid6int.uc ../unroll.awk
63 $(AWK) ../unroll.awk -vN=16 < raid6int.uc > $@
64
65raid6int32.c: raid6int.uc ../unroll.awk
66 $(AWK) ../unroll.awk -vN=32 < raid6int.uc > $@
67
68raid6tables.c: mktables
69 ./mktables > raid6tables.c
70
71clean:
72 rm -f *.o *.a mktables mktables.c raid6int.uc raid6*.c raid6test
73
74spotless: clean
75 rm -f *~
diff --git a/drivers/md/raid6test/test.c b/drivers/md/raid6test/test.c
deleted file mode 100644
index 7a930318b17..00000000000
--- a/drivers/md/raid6test/test.c
+++ /dev/null
@@ -1,124 +0,0 @@
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright 2002-2007 H. Peter Anvin - All Rights Reserved
4 *
5 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2 or (at your
7 * option) any later version; incorporated herein by reference.
8 *
9 * ----------------------------------------------------------------------- */
10
11/*
12 * raid6test.c
13 *
14 * Test RAID-6 recovery with various algorithms
15 */
16
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#include <linux/raid/pq.h>
21
22#define NDISKS 16 /* Including P and Q */
23
24const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
25struct raid6_calls raid6_call;
26
27char *dataptrs[NDISKS];
28char data[NDISKS][PAGE_SIZE];
29char recovi[PAGE_SIZE], recovj[PAGE_SIZE];
30
31static void makedata(void)
32{
33 int i, j;
34
35 for (i = 0; i < NDISKS; i++) {
36 for (j = 0; j < PAGE_SIZE; j++)
37 data[i][j] = rand();
38
39 dataptrs[i] = data[i];
40 }
41}
42
43static char disk_type(int d)
44{
45 switch (d) {
46 case NDISKS-2:
47 return 'P';
48 case NDISKS-1:
49 return 'Q';
50 default:
51 return 'D';
52 }
53}
54
55static int test_disks(int i, int j)
56{
57 int erra, errb;
58
59 memset(recovi, 0xf0, PAGE_SIZE);
60 memset(recovj, 0xba, PAGE_SIZE);
61
62 dataptrs[i] = recovi;
63 dataptrs[j] = recovj;
64
65 raid6_dual_recov(NDISKS, PAGE_SIZE, i, j, (void **)&dataptrs);
66
67 erra = memcmp(data[i], recovi, PAGE_SIZE);
68 errb = memcmp(data[j], recovj, PAGE_SIZE);
69
70 if (i < NDISKS-2 && j == NDISKS-1) {
71 /* We don't implement the DQ failure scenario, since it's
72 equivalent to a RAID-5 failure (XOR, then recompute Q) */
73 erra = errb = 0;
74 } else {
75 printf("algo=%-8s faila=%3d(%c) failb=%3d(%c) %s\n",
76 raid6_call.name,
77 i, disk_type(i),
78 j, disk_type(j),
79 (!erra && !errb) ? "OK" :
80 !erra ? "ERRB" :
81 !errb ? "ERRA" : "ERRAB");
82 }
83
84 dataptrs[i] = data[i];
85 dataptrs[j] = data[j];
86
87 return erra || errb;
88}
89
90int main(int argc, char *argv[])
91{
92 const struct raid6_calls *const *algo;
93 int i, j;
94 int err = 0;
95
96 makedata();
97
98 for (algo = raid6_algos; *algo; algo++) {
99 if (!(*algo)->valid || (*algo)->valid()) {
100 raid6_call = **algo;
101
102 /* Nuke syndromes */
103 memset(data[NDISKS-2], 0xee, 2*PAGE_SIZE);
104
105 /* Generate assumed good syndrome */
106 raid6_call.gen_syndrome(NDISKS, PAGE_SIZE,
107 (void **)&dataptrs);
108
109 for (i = 0; i < NDISKS-1; i++)
110 for (j = i+1; j < NDISKS; j++)
111 err += test_disks(i, j);
112 }
113 printf("\n");
114 }
115
116 printf("\n");
117 /* Pick the best algorithm test */
118 raid6_select_algo();
119
120 if (err)
121 printf("\n*** ERRORS FOUND ***\n");
122
123 return err;
124}
diff --git a/drivers/md/raid6x86.h b/drivers/md/raid6x86.h
deleted file mode 100644
index 4c22c156855..00000000000
--- a/drivers/md/raid6x86.h
+++ /dev/null
@@ -1,61 +0,0 @@
1/* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2002-2004 H. Peter Anvin - All Rights Reserved
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, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
14 * raid6x86.h
15 *
16 * Definitions common to x86 and x86-64 RAID-6 code only
17 */
18
19#ifndef LINUX_RAID_RAID6X86_H
20#define LINUX_RAID_RAID6X86_H
21
22#if (defined(__i386__) || defined(__x86_64__)) && !defined(__arch_um__)
23
24#ifdef __KERNEL__ /* Real code */
25
26#include <asm/i387.h>
27
28#else /* Dummy code for user space testing */
29
30static inline void kernel_fpu_begin(void)
31{
32}
33
34static inline void kernel_fpu_end(void)
35{
36}
37
38#define X86_FEATURE_MMX (0*32+23) /* Multimedia Extensions */
39#define X86_FEATURE_FXSR (0*32+24) /* FXSAVE and FXRSTOR instructions
40 * (fast save and restore) */
41#define X86_FEATURE_XMM (0*32+25) /* Streaming SIMD Extensions */
42#define X86_FEATURE_XMM2 (0*32+26) /* Streaming SIMD Extensions-2 */
43#define X86_FEATURE_MMXEXT (1*32+22) /* AMD MMX extensions */
44
45/* Should work well enough on modern CPUs for testing */
46static inline int boot_cpu_has(int flag)
47{
48 u32 eax = (flag >> 5) ? 0x80000001 : 1;
49 u32 edx;
50
51 asm volatile("cpuid"
52 : "+a" (eax), "=d" (edx)
53 : : "ecx", "ebx");
54
55 return (edx >> (flag & 31)) & 1;
56}
57
58#endif /* ndef __KERNEL__ */
59
60#endif
61#endif
diff --git a/drivers/md/unroll.awk b/drivers/md/unroll.awk
deleted file mode 100644
index c6aa03631df..00000000000
--- a/drivers/md/unroll.awk
+++ /dev/null
@@ -1,20 +0,0 @@
1
2# This filter requires one command line option of form -vN=n
3# where n must be a decimal number.
4#
5# Repeat each input line containing $$ n times, replacing $$ with 0...n-1.
6# Replace each $# with n, and each $* with a single $.
7
8BEGIN {
9 n = N + 0
10}
11{
12 if (/\$\$/) { rep = n } else { rep = 1 }
13 for (i = 0; i < rep; ++i) {
14 tmp = $0
15 gsub(/\$\$/, i, tmp)
16 gsub(/\$\#/, n, tmp)
17 gsub(/\$\*/, "$", tmp)
18 print tmp
19 }
20}
diff --git a/drivers/media/IR/Kconfig b/drivers/media/IR/Kconfig
index 999a8250b3c..30e04915a25 100644
--- a/drivers/media/IR/Kconfig
+++ b/drivers/media/IR/Kconfig
@@ -1,8 +1,10 @@
1config IR_CORE 1menuconfig IR_CORE
2 tristate 2 tristate "Infrared remote controller adapters"
3 depends on INPUT 3 depends on INPUT
4 default INPUT 4 default INPUT
5 5
6if IR_CORE
7
6config VIDEO_IR 8config VIDEO_IR
7 tristate 9 tristate
8 depends on IR_CORE 10 depends on IR_CORE
@@ -16,7 +18,7 @@ config LIRC
16 Enable this option to build the Linux Infrared Remote 18 Enable this option to build the Linux Infrared Remote
17 Control (LIRC) core device interface driver. The LIRC 19 Control (LIRC) core device interface driver. The LIRC
18 interface passes raw IR to and from userspace, where the 20 interface passes raw IR to and from userspace, where the
19 LIRC daemon handles protocol decoding for IR reception ann 21 LIRC daemon handles protocol decoding for IR reception and
20 encoding for IR transmitting (aka "blasting"). 22 encoding for IR transmitting (aka "blasting").
21 23
22source "drivers/media/IR/keymaps/Kconfig" 24source "drivers/media/IR/keymaps/Kconfig"
@@ -103,3 +105,31 @@ config IR_MCEUSB
103 105
104 To compile this driver as a module, choose M here: the 106 To compile this driver as a module, choose M here: the
105 module will be called mceusb. 107 module will be called mceusb.
108
109config IR_ENE
110 tristate "ENE eHome Receiver/Transciever (pnp id: ENE0100/ENE02xxx)"
111 depends on PNP
112 depends on IR_CORE
113 ---help---
114 Say Y here to enable support for integrated infrared receiver
115 /transciever made by ENE.
116
117 You can see if you have it by looking at lspnp output.
118 Output should include ENE0100 ENE0200 or something similiar.
119
120 To compile this driver as a module, choose M here: the
121 module will be called ene_ir.
122
123config IR_STREAMZAP
124 tristate "Streamzap PC Remote IR Receiver"
125 depends on USB_ARCH_HAS_HCD
126 depends on IR_CORE
127 select USB
128 ---help---
129 Say Y here if you want to use a Streamzap PC Remote
130 Infrared Receiver.
131
132 To compile this driver as a module, choose M here: the
133 module will be called streamzap.
134
135endif #IR_CORE
diff --git a/drivers/media/IR/Makefile b/drivers/media/IR/Makefile
index 2ae4f3abfdb..53676838fe9 100644
--- a/drivers/media/IR/Makefile
+++ b/drivers/media/IR/Makefile
@@ -16,3 +16,5 @@ obj-$(CONFIG_IR_LIRC_CODEC) += ir-lirc-codec.o
16# stand-alone IR receivers/transmitters 16# stand-alone IR receivers/transmitters
17obj-$(CONFIG_IR_IMON) += imon.o 17obj-$(CONFIG_IR_IMON) += imon.o
18obj-$(CONFIG_IR_MCEUSB) += mceusb.o 18obj-$(CONFIG_IR_MCEUSB) += mceusb.o
19obj-$(CONFIG_IR_ENE) += ene_ir.o
20obj-$(CONFIG_IR_STREAMZAP) += streamzap.o
diff --git a/drivers/media/IR/ene_ir.c b/drivers/media/IR/ene_ir.c
new file mode 100644
index 00000000000..5447750f5e3
--- /dev/null
+++ b/drivers/media/IR/ene_ir.c
@@ -0,0 +1,1023 @@
1/*
2 * driver for ENE KB3926 B/C/D CIR (pnp id: ENE0XXX)
3 *
4 * Copyright (C) 2010 Maxim Levitsky <maximlevitsky@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
19 * USA
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/pnp.h>
25#include <linux/io.h>
26#include <linux/interrupt.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/input.h>
30#include <media/ir-core.h>
31#include <media/ir-common.h>
32#include "ene_ir.h"
33
34
35static int sample_period = -1;
36static int enable_idle = 1;
37static int input = 1;
38static int debug;
39static int txsim;
40
41static int ene_irq_status(struct ene_device *dev);
42
43/* read a hardware register */
44static u8 ene_hw_read_reg(struct ene_device *dev, u16 reg)
45{
46 u8 retval;
47 outb(reg >> 8, dev->hw_io + ENE_ADDR_HI);
48 outb(reg & 0xFF, dev->hw_io + ENE_ADDR_LO);
49 retval = inb(dev->hw_io + ENE_IO);
50
51 ene_dbg_verbose("reg %04x == %02x", reg, retval);
52 return retval;
53}
54
55/* write a hardware register */
56static void ene_hw_write_reg(struct ene_device *dev, u16 reg, u8 value)
57{
58 outb(reg >> 8, dev->hw_io + ENE_ADDR_HI);
59 outb(reg & 0xFF, dev->hw_io + ENE_ADDR_LO);
60 outb(value, dev->hw_io + ENE_IO);
61
62 ene_dbg_verbose("reg %04x <- %02x", reg, value);
63}
64
65/* change specific bits in hardware register */
66static void ene_hw_write_reg_mask(struct ene_device *dev,
67 u16 reg, u8 value, u8 mask)
68{
69 u8 regvalue;
70
71 outb(reg >> 8, dev->hw_io + ENE_ADDR_HI);
72 outb(reg & 0xFF, dev->hw_io + ENE_ADDR_LO);
73
74 regvalue = inb(dev->hw_io + ENE_IO) & ~mask;
75 regvalue |= (value & mask);
76 outb(regvalue, dev->hw_io + ENE_IO);
77
78 ene_dbg_verbose("reg %04x <- %02x (mask=%02x)", reg, value, mask);
79}
80
81/* detect hardware features */
82static int ene_hw_detect(struct ene_device *dev)
83{
84 u8 chip_major, chip_minor;
85 u8 hw_revision, old_ver;
86 u8 tmp;
87 u8 fw_capabilities;
88 int pll_freq;
89
90 tmp = ene_hw_read_reg(dev, ENE_HW_UNK);
91 ene_hw_write_reg(dev, ENE_HW_UNK, tmp & ~ENE_HW_UNK_CLR);
92
93 chip_major = ene_hw_read_reg(dev, ENE_HW_VER_MAJOR);
94 chip_minor = ene_hw_read_reg(dev, ENE_HW_VER_MINOR);
95
96 ene_hw_write_reg(dev, ENE_HW_UNK, tmp);
97 hw_revision = ene_hw_read_reg(dev, ENE_HW_VERSION);
98 old_ver = ene_hw_read_reg(dev, ENE_HW_VER_OLD);
99
100 pll_freq = (ene_hw_read_reg(dev, ENE_PLLFRH) << 4) +
101 (ene_hw_read_reg(dev, ENE_PLLFRL) >> 4);
102
103 if (pll_freq != 1000)
104 dev->rx_period_adjust = 4;
105 else
106 dev->rx_period_adjust = 2;
107
108
109 ene_printk(KERN_NOTICE, "PLL freq = %d\n", pll_freq);
110
111 if (hw_revision == 0xFF) {
112
113 ene_printk(KERN_WARNING, "device seems to be disabled\n");
114 ene_printk(KERN_WARNING,
115 "send a mail to lirc-list@lists.sourceforge.net\n");
116 ene_printk(KERN_WARNING, "please attach output of acpidump\n");
117 return -ENODEV;
118 }
119
120 if (chip_major == 0x33) {
121 ene_printk(KERN_WARNING, "chips 0x33xx aren't supported\n");
122 return -ENODEV;
123 }
124
125 if (chip_major == 0x39 && chip_minor == 0x26 && hw_revision == 0xC0) {
126 dev->hw_revision = ENE_HW_C;
127 } else if (old_ver == 0x24 && hw_revision == 0xC0) {
128 dev->hw_revision = ENE_HW_B;
129 ene_printk(KERN_NOTICE, "KB3926B detected\n");
130 } else {
131 dev->hw_revision = ENE_HW_D;
132 ene_printk(KERN_WARNING,
133 "unknown ENE chip detected, assuming KB3926D\n");
134 ene_printk(KERN_WARNING,
135 "driver support might be not complete");
136
137 }
138
139 ene_printk(KERN_DEBUG,
140 "chip is 0x%02x%02x - kbver = 0x%02x, rev = 0x%02x\n",
141 chip_major, chip_minor, old_ver, hw_revision);
142
143 /* detect features hardware supports */
144 if (dev->hw_revision < ENE_HW_C)
145 return 0;
146
147 fw_capabilities = ene_hw_read_reg(dev, ENE_FW2);
148 ene_dbg("Firmware capabilities: %02x", fw_capabilities);
149
150 dev->hw_gpio40_learning = fw_capabilities & ENE_FW2_GP40_AS_LEARN;
151 dev->hw_learning_and_tx_capable = fw_capabilities & ENE_FW2_LEARNING;
152
153 dev->hw_fan_as_normal_input = dev->hw_learning_and_tx_capable &&
154 (fw_capabilities & ENE_FW2_FAN_AS_NRML_IN);
155
156 ene_printk(KERN_NOTICE, "hardware features:\n");
157 ene_printk(KERN_NOTICE,
158 "learning and transmit %s, gpio40_learn %s, fan_in %s\n",
159 dev->hw_learning_and_tx_capable ? "on" : "off",
160 dev->hw_gpio40_learning ? "on" : "off",
161 dev->hw_fan_as_normal_input ? "on" : "off");
162
163 if (dev->hw_learning_and_tx_capable) {
164 ene_printk(KERN_WARNING,
165 "Device supports transmitting, but that support is\n");
166 ene_printk(KERN_WARNING,
167 "lightly tested. Please test it and mail\n");
168 ene_printk(KERN_WARNING,
169 "lirc-list@lists.sourceforge.net\n");
170 }
171 return 0;
172}
173
174/* this enables/disables IR input via gpio40*/
175static void ene_enable_gpio40_receive(struct ene_device *dev, int enable)
176{
177 ene_hw_write_reg_mask(dev, ENE_CIR_CONF2, enable ?
178 0 : ENE_CIR_CONF2_GPIO40DIS,
179 ENE_CIR_CONF2_GPIO40DIS);
180}
181
182/* this enables/disables IR via standard input */
183static void ene_enable_normal_receive(struct ene_device *dev, int enable)
184{
185 ene_hw_write_reg(dev, ENE_CIR_CONF1, enable ? ENE_CIR_CONF1_RX_ON : 0);
186}
187
188/* this enables/disables IR input via unused fan tachtometer input */
189static void ene_enable_fan_receive(struct ene_device *dev, int enable)
190{
191 if (!enable)
192 ene_hw_write_reg(dev, ENE_FAN_AS_IN1, 0);
193 else {
194 ene_hw_write_reg(dev, ENE_FAN_AS_IN1, ENE_FAN_AS_IN1_EN);
195 ene_hw_write_reg(dev, ENE_FAN_AS_IN2, ENE_FAN_AS_IN2_EN);
196 }
197 dev->rx_fan_input_inuse = enable;
198}
199
200
201/* Sense current received carrier */
202static int ene_rx_sense_carrier(struct ene_device *dev)
203{
204 int period = ene_hw_read_reg(dev, ENE_RX_CARRIER);
205 int carrier;
206 ene_dbg("RX: hardware carrier period = %02x", period);
207
208 if (!(period & ENE_RX_CARRIER_VALID))
209 return 0;
210
211 period &= ~ENE_RX_CARRIER_VALID;
212
213 if (!period)
214 return 0;
215
216 carrier = 2000000 / period;
217 ene_dbg("RX: sensed carrier = %d Hz", carrier);
218 return carrier;
219}
220
221/* determine which input to use*/
222static void ene_rx_set_inputs(struct ene_device *dev)
223{
224 int learning_mode = dev->learning_enabled;
225
226 ene_dbg("RX: setup receiver, learning mode = %d", learning_mode);
227
228 ene_enable_normal_receive(dev, 1);
229
230 /* old hardware doesn't support learning mode for sure */
231 if (dev->hw_revision <= ENE_HW_B)
232 return;
233
234 /* receiver not learning capable, still set gpio40 correctly */
235 if (!dev->hw_learning_and_tx_capable) {
236 ene_enable_gpio40_receive(dev, !dev->hw_gpio40_learning);
237 return;
238 }
239
240 /* enable learning mode */
241 if (learning_mode) {
242 ene_enable_gpio40_receive(dev, dev->hw_gpio40_learning);
243
244 /* fan input is not used for learning */
245 if (dev->hw_fan_as_normal_input)
246 ene_enable_fan_receive(dev, 0);
247
248 /* disable learning mode */
249 } else {
250 if (dev->hw_fan_as_normal_input) {
251 ene_enable_fan_receive(dev, 1);
252 ene_enable_normal_receive(dev, 0);
253 } else
254 ene_enable_gpio40_receive(dev,
255 !dev->hw_gpio40_learning);
256 }
257
258 /* set few additional settings for this mode */
259 ene_hw_write_reg_mask(dev, ENE_CIR_CONF1, learning_mode ?
260 ENE_CIR_CONF1_LEARN1 : 0, ENE_CIR_CONF1_LEARN1);
261
262 ene_hw_write_reg_mask(dev, ENE_CIR_CONF2, learning_mode ?
263 ENE_CIR_CONF2_LEARN2 : 0, ENE_CIR_CONF2_LEARN2);
264
265 if (dev->rx_fan_input_inuse) {
266 dev->props->rx_resolution = ENE_SAMPLE_PERIOD_FAN * 1000;
267
268 dev->props->timeout =
269 ENE_FAN_VALUE_MASK * ENE_SAMPLE_PERIOD_FAN * 1000;
270 } else {
271 dev->props->rx_resolution = sample_period * 1000;
272 dev->props->timeout = ENE_MAXGAP * 1000;
273 }
274}
275
276/* Enable the device for receive */
277static void ene_rx_enable(struct ene_device *dev)
278{
279 u8 reg_value;
280
281 if (dev->hw_revision < ENE_HW_C) {
282 ene_hw_write_reg(dev, ENEB_IRQ, dev->irq << 1);
283 ene_hw_write_reg(dev, ENEB_IRQ_UNK1, 0x01);
284 } else {
285 reg_value = ene_hw_read_reg(dev, ENEC_IRQ) & 0xF0;
286 reg_value |= ENEC_IRQ_UNK_EN;
287 reg_value &= ~ENEC_IRQ_STATUS;
288 reg_value |= (dev->irq & ENEC_IRQ_MASK);
289 ene_hw_write_reg(dev, ENEC_IRQ, reg_value);
290 ene_hw_write_reg(dev, ENE_TX_UNK1, 0x63);
291 }
292
293 ene_hw_write_reg(dev, ENE_CIR_CONF2, 0x00);
294 ene_rx_set_inputs(dev);
295
296 /* set sampling period */
297 ene_hw_write_reg(dev, ENE_CIR_SAMPLE_PERIOD, sample_period);
298
299 /* ack any pending irqs - just in case */
300 ene_irq_status(dev);
301
302 /* enable firmware bits */
303 ene_hw_write_reg_mask(dev, ENE_FW1,
304 ENE_FW1_ENABLE | ENE_FW1_IRQ,
305 ENE_FW1_ENABLE | ENE_FW1_IRQ);
306
307 /* enter idle mode */
308 ir_raw_event_set_idle(dev->idev, 1);
309 ir_raw_event_reset(dev->idev);
310
311}
312
313/* Disable the device receiver */
314static void ene_rx_disable(struct ene_device *dev)
315{
316 /* disable inputs */
317 ene_enable_normal_receive(dev, 0);
318
319 if (dev->hw_fan_as_normal_input)
320 ene_enable_fan_receive(dev, 0);
321
322 /* disable hardware IRQ and firmware flag */
323 ene_hw_write_reg_mask(dev, ENE_FW1, 0, ENE_FW1_ENABLE | ENE_FW1_IRQ);
324
325 ir_raw_event_set_idle(dev->idev, 1);
326 ir_raw_event_reset(dev->idev);
327}
328
329
330/* prepare transmission */
331static void ene_tx_prepare(struct ene_device *dev)
332{
333 u8 conf1;
334
335 conf1 = ene_hw_read_reg(dev, ENE_CIR_CONF1);
336 dev->saved_conf1 = conf1;
337
338 if (dev->hw_revision == ENE_HW_C)
339 conf1 &= ~ENE_CIR_CONF1_TX_CLEAR;
340
341 /* Enable TX engine */
342 conf1 |= ENE_CIR_CONF1_TX_ON;
343
344 /* Set carrier */
345 if (dev->tx_period) {
346
347 /* NOTE: duty cycle handling is just a guess, it might
348 not be aviable. Default values were tested */
349 int tx_period_in500ns = dev->tx_period * 2;
350
351 int tx_pulse_width_in_500ns =
352 tx_period_in500ns / (100 / dev->tx_duty_cycle);
353
354 if (!tx_pulse_width_in_500ns)
355 tx_pulse_width_in_500ns = 1;
356
357 ene_dbg("TX: pulse distance = %d * 500 ns", tx_period_in500ns);
358 ene_dbg("TX: pulse width = %d * 500 ns",
359 tx_pulse_width_in_500ns);
360
361 ene_hw_write_reg(dev, ENE_TX_PERIOD, ENE_TX_PERIOD_UNKBIT |
362 tx_period_in500ns);
363
364 ene_hw_write_reg(dev, ENE_TX_PERIOD_PULSE,
365 tx_pulse_width_in_500ns);
366
367 conf1 |= ENE_CIR_CONF1_TX_CARR;
368 } else
369 conf1 &= ~ENE_CIR_CONF1_TX_CARR;
370
371 ene_hw_write_reg(dev, ENE_CIR_CONF1, conf1);
372
373}
374
375/* end transmission */
376static void ene_tx_complete(struct ene_device *dev)
377{
378 ene_hw_write_reg(dev, ENE_CIR_CONF1, dev->saved_conf1);
379 dev->tx_buffer = NULL;
380}
381
382/* set transmit mask */
383static void ene_tx_hw_set_transmiter_mask(struct ene_device *dev)
384{
385 u8 txport1 = ene_hw_read_reg(dev, ENE_TX_PORT1) & ~ENE_TX_PORT1_EN;
386 u8 txport2 = ene_hw_read_reg(dev, ENE_TX_PORT2) & ~ENE_TX_PORT2_EN;
387
388 if (dev->transmitter_mask & 0x01)
389 txport1 |= ENE_TX_PORT1_EN;
390
391 if (dev->transmitter_mask & 0x02)
392 txport2 |= ENE_TX_PORT2_EN;
393
394 ene_hw_write_reg(dev, ENE_TX_PORT1, txport1);
395 ene_hw_write_reg(dev, ENE_TX_PORT2, txport2);
396}
397
398/* TX one sample - must be called with dev->hw_lock*/
399static void ene_tx_sample(struct ene_device *dev)
400{
401 u8 raw_tx;
402 u32 sample;
403
404 if (!dev->tx_buffer) {
405 ene_dbg("TX: attempt to transmit NULL buffer");
406 return;
407 }
408
409 /* Grab next TX sample */
410 if (!dev->tx_sample) {
411again:
412 if (dev->tx_pos == dev->tx_len + 1) {
413 if (!dev->tx_done) {
414 ene_dbg("TX: no more data to send");
415 dev->tx_done = 1;
416 goto exit;
417 } else {
418 ene_dbg("TX: last sample sent by hardware");
419 ene_tx_complete(dev);
420 complete(&dev->tx_complete);
421 return;
422 }
423 }
424
425 sample = dev->tx_buffer[dev->tx_pos++];
426 dev->tx_sample_pulse = !dev->tx_sample_pulse;
427
428 ene_dbg("TX: sample %8d (%s)", sample, dev->tx_sample_pulse ?
429 "pulse" : "space");
430
431 dev->tx_sample = DIV_ROUND_CLOSEST(sample, ENE_TX_SMPL_PERIOD);
432
433 /* guard against too short samples */
434 if (!dev->tx_sample)
435 goto again;
436 }
437
438 raw_tx = min(dev->tx_sample , (unsigned int)ENE_TX_SMLP_MASK);
439 dev->tx_sample -= raw_tx;
440
441 if (dev->tx_sample_pulse)
442 raw_tx |= ENE_TX_PULSE_MASK;
443
444 ene_hw_write_reg(dev, ENE_TX_INPUT1 + dev->tx_reg, raw_tx);
445 dev->tx_reg = !dev->tx_reg;
446exit:
447 /* simulate TX done interrupt */
448 if (txsim)
449 mod_timer(&dev->tx_sim_timer, jiffies + HZ / 500);
450}
451
452/* timer to simulate tx done interrupt */
453static void ene_tx_irqsim(unsigned long data)
454{
455 struct ene_device *dev = (struct ene_device *)data;
456 unsigned long flags;
457
458 spin_lock_irqsave(&dev->hw_lock, flags);
459 ene_tx_sample(dev);
460 spin_unlock_irqrestore(&dev->hw_lock, flags);
461}
462
463
464/* read irq status and ack it */
465static int ene_irq_status(struct ene_device *dev)
466{
467 u8 irq_status;
468 u8 fw_flags1, fw_flags2;
469 int cur_rx_pointer;
470 int retval = 0;
471
472 fw_flags2 = ene_hw_read_reg(dev, ENE_FW2);
473 cur_rx_pointer = !!(fw_flags2 & ENE_FW2_BUF_HIGH);
474
475 if (dev->hw_revision < ENE_HW_C) {
476 irq_status = ene_hw_read_reg(dev, ENEB_IRQ_STATUS);
477
478 if (!(irq_status & ENEB_IRQ_STATUS_IR))
479 return 0;
480
481 ene_hw_write_reg(dev, ENEB_IRQ_STATUS,
482 irq_status & ~ENEB_IRQ_STATUS_IR);
483 dev->rx_pointer = cur_rx_pointer;
484 return ENE_IRQ_RX;
485 }
486
487 irq_status = ene_hw_read_reg(dev, ENEC_IRQ);
488
489 if (!(irq_status & ENEC_IRQ_STATUS))
490 return 0;
491
492 /* original driver does that twice - a workaround ? */
493 ene_hw_write_reg(dev, ENEC_IRQ, irq_status & ~ENEC_IRQ_STATUS);
494 ene_hw_write_reg(dev, ENEC_IRQ, irq_status & ~ENEC_IRQ_STATUS);
495
496 /* clear unknown flag in F8F9 */
497 if (fw_flags2 & ENE_FW2_IRQ_CLR)
498 ene_hw_write_reg(dev, ENE_FW2, fw_flags2 & ~ENE_FW2_IRQ_CLR);
499
500 /* check if this is a TX interrupt */
501 fw_flags1 = ene_hw_read_reg(dev, ENE_FW1);
502 if (fw_flags1 & ENE_FW1_TXIRQ) {
503 ene_hw_write_reg(dev, ENE_FW1, fw_flags1 & ~ENE_FW1_TXIRQ);
504 retval |= ENE_IRQ_TX;
505 }
506
507 /* Check if this is RX interrupt */
508 if (dev->rx_pointer != cur_rx_pointer) {
509 retval |= ENE_IRQ_RX;
510 dev->rx_pointer = cur_rx_pointer;
511
512 } else if (!(retval & ENE_IRQ_TX)) {
513 ene_dbg("RX: interrupt without change in RX pointer(%d)",
514 dev->rx_pointer);
515 retval |= ENE_IRQ_RX;
516 }
517
518 if ((retval & ENE_IRQ_RX) && (retval & ENE_IRQ_TX))
519 ene_dbg("both RX and TX interrupt at same time");
520
521 return retval;
522}
523
524/* interrupt handler */
525static irqreturn_t ene_isr(int irq, void *data)
526{
527 u16 hw_value;
528 int i, hw_sample;
529 int pulse;
530 int irq_status;
531 unsigned long flags;
532 int carrier = 0;
533 irqreturn_t retval = IRQ_NONE;
534 struct ene_device *dev = (struct ene_device *)data;
535 struct ir_raw_event ev;
536
537
538 spin_lock_irqsave(&dev->hw_lock, flags);
539 irq_status = ene_irq_status(dev);
540
541 if (!irq_status)
542 goto unlock;
543
544 retval = IRQ_HANDLED;
545
546 if (irq_status & ENE_IRQ_TX) {
547
548 if (!dev->hw_learning_and_tx_capable) {
549 ene_dbg("TX interrupt on unsupported device!");
550 goto unlock;
551 }
552 ene_tx_sample(dev);
553 }
554
555 if (!(irq_status & ENE_IRQ_RX))
556 goto unlock;
557
558
559 if (dev->carrier_detect_enabled || debug)
560 carrier = ene_rx_sense_carrier(dev);
561#if 0
562 /* TODO */
563 if (dev->carrier_detect_enabled && carrier)
564 ir_raw_event_report_frequency(dev->idev, carrier);
565#endif
566
567 for (i = 0; i < ENE_SAMPLES_SIZE; i++) {
568 hw_value = ene_hw_read_reg(dev,
569 ENE_SAMPLE_BUFFER + dev->rx_pointer * 4 + i);
570
571 if (dev->rx_fan_input_inuse) {
572 /* read high part of the sample */
573 hw_value |= ene_hw_read_reg(dev,
574 ENE_SAMPLE_BUFFER_FAN +
575 dev->rx_pointer * 4 + i) << 8;
576 pulse = hw_value & ENE_FAN_SMPL_PULS_MSK;
577
578 /* clear space bit, and other unused bits */
579 hw_value &= ENE_FAN_VALUE_MASK;
580 hw_sample = hw_value * ENE_SAMPLE_PERIOD_FAN;
581
582 } else {
583 pulse = !(hw_value & ENE_SAMPLE_SPC_MASK);
584 hw_value &= ENE_SAMPLE_VALUE_MASK;
585 hw_sample = hw_value * sample_period;
586
587 if (dev->rx_period_adjust) {
588 hw_sample *= (100 - dev->rx_period_adjust);
589 hw_sample /= 100;
590 }
591 }
592 /* no more data */
593 if (!(hw_value))
594 break;
595
596 ene_dbg("RX: %d (%s)", hw_sample, pulse ? "pulse" : "space");
597
598
599 ev.duration = hw_sample * 1000;
600 ev.pulse = pulse;
601 ir_raw_event_store_with_filter(dev->idev, &ev);
602 }
603
604 ir_raw_event_handle(dev->idev);
605unlock:
606 spin_unlock_irqrestore(&dev->hw_lock, flags);
607 return retval;
608}
609
610/* Initialize default settings */
611static void ene_setup_settings(struct ene_device *dev)
612{
613 dev->tx_period = 32;
614 dev->tx_duty_cycle = 25; /*%*/
615 dev->transmitter_mask = 3;
616
617 /* Force learning mode if (input == 2), otherwise
618 let user set it with LIRC_SET_REC_CARRIER */
619 dev->learning_enabled =
620 (input == 2 && dev->hw_learning_and_tx_capable);
621
622 dev->rx_pointer = -1;
623
624}
625
626/* outside interface: called on first open*/
627static int ene_open(void *data)
628{
629 struct ene_device *dev = (struct ene_device *)data;
630 unsigned long flags;
631
632 spin_lock_irqsave(&dev->hw_lock, flags);
633 dev->in_use = 1;
634 ene_setup_settings(dev);
635 ene_rx_enable(dev);
636 spin_unlock_irqrestore(&dev->hw_lock, flags);
637 return 0;
638}
639
640/* outside interface: called on device close*/
641static void ene_close(void *data)
642{
643 struct ene_device *dev = (struct ene_device *)data;
644 unsigned long flags;
645 spin_lock_irqsave(&dev->hw_lock, flags);
646
647 ene_rx_disable(dev);
648 dev->in_use = 0;
649 spin_unlock_irqrestore(&dev->hw_lock, flags);
650}
651
652/* outside interface: set transmitter mask */
653static int ene_set_tx_mask(void *data, u32 tx_mask)
654{
655 struct ene_device *dev = (struct ene_device *)data;
656 unsigned long flags;
657 ene_dbg("TX: attempt to set transmitter mask %02x", tx_mask);
658
659 /* invalid txmask */
660 if (!tx_mask || tx_mask & ~0x3) {
661 ene_dbg("TX: invalid mask");
662 /* return count of transmitters */
663 return 2;
664 }
665
666 spin_lock_irqsave(&dev->hw_lock, flags);
667 dev->transmitter_mask = tx_mask;
668 spin_unlock_irqrestore(&dev->hw_lock, flags);
669 return 0;
670}
671
672/* outside interface : set tx carrier */
673static int ene_set_tx_carrier(void *data, u32 carrier)
674{
675 struct ene_device *dev = (struct ene_device *)data;
676 unsigned long flags;
677 u32 period = 1000000 / carrier; /* (1 / freq) (* # usec in 1 sec) */
678
679 ene_dbg("TX: attempt to set tx carrier to %d kHz", carrier);
680
681 if (period && (period > ENE_TX_PERIOD_MAX ||
682 period < ENE_TX_PERIOD_MIN)) {
683
684 ene_dbg("TX: out of range %d-%d carrier, "
685 "falling back to 32 kHz",
686 1000 / ENE_TX_PERIOD_MIN,
687 1000 / ENE_TX_PERIOD_MAX);
688
689 period = 32; /* this is just a coincidence!!! */
690 }
691 ene_dbg("TX: set carrier to %d kHz", carrier);
692
693 spin_lock_irqsave(&dev->hw_lock, flags);
694 dev->tx_period = period;
695 spin_unlock_irqrestore(&dev->hw_lock, flags);
696 return 0;
697}
698
699
700/* outside interface: enable learning mode */
701static int ene_set_learning_mode(void *data, int enable)
702{
703 struct ene_device *dev = (struct ene_device *)data;
704 unsigned long flags;
705 if (enable == dev->learning_enabled)
706 return 0;
707
708 spin_lock_irqsave(&dev->hw_lock, flags);
709 dev->learning_enabled = enable;
710 ene_rx_set_inputs(dev);
711 spin_unlock_irqrestore(&dev->hw_lock, flags);
712 return 0;
713}
714
715/* outside interface: set rec carrier */
716static int ene_set_rec_carrier(void *data, u32 min, u32 max)
717{
718 struct ene_device *dev = (struct ene_device *)data;
719 ene_set_learning_mode(dev,
720 max > ENE_NORMAL_RX_HI || min < ENE_NORMAL_RX_LOW);
721 return 0;
722}
723
724/* outside interface: enable or disable idle mode */
725static void ene_rx_set_idle(void *data, int idle)
726{
727 struct ene_device *dev = (struct ene_device *)data;
728 ene_dbg("%sabling idle mode", idle ? "en" : "dis");
729
730 ene_hw_write_reg_mask(dev, ENE_CIR_SAMPLE_PERIOD,
731 (enable_idle && idle) ? 0 : ENE_CIR_SAMPLE_OVERFLOW,
732 ENE_CIR_SAMPLE_OVERFLOW);
733}
734
735
736/* outside interface: transmit */
737static int ene_transmit(void *data, int *buf, u32 n)
738{
739 struct ene_device *dev = (struct ene_device *)data;
740 unsigned long flags;
741
742 dev->tx_buffer = buf;
743 dev->tx_len = n / sizeof(int);
744 dev->tx_pos = 0;
745 dev->tx_reg = 0;
746 dev->tx_done = 0;
747 dev->tx_sample = 0;
748 dev->tx_sample_pulse = 0;
749
750 ene_dbg("TX: %d samples", dev->tx_len);
751
752 spin_lock_irqsave(&dev->hw_lock, flags);
753
754 ene_tx_hw_set_transmiter_mask(dev);
755 ene_tx_prepare(dev);
756
757 /* Transmit first two samples */
758 ene_tx_sample(dev);
759 ene_tx_sample(dev);
760
761 spin_unlock_irqrestore(&dev->hw_lock, flags);
762
763 if (wait_for_completion_timeout(&dev->tx_complete, 2 * HZ) == 0) {
764 ene_dbg("TX: timeout");
765 spin_lock_irqsave(&dev->hw_lock, flags);
766 ene_tx_complete(dev);
767 spin_unlock_irqrestore(&dev->hw_lock, flags);
768 } else
769 ene_dbg("TX: done");
770 return n;
771}
772
773
774/* probe entry */
775static int ene_probe(struct pnp_dev *pnp_dev, const struct pnp_device_id *id)
776{
777 int error = -ENOMEM;
778 struct ir_dev_props *ir_props;
779 struct input_dev *input_dev;
780 struct ene_device *dev;
781
782 /* allocate memory */
783 input_dev = input_allocate_device();
784 ir_props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
785 dev = kzalloc(sizeof(struct ene_device), GFP_KERNEL);
786
787 if (!input_dev || !ir_props || !dev)
788 goto error;
789
790 /* validate resources */
791 error = -ENODEV;
792
793 if (!pnp_port_valid(pnp_dev, 0) ||
794 pnp_port_len(pnp_dev, 0) < ENE_MAX_IO)
795 goto error;
796
797 if (!pnp_irq_valid(pnp_dev, 0))
798 goto error;
799
800 dev->hw_io = pnp_port_start(pnp_dev, 0);
801 dev->irq = pnp_irq(pnp_dev, 0);
802 spin_lock_init(&dev->hw_lock);
803
804 /* claim the resources */
805 error = -EBUSY;
806 if (!request_region(dev->hw_io, ENE_MAX_IO, ENE_DRIVER_NAME))
807 goto error;
808
809 if (request_irq(dev->irq, ene_isr,
810 IRQF_SHARED, ENE_DRIVER_NAME, (void *)dev))
811 goto error;
812
813 pnp_set_drvdata(pnp_dev, dev);
814 dev->pnp_dev = pnp_dev;
815
816 /* detect hardware version and features */
817 error = ene_hw_detect(dev);
818 if (error)
819 goto error;
820
821 ene_setup_settings(dev);
822
823 if (!dev->hw_learning_and_tx_capable && txsim) {
824 dev->hw_learning_and_tx_capable = 1;
825 setup_timer(&dev->tx_sim_timer, ene_tx_irqsim,
826 (long unsigned int)dev);
827 ene_printk(KERN_WARNING,
828 "Simulation of TX activated\n");
829 }
830
831 ir_props->driver_type = RC_DRIVER_IR_RAW;
832 ir_props->allowed_protos = IR_TYPE_ALL;
833 ir_props->priv = dev;
834 ir_props->open = ene_open;
835 ir_props->close = ene_close;
836 ir_props->min_timeout = ENE_MINGAP * 1000;
837 ir_props->max_timeout = ENE_MAXGAP * 1000;
838 ir_props->timeout = ENE_MAXGAP * 1000;
839
840 if (dev->hw_revision == ENE_HW_B)
841 ir_props->s_idle = ene_rx_set_idle;
842
843
844 dev->props = ir_props;
845 dev->idev = input_dev;
846
847 /* don't allow too short/long sample periods */
848 if (sample_period < 5 || sample_period > 0x7F)
849 sample_period = -1;
850
851 /* choose default sample period */
852 if (sample_period == -1) {
853
854 sample_period = 50;
855
856 /* on revB, hardware idle mode eats first sample
857 if we set too low sample period */
858 if (dev->hw_revision == ENE_HW_B && enable_idle)
859 sample_period = 75;
860 }
861
862 ir_props->rx_resolution = sample_period * 1000;
863
864 if (dev->hw_learning_and_tx_capable) {
865
866 ir_props->s_learning_mode = ene_set_learning_mode;
867
868 if (input == 0)
869 ir_props->s_rx_carrier_range = ene_set_rec_carrier;
870
871 init_completion(&dev->tx_complete);
872 ir_props->tx_ir = ene_transmit;
873 ir_props->s_tx_mask = ene_set_tx_mask;
874 ir_props->s_tx_carrier = ene_set_tx_carrier;
875 ir_props->tx_resolution = ENE_TX_SMPL_PERIOD * 1000;
876 /* ir_props->s_carrier_report = ene_set_carrier_report; */
877 }
878
879
880 device_set_wakeup_capable(&pnp_dev->dev, 1);
881 device_set_wakeup_enable(&pnp_dev->dev, 1);
882
883 if (dev->hw_learning_and_tx_capable)
884 input_dev->name = "ENE eHome Infrared Remote Transceiver";
885 else
886 input_dev->name = "ENE eHome Infrared Remote Receiver";
887
888
889 error = -ENODEV;
890 if (ir_input_register(input_dev, RC_MAP_RC6_MCE, ir_props,
891 ENE_DRIVER_NAME))
892 goto error;
893
894
895 ene_printk(KERN_NOTICE, "driver has been succesfully loaded\n");
896 return 0;
897error:
898 if (dev->irq)
899 free_irq(dev->irq, dev);
900 if (dev->hw_io)
901 release_region(dev->hw_io, ENE_MAX_IO);
902
903 input_free_device(input_dev);
904 kfree(ir_props);
905 kfree(dev);
906 return error;
907}
908
909/* main unload function */
910static void ene_remove(struct pnp_dev *pnp_dev)
911{
912 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
913 unsigned long flags;
914
915 spin_lock_irqsave(&dev->hw_lock, flags);
916 ene_rx_disable(dev);
917 spin_unlock_irqrestore(&dev->hw_lock, flags);
918
919 free_irq(dev->irq, dev);
920 release_region(dev->hw_io, ENE_MAX_IO);
921 ir_input_unregister(dev->idev);
922 kfree(dev->props);
923 kfree(dev);
924}
925
926/* enable wake on IR (wakes on specific button on original remote) */
927static void ene_enable_wake(struct ene_device *dev, int enable)
928{
929 enable = enable && device_may_wakeup(&dev->pnp_dev->dev);
930
931 ene_dbg("wake on IR %s", enable ? "enabled" : "disabled");
932
933 ene_hw_write_reg_mask(dev, ENE_FW1, enable ?
934 ENE_FW1_WAKE : 0, ENE_FW1_WAKE);
935}
936
937#ifdef CONFIG_PM
938static int ene_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
939{
940 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
941 ene_enable_wake(dev, 1);
942 return 0;
943}
944
945static int ene_resume(struct pnp_dev *pnp_dev)
946{
947 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
948 if (dev->in_use)
949 ene_rx_enable(dev);
950
951 ene_enable_wake(dev, 0);
952 return 0;
953}
954#endif
955
956static void ene_shutdown(struct pnp_dev *pnp_dev)
957{
958 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
959 ene_enable_wake(dev, 1);
960}
961
962static const struct pnp_device_id ene_ids[] = {
963 {.id = "ENE0100",},
964 {.id = "ENE0200",},
965 {.id = "ENE0201",},
966 {.id = "ENE0202",},
967 {},
968};
969
970static struct pnp_driver ene_driver = {
971 .name = ENE_DRIVER_NAME,
972 .id_table = ene_ids,
973 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
974
975 .probe = ene_probe,
976 .remove = __devexit_p(ene_remove),
977#ifdef CONFIG_PM
978 .suspend = ene_suspend,
979 .resume = ene_resume,
980#endif
981 .shutdown = ene_shutdown,
982};
983
984static int __init ene_init(void)
985{
986 return pnp_register_driver(&ene_driver);
987}
988
989static void ene_exit(void)
990{
991 pnp_unregister_driver(&ene_driver);
992}
993
994module_param(sample_period, int, S_IRUGO);
995MODULE_PARM_DESC(sample_period, "Hardware sample period (50 us default)");
996
997module_param(enable_idle, bool, S_IRUGO | S_IWUSR);
998MODULE_PARM_DESC(enable_idle,
999 "Enables turning off signal sampling after long inactivity time; "
1000 "if disabled might help detecting input signal (default: enabled)"
1001 " (KB3926B only)");
1002
1003module_param(input, bool, S_IRUGO);
1004MODULE_PARM_DESC(input, "select which input to use "
1005 "0 - auto, 1 - standard, 2 - wideband(KB3926C+)");
1006
1007module_param(debug, int, S_IRUGO | S_IWUSR);
1008MODULE_PARM_DESC(debug, "Enable debug (debug=2 verbose debug output)");
1009
1010module_param(txsim, bool, S_IRUGO);
1011MODULE_PARM_DESC(txsim,
1012 "Simulate TX features on unsupported hardware (dangerous)");
1013
1014MODULE_DEVICE_TABLE(pnp, ene_ids);
1015MODULE_DESCRIPTION
1016 ("Infrared input driver for KB3926B/KB3926C/KB3926D "
1017 "(aka ENE0100/ENE0200/ENE0201) CIR port");
1018
1019MODULE_AUTHOR("Maxim Levitsky");
1020MODULE_LICENSE("GPL");
1021
1022module_init(ene_init);
1023module_exit(ene_exit);
diff --git a/drivers/media/IR/ene_ir.h b/drivers/media/IR/ene_ir.h
new file mode 100644
index 00000000000..54c76af0d03
--- /dev/null
+++ b/drivers/media/IR/ene_ir.h
@@ -0,0 +1,235 @@
1/*
2 * driver for ENE KB3926 B/C/D CIR (also known as ENE0XXX)
3 *
4 * Copyright (C) 2010 Maxim Levitsky <maximlevitsky@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
19 * USA
20 */
21#include <linux/spinlock.h>
22
23
24/* hardware address */
25#define ENE_STATUS 0 /* hardware status - unused */
26#define ENE_ADDR_HI 1 /* hi byte of register address */
27#define ENE_ADDR_LO 2 /* low byte of register address */
28#define ENE_IO 3 /* read/write window */
29#define ENE_MAX_IO 4
30
31/* 8 bytes of samples, divided in 2 halfs*/
32#define ENE_SAMPLE_BUFFER 0xF8F0 /* regular sample buffer */
33#define ENE_SAMPLE_SPC_MASK 0x80 /* sample is space */
34#define ENE_SAMPLE_VALUE_MASK 0x7F
35#define ENE_SAMPLE_OVERFLOW 0x7F
36#define ENE_SAMPLES_SIZE 4
37
38/* fan input sample buffer */
39#define ENE_SAMPLE_BUFFER_FAN 0xF8FB /* this buffer holds high byte of */
40 /* each sample of normal buffer */
41#define ENE_FAN_SMPL_PULS_MSK 0x8000 /* this bit of combined sample */
42 /* if set, says that sample is pulse */
43#define ENE_FAN_VALUE_MASK 0x0FFF /* mask for valid bits of the value */
44
45/* first firmware register */
46#define ENE_FW1 0xF8F8
47#define ENE_FW1_ENABLE 0x01 /* enable fw processing */
48#define ENE_FW1_TXIRQ 0x02 /* TX interrupt pending */
49#define ENE_FW1_WAKE 0x40 /* enable wake from S3 */
50#define ENE_FW1_IRQ 0x80 /* enable interrupt */
51
52/* second firmware register */
53#define ENE_FW2 0xF8F9
54#define ENE_FW2_BUF_HIGH 0x01 /* which half of the buffer to read */
55#define ENE_FW2_IRQ_CLR 0x04 /* clear this on IRQ */
56#define ENE_FW2_GP40_AS_LEARN 0x08 /* normal input is used as */
57 /* learning input */
58#define ENE_FW2_FAN_AS_NRML_IN 0x40 /* fan is used as normal input */
59#define ENE_FW2_LEARNING 0x80 /* hardware supports learning and TX */
60
61/* transmitter ports */
62#define ENE_TX_PORT2 0xFC01 /* this enables one or both */
63#define ENE_TX_PORT2_EN 0x20 /* TX ports */
64#define ENE_TX_PORT1 0xFC08
65#define ENE_TX_PORT1_EN 0x02
66
67/* IRQ registers block (for revision B) */
68#define ENEB_IRQ 0xFD09 /* IRQ number */
69#define ENEB_IRQ_UNK1 0xFD17 /* unknown setting = 1 */
70#define ENEB_IRQ_STATUS 0xFD80 /* irq status */
71#define ENEB_IRQ_STATUS_IR 0x20 /* IR irq */
72
73/* fan as input settings - only if learning capable */
74#define ENE_FAN_AS_IN1 0xFE30 /* fan init reg 1 */
75#define ENE_FAN_AS_IN1_EN 0xCD
76#define ENE_FAN_AS_IN2 0xFE31 /* fan init reg 2 */
77#define ENE_FAN_AS_IN2_EN 0x03
78#define ENE_SAMPLE_PERIOD_FAN 61 /* fan input has fixed sample period */
79
80/* IRQ registers block (for revision C,D) */
81#define ENEC_IRQ 0xFE9B /* new irq settings register */
82#define ENEC_IRQ_MASK 0x0F /* irq number mask */
83#define ENEC_IRQ_UNK_EN 0x10 /* always enabled */
84#define ENEC_IRQ_STATUS 0x20 /* irq status and ACK */
85
86/* CIR block settings */
87#define ENE_CIR_CONF1 0xFEC0
88#define ENE_CIR_CONF1_TX_CLEAR 0x01 /* clear that on revC */
89 /* while transmitting */
90#define ENE_CIR_CONF1_RX_ON 0x07 /* normal receiver enabled */
91#define ENE_CIR_CONF1_LEARN1 0x08 /* enabled on learning mode */
92#define ENE_CIR_CONF1_TX_ON 0x30 /* enabled on transmit */
93#define ENE_CIR_CONF1_TX_CARR 0x80 /* send TX carrier or not */
94
95#define ENE_CIR_CONF2 0xFEC1 /* unknown setting = 0 */
96#define ENE_CIR_CONF2_LEARN2 0x10 /* set on enable learning */
97#define ENE_CIR_CONF2_GPIO40DIS 0x20 /* disable input via gpio40 */
98
99#define ENE_CIR_SAMPLE_PERIOD 0xFEC8 /* sample period in us */
100#define ENE_CIR_SAMPLE_OVERFLOW 0x80 /* interrupt on overflows if set */
101
102
103/* Two byte tx buffer */
104#define ENE_TX_INPUT1 0xFEC9
105#define ENE_TX_INPUT2 0xFECA
106#define ENE_TX_PULSE_MASK 0x80 /* Transmitted sample is pulse */
107#define ENE_TX_SMLP_MASK 0x7F
108#define ENE_TX_SMPL_PERIOD 50 /* transmit sample period - fixed */
109
110
111/* Unknown TX setting - TX sample period ??? */
112#define ENE_TX_UNK1 0xFECB /* set to 0x63 */
113
114/* Current received carrier period */
115#define ENE_RX_CARRIER 0xFECC /* RX period (500 ns) */
116#define ENE_RX_CARRIER_VALID 0x80 /* Register content valid */
117
118
119/* TX period (1/carrier) */
120#define ENE_TX_PERIOD 0xFECE /* TX period (500 ns) */
121#define ENE_TX_PERIOD_UNKBIT 0x80 /* This bit set on transmit*/
122#define ENE_TX_PERIOD_PULSE 0xFECF /* TX pulse period (500 ns)*/
123
124/* Hardware versions */
125#define ENE_HW_VERSION 0xFF00 /* hardware revision */
126#define ENE_PLLFRH 0xFF16
127#define ENE_PLLFRL 0xFF17
128
129#define ENE_HW_UNK 0xFF1D
130#define ENE_HW_UNK_CLR 0x04
131#define ENE_HW_VER_MAJOR 0xFF1E /* chip version */
132#define ENE_HW_VER_MINOR 0xFF1F
133#define ENE_HW_VER_OLD 0xFD00
134
135/* Normal/Learning carrier ranges - only valid if we have learning input*/
136/* TODO: test */
137#define ENE_NORMAL_RX_LOW 34
138#define ENE_NORMAL_RX_HI 38
139
140/* Tx carrier range */
141/* Hardware might be able to do more, but this range is enough for
142 all purposes */
143#define ENE_TX_PERIOD_MAX 32 /* corresponds to 29.4 kHz */
144#define ENE_TX_PERIOD_MIN 16 /* corrsponds to 62.5 kHz */
145
146
147
148/* Minimal and maximal gaps */
149
150/* Normal case:
151 Minimal gap is 0x7F * sample period
152 Maximum gap depends on hardware.
153 For KB3926B, it is unlimited, for newer models its around
154 250000, after which HW stops sending samples, and that is
155 not possible to change */
156
157/* Fan case:
158 Both minimal and maximal gaps are same, and equal to 0xFFF * 0x61
159 And there is nothing to change this setting
160*/
161
162#define ENE_MAXGAP 250000
163#define ENE_MINGAP (127 * sample_period)
164
165/******************************************************************************/
166
167#define ENE_DRIVER_NAME "ene_ir"
168
169#define ENE_IRQ_RX 1
170#define ENE_IRQ_TX 2
171
172#define ENE_HW_B 1 /* 3926B */
173#define ENE_HW_C 2 /* 3926C */
174#define ENE_HW_D 3 /* 3926D */
175
176#define ene_printk(level, text, ...) \
177 printk(level ENE_DRIVER_NAME ": " text, ## __VA_ARGS__)
178
179#define ene_dbg(text, ...) \
180 if (debug) \
181 printk(KERN_DEBUG \
182 ENE_DRIVER_NAME ": " text "\n" , ## __VA_ARGS__)
183
184#define ene_dbg_verbose(text, ...) \
185 if (debug > 1) \
186 printk(KERN_DEBUG \
187 ENE_DRIVER_NAME ": " text "\n" , ## __VA_ARGS__)
188
189
190struct ene_device {
191 struct pnp_dev *pnp_dev;
192 struct input_dev *idev;
193 struct ir_dev_props *props;
194 int in_use;
195
196 /* hw IO settings */
197 unsigned long hw_io;
198 int irq;
199 spinlock_t hw_lock;
200
201 /* HW features */
202 int hw_revision; /* hardware revision */
203 bool hw_learning_and_tx_capable; /* learning capable */
204 bool hw_gpio40_learning; /* gpio40 is learning */
205 bool hw_fan_as_normal_input; /* fan input is used as */
206 /* regular input */
207 /* HW state*/
208 int rx_pointer; /* hw pointer to rx buffer */
209 bool rx_fan_input_inuse; /* is fan input in use for rx*/
210 int tx_reg; /* current reg used for TX */
211 u8 saved_conf1; /* saved FEC0 reg */
212
213 /* TX sample handling */
214 unsigned int tx_sample; /* current sample for TX */
215 bool tx_sample_pulse; /* current sample is pulse */
216
217 /* TX buffer */
218 int *tx_buffer; /* input samples buffer*/
219 int tx_pos; /* position in that bufer */
220 int tx_len; /* current len of tx buffer */
221 int tx_done; /* done transmitting */
222 /* one more sample pending*/
223 struct completion tx_complete; /* TX completion */
224 struct timer_list tx_sim_timer;
225
226 /* TX settings */
227 int tx_period;
228 int tx_duty_cycle;
229 int transmitter_mask;
230
231 /* RX settings */
232 bool learning_enabled; /* learning input enabled */
233 bool carrier_detect_enabled; /* carrier detect enabled */
234 int rx_period_adjust;
235};
diff --git a/drivers/media/IR/imon.c b/drivers/media/IR/imon.c
index 65c125e44e9..c185422ef28 100644
--- a/drivers/media/IR/imon.c
+++ b/drivers/media/IR/imon.c
@@ -87,7 +87,6 @@ static ssize_t lcd_write(struct file *file, const char *buf,
87struct imon_context { 87struct imon_context {
88 struct device *dev; 88 struct device *dev;
89 struct ir_dev_props *props; 89 struct ir_dev_props *props;
90 struct ir_input_dev *ir;
91 /* Newer devices have two interfaces */ 90 /* Newer devices have two interfaces */
92 struct usb_device *usbdev_intf0; 91 struct usb_device *usbdev_intf0;
93 struct usb_device *usbdev_intf1; 92 struct usb_device *usbdev_intf1;
@@ -1656,7 +1655,6 @@ static struct input_dev *imon_init_idev(struct imon_context *ictx)
1656{ 1655{
1657 struct input_dev *idev; 1656 struct input_dev *idev;
1658 struct ir_dev_props *props; 1657 struct ir_dev_props *props;
1659 struct ir_input_dev *ir;
1660 int ret, i; 1658 int ret, i;
1661 1659
1662 idev = input_allocate_device(); 1660 idev = input_allocate_device();
@@ -1671,12 +1669,6 @@ static struct input_dev *imon_init_idev(struct imon_context *ictx)
1671 goto props_alloc_failed; 1669 goto props_alloc_failed;
1672 } 1670 }
1673 1671
1674 ir = kzalloc(sizeof(struct ir_input_dev), GFP_KERNEL);
1675 if (!ir) {
1676 dev_err(ictx->dev, "remote ir input dev allocation failed\n");
1677 goto ir_dev_alloc_failed;
1678 }
1679
1680 snprintf(ictx->name_idev, sizeof(ictx->name_idev), 1672 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
1681 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product); 1673 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1682 idev->name = ictx->name_idev; 1674 idev->name = ictx->name_idev;
@@ -1706,14 +1698,9 @@ static struct input_dev *imon_init_idev(struct imon_context *ictx)
1706 props->change_protocol = imon_ir_change_protocol; 1698 props->change_protocol = imon_ir_change_protocol;
1707 ictx->props = props; 1699 ictx->props = props;
1708 1700
1709 ictx->ir = ir;
1710 memcpy(&ir->dev, ictx->dev, sizeof(struct device));
1711
1712 usb_to_input_id(ictx->usbdev_intf0, &idev->id); 1701 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1713 idev->dev.parent = ictx->dev; 1702 idev->dev.parent = ictx->dev;
1714 1703
1715 input_set_drvdata(idev, ir);
1716
1717 ret = ir_input_register(idev, RC_MAP_IMON_PAD, props, MOD_NAME); 1704 ret = ir_input_register(idev, RC_MAP_IMON_PAD, props, MOD_NAME);
1718 if (ret < 0) { 1705 if (ret < 0) {
1719 dev_err(ictx->dev, "remote input dev register failed\n"); 1706 dev_err(ictx->dev, "remote input dev register failed\n");
@@ -1723,8 +1710,6 @@ static struct input_dev *imon_init_idev(struct imon_context *ictx)
1723 return idev; 1710 return idev;
1724 1711
1725idev_register_failed: 1712idev_register_failed:
1726 kfree(ir);
1727ir_dev_alloc_failed:
1728 kfree(props); 1713 kfree(props);
1729props_alloc_failed: 1714props_alloc_failed:
1730 input_free_device(idev); 1715 input_free_device(idev);
@@ -1944,7 +1929,6 @@ static struct imon_context *imon_init_intf0(struct usb_interface *intf)
1944 1929
1945urb_submit_failed: 1930urb_submit_failed:
1946 ir_input_unregister(ictx->idev); 1931 ir_input_unregister(ictx->idev);
1947 input_free_device(ictx->idev);
1948idev_setup_failed: 1932idev_setup_failed:
1949find_endpoint_failed: 1933find_endpoint_failed:
1950 mutex_unlock(&ictx->lock); 1934 mutex_unlock(&ictx->lock);
@@ -2014,10 +1998,8 @@ static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2014 return ictx; 1998 return ictx;
2015 1999
2016urb_submit_failed: 2000urb_submit_failed:
2017 if (ictx->touch) { 2001 if (ictx->touch)
2018 input_unregister_device(ictx->touch); 2002 input_unregister_device(ictx->touch);
2019 input_free_device(ictx->touch);
2020 }
2021touch_setup_failed: 2003touch_setup_failed:
2022find_endpoint_failed: 2004find_endpoint_failed:
2023 mutex_unlock(&ictx->lock); 2005 mutex_unlock(&ictx->lock);
diff --git a/drivers/media/IR/ir-core-priv.h b/drivers/media/IR/ir-core-priv.h
index babd52061bc..a85a8c7c905 100644
--- a/drivers/media/IR/ir-core-priv.h
+++ b/drivers/media/IR/ir-core-priv.h
@@ -32,7 +32,7 @@ struct ir_raw_handler {
32 32
33struct ir_raw_event_ctrl { 33struct ir_raw_event_ctrl {
34 struct list_head list; /* to keep track of raw clients */ 34 struct list_head list; /* to keep track of raw clients */
35 struct work_struct rx_work; /* for the rx decoding workqueue */ 35 struct task_struct *thread;
36 struct kfifo kfifo; /* fifo for the pulse/space durations */ 36 struct kfifo kfifo; /* fifo for the pulse/space durations */
37 ktime_t last_event; /* when last event occurred */ 37 ktime_t last_event; /* when last event occurred */
38 enum raw_event_type last_type; /* last event type */ 38 enum raw_event_type last_type; /* last event type */
@@ -41,10 +41,13 @@ struct ir_raw_event_ctrl {
41 41
42 /* raw decoder state follows */ 42 /* raw decoder state follows */
43 struct ir_raw_event prev_ev; 43 struct ir_raw_event prev_ev;
44 struct ir_raw_event this_ev;
44 struct nec_dec { 45 struct nec_dec {
45 int state; 46 int state;
46 unsigned count; 47 unsigned count;
47 u32 bits; 48 u32 bits;
49 bool is_nec_x;
50 bool necx_repeat;
48 } nec; 51 } nec;
49 struct rc5_dec { 52 struct rc5_dec {
50 int state; 53 int state;
@@ -76,7 +79,7 @@ struct ir_raw_event_ctrl {
76 struct lirc_codec { 79 struct lirc_codec {
77 struct ir_input_dev *ir_dev; 80 struct ir_input_dev *ir_dev;
78 struct lirc_driver *drv; 81 struct lirc_driver *drv;
79 int lircdata; 82 int carrier_low;
80 } lirc; 83 } lirc;
81}; 84};
82 85
@@ -104,10 +107,9 @@ static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration)
104 ev->duration -= duration; 107 ev->duration -= duration;
105} 108}
106 109
107#define TO_US(duration) (((duration) + 500) / 1000) 110#define TO_US(duration) DIV_ROUND_CLOSEST((duration), 1000)
108#define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space") 111#define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space")
109#define IS_RESET(ev) (ev.duration == 0) 112#define IS_RESET(ev) (ev.duration == 0)
110
111/* 113/*
112 * Routines from ir-sysfs.c - Meant to be called only internally inside 114 * Routines from ir-sysfs.c - Meant to be called only internally inside
113 * ir-core 115 * ir-core
@@ -126,7 +128,8 @@ int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler);
126void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler); 128void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler);
127void ir_raw_init(void); 129void ir_raw_init(void);
128 130
129 131int ir_rcmap_init(void);
132void ir_rcmap_cleanup(void);
130/* 133/*
131 * Decoder initialization code 134 * Decoder initialization code
132 * 135 *
diff --git a/drivers/media/IR/ir-jvc-decoder.c b/drivers/media/IR/ir-jvc-decoder.c
index 8894d8b3604..77a89c4de01 100644
--- a/drivers/media/IR/ir-jvc-decoder.c
+++ b/drivers/media/IR/ir-jvc-decoder.c
@@ -32,6 +32,7 @@ enum jvc_state {
32 STATE_BIT_SPACE, 32 STATE_BIT_SPACE,
33 STATE_TRAILER_PULSE, 33 STATE_TRAILER_PULSE,
34 STATE_TRAILER_SPACE, 34 STATE_TRAILER_SPACE,
35 STATE_CHECK_REPEAT,
35}; 36};
36 37
37/** 38/**
@@ -60,6 +61,7 @@ static int ir_jvc_decode(struct input_dev *input_dev, struct ir_raw_event ev)
60 IR_dprintk(2, "JVC decode started at state %d (%uus %s)\n", 61 IR_dprintk(2, "JVC decode started at state %d (%uus %s)\n",
61 data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 62 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
62 63
64again:
63 switch (data->state) { 65 switch (data->state) {
64 66
65 case STATE_INACTIVE: 67 case STATE_INACTIVE:
@@ -149,8 +151,18 @@ static int ir_jvc_decode(struct input_dev *input_dev, struct ir_raw_event ev)
149 } 151 }
150 152
151 data->count = 0; 153 data->count = 0;
152 data->state = STATE_BIT_PULSE; 154 data->state = STATE_CHECK_REPEAT;
153 return 0; 155 return 0;
156
157 case STATE_CHECK_REPEAT:
158 if (!ev.pulse)
159 break;
160
161 if (eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
162 data->state = STATE_INACTIVE;
163 else
164 data->state = STATE_BIT_PULSE;
165 goto again;
154 } 166 }
155 167
156out: 168out:
diff --git a/drivers/media/IR/ir-keytable.c b/drivers/media/IR/ir-keytable.c
index 15a0f192d41..7e82a9df726 100644
--- a/drivers/media/IR/ir-keytable.c
+++ b/drivers/media/IR/ir-keytable.c
@@ -339,6 +339,8 @@ void ir_repeat(struct input_dev *dev)
339 339
340 spin_lock_irqsave(&ir->keylock, flags); 340 spin_lock_irqsave(&ir->keylock, flags);
341 341
342 input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode);
343
342 if (!ir->keypressed) 344 if (!ir->keypressed)
343 goto out; 345 goto out;
344 346
@@ -370,6 +372,8 @@ void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
370 372
371 spin_lock_irqsave(&ir->keylock, flags); 373 spin_lock_irqsave(&ir->keylock, flags);
372 374
375 input_event(dev, EV_MSC, MSC_SCAN, scancode);
376
373 /* Repeat event? */ 377 /* Repeat event? */
374 if (ir->keypressed && 378 if (ir->keypressed &&
375 ir->last_scancode == scancode && 379 ir->last_scancode == scancode &&
@@ -383,9 +387,11 @@ void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
383 ir->last_toggle = toggle; 387 ir->last_toggle = toggle;
384 ir->last_keycode = keycode; 388 ir->last_keycode = keycode;
385 389
390
386 if (keycode == KEY_RESERVED) 391 if (keycode == KEY_RESERVED)
387 goto out; 392 goto out;
388 393
394
389 /* Register a keypress */ 395 /* Register a keypress */
390 ir->keypressed = true; 396 ir->keypressed = true;
391 IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n", 397 IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
@@ -428,7 +434,7 @@ static void ir_close(struct input_dev *input_dev)
428 */ 434 */
429int __ir_input_register(struct input_dev *input_dev, 435int __ir_input_register(struct input_dev *input_dev,
430 const struct ir_scancode_table *rc_tab, 436 const struct ir_scancode_table *rc_tab,
431 const struct ir_dev_props *props, 437 struct ir_dev_props *props,
432 const char *driver_name) 438 const char *driver_name)
433{ 439{
434 struct ir_input_dev *ir_dev; 440 struct ir_input_dev *ir_dev;
@@ -480,6 +486,8 @@ int __ir_input_register(struct input_dev *input_dev,
480 486
481 set_bit(EV_KEY, input_dev->evbit); 487 set_bit(EV_KEY, input_dev->evbit);
482 set_bit(EV_REP, input_dev->evbit); 488 set_bit(EV_REP, input_dev->evbit);
489 set_bit(EV_MSC, input_dev->evbit);
490 set_bit(MSC_SCAN, input_dev->mscbit);
483 491
484 if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) { 492 if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
485 rc = -ENOMEM; 493 rc = -ENOMEM;
@@ -499,7 +507,8 @@ int __ir_input_register(struct input_dev *input_dev,
499 507
500 IR_dprintk(1, "Registered input device on %s for %s remote%s.\n", 508 IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
501 driver_name, rc_tab->name, 509 driver_name, rc_tab->name,
502 ir_dev->props->driver_type == RC_DRIVER_IR_RAW ? " in raw mode" : ""); 510 (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ?
511 " in raw mode" : "");
503 512
504 return 0; 513 return 0;
505 514
diff --git a/drivers/media/IR/ir-lirc-codec.c b/drivers/media/IR/ir-lirc-codec.c
index 3ba482d96c4..77b5946413c 100644
--- a/drivers/media/IR/ir-lirc-codec.c
+++ b/drivers/media/IR/ir-lirc-codec.c
@@ -32,6 +32,7 @@
32static int ir_lirc_decode(struct input_dev *input_dev, struct ir_raw_event ev) 32static int ir_lirc_decode(struct input_dev *input_dev, struct ir_raw_event ev)
33{ 33{
34 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); 34 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
35 int sample;
35 36
36 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC)) 37 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC))
37 return 0; 38 return 0;
@@ -39,18 +40,20 @@ static int ir_lirc_decode(struct input_dev *input_dev, struct ir_raw_event ev)
39 if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf) 40 if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf)
40 return -EINVAL; 41 return -EINVAL;
41 42
43 if (IS_RESET(ev))
44 return 0;
45
42 IR_dprintk(2, "LIRC data transfer started (%uus %s)\n", 46 IR_dprintk(2, "LIRC data transfer started (%uus %s)\n",
43 TO_US(ev.duration), TO_STR(ev.pulse)); 47 TO_US(ev.duration), TO_STR(ev.pulse));
44 48
45 ir_dev->raw->lirc.lircdata += ev.duration / 1000; 49 sample = ev.duration / 1000;
46 if (ev.pulse) 50 if (ev.pulse)
47 ir_dev->raw->lirc.lircdata |= PULSE_BIT; 51 sample |= PULSE_BIT;
48 52
49 lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf, 53 lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf,
50 (unsigned char *) &ir_dev->raw->lirc.lircdata); 54 (unsigned char *) &sample);
51 wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll); 55 wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll);
52 56
53 ir_dev->raw->lirc.lircdata = 0;
54 57
55 return 0; 58 return 0;
56} 59}
@@ -92,13 +95,14 @@ out:
92 return ret; 95 return ret;
93} 96}
94 97
95static long ir_lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) 98static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
99 unsigned long __user arg)
96{ 100{
97 struct lirc_codec *lirc; 101 struct lirc_codec *lirc;
98 struct ir_input_dev *ir_dev; 102 struct ir_input_dev *ir_dev;
99 int ret = 0; 103 int ret = 0;
100 void *drv_data; 104 void *drv_data;
101 unsigned long val; 105 unsigned long val = 0;
102 106
103 lirc = lirc_get_pdata(filep); 107 lirc = lirc_get_pdata(filep);
104 if (!lirc) 108 if (!lirc)
@@ -110,47 +114,106 @@ static long ir_lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long ar
110 114
111 drv_data = ir_dev->props->priv; 115 drv_data = ir_dev->props->priv;
112 116
113 switch (cmd) { 117 if (_IOC_DIR(cmd) & _IOC_WRITE) {
114 case LIRC_SET_TRANSMITTER_MASK:
115 ret = get_user(val, (unsigned long *)arg); 118 ret = get_user(val, (unsigned long *)arg);
116 if (ret) 119 if (ret)
117 return ret; 120 return ret;
121 }
122
123 switch (cmd) {
118 124
119 if (ir_dev->props && ir_dev->props->s_tx_mask) 125 /* legacy support */
126 case LIRC_GET_SEND_MODE:
127 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
128 break;
129
130 case LIRC_SET_SEND_MODE:
131 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
132 return -EINVAL;
133 break;
134
135 /* TX settings */
136 case LIRC_SET_TRANSMITTER_MASK:
137 if (ir_dev->props->s_tx_mask)
120 ret = ir_dev->props->s_tx_mask(drv_data, (u32)val); 138 ret = ir_dev->props->s_tx_mask(drv_data, (u32)val);
121 else 139 else
122 return -EINVAL; 140 return -EINVAL;
123 break; 141 break;
124 142
125 case LIRC_SET_SEND_CARRIER: 143 case LIRC_SET_SEND_CARRIER:
126 ret = get_user(val, (unsigned long *)arg); 144 if (ir_dev->props->s_tx_carrier)
127 if (ret)
128 return ret;
129
130 if (ir_dev->props && ir_dev->props->s_tx_carrier)
131 ir_dev->props->s_tx_carrier(drv_data, (u32)val); 145 ir_dev->props->s_tx_carrier(drv_data, (u32)val);
132 else 146 else
133 return -EINVAL; 147 return -EINVAL;
134 break; 148 break;
135 149
136 case LIRC_GET_SEND_MODE: 150 case LIRC_SET_SEND_DUTY_CYCLE:
137 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK; 151 if (!ir_dev->props->s_tx_duty_cycle)
138 ret = put_user(val, (unsigned long *)arg); 152 return -ENOSYS;
153
154 if (val <= 0 || val >= 100)
155 return -EINVAL;
156
157 ir_dev->props->s_tx_duty_cycle(ir_dev->props->priv, val);
139 break; 158 break;
140 159
141 case LIRC_SET_SEND_MODE: 160 /* RX settings */
142 ret = get_user(val, (unsigned long *)arg); 161 case LIRC_SET_REC_CARRIER:
143 if (ret) 162 if (ir_dev->props->s_rx_carrier_range)
144 return ret; 163 ret = ir_dev->props->s_rx_carrier_range(
164 ir_dev->props->priv,
165 ir_dev->raw->lirc.carrier_low, val);
166 else
167 return -ENOSYS;
145 168
146 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK)) 169 if (!ret)
170 ir_dev->raw->lirc.carrier_low = 0;
171 break;
172
173 case LIRC_SET_REC_CARRIER_RANGE:
174 if (val >= 0)
175 ir_dev->raw->lirc.carrier_low = val;
176 break;
177
178
179 case LIRC_GET_REC_RESOLUTION:
180 val = ir_dev->props->rx_resolution;
181 break;
182
183 case LIRC_SET_WIDEBAND_RECEIVER:
184 if (ir_dev->props->s_learning_mode)
185 return ir_dev->props->s_learning_mode(
186 ir_dev->props->priv, !!val);
187 else
188 return -ENOSYS;
189
190 /* Generic timeout support */
191 case LIRC_GET_MIN_TIMEOUT:
192 if (!ir_dev->props->max_timeout)
193 return -ENOSYS;
194 val = ir_dev->props->min_timeout / 1000;
195 break;
196
197 case LIRC_GET_MAX_TIMEOUT:
198 if (!ir_dev->props->max_timeout)
199 return -ENOSYS;
200 val = ir_dev->props->max_timeout / 1000;
201 break;
202
203 case LIRC_SET_REC_TIMEOUT:
204 if (val < ir_dev->props->min_timeout ||
205 val > ir_dev->props->max_timeout)
147 return -EINVAL; 206 return -EINVAL;
207 ir_dev->props->timeout = val * 1000;
148 break; 208 break;
149 209
150 default: 210 default:
151 return lirc_dev_fop_ioctl(filep, cmd, arg); 211 return lirc_dev_fop_ioctl(filep, cmd, arg);
152 } 212 }
153 213
214 if (_IOC_DIR(cmd) & _IOC_READ)
215 ret = put_user(val, (unsigned long *)arg);
216
154 return ret; 217 return ret;
155} 218}
156 219
@@ -196,13 +259,28 @@ static int ir_lirc_register(struct input_dev *input_dev)
196 259
197 features = LIRC_CAN_REC_MODE2; 260 features = LIRC_CAN_REC_MODE2;
198 if (ir_dev->props->tx_ir) { 261 if (ir_dev->props->tx_ir) {
262
199 features |= LIRC_CAN_SEND_PULSE; 263 features |= LIRC_CAN_SEND_PULSE;
200 if (ir_dev->props->s_tx_mask) 264 if (ir_dev->props->s_tx_mask)
201 features |= LIRC_CAN_SET_TRANSMITTER_MASK; 265 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
202 if (ir_dev->props->s_tx_carrier) 266 if (ir_dev->props->s_tx_carrier)
203 features |= LIRC_CAN_SET_SEND_CARRIER; 267 features |= LIRC_CAN_SET_SEND_CARRIER;
268
269 if (ir_dev->props->s_tx_duty_cycle)
270 features |= LIRC_CAN_SET_REC_DUTY_CYCLE;
204 } 271 }
205 272
273 if (ir_dev->props->s_rx_carrier_range)
274 features |= LIRC_CAN_SET_REC_CARRIER |
275 LIRC_CAN_SET_REC_CARRIER_RANGE;
276
277 if (ir_dev->props->s_learning_mode)
278 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
279
280 if (ir_dev->props->max_timeout)
281 features |= LIRC_CAN_SET_REC_TIMEOUT;
282
283
206 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)", 284 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
207 ir_dev->driver_name); 285 ir_dev->driver_name);
208 drv->minor = -1; 286 drv->minor = -1;
@@ -224,8 +302,6 @@ static int ir_lirc_register(struct input_dev *input_dev)
224 302
225 ir_dev->raw->lirc.drv = drv; 303 ir_dev->raw->lirc.drv = drv;
226 ir_dev->raw->lirc.ir_dev = ir_dev; 304 ir_dev->raw->lirc.ir_dev = ir_dev;
227 ir_dev->raw->lirc.lircdata = PULSE_MASK;
228
229 return 0; 305 return 0;
230 306
231lirc_register_failed: 307lirc_register_failed:
diff --git a/drivers/media/IR/ir-nec-decoder.c b/drivers/media/IR/ir-nec-decoder.c
index 52e0f378ae3..d597421d654 100644
--- a/drivers/media/IR/ir-nec-decoder.c
+++ b/drivers/media/IR/ir-nec-decoder.c
@@ -20,12 +20,13 @@
20#define NEC_HEADER_PULSE (16 * NEC_UNIT) 20#define NEC_HEADER_PULSE (16 * NEC_UNIT)
21#define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */ 21#define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */
22#define NEC_HEADER_SPACE (8 * NEC_UNIT) 22#define NEC_HEADER_SPACE (8 * NEC_UNIT)
23#define NEC_REPEAT_SPACE (8 * NEC_UNIT) 23#define NEC_REPEAT_SPACE (4 * NEC_UNIT)
24#define NEC_BIT_PULSE (1 * NEC_UNIT) 24#define NEC_BIT_PULSE (1 * NEC_UNIT)
25#define NEC_BIT_0_SPACE (1 * NEC_UNIT) 25#define NEC_BIT_0_SPACE (1 * NEC_UNIT)
26#define NEC_BIT_1_SPACE (3 * NEC_UNIT) 26#define NEC_BIT_1_SPACE (3 * NEC_UNIT)
27#define NEC_TRAILER_PULSE (1 * NEC_UNIT) 27#define NEC_TRAILER_PULSE (1 * NEC_UNIT)
28#define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */ 28#define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */
29#define NECX_REPEAT_BITS 1
29 30
30enum nec_state { 31enum nec_state {
31 STATE_INACTIVE, 32 STATE_INACTIVE,
@@ -67,8 +68,12 @@ static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev)
67 if (!ev.pulse) 68 if (!ev.pulse)
68 break; 69 break;
69 70
70 if (!eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2) && 71 if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2)) {
71 !eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2)) 72 data->is_nec_x = false;
73 data->necx_repeat = false;
74 } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
75 data->is_nec_x = true;
76 else
72 break; 77 break;
73 78
74 data->count = 0; 79 data->count = 0;
@@ -105,6 +110,17 @@ static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev)
105 if (ev.pulse) 110 if (ev.pulse)
106 break; 111 break;
107 112
113 if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
114 geq_margin(ev.duration,
115 NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
116 IR_dprintk(1, "Repeat last key\n");
117 ir_repeat(input_dev);
118 data->state = STATE_INACTIVE;
119 return 0;
120
121 } else if (data->count > NECX_REPEAT_BITS)
122 data->necx_repeat = false;
123
108 data->bits <<= 1; 124 data->bits <<= 1;
109 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2)) 125 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
110 data->bits |= 1; 126 data->bits |= 1;
@@ -159,6 +175,9 @@ static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev)
159 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode); 175 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
160 } 176 }
161 177
178 if (data->is_nec_x)
179 data->necx_repeat = true;
180
162 ir_keydown(input_dev, scancode, 0); 181 ir_keydown(input_dev, scancode, 0);
163 data->state = STATE_INACTIVE; 182 data->state = STATE_INACTIVE;
164 return 0; 183 return 0;
diff --git a/drivers/media/IR/ir-raw-event.c b/drivers/media/IR/ir-raw-event.c
index 6f192ef31db..43094e7eccf 100644
--- a/drivers/media/IR/ir-raw-event.c
+++ b/drivers/media/IR/ir-raw-event.c
@@ -12,9 +12,10 @@
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 */ 13 */
14 14
15#include <linux/workqueue.h> 15#include <linux/kthread.h>
16#include <linux/spinlock.h> 16#include <linux/mutex.h>
17#include <linux/sched.h> 17#include <linux/sched.h>
18#include <linux/freezer.h>
18#include "ir-core-priv.h" 19#include "ir-core-priv.h"
19 20
20/* Define the max number of pulse/space transitions to buffer */ 21/* Define the max number of pulse/space transitions to buffer */
@@ -24,7 +25,7 @@
24static LIST_HEAD(ir_raw_client_list); 25static LIST_HEAD(ir_raw_client_list);
25 26
26/* Used to handle IR raw handler extensions */ 27/* Used to handle IR raw handler extensions */
27static DEFINE_SPINLOCK(ir_raw_handler_lock); 28static DEFINE_MUTEX(ir_raw_handler_lock);
28static LIST_HEAD(ir_raw_handler_list); 29static LIST_HEAD(ir_raw_handler_list);
29static u64 available_protocols; 30static u64 available_protocols;
30 31
@@ -33,20 +34,30 @@ static u64 available_protocols;
33static struct work_struct wq_load; 34static struct work_struct wq_load;
34#endif 35#endif
35 36
36static void ir_raw_event_work(struct work_struct *work) 37static int ir_raw_event_thread(void *data)
37{ 38{
38 struct ir_raw_event ev; 39 struct ir_raw_event ev;
39 struct ir_raw_handler *handler; 40 struct ir_raw_handler *handler;
40 struct ir_raw_event_ctrl *raw = 41 struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data;
41 container_of(work, struct ir_raw_event_ctrl, rx_work); 42
42 43 while (!kthread_should_stop()) {
43 while (kfifo_out(&raw->kfifo, &ev, sizeof(ev)) == sizeof(ev)) { 44 try_to_freeze();
44 spin_lock(&ir_raw_handler_lock); 45
45 list_for_each_entry(handler, &ir_raw_handler_list, list) 46 mutex_lock(&ir_raw_handler_lock);
46 handler->decode(raw->input_dev, ev); 47
47 spin_unlock(&ir_raw_handler_lock); 48 while (kfifo_out(&raw->kfifo, &ev, sizeof(ev)) == sizeof(ev)) {
48 raw->prev_ev = ev; 49 list_for_each_entry(handler, &ir_raw_handler_list, list)
50 handler->decode(raw->input_dev, ev);
51 raw->prev_ev = ev;
52 }
53
54 mutex_unlock(&ir_raw_handler_lock);
55
56 set_current_state(TASK_INTERRUPTIBLE);
57 schedule();
49 } 58 }
59
60 return 0;
50} 61}
51 62
52/** 63/**
@@ -66,6 +77,9 @@ int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
66 if (!ir->raw) 77 if (!ir->raw)
67 return -EINVAL; 78 return -EINVAL;
68 79
80 IR_dprintk(2, "sample: (05%dus %s)\n",
81 TO_US(ev->duration), TO_STR(ev->pulse));
82
69 if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev)) 83 if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
70 return -ENOMEM; 84 return -ENOMEM;
71 85
@@ -126,6 +140,90 @@ int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type typ
126EXPORT_SYMBOL_GPL(ir_raw_event_store_edge); 140EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
127 141
128/** 142/**
143 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
144 * @input_dev: the struct input_dev device descriptor
145 * @type: the type of the event that has occurred
146 *
147 * This routine (which may be called from an interrupt context) works
148 * in similiar manner to ir_raw_event_store_edge.
149 * This routine is intended for devices with limited internal buffer
150 * It automerges samples of same type, and handles timeouts
151 */
152int ir_raw_event_store_with_filter(struct input_dev *input_dev,
153 struct ir_raw_event *ev)
154{
155 struct ir_input_dev *ir = input_get_drvdata(input_dev);
156 struct ir_raw_event_ctrl *raw = ir->raw;
157
158 if (!raw || !ir->props)
159 return -EINVAL;
160
161 /* Ignore spaces in idle mode */
162 if (ir->idle && !ev->pulse)
163 return 0;
164 else if (ir->idle)
165 ir_raw_event_set_idle(input_dev, 0);
166
167 if (!raw->this_ev.duration) {
168 raw->this_ev = *ev;
169 } else if (ev->pulse == raw->this_ev.pulse) {
170 raw->this_ev.duration += ev->duration;
171 } else {
172 ir_raw_event_store(input_dev, &raw->this_ev);
173 raw->this_ev = *ev;
174 }
175
176 /* Enter idle mode if nessesary */
177 if (!ev->pulse && ir->props->timeout &&
178 raw->this_ev.duration >= ir->props->timeout)
179 ir_raw_event_set_idle(input_dev, 1);
180 return 0;
181}
182EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
183
184void ir_raw_event_set_idle(struct input_dev *input_dev, int idle)
185{
186 struct ir_input_dev *ir = input_get_drvdata(input_dev);
187 struct ir_raw_event_ctrl *raw = ir->raw;
188 ktime_t now;
189 u64 delta;
190
191 if (!ir->props)
192 return;
193
194 if (!ir->raw)
195 goto out;
196
197 if (idle) {
198 IR_dprintk(2, "enter idle mode\n");
199 raw->last_event = ktime_get();
200 } else {
201 IR_dprintk(2, "exit idle mode\n");
202
203 now = ktime_get();
204 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
205
206 WARN_ON(raw->this_ev.pulse);
207
208 raw->this_ev.duration =
209 min(raw->this_ev.duration + delta,
210 (u64)IR_MAX_DURATION);
211
212 ir_raw_event_store(input_dev, &raw->this_ev);
213
214 if (raw->this_ev.duration == IR_MAX_DURATION)
215 ir_raw_event_reset(input_dev);
216
217 raw->this_ev.duration = 0;
218 }
219out:
220 if (ir->props->s_idle)
221 ir->props->s_idle(ir->props->priv, idle);
222 ir->idle = idle;
223}
224EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
225
226/**
129 * ir_raw_event_handle() - schedules the decoding of stored ir data 227 * ir_raw_event_handle() - schedules the decoding of stored ir data
130 * @input_dev: the struct input_dev device descriptor 228 * @input_dev: the struct input_dev device descriptor
131 * 229 *
@@ -138,7 +236,7 @@ void ir_raw_event_handle(struct input_dev *input_dev)
138 if (!ir->raw) 236 if (!ir->raw)
139 return; 237 return;
140 238
141 schedule_work(&ir->raw->rx_work); 239 wake_up_process(ir->raw->thread);
142} 240}
143EXPORT_SYMBOL_GPL(ir_raw_event_handle); 241EXPORT_SYMBOL_GPL(ir_raw_event_handle);
144 242
@@ -147,9 +245,9 @@ u64
147ir_raw_get_allowed_protocols() 245ir_raw_get_allowed_protocols()
148{ 246{
149 u64 protocols; 247 u64 protocols;
150 spin_lock(&ir_raw_handler_lock); 248 mutex_lock(&ir_raw_handler_lock);
151 protocols = available_protocols; 249 protocols = available_protocols;
152 spin_unlock(&ir_raw_handler_lock); 250 mutex_unlock(&ir_raw_handler_lock);
153 return protocols; 251 return protocols;
154} 252}
155 253
@@ -167,7 +265,7 @@ int ir_raw_event_register(struct input_dev *input_dev)
167 return -ENOMEM; 265 return -ENOMEM;
168 266
169 ir->raw->input_dev = input_dev; 267 ir->raw->input_dev = input_dev;
170 INIT_WORK(&ir->raw->rx_work, ir_raw_event_work); 268
171 ir->raw->enabled_protocols = ~0; 269 ir->raw->enabled_protocols = ~0;
172 rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE, 270 rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
173 GFP_KERNEL); 271 GFP_KERNEL);
@@ -177,12 +275,21 @@ int ir_raw_event_register(struct input_dev *input_dev)
177 return rc; 275 return rc;
178 } 276 }
179 277
180 spin_lock(&ir_raw_handler_lock); 278 ir->raw->thread = kthread_run(ir_raw_event_thread, ir->raw,
279 "rc%u", (unsigned int)ir->devno);
280
281 if (IS_ERR(ir->raw->thread)) {
282 kfree(ir->raw);
283 ir->raw = NULL;
284 return PTR_ERR(ir->raw->thread);
285 }
286
287 mutex_lock(&ir_raw_handler_lock);
181 list_add_tail(&ir->raw->list, &ir_raw_client_list); 288 list_add_tail(&ir->raw->list, &ir_raw_client_list);
182 list_for_each_entry(handler, &ir_raw_handler_list, list) 289 list_for_each_entry(handler, &ir_raw_handler_list, list)
183 if (handler->raw_register) 290 if (handler->raw_register)
184 handler->raw_register(ir->raw->input_dev); 291 handler->raw_register(ir->raw->input_dev);
185 spin_unlock(&ir_raw_handler_lock); 292 mutex_unlock(&ir_raw_handler_lock);
186 293
187 return 0; 294 return 0;
188} 295}
@@ -195,14 +302,14 @@ void ir_raw_event_unregister(struct input_dev *input_dev)
195 if (!ir->raw) 302 if (!ir->raw)
196 return; 303 return;
197 304
198 cancel_work_sync(&ir->raw->rx_work); 305 kthread_stop(ir->raw->thread);
199 306
200 spin_lock(&ir_raw_handler_lock); 307 mutex_lock(&ir_raw_handler_lock);
201 list_del(&ir->raw->list); 308 list_del(&ir->raw->list);
202 list_for_each_entry(handler, &ir_raw_handler_list, list) 309 list_for_each_entry(handler, &ir_raw_handler_list, list)
203 if (handler->raw_unregister) 310 if (handler->raw_unregister)
204 handler->raw_unregister(ir->raw->input_dev); 311 handler->raw_unregister(ir->raw->input_dev);
205 spin_unlock(&ir_raw_handler_lock); 312 mutex_unlock(&ir_raw_handler_lock);
206 313
207 kfifo_free(&ir->raw->kfifo); 314 kfifo_free(&ir->raw->kfifo);
208 kfree(ir->raw); 315 kfree(ir->raw);
@@ -217,13 +324,13 @@ int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
217{ 324{
218 struct ir_raw_event_ctrl *raw; 325 struct ir_raw_event_ctrl *raw;
219 326
220 spin_lock(&ir_raw_handler_lock); 327 mutex_lock(&ir_raw_handler_lock);
221 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list); 328 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
222 if (ir_raw_handler->raw_register) 329 if (ir_raw_handler->raw_register)
223 list_for_each_entry(raw, &ir_raw_client_list, list) 330 list_for_each_entry(raw, &ir_raw_client_list, list)
224 ir_raw_handler->raw_register(raw->input_dev); 331 ir_raw_handler->raw_register(raw->input_dev);
225 available_protocols |= ir_raw_handler->protocols; 332 available_protocols |= ir_raw_handler->protocols;
226 spin_unlock(&ir_raw_handler_lock); 333 mutex_unlock(&ir_raw_handler_lock);
227 334
228 return 0; 335 return 0;
229} 336}
@@ -233,13 +340,13 @@ void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
233{ 340{
234 struct ir_raw_event_ctrl *raw; 341 struct ir_raw_event_ctrl *raw;
235 342
236 spin_lock(&ir_raw_handler_lock); 343 mutex_lock(&ir_raw_handler_lock);
237 list_del(&ir_raw_handler->list); 344 list_del(&ir_raw_handler->list);
238 if (ir_raw_handler->raw_unregister) 345 if (ir_raw_handler->raw_unregister)
239 list_for_each_entry(raw, &ir_raw_client_list, list) 346 list_for_each_entry(raw, &ir_raw_client_list, list)
240 ir_raw_handler->raw_unregister(raw->input_dev); 347 ir_raw_handler->raw_unregister(raw->input_dev);
241 available_protocols &= ~ir_raw_handler->protocols; 348 available_protocols &= ~ir_raw_handler->protocols;
242 spin_unlock(&ir_raw_handler_lock); 349 mutex_unlock(&ir_raw_handler_lock);
243} 350}
244EXPORT_SYMBOL(ir_raw_handler_unregister); 351EXPORT_SYMBOL(ir_raw_handler_unregister);
245 352
diff --git a/drivers/media/IR/ir-sysfs.c b/drivers/media/IR/ir-sysfs.c
index 6273047e915..96dafc425c8 100644
--- a/drivers/media/IR/ir-sysfs.c
+++ b/drivers/media/IR/ir-sysfs.c
@@ -325,6 +325,7 @@ static int __init ir_core_init(void)
325 325
326 /* Initialize/load the decoders/keymap code that will be used */ 326 /* Initialize/load the decoders/keymap code that will be used */
327 ir_raw_init(); 327 ir_raw_init();
328 ir_rcmap_init();
328 329
329 return 0; 330 return 0;
330} 331}
@@ -332,6 +333,7 @@ static int __init ir_core_init(void)
332static void __exit ir_core_exit(void) 333static void __exit ir_core_exit(void)
333{ 334{
334 class_unregister(&ir_input_class); 335 class_unregister(&ir_input_class);
336 ir_rcmap_cleanup();
335} 337}
336 338
337module_init(ir_core_init); 339module_init(ir_core_init);
diff --git a/drivers/media/IR/keymaps/Makefile b/drivers/media/IR/keymaps/Makefile
index cbee06243b5..950e5d953c6 100644
--- a/drivers/media/IR/keymaps/Makefile
+++ b/drivers/media/IR/keymaps/Makefile
@@ -19,7 +19,6 @@ obj-$(CONFIG_RC_MAP) += rc-adstech-dvb-t-pci.o \
19 rc-dm1105-nec.o \ 19 rc-dm1105-nec.o \
20 rc-dntv-live-dvb-t.o \ 20 rc-dntv-live-dvb-t.o \
21 rc-dntv-live-dvbt-pro.o \ 21 rc-dntv-live-dvbt-pro.o \
22 rc-empty.o \
23 rc-em-terratec.o \ 22 rc-em-terratec.o \
24 rc-encore-enltv2.o \ 23 rc-encore-enltv2.o \
25 rc-encore-enltv.o \ 24 rc-encore-enltv.o \
@@ -59,6 +58,7 @@ obj-$(CONFIG_RC_MAP) += rc-adstech-dvb-t-pci.o \
59 rc-purpletv.o \ 58 rc-purpletv.o \
60 rc-pv951.o \ 59 rc-pv951.o \
61 rc-rc5-hauppauge-new.o \ 60 rc-rc5-hauppauge-new.o \
61 rc-rc5-streamzap.o \
62 rc-rc5-tv.o \ 62 rc-rc5-tv.o \
63 rc-rc6-mce.o \ 63 rc-rc6-mce.o \
64 rc-real-audio-220-32-keys.o \ 64 rc-real-audio-220-32-keys.o \
diff --git a/drivers/media/IR/keymaps/rc-empty.c b/drivers/media/IR/keymaps/rc-empty.c
deleted file mode 100644
index 3b338d84b47..00000000000
--- a/drivers/media/IR/keymaps/rc-empty.c
+++ /dev/null
@@ -1,44 +0,0 @@
1/* empty.h - Keytable for empty Remote Controller
2 *
3 * keymap imported from ir-keymaps.c
4 *
5 * Copyright (c) 2010 by Mauro Carvalho Chehab <mchehab@redhat.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
13#include <media/rc-map.h>
14
15/* empty keytable, can be used as placeholder for not-yet created keytables */
16
17static struct ir_scancode empty[] = {
18 { 0x2a, KEY_COFFEE },
19};
20
21static struct rc_keymap empty_map = {
22 .map = {
23 .scan = empty,
24 .size = ARRAY_SIZE(empty),
25 .ir_type = IR_TYPE_UNKNOWN, /* Legacy IR type */
26 .name = RC_MAP_EMPTY,
27 }
28};
29
30static int __init init_rc_map_empty(void)
31{
32 return ir_register_map(&empty_map);
33}
34
35static void __exit exit_rc_map_empty(void)
36{
37 ir_unregister_map(&empty_map);
38}
39
40module_init(init_rc_map_empty)
41module_exit(exit_rc_map_empty)
42
43MODULE_LICENSE("GPL");
44MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
diff --git a/drivers/media/IR/keymaps/rc-rc5-streamzap.c b/drivers/media/IR/keymaps/rc-rc5-streamzap.c
new file mode 100644
index 00000000000..4c19c58b46d
--- /dev/null
+++ b/drivers/media/IR/keymaps/rc-rc5-streamzap.c
@@ -0,0 +1,81 @@
1/* rc-rc5-streamzap.c - Keytable for Streamzap PC Remote, for use
2 * with the Streamzap PC Remote IR Receiver.
3 *
4 * Copyright (c) 2010 by Jarod Wilson <jarod@redhat.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
12#include <media/rc-map.h>
13
14static struct ir_scancode rc5_streamzap[] = {
15/*
16 * FIXME: The Streamzap remote isn't actually true RC-5, it has an extra
17 * bit in it, which presently throws the in-kernel RC-5 decoder for a loop.
18 * We either have to enhance the decoder to support it, add a new decoder,
19 * or just rely on lirc userspace decoding.
20 */
21 { 0x00, KEY_NUMERIC_0 },
22 { 0x01, KEY_NUMERIC_1 },
23 { 0x02, KEY_NUMERIC_2 },
24 { 0x03, KEY_NUMERIC_3 },
25 { 0x04, KEY_NUMERIC_4 },
26 { 0x05, KEY_NUMERIC_5 },
27 { 0x06, KEY_NUMERIC_6 },
28 { 0x07, KEY_NUMERIC_7 },
29 { 0x08, KEY_NUMERIC_8 },
30 { 0x0a, KEY_POWER },
31 { 0x0b, KEY_MUTE },
32 { 0x0c, KEY_CHANNELUP },
33 { 0x0d, KEY_VOLUMEUP },
34 { 0x0e, KEY_CHANNELDOWN },
35 { 0x0f, KEY_VOLUMEDOWN },
36 { 0x10, KEY_UP },
37 { 0x11, KEY_LEFT },
38 { 0x12, KEY_OK },
39 { 0x13, KEY_RIGHT },
40 { 0x14, KEY_DOWN },
41 { 0x15, KEY_MENU },
42 { 0x16, KEY_EXIT },
43 { 0x17, KEY_PLAY },
44 { 0x18, KEY_PAUSE },
45 { 0x19, KEY_STOP },
46 { 0x1a, KEY_BACK },
47 { 0x1b, KEY_FORWARD },
48 { 0x1c, KEY_RECORD },
49 { 0x1d, KEY_REWIND },
50 { 0x1e, KEY_FASTFORWARD },
51 { 0x20, KEY_RED },
52 { 0x21, KEY_GREEN },
53 { 0x22, KEY_YELLOW },
54 { 0x23, KEY_BLUE },
55
56};
57
58static struct rc_keymap rc5_streamzap_map = {
59 .map = {
60 .scan = rc5_streamzap,
61 .size = ARRAY_SIZE(rc5_streamzap),
62 .ir_type = IR_TYPE_RC5,
63 .name = RC_MAP_RC5_STREAMZAP,
64 }
65};
66
67static int __init init_rc_map_rc5_streamzap(void)
68{
69 return ir_register_map(&rc5_streamzap_map);
70}
71
72static void __exit exit_rc_map_rc5_streamzap(void)
73{
74 ir_unregister_map(&rc5_streamzap_map);
75}
76
77module_init(init_rc_map_rc5_streamzap)
78module_exit(exit_rc_map_rc5_streamzap)
79
80MODULE_LICENSE("GPL");
81MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
diff --git a/drivers/media/IR/keymaps/rc-rc6-mce.c b/drivers/media/IR/keymaps/rc-rc6-mce.c
index c6726a8039b..64264f7f838 100644
--- a/drivers/media/IR/keymaps/rc-rc6-mce.c
+++ b/drivers/media/IR/keymaps/rc-rc6-mce.c
@@ -74,6 +74,8 @@ static struct ir_scancode rc6_mce[] = {
74 { 0x800f045a, KEY_SUBTITLE }, /* Caption/Teletext */ 74 { 0x800f045a, KEY_SUBTITLE }, /* Caption/Teletext */
75 { 0x800f044d, KEY_TITLE }, 75 { 0x800f044d, KEY_TITLE },
76 76
77 { 0x800f044e, KEY_PRINT }, /* Print - HP OEM version of remote */
78
77 { 0x800f040c, KEY_POWER }, 79 { 0x800f040c, KEY_POWER },
78 { 0x800f040d, KEY_PROG1 }, /* Windows MCE button */ 80 { 0x800f040d, KEY_PROG1 }, /* Windows MCE button */
79 81
diff --git a/drivers/media/IR/mceusb.c b/drivers/media/IR/mceusb.c
index 78bf7f77a1a..ac6bb2c01a4 100644
--- a/drivers/media/IR/mceusb.c
+++ b/drivers/media/IR/mceusb.c
@@ -228,7 +228,6 @@ static struct usb_device_id std_tx_mask_list[] = {
228/* data structure for each usb transceiver */ 228/* data structure for each usb transceiver */
229struct mceusb_dev { 229struct mceusb_dev {
230 /* ir-core bits */ 230 /* ir-core bits */
231 struct ir_input_dev *irdev;
232 struct ir_dev_props *props; 231 struct ir_dev_props *props;
233 struct ir_raw_event rawir; 232 struct ir_raw_event rawir;
234 233
@@ -428,7 +427,7 @@ static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
428 } 427 }
429} 428}
430 429
431static void usb_async_callback(struct urb *urb, struct pt_regs *regs) 430static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
432{ 431{
433 struct mceusb_dev *ir; 432 struct mceusb_dev *ir;
434 int len; 433 int len;
@@ -477,7 +476,7 @@ static void mce_request_packet(struct mceusb_dev *ir,
477 /* outbound data */ 476 /* outbound data */
478 usb_fill_int_urb(async_urb, ir->usbdev, 477 usb_fill_int_urb(async_urb, ir->usbdev,
479 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress), 478 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
480 async_buf, size, (usb_complete_t) usb_async_callback, 479 async_buf, size, (usb_complete_t)mce_async_callback,
481 ir, ep->bInterval); 480 ir, ep->bInterval);
482 memcpy(async_buf, data, size); 481 memcpy(async_buf, data, size);
483 482
@@ -739,7 +738,7 @@ static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
739 738
740 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) { 739 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
741 ir->send_flags = SEND_FLAG_COMPLETE; 740 ir->send_flags = SEND_FLAG_COMPLETE;
742 dev_dbg(&ir->irdev->dev, "setup answer received %d bytes\n", 741 dev_dbg(ir->dev, "setup answer received %d bytes\n",
743 buf_len); 742 buf_len);
744 } 743 }
745 744
@@ -861,7 +860,6 @@ static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
861{ 860{
862 struct input_dev *idev; 861 struct input_dev *idev;
863 struct ir_dev_props *props; 862 struct ir_dev_props *props;
864 struct ir_input_dev *irdev;
865 struct device *dev = ir->dev; 863 struct device *dev = ir->dev;
866 int ret = -ENODEV; 864 int ret = -ENODEV;
867 865
@@ -878,12 +876,6 @@ static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
878 goto props_alloc_failed; 876 goto props_alloc_failed;
879 } 877 }
880 878
881 irdev = kzalloc(sizeof(struct ir_input_dev), GFP_KERNEL);
882 if (!irdev) {
883 dev_err(dev, "remote ir input dev allocation failed\n");
884 goto ir_dev_alloc_failed;
885 }
886
887 snprintf(ir->name, sizeof(ir->name), "Media Center Ed. eHome " 879 snprintf(ir->name, sizeof(ir->name), "Media Center Ed. eHome "
888 "Infrared Remote Transceiver (%04x:%04x)", 880 "Infrared Remote Transceiver (%04x:%04x)",
889 le16_to_cpu(ir->usbdev->descriptor.idVendor), 881 le16_to_cpu(ir->usbdev->descriptor.idVendor),
@@ -902,9 +894,6 @@ static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
902 props->tx_ir = mceusb_tx_ir; 894 props->tx_ir = mceusb_tx_ir;
903 895
904 ir->props = props; 896 ir->props = props;
905 ir->irdev = irdev;
906
907 input_set_drvdata(idev, irdev);
908 897
909 ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME); 898 ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME);
910 if (ret < 0) { 899 if (ret < 0) {
@@ -915,8 +904,6 @@ static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
915 return idev; 904 return idev;
916 905
917irdev_failed: 906irdev_failed:
918 kfree(irdev);
919ir_dev_alloc_failed:
920 kfree(props); 907 kfree(props);
921props_alloc_failed: 908props_alloc_failed:
922 input_free_device(idev); 909 input_free_device(idev);
@@ -932,7 +919,6 @@ static int __devinit mceusb_dev_probe(struct usb_interface *intf,
932 struct usb_endpoint_descriptor *ep = NULL; 919 struct usb_endpoint_descriptor *ep = NULL;
933 struct usb_endpoint_descriptor *ep_in = NULL; 920 struct usb_endpoint_descriptor *ep_in = NULL;
934 struct usb_endpoint_descriptor *ep_out = NULL; 921 struct usb_endpoint_descriptor *ep_out = NULL;
935 struct usb_host_config *config;
936 struct mceusb_dev *ir = NULL; 922 struct mceusb_dev *ir = NULL;
937 int pipe, maxp, i; 923 int pipe, maxp, i;
938 char buf[63], name[128] = ""; 924 char buf[63], name[128] = "";
@@ -942,7 +928,6 @@ static int __devinit mceusb_dev_probe(struct usb_interface *intf,
942 928
943 dev_dbg(&intf->dev, ": %s called\n", __func__); 929 dev_dbg(&intf->dev, ": %s called\n", __func__);
944 930
945 config = dev->actconfig;
946 idesc = intf->cur_altsetting; 931 idesc = intf->cur_altsetting;
947 932
948 is_gen3 = usb_match_id(intf, gen3_list) ? 1 : 0; 933 is_gen3 = usb_match_id(intf, gen3_list) ? 1 : 0;
diff --git a/drivers/media/IR/rc-map.c b/drivers/media/IR/rc-map.c
index 46a8f1524b5..689143f2fff 100644
--- a/drivers/media/IR/rc-map.c
+++ b/drivers/media/IR/rc-map.c
@@ -82,3 +82,26 @@ void ir_unregister_map(struct rc_keymap *map)
82} 82}
83EXPORT_SYMBOL_GPL(ir_unregister_map); 83EXPORT_SYMBOL_GPL(ir_unregister_map);
84 84
85
86static struct ir_scancode empty[] = {
87 { 0x2a, KEY_COFFEE },
88};
89
90static struct rc_keymap empty_map = {
91 .map = {
92 .scan = empty,
93 .size = ARRAY_SIZE(empty),
94 .ir_type = IR_TYPE_UNKNOWN, /* Legacy IR type */
95 .name = RC_MAP_EMPTY,
96 }
97};
98
99int ir_rcmap_init(void)
100{
101 return ir_register_map(&empty_map);
102}
103
104void ir_rcmap_cleanup(void)
105{
106 ir_unregister_map(&empty_map);
107}
diff --git a/drivers/media/IR/streamzap.c b/drivers/media/IR/streamzap.c
new file mode 100644
index 00000000000..058e29fd478
--- /dev/null
+++ b/drivers/media/IR/streamzap.c
@@ -0,0 +1,741 @@
1/*
2 * Streamzap Remote Control driver
3 *
4 * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
5 * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
6 *
7 * This driver was based on the work of Greg Wickham and Adrian
8 * Dewhurst. It was substantially rewritten to support correct signal
9 * gaps and now maintains a delay buffer, which is used to present
10 * consistent timing behaviour to user space applications. Without the
11 * delay buffer an ugly hack would be required in lircd, which can
12 * cause sluggish signal decoding in certain situations.
13 *
14 * Ported to in-kernel ir-core interface by Jarod Wilson
15 *
16 * This driver is based on the USB skeleton driver packaged with the
17 * kernel; copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 */
33
34#include <linux/device.h>
35#include <linux/module.h>
36#include <linux/slab.h>
37#include <linux/usb.h>
38#include <linux/input.h>
39#include <media/ir-core.h>
40
41#define DRIVER_VERSION "1.60"
42#define DRIVER_NAME "streamzap"
43#define DRIVER_DESC "Streamzap Remote Control driver"
44
45#ifdef CONFIG_USB_DEBUG
46static int debug = 1;
47#else
48static int debug;
49#endif
50
51#define USB_STREAMZAP_VENDOR_ID 0x0e9c
52#define USB_STREAMZAP_PRODUCT_ID 0x0000
53
54/* table of devices that work with this driver */
55static struct usb_device_id streamzap_table[] = {
56 /* Streamzap Remote Control */
57 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
58 /* Terminating entry */
59 { }
60};
61
62MODULE_DEVICE_TABLE(usb, streamzap_table);
63
64#define STREAMZAP_PULSE_MASK 0xf0
65#define STREAMZAP_SPACE_MASK 0x0f
66#define STREAMZAP_TIMEOUT 0xff
67#define STREAMZAP_RESOLUTION 256
68
69/* number of samples buffered */
70#define SZ_BUF_LEN 128
71
72enum StreamzapDecoderState {
73 PulseSpace,
74 FullPulse,
75 FullSpace,
76 IgnorePulse
77};
78
79/* structure to hold our device specific stuff */
80struct streamzap_ir {
81
82 /* ir-core */
83 struct ir_dev_props *props;
84 struct ir_raw_event rawir;
85
86 /* core device info */
87 struct device *dev;
88 struct input_dev *idev;
89
90 /* usb */
91 struct usb_device *usbdev;
92 struct usb_interface *interface;
93 struct usb_endpoint_descriptor *endpoint;
94 struct urb *urb_in;
95
96 /* buffer & dma */
97 unsigned char *buf_in;
98 dma_addr_t dma_in;
99 unsigned int buf_in_len;
100
101 /* timer used to support delay buffering */
102 struct timer_list delay_timer;
103 bool timer_running;
104 spinlock_t timer_lock;
105 struct timer_list flush_timer;
106 bool flush;
107
108 /* delay buffer */
109 struct kfifo fifo;
110 bool fifo_initialized;
111
112 /* track what state we're in */
113 enum StreamzapDecoderState decoder_state;
114 /* tracks whether we are currently receiving some signal */
115 bool idle;
116 /* sum of signal lengths received since signal start */
117 unsigned long sum;
118 /* start time of signal; necessary for gap tracking */
119 struct timeval signal_last;
120 struct timeval signal_start;
121 /* bool timeout_enabled; */
122
123 char name[128];
124 char phys[64];
125};
126
127
128/* local function prototypes */
129static int streamzap_probe(struct usb_interface *interface,
130 const struct usb_device_id *id);
131static void streamzap_disconnect(struct usb_interface *interface);
132static void streamzap_callback(struct urb *urb);
133static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
134static int streamzap_resume(struct usb_interface *intf);
135
136/* usb specific object needed to register this driver with the usb subsystem */
137static struct usb_driver streamzap_driver = {
138 .name = DRIVER_NAME,
139 .probe = streamzap_probe,
140 .disconnect = streamzap_disconnect,
141 .suspend = streamzap_suspend,
142 .resume = streamzap_resume,
143 .id_table = streamzap_table,
144};
145
146static void streamzap_stop_timer(struct streamzap_ir *sz)
147{
148 unsigned long flags;
149
150 spin_lock_irqsave(&sz->timer_lock, flags);
151 if (sz->timer_running) {
152 sz->timer_running = false;
153 spin_unlock_irqrestore(&sz->timer_lock, flags);
154 del_timer_sync(&sz->delay_timer);
155 } else {
156 spin_unlock_irqrestore(&sz->timer_lock, flags);
157 }
158}
159
160static void streamzap_flush_timeout(unsigned long arg)
161{
162 struct streamzap_ir *sz = (struct streamzap_ir *)arg;
163
164 dev_info(sz->dev, "%s: callback firing\n", __func__);
165
166 /* finally start accepting data */
167 sz->flush = false;
168}
169
170static void streamzap_delay_timeout(unsigned long arg)
171{
172 struct streamzap_ir *sz = (struct streamzap_ir *)arg;
173 struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
174 unsigned long flags;
175 int len, ret;
176 static unsigned long delay;
177 bool wake = false;
178
179 /* deliver data every 10 ms */
180 delay = msecs_to_jiffies(10);
181
182 spin_lock_irqsave(&sz->timer_lock, flags);
183
184 if (kfifo_len(&sz->fifo) > 0) {
185 ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
186 if (ret != sizeof(rawir))
187 dev_err(sz->dev, "Problem w/kfifo_out...\n");
188 ir_raw_event_store(sz->idev, &rawir);
189 wake = true;
190 }
191
192 len = kfifo_len(&sz->fifo);
193 if (len > 0) {
194 while ((len < SZ_BUF_LEN / 2) &&
195 (len < SZ_BUF_LEN * sizeof(int))) {
196 ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
197 if (ret != sizeof(rawir))
198 dev_err(sz->dev, "Problem w/kfifo_out...\n");
199 ir_raw_event_store(sz->idev, &rawir);
200 wake = true;
201 len = kfifo_len(&sz->fifo);
202 }
203 if (sz->timer_running)
204 mod_timer(&sz->delay_timer, jiffies + delay);
205
206 } else {
207 sz->timer_running = false;
208 }
209
210 if (wake)
211 ir_raw_event_handle(sz->idev);
212
213 spin_unlock_irqrestore(&sz->timer_lock, flags);
214}
215
216static void streamzap_flush_delay_buffer(struct streamzap_ir *sz)
217{
218 struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
219 bool wake = false;
220 int ret;
221
222 while (kfifo_len(&sz->fifo) > 0) {
223 ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
224 if (ret != sizeof(rawir))
225 dev_err(sz->dev, "Problem w/kfifo_out...\n");
226 ir_raw_event_store(sz->idev, &rawir);
227 wake = true;
228 }
229
230 if (wake)
231 ir_raw_event_handle(sz->idev);
232}
233
234static void sz_push(struct streamzap_ir *sz)
235{
236 struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
237 unsigned long flags;
238 int ret;
239
240 spin_lock_irqsave(&sz->timer_lock, flags);
241 if (kfifo_len(&sz->fifo) >= sizeof(int) * SZ_BUF_LEN) {
242 ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
243 if (ret != sizeof(rawir))
244 dev_err(sz->dev, "Problem w/kfifo_out...\n");
245 ir_raw_event_store(sz->idev, &rawir);
246 }
247
248 kfifo_in(&sz->fifo, &sz->rawir, sizeof(rawir));
249
250 if (!sz->timer_running) {
251 sz->delay_timer.expires = jiffies + (HZ / 10);
252 add_timer(&sz->delay_timer);
253 sz->timer_running = true;
254 }
255
256 spin_unlock_irqrestore(&sz->timer_lock, flags);
257}
258
259static void sz_push_full_pulse(struct streamzap_ir *sz,
260 unsigned char value)
261{
262 if (sz->idle) {
263 long deltv;
264
265 sz->signal_last = sz->signal_start;
266 do_gettimeofday(&sz->signal_start);
267
268 deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec;
269 sz->rawir.pulse = false;
270 if (deltv > 15) {
271 /* really long time */
272 sz->rawir.duration = IR_MAX_DURATION;
273 } else {
274 sz->rawir.duration = (int)(deltv * 1000000 +
275 sz->signal_start.tv_usec -
276 sz->signal_last.tv_usec);
277 sz->rawir.duration -= sz->sum;
278 sz->rawir.duration *= 1000;
279 sz->rawir.duration &= IR_MAX_DURATION;
280 }
281 dev_dbg(sz->dev, "ls %u\n", sz->rawir.duration);
282 sz_push(sz);
283
284 sz->idle = 0;
285 sz->sum = 0;
286 }
287
288 sz->rawir.pulse = true;
289 sz->rawir.duration = ((int) value) * STREAMZAP_RESOLUTION;
290 sz->rawir.duration += STREAMZAP_RESOLUTION / 2;
291 sz->sum += sz->rawir.duration;
292 sz->rawir.duration *= 1000;
293 sz->rawir.duration &= IR_MAX_DURATION;
294 dev_dbg(sz->dev, "p %u\n", sz->rawir.duration);
295 sz_push(sz);
296}
297
298static void sz_push_half_pulse(struct streamzap_ir *sz,
299 unsigned char value)
300{
301 sz_push_full_pulse(sz, (value & STREAMZAP_PULSE_MASK) >> 4);
302}
303
304static void sz_push_full_space(struct streamzap_ir *sz,
305 unsigned char value)
306{
307 sz->rawir.pulse = false;
308 sz->rawir.duration = ((int) value) * STREAMZAP_RESOLUTION;
309 sz->rawir.duration += STREAMZAP_RESOLUTION / 2;
310 sz->sum += sz->rawir.duration;
311 sz->rawir.duration *= 1000;
312 dev_dbg(sz->dev, "s %u\n", sz->rawir.duration);
313 sz_push(sz);
314}
315
316static void sz_push_half_space(struct streamzap_ir *sz,
317 unsigned long value)
318{
319 sz_push_full_space(sz, value & STREAMZAP_SPACE_MASK);
320}
321
322/**
323 * streamzap_callback - usb IRQ handler callback
324 *
325 * This procedure is invoked on reception of data from
326 * the usb remote.
327 */
328static void streamzap_callback(struct urb *urb)
329{
330 struct streamzap_ir *sz;
331 unsigned int i;
332 int len;
333 #if 0
334 static int timeout = (((STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION) &
335 IR_MAX_DURATION) | 0x03000000);
336 #endif
337
338 if (!urb)
339 return;
340
341 sz = urb->context;
342 len = urb->actual_length;
343
344 switch (urb->status) {
345 case -ECONNRESET:
346 case -ENOENT:
347 case -ESHUTDOWN:
348 /*
349 * this urb is terminated, clean up.
350 * sz might already be invalid at this point
351 */
352 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
353 return;
354 default:
355 break;
356 }
357
358 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
359 if (!sz->flush) {
360 for (i = 0; i < urb->actual_length; i++) {
361 dev_dbg(sz->dev, "%d: %x\n", i,
362 (unsigned char)sz->buf_in[i]);
363 switch (sz->decoder_state) {
364 case PulseSpace:
365 if ((sz->buf_in[i] & STREAMZAP_PULSE_MASK) ==
366 STREAMZAP_PULSE_MASK) {
367 sz->decoder_state = FullPulse;
368 continue;
369 } else if ((sz->buf_in[i] & STREAMZAP_SPACE_MASK)
370 == STREAMZAP_SPACE_MASK) {
371 sz_push_half_pulse(sz, sz->buf_in[i]);
372 sz->decoder_state = FullSpace;
373 continue;
374 } else {
375 sz_push_half_pulse(sz, sz->buf_in[i]);
376 sz_push_half_space(sz, sz->buf_in[i]);
377 }
378 break;
379 case FullPulse:
380 sz_push_full_pulse(sz, sz->buf_in[i]);
381 sz->decoder_state = IgnorePulse;
382 break;
383 case FullSpace:
384 if (sz->buf_in[i] == STREAMZAP_TIMEOUT) {
385 sz->idle = 1;
386 streamzap_stop_timer(sz);
387 #if 0
388 if (sz->timeout_enabled) {
389 sz->rawir.pulse = false;
390 sz->rawir.duration = timeout;
391 sz->rawir.duration *= 1000;
392 sz_push(sz);
393 }
394 #endif
395 streamzap_flush_delay_buffer(sz);
396 } else
397 sz_push_full_space(sz, sz->buf_in[i]);
398 sz->decoder_state = PulseSpace;
399 break;
400 case IgnorePulse:
401 if ((sz->buf_in[i]&STREAMZAP_SPACE_MASK) ==
402 STREAMZAP_SPACE_MASK) {
403 sz->decoder_state = FullSpace;
404 continue;
405 }
406 sz_push_half_space(sz, sz->buf_in[i]);
407 sz->decoder_state = PulseSpace;
408 break;
409 }
410 }
411 }
412
413 usb_submit_urb(urb, GFP_ATOMIC);
414
415 return;
416}
417
418static struct input_dev *streamzap_init_input_dev(struct streamzap_ir *sz)
419{
420 struct input_dev *idev;
421 struct ir_dev_props *props;
422 struct device *dev = sz->dev;
423 int ret;
424
425 idev = input_allocate_device();
426 if (!idev) {
427 dev_err(dev, "remote input dev allocation failed\n");
428 goto idev_alloc_failed;
429 }
430
431 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
432 if (!props) {
433 dev_err(dev, "remote ir dev props allocation failed\n");
434 goto props_alloc_failed;
435 }
436
437 snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared "
438 "Receiver (%04x:%04x)",
439 le16_to_cpu(sz->usbdev->descriptor.idVendor),
440 le16_to_cpu(sz->usbdev->descriptor.idProduct));
441
442 idev->name = sz->name;
443 usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
444 strlcat(sz->phys, "/input0", sizeof(sz->phys));
445 idev->phys = sz->phys;
446
447 props->priv = sz;
448 props->driver_type = RC_DRIVER_IR_RAW;
449 /* FIXME: not sure about supported protocols, check on this */
450 props->allowed_protos = IR_TYPE_RC5 | IR_TYPE_RC6;
451
452 sz->props = props;
453
454 ret = ir_input_register(idev, RC_MAP_RC5_STREAMZAP, props, DRIVER_NAME);
455 if (ret < 0) {
456 dev_err(dev, "remote input device register failed\n");
457 goto irdev_failed;
458 }
459
460 return idev;
461
462irdev_failed:
463 kfree(props);
464props_alloc_failed:
465 input_free_device(idev);
466idev_alloc_failed:
467 return NULL;
468}
469
470static int streamzap_delay_buf_init(struct streamzap_ir *sz)
471{
472 int ret;
473
474 ret = kfifo_alloc(&sz->fifo, sizeof(int) * SZ_BUF_LEN,
475 GFP_KERNEL);
476 if (ret == 0)
477 sz->fifo_initialized = 1;
478
479 return ret;
480}
481
482static void streamzap_start_flush_timer(struct streamzap_ir *sz)
483{
484 sz->flush_timer.expires = jiffies + HZ;
485 sz->flush = true;
486 add_timer(&sz->flush_timer);
487
488 sz->urb_in->dev = sz->usbdev;
489 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
490 dev_err(sz->dev, "urb submit failed\n");
491}
492
493/**
494 * streamzap_probe
495 *
496 * Called by usb-core to associated with a candidate device
497 * On any failure the return value is the ERROR
498 * On success return 0
499 */
500static int __devinit streamzap_probe(struct usb_interface *intf,
501 const struct usb_device_id *id)
502{
503 struct usb_device *usbdev = interface_to_usbdev(intf);
504 struct usb_host_interface *iface_host;
505 struct streamzap_ir *sz = NULL;
506 char buf[63], name[128] = "";
507 int retval = -ENOMEM;
508 int pipe, maxp;
509
510 /* Allocate space for device driver specific data */
511 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
512 if (!sz)
513 return -ENOMEM;
514
515 sz->usbdev = usbdev;
516 sz->interface = intf;
517
518 /* Check to ensure endpoint information matches requirements */
519 iface_host = intf->cur_altsetting;
520
521 if (iface_host->desc.bNumEndpoints != 1) {
522 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
523 __func__, iface_host->desc.bNumEndpoints);
524 retval = -ENODEV;
525 goto free_sz;
526 }
527
528 sz->endpoint = &(iface_host->endpoint[0].desc);
529 if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
530 != USB_DIR_IN) {
531 dev_err(&intf->dev, "%s: endpoint doesn't match input device "
532 "02%02x\n", __func__, sz->endpoint->bEndpointAddress);
533 retval = -ENODEV;
534 goto free_sz;
535 }
536
537 if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
538 != USB_ENDPOINT_XFER_INT) {
539 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer "
540 "02%02x\n", __func__, sz->endpoint->bmAttributes);
541 retval = -ENODEV;
542 goto free_sz;
543 }
544
545 pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
546 maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
547
548 if (maxp == 0) {
549 dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
550 __func__);
551 retval = -ENODEV;
552 goto free_sz;
553 }
554
555 /* Allocate the USB buffer and IRQ URB */
556 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
557 if (!sz->buf_in)
558 goto free_sz;
559
560 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
561 if (!sz->urb_in)
562 goto free_buf_in;
563
564 sz->dev = &intf->dev;
565 sz->buf_in_len = maxp;
566
567 if (usbdev->descriptor.iManufacturer
568 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
569 buf, sizeof(buf)) > 0)
570 strlcpy(name, buf, sizeof(name));
571
572 if (usbdev->descriptor.iProduct
573 && usb_string(usbdev, usbdev->descriptor.iProduct,
574 buf, sizeof(buf)) > 0)
575 snprintf(name + strlen(name), sizeof(name) - strlen(name),
576 " %s", buf);
577
578 retval = streamzap_delay_buf_init(sz);
579 if (retval) {
580 dev_err(&intf->dev, "%s: delay buffer init failed\n", __func__);
581 goto free_urb_in;
582 }
583
584 sz->idev = streamzap_init_input_dev(sz);
585 if (!sz->idev)
586 goto input_dev_fail;
587
588 sz->idle = true;
589 sz->decoder_state = PulseSpace;
590 #if 0
591 /* not yet supported, depends on patches from maxim */
592 /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
593 sz->timeout_enabled = false;
594 sz->min_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION * 1000;
595 sz->max_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION * 1000;
596 #endif
597
598 init_timer(&sz->delay_timer);
599 sz->delay_timer.function = streamzap_delay_timeout;
600 sz->delay_timer.data = (unsigned long)sz;
601 spin_lock_init(&sz->timer_lock);
602
603 init_timer(&sz->flush_timer);
604 sz->flush_timer.function = streamzap_flush_timeout;
605 sz->flush_timer.data = (unsigned long)sz;
606
607 do_gettimeofday(&sz->signal_start);
608
609 /* Complete final initialisations */
610 usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
611 maxp, (usb_complete_t)streamzap_callback,
612 sz, sz->endpoint->bInterval);
613 sz->urb_in->transfer_dma = sz->dma_in;
614 sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
615
616 usb_set_intfdata(intf, sz);
617
618 streamzap_start_flush_timer(sz);
619
620 dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
621 usbdev->bus->busnum, usbdev->devnum);
622
623 return 0;
624
625input_dev_fail:
626 kfifo_free(&sz->fifo);
627free_urb_in:
628 usb_free_urb(sz->urb_in);
629free_buf_in:
630 usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
631free_sz:
632 kfree(sz);
633
634 return retval;
635}
636
637/**
638 * streamzap_disconnect
639 *
640 * Called by the usb core when the device is removed from the system.
641 *
642 * This routine guarantees that the driver will not submit any more urbs
643 * by clearing dev->usbdev. It is also supposed to terminate any currently
644 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
645 * does not provide any way to do this.
646 */
647static void streamzap_disconnect(struct usb_interface *interface)
648{
649 struct streamzap_ir *sz = usb_get_intfdata(interface);
650 struct usb_device *usbdev = interface_to_usbdev(interface);
651
652 usb_set_intfdata(interface, NULL);
653
654 if (!sz)
655 return;
656
657 if (sz->flush) {
658 sz->flush = false;
659 del_timer_sync(&sz->flush_timer);
660 }
661
662 streamzap_stop_timer(sz);
663
664 sz->usbdev = NULL;
665 ir_input_unregister(sz->idev);
666 usb_kill_urb(sz->urb_in);
667 usb_free_urb(sz->urb_in);
668 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
669
670 kfree(sz);
671}
672
673static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
674{
675 struct streamzap_ir *sz = usb_get_intfdata(intf);
676
677 if (sz->flush) {
678 sz->flush = false;
679 del_timer_sync(&sz->flush_timer);
680 }
681
682 streamzap_stop_timer(sz);
683
684 usb_kill_urb(sz->urb_in);
685
686 return 0;
687}
688
689static int streamzap_resume(struct usb_interface *intf)
690{
691 struct streamzap_ir *sz = usb_get_intfdata(intf);
692
693 if (sz->fifo_initialized)
694 kfifo_reset(&sz->fifo);
695
696 sz->flush_timer.expires = jiffies + HZ;
697 sz->flush = true;
698 add_timer(&sz->flush_timer);
699
700 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
701 dev_err(sz->dev, "Error sumbiting urb\n");
702 return -EIO;
703 }
704
705 return 0;
706}
707
708/**
709 * streamzap_init
710 */
711static int __init streamzap_init(void)
712{
713 int ret;
714
715 /* register this driver with the USB subsystem */
716 ret = usb_register(&streamzap_driver);
717 if (ret < 0)
718 printk(KERN_ERR DRIVER_NAME ": usb register failed, "
719 "result = %d\n", ret);
720
721 return ret;
722}
723
724/**
725 * streamzap_exit
726 */
727static void __exit streamzap_exit(void)
728{
729 usb_deregister(&streamzap_driver);
730}
731
732
733module_init(streamzap_init);
734module_exit(streamzap_exit);
735
736MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
737MODULE_DESCRIPTION(DRIVER_DESC);
738MODULE_LICENSE("GPL");
739
740module_param(debug, bool, S_IRUGO | S_IWUSR);
741MODULE_PARM_DESC(debug, "Enable debugging messages");
diff --git a/drivers/media/common/tuners/Kconfig b/drivers/media/common/tuners/Kconfig
index 409a4261e5b..b3ed5daaacf 100644
--- a/drivers/media/common/tuners/Kconfig
+++ b/drivers/media/common/tuners/Kconfig
@@ -34,7 +34,7 @@ config MEDIA_TUNER
34menuconfig MEDIA_TUNER_CUSTOMISE 34menuconfig MEDIA_TUNER_CUSTOMISE
35 bool "Customize analog and hybrid tuner modules to build" 35 bool "Customize analog and hybrid tuner modules to build"
36 depends on MEDIA_TUNER 36 depends on MEDIA_TUNER
37 default n 37 default y if EMBEDDED
38 help 38 help
39 This allows the user to deselect tuner drivers unnecessary 39 This allows the user to deselect tuner drivers unnecessary
40 for their hardware from the build. Use this option with care 40 for their hardware from the build. Use this option with care
diff --git a/drivers/media/dvb/bt8xx/dst.c b/drivers/media/dvb/bt8xx/dst.c
index 248a2a9d841..caa4e18ed1c 100644
--- a/drivers/media/dvb/bt8xx/dst.c
+++ b/drivers/media/dvb/bt8xx/dst.c
@@ -1763,7 +1763,15 @@ static struct dvb_frontend_ops dst_dvbt_ops = {
1763 .frequency_min = 137000000, 1763 .frequency_min = 137000000,
1764 .frequency_max = 858000000, 1764 .frequency_max = 858000000,
1765 .frequency_stepsize = 166667, 1765 .frequency_stepsize = 166667,
1766 .caps = FE_CAN_FEC_AUTO | FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO 1766 .caps = FE_CAN_FEC_AUTO |
1767 FE_CAN_QAM_AUTO |
1768 FE_CAN_QAM_16 |
1769 FE_CAN_QAM_32 |
1770 FE_CAN_QAM_64 |
1771 FE_CAN_QAM_128 |
1772 FE_CAN_QAM_256 |
1773 FE_CAN_TRANSMISSION_MODE_AUTO |
1774 FE_CAN_GUARD_INTERVAL_AUTO
1767 }, 1775 },
1768 1776
1769 .release = dst_release, 1777 .release = dst_release,
diff --git a/drivers/media/dvb/frontends/Kconfig b/drivers/media/dvb/frontends/Kconfig
index 51d578a758a..b5f6a04f9c1 100644
--- a/drivers/media/dvb/frontends/Kconfig
+++ b/drivers/media/dvb/frontends/Kconfig
@@ -1,7 +1,7 @@
1config DVB_FE_CUSTOMISE 1config DVB_FE_CUSTOMISE
2 bool "Customise the frontend modules to build" 2 bool "Customise the frontend modules to build"
3 depends on DVB_CORE 3 depends on DVB_CORE
4 default N 4 default y if EMBEDDED
5 help 5 help
6 This allows the user to select/deselect frontend drivers for their 6 This allows the user to select/deselect frontend drivers for their
7 hardware from the build. 7 hardware from the build.
diff --git a/drivers/media/dvb/siano/smscoreapi.c b/drivers/media/dvb/siano/smscoreapi.c
index 7f2c94a15ab..d93468cd3a8 100644
--- a/drivers/media/dvb/siano/smscoreapi.c
+++ b/drivers/media/dvb/siano/smscoreapi.c
@@ -1113,9 +1113,11 @@ struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev)
1113 */ 1113 */
1114 1114
1115 prepare_to_wait(&coredev->buffer_mng_waitq, &wait, TASK_INTERRUPTIBLE); 1115 prepare_to_wait(&coredev->buffer_mng_waitq, &wait, TASK_INTERRUPTIBLE);
1116 1116 if (list_empty(&coredev->buffers)) {
1117 if (list_empty(&coredev->buffers)) 1117 spin_unlock_irqrestore(&coredev->bufferslock, flags);
1118 schedule(); 1118 schedule();
1119 spin_lock_irqsave(&coredev->bufferslock, flags);
1120 }
1119 1121
1120 finish_wait(&coredev->buffer_mng_waitq, &wait); 1122 finish_wait(&coredev->buffer_mng_waitq, &wait);
1121 1123
diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
index 2e15903b976..f6e4d047535 100644
--- a/drivers/media/video/Kconfig
+++ b/drivers/media/video/Kconfig
@@ -83,7 +83,7 @@ config VIDEO_FIXED_MINOR_RANGES
83 83
84config VIDEO_HELPER_CHIPS_AUTO 84config VIDEO_HELPER_CHIPS_AUTO
85 bool "Autoselect pertinent encoders/decoders and other helper chips" 85 bool "Autoselect pertinent encoders/decoders and other helper chips"
86 default y 86 default y if !EMBEDDED
87 ---help--- 87 ---help---
88 Most video cards may require additional modules to encode or 88 Most video cards may require additional modules to encode or
89 decode audio/video standards. This option will autoselect 89 decode audio/video standards. This option will autoselect
@@ -792,10 +792,11 @@ config SOC_CAMERA_MT9M001
792 and colour models. 792 and colour models.
793 793
794config SOC_CAMERA_MT9M111 794config SOC_CAMERA_MT9M111
795 tristate "mt9m111 and mt9m112 support" 795 tristate "mt9m111, mt9m112 and mt9m131 support"
796 depends on SOC_CAMERA && I2C 796 depends on SOC_CAMERA && I2C
797 help 797 help
798 This driver supports MT9M111 and MT9M112 cameras from Micron 798 This driver supports MT9M111, MT9M112 and MT9M131 cameras from
799 Micron/Aptina
799 800
800config SOC_CAMERA_MT9T031 801config SOC_CAMERA_MT9T031
801 tristate "mt9t031 support" 802 tristate "mt9t031 support"
@@ -1016,4 +1017,13 @@ config VIDEO_MEM2MEM_TESTDEV
1016 This is a virtual test device for the memory-to-memory driver 1017 This is a virtual test device for the memory-to-memory driver
1017 framework. 1018 framework.
1018 1019
1020config VIDEO_SAMSUNG_S5P_FIMC
1021 tristate "Samsung S5P FIMC (video postprocessor) driver"
1022 depends on VIDEO_DEV && VIDEO_V4L2 && PLAT_S5P
1023 select VIDEOBUF_DMA_CONTIG
1024 select V4L2_MEM2MEM_DEV
1025 help
1026 This is a v4l2 driver for the S5P camera interface
1027 (video postprocessor)
1028
1019endif # V4L_MEM2MEM_DRIVERS 1029endif # V4L_MEM2MEM_DRIVERS
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile
index 1051ecc602e..40f98fba5f8 100644
--- a/drivers/media/video/Makefile
+++ b/drivers/media/video/Makefile
@@ -11,7 +11,7 @@ stkwebcam-objs := stk-webcam.o stk-sensor.o
11omap2cam-objs := omap24xxcam.o omap24xxcam-dma.o 11omap2cam-objs := omap24xxcam.o omap24xxcam-dma.o
12 12
13videodev-objs := v4l2-dev.o v4l2-ioctl.o v4l2-device.o v4l2-fh.o \ 13videodev-objs := v4l2-dev.o v4l2-ioctl.o v4l2-device.o v4l2-fh.o \
14 v4l2-event.o 14 v4l2-event.o v4l2-ctrls.o
15 15
16# V4L2 core modules 16# V4L2 core modules
17 17
@@ -163,6 +163,7 @@ obj-$(CONFIG_VIDEO_MX3) += mx3_camera.o
163obj-$(CONFIG_VIDEO_PXA27x) += pxa_camera.o 163obj-$(CONFIG_VIDEO_PXA27x) += pxa_camera.o
164obj-$(CONFIG_VIDEO_SH_MOBILE_CSI2) += sh_mobile_csi2.o 164obj-$(CONFIG_VIDEO_SH_MOBILE_CSI2) += sh_mobile_csi2.o
165obj-$(CONFIG_VIDEO_SH_MOBILE_CEU) += sh_mobile_ceu_camera.o 165obj-$(CONFIG_VIDEO_SH_MOBILE_CEU) += sh_mobile_ceu_camera.o
166obj-$(CONFIG_VIDEO_SAMSUNG_S5P_FIMC) += s5p-fimc/
166 167
167obj-$(CONFIG_ARCH_DAVINCI) += davinci/ 168obj-$(CONFIG_ARCH_DAVINCI) += davinci/
168 169
diff --git a/drivers/media/video/cs53l32a.c b/drivers/media/video/cs53l32a.c
index 3cc135a98d8..cc9e84d75ea 100644
--- a/drivers/media/video/cs53l32a.c
+++ b/drivers/media/video/cs53l32a.c
@@ -26,10 +26,10 @@
26#include <linux/ioctl.h> 26#include <linux/ioctl.h>
27#include <asm/uaccess.h> 27#include <asm/uaccess.h>
28#include <linux/i2c.h> 28#include <linux/i2c.h>
29#include <linux/i2c-id.h>
30#include <linux/videodev2.h> 29#include <linux/videodev2.h>
31#include <media/v4l2-device.h> 30#include <media/v4l2-device.h>
32#include <media/v4l2-chip-ident.h> 31#include <media/v4l2-chip-ident.h>
32#include <media/v4l2-ctrls.h>
33#include <media/v4l2-i2c-drv.h> 33#include <media/v4l2-i2c-drv.h>
34 34
35MODULE_DESCRIPTION("i2c device driver for cs53l32a Audio ADC"); 35MODULE_DESCRIPTION("i2c device driver for cs53l32a Audio ADC");
@@ -43,6 +43,21 @@ module_param(debug, bool, 0644);
43MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On"); 43MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On");
44 44
45 45
46struct cs53l32a_state {
47 struct v4l2_subdev sd;
48 struct v4l2_ctrl_handler hdl;
49};
50
51static inline struct cs53l32a_state *to_state(struct v4l2_subdev *sd)
52{
53 return container_of(sd, struct cs53l32a_state, sd);
54}
55
56static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
57{
58 return &container_of(ctrl->handler, struct cs53l32a_state, hdl)->sd;
59}
60
46/* ----------------------------------------------------------------------- */ 61/* ----------------------------------------------------------------------- */
47 62
48static int cs53l32a_write(struct v4l2_subdev *sd, u8 reg, u8 value) 63static int cs53l32a_write(struct v4l2_subdev *sd, u8 reg, u8 value)
@@ -74,31 +89,20 @@ static int cs53l32a_s_routing(struct v4l2_subdev *sd,
74 return 0; 89 return 0;
75} 90}
76 91
77static int cs53l32a_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 92static int cs53l32a_s_ctrl(struct v4l2_ctrl *ctrl)
78{ 93{
79 if (ctrl->id == V4L2_CID_AUDIO_MUTE) { 94 struct v4l2_subdev *sd = to_sd(ctrl);
80 ctrl->value = (cs53l32a_read(sd, 0x03) & 0xc0) != 0;
81 return 0;
82 }
83 if (ctrl->id != V4L2_CID_AUDIO_VOLUME)
84 return -EINVAL;
85 ctrl->value = (s8)cs53l32a_read(sd, 0x04);
86 return 0;
87}
88 95
89static int cs53l32a_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 96 switch (ctrl->id) {
90{ 97 case V4L2_CID_AUDIO_MUTE:
91 if (ctrl->id == V4L2_CID_AUDIO_MUTE) { 98 cs53l32a_write(sd, 0x03, ctrl->val ? 0xf0 : 0x30);
92 cs53l32a_write(sd, 0x03, ctrl->value ? 0xf0 : 0x30); 99 return 0;
100 case V4L2_CID_AUDIO_VOLUME:
101 cs53l32a_write(sd, 0x04, (u8)ctrl->val);
102 cs53l32a_write(sd, 0x05, (u8)ctrl->val);
93 return 0; 103 return 0;
94 } 104 }
95 if (ctrl->id != V4L2_CID_AUDIO_VOLUME) 105 return -EINVAL;
96 return -EINVAL;
97 if (ctrl->value > 12 || ctrl->value < -96)
98 return -EINVAL;
99 cs53l32a_write(sd, 0x04, (u8) ctrl->value);
100 cs53l32a_write(sd, 0x05, (u8) ctrl->value);
101 return 0;
102} 106}
103 107
104static int cs53l32a_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip) 108static int cs53l32a_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
@@ -111,23 +115,30 @@ static int cs53l32a_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_id
111 115
112static int cs53l32a_log_status(struct v4l2_subdev *sd) 116static int cs53l32a_log_status(struct v4l2_subdev *sd)
113{ 117{
118 struct cs53l32a_state *state = to_state(sd);
114 u8 v = cs53l32a_read(sd, 0x01); 119 u8 v = cs53l32a_read(sd, 0x01);
115 u8 m = cs53l32a_read(sd, 0x03);
116 s8 vol = cs53l32a_read(sd, 0x04);
117 120
118 v4l2_info(sd, "Input: %d%s\n", (v >> 4) & 3, 121 v4l2_info(sd, "Input: %d\n", (v >> 4) & 3);
119 (m & 0xC0) ? " (muted)" : ""); 122 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
120 v4l2_info(sd, "Volume: %d dB\n", vol);
121 return 0; 123 return 0;
122} 124}
123 125
124/* ----------------------------------------------------------------------- */ 126/* ----------------------------------------------------------------------- */
125 127
128static const struct v4l2_ctrl_ops cs53l32a_ctrl_ops = {
129 .s_ctrl = cs53l32a_s_ctrl,
130};
131
126static const struct v4l2_subdev_core_ops cs53l32a_core_ops = { 132static const struct v4l2_subdev_core_ops cs53l32a_core_ops = {
127 .log_status = cs53l32a_log_status, 133 .log_status = cs53l32a_log_status,
128 .g_chip_ident = cs53l32a_g_chip_ident, 134 .g_chip_ident = cs53l32a_g_chip_ident,
129 .g_ctrl = cs53l32a_g_ctrl, 135 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
130 .s_ctrl = cs53l32a_s_ctrl, 136 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
137 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
138 .g_ctrl = v4l2_subdev_g_ctrl,
139 .s_ctrl = v4l2_subdev_s_ctrl,
140 .queryctrl = v4l2_subdev_queryctrl,
141 .querymenu = v4l2_subdev_querymenu,
131}; 142};
132 143
133static const struct v4l2_subdev_audio_ops cs53l32a_audio_ops = { 144static const struct v4l2_subdev_audio_ops cs53l32a_audio_ops = {
@@ -151,6 +162,7 @@ static const struct v4l2_subdev_ops cs53l32a_ops = {
151static int cs53l32a_probe(struct i2c_client *client, 162static int cs53l32a_probe(struct i2c_client *client,
152 const struct i2c_device_id *id) 163 const struct i2c_device_id *id)
153{ 164{
165 struct cs53l32a_state *state;
154 struct v4l2_subdev *sd; 166 struct v4l2_subdev *sd;
155 int i; 167 int i;
156 168
@@ -164,9 +176,10 @@ static int cs53l32a_probe(struct i2c_client *client,
164 v4l_info(client, "chip found @ 0x%x (%s)\n", 176 v4l_info(client, "chip found @ 0x%x (%s)\n",
165 client->addr << 1, client->adapter->name); 177 client->addr << 1, client->adapter->name);
166 178
167 sd = kmalloc(sizeof(struct v4l2_subdev), GFP_KERNEL); 179 state = kzalloc(sizeof(struct cs53l32a_state), GFP_KERNEL);
168 if (sd == NULL) 180 if (state == NULL)
169 return -ENOMEM; 181 return -ENOMEM;
182 sd = &state->sd;
170 v4l2_i2c_subdev_init(sd, client, &cs53l32a_ops); 183 v4l2_i2c_subdev_init(sd, client, &cs53l32a_ops);
171 184
172 for (i = 1; i <= 7; i++) { 185 for (i = 1; i <= 7; i++) {
@@ -175,15 +188,29 @@ static int cs53l32a_probe(struct i2c_client *client,
175 v4l2_dbg(1, debug, sd, "Read Reg %d %02x\n", i, v); 188 v4l2_dbg(1, debug, sd, "Read Reg %d %02x\n", i, v);
176 } 189 }
177 190
191 v4l2_ctrl_handler_init(&state->hdl, 2);
192 v4l2_ctrl_new_std(&state->hdl, &cs53l32a_ctrl_ops,
193 V4L2_CID_AUDIO_VOLUME, -96, 12, 1, 0);
194 v4l2_ctrl_new_std(&state->hdl, &cs53l32a_ctrl_ops,
195 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
196 sd->ctrl_handler = &state->hdl;
197 if (state->hdl.error) {
198 int err = state->hdl.error;
199
200 v4l2_ctrl_handler_free(&state->hdl);
201 kfree(state);
202 return err;
203 }
204
178 /* Set cs53l32a internal register for Adaptec 2010/2410 setup */ 205 /* Set cs53l32a internal register for Adaptec 2010/2410 setup */
179 206
180 cs53l32a_write(sd, 0x01, (u8) 0x21); 207 cs53l32a_write(sd, 0x01, 0x21);
181 cs53l32a_write(sd, 0x02, (u8) 0x29); 208 cs53l32a_write(sd, 0x02, 0x29);
182 cs53l32a_write(sd, 0x03, (u8) 0x30); 209 cs53l32a_write(sd, 0x03, 0x30);
183 cs53l32a_write(sd, 0x04, (u8) 0x00); 210 cs53l32a_write(sd, 0x04, 0x00);
184 cs53l32a_write(sd, 0x05, (u8) 0x00); 211 cs53l32a_write(sd, 0x05, 0x00);
185 cs53l32a_write(sd, 0x06, (u8) 0x00); 212 cs53l32a_write(sd, 0x06, 0x00);
186 cs53l32a_write(sd, 0x07, (u8) 0x00); 213 cs53l32a_write(sd, 0x07, 0x00);
187 214
188 /* Display results, should be 0x21,0x29,0x30,0x00,0x00,0x00,0x00 */ 215 /* Display results, should be 0x21,0x29,0x30,0x00,0x00,0x00,0x00 */
189 216
@@ -198,9 +225,11 @@ static int cs53l32a_probe(struct i2c_client *client,
198static int cs53l32a_remove(struct i2c_client *client) 225static int cs53l32a_remove(struct i2c_client *client)
199{ 226{
200 struct v4l2_subdev *sd = i2c_get_clientdata(client); 227 struct v4l2_subdev *sd = i2c_get_clientdata(client);
228 struct cs53l32a_state *state = to_state(sd);
201 229
202 v4l2_device_unregister_subdev(sd); 230 v4l2_device_unregister_subdev(sd);
203 kfree(sd); 231 v4l2_ctrl_handler_free(&state->hdl);
232 kfree(state);
204 return 0; 233 return 0;
205} 234}
206 235
diff --git a/drivers/media/video/cx2341x.c b/drivers/media/video/cx2341x.c
index 2bf44ef10fe..e5c3c8da4be 100644
--- a/drivers/media/video/cx2341x.c
+++ b/drivers/media/video/cx2341x.c
@@ -38,6 +38,145 @@ static 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
41/********************** COMMON CODE *********************/
42
43/* definitions for audio properties bits 29-28 */
44#define CX2341X_AUDIO_ENCODING_METHOD_MPEG 0
45#define CX2341X_AUDIO_ENCODING_METHOD_AC3 1
46#define CX2341X_AUDIO_ENCODING_METHOD_LPCM 2
47
48static const char *cx2341x_get_name(u32 id)
49{
50 switch (id) {
51 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
52 return "Spatial Filter Mode";
53 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
54 return "Spatial Filter";
55 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
56 return "Spatial Luma Filter Type";
57 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
58 return "Spatial Chroma Filter Type";
59 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
60 return "Temporal Filter Mode";
61 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
62 return "Temporal Filter";
63 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
64 return "Median Filter Type";
65 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
66 return "Median Luma Filter Maximum";
67 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
68 return "Median Luma Filter Minimum";
69 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
70 return "Median Chroma Filter Maximum";
71 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
72 return "Median Chroma Filter Minimum";
73 case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
74 return "Insert Navigation Packets";
75 }
76 return NULL;
77}
78
79static const char **cx2341x_get_menu(u32 id)
80{
81 static const char *cx2341x_video_spatial_filter_mode_menu[] = {
82 "Manual",
83 "Auto",
84 NULL
85 };
86
87 static const char *cx2341x_video_luma_spatial_filter_type_menu[] = {
88 "Off",
89 "1D Horizontal",
90 "1D Vertical",
91 "2D H/V Separable",
92 "2D Symmetric non-separable",
93 NULL
94 };
95
96 static const char *cx2341x_video_chroma_spatial_filter_type_menu[] = {
97 "Off",
98 "1D Horizontal",
99 NULL
100 };
101
102 static const char *cx2341x_video_temporal_filter_mode_menu[] = {
103 "Manual",
104 "Auto",
105 NULL
106 };
107
108 static const char *cx2341x_video_median_filter_type_menu[] = {
109 "Off",
110 "Horizontal",
111 "Vertical",
112 "Horizontal/Vertical",
113 "Diagonal",
114 NULL
115 };
116
117 switch (id) {
118 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
119 return cx2341x_video_spatial_filter_mode_menu;
120 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
121 return cx2341x_video_luma_spatial_filter_type_menu;
122 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
123 return cx2341x_video_chroma_spatial_filter_type_menu;
124 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
125 return cx2341x_video_temporal_filter_mode_menu;
126 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
127 return cx2341x_video_median_filter_type_menu;
128 }
129 return NULL;
130}
131
132static void cx2341x_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
133 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
134{
135 *name = cx2341x_get_name(id);
136 *flags = 0;
137
138 switch (id) {
139 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
140 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
141 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
142 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
143 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
144 *type = V4L2_CTRL_TYPE_MENU;
145 *min = 0;
146 *step = 0;
147 break;
148 case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
149 *type = V4L2_CTRL_TYPE_BOOLEAN;
150 *min = 0;
151 *max = *step = 1;
152 break;
153 default:
154 *type = V4L2_CTRL_TYPE_INTEGER;
155 break;
156 }
157 switch (id) {
158 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
159 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
160 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
161 *flags |= V4L2_CTRL_FLAG_UPDATE;
162 break;
163 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
164 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
165 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
166 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
167 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
168 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
169 *flags |= V4L2_CTRL_FLAG_SLIDER;
170 break;
171 case V4L2_CID_MPEG_VIDEO_ENCODING:
172 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
173 break;
174 }
175}
176
177
178/********************** OLD CODE *********************/
179
41/* Must be sorted from low to high control ID! */ 180/* Must be sorted from low to high control ID! */
42const u32 cx2341x_mpeg_ctrls[] = { 181const u32 cx2341x_mpeg_ctrls[] = {
43 V4L2_CID_MPEG_CLASS, 182 V4L2_CID_MPEG_CLASS,
@@ -134,8 +273,6 @@ static const struct cx2341x_mpeg_params default_params = {
134 .video_chroma_median_filter_top = 255, 273 .video_chroma_median_filter_top = 255,
135 .video_chroma_median_filter_bottom = 0, 274 .video_chroma_median_filter_bottom = 0,
136}; 275};
137
138
139/* Map the control ID to the correct field in the cx2341x_mpeg_params 276/* Map the control ID to the correct field in the cx2341x_mpeg_params
140 struct. Return -EINVAL if the ID is unknown, else return 0. */ 277 struct. Return -EINVAL if the ID is unknown, else return 0. */
141static int cx2341x_get_ctrl(const struct cx2341x_mpeg_params *params, 278static int cx2341x_get_ctrl(const struct cx2341x_mpeg_params *params,
@@ -415,83 +552,33 @@ static int cx2341x_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
415{ 552{
416 const char *name; 553 const char *name;
417 554
418 qctrl->flags = 0;
419 switch (qctrl->id) { 555 switch (qctrl->id) {
420 /* MPEG controls */ 556 /* MPEG controls */
421 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE: 557 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
422 name = "Spatial Filter Mode";
423 break;
424 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER: 558 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
425 name = "Spatial Filter";
426 break;
427 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE: 559 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
428 name = "Spatial Luma Filter Type";
429 break;
430 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE: 560 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
431 name = "Spatial Chroma Filter Type";
432 break;
433 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE: 561 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
434 name = "Temporal Filter Mode";
435 break;
436 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER: 562 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
437 name = "Temporal Filter";
438 break;
439 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE: 563 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
440 name = "Median Filter Type";
441 break;
442 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP: 564 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
443 name = "Median Luma Filter Maximum";
444 break;
445 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM: 565 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
446 name = "Median Luma Filter Minimum";
447 break;
448 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP: 566 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
449 name = "Median Chroma Filter Maximum";
450 break;
451 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM: 567 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
452 name = "Median Chroma Filter Minimum";
453 break;
454 case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS: 568 case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
455 name = "Insert Navigation Packets"; 569 cx2341x_ctrl_fill(qctrl->id, &name, &qctrl->type,
456 break; 570 &min, &max, &step, &def, &qctrl->flags);
571 qctrl->minimum = min;
572 qctrl->maximum = max;
573 qctrl->step = step;
574 qctrl->default_value = def;
575 qctrl->reserved[0] = qctrl->reserved[1] = 0;
576 strlcpy(qctrl->name, name, sizeof(qctrl->name));
577 return 0;
457 578
458 default: 579 default:
459 return v4l2_ctrl_query_fill(qctrl, min, max, step, def); 580 return v4l2_ctrl_query_fill(qctrl, min, max, step, def);
460 } 581 }
461 switch (qctrl->id) {
462 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
463 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
464 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
465 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
466 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
467 qctrl->type = V4L2_CTRL_TYPE_MENU;
468 min = 0;
469 step = 1;
470 break;
471 case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
472 qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
473 min = 0;
474 max = 1;
475 step = 1;
476 break;
477 default:
478 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
479 break;
480 }
481 switch (qctrl->id) {
482 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
483 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
484 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
485 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
486 break;
487 }
488 qctrl->minimum = min;
489 qctrl->maximum = max;
490 qctrl->step = step;
491 qctrl->default_value = def;
492 qctrl->reserved[0] = qctrl->reserved[1] = 0;
493 snprintf(qctrl->name, sizeof(qctrl->name), name);
494 return 0;
495} 582}
496 583
497int cx2341x_ctrl_query(const struct cx2341x_mpeg_params *params, 584int cx2341x_ctrl_query(const struct cx2341x_mpeg_params *params,
@@ -797,42 +884,6 @@ const char **cx2341x_ctrl_get_menu(const struct cx2341x_mpeg_params *p, u32 id)
797 NULL 884 NULL
798 }; 885 };
799 886
800 static const char *cx2341x_video_spatial_filter_mode_menu[] = {
801 "Manual",
802 "Auto",
803 NULL
804 };
805
806 static const char *cx2341x_video_luma_spatial_filter_type_menu[] = {
807 "Off",
808 "1D Horizontal",
809 "1D Vertical",
810 "2D H/V Separable",
811 "2D Symmetric non-separable",
812 NULL
813 };
814
815 static const char *cx2341x_video_chroma_spatial_filter_type_menu[] = {
816 "Off",
817 "1D Horizontal",
818 NULL
819 };
820
821 static const char *cx2341x_video_temporal_filter_mode_menu[] = {
822 "Manual",
823 "Auto",
824 NULL
825 };
826
827 static const char *cx2341x_video_median_filter_type_menu[] = {
828 "Off",
829 "Horizontal",
830 "Vertical",
831 "Horizontal/Vertical",
832 "Diagonal",
833 NULL
834 };
835
836 switch (id) { 887 switch (id) {
837 case V4L2_CID_MPEG_STREAM_TYPE: 888 case V4L2_CID_MPEG_STREAM_TYPE:
838 return (p->capabilities & CX2341X_CAP_HAS_TS) ? 889 return (p->capabilities & CX2341X_CAP_HAS_TS) ?
@@ -844,26 +895,17 @@ const char **cx2341x_ctrl_get_menu(const struct cx2341x_mpeg_params *p, u32 id)
844 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: 895 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
845 return NULL; 896 return NULL;
846 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE: 897 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
847 return cx2341x_video_spatial_filter_mode_menu;
848 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE: 898 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
849 return cx2341x_video_luma_spatial_filter_type_menu;
850 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE: 899 case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
851 return cx2341x_video_chroma_spatial_filter_type_menu;
852 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE: 900 case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
853 return cx2341x_video_temporal_filter_mode_menu;
854 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE: 901 case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
855 return cx2341x_video_median_filter_type_menu; 902 return cx2341x_get_menu(id);
856 default: 903 default:
857 return v4l2_ctrl_get_menu(id); 904 return v4l2_ctrl_get_menu(id);
858 } 905 }
859} 906}
860EXPORT_SYMBOL(cx2341x_ctrl_get_menu); 907EXPORT_SYMBOL(cx2341x_ctrl_get_menu);
861 908
862/* definitions for audio properties bits 29-28 */
863#define CX2341X_AUDIO_ENCODING_METHOD_MPEG 0
864#define CX2341X_AUDIO_ENCODING_METHOD_AC3 1
865#define CX2341X_AUDIO_ENCODING_METHOD_LPCM 2
866
867static void cx2341x_calc_audio_properties(struct cx2341x_mpeg_params *params) 909static void cx2341x_calc_audio_properties(struct cx2341x_mpeg_params *params)
868{ 910{
869 params->audio_properties = 911 params->audio_properties =
@@ -1195,9 +1237,490 @@ void cx2341x_log_status(const struct cx2341x_mpeg_params *p, const char *prefix)
1195} 1237}
1196EXPORT_SYMBOL(cx2341x_log_status); 1238EXPORT_SYMBOL(cx2341x_log_status);
1197 1239
1198/*
1199 * Local variables:
1200 * c-basic-offset: 8
1201 * End:
1202 */
1203 1240
1241
1242/********************** NEW CODE *********************/
1243
1244static inline struct cx2341x_handler *to_cxhdl(struct v4l2_ctrl *ctrl)
1245{
1246 return container_of(ctrl->handler, struct cx2341x_handler, hdl);
1247}
1248
1249static int cx2341x_hdl_api(struct cx2341x_handler *hdl,
1250 u32 cmd, int args, ...)
1251{
1252 u32 data[CX2341X_MBOX_MAX_DATA];
1253 va_list vargs;
1254 int i;
1255
1256 va_start(vargs, args);
1257
1258 for (i = 0; i < args; i++)
1259 data[i] = va_arg(vargs, int);
1260 va_end(vargs);
1261 return hdl->func(hdl->priv, cmd, args, 0, data);
1262}
1263
1264/* ctrl->handler->lock is held, so it is safe to access cur.val */
1265static inline int cx2341x_neq(struct v4l2_ctrl *ctrl)
1266{
1267 return ctrl && ctrl->val != ctrl->cur.val;
1268}
1269
1270static int cx2341x_try_ctrl(struct v4l2_ctrl *ctrl)
1271{
1272 struct cx2341x_handler *hdl = to_cxhdl(ctrl);
1273 s32 val = ctrl->val;
1274
1275 switch (ctrl->id) {
1276 case V4L2_CID_MPEG_VIDEO_B_FRAMES: {
1277 /* video gop cluster */
1278 int b = val + 1;
1279 int gop = hdl->video_gop_size->val;
1280
1281 gop = b * ((gop + b - 1) / b);
1282
1283 /* Max GOP size = 34 */
1284 while (gop > 34)
1285 gop -= b;
1286 hdl->video_gop_size->val = gop;
1287 break;
1288 }
1289
1290 case V4L2_CID_MPEG_STREAM_TYPE:
1291 /* stream type cluster */
1292 hdl->video_encoding->val =
1293 (hdl->stream_type->val == V4L2_MPEG_STREAM_TYPE_MPEG1_SS ||
1294 hdl->stream_type->val == V4L2_MPEG_STREAM_TYPE_MPEG1_VCD) ?
1295 V4L2_MPEG_VIDEO_ENCODING_MPEG_1 :
1296 V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
1297 if (hdl->video_encoding->val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
1298 /* MPEG-1 implies CBR */
1299 hdl->video_bitrate_mode->val =
1300 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
1301 /* peak bitrate shall be >= normal bitrate */
1302 if (hdl->video_bitrate_mode->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1303 hdl->video_bitrate_peak->val < hdl->video_bitrate->val)
1304 hdl->video_bitrate_peak->val = hdl->video_bitrate->val;
1305 break;
1306 }
1307 return 0;
1308}
1309
1310static int cx2341x_s_ctrl(struct v4l2_ctrl *ctrl)
1311{
1312 static const int mpeg_stream_type[] = {
1313 0, /* MPEG-2 PS */
1314 1, /* MPEG-2 TS */
1315 2, /* MPEG-1 SS */
1316 14, /* DVD */
1317 11, /* VCD */
1318 12, /* SVCD */
1319 };
1320 struct cx2341x_handler *hdl = to_cxhdl(ctrl);
1321 s32 val = ctrl->val;
1322 u32 props;
1323 int err;
1324
1325 switch (ctrl->id) {
1326 case V4L2_CID_MPEG_STREAM_VBI_FMT:
1327 if (hdl->ops && hdl->ops->s_stream_vbi_fmt)
1328 return hdl->ops->s_stream_vbi_fmt(hdl, val);
1329 return 0;
1330
1331 case V4L2_CID_MPEG_VIDEO_ASPECT:
1332 return cx2341x_hdl_api(hdl,
1333 CX2341X_ENC_SET_ASPECT_RATIO, 1, val + 1);
1334
1335 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1336 return cx2341x_hdl_api(hdl, CX2341X_ENC_SET_GOP_CLOSURE, 1, val);
1337
1338 case V4L2_CID_MPEG_AUDIO_MUTE:
1339 return cx2341x_hdl_api(hdl, CX2341X_ENC_MUTE_AUDIO, 1, val);
1340
1341 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
1342 return cx2341x_hdl_api(hdl,
1343 CX2341X_ENC_SET_FRAME_DROP_RATE, 1, val);
1344
1345 case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
1346 return cx2341x_hdl_api(hdl, CX2341X_ENC_MISC, 2, 7, val);
1347
1348 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
1349 /* audio properties cluster */
1350 props = (hdl->audio_sampling_freq->val << 0) |
1351 (hdl->audio_mode->val << 8) |
1352 (hdl->audio_mode_extension->val << 10) |
1353 (hdl->audio_crc->val << 14);
1354 if (hdl->audio_emphasis->val == V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17)
1355 props |= 3 << 12;
1356 else
1357 props |= hdl->audio_emphasis->val << 12;
1358
1359 if (hdl->audio_encoding->val == V4L2_MPEG_AUDIO_ENCODING_AC3) {
1360 props |=
1361#if 1
1362 /* Not sure if this MPEG Layer II setting is required */
1363 ((3 - V4L2_MPEG_AUDIO_ENCODING_LAYER_2) << 2) |
1364#endif
1365 (hdl->audio_ac3_bitrate->val << 4) |
1366 (CX2341X_AUDIO_ENCODING_METHOD_AC3 << 28);
1367 } else {
1368 /* Assuming MPEG Layer II */
1369 props |=
1370 ((3 - hdl->audio_encoding->val) << 2) |
1371 ((1 + hdl->audio_l2_bitrate->val) << 4);
1372 }
1373 err = cx2341x_hdl_api(hdl,
1374 CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, props);
1375 if (err)
1376 return err;
1377
1378 hdl->audio_properties = props;
1379 if (hdl->audio_ac3_bitrate) {
1380 int is_ac3 = hdl->audio_encoding->val ==
1381 V4L2_MPEG_AUDIO_ENCODING_AC3;
1382
1383 v4l2_ctrl_activate(hdl->audio_ac3_bitrate, is_ac3);
1384 v4l2_ctrl_activate(hdl->audio_l2_bitrate, !is_ac3);
1385 }
1386 v4l2_ctrl_activate(hdl->audio_mode_extension,
1387 hdl->audio_mode->val == V4L2_MPEG_AUDIO_MODE_JOINT_STEREO);
1388 if (cx2341x_neq(hdl->audio_sampling_freq) &&
1389 hdl->ops && hdl->ops->s_audio_sampling_freq)
1390 return hdl->ops->s_audio_sampling_freq(hdl, hdl->audio_sampling_freq->val);
1391 if (cx2341x_neq(hdl->audio_mode) &&
1392 hdl->ops && hdl->ops->s_audio_mode)
1393 return hdl->ops->s_audio_mode(hdl, hdl->audio_mode->val);
1394 return 0;
1395
1396 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1397 /* video gop cluster */
1398 return cx2341x_hdl_api(hdl, CX2341X_ENC_SET_GOP_PROPERTIES, 2,
1399 hdl->video_gop_size->val,
1400 hdl->video_b_frames->val + 1);
1401
1402 case V4L2_CID_MPEG_STREAM_TYPE:
1403 /* stream type cluster */
1404 err = cx2341x_hdl_api(hdl,
1405 CX2341X_ENC_SET_STREAM_TYPE, 1, mpeg_stream_type[val]);
1406 if (err)
1407 return err;
1408
1409 err = cx2341x_hdl_api(hdl, CX2341X_ENC_SET_BIT_RATE, 5,
1410 hdl->video_bitrate_mode->val,
1411 hdl->video_bitrate->val,
1412 hdl->video_bitrate_peak->val / 400, 0, 0);
1413 if (err)
1414 return err;
1415
1416 v4l2_ctrl_activate(hdl->video_bitrate_mode,
1417 hdl->video_encoding->val != V4L2_MPEG_VIDEO_ENCODING_MPEG_1);
1418 v4l2_ctrl_activate(hdl->video_bitrate_peak,
1419 hdl->video_bitrate_mode->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
1420 if (cx2341x_neq(hdl->video_encoding) &&
1421 hdl->ops && hdl->ops->s_video_encoding)
1422 return hdl->ops->s_video_encoding(hdl, hdl->video_encoding->val);
1423 return 0;
1424
1425 case V4L2_CID_MPEG_VIDEO_MUTE:
1426 /* video mute cluster */
1427 return cx2341x_hdl_api(hdl, CX2341X_ENC_MUTE_VIDEO, 1,
1428 hdl->video_mute->val |
1429 (hdl->video_mute_yuv->val << 8));
1430
1431 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE: {
1432 int active_filter;
1433
1434 /* video filter mode */
1435 err = cx2341x_hdl_api(hdl, CX2341X_ENC_SET_DNR_FILTER_MODE, 2,
1436 hdl->video_spatial_filter_mode->val |
1437 (hdl->video_temporal_filter_mode->val << 1),
1438 hdl->video_median_filter_type->val);
1439 if (err)
1440 return err;
1441
1442 active_filter = hdl->video_spatial_filter_mode->val !=
1443 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO;
1444 v4l2_ctrl_activate(hdl->video_spatial_filter, active_filter);
1445 v4l2_ctrl_activate(hdl->video_luma_spatial_filter_type, active_filter);
1446 v4l2_ctrl_activate(hdl->video_chroma_spatial_filter_type, active_filter);
1447 active_filter = hdl->video_temporal_filter_mode->val !=
1448 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO;
1449 v4l2_ctrl_activate(hdl->video_temporal_filter, active_filter);
1450 active_filter = hdl->video_median_filter_type->val !=
1451 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF;
1452 v4l2_ctrl_activate(hdl->video_luma_median_filter_bottom, active_filter);
1453 v4l2_ctrl_activate(hdl->video_luma_median_filter_top, active_filter);
1454 v4l2_ctrl_activate(hdl->video_chroma_median_filter_bottom, active_filter);
1455 v4l2_ctrl_activate(hdl->video_chroma_median_filter_top, active_filter);
1456 return 0;
1457 }
1458
1459 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
1460 /* video filter type cluster */
1461 return cx2341x_hdl_api(hdl,
1462 CX2341X_ENC_SET_SPATIAL_FILTER_TYPE, 2,
1463 hdl->video_luma_spatial_filter_type->val,
1464 hdl->video_chroma_spatial_filter_type->val);
1465
1466 case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
1467 /* video filter cluster */
1468 return cx2341x_hdl_api(hdl, CX2341X_ENC_SET_DNR_FILTER_PROPS, 2,
1469 hdl->video_spatial_filter->val,
1470 hdl->video_temporal_filter->val);
1471
1472 case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
1473 /* video median cluster */
1474 return cx2341x_hdl_api(hdl, CX2341X_ENC_SET_CORING_LEVELS, 4,
1475 hdl->video_luma_median_filter_bottom->val,
1476 hdl->video_luma_median_filter_top->val,
1477 hdl->video_chroma_median_filter_bottom->val,
1478 hdl->video_chroma_median_filter_top->val);
1479 }
1480 return -EINVAL;
1481}
1482
1483static const struct v4l2_ctrl_ops cx2341x_ops = {
1484 .try_ctrl = cx2341x_try_ctrl,
1485 .s_ctrl = cx2341x_s_ctrl,
1486};
1487
1488static struct v4l2_ctrl *cx2341x_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1489 u32 id, s32 min, s32 max, s32 step, s32 def)
1490{
1491 struct v4l2_ctrl_config cfg;
1492
1493 cx2341x_ctrl_fill(id, &cfg.name, &cfg.type, &min, &max, &step, &def, &cfg.flags);
1494 cfg.ops = &cx2341x_ops;
1495 cfg.id = id;
1496 cfg.min = min;
1497 cfg.max = max;
1498 cfg.def = def;
1499 if (cfg.type == V4L2_CTRL_TYPE_MENU) {
1500 cfg.step = 0;
1501 cfg.menu_skip_mask = step;
1502 cfg.qmenu = cx2341x_get_menu(id);
1503 } else {
1504 cfg.step = step;
1505 cfg.menu_skip_mask = 0;
1506 }
1507 return v4l2_ctrl_new_custom(hdl, &cfg, NULL);
1508}
1509
1510static struct v4l2_ctrl *cx2341x_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1511 u32 id, s32 min, s32 max, s32 step, s32 def)
1512{
1513 return v4l2_ctrl_new_std(hdl, &cx2341x_ops, id, min, max, step, def);
1514}
1515
1516static struct v4l2_ctrl *cx2341x_ctrl_new_menu(struct v4l2_ctrl_handler *hdl,
1517 u32 id, s32 max, s32 mask, s32 def)
1518{
1519 return v4l2_ctrl_new_std_menu(hdl, &cx2341x_ops, id, max, mask, def);
1520}
1521
1522int cx2341x_handler_init(struct cx2341x_handler *cxhdl,
1523 unsigned nr_of_controls_hint)
1524{
1525 struct v4l2_ctrl_handler *hdl = &cxhdl->hdl;
1526 u32 caps = cxhdl->capabilities;
1527 int has_sliced_vbi = caps & CX2341X_CAP_HAS_SLICED_VBI;
1528 int has_ac3 = caps & CX2341X_CAP_HAS_AC3;
1529 int has_ts = caps & CX2341X_CAP_HAS_TS;
1530
1531 cxhdl->width = 720;
1532 cxhdl->height = 480;
1533
1534 v4l2_ctrl_handler_init(hdl, nr_of_controls_hint);
1535
1536 /* Add controls in ascending control ID order for fastest
1537 insertion time. */
1538 cxhdl->stream_type = cx2341x_ctrl_new_menu(hdl,
1539 V4L2_CID_MPEG_STREAM_TYPE,
1540 V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD, has_ts ? 0 : 2,
1541 V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
1542 cxhdl->stream_vbi_fmt = cx2341x_ctrl_new_menu(hdl,
1543 V4L2_CID_MPEG_STREAM_VBI_FMT,
1544 V4L2_MPEG_STREAM_VBI_FMT_IVTV, has_sliced_vbi ? 0 : 2,
1545 V4L2_MPEG_STREAM_VBI_FMT_NONE);
1546 cxhdl->audio_sampling_freq = cx2341x_ctrl_new_menu(hdl,
1547 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
1548 V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000, 0,
1549 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
1550 cxhdl->audio_encoding = cx2341x_ctrl_new_menu(hdl,
1551 V4L2_CID_MPEG_AUDIO_ENCODING,
1552 V4L2_MPEG_AUDIO_ENCODING_AC3, has_ac3 ? ~0x12 : ~0x2,
1553 V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
1554 cxhdl->audio_l2_bitrate = cx2341x_ctrl_new_menu(hdl,
1555 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
1556 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 0x1ff,
1557 V4L2_MPEG_AUDIO_L2_BITRATE_224K);
1558 cxhdl->audio_mode = cx2341x_ctrl_new_menu(hdl,
1559 V4L2_CID_MPEG_AUDIO_MODE,
1560 V4L2_MPEG_AUDIO_MODE_MONO, 0,
1561 V4L2_MPEG_AUDIO_MODE_STEREO);
1562 cxhdl->audio_mode_extension = cx2341x_ctrl_new_menu(hdl,
1563 V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
1564 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_16, 0,
1565 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4);
1566 cxhdl->audio_emphasis = cx2341x_ctrl_new_menu(hdl,
1567 V4L2_CID_MPEG_AUDIO_EMPHASIS,
1568 V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17, 0,
1569 V4L2_MPEG_AUDIO_EMPHASIS_NONE);
1570 cxhdl->audio_crc = cx2341x_ctrl_new_menu(hdl,
1571 V4L2_CID_MPEG_AUDIO_CRC,
1572 V4L2_MPEG_AUDIO_CRC_CRC16, 0,
1573 V4L2_MPEG_AUDIO_CRC_NONE);
1574
1575 cx2341x_ctrl_new_std(hdl, V4L2_CID_MPEG_AUDIO_MUTE, 0, 1, 1, 0);
1576 if (has_ac3)
1577 cxhdl->audio_ac3_bitrate = cx2341x_ctrl_new_menu(hdl,
1578 V4L2_CID_MPEG_AUDIO_AC3_BITRATE,
1579 V4L2_MPEG_AUDIO_AC3_BITRATE_448K, 0x03,
1580 V4L2_MPEG_AUDIO_AC3_BITRATE_224K);
1581 cxhdl->video_encoding = cx2341x_ctrl_new_menu(hdl,
1582 V4L2_CID_MPEG_VIDEO_ENCODING,
1583 V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 0,
1584 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
1585 cx2341x_ctrl_new_menu(hdl,
1586 V4L2_CID_MPEG_VIDEO_ASPECT,
1587 V4L2_MPEG_VIDEO_ASPECT_221x100, 0,
1588 V4L2_MPEG_VIDEO_ASPECT_4x3);
1589 cxhdl->video_b_frames = cx2341x_ctrl_new_std(hdl,
1590 V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 33, 1, 2);
1591 cxhdl->video_gop_size = cx2341x_ctrl_new_std(hdl,
1592 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
1593 1, 34, 1, cxhdl->is_50hz ? 12 : 15);
1594 cx2341x_ctrl_new_std(hdl, V4L2_CID_MPEG_VIDEO_GOP_CLOSURE, 0, 1, 1, 1);
1595 cxhdl->video_bitrate_mode = cx2341x_ctrl_new_menu(hdl,
1596 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1597 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
1598 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
1599 cxhdl->video_bitrate = cx2341x_ctrl_new_std(hdl,
1600 V4L2_CID_MPEG_VIDEO_BITRATE,
1601 0, 27000000, 1, 6000000);
1602 cxhdl->video_bitrate_peak = cx2341x_ctrl_new_std(hdl,
1603 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1604 0, 27000000, 1, 8000000);
1605 cx2341x_ctrl_new_std(hdl,
1606 V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION, 0, 255, 1, 0);
1607 cxhdl->video_mute = cx2341x_ctrl_new_std(hdl,
1608 V4L2_CID_MPEG_VIDEO_MUTE, 0, 1, 1, 0);
1609 cxhdl->video_mute_yuv = cx2341x_ctrl_new_std(hdl,
1610 V4L2_CID_MPEG_VIDEO_MUTE_YUV, 0, 0xffffff, 1, 0x008080);
1611
1612 /* CX23415/6 specific */
1613 cxhdl->video_spatial_filter_mode = cx2341x_ctrl_new_custom(hdl,
1614 V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
1615 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL,
1616 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO, 0,
1617 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL);
1618 cxhdl->video_spatial_filter = cx2341x_ctrl_new_custom(hdl,
1619 V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
1620 0, 15, 1, 0);
1621 cxhdl->video_luma_spatial_filter_type = cx2341x_ctrl_new_custom(hdl,
1622 V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
1623 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_OFF,
1624 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_2D_SYM_NON_SEPARABLE,
1625 0,
1626 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_1D_HOR);
1627 cxhdl->video_chroma_spatial_filter_type = cx2341x_ctrl_new_custom(hdl,
1628 V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
1629 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_OFF,
1630 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR,
1631 0,
1632 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR);
1633 cxhdl->video_temporal_filter_mode = cx2341x_ctrl_new_custom(hdl,
1634 V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
1635 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL,
1636 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO,
1637 0,
1638 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL);
1639 cxhdl->video_temporal_filter = cx2341x_ctrl_new_custom(hdl,
1640 V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
1641 0, 31, 1, 8);
1642 cxhdl->video_median_filter_type = cx2341x_ctrl_new_custom(hdl,
1643 V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
1644 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF,
1645 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_DIAG,
1646 0,
1647 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF);
1648 cxhdl->video_luma_median_filter_bottom = cx2341x_ctrl_new_custom(hdl,
1649 V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
1650 0, 255, 1, 0);
1651 cxhdl->video_luma_median_filter_top = cx2341x_ctrl_new_custom(hdl,
1652 V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
1653 0, 255, 1, 255);
1654 cxhdl->video_chroma_median_filter_bottom = cx2341x_ctrl_new_custom(hdl,
1655 V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
1656 0, 255, 1, 0);
1657 cxhdl->video_chroma_median_filter_top = cx2341x_ctrl_new_custom(hdl,
1658 V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
1659 0, 255, 1, 255);
1660 cx2341x_ctrl_new_custom(hdl, V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS,
1661 0, 1, 1, 0);
1662
1663 if (hdl->error) {
1664 int err = hdl->error;
1665
1666 v4l2_ctrl_handler_free(hdl);
1667 return err;
1668 }
1669
1670 v4l2_ctrl_cluster(8, &cxhdl->audio_sampling_freq);
1671 v4l2_ctrl_cluster(2, &cxhdl->video_b_frames);
1672 v4l2_ctrl_cluster(5, &cxhdl->stream_type);
1673 v4l2_ctrl_cluster(2, &cxhdl->video_mute);
1674 v4l2_ctrl_cluster(3, &cxhdl->video_spatial_filter_mode);
1675 v4l2_ctrl_cluster(2, &cxhdl->video_luma_spatial_filter_type);
1676 v4l2_ctrl_cluster(2, &cxhdl->video_spatial_filter);
1677 v4l2_ctrl_cluster(4, &cxhdl->video_luma_median_filter_top);
1678
1679 return 0;
1680}
1681EXPORT_SYMBOL(cx2341x_handler_init);
1682
1683void cx2341x_handler_set_50hz(struct cx2341x_handler *cxhdl, int is_50hz)
1684{
1685 cxhdl->is_50hz = is_50hz;
1686 cxhdl->video_gop_size->default_value = cxhdl->is_50hz ? 12 : 15;
1687}
1688EXPORT_SYMBOL(cx2341x_handler_set_50hz);
1689
1690int cx2341x_handler_setup(struct cx2341x_handler *cxhdl)
1691{
1692 int h = cxhdl->height;
1693 int w = cxhdl->width;
1694 int err;
1695
1696 err = cx2341x_hdl_api(cxhdl, CX2341X_ENC_SET_OUTPUT_PORT, 2, cxhdl->port, 0);
1697 if (err)
1698 return err;
1699 err = cx2341x_hdl_api(cxhdl, CX2341X_ENC_SET_FRAME_RATE, 1, cxhdl->is_50hz);
1700 if (err)
1701 return err;
1702
1703 if (v4l2_ctrl_g_ctrl(cxhdl->video_encoding) == V4L2_MPEG_VIDEO_ENCODING_MPEG_1) {
1704 w /= 2;
1705 h /= 2;
1706 }
1707 err = cx2341x_hdl_api(cxhdl, CX2341X_ENC_SET_FRAME_SIZE, 2, h, w);
1708 if (err)
1709 return err;
1710 return v4l2_ctrl_handler_setup(&cxhdl->hdl);
1711}
1712EXPORT_SYMBOL(cx2341x_handler_setup);
1713
1714void cx2341x_handler_set_busy(struct cx2341x_handler *cxhdl, int busy)
1715{
1716 v4l2_ctrl_grab(cxhdl->audio_sampling_freq, busy);
1717 v4l2_ctrl_grab(cxhdl->audio_encoding, busy);
1718 v4l2_ctrl_grab(cxhdl->audio_l2_bitrate, busy);
1719 v4l2_ctrl_grab(cxhdl->audio_ac3_bitrate, busy);
1720 v4l2_ctrl_grab(cxhdl->stream_vbi_fmt, busy);
1721 v4l2_ctrl_grab(cxhdl->stream_type, busy);
1722 v4l2_ctrl_grab(cxhdl->video_bitrate_mode, busy);
1723 v4l2_ctrl_grab(cxhdl->video_bitrate, busy);
1724 v4l2_ctrl_grab(cxhdl->video_bitrate_peak, busy);
1725}
1726EXPORT_SYMBOL(cx2341x_handler_set_busy);
diff --git a/drivers/media/video/cx23885/Kconfig b/drivers/media/video/cx23885/Kconfig
index bcdda9a9aa9..768f000e4b2 100644
--- a/drivers/media/video/cx23885/Kconfig
+++ b/drivers/media/video/cx23885/Kconfig
@@ -5,7 +5,7 @@ config VIDEO_CX23885
5 select VIDEO_BTCX 5 select VIDEO_BTCX
6 select VIDEO_TUNER 6 select VIDEO_TUNER
7 select VIDEO_TVEEPROM 7 select VIDEO_TVEEPROM
8 select VIDEO_IR 8 select IR_CORE
9 select VIDEOBUF_DVB 9 select VIDEOBUF_DVB
10 select VIDEOBUF_DMA_SG 10 select VIDEOBUF_DMA_SG
11 select VIDEO_CX25840 11 select VIDEO_CX25840
diff --git a/drivers/media/video/cx23885/Makefile b/drivers/media/video/cx23885/Makefile
index 5787ae24363..e2ee95f660d 100644
--- a/drivers/media/video/cx23885/Makefile
+++ b/drivers/media/video/cx23885/Makefile
@@ -1,7 +1,8 @@
1cx23885-objs := cx23885-cards.o cx23885-video.o cx23885-vbi.o \ 1cx23885-objs := cx23885-cards.o cx23885-video.o cx23885-vbi.o \
2 cx23885-core.o cx23885-i2c.o cx23885-dvb.o cx23885-417.o \ 2 cx23885-core.o cx23885-i2c.o cx23885-dvb.o cx23885-417.o \
3 cx23885-ioctl.o cx23885-ir.o cx23885-input.o cx23888-ir.o \ 3 cx23885-ioctl.o cx23885-ir.o cx23885-av.o cx23885-input.o \
4 netup-init.o cimax2.o netup-eeprom.o cx23885-f300.o 4 cx23888-ir.o netup-init.o cimax2.o netup-eeprom.o \
5 cx23885-f300.o
5 6
6obj-$(CONFIG_VIDEO_CX23885) += cx23885.o 7obj-$(CONFIG_VIDEO_CX23885) += cx23885.o
7 8
diff --git a/drivers/media/video/cx23885/cx23885-av.c b/drivers/media/video/cx23885/cx23885-av.c
new file mode 100644
index 00000000000..134ebddd860
--- /dev/null
+++ b/drivers/media/video/cx23885/cx23885-av.c
@@ -0,0 +1,35 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * AV device support routines - non-input, non-vl42_subdev routines
5 *
6 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24#include "cx23885.h"
25
26void cx23885_av_work_handler(struct work_struct *work)
27{
28 struct cx23885_dev *dev =
29 container_of(work, struct cx23885_dev, cx25840_work);
30 bool handled;
31
32 v4l2_subdev_call(dev->sd_cx25840, core, interrupt_service_routine,
33 PCI_MSK_AV_CORE, &handled);
34 cx23885_irq_enable(dev, PCI_MSK_AV_CORE);
35}
diff --git a/drivers/media/video/cx23885/cx23885-av.h b/drivers/media/video/cx23885/cx23885-av.h
new file mode 100644
index 00000000000..d2915c3e53a
--- /dev/null
+++ b/drivers/media/video/cx23885/cx23885-av.h
@@ -0,0 +1,27 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * AV device support routines - non-input, non-vl42_subdev routines
5 *
6 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24#ifndef _CX23885_AV_H_
25#define _CX23885_AV_H_
26void cx23885_av_work_handler(struct work_struct *work);
27#endif
diff --git a/drivers/media/video/cx23885/cx23885-cards.c b/drivers/media/video/cx23885/cx23885-cards.c
index 2014daedee8..e76ce8709af 100644
--- a/drivers/media/video/cx23885/cx23885-cards.c
+++ b/drivers/media/video/cx23885/cx23885-cards.c
@@ -30,6 +30,16 @@
30#include "netup-init.h" 30#include "netup-init.h"
31#include "cx23888-ir.h" 31#include "cx23888-ir.h"
32 32
33static unsigned int enable_885_ir;
34module_param(enable_885_ir, int, 0644);
35MODULE_PARM_DESC(enable_885_ir,
36 "Enable integrated IR controller for supported\n"
37 "\t\t CX2388[57] boards that are wired for it:\n"
38 "\t\t\tHVR-1250 (reported safe)\n"
39 "\t\t\tTeVii S470 (reported unsafe)\n"
40 "\t\t This can cause an interrupt storm with some cards.\n"
41 "\t\t Default: 0 [Disabled]");
42
33/* ------------------------------------------------------------------ */ 43/* ------------------------------------------------------------------ */
34/* board config info */ 44/* board config info */
35 45
@@ -626,6 +636,9 @@ static void hauppauge_eeprom(struct cx23885_dev *dev, u8 *eeprom_data)
626 case 79101: 636 case 79101:
627 /* WinTV-HVR1250 (PCIe, Retail, IR, half height, 637 /* WinTV-HVR1250 (PCIe, Retail, IR, half height,
628 ATSC and Basic analog */ 638 ATSC and Basic analog */
639 case 79501:
640 /* WinTV-HVR1250 (PCIe, No IR, half height,
641 ATSC [at least] and Basic analog) */
629 case 79561: 642 case 79561:
630 /* WinTV-HVR1250 (PCIe, OEM, No IR, half height, 643 /* WinTV-HVR1250 (PCIe, OEM, No IR, half height,
631 ATSC and Basic analog */ 644 ATSC and Basic analog */
@@ -959,9 +972,37 @@ void cx23885_gpio_setup(struct cx23885_dev *dev)
959 972
960int cx23885_ir_init(struct cx23885_dev *dev) 973int cx23885_ir_init(struct cx23885_dev *dev)
961{ 974{
975 static struct v4l2_subdev_io_pin_config ir_rxtx_pin_cfg[] = {
976 {
977 .flags = V4L2_SUBDEV_IO_PIN_INPUT,
978 .pin = CX23885_PIN_IR_RX_GPIO19,
979 .function = CX23885_PAD_IR_RX,
980 .value = 0,
981 .strength = CX25840_PIN_DRIVE_MEDIUM,
982 }, {
983 .flags = V4L2_SUBDEV_IO_PIN_OUTPUT,
984 .pin = CX23885_PIN_IR_TX_GPIO20,
985 .function = CX23885_PAD_IR_TX,
986 .value = 0,
987 .strength = CX25840_PIN_DRIVE_MEDIUM,
988 }
989 };
990 const size_t ir_rxtx_pin_cfg_count = ARRAY_SIZE(ir_rxtx_pin_cfg);
991
992 static struct v4l2_subdev_io_pin_config ir_rx_pin_cfg[] = {
993 {
994 .flags = V4L2_SUBDEV_IO_PIN_INPUT,
995 .pin = CX23885_PIN_IR_RX_GPIO19,
996 .function = CX23885_PAD_IR_RX,
997 .value = 0,
998 .strength = CX25840_PIN_DRIVE_MEDIUM,
999 }
1000 };
1001 const size_t ir_rx_pin_cfg_count = ARRAY_SIZE(ir_rx_pin_cfg);
1002
1003 struct v4l2_subdev_ir_parameters params;
962 int ret = 0; 1004 int ret = 0;
963 switch (dev->board) { 1005 switch (dev->board) {
964 case CX23885_BOARD_HAUPPAUGE_HVR1250:
965 case CX23885_BOARD_HAUPPAUGE_HVR1500: 1006 case CX23885_BOARD_HAUPPAUGE_HVR1500:
966 case CX23885_BOARD_HAUPPAUGE_HVR1500Q: 1007 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
967 case CX23885_BOARD_HAUPPAUGE_HVR1800: 1008 case CX23885_BOARD_HAUPPAUGE_HVR1800:
@@ -979,7 +1020,41 @@ int cx23885_ir_init(struct cx23885_dev *dev)
979 if (ret) 1020 if (ret)
980 break; 1021 break;
981 dev->sd_ir = cx23885_find_hw(dev, CX23885_HW_888_IR); 1022 dev->sd_ir = cx23885_find_hw(dev, CX23885_HW_888_IR);
982 dev->pci_irqmask |= PCI_MSK_IR; 1023 v4l2_subdev_call(dev->sd_cx25840, core, s_io_pin_config,
1024 ir_rxtx_pin_cfg_count, ir_rxtx_pin_cfg);
1025 /*
1026 * For these boards we need to invert the Tx output via the
1027 * IR controller to have the LED off while idle
1028 */
1029 v4l2_subdev_call(dev->sd_ir, ir, tx_g_parameters, &params);
1030 params.enable = false;
1031 params.shutdown = false;
1032 params.invert_level = true;
1033 v4l2_subdev_call(dev->sd_ir, ir, tx_s_parameters, &params);
1034 params.shutdown = true;
1035 v4l2_subdev_call(dev->sd_ir, ir, tx_s_parameters, &params);
1036 break;
1037 case CX23885_BOARD_TEVII_S470:
1038 if (!enable_885_ir)
1039 break;
1040 dev->sd_ir = cx23885_find_hw(dev, CX23885_HW_AV_CORE);
1041 if (dev->sd_ir == NULL) {
1042 ret = -ENODEV;
1043 break;
1044 }
1045 v4l2_subdev_call(dev->sd_cx25840, core, s_io_pin_config,
1046 ir_rx_pin_cfg_count, ir_rx_pin_cfg);
1047 break;
1048 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1049 if (!enable_885_ir)
1050 break;
1051 dev->sd_ir = cx23885_find_hw(dev, CX23885_HW_AV_CORE);
1052 if (dev->sd_ir == NULL) {
1053 ret = -ENODEV;
1054 break;
1055 }
1056 v4l2_subdev_call(dev->sd_cx25840, core, s_io_pin_config,
1057 ir_rxtx_pin_cfg_count, ir_rxtx_pin_cfg);
983 break; 1058 break;
984 case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP: 1059 case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP:
985 request_module("ir-kbd-i2c"); 1060 request_module("ir-kbd-i2c");
@@ -994,11 +1069,16 @@ void cx23885_ir_fini(struct cx23885_dev *dev)
994 switch (dev->board) { 1069 switch (dev->board) {
995 case CX23885_BOARD_HAUPPAUGE_HVR1850: 1070 case CX23885_BOARD_HAUPPAUGE_HVR1850:
996 case CX23885_BOARD_HAUPPAUGE_HVR1290: 1071 case CX23885_BOARD_HAUPPAUGE_HVR1290:
997 dev->pci_irqmask &= ~PCI_MSK_IR; 1072 cx23885_irq_remove(dev, PCI_MSK_IR);
998 cx_clear(PCI_INT_MSK, PCI_MSK_IR);
999 cx23888_ir_remove(dev); 1073 cx23888_ir_remove(dev);
1000 dev->sd_ir = NULL; 1074 dev->sd_ir = NULL;
1001 break; 1075 break;
1076 case CX23885_BOARD_TEVII_S470:
1077 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1078 cx23885_irq_remove(dev, PCI_MSK_AV_CORE);
1079 /* sd_ir is a duplicate pointer to the AV Core, just clear it */
1080 dev->sd_ir = NULL;
1081 break;
1002 } 1082 }
1003} 1083}
1004 1084
@@ -1007,8 +1087,13 @@ void cx23885_ir_pci_int_enable(struct cx23885_dev *dev)
1007 switch (dev->board) { 1087 switch (dev->board) {
1008 case CX23885_BOARD_HAUPPAUGE_HVR1850: 1088 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1009 case CX23885_BOARD_HAUPPAUGE_HVR1290: 1089 case CX23885_BOARD_HAUPPAUGE_HVR1290:
1010 if (dev->sd_ir && (dev->pci_irqmask & PCI_MSK_IR)) 1090 if (dev->sd_ir)
1011 cx_set(PCI_INT_MSK, PCI_MSK_IR); 1091 cx23885_irq_add_enable(dev, PCI_MSK_IR);
1092 break;
1093 case CX23885_BOARD_TEVII_S470:
1094 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1095 if (dev->sd_ir)
1096 cx23885_irq_add_enable(dev, PCI_MSK_AV_CORE);
1012 break; 1097 break;
1013 } 1098 }
1014} 1099}
@@ -1028,6 +1113,13 @@ void cx23885_card_setup(struct cx23885_dev *dev)
1028 1113
1029 switch (dev->board) { 1114 switch (dev->board) {
1030 case CX23885_BOARD_HAUPPAUGE_HVR1250: 1115 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1116 if (dev->i2c_bus[0].i2c_rc == 0) {
1117 if (eeprom[0x80] != 0x84)
1118 hauppauge_eeprom(dev, eeprom+0xc0);
1119 else
1120 hauppauge_eeprom(dev, eeprom+0x80);
1121 }
1122 break;
1031 case CX23885_BOARD_HAUPPAUGE_HVR1500: 1123 case CX23885_BOARD_HAUPPAUGE_HVR1500:
1032 case CX23885_BOARD_HAUPPAUGE_HVR1500Q: 1124 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
1033 case CX23885_BOARD_HAUPPAUGE_HVR1400: 1125 case CX23885_BOARD_HAUPPAUGE_HVR1400:
@@ -1136,6 +1228,11 @@ void cx23885_card_setup(struct cx23885_dev *dev)
1136 * loaded, ensure this happens. 1228 * loaded, ensure this happens.
1137 */ 1229 */
1138 switch (dev->board) { 1230 switch (dev->board) {
1231 case CX23885_BOARD_TEVII_S470:
1232 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1233 /* Currently only enabled for the integrated IR controller */
1234 if (!enable_885_ir)
1235 break;
1139 case CX23885_BOARD_HAUPPAUGE_HVR1800: 1236 case CX23885_BOARD_HAUPPAUGE_HVR1800:
1140 case CX23885_BOARD_HAUPPAUGE_HVR1800lp: 1237 case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
1141 case CX23885_BOARD_HAUPPAUGE_HVR1700: 1238 case CX23885_BOARD_HAUPPAUGE_HVR1700:
@@ -1151,7 +1248,10 @@ void cx23885_card_setup(struct cx23885_dev *dev)
1151 dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev, 1248 dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1152 &dev->i2c_bus[2].i2c_adap, 1249 &dev->i2c_bus[2].i2c_adap,
1153 "cx25840", "cx25840", 0x88 >> 1, NULL); 1250 "cx25840", "cx25840", 0x88 >> 1, NULL);
1154 v4l2_subdev_call(dev->sd_cx25840, core, load_fw); 1251 if (dev->sd_cx25840) {
1252 dev->sd_cx25840->grp_id = CX23885_HW_AV_CORE;
1253 v4l2_subdev_call(dev->sd_cx25840, core, load_fw);
1254 }
1155 break; 1255 break;
1156 } 1256 }
1157 1257
diff --git a/drivers/media/video/cx23885/cx23885-core.c b/drivers/media/video/cx23885/cx23885-core.c
index ff76f64edac..f6b62e7398a 100644
--- a/drivers/media/video/cx23885/cx23885-core.c
+++ b/drivers/media/video/cx23885/cx23885-core.c
@@ -34,6 +34,7 @@
34#include "cimax2.h" 34#include "cimax2.h"
35#include "cx23888-ir.h" 35#include "cx23888-ir.h"
36#include "cx23885-ir.h" 36#include "cx23885-ir.h"
37#include "cx23885-av.h"
37#include "cx23885-input.h" 38#include "cx23885-input.h"
38 39
39MODULE_DESCRIPTION("Driver for cx23885 based TV cards"); 40MODULE_DESCRIPTION("Driver for cx23885 based TV cards");
@@ -299,6 +300,83 @@ static struct sram_channel cx23887_sram_channels[] = {
299 }, 300 },
300}; 301};
301 302
303void cx23885_irq_add(struct cx23885_dev *dev, u32 mask)
304{
305 unsigned long flags;
306 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
307
308 dev->pci_irqmask |= mask;
309
310 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
311}
312
313void cx23885_irq_add_enable(struct cx23885_dev *dev, u32 mask)
314{
315 unsigned long flags;
316 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
317
318 dev->pci_irqmask |= mask;
319 cx_set(PCI_INT_MSK, mask);
320
321 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
322}
323
324void cx23885_irq_enable(struct cx23885_dev *dev, u32 mask)
325{
326 u32 v;
327 unsigned long flags;
328 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
329
330 v = mask & dev->pci_irqmask;
331 if (v)
332 cx_set(PCI_INT_MSK, v);
333
334 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
335}
336
337static inline void cx23885_irq_enable_all(struct cx23885_dev *dev)
338{
339 cx23885_irq_enable(dev, 0xffffffff);
340}
341
342void cx23885_irq_disable(struct cx23885_dev *dev, u32 mask)
343{
344 unsigned long flags;
345 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
346
347 cx_clear(PCI_INT_MSK, mask);
348
349 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
350}
351
352static inline void cx23885_irq_disable_all(struct cx23885_dev *dev)
353{
354 cx23885_irq_disable(dev, 0xffffffff);
355}
356
357void cx23885_irq_remove(struct cx23885_dev *dev, u32 mask)
358{
359 unsigned long flags;
360 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
361
362 dev->pci_irqmask &= ~mask;
363 cx_clear(PCI_INT_MSK, mask);
364
365 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
366}
367
368static u32 cx23885_irq_get_mask(struct cx23885_dev *dev)
369{
370 u32 v;
371 unsigned long flags;
372 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
373
374 v = cx_read(PCI_INT_MSK);
375
376 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
377 return v;
378}
379
302static int cx23885_risc_decode(u32 risc) 380static int cx23885_risc_decode(u32 risc)
303{ 381{
304 static char *instr[16] = { 382 static char *instr[16] = {
@@ -548,7 +626,7 @@ static void cx23885_shutdown(struct cx23885_dev *dev)
548 cx_write(UART_CTL, 0); 626 cx_write(UART_CTL, 0);
549 627
550 /* Disable Interrupts */ 628 /* Disable Interrupts */
551 cx_write(PCI_INT_MSK, 0); 629 cx23885_irq_disable_all(dev);
552 cx_write(VID_A_INT_MSK, 0); 630 cx_write(VID_A_INT_MSK, 0);
553 cx_write(VID_B_INT_MSK, 0); 631 cx_write(VID_B_INT_MSK, 0);
554 cx_write(VID_C_INT_MSK, 0); 632 cx_write(VID_C_INT_MSK, 0);
@@ -774,6 +852,8 @@ static int cx23885_dev_setup(struct cx23885_dev *dev)
774{ 852{
775 int i; 853 int i;
776 854
855 spin_lock_init(&dev->pci_irqmask_lock);
856
777 mutex_init(&dev->lock); 857 mutex_init(&dev->lock);
778 mutex_init(&dev->gpio_lock); 858 mutex_init(&dev->gpio_lock);
779 859
@@ -820,9 +900,9 @@ static int cx23885_dev_setup(struct cx23885_dev *dev)
820 900
821 dev->pci_bus = dev->pci->bus->number; 901 dev->pci_bus = dev->pci->bus->number;
822 dev->pci_slot = PCI_SLOT(dev->pci->devfn); 902 dev->pci_slot = PCI_SLOT(dev->pci->devfn);
823 dev->pci_irqmask = 0x001f00; 903 cx23885_irq_add(dev, 0x001f00);
824 if (cx23885_boards[dev->board].cimax > 0) 904 if (cx23885_boards[dev->board].cimax > 0)
825 dev->pci_irqmask |= 0x01800000; /* for CiMaxes */ 905 cx23885_irq_add(dev, 0x01800000); /* for CiMaxes */
826 906
827 /* External Master 1 Bus */ 907 /* External Master 1 Bus */
828 dev->i2c_bus[0].nr = 0; 908 dev->i2c_bus[0].nr = 0;
@@ -1156,7 +1236,7 @@ static void cx23885_tsport_reg_dump(struct cx23885_tsport *port)
1156 dprintk(1, "%s() DEV_CNTRL2 0x%08X\n", __func__, 1236 dprintk(1, "%s() DEV_CNTRL2 0x%08X\n", __func__,
1157 cx_read(DEV_CNTRL2)); 1237 cx_read(DEV_CNTRL2));
1158 dprintk(1, "%s() PCI_INT_MSK 0x%08X\n", __func__, 1238 dprintk(1, "%s() PCI_INT_MSK 0x%08X\n", __func__,
1159 cx_read(PCI_INT_MSK)); 1239 cx23885_irq_get_mask(dev));
1160 dprintk(1, "%s() AUD_INT_INT_MSK 0x%08X\n", __func__, 1240 dprintk(1, "%s() AUD_INT_INT_MSK 0x%08X\n", __func__,
1161 cx_read(AUDIO_INT_INT_MSK)); 1241 cx_read(AUDIO_INT_INT_MSK));
1162 dprintk(1, "%s() AUD_INT_DMA_CTL 0x%08X\n", __func__, 1242 dprintk(1, "%s() AUD_INT_DMA_CTL 0x%08X\n", __func__,
@@ -1292,7 +1372,8 @@ static int cx23885_start_dma(struct cx23885_tsport *port,
1292 dprintk(1, "%s() enabling TS int's and DMA\n", __func__); 1372 dprintk(1, "%s() enabling TS int's and DMA\n", __func__);
1293 cx_set(port->reg_ts_int_msk, port->ts_int_msk_val); 1373 cx_set(port->reg_ts_int_msk, port->ts_int_msk_val);
1294 cx_set(port->reg_dma_ctl, port->dma_ctl_val); 1374 cx_set(port->reg_dma_ctl, port->dma_ctl_val);
1295 cx_set(PCI_INT_MSK, dev->pci_irqmask | port->pci_irqmask); 1375 cx23885_irq_add(dev, port->pci_irqmask);
1376 cx23885_irq_enable_all(dev);
1296 break; 1377 break;
1297 default: 1378 default:
1298 BUG(); 1379 BUG();
@@ -1650,10 +1731,10 @@ static irqreturn_t cx23885_irq(int irq, void *dev_id)
1650 u32 ts1_status, ts1_mask; 1731 u32 ts1_status, ts1_mask;
1651 u32 ts2_status, ts2_mask; 1732 u32 ts2_status, ts2_mask;
1652 int vida_count = 0, ts1_count = 0, ts2_count = 0, handled = 0; 1733 int vida_count = 0, ts1_count = 0, ts2_count = 0, handled = 0;
1653 bool ir_handled = false; 1734 bool subdev_handled;
1654 1735
1655 pci_status = cx_read(PCI_INT_STAT); 1736 pci_status = cx_read(PCI_INT_STAT);
1656 pci_mask = cx_read(PCI_INT_MSK); 1737 pci_mask = cx23885_irq_get_mask(dev);
1657 vida_status = cx_read(VID_A_INT_STAT); 1738 vida_status = cx_read(VID_A_INT_STAT);
1658 vida_mask = cx_read(VID_A_INT_MSK); 1739 vida_mask = cx_read(VID_A_INT_MSK);
1659 ts1_status = cx_read(VID_B_INT_STAT); 1740 ts1_status = cx_read(VID_B_INT_STAT);
@@ -1681,7 +1762,7 @@ static irqreturn_t cx23885_irq(int irq, void *dev_id)
1681 PCI_MSK_VID_C | PCI_MSK_VID_B | PCI_MSK_VID_A | 1762 PCI_MSK_VID_C | PCI_MSK_VID_B | PCI_MSK_VID_A |
1682 PCI_MSK_AUD_INT | PCI_MSK_AUD_EXT | 1763 PCI_MSK_AUD_INT | PCI_MSK_AUD_EXT |
1683 PCI_MSK_GPIO0 | PCI_MSK_GPIO1 | 1764 PCI_MSK_GPIO0 | PCI_MSK_GPIO1 |
1684 PCI_MSK_IR)) { 1765 PCI_MSK_AV_CORE | PCI_MSK_IR)) {
1685 1766
1686 if (pci_status & PCI_MSK_RISC_RD) 1767 if (pci_status & PCI_MSK_RISC_RD)
1687 dprintk(7, " (PCI_MSK_RISC_RD 0x%08x)\n", 1768 dprintk(7, " (PCI_MSK_RISC_RD 0x%08x)\n",
@@ -1731,6 +1812,10 @@ static irqreturn_t cx23885_irq(int irq, void *dev_id)
1731 dprintk(7, " (PCI_MSK_GPIO1 0x%08x)\n", 1812 dprintk(7, " (PCI_MSK_GPIO1 0x%08x)\n",
1732 PCI_MSK_GPIO1); 1813 PCI_MSK_GPIO1);
1733 1814
1815 if (pci_status & PCI_MSK_AV_CORE)
1816 dprintk(7, " (PCI_MSK_AV_CORE 0x%08x)\n",
1817 PCI_MSK_AV_CORE);
1818
1734 if (pci_status & PCI_MSK_IR) 1819 if (pci_status & PCI_MSK_IR)
1735 dprintk(7, " (PCI_MSK_IR 0x%08x)\n", 1820 dprintk(7, " (PCI_MSK_IR 0x%08x)\n",
1736 PCI_MSK_IR); 1821 PCI_MSK_IR);
@@ -1765,12 +1850,22 @@ static irqreturn_t cx23885_irq(int irq, void *dev_id)
1765 handled += cx23885_video_irq(dev, vida_status); 1850 handled += cx23885_video_irq(dev, vida_status);
1766 1851
1767 if (pci_status & PCI_MSK_IR) { 1852 if (pci_status & PCI_MSK_IR) {
1768 v4l2_subdev_call(dev->sd_ir, ir, interrupt_service_routine, 1853 subdev_handled = false;
1769 pci_status, &ir_handled); 1854 v4l2_subdev_call(dev->sd_ir, core, interrupt_service_routine,
1770 if (ir_handled) 1855 pci_status, &subdev_handled);
1856 if (subdev_handled)
1771 handled++; 1857 handled++;
1772 } 1858 }
1773 1859
1860 if ((pci_status & pci_mask) & PCI_MSK_AV_CORE) {
1861 cx23885_irq_disable(dev, PCI_MSK_AV_CORE);
1862 if (!schedule_work(&dev->cx25840_work))
1863 printk(KERN_ERR "%s: failed to set up deferred work for"
1864 " AV Core/IR interrupt. Interrupt is disabled"
1865 " and won't be re-enabled\n", dev->name);
1866 handled++;
1867 }
1868
1774 if (handled) 1869 if (handled)
1775 cx_write(PCI_INT_STAT, pci_status); 1870 cx_write(PCI_INT_STAT, pci_status);
1776out: 1871out:
@@ -1788,11 +1883,11 @@ static void cx23885_v4l2_dev_notify(struct v4l2_subdev *sd,
1788 dev = to_cx23885(sd->v4l2_dev); 1883 dev = to_cx23885(sd->v4l2_dev);
1789 1884
1790 switch (notification) { 1885 switch (notification) {
1791 case V4L2_SUBDEV_IR_RX_NOTIFY: /* Called in an IRQ context */ 1886 case V4L2_SUBDEV_IR_RX_NOTIFY: /* Possibly called in an IRQ context */
1792 if (sd == dev->sd_ir) 1887 if (sd == dev->sd_ir)
1793 cx23885_ir_rx_v4l2_dev_notify(sd, *(u32 *)arg); 1888 cx23885_ir_rx_v4l2_dev_notify(sd, *(u32 *)arg);
1794 break; 1889 break;
1795 case V4L2_SUBDEV_IR_TX_NOTIFY: /* Called in an IRQ context */ 1890 case V4L2_SUBDEV_IR_TX_NOTIFY: /* Possibly called in an IRQ context */
1796 if (sd == dev->sd_ir) 1891 if (sd == dev->sd_ir)
1797 cx23885_ir_tx_v4l2_dev_notify(sd, *(u32 *)arg); 1892 cx23885_ir_tx_v4l2_dev_notify(sd, *(u32 *)arg);
1798 break; 1893 break;
@@ -1801,6 +1896,7 @@ static void cx23885_v4l2_dev_notify(struct v4l2_subdev *sd,
1801 1896
1802static void cx23885_v4l2_dev_notify_init(struct cx23885_dev *dev) 1897static void cx23885_v4l2_dev_notify_init(struct cx23885_dev *dev)
1803{ 1898{
1899 INIT_WORK(&dev->cx25840_work, cx23885_av_work_handler);
1804 INIT_WORK(&dev->ir_rx_work, cx23885_ir_rx_work_handler); 1900 INIT_WORK(&dev->ir_rx_work, cx23885_ir_rx_work_handler);
1805 INIT_WORK(&dev->ir_tx_work, cx23885_ir_tx_work_handler); 1901 INIT_WORK(&dev->ir_tx_work, cx23885_ir_tx_work_handler);
1806 dev->v4l2_dev.notify = cx23885_v4l2_dev_notify; 1902 dev->v4l2_dev.notify = cx23885_v4l2_dev_notify;
@@ -1967,7 +2063,7 @@ static int __devinit cx23885_initdev(struct pci_dev *pci_dev,
1967 2063
1968 switch (dev->board) { 2064 switch (dev->board) {
1969 case CX23885_BOARD_NETUP_DUAL_DVBS2_CI: 2065 case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
1970 cx_set(PCI_INT_MSK, 0x01800000); /* for NetUP */ 2066 cx23885_irq_add_enable(dev, 0x01800000); /* for NetUP */
1971 break; 2067 break;
1972 } 2068 }
1973 2069
diff --git a/drivers/media/video/cx23885/cx23885-i2c.c b/drivers/media/video/cx23885/cx23885-i2c.c
index d4746e06451..1a391486e55 100644
--- a/drivers/media/video/cx23885/cx23885-i2c.c
+++ b/drivers/media/video/cx23885/cx23885-i2c.c
@@ -99,7 +99,7 @@ static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
99 if (!i2c_wait_done(i2c_adap)) 99 if (!i2c_wait_done(i2c_adap))
100 return -EIO; 100 return -EIO;
101 if (!i2c_slave_did_ack(i2c_adap)) 101 if (!i2c_slave_did_ack(i2c_adap))
102 return -EIO; 102 return -ENXIO;
103 103
104 dprintk(1, "%s() returns 0\n", __func__); 104 dprintk(1, "%s() returns 0\n", __func__);
105 return 0; 105 return 0;
@@ -120,11 +120,12 @@ static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
120 cx_write(bus->reg_wdata, wdata); 120 cx_write(bus->reg_wdata, wdata);
121 cx_write(bus->reg_ctrl, ctrl); 121 cx_write(bus->reg_ctrl, ctrl);
122 122
123 retval = i2c_wait_done(i2c_adap); 123 if (!i2c_wait_done(i2c_adap))
124 if (retval < 0)
125 goto err;
126 if (retval == 0)
127 goto eio; 124 goto eio;
125 if (!i2c_slave_did_ack(i2c_adap)) {
126 retval = -ENXIO;
127 goto err;
128 }
128 if (i2c_debug) { 129 if (i2c_debug) {
129 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]); 130 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
130 if (!(ctrl & I2C_NOSTOP)) 131 if (!(ctrl & I2C_NOSTOP))
@@ -145,10 +146,7 @@ static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
145 cx_write(bus->reg_wdata, wdata); 146 cx_write(bus->reg_wdata, wdata);
146 cx_write(bus->reg_ctrl, ctrl); 147 cx_write(bus->reg_ctrl, ctrl);
147 148
148 retval = i2c_wait_done(i2c_adap); 149 if (!i2c_wait_done(i2c_adap))
149 if (retval < 0)
150 goto err;
151 if (retval == 0)
152 goto eio; 150 goto eio;
153 if (i2c_debug) { 151 if (i2c_debug) {
154 dprintk(1, " %02x", msg->buf[cnt]); 152 dprintk(1, " %02x", msg->buf[cnt]);
@@ -185,7 +183,7 @@ static int i2c_readbytes(struct i2c_adapter *i2c_adap,
185 if (!i2c_wait_done(i2c_adap)) 183 if (!i2c_wait_done(i2c_adap))
186 return -EIO; 184 return -EIO;
187 if (!i2c_slave_did_ack(i2c_adap)) 185 if (!i2c_slave_did_ack(i2c_adap))
188 return -EIO; 186 return -ENXIO;
189 187
190 188
191 dprintk(1, "%s() returns 0\n", __func__); 189 dprintk(1, "%s() returns 0\n", __func__);
@@ -209,11 +207,12 @@ static int i2c_readbytes(struct i2c_adapter *i2c_adap,
209 cx_write(bus->reg_addr, msg->addr << 25); 207 cx_write(bus->reg_addr, msg->addr << 25);
210 cx_write(bus->reg_ctrl, ctrl); 208 cx_write(bus->reg_ctrl, ctrl);
211 209
212 retval = i2c_wait_done(i2c_adap); 210 if (!i2c_wait_done(i2c_adap))
213 if (retval < 0)
214 goto err;
215 if (retval == 0)
216 goto eio; 211 goto eio;
212 if (cnt == 0 && !i2c_slave_did_ack(i2c_adap)) {
213 retval = -ENXIO;
214 goto err;
215 }
217 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff; 216 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
218 if (i2c_debug) { 217 if (i2c_debug) {
219 dprintk(1, " %02x", msg->buf[cnt]); 218 dprintk(1, " %02x", msg->buf[cnt]);
diff --git a/drivers/media/video/cx23885/cx23885-input.c b/drivers/media/video/cx23885/cx23885-input.c
index d0b1613ede2..bb61870b8d6 100644
--- a/drivers/media/video/cx23885/cx23885-input.c
+++ b/drivers/media/video/cx23885/cx23885-input.c
@@ -44,40 +44,26 @@
44 44
45#define MODULE_NAME "cx23885" 45#define MODULE_NAME "cx23885"
46 46
47static void convert_measurement(u32 x, struct ir_raw_event *y)
48{
49 if (x == V4L2_SUBDEV_IR_PULSE_RX_SEQ_END) {
50 y->pulse = false;
51 y->duration = V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS;
52 return;
53 }
54
55 y->pulse = (x & V4L2_SUBDEV_IR_PULSE_LEVEL_MASK) ? true : false;
56 y->duration = x & V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS;
57}
58
59static void cx23885_input_process_measurements(struct cx23885_dev *dev, 47static void cx23885_input_process_measurements(struct cx23885_dev *dev,
60 bool overrun) 48 bool overrun)
61{ 49{
62 struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir; 50 struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir;
63 struct ir_raw_event kernel_ir_event;
64 51
65 u32 sd_ir_data[64];
66 ssize_t num; 52 ssize_t num;
67 int count, i; 53 int count, i;
68 bool handle = false; 54 bool handle = false;
55 struct ir_raw_event ir_core_event[64];
69 56
70 do { 57 do {
71 num = 0; 58 num = 0;
72 v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) sd_ir_data, 59 v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ir_core_event,
73 sizeof(sd_ir_data), &num); 60 sizeof(ir_core_event), &num);
74 61
75 count = num / sizeof(u32); 62 count = num / sizeof(struct ir_raw_event);
76 63
77 for (i = 0; i < count; i++) { 64 for (i = 0; i < count; i++) {
78 convert_measurement(sd_ir_data[i], &kernel_ir_event);
79 ir_raw_event_store(kernel_ir->inp_dev, 65 ir_raw_event_store(kernel_ir->inp_dev,
80 &kernel_ir_event); 66 &ir_core_event[i]);
81 handle = true; 67 handle = true;
82 } 68 }
83 } while (num != 0); 69 } while (num != 0);
@@ -99,8 +85,10 @@ void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events)
99 switch (dev->board) { 85 switch (dev->board) {
100 case CX23885_BOARD_HAUPPAUGE_HVR1850: 86 case CX23885_BOARD_HAUPPAUGE_HVR1850:
101 case CX23885_BOARD_HAUPPAUGE_HVR1290: 87 case CX23885_BOARD_HAUPPAUGE_HVR1290:
88 case CX23885_BOARD_TEVII_S470:
89 case CX23885_BOARD_HAUPPAUGE_HVR1250:
102 /* 90 /*
103 * The only board we handle right now. However other boards 91 * The only boards we handle right now. However other boards
104 * using the CX2388x integrated IR controller should be similar 92 * using the CX2388x integrated IR controller should be similar
105 */ 93 */
106 break; 94 break;
@@ -148,6 +136,7 @@ static int cx23885_input_ir_start(struct cx23885_dev *dev)
148 switch (dev->board) { 136 switch (dev->board) {
149 case CX23885_BOARD_HAUPPAUGE_HVR1850: 137 case CX23885_BOARD_HAUPPAUGE_HVR1850:
150 case CX23885_BOARD_HAUPPAUGE_HVR1290: 138 case CX23885_BOARD_HAUPPAUGE_HVR1290:
139 case CX23885_BOARD_HAUPPAUGE_HVR1250:
151 /* 140 /*
152 * The IR controller on this board only returns pulse widths. 141 * The IR controller on this board only returns pulse widths.
153 * Any other mode setting will fail to set up the device. 142 * Any other mode setting will fail to set up the device.
@@ -170,7 +159,38 @@ static int cx23885_input_ir_start(struct cx23885_dev *dev)
170 * mark is received as low logic level; 159 * mark is received as low logic level;
171 * falling edges are detected as rising edges; etc. 160 * falling edges are detected as rising edges; etc.
172 */ 161 */
173 params.invert = true; 162 params.invert_level = true;
163 break;
164 case CX23885_BOARD_TEVII_S470:
165 /*
166 * The IR controller on this board only returns pulse widths.
167 * Any other mode setting will fail to set up the device.
168 */
169 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
170 params.enable = true;
171 params.interrupt_enable = true;
172 params.shutdown = false;
173
174 /* Setup for a standard NEC protocol */
175 params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */
176 params.carrier_range_lower = 33000; /* Hz */
177 params.carrier_range_upper = 43000; /* Hz */
178 params.duty_cycle = 33; /* percent, 33 percent for NEC */
179
180 /*
181 * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units
182 * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns
183 */
184 params.max_pulse_width = 12378022; /* ns */
185
186 /*
187 * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit
188 * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns
189 */
190 params.noise_filter_min_width = 351648; /* ns */
191
192 params.modulation = false;
193 params.invert_level = true;
174 break; 194 break;
175 } 195 }
176 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params); 196 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
@@ -244,12 +264,20 @@ int cx23885_input_init(struct cx23885_dev *dev)
244 switch (dev->board) { 264 switch (dev->board) {
245 case CX23885_BOARD_HAUPPAUGE_HVR1850: 265 case CX23885_BOARD_HAUPPAUGE_HVR1850:
246 case CX23885_BOARD_HAUPPAUGE_HVR1290: 266 case CX23885_BOARD_HAUPPAUGE_HVR1290:
247 /* Integrated CX23888 IR controller */ 267 case CX23885_BOARD_HAUPPAUGE_HVR1250:
268 /* Integrated CX2388[58] IR controller */
248 driver_type = RC_DRIVER_IR_RAW; 269 driver_type = RC_DRIVER_IR_RAW;
249 allowed_protos = IR_TYPE_ALL; 270 allowed_protos = IR_TYPE_ALL;
250 /* The grey Hauppauge RC-5 remote */ 271 /* The grey Hauppauge RC-5 remote */
251 rc_map = RC_MAP_RC5_HAUPPAUGE_NEW; 272 rc_map = RC_MAP_RC5_HAUPPAUGE_NEW;
252 break; 273 break;
274 case CX23885_BOARD_TEVII_S470:
275 /* Integrated CX23885 IR controller */
276 driver_type = RC_DRIVER_IR_RAW;
277 allowed_protos = IR_TYPE_ALL;
278 /* A guess at the remote */
279 rc_map = RC_MAP_TEVII_NEC;
280 break;
253 default: 281 default:
254 return -ENODEV; 282 return -ENODEV;
255 } 283 }
diff --git a/drivers/media/video/cx23885/cx23885-ir.c b/drivers/media/video/cx23885/cx23885-ir.c
index 6ceabd4fba0..7125247dd25 100644
--- a/drivers/media/video/cx23885/cx23885-ir.c
+++ b/drivers/media/video/cx23885/cx23885-ir.c
@@ -72,7 +72,7 @@ void cx23885_ir_tx_work_handler(struct work_struct *work)
72 72
73} 73}
74 74
75/* Called in an IRQ context */ 75/* Possibly called in an IRQ context */
76void cx23885_ir_rx_v4l2_dev_notify(struct v4l2_subdev *sd, u32 events) 76void cx23885_ir_rx_v4l2_dev_notify(struct v4l2_subdev *sd, u32 events)
77{ 77{
78 struct cx23885_dev *dev = to_cx23885(sd->v4l2_dev); 78 struct cx23885_dev *dev = to_cx23885(sd->v4l2_dev);
@@ -86,10 +86,18 @@ void cx23885_ir_rx_v4l2_dev_notify(struct v4l2_subdev *sd, u32 events)
86 set_bit(CX23885_IR_RX_HW_FIFO_OVERRUN, notifications); 86 set_bit(CX23885_IR_RX_HW_FIFO_OVERRUN, notifications);
87 if (events & V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN) 87 if (events & V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN)
88 set_bit(CX23885_IR_RX_SW_FIFO_OVERRUN, notifications); 88 set_bit(CX23885_IR_RX_SW_FIFO_OVERRUN, notifications);
89 schedule_work(&dev->ir_rx_work); 89
90 /*
91 * For the integrated AV core, we are already in a workqueue context.
92 * For the CX23888 integrated IR, we are in an interrupt context.
93 */
94 if (sd == dev->sd_cx25840)
95 cx23885_ir_rx_work_handler(&dev->ir_rx_work);
96 else
97 schedule_work(&dev->ir_rx_work);
90} 98}
91 99
92/* Called in an IRQ context */ 100/* Possibly called in an IRQ context */
93void cx23885_ir_tx_v4l2_dev_notify(struct v4l2_subdev *sd, u32 events) 101void cx23885_ir_tx_v4l2_dev_notify(struct v4l2_subdev *sd, u32 events)
94{ 102{
95 struct cx23885_dev *dev = to_cx23885(sd->v4l2_dev); 103 struct cx23885_dev *dev = to_cx23885(sd->v4l2_dev);
@@ -97,5 +105,13 @@ void cx23885_ir_tx_v4l2_dev_notify(struct v4l2_subdev *sd, u32 events)
97 105
98 if (events & V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ) 106 if (events & V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ)
99 set_bit(CX23885_IR_TX_FIFO_SERVICE_REQ, notifications); 107 set_bit(CX23885_IR_TX_FIFO_SERVICE_REQ, notifications);
100 schedule_work(&dev->ir_tx_work); 108
109 /*
110 * For the integrated AV core, we are already in a workqueue context.
111 * For the CX23888 integrated IR, we are in an interrupt context.
112 */
113 if (sd == dev->sd_cx25840)
114 cx23885_ir_tx_work_handler(&dev->ir_tx_work);
115 else
116 schedule_work(&dev->ir_tx_work);
101} 117}
diff --git a/drivers/media/video/cx23885/cx23885-reg.h b/drivers/media/video/cx23885/cx23885-reg.h
index c0bc9a06895..a28772db11f 100644
--- a/drivers/media/video/cx23885/cx23885-reg.h
+++ b/drivers/media/video/cx23885/cx23885-reg.h
@@ -213,6 +213,7 @@ Channel manager Data Structure entry = 20 DWORD
213#define DEV_CNTRL2 0x00040000 213#define DEV_CNTRL2 0x00040000
214 214
215#define PCI_MSK_IR (1 << 28) 215#define PCI_MSK_IR (1 << 28)
216#define PCI_MSK_AV_CORE (1 << 27)
216#define PCI_MSK_GPIO1 (1 << 24) 217#define PCI_MSK_GPIO1 (1 << 24)
217#define PCI_MSK_GPIO0 (1 << 23) 218#define PCI_MSK_GPIO0 (1 << 23)
218#define PCI_MSK_APB_DMA (1 << 12) 219#define PCI_MSK_APB_DMA (1 << 12)
diff --git a/drivers/media/video/cx23885/cx23885-vbi.c b/drivers/media/video/cx23885/cx23885-vbi.c
index 708a8c766d1..c0b60382ad1 100644
--- a/drivers/media/video/cx23885/cx23885-vbi.c
+++ b/drivers/media/video/cx23885/cx23885-vbi.c
@@ -74,7 +74,7 @@ static int cx23885_start_vbi_dma(struct cx23885_dev *dev,
74 q->count = 1; 74 q->count = 1;
75 75
76 /* enable irqs */ 76 /* enable irqs */
77 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01); 77 cx23885_irq_add_enable(dev, 0x01);
78 cx_set(VID_A_INT_MSK, 0x000022); 78 cx_set(VID_A_INT_MSK, 0x000022);
79 79
80 /* start dma */ 80 /* start dma */
diff --git a/drivers/media/video/cx23885/cx23885-video.c b/drivers/media/video/cx23885/cx23885-video.c
index 4e44dcda387..da66e5f8d91 100644
--- a/drivers/media/video/cx23885/cx23885-video.c
+++ b/drivers/media/video/cx23885/cx23885-video.c
@@ -441,7 +441,7 @@ static int cx23885_start_video_dma(struct cx23885_dev *dev,
441 q->count = 1; 441 q->count = 1;
442 442
443 /* enable irq */ 443 /* enable irq */
444 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01); 444 cx23885_irq_add_enable(dev, 0x01);
445 cx_set(VID_A_INT_MSK, 0x000011); 445 cx_set(VID_A_INT_MSK, 0x000011);
446 446
447 /* start dma */ 447 /* start dma */
@@ -1205,6 +1205,21 @@ static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1205 return 0; 1205 return 0;
1206} 1206}
1207 1207
1208static int vidioc_log_status(struct file *file, void *priv)
1209{
1210 struct cx23885_fh *fh = priv;
1211 struct cx23885_dev *dev = fh->dev;
1212
1213 printk(KERN_INFO
1214 "%s/0: ============ START LOG STATUS ============\n",
1215 dev->name);
1216 call_all(dev, core, log_status);
1217 printk(KERN_INFO
1218 "%s/0: ============= END LOG STATUS =============\n",
1219 dev->name);
1220 return 0;
1221}
1222
1208static int vidioc_queryctrl(struct file *file, void *priv, 1223static int vidioc_queryctrl(struct file *file, void *priv,
1209 struct v4l2_queryctrl *qctrl) 1224 struct v4l2_queryctrl *qctrl)
1210{ 1225{
@@ -1410,6 +1425,7 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
1410 .vidioc_enum_input = vidioc_enum_input, 1425 .vidioc_enum_input = vidioc_enum_input,
1411 .vidioc_g_input = vidioc_g_input, 1426 .vidioc_g_input = vidioc_g_input,
1412 .vidioc_s_input = vidioc_s_input, 1427 .vidioc_s_input = vidioc_s_input,
1428 .vidioc_log_status = vidioc_log_status,
1413 .vidioc_queryctrl = vidioc_queryctrl, 1429 .vidioc_queryctrl = vidioc_queryctrl,
1414 .vidioc_g_ctrl = vidioc_g_ctrl, 1430 .vidioc_g_ctrl = vidioc_g_ctrl,
1415 .vidioc_s_ctrl = vidioc_s_ctrl, 1431 .vidioc_s_ctrl = vidioc_s_ctrl,
@@ -1449,7 +1465,7 @@ static const struct v4l2_file_operations radio_fops = {
1449void cx23885_video_unregister(struct cx23885_dev *dev) 1465void cx23885_video_unregister(struct cx23885_dev *dev)
1450{ 1466{
1451 dprintk(1, "%s()\n", __func__); 1467 dprintk(1, "%s()\n", __func__);
1452 cx_clear(PCI_INT_MSK, 1); 1468 cx23885_irq_remove(dev, 0x01);
1453 1469
1454 if (dev->video_dev) { 1470 if (dev->video_dev) {
1455 if (video_is_registered(dev->video_dev)) 1471 if (video_is_registered(dev->video_dev))
@@ -1486,7 +1502,8 @@ int cx23885_video_register(struct cx23885_dev *dev)
1486 VID_A_DMA_CTL, 0x11, 0x00); 1502 VID_A_DMA_CTL, 0x11, 0x00);
1487 1503
1488 /* Don't enable VBI yet */ 1504 /* Don't enable VBI yet */
1489 cx_set(PCI_INT_MSK, 1); 1505
1506 cx23885_irq_add_enable(dev, 0x01);
1490 1507
1491 if (TUNER_ABSENT != dev->tuner_type) { 1508 if (TUNER_ABSENT != dev->tuner_type) {
1492 struct v4l2_subdev *sd = NULL; 1509 struct v4l2_subdev *sd = NULL;
diff --git a/drivers/media/video/cx23885/cx23885.h b/drivers/media/video/cx23885/cx23885.h
index a33f2b71467..ed94b17dd8a 100644
--- a/drivers/media/video/cx23885/cx23885.h
+++ b/drivers/media/video/cx23885/cx23885.h
@@ -325,6 +325,7 @@ struct cx23885_dev {
325 u32 __iomem *lmmio; 325 u32 __iomem *lmmio;
326 u8 __iomem *bmmio; 326 u8 __iomem *bmmio;
327 int pci_irqmask; 327 int pci_irqmask;
328 spinlock_t pci_irqmask_lock; /* protects mask reg too */
328 int hwrevision; 329 int hwrevision;
329 330
330 /* This valud is board specific and is used to configure the 331 /* This valud is board specific and is used to configure the
@@ -365,6 +366,7 @@ struct cx23885_dev {
365 unsigned char radio_addr; 366 unsigned char radio_addr;
366 unsigned int has_radio; 367 unsigned int has_radio;
367 struct v4l2_subdev *sd_cx25840; 368 struct v4l2_subdev *sd_cx25840;
369 struct work_struct cx25840_work;
368 370
369 /* Infrared */ 371 /* Infrared */
370 struct v4l2_subdev *sd_ir; 372 struct v4l2_subdev *sd_ir;
@@ -403,7 +405,8 @@ static inline struct cx23885_dev *to_cx23885(struct v4l2_device *v4l2_dev)
403#define call_all(dev, o, f, args...) \ 405#define call_all(dev, o, f, args...) \
404 v4l2_device_call_all(&dev->v4l2_dev, 0, o, f, ##args) 406 v4l2_device_call_all(&dev->v4l2_dev, 0, o, f, ##args)
405 407
406#define CX23885_HW_888_IR (1 << 0) 408#define CX23885_HW_888_IR (1 << 0)
409#define CX23885_HW_AV_CORE (1 << 1)
407 410
408#define call_hw(dev, grpid, o, f, args...) \ 411#define call_hw(dev, grpid, o, f, args...) \
409 v4l2_device_call_all(&dev->v4l2_dev, grpid, o, f, ##args) 412 v4l2_device_call_all(&dev->v4l2_dev, grpid, o, f, ##args)
@@ -484,6 +487,10 @@ extern u32 cx23885_gpio_get(struct cx23885_dev *dev, u32 mask);
484extern void cx23885_gpio_enable(struct cx23885_dev *dev, u32 mask, 487extern void cx23885_gpio_enable(struct cx23885_dev *dev, u32 mask,
485 int asoutput); 488 int asoutput);
486 489
490extern void cx23885_irq_add_enable(struct cx23885_dev *dev, u32 mask);
491extern void cx23885_irq_enable(struct cx23885_dev *dev, u32 mask);
492extern void cx23885_irq_disable(struct cx23885_dev *dev, u32 mask);
493extern void cx23885_irq_remove(struct cx23885_dev *dev, u32 mask);
487 494
488/* ----------------------------------------------------------- */ 495/* ----------------------------------------------------------- */
489/* cx23885-cards.c */ 496/* cx23885-cards.c */
diff --git a/drivers/media/video/cx23885/cx23888-ir.c b/drivers/media/video/cx23885/cx23888-ir.c
index f63d378257a..2502a0a6709 100644
--- a/drivers/media/video/cx23885/cx23888-ir.c
+++ b/drivers/media/video/cx23885/cx23888-ir.c
@@ -26,6 +26,7 @@
26 26
27#include <media/v4l2-device.h> 27#include <media/v4l2-device.h>
28#include <media/v4l2-chip-ident.h> 28#include <media/v4l2-chip-ident.h>
29#include <media/ir-core.h>
29 30
30#include "cx23885.h" 31#include "cx23885.h"
31 32
@@ -60,6 +61,8 @@ MODULE_PARM_DESC(ir_888_debug, "enable debug messages [CX23888 IR controller]");
60#define CNTRL_CPL 0x00001000 61#define CNTRL_CPL 0x00001000
61#define CNTRL_LBM 0x00002000 62#define CNTRL_LBM 0x00002000
62#define CNTRL_R 0x00004000 63#define CNTRL_R 0x00004000
64/* CX23888 specific control flag */
65#define CNTRL_IVO 0x00008000
63 66
64#define CX23888_IR_TXCLK_REG 0x170004 67#define CX23888_IR_TXCLK_REG 0x170004
65#define TXCLK_TCD 0x0000FFFF 68#define TXCLK_TCD 0x0000FFFF
@@ -111,8 +114,18 @@ MODULE_PARM_DESC(ir_888_debug, "enable debug messages [CX23888 IR controller]");
111#define CX23888_VIDCLK_FREQ 108000000 /* 108 MHz, BT.656 */ 114#define CX23888_VIDCLK_FREQ 108000000 /* 108 MHz, BT.656 */
112#define CX23888_IR_REFCLK_FREQ (CX23888_VIDCLK_FREQ / 2) 115#define CX23888_IR_REFCLK_FREQ (CX23888_VIDCLK_FREQ / 2)
113 116
114#define CX23888_IR_RX_KFIFO_SIZE (512 * sizeof(u32)) 117/*
115#define CX23888_IR_TX_KFIFO_SIZE (512 * sizeof(u32)) 118 * We use this union internally for convenience, but callers to tx_write
119 * and rx_read will be expecting records of type struct ir_raw_event.
120 * Always ensure the size of this union is dictated by struct ir_raw_event.
121 */
122union cx23888_ir_fifo_rec {
123 u32 hw_fifo_data;
124 struct ir_raw_event ir_core_data;
125};
126
127#define CX23888_IR_RX_KFIFO_SIZE (256 * sizeof(union cx23888_ir_fifo_rec))
128#define CX23888_IR_TX_KFIFO_SIZE (256 * sizeof(union cx23888_ir_fifo_rec))
116 129
117struct cx23888_ir_state { 130struct cx23888_ir_state {
118 struct v4l2_subdev sd; 131 struct v4l2_subdev sd;
@@ -423,6 +436,13 @@ static inline void control_tx_polarity_invert(struct cx23885_dev *dev,
423 invert ? CNTRL_CPL : 0); 436 invert ? CNTRL_CPL : 0);
424} 437}
425 438
439static inline void control_tx_level_invert(struct cx23885_dev *dev,
440 bool invert)
441{
442 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_IVO,
443 invert ? CNTRL_IVO : 0);
444}
445
426/* 446/*
427 * IR Rx & Tx Clock Register helpers 447 * IR Rx & Tx Clock Register helpers
428 */ 448 */
@@ -449,8 +469,8 @@ static u32 txclk_tx_s_max_pulse_width(struct cx23885_dev *dev, u32 ns,
449{ 469{
450 u64 pulse_clocks; 470 u64 pulse_clocks;
451 471
452 if (ns > V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS) 472 if (ns > IR_MAX_DURATION)
453 ns = V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS; 473 ns = IR_MAX_DURATION;
454 pulse_clocks = ns_to_pulse_clocks(ns); 474 pulse_clocks = ns_to_pulse_clocks(ns);
455 *divider = pulse_clocks_to_clock_divider(pulse_clocks); 475 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
456 cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, *divider); 476 cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, *divider);
@@ -462,8 +482,8 @@ static u32 rxclk_rx_s_max_pulse_width(struct cx23885_dev *dev, u32 ns,
462{ 482{
463 u64 pulse_clocks; 483 u64 pulse_clocks;
464 484
465 if (ns > V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS) 485 if (ns > IR_MAX_DURATION)
466 ns = V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS; 486 ns = IR_MAX_DURATION;
467 pulse_clocks = ns_to_pulse_clocks(ns); 487 pulse_clocks = ns_to_pulse_clocks(ns);
468 *divider = pulse_clocks_to_clock_divider(pulse_clocks); 488 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
469 cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, *divider); 489 cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, *divider);
@@ -526,8 +546,8 @@ static int cx23888_ir_irq_handler(struct v4l2_subdev *sd, u32 status,
526 u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG); 546 u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG);
527 u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG); 547 u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG);
528 548
529 u32 rx_data[FIFO_RX_DEPTH]; 549 union cx23888_ir_fifo_rec rx_data[FIFO_RX_DEPTH];
530 int i, j, k; 550 unsigned int i, j, k;
531 u32 events, v; 551 u32 events, v;
532 int tsr, rsr, rto, ror, tse, rse, rte, roe, kror; 552 int tsr, rsr, rto, ror, tse, rse, rte, roe, kror;
533 553
@@ -588,11 +608,12 @@ static int cx23888_ir_irq_handler(struct v4l2_subdev *sd, u32 status,
588 for (j = 0; 608 for (j = 0;
589 (v & FIFO_RX_NDV) && j < FIFO_RX_DEPTH; j++) { 609 (v & FIFO_RX_NDV) && j < FIFO_RX_DEPTH; j++) {
590 v = cx23888_ir_read4(dev, CX23888_IR_FIFO_REG); 610 v = cx23888_ir_read4(dev, CX23888_IR_FIFO_REG);
591 rx_data[i++] = v & ~FIFO_RX_NDV; 611 rx_data[i].hw_fifo_data = v & ~FIFO_RX_NDV;
612 i++;
592 } 613 }
593 if (i == 0) 614 if (i == 0)
594 break; 615 break;
595 j = i * sizeof(u32); 616 j = i * sizeof(union cx23888_ir_fifo_rec);
596 k = kfifo_in_locked(&state->rx_kfifo, 617 k = kfifo_in_locked(&state->rx_kfifo,
597 (unsigned char *) rx_data, j, 618 (unsigned char *) rx_data, j,
598 &state->rx_kfifo_lock); 619 &state->rx_kfifo_lock);
@@ -651,10 +672,11 @@ static int cx23888_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,
651 u16 divider = (u16) atomic_read(&state->rxclk_divider); 672 u16 divider = (u16) atomic_read(&state->rxclk_divider);
652 673
653 unsigned int i, n; 674 unsigned int i, n;
654 u32 *p; 675 union cx23888_ir_fifo_rec *p;
655 u32 u, v; 676 unsigned u, v;
656 677
657 n = count / sizeof(u32) * sizeof(u32); 678 n = count / sizeof(union cx23888_ir_fifo_rec)
679 * sizeof(union cx23888_ir_fifo_rec);
658 if (n == 0) { 680 if (n == 0) {
659 *num = 0; 681 *num = 0;
660 return 0; 682 return 0;
@@ -662,26 +684,28 @@ static int cx23888_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,
662 684
663 n = kfifo_out_locked(&state->rx_kfifo, buf, n, &state->rx_kfifo_lock); 685 n = kfifo_out_locked(&state->rx_kfifo, buf, n, &state->rx_kfifo_lock);
664 686
665 n /= sizeof(u32); 687 n /= sizeof(union cx23888_ir_fifo_rec);
666 *num = n * sizeof(u32); 688 *num = n * sizeof(union cx23888_ir_fifo_rec);
689
690 for (p = (union cx23888_ir_fifo_rec *) buf, i = 0; i < n; p++, i++) {
667 691
668 for (p = (u32 *) buf, i = 0; i < n; p++, i++) { 692 if ((p->hw_fifo_data & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {
669 if ((*p & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) { 693 /* Assume RTO was because of no IR light input */
670 *p = V4L2_SUBDEV_IR_PULSE_RX_SEQ_END; 694 u = 0;
671 v4l2_dbg(2, ir_888_debug, sd, "rx read: end of rx\n"); 695 v4l2_dbg(2, ir_888_debug, sd, "rx read: end of rx\n");
672 continue; 696 } else {
697 u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;
698 if (invert)
699 u = u ? 0 : 1;
673 } 700 }
674 701
675 u = (*p & FIFO_RXTX_LVL) ? V4L2_SUBDEV_IR_PULSE_LEVEL_MASK : 0; 702 v = (unsigned) pulse_width_count_to_ns(
676 if (invert) 703 (u16) (p->hw_fifo_data & FIFO_RXTX), divider);
677 u = u ? 0 : V4L2_SUBDEV_IR_PULSE_LEVEL_MASK; 704 if (v > IR_MAX_DURATION)
705 v = IR_MAX_DURATION;
678 706
679 v = (u32) pulse_width_count_to_ns((u16) (*p & FIFO_RXTX), 707 p->ir_core_data.pulse = u;
680 divider); 708 p->ir_core_data.duration = v;
681 if (v >= V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS)
682 v = V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS - 1;
683
684 *p = u | v;
685 709
686 v4l2_dbg(2, ir_888_debug, sd, "rx read: %10u ns %s\n", 710 v4l2_dbg(2, ir_888_debug, sd, "rx read: %10u ns %s\n",
687 v, u ? "mark" : "space"); 711 v, u ? "mark" : "space");
@@ -740,7 +764,8 @@ static int cx23888_ir_rx_s_parameters(struct v4l2_subdev *sd,
740 764
741 o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH; 765 o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
742 766
743 o->bytes_per_data_element = p->bytes_per_data_element = sizeof(u32); 767 o->bytes_per_data_element = p->bytes_per_data_element
768 = sizeof(union cx23888_ir_fifo_rec);
744 769
745 /* Before we tweak the hardware, we have to disable the receiver */ 770 /* Before we tweak the hardware, we have to disable the receiver */
746 irqenable_rx(dev, 0); 771 irqenable_rx(dev, 0);
@@ -762,12 +787,15 @@ static int cx23888_ir_rx_s_parameters(struct v4l2_subdev *sd,
762 &p->carrier_range_upper); 787 &p->carrier_range_upper);
763 o->carrier_range_lower = p->carrier_range_lower; 788 o->carrier_range_lower = p->carrier_range_lower;
764 o->carrier_range_upper = p->carrier_range_upper; 789 o->carrier_range_upper = p->carrier_range_upper;
790
791 p->max_pulse_width =
792 (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
765 } else { 793 } else {
766 p->max_pulse_width = 794 p->max_pulse_width =
767 rxclk_rx_s_max_pulse_width(dev, p->max_pulse_width, 795 rxclk_rx_s_max_pulse_width(dev, p->max_pulse_width,
768 &rxclk_divider); 796 &rxclk_divider);
769 o->max_pulse_width = p->max_pulse_width;
770 } 797 }
798 o->max_pulse_width = p->max_pulse_width;
771 atomic_set(&state->rxclk_divider, rxclk_divider); 799 atomic_set(&state->rxclk_divider, rxclk_divider);
772 800
773 p->noise_filter_min_width = 801 p->noise_filter_min_width =
@@ -782,8 +810,8 @@ static int cx23888_ir_rx_s_parameters(struct v4l2_subdev *sd,
782 810
783 control_rx_s_edge_detection(dev, CNTRL_EDG_BOTH); 811 control_rx_s_edge_detection(dev, CNTRL_EDG_BOTH);
784 812
785 o->invert = p->invert; 813 o->invert_level = p->invert_level;
786 atomic_set(&state->rx_invert, p->invert); 814 atomic_set(&state->rx_invert, p->invert_level);
787 815
788 o->interrupt_enable = p->interrupt_enable; 816 o->interrupt_enable = p->interrupt_enable;
789 o->enable = p->enable; 817 o->enable = p->enable;
@@ -864,7 +892,8 @@ static int cx23888_ir_tx_s_parameters(struct v4l2_subdev *sd,
864 892
865 o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH; 893 o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
866 894
867 o->bytes_per_data_element = p->bytes_per_data_element = sizeof(u32); 895 o->bytes_per_data_element = p->bytes_per_data_element
896 = sizeof(union cx23888_ir_fifo_rec);
868 897
869 /* Before we tweak the hardware, we have to disable the transmitter */ 898 /* Before we tweak the hardware, we have to disable the transmitter */
870 irqenable_tx(dev, 0); 899 irqenable_tx(dev, 0);
@@ -880,12 +909,15 @@ static int cx23888_ir_tx_s_parameters(struct v4l2_subdev *sd,
880 909
881 p->duty_cycle = cduty_tx_s_duty_cycle(dev, p->duty_cycle); 910 p->duty_cycle = cduty_tx_s_duty_cycle(dev, p->duty_cycle);
882 o->duty_cycle = p->duty_cycle; 911 o->duty_cycle = p->duty_cycle;
912
913 p->max_pulse_width =
914 (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
883 } else { 915 } else {
884 p->max_pulse_width = 916 p->max_pulse_width =
885 txclk_tx_s_max_pulse_width(dev, p->max_pulse_width, 917 txclk_tx_s_max_pulse_width(dev, p->max_pulse_width,
886 &txclk_divider); 918 &txclk_divider);
887 o->max_pulse_width = p->max_pulse_width;
888 } 919 }
920 o->max_pulse_width = p->max_pulse_width;
889 atomic_set(&state->txclk_divider, txclk_divider); 921 atomic_set(&state->txclk_divider, txclk_divider);
890 922
891 p->resolution = clock_divider_to_resolution(txclk_divider); 923 p->resolution = clock_divider_to_resolution(txclk_divider);
@@ -894,8 +926,11 @@ static int cx23888_ir_tx_s_parameters(struct v4l2_subdev *sd,
894 /* FIXME - make this dependent on resolution for better performance */ 926 /* FIXME - make this dependent on resolution for better performance */
895 control_tx_irq_watermark(dev, TX_FIFO_HALF_EMPTY); 927 control_tx_irq_watermark(dev, TX_FIFO_HALF_EMPTY);
896 928
897 control_tx_polarity_invert(dev, p->invert); 929 control_tx_polarity_invert(dev, p->invert_carrier_sense);
898 o->invert = p->invert; 930 o->invert_carrier_sense = p->invert_carrier_sense;
931
932 control_tx_level_invert(dev, p->invert_level);
933 o->invert_level = p->invert_level;
899 934
900 o->interrupt_enable = p->interrupt_enable; 935 o->interrupt_enable = p->interrupt_enable;
901 o->enable = p->enable; 936 o->enable = p->enable;
@@ -988,12 +1023,10 @@ static int cx23888_ir_log_status(struct v4l2_subdev *sd)
988 "-%1d/+%1d, %u to %u Hz\n", i, j, 1023 "-%1d/+%1d, %u to %u Hz\n", i, j,
989 clock_divider_to_freq(rxclk, 16 + j), 1024 clock_divider_to_freq(rxclk, 16 + j),
990 clock_divider_to_freq(rxclk, 16 - i)); 1025 clock_divider_to_freq(rxclk, 16 - i));
991 } else {
992 v4l2_info(sd, "\tMax measurable pulse width: %u us, "
993 "%llu ns\n",
994 pulse_width_count_to_us(FIFO_RXTX, rxclk),
995 pulse_width_count_to_ns(FIFO_RXTX, rxclk));
996 } 1026 }
1027 v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",
1028 pulse_width_count_to_us(FIFO_RXTX, rxclk),
1029 pulse_width_count_to_ns(FIFO_RXTX, rxclk));
997 v4l2_info(sd, "\tLow pass filter: %s\n", 1030 v4l2_info(sd, "\tLow pass filter: %s\n",
998 filtr ? "enabled" : "disabled"); 1031 filtr ? "enabled" : "disabled");
999 if (filtr) 1032 if (filtr)
@@ -1025,19 +1058,20 @@ static int cx23888_ir_log_status(struct v4l2_subdev *sd)
1025 cntrl & CNTRL_TFE ? "enabled" : "disabled"); 1058 cntrl & CNTRL_TFE ? "enabled" : "disabled");
1026 v4l2_info(sd, "\tFIFO interrupt watermark: %s\n", 1059 v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
1027 cntrl & CNTRL_TIC ? "not empty" : "half full or less"); 1060 cntrl & CNTRL_TIC ? "not empty" : "half full or less");
1028 v4l2_info(sd, "\tSignal polarity: %s\n", 1061 v4l2_info(sd, "\tOutput pin level inversion %s\n",
1029 cntrl & CNTRL_CPL ? "0:mark 1:space" : "0:space 1:mark"); 1062 cntrl & CNTRL_IVO ? "yes" : "no");
1063 v4l2_info(sd, "\tCarrier polarity: %s\n",
1064 cntrl & CNTRL_CPL ? "space:burst mark:noburst"
1065 : "space:noburst mark:burst");
1030 if (cntrl & CNTRL_MOD) { 1066 if (cntrl & CNTRL_MOD) {
1031 v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n", 1067 v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",
1032 clock_divider_to_carrier_freq(txclk)); 1068 clock_divider_to_carrier_freq(txclk));
1033 v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n", 1069 v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",
1034 cduty + 1); 1070 cduty + 1);
1035 } else {
1036 v4l2_info(sd, "\tMax pulse width: %u us, "
1037 "%llu ns\n",
1038 pulse_width_count_to_us(FIFO_RXTX, txclk),
1039 pulse_width_count_to_ns(FIFO_RXTX, txclk));
1040 } 1071 }
1072 v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",
1073 pulse_width_count_to_us(FIFO_RXTX, txclk),
1074 pulse_width_count_to_ns(FIFO_RXTX, txclk));
1041 v4l2_info(sd, "\tBusy: %s\n", 1075 v4l2_info(sd, "\tBusy: %s\n",
1042 stats & STATS_TBY ? "yes" : "no"); 1076 stats & STATS_TBY ? "yes" : "no");
1043 v4l2_info(sd, "\tFIFO service requested: %s\n", 1077 v4l2_info(sd, "\tFIFO service requested: %s\n",
@@ -1111,11 +1145,10 @@ static const struct v4l2_subdev_core_ops cx23888_ir_core_ops = {
1111 .g_register = cx23888_ir_g_register, 1145 .g_register = cx23888_ir_g_register,
1112 .s_register = cx23888_ir_s_register, 1146 .s_register = cx23888_ir_s_register,
1113#endif 1147#endif
1148 .interrupt_service_routine = cx23888_ir_irq_handler,
1114}; 1149};
1115 1150
1116static const struct v4l2_subdev_ir_ops cx23888_ir_ir_ops = { 1151static const struct v4l2_subdev_ir_ops cx23888_ir_ir_ops = {
1117 .interrupt_service_routine = cx23888_ir_irq_handler,
1118
1119 .rx_read = cx23888_ir_rx_read, 1152 .rx_read = cx23888_ir_rx_read,
1120 .rx_g_parameters = cx23888_ir_rx_g_parameters, 1153 .rx_g_parameters = cx23888_ir_rx_g_parameters,
1121 .rx_s_parameters = cx23888_ir_rx_s_parameters, 1154 .rx_s_parameters = cx23888_ir_rx_s_parameters,
@@ -1131,7 +1164,7 @@ static const struct v4l2_subdev_ops cx23888_ir_controller_ops = {
1131}; 1164};
1132 1165
1133static const struct v4l2_subdev_ir_parameters default_rx_params = { 1166static const struct v4l2_subdev_ir_parameters default_rx_params = {
1134 .bytes_per_data_element = sizeof(u32), 1167 .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
1135 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH, 1168 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1136 1169
1137 .enable = false, 1170 .enable = false,
@@ -1146,11 +1179,11 @@ static const struct v4l2_subdev_ir_parameters default_rx_params = {
1146 .noise_filter_min_width = 333333, /* ns */ 1179 .noise_filter_min_width = 333333, /* ns */
1147 .carrier_range_lower = 35000, 1180 .carrier_range_lower = 35000,
1148 .carrier_range_upper = 37000, 1181 .carrier_range_upper = 37000,
1149 .invert = false, 1182 .invert_level = false,
1150}; 1183};
1151 1184
1152static const struct v4l2_subdev_ir_parameters default_tx_params = { 1185static const struct v4l2_subdev_ir_parameters default_tx_params = {
1153 .bytes_per_data_element = sizeof(u32), 1186 .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
1154 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH, 1187 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1155 1188
1156 .enable = false, 1189 .enable = false,
@@ -1160,7 +1193,8 @@ static const struct v4l2_subdev_ir_parameters default_tx_params = {
1160 .modulation = true, 1193 .modulation = true,
1161 .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */ 1194 .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */
1162 .duty_cycle = 25, /* 25 % - RC-5 carrier */ 1195 .duty_cycle = 25, /* 25 % - RC-5 carrier */
1163 .invert = false, 1196 .invert_level = false,
1197 .invert_carrier_sense = false,
1164}; 1198};
1165 1199
1166int cx23888_ir_probe(struct cx23885_dev *dev) 1200int cx23888_ir_probe(struct cx23885_dev *dev)
diff --git a/drivers/media/video/cx25840/Makefile b/drivers/media/video/cx25840/Makefile
index 6e8665be895..2ee96d3973b 100644
--- a/drivers/media/video/cx25840/Makefile
+++ b/drivers/media/video/cx25840/Makefile
@@ -1,5 +1,5 @@
1cx25840-objs := cx25840-core.o cx25840-audio.o cx25840-firmware.o \ 1cx25840-objs := cx25840-core.o cx25840-audio.o cx25840-firmware.o \
2 cx25840-vbi.o 2 cx25840-vbi.o cx25840-ir.o
3 3
4obj-$(CONFIG_VIDEO_CX25840) += cx25840.o 4obj-$(CONFIG_VIDEO_CX25840) += cx25840.o
5 5
diff --git a/drivers/media/video/cx25840/cx25840-audio.c b/drivers/media/video/cx25840/cx25840-audio.c
index 45608d50529..6faad34df3a 100644
--- a/drivers/media/video/cx25840/cx25840-audio.c
+++ b/drivers/media/video/cx25840/cx25840-audio.c
@@ -474,33 +474,10 @@ void cx25840_audio_set_path(struct i2c_client *client)
474 cx25840_and_or(client, 0x803, ~0x10, 0x10); 474 cx25840_and_or(client, 0x803, ~0x10, 0x10);
475} 475}
476 476
477static int get_volume(struct i2c_client *client)
478{
479 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
480 int vol;
481
482 if (state->unmute_volume >= 0)
483 return state->unmute_volume;
484
485 /* Volume runs +18dB to -96dB in 1/2dB steps
486 * change to fit the msp3400 -114dB to +12dB range */
487
488 /* check PATH1_VOLUME */
489 vol = 228 - cx25840_read(client, 0x8d4);
490 vol = (vol / 2) + 23;
491 return vol << 9;
492}
493
494static void set_volume(struct i2c_client *client, int volume) 477static void set_volume(struct i2c_client *client, int volume)
495{ 478{
496 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
497 int vol; 479 int vol;
498 480
499 if (state->unmute_volume >= 0) {
500 state->unmute_volume = volume;
501 return;
502 }
503
504 /* Convert the volume to msp3400 values (0-127) */ 481 /* Convert the volume to msp3400 values (0-127) */
505 vol = volume >> 9; 482 vol = volume >> 9;
506 483
@@ -517,52 +494,6 @@ static void set_volume(struct i2c_client *client, int volume)
517 cx25840_write(client, 0x8d4, 228 - (vol * 2)); 494 cx25840_write(client, 0x8d4, 228 - (vol * 2));
518} 495}
519 496
520static int get_bass(struct i2c_client *client)
521{
522 /* bass is 49 steps +12dB to -12dB */
523
524 /* check PATH1_EQ_BASS_VOL */
525 int bass = cx25840_read(client, 0x8d9) & 0x3f;
526 bass = (((48 - bass) * 0xffff) + 47) / 48;
527 return bass;
528}
529
530static void set_bass(struct i2c_client *client, int bass)
531{
532 /* PATH1_EQ_BASS_VOL */
533 cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
534}
535
536static int get_treble(struct i2c_client *client)
537{
538 /* treble is 49 steps +12dB to -12dB */
539
540 /* check PATH1_EQ_TREBLE_VOL */
541 int treble = cx25840_read(client, 0x8db) & 0x3f;
542 treble = (((48 - treble) * 0xffff) + 47) / 48;
543 return treble;
544}
545
546static void set_treble(struct i2c_client *client, int treble)
547{
548 /* PATH1_EQ_TREBLE_VOL */
549 cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
550}
551
552static int get_balance(struct i2c_client *client)
553{
554 /* balance is 7 bit, 0 to -96dB */
555
556 /* check PATH1_BAL_LEVEL */
557 int balance = cx25840_read(client, 0x8d5) & 0x7f;
558 /* check PATH1_BAL_LEFT */
559 if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
560 balance = 0x80 - balance;
561 else
562 balance = 0x80 + balance;
563 return balance << 8;
564}
565
566static void set_balance(struct i2c_client *client, int balance) 497static void set_balance(struct i2c_client *client, int balance)
567{ 498{
568 int bal = balance >> 8; 499 int bal = balance >> 8;
@@ -579,31 +510,6 @@ static void set_balance(struct i2c_client *client, int balance)
579 } 510 }
580} 511}
581 512
582static int get_mute(struct i2c_client *client)
583{
584 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
585
586 return state->unmute_volume >= 0;
587}
588
589static void set_mute(struct i2c_client *client, int mute)
590{
591 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
592
593 if (mute && state->unmute_volume == -1) {
594 int vol = get_volume(client);
595
596 set_volume(client, 0);
597 state->unmute_volume = vol;
598 }
599 else if (!mute && state->unmute_volume != -1) {
600 int vol = state->unmute_volume;
601
602 state->unmute_volume = -1;
603 set_volume(client, vol);
604 }
605}
606
607int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq) 513int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
608{ 514{
609 struct i2c_client *client = v4l2_get_subdevdata(sd); 515 struct i2c_client *client = v4l2_get_subdevdata(sd);
@@ -624,25 +530,31 @@ int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
624 return retval; 530 return retval;
625} 531}
626 532
627int cx25840_audio_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 533static int cx25840_audio_s_ctrl(struct v4l2_ctrl *ctrl)
628{ 534{
535 struct v4l2_subdev *sd = to_sd(ctrl);
536 struct cx25840_state *state = to_state(sd);
629 struct i2c_client *client = v4l2_get_subdevdata(sd); 537 struct i2c_client *client = v4l2_get_subdevdata(sd);
630 538
631 switch (ctrl->id) { 539 switch (ctrl->id) {
632 case V4L2_CID_AUDIO_VOLUME: 540 case V4L2_CID_AUDIO_VOLUME:
633 ctrl->value = get_volume(client); 541 if (state->mute->val)
542 set_volume(client, 0);
543 else
544 set_volume(client, state->volume->val);
634 break; 545 break;
635 case V4L2_CID_AUDIO_BASS: 546 case V4L2_CID_AUDIO_BASS:
636 ctrl->value = get_bass(client); 547 /* PATH1_EQ_BASS_VOL */
548 cx25840_and_or(client, 0x8d9, ~0x3f,
549 48 - (ctrl->val * 48 / 0xffff));
637 break; 550 break;
638 case V4L2_CID_AUDIO_TREBLE: 551 case V4L2_CID_AUDIO_TREBLE:
639 ctrl->value = get_treble(client); 552 /* PATH1_EQ_TREBLE_VOL */
553 cx25840_and_or(client, 0x8db, ~0x3f,
554 48 - (ctrl->val * 48 / 0xffff));
640 break; 555 break;
641 case V4L2_CID_AUDIO_BALANCE: 556 case V4L2_CID_AUDIO_BALANCE:
642 ctrl->value = get_balance(client); 557 set_balance(client, ctrl->val);
643 break;
644 case V4L2_CID_AUDIO_MUTE:
645 ctrl->value = get_mute(client);
646 break; 558 break;
647 default: 559 default:
648 return -EINVAL; 560 return -EINVAL;
@@ -650,28 +562,6 @@ int cx25840_audio_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
650 return 0; 562 return 0;
651} 563}
652 564
653int cx25840_audio_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 565const struct v4l2_ctrl_ops cx25840_audio_ctrl_ops = {
654{ 566 .s_ctrl = cx25840_audio_s_ctrl,
655 struct i2c_client *client = v4l2_get_subdevdata(sd); 567};
656
657 switch (ctrl->id) {
658 case V4L2_CID_AUDIO_VOLUME:
659 set_volume(client, ctrl->value);
660 break;
661 case V4L2_CID_AUDIO_BASS:
662 set_bass(client, ctrl->value);
663 break;
664 case V4L2_CID_AUDIO_TREBLE:
665 set_treble(client, ctrl->value);
666 break;
667 case V4L2_CID_AUDIO_BALANCE:
668 set_balance(client, ctrl->value);
669 break;
670 case V4L2_CID_AUDIO_MUTE:
671 set_mute(client, ctrl->value);
672 break;
673 default:
674 return -EINVAL;
675 }
676 return 0;
677}
diff --git a/drivers/media/video/cx25840/cx25840-core.c b/drivers/media/video/cx25840/cx25840-core.c
index bb4872b2ceb..86ca8c2359d 100644
--- a/drivers/media/video/cx25840/cx25840-core.c
+++ b/drivers/media/video/cx25840/cx25840-core.c
@@ -15,6 +15,9 @@
15 * 15 *
16 * CX23885 support by Steven Toth <stoth@linuxtv.org>. 16 * CX23885 support by Steven Toth <stoth@linuxtv.org>.
17 * 17 *
18 * CX2388[578] IRQ handling, IO Pin mux configuration and other small fixes are
19 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
20 *
18 * This program is free software; you can redistribute it and/or 21 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License 22 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version 2 23 * as published by the Free Software Foundation; either version 2
@@ -48,6 +51,28 @@ MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
48MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford"); 51MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
49MODULE_LICENSE("GPL"); 52MODULE_LICENSE("GPL");
50 53
54#define CX25840_VID_INT_STAT_REG 0x410
55#define CX25840_VID_INT_STAT_BITS 0x0000ffff
56#define CX25840_VID_INT_MASK_BITS 0xffff0000
57#define CX25840_VID_INT_MASK_SHFT 16
58#define CX25840_VID_INT_MASK_REG 0x412
59
60#define CX23885_AUD_MC_INT_MASK_REG 0x80c
61#define CX23885_AUD_MC_INT_STAT_BITS 0xffff0000
62#define CX23885_AUD_MC_INT_CTRL_BITS 0x0000ffff
63#define CX23885_AUD_MC_INT_STAT_SHFT 16
64
65#define CX25840_AUD_INT_CTRL_REG 0x812
66#define CX25840_AUD_INT_STAT_REG 0x813
67
68#define CX23885_PIN_CTRL_IRQ_REG 0x123
69#define CX23885_PIN_CTRL_IRQ_IR_STAT 0x40
70#define CX23885_PIN_CTRL_IRQ_AUD_STAT 0x20
71#define CX23885_PIN_CTRL_IRQ_VID_STAT 0x10
72
73#define CX25840_IR_STATS_REG 0x210
74#define CX25840_IR_IRQEN_REG 0x214
75
51static int cx25840_debug; 76static int cx25840_debug;
52 77
53module_param_named(debug,cx25840_debug, int, 0644); 78module_param_named(debug,cx25840_debug, int, 0644);
@@ -80,33 +105,53 @@ int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
80 105
81u8 cx25840_read(struct i2c_client * client, u16 addr) 106u8 cx25840_read(struct i2c_client * client, u16 addr)
82{ 107{
83 u8 buffer[2]; 108 struct i2c_msg msgs[2];
84 buffer[0] = addr >> 8; 109 u8 tx_buf[2], rx_buf[1];
85 buffer[1] = addr & 0xff; 110
86 111 /* Write register address */
87 if (i2c_master_send(client, buffer, 2) < 2) 112 tx_buf[0] = addr >> 8;
88 return 0; 113 tx_buf[1] = addr & 0xff;
89 114 msgs[0].addr = client->addr;
90 if (i2c_master_recv(client, buffer, 1) < 1) 115 msgs[0].flags = 0;
116 msgs[0].len = 2;
117 msgs[0].buf = (char *) tx_buf;
118
119 /* Read data from register */
120 msgs[1].addr = client->addr;
121 msgs[1].flags = I2C_M_RD;
122 msgs[1].len = 1;
123 msgs[1].buf = (char *) rx_buf;
124
125 if (i2c_transfer(client->adapter, msgs, 2) < 2)
91 return 0; 126 return 0;
92 127
93 return buffer[0]; 128 return rx_buf[0];
94} 129}
95 130
96u32 cx25840_read4(struct i2c_client * client, u16 addr) 131u32 cx25840_read4(struct i2c_client * client, u16 addr)
97{ 132{
98 u8 buffer[4]; 133 struct i2c_msg msgs[2];
99 buffer[0] = addr >> 8; 134 u8 tx_buf[2], rx_buf[4];
100 buffer[1] = addr & 0xff; 135
101 136 /* Write register address */
102 if (i2c_master_send(client, buffer, 2) < 2) 137 tx_buf[0] = addr >> 8;
103 return 0; 138 tx_buf[1] = addr & 0xff;
104 139 msgs[0].addr = client->addr;
105 if (i2c_master_recv(client, buffer, 4) < 4) 140 msgs[0].flags = 0;
141 msgs[0].len = 2;
142 msgs[0].buf = (char *) tx_buf;
143
144 /* Read data from registers */
145 msgs[1].addr = client->addr;
146 msgs[1].flags = I2C_M_RD;
147 msgs[1].len = 4;
148 msgs[1].buf = (char *) rx_buf;
149
150 if (i2c_transfer(client->adapter, msgs, 2) < 2)
106 return 0; 151 return 0;
107 152
108 return (buffer[3] << 24) | (buffer[2] << 16) | 153 return (rx_buf[3] << 24) | (rx_buf[2] << 16) | (rx_buf[1] << 8) |
109 (buffer[1] << 8) | buffer[0]; 154 rx_buf[0];
110} 155}
111 156
112int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask, 157int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
@@ -117,6 +162,14 @@ int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
117 or_value); 162 or_value);
118} 163}
119 164
165int cx25840_and_or4(struct i2c_client *client, u16 addr, u32 and_mask,
166 u32 or_value)
167{
168 return cx25840_write4(client, addr,
169 (cx25840_read4(client, addr) & and_mask) |
170 or_value);
171}
172
120/* ----------------------------------------------------------------------- */ 173/* ----------------------------------------------------------------------- */
121 174
122static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input, 175static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
@@ -124,6 +177,158 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
124 177
125/* ----------------------------------------------------------------------- */ 178/* ----------------------------------------------------------------------- */
126 179
180static int cx23885_s_io_pin_config(struct v4l2_subdev *sd, size_t n,
181 struct v4l2_subdev_io_pin_config *p)
182{
183 struct i2c_client *client = v4l2_get_subdevdata(sd);
184 int i;
185 u32 pin_ctrl;
186 u8 gpio_oe, gpio_data, strength;
187
188 pin_ctrl = cx25840_read4(client, 0x120);
189 gpio_oe = cx25840_read(client, 0x160);
190 gpio_data = cx25840_read(client, 0x164);
191
192 for (i = 0; i < n; i++) {
193 strength = p[i].strength;
194 if (strength > CX25840_PIN_DRIVE_FAST)
195 strength = CX25840_PIN_DRIVE_FAST;
196
197 switch (p[i].pin) {
198 case CX23885_PIN_IRQ_N_GPIO16:
199 if (p[i].function != CX23885_PAD_IRQ_N) {
200 /* GPIO16 */
201 pin_ctrl &= ~(0x1 << 25);
202 } else {
203 /* IRQ_N */
204 if (p[i].flags &
205 (V4L2_SUBDEV_IO_PIN_DISABLE |
206 V4L2_SUBDEV_IO_PIN_INPUT)) {
207 pin_ctrl &= ~(0x1 << 25);
208 } else {
209 pin_ctrl |= (0x1 << 25);
210 }
211 if (p[i].flags &
212 V4L2_SUBDEV_IO_PIN_ACTIVE_LOW) {
213 pin_ctrl &= ~(0x1 << 24);
214 } else {
215 pin_ctrl |= (0x1 << 24);
216 }
217 }
218 break;
219 case CX23885_PIN_IR_RX_GPIO19:
220 if (p[i].function != CX23885_PAD_GPIO19) {
221 /* IR_RX */
222 gpio_oe |= (0x1 << 0);
223 pin_ctrl &= ~(0x3 << 18);
224 pin_ctrl |= (strength << 18);
225 } else {
226 /* GPIO19 */
227 gpio_oe &= ~(0x1 << 0);
228 if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
229 gpio_data &= ~(0x1 << 0);
230 gpio_data |= ((p[i].value & 0x1) << 0);
231 }
232 pin_ctrl &= ~(0x3 << 12);
233 pin_ctrl |= (strength << 12);
234 }
235 break;
236 case CX23885_PIN_IR_TX_GPIO20:
237 if (p[i].function != CX23885_PAD_GPIO20) {
238 /* IR_TX */
239 gpio_oe |= (0x1 << 1);
240 if (p[i].flags & V4L2_SUBDEV_IO_PIN_DISABLE)
241 pin_ctrl &= ~(0x1 << 10);
242 else
243 pin_ctrl |= (0x1 << 10);
244 pin_ctrl &= ~(0x3 << 18);
245 pin_ctrl |= (strength << 18);
246 } else {
247 /* GPIO20 */
248 gpio_oe &= ~(0x1 << 1);
249 if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
250 gpio_data &= ~(0x1 << 1);
251 gpio_data |= ((p[i].value & 0x1) << 1);
252 }
253 pin_ctrl &= ~(0x3 << 12);
254 pin_ctrl |= (strength << 12);
255 }
256 break;
257 case CX23885_PIN_I2S_SDAT_GPIO21:
258 if (p[i].function != CX23885_PAD_GPIO21) {
259 /* I2S_SDAT */
260 /* TODO: Input or Output config */
261 gpio_oe |= (0x1 << 2);
262 pin_ctrl &= ~(0x3 << 22);
263 pin_ctrl |= (strength << 22);
264 } else {
265 /* GPIO21 */
266 gpio_oe &= ~(0x1 << 2);
267 if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
268 gpio_data &= ~(0x1 << 2);
269 gpio_data |= ((p[i].value & 0x1) << 2);
270 }
271 pin_ctrl &= ~(0x3 << 12);
272 pin_ctrl |= (strength << 12);
273 }
274 break;
275 case CX23885_PIN_I2S_WCLK_GPIO22:
276 if (p[i].function != CX23885_PAD_GPIO22) {
277 /* I2S_WCLK */
278 /* TODO: Input or Output config */
279 gpio_oe |= (0x1 << 3);
280 pin_ctrl &= ~(0x3 << 22);
281 pin_ctrl |= (strength << 22);
282 } else {
283 /* GPIO22 */
284 gpio_oe &= ~(0x1 << 3);
285 if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
286 gpio_data &= ~(0x1 << 3);
287 gpio_data |= ((p[i].value & 0x1) << 3);
288 }
289 pin_ctrl &= ~(0x3 << 12);
290 pin_ctrl |= (strength << 12);
291 }
292 break;
293 case CX23885_PIN_I2S_BCLK_GPIO23:
294 if (p[i].function != CX23885_PAD_GPIO23) {
295 /* I2S_BCLK */
296 /* TODO: Input or Output config */
297 gpio_oe |= (0x1 << 4);
298 pin_ctrl &= ~(0x3 << 22);
299 pin_ctrl |= (strength << 22);
300 } else {
301 /* GPIO23 */
302 gpio_oe &= ~(0x1 << 4);
303 if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
304 gpio_data &= ~(0x1 << 4);
305 gpio_data |= ((p[i].value & 0x1) << 4);
306 }
307 pin_ctrl &= ~(0x3 << 12);
308 pin_ctrl |= (strength << 12);
309 }
310 break;
311 }
312 }
313
314 cx25840_write(client, 0x164, gpio_data);
315 cx25840_write(client, 0x160, gpio_oe);
316 cx25840_write4(client, 0x120, pin_ctrl);
317 return 0;
318}
319
320static int common_s_io_pin_config(struct v4l2_subdev *sd, size_t n,
321 struct v4l2_subdev_io_pin_config *pincfg)
322{
323 struct cx25840_state *state = to_state(sd);
324
325 if (is_cx2388x(state))
326 return cx23885_s_io_pin_config(sd, n, pincfg);
327 return 0;
328}
329
330/* ----------------------------------------------------------------------- */
331
127static void init_dll1(struct i2c_client *client) 332static void init_dll1(struct i2c_client *client)
128{ 333{
129 /* This is the Hauppauge sequence used to 334 /* This is the Hauppauge sequence used to
@@ -420,6 +625,13 @@ static void cx23885_initialize(struct i2c_client *client)
420 625
421 /* start microcontroller */ 626 /* start microcontroller */
422 cx25840_and_or(client, 0x803, ~0x10, 0x10); 627 cx25840_and_or(client, 0x803, ~0x10, 0x10);
628
629 /* Disable and clear video interrupts - we don't use them */
630 cx25840_write4(client, CX25840_VID_INT_STAT_REG, 0xffffffff);
631
632 /* Disable and clear audio interrupts - we don't use them */
633 cx25840_write(client, CX25840_AUD_INT_CTRL_REG, 0xff);
634 cx25840_write(client, CX25840_AUD_INT_STAT_REG, 0xff);
423} 635}
424 636
425/* ----------------------------------------------------------------------- */ 637/* ----------------------------------------------------------------------- */
@@ -909,102 +1121,29 @@ static int set_v4lstd(struct i2c_client *client)
909 1121
910/* ----------------------------------------------------------------------- */ 1122/* ----------------------------------------------------------------------- */
911 1123
912static int cx25840_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 1124static int cx25840_s_ctrl(struct v4l2_ctrl *ctrl)
913{ 1125{
914 struct cx25840_state *state = to_state(sd); 1126 struct v4l2_subdev *sd = to_sd(ctrl);
915 struct i2c_client *client = v4l2_get_subdevdata(sd); 1127 struct i2c_client *client = v4l2_get_subdevdata(sd);
916 1128
917 switch (ctrl->id) { 1129 switch (ctrl->id) {
918 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
919 state->pvr150_workaround = ctrl->value;
920 set_input(client, state->vid_input, state->aud_input);
921 break;
922
923 case V4L2_CID_BRIGHTNESS: 1130 case V4L2_CID_BRIGHTNESS:
924 if (ctrl->value < 0 || ctrl->value > 255) { 1131 cx25840_write(client, 0x414, ctrl->val - 128);
925 v4l_err(client, "invalid brightness setting %d\n",
926 ctrl->value);
927 return -ERANGE;
928 }
929
930 cx25840_write(client, 0x414, ctrl->value - 128);
931 break; 1132 break;
932 1133
933 case V4L2_CID_CONTRAST: 1134 case V4L2_CID_CONTRAST:
934 if (ctrl->value < 0 || ctrl->value > 127) { 1135 cx25840_write(client, 0x415, ctrl->val << 1);
935 v4l_err(client, "invalid contrast setting %d\n",
936 ctrl->value);
937 return -ERANGE;
938 }
939
940 cx25840_write(client, 0x415, ctrl->value << 1);
941 break; 1136 break;
942 1137
943 case V4L2_CID_SATURATION: 1138 case V4L2_CID_SATURATION:
944 if (ctrl->value < 0 || ctrl->value > 127) { 1139 cx25840_write(client, 0x420, ctrl->val << 1);
945 v4l_err(client, "invalid saturation setting %d\n", 1140 cx25840_write(client, 0x421, ctrl->val << 1);
946 ctrl->value);
947 return -ERANGE;
948 }
949
950 cx25840_write(client, 0x420, ctrl->value << 1);
951 cx25840_write(client, 0x421, ctrl->value << 1);
952 break; 1141 break;
953 1142
954 case V4L2_CID_HUE: 1143 case V4L2_CID_HUE:
955 if (ctrl->value < -128 || ctrl->value > 127) { 1144 cx25840_write(client, 0x422, ctrl->val);
956 v4l_err(client, "invalid hue setting %d\n", ctrl->value);
957 return -ERANGE;
958 }
959
960 cx25840_write(client, 0x422, ctrl->value);
961 break; 1145 break;
962 1146
963 case V4L2_CID_AUDIO_VOLUME:
964 case V4L2_CID_AUDIO_BASS:
965 case V4L2_CID_AUDIO_TREBLE:
966 case V4L2_CID_AUDIO_BALANCE:
967 case V4L2_CID_AUDIO_MUTE:
968 if (is_cx2583x(state))
969 return -EINVAL;
970 return cx25840_audio_s_ctrl(sd, ctrl);
971
972 default:
973 return -EINVAL;
974 }
975
976 return 0;
977}
978
979static int cx25840_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
980{
981 struct cx25840_state *state = to_state(sd);
982 struct i2c_client *client = v4l2_get_subdevdata(sd);
983
984 switch (ctrl->id) {
985 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
986 ctrl->value = state->pvr150_workaround;
987 break;
988 case V4L2_CID_BRIGHTNESS:
989 ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
990 break;
991 case V4L2_CID_CONTRAST:
992 ctrl->value = cx25840_read(client, 0x415) >> 1;
993 break;
994 case V4L2_CID_SATURATION:
995 ctrl->value = cx25840_read(client, 0x420) >> 1;
996 break;
997 case V4L2_CID_HUE:
998 ctrl->value = (s8)cx25840_read(client, 0x422);
999 break;
1000 case V4L2_CID_AUDIO_VOLUME:
1001 case V4L2_CID_AUDIO_BASS:
1002 case V4L2_CID_AUDIO_TREBLE:
1003 case V4L2_CID_AUDIO_BALANCE:
1004 case V4L2_CID_AUDIO_MUTE:
1005 if (is_cx2583x(state))
1006 return -EINVAL;
1007 return cx25840_audio_g_ctrl(sd, ctrl);
1008 default: 1147 default:
1009 return -EINVAL; 1148 return -EINVAL;
1010 } 1149 }
@@ -1163,8 +1302,6 @@ static void log_audio_status(struct i2c_client *client)
1163 default: p = "not defined"; 1302 default: p = "not defined";
1164 } 1303 }
1165 v4l_info(client, "Detected audio standard: %s\n", p); 1304 v4l_info(client, "Detected audio standard: %s\n", p);
1166 v4l_info(client, "Audio muted: %s\n",
1167 (state->unmute_volume >= 0) ? "yes" : "no");
1168 v4l_info(client, "Audio microcontroller: %s\n", 1305 v4l_info(client, "Audio microcontroller: %s\n",
1169 (download_ctl & 0x10) ? 1306 (download_ctl & 0x10) ?
1170 ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped"); 1307 ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
@@ -1381,40 +1518,6 @@ static int cx25840_s_stream(struct v4l2_subdev *sd, int enable)
1381 return 0; 1518 return 0;
1382} 1519}
1383 1520
1384static int cx25840_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1385{
1386 struct cx25840_state *state = to_state(sd);
1387
1388 switch (qc->id) {
1389 case V4L2_CID_BRIGHTNESS:
1390 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
1391 case V4L2_CID_CONTRAST:
1392 case V4L2_CID_SATURATION:
1393 return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
1394 case V4L2_CID_HUE:
1395 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
1396 default:
1397 break;
1398 }
1399 if (is_cx2583x(state))
1400 return -EINVAL;
1401
1402 switch (qc->id) {
1403 case V4L2_CID_AUDIO_VOLUME:
1404 return v4l2_ctrl_query_fill(qc, 0, 65535,
1405 65535 / 100, state->default_volume);
1406 case V4L2_CID_AUDIO_MUTE:
1407 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
1408 case V4L2_CID_AUDIO_BALANCE:
1409 case V4L2_CID_AUDIO_BASS:
1410 case V4L2_CID_AUDIO_TREBLE:
1411 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
1412 default:
1413 return -EINVAL;
1414 }
1415 return -EINVAL;
1416}
1417
1418static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std) 1521static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1419{ 1522{
1420 struct cx25840_state *state = to_state(sd); 1523 struct cx25840_state *state = to_state(sd);
@@ -1576,24 +1679,134 @@ static int cx25840_log_status(struct v4l2_subdev *sd)
1576 log_video_status(client); 1679 log_video_status(client);
1577 if (!is_cx2583x(state)) 1680 if (!is_cx2583x(state))
1578 log_audio_status(client); 1681 log_audio_status(client);
1682 cx25840_ir_log_status(sd);
1683 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
1684 return 0;
1685}
1686
1687static int cx25840_s_config(struct v4l2_subdev *sd, int irq, void *platform_data)
1688{
1689 struct cx25840_state *state = to_state(sd);
1690 struct i2c_client *client = v4l2_get_subdevdata(sd);
1691
1692 if (platform_data) {
1693 struct cx25840_platform_data *pdata = platform_data;
1694
1695 state->pvr150_workaround = pdata->pvr150_workaround;
1696 set_input(client, state->vid_input, state->aud_input);
1697 }
1579 return 0; 1698 return 0;
1580} 1699}
1581 1700
1701static int cx23885_irq_handler(struct v4l2_subdev *sd, u32 status,
1702 bool *handled)
1703{
1704 struct cx25840_state *state = to_state(sd);
1705 struct i2c_client *c = v4l2_get_subdevdata(sd);
1706 u8 irq_stat, aud_stat, aud_en, ir_stat, ir_en;
1707 u32 vid_stat, aud_mc_stat;
1708 bool block_handled;
1709 int ret = 0;
1710
1711 irq_stat = cx25840_read(c, CX23885_PIN_CTRL_IRQ_REG);
1712 v4l_dbg(2, cx25840_debug, c, "AV Core IRQ status (entry): %s %s %s\n",
1713 irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT ? "ir" : " ",
1714 irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT ? "aud" : " ",
1715 irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT ? "vid" : " ");
1716
1717 if ((is_cx23885(state) || is_cx23887(state))) {
1718 ir_stat = cx25840_read(c, CX25840_IR_STATS_REG);
1719 ir_en = cx25840_read(c, CX25840_IR_IRQEN_REG);
1720 v4l_dbg(2, cx25840_debug, c,
1721 "AV Core ir IRQ status: %#04x disables: %#04x\n",
1722 ir_stat, ir_en);
1723 if (irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT) {
1724 block_handled = false;
1725 ret = cx25840_ir_irq_handler(sd,
1726 status, &block_handled);
1727 if (block_handled)
1728 *handled = true;
1729 }
1730 }
1731
1732 aud_stat = cx25840_read(c, CX25840_AUD_INT_STAT_REG);
1733 aud_en = cx25840_read(c, CX25840_AUD_INT_CTRL_REG);
1734 v4l_dbg(2, cx25840_debug, c,
1735 "AV Core audio IRQ status: %#04x disables: %#04x\n",
1736 aud_stat, aud_en);
1737 aud_mc_stat = cx25840_read4(c, CX23885_AUD_MC_INT_MASK_REG);
1738 v4l_dbg(2, cx25840_debug, c,
1739 "AV Core audio MC IRQ status: %#06x enables: %#06x\n",
1740 aud_mc_stat >> CX23885_AUD_MC_INT_STAT_SHFT,
1741 aud_mc_stat & CX23885_AUD_MC_INT_CTRL_BITS);
1742 if (irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT) {
1743 if (aud_stat) {
1744 cx25840_write(c, CX25840_AUD_INT_STAT_REG, aud_stat);
1745 *handled = true;
1746 }
1747 }
1748
1749 vid_stat = cx25840_read4(c, CX25840_VID_INT_STAT_REG);
1750 v4l_dbg(2, cx25840_debug, c,
1751 "AV Core video IRQ status: %#06x disables: %#06x\n",
1752 vid_stat & CX25840_VID_INT_STAT_BITS,
1753 vid_stat >> CX25840_VID_INT_MASK_SHFT);
1754 if (irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT) {
1755 if (vid_stat & CX25840_VID_INT_STAT_BITS) {
1756 cx25840_write4(c, CX25840_VID_INT_STAT_REG, vid_stat);
1757 *handled = true;
1758 }
1759 }
1760
1761 irq_stat = cx25840_read(c, CX23885_PIN_CTRL_IRQ_REG);
1762 v4l_dbg(2, cx25840_debug, c, "AV Core IRQ status (exit): %s %s %s\n",
1763 irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT ? "ir" : " ",
1764 irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT ? "aud" : " ",
1765 irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT ? "vid" : " ");
1766
1767 return ret;
1768}
1769
1770static int cx25840_irq_handler(struct v4l2_subdev *sd, u32 status,
1771 bool *handled)
1772{
1773 struct cx25840_state *state = to_state(sd);
1774
1775 *handled = false;
1776
1777 /* Only support the CX2388[578] AV Core for now */
1778 if (is_cx2388x(state))
1779 return cx23885_irq_handler(sd, status, handled);
1780
1781 return -ENODEV;
1782}
1783
1582/* ----------------------------------------------------------------------- */ 1784/* ----------------------------------------------------------------------- */
1583 1785
1786static const struct v4l2_ctrl_ops cx25840_ctrl_ops = {
1787 .s_ctrl = cx25840_s_ctrl,
1788};
1789
1584static const struct v4l2_subdev_core_ops cx25840_core_ops = { 1790static const struct v4l2_subdev_core_ops cx25840_core_ops = {
1585 .log_status = cx25840_log_status, 1791 .log_status = cx25840_log_status,
1792 .s_config = cx25840_s_config,
1586 .g_chip_ident = cx25840_g_chip_ident, 1793 .g_chip_ident = cx25840_g_chip_ident,
1587 .g_ctrl = cx25840_g_ctrl, 1794 .g_ctrl = v4l2_subdev_g_ctrl,
1588 .s_ctrl = cx25840_s_ctrl, 1795 .s_ctrl = v4l2_subdev_s_ctrl,
1589 .queryctrl = cx25840_queryctrl, 1796 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1797 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1798 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1799 .queryctrl = v4l2_subdev_queryctrl,
1800 .querymenu = v4l2_subdev_querymenu,
1590 .s_std = cx25840_s_std, 1801 .s_std = cx25840_s_std,
1591 .reset = cx25840_reset, 1802 .reset = cx25840_reset,
1592 .load_fw = cx25840_load_fw, 1803 .load_fw = cx25840_load_fw,
1804 .s_io_pin_config = common_s_io_pin_config,
1593#ifdef CONFIG_VIDEO_ADV_DEBUG 1805#ifdef CONFIG_VIDEO_ADV_DEBUG
1594 .g_register = cx25840_g_register, 1806 .g_register = cx25840_g_register,
1595 .s_register = cx25840_s_register, 1807 .s_register = cx25840_s_register,
1596#endif 1808#endif
1809 .interrupt_service_routine = cx25840_irq_handler,
1597}; 1810};
1598 1811
1599static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = { 1812static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = {
@@ -1628,6 +1841,7 @@ static const struct v4l2_subdev_ops cx25840_ops = {
1628 .audio = &cx25840_audio_ops, 1841 .audio = &cx25840_audio_ops,
1629 .video = &cx25840_video_ops, 1842 .video = &cx25840_video_ops,
1630 .vbi = &cx25840_vbi_ops, 1843 .vbi = &cx25840_vbi_ops,
1844 .ir = &cx25840_ir_ops,
1631}; 1845};
1632 1846
1633/* ----------------------------------------------------------------------- */ 1847/* ----------------------------------------------------------------------- */
@@ -1675,6 +1889,7 @@ static int cx25840_probe(struct i2c_client *client,
1675{ 1889{
1676 struct cx25840_state *state; 1890 struct cx25840_state *state;
1677 struct v4l2_subdev *sd; 1891 struct v4l2_subdev *sd;
1892 int default_volume;
1678 u32 id = V4L2_IDENT_NONE; 1893 u32 id = V4L2_IDENT_NONE;
1679 u16 device_id; 1894 u16 device_id;
1680 1895
@@ -1718,6 +1933,7 @@ static int cx25840_probe(struct i2c_client *client,
1718 1933
1719 sd = &state->sd; 1934 sd = &state->sd;
1720 v4l2_i2c_subdev_init(sd, client, &cx25840_ops); 1935 v4l2_i2c_subdev_init(sd, client, &cx25840_ops);
1936
1721 switch (id) { 1937 switch (id) {
1722 case V4L2_IDENT_CX23885_AV: 1938 case V4L2_IDENT_CX23885_AV:
1723 v4l_info(client, "cx23885 A/V decoder found @ 0x%x (%s)\n", 1939 v4l_info(client, "cx23885 A/V decoder found @ 0x%x (%s)\n",
@@ -1762,22 +1978,62 @@ static int cx25840_probe(struct i2c_client *client,
1762 state->audclk_freq = 48000; 1978 state->audclk_freq = 48000;
1763 state->pvr150_workaround = 0; 1979 state->pvr150_workaround = 0;
1764 state->audmode = V4L2_TUNER_MODE_LANG1; 1980 state->audmode = V4L2_TUNER_MODE_LANG1;
1765 state->unmute_volume = -1;
1766 state->default_volume = 228 - cx25840_read(client, 0x8d4);
1767 state->default_volume = ((state->default_volume / 2) + 23) << 9;
1768 state->vbi_line_offset = 8; 1981 state->vbi_line_offset = 8;
1769 state->id = id; 1982 state->id = id;
1770 state->rev = device_id; 1983 state->rev = device_id;
1984 v4l2_ctrl_handler_init(&state->hdl, 9);
1985 v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
1986 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1987 v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
1988 V4L2_CID_CONTRAST, 0, 127, 1, 64);
1989 v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
1990 V4L2_CID_SATURATION, 0, 127, 1, 64);
1991 v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
1992 V4L2_CID_HUE, -128, 127, 1, 0);
1993 if (!is_cx2583x(state)) {
1994 default_volume = 228 - cx25840_read(client, 0x8d4);
1995 default_volume = ((default_volume / 2) + 23) << 9;
1996
1997 state->volume = v4l2_ctrl_new_std(&state->hdl,
1998 &cx25840_audio_ctrl_ops, V4L2_CID_AUDIO_VOLUME,
1999 0, 65335, 65535 / 100, default_volume);
2000 state->mute = v4l2_ctrl_new_std(&state->hdl,
2001 &cx25840_audio_ctrl_ops, V4L2_CID_AUDIO_MUTE,
2002 0, 1, 1, 0);
2003 v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,
2004 V4L2_CID_AUDIO_BALANCE,
2005 0, 65535, 65535 / 100, 32768);
2006 v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,
2007 V4L2_CID_AUDIO_BASS,
2008 0, 65535, 65535 / 100, 32768);
2009 v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,
2010 V4L2_CID_AUDIO_TREBLE,
2011 0, 65535, 65535 / 100, 32768);
2012 }
2013 sd->ctrl_handler = &state->hdl;
2014 if (state->hdl.error) {
2015 int err = state->hdl.error;
2016
2017 v4l2_ctrl_handler_free(&state->hdl);
2018 kfree(state);
2019 return err;
2020 }
2021 v4l2_ctrl_cluster(2, &state->volume);
2022 v4l2_ctrl_handler_setup(&state->hdl);
1771 2023
2024 cx25840_ir_probe(sd);
1772 return 0; 2025 return 0;
1773} 2026}
1774 2027
1775static int cx25840_remove(struct i2c_client *client) 2028static int cx25840_remove(struct i2c_client *client)
1776{ 2029{
1777 struct v4l2_subdev *sd = i2c_get_clientdata(client); 2030 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2031 struct cx25840_state *state = to_state(sd);
1778 2032
2033 cx25840_ir_remove(sd);
1779 v4l2_device_unregister_subdev(sd); 2034 v4l2_device_unregister_subdev(sd);
1780 kfree(to_state(sd)); 2035 v4l2_ctrl_handler_free(&state->hdl);
2036 kfree(state);
1781 return 0; 2037 return 0;
1782} 2038}
1783 2039
diff --git a/drivers/media/video/cx25840/cx25840-core.h b/drivers/media/video/cx25840/cx25840-core.h
index 04393b97156..bd4ada28b49 100644
--- a/drivers/media/video/cx25840/cx25840-core.h
+++ b/drivers/media/video/cx25840/cx25840-core.h
@@ -24,19 +24,20 @@
24#include <linux/videodev2.h> 24#include <linux/videodev2.h>
25#include <media/v4l2-device.h> 25#include <media/v4l2-device.h>
26#include <media/v4l2-chip-ident.h> 26#include <media/v4l2-chip-ident.h>
27#include <media/v4l2-ctrls.h>
27#include <linux/i2c.h> 28#include <linux/i2c.h>
28 29
29/* ENABLE_PVR150_WORKAROUND activates a workaround for a hardware bug that is 30struct cx25840_ir_state;
30 present in Hauppauge PVR-150 (and possibly PVR-500) cards that have
31 certain NTSC tuners (tveeprom tuner model numbers 85, 99 and 112). The
32 audio autodetect fails on some channels for these models and the workaround
33 is to select the audio standard explicitly. Many thanks to Hauppauge for
34 providing this information. */
35#define CX25840_CID_ENABLE_PVR150_WORKAROUND (V4L2_CID_PRIVATE_BASE+0)
36 31
37struct cx25840_state { 32struct cx25840_state {
38 struct i2c_client *c; 33 struct i2c_client *c;
39 struct v4l2_subdev sd; 34 struct v4l2_subdev sd;
35 struct v4l2_ctrl_handler hdl;
36 struct {
37 /* volume cluster */
38 struct v4l2_ctrl *volume;
39 struct v4l2_ctrl *mute;
40 };
40 int pvr150_workaround; 41 int pvr150_workaround;
41 int radio; 42 int radio;
42 v4l2_std_id std; 43 v4l2_std_id std;
@@ -44,14 +45,13 @@ struct cx25840_state {
44 enum cx25840_audio_input aud_input; 45 enum cx25840_audio_input aud_input;
45 u32 audclk_freq; 46 u32 audclk_freq;
46 int audmode; 47 int audmode;
47 int unmute_volume; /* -1 if not muted */
48 int default_volume;
49 int vbi_line_offset; 48 int vbi_line_offset;
50 u32 id; 49 u32 id;
51 u32 rev; 50 u32 rev;
52 int is_initialized; 51 int is_initialized;
53 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 */
54 struct work_struct fw_work; /* work entry for fw load */ 53 struct work_struct fw_work; /* work entry for fw load */
54 struct cx25840_ir_state *ir_state;
55}; 55};
56 56
57static inline struct cx25840_state *to_state(struct v4l2_subdev *sd) 57static inline struct cx25840_state *to_state(struct v4l2_subdev *sd)
@@ -59,6 +59,11 @@ static inline struct cx25840_state *to_state(struct v4l2_subdev *sd)
59 return container_of(sd, struct cx25840_state, sd); 59 return container_of(sd, struct cx25840_state, sd);
60} 60}
61 61
62static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
63{
64 return &container_of(ctrl->handler, struct cx25840_state, hdl)->sd;
65}
66
62static inline bool is_cx2583x(struct cx25840_state *state) 67static inline bool is_cx2583x(struct cx25840_state *state)
63{ 68{
64 return state->id == V4L2_IDENT_CX25836 || 69 return state->id == V4L2_IDENT_CX25836 ||
@@ -77,6 +82,21 @@ static inline bool is_cx2388x(struct cx25840_state *state)
77 state->id == V4L2_IDENT_CX23888_AV; 82 state->id == V4L2_IDENT_CX23888_AV;
78} 83}
79 84
85static inline bool is_cx23885(struct cx25840_state *state)
86{
87 return state->id == V4L2_IDENT_CX23885_AV;
88}
89
90static inline bool is_cx23887(struct cx25840_state *state)
91{
92 return state->id == V4L2_IDENT_CX23887_AV;
93}
94
95static inline bool is_cx23888(struct cx25840_state *state)
96{
97 return state->id == V4L2_IDENT_CX23888_AV;
98}
99
80/* ----------------------------------------------------------------------- */ 100/* ----------------------------------------------------------------------- */
81/* cx25850-core.c */ 101/* cx25850-core.c */
82int cx25840_write(struct i2c_client *client, u16 addr, u8 value); 102int cx25840_write(struct i2c_client *client, u16 addr, u8 value);
@@ -84,6 +104,8 @@ int cx25840_write4(struct i2c_client *client, u16 addr, u32 value);
84u8 cx25840_read(struct i2c_client *client, u16 addr); 104u8 cx25840_read(struct i2c_client *client, u16 addr);
85u32 cx25840_read4(struct i2c_client *client, u16 addr); 105u32 cx25840_read4(struct i2c_client *client, u16 addr);
86int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned mask, u8 value); 106int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned mask, u8 value);
107int cx25840_and_or4(struct i2c_client *client, u16 addr, u32 and_mask,
108 u32 or_value);
87void cx25840_std_setup(struct i2c_client *client); 109void cx25840_std_setup(struct i2c_client *client);
88 110
89/* ----------------------------------------------------------------------- */ 111/* ----------------------------------------------------------------------- */
@@ -94,8 +116,8 @@ int cx25840_loadfw(struct i2c_client *client);
94/* cx25850-audio.c */ 116/* cx25850-audio.c */
95void cx25840_audio_set_path(struct i2c_client *client); 117void cx25840_audio_set_path(struct i2c_client *client);
96int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq); 118int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq);
97int cx25840_audio_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl); 119
98int cx25840_audio_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl); 120extern const struct v4l2_ctrl_ops cx25840_audio_ctrl_ops;
99 121
100/* ----------------------------------------------------------------------- */ 122/* ----------------------------------------------------------------------- */
101/* cx25850-vbi.c */ 123/* cx25850-vbi.c */
@@ -104,4 +126,12 @@ int cx25840_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *
104int cx25840_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt); 126int cx25840_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
105int cx25840_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi); 127int cx25840_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi);
106 128
129/* ----------------------------------------------------------------------- */
130/* cx25850-ir.c */
131extern const struct v4l2_subdev_ir_ops cx25840_ir_ops;
132int cx25840_ir_log_status(struct v4l2_subdev *sd);
133int cx25840_ir_irq_handler(struct v4l2_subdev *sd, u32 status, bool *handled);
134int cx25840_ir_probe(struct v4l2_subdev *sd);
135int cx25840_ir_remove(struct v4l2_subdev *sd);
136
107#endif 137#endif
diff --git a/drivers/media/video/cx25840/cx25840-ir.c b/drivers/media/video/cx25840/cx25840-ir.c
new file mode 100644
index 00000000000..c2b4c14dc9a
--- /dev/null
+++ b/drivers/media/video/cx25840/cx25840-ir.c
@@ -0,0 +1,1279 @@
1/*
2 * Driver for the Conexant CX2584x Audio/Video decoder chip and related cores
3 *
4 * Integrated Consumer Infrared Controller
5 *
6 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24#include <linux/slab.h>
25#include <linux/kfifo.h>
26#include <media/cx25840.h>
27#include <media/ir-core.h>
28
29#include "cx25840-core.h"
30
31static unsigned int ir_debug;
32module_param(ir_debug, int, 0644);
33MODULE_PARM_DESC(ir_debug, "enable integrated IR debug messages");
34
35#define CX25840_IR_REG_BASE 0x200
36
37#define CX25840_IR_CNTRL_REG 0x200
38#define CNTRL_WIN_3_3 0x00000000
39#define CNTRL_WIN_4_3 0x00000001
40#define CNTRL_WIN_3_4 0x00000002
41#define CNTRL_WIN_4_4 0x00000003
42#define CNTRL_WIN 0x00000003
43#define CNTRL_EDG_NONE 0x00000000
44#define CNTRL_EDG_FALL 0x00000004
45#define CNTRL_EDG_RISE 0x00000008
46#define CNTRL_EDG_BOTH 0x0000000C
47#define CNTRL_EDG 0x0000000C
48#define CNTRL_DMD 0x00000010
49#define CNTRL_MOD 0x00000020
50#define CNTRL_RFE 0x00000040
51#define CNTRL_TFE 0x00000080
52#define CNTRL_RXE 0x00000100
53#define CNTRL_TXE 0x00000200
54#define CNTRL_RIC 0x00000400
55#define CNTRL_TIC 0x00000800
56#define CNTRL_CPL 0x00001000
57#define CNTRL_LBM 0x00002000
58#define CNTRL_R 0x00004000
59
60#define CX25840_IR_TXCLK_REG 0x204
61#define TXCLK_TCD 0x0000FFFF
62
63#define CX25840_IR_RXCLK_REG 0x208
64#define RXCLK_RCD 0x0000FFFF
65
66#define CX25840_IR_CDUTY_REG 0x20C
67#define CDUTY_CDC 0x0000000F
68
69#define CX25840_IR_STATS_REG 0x210
70#define STATS_RTO 0x00000001
71#define STATS_ROR 0x00000002
72#define STATS_RBY 0x00000004
73#define STATS_TBY 0x00000008
74#define STATS_RSR 0x00000010
75#define STATS_TSR 0x00000020
76
77#define CX25840_IR_IRQEN_REG 0x214
78#define IRQEN_RTE 0x00000001
79#define IRQEN_ROE 0x00000002
80#define IRQEN_RSE 0x00000010
81#define IRQEN_TSE 0x00000020
82#define IRQEN_MSK 0x00000033
83
84#define CX25840_IR_FILTR_REG 0x218
85#define FILTR_LPF 0x0000FFFF
86
87#define CX25840_IR_FIFO_REG 0x23C
88#define FIFO_RXTX 0x0000FFFF
89#define FIFO_RXTX_LVL 0x00010000
90#define FIFO_RXTX_RTO 0x0001FFFF
91#define FIFO_RX_NDV 0x00020000
92#define FIFO_RX_DEPTH 8
93#define FIFO_TX_DEPTH 8
94
95#define CX25840_VIDCLK_FREQ 108000000 /* 108 MHz, BT.656 */
96#define CX25840_IR_REFCLK_FREQ (CX25840_VIDCLK_FREQ / 2)
97
98/*
99 * We use this union internally for convenience, but callers to tx_write
100 * and rx_read will be expecting records of type struct ir_raw_event.
101 * Always ensure the size of this union is dictated by struct ir_raw_event.
102 */
103union cx25840_ir_fifo_rec {
104 u32 hw_fifo_data;
105 struct ir_raw_event ir_core_data;
106};
107
108#define CX25840_IR_RX_KFIFO_SIZE (256 * sizeof(union cx25840_ir_fifo_rec))
109#define CX25840_IR_TX_KFIFO_SIZE (256 * sizeof(union cx25840_ir_fifo_rec))
110
111struct cx25840_ir_state {
112 struct i2c_client *c;
113
114 struct v4l2_subdev_ir_parameters rx_params;
115 struct mutex rx_params_lock; /* protects Rx parameter settings cache */
116 atomic_t rxclk_divider;
117 atomic_t rx_invert;
118
119 struct kfifo rx_kfifo;
120 spinlock_t rx_kfifo_lock; /* protect Rx data kfifo */
121
122 struct v4l2_subdev_ir_parameters tx_params;
123 struct mutex tx_params_lock; /* protects Tx parameter settings cache */
124 atomic_t txclk_divider;
125};
126
127static inline struct cx25840_ir_state *to_ir_state(struct v4l2_subdev *sd)
128{
129 struct cx25840_state *state = to_state(sd);
130 return state ? state->ir_state : NULL;
131}
132
133
134/*
135 * Rx and Tx Clock Divider register computations
136 *
137 * Note the largest clock divider value of 0xffff corresponds to:
138 * (0xffff + 1) * 1000 / 108/2 MHz = 1,213,629.629... ns
139 * which fits in 21 bits, so we'll use unsigned int for time arguments.
140 */
141static inline u16 count_to_clock_divider(unsigned int d)
142{
143 if (d > RXCLK_RCD + 1)
144 d = RXCLK_RCD;
145 else if (d < 2)
146 d = 1;
147 else
148 d--;
149 return (u16) d;
150}
151
152static inline u16 ns_to_clock_divider(unsigned int ns)
153{
154 return count_to_clock_divider(
155 DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ / 1000000 * ns, 1000));
156}
157
158static inline unsigned int clock_divider_to_ns(unsigned int divider)
159{
160 /* Period of the Rx or Tx clock in ns */
161 return DIV_ROUND_CLOSEST((divider + 1) * 1000,
162 CX25840_IR_REFCLK_FREQ / 1000000);
163}
164
165static inline u16 carrier_freq_to_clock_divider(unsigned int freq)
166{
167 return count_to_clock_divider(
168 DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, freq * 16));
169}
170
171static inline unsigned int clock_divider_to_carrier_freq(unsigned int divider)
172{
173 return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, (divider + 1) * 16);
174}
175
176static inline u16 freq_to_clock_divider(unsigned int freq,
177 unsigned int rollovers)
178{
179 return count_to_clock_divider(
180 DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, freq * rollovers));
181}
182
183static inline unsigned int clock_divider_to_freq(unsigned int divider,
184 unsigned int rollovers)
185{
186 return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ,
187 (divider + 1) * rollovers);
188}
189
190/*
191 * Low Pass Filter register calculations
192 *
193 * Note the largest count value of 0xffff corresponds to:
194 * 0xffff * 1000 / 108/2 MHz = 1,213,611.11... ns
195 * which fits in 21 bits, so we'll use unsigned int for time arguments.
196 */
197static inline u16 count_to_lpf_count(unsigned int d)
198{
199 if (d > FILTR_LPF)
200 d = FILTR_LPF;
201 else if (d < 4)
202 d = 0;
203 return (u16) d;
204}
205
206static inline u16 ns_to_lpf_count(unsigned int ns)
207{
208 return count_to_lpf_count(
209 DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ / 1000000 * ns, 1000));
210}
211
212static inline unsigned int lpf_count_to_ns(unsigned int count)
213{
214 /* Duration of the Low Pass Filter rejection window in ns */
215 return DIV_ROUND_CLOSEST(count * 1000,
216 CX25840_IR_REFCLK_FREQ / 1000000);
217}
218
219static inline unsigned int lpf_count_to_us(unsigned int count)
220{
221 /* Duration of the Low Pass Filter rejection window in us */
222 return DIV_ROUND_CLOSEST(count, CX25840_IR_REFCLK_FREQ / 1000000);
223}
224
225/*
226 * FIFO register pulse width count compuations
227 */
228static u32 clock_divider_to_resolution(u16 divider)
229{
230 /*
231 * Resolution is the duration of 1 tick of the readable portion of
232 * of the pulse width counter as read from the FIFO. The two lsb's are
233 * not readable, hence the << 2. This function returns ns.
234 */
235 return DIV_ROUND_CLOSEST((1 << 2) * ((u32) divider + 1) * 1000,
236 CX25840_IR_REFCLK_FREQ / 1000000);
237}
238
239static u64 pulse_width_count_to_ns(u16 count, u16 divider)
240{
241 u64 n;
242 u32 rem;
243
244 /*
245 * The 2 lsb's of the pulse width timer count are not readable, hence
246 * the (count << 2) | 0x3
247 */
248 n = (((u64) count << 2) | 0x3) * (divider + 1) * 1000; /* millicycles */
249 rem = do_div(n, CX25840_IR_REFCLK_FREQ / 1000000); /* / MHz => ns */
250 if (rem >= CX25840_IR_REFCLK_FREQ / 1000000 / 2)
251 n++;
252 return n;
253}
254
255#if 0
256/* Keep as we will need this for Transmit functionality */
257static u16 ns_to_pulse_width_count(u32 ns, u16 divider)
258{
259 u64 n;
260 u32 d;
261 u32 rem;
262
263 /*
264 * The 2 lsb's of the pulse width timer count are not accessable, hence
265 * the (1 << 2)
266 */
267 n = ((u64) ns) * CX25840_IR_REFCLK_FREQ / 1000000; /* millicycles */
268 d = (1 << 2) * ((u32) divider + 1) * 1000; /* millicycles/count */
269 rem = do_div(n, d);
270 if (rem >= d / 2)
271 n++;
272
273 if (n > FIFO_RXTX)
274 n = FIFO_RXTX;
275 else if (n == 0)
276 n = 1;
277 return (u16) n;
278}
279
280#endif
281static unsigned int pulse_width_count_to_us(u16 count, u16 divider)
282{
283 u64 n;
284 u32 rem;
285
286 /*
287 * The 2 lsb's of the pulse width timer count are not readable, hence
288 * the (count << 2) | 0x3
289 */
290 n = (((u64) count << 2) | 0x3) * (divider + 1); /* cycles */
291 rem = do_div(n, CX25840_IR_REFCLK_FREQ / 1000000); /* / MHz => us */
292 if (rem >= CX25840_IR_REFCLK_FREQ / 1000000 / 2)
293 n++;
294 return (unsigned int) n;
295}
296
297/*
298 * Pulse Clocks computations: Combined Pulse Width Count & Rx Clock Counts
299 *
300 * The total pulse clock count is an 18 bit pulse width timer count as the most
301 * significant part and (up to) 16 bit clock divider count as a modulus.
302 * When the Rx clock divider ticks down to 0, it increments the 18 bit pulse
303 * width timer count's least significant bit.
304 */
305static u64 ns_to_pulse_clocks(u32 ns)
306{
307 u64 clocks;
308 u32 rem;
309 clocks = CX25840_IR_REFCLK_FREQ / 1000000 * (u64) ns; /* millicycles */
310 rem = do_div(clocks, 1000); /* /1000 = cycles */
311 if (rem >= 1000 / 2)
312 clocks++;
313 return clocks;
314}
315
316static u16 pulse_clocks_to_clock_divider(u64 count)
317{
318 u32 rem;
319
320 rem = do_div(count, (FIFO_RXTX << 2) | 0x3);
321
322 /* net result needs to be rounded down and decremented by 1 */
323 if (count > RXCLK_RCD + 1)
324 count = RXCLK_RCD;
325 else if (count < 2)
326 count = 1;
327 else
328 count--;
329 return (u16) count;
330}
331
332/*
333 * IR Control Register helpers
334 */
335enum tx_fifo_watermark {
336 TX_FIFO_HALF_EMPTY = 0,
337 TX_FIFO_EMPTY = CNTRL_TIC,
338};
339
340enum rx_fifo_watermark {
341 RX_FIFO_HALF_FULL = 0,
342 RX_FIFO_NOT_EMPTY = CNTRL_RIC,
343};
344
345static inline void control_tx_irq_watermark(struct i2c_client *c,
346 enum tx_fifo_watermark level)
347{
348 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_TIC, level);
349}
350
351static inline void control_rx_irq_watermark(struct i2c_client *c,
352 enum rx_fifo_watermark level)
353{
354 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_RIC, level);
355}
356
357static inline void control_tx_enable(struct i2c_client *c, bool enable)
358{
359 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~(CNTRL_TXE | CNTRL_TFE),
360 enable ? (CNTRL_TXE | CNTRL_TFE) : 0);
361}
362
363static inline void control_rx_enable(struct i2c_client *c, bool enable)
364{
365 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~(CNTRL_RXE | CNTRL_RFE),
366 enable ? (CNTRL_RXE | CNTRL_RFE) : 0);
367}
368
369static inline void control_tx_modulation_enable(struct i2c_client *c,
370 bool enable)
371{
372 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_MOD,
373 enable ? CNTRL_MOD : 0);
374}
375
376static inline void control_rx_demodulation_enable(struct i2c_client *c,
377 bool enable)
378{
379 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_DMD,
380 enable ? CNTRL_DMD : 0);
381}
382
383static inline void control_rx_s_edge_detection(struct i2c_client *c,
384 u32 edge_types)
385{
386 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_EDG_BOTH,
387 edge_types & CNTRL_EDG_BOTH);
388}
389
390static void control_rx_s_carrier_window(struct i2c_client *c,
391 unsigned int carrier,
392 unsigned int *carrier_range_low,
393 unsigned int *carrier_range_high)
394{
395 u32 v;
396 unsigned int c16 = carrier * 16;
397
398 if (*carrier_range_low < DIV_ROUND_CLOSEST(c16, 16 + 3)) {
399 v = CNTRL_WIN_3_4;
400 *carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 4);
401 } else {
402 v = CNTRL_WIN_3_3;
403 *carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 3);
404 }
405
406 if (*carrier_range_high > DIV_ROUND_CLOSEST(c16, 16 - 3)) {
407 v |= CNTRL_WIN_4_3;
408 *carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 4);
409 } else {
410 v |= CNTRL_WIN_3_3;
411 *carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 3);
412 }
413 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_WIN, v);
414}
415
416static inline void control_tx_polarity_invert(struct i2c_client *c,
417 bool invert)
418{
419 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_CPL,
420 invert ? CNTRL_CPL : 0);
421}
422
423/*
424 * IR Rx & Tx Clock Register helpers
425 */
426static unsigned int txclk_tx_s_carrier(struct i2c_client *c,
427 unsigned int freq,
428 u16 *divider)
429{
430 *divider = carrier_freq_to_clock_divider(freq);
431 cx25840_write4(c, CX25840_IR_TXCLK_REG, *divider);
432 return clock_divider_to_carrier_freq(*divider);
433}
434
435static unsigned int rxclk_rx_s_carrier(struct i2c_client *c,
436 unsigned int freq,
437 u16 *divider)
438{
439 *divider = carrier_freq_to_clock_divider(freq);
440 cx25840_write4(c, CX25840_IR_RXCLK_REG, *divider);
441 return clock_divider_to_carrier_freq(*divider);
442}
443
444static u32 txclk_tx_s_max_pulse_width(struct i2c_client *c, u32 ns,
445 u16 *divider)
446{
447 u64 pulse_clocks;
448
449 if (ns > IR_MAX_DURATION)
450 ns = IR_MAX_DURATION;
451 pulse_clocks = ns_to_pulse_clocks(ns);
452 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
453 cx25840_write4(c, CX25840_IR_TXCLK_REG, *divider);
454 return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);
455}
456
457static u32 rxclk_rx_s_max_pulse_width(struct i2c_client *c, u32 ns,
458 u16 *divider)
459{
460 u64 pulse_clocks;
461
462 if (ns > IR_MAX_DURATION)
463 ns = IR_MAX_DURATION;
464 pulse_clocks = ns_to_pulse_clocks(ns);
465 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
466 cx25840_write4(c, CX25840_IR_RXCLK_REG, *divider);
467 return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);
468}
469
470/*
471 * IR Tx Carrier Duty Cycle register helpers
472 */
473static unsigned int cduty_tx_s_duty_cycle(struct i2c_client *c,
474 unsigned int duty_cycle)
475{
476 u32 n;
477 n = DIV_ROUND_CLOSEST(duty_cycle * 100, 625); /* 16ths of 100% */
478 if (n != 0)
479 n--;
480 if (n > 15)
481 n = 15;
482 cx25840_write4(c, CX25840_IR_CDUTY_REG, n);
483 return DIV_ROUND_CLOSEST((n + 1) * 100, 16);
484}
485
486/*
487 * IR Filter Register helpers
488 */
489static u32 filter_rx_s_min_width(struct i2c_client *c, u32 min_width_ns)
490{
491 u32 count = ns_to_lpf_count(min_width_ns);
492 cx25840_write4(c, CX25840_IR_FILTR_REG, count);
493 return lpf_count_to_ns(count);
494}
495
496/*
497 * IR IRQ Enable Register helpers
498 */
499static inline void irqenable_rx(struct v4l2_subdev *sd, u32 mask)
500{
501 struct cx25840_state *state = to_state(sd);
502
503 if (is_cx23885(state) || is_cx23887(state))
504 mask ^= IRQEN_MSK;
505 mask &= (IRQEN_RTE | IRQEN_ROE | IRQEN_RSE);
506 cx25840_and_or4(state->c, CX25840_IR_IRQEN_REG,
507 ~(IRQEN_RTE | IRQEN_ROE | IRQEN_RSE), mask);
508}
509
510static inline void irqenable_tx(struct v4l2_subdev *sd, u32 mask)
511{
512 struct cx25840_state *state = to_state(sd);
513
514 if (is_cx23885(state) || is_cx23887(state))
515 mask ^= IRQEN_MSK;
516 mask &= IRQEN_TSE;
517 cx25840_and_or4(state->c, CX25840_IR_IRQEN_REG, ~IRQEN_TSE, mask);
518}
519
520/*
521 * V4L2 Subdevice IR Ops
522 */
523int cx25840_ir_irq_handler(struct v4l2_subdev *sd, u32 status, bool *handled)
524{
525 struct cx25840_state *state = to_state(sd);
526 struct cx25840_ir_state *ir_state = to_ir_state(sd);
527 struct i2c_client *c = NULL;
528 unsigned long flags;
529
530 union cx25840_ir_fifo_rec rx_data[FIFO_RX_DEPTH];
531 unsigned int i, j, k;
532 u32 events, v;
533 int tsr, rsr, rto, ror, tse, rse, rte, roe, kror;
534 u32 cntrl, irqen, stats;
535
536 *handled = false;
537 if (ir_state == NULL)
538 return -ENODEV;
539
540 c = ir_state->c;
541
542 /* Only support the IR controller for the CX2388[57] AV Core for now */
543 if (!(is_cx23885(state) || is_cx23887(state)))
544 return -ENODEV;
545
546 cntrl = cx25840_read4(c, CX25840_IR_CNTRL_REG);
547 irqen = cx25840_read4(c, CX25840_IR_IRQEN_REG);
548 if (is_cx23885(state) || is_cx23887(state))
549 irqen ^= IRQEN_MSK;
550 stats = cx25840_read4(c, CX25840_IR_STATS_REG);
551
552 tsr = stats & STATS_TSR; /* Tx FIFO Service Request */
553 rsr = stats & STATS_RSR; /* Rx FIFO Service Request */
554 rto = stats & STATS_RTO; /* Rx Pulse Width Timer Time Out */
555 ror = stats & STATS_ROR; /* Rx FIFO Over Run */
556
557 tse = irqen & IRQEN_TSE; /* Tx FIFO Service Request IRQ Enable */
558 rse = irqen & IRQEN_RSE; /* Rx FIFO Service Reuqest IRQ Enable */
559 rte = irqen & IRQEN_RTE; /* Rx Pulse Width Timer Time Out IRQ Enable */
560 roe = irqen & IRQEN_ROE; /* Rx FIFO Over Run IRQ Enable */
561
562 v4l2_dbg(2, ir_debug, sd, "IR IRQ Status: %s %s %s %s %s %s\n",
563 tsr ? "tsr" : " ", rsr ? "rsr" : " ",
564 rto ? "rto" : " ", ror ? "ror" : " ",
565 stats & STATS_TBY ? "tby" : " ",
566 stats & STATS_RBY ? "rby" : " ");
567
568 v4l2_dbg(2, ir_debug, sd, "IR IRQ Enables: %s %s %s %s\n",
569 tse ? "tse" : " ", rse ? "rse" : " ",
570 rte ? "rte" : " ", roe ? "roe" : " ");
571
572 /*
573 * Transmitter interrupt service
574 */
575 if (tse && tsr) {
576 /*
577 * TODO:
578 * Check the watermark threshold setting
579 * Pull FIFO_TX_DEPTH or FIFO_TX_DEPTH/2 entries from tx_kfifo
580 * Push the data to the hardware FIFO.
581 * If there was nothing more to send in the tx_kfifo, disable
582 * the TSR IRQ and notify the v4l2_device.
583 * If there was something in the tx_kfifo, check the tx_kfifo
584 * level and notify the v4l2_device, if it is low.
585 */
586 /* For now, inhibit TSR interrupt until Tx is implemented */
587 irqenable_tx(sd, 0);
588 events = V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ;
589 v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_TX_NOTIFY, &events);
590 *handled = true;
591 }
592
593 /*
594 * Receiver interrupt service
595 */
596 kror = 0;
597 if ((rse && rsr) || (rte && rto)) {
598 /*
599 * Receive data on RSR to clear the STATS_RSR.
600 * Receive data on RTO, since we may not have yet hit the RSR
601 * watermark when we receive the RTO.
602 */
603 for (i = 0, v = FIFO_RX_NDV;
604 (v & FIFO_RX_NDV) && !kror; i = 0) {
605 for (j = 0;
606 (v & FIFO_RX_NDV) && j < FIFO_RX_DEPTH; j++) {
607 v = cx25840_read4(c, CX25840_IR_FIFO_REG);
608 rx_data[i].hw_fifo_data = v & ~FIFO_RX_NDV;
609 i++;
610 }
611 if (i == 0)
612 break;
613 j = i * sizeof(union cx25840_ir_fifo_rec);
614 k = kfifo_in_locked(&ir_state->rx_kfifo,
615 (unsigned char *) rx_data, j,
616 &ir_state->rx_kfifo_lock);
617 if (k != j)
618 kror++; /* rx_kfifo over run */
619 }
620 *handled = true;
621 }
622
623 events = 0;
624 v = 0;
625 if (kror) {
626 events |= V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN;
627 v4l2_err(sd, "IR receiver software FIFO overrun\n");
628 }
629 if (roe && ror) {
630 /*
631 * The RX FIFO Enable (CNTRL_RFE) must be toggled to clear
632 * the Rx FIFO Over Run status (STATS_ROR)
633 */
634 v |= CNTRL_RFE;
635 events |= V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN;
636 v4l2_err(sd, "IR receiver hardware FIFO overrun\n");
637 }
638 if (rte && rto) {
639 /*
640 * The IR Receiver Enable (CNTRL_RXE) must be toggled to clear
641 * the Rx Pulse Width Timer Time Out (STATS_RTO)
642 */
643 v |= CNTRL_RXE;
644 events |= V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED;
645 }
646 if (v) {
647 /* Clear STATS_ROR & STATS_RTO as needed by reseting hardware */
648 cx25840_write4(c, CX25840_IR_CNTRL_REG, cntrl & ~v);
649 cx25840_write4(c, CX25840_IR_CNTRL_REG, cntrl);
650 *handled = true;
651 }
652 spin_lock_irqsave(&ir_state->rx_kfifo_lock, flags);
653 if (kfifo_len(&ir_state->rx_kfifo) >= CX25840_IR_RX_KFIFO_SIZE / 2)
654 events |= V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ;
655 spin_unlock_irqrestore(&ir_state->rx_kfifo_lock, flags);
656
657 if (events)
658 v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_RX_NOTIFY, &events);
659 return 0;
660}
661
662/* Receiver */
663static int cx25840_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,
664 ssize_t *num)
665{
666 struct cx25840_ir_state *ir_state = to_ir_state(sd);
667 bool invert;
668 u16 divider;
669 unsigned int i, n;
670 union cx25840_ir_fifo_rec *p;
671 unsigned u, v;
672
673 if (ir_state == NULL)
674 return -ENODEV;
675
676 invert = (bool) atomic_read(&ir_state->rx_invert);
677 divider = (u16) atomic_read(&ir_state->rxclk_divider);
678
679 n = count / sizeof(union cx25840_ir_fifo_rec)
680 * sizeof(union cx25840_ir_fifo_rec);
681 if (n == 0) {
682 *num = 0;
683 return 0;
684 }
685
686 n = kfifo_out_locked(&ir_state->rx_kfifo, buf, n,
687 &ir_state->rx_kfifo_lock);
688
689 n /= sizeof(union cx25840_ir_fifo_rec);
690 *num = n * sizeof(union cx25840_ir_fifo_rec);
691
692 for (p = (union cx25840_ir_fifo_rec *) buf, i = 0; i < n; p++, i++) {
693
694 if ((p->hw_fifo_data & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {
695 /* Assume RTO was because of no IR light input */
696 u = 0;
697 v4l2_dbg(2, ir_debug, sd, "rx read: end of rx\n");
698 } else {
699 u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;
700 if (invert)
701 u = u ? 0 : 1;
702 }
703
704 v = (unsigned) pulse_width_count_to_ns(
705 (u16) (p->hw_fifo_data & FIFO_RXTX), divider);
706 if (v > IR_MAX_DURATION)
707 v = IR_MAX_DURATION;
708
709 p->ir_core_data.pulse = u;
710 p->ir_core_data.duration = v;
711
712 v4l2_dbg(2, ir_debug, sd, "rx read: %10u ns %s\n",
713 v, u ? "mark" : "space");
714 }
715 return 0;
716}
717
718static int cx25840_ir_rx_g_parameters(struct v4l2_subdev *sd,
719 struct v4l2_subdev_ir_parameters *p)
720{
721 struct cx25840_ir_state *ir_state = to_ir_state(sd);
722
723 if (ir_state == NULL)
724 return -ENODEV;
725
726 mutex_lock(&ir_state->rx_params_lock);
727 memcpy(p, &ir_state->rx_params,
728 sizeof(struct v4l2_subdev_ir_parameters));
729 mutex_unlock(&ir_state->rx_params_lock);
730 return 0;
731}
732
733static int cx25840_ir_rx_shutdown(struct v4l2_subdev *sd)
734{
735 struct cx25840_ir_state *ir_state = to_ir_state(sd);
736 struct i2c_client *c;
737
738 if (ir_state == NULL)
739 return -ENODEV;
740
741 c = ir_state->c;
742 mutex_lock(&ir_state->rx_params_lock);
743
744 /* Disable or slow down all IR Rx circuits and counters */
745 irqenable_rx(sd, 0);
746 control_rx_enable(c, false);
747 control_rx_demodulation_enable(c, false);
748 control_rx_s_edge_detection(c, CNTRL_EDG_NONE);
749 filter_rx_s_min_width(c, 0);
750 cx25840_write4(c, CX25840_IR_RXCLK_REG, RXCLK_RCD);
751
752 ir_state->rx_params.shutdown = true;
753
754 mutex_unlock(&ir_state->rx_params_lock);
755 return 0;
756}
757
758static int cx25840_ir_rx_s_parameters(struct v4l2_subdev *sd,
759 struct v4l2_subdev_ir_parameters *p)
760{
761 struct cx25840_ir_state *ir_state = to_ir_state(sd);
762 struct i2c_client *c;
763 struct v4l2_subdev_ir_parameters *o;
764 u16 rxclk_divider;
765
766 if (ir_state == NULL)
767 return -ENODEV;
768
769 if (p->shutdown)
770 return cx25840_ir_rx_shutdown(sd);
771
772 if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
773 return -ENOSYS;
774
775 c = ir_state->c;
776 o = &ir_state->rx_params;
777
778 mutex_lock(&ir_state->rx_params_lock);
779
780 o->shutdown = p->shutdown;
781
782 p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
783 o->mode = p->mode;
784
785 p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);
786 o->bytes_per_data_element = p->bytes_per_data_element;
787
788 /* Before we tweak the hardware, we have to disable the receiver */
789 irqenable_rx(sd, 0);
790 control_rx_enable(c, false);
791
792 control_rx_demodulation_enable(c, p->modulation);
793 o->modulation = p->modulation;
794
795 if (p->modulation) {
796 p->carrier_freq = rxclk_rx_s_carrier(c, p->carrier_freq,
797 &rxclk_divider);
798
799 o->carrier_freq = p->carrier_freq;
800
801 p->duty_cycle = 50;
802 o->duty_cycle = p->duty_cycle;
803
804 control_rx_s_carrier_window(c, p->carrier_freq,
805 &p->carrier_range_lower,
806 &p->carrier_range_upper);
807 o->carrier_range_lower = p->carrier_range_lower;
808 o->carrier_range_upper = p->carrier_range_upper;
809
810 p->max_pulse_width =
811 (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
812 } else {
813 p->max_pulse_width =
814 rxclk_rx_s_max_pulse_width(c, p->max_pulse_width,
815 &rxclk_divider);
816 }
817 o->max_pulse_width = p->max_pulse_width;
818 atomic_set(&ir_state->rxclk_divider, rxclk_divider);
819
820 p->noise_filter_min_width =
821 filter_rx_s_min_width(c, p->noise_filter_min_width);
822 o->noise_filter_min_width = p->noise_filter_min_width;
823
824 p->resolution = clock_divider_to_resolution(rxclk_divider);
825 o->resolution = p->resolution;
826
827 /* FIXME - make this dependent on resolution for better performance */
828 control_rx_irq_watermark(c, RX_FIFO_HALF_FULL);
829
830 control_rx_s_edge_detection(c, CNTRL_EDG_BOTH);
831
832 o->invert_level = p->invert_level;
833 atomic_set(&ir_state->rx_invert, p->invert_level);
834
835 o->interrupt_enable = p->interrupt_enable;
836 o->enable = p->enable;
837 if (p->enable) {
838 unsigned long flags;
839
840 spin_lock_irqsave(&ir_state->rx_kfifo_lock, flags);
841 kfifo_reset(&ir_state->rx_kfifo);
842 spin_unlock_irqrestore(&ir_state->rx_kfifo_lock, flags);
843 if (p->interrupt_enable)
844 irqenable_rx(sd, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);
845 control_rx_enable(c, p->enable);
846 }
847
848 mutex_unlock(&ir_state->rx_params_lock);
849 return 0;
850}
851
852/* Transmitter */
853static int cx25840_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,
854 ssize_t *num)
855{
856 struct cx25840_ir_state *ir_state = to_ir_state(sd);
857 struct i2c_client *c;
858
859 if (ir_state == NULL)
860 return -ENODEV;
861
862 c = ir_state->c;
863#if 0
864 /*
865 * FIXME - the code below is an incomplete and untested sketch of what
866 * may need to be done. The critical part is to get 4 (or 8) pulses
867 * from the tx_kfifo, or converted from ns to the proper units from the
868 * input, and push them off to the hardware Tx FIFO right away, if the
869 * HW TX fifo needs service. The rest can be pushed to the tx_kfifo in
870 * a less critical timeframe. Also watch out for overruning the
871 * tx_kfifo - don't let it happen and let the caller know not all his
872 * pulses were written.
873 */
874 u32 *ns_pulse = (u32 *) buf;
875 unsigned int n;
876 u32 fifo_pulse[FIFO_TX_DEPTH];
877 u32 mark;
878
879 /* Compute how much we can fit in the tx kfifo */
880 n = CX25840_IR_TX_KFIFO_SIZE - kfifo_len(ir_state->tx_kfifo);
881 n = min(n, (unsigned int) count);
882 n /= sizeof(u32);
883
884 /* FIXME - turn on Tx Fifo service interrupt
885 * check hardware fifo level, and other stuff
886 */
887 for (i = 0; i < n; ) {
888 for (j = 0; j < FIFO_TX_DEPTH / 2 && i < n; j++) {
889 mark = ns_pulse[i] & LEVEL_MASK;
890 fifo_pulse[j] = ns_to_pulse_width_count(
891 ns_pulse[i] &
892 ~LEVEL_MASK,
893 ir_state->txclk_divider);
894 if (mark)
895 fifo_pulse[j] &= FIFO_RXTX_LVL;
896 i++;
897 }
898 kfifo_put(ir_state->tx_kfifo, (u8 *) fifo_pulse,
899 j * sizeof(u32));
900 }
901 *num = n * sizeof(u32);
902#else
903 /* For now enable the Tx FIFO Service interrupt & pretend we did work */
904 irqenable_tx(sd, IRQEN_TSE);
905 *num = count;
906#endif
907 return 0;
908}
909
910static int cx25840_ir_tx_g_parameters(struct v4l2_subdev *sd,
911 struct v4l2_subdev_ir_parameters *p)
912{
913 struct cx25840_ir_state *ir_state = to_ir_state(sd);
914
915 if (ir_state == NULL)
916 return -ENODEV;
917
918 mutex_lock(&ir_state->tx_params_lock);
919 memcpy(p, &ir_state->tx_params,
920 sizeof(struct v4l2_subdev_ir_parameters));
921 mutex_unlock(&ir_state->tx_params_lock);
922 return 0;
923}
924
925static int cx25840_ir_tx_shutdown(struct v4l2_subdev *sd)
926{
927 struct cx25840_ir_state *ir_state = to_ir_state(sd);
928 struct i2c_client *c;
929
930 if (ir_state == NULL)
931 return -ENODEV;
932
933 c = ir_state->c;
934 mutex_lock(&ir_state->tx_params_lock);
935
936 /* Disable or slow down all IR Tx circuits and counters */
937 irqenable_tx(sd, 0);
938 control_tx_enable(c, false);
939 control_tx_modulation_enable(c, false);
940 cx25840_write4(c, CX25840_IR_TXCLK_REG, TXCLK_TCD);
941
942 ir_state->tx_params.shutdown = true;
943
944 mutex_unlock(&ir_state->tx_params_lock);
945 return 0;
946}
947
948static int cx25840_ir_tx_s_parameters(struct v4l2_subdev *sd,
949 struct v4l2_subdev_ir_parameters *p)
950{
951 struct cx25840_ir_state *ir_state = to_ir_state(sd);
952 struct i2c_client *c;
953 struct v4l2_subdev_ir_parameters *o;
954 u16 txclk_divider;
955
956 if (ir_state == NULL)
957 return -ENODEV;
958
959 if (p->shutdown)
960 return cx25840_ir_tx_shutdown(sd);
961
962 if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
963 return -ENOSYS;
964
965 c = ir_state->c;
966 o = &ir_state->tx_params;
967 mutex_lock(&ir_state->tx_params_lock);
968
969 o->shutdown = p->shutdown;
970
971 p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
972 o->mode = p->mode;
973
974 p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);
975 o->bytes_per_data_element = p->bytes_per_data_element;
976
977 /* Before we tweak the hardware, we have to disable the transmitter */
978 irqenable_tx(sd, 0);
979 control_tx_enable(c, false);
980
981 control_tx_modulation_enable(c, p->modulation);
982 o->modulation = p->modulation;
983
984 if (p->modulation) {
985 p->carrier_freq = txclk_tx_s_carrier(c, p->carrier_freq,
986 &txclk_divider);
987 o->carrier_freq = p->carrier_freq;
988
989 p->duty_cycle = cduty_tx_s_duty_cycle(c, p->duty_cycle);
990 o->duty_cycle = p->duty_cycle;
991
992 p->max_pulse_width =
993 (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
994 } else {
995 p->max_pulse_width =
996 txclk_tx_s_max_pulse_width(c, p->max_pulse_width,
997 &txclk_divider);
998 }
999 o->max_pulse_width = p->max_pulse_width;
1000 atomic_set(&ir_state->txclk_divider, txclk_divider);
1001
1002 p->resolution = clock_divider_to_resolution(txclk_divider);
1003 o->resolution = p->resolution;
1004
1005 /* FIXME - make this dependent on resolution for better performance */
1006 control_tx_irq_watermark(c, TX_FIFO_HALF_EMPTY);
1007
1008 control_tx_polarity_invert(c, p->invert_carrier_sense);
1009 o->invert_carrier_sense = p->invert_carrier_sense;
1010
1011 /*
1012 * FIXME: we don't have hardware help for IO pin level inversion
1013 * here like we have on the CX23888.
1014 * Act on this with some mix of logical inversion of data levels,
1015 * carrier polarity, and carrier duty cycle.
1016 */
1017 o->invert_level = p->invert_level;
1018
1019 o->interrupt_enable = p->interrupt_enable;
1020 o->enable = p->enable;
1021 if (p->enable) {
1022 /* reset tx_fifo here */
1023 if (p->interrupt_enable)
1024 irqenable_tx(sd, IRQEN_TSE);
1025 control_tx_enable(c, p->enable);
1026 }
1027
1028 mutex_unlock(&ir_state->tx_params_lock);
1029 return 0;
1030}
1031
1032
1033/*
1034 * V4L2 Subdevice Core Ops support
1035 */
1036int cx25840_ir_log_status(struct v4l2_subdev *sd)
1037{
1038 struct cx25840_state *state = to_state(sd);
1039 struct i2c_client *c = state->c;
1040 char *s;
1041 int i, j;
1042 u32 cntrl, txclk, rxclk, cduty, stats, irqen, filtr;
1043
1044 /* The CX23888 chip doesn't have an IR controller on the A/V core */
1045 if (is_cx23888(state))
1046 return 0;
1047
1048 cntrl = cx25840_read4(c, CX25840_IR_CNTRL_REG);
1049 txclk = cx25840_read4(c, CX25840_IR_TXCLK_REG) & TXCLK_TCD;
1050 rxclk = cx25840_read4(c, CX25840_IR_RXCLK_REG) & RXCLK_RCD;
1051 cduty = cx25840_read4(c, CX25840_IR_CDUTY_REG) & CDUTY_CDC;
1052 stats = cx25840_read4(c, CX25840_IR_STATS_REG);
1053 irqen = cx25840_read4(c, CX25840_IR_IRQEN_REG);
1054 if (is_cx23885(state) || is_cx23887(state))
1055 irqen ^= IRQEN_MSK;
1056 filtr = cx25840_read4(c, CX25840_IR_FILTR_REG) & FILTR_LPF;
1057
1058 v4l2_info(sd, "IR Receiver:\n");
1059 v4l2_info(sd, "\tEnabled: %s\n",
1060 cntrl & CNTRL_RXE ? "yes" : "no");
1061 v4l2_info(sd, "\tDemodulation from a carrier: %s\n",
1062 cntrl & CNTRL_DMD ? "enabled" : "disabled");
1063 v4l2_info(sd, "\tFIFO: %s\n",
1064 cntrl & CNTRL_RFE ? "enabled" : "disabled");
1065 switch (cntrl & CNTRL_EDG) {
1066 case CNTRL_EDG_NONE:
1067 s = "disabled";
1068 break;
1069 case CNTRL_EDG_FALL:
1070 s = "falling edge";
1071 break;
1072 case CNTRL_EDG_RISE:
1073 s = "rising edge";
1074 break;
1075 case CNTRL_EDG_BOTH:
1076 s = "rising & falling edges";
1077 break;
1078 default:
1079 s = "??? edge";
1080 break;
1081 }
1082 v4l2_info(sd, "\tPulse timers' start/stop trigger: %s\n", s);
1083 v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",
1084 cntrl & CNTRL_R ? "not loaded" : "overflow marker");
1085 v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
1086 cntrl & CNTRL_RIC ? "not empty" : "half full or greater");
1087 v4l2_info(sd, "\tLoopback mode: %s\n",
1088 cntrl & CNTRL_LBM ? "loopback active" : "normal receive");
1089 if (cntrl & CNTRL_DMD) {
1090 v4l2_info(sd, "\tExpected carrier (16 clocks): %u Hz\n",
1091 clock_divider_to_carrier_freq(rxclk));
1092 switch (cntrl & CNTRL_WIN) {
1093 case CNTRL_WIN_3_3:
1094 i = 3;
1095 j = 3;
1096 break;
1097 case CNTRL_WIN_4_3:
1098 i = 4;
1099 j = 3;
1100 break;
1101 case CNTRL_WIN_3_4:
1102 i = 3;
1103 j = 4;
1104 break;
1105 case CNTRL_WIN_4_4:
1106 i = 4;
1107 j = 4;
1108 break;
1109 default:
1110 i = 0;
1111 j = 0;
1112 break;
1113 }
1114 v4l2_info(sd, "\tNext carrier edge window: 16 clocks "
1115 "-%1d/+%1d, %u to %u Hz\n", i, j,
1116 clock_divider_to_freq(rxclk, 16 + j),
1117 clock_divider_to_freq(rxclk, 16 - i));
1118 }
1119 v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",
1120 pulse_width_count_to_us(FIFO_RXTX, rxclk),
1121 pulse_width_count_to_ns(FIFO_RXTX, rxclk));
1122 v4l2_info(sd, "\tLow pass filter: %s\n",
1123 filtr ? "enabled" : "disabled");
1124 if (filtr)
1125 v4l2_info(sd, "\tMin acceptable pulse width (LPF): %u us, "
1126 "%u ns\n",
1127 lpf_count_to_us(filtr),
1128 lpf_count_to_ns(filtr));
1129 v4l2_info(sd, "\tPulse width timer timed-out: %s\n",
1130 stats & STATS_RTO ? "yes" : "no");
1131 v4l2_info(sd, "\tPulse width timer time-out intr: %s\n",
1132 irqen & IRQEN_RTE ? "enabled" : "disabled");
1133 v4l2_info(sd, "\tFIFO overrun: %s\n",
1134 stats & STATS_ROR ? "yes" : "no");
1135 v4l2_info(sd, "\tFIFO overrun interrupt: %s\n",
1136 irqen & IRQEN_ROE ? "enabled" : "disabled");
1137 v4l2_info(sd, "\tBusy: %s\n",
1138 stats & STATS_RBY ? "yes" : "no");
1139 v4l2_info(sd, "\tFIFO service requested: %s\n",
1140 stats & STATS_RSR ? "yes" : "no");
1141 v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
1142 irqen & IRQEN_RSE ? "enabled" : "disabled");
1143
1144 v4l2_info(sd, "IR Transmitter:\n");
1145 v4l2_info(sd, "\tEnabled: %s\n",
1146 cntrl & CNTRL_TXE ? "yes" : "no");
1147 v4l2_info(sd, "\tModulation onto a carrier: %s\n",
1148 cntrl & CNTRL_MOD ? "enabled" : "disabled");
1149 v4l2_info(sd, "\tFIFO: %s\n",
1150 cntrl & CNTRL_TFE ? "enabled" : "disabled");
1151 v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
1152 cntrl & CNTRL_TIC ? "not empty" : "half full or less");
1153 v4l2_info(sd, "\tCarrier polarity: %s\n",
1154 cntrl & CNTRL_CPL ? "space:burst mark:noburst"
1155 : "space:noburst mark:burst");
1156 if (cntrl & CNTRL_MOD) {
1157 v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",
1158 clock_divider_to_carrier_freq(txclk));
1159 v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",
1160 cduty + 1);
1161 }
1162 v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",
1163 pulse_width_count_to_us(FIFO_RXTX, txclk),
1164 pulse_width_count_to_ns(FIFO_RXTX, txclk));
1165 v4l2_info(sd, "\tBusy: %s\n",
1166 stats & STATS_TBY ? "yes" : "no");
1167 v4l2_info(sd, "\tFIFO service requested: %s\n",
1168 stats & STATS_TSR ? "yes" : "no");
1169 v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
1170 irqen & IRQEN_TSE ? "enabled" : "disabled");
1171
1172 return 0;
1173}
1174
1175
1176const struct v4l2_subdev_ir_ops cx25840_ir_ops = {
1177 .rx_read = cx25840_ir_rx_read,
1178 .rx_g_parameters = cx25840_ir_rx_g_parameters,
1179 .rx_s_parameters = cx25840_ir_rx_s_parameters,
1180
1181 .tx_write = cx25840_ir_tx_write,
1182 .tx_g_parameters = cx25840_ir_tx_g_parameters,
1183 .tx_s_parameters = cx25840_ir_tx_s_parameters,
1184};
1185
1186
1187static const struct v4l2_subdev_ir_parameters default_rx_params = {
1188 .bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),
1189 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1190
1191 .enable = false,
1192 .interrupt_enable = false,
1193 .shutdown = true,
1194
1195 .modulation = true,
1196 .carrier_freq = 36000, /* 36 kHz - RC-5, and RC-6 carrier */
1197
1198 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
1199 /* RC-6: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
1200 .noise_filter_min_width = 333333, /* ns */
1201 .carrier_range_lower = 35000,
1202 .carrier_range_upper = 37000,
1203 .invert_level = false,
1204};
1205
1206static const struct v4l2_subdev_ir_parameters default_tx_params = {
1207 .bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),
1208 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1209
1210 .enable = false,
1211 .interrupt_enable = false,
1212 .shutdown = true,
1213
1214 .modulation = true,
1215 .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */
1216 .duty_cycle = 25, /* 25 % - RC-5 carrier */
1217 .invert_level = false,
1218 .invert_carrier_sense = false,
1219};
1220
1221int cx25840_ir_probe(struct v4l2_subdev *sd)
1222{
1223 struct cx25840_state *state = to_state(sd);
1224 struct cx25840_ir_state *ir_state;
1225 struct v4l2_subdev_ir_parameters default_params;
1226
1227 /* Only init the IR controller for the CX2388[57] AV Core for now */
1228 if (!(is_cx23885(state) || is_cx23887(state)))
1229 return 0;
1230
1231 ir_state = kzalloc(sizeof(struct cx25840_ir_state), GFP_KERNEL);
1232 if (ir_state == NULL)
1233 return -ENOMEM;
1234
1235 spin_lock_init(&ir_state->rx_kfifo_lock);
1236 if (kfifo_alloc(&ir_state->rx_kfifo,
1237 CX25840_IR_RX_KFIFO_SIZE, GFP_KERNEL)) {
1238 kfree(ir_state);
1239 return -ENOMEM;
1240 }
1241
1242 ir_state->c = state->c;
1243 state->ir_state = ir_state;
1244
1245 /* Ensure no interrupts arrive yet */
1246 if (is_cx23885(state) || is_cx23887(state))
1247 cx25840_write4(ir_state->c, CX25840_IR_IRQEN_REG, IRQEN_MSK);
1248 else
1249 cx25840_write4(ir_state->c, CX25840_IR_IRQEN_REG, 0);
1250
1251 mutex_init(&ir_state->rx_params_lock);
1252 memcpy(&default_params, &default_rx_params,
1253 sizeof(struct v4l2_subdev_ir_parameters));
1254 v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);
1255
1256 mutex_init(&ir_state->tx_params_lock);
1257 memcpy(&default_params, &default_tx_params,
1258 sizeof(struct v4l2_subdev_ir_parameters));
1259 v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);
1260
1261 return 0;
1262}
1263
1264int cx25840_ir_remove(struct v4l2_subdev *sd)
1265{
1266 struct cx25840_state *state = to_state(sd);
1267 struct cx25840_ir_state *ir_state = to_ir_state(sd);
1268
1269 if (ir_state == NULL)
1270 return -ENODEV;
1271
1272 cx25840_ir_rx_shutdown(sd);
1273 cx25840_ir_tx_shutdown(sd);
1274
1275 kfifo_free(&ir_state->rx_kfifo);
1276 kfree(ir_state);
1277 state->ir_state = NULL;
1278 return 0;
1279}
diff --git a/drivers/media/video/gspca/gspca.c b/drivers/media/video/gspca/gspca.c
index d951b0f0e05..b9846106913 100644
--- a/drivers/media/video/gspca/gspca.c
+++ b/drivers/media/video/gspca/gspca.c
@@ -55,7 +55,7 @@ MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
55MODULE_DESCRIPTION("GSPCA USB Camera Driver"); 55MODULE_DESCRIPTION("GSPCA USB Camera Driver");
56MODULE_LICENSE("GPL"); 56MODULE_LICENSE("GPL");
57 57
58#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 9, 0) 58#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 10, 0)
59 59
60#ifdef GSPCA_DEBUG 60#ifdef GSPCA_DEBUG
61int gspca_debug = D_ERR | D_PROBE; 61int gspca_debug = D_ERR | D_PROBE;
@@ -440,10 +440,15 @@ void gspca_frame_add(struct gspca_dev *gspca_dev,
440 frame->v4l2_buf.sequence = ++gspca_dev->sequence; 440 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
441 gspca_dev->image = frame->data; 441 gspca_dev->image = frame->data;
442 gspca_dev->image_len = 0; 442 gspca_dev->image_len = 0;
443 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) { 443 } else {
444 if (packet_type == LAST_PACKET) 444 switch (gspca_dev->last_packet_type) {
445 gspca_dev->last_packet_type = packet_type; 445 case DISCARD_PACKET:
446 return; 446 if (packet_type == LAST_PACKET)
447 gspca_dev->last_packet_type = packet_type;
448 return;
449 case LAST_PACKET:
450 return;
451 }
447 } 452 }
448 453
449 /* append the packet to the frame buffer */ 454 /* append the packet to the frame buffer */
@@ -454,6 +459,12 @@ void gspca_frame_add(struct gspca_dev *gspca_dev,
454 gspca_dev->frsz); 459 gspca_dev->frsz);
455 packet_type = DISCARD_PACKET; 460 packet_type = DISCARD_PACKET;
456 } else { 461 } else {
462/* !! image is NULL only when last pkt is LAST or DISCARD
463 if (gspca_dev->image == NULL) {
464 err("gspca_frame_add() image == NULL");
465 return;
466 }
467 */
457 memcpy(gspca_dev->image + gspca_dev->image_len, 468 memcpy(gspca_dev->image + gspca_dev->image_len,
458 data, len); 469 data, len);
459 gspca_dev->image_len += len; 470 gspca_dev->image_len += len;
diff --git a/drivers/media/video/gspca/sonixj.c b/drivers/media/video/gspca/sonixj.c
index ee17b034bf6..370544361be 100644
--- a/drivers/media/video/gspca/sonixj.c
+++ b/drivers/media/video/gspca/sonixj.c
@@ -66,7 +66,11 @@ struct sd {
66#define BRIDGE_SN9C110 2 66#define BRIDGE_SN9C110 2
67#define BRIDGE_SN9C120 3 67#define BRIDGE_SN9C120 3
68 u8 sensor; /* Type of image sensor chip */ 68 u8 sensor; /* Type of image sensor chip */
69enum { 69 u8 i2c_addr;
70
71 u8 jpeg_hdr[JPEG_HDR_SZ];
72};
73enum sensors {
70 SENSOR_ADCM1700, 74 SENSOR_ADCM1700,
71 SENSOR_GC0307, 75 SENSOR_GC0307,
72 SENSOR_HV7131R, 76 SENSOR_HV7131R,
@@ -81,10 +85,6 @@ enum {
81 SENSOR_PO2030N, 85 SENSOR_PO2030N,
82 SENSOR_SOI768, 86 SENSOR_SOI768,
83 SENSOR_SP80708, 87 SENSOR_SP80708,
84} sensors;
85 u8 i2c_addr;
86
87 u8 jpeg_hdr[JPEG_HDR_SZ];
88}; 88};
89 89
90/* V4L2 controls supported by the driver */ 90/* V4L2 controls supported by the driver */
diff --git a/drivers/media/video/gspca/sq930x.c b/drivers/media/video/gspca/sq930x.c
index 37cee5e063c..7ae6522d4ed 100644
--- a/drivers/media/video/gspca/sq930x.c
+++ b/drivers/media/video/gspca/sq930x.c
@@ -23,7 +23,6 @@
23#define MODULE_NAME "sq930x" 23#define MODULE_NAME "sq930x"
24 24
25#include "gspca.h" 25#include "gspca.h"
26#include "jpeg.h"
27 26
28MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>\n" 27MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>\n"
29 "Gerard Klaver <gerard at gkall dot hobby dot nl\n" 28 "Gerard Klaver <gerard at gkall dot hobby dot nl\n"
@@ -31,8 +30,6 @@ MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>\n"
31MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver"); 30MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver");
32MODULE_LICENSE("GPL"); 31MODULE_LICENSE("GPL");
33 32
34#define BULK_TRANSFER_LEN 5128
35
36/* Structure to hold all of our device specific stuff */ 33/* Structure to hold all of our device specific stuff */
37struct sd { 34struct sd {
38 struct gspca_dev gspca_dev; /* !! must be the first item */ 35 struct gspca_dev gspca_dev; /* !! must be the first item */
@@ -40,28 +37,20 @@ struct sd {
40 u16 expo; 37 u16 expo;
41 u8 gain; 38 u8 gain;
42 39
43 u8 quality; /* webcam quality 0..3 */
44#define QUALITY_DEF 1
45
46 u8 gpio[2];
47
48 u8 eof_len;
49 u8 do_ctrl; 40 u8 do_ctrl;
50 41 u8 gpio[2];
51 u8 sensor; 42 u8 sensor;
52enum { 43 u8 type;
44#define Generic 0
45#define Creative_live_motion 1
46};
47enum sensors {
53 SENSOR_ICX098BQ, 48 SENSOR_ICX098BQ,
54 SENSOR_LZ24BP, 49 SENSOR_LZ24BP,
55 SENSOR_MI0360, 50 SENSOR_MI0360,
56 SENSOR_MT9V111, 51 SENSOR_MT9V111, /* = MI360SOC */
57 SENSOR_OV7660, 52 SENSOR_OV7660,
58 SENSOR_OV9630, 53 SENSOR_OV9630,
59} sensors;
60 u8 type;
61#define Generic 0
62#define Creative_live_motion 1
63
64 u8 jpeg_hdr[JPEG_HDR_SZ];
65}; 54};
66 55
67static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val); 56static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val);
@@ -78,7 +67,7 @@ static const struct ctrl sd_ctrls[] = {
78 .minimum = 0x0001, 67 .minimum = 0x0001,
79 .maximum = 0x0fff, 68 .maximum = 0x0fff,
80 .step = 1, 69 .step = 1,
81#define EXPO_DEF 0x027d 70#define EXPO_DEF 0x0356
82 .default_value = EXPO_DEF, 71 .default_value = EXPO_DEF,
83 }, 72 },
84 .set = sd_setexpo, 73 .set = sd_setexpo,
@@ -92,7 +81,7 @@ static const struct ctrl sd_ctrls[] = {
92 .minimum = 0x01, 81 .minimum = 0x01,
93 .maximum = 0xff, 82 .maximum = 0xff,
94 .step = 1, 83 .step = 1,
95#define GAIN_DEF 0x61 84#define GAIN_DEF 0x8d
96 .default_value = GAIN_DEF, 85 .default_value = GAIN_DEF,
97 }, 86 },
98 .set = sd_setgain, 87 .set = sd_setgain,
@@ -101,30 +90,18 @@ static const struct ctrl sd_ctrls[] = {
101}; 90};
102 91
103static struct v4l2_pix_format vga_mode[] = { 92static struct v4l2_pix_format vga_mode[] = {
104 {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 93 {320, 240, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
105 .bytesperline = 160,
106 .sizeimage = 160 * 120 * 5 / 8 + 590,
107 .colorspace = V4L2_COLORSPACE_JPEG,
108 .priv = 0},
109 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
110 .bytesperline = 320, 94 .bytesperline = 320,
111 .sizeimage = 320 * 240 * 4 / 8 + 590, 95 .sizeimage = 320 * 240,
112 .colorspace = V4L2_COLORSPACE_JPEG, 96 .colorspace = V4L2_COLORSPACE_SRGB,
113 .priv = 1}, 97 .priv = 0},
114 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 98 {640, 480, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
115 .bytesperline = 640, 99 .bytesperline = 640,
116 .sizeimage = 640 * 480 * 3 / 8 + 590, 100 .sizeimage = 640 * 480,
117 .colorspace = V4L2_COLORSPACE_JPEG, 101 .colorspace = V4L2_COLORSPACE_SRGB,
118 .priv = 2}, 102 .priv = 1},
119}; 103};
120 104
121/* JPEG quality indexed by webcam quality */
122#define QUAL_0 90
123#define QUAL_1 85
124#define QUAL_2 75
125#define QUAL_3 70
126static const u8 quality_tb[4] = { QUAL_0, QUAL_1, QUAL_2, QUAL_3 };
127
128/* sq930x registers */ 105/* sq930x registers */
129#define SQ930_CTRL_UCBUS_IO 0x0001 106#define SQ930_CTRL_UCBUS_IO 0x0001
130#define SQ930_CTRL_I2C_IO 0x0002 107#define SQ930_CTRL_I2C_IO 0x0002
@@ -302,7 +279,7 @@ static const struct i2c_write_cmd mt9v111_init_0[] = {
302 {0x01, 0x0001}, /* select IFP/SOC registers */ 279 {0x01, 0x0001}, /* select IFP/SOC registers */
303 {0x06, 0x300c}, /* operating mode control */ 280 {0x06, 0x300c}, /* operating mode control */
304 {0x08, 0xcc00}, /* output format control (RGB) */ 281 {0x08, 0xcc00}, /* output format control (RGB) */
305 {0x01, 0x0004}, /* select core registers */ 282 {0x01, 0x0004}, /* select sensor core registers */
306}; 283};
307static const struct i2c_write_cmd mt9v111_init_1[] = { 284static const struct i2c_write_cmd mt9v111_init_1[] = {
308 {0x03, 0x01e5}, /* window height */ 285 {0x03, 0x01e5}, /* window height */
@@ -330,7 +307,8 @@ static const struct i2c_write_cmd mt9v111_init_3[] = {
330 {0x62, 0x0405}, 307 {0x62, 0x0405},
331}; 308};
332static const struct i2c_write_cmd mt9v111_init_4[] = { 309static const struct i2c_write_cmd mt9v111_init_4[] = {
333 {0x05, 0x00ce}, /* horizontal blanking */ 310/* {0x05, 0x00ce}, */
311 {0x05, 0x005d}, /* horizontal blanking */
334}; 312};
335 313
336static const struct ucbus_write_cmd ov7660_start_0[] = { 314static const struct ucbus_write_cmd ov7660_start_0[] = {
@@ -343,78 +321,58 @@ static const struct ucbus_write_cmd ov9630_start_0[] = {
343 {0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03} 321 {0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03}
344}; 322};
345 323
324/* start parameters indexed by [sensor][mode] */
346static const struct cap_s { 325static const struct cap_s {
347 u8 cc_sizeid; 326 u8 cc_sizeid;
348 u8 cc_bytes[32]; 327 u8 cc_bytes[32];
349} capconfig[4][3] = { 328} capconfig[4][2] = {
350 [SENSOR_ICX098BQ] = { 329 [SENSOR_ICX098BQ] = {
351 {0, /* JPEG, 160x120 */ 330 {2, /* Bayer 320x240 */
331 {0x05, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
332 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
333 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0,
334 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
335 {4, /* Bayer 640x480 */
352 {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee, 336 {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
353 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8, 337 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
354 0x02, 0x8b, 0x00, 0x8b, 0x00, 0x41, 0x01, 0x41, 338 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355 0x01, 0x41, 0x01, 0x05, 0x40, 0x01, 0xf0, 0x00} }, 339 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
356 {2, /* JPEG, 320x240 */
357 {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
358 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
359 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
360 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
361 {4, /* JPEG, 640x480 */
362 {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xf0,
363 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
364 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
365 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
366 }, 340 },
367 [SENSOR_LZ24BP] = { 341 [SENSOR_LZ24BP] = {
368 {0, /* JPEG, 160x120 */ 342 {2, /* Bayer 320x240 */
369 {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee, 343 {0x05, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
370 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8, 344 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
371 0x02, 0x8b, 0x00, 0x8b, 0x00, 0x41, 0x01, 0x41, 345 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
372 0x01, 0x41, 0x01, 0x05, 0x40, 0x01, 0xf0, 0x00} }, 346 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
373 {2, /* JPEG, 320x240 */ 347 {4, /* Bayer 640x480 */
374 {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee, 348 {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
375 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8, 349 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
376 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f, 350 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
377 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} }, 351 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
378 {4, /* JPEG, 640x480 */
379 {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xf0,
380 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
381 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
382 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
383 }, 352 },
384 [SENSOR_MI0360] = { 353 [SENSOR_MI0360] = {
385 {0, /* JPEG, 160x120 */ 354 {2, /* Bayer 320x240 */
386 {0x05, 0x3d, 0x20, 0x0b, 0x00, 0xbd, 0x02, 0x0b, 355 {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
387 0x02, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8, 356 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
388 0x02, 0x01, 0x01, 0x01, 0x01, 0x9f, 0x00, 0x9f, 357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
389 0x00, 0x9f, 0x01, 0x05, 0xa0, 0x00, 0x80, 0x00} }, 358 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
390 {2, /* JPEG, 320x240 */ 359 {4, /* Bayer 640x480 */
391 {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1, 360 {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
392/*fixme 03 e3 */
393 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8, 361 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
394 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f, 362 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
395 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} }, 363 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
396 {4, /* JPEG, 640x480 */
397 {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe3,
398 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
399 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
400 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
401 }, 364 },
402 [SENSOR_MT9V111] = { 365 [SENSOR_MT9V111] = {
403 {0, /* JPEG, 160x120 */ 366 {2, /* Bayer 320x240 */
404 {0x05, 0x3d, 0x20, 0x0b, 0x00, 0xbd, 0x02, 0x0b, 367 {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
405 0x02, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8, 368 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
406 0x02, 0x01, 0x01, 0x01, 0x01, 0x9f, 0x00, 0x9f, 369 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
407 0x00, 0x9f, 0x01, 0x05, 0xa0, 0x00, 0x80, 0x00} }, 370 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
408 {2, /* JPEG, 320x240 */ 371 {4, /* Bayer 640x480 */
409 {0x01, 0x02, 0x20, 0x03, 0x20, 0x82, 0x02, 0xe3, 372 {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
410 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
411 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
412 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
413 {4, /* JPEG, 640x480 */
414 {0x01, 0x02, 0x20, 0x03, 0x20, 0x82, 0x02, 0xe3,
415 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8, 373 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
416 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f, 374 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
417 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} }, 375 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
418 }, 376 },
419}; 377};
420 378
@@ -864,7 +822,7 @@ static void setexposure(struct gspca_dev *gspca_dev)
864 buf[i++] = 0x35; /* reg = global gain */ 822 buf[i++] = 0x35; /* reg = global gain */
865 buf[i++] = 0x00; /* val H */ 823 buf[i++] = 0x00; /* val H */
866 buf[i++] = sensor->i2c_dum; 824 buf[i++] = sensor->i2c_dum;
867 buf[i++] = sd->gain; /* val L */ 825 buf[i++] = 0x80 + sd->gain / 2; /* val L */
868 buf[i++] = 0x00; 826 buf[i++] = 0x00;
869 buf[i++] = 0x00; 827 buf[i++] = 0x00;
870 buf[i++] = 0x00; 828 buf[i++] = 0x00;
@@ -889,10 +847,7 @@ static int sd_config(struct gspca_dev *gspca_dev,
889 cam->nmodes = ARRAY_SIZE(vga_mode); 847 cam->nmodes = ARRAY_SIZE(vga_mode);
890 848
891 cam->bulk = 1; 849 cam->bulk = 1;
892 cam->bulk_size = BULK_TRANSFER_LEN;
893/* cam->bulk_nurbs = 2; fixme: if no setexpo sync */
894 850
895 sd->quality = QUALITY_DEF;
896 sd->gain = GAIN_DEF; 851 sd->gain = GAIN_DEF;
897 sd->expo = EXPO_DEF; 852 sd->expo = EXPO_DEF;
898 853
@@ -945,13 +900,10 @@ static int sd_init(struct gspca_dev *gspca_dev)
945 if (sd->sensor == SENSOR_MI0360) { 900 if (sd->sensor == SENSOR_MI0360) {
946 901
947 /* no sensor probe for icam tracer */ 902 /* no sensor probe for icam tracer */
948 if (gspca_dev->usb_buf[5] == 0xf6) { /* if CMOS */ 903 if (gspca_dev->usb_buf[5] == 0xf6) /* if CMOS */
949 sd->sensor = SENSOR_ICX098BQ; 904 sd->sensor = SENSOR_ICX098BQ;
950 gspca_dev->cam.cam_mode = &vga_mode[1]; 905 else
951 gspca_dev->cam.nmodes = 1; /* only 320x240 */
952 } else {
953 cmos_probe(gspca_dev); 906 cmos_probe(gspca_dev);
954 }
955 } 907 }
956 908
957 PDEBUG(D_PROBE, "Sensor %s", sensor_tb[sd->sensor].name); 909 PDEBUG(D_PROBE, "Sensor %s", sensor_tb[sd->sensor].name);
@@ -960,51 +912,24 @@ static int sd_init(struct gspca_dev *gspca_dev)
960 return gspca_dev->usb_err; 912 return gspca_dev->usb_err;
961} 913}
962 914
963/* special function to create the quantization tables of the JPEG header */
964static void sd_jpeg_set_qual(u8 *jpeg_hdr,
965 int quality)
966{
967 int i, sc1, sc2;
968
969 quality = quality_tb[quality]; /* convert to JPEG quality */
970/*
971 * approximative qualities for Y and U/V:
972 * quant = 0:94%/91% 1:91%/87% 2:82%/73% 3:69%/56%
973 * should have:
974 * quant = 0:94%/91% 1:91%/87.5% 2:81.5%/72% 3:69%/54.5%
975 */
976 sc1 = 200 - quality * 2;
977 quality = quality * 7 / 5 - 40; /* UV quality */
978 sc2 = 200 - quality * 2;
979 for (i = 0; i < 64; i++) {
980 jpeg_hdr[JPEG_QT0_OFFSET + i] =
981 (jpeg_head[JPEG_QT0_OFFSET + i] * sc1 + 50) / 100;
982 jpeg_hdr[JPEG_QT1_OFFSET + i] =
983 (jpeg_head[JPEG_QT1_OFFSET + i] * sc2 + 50) / 100;
984 }
985}
986
987/* send the start/stop commands to the webcam */ 915/* send the start/stop commands to the webcam */
988static void send_start(struct gspca_dev *gspca_dev) 916static void send_start(struct gspca_dev *gspca_dev)
989{ 917{
990 struct sd *sd = (struct sd *) gspca_dev; 918 struct sd *sd = (struct sd *) gspca_dev;
991 const struct cap_s *cap; 919 const struct cap_s *cap;
992 int mode, quality; 920 int mode;
993 921
994 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv; 922 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
995 cap = &capconfig[sd->sensor][mode]; 923 cap = &capconfig[sd->sensor][mode];
996 quality = sd->quality; 924 reg_wb(gspca_dev, 0x0900 | SQ930_CTRL_CAP_START,
997 reg_wb(gspca_dev, (quality << 12) 925 0x0a00 | cap->cc_sizeid,
998 | 0x0a00 /* 900 for Bayer */
999 | SQ930_CTRL_CAP_START,
1000 0x0500 /* a00 for Bayer */
1001 | cap->cc_sizeid,
1002 cap->cc_bytes, 32); 926 cap->cc_bytes, 32);
1003}; 927}
928
1004static void send_stop(struct gspca_dev *gspca_dev) 929static void send_stop(struct gspca_dev *gspca_dev)
1005{ 930{
1006 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0); 931 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
1007}; 932}
1008 933
1009/* function called at start time before URB creation */ 934/* function called at start time before URB creation */
1010static int sd_isoc_init(struct gspca_dev *gspca_dev) 935static int sd_isoc_init(struct gspca_dev *gspca_dev)
@@ -1013,6 +938,7 @@ static int sd_isoc_init(struct gspca_dev *gspca_dev)
1013 938
1014 gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */ 939 gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */
1015 sd->do_ctrl = 0; 940 sd->do_ctrl = 0;
941 gspca_dev->cam.bulk_size = gspca_dev->width * gspca_dev->height + 8;
1016 return 0; 942 return 0;
1017} 943}
1018 944
@@ -1022,11 +948,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
1022 struct sd *sd = (struct sd *) gspca_dev; 948 struct sd *sd = (struct sd *) gspca_dev;
1023 int mode; 949 int mode;
1024 950
1025 /* initialize the JPEG header */
1026 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
1027 0x21); /* JPEG 422 */
1028 sd_jpeg_set_qual(sd->jpeg_hdr, sd->quality);
1029
1030 bridge_init(sd); 951 bridge_init(sd);
1031 global_init(sd, 0); 952 global_init(sd, 0);
1032 msleep(100); 953 msleep(100);
@@ -1071,7 +992,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
1071 ARRAY_SIZE(lz24bp_start_2), 992 ARRAY_SIZE(lz24bp_start_2),
1072 6); 993 6);
1073 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv; 994 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
1074 lz24bp_ppl(sd, mode == 2 ? 0x0564 : 0x0310); 995 lz24bp_ppl(sd, mode == 1 ? 0x0564 : 0x0310);
1075 msleep(10); 996 msleep(10);
1076 break; 997 break;
1077 case SENSOR_MI0360: 998 case SENSOR_MI0360:
@@ -1095,7 +1016,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
1095 /* 1st start */ 1016 /* 1st start */
1096 send_start(gspca_dev); 1017 send_start(gspca_dev);
1097 msleep(60); 1018 msleep(60);
1098 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000); 1019 send_stop(gspca_dev);
1099 1020
1100 i2c_write(sd, 1021 i2c_write(sd,
1101 mi0360_start_4, ARRAY_SIZE(mi0360_start_4)); 1022 mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
@@ -1113,7 +1034,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
1113 ARRAY_SIZE(mt9v111_init_2)); 1034 ARRAY_SIZE(mt9v111_init_2));
1114 ucbus_write(gspca_dev, mt9v111_start_1, 1035 ucbus_write(gspca_dev, mt9v111_start_1,
1115 ARRAY_SIZE(mt9v111_start_1), 1036 ARRAY_SIZE(mt9v111_start_1),
1116 8); 1037 5);
1117 i2c_write(sd, mt9v111_init_3, 1038 i2c_write(sd, mt9v111_init_3,
1118 ARRAY_SIZE(mt9v111_init_3)); 1039 ARRAY_SIZE(mt9v111_init_3));
1119 i2c_write(sd, mt9v111_init_4, 1040 i2c_write(sd, mt9v111_init_4,
@@ -1125,8 +1046,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
1125out: 1046out:
1126 msleep(1000); 1047 msleep(1000);
1127 1048
1128 sd->eof_len = 0; /* init packet scan */
1129
1130 if (sd->sensor == SENSOR_MT9V111) 1049 if (sd->sensor == SENSOR_MT9V111)
1131 gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED); 1050 gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
1132 1051
@@ -1166,94 +1085,17 @@ static void sd_dq_callback(struct gspca_dev *gspca_dev)
1166 msleep(100); 1085 msleep(100);
1167} 1086}
1168 1087
1169/* move a packet adding 0x00 after 0xff */
1170static void add_packet(struct gspca_dev *gspca_dev,
1171 u8 *data,
1172 int len)
1173{
1174 int i;
1175
1176 i = 0;
1177 do {
1178 if (data[i] == 0xff) {
1179 gspca_frame_add(gspca_dev, INTER_PACKET,
1180 data, i + 1);
1181 len -= i;
1182 data += i;
1183 *data = 0x00;
1184 i = 0;
1185 }
1186 } while (++i < len);
1187 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1188}
1189
1190/* end a frame and start a new one */
1191static void eof_sof(struct gspca_dev *gspca_dev)
1192{
1193 struct sd *sd = (struct sd *) gspca_dev;
1194 static const u8 ffd9[] = {0xff, 0xd9};
1195
1196 /* if control set, stop bulk transfer */
1197 if (sd->do_ctrl
1198 && gspca_dev->last_packet_type == INTER_PACKET)
1199 gspca_dev->cam.bulk_nurbs = 0;
1200 gspca_frame_add(gspca_dev, LAST_PACKET,
1201 ffd9, 2);
1202 gspca_frame_add(gspca_dev, FIRST_PACKET,
1203 sd->jpeg_hdr, JPEG_HDR_SZ);
1204}
1205
1206static void sd_pkt_scan(struct gspca_dev *gspca_dev, 1088static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1207 u8 *data, /* isoc packet */ 1089 u8 *data, /* isoc packet */
1208 int len) /* iso packet length */ 1090 int len) /* iso packet length */
1209{ 1091{
1210 struct sd *sd = (struct sd *) gspca_dev; 1092 struct sd *sd = (struct sd *) gspca_dev;
1211 u8 *p; 1093
1212 int l; 1094 if (sd->do_ctrl)
1213 1095 gspca_dev->cam.bulk_nurbs = 0;
1214 len -= 8; /* ignore last 8 bytes (00 00 55 aa 55 aa 00 00) */ 1096 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
1215 1097 gspca_frame_add(gspca_dev, INTER_PACKET, data, len - 8);
1216 /* 1098 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
1217 * the end/start of frame is indicated by
1218 * 0x00 * 16 - 0xab * 8
1219 * aligned on 8 bytes boundary
1220 */
1221 if (sd->eof_len != 0) { /* if 'abababab' in previous pkt */
1222 if (*((u32 *) data) == 0xabababab) {
1223 /*fixme: should remove previous 0000ababab*/
1224 eof_sof(gspca_dev);
1225 data += 4;
1226 len -= 4;
1227 }
1228 sd->eof_len = 0;
1229 }
1230 p = data;
1231 l = len;
1232 for (;;) {
1233 if (*((u32 *) p) == 0xabababab) {
1234 if (l < 8) { /* (may be 4 only) */
1235 sd->eof_len = 1;
1236 break;
1237 }
1238 if (*((u32 *) p + 1) == 0xabababab) {
1239 add_packet(gspca_dev, data, p - data - 16);
1240 /* remove previous zeros */
1241 eof_sof(gspca_dev);
1242 p += 8;
1243 l -= 8;
1244 if (l <= 0)
1245 return;
1246 len = l;
1247 data = p;
1248 continue;
1249 }
1250 }
1251 p += 4;
1252 l -= 4;
1253 if (l <= 0)
1254 break;
1255 }
1256 add_packet(gspca_dev, data, len);
1257} 1099}
1258 1100
1259static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val) 1101static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
@@ -1291,45 +1133,6 @@ static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val)
1291 return 0; 1133 return 0;
1292} 1134}
1293 1135
1294static int sd_set_jcomp(struct gspca_dev *gspca_dev,
1295 struct v4l2_jpegcompression *jcomp)
1296{
1297 struct sd *sd = (struct sd *) gspca_dev;
1298 int quality;
1299
1300 if (jcomp->quality >= (QUAL_0 + QUAL_1) / 2)
1301 quality = 0;
1302 else if (jcomp->quality >= (QUAL_1 + QUAL_2) / 2)
1303 quality = 1;
1304 else if (jcomp->quality >= (QUAL_2 + QUAL_3) / 2)
1305 quality = 2;
1306 else
1307 quality = 3;
1308
1309 if (quality != sd->quality) {
1310 sd->quality = quality;
1311 if (gspca_dev->streaming) {
1312 send_stop(gspca_dev);
1313 sd_jpeg_set_qual(sd->jpeg_hdr, sd->quality);
1314 msleep(70);
1315 send_start(gspca_dev);
1316 }
1317 }
1318 return gspca_dev->usb_err;
1319}
1320
1321static int sd_get_jcomp(struct gspca_dev *gspca_dev,
1322 struct v4l2_jpegcompression *jcomp)
1323{
1324 struct sd *sd = (struct sd *) gspca_dev;
1325
1326 memset(jcomp, 0, sizeof *jcomp);
1327 jcomp->quality = quality_tb[sd->quality];
1328 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
1329 | V4L2_JPEG_MARKER_DQT;
1330 return 0;
1331}
1332
1333/* sub-driver description */ 1136/* sub-driver description */
1334static const struct sd_desc sd_desc = { 1137static const struct sd_desc sd_desc = {
1335 .name = MODULE_NAME, 1138 .name = MODULE_NAME,
@@ -1342,8 +1145,6 @@ static const struct sd_desc sd_desc = {
1342 .stopN = sd_stopN, 1145 .stopN = sd_stopN,
1343 .pkt_scan = sd_pkt_scan, 1146 .pkt_scan = sd_pkt_scan,
1344 .dq_callback = sd_dq_callback, 1147 .dq_callback = sd_dq_callback,
1345 .get_jcomp = sd_get_jcomp,
1346 .set_jcomp = sd_set_jcomp,
1347}; 1148};
1348 1149
1349/* Table of supported USB devices */ 1150/* Table of supported USB devices */
diff --git a/drivers/media/video/gspca/t613.c b/drivers/media/video/gspca/t613.c
index 2a0f12d55e4..3b3b983f2b9 100644
--- a/drivers/media/video/gspca/t613.c
+++ b/drivers/media/video/gspca/t613.c
@@ -55,12 +55,12 @@ struct sd {
55 u8 effect; 55 u8 effect;
56 56
57 u8 sensor; 57 u8 sensor;
58enum { 58};
59enum sensors {
59 SENSOR_OM6802, 60 SENSOR_OM6802,
60 SENSOR_OTHER, 61 SENSOR_OTHER,
61 SENSOR_TAS5130A, 62 SENSOR_TAS5130A,
62 SENSOR_LT168G, /* must verify if this is the actual model */ 63 SENSOR_LT168G, /* must verify if this is the actual model */
63} sensors;
64}; 64};
65 65
66/* V4L2 controls supported by the driver */ 66/* V4L2 controls supported by the driver */
diff --git a/drivers/media/video/gspca/vc032x.c b/drivers/media/video/gspca/vc032x.c
index 031266a4081..b16fd47e8ce 100644
--- a/drivers/media/video/gspca/vc032x.c
+++ b/drivers/media/video/gspca/vc032x.c
@@ -39,6 +39,10 @@ struct sd {
39 u8 vflip; 39 u8 vflip;
40 u8 lightfreq; 40 u8 lightfreq;
41 s8 sharpness; 41 s8 sharpness;
42 u16 exposure;
43 u8 gain;
44 u8 autogain;
45 u8 backlight;
42 46
43 u8 image_offset; 47 u8 image_offset;
44 48
@@ -77,6 +81,14 @@ static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
77static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val); 81static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
78static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val); 82static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
79static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val); 83static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
84static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
85static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
86static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
87static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
88static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
89static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
90static int sd_setbacklight(struct gspca_dev *gspca_dev, __s32 val);
91static int sd_getbacklight(struct gspca_dev *gspca_dev, __s32 *val);
80 92
81static const struct ctrl sd_ctrls[] = { 93static const struct ctrl sd_ctrls[] = {
82#define BRIGHTNESS_IDX 0 94#define BRIGHTNESS_IDX 0
@@ -185,6 +197,66 @@ static const struct ctrl sd_ctrls[] = {
185 .set = sd_setsharpness, 197 .set = sd_setsharpness,
186 .get = sd_getsharpness, 198 .get = sd_getsharpness,
187 }, 199 },
200#define GAIN_IDX 7
201 {
202 {
203 .id = V4L2_CID_GAIN,
204 .type = V4L2_CTRL_TYPE_INTEGER,
205 .name = "Gain",
206 .minimum = 0,
207 .maximum = 78,
208 .step = 1,
209#define GAIN_DEF 0
210 .default_value = GAIN_DEF,
211 },
212 .set = sd_setgain,
213 .get = sd_getgain,
214 },
215#define EXPOSURE_IDX 8
216 {
217 {
218 .id = V4L2_CID_EXPOSURE,
219 .type = V4L2_CTRL_TYPE_INTEGER,
220 .name = "Exposure",
221#define EXPOSURE_DEF 450
222 .minimum = 0,
223 .maximum = 4095,
224 .step = 1,
225 .default_value = EXPOSURE_DEF,
226 },
227 .set = sd_setexposure,
228 .get = sd_getexposure,
229 },
230#define AUTOGAIN_IDX 9
231 {
232 {
233 .id = V4L2_CID_AUTOGAIN,
234 .type = V4L2_CTRL_TYPE_BOOLEAN,
235 .name = "Automatic Gain and Exposure",
236 .minimum = 0,
237 .maximum = 1,
238 .step = 1,
239#define AUTOGAIN_DEF 1
240 .default_value = AUTOGAIN_DEF,
241 },
242 .set = sd_setautogain,
243 .get = sd_getautogain,
244 },
245#define BACKLIGHT_IDX 10
246 {
247 {
248 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
249 .type = V4L2_CTRL_TYPE_BOOLEAN,
250 .name = "Backlight Compensation",
251 .minimum = 0,
252 .maximum = 15,
253 .step = 1,
254#define BACKLIGHT_DEF 15
255 .default_value = BACKLIGHT_DEF,
256 },
257 .set = sd_setbacklight,
258 .get = sd_getbacklight,
259 },
188}; 260};
189 261
190/* table of the disabled controls */ 262/* table of the disabled controls */
@@ -192,33 +264,51 @@ static u32 ctrl_dis[] = {
192/* SENSOR_HV7131R 0 */ 264/* SENSOR_HV7131R 0 */
193 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX) 265 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
194 | (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX) 266 | (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX)
195 | (1 << SHARPNESS_IDX), 267 | (1 << SHARPNESS_IDX)
268 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
269 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
196/* SENSOR_MI0360 1 */ 270/* SENSOR_MI0360 1 */
197 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX) 271 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
198 | (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX) 272 | (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX)
199 | (1 << SHARPNESS_IDX), 273 | (1 << SHARPNESS_IDX)
274 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
275 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
200/* SENSOR_MI1310_SOC 2 */ 276/* SENSOR_MI1310_SOC 2 */
201 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX) 277 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
202 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX), 278 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX)
279 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
280 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
203/* SENSOR_MI1320 3 */ 281/* SENSOR_MI1320 3 */
204 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX) 282 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
205 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX), 283 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX)
284 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
285 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
206/* SENSOR_MI1320_SOC 4 */ 286/* SENSOR_MI1320_SOC 4 */
207 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX) 287 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
208 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX), 288 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX)
289 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
290 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
209/* SENSOR_OV7660 5 */ 291/* SENSOR_OV7660 5 */
210 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX) 292 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
211 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX), 293 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX)
294 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
295 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
212/* SENSOR_OV7670 6 */ 296/* SENSOR_OV7670 6 */
213 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX) 297 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
214 | (1 << SHARPNESS_IDX), 298 | (1 << SHARPNESS_IDX)
299 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
300 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
215/* SENSOR_PO1200 7 */ 301/* SENSOR_PO1200 7 */
216 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX) 302 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
217 | (1 << LIGHTFREQ_IDX), 303 | (1 << LIGHTFREQ_IDX)
304 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
305 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
218/* SENSOR_PO3130NC 8 */ 306/* SENSOR_PO3130NC 8 */
219 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX) 307 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
220 | (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX) 308 | (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX)
221 | (1 << SHARPNESS_IDX), 309 | (1 << SHARPNESS_IDX)
310 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
311 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
222/* SENSOR_POxxxx 9 */ 312/* SENSOR_POxxxx 9 */
223 (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX), 313 (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX),
224}; 314};
@@ -2825,7 +2915,9 @@ static const u8 poxxxx_init_common[][4] = {
2825 {0x00, 0x1e, 0xc6, 0xaa}, 2915 {0x00, 0x1e, 0xc6, 0xaa},
2826 {0x00, 0x00, 0x40, 0xdd}, 2916 {0x00, 0x00, 0x40, 0xdd},
2827 {0x00, 0x1d, 0x05, 0xaa}, 2917 {0x00, 0x1d, 0x05, 0xaa},
2828 2918 {}
2919};
2920static const u8 poxxxx_gamma[][4] = {
2829 {0x00, 0xd6, 0x22, 0xaa}, /* gamma 0 */ 2921 {0x00, 0xd6, 0x22, 0xaa}, /* gamma 0 */
2830 {0x00, 0x73, 0x00, 0xaa}, 2922 {0x00, 0x73, 0x00, 0xaa},
2831 {0x00, 0x74, 0x0a, 0xaa}, 2923 {0x00, 0x74, 0x0a, 0xaa},
@@ -2867,19 +2959,9 @@ static const u8 poxxxx_init_common[][4] = {
2867 {0x00, 0x7c, 0xba, 0xaa}, 2959 {0x00, 0x7c, 0xba, 0xaa},
2868 {0x00, 0x7d, 0xd4, 0xaa}, 2960 {0x00, 0x7d, 0xd4, 0xaa},
2869 {0x00, 0x7e, 0xea, 0xaa}, 2961 {0x00, 0x7e, 0xea, 0xaa},
2870 2962 {}
2871 {0x00, 0xaa, 0xff, 0xaa}, /* back light comp */ 2963};
2872 {0x00, 0xc4, 0x03, 0xaa}, 2964static const u8 poxxxx_init_start_3[][4] = {
2873 {0x00, 0xc5, 0x19, 0xaa},
2874 {0x00, 0xc6, 0x03, 0xaa},
2875 {0x00, 0xc7, 0x91, 0xaa},
2876 {0x00, 0xc8, 0x01, 0xaa},
2877 {0x00, 0xc9, 0xdd, 0xaa},
2878 {0x00, 0xca, 0x02, 0xaa},
2879 {0x00, 0xcb, 0x37, 0xaa},
2880
2881/* read d1 */
2882 {0x00, 0xd1, 0x3c, 0xaa},
2883 {0x00, 0xb8, 0x28, 0xaa}, 2965 {0x00, 0xb8, 0x28, 0xaa},
2884 {0x00, 0xb9, 0x1e, 0xaa}, 2966 {0x00, 0xb9, 0x1e, 0xaa},
2885 {0x00, 0xb6, 0x14, 0xaa}, 2967 {0x00, 0xb6, 0x14, 0xaa},
@@ -2959,9 +3041,6 @@ static const u8 poxxxx_init_end_1[][4] = {
2959 {0x00, 0xb3, 0x08, 0xaa}, 3041 {0x00, 0xb3, 0x08, 0xaa},
2960 {0x00, 0xb4, 0x0b, 0xaa}, 3042 {0x00, 0xb4, 0x0b, 0xaa},
2961 {0x00, 0xb5, 0x0d, 0xaa}, 3043 {0x00, 0xb5, 0x0d, 0xaa},
2962 {0x00, 0x59, 0x7e, 0xaa}, /* sharpness */
2963 {0x00, 0x16, 0x00, 0xaa}, /* white balance */
2964 {0x00, 0x18, 0x00, 0xaa},
2965 {} 3044 {}
2966}; 3045};
2967static const u8 poxxxx_init_end_2[][4] = { 3046static const u8 poxxxx_init_end_2[][4] = {
@@ -3312,6 +3391,33 @@ static int sd_config(struct gspca_dev *gspca_dev,
3312 const struct usb_device_id *id) 3391 const struct usb_device_id *id)
3313{ 3392{
3314 struct sd *sd = (struct sd *) gspca_dev; 3393 struct sd *sd = (struct sd *) gspca_dev;
3394
3395 sd->bridge = id->driver_info >> 8;
3396 sd->flags = id->driver_info & 0xff;
3397
3398 if (id->idVendor == 0x046d &&
3399 (id->idProduct == 0x0892 || id->idProduct == 0x0896))
3400 sd->sensor = SENSOR_POxxxx; /* no probe */
3401
3402 sd->brightness = BRIGHTNESS_DEF;
3403 sd->contrast = CONTRAST_DEF;
3404 sd->colors = COLOR_DEF;
3405 sd->hflip = HFLIP_DEF;
3406 sd->vflip = VFLIP_DEF;
3407 sd->lightfreq = FREQ_DEF;
3408 sd->sharpness = SHARPNESS_DEF;
3409 sd->gain = GAIN_DEF;
3410 sd->exposure = EXPOSURE_DEF;
3411 sd->autogain = AUTOGAIN_DEF;
3412 sd->backlight = BACKLIGHT_DEF;
3413
3414 return 0;
3415}
3416
3417/* this function is called at probe and resume time */
3418static int sd_init(struct gspca_dev *gspca_dev)
3419{
3420 struct sd *sd = (struct sd *) gspca_dev;
3315 struct cam *cam; 3421 struct cam *cam;
3316 int sensor; 3422 int sensor;
3317 static u8 npkt[] = { /* number of packets per ISOC message */ 3423 static u8 npkt[] = { /* number of packets per ISOC message */
@@ -3327,14 +3433,11 @@ static int sd_config(struct gspca_dev *gspca_dev,
3327 128, /* POxxxx 9 */ 3433 128, /* POxxxx 9 */
3328 }; 3434 };
3329 3435
3330 cam = &gspca_dev->cam; 3436 if (sd->sensor != SENSOR_POxxxx)
3331 sd->bridge = id->driver_info >> 8;
3332 sd->flags = id->driver_info & 0xff;
3333 if (id->idVendor == 0x046d &&
3334 (id->idProduct == 0x0892 || id->idProduct == 0x0896))
3335 sensor = SENSOR_POxxxx;
3336 else
3337 sensor = vc032x_probe_sensor(gspca_dev); 3437 sensor = vc032x_probe_sensor(gspca_dev);
3438 else
3439 sensor = sd->sensor;
3440
3338 switch (sensor) { 3441 switch (sensor) {
3339 case -1: 3442 case -1:
3340 PDEBUG(D_PROBE, "Unknown sensor..."); 3443 PDEBUG(D_PROBE, "Unknown sensor...");
@@ -3373,6 +3476,7 @@ static int sd_config(struct gspca_dev *gspca_dev,
3373 } 3476 }
3374 sd->sensor = sensor; 3477 sd->sensor = sensor;
3375 3478
3479 cam = &gspca_dev->cam;
3376 if (sd->bridge == BRIDGE_VC0321) { 3480 if (sd->bridge == BRIDGE_VC0321) {
3377 cam->cam_mode = vc0321_mode; 3481 cam->cam_mode = vc0321_mode;
3378 cam->nmodes = ARRAY_SIZE(vc0321_mode); 3482 cam->nmodes = ARRAY_SIZE(vc0321_mode);
@@ -3401,28 +3505,11 @@ static int sd_config(struct gspca_dev *gspca_dev,
3401 } 3505 }
3402 } 3506 }
3403 cam->npkt = npkt[sd->sensor]; 3507 cam->npkt = npkt[sd->sensor];
3404
3405 sd->brightness = BRIGHTNESS_DEF;
3406 sd->contrast = CONTRAST_DEF;
3407 sd->colors = COLOR_DEF;
3408 sd->hflip = HFLIP_DEF;
3409 sd->vflip = VFLIP_DEF;
3410 sd->lightfreq = FREQ_DEF;
3411 sd->sharpness = SHARPNESS_DEF;
3412
3413 gspca_dev->ctrl_dis = ctrl_dis[sd->sensor]; 3508 gspca_dev->ctrl_dis = ctrl_dis[sd->sensor];
3414 3509
3415 if (sd->sensor == SENSOR_OV7670) 3510 if (sd->sensor == SENSOR_OV7670)
3416 sd->flags |= FL_HFLIP | FL_VFLIP; 3511 sd->flags |= FL_HFLIP | FL_VFLIP;
3417 3512
3418 return 0;
3419}
3420
3421/* this function is called at probe and resume time */
3422static int sd_init(struct gspca_dev *gspca_dev)
3423{
3424 struct sd *sd = (struct sd *) gspca_dev;
3425
3426 if (sd->bridge == BRIDGE_VC0321) { 3513 if (sd->bridge == BRIDGE_VC0321) {
3427 reg_r(gspca_dev, 0x8a, 0, 3); 3514 reg_r(gspca_dev, 0x8a, 0, 3);
3428 reg_w(gspca_dev, 0x87, 0x00, 0x0f0f); 3515 reg_w(gspca_dev, 0x87, 0x00, 0x0f0f);
@@ -3433,8 +3520,8 @@ static int sd_init(struct gspca_dev *gspca_dev)
3433 if (gspca_dev->usb_buf[0] != 0) { 3520 if (gspca_dev->usb_buf[0] != 0) {
3434 reg_w(gspca_dev, 0xa0, 0x26, 0xb300); 3521 reg_w(gspca_dev, 0xa0, 0x26, 0xb300);
3435 reg_w(gspca_dev, 0xa0, 0x04, 0xb300); 3522 reg_w(gspca_dev, 0xa0, 0x04, 0xb300);
3436 reg_w(gspca_dev, 0xa0, 0x00, 0xb300);
3437 } 3523 }
3524 reg_w(gspca_dev, 0xa0, 0x00, 0xb300);
3438 } 3525 }
3439 } 3526 }
3440 return gspca_dev->usb_err; 3527 return gspca_dev->usb_err;
@@ -3551,6 +3638,82 @@ static void setsharpness(struct gspca_dev *gspca_dev)
3551 break; 3638 break;
3552 } 3639 }
3553} 3640}
3641static void setgain(struct gspca_dev *gspca_dev)
3642{
3643 struct sd *sd = (struct sd *) gspca_dev;
3644
3645 if (gspca_dev->ctrl_dis & (1 << GAIN_IDX))
3646 return;
3647 i2c_write(gspca_dev, 0x15, &sd->gain, 1);
3648}
3649
3650static void setexposure(struct gspca_dev *gspca_dev)
3651{
3652 struct sd *sd = (struct sd *) gspca_dev;
3653 u8 data;
3654
3655 if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX))
3656 return;
3657 data = sd->exposure >> 8;
3658 i2c_write(gspca_dev, 0x1a, &data, 1);
3659 data = sd->exposure;
3660 i2c_write(gspca_dev, 0x1b, &data, 1);
3661}
3662
3663static void setautogain(struct gspca_dev *gspca_dev)
3664{
3665 struct sd *sd = (struct sd *) gspca_dev;
3666 static const u8 data[2] = {0x28, 0x3c};
3667
3668 if (gspca_dev->ctrl_dis & (1 << AUTOGAIN_IDX))
3669 return;
3670 i2c_write(gspca_dev, 0xd1, &data[sd->autogain], 1);
3671}
3672
3673static void setgamma(struct gspca_dev *gspca_dev)
3674{
3675/*fixme:to do */
3676 usb_exchange(gspca_dev, poxxxx_gamma);
3677}
3678
3679static void setbacklight(struct gspca_dev *gspca_dev)
3680{
3681 struct sd *sd = (struct sd *) gspca_dev;
3682 u16 v;
3683 u8 data;
3684
3685 data = (sd->backlight << 4) | 0x0f;
3686 i2c_write(gspca_dev, 0xaa, &data, 1);
3687 v = 613 + 12 * sd->backlight;
3688 data = v >> 8;
3689 i2c_write(gspca_dev, 0xc4, &data, 1);
3690 data = v;
3691 i2c_write(gspca_dev, 0xc5, &data, 1);
3692 v = 1093 - 12 * sd->backlight;
3693 data = v >> 8;
3694 i2c_write(gspca_dev, 0xc6, &data, 1);
3695 data = v;
3696 i2c_write(gspca_dev, 0xc7, &data, 1);
3697 v = 342 + 9 * sd->backlight;
3698 data = v >> 8;
3699 i2c_write(gspca_dev, 0xc8, &data, 1);
3700 data = v;
3701 i2c_write(gspca_dev, 0xc9, &data, 1);
3702 v = 702 - 9 * sd->backlight;
3703 data = v >> 8;
3704 i2c_write(gspca_dev, 0xca, &data, 1);
3705 data = v;
3706 i2c_write(gspca_dev, 0xcb, &data, 1);
3707}
3708
3709static void setwb(struct gspca_dev *gspca_dev)
3710{
3711/*fixme:to do - valid when reg d1 = 0x1c - (reg16 + reg15 = 0xa3)*/
3712 static const u8 data[2] = {0x00, 0x00};
3713
3714 i2c_write(gspca_dev, 0x16, &data[0], 1);
3715 i2c_write(gspca_dev, 0x18, &data[1], 1);
3716}
3554 3717
3555static int sd_start(struct gspca_dev *gspca_dev) 3718static int sd_start(struct gspca_dev *gspca_dev)
3556{ 3719{
@@ -3662,6 +3825,16 @@ static int sd_start(struct gspca_dev *gspca_dev)
3662 default: 3825 default:
3663/* case SENSOR_POxxxx: */ 3826/* case SENSOR_POxxxx: */
3664 usb_exchange(gspca_dev, poxxxx_init_common); 3827 usb_exchange(gspca_dev, poxxxx_init_common);
3828 setgamma(gspca_dev);
3829 setbacklight(gspca_dev);
3830 setbrightness(gspca_dev);
3831 setcontrast(gspca_dev);
3832 setcolors(gspca_dev);
3833 setsharpness(gspca_dev);
3834 setautogain(gspca_dev);
3835 setexposure(gspca_dev);
3836 setgain(gspca_dev);
3837 usb_exchange(gspca_dev, poxxxx_init_start_3);
3665 if (mode) 3838 if (mode)
3666 init = poxxxx_initQVGA; 3839 init = poxxxx_initQVGA;
3667 else 3840 else
@@ -3693,7 +3866,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
3693 break; 3866 break;
3694 } 3867 }
3695 msleep(100); 3868 msleep(100);
3696 setsharpness(gspca_dev);
3697 sethvflip(gspca_dev); 3869 sethvflip(gspca_dev);
3698 setlightfreq(gspca_dev); 3870 setlightfreq(gspca_dev);
3699 } 3871 }
@@ -3704,14 +3876,10 @@ static int sd_start(struct gspca_dev *gspca_dev)
3704 reg_w(gspca_dev, 0xa0, 0x0000, 0xbfff); 3876 reg_w(gspca_dev, 0xa0, 0x0000, 0xbfff);
3705 break; 3877 break;
3706 case SENSOR_POxxxx: 3878 case SENSOR_POxxxx:
3707 setcolors(gspca_dev);
3708 setbrightness(gspca_dev);
3709 setcontrast(gspca_dev);
3710
3711 /* led on */
3712 msleep(80);
3713 reg_w(gspca_dev, 0x89, 0xffff, 0xfdff);
3714 usb_exchange(gspca_dev, poxxxx_init_end_2); 3879 usb_exchange(gspca_dev, poxxxx_init_end_2);
3880 setwb(gspca_dev);
3881 msleep(80); /* led on */
3882 reg_w(gspca_dev, 0x89, 0xffff, 0xfdff);
3715 break; 3883 break;
3716 } 3884 }
3717 return gspca_dev->usb_err; 3885 return gspca_dev->usb_err;
@@ -3911,6 +4079,80 @@ static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
3911 return 0; 4079 return 0;
3912} 4080}
3913 4081
4082static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
4083{
4084 struct sd *sd = (struct sd *) gspca_dev;
4085
4086 sd->gain = val;
4087 if (gspca_dev->streaming)
4088 setgain(gspca_dev);
4089 return gspca_dev->usb_err;
4090}
4091
4092static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
4093{
4094 struct sd *sd = (struct sd *) gspca_dev;
4095
4096 *val = sd->gain;
4097 return 0;
4098}
4099
4100static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
4101{
4102 struct sd *sd = (struct sd *) gspca_dev;
4103
4104 sd->exposure = val;
4105 if (gspca_dev->streaming)
4106 setexposure(gspca_dev);
4107 return gspca_dev->usb_err;
4108}
4109
4110static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
4111{
4112 struct sd *sd = (struct sd *) gspca_dev;
4113
4114 *val = sd->exposure;
4115 return 0;
4116}
4117
4118static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
4119{
4120 struct sd *sd = (struct sd *) gspca_dev;
4121
4122 sd->autogain = val;
4123 if (gspca_dev->streaming)
4124 setautogain(gspca_dev);
4125
4126 return gspca_dev->usb_err;
4127}
4128
4129static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
4130{
4131 struct sd *sd = (struct sd *) gspca_dev;
4132
4133 *val = sd->autogain;
4134 return 0;
4135}
4136
4137static int sd_setbacklight(struct gspca_dev *gspca_dev, __s32 val)
4138{
4139 struct sd *sd = (struct sd *) gspca_dev;
4140
4141 sd->backlight = val;
4142 if (gspca_dev->streaming)
4143 setbacklight(gspca_dev);
4144
4145 return gspca_dev->usb_err;
4146}
4147
4148static int sd_getbacklight(struct gspca_dev *gspca_dev, __s32 *val)
4149{
4150 struct sd *sd = (struct sd *) gspca_dev;
4151
4152 *val = sd->backlight;
4153 return 0;
4154}
4155
3914static int sd_querymenu(struct gspca_dev *gspca_dev, 4156static int sd_querymenu(struct gspca_dev *gspca_dev,
3915 struct v4l2_querymenu *menu) 4157 struct v4l2_querymenu *menu)
3916{ 4158{
diff --git a/drivers/media/video/gspca/zc3xx.c b/drivers/media/video/gspca/zc3xx.c
index 4473f0fb8b7..0666038a51b 100644
--- a/drivers/media/video/gspca/zc3xx.c
+++ b/drivers/media/video/gspca/zc3xx.c
@@ -21,7 +21,9 @@
21 21
22#define MODULE_NAME "zc3xx" 22#define MODULE_NAME "zc3xx"
23 23
24#ifdef CONFIG_INPUT
24#include <linux/input.h> 25#include <linux/input.h>
26#endif
25#include "gspca.h" 27#include "gspca.h"
26#include "jpeg.h" 28#include "jpeg.h"
27 29
@@ -50,33 +52,38 @@ struct sd {
50#define QUALITY_MAX 80 52#define QUALITY_MAX 80
51#define QUALITY_DEF 70 53#define QUALITY_DEF 70
52 54
55 u8 bridge;
53 u8 sensor; /* Type of image sensor chip */ 56 u8 sensor; /* Type of image sensor chip */
54/* !! values used in different tables */ 57 u16 chip_revision;
55#define SENSOR_ADCM2700 0
56#define SENSOR_CS2102 1
57#define SENSOR_CS2102K 2
58#define SENSOR_GC0305 3
59#define SENSOR_HDCS2020b 4
60#define SENSOR_HV7131B 5
61#define SENSOR_HV7131C 6
62#define SENSOR_ICM105A 7
63#define SENSOR_MC501CB 8
64#define SENSOR_MI0360SOC 9
65#define SENSOR_OV7620 10
66/*#define SENSOR_OV7648 10 - same values */
67#define SENSOR_OV7630C 11
68#define SENSOR_PAS106 12
69#define SENSOR_PAS202B 13
70#define SENSOR_PB0330 14 /* (MI0360) */
71#define SENSOR_PO2030 15
72#define SENSOR_TAS5130CK 16
73#define SENSOR_TAS5130CXX 17
74#define SENSOR_TAS5130C_VF0250 18
75#define SENSOR_MAX 19
76 unsigned short chip_revision;
77 58
78 u8 jpeg_hdr[JPEG_HDR_SZ]; 59 u8 jpeg_hdr[JPEG_HDR_SZ];
79}; 60};
61enum bridges {
62 BRIDGE_ZC301,
63 BRIDGE_ZC303,
64};
65enum sensors {
66 SENSOR_ADCM2700,
67 SENSOR_CS2102,
68 SENSOR_CS2102K,
69 SENSOR_GC0305,
70 SENSOR_HDCS2020b,
71 SENSOR_HV7131B,
72 SENSOR_HV7131R,
73 SENSOR_ICM105A,
74 SENSOR_MC501CB,
75 SENSOR_MT9V111_1, /* (mi360soc) zc301 */
76 SENSOR_MT9V111_3, /* (mi360soc) zc303 */
77 SENSOR_OV7620, /* OV7648 - same values */
78 SENSOR_OV7630C,
79 SENSOR_PAS106,
80 SENSOR_PAS202B,
81 SENSOR_PB0330,
82 SENSOR_PO2030,
83 SENSOR_TAS5130C,
84 SENSOR_TAS5130C_VF0250,
85 SENSOR_MAX
86};
80 87
81/* V4L2 controls supported by the driver */ 88/* V4L2 controls supported by the driver */
82static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val); 89static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
@@ -2074,6 +2081,7 @@ static const struct usb_action hv7131b_NoFlikerScale[] = { /* 320x240 */
2074 {} 2081 {}
2075}; 2082};
2076 2083
2084/* from lPEPI264v.inf (hv7131b!) */
2077static const struct usb_action hv7131r_InitialScale[] = { 2085static const struct usb_action hv7131r_InitialScale[] = {
2078 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL}, 2086 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
2079 {0xa0, 0x10, ZC3XX_R002_CLOCKSELECT}, 2087 {0xa0, 0x10, ZC3XX_R002_CLOCKSELECT},
@@ -2081,8 +2089,8 @@ static const struct usb_action hv7131r_InitialScale[] = {
2081 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING}, 2089 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING},
2082 {0xa0, 0x77, ZC3XX_R101_SENSORCORRECTION}, 2090 {0xa0, 0x77, ZC3XX_R101_SENSORCORRECTION},
2083 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING}, 2091 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
2084 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
2085 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC}, 2092 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC},
2093 {0xa0, 0x01, ZC3XX_R012_VIDEOCONTROLFUNC},
2086 {0xa0, 0x02, ZC3XX_R003_FRAMEWIDTHHIGH}, 2094 {0xa0, 0x02, ZC3XX_R003_FRAMEWIDTHHIGH},
2087 {0xa0, 0x80, ZC3XX_R004_FRAMEWIDTHLOW}, 2095 {0xa0, 0x80, ZC3XX_R004_FRAMEWIDTHLOW},
2088 {0xa0, 0x01, ZC3XX_R005_FRAMEHEIGHTHIGH}, 2096 {0xa0, 0x01, ZC3XX_R005_FRAMEHEIGHTHIGH},
@@ -2095,6 +2103,8 @@ static const struct usb_action hv7131r_InitialScale[] = {
2095 {0xa0, 0x88, ZC3XX_R09E_WINWIDTHLOW}, 2103 {0xa0, 0x88, ZC3XX_R09E_WINWIDTHLOW},
2096 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW}, 2104 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW},
2097 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW}, 2105 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW},
2106 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC},
2107 {0xdd, 0x00, 0x0200},
2098 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC}, 2108 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
2099 {0xaa, 0x01, 0x000c}, 2109 {0xaa, 0x01, 0x000c},
2100 {0xaa, 0x11, 0x0000}, 2110 {0xaa, 0x11, 0x0000},
@@ -2103,10 +2113,10 @@ static const struct usb_action hv7131r_InitialScale[] = {
2103 {0xaa, 0x15, 0x00e8}, 2113 {0xaa, 0x15, 0x00e8},
2104 {0xaa, 0x16, 0x0002}, 2114 {0xaa, 0x16, 0x0002},
2105 {0xaa, 0x17, 0x0088}, 2115 {0xaa, 0x17, 0x0088},
2106 2116 {0xaa, 0x30, 0x000b},
2107 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, 2117 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
2108 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE}, 2118 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE},
2109 {0xa0, 0x89, ZC3XX_R18D_YTARGET}, 2119 {0xa0, 0x78, ZC3XX_R18D_YTARGET},
2110 {0xa0, 0x50, ZC3XX_R1A8_DIGITALGAIN}, 2120 {0xa0, 0x50, ZC3XX_R1A8_DIGITALGAIN},
2111 {0xa0, 0x00, 0x01ad}, 2121 {0xa0, 0x00, 0x01ad},
2112 {0xa0, 0xc0, 0x019b}, 2122 {0xa0, 0xc0, 0x019b},
@@ -2116,96 +2126,44 @@ static const struct usb_action hv7131r_InitialScale[] = {
2116 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05}, 2126 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05},
2117 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE}, 2127 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE},
2118 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS}, 2128 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS},
2119 {0xa1, 0x01, 0x0002},
2120 {0xa0, 0x00, ZC3XX_R092_I2CADDRESSSELECT},
2121 {0xa0, 0x02, ZC3XX_R090_I2CCOMMAND},
2122 {0xa1, 0x01, 0x0091},
2123 {0xa1, 0x01, 0x0095},
2124 {0xa1, 0x01, 0x0096},
2125
2126 {0xa1, 0x01, 0x0008},
2127 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING}, /* clock ? */
2128 {0xa0, 0x08, ZC3XX_R1C6_SHARPNESS00}, /* sharpness+ */
2129 {0xa1, 0x01, 0x01c8},
2130 {0xa1, 0x01, 0x01c9},
2131 {0xa1, 0x01, 0x01ca},
2132 {0xa0, 0x0f, ZC3XX_R1CB_SHARPNESS05}, /* sharpness- */
2133
2134 {0xa0, 0x60, ZC3XX_R10A_RGB00}, /* matrix */
2135 {0xa0, 0xf0, ZC3XX_R10B_RGB01},
2136 {0xa0, 0xf0, ZC3XX_R10C_RGB02},
2137 {0xa0, 0xf0, ZC3XX_R10D_RGB10},
2138 {0xa0, 0x60, ZC3XX_R10E_RGB11},
2139 {0xa0, 0xf0, ZC3XX_R10F_RGB12},
2140 {0xa0, 0xf0, ZC3XX_R110_RGB20},
2141 {0xa0, 0xf0, ZC3XX_R111_RGB21},
2142 {0xa0, 0x60, ZC3XX_R112_RGB22},
2143 {0xa1, 0x01, 0x0180},
2144 {0xa0, 0x10, ZC3XX_R180_AUTOCORRECTENABLE},
2145 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
2146 {0xaa, 0x25, 0x0007},
2147 {0xaa, 0x26, 0x0053},
2148 {0xaa, 0x27, 0x0000},
2149
2150 {0xa0, 0x10, ZC3XX_R190_EXPOSURELIMITHIGH}, /* 2f */
2151 {0xa0, 0x04, ZC3XX_R191_EXPOSURELIMITMID}, /* 9b */
2152 {0xa0, 0x60, ZC3XX_R192_EXPOSURELIMITLOW}, /* 80 */
2153 {0xa0, 0x01, ZC3XX_R195_ANTIFLICKERHIGH},
2154 {0xa0, 0xd4, ZC3XX_R196_ANTIFLICKERMID},
2155 {0xa0, 0xc0, ZC3XX_R197_ANTIFLICKERLOW},
2156 {0xa0, 0x10, ZC3XX_R18C_AEFREEZE},
2157 {0xa0, 0x20, ZC3XX_R18F_AEUNFREEZE},
2158 {0xa0, 0x60, ZC3XX_R1A8_DIGITALGAIN},
2159 {0xa0, 0x10, ZC3XX_R1A9_DIGITALLIMITDIFF},
2160 {0xa0, 0x13, ZC3XX_R1AA_DIGITALGAINSTEP},
2161 {0xa1, 0x01, 0x001d},
2162 {0xa1, 0x01, 0x001e},
2163 {0xa1, 0x01, 0x001f},
2164 {0xa1, 0x01, 0x0020},
2165 {0xa0, 0x40, ZC3XX_R180_AUTOCORRECTENABLE},
2166 {0xa1, 0x01, 0x0180},
2167 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
2168 {} 2129 {}
2169}; 2130};
2170
2171static const struct usb_action hv7131r_Initial[] = { 2131static const struct usb_action hv7131r_Initial[] = {
2172 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL}, 2132 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
2173 2133 {0xa0, 0x00, ZC3XX_R002_CLOCKSELECT},
2174 {0xa0, 0x00, ZC3XX_R002_CLOCKSELECT}, /* diff */
2175 {0xa0, 0x01, ZC3XX_R010_CMOSSENSORSELECT}, 2134 {0xa0, 0x01, ZC3XX_R010_CMOSSENSORSELECT},
2176 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING}, 2135 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING},
2177 {0xa0, 0x77, ZC3XX_R101_SENSORCORRECTION}, 2136 {0xa0, 0x77, ZC3XX_R101_SENSORCORRECTION},
2178 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING}, 2137 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
2179
2180 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
2181 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC}, 2138 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC},
2182 2139 {0xa0, 0x01, ZC3XX_R012_VIDEOCONTROLFUNC},
2183 {0xa0, 0x02, ZC3XX_R003_FRAMEWIDTHHIGH}, 2140 {0xa0, 0x02, ZC3XX_R003_FRAMEWIDTHHIGH},
2184 {0xa0, 0x80, ZC3XX_R004_FRAMEWIDTHLOW}, 2141 {0xa0, 0x80, ZC3XX_R004_FRAMEWIDTHLOW},
2185 {0xa0, 0x01, ZC3XX_R005_FRAMEHEIGHTHIGH}, 2142 {0xa0, 0x01, ZC3XX_R005_FRAMEHEIGHTHIGH},
2186 {0xa0, 0xe0, ZC3XX_R006_FRAMEHEIGHTLOW}, /* 1e0 */ 2143 {0xa0, 0xe0, ZC3XX_R006_FRAMEHEIGHTLOW},
2187 2144
2188 {0xa0, 0x00, ZC3XX_R098_WINYSTARTLOW}, 2145 {0xa0, 0x00, ZC3XX_R098_WINYSTARTLOW},
2189 {0xa0, 0x00, ZC3XX_R09A_WINXSTARTLOW}, 2146 {0xa0, 0x00, ZC3XX_R09A_WINXSTARTLOW},
2190 {0xa0, 0x01, ZC3XX_R09B_WINHEIGHTHIGH}, 2147 {0xa0, 0x01, ZC3XX_R09B_WINHEIGHTHIGH},
2191 {0xa0, 0xe8, ZC3XX_R09C_WINHEIGHTLOW}, 2148 {0xa0, 0xe6, ZC3XX_R09C_WINHEIGHTLOW},
2192 {0xa0, 0x02, ZC3XX_R09D_WINWIDTHHIGH}, 2149 {0xa0, 0x02, ZC3XX_R09D_WINWIDTHHIGH},
2193 {0xa0, 0x88, ZC3XX_R09E_WINWIDTHLOW}, 2150 {0xa0, 0x86, ZC3XX_R09E_WINWIDTHLOW},
2194 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW}, 2151 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW},
2195 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW}, 2152 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW},
2153 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC},
2154 {0xdd, 0x00, 0x0200},
2196 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC}, 2155 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
2197 {0xaa, 0x01, 0x000c}, 2156 {0xaa, 0x01, 0x000c},
2198 {0xaa, 0x11, 0x0000}, 2157 {0xaa, 0x11, 0x0000},
2199 {0xaa, 0x13, 0x0000}, 2158 {0xaa, 0x13, 0x0000},
2200 {0xaa, 0x14, 0x0001}, 2159 {0xaa, 0x14, 0x0001},
2201 {0xaa, 0x15, 0x00e8}, 2160 {0xaa, 0x15, 0x00e6},
2202 {0xaa, 0x16, 0x0002}, 2161 {0xaa, 0x16, 0x0002},
2203 {0xaa, 0x17, 0x0088}, 2162 {0xaa, 0x17, 0x0086},
2204 2163 {0xaa, 0x30, 0x000b},
2205 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00 */ 2164 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
2206
2207 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE}, 2165 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE},
2208 {0xa0, 0x89, ZC3XX_R18D_YTARGET}, 2166 {0xa0, 0x78, ZC3XX_R18D_YTARGET},
2209 {0xa0, 0x50, ZC3XX_R1A8_DIGITALGAIN}, 2167 {0xa0, 0x50, ZC3XX_R1A8_DIGITALGAIN},
2210 {0xa0, 0x00, 0x01ad}, 2168 {0xa0, 0x00, 0x01ad},
2211 {0xa0, 0xc0, 0x019b}, 2169 {0xa0, 0xc0, 0x019b},
@@ -2215,58 +2173,114 @@ static const struct usb_action hv7131r_Initial[] = {
2215 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05}, 2173 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05},
2216 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE}, 2174 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE},
2217 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS}, 2175 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS},
2218 {0xa1, 0x01, 0x0002}, 2176 {}
2219 {0xa0, 0x00, ZC3XX_R092_I2CADDRESSSELECT}, 2177};
2220 /* read the i2c chips ident */ 2178static const struct usb_action hv7131r_50HZ[] = {
2221 {0xa0, 0x02, ZC3XX_R090_I2CCOMMAND},
2222 {0xa1, 0x01, 0x0091},
2223 {0xa1, 0x01, 0x0095},
2224 {0xa1, 0x01, 0x0096},
2225
2226 {0xa1, 0x01, 0x0008},
2227 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING}, /* clock ? */
2228 {0xa0, 0x08, ZC3XX_R1C6_SHARPNESS00}, /* sharpness+ */
2229 {0xa1, 0x01, 0x01c8},
2230 {0xa1, 0x01, 0x01c9},
2231 {0xa1, 0x01, 0x01ca},
2232 {0xa0, 0x0f, ZC3XX_R1CB_SHARPNESS05}, /* sharpness- */
2233
2234 {0xa0, 0x60, ZC3XX_R10A_RGB00}, /* matrix */
2235 {0xa0, 0xf0, ZC3XX_R10B_RGB01},
2236 {0xa0, 0xf0, ZC3XX_R10C_RGB02},
2237 {0xa0, 0xf0, ZC3XX_R10D_RGB10},
2238 {0xa0, 0x60, ZC3XX_R10E_RGB11},
2239 {0xa0, 0xf0, ZC3XX_R10F_RGB12},
2240 {0xa0, 0xf0, ZC3XX_R110_RGB20},
2241 {0xa0, 0xf0, ZC3XX_R111_RGB21},
2242 {0xa0, 0x60, ZC3XX_R112_RGB22},
2243 {0xa1, 0x01, 0x0180},
2244 {0xa0, 0x10, ZC3XX_R180_AUTOCORRECTENABLE},
2245 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, 2179 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
2246 {0xaa, 0x25, 0x0007}, 2180 {0xa0, 0x06, ZC3XX_R190_EXPOSURELIMITHIGH},
2247 {0xaa, 0x26, 0x0053}, 2181 {0xa0, 0x68, ZC3XX_R191_EXPOSURELIMITMID},
2248 {0xaa, 0x27, 0x0000}, 2182 {0xa0, 0xa0, ZC3XX_R192_EXPOSURELIMITLOW},
2249 2183 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
2250 {0xa0, 0x10, ZC3XX_R190_EXPOSURELIMITHIGH}, /* 2f */ 2184 {0xa0, 0xea, ZC3XX_R196_ANTIFLICKERMID},
2251 {0xa0, 0x04, ZC3XX_R191_EXPOSURELIMITMID}, /* 9b */ 2185 {0xa0, 0x60, ZC3XX_R197_ANTIFLICKERLOW},
2252 {0xa0, 0x60, ZC3XX_R192_EXPOSURELIMITLOW}, /* 80 */ 2186 {0xa0, 0x18, ZC3XX_R18C_AEFREEZE},
2253 2187 {0xa0, 0x20, ZC3XX_R18F_AEUNFREEZE},
2188 {0xa0, 0x10, ZC3XX_R1A9_DIGITALLIMITDIFF},
2189 {0xa0, 0x66, ZC3XX_R1AA_DIGITALGAINSTEP},
2190 {0xa0, 0x00, ZC3XX_R01D_HSYNC_0},
2191 {0xa0, 0xd0, ZC3XX_R01E_HSYNC_1},
2192 {0xa0, 0x00, ZC3XX_R01F_HSYNC_2},
2193 {0xa0, 0x08, ZC3XX_R020_HSYNC_3},
2194 {}
2195};
2196static const struct usb_action hv7131r_50HZScale[] = {
2197 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
2198 {0xa0, 0x0c, ZC3XX_R190_EXPOSURELIMITHIGH},
2199 {0xa0, 0xd1, ZC3XX_R191_EXPOSURELIMITMID},
2200 {0xa0, 0x40, ZC3XX_R192_EXPOSURELIMITLOW},
2254 {0xa0, 0x01, ZC3XX_R195_ANTIFLICKERHIGH}, 2201 {0xa0, 0x01, ZC3XX_R195_ANTIFLICKERHIGH},
2255 {0xa0, 0xd4, ZC3XX_R196_ANTIFLICKERMID}, 2202 {0xa0, 0xd4, ZC3XX_R196_ANTIFLICKERMID},
2256 {0xa0, 0xc0, ZC3XX_R197_ANTIFLICKERLOW}, 2203 {0xa0, 0xc0, ZC3XX_R197_ANTIFLICKERLOW},
2257 2204 {0xa0, 0x18, ZC3XX_R18C_AEFREEZE},
2258 {0xa0, 0x10, ZC3XX_R18C_AEFREEZE},
2259 {0xa0, 0x20, ZC3XX_R18F_AEUNFREEZE}, 2205 {0xa0, 0x20, ZC3XX_R18F_AEUNFREEZE},
2260 {0xa0, 0x60, ZC3XX_R1A8_DIGITALGAIN},
2261 {0xa0, 0x10, ZC3XX_R1A9_DIGITALLIMITDIFF}, 2206 {0xa0, 0x10, ZC3XX_R1A9_DIGITALLIMITDIFF},
2262 {0xa0, 0x13, ZC3XX_R1AA_DIGITALGAINSTEP}, 2207 {0xa0, 0x66, ZC3XX_R1AA_DIGITALGAINSTEP},
2263 {0xa1, 0x01, 0x001d}, 2208 {0xa0, 0x00, ZC3XX_R01D_HSYNC_0},
2264 {0xa1, 0x01, 0x001e}, 2209 {0xa0, 0xd0, ZC3XX_R01E_HSYNC_1},
2265 {0xa1, 0x01, 0x001f}, 2210 {0xa0, 0x00, ZC3XX_R01F_HSYNC_2},
2266 {0xa1, 0x01, 0x0020}, 2211 {0xa0, 0x08, ZC3XX_R020_HSYNC_3},
2267 {0xa0, 0x40, ZC3XX_R180_AUTOCORRECTENABLE}, 2212 {}
2268 {0xa1, 0x01, 0x0180}, 2213};
2269 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE}, 2214static const struct usb_action hv7131r_60HZ[] = {
2215 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
2216 {0xa0, 0x06, ZC3XX_R190_EXPOSURELIMITHIGH},
2217 {0xa0, 0x1a, ZC3XX_R191_EXPOSURELIMITMID},
2218 {0xa0, 0x80, ZC3XX_R192_EXPOSURELIMITLOW},
2219 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
2220 {0xa0, 0xc3, ZC3XX_R196_ANTIFLICKERMID},
2221 {0xa0, 0x50, ZC3XX_R197_ANTIFLICKERLOW},
2222 {0xa0, 0x18, ZC3XX_R18C_AEFREEZE},
2223 {0xa0, 0x20, ZC3XX_R18F_AEUNFREEZE},
2224 {0xa0, 0x10, ZC3XX_R1A9_DIGITALLIMITDIFF},
2225 {0xa0, 0x66, ZC3XX_R1AA_DIGITALGAINSTEP},
2226 {0xa0, 0x00, ZC3XX_R01D_HSYNC_0},
2227 {0xa0, 0xd0, ZC3XX_R01E_HSYNC_1},
2228 {0xa0, 0x00, ZC3XX_R01F_HSYNC_2},
2229 {0xa0, 0x08, ZC3XX_R020_HSYNC_3},
2230 {}
2231};
2232static const struct usb_action hv7131r_60HZScale[] = {
2233 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
2234 {0xa0, 0x0c, ZC3XX_R190_EXPOSURELIMITHIGH},
2235 {0xa0, 0x35, ZC3XX_R191_EXPOSURELIMITMID},
2236 {0xa0, 0x00, ZC3XX_R192_EXPOSURELIMITLOW},
2237 {0xa0, 0x01, ZC3XX_R195_ANTIFLICKERHIGH},
2238 {0xa0, 0x86, ZC3XX_R196_ANTIFLICKERMID},
2239 {0xa0, 0xa0, ZC3XX_R197_ANTIFLICKERLOW},
2240 {0xa0, 0x18, ZC3XX_R18C_AEFREEZE},
2241 {0xa0, 0x20, ZC3XX_R18F_AEUNFREEZE},
2242 {0xa0, 0x10, ZC3XX_R1A9_DIGITALLIMITDIFF},
2243 {0xa0, 0x66, ZC3XX_R1AA_DIGITALGAINSTEP},
2244 {0xa0, 0x00, ZC3XX_R01D_HSYNC_0},
2245 {0xa0, 0xd0, ZC3XX_R01E_HSYNC_1},
2246 {0xa0, 0x00, ZC3XX_R01F_HSYNC_2},
2247 {0xa0, 0x08, ZC3XX_R020_HSYNC_3},
2248 {}
2249};
2250static const struct usb_action hv7131r_NoFliker[] = {
2251 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
2252 {0xa0, 0x2f, ZC3XX_R190_EXPOSURELIMITHIGH},
2253 {0xa0, 0xf8, ZC3XX_R191_EXPOSURELIMITMID},
2254 {0xa0, 0x00, ZC3XX_R192_EXPOSURELIMITLOW},
2255 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
2256 {0xa0, 0x02, ZC3XX_R196_ANTIFLICKERMID},
2257 {0xa0, 0x58, ZC3XX_R197_ANTIFLICKERLOW},
2258 {0xa0, 0x0c, ZC3XX_R18C_AEFREEZE},
2259 {0xa0, 0x18, ZC3XX_R18F_AEUNFREEZE},
2260 {0xa0, 0x00, ZC3XX_R1A9_DIGITALLIMITDIFF},
2261 {0xa0, 0x00, ZC3XX_R1AA_DIGITALGAINSTEP},
2262 {0xa0, 0x00, ZC3XX_R01D_HSYNC_0},
2263 {0xa0, 0xd0, ZC3XX_R01E_HSYNC_1},
2264 {0xa0, 0x00, ZC3XX_R01F_HSYNC_2},
2265 {0xa0, 0x08, ZC3XX_R020_HSYNC_3},
2266 {}
2267};
2268static const struct usb_action hv7131r_NoFlikerScale[] = {
2269 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
2270 {0xa0, 0x2f, ZC3XX_R190_EXPOSURELIMITHIGH},
2271 {0xa0, 0xf8, ZC3XX_R191_EXPOSURELIMITMID},
2272 {0xa0, 0x00, ZC3XX_R192_EXPOSURELIMITLOW},
2273 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
2274 {0xa0, 0x04, ZC3XX_R196_ANTIFLICKERMID},
2275 {0xa0, 0xb0, ZC3XX_R197_ANTIFLICKERLOW},
2276 {0xa0, 0x0c, ZC3XX_R18C_AEFREEZE},
2277 {0xa0, 0x18, ZC3XX_R18F_AEUNFREEZE},
2278 {0xa0, 0x00, ZC3XX_R1A9_DIGITALLIMITDIFF},
2279 {0xa0, 0x00, ZC3XX_R1AA_DIGITALGAINSTEP},
2280 {0xa0, 0x00, ZC3XX_R01D_HSYNC_0},
2281 {0xa0, 0xd0, ZC3XX_R01E_HSYNC_1},
2282 {0xa0, 0x00, ZC3XX_R01F_HSYNC_2},
2283 {0xa0, 0x08, ZC3XX_R020_HSYNC_3},
2270 {} 2284 {}
2271}; 2285};
2272 2286
@@ -3350,7 +3364,7 @@ static const struct usb_action ov7620_NoFliker[] = {
3350 {0xa0, 0x00, ZC3XX_R196_ANTIFLICKERMID}, /* 01,96,00,cc */ 3364 {0xa0, 0x00, ZC3XX_R196_ANTIFLICKERMID}, /* 01,96,00,cc */
3351 {0xa0, 0x01, ZC3XX_R197_ANTIFLICKERLOW}, /* 01,97,01,cc */ 3365 {0xa0, 0x01, ZC3XX_R197_ANTIFLICKERLOW}, /* 01,97,01,cc */
3352/* {0xa0, 0x44, ZC3XX_R002_CLOCKSELECT}, * 00,02,44,cc 3366/* {0xa0, 0x44, ZC3XX_R002_CLOCKSELECT}, * 00,02,44,cc
3353 - if mode1 (320x240) */ 3367 * if mode1 (320x240) */
3354/* ?? was 3368/* ?? was
3355 {0xa0, 0x00, 0x0039}, * 00,00,00,dd * 3369 {0xa0, 0x00, 0x0039}, * 00,00,00,dd *
3356 {0xa1, 0x01, 0x0037}, */ 3370 {0xa1, 0x01, 0x0037}, */
@@ -3439,7 +3453,6 @@ static const struct usb_action ov7630c_InitialScale[] = {
3439 {0xa0, 0xf8, ZC3XX_R110_RGB20}, 3453 {0xa0, 0xf8, ZC3XX_R110_RGB20},
3440 {0xa0, 0xf8, ZC3XX_R111_RGB21}, 3454 {0xa0, 0xf8, ZC3XX_R111_RGB21},
3441 {0xa0, 0x50, ZC3XX_R112_RGB22}, 3455 {0xa0, 0x50, ZC3XX_R112_RGB22},
3442/* 0x03, */
3443 {0xa1, 0x01, 0x0008}, 3456 {0xa1, 0x01, 0x0008},
3444 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING}, /* clock ? */ 3457 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING}, /* clock ? */
3445 {0xa0, 0x08, ZC3XX_R1C6_SHARPNESS00}, /* sharpness+ */ 3458 {0xa0, 0x08, ZC3XX_R1C6_SHARPNESS00}, /* sharpness+ */
@@ -3719,7 +3732,7 @@ static const struct usb_action pas106b_InitialScale[] = { /* 176x144 */
3719 {0xaa, 0x0e, 0x0002}, 3732 {0xaa, 0x0e, 0x0002},
3720 {0xaa, 0x14, 0x0081}, 3733 {0xaa, 0x14, 0x0081},
3721 3734
3722/* Other registors */ 3735/* Other registers */
3723 {0xa0, 0x37, ZC3XX_R101_SENSORCORRECTION}, 3736 {0xa0, 0x37, ZC3XX_R101_SENSORCORRECTION},
3724/* Frame retreiving */ 3737/* Frame retreiving */
3725 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, 3738 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
@@ -3730,7 +3743,7 @@ static const struct usb_action pas106b_InitialScale[] = { /* 176x144 */
3730/* Sharpness */ 3743/* Sharpness */
3731 {0xa0, 0x03, ZC3XX_R1C5_SHARPNESSMODE}, 3744 {0xa0, 0x03, ZC3XX_R1C5_SHARPNESSMODE},
3732 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05}, 3745 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05},
3733/* Other registors */ 3746/* Other registers */
3734 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE}, 3747 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE},
3735/* Auto exposure and white balance */ 3748/* Auto exposure and white balance */
3736 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS}, 3749 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS},
@@ -3837,7 +3850,7 @@ static const struct usb_action pas106b_Initial[] = { /* 352x288 */
3837 {0xaa, 0x0e, 0x0002}, 3850 {0xaa, 0x0e, 0x0002},
3838 {0xaa, 0x14, 0x0081}, 3851 {0xaa, 0x14, 0x0081},
3839 3852
3840/* Other registors */ 3853/* Other registers */
3841 {0xa0, 0x37, ZC3XX_R101_SENSORCORRECTION}, 3854 {0xa0, 0x37, ZC3XX_R101_SENSORCORRECTION},
3842/* Frame retreiving */ 3855/* Frame retreiving */
3843 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, 3856 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
@@ -3848,7 +3861,7 @@ static const struct usb_action pas106b_Initial[] = { /* 352x288 */
3848/* Sharpness */ 3861/* Sharpness */
3849 {0xa0, 0x03, ZC3XX_R1C5_SHARPNESSMODE}, 3862 {0xa0, 0x03, ZC3XX_R1C5_SHARPNESSMODE},
3850 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05}, 3863 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05},
3851/* Other registors */ 3864/* Other registers */
3852 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE}, 3865 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE},
3853/* Auto exposure and white balance */ 3866/* Auto exposure and white balance */
3854 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS}, 3867 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS},
@@ -4241,8 +4254,8 @@ static const struct usb_action pas202b_NoFlikerScale[] = {
4241 {} 4254 {}
4242}; 4255};
4243 4256
4244/* mi0360soc and pb0330 from vm30x.inf for 0ac8:301b and 0ac8:303b 07/02/13 */ 4257/* mt9v111 (mi0360soc) and pb0330 from vm30x.inf 0ac8:301b 07/02/13 */
4245static const struct usb_action mi0360soc_Initial[] = { /* 640x480 */ 4258static const struct usb_action mt9v111_1_Initial[] = { /* 640x480 */
4246 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL}, 4259 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
4247 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING}, 4260 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
4248 {0xa0, 0x0a, ZC3XX_R010_CMOSSENSORSELECT}, 4261 {0xa0, 0x0a, ZC3XX_R010_CMOSSENSORSELECT},
@@ -4253,14 +4266,14 @@ static const struct usb_action mi0360soc_Initial[] = { /* 640x480 */
4253 {0xa0, 0xe0, ZC3XX_R006_FRAMEHEIGHTLOW}, 4266 {0xa0, 0xe0, ZC3XX_R006_FRAMEHEIGHTLOW},
4254 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR}, 4267 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
4255 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING}, 4268 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING},
4256 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC}, /*jfm: was 03*/ 4269 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC},
4257/* {0xa0, 0x01, ZC3XX_R012_VIDEOCONTROLFUNC}, */
4258 {0xa0, 0x00, ZC3XX_R098_WINYSTARTLOW}, 4270 {0xa0, 0x00, ZC3XX_R098_WINYSTARTLOW},
4259 {0xa0, 0x00, ZC3XX_R09A_WINXSTARTLOW}, 4271 {0xa0, 0x00, ZC3XX_R09A_WINXSTARTLOW},
4260 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW}, 4272 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW},
4261 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW}, 4273 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW},
4262 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR}, 4274 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
4263 {0xdd, 0x00, 0x0200}, 4275 {0xdd, 0x00, 0x0200},
4276 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
4264 {0xaa, 0x01, 0x0001}, 4277 {0xaa, 0x01, 0x0001},
4265 {0xaa, 0x06, 0x0000}, 4278 {0xaa, 0x06, 0x0000},
4266 {0xaa, 0x08, 0x0483}, 4279 {0xaa, 0x08, 0x0483},
@@ -4270,18 +4283,18 @@ static const struct usb_action mi0360soc_Initial[] = { /* 640x480 */
4270 {0xaa, 0x03, 0x01e5}, /*jfm: was 01e7*/ 4283 {0xaa, 0x03, 0x01e5}, /*jfm: was 01e7*/
4271 {0xaa, 0x04, 0x0285}, /*jfm: was 0287*/ 4284 {0xaa, 0x04, 0x0285}, /*jfm: was 0287*/
4272 {0xaa, 0x07, 0x3002}, 4285 {0xaa, 0x07, 0x3002},
4273 {0xaa, 0x20, 0x5100}, /*jfm: was 1100*/ 4286 {0xaa, 0x20, 0x5100},
4274 {0xaa, 0x35, 0x507f}, /*jfm: was 0050*/ 4287 {0xaa, 0x35, 0x507f},
4275 {0xaa, 0x30, 0x0005}, 4288 {0xaa, 0x30, 0x0005},
4276 {0xaa, 0x31, 0x0000}, 4289 {0xaa, 0x31, 0x0000},
4277 {0xaa, 0x58, 0x0078}, 4290 {0xaa, 0x58, 0x0078},
4278 {0xaa, 0x62, 0x0411}, 4291 {0xaa, 0x62, 0x0411},
4279 {0xaa, 0x2b, 0x0028}, 4292 {0xaa, 0x2b, 0x007f},
4280 {0xaa, 0x2c, 0x007f}, /*jfm: was 0030*/ 4293 {0xaa, 0x2c, 0x007f}, /*jfm: was 0030*/
4281 {0xaa, 0x2d, 0x007f}, /*jfm: was 0030*/ 4294 {0xaa, 0x2d, 0x007f}, /*jfm: was 0030*/
4282 {0xaa, 0x2e, 0x007f}, /*jfm: was 0030*/ 4295 {0xaa, 0x2e, 0x007f}, /*jfm: was 0030*/
4283 {0xa0, 0x10, ZC3XX_R087_EXPTIMEMID}, 4296 {0xa0, 0x10, ZC3XX_R087_EXPTIMEMID},
4284 {0xa0, 0xb7, ZC3XX_R101_SENSORCORRECTION}, /*jfm: was 37*/ 4297 {0xa0, 0xb7, ZC3XX_R101_SENSORCORRECTION},
4285 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC}, 4298 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
4286 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE}, 4299 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE},
4287 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS}, 4300 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS},
@@ -4291,12 +4304,12 @@ static const struct usb_action mi0360soc_Initial[] = { /* 640x480 */
4291 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE}, 4304 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE},
4292 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS}, 4305 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS},
4293 {0xa0, 0x60, ZC3XX_R1A8_DIGITALGAIN}, 4306 {0xa0, 0x60, ZC3XX_R1A8_DIGITALGAIN},
4294 {0xa0, 0x6c, ZC3XX_R18D_YTARGET}, /* jfm: was 78 */ 4307 {0xa0, 0x6c, ZC3XX_R18D_YTARGET},
4295 {0xa0, 0x61, ZC3XX_R116_RGAIN}, 4308 {0xa0, 0x61, ZC3XX_R116_RGAIN},
4296 {0xa0, 0x65, ZC3XX_R118_BGAIN}, 4309 {0xa0, 0x65, ZC3XX_R118_BGAIN},
4297 {} 4310 {}
4298}; 4311};
4299static const struct usb_action mi0360soc_InitialScale[] = { /* 320x240 */ 4312static const struct usb_action mt9v111_1_InitialScale[] = { /* 320x240 */
4300 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL}, 4313 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
4301 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING}, 4314 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
4302 {0xa0, 0x0a, ZC3XX_R010_CMOSSENSORSELECT}, 4315 {0xa0, 0x0a, ZC3XX_R010_CMOSSENSORSELECT},
@@ -4307,14 +4320,14 @@ static const struct usb_action mi0360soc_InitialScale[] = { /* 320x240 */
4307 {0xa0, 0xe0, ZC3XX_R006_FRAMEHEIGHTLOW}, 4320 {0xa0, 0xe0, ZC3XX_R006_FRAMEHEIGHTLOW},
4308 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR}, 4321 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
4309 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING}, 4322 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING},
4310 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC}, /*jfm: was 03*/ 4323 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC},
4311/* {0xa0, 0x01, ZC3XX_R012_VIDEOCONTROLFUNC}, */
4312 {0xa0, 0x00, ZC3XX_R098_WINYSTARTLOW}, 4324 {0xa0, 0x00, ZC3XX_R098_WINYSTARTLOW},
4313 {0xa0, 0x00, ZC3XX_R09A_WINXSTARTLOW}, 4325 {0xa0, 0x00, ZC3XX_R09A_WINXSTARTLOW},
4314 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW}, 4326 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW},
4315 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW}, 4327 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW},
4316 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR}, 4328 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
4317 {0xdd, 0x00, 0x0200}, 4329 {0xdd, 0x00, 0x0200},
4330 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
4318 {0xaa, 0x01, 0x0001}, 4331 {0xaa, 0x01, 0x0001},
4319 {0xaa, 0x06, 0x0000}, 4332 {0xaa, 0x06, 0x0000},
4320 {0xaa, 0x08, 0x0483}, 4333 {0xaa, 0x08, 0x0483},
@@ -4324,7 +4337,7 @@ static const struct usb_action mi0360soc_InitialScale[] = { /* 320x240 */
4324 {0xaa, 0x03, 0x01e7}, 4337 {0xaa, 0x03, 0x01e7},
4325 {0xaa, 0x04, 0x0287}, 4338 {0xaa, 0x04, 0x0287},
4326 {0xaa, 0x07, 0x3002}, 4339 {0xaa, 0x07, 0x3002},
4327 {0xaa, 0x20, 0x5100}, /*jfm: was 1100*/ 4340 {0xaa, 0x20, 0x5100},
4328 {0xaa, 0x35, 0x007f}, /*jfm: was 0050*/ 4341 {0xaa, 0x35, 0x007f}, /*jfm: was 0050*/
4329 {0xaa, 0x30, 0x0005}, 4342 {0xaa, 0x30, 0x0005},
4330 {0xaa, 0x31, 0x0000}, 4343 {0xaa, 0x31, 0x0000},
@@ -4335,7 +4348,7 @@ static const struct usb_action mi0360soc_InitialScale[] = { /* 320x240 */
4335 {0xaa, 0x2d, 0x007f}, /*jfm: was 30*/ 4348 {0xaa, 0x2d, 0x007f}, /*jfm: was 30*/
4336 {0xaa, 0x2e, 0x007f}, /*jfm: was 28*/ 4349 {0xaa, 0x2e, 0x007f}, /*jfm: was 28*/
4337 {0xa0, 0x10, ZC3XX_R087_EXPTIMEMID}, 4350 {0xa0, 0x10, ZC3XX_R087_EXPTIMEMID},
4338 {0xa0, 0xb7, ZC3XX_R101_SENSORCORRECTION}, /*jfm: was 37*/ 4351 {0xa0, 0xb7, ZC3XX_R101_SENSORCORRECTION},
4339 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC}, 4352 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
4340 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE}, 4353 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE},
4341 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS}, 4354 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS},
@@ -4345,12 +4358,12 @@ static const struct usb_action mi0360soc_InitialScale[] = { /* 320x240 */
4345 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE}, 4358 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE},
4346 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS}, 4359 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS},
4347 {0xa0, 0x60, ZC3XX_R1A8_DIGITALGAIN}, 4360 {0xa0, 0x60, ZC3XX_R1A8_DIGITALGAIN},
4348 {0xa0, 0x6c, ZC3XX_R18D_YTARGET}, /*jfm: was 78*/ 4361 {0xa0, 0x6c, ZC3XX_R18D_YTARGET},
4349 {0xa0, 0x61, ZC3XX_R116_RGAIN}, 4362 {0xa0, 0x61, ZC3XX_R116_RGAIN},
4350 {0xa0, 0x65, ZC3XX_R118_BGAIN}, 4363 {0xa0, 0x65, ZC3XX_R118_BGAIN},
4351 {} 4364 {}
4352}; 4365};
4353static const struct usb_action mi360soc_AE50HZ[] = { 4366static const struct usb_action mt9v111_1_AE50HZ[] = {
4354 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE}, 4367 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4355 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, 4368 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4356 {0xbb, 0x00, 0x0562}, 4369 {0xbb, 0x00, 0x0562},
@@ -4373,7 +4386,7 @@ static const struct usb_action mi360soc_AE50HZ[] = {
4373 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE}, 4386 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4374 {} 4387 {}
4375}; 4388};
4376static const struct usb_action mi360soc_AE50HZScale[] = { 4389static const struct usb_action mt9v111_1_AE50HZScale[] = {
4377 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE}, 4390 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4378 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, 4391 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4379 {0xbb, 0x00, 0x0509}, 4392 {0xbb, 0x00, 0x0509},
@@ -4395,11 +4408,11 @@ static const struct usb_action mi360soc_AE50HZScale[] = {
4395 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE}, 4408 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4396 {} 4409 {}
4397}; 4410};
4398static const struct usb_action mi360soc_AE60HZ[] = { 4411static const struct usb_action mt9v111_1_AE60HZ[] = {
4399 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE}, 4412 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4400 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, 4413 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4401 {0xbb, 0x00, 0x053d}, 4414 {0xaa, 0x05, 0x003d},
4402 {0xbb, 0x01, 0x096e}, 4415 {0xaa, 0x09, 0x016e},
4403 {0xa0, 0x00, ZC3XX_R190_EXPOSURELIMITHIGH}, 4416 {0xa0, 0x00, ZC3XX_R190_EXPOSURELIMITHIGH},
4404 {0xa0, 0x07, ZC3XX_R191_EXPOSURELIMITMID}, 4417 {0xa0, 0x07, ZC3XX_R191_EXPOSURELIMITMID},
4405 {0xa0, 0xdd, ZC3XX_R192_EXPOSURELIMITLOW}, 4418 {0xa0, 0xdd, ZC3XX_R192_EXPOSURELIMITLOW},
@@ -4418,7 +4431,7 @@ static const struct usb_action mi360soc_AE60HZ[] = {
4418 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE}, 4431 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4419 {} 4432 {}
4420}; 4433};
4421static const struct usb_action mi360soc_AE60HZScale[] = { 4434static const struct usb_action mt9v111_1_AE60HZScale[] = {
4422 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE}, 4435 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4423 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, 4436 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4424 {0xbb, 0x00, 0x0509}, 4437 {0xbb, 0x00, 0x0509},
@@ -4440,7 +4453,7 @@ static const struct usb_action mi360soc_AE60HZScale[] = {
4440 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE}, 4453 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4441 {} 4454 {}
4442}; 4455};
4443static const struct usb_action mi360soc_AENoFliker[] = { 4456static const struct usb_action mt9v111_1_AENoFliker[] = {
4444 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE}, 4457 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4445 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, 4458 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4446 {0xbb, 0x00, 0x0509}, 4459 {0xbb, 0x00, 0x0509},
@@ -4463,7 +4476,7 @@ static const struct usb_action mi360soc_AENoFliker[] = {
4463 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE}, 4476 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4464 {} 4477 {}
4465}; 4478};
4466static const struct usb_action mi360soc_AENoFlikerScale[] = { 4479static const struct usb_action mt9v111_1_AENoFlikerScale[] = {
4467 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE}, 4480 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4468 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, 4481 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4469 {0xbb, 0x00, 0x0534}, 4482 {0xbb, 0x00, 0x0534},
@@ -4486,6 +4499,251 @@ static const struct usb_action mi360soc_AENoFlikerScale[] = {
4486 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE}, 4499 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4487 {} 4500 {}
4488}; 4501};
4502/* from usbvm303.inf 0ac8:303b 07/03/25 (3 - tas5130c) */
4503static const struct usb_action mt9v111_3_Initial[] = {
4504 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
4505 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
4506 {0xa0, 0x0a, ZC3XX_R010_CMOSSENSORSELECT},
4507 {0xa0, 0x04, ZC3XX_R002_CLOCKSELECT},
4508 {0xa0, 0x02, ZC3XX_R003_FRAMEWIDTHHIGH},
4509 {0xa0, 0x80, ZC3XX_R004_FRAMEWIDTHLOW},
4510 {0xa0, 0x01, ZC3XX_R005_FRAMEHEIGHTHIGH},
4511 {0xa0, 0xe0, ZC3XX_R006_FRAMEHEIGHTLOW},
4512 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
4513 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING},
4514 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC},
4515 {0xa0, 0x00, ZC3XX_R098_WINYSTARTLOW},
4516 {0xa0, 0x00, ZC3XX_R09A_WINXSTARTLOW},
4517 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW},
4518 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW},
4519 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
4520 {0xdd, 0x00, 0x0200},
4521 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
4522 {0xaa, 0x01, 0x0001}, /* select IFP/SOC registers */
4523 {0xaa, 0x06, 0x0000}, /* operating mode control */
4524 {0xaa, 0x08, 0x0483}, /* output format control */
4525 /* H red first, V red or blue first,
4526 * raw Bayer, auto flicker */
4527 {0xaa, 0x01, 0x0004}, /* select sensor core registers */
4528 {0xaa, 0x08, 0x0006}, /* row start */
4529 {0xaa, 0x02, 0x0011}, /* column start */
4530 {0xaa, 0x03, 0x01e5}, /* window height - 1 */
4531 {0xaa, 0x04, 0x0285}, /* window width - 1 */
4532 {0xaa, 0x07, 0x3002}, /* output control */
4533 {0xaa, 0x20, 0x1100}, /* read mode: bits 8 & 12 (?) */
4534 {0xaa, 0x35, 0x007f}, /* global gain */
4535 {0xaa, 0x30, 0x0005},
4536 {0xaa, 0x31, 0x0000},
4537 {0xaa, 0x58, 0x0078},
4538 {0xaa, 0x62, 0x0411},
4539 {0xaa, 0x2b, 0x007f}, /* green1 gain */
4540 {0xaa, 0x2c, 0x007f}, /* blue gain */
4541 {0xaa, 0x2d, 0x007f}, /* red gain */
4542 {0xaa, 0x2e, 0x007f}, /* green2 gain */
4543 {0xa0, 0x10, ZC3XX_R087_EXPTIMEMID},
4544 {0xa0, 0x37, ZC3XX_R101_SENSORCORRECTION},
4545 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
4546 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE},
4547 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS},
4548 {0xa0, 0x00, 0x01ad},
4549 {0xa0, 0x03, ZC3XX_R1C5_SHARPNESSMODE},
4550 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05},
4551 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE},
4552 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS},
4553 {0xa0, 0x60, ZC3XX_R1A8_DIGITALGAIN},
4554 {0xa0, 0x80, ZC3XX_R18D_YTARGET},
4555 {0xa0, 0x61, ZC3XX_R116_RGAIN},
4556 {0xa0, 0x65, ZC3XX_R118_BGAIN},
4557 {}
4558};
4559static const struct usb_action mt9v111_3_InitialScale[] = {
4560 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
4561 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
4562 {0xa0, 0x0a, ZC3XX_R010_CMOSSENSORSELECT},
4563 {0xa0, 0x10, ZC3XX_R002_CLOCKSELECT},
4564 {0xa0, 0x02, ZC3XX_R003_FRAMEWIDTHHIGH},
4565 {0xa0, 0x80, ZC3XX_R004_FRAMEWIDTHLOW},
4566 {0xa0, 0x01, ZC3XX_R005_FRAMEHEIGHTHIGH},
4567 {0xa0, 0xe0, ZC3XX_R006_FRAMEHEIGHTLOW},
4568 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
4569 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING},
4570 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC},
4571 {0xa0, 0x00, ZC3XX_R098_WINYSTARTLOW},
4572 {0xa0, 0x00, ZC3XX_R09A_WINXSTARTLOW},
4573 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW},
4574 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW},
4575 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
4576 {0xdd, 0x00, 0x0200},
4577 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
4578 {0xaa, 0x01, 0x0001},
4579 {0xaa, 0x06, 0x0000},
4580 {0xaa, 0x08, 0x0483},
4581 {0xaa, 0x01, 0x0004},
4582 {0xaa, 0x08, 0x0006},
4583 {0xaa, 0x02, 0x0011},
4584 {0xaa, 0x03, 0x01e7},
4585 {0xaa, 0x04, 0x0287},
4586 {0xaa, 0x07, 0x3002},
4587 {0xaa, 0x20, 0x1100},
4588 {0xaa, 0x35, 0x007f},
4589 {0xaa, 0x30, 0x0005},
4590 {0xaa, 0x31, 0x0000},
4591 {0xaa, 0x58, 0x0078},
4592 {0xaa, 0x62, 0x0411},
4593 {0xaa, 0x2b, 0x007f},
4594 {0xaa, 0x2c, 0x007f},
4595 {0xaa, 0x2d, 0x007f},
4596 {0xaa, 0x2e, 0x007f},
4597 {0xa0, 0x10, ZC3XX_R087_EXPTIMEMID},
4598 {0xa0, 0x37, ZC3XX_R101_SENSORCORRECTION},
4599 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
4600 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE},
4601 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS},
4602 {0xa0, 0x00, 0x01ad},
4603 {0xa0, 0x03, ZC3XX_R1C5_SHARPNESSMODE},
4604 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05},
4605 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE},
4606 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS},
4607 {0xa0, 0x60, ZC3XX_R1A8_DIGITALGAIN},
4608 {0xa0, 0x80, ZC3XX_R18D_YTARGET},
4609 {0xa0, 0x61, ZC3XX_R116_RGAIN},
4610 {0xa0, 0x65, ZC3XX_R118_BGAIN},
4611 {}
4612};
4613static const struct usb_action mt9v111_3_AE50HZ[] = {
4614 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4615 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4616 {0xaa, 0x05, 0x0009}, /* horizontal blanking */
4617 {0xaa, 0x09, 0x01ce}, /* shutter width */
4618 {0xa0, 0x00, ZC3XX_R190_EXPOSURELIMITHIGH},
4619 {0xa0, 0x07, ZC3XX_R191_EXPOSURELIMITMID},
4620 {0xa0, 0xd2, ZC3XX_R192_EXPOSURELIMITLOW},
4621 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
4622 {0xa0, 0x00, ZC3XX_R196_ANTIFLICKERMID},
4623 {0xa0, 0x9a, ZC3XX_R197_ANTIFLICKERLOW},
4624 {0xa0, 0x0e, ZC3XX_R18C_AEFREEZE},
4625 {0xa0, 0x1c, ZC3XX_R18F_AEUNFREEZE},
4626 {0xa0, 0x14, ZC3XX_R1A9_DIGITALLIMITDIFF},
4627 {0xa0, 0x24, ZC3XX_R1AA_DIGITALGAINSTEP},
4628 {0xa0, 0xd7, ZC3XX_R01D_HSYNC_0},
4629 {0xa0, 0xf4, ZC3XX_R01E_HSYNC_1},
4630 {0xa0, 0xf9, ZC3XX_R01F_HSYNC_2},
4631 {0xa0, 0xff, ZC3XX_R020_HSYNC_3},
4632 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4633 {}
4634};
4635static const struct usb_action mt9v111_3_AE50HZScale[] = {
4636 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4637 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4638 {0xaa, 0x05, 0x0009},
4639 {0xaa, 0x09, 0x01ce},
4640 {0xa0, 0x00, ZC3XX_R190_EXPOSURELIMITHIGH},
4641 {0xa0, 0x07, ZC3XX_R191_EXPOSURELIMITMID},
4642 {0xa0, 0xd2, ZC3XX_R192_EXPOSURELIMITLOW},
4643 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
4644 {0xa0, 0x00, ZC3XX_R196_ANTIFLICKERMID},
4645 {0xa0, 0x9a, ZC3XX_R197_ANTIFLICKERLOW},
4646 {0xa0, 0x0e, ZC3XX_R18C_AEFREEZE},
4647 {0xa0, 0x1c, ZC3XX_R18F_AEUNFREEZE},
4648 {0xa0, 0x14, ZC3XX_R1A9_DIGITALLIMITDIFF},
4649 {0xa0, 0x24, ZC3XX_R1AA_DIGITALGAINSTEP},
4650 {0xa0, 0xd7, ZC3XX_R01D_HSYNC_0},
4651 {0xa0, 0xf4, ZC3XX_R01E_HSYNC_1},
4652 {0xa0, 0xf9, ZC3XX_R01F_HSYNC_2},
4653 {0xa0, 0xff, ZC3XX_R020_HSYNC_3},
4654 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4655 {}
4656};
4657static const struct usb_action mt9v111_3_AE60HZ[] = {
4658 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4659 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4660 {0xaa, 0x05, 0x0009},
4661 {0xaa, 0x09, 0x0083},
4662 {0xa0, 0x00, ZC3XX_R190_EXPOSURELIMITHIGH},
4663 {0xa0, 0x07, ZC3XX_R191_EXPOSURELIMITMID},
4664 {0xa0, 0x8f, ZC3XX_R192_EXPOSURELIMITLOW},
4665 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
4666 {0xa0, 0x00, ZC3XX_R196_ANTIFLICKERMID},
4667 {0xa0, 0x81, ZC3XX_R197_ANTIFLICKERLOW},
4668 {0xa0, 0x0e, ZC3XX_R18C_AEFREEZE},
4669 {0xa0, 0x1c, ZC3XX_R18F_AEUNFREEZE},
4670 {0xa0, 0x14, ZC3XX_R1A9_DIGITALLIMITDIFF},
4671 {0xa0, 0x24, ZC3XX_R1AA_DIGITALGAINSTEP},
4672 {0xa0, 0xd7, ZC3XX_R01D_HSYNC_0},
4673 {0xa0, 0xf4, ZC3XX_R01E_HSYNC_1},
4674 {0xa0, 0xf9, ZC3XX_R01F_HSYNC_2},
4675 {0xa0, 0xff, ZC3XX_R020_HSYNC_3},
4676 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4677 {}
4678};
4679static const struct usb_action mt9v111_3_AE60HZScale[] = {
4680 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4681 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4682 {0xaa, 0x05, 0x0009},
4683 {0xaa, 0x09, 0x0083},
4684 {0xa0, 0x00, ZC3XX_R190_EXPOSURELIMITHIGH},
4685 {0xa0, 0x07, ZC3XX_R191_EXPOSURELIMITMID},
4686 {0xa0, 0x8f, ZC3XX_R192_EXPOSURELIMITLOW},
4687 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
4688 {0xa0, 0x00, ZC3XX_R196_ANTIFLICKERMID},
4689 {0xa0, 0x81, ZC3XX_R197_ANTIFLICKERLOW},
4690 {0xa0, 0x0e, ZC3XX_R18C_AEFREEZE},
4691 {0xa0, 0x1c, ZC3XX_R18F_AEUNFREEZE},
4692 {0xa0, 0x14, ZC3XX_R1A9_DIGITALLIMITDIFF},
4693 {0xa0, 0x24, ZC3XX_R1AA_DIGITALGAINSTEP},
4694 {0xa0, 0xd7, ZC3XX_R01D_HSYNC_0},
4695 {0xa0, 0xf4, ZC3XX_R01E_HSYNC_1},
4696 {0xa0, 0xf9, ZC3XX_R01F_HSYNC_2},
4697 {0xa0, 0xff, ZC3XX_R020_HSYNC_3},
4698 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4699 {}
4700};
4701static const struct usb_action mt9v111_3_AENoFliker[] = {
4702 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4703 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4704 {0xaa, 0x05, 0x0034},
4705 {0xaa, 0x09, 0x0260},
4706 {0xa0, 0x00, ZC3XX_R190_EXPOSURELIMITHIGH},
4707 {0xa0, 0x07, ZC3XX_R191_EXPOSURELIMITMID},
4708 {0xa0, 0xf0, ZC3XX_R192_EXPOSURELIMITLOW},
4709 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
4710 {0xa0, 0x00, ZC3XX_R196_ANTIFLICKERMID},
4711 {0xa0, 0x04, ZC3XX_R197_ANTIFLICKERLOW},
4712 {0xa0, 0x0e, ZC3XX_R18C_AEFREEZE},
4713 {0xa0, 0x1c, ZC3XX_R18F_AEUNFREEZE},
4714 {0xa0, 0x00, ZC3XX_R1A9_DIGITALLIMITDIFF},
4715 {0xa0, 0x00, ZC3XX_R1AA_DIGITALGAINSTEP},
4716 {0xa0, 0x34, ZC3XX_R01D_HSYNC_0},
4717 {0xa0, 0x60, ZC3XX_R01E_HSYNC_1},
4718 {0xa0, 0x90, ZC3XX_R01F_HSYNC_2},
4719 {0xa0, 0xe0, ZC3XX_R020_HSYNC_3},
4720 {0xa0, 0x60, ZC3XX_R11D_GLOBALGAIN},
4721 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4722 {}
4723};
4724static const struct usb_action mt9v111_3_AENoFlikerScale[] = {
4725 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
4726 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
4727 {0xaa, 0x05, 0x0034},
4728 {0xaa, 0x09, 0x0260},
4729 {0xa0, 0x00, ZC3XX_R190_EXPOSURELIMITHIGH},
4730 {0xa0, 0x07, ZC3XX_R191_EXPOSURELIMITMID},
4731 {0xa0, 0xf0, ZC3XX_R192_EXPOSURELIMITLOW},
4732 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
4733 {0xa0, 0x00, ZC3XX_R196_ANTIFLICKERMID},
4734 {0xa0, 0x04, ZC3XX_R197_ANTIFLICKERLOW},
4735 {0xa0, 0x0e, ZC3XX_R18C_AEFREEZE},
4736 {0xa0, 0x1c, ZC3XX_R18F_AEUNFREEZE},
4737 {0xa0, 0x00, ZC3XX_R1A9_DIGITALLIMITDIFF},
4738 {0xa0, 0x00, ZC3XX_R1AA_DIGITALGAINSTEP},
4739 {0xa0, 0x34, ZC3XX_R01D_HSYNC_0},
4740 {0xa0, 0x60, ZC3XX_R01E_HSYNC_1},
4741 {0xa0, 0x90, ZC3XX_R01F_HSYNC_2},
4742 {0xa0, 0xe0, ZC3XX_R020_HSYNC_3},
4743 {0xa0, 0x60, ZC3XX_R11D_GLOBALGAIN},
4744 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
4745 {}
4746};
4489 4747
4490static const struct usb_action pb0330_Initial[] = { /* 640x480 */ 4748static const struct usb_action pb0330_Initial[] = { /* 640x480 */
4491 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL}, 4749 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
@@ -4928,419 +5186,7 @@ static const struct usb_action po2030_NoFliker[] = {
4928 {} 5186 {}
4929}; 5187};
4930 5188
4931/* TEST */ 5189static const struct usb_action tas5130c_InitialScale[] = { /* 320x240 */
4932static const struct usb_action tas5130cK_InitialScale[] = {
4933 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
4934 {0xa0, 0x01, 0x003b},
4935 {0xa0, 0x0e, 0x003a},
4936 {0xa0, 0x01, 0x0038},
4937 {0xa0, 0x0b, 0x0039},
4938 {0xa0, 0x00, 0x0038},
4939 {0xa0, 0x0b, 0x0039},
4940 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
4941 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
4942 {0xa0, 0x0a, ZC3XX_R010_CMOSSENSORSELECT},
4943 {0xa0, 0x10, ZC3XX_R002_CLOCKSELECT},
4944 {0xa0, 0x02, ZC3XX_R003_FRAMEWIDTHHIGH},
4945 {0xa0, 0x80, ZC3XX_R004_FRAMEWIDTHLOW},
4946 {0xa0, 0x01, ZC3XX_R005_FRAMEHEIGHTHIGH},
4947 {0xa0, 0xe0, ZC3XX_R006_FRAMEHEIGHTLOW},
4948 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
4949 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING},
4950 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC},
4951 {0xa0, 0x00, ZC3XX_R098_WINYSTARTLOW},
4952 {0xa0, 0x00, ZC3XX_R09A_WINXSTARTLOW},
4953 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW},
4954 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW},
4955 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
4956 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
4957 {0xa0, 0x01, ZC3XX_R092_I2CADDRESSSELECT},
4958 {0xa0, 0x01, ZC3XX_R093_I2CSETVALUE},
4959 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
4960 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
4961 {0xa0, 0x06, ZC3XX_R092_I2CADDRESSSELECT},
4962 {0xa0, 0x00, ZC3XX_R093_I2CSETVALUE},
4963 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
4964 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
4965 {0xa0, 0x08, ZC3XX_R092_I2CADDRESSSELECT},
4966 {0xa0, 0x83, ZC3XX_R093_I2CSETVALUE},
4967 {0xa0, 0x04, ZC3XX_R094_I2CWRITEACK},
4968 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
4969 {0xa0, 0x01, ZC3XX_R092_I2CADDRESSSELECT},
4970 {0xa0, 0x04, ZC3XX_R093_I2CSETVALUE},
4971 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
4972 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
4973 {0xa0, 0x08, ZC3XX_R092_I2CADDRESSSELECT},
4974 {0xa0, 0x06, ZC3XX_R093_I2CSETVALUE},
4975 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
4976 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
4977 {0xa0, 0x02, ZC3XX_R092_I2CADDRESSSELECT},
4978 {0xa0, 0x11, ZC3XX_R093_I2CSETVALUE},
4979 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
4980 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
4981 {0xa0, 0x03, ZC3XX_R092_I2CADDRESSSELECT},
4982 {0xa0, 0xE7, ZC3XX_R093_I2CSETVALUE},
4983 {0xa0, 0x01, ZC3XX_R094_I2CWRITEACK},
4984 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
4985 {0xa0, 0x04, ZC3XX_R092_I2CADDRESSSELECT},
4986 {0xa0, 0x87, ZC3XX_R093_I2CSETVALUE},
4987 {0xa0, 0x02, ZC3XX_R094_I2CWRITEACK},
4988 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
4989 {0xa0, 0x07, ZC3XX_R092_I2CADDRESSSELECT},
4990 {0xa0, 0x02, ZC3XX_R093_I2CSETVALUE},
4991 {0xa0, 0x30, ZC3XX_R094_I2CWRITEACK},
4992 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
4993 {0xa0, 0x20, ZC3XX_R092_I2CADDRESSSELECT},
4994 {0xa0, 0x00, ZC3XX_R093_I2CSETVALUE},
4995 {0xa0, 0x51, ZC3XX_R094_I2CWRITEACK},
4996 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
4997 {0xa0, 0x35, ZC3XX_R092_I2CADDRESSSELECT},
4998 {0xa0, 0x7F, ZC3XX_R093_I2CSETVALUE},
4999 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5000 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5001 {0xa0, 0x30, ZC3XX_R092_I2CADDRESSSELECT},
5002 {0xa0, 0x05, ZC3XX_R093_I2CSETVALUE},
5003 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5004 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5005 {0xa0, 0x31, ZC3XX_R092_I2CADDRESSSELECT},
5006 {0xa0, 0x00, ZC3XX_R093_I2CSETVALUE},
5007 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5008 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5009 {0xa0, 0x58, ZC3XX_R092_I2CADDRESSSELECT},
5010 {0xa0, 0x78, ZC3XX_R093_I2CSETVALUE},
5011 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5012 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5013 {0xa0, 0x62, ZC3XX_R092_I2CADDRESSSELECT},
5014 {0xa0, 0x11, ZC3XX_R093_I2CSETVALUE},
5015 {0xa0, 0x04, ZC3XX_R094_I2CWRITEACK},
5016 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5017 {0xa0, 0x2B, ZC3XX_R092_I2CADDRESSSELECT},
5018 {0xa0, 0x7f, ZC3XX_R093_I2CSETVALUE},
5019 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5020 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5021 {0xa0, 0x2c, ZC3XX_R092_I2CADDRESSSELECT},
5022 {0xa0, 0x7f, ZC3XX_R093_I2CSETVALUE},
5023 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5024 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5025 {0xa0, 0x2D, ZC3XX_R092_I2CADDRESSSELECT},
5026 {0xa0, 0x7f, ZC3XX_R093_I2CSETVALUE},
5027 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5028 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5029 {0xa0, 0x2e, ZC3XX_R092_I2CADDRESSSELECT},
5030 {0xa0, 0x7f, ZC3XX_R093_I2CSETVALUE},
5031 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5032 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5033 {0xa0, 0x10, ZC3XX_R087_EXPTIMEMID},
5034 {0xa0, 0xb7, ZC3XX_R101_SENSORCORRECTION},
5035 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
5036 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE},
5037 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS},
5038 {0xa0, 0x09, 0x01ad},
5039 {0xa0, 0x03, ZC3XX_R1C5_SHARPNESSMODE},
5040 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05},
5041 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE},
5042 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS},
5043 {0xa0, 0x60, ZC3XX_R1A8_DIGITALGAIN},
5044 {0xa0, 0x6c, ZC3XX_R18D_YTARGET},
5045 {0xa0, 0x61, ZC3XX_R116_RGAIN},
5046 {0xa0, 0x65, ZC3XX_R118_BGAIN},
5047 {0xa0, 0x09, 0x01ad},
5048 {0xa0, 0x15, 0x01ae},
5049 {0xa0, 0x4c, ZC3XX_R10A_RGB00}, /* matrix */
5050 {0xa0, 0xf1, ZC3XX_R10B_RGB01},
5051 {0xa0, 0x03, ZC3XX_R10C_RGB02},
5052 {0xa0, 0xfe, ZC3XX_R10D_RGB10},
5053 {0xa0, 0x51, ZC3XX_R10E_RGB11},
5054 {0xa0, 0xf1, ZC3XX_R10F_RGB12},
5055 {0xa0, 0xec, ZC3XX_R110_RGB20},
5056 {0xa0, 0x03, ZC3XX_R111_RGB21},
5057 {0xa0, 0x51, ZC3XX_R112_RGB22},
5058 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
5059 {0xa0, 0x08, ZC3XX_R1C6_SHARPNESS00}, /* sharpness+ */
5060 {0xa0, 0x0f, ZC3XX_R1CB_SHARPNESS05}, /* sharpness- */
5061 {0xa0, 0x38, ZC3XX_R120_GAMMA00}, /* gamma > 5 */
5062 {0xa0, 0x51, ZC3XX_R121_GAMMA01},
5063 {0xa0, 0x6e, ZC3XX_R122_GAMMA02},
5064 {0xa0, 0x8c, ZC3XX_R123_GAMMA03},
5065 {0xa0, 0xa2, ZC3XX_R124_GAMMA04},
5066 {0xa0, 0xb6, ZC3XX_R125_GAMMA05},
5067 {0xa0, 0xc8, ZC3XX_R126_GAMMA06},
5068 {0xa0, 0xd6, ZC3XX_R127_GAMMA07},
5069 {0xa0, 0xe2, ZC3XX_R128_GAMMA08},
5070 {0xa0, 0xed, ZC3XX_R129_GAMMA09},
5071 {0xa0, 0xf5, ZC3XX_R12A_GAMMA0A},
5072 {0xa0, 0xfc, ZC3XX_R12B_GAMMA0B},
5073 {0xa0, 0xff, ZC3XX_R12C_GAMMA0C},
5074 {0xa0, 0xff, ZC3XX_R12D_GAMMA0D},
5075 {0xa0, 0xff, ZC3XX_R12E_GAMMA0E},
5076 {0xa0, 0xff, ZC3XX_R12F_GAMMA0F},
5077 {0xa0, 0x12, ZC3XX_R130_GAMMA10},
5078 {0xa0, 0x1b, ZC3XX_R131_GAMMA11},
5079 {0xa0, 0x1d, ZC3XX_R132_GAMMA12},
5080 {0xa0, 0x1a, ZC3XX_R133_GAMMA13},
5081 {0xa0, 0x15, ZC3XX_R134_GAMMA14},
5082 {0xa0, 0x12, ZC3XX_R135_GAMMA15},
5083 {0xa0, 0x0f, ZC3XX_R136_GAMMA16},
5084 {0xa0, 0x0d, ZC3XX_R137_GAMMA17},
5085 {0xa0, 0x0b, ZC3XX_R138_GAMMA18},
5086 {0xa0, 0x09, ZC3XX_R139_GAMMA19},
5087 {0xa0, 0x07, ZC3XX_R13A_GAMMA1A},
5088 {0xa0, 0x05, ZC3XX_R13B_GAMMA1B},
5089 {0xa0, 0x00, ZC3XX_R13C_GAMMA1C},
5090 {0xa0, 0x00, ZC3XX_R13D_GAMMA1D},
5091 {0xa0, 0x00, ZC3XX_R13E_GAMMA1E},
5092 {0xa0, 0x01, ZC3XX_R13F_GAMMA1F},
5093 {0xa0, 0x4c, ZC3XX_R10A_RGB00}, /* matrix */
5094 {0xa0, 0xf1, ZC3XX_R10B_RGB01},
5095 {0xa0, 0x03, ZC3XX_R10C_RGB02},
5096 {0xa0, 0xfe, ZC3XX_R10D_RGB10},
5097 {0xa0, 0x51, ZC3XX_R10E_RGB11},
5098 {0xa0, 0xf1, ZC3XX_R10F_RGB12},
5099 {0xa0, 0xec, ZC3XX_R110_RGB20},
5100 {0xa0, 0x03, ZC3XX_R111_RGB21},
5101 {0xa0, 0x51, ZC3XX_R112_RGB22},
5102 {0xa0, 0x10, ZC3XX_R180_AUTOCORRECTENABLE},
5103 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
5104 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
5105 {0xa0, 0x05, ZC3XX_R092_I2CADDRESSSELECT},
5106 {0xa0, 0x09, ZC3XX_R093_I2CSETVALUE},
5107 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5108 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5109 {0xa0, 0x09, ZC3XX_R092_I2CADDRESSSELECT},
5110 {0xa0, 0x34, ZC3XX_R093_I2CSETVALUE},
5111 {0xa0, 0x01, ZC3XX_R094_I2CWRITEACK},
5112 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5113 {0xa0, 0x00, ZC3XX_R190_EXPOSURELIMITHIGH},
5114 {0xa0, 0x07, ZC3XX_R191_EXPOSURELIMITMID},
5115 {0xa0, 0xd2, ZC3XX_R192_EXPOSURELIMITLOW},
5116 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
5117 {0xa0, 0x00, ZC3XX_R196_ANTIFLICKERMID},
5118 {0xa0, 0x9a, ZC3XX_R197_ANTIFLICKERLOW},
5119 {0xa0, 0x0e, ZC3XX_R18C_AEFREEZE},
5120 {0xa0, 0x1c, ZC3XX_R18F_AEUNFREEZE},
5121 {0xa0, 0x14, ZC3XX_R1A9_DIGITALLIMITDIFF},
5122 {0xa0, 0x66, ZC3XX_R1AA_DIGITALGAINSTEP},
5123 {0xa0, 0xd7, ZC3XX_R01D_HSYNC_0},
5124 {0xa0, 0xf4, ZC3XX_R01E_HSYNC_1},
5125 {0xa0, 0xf9, ZC3XX_R01F_HSYNC_2},
5126 {0xa0, 0xff, ZC3XX_R020_HSYNC_3},
5127 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
5128 {0xa0, 0x09, 0x01ad},
5129 {0xa0, 0x15, 0x01ae},
5130 {0xa0, 0x40, ZC3XX_R180_AUTOCORRECTENABLE},
5131 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
5132 {}
5133};
5134
5135static const struct usb_action tas5130cK_Initial[] = {
5136 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
5137 {0xa0, 0x01, 0x003b},
5138 {0xa0, 0x0e, 0x003a},
5139 {0xa0, 0x01, 0x0038},
5140 {0xa0, 0x0b, 0x0039},
5141 {0xa0, 0x00, 0x0038},
5142 {0xa0, 0x0b, 0x0039},
5143 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
5144 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
5145 {0xa0, 0x0a, ZC3XX_R010_CMOSSENSORSELECT},
5146 {0xa0, 0x00, ZC3XX_R002_CLOCKSELECT},
5147 {0xa0, 0x02, ZC3XX_R003_FRAMEWIDTHHIGH},
5148 {0xa0, 0x80, ZC3XX_R004_FRAMEWIDTHLOW},
5149 {0xa0, 0x01, ZC3XX_R005_FRAMEHEIGHTHIGH},
5150 {0xa0, 0xe0, ZC3XX_R006_FRAMEHEIGHTLOW},
5151 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
5152 {0xa0, 0x01, ZC3XX_R001_SYSTEMOPERATING},
5153 {0xa0, 0x07, ZC3XX_R012_VIDEOCONTROLFUNC},
5154 {0xa0, 0x00, ZC3XX_R098_WINYSTARTLOW},
5155 {0xa0, 0x00, ZC3XX_R09A_WINXSTARTLOW},
5156 {0xa0, 0x00, ZC3XX_R11A_FIRSTYLOW},
5157 {0xa0, 0x00, ZC3XX_R11C_FIRSTXLOW},
5158 {0xa0, 0xdc, ZC3XX_R08B_I2CDEVICEADDR},
5159 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
5160 {0xa0, 0x01, ZC3XX_R092_I2CADDRESSSELECT},
5161 {0xa0, 0x01, ZC3XX_R093_I2CSETVALUE},
5162 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5163 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5164 {0xa0, 0x06, ZC3XX_R092_I2CADDRESSSELECT},
5165 {0xa0, 0x00, ZC3XX_R093_I2CSETVALUE},
5166 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5167 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5168 {0xa0, 0x08, ZC3XX_R092_I2CADDRESSSELECT},
5169 {0xa0, 0x83, ZC3XX_R093_I2CSETVALUE},
5170 {0xa0, 0x04, ZC3XX_R094_I2CWRITEACK},
5171 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5172 {0xa0, 0x01, ZC3XX_R092_I2CADDRESSSELECT},
5173 {0xa0, 0x04, ZC3XX_R093_I2CSETVALUE},
5174 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5175 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5176 {0xa0, 0x08, ZC3XX_R092_I2CADDRESSSELECT},
5177 {0xa0, 0x06, ZC3XX_R093_I2CSETVALUE},
5178 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5179 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5180 {0xa0, 0x02, ZC3XX_R092_I2CADDRESSSELECT},
5181 {0xa0, 0x11, ZC3XX_R093_I2CSETVALUE},
5182 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5183 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5184 {0xa0, 0x03, ZC3XX_R092_I2CADDRESSSELECT},
5185 {0xa0, 0xe5, ZC3XX_R093_I2CSETVALUE},
5186 {0xa0, 0x01, ZC3XX_R094_I2CWRITEACK},
5187 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5188 {0xa0, 0x04, ZC3XX_R092_I2CADDRESSSELECT},
5189 {0xa0, 0x85, ZC3XX_R093_I2CSETVALUE},
5190 {0xa0, 0x02, ZC3XX_R094_I2CWRITEACK},
5191 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5192 {0xa0, 0x07, ZC3XX_R092_I2CADDRESSSELECT},
5193 {0xa0, 0x02, ZC3XX_R093_I2CSETVALUE},
5194 {0xa0, 0x30, ZC3XX_R094_I2CWRITEACK},
5195 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5196 {0xa0, 0x20, ZC3XX_R092_I2CADDRESSSELECT},
5197 {0xa0, 0x00, ZC3XX_R093_I2CSETVALUE},
5198 {0xa0, 0x51, ZC3XX_R094_I2CWRITEACK},
5199 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5200 {0xa0, 0x35, ZC3XX_R092_I2CADDRESSSELECT},
5201 {0xa0, 0x7F, ZC3XX_R093_I2CSETVALUE},
5202 {0xa0, 0x50, ZC3XX_R094_I2CWRITEACK},
5203 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5204 {0xa0, 0x30, ZC3XX_R092_I2CADDRESSSELECT},
5205 {0xa0, 0x05, ZC3XX_R093_I2CSETVALUE},
5206 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5207 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5208 {0xa0, 0x31, ZC3XX_R092_I2CADDRESSSELECT},
5209 {0xa0, 0x00, ZC3XX_R093_I2CSETVALUE},
5210 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5211 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5212 {0xa0, 0x58, ZC3XX_R092_I2CADDRESSSELECT},
5213 {0xa0, 0x78, ZC3XX_R093_I2CSETVALUE},
5214 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5215 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5216 {0xa0, 0x62, ZC3XX_R092_I2CADDRESSSELECT},
5217 {0xa0, 0x11, ZC3XX_R093_I2CSETVALUE},
5218 {0xa0, 0x04, ZC3XX_R094_I2CWRITEACK},
5219 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5220 {0xa0, 0x2B, ZC3XX_R092_I2CADDRESSSELECT},
5221 {0xa0, 0x7f, ZC3XX_R093_I2CSETVALUE},
5222 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5223 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5224 {0xa0, 0x2C, ZC3XX_R092_I2CADDRESSSELECT},
5225 {0xa0, 0x7F, ZC3XX_R093_I2CSETVALUE},
5226 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5227 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5228 {0xa0, 0x2D, ZC3XX_R092_I2CADDRESSSELECT},
5229 {0xa0, 0x7f, ZC3XX_R093_I2CSETVALUE},
5230 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5231 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5232 {0xa0, 0x2e, ZC3XX_R092_I2CADDRESSSELECT},
5233 {0xa0, 0x7f, ZC3XX_R093_I2CSETVALUE},
5234 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5235 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5236 {0xa0, 0x10, ZC3XX_R087_EXPTIMEMID},
5237 {0xa0, 0xb7, ZC3XX_R101_SENSORCORRECTION},
5238 {0xa0, 0x05, ZC3XX_R012_VIDEOCONTROLFUNC},
5239 {0xa0, 0x0d, ZC3XX_R100_OPERATIONMODE},
5240 {0xa0, 0x06, ZC3XX_R189_AWBSTATUS},
5241 {0xa0, 0x09, 0x01ad},
5242 {0xa0, 0x03, ZC3XX_R1C5_SHARPNESSMODE},
5243 {0xa0, 0x13, ZC3XX_R1CB_SHARPNESS05},
5244 {0xa0, 0x08, ZC3XX_R250_DEADPIXELSMODE},
5245 {0xa0, 0x08, ZC3XX_R301_EEPROMACCESS},
5246 {0xa0, 0x60, ZC3XX_R1A8_DIGITALGAIN},
5247 {0xa0, 0x6c, ZC3XX_R18D_YTARGET},
5248 {0xa0, 0x61, ZC3XX_R116_RGAIN},
5249 {0xa0, 0x65, ZC3XX_R118_BGAIN},
5250 {0xa0, 0x09, 0x01ad},
5251 {0xa0, 0x15, 0x01ae},
5252 {0xa0, 0x4c, ZC3XX_R10A_RGB00}, /* matrix */
5253 {0xa0, 0xf1, ZC3XX_R10B_RGB01},
5254 {0xa0, 0x03, ZC3XX_R10C_RGB02},
5255 {0xa0, 0xfe, ZC3XX_R10D_RGB10},
5256 {0xa0, 0x51, ZC3XX_R10E_RGB11},
5257 {0xa0, 0xf1, ZC3XX_R10F_RGB12},
5258 {0xa0, 0xec, ZC3XX_R110_RGB20},
5259 {0xa0, 0x03, ZC3XX_R111_RGB21},
5260 {0xa0, 0x51, ZC3XX_R112_RGB22},
5261 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
5262 {0xa0, 0x08, ZC3XX_R1C6_SHARPNESS00}, /* sharpness+ */
5263 {0xa0, 0x0f, ZC3XX_R1CB_SHARPNESS05}, /* sharpness- */
5264 {0xa0, 0x38, ZC3XX_R120_GAMMA00}, /* gamma > 5 */
5265 {0xa0, 0x51, ZC3XX_R121_GAMMA01},
5266 {0xa0, 0x6e, ZC3XX_R122_GAMMA02},
5267 {0xa0, 0x8c, ZC3XX_R123_GAMMA03},
5268 {0xa0, 0xa2, ZC3XX_R124_GAMMA04},
5269 {0xa0, 0xb6, ZC3XX_R125_GAMMA05},
5270 {0xa0, 0xc8, ZC3XX_R126_GAMMA06},
5271 {0xa0, 0xd6, ZC3XX_R127_GAMMA07},
5272 {0xa0, 0xe2, ZC3XX_R128_GAMMA08},
5273 {0xa0, 0xed, ZC3XX_R129_GAMMA09},
5274 {0xa0, 0xf5, ZC3XX_R12A_GAMMA0A},
5275 {0xa0, 0xfc, ZC3XX_R12B_GAMMA0B},
5276 {0xa0, 0xff, ZC3XX_R12C_GAMMA0C},
5277 {0xa0, 0xff, ZC3XX_R12D_GAMMA0D},
5278 {0xa0, 0xff, ZC3XX_R12E_GAMMA0E},
5279 {0xa0, 0xff, ZC3XX_R12F_GAMMA0F},
5280 {0xa0, 0x12, ZC3XX_R130_GAMMA10},
5281 {0xa0, 0x1b, ZC3XX_R131_GAMMA11},
5282 {0xa0, 0x1d, ZC3XX_R132_GAMMA12},
5283 {0xa0, 0x1a, ZC3XX_R133_GAMMA13},
5284 {0xa0, 0x15, ZC3XX_R134_GAMMA14},
5285 {0xa0, 0x12, ZC3XX_R135_GAMMA15},
5286 {0xa0, 0x0f, ZC3XX_R136_GAMMA16},
5287 {0xa0, 0x0d, ZC3XX_R137_GAMMA17},
5288 {0xa0, 0x0b, ZC3XX_R138_GAMMA18},
5289 {0xa0, 0x09, ZC3XX_R139_GAMMA19},
5290 {0xa0, 0x07, ZC3XX_R13A_GAMMA1A},
5291 {0xa0, 0x05, ZC3XX_R13B_GAMMA1B},
5292 {0xa0, 0x00, ZC3XX_R13C_GAMMA1C},
5293 {0xa0, 0x00, ZC3XX_R13D_GAMMA1D},
5294 {0xa0, 0x00, ZC3XX_R13E_GAMMA1E},
5295 {0xa0, 0x01, ZC3XX_R13F_GAMMA1F},
5296 {0xa0, 0x4c, ZC3XX_R10A_RGB00}, /* matrix */
5297 {0xa0, 0xf1, ZC3XX_R10B_RGB01},
5298 {0xa0, 0x03, ZC3XX_R10C_RGB02},
5299 {0xa0, 0xfe, ZC3XX_R10D_RGB10},
5300 {0xa0, 0x51, ZC3XX_R10E_RGB11},
5301 {0xa0, 0xf1, ZC3XX_R10F_RGB12},
5302 {0xa0, 0xec, ZC3XX_R110_RGB20},
5303 {0xa0, 0x03, ZC3XX_R111_RGB21},
5304 {0xa0, 0x51, ZC3XX_R112_RGB22},
5305 {0xa0, 0x10, ZC3XX_R180_AUTOCORRECTENABLE},
5306 {0xa0, 0x00, ZC3XX_R180_AUTOCORRECTENABLE},
5307 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS},
5308 {0xa0, 0x05, ZC3XX_R092_I2CADDRESSSELECT},
5309 {0xa0, 0x62, ZC3XX_R093_I2CSETVALUE},
5310 {0xa0, 0x00, ZC3XX_R094_I2CWRITEACK},
5311 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5312 {0xa0, 0x09, ZC3XX_R092_I2CADDRESSSELECT},
5313 {0xa0, 0xaa, ZC3XX_R093_I2CSETVALUE},
5314 {0xa0, 0x01, ZC3XX_R094_I2CWRITEACK},
5315 {0xa0, 0x01, ZC3XX_R090_I2CCOMMAND},
5316 {0xa0, 0x00, ZC3XX_R190_EXPOSURELIMITHIGH},
5317 {0xa0, 0x03, ZC3XX_R191_EXPOSURELIMITMID},
5318 {0xa0, 0x9b, ZC3XX_R192_EXPOSURELIMITLOW},
5319 {0xa0, 0x00, ZC3XX_R195_ANTIFLICKERHIGH},
5320 {0xa0, 0x00, ZC3XX_R196_ANTIFLICKERMID},
5321 {0xa0, 0x47, ZC3XX_R197_ANTIFLICKERLOW},
5322 {0xa0, 0x0e, ZC3XX_R18C_AEFREEZE},
5323 {0xa0, 0x1c, ZC3XX_R18F_AEUNFREEZE},
5324 {0xa0, 0x14, ZC3XX_R1A9_DIGITALLIMITDIFF},
5325 {0xa0, 0x66, ZC3XX_R1AA_DIGITALGAINSTEP},
5326 {0xa0, 0x62, ZC3XX_R01D_HSYNC_0},
5327 {0xa0, 0x90, ZC3XX_R01E_HSYNC_1},
5328 {0xa0, 0xc8, ZC3XX_R01F_HSYNC_2},
5329 {0xa0, 0xff, ZC3XX_R020_HSYNC_3},
5330 {0xa0, 0x60, ZC3XX_R11D_GLOBALGAIN},
5331 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
5332 {0xa0, 0x09, 0x01ad},
5333 {0xa0, 0x15, 0x01ae},
5334 {0xa0, 0x40, ZC3XX_R180_AUTOCORRECTENABLE},
5335 {0xa0, 0x42, ZC3XX_R180_AUTOCORRECTENABLE},
5336 {0xa0, 0x30, 0x0007},
5337 {0xa0, 0x02, ZC3XX_R008_CLOCKSETTING},
5338 {0xa0, 0x00, 0x0007},
5339 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
5340 {}
5341};
5342
5343static const struct usb_action tas5130cxx_InitialScale[] = { /* 320x240 */
5344 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL}, 5190 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
5345 {0xa0, 0x50, ZC3XX_R002_CLOCKSELECT}, 5191 {0xa0, 0x50, ZC3XX_R002_CLOCKSELECT},
5346 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING}, 5192 {0xa0, 0x03, ZC3XX_R008_CLOCKSETTING},
@@ -5377,7 +5223,7 @@ static const struct usb_action tas5130cxx_InitialScale[] = { /* 320x240 */
5377 {0xa0, 0x02, ZC3XX_R0A6_EXPOSUREBLACKLVL}, 5223 {0xa0, 0x02, ZC3XX_R0A6_EXPOSUREBLACKLVL},
5378 {} 5224 {}
5379}; 5225};
5380static const struct usb_action tas5130cxx_Initial[] = { /* 640x480 */ 5226static const struct usb_action tas5130c_Initial[] = { /* 640x480 */
5381 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL}, 5227 {0xa0, 0x01, ZC3XX_R000_SYSTEMCONTROL},
5382 {0xa0, 0x40, ZC3XX_R002_CLOCKSELECT}, 5228 {0xa0, 0x40, ZC3XX_R002_CLOCKSELECT},
5383 {0xa0, 0x00, ZC3XX_R008_CLOCKSETTING}, 5229 {0xa0, 0x00, ZC3XX_R008_CLOCKSETTING},
@@ -5413,7 +5259,7 @@ static const struct usb_action tas5130cxx_Initial[] = { /* 640x480 */
5413 {0xa0, 0x02, ZC3XX_R0A6_EXPOSUREBLACKLVL}, 5259 {0xa0, 0x02, ZC3XX_R0A6_EXPOSUREBLACKLVL},
5414 {} 5260 {}
5415}; 5261};
5416static const struct usb_action tas5130cxx_50HZ[] = { 5262static const struct usb_action tas5130c_50HZ[] = {
5417 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */ 5263 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */
5418 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */ 5264 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */
5419 {0xaa, 0xa4, 0x0063}, /* 00,a4,63,aa */ 5265 {0xaa, 0xa4, 0x0063}, /* 00,a4,63,aa */
@@ -5438,7 +5284,7 @@ static const struct usb_action tas5130cxx_50HZ[] = {
5438 {0xa0, 0x50, ZC3XX_R11D_GLOBALGAIN}, 5284 {0xa0, 0x50, ZC3XX_R11D_GLOBALGAIN},
5439 {} 5285 {}
5440}; 5286};
5441static const struct usb_action tas5130cxx_50HZScale[] = { 5287static const struct usb_action tas5130c_50HZScale[] = {
5442 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */ 5288 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */
5443 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */ 5289 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */
5444 {0xaa, 0xa4, 0x0077}, /* 00,a4,77,aa */ 5290 {0xaa, 0xa4, 0x0077}, /* 00,a4,77,aa */
@@ -5463,7 +5309,7 @@ static const struct usb_action tas5130cxx_50HZScale[] = {
5463 {0xa0, 0x50, ZC3XX_R11D_GLOBALGAIN}, 5309 {0xa0, 0x50, ZC3XX_R11D_GLOBALGAIN},
5464 {} 5310 {}
5465}; 5311};
5466static const struct usb_action tas5130cxx_60HZ[] = { 5312static const struct usb_action tas5130c_60HZ[] = {
5467 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */ 5313 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */
5468 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */ 5314 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */
5469 {0xaa, 0xa4, 0x0036}, /* 00,a4,36,aa */ 5315 {0xaa, 0xa4, 0x0036}, /* 00,a4,36,aa */
@@ -5488,7 +5334,7 @@ static const struct usb_action tas5130cxx_60HZ[] = {
5488 {0xa0, 0x50, ZC3XX_R11D_GLOBALGAIN}, 5334 {0xa0, 0x50, ZC3XX_R11D_GLOBALGAIN},
5489 {} 5335 {}
5490}; 5336};
5491static const struct usb_action tas5130cxx_60HZScale[] = { 5337static const struct usb_action tas5130c_60HZScale[] = {
5492 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */ 5338 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */
5493 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */ 5339 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */
5494 {0xaa, 0xa4, 0x0077}, /* 00,a4,77,aa */ 5340 {0xaa, 0xa4, 0x0077}, /* 00,a4,77,aa */
@@ -5513,7 +5359,7 @@ static const struct usb_action tas5130cxx_60HZScale[] = {
5513 {0xa0, 0x50, ZC3XX_R11D_GLOBALGAIN}, 5359 {0xa0, 0x50, ZC3XX_R11D_GLOBALGAIN},
5514 {} 5360 {}
5515}; 5361};
5516static const struct usb_action tas5130cxx_NoFliker[] = { 5362static const struct usb_action tas5130c_NoFliker[] = {
5517 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */ 5363 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */
5518 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */ 5364 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */
5519 {0xaa, 0xa4, 0x0040}, /* 00,a4,40,aa */ 5365 {0xaa, 0xa4, 0x0040}, /* 00,a4,40,aa */
@@ -5539,7 +5385,7 @@ static const struct usb_action tas5130cxx_NoFliker[] = {
5539 {} 5385 {}
5540}; 5386};
5541 5387
5542static const struct usb_action tas5130cxx_NoFlikerScale[] = { 5388static const struct usb_action tas5130c_NoFlikerScale[] = {
5543 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */ 5389 {0xa0, 0x00, ZC3XX_R019_AUTOADJUSTFPS}, /* 00,19,00,cc */
5544 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */ 5390 {0xaa, 0xa3, 0x0001}, /* 00,a3,01,aa */
5545 {0xaa, 0xa4, 0x0090}, /* 00,a4,90,aa */ 5391 {0xaa, 0xa4, 0x0090}, /* 00,a4,90,aa */
@@ -5840,13 +5686,22 @@ static const struct usb_action tas5130c_vf0250_NoFliker[] = {
5840static u8 reg_r_i(struct gspca_dev *gspca_dev, 5686static u8 reg_r_i(struct gspca_dev *gspca_dev,
5841 u16 index) 5687 u16 index)
5842{ 5688{
5843 usb_control_msg(gspca_dev->dev, 5689 int ret;
5690
5691 if (gspca_dev->usb_err < 0)
5692 return 0;
5693 ret = usb_control_msg(gspca_dev->dev,
5844 usb_rcvctrlpipe(gspca_dev->dev, 0), 5694 usb_rcvctrlpipe(gspca_dev->dev, 0),
5845 0xa1, 5695 0xa1,
5846 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 5696 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
5847 0x01, /* value */ 5697 0x01, /* value */
5848 index, gspca_dev->usb_buf, 1, 5698 index, gspca_dev->usb_buf, 1,
5849 500); 5699 500);
5700 if (ret < 0) {
5701 PDEBUG(D_ERR, "reg_r_i err %d", ret);
5702 gspca_dev->usb_err = ret;
5703 return 0;
5704 }
5850 return gspca_dev->usb_buf[0]; 5705 return gspca_dev->usb_buf[0];
5851} 5706}
5852 5707
@@ -5860,24 +5715,32 @@ static u8 reg_r(struct gspca_dev *gspca_dev,
5860 return ret; 5715 return ret;
5861} 5716}
5862 5717
5863static void reg_w_i(struct usb_device *dev, 5718static void reg_w_i(struct gspca_dev *gspca_dev,
5864 u8 value, 5719 u8 value,
5865 u16 index) 5720 u16 index)
5866{ 5721{
5867 usb_control_msg(dev, 5722 int ret;
5868 usb_sndctrlpipe(dev, 0), 5723
5724 if (gspca_dev->usb_err < 0)
5725 return;
5726 ret = usb_control_msg(gspca_dev->dev,
5727 usb_sndctrlpipe(gspca_dev->dev, 0),
5869 0xa0, 5728 0xa0,
5870 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 5729 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
5871 value, index, NULL, 0, 5730 value, index, NULL, 0,
5872 500); 5731 500);
5732 if (ret < 0) {
5733 PDEBUG(D_ERR, "reg_w_i err %d", ret);
5734 gspca_dev->usb_err = ret;
5735 }
5873} 5736}
5874 5737
5875static void reg_w(struct usb_device *dev, 5738static void reg_w(struct gspca_dev *gspca_dev,
5876 u8 value, 5739 u8 value,
5877 u16 index) 5740 u16 index)
5878{ 5741{
5879 PDEBUG(D_USBO, "reg w [%04x] = %02x", index, value); 5742 PDEBUG(D_USBO, "reg w [%04x] = %02x", index, value);
5880 reg_w_i(dev, value, index); 5743 reg_w_i(gspca_dev, value, index);
5881} 5744}
5882 5745
5883static u16 i2c_read(struct gspca_dev *gspca_dev, 5746static u16 i2c_read(struct gspca_dev *gspca_dev,
@@ -5886,8 +5749,10 @@ static u16 i2c_read(struct gspca_dev *gspca_dev,
5886 u8 retbyte; 5749 u8 retbyte;
5887 u16 retval; 5750 u16 retval;
5888 5751
5889 reg_w_i(gspca_dev->dev, reg, 0x0092); 5752 if (gspca_dev->usb_err < 0)
5890 reg_w_i(gspca_dev->dev, 0x02, 0x0090); /* <- read command */ 5753 return 0;
5754 reg_w_i(gspca_dev, reg, 0x0092);
5755 reg_w_i(gspca_dev, 0x02, 0x0090); /* <- read command */
5891 msleep(20); 5756 msleep(20);
5892 retbyte = reg_r_i(gspca_dev, 0x0091); /* read status */ 5757 retbyte = reg_r_i(gspca_dev, 0x0091); /* read status */
5893 if (retbyte != 0x00) 5758 if (retbyte != 0x00)
@@ -5906,10 +5771,12 @@ static u8 i2c_write(struct gspca_dev *gspca_dev,
5906{ 5771{
5907 u8 retbyte; 5772 u8 retbyte;
5908 5773
5909 reg_w_i(gspca_dev->dev, reg, 0x92); 5774 if (gspca_dev->usb_err < 0)
5910 reg_w_i(gspca_dev->dev, valL, 0x93); 5775 return 0;
5911 reg_w_i(gspca_dev->dev, valH, 0x94); 5776 reg_w_i(gspca_dev, reg, 0x92);
5912 reg_w_i(gspca_dev->dev, 0x01, 0x90); /* <- write command */ 5777 reg_w_i(gspca_dev, valL, 0x93);
5778 reg_w_i(gspca_dev, valH, 0x94);
5779 reg_w_i(gspca_dev, 0x01, 0x90); /* <- write command */
5913 msleep(1); 5780 msleep(1);
5914 retbyte = reg_r_i(gspca_dev, 0x0091); /* read status */ 5781 retbyte = reg_r_i(gspca_dev, 0x0091); /* read status */
5915 if (retbyte != 0x00) 5782 if (retbyte != 0x00)
@@ -5925,7 +5792,7 @@ static void usb_exchange(struct gspca_dev *gspca_dev,
5925 while (action->req) { 5792 while (action->req) {
5926 switch (action->req) { 5793 switch (action->req) {
5927 case 0xa0: /* write register */ 5794 case 0xa0: /* write register */
5928 reg_w(gspca_dev->dev, action->val, action->idx); 5795 reg_w(gspca_dev, action->val, action->idx);
5929 break; 5796 break;
5930 case 0xa1: /* read status */ 5797 case 0xa1: /* read status */
5931 reg_r(gspca_dev, action->idx); 5798 reg_r(gspca_dev, action->idx);
@@ -5974,38 +5841,37 @@ static void setmatrix(struct gspca_dev *gspca_dev)
5974 static const u8 vf0250_matrix[9] = 5841 static const u8 vf0250_matrix[9] =
5975 {0x7b, 0xea, 0xea, 0xea, 0x7b, 0xea, 0xea, 0xea, 0x7b}; 5842 {0x7b, 0xea, 0xea, 0xea, 0x7b, 0xea, 0xea, 0xea, 0x7b};
5976 static const u8 *matrix_tb[SENSOR_MAX] = { 5843 static const u8 *matrix_tb[SENSOR_MAX] = {
5977 adcm2700_matrix, /* SENSOR_ADCM2700 0 */ 5844 [SENSOR_ADCM2700] = adcm2700_matrix,
5978 ov7620_matrix, /* SENSOR_CS2102 1 */ 5845 [SENSOR_CS2102] = ov7620_matrix,
5979 NULL, /* SENSOR_CS2102K 2 */ 5846 [SENSOR_CS2102K] = NULL,
5980 gc0305_matrix, /* SENSOR_GC0305 3 */ 5847 [SENSOR_GC0305] = gc0305_matrix,
5981 NULL, /* SENSOR_HDCS2020b 4 */ 5848 [SENSOR_HDCS2020b] = NULL,
5982 NULL, /* SENSOR_HV7131B 5 */ 5849 [SENSOR_HV7131B] = NULL,
5983 NULL, /* SENSOR_HV7131C 6 */ 5850 [SENSOR_HV7131R] = NULL,
5984 NULL, /* SENSOR_ICM105A 7 */ 5851 [SENSOR_ICM105A] = po2030_matrix,
5985 NULL, /* SENSOR_MC501CB 8 */ 5852 [SENSOR_MC501CB] = NULL,
5986 gc0305_matrix, /* SENSOR_MI0360SOC 9 */ 5853 [SENSOR_MT9V111_1] = gc0305_matrix,
5987 ov7620_matrix, /* SENSOR_OV7620 10 */ 5854 [SENSOR_MT9V111_3] = gc0305_matrix,
5988 NULL, /* SENSOR_OV7630C 11 */ 5855 [SENSOR_OV7620] = ov7620_matrix,
5989 NULL, /* SENSOR_PAS106 12 */ 5856 [SENSOR_OV7630C] = NULL,
5990 pas202b_matrix, /* SENSOR_PAS202B 13 */ 5857 [SENSOR_PAS106] = NULL,
5991 gc0305_matrix, /* SENSOR_PB0330 14 */ 5858 [SENSOR_PAS202B] = pas202b_matrix,
5992 po2030_matrix, /* SENSOR_PO2030 15 */ 5859 [SENSOR_PB0330] = gc0305_matrix,
5993 NULL, /* SENSOR_TAS5130CK 16 */ 5860 [SENSOR_PO2030] = po2030_matrix,
5994 tas5130c_matrix, /* SENSOR_TAS5130CXX 17 */ 5861 [SENSOR_TAS5130C] = tas5130c_matrix,
5995 vf0250_matrix, /* SENSOR_TAS5130C_VF0250 18 */ 5862 [SENSOR_TAS5130C_VF0250] = vf0250_matrix,
5996 }; 5863 };
5997 5864
5998 matrix = matrix_tb[sd->sensor]; 5865 matrix = matrix_tb[sd->sensor];
5999 if (matrix == NULL) 5866 if (matrix == NULL)
6000 return; /* matrix already loaded */ 5867 return; /* matrix already loaded */
6001 for (i = 0; i < ARRAY_SIZE(ov7620_matrix); i++) 5868 for (i = 0; i < ARRAY_SIZE(ov7620_matrix); i++)
6002 reg_w(gspca_dev->dev, matrix[i], 0x010a + i); 5869 reg_w(gspca_dev, matrix[i], 0x010a + i);
6003} 5870}
6004 5871
6005static void setsharpness(struct gspca_dev *gspca_dev) 5872static void setsharpness(struct gspca_dev *gspca_dev)
6006{ 5873{
6007 struct sd *sd = (struct sd *) gspca_dev; 5874 struct sd *sd = (struct sd *) gspca_dev;
6008 struct usb_device *dev = gspca_dev->dev;
6009 int sharpness; 5875 int sharpness;
6010 static const u8 sharpness_tb[][2] = { 5876 static const u8 sharpness_tb[][2] = {
6011 {0x02, 0x03}, 5877 {0x02, 0x03},
@@ -6015,17 +5881,16 @@ static void setsharpness(struct gspca_dev *gspca_dev)
6015 }; 5881 };
6016 5882
6017 sharpness = sd->sharpness; 5883 sharpness = sd->sharpness;
6018 reg_w(dev, sharpness_tb[sharpness][0], 0x01c6); 5884 reg_w(gspca_dev, sharpness_tb[sharpness][0], 0x01c6);
6019 reg_r(gspca_dev, 0x01c8); 5885 reg_r(gspca_dev, 0x01c8);
6020 reg_r(gspca_dev, 0x01c9); 5886 reg_r(gspca_dev, 0x01c9);
6021 reg_r(gspca_dev, 0x01ca); 5887 reg_r(gspca_dev, 0x01ca);
6022 reg_w(dev, sharpness_tb[sharpness][1], 0x01cb); 5888 reg_w(gspca_dev, sharpness_tb[sharpness][1], 0x01cb);
6023} 5889}
6024 5890
6025static void setcontrast(struct gspca_dev *gspca_dev) 5891static void setcontrast(struct gspca_dev *gspca_dev)
6026{ 5892{
6027 struct sd *sd = (struct sd *) gspca_dev; 5893 struct sd *sd = (struct sd *) gspca_dev;
6028 struct usb_device *dev = gspca_dev->dev;
6029 const u8 *Tgamma; 5894 const u8 *Tgamma;
6030 int g, i, brightness, contrast, adj, gp1, gp2; 5895 int g, i, brightness, contrast, adj, gp1, gp2;
6031 u8 gr[16]; 5896 u8 gr[16];
@@ -6063,7 +5928,7 @@ static void setcontrast(struct gspca_dev *gspca_dev)
6063 g = 0xff; 5928 g = 0xff;
6064 else if (g < 0) 5929 else if (g < 0)
6065 g = 0; 5930 g = 0;
6066 reg_w(dev, g, 0x0120 + i); /* gamma */ 5931 reg_w(gspca_dev, g, 0x0120 + i); /* gamma */
6067 if (contrast > 0) 5932 if (contrast > 0)
6068 adj--; 5933 adj--;
6069 else if (contrast < 0) 5934 else if (contrast < 0)
@@ -6077,13 +5942,12 @@ static void setcontrast(struct gspca_dev *gspca_dev)
6077 } 5942 }
6078 gr[15] = (0xff - gp2) / 2; 5943 gr[15] = (0xff - gp2) / 2;
6079 for (i = 0; i < 16; i++) 5944 for (i = 0; i < 16; i++)
6080 reg_w(dev, gr[i], 0x0130 + i); /* gradient */ 5945 reg_w(gspca_dev, gr[i], 0x0130 + i); /* gradient */
6081} 5946}
6082 5947
6083static void setquality(struct gspca_dev *gspca_dev) 5948static void setquality(struct gspca_dev *gspca_dev)
6084{ 5949{
6085 struct sd *sd = (struct sd *) gspca_dev; 5950 struct sd *sd = (struct sd *) gspca_dev;
6086 struct usb_device *dev = gspca_dev->dev;
6087 u8 frxt; 5951 u8 frxt;
6088 5952
6089 switch (sd->sensor) { 5953 switch (sd->sensor) {
@@ -6096,9 +5960,9 @@ static void setquality(struct gspca_dev *gspca_dev)
6096 return; 5960 return;
6097 } 5961 }
6098/*fixme: is it really 0008 0007 0018 for all other sensors? */ 5962/*fixme: is it really 0008 0007 0018 for all other sensors? */
6099 reg_w(dev, QUANT_VAL, 0x0008); 5963 reg_w(gspca_dev, QUANT_VAL, 0x0008);
6100 frxt = 0x30; 5964 frxt = 0x30;
6101 reg_w(dev, frxt, 0x0007); 5965 reg_w(gspca_dev, frxt, 0x0007);
6102#if QUANT_VAL == 0 || QUANT_VAL == 1 || QUANT_VAL == 2 5966#if QUANT_VAL == 0 || QUANT_VAL == 1 || QUANT_VAL == 2
6103 frxt = 0xff; 5967 frxt = 0xff;
6104#elif QUANT_VAL == 3 5968#elif QUANT_VAL == 3
@@ -6108,7 +5972,7 @@ static void setquality(struct gspca_dev *gspca_dev)
6108#else 5972#else
6109 frxt = 0x20; 5973 frxt = 0x20;
6110#endif 5974#endif
6111 reg_w(dev, frxt, 0x0018); 5975 reg_w(gspca_dev, frxt, 0x0018);
6112} 5976}
6113 5977
6114/* Matches the sensor's internal frame rate to the lighting frequency. 5978/* Matches the sensor's internal frame rate to the lighting frequency.
@@ -6116,87 +5980,86 @@ static void setquality(struct gspca_dev *gspca_dev)
6116 * 50Hz, for European and Asian lighting (default) 5980 * 50Hz, for European and Asian lighting (default)
6117 * 60Hz, for American lighting 5981 * 60Hz, for American lighting
6118 * 0 = No Fliker (for outdoore usage) 5982 * 0 = No Fliker (for outdoore usage)
6119 * Returns: 0 for success
6120 */ 5983 */
6121static int setlightfreq(struct gspca_dev *gspca_dev) 5984static void setlightfreq(struct gspca_dev *gspca_dev)
6122{ 5985{
6123 struct sd *sd = (struct sd *) gspca_dev; 5986 struct sd *sd = (struct sd *) gspca_dev;
6124 int i, mode; 5987 int i, mode;
6125 const struct usb_action *zc3_freq; 5988 const struct usb_action *zc3_freq;
6126 static const struct usb_action *freq_tb[SENSOR_MAX][6] = { 5989 static const struct usb_action *freq_tb[SENSOR_MAX][6] = {
6127/* SENSOR_ADCM2700 0 */ 5990 [SENSOR_ADCM2700] =
6128 {adcm2700_NoFliker, adcm2700_NoFliker, 5991 {adcm2700_NoFliker, adcm2700_NoFliker,
6129 adcm2700_50HZ, adcm2700_50HZ, 5992 adcm2700_50HZ, adcm2700_50HZ,
6130 adcm2700_60HZ, adcm2700_60HZ}, 5993 adcm2700_60HZ, adcm2700_60HZ},
6131/* SENSOR_CS2102 1 */ 5994 [SENSOR_CS2102] =
6132 {cs2102_NoFliker, cs2102_NoFlikerScale, 5995 {cs2102_NoFliker, cs2102_NoFlikerScale,
6133 cs2102_50HZ, cs2102_50HZScale, 5996 cs2102_50HZ, cs2102_50HZScale,
6134 cs2102_60HZ, cs2102_60HZScale}, 5997 cs2102_60HZ, cs2102_60HZScale},
6135/* SENSOR_CS2102K 2 */ 5998 [SENSOR_CS2102K] =
6136 {cs2102_NoFliker, cs2102_NoFlikerScale, 5999 {cs2102_NoFliker, cs2102_NoFlikerScale,
6137 NULL, NULL, /* currently disabled */ 6000 NULL, NULL, /* currently disabled */
6138 NULL, NULL}, 6001 NULL, NULL},
6139/* SENSOR_GC0305 3 */ 6002 [SENSOR_GC0305] =
6140 {gc0305_NoFliker, gc0305_NoFliker, 6003 {gc0305_NoFliker, gc0305_NoFliker,
6141 gc0305_50HZ, gc0305_50HZ, 6004 gc0305_50HZ, gc0305_50HZ,
6142 gc0305_60HZ, gc0305_60HZ}, 6005 gc0305_60HZ, gc0305_60HZ},
6143/* SENSOR_HDCS2020b 4 */ 6006 [SENSOR_HDCS2020b] =
6144 {hdcs2020b_NoFliker, hdcs2020b_NoFliker, 6007 {hdcs2020b_NoFliker, hdcs2020b_NoFliker,
6145 hdcs2020b_50HZ, hdcs2020b_50HZ, 6008 hdcs2020b_50HZ, hdcs2020b_50HZ,
6146 hdcs2020b_60HZ, hdcs2020b_60HZ}, 6009 hdcs2020b_60HZ, hdcs2020b_60HZ},
6147/* SENSOR_HV7131B 5 */ 6010 [SENSOR_HV7131B] =
6148 {hv7131b_NoFliker, hv7131b_NoFlikerScale, 6011 {hv7131b_NoFliker, hv7131b_NoFlikerScale,
6149 hv7131b_50HZ, hv7131b_50HZScale, 6012 hv7131b_50HZ, hv7131b_50HZScale,
6150 hv7131b_60HZ, hv7131b_60HZScale}, 6013 hv7131b_60HZ, hv7131b_60HZScale},
6151/* SENSOR_HV7131C 6 */ 6014 [SENSOR_HV7131R] =
6152 {NULL, NULL, 6015 {hv7131r_NoFliker, hv7131r_NoFlikerScale,
6153 NULL, NULL, 6016 hv7131r_50HZ, hv7131r_50HZScale,
6154 NULL, NULL}, 6017 hv7131r_60HZ, hv7131r_60HZScale},
6155/* SENSOR_ICM105A 7 */ 6018 [SENSOR_ICM105A] =
6156 {icm105a_NoFliker, icm105a_NoFlikerScale, 6019 {icm105a_NoFliker, icm105a_NoFlikerScale,
6157 icm105a_50HZ, icm105a_50HZScale, 6020 icm105a_50HZ, icm105a_50HZScale,
6158 icm105a_60HZ, icm105a_60HZScale}, 6021 icm105a_60HZ, icm105a_60HZScale},
6159/* SENSOR_MC501CB 8 */ 6022 [SENSOR_MC501CB] =
6160 {mc501cb_NoFliker, mc501cb_NoFlikerScale, 6023 {mc501cb_NoFliker, mc501cb_NoFlikerScale,
6161 mc501cb_50HZ, mc501cb_50HZScale, 6024 mc501cb_50HZ, mc501cb_50HZScale,
6162 mc501cb_60HZ, mc501cb_60HZScale}, 6025 mc501cb_60HZ, mc501cb_60HZScale},
6163/* SENSOR_MI0360SOC 9 */ 6026 [SENSOR_MT9V111_1] =
6164 {mi360soc_AENoFliker, mi360soc_AENoFlikerScale, 6027 {mt9v111_1_AENoFliker, mt9v111_1_AENoFlikerScale,
6165 mi360soc_AE50HZ, mi360soc_AE50HZScale, 6028 mt9v111_1_AE50HZ, mt9v111_1_AE50HZScale,
6166 mi360soc_AE60HZ, mi360soc_AE60HZScale}, 6029 mt9v111_1_AE60HZ, mt9v111_1_AE60HZScale},
6167/* SENSOR_OV7620 10 */ 6030 [SENSOR_MT9V111_3] =
6031 {mt9v111_3_AENoFliker, mt9v111_3_AENoFlikerScale,
6032 mt9v111_3_AE50HZ, mt9v111_3_AE50HZScale,
6033 mt9v111_3_AE60HZ, mt9v111_3_AE60HZScale},
6034 [SENSOR_OV7620] =
6168 {ov7620_NoFliker, ov7620_NoFliker, 6035 {ov7620_NoFliker, ov7620_NoFliker,
6169 ov7620_50HZ, ov7620_50HZ, 6036 ov7620_50HZ, ov7620_50HZ,
6170 ov7620_60HZ, ov7620_60HZ}, 6037 ov7620_60HZ, ov7620_60HZ},
6171/* SENSOR_OV7630C 11 */ 6038 [SENSOR_OV7630C] =
6172 {NULL, NULL, 6039 {NULL, NULL,
6173 NULL, NULL, 6040 NULL, NULL,
6174 NULL, NULL}, 6041 NULL, NULL},
6175/* SENSOR_PAS106 12 */ 6042 [SENSOR_PAS106] =
6176 {pas106b_NoFliker, pas106b_NoFliker, 6043 {pas106b_NoFliker, pas106b_NoFliker,
6177 pas106b_50HZ, pas106b_50HZ, 6044 pas106b_50HZ, pas106b_50HZ,
6178 pas106b_60HZ, pas106b_60HZ}, 6045 pas106b_60HZ, pas106b_60HZ},
6179/* SENSOR_PAS202B 13 */ 6046 [SENSOR_PAS202B] =
6180 {pas202b_NoFliker, pas202b_NoFlikerScale, 6047 {pas202b_NoFliker, pas202b_NoFlikerScale,
6181 pas202b_50HZ, pas202b_50HZScale, 6048 pas202b_50HZ, pas202b_50HZScale,
6182 pas202b_60HZ, pas202b_60HZScale}, 6049 pas202b_60HZ, pas202b_60HZScale},
6183/* SENSOR_PB0330 14 */ 6050 [SENSOR_PB0330] =
6184 {pb0330_NoFliker, pb0330_NoFlikerScale, 6051 {pb0330_NoFliker, pb0330_NoFlikerScale,
6185 pb0330_50HZ, pb0330_50HZScale, 6052 pb0330_50HZ, pb0330_50HZScale,
6186 pb0330_60HZ, pb0330_60HZScale}, 6053 pb0330_60HZ, pb0330_60HZScale},
6187/* SENSOR_PO2030 15 */ 6054 [SENSOR_PO2030] =
6188 {po2030_NoFliker, po2030_NoFliker, 6055 {po2030_NoFliker, po2030_NoFliker,
6189 po2030_50HZ, po2030_50HZ, 6056 po2030_50HZ, po2030_50HZ,
6190 po2030_60HZ, po2030_60HZ}, 6057 po2030_60HZ, po2030_60HZ},
6191/* SENSOR_TAS5130CK 16 */ 6058 [SENSOR_TAS5130C] =
6192 {tas5130cxx_NoFliker, tas5130cxx_NoFlikerScale, 6059 {tas5130c_NoFliker, tas5130c_NoFlikerScale,
6193 tas5130cxx_50HZ, tas5130cxx_50HZScale, 6060 tas5130c_50HZ, tas5130c_50HZScale,
6194 tas5130cxx_60HZ, tas5130cxx_60HZScale}, 6061 tas5130c_60HZ, tas5130c_60HZScale},
6195/* SENSOR_TAS5130CXX 17 */ 6062 [SENSOR_TAS5130C_VF0250] =
6196 {tas5130cxx_NoFliker, tas5130cxx_NoFlikerScale,
6197 tas5130cxx_50HZ, tas5130cxx_50HZScale,
6198 tas5130cxx_60HZ, tas5130cxx_60HZScale},
6199/* SENSOR_TAS5130C_VF0250 18 */
6200 {tas5130c_vf0250_NoFliker, tas5130c_vf0250_NoFlikerScale, 6063 {tas5130c_vf0250_NoFliker, tas5130c_vf0250_NoFlikerScale,
6201 tas5130c_vf0250_50HZ, tas5130c_vf0250_50HZScale, 6064 tas5130c_vf0250_50HZ, tas5130c_vf0250_50HZScale,
6202 tas5130c_vf0250_60HZ, tas5130c_vf0250_60HZScale}, 6065 tas5130c_vf0250_60HZ, tas5130c_vf0250_60HZScale},
@@ -6207,29 +6070,28 @@ static int setlightfreq(struct gspca_dev *gspca_dev)
6207 if (mode) 6070 if (mode)
6208 i++; /* 320x240 */ 6071 i++; /* 320x240 */
6209 zc3_freq = freq_tb[sd->sensor][i]; 6072 zc3_freq = freq_tb[sd->sensor][i];
6210 if (zc3_freq != NULL) { 6073 if (zc3_freq == NULL)
6211 usb_exchange(gspca_dev, zc3_freq); 6074 return;
6212 switch (sd->sensor) { 6075 usb_exchange(gspca_dev, zc3_freq);
6213 case SENSOR_GC0305: 6076 switch (sd->sensor) {
6214 if (mode /* if 320x240 */ 6077 case SENSOR_GC0305:
6215 && sd->lightfreq == 1) /* and 50Hz */ 6078 if (mode /* if 320x240 */
6216 reg_w(gspca_dev->dev, 0x85, 0x018d); 6079 && sd->lightfreq == 1) /* and 50Hz */
6217 /* win: 0x80, 0x018d */ 6080 reg_w(gspca_dev, 0x85, 0x018d);
6218 break; 6081 /* win: 0x80, 0x018d */
6219 case SENSOR_OV7620: 6082 break;
6220 if (!mode) { /* if 640x480 */ 6083 case SENSOR_OV7620:
6221 if (sd->lightfreq != 0) /* and 50 or 60 Hz */ 6084 if (!mode) { /* if 640x480 */
6222 reg_w(gspca_dev->dev, 0x40, 0x0002); 6085 if (sd->lightfreq != 0) /* and 50 or 60 Hz */
6223 else 6086 reg_w(gspca_dev, 0x40, 0x0002);
6224 reg_w(gspca_dev->dev, 0x44, 0x0002); 6087 else
6225 } 6088 reg_w(gspca_dev, 0x44, 0x0002);
6226 break;
6227 case SENSOR_PAS202B:
6228 reg_w(gspca_dev->dev, 0x00, 0x01a7);
6229 break;
6230 } 6089 }
6090 break;
6091 case SENSOR_PAS202B:
6092 reg_w(gspca_dev, 0x00, 0x01a7);
6093 break;
6231 } 6094 }
6232 return 0;
6233} 6095}
6234 6096
6235static void setautogain(struct gspca_dev *gspca_dev) 6097static void setautogain(struct gspca_dev *gspca_dev)
@@ -6241,45 +6103,46 @@ static void setautogain(struct gspca_dev *gspca_dev)
6241 autoval = 0x42; 6103 autoval = 0x42;
6242 else 6104 else
6243 autoval = 0x02; 6105 autoval = 0x02;
6244 reg_w(gspca_dev->dev, autoval, 0x0180); 6106 reg_w(gspca_dev, autoval, 0x0180);
6245} 6107}
6246 6108
6247static void send_unknown(struct usb_device *dev, int sensor) 6109static void send_unknown(struct gspca_dev *gspca_dev, int sensor)
6248{ 6110{
6249 reg_w(dev, 0x01, 0x0000); /* led off */ 6111 reg_w(gspca_dev, 0x01, 0x0000); /* led off */
6250 switch (sensor) { 6112 switch (sensor) {
6251 case SENSOR_PAS106: 6113 case SENSOR_PAS106:
6252 reg_w(dev, 0x03, 0x003a); 6114 reg_w(gspca_dev, 0x03, 0x003a);
6253 reg_w(dev, 0x0c, 0x003b); 6115 reg_w(gspca_dev, 0x0c, 0x003b);
6254 reg_w(dev, 0x08, 0x0038); 6116 reg_w(gspca_dev, 0x08, 0x0038);
6255 break; 6117 break;
6256 case SENSOR_ADCM2700: 6118 case SENSOR_ADCM2700:
6257 case SENSOR_GC0305: 6119 case SENSOR_GC0305:
6258 case SENSOR_OV7620: 6120 case SENSOR_OV7620:
6259 case SENSOR_MI0360SOC: 6121 case SENSOR_MT9V111_1:
6122 case SENSOR_MT9V111_3:
6260 case SENSOR_PB0330: 6123 case SENSOR_PB0330:
6261 case SENSOR_PO2030: 6124 case SENSOR_PO2030:
6262 reg_w(dev, 0x0d, 0x003a); 6125 reg_w(gspca_dev, 0x0d, 0x003a);
6263 reg_w(dev, 0x02, 0x003b); 6126 reg_w(gspca_dev, 0x02, 0x003b);
6264 reg_w(dev, 0x00, 0x0038); 6127 reg_w(gspca_dev, 0x00, 0x0038);
6265 break; 6128 break;
6266 case SENSOR_PAS202B: 6129 case SENSOR_PAS202B:
6267 reg_w(dev, 0x03, 0x003b); 6130 reg_w(gspca_dev, 0x03, 0x003b);
6268 reg_w(dev, 0x0c, 0x003a); 6131 reg_w(gspca_dev, 0x0c, 0x003a);
6269 reg_w(dev, 0x0b, 0x0039); 6132 reg_w(gspca_dev, 0x0b, 0x0039);
6270 reg_w(dev, 0x0b, 0x0038); 6133 reg_w(gspca_dev, 0x0b, 0x0038);
6271 break; 6134 break;
6272 } 6135 }
6273} 6136}
6274 6137
6275/* start probe 2 wires */ 6138/* start probe 2 wires */
6276static void start_2wr_probe(struct usb_device *dev, int sensor) 6139static void start_2wr_probe(struct gspca_dev *gspca_dev, int sensor)
6277{ 6140{
6278 reg_w(dev, 0x01, 0x0000); 6141 reg_w(gspca_dev, 0x01, 0x0000);
6279 reg_w(dev, sensor, 0x0010); 6142 reg_w(gspca_dev, sensor, 0x0010);
6280 reg_w(dev, 0x01, 0x0001); 6143 reg_w(gspca_dev, 0x01, 0x0001);
6281 reg_w(dev, 0x03, 0x0012); 6144 reg_w(gspca_dev, 0x03, 0x0012);
6282 reg_w(dev, 0x01, 0x0012); 6145 reg_w(gspca_dev, 0x01, 0x0012);
6283/* msleep(2); */ 6146/* msleep(2); */
6284} 6147}
6285 6148
@@ -6287,14 +6150,14 @@ static int sif_probe(struct gspca_dev *gspca_dev)
6287{ 6150{
6288 u16 checkword; 6151 u16 checkword;
6289 6152
6290 start_2wr_probe(gspca_dev->dev, 0x0f); /* PAS106 */ 6153 start_2wr_probe(gspca_dev, 0x0f); /* PAS106 */
6291 reg_w(gspca_dev->dev, 0x08, 0x008d); 6154 reg_w(gspca_dev, 0x08, 0x008d);
6292 msleep(150); 6155 msleep(150);
6293 checkword = ((i2c_read(gspca_dev, 0x00) & 0x0f) << 4) 6156 checkword = ((i2c_read(gspca_dev, 0x00) & 0x0f) << 4)
6294 | ((i2c_read(gspca_dev, 0x01) & 0xf0) >> 4); 6157 | ((i2c_read(gspca_dev, 0x01) & 0xf0) >> 4);
6295 PDEBUG(D_PROBE, "probe sif 0x%04x", checkword); 6158 PDEBUG(D_PROBE, "probe sif 0x%04x", checkword);
6296 if (checkword == 0x0007) { 6159 if (checkword == 0x0007) {
6297 send_unknown(gspca_dev->dev, SENSOR_PAS106); 6160 send_unknown(gspca_dev, SENSOR_PAS106);
6298 return 0x0f; /* PAS106 */ 6161 return 0x0f; /* PAS106 */
6299 } 6162 }
6300 return -1; 6163 return -1;
@@ -6302,23 +6165,22 @@ static int sif_probe(struct gspca_dev *gspca_dev)
6302 6165
6303static int vga_2wr_probe(struct gspca_dev *gspca_dev) 6166static int vga_2wr_probe(struct gspca_dev *gspca_dev)
6304{ 6167{
6305 struct usb_device *dev = gspca_dev->dev;
6306 u16 retword; 6168 u16 retword;
6307 6169
6308 start_2wr_probe(dev, 0x00); /* HV7131B */ 6170 start_2wr_probe(gspca_dev, 0x00); /* HV7131B */
6309 i2c_write(gspca_dev, 0x01, 0xaa, 0x00); 6171 i2c_write(gspca_dev, 0x01, 0xaa, 0x00);
6310 retword = i2c_read(gspca_dev, 0x01); 6172 retword = i2c_read(gspca_dev, 0x01);
6311 if (retword != 0) 6173 if (retword != 0)
6312 return 0x00; /* HV7131B */ 6174 return 0x00; /* HV7131B */
6313 6175
6314 start_2wr_probe(dev, 0x04); /* CS2102 */ 6176 start_2wr_probe(gspca_dev, 0x04); /* CS2102 */
6315 i2c_write(gspca_dev, 0x01, 0xaa, 0x00); 6177 i2c_write(gspca_dev, 0x01, 0xaa, 0x00);
6316 retword = i2c_read(gspca_dev, 0x01); 6178 retword = i2c_read(gspca_dev, 0x01);
6317 if (retword != 0) 6179 if (retword != 0)
6318 return 0x04; /* CS2102 */ 6180 return 0x04; /* CS2102 */
6319 6181
6320 start_2wr_probe(dev, 0x06); /* OmniVision */ 6182 start_2wr_probe(gspca_dev, 0x06); /* OmniVision */
6321 reg_w(dev, 0x08, 0x008d); 6183 reg_w(gspca_dev, 0x08, 0x008d);
6322 i2c_write(gspca_dev, 0x11, 0xaa, 0x00); 6184 i2c_write(gspca_dev, 0x11, 0xaa, 0x00);
6323 retword = i2c_read(gspca_dev, 0x11); 6185 retword = i2c_read(gspca_dev, 0x11);
6324 if (retword != 0) { 6186 if (retword != 0) {
@@ -6327,14 +6189,14 @@ static int vga_2wr_probe(struct gspca_dev *gspca_dev)
6327 goto ov_check; 6189 goto ov_check;
6328 } 6190 }
6329 6191
6330 start_2wr_probe(dev, 0x08); /* HDCS2020 */ 6192 start_2wr_probe(gspca_dev, 0x08); /* HDCS2020 */
6331 i2c_write(gspca_dev, 0x1c, 0x00, 0x00); 6193 i2c_write(gspca_dev, 0x1c, 0x00, 0x00);
6332 i2c_write(gspca_dev, 0x15, 0xaa, 0x00); 6194 i2c_write(gspca_dev, 0x15, 0xaa, 0x00);
6333 retword = i2c_read(gspca_dev, 0x15); 6195 retword = i2c_read(gspca_dev, 0x15);
6334 if (retword != 0) 6196 if (retword != 0)
6335 return 0x08; /* HDCS2020 */ 6197 return 0x08; /* HDCS2020 */
6336 6198
6337 start_2wr_probe(dev, 0x0a); /* PB0330 */ 6199 start_2wr_probe(gspca_dev, 0x0a); /* PB0330 */
6338 i2c_write(gspca_dev, 0x07, 0xaa, 0xaa); 6200 i2c_write(gspca_dev, 0x07, 0xaa, 0xaa);
6339 retword = i2c_read(gspca_dev, 0x07); 6201 retword = i2c_read(gspca_dev, 0x07);
6340 if (retword != 0) 6202 if (retword != 0)
@@ -6346,23 +6208,23 @@ static int vga_2wr_probe(struct gspca_dev *gspca_dev)
6346 if (retword != 0) 6208 if (retword != 0)
6347 return 0x0a; /* PB0330 ?? */ 6209 return 0x0a; /* PB0330 ?? */
6348 6210
6349 start_2wr_probe(dev, 0x0c); /* ICM105A */ 6211 start_2wr_probe(gspca_dev, 0x0c); /* ICM105A */
6350 i2c_write(gspca_dev, 0x01, 0x11, 0x00); 6212 i2c_write(gspca_dev, 0x01, 0x11, 0x00);
6351 retword = i2c_read(gspca_dev, 0x01); 6213 retword = i2c_read(gspca_dev, 0x01);
6352 if (retword != 0) 6214 if (retword != 0)
6353 return 0x0c; /* ICM105A */ 6215 return 0x0c; /* ICM105A */
6354 6216
6355 start_2wr_probe(dev, 0x0e); /* PAS202BCB */ 6217 start_2wr_probe(gspca_dev, 0x0e); /* PAS202BCB */
6356 reg_w(dev, 0x08, 0x008d); 6218 reg_w(gspca_dev, 0x08, 0x008d);
6357 i2c_write(gspca_dev, 0x03, 0xaa, 0x00); 6219 i2c_write(gspca_dev, 0x03, 0xaa, 0x00);
6358 msleep(50); 6220 msleep(50);
6359 retword = i2c_read(gspca_dev, 0x03); 6221 retword = i2c_read(gspca_dev, 0x03);
6360 if (retword != 0) { 6222 if (retword != 0) {
6361 send_unknown(dev, SENSOR_PAS202B); 6223 send_unknown(gspca_dev, SENSOR_PAS202B);
6362 return 0x0e; /* PAS202BCB */ 6224 return 0x0e; /* PAS202BCB */
6363 } 6225 }
6364 6226
6365 start_2wr_probe(dev, 0x02); /* TAS5130C */ 6227 start_2wr_probe(gspca_dev, 0x02); /* TAS5130C */
6366 i2c_write(gspca_dev, 0x01, 0xaa, 0x00); 6228 i2c_write(gspca_dev, 0x01, 0xaa, 0x00);
6367 retword = i2c_read(gspca_dev, 0x01); 6229 retword = i2c_read(gspca_dev, 0x01);
6368 if (retword != 0) 6230 if (retword != 0)
@@ -6371,20 +6233,20 @@ ov_check:
6371 reg_r(gspca_dev, 0x0010); /* ?? */ 6233 reg_r(gspca_dev, 0x0010); /* ?? */
6372 reg_r(gspca_dev, 0x0010); 6234 reg_r(gspca_dev, 0x0010);
6373 6235
6374 reg_w(dev, 0x01, 0x0000); 6236 reg_w(gspca_dev, 0x01, 0x0000);
6375 reg_w(dev, 0x01, 0x0001); 6237 reg_w(gspca_dev, 0x01, 0x0001);
6376 reg_w(dev, 0x06, 0x0010); /* OmniVision */ 6238 reg_w(gspca_dev, 0x06, 0x0010); /* OmniVision */
6377 reg_w(dev, 0xa1, 0x008b); 6239 reg_w(gspca_dev, 0xa1, 0x008b);
6378 reg_w(dev, 0x08, 0x008d); 6240 reg_w(gspca_dev, 0x08, 0x008d);
6379 msleep(500); 6241 msleep(500);
6380 reg_w(dev, 0x01, 0x0012); 6242 reg_w(gspca_dev, 0x01, 0x0012);
6381 i2c_write(gspca_dev, 0x12, 0x80, 0x00); /* sensor reset */ 6243 i2c_write(gspca_dev, 0x12, 0x80, 0x00); /* sensor reset */
6382 retword = i2c_read(gspca_dev, 0x0a) << 8; 6244 retword = i2c_read(gspca_dev, 0x0a) << 8;
6383 retword |= i2c_read(gspca_dev, 0x0b); 6245 retword |= i2c_read(gspca_dev, 0x0b);
6384 PDEBUG(D_PROBE, "probe 2wr ov vga 0x%04x", retword); 6246 PDEBUG(D_PROBE, "probe 2wr ov vga 0x%04x", retword);
6385 switch (retword) { 6247 switch (retword) {
6386 case 0x7631: /* OV7630C */ 6248 case 0x7631: /* OV7630C */
6387 reg_w(dev, 0x06, 0x0010); 6249 reg_w(gspca_dev, 0x06, 0x0010);
6388 break; 6250 break;
6389 case 0x7620: /* OV7620 */ 6251 case 0x7620: /* OV7620 */
6390 case 0x7648: /* OV7648 */ 6252 case 0x7648: /* OV7648 */
@@ -6401,32 +6263,31 @@ struct sensor_by_chipset_revision {
6401}; 6263};
6402static const struct sensor_by_chipset_revision chipset_revision_sensor[] = { 6264static const struct sensor_by_chipset_revision chipset_revision_sensor[] = {
6403 {0xc000, 0x12}, /* TAS5130C */ 6265 {0xc000, 0x12}, /* TAS5130C */
6404 {0xc001, 0x13}, /* MI0360SOC */ 6266 {0xc001, 0x13}, /* MT9V111 */
6405 {0xe001, 0x13}, 6267 {0xe001, 0x13},
6406 {0x8001, 0x13}, 6268 {0x8001, 0x13},
6407 {0x8000, 0x14}, /* CS2102K */ 6269 {0x8000, 0x14}, /* CS2102K */
6408 {0x8400, 0x15}, /* TAS5130K */ 6270 {0x8400, 0x15}, /* MT9V111 */
6409 {0xe400, 0x15}, 6271 {0xe400, 0x15},
6410}; 6272};
6411 6273
6412static int vga_3wr_probe(struct gspca_dev *gspca_dev) 6274static int vga_3wr_probe(struct gspca_dev *gspca_dev)
6413{ 6275{
6414 struct sd *sd = (struct sd *) gspca_dev; 6276 struct sd *sd = (struct sd *) gspca_dev;
6415 struct usb_device *dev = gspca_dev->dev;
6416 int i; 6277 int i;
6417 u8 retbyte; 6278 u8 retbyte;
6418 u16 retword; 6279 u16 retword;
6419 6280
6420/*fixme: lack of 8b=b3 (11,12)-> 10, 8b=e0 (14,15,16)-> 12 found in gspcav1*/ 6281/*fixme: lack of 8b=b3 (11,12)-> 10, 8b=e0 (14,15,16)-> 12 found in gspcav1*/
6421 reg_w(dev, 0x02, 0x0010); 6282 reg_w(gspca_dev, 0x02, 0x0010);
6422 reg_r(gspca_dev, 0x0010); 6283 reg_r(gspca_dev, 0x0010);
6423 reg_w(dev, 0x01, 0x0000); 6284 reg_w(gspca_dev, 0x01, 0x0000);
6424 reg_w(dev, 0x00, 0x0010); 6285 reg_w(gspca_dev, 0x00, 0x0010);
6425 reg_w(dev, 0x01, 0x0001); 6286 reg_w(gspca_dev, 0x01, 0x0001);
6426 reg_w(dev, 0x91, 0x008b); 6287 reg_w(gspca_dev, 0x91, 0x008b);
6427 reg_w(dev, 0x03, 0x0012); 6288 reg_w(gspca_dev, 0x03, 0x0012);
6428 reg_w(dev, 0x01, 0x0012); 6289 reg_w(gspca_dev, 0x01, 0x0012);
6429 reg_w(dev, 0x05, 0x0012); 6290 reg_w(gspca_dev, 0x05, 0x0012);
6430 retword = i2c_read(gspca_dev, 0x14); 6291 retword = i2c_read(gspca_dev, 0x14);
6431 if (retword != 0) 6292 if (retword != 0)
6432 return 0x11; /* HV7131R */ 6293 return 0x11; /* HV7131R */
@@ -6437,93 +6298,90 @@ static int vga_3wr_probe(struct gspca_dev *gspca_dev)
6437 if (retword != 0) 6298 if (retword != 0)
6438 return 0x11; /* HV7131R */ 6299 return 0x11; /* HV7131R */
6439 6300
6440 reg_w(dev, 0x02, 0x0010); 6301 reg_w(gspca_dev, 0x02, 0x0010);
6441 retword = reg_r(gspca_dev, 0x000b) << 8; 6302 retword = reg_r(gspca_dev, 0x000b) << 8;
6442 retword |= reg_r(gspca_dev, 0x000a); 6303 retword |= reg_r(gspca_dev, 0x000a);
6443 PDEBUG(D_PROBE, "probe 3wr vga 1 0x%04x", retword); 6304 PDEBUG(D_PROBE, "probe 3wr vga 1 0x%04x", retword);
6444 reg_r(gspca_dev, 0x0010); 6305 reg_r(gspca_dev, 0x0010);
6445 /* value 0x4001 is meaningless */ 6306 if ((retword & 0xff00) == 0x6400)
6446 if (retword != 0x4001) { 6307 return 0x02; /* TAS5130C */
6447 if ((retword & 0xff00) == 0x6400) 6308 for (i = 0; i < ARRAY_SIZE(chipset_revision_sensor); i++) {
6448 return 0x02; /* TAS5130C */ 6309 if (chipset_revision_sensor[i].revision == retword) {
6449 for (i = 0; i < ARRAY_SIZE(chipset_revision_sensor); i++) { 6310 sd->chip_revision = retword;
6450 if (chipset_revision_sensor[i].revision == retword) { 6311 send_unknown(gspca_dev, SENSOR_PB0330);
6451 sd->chip_revision = retword; 6312 return chipset_revision_sensor[i]
6452 send_unknown(dev, SENSOR_PB0330); 6313 .internal_sensor_id;
6453 return chipset_revision_sensor[i]
6454 .internal_sensor_id;
6455 }
6456 } 6314 }
6457 } 6315 }
6458 6316
6459 reg_w(dev, 0x01, 0x0000); /* check PB0330 */ 6317 reg_w(gspca_dev, 0x01, 0x0000); /* check PB0330 */
6460 reg_w(dev, 0x01, 0x0001); 6318 reg_w(gspca_dev, 0x01, 0x0001);
6461 reg_w(dev, 0xdd, 0x008b); 6319 reg_w(gspca_dev, 0xdd, 0x008b);
6462 reg_w(dev, 0x0a, 0x0010); 6320 reg_w(gspca_dev, 0x0a, 0x0010);
6463 reg_w(dev, 0x03, 0x0012); 6321 reg_w(gspca_dev, 0x03, 0x0012);
6464 reg_w(dev, 0x01, 0x0012); 6322 reg_w(gspca_dev, 0x01, 0x0012);
6465 retword = i2c_read(gspca_dev, 0x00); 6323 retword = i2c_read(gspca_dev, 0x00);
6466 if (retword != 0) { 6324 if (retword != 0) {
6467 PDEBUG(D_PROBE, "probe 3wr vga type 0a ?"); 6325 PDEBUG(D_PROBE, "probe 3wr vga type 0a");
6468 return 0x0a; /* PB0330 */ 6326 return 0x0a; /* PB0330 */
6469 } 6327 }
6470 6328
6471 reg_w(dev, 0x01, 0x0000); 6329 reg_w(gspca_dev, 0x01, 0x0000);
6472 reg_w(dev, 0x01, 0x0001); 6330 reg_w(gspca_dev, 0x01, 0x0001);
6473 reg_w(dev, 0x98, 0x008b); 6331 reg_w(gspca_dev, 0x98, 0x008b);
6474 reg_w(dev, 0x01, 0x0010); 6332 reg_w(gspca_dev, 0x01, 0x0010);
6475 reg_w(dev, 0x03, 0x0012); 6333 reg_w(gspca_dev, 0x03, 0x0012);
6476 msleep(2); 6334 msleep(2);
6477 reg_w(dev, 0x01, 0x0012); 6335 reg_w(gspca_dev, 0x01, 0x0012);
6478 retword = i2c_read(gspca_dev, 0x00); 6336 retword = i2c_read(gspca_dev, 0x00);
6479 if (retword != 0) { 6337 if (retword != 0) {
6480 PDEBUG(D_PROBE, "probe 3wr vga type %02x", retword); 6338 PDEBUG(D_PROBE, "probe 3wr vga type %02x", retword);
6481 if (retword == 0x0011) /* VF0250 */ 6339 if (retword == 0x0011) /* VF0250 */
6482 return 0x0250; 6340 return 0x0250;
6483 if (retword == 0x0029) /* gc0305 */ 6341 if (retword == 0x0029) /* gc0305 */
6484 send_unknown(dev, SENSOR_GC0305); 6342 send_unknown(gspca_dev, SENSOR_GC0305);
6485 return retword; 6343 return retword;
6486 } 6344 }
6487 6345
6488 reg_w(dev, 0x01, 0x0000); /* check OmniVision */ 6346 reg_w(gspca_dev, 0x01, 0x0000); /* check OmniVision */
6489 reg_w(dev, 0x01, 0x0001); 6347 reg_w(gspca_dev, 0x01, 0x0001);
6490 reg_w(dev, 0xa1, 0x008b); 6348 reg_w(gspca_dev, 0xa1, 0x008b);
6491 reg_w(dev, 0x08, 0x008d); 6349 reg_w(gspca_dev, 0x08, 0x008d);
6492 reg_w(dev, 0x06, 0x0010); 6350 reg_w(gspca_dev, 0x06, 0x0010);
6493 reg_w(dev, 0x01, 0x0012); 6351 reg_w(gspca_dev, 0x01, 0x0012);
6494 reg_w(dev, 0x05, 0x0012); 6352 reg_w(gspca_dev, 0x05, 0x0012);
6495 if (i2c_read(gspca_dev, 0x1c) == 0x007f /* OV7610 - manufacturer ID */ 6353 if (i2c_read(gspca_dev, 0x1c) == 0x007f /* OV7610 - manufacturer ID */
6496 && i2c_read(gspca_dev, 0x1d) == 0x00a2) { 6354 && i2c_read(gspca_dev, 0x1d) == 0x00a2) {
6497 send_unknown(dev, SENSOR_OV7620); 6355 send_unknown(gspca_dev, SENSOR_OV7620);
6498 return 0x06; /* OmniVision confirm ? */ 6356 return 0x06; /* OmniVision confirm ? */
6499 } 6357 }
6500 6358
6501 reg_w(dev, 0x01, 0x0000); 6359 reg_w(gspca_dev, 0x01, 0x0000);
6502 reg_w(dev, 0x00, 0x0002); 6360 reg_w(gspca_dev, 0x00, 0x0002);
6503 reg_w(dev, 0x01, 0x0010); 6361 reg_w(gspca_dev, 0x01, 0x0010);
6504 reg_w(dev, 0x01, 0x0001); 6362 reg_w(gspca_dev, 0x01, 0x0001);
6505 reg_w(dev, 0xee, 0x008b); 6363 reg_w(gspca_dev, 0xee, 0x008b);
6506 reg_w(dev, 0x03, 0x0012); 6364 reg_w(gspca_dev, 0x03, 0x0012);
6507 reg_w(dev, 0x01, 0x0012); 6365 reg_w(gspca_dev, 0x01, 0x0012);
6508 reg_w(dev, 0x05, 0x0012); 6366 reg_w(gspca_dev, 0x05, 0x0012);
6509 retword = i2c_read(gspca_dev, 0x00) << 8; /* ID 0 */ 6367 retword = i2c_read(gspca_dev, 0x00) << 8; /* ID 0 */
6510 retword |= i2c_read(gspca_dev, 0x01); /* ID 1 */ 6368 retword |= i2c_read(gspca_dev, 0x01); /* ID 1 */
6511 PDEBUG(D_PROBE, "probe 3wr vga 2 0x%04x", retword); 6369 PDEBUG(D_PROBE, "probe 3wr vga 2 0x%04x", retword);
6512 if (retword == 0x2030) { 6370 if (retword == 0x2030) {
6513 retbyte = i2c_read(gspca_dev, 0x02); /* revision number */ 6371 retbyte = i2c_read(gspca_dev, 0x02); /* revision number */
6514 PDEBUG(D_PROBE, "sensor PO2030 rev 0x%02x", retbyte); 6372 PDEBUG(D_PROBE, "sensor PO2030 rev 0x%02x", retbyte);
6515 send_unknown(dev, SENSOR_PO2030); 6373 send_unknown(gspca_dev, SENSOR_PO2030);
6516 return retword; 6374 return retword;
6517 } 6375 }
6518 6376
6519 reg_w(dev, 0x01, 0x0000); 6377 reg_w(gspca_dev, 0x01, 0x0000);
6520 reg_w(dev, 0x0a, 0x0010); 6378 reg_w(gspca_dev, 0x0a, 0x0010);
6521 reg_w(dev, 0xd3, 0x008b); 6379 reg_w(gspca_dev, 0xd3, 0x008b);
6522 reg_w(dev, 0x01, 0x0001); 6380 reg_w(gspca_dev, 0x01, 0x0001);
6523 reg_w(dev, 0x03, 0x0012); 6381 reg_w(gspca_dev, 0x03, 0x0012);
6524 reg_w(dev, 0x01, 0x0012); 6382 reg_w(gspca_dev, 0x01, 0x0012);
6525 reg_w(dev, 0x05, 0x0012); 6383 reg_w(gspca_dev, 0x05, 0x0012);
6526 reg_w(dev, 0xd3, 0x008b); 6384 reg_w(gspca_dev, 0xd3, 0x008b);
6527 retword = i2c_read(gspca_dev, 0x01); 6385 retword = i2c_read(gspca_dev, 0x01);
6528 if (retword != 0) { 6386 if (retword != 0) {
6529 PDEBUG(D_PROBE, "probe 3wr vga type 0a ? ret: %04x", retword); 6387 PDEBUG(D_PROBE, "probe 3wr vga type 0a ? ret: %04x", retword);
@@ -6560,54 +6418,74 @@ static int sd_config(struct gspca_dev *gspca_dev,
6560 const struct usb_device_id *id) 6418 const struct usb_device_id *id)
6561{ 6419{
6562 struct sd *sd = (struct sd *) gspca_dev; 6420 struct sd *sd = (struct sd *) gspca_dev;
6421
6422 if (id->idProduct == 0x301b)
6423 sd->bridge = BRIDGE_ZC301;
6424 else
6425 sd->bridge = BRIDGE_ZC303;
6426
6427 /* define some sensors from the vendor/product */
6428 sd->sensor = id->driver_info;
6429
6430 sd->sharpness = SHARPNESS_DEF;
6431 sd->brightness = BRIGHTNESS_DEF;
6432 sd->contrast = CONTRAST_DEF;
6433 sd->autogain = AUTOGAIN_DEF;
6434 sd->lightfreq = FREQ_DEF;
6435 sd->quality = QUALITY_DEF;
6436
6437 return 0;
6438}
6439
6440/* this function is called at probe and resume time */
6441static int sd_init(struct gspca_dev *gspca_dev)
6442{
6443 struct sd *sd = (struct sd *) gspca_dev;
6563 struct cam *cam; 6444 struct cam *cam;
6564 int sensor; 6445 int sensor;
6565 static const u8 gamma[SENSOR_MAX] = { 6446 static const u8 gamma[SENSOR_MAX] = {
6566 4, /* SENSOR_ADCM2700 0 */ 6447 [SENSOR_ADCM2700] = 4,
6567 4, /* SENSOR_CS2102 1 */ 6448 [SENSOR_CS2102] = 4,
6568 5, /* SENSOR_CS2102K 2 */ 6449 [SENSOR_CS2102K] = 5,
6569 4, /* SENSOR_GC0305 3 */ 6450 [SENSOR_GC0305] = 4,
6570 4, /* SENSOR_HDCS2020b 4 */ 6451 [SENSOR_HDCS2020b] = 4,
6571 4, /* SENSOR_HV7131B 5 */ 6452 [SENSOR_HV7131B] = 4,
6572 4, /* SENSOR_HV7131C 6 */ 6453 [SENSOR_HV7131R] = 4,
6573 4, /* SENSOR_ICM105A 7 */ 6454 [SENSOR_ICM105A] = 4,
6574 4, /* SENSOR_MC501CB 8 */ 6455 [SENSOR_MC501CB] = 4,
6575 4, /* SENSOR_MI0360SOC 9 */ 6456 [SENSOR_MT9V111_1] = 4,
6576 3, /* SENSOR_OV7620 10 */ 6457 [SENSOR_MT9V111_3] = 4,
6577 4, /* SENSOR_OV7630C 11 */ 6458 [SENSOR_OV7620] = 3,
6578 4, /* SENSOR_PAS106 12 */ 6459 [SENSOR_OV7630C] = 4,
6579 4, /* SENSOR_PAS202B 13 */ 6460 [SENSOR_PAS106] = 4,
6580 4, /* SENSOR_PB0330 14 */ 6461 [SENSOR_PAS202B] = 4,
6581 4, /* SENSOR_PO2030 15 */ 6462 [SENSOR_PB0330] = 4,
6582 4, /* SENSOR_TAS5130CK 16 */ 6463 [SENSOR_PO2030] = 4,
6583 3, /* SENSOR_TAS5130CXX 17 */ 6464 [SENSOR_TAS5130C] = 3,
6584 3, /* SENSOR_TAS5130C_VF0250 18 */ 6465 [SENSOR_TAS5130C_VF0250] = 3,
6585 }; 6466 };
6586 static const u8 mode_tb[SENSOR_MAX] = { 6467 static const u8 mode_tb[SENSOR_MAX] = {
6587 2, /* SENSOR_ADCM2700 0 */ 6468 [SENSOR_ADCM2700] = 2,
6588 1, /* SENSOR_CS2102 1 */ 6469 [SENSOR_CS2102] = 1,
6589 1, /* SENSOR_CS2102K 2 */ 6470 [SENSOR_CS2102K] = 1,
6590 1, /* SENSOR_GC0305 3 */ 6471 [SENSOR_GC0305] = 1,
6591 1, /* SENSOR_HDCS2020b 4 */ 6472 [SENSOR_HDCS2020b] = 1,
6592 1, /* SENSOR_HV7131B 5 */ 6473 [SENSOR_HV7131B] = 1,
6593 1, /* SENSOR_HV7131C 6 */ 6474 [SENSOR_HV7131R] = 1,
6594 1, /* SENSOR_ICM105A 7 */ 6475 [SENSOR_ICM105A] = 1,
6595 2, /* SENSOR_MC501CB 8 */ 6476 [SENSOR_MC501CB] = 2,
6596 1, /* SENSOR_MI0360SOC 9 */ 6477 [SENSOR_MT9V111_1] = 1,
6597 2, /* SENSOR_OV7620 10 */ 6478 [SENSOR_MT9V111_3] = 1,
6598 1, /* SENSOR_OV7630C 11 */ 6479 [SENSOR_OV7620] = 2,
6599 0, /* SENSOR_PAS106 12 */ 6480 [SENSOR_OV7630C] = 1,
6600 1, /* SENSOR_PAS202B 13 */ 6481 [SENSOR_PAS106] = 0,
6601 1, /* SENSOR_PB0330 14 */ 6482 [SENSOR_PAS202B] = 1,
6602 1, /* SENSOR_PO2030 15 */ 6483 [SENSOR_PB0330] = 1,
6603 1, /* SENSOR_TAS5130CK 16 */ 6484 [SENSOR_PO2030] = 1,
6604 1, /* SENSOR_TAS5130CXX 17 */ 6485 [SENSOR_TAS5130C] = 1,
6605 1, /* SENSOR_TAS5130C_VF0250 18 */ 6486 [SENSOR_TAS5130C_VF0250] = 1,
6606 }; 6487 };
6607 6488
6608 /* define some sensors from the vendor/product */
6609 sd->sharpness = SHARPNESS_DEF;
6610 sd->sensor = id->driver_info;
6611 sensor = zcxx_probeSensor(gspca_dev); 6489 sensor = zcxx_probeSensor(gspca_dev);
6612 if (sensor >= 0) 6490 if (sensor >= 0)
6613 PDEBUG(D_PROBE, "probe sensor -> %04x", sensor); 6491 PDEBUG(D_PROBE, "probe sensor -> %04x", sensor);
@@ -6626,8 +6504,8 @@ static int sd_config(struct gspca_dev *gspca_dev,
6626 break; 6504 break;
6627 default: 6505 default:
6628 PDEBUG(D_PROBE, 6506 PDEBUG(D_PROBE,
6629 "Sensor UNKNOWN_0 force Tas5130"); 6507 "Unknown sensor - set to TAS5130C");
6630 sd->sensor = SENSOR_TAS5130CXX; 6508 sd->sensor = SENSOR_TAS5130C;
6631 } 6509 }
6632 break; 6510 break;
6633 case 0: 6511 case 0:
@@ -6642,14 +6520,14 @@ static int sd_config(struct gspca_dev *gspca_dev,
6642 break; 6520 break;
6643 default: 6521 default:
6644/* case 2: * hv7131r */ 6522/* case 2: * hv7131r */
6645 PDEBUG(D_PROBE, "Find Sensor HV7131R(c)"); 6523 PDEBUG(D_PROBE, "Find Sensor HV7131R");
6646 sd->sensor = SENSOR_HV7131C; 6524 sd->sensor = SENSOR_HV7131R;
6647 break; 6525 break;
6648 } 6526 }
6649 break; 6527 break;
6650 case 0x02: 6528 case 0x02:
6651 PDEBUG(D_PROBE, "Sensor TAS5130C"); 6529 PDEBUG(D_PROBE, "Sensor TAS5130C");
6652 sd->sensor = SENSOR_TAS5130CXX; 6530 sd->sensor = SENSOR_TAS5130C;
6653 break; 6531 break;
6654 case 0x04: 6532 case 0x04:
6655 PDEBUG(D_PROBE, "Find Sensor CS2102"); 6533 PDEBUG(D_PROBE, "Find Sensor CS2102");
@@ -6681,17 +6559,20 @@ static int sd_config(struct gspca_dev *gspca_dev,
6681 case 0x10: 6559 case 0x10:
6682 case 0x12: 6560 case 0x12:
6683 PDEBUG(D_PROBE, "Find Sensor TAS5130C"); 6561 PDEBUG(D_PROBE, "Find Sensor TAS5130C");
6684 sd->sensor = SENSOR_TAS5130CXX; 6562 sd->sensor = SENSOR_TAS5130C;
6685 break; 6563 break;
6686 case 0x11: 6564 case 0x11:
6687 PDEBUG(D_PROBE, "Find Sensor HV7131R(c)"); 6565 PDEBUG(D_PROBE, "Find Sensor HV7131R");
6688 sd->sensor = SENSOR_HV7131C; 6566 sd->sensor = SENSOR_HV7131R;
6689 break; 6567 break;
6690 case 0x13: 6568 case 0x13:
6569 case 0x15:
6691 PDEBUG(D_PROBE, 6570 PDEBUG(D_PROBE,
6692 "Find Sensor MI0360SOC. Chip revision %x", 6571 "Sensor MT9V111. Chip revision %04x",
6693 sd->chip_revision); 6572 sd->chip_revision);
6694 sd->sensor = SENSOR_MI0360SOC; 6573 sd->sensor = sd->bridge == BRIDGE_ZC301
6574 ? SENSOR_MT9V111_1
6575 : SENSOR_MT9V111_3;
6695 break; 6576 break;
6696 case 0x14: 6577 case 0x14:
6697 PDEBUG(D_PROBE, 6578 PDEBUG(D_PROBE,
@@ -6699,12 +6580,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
6699 sd->chip_revision); 6580 sd->chip_revision);
6700 sd->sensor = SENSOR_CS2102K; 6581 sd->sensor = SENSOR_CS2102K;
6701 break; 6582 break;
6702 case 0x15:
6703 PDEBUG(D_PROBE,
6704 "Find Sensor TAS5130CK?. Chip revision %x",
6705 sd->chip_revision);
6706 sd->sensor = SENSOR_TAS5130CK;
6707 break;
6708 case 0x16: 6583 case 0x16:
6709 PDEBUG(D_PROBE, "Find Sensor ADCM2700"); 6584 PDEBUG(D_PROBE, "Find Sensor ADCM2700");
6710 sd->sensor = SENSOR_ADCM2700; 6585 sd->sensor = SENSOR_ADCM2700;
@@ -6741,13 +6616,11 @@ static int sd_config(struct gspca_dev *gspca_dev,
6741 } 6616 }
6742 if (sensor < 0x20) { 6617 if (sensor < 0x20) {
6743 if (sensor == -1 || sensor == 0x10 || sensor == 0x12) 6618 if (sensor == -1 || sensor == 0x10 || sensor == 0x12)
6744 reg_w(gspca_dev->dev, 0x02, 0x0010); 6619 reg_w(gspca_dev, 0x02, 0x0010);
6745 reg_r(gspca_dev, 0x0010); 6620 reg_r(gspca_dev, 0x0010);
6746 } 6621 }
6747 6622
6748 cam = &gspca_dev->cam; 6623 cam = &gspca_dev->cam;
6749/*fixme:test*/
6750 gspca_dev->nbalt--;
6751 switch (mode_tb[sd->sensor]) { 6624 switch (mode_tb[sd->sensor]) {
6752 case 0: 6625 case 0:
6753 cam->cam_mode = sif_mode; 6626 cam->cam_mode = sif_mode;
@@ -6763,58 +6636,62 @@ static int sd_config(struct gspca_dev *gspca_dev,
6763 cam->nmodes = ARRAY_SIZE(broken_vga_mode); 6636 cam->nmodes = ARRAY_SIZE(broken_vga_mode);
6764 break; 6637 break;
6765 } 6638 }
6766 sd->brightness = BRIGHTNESS_DEF;
6767 sd->contrast = CONTRAST_DEF;
6768 sd->gamma = gamma[sd->sensor]; 6639 sd->gamma = gamma[sd->sensor];
6769 sd->autogain = AUTOGAIN_DEF;
6770 sd->lightfreq = FREQ_DEF;
6771 sd->quality = QUALITY_DEF;
6772 6640
6773 switch (sd->sensor) { 6641 switch (sd->sensor) {
6774 case SENSOR_HV7131B:
6775 case SENSOR_HV7131C:
6776 case SENSOR_OV7630C: 6642 case SENSOR_OV7630C:
6777 gspca_dev->ctrl_dis = (1 << LIGHTFREQ_IDX); 6643 gspca_dev->ctrl_dis = (1 << LIGHTFREQ_IDX);
6778 break; 6644 break;
6779 } 6645 }
6780 6646
6781 return 0;
6782}
6783
6784/* this function is called at probe and resume time */
6785static int sd_init(struct gspca_dev *gspca_dev)
6786{
6787 /* switch off the led */ 6647 /* switch off the led */
6788 reg_w(gspca_dev->dev, 0x01, 0x0000); 6648 reg_w(gspca_dev, 0x01, 0x0000);
6789 return 0; 6649 return gspca_dev->usb_err;
6790} 6650}
6791 6651
6792static int sd_start(struct gspca_dev *gspca_dev) 6652static int sd_start(struct gspca_dev *gspca_dev)
6793{ 6653{
6794 struct sd *sd = (struct sd *) gspca_dev; 6654 struct sd *sd = (struct sd *) gspca_dev;
6795 struct usb_device *dev = gspca_dev->dev;
6796 int mode; 6655 int mode;
6797 static const struct usb_action *init_tb[SENSOR_MAX][2] = { 6656 static const struct usb_action *init_tb[SENSOR_MAX][2] = {
6798 {adcm2700_Initial, adcm2700_InitialScale}, /* 0 */ 6657 [SENSOR_ADCM2700] =
6799 {cs2102_Initial, cs2102_InitialScale}, /* 1 */ 6658 {adcm2700_Initial, adcm2700_InitialScale},
6800 {cs2102K_Initial, cs2102K_InitialScale}, /* 2 */ 6659 [SENSOR_CS2102] =
6801 {gc0305_Initial, gc0305_InitialScale}, /* 3 */ 6660 {cs2102_Initial, cs2102_InitialScale},
6802 {hdcs2020b_Initial, hdcs2020b_InitialScale}, /* 4 */ 6661 [SENSOR_CS2102K] =
6803 {hv7131b_Initial, hv7131b_InitialScale}, /* 5 */ 6662 {cs2102K_Initial, cs2102K_InitialScale},
6804 {hv7131r_Initial, hv7131r_InitialScale}, /* 6 */ 6663 [SENSOR_GC0305] =
6805 {icm105a_Initial, icm105a_InitialScale}, /* 7 */ 6664 {gc0305_Initial, gc0305_InitialScale},
6806 {mc501cb_Initial, mc501cb_InitialScale}, /* 8 */ 6665 [SENSOR_HDCS2020b] =
6807 {mi0360soc_Initial, mi0360soc_InitialScale}, /* 9 */ 6666 {hdcs2020b_Initial, hdcs2020b_InitialScale},
6808 {ov7620_Initial, ov7620_InitialScale}, /* 10 */ 6667 [SENSOR_HV7131B] =
6809 {ov7630c_Initial, ov7630c_InitialScale}, /* 11 */ 6668 {hv7131b_Initial, hv7131b_InitialScale},
6810 {pas106b_Initial, pas106b_InitialScale}, /* 12 */ 6669 [SENSOR_HV7131R] =
6811 {pas202b_Initial, pas202b_InitialScale}, /* 13 */ 6670 {hv7131r_Initial, hv7131r_InitialScale},
6812 {pb0330_Initial, pb0330_InitialScale}, /* 14 */ 6671 [SENSOR_ICM105A] =
6813 {po2030_Initial, po2030_InitialScale}, /* 15 */ 6672 {icm105a_Initial, icm105a_InitialScale},
6814 {tas5130cK_Initial, tas5130cK_InitialScale}, /* 16 */ 6673 [SENSOR_MC501CB] =
6815 {tas5130cxx_Initial, tas5130cxx_InitialScale}, /* 17 */ 6674 {mc501cb_Initial, mc501cb_InitialScale},
6675 [SENSOR_MT9V111_1] =
6676 {mt9v111_1_Initial, mt9v111_1_InitialScale},
6677 [SENSOR_MT9V111_3] =
6678 {mt9v111_3_Initial, mt9v111_3_InitialScale},
6679 [SENSOR_OV7620] =
6680 {ov7620_Initial, ov7620_InitialScale},
6681 [SENSOR_OV7630C] =
6682 {ov7630c_Initial, ov7630c_InitialScale},
6683 [SENSOR_PAS106] =
6684 {pas106b_Initial, pas106b_InitialScale},
6685 [SENSOR_PAS202B] =
6686 {pas202b_Initial, pas202b_InitialScale},
6687 [SENSOR_PB0330] =
6688 {pb0330_Initial, pb0330_InitialScale},
6689 [SENSOR_PO2030] =
6690 {po2030_Initial, po2030_InitialScale},
6691 [SENSOR_TAS5130C] =
6692 {tas5130c_Initial, tas5130c_InitialScale},
6693 [SENSOR_TAS5130C_VF0250] =
6816 {tas5130c_vf0250_Initial, tas5130c_vf0250_InitialScale}, 6694 {tas5130c_vf0250_Initial, tas5130c_vf0250_InitialScale},
6817 /* 18 */
6818 }; 6695 };
6819 6696
6820 /* create the JPEG header */ 6697 /* create the JPEG header */
@@ -6824,7 +6701,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
6824 6701
6825 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv; 6702 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
6826 switch (sd->sensor) { 6703 switch (sd->sensor) {
6827 case SENSOR_HV7131C: 6704 case SENSOR_HV7131R:
6828 zcxx_probeSensor(gspca_dev); 6705 zcxx_probeSensor(gspca_dev);
6829 break; 6706 break;
6830 case SENSOR_PAS106: 6707 case SENSOR_PAS106:
@@ -6838,22 +6715,22 @@ static int sd_start(struct gspca_dev *gspca_dev)
6838 case SENSOR_GC0305: 6715 case SENSOR_GC0305:
6839 case SENSOR_OV7620: 6716 case SENSOR_OV7620:
6840 case SENSOR_PO2030: 6717 case SENSOR_PO2030:
6841 case SENSOR_TAS5130CXX: 6718 case SENSOR_TAS5130C:
6842 case SENSOR_TAS5130C_VF0250: 6719 case SENSOR_TAS5130C_VF0250:
6843/* msleep(100); * ?? */ 6720/* msleep(100); * ?? */
6844 reg_r(gspca_dev, 0x0002); /* --> 0x40 */ 6721 reg_r(gspca_dev, 0x0002); /* --> 0x40 */
6845 reg_w(dev, 0x09, 0x01ad); /* (from win traces) */ 6722 reg_w(gspca_dev, 0x09, 0x01ad); /* (from win traces) */
6846 reg_w(dev, 0x15, 0x01ae); 6723 reg_w(gspca_dev, 0x15, 0x01ae);
6847 if (sd->sensor == SENSOR_TAS5130CXX) 6724 if (sd->sensor == SENSOR_TAS5130C)
6848 break; 6725 break;
6849 reg_w(dev, 0x0d, 0x003a); 6726 reg_w(gspca_dev, 0x0d, 0x003a);
6850 reg_w(dev, 0x02, 0x003b); 6727 reg_w(gspca_dev, 0x02, 0x003b);
6851 reg_w(dev, 0x00, 0x0038); 6728 reg_w(gspca_dev, 0x00, 0x0038);
6852 break; 6729 break;
6853 case SENSOR_PAS202B: 6730 case SENSOR_PAS202B:
6854 reg_w(dev, 0x03, 0x003b); 6731 reg_w(gspca_dev, 0x03, 0x003b);
6855 reg_w(dev, 0x0c, 0x003a); 6732 reg_w(gspca_dev, 0x0c, 0x003a);
6856 reg_w(dev, 0x0b, 0x0039); 6733 reg_w(gspca_dev, 0x0b, 0x0039);
6857 break; 6734 break;
6858 } 6735 }
6859 6736
@@ -6862,15 +6739,15 @@ static int sd_start(struct gspca_dev *gspca_dev)
6862 case SENSOR_ADCM2700: 6739 case SENSOR_ADCM2700:
6863 case SENSOR_OV7620: 6740 case SENSOR_OV7620:
6864 reg_r(gspca_dev, 0x0008); 6741 reg_r(gspca_dev, 0x0008);
6865 reg_w(dev, 0x00, 0x0008); 6742 reg_w(gspca_dev, 0x00, 0x0008);
6866 break; 6743 break;
6867 case SENSOR_PAS202B: 6744 case SENSOR_PAS202B:
6868 case SENSOR_GC0305: 6745 case SENSOR_GC0305:
6869 case SENSOR_TAS5130CXX: 6746 case SENSOR_TAS5130C:
6870 reg_r(gspca_dev, 0x0008); 6747 reg_r(gspca_dev, 0x0008);
6871 /* fall thru */ 6748 /* fall thru */
6872 case SENSOR_PO2030: 6749 case SENSOR_PO2030:
6873 reg_w(dev, 0x03, 0x0008); 6750 reg_w(gspca_dev, 0x03, 0x0008);
6874 break; 6751 break;
6875 } 6752 }
6876 setsharpness(gspca_dev); 6753 setsharpness(gspca_dev);
@@ -6880,7 +6757,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
6880 case SENSOR_CS2102K: /* gamma set in xxx_Initial */ 6757 case SENSOR_CS2102K: /* gamma set in xxx_Initial */
6881 case SENSOR_HDCS2020b: 6758 case SENSOR_HDCS2020b:
6882 case SENSOR_OV7630C: 6759 case SENSOR_OV7630C:
6883 case SENSOR_TAS5130CK:
6884 break; 6760 break;
6885 default: 6761 default:
6886 setcontrast(gspca_dev); 6762 setcontrast(gspca_dev);
@@ -6891,7 +6767,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
6891 case SENSOR_OV7620: 6767 case SENSOR_OV7620:
6892 case SENSOR_PAS202B: 6768 case SENSOR_PAS202B:
6893 reg_r(gspca_dev, 0x0180); /* from win */ 6769 reg_r(gspca_dev, 0x0180); /* from win */
6894 reg_w(dev, 0x00, 0x0180); 6770 reg_w(gspca_dev, 0x00, 0x0180);
6895 break; 6771 break;
6896 default: 6772 default:
6897 setquality(gspca_dev); 6773 setquality(gspca_dev);
@@ -6901,29 +6777,29 @@ static int sd_start(struct gspca_dev *gspca_dev)
6901 6777
6902 switch (sd->sensor) { 6778 switch (sd->sensor) {
6903 case SENSOR_ADCM2700: 6779 case SENSOR_ADCM2700:
6904 reg_w(dev, 0x09, 0x01ad); /* (from win traces) */ 6780 reg_w(gspca_dev, 0x09, 0x01ad); /* (from win traces) */
6905 reg_w(dev, 0x15, 0x01ae); 6781 reg_w(gspca_dev, 0x15, 0x01ae);
6906 reg_w(dev, 0x02, 0x0180); 6782 reg_w(gspca_dev, 0x02, 0x0180);
6907 /* ms-win + */ 6783 /* ms-win + */
6908 reg_w(dev, 0x40, 0x0117); 6784 reg_w(gspca_dev, 0x40, 0x0117);
6909 break; 6785 break;
6910 case SENSOR_GC0305: 6786 case SENSOR_GC0305:
6911 case SENSOR_TAS5130CXX: 6787 case SENSOR_TAS5130C:
6912 reg_w(dev, 0x09, 0x01ad); /* (from win traces) */ 6788 reg_w(gspca_dev, 0x09, 0x01ad); /* (from win traces) */
6913 reg_w(dev, 0x15, 0x01ae); 6789 reg_w(gspca_dev, 0x15, 0x01ae);
6914 /* fall thru */ 6790 /* fall thru */
6915 case SENSOR_PAS202B: 6791 case SENSOR_PAS202B:
6916 case SENSOR_PO2030: 6792 case SENSOR_PO2030:
6917/* reg_w(dev, 0x40, ZC3XX_R117_GGAIN); * (from win traces) */ 6793/* reg_w(gspca_dev, 0x40, ZC3XX_R117_GGAIN); * (from win traces) */
6918 reg_r(gspca_dev, 0x0180); 6794 reg_r(gspca_dev, 0x0180);
6919 break; 6795 break;
6920 case SENSOR_OV7620: 6796 case SENSOR_OV7620:
6921 reg_w(dev, 0x09, 0x01ad); 6797 reg_w(gspca_dev, 0x09, 0x01ad);
6922 reg_w(dev, 0x15, 0x01ae); 6798 reg_w(gspca_dev, 0x15, 0x01ae);
6923 i2c_read(gspca_dev, 0x13); /*fixme: returns 0xa3 */ 6799 i2c_read(gspca_dev, 0x13); /*fixme: returns 0xa3 */
6924 i2c_write(gspca_dev, 0x13, 0xa3, 0x00); 6800 i2c_write(gspca_dev, 0x13, 0xa3, 0x00);
6925 /*fixme: returned value to send? */ 6801 /*fixme: returned value to send? */
6926 reg_w(dev, 0x40, 0x0117); 6802 reg_w(gspca_dev, 0x40, 0x0117);
6927 reg_r(gspca_dev, 0x0180); 6803 reg_r(gspca_dev, 0x0180);
6928 break; 6804 break;
6929 } 6805 }
@@ -6932,11 +6808,11 @@ static int sd_start(struct gspca_dev *gspca_dev)
6932 switch (sd->sensor) { 6808 switch (sd->sensor) {
6933 case SENSOR_PO2030: 6809 case SENSOR_PO2030:
6934 msleep(50); 6810 msleep(50);
6935 reg_w(dev, 0x00, 0x0007); /* (from win traces) */ 6811 reg_w(gspca_dev, 0x00, 0x0007); /* (from win traces) */
6936 reg_w(dev, 0x02, ZC3XX_R008_CLOCKSETTING); 6812 reg_w(gspca_dev, 0x02, ZC3XX_R008_CLOCKSETTING);
6937 break; 6813 break;
6938 } 6814 }
6939 return 0; 6815 return gspca_dev->usb_err;
6940} 6816}
6941 6817
6942/* called on streamoff with alt 0 and on disconnect */ 6818/* called on streamoff with alt 0 and on disconnect */
@@ -6946,7 +6822,7 @@ static void sd_stop0(struct gspca_dev *gspca_dev)
6946 6822
6947 if (!gspca_dev->present) 6823 if (!gspca_dev->present)
6948 return; 6824 return;
6949 send_unknown(gspca_dev->dev, sd->sensor); 6825 send_unknown(gspca_dev, sd->sensor);
6950} 6826}
6951 6827
6952static void sd_pkt_scan(struct gspca_dev *gspca_dev, 6828static void sd_pkt_scan(struct gspca_dev *gspca_dev,
@@ -6981,7 +6857,7 @@ static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
6981 sd->brightness = val; 6857 sd->brightness = val;
6982 if (gspca_dev->streaming) 6858 if (gspca_dev->streaming)
6983 setcontrast(gspca_dev); 6859 setcontrast(gspca_dev);
6984 return 0; 6860 return gspca_dev->usb_err;
6985} 6861}
6986 6862
6987static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val) 6863static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
@@ -6999,7 +6875,7 @@ static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
6999 sd->contrast = val; 6875 sd->contrast = val;
7000 if (gspca_dev->streaming) 6876 if (gspca_dev->streaming)
7001 setcontrast(gspca_dev); 6877 setcontrast(gspca_dev);
7002 return 0; 6878 return gspca_dev->usb_err;
7003} 6879}
7004 6880
7005static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val) 6881static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
@@ -7017,7 +6893,7 @@ static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
7017 sd->autogain = val; 6893 sd->autogain = val;
7018 if (gspca_dev->streaming) 6894 if (gspca_dev->streaming)
7019 setautogain(gspca_dev); 6895 setautogain(gspca_dev);
7020 return 0; 6896 return gspca_dev->usb_err;
7021} 6897}
7022 6898
7023static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val) 6899static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
@@ -7035,7 +6911,7 @@ static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val)
7035 sd->gamma = val; 6911 sd->gamma = val;
7036 if (gspca_dev->streaming) 6912 if (gspca_dev->streaming)
7037 setcontrast(gspca_dev); 6913 setcontrast(gspca_dev);
7038 return 0; 6914 return gspca_dev->usb_err;
7039} 6915}
7040 6916
7041static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val) 6917static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val)
@@ -7053,7 +6929,7 @@ static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
7053 sd->lightfreq = val; 6929 sd->lightfreq = val;
7054 if (gspca_dev->streaming) 6930 if (gspca_dev->streaming)
7055 setlightfreq(gspca_dev); 6931 setlightfreq(gspca_dev);
7056 return 0; 6932 return gspca_dev->usb_err;
7057} 6933}
7058 6934
7059static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val) 6935static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
@@ -7071,7 +6947,7 @@ static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
7071 sd->sharpness = val; 6947 sd->sharpness = val;
7072 if (gspca_dev->streaming) 6948 if (gspca_dev->streaming)
7073 setsharpness(gspca_dev); 6949 setsharpness(gspca_dev);
7074 return 0; 6950 return gspca_dev->usb_err;
7075} 6951}
7076 6952
7077static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val) 6953static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
@@ -7116,7 +6992,7 @@ static int sd_set_jcomp(struct gspca_dev *gspca_dev,
7116 sd->quality = jcomp->quality; 6992 sd->quality = jcomp->quality;
7117 if (gspca_dev->streaming) 6993 if (gspca_dev->streaming)
7118 jpeg_set_qual(sd->jpeg_hdr, sd->quality); 6994 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
7119 return 0; 6995 return gspca_dev->usb_err;
7120} 6996}
7121 6997
7122static int sd_get_jcomp(struct gspca_dev *gspca_dev, 6998static int sd_get_jcomp(struct gspca_dev *gspca_dev,
@@ -7220,7 +7096,6 @@ static const __devinitdata struct usb_device_id device_table[] = {
7220 {USB_DEVICE(0x10fd, 0x8050)}, 7096 {USB_DEVICE(0x10fd, 0x8050)},
7221 {} /* end of entry */ 7097 {} /* end of entry */
7222}; 7098};
7223#undef DVNAME
7224MODULE_DEVICE_TABLE(usb, device_table); 7099MODULE_DEVICE_TABLE(usb, device_table);
7225 7100
7226/* -- device connect -- */ 7101/* -- device connect -- */
diff --git a/drivers/media/video/ivtv/ivtv-controls.c b/drivers/media/video/ivtv/ivtv-controls.c
index b588e30cbcf..b31ee1bceef 100644
--- a/drivers/media/video/ivtv/ivtv-controls.c
+++ b/drivers/media/video/ivtv/ivtv-controls.c
@@ -17,163 +17,14 @@
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */ 19 */
20#include <linux/kernel.h>
21#include <linux/slab.h>
22 20
23#include "ivtv-driver.h" 21#include "ivtv-driver.h"
24#include "ivtv-cards.h"
25#include "ivtv-ioctl.h" 22#include "ivtv-ioctl.h"
26#include "ivtv-routing.h"
27#include "ivtv-i2c.h"
28#include "ivtv-mailbox.h"
29#include "ivtv-controls.h" 23#include "ivtv-controls.h"
30 24
31/* Must be sorted from low to high control ID! */ 25static int ivtv_s_stream_vbi_fmt(struct cx2341x_handler *cxhdl, u32 fmt)
32static const u32 user_ctrls[] = {
33 V4L2_CID_USER_CLASS,
34 V4L2_CID_BRIGHTNESS,
35 V4L2_CID_CONTRAST,
36 V4L2_CID_SATURATION,
37 V4L2_CID_HUE,
38 V4L2_CID_AUDIO_VOLUME,
39 V4L2_CID_AUDIO_BALANCE,
40 V4L2_CID_AUDIO_BASS,
41 V4L2_CID_AUDIO_TREBLE,
42 V4L2_CID_AUDIO_MUTE,
43 V4L2_CID_AUDIO_LOUDNESS,
44 0
45};
46
47static const u32 *ctrl_classes[] = {
48 user_ctrls,
49 cx2341x_mpeg_ctrls,
50 NULL
51};
52
53
54int ivtv_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
55{
56 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
57 const char *name;
58
59 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
60 if (qctrl->id == 0)
61 return -EINVAL;
62
63 switch (qctrl->id) {
64 /* Standard V4L2 controls */
65 case V4L2_CID_USER_CLASS:
66 return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
67 case V4L2_CID_BRIGHTNESS:
68 case V4L2_CID_HUE:
69 case V4L2_CID_SATURATION:
70 case V4L2_CID_CONTRAST:
71 if (v4l2_subdev_call(itv->sd_video, core, queryctrl, qctrl))
72 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
73 return 0;
74
75 case V4L2_CID_AUDIO_VOLUME:
76 case V4L2_CID_AUDIO_MUTE:
77 case V4L2_CID_AUDIO_BALANCE:
78 case V4L2_CID_AUDIO_BASS:
79 case V4L2_CID_AUDIO_TREBLE:
80 case V4L2_CID_AUDIO_LOUDNESS:
81 if (v4l2_subdev_call(itv->sd_audio, core, queryctrl, qctrl))
82 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
83 return 0;
84
85 default:
86 if (cx2341x_ctrl_query(&itv->params, qctrl))
87 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
88 return 0;
89 }
90 strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
91 qctrl->name[sizeof(qctrl->name) - 1] = 0;
92 return 0;
93}
94
95int ivtv_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
96{
97 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
98 struct v4l2_queryctrl qctrl;
99
100 qctrl.id = qmenu->id;
101 ivtv_queryctrl(file, fh, &qctrl);
102 return v4l2_ctrl_query_menu(qmenu, &qctrl,
103 cx2341x_ctrl_get_menu(&itv->params, qmenu->id));
104}
105
106static int ivtv_try_ctrl(struct file *file, void *fh,
107 struct v4l2_ext_control *vctrl)
108{
109 struct v4l2_queryctrl qctrl;
110 const char **menu_items = NULL;
111 int err;
112
113 qctrl.id = vctrl->id;
114 err = ivtv_queryctrl(file, fh, &qctrl);
115 if (err)
116 return err;
117 if (qctrl.type == V4L2_CTRL_TYPE_MENU)
118 menu_items = v4l2_ctrl_get_menu(qctrl.id);
119 return v4l2_ctrl_check(vctrl, &qctrl, menu_items);
120}
121
122static int ivtv_s_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
123{
124 switch (vctrl->id) {
125 /* Standard V4L2 controls */
126 case V4L2_CID_BRIGHTNESS:
127 case V4L2_CID_HUE:
128 case V4L2_CID_SATURATION:
129 case V4L2_CID_CONTRAST:
130 return v4l2_subdev_call(itv->sd_video, core, s_ctrl, vctrl);
131
132 case V4L2_CID_AUDIO_VOLUME:
133 case V4L2_CID_AUDIO_MUTE:
134 case V4L2_CID_AUDIO_BALANCE:
135 case V4L2_CID_AUDIO_BASS:
136 case V4L2_CID_AUDIO_TREBLE:
137 case V4L2_CID_AUDIO_LOUDNESS:
138 return v4l2_subdev_call(itv->sd_audio, core, s_ctrl, vctrl);
139
140 default:
141 IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
142 return -EINVAL;
143 }
144 return 0;
145}
146
147static int ivtv_g_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
148{ 26{
149 switch (vctrl->id) { 27 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
150 /* Standard V4L2 controls */
151 case V4L2_CID_BRIGHTNESS:
152 case V4L2_CID_HUE:
153 case V4L2_CID_SATURATION:
154 case V4L2_CID_CONTRAST:
155 return v4l2_subdev_call(itv->sd_video, core, g_ctrl, vctrl);
156
157 case V4L2_CID_AUDIO_VOLUME:
158 case V4L2_CID_AUDIO_MUTE:
159 case V4L2_CID_AUDIO_BALANCE:
160 case V4L2_CID_AUDIO_BASS:
161 case V4L2_CID_AUDIO_TREBLE:
162 case V4L2_CID_AUDIO_LOUDNESS:
163 return v4l2_subdev_call(itv->sd_audio, core, g_ctrl, vctrl);
164 default:
165 IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
166 return -EINVAL;
167 }
168 return 0;
169}
170
171static int ivtv_setup_vbi_fmt(struct ivtv *itv, enum v4l2_mpeg_stream_vbi_fmt fmt)
172{
173 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
174 return -EINVAL;
175 if (atomic_read(&itv->capturing) > 0)
176 return -EBUSY;
177 28
178 /* First try to allocate sliced VBI buffers if needed. */ 29 /* First try to allocate sliced VBI buffers if needed. */
179 if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) { 30 if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
@@ -208,106 +59,43 @@ static int ivtv_setup_vbi_fmt(struct ivtv *itv, enum v4l2_mpeg_stream_vbi_fmt fm
208 return 0; 59 return 0;
209} 60}
210 61
211int ivtv_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c) 62static int ivtv_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
212{ 63{
213 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv; 64 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
214 struct v4l2_control ctrl; 65 int is_mpeg1 = val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
215 66 struct v4l2_mbus_framefmt fmt;
216 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) { 67
217 int i; 68 /* fix videodecoder resolution */
218 int err = 0; 69 fmt.width = cxhdl->width / (is_mpeg1 ? 2 : 1);
219 70 fmt.height = cxhdl->height;
220 for (i = 0; i < c->count; i++) { 71 fmt.code = V4L2_MBUS_FMT_FIXED;
221 ctrl.id = c->controls[i].id; 72 v4l2_subdev_call(itv->sd_video, video, s_mbus_fmt, &fmt);
222 ctrl.value = c->controls[i].value; 73 return 0;
223 err = ivtv_g_ctrl(itv, &ctrl);
224 c->controls[i].value = ctrl.value;
225 if (err) {
226 c->error_idx = i;
227 break;
228 }
229 }
230 return err;
231 }
232 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
233 return cx2341x_ext_ctrls(&itv->params, 0, c, VIDIOC_G_EXT_CTRLS);
234 return -EINVAL;
235} 74}
236 75
237int ivtv_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c) 76static int ivtv_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
238{ 77{
239 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv; 78 static const u32 freqs[3] = { 44100, 48000, 32000 };
240 struct v4l2_control ctrl; 79 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
241
242 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
243 int i;
244 int err = 0;
245
246 for (i = 0; i < c->count; i++) {
247 ctrl.id = c->controls[i].id;
248 ctrl.value = c->controls[i].value;
249 err = ivtv_s_ctrl(itv, &ctrl);
250 c->controls[i].value = ctrl.value;
251 if (err) {
252 c->error_idx = i;
253 break;
254 }
255 }
256 return err;
257 }
258 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
259 static u32 freqs[3] = { 44100, 48000, 32000 };
260 struct cx2341x_mpeg_params p = itv->params;
261 int err = cx2341x_ext_ctrls(&p, atomic_read(&itv->capturing), c, VIDIOC_S_EXT_CTRLS);
262 unsigned idx;
263
264 if (err)
265 return err;
266 80
267 if (p.video_encoding != itv->params.video_encoding) { 81 /* The audio clock of the digitizer must match the codec sample
268 int is_mpeg1 = p.video_encoding == 82 rate otherwise you get some very strange effects. */
269 V4L2_MPEG_VIDEO_ENCODING_MPEG_1; 83 if (idx < ARRAY_SIZE(freqs))
270 struct v4l2_mbus_framefmt fmt; 84 ivtv_call_all(itv, audio, s_clock_freq, freqs[idx]);
271 85 return 0;
272 /* fix videodecoder resolution */
273 fmt.width = itv->params.width / (is_mpeg1 ? 2 : 1);
274 fmt.height = itv->params.height;
275 fmt.code = V4L2_MBUS_FMT_FIXED;
276 v4l2_subdev_call(itv->sd_video, video, s_mbus_fmt, &fmt);
277 }
278 err = cx2341x_update(itv, ivtv_api_func, &itv->params, &p);
279 if (!err && itv->params.stream_vbi_fmt != p.stream_vbi_fmt)
280 err = ivtv_setup_vbi_fmt(itv, p.stream_vbi_fmt);
281 itv->params = p;
282 itv->dualwatch_stereo_mode = p.audio_properties & 0x0300;
283 idx = p.audio_properties & 0x03;
284 /* The audio clock of the digitizer must match the codec sample
285 rate otherwise you get some very strange effects. */
286 if (idx < ARRAY_SIZE(freqs))
287 ivtv_call_all(itv, audio, s_clock_freq, freqs[idx]);
288 return err;
289 }
290 return -EINVAL;
291} 86}
292 87
293int ivtv_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c) 88static int ivtv_s_audio_mode(struct cx2341x_handler *cxhdl, u32 val)
294{ 89{
295 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv; 90 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
296 91
297 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) { 92 itv->dualwatch_stereo_mode = val;
298 int i; 93 return 0;
299 int err = 0;
300
301 for (i = 0; i < c->count; i++) {
302 err = ivtv_try_ctrl(file, fh, &c->controls[i]);
303 if (err) {
304 c->error_idx = i;
305 break;
306 }
307 }
308 return err;
309 }
310 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
311 return cx2341x_ext_ctrls(&itv->params, atomic_read(&itv->capturing), c, VIDIOC_TRY_EXT_CTRLS);
312 return -EINVAL;
313} 94}
95
96struct cx2341x_handler_ops ivtv_cxhdl_ops = {
97 .s_audio_mode = ivtv_s_audio_mode,
98 .s_audio_sampling_freq = ivtv_s_audio_sampling_freq,
99 .s_video_encoding = ivtv_s_video_encoding,
100 .s_stream_vbi_fmt = ivtv_s_stream_vbi_fmt,
101};
diff --git a/drivers/media/video/ivtv/ivtv-controls.h b/drivers/media/video/ivtv/ivtv-controls.h
index 1c7721e23c9..d12893dd018 100644
--- a/drivers/media/video/ivtv/ivtv-controls.h
+++ b/drivers/media/video/ivtv/ivtv-controls.h
@@ -21,10 +21,6 @@
21#ifndef IVTV_CONTROLS_H 21#ifndef IVTV_CONTROLS_H
22#define IVTV_CONTROLS_H 22#define IVTV_CONTROLS_H
23 23
24int ivtv_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *a); 24extern struct cx2341x_handler_ops ivtv_cxhdl_ops;
25int ivtv_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *a);
26int ivtv_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *a);
27int ivtv_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *a);
28int ivtv_querymenu(struct file *file, void *fh, struct v4l2_querymenu *a);
29 25
30#endif 26#endif
diff --git a/drivers/media/video/ivtv/ivtv-driver.c b/drivers/media/video/ivtv/ivtv-driver.c
index 07c5c18a25c..e421d15b0f5 100644
--- a/drivers/media/video/ivtv/ivtv-driver.c
+++ b/drivers/media/video/ivtv/ivtv-driver.c
@@ -53,6 +53,7 @@
53#include "ivtv-cards.h" 53#include "ivtv-cards.h"
54#include "ivtv-vbi.h" 54#include "ivtv-vbi.h"
55#include "ivtv-routing.h" 55#include "ivtv-routing.h"
56#include "ivtv-controls.h"
56#include "ivtv-gpio.h" 57#include "ivtv-gpio.h"
57 58
58#include <media/tveeprom.h> 59#include <media/tveeprom.h>
@@ -734,9 +735,8 @@ static int __devinit ivtv_init_struct1(struct ivtv *itv)
734 itv->open_id = 1; 735 itv->open_id = 1;
735 736
736 /* Initial settings */ 737 /* Initial settings */
737 cx2341x_fill_defaults(&itv->params); 738 itv->cxhdl.port = CX2341X_PORT_MEMORY;
738 itv->params.port = CX2341X_PORT_MEMORY; 739 itv->cxhdl.capabilities = CX2341X_CAP_HAS_SLICED_VBI;
739 itv->params.capabilities = CX2341X_CAP_HAS_SLICED_VBI;
740 init_waitqueue_head(&itv->eos_waitq); 740 init_waitqueue_head(&itv->eos_waitq);
741 init_waitqueue_head(&itv->event_waitq); 741 init_waitqueue_head(&itv->event_waitq);
742 init_waitqueue_head(&itv->vsync_waitq); 742 init_waitqueue_head(&itv->vsync_waitq);
@@ -1006,6 +1006,13 @@ static int __devinit ivtv_probe(struct pci_dev *pdev,
1006 retval = -ENOMEM; 1006 retval = -ENOMEM;
1007 goto err; 1007 goto err;
1008 } 1008 }
1009 retval = cx2341x_handler_init(&itv->cxhdl, 50);
1010 if (retval)
1011 goto err;
1012 itv->v4l2_dev.ctrl_handler = &itv->cxhdl.hdl;
1013 itv->cxhdl.ops = &ivtv_cxhdl_ops;
1014 itv->cxhdl.priv = itv;
1015 itv->cxhdl.func = ivtv_api_func;
1009 1016
1010 IVTV_DEBUG_INFO("base addr: 0x%08x\n", itv->base_addr); 1017 IVTV_DEBUG_INFO("base addr: 0x%08x\n", itv->base_addr);
1011 1018
@@ -1127,7 +1134,7 @@ static int __devinit ivtv_probe(struct pci_dev *pdev,
1127 itv->yuv_info.v4l2_src_w = itv->yuv_info.osd_full_w; 1134 itv->yuv_info.v4l2_src_w = itv->yuv_info.osd_full_w;
1128 itv->yuv_info.v4l2_src_h = itv->yuv_info.osd_full_h; 1135 itv->yuv_info.v4l2_src_h = itv->yuv_info.osd_full_h;
1129 1136
1130 itv->params.video_gop_size = itv->is_60hz ? 15 : 12; 1137 cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1131 1138
1132 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_MPG] = 0x08000; 1139 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_MPG] = 0x08000;
1133 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_PCM] = 0x01200; 1140 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_PCM] = 0x01200;
@@ -1269,15 +1276,8 @@ int ivtv_init_on_first_open(struct ivtv *itv)
1269 IVTV_DEBUG_INFO("Getting firmware version..\n"); 1276 IVTV_DEBUG_INFO("Getting firmware version..\n");
1270 ivtv_firmware_versions(itv); 1277 ivtv_firmware_versions(itv);
1271 1278
1272 if (itv->card->hw_all & IVTV_HW_CX25840) { 1279 if (itv->card->hw_all & IVTV_HW_CX25840)
1273 struct v4l2_control ctrl;
1274
1275 v4l2_subdev_call(itv->sd_video, core, load_fw); 1280 v4l2_subdev_call(itv->sd_video, core, load_fw);
1276 /* CX25840_CID_ENABLE_PVR150_WORKAROUND */
1277 ctrl.id = V4L2_CID_PRIVATE_BASE;
1278 ctrl.value = itv->pvr150_workaround;
1279 v4l2_subdev_call(itv->sd_video, core, s_ctrl, &ctrl);
1280 }
1281 1281
1282 vf.tuner = 0; 1282 vf.tuner = 0;
1283 vf.type = V4L2_TUNER_ANALOG_TV; 1283 vf.type = V4L2_TUNER_ANALOG_TV;
@@ -1329,6 +1329,8 @@ int ivtv_init_on_first_open(struct ivtv *itv)
1329 /* For cards with video out, this call needs interrupts enabled */ 1329 /* For cards with video out, this call needs interrupts enabled */
1330 ivtv_s_std(NULL, &fh, &itv->tuner_std); 1330 ivtv_s_std(NULL, &fh, &itv->tuner_std);
1331 1331
1332 /* Setup initial controls */
1333 cx2341x_handler_setup(&itv->cxhdl);
1332 return 0; 1334 return 0;
1333} 1335}
1334 1336
diff --git a/drivers/media/video/ivtv/ivtv-driver.h b/drivers/media/video/ivtv/ivtv-driver.h
index 10207124621..75803141481 100644
--- a/drivers/media/video/ivtv/ivtv-driver.h
+++ b/drivers/media/video/ivtv/ivtv-driver.h
@@ -62,6 +62,7 @@
62#include <linux/dvb/audio.h> 62#include <linux/dvb/audio.h>
63#include <media/v4l2-common.h> 63#include <media/v4l2-common.h>
64#include <media/v4l2-ioctl.h> 64#include <media/v4l2-ioctl.h>
65#include <media/v4l2-ctrls.h>
65#include <media/v4l2-device.h> 66#include <media/v4l2-device.h>
66#include <media/v4l2-fh.h> 67#include <media/v4l2-fh.h>
67#include <media/tuner.h> 68#include <media/tuner.h>
@@ -631,6 +632,8 @@ struct ivtv {
631 struct ivtv_options options; /* user options */ 632 struct ivtv_options options; /* user options */
632 633
633 struct v4l2_device v4l2_dev; 634 struct v4l2_device v4l2_dev;
635 struct cx2341x_handler cxhdl;
636 struct v4l2_ctrl_handler hdl_gpio;
634 struct v4l2_subdev sd_gpio; /* GPIO sub-device */ 637 struct v4l2_subdev sd_gpio; /* GPIO sub-device */
635 u16 instance; 638 u16 instance;
636 639
@@ -648,7 +651,6 @@ struct ivtv {
648 v4l2_std_id std_out; /* current TV output standard */ 651 v4l2_std_id std_out; /* current TV output standard */
649 u8 audio_stereo_mode; /* decoder setting how to handle stereo MPEG audio */ 652 u8 audio_stereo_mode; /* decoder setting how to handle stereo MPEG audio */
650 u8 audio_bilingual_mode; /* decoder setting how to handle bilingual MPEG audio */ 653 u8 audio_bilingual_mode; /* decoder setting how to handle bilingual MPEG audio */
651 struct cx2341x_mpeg_params params; /* current encoder parameters */
652 654
653 655
654 /* Locking */ 656 /* Locking */
diff --git a/drivers/media/video/ivtv/ivtv-fileops.c b/drivers/media/video/ivtv/ivtv-fileops.c
index a6a2cdb8156..d727485da88 100644
--- a/drivers/media/video/ivtv/ivtv-fileops.c
+++ b/drivers/media/video/ivtv/ivtv-fileops.c
@@ -150,12 +150,10 @@ void ivtv_release_stream(struct ivtv_stream *s)
150static void ivtv_dualwatch(struct ivtv *itv) 150static void ivtv_dualwatch(struct ivtv *itv)
151{ 151{
152 struct v4l2_tuner vt; 152 struct v4l2_tuner vt;
153 u32 new_bitmap;
154 u32 new_stereo_mode; 153 u32 new_stereo_mode;
155 const u32 stereo_mask = 0x0300; 154 const u32 dual = 0x02;
156 const u32 dual = 0x0200;
157 155
158 new_stereo_mode = itv->params.audio_properties & stereo_mask; 156 new_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
159 memset(&vt, 0, sizeof(vt)); 157 memset(&vt, 0, sizeof(vt));
160 ivtv_call_all(itv, tuner, g_tuner, &vt); 158 ivtv_call_all(itv, tuner, g_tuner, &vt);
161 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2)) 159 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
@@ -164,16 +162,10 @@ static void ivtv_dualwatch(struct ivtv *itv)
164 if (new_stereo_mode == itv->dualwatch_stereo_mode) 162 if (new_stereo_mode == itv->dualwatch_stereo_mode)
165 return; 163 return;
166 164
167 new_bitmap = new_stereo_mode | (itv->params.audio_properties & ~stereo_mask); 165 IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
168 166 itv->dualwatch_stereo_mode, new_stereo_mode);
169 IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. new audio_bitmask=0x%ux\n", 167 if (v4l2_ctrl_s_ctrl(itv->cxhdl.audio_mode, new_stereo_mode))
170 itv->dualwatch_stereo_mode, new_stereo_mode, new_bitmap); 168 IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
171
172 if (ivtv_vapi(itv, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, new_bitmap) == 0) {
173 itv->dualwatch_stereo_mode = new_stereo_mode;
174 return;
175 }
176 IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
177} 169}
178 170
179static void ivtv_update_pgm_info(struct ivtv *itv) 171static void ivtv_update_pgm_info(struct ivtv *itv)
@@ -894,7 +886,8 @@ int ivtv_v4l2_close(struct file *filp)
894 if (atomic_read(&itv->capturing) > 0) { 886 if (atomic_read(&itv->capturing) > 0) {
895 /* Undo video mute */ 887 /* Undo video mute */
896 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1, 888 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
897 itv->params.video_mute | (itv->params.video_mute_yuv << 8)); 889 v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute) |
890 (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
898 } 891 }
899 /* Done! Unmute and continue. */ 892 /* Done! Unmute and continue. */
900 ivtv_unmute(itv); 893 ivtv_unmute(itv);
diff --git a/drivers/media/video/ivtv/ivtv-firmware.c b/drivers/media/video/ivtv/ivtv-firmware.c
index d8bf2b01729..4df01947a7d 100644
--- a/drivers/media/video/ivtv/ivtv-firmware.c
+++ b/drivers/media/video/ivtv/ivtv-firmware.c
@@ -248,9 +248,9 @@ void ivtv_init_mpeg_decoder(struct ivtv *itv)
248 volatile u8 __iomem *mem_offset; 248 volatile u8 __iomem *mem_offset;
249 249
250 data[0] = 0; 250 data[0] = 0;
251 data[1] = itv->params.width; /* YUV source width */ 251 data[1] = itv->cxhdl.width; /* YUV source width */
252 data[2] = itv->params.height; 252 data[2] = itv->cxhdl.height;
253 data[3] = itv->params.audio_properties; /* Audio settings to use, 253 data[3] = itv->cxhdl.audio_properties; /* Audio settings to use,
254 bitmap. see docs. */ 254 bitmap. see docs. */
255 if (ivtv_api(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, data)) { 255 if (ivtv_api(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, data)) {
256 IVTV_ERR("ivtv_init_mpeg_decoder failed to set decoder source\n"); 256 IVTV_ERR("ivtv_init_mpeg_decoder failed to set decoder source\n");
diff --git a/drivers/media/video/ivtv/ivtv-gpio.c b/drivers/media/video/ivtv/ivtv-gpio.c
index aede061cae5..8f0d0778905 100644
--- a/drivers/media/video/ivtv/ivtv-gpio.c
+++ b/drivers/media/video/ivtv/ivtv-gpio.c
@@ -24,6 +24,7 @@
24#include "ivtv-gpio.h" 24#include "ivtv-gpio.h"
25#include "tuner-xc2028.h" 25#include "tuner-xc2028.h"
26#include <media/tuner.h> 26#include <media/tuner.h>
27#include <media/v4l2-ctrls.h>
27 28
28/* 29/*
29 * GPIO assignment of Yuan MPG600/MPG160 30 * GPIO assignment of Yuan MPG600/MPG160
@@ -149,16 +150,10 @@ static inline struct ivtv *sd_to_ivtv(struct v4l2_subdev *sd)
149 return container_of(sd, struct ivtv, sd_gpio); 150 return container_of(sd, struct ivtv, sd_gpio);
150} 151}
151 152
152static struct v4l2_queryctrl gpio_ctrl_mute = { 153static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
153 .id = V4L2_CID_AUDIO_MUTE, 154{
154 .type = V4L2_CTRL_TYPE_BOOLEAN, 155 return &container_of(ctrl->handler, struct ivtv, hdl_gpio)->sd_gpio;
155 .name = "Mute", 156}
156 .minimum = 0,
157 .maximum = 1,
158 .step = 1,
159 .default_value = 1,
160 .flags = 0,
161};
162 157
163static int subdev_s_clock_freq(struct v4l2_subdev *sd, u32 freq) 158static int subdev_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
164{ 159{
@@ -262,40 +257,24 @@ static int subdev_s_audio_routing(struct v4l2_subdev *sd,
262 return 0; 257 return 0;
263} 258}
264 259
265static int subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 260static int subdev_s_ctrl(struct v4l2_ctrl *ctrl)
266{ 261{
262 struct v4l2_subdev *sd = to_sd(ctrl);
267 struct ivtv *itv = sd_to_ivtv(sd); 263 struct ivtv *itv = sd_to_ivtv(sd);
268 u16 mask, data; 264 u16 mask, data;
269 265
270 if (ctrl->id != V4L2_CID_AUDIO_MUTE) 266 switch (ctrl->id) {
271 return -EINVAL; 267 case V4L2_CID_AUDIO_MUTE:
272 mask = itv->card->gpio_audio_mute.mask; 268 mask = itv->card->gpio_audio_mute.mask;
273 data = itv->card->gpio_audio_mute.mute; 269 data = ctrl->val ? itv->card->gpio_audio_mute.mute : 0;
274 ctrl->value = (read_reg(IVTV_REG_GPIO_OUT) & mask) == data; 270 if (mask)
275 return 0; 271 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) |
276} 272 (data & mask), IVTV_REG_GPIO_OUT);
277 273 return 0;
278static int subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 274 }
279{ 275 return -EINVAL;
280 struct ivtv *itv = sd_to_ivtv(sd);
281 u16 mask, data;
282
283 if (ctrl->id != V4L2_CID_AUDIO_MUTE)
284 return -EINVAL;
285 mask = itv->card->gpio_audio_mute.mask;
286 data = ctrl->value ? itv->card->gpio_audio_mute.mute : 0;
287 if (mask)
288 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
289 return 0;
290} 276}
291 277
292static int subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
293{
294 if (qc->id != V4L2_CID_AUDIO_MUTE)
295 return -EINVAL;
296 *qc = gpio_ctrl_mute;
297 return 0;
298}
299 278
300static int subdev_log_status(struct v4l2_subdev *sd) 279static int subdev_log_status(struct v4l2_subdev *sd)
301{ 280{
@@ -304,6 +283,7 @@ static int subdev_log_status(struct v4l2_subdev *sd)
304 IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n", 283 IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",
305 read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT), 284 read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT),
306 read_reg(IVTV_REG_GPIO_IN)); 285 read_reg(IVTV_REG_GPIO_IN));
286 v4l2_ctrl_handler_log_status(&itv->hdl_gpio, sd->name);
307 return 0; 287 return 0;
308} 288}
309 289
@@ -327,11 +307,19 @@ static int subdev_s_video_routing(struct v4l2_subdev *sd,
327 return 0; 307 return 0;
328} 308}
329 309
310static const struct v4l2_ctrl_ops gpio_ctrl_ops = {
311 .s_ctrl = subdev_s_ctrl,
312};
313
330static const struct v4l2_subdev_core_ops subdev_core_ops = { 314static const struct v4l2_subdev_core_ops subdev_core_ops = {
331 .log_status = subdev_log_status, 315 .log_status = subdev_log_status,
332 .g_ctrl = subdev_g_ctrl, 316 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
333 .s_ctrl = subdev_s_ctrl, 317 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
334 .queryctrl = subdev_queryctrl, 318 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
319 .g_ctrl = v4l2_subdev_g_ctrl,
320 .s_ctrl = v4l2_subdev_s_ctrl,
321 .queryctrl = v4l2_subdev_queryctrl,
322 .querymenu = v4l2_subdev_querymenu,
335}; 323};
336 324
337static const struct v4l2_subdev_tuner_ops subdev_tuner_ops = { 325static const struct v4l2_subdev_tuner_ops subdev_tuner_ops = {
@@ -375,5 +363,12 @@ int ivtv_gpio_init(struct ivtv *itv)
375 v4l2_subdev_init(&itv->sd_gpio, &subdev_ops); 363 v4l2_subdev_init(&itv->sd_gpio, &subdev_ops);
376 snprintf(itv->sd_gpio.name, sizeof(itv->sd_gpio.name), "%s-gpio", itv->v4l2_dev.name); 364 snprintf(itv->sd_gpio.name, sizeof(itv->sd_gpio.name), "%s-gpio", itv->v4l2_dev.name);
377 itv->sd_gpio.grp_id = IVTV_HW_GPIO; 365 itv->sd_gpio.grp_id = IVTV_HW_GPIO;
366 v4l2_ctrl_handler_init(&itv->hdl_gpio, 1);
367 v4l2_ctrl_new_std(&itv->hdl_gpio, &gpio_ctrl_ops,
368 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
369 if (itv->hdl_gpio.error)
370 return itv->hdl_gpio.error;
371 itv->sd_gpio.ctrl_handler = &itv->hdl_gpio;
372 v4l2_ctrl_handler_setup(&itv->hdl_gpio);
378 return v4l2_device_register_subdev(&itv->v4l2_dev, &itv->sd_gpio); 373 return v4l2_device_register_subdev(&itv->v4l2_dev, &itv->sd_gpio);
379} 374}
diff --git a/drivers/media/video/ivtv/ivtv-i2c.c b/drivers/media/video/ivtv/ivtv-i2c.c
index a5b92d109c6..d391bbdb0b8 100644
--- a/drivers/media/video/ivtv/ivtv-i2c.c
+++ b/drivers/media/video/ivtv/ivtv-i2c.c
@@ -63,6 +63,7 @@
63#include "ivtv-cards.h" 63#include "ivtv-cards.h"
64#include "ivtv-gpio.h" 64#include "ivtv-gpio.h"
65#include "ivtv-i2c.h" 65#include "ivtv-i2c.h"
66#include <media/cx25840.h>
66 67
67/* i2c implementation for cx23415/6 chip, ivtv project. 68/* i2c implementation for cx23415/6 chip, ivtv project.
68 * Author: Kevin Thayer (nufan_wfk at yahoo.com) 69 * Author: Kevin Thayer (nufan_wfk at yahoo.com)
@@ -292,6 +293,12 @@ int ivtv_i2c_register(struct ivtv *itv, unsigned idx)
292 if (hw == IVTV_HW_UPD64031A || hw == IVTV_HW_UPD6408X) { 293 if (hw == IVTV_HW_UPD64031A || hw == IVTV_HW_UPD6408X) {
293 sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, 294 sd = v4l2_i2c_new_subdev(&itv->v4l2_dev,
294 adap, mod, type, 0, I2C_ADDRS(hw_addrs[idx])); 295 adap, mod, type, 0, I2C_ADDRS(hw_addrs[idx]));
296 } else if (hw == IVTV_HW_CX25840) {
297 struct cx25840_platform_data pdata;
298
299 pdata.pvr150_workaround = itv->pvr150_workaround;
300 sd = v4l2_i2c_new_subdev_cfg(&itv->v4l2_dev,
301 adap, mod, type, 0, &pdata, hw_addrs[idx], NULL);
295 } else { 302 } else {
296 sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, 303 sd = v4l2_i2c_new_subdev(&itv->v4l2_dev,
297 adap, mod, type, hw_addrs[idx], NULL); 304 adap, mod, type, hw_addrs[idx], NULL);
diff --git a/drivers/media/video/ivtv/ivtv-ioctl.c b/drivers/media/video/ivtv/ivtv-ioctl.c
index 11ac2fa33ef..4eed9123683 100644
--- a/drivers/media/video/ivtv/ivtv-ioctl.c
+++ b/drivers/media/video/ivtv/ivtv-ioctl.c
@@ -162,7 +162,7 @@ int ivtv_set_speed(struct ivtv *itv, int speed)
162 data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0; 162 data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
163 data[1] = (speed < 0); 163 data[1] = (speed < 0);
164 data[2] = speed < 0 ? 3 : 7; 164 data[2] = speed < 0 ? 3 : 7;
165 data[3] = itv->params.video_b_frames; 165 data[3] = v4l2_ctrl_g_ctrl(itv->cxhdl.video_b_frames);
166 data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0; 166 data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
167 data[5] = 0; 167 data[5] = 0;
168 data[6] = 0; 168 data[6] = 0;
@@ -339,8 +339,8 @@ static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f
339 struct ivtv *itv = id->itv; 339 struct ivtv *itv = id->itv;
340 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix; 340 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
341 341
342 pixfmt->width = itv->params.width; 342 pixfmt->width = itv->cxhdl.width;
343 pixfmt->height = itv->params.height; 343 pixfmt->height = itv->cxhdl.height;
344 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M; 344 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
345 pixfmt->field = V4L2_FIELD_INTERLACED; 345 pixfmt->field = V4L2_FIELD_INTERLACED;
346 pixfmt->priv = 0; 346 pixfmt->priv = 0;
@@ -568,7 +568,6 @@ static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f
568{ 568{
569 struct ivtv_open_id *id = fh; 569 struct ivtv_open_id *id = fh;
570 struct ivtv *itv = id->itv; 570 struct ivtv *itv = id->itv;
571 struct cx2341x_mpeg_params *p = &itv->params;
572 struct v4l2_mbus_framefmt mbus_fmt; 571 struct v4l2_mbus_framefmt mbus_fmt;
573 int ret = ivtv_try_fmt_vid_cap(file, fh, fmt); 572 int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
574 int w = fmt->fmt.pix.width; 573 int w = fmt->fmt.pix.width;
@@ -577,15 +576,15 @@ static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f
577 if (ret) 576 if (ret)
578 return ret; 577 return ret;
579 578
580 if (p->width == w && p->height == h) 579 if (itv->cxhdl.width == w && itv->cxhdl.height == h)
581 return 0; 580 return 0;
582 581
583 if (atomic_read(&itv->capturing) > 0) 582 if (atomic_read(&itv->capturing) > 0)
584 return -EBUSY; 583 return -EBUSY;
585 584
586 p->width = w; 585 itv->cxhdl.width = w;
587 p->height = h; 586 itv->cxhdl.height = h;
588 if (p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1) 587 if (v4l2_ctrl_g_ctrl(itv->cxhdl.video_encoding) == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
589 fmt->fmt.pix.width /= 2; 588 fmt->fmt.pix.width /= 2;
590 mbus_fmt.width = fmt->fmt.pix.width; 589 mbus_fmt.width = fmt->fmt.pix.width;
591 mbus_fmt.height = h; 590 mbus_fmt.height = h;
@@ -1114,9 +1113,10 @@ int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
1114 1113
1115 itv->std = *std; 1114 itv->std = *std;
1116 itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0; 1115 itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
1117 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz; 1116 itv->is_50hz = !itv->is_60hz;
1118 itv->params.width = 720; 1117 cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1119 itv->params.height = itv->is_50hz ? 576 : 480; 1118 itv->cxhdl.width = 720;
1119 itv->cxhdl.height = itv->is_50hz ? 576 : 480;
1120 itv->vbi.count = itv->is_50hz ? 18 : 12; 1120 itv->vbi.count = itv->is_50hz ? 18 : 12;
1121 itv->vbi.start[0] = itv->is_50hz ? 6 : 10; 1121 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1122 itv->vbi.start[1] = itv->is_50hz ? 318 : 273; 1122 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
@@ -1157,7 +1157,7 @@ int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
1157 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz); 1157 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1158 itv->main_rect.left = itv->main_rect.top = 0; 1158 itv->main_rect.left = itv->main_rect.top = 0;
1159 itv->main_rect.width = 720; 1159 itv->main_rect.width = 720;
1160 itv->main_rect.height = itv->params.height; 1160 itv->main_rect.height = itv->cxhdl.height;
1161 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4, 1161 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1162 720, itv->main_rect.height, 0, 0); 1162 720, itv->main_rect.height, 0, 0);
1163 yi->main_rect = itv->main_rect; 1163 yi->main_rect = itv->main_rect;
@@ -1554,7 +1554,7 @@ static int ivtv_log_status(struct file *file, void *fh)
1554 } 1554 }
1555 IVTV_INFO("Tuner: %s\n", 1555 IVTV_INFO("Tuner: %s\n",
1556 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV"); 1556 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1557 cx2341x_log_status(&itv->params, itv->v4l2_dev.name); 1557 v4l2_ctrl_handler_log_status(&itv->cxhdl.hdl, itv->v4l2_dev.name);
1558 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags); 1558 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1559 for (i = 0; i < IVTV_MAX_STREAMS; i++) { 1559 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1560 struct ivtv_stream *s = &itv->streams[i]; 1560 struct ivtv_stream *s = &itv->streams[i];
@@ -1942,11 +1942,6 @@ static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1942 .vidioc_s_register = ivtv_s_register, 1942 .vidioc_s_register = ivtv_s_register,
1943#endif 1943#endif
1944 .vidioc_default = ivtv_default, 1944 .vidioc_default = ivtv_default,
1945 .vidioc_queryctrl = ivtv_queryctrl,
1946 .vidioc_querymenu = ivtv_querymenu,
1947 .vidioc_g_ext_ctrls = ivtv_g_ext_ctrls,
1948 .vidioc_s_ext_ctrls = ivtv_s_ext_ctrls,
1949 .vidioc_try_ext_ctrls = ivtv_try_ext_ctrls,
1950 .vidioc_subscribe_event = ivtv_subscribe_event, 1945 .vidioc_subscribe_event = ivtv_subscribe_event,
1951 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1946 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1952}; 1947};
diff --git a/drivers/media/video/ivtv/ivtv-streams.c b/drivers/media/video/ivtv/ivtv-streams.c
index 55df4190c28..512607e0cda 100644
--- a/drivers/media/video/ivtv/ivtv-streams.c
+++ b/drivers/media/video/ivtv/ivtv-streams.c
@@ -210,6 +210,7 @@ static int ivtv_prep_dev(struct ivtv *itv, int type)
210 210
211 s->vdev->num = num; 211 s->vdev->num = num;
212 s->vdev->v4l2_dev = &itv->v4l2_dev; 212 s->vdev->v4l2_dev = &itv->v4l2_dev;
213 s->vdev->ctrl_handler = itv->v4l2_dev.ctrl_handler;
213 s->vdev->fops = ivtv_stream_info[type].fops; 214 s->vdev->fops = ivtv_stream_info[type].fops;
214 s->vdev->release = video_device_release; 215 s->vdev->release = video_device_release;
215 s->vdev->tvnorms = V4L2_STD_ALL; 216 s->vdev->tvnorms = V4L2_STD_ALL;
@@ -451,7 +452,6 @@ int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
451{ 452{
452 u32 data[CX2341X_MBOX_MAX_DATA]; 453 u32 data[CX2341X_MBOX_MAX_DATA];
453 struct ivtv *itv = s->itv; 454 struct ivtv *itv = s->itv;
454 struct cx2341x_mpeg_params *p = &itv->params;
455 int captype = 0, subtype = 0; 455 int captype = 0, subtype = 0;
456 int enable_passthrough = 0; 456 int enable_passthrough = 0;
457 457
@@ -472,7 +472,7 @@ int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
472 } 472 }
473 itv->mpg_data_received = itv->vbi_data_inserted = 0; 473 itv->mpg_data_received = itv->vbi_data_inserted = 0;
474 itv->dualwatch_jiffies = jiffies; 474 itv->dualwatch_jiffies = jiffies;
475 itv->dualwatch_stereo_mode = p->audio_properties & 0x0300; 475 itv->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
476 itv->search_pack_header = 0; 476 itv->search_pack_header = 0;
477 break; 477 break;
478 478
@@ -560,12 +560,12 @@ int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
560 itv->pgm_info_offset, itv->pgm_info_num); 560 itv->pgm_info_offset, itv->pgm_info_num);
561 561
562 /* Setup API for Stream */ 562 /* Setup API for Stream */
563 cx2341x_update(itv, ivtv_api_func, NULL, p); 563 cx2341x_handler_setup(&itv->cxhdl);
564 564
565 /* mute if capturing radio */ 565 /* mute if capturing radio */
566 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) 566 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags))
567 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1, 567 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
568 1 | (p->video_mute_yuv << 8)); 568 1 | (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
569 } 569 }
570 570
571 /* Vsync Setup */ 571 /* Vsync Setup */
@@ -581,6 +581,8 @@ int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
581 581
582 clear_bit(IVTV_F_I_EOS, &itv->i_flags); 582 clear_bit(IVTV_F_I_EOS, &itv->i_flags);
583 583
584 cx2341x_handler_set_busy(&itv->cxhdl, 1);
585
584 /* Initialize Digitizer for Capture */ 586 /* Initialize Digitizer for Capture */
585 /* Avoid tinny audio problem - ensure audio clocks are going */ 587 /* Avoid tinny audio problem - ensure audio clocks are going */
586 v4l2_subdev_call(itv->sd_audio, audio, s_stream, 1); 588 v4l2_subdev_call(itv->sd_audio, audio, s_stream, 1);
@@ -617,7 +619,6 @@ static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)
617{ 619{
618 u32 data[CX2341X_MBOX_MAX_DATA]; 620 u32 data[CX2341X_MBOX_MAX_DATA];
619 struct ivtv *itv = s->itv; 621 struct ivtv *itv = s->itv;
620 struct cx2341x_mpeg_params *p = &itv->params;
621 int datatype; 622 int datatype;
622 u16 width; 623 u16 width;
623 u16 height; 624 u16 height;
@@ -627,8 +628,8 @@ static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)
627 628
628 IVTV_DEBUG_INFO("Setting some initial decoder settings\n"); 629 IVTV_DEBUG_INFO("Setting some initial decoder settings\n");
629 630
630 width = p->width; 631 width = itv->cxhdl.width;
631 height = p->height; 632 height = itv->cxhdl.height;
632 633
633 /* set audio mode to left/stereo for dual/stereo mode. */ 634 /* set audio mode to left/stereo for dual/stereo mode. */
634 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode); 635 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
@@ -668,7 +669,7 @@ static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)
668 break; 669 break;
669 } 670 }
670 if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype, 671 if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype,
671 width, height, p->audio_properties)) { 672 width, height, itv->cxhdl.audio_properties)) {
672 IVTV_DEBUG_WARN("Couldn't initialize decoder source\n"); 673 IVTV_DEBUG_WARN("Couldn't initialize decoder source\n");
673 } 674 }
674 675
@@ -847,6 +848,8 @@ int ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end)
847 return 0; 848 return 0;
848 } 849 }
849 850
851 cx2341x_handler_set_busy(&itv->cxhdl, 0);
852
850 /* Set the following Interrupt mask bits for capture */ 853 /* Set the following Interrupt mask bits for capture */
851 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE); 854 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
852 del_timer(&itv->dma_timer); 855 del_timer(&itv->dma_timer);
@@ -967,7 +970,8 @@ int ivtv_passthrough_mode(struct ivtv *itv, int enable)
967 970
968 /* Setup capture if not already done */ 971 /* Setup capture if not already done */
969 if (atomic_read(&itv->capturing) == 0) { 972 if (atomic_read(&itv->capturing) == 0) {
970 cx2341x_update(itv, ivtv_api_func, NULL, &itv->params); 973 cx2341x_handler_setup(&itv->cxhdl);
974 cx2341x_handler_set_busy(&itv->cxhdl, 1);
971 } 975 }
972 976
973 /* Start Passthrough Mode */ 977 /* Start Passthrough Mode */
@@ -988,6 +992,8 @@ int ivtv_passthrough_mode(struct ivtv *itv, int enable)
988 clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags); 992 clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
989 clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags); 993 clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
990 itv->output_mode = OUT_NONE; 994 itv->output_mode = OUT_NONE;
995 if (atomic_read(&itv->capturing) == 0)
996 cx2341x_handler_set_busy(&itv->cxhdl, 0);
991 997
992 return 0; 998 return 0;
993} 999}
diff --git a/drivers/media/video/msp3400-driver.c b/drivers/media/video/msp3400-driver.c
index e9df3cb02cc..0e412131da7 100644
--- a/drivers/media/video/msp3400-driver.c
+++ b/drivers/media/video/msp3400-driver.c
@@ -283,51 +283,6 @@ void msp_set_scart(struct i2c_client *client, int in, int out)
283 msp_write_dem(client, 0x40, state->i2s_mode); 283 msp_write_dem(client, 0x40, state->i2s_mode);
284} 284}
285 285
286void msp_set_audio(struct i2c_client *client)
287{
288 struct msp_state *state = to_state(i2c_get_clientdata(client));
289 int bal = 0, bass, treble, loudness;
290 int val = 0;
291 int reallymuted = state->muted | state->scan_in_progress;
292
293 if (!reallymuted)
294 val = (state->volume * 0x7f / 65535) << 8;
295
296 v4l_dbg(1, msp_debug, client, "mute=%s scanning=%s volume=%d\n",
297 state->muted ? "on" : "off",
298 state->scan_in_progress ? "yes" : "no",
299 state->volume);
300
301 msp_write_dsp(client, 0x0000, val);
302 msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
303 if (state->has_scart2_out_volume)
304 msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
305 if (state->has_headphones)
306 msp_write_dsp(client, 0x0006, val);
307 if (!state->has_sound_processing)
308 return;
309
310 if (val)
311 bal = (u8)((state->balance / 256) - 128);
312 bass = ((state->bass - 32768) * 0x60 / 65535) << 8;
313 treble = ((state->treble - 32768) * 0x60 / 65535) << 8;
314 loudness = state->loudness ? ((5 * 4) << 8) : 0;
315
316 v4l_dbg(1, msp_debug, client, "balance=%d bass=%d treble=%d loudness=%d\n",
317 state->balance, state->bass, state->treble, state->loudness);
318
319 msp_write_dsp(client, 0x0001, bal << 8);
320 msp_write_dsp(client, 0x0002, bass);
321 msp_write_dsp(client, 0x0003, treble);
322 msp_write_dsp(client, 0x0004, loudness);
323 if (!state->has_headphones)
324 return;
325 msp_write_dsp(client, 0x0030, bal << 8);
326 msp_write_dsp(client, 0x0031, bass);
327 msp_write_dsp(client, 0x0032, treble);
328 msp_write_dsp(client, 0x0033, loudness);
329}
330
331/* ------------------------------------------------------------------------ */ 286/* ------------------------------------------------------------------------ */
332 287
333static void msp_wake_thread(struct i2c_client *client) 288static void msp_wake_thread(struct i2c_client *client)
@@ -363,98 +318,73 @@ int msp_sleep(struct msp_state *state, int timeout)
363 318
364/* ------------------------------------------------------------------------ */ 319/* ------------------------------------------------------------------------ */
365 320
366static int msp_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 321static int msp_s_ctrl(struct v4l2_ctrl *ctrl)
367{ 322{
368 struct msp_state *state = to_state(sd); 323 struct msp_state *state = ctrl_to_state(ctrl);
324 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
325 int val = ctrl->val;
369 326
370 switch (ctrl->id) { 327 switch (ctrl->id) {
371 case V4L2_CID_AUDIO_VOLUME: 328 case V4L2_CID_AUDIO_VOLUME: {
372 ctrl->value = state->volume; 329 /* audio volume cluster */
373 break; 330 int reallymuted = state->muted->val | state->scan_in_progress;
374 331
375 case V4L2_CID_AUDIO_MUTE: 332 if (!reallymuted)
376 ctrl->value = state->muted; 333 val = (val * 0x7f / 65535) << 8;
377 break; 334
378 335 v4l_dbg(1, msp_debug, client, "mute=%s scanning=%s volume=%d\n",
379 case V4L2_CID_AUDIO_BALANCE: 336 state->muted->val ? "on" : "off",
380 if (!state->has_sound_processing) 337 state->scan_in_progress ? "yes" : "no",
381 return -EINVAL; 338 state->volume->val);
382 ctrl->value = state->balance; 339
383 break; 340 msp_write_dsp(client, 0x0000, val);
384 341 msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
385 case V4L2_CID_AUDIO_BASS: 342 if (state->has_scart2_out_volume)
386 if (!state->has_sound_processing) 343 msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
387 return -EINVAL; 344 if (state->has_headphones)
388 ctrl->value = state->bass; 345 msp_write_dsp(client, 0x0006, val);
389 break; 346 break;
390
391 case V4L2_CID_AUDIO_TREBLE:
392 if (!state->has_sound_processing)
393 return -EINVAL;
394 ctrl->value = state->treble;
395 break;
396
397 case V4L2_CID_AUDIO_LOUDNESS:
398 if (!state->has_sound_processing)
399 return -EINVAL;
400 ctrl->value = state->loudness;
401 break;
402
403 default:
404 return -EINVAL;
405 } 347 }
406 return 0;
407}
408
409static int msp_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
410{
411 struct msp_state *state = to_state(sd);
412 struct i2c_client *client = v4l2_get_subdevdata(sd);
413
414 switch (ctrl->id) {
415 case V4L2_CID_AUDIO_VOLUME:
416 state->volume = ctrl->value;
417 if (state->volume == 0)
418 state->balance = 32768;
419 break;
420
421 case V4L2_CID_AUDIO_MUTE:
422 if (ctrl->value < 0 || ctrl->value >= 2)
423 return -ERANGE;
424 state->muted = ctrl->value;
425 break;
426 348
427 case V4L2_CID_AUDIO_BASS: 349 case V4L2_CID_AUDIO_BASS:
428 if (!state->has_sound_processing) 350 val = ((val - 32768) * 0x60 / 65535) << 8;
429 return -EINVAL; 351 msp_write_dsp(client, 0x0002, val);
430 state->bass = ctrl->value; 352 if (state->has_headphones)
353 msp_write_dsp(client, 0x0031, val);
431 break; 354 break;
432 355
433 case V4L2_CID_AUDIO_TREBLE: 356 case V4L2_CID_AUDIO_TREBLE:
434 if (!state->has_sound_processing) 357 val = ((val - 32768) * 0x60 / 65535) << 8;
435 return -EINVAL; 358 msp_write_dsp(client, 0x0003, val);
436 state->treble = ctrl->value; 359 if (state->has_headphones)
360 msp_write_dsp(client, 0x0032, val);
437 break; 361 break;
438 362
439 case V4L2_CID_AUDIO_LOUDNESS: 363 case V4L2_CID_AUDIO_LOUDNESS:
440 if (!state->has_sound_processing) 364 val = val ? ((5 * 4) << 8) : 0;
441 return -EINVAL; 365 msp_write_dsp(client, 0x0004, val);
442 state->loudness = ctrl->value; 366 if (state->has_headphones)
367 msp_write_dsp(client, 0x0033, val);
443 break; 368 break;
444 369
445 case V4L2_CID_AUDIO_BALANCE: 370 case V4L2_CID_AUDIO_BALANCE:
446 if (!state->has_sound_processing) 371 val = (u8)((val / 256) - 128);
447 return -EINVAL; 372 msp_write_dsp(client, 0x0001, val << 8);
448 state->balance = ctrl->value; 373 if (state->has_headphones)
374 msp_write_dsp(client, 0x0030, val << 8);
449 break; 375 break;
450 376
451 default: 377 default:
452 return -EINVAL; 378 return -EINVAL;
453 } 379 }
454 msp_set_audio(client);
455 return 0; 380 return 0;
456} 381}
457 382
383void msp_update_volume(struct msp_state *state)
384{
385 v4l2_ctrl_s_ctrl(state->volume, v4l2_ctrl_g_ctrl(state->volume));
386}
387
458/* --- v4l2 ioctls --- */ 388/* --- v4l2 ioctls --- */
459static int msp_s_radio(struct v4l2_subdev *sd) 389static int msp_s_radio(struct v4l2_subdev *sd)
460{ 390{
@@ -472,7 +402,7 @@ static int msp_s_radio(struct v4l2_subdev *sd)
472 msp3400c_set_mode(client, MSP_MODE_FM_RADIO); 402 msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
473 msp3400c_set_carrier(client, MSP_CARRIER(10.7), 403 msp3400c_set_carrier(client, MSP_CARRIER(10.7),
474 MSP_CARRIER(10.7)); 404 MSP_CARRIER(10.7));
475 msp_set_audio(client); 405 msp_update_volume(state);
476 break; 406 break;
477 case OPMODE_AUTODETECT: 407 case OPMODE_AUTODETECT:
478 case OPMODE_AUTOSELECT: 408 case OPMODE_AUTOSELECT:
@@ -592,33 +522,6 @@ static int msp_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
592 return 0; 522 return 0;
593} 523}
594 524
595static int msp_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
596{
597 struct msp_state *state = to_state(sd);
598
599 switch (qc->id) {
600 case V4L2_CID_AUDIO_VOLUME:
601 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 58880);
602 case V4L2_CID_AUDIO_MUTE:
603 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
604 default:
605 break;
606 }
607 if (!state->has_sound_processing)
608 return -EINVAL;
609 switch (qc->id) {
610 case V4L2_CID_AUDIO_LOUDNESS:
611 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
612 case V4L2_CID_AUDIO_BALANCE:
613 case V4L2_CID_AUDIO_BASS:
614 case V4L2_CID_AUDIO_TREBLE:
615 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
616 default:
617 return -EINVAL;
618 }
619 return 0;
620}
621
622static int msp_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip) 525static int msp_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
623{ 526{
624 struct msp_state *state = to_state(sd); 527 struct msp_state *state = to_state(sd);
@@ -633,19 +536,14 @@ static int msp_log_status(struct v4l2_subdev *sd)
633 struct msp_state *state = to_state(sd); 536 struct msp_state *state = to_state(sd);
634 struct i2c_client *client = v4l2_get_subdevdata(sd); 537 struct i2c_client *client = v4l2_get_subdevdata(sd);
635 const char *p; 538 const char *p;
539 char prefix[V4L2_SUBDEV_NAME_SIZE + 20];
636 540
637 if (state->opmode == OPMODE_AUTOSELECT) 541 if (state->opmode == OPMODE_AUTOSELECT)
638 msp_detect_stereo(client); 542 msp_detect_stereo(client);
639 v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n", 543 v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
640 client->name, state->rev1, state->rev2); 544 client->name, state->rev1, state->rev2);
641 v4l_info(client, "Audio: volume %d%s\n", 545 snprintf(prefix, sizeof(prefix), "%s: Audio: ", sd->name);
642 state->volume, state->muted ? " (muted)" : ""); 546 v4l2_ctrl_handler_log_status(&state->hdl, prefix);
643 if (state->has_sound_processing) {
644 v4l_info(client, "Audio: balance %d bass %d treble %d loudness %s\n",
645 state->balance, state->bass,
646 state->treble,
647 state->loudness ? "on" : "off");
648 }
649 switch (state->mode) { 547 switch (state->mode) {
650 case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break; 548 case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
651 case MSP_MODE_FM_RADIO: p = "FM Radio"; break; 549 case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
@@ -695,12 +593,20 @@ static int msp_resume(struct i2c_client *client)
695 593
696/* ----------------------------------------------------------------------- */ 594/* ----------------------------------------------------------------------- */
697 595
596static const struct v4l2_ctrl_ops msp_ctrl_ops = {
597 .s_ctrl = msp_s_ctrl,
598};
599
698static const struct v4l2_subdev_core_ops msp_core_ops = { 600static const struct v4l2_subdev_core_ops msp_core_ops = {
699 .log_status = msp_log_status, 601 .log_status = msp_log_status,
700 .g_chip_ident = msp_g_chip_ident, 602 .g_chip_ident = msp_g_chip_ident,
701 .g_ctrl = msp_g_ctrl, 603 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
702 .s_ctrl = msp_s_ctrl, 604 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
703 .queryctrl = msp_queryctrl, 605 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
606 .g_ctrl = v4l2_subdev_g_ctrl,
607 .s_ctrl = v4l2_subdev_s_ctrl,
608 .queryctrl = v4l2_subdev_queryctrl,
609 .querymenu = v4l2_subdev_querymenu,
704 .s_std = msp_s_std, 610 .s_std = msp_s_std,
705}; 611};
706 612
@@ -728,6 +634,7 @@ static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
728{ 634{
729 struct msp_state *state; 635 struct msp_state *state;
730 struct v4l2_subdev *sd; 636 struct v4l2_subdev *sd;
637 struct v4l2_ctrl_handler *hdl;
731 int (*thread_func)(void *data) = NULL; 638 int (*thread_func)(void *data) = NULL;
732 int msp_hard; 639 int msp_hard;
733 int msp_family; 640 int msp_family;
@@ -752,13 +659,7 @@ static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
752 659
753 state->v4l2_std = V4L2_STD_NTSC; 660 state->v4l2_std = V4L2_STD_NTSC;
754 state->audmode = V4L2_TUNER_MODE_STEREO; 661 state->audmode = V4L2_TUNER_MODE_STEREO;
755 state->volume = 58880; /* 0db gain */
756 state->balance = 32768; /* 0db gain */
757 state->bass = 32768;
758 state->treble = 32768;
759 state->loudness = 0;
760 state->input = -1; 662 state->input = -1;
761 state->muted = 0;
762 state->i2s_mode = 0; 663 state->i2s_mode = 0;
763 init_waitqueue_head(&state->wq); 664 init_waitqueue_head(&state->wq);
764 /* These are the reset input/output positions */ 665 /* These are the reset input/output positions */
@@ -777,8 +678,6 @@ static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
777 return -ENODEV; 678 return -ENODEV;
778 } 679 }
779 680
780 msp_set_audio(client);
781
782 msp_family = ((state->rev1 >> 4) & 0x0f) + 3; 681 msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
783 msp_product = (state->rev2 >> 8) & 0xff; 682 msp_product = (state->rev2 >> 8) & 0xff;
784 msp_prod_hi = msp_product / 10; 683 msp_prod_hi = msp_product / 10;
@@ -849,6 +748,34 @@ static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
849 state->opmode = OPMODE_MANUAL; 748 state->opmode = OPMODE_MANUAL;
850 } 749 }
851 750
751 hdl = &state->hdl;
752 v4l2_ctrl_handler_init(hdl, 6);
753 if (state->has_sound_processing) {
754 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
755 V4L2_CID_AUDIO_BASS, 0, 65535, 65535 / 100, 32768);
756 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
757 V4L2_CID_AUDIO_TREBLE, 0, 65535, 65535 / 100, 32768);
758 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
759 V4L2_CID_AUDIO_LOUDNESS, 0, 1, 1, 0);
760 }
761 state->volume = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
762 V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 58880);
763 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
764 V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
765 state->muted = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
766 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
767 sd->ctrl_handler = hdl;
768 if (hdl->error) {
769 int err = hdl->error;
770
771 v4l2_ctrl_handler_free(hdl);
772 kfree(state);
773 return err;
774 }
775
776 v4l2_ctrl_cluster(2, &state->volume);
777 v4l2_ctrl_handler_setup(hdl);
778
852 /* hello world :-) */ 779 /* hello world :-) */
853 v4l_info(client, "MSP%d4%02d%c-%c%d found @ 0x%x (%s)\n", 780 v4l_info(client, "MSP%d4%02d%c-%c%d found @ 0x%x (%s)\n",
854 msp_family, msp_product, 781 msp_family, msp_product,
@@ -903,6 +830,7 @@ static int msp_remove(struct i2c_client *client)
903 } 830 }
904 msp_reset(client); 831 msp_reset(client);
905 832
833 v4l2_ctrl_handler_free(&state->hdl);
906 kfree(state); 834 kfree(state);
907 return 0; 835 return 0;
908} 836}
diff --git a/drivers/media/video/msp3400-driver.h b/drivers/media/video/msp3400-driver.h
index d6b3e6d0eef..32a478e532f 100644
--- a/drivers/media/video/msp3400-driver.h
+++ b/drivers/media/video/msp3400-driver.h
@@ -6,6 +6,7 @@
6 6
7#include <media/msp3400.h> 7#include <media/msp3400.h>
8#include <media/v4l2-device.h> 8#include <media/v4l2-device.h>
9#include <media/v4l2-ctrls.h>
9 10
10/* ---------------------------------------------------------------------- */ 11/* ---------------------------------------------------------------------- */
11 12
@@ -51,6 +52,7 @@ extern int msp_stereo_thresh;
51 52
52struct msp_state { 53struct msp_state {
53 struct v4l2_subdev sd; 54 struct v4l2_subdev sd;
55 struct v4l2_ctrl_handler hdl;
54 int rev1, rev2; 56 int rev1, rev2;
55 int ident; 57 int ident;
56 u8 has_nicam; 58 u8 has_nicam;
@@ -87,9 +89,12 @@ struct msp_state {
87 int audmode; 89 int audmode;
88 int rxsubchans; 90 int rxsubchans;
89 91
90 int volume, muted; 92 struct {
91 int balance, loudness; 93 /* volume cluster */
92 int bass, treble; 94 struct v4l2_ctrl *volume;
95 struct v4l2_ctrl *muted;
96 };
97
93 int scan_in_progress; 98 int scan_in_progress;
94 99
95 /* thread */ 100 /* thread */
@@ -104,6 +109,11 @@ static inline struct msp_state *to_state(struct v4l2_subdev *sd)
104 return container_of(sd, struct msp_state, sd); 109 return container_of(sd, struct msp_state, sd);
105} 110}
106 111
112static inline struct msp_state *ctrl_to_state(struct v4l2_ctrl *ctrl)
113{
114 return container_of(ctrl->handler, struct msp_state, hdl);
115}
116
107/* msp3400-driver.c */ 117/* msp3400-driver.c */
108int msp_write_dem(struct i2c_client *client, int addr, int val); 118int msp_write_dem(struct i2c_client *client, int addr, int val);
109int msp_write_dsp(struct i2c_client *client, int addr, int val); 119int msp_write_dsp(struct i2c_client *client, int addr, int val);
@@ -111,7 +121,7 @@ int msp_read_dem(struct i2c_client *client, int addr);
111int msp_read_dsp(struct i2c_client *client, int addr); 121int msp_read_dsp(struct i2c_client *client, int addr);
112int msp_reset(struct i2c_client *client); 122int msp_reset(struct i2c_client *client);
113void msp_set_scart(struct i2c_client *client, int in, int out); 123void msp_set_scart(struct i2c_client *client, int in, int out);
114void msp_set_audio(struct i2c_client *client); 124void msp_update_volume(struct msp_state *state);
115int msp_sleep(struct msp_state *state, int timeout); 125int msp_sleep(struct msp_state *state, int timeout);
116 126
117/* msp3400-kthreads.c */ 127/* msp3400-kthreads.c */
diff --git a/drivers/media/video/msp3400-kthreads.c b/drivers/media/video/msp3400-kthreads.c
index d5a69c5ee5e..b376fcdee65 100644
--- a/drivers/media/video/msp3400-kthreads.c
+++ b/drivers/media/video/msp3400-kthreads.c
@@ -496,13 +496,13 @@ restart:
496 v4l_dbg(1, msp_debug, client, 496 v4l_dbg(1, msp_debug, client,
497 "thread: no carrier scan\n"); 497 "thread: no carrier scan\n");
498 state->scan_in_progress = 0; 498 state->scan_in_progress = 0;
499 msp_set_audio(client); 499 msp_update_volume(state);
500 continue; 500 continue;
501 } 501 }
502 502
503 /* mute audio */ 503 /* mute audio */
504 state->scan_in_progress = 1; 504 state->scan_in_progress = 1;
505 msp_set_audio(client); 505 msp_update_volume(state);
506 506
507 msp3400c_set_mode(client, MSP_MODE_AM_DETECT); 507 msp3400c_set_mode(client, MSP_MODE_AM_DETECT);
508 val1 = val2 = 0; 508 val1 = val2 = 0;
@@ -634,7 +634,7 @@ no_second:
634 /* unmute */ 634 /* unmute */
635 state->scan_in_progress = 0; 635 state->scan_in_progress = 0;
636 msp3400c_set_audmode(client); 636 msp3400c_set_audmode(client);
637 msp_set_audio(client); 637 msp_update_volume(state);
638 638
639 if (msp_debug) 639 if (msp_debug)
640 msp3400c_print_mode(client); 640 msp3400c_print_mode(client);
@@ -679,13 +679,13 @@ restart:
679 v4l_dbg(1, msp_debug, client, 679 v4l_dbg(1, msp_debug, client,
680 "thread: no carrier scan\n"); 680 "thread: no carrier scan\n");
681 state->scan_in_progress = 0; 681 state->scan_in_progress = 0;
682 msp_set_audio(client); 682 msp_update_volume(state);
683 continue; 683 continue;
684 } 684 }
685 685
686 /* mute audio */ 686 /* mute audio */
687 state->scan_in_progress = 1; 687 state->scan_in_progress = 1;
688 msp_set_audio(client); 688 msp_update_volume(state);
689 689
690 /* start autodetect. Note: autodetect is not supported for 690 /* start autodetect. Note: autodetect is not supported for
691 NTSC-M and radio, hence we force the standard in those 691 NTSC-M and radio, hence we force the standard in those
@@ -797,7 +797,7 @@ restart:
797 /* unmute */ 797 /* unmute */
798 msp3400c_set_audmode(client); 798 msp3400c_set_audmode(client);
799 state->scan_in_progress = 0; 799 state->scan_in_progress = 0;
800 msp_set_audio(client); 800 msp_update_volume(state);
801 801
802 /* monitor tv audio mode, the first time don't wait 802 /* monitor tv audio mode, the first time don't wait
803 so long to get a quick stereo/bilingual result */ 803 so long to get a quick stereo/bilingual result */
@@ -974,7 +974,7 @@ restart:
974 v4l_dbg(1, msp_debug, client, 974 v4l_dbg(1, msp_debug, client,
975 "thread: no carrier scan\n"); 975 "thread: no carrier scan\n");
976 state->scan_in_progress = 0; 976 state->scan_in_progress = 0;
977 msp_set_audio(client); 977 msp_update_volume(state);
978 continue; 978 continue;
979 } 979 }
980 980
@@ -1020,7 +1020,7 @@ unmute:
1020 } 1020 }
1021 1021
1022 /* unmute: dispatch sound to scart output, set scart volume */ 1022 /* unmute: dispatch sound to scart output, set scart volume */
1023 msp_set_audio(client); 1023 msp_update_volume(state);
1024 1024
1025 /* restore ACB */ 1025 /* restore ACB */
1026 if (msp_write_dsp(client, 0x13, state->acb)) 1026 if (msp_write_dsp(client, 0x13, state->acb))
diff --git a/drivers/media/video/mt9m111.c b/drivers/media/video/mt9m111.c
index 31cc3d04bcc..758a4db27d6 100644
--- a/drivers/media/video/mt9m111.c
+++ b/drivers/media/video/mt9m111.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Driver for MT9M111/MT9M112 CMOS Image Sensor from Micron 2 * Driver for MT9M111/MT9M112/MT9M131 CMOS Image Sensor from Micron/Aptina
3 * 3 *
4 * Copyright (C) 2008, Robert Jarzmik <robert.jarzmik@free.fr> 4 * Copyright (C) 2008, Robert Jarzmik <robert.jarzmik@free.fr>
5 * 5 *
@@ -19,11 +19,14 @@
19#include <media/soc_camera.h> 19#include <media/soc_camera.h>
20 20
21/* 21/*
22 * mt9m111 and mt9m112 i2c address is 0x5d or 0x48 (depending on SAddr pin) 22 * MT9M111, MT9M112 and MT9M131:
23 * i2c address is 0x48 or 0x5d (depending on SADDR pin)
23 * The platform has to define i2c_board_info and call i2c_register_board_info() 24 * The platform has to define i2c_board_info and call i2c_register_board_info()
24 */ 25 */
25 26
26/* mt9m111: Sensor register addresses */ 27/*
28 * Sensor core register addresses (0x000..0x0ff)
29 */
27#define MT9M111_CHIP_VERSION 0x000 30#define MT9M111_CHIP_VERSION 0x000
28#define MT9M111_ROW_START 0x001 31#define MT9M111_ROW_START 0x001
29#define MT9M111_COLUMN_START 0x002 32#define MT9M111_COLUMN_START 0x002
@@ -72,8 +75,9 @@
72#define MT9M111_CTXT_CTRL_LED_FLASH_EN (1 << 2) 75#define MT9M111_CTXT_CTRL_LED_FLASH_EN (1 << 2)
73#define MT9M111_CTXT_CTRL_VBLANK_SEL_B (1 << 1) 76#define MT9M111_CTXT_CTRL_VBLANK_SEL_B (1 << 1)
74#define MT9M111_CTXT_CTRL_HBLANK_SEL_B (1 << 0) 77#define MT9M111_CTXT_CTRL_HBLANK_SEL_B (1 << 0)
78
75/* 79/*
76 * mt9m111: Colorpipe register addresses (0x100..0x1ff) 80 * Colorpipe register addresses (0x100..0x1ff)
77 */ 81 */
78#define MT9M111_OPER_MODE_CTRL 0x106 82#define MT9M111_OPER_MODE_CTRL 0x106
79#define MT9M111_OUTPUT_FORMAT_CTRL 0x108 83#define MT9M111_OUTPUT_FORMAT_CTRL 0x108
@@ -109,8 +113,9 @@
109#define MT9M111_OUTFMT_SWAP_YCbCr_C_Y (1 << 1) 113#define MT9M111_OUTFMT_SWAP_YCbCr_C_Y (1 << 1)
110#define MT9M111_OUTFMT_SWAP_RGB_EVEN (1 << 1) 114#define MT9M111_OUTFMT_SWAP_RGB_EVEN (1 << 1)
111#define MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr (1 << 0) 115#define MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr (1 << 0)
116
112/* 117/*
113 * mt9m111: Camera control register addresses (0x200..0x2ff not implemented) 118 * Camera control register addresses (0x200..0x2ff not implemented)
114 */ 119 */
115 120
116#define reg_read(reg) mt9m111_reg_read(client, MT9M111_##reg) 121#define reg_read(reg) mt9m111_reg_read(client, MT9M111_##reg)
@@ -160,7 +165,8 @@ enum mt9m111_context {
160 165
161struct mt9m111 { 166struct mt9m111 {
162 struct v4l2_subdev subdev; 167 struct v4l2_subdev subdev;
163 int model; /* V4L2_IDENT_MT9M11x* codes from v4l2-chip-ident.h */ 168 int model; /* V4L2_IDENT_MT9M111 or V4L2_IDENT_MT9M112 code
169 * from v4l2-chip-ident.h */
164 enum mt9m111_context context; 170 enum mt9m111_context context;
165 struct v4l2_rect rect; 171 struct v4l2_rect rect;
166 const struct mt9m111_datafmt *fmt; 172 const struct mt9m111_datafmt *fmt;
@@ -934,7 +940,7 @@ static int mt9m111_init(struct i2c_client *client)
934 if (!ret) 940 if (!ret)
935 ret = mt9m111_set_autoexposure(client, mt9m111->autoexposure); 941 ret = mt9m111_set_autoexposure(client, mt9m111->autoexposure);
936 if (ret) 942 if (ret)
937 dev_err(&client->dev, "mt9m11x init failed: %d\n", ret); 943 dev_err(&client->dev, "mt9m111 init failed: %d\n", ret);
938 return ret; 944 return ret;
939} 945}
940 946
@@ -963,27 +969,27 @@ static int mt9m111_video_probe(struct soc_camera_device *icd,
963 mt9m111->swap_rgb_even_odd = 1; 969 mt9m111->swap_rgb_even_odd = 1;
964 mt9m111->swap_rgb_red_blue = 1; 970 mt9m111->swap_rgb_red_blue = 1;
965 971
966 ret = mt9m111_init(client);
967 if (ret)
968 goto ei2c;
969
970 data = reg_read(CHIP_VERSION); 972 data = reg_read(CHIP_VERSION);
971 973
972 switch (data) { 974 switch (data) {
973 case 0x143a: /* MT9M111 */ 975 case 0x143a: /* MT9M111 or MT9M131 */
974 mt9m111->model = V4L2_IDENT_MT9M111; 976 mt9m111->model = V4L2_IDENT_MT9M111;
977 dev_info(&client->dev,
978 "Detected a MT9M111/MT9M131 chip ID %x\n", data);
975 break; 979 break;
976 case 0x148c: /* MT9M112 */ 980 case 0x148c: /* MT9M112 */
977 mt9m111->model = V4L2_IDENT_MT9M112; 981 mt9m111->model = V4L2_IDENT_MT9M112;
982 dev_info(&client->dev, "Detected a MT9M112 chip ID %x\n", data);
978 break; 983 break;
979 default: 984 default:
980 ret = -ENODEV; 985 ret = -ENODEV;
981 dev_err(&client->dev, 986 dev_err(&client->dev,
982 "No MT9M11x chip detected, register read %x\n", data); 987 "No MT9M111/MT9M112/MT9M131 chip detected register read %x\n",
988 data);
983 goto ei2c; 989 goto ei2c;
984 } 990 }
985 991
986 dev_info(&client->dev, "Detected a MT9M11x chip ID %x\n", data); 992 ret = mt9m111_init(client);
987 993
988ei2c: 994ei2c:
989 return ret; 995 return ret;
@@ -1034,13 +1040,13 @@ static int mt9m111_probe(struct i2c_client *client,
1034 int ret; 1040 int ret;
1035 1041
1036 if (!icd) { 1042 if (!icd) {
1037 dev_err(&client->dev, "MT9M11x: missing soc-camera data!\n"); 1043 dev_err(&client->dev, "mt9m111: soc-camera data missing!\n");
1038 return -EINVAL; 1044 return -EINVAL;
1039 } 1045 }
1040 1046
1041 icl = to_soc_camera_link(icd); 1047 icl = to_soc_camera_link(icd);
1042 if (!icl) { 1048 if (!icl) {
1043 dev_err(&client->dev, "MT9M11x driver needs platform data\n"); 1049 dev_err(&client->dev, "mt9m111: driver needs platform data\n");
1044 return -EINVAL; 1050 return -EINVAL;
1045 } 1051 }
1046 1052
@@ -1114,6 +1120,6 @@ static void __exit mt9m111_mod_exit(void)
1114module_init(mt9m111_mod_init); 1120module_init(mt9m111_mod_init);
1115module_exit(mt9m111_mod_exit); 1121module_exit(mt9m111_mod_exit);
1116 1122
1117MODULE_DESCRIPTION("Micron MT9M111/MT9M112 Camera driver"); 1123MODULE_DESCRIPTION("Micron/Aptina MT9M111/MT9M112/MT9M131 Camera driver");
1118MODULE_AUTHOR("Robert Jarzmik"); 1124MODULE_AUTHOR("Robert Jarzmik");
1119MODULE_LICENSE("GPL"); 1125MODULE_LICENSE("GPL");
diff --git a/drivers/media/video/mx2_camera.c b/drivers/media/video/mx2_camera.c
index 026bef0ba40..66ff174151b 100644
--- a/drivers/media/video/mx2_camera.c
+++ b/drivers/media/video/mx2_camera.c
@@ -785,6 +785,8 @@ static int mx2_camera_set_bus_param(struct soc_camera_device *icd,
785 if (ret < 0) 785 if (ret < 0)
786 return ret; 786 return ret;
787 787
788 if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
789 csicr1 |= CSICR1_REDGE;
788 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING) 790 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
789 csicr1 |= CSICR1_INV_PCLK; 791 csicr1 |= CSICR1_INV_PCLK;
790 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH) 792 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
@@ -1201,7 +1203,7 @@ static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
1201 buf = list_entry(pcdev->capture.next, 1203 buf = list_entry(pcdev->capture.next,
1202 struct mx2_buffer, vb.queue); 1204 struct mx2_buffer, vb.queue);
1203 1205
1204 buf->bufnum = bufnum; 1206 buf->bufnum = !bufnum;
1205 1207
1206 list_move_tail(pcdev->capture.next, &pcdev->active_bufs); 1208 list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
1207 1209
diff --git a/drivers/media/video/pvrusb2/pvrusb2-debugifc.c b/drivers/media/video/pvrusb2/pvrusb2-debugifc.c
index e9b11e119f6..4279ebb811a 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-debugifc.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-debugifc.c
@@ -94,8 +94,6 @@ static int debugifc_parse_unsigned_number(const char *buf,unsigned int count,
94 u32 *num_ptr) 94 u32 *num_ptr)
95{ 95{
96 u32 result = 0; 96 u32 result = 0;
97 u32 val;
98 int ch;
99 int radix = 10; 97 int radix = 10;
100 if ((count >= 2) && (buf[0] == '0') && 98 if ((count >= 2) && (buf[0] == '0') &&
101 ((buf[1] == 'x') || (buf[1] == 'X'))) { 99 ((buf[1] == 'x') || (buf[1] == 'X'))) {
@@ -107,17 +105,9 @@ static int debugifc_parse_unsigned_number(const char *buf,unsigned int count,
107 } 105 }
108 106
109 while (count--) { 107 while (count--) {
110 ch = *buf++; 108 int val = hex_to_bin(*buf++);
111 if ((ch >= '0') && (ch <= '9')) { 109 if (val < 0 || val >= radix)
112 val = ch - '0';
113 } else if ((ch >= 'a') && (ch <= 'f')) {
114 val = ch - 'a' + 10;
115 } else if ((ch >= 'A') && (ch <= 'F')) {
116 val = ch - 'A' + 10;
117 } else {
118 return -EINVAL; 110 return -EINVAL;
119 }
120 if (val >= radix) return -EINVAL;
121 result *= radix; 111 result *= radix;
122 result += val; 112 result += val;
123 } 113 }
diff --git a/drivers/media/video/s5p-fimc/Makefile b/drivers/media/video/s5p-fimc/Makefile
new file mode 100644
index 00000000000..0d9d54132ec
--- /dev/null
+++ b/drivers/media/video/s5p-fimc/Makefile
@@ -0,0 +1,3 @@
1
2obj-$(CONFIG_VIDEO_SAMSUNG_S5P_FIMC) := s5p-fimc.o
3s5p-fimc-y := fimc-core.o fimc-reg.o
diff --git a/drivers/media/video/s5p-fimc/fimc-core.c b/drivers/media/video/s5p-fimc/fimc-core.c
new file mode 100644
index 00000000000..b151c7be8a5
--- /dev/null
+++ b/drivers/media/video/s5p-fimc/fimc-core.c
@@ -0,0 +1,1586 @@
1/*
2 * S5P camera interface (video postprocessor) driver
3 *
4 * Copyright (c) 2010 Samsung Electronics
5 *
6 * Sylwester Nawrocki, <s.nawrocki@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation, either version 2 of the License,
11 * or (at your option) any later version.
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/version.h>
17#include <linux/types.h>
18#include <linux/errno.h>
19#include <linux/bug.h>
20#include <linux/interrupt.h>
21#include <linux/device.h>
22#include <linux/platform_device.h>
23#include <linux/list.h>
24#include <linux/io.h>
25#include <linux/slab.h>
26#include <linux/clk.h>
27#include <media/v4l2-ioctl.h>
28#include <media/videobuf-dma-contig.h>
29
30#include "fimc-core.h"
31
32static char *fimc_clock_name[NUM_FIMC_CLOCKS] = { "sclk_fimc", "fimc" };
33
34static struct fimc_fmt fimc_formats[] = {
35 {
36 .name = "RGB565",
37 .fourcc = V4L2_PIX_FMT_RGB565X,
38 .depth = 16,
39 .color = S5P_FIMC_RGB565,
40 .buff_cnt = 1,
41 .planes_cnt = 1
42 }, {
43 .name = "BGR666",
44 .fourcc = V4L2_PIX_FMT_BGR666,
45 .depth = 32,
46 .color = S5P_FIMC_RGB666,
47 .buff_cnt = 1,
48 .planes_cnt = 1
49 }, {
50 .name = "XRGB-8-8-8-8, 24 bpp",
51 .fourcc = V4L2_PIX_FMT_RGB24,
52 .depth = 32,
53 .color = S5P_FIMC_RGB888,
54 .buff_cnt = 1,
55 .planes_cnt = 1
56 }, {
57 .name = "YUV 4:2:2 packed, YCbYCr",
58 .fourcc = V4L2_PIX_FMT_YUYV,
59 .depth = 16,
60 .color = S5P_FIMC_YCBYCR422,
61 .buff_cnt = 1,
62 .planes_cnt = 1
63 }, {
64 .name = "YUV 4:2:2 packed, CbYCrY",
65 .fourcc = V4L2_PIX_FMT_UYVY,
66 .depth = 16,
67 .color = S5P_FIMC_CBYCRY422,
68 .buff_cnt = 1,
69 .planes_cnt = 1
70 }, {
71 .name = "YUV 4:2:2 packed, CrYCbY",
72 .fourcc = V4L2_PIX_FMT_VYUY,
73 .depth = 16,
74 .color = S5P_FIMC_CRYCBY422,
75 .buff_cnt = 1,
76 .planes_cnt = 1
77 }, {
78 .name = "YUV 4:2:2 packed, YCrYCb",
79 .fourcc = V4L2_PIX_FMT_YVYU,
80 .depth = 16,
81 .color = S5P_FIMC_YCRYCB422,
82 .buff_cnt = 1,
83 .planes_cnt = 1
84 }, {
85 .name = "YUV 4:2:2 planar, Y/Cb/Cr",
86 .fourcc = V4L2_PIX_FMT_YUV422P,
87 .depth = 12,
88 .color = S5P_FIMC_YCBCR422,
89 .buff_cnt = 1,
90 .planes_cnt = 3
91 }, {
92 .name = "YUV 4:2:2 planar, Y/CbCr",
93 .fourcc = V4L2_PIX_FMT_NV16,
94 .depth = 16,
95 .color = S5P_FIMC_YCBCR422,
96 .buff_cnt = 1,
97 .planes_cnt = 2
98 }, {
99 .name = "YUV 4:2:2 planar, Y/CrCb",
100 .fourcc = V4L2_PIX_FMT_NV61,
101 .depth = 16,
102 .color = S5P_FIMC_RGB565,
103 .buff_cnt = 1,
104 .planes_cnt = 2
105 }, {
106 .name = "YUV 4:2:0 planar, YCbCr",
107 .fourcc = V4L2_PIX_FMT_YUV420,
108 .depth = 12,
109 .color = S5P_FIMC_YCBCR420,
110 .buff_cnt = 1,
111 .planes_cnt = 3
112 }, {
113 .name = "YUV 4:2:0 planar, Y/CbCr",
114 .fourcc = V4L2_PIX_FMT_NV12,
115 .depth = 12,
116 .color = S5P_FIMC_YCBCR420,
117 .buff_cnt = 1,
118 .planes_cnt = 2
119 }
120 };
121
122static struct v4l2_queryctrl fimc_ctrls[] = {
123 {
124 .id = V4L2_CID_HFLIP,
125 .type = V4L2_CTRL_TYPE_BOOLEAN,
126 .name = "Horizontal flip",
127 .minimum = 0,
128 .maximum = 1,
129 .default_value = 0,
130 },
131 {
132 .id = V4L2_CID_VFLIP,
133 .type = V4L2_CTRL_TYPE_BOOLEAN,
134 .name = "Vertical flip",
135 .minimum = 0,
136 .maximum = 1,
137 .default_value = 0,
138 },
139 {
140 .id = V4L2_CID_ROTATE,
141 .type = V4L2_CTRL_TYPE_INTEGER,
142 .name = "Rotation (CCW)",
143 .minimum = 0,
144 .maximum = 270,
145 .step = 90,
146 .default_value = 0,
147 },
148};
149
150
151static struct v4l2_queryctrl *get_ctrl(int id)
152{
153 int i;
154
155 for (i = 0; i < ARRAY_SIZE(fimc_ctrls); ++i)
156 if (id == fimc_ctrls[i].id)
157 return &fimc_ctrls[i];
158 return NULL;
159}
160
161static int fimc_check_scaler_ratio(struct v4l2_rect *r, struct fimc_frame *f)
162{
163 if (r->width > f->width) {
164 if (f->width > (r->width * SCALER_MAX_HRATIO))
165 return -EINVAL;
166 } else {
167 if ((f->width * SCALER_MAX_HRATIO) < r->width)
168 return -EINVAL;
169 }
170
171 if (r->height > f->height) {
172 if (f->height > (r->height * SCALER_MAX_VRATIO))
173 return -EINVAL;
174 } else {
175 if ((f->height * SCALER_MAX_VRATIO) < r->height)
176 return -EINVAL;
177 }
178
179 return 0;
180}
181
182static int fimc_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
183{
184 if (src >= tar * 64) {
185 return -EINVAL;
186 } else if (src >= tar * 32) {
187 *ratio = 32;
188 *shift = 5;
189 } else if (src >= tar * 16) {
190 *ratio = 16;
191 *shift = 4;
192 } else if (src >= tar * 8) {
193 *ratio = 8;
194 *shift = 3;
195 } else if (src >= tar * 4) {
196 *ratio = 4;
197 *shift = 2;
198 } else if (src >= tar * 2) {
199 *ratio = 2;
200 *shift = 1;
201 } else {
202 *ratio = 1;
203 *shift = 0;
204 }
205
206 return 0;
207}
208
209static int fimc_set_scaler_info(struct fimc_ctx *ctx)
210{
211 struct fimc_scaler *sc = &ctx->scaler;
212 struct fimc_frame *s_frame = &ctx->s_frame;
213 struct fimc_frame *d_frame = &ctx->d_frame;
214 int tx, ty, sx, sy;
215 int ret;
216
217 tx = d_frame->width;
218 ty = d_frame->height;
219 if (tx <= 0 || ty <= 0) {
220 v4l2_err(&ctx->fimc_dev->m2m.v4l2_dev,
221 "invalid target size: %d x %d", tx, ty);
222 return -EINVAL;
223 }
224
225 sx = s_frame->width;
226 sy = s_frame->height;
227 if (sx <= 0 || sy <= 0) {
228 err("invalid source size: %d x %d", sx, sy);
229 return -EINVAL;
230 }
231
232 sc->real_width = sx;
233 sc->real_height = sy;
234 dbg("sx= %d, sy= %d, tx= %d, ty= %d", sx, sy, tx, ty);
235
236 ret = fimc_get_scaler_factor(sx, tx, &sc->pre_hratio, &sc->hfactor);
237 if (ret)
238 return ret;
239
240 ret = fimc_get_scaler_factor(sy, ty, &sc->pre_vratio, &sc->vfactor);
241 if (ret)
242 return ret;
243
244 sc->pre_dst_width = sx / sc->pre_hratio;
245 sc->pre_dst_height = sy / sc->pre_vratio;
246
247 sc->main_hratio = (sx << 8) / (tx << sc->hfactor);
248 sc->main_vratio = (sy << 8) / (ty << sc->vfactor);
249
250 sc->scaleup_h = (tx >= sx) ? 1 : 0;
251 sc->scaleup_v = (ty >= sy) ? 1 : 0;
252
253 /* check to see if input and output size/format differ */
254 if (s_frame->fmt->color == d_frame->fmt->color
255 && s_frame->width == d_frame->width
256 && s_frame->height == d_frame->height)
257 sc->copy_mode = 1;
258 else
259 sc->copy_mode = 0;
260
261 return 0;
262}
263
264
265static irqreturn_t fimc_isr(int irq, void *priv)
266{
267 struct fimc_vid_buffer *src_buf, *dst_buf;
268 struct fimc_dev *fimc = (struct fimc_dev *)priv;
269 struct fimc_ctx *ctx;
270
271 BUG_ON(!fimc);
272 fimc_hw_clear_irq(fimc);
273
274 spin_lock(&fimc->slock);
275
276 if (test_and_clear_bit(ST_M2M_PEND, &fimc->state)) {
277 ctx = v4l2_m2m_get_curr_priv(fimc->m2m.m2m_dev);
278 if (!ctx || !ctx->m2m_ctx)
279 goto isr_unlock;
280 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
281 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
282 if (src_buf && dst_buf) {
283 spin_lock(&fimc->irqlock);
284 src_buf->vb.state = dst_buf->vb.state = VIDEOBUF_DONE;
285 wake_up(&src_buf->vb.done);
286 wake_up(&dst_buf->vb.done);
287 spin_unlock(&fimc->irqlock);
288 v4l2_m2m_job_finish(fimc->m2m.m2m_dev, ctx->m2m_ctx);
289 }
290 }
291
292isr_unlock:
293 spin_unlock(&fimc->slock);
294 return IRQ_HANDLED;
295}
296
297/* The color format (planes_cnt, buff_cnt) must be already configured. */
298static int fimc_prepare_addr(struct fimc_ctx *ctx,
299 struct fimc_vid_buffer *buf, enum v4l2_buf_type type)
300{
301 struct fimc_frame *frame;
302 struct fimc_addr *paddr;
303 u32 pix_size;
304 int ret = 0;
305
306 frame = ctx_m2m_get_frame(ctx, type);
307 if (IS_ERR(frame))
308 return PTR_ERR(frame);
309 paddr = &frame->paddr;
310
311 if (!buf)
312 return -EINVAL;
313
314 pix_size = frame->width * frame->height;
315
316 dbg("buff_cnt= %d, planes_cnt= %d, frame->size= %d, pix_size= %d",
317 frame->fmt->buff_cnt, frame->fmt->planes_cnt,
318 frame->size, pix_size);
319
320 if (frame->fmt->buff_cnt == 1) {
321 paddr->y = videobuf_to_dma_contig(&buf->vb);
322 switch (frame->fmt->planes_cnt) {
323 case 1:
324 paddr->cb = 0;
325 paddr->cr = 0;
326 break;
327 case 2:
328 /* decompose Y into Y/Cb */
329 paddr->cb = (u32)(paddr->y + pix_size);
330 paddr->cr = 0;
331 break;
332 case 3:
333 paddr->cb = (u32)(paddr->y + pix_size);
334 /* decompose Y into Y/Cb/Cr */
335 if (S5P_FIMC_YCBCR420 == frame->fmt->color)
336 paddr->cr = (u32)(paddr->cb
337 + (pix_size >> 2));
338 else /* 422 */
339 paddr->cr = (u32)(paddr->cb
340 + (pix_size >> 1));
341 break;
342 default:
343 return -EINVAL;
344 }
345 }
346
347 dbg("PHYS_ADDR: type= %d, y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
348 type, paddr->y, paddr->cb, paddr->cr, ret);
349
350 return ret;
351}
352
353/* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
354static void fimc_set_yuv_order(struct fimc_ctx *ctx)
355{
356 /* The one only mode supported in SoC. */
357 ctx->in_order_2p = S5P_FIMC_LSB_CRCB;
358 ctx->out_order_2p = S5P_FIMC_LSB_CRCB;
359
360 /* Set order for 1 plane input formats. */
361 switch (ctx->s_frame.fmt->color) {
362 case S5P_FIMC_YCRYCB422:
363 ctx->in_order_1p = S5P_FIMC_IN_YCRYCB;
364 break;
365 case S5P_FIMC_CBYCRY422:
366 ctx->in_order_1p = S5P_FIMC_IN_CBYCRY;
367 break;
368 case S5P_FIMC_CRYCBY422:
369 ctx->in_order_1p = S5P_FIMC_IN_CRYCBY;
370 break;
371 case S5P_FIMC_YCBYCR422:
372 default:
373 ctx->in_order_1p = S5P_FIMC_IN_YCBYCR;
374 break;
375 }
376 dbg("ctx->in_order_1p= %d", ctx->in_order_1p);
377
378 switch (ctx->d_frame.fmt->color) {
379 case S5P_FIMC_YCRYCB422:
380 ctx->out_order_1p = S5P_FIMC_OUT_YCRYCB;
381 break;
382 case S5P_FIMC_CBYCRY422:
383 ctx->out_order_1p = S5P_FIMC_OUT_CBYCRY;
384 break;
385 case S5P_FIMC_CRYCBY422:
386 ctx->out_order_1p = S5P_FIMC_OUT_CRYCBY;
387 break;
388 case S5P_FIMC_YCBYCR422:
389 default:
390 ctx->out_order_1p = S5P_FIMC_OUT_YCBYCR;
391 break;
392 }
393 dbg("ctx->out_order_1p= %d", ctx->out_order_1p);
394}
395
396/**
397 * fimc_prepare_config - check dimensions, operation and color mode
398 * and pre-calculate offset and the scaling coefficients.
399 *
400 * @ctx: hardware context information
401 * @flags: flags indicating which parameters to check/update
402 *
403 * Return: 0 if dimensions are valid or non zero otherwise.
404 */
405static int fimc_prepare_config(struct fimc_ctx *ctx, u32 flags)
406{
407 struct fimc_frame *s_frame, *d_frame;
408 struct fimc_vid_buffer *buf = NULL;
409 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
410 int ret = 0;
411
412 s_frame = &ctx->s_frame;
413 d_frame = &ctx->d_frame;
414
415 if (flags & FIMC_PARAMS) {
416 if ((ctx->out_path == FIMC_DMA) &&
417 (ctx->rotation == 90 || ctx->rotation == 270)) {
418 swap(d_frame->f_width, d_frame->f_height);
419 swap(d_frame->width, d_frame->height);
420 }
421
422 /* Prepare the output offset ratios for scaler. */
423 d_frame->dma_offset.y_h = d_frame->offs_h;
424 if (!variant->pix_hoff)
425 d_frame->dma_offset.y_h *= (d_frame->fmt->depth >> 3);
426
427 d_frame->dma_offset.y_v = d_frame->offs_v;
428
429 d_frame->dma_offset.cb_h = d_frame->offs_h;
430 d_frame->dma_offset.cb_v = d_frame->offs_v;
431
432 d_frame->dma_offset.cr_h = d_frame->offs_h;
433 d_frame->dma_offset.cr_v = d_frame->offs_v;
434
435 if (!variant->pix_hoff && d_frame->fmt->planes_cnt == 3) {
436 d_frame->dma_offset.cb_h >>= 1;
437 d_frame->dma_offset.cb_v >>= 1;
438 d_frame->dma_offset.cr_h >>= 1;
439 d_frame->dma_offset.cr_v >>= 1;
440 }
441
442 dbg("out offset: color= %d, y_h= %d, y_v= %d",
443 d_frame->fmt->color,
444 d_frame->dma_offset.y_h, d_frame->dma_offset.y_v);
445
446 /* Prepare the input offset ratios for scaler. */
447 s_frame->dma_offset.y_h = s_frame->offs_h;
448 if (!variant->pix_hoff)
449 s_frame->dma_offset.y_h *= (s_frame->fmt->depth >> 3);
450 s_frame->dma_offset.y_v = s_frame->offs_v;
451
452 s_frame->dma_offset.cb_h = s_frame->offs_h;
453 s_frame->dma_offset.cb_v = s_frame->offs_v;
454
455 s_frame->dma_offset.cr_h = s_frame->offs_h;
456 s_frame->dma_offset.cr_v = s_frame->offs_v;
457
458 if (!variant->pix_hoff && s_frame->fmt->planes_cnt == 3) {
459 s_frame->dma_offset.cb_h >>= 1;
460 s_frame->dma_offset.cb_v >>= 1;
461 s_frame->dma_offset.cr_h >>= 1;
462 s_frame->dma_offset.cr_v >>= 1;
463 }
464
465 dbg("in offset: color= %d, y_h= %d, y_v= %d",
466 s_frame->fmt->color, s_frame->dma_offset.y_h,
467 s_frame->dma_offset.y_v);
468
469 fimc_set_yuv_order(ctx);
470
471 /* Check against the scaler ratio. */
472 if (s_frame->height > (SCALER_MAX_VRATIO * d_frame->height) ||
473 s_frame->width > (SCALER_MAX_HRATIO * d_frame->width)) {
474 err("out of scaler range");
475 return -EINVAL;
476 }
477 }
478
479 /* Input DMA mode is not allowed when the scaler is disabled. */
480 ctx->scaler.enabled = 1;
481
482 if (flags & FIMC_SRC_ADDR) {
483 buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
484 ret = fimc_prepare_addr(ctx, buf,
485 V4L2_BUF_TYPE_VIDEO_OUTPUT);
486 if (ret)
487 return ret;
488 }
489
490 if (flags & FIMC_DST_ADDR) {
491 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
492 ret = fimc_prepare_addr(ctx, buf,
493 V4L2_BUF_TYPE_VIDEO_CAPTURE);
494 }
495
496 return ret;
497}
498
499static void fimc_dma_run(void *priv)
500{
501 struct fimc_ctx *ctx = priv;
502 struct fimc_dev *fimc;
503 unsigned long flags;
504 u32 ret;
505
506 if (WARN(!ctx, "null hardware context"))
507 return;
508
509 fimc = ctx->fimc_dev;
510
511 spin_lock_irqsave(&ctx->slock, flags);
512 set_bit(ST_M2M_PEND, &fimc->state);
513
514 ctx->state |= (FIMC_SRC_ADDR | FIMC_DST_ADDR);
515 ret = fimc_prepare_config(ctx, ctx->state);
516 if (ret) {
517 err("general configuration error");
518 goto dma_unlock;
519 }
520
521 if (fimc->m2m.ctx != ctx)
522 ctx->state |= FIMC_PARAMS;
523
524 fimc_hw_set_input_addr(fimc, &ctx->s_frame.paddr);
525
526 if (ctx->state & FIMC_PARAMS) {
527 fimc_hw_set_input_path(ctx);
528 fimc_hw_set_in_dma(ctx);
529 if (fimc_set_scaler_info(ctx)) {
530 err("scaler configuration error");
531 goto dma_unlock;
532 }
533 fimc_hw_set_prescaler(ctx);
534 fimc_hw_set_scaler(ctx);
535 fimc_hw_set_target_format(ctx);
536 fimc_hw_set_rotation(ctx);
537 fimc_hw_set_effect(ctx);
538 }
539
540 fimc_hw_set_output_path(ctx);
541 if (ctx->state & (FIMC_DST_ADDR | FIMC_PARAMS))
542 fimc_hw_set_output_addr(fimc, &ctx->d_frame.paddr);
543
544 if (ctx->state & FIMC_PARAMS)
545 fimc_hw_set_out_dma(ctx);
546
547 if (ctx->scaler.enabled)
548 fimc_hw_start_scaler(fimc);
549 fimc_hw_en_capture(ctx);
550
551 ctx->state = 0;
552 fimc_hw_start_in_dma(fimc);
553
554 fimc->m2m.ctx = ctx;
555
556dma_unlock:
557 spin_unlock_irqrestore(&ctx->slock, flags);
558}
559
560static void fimc_job_abort(void *priv)
561{
562 /* Nothing done in job_abort. */
563}
564
565static void fimc_buf_release(struct videobuf_queue *vq,
566 struct videobuf_buffer *vb)
567{
568 videobuf_dma_contig_free(vq, vb);
569 vb->state = VIDEOBUF_NEEDS_INIT;
570}
571
572static int fimc_buf_setup(struct videobuf_queue *vq, unsigned int *count,
573 unsigned int *size)
574{
575 struct fimc_ctx *ctx = vq->priv_data;
576 struct fimc_frame *frame;
577
578 frame = ctx_m2m_get_frame(ctx, vq->type);
579 if (IS_ERR(frame))
580 return PTR_ERR(frame);
581
582 *size = (frame->width * frame->height * frame->fmt->depth) >> 3;
583 if (0 == *count)
584 *count = 1;
585 return 0;
586}
587
588static int fimc_buf_prepare(struct videobuf_queue *vq,
589 struct videobuf_buffer *vb, enum v4l2_field field)
590{
591 struct fimc_ctx *ctx = vq->priv_data;
592 struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev;
593 struct fimc_frame *frame;
594 int ret;
595
596 frame = ctx_m2m_get_frame(ctx, vq->type);
597 if (IS_ERR(frame))
598 return PTR_ERR(frame);
599
600 if (vb->baddr) {
601 if (vb->bsize < frame->size) {
602 v4l2_err(v4l2_dev,
603 "User-provided buffer too small (%d < %d)\n",
604 vb->bsize, frame->size);
605 WARN_ON(1);
606 return -EINVAL;
607 }
608 } else if (vb->state != VIDEOBUF_NEEDS_INIT
609 && vb->bsize < frame->size) {
610 return -EINVAL;
611 }
612
613 vb->width = frame->width;
614 vb->height = frame->height;
615 vb->bytesperline = (frame->width * frame->fmt->depth) >> 3;
616 vb->size = frame->size;
617 vb->field = field;
618
619 if (VIDEOBUF_NEEDS_INIT == vb->state) {
620 ret = videobuf_iolock(vq, vb, NULL);
621 if (ret) {
622 v4l2_err(v4l2_dev, "Iolock failed\n");
623 fimc_buf_release(vq, vb);
624 return ret;
625 }
626 }
627 vb->state = VIDEOBUF_PREPARED;
628
629 return 0;
630}
631
632static void fimc_buf_queue(struct videobuf_queue *vq,
633 struct videobuf_buffer *vb)
634{
635 struct fimc_ctx *ctx = vq->priv_data;
636 v4l2_m2m_buf_queue(ctx->m2m_ctx, vq, vb);
637}
638
639static struct videobuf_queue_ops fimc_qops = {
640 .buf_setup = fimc_buf_setup,
641 .buf_prepare = fimc_buf_prepare,
642 .buf_queue = fimc_buf_queue,
643 .buf_release = fimc_buf_release,
644};
645
646static int fimc_m2m_querycap(struct file *file, void *priv,
647 struct v4l2_capability *cap)
648{
649 struct fimc_ctx *ctx = file->private_data;
650 struct fimc_dev *fimc = ctx->fimc_dev;
651
652 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
653 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
654 cap->bus_info[0] = 0;
655 cap->version = KERNEL_VERSION(1, 0, 0);
656 cap->capabilities = V4L2_CAP_STREAMING |
657 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
658
659 return 0;
660}
661
662static int fimc_m2m_enum_fmt(struct file *file, void *priv,
663 struct v4l2_fmtdesc *f)
664{
665 struct fimc_fmt *fmt;
666
667 if (f->index >= ARRAY_SIZE(fimc_formats))
668 return -EINVAL;
669
670 fmt = &fimc_formats[f->index];
671 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
672 f->pixelformat = fmt->fourcc;
673 return 0;
674}
675
676static int fimc_m2m_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
677{
678 struct fimc_ctx *ctx = priv;
679 struct fimc_frame *frame;
680
681 frame = ctx_m2m_get_frame(ctx, f->type);
682 if (IS_ERR(frame))
683 return PTR_ERR(frame);
684
685 f->fmt.pix.width = frame->width;
686 f->fmt.pix.height = frame->height;
687 f->fmt.pix.field = V4L2_FIELD_NONE;
688 f->fmt.pix.pixelformat = frame->fmt->fourcc;
689
690 return 0;
691}
692
693static struct fimc_fmt *find_format(struct v4l2_format *f)
694{
695 struct fimc_fmt *fmt;
696 unsigned int i;
697
698 for (i = 0; i < ARRAY_SIZE(fimc_formats); ++i) {
699 fmt = &fimc_formats[i];
700 if (fmt->fourcc == f->fmt.pix.pixelformat)
701 break;
702 }
703 if (i == ARRAY_SIZE(fimc_formats))
704 return NULL;
705
706 return fmt;
707}
708
709static int fimc_m2m_try_fmt(struct file *file, void *priv,
710 struct v4l2_format *f)
711{
712 struct fimc_fmt *fmt;
713 u32 max_width, max_height, mod_x, mod_y;
714 struct fimc_ctx *ctx = priv;
715 struct fimc_dev *fimc = ctx->fimc_dev;
716 struct v4l2_pix_format *pix = &f->fmt.pix;
717 struct samsung_fimc_variant *variant = fimc->variant;
718
719 fmt = find_format(f);
720 if (!fmt) {
721 v4l2_err(&fimc->m2m.v4l2_dev,
722 "Fourcc format (0x%X) invalid.\n", pix->pixelformat);
723 return -EINVAL;
724 }
725
726 if (pix->field == V4L2_FIELD_ANY)
727 pix->field = V4L2_FIELD_NONE;
728 else if (V4L2_FIELD_NONE != pix->field)
729 return -EINVAL;
730
731 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
732 max_width = variant->scaler_dis_w;
733 max_height = variant->scaler_dis_w;
734 mod_x = variant->min_inp_pixsize;
735 mod_y = variant->min_inp_pixsize;
736 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
737 max_width = variant->out_rot_dis_w;
738 max_height = variant->out_rot_dis_w;
739 mod_x = variant->min_out_pixsize;
740 mod_y = variant->min_out_pixsize;
741 } else {
742 err("Wrong stream type (%d)", f->type);
743 return -EINVAL;
744 }
745
746 dbg("max_w= %d, max_h= %d", max_width, max_height);
747
748 if (pix->height > max_height)
749 pix->height = max_height;
750 if (pix->width > max_width)
751 pix->width = max_width;
752
753 if (tiled_fmt(fmt)) {
754 mod_x = 64; /* 64x32 tile */
755 mod_y = 32;
756 }
757
758 dbg("mod_x= 0x%X, mod_y= 0x%X", mod_x, mod_y);
759
760 pix->width = (pix->width == 0) ? mod_x : ALIGN(pix->width, mod_x);
761 pix->height = (pix->height == 0) ? mod_y : ALIGN(pix->height, mod_y);
762
763 if (pix->bytesperline == 0 ||
764 pix->bytesperline * 8 / fmt->depth > pix->width)
765 pix->bytesperline = (pix->width * fmt->depth) >> 3;
766
767 if (pix->sizeimage == 0)
768 pix->sizeimage = pix->height * pix->bytesperline;
769
770 dbg("pix->bytesperline= %d, fmt->depth= %d",
771 pix->bytesperline, fmt->depth);
772
773 return 0;
774}
775
776
777static int fimc_m2m_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
778{
779 struct fimc_ctx *ctx = priv;
780 struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev;
781 struct videobuf_queue *src_vq, *dst_vq;
782 struct fimc_frame *frame;
783 struct v4l2_pix_format *pix;
784 unsigned long flags;
785 int ret = 0;
786
787 BUG_ON(!ctx);
788
789 ret = fimc_m2m_try_fmt(file, priv, f);
790 if (ret)
791 return ret;
792
793 mutex_lock(&ctx->fimc_dev->lock);
794
795 src_vq = v4l2_m2m_get_src_vq(ctx->m2m_ctx);
796 dst_vq = v4l2_m2m_get_dst_vq(ctx->m2m_ctx);
797
798 mutex_lock(&src_vq->vb_lock);
799 mutex_lock(&dst_vq->vb_lock);
800
801 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
802 if (videobuf_queue_is_busy(src_vq)) {
803 v4l2_err(v4l2_dev, "%s queue busy\n", __func__);
804 ret = -EBUSY;
805 goto s_fmt_out;
806 }
807 frame = &ctx->s_frame;
808 spin_lock_irqsave(&ctx->slock, flags);
809 ctx->state |= FIMC_SRC_FMT;
810 spin_unlock_irqrestore(&ctx->slock, flags);
811
812 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
813 if (videobuf_queue_is_busy(dst_vq)) {
814 v4l2_err(v4l2_dev, "%s queue busy\n", __func__);
815 ret = -EBUSY;
816 goto s_fmt_out;
817 }
818 frame = &ctx->d_frame;
819 spin_lock_irqsave(&ctx->slock, flags);
820 ctx->state |= FIMC_DST_FMT;
821 spin_unlock_irqrestore(&ctx->slock, flags);
822 } else {
823 v4l2_err(&ctx->fimc_dev->m2m.v4l2_dev,
824 "Wrong buffer/video queue type (%d)\n", f->type);
825 return -EINVAL;
826 }
827
828 pix = &f->fmt.pix;
829 frame->fmt = find_format(f);
830 if (!frame->fmt) {
831 ret = -EINVAL;
832 goto s_fmt_out;
833 }
834
835 frame->f_width = pix->bytesperline * 8 / frame->fmt->depth;
836 frame->f_height = pix->sizeimage/pix->bytesperline;
837 frame->width = pix->width;
838 frame->height = pix->height;
839 frame->o_width = pix->width;
840 frame->o_height = pix->height;
841 frame->offs_h = 0;
842 frame->offs_v = 0;
843 frame->size = (pix->width * pix->height * frame->fmt->depth) >> 3;
844 src_vq->field = dst_vq->field = pix->field;
845 spin_lock_irqsave(&ctx->slock, flags);
846 ctx->state |= FIMC_PARAMS;
847 spin_unlock_irqrestore(&ctx->slock, flags);
848
849 dbg("f_width= %d, f_height= %d", frame->f_width, frame->f_height);
850
851s_fmt_out:
852 mutex_unlock(&dst_vq->vb_lock);
853 mutex_unlock(&src_vq->vb_lock);
854 mutex_unlock(&ctx->fimc_dev->lock);
855 return ret;
856}
857
858static int fimc_m2m_reqbufs(struct file *file, void *priv,
859 struct v4l2_requestbuffers *reqbufs)
860{
861 struct fimc_ctx *ctx = priv;
862 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
863}
864
865static int fimc_m2m_querybuf(struct file *file, void *priv,
866 struct v4l2_buffer *buf)
867{
868 struct fimc_ctx *ctx = priv;
869 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
870}
871
872static int fimc_m2m_qbuf(struct file *file, void *priv,
873 struct v4l2_buffer *buf)
874{
875 struct fimc_ctx *ctx = priv;
876
877 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
878}
879
880static int fimc_m2m_dqbuf(struct file *file, void *priv,
881 struct v4l2_buffer *buf)
882{
883 struct fimc_ctx *ctx = priv;
884 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
885}
886
887static int fimc_m2m_streamon(struct file *file, void *priv,
888 enum v4l2_buf_type type)
889{
890 struct fimc_ctx *ctx = priv;
891 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
892}
893
894static int fimc_m2m_streamoff(struct file *file, void *priv,
895 enum v4l2_buf_type type)
896{
897 struct fimc_ctx *ctx = priv;
898 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
899}
900
901int fimc_m2m_queryctrl(struct file *file, void *priv,
902 struct v4l2_queryctrl *qc)
903{
904 struct v4l2_queryctrl *c;
905 c = get_ctrl(qc->id);
906 if (!c)
907 return -EINVAL;
908 *qc = *c;
909 return 0;
910}
911
912int fimc_m2m_g_ctrl(struct file *file, void *priv,
913 struct v4l2_control *ctrl)
914{
915 struct fimc_ctx *ctx = priv;
916
917 switch (ctrl->id) {
918 case V4L2_CID_HFLIP:
919 ctrl->value = (FLIP_X_AXIS & ctx->flip) ? 1 : 0;
920 break;
921 case V4L2_CID_VFLIP:
922 ctrl->value = (FLIP_Y_AXIS & ctx->flip) ? 1 : 0;
923 break;
924 case V4L2_CID_ROTATE:
925 ctrl->value = ctx->rotation;
926 break;
927 default:
928 v4l2_err(&ctx->fimc_dev->m2m.v4l2_dev, "Invalid control\n");
929 return -EINVAL;
930 }
931 dbg("ctrl->value= %d", ctrl->value);
932 return 0;
933}
934
935static int check_ctrl_val(struct fimc_ctx *ctx,
936 struct v4l2_control *ctrl)
937{
938 struct v4l2_queryctrl *c;
939 c = get_ctrl(ctrl->id);
940 if (!c)
941 return -EINVAL;
942
943 if (ctrl->value < c->minimum || ctrl->value > c->maximum
944 || (c->step != 0 && ctrl->value % c->step != 0)) {
945 v4l2_err(&ctx->fimc_dev->m2m.v4l2_dev,
946 "Invalid control value\n");
947 return -ERANGE;
948 }
949
950 return 0;
951}
952
953int fimc_m2m_s_ctrl(struct file *file, void *priv,
954 struct v4l2_control *ctrl)
955{
956 struct fimc_ctx *ctx = priv;
957 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
958 unsigned long flags;
959 int ret = 0;
960
961 ret = check_ctrl_val(ctx, ctrl);
962 if (ret)
963 return ret;
964
965 switch (ctrl->id) {
966 case V4L2_CID_HFLIP:
967 if (ctx->rotation != 0)
968 return 0;
969 if (ctrl->value)
970 ctx->flip |= FLIP_X_AXIS;
971 else
972 ctx->flip &= ~FLIP_X_AXIS;
973 break;
974
975 case V4L2_CID_VFLIP:
976 if (ctx->rotation != 0)
977 return 0;
978 if (ctrl->value)
979 ctx->flip |= FLIP_Y_AXIS;
980 else
981 ctx->flip &= ~FLIP_Y_AXIS;
982 break;
983
984 case V4L2_CID_ROTATE:
985 if (ctrl->value == 90 || ctrl->value == 270) {
986 if (ctx->out_path == FIMC_LCDFIFO &&
987 !variant->has_inp_rot) {
988 return -EINVAL;
989 } else if (ctx->in_path == FIMC_DMA &&
990 !variant->has_out_rot) {
991 return -EINVAL;
992 }
993 }
994 ctx->rotation = ctrl->value;
995 if (ctrl->value == 180)
996 ctx->flip = FLIP_XY_AXIS;
997 break;
998
999 default:
1000 v4l2_err(&ctx->fimc_dev->m2m.v4l2_dev, "Invalid control\n");
1001 return -EINVAL;
1002 }
1003 spin_lock_irqsave(&ctx->slock, flags);
1004 ctx->state |= FIMC_PARAMS;
1005 spin_unlock_irqrestore(&ctx->slock, flags);
1006 return 0;
1007}
1008
1009
1010static int fimc_m2m_cropcap(struct file *file, void *fh,
1011 struct v4l2_cropcap *cr)
1012{
1013 struct fimc_frame *frame;
1014 struct fimc_ctx *ctx = fh;
1015
1016 frame = ctx_m2m_get_frame(ctx, cr->type);
1017 if (IS_ERR(frame))
1018 return PTR_ERR(frame);
1019
1020 cr->bounds.left = 0;
1021 cr->bounds.top = 0;
1022 cr->bounds.width = frame->f_width;
1023 cr->bounds.height = frame->f_height;
1024 cr->defrect.left = frame->offs_h;
1025 cr->defrect.top = frame->offs_v;
1026 cr->defrect.width = frame->o_width;
1027 cr->defrect.height = frame->o_height;
1028 return 0;
1029}
1030
1031static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1032{
1033 struct fimc_frame *frame;
1034 struct fimc_ctx *ctx = file->private_data;
1035
1036 frame = ctx_m2m_get_frame(ctx, cr->type);
1037 if (IS_ERR(frame))
1038 return PTR_ERR(frame);
1039
1040 cr->c.left = frame->offs_h;
1041 cr->c.top = frame->offs_v;
1042 cr->c.width = frame->width;
1043 cr->c.height = frame->height;
1044
1045 return 0;
1046}
1047
1048static int fimc_m2m_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1049{
1050 struct fimc_ctx *ctx = file->private_data;
1051 struct fimc_dev *fimc = ctx->fimc_dev;
1052 unsigned long flags;
1053 struct fimc_frame *f;
1054 u32 min_size;
1055 int ret = 0;
1056
1057 if (cr->c.top < 0 || cr->c.left < 0) {
1058 v4l2_err(&fimc->m2m.v4l2_dev,
1059 "doesn't support negative values for top & left\n");
1060 return -EINVAL;
1061 }
1062
1063 if (cr->c.width <= 0 || cr->c.height <= 0) {
1064 v4l2_err(&fimc->m2m.v4l2_dev,
1065 "crop width and height must be greater than 0\n");
1066 return -EINVAL;
1067 }
1068
1069 f = ctx_m2m_get_frame(ctx, cr->type);
1070 if (IS_ERR(f))
1071 return PTR_ERR(f);
1072
1073 /* Adjust to required pixel boundary. */
1074 min_size = (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) ?
1075 fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
1076
1077 cr->c.width = round_down(cr->c.width, min_size);
1078 cr->c.height = round_down(cr->c.height, min_size);
1079 cr->c.left = round_down(cr->c.left + 1, min_size);
1080 cr->c.top = round_down(cr->c.top + 1, min_size);
1081
1082 if ((cr->c.left + cr->c.width > f->o_width)
1083 || (cr->c.top + cr->c.height > f->o_height)) {
1084 v4l2_err(&fimc->m2m.v4l2_dev, "Error in S_CROP params\n");
1085 return -EINVAL;
1086 }
1087
1088 spin_lock_irqsave(&ctx->slock, flags);
1089 if ((ctx->state & FIMC_SRC_FMT) && (ctx->state & FIMC_DST_FMT)) {
1090 /* Check for the pixel scaling ratio when cropping input img. */
1091 if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1092 ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame);
1093 else if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1094 ret = fimc_check_scaler_ratio(&cr->c, &ctx->s_frame);
1095
1096 if (ret) {
1097 spin_unlock_irqrestore(&ctx->slock, flags);
1098 v4l2_err(&fimc->m2m.v4l2_dev, "Out of scaler range");
1099 return -EINVAL;
1100 }
1101 }
1102 ctx->state |= FIMC_PARAMS;
1103 spin_unlock_irqrestore(&ctx->slock, flags);
1104
1105 f->offs_h = cr->c.left;
1106 f->offs_v = cr->c.top;
1107 f->width = cr->c.width;
1108 f->height = cr->c.height;
1109 return 0;
1110}
1111
1112static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
1113 .vidioc_querycap = fimc_m2m_querycap,
1114
1115 .vidioc_enum_fmt_vid_cap = fimc_m2m_enum_fmt,
1116 .vidioc_enum_fmt_vid_out = fimc_m2m_enum_fmt,
1117
1118 .vidioc_g_fmt_vid_cap = fimc_m2m_g_fmt,
1119 .vidioc_g_fmt_vid_out = fimc_m2m_g_fmt,
1120
1121 .vidioc_try_fmt_vid_cap = fimc_m2m_try_fmt,
1122 .vidioc_try_fmt_vid_out = fimc_m2m_try_fmt,
1123
1124 .vidioc_s_fmt_vid_cap = fimc_m2m_s_fmt,
1125 .vidioc_s_fmt_vid_out = fimc_m2m_s_fmt,
1126
1127 .vidioc_reqbufs = fimc_m2m_reqbufs,
1128 .vidioc_querybuf = fimc_m2m_querybuf,
1129
1130 .vidioc_qbuf = fimc_m2m_qbuf,
1131 .vidioc_dqbuf = fimc_m2m_dqbuf,
1132
1133 .vidioc_streamon = fimc_m2m_streamon,
1134 .vidioc_streamoff = fimc_m2m_streamoff,
1135
1136 .vidioc_queryctrl = fimc_m2m_queryctrl,
1137 .vidioc_g_ctrl = fimc_m2m_g_ctrl,
1138 .vidioc_s_ctrl = fimc_m2m_s_ctrl,
1139
1140 .vidioc_g_crop = fimc_m2m_g_crop,
1141 .vidioc_s_crop = fimc_m2m_s_crop,
1142 .vidioc_cropcap = fimc_m2m_cropcap
1143
1144};
1145
1146static void queue_init(void *priv, struct videobuf_queue *vq,
1147 enum v4l2_buf_type type)
1148{
1149 struct fimc_ctx *ctx = priv;
1150 struct fimc_dev *fimc = ctx->fimc_dev;
1151
1152 videobuf_queue_dma_contig_init(vq, &fimc_qops,
1153 fimc->m2m.v4l2_dev.dev,
1154 &fimc->irqlock, type, V4L2_FIELD_NONE,
1155 sizeof(struct fimc_vid_buffer), priv);
1156}
1157
1158static int fimc_m2m_open(struct file *file)
1159{
1160 struct fimc_dev *fimc = video_drvdata(file);
1161 struct fimc_ctx *ctx = NULL;
1162 int err = 0;
1163
1164 mutex_lock(&fimc->lock);
1165 fimc->m2m.refcnt++;
1166 set_bit(ST_OUTDMA_RUN, &fimc->state);
1167 mutex_unlock(&fimc->lock);
1168
1169
1170 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1171 if (!ctx)
1172 return -ENOMEM;
1173
1174 file->private_data = ctx;
1175 ctx->fimc_dev = fimc;
1176 /* default format */
1177 ctx->s_frame.fmt = &fimc_formats[0];
1178 ctx->d_frame.fmt = &fimc_formats[0];
1179 /* per user process device context initialization */
1180 ctx->state = 0;
1181 ctx->flags = 0;
1182 ctx->effect.type = S5P_FIMC_EFFECT_ORIGINAL;
1183 ctx->in_path = FIMC_DMA;
1184 ctx->out_path = FIMC_DMA;
1185 spin_lock_init(&ctx->slock);
1186
1187 ctx->m2m_ctx = v4l2_m2m_ctx_init(ctx, fimc->m2m.m2m_dev, queue_init);
1188 if (IS_ERR(ctx->m2m_ctx)) {
1189 err = PTR_ERR(ctx->m2m_ctx);
1190 kfree(ctx);
1191 }
1192 return err;
1193}
1194
1195static int fimc_m2m_release(struct file *file)
1196{
1197 struct fimc_ctx *ctx = file->private_data;
1198 struct fimc_dev *fimc = ctx->fimc_dev;
1199
1200 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1201 kfree(ctx);
1202 mutex_lock(&fimc->lock);
1203 if (--fimc->m2m.refcnt <= 0)
1204 clear_bit(ST_OUTDMA_RUN, &fimc->state);
1205 mutex_unlock(&fimc->lock);
1206 return 0;
1207}
1208
1209static unsigned int fimc_m2m_poll(struct file *file,
1210 struct poll_table_struct *wait)
1211{
1212 struct fimc_ctx *ctx = file->private_data;
1213 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1214}
1215
1216
1217static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
1218{
1219 struct fimc_ctx *ctx = file->private_data;
1220 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1221}
1222
1223static const struct v4l2_file_operations fimc_m2m_fops = {
1224 .owner = THIS_MODULE,
1225 .open = fimc_m2m_open,
1226 .release = fimc_m2m_release,
1227 .poll = fimc_m2m_poll,
1228 .ioctl = video_ioctl2,
1229 .mmap = fimc_m2m_mmap,
1230};
1231
1232static struct v4l2_m2m_ops m2m_ops = {
1233 .device_run = fimc_dma_run,
1234 .job_abort = fimc_job_abort,
1235};
1236
1237
1238static int fimc_register_m2m_device(struct fimc_dev *fimc)
1239{
1240 struct video_device *vfd;
1241 struct platform_device *pdev;
1242 struct v4l2_device *v4l2_dev;
1243 int ret = 0;
1244
1245 if (!fimc)
1246 return -ENODEV;
1247
1248 pdev = fimc->pdev;
1249 v4l2_dev = &fimc->m2m.v4l2_dev;
1250
1251 /* set name if it is empty */
1252 if (!v4l2_dev->name[0])
1253 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
1254 "%s.m2m", dev_name(&pdev->dev));
1255
1256 ret = v4l2_device_register(&pdev->dev, v4l2_dev);
1257 if (ret)
1258 return ret;;
1259
1260 vfd = video_device_alloc();
1261 if (!vfd) {
1262 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1263 goto err_m2m_r1;
1264 }
1265
1266 vfd->fops = &fimc_m2m_fops;
1267 vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
1268 vfd->minor = -1;
1269 vfd->release = video_device_release;
1270
1271 snprintf(vfd->name, sizeof(vfd->name), "%s:m2m", dev_name(&pdev->dev));
1272
1273 video_set_drvdata(vfd, fimc);
1274 platform_set_drvdata(pdev, fimc);
1275
1276 fimc->m2m.vfd = vfd;
1277 fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
1278 if (IS_ERR(fimc->m2m.m2m_dev)) {
1279 v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
1280 ret = PTR_ERR(fimc->m2m.m2m_dev);
1281 goto err_m2m_r2;
1282 }
1283
1284 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1285 if (ret) {
1286 v4l2_err(v4l2_dev,
1287 "%s(): failed to register video device\n", __func__);
1288 goto err_m2m_r3;
1289 }
1290 v4l2_info(v4l2_dev,
1291 "FIMC m2m driver registered as /dev/video%d\n", vfd->num);
1292
1293 return 0;
1294
1295err_m2m_r3:
1296 v4l2_m2m_release(fimc->m2m.m2m_dev);
1297err_m2m_r2:
1298 video_device_release(fimc->m2m.vfd);
1299err_m2m_r1:
1300 v4l2_device_unregister(v4l2_dev);
1301
1302 return ret;
1303}
1304
1305static void fimc_unregister_m2m_device(struct fimc_dev *fimc)
1306{
1307 if (fimc) {
1308 v4l2_m2m_release(fimc->m2m.m2m_dev);
1309 video_unregister_device(fimc->m2m.vfd);
1310 video_device_release(fimc->m2m.vfd);
1311 v4l2_device_unregister(&fimc->m2m.v4l2_dev);
1312 }
1313}
1314
1315static void fimc_clk_release(struct fimc_dev *fimc)
1316{
1317 int i;
1318 for (i = 0; i < NUM_FIMC_CLOCKS; i++) {
1319 if (fimc->clock[i]) {
1320 clk_disable(fimc->clock[i]);
1321 clk_put(fimc->clock[i]);
1322 }
1323 }
1324}
1325
1326static int fimc_clk_get(struct fimc_dev *fimc)
1327{
1328 int i;
1329 for (i = 0; i < NUM_FIMC_CLOCKS; i++) {
1330 fimc->clock[i] = clk_get(&fimc->pdev->dev, fimc_clock_name[i]);
1331 if (IS_ERR(fimc->clock[i])) {
1332 dev_err(&fimc->pdev->dev,
1333 "failed to get fimc clock: %s\n",
1334 fimc_clock_name[i]);
1335 return -ENXIO;
1336 }
1337 clk_enable(fimc->clock[i]);
1338 }
1339 return 0;
1340}
1341
1342static int fimc_probe(struct platform_device *pdev)
1343{
1344 struct fimc_dev *fimc;
1345 struct resource *res;
1346 struct samsung_fimc_driverdata *drv_data;
1347 int ret = 0;
1348
1349 dev_dbg(&pdev->dev, "%s():\n", __func__);
1350
1351 drv_data = (struct samsung_fimc_driverdata *)
1352 platform_get_device_id(pdev)->driver_data;
1353
1354 if (pdev->id >= drv_data->devs_cnt) {
1355 dev_err(&pdev->dev, "Invalid platform device id: %d\n",
1356 pdev->id);
1357 return -EINVAL;
1358 }
1359
1360 fimc = kzalloc(sizeof(struct fimc_dev), GFP_KERNEL);
1361 if (!fimc)
1362 return -ENOMEM;
1363
1364 fimc->id = pdev->id;
1365 fimc->variant = drv_data->variant[fimc->id];
1366 fimc->pdev = pdev;
1367 fimc->state = ST_IDLE;
1368
1369 spin_lock_init(&fimc->irqlock);
1370 spin_lock_init(&fimc->slock);
1371
1372 mutex_init(&fimc->lock);
1373
1374 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1375 if (!res) {
1376 dev_err(&pdev->dev, "failed to find the registers\n");
1377 ret = -ENOENT;
1378 goto err_info;
1379 }
1380
1381 fimc->regs_res = request_mem_region(res->start, resource_size(res),
1382 dev_name(&pdev->dev));
1383 if (!fimc->regs_res) {
1384 dev_err(&pdev->dev, "failed to obtain register region\n");
1385 ret = -ENOENT;
1386 goto err_info;
1387 }
1388
1389 fimc->regs = ioremap(res->start, resource_size(res));
1390 if (!fimc->regs) {
1391 dev_err(&pdev->dev, "failed to map registers\n");
1392 ret = -ENXIO;
1393 goto err_req_region;
1394 }
1395
1396 ret = fimc_clk_get(fimc);
1397 if (ret)
1398 goto err_regs_unmap;
1399
1400 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1401 if (!res) {
1402 dev_err(&pdev->dev, "failed to get IRQ resource\n");
1403 ret = -ENXIO;
1404 goto err_clk;
1405 }
1406 fimc->irq = res->start;
1407
1408 fimc_hw_reset(fimc);
1409
1410 ret = request_irq(fimc->irq, fimc_isr, 0, pdev->name, fimc);
1411 if (ret) {
1412 dev_err(&pdev->dev, "failed to install irq (%d)\n", ret);
1413 goto err_clk;
1414 }
1415
1416 fimc->work_queue = create_workqueue(dev_name(&fimc->pdev->dev));
1417 if (!fimc->work_queue)
1418 goto err_irq;
1419
1420 ret = fimc_register_m2m_device(fimc);
1421 if (ret)
1422 goto err_wq;
1423
1424 fimc_hw_en_lastirq(fimc, true);
1425
1426 dev_dbg(&pdev->dev, "%s(): fimc-%d registered successfully\n",
1427 __func__, fimc->id);
1428
1429 return 0;
1430
1431err_wq:
1432 destroy_workqueue(fimc->work_queue);
1433err_irq:
1434 free_irq(fimc->irq, fimc);
1435err_clk:
1436 fimc_clk_release(fimc);
1437err_regs_unmap:
1438 iounmap(fimc->regs);
1439err_req_region:
1440 release_resource(fimc->regs_res);
1441 kfree(fimc->regs_res);
1442err_info:
1443 kfree(fimc);
1444 dev_err(&pdev->dev, "failed to install\n");
1445 return ret;
1446}
1447
1448static int __devexit fimc_remove(struct platform_device *pdev)
1449{
1450 struct fimc_dev *fimc =
1451 (struct fimc_dev *)platform_get_drvdata(pdev);
1452
1453 v4l2_info(&fimc->m2m.v4l2_dev, "Removing %s\n", pdev->name);
1454
1455 free_irq(fimc->irq, fimc);
1456
1457 fimc_hw_reset(fimc);
1458
1459 fimc_unregister_m2m_device(fimc);
1460 fimc_clk_release(fimc);
1461 iounmap(fimc->regs);
1462 release_resource(fimc->regs_res);
1463 kfree(fimc->regs_res);
1464 kfree(fimc);
1465 return 0;
1466}
1467
1468static struct samsung_fimc_variant fimc01_variant_s5p = {
1469 .has_inp_rot = 1,
1470 .has_out_rot = 1,
1471 .min_inp_pixsize = 16,
1472 .min_out_pixsize = 16,
1473
1474 .scaler_en_w = 3264,
1475 .scaler_dis_w = 8192,
1476 .in_rot_en_h = 1920,
1477 .in_rot_dis_w = 8192,
1478 .out_rot_en_w = 1920,
1479 .out_rot_dis_w = 4224,
1480};
1481
1482static struct samsung_fimc_variant fimc2_variant_s5p = {
1483 .min_inp_pixsize = 16,
1484 .min_out_pixsize = 16,
1485
1486 .scaler_en_w = 4224,
1487 .scaler_dis_w = 8192,
1488 .in_rot_en_h = 1920,
1489 .in_rot_dis_w = 8192,
1490 .out_rot_en_w = 1920,
1491 .out_rot_dis_w = 4224,
1492};
1493
1494static struct samsung_fimc_variant fimc01_variant_s5pv210 = {
1495 .has_inp_rot = 1,
1496 .has_out_rot = 1,
1497 .min_inp_pixsize = 16,
1498 .min_out_pixsize = 32,
1499
1500 .scaler_en_w = 4224,
1501 .scaler_dis_w = 8192,
1502 .in_rot_en_h = 1920,
1503 .in_rot_dis_w = 8192,
1504 .out_rot_en_w = 1920,
1505 .out_rot_dis_w = 4224,
1506};
1507
1508static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
1509 .min_inp_pixsize = 16,
1510 .min_out_pixsize = 32,
1511
1512 .scaler_en_w = 1920,
1513 .scaler_dis_w = 8192,
1514 .in_rot_en_h = 1280,
1515 .in_rot_dis_w = 8192,
1516 .out_rot_en_w = 1280,
1517 .out_rot_dis_w = 1920,
1518};
1519
1520static struct samsung_fimc_driverdata fimc_drvdata_s5p = {
1521 .variant = {
1522 [0] = &fimc01_variant_s5p,
1523 [1] = &fimc01_variant_s5p,
1524 [2] = &fimc2_variant_s5p,
1525 },
1526 .devs_cnt = 3
1527};
1528
1529static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
1530 .variant = {
1531 [0] = &fimc01_variant_s5pv210,
1532 [1] = &fimc01_variant_s5pv210,
1533 [2] = &fimc2_variant_s5pv210,
1534 },
1535 .devs_cnt = 3
1536};
1537
1538static struct platform_device_id fimc_driver_ids[] = {
1539 {
1540 .name = "s5p-fimc",
1541 .driver_data = (unsigned long)&fimc_drvdata_s5p,
1542 }, {
1543 .name = "s5pv210-fimc",
1544 .driver_data = (unsigned long)&fimc_drvdata_s5pv210,
1545 },
1546 {},
1547};
1548MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1549
1550static struct platform_driver fimc_driver = {
1551 .probe = fimc_probe,
1552 .remove = __devexit_p(fimc_remove),
1553 .id_table = fimc_driver_ids,
1554 .driver = {
1555 .name = MODULE_NAME,
1556 .owner = THIS_MODULE,
1557 }
1558};
1559
1560static char banner[] __initdata = KERN_INFO
1561 "S5PC Camera Interface V4L2 Driver, (c) 2010 Samsung Electronics\n";
1562
1563static int __init fimc_init(void)
1564{
1565 u32 ret;
1566 printk(banner);
1567
1568 ret = platform_driver_register(&fimc_driver);
1569 if (ret) {
1570 printk(KERN_ERR "FIMC platform driver register failed\n");
1571 return -1;
1572 }
1573 return 0;
1574}
1575
1576static void __exit fimc_exit(void)
1577{
1578 platform_driver_unregister(&fimc_driver);
1579}
1580
1581module_init(fimc_init);
1582module_exit(fimc_exit);
1583
1584MODULE_AUTHOR("Sylwester Nawrocki, s.nawrocki@samsung.com");
1585MODULE_DESCRIPTION("S3C/S5P FIMC (video postprocessor) driver");
1586MODULE_LICENSE("GPL");
diff --git a/drivers/media/video/s5p-fimc/fimc-core.h b/drivers/media/video/s5p-fimc/fimc-core.h
new file mode 100644
index 00000000000..6b3e0cd73cd
--- /dev/null
+++ b/drivers/media/video/s5p-fimc/fimc-core.h
@@ -0,0 +1,471 @@
1/*
2 * Copyright (c) 2010 Samsung Electronics
3 *
4 * Sylwester Nawrocki, <s.nawrocki@samsung.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 version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#ifndef FIMC_CORE_H_
12#define FIMC_CORE_H_
13
14#include <linux/types.h>
15#include <media/videobuf-core.h>
16#include <media/v4l2-device.h>
17#include <media/v4l2-mem2mem.h>
18#include <linux/videodev2.h>
19#include "regs-fimc.h"
20
21#define err(fmt, args...) \
22 printk(KERN_ERR "%s:%d: " fmt "\n", __func__, __LINE__, ##args)
23
24#ifdef DEBUG
25#define dbg(fmt, args...) \
26 printk(KERN_DEBUG "%s:%d: " fmt "\n", __func__, __LINE__, ##args)
27#else
28#define dbg(fmt, args...)
29#endif
30
31#define NUM_FIMC_CLOCKS 2
32#define MODULE_NAME "s5p-fimc"
33#define FIMC_MAX_DEVS 3
34#define FIMC_MAX_OUT_BUFS 4
35#define SCALER_MAX_HRATIO 64
36#define SCALER_MAX_VRATIO 64
37
38enum {
39 ST_IDLE,
40 ST_OUTDMA_RUN,
41 ST_M2M_PEND,
42};
43
44#define fimc_m2m_active(dev) test_bit(ST_OUTDMA_RUN, &(dev)->state)
45#define fimc_m2m_pending(dev) test_bit(ST_M2M_PEND, &(dev)->state)
46
47enum fimc_datapath {
48 FIMC_ITU_CAM_A,
49 FIMC_ITU_CAM_B,
50 FIMC_MIPI_CAM,
51 FIMC_DMA,
52 FIMC_LCDFIFO,
53 FIMC_WRITEBACK
54};
55
56enum fimc_color_fmt {
57 S5P_FIMC_RGB565,
58 S5P_FIMC_RGB666,
59 S5P_FIMC_RGB888,
60 S5P_FIMC_YCBCR420,
61 S5P_FIMC_YCBCR422,
62 S5P_FIMC_YCBYCR422,
63 S5P_FIMC_YCRYCB422,
64 S5P_FIMC_CBYCRY422,
65 S5P_FIMC_CRYCBY422,
66 S5P_FIMC_RGB30_LOCAL,
67 S5P_FIMC_YCBCR444_LOCAL,
68 S5P_FIMC_MAX_COLOR = S5P_FIMC_YCBCR444_LOCAL,
69 S5P_FIMC_COLOR_MASK = 0x0F,
70};
71
72/* Y/Cb/Cr components order at DMA output for 1 plane YCbCr 4:2:2 formats. */
73#define S5P_FIMC_OUT_CRYCBY S5P_CIOCTRL_ORDER422_CRYCBY
74#define S5P_FIMC_OUT_CBYCRY S5P_CIOCTRL_ORDER422_YCRYCB
75#define S5P_FIMC_OUT_YCRYCB S5P_CIOCTRL_ORDER422_CBYCRY
76#define S5P_FIMC_OUT_YCBYCR S5P_CIOCTRL_ORDER422_YCBYCR
77
78/* Input Y/Cb/Cr components order for 1 plane YCbCr 4:2:2 color formats. */
79#define S5P_FIMC_IN_CRYCBY S5P_MSCTRL_ORDER422_CRYCBY
80#define S5P_FIMC_IN_CBYCRY S5P_MSCTRL_ORDER422_YCRYCB
81#define S5P_FIMC_IN_YCRYCB S5P_MSCTRL_ORDER422_CBYCRY
82#define S5P_FIMC_IN_YCBYCR S5P_MSCTRL_ORDER422_YCBYCR
83
84/* Cb/Cr chrominance components order for 2 plane Y/CbCr 4:2:2 formats. */
85#define S5P_FIMC_LSB_CRCB S5P_CIOCTRL_ORDER422_2P_LSB_CRCB
86
87/* The embedded image effect selection */
88#define S5P_FIMC_EFFECT_ORIGINAL S5P_CIIMGEFF_FIN_BYPASS
89#define S5P_FIMC_EFFECT_ARBITRARY S5P_CIIMGEFF_FIN_ARBITRARY
90#define S5P_FIMC_EFFECT_NEGATIVE S5P_CIIMGEFF_FIN_NEGATIVE
91#define S5P_FIMC_EFFECT_ARTFREEZE S5P_CIIMGEFF_FIN_ARTFREEZE
92#define S5P_FIMC_EFFECT_EMBOSSING S5P_CIIMGEFF_FIN_EMBOSSING
93#define S5P_FIMC_EFFECT_SIKHOUETTE S5P_CIIMGEFF_FIN_SILHOUETTE
94
95/* The hardware context state. */
96#define FIMC_PARAMS (1 << 0)
97#define FIMC_SRC_ADDR (1 << 1)
98#define FIMC_DST_ADDR (1 << 2)
99#define FIMC_SRC_FMT (1 << 3)
100#define FIMC_DST_FMT (1 << 4)
101
102/* Image conversion flags */
103#define FIMC_IN_DMA_ACCESS_TILED (1 << 0)
104#define FIMC_IN_DMA_ACCESS_LINEAR (0 << 0)
105#define FIMC_OUT_DMA_ACCESS_TILED (1 << 1)
106#define FIMC_OUT_DMA_ACCESS_LINEAR (0 << 1)
107#define FIMC_SCAN_MODE_PROGRESSIVE (0 << 2)
108#define FIMC_SCAN_MODE_INTERLACED (1 << 2)
109/* YCbCr data dynamic range for RGB-YUV color conversion. Y/Cb/Cr: (0 ~ 255) */
110#define FIMC_COLOR_RANGE_WIDE (0 << 3)
111/* Y (16 ~ 235), Cb/Cr (16 ~ 240) */
112#define FIMC_COLOR_RANGE_NARROW (1 << 3)
113
114#define FLIP_NONE 0
115#define FLIP_X_AXIS 1
116#define FLIP_Y_AXIS 2
117#define FLIP_XY_AXIS (FLIP_X_AXIS | FLIP_Y_AXIS)
118
119/**
120 * struct fimc_fmt - the driver's internal color format data
121 * @name: format description
122 * @fourcc: the fourcc code for this format
123 * @color: the corresponding fimc_color_fmt
124 * @depth: number of bits per pixel
125 * @buff_cnt: number of physically non-contiguous data planes
126 * @planes_cnt: number of physically contiguous data planes
127 */
128struct fimc_fmt {
129 char *name;
130 u32 fourcc;
131 u32 color;
132 u32 depth;
133 u16 buff_cnt;
134 u16 planes_cnt;
135};
136
137/**
138 * struct fimc_dma_offset - pixel offset information for DMA
139 * @y_h: y value horizontal offset
140 * @y_v: y value vertical offset
141 * @cb_h: cb value horizontal offset
142 * @cb_v: cb value vertical offset
143 * @cr_h: cr value horizontal offset
144 * @cr_v: cr value vertical offset
145 */
146struct fimc_dma_offset {
147 int y_h;
148 int y_v;
149 int cb_h;
150 int cb_v;
151 int cr_h;
152 int cr_v;
153};
154
155/**
156 * struct fimc_effect - the configuration data for the "Arbitrary" image effect
157 * @type: effect type
158 * @pat_cb: cr value when type is "arbitrary"
159 * @pat_cr: cr value when type is "arbitrary"
160 */
161struct fimc_effect {
162 u32 type;
163 u8 pat_cb;
164 u8 pat_cr;
165};
166
167/**
168 * struct fimc_scaler - the configuration data for FIMC inetrnal scaler
169 *
170 * @enabled: the flag set when the scaler is used
171 * @hfactor: horizontal shift factor
172 * @vfactor: vertical shift factor
173 * @pre_hratio: horizontal ratio of the prescaler
174 * @pre_vratio: vertical ratio of the prescaler
175 * @pre_dst_width: the prescaler's destination width
176 * @pre_dst_height: the prescaler's destination height
177 * @scaleup_h: flag indicating scaling up horizontally
178 * @scaleup_v: flag indicating scaling up vertically
179 * @main_hratio: the main scaler's horizontal ratio
180 * @main_vratio: the main scaler's vertical ratio
181 * @real_width: source width - offset
182 * @real_height: source height - offset
183 * @copy_mode: flag set if one-to-one mode is used, i.e. no scaling
184 * and color format conversion
185 */
186struct fimc_scaler {
187 u32 enabled;
188 u32 hfactor;
189 u32 vfactor;
190 u32 pre_hratio;
191 u32 pre_vratio;
192 u32 pre_dst_width;
193 u32 pre_dst_height;
194 u32 scaleup_h;
195 u32 scaleup_v;
196 u32 main_hratio;
197 u32 main_vratio;
198 u32 real_width;
199 u32 real_height;
200 u32 copy_mode;
201};
202
203/**
204 * struct fimc_addr - the FIMC physical address set for DMA
205 *
206 * @y: luminance plane physical address
207 * @cb: Cb plane physical address
208 * @cr: Cr plane physical address
209 */
210struct fimc_addr {
211 u32 y;
212 u32 cb;
213 u32 cr;
214};
215
216/**
217 * struct fimc_vid_buffer - the driver's video buffer
218 * @vb: v4l videobuf buffer
219 */
220struct fimc_vid_buffer {
221 struct videobuf_buffer vb;
222};
223
224/**
225 * struct fimc_frame - input/output frame format properties
226 *
227 * @f_width: image full width (virtual screen size)
228 * @f_height: image full height (virtual screen size)
229 * @o_width: original image width as set by S_FMT
230 * @o_height: original image height as set by S_FMT
231 * @offs_h: image horizontal pixel offset
232 * @offs_v: image vertical pixel offset
233 * @width: image pixel width
234 * @height: image pixel weight
235 * @paddr: image frame buffer physical addresses
236 * @buf_cnt: number of buffers depending on a color format
237 * @size: image size in bytes
238 * @color: color format
239 * @dma_offset: DMA offset in bytes
240 */
241struct fimc_frame {
242 u32 f_width;
243 u32 f_height;
244 u32 o_width;
245 u32 o_height;
246 u32 offs_h;
247 u32 offs_v;
248 u32 width;
249 u32 height;
250 u32 size;
251 struct fimc_addr paddr;
252 struct fimc_dma_offset dma_offset;
253 struct fimc_fmt *fmt;
254};
255
256/**
257 * struct fimc_m2m_device - v4l2 memory-to-memory device data
258 * @vfd: the video device node for v4l2 m2m mode
259 * @v4l2_dev: v4l2 device for m2m mode
260 * @m2m_dev: v4l2 memory-to-memory device data
261 * @ctx: hardware context data
262 * @refcnt: the reference counter
263 */
264struct fimc_m2m_device {
265 struct video_device *vfd;
266 struct v4l2_device v4l2_dev;
267 struct v4l2_m2m_dev *m2m_dev;
268 struct fimc_ctx *ctx;
269 int refcnt;
270};
271
272/**
273 * struct samsung_fimc_variant - camera interface variant information
274 *
275 * @pix_hoff: indicate whether horizontal offset is in pixels or in bytes
276 * @has_inp_rot: set if has input rotator
277 * @has_out_rot: set if has output rotator
278 * @min_inp_pixsize: minimum input pixel size
279 * @min_out_pixsize: minimum output pixel size
280 * @scaler_en_w: maximum input pixel width when the scaler is enabled
281 * @scaler_dis_w: maximum input pixel width when the scaler is disabled
282 * @in_rot_en_h: maximum input width when the input rotator is used
283 * @in_rot_dis_w: maximum input width when the input rotator is used
284 * @out_rot_en_w: maximum output width for the output rotator enabled
285 * @out_rot_dis_w: maximum output width for the output rotator enabled
286 */
287struct samsung_fimc_variant {
288 unsigned int pix_hoff:1;
289 unsigned int has_inp_rot:1;
290 unsigned int has_out_rot:1;
291
292 u16 min_inp_pixsize;
293 u16 min_out_pixsize;
294 u16 scaler_en_w;
295 u16 scaler_dis_w;
296 u16 in_rot_en_h;
297 u16 in_rot_dis_w;
298 u16 out_rot_en_w;
299 u16 out_rot_dis_w;
300};
301
302/**
303 * struct samsung_fimc_driverdata - per-device type driver data for init time.
304 *
305 * @variant: the variant information for this driver.
306 * @dev_cnt: number of fimc sub-devices available in SoC
307 */
308struct samsung_fimc_driverdata {
309 struct samsung_fimc_variant *variant[FIMC_MAX_DEVS];
310 int devs_cnt;
311};
312
313struct fimc_ctx;
314
315/**
316 * struct fimc_subdev - abstraction for a FIMC entity
317 *
318 * @slock: the spinlock protecting this data structure
319 * @lock: the mutex protecting this data structure
320 * @pdev: pointer to the FIMC platform device
321 * @id: FIMC device index (0..2)
322 * @clock[]: the clocks required for FIMC operation
323 * @regs: the mapped hardware registers
324 * @regs_res: the resource claimed for IO registers
325 * @irq: interrupt number of the FIMC subdevice
326 * @irqlock: spinlock protecting videbuffer queue
327 * @m2m: memory-to-memory V4L2 device information
328 * @state: the FIMC device state flags
329 */
330struct fimc_dev {
331 spinlock_t slock;
332 struct mutex lock;
333 struct platform_device *pdev;
334 struct samsung_fimc_variant *variant;
335 int id;
336 struct clk *clock[NUM_FIMC_CLOCKS];
337 void __iomem *regs;
338 struct resource *regs_res;
339 int irq;
340 spinlock_t irqlock;
341 struct workqueue_struct *work_queue;
342 struct fimc_m2m_device m2m;
343 unsigned long state;
344};
345
346/**
347 * fimc_ctx - the device context data
348 *
349 * @lock: mutex protecting this data structure
350 * @s_frame: source frame properties
351 * @d_frame: destination frame properties
352 * @out_order_1p: output 1-plane YCBCR order
353 * @out_order_2p: output 2-plane YCBCR order
354 * @in_order_1p input 1-plane YCBCR order
355 * @in_order_2p: input 2-plane YCBCR order
356 * @in_path: input mode (DMA or camera)
357 * @out_path: output mode (DMA or FIFO)
358 * @scaler: image scaler properties
359 * @effect: image effect
360 * @rotation: image clockwise rotation in degrees
361 * @flip: image flip mode
362 * @flags: an additional flags for image conversion
363 * @state: flags to keep track of user configuration
364 * @fimc_dev: the FIMC device this context applies to
365 * @m2m_ctx: memory-to-memory device context
366 */
367struct fimc_ctx {
368 spinlock_t slock;
369 struct fimc_frame s_frame;
370 struct fimc_frame d_frame;
371 u32 out_order_1p;
372 u32 out_order_2p;
373 u32 in_order_1p;
374 u32 in_order_2p;
375 enum fimc_datapath in_path;
376 enum fimc_datapath out_path;
377 struct fimc_scaler scaler;
378 struct fimc_effect effect;
379 int rotation;
380 u32 flip;
381 u32 flags;
382 u32 state;
383 struct fimc_dev *fimc_dev;
384 struct v4l2_m2m_ctx *m2m_ctx;
385};
386
387
388static inline int tiled_fmt(struct fimc_fmt *fmt)
389{
390 return 0;
391}
392
393static inline void fimc_hw_clear_irq(struct fimc_dev *dev)
394{
395 u32 cfg = readl(dev->regs + S5P_CIGCTRL);
396 cfg |= S5P_CIGCTRL_IRQ_CLR;
397 writel(cfg, dev->regs + S5P_CIGCTRL);
398}
399
400static inline void fimc_hw_start_scaler(struct fimc_dev *dev)
401{
402 u32 cfg = readl(dev->regs + S5P_CISCCTRL);
403 cfg |= S5P_CISCCTRL_SCALERSTART;
404 writel(cfg, dev->regs + S5P_CISCCTRL);
405}
406
407static inline void fimc_hw_stop_scaler(struct fimc_dev *dev)
408{
409 u32 cfg = readl(dev->regs + S5P_CISCCTRL);
410 cfg &= ~S5P_CISCCTRL_SCALERSTART;
411 writel(cfg, dev->regs + S5P_CISCCTRL);
412}
413
414static inline void fimc_hw_dis_capture(struct fimc_dev *dev)
415{
416 u32 cfg = readl(dev->regs + S5P_CIIMGCPT);
417 cfg &= ~(S5P_CIIMGCPT_IMGCPTEN | S5P_CIIMGCPT_IMGCPTEN_SC);
418 writel(cfg, dev->regs + S5P_CIIMGCPT);
419}
420
421static inline void fimc_hw_start_in_dma(struct fimc_dev *dev)
422{
423 u32 cfg = readl(dev->regs + S5P_MSCTRL);
424 cfg |= S5P_MSCTRL_ENVID;
425 writel(cfg, dev->regs + S5P_MSCTRL);
426}
427
428static inline void fimc_hw_stop_in_dma(struct fimc_dev *dev)
429{
430 u32 cfg = readl(dev->regs + S5P_MSCTRL);
431 cfg &= ~S5P_MSCTRL_ENVID;
432 writel(cfg, dev->regs + S5P_MSCTRL);
433}
434
435static inline struct fimc_frame *ctx_m2m_get_frame(struct fimc_ctx *ctx,
436 enum v4l2_buf_type type)
437{
438 struct fimc_frame *frame;
439
440 if (V4L2_BUF_TYPE_VIDEO_OUTPUT == type) {
441 frame = &ctx->s_frame;
442 } else if (V4L2_BUF_TYPE_VIDEO_CAPTURE == type) {
443 frame = &ctx->d_frame;
444 } else {
445 v4l2_err(&ctx->fimc_dev->m2m.v4l2_dev,
446 "Wrong buffer/video queue type (%d)\n", type);
447 return ERR_PTR(-EINVAL);
448 }
449
450 return frame;
451}
452
453/* -----------------------------------------------------*/
454/* fimc-reg.c */
455void fimc_hw_reset(struct fimc_dev *dev);
456void fimc_hw_set_rotation(struct fimc_ctx *ctx);
457void fimc_hw_set_target_format(struct fimc_ctx *ctx);
458void fimc_hw_set_out_dma(struct fimc_ctx *ctx);
459void fimc_hw_en_lastirq(struct fimc_dev *dev, int enable);
460void fimc_hw_en_irq(struct fimc_dev *dev, int enable);
461void fimc_hw_set_prescaler(struct fimc_ctx *ctx);
462void fimc_hw_set_scaler(struct fimc_ctx *ctx);
463void fimc_hw_en_capture(struct fimc_ctx *ctx);
464void fimc_hw_set_effect(struct fimc_ctx *ctx);
465void fimc_hw_set_in_dma(struct fimc_ctx *ctx);
466void fimc_hw_set_input_path(struct fimc_ctx *ctx);
467void fimc_hw_set_output_path(struct fimc_ctx *ctx);
468void fimc_hw_set_input_addr(struct fimc_dev *dev, struct fimc_addr *paddr);
469void fimc_hw_set_output_addr(struct fimc_dev *dev, struct fimc_addr *paddr);
470
471#endif /* FIMC_CORE_H_ */
diff --git a/drivers/media/video/s5p-fimc/fimc-reg.c b/drivers/media/video/s5p-fimc/fimc-reg.c
new file mode 100644
index 00000000000..5570f1ce0c9
--- /dev/null
+++ b/drivers/media/video/s5p-fimc/fimc-reg.c
@@ -0,0 +1,527 @@
1/*
2 * Register interface file for Samsung Camera Interface (FIMC) driver
3 *
4 * Copyright (c) 2010 Samsung Electronics
5 *
6 * Sylwester Nawrocki, s.nawrocki@samsung.com
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11*/
12
13#include <linux/io.h>
14#include <linux/delay.h>
15#include <mach/map.h>
16
17#include "fimc-core.h"
18
19
20void fimc_hw_reset(struct fimc_dev *dev)
21{
22 u32 cfg;
23
24 cfg = readl(dev->regs + S5P_CISRCFMT);
25 cfg |= S5P_CISRCFMT_ITU601_8BIT;
26 writel(cfg, dev->regs + S5P_CISRCFMT);
27
28 /* Software reset. */
29 cfg = readl(dev->regs + S5P_CIGCTRL);
30 cfg |= (S5P_CIGCTRL_SWRST | S5P_CIGCTRL_IRQ_LEVEL);
31 writel(cfg, dev->regs + S5P_CIGCTRL);
32 msleep(1);
33
34 cfg = readl(dev->regs + S5P_CIGCTRL);
35 cfg &= ~S5P_CIGCTRL_SWRST;
36 writel(cfg, dev->regs + S5P_CIGCTRL);
37
38}
39
40void fimc_hw_set_rotation(struct fimc_ctx *ctx)
41{
42 u32 cfg, flip;
43 struct fimc_dev *dev = ctx->fimc_dev;
44
45 cfg = readl(dev->regs + S5P_CITRGFMT);
46 cfg &= ~(S5P_CITRGFMT_INROT90 | S5P_CITRGFMT_OUTROT90);
47
48 flip = readl(dev->regs + S5P_MSCTRL);
49 flip &= ~S5P_MSCTRL_FLIP_MASK;
50
51 /*
52 * The input and output rotator cannot work simultaneously.
53 * Use the output rotator in output DMA mode or the input rotator
54 * in direct fifo output mode.
55 */
56 if (ctx->rotation == 90 || ctx->rotation == 270) {
57 if (ctx->out_path == FIMC_LCDFIFO) {
58 cfg |= S5P_CITRGFMT_INROT90;
59 if (ctx->rotation == 270)
60 flip |= S5P_MSCTRL_FLIP_180;
61 } else {
62 cfg |= S5P_CITRGFMT_OUTROT90;
63 if (ctx->rotation == 270)
64 cfg |= S5P_CITRGFMT_FLIP_180;
65 }
66 } else if (ctx->rotation == 180) {
67 if (ctx->out_path == FIMC_LCDFIFO)
68 flip |= S5P_MSCTRL_FLIP_180;
69 else
70 cfg |= S5P_CITRGFMT_FLIP_180;
71 }
72 if (ctx->rotation == 180 || ctx->rotation == 270)
73 writel(flip, dev->regs + S5P_MSCTRL);
74 writel(cfg, dev->regs + S5P_CITRGFMT);
75}
76
77static u32 fimc_hw_get_in_flip(u32 ctx_flip)
78{
79 u32 flip = S5P_MSCTRL_FLIP_NORMAL;
80
81 switch (ctx_flip) {
82 case FLIP_X_AXIS:
83 flip = S5P_MSCTRL_FLIP_X_MIRROR;
84 break;
85 case FLIP_Y_AXIS:
86 flip = S5P_MSCTRL_FLIP_Y_MIRROR;
87 break;
88 case FLIP_XY_AXIS:
89 flip = S5P_MSCTRL_FLIP_180;
90 break;
91 }
92
93 return flip;
94}
95
96static u32 fimc_hw_get_target_flip(u32 ctx_flip)
97{
98 u32 flip = S5P_CITRGFMT_FLIP_NORMAL;
99
100 switch (ctx_flip) {
101 case FLIP_X_AXIS:
102 flip = S5P_CITRGFMT_FLIP_X_MIRROR;
103 break;
104 case FLIP_Y_AXIS:
105 flip = S5P_CITRGFMT_FLIP_Y_MIRROR;
106 break;
107 case FLIP_XY_AXIS:
108 flip = S5P_CITRGFMT_FLIP_180;
109 break;
110 case FLIP_NONE:
111 break;
112
113 }
114 return flip;
115}
116
117void fimc_hw_set_target_format(struct fimc_ctx *ctx)
118{
119 u32 cfg;
120 struct fimc_dev *dev = ctx->fimc_dev;
121 struct fimc_frame *frame = &ctx->d_frame;
122
123 dbg("w= %d, h= %d color: %d", frame->width,
124 frame->height, frame->fmt->color);
125
126 cfg = readl(dev->regs + S5P_CITRGFMT);
127 cfg &= ~(S5P_CITRGFMT_FMT_MASK | S5P_CITRGFMT_HSIZE_MASK |
128 S5P_CITRGFMT_VSIZE_MASK);
129
130 switch (frame->fmt->color) {
131 case S5P_FIMC_RGB565:
132 case S5P_FIMC_RGB666:
133 case S5P_FIMC_RGB888:
134 cfg |= S5P_CITRGFMT_RGB;
135 break;
136 case S5P_FIMC_YCBCR420:
137 cfg |= S5P_CITRGFMT_YCBCR420;
138 break;
139 case S5P_FIMC_YCBYCR422:
140 case S5P_FIMC_YCRYCB422:
141 case S5P_FIMC_CBYCRY422:
142 case S5P_FIMC_CRYCBY422:
143 if (frame->fmt->planes_cnt == 1)
144 cfg |= S5P_CITRGFMT_YCBCR422_1P;
145 else
146 cfg |= S5P_CITRGFMT_YCBCR422;
147 break;
148 default:
149 break;
150 }
151
152 cfg |= S5P_CITRGFMT_HSIZE(frame->width);
153 cfg |= S5P_CITRGFMT_VSIZE(frame->height);
154
155 if (ctx->rotation == 0) {
156 cfg &= ~S5P_CITRGFMT_FLIP_MASK;
157 cfg |= fimc_hw_get_target_flip(ctx->flip);
158 }
159 writel(cfg, dev->regs + S5P_CITRGFMT);
160
161 cfg = readl(dev->regs + S5P_CITAREA) & ~S5P_CITAREA_MASK;
162 cfg |= (frame->width * frame->height);
163 writel(cfg, dev->regs + S5P_CITAREA);
164}
165
166static void fimc_hw_set_out_dma_size(struct fimc_ctx *ctx)
167{
168 struct fimc_dev *dev = ctx->fimc_dev;
169 struct fimc_frame *frame = &ctx->d_frame;
170 u32 cfg = 0;
171
172 if (ctx->rotation == 90 || ctx->rotation == 270) {
173 cfg |= S5P_ORIG_SIZE_HOR(frame->f_height);
174 cfg |= S5P_ORIG_SIZE_VER(frame->f_width);
175 } else {
176 cfg |= S5P_ORIG_SIZE_HOR(frame->f_width);
177 cfg |= S5P_ORIG_SIZE_VER(frame->f_height);
178 }
179 writel(cfg, dev->regs + S5P_ORGOSIZE);
180}
181
182void fimc_hw_set_out_dma(struct fimc_ctx *ctx)
183{
184 u32 cfg;
185 struct fimc_dev *dev = ctx->fimc_dev;
186 struct fimc_frame *frame = &ctx->d_frame;
187 struct fimc_dma_offset *offset = &frame->dma_offset;
188
189 /* Set the input dma offsets. */
190 cfg = 0;
191 cfg |= S5P_CIO_OFFS_HOR(offset->y_h);
192 cfg |= S5P_CIO_OFFS_VER(offset->y_v);
193 writel(cfg, dev->regs + S5P_CIOYOFF);
194
195 cfg = 0;
196 cfg |= S5P_CIO_OFFS_HOR(offset->cb_h);
197 cfg |= S5P_CIO_OFFS_VER(offset->cb_v);
198 writel(cfg, dev->regs + S5P_CIOCBOFF);
199
200 cfg = 0;
201 cfg |= S5P_CIO_OFFS_HOR(offset->cr_h);
202 cfg |= S5P_CIO_OFFS_VER(offset->cr_v);
203 writel(cfg, dev->regs + S5P_CIOCROFF);
204
205 fimc_hw_set_out_dma_size(ctx);
206
207 /* Configure chroma components order. */
208 cfg = readl(dev->regs + S5P_CIOCTRL);
209
210 cfg &= ~(S5P_CIOCTRL_ORDER2P_MASK | S5P_CIOCTRL_ORDER422_MASK |
211 S5P_CIOCTRL_YCBCR_PLANE_MASK);
212
213 if (frame->fmt->planes_cnt == 1)
214 cfg |= ctx->out_order_1p;
215 else if (frame->fmt->planes_cnt == 2)
216 cfg |= ctx->out_order_2p | S5P_CIOCTRL_YCBCR_2PLANE;
217 else if (frame->fmt->planes_cnt == 3)
218 cfg |= S5P_CIOCTRL_YCBCR_3PLANE;
219
220 writel(cfg, dev->regs + S5P_CIOCTRL);
221}
222
223static void fimc_hw_en_autoload(struct fimc_dev *dev, int enable)
224{
225 u32 cfg = readl(dev->regs + S5P_ORGISIZE);
226 if (enable)
227 cfg |= S5P_CIREAL_ISIZE_AUTOLOAD_EN;
228 else
229 cfg &= ~S5P_CIREAL_ISIZE_AUTOLOAD_EN;
230 writel(cfg, dev->regs + S5P_ORGISIZE);
231}
232
233void fimc_hw_en_lastirq(struct fimc_dev *dev, int enable)
234{
235 unsigned long flags;
236 u32 cfg;
237
238 spin_lock_irqsave(&dev->slock, flags);
239
240 cfg = readl(dev->regs + S5P_CIOCTRL);
241 if (enable)
242 cfg |= S5P_CIOCTRL_LASTIRQ_ENABLE;
243 else
244 cfg &= ~S5P_CIOCTRL_LASTIRQ_ENABLE;
245 writel(cfg, dev->regs + S5P_CIOCTRL);
246
247 spin_unlock_irqrestore(&dev->slock, flags);
248}
249
250void fimc_hw_set_prescaler(struct fimc_ctx *ctx)
251{
252 struct fimc_dev *dev = ctx->fimc_dev;
253 struct fimc_scaler *sc = &ctx->scaler;
254 u32 cfg = 0, shfactor;
255
256 shfactor = 10 - (sc->hfactor + sc->vfactor);
257
258 cfg |= S5P_CISCPRERATIO_SHFACTOR(shfactor);
259 cfg |= S5P_CISCPRERATIO_HOR(sc->pre_hratio);
260 cfg |= S5P_CISCPRERATIO_VER(sc->pre_vratio);
261 writel(cfg, dev->regs + S5P_CISCPRERATIO);
262
263 cfg = 0;
264 cfg |= S5P_CISCPREDST_WIDTH(sc->pre_dst_width);
265 cfg |= S5P_CISCPREDST_HEIGHT(sc->pre_dst_height);
266 writel(cfg, dev->regs + S5P_CISCPREDST);
267}
268
269void fimc_hw_set_scaler(struct fimc_ctx *ctx)
270{
271 struct fimc_dev *dev = ctx->fimc_dev;
272 struct fimc_scaler *sc = &ctx->scaler;
273 struct fimc_frame *src_frame = &ctx->s_frame;
274 struct fimc_frame *dst_frame = &ctx->d_frame;
275 u32 cfg = 0;
276
277 if (!(ctx->flags & FIMC_COLOR_RANGE_NARROW))
278 cfg |= (S5P_CISCCTRL_CSCR2Y_WIDE | S5P_CISCCTRL_CSCY2R_WIDE);
279
280 if (!sc->enabled)
281 cfg |= S5P_CISCCTRL_SCALERBYPASS;
282
283 if (sc->scaleup_h)
284 cfg |= S5P_CISCCTRL_SCALEUP_H;
285
286 if (sc->scaleup_v)
287 cfg |= S5P_CISCCTRL_SCALEUP_V;
288
289 if (sc->copy_mode)
290 cfg |= S5P_CISCCTRL_ONE2ONE;
291
292
293 if (ctx->in_path == FIMC_DMA) {
294 if (src_frame->fmt->color == S5P_FIMC_RGB565)
295 cfg |= S5P_CISCCTRL_INRGB_FMT_RGB565;
296 else if (src_frame->fmt->color == S5P_FIMC_RGB666)
297 cfg |= S5P_CISCCTRL_INRGB_FMT_RGB666;
298 else if (src_frame->fmt->color == S5P_FIMC_RGB888)
299 cfg |= S5P_CISCCTRL_INRGB_FMT_RGB888;
300 }
301
302 if (ctx->out_path == FIMC_DMA) {
303 if (dst_frame->fmt->color == S5P_FIMC_RGB565)
304 cfg |= S5P_CISCCTRL_OUTRGB_FMT_RGB565;
305 else if (dst_frame->fmt->color == S5P_FIMC_RGB666)
306 cfg |= S5P_CISCCTRL_OUTRGB_FMT_RGB666;
307 else if (dst_frame->fmt->color == S5P_FIMC_RGB888)
308 cfg |= S5P_CISCCTRL_OUTRGB_FMT_RGB888;
309 } else {
310 cfg |= S5P_CISCCTRL_OUTRGB_FMT_RGB888;
311
312 if (ctx->flags & FIMC_SCAN_MODE_INTERLACED)
313 cfg |= S5P_CISCCTRL_INTERLACE;
314 }
315
316 dbg("main_hratio= 0x%X main_vratio= 0x%X",
317 sc->main_hratio, sc->main_vratio);
318
319 cfg |= S5P_CISCCTRL_SC_HORRATIO(sc->main_hratio);
320 cfg |= S5P_CISCCTRL_SC_VERRATIO(sc->main_vratio);
321
322 writel(cfg, dev->regs + S5P_CISCCTRL);
323}
324
325void fimc_hw_en_capture(struct fimc_ctx *ctx)
326{
327 struct fimc_dev *dev = ctx->fimc_dev;
328 u32 cfg;
329
330 cfg = readl(dev->regs + S5P_CIIMGCPT);
331 /* One shot mode for output DMA or freerun for FIFO. */
332 if (ctx->out_path == FIMC_DMA)
333 cfg |= S5P_CIIMGCPT_CPT_FREN_ENABLE;
334 else
335 cfg &= ~S5P_CIIMGCPT_CPT_FREN_ENABLE;
336
337 if (ctx->scaler.enabled)
338 cfg |= S5P_CIIMGCPT_IMGCPTEN_SC;
339
340 writel(cfg | S5P_CIIMGCPT_IMGCPTEN, dev->regs + S5P_CIIMGCPT);
341}
342
343void fimc_hw_set_effect(struct fimc_ctx *ctx)
344{
345 struct fimc_dev *dev = ctx->fimc_dev;
346 struct fimc_effect *effect = &ctx->effect;
347 u32 cfg = (S5P_CIIMGEFF_IE_ENABLE | S5P_CIIMGEFF_IE_SC_AFTER);
348
349 cfg |= effect->type;
350
351 if (effect->type == S5P_FIMC_EFFECT_ARBITRARY) {
352 cfg |= S5P_CIIMGEFF_PAT_CB(effect->pat_cb);
353 cfg |= S5P_CIIMGEFF_PAT_CR(effect->pat_cr);
354 }
355
356 writel(cfg, dev->regs + S5P_CIIMGEFF);
357}
358
359static void fimc_hw_set_in_dma_size(struct fimc_ctx *ctx)
360{
361 struct fimc_dev *dev = ctx->fimc_dev;
362 struct fimc_frame *frame = &ctx->s_frame;
363 u32 cfg_o = 0;
364 u32 cfg_r = 0;
365
366 if (FIMC_LCDFIFO == ctx->out_path)
367 cfg_r |= S5P_CIREAL_ISIZE_AUTOLOAD_EN;
368
369 cfg_o |= S5P_ORIG_SIZE_HOR(frame->f_width);
370 cfg_o |= S5P_ORIG_SIZE_VER(frame->f_height);
371 cfg_r |= S5P_CIREAL_ISIZE_WIDTH(frame->width);
372 cfg_r |= S5P_CIREAL_ISIZE_HEIGHT(frame->height);
373
374 writel(cfg_o, dev->regs + S5P_ORGISIZE);
375 writel(cfg_r, dev->regs + S5P_CIREAL_ISIZE);
376}
377
378void fimc_hw_set_in_dma(struct fimc_ctx *ctx)
379{
380 struct fimc_dev *dev = ctx->fimc_dev;
381 struct fimc_frame *frame = &ctx->s_frame;
382 struct fimc_dma_offset *offset = &frame->dma_offset;
383 u32 cfg = 0;
384
385 /* Set the pixel offsets. */
386 cfg |= S5P_CIO_OFFS_HOR(offset->y_h);
387 cfg |= S5P_CIO_OFFS_VER(offset->y_v);
388 writel(cfg, dev->regs + S5P_CIIYOFF);
389
390 cfg = 0;
391 cfg |= S5P_CIO_OFFS_HOR(offset->cb_h);
392 cfg |= S5P_CIO_OFFS_VER(offset->cb_v);
393 writel(cfg, dev->regs + S5P_CIICBOFF);
394
395 cfg = 0;
396 cfg |= S5P_CIO_OFFS_HOR(offset->cr_h);
397 cfg |= S5P_CIO_OFFS_VER(offset->cr_v);
398 writel(cfg, dev->regs + S5P_CIICROFF);
399
400 /* Input original and real size. */
401 fimc_hw_set_in_dma_size(ctx);
402
403 /* Autoload is used currently only in FIFO mode. */
404 fimc_hw_en_autoload(dev, ctx->out_path == FIMC_LCDFIFO);
405
406 /* Set the input DMA to process single frame only. */
407 cfg = readl(dev->regs + S5P_MSCTRL);
408 cfg &= ~(S5P_MSCTRL_FLIP_MASK
409 | S5P_MSCTRL_INFORMAT_MASK
410 | S5P_MSCTRL_IN_BURST_COUNT_MASK
411 | S5P_MSCTRL_INPUT_MASK
412 | S5P_MSCTRL_C_INT_IN_MASK
413 | S5P_MSCTRL_2P_IN_ORDER_MASK);
414
415 cfg |= (S5P_MSCTRL_FRAME_COUNT(1) | S5P_MSCTRL_INPUT_MEMORY);
416
417 switch (frame->fmt->color) {
418 case S5P_FIMC_RGB565:
419 case S5P_FIMC_RGB666:
420 case S5P_FIMC_RGB888:
421 cfg |= S5P_MSCTRL_INFORMAT_RGB;
422 break;
423 case S5P_FIMC_YCBCR420:
424 cfg |= S5P_MSCTRL_INFORMAT_YCBCR420;
425
426 if (frame->fmt->planes_cnt == 2)
427 cfg |= ctx->in_order_2p | S5P_MSCTRL_C_INT_IN_2PLANE;
428 else
429 cfg |= S5P_MSCTRL_C_INT_IN_3PLANE;
430
431 break;
432 case S5P_FIMC_YCBYCR422:
433 case S5P_FIMC_YCRYCB422:
434 case S5P_FIMC_CBYCRY422:
435 case S5P_FIMC_CRYCBY422:
436 if (frame->fmt->planes_cnt == 1) {
437 cfg |= ctx->in_order_1p
438 | S5P_MSCTRL_INFORMAT_YCBCR422_1P;
439 } else {
440 cfg |= S5P_MSCTRL_INFORMAT_YCBCR422;
441
442 if (frame->fmt->planes_cnt == 2)
443 cfg |= ctx->in_order_2p
444 | S5P_MSCTRL_C_INT_IN_2PLANE;
445 else
446 cfg |= S5P_MSCTRL_C_INT_IN_3PLANE;
447 }
448 break;
449 default:
450 break;
451 }
452
453 /*
454 * Input DMA flip mode (and rotation).
455 * Do not allow simultaneous rotation and flipping.
456 */
457 if (!ctx->rotation && ctx->out_path == FIMC_LCDFIFO)
458 cfg |= fimc_hw_get_in_flip(ctx->flip);
459
460 writel(cfg, dev->regs + S5P_MSCTRL);
461
462 /* Input/output DMA linear/tiled mode. */
463 cfg = readl(dev->regs + S5P_CIDMAPARAM);
464 cfg &= ~S5P_CIDMAPARAM_TILE_MASK;
465
466 if (tiled_fmt(ctx->s_frame.fmt))
467 cfg |= S5P_CIDMAPARAM_R_64X32;
468
469 if (tiled_fmt(ctx->d_frame.fmt))
470 cfg |= S5P_CIDMAPARAM_W_64X32;
471
472 writel(cfg, dev->regs + S5P_CIDMAPARAM);
473}
474
475
476void fimc_hw_set_input_path(struct fimc_ctx *ctx)
477{
478 struct fimc_dev *dev = ctx->fimc_dev;
479
480 u32 cfg = readl(dev->regs + S5P_MSCTRL);
481 cfg &= ~S5P_MSCTRL_INPUT_MASK;
482
483 if (ctx->in_path == FIMC_DMA)
484 cfg |= S5P_MSCTRL_INPUT_MEMORY;
485 else
486 cfg |= S5P_MSCTRL_INPUT_EXTCAM;
487
488 writel(cfg, dev->regs + S5P_MSCTRL);
489}
490
491void fimc_hw_set_output_path(struct fimc_ctx *ctx)
492{
493 struct fimc_dev *dev = ctx->fimc_dev;
494
495 u32 cfg = readl(dev->regs + S5P_CISCCTRL);
496 cfg &= ~S5P_CISCCTRL_LCDPATHEN_FIFO;
497 if (ctx->out_path == FIMC_LCDFIFO)
498 cfg |= S5P_CISCCTRL_LCDPATHEN_FIFO;
499 writel(cfg, dev->regs + S5P_CISCCTRL);
500}
501
502void fimc_hw_set_input_addr(struct fimc_dev *dev, struct fimc_addr *paddr)
503{
504 u32 cfg = 0;
505
506 cfg = readl(dev->regs + S5P_CIREAL_ISIZE);
507 cfg |= S5P_CIREAL_ISIZE_ADDR_CH_DIS;
508 writel(cfg, dev->regs + S5P_CIREAL_ISIZE);
509
510 writel(paddr->y, dev->regs + S5P_CIIYSA0);
511 writel(paddr->cb, dev->regs + S5P_CIICBSA0);
512 writel(paddr->cr, dev->regs + S5P_CIICRSA0);
513
514 cfg &= ~S5P_CIREAL_ISIZE_ADDR_CH_DIS;
515 writel(cfg, dev->regs + S5P_CIREAL_ISIZE);
516}
517
518void fimc_hw_set_output_addr(struct fimc_dev *dev, struct fimc_addr *paddr)
519{
520 int i;
521 /* Set all the output register sets to point to single video buffer. */
522 for (i = 0; i < FIMC_MAX_OUT_BUFS; i++) {
523 writel(paddr->y, dev->regs + S5P_CIOYSA(i));
524 writel(paddr->cb, dev->regs + S5P_CIOCBSA(i));
525 writel(paddr->cr, dev->regs + S5P_CIOCRSA(i));
526 }
527}
diff --git a/drivers/media/video/s5p-fimc/regs-fimc.h b/drivers/media/video/s5p-fimc/regs-fimc.h
new file mode 100644
index 00000000000..a3cfe824db0
--- /dev/null
+++ b/drivers/media/video/s5p-fimc/regs-fimc.h
@@ -0,0 +1,293 @@
1/*
2 * Register definition file for Samsung Camera Interface (FIMC) driver
3 *
4 * Copyright (c) 2010 Samsung Electronics
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 version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#ifndef REGS_FIMC_H_
12#define REGS_FIMC_H_
13
14#define S5P_CIOYSA(__x) (0x18 + (__x) * 4)
15#define S5P_CIOCBSA(__x) (0x28 + (__x) * 4)
16#define S5P_CIOCRSA(__x) (0x38 + (__x) * 4)
17
18/* Input source format */
19#define S5P_CISRCFMT 0x00
20#define S5P_CISRCFMT_ITU601_8BIT (1 << 31)
21#define S5P_CISRCFMT_ITU601_16BIT (1 << 29)
22#define S5P_CISRCFMT_ORDER422_YCBYCR (0 << 14)
23#define S5P_CISRCFMT_ORDER422_YCRYCB (1 << 14)
24#define S5P_CISRCFMT_ORDER422_CBYCRY (2 << 14)
25#define S5P_CISRCFMT_ORDER422_CRYCBY (3 << 14)
26#define S5P_CISRCFMT_HSIZE(x) ((x) << 16)
27#define S5P_CISRCFMT_VSIZE(x) ((x) << 0)
28
29/* Window offset */
30#define S5P_CIWDOFST 0x04
31#define S5P_CIWDOFST_WINOFSEN (1 << 31)
32#define S5P_CIWDOFST_CLROVFIY (1 << 30)
33#define S5P_CIWDOFST_CLROVRLB (1 << 29)
34#define S5P_CIWDOFST_WINHOROFST_MASK (0x7ff << 16)
35#define S5P_CIWDOFST_CLROVFICB (1 << 15)
36#define S5P_CIWDOFST_CLROVFICR (1 << 14)
37#define S5P_CIWDOFST_WINHOROFST(x) ((x) << 16)
38#define S5P_CIWDOFST_WINVEROFST(x) ((x) << 0)
39#define S5P_CIWDOFST_WINVEROFST_MASK (0xfff << 0)
40
41/* Global control */
42#define S5P_CIGCTRL 0x08
43#define S5P_CIGCTRL_SWRST (1 << 31)
44#define S5P_CIGCTRL_CAMRST_A (1 << 30)
45#define S5P_CIGCTRL_SELCAM_ITU_A (1 << 29)
46#define S5P_CIGCTRL_SELCAM_ITU_MASK (1 << 29)
47#define S5P_CIGCTRL_TESTPAT_NORMAL (0 << 27)
48#define S5P_CIGCTRL_TESTPAT_COLOR_BAR (1 << 27)
49#define S5P_CIGCTRL_TESTPAT_HOR_INC (2 << 27)
50#define S5P_CIGCTRL_TESTPAT_VER_INC (3 << 27)
51#define S5P_CIGCTRL_TESTPAT_MASK (3 << 27)
52#define S5P_CIGCTRL_TESTPAT_SHIFT (27)
53#define S5P_CIGCTRL_INVPOLPCLK (1 << 26)
54#define S5P_CIGCTRL_INVPOLVSYNC (1 << 25)
55#define S5P_CIGCTRL_INVPOLHREF (1 << 24)
56#define S5P_CIGCTRL_IRQ_OVFEN (1 << 22)
57#define S5P_CIGCTRL_HREF_MASK (1 << 21)
58#define S5P_CIGCTRL_IRQ_LEVEL (1 << 20)
59#define S5P_CIGCTRL_IRQ_CLR (1 << 19)
60#define S5P_CIGCTRL_IRQ_ENABLE (1 << 16)
61#define S5P_CIGCTRL_SHDW_DISABLE (1 << 12)
62#define S5P_CIGCTRL_SELCAM_MIPI_A (1 << 7)
63#define S5P_CIGCTRL_CAMIF_SELWB (1 << 6)
64#define S5P_CIGCTRL_INVPOLHSYNC (1 << 4)
65#define S5P_CIGCTRL_SELCAM_MIPI (1 << 3)
66#define S5P_CIGCTRL_INTERLACE (1 << 0)
67
68/* Window offset 2 */
69#define S5P_CIWDOFST2 0x14
70#define S5P_CIWDOFST2_HOROFF_MASK (0xfff << 16)
71#define S5P_CIWDOFST2_VEROFF_MASK (0xfff << 0)
72#define S5P_CIWDOFST2_HOROFF(x) ((x) << 16)
73#define S5P_CIWDOFST2_VEROFF(x) ((x) << 0)
74
75/* Output DMA Y plane start address */
76#define S5P_CIOYSA1 0x18
77#define S5P_CIOYSA2 0x1c
78#define S5P_CIOYSA3 0x20
79#define S5P_CIOYSA4 0x24
80
81/* Output DMA Cb plane start address */
82#define S5P_CIOCBSA1 0x28
83#define S5P_CIOCBSA2 0x2c
84#define S5P_CIOCBSA3 0x30
85#define S5P_CIOCBSA4 0x34
86
87/* Output DMA Cr plane start address */
88#define S5P_CIOCRSA1 0x38
89#define S5P_CIOCRSA2 0x3c
90#define S5P_CIOCRSA3 0x40
91#define S5P_CIOCRSA4 0x44
92
93/* Target image format */
94#define S5P_CITRGFMT 0x48
95#define S5P_CITRGFMT_INROT90 (1 << 31)
96#define S5P_CITRGFMT_YCBCR420 (0 << 29)
97#define S5P_CITRGFMT_YCBCR422 (1 << 29)
98#define S5P_CITRGFMT_YCBCR422_1P (2 << 29)
99#define S5P_CITRGFMT_RGB (3 << 29)
100#define S5P_CITRGFMT_FMT_MASK (3 << 29)
101#define S5P_CITRGFMT_HSIZE_MASK (0xfff << 16)
102#define S5P_CITRGFMT_FLIP_SHIFT (14)
103#define S5P_CITRGFMT_FLIP_NORMAL (0 << 14)
104#define S5P_CITRGFMT_FLIP_X_MIRROR (1 << 14)
105#define S5P_CITRGFMT_FLIP_Y_MIRROR (2 << 14)
106#define S5P_CITRGFMT_FLIP_180 (3 << 14)
107#define S5P_CITRGFMT_FLIP_MASK (3 << 14)
108#define S5P_CITRGFMT_OUTROT90 (1 << 13)
109#define S5P_CITRGFMT_VSIZE_MASK (0xfff << 0)
110#define S5P_CITRGFMT_HSIZE(x) ((x) << 16)
111#define S5P_CITRGFMT_VSIZE(x) ((x) << 0)
112
113/* Output DMA control */
114#define S5P_CIOCTRL 0x4c
115#define S5P_CIOCTRL_ORDER422_MASK (3 << 0)
116#define S5P_CIOCTRL_ORDER422_CRYCBY (0 << 0)
117#define S5P_CIOCTRL_ORDER422_YCRYCB (1 << 0)
118#define S5P_CIOCTRL_ORDER422_CBYCRY (2 << 0)
119#define S5P_CIOCTRL_ORDER422_YCBYCR (3 << 0)
120#define S5P_CIOCTRL_LASTIRQ_ENABLE (1 << 2)
121#define S5P_CIOCTRL_YCBCR_3PLANE (0 << 3)
122#define S5P_CIOCTRL_YCBCR_2PLANE (1 << 3)
123#define S5P_CIOCTRL_YCBCR_PLANE_MASK (1 << 3)
124#define S5P_CIOCTRL_ORDER2P_SHIFT (24)
125#define S5P_CIOCTRL_ORDER2P_MASK (3 << 24)
126#define S5P_CIOCTRL_ORDER422_2P_LSB_CRCB (0 << 24)
127
128/* Pre-scaler control 1 */
129#define S5P_CISCPRERATIO 0x50
130#define S5P_CISCPRERATIO_SHFACTOR(x) ((x) << 28)
131#define S5P_CISCPRERATIO_HOR(x) ((x) << 16)
132#define S5P_CISCPRERATIO_VER(x) ((x) << 0)
133
134#define S5P_CISCPREDST 0x54
135#define S5P_CISCPREDST_WIDTH(x) ((x) << 16)
136#define S5P_CISCPREDST_HEIGHT(x) ((x) << 0)
137
138/* Main scaler control */
139#define S5P_CISCCTRL 0x58
140#define S5P_CISCCTRL_SCALERBYPASS (1 << 31)
141#define S5P_CISCCTRL_SCALEUP_H (1 << 30)
142#define S5P_CISCCTRL_SCALEUP_V (1 << 29)
143#define S5P_CISCCTRL_CSCR2Y_WIDE (1 << 28)
144#define S5P_CISCCTRL_CSCY2R_WIDE (1 << 27)
145#define S5P_CISCCTRL_LCDPATHEN_FIFO (1 << 26)
146#define S5P_CISCCTRL_INTERLACE (1 << 25)
147#define S5P_CISCCTRL_SCALERSTART (1 << 15)
148#define S5P_CISCCTRL_INRGB_FMT_RGB565 (0 << 13)
149#define S5P_CISCCTRL_INRGB_FMT_RGB666 (1 << 13)
150#define S5P_CISCCTRL_INRGB_FMT_RGB888 (2 << 13)
151#define S5P_CISCCTRL_INRGB_FMT_MASK (3 << 13)
152#define S5P_CISCCTRL_OUTRGB_FMT_RGB565 (0 << 11)
153#define S5P_CISCCTRL_OUTRGB_FMT_RGB666 (1 << 11)
154#define S5P_CISCCTRL_OUTRGB_FMT_RGB888 (2 << 11)
155#define S5P_CISCCTRL_OUTRGB_FMT_MASK (3 << 11)
156#define S5P_CISCCTRL_RGB_EXT (1 << 10)
157#define S5P_CISCCTRL_ONE2ONE (1 << 9)
158#define S5P_CISCCTRL_SC_HORRATIO(x) ((x) << 16)
159#define S5P_CISCCTRL_SC_VERRATIO(x) ((x) << 0)
160
161/* Target area */
162#define S5P_CITAREA 0x5c
163#define S5P_CITAREA_MASK 0x0fffffff
164
165/* General status */
166#define S5P_CISTATUS 0x64
167#define S5P_CISTATUS_OVFIY (1 << 31)
168#define S5P_CISTATUS_OVFICB (1 << 30)
169#define S5P_CISTATUS_OVFICR (1 << 29)
170#define S5P_CISTATUS_VSYNC (1 << 28)
171#define S5P_CISTATUS_WINOFF_EN (1 << 25)
172#define S5P_CISTATUS_IMGCPT_EN (1 << 22)
173#define S5P_CISTATUS_IMGCPT_SCEN (1 << 21)
174#define S5P_CISTATUS_VSYNC_A (1 << 20)
175#define S5P_CISTATUS_VSYNC_B (1 << 19)
176#define S5P_CISTATUS_OVRLB (1 << 18)
177#define S5P_CISTATUS_FRAME_END (1 << 17)
178#define S5P_CISTATUS_LASTCAPT_END (1 << 16)
179#define S5P_CISTATUS_VVALID_A (1 << 15)
180#define S5P_CISTATUS_VVALID_B (1 << 14)
181
182/* Image capture control */
183#define S5P_CIIMGCPT 0xc0
184#define S5P_CIIMGCPT_IMGCPTEN (1 << 31)
185#define S5P_CIIMGCPT_IMGCPTEN_SC (1 << 30)
186#define S5P_CIIMGCPT_CPT_FREN_ENABLE (1 << 25)
187#define S5P_CIIMGCPT_CPT_FRMOD_CNT (1 << 18)
188
189/* Frame capture sequence */
190#define S5P_CICPTSEQ 0xc4
191
192/* Image effect */
193#define S5P_CIIMGEFF 0xd0
194#define S5P_CIIMGEFF_IE_DISABLE (0 << 30)
195#define S5P_CIIMGEFF_IE_ENABLE (1 << 30)
196#define S5P_CIIMGEFF_IE_SC_BEFORE (0 << 29)
197#define S5P_CIIMGEFF_IE_SC_AFTER (1 << 29)
198#define S5P_CIIMGEFF_FIN_BYPASS (0 << 26)
199#define S5P_CIIMGEFF_FIN_ARBITRARY (1 << 26)
200#define S5P_CIIMGEFF_FIN_NEGATIVE (2 << 26)
201#define S5P_CIIMGEFF_FIN_ARTFREEZE (3 << 26)
202#define S5P_CIIMGEFF_FIN_EMBOSSING (4 << 26)
203#define S5P_CIIMGEFF_FIN_SILHOUETTE (5 << 26)
204#define S5P_CIIMGEFF_FIN_MASK (7 << 26)
205#define S5P_CIIMGEFF_PAT_CBCR_MASK ((0xff < 13) | (0xff < 0))
206#define S5P_CIIMGEFF_PAT_CB(x) ((x) << 13)
207#define S5P_CIIMGEFF_PAT_CR(x) ((x) << 0)
208
209/* Input DMA Y/Cb/Cr plane start address 0 */
210#define S5P_CIIYSA0 0xd4
211#define S5P_CIICBSA0 0xd8
212#define S5P_CIICRSA0 0xdc
213
214/* Real input DMA image size */
215#define S5P_CIREAL_ISIZE 0xf8
216#define S5P_CIREAL_ISIZE_AUTOLOAD_EN (1 << 31)
217#define S5P_CIREAL_ISIZE_ADDR_CH_DIS (1 << 30)
218#define S5P_CIREAL_ISIZE_HEIGHT(x) ((x) << 16)
219#define S5P_CIREAL_ISIZE_WIDTH(x) ((x) << 0)
220
221
222/* Input DMA control */
223#define S5P_MSCTRL 0xfc
224#define S5P_MSCTRL_IN_BURST_COUNT_MASK (3 << 24)
225#define S5P_MSCTRL_2P_IN_ORDER_MASK (3 << 16)
226#define S5P_MSCTRL_2P_IN_ORDER_SHIFT 16
227#define S5P_MSCTRL_C_INT_IN_3PLANE (0 << 15)
228#define S5P_MSCTRL_C_INT_IN_2PLANE (1 << 15)
229#define S5P_MSCTRL_C_INT_IN_MASK (1 << 15)
230#define S5P_MSCTRL_FLIP_SHIFT 13
231#define S5P_MSCTRL_FLIP_MASK (3 << 13)
232#define S5P_MSCTRL_FLIP_NORMAL (0 << 13)
233#define S5P_MSCTRL_FLIP_X_MIRROR (1 << 13)
234#define S5P_MSCTRL_FLIP_Y_MIRROR (2 << 13)
235#define S5P_MSCTRL_FLIP_180 (3 << 13)
236#define S5P_MSCTRL_ORDER422_SHIFT 4
237#define S5P_MSCTRL_ORDER422_CRYCBY (0 << 4)
238#define S5P_MSCTRL_ORDER422_YCRYCB (1 << 4)
239#define S5P_MSCTRL_ORDER422_CBYCRY (2 << 4)
240#define S5P_MSCTRL_ORDER422_YCBYCR (3 << 4)
241#define S5P_MSCTRL_ORDER422_MASK (3 << 4)
242#define S5P_MSCTRL_INPUT_EXTCAM (0 << 3)
243#define S5P_MSCTRL_INPUT_MEMORY (1 << 3)
244#define S5P_MSCTRL_INPUT_MASK (1 << 3)
245#define S5P_MSCTRL_INFORMAT_YCBCR420 (0 << 1)
246#define S5P_MSCTRL_INFORMAT_YCBCR422 (1 << 1)
247#define S5P_MSCTRL_INFORMAT_YCBCR422_1P (2 << 1)
248#define S5P_MSCTRL_INFORMAT_RGB (3 << 1)
249#define S5P_MSCTRL_INFORMAT_MASK (3 << 1)
250#define S5P_MSCTRL_ENVID (1 << 0)
251#define S5P_MSCTRL_FRAME_COUNT(x) ((x) << 24)
252
253/* Input DMA Y/Cb/Cr plane start address 1 */
254#define S5P_CIIYSA1 0x144
255#define S5P_CIICBSA1 0x148
256#define S5P_CIICRSA1 0x14c
257
258/* Output DMA Y/Cb/Cr offset */
259#define S5P_CIOYOFF 0x168
260#define S5P_CIOCBOFF 0x16c
261#define S5P_CIOCROFF 0x170
262
263/* Input DMA Y/Cb/Cr offset */
264#define S5P_CIIYOFF 0x174
265#define S5P_CIICBOFF 0x178
266#define S5P_CIICROFF 0x17c
267
268#define S5P_CIO_OFFS_VER(x) ((x) << 16)
269#define S5P_CIO_OFFS_HOR(x) ((x) << 0)
270
271/* Input DMA original image size */
272#define S5P_ORGISIZE 0x180
273
274/* Output DMA original image size */
275#define S5P_ORGOSIZE 0x184
276
277#define S5P_ORIG_SIZE_VER(x) ((x) << 16)
278#define S5P_ORIG_SIZE_HOR(x) ((x) << 0)
279
280/* Real output DMA image size (extension register) */
281#define S5P_CIEXTEN 0x188
282
283#define S5P_CIDMAPARAM 0x18c
284#define S5P_CIDMAPARAM_R_LINEAR (0 << 29)
285#define S5P_CIDMAPARAM_R_64X32 (3 << 29)
286#define S5P_CIDMAPARAM_W_LINEAR (0 << 13)
287#define S5P_CIDMAPARAM_W_64X32 (3 << 13)
288#define S5P_CIDMAPARAM_TILE_MASK ((3 << 29) | (3 << 13))
289
290/* MIPI CSI image format */
291#define S5P_CSIIMGFMT 0x194
292
293#endif /* REGS_FIMC_H_ */
diff --git a/drivers/media/video/saa7115.c b/drivers/media/video/saa7115.c
index 76da7436868..ee963f4d01b 100644
--- a/drivers/media/video/saa7115.c
+++ b/drivers/media/video/saa7115.c
@@ -45,6 +45,7 @@
45#include <linux/i2c.h> 45#include <linux/i2c.h>
46#include <linux/videodev2.h> 46#include <linux/videodev2.h>
47#include <media/v4l2-device.h> 47#include <media/v4l2-device.h>
48#include <media/v4l2-ctrls.h>
48#include <media/v4l2-chip-ident.h> 49#include <media/v4l2-chip-ident.h>
49#include <media/v4l2-i2c-drv.h> 50#include <media/v4l2-i2c-drv.h>
50#include <media/saa7115.h> 51#include <media/saa7115.h>
@@ -65,16 +66,19 @@ MODULE_PARM_DESC(debug, "Debug level (0-1)");
65 66
66struct saa711x_state { 67struct saa711x_state {
67 struct v4l2_subdev sd; 68 struct v4l2_subdev sd;
69 struct v4l2_ctrl_handler hdl;
70
71 struct {
72 /* chroma gain control cluster */
73 struct v4l2_ctrl *agc;
74 struct v4l2_ctrl *gain;
75 };
76
68 v4l2_std_id std; 77 v4l2_std_id std;
69 int input; 78 int input;
70 int output; 79 int output;
71 int enable; 80 int enable;
72 int radio; 81 int radio;
73 int bright;
74 int contrast;
75 int hue;
76 int sat;
77 int chroma_agc;
78 int width; 82 int width;
79 int height; 83 int height;
80 u32 ident; 84 u32 ident;
@@ -90,6 +94,11 @@ static inline struct saa711x_state *to_state(struct v4l2_subdev *sd)
90 return container_of(sd, struct saa711x_state, sd); 94 return container_of(sd, struct saa711x_state, sd);
91} 95}
92 96
97static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
98{
99 return &container_of(ctrl->handler, struct saa711x_state, hdl)->sd;
100}
101
93/* ----------------------------------------------------------------------- */ 102/* ----------------------------------------------------------------------- */
94 103
95static inline int saa711x_write(struct v4l2_subdev *sd, u8 reg, u8 value) 104static inline int saa711x_write(struct v4l2_subdev *sd, u8 reg, u8 value)
@@ -741,96 +750,53 @@ static int saa711x_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
741 return 0; 750 return 0;
742} 751}
743 752
744static int saa711x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 753static int saa711x_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
745{ 754{
755 struct v4l2_subdev *sd = to_sd(ctrl);
746 struct saa711x_state *state = to_state(sd); 756 struct saa711x_state *state = to_state(sd);
747 u8 val;
748 757
749 switch (ctrl->id) { 758 switch (ctrl->id) {
750 case V4L2_CID_BRIGHTNESS:
751 if (ctrl->value < 0 || ctrl->value > 255) {
752 v4l2_err(sd, "invalid brightness setting %d\n", ctrl->value);
753 return -ERANGE;
754 }
755
756 state->bright = ctrl->value;
757 saa711x_write(sd, R_0A_LUMA_BRIGHT_CNTL, state->bright);
758 break;
759
760 case V4L2_CID_CONTRAST:
761 if (ctrl->value < 0 || ctrl->value > 127) {
762 v4l2_err(sd, "invalid contrast setting %d\n", ctrl->value);
763 return -ERANGE;
764 }
765
766 state->contrast = ctrl->value;
767 saa711x_write(sd, R_0B_LUMA_CONTRAST_CNTL, state->contrast);
768 break;
769
770 case V4L2_CID_SATURATION:
771 if (ctrl->value < 0 || ctrl->value > 127) {
772 v4l2_err(sd, "invalid saturation setting %d\n", ctrl->value);
773 return -ERANGE;
774 }
775
776 state->sat = ctrl->value;
777 saa711x_write(sd, R_0C_CHROMA_SAT_CNTL, state->sat);
778 break;
779
780 case V4L2_CID_HUE:
781 if (ctrl->value < -128 || ctrl->value > 127) {
782 v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
783 return -ERANGE;
784 }
785
786 state->hue = ctrl->value;
787 saa711x_write(sd, R_0D_CHROMA_HUE_CNTL, state->hue);
788 break;
789 case V4L2_CID_CHROMA_AGC: 759 case V4L2_CID_CHROMA_AGC:
790 val = saa711x_read(sd, R_0F_CHROMA_GAIN_CNTL); 760 /* chroma gain cluster */
791 state->chroma_agc = ctrl->value; 761 if (state->agc->cur.val)
792 if (ctrl->value) 762 state->gain->cur.val =
793 val &= 0x7f; 763 saa711x_read(sd, R_0F_CHROMA_GAIN_CNTL) & 0x7f;
794 else
795 val |= 0x80;
796 saa711x_write(sd, R_0F_CHROMA_GAIN_CNTL, val);
797 break; 764 break;
798 case V4L2_CID_CHROMA_GAIN:
799 /* Chroma gain cannot be set when AGC is enabled */
800 if (state->chroma_agc == 1)
801 return -EINVAL;
802 saa711x_write(sd, R_0F_CHROMA_GAIN_CNTL, ctrl->value | 0x80);
803 break;
804 default:
805 return -EINVAL;
806 } 765 }
807
808 return 0; 766 return 0;
809} 767}
810 768
811static int saa711x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 769static int saa711x_s_ctrl(struct v4l2_ctrl *ctrl)
812{ 770{
771 struct v4l2_subdev *sd = to_sd(ctrl);
813 struct saa711x_state *state = to_state(sd); 772 struct saa711x_state *state = to_state(sd);
814 773
815 switch (ctrl->id) { 774 switch (ctrl->id) {
816 case V4L2_CID_BRIGHTNESS: 775 case V4L2_CID_BRIGHTNESS:
817 ctrl->value = state->bright; 776 saa711x_write(sd, R_0A_LUMA_BRIGHT_CNTL, ctrl->val);
818 break; 777 break;
778
819 case V4L2_CID_CONTRAST: 779 case V4L2_CID_CONTRAST:
820 ctrl->value = state->contrast; 780 saa711x_write(sd, R_0B_LUMA_CONTRAST_CNTL, ctrl->val);
821 break; 781 break;
782
822 case V4L2_CID_SATURATION: 783 case V4L2_CID_SATURATION:
823 ctrl->value = state->sat; 784 saa711x_write(sd, R_0C_CHROMA_SAT_CNTL, ctrl->val);
824 break; 785 break;
786
825 case V4L2_CID_HUE: 787 case V4L2_CID_HUE:
826 ctrl->value = state->hue; 788 saa711x_write(sd, R_0D_CHROMA_HUE_CNTL, ctrl->val);
827 break; 789 break;
790
828 case V4L2_CID_CHROMA_AGC: 791 case V4L2_CID_CHROMA_AGC:
829 ctrl->value = state->chroma_agc; 792 /* chroma gain cluster */
830 break; 793 if (state->agc->val)
831 case V4L2_CID_CHROMA_GAIN: 794 saa711x_write(sd, R_0F_CHROMA_GAIN_CNTL, state->gain->val);
832 ctrl->value = saa711x_read(sd, R_0F_CHROMA_GAIN_CNTL) & 0x7f; 795 else
796 saa711x_write(sd, R_0F_CHROMA_GAIN_CNTL, state->gain->val | 0x80);
797 v4l2_ctrl_activate(state->gain, !state->agc->val);
833 break; 798 break;
799
834 default: 800 default:
835 return -EINVAL; 801 return -EINVAL;
836 } 802 }
@@ -1223,25 +1189,6 @@ static int saa711x_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1223 return 0; 1189 return 0;
1224} 1190}
1225 1191
1226static int saa711x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1227{
1228 switch (qc->id) {
1229 case V4L2_CID_BRIGHTNESS:
1230 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
1231 case V4L2_CID_CONTRAST:
1232 case V4L2_CID_SATURATION:
1233 return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
1234 case V4L2_CID_HUE:
1235 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
1236 case V4L2_CID_CHROMA_AGC:
1237 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1238 case V4L2_CID_CHROMA_GAIN:
1239 return v4l2_ctrl_query_fill(qc, 0, 127, 1, 48);
1240 default:
1241 return -EINVAL;
1242 }
1243}
1244
1245static int saa711x_s_std(struct v4l2_subdev *sd, v4l2_std_id std) 1192static int saa711x_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1246{ 1193{
1247 struct saa711x_state *state = to_state(sd); 1194 struct saa711x_state *state = to_state(sd);
@@ -1518,17 +1465,27 @@ static int saa711x_log_status(struct v4l2_subdev *sd)
1518 break; 1465 break;
1519 } 1466 }
1520 v4l2_info(sd, "Width, Height: %d, %d\n", state->width, state->height); 1467 v4l2_info(sd, "Width, Height: %d, %d\n", state->width, state->height);
1468 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
1521 return 0; 1469 return 0;
1522} 1470}
1523 1471
1524/* ----------------------------------------------------------------------- */ 1472/* ----------------------------------------------------------------------- */
1525 1473
1474static const struct v4l2_ctrl_ops saa711x_ctrl_ops = {
1475 .s_ctrl = saa711x_s_ctrl,
1476 .g_volatile_ctrl = saa711x_g_volatile_ctrl,
1477};
1478
1526static const struct v4l2_subdev_core_ops saa711x_core_ops = { 1479static const struct v4l2_subdev_core_ops saa711x_core_ops = {
1527 .log_status = saa711x_log_status, 1480 .log_status = saa711x_log_status,
1528 .g_chip_ident = saa711x_g_chip_ident, 1481 .g_chip_ident = saa711x_g_chip_ident,
1529 .g_ctrl = saa711x_g_ctrl, 1482 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1530 .s_ctrl = saa711x_s_ctrl, 1483 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1531 .queryctrl = saa711x_queryctrl, 1484 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1485 .g_ctrl = v4l2_subdev_g_ctrl,
1486 .s_ctrl = v4l2_subdev_s_ctrl,
1487 .queryctrl = v4l2_subdev_queryctrl,
1488 .querymenu = v4l2_subdev_querymenu,
1532 .s_std = saa711x_s_std, 1489 .s_std = saa711x_s_std,
1533 .reset = saa711x_reset, 1490 .reset = saa711x_reset,
1534 .s_gpio = saa711x_s_gpio, 1491 .s_gpio = saa711x_s_gpio,
@@ -1579,8 +1536,9 @@ static int saa711x_probe(struct i2c_client *client,
1579{ 1536{
1580 struct saa711x_state *state; 1537 struct saa711x_state *state;
1581 struct v4l2_subdev *sd; 1538 struct v4l2_subdev *sd;
1582 int i; 1539 struct v4l2_ctrl_handler *hdl;
1583 char name[17]; 1540 int i;
1541 char name[17];
1584 char chip_id; 1542 char chip_id;
1585 int autodetect = !id || id->driver_data == 1; 1543 int autodetect = !id || id->driver_data == 1;
1586 1544
@@ -1619,15 +1577,38 @@ static int saa711x_probe(struct i2c_client *client,
1619 return -ENOMEM; 1577 return -ENOMEM;
1620 sd = &state->sd; 1578 sd = &state->sd;
1621 v4l2_i2c_subdev_init(sd, client, &saa711x_ops); 1579 v4l2_i2c_subdev_init(sd, client, &saa711x_ops);
1580
1581 hdl = &state->hdl;
1582 v4l2_ctrl_handler_init(hdl, 6);
1583 /* add in ascending ID order */
1584 v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
1585 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1586 v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
1587 V4L2_CID_CONTRAST, 0, 127, 1, 64);
1588 v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
1589 V4L2_CID_SATURATION, 0, 127, 1, 64);
1590 v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
1591 V4L2_CID_HUE, -128, 127, 1, 0);
1592 state->agc = v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
1593 V4L2_CID_CHROMA_AGC, 0, 1, 1, 1);
1594 state->gain = v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
1595 V4L2_CID_CHROMA_GAIN, 0, 127, 1, 40);
1596 state->gain->is_volatile = 1;
1597 sd->ctrl_handler = hdl;
1598 if (hdl->error) {
1599 int err = hdl->error;
1600
1601 v4l2_ctrl_handler_free(hdl);
1602 kfree(state);
1603 return err;
1604 }
1605 state->agc->flags |= V4L2_CTRL_FLAG_UPDATE;
1606 v4l2_ctrl_cluster(2, &state->agc);
1607
1622 state->input = -1; 1608 state->input = -1;
1623 state->output = SAA7115_IPORT_ON; 1609 state->output = SAA7115_IPORT_ON;
1624 state->enable = 1; 1610 state->enable = 1;
1625 state->radio = 0; 1611 state->radio = 0;
1626 state->bright = 128;
1627 state->contrast = 64;
1628 state->hue = 0;
1629 state->sat = 64;
1630 state->chroma_agc = 1;
1631 switch (chip_id) { 1612 switch (chip_id) {
1632 case '1': 1613 case '1':
1633 state->ident = V4L2_IDENT_SAA7111; 1614 state->ident = V4L2_IDENT_SAA7111;
@@ -1675,6 +1656,7 @@ static int saa711x_probe(struct i2c_client *client,
1675 if (state->ident > V4L2_IDENT_SAA7111A) 1656 if (state->ident > V4L2_IDENT_SAA7111A)
1676 saa711x_writeregs(sd, saa7115_init_misc); 1657 saa711x_writeregs(sd, saa7115_init_misc);
1677 saa711x_set_v4lstd(sd, V4L2_STD_NTSC); 1658 saa711x_set_v4lstd(sd, V4L2_STD_NTSC);
1659 v4l2_ctrl_handler_setup(hdl);
1678 1660
1679 v4l2_dbg(1, debug, sd, "status: (1E) 0x%02x, (1F) 0x%02x\n", 1661 v4l2_dbg(1, debug, sd, "status: (1E) 0x%02x, (1F) 0x%02x\n",
1680 saa711x_read(sd, R_1E_STATUS_BYTE_1_VD_DEC), 1662 saa711x_read(sd, R_1E_STATUS_BYTE_1_VD_DEC),
@@ -1689,6 +1671,7 @@ static int saa711x_remove(struct i2c_client *client)
1689 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1671 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1690 1672
1691 v4l2_device_unregister_subdev(sd); 1673 v4l2_device_unregister_subdev(sd);
1674 v4l2_ctrl_handler_free(sd->ctrl_handler);
1692 kfree(to_state(sd)); 1675 kfree(to_state(sd));
1693 return 0; 1676 return 0;
1694} 1677}
diff --git a/drivers/media/video/saa717x.c b/drivers/media/video/saa717x.c
index 78d69950c00..45f8bfc1342 100644
--- a/drivers/media/video/saa717x.c
+++ b/drivers/media/video/saa717x.c
@@ -38,6 +38,7 @@
38#include <linux/videodev2.h> 38#include <linux/videodev2.h>
39#include <linux/i2c.h> 39#include <linux/i2c.h>
40#include <media/v4l2-device.h> 40#include <media/v4l2-device.h>
41#include <media/v4l2-ctrls.h>
41#include <media/v4l2-i2c-drv.h> 42#include <media/v4l2-i2c-drv.h>
42 43
43MODULE_DESCRIPTION("Philips SAA717x audio/video decoder driver"); 44MODULE_DESCRIPTION("Philips SAA717x audio/video decoder driver");
@@ -55,14 +56,11 @@ MODULE_PARM_DESC(debug, "Debug level (0-1)");
55 56
56struct saa717x_state { 57struct saa717x_state {
57 struct v4l2_subdev sd; 58 struct v4l2_subdev sd;
59 struct v4l2_ctrl_handler hdl;
58 v4l2_std_id std; 60 v4l2_std_id std;
59 int input; 61 int input;
60 int enable; 62 int enable;
61 int radio; 63 int radio;
62 int bright;
63 int contrast;
64 int hue;
65 int sat;
66 int playback; 64 int playback;
67 int audio; 65 int audio;
68 int tuner_audio_mode; 66 int tuner_audio_mode;
@@ -81,6 +79,11 @@ static inline struct saa717x_state *to_state(struct v4l2_subdev *sd)
81 return container_of(sd, struct saa717x_state, sd); 79 return container_of(sd, struct saa717x_state, sd);
82} 80}
83 81
82static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
83{
84 return &container_of(ctrl->handler, struct saa717x_state, hdl)->sd;
85}
86
84/* ----------------------------------------------------------------------- */ 87/* ----------------------------------------------------------------------- */
85 88
86/* for audio mode */ 89/* for audio mode */
@@ -774,29 +777,6 @@ static void set_audio_mode(struct v4l2_subdev *sd, int audio_mode)
774 saa717x_write(sd, 0x470, reg_set_audio_template[audio_mode][1]); 777 saa717x_write(sd, 0x470, reg_set_audio_template[audio_mode][1]);
775} 778}
776 779
777/* write regs to video output level (bright,contrast,hue,sat) */
778static void set_video_output_level_regs(struct v4l2_subdev *sd,
779 struct saa717x_state *decoder)
780{
781 /* brightness ffh (bright) - 80h (ITU level) - 00h (dark) */
782 saa717x_write(sd, 0x10a, decoder->bright);
783
784 /* contrast 7fh (max: 1.984) - 44h (ITU) - 40h (1.0) -
785 0h (luminance off) 40: i2c dump
786 c0h (-1.0 inverse chrominance)
787 80h (-2.0 inverse chrominance) */
788 saa717x_write(sd, 0x10b, decoder->contrast);
789
790 /* saturation? 7fh(max)-40h(ITU)-0h(color off)
791 c0h (-1.0 inverse chrominance)
792 80h (-2.0 inverse chrominance) */
793 saa717x_write(sd, 0x10c, decoder->sat);
794
795 /* color hue (phase) control
796 7fh (+178.6) - 0h (0 normal) - 80h (-180.0) */
797 saa717x_write(sd, 0x10d, decoder->hue);
798}
799
800/* write regs to set audio volume, bass and treble */ 780/* write regs to set audio volume, bass and treble */
801static int set_audio_regs(struct v4l2_subdev *sd, 781static int set_audio_regs(struct v4l2_subdev *sd,
802 struct saa717x_state *decoder) 782 struct saa717x_state *decoder)
@@ -829,9 +809,9 @@ static int set_audio_regs(struct v4l2_subdev *sd,
829 809
830 saa717x_write(sd, 0x480, val); 810 saa717x_write(sd, 0x480, val);
831 811
832 /* bass and treble; go to another function */
833 /* set bass and treble */ 812 /* set bass and treble */
834 val = decoder->audio_main_bass | (decoder->audio_main_treble << 8); 813 val = decoder->audio_main_bass & 0x1f;
814 val |= (decoder->audio_main_treble & 0x1f) << 5;
835 saa717x_write(sd, 0x488, val); 815 saa717x_write(sd, 0x488, val);
836 return 0; 816 return 0;
837} 817}
@@ -893,218 +873,55 @@ static void set_v_scale(struct v4l2_subdev *sd, int task, int yscale)
893 saa717x_write(sd, 0x71 + task_shift, yscale >> 8); 873 saa717x_write(sd, 0x71 + task_shift, yscale >> 8);
894} 874}
895 875
896static int saa717x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 876static int saa717x_s_ctrl(struct v4l2_ctrl *ctrl)
897{
898 struct saa717x_state *state = to_state(sd);
899
900 switch (ctrl->id) {
901 case V4L2_CID_BRIGHTNESS:
902 if (ctrl->value < 0 || ctrl->value > 255) {
903 v4l2_err(sd, "invalid brightness setting %d\n", ctrl->value);
904 return -ERANGE;
905 }
906
907 state->bright = ctrl->value;
908 v4l2_dbg(1, debug, sd, "bright:%d\n", state->bright);
909 saa717x_write(sd, 0x10a, state->bright);
910 break;
911
912 case V4L2_CID_CONTRAST:
913 if (ctrl->value < 0 || ctrl->value > 127) {
914 v4l2_err(sd, "invalid contrast setting %d\n", ctrl->value);
915 return -ERANGE;
916 }
917
918 state->contrast = ctrl->value;
919 v4l2_dbg(1, debug, sd, "contrast:%d\n", state->contrast);
920 saa717x_write(sd, 0x10b, state->contrast);
921 break;
922
923 case V4L2_CID_SATURATION:
924 if (ctrl->value < 0 || ctrl->value > 127) {
925 v4l2_err(sd, "invalid saturation setting %d\n", ctrl->value);
926 return -ERANGE;
927 }
928
929 state->sat = ctrl->value;
930 v4l2_dbg(1, debug, sd, "sat:%d\n", state->sat);
931 saa717x_write(sd, 0x10c, state->sat);
932 break;
933
934 case V4L2_CID_HUE:
935 if (ctrl->value < -128 || ctrl->value > 127) {
936 v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
937 return -ERANGE;
938 }
939
940 state->hue = ctrl->value;
941 v4l2_dbg(1, debug, sd, "hue:%d\n", state->hue);
942 saa717x_write(sd, 0x10d, state->hue);
943 break;
944
945 case V4L2_CID_AUDIO_MUTE:
946 state->audio_main_mute = ctrl->value;
947 set_audio_regs(sd, state);
948 break;
949
950 case V4L2_CID_AUDIO_VOLUME:
951 state->audio_main_volume = ctrl->value;
952 set_audio_regs(sd, state);
953 break;
954
955 case V4L2_CID_AUDIO_BALANCE:
956 state->audio_main_balance = ctrl->value;
957 set_audio_regs(sd, state);
958 break;
959
960 case V4L2_CID_AUDIO_TREBLE:
961 state->audio_main_treble = ctrl->value;
962 set_audio_regs(sd, state);
963 break;
964
965 case V4L2_CID_AUDIO_BASS:
966 state->audio_main_bass = ctrl->value;
967 set_audio_regs(sd, state);
968 break;
969
970 default:
971 return -EINVAL;
972 }
973
974 return 0;
975}
976
977static int saa717x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
978{ 877{
878 struct v4l2_subdev *sd = to_sd(ctrl);
979 struct saa717x_state *state = to_state(sd); 879 struct saa717x_state *state = to_state(sd);
980 880
981 switch (ctrl->id) { 881 switch (ctrl->id) {
982 case V4L2_CID_BRIGHTNESS: 882 case V4L2_CID_BRIGHTNESS:
983 ctrl->value = state->bright; 883 saa717x_write(sd, 0x10a, ctrl->val);
984 break; 884 return 0;
985 885
986 case V4L2_CID_CONTRAST: 886 case V4L2_CID_CONTRAST:
987 ctrl->value = state->contrast; 887 saa717x_write(sd, 0x10b, ctrl->val);
988 break; 888 return 0;
989 889
990 case V4L2_CID_SATURATION: 890 case V4L2_CID_SATURATION:
991 ctrl->value = state->sat; 891 saa717x_write(sd, 0x10c, ctrl->val);
992 break; 892 return 0;
993 893
994 case V4L2_CID_HUE: 894 case V4L2_CID_HUE:
995 ctrl->value = state->hue; 895 saa717x_write(sd, 0x10d, ctrl->val);
996 break; 896 return 0;
997 897
998 case V4L2_CID_AUDIO_MUTE: 898 case V4L2_CID_AUDIO_MUTE:
999 ctrl->value = state->audio_main_mute; 899 state->audio_main_mute = ctrl->val;
1000 break; 900 break;
1001 901
1002 case V4L2_CID_AUDIO_VOLUME: 902 case V4L2_CID_AUDIO_VOLUME:
1003 ctrl->value = state->audio_main_volume; 903 state->audio_main_volume = ctrl->val;
1004 break; 904 break;
1005 905
1006 case V4L2_CID_AUDIO_BALANCE: 906 case V4L2_CID_AUDIO_BALANCE:
1007 ctrl->value = state->audio_main_balance; 907 state->audio_main_balance = ctrl->val;
1008 break; 908 break;
1009 909
1010 case V4L2_CID_AUDIO_TREBLE: 910 case V4L2_CID_AUDIO_TREBLE:
1011 ctrl->value = state->audio_main_treble; 911 state->audio_main_treble = ctrl->val;
1012 break; 912 break;
1013 913
1014 case V4L2_CID_AUDIO_BASS: 914 case V4L2_CID_AUDIO_BASS:
1015 ctrl->value = state->audio_main_bass; 915 state->audio_main_bass = ctrl->val;
1016 break; 916 break;
1017 917
1018 default: 918 default:
1019 return -EINVAL; 919 return 0;
1020 } 920 }
1021 921 set_audio_regs(sd, state);
1022 return 0; 922 return 0;
1023} 923}
1024 924
1025static struct v4l2_queryctrl saa717x_qctrl[] = {
1026 {
1027 .id = V4L2_CID_BRIGHTNESS,
1028 .type = V4L2_CTRL_TYPE_INTEGER,
1029 .name = "Brightness",
1030 .minimum = 0,
1031 .maximum = 255,
1032 .step = 1,
1033 .default_value = 128,
1034 .flags = 0,
1035 }, {
1036 .id = V4L2_CID_CONTRAST,
1037 .type = V4L2_CTRL_TYPE_INTEGER,
1038 .name = "Contrast",
1039 .minimum = 0,
1040 .maximum = 255,
1041 .step = 1,
1042 .default_value = 64,
1043 .flags = 0,
1044 }, {
1045 .id = V4L2_CID_SATURATION,
1046 .type = V4L2_CTRL_TYPE_INTEGER,
1047 .name = "Saturation",
1048 .minimum = 0,
1049 .maximum = 255,
1050 .step = 1,
1051 .default_value = 64,
1052 .flags = 0,
1053 }, {
1054 .id = V4L2_CID_HUE,
1055 .type = V4L2_CTRL_TYPE_INTEGER,
1056 .name = "Hue",
1057 .minimum = -128,
1058 .maximum = 127,
1059 .step = 1,
1060 .default_value = 0,
1061 .flags = 0,
1062 }, {
1063 .id = V4L2_CID_AUDIO_VOLUME,
1064 .type = V4L2_CTRL_TYPE_INTEGER,
1065 .name = "Volume",
1066 .minimum = 0,
1067 .maximum = 65535,
1068 .step = 65535 / 100,
1069 .default_value = 58880,
1070 .flags = 0,
1071 }, {
1072 .id = V4L2_CID_AUDIO_BALANCE,
1073 .type = V4L2_CTRL_TYPE_INTEGER,
1074 .name = "Balance",
1075 .minimum = 0,
1076 .maximum = 65535,
1077 .step = 65535 / 100,
1078 .default_value = 32768,
1079 .flags = 0,
1080 }, {
1081 .id = V4L2_CID_AUDIO_MUTE,
1082 .type = V4L2_CTRL_TYPE_BOOLEAN,
1083 .name = "Mute",
1084 .minimum = 0,
1085 .maximum = 1,
1086 .step = 1,
1087 .default_value = 1,
1088 .flags = 0,
1089 }, {
1090 .id = V4L2_CID_AUDIO_BASS,
1091 .type = V4L2_CTRL_TYPE_INTEGER,
1092 .name = "Bass",
1093 .minimum = 0,
1094 .maximum = 65535,
1095 .step = 65535 / 100,
1096 .default_value = 32768,
1097 }, {
1098 .id = V4L2_CID_AUDIO_TREBLE,
1099 .type = V4L2_CTRL_TYPE_INTEGER,
1100 .name = "Treble",
1101 .minimum = 0,
1102 .maximum = 65535,
1103 .step = 65535 / 100,
1104 .default_value = 32768,
1105 },
1106};
1107
1108static int saa717x_s_video_routing(struct v4l2_subdev *sd, 925static int saa717x_s_video_routing(struct v4l2_subdev *sd,
1109 u32 input, u32 output, u32 config) 926 u32 input, u32 output, u32 config)
1110{ 927{
@@ -1158,18 +975,6 @@ static int saa717x_s_video_routing(struct v4l2_subdev *sd,
1158 return 0; 975 return 0;
1159} 976}
1160 977
1161static int saa717x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1162{
1163 int i;
1164
1165 for (i = 0; i < ARRAY_SIZE(saa717x_qctrl); i++)
1166 if (qc->id && qc->id == saa717x_qctrl[i].id) {
1167 memcpy(qc, &saa717x_qctrl[i], sizeof(*qc));
1168 return 0;
1169 }
1170 return -EINVAL;
1171}
1172
1173#ifdef CONFIG_VIDEO_ADV_DEBUG 978#ifdef CONFIG_VIDEO_ADV_DEBUG
1174static int saa717x_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg) 979static int saa717x_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
1175{ 980{
@@ -1386,17 +1191,34 @@ static int saa717x_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1386 return 0; 1191 return 0;
1387} 1192}
1388 1193
1194static int saa717x_log_status(struct v4l2_subdev *sd)
1195{
1196 struct saa717x_state *state = to_state(sd);
1197
1198 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
1199 return 0;
1200}
1201
1389/* ----------------------------------------------------------------------- */ 1202/* ----------------------------------------------------------------------- */
1390 1203
1204static const struct v4l2_ctrl_ops saa717x_ctrl_ops = {
1205 .s_ctrl = saa717x_s_ctrl,
1206};
1207
1391static const struct v4l2_subdev_core_ops saa717x_core_ops = { 1208static const struct v4l2_subdev_core_ops saa717x_core_ops = {
1392#ifdef CONFIG_VIDEO_ADV_DEBUG 1209#ifdef CONFIG_VIDEO_ADV_DEBUG
1393 .g_register = saa717x_g_register, 1210 .g_register = saa717x_g_register,
1394 .s_register = saa717x_s_register, 1211 .s_register = saa717x_s_register,
1395#endif 1212#endif
1396 .queryctrl = saa717x_queryctrl,
1397 .g_ctrl = saa717x_g_ctrl,
1398 .s_ctrl = saa717x_s_ctrl,
1399 .s_std = saa717x_s_std, 1213 .s_std = saa717x_s_std,
1214 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1215 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1216 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1217 .g_ctrl = v4l2_subdev_g_ctrl,
1218 .s_ctrl = v4l2_subdev_s_ctrl,
1219 .queryctrl = v4l2_subdev_queryctrl,
1220 .querymenu = v4l2_subdev_querymenu,
1221 .log_status = saa717x_log_status,
1400}; 1222};
1401 1223
1402static const struct v4l2_subdev_tuner_ops saa717x_tuner_ops = { 1224static const struct v4l2_subdev_tuner_ops saa717x_tuner_ops = {
@@ -1432,6 +1254,7 @@ static int saa717x_probe(struct i2c_client *client,
1432 const struct i2c_device_id *did) 1254 const struct i2c_device_id *did)
1433{ 1255{
1434 struct saa717x_state *decoder; 1256 struct saa717x_state *decoder;
1257 struct v4l2_ctrl_handler *hdl;
1435 struct v4l2_subdev *sd; 1258 struct v4l2_subdev *sd;
1436 u8 id = 0; 1259 u8 id = 0;
1437 char *p = ""; 1260 char *p = "";
@@ -1467,16 +1290,41 @@ static int saa717x_probe(struct i2c_client *client,
1467 p = "saa7171"; 1290 p = "saa7171";
1468 v4l2_info(sd, "%s found @ 0x%x (%s)\n", p, 1291 v4l2_info(sd, "%s found @ 0x%x (%s)\n", p,
1469 client->addr << 1, client->adapter->name); 1292 client->addr << 1, client->adapter->name);
1293
1294 hdl = &decoder->hdl;
1295 v4l2_ctrl_handler_init(hdl, 9);
1296 /* add in ascending ID order */
1297 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1298 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1299 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1300 V4L2_CID_CONTRAST, 0, 255, 1, 68);
1301 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1302 V4L2_CID_SATURATION, 0, 255, 1, 64);
1303 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1304 V4L2_CID_HUE, -128, 127, 1, 0);
1305 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1306 V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 42000);
1307 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1308 V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
1309 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1310 V4L2_CID_AUDIO_BASS, -16, 15, 1, 0);
1311 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1312 V4L2_CID_AUDIO_TREBLE, -16, 15, 1, 0);
1313 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1314 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1315 sd->ctrl_handler = hdl;
1316 if (hdl->error) {
1317 int err = hdl->error;
1318
1319 v4l2_ctrl_handler_free(hdl);
1320 kfree(decoder);
1321 return err;
1322 }
1323
1470 decoder->std = V4L2_STD_NTSC; 1324 decoder->std = V4L2_STD_NTSC;
1471 decoder->input = -1; 1325 decoder->input = -1;
1472 decoder->enable = 1; 1326 decoder->enable = 1;
1473 1327
1474 /* tune these parameters */
1475 decoder->bright = 0x80;
1476 decoder->contrast = 0x44;
1477 decoder->sat = 0x40;
1478 decoder->hue = 0x00;
1479
1480 /* FIXME!! */ 1328 /* FIXME!! */
1481 decoder->playback = 0; /* initially capture mode used */ 1329 decoder->playback = 0; /* initially capture mode used */
1482 decoder->audio = 1; /* DECODER_AUDIO_48_KHZ */ 1330 decoder->audio = 1; /* DECODER_AUDIO_48_KHZ */
@@ -1487,23 +1335,13 @@ static int saa717x_probe(struct i2c_client *client,
1487 /* set volume, bass and treble */ 1335 /* set volume, bass and treble */
1488 decoder->audio_main_vol_l = 6; 1336 decoder->audio_main_vol_l = 6;
1489 decoder->audio_main_vol_r = 6; 1337 decoder->audio_main_vol_r = 6;
1490 decoder->audio_main_bass = 0;
1491 decoder->audio_main_treble = 0;
1492 decoder->audio_main_mute = 0;
1493 decoder->audio_main_balance = 32768;
1494 /* normalize (24 to -40 (not -84) -> 65535 to 0) */
1495 decoder->audio_main_volume =
1496 (decoder->audio_main_vol_r + 41) * 65535 / (24 - (-40));
1497 1338
1498 v4l2_dbg(1, debug, sd, "writing init values\n"); 1339 v4l2_dbg(1, debug, sd, "writing init values\n");
1499 1340
1500 /* FIXME!! */ 1341 /* FIXME!! */
1501 saa717x_write_regs(sd, reg_init_initialize); 1342 saa717x_write_regs(sd, reg_init_initialize);
1502 set_video_output_level_regs(sd, decoder); 1343
1503 /* set bass,treble to 0db 20041101 K.Ohta */ 1344 v4l2_ctrl_handler_setup(hdl);
1504 decoder->audio_main_bass = 0;
1505 decoder->audio_main_treble = 0;
1506 set_audio_regs(sd, decoder);
1507 1345
1508 set_current_state(TASK_INTERRUPTIBLE); 1346 set_current_state(TASK_INTERRUPTIBLE);
1509 schedule_timeout(2*HZ); 1347 schedule_timeout(2*HZ);
@@ -1515,6 +1353,7 @@ static int saa717x_remove(struct i2c_client *client)
1515 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1353 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1516 1354
1517 v4l2_device_unregister_subdev(sd); 1355 v4l2_device_unregister_subdev(sd);
1356 v4l2_ctrl_handler_free(sd->ctrl_handler);
1518 kfree(to_state(sd)); 1357 kfree(to_state(sd));
1519 return 0; 1358 return 0;
1520} 1359}
diff --git a/drivers/media/video/soc_camera.c b/drivers/media/video/soc_camera.c
index f2032939fd4..a499cacec1f 100644
--- a/drivers/media/video/soc_camera.c
+++ b/drivers/media/video/soc_camera.c
@@ -779,9 +779,12 @@ static int soc_camera_s_crop(struct file *file, void *fh,
779 ret = ici->ops->get_crop(icd, &current_crop); 779 ret = ici->ops->get_crop(icd, &current_crop);
780 780
781 /* Prohibit window size change with initialised buffers */ 781 /* Prohibit window size change with initialised buffers */
782 if (icf->vb_vidq.bufs[0] && !ret && 782 if (ret < 0) {
783 (a->c.width != current_crop.c.width || 783 dev_err(&icd->dev,
784 a->c.height != current_crop.c.height)) { 784 "S_CROP denied: getting current crop failed\n");
785 } else if (icf->vb_vidq.bufs[0] &&
786 (a->c.width != current_crop.c.width ||
787 a->c.height != current_crop.c.height)) {
785 dev_err(&icd->dev, 788 dev_err(&icd->dev,
786 "S_CROP denied: queue initialised and sizes differ\n"); 789 "S_CROP denied: queue initialised and sizes differ\n");
787 ret = -EBUSY; 790 ret = -EBUSY;
diff --git a/drivers/media/video/tvp7002.c b/drivers/media/video/tvp7002.c
index 8085ac39244..48f5c76ab52 100644
--- a/drivers/media/video/tvp7002.c
+++ b/drivers/media/video/tvp7002.c
@@ -179,7 +179,7 @@ static const struct i2c_reg_value tvp7002_init_default[] = {
179/* Register parameters for 480P */ 179/* Register parameters for 480P */
180static const struct i2c_reg_value tvp7002_parms_480P[] = { 180static const struct i2c_reg_value tvp7002_parms_480P[] = {
181 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x35, TVP7002_WRITE }, 181 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x35, TVP7002_WRITE },
182 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x0a, TVP7002_WRITE }, 182 { TVP7002_HPLL_FDBK_DIV_LSBS, 0xa0, TVP7002_WRITE },
183 { TVP7002_HPLL_CRTL, 0x02, TVP7002_WRITE }, 183 { TVP7002_HPLL_CRTL, 0x02, TVP7002_WRITE },
184 { TVP7002_HPLL_PHASE_SEL, 0x14, TVP7002_WRITE }, 184 { TVP7002_HPLL_PHASE_SEL, 0x14, TVP7002_WRITE },
185 { TVP7002_AVID_START_PIXEL_LSBS, 0x91, TVP7002_WRITE }, 185 { TVP7002_AVID_START_PIXEL_LSBS, 0x91, TVP7002_WRITE },
@@ -223,7 +223,7 @@ static const struct i2c_reg_value tvp7002_parms_576P[] = {
223/* Register parameters for 1080I60 */ 223/* Register parameters for 1080I60 */
224static const struct i2c_reg_value tvp7002_parms_1080I60[] = { 224static const struct i2c_reg_value tvp7002_parms_1080I60[] = {
225 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE }, 225 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE },
226 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x08, TVP7002_WRITE }, 226 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x80, TVP7002_WRITE },
227 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE }, 227 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
228 { TVP7002_HPLL_PHASE_SEL, 0x14, TVP7002_WRITE }, 228 { TVP7002_HPLL_PHASE_SEL, 0x14, TVP7002_WRITE },
229 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE }, 229 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
@@ -245,7 +245,7 @@ static const struct i2c_reg_value tvp7002_parms_1080I60[] = {
245/* Register parameters for 1080P60 */ 245/* Register parameters for 1080P60 */
246static const struct i2c_reg_value tvp7002_parms_1080P60[] = { 246static const struct i2c_reg_value tvp7002_parms_1080P60[] = {
247 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE }, 247 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE },
248 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x08, TVP7002_WRITE }, 248 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x80, TVP7002_WRITE },
249 { TVP7002_HPLL_CRTL, 0xE0, TVP7002_WRITE }, 249 { TVP7002_HPLL_CRTL, 0xE0, TVP7002_WRITE },
250 { TVP7002_HPLL_PHASE_SEL, 0x14, TVP7002_WRITE }, 250 { TVP7002_HPLL_PHASE_SEL, 0x14, TVP7002_WRITE },
251 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE }, 251 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
@@ -289,7 +289,7 @@ static const struct i2c_reg_value tvp7002_parms_1080I50[] = {
289/* Register parameters for 720P60 */ 289/* Register parameters for 720P60 */
290static const struct i2c_reg_value tvp7002_parms_720P60[] = { 290static const struct i2c_reg_value tvp7002_parms_720P60[] = {
291 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x67, TVP7002_WRITE }, 291 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x67, TVP7002_WRITE },
292 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x02, TVP7002_WRITE }, 292 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x20, TVP7002_WRITE },
293 { TVP7002_HPLL_CRTL, 0xa0, TVP7002_WRITE }, 293 { TVP7002_HPLL_CRTL, 0xa0, TVP7002_WRITE },
294 { TVP7002_HPLL_PHASE_SEL, 0x16, TVP7002_WRITE }, 294 { TVP7002_HPLL_PHASE_SEL, 0x16, TVP7002_WRITE },
295 { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE }, 295 { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE },
@@ -311,7 +311,7 @@ static const struct i2c_reg_value tvp7002_parms_720P60[] = {
311/* Register parameters for 720P50 */ 311/* Register parameters for 720P50 */
312static const struct i2c_reg_value tvp7002_parms_720P50[] = { 312static const struct i2c_reg_value tvp7002_parms_720P50[] = {
313 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x7b, TVP7002_WRITE }, 313 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x7b, TVP7002_WRITE },
314 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x0c, TVP7002_WRITE }, 314 { TVP7002_HPLL_FDBK_DIV_LSBS, 0xc0, TVP7002_WRITE },
315 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE }, 315 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
316 { TVP7002_HPLL_PHASE_SEL, 0x16, TVP7002_WRITE }, 316 { TVP7002_HPLL_PHASE_SEL, 0x16, TVP7002_WRITE },
317 { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE }, 317 { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE },
diff --git a/drivers/media/video/usbvideo/usbvideo.c b/drivers/media/video/usbvideo/usbvideo.c
index 5ac37c6c431..f1fcf974496 100644
--- a/drivers/media/video/usbvideo/usbvideo.c
+++ b/drivers/media/video/usbvideo/usbvideo.c
@@ -282,19 +282,15 @@ static void usbvideo_OverlayChar(struct uvd *uvd, struct usbvideo_frame *frame,
282 }; 282 };
283 unsigned short digit; 283 unsigned short digit;
284 int ix, iy; 284 int ix, iy;
285 int value;
285 286
286 if ((uvd == NULL) || (frame == NULL)) 287 if ((uvd == NULL) || (frame == NULL))
287 return; 288 return;
288 289
289 if (ch >= '0' && ch <= '9') 290 value = hex_to_bin(ch);
290 ch -= '0'; 291 if (value < 0)
291 else if (ch >= 'A' && ch <= 'F')
292 ch = 10 + (ch - 'A');
293 else if (ch >= 'a' && ch <= 'f')
294 ch = 10 + (ch - 'a');
295 else
296 return; 292 return;
297 digit = digits[ch]; 293 digit = digits[value];
298 294
299 for (iy=0; iy < 5; iy++) { 295 for (iy=0; iy < 5; iy++) {
300 for (ix=0; ix < 3; ix++) { 296 for (ix=0; ix < 3; ix++) {
diff --git a/drivers/media/video/uvc/uvc_driver.c b/drivers/media/video/uvc/uvc_driver.c
index 7eaf99b22a4..8bdd940f32e 100644
--- a/drivers/media/video/uvc/uvc_driver.c
+++ b/drivers/media/video/uvc/uvc_driver.c
@@ -2145,6 +2145,15 @@ static struct usb_device_id uvc_ids[] = {
2145 .bInterfaceSubClass = 1, 2145 .bInterfaceSubClass = 1,
2146 .bInterfaceProtocol = 0, 2146 .bInterfaceProtocol = 0,
2147 .driver_info = UVC_QUIRK_STREAM_NO_FID }, 2147 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2148 /* Miricle 307K */
2149 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2150 | USB_DEVICE_ID_MATCH_INT_INFO,
2151 .idVendor = 0x17dc,
2152 .idProduct = 0x0202,
2153 .bInterfaceClass = USB_CLASS_VIDEO,
2154 .bInterfaceSubClass = 1,
2155 .bInterfaceProtocol = 0,
2156 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2148 /* Lenovo Thinkpad SL400/SL500 */ 2157 /* Lenovo Thinkpad SL400/SL500 */
2149 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2158 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2150 | USB_DEVICE_ID_MATCH_INT_INFO, 2159 | USB_DEVICE_ID_MATCH_INT_INFO,
diff --git a/drivers/media/video/uvc/uvc_queue.c b/drivers/media/video/uvc/uvc_queue.c
index 133c78d113a..e9928a41508 100644
--- a/drivers/media/video/uvc/uvc_queue.c
+++ b/drivers/media/video/uvc/uvc_queue.c
@@ -78,12 +78,14 @@
78 * 78 *
79 */ 79 */
80 80
81void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type) 81void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
82 int drop_corrupted)
82{ 83{
83 mutex_init(&queue->mutex); 84 mutex_init(&queue->mutex);
84 spin_lock_init(&queue->irqlock); 85 spin_lock_init(&queue->irqlock);
85 INIT_LIST_HEAD(&queue->mainqueue); 86 INIT_LIST_HEAD(&queue->mainqueue);
86 INIT_LIST_HEAD(&queue->irqqueue); 87 INIT_LIST_HEAD(&queue->irqqueue);
88 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
87 queue->type = type; 89 queue->type = type;
88} 90}
89 91
@@ -435,8 +437,10 @@ int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
435 uvc_queue_cancel(queue, 0); 437 uvc_queue_cancel(queue, 0);
436 INIT_LIST_HEAD(&queue->mainqueue); 438 INIT_LIST_HEAD(&queue->mainqueue);
437 439
438 for (i = 0; i < queue->count; ++i) 440 for (i = 0; i < queue->count; ++i) {
441 queue->buffer[i].error = 0;
439 queue->buffer[i].state = UVC_BUF_STATE_IDLE; 442 queue->buffer[i].state = UVC_BUF_STATE_IDLE;
443 }
440 444
441 queue->flags &= ~UVC_QUEUE_STREAMING; 445 queue->flags &= ~UVC_QUEUE_STREAMING;
442 } 446 }
@@ -488,8 +492,8 @@ struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
488 struct uvc_buffer *nextbuf; 492 struct uvc_buffer *nextbuf;
489 unsigned long flags; 493 unsigned long flags;
490 494
491 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) && 495 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
492 buf->buf.length != buf->buf.bytesused) { 496 buf->error = 0;
493 buf->state = UVC_BUF_STATE_QUEUED; 497 buf->state = UVC_BUF_STATE_QUEUED;
494 buf->buf.bytesused = 0; 498 buf->buf.bytesused = 0;
495 return buf; 499 return buf;
@@ -497,6 +501,7 @@ struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
497 501
498 spin_lock_irqsave(&queue->irqlock, flags); 502 spin_lock_irqsave(&queue->irqlock, flags);
499 list_del(&buf->queue); 503 list_del(&buf->queue);
504 buf->error = 0;
500 buf->state = UVC_BUF_STATE_DONE; 505 buf->state = UVC_BUF_STATE_DONE;
501 if (!list_empty(&queue->irqqueue)) 506 if (!list_empty(&queue->irqqueue))
502 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer, 507 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
diff --git a/drivers/media/video/uvc/uvc_video.c b/drivers/media/video/uvc/uvc_video.c
index 53f3ef4635e..e27cf0d3b6d 100644
--- a/drivers/media/video/uvc/uvc_video.c
+++ b/drivers/media/video/uvc/uvc_video.c
@@ -555,6 +555,9 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
555 if (urb->iso_frame_desc[i].status < 0) { 555 if (urb->iso_frame_desc[i].status < 0) {
556 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame " 556 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
557 "lost (%d).\n", urb->iso_frame_desc[i].status); 557 "lost (%d).\n", urb->iso_frame_desc[i].status);
558 /* Mark the buffer as faulty. */
559 if (buf != NULL)
560 buf->error = 1;
558 continue; 561 continue;
559 } 562 }
560 563
@@ -579,8 +582,14 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
579 uvc_video_decode_end(stream, buf, mem, 582 uvc_video_decode_end(stream, buf, mem,
580 urb->iso_frame_desc[i].actual_length); 583 urb->iso_frame_desc[i].actual_length);
581 584
582 if (buf->state == UVC_BUF_STATE_READY) 585 if (buf->state == UVC_BUF_STATE_READY) {
586 if (buf->buf.length != buf->buf.bytesused &&
587 !(stream->cur_format->flags &
588 UVC_FMT_FLAG_COMPRESSED))
589 buf->error = 1;
590
583 buf = uvc_queue_next_buffer(&stream->queue, buf); 591 buf = uvc_queue_next_buffer(&stream->queue, buf);
592 }
584 } 593 }
585} 594}
586 595
@@ -1104,7 +1113,7 @@ int uvc_video_init(struct uvc_streaming *stream)
1104 atomic_set(&stream->active, 0); 1113 atomic_set(&stream->active, 0);
1105 1114
1106 /* Initialize the video buffers queue. */ 1115 /* Initialize the video buffers queue. */
1107 uvc_queue_init(&stream->queue, stream->type); 1116 uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param);
1108 1117
1109 /* Alternate setting 0 should be the default, yet the XBox Live Vision 1118 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1110 * Cam (and possibly other devices) crash or otherwise misbehave if 1119 * Cam (and possibly other devices) crash or otherwise misbehave if
@@ -1197,12 +1206,6 @@ int uvc_video_enable(struct uvc_streaming *stream, int enable)
1197 return 0; 1206 return 0;
1198 } 1207 }
1199 1208
1200 if ((stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) ||
1201 uvc_no_drop_param)
1202 stream->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
1203 else
1204 stream->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
1205
1206 ret = uvc_queue_enable(&stream->queue, 1); 1209 ret = uvc_queue_enable(&stream->queue, 1);
1207 if (ret < 0) 1210 if (ret < 0)
1208 return ret; 1211 return ret;
diff --git a/drivers/media/video/uvc/uvcvideo.h b/drivers/media/video/uvc/uvcvideo.h
index ac272456fbf..bdacf3beabf 100644
--- a/drivers/media/video/uvc/uvcvideo.h
+++ b/drivers/media/video/uvc/uvcvideo.h
@@ -379,11 +379,12 @@ struct uvc_buffer {
379 struct list_head queue; 379 struct list_head queue;
380 wait_queue_head_t wait; 380 wait_queue_head_t wait;
381 enum uvc_buffer_state state; 381 enum uvc_buffer_state state;
382 unsigned int error;
382}; 383};
383 384
384#define UVC_QUEUE_STREAMING (1 << 0) 385#define UVC_QUEUE_STREAMING (1 << 0)
385#define UVC_QUEUE_DISCONNECTED (1 << 1) 386#define UVC_QUEUE_DISCONNECTED (1 << 1)
386#define UVC_QUEUE_DROP_INCOMPLETE (1 << 2) 387#define UVC_QUEUE_DROP_CORRUPTED (1 << 2)
387 388
388struct uvc_video_queue { 389struct uvc_video_queue {
389 enum v4l2_buf_type type; 390 enum v4l2_buf_type type;
@@ -562,7 +563,7 @@ extern struct uvc_driver uvc_driver;
562 563
563/* Video buffers queue management. */ 564/* Video buffers queue management. */
564extern void uvc_queue_init(struct uvc_video_queue *queue, 565extern void uvc_queue_init(struct uvc_video_queue *queue,
565 enum v4l2_buf_type type); 566 enum v4l2_buf_type type, int drop_corrupted);
566extern int uvc_alloc_buffers(struct uvc_video_queue *queue, 567extern int uvc_alloc_buffers(struct uvc_video_queue *queue,
567 unsigned int nbuffers, unsigned int buflength); 568 unsigned int nbuffers, unsigned int buflength);
568extern int uvc_free_buffers(struct uvc_video_queue *queue); 569extern int uvc_free_buffers(struct uvc_video_queue *queue);
diff --git a/drivers/media/video/v4l2-common.c b/drivers/media/video/v4l2-common.c
index 4e53b0b3339..3ce7c64e578 100644
--- a/drivers/media/video/v4l2-common.c
+++ b/drivers/media/video/v4l2-common.c
@@ -62,6 +62,7 @@
62#define __OLD_VIDIOC_ /* To allow fixing old calls*/ 62#define __OLD_VIDIOC_ /* To allow fixing old calls*/
63#include <media/v4l2-common.h> 63#include <media/v4l2-common.h>
64#include <media/v4l2-device.h> 64#include <media/v4l2-device.h>
65#include <media/v4l2-ctrls.h>
65#include <media/v4l2-chip-ident.h> 66#include <media/v4l2-chip-ident.h>
66 67
67#include <linux/videodev2.h> 68#include <linux/videodev2.h>
@@ -172,487 +173,17 @@ int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
172} 173}
173EXPORT_SYMBOL(v4l2_ctrl_check); 174EXPORT_SYMBOL(v4l2_ctrl_check);
174 175
175/* Returns NULL or a character pointer array containing the menu for
176 the given control ID. The pointer array ends with a NULL pointer.
177 An empty string signifies a menu entry that is invalid. This allows
178 drivers to disable certain options if it is not supported. */
179const char **v4l2_ctrl_get_menu(u32 id)
180{
181 static const char *mpeg_audio_sampling_freq[] = {
182 "44.1 kHz",
183 "48 kHz",
184 "32 kHz",
185 NULL
186 };
187 static const char *mpeg_audio_encoding[] = {
188 "MPEG-1/2 Layer I",
189 "MPEG-1/2 Layer II",
190 "MPEG-1/2 Layer III",
191 "MPEG-2/4 AAC",
192 "AC-3",
193 NULL
194 };
195 static const char *mpeg_audio_l1_bitrate[] = {
196 "32 kbps",
197 "64 kbps",
198 "96 kbps",
199 "128 kbps",
200 "160 kbps",
201 "192 kbps",
202 "224 kbps",
203 "256 kbps",
204 "288 kbps",
205 "320 kbps",
206 "352 kbps",
207 "384 kbps",
208 "416 kbps",
209 "448 kbps",
210 NULL
211 };
212 static const char *mpeg_audio_l2_bitrate[] = {
213 "32 kbps",
214 "48 kbps",
215 "56 kbps",
216 "64 kbps",
217 "80 kbps",
218 "96 kbps",
219 "112 kbps",
220 "128 kbps",
221 "160 kbps",
222 "192 kbps",
223 "224 kbps",
224 "256 kbps",
225 "320 kbps",
226 "384 kbps",
227 NULL
228 };
229 static const char *mpeg_audio_l3_bitrate[] = {
230 "32 kbps",
231 "40 kbps",
232 "48 kbps",
233 "56 kbps",
234 "64 kbps",
235 "80 kbps",
236 "96 kbps",
237 "112 kbps",
238 "128 kbps",
239 "160 kbps",
240 "192 kbps",
241 "224 kbps",
242 "256 kbps",
243 "320 kbps",
244 NULL
245 };
246 static const char *mpeg_audio_ac3_bitrate[] = {
247 "32 kbps",
248 "40 kbps",
249 "48 kbps",
250 "56 kbps",
251 "64 kbps",
252 "80 kbps",
253 "96 kbps",
254 "112 kbps",
255 "128 kbps",
256 "160 kbps",
257 "192 kbps",
258 "224 kbps",
259 "256 kbps",
260 "320 kbps",
261 "384 kbps",
262 "448 kbps",
263 "512 kbps",
264 "576 kbps",
265 "640 kbps",
266 NULL
267 };
268 static const char *mpeg_audio_mode[] = {
269 "Stereo",
270 "Joint Stereo",
271 "Dual",
272 "Mono",
273 NULL
274 };
275 static const char *mpeg_audio_mode_extension[] = {
276 "Bound 4",
277 "Bound 8",
278 "Bound 12",
279 "Bound 16",
280 NULL
281 };
282 static const char *mpeg_audio_emphasis[] = {
283 "No Emphasis",
284 "50/15 us",
285 "CCITT J17",
286 NULL
287 };
288 static const char *mpeg_audio_crc[] = {
289 "No CRC",
290 "16-bit CRC",
291 NULL
292 };
293 static const char *mpeg_video_encoding[] = {
294 "MPEG-1",
295 "MPEG-2",
296 "MPEG-4 AVC",
297 NULL
298 };
299 static const char *mpeg_video_aspect[] = {
300 "1x1",
301 "4x3",
302 "16x9",
303 "2.21x1",
304 NULL
305 };
306 static const char *mpeg_video_bitrate_mode[] = {
307 "Variable Bitrate",
308 "Constant Bitrate",
309 NULL
310 };
311 static const char *mpeg_stream_type[] = {
312 "MPEG-2 Program Stream",
313 "MPEG-2 Transport Stream",
314 "MPEG-1 System Stream",
315 "MPEG-2 DVD-compatible Stream",
316 "MPEG-1 VCD-compatible Stream",
317 "MPEG-2 SVCD-compatible Stream",
318 NULL
319 };
320 static const char *mpeg_stream_vbi_fmt[] = {
321 "No VBI",
322 "Private packet, IVTV format",
323 NULL
324 };
325 static const char *camera_power_line_frequency[] = {
326 "Disabled",
327 "50 Hz",
328 "60 Hz",
329 NULL
330 };
331 static const char *camera_exposure_auto[] = {
332 "Auto Mode",
333 "Manual Mode",
334 "Shutter Priority Mode",
335 "Aperture Priority Mode",
336 NULL
337 };
338 static const char *colorfx[] = {
339 "None",
340 "Black & White",
341 "Sepia",
342 "Negative",
343 "Emboss",
344 "Sketch",
345 "Sky blue",
346 "Grass green",
347 "Skin whiten",
348 "Vivid",
349 NULL
350 };
351 static const char *tune_preemphasis[] = {
352 "No preemphasis",
353 "50 useconds",
354 "75 useconds",
355 NULL,
356 };
357
358 switch (id) {
359 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
360 return mpeg_audio_sampling_freq;
361 case V4L2_CID_MPEG_AUDIO_ENCODING:
362 return mpeg_audio_encoding;
363 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
364 return mpeg_audio_l1_bitrate;
365 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
366 return mpeg_audio_l2_bitrate;
367 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
368 return mpeg_audio_l3_bitrate;
369 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
370 return mpeg_audio_ac3_bitrate;
371 case V4L2_CID_MPEG_AUDIO_MODE:
372 return mpeg_audio_mode;
373 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
374 return mpeg_audio_mode_extension;
375 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
376 return mpeg_audio_emphasis;
377 case V4L2_CID_MPEG_AUDIO_CRC:
378 return mpeg_audio_crc;
379 case V4L2_CID_MPEG_VIDEO_ENCODING:
380 return mpeg_video_encoding;
381 case V4L2_CID_MPEG_VIDEO_ASPECT:
382 return mpeg_video_aspect;
383 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
384 return mpeg_video_bitrate_mode;
385 case V4L2_CID_MPEG_STREAM_TYPE:
386 return mpeg_stream_type;
387 case V4L2_CID_MPEG_STREAM_VBI_FMT:
388 return mpeg_stream_vbi_fmt;
389 case V4L2_CID_POWER_LINE_FREQUENCY:
390 return camera_power_line_frequency;
391 case V4L2_CID_EXPOSURE_AUTO:
392 return camera_exposure_auto;
393 case V4L2_CID_COLORFX:
394 return colorfx;
395 case V4L2_CID_TUNE_PREEMPHASIS:
396 return tune_preemphasis;
397 default:
398 return NULL;
399 }
400}
401EXPORT_SYMBOL(v4l2_ctrl_get_menu);
402
403/* Return the control name. */
404const char *v4l2_ctrl_get_name(u32 id)
405{
406 switch (id) {
407 /* USER controls */
408 case V4L2_CID_USER_CLASS: return "User Controls";
409 case V4L2_CID_BRIGHTNESS: return "Brightness";
410 case V4L2_CID_CONTRAST: return "Contrast";
411 case V4L2_CID_SATURATION: return "Saturation";
412 case V4L2_CID_HUE: return "Hue";
413 case V4L2_CID_AUDIO_VOLUME: return "Volume";
414 case V4L2_CID_AUDIO_BALANCE: return "Balance";
415 case V4L2_CID_AUDIO_BASS: return "Bass";
416 case V4L2_CID_AUDIO_TREBLE: return "Treble";
417 case V4L2_CID_AUDIO_MUTE: return "Mute";
418 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
419 case V4L2_CID_BLACK_LEVEL: return "Black Level";
420 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
421 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
422 case V4L2_CID_RED_BALANCE: return "Red Balance";
423 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
424 case V4L2_CID_GAMMA: return "Gamma";
425 case V4L2_CID_EXPOSURE: return "Exposure";
426 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
427 case V4L2_CID_GAIN: return "Gain";
428 case V4L2_CID_HFLIP: return "Horizontal Flip";
429 case V4L2_CID_VFLIP: return "Vertical Flip";
430 case V4L2_CID_HCENTER: return "Horizontal Center";
431 case V4L2_CID_VCENTER: return "Vertical Center";
432 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
433 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
434 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
435 case V4L2_CID_SHARPNESS: return "Sharpness";
436 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
437 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
438 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
439 case V4L2_CID_COLOR_KILLER: return "Color Killer";
440 case V4L2_CID_COLORFX: return "Color Effects";
441 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
442 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
443 case V4L2_CID_ROTATE: return "Rotate";
444 case V4L2_CID_BG_COLOR: return "Background Color";
445
446 /* MPEG controls */
447 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
448 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
449 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
450 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
451 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
452 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
453 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
454 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
455 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
456 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
457 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
458 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
459 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
460 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
461 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
462 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
463 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
464 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
465 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
466 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
467 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
468 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
469 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
470 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
471 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
472 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
473 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
474 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
475 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
476 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
477 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
478 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
479 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
480
481 /* CAMERA controls */
482 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
483 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
484 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
485 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
486 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
487 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
488 case V4L2_CID_PAN_RESET: return "Pan, Reset";
489 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
490 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
491 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
492 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
493 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
494 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
495 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
496 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
497 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
498 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
499 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
500 case V4L2_CID_PRIVACY: return "Privacy";
501
502 /* FM Radio Modulator control */
503 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
504 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
505 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
506 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
507 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
508 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
509 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
510 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
511 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
512 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
513 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
514 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
515 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
516 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
517 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
518 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
519 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
520 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
521 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
522 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
523
524 default:
525 return NULL;
526 }
527}
528EXPORT_SYMBOL(v4l2_ctrl_get_name);
529
530/* Fill in a struct v4l2_queryctrl */ 176/* Fill in a struct v4l2_queryctrl */
531int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def) 177int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
532{ 178{
533 const char *name = v4l2_ctrl_get_name(qctrl->id); 179 const char *name;
180
181 v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
182 &min, &max, &step, &def, &qctrl->flags);
534 183
535 qctrl->flags = 0;
536 if (name == NULL) 184 if (name == NULL)
537 return -EINVAL; 185 return -EINVAL;
538 186
539 switch (qctrl->id) {
540 case V4L2_CID_AUDIO_MUTE:
541 case V4L2_CID_AUDIO_LOUDNESS:
542 case V4L2_CID_AUTO_WHITE_BALANCE:
543 case V4L2_CID_AUTOGAIN:
544 case V4L2_CID_HFLIP:
545 case V4L2_CID_VFLIP:
546 case V4L2_CID_HUE_AUTO:
547 case V4L2_CID_CHROMA_AGC:
548 case V4L2_CID_COLOR_KILLER:
549 case V4L2_CID_MPEG_AUDIO_MUTE:
550 case V4L2_CID_MPEG_VIDEO_MUTE:
551 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
552 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
553 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
554 case V4L2_CID_FOCUS_AUTO:
555 case V4L2_CID_PRIVACY:
556 case V4L2_CID_AUDIO_LIMITER_ENABLED:
557 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
558 case V4L2_CID_PILOT_TONE_ENABLED:
559 qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
560 min = 0;
561 max = step = 1;
562 break;
563 case V4L2_CID_PAN_RESET:
564 case V4L2_CID_TILT_RESET:
565 qctrl->type = V4L2_CTRL_TYPE_BUTTON;
566 qctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
567 min = max = step = def = 0;
568 break;
569 case V4L2_CID_POWER_LINE_FREQUENCY:
570 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
571 case V4L2_CID_MPEG_AUDIO_ENCODING:
572 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
573 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
574 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
575 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
576 case V4L2_CID_MPEG_AUDIO_MODE:
577 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
578 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
579 case V4L2_CID_MPEG_AUDIO_CRC:
580 case V4L2_CID_MPEG_VIDEO_ENCODING:
581 case V4L2_CID_MPEG_VIDEO_ASPECT:
582 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
583 case V4L2_CID_MPEG_STREAM_TYPE:
584 case V4L2_CID_MPEG_STREAM_VBI_FMT:
585 case V4L2_CID_EXPOSURE_AUTO:
586 case V4L2_CID_COLORFX:
587 case V4L2_CID_TUNE_PREEMPHASIS:
588 qctrl->type = V4L2_CTRL_TYPE_MENU;
589 step = 1;
590 break;
591 case V4L2_CID_RDS_TX_PS_NAME:
592 case V4L2_CID_RDS_TX_RADIO_TEXT:
593 qctrl->type = V4L2_CTRL_TYPE_STRING;
594 break;
595 case V4L2_CID_USER_CLASS:
596 case V4L2_CID_CAMERA_CLASS:
597 case V4L2_CID_MPEG_CLASS:
598 case V4L2_CID_FM_TX_CLASS:
599 qctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
600 qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
601 min = max = step = def = 0;
602 break;
603 case V4L2_CID_BG_COLOR:
604 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
605 step = 1;
606 min = 0;
607 /* Max is calculated as RGB888 that is 2^24 */
608 max = 0xFFFFFF;
609 break;
610 default:
611 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
612 break;
613 }
614 switch (qctrl->id) {
615 case V4L2_CID_MPEG_AUDIO_ENCODING:
616 case V4L2_CID_MPEG_AUDIO_MODE:
617 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
618 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
619 case V4L2_CID_MPEG_STREAM_TYPE:
620 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
621 break;
622 case V4L2_CID_AUDIO_VOLUME:
623 case V4L2_CID_AUDIO_BALANCE:
624 case V4L2_CID_AUDIO_BASS:
625 case V4L2_CID_AUDIO_TREBLE:
626 case V4L2_CID_BRIGHTNESS:
627 case V4L2_CID_CONTRAST:
628 case V4L2_CID_SATURATION:
629 case V4L2_CID_HUE:
630 case V4L2_CID_RED_BALANCE:
631 case V4L2_CID_BLUE_BALANCE:
632 case V4L2_CID_GAMMA:
633 case V4L2_CID_SHARPNESS:
634 case V4L2_CID_CHROMA_GAIN:
635 case V4L2_CID_RDS_TX_DEVIATION:
636 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
637 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
638 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
639 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
640 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
641 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
642 case V4L2_CID_PILOT_TONE_DEVIATION:
643 case V4L2_CID_PILOT_TONE_FREQUENCY:
644 case V4L2_CID_TUNE_POWER_LEVEL:
645 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
646 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
647 break;
648 case V4L2_CID_PAN_RELATIVE:
649 case V4L2_CID_TILT_RELATIVE:
650 case V4L2_CID_FOCUS_RELATIVE:
651 case V4L2_CID_IRIS_RELATIVE:
652 case V4L2_CID_ZOOM_RELATIVE:
653 qctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
654 break;
655 }
656 qctrl->minimum = min; 187 qctrl->minimum = min;
657 qctrl->maximum = max; 188 qctrl->maximum = max;
658 qctrl->step = step; 189 qctrl->step = step;
diff --git a/drivers/media/video/v4l2-ctrls.c b/drivers/media/video/v4l2-ctrls.c
new file mode 100644
index 00000000000..84c1a53a727
--- /dev/null
+++ b/drivers/media/video/v4l2-ctrls.c
@@ -0,0 +1,1851 @@
1/*
2 V4L2 controls framework implementation.
3
4 Copyright (C) 2010 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/ctype.h>
22#include <media/v4l2-ioctl.h>
23#include <media/v4l2-device.h>
24#include <media/v4l2-ctrls.h>
25#include <media/v4l2-dev.h>
26
27/* Internal temporary helper struct, one for each v4l2_ext_control */
28struct ctrl_helper {
29 /* The control corresponding to the v4l2_ext_control ID field. */
30 struct v4l2_ctrl *ctrl;
31 /* Used internally to mark whether this control was already
32 processed. */
33 bool handled;
34};
35
36/* Returns NULL or a character pointer array containing the menu for
37 the given control ID. The pointer array ends with a NULL pointer.
38 An empty string signifies a menu entry that is invalid. This allows
39 drivers to disable certain options if it is not supported. */
40const char **v4l2_ctrl_get_menu(u32 id)
41{
42 static const char *mpeg_audio_sampling_freq[] = {
43 "44.1 kHz",
44 "48 kHz",
45 "32 kHz",
46 NULL
47 };
48 static const char *mpeg_audio_encoding[] = {
49 "MPEG-1/2 Layer I",
50 "MPEG-1/2 Layer II",
51 "MPEG-1/2 Layer III",
52 "MPEG-2/4 AAC",
53 "AC-3",
54 NULL
55 };
56 static const char *mpeg_audio_l1_bitrate[] = {
57 "32 kbps",
58 "64 kbps",
59 "96 kbps",
60 "128 kbps",
61 "160 kbps",
62 "192 kbps",
63 "224 kbps",
64 "256 kbps",
65 "288 kbps",
66 "320 kbps",
67 "352 kbps",
68 "384 kbps",
69 "416 kbps",
70 "448 kbps",
71 NULL
72 };
73 static const char *mpeg_audio_l2_bitrate[] = {
74 "32 kbps",
75 "48 kbps",
76 "56 kbps",
77 "64 kbps",
78 "80 kbps",
79 "96 kbps",
80 "112 kbps",
81 "128 kbps",
82 "160 kbps",
83 "192 kbps",
84 "224 kbps",
85 "256 kbps",
86 "320 kbps",
87 "384 kbps",
88 NULL
89 };
90 static const char *mpeg_audio_l3_bitrate[] = {
91 "32 kbps",
92 "40 kbps",
93 "48 kbps",
94 "56 kbps",
95 "64 kbps",
96 "80 kbps",
97 "96 kbps",
98 "112 kbps",
99 "128 kbps",
100 "160 kbps",
101 "192 kbps",
102 "224 kbps",
103 "256 kbps",
104 "320 kbps",
105 NULL
106 };
107 static const char *mpeg_audio_ac3_bitrate[] = {
108 "32 kbps",
109 "40 kbps",
110 "48 kbps",
111 "56 kbps",
112 "64 kbps",
113 "80 kbps",
114 "96 kbps",
115 "112 kbps",
116 "128 kbps",
117 "160 kbps",
118 "192 kbps",
119 "224 kbps",
120 "256 kbps",
121 "320 kbps",
122 "384 kbps",
123 "448 kbps",
124 "512 kbps",
125 "576 kbps",
126 "640 kbps",
127 NULL
128 };
129 static const char *mpeg_audio_mode[] = {
130 "Stereo",
131 "Joint Stereo",
132 "Dual",
133 "Mono",
134 NULL
135 };
136 static const char *mpeg_audio_mode_extension[] = {
137 "Bound 4",
138 "Bound 8",
139 "Bound 12",
140 "Bound 16",
141 NULL
142 };
143 static const char *mpeg_audio_emphasis[] = {
144 "No Emphasis",
145 "50/15 us",
146 "CCITT J17",
147 NULL
148 };
149 static const char *mpeg_audio_crc[] = {
150 "No CRC",
151 "16-bit CRC",
152 NULL
153 };
154 static const char *mpeg_video_encoding[] = {
155 "MPEG-1",
156 "MPEG-2",
157 "MPEG-4 AVC",
158 NULL
159 };
160 static const char *mpeg_video_aspect[] = {
161 "1x1",
162 "4x3",
163 "16x9",
164 "2.21x1",
165 NULL
166 };
167 static const char *mpeg_video_bitrate_mode[] = {
168 "Variable Bitrate",
169 "Constant Bitrate",
170 NULL
171 };
172 static const char *mpeg_stream_type[] = {
173 "MPEG-2 Program Stream",
174 "MPEG-2 Transport Stream",
175 "MPEG-1 System Stream",
176 "MPEG-2 DVD-compatible Stream",
177 "MPEG-1 VCD-compatible Stream",
178 "MPEG-2 SVCD-compatible Stream",
179 NULL
180 };
181 static const char *mpeg_stream_vbi_fmt[] = {
182 "No VBI",
183 "Private packet, IVTV format",
184 NULL
185 };
186 static const char *camera_power_line_frequency[] = {
187 "Disabled",
188 "50 Hz",
189 "60 Hz",
190 NULL
191 };
192 static const char *camera_exposure_auto[] = {
193 "Auto Mode",
194 "Manual Mode",
195 "Shutter Priority Mode",
196 "Aperture Priority Mode",
197 NULL
198 };
199 static const char *colorfx[] = {
200 "None",
201 "Black & White",
202 "Sepia",
203 "Negative",
204 "Emboss",
205 "Sketch",
206 "Sky blue",
207 "Grass green",
208 "Skin whiten",
209 "Vivid",
210 NULL
211 };
212 static const char *tune_preemphasis[] = {
213 "No preemphasis",
214 "50 useconds",
215 "75 useconds",
216 NULL,
217 };
218
219 switch (id) {
220 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
221 return mpeg_audio_sampling_freq;
222 case V4L2_CID_MPEG_AUDIO_ENCODING:
223 return mpeg_audio_encoding;
224 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
225 return mpeg_audio_l1_bitrate;
226 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
227 return mpeg_audio_l2_bitrate;
228 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
229 return mpeg_audio_l3_bitrate;
230 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
231 return mpeg_audio_ac3_bitrate;
232 case V4L2_CID_MPEG_AUDIO_MODE:
233 return mpeg_audio_mode;
234 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
235 return mpeg_audio_mode_extension;
236 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
237 return mpeg_audio_emphasis;
238 case V4L2_CID_MPEG_AUDIO_CRC:
239 return mpeg_audio_crc;
240 case V4L2_CID_MPEG_VIDEO_ENCODING:
241 return mpeg_video_encoding;
242 case V4L2_CID_MPEG_VIDEO_ASPECT:
243 return mpeg_video_aspect;
244 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
245 return mpeg_video_bitrate_mode;
246 case V4L2_CID_MPEG_STREAM_TYPE:
247 return mpeg_stream_type;
248 case V4L2_CID_MPEG_STREAM_VBI_FMT:
249 return mpeg_stream_vbi_fmt;
250 case V4L2_CID_POWER_LINE_FREQUENCY:
251 return camera_power_line_frequency;
252 case V4L2_CID_EXPOSURE_AUTO:
253 return camera_exposure_auto;
254 case V4L2_CID_COLORFX:
255 return colorfx;
256 case V4L2_CID_TUNE_PREEMPHASIS:
257 return tune_preemphasis;
258 default:
259 return NULL;
260 }
261}
262EXPORT_SYMBOL(v4l2_ctrl_get_menu);
263
264/* Return the control name. */
265const char *v4l2_ctrl_get_name(u32 id)
266{
267 switch (id) {
268 /* USER controls */
269 /* Keep the order of the 'case's the same as in videodev2.h! */
270 case V4L2_CID_USER_CLASS: return "User Controls";
271 case V4L2_CID_BRIGHTNESS: return "Brightness";
272 case V4L2_CID_CONTRAST: return "Contrast";
273 case V4L2_CID_SATURATION: return "Saturation";
274 case V4L2_CID_HUE: return "Hue";
275 case V4L2_CID_AUDIO_VOLUME: return "Volume";
276 case V4L2_CID_AUDIO_BALANCE: return "Balance";
277 case V4L2_CID_AUDIO_BASS: return "Bass";
278 case V4L2_CID_AUDIO_TREBLE: return "Treble";
279 case V4L2_CID_AUDIO_MUTE: return "Mute";
280 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
281 case V4L2_CID_BLACK_LEVEL: return "Black Level";
282 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
283 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
284 case V4L2_CID_RED_BALANCE: return "Red Balance";
285 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
286 case V4L2_CID_GAMMA: return "Gamma";
287 case V4L2_CID_EXPOSURE: return "Exposure";
288 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
289 case V4L2_CID_GAIN: return "Gain";
290 case V4L2_CID_HFLIP: return "Horizontal Flip";
291 case V4L2_CID_VFLIP: return "Vertical Flip";
292 case V4L2_CID_HCENTER: return "Horizontal Center";
293 case V4L2_CID_VCENTER: return "Vertical Center";
294 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
295 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
296 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
297 case V4L2_CID_SHARPNESS: return "Sharpness";
298 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
299 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
300 case V4L2_CID_COLOR_KILLER: return "Color Killer";
301 case V4L2_CID_COLORFX: return "Color Effects";
302 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
303 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
304 case V4L2_CID_ROTATE: return "Rotate";
305 case V4L2_CID_BG_COLOR: return "Background Color";
306 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
307
308 /* MPEG controls */
309 /* Keep the order of the 'case's the same as in videodev2.h! */
310 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
311 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
312 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
313 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
314 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
315 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
316 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
317 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
318 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
319 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
320 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
321 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
322 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
323 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
324 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
325 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
326 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
327 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
328 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
329 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
330 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
331 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
332 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
333 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
334 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
335 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
336 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
337 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
338 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
339 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
340 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
341 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
342 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
343
344 /* CAMERA controls */
345 /* Keep the order of the 'case's the same as in videodev2.h! */
346 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
347 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
348 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
349 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
350 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
351 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
352 case V4L2_CID_PAN_RESET: return "Pan, Reset";
353 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
354 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
355 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
356 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
357 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
358 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
359 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
360 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
361 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
362 case V4L2_CID_PRIVACY: return "Privacy";
363 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
364 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
365
366 /* FM Radio Modulator control */
367 /* Keep the order of the 'case's the same as in videodev2.h! */
368 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
369 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
370 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
371 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
372 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
373 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
374 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
375 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
376 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
377 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
378 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
379 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
380 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
381 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
382 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
383 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
384 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
385 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
386 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
387 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
388
389 default:
390 return NULL;
391 }
392}
393EXPORT_SYMBOL(v4l2_ctrl_get_name);
394
395void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
396 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
397{
398 *name = v4l2_ctrl_get_name(id);
399 *flags = 0;
400
401 switch (id) {
402 case V4L2_CID_AUDIO_MUTE:
403 case V4L2_CID_AUDIO_LOUDNESS:
404 case V4L2_CID_AUTO_WHITE_BALANCE:
405 case V4L2_CID_AUTOGAIN:
406 case V4L2_CID_HFLIP:
407 case V4L2_CID_VFLIP:
408 case V4L2_CID_HUE_AUTO:
409 case V4L2_CID_CHROMA_AGC:
410 case V4L2_CID_COLOR_KILLER:
411 case V4L2_CID_MPEG_AUDIO_MUTE:
412 case V4L2_CID_MPEG_VIDEO_MUTE:
413 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
414 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
415 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
416 case V4L2_CID_FOCUS_AUTO:
417 case V4L2_CID_PRIVACY:
418 case V4L2_CID_AUDIO_LIMITER_ENABLED:
419 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
420 case V4L2_CID_PILOT_TONE_ENABLED:
421 *type = V4L2_CTRL_TYPE_BOOLEAN;
422 *min = 0;
423 *max = *step = 1;
424 break;
425 case V4L2_CID_PAN_RESET:
426 case V4L2_CID_TILT_RESET:
427 *type = V4L2_CTRL_TYPE_BUTTON;
428 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
429 *min = *max = *step = *def = 0;
430 break;
431 case V4L2_CID_POWER_LINE_FREQUENCY:
432 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
433 case V4L2_CID_MPEG_AUDIO_ENCODING:
434 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
435 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
436 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
437 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
438 case V4L2_CID_MPEG_AUDIO_MODE:
439 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
440 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
441 case V4L2_CID_MPEG_AUDIO_CRC:
442 case V4L2_CID_MPEG_VIDEO_ENCODING:
443 case V4L2_CID_MPEG_VIDEO_ASPECT:
444 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
445 case V4L2_CID_MPEG_STREAM_TYPE:
446 case V4L2_CID_MPEG_STREAM_VBI_FMT:
447 case V4L2_CID_EXPOSURE_AUTO:
448 case V4L2_CID_COLORFX:
449 case V4L2_CID_TUNE_PREEMPHASIS:
450 *type = V4L2_CTRL_TYPE_MENU;
451 break;
452 case V4L2_CID_RDS_TX_PS_NAME:
453 case V4L2_CID_RDS_TX_RADIO_TEXT:
454 *type = V4L2_CTRL_TYPE_STRING;
455 break;
456 case V4L2_CID_USER_CLASS:
457 case V4L2_CID_CAMERA_CLASS:
458 case V4L2_CID_MPEG_CLASS:
459 case V4L2_CID_FM_TX_CLASS:
460 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
461 /* You can neither read not write these */
462 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
463 *min = *max = *step = *def = 0;
464 break;
465 case V4L2_CID_BG_COLOR:
466 *type = V4L2_CTRL_TYPE_INTEGER;
467 *step = 1;
468 *min = 0;
469 /* Max is calculated as RGB888 that is 2^24 */
470 *max = 0xFFFFFF;
471 break;
472 default:
473 *type = V4L2_CTRL_TYPE_INTEGER;
474 break;
475 }
476 switch (id) {
477 case V4L2_CID_MPEG_AUDIO_ENCODING:
478 case V4L2_CID_MPEG_AUDIO_MODE:
479 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
480 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
481 case V4L2_CID_MPEG_STREAM_TYPE:
482 *flags |= V4L2_CTRL_FLAG_UPDATE;
483 break;
484 case V4L2_CID_AUDIO_VOLUME:
485 case V4L2_CID_AUDIO_BALANCE:
486 case V4L2_CID_AUDIO_BASS:
487 case V4L2_CID_AUDIO_TREBLE:
488 case V4L2_CID_BRIGHTNESS:
489 case V4L2_CID_CONTRAST:
490 case V4L2_CID_SATURATION:
491 case V4L2_CID_HUE:
492 case V4L2_CID_RED_BALANCE:
493 case V4L2_CID_BLUE_BALANCE:
494 case V4L2_CID_GAMMA:
495 case V4L2_CID_SHARPNESS:
496 case V4L2_CID_CHROMA_GAIN:
497 case V4L2_CID_RDS_TX_DEVIATION:
498 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
499 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
500 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
501 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
502 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
503 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
504 case V4L2_CID_PILOT_TONE_DEVIATION:
505 case V4L2_CID_PILOT_TONE_FREQUENCY:
506 case V4L2_CID_TUNE_POWER_LEVEL:
507 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
508 *flags |= V4L2_CTRL_FLAG_SLIDER;
509 break;
510 case V4L2_CID_PAN_RELATIVE:
511 case V4L2_CID_TILT_RELATIVE:
512 case V4L2_CID_FOCUS_RELATIVE:
513 case V4L2_CID_IRIS_RELATIVE:
514 case V4L2_CID_ZOOM_RELATIVE:
515 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
516 break;
517 }
518}
519EXPORT_SYMBOL(v4l2_ctrl_fill);
520
521/* Helper function to determine whether the control type is compatible with
522 VIDIOC_G/S_CTRL. */
523static bool type_is_int(const struct v4l2_ctrl *ctrl)
524{
525 switch (ctrl->type) {
526 case V4L2_CTRL_TYPE_INTEGER64:
527 case V4L2_CTRL_TYPE_STRING:
528 /* Nope, these need v4l2_ext_control */
529 return false;
530 default:
531 return true;
532 }
533}
534
535/* Helper function: copy the current control value back to the caller */
536static int cur_to_user(struct v4l2_ext_control *c,
537 struct v4l2_ctrl *ctrl)
538{
539 u32 len;
540
541 switch (ctrl->type) {
542 case V4L2_CTRL_TYPE_STRING:
543 len = strlen(ctrl->cur.string);
544 if (c->size < len + 1) {
545 c->size = len + 1;
546 return -ENOSPC;
547 }
548 return copy_to_user(c->string, ctrl->cur.string,
549 len + 1) ? -EFAULT : 0;
550 case V4L2_CTRL_TYPE_INTEGER64:
551 c->value64 = ctrl->cur.val64;
552 break;
553 default:
554 c->value = ctrl->cur.val;
555 break;
556 }
557 return 0;
558}
559
560/* Helper function: copy the caller-provider value as the new control value */
561static int user_to_new(struct v4l2_ext_control *c,
562 struct v4l2_ctrl *ctrl)
563{
564 int ret;
565 u32 size;
566
567 ctrl->has_new = 1;
568 switch (ctrl->type) {
569 case V4L2_CTRL_TYPE_INTEGER64:
570 ctrl->val64 = c->value64;
571 break;
572 case V4L2_CTRL_TYPE_STRING:
573 size = c->size;
574 if (size == 0)
575 return -ERANGE;
576 if (size > ctrl->maximum + 1)
577 size = ctrl->maximum + 1;
578 ret = copy_from_user(ctrl->string, c->string, size);
579 if (!ret) {
580 char last = ctrl->string[size - 1];
581
582 ctrl->string[size - 1] = 0;
583 /* If the string was longer than ctrl->maximum,
584 then return an error. */
585 if (strlen(ctrl->string) == ctrl->maximum && last)
586 return -ERANGE;
587 }
588 return ret ? -EFAULT : 0;
589 default:
590 ctrl->val = c->value;
591 break;
592 }
593 return 0;
594}
595
596/* Helper function: copy the new control value back to the caller */
597static int new_to_user(struct v4l2_ext_control *c,
598 struct v4l2_ctrl *ctrl)
599{
600 u32 len;
601
602 switch (ctrl->type) {
603 case V4L2_CTRL_TYPE_STRING:
604 len = strlen(ctrl->string);
605 if (c->size < len + 1) {
606 c->size = ctrl->maximum + 1;
607 return -ENOSPC;
608 }
609 return copy_to_user(c->string, ctrl->string,
610 len + 1) ? -EFAULT : 0;
611 case V4L2_CTRL_TYPE_INTEGER64:
612 c->value64 = ctrl->val64;
613 break;
614 default:
615 c->value = ctrl->val;
616 break;
617 }
618 return 0;
619}
620
621/* Copy the new value to the current value. */
622static void new_to_cur(struct v4l2_ctrl *ctrl)
623{
624 if (ctrl == NULL)
625 return;
626 switch (ctrl->type) {
627 case V4L2_CTRL_TYPE_STRING:
628 /* strings are always 0-terminated */
629 strcpy(ctrl->cur.string, ctrl->string);
630 break;
631 case V4L2_CTRL_TYPE_INTEGER64:
632 ctrl->cur.val64 = ctrl->val64;
633 break;
634 default:
635 ctrl->cur.val = ctrl->val;
636 break;
637 }
638}
639
640/* Copy the current value to the new value */
641static void cur_to_new(struct v4l2_ctrl *ctrl)
642{
643 if (ctrl == NULL)
644 return;
645 switch (ctrl->type) {
646 case V4L2_CTRL_TYPE_STRING:
647 /* strings are always 0-terminated */
648 strcpy(ctrl->string, ctrl->cur.string);
649 break;
650 case V4L2_CTRL_TYPE_INTEGER64:
651 ctrl->val64 = ctrl->cur.val64;
652 break;
653 default:
654 ctrl->val = ctrl->cur.val;
655 break;
656 }
657}
658
659/* Return non-zero if one or more of the controls in the cluster has a new
660 value that differs from the current value. */
661static int cluster_changed(struct v4l2_ctrl *master)
662{
663 int diff = 0;
664 int i;
665
666 for (i = 0; !diff && i < master->ncontrols; i++) {
667 struct v4l2_ctrl *ctrl = master->cluster[i];
668
669 if (ctrl == NULL)
670 continue;
671 switch (ctrl->type) {
672 case V4L2_CTRL_TYPE_BUTTON:
673 /* Button controls are always 'different' */
674 return 1;
675 case V4L2_CTRL_TYPE_STRING:
676 /* strings are always 0-terminated */
677 diff = strcmp(ctrl->string, ctrl->cur.string);
678 break;
679 case V4L2_CTRL_TYPE_INTEGER64:
680 diff = ctrl->val64 != ctrl->cur.val64;
681 break;
682 default:
683 diff = ctrl->val != ctrl->cur.val;
684 break;
685 }
686 }
687 return diff;
688}
689
690/* Validate a new control */
691static int validate_new(struct v4l2_ctrl *ctrl)
692{
693 s32 val = ctrl->val;
694 char *s = ctrl->string;
695 u32 offset;
696 size_t len;
697
698 switch (ctrl->type) {
699 case V4L2_CTRL_TYPE_INTEGER:
700 /* Round towards the closest legal value */
701 val += ctrl->step / 2;
702 if (val < ctrl->minimum)
703 val = ctrl->minimum;
704 if (val > ctrl->maximum)
705 val = ctrl->maximum;
706 offset = val - ctrl->minimum;
707 offset = ctrl->step * (offset / ctrl->step);
708 val = ctrl->minimum + offset;
709 ctrl->val = val;
710 return 0;
711
712 case V4L2_CTRL_TYPE_BOOLEAN:
713 ctrl->val = !!ctrl->val;
714 return 0;
715
716 case V4L2_CTRL_TYPE_MENU:
717 if (val < ctrl->minimum || val > ctrl->maximum)
718 return -ERANGE;
719 if (ctrl->qmenu[val][0] == '\0' ||
720 (ctrl->menu_skip_mask & (1 << val)))
721 return -EINVAL;
722 return 0;
723
724 case V4L2_CTRL_TYPE_BUTTON:
725 case V4L2_CTRL_TYPE_CTRL_CLASS:
726 ctrl->val64 = 0;
727 return 0;
728
729 case V4L2_CTRL_TYPE_INTEGER64:
730 return 0;
731
732 case V4L2_CTRL_TYPE_STRING:
733 len = strlen(s);
734 if (len < ctrl->minimum)
735 return -ERANGE;
736 if ((len - ctrl->minimum) % ctrl->step)
737 return -ERANGE;
738 return 0;
739
740 default:
741 return -EINVAL;
742 }
743}
744
745static inline u32 node2id(struct list_head *node)
746{
747 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
748}
749
750/* Set the handler's error code if it wasn't set earlier already */
751static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
752{
753 if (hdl->error == 0)
754 hdl->error = err;
755 return err;
756}
757
758/* Initialize the handler */
759int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
760 unsigned nr_of_controls_hint)
761{
762 mutex_init(&hdl->lock);
763 INIT_LIST_HEAD(&hdl->ctrls);
764 INIT_LIST_HEAD(&hdl->ctrl_refs);
765 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
766 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
767 GFP_KERNEL);
768 hdl->error = hdl->buckets ? 0 : -ENOMEM;
769 return hdl->error;
770}
771EXPORT_SYMBOL(v4l2_ctrl_handler_init);
772
773/* Free all controls and control refs */
774void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
775{
776 struct v4l2_ctrl_ref *ref, *next_ref;
777 struct v4l2_ctrl *ctrl, *next_ctrl;
778
779 if (hdl == NULL || hdl->buckets == NULL)
780 return;
781
782 mutex_lock(&hdl->lock);
783 /* Free all nodes */
784 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
785 list_del(&ref->node);
786 kfree(ref);
787 }
788 /* Free all controls owned by the handler */
789 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
790 list_del(&ctrl->node);
791 kfree(ctrl);
792 }
793 kfree(hdl->buckets);
794 hdl->buckets = NULL;
795 hdl->cached = NULL;
796 hdl->error = 0;
797 mutex_unlock(&hdl->lock);
798}
799EXPORT_SYMBOL(v4l2_ctrl_handler_free);
800
801/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
802 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
803 with applications that do not use the NEXT_CTRL flag.
804
805 We just find the n-th private user control. It's O(N), but that should not
806 be an issue in this particular case. */
807static struct v4l2_ctrl_ref *find_private_ref(
808 struct v4l2_ctrl_handler *hdl, u32 id)
809{
810 struct v4l2_ctrl_ref *ref;
811
812 id -= V4L2_CID_PRIVATE_BASE;
813 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
814 /* Search for private user controls that are compatible with
815 VIDIOC_G/S_CTRL. */
816 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
817 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
818 if (!type_is_int(ref->ctrl))
819 continue;
820 if (id == 0)
821 return ref;
822 id--;
823 }
824 }
825 return NULL;
826}
827
828/* Find a control with the given ID. */
829static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
830{
831 struct v4l2_ctrl_ref *ref;
832 int bucket;
833
834 id &= V4L2_CTRL_ID_MASK;
835
836 /* Old-style private controls need special handling */
837 if (id >= V4L2_CID_PRIVATE_BASE)
838 return find_private_ref(hdl, id);
839 bucket = id % hdl->nr_of_buckets;
840
841 /* Simple optimization: cache the last control found */
842 if (hdl->cached && hdl->cached->ctrl->id == id)
843 return hdl->cached;
844
845 /* Not in cache, search the hash */
846 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
847 while (ref && ref->ctrl->id != id)
848 ref = ref->next;
849
850 if (ref)
851 hdl->cached = ref; /* cache it! */
852 return ref;
853}
854
855/* Find a control with the given ID. Take the handler's lock first. */
856static struct v4l2_ctrl_ref *find_ref_lock(
857 struct v4l2_ctrl_handler *hdl, u32 id)
858{
859 struct v4l2_ctrl_ref *ref = NULL;
860
861 if (hdl) {
862 mutex_lock(&hdl->lock);
863 ref = find_ref(hdl, id);
864 mutex_unlock(&hdl->lock);
865 }
866 return ref;
867}
868
869/* Find a control with the given ID. */
870struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
871{
872 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
873
874 return ref ? ref->ctrl : NULL;
875}
876EXPORT_SYMBOL(v4l2_ctrl_find);
877
878/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
879static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
880 struct v4l2_ctrl *ctrl)
881{
882 struct v4l2_ctrl_ref *ref;
883 struct v4l2_ctrl_ref *new_ref;
884 u32 id = ctrl->id;
885 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
886 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
887
888 /* Automatically add the control class if it is not yet present. */
889 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
890 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
891 return hdl->error;
892
893 if (hdl->error)
894 return hdl->error;
895
896 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
897 if (!new_ref)
898 return handler_set_err(hdl, -ENOMEM);
899 new_ref->ctrl = ctrl;
900 if (ctrl->handler == hdl) {
901 /* By default each control starts in a cluster of its own.
902 new_ref->ctrl is basically a cluster array with one
903 element, so that's perfect to use as the cluster pointer.
904 But only do this for the handler that owns the control. */
905 ctrl->cluster = &new_ref->ctrl;
906 ctrl->ncontrols = 1;
907 }
908
909 INIT_LIST_HEAD(&new_ref->node);
910
911 mutex_lock(&hdl->lock);
912
913 /* Add immediately at the end of the list if the list is empty, or if
914 the last element in the list has a lower ID.
915 This ensures that when elements are added in ascending order the
916 insertion is an O(1) operation. */
917 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
918 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
919 goto insert_in_hash;
920 }
921
922 /* Find insert position in sorted list */
923 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
924 if (ref->ctrl->id < id)
925 continue;
926 /* Don't add duplicates */
927 if (ref->ctrl->id == id) {
928 kfree(new_ref);
929 goto unlock;
930 }
931 list_add(&new_ref->node, ref->node.prev);
932 break;
933 }
934
935insert_in_hash:
936 /* Insert the control node in the hash */
937 new_ref->next = hdl->buckets[bucket];
938 hdl->buckets[bucket] = new_ref;
939
940unlock:
941 mutex_unlock(&hdl->lock);
942 return 0;
943}
944
945/* Add a new control */
946static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
947 const struct v4l2_ctrl_ops *ops,
948 u32 id, const char *name, enum v4l2_ctrl_type type,
949 s32 min, s32 max, u32 step, s32 def,
950 u32 flags, const char **qmenu, void *priv)
951{
952 struct v4l2_ctrl *ctrl;
953 unsigned sz_extra = 0;
954
955 if (hdl->error)
956 return NULL;
957
958 /* Sanity checks */
959 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
960 def < min || def > max || max < min ||
961 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
962 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
963 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
964 handler_set_err(hdl, -ERANGE);
965 return NULL;
966 }
967
968 if (type == V4L2_CTRL_TYPE_BUTTON)
969 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
970 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
971 flags |= V4L2_CTRL_FLAG_READ_ONLY;
972 else if (type == V4L2_CTRL_TYPE_STRING)
973 sz_extra += 2 * (max + 1);
974
975 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
976 if (ctrl == NULL) {
977 handler_set_err(hdl, -ENOMEM);
978 return NULL;
979 }
980
981 INIT_LIST_HEAD(&ctrl->node);
982 ctrl->handler = hdl;
983 ctrl->ops = ops;
984 ctrl->id = id;
985 ctrl->name = name;
986 ctrl->type = type;
987 ctrl->flags = flags;
988 ctrl->minimum = min;
989 ctrl->maximum = max;
990 ctrl->step = step;
991 ctrl->qmenu = qmenu;
992 ctrl->priv = priv;
993 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
994
995 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
996 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
997 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
998 if (ctrl->minimum)
999 memset(ctrl->cur.string, ' ', ctrl->minimum);
1000 }
1001 if (handler_new_ref(hdl, ctrl)) {
1002 kfree(ctrl);
1003 return NULL;
1004 }
1005 mutex_lock(&hdl->lock);
1006 list_add_tail(&ctrl->node, &hdl->ctrls);
1007 mutex_unlock(&hdl->lock);
1008 return ctrl;
1009}
1010
1011struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1012 const struct v4l2_ctrl_config *cfg, void *priv)
1013{
1014 bool is_menu;
1015 struct v4l2_ctrl *ctrl;
1016 const char *name = cfg->name;
1017 const char **qmenu = cfg->qmenu;
1018 enum v4l2_ctrl_type type = cfg->type;
1019 u32 flags = cfg->flags;
1020 s32 min = cfg->min;
1021 s32 max = cfg->max;
1022 u32 step = cfg->step;
1023 s32 def = cfg->def;
1024
1025 if (name == NULL)
1026 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1027 &def, &flags);
1028
1029 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1030 if (is_menu)
1031 WARN_ON(step);
1032 else
1033 WARN_ON(cfg->menu_skip_mask);
1034 if (is_menu && qmenu == NULL)
1035 qmenu = v4l2_ctrl_get_menu(cfg->id);
1036
1037 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1038 type, min, max,
1039 is_menu ? cfg->menu_skip_mask : step,
1040 def, flags, qmenu, priv);
1041 if (ctrl) {
1042 ctrl->is_private = cfg->is_private;
1043 ctrl->is_volatile = cfg->is_volatile;
1044 }
1045 return ctrl;
1046}
1047EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1048
1049/* Helper function for standard non-menu controls */
1050struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1051 const struct v4l2_ctrl_ops *ops,
1052 u32 id, s32 min, s32 max, u32 step, s32 def)
1053{
1054 const char *name;
1055 enum v4l2_ctrl_type type;
1056 u32 flags;
1057
1058 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1059 if (type == V4L2_CTRL_TYPE_MENU) {
1060 handler_set_err(hdl, -EINVAL);
1061 return NULL;
1062 }
1063 return v4l2_ctrl_new(hdl, ops, id, name, type,
1064 min, max, step, def, flags, NULL, NULL);
1065}
1066EXPORT_SYMBOL(v4l2_ctrl_new_std);
1067
1068/* Helper function for standard menu controls */
1069struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1070 const struct v4l2_ctrl_ops *ops,
1071 u32 id, s32 max, s32 mask, s32 def)
1072{
1073 const char **qmenu = v4l2_ctrl_get_menu(id);
1074 const char *name;
1075 enum v4l2_ctrl_type type;
1076 s32 min;
1077 s32 step;
1078 u32 flags;
1079
1080 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1081 if (type != V4L2_CTRL_TYPE_MENU) {
1082 handler_set_err(hdl, -EINVAL);
1083 return NULL;
1084 }
1085 return v4l2_ctrl_new(hdl, ops, id, name, type,
1086 0, max, mask, def, flags, qmenu, NULL);
1087}
1088EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1089
1090/* Add a control from another handler to this handler */
1091struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1092 struct v4l2_ctrl *ctrl)
1093{
1094 if (hdl == NULL || hdl->error)
1095 return NULL;
1096 if (ctrl == NULL) {
1097 handler_set_err(hdl, -EINVAL);
1098 return NULL;
1099 }
1100 if (ctrl->handler == hdl)
1101 return ctrl;
1102 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1103}
1104EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1105
1106/* Add the controls from another handler to our own. */
1107int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1108 struct v4l2_ctrl_handler *add)
1109{
1110 struct v4l2_ctrl *ctrl;
1111 int ret = 0;
1112
1113 /* Do nothing if either handler is NULL or if they are the same */
1114 if (!hdl || !add || hdl == add)
1115 return 0;
1116 if (hdl->error)
1117 return hdl->error;
1118 mutex_lock(&add->lock);
1119 list_for_each_entry(ctrl, &add->ctrls, node) {
1120 /* Skip handler-private controls. */
1121 if (ctrl->is_private)
1122 continue;
1123 ret = handler_new_ref(hdl, ctrl);
1124 if (ret)
1125 break;
1126 }
1127 mutex_unlock(&add->lock);
1128 return ret;
1129}
1130EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1131
1132/* Cluster controls */
1133void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1134{
1135 int i;
1136
1137 /* The first control is the master control and it must not be NULL */
1138 BUG_ON(controls[0] == NULL);
1139
1140 for (i = 0; i < ncontrols; i++) {
1141 if (controls[i]) {
1142 controls[i]->cluster = controls;
1143 controls[i]->ncontrols = ncontrols;
1144 }
1145 }
1146}
1147EXPORT_SYMBOL(v4l2_ctrl_cluster);
1148
1149/* Activate/deactivate a control. */
1150void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1151{
1152 if (ctrl == NULL)
1153 return;
1154
1155 if (!active)
1156 /* set V4L2_CTRL_FLAG_INACTIVE */
1157 set_bit(4, &ctrl->flags);
1158 else
1159 /* clear V4L2_CTRL_FLAG_INACTIVE */
1160 clear_bit(4, &ctrl->flags);
1161}
1162EXPORT_SYMBOL(v4l2_ctrl_activate);
1163
1164/* Grab/ungrab a control.
1165 Typically used when streaming starts and you want to grab controls,
1166 preventing the user from changing them.
1167
1168 Just call this and the framework will block any attempts to change
1169 these controls. */
1170void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1171{
1172 if (ctrl == NULL)
1173 return;
1174
1175 if (grabbed)
1176 /* set V4L2_CTRL_FLAG_GRABBED */
1177 set_bit(1, &ctrl->flags);
1178 else
1179 /* clear V4L2_CTRL_FLAG_GRABBED */
1180 clear_bit(1, &ctrl->flags);
1181}
1182EXPORT_SYMBOL(v4l2_ctrl_grab);
1183
1184/* Log the control name and value */
1185static void log_ctrl(const struct v4l2_ctrl *ctrl,
1186 const char *prefix, const char *colon)
1187{
1188 int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1189 int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1190
1191 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1192 return;
1193 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1194 return;
1195
1196 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1197
1198 switch (ctrl->type) {
1199 case V4L2_CTRL_TYPE_INTEGER:
1200 printk(KERN_CONT "%d", ctrl->cur.val);
1201 break;
1202 case V4L2_CTRL_TYPE_BOOLEAN:
1203 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1204 break;
1205 case V4L2_CTRL_TYPE_MENU:
1206 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1207 break;
1208 case V4L2_CTRL_TYPE_INTEGER64:
1209 printk(KERN_CONT "%lld", ctrl->cur.val64);
1210 break;
1211 case V4L2_CTRL_TYPE_STRING:
1212 printk(KERN_CONT "%s", ctrl->cur.string);
1213 break;
1214 default:
1215 printk(KERN_CONT "unknown type %d", ctrl->type);
1216 break;
1217 }
1218 if (fl_inact && fl_grabbed)
1219 printk(KERN_CONT " (inactive, grabbed)\n");
1220 else if (fl_inact)
1221 printk(KERN_CONT " (inactive)\n");
1222 else if (fl_grabbed)
1223 printk(KERN_CONT " (grabbed)\n");
1224 else
1225 printk(KERN_CONT "\n");
1226}
1227
1228/* Log all controls owned by the handler */
1229void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1230 const char *prefix)
1231{
1232 struct v4l2_ctrl *ctrl;
1233 const char *colon = "";
1234 int len;
1235
1236 if (hdl == NULL)
1237 return;
1238 if (prefix == NULL)
1239 prefix = "";
1240 len = strlen(prefix);
1241 if (len && prefix[len - 1] != ' ')
1242 colon = ": ";
1243 mutex_lock(&hdl->lock);
1244 list_for_each_entry(ctrl, &hdl->ctrls, node)
1245 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1246 log_ctrl(ctrl, prefix, colon);
1247 mutex_unlock(&hdl->lock);
1248}
1249EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1250
1251/* Call s_ctrl for all controls owned by the handler */
1252int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1253{
1254 struct v4l2_ctrl *ctrl;
1255 int ret = 0;
1256
1257 if (hdl == NULL)
1258 return 0;
1259 mutex_lock(&hdl->lock);
1260 list_for_each_entry(ctrl, &hdl->ctrls, node)
1261 ctrl->done = false;
1262
1263 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1264 struct v4l2_ctrl *master = ctrl->cluster[0];
1265 int i;
1266
1267 /* Skip if this control was already handled by a cluster. */
1268 if (ctrl->done)
1269 continue;
1270
1271 for (i = 0; i < master->ncontrols; i++)
1272 cur_to_new(master->cluster[i]);
1273
1274 /* Skip button controls and read-only controls. */
1275 if (ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1276 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1277 continue;
1278 ret = master->ops->s_ctrl(master);
1279 if (ret)
1280 break;
1281 for (i = 0; i < master->ncontrols; i++)
1282 if (master->cluster[i])
1283 master->cluster[i]->done = true;
1284 }
1285 mutex_unlock(&hdl->lock);
1286 return ret;
1287}
1288EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1289
1290/* Implement VIDIOC_QUERYCTRL */
1291int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1292{
1293 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1294 struct v4l2_ctrl_ref *ref;
1295 struct v4l2_ctrl *ctrl;
1296
1297 if (hdl == NULL)
1298 return -EINVAL;
1299
1300 mutex_lock(&hdl->lock);
1301
1302 /* Try to find it */
1303 ref = find_ref(hdl, id);
1304
1305 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1306 /* Find the next control with ID > qc->id */
1307
1308 /* Did we reach the end of the control list? */
1309 if (id >= node2id(hdl->ctrl_refs.prev)) {
1310 ref = NULL; /* Yes, so there is no next control */
1311 } else if (ref) {
1312 /* We found a control with the given ID, so just get
1313 the next one in the list. */
1314 ref = list_entry(ref->node.next, typeof(*ref), node);
1315 } else {
1316 /* No control with the given ID exists, so start
1317 searching for the next largest ID. We know there
1318 is one, otherwise the first 'if' above would have
1319 been true. */
1320 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1321 if (id < ref->ctrl->id)
1322 break;
1323 }
1324 }
1325 mutex_unlock(&hdl->lock);
1326 if (!ref)
1327 return -EINVAL;
1328
1329 ctrl = ref->ctrl;
1330 memset(qc, 0, sizeof(*qc));
1331 qc->id = ctrl->id;
1332 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1333 qc->minimum = ctrl->minimum;
1334 qc->maximum = ctrl->maximum;
1335 qc->default_value = ctrl->default_value;
1336 if (qc->type == V4L2_CTRL_TYPE_MENU)
1337 qc->step = 1;
1338 else
1339 qc->step = ctrl->step;
1340 qc->flags = ctrl->flags;
1341 qc->type = ctrl->type;
1342 return 0;
1343}
1344EXPORT_SYMBOL(v4l2_queryctrl);
1345
1346int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1347{
1348 return v4l2_queryctrl(sd->ctrl_handler, qc);
1349}
1350EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1351
1352/* Implement VIDIOC_QUERYMENU */
1353int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1354{
1355 struct v4l2_ctrl *ctrl;
1356 u32 i = qm->index;
1357
1358 ctrl = v4l2_ctrl_find(hdl, qm->id);
1359 if (!ctrl)
1360 return -EINVAL;
1361
1362 qm->reserved = 0;
1363 /* Sanity checks */
1364 if (ctrl->qmenu == NULL ||
1365 i < ctrl->minimum || i > ctrl->maximum)
1366 return -EINVAL;
1367 /* Use mask to see if this menu item should be skipped */
1368 if (ctrl->menu_skip_mask & (1 << i))
1369 return -EINVAL;
1370 /* Empty menu items should also be skipped */
1371 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1372 return -EINVAL;
1373 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1374 return 0;
1375}
1376EXPORT_SYMBOL(v4l2_querymenu);
1377
1378int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1379{
1380 return v4l2_querymenu(sd->ctrl_handler, qm);
1381}
1382EXPORT_SYMBOL(v4l2_subdev_querymenu);
1383
1384
1385
1386/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1387
1388 It is not a fully atomic operation, just best-effort only. After all, if
1389 multiple controls have to be set through multiple i2c writes (for example)
1390 then some initial writes may succeed while others fail. Thus leaving the
1391 system in an inconsistent state. The question is how much effort you are
1392 willing to spend on trying to make something atomic that really isn't.
1393
1394 From the point of view of an application the main requirement is that
1395 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1396 error should be returned without actually affecting any controls.
1397
1398 If all the values are correct, then it is acceptable to just give up
1399 in case of low-level errors.
1400
1401 It is important though that the application can tell when only a partial
1402 configuration was done. The way we do that is through the error_idx field
1403 of struct v4l2_ext_controls: if that is equal to the count field then no
1404 controls were affected. Otherwise all controls before that index were
1405 successful in performing their 'get' or 'set' operation, the control at
1406 the given index failed, and you don't know what happened with the controls
1407 after the failed one. Since if they were part of a control cluster they
1408 could have been successfully processed (if a cluster member was encountered
1409 at index < error_idx), they could have failed (if a cluster member was at
1410 error_idx), or they may not have been processed yet (if the first cluster
1411 member appeared after error_idx).
1412
1413 It is all fairly theoretical, though. In practice all you can do is to
1414 bail out. If error_idx == count, then it is an application bug. If
1415 error_idx < count then it is only an application bug if the error code was
1416 EBUSY. That usually means that something started streaming just when you
1417 tried to set the controls. In all other cases it is a driver/hardware
1418 problem and all you can do is to retry or bail out.
1419
1420 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1421 never modifies controls the error_idx is just set to whatever control
1422 has an invalid value.
1423 */
1424
1425/* Prepare for the extended g/s/try functions.
1426 Find the controls in the control array and do some basic checks. */
1427static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1428 struct v4l2_ext_controls *cs,
1429 struct ctrl_helper *helpers,
1430 bool try)
1431{
1432 u32 i;
1433
1434 for (i = 0; i < cs->count; i++) {
1435 struct v4l2_ext_control *c = &cs->controls[i];
1436 struct v4l2_ctrl *ctrl;
1437 u32 id = c->id & V4L2_CTRL_ID_MASK;
1438
1439 if (try)
1440 cs->error_idx = i;
1441
1442 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1443 return -EINVAL;
1444
1445 /* Old-style private controls are not allowed for
1446 extended controls */
1447 if (id >= V4L2_CID_PRIVATE_BASE)
1448 return -EINVAL;
1449 ctrl = v4l2_ctrl_find(hdl, id);
1450 if (ctrl == NULL)
1451 return -EINVAL;
1452 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1453 return -EINVAL;
1454
1455 helpers[i].ctrl = ctrl;
1456 helpers[i].handled = false;
1457 }
1458 return 0;
1459}
1460
1461typedef int (*cluster_func)(struct v4l2_ext_control *c,
1462 struct v4l2_ctrl *ctrl);
1463
1464/* Walk over all controls in v4l2_ext_controls belonging to the same cluster
1465 and call the provided function. */
1466static int cluster_walk(unsigned from,
1467 struct v4l2_ext_controls *cs,
1468 struct ctrl_helper *helpers,
1469 cluster_func f)
1470{
1471 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1472 int ret = 0;
1473 int i;
1474
1475 /* Find any controls from the same cluster and call the function */
1476 for (i = from; !ret && i < cs->count; i++) {
1477 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1478
1479 if (!helpers[i].handled && ctrl->cluster == cluster)
1480 ret = f(&cs->controls[i], ctrl);
1481 }
1482 return ret;
1483}
1484
1485static void cluster_done(unsigned from,
1486 struct v4l2_ext_controls *cs,
1487 struct ctrl_helper *helpers)
1488{
1489 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1490 int i;
1491
1492 /* Find any controls from the same cluster and mark them as handled */
1493 for (i = from; i < cs->count; i++)
1494 if (helpers[i].ctrl->cluster == cluster)
1495 helpers[i].handled = true;
1496}
1497
1498/* Handles the corner case where cs->count == 0. It checks whether the
1499 specified control class exists. If that class ID is 0, then it checks
1500 whether there are any controls at all. */
1501static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1502{
1503 if (ctrl_class == 0)
1504 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1505 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1506}
1507
1508
1509
1510/* Get extended controls. Allocates the helpers array if needed. */
1511int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1512{
1513 struct ctrl_helper helper[4];
1514 struct ctrl_helper *helpers = helper;
1515 int ret;
1516 int i;
1517
1518 cs->error_idx = cs->count;
1519 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1520
1521 if (hdl == NULL)
1522 return -EINVAL;
1523
1524 if (cs->count == 0)
1525 return class_check(hdl, cs->ctrl_class);
1526
1527 if (cs->count > ARRAY_SIZE(helper)) {
1528 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1529 if (helpers == NULL)
1530 return -ENOMEM;
1531 }
1532
1533 ret = prepare_ext_ctrls(hdl, cs, helpers, false);
1534
1535 for (i = 0; !ret && i < cs->count; i++)
1536 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1537 ret = -EACCES;
1538
1539 for (i = 0; !ret && i < cs->count; i++) {
1540 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1541 struct v4l2_ctrl *master = ctrl->cluster[0];
1542
1543 if (helpers[i].handled)
1544 continue;
1545
1546 cs->error_idx = i;
1547
1548 v4l2_ctrl_lock(master);
1549 /* g_volatile_ctrl will update the current control values */
1550 if (ctrl->is_volatile && master->ops->g_volatile_ctrl)
1551 ret = master->ops->g_volatile_ctrl(master);
1552 /* If OK, then copy the current control values to the caller */
1553 if (!ret)
1554 ret = cluster_walk(i, cs, helpers, cur_to_user);
1555 v4l2_ctrl_unlock(master);
1556 cluster_done(i, cs, helpers);
1557 }
1558
1559 if (cs->count > ARRAY_SIZE(helper))
1560 kfree(helpers);
1561 return ret;
1562}
1563EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1564
1565int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1566{
1567 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1568}
1569EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1570
1571/* Helper function to get a single control */
1572static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1573{
1574 struct v4l2_ctrl *master = ctrl->cluster[0];
1575 int ret = 0;
1576
1577 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1578 return -EACCES;
1579
1580 v4l2_ctrl_lock(master);
1581 /* g_volatile_ctrl will update the current control values */
1582 if (ctrl->is_volatile && master->ops->g_volatile_ctrl)
1583 ret = master->ops->g_volatile_ctrl(master);
1584 *val = ctrl->cur.val;
1585 v4l2_ctrl_unlock(master);
1586 return ret;
1587}
1588
1589int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1590{
1591 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1592
1593 if (ctrl == NULL || !type_is_int(ctrl))
1594 return -EINVAL;
1595 return get_ctrl(ctrl, &control->value);
1596}
1597EXPORT_SYMBOL(v4l2_g_ctrl);
1598
1599int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1600{
1601 return v4l2_g_ctrl(sd->ctrl_handler, control);
1602}
1603EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1604
1605s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1606{
1607 s32 val = 0;
1608
1609 /* It's a driver bug if this happens. */
1610 WARN_ON(!type_is_int(ctrl));
1611 get_ctrl(ctrl, &val);
1612 return val;
1613}
1614EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1615
1616
1617/* Core function that calls try/s_ctrl and ensures that the new value is
1618 copied to the current value on a set.
1619 Must be called with ctrl->handler->lock held. */
1620static int try_or_set_control_cluster(struct v4l2_ctrl *master, bool set)
1621{
1622 bool try = !set;
1623 int ret = 0;
1624 int i;
1625
1626 /* Go through the cluster and either validate the new value or
1627 (if no new value was set), copy the current value to the new
1628 value, ensuring a consistent view for the control ops when
1629 called. */
1630 for (i = 0; !ret && i < master->ncontrols; i++) {
1631 struct v4l2_ctrl *ctrl = master->cluster[i];
1632
1633 if (ctrl == NULL)
1634 continue;
1635
1636 if (ctrl->has_new) {
1637 /* Double check this: it may have changed since the
1638 last check in try_or_set_ext_ctrls(). */
1639 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1640 return -EBUSY;
1641
1642 /* Validate if required */
1643 if (!set)
1644 ret = validate_new(ctrl);
1645 continue;
1646 }
1647 /* No new value was set, so copy the current and force
1648 a call to try_ctrl later, since the values for the cluster
1649 may now have changed and the end result might be invalid. */
1650 try = true;
1651 cur_to_new(ctrl);
1652 }
1653
1654 /* For larger clusters you have to call try_ctrl again to
1655 verify that the controls are still valid after the
1656 'cur_to_new' above. */
1657 if (!ret && master->ops->try_ctrl && try)
1658 ret = master->ops->try_ctrl(master);
1659
1660 /* Don't set if there is no change */
1661 if (!ret && set && cluster_changed(master)) {
1662 ret = master->ops->s_ctrl(master);
1663 /* If OK, then make the new values permanent. */
1664 if (!ret)
1665 for (i = 0; i < master->ncontrols; i++)
1666 new_to_cur(master->cluster[i]);
1667 }
1668 return ret;
1669}
1670
1671/* Try or set controls. */
1672static int try_or_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1673 struct v4l2_ext_controls *cs,
1674 struct ctrl_helper *helpers,
1675 bool set)
1676{
1677 unsigned i, j;
1678 int ret = 0;
1679
1680 cs->error_idx = cs->count;
1681 for (i = 0; i < cs->count; i++) {
1682 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1683
1684 if (!set)
1685 cs->error_idx = i;
1686
1687 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1688 return -EACCES;
1689 /* This test is also done in try_set_control_cluster() which
1690 is called in atomic context, so that has the final say,
1691 but it makes sense to do an up-front check as well. Once
1692 an error occurs in try_set_control_cluster() some other
1693 controls may have been set already and we want to do a
1694 best-effort to avoid that. */
1695 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1696 return -EBUSY;
1697 }
1698
1699 for (i = 0; !ret && i < cs->count; i++) {
1700 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1701 struct v4l2_ctrl *master = ctrl->cluster[0];
1702
1703 cs->error_idx = i;
1704
1705 if (helpers[i].handled)
1706 continue;
1707
1708 v4l2_ctrl_lock(ctrl);
1709
1710 /* Reset the 'has_new' flags of the cluster */
1711 for (j = 0; j < master->ncontrols; j++)
1712 if (master->cluster[j])
1713 master->cluster[j]->has_new = 0;
1714
1715 /* Copy the new caller-supplied control values.
1716 user_to_new() sets 'has_new' to 1. */
1717 ret = cluster_walk(i, cs, helpers, user_to_new);
1718
1719 if (!ret)
1720 ret = try_or_set_control_cluster(master, set);
1721
1722 /* Copy the new values back to userspace. */
1723 if (!ret)
1724 ret = cluster_walk(i, cs, helpers, new_to_user);
1725
1726 v4l2_ctrl_unlock(ctrl);
1727 cluster_done(i, cs, helpers);
1728 }
1729 return ret;
1730}
1731
1732/* Try or try-and-set controls */
1733static int try_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1734 struct v4l2_ext_controls *cs,
1735 bool set)
1736{
1737 struct ctrl_helper helper[4];
1738 struct ctrl_helper *helpers = helper;
1739 int ret;
1740 int i;
1741
1742 cs->error_idx = cs->count;
1743 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1744
1745 if (hdl == NULL)
1746 return -EINVAL;
1747
1748 if (cs->count == 0)
1749 return class_check(hdl, cs->ctrl_class);
1750
1751 if (cs->count > ARRAY_SIZE(helper)) {
1752 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1753 if (!helpers)
1754 return -ENOMEM;
1755 }
1756 ret = prepare_ext_ctrls(hdl, cs, helpers, !set);
1757 if (ret)
1758 goto free;
1759
1760 /* First 'try' all controls and abort on error */
1761 ret = try_or_set_ext_ctrls(hdl, cs, helpers, false);
1762 /* If this is a 'set' operation and the initial 'try' failed,
1763 then set error_idx to count to tell the application that no
1764 controls changed value yet. */
1765 if (set)
1766 cs->error_idx = cs->count;
1767 if (!ret && set) {
1768 /* Reset 'handled' state */
1769 for (i = 0; i < cs->count; i++)
1770 helpers[i].handled = false;
1771 ret = try_or_set_ext_ctrls(hdl, cs, helpers, true);
1772 }
1773
1774free:
1775 if (cs->count > ARRAY_SIZE(helper))
1776 kfree(helpers);
1777 return ret;
1778}
1779
1780int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1781{
1782 return try_set_ext_ctrls(hdl, cs, false);
1783}
1784EXPORT_SYMBOL(v4l2_try_ext_ctrls);
1785
1786int v4l2_s_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1787{
1788 return try_set_ext_ctrls(hdl, cs, true);
1789}
1790EXPORT_SYMBOL(v4l2_s_ext_ctrls);
1791
1792int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1793{
1794 return try_set_ext_ctrls(sd->ctrl_handler, cs, false);
1795}
1796EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
1797
1798int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1799{
1800 return try_set_ext_ctrls(sd->ctrl_handler, cs, true);
1801}
1802EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
1803
1804/* Helper function for VIDIOC_S_CTRL compatibility */
1805static int set_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1806{
1807 struct v4l2_ctrl *master = ctrl->cluster[0];
1808 int ret;
1809 int i;
1810
1811 v4l2_ctrl_lock(ctrl);
1812
1813 /* Reset the 'has_new' flags of the cluster */
1814 for (i = 0; i < master->ncontrols; i++)
1815 if (master->cluster[i])
1816 master->cluster[i]->has_new = 0;
1817
1818 ctrl->val = *val;
1819 ctrl->has_new = 1;
1820 ret = try_or_set_control_cluster(master, false);
1821 if (!ret)
1822 ret = try_or_set_control_cluster(master, true);
1823 *val = ctrl->cur.val;
1824 v4l2_ctrl_unlock(ctrl);
1825 return ret;
1826}
1827
1828int v4l2_s_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1829{
1830 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1831
1832 if (ctrl == NULL || !type_is_int(ctrl))
1833 return -EINVAL;
1834
1835 return set_ctrl(ctrl, &control->value);
1836}
1837EXPORT_SYMBOL(v4l2_s_ctrl);
1838
1839int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1840{
1841 return v4l2_s_ctrl(sd->ctrl_handler, control);
1842}
1843EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
1844
1845int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
1846{
1847 /* It's a driver bug if this happens. */
1848 WARN_ON(!type_is_int(ctrl));
1849 return set_ctrl(ctrl, &val);
1850}
1851EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
diff --git a/drivers/media/video/v4l2-dev.c b/drivers/media/video/v4l2-dev.c
index 9e89bf61779..cb77197d480 100644
--- a/drivers/media/video/v4l2-dev.c
+++ b/drivers/media/video/v4l2-dev.c
@@ -25,6 +25,7 @@
25#include <linux/init.h> 25#include <linux/init.h>
26#include <linux/kmod.h> 26#include <linux/kmod.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/smp_lock.h>
28#include <asm/uaccess.h> 29#include <asm/uaccess.h>
29#include <asm/system.h> 30#include <asm/system.h>
30 31
@@ -215,28 +216,24 @@ static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
215 return vdev->fops->poll(filp, poll); 216 return vdev->fops->poll(filp, poll);
216} 217}
217 218
218static int v4l2_ioctl(struct inode *inode, struct file *filp, 219static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
219 unsigned int cmd, unsigned long arg)
220{ 220{
221 struct video_device *vdev = video_devdata(filp); 221 struct video_device *vdev = video_devdata(filp);
222 int ret;
222 223
223 if (!vdev->fops->ioctl)
224 return -ENOTTY;
225 /* Allow ioctl to continue even if the device was unregistered. 224 /* Allow ioctl to continue even if the device was unregistered.
226 Things like dequeueing buffers might still be useful. */ 225 Things like dequeueing buffers might still be useful. */
227 return vdev->fops->ioctl(filp, cmd, arg); 226 if (vdev->fops->unlocked_ioctl) {
228} 227 ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
229 228 } else if (vdev->fops->ioctl) {
230static long v4l2_unlocked_ioctl(struct file *filp, 229 /* TODO: convert all drivers to unlocked_ioctl */
231 unsigned int cmd, unsigned long arg) 230 lock_kernel();
232{ 231 ret = vdev->fops->ioctl(filp, cmd, arg);
233 struct video_device *vdev = video_devdata(filp); 232 unlock_kernel();
233 } else
234 ret = -ENOTTY;
234 235
235 if (!vdev->fops->unlocked_ioctl) 236 return ret;
236 return -ENOTTY;
237 /* Allow ioctl to continue even if the device was unregistered.
238 Things like dequeueing buffers might still be useful. */
239 return vdev->fops->unlocked_ioctl(filp, cmd, arg);
240} 237}
241 238
242#ifdef CONFIG_MMU 239#ifdef CONFIG_MMU
@@ -307,22 +304,6 @@ static int v4l2_release(struct inode *inode, struct file *filp)
307 return ret; 304 return ret;
308} 305}
309 306
310static const struct file_operations v4l2_unlocked_fops = {
311 .owner = THIS_MODULE,
312 .read = v4l2_read,
313 .write = v4l2_write,
314 .open = v4l2_open,
315 .get_unmapped_area = v4l2_get_unmapped_area,
316 .mmap = v4l2_mmap,
317 .unlocked_ioctl = v4l2_unlocked_ioctl,
318#ifdef CONFIG_COMPAT
319 .compat_ioctl = v4l2_compat_ioctl32,
320#endif
321 .release = v4l2_release,
322 .poll = v4l2_poll,
323 .llseek = no_llseek,
324};
325
326static const struct file_operations v4l2_fops = { 307static const struct file_operations v4l2_fops = {
327 .owner = THIS_MODULE, 308 .owner = THIS_MODULE,
328 .read = v4l2_read, 309 .read = v4l2_read,
@@ -330,7 +311,7 @@ static const struct file_operations v4l2_fops = {
330 .open = v4l2_open, 311 .open = v4l2_open,
331 .get_unmapped_area = v4l2_get_unmapped_area, 312 .get_unmapped_area = v4l2_get_unmapped_area,
332 .mmap = v4l2_mmap, 313 .mmap = v4l2_mmap,
333 .ioctl = v4l2_ioctl, 314 .unlocked_ioctl = v4l2_ioctl,
334#ifdef CONFIG_COMPAT 315#ifdef CONFIG_COMPAT
335 .compat_ioctl = v4l2_compat_ioctl32, 316 .compat_ioctl = v4l2_compat_ioctl32,
336#endif 317#endif
@@ -447,8 +428,12 @@ static int __video_register_device(struct video_device *vdev, int type, int nr,
447 428
448 vdev->vfl_type = type; 429 vdev->vfl_type = type;
449 vdev->cdev = NULL; 430 vdev->cdev = NULL;
450 if (vdev->v4l2_dev && vdev->v4l2_dev->dev) 431 if (vdev->v4l2_dev) {
451 vdev->parent = vdev->v4l2_dev->dev; 432 if (vdev->v4l2_dev->dev)
433 vdev->parent = vdev->v4l2_dev->dev;
434 if (vdev->ctrl_handler == NULL)
435 vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
436 }
452 437
453 /* Part 2: find a free minor, device node number and device index. */ 438 /* Part 2: find a free minor, device node number and device index. */
454#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES 439#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
@@ -521,10 +506,7 @@ static int __video_register_device(struct video_device *vdev, int type, int nr,
521 ret = -ENOMEM; 506 ret = -ENOMEM;
522 goto cleanup; 507 goto cleanup;
523 } 508 }
524 if (vdev->fops->unlocked_ioctl) 509 vdev->cdev->ops = &v4l2_fops;
525 vdev->cdev->ops = &v4l2_unlocked_fops;
526 else
527 vdev->cdev->ops = &v4l2_fops;
528 vdev->cdev->owner = vdev->fops->owner; 510 vdev->cdev->owner = vdev->fops->owner;
529 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1); 511 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
530 if (ret < 0) { 512 if (ret < 0) {
diff --git a/drivers/media/video/v4l2-device.c b/drivers/media/video/v4l2-device.c
index 5a7dc4afe92..0b08f96b74a 100644
--- a/drivers/media/video/v4l2-device.c
+++ b/drivers/media/video/v4l2-device.c
@@ -26,6 +26,7 @@
26#endif 26#endif
27#include <linux/videodev2.h> 27#include <linux/videodev2.h>
28#include <media/v4l2-device.h> 28#include <media/v4l2-device.h>
29#include <media/v4l2-ctrls.h>
29 30
30int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev) 31int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
31{ 32{
@@ -115,6 +116,8 @@ EXPORT_SYMBOL_GPL(v4l2_device_unregister);
115int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev, 116int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
116 struct v4l2_subdev *sd) 117 struct v4l2_subdev *sd)
117{ 118{
119 int err;
120
118 /* Check for valid input */ 121 /* Check for valid input */
119 if (v4l2_dev == NULL || sd == NULL || !sd->name[0]) 122 if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
120 return -EINVAL; 123 return -EINVAL;
@@ -122,6 +125,10 @@ int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
122 WARN_ON(sd->v4l2_dev != NULL); 125 WARN_ON(sd->v4l2_dev != NULL);
123 if (!try_module_get(sd->owner)) 126 if (!try_module_get(sd->owner))
124 return -ENODEV; 127 return -ENODEV;
128 /* This just returns 0 if either of the two args is NULL */
129 err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
130 if (err)
131 return err;
125 sd->v4l2_dev = v4l2_dev; 132 sd->v4l2_dev = v4l2_dev;
126 spin_lock(&v4l2_dev->lock); 133 spin_lock(&v4l2_dev->lock);
127 list_add_tail(&sd->list, &v4l2_dev->subdevs); 134 list_add_tail(&sd->list, &v4l2_dev->subdevs);
diff --git a/drivers/media/video/v4l2-ioctl.c b/drivers/media/video/v4l2-ioctl.c
index 0eeceae5032..dd9283fcb56 100644
--- a/drivers/media/video/v4l2-ioctl.c
+++ b/drivers/media/video/v4l2-ioctl.c
@@ -26,6 +26,7 @@
26#endif 26#endif
27#include <media/v4l2-common.h> 27#include <media/v4l2-common.h>
28#include <media/v4l2-ioctl.h> 28#include <media/v4l2-ioctl.h>
29#include <media/v4l2-ctrls.h>
29#include <media/v4l2-fh.h> 30#include <media/v4l2-fh.h>
30#include <media/v4l2-event.h> 31#include <media/v4l2-event.h>
31#include <media/v4l2-chip-ident.h> 32#include <media/v4l2-chip-ident.h>
@@ -1259,9 +1260,12 @@ static long __video_do_ioctl(struct file *file,
1259 { 1260 {
1260 struct v4l2_queryctrl *p = arg; 1261 struct v4l2_queryctrl *p = arg;
1261 1262
1262 if (!ops->vidioc_queryctrl) 1263 if (vfd->ctrl_handler)
1264 ret = v4l2_queryctrl(vfd->ctrl_handler, p);
1265 else if (ops->vidioc_queryctrl)
1266 ret = ops->vidioc_queryctrl(file, fh, p);
1267 else
1263 break; 1268 break;
1264 ret = ops->vidioc_queryctrl(file, fh, p);
1265 if (!ret) 1269 if (!ret)
1266 dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, " 1270 dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
1267 "step=%d, default=%d, flags=0x%08x\n", 1271 "step=%d, default=%d, flags=0x%08x\n",
@@ -1276,7 +1280,9 @@ static long __video_do_ioctl(struct file *file,
1276 { 1280 {
1277 struct v4l2_control *p = arg; 1281 struct v4l2_control *p = arg;
1278 1282
1279 if (ops->vidioc_g_ctrl) 1283 if (vfd->ctrl_handler)
1284 ret = v4l2_g_ctrl(vfd->ctrl_handler, p);
1285 else if (ops->vidioc_g_ctrl)
1280 ret = ops->vidioc_g_ctrl(file, fh, p); 1286 ret = ops->vidioc_g_ctrl(file, fh, p);
1281 else if (ops->vidioc_g_ext_ctrls) { 1287 else if (ops->vidioc_g_ext_ctrls) {
1282 struct v4l2_ext_controls ctrls; 1288 struct v4l2_ext_controls ctrls;
@@ -1306,11 +1312,16 @@ static long __video_do_ioctl(struct file *file,
1306 struct v4l2_ext_controls ctrls; 1312 struct v4l2_ext_controls ctrls;
1307 struct v4l2_ext_control ctrl; 1313 struct v4l2_ext_control ctrl;
1308 1314
1309 if (!ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls) 1315 if (!vfd->ctrl_handler &&
1316 !ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
1310 break; 1317 break;
1311 1318
1312 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value); 1319 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1313 1320
1321 if (vfd->ctrl_handler) {
1322 ret = v4l2_s_ctrl(vfd->ctrl_handler, p);
1323 break;
1324 }
1314 if (ops->vidioc_s_ctrl) { 1325 if (ops->vidioc_s_ctrl) {
1315 ret = ops->vidioc_s_ctrl(file, fh, p); 1326 ret = ops->vidioc_s_ctrl(file, fh, p);
1316 break; 1327 break;
@@ -1332,10 +1343,12 @@ static long __video_do_ioctl(struct file *file,
1332 struct v4l2_ext_controls *p = arg; 1343 struct v4l2_ext_controls *p = arg;
1333 1344
1334 p->error_idx = p->count; 1345 p->error_idx = p->count;
1335 if (!ops->vidioc_g_ext_ctrls) 1346 if (vfd->ctrl_handler)
1336 break; 1347 ret = v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1337 if (check_ext_ctrls(p, 0)) 1348 else if (ops->vidioc_g_ext_ctrls && check_ext_ctrls(p, 0))
1338 ret = ops->vidioc_g_ext_ctrls(file, fh, p); 1349 ret = ops->vidioc_g_ext_ctrls(file, fh, p);
1350 else
1351 break;
1339 v4l_print_ext_ctrls(cmd, vfd, p, !ret); 1352 v4l_print_ext_ctrls(cmd, vfd, p, !ret);
1340 break; 1353 break;
1341 } 1354 }
@@ -1344,10 +1357,12 @@ static long __video_do_ioctl(struct file *file,
1344 struct v4l2_ext_controls *p = arg; 1357 struct v4l2_ext_controls *p = arg;
1345 1358
1346 p->error_idx = p->count; 1359 p->error_idx = p->count;
1347 if (!ops->vidioc_s_ext_ctrls) 1360 if (!vfd->ctrl_handler && !ops->vidioc_s_ext_ctrls)
1348 break; 1361 break;
1349 v4l_print_ext_ctrls(cmd, vfd, p, 1); 1362 v4l_print_ext_ctrls(cmd, vfd, p, 1);
1350 if (check_ext_ctrls(p, 0)) 1363 if (vfd->ctrl_handler)
1364 ret = v4l2_s_ext_ctrls(vfd->ctrl_handler, p);
1365 else if (check_ext_ctrls(p, 0))
1351 ret = ops->vidioc_s_ext_ctrls(file, fh, p); 1366 ret = ops->vidioc_s_ext_ctrls(file, fh, p);
1352 break; 1367 break;
1353 } 1368 }
@@ -1356,10 +1371,12 @@ static long __video_do_ioctl(struct file *file,
1356 struct v4l2_ext_controls *p = arg; 1371 struct v4l2_ext_controls *p = arg;
1357 1372
1358 p->error_idx = p->count; 1373 p->error_idx = p->count;
1359 if (!ops->vidioc_try_ext_ctrls) 1374 if (!vfd->ctrl_handler && !ops->vidioc_try_ext_ctrls)
1360 break; 1375 break;
1361 v4l_print_ext_ctrls(cmd, vfd, p, 1); 1376 v4l_print_ext_ctrls(cmd, vfd, p, 1);
1362 if (check_ext_ctrls(p, 0)) 1377 if (vfd->ctrl_handler)
1378 ret = v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1379 else if (check_ext_ctrls(p, 0))
1363 ret = ops->vidioc_try_ext_ctrls(file, fh, p); 1380 ret = ops->vidioc_try_ext_ctrls(file, fh, p);
1364 break; 1381 break;
1365 } 1382 }
@@ -1367,9 +1384,12 @@ static long __video_do_ioctl(struct file *file,
1367 { 1384 {
1368 struct v4l2_querymenu *p = arg; 1385 struct v4l2_querymenu *p = arg;
1369 1386
1370 if (!ops->vidioc_querymenu) 1387 if (vfd->ctrl_handler)
1388 ret = v4l2_querymenu(vfd->ctrl_handler, p);
1389 else if (ops->vidioc_querymenu)
1390 ret = ops->vidioc_querymenu(file, fh, p);
1391 else
1371 break; 1392 break;
1372 ret = ops->vidioc_querymenu(file, fh, p);
1373 if (!ret) 1393 if (!ret)
1374 dbgarg(cmd, "id=0x%x, index=%d, name=%s\n", 1394 dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
1375 p->id, p->index, p->name); 1395 p->id, p->index, p->name);
diff --git a/drivers/media/video/wm8739.c b/drivers/media/video/wm8739.c
index a11b99b4226..d5965543eca 100644
--- a/drivers/media/video/wm8739.c
+++ b/drivers/media/video/wm8739.c
@@ -27,11 +27,11 @@
27#include <linux/ioctl.h> 27#include <linux/ioctl.h>
28#include <asm/uaccess.h> 28#include <asm/uaccess.h>
29#include <linux/i2c.h> 29#include <linux/i2c.h>
30#include <linux/i2c-id.h>
31#include <linux/videodev2.h> 30#include <linux/videodev2.h>
32#include <media/v4l2-device.h> 31#include <media/v4l2-device.h>
33#include <media/v4l2-chip-ident.h> 32#include <media/v4l2-chip-ident.h>
34#include <media/v4l2-i2c-drv.h> 33#include <media/v4l2-i2c-drv.h>
34#include <media/v4l2-ctrls.h>
35 35
36MODULE_DESCRIPTION("wm8739 driver"); 36MODULE_DESCRIPTION("wm8739 driver");
37MODULE_AUTHOR("T. Adachi, Hans Verkuil"); 37MODULE_AUTHOR("T. Adachi, Hans Verkuil");
@@ -54,12 +54,14 @@ enum {
54 54
55struct wm8739_state { 55struct wm8739_state {
56 struct v4l2_subdev sd; 56 struct v4l2_subdev sd;
57 struct v4l2_ctrl_handler hdl;
58 struct {
59 /* audio cluster */
60 struct v4l2_ctrl *volume;
61 struct v4l2_ctrl *mute;
62 struct v4l2_ctrl *balance;
63 };
57 u32 clock_freq; 64 u32 clock_freq;
58 u8 muted;
59 u16 volume;
60 u16 balance;
61 u8 vol_l; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
62 u8 vol_r; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
63}; 65};
64 66
65static inline struct wm8739_state *to_state(struct v4l2_subdev *sd) 67static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
@@ -67,6 +69,11 @@ static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
67 return container_of(sd, struct wm8739_state, sd); 69 return container_of(sd, struct wm8739_state, sd);
68} 70}
69 71
72static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
73{
74 return &container_of(ctrl->handler, struct wm8739_state, hdl)->sd;
75}
76
70/* ------------------------------------------------------------------------ */ 77/* ------------------------------------------------------------------------ */
71 78
72static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val) 79static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
@@ -89,58 +96,17 @@ static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
89 return -1; 96 return -1;
90} 97}
91 98
92/* write regs to set audio volume etc */ 99static int wm8739_s_ctrl(struct v4l2_ctrl *ctrl)
93static void wm8739_set_audio(struct v4l2_subdev *sd)
94{
95 struct wm8739_state *state = to_state(sd);
96 u16 mute = state->muted ? 0x80 : 0;
97
98 /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
99 * Default setting: 0x17 = 0 dB
100 */
101 wm8739_write(sd, R0, (state->vol_l & 0x1f) | mute);
102 wm8739_write(sd, R1, (state->vol_r & 0x1f) | mute);
103}
104
105static int wm8739_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
106{
107 struct wm8739_state *state = to_state(sd);
108
109 switch (ctrl->id) {
110 case V4L2_CID_AUDIO_MUTE:
111 ctrl->value = state->muted;
112 break;
113
114 case V4L2_CID_AUDIO_VOLUME:
115 ctrl->value = state->volume;
116 break;
117
118 case V4L2_CID_AUDIO_BALANCE:
119 ctrl->value = state->balance;
120 break;
121
122 default:
123 return -EINVAL;
124 }
125 return 0;
126}
127
128static int wm8739_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
129{ 100{
101 struct v4l2_subdev *sd = to_sd(ctrl);
130 struct wm8739_state *state = to_state(sd); 102 struct wm8739_state *state = to_state(sd);
131 unsigned int work_l, work_r; 103 unsigned int work_l, work_r;
104 u8 vol_l; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
105 u8 vol_r; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
106 u16 mute;
132 107
133 switch (ctrl->id) { 108 switch (ctrl->id) {
134 case V4L2_CID_AUDIO_MUTE:
135 state->muted = ctrl->value;
136 break;
137
138 case V4L2_CID_AUDIO_VOLUME: 109 case V4L2_CID_AUDIO_VOLUME:
139 state->volume = ctrl->value;
140 break;
141
142 case V4L2_CID_AUDIO_BALANCE:
143 state->balance = ctrl->value;
144 break; 110 break;
145 111
146 default: 112 default:
@@ -148,52 +114,25 @@ static int wm8739_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
148 } 114 }
149 115
150 /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */ 116 /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */
151 work_l = (min(65536 - state->balance, 32768) * state->volume) / 32768; 117 work_l = (min(65536 - state->balance->val, 32768) * state->volume->val) / 32768;
152 work_r = (min(state->balance, (u16)32768) * state->volume) / 32768; 118 work_r = (min(state->balance->val, 32768) * state->volume->val) / 32768;
153 119
154 state->vol_l = (long)work_l * 31 / 65535; 120 vol_l = (long)work_l * 31 / 65535;
155 state->vol_r = (long)work_r * 31 / 65535; 121 vol_r = (long)work_r * 31 / 65535;
156 122
157 /* set audio volume etc. */ 123 /* set audio volume etc. */
158 wm8739_set_audio(sd); 124 mute = state->mute->val ? 0x80 : 0;
125
126 /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
127 * Default setting: 0x17 = 0 dB
128 */
129 wm8739_write(sd, R0, (vol_l & 0x1f) | mute);
130 wm8739_write(sd, R1, (vol_r & 0x1f) | mute);
159 return 0; 131 return 0;
160} 132}
161 133
162/* ------------------------------------------------------------------------ */ 134/* ------------------------------------------------------------------------ */
163 135
164static struct v4l2_queryctrl wm8739_qctrl[] = {
165 {
166 .id = V4L2_CID_AUDIO_VOLUME,
167 .name = "Volume",
168 .minimum = 0,
169 .maximum = 65535,
170 .step = 65535/100,
171 .default_value = 58880,
172 .flags = 0,
173 .type = V4L2_CTRL_TYPE_INTEGER,
174 }, {
175 .id = V4L2_CID_AUDIO_MUTE,
176 .name = "Mute",
177 .minimum = 0,
178 .maximum = 1,
179 .step = 1,
180 .default_value = 1,
181 .flags = 0,
182 .type = V4L2_CTRL_TYPE_BOOLEAN,
183 }, {
184 .id = V4L2_CID_AUDIO_BALANCE,
185 .name = "Balance",
186 .minimum = 0,
187 .maximum = 65535,
188 .step = 65535/100,
189 .default_value = 32768,
190 .flags = 0,
191 .type = V4L2_CTRL_TYPE_INTEGER,
192 }
193};
194
195/* ------------------------------------------------------------------------ */
196
197static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq) 136static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
198{ 137{
199 struct wm8739_state *state = to_state(sd); 138 struct wm8739_state *state = to_state(sd);
@@ -222,18 +161,6 @@ static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
222 return 0; 161 return 0;
223} 162}
224 163
225static int wm8739_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
226{
227 int i;
228
229 for (i = 0; i < ARRAY_SIZE(wm8739_qctrl); i++)
230 if (qc->id && qc->id == wm8739_qctrl[i].id) {
231 memcpy(qc, &wm8739_qctrl[i], sizeof(*qc));
232 return 0;
233 }
234 return -EINVAL;
235}
236
237static int wm8739_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip) 164static int wm8739_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
238{ 165{
239 struct i2c_client *client = v4l2_get_subdevdata(sd); 166 struct i2c_client *client = v4l2_get_subdevdata(sd);
@@ -246,21 +173,26 @@ static int wm8739_log_status(struct v4l2_subdev *sd)
246 struct wm8739_state *state = to_state(sd); 173 struct wm8739_state *state = to_state(sd);
247 174
248 v4l2_info(sd, "Frequency: %u Hz\n", state->clock_freq); 175 v4l2_info(sd, "Frequency: %u Hz\n", state->clock_freq);
249 v4l2_info(sd, "Volume L: %02x%s\n", state->vol_l & 0x1f, 176 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
250 state->muted ? " (muted)" : "");
251 v4l2_info(sd, "Volume R: %02x%s\n", state->vol_r & 0x1f,
252 state->muted ? " (muted)" : "");
253 return 0; 177 return 0;
254} 178}
255 179
256/* ----------------------------------------------------------------------- */ 180/* ----------------------------------------------------------------------- */
257 181
182static const struct v4l2_ctrl_ops wm8739_ctrl_ops = {
183 .s_ctrl = wm8739_s_ctrl,
184};
185
258static const struct v4l2_subdev_core_ops wm8739_core_ops = { 186static const struct v4l2_subdev_core_ops wm8739_core_ops = {
259 .log_status = wm8739_log_status, 187 .log_status = wm8739_log_status,
260 .g_chip_ident = wm8739_g_chip_ident, 188 .g_chip_ident = wm8739_g_chip_ident,
261 .queryctrl = wm8739_queryctrl, 189 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
262 .g_ctrl = wm8739_g_ctrl, 190 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
263 .s_ctrl = wm8739_s_ctrl, 191 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
192 .g_ctrl = v4l2_subdev_g_ctrl,
193 .s_ctrl = v4l2_subdev_s_ctrl,
194 .queryctrl = v4l2_subdev_queryctrl,
195 .querymenu = v4l2_subdev_querymenu,
264}; 196};
265 197
266static const struct v4l2_subdev_audio_ops wm8739_audio_ops = { 198static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
@@ -289,17 +221,28 @@ static int wm8739_probe(struct i2c_client *client,
289 v4l_info(client, "chip found @ 0x%x (%s)\n", 221 v4l_info(client, "chip found @ 0x%x (%s)\n",
290 client->addr << 1, client->adapter->name); 222 client->addr << 1, client->adapter->name);
291 223
292 state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL); 224 state = kzalloc(sizeof(struct wm8739_state), GFP_KERNEL);
293 if (state == NULL) 225 if (state == NULL)
294 return -ENOMEM; 226 return -ENOMEM;
295 sd = &state->sd; 227 sd = &state->sd;
296 v4l2_i2c_subdev_init(sd, client, &wm8739_ops); 228 v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
297 state->vol_l = 0x17; /* 0dB */ 229 v4l2_ctrl_handler_init(&state->hdl, 2);
298 state->vol_r = 0x17; /* 0dB */ 230 state->volume = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
299 state->muted = 0; 231 V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 50736);
300 state->balance = 32768; 232 state->mute = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
301 /* normalize (12dB(31) to -34.5dB(0) [0dB(23)] -> 65535 to 0) */ 233 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
302 state->volume = ((long)state->vol_l + 1) * 65535 / 31; 234 state->balance = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
235 V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
236 sd->ctrl_handler = &state->hdl;
237 if (state->hdl.error) {
238 int err = state->hdl.error;
239
240 v4l2_ctrl_handler_free(&state->hdl);
241 kfree(state);
242 return err;
243 }
244 v4l2_ctrl_cluster(3, &state->volume);
245
303 state->clock_freq = 48000; 246 state->clock_freq = 48000;
304 247
305 /* Initialize wm8739 */ 248 /* Initialize wm8739 */
@@ -318,15 +261,17 @@ static int wm8739_probe(struct i2c_client *client,
318 /* activate */ 261 /* activate */
319 wm8739_write(sd, R9, 0x001); 262 wm8739_write(sd, R9, 0x001);
320 /* set volume/mute */ 263 /* set volume/mute */
321 wm8739_set_audio(sd); 264 v4l2_ctrl_handler_setup(&state->hdl);
322 return 0; 265 return 0;
323} 266}
324 267
325static int wm8739_remove(struct i2c_client *client) 268static int wm8739_remove(struct i2c_client *client)
326{ 269{
327 struct v4l2_subdev *sd = i2c_get_clientdata(client); 270 struct v4l2_subdev *sd = i2c_get_clientdata(client);
271 struct wm8739_state *state = to_state(sd);
328 272
329 v4l2_device_unregister_subdev(sd); 273 v4l2_device_unregister_subdev(sd);
274 v4l2_ctrl_handler_free(&state->hdl);
330 kfree(to_state(sd)); 275 kfree(to_state(sd));
331 return 0; 276 return 0;
332} 277}
diff --git a/drivers/media/video/wm8775.c b/drivers/media/video/wm8775.c
index 5c2ba599c0c..23bad3fd6dc 100644
--- a/drivers/media/video/wm8775.c
+++ b/drivers/media/video/wm8775.c
@@ -35,6 +35,7 @@
35#include <linux/videodev2.h> 35#include <linux/videodev2.h>
36#include <media/v4l2-device.h> 36#include <media/v4l2-device.h>
37#include <media/v4l2-chip-ident.h> 37#include <media/v4l2-chip-ident.h>
38#include <media/v4l2-ctrls.h>
38#include <media/v4l2-i2c-drv.h> 39#include <media/v4l2-i2c-drv.h>
39 40
40MODULE_DESCRIPTION("wm8775 driver"); 41MODULE_DESCRIPTION("wm8775 driver");
@@ -53,8 +54,9 @@ enum {
53 54
54struct wm8775_state { 55struct wm8775_state {
55 struct v4l2_subdev sd; 56 struct v4l2_subdev sd;
57 struct v4l2_ctrl_handler hdl;
58 struct v4l2_ctrl *mute;
56 u8 input; /* Last selected input (0-0xf) */ 59 u8 input; /* Last selected input (0-0xf) */
57 u8 muted;
58}; 60};
59 61
60static inline struct wm8775_state *to_state(struct v4l2_subdev *sd) 62static inline struct wm8775_state *to_state(struct v4l2_subdev *sd)
@@ -62,6 +64,11 @@ static inline struct wm8775_state *to_state(struct v4l2_subdev *sd)
62 return container_of(sd, struct wm8775_state, sd); 64 return container_of(sd, struct wm8775_state, sd);
63} 65}
64 66
67static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
68{
69 return &container_of(ctrl->handler, struct wm8775_state, hdl)->sd;
70}
71
65static int wm8775_write(struct v4l2_subdev *sd, int reg, u16 val) 72static int wm8775_write(struct v4l2_subdev *sd, int reg, u16 val)
66{ 73{
67 struct i2c_client *client = v4l2_get_subdevdata(sd); 74 struct i2c_client *client = v4l2_get_subdevdata(sd);
@@ -95,7 +102,7 @@ static int wm8775_s_routing(struct v4l2_subdev *sd,
95 return -EINVAL; 102 return -EINVAL;
96 } 103 }
97 state->input = input; 104 state->input = input;
98 if (state->muted) 105 if (!v4l2_ctrl_g_ctrl(state->mute))
99 return 0; 106 return 0;
100 wm8775_write(sd, R21, 0x0c0); 107 wm8775_write(sd, R21, 0x0c0);
101 wm8775_write(sd, R14, 0x1d4); 108 wm8775_write(sd, R14, 0x1d4);
@@ -104,29 +111,21 @@ static int wm8775_s_routing(struct v4l2_subdev *sd,
104 return 0; 111 return 0;
105} 112}
106 113
107static int wm8775_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 114static int wm8775_s_ctrl(struct v4l2_ctrl *ctrl)
108{ 115{
116 struct v4l2_subdev *sd = to_sd(ctrl);
109 struct wm8775_state *state = to_state(sd); 117 struct wm8775_state *state = to_state(sd);
110 118
111 if (ctrl->id != V4L2_CID_AUDIO_MUTE) 119 switch (ctrl->id) {
112 return -EINVAL; 120 case V4L2_CID_AUDIO_MUTE:
113 ctrl->value = state->muted; 121 wm8775_write(sd, R21, 0x0c0);
114 return 0; 122 wm8775_write(sd, R14, 0x1d4);
115} 123 wm8775_write(sd, R15, 0x1d4);
116 124 if (!ctrl->val)
117static int wm8775_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) 125 wm8775_write(sd, R21, 0x100 + state->input);
118{ 126 return 0;
119 struct wm8775_state *state = to_state(sd); 127 }
120 128 return -EINVAL;
121 if (ctrl->id != V4L2_CID_AUDIO_MUTE)
122 return -EINVAL;
123 state->muted = ctrl->value;
124 wm8775_write(sd, R21, 0x0c0);
125 wm8775_write(sd, R14, 0x1d4);
126 wm8775_write(sd, R15, 0x1d4);
127 if (!state->muted)
128 wm8775_write(sd, R21, 0x100 + state->input);
129 return 0;
130} 129}
131 130
132static int wm8775_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip) 131static int wm8775_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
@@ -140,8 +139,8 @@ static int wm8775_log_status(struct v4l2_subdev *sd)
140{ 139{
141 struct wm8775_state *state = to_state(sd); 140 struct wm8775_state *state = to_state(sd);
142 141
143 v4l2_info(sd, "Input: %d%s\n", state->input, 142 v4l2_info(sd, "Input: %d\n", state->input);
144 state->muted ? " (muted)" : ""); 143 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
145 return 0; 144 return 0;
146} 145}
147 146
@@ -162,11 +161,20 @@ static int wm8775_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *fre
162 161
163/* ----------------------------------------------------------------------- */ 162/* ----------------------------------------------------------------------- */
164 163
164static const struct v4l2_ctrl_ops wm8775_ctrl_ops = {
165 .s_ctrl = wm8775_s_ctrl,
166};
167
165static const struct v4l2_subdev_core_ops wm8775_core_ops = { 168static const struct v4l2_subdev_core_ops wm8775_core_ops = {
166 .log_status = wm8775_log_status, 169 .log_status = wm8775_log_status,
167 .g_chip_ident = wm8775_g_chip_ident, 170 .g_chip_ident = wm8775_g_chip_ident,
168 .g_ctrl = wm8775_g_ctrl, 171 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
169 .s_ctrl = wm8775_s_ctrl, 172 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
173 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
174 .g_ctrl = v4l2_subdev_g_ctrl,
175 .s_ctrl = v4l2_subdev_s_ctrl,
176 .queryctrl = v4l2_subdev_queryctrl,
177 .querymenu = v4l2_subdev_querymenu,
170}; 178};
171 179
172static const struct v4l2_subdev_tuner_ops wm8775_tuner_ops = { 180static const struct v4l2_subdev_tuner_ops wm8775_tuner_ops = {
@@ -205,13 +213,24 @@ static int wm8775_probe(struct i2c_client *client,
205 v4l_info(client, "chip found @ 0x%02x (%s)\n", 213 v4l_info(client, "chip found @ 0x%02x (%s)\n",
206 client->addr << 1, client->adapter->name); 214 client->addr << 1, client->adapter->name);
207 215
208 state = kmalloc(sizeof(struct wm8775_state), GFP_KERNEL); 216 state = kzalloc(sizeof(struct wm8775_state), GFP_KERNEL);
209 if (state == NULL) 217 if (state == NULL)
210 return -ENOMEM; 218 return -ENOMEM;
211 sd = &state->sd; 219 sd = &state->sd;
212 v4l2_i2c_subdev_init(sd, client, &wm8775_ops); 220 v4l2_i2c_subdev_init(sd, client, &wm8775_ops);
213 state->input = 2; 221 state->input = 2;
214 state->muted = 0; 222
223 v4l2_ctrl_handler_init(&state->hdl, 1);
224 state->mute = v4l2_ctrl_new_std(&state->hdl, &wm8775_ctrl_ops,
225 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
226 sd->ctrl_handler = &state->hdl;
227 if (state->hdl.error) {
228 int err = state->hdl.error;
229
230 v4l2_ctrl_handler_free(&state->hdl);
231 kfree(state);
232 return err;
233 }
215 234
216 /* Initialize wm8775 */ 235 /* Initialize wm8775 */
217 236
@@ -248,9 +267,11 @@ static int wm8775_probe(struct i2c_client *client,
248static int wm8775_remove(struct i2c_client *client) 267static int wm8775_remove(struct i2c_client *client)
249{ 268{
250 struct v4l2_subdev *sd = i2c_get_clientdata(client); 269 struct v4l2_subdev *sd = i2c_get_clientdata(client);
270 struct wm8775_state *state = to_state(sd);
251 271
252 v4l2_device_unregister_subdev(sd); 272 v4l2_device_unregister_subdev(sd);
253 kfree(to_state(sd)); 273 v4l2_ctrl_handler_free(&state->hdl);
274 kfree(state);
254 return 0; 275 return 0;
255} 276}
256 277
diff --git a/drivers/memstick/core/mspro_block.c b/drivers/memstick/core/mspro_block.c
index 8327e248520..eef78a068fd 100644
--- a/drivers/memstick/core/mspro_block.c
+++ b/drivers/memstick/core/mspro_block.c
@@ -18,6 +18,7 @@
18#include <linux/kthread.h> 18#include <linux/kthread.h>
19#include <linux/delay.h> 19#include <linux/delay.h>
20#include <linux/slab.h> 20#include <linux/slab.h>
21#include <linux/smp_lock.h>
21#include <linux/memstick.h> 22#include <linux/memstick.h>
22 23
23#define DRIVER_NAME "mspro_block" 24#define DRIVER_NAME "mspro_block"
@@ -179,6 +180,7 @@ static int mspro_block_bd_open(struct block_device *bdev, fmode_t mode)
179 struct mspro_block_data *msb = disk->private_data; 180 struct mspro_block_data *msb = disk->private_data;
180 int rc = -ENXIO; 181 int rc = -ENXIO;
181 182
183 lock_kernel();
182 mutex_lock(&mspro_block_disk_lock); 184 mutex_lock(&mspro_block_disk_lock);
183 185
184 if (msb && msb->card) { 186 if (msb && msb->card) {
@@ -190,6 +192,7 @@ static int mspro_block_bd_open(struct block_device *bdev, fmode_t mode)
190 } 192 }
191 193
192 mutex_unlock(&mspro_block_disk_lock); 194 mutex_unlock(&mspro_block_disk_lock);
195 unlock_kernel();
193 196
194 return rc; 197 return rc;
195} 198}
@@ -221,7 +224,11 @@ static int mspro_block_disk_release(struct gendisk *disk)
221 224
222static int mspro_block_bd_release(struct gendisk *disk, fmode_t mode) 225static int mspro_block_bd_release(struct gendisk *disk, fmode_t mode)
223{ 226{
224 return mspro_block_disk_release(disk); 227 int ret;
228 lock_kernel();
229 ret = mspro_block_disk_release(disk);
230 unlock_kernel();
231 return ret;
225} 232}
226 233
227static int mspro_block_bd_getgeo(struct block_device *bdev, 234static int mspro_block_bd_getgeo(struct block_device *bdev,
@@ -805,7 +812,8 @@ static void mspro_block_start(struct memstick_dev *card)
805 812
806static int mspro_block_prepare_req(struct request_queue *q, struct request *req) 813static int mspro_block_prepare_req(struct request_queue *q, struct request *req)
807{ 814{
808 if (!blk_fs_request(req) && !blk_pc_request(req)) { 815 if (req->cmd_type != REQ_TYPE_FS &&
816 req->cmd_type != REQ_TYPE_BLOCK_PC) {
809 blk_dump_rq_flags(req, "MSPro unsupported request"); 817 blk_dump_rq_flags(req, "MSPro unsupported request");
810 return BLKPREP_KILL; 818 return BLKPREP_KILL;
811 } 819 }
diff --git a/drivers/message/i2o/i2o_block.c b/drivers/message/i2o/i2o_block.c
index fc593fbab69..e6733bc9972 100644
--- a/drivers/message/i2o/i2o_block.c
+++ b/drivers/message/i2o/i2o_block.c
@@ -53,6 +53,7 @@
53#include <linux/module.h> 53#include <linux/module.h>
54#include <linux/slab.h> 54#include <linux/slab.h>
55#include <linux/i2o.h> 55#include <linux/i2o.h>
56#include <linux/smp_lock.h>
56 57
57#include <linux/mempool.h> 58#include <linux/mempool.h>
58 59
@@ -577,6 +578,7 @@ static int i2o_block_open(struct block_device *bdev, fmode_t mode)
577 if (!dev->i2o_dev) 578 if (!dev->i2o_dev)
578 return -ENODEV; 579 return -ENODEV;
579 580
581 lock_kernel();
580 if (dev->power > 0x1f) 582 if (dev->power > 0x1f)
581 i2o_block_device_power(dev, 0x02); 583 i2o_block_device_power(dev, 0x02);
582 584
@@ -585,6 +587,7 @@ static int i2o_block_open(struct block_device *bdev, fmode_t mode)
585 i2o_block_device_lock(dev->i2o_dev, -1); 587 i2o_block_device_lock(dev->i2o_dev, -1);
586 588
587 osm_debug("Ready.\n"); 589 osm_debug("Ready.\n");
590 unlock_kernel();
588 591
589 return 0; 592 return 0;
590}; 593};
@@ -615,6 +618,7 @@ static int i2o_block_release(struct gendisk *disk, fmode_t mode)
615 if (!dev->i2o_dev) 618 if (!dev->i2o_dev)
616 return 0; 619 return 0;
617 620
621 lock_kernel();
618 i2o_block_device_flush(dev->i2o_dev); 622 i2o_block_device_flush(dev->i2o_dev);
619 623
620 i2o_block_device_unlock(dev->i2o_dev, -1); 624 i2o_block_device_unlock(dev->i2o_dev, -1);
@@ -625,6 +629,7 @@ static int i2o_block_release(struct gendisk *disk, fmode_t mode)
625 operation = 0x24; 629 operation = 0x24;
626 630
627 i2o_block_device_power(dev, operation); 631 i2o_block_device_power(dev, operation);
632 unlock_kernel();
628 633
629 return 0; 634 return 0;
630} 635}
@@ -652,30 +657,40 @@ static int i2o_block_ioctl(struct block_device *bdev, fmode_t mode,
652{ 657{
653 struct gendisk *disk = bdev->bd_disk; 658 struct gendisk *disk = bdev->bd_disk;
654 struct i2o_block_device *dev = disk->private_data; 659 struct i2o_block_device *dev = disk->private_data;
660 int ret = -ENOTTY;
655 661
656 /* Anyone capable of this syscall can do *real bad* things */ 662 /* Anyone capable of this syscall can do *real bad* things */
657 663
658 if (!capable(CAP_SYS_ADMIN)) 664 if (!capable(CAP_SYS_ADMIN))
659 return -EPERM; 665 return -EPERM;
660 666
667 lock_kernel();
661 switch (cmd) { 668 switch (cmd) {
662 case BLKI2OGRSTRAT: 669 case BLKI2OGRSTRAT:
663 return put_user(dev->rcache, (int __user *)arg); 670 ret = put_user(dev->rcache, (int __user *)arg);
671 break;
664 case BLKI2OGWSTRAT: 672 case BLKI2OGWSTRAT:
665 return put_user(dev->wcache, (int __user *)arg); 673 ret = put_user(dev->wcache, (int __user *)arg);
674 break;
666 case BLKI2OSRSTRAT: 675 case BLKI2OSRSTRAT:
676 ret = -EINVAL;
667 if (arg < 0 || arg > CACHE_SMARTFETCH) 677 if (arg < 0 || arg > CACHE_SMARTFETCH)
668 return -EINVAL; 678 break;
669 dev->rcache = arg; 679 dev->rcache = arg;
680 ret = 0;
670 break; 681 break;
671 case BLKI2OSWSTRAT: 682 case BLKI2OSWSTRAT:
683 ret = -EINVAL;
672 if (arg != 0 684 if (arg != 0
673 && (arg < CACHE_WRITETHROUGH || arg > CACHE_SMARTBACK)) 685 && (arg < CACHE_WRITETHROUGH || arg > CACHE_SMARTBACK))
674 return -EINVAL; 686 break;
675 dev->wcache = arg; 687 dev->wcache = arg;
688 ret = 0;
676 break; 689 break;
677 } 690 }
678 return -ENOTTY; 691 unlock_kernel();
692
693 return ret;
679}; 694};
680 695
681/** 696/**
@@ -883,7 +898,7 @@ static void i2o_block_request_fn(struct request_queue *q)
883 if (!req) 898 if (!req)
884 break; 899 break;
885 900
886 if (blk_fs_request(req)) { 901 if (req->cmd_type == REQ_TYPE_FS) {
887 struct i2o_block_delayed_request *dreq; 902 struct i2o_block_delayed_request *dreq;
888 struct i2o_block_request *ireq = req->special; 903 struct i2o_block_request *ireq = req->special;
889 unsigned int queue_depth; 904 unsigned int queue_depth;
@@ -930,7 +945,8 @@ static const struct block_device_operations i2o_block_fops = {
930 .owner = THIS_MODULE, 945 .owner = THIS_MODULE,
931 .open = i2o_block_open, 946 .open = i2o_block_open,
932 .release = i2o_block_release, 947 .release = i2o_block_release,
933 .locked_ioctl = i2o_block_ioctl, 948 .ioctl = i2o_block_ioctl,
949 .compat_ioctl = i2o_block_ioctl,
934 .getgeo = i2o_block_getgeo, 950 .getgeo = i2o_block_getgeo,
935 .media_changed = i2o_block_media_changed 951 .media_changed = i2o_block_media_changed
936}; 952};
diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index cb9fbc83b09..8433cde29c8 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -29,6 +29,7 @@
29#include <linux/kdev_t.h> 29#include <linux/kdev_t.h>
30#include <linux/blkdev.h> 30#include <linux/blkdev.h>
31#include <linux/mutex.h> 31#include <linux/mutex.h>
32#include <linux/smp_lock.h>
32#include <linux/scatterlist.h> 33#include <linux/scatterlist.h>
33#include <linux/string_helpers.h> 34#include <linux/string_helpers.h>
34 35
@@ -107,6 +108,7 @@ static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
107 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); 108 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
108 int ret = -ENXIO; 109 int ret = -ENXIO;
109 110
111 lock_kernel();
110 if (md) { 112 if (md) {
111 if (md->usage == 2) 113 if (md->usage == 2)
112 check_disk_change(bdev); 114 check_disk_change(bdev);
@@ -117,6 +119,7 @@ static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
117 ret = -EROFS; 119 ret = -EROFS;
118 } 120 }
119 } 121 }
122 unlock_kernel();
120 123
121 return ret; 124 return ret;
122} 125}
@@ -125,7 +128,9 @@ static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
125{ 128{
126 struct mmc_blk_data *md = disk->private_data; 129 struct mmc_blk_data *md = disk->private_data;
127 130
131 lock_kernel();
128 mmc_blk_put(md); 132 mmc_blk_put(md);
133 unlock_kernel();
129 return 0; 134 return 0;
130} 135}
131 136
diff --git a/drivers/mmc/card/queue.c b/drivers/mmc/card/queue.c
index d6ded247d94..c77eb49eda0 100644
--- a/drivers/mmc/card/queue.c
+++ b/drivers/mmc/card/queue.c
@@ -32,7 +32,7 @@ static int mmc_prep_request(struct request_queue *q, struct request *req)
32 /* 32 /*
33 * We only like normal block requests. 33 * We only like normal block requests.
34 */ 34 */
35 if (!blk_fs_request(req)) { 35 if (req->cmd_type != REQ_TYPE_FS) {
36 blk_dump_rq_flags(req, "MMC bad request"); 36 blk_dump_rq_flags(req, "MMC bad request");
37 return BLKPREP_KILL; 37 return BLKPREP_KILL;
38 } 38 }
@@ -128,7 +128,7 @@ int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock
128 mq->req = NULL; 128 mq->req = NULL;
129 129
130 blk_queue_prep_rq(mq->queue, mmc_prep_request); 130 blk_queue_prep_rq(mq->queue, mmc_prep_request);
131 blk_queue_ordered(mq->queue, QUEUE_ORDERED_DRAIN, NULL); 131 blk_queue_ordered(mq->queue, QUEUE_ORDERED_DRAIN);
132 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue); 132 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
133 133
134#ifdef CONFIG_MMC_BLOCK_BOUNCE 134#ifdef CONFIG_MMC_BLOCK_BOUNCE
diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig
index f8210bf2d24..1e2cbf5d9aa 100644
--- a/drivers/mtd/Kconfig
+++ b/drivers/mtd/Kconfig
@@ -311,15 +311,17 @@ config SM_FTL
311 select MTD_BLKDEVS 311 select MTD_BLKDEVS
312 select MTD_NAND_ECC 312 select MTD_NAND_ECC
313 help 313 help
314 This enables new and very EXPERMENTAL support for SmartMedia/xD 314 This enables EXPERIMENTAL R/W support for SmartMedia/xD
315 FTL (Flash translation layer). 315 FTL (Flash translation layer).
316 Write support isn't yet well tested, therefore this code IS likely to 316 Write support is only lightly tested, therefore this driver
317 eat your card, so please don't use it together with valuable data. 317 isn't recommended to use with valuable data (anyway if you have
318 Use readonly driver (CONFIG_SSFDC) instead. 318 valuable data, do backups regardless of software/hardware you
319 use, because you never know what will eat your data...)
320 If you only need R/O access, you can use older R/O driver
321 (CONFIG_SSFDC)
319 322
320config MTD_OOPS 323config MTD_OOPS
321 tristate "Log panic/oops to an MTD buffer" 324 tristate "Log panic/oops to an MTD buffer"
322 depends on MTD
323 help 325 help
324 This enables panic and oops messages to be logged to a circular 326 This enables panic and oops messages to be logged to a circular
325 buffer in a flash partition where it can be read back at some 327 buffer in a flash partition where it can be read back at some
diff --git a/drivers/mtd/afs.c b/drivers/mtd/afs.c
index cec7ab98b2a..302372c08b5 100644
--- a/drivers/mtd/afs.c
+++ b/drivers/mtd/afs.c
@@ -2,7 +2,7 @@
2 2
3 drivers/mtd/afs.c: ARM Flash Layout/Partitioning 3 drivers/mtd/afs.c: ARM Flash Layout/Partitioning
4 4
5 Copyright (C) 2000 ARM Limited 5 Copyright © 2000 ARM Limited
6 6
7 This program is free software; you can redistribute it and/or modify 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 8 it under the terms of the GNU General Public License as published by
diff --git a/drivers/mtd/chips/cfi_cmdset_0001.c b/drivers/mtd/chips/cfi_cmdset_0001.c
index 62f3ea9de84..9e2b7e9e0ad 100644
--- a/drivers/mtd/chips/cfi_cmdset_0001.c
+++ b/drivers/mtd/chips/cfi_cmdset_0001.c
@@ -34,7 +34,6 @@
34#include <linux/mtd/xip.h> 34#include <linux/mtd/xip.h>
35#include <linux/mtd/map.h> 35#include <linux/mtd/map.h>
36#include <linux/mtd/mtd.h> 36#include <linux/mtd/mtd.h>
37#include <linux/mtd/compatmac.h>
38#include <linux/mtd/cfi.h> 37#include <linux/mtd/cfi.h>
39 38
40/* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */ 39/* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */
@@ -63,6 +62,8 @@ static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
63static void cfi_intelext_sync (struct mtd_info *); 62static void cfi_intelext_sync (struct mtd_info *);
64static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len); 63static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
65static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len); 64static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
65static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs,
66 uint64_t len);
66#ifdef CONFIG_MTD_OTP 67#ifdef CONFIG_MTD_OTP
67static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *); 68static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
68static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *); 69static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
@@ -448,6 +449,7 @@ struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
448 mtd->sync = cfi_intelext_sync; 449 mtd->sync = cfi_intelext_sync;
449 mtd->lock = cfi_intelext_lock; 450 mtd->lock = cfi_intelext_lock;
450 mtd->unlock = cfi_intelext_unlock; 451 mtd->unlock = cfi_intelext_unlock;
452 mtd->is_locked = cfi_intelext_is_locked;
451 mtd->suspend = cfi_intelext_suspend; 453 mtd->suspend = cfi_intelext_suspend;
452 mtd->resume = cfi_intelext_resume; 454 mtd->resume = cfi_intelext_resume;
453 mtd->flags = MTD_CAP_NORFLASH; 455 mtd->flags = MTD_CAP_NORFLASH;
@@ -717,7 +719,7 @@ static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
717 chip = &newcfi->chips[0]; 719 chip = &newcfi->chips[0];
718 for (i = 0; i < cfi->numchips; i++) { 720 for (i = 0; i < cfi->numchips; i++) {
719 shared[i].writing = shared[i].erasing = NULL; 721 shared[i].writing = shared[i].erasing = NULL;
720 spin_lock_init(&shared[i].lock); 722 mutex_init(&shared[i].lock);
721 for (j = 0; j < numparts; j++) { 723 for (j = 0; j < numparts; j++) {
722 *chip = cfi->chips[i]; 724 *chip = cfi->chips[i];
723 chip->start += j << partshift; 725 chip->start += j << partshift;
@@ -886,7 +888,7 @@ static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr
886 */ 888 */
887 struct flchip_shared *shared = chip->priv; 889 struct flchip_shared *shared = chip->priv;
888 struct flchip *contender; 890 struct flchip *contender;
889 spin_lock(&shared->lock); 891 mutex_lock(&shared->lock);
890 contender = shared->writing; 892 contender = shared->writing;
891 if (contender && contender != chip) { 893 if (contender && contender != chip) {
892 /* 894 /*
@@ -899,7 +901,7 @@ static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr
899 * get_chip returns success we're clear to go ahead. 901 * get_chip returns success we're clear to go ahead.
900 */ 902 */
901 ret = mutex_trylock(&contender->mutex); 903 ret = mutex_trylock(&contender->mutex);
902 spin_unlock(&shared->lock); 904 mutex_unlock(&shared->lock);
903 if (!ret) 905 if (!ret)
904 goto retry; 906 goto retry;
905 mutex_unlock(&chip->mutex); 907 mutex_unlock(&chip->mutex);
@@ -914,7 +916,7 @@ static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr
914 mutex_unlock(&contender->mutex); 916 mutex_unlock(&contender->mutex);
915 return ret; 917 return ret;
916 } 918 }
917 spin_lock(&shared->lock); 919 mutex_lock(&shared->lock);
918 920
919 /* We should not own chip if it is already 921 /* We should not own chip if it is already
920 * in FL_SYNCING state. Put contender and retry. */ 922 * in FL_SYNCING state. Put contender and retry. */
@@ -930,7 +932,7 @@ static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr
930 * on this chip. Sleep. */ 932 * on this chip. Sleep. */
931 if (mode == FL_ERASING && shared->erasing 933 if (mode == FL_ERASING && shared->erasing
932 && shared->erasing->oldstate == FL_ERASING) { 934 && shared->erasing->oldstate == FL_ERASING) {
933 spin_unlock(&shared->lock); 935 mutex_unlock(&shared->lock);
934 set_current_state(TASK_UNINTERRUPTIBLE); 936 set_current_state(TASK_UNINTERRUPTIBLE);
935 add_wait_queue(&chip->wq, &wait); 937 add_wait_queue(&chip->wq, &wait);
936 mutex_unlock(&chip->mutex); 938 mutex_unlock(&chip->mutex);
@@ -944,7 +946,7 @@ static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr
944 shared->writing = chip; 946 shared->writing = chip;
945 if (mode == FL_ERASING) 947 if (mode == FL_ERASING)
946 shared->erasing = chip; 948 shared->erasing = chip;
947 spin_unlock(&shared->lock); 949 mutex_unlock(&shared->lock);
948 } 950 }
949 ret = chip_ready(map, chip, adr, mode); 951 ret = chip_ready(map, chip, adr, mode);
950 if (ret == -EAGAIN) 952 if (ret == -EAGAIN)
@@ -959,7 +961,7 @@ static void put_chip(struct map_info *map, struct flchip *chip, unsigned long ad
959 961
960 if (chip->priv) { 962 if (chip->priv) {
961 struct flchip_shared *shared = chip->priv; 963 struct flchip_shared *shared = chip->priv;
962 spin_lock(&shared->lock); 964 mutex_lock(&shared->lock);
963 if (shared->writing == chip && chip->oldstate == FL_READY) { 965 if (shared->writing == chip && chip->oldstate == FL_READY) {
964 /* We own the ability to write, but we're done */ 966 /* We own the ability to write, but we're done */
965 shared->writing = shared->erasing; 967 shared->writing = shared->erasing;
@@ -967,7 +969,7 @@ static void put_chip(struct map_info *map, struct flchip *chip, unsigned long ad
967 /* give back ownership to who we loaned it from */ 969 /* give back ownership to who we loaned it from */
968 struct flchip *loaner = shared->writing; 970 struct flchip *loaner = shared->writing;
969 mutex_lock(&loaner->mutex); 971 mutex_lock(&loaner->mutex);
970 spin_unlock(&shared->lock); 972 mutex_unlock(&shared->lock);
971 mutex_unlock(&chip->mutex); 973 mutex_unlock(&chip->mutex);
972 put_chip(map, loaner, loaner->start); 974 put_chip(map, loaner, loaner->start);
973 mutex_lock(&chip->mutex); 975 mutex_lock(&chip->mutex);
@@ -985,11 +987,11 @@ static void put_chip(struct map_info *map, struct flchip *chip, unsigned long ad
985 * Don't let the switch below mess things up since 987 * Don't let the switch below mess things up since
986 * we don't have ownership to resume anything. 988 * we don't have ownership to resume anything.
987 */ 989 */
988 spin_unlock(&shared->lock); 990 mutex_unlock(&shared->lock);
989 wake_up(&chip->wq); 991 wake_up(&chip->wq);
990 return; 992 return;
991 } 993 }
992 spin_unlock(&shared->lock); 994 mutex_unlock(&shared->lock);
993 } 995 }
994 996
995 switch(chip->oldstate) { 997 switch(chip->oldstate) {
@@ -2139,6 +2141,13 @@ static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2139 return ret; 2141 return ret;
2140} 2142}
2141 2143
2144static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs,
2145 uint64_t len)
2146{
2147 return cfi_varsize_frob(mtd, do_getlockstatus_oneblock,
2148 ofs, len, NULL) ? 1 : 0;
2149}
2150
2142#ifdef CONFIG_MTD_OTP 2151#ifdef CONFIG_MTD_OTP
2143 2152
2144typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip, 2153typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
diff --git a/drivers/mtd/chips/cfi_cmdset_0002.c b/drivers/mtd/chips/cfi_cmdset_0002.c
index d81079ef91a..3e6c47bdce5 100644
--- a/drivers/mtd/chips/cfi_cmdset_0002.c
+++ b/drivers/mtd/chips/cfi_cmdset_0002.c
@@ -33,7 +33,6 @@
33#include <linux/delay.h> 33#include <linux/delay.h>
34#include <linux/interrupt.h> 34#include <linux/interrupt.h>
35#include <linux/reboot.h> 35#include <linux/reboot.h>
36#include <linux/mtd/compatmac.h>
37#include <linux/mtd/map.h> 36#include <linux/mtd/map.h>
38#include <linux/mtd/mtd.h> 37#include <linux/mtd/mtd.h>
39#include <linux/mtd/cfi.h> 38#include <linux/mtd/cfi.h>
@@ -417,16 +416,26 @@ struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
417 */ 416 */
418 cfi_fixup_major_minor(cfi, extp); 417 cfi_fixup_major_minor(cfi, extp);
419 418
419 /*
420 * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4
421 * see: http://www.amd.com/us-en/assets/content_type/DownloadableAssets/cfi_r20.pdf, page 19
422 * http://www.amd.com/us-en/assets/content_type/DownloadableAssets/cfi_100_20011201.pdf
423 * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
424 */
420 if (extp->MajorVersion != '1' || 425 if (extp->MajorVersion != '1' ||
421 (extp->MinorVersion < '0' || extp->MinorVersion > '4')) { 426 (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '4'))) {
422 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query " 427 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
423 "version %c.%c.\n", extp->MajorVersion, 428 "version %c.%c (%#02x/%#02x).\n",
424 extp->MinorVersion); 429 extp->MajorVersion, extp->MinorVersion,
430 extp->MajorVersion, extp->MinorVersion);
425 kfree(extp); 431 kfree(extp);
426 kfree(mtd); 432 kfree(mtd);
427 return NULL; 433 return NULL;
428 } 434 }
429 435
436 printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
437 extp->MajorVersion, extp->MinorVersion);
438
430 /* Install our own private info structure */ 439 /* Install our own private info structure */
431 cfi->cmdset_priv = extp; 440 cfi->cmdset_priv = extp;
432 441
diff --git a/drivers/mtd/chips/cfi_cmdset_0020.c b/drivers/mtd/chips/cfi_cmdset_0020.c
index e54e8c169d7..314af1f5a37 100644
--- a/drivers/mtd/chips/cfi_cmdset_0020.c
+++ b/drivers/mtd/chips/cfi_cmdset_0020.c
@@ -33,7 +33,6 @@
33#include <linux/mtd/map.h> 33#include <linux/mtd/map.h>
34#include <linux/mtd/cfi.h> 34#include <linux/mtd/cfi.h>
35#include <linux/mtd/mtd.h> 35#include <linux/mtd/mtd.h>
36#include <linux/mtd/compatmac.h>
37 36
38 37
39static int cfi_staa_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *); 38static int cfi_staa_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
diff --git a/drivers/mtd/chips/cfi_probe.c b/drivers/mtd/chips/cfi_probe.c
index b2acd32f4fb..8f5b96aa87a 100644
--- a/drivers/mtd/chips/cfi_probe.c
+++ b/drivers/mtd/chips/cfi_probe.c
@@ -235,9 +235,9 @@ static int __xipram cfi_chip_setup(struct map_info *map,
235 cfi_qry_mode_off(base, map, cfi); 235 cfi_qry_mode_off(base, map, cfi);
236 xip_allowed(base, map); 236 xip_allowed(base, map);
237 237
238 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank\n", 238 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank. Manufacturer ID %#08x Chip ID %#08x\n",
239 map->name, cfi->interleave, cfi->device_type*8, base, 239 map->name, cfi->interleave, cfi->device_type*8, base,
240 map->bankwidth*8); 240 map->bankwidth*8, cfi->mfr, cfi->id);
241 241
242 return 1; 242 return 1;
243} 243}
diff --git a/drivers/mtd/chips/cfi_util.c b/drivers/mtd/chips/cfi_util.c
index d7c2c672757..e503b2ca894 100644
--- a/drivers/mtd/chips/cfi_util.c
+++ b/drivers/mtd/chips/cfi_util.c
@@ -22,7 +22,6 @@
22#include <linux/mtd/mtd.h> 22#include <linux/mtd/mtd.h>
23#include <linux/mtd/map.h> 23#include <linux/mtd/map.h>
24#include <linux/mtd/cfi.h> 24#include <linux/mtd/cfi.h>
25#include <linux/mtd/compatmac.h>
26 25
27int __xipram cfi_qry_present(struct map_info *map, __u32 base, 26int __xipram cfi_qry_present(struct map_info *map, __u32 base,
28 struct cfi_private *cfi) 27 struct cfi_private *cfi)
diff --git a/drivers/mtd/chips/chipreg.c b/drivers/mtd/chips/chipreg.c
index c8576096822..da1f96f385c 100644
--- a/drivers/mtd/chips/chipreg.c
+++ b/drivers/mtd/chips/chipreg.c
@@ -10,7 +10,6 @@
10#include <linux/slab.h> 10#include <linux/slab.h>
11#include <linux/mtd/map.h> 11#include <linux/mtd/map.h>
12#include <linux/mtd/mtd.h> 12#include <linux/mtd/mtd.h>
13#include <linux/mtd/compatmac.h>
14 13
15static DEFINE_SPINLOCK(chip_drvs_lock); 14static DEFINE_SPINLOCK(chip_drvs_lock);
16static LIST_HEAD(chip_drvs_list); 15static LIST_HEAD(chip_drvs_list);
diff --git a/drivers/mtd/chips/map_absent.c b/drivers/mtd/chips/map_absent.c
index 494d30d0631..f2b87294687 100644
--- a/drivers/mtd/chips/map_absent.c
+++ b/drivers/mtd/chips/map_absent.c
@@ -25,7 +25,6 @@
25#include <linux/init.h> 25#include <linux/init.h>
26#include <linux/mtd/mtd.h> 26#include <linux/mtd/mtd.h>
27#include <linux/mtd/map.h> 27#include <linux/mtd/map.h>
28#include <linux/mtd/compatmac.h>
29 28
30static int map_absent_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); 29static int map_absent_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
31static int map_absent_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *); 30static int map_absent_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
diff --git a/drivers/mtd/chips/map_ram.c b/drivers/mtd/chips/map_ram.c
index 6bdc50c727e..67640ccb2d4 100644
--- a/drivers/mtd/chips/map_ram.c
+++ b/drivers/mtd/chips/map_ram.c
@@ -13,7 +13,6 @@
13#include <linux/init.h> 13#include <linux/init.h>
14#include <linux/mtd/mtd.h> 14#include <linux/mtd/mtd.h>
15#include <linux/mtd/map.h> 15#include <linux/mtd/map.h>
16#include <linux/mtd/compatmac.h>
17 16
18 17
19static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); 18static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
diff --git a/drivers/mtd/chips/map_rom.c b/drivers/mtd/chips/map_rom.c
index 076090a67b9..593f73d480d 100644
--- a/drivers/mtd/chips/map_rom.c
+++ b/drivers/mtd/chips/map_rom.c
@@ -13,7 +13,6 @@
13#include <linux/init.h> 13#include <linux/init.h>
14#include <linux/mtd/mtd.h> 14#include <linux/mtd/mtd.h>
15#include <linux/mtd/map.h> 15#include <linux/mtd/map.h>
16#include <linux/mtd/compatmac.h>
17 16
18static int maprom_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); 17static int maprom_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
19static int maprom_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *); 18static int maprom_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
diff --git a/drivers/mtd/cmdlinepart.c b/drivers/mtd/cmdlinepart.c
index 1479da6d3aa..e790f38893b 100644
--- a/drivers/mtd/cmdlinepart.c
+++ b/drivers/mtd/cmdlinepart.c
@@ -1,7 +1,22 @@
1/* 1/*
2 * Read flash partition table from command line 2 * Read flash partition table from command line
3 * 3 *
4 * Copyright 2002 SYSGO Real-Time Solutions GmbH 4 * Copyright © 2002 SYSGO Real-Time Solutions GmbH
5 * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
5 * 20 *
6 * The format for the command line is as follows: 21 * The format for the command line is as follows:
7 * 22 *
diff --git a/drivers/mtd/devices/docecc.c b/drivers/mtd/devices/docecc.c
index a19cda52da5..a99838bb2dc 100644
--- a/drivers/mtd/devices/docecc.c
+++ b/drivers/mtd/devices/docecc.c
@@ -31,7 +31,6 @@
31#include <linux/init.h> 31#include <linux/init.h>
32#include <linux/types.h> 32#include <linux/types.h>
33 33
34#include <linux/mtd/compatmac.h> /* for min() in older kernels */
35#include <linux/mtd/mtd.h> 34#include <linux/mtd/mtd.h>
36#include <linux/mtd/doc2000.h> 35#include <linux/mtd/doc2000.h>
37 36
diff --git a/drivers/mtd/devices/docprobe.c b/drivers/mtd/devices/docprobe.c
index 6e62922942b..d374603493a 100644
--- a/drivers/mtd/devices/docprobe.c
+++ b/drivers/mtd/devices/docprobe.c
@@ -49,7 +49,6 @@
49#include <linux/mtd/mtd.h> 49#include <linux/mtd/mtd.h>
50#include <linux/mtd/nand.h> 50#include <linux/mtd/nand.h>
51#include <linux/mtd/doc2000.h> 51#include <linux/mtd/doc2000.h>
52#include <linux/mtd/compatmac.h>
53 52
54/* Where to look for the devices? */ 53/* Where to look for the devices? */
55#ifndef CONFIG_MTD_DOCPROBE_ADDRESS 54#ifndef CONFIG_MTD_DOCPROBE_ADDRESS
diff --git a/drivers/mtd/devices/m25p80.c b/drivers/mtd/devices/m25p80.c
index 81e49a9b017..f90941a785e 100644
--- a/drivers/mtd/devices/m25p80.c
+++ b/drivers/mtd/devices/m25p80.c
@@ -16,6 +16,8 @@
16 */ 16 */
17 17
18#include <linux/init.h> 18#include <linux/init.h>
19#include <linux/err.h>
20#include <linux/errno.h>
19#include <linux/module.h> 21#include <linux/module.h>
20#include <linux/device.h> 22#include <linux/device.h>
21#include <linux/interrupt.h> 23#include <linux/interrupt.h>
@@ -639,8 +641,18 @@ static const struct spi_device_id m25p_ids[] = {
639 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) }, 641 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
640 { "at26df321", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) }, 642 { "at26df321", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
641 643
644 /* EON -- en25pxx */
645 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
646 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
647
648 /* Intel/Numonyx -- xxxs33b */
649 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
650 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
651 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
652
642 /* Macronix */ 653 /* Macronix */
643 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) }, 654 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
655 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
644 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) }, 656 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
645 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) }, 657 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
646 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) }, 658 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
@@ -680,6 +692,16 @@ static const struct spi_device_id m25p_ids[] = {
680 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) }, 692 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
681 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) }, 693 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
682 694
695 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
696 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
697 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
698 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
699 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
700 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
701 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
702 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
703 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
704
683 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) }, 705 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
684 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) }, 706 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
685 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) }, 707 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
@@ -694,6 +716,7 @@ static const struct spi_device_id m25p_ids[] = {
694 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) }, 716 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
695 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) }, 717 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
696 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) }, 718 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
719 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
697 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) }, 720 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
698 721
699 /* Catalyst / On Semiconductor -- non-JEDEC */ 722 /* Catalyst / On Semiconductor -- non-JEDEC */
@@ -723,7 +746,7 @@ static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi)
723 if (tmp < 0) { 746 if (tmp < 0) {
724 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n", 747 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
725 dev_name(&spi->dev), tmp); 748 dev_name(&spi->dev), tmp);
726 return NULL; 749 return ERR_PTR(tmp);
727 } 750 }
728 jedec = id[0]; 751 jedec = id[0];
729 jedec = jedec << 8; 752 jedec = jedec << 8;
@@ -731,14 +754,6 @@ static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi)
731 jedec = jedec << 8; 754 jedec = jedec << 8;
732 jedec |= id[2]; 755 jedec |= id[2];
733 756
734 /*
735 * Some chips (like Numonyx M25P80) have JEDEC and non-JEDEC variants,
736 * which depend on technology process. Officially RDID command doesn't
737 * exist for non-JEDEC chips, but for compatibility they return ID 0.
738 */
739 if (jedec == 0)
740 return NULL;
741
742 ext_jedec = id[3] << 8 | id[4]; 757 ext_jedec = id[3] << 8 | id[4];
743 758
744 for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) { 759 for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
@@ -749,7 +764,7 @@ static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi)
749 return &m25p_ids[tmp]; 764 return &m25p_ids[tmp];
750 } 765 }
751 } 766 }
752 return NULL; 767 return ERR_PTR(-ENODEV);
753} 768}
754 769
755 770
@@ -794,9 +809,8 @@ static int __devinit m25p_probe(struct spi_device *spi)
794 const struct spi_device_id *jid; 809 const struct spi_device_id *jid;
795 810
796 jid = jedec_probe(spi); 811 jid = jedec_probe(spi);
797 if (!jid) { 812 if (IS_ERR(jid)) {
798 dev_info(&spi->dev, "non-JEDEC variant of %s\n", 813 return PTR_ERR(jid);
799 id->name);
800 } else if (jid != id) { 814 } else if (jid != id) {
801 /* 815 /*
802 * JEDEC knows better, so overwrite platform ID. We 816 * JEDEC knows better, so overwrite platform ID. We
@@ -826,11 +840,12 @@ static int __devinit m25p_probe(struct spi_device *spi)
826 dev_set_drvdata(&spi->dev, flash); 840 dev_set_drvdata(&spi->dev, flash);
827 841
828 /* 842 /*
829 * Atmel and SST serial flash tend to power 843 * Atmel, SST and Intel/Numonyx serial flash tend to power
830 * up with the software protection bits set 844 * up with the software protection bits set
831 */ 845 */
832 846
833 if (info->jedec_id >> 16 == 0x1f || 847 if (info->jedec_id >> 16 == 0x1f ||
848 info->jedec_id >> 16 == 0x89 ||
834 info->jedec_id >> 16 == 0xbf) { 849 info->jedec_id >> 16 == 0xbf) {
835 write_enable(flash); 850 write_enable(flash);
836 write_sr(flash, 0); 851 write_sr(flash, 0);
diff --git a/drivers/mtd/devices/mtd_dataflash.c b/drivers/mtd/devices/mtd_dataflash.c
index 19817404ce7..c5015cc721d 100644
--- a/drivers/mtd/devices/mtd_dataflash.c
+++ b/drivers/mtd/devices/mtd_dataflash.c
@@ -141,7 +141,7 @@ static int dataflash_waitready(struct spi_device *spi)
141 */ 141 */
142static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr) 142static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr)
143{ 143{
144 struct dataflash *priv = (struct dataflash *)mtd->priv; 144 struct dataflash *priv = mtd->priv;
145 struct spi_device *spi = priv->spi; 145 struct spi_device *spi = priv->spi;
146 struct spi_transfer x = { .tx_dma = 0, }; 146 struct spi_transfer x = { .tx_dma = 0, };
147 struct spi_message msg; 147 struct spi_message msg;
@@ -231,7 +231,7 @@ static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr)
231static int dataflash_read(struct mtd_info *mtd, loff_t from, size_t len, 231static int dataflash_read(struct mtd_info *mtd, loff_t from, size_t len,
232 size_t *retlen, u_char *buf) 232 size_t *retlen, u_char *buf)
233{ 233{
234 struct dataflash *priv = (struct dataflash *)mtd->priv; 234 struct dataflash *priv = mtd->priv;
235 struct spi_transfer x[2] = { { .tx_dma = 0, }, }; 235 struct spi_transfer x[2] = { { .tx_dma = 0, }, };
236 struct spi_message msg; 236 struct spi_message msg;
237 unsigned int addr; 237 unsigned int addr;
@@ -304,7 +304,7 @@ static int dataflash_read(struct mtd_info *mtd, loff_t from, size_t len,
304static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len, 304static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
305 size_t * retlen, const u_char * buf) 305 size_t * retlen, const u_char * buf)
306{ 306{
307 struct dataflash *priv = (struct dataflash *)mtd->priv; 307 struct dataflash *priv = mtd->priv;
308 struct spi_device *spi = priv->spi; 308 struct spi_device *spi = priv->spi;
309 struct spi_transfer x[2] = { { .tx_dma = 0, }, }; 309 struct spi_transfer x[2] = { { .tx_dma = 0, }, };
310 struct spi_message msg; 310 struct spi_message msg;
@@ -515,7 +515,7 @@ static ssize_t otp_read(struct spi_device *spi, unsigned base,
515static int dataflash_read_fact_otp(struct mtd_info *mtd, 515static int dataflash_read_fact_otp(struct mtd_info *mtd,
516 loff_t from, size_t len, size_t *retlen, u_char *buf) 516 loff_t from, size_t len, size_t *retlen, u_char *buf)
517{ 517{
518 struct dataflash *priv = (struct dataflash *)mtd->priv; 518 struct dataflash *priv = mtd->priv;
519 int status; 519 int status;
520 520
521 /* 64 bytes, from 0..63 ... start at 64 on-chip */ 521 /* 64 bytes, from 0..63 ... start at 64 on-chip */
@@ -532,7 +532,7 @@ static int dataflash_read_fact_otp(struct mtd_info *mtd,
532static int dataflash_read_user_otp(struct mtd_info *mtd, 532static int dataflash_read_user_otp(struct mtd_info *mtd,
533 loff_t from, size_t len, size_t *retlen, u_char *buf) 533 loff_t from, size_t len, size_t *retlen, u_char *buf)
534{ 534{
535 struct dataflash *priv = (struct dataflash *)mtd->priv; 535 struct dataflash *priv = mtd->priv;
536 int status; 536 int status;
537 537
538 /* 64 bytes, from 0..63 ... start at 0 on-chip */ 538 /* 64 bytes, from 0..63 ... start at 0 on-chip */
@@ -553,7 +553,7 @@ static int dataflash_write_user_otp(struct mtd_info *mtd,
553 const size_t l = 4 + 64; 553 const size_t l = 4 + 64;
554 uint8_t *scratch; 554 uint8_t *scratch;
555 struct spi_transfer t; 555 struct spi_transfer t;
556 struct dataflash *priv = (struct dataflash *)mtd->priv; 556 struct dataflash *priv = mtd->priv;
557 int status; 557 int status;
558 558
559 if (len > 64) 559 if (len > 64)
diff --git a/drivers/mtd/devices/mtdram.c b/drivers/mtd/devices/mtdram.c
index fce5ff7589a..26a6e809013 100644
--- a/drivers/mtd/devices/mtdram.c
+++ b/drivers/mtd/devices/mtdram.c
@@ -14,7 +14,6 @@
14#include <linux/ioport.h> 14#include <linux/ioport.h>
15#include <linux/vmalloc.h> 15#include <linux/vmalloc.h>
16#include <linux/init.h> 16#include <linux/init.h>
17#include <linux/mtd/compatmac.h>
18#include <linux/mtd/mtd.h> 17#include <linux/mtd/mtd.h>
19#include <linux/mtd/mtdram.h> 18#include <linux/mtd/mtdram.h>
20 19
diff --git a/drivers/mtd/devices/pmc551.c b/drivers/mtd/devices/pmc551.c
index fc8ea0a57ac..ef0aba0ce58 100644
--- a/drivers/mtd/devices/pmc551.c
+++ b/drivers/mtd/devices/pmc551.c
@@ -98,7 +98,6 @@
98 98
99#include <linux/mtd/mtd.h> 99#include <linux/mtd/mtd.h>
100#include <linux/mtd/pmc551.h> 100#include <linux/mtd/pmc551.h>
101#include <linux/mtd/compatmac.h>
102 101
103static struct mtd_info *pmc551list; 102static struct mtd_info *pmc551list;
104 103
diff --git a/drivers/mtd/devices/sst25l.c b/drivers/mtd/devices/sst25l.c
index ab5d8cd02a1..684247a8a5e 100644
--- a/drivers/mtd/devices/sst25l.c
+++ b/drivers/mtd/devices/sst25l.c
@@ -454,7 +454,7 @@ static int __init sst25l_probe(struct spi_device *spi)
454 parts, nr_parts); 454 parts, nr_parts);
455 } 455 }
456 456
457 } else if (data->nr_parts) { 457 } else if (data && data->nr_parts) {
458 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n", 458 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
459 data->nr_parts, data->name); 459 data->nr_parts, data->name);
460 } 460 }
diff --git a/drivers/mtd/ftl.c b/drivers/mtd/ftl.c
index 62da9eb7032..4d6a64c387e 100644
--- a/drivers/mtd/ftl.c
+++ b/drivers/mtd/ftl.c
@@ -26,7 +26,7 @@
26 26
27 The initial developer of the original code is David A. Hinds 27 The initial developer of the original code is David A. Hinds
28 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds 28 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
29 are Copyright (C) 1999 David A. Hinds. All Rights Reserved. 29 are Copyright © 1999 David A. Hinds. All Rights Reserved.
30 30
31 Alternatively, the contents of this file may be used under the 31 Alternatively, the contents of this file may be used under the
32 terms of the GNU General Public License version 2 (the "GPL"), in 32 terms of the GNU General Public License version 2 (the "GPL"), in
diff --git a/drivers/mtd/inftlcore.c b/drivers/mtd/inftlcore.c
index 015a7fe1b6e..d7592e67d04 100644
--- a/drivers/mtd/inftlcore.c
+++ b/drivers/mtd/inftlcore.c
@@ -1,11 +1,11 @@
1/* 1/*
2 * inftlcore.c -- Linux driver for Inverse Flash Translation Layer (INFTL) 2 * inftlcore.c -- Linux driver for Inverse Flash Translation Layer (INFTL)
3 * 3 *
4 * (C) Copyright 2002, Greg Ungerer (gerg@snapgear.com) 4 * Copyright © 2002, Greg Ungerer (gerg@snapgear.com)
5 * 5 *
6 * Based heavily on the nftlcore.c code which is: 6 * Based heavily on the nftlcore.c code which is:
7 * (c) 1999 Machine Vision Holdings, Inc. 7 * Copyright © 1999 Machine Vision Holdings, Inc.
8 * Author: David Woodhouse <dwmw2@infradead.org> 8 * Copyright © 1999 David Woodhouse <dwmw2@infradead.org>
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
diff --git a/drivers/mtd/inftlmount.c b/drivers/mtd/inftlmount.c
index 8f988d7d3c5..104052e774b 100644
--- a/drivers/mtd/inftlmount.c
+++ b/drivers/mtd/inftlmount.c
@@ -2,11 +2,11 @@
2 * inftlmount.c -- INFTL mount code with extensive checks. 2 * inftlmount.c -- INFTL mount code with extensive checks.
3 * 3 *
4 * Author: Greg Ungerer (gerg@snapgear.com) 4 * Author: Greg Ungerer (gerg@snapgear.com)
5 * (C) Copyright 2002-2003, Greg Ungerer (gerg@snapgear.com) 5 * Copyright © 2002-2003, Greg Ungerer (gerg@snapgear.com)
6 * 6 *
7 * Based heavily on the nftlmount.c code which is: 7 * Based heavily on the nftlmount.c code which is:
8 * Author: Fabrice Bellard (fabrice.bellard@netgem.com) 8 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
9 * Copyright (C) 2000 Netgem S.A. 9 * Copyright © 2000 Netgem S.A.
10 * 10 *
11 * This program is free software; you can redistribute it and/or modify 11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by 12 * it under the terms of the GNU General Public License as published by
@@ -34,7 +34,6 @@
34#include <linux/mtd/mtd.h> 34#include <linux/mtd/mtd.h>
35#include <linux/mtd/nftl.h> 35#include <linux/mtd/nftl.h>
36#include <linux/mtd/inftl.h> 36#include <linux/mtd/inftl.h>
37#include <linux/mtd/compatmac.h>
38 37
39/* 38/*
40 * find_boot_record: Find the INFTL Media Header and its Spare copy which 39 * find_boot_record: Find the INFTL Media Header and its Spare copy which
diff --git a/drivers/mtd/lpddr/lpddr_cmds.c b/drivers/mtd/lpddr/lpddr_cmds.c
index fece5be5871..04fdfcca93f 100644
--- a/drivers/mtd/lpddr/lpddr_cmds.c
+++ b/drivers/mtd/lpddr/lpddr_cmds.c
@@ -98,7 +98,7 @@ struct mtd_info *lpddr_cmdset(struct map_info *map)
98 numchips = lpddr->numchips / lpddr->qinfo->HWPartsNum; 98 numchips = lpddr->numchips / lpddr->qinfo->HWPartsNum;
99 for (i = 0; i < numchips; i++) { 99 for (i = 0; i < numchips; i++) {
100 shared[i].writing = shared[i].erasing = NULL; 100 shared[i].writing = shared[i].erasing = NULL;
101 spin_lock_init(&shared[i].lock); 101 mutex_init(&shared[i].lock);
102 for (j = 0; j < lpddr->qinfo->HWPartsNum; j++) { 102 for (j = 0; j < lpddr->qinfo->HWPartsNum; j++) {
103 *chip = lpddr->chips[i]; 103 *chip = lpddr->chips[i];
104 chip->start += j << lpddr->chipshift; 104 chip->start += j << lpddr->chipshift;
@@ -217,7 +217,7 @@ static int get_chip(struct map_info *map, struct flchip *chip, int mode)
217 */ 217 */
218 struct flchip_shared *shared = chip->priv; 218 struct flchip_shared *shared = chip->priv;
219 struct flchip *contender; 219 struct flchip *contender;
220 spin_lock(&shared->lock); 220 mutex_lock(&shared->lock);
221 contender = shared->writing; 221 contender = shared->writing;
222 if (contender && contender != chip) { 222 if (contender && contender != chip) {
223 /* 223 /*
@@ -230,7 +230,7 @@ static int get_chip(struct map_info *map, struct flchip *chip, int mode)
230 * get_chip returns success we're clear to go ahead. 230 * get_chip returns success we're clear to go ahead.
231 */ 231 */
232 ret = mutex_trylock(&contender->mutex); 232 ret = mutex_trylock(&contender->mutex);
233 spin_unlock(&shared->lock); 233 mutex_unlock(&shared->lock);
234 if (!ret) 234 if (!ret)
235 goto retry; 235 goto retry;
236 mutex_unlock(&chip->mutex); 236 mutex_unlock(&chip->mutex);
@@ -245,7 +245,7 @@ static int get_chip(struct map_info *map, struct flchip *chip, int mode)
245 mutex_unlock(&contender->mutex); 245 mutex_unlock(&contender->mutex);
246 return ret; 246 return ret;
247 } 247 }
248 spin_lock(&shared->lock); 248 mutex_lock(&shared->lock);
249 249
250 /* We should not own chip if it is already in FL_SYNCING 250 /* We should not own chip if it is already in FL_SYNCING
251 * state. Put contender and retry. */ 251 * state. Put contender and retry. */
@@ -261,7 +261,7 @@ static int get_chip(struct map_info *map, struct flchip *chip, int mode)
261 Must sleep in such a case. */ 261 Must sleep in such a case. */
262 if (mode == FL_ERASING && shared->erasing 262 if (mode == FL_ERASING && shared->erasing
263 && shared->erasing->oldstate == FL_ERASING) { 263 && shared->erasing->oldstate == FL_ERASING) {
264 spin_unlock(&shared->lock); 264 mutex_unlock(&shared->lock);
265 set_current_state(TASK_UNINTERRUPTIBLE); 265 set_current_state(TASK_UNINTERRUPTIBLE);
266 add_wait_queue(&chip->wq, &wait); 266 add_wait_queue(&chip->wq, &wait);
267 mutex_unlock(&chip->mutex); 267 mutex_unlock(&chip->mutex);
@@ -275,7 +275,7 @@ static int get_chip(struct map_info *map, struct flchip *chip, int mode)
275 shared->writing = chip; 275 shared->writing = chip;
276 if (mode == FL_ERASING) 276 if (mode == FL_ERASING)
277 shared->erasing = chip; 277 shared->erasing = chip;
278 spin_unlock(&shared->lock); 278 mutex_unlock(&shared->lock);
279 } 279 }
280 280
281 ret = chip_ready(map, chip, mode); 281 ret = chip_ready(map, chip, mode);
@@ -348,7 +348,7 @@ static void put_chip(struct map_info *map, struct flchip *chip)
348{ 348{
349 if (chip->priv) { 349 if (chip->priv) {
350 struct flchip_shared *shared = chip->priv; 350 struct flchip_shared *shared = chip->priv;
351 spin_lock(&shared->lock); 351 mutex_lock(&shared->lock);
352 if (shared->writing == chip && chip->oldstate == FL_READY) { 352 if (shared->writing == chip && chip->oldstate == FL_READY) {
353 /* We own the ability to write, but we're done */ 353 /* We own the ability to write, but we're done */
354 shared->writing = shared->erasing; 354 shared->writing = shared->erasing;
@@ -356,7 +356,7 @@ static void put_chip(struct map_info *map, struct flchip *chip)
356 /* give back the ownership */ 356 /* give back the ownership */
357 struct flchip *loaner = shared->writing; 357 struct flchip *loaner = shared->writing;
358 mutex_lock(&loaner->mutex); 358 mutex_lock(&loaner->mutex);
359 spin_unlock(&shared->lock); 359 mutex_unlock(&shared->lock);
360 mutex_unlock(&chip->mutex); 360 mutex_unlock(&chip->mutex);
361 put_chip(map, loaner); 361 put_chip(map, loaner);
362 mutex_lock(&chip->mutex); 362 mutex_lock(&chip->mutex);
@@ -374,11 +374,11 @@ static void put_chip(struct map_info *map, struct flchip *chip)
374 * Don't let the switch below mess things up since 374 * Don't let the switch below mess things up since
375 * we don't have ownership to resume anything. 375 * we don't have ownership to resume anything.
376 */ 376 */
377 spin_unlock(&shared->lock); 377 mutex_unlock(&shared->lock);
378 wake_up(&chip->wq); 378 wake_up(&chip->wq);
379 return; 379 return;
380 } 380 }
381 spin_unlock(&shared->lock); 381 mutex_unlock(&shared->lock);
382 } 382 }
383 383
384 switch (chip->oldstate) { 384 switch (chip->oldstate) {
diff --git a/drivers/mtd/maps/Kconfig b/drivers/mtd/maps/Kconfig
index 6629d09f3b3..701d942c679 100644
--- a/drivers/mtd/maps/Kconfig
+++ b/drivers/mtd/maps/Kconfig
@@ -319,14 +319,6 @@ config MTD_CFI_FLAGADM
319 Mapping for the Flaga digital module. If you don't have one, ignore 319 Mapping for the Flaga digital module. If you don't have one, ignore
320 this setting. 320 this setting.
321 321
322config MTD_REDWOOD
323 tristate "CFI Flash devices mapped on IBM Redwood"
324 depends on MTD_CFI
325 help
326 This enables access routines for the flash chips on the IBM
327 Redwood board. If you have one of these boards and would like to
328 use the flash chips on it, say 'Y'.
329
330config MTD_SOLUTIONENGINE 322config MTD_SOLUTIONENGINE
331 tristate "CFI Flash device mapped on Hitachi SolutionEngine" 323 tristate "CFI Flash device mapped on Hitachi SolutionEngine"
332 depends on SUPERH && SOLUTION_ENGINE && MTD_CFI && MTD_REDBOOT_PARTS 324 depends on SUPERH && SOLUTION_ENGINE && MTD_CFI && MTD_REDBOOT_PARTS
diff --git a/drivers/mtd/maps/Makefile b/drivers/mtd/maps/Makefile
index bb035cd54c7..f216bb57371 100644
--- a/drivers/mtd/maps/Makefile
+++ b/drivers/mtd/maps/Makefile
@@ -44,7 +44,6 @@ obj-$(CONFIG_MTD_AUTCPU12) += autcpu12-nvram.o
44obj-$(CONFIG_MTD_EDB7312) += edb7312.o 44obj-$(CONFIG_MTD_EDB7312) += edb7312.o
45obj-$(CONFIG_MTD_IMPA7) += impa7.o 45obj-$(CONFIG_MTD_IMPA7) += impa7.o
46obj-$(CONFIG_MTD_FORTUNET) += fortunet.o 46obj-$(CONFIG_MTD_FORTUNET) += fortunet.o
47obj-$(CONFIG_MTD_REDWOOD) += redwood.o
48obj-$(CONFIG_MTD_UCLINUX) += uclinux.o 47obj-$(CONFIG_MTD_UCLINUX) += uclinux.o
49obj-$(CONFIG_MTD_NETtel) += nettel.o 48obj-$(CONFIG_MTD_NETtel) += nettel.o
50obj-$(CONFIG_MTD_SCB2_FLASH) += scb2_flash.o 49obj-$(CONFIG_MTD_SCB2_FLASH) += scb2_flash.o
diff --git a/drivers/mtd/maps/ixp4xx.c b/drivers/mtd/maps/ixp4xx.c
index e0a5e0426ea..1f9fde0dad3 100644
--- a/drivers/mtd/maps/ixp4xx.c
+++ b/drivers/mtd/maps/ixp4xx.c
@@ -118,7 +118,7 @@ static void ixp4xx_copy_from(struct map_info *map, void *to,
118 *dest++ = BYTE1(data); 118 *dest++ = BYTE1(data);
119 src += 2; 119 src += 2;
120 len -= 2; 120 len -= 2;
121 } 121 }
122 122
123 if (len > 0) 123 if (len > 0)
124 *dest++ = BYTE0(flash_read16(src)); 124 *dest++ = BYTE0(flash_read16(src));
@@ -185,6 +185,8 @@ static int ixp4xx_flash_probe(struct platform_device *dev)
185{ 185{
186 struct flash_platform_data *plat = dev->dev.platform_data; 186 struct flash_platform_data *plat = dev->dev.platform_data;
187 struct ixp4xx_flash_info *info; 187 struct ixp4xx_flash_info *info;
188 const char *part_type = NULL;
189 int nr_parts = 0;
188 int err = -1; 190 int err = -1;
189 191
190 if (!plat) 192 if (!plat)
@@ -218,9 +220,9 @@ static int ixp4xx_flash_probe(struct platform_device *dev)
218 */ 220 */
219 info->map.bankwidth = 2; 221 info->map.bankwidth = 2;
220 info->map.name = dev_name(&dev->dev); 222 info->map.name = dev_name(&dev->dev);
221 info->map.read = ixp4xx_read16, 223 info->map.read = ixp4xx_read16;
222 info->map.write = ixp4xx_probe_write16, 224 info->map.write = ixp4xx_probe_write16;
223 info->map.copy_from = ixp4xx_copy_from, 225 info->map.copy_from = ixp4xx_copy_from;
224 226
225 info->res = request_mem_region(dev->resource->start, 227 info->res = request_mem_region(dev->resource->start,
226 resource_size(dev->resource), 228 resource_size(dev->resource),
@@ -248,11 +250,28 @@ static int ixp4xx_flash_probe(struct platform_device *dev)
248 info->mtd->owner = THIS_MODULE; 250 info->mtd->owner = THIS_MODULE;
249 251
250 /* Use the fast version */ 252 /* Use the fast version */
251 info->map.write = ixp4xx_write16, 253 info->map.write = ixp4xx_write16;
254
255#ifdef CONFIG_MTD_PARTITIONS
256 nr_parts = parse_mtd_partitions(info->mtd, probes, &info->partitions,
257 dev->resource->start);
258#endif
259 if (nr_parts > 0) {
260 part_type = "dynamic";
261 } else {
262 info->partitions = plat->parts;
263 nr_parts = plat->nr_parts;
264 part_type = "static";
265 }
266 if (nr_parts == 0) {
267 printk(KERN_NOTICE "IXP4xx flash: no partition info "
268 "available, registering whole flash\n");
269 err = add_mtd_device(info->mtd);
270 } else {
271 printk(KERN_NOTICE "IXP4xx flash: using %s partition "
272 "definition\n", part_type);
273 err = add_mtd_partitions(info->mtd, info->partitions, nr_parts);
252 274
253 err = parse_mtd_partitions(info->mtd, probes, &info->partitions, dev->resource->start);
254 if (err > 0) {
255 err = add_mtd_partitions(info->mtd, info->partitions, err);
256 if(err) 275 if(err)
257 printk(KERN_ERR "Could not parse partitions\n"); 276 printk(KERN_ERR "Could not parse partitions\n");
258 } 277 }
diff --git a/drivers/mtd/maps/physmap.c b/drivers/mtd/maps/physmap.c
index 426461a5f0d..4c18b98a311 100644
--- a/drivers/mtd/maps/physmap.c
+++ b/drivers/mtd/maps/physmap.c
@@ -106,12 +106,12 @@ static int physmap_flash_probe(struct platform_device *dev)
106 106
107 for (i = 0; i < dev->num_resources; i++) { 107 for (i = 0; i < dev->num_resources; i++) {
108 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n", 108 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
109 (unsigned long long)(dev->resource[i].end - dev->resource[i].start + 1), 109 (unsigned long long)resource_size(&dev->resource[i]),
110 (unsigned long long)dev->resource[i].start); 110 (unsigned long long)dev->resource[i].start);
111 111
112 if (!devm_request_mem_region(&dev->dev, 112 if (!devm_request_mem_region(&dev->dev,
113 dev->resource[i].start, 113 dev->resource[i].start,
114 dev->resource[i].end - dev->resource[i].start + 1, 114 resource_size(&dev->resource[i]),
115 dev_name(&dev->dev))) { 115 dev_name(&dev->dev))) {
116 dev_err(&dev->dev, "Could not reserve memory region\n"); 116 dev_err(&dev->dev, "Could not reserve memory region\n");
117 err = -ENOMEM; 117 err = -ENOMEM;
@@ -120,7 +120,7 @@ static int physmap_flash_probe(struct platform_device *dev)
120 120
121 info->map[i].name = dev_name(&dev->dev); 121 info->map[i].name = dev_name(&dev->dev);
122 info->map[i].phys = dev->resource[i].start; 122 info->map[i].phys = dev->resource[i].start;
123 info->map[i].size = dev->resource[i].end - dev->resource[i].start + 1; 123 info->map[i].size = resource_size(&dev->resource[i]);
124 info->map[i].bankwidth = physmap_data->width; 124 info->map[i].bankwidth = physmap_data->width;
125 info->map[i].set_vpp = physmap_data->set_vpp; 125 info->map[i].set_vpp = physmap_data->set_vpp;
126 info->map[i].pfow_base = physmap_data->pfow_base; 126 info->map[i].pfow_base = physmap_data->pfow_base;
@@ -136,8 +136,12 @@ static int physmap_flash_probe(struct platform_device *dev)
136 simple_map_init(&info->map[i]); 136 simple_map_init(&info->map[i]);
137 137
138 probe_type = rom_probe_types; 138 probe_type = rom_probe_types;
139 for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++) 139 if (physmap_data->probe_type == NULL) {
140 info->mtd[i] = do_map_probe(*probe_type, &info->map[i]); 140 for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
141 info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
142 } else
143 info->mtd[i] = do_map_probe(physmap_data->probe_type, &info->map[i]);
144
141 if (info->mtd[i] == NULL) { 145 if (info->mtd[i] == NULL) {
142 dev_err(&dev->dev, "map_probe failed\n"); 146 dev_err(&dev->dev, "map_probe failed\n");
143 err = -ENXIO; 147 err = -ENXIO;
diff --git a/drivers/mtd/maps/physmap_of.c b/drivers/mtd/maps/physmap_of.c
index ba124baa646..6ac5f9f28ac 100644
--- a/drivers/mtd/maps/physmap_of.c
+++ b/drivers/mtd/maps/physmap_of.c
@@ -353,7 +353,7 @@ static int __devinit of_flash_probe(struct of_device *dev,
353 &info->parts, 0); 353 &info->parts, 0);
354 if (err < 0) { 354 if (err < 0) {
355 of_free_probes(part_probe_types); 355 of_free_probes(part_probe_types);
356 return err; 356 goto err_out;
357 } 357 }
358 of_free_probes(part_probe_types); 358 of_free_probes(part_probe_types);
359 359
@@ -361,14 +361,14 @@ static int __devinit of_flash_probe(struct of_device *dev,
361 if (err == 0) { 361 if (err == 0) {
362 err = of_mtd_parse_partitions(&dev->dev, dp, &info->parts); 362 err = of_mtd_parse_partitions(&dev->dev, dp, &info->parts);
363 if (err < 0) 363 if (err < 0)
364 return err; 364 goto err_out;
365 } 365 }
366#endif 366#endif
367 367
368 if (err == 0) { 368 if (err == 0) {
369 err = parse_obsolete_partitions(dev, info, dp); 369 err = parse_obsolete_partitions(dev, info, dp);
370 if (err < 0) 370 if (err < 0)
371 return err; 371 goto err_out;
372 } 372 }
373 373
374 if (err > 0) 374 if (err > 0)
diff --git a/drivers/mtd/maps/redwood.c b/drivers/mtd/maps/redwood.c
deleted file mode 100644
index d2c9db00db0..00000000000
--- a/drivers/mtd/maps/redwood.c
+++ /dev/null
@@ -1,131 +0,0 @@
1/*
2 * drivers/mtd/maps/redwood.c
3 *
4 * FLASH map for the IBM Redwood 4/5/6 boards.
5 *
6 * Author: MontaVista Software, Inc. <source@mvista.com>
7 *
8 * 2001-2003 (c) MontaVista, Software, Inc. This file is licensed under
9 * the terms of the GNU General Public License version 2. This program
10 * is licensed "as is" without any warranty of any kind, whether express
11 * or implied.
12 */
13
14#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18
19#include <linux/mtd/mtd.h>
20#include <linux/mtd/map.h>
21#include <linux/mtd/partitions.h>
22
23#include <asm/io.h>
24
25#define WINDOW_ADDR 0xffc00000
26#define WINDOW_SIZE 0x00400000
27
28#define RW_PART0_OF 0
29#define RW_PART0_SZ 0x10000
30#define RW_PART1_OF RW_PART0_SZ
31#define RW_PART1_SZ 0x200000 - 0x10000
32#define RW_PART2_OF 0x200000
33#define RW_PART2_SZ 0x10000
34#define RW_PART3_OF 0x210000
35#define RW_PART3_SZ 0x200000 - (0x10000 + 0x20000)
36#define RW_PART4_OF 0x3e0000
37#define RW_PART4_SZ 0x20000
38
39static struct mtd_partition redwood_flash_partitions[] = {
40 {
41 .name = "Redwood OpenBIOS Vital Product Data",
42 .offset = RW_PART0_OF,
43 .size = RW_PART0_SZ,
44 .mask_flags = MTD_WRITEABLE /* force read-only */
45 },
46 {
47 .name = "Redwood kernel",
48 .offset = RW_PART1_OF,
49 .size = RW_PART1_SZ
50 },
51 {
52 .name = "Redwood OpenBIOS non-volatile storage",
53 .offset = RW_PART2_OF,
54 .size = RW_PART2_SZ,
55 .mask_flags = MTD_WRITEABLE /* force read-only */
56 },
57 {
58 .name = "Redwood filesystem",
59 .offset = RW_PART3_OF,
60 .size = RW_PART3_SZ
61 },
62 {
63 .name = "Redwood OpenBIOS",
64 .offset = RW_PART4_OF,
65 .size = RW_PART4_SZ,
66 .mask_flags = MTD_WRITEABLE /* force read-only */
67 }
68};
69
70struct map_info redwood_flash_map = {
71 .name = "IBM Redwood",
72 .size = WINDOW_SIZE,
73 .bankwidth = 2,
74 .phys = WINDOW_ADDR,
75};
76
77
78#define NUM_REDWOOD_FLASH_PARTITIONS ARRAY_SIZE(redwood_flash_partitions)
79
80static struct mtd_info *redwood_mtd;
81
82static int __init init_redwood_flash(void)
83{
84 int err;
85
86 printk(KERN_NOTICE "redwood: flash mapping: %x at %x\n",
87 WINDOW_SIZE, WINDOW_ADDR);
88
89 redwood_flash_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
90
91 if (!redwood_flash_map.virt) {
92 printk("init_redwood_flash: failed to ioremap\n");
93 return -EIO;
94 }
95 simple_map_init(&redwood_flash_map);
96
97 redwood_mtd = do_map_probe("cfi_probe",&redwood_flash_map);
98
99 if (redwood_mtd) {
100 redwood_mtd->owner = THIS_MODULE;
101 err = add_mtd_partitions(redwood_mtd,
102 redwood_flash_partitions,
103 NUM_REDWOOD_FLASH_PARTITIONS);
104 if (err) {
105 printk("init_redwood_flash: add_mtd_partitions failed\n");
106 iounmap(redwood_flash_map.virt);
107 }
108 return err;
109
110 }
111
112 iounmap(redwood_flash_map.virt);
113 return -ENXIO;
114}
115
116static void __exit cleanup_redwood_flash(void)
117{
118 if (redwood_mtd) {
119 del_mtd_partitions(redwood_mtd);
120 /* moved iounmap after map_destroy - armin */
121 map_destroy(redwood_mtd);
122 iounmap((void *)redwood_flash_map.virt);
123 }
124}
125
126module_init(init_redwood_flash);
127module_exit(cleanup_redwood_flash);
128
129MODULE_LICENSE("GPL");
130MODULE_AUTHOR("MontaVista Software <source@mvista.com>");
131MODULE_DESCRIPTION("MTD map driver for the IBM Redwood reference boards");
diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c
index 03e19c1965c..62e68707b07 100644
--- a/drivers/mtd/mtd_blkdevs.c
+++ b/drivers/mtd/mtd_blkdevs.c
@@ -1,7 +1,21 @@
1/* 1/*
2 * (C) 2003 David Woodhouse <dwmw2@infradead.org> 2 * Interface to Linux block layer for MTD 'translation layers'.
3 * 3 *
4 * Interface to Linux 2.5 block layer for MTD 'translation layers'. 4 * Copyright © 2003-2010 David Woodhouse <dwmw2@infradead.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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
5 * 19 *
6 */ 20 */
7 21
@@ -15,6 +29,7 @@
15#include <linux/blkdev.h> 29#include <linux/blkdev.h>
16#include <linux/blkpg.h> 30#include <linux/blkpg.h>
17#include <linux/spinlock.h> 31#include <linux/spinlock.h>
32#include <linux/smp_lock.h>
18#include <linux/hdreg.h> 33#include <linux/hdreg.h>
19#include <linux/init.h> 34#include <linux/init.h>
20#include <linux/mutex.h> 35#include <linux/mutex.h>
@@ -73,14 +88,14 @@ static int do_blktrans_request(struct mtd_blktrans_ops *tr,
73 88
74 buf = req->buffer; 89 buf = req->buffer;
75 90
76 if (!blk_fs_request(req)) 91 if (req->cmd_type != REQ_TYPE_FS)
77 return -EIO; 92 return -EIO;
78 93
79 if (blk_rq_pos(req) + blk_rq_cur_sectors(req) > 94 if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
80 get_capacity(req->rq_disk)) 95 get_capacity(req->rq_disk))
81 return -EIO; 96 return -EIO;
82 97
83 if (blk_discard_rq(req)) 98 if (req->cmd_flags & REQ_DISCARD)
84 return tr->discard(dev, block, nsect); 99 return tr->discard(dev, block, nsect);
85 100
86 switch(rq_data_dir(req)) { 101 switch(rq_data_dir(req)) {
@@ -164,8 +179,9 @@ static int blktrans_open(struct block_device *bdev, fmode_t mode)
164 int ret; 179 int ret;
165 180
166 if (!dev) 181 if (!dev)
167 return -ERESTARTSYS; 182 return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/
168 183
184 lock_kernel();
169 mutex_lock(&dev->lock); 185 mutex_lock(&dev->lock);
170 186
171 if (!dev->mtd) { 187 if (!dev->mtd) {
@@ -182,6 +198,7 @@ static int blktrans_open(struct block_device *bdev, fmode_t mode)
182unlock: 198unlock:
183 mutex_unlock(&dev->lock); 199 mutex_unlock(&dev->lock);
184 blktrans_dev_put(dev); 200 blktrans_dev_put(dev);
201 unlock_kernel();
185 return ret; 202 return ret;
186} 203}
187 204
@@ -193,6 +210,7 @@ static int blktrans_release(struct gendisk *disk, fmode_t mode)
193 if (!dev) 210 if (!dev)
194 return ret; 211 return ret;
195 212
213 lock_kernel();
196 mutex_lock(&dev->lock); 214 mutex_lock(&dev->lock);
197 215
198 /* Release one reference, we sure its not the last one here*/ 216 /* Release one reference, we sure its not the last one here*/
@@ -205,6 +223,7 @@ static int blktrans_release(struct gendisk *disk, fmode_t mode)
205unlock: 223unlock:
206 mutex_unlock(&dev->lock); 224 mutex_unlock(&dev->lock);
207 blktrans_dev_put(dev); 225 blktrans_dev_put(dev);
226 unlock_kernel();
208 return ret; 227 return ret;
209} 228}
210 229
@@ -237,6 +256,7 @@ static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
237 if (!dev) 256 if (!dev)
238 return ret; 257 return ret;
239 258
259 lock_kernel();
240 mutex_lock(&dev->lock); 260 mutex_lock(&dev->lock);
241 261
242 if (!dev->mtd) 262 if (!dev->mtd)
@@ -245,11 +265,13 @@ static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
245 switch (cmd) { 265 switch (cmd) {
246 case BLKFLSBUF: 266 case BLKFLSBUF:
247 ret = dev->tr->flush ? dev->tr->flush(dev) : 0; 267 ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
268 break;
248 default: 269 default:
249 ret = -ENOTTY; 270 ret = -ENOTTY;
250 } 271 }
251unlock: 272unlock:
252 mutex_unlock(&dev->lock); 273 mutex_unlock(&dev->lock);
274 unlock_kernel();
253 blktrans_dev_put(dev); 275 blktrans_dev_put(dev);
254 return ret; 276 return ret;
255} 277}
@@ -258,7 +280,7 @@ static const struct block_device_operations mtd_blktrans_ops = {
258 .owner = THIS_MODULE, 280 .owner = THIS_MODULE,
259 .open = blktrans_open, 281 .open = blktrans_open,
260 .release = blktrans_release, 282 .release = blktrans_release,
261 .locked_ioctl = blktrans_ioctl, 283 .ioctl = blktrans_ioctl,
262 .getgeo = blktrans_getgeo, 284 .getgeo = blktrans_getgeo,
263}; 285};
264 286
@@ -409,13 +431,14 @@ int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
409 BUG(); 431 BUG();
410 } 432 }
411 433
412 /* Stop new requests to arrive */
413 del_gendisk(old->disk);
414
415 if (old->disk_attributes) 434 if (old->disk_attributes)
416 sysfs_remove_group(&disk_to_dev(old->disk)->kobj, 435 sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
417 old->disk_attributes); 436 old->disk_attributes);
418 437
438 /* Stop new requests to arrive */
439 del_gendisk(old->disk);
440
441
419 /* Stop the thread */ 442 /* Stop the thread */
420 kthread_stop(old->thread); 443 kthread_stop(old->thread);
421 444
diff --git a/drivers/mtd/mtdblock.c b/drivers/mtd/mtdblock.c
index e6edbec609f..1e74ad96104 100644
--- a/drivers/mtd/mtdblock.c
+++ b/drivers/mtd/mtdblock.c
@@ -1,8 +1,23 @@
1/* 1/*
2 * Direct MTD block device access 2 * Direct MTD block device access
3 * 3 *
4 * (C) 2000-2003 Nicolas Pitre <nico@fluxnic.net> 4 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
5 * (C) 1999-2003 David Woodhouse <dwmw2@infradead.org> 5 * Copyright © 2000-2003 Nicolas Pitre <nico@fluxnic.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
6 */ 21 */
7 22
8#include <linux/fs.h> 23#include <linux/fs.h>
diff --git a/drivers/mtd/mtdblock_ro.c b/drivers/mtd/mtdblock_ro.c
index d0d3f79f9d0..795a8c0a05b 100644
--- a/drivers/mtd/mtdblock_ro.c
+++ b/drivers/mtd/mtdblock_ro.c
@@ -1,7 +1,22 @@
1/* 1/*
2 * (C) 2003 David Woodhouse <dwmw2@infradead.org>
3 *
4 * Simple read-only (writable only for RAM) mtdblock driver 2 * Simple read-only (writable only for RAM) mtdblock driver
3 *
4 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
5 */ 20 */
6 21
7#include <linux/init.h> 22#include <linux/init.h>
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 91c8013cf0d..a825002123c 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -1,5 +1,19 @@
1/* 1/*
2 * Character-device access to raw MTD devices. 2 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3 * 17 *
4 */ 18 */
5 19
@@ -18,7 +32,7 @@
18#include <linux/mount.h> 32#include <linux/mount.h>
19 33
20#include <linux/mtd/mtd.h> 34#include <linux/mtd/mtd.h>
21#include <linux/mtd/compatmac.h> 35#include <linux/mtd/map.h>
22 36
23#include <asm/uaccess.h> 37#include <asm/uaccess.h>
24 38
@@ -675,6 +689,20 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
675 break; 689 break;
676 } 690 }
677 691
692 case MEMISLOCKED:
693 {
694 struct erase_info_user einfo;
695
696 if (copy_from_user(&einfo, argp, sizeof(einfo)))
697 return -EFAULT;
698
699 if (!mtd->is_locked)
700 ret = -EOPNOTSUPP;
701 else
702 ret = mtd->is_locked(mtd, einfo.start, einfo.length);
703 break;
704 }
705
678 /* Legacy interface */ 706 /* Legacy interface */
679 case MEMGETOOBSEL: 707 case MEMGETOOBSEL:
680 { 708 {
@@ -950,9 +978,34 @@ static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
950#ifdef CONFIG_MMU 978#ifdef CONFIG_MMU
951 struct mtd_file_info *mfi = file->private_data; 979 struct mtd_file_info *mfi = file->private_data;
952 struct mtd_info *mtd = mfi->mtd; 980 struct mtd_info *mtd = mfi->mtd;
981 struct map_info *map = mtd->priv;
982 unsigned long start;
983 unsigned long off;
984 u32 len;
985
986 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
987 off = vma->vm_pgoff << PAGE_SHIFT;
988 start = map->phys;
989 len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
990 start &= PAGE_MASK;
991 if ((vma->vm_end - vma->vm_start + off) > len)
992 return -EINVAL;
993
994 off += start;
995 vma->vm_pgoff = off >> PAGE_SHIFT;
996 vma->vm_flags |= VM_IO | VM_RESERVED;
997
998#ifdef pgprot_noncached
999 if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
1000 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1001#endif
1002 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1003 vma->vm_end - vma->vm_start,
1004 vma->vm_page_prot))
1005 return -EAGAIN;
953 1006
954 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM)
955 return 0; 1007 return 0;
1008 }
956 return -ENOSYS; 1009 return -ENOSYS;
957#else 1010#else
958 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS; 1011 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
diff --git a/drivers/mtd/mtdconcat.c b/drivers/mtd/mtdconcat.c
index 7e075621bbf..bf8de094310 100644
--- a/drivers/mtd/mtdconcat.c
+++ b/drivers/mtd/mtdconcat.c
@@ -1,11 +1,25 @@
1/* 1/*
2 * MTD device concatenation layer 2 * MTD device concatenation layer
3 * 3 *
4 * (C) 2002 Robert Kaiser <rkaiser@sysgo.de> 4 * Copyright © 2002 Robert Kaiser <rkaiser@sysgo.de>
5 * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
5 * 6 *
6 * NAND support by Christian Gan <cgan@iders.ca> 7 * NAND support by Christian Gan <cgan@iders.ca>
7 * 8 *
8 * This code is GPL 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
9 */ 23 */
10 24
11#include <linux/kernel.h> 25#include <linux/kernel.h>
@@ -540,10 +554,12 @@ static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
540 else 554 else
541 size = len; 555 size = len;
542 556
543 err = subdev->lock(subdev, ofs, size); 557 if (subdev->lock) {
544 558 err = subdev->lock(subdev, ofs, size);
545 if (err) 559 if (err)
546 break; 560 break;
561 } else
562 err = -EOPNOTSUPP;
547 563
548 len -= size; 564 len -= size;
549 if (len == 0) 565 if (len == 0)
@@ -578,10 +594,12 @@ static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
578 else 594 else
579 size = len; 595 size = len;
580 596
581 err = subdev->unlock(subdev, ofs, size); 597 if (subdev->unlock) {
582 598 err = subdev->unlock(subdev, ofs, size);
583 if (err) 599 if (err)
584 break; 600 break;
601 } else
602 err = -EOPNOTSUPP;
585 603
586 len -= size; 604 len -= size;
587 if (len == 0) 605 if (len == 0)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index a1b8b70d2d0..527cebf58da 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -2,9 +2,23 @@
2 * Core registration and callback routines for MTD 2 * Core registration and callback routines for MTD
3 * drivers and users. 3 * drivers and users.
4 * 4 *
5 * bdi bits are: 5 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
6 * Copyright © 2006 Red Hat, Inc. All Rights Reserved. 6 * Copyright © 2006 Red Hat UK Limited
7 * Written by David Howells (dhowells@redhat.com) 7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
8 */ 22 */
9 23
10#include <linux/module.h> 24#include <linux/module.h>
@@ -17,7 +31,6 @@
17#include <linux/err.h> 31#include <linux/err.h>
18#include <linux/ioctl.h> 32#include <linux/ioctl.h>
19#include <linux/init.h> 33#include <linux/init.h>
20#include <linux/mtd/compatmac.h>
21#include <linux/proc_fs.h> 34#include <linux/proc_fs.h>
22#include <linux/idr.h> 35#include <linux/idr.h>
23#include <linux/backing-dev.h> 36#include <linux/backing-dev.h>
diff --git a/drivers/mtd/mtdoops.c b/drivers/mtd/mtdoops.c
index 328313c3dcc..1ee72f3f051 100644
--- a/drivers/mtd/mtdoops.c
+++ b/drivers/mtd/mtdoops.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * MTD Oops/Panic logger 2 * MTD Oops/Panic logger
3 * 3 *
4 * Copyright (C) 2007 Nokia Corporation. All rights reserved. 4 * Copyright © 2007 Nokia Corporation. All rights reserved.
5 * 5 *
6 * Author: Richard Purdie <rpurdie@openedhand.com> 6 * Author: Richard Purdie <rpurdie@openedhand.com>
7 * 7 *
diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index b8043a9ba32..dc655856887 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -1,12 +1,24 @@
1/* 1/*
2 * Simple MTD partitioning layer 2 * Simple MTD partitioning layer
3 * 3 *
4 * (C) 2000 Nicolas Pitre <nico@fluxnic.net> 4 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
5 * 7 *
6 * This code is GPL 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
7 * 21 *
8 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
9 * added support for read_oob, write_oob
10 */ 22 */
11 23
12#include <linux/module.h> 24#include <linux/module.h>
@@ -17,7 +29,6 @@
17#include <linux/kmod.h> 29#include <linux/kmod.h>
18#include <linux/mtd/mtd.h> 30#include <linux/mtd/mtd.h>
19#include <linux/mtd/partitions.h> 31#include <linux/mtd/partitions.h>
20#include <linux/mtd/compatmac.h>
21 32
22/* Our partition linked list */ 33/* Our partition linked list */
23static LIST_HEAD(mtd_partitions); 34static LIST_HEAD(mtd_partitions);
@@ -264,6 +275,14 @@ static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
264 return part->master->unlock(part->master, ofs + part->offset, len); 275 return part->master->unlock(part->master, ofs + part->offset, len);
265} 276}
266 277
278static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
279{
280 struct mtd_part *part = PART(mtd);
281 if ((len + ofs) > mtd->size)
282 return -EINVAL;
283 return part->master->is_locked(part->master, ofs + part->offset, len);
284}
285
267static void part_sync(struct mtd_info *mtd) 286static void part_sync(struct mtd_info *mtd)
268{ 287{
269 struct mtd_part *part = PART(mtd); 288 struct mtd_part *part = PART(mtd);
@@ -402,6 +421,8 @@ static struct mtd_part *add_one_partition(struct mtd_info *master,
402 slave->mtd.lock = part_lock; 421 slave->mtd.lock = part_lock;
403 if (master->unlock) 422 if (master->unlock)
404 slave->mtd.unlock = part_unlock; 423 slave->mtd.unlock = part_unlock;
424 if (master->is_locked)
425 slave->mtd.is_locked = part_is_locked;
405 if (master->block_isbad) 426 if (master->block_isbad)
406 slave->mtd.block_isbad = part_block_isbad; 427 slave->mtd.block_isbad = part_block_isbad;
407 if (master->block_markbad) 428 if (master->block_markbad)
diff --git a/drivers/mtd/mtdsuper.c b/drivers/mtd/mtdsuper.c
index bd9a443ccf6..38e2ab07e7a 100644
--- a/drivers/mtd/mtdsuper.c
+++ b/drivers/mtd/mtdsuper.c
@@ -1,6 +1,8 @@
1/* MTD-based superblock management 1/* MTD-based superblock management
2 * 2 *
3 * Copyright © 2001-2007 Red Hat, Inc. All Rights Reserved. 3 * Copyright © 2001-2007 Red Hat, Inc. All Rights Reserved.
4 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
5 *
4 * Written by: David Howells <dhowells@redhat.com> 6 * Written by: David Howells <dhowells@redhat.com>
5 * David Woodhouse <dwmw2@infradead.org> 7 * David Woodhouse <dwmw2@infradead.org>
6 * 8 *
diff --git a/drivers/mtd/nand/Kconfig b/drivers/mtd/nand/Kconfig
index 362d177efe1..8b4b67c8a39 100644
--- a/drivers/mtd/nand/Kconfig
+++ b/drivers/mtd/nand/Kconfig
@@ -37,7 +37,6 @@ config MTD_SM_COMMON
37 37
38config MTD_NAND_MUSEUM_IDS 38config MTD_NAND_MUSEUM_IDS
39 bool "Enable chip ids for obsolete ancient NAND devices" 39 bool "Enable chip ids for obsolete ancient NAND devices"
40 depends on MTD_NAND
41 default n 40 default n
42 help 41 help
43 Enable this option only when your board has first generation 42 Enable this option only when your board has first generation
@@ -61,6 +60,7 @@ config MTD_NAND_DENALI
61config MTD_NAND_DENALI_SCRATCH_REG_ADDR 60config MTD_NAND_DENALI_SCRATCH_REG_ADDR
62 hex "Denali NAND size scratch register address" 61 hex "Denali NAND size scratch register address"
63 default "0xFF108018" 62 default "0xFF108018"
63 depends on MTD_NAND_DENALI
64 help 64 help
65 Some platforms place the NAND chip size in a scratch register 65 Some platforms place the NAND chip size in a scratch register
66 because (some versions of) the driver aren't able to automatically 66 because (some versions of) the driver aren't able to automatically
@@ -101,13 +101,13 @@ config MTD_NAND_AMS_DELTA
101 101
102config MTD_NAND_OMAP2 102config MTD_NAND_OMAP2
103 tristate "NAND Flash device on OMAP2 and OMAP3" 103 tristate "NAND Flash device on OMAP2 and OMAP3"
104 depends on ARM && MTD_NAND && (ARCH_OMAP2 || ARCH_OMAP3) 104 depends on ARM && (ARCH_OMAP2 || ARCH_OMAP3)
105 help 105 help
106 Support for NAND flash on Texas Instruments OMAP2 and OMAP3 platforms. 106 Support for NAND flash on Texas Instruments OMAP2 and OMAP3 platforms.
107 107
108config MTD_NAND_OMAP_PREFETCH 108config MTD_NAND_OMAP_PREFETCH
109 bool "GPMC prefetch support for NAND Flash device" 109 bool "GPMC prefetch support for NAND Flash device"
110 depends on MTD_NAND && MTD_NAND_OMAP2 110 depends on MTD_NAND_OMAP2
111 default y 111 default y
112 help 112 help
113 The NAND device can be accessed for Read/Write using GPMC PREFETCH engine 113 The NAND device can be accessed for Read/Write using GPMC PREFETCH engine
@@ -146,7 +146,7 @@ config MTD_NAND_AU1550
146 146
147config MTD_NAND_BF5XX 147config MTD_NAND_BF5XX
148 tristate "Blackfin on-chip NAND Flash Controller driver" 148 tristate "Blackfin on-chip NAND Flash Controller driver"
149 depends on (BF54x || BF52x) && MTD_NAND 149 depends on BF54x || BF52x
150 help 150 help
151 This enables the Blackfin on-chip NAND flash controller 151 This enables the Blackfin on-chip NAND flash controller
152 152
@@ -236,7 +236,7 @@ config MTD_NAND_S3C2410_CLKSTOP
236 236
237config MTD_NAND_BCM_UMI 237config MTD_NAND_BCM_UMI
238 tristate "NAND Flash support for BCM Reference Boards" 238 tristate "NAND Flash support for BCM Reference Boards"
239 depends on ARCH_BCMRING && MTD_NAND 239 depends on ARCH_BCMRING
240 help 240 help
241 This enables the NAND flash controller on the BCM UMI block. 241 This enables the NAND flash controller on the BCM UMI block.
242 242
@@ -395,7 +395,7 @@ endchoice
395 395
396config MTD_NAND_PXA3xx 396config MTD_NAND_PXA3xx
397 tristate "Support for NAND flash devices on PXA3xx" 397 tristate "Support for NAND flash devices on PXA3xx"
398 depends on MTD_NAND && (PXA3xx || ARCH_MMP) 398 depends on PXA3xx || ARCH_MMP
399 help 399 help
400 This enables the driver for the NAND flash device found on 400 This enables the driver for the NAND flash device found on
401 PXA3xx processors 401 PXA3xx processors
@@ -409,18 +409,18 @@ config MTD_NAND_PXA3xx_BUILTIN
409 409
410config MTD_NAND_CM_X270 410config MTD_NAND_CM_X270
411 tristate "Support for NAND Flash on CM-X270 modules" 411 tristate "Support for NAND Flash on CM-X270 modules"
412 depends on MTD_NAND && MACH_ARMCORE 412 depends on MACH_ARMCORE
413 413
414config MTD_NAND_PASEMI 414config MTD_NAND_PASEMI
415 tristate "NAND support for PA Semi PWRficient" 415 tristate "NAND support for PA Semi PWRficient"
416 depends on MTD_NAND && PPC_PASEMI 416 depends on PPC_PASEMI
417 help 417 help
418 Enables support for NAND Flash interface on PA Semi PWRficient 418 Enables support for NAND Flash interface on PA Semi PWRficient
419 based boards 419 based boards
420 420
421config MTD_NAND_TMIO 421config MTD_NAND_TMIO
422 tristate "NAND Flash device on Toshiba Mobile IO Controller" 422 tristate "NAND Flash device on Toshiba Mobile IO Controller"
423 depends on MTD_NAND && MFD_TMIO 423 depends on MFD_TMIO
424 help 424 help
425 Support for NAND flash connected to a Toshiba Mobile IO 425 Support for NAND flash connected to a Toshiba Mobile IO
426 Controller in some PDAs, including the Sharp SL6000x. 426 Controller in some PDAs, including the Sharp SL6000x.
@@ -434,7 +434,6 @@ config MTD_NAND_NANDSIM
434 434
435config MTD_NAND_PLATFORM 435config MTD_NAND_PLATFORM
436 tristate "Support for generic platform NAND driver" 436 tristate "Support for generic platform NAND driver"
437 depends on MTD_NAND
438 help 437 help
439 This implements a generic NAND driver for on-SOC platform 438 This implements a generic NAND driver for on-SOC platform
440 devices. You will need to provide platform-specific functions 439 devices. You will need to provide platform-specific functions
@@ -442,14 +441,14 @@ config MTD_NAND_PLATFORM
442 441
443config MTD_ALAUDA 442config MTD_ALAUDA
444 tristate "MTD driver for Olympus MAUSB-10 and Fujifilm DPC-R1" 443 tristate "MTD driver for Olympus MAUSB-10 and Fujifilm DPC-R1"
445 depends on MTD_NAND && USB 444 depends on USB
446 help 445 help
447 These two (and possibly other) Alauda-based cardreaders for 446 These two (and possibly other) Alauda-based cardreaders for
448 SmartMedia and xD allow raw flash access. 447 SmartMedia and xD allow raw flash access.
449 448
450config MTD_NAND_ORION 449config MTD_NAND_ORION
451 tristate "NAND Flash support for Marvell Orion SoC" 450 tristate "NAND Flash support for Marvell Orion SoC"
452 depends on PLAT_ORION && MTD_NAND 451 depends on PLAT_ORION
453 help 452 help
454 This enables the NAND flash controller on Orion machines. 453 This enables the NAND flash controller on Orion machines.
455 454
@@ -458,7 +457,7 @@ config MTD_NAND_ORION
458 457
459config MTD_NAND_FSL_ELBC 458config MTD_NAND_FSL_ELBC
460 tristate "NAND support for Freescale eLBC controllers" 459 tristate "NAND support for Freescale eLBC controllers"
461 depends on MTD_NAND && PPC_OF 460 depends on PPC_OF
462 help 461 help
463 Various Freescale chips, including the 8313, include a NAND Flash 462 Various Freescale chips, including the 8313, include a NAND Flash
464 Controller Module with built-in hardware ECC capabilities. 463 Controller Module with built-in hardware ECC capabilities.
@@ -467,7 +466,7 @@ config MTD_NAND_FSL_ELBC
467 466
468config MTD_NAND_FSL_UPM 467config MTD_NAND_FSL_UPM
469 tristate "Support for NAND on Freescale UPM" 468 tristate "Support for NAND on Freescale UPM"
470 depends on MTD_NAND && (PPC_83xx || PPC_85xx) 469 depends on PPC_83xx || PPC_85xx
471 select FSL_LBC 470 select FSL_LBC
472 help 471 help
473 Enables support for NAND Flash chips wired onto Freescale PowerPC 472 Enables support for NAND Flash chips wired onto Freescale PowerPC
@@ -482,7 +481,7 @@ config MTD_NAND_MPC5121_NFC
482 481
483config MTD_NAND_MXC 482config MTD_NAND_MXC
484 tristate "MXC NAND support" 483 tristate "MXC NAND support"
485 depends on ARCH_MX2 || ARCH_MX25 || ARCH_MX3 484 depends on ARCH_MX2 || ARCH_MX25 || ARCH_MX3 || ARCH_MX51
486 help 485 help
487 This enables the driver for the NAND flash controller on the 486 This enables the driver for the NAND flash controller on the
488 MXC processors. 487 MXC processors.
@@ -495,7 +494,7 @@ config MTD_NAND_NOMADIK
495 494
496config MTD_NAND_SH_FLCTL 495config MTD_NAND_SH_FLCTL
497 tristate "Support for NAND on Renesas SuperH FLCTL" 496 tristate "Support for NAND on Renesas SuperH FLCTL"
498 depends on MTD_NAND && (SUPERH || ARCH_SHMOBILE) 497 depends on SUPERH || ARCH_SHMOBILE
499 help 498 help
500 Several Renesas SuperH CPU has FLCTL. This option enables support 499 Several Renesas SuperH CPU has FLCTL. This option enables support
501 for NAND Flash using FLCTL. 500 for NAND Flash using FLCTL.
@@ -515,7 +514,7 @@ config MTD_NAND_TXX9NDFMC
515 514
516config MTD_NAND_SOCRATES 515config MTD_NAND_SOCRATES
517 tristate "Support for NAND on Socrates board" 516 tristate "Support for NAND on Socrates board"
518 depends on MTD_NAND && SOCRATES 517 depends on SOCRATES
519 help 518 help
520 Enables support for NAND Flash chips wired onto Socrates board. 519 Enables support for NAND Flash chips wired onto Socrates board.
521 520
diff --git a/drivers/mtd/nand/atmel_nand.c b/drivers/mtd/nand/atmel_nand.c
index 04d30887ca7..ccce0f03b5d 100644
--- a/drivers/mtd/nand/atmel_nand.c
+++ b/drivers/mtd/nand/atmel_nand.c
@@ -364,7 +364,7 @@ static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
364 } 364 }
365} 365}
366 366
367#ifdef CONFIG_MTD_PARTITIONS 367#ifdef CONFIG_MTD_CMDLINE_PARTS
368static const char *part_probes[] = { "cmdlinepart", NULL }; 368static const char *part_probes[] = { "cmdlinepart", NULL };
369#endif 369#endif
370 370
diff --git a/drivers/mtd/nand/bf5xx_nand.c b/drivers/mtd/nand/bf5xx_nand.c
index 2974995e194..a382e3dd0a5 100644
--- a/drivers/mtd/nand/bf5xx_nand.c
+++ b/drivers/mtd/nand/bf5xx_nand.c
@@ -20,9 +20,6 @@
20 * - DMA supported in ECC_HW 20 * - DMA supported in ECC_HW
21 * - YAFFS tested as rootfs in both ECC_HW and ECC_SW 21 * - YAFFS tested as rootfs in both ECC_HW and ECC_SW
22 * 22 *
23 * TODO:
24 * Enable JFFS2 over NAND as rootfs
25 *
26 * This program is free software; you can redistribute it and/or modify 23 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by 24 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or 25 * the Free Software Foundation; either version 2 of the License, or
@@ -206,7 +203,7 @@ static void bf5xx_nand_hwcontrol(struct mtd_info *mtd, int cmd,
206 203
207 if (ctrl & NAND_CLE) 204 if (ctrl & NAND_CLE)
208 bfin_write_NFC_CMD(cmd); 205 bfin_write_NFC_CMD(cmd);
209 else 206 else if (ctrl & NAND_ALE)
210 bfin_write_NFC_ADDR(cmd); 207 bfin_write_NFC_ADDR(cmd);
211 SSYNC(); 208 SSYNC();
212} 209}
@@ -218,9 +215,9 @@ static void bf5xx_nand_hwcontrol(struct mtd_info *mtd, int cmd,
218 */ 215 */
219static int bf5xx_nand_devready(struct mtd_info *mtd) 216static int bf5xx_nand_devready(struct mtd_info *mtd)
220{ 217{
221 unsigned short val = bfin_read_NFC_IRQSTAT(); 218 unsigned short val = bfin_read_NFC_STAT();
222 219
223 if ((val & NBUSYIRQ) == NBUSYIRQ) 220 if ((val & NBUSY) == NBUSY)
224 return 1; 221 return 1;
225 else 222 else
226 return 0; 223 return 0;
@@ -317,18 +314,16 @@ static int bf5xx_nand_correct_data_256(struct mtd_info *mtd, u_char *dat,
317static int bf5xx_nand_correct_data(struct mtd_info *mtd, u_char *dat, 314static int bf5xx_nand_correct_data(struct mtd_info *mtd, u_char *dat,
318 u_char *read_ecc, u_char *calc_ecc) 315 u_char *read_ecc, u_char *calc_ecc)
319{ 316{
320 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd); 317 struct nand_chip *chip = mtd->priv;
321 struct bf5xx_nand_platform *plat = info->platform;
322 unsigned short page_size = (plat->page_size ? 512 : 256);
323 int ret; 318 int ret;
324 319
325 ret = bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc); 320 ret = bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
326 321
327 /* If page size is 512, correct second 256 bytes */ 322 /* If ecc size is 512, correct second 256 bytes */
328 if (page_size == 512) { 323 if (chip->ecc.size == 512) {
329 dat += 256; 324 dat += 256;
330 read_ecc += 8; 325 read_ecc += 3;
331 calc_ecc += 8; 326 calc_ecc += 3;
332 ret |= bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc); 327 ret |= bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
333 } 328 }
334 329
@@ -344,13 +339,12 @@ static int bf5xx_nand_calculate_ecc(struct mtd_info *mtd,
344 const u_char *dat, u_char *ecc_code) 339 const u_char *dat, u_char *ecc_code)
345{ 340{
346 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd); 341 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
347 struct bf5xx_nand_platform *plat = info->platform; 342 struct nand_chip *chip = mtd->priv;
348 u16 page_size = (plat->page_size ? 512 : 256);
349 u16 ecc0, ecc1; 343 u16 ecc0, ecc1;
350 u32 code[2]; 344 u32 code[2];
351 u8 *p; 345 u8 *p;
352 346
353 /* first 4 bytes ECC code for 256 page size */ 347 /* first 3 bytes ECC code for 256 page size */
354 ecc0 = bfin_read_NFC_ECC0(); 348 ecc0 = bfin_read_NFC_ECC0();
355 ecc1 = bfin_read_NFC_ECC1(); 349 ecc1 = bfin_read_NFC_ECC1();
356 350
@@ -358,12 +352,11 @@ static int bf5xx_nand_calculate_ecc(struct mtd_info *mtd,
358 352
359 dev_dbg(info->device, "returning ecc 0x%08x\n", code[0]); 353 dev_dbg(info->device, "returning ecc 0x%08x\n", code[0]);
360 354
361 /* first 3 bytes in ecc_code for 256 page size */
362 p = (u8 *) code; 355 p = (u8 *) code;
363 memcpy(ecc_code, p, 3); 356 memcpy(ecc_code, p, 3);
364 357
365 /* second 4 bytes ECC code for 512 page size */ 358 /* second 3 bytes ECC code for 512 ecc size */
366 if (page_size == 512) { 359 if (chip->ecc.size == 512) {
367 ecc0 = bfin_read_NFC_ECC2(); 360 ecc0 = bfin_read_NFC_ECC2();
368 ecc1 = bfin_read_NFC_ECC3(); 361 ecc1 = bfin_read_NFC_ECC3();
369 code[1] = (ecc0 & 0x7ff) | ((ecc1 & 0x7ff) << 11); 362 code[1] = (ecc0 & 0x7ff) | ((ecc1 & 0x7ff) << 11);
@@ -483,8 +476,7 @@ static void bf5xx_nand_dma_rw(struct mtd_info *mtd,
483 uint8_t *buf, int is_read) 476 uint8_t *buf, int is_read)
484{ 477{
485 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd); 478 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
486 struct bf5xx_nand_platform *plat = info->platform; 479 struct nand_chip *chip = mtd->priv;
487 unsigned short page_size = (plat->page_size ? 512 : 256);
488 unsigned short val; 480 unsigned short val;
489 481
490 dev_dbg(info->device, " mtd->%p, buf->%p, is_read %d\n", 482 dev_dbg(info->device, " mtd->%p, buf->%p, is_read %d\n",
@@ -498,10 +490,10 @@ static void bf5xx_nand_dma_rw(struct mtd_info *mtd,
498 */ 490 */
499 if (is_read) 491 if (is_read)
500 invalidate_dcache_range((unsigned int)buf, 492 invalidate_dcache_range((unsigned int)buf,
501 (unsigned int)(buf + page_size)); 493 (unsigned int)(buf + chip->ecc.size));
502 else 494 else
503 flush_dcache_range((unsigned int)buf, 495 flush_dcache_range((unsigned int)buf,
504 (unsigned int)(buf + page_size)); 496 (unsigned int)(buf + chip->ecc.size));
505 497
506 /* 498 /*
507 * This register must be written before each page is 499 * This register must be written before each page is
@@ -510,6 +502,8 @@ static void bf5xx_nand_dma_rw(struct mtd_info *mtd,
510 */ 502 */
511 bfin_write_NFC_RST(ECC_RST); 503 bfin_write_NFC_RST(ECC_RST);
512 SSYNC(); 504 SSYNC();
505 while (bfin_read_NFC_RST() & ECC_RST)
506 cpu_relax();
513 507
514 disable_dma(CH_NFC); 508 disable_dma(CH_NFC);
515 clear_dma_irqstat(CH_NFC); 509 clear_dma_irqstat(CH_NFC);
@@ -520,13 +514,13 @@ static void bf5xx_nand_dma_rw(struct mtd_info *mtd,
520 514
521 /* The DMAs have different size on BF52x and BF54x */ 515 /* The DMAs have different size on BF52x and BF54x */
522#ifdef CONFIG_BF52x 516#ifdef CONFIG_BF52x
523 set_dma_x_count(CH_NFC, (page_size >> 1)); 517 set_dma_x_count(CH_NFC, (chip->ecc.size >> 1));
524 set_dma_x_modify(CH_NFC, 2); 518 set_dma_x_modify(CH_NFC, 2);
525 val = DI_EN | WDSIZE_16; 519 val = DI_EN | WDSIZE_16;
526#endif 520#endif
527 521
528#ifdef CONFIG_BF54x 522#ifdef CONFIG_BF54x
529 set_dma_x_count(CH_NFC, (page_size >> 2)); 523 set_dma_x_count(CH_NFC, (chip->ecc.size >> 2));
530 set_dma_x_modify(CH_NFC, 4); 524 set_dma_x_modify(CH_NFC, 4);
531 val = DI_EN | WDSIZE_32; 525 val = DI_EN | WDSIZE_32;
532#endif 526#endif
@@ -548,12 +542,11 @@ static void bf5xx_nand_dma_read_buf(struct mtd_info *mtd,
548 uint8_t *buf, int len) 542 uint8_t *buf, int len)
549{ 543{
550 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd); 544 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
551 struct bf5xx_nand_platform *plat = info->platform; 545 struct nand_chip *chip = mtd->priv;
552 unsigned short page_size = (plat->page_size ? 512 : 256);
553 546
554 dev_dbg(info->device, "mtd->%p, buf->%p, int %d\n", mtd, buf, len); 547 dev_dbg(info->device, "mtd->%p, buf->%p, int %d\n", mtd, buf, len);
555 548
556 if (len == page_size) 549 if (len == chip->ecc.size)
557 bf5xx_nand_dma_rw(mtd, buf, 1); 550 bf5xx_nand_dma_rw(mtd, buf, 1);
558 else 551 else
559 bf5xx_nand_read_buf(mtd, buf, len); 552 bf5xx_nand_read_buf(mtd, buf, len);
@@ -563,17 +556,32 @@ static void bf5xx_nand_dma_write_buf(struct mtd_info *mtd,
563 const uint8_t *buf, int len) 556 const uint8_t *buf, int len)
564{ 557{
565 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd); 558 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
566 struct bf5xx_nand_platform *plat = info->platform; 559 struct nand_chip *chip = mtd->priv;
567 unsigned short page_size = (plat->page_size ? 512 : 256);
568 560
569 dev_dbg(info->device, "mtd->%p, buf->%p, len %d\n", mtd, buf, len); 561 dev_dbg(info->device, "mtd->%p, buf->%p, len %d\n", mtd, buf, len);
570 562
571 if (len == page_size) 563 if (len == chip->ecc.size)
572 bf5xx_nand_dma_rw(mtd, (uint8_t *)buf, 0); 564 bf5xx_nand_dma_rw(mtd, (uint8_t *)buf, 0);
573 else 565 else
574 bf5xx_nand_write_buf(mtd, buf, len); 566 bf5xx_nand_write_buf(mtd, buf, len);
575} 567}
576 568
569static int bf5xx_nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
570 uint8_t *buf, int page)
571{
572 bf5xx_nand_read_buf(mtd, buf, mtd->writesize);
573 bf5xx_nand_read_buf(mtd, chip->oob_poi, mtd->oobsize);
574
575 return 0;
576}
577
578static void bf5xx_nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
579 const uint8_t *buf)
580{
581 bf5xx_nand_write_buf(mtd, buf, mtd->writesize);
582 bf5xx_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
583}
584
577/* 585/*
578 * System initialization functions 586 * System initialization functions
579 */ 587 */
@@ -627,15 +635,14 @@ static int bf5xx_nand_hw_init(struct bf5xx_nand_info *info)
627 635
628 /* setup NFC_CTL register */ 636 /* setup NFC_CTL register */
629 dev_info(info->device, 637 dev_info(info->device,
630 "page_size=%d, data_width=%d, wr_dly=%d, rd_dly=%d\n", 638 "data_width=%d, wr_dly=%d, rd_dly=%d\n",
631 (plat->page_size ? 512 : 256),
632 (plat->data_width ? 16 : 8), 639 (plat->data_width ? 16 : 8),
633 plat->wr_dly, plat->rd_dly); 640 plat->wr_dly, plat->rd_dly);
634 641
635 val = (plat->page_size << NFC_PG_SIZE_OFFSET) | 642 val = (1 << NFC_PG_SIZE_OFFSET) |
636 (plat->data_width << NFC_NWIDTH_OFFSET) | 643 (plat->data_width << NFC_NWIDTH_OFFSET) |
637 (plat->rd_dly << NFC_RDDLY_OFFSET) | 644 (plat->rd_dly << NFC_RDDLY_OFFSET) |
638 (plat->rd_dly << NFC_WRDLY_OFFSET); 645 (plat->wr_dly << NFC_WRDLY_OFFSET);
639 dev_dbg(info->device, "NFC_CTL is 0x%04x\n", val); 646 dev_dbg(info->device, "NFC_CTL is 0x%04x\n", val);
640 647
641 bfin_write_NFC_CTL(val); 648 bfin_write_NFC_CTL(val);
@@ -698,6 +705,33 @@ static int __devexit bf5xx_nand_remove(struct platform_device *pdev)
698 return 0; 705 return 0;
699} 706}
700 707
708static int bf5xx_nand_scan(struct mtd_info *mtd)
709{
710 struct nand_chip *chip = mtd->priv;
711 int ret;
712
713 ret = nand_scan_ident(mtd, 1);
714 if (ret)
715 return ret;
716
717 if (hardware_ecc) {
718 /*
719 * for nand with page size > 512B, think it as several sections with 512B
720 */
721 if (likely(mtd->writesize >= 512)) {
722 chip->ecc.size = 512;
723 chip->ecc.bytes = 6;
724 } else {
725 chip->ecc.size = 256;
726 chip->ecc.bytes = 3;
727 bfin_write_NFC_CTL(bfin_read_NFC_CTL() & ~(1 << NFC_PG_SIZE_OFFSET));
728 SSYNC();
729 }
730 }
731
732 return nand_scan_tail(mtd);
733}
734
701/* 735/*
702 * bf5xx_nand_probe 736 * bf5xx_nand_probe
703 * 737 *
@@ -783,27 +817,20 @@ static int __devinit bf5xx_nand_probe(struct platform_device *pdev)
783 chip->badblock_pattern = &bootrom_bbt; 817 chip->badblock_pattern = &bootrom_bbt;
784 chip->ecc.layout = &bootrom_ecclayout; 818 chip->ecc.layout = &bootrom_ecclayout;
785#endif 819#endif
786
787 if (plat->page_size == NFC_PG_SIZE_256) {
788 chip->ecc.bytes = 3;
789 chip->ecc.size = 256;
790 } else if (plat->page_size == NFC_PG_SIZE_512) {
791 chip->ecc.bytes = 6;
792 chip->ecc.size = 512;
793 }
794
795 chip->read_buf = bf5xx_nand_dma_read_buf; 820 chip->read_buf = bf5xx_nand_dma_read_buf;
796 chip->write_buf = bf5xx_nand_dma_write_buf; 821 chip->write_buf = bf5xx_nand_dma_write_buf;
797 chip->ecc.calculate = bf5xx_nand_calculate_ecc; 822 chip->ecc.calculate = bf5xx_nand_calculate_ecc;
798 chip->ecc.correct = bf5xx_nand_correct_data; 823 chip->ecc.correct = bf5xx_nand_correct_data;
799 chip->ecc.mode = NAND_ECC_HW; 824 chip->ecc.mode = NAND_ECC_HW;
800 chip->ecc.hwctl = bf5xx_nand_enable_hwecc; 825 chip->ecc.hwctl = bf5xx_nand_enable_hwecc;
826 chip->ecc.read_page_raw = bf5xx_nand_read_page_raw;
827 chip->ecc.write_page_raw = bf5xx_nand_write_page_raw;
801 } else { 828 } else {
802 chip->ecc.mode = NAND_ECC_SOFT; 829 chip->ecc.mode = NAND_ECC_SOFT;
803 } 830 }
804 831
805 /* scan hardware nand chip and setup mtd info data struct */ 832 /* scan hardware nand chip and setup mtd info data struct */
806 if (nand_scan(mtd, 1)) { 833 if (bf5xx_nand_scan(mtd)) {
807 err = -ENXIO; 834 err = -ENXIO;
808 goto out_err_nand_scan; 835 goto out_err_nand_scan;
809 } 836 }
diff --git a/drivers/mtd/nand/davinci_nand.c b/drivers/mtd/nand/davinci_nand.c
index 9c9d893affe..2ac7367afe7 100644
--- a/drivers/mtd/nand/davinci_nand.c
+++ b/drivers/mtd/nand/davinci_nand.c
@@ -311,7 +311,9 @@ static int nand_davinci_correct_4bit(struct mtd_info *mtd,
311 unsigned short ecc10[8]; 311 unsigned short ecc10[8];
312 unsigned short *ecc16; 312 unsigned short *ecc16;
313 u32 syndrome[4]; 313 u32 syndrome[4];
314 u32 ecc_state;
314 unsigned num_errors, corrected; 315 unsigned num_errors, corrected;
316 unsigned long timeo = jiffies + msecs_to_jiffies(100);
315 317
316 /* All bytes 0xff? It's an erased page; ignore its ECC. */ 318 /* All bytes 0xff? It's an erased page; ignore its ECC. */
317 for (i = 0; i < 10; i++) { 319 for (i = 0; i < 10; i++) {
@@ -361,6 +363,21 @@ compare:
361 */ 363 */
362 davinci_nand_writel(info, NANDFCR_OFFSET, 364 davinci_nand_writel(info, NANDFCR_OFFSET,
363 davinci_nand_readl(info, NANDFCR_OFFSET) | BIT(13)); 365 davinci_nand_readl(info, NANDFCR_OFFSET) | BIT(13));
366
367 /*
368 * ECC_STATE field reads 0x3 (Error correction complete) immediately
369 * after setting the 4BITECC_ADD_CALC_START bit. So if you immediately
370 * begin trying to poll for the state, you may fall right out of your
371 * loop without any of the correction calculations having taken place.
372 * The recommendation from the hardware team is to wait till ECC_STATE
373 * reads less than 4, which means ECC HW has entered correction state.
374 */
375 do {
376 ecc_state = (davinci_nand_readl(info,
377 NANDFSR_OFFSET) >> 8) & 0x0f;
378 cpu_relax();
379 } while ((ecc_state < 4) && time_before(jiffies, timeo));
380
364 for (;;) { 381 for (;;) {
365 u32 fsr = davinci_nand_readl(info, NANDFSR_OFFSET); 382 u32 fsr = davinci_nand_readl(info, NANDFSR_OFFSET);
366 383
diff --git a/drivers/mtd/nand/denali.c b/drivers/mtd/nand/denali.c
index 3dfda9cc677..618fb42b86b 100644
--- a/drivers/mtd/nand/denali.c
+++ b/drivers/mtd/nand/denali.c
@@ -21,6 +21,7 @@
21#include <linux/delay.h> 21#include <linux/delay.h>
22#include <linux/wait.h> 22#include <linux/wait.h>
23#include <linux/mutex.h> 23#include <linux/mutex.h>
24#include <linux/slab.h>
24#include <linux/pci.h> 25#include <linux/pci.h>
25#include <linux/mtd/mtd.h> 26#include <linux/mtd/mtd.h>
26#include <linux/module.h> 27#include <linux/module.h>
@@ -29,15 +30,15 @@
29 30
30MODULE_LICENSE("GPL"); 31MODULE_LICENSE("GPL");
31 32
32/* We define a module parameter that allows the user to override 33/* We define a module parameter that allows the user to override
33 * the hardware and decide what timing mode should be used. 34 * the hardware and decide what timing mode should be used.
34 */ 35 */
35#define NAND_DEFAULT_TIMINGS -1 36#define NAND_DEFAULT_TIMINGS -1
36 37
37static int onfi_timing_mode = NAND_DEFAULT_TIMINGS; 38static int onfi_timing_mode = NAND_DEFAULT_TIMINGS;
38module_param(onfi_timing_mode, int, S_IRUGO); 39module_param(onfi_timing_mode, int, S_IRUGO);
39MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting. -1 indicates" 40MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting."
40 " use default timings"); 41 " -1 indicates use default timings");
41 42
42#define DENALI_NAND_NAME "denali-nand" 43#define DENALI_NAND_NAME "denali-nand"
43 44
@@ -54,13 +55,13 @@ MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting. -1 indicates
54 INTR_STATUS0__RST_COMP | \ 55 INTR_STATUS0__RST_COMP | \
55 INTR_STATUS0__ERASE_COMP) 56 INTR_STATUS0__ERASE_COMP)
56 57
57/* indicates whether or not the internal value for the flash bank is 58/* indicates whether or not the internal value for the flash bank is
58 valid or not */ 59 valid or not */
59#define CHIP_SELECT_INVALID -1 60#define CHIP_SELECT_INVALID -1
60 61
61#define SUPPORT_8BITECC 1 62#define SUPPORT_8BITECC 1
62 63
63/* This macro divides two integers and rounds fractional values up 64/* This macro divides two integers and rounds fractional values up
64 * to the nearest integer value. */ 65 * to the nearest integer value. */
65#define CEIL_DIV(X, Y) (((X)%(Y)) ? ((X)/(Y)+1) : ((X)/(Y))) 66#define CEIL_DIV(X, Y) (((X)%(Y)) ? ((X)/(Y)+1) : ((X)/(Y)))
66 67
@@ -83,7 +84,7 @@ MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting. -1 indicates
83#define ADDR_CYCLE 1 84#define ADDR_CYCLE 1
84#define STATUS_CYCLE 2 85#define STATUS_CYCLE 2
85 86
86/* this is a helper macro that allows us to 87/* this is a helper macro that allows us to
87 * format the bank into the proper bits for the controller */ 88 * format the bank into the proper bits for the controller */
88#define BANK(x) ((x) << 24) 89#define BANK(x) ((x) << 24)
89 90
@@ -95,59 +96,64 @@ static const struct pci_device_id denali_pci_ids[] = {
95}; 96};
96 97
97 98
98/* these are static lookup tables that give us easy access to 99/* these are static lookup tables that give us easy access to
99 registers in the NAND controller. 100 registers in the NAND controller.
100 */ 101 */
101static const uint32_t intr_status_addresses[4] = {INTR_STATUS0, 102static const uint32_t intr_status_addresses[4] = {INTR_STATUS0,
102 INTR_STATUS1, 103 INTR_STATUS1,
103 INTR_STATUS2, 104 INTR_STATUS2,
104 INTR_STATUS3}; 105 INTR_STATUS3};
105 106
106static const uint32_t device_reset_banks[4] = {DEVICE_RESET__BANK0, 107static const uint32_t device_reset_banks[4] = {DEVICE_RESET__BANK0,
107 DEVICE_RESET__BANK1, 108 DEVICE_RESET__BANK1,
108 DEVICE_RESET__BANK2, 109 DEVICE_RESET__BANK2,
109 DEVICE_RESET__BANK3}; 110 DEVICE_RESET__BANK3};
110 111
111static const uint32_t operation_timeout[4] = {INTR_STATUS0__TIME_OUT, 112static const uint32_t operation_timeout[4] = {INTR_STATUS0__TIME_OUT,
112 INTR_STATUS1__TIME_OUT, 113 INTR_STATUS1__TIME_OUT,
113 INTR_STATUS2__TIME_OUT, 114 INTR_STATUS2__TIME_OUT,
114 INTR_STATUS3__TIME_OUT}; 115 INTR_STATUS3__TIME_OUT};
115 116
116static const uint32_t reset_complete[4] = {INTR_STATUS0__RST_COMP, 117static const uint32_t reset_complete[4] = {INTR_STATUS0__RST_COMP,
117 INTR_STATUS1__RST_COMP, 118 INTR_STATUS1__RST_COMP,
118 INTR_STATUS2__RST_COMP, 119 INTR_STATUS2__RST_COMP,
119 INTR_STATUS3__RST_COMP}; 120 INTR_STATUS3__RST_COMP};
120 121
121/* specifies the debug level of the driver */ 122/* specifies the debug level of the driver */
122static int nand_debug_level = 0; 123static int nand_debug_level;
123 124
124/* forward declarations */ 125/* forward declarations */
125static void clear_interrupts(struct denali_nand_info *denali); 126static void clear_interrupts(struct denali_nand_info *denali);
126static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask); 127static uint32_t wait_for_irq(struct denali_nand_info *denali,
127static void denali_irq_enable(struct denali_nand_info *denali, uint32_t int_mask); 128 uint32_t irq_mask);
129static void denali_irq_enable(struct denali_nand_info *denali,
130 uint32_t int_mask);
128static uint32_t read_interrupt_status(struct denali_nand_info *denali); 131static uint32_t read_interrupt_status(struct denali_nand_info *denali);
129 132
130#define DEBUG_DENALI 0 133#define DEBUG_DENALI 0
131 134
132/* This is a wrapper for writing to the denali registers. 135/* This is a wrapper for writing to the denali registers.
133 * this allows us to create debug information so we can 136 * this allows us to create debug information so we can
134 * observe how the driver is programming the device. 137 * observe how the driver is programming the device.
135 * it uses standard linux convention for (val, addr) */ 138 * it uses standard linux convention for (val, addr) */
136static void denali_write32(uint32_t value, void *addr) 139static void denali_write32(uint32_t value, void *addr)
137{ 140{
138 iowrite32(value, addr); 141 iowrite32(value, addr);
139 142
140#if DEBUG_DENALI 143#if DEBUG_DENALI
141 printk(KERN_ERR "wrote: 0x%x -> 0x%x\n", value, (uint32_t)((uint32_t)addr & 0x1fff)); 144 printk(KERN_INFO "wrote: 0x%x -> 0x%x\n", value,
145 (uint32_t)((uint32_t)addr & 0x1fff));
142#endif 146#endif
143} 147}
144 148
145/* Certain operations for the denali NAND controller use an indexed mode to read/write 149/* Certain operations for the denali NAND controller use
146 data. The operation is performed by writing the address value of the command to 150 * an indexed mode to read/write data. The operation is
147 the device memory followed by the data. This function abstracts this common 151 * performed by writing the address value of the command
148 operation. 152 * to the device memory followed by the data. This function
153 * abstracts this common operation.
149*/ 154*/
150static void index_addr(struct denali_nand_info *denali, uint32_t address, uint32_t data) 155static void index_addr(struct denali_nand_info *denali,
156 uint32_t address, uint32_t data)
151{ 157{
152 denali_write32(address, denali->flash_mem); 158 denali_write32(address, denali->flash_mem);
153 denali_write32(data, denali->flash_mem + 0x10); 159 denali_write32(data, denali->flash_mem + 0x10);
@@ -161,7 +167,7 @@ static void index_addr_read_data(struct denali_nand_info *denali,
161 *pdata = ioread32(denali->flash_mem + 0x10); 167 *pdata = ioread32(denali->flash_mem + 0x10);
162} 168}
163 169
164/* We need to buffer some data for some of the NAND core routines. 170/* We need to buffer some data for some of the NAND core routines.
165 * The operations manage buffering that data. */ 171 * The operations manage buffering that data. */
166static void reset_buf(struct denali_nand_info *denali) 172static void reset_buf(struct denali_nand_info *denali)
167{ 173{
@@ -183,7 +189,7 @@ static void read_status(struct denali_nand_info *denali)
183 reset_buf(denali); 189 reset_buf(denali);
184 190
185 /* initiate a device status read */ 191 /* initiate a device status read */
186 cmd = MODE_11 | BANK(denali->flash_bank); 192 cmd = MODE_11 | BANK(denali->flash_bank);
187 index_addr(denali, cmd | COMMAND_CYCLE, 0x70); 193 index_addr(denali, cmd | COMMAND_CYCLE, 0x70);
188 denali_write32(cmd | STATUS_CYCLE, denali->flash_mem); 194 denali_write32(cmd | STATUS_CYCLE, denali->flash_mem);
189 195
@@ -191,7 +197,8 @@ static void read_status(struct denali_nand_info *denali)
191 write_byte_to_buf(denali, ioread32(denali->flash_mem + 0x10)); 197 write_byte_to_buf(denali, ioread32(denali->flash_mem + 0x10));
192 198
193#if DEBUG_DENALI 199#if DEBUG_DENALI
194 printk("device reporting status value of 0x%2x\n", denali->buf.buf[0]); 200 printk(KERN_INFO "device reporting status value of 0x%2x\n",
201 denali->buf.buf[0]);
195#endif 202#endif
196} 203}
197 204
@@ -199,7 +206,7 @@ static void read_status(struct denali_nand_info *denali)
199static void reset_bank(struct denali_nand_info *denali) 206static void reset_bank(struct denali_nand_info *denali)
200{ 207{
201 uint32_t irq_status = 0; 208 uint32_t irq_status = 0;
202 uint32_t irq_mask = reset_complete[denali->flash_bank] | 209 uint32_t irq_mask = reset_complete[denali->flash_bank] |
203 operation_timeout[denali->flash_bank]; 210 operation_timeout[denali->flash_bank];
204 int bank = 0; 211 int bank = 0;
205 212
@@ -209,15 +216,13 @@ static void reset_bank(struct denali_nand_info *denali)
209 denali_write32(bank, denali->flash_reg + DEVICE_RESET); 216 denali_write32(bank, denali->flash_reg + DEVICE_RESET);
210 217
211 irq_status = wait_for_irq(denali, irq_mask); 218 irq_status = wait_for_irq(denali, irq_mask);
212 219
213 if (irq_status & operation_timeout[denali->flash_bank]) 220 if (irq_status & operation_timeout[denali->flash_bank])
214 {
215 printk(KERN_ERR "reset bank failed.\n"); 221 printk(KERN_ERR "reset bank failed.\n");
216 }
217} 222}
218 223
219/* Reset the flash controller */ 224/* Reset the flash controller */
220static uint16_t NAND_Flash_Reset(struct denali_nand_info *denali) 225static uint16_t denali_nand_reset(struct denali_nand_info *denali)
221{ 226{
222 uint32_t i; 227 uint32_t i;
223 228
@@ -229,8 +234,10 @@ static uint16_t NAND_Flash_Reset(struct denali_nand_info *denali)
229 denali->flash_reg + intr_status_addresses[i]); 234 denali->flash_reg + intr_status_addresses[i]);
230 235
231 for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++) { 236 for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++) {
232 denali_write32(device_reset_banks[i], denali->flash_reg + DEVICE_RESET); 237 denali_write32(device_reset_banks[i],
233 while (!(ioread32(denali->flash_reg + intr_status_addresses[i]) & 238 denali->flash_reg + DEVICE_RESET);
239 while (!(ioread32(denali->flash_reg +
240 intr_status_addresses[i]) &
234 (reset_complete[i] | operation_timeout[i]))) 241 (reset_complete[i] | operation_timeout[i])))
235 ; 242 ;
236 if (ioread32(denali->flash_reg + intr_status_addresses[i]) & 243 if (ioread32(denali->flash_reg + intr_status_addresses[i]) &
@@ -246,11 +253,12 @@ static uint16_t NAND_Flash_Reset(struct denali_nand_info *denali)
246 return PASS; 253 return PASS;
247} 254}
248 255
249/* this routine calculates the ONFI timing values for a given mode and programs 256/* this routine calculates the ONFI timing values for a given mode and
250 * the clocking register accordingly. The mode is determined by the get_onfi_nand_para 257 * programs the clocking register accordingly. The mode is determined by
251 routine. 258 * the get_onfi_nand_para routine.
252 */ 259 */
253static void NAND_ONFi_Timing_Mode(struct denali_nand_info *denali, uint16_t mode) 260static void nand_onfi_timing_set(struct denali_nand_info *denali,
261 uint16_t mode)
254{ 262{
255 uint16_t Trea[6] = {40, 30, 25, 20, 20, 16}; 263 uint16_t Trea[6] = {40, 30, 25, 20, 20, 16};
256 uint16_t Trp[6] = {50, 25, 17, 15, 12, 10}; 264 uint16_t Trp[6] = {50, 25, 17, 15, 12, 10};
@@ -347,136 +355,24 @@ static void NAND_ONFi_Timing_Mode(struct denali_nand_info *denali, uint16_t mode
347 denali_write32(cs_cnt, denali->flash_reg + CS_SETUP_CNT); 355 denali_write32(cs_cnt, denali->flash_reg + CS_SETUP_CNT);
348} 356}
349 357
350/* configures the initial ECC settings for the controller */
351static void set_ecc_config(struct denali_nand_info *denali)
352{
353#if SUPPORT_8BITECC
354 if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) < 4096) ||
355 (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) <= 128))
356 denali_write32(8, denali->flash_reg + ECC_CORRECTION);
357#endif
358
359 if ((ioread32(denali->flash_reg + ECC_CORRECTION) & ECC_CORRECTION__VALUE)
360 == 1) {
361 denali->dev_info.wECCBytesPerSector = 4;
362 denali->dev_info.wECCBytesPerSector *= denali->dev_info.wDevicesConnected;
363 denali->dev_info.wNumPageSpareFlag =
364 denali->dev_info.wPageSpareSize -
365 denali->dev_info.wPageDataSize /
366 (ECC_SECTOR_SIZE * denali->dev_info.wDevicesConnected) *
367 denali->dev_info.wECCBytesPerSector
368 - denali->dev_info.wSpareSkipBytes;
369 } else {
370 denali->dev_info.wECCBytesPerSector =
371 (ioread32(denali->flash_reg + ECC_CORRECTION) &
372 ECC_CORRECTION__VALUE) * 13 / 8;
373 if ((denali->dev_info.wECCBytesPerSector) % 2 == 0)
374 denali->dev_info.wECCBytesPerSector += 2;
375 else
376 denali->dev_info.wECCBytesPerSector += 1;
377
378 denali->dev_info.wECCBytesPerSector *= denali->dev_info.wDevicesConnected;
379 denali->dev_info.wNumPageSpareFlag = denali->dev_info.wPageSpareSize -
380 denali->dev_info.wPageDataSize /
381 (ECC_SECTOR_SIZE * denali->dev_info.wDevicesConnected) *
382 denali->dev_info.wECCBytesPerSector
383 - denali->dev_info.wSpareSkipBytes;
384 }
385}
386
387/* queries the NAND device to see what ONFI modes it supports. */ 358/* queries the NAND device to see what ONFI modes it supports. */
388static uint16_t get_onfi_nand_para(struct denali_nand_info *denali) 359static uint16_t get_onfi_nand_para(struct denali_nand_info *denali)
389{ 360{
390 int i; 361 int i;
391 uint16_t blks_lun_l, blks_lun_h, n_of_luns; 362 /* we needn't to do a reset here because driver has already
392 uint32_t blockperlun, id; 363 * reset all the banks before
393 364 * */
394 denali_write32(DEVICE_RESET__BANK0, denali->flash_reg + DEVICE_RESET);
395
396 while (!((ioread32(denali->flash_reg + INTR_STATUS0) &
397 INTR_STATUS0__RST_COMP) |
398 (ioread32(denali->flash_reg + INTR_STATUS0) &
399 INTR_STATUS0__TIME_OUT)))
400 ;
401
402 if (ioread32(denali->flash_reg + INTR_STATUS0) & INTR_STATUS0__RST_COMP) {
403 denali_write32(DEVICE_RESET__BANK1, denali->flash_reg + DEVICE_RESET);
404 while (!((ioread32(denali->flash_reg + INTR_STATUS1) &
405 INTR_STATUS1__RST_COMP) |
406 (ioread32(denali->flash_reg + INTR_STATUS1) &
407 INTR_STATUS1__TIME_OUT)))
408 ;
409
410 if (ioread32(denali->flash_reg + INTR_STATUS1) &
411 INTR_STATUS1__RST_COMP) {
412 denali_write32(DEVICE_RESET__BANK2,
413 denali->flash_reg + DEVICE_RESET);
414 while (!((ioread32(denali->flash_reg + INTR_STATUS2) &
415 INTR_STATUS2__RST_COMP) |
416 (ioread32(denali->flash_reg + INTR_STATUS2) &
417 INTR_STATUS2__TIME_OUT)))
418 ;
419
420 if (ioread32(denali->flash_reg + INTR_STATUS2) &
421 INTR_STATUS2__RST_COMP) {
422 denali_write32(DEVICE_RESET__BANK3,
423 denali->flash_reg + DEVICE_RESET);
424 while (!((ioread32(denali->flash_reg + INTR_STATUS3) &
425 INTR_STATUS3__RST_COMP) |
426 (ioread32(denali->flash_reg + INTR_STATUS3) &
427 INTR_STATUS3__TIME_OUT)))
428 ;
429 } else {
430 printk(KERN_ERR "Getting a time out for bank 2!\n");
431 }
432 } else {
433 printk(KERN_ERR "Getting a time out for bank 1!\n");
434 }
435 }
436
437 denali_write32(INTR_STATUS0__TIME_OUT, denali->flash_reg + INTR_STATUS0);
438 denali_write32(INTR_STATUS1__TIME_OUT, denali->flash_reg + INTR_STATUS1);
439 denali_write32(INTR_STATUS2__TIME_OUT, denali->flash_reg + INTR_STATUS2);
440 denali_write32(INTR_STATUS3__TIME_OUT, denali->flash_reg + INTR_STATUS3);
441
442 denali->dev_info.wONFIDevFeatures =
443 ioread32(denali->flash_reg + ONFI_DEVICE_FEATURES);
444 denali->dev_info.wONFIOptCommands =
445 ioread32(denali->flash_reg + ONFI_OPTIONAL_COMMANDS);
446 denali->dev_info.wONFITimingMode =
447 ioread32(denali->flash_reg + ONFI_TIMING_MODE);
448 denali->dev_info.wONFIPgmCacheTimingMode =
449 ioread32(denali->flash_reg + ONFI_PGM_CACHE_TIMING_MODE);
450
451 n_of_luns = ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
452 ONFI_DEVICE_NO_OF_LUNS__NO_OF_LUNS;
453 blks_lun_l = ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_BLOCKS_PER_LUN_L);
454 blks_lun_h = ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_BLOCKS_PER_LUN_U);
455
456 blockperlun = (blks_lun_h << 16) | blks_lun_l;
457
458 denali->dev_info.wTotalBlocks = n_of_luns * blockperlun;
459
460 if (!(ioread32(denali->flash_reg + ONFI_TIMING_MODE) & 365 if (!(ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
461 ONFI_TIMING_MODE__VALUE)) 366 ONFI_TIMING_MODE__VALUE))
462 return FAIL; 367 return FAIL;
463 368
464 for (i = 5; i > 0; i--) { 369 for (i = 5; i > 0; i--) {
465 if (ioread32(denali->flash_reg + ONFI_TIMING_MODE) & (0x01 << i)) 370 if (ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
371 (0x01 << i))
466 break; 372 break;
467 } 373 }
468 374
469 NAND_ONFi_Timing_Mode(denali, i); 375 nand_onfi_timing_set(denali, i);
470
471 index_addr(denali, MODE_11 | 0, 0x90);
472 index_addr(denali, MODE_11 | 1, 0);
473
474 for (i = 0; i < 3; i++)
475 index_addr_read_data(denali, MODE_11 | 2, &id);
476
477 nand_dbg_print(NAND_DBG_DEBUG, "3rd ID: 0x%x\n", id);
478
479 denali->dev_info.MLCDevice = id & 0x0C;
480 376
481 /* By now, all the ONFI devices we know support the page cache */ 377 /* By now, all the ONFI devices we know support the page cache */
482 /* rw feature. So here we enable the pipeline_rw_ahead feature */ 378 /* rw feature. So here we enable the pipeline_rw_ahead feature */
@@ -486,25 +382,10 @@ static uint16_t get_onfi_nand_para(struct denali_nand_info *denali)
486 return PASS; 382 return PASS;
487} 383}
488 384
489static void get_samsung_nand_para(struct denali_nand_info *denali) 385static void get_samsung_nand_para(struct denali_nand_info *denali,
386 uint8_t device_id)
490{ 387{
491 uint8_t no_of_planes; 388 if (device_id == 0xd3) { /* Samsung K9WAG08U1A */
492 uint32_t blk_size;
493 uint64_t plane_size, capacity;
494 uint32_t id_bytes[5];
495 int i;
496
497 index_addr(denali, (uint32_t)(MODE_11 | 0), 0x90);
498 index_addr(denali, (uint32_t)(MODE_11 | 1), 0);
499 for (i = 0; i < 5; i++)
500 index_addr_read_data(denali, (uint32_t)(MODE_11 | 2), &id_bytes[i]);
501
502 nand_dbg_print(NAND_DBG_DEBUG,
503 "ID bytes: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
504 id_bytes[0], id_bytes[1], id_bytes[2],
505 id_bytes[3], id_bytes[4]);
506
507 if ((id_bytes[1] & 0xff) == 0xd3) { /* Samsung K9WAG08U1A */
508 /* Set timing register values according to datasheet */ 389 /* Set timing register values according to datasheet */
509 denali_write32(5, denali->flash_reg + ACC_CLKS); 390 denali_write32(5, denali->flash_reg + ACC_CLKS);
510 denali_write32(20, denali->flash_reg + RE_2_WE); 391 denali_write32(20, denali->flash_reg + RE_2_WE);
@@ -514,19 +395,10 @@ static void get_samsung_nand_para(struct denali_nand_info *denali)
514 denali_write32(2, denali->flash_reg + RDWR_EN_HI_CNT); 395 denali_write32(2, denali->flash_reg + RDWR_EN_HI_CNT);
515 denali_write32(2, denali->flash_reg + CS_SETUP_CNT); 396 denali_write32(2, denali->flash_reg + CS_SETUP_CNT);
516 } 397 }
517
518 no_of_planes = 1 << ((id_bytes[4] & 0x0c) >> 2);
519 plane_size = (uint64_t)64 << ((id_bytes[4] & 0x70) >> 4);
520 blk_size = 64 << ((ioread32(denali->flash_reg + DEVICE_PARAM_1) & 0x30) >> 4);
521 capacity = (uint64_t)128 * plane_size * no_of_planes;
522
523 do_div(capacity, blk_size);
524 denali->dev_info.wTotalBlocks = capacity;
525} 398}
526 399
527static void get_toshiba_nand_para(struct denali_nand_info *denali) 400static void get_toshiba_nand_para(struct denali_nand_info *denali)
528{ 401{
529 void __iomem *scratch_reg;
530 uint32_t tmp; 402 uint32_t tmp;
531 403
532 /* Workaround to fix a controller bug which reports a wrong */ 404 /* Workaround to fix a controller bug which reports a wrong */
@@ -536,81 +408,52 @@ static void get_toshiba_nand_para(struct denali_nand_info *denali)
536 denali_write32(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE); 408 denali_write32(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
537 tmp = ioread32(denali->flash_reg + DEVICES_CONNECTED) * 409 tmp = ioread32(denali->flash_reg + DEVICES_CONNECTED) *
538 ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE); 410 ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
539 denali_write32(tmp, denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE); 411 denali_write32(tmp,
412 denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
540#if SUPPORT_15BITECC 413#if SUPPORT_15BITECC
541 denali_write32(15, denali->flash_reg + ECC_CORRECTION); 414 denali_write32(15, denali->flash_reg + ECC_CORRECTION);
542#elif SUPPORT_8BITECC 415#elif SUPPORT_8BITECC
543 denali_write32(8, denali->flash_reg + ECC_CORRECTION); 416 denali_write32(8, denali->flash_reg + ECC_CORRECTION);
544#endif 417#endif
545 } 418 }
546
547 /* As Toshiba NAND can not provide it's block number, */
548 /* so here we need user to provide the correct block */
549 /* number in a scratch register before the Linux NAND */
550 /* driver is loaded. If no valid value found in the scratch */
551 /* register, then we use default block number value */
552 scratch_reg = ioremap_nocache(SCRATCH_REG_ADDR, SCRATCH_REG_SIZE);
553 if (!scratch_reg) {
554 printk(KERN_ERR "Spectra: ioremap failed in %s, Line %d",
555 __FILE__, __LINE__);
556 denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS;
557 } else {
558 nand_dbg_print(NAND_DBG_WARN,
559 "Spectra: ioremap reg address: 0x%p\n", scratch_reg);
560 denali->dev_info.wTotalBlocks = 1 << ioread8(scratch_reg);
561 if (denali->dev_info.wTotalBlocks < 512)
562 denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS;
563 iounmap(scratch_reg);
564 }
565} 419}
566 420
567static void get_hynix_nand_para(struct denali_nand_info *denali) 421static void get_hynix_nand_para(struct denali_nand_info *denali,
422 uint8_t device_id)
568{ 423{
569 void __iomem *scratch_reg;
570 uint32_t main_size, spare_size; 424 uint32_t main_size, spare_size;
571 425
572 switch (denali->dev_info.wDeviceID) { 426 switch (device_id) {
573 case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */ 427 case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */
574 case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */ 428 case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */
575 denali_write32(128, denali->flash_reg + PAGES_PER_BLOCK); 429 denali_write32(128, denali->flash_reg + PAGES_PER_BLOCK);
576 denali_write32(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE); 430 denali_write32(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
577 denali_write32(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE); 431 denali_write32(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
578 main_size = 4096 * ioread32(denali->flash_reg + DEVICES_CONNECTED); 432 main_size = 4096 *
579 spare_size = 224 * ioread32(denali->flash_reg + DEVICES_CONNECTED); 433 ioread32(denali->flash_reg + DEVICES_CONNECTED);
580 denali_write32(main_size, denali->flash_reg + LOGICAL_PAGE_DATA_SIZE); 434 spare_size = 224 *
581 denali_write32(spare_size, denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE); 435 ioread32(denali->flash_reg + DEVICES_CONNECTED);
436 denali_write32(main_size,
437 denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
438 denali_write32(spare_size,
439 denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
582 denali_write32(0, denali->flash_reg + DEVICE_WIDTH); 440 denali_write32(0, denali->flash_reg + DEVICE_WIDTH);
583#if SUPPORT_15BITECC 441#if SUPPORT_15BITECC
584 denali_write32(15, denali->flash_reg + ECC_CORRECTION); 442 denali_write32(15, denali->flash_reg + ECC_CORRECTION);
585#elif SUPPORT_8BITECC 443#elif SUPPORT_8BITECC
586 denali_write32(8, denali->flash_reg + ECC_CORRECTION); 444 denali_write32(8, denali->flash_reg + ECC_CORRECTION);
587#endif 445#endif
588 denali->dev_info.MLCDevice = 1;
589 break; 446 break;
590 default: 447 default:
591 nand_dbg_print(NAND_DBG_WARN, 448 nand_dbg_print(NAND_DBG_WARN,
592 "Spectra: Unknown Hynix NAND (Device ID: 0x%x)." 449 "Spectra: Unknown Hynix NAND (Device ID: 0x%x)."
593 "Will use default parameter values instead.\n", 450 "Will use default parameter values instead.\n",
594 denali->dev_info.wDeviceID); 451 device_id);
595 }
596
597 scratch_reg = ioremap_nocache(SCRATCH_REG_ADDR, SCRATCH_REG_SIZE);
598 if (!scratch_reg) {
599 printk(KERN_ERR "Spectra: ioremap failed in %s, Line %d",
600 __FILE__, __LINE__);
601 denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS;
602 } else {
603 nand_dbg_print(NAND_DBG_WARN,
604 "Spectra: ioremap reg address: 0x%p\n", scratch_reg);
605 denali->dev_info.wTotalBlocks = 1 << ioread8(scratch_reg);
606 if (denali->dev_info.wTotalBlocks < 512)
607 denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS;
608 iounmap(scratch_reg);
609 } 452 }
610} 453}
611 454
612/* determines how many NAND chips are connected to the controller. Note for 455/* determines how many NAND chips are connected to the controller. Note for
613 Intel CE4100 devices we don't support more than one device. 456 Intel CE4100 devices we don't support more than one device.
614 */ 457 */
615static void find_valid_banks(struct denali_nand_info *denali) 458static void find_valid_banks(struct denali_nand_info *denali)
616{ 459{
@@ -621,7 +464,8 @@ static void find_valid_banks(struct denali_nand_info *denali)
621 for (i = 0; i < LLD_MAX_FLASH_BANKS; i++) { 464 for (i = 0; i < LLD_MAX_FLASH_BANKS; i++) {
622 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 0), 0x90); 465 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 0), 0x90);
623 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 1), 0); 466 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 1), 0);
624 index_addr_read_data(denali, (uint32_t)(MODE_11 | (i << 24) | 2), &id[i]); 467 index_addr_read_data(denali,
468 (uint32_t)(MODE_11 | (i << 24) | 2), &id[i]);
625 469
626 nand_dbg_print(NAND_DBG_DEBUG, 470 nand_dbg_print(NAND_DBG_DEBUG,
627 "Return 1st ID for bank[%d]: %x\n", i, id[i]); 471 "Return 1st ID for bank[%d]: %x\n", i, id[i]);
@@ -637,14 +481,12 @@ static void find_valid_banks(struct denali_nand_info *denali)
637 } 481 }
638 } 482 }
639 483
640 if (denali->platform == INTEL_CE4100) 484 if (denali->platform == INTEL_CE4100) {
641 {
642 /* Platform limitations of the CE4100 device limit 485 /* Platform limitations of the CE4100 device limit
643 * users to a single chip solution for NAND. 486 * users to a single chip solution for NAND.
644 * Multichip support is not enabled. 487 * Multichip support is not enabled.
645 */ 488 */
646 if (denali->total_used_banks != 1) 489 if (denali->total_used_banks != 1) {
647 {
648 printk(KERN_ERR "Sorry, Intel CE4100 only supports " 490 printk(KERN_ERR "Sorry, Intel CE4100 only supports "
649 "a single NAND device.\n"); 491 "a single NAND device.\n");
650 BUG(); 492 BUG();
@@ -656,150 +498,60 @@ static void find_valid_banks(struct denali_nand_info *denali)
656 498
657static void detect_partition_feature(struct denali_nand_info *denali) 499static void detect_partition_feature(struct denali_nand_info *denali)
658{ 500{
501 /* For MRST platform, denali->fwblks represent the
502 * number of blocks firmware is taken,
503 * FW is in protect partition and MTD driver has no
504 * permission to access it. So let driver know how many
505 * blocks it can't touch.
506 * */
659 if (ioread32(denali->flash_reg + FEATURES) & FEATURES__PARTITION) { 507 if (ioread32(denali->flash_reg + FEATURES) & FEATURES__PARTITION) {
660 if ((ioread32(denali->flash_reg + PERM_SRC_ID_1) & 508 if ((ioread32(denali->flash_reg + PERM_SRC_ID_1) &
661 PERM_SRC_ID_1__SRCID) == SPECTRA_PARTITION_ID) { 509 PERM_SRC_ID_1__SRCID) == SPECTRA_PARTITION_ID) {
662 denali->dev_info.wSpectraStartBlock = 510 denali->fwblks =
663 ((ioread32(denali->flash_reg + MIN_MAX_BANK_1) & 511 ((ioread32(denali->flash_reg + MIN_MAX_BANK_1) &
664 MIN_MAX_BANK_1__MIN_VALUE) * 512 MIN_MAX_BANK_1__MIN_VALUE) *
665 denali->dev_info.wTotalBlocks) 513 denali->blksperchip)
666 + 514 +
667 (ioread32(denali->flash_reg + MIN_BLK_ADDR_1) & 515 (ioread32(denali->flash_reg + MIN_BLK_ADDR_1) &
668 MIN_BLK_ADDR_1__VALUE); 516 MIN_BLK_ADDR_1__VALUE);
669 517 } else
670 denali->dev_info.wSpectraEndBlock = 518 denali->fwblks = SPECTRA_START_BLOCK;
671 (((ioread32(denali->flash_reg + MIN_MAX_BANK_1) & 519 } else
672 MIN_MAX_BANK_1__MAX_VALUE) >> 2) * 520 denali->fwblks = SPECTRA_START_BLOCK;
673 denali->dev_info.wTotalBlocks)
674 +
675 (ioread32(denali->flash_reg + MAX_BLK_ADDR_1) &
676 MAX_BLK_ADDR_1__VALUE);
677
678 denali->dev_info.wTotalBlocks *= denali->total_used_banks;
679
680 if (denali->dev_info.wSpectraEndBlock >=
681 denali->dev_info.wTotalBlocks) {
682 denali->dev_info.wSpectraEndBlock =
683 denali->dev_info.wTotalBlocks - 1;
684 }
685
686 denali->dev_info.wDataBlockNum =
687 denali->dev_info.wSpectraEndBlock -
688 denali->dev_info.wSpectraStartBlock + 1;
689 } else {
690 denali->dev_info.wTotalBlocks *= denali->total_used_banks;
691 denali->dev_info.wSpectraStartBlock = SPECTRA_START_BLOCK;
692 denali->dev_info.wSpectraEndBlock =
693 denali->dev_info.wTotalBlocks - 1;
694 denali->dev_info.wDataBlockNum =
695 denali->dev_info.wSpectraEndBlock -
696 denali->dev_info.wSpectraStartBlock + 1;
697 }
698 } else {
699 denali->dev_info.wTotalBlocks *= denali->total_used_banks;
700 denali->dev_info.wSpectraStartBlock = SPECTRA_START_BLOCK;
701 denali->dev_info.wSpectraEndBlock = denali->dev_info.wTotalBlocks - 1;
702 denali->dev_info.wDataBlockNum =
703 denali->dev_info.wSpectraEndBlock -
704 denali->dev_info.wSpectraStartBlock + 1;
705 }
706} 521}
707 522
708static void dump_device_info(struct denali_nand_info *denali) 523static uint16_t denali_nand_timing_set(struct denali_nand_info *denali)
709{
710 nand_dbg_print(NAND_DBG_DEBUG, "denali->dev_info:\n");
711 nand_dbg_print(NAND_DBG_DEBUG, "DeviceMaker: 0x%x\n",
712 denali->dev_info.wDeviceMaker);
713 nand_dbg_print(NAND_DBG_DEBUG, "DeviceID: 0x%x\n",
714 denali->dev_info.wDeviceID);
715 nand_dbg_print(NAND_DBG_DEBUG, "DeviceType: 0x%x\n",
716 denali->dev_info.wDeviceType);
717 nand_dbg_print(NAND_DBG_DEBUG, "SpectraStartBlock: %d\n",
718 denali->dev_info.wSpectraStartBlock);
719 nand_dbg_print(NAND_DBG_DEBUG, "SpectraEndBlock: %d\n",
720 denali->dev_info.wSpectraEndBlock);
721 nand_dbg_print(NAND_DBG_DEBUG, "TotalBlocks: %d\n",
722 denali->dev_info.wTotalBlocks);
723 nand_dbg_print(NAND_DBG_DEBUG, "PagesPerBlock: %d\n",
724 denali->dev_info.wPagesPerBlock);
725 nand_dbg_print(NAND_DBG_DEBUG, "PageSize: %d\n",
726 denali->dev_info.wPageSize);
727 nand_dbg_print(NAND_DBG_DEBUG, "PageDataSize: %d\n",
728 denali->dev_info.wPageDataSize);
729 nand_dbg_print(NAND_DBG_DEBUG, "PageSpareSize: %d\n",
730 denali->dev_info.wPageSpareSize);
731 nand_dbg_print(NAND_DBG_DEBUG, "NumPageSpareFlag: %d\n",
732 denali->dev_info.wNumPageSpareFlag);
733 nand_dbg_print(NAND_DBG_DEBUG, "ECCBytesPerSector: %d\n",
734 denali->dev_info.wECCBytesPerSector);
735 nand_dbg_print(NAND_DBG_DEBUG, "BlockSize: %d\n",
736 denali->dev_info.wBlockSize);
737 nand_dbg_print(NAND_DBG_DEBUG, "BlockDataSize: %d\n",
738 denali->dev_info.wBlockDataSize);
739 nand_dbg_print(NAND_DBG_DEBUG, "DataBlockNum: %d\n",
740 denali->dev_info.wDataBlockNum);
741 nand_dbg_print(NAND_DBG_DEBUG, "PlaneNum: %d\n",
742 denali->dev_info.bPlaneNum);
743 nand_dbg_print(NAND_DBG_DEBUG, "DeviceMainAreaSize: %d\n",
744 denali->dev_info.wDeviceMainAreaSize);
745 nand_dbg_print(NAND_DBG_DEBUG, "DeviceSpareAreaSize: %d\n",
746 denali->dev_info.wDeviceSpareAreaSize);
747 nand_dbg_print(NAND_DBG_DEBUG, "DevicesConnected: %d\n",
748 denali->dev_info.wDevicesConnected);
749 nand_dbg_print(NAND_DBG_DEBUG, "DeviceWidth: %d\n",
750 denali->dev_info.wDeviceWidth);
751 nand_dbg_print(NAND_DBG_DEBUG, "HWRevision: 0x%x\n",
752 denali->dev_info.wHWRevision);
753 nand_dbg_print(NAND_DBG_DEBUG, "HWFeatures: 0x%x\n",
754 denali->dev_info.wHWFeatures);
755 nand_dbg_print(NAND_DBG_DEBUG, "ONFIDevFeatures: 0x%x\n",
756 denali->dev_info.wONFIDevFeatures);
757 nand_dbg_print(NAND_DBG_DEBUG, "ONFIOptCommands: 0x%x\n",
758 denali->dev_info.wONFIOptCommands);
759 nand_dbg_print(NAND_DBG_DEBUG, "ONFITimingMode: 0x%x\n",
760 denali->dev_info.wONFITimingMode);
761 nand_dbg_print(NAND_DBG_DEBUG, "ONFIPgmCacheTimingMode: 0x%x\n",
762 denali->dev_info.wONFIPgmCacheTimingMode);
763 nand_dbg_print(NAND_DBG_DEBUG, "MLCDevice: %s\n",
764 denali->dev_info.MLCDevice ? "Yes" : "No");
765 nand_dbg_print(NAND_DBG_DEBUG, "SpareSkipBytes: %d\n",
766 denali->dev_info.wSpareSkipBytes);
767 nand_dbg_print(NAND_DBG_DEBUG, "BitsInPageNumber: %d\n",
768 denali->dev_info.nBitsInPageNumber);
769 nand_dbg_print(NAND_DBG_DEBUG, "BitsInPageDataSize: %d\n",
770 denali->dev_info.nBitsInPageDataSize);
771 nand_dbg_print(NAND_DBG_DEBUG, "BitsInBlockDataSize: %d\n",
772 denali->dev_info.nBitsInBlockDataSize);
773}
774
775static uint16_t NAND_Read_Device_ID(struct denali_nand_info *denali)
776{ 524{
777 uint16_t status = PASS; 525 uint16_t status = PASS;
778 uint8_t no_of_planes; 526 uint32_t id_bytes[5], addr;
527 uint8_t i, maf_id, device_id;
779 528
780 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n", 529 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
781 __FILE__, __LINE__, __func__); 530 __FILE__, __LINE__, __func__);
782 531
783 denali->dev_info.wDeviceMaker = ioread32(denali->flash_reg + MANUFACTURER_ID); 532 /* Use read id method to get device ID and other
784 denali->dev_info.wDeviceID = ioread32(denali->flash_reg + DEVICE_ID); 533 * params. For some NAND chips, controller can't
785 denali->dev_info.bDeviceParam0 = ioread32(denali->flash_reg + DEVICE_PARAM_0); 534 * report the correct device ID by reading from
786 denali->dev_info.bDeviceParam1 = ioread32(denali->flash_reg + DEVICE_PARAM_1); 535 * DEVICE_ID register
787 denali->dev_info.bDeviceParam2 = ioread32(denali->flash_reg + DEVICE_PARAM_2); 536 * */
788 537 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
789 denali->dev_info.MLCDevice = ioread32(denali->flash_reg + DEVICE_PARAM_0) & 0x0c; 538 index_addr(denali, (uint32_t)addr | 0, 0x90);
539 index_addr(denali, (uint32_t)addr | 1, 0);
540 for (i = 0; i < 5; i++)
541 index_addr_read_data(denali, addr | 2, &id_bytes[i]);
542 maf_id = id_bytes[0];
543 device_id = id_bytes[1];
790 544
791 if (ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) & 545 if (ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
792 ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */ 546 ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */
793 if (FAIL == get_onfi_nand_para(denali)) 547 if (FAIL == get_onfi_nand_para(denali))
794 return FAIL; 548 return FAIL;
795 } else if (denali->dev_info.wDeviceMaker == 0xEC) { /* Samsung NAND */ 549 } else if (maf_id == 0xEC) { /* Samsung NAND */
796 get_samsung_nand_para(denali); 550 get_samsung_nand_para(denali, device_id);
797 } else if (denali->dev_info.wDeviceMaker == 0x98) { /* Toshiba NAND */ 551 } else if (maf_id == 0x98) { /* Toshiba NAND */
798 get_toshiba_nand_para(denali); 552 get_toshiba_nand_para(denali);
799 } else if (denali->dev_info.wDeviceMaker == 0xAD) { /* Hynix NAND */ 553 } else if (maf_id == 0xAD) { /* Hynix NAND */
800 get_hynix_nand_para(denali); 554 get_hynix_nand_para(denali, device_id);
801 } else {
802 denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS;
803 } 555 }
804 556
805 nand_dbg_print(NAND_DBG_DEBUG, "Dump timing register values:" 557 nand_dbg_print(NAND_DBG_DEBUG, "Dump timing register values:"
@@ -814,88 +566,20 @@ static uint16_t NAND_Read_Device_ID(struct denali_nand_info *denali)
814 ioread32(denali->flash_reg + RDWR_EN_HI_CNT), 566 ioread32(denali->flash_reg + RDWR_EN_HI_CNT),
815 ioread32(denali->flash_reg + CS_SETUP_CNT)); 567 ioread32(denali->flash_reg + CS_SETUP_CNT));
816 568
817 denali->dev_info.wHWRevision = ioread32(denali->flash_reg + REVISION);
818 denali->dev_info.wHWFeatures = ioread32(denali->flash_reg + FEATURES);
819
820 denali->dev_info.wDeviceMainAreaSize =
821 ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
822 denali->dev_info.wDeviceSpareAreaSize =
823 ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
824
825 denali->dev_info.wPageDataSize =
826 ioread32(denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
827
828 /* Note: When using the Micon 4K NAND device, the controller will report
829 * Page Spare Size as 216 bytes. But Micron's Spec say it's 218 bytes.
830 * And if force set it to 218 bytes, the controller can not work
831 * correctly. So just let it be. But keep in mind that this bug may
832 * cause
833 * other problems in future. - Yunpeng 2008-10-10
834 */
835 denali->dev_info.wPageSpareSize =
836 ioread32(denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
837
838 denali->dev_info.wPagesPerBlock = ioread32(denali->flash_reg + PAGES_PER_BLOCK);
839
840 denali->dev_info.wPageSize =
841 denali->dev_info.wPageDataSize + denali->dev_info.wPageSpareSize;
842 denali->dev_info.wBlockSize =
843 denali->dev_info.wPageSize * denali->dev_info.wPagesPerBlock;
844 denali->dev_info.wBlockDataSize =
845 denali->dev_info.wPagesPerBlock * denali->dev_info.wPageDataSize;
846
847 denali->dev_info.wDeviceWidth = ioread32(denali->flash_reg + DEVICE_WIDTH);
848 denali->dev_info.wDeviceType =
849 ((ioread32(denali->flash_reg + DEVICE_WIDTH) > 0) ? 16 : 8);
850
851 denali->dev_info.wDevicesConnected = ioread32(denali->flash_reg + DEVICES_CONNECTED);
852
853 denali->dev_info.wSpareSkipBytes =
854 ioread32(denali->flash_reg + SPARE_AREA_SKIP_BYTES) *
855 denali->dev_info.wDevicesConnected;
856
857 denali->dev_info.nBitsInPageNumber =
858 ilog2(denali->dev_info.wPagesPerBlock);
859 denali->dev_info.nBitsInPageDataSize =
860 ilog2(denali->dev_info.wPageDataSize);
861 denali->dev_info.nBitsInBlockDataSize =
862 ilog2(denali->dev_info.wBlockDataSize);
863
864 set_ecc_config(denali);
865
866 no_of_planes = ioread32(denali->flash_reg + NUMBER_OF_PLANES) &
867 NUMBER_OF_PLANES__VALUE;
868
869 switch (no_of_planes) {
870 case 0:
871 case 1:
872 case 3:
873 case 7:
874 denali->dev_info.bPlaneNum = no_of_planes + 1;
875 break;
876 default:
877 status = FAIL;
878 break;
879 }
880
881 find_valid_banks(denali); 569 find_valid_banks(denali);
882 570
883 detect_partition_feature(denali); 571 detect_partition_feature(denali);
884 572
885 dump_device_info(denali);
886
887 /* If the user specified to override the default timings 573 /* If the user specified to override the default timings
888 * with a specific ONFI mode, we apply those changes here. 574 * with a specific ONFI mode, we apply those changes here.
889 */ 575 */
890 if (onfi_timing_mode != NAND_DEFAULT_TIMINGS) 576 if (onfi_timing_mode != NAND_DEFAULT_TIMINGS)
891 { 577 nand_onfi_timing_set(denali, onfi_timing_mode);
892 NAND_ONFi_Timing_Mode(denali, onfi_timing_mode);
893 }
894 578
895 return status; 579 return status;
896} 580}
897 581
898static void NAND_LLD_Enable_Disable_Interrupts(struct denali_nand_info *denali, 582static void denali_set_intr_modes(struct denali_nand_info *denali,
899 uint16_t INT_ENABLE) 583 uint16_t INT_ENABLE)
900{ 584{
901 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n", 585 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
@@ -912,7 +596,7 @@ static void NAND_LLD_Enable_Disable_Interrupts(struct denali_nand_info *denali,
912 */ 596 */
913static inline bool is_flash_bank_valid(int flash_bank) 597static inline bool is_flash_bank_valid(int flash_bank)
914{ 598{
915 return (flash_bank >= 0 && flash_bank < 4); 599 return (flash_bank >= 0 && flash_bank < 4);
916} 600}
917 601
918static void denali_irq_init(struct denali_nand_info *denali) 602static void denali_irq_init(struct denali_nand_info *denali)
@@ -920,7 +604,7 @@ static void denali_irq_init(struct denali_nand_info *denali)
920 uint32_t int_mask = 0; 604 uint32_t int_mask = 0;
921 605
922 /* Disable global interrupts */ 606 /* Disable global interrupts */
923 NAND_LLD_Enable_Disable_Interrupts(denali, false); 607 denali_set_intr_modes(denali, false);
924 608
925 int_mask = DENALI_IRQ_ALL; 609 int_mask = DENALI_IRQ_ALL;
926 610
@@ -935,11 +619,12 @@ static void denali_irq_init(struct denali_nand_info *denali)
935 619
936static void denali_irq_cleanup(int irqnum, struct denali_nand_info *denali) 620static void denali_irq_cleanup(int irqnum, struct denali_nand_info *denali)
937{ 621{
938 NAND_LLD_Enable_Disable_Interrupts(denali, false); 622 denali_set_intr_modes(denali, false);
939 free_irq(irqnum, denali); 623 free_irq(irqnum, denali);
940} 624}
941 625
942static void denali_irq_enable(struct denali_nand_info *denali, uint32_t int_mask) 626static void denali_irq_enable(struct denali_nand_info *denali,
627 uint32_t int_mask)
943{ 628{
944 denali_write32(int_mask, denali->flash_reg + INTR_EN0); 629 denali_write32(int_mask, denali->flash_reg + INTR_EN0);
945 denali_write32(int_mask, denali->flash_reg + INTR_EN1); 630 denali_write32(int_mask, denali->flash_reg + INTR_EN1);
@@ -948,15 +633,16 @@ static void denali_irq_enable(struct denali_nand_info *denali, uint32_t int_mask
948} 633}
949 634
950/* This function only returns when an interrupt that this driver cares about 635/* This function only returns when an interrupt that this driver cares about
951 * occurs. This is to reduce the overhead of servicing interrupts 636 * occurs. This is to reduce the overhead of servicing interrupts
952 */ 637 */
953static inline uint32_t denali_irq_detected(struct denali_nand_info *denali) 638static inline uint32_t denali_irq_detected(struct denali_nand_info *denali)
954{ 639{
955 return (read_interrupt_status(denali) & DENALI_IRQ_ALL); 640 return read_interrupt_status(denali) & DENALI_IRQ_ALL;
956} 641}
957 642
958/* Interrupts are cleared by writing a 1 to the appropriate status bit */ 643/* Interrupts are cleared by writing a 1 to the appropriate status bit */
959static inline void clear_interrupt(struct denali_nand_info *denali, uint32_t irq_mask) 644static inline void clear_interrupt(struct denali_nand_info *denali,
645 uint32_t irq_mask)
960{ 646{
961 uint32_t intr_status_reg = 0; 647 uint32_t intr_status_reg = 0;
962 648
@@ -995,17 +681,15 @@ static void print_irq_log(struct denali_nand_info *denali)
995{ 681{
996 int i = 0; 682 int i = 0;
997 683
998 printk("ISR debug log index = %X\n", denali->idx); 684 printk(KERN_INFO "ISR debug log index = %X\n", denali->idx);
999 for (i = 0; i < 32; i++) 685 for (i = 0; i < 32; i++)
1000 { 686 printk(KERN_INFO "%08X: %08X\n", i, denali->irq_debug_array[i]);
1001 printk("%08X: %08X\n", i, denali->irq_debug_array[i]);
1002 }
1003} 687}
1004#endif 688#endif
1005 689
1006/* This is the interrupt service routine. It handles all interrupts 690/* This is the interrupt service routine. It handles all interrupts
1007 * sent to this device. Note that on CE4100, this is a shared 691 * sent to this device. Note that on CE4100, this is a shared
1008 * interrupt. 692 * interrupt.
1009 */ 693 */
1010static irqreturn_t denali_isr(int irq, void *dev_id) 694static irqreturn_t denali_isr(int irq, void *dev_id)
1011{ 695{
@@ -1015,20 +699,20 @@ static irqreturn_t denali_isr(int irq, void *dev_id)
1015 699
1016 spin_lock(&denali->irq_lock); 700 spin_lock(&denali->irq_lock);
1017 701
1018 /* check to see if a valid NAND chip has 702 /* check to see if a valid NAND chip has
1019 * been selected. 703 * been selected.
1020 */ 704 */
1021 if (is_flash_bank_valid(denali->flash_bank)) 705 if (is_flash_bank_valid(denali->flash_bank)) {
1022 { 706 /* check to see if controller generated
1023 /* check to see if controller generated
1024 * the interrupt, since this is a shared interrupt */ 707 * the interrupt, since this is a shared interrupt */
1025 if ((irq_status = denali_irq_detected(denali)) != 0) 708 irq_status = denali_irq_detected(denali);
1026 { 709 if (irq_status != 0) {
1027#if DEBUG_DENALI 710#if DEBUG_DENALI
1028 denali->irq_debug_array[denali->idx++] = 0x10000000 | irq_status; 711 denali->irq_debug_array[denali->idx++] =
712 0x10000000 | irq_status;
1029 denali->idx %= 32; 713 denali->idx %= 32;
1030 714
1031 printk("IRQ status = 0x%04x\n", irq_status); 715 printk(KERN_INFO "IRQ status = 0x%04x\n", irq_status);
1032#endif 716#endif
1033 /* handle interrupt */ 717 /* handle interrupt */
1034 /* first acknowledge it */ 718 /* first acknowledge it */
@@ -1054,61 +738,62 @@ static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask)
1054 bool retry = false; 738 bool retry = false;
1055 unsigned long timeout = msecs_to_jiffies(1000); 739 unsigned long timeout = msecs_to_jiffies(1000);
1056 740
1057 do 741 do {
1058 {
1059#if DEBUG_DENALI 742#if DEBUG_DENALI
1060 printk("waiting for 0x%x\n", irq_mask); 743 printk(KERN_INFO "waiting for 0x%x\n", irq_mask);
1061#endif 744#endif
1062 comp_res = wait_for_completion_timeout(&denali->complete, timeout); 745 comp_res =
746 wait_for_completion_timeout(&denali->complete, timeout);
1063 spin_lock_irq(&denali->irq_lock); 747 spin_lock_irq(&denali->irq_lock);
1064 intr_status = denali->irq_status; 748 intr_status = denali->irq_status;
1065 749
1066#if DEBUG_DENALI 750#if DEBUG_DENALI
1067 denali->irq_debug_array[denali->idx++] = 0x20000000 | (irq_mask << 16) | intr_status; 751 denali->irq_debug_array[denali->idx++] =
752 0x20000000 | (irq_mask << 16) | intr_status;
1068 denali->idx %= 32; 753 denali->idx %= 32;
1069#endif 754#endif
1070 755
1071 if (intr_status & irq_mask) 756 if (intr_status & irq_mask) {
1072 {
1073 denali->irq_status &= ~irq_mask; 757 denali->irq_status &= ~irq_mask;
1074 spin_unlock_irq(&denali->irq_lock); 758 spin_unlock_irq(&denali->irq_lock);
1075#if DEBUG_DENALI 759#if DEBUG_DENALI
1076 if (retry) printk("status on retry = 0x%x\n", intr_status); 760 if (retry)
761 printk(KERN_INFO "status on retry = 0x%x\n",
762 intr_status);
1077#endif 763#endif
1078 /* our interrupt was detected */ 764 /* our interrupt was detected */
1079 break; 765 break;
1080 } 766 } else {
1081 else 767 /* these are not the interrupts you are looking for -
1082 { 768 * need to wait again */
1083 /* these are not the interrupts you are looking for -
1084 need to wait again */
1085 spin_unlock_irq(&denali->irq_lock); 769 spin_unlock_irq(&denali->irq_lock);
1086#if DEBUG_DENALI 770#if DEBUG_DENALI
1087 print_irq_log(denali); 771 print_irq_log(denali);
1088 printk("received irq nobody cared: irq_status = 0x%x," 772 printk(KERN_INFO "received irq nobody cared:"
1089 " irq_mask = 0x%x, timeout = %ld\n", intr_status, irq_mask, comp_res); 773 " irq_status = 0x%x, irq_mask = 0x%x,"
774 " timeout = %ld\n", intr_status,
775 irq_mask, comp_res);
1090#endif 776#endif
1091 retry = true; 777 retry = true;
1092 } 778 }
1093 } while (comp_res != 0); 779 } while (comp_res != 0);
1094 780
1095 if (comp_res == 0) 781 if (comp_res == 0) {
1096 {
1097 /* timeout */ 782 /* timeout */
1098 printk(KERN_ERR "timeout occurred, status = 0x%x, mask = 0x%x\n", 783 printk(KERN_ERR "timeout occurred, status = 0x%x, mask = 0x%x\n",
1099 intr_status, irq_mask); 784 intr_status, irq_mask);
1100 785
1101 intr_status = 0; 786 intr_status = 0;
1102 } 787 }
1103 return intr_status; 788 return intr_status;
1104} 789}
1105 790
1106/* This helper function setups the registers for ECC and whether or not 791/* This helper function setups the registers for ECC and whether or not
1107 the spare area will be transfered. */ 792 the spare area will be transfered. */
1108static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en, 793static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
1109 bool transfer_spare) 794 bool transfer_spare)
1110{ 795{
1111 int ecc_en_flag = 0, transfer_spare_flag = 0; 796 int ecc_en_flag = 0, transfer_spare_flag = 0;
1112 797
1113 /* set ECC, transfer spare bits if needed */ 798 /* set ECC, transfer spare bits if needed */
1114 ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0; 799 ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
@@ -1116,85 +801,85 @@ static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
1116 801
1117 /* Enable spare area/ECC per user's request. */ 802 /* Enable spare area/ECC per user's request. */
1118 denali_write32(ecc_en_flag, denali->flash_reg + ECC_ENABLE); 803 denali_write32(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
1119 denali_write32(transfer_spare_flag, denali->flash_reg + TRANSFER_SPARE_REG); 804 denali_write32(transfer_spare_flag,
805 denali->flash_reg + TRANSFER_SPARE_REG);
1120} 806}
1121 807
1122/* sends a pipeline command operation to the controller. See the Denali NAND 808/* sends a pipeline command operation to the controller. See the Denali NAND
1123 controller's user guide for more information (section 4.2.3.6). 809 controller's user guide for more information (section 4.2.3.6).
1124 */ 810 */
1125static int denali_send_pipeline_cmd(struct denali_nand_info *denali, bool ecc_en, 811static int denali_send_pipeline_cmd(struct denali_nand_info *denali,
1126 bool transfer_spare, int access_type, 812 bool ecc_en,
1127 int op) 813 bool transfer_spare,
814 int access_type,
815 int op)
1128{ 816{
1129 int status = PASS; 817 int status = PASS;
1130 uint32_t addr = 0x0, cmd = 0x0, page_count = 1, irq_status = 0, 818 uint32_t addr = 0x0, cmd = 0x0, page_count = 1, irq_status = 0,
1131 irq_mask = 0; 819 irq_mask = 0;
1132 820
1133 if (op == DENALI_READ) irq_mask = INTR_STATUS0__LOAD_COMP; 821 if (op == DENALI_READ)
1134 else if (op == DENALI_WRITE) irq_mask = 0; 822 irq_mask = INTR_STATUS0__LOAD_COMP;
1135 else BUG(); 823 else if (op == DENALI_WRITE)
824 irq_mask = 0;
825 else
826 BUG();
1136 827
1137 setup_ecc_for_xfer(denali, ecc_en, transfer_spare); 828 setup_ecc_for_xfer(denali, ecc_en, transfer_spare);
1138 829
1139#if DEBUG_DENALI 830#if DEBUG_DENALI
1140 spin_lock_irq(&denali->irq_lock); 831 spin_lock_irq(&denali->irq_lock);
1141 denali->irq_debug_array[denali->idx++] = 0x40000000 | ioread32(denali->flash_reg + ECC_ENABLE) | (access_type << 4); 832 denali->irq_debug_array[denali->idx++] =
833 0x40000000 | ioread32(denali->flash_reg + ECC_ENABLE) |
834 (access_type << 4);
1142 denali->idx %= 32; 835 denali->idx %= 32;
1143 spin_unlock_irq(&denali->irq_lock); 836 spin_unlock_irq(&denali->irq_lock);
1144#endif 837#endif
1145 838
1146 839
1147 /* clear interrupts */ 840 /* clear interrupts */
1148 clear_interrupts(denali); 841 clear_interrupts(denali);
1149 842
1150 addr = BANK(denali->flash_bank) | denali->page; 843 addr = BANK(denali->flash_bank) | denali->page;
1151 844
1152 if (op == DENALI_WRITE && access_type != SPARE_ACCESS) 845 if (op == DENALI_WRITE && access_type != SPARE_ACCESS) {
1153 { 846 cmd = MODE_01 | addr;
1154 cmd = MODE_01 | addr;
1155 denali_write32(cmd, denali->flash_mem); 847 denali_write32(cmd, denali->flash_mem);
1156 } 848 } else if (op == DENALI_WRITE && access_type == SPARE_ACCESS) {
1157 else if (op == DENALI_WRITE && access_type == SPARE_ACCESS)
1158 {
1159 /* read spare area */ 849 /* read spare area */
1160 cmd = MODE_10 | addr; 850 cmd = MODE_10 | addr;
1161 index_addr(denali, (uint32_t)cmd, access_type); 851 index_addr(denali, (uint32_t)cmd, access_type);
1162 852
1163 cmd = MODE_01 | addr; 853 cmd = MODE_01 | addr;
1164 denali_write32(cmd, denali->flash_mem); 854 denali_write32(cmd, denali->flash_mem);
1165 } 855 } else if (op == DENALI_READ) {
1166 else if (op == DENALI_READ)
1167 {
1168 /* setup page read request for access type */ 856 /* setup page read request for access type */
1169 cmd = MODE_10 | addr; 857 cmd = MODE_10 | addr;
1170 index_addr(denali, (uint32_t)cmd, access_type); 858 index_addr(denali, (uint32_t)cmd, access_type);
1171 859
1172 /* page 33 of the NAND controller spec indicates we should not 860 /* page 33 of the NAND controller spec indicates we should not
1173 use the pipeline commands in Spare area only mode. So we 861 use the pipeline commands in Spare area only mode. So we
1174 don't. 862 don't.
1175 */ 863 */
1176 if (access_type == SPARE_ACCESS) 864 if (access_type == SPARE_ACCESS) {
1177 {
1178 cmd = MODE_01 | addr; 865 cmd = MODE_01 | addr;
1179 denali_write32(cmd, denali->flash_mem); 866 denali_write32(cmd, denali->flash_mem);
1180 } 867 } else {
1181 else 868 index_addr(denali, (uint32_t)cmd,
1182 { 869 0x2000 | op | page_count);
1183 index_addr(denali, (uint32_t)cmd, 0x2000 | op | page_count); 870
1184 871 /* wait for command to be accepted
1185 /* wait for command to be accepted 872 * can always use status0 bit as the
1186 * can always use status0 bit as the mask is identical for each 873 * mask is identical for each
1187 * bank. */ 874 * bank. */
1188 irq_status = wait_for_irq(denali, irq_mask); 875 irq_status = wait_for_irq(denali, irq_mask);
1189 876
1190 if (irq_status == 0) 877 if (irq_status == 0) {
1191 {
1192 printk(KERN_ERR "cmd, page, addr on timeout " 878 printk(KERN_ERR "cmd, page, addr on timeout "
1193 "(0x%x, 0x%x, 0x%x)\n", cmd, denali->page, addr); 879 "(0x%x, 0x%x, 0x%x)\n", cmd,
880 denali->page, addr);
1194 status = FAIL; 881 status = FAIL;
1195 } 882 } else {
1196 else
1197 {
1198 cmd = MODE_01 | addr; 883 cmd = MODE_01 | addr;
1199 denali_write32(cmd, denali->flash_mem); 884 denali_write32(cmd, denali->flash_mem);
1200 } 885 }
@@ -1204,36 +889,35 @@ static int denali_send_pipeline_cmd(struct denali_nand_info *denali, bool ecc_en
1204} 889}
1205 890
1206/* helper function that simply writes a buffer to the flash */ 891/* helper function that simply writes a buffer to the flash */
1207static int write_data_to_flash_mem(struct denali_nand_info *denali, const uint8_t *buf, 892static int write_data_to_flash_mem(struct denali_nand_info *denali,
1208 int len) 893 const uint8_t *buf,
894 int len)
1209{ 895{
1210 uint32_t i = 0, *buf32; 896 uint32_t i = 0, *buf32;
1211 897
1212 /* verify that the len is a multiple of 4. see comment in 898 /* verify that the len is a multiple of 4. see comment in
1213 * read_data_from_flash_mem() */ 899 * read_data_from_flash_mem() */
1214 BUG_ON((len % 4) != 0); 900 BUG_ON((len % 4) != 0);
1215 901
1216 /* write the data to the flash memory */ 902 /* write the data to the flash memory */
1217 buf32 = (uint32_t *)buf; 903 buf32 = (uint32_t *)buf;
1218 for (i = 0; i < len / 4; i++) 904 for (i = 0; i < len / 4; i++)
1219 {
1220 denali_write32(*buf32++, denali->flash_mem + 0x10); 905 denali_write32(*buf32++, denali->flash_mem + 0x10);
1221 } 906 return i*4; /* intent is to return the number of bytes read */
1222 return i*4; /* intent is to return the number of bytes read */
1223} 907}
1224 908
1225/* helper function that simply reads a buffer from the flash */ 909/* helper function that simply reads a buffer from the flash */
1226static int read_data_from_flash_mem(struct denali_nand_info *denali, uint8_t *buf, 910static int read_data_from_flash_mem(struct denali_nand_info *denali,
1227 int len) 911 uint8_t *buf,
912 int len)
1228{ 913{
1229 uint32_t i = 0, *buf32; 914 uint32_t i = 0, *buf32;
1230 915
1231 /* we assume that len will be a multiple of 4, if not 916 /* we assume that len will be a multiple of 4, if not
1232 * it would be nice to know about it ASAP rather than 917 * it would be nice to know about it ASAP rather than
1233 * have random failures... 918 * have random failures...
1234 * 919 * This assumption is based on the fact that this
1235 * This assumption is based on the fact that this 920 * function is designed to be used to read flash pages,
1236 * function is designed to be used to read flash pages,
1237 * which are typically multiples of 4... 921 * which are typically multiples of 4...
1238 */ 922 */
1239 923
@@ -1242,10 +926,8 @@ static int read_data_from_flash_mem(struct denali_nand_info *denali, uint8_t *bu
1242 /* transfer the data from the flash */ 926 /* transfer the data from the flash */
1243 buf32 = (uint32_t *)buf; 927 buf32 = (uint32_t *)buf;
1244 for (i = 0; i < len / 4; i++) 928 for (i = 0; i < len / 4; i++)
1245 {
1246 *buf32++ = ioread32(denali->flash_mem + 0x10); 929 *buf32++ = ioread32(denali->flash_mem + 0x10);
1247 } 930 return i*4; /* intent is to return the number of bytes read */
1248 return i*4; /* intent is to return the number of bytes read */
1249} 931}
1250 932
1251/* writes OOB data to the device */ 933/* writes OOB data to the device */
@@ -1253,38 +935,35 @@ static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
1253{ 935{
1254 struct denali_nand_info *denali = mtd_to_denali(mtd); 936 struct denali_nand_info *denali = mtd_to_denali(mtd);
1255 uint32_t irq_status = 0; 937 uint32_t irq_status = 0;
1256 uint32_t irq_mask = INTR_STATUS0__PROGRAM_COMP | 938 uint32_t irq_mask = INTR_STATUS0__PROGRAM_COMP |
1257 INTR_STATUS0__PROGRAM_FAIL; 939 INTR_STATUS0__PROGRAM_FAIL;
1258 int status = 0; 940 int status = 0;
1259 941
1260 denali->page = page; 942 denali->page = page;
1261 943
1262 if (denali_send_pipeline_cmd(denali, false, false, SPARE_ACCESS, 944 if (denali_send_pipeline_cmd(denali, false, false, SPARE_ACCESS,
1263 DENALI_WRITE) == PASS) 945 DENALI_WRITE) == PASS) {
1264 {
1265 write_data_to_flash_mem(denali, buf, mtd->oobsize); 946 write_data_to_flash_mem(denali, buf, mtd->oobsize);
1266 947
1267#if DEBUG_DENALI 948#if DEBUG_DENALI
1268 spin_lock_irq(&denali->irq_lock); 949 spin_lock_irq(&denali->irq_lock);
1269 denali->irq_debug_array[denali->idx++] = 0x80000000 | mtd->oobsize; 950 denali->irq_debug_array[denali->idx++] =
951 0x80000000 | mtd->oobsize;
1270 denali->idx %= 32; 952 denali->idx %= 32;
1271 spin_unlock_irq(&denali->irq_lock); 953 spin_unlock_irq(&denali->irq_lock);
1272#endif 954#endif
1273 955
1274 956
1275 /* wait for operation to complete */ 957 /* wait for operation to complete */
1276 irq_status = wait_for_irq(denali, irq_mask); 958 irq_status = wait_for_irq(denali, irq_mask);
1277 959
1278 if (irq_status == 0) 960 if (irq_status == 0) {
1279 {
1280 printk(KERN_ERR "OOB write failed\n"); 961 printk(KERN_ERR "OOB write failed\n");
1281 status = -EIO; 962 status = -EIO;
1282 } 963 }
1283 } 964 } else {
1284 else
1285 {
1286 printk(KERN_ERR "unable to send pipeline command\n"); 965 printk(KERN_ERR "unable to send pipeline command\n");
1287 status = -EIO; 966 status = -EIO;
1288 } 967 }
1289 return status; 968 return status;
1290} 969}
@@ -1293,60 +972,56 @@ static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
1293static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page) 972static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
1294{ 973{
1295 struct denali_nand_info *denali = mtd_to_denali(mtd); 974 struct denali_nand_info *denali = mtd_to_denali(mtd);
1296 uint32_t irq_mask = INTR_STATUS0__LOAD_COMP, irq_status = 0, addr = 0x0, cmd = 0x0; 975 uint32_t irq_mask = INTR_STATUS0__LOAD_COMP,
976 irq_status = 0, addr = 0x0, cmd = 0x0;
1297 977
1298 denali->page = page; 978 denali->page = page;
1299 979
1300#if DEBUG_DENALI 980#if DEBUG_DENALI
1301 printk("read_oob %d\n", page); 981 printk(KERN_INFO "read_oob %d\n", page);
1302#endif 982#endif
1303 if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS, 983 if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
1304 DENALI_READ) == PASS) 984 DENALI_READ) == PASS) {
1305 { 985 read_data_from_flash_mem(denali, buf, mtd->oobsize);
1306 read_data_from_flash_mem(denali, buf, mtd->oobsize);
1307 986
1308 /* wait for command to be accepted 987 /* wait for command to be accepted
1309 * can always use status0 bit as the mask is identical for each 988 * can always use status0 bit as the mask is identical for each
1310 * bank. */ 989 * bank. */
1311 irq_status = wait_for_irq(denali, irq_mask); 990 irq_status = wait_for_irq(denali, irq_mask);
1312 991
1313 if (irq_status == 0) 992 if (irq_status == 0)
1314 { 993 printk(KERN_ERR "page on OOB timeout %d\n",
1315 printk(KERN_ERR "page on OOB timeout %d\n", denali->page); 994 denali->page);
1316 }
1317 995
1318 /* We set the device back to MAIN_ACCESS here as I observed 996 /* We set the device back to MAIN_ACCESS here as I observed
1319 * instability with the controller if you do a block erase 997 * instability with the controller if you do a block erase
1320 * and the last transaction was a SPARE_ACCESS. Block erase 998 * and the last transaction was a SPARE_ACCESS. Block erase
1321 * is reliable (according to the MTD test infrastructure) 999 * is reliable (according to the MTD test infrastructure)
1322 * if you are in MAIN_ACCESS. 1000 * if you are in MAIN_ACCESS.
1323 */ 1001 */
1324 addr = BANK(denali->flash_bank) | denali->page; 1002 addr = BANK(denali->flash_bank) | denali->page;
1325 cmd = MODE_10 | addr; 1003 cmd = MODE_10 | addr;
1326 index_addr(denali, (uint32_t)cmd, MAIN_ACCESS); 1004 index_addr(denali, (uint32_t)cmd, MAIN_ACCESS);
1327 1005
1328#if DEBUG_DENALI 1006#if DEBUG_DENALI
1329 spin_lock_irq(&denali->irq_lock); 1007 spin_lock_irq(&denali->irq_lock);
1330 denali->irq_debug_array[denali->idx++] = 0x60000000 | mtd->oobsize; 1008 denali->irq_debug_array[denali->idx++] =
1009 0x60000000 | mtd->oobsize;
1331 denali->idx %= 32; 1010 denali->idx %= 32;
1332 spin_unlock_irq(&denali->irq_lock); 1011 spin_unlock_irq(&denali->irq_lock);
1333#endif 1012#endif
1334 } 1013 }
1335} 1014}
1336 1015
1337/* this function examines buffers to see if they contain data that 1016/* this function examines buffers to see if they contain data that
1338 * indicate that the buffer is part of an erased region of flash. 1017 * indicate that the buffer is part of an erased region of flash.
1339 */ 1018 */
1340bool is_erased(uint8_t *buf, int len) 1019bool is_erased(uint8_t *buf, int len)
1341{ 1020{
1342 int i = 0; 1021 int i = 0;
1343 for (i = 0; i < len; i++) 1022 for (i = 0; i < len; i++)
1344 {
1345 if (buf[i] != 0xFF) 1023 if (buf[i] != 0xFF)
1346 {
1347 return false; 1024 return false;
1348 }
1349 }
1350 return true; 1025 return true;
1351} 1026}
1352#define ECC_SECTOR_SIZE 512 1027#define ECC_SECTOR_SIZE 512
@@ -1358,65 +1033,59 @@ bool is_erased(uint8_t *buf, int len)
1358#define ECC_ERR_DEVICE(x) ((x) & ERR_CORRECTION_INFO__DEVICE_NR >> 8) 1033#define ECC_ERR_DEVICE(x) ((x) & ERR_CORRECTION_INFO__DEVICE_NR >> 8)
1359#define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO) 1034#define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
1360 1035
1361static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf, 1036static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
1362 uint8_t *oobbuf, uint32_t irq_status) 1037 uint8_t *oobbuf, uint32_t irq_status)
1363{ 1038{
1364 bool check_erased_page = false; 1039 bool check_erased_page = false;
1365 1040
1366 if (irq_status & INTR_STATUS0__ECC_ERR) 1041 if (irq_status & INTR_STATUS0__ECC_ERR) {
1367 {
1368 /* read the ECC errors. we'll ignore them for now */ 1042 /* read the ECC errors. we'll ignore them for now */
1369 uint32_t err_address = 0, err_correction_info = 0; 1043 uint32_t err_address = 0, err_correction_info = 0;
1370 uint32_t err_byte = 0, err_sector = 0, err_device = 0; 1044 uint32_t err_byte = 0, err_sector = 0, err_device = 0;
1371 uint32_t err_correction_value = 0; 1045 uint32_t err_correction_value = 0;
1372 1046
1373 do 1047 do {
1374 { 1048 err_address = ioread32(denali->flash_reg +
1375 err_address = ioread32(denali->flash_reg +
1376 ECC_ERROR_ADDRESS); 1049 ECC_ERROR_ADDRESS);
1377 err_sector = ECC_SECTOR(err_address); 1050 err_sector = ECC_SECTOR(err_address);
1378 err_byte = ECC_BYTE(err_address); 1051 err_byte = ECC_BYTE(err_address);
1379 1052
1380 1053
1381 err_correction_info = ioread32(denali->flash_reg + 1054 err_correction_info = ioread32(denali->flash_reg +
1382 ERR_CORRECTION_INFO); 1055 ERR_CORRECTION_INFO);
1383 err_correction_value = 1056 err_correction_value =
1384 ECC_CORRECTION_VALUE(err_correction_info); 1057 ECC_CORRECTION_VALUE(err_correction_info);
1385 err_device = ECC_ERR_DEVICE(err_correction_info); 1058 err_device = ECC_ERR_DEVICE(err_correction_info);
1386 1059
1387 if (ECC_ERROR_CORRECTABLE(err_correction_info)) 1060 if (ECC_ERROR_CORRECTABLE(err_correction_info)) {
1388 {
1389 /* offset in our buffer is computed as: 1061 /* offset in our buffer is computed as:
1390 sector number * sector size + offset in 1062 sector number * sector size + offset in
1391 sector 1063 sector
1392 */ 1064 */
1393 int offset = err_sector * ECC_SECTOR_SIZE + 1065 int offset = err_sector * ECC_SECTOR_SIZE +
1394 err_byte; 1066 err_byte;
1395 if (offset < denali->mtd.writesize) 1067 if (offset < denali->mtd.writesize) {
1396 {
1397 /* correct the ECC error */ 1068 /* correct the ECC error */
1398 buf[offset] ^= err_correction_value; 1069 buf[offset] ^= err_correction_value;
1399 denali->mtd.ecc_stats.corrected++; 1070 denali->mtd.ecc_stats.corrected++;
1400 } 1071 } else {
1401 else
1402 {
1403 /* bummer, couldn't correct the error */ 1072 /* bummer, couldn't correct the error */
1404 printk(KERN_ERR "ECC offset invalid\n"); 1073 printk(KERN_ERR "ECC offset invalid\n");
1405 denali->mtd.ecc_stats.failed++; 1074 denali->mtd.ecc_stats.failed++;
1406 } 1075 }
1407 } 1076 } else {
1408 else 1077 /* if the error is not correctable, need to
1409 { 1078 * look at the page to see if it is an erased
1410 /* if the error is not correctable, need to 1079 * page. if so, then it's not a real ECC error
1411 * look at the page to see if it is an erased page. 1080 * */
1412 * if so, then it's not a real ECC error */
1413 check_erased_page = true; 1081 check_erased_page = true;
1414 } 1082 }
1415 1083
1416#if DEBUG_DENALI 1084#if DEBUG_DENALI
1417 printk("Detected ECC error in page %d: err_addr = 0x%08x," 1085 printk(KERN_INFO "Detected ECC error in page %d:"
1418 " info to fix is 0x%08x\n", denali->page, err_address, 1086 " err_addr = 0x%08x, info to fix is"
1419 err_correction_info); 1087 " 0x%08x\n", denali->page, err_address,
1088 err_correction_info);
1420#endif 1089#endif
1421 } while (!ECC_LAST_ERR(err_correction_info)); 1090 } while (!ECC_LAST_ERR(err_correction_info));
1422 } 1091 }
@@ -1428,7 +1097,8 @@ static void denali_enable_dma(struct denali_nand_info *denali, bool en)
1428{ 1097{
1429 uint32_t reg_val = 0x0; 1098 uint32_t reg_val = 0x0;
1430 1099
1431 if (en) reg_val = DMA_ENABLE__FLAG; 1100 if (en)
1101 reg_val = DMA_ENABLE__FLAG;
1432 1102
1433 denali_write32(reg_val, denali->flash_reg + DMA_ENABLE); 1103 denali_write32(reg_val, denali->flash_reg + DMA_ENABLE);
1434 ioread32(denali->flash_reg + DMA_ENABLE); 1104 ioread32(denali->flash_reg + DMA_ENABLE);
@@ -1458,9 +1128,9 @@ static void denali_setup_dma(struct denali_nand_info *denali, int op)
1458 index_addr(denali, mode | 0x14000, 0x2400); 1128 index_addr(denali, mode | 0x14000, 0x2400);
1459} 1129}
1460 1130
1461/* writes a page. user specifies type, and this function handles the 1131/* writes a page. user specifies type, and this function handles the
1462 configuration details. */ 1132 configuration details. */
1463static void write_page(struct mtd_info *mtd, struct nand_chip *chip, 1133static void write_page(struct mtd_info *mtd, struct nand_chip *chip,
1464 const uint8_t *buf, bool raw_xfer) 1134 const uint8_t *buf, bool raw_xfer)
1465{ 1135{
1466 struct denali_nand_info *denali = mtd_to_denali(mtd); 1136 struct denali_nand_info *denali = mtd_to_denali(mtd);
@@ -1470,7 +1140,7 @@ static void write_page(struct mtd_info *mtd, struct nand_chip *chip,
1470 size_t size = denali->mtd.writesize + denali->mtd.oobsize; 1140 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1471 1141
1472 uint32_t irq_status = 0; 1142 uint32_t irq_status = 0;
1473 uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP | 1143 uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP |
1474 INTR_STATUS0__PROGRAM_FAIL; 1144 INTR_STATUS0__PROGRAM_FAIL;
1475 1145
1476 /* if it is a raw xfer, we want to disable ecc, and send 1146 /* if it is a raw xfer, we want to disable ecc, and send
@@ -1483,74 +1153,73 @@ static void write_page(struct mtd_info *mtd, struct nand_chip *chip,
1483 /* copy buffer into DMA buffer */ 1153 /* copy buffer into DMA buffer */
1484 memcpy(denali->buf.buf, buf, mtd->writesize); 1154 memcpy(denali->buf.buf, buf, mtd->writesize);
1485 1155
1486 if (raw_xfer) 1156 if (raw_xfer) {
1487 {
1488 /* transfer the data to the spare area */ 1157 /* transfer the data to the spare area */
1489 memcpy(denali->buf.buf + mtd->writesize, 1158 memcpy(denali->buf.buf + mtd->writesize,
1490 chip->oob_poi, 1159 chip->oob_poi,
1491 mtd->oobsize); 1160 mtd->oobsize);
1492 } 1161 }
1493 1162
1494 pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_TODEVICE); 1163 pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_TODEVICE);
1495 1164
1496 clear_interrupts(denali); 1165 clear_interrupts(denali);
1497 denali_enable_dma(denali, true); 1166 denali_enable_dma(denali, true);
1498 1167
1499 denali_setup_dma(denali, DENALI_WRITE); 1168 denali_setup_dma(denali, DENALI_WRITE);
1500 1169
1501 /* wait for operation to complete */ 1170 /* wait for operation to complete */
1502 irq_status = wait_for_irq(denali, irq_mask); 1171 irq_status = wait_for_irq(denali, irq_mask);
1503 1172
1504 if (irq_status == 0) 1173 if (irq_status == 0) {
1505 { 1174 printk(KERN_ERR "timeout on write_page"
1506 printk(KERN_ERR "timeout on write_page (type = %d)\n", raw_xfer); 1175 " (type = %d)\n", raw_xfer);
1507 denali->status = 1176 denali->status =
1508 (irq_status & INTR_STATUS0__PROGRAM_FAIL) ? NAND_STATUS_FAIL : 1177 (irq_status & INTR_STATUS0__PROGRAM_FAIL) ?
1509 PASS; 1178 NAND_STATUS_FAIL : PASS;
1510 } 1179 }
1511 1180
1512 denali_enable_dma(denali, false); 1181 denali_enable_dma(denali, false);
1513 pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_TODEVICE); 1182 pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_TODEVICE);
1514} 1183}
1515 1184
1516/* NAND core entry points */ 1185/* NAND core entry points */
1517 1186
1518/* this is the callback that the NAND core calls to write a page. Since 1187/* this is the callback that the NAND core calls to write a page. Since
1519 writing a page with ECC or without is similar, all the work is done 1188 writing a page with ECC or without is similar, all the work is done
1520 by write_page above. */ 1189 by write_page above. */
1521static void denali_write_page(struct mtd_info *mtd, struct nand_chip *chip, 1190static void denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1522 const uint8_t *buf) 1191 const uint8_t *buf)
1523{ 1192{
1524 /* for regular page writes, we let HW handle all the ECC 1193 /* for regular page writes, we let HW handle all the ECC
1525 * data written to the device. */ 1194 * data written to the device. */
1526 write_page(mtd, chip, buf, false); 1195 write_page(mtd, chip, buf, false);
1527} 1196}
1528 1197
1529/* This is the callback that the NAND core calls to write a page without ECC. 1198/* This is the callback that the NAND core calls to write a page without ECC.
1530 raw access is similiar to ECC page writes, so all the work is done in the 1199 raw access is similiar to ECC page writes, so all the work is done in the
1531 write_page() function above. 1200 write_page() function above.
1532 */ 1201 */
1533static void denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, 1202static void denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1534 const uint8_t *buf) 1203 const uint8_t *buf)
1535{ 1204{
1536 /* for raw page writes, we want to disable ECC and simply write 1205 /* for raw page writes, we want to disable ECC and simply write
1537 whatever data is in the buffer. */ 1206 whatever data is in the buffer. */
1538 write_page(mtd, chip, buf, true); 1207 write_page(mtd, chip, buf, true);
1539} 1208}
1540 1209
1541static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip, 1210static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1542 int page) 1211 int page)
1543{ 1212{
1544 return write_oob_data(mtd, chip->oob_poi, page); 1213 return write_oob_data(mtd, chip->oob_poi, page);
1545} 1214}
1546 1215
1547static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip, 1216static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1548 int page, int sndcmd) 1217 int page, int sndcmd)
1549{ 1218{
1550 read_oob_data(mtd, chip->oob_poi, page); 1219 read_oob_data(mtd, chip->oob_poi, page);
1551 1220
1552 return 0; /* notify NAND core to send command to 1221 return 0; /* notify NAND core to send command to
1553 * NAND device. */ 1222 NAND device. */
1554} 1223}
1555 1224
1556static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip, 1225static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
@@ -1563,7 +1232,7 @@ static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1563 size_t size = denali->mtd.writesize + denali->mtd.oobsize; 1232 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1564 1233
1565 uint32_t irq_status = 0; 1234 uint32_t irq_status = 0;
1566 uint32_t irq_mask = INTR_STATUS0__ECC_TRANSACTION_DONE | 1235 uint32_t irq_mask = INTR_STATUS0__ECC_TRANSACTION_DONE |
1567 INTR_STATUS0__ECC_ERR; 1236 INTR_STATUS0__ECC_ERR;
1568 bool check_erased_page = false; 1237 bool check_erased_page = false;
1569 1238
@@ -1581,26 +1250,20 @@ static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1581 pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE); 1250 pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
1582 1251
1583 memcpy(buf, denali->buf.buf, mtd->writesize); 1252 memcpy(buf, denali->buf.buf, mtd->writesize);
1584 1253
1585 check_erased_page = handle_ecc(denali, buf, chip->oob_poi, irq_status); 1254 check_erased_page = handle_ecc(denali, buf, chip->oob_poi, irq_status);
1586 denali_enable_dma(denali, false); 1255 denali_enable_dma(denali, false);
1587 1256
1588 if (check_erased_page) 1257 if (check_erased_page) {
1589 {
1590 read_oob_data(&denali->mtd, chip->oob_poi, denali->page); 1258 read_oob_data(&denali->mtd, chip->oob_poi, denali->page);
1591 1259
1592 /* check ECC failures that may have occurred on erased pages */ 1260 /* check ECC failures that may have occurred on erased pages */
1593 if (check_erased_page) 1261 if (check_erased_page) {
1594 {
1595 if (!is_erased(buf, denali->mtd.writesize)) 1262 if (!is_erased(buf, denali->mtd.writesize))
1596 {
1597 denali->mtd.ecc_stats.failed++; 1263 denali->mtd.ecc_stats.failed++;
1598 }
1599 if (!is_erased(buf, denali->mtd.oobsize)) 1264 if (!is_erased(buf, denali->mtd.oobsize))
1600 {
1601 denali->mtd.ecc_stats.failed++; 1265 denali->mtd.ecc_stats.failed++;
1602 } 1266 }
1603 }
1604 } 1267 }
1605 return 0; 1268 return 0;
1606} 1269}
@@ -1616,7 +1279,7 @@ static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1616 1279
1617 uint32_t irq_status = 0; 1280 uint32_t irq_status = 0;
1618 uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP; 1281 uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP;
1619 1282
1620 setup_ecc_for_xfer(denali, false, true); 1283 setup_ecc_for_xfer(denali, false, true);
1621 denali_enable_dma(denali, true); 1284 denali_enable_dma(denali, true);
1622 1285
@@ -1644,12 +1307,10 @@ static uint8_t denali_read_byte(struct mtd_info *mtd)
1644 uint8_t result = 0xff; 1307 uint8_t result = 0xff;
1645 1308
1646 if (denali->buf.head < denali->buf.tail) 1309 if (denali->buf.head < denali->buf.tail)
1647 {
1648 result = denali->buf.buf[denali->buf.head++]; 1310 result = denali->buf.buf[denali->buf.head++];
1649 }
1650 1311
1651#if DEBUG_DENALI 1312#if DEBUG_DENALI
1652 printk("read byte -> 0x%02x\n", result); 1313 printk(KERN_INFO "read byte -> 0x%02x\n", result);
1653#endif 1314#endif
1654 return result; 1315 return result;
1655} 1316}
@@ -1658,7 +1319,7 @@ static void denali_select_chip(struct mtd_info *mtd, int chip)
1658{ 1319{
1659 struct denali_nand_info *denali = mtd_to_denali(mtd); 1320 struct denali_nand_info *denali = mtd_to_denali(mtd);
1660#if DEBUG_DENALI 1321#if DEBUG_DENALI
1661 printk("denali select chip %d\n", chip); 1322 printk(KERN_INFO "denali select chip %d\n", chip);
1662#endif 1323#endif
1663 spin_lock_irq(&denali->irq_lock); 1324 spin_lock_irq(&denali->irq_lock);
1664 denali->flash_bank = chip; 1325 denali->flash_bank = chip;
@@ -1672,7 +1333,7 @@ static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
1672 denali->status = 0; 1333 denali->status = 0;
1673 1334
1674#if DEBUG_DENALI 1335#if DEBUG_DENALI
1675 printk("waitfunc %d\n", status); 1336 printk(KERN_INFO "waitfunc %d\n", status);
1676#endif 1337#endif
1677 return status; 1338 return status;
1678} 1339}
@@ -1684,76 +1345,74 @@ static void denali_erase(struct mtd_info *mtd, int page)
1684 uint32_t cmd = 0x0, irq_status = 0; 1345 uint32_t cmd = 0x0, irq_status = 0;
1685 1346
1686#if DEBUG_DENALI 1347#if DEBUG_DENALI
1687 printk("erase page: %d\n", page); 1348 printk(KERN_INFO "erase page: %d\n", page);
1688#endif 1349#endif
1689 /* clear interrupts */ 1350 /* clear interrupts */
1690 clear_interrupts(denali); 1351 clear_interrupts(denali);
1691 1352
1692 /* setup page read request for access type */ 1353 /* setup page read request for access type */
1693 cmd = MODE_10 | BANK(denali->flash_bank) | page; 1354 cmd = MODE_10 | BANK(denali->flash_bank) | page;
1694 index_addr(denali, (uint32_t)cmd, 0x1); 1355 index_addr(denali, (uint32_t)cmd, 0x1);
1695 1356
1696 /* wait for erase to complete or failure to occur */ 1357 /* wait for erase to complete or failure to occur */
1697 irq_status = wait_for_irq(denali, INTR_STATUS0__ERASE_COMP | 1358 irq_status = wait_for_irq(denali, INTR_STATUS0__ERASE_COMP |
1698 INTR_STATUS0__ERASE_FAIL); 1359 INTR_STATUS0__ERASE_FAIL);
1699 1360
1700 denali->status = (irq_status & INTR_STATUS0__ERASE_FAIL) ? NAND_STATUS_FAIL : 1361 denali->status = (irq_status & INTR_STATUS0__ERASE_FAIL) ?
1701 PASS; 1362 NAND_STATUS_FAIL : PASS;
1702} 1363}
1703 1364
1704static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col, 1365static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
1705 int page) 1366 int page)
1706{ 1367{
1707 struct denali_nand_info *denali = mtd_to_denali(mtd); 1368 struct denali_nand_info *denali = mtd_to_denali(mtd);
1369 uint32_t addr, id;
1370 int i;
1708 1371
1709#if DEBUG_DENALI 1372#if DEBUG_DENALI
1710 printk("cmdfunc: 0x%x %d %d\n", cmd, col, page); 1373 printk(KERN_INFO "cmdfunc: 0x%x %d %d\n", cmd, col, page);
1711#endif 1374#endif
1712 switch (cmd) 1375 switch (cmd) {
1713 { 1376 case NAND_CMD_PAGEPROG:
1714 case NAND_CMD_PAGEPROG: 1377 break;
1715 break; 1378 case NAND_CMD_STATUS:
1716 case NAND_CMD_STATUS: 1379 read_status(denali);
1717 read_status(denali); 1380 break;
1718 break; 1381 case NAND_CMD_READID:
1719 case NAND_CMD_READID: 1382 reset_buf(denali);
1720 reset_buf(denali); 1383 /*sometimes ManufactureId read from register is not right
1721 if (denali->flash_bank < denali->total_used_banks) 1384 * e.g. some of Micron MT29F32G08QAA MLC NAND chips
1722 { 1385 * So here we send READID cmd to NAND insteand
1723 /* write manufacturer information into nand 1386 * */
1724 buffer for NAND subsystem to fetch. 1387 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
1725 */ 1388 index_addr(denali, (uint32_t)addr | 0, 0x90);
1726 write_byte_to_buf(denali, denali->dev_info.wDeviceMaker); 1389 index_addr(denali, (uint32_t)addr | 1, 0);
1727 write_byte_to_buf(denali, denali->dev_info.wDeviceID); 1390 for (i = 0; i < 5; i++) {
1728 write_byte_to_buf(denali, denali->dev_info.bDeviceParam0); 1391 index_addr_read_data(denali,
1729 write_byte_to_buf(denali, denali->dev_info.bDeviceParam1); 1392 (uint32_t)addr | 2,
1730 write_byte_to_buf(denali, denali->dev_info.bDeviceParam2); 1393 &id);
1731 } 1394 write_byte_to_buf(denali, id);
1732 else 1395 }
1733 { 1396 break;
1734 int i; 1397 case NAND_CMD_READ0:
1735 for (i = 0; i < 5; i++) 1398 case NAND_CMD_SEQIN:
1736 write_byte_to_buf(denali, 0xff); 1399 denali->page = page;
1737 } 1400 break;
1738 break; 1401 case NAND_CMD_RESET:
1739 case NAND_CMD_READ0: 1402 reset_bank(denali);
1740 case NAND_CMD_SEQIN: 1403 break;
1741 denali->page = page; 1404 case NAND_CMD_READOOB:
1742 break; 1405 /* TODO: Read OOB data */
1743 case NAND_CMD_RESET: 1406 break;
1744 reset_bank(denali); 1407 default:
1745 break; 1408 printk(KERN_ERR ": unsupported command"
1746 case NAND_CMD_READOOB: 1409 " received 0x%x\n", cmd);
1747 /* TODO: Read OOB data */ 1410 break;
1748 break;
1749 default:
1750 printk(KERN_ERR ": unsupported command received 0x%x\n", cmd);
1751 break;
1752 } 1411 }
1753} 1412}
1754 1413
1755/* stubs for ECC functions not used by the NAND core */ 1414/* stubs for ECC functions not used by the NAND core */
1756static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data, 1415static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
1757 uint8_t *ecc_code) 1416 uint8_t *ecc_code)
1758{ 1417{
1759 printk(KERN_ERR "denali_ecc_calculate called unexpectedly\n"); 1418 printk(KERN_ERR "denali_ecc_calculate called unexpectedly\n");
@@ -1761,7 +1420,7 @@ static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
1761 return -EIO; 1420 return -EIO;
1762} 1421}
1763 1422
1764static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data, 1423static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data,
1765 uint8_t *read_ecc, uint8_t *calc_ecc) 1424 uint8_t *read_ecc, uint8_t *calc_ecc)
1766{ 1425{
1767 printk(KERN_ERR "denali_ecc_correct called unexpectedly\n"); 1426 printk(KERN_ERR "denali_ecc_correct called unexpectedly\n");
@@ -1779,10 +1438,18 @@ static void denali_ecc_hwctl(struct mtd_info *mtd, int mode)
1779/* Initialization code to bring the device up to a known good state */ 1438/* Initialization code to bring the device up to a known good state */
1780static void denali_hw_init(struct denali_nand_info *denali) 1439static void denali_hw_init(struct denali_nand_info *denali)
1781{ 1440{
1441 /* tell driver how many bit controller will skip before
1442 * writing ECC code in OOB, this register may be already
1443 * set by firmware. So we read this value out.
1444 * if this value is 0, just let it be.
1445 * */
1446 denali->bbtskipbytes = ioread32(denali->flash_reg +
1447 SPARE_AREA_SKIP_BYTES);
1782 denali_irq_init(denali); 1448 denali_irq_init(denali);
1783 NAND_Flash_Reset(denali); 1449 denali_nand_reset(denali);
1784 denali_write32(0x0F, denali->flash_reg + RB_PIN_ENABLED); 1450 denali_write32(0x0F, denali->flash_reg + RB_PIN_ENABLED);
1785 denali_write32(CHIP_EN_DONT_CARE__FLAG, denali->flash_reg + CHIP_ENABLE_DONT_CARE); 1451 denali_write32(CHIP_EN_DONT_CARE__FLAG,
1452 denali->flash_reg + CHIP_ENABLE_DONT_CARE);
1786 1453
1787 denali_write32(0x0, denali->flash_reg + SPARE_AREA_SKIP_BYTES); 1454 denali_write32(0x0, denali->flash_reg + SPARE_AREA_SKIP_BYTES);
1788 denali_write32(0xffff, denali->flash_reg + SPARE_AREA_MARKER); 1455 denali_write32(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
@@ -1792,25 +1459,18 @@ static void denali_hw_init(struct denali_nand_info *denali)
1792 denali_write32(1, denali->flash_reg + ECC_ENABLE); 1459 denali_write32(1, denali->flash_reg + ECC_ENABLE);
1793} 1460}
1794 1461
1795/* ECC layout for SLC devices. Denali spec indicates SLC fixed at 4 bytes */ 1462/* Althogh controller spec said SLC ECC is forceb to be 4bit,
1796#define ECC_BYTES_SLC 4 * (2048 / ECC_SECTOR_SIZE) 1463 * but denali controller in MRST only support 15bit and 8bit ECC
1797static struct nand_ecclayout nand_oob_slc = { 1464 * correction
1798 .eccbytes = 4, 1465 * */
1799 .eccpos = { 0, 1, 2, 3 }, /* not used */ 1466#define ECC_8BITS 14
1800 .oobfree = {{ 1467static struct nand_ecclayout nand_8bit_oob = {
1801 .offset = ECC_BYTES_SLC, 1468 .eccbytes = 14,
1802 .length = 64 - ECC_BYTES_SLC
1803 }}
1804}; 1469};
1805 1470
1806#define ECC_BYTES_MLC 14 * (2048 / ECC_SECTOR_SIZE) 1471#define ECC_15BITS 26
1807static struct nand_ecclayout nand_oob_mlc_14bit = { 1472static struct nand_ecclayout nand_15bit_oob = {
1808 .eccbytes = 14, 1473 .eccbytes = 26,
1809 .eccpos = { 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, /* not used */
1810 .oobfree = {{
1811 .offset = ECC_BYTES_MLC,
1812 .length = 64 - ECC_BYTES_MLC
1813 }}
1814}; 1474};
1815 1475
1816static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' }; 1476static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
@@ -1842,12 +1502,12 @@ void denali_drv_init(struct denali_nand_info *denali)
1842 denali->idx = 0; 1502 denali->idx = 0;
1843 1503
1844 /* setup interrupt handler */ 1504 /* setup interrupt handler */
1845 /* the completion object will be used to notify 1505 /* the completion object will be used to notify
1846 * the callee that the interrupt is done */ 1506 * the callee that the interrupt is done */
1847 init_completion(&denali->complete); 1507 init_completion(&denali->complete);
1848 1508
1849 /* the spinlock will be used to synchronize the ISR 1509 /* the spinlock will be used to synchronize the ISR
1850 * with any element that might be access shared 1510 * with any element that might be access shared
1851 * data (interrupt status) */ 1511 * data (interrupt status) */
1852 spin_lock_init(&denali->irq_lock); 1512 spin_lock_init(&denali->irq_lock);
1853 1513
@@ -1880,13 +1540,12 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1880 } 1540 }
1881 1541
1882 if (id->driver_data == INTEL_CE4100) { 1542 if (id->driver_data == INTEL_CE4100) {
1883 /* Due to a silicon limitation, we can only support 1543 /* Due to a silicon limitation, we can only support
1884 * ONFI timing mode 1 and below. 1544 * ONFI timing mode 1 and below.
1885 */ 1545 */
1886 if (onfi_timing_mode < -1 || onfi_timing_mode > 1) 1546 if (onfi_timing_mode < -1 || onfi_timing_mode > 1) {
1887 { 1547 printk(KERN_ERR "Intel CE4100 only supports"
1888 printk("Intel CE4100 only supports ONFI timing mode 1 " 1548 " ONFI timing mode 1 or below\n");
1889 "or below\n");
1890 ret = -EINVAL; 1549 ret = -EINVAL;
1891 goto failed_enable; 1550 goto failed_enable;
1892 } 1551 }
@@ -1905,7 +1564,9 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1905 mem_base = csr_base + csr_len; 1564 mem_base = csr_base + csr_len;
1906 mem_len = csr_len; 1565 mem_len = csr_len;
1907 nand_dbg_print(NAND_DBG_WARN, 1566 nand_dbg_print(NAND_DBG_WARN,
1908 "Spectra: No second BAR for PCI device; assuming %08Lx\n", 1567 "Spectra: No second"
1568 " BAR for PCI device;"
1569 " assuming %08Lx\n",
1909 (uint64_t)csr_base); 1570 (uint64_t)csr_base);
1910 } 1571 }
1911 } 1572 }
@@ -1913,16 +1574,16 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1913 /* Is 32-bit DMA supported? */ 1574 /* Is 32-bit DMA supported? */
1914 ret = pci_set_dma_mask(dev, DMA_BIT_MASK(32)); 1575 ret = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
1915 1576
1916 if (ret) 1577 if (ret) {
1917 {
1918 printk(KERN_ERR "Spectra: no usable DMA configuration\n"); 1578 printk(KERN_ERR "Spectra: no usable DMA configuration\n");
1919 goto failed_enable; 1579 goto failed_enable;
1920 } 1580 }
1921 denali->buf.dma_buf = pci_map_single(dev, denali->buf.buf, DENALI_BUF_SIZE, 1581 denali->buf.dma_buf =
1922 PCI_DMA_BIDIRECTIONAL); 1582 pci_map_single(dev, denali->buf.buf,
1583 DENALI_BUF_SIZE,
1584 PCI_DMA_BIDIRECTIONAL);
1923 1585
1924 if (pci_dma_mapping_error(dev, denali->buf.dma_buf)) 1586 if (pci_dma_mapping_error(dev, denali->buf.dma_buf)) {
1925 {
1926 printk(KERN_ERR "Spectra: failed to map DMA buffer\n"); 1587 printk(KERN_ERR "Spectra: failed to map DMA buffer\n");
1927 goto failed_enable; 1588 goto failed_enable;
1928 } 1589 }
@@ -1970,22 +1631,11 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1970 } 1631 }
1971 1632
1972 /* now that our ISR is registered, we can enable interrupts */ 1633 /* now that our ISR is registered, we can enable interrupts */
1973 NAND_LLD_Enable_Disable_Interrupts(denali, true); 1634 denali_set_intr_modes(denali, true);
1974 1635
1975 pci_set_drvdata(dev, denali); 1636 pci_set_drvdata(dev, denali);
1976 1637
1977 NAND_Read_Device_ID(denali); 1638 denali_nand_timing_set(denali);
1978
1979 /* MTD supported page sizes vary by kernel. We validate our
1980 kernel supports the device here.
1981 */
1982 if (denali->dev_info.wPageSize > NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE)
1983 {
1984 ret = -ENODEV;
1985 printk(KERN_ERR "Spectra: device size not supported by this "
1986 "version of MTD.");
1987 goto failed_nand;
1988 }
1989 1639
1990 nand_dbg_print(NAND_DBG_DEBUG, "Dump timing register values:" 1640 nand_dbg_print(NAND_DBG_DEBUG, "Dump timing register values:"
1991 "acc_clks: %d, re_2_we: %d, we_2_re: %d," 1641 "acc_clks: %d, re_2_we: %d, we_2_re: %d,"
@@ -2009,18 +1659,46 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
2009 denali->nand.read_byte = denali_read_byte; 1659 denali->nand.read_byte = denali_read_byte;
2010 denali->nand.waitfunc = denali_waitfunc; 1660 denali->nand.waitfunc = denali_waitfunc;
2011 1661
2012 /* scan for NAND devices attached to the controller 1662 /* scan for NAND devices attached to the controller
2013 * this is the first stage in a two step process to register 1663 * this is the first stage in a two step process to register
2014 * with the nand subsystem */ 1664 * with the nand subsystem */
2015 if (nand_scan_ident(&denali->mtd, LLD_MAX_FLASH_BANKS, NULL)) 1665 if (nand_scan_ident(&denali->mtd, LLD_MAX_FLASH_BANKS, NULL)) {
2016 {
2017 ret = -ENXIO; 1666 ret = -ENXIO;
2018 goto failed_nand; 1667 goto failed_nand;
2019 } 1668 }
2020 1669
2021 /* second stage of the NAND scan 1670 /* MTD supported page sizes vary by kernel. We validate our
2022 * this stage requires information regarding ECC and 1671 * kernel supports the device here.
2023 * bad block management. */ 1672 */
1673 if (denali->mtd.writesize > NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE) {
1674 ret = -ENODEV;
1675 printk(KERN_ERR "Spectra: device size not supported by this "
1676 "version of MTD.");
1677 goto failed_nand;
1678 }
1679
1680 /* support for multi nand
1681 * MTD known nothing about multi nand,
1682 * so we should tell it the real pagesize
1683 * and anything necessery
1684 */
1685 denali->devnum = ioread32(denali->flash_reg + DEVICES_CONNECTED);
1686 denali->nand.chipsize <<= (denali->devnum - 1);
1687 denali->nand.page_shift += (denali->devnum - 1);
1688 denali->nand.pagemask = (denali->nand.chipsize >>
1689 denali->nand.page_shift) - 1;
1690 denali->nand.bbt_erase_shift += (denali->devnum - 1);
1691 denali->nand.phys_erase_shift = denali->nand.bbt_erase_shift;
1692 denali->nand.chip_shift += (denali->devnum - 1);
1693 denali->mtd.writesize <<= (denali->devnum - 1);
1694 denali->mtd.oobsize <<= (denali->devnum - 1);
1695 denali->mtd.erasesize <<= (denali->devnum - 1);
1696 denali->mtd.size = denali->nand.numchips * denali->nand.chipsize;
1697 denali->bbtskipbytes *= denali->devnum;
1698
1699 /* second stage of the NAND scan
1700 * this stage requires information regarding ECC and
1701 * bad block management. */
2024 1702
2025 /* Bad block management */ 1703 /* Bad block management */
2026 denali->nand.bbt_td = &bbt_main_descr; 1704 denali->nand.bbt_td = &bbt_main_descr;
@@ -2030,26 +1708,57 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
2030 denali->nand.options |= NAND_USE_FLASH_BBT | NAND_SKIP_BBTSCAN; 1708 denali->nand.options |= NAND_USE_FLASH_BBT | NAND_SKIP_BBTSCAN;
2031 denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME; 1709 denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
2032 1710
2033 if (denali->dev_info.MLCDevice) 1711 /* Denali Controller only support 15bit and 8bit ECC in MRST,
2034 { 1712 * so just let controller do 15bit ECC for MLC and 8bit ECC for
2035 denali->nand.ecc.layout = &nand_oob_mlc_14bit; 1713 * SLC if possible.
2036 denali->nand.ecc.bytes = ECC_BYTES_MLC; 1714 * */
2037 } 1715 if (denali->nand.cellinfo & 0xc &&
2038 else /* SLC */ 1716 (denali->mtd.oobsize > (denali->bbtskipbytes +
2039 { 1717 ECC_15BITS * (denali->mtd.writesize /
2040 denali->nand.ecc.layout = &nand_oob_slc; 1718 ECC_SECTOR_SIZE)))) {
2041 denali->nand.ecc.bytes = ECC_BYTES_SLC; 1719 /* if MLC OOB size is large enough, use 15bit ECC*/
1720 denali->nand.ecc.layout = &nand_15bit_oob;
1721 denali->nand.ecc.bytes = ECC_15BITS;
1722 denali_write32(15, denali->flash_reg + ECC_CORRECTION);
1723 } else if (denali->mtd.oobsize < (denali->bbtskipbytes +
1724 ECC_8BITS * (denali->mtd.writesize /
1725 ECC_SECTOR_SIZE))) {
1726 printk(KERN_ERR "Your NAND chip OOB is not large enough to"
1727 " contain 8bit ECC correction codes");
1728 goto failed_nand;
1729 } else {
1730 denali->nand.ecc.layout = &nand_8bit_oob;
1731 denali->nand.ecc.bytes = ECC_8BITS;
1732 denali_write32(8, denali->flash_reg + ECC_CORRECTION);
2042 } 1733 }
2043 1734
2044 /* These functions are required by the NAND core framework, otherwise, 1735 denali->nand.ecc.bytes *= denali->devnum;
2045 the NAND core will assert. However, we don't need them, so we'll stub 1736 denali->nand.ecc.layout->eccbytes *=
2046 them out. */ 1737 denali->mtd.writesize / ECC_SECTOR_SIZE;
1738 denali->nand.ecc.layout->oobfree[0].offset =
1739 denali->bbtskipbytes + denali->nand.ecc.layout->eccbytes;
1740 denali->nand.ecc.layout->oobfree[0].length =
1741 denali->mtd.oobsize - denali->nand.ecc.layout->eccbytes -
1742 denali->bbtskipbytes;
1743
1744 /* Let driver know the total blocks number and
1745 * how many blocks contained by each nand chip.
1746 * blksperchip will help driver to know how many
1747 * blocks is taken by FW.
1748 * */
1749 denali->totalblks = denali->mtd.size >>
1750 denali->nand.phys_erase_shift;
1751 denali->blksperchip = denali->totalblks / denali->nand.numchips;
1752
1753 /* These functions are required by the NAND core framework, otherwise,
1754 * the NAND core will assert. However, we don't need them, so we'll stub
1755 * them out. */
2047 denali->nand.ecc.calculate = denali_ecc_calculate; 1756 denali->nand.ecc.calculate = denali_ecc_calculate;
2048 denali->nand.ecc.correct = denali_ecc_correct; 1757 denali->nand.ecc.correct = denali_ecc_correct;
2049 denali->nand.ecc.hwctl = denali_ecc_hwctl; 1758 denali->nand.ecc.hwctl = denali_ecc_hwctl;
2050 1759
2051 /* override the default read operations */ 1760 /* override the default read operations */
2052 denali->nand.ecc.size = denali->mtd.writesize; 1761 denali->nand.ecc.size = ECC_SECTOR_SIZE * denali->devnum;
2053 denali->nand.ecc.read_page = denali_read_page; 1762 denali->nand.ecc.read_page = denali_read_page;
2054 denali->nand.ecc.read_page_raw = denali_read_page_raw; 1763 denali->nand.ecc.read_page_raw = denali_read_page_raw;
2055 denali->nand.ecc.write_page = denali_write_page; 1764 denali->nand.ecc.write_page = denali_write_page;
@@ -2058,15 +1767,15 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
2058 denali->nand.ecc.write_oob = denali_write_oob; 1767 denali->nand.ecc.write_oob = denali_write_oob;
2059 denali->nand.erase_cmd = denali_erase; 1768 denali->nand.erase_cmd = denali_erase;
2060 1769
2061 if (nand_scan_tail(&denali->mtd)) 1770 if (nand_scan_tail(&denali->mtd)) {
2062 {
2063 ret = -ENXIO; 1771 ret = -ENXIO;
2064 goto failed_nand; 1772 goto failed_nand;
2065 } 1773 }
2066 1774
2067 ret = add_mtd_device(&denali->mtd); 1775 ret = add_mtd_device(&denali->mtd);
2068 if (ret) { 1776 if (ret) {
2069 printk(KERN_ERR "Spectra: Failed to register MTD device: %d\n", ret); 1777 printk(KERN_ERR "Spectra: Failed to register"
1778 " MTD device: %d\n", ret);
2070 goto failed_nand; 1779 goto failed_nand;
2071 } 1780 }
2072 return 0; 1781 return 0;
@@ -2079,7 +1788,7 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
2079 failed_remap_csr: 1788 failed_remap_csr:
2080 pci_release_regions(dev); 1789 pci_release_regions(dev);
2081 failed_req_csr: 1790 failed_req_csr:
2082 pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE, 1791 pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
2083 PCI_DMA_BIDIRECTIONAL); 1792 PCI_DMA_BIDIRECTIONAL);
2084 failed_enable: 1793 failed_enable:
2085 kfree(denali); 1794 kfree(denali);
@@ -2103,7 +1812,7 @@ static void denali_pci_remove(struct pci_dev *dev)
2103 iounmap(denali->flash_mem); 1812 iounmap(denali->flash_mem);
2104 pci_release_regions(dev); 1813 pci_release_regions(dev);
2105 pci_disable_device(dev); 1814 pci_disable_device(dev);
2106 pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE, 1815 pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
2107 PCI_DMA_BIDIRECTIONAL); 1816 PCI_DMA_BIDIRECTIONAL);
2108 pci_set_drvdata(dev, NULL); 1817 pci_set_drvdata(dev, NULL);
2109 kfree(denali); 1818 kfree(denali);
@@ -2120,7 +1829,8 @@ static struct pci_driver denali_pci_driver = {
2120 1829
2121static int __devinit denali_init(void) 1830static int __devinit denali_init(void)
2122{ 1831{
2123 printk(KERN_INFO "Spectra MTD driver built on %s @ %s\n", __DATE__, __TIME__); 1832 printk(KERN_INFO "Spectra MTD driver built on %s @ %s\n",
1833 __DATE__, __TIME__);
2124 return pci_register_driver(&denali_pci_driver); 1834 return pci_register_driver(&denali_pci_driver);
2125} 1835}
2126 1836
diff --git a/drivers/mtd/nand/denali.h b/drivers/mtd/nand/denali.h
index 422a29ab2f6..b680474e633 100644
--- a/drivers/mtd/nand/denali.h
+++ b/drivers/mtd/nand/denali.h
@@ -17,7 +17,7 @@
17 * 17 *
18 */ 18 */
19 19
20#include <linux/mtd/nand.h> 20#include <linux/mtd/nand.h>
21 21
22#define DEVICE_RESET 0x0 22#define DEVICE_RESET 0x0
23#define DEVICE_RESET__BANK0 0x0001 23#define DEVICE_RESET__BANK0 0x0001
@@ -29,7 +29,7 @@
29#define TRANSFER_SPARE_REG__FLAG 0x0001 29#define TRANSFER_SPARE_REG__FLAG 0x0001
30 30
31#define LOAD_WAIT_CNT 0x20 31#define LOAD_WAIT_CNT 0x20
32#define LOAD_WAIT_CNT__VALUE 0xffff 32#define LOAD_WAIT_CNT__VALUE 0xffff
33 33
34#define PROGRAM_WAIT_CNT 0x30 34#define PROGRAM_WAIT_CNT 0x30
35#define PROGRAM_WAIT_CNT__VALUE 0xffff 35#define PROGRAM_WAIT_CNT__VALUE 0xffff
@@ -83,7 +83,7 @@
83#define RE_2_WE 0x120 83#define RE_2_WE 0x120
84#define RE_2_WE__VALUE 0x003f 84#define RE_2_WE__VALUE 0x003f
85 85
86#define ACC_CLKS 0x130 86#define ACC_CLKS 0x130
87#define ACC_CLKS__VALUE 0x000f 87#define ACC_CLKS__VALUE 0x000f
88 88
89#define NUMBER_OF_PLANES 0x140 89#define NUMBER_OF_PLANES 0x140
@@ -140,7 +140,7 @@
140#define DEVICES_CONNECTED 0x250 140#define DEVICES_CONNECTED 0x250
141#define DEVICES_CONNECTED__VALUE 0x0007 141#define DEVICES_CONNECTED__VALUE 0x0007
142 142
143#define DIE_MASK 0x260 143#define DIE_MASK 0x260
144#define DIE_MASK__VALUE 0x00ff 144#define DIE_MASK__VALUE 0x00ff
145 145
146#define FIRST_BLOCK_OF_NEXT_PLANE 0x270 146#define FIRST_BLOCK_OF_NEXT_PLANE 0x270
@@ -152,7 +152,7 @@
152#define RE_2_RE 0x290 152#define RE_2_RE 0x290
153#define RE_2_RE__VALUE 0x003f 153#define RE_2_RE__VALUE 0x003f
154 154
155#define MANUFACTURER_ID 0x300 155#define MANUFACTURER_ID 0x300
156#define MANUFACTURER_ID__VALUE 0x00ff 156#define MANUFACTURER_ID__VALUE 0x00ff
157 157
158#define DEVICE_ID 0x310 158#define DEVICE_ID 0x310
@@ -173,13 +173,13 @@
173#define LOGICAL_PAGE_SPARE_SIZE 0x360 173#define LOGICAL_PAGE_SPARE_SIZE 0x360
174#define LOGICAL_PAGE_SPARE_SIZE__VALUE 0xffff 174#define LOGICAL_PAGE_SPARE_SIZE__VALUE 0xffff
175 175
176#define REVISION 0x370 176#define REVISION 0x370
177#define REVISION__VALUE 0xffff 177#define REVISION__VALUE 0xffff
178 178
179#define ONFI_DEVICE_FEATURES 0x380 179#define ONFI_DEVICE_FEATURES 0x380
180#define ONFI_DEVICE_FEATURES__VALUE 0x003f 180#define ONFI_DEVICE_FEATURES__VALUE 0x003f
181 181
182#define ONFI_OPTIONAL_COMMANDS 0x390 182#define ONFI_OPTIONAL_COMMANDS 0x390
183#define ONFI_OPTIONAL_COMMANDS__VALUE 0x003f 183#define ONFI_OPTIONAL_COMMANDS__VALUE 0x003f
184 184
185#define ONFI_TIMING_MODE 0x3a0 185#define ONFI_TIMING_MODE 0x3a0
@@ -201,12 +201,12 @@
201#define FEATURES 0x3f0 201#define FEATURES 0x3f0
202#define FEATURES__N_BANKS 0x0003 202#define FEATURES__N_BANKS 0x0003
203#define FEATURES__ECC_MAX_ERR 0x003c 203#define FEATURES__ECC_MAX_ERR 0x003c
204#define FEATURES__DMA 0x0040 204#define FEATURES__DMA 0x0040
205#define FEATURES__CMD_DMA 0x0080 205#define FEATURES__CMD_DMA 0x0080
206#define FEATURES__PARTITION 0x0100 206#define FEATURES__PARTITION 0x0100
207#define FEATURES__XDMA_SIDEBAND 0x0200 207#define FEATURES__XDMA_SIDEBAND 0x0200
208#define FEATURES__GPREG 0x0400 208#define FEATURES__GPREG 0x0400
209#define FEATURES__INDEX_ADDR 0x0800 209#define FEATURES__INDEX_ADDR 0x0800
210 210
211#define TRANSFER_MODE 0x400 211#define TRANSFER_MODE 0x400
212#define TRANSFER_MODE__VALUE 0x0003 212#define TRANSFER_MODE__VALUE 0x0003
@@ -235,12 +235,12 @@
235#define INTR_EN0__DMA_CMD_COMP 0x0004 235#define INTR_EN0__DMA_CMD_COMP 0x0004
236#define INTR_EN0__TIME_OUT 0x0008 236#define INTR_EN0__TIME_OUT 0x0008
237#define INTR_EN0__PROGRAM_FAIL 0x0010 237#define INTR_EN0__PROGRAM_FAIL 0x0010
238#define INTR_EN0__ERASE_FAIL 0x0020 238#define INTR_EN0__ERASE_FAIL 0x0020
239#define INTR_EN0__LOAD_COMP 0x0040 239#define INTR_EN0__LOAD_COMP 0x0040
240#define INTR_EN0__PROGRAM_COMP 0x0080 240#define INTR_EN0__PROGRAM_COMP 0x0080
241#define INTR_EN0__ERASE_COMP 0x0100 241#define INTR_EN0__ERASE_COMP 0x0100
242#define INTR_EN0__PIPE_CPYBCK_CMD_COMP 0x0200 242#define INTR_EN0__PIPE_CPYBCK_CMD_COMP 0x0200
243#define INTR_EN0__LOCKED_BLK 0x0400 243#define INTR_EN0__LOCKED_BLK 0x0400
244#define INTR_EN0__UNSUP_CMD 0x0800 244#define INTR_EN0__UNSUP_CMD 0x0800
245#define INTR_EN0__INT_ACT 0x1000 245#define INTR_EN0__INT_ACT 0x1000
246#define INTR_EN0__RST_COMP 0x2000 246#define INTR_EN0__RST_COMP 0x2000
@@ -253,7 +253,7 @@
253#define ERR_PAGE_ADDR0 0x440 253#define ERR_PAGE_ADDR0 0x440
254#define ERR_PAGE_ADDR0__VALUE 0xffff 254#define ERR_PAGE_ADDR0__VALUE 0xffff
255 255
256#define ERR_BLOCK_ADDR0 0x450 256#define ERR_BLOCK_ADDR0 0x450
257#define ERR_BLOCK_ADDR0__VALUE 0xffff 257#define ERR_BLOCK_ADDR0__VALUE 0xffff
258 258
259#define INTR_STATUS1 0x460 259#define INTR_STATUS1 0x460
@@ -280,12 +280,12 @@
280#define INTR_EN1__DMA_CMD_COMP 0x0004 280#define INTR_EN1__DMA_CMD_COMP 0x0004
281#define INTR_EN1__TIME_OUT 0x0008 281#define INTR_EN1__TIME_OUT 0x0008
282#define INTR_EN1__PROGRAM_FAIL 0x0010 282#define INTR_EN1__PROGRAM_FAIL 0x0010
283#define INTR_EN1__ERASE_FAIL 0x0020 283#define INTR_EN1__ERASE_FAIL 0x0020
284#define INTR_EN1__LOAD_COMP 0x0040 284#define INTR_EN1__LOAD_COMP 0x0040
285#define INTR_EN1__PROGRAM_COMP 0x0080 285#define INTR_EN1__PROGRAM_COMP 0x0080
286#define INTR_EN1__ERASE_COMP 0x0100 286#define INTR_EN1__ERASE_COMP 0x0100
287#define INTR_EN1__PIPE_CPYBCK_CMD_COMP 0x0200 287#define INTR_EN1__PIPE_CPYBCK_CMD_COMP 0x0200
288#define INTR_EN1__LOCKED_BLK 0x0400 288#define INTR_EN1__LOCKED_BLK 0x0400
289#define INTR_EN1__UNSUP_CMD 0x0800 289#define INTR_EN1__UNSUP_CMD 0x0800
290#define INTR_EN1__INT_ACT 0x1000 290#define INTR_EN1__INT_ACT 0x1000
291#define INTR_EN1__RST_COMP 0x2000 291#define INTR_EN1__RST_COMP 0x2000
@@ -298,7 +298,7 @@
298#define ERR_PAGE_ADDR1 0x490 298#define ERR_PAGE_ADDR1 0x490
299#define ERR_PAGE_ADDR1__VALUE 0xffff 299#define ERR_PAGE_ADDR1__VALUE 0xffff
300 300
301#define ERR_BLOCK_ADDR1 0x4a0 301#define ERR_BLOCK_ADDR1 0x4a0
302#define ERR_BLOCK_ADDR1__VALUE 0xffff 302#define ERR_BLOCK_ADDR1__VALUE 0xffff
303 303
304#define INTR_STATUS2 0x4b0 304#define INTR_STATUS2 0x4b0
@@ -325,12 +325,12 @@
325#define INTR_EN2__DMA_CMD_COMP 0x0004 325#define INTR_EN2__DMA_CMD_COMP 0x0004
326#define INTR_EN2__TIME_OUT 0x0008 326#define INTR_EN2__TIME_OUT 0x0008
327#define INTR_EN2__PROGRAM_FAIL 0x0010 327#define INTR_EN2__PROGRAM_FAIL 0x0010
328#define INTR_EN2__ERASE_FAIL 0x0020 328#define INTR_EN2__ERASE_FAIL 0x0020
329#define INTR_EN2__LOAD_COMP 0x0040 329#define INTR_EN2__LOAD_COMP 0x0040
330#define INTR_EN2__PROGRAM_COMP 0x0080 330#define INTR_EN2__PROGRAM_COMP 0x0080
331#define INTR_EN2__ERASE_COMP 0x0100 331#define INTR_EN2__ERASE_COMP 0x0100
332#define INTR_EN2__PIPE_CPYBCK_CMD_COMP 0x0200 332#define INTR_EN2__PIPE_CPYBCK_CMD_COMP 0x0200
333#define INTR_EN2__LOCKED_BLK 0x0400 333#define INTR_EN2__LOCKED_BLK 0x0400
334#define INTR_EN2__UNSUP_CMD 0x0800 334#define INTR_EN2__UNSUP_CMD 0x0800
335#define INTR_EN2__INT_ACT 0x1000 335#define INTR_EN2__INT_ACT 0x1000
336#define INTR_EN2__RST_COMP 0x2000 336#define INTR_EN2__RST_COMP 0x2000
@@ -343,7 +343,7 @@
343#define ERR_PAGE_ADDR2 0x4e0 343#define ERR_PAGE_ADDR2 0x4e0
344#define ERR_PAGE_ADDR2__VALUE 0xffff 344#define ERR_PAGE_ADDR2__VALUE 0xffff
345 345
346#define ERR_BLOCK_ADDR2 0x4f0 346#define ERR_BLOCK_ADDR2 0x4f0
347#define ERR_BLOCK_ADDR2__VALUE 0xffff 347#define ERR_BLOCK_ADDR2__VALUE 0xffff
348 348
349#define INTR_STATUS3 0x500 349#define INTR_STATUS3 0x500
@@ -370,12 +370,12 @@
370#define INTR_EN3__DMA_CMD_COMP 0x0004 370#define INTR_EN3__DMA_CMD_COMP 0x0004
371#define INTR_EN3__TIME_OUT 0x0008 371#define INTR_EN3__TIME_OUT 0x0008
372#define INTR_EN3__PROGRAM_FAIL 0x0010 372#define INTR_EN3__PROGRAM_FAIL 0x0010
373#define INTR_EN3__ERASE_FAIL 0x0020 373#define INTR_EN3__ERASE_FAIL 0x0020
374#define INTR_EN3__LOAD_COMP 0x0040 374#define INTR_EN3__LOAD_COMP 0x0040
375#define INTR_EN3__PROGRAM_COMP 0x0080 375#define INTR_EN3__PROGRAM_COMP 0x0080
376#define INTR_EN3__ERASE_COMP 0x0100 376#define INTR_EN3__ERASE_COMP 0x0100
377#define INTR_EN3__PIPE_CPYBCK_CMD_COMP 0x0200 377#define INTR_EN3__PIPE_CPYBCK_CMD_COMP 0x0200
378#define INTR_EN3__LOCKED_BLK 0x0400 378#define INTR_EN3__LOCKED_BLK 0x0400
379#define INTR_EN3__UNSUP_CMD 0x0800 379#define INTR_EN3__UNSUP_CMD 0x0800
380#define INTR_EN3__INT_ACT 0x1000 380#define INTR_EN3__INT_ACT 0x1000
381#define INTR_EN3__RST_COMP 0x2000 381#define INTR_EN3__RST_COMP 0x2000
@@ -388,7 +388,7 @@
388#define ERR_PAGE_ADDR3 0x530 388#define ERR_PAGE_ADDR3 0x530
389#define ERR_PAGE_ADDR3__VALUE 0xffff 389#define ERR_PAGE_ADDR3__VALUE 0xffff
390 390
391#define ERR_BLOCK_ADDR3 0x540 391#define ERR_BLOCK_ADDR3 0x540
392#define ERR_BLOCK_ADDR3__VALUE 0xffff 392#define ERR_BLOCK_ADDR3__VALUE 0xffff
393 393
394#define DATA_INTR 0x550 394#define DATA_INTR 0x550
@@ -412,9 +412,9 @@
412#define GPREG_3__VALUE 0xffff 412#define GPREG_3__VALUE 0xffff
413 413
414#define ECC_THRESHOLD 0x600 414#define ECC_THRESHOLD 0x600
415#define ECC_THRESHOLD__VALUE 0x03ff 415#define ECC_THRESHOLD__VALUE 0x03ff
416 416
417#define ECC_ERROR_BLOCK_ADDRESS 0x610 417#define ECC_ERROR_BLOCK_ADDRESS 0x610
418#define ECC_ERROR_BLOCK_ADDRESS__VALUE 0xffff 418#define ECC_ERROR_BLOCK_ADDRESS__VALUE 0xffff
419 419
420#define ECC_ERROR_PAGE_ADDRESS 0x620 420#define ECC_ERROR_PAGE_ADDRESS 0x620
@@ -466,7 +466,7 @@
466#define CHNL_ACTIVE__CHANNEL3 0x0008 466#define CHNL_ACTIVE__CHANNEL3 0x0008
467 467
468#define ACTIVE_SRC_ID 0x800 468#define ACTIVE_SRC_ID 0x800
469#define ACTIVE_SRC_ID__VALUE 0x00ff 469#define ACTIVE_SRC_ID__VALUE 0x00ff
470 470
471#define PTN_INTR 0x810 471#define PTN_INTR 0x810
472#define PTN_INTR__CONFIG_ERROR 0x0001 472#define PTN_INTR__CONFIG_ERROR 0x0001
@@ -485,7 +485,7 @@
485#define PTN_INTR_EN__REG_ACCESS_ERROR 0x0020 485#define PTN_INTR_EN__REG_ACCESS_ERROR 0x0020
486 486
487#define PERM_SRC_ID_0 0x830 487#define PERM_SRC_ID_0 0x830
488#define PERM_SRC_ID_0__SRCID 0x00ff 488#define PERM_SRC_ID_0__SRCID 0x00ff
489#define PERM_SRC_ID_0__DIRECT_ACCESS_ACTIVE 0x0800 489#define PERM_SRC_ID_0__DIRECT_ACCESS_ACTIVE 0x0800
490#define PERM_SRC_ID_0__WRITE_ACTIVE 0x2000 490#define PERM_SRC_ID_0__WRITE_ACTIVE 0x2000
491#define PERM_SRC_ID_0__READ_ACTIVE 0x4000 491#define PERM_SRC_ID_0__READ_ACTIVE 0x4000
@@ -502,7 +502,7 @@
502#define MIN_MAX_BANK_0__MAX_VALUE 0x000c 502#define MIN_MAX_BANK_0__MAX_VALUE 0x000c
503 503
504#define PERM_SRC_ID_1 0x870 504#define PERM_SRC_ID_1 0x870
505#define PERM_SRC_ID_1__SRCID 0x00ff 505#define PERM_SRC_ID_1__SRCID 0x00ff
506#define PERM_SRC_ID_1__DIRECT_ACCESS_ACTIVE 0x0800 506#define PERM_SRC_ID_1__DIRECT_ACCESS_ACTIVE 0x0800
507#define PERM_SRC_ID_1__WRITE_ACTIVE 0x2000 507#define PERM_SRC_ID_1__WRITE_ACTIVE 0x2000
508#define PERM_SRC_ID_1__READ_ACTIVE 0x4000 508#define PERM_SRC_ID_1__READ_ACTIVE 0x4000
@@ -519,7 +519,7 @@
519#define MIN_MAX_BANK_1__MAX_VALUE 0x000c 519#define MIN_MAX_BANK_1__MAX_VALUE 0x000c
520 520
521#define PERM_SRC_ID_2 0x8b0 521#define PERM_SRC_ID_2 0x8b0
522#define PERM_SRC_ID_2__SRCID 0x00ff 522#define PERM_SRC_ID_2__SRCID 0x00ff
523#define PERM_SRC_ID_2__DIRECT_ACCESS_ACTIVE 0x0800 523#define PERM_SRC_ID_2__DIRECT_ACCESS_ACTIVE 0x0800
524#define PERM_SRC_ID_2__WRITE_ACTIVE 0x2000 524#define PERM_SRC_ID_2__WRITE_ACTIVE 0x2000
525#define PERM_SRC_ID_2__READ_ACTIVE 0x4000 525#define PERM_SRC_ID_2__READ_ACTIVE 0x4000
@@ -536,7 +536,7 @@
536#define MIN_MAX_BANK_2__MAX_VALUE 0x000c 536#define MIN_MAX_BANK_2__MAX_VALUE 0x000c
537 537
538#define PERM_SRC_ID_3 0x8f0 538#define PERM_SRC_ID_3 0x8f0
539#define PERM_SRC_ID_3__SRCID 0x00ff 539#define PERM_SRC_ID_3__SRCID 0x00ff
540#define PERM_SRC_ID_3__DIRECT_ACCESS_ACTIVE 0x0800 540#define PERM_SRC_ID_3__DIRECT_ACCESS_ACTIVE 0x0800
541#define PERM_SRC_ID_3__WRITE_ACTIVE 0x2000 541#define PERM_SRC_ID_3__WRITE_ACTIVE 0x2000
542#define PERM_SRC_ID_3__READ_ACTIVE 0x4000 542#define PERM_SRC_ID_3__READ_ACTIVE 0x4000
@@ -553,7 +553,7 @@
553#define MIN_MAX_BANK_3__MAX_VALUE 0x000c 553#define MIN_MAX_BANK_3__MAX_VALUE 0x000c
554 554
555#define PERM_SRC_ID_4 0x930 555#define PERM_SRC_ID_4 0x930
556#define PERM_SRC_ID_4__SRCID 0x00ff 556#define PERM_SRC_ID_4__SRCID 0x00ff
557#define PERM_SRC_ID_4__DIRECT_ACCESS_ACTIVE 0x0800 557#define PERM_SRC_ID_4__DIRECT_ACCESS_ACTIVE 0x0800
558#define PERM_SRC_ID_4__WRITE_ACTIVE 0x2000 558#define PERM_SRC_ID_4__WRITE_ACTIVE 0x2000
559#define PERM_SRC_ID_4__READ_ACTIVE 0x4000 559#define PERM_SRC_ID_4__READ_ACTIVE 0x4000
@@ -570,7 +570,7 @@
570#define MIN_MAX_BANK_4__MAX_VALUE 0x000c 570#define MIN_MAX_BANK_4__MAX_VALUE 0x000c
571 571
572#define PERM_SRC_ID_5 0x970 572#define PERM_SRC_ID_5 0x970
573#define PERM_SRC_ID_5__SRCID 0x00ff 573#define PERM_SRC_ID_5__SRCID 0x00ff
574#define PERM_SRC_ID_5__DIRECT_ACCESS_ACTIVE 0x0800 574#define PERM_SRC_ID_5__DIRECT_ACCESS_ACTIVE 0x0800
575#define PERM_SRC_ID_5__WRITE_ACTIVE 0x2000 575#define PERM_SRC_ID_5__WRITE_ACTIVE 0x2000
576#define PERM_SRC_ID_5__READ_ACTIVE 0x4000 576#define PERM_SRC_ID_5__READ_ACTIVE 0x4000
@@ -587,7 +587,7 @@
587#define MIN_MAX_BANK_5__MAX_VALUE 0x000c 587#define MIN_MAX_BANK_5__MAX_VALUE 0x000c
588 588
589#define PERM_SRC_ID_6 0x9b0 589#define PERM_SRC_ID_6 0x9b0
590#define PERM_SRC_ID_6__SRCID 0x00ff 590#define PERM_SRC_ID_6__SRCID 0x00ff
591#define PERM_SRC_ID_6__DIRECT_ACCESS_ACTIVE 0x0800 591#define PERM_SRC_ID_6__DIRECT_ACCESS_ACTIVE 0x0800
592#define PERM_SRC_ID_6__WRITE_ACTIVE 0x2000 592#define PERM_SRC_ID_6__WRITE_ACTIVE 0x2000
593#define PERM_SRC_ID_6__READ_ACTIVE 0x4000 593#define PERM_SRC_ID_6__READ_ACTIVE 0x4000
@@ -604,7 +604,7 @@
604#define MIN_MAX_BANK_6__MAX_VALUE 0x000c 604#define MIN_MAX_BANK_6__MAX_VALUE 0x000c
605 605
606#define PERM_SRC_ID_7 0x9f0 606#define PERM_SRC_ID_7 0x9f0
607#define PERM_SRC_ID_7__SRCID 0x00ff 607#define PERM_SRC_ID_7__SRCID 0x00ff
608#define PERM_SRC_ID_7__DIRECT_ACCESS_ACTIVE 0x0800 608#define PERM_SRC_ID_7__DIRECT_ACCESS_ACTIVE 0x0800
609#define PERM_SRC_ID_7__WRITE_ACTIVE 0x2000 609#define PERM_SRC_ID_7__WRITE_ACTIVE 0x2000
610#define PERM_SRC_ID_7__READ_ACTIVE 0x4000 610#define PERM_SRC_ID_7__READ_ACTIVE 0x4000
@@ -620,47 +620,6 @@
620#define MIN_MAX_BANK_7__MIN_VALUE 0x0003 620#define MIN_MAX_BANK_7__MIN_VALUE 0x0003
621#define MIN_MAX_BANK_7__MAX_VALUE 0x000c 621#define MIN_MAX_BANK_7__MAX_VALUE 0x000c
622 622
623/* flash.h */
624struct device_info_tag {
625 uint16_t wDeviceMaker;
626 uint16_t wDeviceID;
627 uint8_t bDeviceParam0;
628 uint8_t bDeviceParam1;
629 uint8_t bDeviceParam2;
630 uint32_t wDeviceType;
631 uint32_t wSpectraStartBlock;
632 uint32_t wSpectraEndBlock;
633 uint32_t wTotalBlocks;
634 uint16_t wPagesPerBlock;
635 uint16_t wPageSize;
636 uint16_t wPageDataSize;
637 uint16_t wPageSpareSize;
638 uint16_t wNumPageSpareFlag;
639 uint16_t wECCBytesPerSector;
640 uint32_t wBlockSize;
641 uint32_t wBlockDataSize;
642 uint32_t wDataBlockNum;
643 uint8_t bPlaneNum;
644 uint16_t wDeviceMainAreaSize;
645 uint16_t wDeviceSpareAreaSize;
646 uint16_t wDevicesConnected;
647 uint16_t wDeviceWidth;
648 uint16_t wHWRevision;
649 uint16_t wHWFeatures;
650
651 uint16_t wONFIDevFeatures;
652 uint16_t wONFIOptCommands;
653 uint16_t wONFITimingMode;
654 uint16_t wONFIPgmCacheTimingMode;
655
656 uint16_t MLCDevice;
657 uint16_t wSpareSkipBytes;
658
659 uint8_t nBitsInPageNumber;
660 uint8_t nBitsInPageDataSize;
661 uint8_t nBitsInBlockDataSize;
662};
663
664/* ffsdefs.h */ 623/* ffsdefs.h */
665#define CLEAR 0 /*use this to clear a field instead of "fail"*/ 624#define CLEAR 0 /*use this to clear a field instead of "fail"*/
666#define SET 1 /*use this to set a field instead of "pass"*/ 625#define SET 1 /*use this to set a field instead of "pass"*/
@@ -684,11 +643,11 @@ struct device_info_tag {
684#define NAND_DBG_TRACE 3 643#define NAND_DBG_TRACE 3
685 644
686#ifdef VERBOSE 645#ifdef VERBOSE
687#define nand_dbg_print(level, args...) \ 646#define nand_dbg_print(level, args...) \
688 do { \ 647 do { \
689 if (level <= nand_debug_level) \ 648 if (level <= nand_debug_level) \
690 printk(KERN_ALERT args); \ 649 printk(KERN_ALERT args); \
691 } while (0) 650 } while (0)
692#else 651#else
693#define nand_dbg_print(level, args...) 652#define nand_dbg_print(level, args...)
694#endif 653#endif
@@ -772,10 +731,9 @@ struct device_info_tag {
772#define ECC_SECTOR_SIZE 512 731#define ECC_SECTOR_SIZE 512
773#define LLD_MAX_FLASH_BANKS 4 732#define LLD_MAX_FLASH_BANKS 4
774 733
775#define DENALI_BUF_SIZE NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE 734#define DENALI_BUF_SIZE (NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE)
776 735
777struct nand_buf 736struct nand_buf {
778{
779 int head; 737 int head;
780 int tail; 738 int tail;
781 uint8_t buf[DENALI_BUF_SIZE]; 739 uint8_t buf[DENALI_BUF_SIZE];
@@ -788,7 +746,6 @@ struct nand_buf
788struct denali_nand_info { 746struct denali_nand_info {
789 struct mtd_info mtd; 747 struct mtd_info mtd;
790 struct nand_chip nand; 748 struct nand_chip nand;
791 struct device_info_tag dev_info;
792 int flash_bank; /* currently selected chip */ 749 int flash_bank; /* currently selected chip */
793 int status; 750 int status;
794 int platform; 751 int platform;
@@ -806,11 +763,12 @@ struct denali_nand_info {
806 uint32_t irq_status; 763 uint32_t irq_status;
807 int irq_debug_array[32]; 764 int irq_debug_array[32];
808 int idx; 765 int idx;
809};
810 766
811static uint16_t NAND_Flash_Reset(struct denali_nand_info *denali); 767 uint32_t devnum; /* represent how many nands connected */
812static uint16_t NAND_Read_Device_ID(struct denali_nand_info *denali); 768 uint32_t fwblks; /* represent how many blocks FW used */
813static void NAND_LLD_Enable_Disable_Interrupts(struct denali_nand_info *denali, uint16_t INT_ENABLE); 769 uint32_t totalblks;
770 uint32_t blksperchip;
771 uint32_t bbtskipbytes;
772};
814 773
815#endif /*_LLD_NAND_*/ 774#endif /*_LLD_NAND_*/
816
diff --git a/drivers/mtd/nand/diskonchip.c b/drivers/mtd/nand/diskonchip.c
index 47067bc9824..b7f8de7b278 100644
--- a/drivers/mtd/nand/diskonchip.c
+++ b/drivers/mtd/nand/diskonchip.c
@@ -29,7 +29,6 @@
29#include <linux/mtd/mtd.h> 29#include <linux/mtd/mtd.h>
30#include <linux/mtd/nand.h> 30#include <linux/mtd/nand.h>
31#include <linux/mtd/doc2000.h> 31#include <linux/mtd/doc2000.h>
32#include <linux/mtd/compatmac.h>
33#include <linux/mtd/partitions.h> 32#include <linux/mtd/partitions.h>
34#include <linux/mtd/inftl.h> 33#include <linux/mtd/inftl.h>
35 34
@@ -146,6 +145,7 @@ static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
146 uint8_t parity; 145 uint8_t parity;
147 uint16_t ds[4], s[5], tmp, errval[8], syn[4]; 146 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
148 147
148 memset(syn, 0, sizeof(syn));
149 /* Convert the ecc bytes into words */ 149 /* Convert the ecc bytes into words */
150 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8); 150 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
151 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6); 151 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
@@ -169,9 +169,9 @@ static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
169 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)]; 169 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
170 } 170 }
171 171
172 /* Calc s[i] = s[i] / alpha^(v + i) */ 172 /* Calc syn[i] = s[i] / alpha^(v + i) */
173 for (i = 0; i < NROOTS; i++) { 173 for (i = 0; i < NROOTS; i++) {
174 if (syn[i]) 174 if (s[i])
175 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i)); 175 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
176 } 176 }
177 /* Call the decoder library */ 177 /* Call the decoder library */
diff --git a/drivers/mtd/nand/mxc_nand.c b/drivers/mtd/nand/mxc_nand.c
index 0d76b169482..fcf8ceb277d 100644
--- a/drivers/mtd/nand/mxc_nand.c
+++ b/drivers/mtd/nand/mxc_nand.c
@@ -39,60 +39,96 @@
39 39
40#define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35()) 40#define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35())
41#define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21()) 41#define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21())
42#define nfc_is_v3_2() cpu_is_mx51()
43#define nfc_is_v3() nfc_is_v3_2()
42 44
43/* Addresses for NFC registers */ 45/* Addresses for NFC registers */
44#define NFC_BUF_SIZE 0xE00 46#define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
45#define NFC_BUF_ADDR 0xE04 47#define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
46#define NFC_FLASH_ADDR 0xE06 48#define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
47#define NFC_FLASH_CMD 0xE08 49#define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
48#define NFC_CONFIG 0xE0A 50#define NFC_V1_V2_CONFIG (host->regs + 0x0a)
49#define NFC_ECC_STATUS_RESULT 0xE0C 51#define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
50#define NFC_RSLTMAIN_AREA 0xE0E 52#define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
51#define NFC_RSLTSPARE_AREA 0xE10 53#define NFC_V1_V2_RSLTSPARE_AREA (host->regs + 0x10)
52#define NFC_WRPROT 0xE12 54#define NFC_V1_V2_WRPROT (host->regs + 0x12)
53#define NFC_V1_UNLOCKSTART_BLKADDR 0xe14 55#define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
54#define NFC_V1_UNLOCKEND_BLKADDR 0xe16 56#define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
55#define NFC_V21_UNLOCKSTART_BLKADDR 0xe20 57#define NFC_V21_UNLOCKSTART_BLKADDR (host->regs + 0x20)
56#define NFC_V21_UNLOCKEND_BLKADDR 0xe22 58#define NFC_V21_UNLOCKEND_BLKADDR (host->regs + 0x22)
57#define NFC_NF_WRPRST 0xE18 59#define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
58#define NFC_CONFIG1 0xE1A 60#define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
59#define NFC_CONFIG2 0xE1C 61#define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
60 62
61/* Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register 63#define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
62 * for Command operation */ 64#define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
63#define NFC_CMD 0x1 65#define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
64 66#define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
65/* Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register 67#define NFC_V1_V2_CONFIG1_BIG (1 << 5)
66 * for Address operation */ 68#define NFC_V1_V2_CONFIG1_RST (1 << 6)
67#define NFC_ADDR 0x2 69#define NFC_V1_V2_CONFIG1_CE (1 << 7)
68 70#define NFC_V1_V2_CONFIG1_ONE_CYCLE (1 << 8)
69/* Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register 71
70 * for Input operation */ 72#define NFC_V1_V2_CONFIG2_INT (1 << 15)
71#define NFC_INPUT 0x4 73
72 74/*
73/* Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register 75 * Operation modes for the NFC. Valid for v1, v2 and v3
74 * for Data Output operation */ 76 * type controllers.
75#define NFC_OUTPUT 0x8 77 */
76 78#define NFC_CMD (1 << 0)
77/* Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register 79#define NFC_ADDR (1 << 1)
78 * for Read ID operation */ 80#define NFC_INPUT (1 << 2)
79#define NFC_ID 0x10 81#define NFC_OUTPUT (1 << 3)
80 82#define NFC_ID (1 << 4)
81/* Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register 83#define NFC_STATUS (1 << 5)
82 * for Read Status operation */ 84
83#define NFC_STATUS 0x20 85#define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
84 86#define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
85/* Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read 87
86 * Status operation */ 88#define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
87#define NFC_INT 0x8000 89#define NFC_V3_CONFIG1_SP_EN (1 << 0)
88 90#define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
89#define NFC_SP_EN (1 << 2) 91
90#define NFC_ECC_EN (1 << 3) 92#define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
91#define NFC_INT_MSK (1 << 4) 93
92#define NFC_BIG (1 << 5) 94#define NFC_V3_LAUNCH (host->regs_axi + 0x40)
93#define NFC_RST (1 << 6) 95
94#define NFC_CE (1 << 7) 96#define NFC_V3_WRPROT (host->regs_ip + 0x0)
95#define NFC_ONE_CYCLE (1 << 8) 97#define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
98#define NFC_V3_WRPROT_LOCK (1 << 1)
99#define NFC_V3_WRPROT_UNLOCK (1 << 2)
100#define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
101
102#define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
103
104#define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
105#define NFC_V3_CONFIG2_PS_512 (0 << 0)
106#define NFC_V3_CONFIG2_PS_2048 (1 << 0)
107#define NFC_V3_CONFIG2_PS_4096 (2 << 0)
108#define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
109#define NFC_V3_CONFIG2_ECC_EN (1 << 3)
110#define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
111#define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
112#define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
113#define NFC_V3_CONFIG2_PPB(x) (((x) & 0x3) << 7)
114#define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
115#define NFC_V3_CONFIG2_INT_MSK (1 << 15)
116#define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
117#define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
118
119#define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
120#define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
121#define NFC_V3_CONFIG3_FW8 (1 << 3)
122#define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
123#define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
124#define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
125#define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
126
127#define NFC_V3_IPC (host->regs_ip + 0x2C)
128#define NFC_V3_IPC_CREQ (1 << 0)
129#define NFC_V3_IPC_INT (1 << 31)
130
131#define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
96 132
97struct mxc_nand_host { 133struct mxc_nand_host {
98 struct mtd_info mtd; 134 struct mtd_info mtd;
@@ -102,20 +138,30 @@ struct mxc_nand_host {
102 138
103 void *spare0; 139 void *spare0;
104 void *main_area0; 140 void *main_area0;
105 void *main_area1;
106 141
107 void __iomem *base; 142 void __iomem *base;
108 void __iomem *regs; 143 void __iomem *regs;
144 void __iomem *regs_axi;
145 void __iomem *regs_ip;
109 int status_request; 146 int status_request;
110 struct clk *clk; 147 struct clk *clk;
111 int clk_act; 148 int clk_act;
112 int irq; 149 int irq;
150 int eccsize;
113 151
114 wait_queue_head_t irq_waitq; 152 wait_queue_head_t irq_waitq;
115 153
116 uint8_t *data_buf; 154 uint8_t *data_buf;
117 unsigned int buf_start; 155 unsigned int buf_start;
118 int spare_len; 156 int spare_len;
157
158 void (*preset)(struct mtd_info *);
159 void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
160 void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
161 void (*send_page)(struct mtd_info *, unsigned int);
162 void (*send_read_id)(struct mxc_nand_host *);
163 uint16_t (*get_dev_status)(struct mxc_nand_host *);
164 int (*check_int)(struct mxc_nand_host *);
119}; 165};
120 166
121/* OOB placement block for use with hardware ecc generation */ 167/* OOB placement block for use with hardware ecc generation */
@@ -175,34 +221,52 @@ static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
175 return IRQ_HANDLED; 221 return IRQ_HANDLED;
176} 222}
177 223
224static int check_int_v3(struct mxc_nand_host *host)
225{
226 uint32_t tmp;
227
228 tmp = readl(NFC_V3_IPC);
229 if (!(tmp & NFC_V3_IPC_INT))
230 return 0;
231
232 tmp &= ~NFC_V3_IPC_INT;
233 writel(tmp, NFC_V3_IPC);
234
235 return 1;
236}
237
238static int check_int_v1_v2(struct mxc_nand_host *host)
239{
240 uint32_t tmp;
241
242 tmp = readw(NFC_V1_V2_CONFIG2);
243 if (!(tmp & NFC_V1_V2_CONFIG2_INT))
244 return 0;
245
246 writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
247
248 return 1;
249}
250
178/* This function polls the NANDFC to wait for the basic operation to 251/* This function polls the NANDFC to wait for the basic operation to
179 * complete by checking the INT bit of config2 register. 252 * complete by checking the INT bit of config2 register.
180 */ 253 */
181static void wait_op_done(struct mxc_nand_host *host, int useirq) 254static void wait_op_done(struct mxc_nand_host *host, int useirq)
182{ 255{
183 uint16_t tmp;
184 int max_retries = 8000; 256 int max_retries = 8000;
185 257
186 if (useirq) { 258 if (useirq) {
187 if ((readw(host->regs + NFC_CONFIG2) & NFC_INT) == 0) { 259 if (!host->check_int(host)) {
188 260
189 enable_irq(host->irq); 261 enable_irq(host->irq);
190 262
191 wait_event(host->irq_waitq, 263 wait_event(host->irq_waitq, host->check_int(host));
192 readw(host->regs + NFC_CONFIG2) & NFC_INT);
193
194 tmp = readw(host->regs + NFC_CONFIG2);
195 tmp &= ~NFC_INT;
196 writew(tmp, host->regs + NFC_CONFIG2);
197 } 264 }
198 } else { 265 } else {
199 while (max_retries-- > 0) { 266 while (max_retries-- > 0) {
200 if (readw(host->regs + NFC_CONFIG2) & NFC_INT) { 267 if (host->check_int(host))
201 tmp = readw(host->regs + NFC_CONFIG2);
202 tmp &= ~NFC_INT;
203 writew(tmp, host->regs + NFC_CONFIG2);
204 break; 268 break;
205 } 269
206 udelay(1); 270 udelay(1);
207 } 271 }
208 if (max_retries < 0) 272 if (max_retries < 0)
@@ -211,21 +275,33 @@ static void wait_op_done(struct mxc_nand_host *host, int useirq)
211 } 275 }
212} 276}
213 277
278static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
279{
280 /* fill command */
281 writel(cmd, NFC_V3_FLASH_CMD);
282
283 /* send out command */
284 writel(NFC_CMD, NFC_V3_LAUNCH);
285
286 /* Wait for operation to complete */
287 wait_op_done(host, useirq);
288}
289
214/* This function issues the specified command to the NAND device and 290/* This function issues the specified command to the NAND device and
215 * waits for completion. */ 291 * waits for completion. */
216static void send_cmd(struct mxc_nand_host *host, uint16_t cmd, int useirq) 292static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
217{ 293{
218 DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq); 294 DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
219 295
220 writew(cmd, host->regs + NFC_FLASH_CMD); 296 writew(cmd, NFC_V1_V2_FLASH_CMD);
221 writew(NFC_CMD, host->regs + NFC_CONFIG2); 297 writew(NFC_CMD, NFC_V1_V2_CONFIG2);
222 298
223 if (cpu_is_mx21() && (cmd == NAND_CMD_RESET)) { 299 if (cpu_is_mx21() && (cmd == NAND_CMD_RESET)) {
224 int max_retries = 100; 300 int max_retries = 100;
225 /* Reset completion is indicated by NFC_CONFIG2 */ 301 /* Reset completion is indicated by NFC_CONFIG2 */
226 /* being set to 0 */ 302 /* being set to 0 */
227 while (max_retries-- > 0) { 303 while (max_retries-- > 0) {
228 if (readw(host->regs + NFC_CONFIG2) == 0) { 304 if (readw(NFC_V1_V2_CONFIG2) == 0) {
229 break; 305 break;
230 } 306 }
231 udelay(1); 307 udelay(1);
@@ -239,21 +315,48 @@ static void send_cmd(struct mxc_nand_host *host, uint16_t cmd, int useirq)
239 } 315 }
240} 316}
241 317
318static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
319{
320 /* fill address */
321 writel(addr, NFC_V3_FLASH_ADDR0);
322
323 /* send out address */
324 writel(NFC_ADDR, NFC_V3_LAUNCH);
325
326 wait_op_done(host, 0);
327}
328
242/* This function sends an address (or partial address) to the 329/* This function sends an address (or partial address) to the
243 * NAND device. The address is used to select the source/destination for 330 * NAND device. The address is used to select the source/destination for
244 * a NAND command. */ 331 * a NAND command. */
245static void send_addr(struct mxc_nand_host *host, uint16_t addr, int islast) 332static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
246{ 333{
247 DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast); 334 DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
248 335
249 writew(addr, host->regs + NFC_FLASH_ADDR); 336 writew(addr, NFC_V1_V2_FLASH_ADDR);
250 writew(NFC_ADDR, host->regs + NFC_CONFIG2); 337 writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
251 338
252 /* Wait for operation to complete */ 339 /* Wait for operation to complete */
253 wait_op_done(host, islast); 340 wait_op_done(host, islast);
254} 341}
255 342
256static void send_page(struct mtd_info *mtd, unsigned int ops) 343static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
344{
345 struct nand_chip *nand_chip = mtd->priv;
346 struct mxc_nand_host *host = nand_chip->priv;
347 uint32_t tmp;
348
349 tmp = readl(NFC_V3_CONFIG1);
350 tmp &= ~(7 << 4);
351 writel(tmp, NFC_V3_CONFIG1);
352
353 /* transfer data from NFC ram to nand */
354 writel(ops, NFC_V3_LAUNCH);
355
356 wait_op_done(host, false);
357}
358
359static void send_page_v1_v2(struct mtd_info *mtd, unsigned int ops)
257{ 360{
258 struct nand_chip *nand_chip = mtd->priv; 361 struct nand_chip *nand_chip = mtd->priv;
259 struct mxc_nand_host *host = nand_chip->priv; 362 struct mxc_nand_host *host = nand_chip->priv;
@@ -267,24 +370,34 @@ static void send_page(struct mtd_info *mtd, unsigned int ops)
267 for (i = 0; i < bufs; i++) { 370 for (i = 0; i < bufs; i++) {
268 371
269 /* NANDFC buffer 0 is used for page read/write */ 372 /* NANDFC buffer 0 is used for page read/write */
270 writew(i, host->regs + NFC_BUF_ADDR); 373 writew(i, NFC_V1_V2_BUF_ADDR);
271 374
272 writew(ops, host->regs + NFC_CONFIG2); 375 writew(ops, NFC_V1_V2_CONFIG2);
273 376
274 /* Wait for operation to complete */ 377 /* Wait for operation to complete */
275 wait_op_done(host, true); 378 wait_op_done(host, true);
276 } 379 }
277} 380}
278 381
382static void send_read_id_v3(struct mxc_nand_host *host)
383{
384 /* Read ID into main buffer */
385 writel(NFC_ID, NFC_V3_LAUNCH);
386
387 wait_op_done(host, true);
388
389 memcpy(host->data_buf, host->main_area0, 16);
390}
391
279/* Request the NANDFC to perform a read of the NAND device ID. */ 392/* Request the NANDFC to perform a read of the NAND device ID. */
280static void send_read_id(struct mxc_nand_host *host) 393static void send_read_id_v1_v2(struct mxc_nand_host *host)
281{ 394{
282 struct nand_chip *this = &host->nand; 395 struct nand_chip *this = &host->nand;
283 396
284 /* NANDFC buffer 0 is used for device ID output */ 397 /* NANDFC buffer 0 is used for device ID output */
285 writew(0x0, host->regs + NFC_BUF_ADDR); 398 writew(0x0, NFC_V1_V2_BUF_ADDR);
286 399
287 writew(NFC_ID, host->regs + NFC_CONFIG2); 400 writew(NFC_ID, NFC_V1_V2_CONFIG2);
288 401
289 /* Wait for operation to complete */ 402 /* Wait for operation to complete */
290 wait_op_done(host, true); 403 wait_op_done(host, true);
@@ -301,29 +414,36 @@ static void send_read_id(struct mxc_nand_host *host)
301 memcpy(host->data_buf, host->main_area0, 16); 414 memcpy(host->data_buf, host->main_area0, 16);
302} 415}
303 416
417static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
418{
419 writew(NFC_STATUS, NFC_V3_LAUNCH);
420 wait_op_done(host, true);
421
422 return readl(NFC_V3_CONFIG1) >> 16;
423}
424
304/* This function requests the NANDFC to perform a read of the 425/* This function requests the NANDFC to perform a read of the
305 * NAND device status and returns the current status. */ 426 * NAND device status and returns the current status. */
306static uint16_t get_dev_status(struct mxc_nand_host *host) 427static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
307{ 428{
308 void __iomem *main_buf = host->main_area1; 429 void __iomem *main_buf = host->main_area0;
309 uint32_t store; 430 uint32_t store;
310 uint16_t ret; 431 uint16_t ret;
311 /* Issue status request to NAND device */
312 432
313 /* store the main area1 first word, later do recovery */ 433 writew(0x0, NFC_V1_V2_BUF_ADDR);
314 store = readl(main_buf);
315 /* NANDFC buffer 1 is used for device status to prevent
316 * corruption of read/write buffer on status requests. */
317 writew(1, host->regs + NFC_BUF_ADDR);
318 434
319 writew(NFC_STATUS, host->regs + NFC_CONFIG2); 435 /*
436 * The device status is stored in main_area0. To
437 * prevent corruption of the buffer save the value
438 * and restore it afterwards.
439 */
440 store = readl(main_buf);
320 441
321 /* Wait for operation to complete */ 442 writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
322 wait_op_done(host, true); 443 wait_op_done(host, true);
323 444
324 /* Status is placed in first word of main buffer */
325 /* get status, then recovery area 1 data */
326 ret = readw(main_buf); 445 ret = readw(main_buf);
446
327 writel(store, main_buf); 447 writel(store, main_buf);
328 448
329 return ret; 449 return ret;
@@ -347,7 +467,7 @@ static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
347 */ 467 */
348} 468}
349 469
350static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat, 470static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
351 u_char *read_ecc, u_char *calc_ecc) 471 u_char *read_ecc, u_char *calc_ecc)
352{ 472{
353 struct nand_chip *nand_chip = mtd->priv; 473 struct nand_chip *nand_chip = mtd->priv;
@@ -358,7 +478,7 @@ static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
358 * additional correction. 2-Bit errors cannot be corrected by 478 * additional correction. 2-Bit errors cannot be corrected by
359 * HW ECC, so we need to return failure 479 * HW ECC, so we need to return failure
360 */ 480 */
361 uint16_t ecc_status = readw(host->regs + NFC_ECC_STATUS_RESULT); 481 uint16_t ecc_status = readw(NFC_V1_V2_ECC_STATUS_RESULT);
362 482
363 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) { 483 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
364 DEBUG(MTD_DEBUG_LEVEL0, 484 DEBUG(MTD_DEBUG_LEVEL0,
@@ -369,6 +489,43 @@ static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
369 return 0; 489 return 0;
370} 490}
371 491
492static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
493 u_char *read_ecc, u_char *calc_ecc)
494{
495 struct nand_chip *nand_chip = mtd->priv;
496 struct mxc_nand_host *host = nand_chip->priv;
497 u32 ecc_stat, err;
498 int no_subpages = 1;
499 int ret = 0;
500 u8 ecc_bit_mask, err_limit;
501
502 ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
503 err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
504
505 no_subpages = mtd->writesize >> 9;
506
507 if (nfc_is_v21())
508 ecc_stat = readl(NFC_V1_V2_ECC_STATUS_RESULT);
509 else
510 ecc_stat = readl(NFC_V3_ECC_STATUS_RESULT);
511
512 do {
513 err = ecc_stat & ecc_bit_mask;
514 if (err > err_limit) {
515 printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
516 return -1;
517 } else {
518 ret += err;
519 }
520 ecc_stat >>= 4;
521 } while (--no_subpages);
522
523 mtd->ecc_stats.corrected += ret;
524 pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
525
526 return ret;
527}
528
372static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, 529static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
373 u_char *ecc_code) 530 u_char *ecc_code)
374{ 531{
@@ -383,7 +540,7 @@ static u_char mxc_nand_read_byte(struct mtd_info *mtd)
383 540
384 /* Check for status request */ 541 /* Check for status request */
385 if (host->status_request) 542 if (host->status_request)
386 return get_dev_status(host) & 0xFF; 543 return host->get_dev_status(host) & 0xFF;
387 544
388 ret = *(uint8_t *)(host->data_buf + host->buf_start); 545 ret = *(uint8_t *)(host->data_buf + host->buf_start);
389 host->buf_start++; 546 host->buf_start++;
@@ -519,71 +676,163 @@ static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
519 * we will used the saved column address to index into 676 * we will used the saved column address to index into
520 * the full page. 677 * the full page.
521 */ 678 */
522 send_addr(host, 0, page_addr == -1); 679 host->send_addr(host, 0, page_addr == -1);
523 if (mtd->writesize > 512) 680 if (mtd->writesize > 512)
524 /* another col addr cycle for 2k page */ 681 /* another col addr cycle for 2k page */
525 send_addr(host, 0, false); 682 host->send_addr(host, 0, false);
526 } 683 }
527 684
528 /* Write out page address, if necessary */ 685 /* Write out page address, if necessary */
529 if (page_addr != -1) { 686 if (page_addr != -1) {
530 /* paddr_0 - p_addr_7 */ 687 /* paddr_0 - p_addr_7 */
531 send_addr(host, (page_addr & 0xff), false); 688 host->send_addr(host, (page_addr & 0xff), false);
532 689
533 if (mtd->writesize > 512) { 690 if (mtd->writesize > 512) {
534 if (mtd->size >= 0x10000000) { 691 if (mtd->size >= 0x10000000) {
535 /* paddr_8 - paddr_15 */ 692 /* paddr_8 - paddr_15 */
536 send_addr(host, (page_addr >> 8) & 0xff, false); 693 host->send_addr(host, (page_addr >> 8) & 0xff, false);
537 send_addr(host, (page_addr >> 16) & 0xff, true); 694 host->send_addr(host, (page_addr >> 16) & 0xff, true);
538 } else 695 } else
539 /* paddr_8 - paddr_15 */ 696 /* paddr_8 - paddr_15 */
540 send_addr(host, (page_addr >> 8) & 0xff, true); 697 host->send_addr(host, (page_addr >> 8) & 0xff, true);
541 } else { 698 } else {
542 /* One more address cycle for higher density devices */ 699 /* One more address cycle for higher density devices */
543 if (mtd->size >= 0x4000000) { 700 if (mtd->size >= 0x4000000) {
544 /* paddr_8 - paddr_15 */ 701 /* paddr_8 - paddr_15 */
545 send_addr(host, (page_addr >> 8) & 0xff, false); 702 host->send_addr(host, (page_addr >> 8) & 0xff, false);
546 send_addr(host, (page_addr >> 16) & 0xff, true); 703 host->send_addr(host, (page_addr >> 16) & 0xff, true);
547 } else 704 } else
548 /* paddr_8 - paddr_15 */ 705 /* paddr_8 - paddr_15 */
549 send_addr(host, (page_addr >> 8) & 0xff, true); 706 host->send_addr(host, (page_addr >> 8) & 0xff, true);
550 } 707 }
551 } 708 }
552} 709}
553 710
554static void preset(struct mtd_info *mtd) 711/*
712 * v2 and v3 type controllers can do 4bit or 8bit ecc depending
713 * on how much oob the nand chip has. For 8bit ecc we need at least
714 * 26 bytes of oob data per 512 byte block.
715 */
716static int get_eccsize(struct mtd_info *mtd)
717{
718 int oobbytes_per_512 = 0;
719
720 oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
721
722 if (oobbytes_per_512 < 26)
723 return 4;
724 else
725 return 8;
726}
727
728static void preset_v1_v2(struct mtd_info *mtd)
555{ 729{
556 struct nand_chip *nand_chip = mtd->priv; 730 struct nand_chip *nand_chip = mtd->priv;
557 struct mxc_nand_host *host = nand_chip->priv; 731 struct mxc_nand_host *host = nand_chip->priv;
558 uint16_t tmp; 732 uint16_t tmp;
559 733
560 /* enable interrupt, disable spare enable */ 734 /* enable interrupt, disable spare enable */
561 tmp = readw(host->regs + NFC_CONFIG1); 735 tmp = readw(NFC_V1_V2_CONFIG1);
562 tmp &= ~NFC_INT_MSK; 736 tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
563 tmp &= ~NFC_SP_EN; 737 tmp &= ~NFC_V1_V2_CONFIG1_SP_EN;
564 if (nand_chip->ecc.mode == NAND_ECC_HW) { 738 if (nand_chip->ecc.mode == NAND_ECC_HW) {
565 tmp |= NFC_ECC_EN; 739 tmp |= NFC_V1_V2_CONFIG1_ECC_EN;
740 } else {
741 tmp &= ~NFC_V1_V2_CONFIG1_ECC_EN;
742 }
743
744 if (nfc_is_v21() && mtd->writesize) {
745 host->eccsize = get_eccsize(mtd);
746 if (host->eccsize == 4)
747 tmp |= NFC_V2_CONFIG1_ECC_MODE_4;
566 } else { 748 } else {
567 tmp &= ~NFC_ECC_EN; 749 host->eccsize = 1;
568 } 750 }
569 writew(tmp, host->regs + NFC_CONFIG1); 751
752 writew(tmp, NFC_V1_V2_CONFIG1);
570 /* preset operation */ 753 /* preset operation */
571 754
572 /* Unlock the internal RAM Buffer */ 755 /* Unlock the internal RAM Buffer */
573 writew(0x2, host->regs + NFC_CONFIG); 756 writew(0x2, NFC_V1_V2_CONFIG);
574 757
575 /* Blocks to be unlocked */ 758 /* Blocks to be unlocked */
576 if (nfc_is_v21()) { 759 if (nfc_is_v21()) {
577 writew(0x0, host->regs + NFC_V21_UNLOCKSTART_BLKADDR); 760 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR);
578 writew(0xffff, host->regs + NFC_V21_UNLOCKEND_BLKADDR); 761 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR);
579 } else if (nfc_is_v1()) { 762 } else if (nfc_is_v1()) {
580 writew(0x0, host->regs + NFC_V1_UNLOCKSTART_BLKADDR); 763 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
581 writew(0x4000, host->regs + NFC_V1_UNLOCKEND_BLKADDR); 764 writew(0x4000, NFC_V1_UNLOCKEND_BLKADDR);
582 } else 765 } else
583 BUG(); 766 BUG();
584 767
585 /* Unlock Block Command for given address range */ 768 /* Unlock Block Command for given address range */
586 writew(0x4, host->regs + NFC_WRPROT); 769 writew(0x4, NFC_V1_V2_WRPROT);
770}
771
772static void preset_v3(struct mtd_info *mtd)
773{
774 struct nand_chip *chip = mtd->priv;
775 struct mxc_nand_host *host = chip->priv;
776 uint32_t config2, config3;
777 int i, addr_phases;
778
779 writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
780 writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
781
782 /* Unlock the internal RAM Buffer */
783 writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
784 NFC_V3_WRPROT);
785
786 /* Blocks to be unlocked */
787 for (i = 0; i < NAND_MAX_CHIPS; i++)
788 writel(0x0 | (0xffff << 16),
789 NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
790
791 writel(0, NFC_V3_IPC);
792
793 config2 = NFC_V3_CONFIG2_ONE_CYCLE |
794 NFC_V3_CONFIG2_2CMD_PHASES |
795 NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
796 NFC_V3_CONFIG2_ST_CMD(0x70) |
797 NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
798
799 if (chip->ecc.mode == NAND_ECC_HW)
800 config2 |= NFC_V3_CONFIG2_ECC_EN;
801
802 addr_phases = fls(chip->pagemask) >> 3;
803
804 if (mtd->writesize == 2048) {
805 config2 |= NFC_V3_CONFIG2_PS_2048;
806 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
807 } else if (mtd->writesize == 4096) {
808 config2 |= NFC_V3_CONFIG2_PS_4096;
809 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
810 } else {
811 config2 |= NFC_V3_CONFIG2_PS_512;
812 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
813 }
814
815 if (mtd->writesize) {
816 config2 |= NFC_V3_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6);
817 host->eccsize = get_eccsize(mtd);
818 if (host->eccsize == 8)
819 config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
820 }
821
822 writel(config2, NFC_V3_CONFIG2);
823
824 config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
825 NFC_V3_CONFIG3_NO_SDMA |
826 NFC_V3_CONFIG3_RBB_MODE |
827 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
828 NFC_V3_CONFIG3_ADD_OP(0);
829
830 if (!(chip->options & NAND_BUSWIDTH_16))
831 config3 |= NFC_V3_CONFIG3_FW8;
832
833 writel(config3, NFC_V3_CONFIG3);
834
835 writel(0, NFC_V3_DELAY_LINE);
587} 836}
588 837
589/* Used by the upper layer to write command to NAND Flash for 838/* Used by the upper layer to write command to NAND Flash for
@@ -604,15 +853,15 @@ static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
604 /* Command pre-processing step */ 853 /* Command pre-processing step */
605 switch (command) { 854 switch (command) {
606 case NAND_CMD_RESET: 855 case NAND_CMD_RESET:
607 send_cmd(host, command, false); 856 host->preset(mtd);
608 preset(mtd); 857 host->send_cmd(host, command, false);
609 break; 858 break;
610 859
611 case NAND_CMD_STATUS: 860 case NAND_CMD_STATUS:
612 host->buf_start = 0; 861 host->buf_start = 0;
613 host->status_request = true; 862 host->status_request = true;
614 863
615 send_cmd(host, command, true); 864 host->send_cmd(host, command, true);
616 mxc_do_addr_cycle(mtd, column, page_addr); 865 mxc_do_addr_cycle(mtd, column, page_addr);
617 break; 866 break;
618 867
@@ -625,13 +874,13 @@ static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
625 874
626 command = NAND_CMD_READ0; /* only READ0 is valid */ 875 command = NAND_CMD_READ0; /* only READ0 is valid */
627 876
628 send_cmd(host, command, false); 877 host->send_cmd(host, command, false);
629 mxc_do_addr_cycle(mtd, column, page_addr); 878 mxc_do_addr_cycle(mtd, column, page_addr);
630 879
631 if (mtd->writesize > 512) 880 if (mtd->writesize > 512)
632 send_cmd(host, NAND_CMD_READSTART, true); 881 host->send_cmd(host, NAND_CMD_READSTART, true);
633 882
634 send_page(mtd, NFC_OUTPUT); 883 host->send_page(mtd, NFC_OUTPUT);
635 884
636 memcpy(host->data_buf, host->main_area0, mtd->writesize); 885 memcpy(host->data_buf, host->main_area0, mtd->writesize);
637 copy_spare(mtd, true); 886 copy_spare(mtd, true);
@@ -644,28 +893,28 @@ static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
644 893
645 host->buf_start = column; 894 host->buf_start = column;
646 895
647 send_cmd(host, command, false); 896 host->send_cmd(host, command, false);
648 mxc_do_addr_cycle(mtd, column, page_addr); 897 mxc_do_addr_cycle(mtd, column, page_addr);
649 break; 898 break;
650 899
651 case NAND_CMD_PAGEPROG: 900 case NAND_CMD_PAGEPROG:
652 memcpy(host->main_area0, host->data_buf, mtd->writesize); 901 memcpy(host->main_area0, host->data_buf, mtd->writesize);
653 copy_spare(mtd, false); 902 copy_spare(mtd, false);
654 send_page(mtd, NFC_INPUT); 903 host->send_page(mtd, NFC_INPUT);
655 send_cmd(host, command, true); 904 host->send_cmd(host, command, true);
656 mxc_do_addr_cycle(mtd, column, page_addr); 905 mxc_do_addr_cycle(mtd, column, page_addr);
657 break; 906 break;
658 907
659 case NAND_CMD_READID: 908 case NAND_CMD_READID:
660 send_cmd(host, command, true); 909 host->send_cmd(host, command, true);
661 mxc_do_addr_cycle(mtd, column, page_addr); 910 mxc_do_addr_cycle(mtd, column, page_addr);
662 send_read_id(host); 911 host->send_read_id(host);
663 host->buf_start = column; 912 host->buf_start = column;
664 break; 913 break;
665 914
666 case NAND_CMD_ERASE1: 915 case NAND_CMD_ERASE1:
667 case NAND_CMD_ERASE2: 916 case NAND_CMD_ERASE2:
668 send_cmd(host, command, false); 917 host->send_cmd(host, command, false);
669 mxc_do_addr_cycle(mtd, column, page_addr); 918 mxc_do_addr_cycle(mtd, column, page_addr);
670 919
671 break; 920 break;
@@ -761,22 +1010,55 @@ static int __init mxcnd_probe(struct platform_device *pdev)
761 } 1010 }
762 1011
763 host->main_area0 = host->base; 1012 host->main_area0 = host->base;
764 host->main_area1 = host->base + 0x200; 1013
1014 if (nfc_is_v1() || nfc_is_v21()) {
1015 host->preset = preset_v1_v2;
1016 host->send_cmd = send_cmd_v1_v2;
1017 host->send_addr = send_addr_v1_v2;
1018 host->send_page = send_page_v1_v2;
1019 host->send_read_id = send_read_id_v1_v2;
1020 host->get_dev_status = get_dev_status_v1_v2;
1021 host->check_int = check_int_v1_v2;
1022 }
765 1023
766 if (nfc_is_v21()) { 1024 if (nfc_is_v21()) {
767 host->regs = host->base + 0x1000; 1025 host->regs = host->base + 0x1e00;
768 host->spare0 = host->base + 0x1000; 1026 host->spare0 = host->base + 0x1000;
769 host->spare_len = 64; 1027 host->spare_len = 64;
770 oob_smallpage = &nandv2_hw_eccoob_smallpage; 1028 oob_smallpage = &nandv2_hw_eccoob_smallpage;
771 oob_largepage = &nandv2_hw_eccoob_largepage; 1029 oob_largepage = &nandv2_hw_eccoob_largepage;
772 this->ecc.bytes = 9; 1030 this->ecc.bytes = 9;
773 } else if (nfc_is_v1()) { 1031 } else if (nfc_is_v1()) {
774 host->regs = host->base; 1032 host->regs = host->base + 0xe00;
775 host->spare0 = host->base + 0x800; 1033 host->spare0 = host->base + 0x800;
776 host->spare_len = 16; 1034 host->spare_len = 16;
777 oob_smallpage = &nandv1_hw_eccoob_smallpage; 1035 oob_smallpage = &nandv1_hw_eccoob_smallpage;
778 oob_largepage = &nandv1_hw_eccoob_largepage; 1036 oob_largepage = &nandv1_hw_eccoob_largepage;
779 this->ecc.bytes = 3; 1037 this->ecc.bytes = 3;
1038 host->eccsize = 1;
1039 } else if (nfc_is_v3_2()) {
1040 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1041 if (!res) {
1042 err = -ENODEV;
1043 goto eirq;
1044 }
1045 host->regs_ip = ioremap(res->start, resource_size(res));
1046 if (!host->regs_ip) {
1047 err = -ENOMEM;
1048 goto eirq;
1049 }
1050 host->regs_axi = host->base + 0x1e00;
1051 host->spare0 = host->base + 0x1000;
1052 host->spare_len = 64;
1053 host->preset = preset_v3;
1054 host->send_cmd = send_cmd_v3;
1055 host->send_addr = send_addr_v3;
1056 host->send_page = send_page_v3;
1057 host->send_read_id = send_read_id_v3;
1058 host->check_int = check_int_v3;
1059 host->get_dev_status = get_dev_status_v3;
1060 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1061 oob_largepage = &nandv2_hw_eccoob_largepage;
780 } else 1062 } else
781 BUG(); 1063 BUG();
782 1064
@@ -786,7 +1068,10 @@ static int __init mxcnd_probe(struct platform_device *pdev)
786 if (pdata->hw_ecc) { 1068 if (pdata->hw_ecc) {
787 this->ecc.calculate = mxc_nand_calculate_ecc; 1069 this->ecc.calculate = mxc_nand_calculate_ecc;
788 this->ecc.hwctl = mxc_nand_enable_hwecc; 1070 this->ecc.hwctl = mxc_nand_enable_hwecc;
789 this->ecc.correct = mxc_nand_correct_data; 1071 if (nfc_is_v1())
1072 this->ecc.correct = mxc_nand_correct_data_v1;
1073 else
1074 this->ecc.correct = mxc_nand_correct_data_v2_v3;
790 this->ecc.mode = NAND_ECC_HW; 1075 this->ecc.mode = NAND_ECC_HW;
791 } else { 1076 } else {
792 this->ecc.mode = NAND_ECC_SOFT; 1077 this->ecc.mode = NAND_ECC_SOFT;
@@ -817,6 +1102,9 @@ static int __init mxcnd_probe(struct platform_device *pdev)
817 goto escan; 1102 goto escan;
818 } 1103 }
819 1104
1105 /* Call preset again, with correct writesize this time */
1106 host->preset(mtd);
1107
820 if (mtd->writesize == 2048) 1108 if (mtd->writesize == 2048)
821 this->ecc.layout = oob_largepage; 1109 this->ecc.layout = oob_largepage;
822 1110
@@ -848,6 +1136,8 @@ static int __init mxcnd_probe(struct platform_device *pdev)
848escan: 1136escan:
849 free_irq(host->irq, host); 1137 free_irq(host->irq, host);
850eirq: 1138eirq:
1139 if (host->regs_ip)
1140 iounmap(host->regs_ip);
851 iounmap(host->base); 1141 iounmap(host->base);
852eres: 1142eres:
853 clk_put(host->clk); 1143 clk_put(host->clk);
@@ -867,59 +1157,19 @@ static int __devexit mxcnd_remove(struct platform_device *pdev)
867 1157
868 nand_release(&host->mtd); 1158 nand_release(&host->mtd);
869 free_irq(host->irq, host); 1159 free_irq(host->irq, host);
1160 if (host->regs_ip)
1161 iounmap(host->regs_ip);
870 iounmap(host->base); 1162 iounmap(host->base);
871 kfree(host); 1163 kfree(host);
872 1164
873 return 0; 1165 return 0;
874} 1166}
875 1167
876#ifdef CONFIG_PM
877static int mxcnd_suspend(struct platform_device *pdev, pm_message_t state)
878{
879 struct mtd_info *mtd = platform_get_drvdata(pdev);
880 struct nand_chip *nand_chip = mtd->priv;
881 struct mxc_nand_host *host = nand_chip->priv;
882 int ret = 0;
883
884 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n");
885
886 ret = mtd->suspend(mtd);
887
888 /*
889 * nand_suspend locks the device for exclusive access, so
890 * the clock must already be off.
891 */
892 BUG_ON(!ret && host->clk_act);
893
894 return ret;
895}
896
897static int mxcnd_resume(struct platform_device *pdev)
898{
899 struct mtd_info *mtd = platform_get_drvdata(pdev);
900 struct nand_chip *nand_chip = mtd->priv;
901 struct mxc_nand_host *host = nand_chip->priv;
902 int ret = 0;
903
904 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n");
905
906 mtd->resume(mtd);
907
908 return ret;
909}
910
911#else
912# define mxcnd_suspend NULL
913# define mxcnd_resume NULL
914#endif /* CONFIG_PM */
915
916static struct platform_driver mxcnd_driver = { 1168static struct platform_driver mxcnd_driver = {
917 .driver = { 1169 .driver = {
918 .name = DRIVER_NAME, 1170 .name = DRIVER_NAME,
919 }, 1171 },
920 .remove = __devexit_p(mxcnd_remove), 1172 .remove = __devexit_p(mxcnd_remove),
921 .suspend = mxcnd_suspend,
922 .resume = mxcnd_resume,
923}; 1173};
924 1174
925static int __init mxc_nd_init(void) 1175static int __init mxc_nd_init(void)
diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
index 4a7b86423ee..16a1714df00 100644
--- a/drivers/mtd/nand/nand_base.c
+++ b/drivers/mtd/nand/nand_base.c
@@ -42,7 +42,6 @@
42#include <linux/mtd/mtd.h> 42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h> 43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h> 44#include <linux/mtd/nand_ecc.h>
45#include <linux/mtd/compatmac.h>
46#include <linux/interrupt.h> 45#include <linux/interrupt.h>
47#include <linux/bitops.h> 46#include <linux/bitops.h>
48#include <linux/leds.h> 47#include <linux/leds.h>
@@ -347,7 +346,7 @@ static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
347 struct nand_chip *chip = mtd->priv; 346 struct nand_chip *chip = mtd->priv;
348 u16 bad; 347 u16 bad;
349 348
350 if (chip->options & NAND_BB_LAST_PAGE) 349 if (chip->options & NAND_BBT_SCANLASTPAGE)
351 ofs += mtd->erasesize - mtd->writesize; 350 ofs += mtd->erasesize - mtd->writesize;
352 351
353 page = (int)(ofs >> chip->page_shift) & chip->pagemask; 352 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
@@ -397,9 +396,9 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
397{ 396{
398 struct nand_chip *chip = mtd->priv; 397 struct nand_chip *chip = mtd->priv;
399 uint8_t buf[2] = { 0, 0 }; 398 uint8_t buf[2] = { 0, 0 };
400 int block, ret; 399 int block, ret, i = 0;
401 400
402 if (chip->options & NAND_BB_LAST_PAGE) 401 if (chip->options & NAND_BBT_SCANLASTPAGE)
403 ofs += mtd->erasesize - mtd->writesize; 402 ofs += mtd->erasesize - mtd->writesize;
404 403
405 /* Get block number */ 404 /* Get block number */
@@ -411,17 +410,31 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
411 if (chip->options & NAND_USE_FLASH_BBT) 410 if (chip->options & NAND_USE_FLASH_BBT)
412 ret = nand_update_bbt(mtd, ofs); 411 ret = nand_update_bbt(mtd, ofs);
413 else { 412 else {
414 /* We write two bytes, so we dont have to mess with 16 bit
415 * access
416 */
417 nand_get_device(chip, mtd, FL_WRITING); 413 nand_get_device(chip, mtd, FL_WRITING);
418 ofs += mtd->oobsize;
419 chip->ops.len = chip->ops.ooblen = 2;
420 chip->ops.datbuf = NULL;
421 chip->ops.oobbuf = buf;
422 chip->ops.ooboffs = chip->badblockpos & ~0x01;
423 414
424 ret = nand_do_write_oob(mtd, ofs, &chip->ops); 415 /* Write to first two pages and to byte 1 and 6 if necessary.
416 * If we write to more than one location, the first error
417 * encountered quits the procedure. We write two bytes per
418 * location, so we dont have to mess with 16 bit access.
419 */
420 do {
421 chip->ops.len = chip->ops.ooblen = 2;
422 chip->ops.datbuf = NULL;
423 chip->ops.oobbuf = buf;
424 chip->ops.ooboffs = chip->badblockpos & ~0x01;
425
426 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
427
428 if (!ret && (chip->options & NAND_BBT_SCANBYTE1AND6)) {
429 chip->ops.ooboffs = NAND_SMALL_BADBLOCK_POS
430 & ~0x01;
431 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
432 }
433 i++;
434 ofs += mtd->writesize;
435 } while (!ret && (chip->options & NAND_BBT_SCAN2NDPAGE) &&
436 i < 2);
437
425 nand_release_device(mtd); 438 nand_release_device(mtd);
426 } 439 }
427 if (!ret) 440 if (!ret)
@@ -2920,9 +2933,14 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2920 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1; 2933 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
2921 2934
2922 /* Set the bad block position */ 2935 /* Set the bad block position */
2923 chip->badblockpos = mtd->writesize > 512 ? 2936 if (!(busw & NAND_BUSWIDTH_16) && (*maf_id == NAND_MFR_STMICRO ||
2924 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS; 2937 (*maf_id == NAND_MFR_SAMSUNG &&
2925 chip->badblockbits = 8; 2938 mtd->writesize == 512) ||
2939 *maf_id == NAND_MFR_AMD))
2940 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
2941 else
2942 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
2943
2926 2944
2927 /* Get chip options, preserve non chip based options */ 2945 /* Get chip options, preserve non chip based options */
2928 chip->options &= ~NAND_CHIPOPTIONS_MSK; 2946 chip->options &= ~NAND_CHIPOPTIONS_MSK;
@@ -2941,12 +2959,32 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2941 2959
2942 /* 2960 /*
2943 * Bad block marker is stored in the last page of each block 2961 * Bad block marker is stored in the last page of each block
2944 * on Samsung and Hynix MLC devices 2962 * on Samsung and Hynix MLC devices; stored in first two pages
2963 * of each block on Micron devices with 2KiB pages and on
2964 * SLC Samsung, Hynix, and AMD/Spansion. All others scan only
2965 * the first page.
2945 */ 2966 */
2946 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) && 2967 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2947 (*maf_id == NAND_MFR_SAMSUNG || 2968 (*maf_id == NAND_MFR_SAMSUNG ||
2948 *maf_id == NAND_MFR_HYNIX)) 2969 *maf_id == NAND_MFR_HYNIX))
2949 chip->options |= NAND_BB_LAST_PAGE; 2970 chip->options |= NAND_BBT_SCANLASTPAGE;
2971 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2972 (*maf_id == NAND_MFR_SAMSUNG ||
2973 *maf_id == NAND_MFR_HYNIX ||
2974 *maf_id == NAND_MFR_AMD)) ||
2975 (mtd->writesize == 2048 &&
2976 *maf_id == NAND_MFR_MICRON))
2977 chip->options |= NAND_BBT_SCAN2NDPAGE;
2978
2979 /*
2980 * Numonyx/ST 2K pages, x8 bus use BOTH byte 1 and 6
2981 */
2982 if (!(busw & NAND_BUSWIDTH_16) &&
2983 *maf_id == NAND_MFR_STMICRO &&
2984 mtd->writesize == 2048) {
2985 chip->options |= NAND_BBT_SCANBYTE1AND6;
2986 chip->badblockpos = 0;
2987 }
2950 2988
2951 /* Check for AND chips with 4 page planes */ 2989 /* Check for AND chips with 4 page planes */
2952 if (chip->options & NAND_4PAGE_ARRAY) 2990 if (chip->options & NAND_4PAGE_ARRAY)
@@ -3306,6 +3344,11 @@ void nand_release(struct mtd_info *mtd)
3306 kfree(chip->bbt); 3344 kfree(chip->bbt);
3307 if (!(chip->options & NAND_OWN_BUFFERS)) 3345 if (!(chip->options & NAND_OWN_BUFFERS))
3308 kfree(chip->buffers); 3346 kfree(chip->buffers);
3347
3348 /* Free bad block descriptor memory */
3349 if (chip->badblock_pattern && chip->badblock_pattern->options
3350 & NAND_BBT_DYNAMICSTRUCT)
3351 kfree(chip->badblock_pattern);
3309} 3352}
3310 3353
3311EXPORT_SYMBOL_GPL(nand_lock); 3354EXPORT_SYMBOL_GPL(nand_lock);
diff --git a/drivers/mtd/nand/nand_bbt.c b/drivers/mtd/nand/nand_bbt.c
index ad97c0ce73b..5fedf4a74f1 100644
--- a/drivers/mtd/nand/nand_bbt.c
+++ b/drivers/mtd/nand/nand_bbt.c
@@ -55,7 +55,6 @@
55#include <linux/mtd/mtd.h> 55#include <linux/mtd/mtd.h>
56#include <linux/mtd/nand.h> 56#include <linux/mtd/nand.h>
57#include <linux/mtd/nand_ecc.h> 57#include <linux/mtd/nand_ecc.h>
58#include <linux/mtd/compatmac.h>
59#include <linux/bitops.h> 58#include <linux/bitops.h>
60#include <linux/delay.h> 59#include <linux/delay.h>
61#include <linux/vmalloc.h> 60#include <linux/vmalloc.h>
@@ -93,6 +92,28 @@ static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_desc
93 return -1; 92 return -1;
94 } 93 }
95 94
95 /* Check both positions 1 and 6 for pattern? */
96 if (td->options & NAND_BBT_SCANBYTE1AND6) {
97 if (td->options & NAND_BBT_SCANEMPTY) {
98 p += td->len;
99 end += NAND_SMALL_BADBLOCK_POS - td->offs;
100 /* Check region between positions 1 and 6 */
101 for (i = 0; i < NAND_SMALL_BADBLOCK_POS - td->offs - td->len;
102 i++) {
103 if (*p++ != 0xff)
104 return -1;
105 }
106 }
107 else {
108 p += NAND_SMALL_BADBLOCK_POS - td->offs;
109 }
110 /* Compare the pattern */
111 for (i = 0; i < td->len; i++) {
112 if (p[i] != td->pattern[i])
113 return -1;
114 }
115 }
116
96 if (td->options & NAND_BBT_SCANEMPTY) { 117 if (td->options & NAND_BBT_SCANEMPTY) {
97 p += td->len; 118 p += td->len;
98 end += td->len; 119 end += td->len;
@@ -124,6 +145,13 @@ static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
124 if (p[td->offs + i] != td->pattern[i]) 145 if (p[td->offs + i] != td->pattern[i])
125 return -1; 146 return -1;
126 } 147 }
148 /* Need to check location 1 AND 6? */
149 if (td->options & NAND_BBT_SCANBYTE1AND6) {
150 for (i = 0; i < td->len; i++) {
151 if (p[NAND_SMALL_BADBLOCK_POS + i] != td->pattern[i])
152 return -1;
153 }
154 }
127 return 0; 155 return 0;
128} 156}
129 157
@@ -397,12 +425,10 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
397 425
398 if (bd->options & NAND_BBT_SCANALLPAGES) 426 if (bd->options & NAND_BBT_SCANALLPAGES)
399 len = 1 << (this->bbt_erase_shift - this->page_shift); 427 len = 1 << (this->bbt_erase_shift - this->page_shift);
400 else { 428 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
401 if (bd->options & NAND_BBT_SCAN2NDPAGE) 429 len = 2;
402 len = 2; 430 else
403 else 431 len = 1;
404 len = 1;
405 }
406 432
407 if (!(bd->options & NAND_BBT_SCANEMPTY)) { 433 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
408 /* We need only read few bytes from the OOB area */ 434 /* We need only read few bytes from the OOB area */
@@ -432,7 +458,7 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
432 from = (loff_t)startblock << (this->bbt_erase_shift - 1); 458 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
433 } 459 }
434 460
435 if (this->options & NAND_BB_LAST_PAGE) 461 if (this->options & NAND_BBT_SCANLASTPAGE)
436 from += mtd->erasesize - (mtd->writesize * len); 462 from += mtd->erasesize - (mtd->writesize * len);
437 463
438 for (i = startblock; i < numblocks;) { 464 for (i = startblock; i < numblocks;) {
@@ -1092,30 +1118,16 @@ int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
1092 * while scanning a device for factory marked good / bad blocks. */ 1118 * while scanning a device for factory marked good / bad blocks. */
1093static uint8_t scan_ff_pattern[] = { 0xff, 0xff }; 1119static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1094 1120
1095static struct nand_bbt_descr smallpage_memorybased = {
1096 .options = NAND_BBT_SCAN2NDPAGE,
1097 .offs = 5,
1098 .len = 1,
1099 .pattern = scan_ff_pattern
1100};
1101
1102static struct nand_bbt_descr largepage_memorybased = {
1103 .options = 0,
1104 .offs = 0,
1105 .len = 2,
1106 .pattern = scan_ff_pattern
1107};
1108
1109static struct nand_bbt_descr smallpage_flashbased = { 1121static struct nand_bbt_descr smallpage_flashbased = {
1110 .options = NAND_BBT_SCAN2NDPAGE, 1122 .options = NAND_BBT_SCAN2NDPAGE,
1111 .offs = 5, 1123 .offs = NAND_SMALL_BADBLOCK_POS,
1112 .len = 1, 1124 .len = 1,
1113 .pattern = scan_ff_pattern 1125 .pattern = scan_ff_pattern
1114}; 1126};
1115 1127
1116static struct nand_bbt_descr largepage_flashbased = { 1128static struct nand_bbt_descr largepage_flashbased = {
1117 .options = NAND_BBT_SCAN2NDPAGE, 1129 .options = NAND_BBT_SCAN2NDPAGE,
1118 .offs = 0, 1130 .offs = NAND_LARGE_BADBLOCK_POS,
1119 .len = 2, 1131 .len = 2,
1120 .pattern = scan_ff_pattern 1132 .pattern = scan_ff_pattern
1121}; 1133};
@@ -1154,6 +1166,43 @@ static struct nand_bbt_descr bbt_mirror_descr = {
1154 .pattern = mirror_pattern 1166 .pattern = mirror_pattern
1155}; 1167};
1156 1168
1169#define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
1170 NAND_BBT_SCANBYTE1AND6)
1171/**
1172 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1173 * @this: NAND chip to create descriptor for
1174 *
1175 * This function allocates and initializes a nand_bbt_descr for BBM detection
1176 * based on the properties of "this". The new descriptor is stored in
1177 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1178 * passed to this function.
1179 *
1180 * TODO: Handle other flags, replace other static structs
1181 * (e.g. handle NAND_BBT_FLASH for flash-based BBT,
1182 * replace smallpage_flashbased)
1183 *
1184 */
1185static int nand_create_default_bbt_descr(struct nand_chip *this)
1186{
1187 struct nand_bbt_descr *bd;
1188 if (this->badblock_pattern) {
1189 printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
1190 return -EINVAL;
1191 }
1192 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1193 if (!bd) {
1194 printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
1195 return -ENOMEM;
1196 }
1197 bd->options = this->options & BBT_SCAN_OPTIONS;
1198 bd->offs = this->badblockpos;
1199 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1200 bd->pattern = scan_ff_pattern;
1201 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1202 this->badblock_pattern = bd;
1203 return 0;
1204}
1205
1157/** 1206/**
1158 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device 1207 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1159 * @mtd: MTD device structure 1208 * @mtd: MTD device structure
@@ -1196,10 +1245,8 @@ int nand_default_bbt(struct mtd_info *mtd)
1196 } else { 1245 } else {
1197 this->bbt_td = NULL; 1246 this->bbt_td = NULL;
1198 this->bbt_md = NULL; 1247 this->bbt_md = NULL;
1199 if (!this->badblock_pattern) { 1248 if (!this->badblock_pattern)
1200 this->badblock_pattern = (mtd->writesize > 512) ? 1249 nand_create_default_bbt_descr(this);
1201 &largepage_memorybased : &smallpage_memorybased;
1202 }
1203 } 1250 }
1204 return nand_scan_bbt(mtd, this->badblock_pattern); 1251 return nand_scan_bbt(mtd, this->badblock_pattern);
1205} 1252}
diff --git a/drivers/mtd/nand/nand_ids.c b/drivers/mtd/nand/nand_ids.c
index 89907ed9900..a04b89105b6 100644
--- a/drivers/mtd/nand/nand_ids.c
+++ b/drivers/mtd/nand/nand_ids.c
@@ -85,6 +85,7 @@ struct nand_flash_dev nand_flash_ids[] = {
85 {"NAND 128MiB 3,3V 8-bit", 0xD1, 0, 128, 0, LP_OPTIONS}, 85 {"NAND 128MiB 3,3V 8-bit", 0xD1, 0, 128, 0, LP_OPTIONS},
86 {"NAND 128MiB 1,8V 16-bit", 0xB1, 0, 128, 0, LP_OPTIONS16}, 86 {"NAND 128MiB 1,8V 16-bit", 0xB1, 0, 128, 0, LP_OPTIONS16},
87 {"NAND 128MiB 3,3V 16-bit", 0xC1, 0, 128, 0, LP_OPTIONS16}, 87 {"NAND 128MiB 3,3V 16-bit", 0xC1, 0, 128, 0, LP_OPTIONS16},
88 {"NAND 128MiB 1,8V 16-bit", 0xAD, 0, 128, 0, LP_OPTIONS16},
88 89
89 /* 2 Gigabit */ 90 /* 2 Gigabit */
90 {"NAND 256MiB 1,8V 8-bit", 0xAA, 0, 256, 0, LP_OPTIONS}, 91 {"NAND 256MiB 1,8V 8-bit", 0xAA, 0, 256, 0, LP_OPTIONS},
@@ -110,6 +111,9 @@ struct nand_flash_dev nand_flash_ids[] = {
110 {"NAND 2GiB 1,8V 16-bit", 0xB5, 0, 2048, 0, LP_OPTIONS16}, 111 {"NAND 2GiB 1,8V 16-bit", 0xB5, 0, 2048, 0, LP_OPTIONS16},
111 {"NAND 2GiB 3,3V 16-bit", 0xC5, 0, 2048, 0, LP_OPTIONS16}, 112 {"NAND 2GiB 3,3V 16-bit", 0xC5, 0, 2048, 0, LP_OPTIONS16},
112 113
114 /* 32 Gigabit */
115 {"NAND 4GiB 3,3V 8-bit", 0xD7, 0, 4096, 0, LP_OPTIONS16},
116
113 /* 117 /*
114 * Renesas AND 1 Gigabit. Those chips do not support extended id and 118 * Renesas AND 1 Gigabit. Those chips do not support extended id and
115 * have a strange page/block layout ! The chosen minimum erasesize is 119 * have a strange page/block layout ! The chosen minimum erasesize is
diff --git a/drivers/mtd/nand/nandsim.c b/drivers/mtd/nand/nandsim.c
index 261337efe0e..c25648bb579 100644
--- a/drivers/mtd/nand/nandsim.c
+++ b/drivers/mtd/nand/nandsim.c
@@ -553,8 +553,8 @@ static uint64_t divide(uint64_t n, uint32_t d)
553 */ 553 */
554static int init_nandsim(struct mtd_info *mtd) 554static int init_nandsim(struct mtd_info *mtd)
555{ 555{
556 struct nand_chip *chip = (struct nand_chip *)mtd->priv; 556 struct nand_chip *chip = mtd->priv;
557 struct nandsim *ns = (struct nandsim *)(chip->priv); 557 struct nandsim *ns = chip->priv;
558 int i, ret = 0; 558 int i, ret = 0;
559 uint64_t remains; 559 uint64_t remains;
560 uint64_t next_offset; 560 uint64_t next_offset;
@@ -1877,7 +1877,7 @@ static void switch_state(struct nandsim *ns)
1877 1877
1878static u_char ns_nand_read_byte(struct mtd_info *mtd) 1878static u_char ns_nand_read_byte(struct mtd_info *mtd)
1879{ 1879{
1880 struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv; 1880 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
1881 u_char outb = 0x00; 1881 u_char outb = 0x00;
1882 1882
1883 /* Sanity and correctness checks */ 1883 /* Sanity and correctness checks */
@@ -1950,7 +1950,7 @@ static u_char ns_nand_read_byte(struct mtd_info *mtd)
1950 1950
1951static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte) 1951static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
1952{ 1952{
1953 struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv; 1953 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
1954 1954
1955 /* Sanity and correctness checks */ 1955 /* Sanity and correctness checks */
1956 if (!ns->lines.ce) { 1956 if (!ns->lines.ce) {
@@ -2132,7 +2132,7 @@ static uint16_t ns_nand_read_word(struct mtd_info *mtd)
2132 2132
2133static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) 2133static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
2134{ 2134{
2135 struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv; 2135 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
2136 2136
2137 /* Check that chip is expecting data input */ 2137 /* Check that chip is expecting data input */
2138 if (!(ns->state & STATE_DATAIN_MASK)) { 2138 if (!(ns->state & STATE_DATAIN_MASK)) {
@@ -2159,7 +2159,7 @@ static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
2159 2159
2160static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) 2160static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
2161{ 2161{
2162 struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv; 2162 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
2163 2163
2164 /* Sanity and correctness checks */ 2164 /* Sanity and correctness checks */
2165 if (!ns->lines.ce) { 2165 if (!ns->lines.ce) {
@@ -2352,7 +2352,7 @@ module_init(ns_init_module);
2352 */ 2352 */
2353static void __exit ns_cleanup_module(void) 2353static void __exit ns_cleanup_module(void)
2354{ 2354{
2355 struct nandsim *ns = (struct nandsim *)(((struct nand_chip *)nsmtd->priv)->priv); 2355 struct nandsim *ns = ((struct nand_chip *)nsmtd->priv)->priv;
2356 int i; 2356 int i;
2357 2357
2358 free_nandsim(ns); /* Free nandsim private resources */ 2358 free_nandsim(ns); /* Free nandsim private resources */
diff --git a/drivers/mtd/nand/plat_nand.c b/drivers/mtd/nand/plat_nand.c
index 8d467315f02..90e143e5ad3 100644
--- a/drivers/mtd/nand/plat_nand.c
+++ b/drivers/mtd/nand/plat_nand.c
@@ -91,7 +91,7 @@ static int __devinit plat_nand_probe(struct platform_device *pdev)
91 } 91 }
92 92
93 /* Scan to find existance of the device */ 93 /* Scan to find existance of the device */
94 if (nand_scan(&data->mtd, 1)) { 94 if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
95 err = -ENXIO; 95 err = -ENXIO;
96 goto out; 96 goto out;
97 } 97 }
diff --git a/drivers/mtd/nand/r852.c b/drivers/mtd/nand/r852.c
index bcfc851fe55..5169ca6a66b 100644
--- a/drivers/mtd/nand/r852.c
+++ b/drivers/mtd/nand/r852.c
@@ -64,8 +64,8 @@ static inline void r852_write_reg_dword(struct r852_device *dev,
64/* returns pointer to our private structure */ 64/* returns pointer to our private structure */
65static inline struct r852_device *r852_get_dev(struct mtd_info *mtd) 65static inline struct r852_device *r852_get_dev(struct mtd_info *mtd)
66{ 66{
67 struct nand_chip *chip = (struct nand_chip *)mtd->priv; 67 struct nand_chip *chip = mtd->priv;
68 return (struct r852_device *)chip->priv; 68 return chip->priv;
69} 69}
70 70
71 71
@@ -380,7 +380,7 @@ void r852_cmdctl(struct mtd_info *mtd, int dat, unsigned int ctrl)
380 */ 380 */
381int r852_wait(struct mtd_info *mtd, struct nand_chip *chip) 381int r852_wait(struct mtd_info *mtd, struct nand_chip *chip)
382{ 382{
383 struct r852_device *dev = (struct r852_device *)chip->priv; 383 struct r852_device *dev = chip->priv;
384 384
385 unsigned long timeout; 385 unsigned long timeout;
386 int status; 386 int status;
diff --git a/drivers/mtd/nand/rtc_from4.c b/drivers/mtd/nand/rtc_from4.c
index a033c4cd8e1..67440b5beef 100644
--- a/drivers/mtd/nand/rtc_from4.c
+++ b/drivers/mtd/nand/rtc_from4.c
@@ -24,7 +24,6 @@
24#include <linux/rslib.h> 24#include <linux/rslib.h>
25#include <linux/bitrev.h> 25#include <linux/bitrev.h>
26#include <linux/module.h> 26#include <linux/module.h>
27#include <linux/mtd/compatmac.h>
28#include <linux/mtd/mtd.h> 27#include <linux/mtd/mtd.h>
29#include <linux/mtd/nand.h> 28#include <linux/mtd/nand.h>
30#include <linux/mtd/partitions.h> 29#include <linux/mtd/partitions.h>
diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
index 239aadfd01b..33d832dddfd 100644
--- a/drivers/mtd/nand/s3c2410.c
+++ b/drivers/mtd/nand/s3c2410.c
@@ -727,15 +727,12 @@ static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
727 if (set == NULL) 727 if (set == NULL)
728 return add_mtd_device(&mtd->mtd); 728 return add_mtd_device(&mtd->mtd);
729 729
730 if (set->nr_partitions == 0) { 730 mtd->mtd.name = set->name;
731 mtd->mtd.name = set->name; 731 nr_part = parse_mtd_partitions(&mtd->mtd, part_probes, &part_info, 0);
732 nr_part = parse_mtd_partitions(&mtd->mtd, part_probes, 732
733 &part_info, 0); 733 if (nr_part <= 0 && set->nr_partitions > 0) {
734 } else { 734 nr_part = set->nr_partitions;
735 if (set->nr_partitions > 0 && set->partitions != NULL) { 735 part_info = set->partitions;
736 nr_part = set->nr_partitions;
737 part_info = set->partitions;
738 }
739 } 736 }
740 737
741 if (nr_part > 0 && part_info) 738 if (nr_part > 0 && part_info)
diff --git a/drivers/mtd/nand/sm_common.c b/drivers/mtd/nand/sm_common.c
index ac80fb362e6..4a8f367c295 100644
--- a/drivers/mtd/nand/sm_common.c
+++ b/drivers/mtd/nand/sm_common.c
@@ -109,7 +109,7 @@ static struct nand_flash_dev nand_xd_flash_ids[] = {
109 109
110int sm_register_device(struct mtd_info *mtd, int smartmedia) 110int sm_register_device(struct mtd_info *mtd, int smartmedia)
111{ 111{
112 struct nand_chip *chip = (struct nand_chip *)mtd->priv; 112 struct nand_chip *chip = mtd->priv;
113 int ret; 113 int ret;
114 114
115 chip->options |= NAND_SKIP_BBTSCAN; 115 chip->options |= NAND_SKIP_BBTSCAN;
diff --git a/drivers/mtd/nftlcore.c b/drivers/mtd/nftlcore.c
index a4578bf903a..b155666acfb 100644
--- a/drivers/mtd/nftlcore.c
+++ b/drivers/mtd/nftlcore.c
@@ -1,11 +1,22 @@
1/* Linux driver for NAND Flash Translation Layer */
2/* (c) 1999 Machine Vision Holdings, Inc. */
3/* Author: David Woodhouse <dwmw2@infradead.org> */
4
5/* 1/*
6 The contents of this file are distributed under the GNU General 2 * Linux driver for NAND Flash Translation Layer
7 Public License version 2. The author places no additional 3 *
8 restrictions of any kind on it. 4 * Copyright © 1999 Machine Vision Holdings, Inc.
5 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9 */ 20 */
10 21
11#define PRERELEASE 22#define PRERELEASE
diff --git a/drivers/mtd/nftlmount.c b/drivers/mtd/nftlmount.c
index 8b22b1836e9..e3cd1ffad2f 100644
--- a/drivers/mtd/nftlmount.c
+++ b/drivers/mtd/nftlmount.c
@@ -2,7 +2,8 @@
2 * NFTL mount code with extensive checks 2 * NFTL mount code with extensive checks
3 * 3 *
4 * Author: Fabrice Bellard (fabrice.bellard@netgem.com) 4 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
5 * Copyright (C) 2000 Netgem S.A. 5 * Copyright © 2000 Netgem S.A.
6 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
6 * 7 *
7 * This program is free software; you can redistribute it and/or modify 8 * 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 * it under the terms of the GNU General Public License as published by
diff --git a/drivers/mtd/ofpart.c b/drivers/mtd/ofpart.c
index 4f0d635674f..8bf7dc6d1ce 100644
--- a/drivers/mtd/ofpart.c
+++ b/drivers/mtd/ofpart.c
@@ -1,11 +1,11 @@
1/* 1/*
2 * Flash partitions described by the OF (or flattened) device tree 2 * Flash partitions described by the OF (or flattened) device tree
3 * 3 *
4 * Copyright (C) 2006 MontaVista Software Inc. 4 * Copyright © 2006 MontaVista Software Inc.
5 * Author: Vitaly Wool <vwool@ru.mvista.com> 5 * Author: Vitaly Wool <vwool@ru.mvista.com>
6 * 6 *
7 * Revised to handle newer style flash binding by: 7 * Revised to handle newer style flash binding by:
8 * Copyright (C) 2007 David Gibson, IBM Corporation. 8 * Copyright © 2007 David Gibson, IBM Corporation.
9 * 9 *
10 * This program is free software; you can redistribute it and/or modify it 10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the 11 * under the terms of the GNU General Public License as published by the
diff --git a/drivers/mtd/onenand/Kconfig b/drivers/mtd/onenand/Kconfig
index 9a49d68ba5f..3f32289fdbb 100644
--- a/drivers/mtd/onenand/Kconfig
+++ b/drivers/mtd/onenand/Kconfig
@@ -25,14 +25,14 @@ config MTD_ONENAND_GENERIC
25 25
26config MTD_ONENAND_OMAP2 26config MTD_ONENAND_OMAP2
27 tristate "OneNAND on OMAP2/OMAP3 support" 27 tristate "OneNAND on OMAP2/OMAP3 support"
28 depends on MTD_ONENAND && (ARCH_OMAP2 || ARCH_OMAP3) 28 depends on ARCH_OMAP2 || ARCH_OMAP3
29 help 29 help
30 Support for a OneNAND flash device connected to an OMAP2/OMAP3 CPU 30 Support for a OneNAND flash device connected to an OMAP2/OMAP3 CPU
31 via the GPMC memory controller. 31 via the GPMC memory controller.
32 32
33config MTD_ONENAND_SAMSUNG 33config MTD_ONENAND_SAMSUNG
34 tristate "OneNAND on Samsung SOC controller support" 34 tristate "OneNAND on Samsung SOC controller support"
35 depends on MTD_ONENAND && (ARCH_S3C64XX || ARCH_S5PC100 || ARCH_S5PV210) 35 depends on ARCH_S3C64XX || ARCH_S5PC100 || ARCH_S5PV210
36 help 36 help
37 Support for a OneNAND flash device connected to an Samsung SOC 37 Support for a OneNAND flash device connected to an Samsung SOC
38 S3C64XX/S5PC1XX controller. 38 S3C64XX/S5PC1XX controller.
diff --git a/drivers/mtd/onenand/onenand_base.c b/drivers/mtd/onenand/onenand_base.c
index 26caf2590da..a2bb520286f 100644
--- a/drivers/mtd/onenand/onenand_base.c
+++ b/drivers/mtd/onenand/onenand_base.c
@@ -377,8 +377,11 @@ static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t le
377 377
378 default: 378 default:
379 block = onenand_block(this, addr); 379 block = onenand_block(this, addr);
380 page = (int) (addr - onenand_addr(this, block)) >> this->page_shift; 380 if (FLEXONENAND(this))
381 381 page = (int) (addr - onenand_addr(this, block))>>\
382 this->page_shift;
383 else
384 page = (int) (addr >> this->page_shift);
382 if (ONENAND_IS_2PLANE(this)) { 385 if (ONENAND_IS_2PLANE(this)) {
383 /* Make the even block number */ 386 /* Make the even block number */
384 block &= ~1; 387 block &= ~1;
@@ -3730,17 +3733,16 @@ out:
3730} 3733}
3731 3734
3732/** 3735/**
3733 * onenand_probe - [OneNAND Interface] Probe the OneNAND device 3736 * onenand_chip_probe - [OneNAND Interface] The generic chip probe
3734 * @param mtd MTD device structure 3737 * @param mtd MTD device structure
3735 * 3738 *
3736 * OneNAND detection method: 3739 * OneNAND detection method:
3737 * Compare the values from command with ones from register 3740 * Compare the values from command with ones from register
3738 */ 3741 */
3739static int onenand_probe(struct mtd_info *mtd) 3742static int onenand_chip_probe(struct mtd_info *mtd)
3740{ 3743{
3741 struct onenand_chip *this = mtd->priv; 3744 struct onenand_chip *this = mtd->priv;
3742 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id; 3745 int bram_maf_id, bram_dev_id, maf_id, dev_id;
3743 int density;
3744 int syscfg; 3746 int syscfg;
3745 3747
3746 /* Save system configuration 1 */ 3748 /* Save system configuration 1 */
@@ -3763,12 +3765,6 @@ static int onenand_probe(struct mtd_info *mtd)
3763 /* Restore system configuration 1 */ 3765 /* Restore system configuration 1 */
3764 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1); 3766 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3765 3767
3766 /* Workaround */
3767 if (syscfg & ONENAND_SYS_CFG1_SYNC_WRITE) {
3768 bram_maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3769 bram_dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3770 }
3771
3772 /* Check manufacturer ID */ 3768 /* Check manufacturer ID */
3773 if (onenand_check_maf(bram_maf_id)) 3769 if (onenand_check_maf(bram_maf_id))
3774 return -ENXIO; 3770 return -ENXIO;
@@ -3776,13 +3772,35 @@ static int onenand_probe(struct mtd_info *mtd)
3776 /* Read manufacturer and device IDs from Register */ 3772 /* Read manufacturer and device IDs from Register */
3777 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID); 3773 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3778 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID); 3774 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3779 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
3780 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
3781 3775
3782 /* Check OneNAND device */ 3776 /* Check OneNAND device */
3783 if (maf_id != bram_maf_id || dev_id != bram_dev_id) 3777 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
3784 return -ENXIO; 3778 return -ENXIO;
3785 3779
3780 return 0;
3781}
3782
3783/**
3784 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3785 * @param mtd MTD device structure
3786 */
3787static int onenand_probe(struct mtd_info *mtd)
3788{
3789 struct onenand_chip *this = mtd->priv;
3790 int maf_id, dev_id, ver_id;
3791 int density;
3792 int ret;
3793
3794 ret = this->chip_probe(mtd);
3795 if (ret)
3796 return ret;
3797
3798 /* Read manufacturer and device IDs from Register */
3799 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3800 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3801 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
3802 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
3803
3786 /* Flash device information */ 3804 /* Flash device information */
3787 onenand_print_device_info(dev_id, ver_id); 3805 onenand_print_device_info(dev_id, ver_id);
3788 this->device_id = dev_id; 3806 this->device_id = dev_id;
@@ -3909,6 +3927,9 @@ int onenand_scan(struct mtd_info *mtd, int maxchips)
3909 if (!this->unlock_all) 3927 if (!this->unlock_all)
3910 this->unlock_all = onenand_unlock_all; 3928 this->unlock_all = onenand_unlock_all;
3911 3929
3930 if (!this->chip_probe)
3931 this->chip_probe = onenand_chip_probe;
3932
3912 if (!this->read_bufferram) 3933 if (!this->read_bufferram)
3913 this->read_bufferram = onenand_read_bufferram; 3934 this->read_bufferram = onenand_read_bufferram;
3914 if (!this->write_bufferram) 3935 if (!this->write_bufferram)
diff --git a/drivers/mtd/onenand/onenand_bbt.c b/drivers/mtd/onenand/onenand_bbt.c
index a91fcac1af0..01ab5b3c453 100644
--- a/drivers/mtd/onenand/onenand_bbt.c
+++ b/drivers/mtd/onenand/onenand_bbt.c
@@ -15,7 +15,6 @@
15#include <linux/slab.h> 15#include <linux/slab.h>
16#include <linux/mtd/mtd.h> 16#include <linux/mtd/mtd.h>
17#include <linux/mtd/onenand.h> 17#include <linux/mtd/onenand.h>
18#include <linux/mtd/compatmac.h>
19 18
20/** 19/**
21 * check_short_pattern - [GENERIC] check if a pattern is in the buffer 20 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
diff --git a/drivers/mtd/onenand/samsung.c b/drivers/mtd/onenand/samsung.c
index 2750317cb58..cb443af3d45 100644
--- a/drivers/mtd/onenand/samsung.c
+++ b/drivers/mtd/onenand/samsung.c
@@ -630,6 +630,12 @@ normal:
630 return 0; 630 return 0;
631} 631}
632 632
633static int s5pc110_chip_probe(struct mtd_info *mtd)
634{
635 /* Now just return 0 */
636 return 0;
637}
638
633static int s3c_onenand_bbt_wait(struct mtd_info *mtd, int state) 639static int s3c_onenand_bbt_wait(struct mtd_info *mtd, int state)
634{ 640{
635 unsigned int flags = INT_ACT | LOAD_CMP; 641 unsigned int flags = INT_ACT | LOAD_CMP;
@@ -757,6 +763,7 @@ static void s3c_onenand_setup(struct mtd_info *mtd)
757 /* Use generic onenand functions */ 763 /* Use generic onenand functions */
758 onenand->cmd_map = s5pc1xx_cmd_map; 764 onenand->cmd_map = s5pc1xx_cmd_map;
759 this->read_bufferram = s5pc110_read_bufferram; 765 this->read_bufferram = s5pc110_read_bufferram;
766 this->chip_probe = s5pc110_chip_probe;
760 return; 767 return;
761 } else { 768 } else {
762 BUG(); 769 BUG();
@@ -781,7 +788,6 @@ static int s3c_onenand_probe(struct platform_device *pdev)
781 struct mtd_info *mtd; 788 struct mtd_info *mtd;
782 struct resource *r; 789 struct resource *r;
783 int size, err; 790 int size, err;
784 unsigned long onenand_ctrl_cfg = 0;
785 791
786 pdata = pdev->dev.platform_data; 792 pdata = pdev->dev.platform_data;
787 /* No need to check pdata. the platform data is optional */ 793 /* No need to check pdata. the platform data is optional */
@@ -900,14 +906,6 @@ static int s3c_onenand_probe(struct platform_device *pdev)
900 } 906 }
901 907
902 onenand->phys_base = onenand->base_res->start; 908 onenand->phys_base = onenand->base_res->start;
903
904 onenand_ctrl_cfg = readl(onenand->dma_addr + 0x100);
905 if ((onenand_ctrl_cfg & ONENAND_SYS_CFG1_SYNC_WRITE) &&
906 onenand->dma_addr)
907 writel(onenand_ctrl_cfg & ~ONENAND_SYS_CFG1_SYNC_WRITE,
908 onenand->dma_addr + 0x100);
909 else
910 onenand_ctrl_cfg = 0;
911 } 909 }
912 910
913 if (onenand_scan(mtd, 1)) { 911 if (onenand_scan(mtd, 1)) {
@@ -915,10 +913,7 @@ static int s3c_onenand_probe(struct platform_device *pdev)
915 goto scan_failed; 913 goto scan_failed;
916 } 914 }
917 915
918 if (onenand->type == TYPE_S5PC110) { 916 if (onenand->type != TYPE_S5PC110) {
919 if (onenand_ctrl_cfg && onenand->dma_addr)
920 writel(onenand_ctrl_cfg, onenand->dma_addr + 0x100);
921 } else {
922 /* S3C doesn't handle subpage write */ 917 /* S3C doesn't handle subpage write */
923 mtd->subpage_sft = 0; 918 mtd->subpage_sft = 0;
924 this->subpagesize = mtd->writesize; 919 this->subpagesize = mtd->writesize;
diff --git a/drivers/mtd/redboot.c b/drivers/mtd/redboot.c
index 2d600a1bf2a..7a87d07cd79 100644
--- a/drivers/mtd/redboot.c
+++ b/drivers/mtd/redboot.c
@@ -1,6 +1,24 @@
1/* 1/*
2 * Parse RedBoot-style Flash Image System (FIS) tables and 2 * Parse RedBoot-style Flash Image System (FIS) tables and
3 * produce a Linux partition array to match. 3 * produce a Linux partition array to match.
4 *
5 * Copyright © 2001 Red Hat UK Limited
6 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
4 */ 22 */
5 23
6#include <linux/kernel.h> 24#include <linux/kernel.h>
diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index 63b83c0d9a1..cc4d1805b86 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * rfd_ftl.c -- resident flash disk (flash translation layer) 2 * rfd_ftl.c -- resident flash disk (flash translation layer)
3 * 3 *
4 * Copyright (C) 2005 Sean Young <sean@mess.org> 4 * Copyright © 2005 Sean Young <sean@mess.org>
5 * 5 *
6 * This type of flash translation layer (FTL) is used by the Embedded BIOS 6 * This type of flash translation layer (FTL) is used by the Embedded BIOS
7 * by General Software. It is known as the Resident Flash Disk (RFD), see: 7 * by General Software. It is known as the Resident Flash Disk (RFD), see:
diff --git a/drivers/mtd/ssfdc.c b/drivers/mtd/ssfdc.c
index 81c4ecdc11f..5cd18979333 100644
--- a/drivers/mtd/ssfdc.c
+++ b/drivers/mtd/ssfdc.c
@@ -1,6 +1,6 @@
1/* 1/*
2 * Linux driver for SSFDC Flash Translation Layer (Read only) 2 * Linux driver for SSFDC Flash Translation Layer (Read only)
3 * (c) 2005 Eptar srl 3 * © 2005 Eptar srl
4 * Author: Claudio Lanconelli <lanconelli.claudio@eptar.com> 4 * Author: Claudio Lanconelli <lanconelli.claudio@eptar.com>
5 * 5 *
6 * Based on NTFL and MTDBLOCK_RO drivers 6 * Based on NTFL and MTDBLOCK_RO drivers
diff --git a/drivers/mtd/tests/mtd_pagetest.c b/drivers/mtd/tests/mtd_pagetest.c
index 6bc1b8276c6..00b937e38c1 100644
--- a/drivers/mtd/tests/mtd_pagetest.c
+++ b/drivers/mtd/tests/mtd_pagetest.c
@@ -310,7 +310,7 @@ static int crosstest(void)
310static int erasecrosstest(void) 310static int erasecrosstest(void)
311{ 311{
312 size_t read = 0, written = 0; 312 size_t read = 0, written = 0;
313 int err = 0, i, ebnum, ok = 1, ebnum2; 313 int err = 0, i, ebnum, ebnum2;
314 loff_t addr0; 314 loff_t addr0;
315 char *readbuf = twopages; 315 char *readbuf = twopages;
316 316
@@ -357,8 +357,7 @@ static int erasecrosstest(void)
357 if (memcmp(writebuf, readbuf, pgsize)) { 357 if (memcmp(writebuf, readbuf, pgsize)) {
358 printk(PRINT_PREF "verify failed!\n"); 358 printk(PRINT_PREF "verify failed!\n");
359 errcnt += 1; 359 errcnt += 1;
360 ok = 0; 360 return -1;
361 return err;
362 } 361 }
363 362
364 printk(PRINT_PREF "erasing block %d\n", ebnum); 363 printk(PRINT_PREF "erasing block %d\n", ebnum);
@@ -396,10 +395,10 @@ static int erasecrosstest(void)
396 if (memcmp(writebuf, readbuf, pgsize)) { 395 if (memcmp(writebuf, readbuf, pgsize)) {
397 printk(PRINT_PREF "verify failed!\n"); 396 printk(PRINT_PREF "verify failed!\n");
398 errcnt += 1; 397 errcnt += 1;
399 ok = 0; 398 return -1;
400 } 399 }
401 400
402 if (ok && !err) 401 if (!err)
403 printk(PRINT_PREF "erasecrosstest ok\n"); 402 printk(PRINT_PREF "erasecrosstest ok\n");
404 return err; 403 return err;
405} 404}
diff --git a/drivers/power/olpc_battery.c b/drivers/power/olpc_battery.c
index baefcf1cffc..5ab9109b69e 100644
--- a/drivers/power/olpc_battery.c
+++ b/drivers/power/olpc_battery.c
@@ -384,7 +384,6 @@ static struct bin_attribute olpc_bat_eeprom = {
384 .attr = { 384 .attr = {
385 .name = "eeprom", 385 .name = "eeprom",
386 .mode = S_IRUGO, 386 .mode = S_IRUGO,
387 .owner = THIS_MODULE,
388 }, 387 },
389 .size = 0, 388 .size = 0,
390 .read = olpc_bat_eeprom_read, 389 .read = olpc_bat_eeprom_read,
diff --git a/drivers/s390/block/dasd.c b/drivers/s390/block/dasd.c
index 33975e922d6..1a84fae155e 100644
--- a/drivers/s390/block/dasd.c
+++ b/drivers/s390/block/dasd.c
@@ -21,6 +21,7 @@
21#include <linux/hdreg.h> 21#include <linux/hdreg.h>
22#include <linux/async.h> 22#include <linux/async.h>
23#include <linux/mutex.h> 23#include <linux/mutex.h>
24#include <linux/smp_lock.h>
24 25
25#include <asm/ccwdev.h> 26#include <asm/ccwdev.h>
26#include <asm/ebcdic.h> 27#include <asm/ebcdic.h>
@@ -2196,7 +2197,7 @@ static void dasd_setup_queue(struct dasd_block *block)
2196 */ 2197 */
2197 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE); 2198 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2198 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1); 2199 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
2199 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL); 2200 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN);
2200} 2201}
2201 2202
2202/* 2203/*
@@ -2235,6 +2236,7 @@ static int dasd_open(struct block_device *bdev, fmode_t mode)
2235 if (!block) 2236 if (!block)
2236 return -ENODEV; 2237 return -ENODEV;
2237 2238
2239 lock_kernel();
2238 base = block->base; 2240 base = block->base;
2239 atomic_inc(&block->open_count); 2241 atomic_inc(&block->open_count);
2240 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) { 2242 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
@@ -2269,12 +2271,14 @@ static int dasd_open(struct block_device *bdev, fmode_t mode)
2269 goto out; 2271 goto out;
2270 } 2272 }
2271 2273
2274 unlock_kernel();
2272 return 0; 2275 return 0;
2273 2276
2274out: 2277out:
2275 module_put(base->discipline->owner); 2278 module_put(base->discipline->owner);
2276unlock: 2279unlock:
2277 atomic_dec(&block->open_count); 2280 atomic_dec(&block->open_count);
2281 unlock_kernel();
2278 return rc; 2282 return rc;
2279} 2283}
2280 2284
@@ -2282,8 +2286,10 @@ static int dasd_release(struct gendisk *disk, fmode_t mode)
2282{ 2286{
2283 struct dasd_block *block = disk->private_data; 2287 struct dasd_block *block = disk->private_data;
2284 2288
2289 lock_kernel();
2285 atomic_dec(&block->open_count); 2290 atomic_dec(&block->open_count);
2286 module_put(block->base->discipline->owner); 2291 module_put(block->base->discipline->owner);
2292 unlock_kernel();
2287 return 0; 2293 return 0;
2288} 2294}
2289 2295
diff --git a/drivers/s390/block/dasd_devmap.c b/drivers/s390/block/dasd_devmap.c
index bed7b4634cc..8d41f3ed38d 100644
--- a/drivers/s390/block/dasd_devmap.c
+++ b/drivers/s390/block/dasd_devmap.c
@@ -1083,6 +1083,49 @@ dasd_eer_store(struct device *dev, struct device_attribute *attr,
1083 1083
1084static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store); 1084static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
1085 1085
1086/*
1087 * expiration time for default requests
1088 */
1089static ssize_t
1090dasd_expires_show(struct device *dev, struct device_attribute *attr, char *buf)
1091{
1092 struct dasd_device *device;
1093 int len;
1094
1095 device = dasd_device_from_cdev(to_ccwdev(dev));
1096 if (IS_ERR(device))
1097 return -ENODEV;
1098 len = snprintf(buf, PAGE_SIZE, "%lu\n", device->default_expires);
1099 dasd_put_device(device);
1100 return len;
1101}
1102
1103static ssize_t
1104dasd_expires_store(struct device *dev, struct device_attribute *attr,
1105 const char *buf, size_t count)
1106{
1107 struct dasd_device *device;
1108 unsigned long val;
1109
1110 device = dasd_device_from_cdev(to_ccwdev(dev));
1111 if (IS_ERR(device))
1112 return -ENODEV;
1113
1114 if ((strict_strtoul(buf, 10, &val) != 0) ||
1115 (val > DASD_EXPIRES_MAX) || val == 0) {
1116 dasd_put_device(device);
1117 return -EINVAL;
1118 }
1119
1120 if (val)
1121 device->default_expires = val;
1122
1123 dasd_put_device(device);
1124 return count;
1125}
1126
1127static DEVICE_ATTR(expires, 0644, dasd_expires_show, dasd_expires_store);
1128
1086static struct attribute * dasd_attrs[] = { 1129static struct attribute * dasd_attrs[] = {
1087 &dev_attr_readonly.attr, 1130 &dev_attr_readonly.attr,
1088 &dev_attr_discipline.attr, 1131 &dev_attr_discipline.attr,
@@ -1094,6 +1137,7 @@ static struct attribute * dasd_attrs[] = {
1094 &dev_attr_eer_enabled.attr, 1137 &dev_attr_eer_enabled.attr,
1095 &dev_attr_erplog.attr, 1138 &dev_attr_erplog.attr,
1096 &dev_attr_failfast.attr, 1139 &dev_attr_failfast.attr,
1140 &dev_attr_expires.attr,
1097 NULL, 1141 NULL,
1098}; 1142};
1099 1143
diff --git a/drivers/s390/block/dasd_diag.c b/drivers/s390/block/dasd_diag.c
index 687f323cdc3..2b3bc3ec054 100644
--- a/drivers/s390/block/dasd_diag.c
+++ b/drivers/s390/block/dasd_diag.c
@@ -43,7 +43,7 @@ MODULE_LICENSE("GPL");
43 sizeof(struct dasd_diag_req)) / \ 43 sizeof(struct dasd_diag_req)) / \
44 sizeof(struct dasd_diag_bio)) / 2) 44 sizeof(struct dasd_diag_bio)) / 2)
45#define DIAG_MAX_RETRIES 32 45#define DIAG_MAX_RETRIES 32
46#define DIAG_TIMEOUT 50 * HZ 46#define DIAG_TIMEOUT 50
47 47
48static struct dasd_discipline dasd_diag_discipline; 48static struct dasd_discipline dasd_diag_discipline;
49 49
@@ -360,6 +360,8 @@ dasd_diag_check_device(struct dasd_device *device)
360 goto out; 360 goto out;
361 } 361 }
362 362
363 device->default_expires = DIAG_TIMEOUT;
364
363 /* Figure out position of label block */ 365 /* Figure out position of label block */
364 switch (private->rdc_data.vdev_class) { 366 switch (private->rdc_data.vdev_class) {
365 case DEV_CLASS_FBA: 367 case DEV_CLASS_FBA:
@@ -563,7 +565,7 @@ static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
563 cqr->startdev = memdev; 565 cqr->startdev = memdev;
564 cqr->memdev = memdev; 566 cqr->memdev = memdev;
565 cqr->block = block; 567 cqr->block = block;
566 cqr->expires = DIAG_TIMEOUT; 568 cqr->expires = memdev->default_expires * HZ;
567 cqr->status = DASD_CQR_FILLED; 569 cqr->status = DASD_CQR_FILLED;
568 return cqr; 570 return cqr;
569} 571}
diff --git a/drivers/s390/block/dasd_eckd.c b/drivers/s390/block/dasd_eckd.c
index ab84da5592e..66360c24bd4 100644
--- a/drivers/s390/block/dasd_eckd.c
+++ b/drivers/s390/block/dasd_eckd.c
@@ -82,6 +82,14 @@ static struct ccw_driver dasd_eckd_driver; /* see below */
82#define INIT_CQR_UNFORMATTED 1 82#define INIT_CQR_UNFORMATTED 1
83#define INIT_CQR_ERROR 2 83#define INIT_CQR_ERROR 2
84 84
85/* emergency request for reserve/release */
86static struct {
87 struct dasd_ccw_req cqr;
88 struct ccw1 ccw;
89 char data[32];
90} *dasd_reserve_req;
91static DEFINE_MUTEX(dasd_reserve_mutex);
92
85 93
86/* initial attempt at a probe function. this can be simplified once 94/* initial attempt at a probe function. this can be simplified once
87 * the other detection code is gone */ 95 * the other detection code is gone */
@@ -1107,8 +1115,9 @@ dasd_eckd_check_characteristics(struct dasd_device *device)
1107 struct dasd_eckd_private *private; 1115 struct dasd_eckd_private *private;
1108 struct dasd_block *block; 1116 struct dasd_block *block;
1109 struct dasd_uid temp_uid; 1117 struct dasd_uid temp_uid;
1110 int is_known, rc; 1118 int is_known, rc, i;
1111 int readonly; 1119 int readonly;
1120 unsigned long value;
1112 1121
1113 if (!ccw_device_is_pathgroup(device->cdev)) { 1122 if (!ccw_device_is_pathgroup(device->cdev)) {
1114 dev_warn(&device->cdev->dev, 1123 dev_warn(&device->cdev->dev,
@@ -1143,6 +1152,18 @@ dasd_eckd_check_characteristics(struct dasd_device *device)
1143 if (rc) 1152 if (rc)
1144 goto out_err1; 1153 goto out_err1;
1145 1154
1155 /* set default timeout */
1156 device->default_expires = DASD_EXPIRES;
1157 if (private->gneq) {
1158 value = 1;
1159 for (i = 0; i < private->gneq->timeout.value; i++)
1160 value = 10 * value;
1161 value = value * private->gneq->timeout.number;
1162 /* do not accept useless values */
1163 if (value != 0 && value <= DASD_EXPIRES_MAX)
1164 device->default_expires = value;
1165 }
1166
1146 /* Generate device unique id */ 1167 /* Generate device unique id */
1147 rc = dasd_eckd_generate_uid(device); 1168 rc = dasd_eckd_generate_uid(device);
1148 if (rc) 1169 if (rc)
@@ -1973,7 +1994,7 @@ static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
1973 cqr->startdev = startdev; 1994 cqr->startdev = startdev;
1974 cqr->memdev = startdev; 1995 cqr->memdev = startdev;
1975 cqr->block = block; 1996 cqr->block = block;
1976 cqr->expires = 5 * 60 * HZ; /* 5 minutes */ 1997 cqr->expires = startdev->default_expires * HZ; /* default 5 minutes */
1977 cqr->lpm = private->path_data.ppm; 1998 cqr->lpm = private->path_data.ppm;
1978 cqr->retries = 256; 1999 cqr->retries = 256;
1979 cqr->buildclk = get_clock(); 2000 cqr->buildclk = get_clock();
@@ -2150,7 +2171,7 @@ static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_track(
2150 cqr->startdev = startdev; 2171 cqr->startdev = startdev;
2151 cqr->memdev = startdev; 2172 cqr->memdev = startdev;
2152 cqr->block = block; 2173 cqr->block = block;
2153 cqr->expires = 5 * 60 * HZ; /* 5 minutes */ 2174 cqr->expires = startdev->default_expires * HZ; /* default 5 minutes */
2154 cqr->lpm = private->path_data.ppm; 2175 cqr->lpm = private->path_data.ppm;
2155 cqr->retries = 256; 2176 cqr->retries = 256;
2156 cqr->buildclk = get_clock(); 2177 cqr->buildclk = get_clock();
@@ -2398,7 +2419,7 @@ static struct dasd_ccw_req *dasd_eckd_build_cp_tpm_track(
2398 cqr->startdev = startdev; 2419 cqr->startdev = startdev;
2399 cqr->memdev = startdev; 2420 cqr->memdev = startdev;
2400 cqr->block = block; 2421 cqr->block = block;
2401 cqr->expires = 5 * 60 * HZ; /* 5 minutes */ 2422 cqr->expires = startdev->default_expires * HZ; /* default 5 minutes */
2402 cqr->lpm = private->path_data.ppm; 2423 cqr->lpm = private->path_data.ppm;
2403 cqr->retries = 256; 2424 cqr->retries = 256;
2404 cqr->buildclk = get_clock(); 2425 cqr->buildclk = get_clock();
@@ -2645,15 +2666,23 @@ dasd_eckd_release(struct dasd_device *device)
2645 struct dasd_ccw_req *cqr; 2666 struct dasd_ccw_req *cqr;
2646 int rc; 2667 int rc;
2647 struct ccw1 *ccw; 2668 struct ccw1 *ccw;
2669 int useglobal;
2648 2670
2649 if (!capable(CAP_SYS_ADMIN)) 2671 if (!capable(CAP_SYS_ADMIN))
2650 return -EACCES; 2672 return -EACCES;
2651 2673
2674 useglobal = 0;
2652 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device); 2675 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
2653 if (IS_ERR(cqr)) { 2676 if (IS_ERR(cqr)) {
2654 DBF_DEV_EVENT(DBF_WARNING, device, "%s", 2677 mutex_lock(&dasd_reserve_mutex);
2655 "Could not allocate initialization request"); 2678 useglobal = 1;
2656 return PTR_ERR(cqr); 2679 cqr = &dasd_reserve_req->cqr;
2680 memset(cqr, 0, sizeof(*cqr));
2681 memset(&dasd_reserve_req->ccw, 0,
2682 sizeof(dasd_reserve_req->ccw));
2683 cqr->cpaddr = &dasd_reserve_req->ccw;
2684 cqr->data = &dasd_reserve_req->data;
2685 cqr->magic = DASD_ECKD_MAGIC;
2657 } 2686 }
2658 ccw = cqr->cpaddr; 2687 ccw = cqr->cpaddr;
2659 ccw->cmd_code = DASD_ECKD_CCW_RELEASE; 2688 ccw->cmd_code = DASD_ECKD_CCW_RELEASE;
@@ -2671,7 +2700,10 @@ dasd_eckd_release(struct dasd_device *device)
2671 2700
2672 rc = dasd_sleep_on_immediatly(cqr); 2701 rc = dasd_sleep_on_immediatly(cqr);
2673 2702
2674 dasd_sfree_request(cqr, cqr->memdev); 2703 if (useglobal)
2704 mutex_unlock(&dasd_reserve_mutex);
2705 else
2706 dasd_sfree_request(cqr, cqr->memdev);
2675 return rc; 2707 return rc;
2676} 2708}
2677 2709
@@ -2687,15 +2719,23 @@ dasd_eckd_reserve(struct dasd_device *device)
2687 struct dasd_ccw_req *cqr; 2719 struct dasd_ccw_req *cqr;
2688 int rc; 2720 int rc;
2689 struct ccw1 *ccw; 2721 struct ccw1 *ccw;
2722 int useglobal;
2690 2723
2691 if (!capable(CAP_SYS_ADMIN)) 2724 if (!capable(CAP_SYS_ADMIN))
2692 return -EACCES; 2725 return -EACCES;
2693 2726
2727 useglobal = 0;
2694 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device); 2728 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
2695 if (IS_ERR(cqr)) { 2729 if (IS_ERR(cqr)) {
2696 DBF_DEV_EVENT(DBF_WARNING, device, "%s", 2730 mutex_lock(&dasd_reserve_mutex);
2697 "Could not allocate initialization request"); 2731 useglobal = 1;
2698 return PTR_ERR(cqr); 2732 cqr = &dasd_reserve_req->cqr;
2733 memset(cqr, 0, sizeof(*cqr));
2734 memset(&dasd_reserve_req->ccw, 0,
2735 sizeof(dasd_reserve_req->ccw));
2736 cqr->cpaddr = &dasd_reserve_req->ccw;
2737 cqr->data = &dasd_reserve_req->data;
2738 cqr->magic = DASD_ECKD_MAGIC;
2699 } 2739 }
2700 ccw = cqr->cpaddr; 2740 ccw = cqr->cpaddr;
2701 ccw->cmd_code = DASD_ECKD_CCW_RESERVE; 2741 ccw->cmd_code = DASD_ECKD_CCW_RESERVE;
@@ -2713,7 +2753,10 @@ dasd_eckd_reserve(struct dasd_device *device)
2713 2753
2714 rc = dasd_sleep_on_immediatly(cqr); 2754 rc = dasd_sleep_on_immediatly(cqr);
2715 2755
2716 dasd_sfree_request(cqr, cqr->memdev); 2756 if (useglobal)
2757 mutex_unlock(&dasd_reserve_mutex);
2758 else
2759 dasd_sfree_request(cqr, cqr->memdev);
2717 return rc; 2760 return rc;
2718} 2761}
2719 2762
@@ -2728,15 +2771,23 @@ dasd_eckd_steal_lock(struct dasd_device *device)
2728 struct dasd_ccw_req *cqr; 2771 struct dasd_ccw_req *cqr;
2729 int rc; 2772 int rc;
2730 struct ccw1 *ccw; 2773 struct ccw1 *ccw;
2774 int useglobal;
2731 2775
2732 if (!capable(CAP_SYS_ADMIN)) 2776 if (!capable(CAP_SYS_ADMIN))
2733 return -EACCES; 2777 return -EACCES;
2734 2778
2779 useglobal = 0;
2735 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device); 2780 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
2736 if (IS_ERR(cqr)) { 2781 if (IS_ERR(cqr)) {
2737 DBF_DEV_EVENT(DBF_WARNING, device, "%s", 2782 mutex_lock(&dasd_reserve_mutex);
2738 "Could not allocate initialization request"); 2783 useglobal = 1;
2739 return PTR_ERR(cqr); 2784 cqr = &dasd_reserve_req->cqr;
2785 memset(cqr, 0, sizeof(*cqr));
2786 memset(&dasd_reserve_req->ccw, 0,
2787 sizeof(dasd_reserve_req->ccw));
2788 cqr->cpaddr = &dasd_reserve_req->ccw;
2789 cqr->data = &dasd_reserve_req->data;
2790 cqr->magic = DASD_ECKD_MAGIC;
2740 } 2791 }
2741 ccw = cqr->cpaddr; 2792 ccw = cqr->cpaddr;
2742 ccw->cmd_code = DASD_ECKD_CCW_SLCK; 2793 ccw->cmd_code = DASD_ECKD_CCW_SLCK;
@@ -2754,7 +2805,10 @@ dasd_eckd_steal_lock(struct dasd_device *device)
2754 2805
2755 rc = dasd_sleep_on_immediatly(cqr); 2806 rc = dasd_sleep_on_immediatly(cqr);
2756 2807
2757 dasd_sfree_request(cqr, cqr->memdev); 2808 if (useglobal)
2809 mutex_unlock(&dasd_reserve_mutex);
2810 else
2811 dasd_sfree_request(cqr, cqr->memdev);
2758 return rc; 2812 return rc;
2759} 2813}
2760 2814
@@ -3488,10 +3542,15 @@ dasd_eckd_init(void)
3488 int ret; 3542 int ret;
3489 3543
3490 ASCEBC(dasd_eckd_discipline.ebcname, 4); 3544 ASCEBC(dasd_eckd_discipline.ebcname, 4);
3545 dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
3546 GFP_KERNEL | GFP_DMA);
3547 if (!dasd_reserve_req)
3548 return -ENOMEM;
3491 ret = ccw_driver_register(&dasd_eckd_driver); 3549 ret = ccw_driver_register(&dasd_eckd_driver);
3492 if (!ret) 3550 if (!ret)
3493 wait_for_device_probe(); 3551 wait_for_device_probe();
3494 3552 else
3553 kfree(dasd_reserve_req);
3495 return ret; 3554 return ret;
3496} 3555}
3497 3556
@@ -3499,6 +3558,7 @@ static void __exit
3499dasd_eckd_cleanup(void) 3558dasd_eckd_cleanup(void)
3500{ 3559{
3501 ccw_driver_unregister(&dasd_eckd_driver); 3560 ccw_driver_unregister(&dasd_eckd_driver);
3561 kfree(dasd_reserve_req);
3502} 3562}
3503 3563
3504module_init(dasd_eckd_init); 3564module_init(dasd_eckd_init);
diff --git a/drivers/s390/block/dasd_eckd.h b/drivers/s390/block/dasd_eckd.h
index dd6385a5af1..0eb49655a6c 100644
--- a/drivers/s390/block/dasd_eckd.h
+++ b/drivers/s390/block/dasd_eckd.h
@@ -320,7 +320,12 @@ struct dasd_gneq {
320 __u8 identifier:2; 320 __u8 identifier:2;
321 __u8 reserved:6; 321 __u8 reserved:6;
322 } __attribute__ ((packed)) flags; 322 } __attribute__ ((packed)) flags;
323 __u8 reserved[7]; 323 __u8 reserved[5];
324 struct {
325 __u8 value:2;
326 __u8 number:6;
327 } __attribute__ ((packed)) timeout;
328 __u8 reserved3;
324 __u16 subsystemID; 329 __u16 subsystemID;
325 __u8 reserved2[22]; 330 __u8 reserved2[22];
326} __attribute__ ((packed)); 331} __attribute__ ((packed));
diff --git a/drivers/s390/block/dasd_fba.c b/drivers/s390/block/dasd_fba.c
index 37282b90eec..bec5486e0e6 100644
--- a/drivers/s390/block/dasd_fba.c
+++ b/drivers/s390/block/dasd_fba.c
@@ -163,6 +163,8 @@ dasd_fba_check_characteristics(struct dasd_device *device)
163 return rc; 163 return rc;
164 } 164 }
165 165
166 device->default_expires = DASD_EXPIRES;
167
166 readonly = dasd_device_is_ro(device); 168 readonly = dasd_device_is_ro(device);
167 if (readonly) 169 if (readonly)
168 set_bit(DASD_FLAG_DEVICE_RO, &device->flags); 170 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
@@ -370,7 +372,7 @@ static struct dasd_ccw_req *dasd_fba_build_cp(struct dasd_device * memdev,
370 cqr->startdev = memdev; 372 cqr->startdev = memdev;
371 cqr->memdev = memdev; 373 cqr->memdev = memdev;
372 cqr->block = block; 374 cqr->block = block;
373 cqr->expires = 5 * 60 * HZ; /* 5 minutes */ 375 cqr->expires = memdev->default_expires * HZ; /* default 5 minutes */
374 cqr->retries = 32; 376 cqr->retries = 32;
375 cqr->buildclk = get_clock(); 377 cqr->buildclk = get_clock();
376 cqr->status = DASD_CQR_FILLED; 378 cqr->status = DASD_CQR_FILLED;
diff --git a/drivers/s390/block/dasd_int.h b/drivers/s390/block/dasd_int.h
index 49b431d135e..500678d7116 100644
--- a/drivers/s390/block/dasd_int.h
+++ b/drivers/s390/block/dasd_int.h
@@ -186,7 +186,7 @@ struct dasd_ccw_req {
186 186
187 /* ... and how */ 187 /* ... and how */
188 unsigned long starttime; /* jiffies time of request start */ 188 unsigned long starttime; /* jiffies time of request start */
189 int expires; /* expiration period in jiffies */ 189 unsigned long expires; /* expiration period in jiffies */
190 char lpm; /* logical path mask */ 190 char lpm; /* logical path mask */
191 void *data; /* pointer to data area */ 191 void *data; /* pointer to data area */
192 192
@@ -224,6 +224,9 @@ struct dasd_ccw_req {
224#define DASD_CQR_CLEARED 0x84 /* request was cleared */ 224#define DASD_CQR_CLEARED 0x84 /* request was cleared */
225#define DASD_CQR_SUCCESS 0x85 /* request was successful */ 225#define DASD_CQR_SUCCESS 0x85 /* request was successful */
226 226
227/* default expiration time*/
228#define DASD_EXPIRES 300
229#define DASD_EXPIRES_MAX 40000000
227 230
228/* per dasd_ccw_req flags */ 231/* per dasd_ccw_req flags */
229#define DASD_CQR_FLAGS_USE_ERP 0 /* use ERP for this request */ 232#define DASD_CQR_FLAGS_USE_ERP 0 /* use ERP for this request */
@@ -404,6 +407,9 @@ struct dasd_device {
404 407
405 /* hook for alias management */ 408 /* hook for alias management */
406 struct list_head alias_list; 409 struct list_head alias_list;
410
411 /* default expiration time in s */
412 unsigned long default_expires;
407}; 413};
408 414
409struct dasd_block { 415struct dasd_block {
diff --git a/drivers/s390/block/dcssblk.c b/drivers/s390/block/dcssblk.c
index 9b43ae94beb..2bd72aa34c5 100644
--- a/drivers/s390/block/dcssblk.c
+++ b/drivers/s390/block/dcssblk.c
@@ -14,6 +14,7 @@
14#include <linux/init.h> 14#include <linux/init.h>
15#include <linux/slab.h> 15#include <linux/slab.h>
16#include <linux/blkdev.h> 16#include <linux/blkdev.h>
17#include <linux/smp_lock.h>
17#include <linux/completion.h> 18#include <linux/completion.h>
18#include <linux/interrupt.h> 19#include <linux/interrupt.h>
19#include <linux/platform_device.h> 20#include <linux/platform_device.h>
@@ -775,6 +776,7 @@ dcssblk_open(struct block_device *bdev, fmode_t mode)
775 struct dcssblk_dev_info *dev_info; 776 struct dcssblk_dev_info *dev_info;
776 int rc; 777 int rc;
777 778
779 lock_kernel();
778 dev_info = bdev->bd_disk->private_data; 780 dev_info = bdev->bd_disk->private_data;
779 if (NULL == dev_info) { 781 if (NULL == dev_info) {
780 rc = -ENODEV; 782 rc = -ENODEV;
@@ -784,6 +786,7 @@ dcssblk_open(struct block_device *bdev, fmode_t mode)
784 bdev->bd_block_size = 4096; 786 bdev->bd_block_size = 4096;
785 rc = 0; 787 rc = 0;
786out: 788out:
789 unlock_kernel();
787 return rc; 790 return rc;
788} 791}
789 792
@@ -794,6 +797,7 @@ dcssblk_release(struct gendisk *disk, fmode_t mode)
794 struct segment_info *entry; 797 struct segment_info *entry;
795 int rc; 798 int rc;
796 799
800 lock_kernel();
797 if (!dev_info) { 801 if (!dev_info) {
798 rc = -ENODEV; 802 rc = -ENODEV;
799 goto out; 803 goto out;
@@ -811,6 +815,7 @@ dcssblk_release(struct gendisk *disk, fmode_t mode)
811 up_write(&dcssblk_devices_sem); 815 up_write(&dcssblk_devices_sem);
812 rc = 0; 816 rc = 0;
813out: 817out:
818 unlock_kernel();
814 return rc; 819 return rc;
815} 820}
816 821
diff --git a/drivers/s390/char/tape_block.c b/drivers/s390/char/tape_block.c
index 097da8ce6be..b7de02525ec 100644
--- a/drivers/s390/char/tape_block.c
+++ b/drivers/s390/char/tape_block.c
@@ -16,6 +16,7 @@
16#include <linux/fs.h> 16#include <linux/fs.h>
17#include <linux/module.h> 17#include <linux/module.h>
18#include <linux/blkdev.h> 18#include <linux/blkdev.h>
19#include <linux/smp_lock.h>
19#include <linux/interrupt.h> 20#include <linux/interrupt.h>
20#include <linux/buffer_head.h> 21#include <linux/buffer_head.h>
21#include <linux/kernel.h> 22#include <linux/kernel.h>
@@ -361,6 +362,7 @@ tapeblock_open(struct block_device *bdev, fmode_t mode)
361 struct tape_device * device; 362 struct tape_device * device;
362 int rc; 363 int rc;
363 364
365 lock_kernel();
364 device = tape_get_device(disk->private_data); 366 device = tape_get_device(disk->private_data);
365 367
366 if (device->required_tapemarks) { 368 if (device->required_tapemarks) {
@@ -384,12 +386,14 @@ tapeblock_open(struct block_device *bdev, fmode_t mode)
384 * is called. 386 * is called.
385 */ 387 */
386 tape_state_set(device, TS_BLKUSE); 388 tape_state_set(device, TS_BLKUSE);
389 unlock_kernel();
387 return 0; 390 return 0;
388 391
389release: 392release:
390 tape_release(device); 393 tape_release(device);
391 put_device: 394 put_device:
392 tape_put_device(device); 395 tape_put_device(device);
396 unlock_kernel();
393 return rc; 397 return rc;
394} 398}
395 399
@@ -403,10 +407,12 @@ static int
403tapeblock_release(struct gendisk *disk, fmode_t mode) 407tapeblock_release(struct gendisk *disk, fmode_t mode)
404{ 408{
405 struct tape_device *device = disk->private_data; 409 struct tape_device *device = disk->private_data;
406 410
411 lock_kernel();
407 tape_state_set(device, TS_IN_USE); 412 tape_state_set(device, TS_IN_USE);
408 tape_release(device); 413 tape_release(device);
409 tape_put_device(device); 414 tape_put_device(device);
415 unlock_kernel();
410 416
411 return 0; 417 return 0;
412} 418}
diff --git a/drivers/s390/cio/ccwreq.c b/drivers/s390/cio/ccwreq.c
index 7f206ed44fd..d15f8b4d78b 100644
--- a/drivers/s390/cio/ccwreq.c
+++ b/drivers/s390/cio/ccwreq.c
@@ -38,9 +38,13 @@ static u16 ccwreq_next_path(struct ccw_device *cdev)
38{ 38{
39 struct ccw_request *req = &cdev->private->req; 39 struct ccw_request *req = &cdev->private->req;
40 40
41 if (!req->singlepath) {
42 req->mask = 0;
43 goto out;
44 }
41 req->retries = req->maxretries; 45 req->retries = req->maxretries;
42 req->mask = lpm_adjust(req->mask >>= 1, req->lpm); 46 req->mask = lpm_adjust(req->mask >>= 1, req->lpm);
43 47out:
44 return req->mask; 48 return req->mask;
45} 49}
46 50
@@ -113,8 +117,12 @@ void ccw_request_start(struct ccw_device *cdev)
113{ 117{
114 struct ccw_request *req = &cdev->private->req; 118 struct ccw_request *req = &cdev->private->req;
115 119
116 /* Try all paths twice to counter link flapping. */ 120 if (req->singlepath) {
117 req->mask = 0x8080; 121 /* Try all paths twice to counter link flapping. */
122 req->mask = 0x8080;
123 } else
124 req->mask = req->lpm;
125
118 req->retries = req->maxretries; 126 req->retries = req->maxretries;
119 req->mask = lpm_adjust(req->mask, req->lpm); 127 req->mask = lpm_adjust(req->mask, req->lpm);
120 req->drc = 0; 128 req->drc = 0;
@@ -182,6 +190,8 @@ static enum io_status ccwreq_status(struct ccw_device *cdev, struct irb *lcirb)
182 /* Ask the driver what to do */ 190 /* Ask the driver what to do */
183 if (cdev->drv && cdev->drv->uc_handler) { 191 if (cdev->drv && cdev->drv->uc_handler) {
184 todo = cdev->drv->uc_handler(cdev, lcirb); 192 todo = cdev->drv->uc_handler(cdev, lcirb);
193 CIO_TRACE_EVENT(2, "uc_response");
194 CIO_HEX_EVENT(2, &todo, sizeof(todo));
185 switch (todo) { 195 switch (todo) {
186 case UC_TODO_RETRY: 196 case UC_TODO_RETRY:
187 return IO_STATUS_ERROR; 197 return IO_STATUS_ERROR;
diff --git a/drivers/s390/cio/chsc.c b/drivers/s390/cio/chsc.c
index 407d0e9adfa..4cbb1a6ca33 100644
--- a/drivers/s390/cio/chsc.c
+++ b/drivers/s390/cio/chsc.c
@@ -29,6 +29,7 @@
29#include "chsc.h" 29#include "chsc.h"
30 30
31static void *sei_page; 31static void *sei_page;
32static DEFINE_SPINLOCK(siosl_lock);
32static DEFINE_SPINLOCK(sda_lock); 33static DEFINE_SPINLOCK(sda_lock);
33 34
34/** 35/**
@@ -48,6 +49,7 @@ int chsc_error_from_response(int response)
48 case 0x0007: 49 case 0x0007:
49 case 0x0008: 50 case 0x0008:
50 case 0x000a: 51 case 0x000a:
52 case 0x0104:
51 return -EINVAL; 53 return -EINVAL;
52 case 0x0004: 54 case 0x0004:
53 return -EOPNOTSUPP; 55 return -EOPNOTSUPP;
@@ -974,3 +976,49 @@ int chsc_sstpi(void *page, void *result, size_t size)
974 return (rr->response.code == 0x0001) ? 0 : -EIO; 976 return (rr->response.code == 0x0001) ? 0 : -EIO;
975} 977}
976 978
979static struct {
980 struct chsc_header request;
981 u32 word1;
982 struct subchannel_id sid;
983 u32 word3;
984 struct chsc_header response;
985 u32 word[11];
986} __attribute__ ((packed)) siosl_area __attribute__ ((__aligned__(PAGE_SIZE)));
987
988int chsc_siosl(struct subchannel_id schid)
989{
990 unsigned long flags;
991 int ccode;
992 int rc;
993
994 spin_lock_irqsave(&siosl_lock, flags);
995 memset(&siosl_area, 0, sizeof(siosl_area));
996 siosl_area.request.length = 0x0010;
997 siosl_area.request.code = 0x0046;
998 siosl_area.word1 = 0x80000000;
999 siosl_area.sid = schid;
1000
1001 ccode = chsc(&siosl_area);
1002 if (ccode > 0) {
1003 if (ccode == 3)
1004 rc = -ENODEV;
1005 else
1006 rc = -EBUSY;
1007 CIO_MSG_EVENT(2, "chsc: chsc failed for 0.%x.%04x (ccode=%d)\n",
1008 schid.ssid, schid.sch_no, ccode);
1009 goto out;
1010 }
1011 rc = chsc_error_from_response(siosl_area.response.code);
1012 if (rc)
1013 CIO_MSG_EVENT(2, "chsc: siosl failed for 0.%x.%04x (rc=%04x)\n",
1014 schid.ssid, schid.sch_no,
1015 siosl_area.response.code);
1016 else
1017 CIO_MSG_EVENT(4, "chsc: siosl succeeded for 0.%x.%04x\n",
1018 schid.ssid, schid.sch_no);
1019out:
1020 spin_unlock_irqrestore(&siosl_lock, flags);
1021
1022 return rc;
1023}
1024EXPORT_SYMBOL_GPL(chsc_siosl);
diff --git a/drivers/s390/cio/chsc.h b/drivers/s390/cio/chsc.h
index 37aa611d4ac..5453013f094 100644
--- a/drivers/s390/cio/chsc.h
+++ b/drivers/s390/cio/chsc.h
@@ -80,4 +80,6 @@ int chsc_get_channel_measurement_chars(struct channel_path *chp);
80 80
81int chsc_error_from_response(int response); 81int chsc_error_from_response(int response);
82 82
83int chsc_siosl(struct subchannel_id schid);
84
83#endif 85#endif
diff --git a/drivers/s390/cio/device.c b/drivers/s390/cio/device.c
index 6d229f3523a..51bd3687d16 100644
--- a/drivers/s390/cio/device.c
+++ b/drivers/s390/cio/device.c
@@ -36,6 +36,7 @@
36#include "ioasm.h" 36#include "ioasm.h"
37#include "io_sch.h" 37#include "io_sch.h"
38#include "blacklist.h" 38#include "blacklist.h"
39#include "chsc.h"
39 40
40static struct timer_list recovery_timer; 41static struct timer_list recovery_timer;
41static DEFINE_SPINLOCK(recovery_lock); 42static DEFINE_SPINLOCK(recovery_lock);
@@ -486,9 +487,11 @@ static int online_store_handle_offline(struct ccw_device *cdev)
486 spin_lock_irq(cdev->ccwlock); 487 spin_lock_irq(cdev->ccwlock);
487 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL); 488 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
488 spin_unlock_irq(cdev->ccwlock); 489 spin_unlock_irq(cdev->ccwlock);
489 } else if (cdev->online && cdev->drv && cdev->drv->set_offline) 490 return 0;
491 }
492 if (cdev->drv && cdev->drv->set_offline)
490 return ccw_device_set_offline(cdev); 493 return ccw_device_set_offline(cdev);
491 return 0; 494 return -EINVAL;
492} 495}
493 496
494static int online_store_recog_and_online(struct ccw_device *cdev) 497static int online_store_recog_and_online(struct ccw_device *cdev)
@@ -505,8 +508,8 @@ static int online_store_recog_and_online(struct ccw_device *cdev)
505 return -EAGAIN; 508 return -EAGAIN;
506 } 509 }
507 if (cdev->drv && cdev->drv->set_online) 510 if (cdev->drv && cdev->drv->set_online)
508 ccw_device_set_online(cdev); 511 return ccw_device_set_online(cdev);
509 return 0; 512 return -EINVAL;
510} 513}
511 514
512static int online_store_handle_online(struct ccw_device *cdev, int force) 515static int online_store_handle_online(struct ccw_device *cdev, int force)
@@ -598,6 +601,25 @@ available_show (struct device *dev, struct device_attribute *attr, char *buf)
598 } 601 }
599} 602}
600 603
604static ssize_t
605initiate_logging(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
607{
608 struct subchannel *sch = to_subchannel(dev);
609 int rc;
610
611 rc = chsc_siosl(sch->schid);
612 if (rc < 0) {
613 pr_warning("Logging for subchannel 0.%x.%04x failed with "
614 "errno=%d\n",
615 sch->schid.ssid, sch->schid.sch_no, rc);
616 return rc;
617 }
618 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
619 sch->schid.ssid, sch->schid.sch_no);
620 return count;
621}
622
601static DEVICE_ATTR(chpids, 0444, chpids_show, NULL); 623static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
602static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL); 624static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
603static DEVICE_ATTR(devtype, 0444, devtype_show, NULL); 625static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
@@ -605,10 +627,12 @@ static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
605static DEVICE_ATTR(modalias, 0444, modalias_show, NULL); 627static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
606static DEVICE_ATTR(online, 0644, online_show, online_store); 628static DEVICE_ATTR(online, 0644, online_show, online_store);
607static DEVICE_ATTR(availability, 0444, available_show, NULL); 629static DEVICE_ATTR(availability, 0444, available_show, NULL);
630static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
608 631
609static struct attribute *io_subchannel_attrs[] = { 632static struct attribute *io_subchannel_attrs[] = {
610 &dev_attr_chpids.attr, 633 &dev_attr_chpids.attr,
611 &dev_attr_pimpampom.attr, 634 &dev_attr_pimpampom.attr,
635 &dev_attr_logging.attr,
612 NULL, 636 NULL,
613}; 637};
614 638
@@ -2036,6 +2060,21 @@ void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2036 } 2060 }
2037} 2061}
2038 2062
2063/**
2064 * ccw_device_siosl() - initiate logging
2065 * @cdev: ccw device
2066 *
2067 * This function is used to invoke model-dependent logging within the channel
2068 * subsystem.
2069 */
2070int ccw_device_siosl(struct ccw_device *cdev)
2071{
2072 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2073
2074 return chsc_siosl(sch->schid);
2075}
2076EXPORT_SYMBOL_GPL(ccw_device_siosl);
2077
2039MODULE_LICENSE("GPL"); 2078MODULE_LICENSE("GPL");
2040EXPORT_SYMBOL(ccw_device_set_online); 2079EXPORT_SYMBOL(ccw_device_set_online);
2041EXPORT_SYMBOL(ccw_device_set_offline); 2080EXPORT_SYMBOL(ccw_device_set_offline);
diff --git a/drivers/s390/cio/device_pgid.c b/drivers/s390/cio/device_pgid.c
index 6facb5499a6..82a5ad0d63f 100644
--- a/drivers/s390/cio/device_pgid.c
+++ b/drivers/s390/cio/device_pgid.c
@@ -208,6 +208,7 @@ static void spid_start(struct ccw_device *cdev)
208 req->timeout = PGID_TIMEOUT; 208 req->timeout = PGID_TIMEOUT;
209 req->maxretries = PGID_RETRIES; 209 req->maxretries = PGID_RETRIES;
210 req->lpm = 0x80; 210 req->lpm = 0x80;
211 req->singlepath = 1;
211 req->callback = spid_callback; 212 req->callback = spid_callback;
212 spid_do(cdev); 213 spid_do(cdev);
213} 214}
@@ -420,6 +421,7 @@ static void verify_start(struct ccw_device *cdev)
420 req->timeout = PGID_TIMEOUT; 421 req->timeout = PGID_TIMEOUT;
421 req->maxretries = PGID_RETRIES; 422 req->maxretries = PGID_RETRIES;
422 req->lpm = 0x80; 423 req->lpm = 0x80;
424 req->singlepath = 1;
423 if (cdev->private->flags.pgroup) { 425 if (cdev->private->flags.pgroup) {
424 CIO_TRACE_EVENT(4, "snid"); 426 CIO_TRACE_EVENT(4, "snid");
425 CIO_HEX_EVENT(4, devid, sizeof(*devid)); 427 CIO_HEX_EVENT(4, devid, sizeof(*devid));
@@ -507,6 +509,7 @@ void ccw_device_disband_start(struct ccw_device *cdev)
507 req->timeout = PGID_TIMEOUT; 509 req->timeout = PGID_TIMEOUT;
508 req->maxretries = PGID_RETRIES; 510 req->maxretries = PGID_RETRIES;
509 req->lpm = sch->schib.pmcw.pam & sch->opm; 511 req->lpm = sch->schib.pmcw.pam & sch->opm;
512 req->singlepath = 1;
510 req->callback = disband_callback; 513 req->callback = disband_callback;
511 fn = SPID_FUNC_DISBAND; 514 fn = SPID_FUNC_DISBAND;
512 if (cdev->private->flags.mpath) 515 if (cdev->private->flags.mpath)
diff --git a/drivers/s390/cio/io_sch.h b/drivers/s390/cio/io_sch.h
index b9ce712a7f2..469ef93f230 100644
--- a/drivers/s390/cio/io_sch.h
+++ b/drivers/s390/cio/io_sch.h
@@ -92,11 +92,12 @@ enum io_status {
92 * @filter: optional callback to adjust request status based on IRB data 92 * @filter: optional callback to adjust request status based on IRB data
93 * @callback: final callback 93 * @callback: final callback
94 * @data: user-defined pointer passed to all callbacks 94 * @data: user-defined pointer passed to all callbacks
95 * @singlepath: if set, use only one path from @lpm per start I/O
96 * @cancel: non-zero if request was cancelled
97 * @done: non-zero if request was finished
95 * @mask: current path mask 98 * @mask: current path mask
96 * @retries: current number of retries 99 * @retries: current number of retries
97 * @drc: delayed return code 100 * @drc: delayed return code
98 * @cancel: non-zero if request was cancelled
99 * @done: non-zero if request was finished
100 */ 101 */
101struct ccw_request { 102struct ccw_request {
102 struct ccw1 *cp; 103 struct ccw1 *cp;
@@ -108,12 +109,13 @@ struct ccw_request {
108 enum io_status); 109 enum io_status);
109 void (*callback)(struct ccw_device *, void *, int); 110 void (*callback)(struct ccw_device *, void *, int);
110 void *data; 111 void *data;
112 unsigned int singlepath:1;
111 /* These fields are used internally. */ 113 /* These fields are used internally. */
114 unsigned int cancel:1;
115 unsigned int done:1;
112 u16 mask; 116 u16 mask;
113 u16 retries; 117 u16 retries;
114 int drc; 118 int drc;
115 int cancel:1;
116 int done:1;
117} __attribute__((packed)); 119} __attribute__((packed));
118 120
119/* 121/*
diff --git a/drivers/s390/net/smsgiucv_app.c b/drivers/s390/net/smsgiucv_app.c
index 13768879020..4d2ea400042 100644
--- a/drivers/s390/net/smsgiucv_app.c
+++ b/drivers/s390/net/smsgiucv_app.c
@@ -180,6 +180,13 @@ static int __init smsgiucv_app_init(void)
180 goto fail_put_driver; 180 goto fail_put_driver;
181 } 181 }
182 182
183 /* convert sender to uppercase characters */
184 if (sender) {
185 int len = strlen(sender);
186 while (len--)
187 sender[len] = toupper(sender[len]);
188 }
189
183 /* register with the smsgiucv device driver */ 190 /* register with the smsgiucv device driver */
184 rc = smsg_register_callback(SMSG_PREFIX, smsg_app_callback); 191 rc = smsg_register_callback(SMSG_PREFIX, smsg_app_callback);
185 if (rc) { 192 if (rc) {
diff --git a/drivers/scsi/aha1542.c b/drivers/scsi/aha1542.c
index 2a8cf137f60..4f785f254c1 100644
--- a/drivers/scsi/aha1542.c
+++ b/drivers/scsi/aha1542.c
@@ -52,22 +52,6 @@
52#define SCSI_BUF_PA(address) isa_virt_to_bus(address) 52#define SCSI_BUF_PA(address) isa_virt_to_bus(address)
53#define SCSI_SG_PA(sgent) (isa_page_to_bus(sg_page((sgent))) + (sgent)->offset) 53#define SCSI_SG_PA(sgent) (isa_page_to_bus(sg_page((sgent))) + (sgent)->offset)
54 54
55static void BAD_SG_DMA(Scsi_Cmnd * SCpnt,
56 struct scatterlist *sgp,
57 int nseg,
58 int badseg)
59{
60 printk(KERN_CRIT "sgpnt[%d:%d] page %p/0x%llx length %u\n",
61 badseg, nseg, sg_virt(sgp),
62 (unsigned long long)SCSI_SG_PA(sgp),
63 sgp->length);
64
65 /*
66 * Not safe to continue.
67 */
68 panic("Buffer at physical address > 16Mb used for aha1542");
69}
70
71#include<linux/stat.h> 55#include<linux/stat.h>
72 56
73#ifdef DEBUG 57#ifdef DEBUG
@@ -691,8 +675,6 @@ static int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
691 } 675 }
692 scsi_for_each_sg(SCpnt, sg, sg_count, i) { 676 scsi_for_each_sg(SCpnt, sg, sg_count, i) {
693 any2scsi(cptr[i].dataptr, SCSI_SG_PA(sg)); 677 any2scsi(cptr[i].dataptr, SCSI_SG_PA(sg));
694 if (SCSI_SG_PA(sg) + sg->length - 1 > ISA_DMA_THRESHOLD)
695 BAD_SG_DMA(SCpnt, scsi_sglist(SCpnt), sg_count, i);
696 any2scsi(cptr[i].datalen, sg->length); 678 any2scsi(cptr[i].datalen, sg->length);
697 }; 679 };
698 any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain)); 680 any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
@@ -1133,16 +1115,9 @@ static int __init aha1542_detect(struct scsi_host_template * tpnt)
1133 release_region(bases[indx], 4); 1115 release_region(bases[indx], 4);
1134 continue; 1116 continue;
1135 } 1117 }
1136 /* For now we do this - until kmalloc is more intelligent
1137 we are resigned to stupid hacks like this */
1138 if (SCSI_BUF_PA(shpnt) >= ISA_DMA_THRESHOLD) {
1139 printk(KERN_ERR "Invalid address for shpnt with 1542.\n");
1140 goto unregister;
1141 }
1142 if (!aha1542_test_port(bases[indx], shpnt)) 1118 if (!aha1542_test_port(bases[indx], shpnt))
1143 goto unregister; 1119 goto unregister;
1144 1120
1145
1146 base_io = bases[indx]; 1121 base_io = bases[indx];
1147 1122
1148 /* Set the Bus on/off-times as not to ruin floppy performance */ 1123 /* Set the Bus on/off-times as not to ruin floppy performance */
diff --git a/drivers/scsi/osd/osd_initiator.c b/drivers/scsi/osd/osd_initiator.c
index ee4b6914667..fda4de3440c 100644
--- a/drivers/scsi/osd/osd_initiator.c
+++ b/drivers/scsi/osd/osd_initiator.c
@@ -716,7 +716,7 @@ static int _osd_req_list_objects(struct osd_request *or,
716 return PTR_ERR(bio); 716 return PTR_ERR(bio);
717 } 717 }
718 718
719 bio->bi_rw &= ~(1 << BIO_RW); 719 bio->bi_rw &= ~REQ_WRITE;
720 or->in.bio = bio; 720 or->in.bio = bio;
721 or->in.total_bytes = bio->bi_size; 721 or->in.total_bytes = bio->bi_size;
722 return 0; 722 return 0;
@@ -814,7 +814,7 @@ void osd_req_write(struct osd_request *or,
814{ 814{
815 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len); 815 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len);
816 WARN_ON(or->out.bio || or->out.total_bytes); 816 WARN_ON(or->out.bio || or->out.total_bytes);
817 WARN_ON(0 == bio_rw_flagged(bio, BIO_RW)); 817 WARN_ON(0 == (bio->bi_rw & REQ_WRITE));
818 or->out.bio = bio; 818 or->out.bio = bio;
819 or->out.total_bytes = len; 819 or->out.total_bytes = len;
820} 820}
@@ -829,7 +829,7 @@ int osd_req_write_kern(struct osd_request *or,
829 if (IS_ERR(bio)) 829 if (IS_ERR(bio))
830 return PTR_ERR(bio); 830 return PTR_ERR(bio);
831 831
832 bio->bi_rw |= (1 << BIO_RW); /* FIXME: bio_set_dir() */ 832 bio->bi_rw |= REQ_WRITE; /* FIXME: bio_set_dir() */
833 osd_req_write(or, obj, offset, bio, len); 833 osd_req_write(or, obj, offset, bio, len);
834 return 0; 834 return 0;
835} 835}
@@ -865,7 +865,7 @@ void osd_req_read(struct osd_request *or,
865{ 865{
866 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len); 866 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len);
867 WARN_ON(or->in.bio || or->in.total_bytes); 867 WARN_ON(or->in.bio || or->in.total_bytes);
868 WARN_ON(1 == bio_rw_flagged(bio, BIO_RW)); 868 WARN_ON(1 == (bio->bi_rw & REQ_WRITE));
869 or->in.bio = bio; 869 or->in.bio = bio;
870 or->in.total_bytes = len; 870 or->in.total_bytes = len;
871} 871}
diff --git a/drivers/scsi/scsi_error.c b/drivers/scsi/scsi_error.c
index 2bf98469dc4..bbbc186dbc1 100644
--- a/drivers/scsi/scsi_error.c
+++ b/drivers/scsi/scsi_error.c
@@ -320,7 +320,7 @@ static int scsi_check_sense(struct scsi_cmnd *scmd)
320 "changed. The Linux SCSI layer does not " 320 "changed. The Linux SCSI layer does not "
321 "automatically adjust these parameters.\n"); 321 "automatically adjust these parameters.\n");
322 322
323 if (blk_barrier_rq(scmd->request)) 323 if (scmd->request->cmd_flags & REQ_HARDBARRIER)
324 /* 324 /*
325 * barrier requests should always retry on UA 325 * barrier requests should always retry on UA
326 * otherwise block will get a spurious error 326 * otherwise block will get a spurious error
@@ -1331,16 +1331,16 @@ int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1331 case DID_OK: 1331 case DID_OK:
1332 break; 1332 break;
1333 case DID_BUS_BUSY: 1333 case DID_BUS_BUSY:
1334 return blk_failfast_transport(scmd->request); 1334 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
1335 case DID_PARITY: 1335 case DID_PARITY:
1336 return blk_failfast_dev(scmd->request); 1336 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
1337 case DID_ERROR: 1337 case DID_ERROR:
1338 if (msg_byte(scmd->result) == COMMAND_COMPLETE && 1338 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1339 status_byte(scmd->result) == RESERVATION_CONFLICT) 1339 status_byte(scmd->result) == RESERVATION_CONFLICT)
1340 return 0; 1340 return 0;
1341 /* fall through */ 1341 /* fall through */
1342 case DID_SOFT_ERROR: 1342 case DID_SOFT_ERROR:
1343 return blk_failfast_driver(scmd->request); 1343 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
1344 } 1344 }
1345 1345
1346 switch (status_byte(scmd->result)) { 1346 switch (status_byte(scmd->result)) {
@@ -1349,7 +1349,9 @@ int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1349 * assume caller has checked sense and determinted 1349 * assume caller has checked sense and determinted
1350 * the check condition was retryable. 1350 * the check condition was retryable.
1351 */ 1351 */
1352 return blk_failfast_dev(scmd->request); 1352 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
1353 scmd->request->cmd_type == REQ_TYPE_BLOCK_PC)
1354 return 1;
1353 } 1355 }
1354 1356
1355 return 0; 1357 return 0;
diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 1646fe7cbd4..b8de389636f 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -85,7 +85,7 @@ static void scsi_unprep_request(struct request *req)
85{ 85{
86 struct scsi_cmnd *cmd = req->special; 86 struct scsi_cmnd *cmd = req->special;
87 87
88 req->cmd_flags &= ~REQ_DONTPREP; 88 blk_unprep_request(req);
89 req->special = NULL; 89 req->special = NULL;
90 90
91 scsi_put_command(cmd); 91 scsi_put_command(cmd);
@@ -722,7 +722,7 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
722 sense_deferred = scsi_sense_is_deferred(&sshdr); 722 sense_deferred = scsi_sense_is_deferred(&sshdr);
723 } 723 }
724 724
725 if (blk_pc_request(req)) { /* SG_IO ioctl from block level */ 725 if (req->cmd_type == REQ_TYPE_BLOCK_PC) { /* SG_IO ioctl from block level */
726 req->errors = result; 726 req->errors = result;
727 if (result) { 727 if (result) {
728 if (sense_valid && req->sense) { 728 if (sense_valid && req->sense) {
@@ -757,7 +757,8 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
757 } 757 }
758 } 758 }
759 759
760 BUG_ON(blk_bidi_rq(req)); /* bidi not support for !blk_pc_request yet */ 760 /* no bidi support for !REQ_TYPE_BLOCK_PC yet */
761 BUG_ON(blk_bidi_rq(req));
761 762
762 /* 763 /*
763 * Next deal with any sectors which we were able to correctly 764 * Next deal with any sectors which we were able to correctly
@@ -1010,11 +1011,8 @@ int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
1010 1011
1011err_exit: 1012err_exit:
1012 scsi_release_buffers(cmd); 1013 scsi_release_buffers(cmd);
1013 if (error == BLKPREP_KILL) 1014 scsi_put_command(cmd);
1014 scsi_put_command(cmd); 1015 cmd->request->special = NULL;
1015 else /* BLKPREP_DEFER */
1016 scsi_unprep_request(cmd->request);
1017
1018 return error; 1016 return error;
1019} 1017}
1020EXPORT_SYMBOL(scsi_init_io); 1018EXPORT_SYMBOL(scsi_init_io);
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index cc8a1d1d915..8e2e893db9e 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -46,6 +46,7 @@
46#include <linux/blkdev.h> 46#include <linux/blkdev.h>
47#include <linux/blkpg.h> 47#include <linux/blkpg.h>
48#include <linux/delay.h> 48#include <linux/delay.h>
49#include <linux/smp_lock.h>
49#include <linux/mutex.h> 50#include <linux/mutex.h>
50#include <linux/string_helpers.h> 51#include <linux/string_helpers.h>
51#include <linux/async.h> 52#include <linux/async.h>
@@ -411,54 +412,85 @@ static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
411} 412}
412 413
413/** 414/**
414 * sd_prepare_discard - unmap blocks on thinly provisioned device 415 * scsi_setup_discard_cmnd - unmap blocks on thinly provisioned device
416 * @sdp: scsi device to operate one
415 * @rq: Request to prepare 417 * @rq: Request to prepare
416 * 418 *
417 * Will issue either UNMAP or WRITE SAME(16) depending on preference 419 * Will issue either UNMAP or WRITE SAME(16) depending on preference
418 * indicated by target device. 420 * indicated by target device.
419 **/ 421 **/
420static int sd_prepare_discard(struct request *rq) 422static int scsi_setup_discard_cmnd(struct scsi_device *sdp, struct request *rq)
421{ 423{
422 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); 424 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
423 struct bio *bio = rq->bio; 425 struct bio *bio = rq->bio;
424 sector_t sector = bio->bi_sector; 426 sector_t sector = bio->bi_sector;
425 unsigned int num = bio_sectors(bio); 427 unsigned int nr_sectors = bio_sectors(bio);
428 unsigned int len;
429 int ret;
430 struct page *page;
426 431
427 if (sdkp->device->sector_size == 4096) { 432 if (sdkp->device->sector_size == 4096) {
428 sector >>= 3; 433 sector >>= 3;
429 num >>= 3; 434 nr_sectors >>= 3;
430 } 435 }
431 436
432 rq->cmd_type = REQ_TYPE_BLOCK_PC;
433 rq->timeout = SD_TIMEOUT; 437 rq->timeout = SD_TIMEOUT;
434 438
435 memset(rq->cmd, 0, rq->cmd_len); 439 memset(rq->cmd, 0, rq->cmd_len);
436 440
441 page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
442 if (!page)
443 return BLKPREP_DEFER;
444
437 if (sdkp->unmap) { 445 if (sdkp->unmap) {
438 char *buf = kmap_atomic(bio_page(bio), KM_USER0); 446 char *buf = page_address(page);
439 447
448 rq->cmd_len = 10;
440 rq->cmd[0] = UNMAP; 449 rq->cmd[0] = UNMAP;
441 rq->cmd[8] = 24; 450 rq->cmd[8] = 24;
442 rq->cmd_len = 10;
443
444 /* Ensure that data length matches payload */
445 rq->__data_len = bio->bi_size = bio->bi_io_vec->bv_len = 24;
446 451
447 put_unaligned_be16(6 + 16, &buf[0]); 452 put_unaligned_be16(6 + 16, &buf[0]);
448 put_unaligned_be16(16, &buf[2]); 453 put_unaligned_be16(16, &buf[2]);
449 put_unaligned_be64(sector, &buf[8]); 454 put_unaligned_be64(sector, &buf[8]);
450 put_unaligned_be32(num, &buf[16]); 455 put_unaligned_be32(nr_sectors, &buf[16]);
451 456
452 kunmap_atomic(buf, KM_USER0); 457 len = 24;
453 } else { 458 } else {
459 rq->cmd_len = 16;
454 rq->cmd[0] = WRITE_SAME_16; 460 rq->cmd[0] = WRITE_SAME_16;
455 rq->cmd[1] = 0x8; /* UNMAP */ 461 rq->cmd[1] = 0x8; /* UNMAP */
456 put_unaligned_be64(sector, &rq->cmd[2]); 462 put_unaligned_be64(sector, &rq->cmd[2]);
457 put_unaligned_be32(num, &rq->cmd[10]); 463 put_unaligned_be32(nr_sectors, &rq->cmd[10]);
458 rq->cmd_len = 16; 464
465 len = sdkp->device->sector_size;
459 } 466 }
460 467
461 return BLKPREP_OK; 468 blk_add_request_payload(rq, page, len);
469 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
470 rq->buffer = page_address(page);
471 if (ret != BLKPREP_OK) {
472 __free_page(page);
473 rq->buffer = NULL;
474 }
475 return ret;
476}
477
478static int scsi_setup_flush_cmnd(struct scsi_device *sdp, struct request *rq)
479{
480 rq->timeout = SD_TIMEOUT;
481 rq->retries = SD_MAX_RETRIES;
482 rq->cmd[0] = SYNCHRONIZE_CACHE;
483 rq->cmd_len = 10;
484
485 return scsi_setup_blk_pc_cmnd(sdp, rq);
486}
487
488static void sd_unprep_fn(struct request_queue *q, struct request *rq)
489{
490 if (rq->cmd_flags & REQ_DISCARD) {
491 free_page((unsigned long)rq->buffer);
492 rq->buffer = NULL;
493 }
462} 494}
463 495
464/** 496/**
@@ -485,10 +517,13 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
485 * Discard request come in as REQ_TYPE_FS but we turn them into 517 * Discard request come in as REQ_TYPE_FS but we turn them into
486 * block PC requests to make life easier. 518 * block PC requests to make life easier.
487 */ 519 */
488 if (blk_discard_rq(rq)) 520 if (rq->cmd_flags & REQ_DISCARD) {
489 ret = sd_prepare_discard(rq); 521 ret = scsi_setup_discard_cmnd(sdp, rq);
490 522 goto out;
491 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { 523 } else if (rq->cmd_flags & REQ_FLUSH) {
524 ret = scsi_setup_flush_cmnd(sdp, rq);
525 goto out;
526 } else if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
492 ret = scsi_setup_blk_pc_cmnd(sdp, rq); 527 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
493 goto out; 528 goto out;
494 } else if (rq->cmd_type != REQ_TYPE_FS) { 529 } else if (rq->cmd_type != REQ_TYPE_FS) {
@@ -636,7 +671,7 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
636 SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD; 671 SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
637 SCpnt->cmnd[7] = 0x18; 672 SCpnt->cmnd[7] = 0x18;
638 SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32; 673 SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
639 SCpnt->cmnd[10] = protect | (blk_fua_rq(rq) ? 0x8 : 0); 674 SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
640 675
641 /* LBA */ 676 /* LBA */
642 SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0; 677 SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
@@ -661,7 +696,7 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
661 SCpnt->cmnd[31] = (unsigned char) this_count & 0xff; 696 SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
662 } else if (block > 0xffffffff) { 697 } else if (block > 0xffffffff) {
663 SCpnt->cmnd[0] += READ_16 - READ_6; 698 SCpnt->cmnd[0] += READ_16 - READ_6;
664 SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0); 699 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
665 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0; 700 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
666 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0; 701 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
667 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0; 702 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
@@ -682,7 +717,7 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
682 this_count = 0xffff; 717 this_count = 0xffff;
683 718
684 SCpnt->cmnd[0] += READ_10 - READ_6; 719 SCpnt->cmnd[0] += READ_10 - READ_6;
685 SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0); 720 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
686 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; 721 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
687 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; 722 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
688 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; 723 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
@@ -691,7 +726,7 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
691 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; 726 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
692 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; 727 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
693 } else { 728 } else {
694 if (unlikely(blk_fua_rq(rq))) { 729 if (unlikely(rq->cmd_flags & REQ_FUA)) {
695 /* 730 /*
696 * This happens only if this drive failed 731 * This happens only if this drive failed
697 * 10byte rw command with ILLEGAL_REQUEST 732 * 10byte rw command with ILLEGAL_REQUEST
@@ -745,6 +780,8 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
745 * or from within the kernel (e.g. as a result of a mount(1) ). 780 * or from within the kernel (e.g. as a result of a mount(1) ).
746 * In the latter case @inode and @filp carry an abridged amount 781 * In the latter case @inode and @filp carry an abridged amount
747 * of information as noted above. 782 * of information as noted above.
783 *
784 * Locking: called with bdev->bd_mutex held.
748 **/ 785 **/
749static int sd_open(struct block_device *bdev, fmode_t mode) 786static int sd_open(struct block_device *bdev, fmode_t mode)
750{ 787{
@@ -799,7 +836,7 @@ static int sd_open(struct block_device *bdev, fmode_t mode)
799 if (!scsi_device_online(sdev)) 836 if (!scsi_device_online(sdev))
800 goto error_out; 837 goto error_out;
801 838
802 if (!sdkp->openers++ && sdev->removable) { 839 if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
803 if (scsi_block_when_processing_errors(sdev)) 840 if (scsi_block_when_processing_errors(sdev))
804 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT); 841 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
805 } 842 }
@@ -823,6 +860,8 @@ error_autopm:
823 * 860 *
824 * Note: may block (uninterruptible) if error recovery is underway 861 * Note: may block (uninterruptible) if error recovery is underway
825 * on this disk. 862 * on this disk.
863 *
864 * Locking: called with bdev->bd_mutex held.
826 **/ 865 **/
827static int sd_release(struct gendisk *disk, fmode_t mode) 866static int sd_release(struct gendisk *disk, fmode_t mode)
828{ 867{
@@ -831,7 +870,7 @@ static int sd_release(struct gendisk *disk, fmode_t mode)
831 870
832 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n")); 871 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
833 872
834 if (!--sdkp->openers && sdev->removable) { 873 if (atomic_dec_return(&sdkp->openers) && sdev->removable) {
835 if (scsi_block_when_processing_errors(sdev)) 874 if (scsi_block_when_processing_errors(sdev))
836 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW); 875 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
837 } 876 }
@@ -904,7 +943,7 @@ static int sd_ioctl(struct block_device *bdev, fmode_t mode,
904 error = scsi_nonblockable_ioctl(sdp, cmd, p, 943 error = scsi_nonblockable_ioctl(sdp, cmd, p,
905 (mode & FMODE_NDELAY) != 0); 944 (mode & FMODE_NDELAY) != 0);
906 if (!scsi_block_when_processing_errors(sdp) || !error) 945 if (!scsi_block_when_processing_errors(sdp) || !error)
907 return error; 946 goto out;
908 947
909 /* 948 /*
910 * Send SCSI addressing ioctls directly to mid level, send other 949 * Send SCSI addressing ioctls directly to mid level, send other
@@ -914,13 +953,17 @@ static int sd_ioctl(struct block_device *bdev, fmode_t mode,
914 switch (cmd) { 953 switch (cmd) {
915 case SCSI_IOCTL_GET_IDLUN: 954 case SCSI_IOCTL_GET_IDLUN:
916 case SCSI_IOCTL_GET_BUS_NUMBER: 955 case SCSI_IOCTL_GET_BUS_NUMBER:
917 return scsi_ioctl(sdp, cmd, p); 956 error = scsi_ioctl(sdp, cmd, p);
957 break;
918 default: 958 default:
919 error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p); 959 error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
920 if (error != -ENOTTY) 960 if (error != -ENOTTY)
921 return error; 961 break;
962 error = scsi_ioctl(sdp, cmd, p);
963 break;
922 } 964 }
923 return scsi_ioctl(sdp, cmd, p); 965out:
966 return error;
924} 967}
925 968
926static void set_media_not_present(struct scsi_disk *sdkp) 969static void set_media_not_present(struct scsi_disk *sdkp)
@@ -1045,15 +1088,6 @@ static int sd_sync_cache(struct scsi_disk *sdkp)
1045 return 0; 1088 return 0;
1046} 1089}
1047 1090
1048static void sd_prepare_flush(struct request_queue *q, struct request *rq)
1049{
1050 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1051 rq->timeout = SD_TIMEOUT;
1052 rq->retries = SD_MAX_RETRIES;
1053 rq->cmd[0] = SYNCHRONIZE_CACHE;
1054 rq->cmd_len = 10;
1055}
1056
1057static void sd_rescan(struct device *dev) 1091static void sd_rescan(struct device *dev)
1058{ 1092{
1059 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); 1093 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
@@ -1103,7 +1137,7 @@ static const struct block_device_operations sd_fops = {
1103 .owner = THIS_MODULE, 1137 .owner = THIS_MODULE,
1104 .open = sd_open, 1138 .open = sd_open,
1105 .release = sd_release, 1139 .release = sd_release,
1106 .locked_ioctl = sd_ioctl, 1140 .ioctl = sd_ioctl,
1107 .getgeo = sd_getgeo, 1141 .getgeo = sd_getgeo,
1108#ifdef CONFIG_COMPAT 1142#ifdef CONFIG_COMPAT
1109 .compat_ioctl = sd_compat_ioctl, 1143 .compat_ioctl = sd_compat_ioctl,
@@ -1120,7 +1154,7 @@ static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1120 u64 bad_lba; 1154 u64 bad_lba;
1121 int info_valid; 1155 int info_valid;
1122 1156
1123 if (!blk_fs_request(scmd->request)) 1157 if (scmd->request->cmd_type != REQ_TYPE_FS)
1124 return 0; 1158 return 0;
1125 1159
1126 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer, 1160 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
@@ -1171,6 +1205,12 @@ static int sd_done(struct scsi_cmnd *SCpnt)
1171 int sense_valid = 0; 1205 int sense_valid = 0;
1172 int sense_deferred = 0; 1206 int sense_deferred = 0;
1173 1207
1208 if (SCpnt->request->cmd_flags & REQ_DISCARD) {
1209 if (!result)
1210 scsi_set_resid(SCpnt, 0);
1211 return good_bytes;
1212 }
1213
1174 if (result) { 1214 if (result) {
1175 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr); 1215 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1176 if (sense_valid) 1216 if (sense_valid)
@@ -2121,7 +2161,7 @@ static int sd_revalidate_disk(struct gendisk *disk)
2121 else 2161 else
2122 ordered = QUEUE_ORDERED_DRAIN; 2162 ordered = QUEUE_ORDERED_DRAIN;
2123 2163
2124 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush); 2164 blk_queue_ordered(sdkp->disk->queue, ordered);
2125 2165
2126 set_capacity(disk, sdkp->capacity); 2166 set_capacity(disk, sdkp->capacity);
2127 kfree(buffer); 2167 kfree(buffer);
@@ -2234,6 +2274,7 @@ static void sd_probe_async(void *data, async_cookie_t cookie)
2234 sd_revalidate_disk(gd); 2274 sd_revalidate_disk(gd);
2235 2275
2236 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn); 2276 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
2277 blk_queue_unprep_rq(sdp->request_queue, sd_unprep_fn);
2237 2278
2238 gd->driverfs_dev = &sdp->sdev_gendev; 2279 gd->driverfs_dev = &sdp->sdev_gendev;
2239 gd->flags = GENHD_FL_EXT_DEVT; 2280 gd->flags = GENHD_FL_EXT_DEVT;
@@ -2313,7 +2354,7 @@ static int sd_probe(struct device *dev)
2313 sdkp->driver = &sd_template; 2354 sdkp->driver = &sd_template;
2314 sdkp->disk = gd; 2355 sdkp->disk = gd;
2315 sdkp->index = index; 2356 sdkp->index = index;
2316 sdkp->openers = 0; 2357 atomic_set(&sdkp->openers, 0);
2317 sdkp->previous_state = 1; 2358 sdkp->previous_state = 1;
2318 2359
2319 if (!sdp->request_queue->rq_timeout) { 2360 if (!sdp->request_queue->rq_timeout) {
@@ -2372,6 +2413,7 @@ static int sd_remove(struct device *dev)
2372 2413
2373 async_synchronize_full(); 2414 async_synchronize_full();
2374 blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn); 2415 blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
2416 blk_queue_unprep_rq(sdkp->device->request_queue, NULL);
2375 device_del(&sdkp->dev); 2417 device_del(&sdkp->dev);
2376 del_gendisk(sdkp->disk); 2418 del_gendisk(sdkp->disk);
2377 sd_shutdown(dev); 2419 sd_shutdown(dev);
diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h
index 43d3caf268e..f81a9309e6d 100644
--- a/drivers/scsi/sd.h
+++ b/drivers/scsi/sd.h
@@ -47,7 +47,7 @@ struct scsi_disk {
47 struct scsi_device *device; 47 struct scsi_device *device;
48 struct device dev; 48 struct device dev;
49 struct gendisk *disk; 49 struct gendisk *disk;
50 unsigned int openers; /* protected by BKL for now, yuck */ 50 atomic_t openers;
51 sector_t capacity; /* size in 512-byte sectors */ 51 sector_t capacity; /* size in 512-byte sectors */
52 u32 index; 52 u32 index;
53 unsigned short hw_sector_size; 53 unsigned short hw_sector_size;
diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c
index 0a90abc7f14..ba9c3e0387c 100644
--- a/drivers/scsi/sr.c
+++ b/drivers/scsi/sr.c
@@ -44,6 +44,7 @@
44#include <linux/init.h> 44#include <linux/init.h>
45#include <linux/blkdev.h> 45#include <linux/blkdev.h>
46#include <linux/mutex.h> 46#include <linux/mutex.h>
47#include <linux/smp_lock.h>
47#include <linux/slab.h> 48#include <linux/slab.h>
48#include <asm/uaccess.h> 49#include <asm/uaccess.h>
49 50
@@ -466,22 +467,27 @@ static int sr_prep_fn(struct request_queue *q, struct request *rq)
466 467
467static int sr_block_open(struct block_device *bdev, fmode_t mode) 468static int sr_block_open(struct block_device *bdev, fmode_t mode)
468{ 469{
469 struct scsi_cd *cd = scsi_cd_get(bdev->bd_disk); 470 struct scsi_cd *cd;
470 int ret = -ENXIO; 471 int ret = -ENXIO;
471 472
473 lock_kernel();
474 cd = scsi_cd_get(bdev->bd_disk);
472 if (cd) { 475 if (cd) {
473 ret = cdrom_open(&cd->cdi, bdev, mode); 476 ret = cdrom_open(&cd->cdi, bdev, mode);
474 if (ret) 477 if (ret)
475 scsi_cd_put(cd); 478 scsi_cd_put(cd);
476 } 479 }
480 unlock_kernel();
477 return ret; 481 return ret;
478} 482}
479 483
480static int sr_block_release(struct gendisk *disk, fmode_t mode) 484static int sr_block_release(struct gendisk *disk, fmode_t mode)
481{ 485{
482 struct scsi_cd *cd = scsi_cd(disk); 486 struct scsi_cd *cd = scsi_cd(disk);
487 lock_kernel();
483 cdrom_release(&cd->cdi, mode); 488 cdrom_release(&cd->cdi, mode);
484 scsi_cd_put(cd); 489 scsi_cd_put(cd);
490 unlock_kernel();
485 return 0; 491 return 0;
486} 492}
487 493
@@ -493,6 +499,8 @@ static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
493 void __user *argp = (void __user *)arg; 499 void __user *argp = (void __user *)arg;
494 int ret; 500 int ret;
495 501
502 lock_kernel();
503
496 /* 504 /*
497 * Send SCSI addressing ioctls directly to mid level, send other 505 * Send SCSI addressing ioctls directly to mid level, send other
498 * ioctls to cdrom/block level. 506 * ioctls to cdrom/block level.
@@ -500,12 +508,13 @@ static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
500 switch (cmd) { 508 switch (cmd) {
501 case SCSI_IOCTL_GET_IDLUN: 509 case SCSI_IOCTL_GET_IDLUN:
502 case SCSI_IOCTL_GET_BUS_NUMBER: 510 case SCSI_IOCTL_GET_BUS_NUMBER:
503 return scsi_ioctl(sdev, cmd, argp); 511 ret = scsi_ioctl(sdev, cmd, argp);
512 goto out;
504 } 513 }
505 514
506 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg); 515 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
507 if (ret != -ENOSYS) 516 if (ret != -ENOSYS)
508 return ret; 517 goto out;
509 518
510 /* 519 /*
511 * ENODEV means that we didn't recognise the ioctl, or that we 520 * ENODEV means that we didn't recognise the ioctl, or that we
@@ -516,8 +525,12 @@ static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
516 ret = scsi_nonblockable_ioctl(sdev, cmd, argp, 525 ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
517 (mode & FMODE_NDELAY) != 0); 526 (mode & FMODE_NDELAY) != 0);
518 if (ret != -ENODEV) 527 if (ret != -ENODEV)
519 return ret; 528 goto out;
520 return scsi_ioctl(sdev, cmd, argp); 529 ret = scsi_ioctl(sdev, cmd, argp);
530
531out:
532 unlock_kernel();
533 return ret;
521} 534}
522 535
523static int sr_block_media_changed(struct gendisk *disk) 536static int sr_block_media_changed(struct gendisk *disk)
@@ -531,7 +544,7 @@ static const struct block_device_operations sr_bdops =
531 .owner = THIS_MODULE, 544 .owner = THIS_MODULE,
532 .open = sr_block_open, 545 .open = sr_block_open,
533 .release = sr_block_release, 546 .release = sr_block_release,
534 .locked_ioctl = sr_block_ioctl, 547 .ioctl = sr_block_ioctl,
535 .media_changed = sr_block_media_changed, 548 .media_changed = sr_block_media_changed,
536 /* 549 /*
537 * No compat_ioctl for now because sr_block_ioctl never 550 * No compat_ioctl for now because sr_block_ioctl never
diff --git a/drivers/scsi/sun3_NCR5380.c b/drivers/scsi/sun3_NCR5380.c
index b5838d547c6..713620ed70d 100644
--- a/drivers/scsi/sun3_NCR5380.c
+++ b/drivers/scsi/sun3_NCR5380.c
@@ -2022,7 +2022,7 @@ static void NCR5380_information_transfer (struct Scsi_Host *instance)
2022 if((count > SUN3_DMA_MINSIZE) && (sun3_dma_setup_done 2022 if((count > SUN3_DMA_MINSIZE) && (sun3_dma_setup_done
2023 != cmd)) 2023 != cmd))
2024 { 2024 {
2025 if(blk_fs_request(cmd->request)) { 2025 if (cmd->request->cmd_type == REQ_TYPE_FS) {
2026 sun3scsi_dma_setup(d, count, 2026 sun3scsi_dma_setup(d, count,
2027 rq_data_dir(cmd->request)); 2027 rq_data_dir(cmd->request));
2028 sun3_dma_setup_done = cmd; 2028 sun3_dma_setup_done = cmd;
diff --git a/drivers/scsi/sun3_scsi.c b/drivers/scsi/sun3_scsi.c
index e606cf0a2eb..613f5880d13 100644
--- a/drivers/scsi/sun3_scsi.c
+++ b/drivers/scsi/sun3_scsi.c
@@ -524,7 +524,7 @@ static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
524 struct scsi_cmnd *cmd, 524 struct scsi_cmnd *cmd,
525 int write_flag) 525 int write_flag)
526{ 526{
527 if(blk_fs_request(cmd->request)) 527 if (cmd->request->cmd_type == REQ_TYPE_FS)
528 return wanted; 528 return wanted;
529 else 529 else
530 return 0; 530 return 0;
diff --git a/drivers/scsi/sun3_scsi_vme.c b/drivers/scsi/sun3_scsi_vme.c
index aaa4fd0dd1b..7c526b8e30a 100644
--- a/drivers/scsi/sun3_scsi_vme.c
+++ b/drivers/scsi/sun3_scsi_vme.c
@@ -458,7 +458,7 @@ static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
458 struct scsi_cmnd *cmd, 458 struct scsi_cmnd *cmd,
459 int write_flag) 459 int write_flag)
460{ 460{
461 if(blk_fs_request(cmd->request)) 461 if (cmd->request->cmd_type == REQ_TYPE_FS)
462 return wanted; 462 return wanted;
463 else 463 else
464 return 0; 464 return 0;
diff --git a/drivers/serial/21285.c b/drivers/serial/21285.c
index 8681f134505..d89aa38c5cf 100644
--- a/drivers/serial/21285.c
+++ b/drivers/serial/21285.c
@@ -216,7 +216,7 @@ serial21285_set_termios(struct uart_port *port, struct ktermios *termios,
216 struct ktermios *old) 216 struct ktermios *old)
217{ 217{
218 unsigned long flags; 218 unsigned long flags;
219 unsigned int baud, quot, h_lcr; 219 unsigned int baud, quot, h_lcr, b;
220 220
221 /* 221 /*
222 * We don't support modem control lines. 222 * We don't support modem control lines.
@@ -234,12 +234,8 @@ serial21285_set_termios(struct uart_port *port, struct ktermios *termios,
234 */ 234 */
235 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); 235 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
236 quot = uart_get_divisor(port, baud); 236 quot = uart_get_divisor(port, baud);
237 237 b = port->uartclk / (16 * quot);
238 if (port->state && port->state->port.tty) { 238 tty_termios_encode_baud_rate(termios, b, b);
239 struct tty_struct *tty = port->state->port.tty;
240 unsigned int b = port->uartclk / (16 * quot);
241 tty_encode_baud_rate(tty, b, b);
242 }
243 239
244 switch (termios->c_cflag & CSIZE) { 240 switch (termios->c_cflag & CSIZE) {
245 case CS5: 241 case CS5:
diff --git a/drivers/serial/68328serial.c b/drivers/serial/68328serial.c
index 30463862603..7356a56ac45 100644
--- a/drivers/serial/68328serial.c
+++ b/drivers/serial/68328serial.c
@@ -78,10 +78,6 @@ struct m68k_serial *m68k_consinfo = 0;
78 78
79#define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */ 79#define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
80 80
81#ifdef CONFIG_CONSOLE
82extern wait_queue_head_t keypress_wait;
83#endif
84
85struct tty_driver *serial_driver; 81struct tty_driver *serial_driver;
86 82
87/* number of characters left in xmit buffer before we ask for more */ 83/* number of characters left in xmit buffer before we ask for more */
@@ -102,19 +98,13 @@ static void change_speed(struct m68k_serial *info);
102 * Setup for console. Argument comes from the boot command line. 98 * Setup for console. Argument comes from the boot command line.
103 */ 99 */
104 100
105#if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ) 101/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
106#define CONSOLE_BAUD_RATE 115200 102#ifdef CONFIG_M68VZ328
107#define DEFAULT_CBAUD B115200 103#define CONSOLE_BAUD_RATE 19200
108#else 104#define DEFAULT_CBAUD B19200
109 /* (es) */
110 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
111 #ifdef CONFIG_M68VZ328
112 #define CONSOLE_BAUD_RATE 19200
113 #define DEFAULT_CBAUD B19200
114 #endif
115 /* (/es) */
116#endif 105#endif
117 106
107
118#ifndef CONSOLE_BAUD_RATE 108#ifndef CONSOLE_BAUD_RATE
119#define CONSOLE_BAUD_RATE 9600 109#define CONSOLE_BAUD_RATE 9600
120#define DEFAULT_CBAUD B9600 110#define DEFAULT_CBAUD B9600
@@ -300,10 +290,6 @@ static void receive_chars(struct m68k_serial *info, unsigned short rx)
300 return; 290 return;
301#endif /* CONFIG_MAGIC_SYSRQ */ 291#endif /* CONFIG_MAGIC_SYSRQ */
302 } 292 }
303 /* It is a 'keyboard interrupt' ;-) */
304#ifdef CONFIG_CONSOLE
305 wake_up(&keypress_wait);
306#endif
307 } 293 }
308 294
309 if(!tty) 295 if(!tty)
@@ -1243,7 +1229,9 @@ static int block_til_ready(struct tty_struct *tty, struct file * filp,
1243 retval = -ERESTARTSYS; 1229 retval = -ERESTARTSYS;
1244 break; 1230 break;
1245 } 1231 }
1232 tty_unlock();
1246 schedule(); 1233 schedule();
1234 tty_lock();
1247 } 1235 }
1248 current->state = TASK_RUNNING; 1236 current->state = TASK_RUNNING;
1249 remove_wait_queue(&info->open_wait, &wait); 1237 remove_wait_queue(&info->open_wait, &wait);
diff --git a/drivers/serial/68360serial.c b/drivers/serial/68360serial.c
index 768612f8e41..0dff3bbddc8 100644
--- a/drivers/serial/68360serial.c
+++ b/drivers/serial/68360serial.c
@@ -1705,7 +1705,6 @@ static void rs_360_wait_until_sent(struct tty_struct *tty, int timeout)
1705 printk("jiff=%lu...", jiffies); 1705 printk("jiff=%lu...", jiffies);
1706#endif 1706#endif
1707 1707
1708 lock_kernel();
1709 /* We go through the loop at least once because we can't tell 1708 /* We go through the loop at least once because we can't tell
1710 * exactly when the last character exits the shifter. There can 1709 * exactly when the last character exits the shifter. There can
1711 * be at least two characters waiting to be sent after the buffers 1710 * be at least two characters waiting to be sent after the buffers
@@ -1734,7 +1733,6 @@ static void rs_360_wait_until_sent(struct tty_struct *tty, int timeout)
1734 bdp--; 1733 bdp--;
1735 } while (bdp->status & BD_SC_READY); 1734 } while (bdp->status & BD_SC_READY);
1736 current->state = TASK_RUNNING; 1735 current->state = TASK_RUNNING;
1737 unlock_kernel();
1738#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT 1736#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1739 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies); 1737 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1740#endif 1738#endif
@@ -1862,7 +1860,9 @@ static int block_til_ready(struct tty_struct *tty, struct file * filp,
1862 printk("block_til_ready blocking: ttys%d, count = %d\n", 1860 printk("block_til_ready blocking: ttys%d, count = %d\n",
1863 info->line, state->count); 1861 info->line, state->count);
1864#endif 1862#endif
1863 tty_unlock();
1865 schedule(); 1864 schedule();
1865 tty_lock();
1866 } 1866 }
1867 current->state = TASK_RUNNING; 1867 current->state = TASK_RUNNING;
1868 remove_wait_queue(&info->open_wait, &wait); 1868 remove_wait_queue(&info->open_wait, &wait);
diff --git a/drivers/serial/8250.c b/drivers/serial/8250.c
index 09ef57034c9..24110f6f61e 100644
--- a/drivers/serial/8250.c
+++ b/drivers/serial/8250.c
@@ -241,7 +241,7 @@ static const struct serial8250_config uart_config[] = {
241 .fifo_size = 128, 241 .fifo_size = 128,
242 .tx_loadsz = 128, 242 .tx_loadsz = 128,
243 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 243 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
244 .flags = UART_CAP_FIFO, 244 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
245 }, 245 },
246 [PORT_16654] = { 246 [PORT_16654] = {
247 .name = "ST16654", 247 .name = "ST16654",
@@ -300,6 +300,13 @@ static const struct serial8250_config uart_config[] = {
300 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00, 300 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
301 .flags = UART_CAP_FIFO | UART_CAP_AFE, 301 .flags = UART_CAP_FIFO | UART_CAP_AFE,
302 }, 302 },
303 [PORT_U6_16550A] = {
304 .name = "U6_16550A",
305 .fifo_size = 64,
306 .tx_loadsz = 64,
307 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
308 .flags = UART_CAP_FIFO | UART_CAP_AFE,
309 },
303}; 310};
304 311
305#if defined(CONFIG_MIPS_ALCHEMY) 312#if defined(CONFIG_MIPS_ALCHEMY)
@@ -1070,6 +1077,15 @@ static void autoconfig_16550a(struct uart_8250_port *up)
1070 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 "); 1077 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1071 } 1078 }
1072 serial_outp(up, UART_IER, iersave); 1079 serial_outp(up, UART_IER, iersave);
1080
1081 /*
1082 * We distinguish between 16550A and U6 16550A by counting
1083 * how many bytes are in the FIFO.
1084 */
1085 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1086 up->port.type = PORT_U6_16550A;
1087 up->capabilities |= UART_CAP_AFE;
1088 }
1073} 1089}
1074 1090
1075/* 1091/*
@@ -2224,9 +2240,9 @@ static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int
2224 return quot; 2240 return quot;
2225} 2241}
2226 2242
2227static void 2243void
2228serial8250_set_termios(struct uart_port *port, struct ktermios *termios, 2244serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2229 struct ktermios *old) 2245 struct ktermios *old)
2230{ 2246{
2231 struct uart_8250_port *up = (struct uart_8250_port *)port; 2247 struct uart_8250_port *up = (struct uart_8250_port *)port;
2232 unsigned char cval, fcr = 0; 2248 unsigned char cval, fcr = 0;
@@ -2402,16 +2418,22 @@ serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2402 if (tty_termios_baud_rate(termios)) 2418 if (tty_termios_baud_rate(termios))
2403 tty_termios_encode_baud_rate(termios, baud, baud); 2419 tty_termios_encode_baud_rate(termios, baud, baud);
2404} 2420}
2421EXPORT_SYMBOL(serial8250_do_set_termios);
2405 2422
2406static void 2423static void
2407serial8250_set_ldisc(struct uart_port *port) 2424serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2425 struct ktermios *old)
2408{ 2426{
2409 int line = port->line; 2427 if (port->set_termios)
2410 2428 port->set_termios(port, termios, old);
2411 if (line >= port->state->port.tty->driver->num) 2429 else
2412 return; 2430 serial8250_do_set_termios(port, termios, old);
2431}
2413 2432
2414 if (port->state->port.tty->ldisc->ops->num == N_PPS) { 2433static void
2434serial8250_set_ldisc(struct uart_port *port, int new)
2435{
2436 if (new == N_PPS) {
2415 port->flags |= UPF_HARDPPS_CD; 2437 port->flags |= UPF_HARDPPS_CD;
2416 serial8250_enable_ms(port); 2438 serial8250_enable_ms(port);
2417 } else 2439 } else
@@ -2987,6 +3009,7 @@ static int __devinit serial8250_probe(struct platform_device *dev)
2987 port.type = p->type; 3009 port.type = p->type;
2988 port.serial_in = p->serial_in; 3010 port.serial_in = p->serial_in;
2989 port.serial_out = p->serial_out; 3011 port.serial_out = p->serial_out;
3012 port.set_termios = p->set_termios;
2990 port.dev = &dev->dev; 3013 port.dev = &dev->dev;
2991 port.irqflags |= irqflag; 3014 port.irqflags |= irqflag;
2992 ret = serial8250_register_port(&port); 3015 ret = serial8250_register_port(&port);
@@ -3150,6 +3173,9 @@ int serial8250_register_port(struct uart_port *port)
3150 uart->port.serial_in = port->serial_in; 3173 uart->port.serial_in = port->serial_in;
3151 if (port->serial_out) 3174 if (port->serial_out)
3152 uart->port.serial_out = port->serial_out; 3175 uart->port.serial_out = port->serial_out;
3176 /* Possibly override set_termios call */
3177 if (port->set_termios)
3178 uart->port.set_termios = port->set_termios;
3153 3179
3154 ret = uart_add_one_port(&serial8250_reg, &uart->port); 3180 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3155 if (ret == 0) 3181 if (ret == 0)
diff --git a/drivers/serial/8250_early.c b/drivers/serial/8250_early.c
index f279745e9fe..b745792ec25 100644
--- a/drivers/serial/8250_early.c
+++ b/drivers/serial/8250_early.c
@@ -19,9 +19,11 @@
19 * The user can specify the device directly, e.g., 19 * The user can specify the device directly, e.g.,
20 * earlycon=uart8250,io,0x3f8,9600n8 20 * earlycon=uart8250,io,0x3f8,9600n8
21 * earlycon=uart8250,mmio,0xff5e0000,115200n8 21 * earlycon=uart8250,mmio,0xff5e0000,115200n8
22 * earlycon=uart8250,mmio32,0xff5e0000,115200n8
22 * or 23 * or
23 * console=uart8250,io,0x3f8,9600n8 24 * console=uart8250,io,0x3f8,9600n8
24 * console=uart8250,mmio,0xff5e0000,115200n8 25 * console=uart8250,mmio,0xff5e0000,115200n8
26 * console=uart8250,mmio32,0xff5e0000,115200n8
25 */ 27 */
26 28
27#include <linux/tty.h> 29#include <linux/tty.h>
@@ -48,18 +50,31 @@ static struct early_serial8250_device early_device;
48 50
49static unsigned int __init serial_in(struct uart_port *port, int offset) 51static unsigned int __init serial_in(struct uart_port *port, int offset)
50{ 52{
51 if (port->iotype == UPIO_MEM) 53 switch (port->iotype) {
54 case UPIO_MEM:
52 return readb(port->membase + offset); 55 return readb(port->membase + offset);
53 else 56 case UPIO_MEM32:
57 return readl(port->membase + (offset << 2));
58 case UPIO_PORT:
54 return inb(port->iobase + offset); 59 return inb(port->iobase + offset);
60 default:
61 return 0;
62 }
55} 63}
56 64
57static void __init serial_out(struct uart_port *port, int offset, int value) 65static void __init serial_out(struct uart_port *port, int offset, int value)
58{ 66{
59 if (port->iotype == UPIO_MEM) 67 switch (port->iotype) {
68 case UPIO_MEM:
60 writeb(value, port->membase + offset); 69 writeb(value, port->membase + offset);
61 else 70 break;
71 case UPIO_MEM32:
72 writel(value, port->membase + (offset << 2));
73 break;
74 case UPIO_PORT:
62 outb(value, port->iobase + offset); 75 outb(value, port->iobase + offset);
76 break;
77 }
63} 78}
64 79
65#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE) 80#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
@@ -137,15 +152,21 @@ static int __init parse_options(struct early_serial8250_device *device,
137 char *options) 152 char *options)
138{ 153{
139 struct uart_port *port = &device->port; 154 struct uart_port *port = &device->port;
140 int mmio, length; 155 int mmio, mmio32, length;
141 156
142 if (!options) 157 if (!options)
143 return -ENODEV; 158 return -ENODEV;
144 159
145 port->uartclk = BASE_BAUD * 16; 160 port->uartclk = BASE_BAUD * 16;
146 if (!strncmp(options, "mmio,", 5)) { 161
147 port->iotype = UPIO_MEM; 162 mmio = !strncmp(options, "mmio,", 5);
148 port->mapbase = simple_strtoul(options + 5, &options, 0); 163 mmio32 = !strncmp(options, "mmio32,", 7);
164 if (mmio || mmio32) {
165 port->iotype = (mmio ? UPIO_MEM : UPIO_MEM32);
166 port->mapbase = simple_strtoul(options + (mmio ? 5 : 7),
167 &options, 0);
168 if (mmio32)
169 port->regshift = 2;
149#ifdef CONFIG_FIX_EARLYCON_MEM 170#ifdef CONFIG_FIX_EARLYCON_MEM
150 set_fixmap_nocache(FIX_EARLYCON_MEM_BASE, 171 set_fixmap_nocache(FIX_EARLYCON_MEM_BASE,
151 port->mapbase & PAGE_MASK); 172 port->mapbase & PAGE_MASK);
@@ -157,11 +178,10 @@ static int __init parse_options(struct early_serial8250_device *device,
157 if (!port->membase) { 178 if (!port->membase) {
158 printk(KERN_ERR "%s: Couldn't ioremap 0x%llx\n", 179 printk(KERN_ERR "%s: Couldn't ioremap 0x%llx\n",
159 __func__, 180 __func__,
160 (unsigned long long)port->mapbase); 181 (unsigned long long) port->mapbase);
161 return -ENOMEM; 182 return -ENOMEM;
162 } 183 }
163#endif 184#endif
164 mmio = 1;
165 } else if (!strncmp(options, "io,", 3)) { 185 } else if (!strncmp(options, "io,", 3)) {
166 port->iotype = UPIO_PORT; 186 port->iotype = UPIO_PORT;
167 port->iobase = simple_strtoul(options + 3, &options, 0); 187 port->iobase = simple_strtoul(options + 3, &options, 0);
@@ -181,11 +201,18 @@ static int __init parse_options(struct early_serial8250_device *device,
181 device->baud); 201 device->baud);
182 } 202 }
183 203
184 printk(KERN_INFO "Early serial console at %s 0x%llx (options '%s')\n", 204 if (mmio || mmio32)
185 mmio ? "MMIO" : "I/O port", 205 printk(KERN_INFO
186 mmio ? (unsigned long long) port->mapbase 206 "Early serial console at MMIO%s 0x%llu (options '%s')\n",
187 : (unsigned long long) port->iobase, 207 mmio32 ? "32" : "",
188 device->options); 208 (unsigned long long)port->mapbase,
209 device->options);
210 else
211 printk(KERN_INFO
212 "Early serial console at I/O port 0x%lu (options '%s')\n",
213 port->iobase,
214 device->options);
215
189 return 0; 216 return 0;
190} 217}
191 218
diff --git a/drivers/serial/8250_pci.c b/drivers/serial/8250_pci.c
index 746a44621d9..53be4d35a0a 100644
--- a/drivers/serial/8250_pci.c
+++ b/drivers/serial/8250_pci.c
@@ -994,6 +994,7 @@ static int skip_tx_en_setup(struct serial_private *priv,
994#define PCI_DEVICE_ID_TITAN_800E 0xA014 994#define PCI_DEVICE_ID_TITAN_800E 0xA014
995#define PCI_DEVICE_ID_TITAN_200EI 0xA016 995#define PCI_DEVICE_ID_TITAN_200EI 0xA016
996#define PCI_DEVICE_ID_TITAN_200EISI 0xA017 996#define PCI_DEVICE_ID_TITAN_200EISI 0xA017
997#define PCI_DEVICE_ID_OXSEMI_16PCI958 0x9538
997 998
998/* Unknown vendors/cards - this should not be in linux/pci_ids.h */ 999/* Unknown vendors/cards - this should not be in linux/pci_ids.h */
999#define PCI_SUBDEVICE_ID_UNKNOWN_0x1584 0x1584 1000#define PCI_SUBDEVICE_ID_UNKNOWN_0x1584 0x1584
@@ -1542,6 +1543,8 @@ enum pci_board_num_t {
1542 pbn_b2_4_921600, 1543 pbn_b2_4_921600,
1543 pbn_b2_8_921600, 1544 pbn_b2_8_921600,
1544 1545
1546 pbn_b2_8_1152000,
1547
1545 pbn_b2_bt_1_115200, 1548 pbn_b2_bt_1_115200,
1546 pbn_b2_bt_2_115200, 1549 pbn_b2_bt_2_115200,
1547 pbn_b2_bt_4_115200, 1550 pbn_b2_bt_4_115200,
@@ -1960,6 +1963,13 @@ static struct pciserial_board pci_boards[] __devinitdata = {
1960 .uart_offset = 8, 1963 .uart_offset = 8,
1961 }, 1964 },
1962 1965
1966 [pbn_b2_8_1152000] = {
1967 .flags = FL_BASE2,
1968 .num_ports = 8,
1969 .base_baud = 1152000,
1970 .uart_offset = 8,
1971 },
1972
1963 [pbn_b2_bt_1_115200] = { 1973 [pbn_b2_bt_1_115200] = {
1964 .flags = FL_BASE2|FL_BASE_BARS, 1974 .flags = FL_BASE2|FL_BASE_BARS,
1965 .num_ports = 1, 1975 .num_ports = 1,
@@ -2875,6 +2885,9 @@ static struct pci_device_id serial_pci_tbl[] = {
2875 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI952, 2885 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI952,
2876 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2886 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2877 pbn_b0_bt_2_921600 }, 2887 pbn_b0_bt_2_921600 },
2888 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI958,
2889 PCI_ANY_ID , PCI_ANY_ID, 0, 0,
2890 pbn_b2_8_1152000 },
2878 2891
2879 /* 2892 /*
2880 * Oxford Semiconductor Inc. Tornado PCI express device range. 2893 * Oxford Semiconductor Inc. Tornado PCI express device range.
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index e437ce8c174..a22e60c06f4 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -542,6 +542,7 @@ config SERIAL_S5PV210
542 help 542 help
543 Serial port support for Samsung's S5P Family of SoC's 543 Serial port support for Samsung's S5P Family of SoC's
544 544
545
545config SERIAL_MAX3100 546config SERIAL_MAX3100
546 tristate "MAX3100 support" 547 tristate "MAX3100 support"
547 depends on SPI 548 depends on SPI
@@ -549,6 +550,22 @@ config SERIAL_MAX3100
549 help 550 help
550 MAX3100 chip support 551 MAX3100 chip support
551 552
553config SERIAL_MAX3107
554 tristate "MAX3107 support"
555 depends on SPI
556 select SERIAL_CORE
557 help
558 MAX3107 chip support
559
560config SERIAL_MAX3107_AAVA
561 tristate "MAX3107 AAVA platform support"
562 depends on X86_MRST && SERIAL_MAX3107 && GPIOLIB
563 select SERIAL_CORE
564 help
565 Support for the MAX3107 chip configuration found on the AAVA
566 platform. Includes the extra initialisation and GPIO support
567 neded for this device.
568
552config SERIAL_DZ 569config SERIAL_DZ
553 bool "DECstation DZ serial driver" 570 bool "DECstation DZ serial driver"
554 depends on MACH_DECSTATION && 32BIT 571 depends on MACH_DECSTATION && 32BIT
@@ -690,6 +707,33 @@ config SERIAL_SA1100_CONSOLE
690 your boot loader (lilo or loadlin) about how to pass options to the 707 your boot loader (lilo or loadlin) about how to pass options to the
691 kernel at boot time.) 708 kernel at boot time.)
692 709
710config SERIAL_MRST_MAX3110
711 tristate "SPI UART driver for Max3110"
712 depends on SPI_DW_PCI
713 select SERIAL_CORE
714 select SERIAL_CORE_CONSOLE
715 help
716 This is the UART protocol driver for the MAX3110 device on
717 the Intel Moorestown platform. On other systems use the max3100
718 driver.
719
720config MRST_MAX3110_IRQ
721 boolean "Enable GPIO IRQ for Max3110 over Moorestown"
722 default n
723 depends on SERIAL_MRST_MAX3110 && GPIO_LANGWELL
724 help
725 This has to be enabled after Moorestown GPIO driver is loaded
726
727config SERIAL_MFD_HSU
728 tristate "Medfield High Speed UART support"
729 depends on PCI
730 select SERIAL_CORE
731
732config SERIAL_MFD_HSU_CONSOLE
733 boolean "Medfile HSU serial console support"
734 depends on SERIAL_MFD_HSU=y
735 select SERIAL_CORE_CONSOLE
736
693config SERIAL_BFIN 737config SERIAL_BFIN
694 tristate "Blackfin serial port support" 738 tristate "Blackfin serial port support"
695 depends on BLACKFIN 739 depends on BLACKFIN
diff --git a/drivers/serial/Makefile b/drivers/serial/Makefile
index 208a85572c3..1ca4fd599ff 100644
--- a/drivers/serial/Makefile
+++ b/drivers/serial/Makefile
@@ -46,6 +46,8 @@ obj-$(CONFIG_SERIAL_S3C24A0) += s3c24a0.o
46obj-$(CONFIG_SERIAL_S3C6400) += s3c6400.o 46obj-$(CONFIG_SERIAL_S3C6400) += s3c6400.o
47obj-$(CONFIG_SERIAL_S5PV210) += s5pv210.o 47obj-$(CONFIG_SERIAL_S5PV210) += s5pv210.o
48obj-$(CONFIG_SERIAL_MAX3100) += max3100.o 48obj-$(CONFIG_SERIAL_MAX3100) += max3100.o
49obj-$(CONFIG_SERIAL_MAX3107) += max3107.o
50obj-$(CONFIG_SERIAL_MAX3107_AAVA) += max3107-aava.o
49obj-$(CONFIG_SERIAL_IP22_ZILOG) += ip22zilog.o 51obj-$(CONFIG_SERIAL_IP22_ZILOG) += ip22zilog.o
50obj-$(CONFIG_SERIAL_MUX) += mux.o 52obj-$(CONFIG_SERIAL_MUX) += mux.o
51obj-$(CONFIG_SERIAL_68328) += 68328serial.o 53obj-$(CONFIG_SERIAL_68328) += 68328serial.o
@@ -84,3 +86,5 @@ obj-$(CONFIG_SERIAL_TIMBERDALE) += timbuart.o
84obj-$(CONFIG_SERIAL_GRLIB_GAISLER_APBUART) += apbuart.o 86obj-$(CONFIG_SERIAL_GRLIB_GAISLER_APBUART) += apbuart.o
85obj-$(CONFIG_SERIAL_ALTERA_JTAGUART) += altera_jtaguart.o 87obj-$(CONFIG_SERIAL_ALTERA_JTAGUART) += altera_jtaguart.o
86obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o 88obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o
89obj-$(CONFIG_SERIAL_MRST_MAX3110) += mrst_max3110.o
90obj-$(CONFIG_SERIAL_MFD_HSU) += mfd.o
diff --git a/drivers/serial/altera_uart.c b/drivers/serial/altera_uart.c
index 0f1189605d2..f8d8a00554d 100644
--- a/drivers/serial/altera_uart.c
+++ b/drivers/serial/altera_uart.c
@@ -394,7 +394,7 @@ int __init early_altera_uart_setup(struct altera_uart_platform_uart *platp)
394static void altera_uart_console_putc(struct uart_port *port, const char c) 394static void altera_uart_console_putc(struct uart_port *port, const char c)
395{ 395{
396 while (!(readl(port->membase + ALTERA_UART_STATUS_REG) & 396 while (!(readl(port->membase + ALTERA_UART_STATUS_REG) &
397 ALTERA_UART_STATUS_TRDY_MSK)) 397 ALTERA_UART_STATUS_TRDY_MSK))
398 cpu_relax(); 398 cpu_relax();
399 399
400 writel(c, port->membase + ALTERA_UART_TXDATA_REG); 400 writel(c, port->membase + ALTERA_UART_TXDATA_REG);
diff --git a/drivers/serial/atmel_serial.c b/drivers/serial/atmel_serial.c
index a182def7007..3892666b5fb 100644
--- a/drivers/serial/atmel_serial.c
+++ b/drivers/serial/atmel_serial.c
@@ -217,7 +217,8 @@ void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
217 if (rs485conf->flags & SER_RS485_ENABLED) { 217 if (rs485conf->flags & SER_RS485_ENABLED) {
218 dev_dbg(port->dev, "Setting UART to RS485\n"); 218 dev_dbg(port->dev, "Setting UART to RS485\n");
219 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 219 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
220 UART_PUT_TTGR(port, rs485conf->delay_rts_before_send); 220 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
221 UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
221 mode |= ATMEL_US_USMODE_RS485; 222 mode |= ATMEL_US_USMODE_RS485;
222 } else { 223 } else {
223 dev_dbg(port->dev, "Setting UART to RS232\n"); 224 dev_dbg(port->dev, "Setting UART to RS232\n");
@@ -292,7 +293,9 @@ static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
292 293
293 if (atmel_port->rs485.flags & SER_RS485_ENABLED) { 294 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
294 dev_dbg(port->dev, "Setting UART to RS485\n"); 295 dev_dbg(port->dev, "Setting UART to RS485\n");
295 UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send); 296 if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
297 UART_PUT_TTGR(port,
298 atmel_port->rs485.delay_rts_after_send);
296 mode |= ATMEL_US_USMODE_RS485; 299 mode |= ATMEL_US_USMODE_RS485;
297 } else { 300 } else {
298 dev_dbg(port->dev, "Setting UART to RS232\n"); 301 dev_dbg(port->dev, "Setting UART to RS232\n");
@@ -1211,7 +1214,9 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1211 1214
1212 if (atmel_port->rs485.flags & SER_RS485_ENABLED) { 1215 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1213 dev_dbg(port->dev, "Setting UART to RS485\n"); 1216 dev_dbg(port->dev, "Setting UART to RS485\n");
1214 UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send); 1217 if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1218 UART_PUT_TTGR(port,
1219 atmel_port->rs485.delay_rts_after_send);
1215 mode |= ATMEL_US_USMODE_RS485; 1220 mode |= ATMEL_US_USMODE_RS485;
1216 } else { 1221 } else {
1217 dev_dbg(port->dev, "Setting UART to RS232\n"); 1222 dev_dbg(port->dev, "Setting UART to RS232\n");
diff --git a/drivers/serial/bfin_5xx.c b/drivers/serial/bfin_5xx.c
index 511cbf68787..a9eff2b18ea 100644
--- a/drivers/serial/bfin_5xx.c
+++ b/drivers/serial/bfin_5xx.c
@@ -957,15 +957,12 @@ bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
957 * Enable the IrDA function if tty->ldisc.num is N_IRDA. 957 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
958 * In other cases, disable IrDA function. 958 * In other cases, disable IrDA function.
959 */ 959 */
960static void bfin_serial_set_ldisc(struct uart_port *port) 960static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
961{ 961{
962 int line = port->line; 962 int line = port->line;
963 unsigned short val; 963 unsigned short val;
964 964
965 if (line >= port->state->port.tty->driver->num) 965 switch (ld) {
966 return;
967
968 switch (port->state->port.tty->termios->c_line) {
969 case N_IRDA: 966 case N_IRDA:
970 val = UART_GET_GCTL(&bfin_serial_ports[line]); 967 val = UART_GET_GCTL(&bfin_serial_ports[line]);
971 val |= (IREN | RPOLC); 968 val |= (IREN | RPOLC);
diff --git a/drivers/serial/crisv10.c b/drivers/serial/crisv10.c
index 30626440a06..c856905bb3b 100644
--- a/drivers/serial/crisv10.c
+++ b/drivers/serial/crisv10.c
@@ -3935,7 +3935,6 @@ static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
3935 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO 3935 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
3936 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k) 3936 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
3937 */ 3937 */
3938 lock_kernel();
3939 orig_jiffies = jiffies; 3938 orig_jiffies = jiffies;
3940 while (info->xmit.head != info->xmit.tail || /* More in send queue */ 3939 while (info->xmit.head != info->xmit.tail || /* More in send queue */
3941 (*info->ostatusadr & 0x007f) || /* more in FIFO */ 3940 (*info->ostatusadr & 0x007f) || /* more in FIFO */
@@ -3952,7 +3951,6 @@ static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
3952 curr_time_usec - info->last_tx_active_usec; 3951 curr_time_usec - info->last_tx_active_usec;
3953 } 3952 }
3954 set_current_state(TASK_RUNNING); 3953 set_current_state(TASK_RUNNING);
3955 unlock_kernel();
3956} 3954}
3957 3955
3958/* 3956/*
@@ -3992,7 +3990,7 @@ block_til_ready(struct tty_struct *tty, struct file * filp,
3992 */ 3990 */
3993 if (tty_hung_up_p(filp) || 3991 if (tty_hung_up_p(filp) ||
3994 (info->flags & ASYNC_CLOSING)) { 3992 (info->flags & ASYNC_CLOSING)) {
3995 wait_event_interruptible(info->close_wait, 3993 wait_event_interruptible_tty(info->close_wait,
3996 !(info->flags & ASYNC_CLOSING)); 3994 !(info->flags & ASYNC_CLOSING));
3997#ifdef SERIAL_DO_RESTART 3995#ifdef SERIAL_DO_RESTART
3998 if (info->flags & ASYNC_HUP_NOTIFY) 3996 if (info->flags & ASYNC_HUP_NOTIFY)
@@ -4068,7 +4066,9 @@ block_til_ready(struct tty_struct *tty, struct file * filp,
4068 printk("block_til_ready blocking: ttyS%d, count = %d\n", 4066 printk("block_til_ready blocking: ttyS%d, count = %d\n",
4069 info->line, info->count); 4067 info->line, info->count);
4070#endif 4068#endif
4069 tty_unlock();
4071 schedule(); 4070 schedule();
4071 tty_lock();
4072 } 4072 }
4073 set_current_state(TASK_RUNNING); 4073 set_current_state(TASK_RUNNING);
4074 remove_wait_queue(&info->open_wait, &wait); 4074 remove_wait_queue(&info->open_wait, &wait);
@@ -4150,7 +4150,7 @@ rs_open(struct tty_struct *tty, struct file * filp)
4150 */ 4150 */
4151 if (tty_hung_up_p(filp) || 4151 if (tty_hung_up_p(filp) ||
4152 (info->flags & ASYNC_CLOSING)) { 4152 (info->flags & ASYNC_CLOSING)) {
4153 wait_event_interruptible(info->close_wait, 4153 wait_event_interruptible_tty(info->close_wait,
4154 !(info->flags & ASYNC_CLOSING)); 4154 !(info->flags & ASYNC_CLOSING));
4155#ifdef SERIAL_DO_RESTART 4155#ifdef SERIAL_DO_RESTART
4156 return ((info->flags & ASYNC_HUP_NOTIFY) ? 4156 return ((info->flags & ASYNC_HUP_NOTIFY) ?
@@ -4533,8 +4533,8 @@ static int __init rs_init(void)
4533 INIT_WORK(&info->work, do_softint); 4533 INIT_WORK(&info->work, do_softint);
4534 4534
4535 if (info->enabled) { 4535 if (info->enabled) {
4536 printk(KERN_INFO "%s%d at 0x%x is a builtin UART with DMA\n", 4536 printk(KERN_INFO "%s%d at %p is a builtin UART with DMA\n",
4537 serial_driver->name, info->line, (unsigned int)info->ioport); 4537 serial_driver->name, info->line, info->ioport);
4538 } 4538 }
4539 } 4539 }
4540#ifdef CONFIG_ETRAX_FAST_TIMER 4540#ifdef CONFIG_ETRAX_FAST_TIMER
diff --git a/drivers/serial/imx.c b/drivers/serial/imx.c
index eacb588a934..66ecc7ab6da 100644
--- a/drivers/serial/imx.c
+++ b/drivers/serial/imx.c
@@ -909,13 +909,11 @@ imx_set_termios(struct uart_port *port, struct ktermios *termios,
909 rational_best_approximation(16 * div * baud, sport->port.uartclk, 909 rational_best_approximation(16 * div * baud, sport->port.uartclk,
910 1 << 16, 1 << 16, &num, &denom); 910 1 << 16, 1 << 16, &num, &denom);
911 911
912 if (port->state && port->state->port.tty) { 912 tdiv64 = sport->port.uartclk;
913 tdiv64 = sport->port.uartclk; 913 tdiv64 *= num;
914 tdiv64 *= num; 914 do_div(tdiv64, denom * 16 * div);
915 do_div(tdiv64, denom * 16 * div); 915 tty_termios_encode_baud_rate(termios,
916 tty_encode_baud_rate(sport->port.state->port.tty,
917 (speed_t)tdiv64, (speed_t)tdiv64); 916 (speed_t)tdiv64, (speed_t)tdiv64);
918 }
919 917
920 num -= 1; 918 num -= 1;
921 denom -= 1; 919 denom -= 1;
diff --git a/drivers/serial/ioc3_serial.c b/drivers/serial/ioc3_serial.c
index f164ba4eba0..93de907b120 100644
--- a/drivers/serial/ioc3_serial.c
+++ b/drivers/serial/ioc3_serial.c
@@ -954,12 +954,13 @@ ioc3_change_speed(struct uart_port *the_port,
954 struct ktermios *new_termios, struct ktermios *old_termios) 954 struct ktermios *new_termios, struct ktermios *old_termios)
955{ 955{
956 struct ioc3_port *port = get_ioc3_port(the_port); 956 struct ioc3_port *port = get_ioc3_port(the_port);
957 unsigned int cflag; 957 unsigned int cflag, iflag;
958 int baud; 958 int baud;
959 int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8; 959 int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
960 struct uart_state *state = the_port->state; 960 struct uart_state *state = the_port->state;
961 961
962 cflag = new_termios->c_cflag; 962 cflag = new_termios->c_cflag;
963 iflag = new_termios->c_iflag;
963 964
964 switch (cflag & CSIZE) { 965 switch (cflag & CSIZE) {
965 case CS5: 966 case CS5:
@@ -1000,12 +1001,12 @@ ioc3_change_speed(struct uart_port *the_port,
1000 1001
1001 state->port.tty->low_latency = 1; 1002 state->port.tty->low_latency = 1;
1002 1003
1003 if (I_IGNPAR(state->port.tty)) 1004 if (iflag & IGNPAR)
1004 the_port->ignore_status_mask &= ~(N_PARITY_ERROR 1005 the_port->ignore_status_mask &= ~(N_PARITY_ERROR
1005 | N_FRAMING_ERROR); 1006 | N_FRAMING_ERROR);
1006 if (I_IGNBRK(state->port.tty)) { 1007 if (iflag & IGNBRK) {
1007 the_port->ignore_status_mask &= ~N_BREAK; 1008 the_port->ignore_status_mask &= ~N_BREAK;
1008 if (I_IGNPAR(state->port.tty)) 1009 if (iflag & IGNPAR)
1009 the_port->ignore_status_mask &= ~N_OVERRUN_ERROR; 1010 the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
1010 } 1011 }
1011 if (!(cflag & CREAD)) { 1012 if (!(cflag & CREAD)) {
diff --git a/drivers/serial/ioc4_serial.c b/drivers/serial/ioc4_serial.c
index 8ad28fc6492..fcfe82653ac 100644
--- a/drivers/serial/ioc4_serial.c
+++ b/drivers/serial/ioc4_serial.c
@@ -1685,11 +1685,12 @@ ioc4_change_speed(struct uart_port *the_port,
1685{ 1685{
1686 struct ioc4_port *port = get_ioc4_port(the_port, 0); 1686 struct ioc4_port *port = get_ioc4_port(the_port, 0);
1687 int baud, bits; 1687 int baud, bits;
1688 unsigned cflag; 1688 unsigned cflag, iflag;
1689 int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8; 1689 int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
1690 struct uart_state *state = the_port->state; 1690 struct uart_state *state = the_port->state;
1691 1691
1692 cflag = new_termios->c_cflag; 1692 cflag = new_termios->c_cflag;
1693 iflag = new_termios->c_iflag;
1693 1694
1694 switch (cflag & CSIZE) { 1695 switch (cflag & CSIZE) {
1695 case CS5: 1696 case CS5:
@@ -1741,12 +1742,12 @@ ioc4_change_speed(struct uart_port *the_port,
1741 1742
1742 state->port.tty->low_latency = 1; 1743 state->port.tty->low_latency = 1;
1743 1744
1744 if (I_IGNPAR(state->port.tty)) 1745 if (iflag & IGNPAR)
1745 the_port->ignore_status_mask &= ~(N_PARITY_ERROR 1746 the_port->ignore_status_mask &= ~(N_PARITY_ERROR
1746 | N_FRAMING_ERROR); 1747 | N_FRAMING_ERROR);
1747 if (I_IGNBRK(state->port.tty)) { 1748 if (iflag & IGNBRK) {
1748 the_port->ignore_status_mask &= ~N_BREAK; 1749 the_port->ignore_status_mask &= ~N_BREAK;
1749 if (I_IGNPAR(state->port.tty)) 1750 if (iflag & IGNPAR)
1750 the_port->ignore_status_mask &= ~N_OVERRUN_ERROR; 1751 the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
1751 } 1752 }
1752 if (!(cflag & CREAD)) { 1753 if (!(cflag & CREAD)) {
diff --git a/drivers/serial/max3100.c b/drivers/serial/max3100.c
index 3351c3bd59e..beb1afa27d8 100644
--- a/drivers/serial/max3100.c
+++ b/drivers/serial/max3100.c
@@ -430,17 +430,14 @@ max3100_set_termios(struct uart_port *port, struct ktermios *termios,
430 int baud = 0; 430 int baud = 0;
431 unsigned cflag; 431 unsigned cflag;
432 u32 param_new, param_mask, parity = 0; 432 u32 param_new, param_mask, parity = 0;
433 struct tty_struct *tty = s->port.state->port.tty;
434 433
435 dev_dbg(&s->spi->dev, "%s\n", __func__); 434 dev_dbg(&s->spi->dev, "%s\n", __func__);
436 if (!tty)
437 return;
438 435
439 cflag = termios->c_cflag; 436 cflag = termios->c_cflag;
440 param_new = 0; 437 param_new = 0;
441 param_mask = 0; 438 param_mask = 0;
442 439
443 baud = tty_get_baud_rate(tty); 440 baud = tty_termios_baud_rate(termios);
444 param_new = s->conf & MAX3100_BAUD; 441 param_new = s->conf & MAX3100_BAUD;
445 switch (baud) { 442 switch (baud) {
446 case 300: 443 case 300:
@@ -485,7 +482,7 @@ max3100_set_termios(struct uart_port *port, struct ktermios *termios,
485 default: 482 default:
486 baud = s->baud; 483 baud = s->baud;
487 } 484 }
488 tty_encode_baud_rate(tty, baud, baud); 485 tty_termios_encode_baud_rate(termios, baud, baud);
489 s->baud = baud; 486 s->baud = baud;
490 param_mask |= MAX3100_BAUD; 487 param_mask |= MAX3100_BAUD;
491 488
diff --git a/drivers/serial/max3107-aava.c b/drivers/serial/max3107-aava.c
new file mode 100644
index 00000000000..a1fe304f2f5
--- /dev/null
+++ b/drivers/serial/max3107-aava.c
@@ -0,0 +1,344 @@
1/*
2 * max3107.c - spi uart protocol driver for Maxim 3107
3 * Based on max3100.c
4 * by Christian Pellegrin <chripell@evolware.org>
5 * and max3110.c
6 * by Feng Tang <feng.tang@intel.com>
7 *
8 * Copyright (C) Aavamobile 2009
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 *
28 */
29
30#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/spi/spi.h>
35#include <linux/freezer.h>
36#include <linux/platform_device.h>
37#include <linux/gpio.h>
38#include <linux/sfi.h>
39#include <asm/mrst.h>
40#include "max3107.h"
41
42/* GPIO direction to input function */
43static int max3107_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
44{
45 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
46 u16 buf[1]; /* Buffer for SPI transfer */
47
48 if (offset >= MAX3107_GPIO_COUNT) {
49 dev_err(&s->spi->dev, "Invalid GPIO\n");
50 return -EINVAL;
51 }
52
53 /* Read current GPIO configuration register */
54 buf[0] = MAX3107_GPIOCFG_REG;
55 /* Perform SPI transfer */
56 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
57 dev_err(&s->spi->dev, "SPI transfer GPIO read failed\n");
58 return -EIO;
59 }
60 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
61
62 /* Set GPIO to input */
63 buf[0] &= ~(0x0001 << offset);
64
65 /* Write new GPIO configuration register value */
66 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
67 /* Perform SPI transfer */
68 if (max3107_rw(s, (u8 *)buf, NULL, 2)) {
69 dev_err(&s->spi->dev, "SPI transfer GPIO write failed\n");
70 return -EIO;
71 }
72 return 0;
73}
74
75/* GPIO direction to output function */
76static int max3107_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
77 int value)
78{
79 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
80 u16 buf[2]; /* Buffer for SPI transfers */
81
82 if (offset >= MAX3107_GPIO_COUNT) {
83 dev_err(&s->spi->dev, "Invalid GPIO\n");
84 return -EINVAL;
85 }
86
87 /* Read current GPIO configuration and data registers */
88 buf[0] = MAX3107_GPIOCFG_REG;
89 buf[1] = MAX3107_GPIODATA_REG;
90 /* Perform SPI transfer */
91 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
92 dev_err(&s->spi->dev, "SPI transfer gpio failed\n");
93 return -EIO;
94 }
95 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
96 buf[1] &= MAX3107_SPI_RX_DATA_MASK;
97
98 /* Set GPIO to output */
99 buf[0] |= (0x0001 << offset);
100 /* Set value */
101 if (value)
102 buf[1] |= (0x0001 << offset);
103 else
104 buf[1] &= ~(0x0001 << offset);
105
106 /* Write new GPIO configuration and data register values */
107 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
108 buf[1] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
109 /* Perform SPI transfer */
110 if (max3107_rw(s, (u8 *)buf, NULL, 4)) {
111 dev_err(&s->spi->dev,
112 "SPI transfer for GPIO conf data w failed\n");
113 return -EIO;
114 }
115 return 0;
116}
117
118/* GPIO value query function */
119static int max3107_gpio_get(struct gpio_chip *chip, unsigned offset)
120{
121 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
122 u16 buf[1]; /* Buffer for SPI transfer */
123
124 if (offset >= MAX3107_GPIO_COUNT) {
125 dev_err(&s->spi->dev, "Invalid GPIO\n");
126 return -EINVAL;
127 }
128
129 /* Read current GPIO data register */
130 buf[0] = MAX3107_GPIODATA_REG;
131 /* Perform SPI transfer */
132 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
133 dev_err(&s->spi->dev, "SPI transfer GPIO data r failed\n");
134 return -EIO;
135 }
136 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
137
138 /* Return value */
139 return buf[0] & (0x0001 << offset);
140}
141
142/* GPIO value set function */
143static void max3107_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
144{
145 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
146 u16 buf[2]; /* Buffer for SPI transfers */
147
148 if (offset >= MAX3107_GPIO_COUNT) {
149 dev_err(&s->spi->dev, "Invalid GPIO\n");
150 return;
151 }
152
153 /* Read current GPIO configuration registers*/
154 buf[0] = MAX3107_GPIODATA_REG;
155 buf[1] = MAX3107_GPIOCFG_REG;
156 /* Perform SPI transfer */
157 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
158 dev_err(&s->spi->dev,
159 "SPI transfer for GPIO data and config read failed\n");
160 return;
161 }
162 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
163 buf[1] &= MAX3107_SPI_RX_DATA_MASK;
164
165 if (!(buf[1] & (0x0001 << offset))) {
166 /* Configured as input, can't set value */
167 dev_warn(&s->spi->dev,
168 "Trying to set value for input GPIO\n");
169 return;
170 }
171
172 /* Set value */
173 if (value)
174 buf[0] |= (0x0001 << offset);
175 else
176 buf[0] &= ~(0x0001 << offset);
177
178 /* Write new GPIO data register value */
179 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
180 /* Perform SPI transfer */
181 if (max3107_rw(s, (u8 *)buf, NULL, 2))
182 dev_err(&s->spi->dev, "SPI transfer GPIO data w failed\n");
183}
184
185/* GPIO chip data */
186static struct gpio_chip max3107_gpio_chip = {
187 .owner = THIS_MODULE,
188 .direction_input = max3107_gpio_direction_in,
189 .direction_output = max3107_gpio_direction_out,
190 .get = max3107_gpio_get,
191 .set = max3107_gpio_set,
192 .can_sleep = 1,
193 .base = MAX3107_GPIO_BASE,
194 .ngpio = MAX3107_GPIO_COUNT,
195};
196
197/**
198 * max3107_aava_reset - reset on AAVA systems
199 * @spi: The SPI device we are probing
200 *
201 * Reset the device ready for probing.
202 */
203
204static int max3107_aava_reset(struct spi_device *spi)
205{
206 /* Reset the chip */
207 if (gpio_request(MAX3107_RESET_GPIO, "max3107")) {
208 pr_err("Requesting RESET GPIO failed\n");
209 return -EIO;
210 }
211 if (gpio_direction_output(MAX3107_RESET_GPIO, 0)) {
212 pr_err("Setting RESET GPIO to 0 failed\n");
213 gpio_free(MAX3107_RESET_GPIO);
214 return -EIO;
215 }
216 msleep(MAX3107_RESET_DELAY);
217 if (gpio_direction_output(MAX3107_RESET_GPIO, 1)) {
218 pr_err("Setting RESET GPIO to 1 failed\n");
219 gpio_free(MAX3107_RESET_GPIO);
220 return -EIO;
221 }
222 gpio_free(MAX3107_RESET_GPIO);
223 msleep(MAX3107_WAKEUP_DELAY);
224 return 0;
225}
226
227static int max3107_aava_configure(struct max3107_port *s)
228{
229 int retval;
230
231 /* Initialize GPIO chip data */
232 s->chip = max3107_gpio_chip;
233 s->chip.label = s->spi->modalias;
234 s->chip.dev = &s->spi->dev;
235
236 /* Add GPIO chip */
237 retval = gpiochip_add(&s->chip);
238 if (retval) {
239 dev_err(&s->spi->dev, "Adding GPIO chip failed\n");
240 return retval;
241 }
242
243 /* Temporary fix for EV2 boot problems, set modem reset to 0 */
244 max3107_gpio_direction_out(&s->chip, 3, 0);
245 return 0;
246}
247
248#if 0
249/* This will get enabled once we have the board stuff merged for this
250 specific case */
251
252static const struct baud_table brg13_ext[] = {
253 { 300, MAX3107_BRG13_B300 },
254 { 600, MAX3107_BRG13_B600 },
255 { 1200, MAX3107_BRG13_B1200 },
256 { 2400, MAX3107_BRG13_B2400 },
257 { 4800, MAX3107_BRG13_B4800 },
258 { 9600, MAX3107_BRG13_B9600 },
259 { 19200, MAX3107_BRG13_B19200 },
260 { 57600, MAX3107_BRG13_B57600 },
261 { 115200, MAX3107_BRG13_B115200 },
262 { 230400, MAX3107_BRG13_B230400 },
263 { 460800, MAX3107_BRG13_B460800 },
264 { 921600, MAX3107_BRG13_B921600 },
265 { 0, 0 }
266};
267
268static void max3107_aava_init(struct max3107_port *s)
269{
270 /*override for AAVA SC specific*/
271 if (mrst_platform_id() == MRST_PLATFORM_AAVA_SC) {
272 if (get_koski_build_id() <= KOSKI_EV2)
273 if (s->ext_clk) {
274 s->brg_cfg = MAX3107_BRG13_B9600;
275 s->baud_tbl = (struct baud_table *)brg13_ext;
276 }
277 }
278}
279#endif
280
281static int __devexit max3107_aava_remove(struct spi_device *spi)
282{
283 struct max3107_port *s = dev_get_drvdata(&spi->dev);
284
285 /* Remove GPIO chip */
286 if (gpiochip_remove(&s->chip))
287 dev_warn(&spi->dev, "Removing GPIO chip failed\n");
288
289 /* Then do the default remove */
290 return max3107_remove(spi);
291}
292
293/* Platform data */
294static struct max3107_plat aava_plat_data = {
295 .loopback = 0,
296 .ext_clk = 1,
297/* .init = max3107_aava_init, */
298 .configure = max3107_aava_configure,
299 .hw_suspend = max3107_hw_susp,
300 .polled_mode = 0,
301 .poll_time = 0,
302};
303
304
305static int __devinit max3107_probe_aava(struct spi_device *spi)
306{
307 int err = max3107_aava_reset(spi);
308 if (err < 0)
309 return err;
310 return max3107_probe(spi, &aava_plat_data);
311}
312
313/* Spi driver data */
314static struct spi_driver max3107_driver = {
315 .driver = {
316 .name = "aava-max3107",
317 .bus = &spi_bus_type,
318 .owner = THIS_MODULE,
319 },
320 .probe = max3107_probe_aava,
321 .remove = __devexit_p(max3107_aava_remove),
322 .suspend = max3107_suspend,
323 .resume = max3107_resume,
324};
325
326/* Driver init function */
327static int __init max3107_init(void)
328{
329 return spi_register_driver(&max3107_driver);
330}
331
332/* Driver exit function */
333static void __exit max3107_exit(void)
334{
335 spi_unregister_driver(&max3107_driver);
336}
337
338module_init(max3107_init);
339module_exit(max3107_exit);
340
341MODULE_DESCRIPTION("MAX3107 driver");
342MODULE_AUTHOR("Aavamobile");
343MODULE_ALIAS("aava-max3107-spi");
344MODULE_LICENSE("GPL v2");
diff --git a/drivers/serial/max3107.c b/drivers/serial/max3107.c
new file mode 100644
index 00000000000..67283c1a57f
--- /dev/null
+++ b/drivers/serial/max3107.c
@@ -0,0 +1,1197 @@
1/*
2 * max3107.c - spi uart protocol driver for Maxim 3107
3 * Based on max3100.c
4 * by Christian Pellegrin <chripell@evolware.org>
5 * and max3110.c
6 * by Feng Tang <feng.tang@intel.com>
7 *
8 * Copyright (C) Aavamobile 2009
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 *
28 */
29
30#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/gpio.h>
35#include <linux/spi/spi.h>
36#include <linux/freezer.h>
37#include "max3107.h"
38
39static const struct baud_table brg26_ext[] = {
40 { 300, MAX3107_BRG26_B300 },
41 { 600, MAX3107_BRG26_B600 },
42 { 1200, MAX3107_BRG26_B1200 },
43 { 2400, MAX3107_BRG26_B2400 },
44 { 4800, MAX3107_BRG26_B4800 },
45 { 9600, MAX3107_BRG26_B9600 },
46 { 19200, MAX3107_BRG26_B19200 },
47 { 57600, MAX3107_BRG26_B57600 },
48 { 115200, MAX3107_BRG26_B115200 },
49 { 230400, MAX3107_BRG26_B230400 },
50 { 460800, MAX3107_BRG26_B460800 },
51 { 921600, MAX3107_BRG26_B921600 },
52 { 0, 0 }
53};
54
55static const struct baud_table brg13_int[] = {
56 { 300, MAX3107_BRG13_IB300 },
57 { 600, MAX3107_BRG13_IB600 },
58 { 1200, MAX3107_BRG13_IB1200 },
59 { 2400, MAX3107_BRG13_IB2400 },
60 { 4800, MAX3107_BRG13_IB4800 },
61 { 9600, MAX3107_BRG13_IB9600 },
62 { 19200, MAX3107_BRG13_IB19200 },
63 { 57600, MAX3107_BRG13_IB57600 },
64 { 115200, MAX3107_BRG13_IB115200 },
65 { 230400, MAX3107_BRG13_IB230400 },
66 { 460800, MAX3107_BRG13_IB460800 },
67 { 921600, MAX3107_BRG13_IB921600 },
68 { 0, 0 }
69};
70
71static u32 get_new_brg(int baud, struct max3107_port *s)
72{
73 int i;
74 const struct baud_table *baud_tbl = s->baud_tbl;
75
76 for (i = 0; i < 13; i++) {
77 if (baud == baud_tbl[i].baud)
78 return baud_tbl[i].new_brg;
79 }
80
81 return 0;
82}
83
84/* Perform SPI transfer for write/read of device register(s) */
85int max3107_rw(struct max3107_port *s, u8 *tx, u8 *rx, int len)
86{
87 struct spi_message spi_msg;
88 struct spi_transfer spi_xfer;
89
90 /* Initialize SPI ,message */
91 spi_message_init(&spi_msg);
92
93 /* Initialize SPI transfer */
94 memset(&spi_xfer, 0, sizeof spi_xfer);
95 spi_xfer.len = len;
96 spi_xfer.tx_buf = tx;
97 spi_xfer.rx_buf = rx;
98 spi_xfer.speed_hz = MAX3107_SPI_SPEED;
99
100 /* Add SPI transfer to SPI message */
101 spi_message_add_tail(&spi_xfer, &spi_msg);
102
103#ifdef DBG_TRACE_SPI_DATA
104 {
105 int i;
106 pr_info("tx len %d:\n", spi_xfer.len);
107 for (i = 0 ; i < spi_xfer.len && i < 32 ; i++)
108 pr_info(" %x", ((u8 *)spi_xfer.tx_buf)[i]);
109 pr_info("\n");
110 }
111#endif
112
113 /* Perform synchronous SPI transfer */
114 if (spi_sync(s->spi, &spi_msg)) {
115 dev_err(&s->spi->dev, "spi_sync failure\n");
116 return -EIO;
117 }
118
119#ifdef DBG_TRACE_SPI_DATA
120 if (spi_xfer.rx_buf) {
121 int i;
122 pr_info("rx len %d:\n", spi_xfer.len);
123 for (i = 0 ; i < spi_xfer.len && i < 32 ; i++)
124 pr_info(" %x", ((u8 *)spi_xfer.rx_buf)[i]);
125 pr_info("\n");
126 }
127#endif
128 return 0;
129}
130EXPORT_SYMBOL_GPL(max3107_rw);
131
132/* Puts received data to circular buffer */
133static void put_data_to_circ_buf(struct max3107_port *s, unsigned char *data,
134 int len)
135{
136 struct uart_port *port = &s->port;
137 struct tty_struct *tty;
138
139 if (!port->state)
140 return;
141
142 tty = port->state->port.tty;
143 if (!tty)
144 return;
145
146 /* Insert received data */
147 tty_insert_flip_string(tty, data, len);
148 /* Update RX counter */
149 port->icount.rx += len;
150}
151
152/* Handle data receiving */
153static void max3107_handlerx(struct max3107_port *s, u16 rxlvl)
154{
155 int i;
156 int j;
157 int len; /* SPI transfer buffer length */
158 u16 *buf;
159 u8 *valid_str;
160
161 if (!s->rx_enabled)
162 /* RX is disabled */
163 return;
164
165 if (rxlvl == 0) {
166 /* RX fifo is empty */
167 return;
168 } else if (rxlvl >= MAX3107_RX_FIFO_SIZE) {
169 dev_warn(&s->spi->dev, "Possible RX FIFO overrun %d\n", rxlvl);
170 /* Ensure sanity of RX level */
171 rxlvl = MAX3107_RX_FIFO_SIZE;
172 }
173 if ((s->rxbuf == 0) || (s->rxstr == 0)) {
174 dev_warn(&s->spi->dev, "Rx buffer/str isn't ready\n");
175 return;
176 }
177 buf = s->rxbuf;
178 valid_str = s->rxstr;
179 while (rxlvl) {
180 pr_debug("rxlvl %d\n", rxlvl);
181 /* Clear buffer */
182 memset(buf, 0, sizeof(u16) * (MAX3107_RX_FIFO_SIZE + 2));
183 len = 0;
184 if (s->irqen_reg & MAX3107_IRQ_RXFIFO_BIT) {
185 /* First disable RX FIFO interrupt */
186 pr_debug("Disabling RX INT\n");
187 buf[0] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
188 s->irqen_reg &= ~MAX3107_IRQ_RXFIFO_BIT;
189 buf[0] |= s->irqen_reg;
190 len++;
191 }
192 /* Just increase the length by amount of words in FIFO since
193 * buffer was zeroed and SPI transfer of 0x0000 means reading
194 * from RX FIFO
195 */
196 len += rxlvl;
197 /* Append RX level query */
198 buf[len] = MAX3107_RXFIFOLVL_REG;
199 len++;
200
201 /* Perform the SPI transfer */
202 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, len * 2)) {
203 dev_err(&s->spi->dev, "SPI transfer for RX h failed\n");
204 return;
205 }
206
207 /* Skip RX FIFO interrupt disabling word if it was added */
208 j = ((len - 1) - rxlvl);
209 /* Read received words */
210 for (i = 0; i < rxlvl; i++, j++)
211 valid_str[i] = (u8)buf[j];
212 put_data_to_circ_buf(s, valid_str, rxlvl);
213 /* Get new RX level */
214 rxlvl = (buf[len - 1] & MAX3107_SPI_RX_DATA_MASK);
215 }
216
217 if (s->rx_enabled) {
218 /* RX still enabled, re-enable RX FIFO interrupt */
219 pr_debug("Enabling RX INT\n");
220 buf[0] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
221 s->irqen_reg |= MAX3107_IRQ_RXFIFO_BIT;
222 buf[0] |= s->irqen_reg;
223 if (max3107_rw(s, (u8 *)buf, NULL, 2))
224 dev_err(&s->spi->dev, "RX FIFO INT enabling failed\n");
225 }
226
227 /* Push the received data to receivers */
228 if (s->port.state->port.tty)
229 tty_flip_buffer_push(s->port.state->port.tty);
230}
231
232
233/* Handle data sending */
234static void max3107_handletx(struct max3107_port *s)
235{
236 struct circ_buf *xmit = &s->port.state->xmit;
237 int i;
238 unsigned long flags;
239 int len; /* SPI transfer buffer length */
240 u16 *buf;
241
242 if (!s->tx_fifo_empty)
243 /* Don't send more data before previous data is sent */
244 return;
245
246 if (uart_circ_empty(xmit) || uart_tx_stopped(&s->port))
247 /* No data to send or TX is stopped */
248 return;
249
250 if (!s->txbuf) {
251 dev_warn(&s->spi->dev, "Txbuf isn't ready\n");
252 return;
253 }
254 buf = s->txbuf;
255 /* Get length of data pending in circular buffer */
256 len = uart_circ_chars_pending(xmit);
257 if (len) {
258 /* Limit to size of TX FIFO */
259 if (len > MAX3107_TX_FIFO_SIZE)
260 len = MAX3107_TX_FIFO_SIZE;
261
262 pr_debug("txlen %d\n", len);
263
264 /* Update TX counter */
265 s->port.icount.tx += len;
266
267 /* TX FIFO will no longer be empty */
268 s->tx_fifo_empty = 0;
269
270 i = 0;
271 if (s->irqen_reg & MAX3107_IRQ_TXEMPTY_BIT) {
272 /* First disable TX empty interrupt */
273 pr_debug("Disabling TE INT\n");
274 buf[i] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
275 s->irqen_reg &= ~MAX3107_IRQ_TXEMPTY_BIT;
276 buf[i] |= s->irqen_reg;
277 i++;
278 len++;
279 }
280 /* Add data to send */
281 spin_lock_irqsave(&s->port.lock, flags);
282 for ( ; i < len ; i++) {
283 buf[i] = (MAX3107_WRITE_BIT | MAX3107_THR_REG);
284 buf[i] |= ((u16)xmit->buf[xmit->tail] &
285 MAX3107_SPI_TX_DATA_MASK);
286 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
287 }
288 spin_unlock_irqrestore(&s->port.lock, flags);
289 if (!(s->irqen_reg & MAX3107_IRQ_TXEMPTY_BIT)) {
290 /* Enable TX empty interrupt */
291 pr_debug("Enabling TE INT\n");
292 buf[i] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
293 s->irqen_reg |= MAX3107_IRQ_TXEMPTY_BIT;
294 buf[i] |= s->irqen_reg;
295 i++;
296 len++;
297 }
298 if (!s->tx_enabled) {
299 /* Enable TX */
300 pr_debug("Enable TX\n");
301 buf[i] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
302 spin_lock_irqsave(&s->data_lock, flags);
303 s->mode1_reg &= ~MAX3107_MODE1_TXDIS_BIT;
304 buf[i] |= s->mode1_reg;
305 spin_unlock_irqrestore(&s->data_lock, flags);
306 s->tx_enabled = 1;
307 i++;
308 len++;
309 }
310
311 /* Perform the SPI transfer */
312 if (max3107_rw(s, (u8 *)buf, NULL, len*2)) {
313 dev_err(&s->spi->dev,
314 "SPI transfer TX handling failed\n");
315 return;
316 }
317 }
318
319 /* Indicate wake up if circular buffer is getting low on data */
320 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
321 uart_write_wakeup(&s->port);
322
323}
324
325/* Handle interrupts
326 * Also reads and returns current RX FIFO level
327 */
328static u16 handle_interrupt(struct max3107_port *s)
329{
330 u16 buf[4]; /* Buffer for SPI transfers */
331 u8 irq_status;
332 u16 rx_level;
333 unsigned long flags;
334
335 /* Read IRQ status register */
336 buf[0] = MAX3107_IRQSTS_REG;
337 /* Read status IRQ status register */
338 buf[1] = MAX3107_STS_IRQSTS_REG;
339 /* Read LSR IRQ status register */
340 buf[2] = MAX3107_LSR_IRQSTS_REG;
341 /* Query RX level */
342 buf[3] = MAX3107_RXFIFOLVL_REG;
343
344 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 8)) {
345 dev_err(&s->spi->dev,
346 "SPI transfer for INTR handling failed\n");
347 return 0;
348 }
349
350 irq_status = (u8)buf[0];
351 pr_debug("IRQSTS %x\n", irq_status);
352 rx_level = (buf[3] & MAX3107_SPI_RX_DATA_MASK);
353
354 if (irq_status & MAX3107_IRQ_LSR_BIT) {
355 /* LSR interrupt */
356 if (buf[2] & MAX3107_LSR_RXTO_BIT)
357 /* RX timeout interrupt,
358 * handled by normal RX handling
359 */
360 pr_debug("RX TO INT\n");
361 }
362
363 if (irq_status & MAX3107_IRQ_TXEMPTY_BIT) {
364 /* Tx empty interrupt,
365 * disable TX and set tx_fifo_empty flag
366 */
367 pr_debug("TE INT, disabling TX\n");
368 buf[0] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
369 spin_lock_irqsave(&s->data_lock, flags);
370 s->mode1_reg |= MAX3107_MODE1_TXDIS_BIT;
371 buf[0] |= s->mode1_reg;
372 spin_unlock_irqrestore(&s->data_lock, flags);
373 if (max3107_rw(s, (u8 *)buf, NULL, 2))
374 dev_err(&s->spi->dev, "SPI transfer TX dis failed\n");
375 s->tx_enabled = 0;
376 s->tx_fifo_empty = 1;
377 }
378
379 if (irq_status & MAX3107_IRQ_RXFIFO_BIT)
380 /* RX FIFO interrupt,
381 * handled by normal RX handling
382 */
383 pr_debug("RFIFO INT\n");
384
385 /* Return RX level */
386 return rx_level;
387}
388
389/* Trigger work thread*/
390static void max3107_dowork(struct max3107_port *s)
391{
392 if (!work_pending(&s->work) && !freezing(current) && !s->suspended)
393 queue_work(s->workqueue, &s->work);
394 else
395 dev_warn(&s->spi->dev, "interrup isn't serviced normally!\n");
396}
397
398/* Work thread */
399static void max3107_work(struct work_struct *w)
400{
401 struct max3107_port *s = container_of(w, struct max3107_port, work);
402 u16 rxlvl = 0;
403 int len; /* SPI transfer buffer length */
404 u16 buf[5]; /* Buffer for SPI transfers */
405 unsigned long flags;
406
407 /* Start by reading current RX FIFO level */
408 buf[0] = MAX3107_RXFIFOLVL_REG;
409 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
410 dev_err(&s->spi->dev, "SPI transfer RX lev failed\n");
411 rxlvl = 0;
412 } else {
413 rxlvl = (buf[0] & MAX3107_SPI_RX_DATA_MASK);
414 }
415
416 do {
417 pr_debug("rxlvl %d\n", rxlvl);
418
419 /* Handle RX */
420 max3107_handlerx(s, rxlvl);
421 rxlvl = 0;
422
423 if (s->handle_irq) {
424 /* Handle pending interrupts
425 * We also get new RX FIFO level since new data may
426 * have been received while pushing received data to
427 * receivers
428 */
429 s->handle_irq = 0;
430 rxlvl = handle_interrupt(s);
431 }
432
433 /* Handle TX */
434 max3107_handletx(s);
435
436 /* Handle configuration changes */
437 len = 0;
438 spin_lock_irqsave(&s->data_lock, flags);
439 if (s->mode1_commit) {
440 pr_debug("mode1_commit\n");
441 buf[len] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
442 buf[len++] |= s->mode1_reg;
443 s->mode1_commit = 0;
444 }
445 if (s->lcr_commit) {
446 pr_debug("lcr_commit\n");
447 buf[len] = (MAX3107_WRITE_BIT | MAX3107_LCR_REG);
448 buf[len++] |= s->lcr_reg;
449 s->lcr_commit = 0;
450 }
451 if (s->brg_commit) {
452 pr_debug("brg_commit\n");
453 buf[len] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVMSB_REG);
454 buf[len++] |= ((s->brg_cfg >> 16) &
455 MAX3107_SPI_TX_DATA_MASK);
456 buf[len] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVLSB_REG);
457 buf[len++] |= ((s->brg_cfg >> 8) &
458 MAX3107_SPI_TX_DATA_MASK);
459 buf[len] = (MAX3107_WRITE_BIT | MAX3107_BRGCFG_REG);
460 buf[len++] |= ((s->brg_cfg) & 0xff);
461 s->brg_commit = 0;
462 }
463 spin_unlock_irqrestore(&s->data_lock, flags);
464
465 if (len > 0) {
466 if (max3107_rw(s, (u8 *)buf, NULL, len * 2))
467 dev_err(&s->spi->dev,
468 "SPI transfer config failed\n");
469 }
470
471 /* Reloop if interrupt handling indicated data in RX FIFO */
472 } while (rxlvl);
473
474}
475
476/* Set sleep mode */
477static void max3107_set_sleep(struct max3107_port *s, int mode)
478{
479 u16 buf[1]; /* Buffer for SPI transfer */
480 unsigned long flags;
481 pr_debug("enter, mode %d\n", mode);
482
483 buf[0] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
484 spin_lock_irqsave(&s->data_lock, flags);
485 switch (mode) {
486 case MAX3107_DISABLE_FORCED_SLEEP:
487 s->mode1_reg &= ~MAX3107_MODE1_FORCESLEEP_BIT;
488 break;
489 case MAX3107_ENABLE_FORCED_SLEEP:
490 s->mode1_reg |= MAX3107_MODE1_FORCESLEEP_BIT;
491 break;
492 case MAX3107_DISABLE_AUTOSLEEP:
493 s->mode1_reg &= ~MAX3107_MODE1_AUTOSLEEP_BIT;
494 break;
495 case MAX3107_ENABLE_AUTOSLEEP:
496 s->mode1_reg |= MAX3107_MODE1_AUTOSLEEP_BIT;
497 break;
498 default:
499 spin_unlock_irqrestore(&s->data_lock, flags);
500 dev_warn(&s->spi->dev, "invalid sleep mode\n");
501 return;
502 }
503 buf[0] |= s->mode1_reg;
504 spin_unlock_irqrestore(&s->data_lock, flags);
505
506 if (max3107_rw(s, (u8 *)buf, NULL, 2))
507 dev_err(&s->spi->dev, "SPI transfer sleep mode failed\n");
508
509 if (mode == MAX3107_DISABLE_AUTOSLEEP ||
510 mode == MAX3107_DISABLE_FORCED_SLEEP)
511 msleep(MAX3107_WAKEUP_DELAY);
512}
513
514/* Perform full register initialization */
515static void max3107_register_init(struct max3107_port *s)
516{
517 u16 buf[11]; /* Buffer for SPI transfers */
518
519 /* 1. Configure baud rate, 9600 as default */
520 s->baud = 9600;
521 /* the below is default*/
522 if (s->ext_clk) {
523 s->brg_cfg = MAX3107_BRG26_B9600;
524 s->baud_tbl = (struct baud_table *)brg26_ext;
525 } else {
526 s->brg_cfg = MAX3107_BRG13_IB9600;
527 s->baud_tbl = (struct baud_table *)brg13_int;
528 }
529
530 if (s->pdata->init)
531 s->pdata->init(s);
532
533 buf[0] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVMSB_REG)
534 | ((s->brg_cfg >> 16) & MAX3107_SPI_TX_DATA_MASK);
535 buf[1] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVLSB_REG)
536 | ((s->brg_cfg >> 8) & MAX3107_SPI_TX_DATA_MASK);
537 buf[2] = (MAX3107_WRITE_BIT | MAX3107_BRGCFG_REG)
538 | ((s->brg_cfg) & 0xff);
539
540 /* 2. Configure LCR register, 8N1 mode by default */
541 s->lcr_reg = MAX3107_LCR_WORD_LEN_8;
542 buf[3] = (MAX3107_WRITE_BIT | MAX3107_LCR_REG)
543 | s->lcr_reg;
544
545 /* 3. Configure MODE 1 register */
546 s->mode1_reg = 0;
547 /* Enable IRQ pin */
548 s->mode1_reg |= MAX3107_MODE1_IRQSEL_BIT;
549 /* Disable TX */
550 s->mode1_reg |= MAX3107_MODE1_TXDIS_BIT;
551 s->tx_enabled = 0;
552 /* RX is enabled */
553 s->rx_enabled = 1;
554 buf[4] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG)
555 | s->mode1_reg;
556
557 /* 4. Configure MODE 2 register */
558 buf[5] = (MAX3107_WRITE_BIT | MAX3107_MODE2_REG);
559 if (s->loopback) {
560 /* Enable loopback */
561 buf[5] |= MAX3107_MODE2_LOOPBACK_BIT;
562 }
563 /* Reset FIFOs */
564 buf[5] |= MAX3107_MODE2_FIFORST_BIT;
565 s->tx_fifo_empty = 1;
566
567 /* 5. Configure FIFO trigger level register */
568 buf[6] = (MAX3107_WRITE_BIT | MAX3107_FIFOTRIGLVL_REG);
569 /* RX FIFO trigger for 16 words, TX FIFO trigger not used */
570 buf[6] |= (MAX3107_FIFOTRIGLVL_RX(16) | MAX3107_FIFOTRIGLVL_TX(0));
571
572 /* 6. Configure flow control levels */
573 buf[7] = (MAX3107_WRITE_BIT | MAX3107_FLOWLVL_REG);
574 /* Flow control halt level 96, resume level 48 */
575 buf[7] |= (MAX3107_FLOWLVL_RES(48) | MAX3107_FLOWLVL_HALT(96));
576
577 /* 7. Configure flow control */
578 buf[8] = (MAX3107_WRITE_BIT | MAX3107_FLOWCTRL_REG);
579 /* Enable auto CTS and auto RTS flow control */
580 buf[8] |= (MAX3107_FLOWCTRL_AUTOCTS_BIT | MAX3107_FLOWCTRL_AUTORTS_BIT);
581
582 /* 8. Configure RX timeout register */
583 buf[9] = (MAX3107_WRITE_BIT | MAX3107_RXTO_REG);
584 /* Timeout after 48 character intervals */
585 buf[9] |= 0x0030;
586
587 /* 9. Configure LSR interrupt enable register */
588 buf[10] = (MAX3107_WRITE_BIT | MAX3107_LSR_IRQEN_REG);
589 /* Enable RX timeout interrupt */
590 buf[10] |= MAX3107_LSR_RXTO_BIT;
591
592 /* Perform SPI transfer */
593 if (max3107_rw(s, (u8 *)buf, NULL, 22))
594 dev_err(&s->spi->dev, "SPI transfer for init failed\n");
595
596 /* 10. Clear IRQ status register by reading it */
597 buf[0] = MAX3107_IRQSTS_REG;
598
599 /* 11. Configure interrupt enable register */
600 /* Enable LSR interrupt */
601 s->irqen_reg = MAX3107_IRQ_LSR_BIT;
602 /* Enable RX FIFO interrupt */
603 s->irqen_reg |= MAX3107_IRQ_RXFIFO_BIT;
604 buf[1] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG)
605 | s->irqen_reg;
606
607 /* 12. Clear FIFO reset that was set in step 6 */
608 buf[2] = (MAX3107_WRITE_BIT | MAX3107_MODE2_REG);
609 if (s->loopback) {
610 /* Keep loopback enabled */
611 buf[2] |= MAX3107_MODE2_LOOPBACK_BIT;
612 }
613
614 /* Perform SPI transfer */
615 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 6))
616 dev_err(&s->spi->dev, "SPI transfer for init failed\n");
617
618}
619
620/* IRQ handler */
621static irqreturn_t max3107_irq(int irqno, void *dev_id)
622{
623 struct max3107_port *s = dev_id;
624
625 if (irqno != s->spi->irq) {
626 /* Unexpected IRQ */
627 return IRQ_NONE;
628 }
629
630 /* Indicate irq */
631 s->handle_irq = 1;
632
633 /* Trigger work thread */
634 max3107_dowork(s);
635
636 return IRQ_HANDLED;
637}
638
639/* HW suspension function
640 *
641 * Currently autosleep is used to decrease current consumption, alternative
642 * approach would be to set the chip to reset mode if UART is not being
643 * used but that would mess the GPIOs
644 *
645 */
646void max3107_hw_susp(struct max3107_port *s, int suspend)
647{
648 pr_debug("enter, suspend %d\n", suspend);
649
650 if (suspend) {
651 /* Suspend requested,
652 * enable autosleep to decrease current consumption
653 */
654 s->suspended = 1;
655 max3107_set_sleep(s, MAX3107_ENABLE_AUTOSLEEP);
656 } else {
657 /* Resume requested,
658 * disable autosleep
659 */
660 s->suspended = 0;
661 max3107_set_sleep(s, MAX3107_DISABLE_AUTOSLEEP);
662 }
663}
664EXPORT_SYMBOL_GPL(max3107_hw_susp);
665
666/* Modem status IRQ enabling */
667static void max3107_enable_ms(struct uart_port *port)
668{
669 /* Modem status not supported */
670}
671
672/* Data send function */
673static void max3107_start_tx(struct uart_port *port)
674{
675 struct max3107_port *s = container_of(port, struct max3107_port, port);
676
677 /* Trigger work thread for sending data */
678 max3107_dowork(s);
679}
680
681/* Function for checking that there is no pending transfers */
682static unsigned int max3107_tx_empty(struct uart_port *port)
683{
684 struct max3107_port *s = container_of(port, struct max3107_port, port);
685
686 pr_debug("returning %d\n",
687 (s->tx_fifo_empty && uart_circ_empty(&s->port.state->xmit)));
688 return s->tx_fifo_empty && uart_circ_empty(&s->port.state->xmit);
689}
690
691/* Function for stopping RX */
692static void max3107_stop_rx(struct uart_port *port)
693{
694 struct max3107_port *s = container_of(port, struct max3107_port, port);
695 unsigned long flags;
696
697 /* Set RX disabled in MODE 1 register */
698 spin_lock_irqsave(&s->data_lock, flags);
699 s->mode1_reg |= MAX3107_MODE1_RXDIS_BIT;
700 s->mode1_commit = 1;
701 spin_unlock_irqrestore(&s->data_lock, flags);
702 /* Set RX disabled */
703 s->rx_enabled = 0;
704 /* Trigger work thread for doing the actual configuration change */
705 max3107_dowork(s);
706}
707
708/* Function for returning control pin states */
709static unsigned int max3107_get_mctrl(struct uart_port *port)
710{
711 /* DCD and DSR are not wired and CTS/RTS is handled automatically
712 * so just indicate DSR and CAR asserted
713 */
714 return TIOCM_DSR | TIOCM_CAR;
715}
716
717/* Function for setting control pin states */
718static void max3107_set_mctrl(struct uart_port *port, unsigned int mctrl)
719{
720 /* DCD and DSR are not wired and CTS/RTS is hadnled automatically
721 * so do nothing
722 */
723}
724
725/* Function for configuring UART parameters */
726static void max3107_set_termios(struct uart_port *port,
727 struct ktermios *termios,
728 struct ktermios *old)
729{
730 struct max3107_port *s = container_of(port, struct max3107_port, port);
731 struct tty_struct *tty;
732 int baud;
733 u16 new_lcr = 0;
734 u32 new_brg = 0;
735 unsigned long flags;
736
737 if (!port->state)
738 return;
739
740 tty = port->state->port.tty;
741 if (!tty)
742 return;
743
744 /* Get new LCR register values */
745 /* Word size */
746 if ((termios->c_cflag & CSIZE) == CS7)
747 new_lcr |= MAX3107_LCR_WORD_LEN_7;
748 else
749 new_lcr |= MAX3107_LCR_WORD_LEN_8;
750
751 /* Parity */
752 if (termios->c_cflag & PARENB) {
753 new_lcr |= MAX3107_LCR_PARITY_BIT;
754 if (!(termios->c_cflag & PARODD))
755 new_lcr |= MAX3107_LCR_EVENPARITY_BIT;
756 }
757
758 /* Stop bits */
759 if (termios->c_cflag & CSTOPB) {
760 /* 2 stop bits */
761 new_lcr |= MAX3107_LCR_STOPLEN_BIT;
762 }
763
764 /* Mask termios capabilities we don't support */
765 termios->c_cflag &= ~CMSPAR;
766
767 /* Set status ignore mask */
768 s->port.ignore_status_mask = 0;
769 if (termios->c_iflag & IGNPAR)
770 s->port.ignore_status_mask |= MAX3107_ALL_ERRORS;
771
772 /* Set low latency to immediately handle pushed data */
773 s->port.state->port.tty->low_latency = 1;
774
775 /* Get new baud rate generator configuration */
776 baud = tty_get_baud_rate(tty);
777
778 spin_lock_irqsave(&s->data_lock, flags);
779 new_brg = get_new_brg(baud, s);
780 /* if can't find the corrent config, use previous */
781 if (!new_brg) {
782 baud = s->baud;
783 new_brg = s->brg_cfg;
784 }
785 spin_unlock_irqrestore(&s->data_lock, flags);
786 tty_termios_encode_baud_rate(termios, baud, baud);
787 s->baud = baud;
788
789 /* Update timeout according to new baud rate */
790 uart_update_timeout(port, termios->c_cflag, baud);
791
792 spin_lock_irqsave(&s->data_lock, flags);
793 if (s->lcr_reg != new_lcr) {
794 s->lcr_reg = new_lcr;
795 s->lcr_commit = 1;
796 }
797 if (s->brg_cfg != new_brg) {
798 s->brg_cfg = new_brg;
799 s->brg_commit = 1;
800 }
801 spin_unlock_irqrestore(&s->data_lock, flags);
802
803 /* Trigger work thread for doing the actual configuration change */
804 max3107_dowork(s);
805}
806
807/* Port shutdown function */
808static void max3107_shutdown(struct uart_port *port)
809{
810 struct max3107_port *s = container_of(port, struct max3107_port, port);
811
812 if (s->suspended && s->pdata->hw_suspend)
813 s->pdata->hw_suspend(s, 0);
814
815 /* Free the interrupt */
816 free_irq(s->spi->irq, s);
817
818 if (s->workqueue) {
819 /* Flush and destroy work queue */
820 flush_workqueue(s->workqueue);
821 destroy_workqueue(s->workqueue);
822 s->workqueue = NULL;
823 }
824
825 /* Suspend HW */
826 if (s->pdata->hw_suspend)
827 s->pdata->hw_suspend(s, 1);
828}
829
830/* Port startup function */
831static int max3107_startup(struct uart_port *port)
832{
833 struct max3107_port *s = container_of(port, struct max3107_port, port);
834
835 /* Initialize work queue */
836 s->workqueue = create_freezeable_workqueue("max3107");
837 if (!s->workqueue) {
838 dev_err(&s->spi->dev, "Workqueue creation failed\n");
839 return -EBUSY;
840 }
841 INIT_WORK(&s->work, max3107_work);
842
843 /* Setup IRQ */
844 if (request_irq(s->spi->irq, max3107_irq, IRQF_TRIGGER_FALLING,
845 "max3107", s)) {
846 dev_err(&s->spi->dev, "IRQ reguest failed\n");
847 destroy_workqueue(s->workqueue);
848 s->workqueue = NULL;
849 return -EBUSY;
850 }
851
852 /* Resume HW */
853 if (s->pdata->hw_suspend)
854 s->pdata->hw_suspend(s, 0);
855
856 /* Init registers */
857 max3107_register_init(s);
858
859 return 0;
860}
861
862/* Port type function */
863static const char *max3107_type(struct uart_port *port)
864{
865 struct max3107_port *s = container_of(port, struct max3107_port, port);
866 return s->spi->modalias;
867}
868
869/* Port release function */
870static void max3107_release_port(struct uart_port *port)
871{
872 /* Do nothing */
873}
874
875/* Port request function */
876static int max3107_request_port(struct uart_port *port)
877{
878 /* Do nothing */
879 return 0;
880}
881
882/* Port config function */
883static void max3107_config_port(struct uart_port *port, int flags)
884{
885 struct max3107_port *s = container_of(port, struct max3107_port, port);
886 s->port.type = PORT_MAX3107;
887}
888
889/* Port verify function */
890static int max3107_verify_port(struct uart_port *port,
891 struct serial_struct *ser)
892{
893 if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3107)
894 return 0;
895
896 return -EINVAL;
897}
898
899/* Port stop TX function */
900static void max3107_stop_tx(struct uart_port *port)
901{
902 /* Do nothing */
903}
904
905/* Port break control function */
906static void max3107_break_ctl(struct uart_port *port, int break_state)
907{
908 /* We don't support break control, do nothing */
909}
910
911
912/* Port functions */
913static struct uart_ops max3107_ops = {
914 .tx_empty = max3107_tx_empty,
915 .set_mctrl = max3107_set_mctrl,
916 .get_mctrl = max3107_get_mctrl,
917 .stop_tx = max3107_stop_tx,
918 .start_tx = max3107_start_tx,
919 .stop_rx = max3107_stop_rx,
920 .enable_ms = max3107_enable_ms,
921 .break_ctl = max3107_break_ctl,
922 .startup = max3107_startup,
923 .shutdown = max3107_shutdown,
924 .set_termios = max3107_set_termios,
925 .type = max3107_type,
926 .release_port = max3107_release_port,
927 .request_port = max3107_request_port,
928 .config_port = max3107_config_port,
929 .verify_port = max3107_verify_port,
930};
931
932/* UART driver data */
933static struct uart_driver max3107_uart_driver = {
934 .owner = THIS_MODULE,
935 .driver_name = "ttyMAX",
936 .dev_name = "ttyMAX",
937 .nr = 1,
938};
939
940static int driver_registered = 0;
941
942
943
944/* 'Generic' platform data */
945static struct max3107_plat generic_plat_data = {
946 .loopback = 0,
947 .ext_clk = 1,
948 .hw_suspend = max3107_hw_susp,
949 .polled_mode = 0,
950 .poll_time = 0,
951};
952
953
954/*******************************************************************/
955
956/**
957 * max3107_probe - SPI bus probe entry point
958 * @spi: the spi device
959 *
960 * SPI wants us to probe this device and if appropriate claim it.
961 * Perform any platform specific requirements and then initialise
962 * the device.
963 */
964
965int max3107_probe(struct spi_device *spi, struct max3107_plat *pdata)
966{
967 struct max3107_port *s;
968 u16 buf[2]; /* Buffer for SPI transfers */
969 int retval;
970
971 pr_info("enter max3107 probe\n");
972
973 /* Allocate port structure */
974 s = kzalloc(sizeof(*s), GFP_KERNEL);
975 if (!s) {
976 pr_err("Allocating port structure failed\n");
977 return -ENOMEM;
978 }
979
980 s->pdata = pdata;
981
982 /* SPI Rx buffer
983 * +2 for RX FIFO interrupt
984 * disabling and RX level query
985 */
986 s->rxbuf = kzalloc(sizeof(u16) * (MAX3107_RX_FIFO_SIZE+2), GFP_KERNEL);
987 if (!s->rxbuf) {
988 pr_err("Allocating RX buffer failed\n");
989 return -ENOMEM;
990 }
991 s->rxstr = kzalloc(sizeof(u8) * MAX3107_RX_FIFO_SIZE, GFP_KERNEL);
992 if (!s->rxstr) {
993 pr_err("Allocating RX buffer failed\n");
994 return -ENOMEM;
995 }
996 /* SPI Tx buffer
997 * SPI transfer buffer
998 * +3 for TX FIFO empty
999 * interrupt disabling and
1000 * enabling and TX enabling
1001 */
1002 s->txbuf = kzalloc(sizeof(u16) * MAX3107_TX_FIFO_SIZE + 3, GFP_KERNEL);
1003 if (!s->txbuf) {
1004 pr_err("Allocating TX buffer failed\n");
1005 return -ENOMEM;
1006 }
1007 /* Initialize shared data lock */
1008 spin_lock_init(&s->data_lock);
1009
1010 /* SPI intializations */
1011 dev_set_drvdata(&spi->dev, s);
1012 spi->mode = SPI_MODE_0;
1013 spi->dev.platform_data = pdata;
1014 spi->bits_per_word = 16;
1015 s->ext_clk = pdata->ext_clk;
1016 s->loopback = pdata->loopback;
1017 spi_setup(spi);
1018 s->spi = spi;
1019
1020 /* Check REV ID to ensure we are talking to what we expect */
1021 buf[0] = MAX3107_REVID_REG;
1022 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
1023 dev_err(&s->spi->dev, "SPI transfer for REVID read failed\n");
1024 return -EIO;
1025 }
1026 if ((buf[0] & MAX3107_SPI_RX_DATA_MASK) != MAX3107_REVID1 &&
1027 (buf[0] & MAX3107_SPI_RX_DATA_MASK) != MAX3107_REVID2) {
1028 dev_err(&s->spi->dev, "REVID %x does not match\n",
1029 (buf[0] & MAX3107_SPI_RX_DATA_MASK));
1030 return -ENODEV;
1031 }
1032
1033 /* Disable all interrupts */
1034 buf[0] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG | 0x0000);
1035 buf[0] |= 0x0000;
1036
1037 /* Configure clock source */
1038 buf[1] = (MAX3107_WRITE_BIT | MAX3107_CLKSRC_REG);
1039 if (s->ext_clk) {
1040 /* External clock */
1041 buf[1] |= MAX3107_CLKSRC_EXTCLK_BIT;
1042 }
1043
1044 /* PLL bypass ON */
1045 buf[1] |= MAX3107_CLKSRC_PLLBYP_BIT;
1046
1047 /* Perform SPI transfer */
1048 if (max3107_rw(s, (u8 *)buf, NULL, 4)) {
1049 dev_err(&s->spi->dev, "SPI transfer for init failed\n");
1050 return -EIO;
1051 }
1052
1053 /* Register UART driver */
1054 if (!driver_registered) {
1055 retval = uart_register_driver(&max3107_uart_driver);
1056 if (retval) {
1057 dev_err(&s->spi->dev, "Registering UART driver failed\n");
1058 return retval;
1059 }
1060 driver_registered = 1;
1061 }
1062
1063 /* Initialize UART port data */
1064 s->port.fifosize = 128;
1065 s->port.ops = &max3107_ops;
1066 s->port.line = 0;
1067 s->port.dev = &spi->dev;
1068 s->port.uartclk = 9600;
1069 s->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
1070 s->port.irq = s->spi->irq;
1071 s->port.type = PORT_MAX3107;
1072
1073 /* Add UART port */
1074 retval = uart_add_one_port(&max3107_uart_driver, &s->port);
1075 if (retval < 0) {
1076 dev_err(&s->spi->dev, "Adding UART port failed\n");
1077 return retval;
1078 }
1079
1080 if (pdata->configure) {
1081 retval = pdata->configure(s);
1082 if (retval < 0)
1083 return retval;
1084 }
1085
1086 /* Go to suspend mode */
1087 if (pdata->hw_suspend)
1088 pdata->hw_suspend(s, 1);
1089
1090 return 0;
1091}
1092EXPORT_SYMBOL_GPL(max3107_probe);
1093
1094/* Driver remove function */
1095int max3107_remove(struct spi_device *spi)
1096{
1097 struct max3107_port *s = dev_get_drvdata(&spi->dev);
1098
1099 pr_info("enter max3107 remove\n");
1100
1101 /* Remove port */
1102 if (uart_remove_one_port(&max3107_uart_driver, &s->port))
1103 dev_warn(&s->spi->dev, "Removing UART port failed\n");
1104
1105
1106 /* Free TxRx buffer */
1107 kfree(s->rxbuf);
1108 kfree(s->rxstr);
1109 kfree(s->txbuf);
1110
1111 /* Free port structure */
1112 kfree(s);
1113
1114 return 0;
1115}
1116EXPORT_SYMBOL_GPL(max3107_remove);
1117
1118/* Driver suspend function */
1119int max3107_suspend(struct spi_device *spi, pm_message_t state)
1120{
1121#ifdef CONFIG_PM
1122 struct max3107_port *s = dev_get_drvdata(&spi->dev);
1123
1124 pr_debug("enter suspend\n");
1125
1126 /* Suspend UART port */
1127 uart_suspend_port(&max3107_uart_driver, &s->port);
1128
1129 /* Go to suspend mode */
1130 if (s->pdata->hw_suspend)
1131 s->pdata->hw_suspend(s, 1);
1132#endif /* CONFIG_PM */
1133 return 0;
1134}
1135EXPORT_SYMBOL_GPL(max3107_suspend);
1136
1137/* Driver resume function */
1138int max3107_resume(struct spi_device *spi)
1139{
1140#ifdef CONFIG_PM
1141 struct max3107_port *s = dev_get_drvdata(&spi->dev);
1142
1143 pr_debug("enter resume\n");
1144
1145 /* Resume from suspend */
1146 if (s->pdata->hw_suspend)
1147 s->pdata->hw_suspend(s, 0);
1148
1149 /* Resume UART port */
1150 uart_resume_port(&max3107_uart_driver, &s->port);
1151#endif /* CONFIG_PM */
1152 return 0;
1153}
1154EXPORT_SYMBOL_GPL(max3107_resume);
1155
1156static int max3107_probe_generic(struct spi_device *spi)
1157{
1158 return max3107_probe(spi, &generic_plat_data);
1159}
1160
1161/* Spi driver data */
1162static struct spi_driver max3107_driver = {
1163 .driver = {
1164 .name = "max3107",
1165 .bus = &spi_bus_type,
1166 .owner = THIS_MODULE,
1167 },
1168 .probe = max3107_probe_generic,
1169 .remove = __devexit_p(max3107_remove),
1170 .suspend = max3107_suspend,
1171 .resume = max3107_resume,
1172};
1173
1174/* Driver init function */
1175static int __init max3107_init(void)
1176{
1177 pr_info("enter max3107 init\n");
1178 return spi_register_driver(&max3107_driver);
1179}
1180
1181/* Driver exit function */
1182static void __exit max3107_exit(void)
1183{
1184 pr_info("enter max3107 exit\n");
1185 /* Unregister UART driver */
1186 if (driver_registered)
1187 uart_unregister_driver(&max3107_uart_driver);
1188 spi_unregister_driver(&max3107_driver);
1189}
1190
1191module_init(max3107_init);
1192module_exit(max3107_exit);
1193
1194MODULE_DESCRIPTION("MAX3107 driver");
1195MODULE_AUTHOR("Aavamobile");
1196MODULE_ALIAS("max3107-spi");
1197MODULE_LICENSE("GPL v2");
diff --git a/drivers/serial/max3107.h b/drivers/serial/max3107.h
new file mode 100644
index 00000000000..7ab63239250
--- /dev/null
+++ b/drivers/serial/max3107.h
@@ -0,0 +1,441 @@
1/*
2 * max3107.h - spi uart protocol driver header for Maxim 3107
3 *
4 * Copyright (C) Aavamobile 2009
5 * Based on serial_max3100.h by Christian Pellegrin
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
13#ifndef _MAX3107_H
14#define _MAX3107_H
15
16/* Serial error status definitions */
17#define MAX3107_PARITY_ERROR 1
18#define MAX3107_FRAME_ERROR 2
19#define MAX3107_OVERRUN_ERROR 4
20#define MAX3107_ALL_ERRORS (MAX3107_PARITY_ERROR | \
21 MAX3107_FRAME_ERROR | \
22 MAX3107_OVERRUN_ERROR)
23
24/* GPIO definitions */
25#define MAX3107_GPIO_BASE 88
26#define MAX3107_GPIO_COUNT 4
27
28
29/* GPIO connected to chip's reset pin */
30#define MAX3107_RESET_GPIO 87
31
32
33/* Chip reset delay */
34#define MAX3107_RESET_DELAY 10
35
36/* Chip wakeup delay */
37#define MAX3107_WAKEUP_DELAY 50
38
39
40/* Sleep mode definitions */
41#define MAX3107_DISABLE_FORCED_SLEEP 0
42#define MAX3107_ENABLE_FORCED_SLEEP 1
43#define MAX3107_DISABLE_AUTOSLEEP 2
44#define MAX3107_ENABLE_AUTOSLEEP 3
45
46
47/* Definitions for register access with SPI transfers
48 *
49 * SPI transfer format:
50 *
51 * Master to slave bits xzzzzzzzyyyyyyyy
52 * Slave to master bits aaaaaaaabbbbbbbb
53 *
54 * where:
55 * x = 0 for reads, 1 for writes
56 * z = register address
57 * y = new register value if write, 0 if read
58 * a = unspecified
59 * b = register value if read, unspecified if write
60 */
61
62/* SPI speed */
63#define MAX3107_SPI_SPEED (3125000 * 2)
64
65/* Write bit */
66#define MAX3107_WRITE_BIT (1 << 15)
67
68/* SPI TX data mask */
69#define MAX3107_SPI_RX_DATA_MASK (0x00ff)
70
71/* SPI RX data mask */
72#define MAX3107_SPI_TX_DATA_MASK (0x00ff)
73
74/* Register access masks */
75#define MAX3107_RHR_REG (0x0000) /* RX FIFO */
76#define MAX3107_THR_REG (0x0000) /* TX FIFO */
77#define MAX3107_IRQEN_REG (0x0100) /* IRQ enable */
78#define MAX3107_IRQSTS_REG (0x0200) /* IRQ status */
79#define MAX3107_LSR_IRQEN_REG (0x0300) /* LSR IRQ enable */
80#define MAX3107_LSR_IRQSTS_REG (0x0400) /* LSR IRQ status */
81#define MAX3107_SPCHR_IRQEN_REG (0x0500) /* Special char IRQ enable */
82#define MAX3107_SPCHR_IRQSTS_REG (0x0600) /* Special char IRQ status */
83#define MAX3107_STS_IRQEN_REG (0x0700) /* Status IRQ enable */
84#define MAX3107_STS_IRQSTS_REG (0x0800) /* Status IRQ status */
85#define MAX3107_MODE1_REG (0x0900) /* MODE1 */
86#define MAX3107_MODE2_REG (0x0a00) /* MODE2 */
87#define MAX3107_LCR_REG (0x0b00) /* LCR */
88#define MAX3107_RXTO_REG (0x0c00) /* RX timeout */
89#define MAX3107_HDPIXDELAY_REG (0x0d00) /* Auto transceiver delays */
90#define MAX3107_IRDA_REG (0x0e00) /* IRDA settings */
91#define MAX3107_FLOWLVL_REG (0x0f00) /* Flow control levels */
92#define MAX3107_FIFOTRIGLVL_REG (0x1000) /* FIFO IRQ trigger levels */
93#define MAX3107_TXFIFOLVL_REG (0x1100) /* TX FIFO level */
94#define MAX3107_RXFIFOLVL_REG (0x1200) /* RX FIFO level */
95#define MAX3107_FLOWCTRL_REG (0x1300) /* Flow control */
96#define MAX3107_XON1_REG (0x1400) /* XON1 character */
97#define MAX3107_XON2_REG (0x1500) /* XON2 character */
98#define MAX3107_XOFF1_REG (0x1600) /* XOFF1 character */
99#define MAX3107_XOFF2_REG (0x1700) /* XOFF2 character */
100#define MAX3107_GPIOCFG_REG (0x1800) /* GPIO config */
101#define MAX3107_GPIODATA_REG (0x1900) /* GPIO data */
102#define MAX3107_PLLCFG_REG (0x1a00) /* PLL config */
103#define MAX3107_BRGCFG_REG (0x1b00) /* Baud rate generator conf */
104#define MAX3107_BRGDIVLSB_REG (0x1c00) /* Baud rate divisor LSB */
105#define MAX3107_BRGDIVMSB_REG (0x1d00) /* Baud rate divisor MSB */
106#define MAX3107_CLKSRC_REG (0x1e00) /* Clock source */
107#define MAX3107_REVID_REG (0x1f00) /* Revision identification */
108
109/* IRQ register bits */
110#define MAX3107_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */
111#define MAX3107_IRQ_SPCHR_BIT (1 << 1) /* Special char interrupt */
112#define MAX3107_IRQ_STS_BIT (1 << 2) /* Status interrupt */
113#define MAX3107_IRQ_RXFIFO_BIT (1 << 3) /* RX FIFO interrupt */
114#define MAX3107_IRQ_TXFIFO_BIT (1 << 4) /* TX FIFO interrupt */
115#define MAX3107_IRQ_TXEMPTY_BIT (1 << 5) /* TX FIFO empty interrupt */
116#define MAX3107_IRQ_RXEMPTY_BIT (1 << 6) /* RX FIFO empty interrupt */
117#define MAX3107_IRQ_CTS_BIT (1 << 7) /* CTS interrupt */
118
119/* LSR register bits */
120#define MAX3107_LSR_RXTO_BIT (1 << 0) /* RX timeout */
121#define MAX3107_LSR_RXOVR_BIT (1 << 1) /* RX overrun */
122#define MAX3107_LSR_RXPAR_BIT (1 << 2) /* RX parity error */
123#define MAX3107_LSR_FRERR_BIT (1 << 3) /* Frame error */
124#define MAX3107_LSR_RXBRK_BIT (1 << 4) /* RX break */
125#define MAX3107_LSR_RXNOISE_BIT (1 << 5) /* RX noise */
126#define MAX3107_LSR_UNDEF6_BIT (1 << 6) /* Undefined/not used */
127#define MAX3107_LSR_CTS_BIT (1 << 7) /* CTS pin state */
128
129/* Special character register bits */
130#define MAX3107_SPCHR_XON1_BIT (1 << 0) /* XON1 character */
131#define MAX3107_SPCHR_XON2_BIT (1 << 1) /* XON2 character */
132#define MAX3107_SPCHR_XOFF1_BIT (1 << 2) /* XOFF1 character */
133#define MAX3107_SPCHR_XOFF2_BIT (1 << 3) /* XOFF2 character */
134#define MAX3107_SPCHR_BREAK_BIT (1 << 4) /* RX break */
135#define MAX3107_SPCHR_MULTIDROP_BIT (1 << 5) /* 9-bit multidrop addr char */
136#define MAX3107_SPCHR_UNDEF6_BIT (1 << 6) /* Undefined/not used */
137#define MAX3107_SPCHR_UNDEF7_BIT (1 << 7) /* Undefined/not used */
138
139/* Status register bits */
140#define MAX3107_STS_GPIO0_BIT (1 << 0) /* GPIO 0 interrupt */
141#define MAX3107_STS_GPIO1_BIT (1 << 1) /* GPIO 1 interrupt */
142#define MAX3107_STS_GPIO2_BIT (1 << 2) /* GPIO 2 interrupt */
143#define MAX3107_STS_GPIO3_BIT (1 << 3) /* GPIO 3 interrupt */
144#define MAX3107_STS_UNDEF4_BIT (1 << 4) /* Undefined/not used */
145#define MAX3107_STS_CLKREADY_BIT (1 << 5) /* Clock ready */
146#define MAX3107_STS_SLEEP_BIT (1 << 6) /* Sleep interrupt */
147#define MAX3107_STS_UNDEF7_BIT (1 << 7) /* Undefined/not used */
148
149/* MODE1 register bits */
150#define MAX3107_MODE1_RXDIS_BIT (1 << 0) /* RX disable */
151#define MAX3107_MODE1_TXDIS_BIT (1 << 1) /* TX disable */
152#define MAX3107_MODE1_TXHIZ_BIT (1 << 2) /* TX pin three-state */
153#define MAX3107_MODE1_RTSHIZ_BIT (1 << 3) /* RTS pin three-state */
154#define MAX3107_MODE1_TRNSCVCTRL_BIT (1 << 4) /* Transceiver ctrl enable */
155#define MAX3107_MODE1_FORCESLEEP_BIT (1 << 5) /* Force sleep mode */
156#define MAX3107_MODE1_AUTOSLEEP_BIT (1 << 6) /* Auto sleep enable */
157#define MAX3107_MODE1_IRQSEL_BIT (1 << 7) /* IRQ pin enable */
158
159/* MODE2 register bits */
160#define MAX3107_MODE2_RST_BIT (1 << 0) /* Chip reset */
161#define MAX3107_MODE2_FIFORST_BIT (1 << 1) /* FIFO reset */
162#define MAX3107_MODE2_RXTRIGINV_BIT (1 << 2) /* RX FIFO INT invert */
163#define MAX3107_MODE2_RXEMPTINV_BIT (1 << 3) /* RX FIFO empty INT invert */
164#define MAX3107_MODE2_SPCHR_BIT (1 << 4) /* Special chr detect enable */
165#define MAX3107_MODE2_LOOPBACK_BIT (1 << 5) /* Internal loopback enable */
166#define MAX3107_MODE2_MULTIDROP_BIT (1 << 6) /* 9-bit multidrop enable */
167#define MAX3107_MODE2_ECHOSUPR_BIT (1 << 7) /* ECHO suppression enable */
168
169/* LCR register bits */
170#define MAX3107_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */
171#define MAX3107_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1
172 *
173 * Word length bits table:
174 * 00 -> 5 bit words
175 * 01 -> 6 bit words
176 * 10 -> 7 bit words
177 * 11 -> 8 bit words
178 */
179#define MAX3107_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit
180 *
181 * STOP length bit table:
182 * 0 -> 1 stop bit
183 * 1 -> 1-1.5 stop bits if
184 * word length is 5,
185 * 2 stop bits otherwise
186 */
187#define MAX3107_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */
188#define MAX3107_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */
189#define MAX3107_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */
190#define MAX3107_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */
191#define MAX3107_LCR_RTS_BIT (1 << 7) /* RTS pin control */
192#define MAX3107_LCR_WORD_LEN_5 (0x0000)
193#define MAX3107_LCR_WORD_LEN_6 (0x0001)
194#define MAX3107_LCR_WORD_LEN_7 (0x0002)
195#define MAX3107_LCR_WORD_LEN_8 (0x0003)
196
197
198/* IRDA register bits */
199#define MAX3107_IRDA_IRDAEN_BIT (1 << 0) /* IRDA mode enable */
200#define MAX3107_IRDA_SIR_BIT (1 << 1) /* SIR mode enable */
201#define MAX3107_IRDA_SHORTIR_BIT (1 << 2) /* Short SIR mode enable */
202#define MAX3107_IRDA_MIR_BIT (1 << 3) /* MIR mode enable */
203#define MAX3107_IRDA_RXINV_BIT (1 << 4) /* RX logic inversion enable */
204#define MAX3107_IRDA_TXINV_BIT (1 << 5) /* TX logic inversion enable */
205#define MAX3107_IRDA_UNDEF6_BIT (1 << 6) /* Undefined/not used */
206#define MAX3107_IRDA_UNDEF7_BIT (1 << 7) /* Undefined/not used */
207
208/* Flow control trigger level register masks */
209#define MAX3107_FLOWLVL_HALT_MASK (0x000f) /* Flow control halt level */
210#define MAX3107_FLOWLVL_RES_MASK (0x00f0) /* Flow control resume level */
211#define MAX3107_FLOWLVL_HALT(words) ((words/8) & 0x000f)
212#define MAX3107_FLOWLVL_RES(words) (((words/8) & 0x000f) << 4)
213
214/* FIFO interrupt trigger level register masks */
215#define MAX3107_FIFOTRIGLVL_TX_MASK (0x000f) /* TX FIFO trigger level */
216#define MAX3107_FIFOTRIGLVL_RX_MASK (0x00f0) /* RX FIFO trigger level */
217#define MAX3107_FIFOTRIGLVL_TX(words) ((words/8) & 0x000f)
218#define MAX3107_FIFOTRIGLVL_RX(words) (((words/8) & 0x000f) << 4)
219
220/* Flow control register bits */
221#define MAX3107_FLOWCTRL_AUTORTS_BIT (1 << 0) /* Auto RTS flow ctrl enable */
222#define MAX3107_FLOWCTRL_AUTOCTS_BIT (1 << 1) /* Auto CTS flow ctrl enable */
223#define MAX3107_FLOWCTRL_GPIADDR_BIT (1 << 2) /* Enables that GPIO inputs
224 * are used in conjunction with
225 * XOFF2 for definition of
226 * special character */
227#define MAX3107_FLOWCTRL_SWFLOWEN_BIT (1 << 3) /* Auto SW flow ctrl enable */
228#define MAX3107_FLOWCTRL_SWFLOW0_BIT (1 << 4) /* SWFLOW bit 0 */
229#define MAX3107_FLOWCTRL_SWFLOW1_BIT (1 << 5) /* SWFLOW bit 1
230 *
231 * SWFLOW bits 1 & 0 table:
232 * 00 -> no transmitter flow
233 * control
234 * 01 -> receiver compares
235 * XON2 and XOFF2
236 * and controls
237 * transmitter
238 * 10 -> receiver compares
239 * XON1 and XOFF1
240 * and controls
241 * transmitter
242 * 11 -> receiver compares
243 * XON1, XON2, XOFF1 and
244 * XOFF2 and controls
245 * transmitter
246 */
247#define MAX3107_FLOWCTRL_SWFLOW2_BIT (1 << 6) /* SWFLOW bit 2 */
248#define MAX3107_FLOWCTRL_SWFLOW3_BIT (1 << 7) /* SWFLOW bit 3
249 *
250 * SWFLOW bits 3 & 2 table:
251 * 00 -> no received flow
252 * control
253 * 01 -> transmitter generates
254 * XON2 and XOFF2
255 * 10 -> transmitter generates
256 * XON1 and XOFF1
257 * 11 -> transmitter generates
258 * XON1, XON2, XOFF1 and
259 * XOFF2
260 */
261
262/* GPIO configuration register bits */
263#define MAX3107_GPIOCFG_GP0OUT_BIT (1 << 0) /* GPIO 0 output enable */
264#define MAX3107_GPIOCFG_GP1OUT_BIT (1 << 1) /* GPIO 1 output enable */
265#define MAX3107_GPIOCFG_GP2OUT_BIT (1 << 2) /* GPIO 2 output enable */
266#define MAX3107_GPIOCFG_GP3OUT_BIT (1 << 3) /* GPIO 3 output enable */
267#define MAX3107_GPIOCFG_GP0OD_BIT (1 << 4) /* GPIO 0 open-drain enable */
268#define MAX3107_GPIOCFG_GP1OD_BIT (1 << 5) /* GPIO 1 open-drain enable */
269#define MAX3107_GPIOCFG_GP2OD_BIT (1 << 6) /* GPIO 2 open-drain enable */
270#define MAX3107_GPIOCFG_GP3OD_BIT (1 << 7) /* GPIO 3 open-drain enable */
271
272/* GPIO DATA register bits */
273#define MAX3107_GPIODATA_GP0OUT_BIT (1 << 0) /* GPIO 0 output value */
274#define MAX3107_GPIODATA_GP1OUT_BIT (1 << 1) /* GPIO 1 output value */
275#define MAX3107_GPIODATA_GP2OUT_BIT (1 << 2) /* GPIO 2 output value */
276#define MAX3107_GPIODATA_GP3OUT_BIT (1 << 3) /* GPIO 3 output value */
277#define MAX3107_GPIODATA_GP0IN_BIT (1 << 4) /* GPIO 0 input value */
278#define MAX3107_GPIODATA_GP1IN_BIT (1 << 5) /* GPIO 1 input value */
279#define MAX3107_GPIODATA_GP2IN_BIT (1 << 6) /* GPIO 2 input value */
280#define MAX3107_GPIODATA_GP3IN_BIT (1 << 7) /* GPIO 3 input value */
281
282/* PLL configuration register masks */
283#define MAX3107_PLLCFG_PREDIV_MASK (0x003f) /* PLL predivision value */
284#define MAX3107_PLLCFG_PLLFACTOR_MASK (0x00c0) /* PLL multiplication factor */
285
286/* Baud rate generator configuration register masks and bits */
287#define MAX3107_BRGCFG_FRACT_MASK (0x000f) /* Fractional portion of
288 * Baud rate generator divisor
289 */
290#define MAX3107_BRGCFG_2XMODE_BIT (1 << 4) /* Double baud rate */
291#define MAX3107_BRGCFG_4XMODE_BIT (1 << 5) /* Quadruple baud rate */
292#define MAX3107_BRGCFG_UNDEF6_BIT (1 << 6) /* Undefined/not used */
293#define MAX3107_BRGCFG_UNDEF7_BIT (1 << 7) /* Undefined/not used */
294
295/* Clock source register bits */
296#define MAX3107_CLKSRC_INTOSC_BIT (1 << 0) /* Internal osc enable */
297#define MAX3107_CLKSRC_CRYST_BIT (1 << 1) /* Crystal osc enable */
298#define MAX3107_CLKSRC_PLL_BIT (1 << 2) /* PLL enable */
299#define MAX3107_CLKSRC_PLLBYP_BIT (1 << 3) /* PLL bypass */
300#define MAX3107_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */
301#define MAX3107_CLKSRC_UNDEF5_BIT (1 << 5) /* Undefined/not used */
302#define MAX3107_CLKSRC_UNDEF6_BIT (1 << 6) /* Undefined/not used */
303#define MAX3107_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */
304
305
306/* HW definitions */
307#define MAX3107_RX_FIFO_SIZE 128
308#define MAX3107_TX_FIFO_SIZE 128
309#define MAX3107_REVID1 0x00a0
310#define MAX3107_REVID2 0x00a1
311
312
313/* Baud rate generator configuration values for external clock 13MHz */
314#define MAX3107_BRG13_B300 (0x0A9400 | 0x05)
315#define MAX3107_BRG13_B600 (0x054A00 | 0x03)
316#define MAX3107_BRG13_B1200 (0x02A500 | 0x01)
317#define MAX3107_BRG13_B2400 (0x015200 | 0x09)
318#define MAX3107_BRG13_B4800 (0x00A900 | 0x04)
319#define MAX3107_BRG13_B9600 (0x005400 | 0x0A)
320#define MAX3107_BRG13_B19200 (0x002A00 | 0x05)
321#define MAX3107_BRG13_B38400 (0x001500 | 0x03)
322#define MAX3107_BRG13_B57600 (0x000E00 | 0x02)
323#define MAX3107_BRG13_B115200 (0x000700 | 0x01)
324#define MAX3107_BRG13_B230400 (0x000300 | 0x08)
325#define MAX3107_BRG13_B460800 (0x000100 | 0x0c)
326#define MAX3107_BRG13_B921600 (0x000100 | 0x1c)
327
328/* Baud rate generator configuration values for external clock 26MHz */
329#define MAX3107_BRG26_B300 (0x152800 | 0x0A)
330#define MAX3107_BRG26_B600 (0x0A9400 | 0x05)
331#define MAX3107_BRG26_B1200 (0x054A00 | 0x03)
332#define MAX3107_BRG26_B2400 (0x02A500 | 0x01)
333#define MAX3107_BRG26_B4800 (0x015200 | 0x09)
334#define MAX3107_BRG26_B9600 (0x00A900 | 0x04)
335#define MAX3107_BRG26_B19200 (0x005400 | 0x0A)
336#define MAX3107_BRG26_B38400 (0x002A00 | 0x05)
337#define MAX3107_BRG26_B57600 (0x001C00 | 0x03)
338#define MAX3107_BRG26_B115200 (0x000E00 | 0x02)
339#define MAX3107_BRG26_B230400 (0x000700 | 0x01)
340#define MAX3107_BRG26_B460800 (0x000300 | 0x08)
341#define MAX3107_BRG26_B921600 (0x000100 | 0x0C)
342
343/* Baud rate generator configuration values for internal clock */
344#define MAX3107_BRG13_IB300 (0x008000 | 0x00)
345#define MAX3107_BRG13_IB600 (0x004000 | 0x00)
346#define MAX3107_BRG13_IB1200 (0x002000 | 0x00)
347#define MAX3107_BRG13_IB2400 (0x001000 | 0x00)
348#define MAX3107_BRG13_IB4800 (0x000800 | 0x00)
349#define MAX3107_BRG13_IB9600 (0x000400 | 0x00)
350#define MAX3107_BRG13_IB19200 (0x000200 | 0x00)
351#define MAX3107_BRG13_IB38400 (0x000100 | 0x00)
352#define MAX3107_BRG13_IB57600 (0x000000 | 0x0B)
353#define MAX3107_BRG13_IB115200 (0x000000 | 0x05)
354#define MAX3107_BRG13_IB230400 (0x000000 | 0x03)
355#define MAX3107_BRG13_IB460800 (0x000000 | 0x00)
356#define MAX3107_BRG13_IB921600 (0x000000 | 0x00)
357
358
359struct baud_table {
360 int baud;
361 u32 new_brg;
362};
363
364struct max3107_port {
365 /* UART port structure */
366 struct uart_port port;
367
368 /* SPI device structure */
369 struct spi_device *spi;
370
371#if defined(CONFIG_GPIOLIB)
372 /* GPIO chip stucture */
373 struct gpio_chip chip;
374#endif
375
376 /* Workqueue that does all the magic */
377 struct workqueue_struct *workqueue;
378 struct work_struct work;
379
380 /* Lock for shared data */
381 spinlock_t data_lock;
382
383 /* Device configuration */
384 int ext_clk; /* 1 if external clock used */
385 int loopback; /* Current loopback mode state */
386 int baud; /* Current baud rate */
387
388 /* State flags */
389 int suspended; /* Indicates suspend mode */
390 int tx_fifo_empty; /* Flag for TX FIFO state */
391 int rx_enabled; /* Flag for receiver state */
392 int tx_enabled; /* Flag for transmitter state */
393
394 u16 irqen_reg; /* Current IRQ enable register value */
395 /* Shared data */
396 u16 mode1_reg; /* Current mode1 register value*/
397 int mode1_commit; /* Flag for setting new mode1 register value */
398 u16 lcr_reg; /* Current LCR register value */
399 int lcr_commit; /* Flag for setting new LCR register value */
400 u32 brg_cfg; /* Current Baud rate generator config */
401 int brg_commit; /* Flag for setting new baud rate generator
402 * config
403 */
404 struct baud_table *baud_tbl;
405 int handle_irq; /* Indicates that IRQ should be handled */
406
407 /* Rx buffer and str*/
408 u16 *rxbuf;
409 u8 *rxstr;
410 /* Tx buffer*/
411 u16 *txbuf;
412
413 struct max3107_plat *pdata; /* Platform data */
414};
415
416/* Platform data structure */
417struct max3107_plat {
418 /* Loopback mode enable */
419 int loopback;
420 /* External clock enable */
421 int ext_clk;
422 /* Called during the register initialisation */
423 void (*init)(struct max3107_port *s);
424 /* Called when the port is found and configured */
425 int (*configure)(struct max3107_port *s);
426 /* HW suspend function */
427 void (*hw_suspend) (struct max3107_port *s, int suspend);
428 /* Polling mode enable */
429 int polled_mode;
430 /* Polling period if polling mode enabled */
431 int poll_time;
432};
433
434extern int max3107_rw(struct max3107_port *s, u8 *tx, u8 *rx, int len);
435extern void max3107_hw_susp(struct max3107_port *s, int suspend);
436extern int max3107_probe(struct spi_device *spi, struct max3107_plat *pdata);
437extern int max3107_remove(struct spi_device *spi);
438extern int max3107_suspend(struct spi_device *spi, pm_message_t state);
439extern int max3107_resume(struct spi_device *spi);
440
441#endif /* _LINUX_SERIAL_MAX3107_H */
diff --git a/drivers/serial/mcf.c b/drivers/serial/mcf.c
index b5aaef965f2..3394b7cc172 100644
--- a/drivers/serial/mcf.c
+++ b/drivers/serial/mcf.c
@@ -70,16 +70,14 @@ static unsigned int mcf_tx_empty(struct uart_port *port)
70static unsigned int mcf_get_mctrl(struct uart_port *port) 70static unsigned int mcf_get_mctrl(struct uart_port *port)
71{ 71{
72 struct mcf_uart *pp = container_of(port, struct mcf_uart, port); 72 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
73 unsigned long flags;
74 unsigned int sigs; 73 unsigned int sigs;
75 74
76 spin_lock_irqsave(&port->lock, flags);
77 sigs = (readb(port->membase + MCFUART_UIPR) & MCFUART_UIPR_CTS) ? 75 sigs = (readb(port->membase + MCFUART_UIPR) & MCFUART_UIPR_CTS) ?
78 0 : TIOCM_CTS; 76 0 : TIOCM_CTS;
79 sigs |= (pp->sigs & TIOCM_RTS); 77 sigs |= (pp->sigs & TIOCM_RTS);
80 sigs |= (mcf_getppdcd(port->line) ? TIOCM_CD : 0); 78 sigs |= (mcf_getppdcd(port->line) ? TIOCM_CD : 0);
81 sigs |= (mcf_getppdtr(port->line) ? TIOCM_DTR : 0); 79 sigs |= (mcf_getppdtr(port->line) ? TIOCM_DTR : 0);
82 spin_unlock_irqrestore(&port->lock, flags); 80
83 return sigs; 81 return sigs;
84} 82}
85 83
@@ -88,16 +86,13 @@ static unsigned int mcf_get_mctrl(struct uart_port *port)
88static void mcf_set_mctrl(struct uart_port *port, unsigned int sigs) 86static void mcf_set_mctrl(struct uart_port *port, unsigned int sigs)
89{ 87{
90 struct mcf_uart *pp = container_of(port, struct mcf_uart, port); 88 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
91 unsigned long flags;
92 89
93 spin_lock_irqsave(&port->lock, flags);
94 pp->sigs = sigs; 90 pp->sigs = sigs;
95 mcf_setppdtr(port->line, (sigs & TIOCM_DTR)); 91 mcf_setppdtr(port->line, (sigs & TIOCM_DTR));
96 if (sigs & TIOCM_RTS) 92 if (sigs & TIOCM_RTS)
97 writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP1); 93 writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP1);
98 else 94 else
99 writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP0); 95 writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP0);
100 spin_unlock_irqrestore(&port->lock, flags);
101} 96}
102 97
103/****************************************************************************/ 98/****************************************************************************/
@@ -105,12 +100,9 @@ static void mcf_set_mctrl(struct uart_port *port, unsigned int sigs)
105static void mcf_start_tx(struct uart_port *port) 100static void mcf_start_tx(struct uart_port *port)
106{ 101{
107 struct mcf_uart *pp = container_of(port, struct mcf_uart, port); 102 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
108 unsigned long flags;
109 103
110 spin_lock_irqsave(&port->lock, flags);
111 pp->imr |= MCFUART_UIR_TXREADY; 104 pp->imr |= MCFUART_UIR_TXREADY;
112 writeb(pp->imr, port->membase + MCFUART_UIMR); 105 writeb(pp->imr, port->membase + MCFUART_UIMR);
113 spin_unlock_irqrestore(&port->lock, flags);
114} 106}
115 107
116/****************************************************************************/ 108/****************************************************************************/
@@ -118,12 +110,9 @@ static void mcf_start_tx(struct uart_port *port)
118static void mcf_stop_tx(struct uart_port *port) 110static void mcf_stop_tx(struct uart_port *port)
119{ 111{
120 struct mcf_uart *pp = container_of(port, struct mcf_uart, port); 112 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
121 unsigned long flags;
122 113
123 spin_lock_irqsave(&port->lock, flags);
124 pp->imr &= ~MCFUART_UIR_TXREADY; 114 pp->imr &= ~MCFUART_UIR_TXREADY;
125 writeb(pp->imr, port->membase + MCFUART_UIMR); 115 writeb(pp->imr, port->membase + MCFUART_UIMR);
126 spin_unlock_irqrestore(&port->lock, flags);
127} 116}
128 117
129/****************************************************************************/ 118/****************************************************************************/
@@ -131,12 +120,9 @@ static void mcf_stop_tx(struct uart_port *port)
131static void mcf_stop_rx(struct uart_port *port) 120static void mcf_stop_rx(struct uart_port *port)
132{ 121{
133 struct mcf_uart *pp = container_of(port, struct mcf_uart, port); 122 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
134 unsigned long flags;
135 123
136 spin_lock_irqsave(&port->lock, flags);
137 pp->imr &= ~MCFUART_UIR_RXREADY; 124 pp->imr &= ~MCFUART_UIR_RXREADY;
138 writeb(pp->imr, port->membase + MCFUART_UIMR); 125 writeb(pp->imr, port->membase + MCFUART_UIMR);
139 spin_unlock_irqrestore(&port->lock, flags);
140} 126}
141 127
142/****************************************************************************/ 128/****************************************************************************/
@@ -366,13 +352,22 @@ static irqreturn_t mcf_interrupt(int irq, void *data)
366 struct uart_port *port = data; 352 struct uart_port *port = data;
367 struct mcf_uart *pp = container_of(port, struct mcf_uart, port); 353 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
368 unsigned int isr; 354 unsigned int isr;
355 irqreturn_t ret = IRQ_NONE;
369 356
370 isr = readb(port->membase + MCFUART_UISR) & pp->imr; 357 isr = readb(port->membase + MCFUART_UISR) & pp->imr;
371 if (isr & MCFUART_UIR_RXREADY) 358
359 spin_lock(&port->lock);
360 if (isr & MCFUART_UIR_RXREADY) {
372 mcf_rx_chars(pp); 361 mcf_rx_chars(pp);
373 if (isr & MCFUART_UIR_TXREADY) 362 ret = IRQ_HANDLED;
363 }
364 if (isr & MCFUART_UIR_TXREADY) {
374 mcf_tx_chars(pp); 365 mcf_tx_chars(pp);
375 return IRQ_HANDLED; 366 ret = IRQ_HANDLED;
367 }
368 spin_unlock(&port->lock);
369
370 return ret;
376} 371}
377 372
378/****************************************************************************/ 373/****************************************************************************/
diff --git a/drivers/serial/mfd.c b/drivers/serial/mfd.c
new file mode 100644
index 00000000000..bc9af503907
--- /dev/null
+++ b/drivers/serial/mfd.c
@@ -0,0 +1,1498 @@
1/*
2 * mfd.c: driver for High Speed UART device of Intel Medfield platform
3 *
4 * Refer pxa.c, 8250.c and some other drivers in drivers/serial/
5 *
6 * (C) Copyright 2010 Intel Corporation
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; version 2
11 * of the License.
12 */
13
14/* Notes:
15 * 1. DMA channel allocation: 0/1 channel are assigned to port 0,
16 * 2/3 chan to port 1, 4/5 chan to port 3. Even number chans
17 * are used for RX, odd chans for TX
18 *
19 * 2. In A0 stepping, UART will not support TX half empty flag
20 *
21 * 3. The RI/DSR/DCD/DTR are not pinned out, DCD & DSR are always
22 * asserted, only when the HW is reset the DDCD and DDSR will
23 * be triggered
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/console.h>
29#include <linux/sysrq.h>
30#include <linux/serial_reg.h>
31#include <linux/circ_buf.h>
32#include <linux/delay.h>
33#include <linux/interrupt.h>
34#include <linux/tty.h>
35#include <linux/tty_flip.h>
36#include <linux/serial_core.h>
37#include <linux/serial_mfd.h>
38#include <linux/dma-mapping.h>
39#include <linux/pci.h>
40#include <linux/io.h>
41#include <linux/debugfs.h>
42
43#define MFD_HSU_A0_STEPPING 1
44
45#define HSU_DMA_BUF_SIZE 2048
46
47#define chan_readl(chan, offset) readl(chan->reg + offset)
48#define chan_writel(chan, offset, val) writel(val, chan->reg + offset)
49
50#define mfd_readl(obj, offset) readl(obj->reg + offset)
51#define mfd_writel(obj, offset, val) writel(val, obj->reg + offset)
52
53#define HSU_DMA_TIMEOUT_CHECK_FREQ (HZ/10)
54
55struct hsu_dma_buffer {
56 u8 *buf;
57 dma_addr_t dma_addr;
58 u32 dma_size;
59 u32 ofs;
60};
61
62struct hsu_dma_chan {
63 u32 id;
64 enum dma_data_direction dirt;
65 struct uart_hsu_port *uport;
66 void __iomem *reg;
67 struct timer_list rx_timer; /* only needed by RX channel */
68};
69
70struct uart_hsu_port {
71 struct uart_port port;
72 unsigned char ier;
73 unsigned char lcr;
74 unsigned char mcr;
75 unsigned int lsr_break_flag;
76 char name[12];
77 int index;
78 struct device *dev;
79
80 struct hsu_dma_chan *txc;
81 struct hsu_dma_chan *rxc;
82 struct hsu_dma_buffer txbuf;
83 struct hsu_dma_buffer rxbuf;
84 int use_dma; /* flag for DMA/PIO */
85 int running;
86 int dma_tx_on;
87};
88
89/* Top level data structure of HSU */
90struct hsu_port {
91 void __iomem *reg;
92 unsigned long paddr;
93 unsigned long iolen;
94 u32 irq;
95
96 struct uart_hsu_port port[3];
97 struct hsu_dma_chan chans[10];
98
99 struct dentry *debugfs;
100};
101
102static inline unsigned int serial_in(struct uart_hsu_port *up, int offset)
103{
104 unsigned int val;
105
106 if (offset > UART_MSR) {
107 offset <<= 2;
108 val = readl(up->port.membase + offset);
109 } else
110 val = (unsigned int)readb(up->port.membase + offset);
111
112 return val;
113}
114
115static inline void serial_out(struct uart_hsu_port *up, int offset, int value)
116{
117 if (offset > UART_MSR) {
118 offset <<= 2;
119 writel(value, up->port.membase + offset);
120 } else {
121 unsigned char val = value & 0xff;
122 writeb(val, up->port.membase + offset);
123 }
124}
125
126#ifdef CONFIG_DEBUG_FS
127
128#define HSU_REGS_BUFSIZE 1024
129
130static int hsu_show_regs_open(struct inode *inode, struct file *file)
131{
132 file->private_data = inode->i_private;
133 return 0;
134}
135
136static ssize_t port_show_regs(struct file *file, char __user *user_buf,
137 size_t count, loff_t *ppos)
138{
139 struct uart_hsu_port *up = file->private_data;
140 char *buf;
141 u32 len = 0;
142 ssize_t ret;
143
144 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
145 if (!buf)
146 return 0;
147
148 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
149 "MFD HSU port[%d] regs:\n", up->index);
150
151 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
152 "=================================\n");
153 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
154 "IER: \t\t0x%08x\n", serial_in(up, UART_IER));
155 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
156 "IIR: \t\t0x%08x\n", serial_in(up, UART_IIR));
157 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
158 "LCR: \t\t0x%08x\n", serial_in(up, UART_LCR));
159 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
160 "MCR: \t\t0x%08x\n", serial_in(up, UART_MCR));
161 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
162 "LSR: \t\t0x%08x\n", serial_in(up, UART_LSR));
163 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
164 "MSR: \t\t0x%08x\n", serial_in(up, UART_MSR));
165 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
166 "FOR: \t\t0x%08x\n", serial_in(up, UART_FOR));
167 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
168 "PS: \t\t0x%08x\n", serial_in(up, UART_PS));
169 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
170 "MUL: \t\t0x%08x\n", serial_in(up, UART_MUL));
171 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
172 "DIV: \t\t0x%08x\n", serial_in(up, UART_DIV));
173
174 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
175 kfree(buf);
176 return ret;
177}
178
179static ssize_t dma_show_regs(struct file *file, char __user *user_buf,
180 size_t count, loff_t *ppos)
181{
182 struct hsu_dma_chan *chan = file->private_data;
183 char *buf;
184 u32 len = 0;
185 ssize_t ret;
186
187 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
188 if (!buf)
189 return 0;
190
191 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
192 "MFD HSU DMA channel [%d] regs:\n", chan->id);
193
194 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
195 "=================================\n");
196 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
197 "CR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_CR));
198 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
199 "DCR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_DCR));
200 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
201 "BSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_BSR));
202 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
203 "MOTSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_MOTSR));
204 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
205 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0SAR));
206 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
207 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0TSR));
208 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
209 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1SAR));
210 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
211 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1TSR));
212 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
213 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2SAR));
214 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
215 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2TSR));
216 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
217 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3SAR));
218 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
219 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3TSR));
220
221 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
222 kfree(buf);
223 return ret;
224}
225
226static const struct file_operations port_regs_ops = {
227 .owner = THIS_MODULE,
228 .open = hsu_show_regs_open,
229 .read = port_show_regs,
230};
231
232static const struct file_operations dma_regs_ops = {
233 .owner = THIS_MODULE,
234 .open = hsu_show_regs_open,
235 .read = dma_show_regs,
236};
237
238static int hsu_debugfs_init(struct hsu_port *hsu)
239{
240 int i;
241 char name[32];
242
243 hsu->debugfs = debugfs_create_dir("hsu", NULL);
244 if (!hsu->debugfs)
245 return -ENOMEM;
246
247 for (i = 0; i < 3; i++) {
248 snprintf(name, sizeof(name), "port_%d_regs", i);
249 debugfs_create_file(name, S_IFREG | S_IRUGO,
250 hsu->debugfs, (void *)(&hsu->port[i]), &port_regs_ops);
251 }
252
253 for (i = 0; i < 6; i++) {
254 snprintf(name, sizeof(name), "dma_chan_%d_regs", i);
255 debugfs_create_file(name, S_IFREG | S_IRUGO,
256 hsu->debugfs, (void *)&hsu->chans[i], &dma_regs_ops);
257 }
258
259 return 0;
260}
261
262static void hsu_debugfs_remove(struct hsu_port *hsu)
263{
264 if (hsu->debugfs)
265 debugfs_remove_recursive(hsu->debugfs);
266}
267
268#else
269static inline int hsu_debugfs_init(struct hsu_port *hsu)
270{
271 return 0;
272}
273
274static inline void hsu_debugfs_remove(struct hsu_port *hsu)
275{
276}
277#endif /* CONFIG_DEBUG_FS */
278
279static void serial_hsu_enable_ms(struct uart_port *port)
280{
281 struct uart_hsu_port *up =
282 container_of(port, struct uart_hsu_port, port);
283
284 up->ier |= UART_IER_MSI;
285 serial_out(up, UART_IER, up->ier);
286}
287
288void hsu_dma_tx(struct uart_hsu_port *up)
289{
290 struct circ_buf *xmit = &up->port.state->xmit;
291 struct hsu_dma_buffer *dbuf = &up->txbuf;
292 int count;
293
294 /* test_and_set_bit may be better, but anyway it's in lock protected mode */
295 if (up->dma_tx_on)
296 return;
297
298 /* Update the circ buf info */
299 xmit->tail += dbuf->ofs;
300 xmit->tail &= UART_XMIT_SIZE - 1;
301
302 up->port.icount.tx += dbuf->ofs;
303 dbuf->ofs = 0;
304
305 /* Disable the channel */
306 chan_writel(up->txc, HSU_CH_CR, 0x0);
307
308 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&up->port)) {
309 dma_sync_single_for_device(up->port.dev,
310 dbuf->dma_addr,
311 dbuf->dma_size,
312 DMA_TO_DEVICE);
313
314 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
315 dbuf->ofs = count;
316
317 /* Reprogram the channel */
318 chan_writel(up->txc, HSU_CH_D0SAR, dbuf->dma_addr + xmit->tail);
319 chan_writel(up->txc, HSU_CH_D0TSR, count);
320
321 /* Reenable the channel */
322 chan_writel(up->txc, HSU_CH_DCR, 0x1
323 | (0x1 << 8)
324 | (0x1 << 16)
325 | (0x1 << 24));
326 up->dma_tx_on = 1;
327 chan_writel(up->txc, HSU_CH_CR, 0x1);
328 }
329
330 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
331 uart_write_wakeup(&up->port);
332}
333
334/* The buffer is already cache coherent */
335void hsu_dma_start_rx_chan(struct hsu_dma_chan *rxc, struct hsu_dma_buffer *dbuf)
336{
337 dbuf->ofs = 0;
338
339 chan_writel(rxc, HSU_CH_BSR, 32);
340 chan_writel(rxc, HSU_CH_MOTSR, 4);
341
342 chan_writel(rxc, HSU_CH_D0SAR, dbuf->dma_addr);
343 chan_writel(rxc, HSU_CH_D0TSR, dbuf->dma_size);
344 chan_writel(rxc, HSU_CH_DCR, 0x1 | (0x1 << 8)
345 | (0x1 << 16)
346 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
347 );
348 chan_writel(rxc, HSU_CH_CR, 0x3);
349
350 mod_timer(&rxc->rx_timer, jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ);
351}
352
353/* Protected by spin_lock_irqsave(port->lock) */
354static void serial_hsu_start_tx(struct uart_port *port)
355{
356 struct uart_hsu_port *up =
357 container_of(port, struct uart_hsu_port, port);
358
359 if (up->use_dma) {
360 hsu_dma_tx(up);
361 } else if (!(up->ier & UART_IER_THRI)) {
362 up->ier |= UART_IER_THRI;
363 serial_out(up, UART_IER, up->ier);
364 }
365}
366
367static void serial_hsu_stop_tx(struct uart_port *port)
368{
369 struct uart_hsu_port *up =
370 container_of(port, struct uart_hsu_port, port);
371 struct hsu_dma_chan *txc = up->txc;
372
373 if (up->use_dma)
374 chan_writel(txc, HSU_CH_CR, 0x0);
375 else if (up->ier & UART_IER_THRI) {
376 up->ier &= ~UART_IER_THRI;
377 serial_out(up, UART_IER, up->ier);
378 }
379}
380
381/* This is always called in spinlock protected mode, so
382 * modify timeout timer is safe here */
383void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts)
384{
385 struct hsu_dma_buffer *dbuf = &up->rxbuf;
386 struct hsu_dma_chan *chan = up->rxc;
387 struct uart_port *port = &up->port;
388 struct tty_struct *tty = port->state->port.tty;
389 int count;
390
391 if (!tty)
392 return;
393
394 /*
395 * First need to know how many is already transferred,
396 * then check if its a timeout DMA irq, and return
397 * the trail bytes out, push them up and reenable the
398 * channel
399 */
400
401 /* Timeout IRQ, need wait some time, see Errata 2 */
402 if (int_sts & 0xf00)
403 udelay(2);
404
405 /* Stop the channel */
406 chan_writel(chan, HSU_CH_CR, 0x0);
407
408 count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
409 if (!count) {
410 /* Restart the channel before we leave */
411 chan_writel(chan, HSU_CH_CR, 0x3);
412 return;
413 }
414 del_timer(&chan->rx_timer);
415
416 dma_sync_single_for_cpu(port->dev, dbuf->dma_addr,
417 dbuf->dma_size, DMA_FROM_DEVICE);
418
419 /*
420 * Head will only wrap around when we recycle
421 * the DMA buffer, and when that happens, we
422 * explicitly set tail to 0. So head will
423 * always be greater than tail.
424 */
425 tty_insert_flip_string(tty, dbuf->buf, count);
426 port->icount.rx += count;
427
428 dma_sync_single_for_device(up->port.dev, dbuf->dma_addr,
429 dbuf->dma_size, DMA_FROM_DEVICE);
430
431 /* Reprogram the channel */
432 chan_writel(chan, HSU_CH_D0SAR, dbuf->dma_addr);
433 chan_writel(chan, HSU_CH_D0TSR, dbuf->dma_size);
434 chan_writel(chan, HSU_CH_DCR, 0x1
435 | (0x1 << 8)
436 | (0x1 << 16)
437 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
438 );
439 tty_flip_buffer_push(tty);
440
441 chan_writel(chan, HSU_CH_CR, 0x3);
442 chan->rx_timer.expires = jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ;
443 add_timer(&chan->rx_timer);
444
445}
446
447static void serial_hsu_stop_rx(struct uart_port *port)
448{
449 struct uart_hsu_port *up =
450 container_of(port, struct uart_hsu_port, port);
451 struct hsu_dma_chan *chan = up->rxc;
452
453 if (up->use_dma)
454 chan_writel(chan, HSU_CH_CR, 0x2);
455 else {
456 up->ier &= ~UART_IER_RLSI;
457 up->port.read_status_mask &= ~UART_LSR_DR;
458 serial_out(up, UART_IER, up->ier);
459 }
460}
461
462static inline void receive_chars(struct uart_hsu_port *up, int *status)
463{
464 struct tty_struct *tty = up->port.state->port.tty;
465 unsigned int ch, flag;
466 unsigned int max_count = 256;
467
468 if (!tty)
469 return;
470
471 do {
472 ch = serial_in(up, UART_RX);
473 flag = TTY_NORMAL;
474 up->port.icount.rx++;
475
476 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
477 UART_LSR_FE | UART_LSR_OE))) {
478
479 dev_warn(up->dev, "We really rush into ERR/BI case"
480 "status = 0x%02x", *status);
481 /* For statistics only */
482 if (*status & UART_LSR_BI) {
483 *status &= ~(UART_LSR_FE | UART_LSR_PE);
484 up->port.icount.brk++;
485 /*
486 * We do the SysRQ and SAK checking
487 * here because otherwise the break
488 * may get masked by ignore_status_mask
489 * or read_status_mask.
490 */
491 if (uart_handle_break(&up->port))
492 goto ignore_char;
493 } else if (*status & UART_LSR_PE)
494 up->port.icount.parity++;
495 else if (*status & UART_LSR_FE)
496 up->port.icount.frame++;
497 if (*status & UART_LSR_OE)
498 up->port.icount.overrun++;
499
500 /* Mask off conditions which should be ignored. */
501 *status &= up->port.read_status_mask;
502
503#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
504 if (up->port.cons &&
505 up->port.cons->index == up->port.line) {
506 /* Recover the break flag from console xmit */
507 *status |= up->lsr_break_flag;
508 up->lsr_break_flag = 0;
509 }
510#endif
511 if (*status & UART_LSR_BI) {
512 flag = TTY_BREAK;
513 } else if (*status & UART_LSR_PE)
514 flag = TTY_PARITY;
515 else if (*status & UART_LSR_FE)
516 flag = TTY_FRAME;
517 }
518
519 if (uart_handle_sysrq_char(&up->port, ch))
520 goto ignore_char;
521
522 uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
523 ignore_char:
524 *status = serial_in(up, UART_LSR);
525 } while ((*status & UART_LSR_DR) && max_count--);
526 tty_flip_buffer_push(tty);
527}
528
529static void transmit_chars(struct uart_hsu_port *up)
530{
531 struct circ_buf *xmit = &up->port.state->xmit;
532 int count;
533
534 if (up->port.x_char) {
535 serial_out(up, UART_TX, up->port.x_char);
536 up->port.icount.tx++;
537 up->port.x_char = 0;
538 return;
539 }
540 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
541 serial_hsu_stop_tx(&up->port);
542 return;
543 }
544
545#ifndef MFD_HSU_A0_STEPPING
546 count = up->port.fifosize / 2;
547#else
548 /*
549 * A0 only supports fully empty IRQ, and the first char written
550 * into it won't clear the EMPT bit, so we may need be cautious
551 * by useing a shorter buffer
552 */
553 count = up->port.fifosize - 4;
554#endif
555 do {
556 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
557 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
558
559 up->port.icount.tx++;
560 if (uart_circ_empty(xmit))
561 break;
562 } while (--count > 0);
563
564 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
565 uart_write_wakeup(&up->port);
566
567 if (uart_circ_empty(xmit))
568 serial_hsu_stop_tx(&up->port);
569}
570
571static inline void check_modem_status(struct uart_hsu_port *up)
572{
573 int status;
574
575 status = serial_in(up, UART_MSR);
576
577 if ((status & UART_MSR_ANY_DELTA) == 0)
578 return;
579
580 if (status & UART_MSR_TERI)
581 up->port.icount.rng++;
582 if (status & UART_MSR_DDSR)
583 up->port.icount.dsr++;
584 /* We may only get DDCD when HW init and reset */
585 if (status & UART_MSR_DDCD)
586 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
587 /* Will start/stop_tx accordingly */
588 if (status & UART_MSR_DCTS)
589 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
590
591 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
592}
593
594/*
595 * This handles the interrupt from one port.
596 */
597static irqreturn_t port_irq(int irq, void *dev_id)
598{
599 struct uart_hsu_port *up = dev_id;
600 unsigned int iir, lsr;
601 unsigned long flags;
602
603 if (unlikely(!up->running))
604 return IRQ_NONE;
605
606 spin_lock_irqsave(&up->port.lock, flags);
607 if (up->use_dma) {
608 lsr = serial_in(up, UART_LSR);
609 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
610 UART_LSR_FE | UART_LSR_OE)))
611 dev_warn(up->dev,
612 "Got lsr irq while using DMA, lsr = 0x%2x\n",
613 lsr);
614 check_modem_status(up);
615 spin_unlock_irqrestore(&up->port.lock, flags);
616 return IRQ_HANDLED;
617 }
618
619 iir = serial_in(up, UART_IIR);
620 if (iir & UART_IIR_NO_INT) {
621 spin_unlock_irqrestore(&up->port.lock, flags);
622 return IRQ_NONE;
623 }
624
625 lsr = serial_in(up, UART_LSR);
626 if (lsr & UART_LSR_DR)
627 receive_chars(up, &lsr);
628 check_modem_status(up);
629
630 /* lsr will be renewed during the receive_chars */
631 if (lsr & UART_LSR_THRE)
632 transmit_chars(up);
633
634 spin_unlock_irqrestore(&up->port.lock, flags);
635 return IRQ_HANDLED;
636}
637
638static inline void dma_chan_irq(struct hsu_dma_chan *chan)
639{
640 struct uart_hsu_port *up = chan->uport;
641 unsigned long flags;
642 u32 int_sts;
643
644 spin_lock_irqsave(&up->port.lock, flags);
645
646 if (!up->use_dma || !up->running)
647 goto exit;
648
649 /*
650 * No matter what situation, need read clear the IRQ status
651 * There is a bug, see Errata 5, HSD 2900918
652 */
653 int_sts = chan_readl(chan, HSU_CH_SR);
654
655 /* Rx channel */
656 if (chan->dirt == DMA_FROM_DEVICE)
657 hsu_dma_rx(up, int_sts);
658
659 /* Tx channel */
660 if (chan->dirt == DMA_TO_DEVICE) {
661 chan_writel(chan, HSU_CH_CR, 0x0);
662 up->dma_tx_on = 0;
663 hsu_dma_tx(up);
664 }
665
666exit:
667 spin_unlock_irqrestore(&up->port.lock, flags);
668 return;
669}
670
671static irqreturn_t dma_irq(int irq, void *dev_id)
672{
673 struct hsu_port *hsu = dev_id;
674 u32 int_sts, i;
675
676 int_sts = mfd_readl(hsu, HSU_GBL_DMAISR);
677
678 /* Currently we only have 6 channels may be used */
679 for (i = 0; i < 6; i++) {
680 if (int_sts & 0x1)
681 dma_chan_irq(&hsu->chans[i]);
682 int_sts >>= 1;
683 }
684
685 return IRQ_HANDLED;
686}
687
688static unsigned int serial_hsu_tx_empty(struct uart_port *port)
689{
690 struct uart_hsu_port *up =
691 container_of(port, struct uart_hsu_port, port);
692 unsigned long flags;
693 unsigned int ret;
694
695 spin_lock_irqsave(&up->port.lock, flags);
696 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
697 spin_unlock_irqrestore(&up->port.lock, flags);
698
699 return ret;
700}
701
702static unsigned int serial_hsu_get_mctrl(struct uart_port *port)
703{
704 struct uart_hsu_port *up =
705 container_of(port, struct uart_hsu_port, port);
706 unsigned char status;
707 unsigned int ret;
708
709 status = serial_in(up, UART_MSR);
710
711 ret = 0;
712 if (status & UART_MSR_DCD)
713 ret |= TIOCM_CAR;
714 if (status & UART_MSR_RI)
715 ret |= TIOCM_RNG;
716 if (status & UART_MSR_DSR)
717 ret |= TIOCM_DSR;
718 if (status & UART_MSR_CTS)
719 ret |= TIOCM_CTS;
720 return ret;
721}
722
723static void serial_hsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
724{
725 struct uart_hsu_port *up =
726 container_of(port, struct uart_hsu_port, port);
727 unsigned char mcr = 0;
728
729 if (mctrl & TIOCM_RTS)
730 mcr |= UART_MCR_RTS;
731 if (mctrl & TIOCM_DTR)
732 mcr |= UART_MCR_DTR;
733 if (mctrl & TIOCM_OUT1)
734 mcr |= UART_MCR_OUT1;
735 if (mctrl & TIOCM_OUT2)
736 mcr |= UART_MCR_OUT2;
737 if (mctrl & TIOCM_LOOP)
738 mcr |= UART_MCR_LOOP;
739
740 mcr |= up->mcr;
741
742 serial_out(up, UART_MCR, mcr);
743}
744
745static void serial_hsu_break_ctl(struct uart_port *port, int break_state)
746{
747 struct uart_hsu_port *up =
748 container_of(port, struct uart_hsu_port, port);
749 unsigned long flags;
750
751 spin_lock_irqsave(&up->port.lock, flags);
752 if (break_state == -1)
753 up->lcr |= UART_LCR_SBC;
754 else
755 up->lcr &= ~UART_LCR_SBC;
756 serial_out(up, UART_LCR, up->lcr);
757 spin_unlock_irqrestore(&up->port.lock, flags);
758}
759
760/*
761 * What special to do:
762 * 1. chose the 64B fifo mode
763 * 2. make sure not to select half empty mode for A0 stepping
764 * 3. start dma or pio depends on configuration
765 * 4. we only allocate dma memory when needed
766 */
767static int serial_hsu_startup(struct uart_port *port)
768{
769 struct uart_hsu_port *up =
770 container_of(port, struct uart_hsu_port, port);
771 unsigned long flags;
772
773 /*
774 * Clear the FIFO buffers and disable them.
775 * (they will be reenabled in set_termios())
776 */
777 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
778 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
779 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
780 serial_out(up, UART_FCR, 0);
781
782 /* Clear the interrupt registers. */
783 (void) serial_in(up, UART_LSR);
784 (void) serial_in(up, UART_RX);
785 (void) serial_in(up, UART_IIR);
786 (void) serial_in(up, UART_MSR);
787
788 /* Now, initialize the UART, default is 8n1 */
789 serial_out(up, UART_LCR, UART_LCR_WLEN8);
790
791 spin_lock_irqsave(&up->port.lock, flags);
792
793 up->port.mctrl |= TIOCM_OUT2;
794 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
795
796 /*
797 * Finally, enable interrupts. Note: Modem status interrupts
798 * are set via set_termios(), which will be occurring imminently
799 * anyway, so we don't enable them here.
800 */
801 if (!up->use_dma)
802 up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE;
803 else
804 up->ier = 0;
805 serial_out(up, UART_IER, up->ier);
806
807 spin_unlock_irqrestore(&up->port.lock, flags);
808
809 /* DMA init */
810 if (up->use_dma) {
811 struct hsu_dma_buffer *dbuf;
812 struct circ_buf *xmit = &port->state->xmit;
813
814 up->dma_tx_on = 0;
815
816 /* First allocate the RX buffer */
817 dbuf = &up->rxbuf;
818 dbuf->buf = kzalloc(HSU_DMA_BUF_SIZE, GFP_KERNEL);
819 if (!dbuf->buf) {
820 up->use_dma = 0;
821 goto exit;
822 }
823 dbuf->dma_addr = dma_map_single(port->dev,
824 dbuf->buf,
825 HSU_DMA_BUF_SIZE,
826 DMA_FROM_DEVICE);
827 dbuf->dma_size = HSU_DMA_BUF_SIZE;
828
829 /* Start the RX channel right now */
830 hsu_dma_start_rx_chan(up->rxc, dbuf);
831
832 /* Next init the TX DMA */
833 dbuf = &up->txbuf;
834 dbuf->buf = xmit->buf;
835 dbuf->dma_addr = dma_map_single(port->dev,
836 dbuf->buf,
837 UART_XMIT_SIZE,
838 DMA_TO_DEVICE);
839 dbuf->dma_size = UART_XMIT_SIZE;
840
841 /* This should not be changed all around */
842 chan_writel(up->txc, HSU_CH_BSR, 32);
843 chan_writel(up->txc, HSU_CH_MOTSR, 4);
844 dbuf->ofs = 0;
845 }
846
847exit:
848 /* And clear the interrupt registers again for luck. */
849 (void) serial_in(up, UART_LSR);
850 (void) serial_in(up, UART_RX);
851 (void) serial_in(up, UART_IIR);
852 (void) serial_in(up, UART_MSR);
853
854 up->running = 1;
855 return 0;
856}
857
858static void serial_hsu_shutdown(struct uart_port *port)
859{
860 struct uart_hsu_port *up =
861 container_of(port, struct uart_hsu_port, port);
862 unsigned long flags;
863
864 del_timer_sync(&up->rxc->rx_timer);
865
866 /* Disable interrupts from this port */
867 up->ier = 0;
868 serial_out(up, UART_IER, 0);
869 up->running = 0;
870
871 spin_lock_irqsave(&up->port.lock, flags);
872 up->port.mctrl &= ~TIOCM_OUT2;
873 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
874 spin_unlock_irqrestore(&up->port.lock, flags);
875
876 /* Disable break condition and FIFOs */
877 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
878 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
879 UART_FCR_CLEAR_RCVR |
880 UART_FCR_CLEAR_XMIT);
881 serial_out(up, UART_FCR, 0);
882}
883
884static void
885serial_hsu_set_termios(struct uart_port *port, struct ktermios *termios,
886 struct ktermios *old)
887{
888 struct uart_hsu_port *up =
889 container_of(port, struct uart_hsu_port, port);
890 struct tty_struct *tty = port->state->port.tty;
891 unsigned char cval, fcr = 0;
892 unsigned long flags;
893 unsigned int baud, quot;
894 u32 mul = 0x3600;
895 u32 ps = 0x10;
896
897 switch (termios->c_cflag & CSIZE) {
898 case CS5:
899 cval = UART_LCR_WLEN5;
900 break;
901 case CS6:
902 cval = UART_LCR_WLEN6;
903 break;
904 case CS7:
905 cval = UART_LCR_WLEN7;
906 break;
907 default:
908 case CS8:
909 cval = UART_LCR_WLEN8;
910 break;
911 }
912
913 /* CMSPAR isn't supported by this driver */
914 if (tty)
915 tty->termios->c_cflag &= ~CMSPAR;
916
917 if (termios->c_cflag & CSTOPB)
918 cval |= UART_LCR_STOP;
919 if (termios->c_cflag & PARENB)
920 cval |= UART_LCR_PARITY;
921 if (!(termios->c_cflag & PARODD))
922 cval |= UART_LCR_EPAR;
923
924 /*
925 * For those basic low baud rate we can get the direct
926 * scalar from 2746800, like 115200 = 2746800/24, for those
927 * higher baud rate, we have to handle them case by case,
928 * but DIV reg is never touched as its default value 0x3d09
929 */
930 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
931 quot = uart_get_divisor(port, baud);
932
933 switch (baud) {
934 case 3500000:
935 mul = 0x3345;
936 ps = 0xC;
937 quot = 1;
938 break;
939 case 2500000:
940 mul = 0x2710;
941 ps = 0x10;
942 quot = 1;
943 break;
944 case 18432000:
945 mul = 0x2400;
946 ps = 0x10;
947 quot = 1;
948 break;
949 case 1500000:
950 mul = 0x1D4C;
951 ps = 0xc;
952 quot = 1;
953 break;
954 default:
955 ;
956 }
957
958 if ((up->port.uartclk / quot) < (2400 * 16))
959 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_1B;
960 else if ((up->port.uartclk / quot) < (230400 * 16))
961 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_16B;
962 else
963 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_32B;
964
965 fcr |= UART_FCR_HSU_64B_FIFO;
966#ifdef MFD_HSU_A0_STEPPING
967 /* A0 doesn't support half empty IRQ */
968 fcr |= UART_FCR_FULL_EMPT_TXI;
969#endif
970
971 /*
972 * Ok, we're now changing the port state. Do it with
973 * interrupts disabled.
974 */
975 spin_lock_irqsave(&up->port.lock, flags);
976
977 /* Update the per-port timeout */
978 uart_update_timeout(port, termios->c_cflag, baud);
979
980 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
981 if (termios->c_iflag & INPCK)
982 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
983 if (termios->c_iflag & (BRKINT | PARMRK))
984 up->port.read_status_mask |= UART_LSR_BI;
985
986 /* Characters to ignore */
987 up->port.ignore_status_mask = 0;
988 if (termios->c_iflag & IGNPAR)
989 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
990 if (termios->c_iflag & IGNBRK) {
991 up->port.ignore_status_mask |= UART_LSR_BI;
992 /*
993 * If we're ignoring parity and break indicators,
994 * ignore overruns too (for real raw support).
995 */
996 if (termios->c_iflag & IGNPAR)
997 up->port.ignore_status_mask |= UART_LSR_OE;
998 }
999
1000 /* Ignore all characters if CREAD is not set */
1001 if ((termios->c_cflag & CREAD) == 0)
1002 up->port.ignore_status_mask |= UART_LSR_DR;
1003
1004 /*
1005 * CTS flow control flag and modem status interrupts, disable
1006 * MSI by default
1007 */
1008 up->ier &= ~UART_IER_MSI;
1009 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
1010 up->ier |= UART_IER_MSI;
1011
1012 serial_out(up, UART_IER, up->ier);
1013
1014 if (termios->c_cflag & CRTSCTS)
1015 up->mcr |= UART_MCR_AFE | UART_MCR_RTS;
1016 else
1017 up->mcr &= ~UART_MCR_AFE;
1018
1019 serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1020 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
1021 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
1022 serial_out(up, UART_LCR, cval); /* reset DLAB */
1023 serial_out(up, UART_MUL, mul); /* set MUL */
1024 serial_out(up, UART_PS, ps); /* set PS */
1025 up->lcr = cval; /* Save LCR */
1026 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
1027 serial_out(up, UART_FCR, fcr);
1028 spin_unlock_irqrestore(&up->port.lock, flags);
1029}
1030
1031static void
1032serial_hsu_pm(struct uart_port *port, unsigned int state,
1033 unsigned int oldstate)
1034{
1035}
1036
1037static void serial_hsu_release_port(struct uart_port *port)
1038{
1039}
1040
1041static int serial_hsu_request_port(struct uart_port *port)
1042{
1043 return 0;
1044}
1045
1046static void serial_hsu_config_port(struct uart_port *port, int flags)
1047{
1048 struct uart_hsu_port *up =
1049 container_of(port, struct uart_hsu_port, port);
1050 up->port.type = PORT_MFD;
1051}
1052
1053static int
1054serial_hsu_verify_port(struct uart_port *port, struct serial_struct *ser)
1055{
1056 /* We don't want the core code to modify any port params */
1057 return -EINVAL;
1058}
1059
1060static const char *
1061serial_hsu_type(struct uart_port *port)
1062{
1063 struct uart_hsu_port *up =
1064 container_of(port, struct uart_hsu_port, port);
1065 return up->name;
1066}
1067
1068/* Mainly for uart console use */
1069static struct uart_hsu_port *serial_hsu_ports[3];
1070static struct uart_driver serial_hsu_reg;
1071
1072#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1073
1074#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1075
1076/* Wait for transmitter & holding register to empty */
1077static inline void wait_for_xmitr(struct uart_hsu_port *up)
1078{
1079 unsigned int status, tmout = 1000;
1080
1081 /* Wait up to 1ms for the character to be sent. */
1082 do {
1083 status = serial_in(up, UART_LSR);
1084
1085 if (status & UART_LSR_BI)
1086 up->lsr_break_flag = UART_LSR_BI;
1087
1088 if (--tmout == 0)
1089 break;
1090 udelay(1);
1091 } while (!(status & BOTH_EMPTY));
1092
1093 /* Wait up to 1s for flow control if necessary */
1094 if (up->port.flags & UPF_CONS_FLOW) {
1095 tmout = 1000000;
1096 while (--tmout &&
1097 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1098 udelay(1);
1099 }
1100}
1101
1102static void serial_hsu_console_putchar(struct uart_port *port, int ch)
1103{
1104 struct uart_hsu_port *up =
1105 container_of(port, struct uart_hsu_port, port);
1106
1107 wait_for_xmitr(up);
1108 serial_out(up, UART_TX, ch);
1109}
1110
1111/*
1112 * Print a string to the serial port trying not to disturb
1113 * any possible real use of the port...
1114 *
1115 * The console_lock must be held when we get here.
1116 */
1117static void
1118serial_hsu_console_write(struct console *co, const char *s, unsigned int count)
1119{
1120 struct uart_hsu_port *up = serial_hsu_ports[co->index];
1121 unsigned long flags;
1122 unsigned int ier;
1123 int locked = 1;
1124
1125 local_irq_save(flags);
1126 if (up->port.sysrq)
1127 locked = 0;
1128 else if (oops_in_progress) {
1129 locked = spin_trylock(&up->port.lock);
1130 } else
1131 spin_lock(&up->port.lock);
1132
1133 /* First save the IER then disable the interrupts */
1134 ier = serial_in(up, UART_IER);
1135 serial_out(up, UART_IER, 0);
1136
1137 uart_console_write(&up->port, s, count, serial_hsu_console_putchar);
1138
1139 /*
1140 * Finally, wait for transmitter to become empty
1141 * and restore the IER
1142 */
1143 wait_for_xmitr(up);
1144 serial_out(up, UART_IER, ier);
1145
1146 if (locked)
1147 spin_unlock(&up->port.lock);
1148 local_irq_restore(flags);
1149}
1150
1151static struct console serial_hsu_console;
1152
1153static int __init
1154serial_hsu_console_setup(struct console *co, char *options)
1155{
1156 struct uart_hsu_port *up;
1157 int baud = 115200;
1158 int bits = 8;
1159 int parity = 'n';
1160 int flow = 'n';
1161 int ret;
1162
1163 if (co->index == -1 || co->index >= serial_hsu_reg.nr)
1164 co->index = 0;
1165 up = serial_hsu_ports[co->index];
1166 if (!up)
1167 return -ENODEV;
1168
1169 if (options)
1170 uart_parse_options(options, &baud, &parity, &bits, &flow);
1171
1172 ret = uart_set_options(&up->port, co, baud, parity, bits, flow);
1173
1174 return ret;
1175}
1176
1177static struct console serial_hsu_console = {
1178 .name = "ttyMFD",
1179 .write = serial_hsu_console_write,
1180 .device = uart_console_device,
1181 .setup = serial_hsu_console_setup,
1182 .flags = CON_PRINTBUFFER,
1183 .index = 2,
1184 .data = &serial_hsu_reg,
1185};
1186#endif
1187
1188struct uart_ops serial_hsu_pops = {
1189 .tx_empty = serial_hsu_tx_empty,
1190 .set_mctrl = serial_hsu_set_mctrl,
1191 .get_mctrl = serial_hsu_get_mctrl,
1192 .stop_tx = serial_hsu_stop_tx,
1193 .start_tx = serial_hsu_start_tx,
1194 .stop_rx = serial_hsu_stop_rx,
1195 .enable_ms = serial_hsu_enable_ms,
1196 .break_ctl = serial_hsu_break_ctl,
1197 .startup = serial_hsu_startup,
1198 .shutdown = serial_hsu_shutdown,
1199 .set_termios = serial_hsu_set_termios,
1200 .pm = serial_hsu_pm,
1201 .type = serial_hsu_type,
1202 .release_port = serial_hsu_release_port,
1203 .request_port = serial_hsu_request_port,
1204 .config_port = serial_hsu_config_port,
1205 .verify_port = serial_hsu_verify_port,
1206};
1207
1208static struct uart_driver serial_hsu_reg = {
1209 .owner = THIS_MODULE,
1210 .driver_name = "MFD serial",
1211 .dev_name = "ttyMFD",
1212 .major = TTY_MAJOR,
1213 .minor = 128,
1214 .nr = 3,
1215};
1216
1217#ifdef CONFIG_PM
1218static int serial_hsu_suspend(struct pci_dev *pdev, pm_message_t state)
1219{
1220 void *priv = pci_get_drvdata(pdev);
1221 struct uart_hsu_port *up;
1222
1223 /* Make sure this is not the internal dma controller */
1224 if (priv && (pdev->device != 0x081E)) {
1225 up = priv;
1226 uart_suspend_port(&serial_hsu_reg, &up->port);
1227 }
1228
1229 pci_save_state(pdev);
1230 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1231 return 0;
1232}
1233
1234static int serial_hsu_resume(struct pci_dev *pdev)
1235{
1236 void *priv = pci_get_drvdata(pdev);
1237 struct uart_hsu_port *up;
1238 int ret;
1239
1240 pci_set_power_state(pdev, PCI_D0);
1241 pci_restore_state(pdev);
1242
1243 ret = pci_enable_device(pdev);
1244 if (ret)
1245 dev_warn(&pdev->dev,
1246 "HSU: can't re-enable device, try to continue\n");
1247
1248 if (priv && (pdev->device != 0x081E)) {
1249 up = priv;
1250 uart_resume_port(&serial_hsu_reg, &up->port);
1251 }
1252 return 0;
1253}
1254#else
1255#define serial_hsu_suspend NULL
1256#define serial_hsu_resume NULL
1257#endif
1258
1259/* temp global pointer before we settle down on using one or four PCI dev */
1260static struct hsu_port *phsu;
1261
1262static int serial_hsu_probe(struct pci_dev *pdev,
1263 const struct pci_device_id *ent)
1264{
1265 struct uart_hsu_port *uport;
1266 int index, ret;
1267
1268 printk(KERN_INFO "HSU: found PCI Serial controller(ID: %04x:%04x)\n",
1269 pdev->vendor, pdev->device);
1270
1271 switch (pdev->device) {
1272 case 0x081B:
1273 index = 0;
1274 break;
1275 case 0x081C:
1276 index = 1;
1277 break;
1278 case 0x081D:
1279 index = 2;
1280 break;
1281 case 0x081E:
1282 /* internal DMA controller */
1283 index = 3;
1284 break;
1285 default:
1286 dev_err(&pdev->dev, "HSU: out of index!");
1287 return -ENODEV;
1288 }
1289
1290 ret = pci_enable_device(pdev);
1291 if (ret)
1292 return ret;
1293
1294 if (index == 3) {
1295 /* DMA controller */
1296 ret = request_irq(pdev->irq, dma_irq, 0, "hsu_dma", phsu);
1297 if (ret) {
1298 dev_err(&pdev->dev, "can not get IRQ\n");
1299 goto err_disable;
1300 }
1301 pci_set_drvdata(pdev, phsu);
1302 } else {
1303 /* UART port 0~2 */
1304 uport = &phsu->port[index];
1305 uport->port.irq = pdev->irq;
1306 uport->port.dev = &pdev->dev;
1307 uport->dev = &pdev->dev;
1308
1309 ret = request_irq(pdev->irq, port_irq, 0, uport->name, uport);
1310 if (ret) {
1311 dev_err(&pdev->dev, "can not get IRQ\n");
1312 goto err_disable;
1313 }
1314 uart_add_one_port(&serial_hsu_reg, &uport->port);
1315
1316#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1317 if (index == 2) {
1318 register_console(&serial_hsu_console);
1319 uport->port.cons = &serial_hsu_console;
1320 }
1321#endif
1322 pci_set_drvdata(pdev, uport);
1323 }
1324
1325 return 0;
1326
1327err_disable:
1328 pci_disable_device(pdev);
1329 return ret;
1330}
1331
1332static void hsu_dma_rx_timeout(unsigned long data)
1333{
1334 struct hsu_dma_chan *chan = (void *)data;
1335 struct uart_hsu_port *up = chan->uport;
1336 struct hsu_dma_buffer *dbuf = &up->rxbuf;
1337 int count = 0;
1338 unsigned long flags;
1339
1340 spin_lock_irqsave(&up->port.lock, flags);
1341
1342 count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
1343
1344 if (!count) {
1345 mod_timer(&chan->rx_timer, jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ);
1346 goto exit;
1347 }
1348
1349 hsu_dma_rx(up, 0);
1350exit:
1351 spin_unlock_irqrestore(&up->port.lock, flags);
1352}
1353
1354static void hsu_global_init(void)
1355{
1356 struct hsu_port *hsu;
1357 struct uart_hsu_port *uport;
1358 struct hsu_dma_chan *dchan;
1359 int i, ret;
1360
1361 hsu = kzalloc(sizeof(struct hsu_port), GFP_KERNEL);
1362 if (!hsu)
1363 return;
1364
1365 /* Get basic io resource and map it */
1366 hsu->paddr = 0xffa28000;
1367 hsu->iolen = 0x1000;
1368
1369 if (!(request_mem_region(hsu->paddr, hsu->iolen, "HSU global")))
1370 pr_warning("HSU: error in request mem region\n");
1371
1372 hsu->reg = ioremap_nocache((unsigned long)hsu->paddr, hsu->iolen);
1373 if (!hsu->reg) {
1374 pr_err("HSU: error in ioremap\n");
1375 ret = -ENOMEM;
1376 goto err_free_region;
1377 }
1378
1379 /* Initialise the 3 UART ports */
1380 uport = hsu->port;
1381 for (i = 0; i < 3; i++) {
1382 uport->port.type = PORT_MFD;
1383 uport->port.iotype = UPIO_MEM;
1384 uport->port.mapbase = (resource_size_t)hsu->paddr
1385 + HSU_PORT_REG_OFFSET
1386 + i * HSU_PORT_REG_LENGTH;
1387 uport->port.membase = hsu->reg + HSU_PORT_REG_OFFSET
1388 + i * HSU_PORT_REG_LENGTH;
1389
1390 sprintf(uport->name, "hsu_port%d", i);
1391 uport->port.fifosize = 64;
1392 uport->port.ops = &serial_hsu_pops;
1393 uport->port.line = i;
1394 uport->port.flags = UPF_IOREMAP;
1395 /* set the scalable maxim support rate to 2746800 bps */
1396 uport->port.uartclk = 115200 * 24 * 16;
1397
1398 uport->running = 0;
1399 uport->txc = &hsu->chans[i * 2];
1400 uport->rxc = &hsu->chans[i * 2 + 1];
1401
1402 serial_hsu_ports[i] = uport;
1403 uport->index = i;
1404 uport++;
1405 }
1406
1407 /* Initialise 6 dma channels */
1408 dchan = hsu->chans;
1409 for (i = 0; i < 6; i++) {
1410 dchan->id = i;
1411 dchan->dirt = (i & 0x1) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1412 dchan->uport = &hsu->port[i/2];
1413 dchan->reg = hsu->reg + HSU_DMA_CHANS_REG_OFFSET +
1414 i * HSU_DMA_CHANS_REG_LENGTH;
1415
1416 /* Work around for RX */
1417 if (dchan->dirt == DMA_FROM_DEVICE) {
1418 init_timer(&dchan->rx_timer);
1419 dchan->rx_timer.function = hsu_dma_rx_timeout;
1420 dchan->rx_timer.data = (unsigned long)dchan;
1421 }
1422 dchan++;
1423 }
1424
1425 phsu = hsu;
1426
1427 hsu_debugfs_init(hsu);
1428 return;
1429
1430err_free_region:
1431 release_mem_region(hsu->paddr, hsu->iolen);
1432 kfree(hsu);
1433 return;
1434}
1435
1436static void serial_hsu_remove(struct pci_dev *pdev)
1437{
1438 struct hsu_port *hsu;
1439 int i;
1440
1441 hsu = pci_get_drvdata(pdev);
1442 if (!hsu)
1443 return;
1444
1445 for (i = 0; i < 3; i++)
1446 uart_remove_one_port(&serial_hsu_reg, &hsu->port[i].port);
1447
1448 pci_set_drvdata(pdev, NULL);
1449 free_irq(hsu->irq, hsu);
1450 pci_disable_device(pdev);
1451}
1452
1453/* First 3 are UART ports, and the 4th is the DMA */
1454static const struct pci_device_id pci_ids[] __devinitdata = {
1455 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081B) },
1456 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081C) },
1457 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081D) },
1458 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081E) },
1459 {},
1460};
1461
1462static struct pci_driver hsu_pci_driver = {
1463 .name = "HSU serial",
1464 .id_table = pci_ids,
1465 .probe = serial_hsu_probe,
1466 .remove = __devexit_p(serial_hsu_remove),
1467 .suspend = serial_hsu_suspend,
1468 .resume = serial_hsu_resume,
1469};
1470
1471static int __init hsu_pci_init(void)
1472{
1473 int ret;
1474
1475 hsu_global_init();
1476
1477 ret = uart_register_driver(&serial_hsu_reg);
1478 if (ret)
1479 return ret;
1480
1481 return pci_register_driver(&hsu_pci_driver);
1482}
1483
1484static void __exit hsu_pci_exit(void)
1485{
1486 pci_unregister_driver(&hsu_pci_driver);
1487 uart_unregister_driver(&serial_hsu_reg);
1488
1489 hsu_debugfs_remove(phsu);
1490
1491 kfree(phsu);
1492}
1493
1494module_init(hsu_pci_init);
1495module_exit(hsu_pci_exit);
1496
1497MODULE_LICENSE("GPL v2");
1498MODULE_ALIAS("platform:medfield-hsu");
diff --git a/drivers/serial/mrst_max3110.c b/drivers/serial/mrst_max3110.c
new file mode 100644
index 00000000000..f6ad1ecbff7
--- /dev/null
+++ b/drivers/serial/mrst_max3110.c
@@ -0,0 +1,844 @@
1/*
2 * max3110.c - spi uart protocol driver for Maxim 3110 on Moorestown
3 *
4 * Copyright (C) Intel 2008 Feng Tang <feng.tang@intel.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20/*
21 * Note:
22 * 1. From Max3110 spec, the Rx FIFO has 8 words, while the Tx FIFO only has
23 * 1 word. If SPI master controller doesn't support sclk frequency change,
24 * then the char need be sent out one by one with some delay
25 *
26 * 2. Currently only RX availabe interrrupt is used, no need for waiting TXE
27 * interrupt for a low speed UART device
28 */
29
30#include <linux/module.h>
31#include <linux/ioport.h>
32#include <linux/init.h>
33#include <linux/console.h>
34#include <linux/sysrq.h>
35#include <linux/platform_device.h>
36#include <linux/tty.h>
37#include <linux/tty_flip.h>
38#include <linux/serial_core.h>
39#include <linux/serial_reg.h>
40
41#include <linux/kthread.h>
42#include <linux/delay.h>
43#include <asm/atomic.h>
44#include <linux/spi/spi.h>
45#include <linux/spi/dw_spi.h>
46
47#include "mrst_max3110.h"
48
49#define PR_FMT "mrst_max3110: "
50
51#define UART_TX_NEEDED 1
52#define CON_TX_NEEDED 2
53#define BIT_IRQ_PENDING 3
54
55struct uart_max3110 {
56 struct uart_port port;
57 struct spi_device *spi;
58 char *name;
59
60 wait_queue_head_t wq;
61 struct task_struct *main_thread;
62 struct task_struct *read_thread;
63 struct mutex thread_mutex;;
64
65 u32 baud;
66 u16 cur_conf;
67 u8 clock;
68 u8 parity, word_7bits;
69
70 unsigned long uart_flags;
71
72 /* console related */
73 struct circ_buf con_xmit;
74
75 /* irq related */
76 u16 irq;
77};
78
79/* global data structure, may need be removed */
80struct uart_max3110 *pmax;
81static inline void receive_char(struct uart_max3110 *max, u8 ch);
82static void receive_chars(struct uart_max3110 *max,
83 unsigned char *str, int len);
84static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf);
85static void max3110_console_receive(struct uart_max3110 *max);
86
87int max3110_write_then_read(struct uart_max3110 *max,
88 const u8 *txbuf, u8 *rxbuf, unsigned len, int always_fast)
89{
90 struct spi_device *spi = max->spi;
91 struct spi_message message;
92 struct spi_transfer x;
93 int ret;
94
95 if (!txbuf || !rxbuf)
96 return -EINVAL;
97
98 spi_message_init(&message);
99 memset(&x, 0, sizeof x);
100 x.len = len;
101 x.tx_buf = txbuf;
102 x.rx_buf = rxbuf;
103 spi_message_add_tail(&x, &message);
104
105 if (always_fast)
106 x.speed_hz = 3125000;
107 else if (max->baud)
108 x.speed_hz = max->baud;
109
110 /* Do the i/o */
111 ret = spi_sync(spi, &message);
112 return ret;
113}
114
115/* Write a u16 to the device, and return one u16 read back */
116int max3110_out(struct uart_max3110 *max, const u16 out)
117{
118 u16 tmp;
119 int ret;
120
121 ret = max3110_write_then_read(max, (u8 *)&out, (u8 *)&tmp, 2, 1);
122 if (ret)
123 return ret;
124
125 /* If some valid data is read back */
126 if (tmp & MAX3110_READ_DATA_AVAILABLE)
127 receive_char(max, (tmp & 0xff));
128
129 return ret;
130}
131
132#define MAX_READ_LEN 20
133/*
134 * This is usually used to read data from SPIC RX FIFO, which doesn't
135 * need any delay like flushing character out. It returns how many
136 * valide bytes are read back
137 */
138static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf)
139{
140 u16 out[MAX_READ_LEN], in[MAX_READ_LEN];
141 u8 *pbuf, valid_str[MAX_READ_LEN];
142 int i, j, bytelen;
143
144 if (len > MAX_READ_LEN) {
145 pr_err(PR_FMT "read len %d is too large\n", len);
146 return 0;
147 }
148
149 bytelen = len * 2;
150 memset(out, 0, bytelen);
151 memset(in, 0, bytelen);
152
153 if (max3110_write_then_read(max, (u8 *)out, (u8 *)in, bytelen, 1))
154 return 0;
155
156 /* If caller don't provide a buffer, then handle received char */
157 pbuf = buf ? buf : valid_str;
158
159 for (i = 0, j = 0; i < len; i++) {
160 if (in[i] & MAX3110_READ_DATA_AVAILABLE)
161 pbuf[j++] = (u8)(in[i] & 0xff);
162 }
163
164 if (j && (pbuf == valid_str))
165 receive_chars(max, valid_str, j);
166
167 return j;
168}
169
170static void serial_m3110_con_putchar(struct uart_port *port, int ch)
171{
172 struct uart_max3110 *max =
173 container_of(port, struct uart_max3110, port);
174 struct circ_buf *xmit = &max->con_xmit;
175
176 if (uart_circ_chars_free(xmit)) {
177 xmit->buf[xmit->head] = (char)ch;
178 xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
179 }
180
181
182 if (!test_and_set_bit(CON_TX_NEEDED, &max->uart_flags))
183 wake_up_process(max->main_thread);
184}
185
186/*
187 * Print a string to the serial port trying not to disturb
188 * any possible real use of the port...
189 *
190 * The console_lock must be held when we get here.
191 */
192static void serial_m3110_con_write(struct console *co,
193 const char *s, unsigned int count)
194{
195 if (!pmax)
196 return;
197
198 uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar);
199}
200
201static int __init
202serial_m3110_con_setup(struct console *co, char *options)
203{
204 struct uart_max3110 *max = pmax;
205 int baud = 115200;
206 int bits = 8;
207 int parity = 'n';
208 int flow = 'n';
209
210 pr_info(PR_FMT "setting up console\n");
211
212 if (!max) {
213 pr_err(PR_FMT "pmax is NULL, return");
214 return -ENODEV;
215 }
216
217 if (options)
218 uart_parse_options(options, &baud, &parity, &bits, &flow);
219
220 return uart_set_options(&max->port, co, baud, parity, bits, flow);
221}
222
223static struct tty_driver *serial_m3110_con_device(struct console *co,
224 int *index)
225{
226 struct uart_driver *p = co->data;
227 *index = co->index;
228 return p->tty_driver;
229}
230
231static struct uart_driver serial_m3110_reg;
232static struct console serial_m3110_console = {
233 .name = "ttyS",
234 .write = serial_m3110_con_write,
235 .device = serial_m3110_con_device,
236 .setup = serial_m3110_con_setup,
237 .flags = CON_PRINTBUFFER,
238 .index = -1,
239 .data = &serial_m3110_reg,
240};
241
242#define MRST_CONSOLE (&serial_m3110_console)
243
244static unsigned int serial_m3110_tx_empty(struct uart_port *port)
245{
246 return 1;
247}
248
249static void serial_m3110_stop_tx(struct uart_port *port)
250{
251 return;
252}
253
254/* stop_rx will be called in spin_lock env */
255static void serial_m3110_stop_rx(struct uart_port *port)
256{
257 return;
258}
259
260#define WORDS_PER_XFER 128
261static inline void send_circ_buf(struct uart_max3110 *max,
262 struct circ_buf *xmit)
263{
264 int len, left = 0;
265 u16 obuf[WORDS_PER_XFER], ibuf[WORDS_PER_XFER];
266 u8 valid_str[WORDS_PER_XFER];
267 int i, j;
268
269 while (!uart_circ_empty(xmit)) {
270 left = uart_circ_chars_pending(xmit);
271 while (left) {
272 len = (left >= WORDS_PER_XFER) ? WORDS_PER_XFER : left;
273
274 memset(obuf, 0, len * 2);
275 memset(ibuf, 0, len * 2);
276 for (i = 0; i < len; i++) {
277 obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
278 xmit->tail = (xmit->tail + 1) &
279 (UART_XMIT_SIZE - 1);
280 }
281 max3110_write_then_read(max, (u8 *)obuf,
282 (u8 *)ibuf, len * 2, 0);
283
284 for (i = 0, j = 0; i < len; i++) {
285 if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
286 valid_str[j++] = (u8)(ibuf[i] & 0xff);
287 }
288
289 if (j)
290 receive_chars(max, valid_str, j);
291
292 max->port.icount.tx += len;
293 left -= len;
294 }
295 }
296}
297
298static void transmit_char(struct uart_max3110 *max)
299{
300 struct uart_port *port = &max->port;
301 struct circ_buf *xmit = &port->state->xmit;
302
303 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
304 return;
305
306 send_circ_buf(max, xmit);
307
308 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
309 uart_write_wakeup(port);
310
311 if (uart_circ_empty(xmit))
312 serial_m3110_stop_tx(port);
313}
314
315/* This will be called by uart_write() and tty_write, can't
316 * go to sleep */
317static void serial_m3110_start_tx(struct uart_port *port)
318{
319 struct uart_max3110 *max =
320 container_of(port, struct uart_max3110, port);
321
322 if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags))
323 wake_up_process(max->main_thread);
324}
325
326static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len)
327{
328 struct uart_port *port = &max->port;
329 struct tty_struct *tty;
330 int usable;
331
332 /* If uart is not opened, just return */
333 if (!port->state)
334 return;
335
336 tty = port->state->port.tty;
337 if (!tty)
338 return; /* receive some char before the tty is opened */
339
340 while (len) {
341 usable = tty_buffer_request_room(tty, len);
342 if (usable) {
343 tty_insert_flip_string(tty, str, usable);
344 str += usable;
345 port->icount.rx += usable;
346 tty_flip_buffer_push(tty);
347 }
348 len -= usable;
349 }
350}
351
352static inline void receive_char(struct uart_max3110 *max, u8 ch)
353{
354 receive_chars(max, &ch, 1);
355}
356
357static void max3110_console_receive(struct uart_max3110 *max)
358{
359 int loop = 1, num, total = 0;
360 u8 recv_buf[512], *pbuf;
361
362 pbuf = recv_buf;
363 do {
364 num = max3110_read_multi(max, 8, pbuf);
365
366 if (num) {
367 loop = 10;
368 pbuf += num;
369 total += num;
370
371 if (total >= 500) {
372 receive_chars(max, recv_buf, total);
373 pbuf = recv_buf;
374 total = 0;
375 }
376 }
377 } while (--loop);
378
379 if (total)
380 receive_chars(max, recv_buf, total);
381}
382
383static int max3110_main_thread(void *_max)
384{
385 struct uart_max3110 *max = _max;
386 wait_queue_head_t *wq = &max->wq;
387 int ret = 0;
388 struct circ_buf *xmit = &max->con_xmit;
389
390 init_waitqueue_head(wq);
391 pr_info(PR_FMT "start main thread\n");
392
393 do {
394 wait_event_interruptible(*wq, max->uart_flags || kthread_should_stop());
395
396 mutex_lock(&max->thread_mutex);
397
398 if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
399 max3110_console_receive(max);
400
401 /* first handle console output */
402 if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
403 send_circ_buf(max, xmit);
404
405 /* handle uart output */
406 if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
407 transmit_char(max);
408
409 mutex_unlock(&max->thread_mutex);
410
411 } while (!kthread_should_stop());
412
413 return ret;
414}
415
416#ifdef CONFIG_MRST_MAX3110_IRQ
417static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
418{
419 struct uart_max3110 *max = dev_id;
420
421 /* max3110's irq is a falling edge, not level triggered,
422 * so no need to disable the irq */
423 if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
424 wake_up_process(max->main_thread);
425
426 return IRQ_HANDLED;
427}
428#else
429/* if don't use RX IRQ, then need a thread to polling read */
430static int max3110_read_thread(void *_max)
431{
432 struct uart_max3110 *max = _max;
433
434 pr_info(PR_FMT "start read thread\n");
435 do {
436 mutex_lock(&max->thread_mutex);
437 max3110_console_receive(max);
438 mutex_unlock(&max->thread_mutex);
439
440 set_current_state(TASK_INTERRUPTIBLE);
441 schedule_timeout(HZ / 20);
442 } while (!kthread_should_stop());
443
444 return 0;
445}
446#endif
447
448static int serial_m3110_startup(struct uart_port *port)
449{
450 struct uart_max3110 *max =
451 container_of(port, struct uart_max3110, port);
452 u16 config = 0;
453 int ret = 0;
454
455 if (port->line != 0)
456 pr_err(PR_FMT "uart port startup failed\n");
457
458 /* firstly disable all IRQ and config it to 115200, 8n1 */
459 config = WC_TAG | WC_FIFO_ENABLE
460 | WC_1_STOPBITS
461 | WC_8BIT_WORD
462 | WC_BAUD_DR2;
463 ret = max3110_out(max, config);
464
465 /* as we use thread to handle tx/rx, need set low latency */
466 port->state->port.tty->low_latency = 1;
467
468#ifdef CONFIG_MRST_MAX3110_IRQ
469 ret = request_irq(max->irq, serial_m3110_irq,
470 IRQ_TYPE_EDGE_FALLING, "max3110", max);
471 if (ret)
472 return ret;
473
474 /* enable RX IRQ only */
475 config |= WC_RXA_IRQ_ENABLE;
476 max3110_out(max, config);
477#else
478 /* if IRQ is disabled, start a read thread for input data */
479 max->read_thread =
480 kthread_run(max3110_read_thread, max, "max3110_read");
481#endif
482
483 max->cur_conf = config;
484 return 0;
485}
486
487static void serial_m3110_shutdown(struct uart_port *port)
488{
489 struct uart_max3110 *max =
490 container_of(port, struct uart_max3110, port);
491 u16 config;
492
493 if (max->read_thread) {
494 kthread_stop(max->read_thread);
495 max->read_thread = NULL;
496 }
497
498#ifdef CONFIG_MRST_MAX3110_IRQ
499 free_irq(max->irq, max);
500#endif
501
502 /* Disable interrupts from this port */
503 config = WC_TAG | WC_SW_SHDI;
504 max3110_out(max, config);
505}
506
507static void serial_m3110_release_port(struct uart_port *port)
508{
509}
510
511static int serial_m3110_request_port(struct uart_port *port)
512{
513 return 0;
514}
515
516static void serial_m3110_config_port(struct uart_port *port, int flags)
517{
518 /* give it fake type */
519 port->type = PORT_PXA;
520}
521
522static int
523serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
524{
525 /* we don't want the core code to modify any port params */
526 return -EINVAL;
527}
528
529
530static const char *serial_m3110_type(struct uart_port *port)
531{
532 struct uart_max3110 *max =
533 container_of(port, struct uart_max3110, port);
534 return max->name;
535}
536
537static void
538serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
539 struct ktermios *old)
540{
541 struct uart_max3110 *max =
542 container_of(port, struct uart_max3110, port);
543 unsigned char cval;
544 unsigned int baud, parity = 0;
545 int clk_div = -1;
546 u16 new_conf = max->cur_conf;
547
548 switch (termios->c_cflag & CSIZE) {
549 case CS7:
550 cval = UART_LCR_WLEN7;
551 new_conf |= WC_7BIT_WORD;
552 break;
553 default:
554 case CS8:
555 cval = UART_LCR_WLEN8;
556 new_conf |= WC_8BIT_WORD;
557 break;
558 }
559
560 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
561
562 /* first calc the div for 1.8MHZ clock case */
563 switch (baud) {
564 case 300:
565 clk_div = WC_BAUD_DR384;
566 break;
567 case 600:
568 clk_div = WC_BAUD_DR192;
569 break;
570 case 1200:
571 clk_div = WC_BAUD_DR96;
572 break;
573 case 2400:
574 clk_div = WC_BAUD_DR48;
575 break;
576 case 4800:
577 clk_div = WC_BAUD_DR24;
578 break;
579 case 9600:
580 clk_div = WC_BAUD_DR12;
581 break;
582 case 19200:
583 clk_div = WC_BAUD_DR6;
584 break;
585 case 38400:
586 clk_div = WC_BAUD_DR3;
587 break;
588 case 57600:
589 clk_div = WC_BAUD_DR2;
590 break;
591 case 115200:
592 clk_div = WC_BAUD_DR1;
593 break;
594 case 230400:
595 if (max->clock & MAX3110_HIGH_CLK)
596 break;
597 default:
598 /* pick the previous baud rate */
599 baud = max->baud;
600 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
601 tty_termios_encode_baud_rate(termios, baud, baud);
602 }
603
604 if (max->clock & MAX3110_HIGH_CLK) {
605 clk_div += 1;
606 /* high clk version max3110 doesn't support B300 */
607 if (baud == 300)
608 baud = 600;
609 if (baud == 230400)
610 clk_div = WC_BAUD_DR1;
611 tty_termios_encode_baud_rate(termios, baud, baud);
612 }
613
614 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
615 if (termios->c_cflag & CSTOPB)
616 new_conf |= WC_2_STOPBITS;
617 else
618 new_conf &= ~WC_2_STOPBITS;
619
620 if (termios->c_cflag & PARENB) {
621 new_conf |= WC_PARITY_ENABLE;
622 parity |= UART_LCR_PARITY;
623 } else
624 new_conf &= ~WC_PARITY_ENABLE;
625
626 if (!(termios->c_cflag & PARODD))
627 parity |= UART_LCR_EPAR;
628 max->parity = parity;
629
630 uart_update_timeout(port, termios->c_cflag, baud);
631
632 new_conf |= WC_TAG;
633 if (new_conf != max->cur_conf) {
634 max3110_out(max, new_conf);
635 max->cur_conf = new_conf;
636 max->baud = baud;
637 }
638}
639
640/* don't handle hw handshaking */
641static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
642{
643 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
644}
645
646static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
647{
648}
649
650static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
651{
652}
653
654static void serial_m3110_pm(struct uart_port *port, unsigned int state,
655 unsigned int oldstate)
656{
657}
658
659static void serial_m3110_enable_ms(struct uart_port *port)
660{
661}
662
663struct uart_ops serial_m3110_ops = {
664 .tx_empty = serial_m3110_tx_empty,
665 .set_mctrl = serial_m3110_set_mctrl,
666 .get_mctrl = serial_m3110_get_mctrl,
667 .stop_tx = serial_m3110_stop_tx,
668 .start_tx = serial_m3110_start_tx,
669 .stop_rx = serial_m3110_stop_rx,
670 .enable_ms = serial_m3110_enable_ms,
671 .break_ctl = serial_m3110_break_ctl,
672 .startup = serial_m3110_startup,
673 .shutdown = serial_m3110_shutdown,
674 .set_termios = serial_m3110_set_termios, /* must have */
675 .pm = serial_m3110_pm,
676 .type = serial_m3110_type,
677 .release_port = serial_m3110_release_port,
678 .request_port = serial_m3110_request_port,
679 .config_port = serial_m3110_config_port,
680 .verify_port = serial_m3110_verify_port,
681};
682
683static struct uart_driver serial_m3110_reg = {
684 .owner = THIS_MODULE,
685 .driver_name = "MRST serial",
686 .dev_name = "ttyS",
687 .major = TTY_MAJOR,
688 .minor = 64,
689 .nr = 1,
690 .cons = MRST_CONSOLE,
691};
692
693static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
694{
695 return 0;
696}
697
698static int serial_m3110_resume(struct spi_device *spi)
699{
700 return 0;
701}
702
703static struct dw_spi_chip spi0_uart = {
704 .poll_mode = 1,
705 .enable_dma = 0,
706 .type = SPI_FRF_SPI,
707};
708
709static int serial_m3110_probe(struct spi_device *spi)
710{
711 struct uart_max3110 *max;
712 int ret;
713 unsigned char *buffer;
714 u16 res;
715 max = kzalloc(sizeof(*max), GFP_KERNEL);
716 if (!max)
717 return -ENOMEM;
718
719 /* set spi info */
720 spi->mode = SPI_MODE_0;
721 spi->bits_per_word = 16;
722 max->clock = MAX3110_HIGH_CLK;
723 spi->controller_data = &spi0_uart;
724
725 spi_setup(spi);
726
727 max->port.type = PORT_PXA; /* need apply for a max3110 type */
728 max->port.fifosize = 2; /* only have 16b buffer */
729 max->port.ops = &serial_m3110_ops;
730 max->port.line = 0;
731 max->port.dev = &spi->dev;
732 max->port.uartclk = 115200;
733
734 max->spi = spi;
735 max->name = spi->modalias; /* use spi name as the name */
736 max->irq = (u16)spi->irq;
737
738 mutex_init(&max->thread_mutex);
739
740 max->word_7bits = 0;
741 max->parity = 0;
742 max->baud = 0;
743
744 max->cur_conf = 0;
745 max->uart_flags = 0;
746
747 /* Check if reading configuration register returns something sane */
748
749 res = RC_TAG;
750 ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
751 if (ret < 0 || res == 0 || res == 0xffff) {
752 printk(KERN_ERR "MAX3111 deemed not present (conf reg %04x)",
753 res);
754 ret = -ENODEV;
755 goto err_get_page;
756 }
757 buffer = (unsigned char *)__get_free_page(GFP_KERNEL);
758 if (!buffer) {
759 ret = -ENOMEM;
760 goto err_get_page;
761 }
762 max->con_xmit.buf = (unsigned char *)buffer;
763 max->con_xmit.head = max->con_xmit.tail = 0;
764
765 max->main_thread = kthread_run(max3110_main_thread,
766 max, "max3110_main");
767 if (IS_ERR(max->main_thread)) {
768 ret = PTR_ERR(max->main_thread);
769 goto err_kthread;
770 }
771
772 pmax = max;
773 /* give membase a psudo value to pass serial_core's check */
774 max->port.membase = (void *)0xff110000;
775 uart_add_one_port(&serial_m3110_reg, &max->port);
776
777 return 0;
778
779err_kthread:
780 free_page((unsigned long)buffer);
781err_get_page:
782 pmax = NULL;
783 kfree(max);
784 return ret;
785}
786
787static int max3110_remove(struct spi_device *dev)
788{
789 struct uart_max3110 *max = pmax;
790
791 if (!pmax)
792 return 0;
793
794 pmax = NULL;
795 uart_remove_one_port(&serial_m3110_reg, &max->port);
796
797 free_page((unsigned long)max->con_xmit.buf);
798
799 if (max->main_thread)
800 kthread_stop(max->main_thread);
801
802 kfree(max);
803 return 0;
804}
805
806static struct spi_driver uart_max3110_driver = {
807 .driver = {
808 .name = "spi_max3111",
809 .bus = &spi_bus_type,
810 .owner = THIS_MODULE,
811 },
812 .probe = serial_m3110_probe,
813 .remove = __devexit_p(max3110_remove),
814 .suspend = serial_m3110_suspend,
815 .resume = serial_m3110_resume,
816};
817
818
819int __init serial_m3110_init(void)
820{
821 int ret = 0;
822
823 ret = uart_register_driver(&serial_m3110_reg);
824 if (ret)
825 return ret;
826
827 ret = spi_register_driver(&uart_max3110_driver);
828 if (ret)
829 uart_unregister_driver(&serial_m3110_reg);
830
831 return ret;
832}
833
834void __exit serial_m3110_exit(void)
835{
836 spi_unregister_driver(&uart_max3110_driver);
837 uart_unregister_driver(&serial_m3110_reg);
838}
839
840module_init(serial_m3110_init);
841module_exit(serial_m3110_exit);
842
843MODULE_LICENSE("GPL");
844MODULE_ALIAS("max3110-uart");
diff --git a/drivers/serial/mrst_max3110.h b/drivers/serial/mrst_max3110.h
new file mode 100644
index 00000000000..363478acb2c
--- /dev/null
+++ b/drivers/serial/mrst_max3110.h
@@ -0,0 +1,59 @@
1#ifndef _MRST_MAX3110_H
2#define _MRST_MAX3110_H
3
4#define MAX3110_HIGH_CLK 0x1 /* 3.6864 MHZ */
5#define MAX3110_LOW_CLK 0x0 /* 1.8432 MHZ */
6
7/* status bits for all 4 MAX3110 operate modes */
8#define MAX3110_READ_DATA_AVAILABLE (1 << 15)
9#define MAX3110_WRITE_BUF_EMPTY (1 << 14)
10
11#define WC_TAG (3 << 14)
12#define RC_TAG (1 << 14)
13#define WD_TAG (2 << 14)
14#define RD_TAG (0 << 14)
15
16/* bits def for write configuration */
17#define WC_FIFO_ENABLE_MASK (1 << 13)
18#define WC_FIFO_ENABLE (0 << 13)
19
20#define WC_SW_SHDI (1 << 12)
21
22#define WC_IRQ_MASK (0xF << 8)
23#define WC_TXE_IRQ_ENABLE (1 << 11) /* TX empty irq */
24#define WC_RXA_IRQ_ENABLE (1 << 10) /* RX availabe irq */
25#define WC_PAR_HIGH_IRQ_ENABLE (1 << 9)
26#define WC_REC_ACT_IRQ_ENABLE (1 << 8)
27
28#define WC_IRDA_ENABLE (1 << 7)
29
30#define WC_STOPBITS_MASK (1 << 6)
31#define WC_2_STOPBITS (1 << 6)
32#define WC_1_STOPBITS (0 << 6)
33
34#define WC_PARITY_ENABLE_MASK (1 << 5)
35#define WC_PARITY_ENABLE (1 << 5)
36
37#define WC_WORDLEN_MASK (1 << 4)
38#define WC_7BIT_WORD (1 << 4)
39#define WC_8BIT_WORD (0 << 4)
40
41#define WC_BAUD_DIV_MASK (0xF)
42#define WC_BAUD_DR1 (0x0)
43#define WC_BAUD_DR2 (0x1)
44#define WC_BAUD_DR4 (0x2)
45#define WC_BAUD_DR8 (0x3)
46#define WC_BAUD_DR16 (0x4)
47#define WC_BAUD_DR32 (0x5)
48#define WC_BAUD_DR64 (0x6)
49#define WC_BAUD_DR128 (0x7)
50#define WC_BAUD_DR3 (0x8)
51#define WC_BAUD_DR6 (0x9)
52#define WC_BAUD_DR12 (0xA)
53#define WC_BAUD_DR24 (0xB)
54#define WC_BAUD_DR48 (0xC)
55#define WC_BAUD_DR96 (0xD)
56#define WC_BAUD_DR192 (0xE)
57#define WC_BAUD_DR384 (0xF)
58
59#endif
diff --git a/drivers/serial/serial_core.c b/drivers/serial/serial_core.c
index 7f283070951..cd8511298bc 100644
--- a/drivers/serial/serial_core.c
+++ b/drivers/serial/serial_core.c
@@ -58,9 +58,9 @@ static struct lock_class_key port_lock_key;
58#define uart_console(port) (0) 58#define uart_console(port) (0)
59#endif 59#endif
60 60
61static void uart_change_speed(struct uart_state *state, 61static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
62 struct ktermios *old_termios); 62 struct ktermios *old_termios);
63static void uart_wait_until_sent(struct tty_struct *tty, int timeout); 63static void __uart_wait_until_sent(struct uart_port *port, int timeout);
64static void uart_change_pm(struct uart_state *state, int pm_state); 64static void uart_change_pm(struct uart_state *state, int pm_state);
65 65
66/* 66/*
@@ -137,7 +137,7 @@ uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
137 * Startup the port. This will be called once per open. All calls 137 * Startup the port. This will be called once per open. All calls
138 * will be serialised by the per-port mutex. 138 * will be serialised by the per-port mutex.
139 */ 139 */
140static int uart_startup(struct uart_state *state, int init_hw) 140static int uart_startup(struct tty_struct *tty, struct uart_state *state, int init_hw)
141{ 141{
142 struct uart_port *uport = state->uart_port; 142 struct uart_port *uport = state->uart_port;
143 struct tty_port *port = &state->port; 143 struct tty_port *port = &state->port;
@@ -152,7 +152,7 @@ static int uart_startup(struct uart_state *state, int init_hw)
152 * once we have successfully opened the port. Also set 152 * once we have successfully opened the port. Also set
153 * up the tty->alt_speed kludge 153 * up the tty->alt_speed kludge
154 */ 154 */
155 set_bit(TTY_IO_ERROR, &port->tty->flags); 155 set_bit(TTY_IO_ERROR, &tty->flags);
156 156
157 if (uport->type == PORT_UNKNOWN) 157 if (uport->type == PORT_UNKNOWN)
158 return 0; 158 return 0;
@@ -177,26 +177,26 @@ static int uart_startup(struct uart_state *state, int init_hw)
177 /* 177 /*
178 * Initialise the hardware port settings. 178 * Initialise the hardware port settings.
179 */ 179 */
180 uart_change_speed(state, NULL); 180 uart_change_speed(tty, state, NULL);
181 181
182 /* 182 /*
183 * Setup the RTS and DTR signals once the 183 * Setup the RTS and DTR signals once the
184 * port is open and ready to respond. 184 * port is open and ready to respond.
185 */ 185 */
186 if (port->tty->termios->c_cflag & CBAUD) 186 if (tty->termios->c_cflag & CBAUD)
187 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR); 187 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
188 } 188 }
189 189
190 if (port->flags & ASYNC_CTS_FLOW) { 190 if (port->flags & ASYNC_CTS_FLOW) {
191 spin_lock_irq(&uport->lock); 191 spin_lock_irq(&uport->lock);
192 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS)) 192 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
193 port->tty->hw_stopped = 1; 193 tty->hw_stopped = 1;
194 spin_unlock_irq(&uport->lock); 194 spin_unlock_irq(&uport->lock);
195 } 195 }
196 196
197 set_bit(ASYNCB_INITIALIZED, &port->flags); 197 set_bit(ASYNCB_INITIALIZED, &port->flags);
198 198
199 clear_bit(TTY_IO_ERROR, &port->tty->flags); 199 clear_bit(TTY_IO_ERROR, &tty->flags);
200 } 200 }
201 201
202 if (retval && capable(CAP_SYS_ADMIN)) 202 if (retval && capable(CAP_SYS_ADMIN))
@@ -210,11 +210,10 @@ static int uart_startup(struct uart_state *state, int init_hw)
210 * DTR is dropped if the hangup on close termio flag is on. Calls to 210 * DTR is dropped if the hangup on close termio flag is on. Calls to
211 * uart_shutdown are serialised by the per-port semaphore. 211 * uart_shutdown are serialised by the per-port semaphore.
212 */ 212 */
213static void uart_shutdown(struct uart_state *state) 213static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
214{ 214{
215 struct uart_port *uport = state->uart_port; 215 struct uart_port *uport = state->uart_port;
216 struct tty_port *port = &state->port; 216 struct tty_port *port = &state->port;
217 struct tty_struct *tty = port->tty;
218 217
219 /* 218 /*
220 * Set the TTY IO error marker 219 * Set the TTY IO error marker
@@ -430,11 +429,10 @@ uart_get_divisor(struct uart_port *port, unsigned int baud)
430EXPORT_SYMBOL(uart_get_divisor); 429EXPORT_SYMBOL(uart_get_divisor);
431 430
432/* FIXME: Consistent locking policy */ 431/* FIXME: Consistent locking policy */
433static void 432static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
434uart_change_speed(struct uart_state *state, struct ktermios *old_termios) 433 struct ktermios *old_termios)
435{ 434{
436 struct tty_port *port = &state->port; 435 struct tty_port *port = &state->port;
437 struct tty_struct *tty = port->tty;
438 struct uart_port *uport = state->uart_port; 436 struct uart_port *uport = state->uart_port;
439 struct ktermios *termios; 437 struct ktermios *termios;
440 438
@@ -463,8 +461,8 @@ uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
463 uport->ops->set_termios(uport, termios, old_termios); 461 uport->ops->set_termios(uport, termios, old_termios);
464} 462}
465 463
466static inline int 464static inline int __uart_put_char(struct uart_port *port,
467__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c) 465 struct circ_buf *circ, unsigned char c)
468{ 466{
469 unsigned long flags; 467 unsigned long flags;
470 int ret = 0; 468 int ret = 0;
@@ -494,8 +492,8 @@ static void uart_flush_chars(struct tty_struct *tty)
494 uart_start(tty); 492 uart_start(tty);
495} 493}
496 494
497static int 495static int uart_write(struct tty_struct *tty,
498uart_write(struct tty_struct *tty, const unsigned char *buf, int count) 496 const unsigned char *buf, int count)
499{ 497{
500 struct uart_state *state = tty->driver_data; 498 struct uart_state *state = tty->driver_data;
501 struct uart_port *port; 499 struct uart_port *port;
@@ -675,7 +673,7 @@ static int uart_get_info(struct uart_state *state,
675 return 0; 673 return 0;
676} 674}
677 675
678static int uart_set_info(struct uart_state *state, 676static int uart_set_info(struct tty_struct *tty, struct uart_state *state,
679 struct serial_struct __user *newinfo) 677 struct serial_struct __user *newinfo)
680{ 678{
681 struct serial_struct new_serial; 679 struct serial_struct new_serial;
@@ -770,7 +768,7 @@ static int uart_set_info(struct uart_state *state,
770 * We need to shutdown the serial port at the old 768 * We need to shutdown the serial port at the old
771 * port/type/irq combination. 769 * port/type/irq combination.
772 */ 770 */
773 uart_shutdown(state); 771 uart_shutdown(tty, state);
774 } 772 }
775 773
776 if (change_port) { 774 if (change_port) {
@@ -869,25 +867,27 @@ static int uart_set_info(struct uart_state *state,
869 "is deprecated.\n", current->comm, 867 "is deprecated.\n", current->comm,
870 tty_name(port->tty, buf)); 868 tty_name(port->tty, buf));
871 } 869 }
872 uart_change_speed(state, NULL); 870 uart_change_speed(tty, state, NULL);
873 } 871 }
874 } else 872 } else
875 retval = uart_startup(state, 1); 873 retval = uart_startup(tty, state, 1);
876 exit: 874 exit:
877 mutex_unlock(&port->mutex); 875 mutex_unlock(&port->mutex);
878 return retval; 876 return retval;
879} 877}
880 878
881 879/**
882/* 880 * uart_get_lsr_info - get line status register info
883 * uart_get_lsr_info - get line status register info. 881 * @tty: tty associated with the UART
884 * Note: uart_ioctl protects us against hangups. 882 * @state: UART being queried
883 * @value: returned modem value
884 *
885 * Note: uart_ioctl protects us against hangups.
885 */ 886 */
886static int uart_get_lsr_info(struct uart_state *state, 887static int uart_get_lsr_info(struct tty_struct *tty,
887 unsigned int __user *value) 888 struct uart_state *state, unsigned int __user *value)
888{ 889{
889 struct uart_port *uport = state->uart_port; 890 struct uart_port *uport = state->uart_port;
890 struct tty_port *port = &state->port;
891 unsigned int result; 891 unsigned int result;
892 892
893 result = uport->ops->tx_empty(uport); 893 result = uport->ops->tx_empty(uport);
@@ -900,7 +900,7 @@ static int uart_get_lsr_info(struct uart_state *state,
900 */ 900 */
901 if (uport->x_char || 901 if (uport->x_char ||
902 ((uart_circ_chars_pending(&state->xmit) > 0) && 902 ((uart_circ_chars_pending(&state->xmit) > 0) &&
903 !port->tty->stopped && !port->tty->hw_stopped)) 903 !tty->stopped && !tty->hw_stopped))
904 result &= ~TIOCSER_TEMT; 904 result &= ~TIOCSER_TEMT;
905 905
906 return put_user(result, value); 906 return put_user(result, value);
@@ -961,7 +961,7 @@ static int uart_break_ctl(struct tty_struct *tty, int break_state)
961 return 0; 961 return 0;
962} 962}
963 963
964static int uart_do_autoconfig(struct uart_state *state) 964static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
965{ 965{
966 struct uart_port *uport = state->uart_port; 966 struct uart_port *uport = state->uart_port;
967 struct tty_port *port = &state->port; 967 struct tty_port *port = &state->port;
@@ -980,7 +980,7 @@ static int uart_do_autoconfig(struct uart_state *state)
980 980
981 ret = -EBUSY; 981 ret = -EBUSY;
982 if (tty_port_users(port) == 1) { 982 if (tty_port_users(port) == 1) {
983 uart_shutdown(state); 983 uart_shutdown(tty, state);
984 984
985 /* 985 /*
986 * If we already have a port type configured, 986 * If we already have a port type configured,
@@ -999,7 +999,7 @@ static int uart_do_autoconfig(struct uart_state *state)
999 */ 999 */
1000 uport->ops->config_port(uport, flags); 1000 uport->ops->config_port(uport, flags);
1001 1001
1002 ret = uart_startup(state, 1); 1002 ret = uart_startup(tty, state, 1);
1003 } 1003 }
1004 mutex_unlock(&port->mutex); 1004 mutex_unlock(&port->mutex);
1005 return ret; 1005 return ret;
@@ -1122,11 +1122,11 @@ uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1122 break; 1122 break;
1123 1123
1124 case TIOCSSERIAL: 1124 case TIOCSSERIAL:
1125 ret = uart_set_info(state, uarg); 1125 ret = uart_set_info(tty, state, uarg);
1126 break; 1126 break;
1127 1127
1128 case TIOCSERCONFIG: 1128 case TIOCSERCONFIG:
1129 ret = uart_do_autoconfig(state); 1129 ret = uart_do_autoconfig(tty, state);
1130 break; 1130 break;
1131 1131
1132 case TIOCSERGWILD: /* obsolete */ 1132 case TIOCSERGWILD: /* obsolete */
@@ -1172,7 +1172,7 @@ uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1172 */ 1172 */
1173 switch (cmd) { 1173 switch (cmd) {
1174 case TIOCSERGETLSR: /* Get line status register */ 1174 case TIOCSERGETLSR: /* Get line status register */
1175 ret = uart_get_lsr_info(state, uarg); 1175 ret = uart_get_lsr_info(tty, state, uarg);
1176 break; 1176 break;
1177 1177
1178 default: { 1178 default: {
@@ -1194,7 +1194,7 @@ static void uart_set_ldisc(struct tty_struct *tty)
1194 struct uart_port *uport = state->uart_port; 1194 struct uart_port *uport = state->uart_port;
1195 1195
1196 if (uport->ops->set_ldisc) 1196 if (uport->ops->set_ldisc)
1197 uport->ops->set_ldisc(uport); 1197 uport->ops->set_ldisc(uport, tty->termios->c_line);
1198} 1198}
1199 1199
1200static void uart_set_termios(struct tty_struct *tty, 1200static void uart_set_termios(struct tty_struct *tty,
@@ -1219,7 +1219,7 @@ static void uart_set_termios(struct tty_struct *tty,
1219 return; 1219 return;
1220 } 1220 }
1221 1221
1222 uart_change_speed(state, old_termios); 1222 uart_change_speed(tty, state, old_termios);
1223 1223
1224 /* Handle transition to B0 status */ 1224 /* Handle transition to B0 status */
1225 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) 1225 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
@@ -1272,8 +1272,9 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
1272 struct uart_state *state = tty->driver_data; 1272 struct uart_state *state = tty->driver_data;
1273 struct tty_port *port; 1273 struct tty_port *port;
1274 struct uart_port *uport; 1274 struct uart_port *uport;
1275 unsigned long flags;
1275 1276
1276 BUG_ON(!kernel_locked()); 1277 BUG_ON(!tty_locked());
1277 1278
1278 if (!state) 1279 if (!state)
1279 return; 1280 return;
@@ -1284,9 +1285,12 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
1284 pr_debug("uart_close(%d) called\n", uport->line); 1285 pr_debug("uart_close(%d) called\n", uport->line);
1285 1286
1286 mutex_lock(&port->mutex); 1287 mutex_lock(&port->mutex);
1288 spin_lock_irqsave(&port->lock, flags);
1287 1289
1288 if (tty_hung_up_p(filp)) 1290 if (tty_hung_up_p(filp)) {
1291 spin_unlock_irqrestore(&port->lock, flags);
1289 goto done; 1292 goto done;
1293 }
1290 1294
1291 if ((tty->count == 1) && (port->count != 1)) { 1295 if ((tty->count == 1) && (port->count != 1)) {
1292 /* 1296 /*
@@ -1305,8 +1309,10 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
1305 tty->name, port->count); 1309 tty->name, port->count);
1306 port->count = 0; 1310 port->count = 0;
1307 } 1311 }
1308 if (port->count) 1312 if (port->count) {
1313 spin_unlock_irqrestore(&port->lock, flags);
1309 goto done; 1314 goto done;
1315 }
1310 1316
1311 /* 1317 /*
1312 * Now we wait for the transmit buffer to clear; and we notify 1318 * Now we wait for the transmit buffer to clear; and we notify
@@ -1314,9 +1320,18 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
1314 * setting tty->closing. 1320 * setting tty->closing.
1315 */ 1321 */
1316 tty->closing = 1; 1322 tty->closing = 1;
1323 spin_unlock_irqrestore(&port->lock, flags);
1317 1324
1318 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) 1325 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
1319 tty_wait_until_sent(tty, msecs_to_jiffies(port->closing_wait)); 1326 /*
1327 * hack: open-coded tty_wait_until_sent to avoid
1328 * recursive tty_lock
1329 */
1330 long timeout = msecs_to_jiffies(port->closing_wait);
1331 if (wait_event_interruptible_timeout(tty->write_wait,
1332 !tty_chars_in_buffer(tty), timeout) >= 0)
1333 __uart_wait_until_sent(uport, timeout);
1334 }
1320 1335
1321 /* 1336 /*
1322 * At this point, we stop accepting input. To do this, we 1337 * At this point, we stop accepting input. To do this, we
@@ -1332,45 +1347,47 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
1332 * has completely drained; this is especially 1347 * has completely drained; this is especially
1333 * important if there is a transmit FIFO! 1348 * important if there is a transmit FIFO!
1334 */ 1349 */
1335 uart_wait_until_sent(tty, uport->timeout); 1350 __uart_wait_until_sent(uport, uport->timeout);
1336 } 1351 }
1337 1352
1338 uart_shutdown(state); 1353 uart_shutdown(tty, state);
1339 uart_flush_buffer(tty); 1354 uart_flush_buffer(tty);
1340 1355
1341 tty_ldisc_flush(tty); 1356 tty_ldisc_flush(tty);
1342 1357
1343 tty->closing = 0;
1344 tty_port_tty_set(port, NULL); 1358 tty_port_tty_set(port, NULL);
1359 spin_lock_irqsave(&port->lock, flags);
1360 tty->closing = 0;
1345 1361
1346 if (port->blocked_open) { 1362 if (port->blocked_open) {
1363 spin_unlock_irqrestore(&port->lock, flags);
1347 if (port->close_delay) 1364 if (port->close_delay)
1348 msleep_interruptible(port->close_delay); 1365 msleep_interruptible(port->close_delay);
1366 spin_lock_irqsave(&port->lock, flags);
1349 } else if (!uart_console(uport)) { 1367 } else if (!uart_console(uport)) {
1368 spin_unlock_irqrestore(&port->lock, flags);
1350 uart_change_pm(state, 3); 1369 uart_change_pm(state, 3);
1370 spin_lock_irqsave(&port->lock, flags);
1351 } 1371 }
1352 1372
1353 /* 1373 /*
1354 * Wake up anyone trying to open this port. 1374 * Wake up anyone trying to open this port.
1355 */ 1375 */
1356 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags); 1376 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1377 spin_unlock_irqrestore(&port->lock, flags);
1357 wake_up_interruptible(&port->open_wait); 1378 wake_up_interruptible(&port->open_wait);
1358 1379
1359done: 1380done:
1360 mutex_unlock(&port->mutex); 1381 mutex_unlock(&port->mutex);
1361} 1382}
1362 1383
1363static void uart_wait_until_sent(struct tty_struct *tty, int timeout) 1384static void __uart_wait_until_sent(struct uart_port *port, int timeout)
1364{ 1385{
1365 struct uart_state *state = tty->driver_data;
1366 struct uart_port *port = state->uart_port;
1367 unsigned long char_time, expire; 1386 unsigned long char_time, expire;
1368 1387
1369 if (port->type == PORT_UNKNOWN || port->fifosize == 0) 1388 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1370 return; 1389 return;
1371 1390
1372 lock_kernel();
1373
1374 /* 1391 /*
1375 * Set the check interval to be 1/5 of the estimated time to 1392 * Set the check interval to be 1/5 of the estimated time to
1376 * send a single character, and make it at least 1. The check 1393 * send a single character, and make it at least 1. The check
@@ -1416,7 +1433,16 @@ static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1416 break; 1433 break;
1417 } 1434 }
1418 set_current_state(TASK_RUNNING); /* might not be needed */ 1435 set_current_state(TASK_RUNNING); /* might not be needed */
1419 unlock_kernel(); 1436}
1437
1438static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1439{
1440 struct uart_state *state = tty->driver_data;
1441 struct uart_port *port = state->uart_port;
1442
1443 tty_lock();
1444 __uart_wait_until_sent(port, timeout);
1445 tty_unlock();
1420} 1446}
1421 1447
1422/* 1448/*
@@ -1429,16 +1455,19 @@ static void uart_hangup(struct tty_struct *tty)
1429{ 1455{
1430 struct uart_state *state = tty->driver_data; 1456 struct uart_state *state = tty->driver_data;
1431 struct tty_port *port = &state->port; 1457 struct tty_port *port = &state->port;
1458 unsigned long flags;
1432 1459
1433 BUG_ON(!kernel_locked()); 1460 BUG_ON(!tty_locked());
1434 pr_debug("uart_hangup(%d)\n", state->uart_port->line); 1461 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
1435 1462
1436 mutex_lock(&port->mutex); 1463 mutex_lock(&port->mutex);
1437 if (port->flags & ASYNC_NORMAL_ACTIVE) { 1464 if (port->flags & ASYNC_NORMAL_ACTIVE) {
1438 uart_flush_buffer(tty); 1465 uart_flush_buffer(tty);
1439 uart_shutdown(state); 1466 uart_shutdown(tty, state);
1467 spin_lock_irqsave(&port->lock, flags);
1440 port->count = 0; 1468 port->count = 0;
1441 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags); 1469 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1470 spin_unlock_irqrestore(&port->lock, flags);
1442 tty_port_tty_set(port, NULL); 1471 tty_port_tty_set(port, NULL);
1443 wake_up_interruptible(&port->open_wait); 1472 wake_up_interruptible(&port->open_wait);
1444 wake_up_interruptible(&port->delta_msr_wait); 1473 wake_up_interruptible(&port->delta_msr_wait);
@@ -1446,15 +1475,19 @@ static void uart_hangup(struct tty_struct *tty)
1446 mutex_unlock(&port->mutex); 1475 mutex_unlock(&port->mutex);
1447} 1476}
1448 1477
1449/* 1478/**
1450 * Copy across the serial console cflag setting into the termios settings 1479 * uart_update_termios - update the terminal hw settings
1451 * for the initial open of the port. This allows continuity between the 1480 * @tty: tty associated with UART
1452 * kernel settings, and the settings init adopts when it opens the port 1481 * @state: UART to update
1453 * for the first time. 1482 *
1483 * Copy across the serial console cflag setting into the termios settings
1484 * for the initial open of the port. This allows continuity between the
1485 * kernel settings, and the settings init adopts when it opens the port
1486 * for the first time.
1454 */ 1487 */
1455static void uart_update_termios(struct uart_state *state) 1488static void uart_update_termios(struct tty_struct *tty,
1489 struct uart_state *state)
1456{ 1490{
1457 struct tty_struct *tty = state->port.tty;
1458 struct uart_port *port = state->uart_port; 1491 struct uart_port *port = state->uart_port;
1459 1492
1460 if (uart_console(port) && port->cons->cflag) { 1493 if (uart_console(port) && port->cons->cflag) {
@@ -1471,7 +1504,7 @@ static void uart_update_termios(struct uart_state *state)
1471 /* 1504 /*
1472 * Make termios settings take effect. 1505 * Make termios settings take effect.
1473 */ 1506 */
1474 uart_change_speed(state, NULL); 1507 uart_change_speed(tty, state, NULL);
1475 1508
1476 /* 1509 /*
1477 * And finally enable the RTS and DTR signals. 1510 * And finally enable the RTS and DTR signals.
@@ -1481,90 +1514,37 @@ static void uart_update_termios(struct uart_state *state)
1481 } 1514 }
1482} 1515}
1483 1516
1484/* 1517static int uart_carrier_raised(struct tty_port *port)
1485 * Block the open until the port is ready. We must be called with
1486 * the per-port semaphore held.
1487 */
1488static int
1489uart_block_til_ready(struct file *filp, struct uart_state *state)
1490{ 1518{
1491 DECLARE_WAITQUEUE(wait, current); 1519 struct uart_state *state = container_of(port, struct uart_state, port);
1492 struct uart_port *uport = state->uart_port; 1520 struct uart_port *uport = state->uart_port;
1493 struct tty_port *port = &state->port; 1521 int mctrl;
1494 unsigned int mctrl; 1522 spin_lock_irq(&uport->lock);
1495 1523 uport->ops->enable_ms(uport);
1496 port->blocked_open++; 1524 mctrl = uport->ops->get_mctrl(uport);
1497 port->count--; 1525 spin_unlock_irq(&uport->lock);
1498 1526 if (mctrl & TIOCM_CAR)
1499 add_wait_queue(&port->open_wait, &wait); 1527 return 1;
1500 while (1) { 1528 return 0;
1501 set_current_state(TASK_INTERRUPTIBLE); 1529}
1502
1503 /*
1504 * If we have been hung up, tell userspace/restart open.
1505 */
1506 if (tty_hung_up_p(filp) || port->tty == NULL)
1507 break;
1508
1509 /*
1510 * If the port has been closed, tell userspace/restart open.
1511 */
1512 if (!(port->flags & ASYNC_INITIALIZED))
1513 break;
1514 1530
1515 /* 1531static void uart_dtr_rts(struct tty_port *port, int onoff)
1516 * If non-blocking mode is set, or CLOCAL mode is set, 1532{
1517 * we don't want to wait for the modem status lines to 1533 struct uart_state *state = container_of(port, struct uart_state, port);
1518 * indicate that the port is ready. 1534 struct uart_port *uport = state->uart_port;
1519 *
1520 * Also, if the port is not enabled/configured, we want
1521 * to allow the open to succeed here. Note that we will
1522 * have set TTY_IO_ERROR for a non-existant port.
1523 */
1524 if ((filp->f_flags & O_NONBLOCK) ||
1525 (port->tty->termios->c_cflag & CLOCAL) ||
1526 (port->tty->flags & (1 << TTY_IO_ERROR)))
1527 break;
1528 1535
1529 /* 1536 if (onoff) {
1530 * Set DTR to allow modem to know we're waiting. Do 1537 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1531 * not set RTS here - we want to make sure we catch
1532 * the data from the modem.
1533 */
1534 if (port->tty->termios->c_cflag & CBAUD)
1535 uart_set_mctrl(uport, TIOCM_DTR);
1536 1538
1537 /* 1539 /*
1538 * and wait for the carrier to indicate that the 1540 * If this is the first open to succeed,
1539 * modem is ready for us. 1541 * adjust things to suit.
1540 */ 1542 */
1541 spin_lock_irq(&uport->lock); 1543 if (!test_and_set_bit(ASYNCB_NORMAL_ACTIVE, &port->flags))
1542 uport->ops->enable_ms(uport); 1544 uart_update_termios(port->tty, state);
1543 mctrl = uport->ops->get_mctrl(uport);
1544 spin_unlock_irq(&uport->lock);
1545 if (mctrl & TIOCM_CAR)
1546 break;
1547
1548 mutex_unlock(&port->mutex);
1549 schedule();
1550 mutex_lock(&port->mutex);
1551
1552 if (signal_pending(current))
1553 break;
1554 } 1545 }
1555 set_current_state(TASK_RUNNING); 1546 else
1556 remove_wait_queue(&port->open_wait, &wait); 1547 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1557
1558 port->count++;
1559 port->blocked_open--;
1560
1561 if (signal_pending(current))
1562 return -ERESTARTSYS;
1563
1564 if (!port->tty || tty_hung_up_p(filp))
1565 return -EAGAIN;
1566
1567 return 0;
1568} 1548}
1569 1549
1570static struct uart_state *uart_get(struct uart_driver *drv, int line) 1550static struct uart_state *uart_get(struct uart_driver *drv, int line)
@@ -1611,7 +1591,7 @@ static int uart_open(struct tty_struct *tty, struct file *filp)
1611 struct tty_port *port; 1591 struct tty_port *port;
1612 int retval, line = tty->index; 1592 int retval, line = tty->index;
1613 1593
1614 BUG_ON(!kernel_locked()); 1594 BUG_ON(!tty_locked());
1615 pr_debug("uart_open(%d) called\n", line); 1595 pr_debug("uart_open(%d) called\n", line);
1616 1596
1617 /* 1597 /*
@@ -1668,23 +1648,14 @@ static int uart_open(struct tty_struct *tty, struct file *filp)
1668 /* 1648 /*
1669 * Start up the serial port. 1649 * Start up the serial port.
1670 */ 1650 */
1671 retval = uart_startup(state, 0); 1651 retval = uart_startup(tty, state, 0);
1672 1652
1673 /* 1653 /*
1674 * If we succeeded, wait until the port is ready. 1654 * If we succeeded, wait until the port is ready.
1675 */ 1655 */
1676 if (retval == 0)
1677 retval = uart_block_til_ready(filp, state);
1678 mutex_unlock(&port->mutex); 1656 mutex_unlock(&port->mutex);
1679 1657 if (retval == 0)
1680 /* 1658 retval = tty_port_block_til_ready(port, tty, filp);
1681 * If this is the first open to succeed, adjust things to suit.
1682 */
1683 if (retval == 0 && !(port->flags & ASYNC_NORMAL_ACTIVE)) {
1684 set_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1685
1686 uart_update_termios(state);
1687 }
1688 1659
1689fail: 1660fail:
1690 return retval; 1661 return retval;
@@ -2010,9 +1981,13 @@ int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2010 struct tty_port *port = &state->port; 1981 struct tty_port *port = &state->port;
2011 struct device *tty_dev; 1982 struct device *tty_dev;
2012 struct uart_match match = {uport, drv}; 1983 struct uart_match match = {uport, drv};
1984 struct tty_struct *tty;
2013 1985
2014 mutex_lock(&port->mutex); 1986 mutex_lock(&port->mutex);
2015 1987
1988 /* Must be inside the mutex lock until we convert to tty_port */
1989 tty = port->tty;
1990
2016 tty_dev = device_find_child(uport->dev, &match, serial_match_port); 1991 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2017 if (device_may_wakeup(tty_dev)) { 1992 if (device_may_wakeup(tty_dev)) {
2018 enable_irq_wake(uport->irq); 1993 enable_irq_wake(uport->irq);
@@ -2105,9 +2080,12 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2105 ops->set_mctrl(uport, 0); 2080 ops->set_mctrl(uport, 0);
2106 spin_unlock_irq(&uport->lock); 2081 spin_unlock_irq(&uport->lock);
2107 if (console_suspend_enabled || !uart_console(uport)) { 2082 if (console_suspend_enabled || !uart_console(uport)) {
2083 /* Protected by port mutex for now */
2084 struct tty_struct *tty = port->tty;
2108 ret = ops->startup(uport); 2085 ret = ops->startup(uport);
2109 if (ret == 0) { 2086 if (ret == 0) {
2110 uart_change_speed(state, NULL); 2087 if (tty)
2088 uart_change_speed(tty, state, NULL);
2111 spin_lock_irq(&uport->lock); 2089 spin_lock_irq(&uport->lock);
2112 ops->set_mctrl(uport, uport->mctrl); 2090 ops->set_mctrl(uport, uport->mctrl);
2113 ops->start_tx(uport); 2091 ops->start_tx(uport);
@@ -2119,7 +2097,7 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2119 * Clear the "initialized" flag so we won't try 2097 * Clear the "initialized" flag so we won't try
2120 * to call the low level drivers shutdown method. 2098 * to call the low level drivers shutdown method.
2121 */ 2099 */
2122 uart_shutdown(state); 2100 uart_shutdown(tty, state);
2123 } 2101 }
2124 } 2102 }
2125 2103
@@ -2312,6 +2290,11 @@ static const struct tty_operations uart_ops = {
2312#endif 2290#endif
2313}; 2291};
2314 2292
2293static const struct tty_port_operations uart_port_ops = {
2294 .carrier_raised = uart_carrier_raised,
2295 .dtr_rts = uart_dtr_rts,
2296};
2297
2315/** 2298/**
2316 * uart_register_driver - register a driver with the uart core layer 2299 * uart_register_driver - register a driver with the uart core layer
2317 * @drv: low level driver structure 2300 * @drv: low level driver structure
@@ -2368,6 +2351,7 @@ int uart_register_driver(struct uart_driver *drv)
2368 struct tty_port *port = &state->port; 2351 struct tty_port *port = &state->port;
2369 2352
2370 tty_port_init(port); 2353 tty_port_init(port);
2354 port->ops = &uart_port_ops;
2371 port->close_delay = 500; /* .5 seconds */ 2355 port->close_delay = 500; /* .5 seconds */
2372 port->closing_wait = 30000; /* 30 seconds */ 2356 port->closing_wait = 30000; /* 30 seconds */
2373 tasklet_init(&state->tlet, uart_tasklet_action, 2357 tasklet_init(&state->tlet, uart_tasklet_action,
diff --git a/drivers/serial/timbuart.c b/drivers/serial/timbuart.c
index 67ca642713b..1f36b7eb735 100644
--- a/drivers/serial/timbuart.c
+++ b/drivers/serial/timbuart.c
@@ -423,7 +423,7 @@ static struct uart_driver timbuart_driver = {
423 .nr = 1 423 .nr = 1
424}; 424};
425 425
426static int timbuart_probe(struct platform_device *dev) 426static int __devinit timbuart_probe(struct platform_device *dev)
427{ 427{
428 int err, irq; 428 int err, irq;
429 struct timbuart_port *uart; 429 struct timbuart_port *uart;
@@ -489,7 +489,7 @@ err_mem:
489 return err; 489 return err;
490} 490}
491 491
492static int timbuart_remove(struct platform_device *dev) 492static int __devexit timbuart_remove(struct platform_device *dev)
493{ 493{
494 struct timbuart_port *uart = platform_get_drvdata(dev); 494 struct timbuart_port *uart = platform_get_drvdata(dev);
495 495
@@ -507,7 +507,7 @@ static struct platform_driver timbuart_platform_driver = {
507 .owner = THIS_MODULE, 507 .owner = THIS_MODULE,
508 }, 508 },
509 .probe = timbuart_probe, 509 .probe = timbuart_probe,
510 .remove = timbuart_remove, 510 .remove = __devexit_p(timbuart_remove),
511}; 511};
512 512
513/*--------------------------------------------------------------------------*/ 513/*--------------------------------------------------------------------------*/
diff --git a/drivers/staging/easycap/easycap.h b/drivers/staging/easycap/easycap.h
index ad836d2d26f..f3c827eb0ab 100644
--- a/drivers/staging/easycap/easycap.h
+++ b/drivers/staging/easycap/easycap.h
@@ -463,15 +463,12 @@ struct data_buffer audio_buffer[];
463void easycap_complete(struct urb *); 463void easycap_complete(struct urb *);
464int easycap_open(struct inode *, struct file *); 464int easycap_open(struct inode *, struct file *);
465int easycap_release(struct inode *, struct file *); 465int easycap_release(struct inode *, struct file *);
466int easycap_ioctl(struct inode *, struct file *, \ 466long easycap_ioctl(struct file *, unsigned int, unsigned long);
467 unsigned int, unsigned long);
468 467
469/*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/ 468/*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
470#if defined(EASYCAP_IS_VIDEODEV_CLIENT) 469#if defined(EASYCAP_IS_VIDEODEV_CLIENT)
471int easycap_open_noinode(struct file *); 470int easycap_open_noinode(struct file *);
472int easycap_release_noinode(struct file *); 471int easycap_release_noinode(struct file *);
473long easycap_ioctl_noinode(struct file *, \
474 unsigned int, unsigned long);
475int videodev_release(struct video_device *); 472int videodev_release(struct video_device *);
476#endif /*EASYCAP_IS_VIDEODEV_CLIENT*/ 473#endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
477/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/ 474/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
@@ -515,8 +512,7 @@ void easysnd_complete(struct urb *);
515ssize_t easysnd_read(struct file *, char __user *, size_t, loff_t *); 512ssize_t easysnd_read(struct file *, char __user *, size_t, loff_t *);
516int easysnd_open(struct inode *, struct file *); 513int easysnd_open(struct inode *, struct file *);
517int easysnd_release(struct inode *, struct file *); 514int easysnd_release(struct inode *, struct file *);
518int easysnd_ioctl(struct inode *, struct file *, \ 515long easysnd_ioctl(struct file *, unsigned int, unsigned long);
519 unsigned int, unsigned long);
520unsigned int easysnd_poll(struct file *, poll_table *); 516unsigned int easysnd_poll(struct file *, poll_table *);
521void easysnd_delete(struct kref *); 517void easysnd_delete(struct kref *);
522int submit_audio_urbs(struct easycap *); 518int submit_audio_urbs(struct easycap *);
diff --git a/drivers/staging/easycap/easycap_ioctl.c b/drivers/staging/easycap/easycap_ioctl.c
index 276b63dfe27..9a42ae02cd5 100644
--- a/drivers/staging/easycap/easycap_ioctl.c
+++ b/drivers/staging/easycap/easycap_ioctl.c
@@ -25,6 +25,7 @@
25*/ 25*/
26/*****************************************************************************/ 26/*****************************************************************************/
27 27
28#include <linux/smp_lock.h>
28#include "easycap.h" 29#include "easycap.h"
29#include "easycap_debug.h" 30#include "easycap_debug.h"
30#include "easycap_standard.h" 31#include "easycap_standard.h"
@@ -773,19 +774,10 @@ while (0xFFFFFFFF != easycap_control[i1].id) {
773SAY("WARNING: failed to adjust mute: control not found\n"); 774SAY("WARNING: failed to adjust mute: control not found\n");
774return -ENOENT; 775return -ENOENT;
775} 776}
776/****************************************************************************/ 777
777/*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
778#if defined(EASYCAP_IS_VIDEODEV_CLIENT)
779long
780easycap_ioctl_noinode(struct file *file, unsigned int cmd, unsigned long arg)\
781 {
782 return easycap_ioctl((struct inode *)NULL, file, cmd, arg);
783}
784#endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
785/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
786/*--------------------------------------------------------------------------*/ 778/*--------------------------------------------------------------------------*/
787int easycap_ioctl(struct inode *inode, struct file *file, \ 779static int easycap_ioctl_bkl(struct inode *inode, struct file *file,
788 unsigned int cmd, unsigned long arg) 780 unsigned int cmd, unsigned long arg)
789{ 781{
790static struct easycap *peasycap; 782static struct easycap *peasycap;
791static struct usb_device *p; 783static struct usb_device *p;
@@ -1956,19 +1948,22 @@ default: {
1956} 1948}
1957return 0; 1949return 0;
1958} 1950}
1959/****************************************************************************/ 1951
1960/*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/ 1952long easycap_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1961#if defined(EASYCAP_IS_VIDEODEV_CLIENT)
1962long
1963easysnd_ioctl_noinode(struct file *file, unsigned int cmd, unsigned long arg)
1964{ 1953{
1965 return easysnd_ioctl((struct inode *)NULL, file, cmd, arg); 1954 struct inode *inode = file->f_dentry->d_inode;
1955 long ret;
1956
1957 lock_kernel();
1958 ret = easycap_ioctl_bkl(inode, file, cmd, arg);
1959 unlock_kernel();
1960
1961 return ret;
1966} 1962}
1967#endif /*EASYCAP_IS_VIDEODEV_CLIENT*/ 1963
1968/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
1969/*--------------------------------------------------------------------------*/ 1964/*--------------------------------------------------------------------------*/
1970int easysnd_ioctl(struct inode *inode, struct file *file, \ 1965static int easysnd_ioctl_bkl(struct inode *inode, struct file *file,
1971 unsigned int cmd, unsigned long arg) 1966 unsigned int cmd, unsigned long arg)
1972{ 1967{
1973struct easycap *peasycap; 1968struct easycap *peasycap;
1974struct usb_device *p; 1969struct usb_device *p;
@@ -2158,6 +2153,19 @@ default: {
2158} 2153}
2159return 0; 2154return 0;
2160} 2155}
2156
2157long easysnd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2158{
2159 struct inode *inode = file->f_dentry->d_inode;
2160 long ret;
2161
2162 lock_kernel();
2163 ret = easysnd_ioctl_bkl(inode, file, cmd, arg);
2164 unlock_kernel();
2165
2166 return ret;
2167}
2168
2161/*****************************************************************************/ 2169/*****************************************************************************/
2162int explain_ioctl(__u32 wot) 2170int explain_ioctl(__u32 wot)
2163{ 2171{
diff --git a/drivers/staging/easycap/easycap_main.c b/drivers/staging/easycap/easycap_main.c
index 09c194ce10a..5a4bbd9b453 100644
--- a/drivers/staging/easycap/easycap_main.c
+++ b/drivers/staging/easycap/easycap_main.c
@@ -60,13 +60,13 @@ struct usb_driver easycap_usb_driver = {
60 */ 60 */
61/*---------------------------------------------------------------------------*/ 61/*---------------------------------------------------------------------------*/
62const struct file_operations easycap_fops = { 62const struct file_operations easycap_fops = {
63.owner = THIS_MODULE, 63 .owner = THIS_MODULE,
64.open = easycap_open, 64 .open = easycap_open,
65.release = easycap_release, 65 .release = easycap_release,
66.ioctl = easycap_ioctl, 66 .unlocked_ioctl = easycap_ioctl,
67.poll = easycap_poll, 67 .poll = easycap_poll,
68.mmap = easycap_mmap, 68 .mmap = easycap_mmap,
69.llseek = no_llseek, 69 .llseek = no_llseek,
70}; 70};
71struct vm_operations_struct easycap_vm_ops = { 71struct vm_operations_struct easycap_vm_ops = {
72.open = easycap_vma_open, 72.open = easycap_vma_open,
@@ -83,12 +83,12 @@ struct usb_class_driver easycap_class = {
83#if defined(EASYCAP_IS_VIDEODEV_CLIENT) 83#if defined(EASYCAP_IS_VIDEODEV_CLIENT)
84#if defined(EASYCAP_NEEDS_V4L2_FOPS) 84#if defined(EASYCAP_NEEDS_V4L2_FOPS)
85const struct v4l2_file_operations v4l2_fops = { 85const struct v4l2_file_operations v4l2_fops = {
86.owner = THIS_MODULE, 86 .owner = THIS_MODULE,
87.open = easycap_open_noinode, 87 .open = easycap_open_noinode,
88.release = easycap_release_noinode, 88 .release = easycap_release_noinode,
89.ioctl = easycap_ioctl_noinode, 89 .unlocked_ioctl = easycap_ioctl,
90.poll = easycap_poll, 90 .poll = easycap_poll,
91.mmap = easycap_mmap, 91 .mmap = easycap_mmap,
92}; 92};
93#endif /*EASYCAP_NEEDS_V4L2_FOPS*/ 93#endif /*EASYCAP_NEEDS_V4L2_FOPS*/
94int video_device_many /*=0*/; 94int video_device_many /*=0*/;
@@ -102,12 +102,12 @@ struct video_device *pvideo_array[VIDEO_DEVICE_MANY], *pvideo_device;
102 */ 102 */
103/*--------------------------------------------------------------------------*/ 103/*--------------------------------------------------------------------------*/
104const struct file_operations easysnd_fops = { 104const struct file_operations easysnd_fops = {
105.owner = THIS_MODULE, 105 .owner = THIS_MODULE,
106.open = easysnd_open, 106 .open = easysnd_open,
107.release = easysnd_release, 107 .release = easysnd_release,
108.ioctl = easysnd_ioctl, 108 .unlocked_ioctl = easysnd_ioctl,
109.read = easysnd_read, 109 .read = easysnd_read,
110.llseek = no_llseek, 110 .llseek = no_llseek,
111}; 111};
112struct usb_class_driver easysnd_class = { 112struct usb_class_driver easysnd_class = {
113.name = "usb/easysnd%d", 113.name = "usb/easysnd%d",
diff --git a/drivers/staging/hv/blkvsc_drv.c b/drivers/staging/hv/blkvsc_drv.c
index f7ea2a3efed..ff1d24720f1 100644
--- a/drivers/staging/hv/blkvsc_drv.c
+++ b/drivers/staging/hv/blkvsc_drv.c
@@ -25,6 +25,7 @@
25#include <linux/major.h> 25#include <linux/major.h>
26#include <linux/delay.h> 26#include <linux/delay.h>
27#include <linux/hdreg.h> 27#include <linux/hdreg.h>
28#include <linux/smp_lock.h>
28#include <linux/slab.h> 29#include <linux/slab.h>
29#include <scsi/scsi.h> 30#include <scsi/scsi.h>
30#include <scsi/scsi_cmnd.h> 31#include <scsi/scsi_cmnd.h>
@@ -805,7 +806,8 @@ static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
805 blkvsc_req->cmnd[0] = READ_16; 806 blkvsc_req->cmnd[0] = READ_16;
806 } 807 }
807 808
808 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0; 809 blkvsc_req->cmnd[1] |=
810 (blkvsc_req->req->cmd_flags & REQ_FUA) ? 0x8 : 0;
809 811
810 *(unsigned long long *)&blkvsc_req->cmnd[2] = 812 *(unsigned long long *)&blkvsc_req->cmnd[2] =
811 cpu_to_be64(blkvsc_req->sector_start); 813 cpu_to_be64(blkvsc_req->sector_start);
@@ -821,7 +823,8 @@ static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
821 blkvsc_req->cmnd[0] = READ_10; 823 blkvsc_req->cmnd[0] = READ_10;
822 } 824 }
823 825
824 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0; 826 blkvsc_req->cmnd[1] |=
827 (blkvsc_req->req->cmd_flags & REQ_FUA) ? 0x8 : 0;
825 828
826 *(unsigned int *)&blkvsc_req->cmnd[2] = 829 *(unsigned int *)&blkvsc_req->cmnd[2] =
827 cpu_to_be32(blkvsc_req->sector_start); 830 cpu_to_be32(blkvsc_req->sector_start);
@@ -1268,7 +1271,7 @@ static void blkvsc_request(struct request_queue *queue)
1268 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req); 1271 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1269 1272
1270 blkdev = req->rq_disk->private_data; 1273 blkdev = req->rq_disk->private_data;
1271 if (blkdev->shutting_down || !blk_fs_request(req) || 1274 if (blkdev->shutting_down || req->cmd_type != REQ_TYPE_FS ||
1272 blkdev->media_not_present) { 1275 blkdev->media_not_present) {
1273 __blk_end_request_cur(req, 0); 1276 __blk_end_request_cur(req, 0);
1274 continue; 1277 continue;
@@ -1306,6 +1309,7 @@ static int blkvsc_open(struct block_device *bdev, fmode_t mode)
1306 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users, 1309 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1307 blkdev->gd->disk_name); 1310 blkdev->gd->disk_name);
1308 1311
1312 lock_kernel();
1309 spin_lock(&blkdev->lock); 1313 spin_lock(&blkdev->lock);
1310 1314
1311 if (!blkdev->users && blkdev->device_type == DVD_TYPE) { 1315 if (!blkdev->users && blkdev->device_type == DVD_TYPE) {
@@ -1317,6 +1321,7 @@ static int blkvsc_open(struct block_device *bdev, fmode_t mode)
1317 blkdev->users++; 1321 blkdev->users++;
1318 1322
1319 spin_unlock(&blkdev->lock); 1323 spin_unlock(&blkdev->lock);
1324 unlock_kernel();
1320 return 0; 1325 return 0;
1321} 1326}
1322 1327
@@ -1327,6 +1332,7 @@ static int blkvsc_release(struct gendisk *disk, fmode_t mode)
1327 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users, 1332 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1328 blkdev->gd->disk_name); 1333 blkdev->gd->disk_name);
1329 1334
1335 lock_kernel();
1330 spin_lock(&blkdev->lock); 1336 spin_lock(&blkdev->lock);
1331 if (blkdev->users == 1) { 1337 if (blkdev->users == 1) {
1332 spin_unlock(&blkdev->lock); 1338 spin_unlock(&blkdev->lock);
@@ -1337,6 +1343,7 @@ static int blkvsc_release(struct gendisk *disk, fmode_t mode)
1337 blkdev->users--; 1343 blkdev->users--;
1338 1344
1339 spin_unlock(&blkdev->lock); 1345 spin_unlock(&blkdev->lock);
1346 unlock_kernel();
1340 return 0; 1347 return 0;
1341} 1348}
1342 1349
diff --git a/drivers/staging/lirc/Kconfig b/drivers/staging/lirc/Kconfig
index 968c2adee06..100c4d4b812 100644
--- a/drivers/staging/lirc/Kconfig
+++ b/drivers/staging/lirc/Kconfig
@@ -3,6 +3,7 @@
3# 3#
4menuconfig LIRC_STAGING 4menuconfig LIRC_STAGING
5 bool "Linux Infrared Remote Control IR receiver/transmitter drivers" 5 bool "Linux Infrared Remote Control IR receiver/transmitter drivers"
6 depends on LIRC
6 help 7 help
7 Say Y here, and all supported Linux Infrared Remote Control IR and 8 Say Y here, and all supported Linux Infrared Remote Control IR and
8 RF receiver and transmitter drivers will be displayed. When paired 9 RF receiver and transmitter drivers will be displayed. When paired
@@ -13,21 +14,13 @@ if LIRC_STAGING
13 14
14config LIRC_BT829 15config LIRC_BT829
15 tristate "BT829 based hardware" 16 tristate "BT829 based hardware"
16 depends on LIRC_STAGING 17 depends on LIRC_STAGING && PCI
17 help 18 help
18 Driver for the IR interface on BT829-based hardware 19 Driver for the IR interface on BT829-based hardware
19 20
20config LIRC_ENE0100
21 tristate "ENE KB3924/ENE0100 CIR Port Reciever"
22 depends on LIRC_STAGING
23 help
24 This is a driver for CIR port handled by ENE KB3924 embedded
25 controller found on some notebooks.
26 It appears on PNP list as ENE0100.
27
28config LIRC_I2C 21config LIRC_I2C
29 tristate "I2C Based IR Receivers" 22 tristate "I2C Based IR Receivers"
30 depends on LIRC_STAGING 23 depends on LIRC_STAGING && I2C
31 help 24 help
32 Driver for I2C-based IR receivers, such as those commonly 25 Driver for I2C-based IR receivers, such as those commonly
33 found onboard Hauppauge PVR-150/250/350 video capture cards 26 found onboard Hauppauge PVR-150/250/350 video capture cards
@@ -40,7 +33,7 @@ config LIRC_IGORPLUGUSB
40 33
41config LIRC_IMON 34config LIRC_IMON
42 tristate "Legacy SoundGraph iMON Receiver and Display" 35 tristate "Legacy SoundGraph iMON Receiver and Display"
43 depends on LIRC_STAGING 36 depends on LIRC_STAGING && USB
44 help 37 help
45 Driver for the original SoundGraph iMON IR Receiver and Display 38 Driver for the original SoundGraph iMON IR Receiver and Display
46 39
@@ -48,7 +41,7 @@ config LIRC_IMON
48 41
49config LIRC_IT87 42config LIRC_IT87
50 tristate "ITE IT87XX CIR Port Receiver" 43 tristate "ITE IT87XX CIR Port Receiver"
51 depends on LIRC_STAGING 44 depends on LIRC_STAGING && PNP
52 help 45 help
53 Driver for the ITE IT87xx IR Receiver 46 Driver for the ITE IT87xx IR Receiver
54 47
@@ -60,13 +53,13 @@ config LIRC_ITE8709
60 53
61config LIRC_PARALLEL 54config LIRC_PARALLEL
62 tristate "Homebrew Parallel Port Receiver" 55 tristate "Homebrew Parallel Port Receiver"
63 depends on LIRC_STAGING && !SMP 56 depends on LIRC_STAGING && PARPORT && !SMP
64 help 57 help
65 Driver for Homebrew Parallel Port Receivers 58 Driver for Homebrew Parallel Port Receivers
66 59
67config LIRC_SASEM 60config LIRC_SASEM
68 tristate "Sasem USB IR Remote" 61 tristate "Sasem USB IR Remote"
69 depends on LIRC_STAGING 62 depends on LIRC_STAGING && USB
70 help 63 help
71 Driver for the Sasem OnAir Remocon-V or Dign HV5 HTPC IR/VFD Module 64 Driver for the Sasem OnAir Remocon-V or Dign HV5 HTPC IR/VFD Module
72 65
@@ -89,12 +82,6 @@ config LIRC_SIR
89 help 82 help
90 Driver for the SIR IrDA port 83 Driver for the SIR IrDA port
91 84
92config LIRC_STREAMZAP
93 tristate "Streamzap PC Receiver"
94 depends on LIRC_STAGING
95 help
96 Driver for the Streamzap PC Receiver
97
98config LIRC_TTUSBIR 85config LIRC_TTUSBIR
99 tristate "Technotrend USB IR Receiver" 86 tristate "Technotrend USB IR Receiver"
100 depends on LIRC_STAGING && USB 87 depends on LIRC_STAGING && USB
@@ -103,7 +90,7 @@ config LIRC_TTUSBIR
103 90
104config LIRC_ZILOG 91config LIRC_ZILOG
105 tristate "Zilog/Hauppauge IR Transmitter" 92 tristate "Zilog/Hauppauge IR Transmitter"
106 depends on LIRC_STAGING 93 depends on LIRC_STAGING && I2C
107 help 94 help
108 Driver for the Zilog/Hauppauge IR Transmitter, found on 95 Driver for the Zilog/Hauppauge IR Transmitter, found on
109 PVR-150/500, HVR-1200/1250/1700/1800, HD-PVR and other cards 96 PVR-150/500, HVR-1200/1250/1700/1800, HD-PVR and other cards
diff --git a/drivers/staging/lirc/Makefile b/drivers/staging/lirc/Makefile
index a019182a7a3..4da1f3397a1 100644
--- a/drivers/staging/lirc/Makefile
+++ b/drivers/staging/lirc/Makefile
@@ -4,7 +4,6 @@
4# Each configuration option enables a list of files. 4# Each configuration option enables a list of files.
5 5
6obj-$(CONFIG_LIRC_BT829) += lirc_bt829.o 6obj-$(CONFIG_LIRC_BT829) += lirc_bt829.o
7obj-$(CONFIG_LIRC_ENE0100) += lirc_ene0100.o
8obj-$(CONFIG_LIRC_I2C) += lirc_i2c.o 7obj-$(CONFIG_LIRC_I2C) += lirc_i2c.o
9obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o 8obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
10obj-$(CONFIG_LIRC_IMON) += lirc_imon.o 9obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
@@ -14,6 +13,5 @@ obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
14obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o 13obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
15obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o 14obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
16obj-$(CONFIG_LIRC_SIR) += lirc_sir.o 15obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
17obj-$(CONFIG_LIRC_STREAMZAP) += lirc_streamzap.o
18obj-$(CONFIG_LIRC_TTUSBIR) += lirc_ttusbir.o 16obj-$(CONFIG_LIRC_TTUSBIR) += lirc_ttusbir.o
19obj-$(CONFIG_LIRC_ZILOG) += lirc_zilog.o 17obj-$(CONFIG_LIRC_ZILOG) += lirc_zilog.o
diff --git a/drivers/staging/lirc/lirc_ene0100.c b/drivers/staging/lirc/lirc_ene0100.c
deleted file mode 100644
index a152c52b074..00000000000
--- a/drivers/staging/lirc/lirc_ene0100.c
+++ /dev/null
@@ -1,646 +0,0 @@
1/*
2 * driver for ENE KB3926 B/C/D CIR (also known as ENE0100)
3 *
4 * Copyright (C) 2009 Maxim Levitsky <maximlevitsky@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
19 * USA
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/pnp.h>
25#include <linux/io.h>
26#include <linux/interrupt.h>
27#include <linux/sched.h>
28#include "lirc_ene0100.h"
29
30static int sample_period = 75;
31static int enable_idle = 1;
32static int enable_learning;
33
34static void ene_set_idle(struct ene_device *dev, int idle);
35static void ene_set_inputs(struct ene_device *dev, int enable);
36
37/* read a hardware register */
38static u8 ene_hw_read_reg(struct ene_device *dev, u16 reg)
39{
40 outb(reg >> 8, dev->hw_io + ENE_ADDR_HI);
41 outb(reg & 0xFF, dev->hw_io + ENE_ADDR_LO);
42 return inb(dev->hw_io + ENE_IO);
43}
44
45/* write a hardware register */
46static void ene_hw_write_reg(struct ene_device *dev, u16 reg, u8 value)
47{
48 outb(reg >> 8, dev->hw_io + ENE_ADDR_HI);
49 outb(reg & 0xFF, dev->hw_io + ENE_ADDR_LO);
50 outb(value, dev->hw_io + ENE_IO);
51}
52
53/* change specific bits in hardware register */
54static void ene_hw_write_reg_mask(struct ene_device *dev,
55 u16 reg, u8 value, u8 mask)
56{
57 u8 regvalue;
58
59 outb(reg >> 8, dev->hw_io + ENE_ADDR_HI);
60 outb(reg & 0xFF, dev->hw_io + ENE_ADDR_LO);
61
62 regvalue = inb(dev->hw_io + ENE_IO) & ~mask;
63 regvalue |= (value & mask);
64 outb(regvalue, dev->hw_io + ENE_IO);
65}
66
67/* read irq status and ack it */
68static int ene_hw_irq_status(struct ene_device *dev, int *buffer_pointer)
69{
70 u8 irq_status;
71 u8 fw_flags1, fw_flags2;
72
73 fw_flags2 = ene_hw_read_reg(dev, ENE_FW2);
74
75 if (buffer_pointer)
76 *buffer_pointer = 4 * (fw_flags2 & ENE_FW2_BUF_HIGH);
77
78 if (dev->hw_revision < ENE_HW_C) {
79 irq_status = ene_hw_read_reg(dev, ENEB_IRQ_STATUS);
80
81 if (!(irq_status & ENEB_IRQ_STATUS_IR))
82 return 0;
83 ene_hw_write_reg(dev, ENEB_IRQ_STATUS,
84 irq_status & ~ENEB_IRQ_STATUS_IR);
85
86 /* rev B support only recieving */
87 return ENE_IRQ_RX;
88 }
89
90 irq_status = ene_hw_read_reg(dev, ENEC_IRQ);
91
92 if (!(irq_status & ENEC_IRQ_STATUS))
93 return 0;
94
95 /* original driver does that twice - a workaround ? */
96 ene_hw_write_reg(dev, ENEC_IRQ, irq_status & ~ENEC_IRQ_STATUS);
97 ene_hw_write_reg(dev, ENEC_IRQ, irq_status & ~ENEC_IRQ_STATUS);
98
99 /* clear unknown flag in F8F9 */
100 if (fw_flags2 & ENE_FW2_IRQ_CLR)
101 ene_hw_write_reg(dev, ENE_FW2, fw_flags2 & ~ENE_FW2_IRQ_CLR);
102
103 /* check if this is a TX interrupt */
104 fw_flags1 = ene_hw_read_reg(dev, ENE_FW1);
105
106 if (fw_flags1 & ENE_FW1_TXIRQ) {
107 ene_hw_write_reg(dev, ENE_FW1, fw_flags1 & ~ENE_FW1_TXIRQ);
108 return ENE_IRQ_TX;
109 } else
110 return ENE_IRQ_RX;
111}
112
113static int ene_hw_detect(struct ene_device *dev)
114{
115 u8 chip_major, chip_minor;
116 u8 hw_revision, old_ver;
117 u8 tmp;
118 u8 fw_capabilities;
119
120 tmp = ene_hw_read_reg(dev, ENE_HW_UNK);
121 ene_hw_write_reg(dev, ENE_HW_UNK, tmp & ~ENE_HW_UNK_CLR);
122
123 chip_major = ene_hw_read_reg(dev, ENE_HW_VER_MAJOR);
124 chip_minor = ene_hw_read_reg(dev, ENE_HW_VER_MINOR);
125
126 ene_hw_write_reg(dev, ENE_HW_UNK, tmp);
127 hw_revision = ene_hw_read_reg(dev, ENE_HW_VERSION);
128 old_ver = ene_hw_read_reg(dev, ENE_HW_VER_OLD);
129
130 if (hw_revision == 0xFF) {
131
132 ene_printk(KERN_WARNING, "device seems to be disabled\n");
133 ene_printk(KERN_WARNING,
134 "send a mail to lirc-list@lists.sourceforge.net\n");
135 ene_printk(KERN_WARNING, "please attach output of acpidump\n");
136
137 return -ENODEV;
138 }
139
140 if (chip_major == 0x33) {
141 ene_printk(KERN_WARNING, "chips 0x33xx aren't supported yet\n");
142 return -ENODEV;
143 }
144
145 if (chip_major == 0x39 && chip_minor == 0x26 && hw_revision == 0xC0) {
146 dev->hw_revision = ENE_HW_C;
147 ene_printk(KERN_WARNING,
148 "KB3926C detected, driver support is not complete!\n");
149
150 } else if (old_ver == 0x24 && hw_revision == 0xC0) {
151 dev->hw_revision = ENE_HW_B;
152 ene_printk(KERN_NOTICE, "KB3926B detected\n");
153 } else {
154 dev->hw_revision = ENE_HW_D;
155 ene_printk(KERN_WARNING,
156 "unknown ENE chip detected, assuming KB3926D\n");
157 ene_printk(KERN_WARNING, "driver support incomplete");
158
159 }
160
161 ene_printk(KERN_DEBUG, "chip is 0x%02x%02x - 0x%02x, 0x%02x\n",
162 chip_major, chip_minor, old_ver, hw_revision);
163
164
165 /* detect features hardware supports */
166
167 if (dev->hw_revision < ENE_HW_C)
168 return 0;
169
170 fw_capabilities = ene_hw_read_reg(dev, ENE_FW2);
171
172 dev->hw_gpio40_learning = fw_capabilities & ENE_FW2_GP40_AS_LEARN;
173 dev->hw_learning_and_tx_capable = fw_capabilities & ENE_FW2_LEARNING;
174
175 dev->hw_fan_as_normal_input = dev->hw_learning_and_tx_capable &&
176 fw_capabilities & ENE_FW2_FAN_AS_NRML_IN;
177
178 ene_printk(KERN_NOTICE, "hardware features:\n");
179 ene_printk(KERN_NOTICE,
180 "learning and tx %s, gpio40_learn %s, fan_in %s\n",
181 dev->hw_learning_and_tx_capable ? "on" : "off",
182 dev->hw_gpio40_learning ? "on" : "off",
183 dev->hw_fan_as_normal_input ? "on" : "off");
184
185 if (!dev->hw_learning_and_tx_capable && enable_learning)
186 enable_learning = 0;
187
188 if (dev->hw_learning_and_tx_capable) {
189 ene_printk(KERN_WARNING,
190 "Device supports transmitting, but the driver doesn't\n");
191 ene_printk(KERN_WARNING,
192 "due to lack of hardware to test against.\n");
193 ene_printk(KERN_WARNING,
194 "Send a mail to: lirc-list@lists.sourceforge.net\n");
195 }
196 return 0;
197}
198
199/* hardware initialization */
200static int ene_hw_init(void *data)
201{
202 u8 reg_value;
203 struct ene_device *dev = (struct ene_device *)data;
204 dev->in_use = 1;
205
206 if (dev->hw_revision < ENE_HW_C) {
207 ene_hw_write_reg(dev, ENEB_IRQ, dev->irq << 1);
208 ene_hw_write_reg(dev, ENEB_IRQ_UNK1, 0x01);
209 } else {
210 reg_value = ene_hw_read_reg(dev, ENEC_IRQ) & 0xF0;
211 reg_value |= ENEC_IRQ_UNK_EN;
212 reg_value &= ~ENEC_IRQ_STATUS;
213 reg_value |= (dev->irq & ENEC_IRQ_MASK);
214 ene_hw_write_reg(dev, ENEC_IRQ, reg_value);
215 ene_hw_write_reg(dev, ENE_TX_UNK1, 0x63);
216 }
217
218 ene_hw_write_reg(dev, ENE_CIR_CONF2, 0x00);
219 ene_set_inputs(dev, enable_learning);
220
221 /* set sampling period */
222 ene_hw_write_reg(dev, ENE_CIR_SAMPLE_PERIOD, sample_period);
223
224 /* ack any pending irqs - just in case */
225 ene_hw_irq_status(dev, NULL);
226
227 /* enter idle mode */
228 ene_set_idle(dev, 1);
229
230 /* enable firmware bits */
231 ene_hw_write_reg_mask(dev, ENE_FW1,
232 ENE_FW1_ENABLE | ENE_FW1_IRQ,
233 ENE_FW1_ENABLE | ENE_FW1_IRQ);
234 /* clear stats */
235 dev->sample = 0;
236 return 0;
237}
238
239/* this enables gpio40 signal, used if connected to wide band input*/
240static void ene_enable_gpio40(struct ene_device *dev, int enable)
241{
242 ene_hw_write_reg_mask(dev, ENE_CIR_CONF1, enable ?
243 0 : ENE_CIR_CONF2_GPIO40DIS,
244 ENE_CIR_CONF2_GPIO40DIS);
245}
246
247/* this enables the classic sampler */
248static void ene_enable_normal_recieve(struct ene_device *dev, int enable)
249{
250 ene_hw_write_reg(dev, ENE_CIR_CONF1, enable ? ENE_CIR_CONF1_ADC_ON : 0);
251}
252
253/* this enables recieve via fan input */
254static void ene_enable_fan_recieve(struct ene_device *dev, int enable)
255{
256 if (!enable)
257 ene_hw_write_reg(dev, ENE_FAN_AS_IN1, 0);
258 else {
259 ene_hw_write_reg(dev, ENE_FAN_AS_IN1, ENE_FAN_AS_IN1_EN);
260 ene_hw_write_reg(dev, ENE_FAN_AS_IN2, ENE_FAN_AS_IN2_EN);
261 }
262 dev->fan_input_inuse = enable;
263}
264
265/* determine which input to use*/
266static void ene_set_inputs(struct ene_device *dev, int learning_enable)
267{
268 ene_enable_normal_recieve(dev, 1);
269
270 /* old hardware doesn't support learning mode for sure */
271 if (dev->hw_revision <= ENE_HW_B)
272 return;
273
274 /* reciever not learning capable, still set gpio40 correctly */
275 if (!dev->hw_learning_and_tx_capable) {
276 ene_enable_gpio40(dev, !dev->hw_gpio40_learning);
277 return;
278 }
279
280 /* enable learning mode */
281 if (learning_enable) {
282 ene_enable_gpio40(dev, dev->hw_gpio40_learning);
283
284 /* fan input is not used for learning */
285 if (dev->hw_fan_as_normal_input)
286 ene_enable_fan_recieve(dev, 0);
287
288 /* disable learning mode */
289 } else {
290 if (dev->hw_fan_as_normal_input) {
291 ene_enable_fan_recieve(dev, 1);
292 ene_enable_normal_recieve(dev, 0);
293 } else
294 ene_enable_gpio40(dev, !dev->hw_gpio40_learning);
295 }
296
297 /* set few additional settings for this mode */
298 ene_hw_write_reg_mask(dev, ENE_CIR_CONF1, learning_enable ?
299 ENE_CIR_CONF1_LEARN1 : 0, ENE_CIR_CONF1_LEARN1);
300
301 ene_hw_write_reg_mask(dev, ENE_CIR_CONF2, learning_enable ?
302 ENE_CIR_CONF2_LEARN2 : 0, ENE_CIR_CONF2_LEARN2);
303}
304
305/* deinitialization */
306static void ene_hw_deinit(void *data)
307{
308 struct ene_device *dev = (struct ene_device *)data;
309
310 /* disable samplers */
311 ene_enable_normal_recieve(dev, 0);
312
313 if (dev->hw_fan_as_normal_input)
314 ene_enable_fan_recieve(dev, 0);
315
316 /* disable hardware IRQ and firmware flag */
317 ene_hw_write_reg_mask(dev, ENE_FW1, 0, ENE_FW1_ENABLE | ENE_FW1_IRQ);
318
319 ene_set_idle(dev, 1);
320 dev->in_use = 0;
321}
322
323/* sends current sample to userspace */
324static void send_sample(struct ene_device *dev)
325{
326 int value = abs(dev->sample) & PULSE_MASK;
327
328 if (dev->sample > 0)
329 value |= PULSE_BIT;
330
331 if (!lirc_buffer_full(dev->lirc_driver->rbuf)) {
332 lirc_buffer_write(dev->lirc_driver->rbuf, (void *)&value);
333 wake_up(&dev->lirc_driver->rbuf->wait_poll);
334 }
335 dev->sample = 0;
336}
337
338/* this updates current sample */
339static void update_sample(struct ene_device *dev, int sample)
340{
341 if (!dev->sample)
342 dev->sample = sample;
343 else if (same_sign(dev->sample, sample))
344 dev->sample += sample;
345 else {
346 send_sample(dev);
347 dev->sample = sample;
348 }
349}
350
351/* enable or disable idle mode */
352static void ene_set_idle(struct ene_device *dev, int idle)
353{
354 struct timeval now;
355 int disable = idle && enable_idle && (dev->hw_revision < ENE_HW_C);
356
357 ene_hw_write_reg_mask(dev, ENE_CIR_SAMPLE_PERIOD,
358 disable ? 0 : ENE_CIR_SAMPLE_OVERFLOW,
359 ENE_CIR_SAMPLE_OVERFLOW);
360 dev->idle = idle;
361
362 /* remember when we have entered the idle mode */
363 if (idle) {
364 do_gettimeofday(&dev->gap_start);
365 return;
366 }
367
368 /* send the gap between keypresses now */
369 do_gettimeofday(&now);
370
371 if (now.tv_sec - dev->gap_start.tv_sec > 16)
372 dev->sample = space(PULSE_MASK);
373 else
374 dev->sample = dev->sample +
375 space(1000000ull * (now.tv_sec - dev->gap_start.tv_sec))
376 + space(now.tv_usec - dev->gap_start.tv_usec);
377
378 if (abs(dev->sample) > PULSE_MASK)
379 dev->sample = space(PULSE_MASK);
380 send_sample(dev);
381}
382
383/* interrupt handler */
384static irqreturn_t ene_hw_irq(int irq, void *data)
385{
386 u16 hw_value;
387 int i, hw_sample;
388 int space;
389 int buffer_pointer;
390 int irq_status;
391
392 struct ene_device *dev = (struct ene_device *)data;
393 irq_status = ene_hw_irq_status(dev, &buffer_pointer);
394
395 if (!irq_status)
396 return IRQ_NONE;
397
398 /* TODO: only RX for now */
399 if (irq_status == ENE_IRQ_TX)
400 return IRQ_HANDLED;
401
402 for (i = 0; i < ENE_SAMPLES_SIZE; i++) {
403
404 hw_value = ene_hw_read_reg(dev,
405 ENE_SAMPLE_BUFFER + buffer_pointer + i);
406
407 if (dev->fan_input_inuse) {
408 /* read high part of the sample */
409 hw_value |= ene_hw_read_reg(dev,
410 ENE_SAMPLE_BUFFER_FAN + buffer_pointer + i) << 8;
411
412 /* test for _space_ bit */
413 space = !(hw_value & ENE_FAN_SMPL_PULS_MSK);
414
415 /* clear space bit, and other unused bits */
416 hw_value &= ENE_FAN_VALUE_MASK;
417 hw_sample = hw_value * ENE_SAMPLE_PERIOD_FAN;
418
419 } else {
420 space = hw_value & ENE_SAMPLE_SPC_MASK;
421 hw_value &= ENE_SAMPLE_VALUE_MASK;
422 hw_sample = hw_value * sample_period;
423 }
424
425 /* no more data */
426 if (!(hw_value))
427 break;
428
429 if (space)
430 hw_sample *= -1;
431
432 /* overflow sample recieved, handle it */
433
434 if (!dev->fan_input_inuse && hw_value == ENE_SAMPLE_OVERFLOW) {
435
436 if (dev->idle)
437 continue;
438
439 if (dev->sample > 0 || abs(dev->sample) <= ENE_MAXGAP)
440 update_sample(dev, hw_sample);
441 else
442 ene_set_idle(dev, 1);
443
444 continue;
445 }
446
447 /* normal first sample recieved */
448 if (!dev->fan_input_inuse && dev->idle) {
449 ene_set_idle(dev, 0);
450
451 /* discard first recieved value, its random
452 since its the time signal was off before
453 first pulse if idle mode is enabled, HW
454 does that for us */
455
456 if (!enable_idle)
457 continue;
458 }
459 update_sample(dev, hw_sample);
460 send_sample(dev);
461 }
462 return IRQ_HANDLED;
463}
464
465static int ene_probe(struct pnp_dev *pnp_dev,
466 const struct pnp_device_id *dev_id)
467{
468 struct ene_device *dev;
469 struct lirc_driver *lirc_driver;
470 int error = -ENOMEM;
471
472 dev = kzalloc(sizeof(struct ene_device), GFP_KERNEL);
473
474 if (!dev)
475 goto err1;
476
477 dev->pnp_dev = pnp_dev;
478 pnp_set_drvdata(pnp_dev, dev);
479
480
481 /* prepare lirc interface */
482 error = -ENOMEM;
483 lirc_driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
484
485 if (!lirc_driver)
486 goto err2;
487
488 dev->lirc_driver = lirc_driver;
489
490 strcpy(lirc_driver->name, ENE_DRIVER_NAME);
491 lirc_driver->minor = -1;
492 lirc_driver->code_length = sizeof(int) * 8;
493 lirc_driver->features = LIRC_CAN_REC_MODE2;
494 lirc_driver->data = dev;
495 lirc_driver->set_use_inc = ene_hw_init;
496 lirc_driver->set_use_dec = ene_hw_deinit;
497 lirc_driver->dev = &pnp_dev->dev;
498 lirc_driver->owner = THIS_MODULE;
499
500 lirc_driver->rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
501
502 if (!lirc_driver->rbuf)
503 goto err3;
504
505 if (lirc_buffer_init(lirc_driver->rbuf, sizeof(int), sizeof(int) * 256))
506 goto err4;
507
508 error = -ENODEV;
509 if (lirc_register_driver(lirc_driver))
510 goto err5;
511
512 /* validate resources */
513 if (!pnp_port_valid(pnp_dev, 0) ||
514 pnp_port_len(pnp_dev, 0) < ENE_MAX_IO)
515 goto err6;
516
517 if (!pnp_irq_valid(pnp_dev, 0))
518 goto err6;
519
520 dev->hw_io = pnp_port_start(pnp_dev, 0);
521 dev->irq = pnp_irq(pnp_dev, 0);
522
523 /* claim the resources */
524 error = -EBUSY;
525 if (!request_region(dev->hw_io, ENE_MAX_IO, ENE_DRIVER_NAME))
526 goto err6;
527
528 if (request_irq(dev->irq, ene_hw_irq,
529 IRQF_SHARED, ENE_DRIVER_NAME, (void *)dev))
530 goto err7;
531
532 /* detect hardware version and features */
533 error = ene_hw_detect(dev);
534 if (error)
535 goto err8;
536
537 ene_printk(KERN_NOTICE, "driver has been succesfully loaded\n");
538 return 0;
539
540err8:
541 free_irq(dev->irq, dev);
542err7:
543 release_region(dev->hw_io, ENE_MAX_IO);
544err6:
545 lirc_unregister_driver(lirc_driver->minor);
546err5:
547 lirc_buffer_free(lirc_driver->rbuf);
548err4:
549 kfree(lirc_driver->rbuf);
550err3:
551 kfree(lirc_driver);
552err2:
553 kfree(dev);
554err1:
555 return error;
556}
557
558static void ene_remove(struct pnp_dev *pnp_dev)
559{
560 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
561 ene_hw_deinit(dev);
562 free_irq(dev->irq, dev);
563 release_region(dev->hw_io, ENE_MAX_IO);
564 lirc_unregister_driver(dev->lirc_driver->minor);
565 lirc_buffer_free(dev->lirc_driver->rbuf);
566 kfree(dev->lirc_driver);
567 kfree(dev);
568}
569
570#ifdef CONFIG_PM
571
572/* TODO: make 'wake on IR' configurable and add .shutdown */
573/* currently impossible due to lack of kernel support */
574
575static int ene_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
576{
577 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
578 ene_hw_write_reg_mask(dev, ENE_FW1, ENE_FW1_WAKE, ENE_FW1_WAKE);
579 return 0;
580}
581
582static int ene_resume(struct pnp_dev *pnp_dev)
583{
584 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
585 if (dev->in_use)
586 ene_hw_init(dev);
587
588 ene_hw_write_reg_mask(dev, ENE_FW1, 0, ENE_FW1_WAKE);
589 return 0;
590}
591
592#endif
593
594static const struct pnp_device_id ene_ids[] = {
595 {.id = "ENE0100",},
596 {},
597};
598
599static struct pnp_driver ene_driver = {
600 .name = ENE_DRIVER_NAME,
601 .id_table = ene_ids,
602 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
603
604 .probe = ene_probe,
605 .remove = __devexit_p(ene_remove),
606
607#ifdef CONFIG_PM
608 .suspend = ene_suspend,
609 .resume = ene_resume,
610#endif
611};
612
613static int __init ene_init(void)
614{
615 if (sample_period < 5) {
616 ene_printk(KERN_ERR, "sample period must be at\n");
617 ene_printk(KERN_ERR, "least 5 us, (at least 30 recommended)\n");
618 return -EINVAL;
619 }
620 return pnp_register_driver(&ene_driver);
621}
622
623static void ene_exit(void)
624{
625 pnp_unregister_driver(&ene_driver);
626}
627
628module_param(sample_period, int, S_IRUGO);
629MODULE_PARM_DESC(sample_period, "Hardware sample period (75 us default)");
630
631module_param(enable_idle, bool, S_IRUGO | S_IWUSR);
632MODULE_PARM_DESC(enable_idle,
633 "Enables turning off signal sampling after long inactivity time; "
634 "if disabled might help detecting input signal (default: enabled)");
635
636module_param(enable_learning, bool, S_IRUGO);
637MODULE_PARM_DESC(enable_learning, "Use wide band (learning) reciever");
638
639MODULE_DEVICE_TABLE(pnp, ene_ids);
640MODULE_DESCRIPTION
641 ("LIRC driver for KB3926B/KB3926C/KB3926D (aka ENE0100) CIR port");
642MODULE_AUTHOR("Maxim Levitsky");
643MODULE_LICENSE("GPL");
644
645module_init(ene_init);
646module_exit(ene_exit);
diff --git a/drivers/staging/lirc/lirc_it87.c b/drivers/staging/lirc/lirc_it87.c
index 09f36961c6d..ec11c0e949a 100644
--- a/drivers/staging/lirc/lirc_it87.c
+++ b/drivers/staging/lirc/lirc_it87.c
@@ -109,6 +109,7 @@ static DECLARE_WAIT_QUEUE_HEAD(lirc_read_queue);
109 109
110static DEFINE_SPINLOCK(hardware_lock); 110static DEFINE_SPINLOCK(hardware_lock);
111static DEFINE_SPINLOCK(dev_lock); 111static DEFINE_SPINLOCK(dev_lock);
112static bool device_open;
112 113
113static int rx_buf[RBUF_LEN]; 114static int rx_buf[RBUF_LEN];
114unsigned int rx_tail, rx_head; 115unsigned int rx_tail, rx_head;
@@ -147,10 +148,11 @@ static void drop_port(void);
147static int lirc_open(struct inode *inode, struct file *file) 148static int lirc_open(struct inode *inode, struct file *file)
148{ 149{
149 spin_lock(&dev_lock); 150 spin_lock(&dev_lock);
150 if (module_refcount(THIS_MODULE)) { 151 if (device_open) {
151 spin_unlock(&dev_lock); 152 spin_unlock(&dev_lock);
152 return -EBUSY; 153 return -EBUSY;
153 } 154 }
155 device_open = true;
154 spin_unlock(&dev_lock); 156 spin_unlock(&dev_lock);
155 return 0; 157 return 0;
156} 158}
@@ -158,6 +160,9 @@ static int lirc_open(struct inode *inode, struct file *file)
158 160
159static int lirc_close(struct inode *inode, struct file *file) 161static int lirc_close(struct inode *inode, struct file *file)
160{ 162{
163 spin_lock(&dev_lock);
164 device_open = false;
165 spin_unlock(&dev_lock);
161 return 0; 166 return 0;
162} 167}
163 168
@@ -363,7 +368,6 @@ static struct lirc_driver driver = {
363}; 368};
364 369
365 370
366#ifdef MODULE
367static int init_chrdev(void) 371static int init_chrdev(void)
368{ 372{
369 driver.minor = lirc_register_driver(&driver); 373 driver.minor = lirc_register_driver(&driver);
@@ -380,7 +384,6 @@ static void drop_chrdev(void)
380{ 384{
381 lirc_unregister_driver(driver.minor); 385 lirc_unregister_driver(driver.minor);
382} 386}
383#endif
384 387
385 388
386/* SECTION: Hardware */ 389/* SECTION: Hardware */
diff --git a/drivers/staging/lirc/lirc_parallel.c b/drivers/staging/lirc/lirc_parallel.c
index a1ebd071640..6da4a8c6ebc 100644
--- a/drivers/staging/lirc/lirc_parallel.c
+++ b/drivers/staging/lirc/lirc_parallel.c
@@ -240,7 +240,7 @@ static void irq_handler(void *blah)
240 unsigned int level, newlevel; 240 unsigned int level, newlevel;
241 unsigned int timeout; 241 unsigned int timeout;
242 242
243 if (!module_refcount(THIS_MODULE)) 243 if (!is_open)
244 return; 244 return;
245 245
246 if (!is_claimed) 246 if (!is_claimed)
@@ -515,7 +515,7 @@ static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
515 515
516static int lirc_open(struct inode *node, struct file *filep) 516static int lirc_open(struct inode *node, struct file *filep)
517{ 517{
518 if (module_refcount(THIS_MODULE) || !lirc_claim()) 518 if (is_open || !lirc_claim())
519 return -EBUSY; 519 return -EBUSY;
520 520
521 parport_enable_irq(pport); 521 parport_enable_irq(pport);
diff --git a/drivers/staging/lirc/lirc_streamzap.c b/drivers/staging/lirc/lirc_streamzap.c
deleted file mode 100644
index be09c103f0c..00000000000
--- a/drivers/staging/lirc/lirc_streamzap.c
+++ /dev/null
@@ -1,821 +0,0 @@
1/*
2 * Streamzap Remote Control driver
3 *
4 * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
5 *
6 * This driver was based on the work of Greg Wickham and Adrian
7 * Dewhurst. It was substantially rewritten to support correct signal
8 * gaps and now maintains a delay buffer, which is used to present
9 * consistent timing behaviour to user space applications. Without the
10 * delay buffer an ugly hack would be required in lircd, which can
11 * cause sluggish signal decoding in certain situations.
12 *
13 * This driver is based on the USB skeleton driver packaged with the
14 * kernel; copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */
30
31#include <linux/kernel.h>
32#include <linux/errno.h>
33#include <linux/init.h>
34#include <linux/slab.h>
35#include <linux/module.h>
36#include <linux/smp_lock.h>
37#include <linux/completion.h>
38#include <linux/uaccess.h>
39#include <linux/usb.h>
40
41#include <media/lirc.h>
42#include <media/lirc_dev.h>
43
44#define DRIVER_VERSION "1.28"
45#define DRIVER_NAME "lirc_streamzap"
46#define DRIVER_DESC "Streamzap Remote Control driver"
47
48static int debug;
49
50#define USB_STREAMZAP_VENDOR_ID 0x0e9c
51#define USB_STREAMZAP_PRODUCT_ID 0x0000
52
53/* Use our own dbg macro */
54#define dprintk(fmt, args...) \
55 do { \
56 if (debug) \
57 printk(KERN_DEBUG DRIVER_NAME "[%d]: " \
58 fmt "\n", ## args); \
59 } while (0)
60
61/* table of devices that work with this driver */
62static struct usb_device_id streamzap_table[] = {
63 /* Streamzap Remote Control */
64 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
65 /* Terminating entry */
66 { }
67};
68
69MODULE_DEVICE_TABLE(usb, streamzap_table);
70
71#define STREAMZAP_PULSE_MASK 0xf0
72#define STREAMZAP_SPACE_MASK 0x0f
73#define STREAMZAP_TIMEOUT 0xff
74#define STREAMZAP_RESOLUTION 256
75
76/* number of samples buffered */
77#define STREAMZAP_BUF_LEN 128
78
79enum StreamzapDecoderState {
80 PulseSpace,
81 FullPulse,
82 FullSpace,
83 IgnorePulse
84};
85
86/* Structure to hold all of our device specific stuff
87 *
88 * some remarks regarding locking:
89 * theoretically this struct can be accessed from three threads:
90 *
91 * - from lirc_dev through set_use_inc/set_use_dec
92 *
93 * - from the USB layer throuh probe/disconnect/irq
94 *
95 * Careful placement of lirc_register_driver/lirc_unregister_driver
96 * calls will prevent conflicts. lirc_dev makes sure that
97 * set_use_inc/set_use_dec are not being executed and will not be
98 * called after lirc_unregister_driver returns.
99 *
100 * - by the timer callback
101 *
102 * The timer is only running when the device is connected and the
103 * LIRC device is open. Making sure the timer is deleted by
104 * set_use_dec will make conflicts impossible.
105 */
106struct usb_streamzap {
107
108 /* usb */
109 /* save off the usb device pointer */
110 struct usb_device *udev;
111 /* the interface for this device */
112 struct usb_interface *interface;
113
114 /* buffer & dma */
115 unsigned char *buf_in;
116 dma_addr_t dma_in;
117 unsigned int buf_in_len;
118
119 struct usb_endpoint_descriptor *endpoint;
120
121 /* IRQ */
122 struct urb *urb_in;
123
124 /* lirc */
125 struct lirc_driver *driver;
126 struct lirc_buffer *delay_buf;
127
128 /* timer used to support delay buffering */
129 struct timer_list delay_timer;
130 int timer_running;
131 spinlock_t timer_lock;
132
133 /* tracks whether we are currently receiving some signal */
134 int idle;
135 /* sum of signal lengths received since signal start */
136 unsigned long sum;
137 /* start time of signal; necessary for gap tracking */
138 struct timeval signal_last;
139 struct timeval signal_start;
140 enum StreamzapDecoderState decoder_state;
141 struct timer_list flush_timer;
142 int flush;
143 int in_use;
144 int timeout_enabled;
145};
146
147
148/* local function prototypes */
149static int streamzap_probe(struct usb_interface *interface,
150 const struct usb_device_id *id);
151static void streamzap_disconnect(struct usb_interface *interface);
152static void usb_streamzap_irq(struct urb *urb);
153static int streamzap_use_inc(void *data);
154static void streamzap_use_dec(void *data);
155static long streamzap_ioctl(struct file *filep, unsigned int cmd,
156 unsigned long arg);
157static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
158static int streamzap_resume(struct usb_interface *intf);
159
160/* usb specific object needed to register this driver with the usb subsystem */
161
162static struct usb_driver streamzap_driver = {
163 .name = DRIVER_NAME,
164 .probe = streamzap_probe,
165 .disconnect = streamzap_disconnect,
166 .suspend = streamzap_suspend,
167 .resume = streamzap_resume,
168 .id_table = streamzap_table,
169};
170
171static void stop_timer(struct usb_streamzap *sz)
172{
173 unsigned long flags;
174
175 spin_lock_irqsave(&sz->timer_lock, flags);
176 if (sz->timer_running) {
177 sz->timer_running = 0;
178 spin_unlock_irqrestore(&sz->timer_lock, flags);
179 del_timer_sync(&sz->delay_timer);
180 } else {
181 spin_unlock_irqrestore(&sz->timer_lock, flags);
182 }
183}
184
185static void flush_timeout(unsigned long arg)
186{
187 struct usb_streamzap *sz = (struct usb_streamzap *) arg;
188
189 /* finally start accepting data */
190 sz->flush = 0;
191}
192static void delay_timeout(unsigned long arg)
193{
194 unsigned long flags;
195 /* deliver data every 10 ms */
196 static unsigned long timer_inc =
197 (10000/(1000000/HZ)) == 0 ? 1 : (10000/(1000000/HZ));
198 struct usb_streamzap *sz = (struct usb_streamzap *) arg;
199 int data;
200
201 spin_lock_irqsave(&sz->timer_lock, flags);
202
203 if (!lirc_buffer_empty(sz->delay_buf) &&
204 !lirc_buffer_full(sz->driver->rbuf)) {
205 lirc_buffer_read(sz->delay_buf, (unsigned char *) &data);
206 lirc_buffer_write(sz->driver->rbuf, (unsigned char *) &data);
207 }
208 if (!lirc_buffer_empty(sz->delay_buf)) {
209 while (lirc_buffer_available(sz->delay_buf) <
210 STREAMZAP_BUF_LEN / 2 &&
211 !lirc_buffer_full(sz->driver->rbuf)) {
212 lirc_buffer_read(sz->delay_buf,
213 (unsigned char *) &data);
214 lirc_buffer_write(sz->driver->rbuf,
215 (unsigned char *) &data);
216 }
217 if (sz->timer_running) {
218 sz->delay_timer.expires = jiffies + timer_inc;
219 add_timer(&sz->delay_timer);
220 }
221 } else {
222 sz->timer_running = 0;
223 }
224
225 if (!lirc_buffer_empty(sz->driver->rbuf))
226 wake_up(&sz->driver->rbuf->wait_poll);
227
228 spin_unlock_irqrestore(&sz->timer_lock, flags);
229}
230
231static void flush_delay_buffer(struct usb_streamzap *sz)
232{
233 int data;
234 int empty = 1;
235
236 while (!lirc_buffer_empty(sz->delay_buf)) {
237 empty = 0;
238 lirc_buffer_read(sz->delay_buf, (unsigned char *) &data);
239 if (!lirc_buffer_full(sz->driver->rbuf)) {
240 lirc_buffer_write(sz->driver->rbuf,
241 (unsigned char *) &data);
242 } else {
243 dprintk("buffer overflow", sz->driver->minor);
244 }
245 }
246 if (!empty)
247 wake_up(&sz->driver->rbuf->wait_poll);
248}
249
250static void push(struct usb_streamzap *sz, unsigned char *data)
251{
252 unsigned long flags;
253
254 spin_lock_irqsave(&sz->timer_lock, flags);
255 if (lirc_buffer_full(sz->delay_buf)) {
256 int read_data;
257
258 lirc_buffer_read(sz->delay_buf,
259 (unsigned char *) &read_data);
260 if (!lirc_buffer_full(sz->driver->rbuf)) {
261 lirc_buffer_write(sz->driver->rbuf,
262 (unsigned char *) &read_data);
263 } else {
264 dprintk("buffer overflow", sz->driver->minor);
265 }
266 }
267
268 lirc_buffer_write(sz->delay_buf, data);
269
270 if (!sz->timer_running) {
271 sz->delay_timer.expires = jiffies + HZ/10;
272 add_timer(&sz->delay_timer);
273 sz->timer_running = 1;
274 }
275
276 spin_unlock_irqrestore(&sz->timer_lock, flags);
277}
278
279static void push_full_pulse(struct usb_streamzap *sz,
280 unsigned char value)
281{
282 int pulse;
283
284 if (sz->idle) {
285 long deltv;
286 int tmp;
287
288 sz->signal_last = sz->signal_start;
289 do_gettimeofday(&sz->signal_start);
290
291 deltv = sz->signal_start.tv_sec-sz->signal_last.tv_sec;
292 if (deltv > 15) {
293 /* really long time */
294 tmp = LIRC_SPACE(LIRC_VALUE_MASK);
295 } else {
296 tmp = (int) (deltv*1000000+
297 sz->signal_start.tv_usec -
298 sz->signal_last.tv_usec);
299 tmp -= sz->sum;
300 tmp = LIRC_SPACE(tmp);
301 }
302 dprintk("ls %u", sz->driver->minor, tmp);
303 push(sz, (char *)&tmp);
304
305 sz->idle = 0;
306 sz->sum = 0;
307 }
308
309 pulse = ((int) value) * STREAMZAP_RESOLUTION;
310 pulse += STREAMZAP_RESOLUTION / 2;
311 sz->sum += pulse;
312 pulse = LIRC_PULSE(pulse);
313
314 dprintk("p %u", sz->driver->minor, pulse & PULSE_MASK);
315 push(sz, (char *)&pulse);
316}
317
318static void push_half_pulse(struct usb_streamzap *sz,
319 unsigned char value)
320{
321 push_full_pulse(sz, (value & STREAMZAP_PULSE_MASK)>>4);
322}
323
324static void push_full_space(struct usb_streamzap *sz,
325 unsigned char value)
326{
327 int space;
328
329 space = ((int) value)*STREAMZAP_RESOLUTION;
330 space += STREAMZAP_RESOLUTION/2;
331 sz->sum += space;
332 space = LIRC_SPACE(space);
333 dprintk("s %u", sz->driver->minor, space);
334 push(sz, (char *)&space);
335}
336
337static void push_half_space(struct usb_streamzap *sz,
338 unsigned char value)
339{
340 push_full_space(sz, value & STREAMZAP_SPACE_MASK);
341}
342
343/**
344 * usb_streamzap_irq - IRQ handler
345 *
346 * This procedure is invoked on reception of data from
347 * the usb remote.
348 */
349static void usb_streamzap_irq(struct urb *urb)
350{
351 struct usb_streamzap *sz;
352 int len;
353 unsigned int i = 0;
354
355 if (!urb)
356 return;
357
358 sz = urb->context;
359 len = urb->actual_length;
360
361 switch (urb->status) {
362 case -ECONNRESET:
363 case -ENOENT:
364 case -ESHUTDOWN:
365 /*
366 * this urb is terminated, clean up.
367 * sz might already be invalid at this point
368 */
369 dprintk("urb status: %d", -1, urb->status);
370 return;
371 default:
372 break;
373 }
374
375 dprintk("received %d", sz->driver->minor, urb->actual_length);
376 if (!sz->flush) {
377 for (i = 0; i < urb->actual_length; i++) {
378 dprintk("%d: %x", sz->driver->minor,
379 i, (unsigned char) sz->buf_in[i]);
380 switch (sz->decoder_state) {
381 case PulseSpace:
382 if ((sz->buf_in[i]&STREAMZAP_PULSE_MASK) ==
383 STREAMZAP_PULSE_MASK) {
384 sz->decoder_state = FullPulse;
385 continue;
386 } else if ((sz->buf_in[i]&STREAMZAP_SPACE_MASK)
387 == STREAMZAP_SPACE_MASK) {
388 push_half_pulse(sz, sz->buf_in[i]);
389 sz->decoder_state = FullSpace;
390 continue;
391 } else {
392 push_half_pulse(sz, sz->buf_in[i]);
393 push_half_space(sz, sz->buf_in[i]);
394 }
395 break;
396 case FullPulse:
397 push_full_pulse(sz, sz->buf_in[i]);
398 sz->decoder_state = IgnorePulse;
399 break;
400 case FullSpace:
401 if (sz->buf_in[i] == STREAMZAP_TIMEOUT) {
402 sz->idle = 1;
403 stop_timer(sz);
404 if (sz->timeout_enabled) {
405 int timeout =
406 LIRC_TIMEOUT
407 (STREAMZAP_TIMEOUT *
408 STREAMZAP_RESOLUTION);
409 push(sz, (char *)&timeout);
410 }
411 flush_delay_buffer(sz);
412 } else
413 push_full_space(sz, sz->buf_in[i]);
414 sz->decoder_state = PulseSpace;
415 break;
416 case IgnorePulse:
417 if ((sz->buf_in[i]&STREAMZAP_SPACE_MASK) ==
418 STREAMZAP_SPACE_MASK) {
419 sz->decoder_state = FullSpace;
420 continue;
421 }
422 push_half_space(sz, sz->buf_in[i]);
423 sz->decoder_state = PulseSpace;
424 break;
425 }
426 }
427 }
428
429 usb_submit_urb(urb, GFP_ATOMIC);
430
431 return;
432}
433
434static const struct file_operations streamzap_fops = {
435 .owner = THIS_MODULE,
436 .unlocked_ioctl = streamzap_ioctl,
437 .read = lirc_dev_fop_read,
438 .write = lirc_dev_fop_write,
439 .poll = lirc_dev_fop_poll,
440 .open = lirc_dev_fop_open,
441 .release = lirc_dev_fop_close,
442};
443
444
445/**
446 * streamzap_probe
447 *
448 * Called by usb-core to associated with a candidate device
449 * On any failure the return value is the ERROR
450 * On success return 0
451 */
452static int streamzap_probe(struct usb_interface *interface,
453 const struct usb_device_id *id)
454{
455 struct usb_device *udev = interface_to_usbdev(interface);
456 struct usb_host_interface *iface_host;
457 struct usb_streamzap *sz;
458 struct lirc_driver *driver;
459 struct lirc_buffer *lirc_buf;
460 struct lirc_buffer *delay_buf;
461 char buf[63], name[128] = "";
462 int retval = -ENOMEM;
463 int minor = 0;
464
465 /* Allocate space for device driver specific data */
466 sz = kzalloc(sizeof(struct usb_streamzap), GFP_KERNEL);
467 if (sz == NULL)
468 return -ENOMEM;
469
470 sz->udev = udev;
471 sz->interface = interface;
472
473 /* Check to ensure endpoint information matches requirements */
474 iface_host = interface->cur_altsetting;
475
476 if (iface_host->desc.bNumEndpoints != 1) {
477 err("%s: Unexpected desc.bNumEndpoints (%d)", __func__,
478 iface_host->desc.bNumEndpoints);
479 retval = -ENODEV;
480 goto free_sz;
481 }
482
483 sz->endpoint = &(iface_host->endpoint[0].desc);
484 if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
485 != USB_DIR_IN) {
486 err("%s: endpoint doesn't match input device 02%02x",
487 __func__, sz->endpoint->bEndpointAddress);
488 retval = -ENODEV;
489 goto free_sz;
490 }
491
492 if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
493 != USB_ENDPOINT_XFER_INT) {
494 err("%s: endpoint attributes don't match xfer 02%02x",
495 __func__, sz->endpoint->bmAttributes);
496 retval = -ENODEV;
497 goto free_sz;
498 }
499
500 if (sz->endpoint->wMaxPacketSize == 0) {
501 err("%s: endpoint message size==0? ", __func__);
502 retval = -ENODEV;
503 goto free_sz;
504 }
505
506 /* Allocate the USB buffer and IRQ URB */
507
508 sz->buf_in_len = sz->endpoint->wMaxPacketSize;
509 sz->buf_in = usb_alloc_coherent(sz->udev, sz->buf_in_len,
510 GFP_ATOMIC, &sz->dma_in);
511 if (sz->buf_in == NULL)
512 goto free_sz;
513
514 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
515 if (sz->urb_in == NULL)
516 goto free_sz;
517
518 /* Connect this device to the LIRC sub-system */
519 driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
520 if (!driver)
521 goto free_sz;
522
523 lirc_buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
524 if (!lirc_buf)
525 goto free_driver;
526 if (lirc_buffer_init(lirc_buf, sizeof(int), STREAMZAP_BUF_LEN))
527 goto kfree_lirc_buf;
528
529 delay_buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
530 if (!delay_buf)
531 goto free_lirc_buf;
532 if (lirc_buffer_init(delay_buf, sizeof(int), STREAMZAP_BUF_LEN))
533 goto kfree_delay_buf;
534
535 sz->driver = driver;
536 strcpy(sz->driver->name, DRIVER_NAME);
537 sz->driver->minor = -1;
538 sz->driver->sample_rate = 0;
539 sz->driver->code_length = sizeof(int) * 8;
540 sz->driver->features = LIRC_CAN_REC_MODE2 |
541 LIRC_CAN_GET_REC_RESOLUTION |
542 LIRC_CAN_SET_REC_TIMEOUT;
543 sz->driver->data = sz;
544 sz->driver->min_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION;
545 sz->driver->max_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION;
546 sz->driver->rbuf = lirc_buf;
547 sz->delay_buf = delay_buf;
548 sz->driver->set_use_inc = &streamzap_use_inc;
549 sz->driver->set_use_dec = &streamzap_use_dec;
550 sz->driver->fops = &streamzap_fops;
551 sz->driver->dev = &interface->dev;
552 sz->driver->owner = THIS_MODULE;
553
554 sz->idle = 1;
555 sz->decoder_state = PulseSpace;
556 init_timer(&sz->delay_timer);
557 sz->delay_timer.function = delay_timeout;
558 sz->delay_timer.data = (unsigned long) sz;
559 sz->timer_running = 0;
560 spin_lock_init(&sz->timer_lock);
561
562 init_timer(&sz->flush_timer);
563 sz->flush_timer.function = flush_timeout;
564 sz->flush_timer.data = (unsigned long) sz;
565 /* Complete final initialisations */
566
567 usb_fill_int_urb(sz->urb_in, udev,
568 usb_rcvintpipe(udev, sz->endpoint->bEndpointAddress),
569 sz->buf_in, sz->buf_in_len, usb_streamzap_irq, sz,
570 sz->endpoint->bInterval);
571 sz->urb_in->transfer_dma = sz->dma_in;
572 sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
573
574 if (udev->descriptor.iManufacturer
575 && usb_string(udev, udev->descriptor.iManufacturer,
576 buf, sizeof(buf)) > 0)
577 strlcpy(name, buf, sizeof(name));
578
579 if (udev->descriptor.iProduct
580 && usb_string(udev, udev->descriptor.iProduct,
581 buf, sizeof(buf)) > 0)
582 snprintf(name + strlen(name), sizeof(name) - strlen(name),
583 " %s", buf);
584
585 minor = lirc_register_driver(driver);
586
587 if (minor < 0)
588 goto free_delay_buf;
589
590 sz->driver->minor = minor;
591
592 usb_set_intfdata(interface, sz);
593
594 printk(KERN_INFO DRIVER_NAME "[%d]: %s on usb%d:%d attached\n",
595 sz->driver->minor, name,
596 udev->bus->busnum, sz->udev->devnum);
597
598 return 0;
599
600free_delay_buf:
601 lirc_buffer_free(sz->delay_buf);
602kfree_delay_buf:
603 kfree(delay_buf);
604free_lirc_buf:
605 lirc_buffer_free(sz->driver->rbuf);
606kfree_lirc_buf:
607 kfree(lirc_buf);
608free_driver:
609 kfree(driver);
610free_sz:
611 if (retval == -ENOMEM)
612 err("Out of memory");
613
614 if (sz) {
615 usb_free_urb(sz->urb_in);
616 usb_free_coherent(udev, sz->buf_in_len, sz->buf_in, sz->dma_in);
617 kfree(sz);
618 }
619
620 return retval;
621}
622
623static int streamzap_use_inc(void *data)
624{
625 struct usb_streamzap *sz = data;
626
627 if (!sz) {
628 dprintk("%s called with no context", -1, __func__);
629 return -EINVAL;
630 }
631 dprintk("set use inc", sz->driver->minor);
632
633 lirc_buffer_clear(sz->driver->rbuf);
634 lirc_buffer_clear(sz->delay_buf);
635
636 sz->flush_timer.expires = jiffies + HZ;
637 sz->flush = 1;
638 add_timer(&sz->flush_timer);
639
640 sz->urb_in->dev = sz->udev;
641 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
642 dprintk("open result = -EIO error submitting urb",
643 sz->driver->minor);
644 return -EIO;
645 }
646 sz->in_use++;
647
648 return 0;
649}
650
651static void streamzap_use_dec(void *data)
652{
653 struct usb_streamzap *sz = data;
654
655 if (!sz) {
656 dprintk("%s called with no context", -1, __func__);
657 return;
658 }
659 dprintk("set use dec", sz->driver->minor);
660
661 if (sz->flush) {
662 sz->flush = 0;
663 del_timer_sync(&sz->flush_timer);
664 }
665
666 usb_kill_urb(sz->urb_in);
667
668 stop_timer(sz);
669
670 sz->in_use--;
671}
672
673static long streamzap_ioctl(struct file *filep, unsigned int cmd,
674 unsigned long arg)
675{
676 int result = 0;
677 int val;
678 struct usb_streamzap *sz = lirc_get_pdata(filep);
679
680 switch (cmd) {
681 case LIRC_GET_REC_RESOLUTION:
682 result = put_user(STREAMZAP_RESOLUTION, (unsigned int *) arg);
683 break;
684 case LIRC_SET_REC_TIMEOUT:
685 result = get_user(val, (int *)arg);
686 if (result == 0) {
687 if (val == STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION)
688 sz->timeout_enabled = 1;
689 else if (val == 0)
690 sz->timeout_enabled = 0;
691 else
692 result = -EINVAL;
693 }
694 break;
695 default:
696 return lirc_dev_fop_ioctl(filep, cmd, arg);
697 }
698 return result;
699}
700
701/**
702 * streamzap_disconnect
703 *
704 * Called by the usb core when the device is removed from the system.
705 *
706 * This routine guarantees that the driver will not submit any more urbs
707 * by clearing dev->udev. It is also supposed to terminate any currently
708 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
709 * does not provide any way to do this.
710 */
711static void streamzap_disconnect(struct usb_interface *interface)
712{
713 struct usb_streamzap *sz;
714 int errnum;
715 int minor;
716
717 sz = usb_get_intfdata(interface);
718
719 /* unregister from the LIRC sub-system */
720
721 errnum = lirc_unregister_driver(sz->driver->minor);
722 if (errnum != 0)
723 dprintk("error in lirc_unregister: (returned %d)",
724 sz->driver->minor, errnum);
725
726 lirc_buffer_free(sz->delay_buf);
727 lirc_buffer_free(sz->driver->rbuf);
728
729 /* unregister from the USB sub-system */
730
731 usb_free_urb(sz->urb_in);
732
733 usb_free_coherent(sz->udev, sz->buf_in_len, sz->buf_in, sz->dma_in);
734
735 minor = sz->driver->minor;
736 kfree(sz->driver->rbuf);
737 kfree(sz->driver);
738 kfree(sz->delay_buf);
739 kfree(sz);
740
741 printk(KERN_INFO DRIVER_NAME "[%d]: disconnected\n", minor);
742}
743
744static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
745{
746 struct usb_streamzap *sz = usb_get_intfdata(intf);
747
748 printk(KERN_INFO DRIVER_NAME "[%d]: suspend\n", sz->driver->minor);
749 if (sz->in_use) {
750 if (sz->flush) {
751 sz->flush = 0;
752 del_timer_sync(&sz->flush_timer);
753 }
754
755 stop_timer(sz);
756
757 usb_kill_urb(sz->urb_in);
758 }
759 return 0;
760}
761
762static int streamzap_resume(struct usb_interface *intf)
763{
764 struct usb_streamzap *sz = usb_get_intfdata(intf);
765
766 lirc_buffer_clear(sz->driver->rbuf);
767 lirc_buffer_clear(sz->delay_buf);
768
769 if (sz->in_use) {
770 sz->flush_timer.expires = jiffies + HZ;
771 sz->flush = 1;
772 add_timer(&sz->flush_timer);
773
774 sz->urb_in->dev = sz->udev;
775 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
776 dprintk("open result = -EIO error submitting urb",
777 sz->driver->minor);
778 return -EIO;
779 }
780 }
781 return 0;
782}
783
784/**
785 * usb_streamzap_init
786 */
787static int __init usb_streamzap_init(void)
788{
789 int result;
790
791 /* register this driver with the USB subsystem */
792 result = usb_register(&streamzap_driver);
793
794 if (result) {
795 err("usb_register failed. Error number %d",
796 result);
797 return result;
798 }
799
800 printk(KERN_INFO DRIVER_NAME " " DRIVER_VERSION " registered\n");
801 return 0;
802}
803
804/**
805 * usb_streamzap_exit
806 */
807static void __exit usb_streamzap_exit(void)
808{
809 usb_deregister(&streamzap_driver);
810}
811
812
813module_init(usb_streamzap_init);
814module_exit(usb_streamzap_exit);
815
816MODULE_AUTHOR("Christoph Bartelmus, Greg Wickham, Adrian Dewhurst");
817MODULE_DESCRIPTION(DRIVER_DESC);
818MODULE_LICENSE("GPL");
819
820module_param(debug, bool, S_IRUGO | S_IWUSR);
821MODULE_PARM_DESC(debug, "Enable debugging messages");
diff --git a/drivers/staging/pohmelfs/inode.c b/drivers/staging/pohmelfs/inode.c
index bc1c6051a6f..97dae297ca3 100644
--- a/drivers/staging/pohmelfs/inode.c
+++ b/drivers/staging/pohmelfs/inode.c
@@ -968,12 +968,18 @@ int pohmelfs_setattr_raw(struct inode *inode, struct iattr *attr)
968 goto err_out_exit; 968 goto err_out_exit;
969 } 969 }
970 970
971 err = inode_setattr(inode, attr); 971 if ((attr->ia_valid & ATTR_SIZE) &&
972 if (err) { 972 attr->ia_size != i_size_read(inode)) {
973 dprintk("%s: ino: %llu, failed to set the attributes.\n", __func__, POHMELFS_I(inode)->ino); 973 err = vmtruncate(inode, attr->ia_size);
974 goto err_out_exit; 974 if (err) {
975 dprintk("%s: ino: %llu, failed to set the attributes.\n", __func__, POHMELFS_I(inode)->ino);
976 goto err_out_exit;
977 }
975 } 978 }
976 979
980 setattr_copy(inode, attr);
981 mark_inode_dirty(inode);
982
977 dprintk("%s: ino: %llu, mode: %o -> %o, uid: %u -> %u, gid: %u -> %u, size: %llu -> %llu.\n", 983 dprintk("%s: ino: %llu, mode: %o -> %o, uid: %u -> %u, gid: %u -> %u, size: %llu -> %llu.\n",
978 __func__, POHMELFS_I(inode)->ino, inode->i_mode, attr->ia_mode, 984 __func__, POHMELFS_I(inode)->ino, inode->i_mode, attr->ia_mode,
979 inode->i_uid, attr->ia_uid, inode->i_gid, attr->ia_gid, inode->i_size, attr->ia_size); 985 inode->i_uid, attr->ia_uid, inode->i_gid, attr->ia_gid, inode->i_size, attr->ia_size);
@@ -1217,7 +1223,7 @@ void pohmelfs_fill_inode(struct inode *inode, struct netfs_inode_info *info)
1217 } 1223 }
1218} 1224}
1219 1225
1220static void pohmelfs_drop_inode(struct inode *inode) 1226static int pohmelfs_drop_inode(struct inode *inode)
1221{ 1227{
1222 struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb); 1228 struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
1223 struct pohmelfs_inode *pi = POHMELFS_I(inode); 1229 struct pohmelfs_inode *pi = POHMELFS_I(inode);
@@ -1226,7 +1232,7 @@ static void pohmelfs_drop_inode(struct inode *inode)
1226 list_del_init(&pi->inode_entry); 1232 list_del_init(&pi->inode_entry);
1227 spin_unlock(&psb->ino_lock); 1233 spin_unlock(&psb->ino_lock);
1228 1234
1229 generic_drop_inode(inode); 1235 return generic_drop_inode(inode);
1230} 1236}
1231 1237
1232static struct pohmelfs_inode *pohmelfs_get_inode_from_list(struct pohmelfs_sb *psb, 1238static struct pohmelfs_inode *pohmelfs_get_inode_from_list(struct pohmelfs_sb *psb,
diff --git a/drivers/staging/usbip/vhci_hcd.c b/drivers/staging/usbip/vhci_hcd.c
index be5d8db9816..0574d848b90 100644
--- a/drivers/staging/usbip/vhci_hcd.c
+++ b/drivers/staging/usbip/vhci_hcd.c
@@ -215,7 +215,7 @@ static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
215 vhci = hcd_to_vhci(hcd); 215 vhci = hcd_to_vhci(hcd);
216 216
217 spin_lock_irqsave(&vhci->lock, flags); 217 spin_lock_irqsave(&vhci->lock, flags);
218 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { 218 if (!HCD_HW_ACCESSIBLE(hcd)) {
219 usbip_dbg_vhci_rh("hw accessible flag in on?\n"); 219 usbip_dbg_vhci_rh("hw accessible flag in on?\n");
220 goto done; 220 goto done;
221 } 221 }
@@ -269,7 +269,7 @@ static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
269 269
270 u32 prev_port_status[VHCI_NPORTS]; 270 u32 prev_port_status[VHCI_NPORTS];
271 271
272 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) 272 if (!HCD_HW_ACCESSIBLE(hcd))
273 return -ETIMEDOUT; 273 return -ETIMEDOUT;
274 274
275 /* 275 /*
@@ -1041,7 +1041,7 @@ static int vhci_bus_resume(struct usb_hcd *hcd)
1041 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); 1041 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1042 1042
1043 spin_lock_irq(&vhci->lock); 1043 spin_lock_irq(&vhci->lock);
1044 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { 1044 if (!HCD_HW_ACCESSIBLE(hcd)) {
1045 rc = -ESHUTDOWN; 1045 rc = -ESHUTDOWN;
1046 } else { 1046 } else {
1047 /* vhci->rh_state = DUMMY_RH_RUNNING; 1047 /* vhci->rh_state = DUMMY_RH_RUNNING;
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index 80b4008c89b..239f050efa3 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -41,7 +41,7 @@ obj-$(CONFIG_USB_MICROTEK) += image/
41obj-$(CONFIG_USB_SERIAL) += serial/ 41obj-$(CONFIG_USB_SERIAL) += serial/
42 42
43obj-$(CONFIG_USB) += misc/ 43obj-$(CONFIG_USB) += misc/
44obj-y += early/ 44obj-$(CONFIG_EARLY_PRINTK_DBGP) += early/
45 45
46obj-$(CONFIG_USB_ATM) += atm/ 46obj-$(CONFIG_USB_ATM) += atm/
47obj-$(CONFIG_USB_SPEEDTOUCH) += atm/ 47obj-$(CONFIG_USB_SPEEDTOUCH) += atm/
diff --git a/drivers/usb/atm/cxacru.c b/drivers/usb/atm/cxacru.c
index 101ffc965ee..593fc5e2d2e 100644
--- a/drivers/usb/atm/cxacru.c
+++ b/drivers/usb/atm/cxacru.c
@@ -564,7 +564,7 @@ static void cxacru_timeout_kill(unsigned long data)
564} 564}
565 565
566static int cxacru_start_wait_urb(struct urb *urb, struct completion *done, 566static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
567 int* actual_length) 567 int *actual_length)
568{ 568{
569 struct timer_list timer; 569 struct timer_list timer;
570 570
@@ -952,7 +952,7 @@ static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw,
952 put_unaligned(cpu_to_le32(addr), (__le32 *)(buf + offb)); 952 put_unaligned(cpu_to_le32(addr), (__le32 *)(buf + offb));
953 offb += 4; 953 offb += 4;
954 addr += l; 954 addr += l;
955 if(l) 955 if (l)
956 memcpy(buf + offb, data + offd, l); 956 memcpy(buf + offb, data + offd, l);
957 if (l < stride) 957 if (l < stride)
958 memset(buf + offb + l, 0, stride - l); 958 memset(buf + offb + l, 0, stride - l);
@@ -967,7 +967,7 @@ static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw,
967 } 967 }
968 offb = 0; 968 offb = 0;
969 } 969 }
970 } while(offd < size); 970 } while (offd < size);
971 dbg("sent fw %#x", fw); 971 dbg("sent fw %#x", fw);
972 972
973 ret = 0; 973 ret = 0;
@@ -1043,8 +1043,7 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
1043 if (instance->modem_type->boot_rom_patch) { 1043 if (instance->modem_type->boot_rom_patch) {
1044 val = cpu_to_le32(BR_ADDR); 1044 val = cpu_to_le32(BR_ADDR);
1045 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_STACK_ADDR, (u8 *) &val, 4); 1045 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_STACK_ADDR, (u8 *) &val, 4);
1046 } 1046 } else {
1047 else {
1048 ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0); 1047 ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0);
1049 } 1048 }
1050 if (ret) { 1049 if (ret) {
@@ -1068,7 +1067,7 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
1068} 1067}
1069 1068
1070static int cxacru_find_firmware(struct cxacru_data *instance, 1069static int cxacru_find_firmware(struct cxacru_data *instance,
1071 char* phase, const struct firmware **fw_p) 1070 char *phase, const struct firmware **fw_p)
1072{ 1071{
1073 struct usbatm_data *usbatm = instance->usbatm; 1072 struct usbatm_data *usbatm = instance->usbatm;
1074 struct device *dev = &usbatm->usb_intf->dev; 1073 struct device *dev = &usbatm->usb_intf->dev;
diff --git a/drivers/usb/atm/speedtch.c b/drivers/usb/atm/speedtch.c
index 80f9617d3a1..4716e707de5 100644
--- a/drivers/usb/atm/speedtch.c
+++ b/drivers/usb/atm/speedtch.c
@@ -753,11 +753,13 @@ static struct usb_driver speedtch_usb_driver = {
753 .id_table = speedtch_usb_ids 753 .id_table = speedtch_usb_ids
754}; 754};
755 755
756static void speedtch_release_interfaces(struct usb_device *usb_dev, int num_interfaces) { 756static void speedtch_release_interfaces(struct usb_device *usb_dev,
757 int num_interfaces)
758{
757 struct usb_interface *cur_intf; 759 struct usb_interface *cur_intf;
758 int i; 760 int i;
759 761
760 for(i = 0; i < num_interfaces; i++) 762 for (i = 0; i < num_interfaces; i++)
761 if ((cur_intf = usb_ifnum_to_if(usb_dev, i))) { 763 if ((cur_intf = usb_ifnum_to_if(usb_dev, i))) {
762 usb_set_intfdata(cur_intf, NULL); 764 usb_set_intfdata(cur_intf, NULL);
763 usb_driver_release_interface(&speedtch_usb_driver, cur_intf); 765 usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
@@ -792,7 +794,7 @@ static int speedtch_bind(struct usbatm_data *usbatm,
792 794
793 /* claim all interfaces */ 795 /* claim all interfaces */
794 796
795 for (i=0; i < num_interfaces; i++) { 797 for (i = 0; i < num_interfaces; i++) {
796 cur_intf = usb_ifnum_to_if(usb_dev, i); 798 cur_intf = usb_ifnum_to_if(usb_dev, i);
797 799
798 if ((i != ifnum) && cur_intf) { 800 if ((i != ifnum) && cur_intf) {
@@ -842,7 +844,7 @@ static int speedtch_bind(struct usbatm_data *usbatm,
842 844
843 use_isoc = 0; /* fall back to bulk if endpoint not found */ 845 use_isoc = 0; /* fall back to bulk if endpoint not found */
844 846
845 for (i=0; i<desc->desc.bNumEndpoints; i++) { 847 for (i = 0; i < desc->desc.bNumEndpoints; i++) {
846 const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc; 848 const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
847 849
848 if ((endpoint_desc->bEndpointAddress == target_address)) { 850 if ((endpoint_desc->bEndpointAddress == target_address)) {
diff --git a/drivers/usb/atm/ueagle-atm.c b/drivers/usb/atm/ueagle-atm.c
index ebae9448014..5b3f555e01c 100644
--- a/drivers/usb/atm/ueagle-atm.c
+++ b/drivers/usb/atm/ueagle-atm.c
@@ -67,6 +67,7 @@
67#include <linux/mutex.h> 67#include <linux/mutex.h>
68#include <linux/freezer.h> 68#include <linux/freezer.h>
69#include <linux/slab.h> 69#include <linux/slab.h>
70#include <linux/kernel.h>
70 71
71#include <asm/unaligned.h> 72#include <asm/unaligned.h>
72 73
@@ -2436,7 +2437,6 @@ UEA_ATTR(firmid, 0);
2436 2437
2437/* Retrieve the device End System Identifier (MAC) */ 2438/* Retrieve the device End System Identifier (MAC) */
2438 2439
2439#define htoi(x) (isdigit(x) ? x-'0' : toupper(x)-'A'+10)
2440static int uea_getesi(struct uea_softc *sc, u_char * esi) 2440static int uea_getesi(struct uea_softc *sc, u_char * esi)
2441{ 2441{
2442 unsigned char mac_str[2 * ETH_ALEN + 1]; 2442 unsigned char mac_str[2 * ETH_ALEN + 1];
@@ -2447,7 +2447,8 @@ static int uea_getesi(struct uea_softc *sc, u_char * esi)
2447 return 1; 2447 return 1;
2448 2448
2449 for (i = 0; i < ETH_ALEN; i++) 2449 for (i = 0; i < ETH_ALEN; i++)
2450 esi[i] = htoi(mac_str[2 * i]) * 16 + htoi(mac_str[2 * i + 1]); 2450 esi[i] = hex_to_bin(mac_str[2 * i]) * 16 +
2451 hex_to_bin(mac_str[2 * i + 1]);
2451 2452
2452 return 0; 2453 return 0;
2453} 2454}
diff --git a/drivers/usb/atm/usbatm.c b/drivers/usb/atm/usbatm.c
index 9b53e8df464..05bf5a27b5b 100644
--- a/drivers/usb/atm/usbatm.c
+++ b/drivers/usb/atm/usbatm.c
@@ -84,8 +84,8 @@
84 84
85#ifdef VERBOSE_DEBUG 85#ifdef VERBOSE_DEBUG
86static int usbatm_print_packet(const unsigned char *data, int len); 86static int usbatm_print_packet(const unsigned char *data, int len);
87#define PACKETDEBUG(arg...) usbatm_print_packet (arg) 87#define PACKETDEBUG(arg...) usbatm_print_packet(arg)
88#define vdbg(arg...) dbg (arg) 88#define vdbg(arg...) dbg(arg)
89#else 89#else
90#define PACKETDEBUG(arg...) 90#define PACKETDEBUG(arg...)
91#define vdbg(arg...) 91#define vdbg(arg...)
@@ -273,8 +273,7 @@ static void usbatm_complete(struct urb *urb)
273 273
274 if (unlikely(status) && 274 if (unlikely(status) &&
275 (!(channel->usbatm->flags & UDSL_IGNORE_EILSEQ) || 275 (!(channel->usbatm->flags & UDSL_IGNORE_EILSEQ) ||
276 status != -EILSEQ )) 276 status != -EILSEQ)) {
277 {
278 if (status == -ESHUTDOWN) 277 if (status == -ESHUTDOWN)
279 return; 278 return;
280 279
@@ -494,7 +493,7 @@ static unsigned int usbatm_write_cells(struct usbatm_data *instance,
494 ptr += data_len; 493 ptr += data_len;
495 __skb_pull(skb, data_len); 494 __skb_pull(skb, data_len);
496 495
497 if(!left) 496 if (!left)
498 continue; 497 continue;
499 498
500 memset(ptr, 0, left); 499 memset(ptr, 0, left);
@@ -506,7 +505,7 @@ static unsigned int usbatm_write_cells(struct usbatm_data *instance,
506 trailer[2] = ctrl->len >> 8; 505 trailer[2] = ctrl->len >> 8;
507 trailer[3] = ctrl->len; 506 trailer[3] = ctrl->len;
508 507
509 ctrl->crc = ~ crc32_be(ctrl->crc, ptr, left - 4); 508 ctrl->crc = ~crc32_be(ctrl->crc, ptr, left - 4);
510 509
511 trailer[4] = ctrl->crc >> 24; 510 trailer[4] = ctrl->crc >> 24;
512 trailer[5] = ctrl->crc >> 16; 511 trailer[5] = ctrl->crc >> 16;
@@ -516,8 +515,7 @@ static unsigned int usbatm_write_cells(struct usbatm_data *instance,
516 target[3] |= 0x2; /* adjust PTI */ 515 target[3] |= 0x2; /* adjust PTI */
517 516
518 ctrl->len = 0; /* tag this skb finished */ 517 ctrl->len = 0; /* tag this skb finished */
519 } 518 } else
520 else
521 ctrl->crc = crc32_be(ctrl->crc, ptr, left); 519 ctrl->crc = crc32_be(ctrl->crc, ptr, left);
522 } 520 }
523 521
@@ -1146,7 +1144,7 @@ int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
1146 instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->bulk_out); 1144 instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->bulk_out);
1147 1145
1148 /* tx buffer size must be a positive multiple of the stride */ 1146 /* tx buffer size must be a positive multiple of the stride */
1149 instance->tx_channel.buf_size = max (instance->tx_channel.stride, 1147 instance->tx_channel.buf_size = max(instance->tx_channel.stride,
1150 snd_buf_bytes - (snd_buf_bytes % instance->tx_channel.stride)); 1148 snd_buf_bytes - (snd_buf_bytes % instance->tx_channel.stride));
1151 1149
1152 /* rx buffer size must be a positive multiple of the endpoint maxpacket */ 1150 /* rx buffer size must be a positive multiple of the endpoint maxpacket */
@@ -1159,7 +1157,7 @@ int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
1159 goto fail_unbind; 1157 goto fail_unbind;
1160 } 1158 }
1161 1159
1162 num_packets = max (1U, (rcv_buf_bytes + maxpacket / 2) / maxpacket); /* round */ 1160 num_packets = max(1U, (rcv_buf_bytes + maxpacket / 2) / maxpacket); /* round */
1163 1161
1164 if (num_packets * maxpacket > UDSL_MAX_BUF_SIZE) 1162 if (num_packets * maxpacket > UDSL_MAX_BUF_SIZE)
1165 num_packets--; 1163 num_packets--;
@@ -1262,7 +1260,7 @@ int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
1262 usb_free_urb(instance->urbs[i]); 1260 usb_free_urb(instance->urbs[i]);
1263 } 1261 }
1264 1262
1265 kfree (instance); 1263 kfree(instance);
1266 1264
1267 return error; 1265 return error;
1268} 1266}
@@ -1390,9 +1388,8 @@ static int usbatm_print_packet(const unsigned char *data, int len)
1390 for (i = 0; i < len;) { 1388 for (i = 0; i < len;) {
1391 buffer[0] = '\0'; 1389 buffer[0] = '\0';
1392 sprintf(buffer, "%.3d :", i); 1390 sprintf(buffer, "%.3d :", i);
1393 for (j = 0; (j < 16) && (i < len); j++, i++) { 1391 for (j = 0; (j < 16) && (i < len); j++, i++)
1394 sprintf(buffer, "%s %2.2x", buffer, data[i]); 1392 sprintf(buffer, "%s %2.2x", buffer, data[i]);
1395 }
1396 dbg("%s", buffer); 1393 dbg("%s", buffer);
1397 } 1394 }
1398 return i; 1395 return i;
diff --git a/drivers/usb/atm/usbatm.h b/drivers/usb/atm/usbatm.h
index 0863f85fcc2..5fc48940521 100644
--- a/drivers/usb/atm/usbatm.h
+++ b/drivers/usb/atm/usbatm.h
@@ -48,7 +48,7 @@
48 dev_warn(&(instance)->usb_intf->dev, \ 48 dev_warn(&(instance)->usb_intf->dev, \
49 "failed assertion '%s' at line %d", \ 49 "failed assertion '%s' at line %d", \
50 __stringify(x), __LINE__); \ 50 __stringify(x), __LINE__); \
51 } while(0) 51 } while (0)
52#endif 52#endif
53 53
54#define usb_err(instance, format, arg...) \ 54#define usb_err(instance, format, arg...) \
@@ -59,7 +59,7 @@
59 dev_warn(&(instance)->usb_intf->dev , format , ## arg) 59 dev_warn(&(instance)->usb_intf->dev , format , ## arg)
60#ifdef DEBUG 60#ifdef DEBUG
61#define usb_dbg(instance, format, arg...) \ 61#define usb_dbg(instance, format, arg...) \
62 dev_printk(KERN_DEBUG , &(instance)->usb_intf->dev , format , ## arg) 62 dev_printk(KERN_DEBUG , &(instance)->usb_intf->dev , format , ## arg)
63#else 63#else
64#define usb_dbg(instance, format, arg...) \ 64#define usb_dbg(instance, format, arg...) \
65 do {} while (0) 65 do {} while (0)
@@ -104,21 +104,21 @@ struct usbatm_data;
104/* 104/*
105* Assuming all methods exist and succeed, they are called in this order: 105* Assuming all methods exist and succeed, they are called in this order:
106* 106*
107* bind, heavy_init, atm_start, ..., atm_stop, unbind 107* bind, heavy_init, atm_start, ..., atm_stop, unbind
108*/ 108*/
109 109
110struct usbatm_driver { 110struct usbatm_driver {
111 const char *driver_name; 111 const char *driver_name;
112 112
113 /* init device ... can sleep, or cause probe() failure */ 113 /* init device ... can sleep, or cause probe() failure */
114 int (*bind) (struct usbatm_data *, struct usb_interface *, 114 int (*bind) (struct usbatm_data *, struct usb_interface *,
115 const struct usb_device_id *id); 115 const struct usb_device_id *id);
116 116
117 /* additional device initialization that is too slow to be done in probe() */ 117 /* additional device initialization that is too slow to be done in probe() */
118 int (*heavy_init) (struct usbatm_data *, struct usb_interface *); 118 int (*heavy_init) (struct usbatm_data *, struct usb_interface *);
119 119
120 /* cleanup device ... can sleep, but can't fail */ 120 /* cleanup device ... can sleep, but can't fail */
121 void (*unbind) (struct usbatm_data *, struct usb_interface *); 121 void (*unbind) (struct usbatm_data *, struct usb_interface *);
122 122
123 /* init ATM device ... can sleep, or cause ATM initialization failure */ 123 /* init ATM device ... can sleep, or cause ATM initialization failure */
124 int (*atm_start) (struct usbatm_data *, struct atm_dev *); 124 int (*atm_start) (struct usbatm_data *, struct atm_dev *);
@@ -126,9 +126,9 @@ struct usbatm_driver {
126 /* cleanup ATM device ... can sleep, but can't fail */ 126 /* cleanup ATM device ... can sleep, but can't fail */
127 void (*atm_stop) (struct usbatm_data *, struct atm_dev *); 127 void (*atm_stop) (struct usbatm_data *, struct atm_dev *);
128 128
129 int bulk_in; /* bulk rx endpoint */ 129 int bulk_in; /* bulk rx endpoint */
130 int isoc_in; /* isochronous rx endpoint */ 130 int isoc_in; /* isochronous rx endpoint */
131 int bulk_out; /* bulk tx endpoint */ 131 int bulk_out; /* bulk tx endpoint */
132 132
133 unsigned rx_padding; 133 unsigned rx_padding;
134 unsigned tx_padding; 134 unsigned tx_padding;
@@ -156,7 +156,7 @@ struct usbatm_channel {
156struct usbatm_data { 156struct usbatm_data {
157 /****************** 157 /******************
158 * public fields * 158 * public fields *
159 ******************/ 159 ******************/
160 160
161 /* mini driver */ 161 /* mini driver */
162 struct usbatm_driver *driver; 162 struct usbatm_driver *driver;
@@ -174,7 +174,7 @@ struct usbatm_data {
174 174
175 /******************************** 175 /********************************
176 * private fields - do not use * 176 * private fields - do not use *
177 ********************************/ 177 ********************************/
178 178
179 struct kref refcount; 179 struct kref refcount;
180 struct mutex serialize; 180 struct mutex serialize;
diff --git a/drivers/usb/atm/xusbatm.c b/drivers/usb/atm/xusbatm.c
index 17d167bbd2d..48ee0c5ff28 100644
--- a/drivers/usb/atm/xusbatm.c
+++ b/drivers/usb/atm/xusbatm.c
@@ -49,13 +49,13 @@ static struct usbatm_driver xusbatm_drivers[XUSBATM_DRIVERS_MAX];
49static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1]; 49static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1];
50static struct usb_driver xusbatm_usb_driver; 50static struct usb_driver xusbatm_usb_driver;
51 51
52static struct usb_interface *xusbatm_find_intf (struct usb_device *usb_dev, int altsetting, u8 ep) 52static struct usb_interface *xusbatm_find_intf(struct usb_device *usb_dev, int altsetting, u8 ep)
53{ 53{
54 struct usb_host_interface *alt; 54 struct usb_host_interface *alt;
55 struct usb_interface *intf; 55 struct usb_interface *intf;
56 int i, j; 56 int i, j;
57 57
58 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) 58 for (i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++)
59 if ((intf = usb_dev->actconfig->interface[i]) && (alt = usb_altnum_to_altsetting(intf, altsetting))) 59 if ((intf = usb_dev->actconfig->interface[i]) && (alt = usb_altnum_to_altsetting(intf, altsetting)))
60 for (j = 0; j < alt->desc.bNumEndpoints; j++) 60 for (j = 0; j < alt->desc.bNumEndpoints; j++)
61 if (alt->endpoint[j].desc.bEndpointAddress == ep) 61 if (alt->endpoint[j].desc.bEndpointAddress == ep)
@@ -63,7 +63,7 @@ static struct usb_interface *xusbatm_find_intf (struct usb_device *usb_dev, int
63 return NULL; 63 return NULL;
64} 64}
65 65
66static int xusbatm_capture_intf (struct usbatm_data *usbatm, struct usb_device *usb_dev, 66static int xusbatm_capture_intf(struct usbatm_data *usbatm, struct usb_device *usb_dev,
67 struct usb_interface *intf, int altsetting, int claim) 67 struct usb_interface *intf, int altsetting, int claim)
68{ 68{
69 int ifnum = intf->altsetting->desc.bInterfaceNumber; 69 int ifnum = intf->altsetting->desc.bInterfaceNumber;
@@ -80,7 +80,7 @@ static int xusbatm_capture_intf (struct usbatm_data *usbatm, struct usb_device *
80 return 0; 80 return 0;
81} 81}
82 82
83static void xusbatm_release_intf (struct usb_device *usb_dev, struct usb_interface *intf, int claimed) 83static void xusbatm_release_intf(struct usb_device *usb_dev, struct usb_interface *intf, int claimed)
84{ 84{
85 if (claimed) { 85 if (claimed) {
86 usb_set_intfdata(intf, NULL); 86 usb_set_intfdata(intf, NULL);
@@ -147,7 +147,7 @@ static void xusbatm_unbind(struct usbatm_data *usbatm,
147 147
148 usb_dbg(usbatm, "%s entered\n", __func__); 148 usb_dbg(usbatm, "%s entered\n", __func__);
149 149
150 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) { 150 for (i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
151 struct usb_interface *cur_intf = usb_dev->actconfig->interface[i]; 151 struct usb_interface *cur_intf = usb_dev->actconfig->interface[i];
152 152
153 if (cur_intf && (usb_get_intfdata(cur_intf) == usbatm)) { 153 if (cur_intf && (usb_get_intfdata(cur_intf) == usbatm)) {
diff --git a/drivers/usb/c67x00/c67x00-hcd.c b/drivers/usb/c67x00/c67x00-hcd.c
index a22b887f4e9..d3e1356d091 100644
--- a/drivers/usb/c67x00/c67x00-hcd.c
+++ b/drivers/usb/c67x00/c67x00-hcd.c
@@ -264,7 +264,7 @@ static void c67x00_hcd_irq(struct c67x00_sie *sie, u16 int_status, u16 msg)
264 if (unlikely(hcd->state == HC_STATE_HALT)) 264 if (unlikely(hcd->state == HC_STATE_HALT))
265 return; 265 return;
266 266
267 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) 267 if (!HCD_HW_ACCESSIBLE(hcd))
268 return; 268 return;
269 269
270 /* Handle Start of frame events */ 270 /* Handle Start of frame events */
@@ -282,7 +282,7 @@ static int c67x00_hcd_start(struct usb_hcd *hcd)
282{ 282{
283 hcd->uses_new_polling = 1; 283 hcd->uses_new_polling = 1;
284 hcd->state = HC_STATE_RUNNING; 284 hcd->state = HC_STATE_RUNNING;
285 hcd->poll_rh = 1; 285 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
286 286
287 return 0; 287 return 0;
288} 288}
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index 89d260d6b03..1833b3a7151 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -636,19 +636,13 @@ static void acm_tty_unregister(struct acm *acm)
636 636
637static int acm_tty_chars_in_buffer(struct tty_struct *tty); 637static int acm_tty_chars_in_buffer(struct tty_struct *tty);
638 638
639static void acm_port_down(struct acm *acm, int drain) 639static void acm_port_down(struct acm *acm)
640{ 640{
641 int i, nr = acm->rx_buflimit; 641 int i, nr = acm->rx_buflimit;
642 mutex_lock(&open_mutex); 642 mutex_lock(&open_mutex);
643 if (acm->dev) { 643 if (acm->dev) {
644 usb_autopm_get_interface(acm->control); 644 usb_autopm_get_interface(acm->control);
645 acm_set_control(acm, acm->ctrlout = 0); 645 acm_set_control(acm, acm->ctrlout = 0);
646 /* try letting the last writes drain naturally */
647 if (drain) {
648 wait_event_interruptible_timeout(acm->drain_wait,
649 (ACM_NW == acm_wb_is_avail(acm)) || !acm->dev,
650 ACM_CLOSE_TIMEOUT * HZ);
651 }
652 usb_kill_urb(acm->ctrlurb); 646 usb_kill_urb(acm->ctrlurb);
653 for (i = 0; i < ACM_NW; i++) 647 for (i = 0; i < ACM_NW; i++)
654 usb_kill_urb(acm->wb[i].urb); 648 usb_kill_urb(acm->wb[i].urb);
@@ -664,7 +658,7 @@ static void acm_tty_hangup(struct tty_struct *tty)
664{ 658{
665 struct acm *acm = tty->driver_data; 659 struct acm *acm = tty->driver_data;
666 tty_port_hangup(&acm->port); 660 tty_port_hangup(&acm->port);
667 acm_port_down(acm, 0); 661 acm_port_down(acm);
668} 662}
669 663
670static void acm_tty_close(struct tty_struct *tty, struct file *filp) 664static void acm_tty_close(struct tty_struct *tty, struct file *filp)
@@ -685,7 +679,7 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
685 mutex_unlock(&open_mutex); 679 mutex_unlock(&open_mutex);
686 return; 680 return;
687 } 681 }
688 acm_port_down(acm, 0); 682 acm_port_down(acm);
689 tty_port_close_end(&acm->port, tty); 683 tty_port_close_end(&acm->port, tty);
690 tty_port_tty_set(&acm->port, NULL); 684 tty_port_tty_set(&acm->port, NULL);
691} 685}
diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c
index 84f9e52327f..e325162859b 100644
--- a/drivers/usb/class/usblp.c
+++ b/drivers/usb/class/usblp.c
@@ -135,7 +135,7 @@ MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:H
135 * ->lock locks what interrupt accesses. 135 * ->lock locks what interrupt accesses.
136 */ 136 */
137struct usblp { 137struct usblp {
138 struct usb_device *dev; /* USB device */ 138 struct usb_device *dev; /* USB device */
139 struct mutex wmut; 139 struct mutex wmut;
140 struct mutex mut; 140 struct mutex mut;
141 spinlock_t lock; /* locks rcomplete, wcomplete */ 141 spinlock_t lock; /* locks rcomplete, wcomplete */
@@ -169,7 +169,8 @@ struct usblp {
169}; 169};
170 170
171#ifdef DEBUG 171#ifdef DEBUG
172static void usblp_dump(struct usblp *usblp) { 172static void usblp_dump(struct usblp *usblp)
173{
173 int p; 174 int p;
174 175
175 dbg("usblp=0x%p", usblp); 176 dbg("usblp=0x%p", usblp);
@@ -216,8 +217,8 @@ static const struct quirk_printer_struct quirk_printers[] = {
216 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */ 217 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
217 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */ 218 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
218 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */ 219 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
219 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */ 220 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
220 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */ 221 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
221 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */ 222 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
222 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */ 223 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
223 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */ 224 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
@@ -254,9 +255,8 @@ static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, i
254 /* High byte has the interface index. 255 /* High byte has the interface index.
255 Low byte has the alternate setting. 256 Low byte has the alternate setting.
256 */ 257 */
257 if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) { 258 if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS))
258 index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting; 259 index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
259 }
260 260
261 retval = usb_control_msg(usblp->dev, 261 retval = usb_control_msg(usblp->dev,
262 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0), 262 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
@@ -372,7 +372,7 @@ static int usblp_check_status(struct usblp *usblp, int err)
372 return newerr; 372 return newerr;
373} 373}
374 374
375static int handle_bidir (struct usblp *usblp) 375static int handle_bidir(struct usblp *usblp)
376{ 376{
377 if (usblp->bidir && usblp->used) { 377 if (usblp->bidir && usblp->used) {
378 if (usblp_submit_read(usblp) < 0) 378 if (usblp_submit_read(usblp) < 0)
@@ -395,14 +395,13 @@ static int usblp_open(struct inode *inode, struct file *file)
395 if (minor < 0) 395 if (minor < 0)
396 return -ENODEV; 396 return -ENODEV;
397 397
398 mutex_lock (&usblp_mutex); 398 mutex_lock(&usblp_mutex);
399 399
400 retval = -ENODEV; 400 retval = -ENODEV;
401 intf = usb_find_interface(&usblp_driver, minor); 401 intf = usb_find_interface(&usblp_driver, minor);
402 if (!intf) { 402 if (!intf)
403 goto out; 403 goto out;
404 } 404 usblp = usb_get_intfdata(intf);
405 usblp = usb_get_intfdata (intf);
406 if (!usblp || !usblp->dev || !usblp->present) 405 if (!usblp || !usblp->dev || !usblp->present)
407 goto out; 406 goto out;
408 407
@@ -433,18 +432,18 @@ static int usblp_open(struct inode *inode, struct file *file)
433 retval = -EIO; 432 retval = -EIO;
434 } 433 }
435out: 434out:
436 mutex_unlock (&usblp_mutex); 435 mutex_unlock(&usblp_mutex);
437 return retval; 436 return retval;
438} 437}
439 438
440static void usblp_cleanup (struct usblp *usblp) 439static void usblp_cleanup(struct usblp *usblp)
441{ 440{
442 printk(KERN_INFO "usblp%d: removed\n", usblp->minor); 441 printk(KERN_INFO "usblp%d: removed\n", usblp->minor);
443 442
444 kfree(usblp->readbuf); 443 kfree(usblp->readbuf);
445 kfree (usblp->device_id_string); 444 kfree(usblp->device_id_string);
446 kfree (usblp->statusbuf); 445 kfree(usblp->statusbuf);
447 kfree (usblp); 446 kfree(usblp);
448} 447}
449 448
450static void usblp_unlink_urbs(struct usblp *usblp) 449static void usblp_unlink_urbs(struct usblp *usblp)
@@ -458,14 +457,14 @@ static int usblp_release(struct inode *inode, struct file *file)
458 457
459 usblp->flags &= ~LP_ABORT; 458 usblp->flags &= ~LP_ABORT;
460 459
461 mutex_lock (&usblp_mutex); 460 mutex_lock(&usblp_mutex);
462 usblp->used = 0; 461 usblp->used = 0;
463 if (usblp->present) { 462 if (usblp->present) {
464 usblp_unlink_urbs(usblp); 463 usblp_unlink_urbs(usblp);
465 usb_autopm_put_interface(usblp->intf); 464 usb_autopm_put_interface(usblp->intf);
466 } else /* finish cleanup from disconnect */ 465 } else /* finish cleanup from disconnect */
467 usblp_cleanup (usblp); 466 usblp_cleanup(usblp);
468 mutex_unlock (&usblp_mutex); 467 mutex_unlock(&usblp_mutex);
469 return 0; 468 return 0;
470} 469}
471 470
@@ -495,190 +494,190 @@ static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
495 int twoints[2]; 494 int twoints[2];
496 int retval = 0; 495 int retval = 0;
497 496
498 mutex_lock (&usblp->mut); 497 mutex_lock(&usblp->mut);
499 if (!usblp->present) { 498 if (!usblp->present) {
500 retval = -ENODEV; 499 retval = -ENODEV;
501 goto done; 500 goto done;
502 } 501 }
503 502
504 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd), 503 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
505 _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) ); 504 _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd));
506 505
507 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */ 506 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
508 507
509 switch (_IOC_NR(cmd)) { 508 switch (_IOC_NR(cmd)) {
510 509
511 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */ 510 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
512 if (_IOC_DIR(cmd) != _IOC_READ) { 511 if (_IOC_DIR(cmd) != _IOC_READ) {
513 retval = -EINVAL; 512 retval = -EINVAL;
514 goto done; 513 goto done;
515 } 514 }
516 515
517 length = usblp_cache_device_id_string(usblp); 516 length = usblp_cache_device_id_string(usblp);
518 if (length < 0) { 517 if (length < 0) {
519 retval = length; 518 retval = length;
520 goto done; 519 goto done;
521 } 520 }
522 if (length > _IOC_SIZE(cmd)) 521 if (length > _IOC_SIZE(cmd))
523 length = _IOC_SIZE(cmd); /* truncate */ 522 length = _IOC_SIZE(cmd); /* truncate */
524 523
525 if (copy_to_user((void __user *) arg, 524 if (copy_to_user((void __user *) arg,
526 usblp->device_id_string, 525 usblp->device_id_string,
527 (unsigned long) length)) { 526 (unsigned long) length)) {
528 retval = -EFAULT; 527 retval = -EFAULT;
529 goto done; 528 goto done;
530 } 529 }
531 530
532 break; 531 break;
533 532
534 case IOCNR_GET_PROTOCOLS: 533 case IOCNR_GET_PROTOCOLS:
535 if (_IOC_DIR(cmd) != _IOC_READ || 534 if (_IOC_DIR(cmd) != _IOC_READ ||
536 _IOC_SIZE(cmd) < sizeof(twoints)) { 535 _IOC_SIZE(cmd) < sizeof(twoints)) {
537 retval = -EINVAL; 536 retval = -EINVAL;
538 goto done; 537 goto done;
539 } 538 }
540 539
541 twoints[0] = usblp->current_protocol; 540 twoints[0] = usblp->current_protocol;
542 twoints[1] = 0; 541 twoints[1] = 0;
543 for (i = USBLP_FIRST_PROTOCOL; 542 for (i = USBLP_FIRST_PROTOCOL;
544 i <= USBLP_LAST_PROTOCOL; i++) { 543 i <= USBLP_LAST_PROTOCOL; i++) {
545 if (usblp->protocol[i].alt_setting >= 0) 544 if (usblp->protocol[i].alt_setting >= 0)
546 twoints[1] |= (1<<i); 545 twoints[1] |= (1<<i);
547 } 546 }
548 547
549 if (copy_to_user((void __user *)arg, 548 if (copy_to_user((void __user *)arg,
550 (unsigned char *)twoints, 549 (unsigned char *)twoints,
551 sizeof(twoints))) { 550 sizeof(twoints))) {
552 retval = -EFAULT; 551 retval = -EFAULT;
553 goto done; 552 goto done;
554 } 553 }
555 554
556 break; 555 break;
557 556
558 case IOCNR_SET_PROTOCOL: 557 case IOCNR_SET_PROTOCOL:
559 if (_IOC_DIR(cmd) != _IOC_WRITE) { 558 if (_IOC_DIR(cmd) != _IOC_WRITE) {
560 retval = -EINVAL; 559 retval = -EINVAL;
561 goto done; 560 goto done;
562 } 561 }
563 562
564#ifdef DEBUG 563#ifdef DEBUG
565 if (arg == -10) { 564 if (arg == -10) {
566 usblp_dump(usblp); 565 usblp_dump(usblp);
567 break; 566 break;
568 } 567 }
569#endif 568#endif
570 569
571 usblp_unlink_urbs(usblp); 570 usblp_unlink_urbs(usblp);
572 retval = usblp_set_protocol(usblp, arg); 571 retval = usblp_set_protocol(usblp, arg);
573 if (retval < 0) { 572 if (retval < 0) {
574 usblp_set_protocol(usblp, 573 usblp_set_protocol(usblp,
575 usblp->current_protocol); 574 usblp->current_protocol);
576 } 575 }
577 break; 576 break;
578 577
579 case IOCNR_HP_SET_CHANNEL: 578 case IOCNR_HP_SET_CHANNEL:
580 if (_IOC_DIR(cmd) != _IOC_WRITE || 579 if (_IOC_DIR(cmd) != _IOC_WRITE ||
581 le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 || 580 le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
582 usblp->quirks & USBLP_QUIRK_BIDIR) { 581 usblp->quirks & USBLP_QUIRK_BIDIR) {
583 retval = -EINVAL; 582 retval = -EINVAL;
584 goto done; 583 goto done;
585 } 584 }
586 585
587 err = usblp_hp_channel_change_request(usblp, 586 err = usblp_hp_channel_change_request(usblp,
588 arg, &newChannel); 587 arg, &newChannel);
589 if (err < 0) { 588 if (err < 0) {
590 dev_err(&usblp->dev->dev, 589 dev_err(&usblp->dev->dev,
591 "usblp%d: error = %d setting " 590 "usblp%d: error = %d setting "
592 "HP channel\n", 591 "HP channel\n",
593 usblp->minor, err); 592 usblp->minor, err);
594 retval = -EIO; 593 retval = -EIO;
595 goto done; 594 goto done;
596 } 595 }
597 596
598 dbg("usblp%d requested/got HP channel %ld/%d", 597 dbg("usblp%d requested/got HP channel %ld/%d",
599 usblp->minor, arg, newChannel); 598 usblp->minor, arg, newChannel);
600 break; 599 break;
601 600
602 case IOCNR_GET_BUS_ADDRESS: 601 case IOCNR_GET_BUS_ADDRESS:
603 if (_IOC_DIR(cmd) != _IOC_READ || 602 if (_IOC_DIR(cmd) != _IOC_READ ||
604 _IOC_SIZE(cmd) < sizeof(twoints)) { 603 _IOC_SIZE(cmd) < sizeof(twoints)) {
605 retval = -EINVAL; 604 retval = -EINVAL;
606 goto done; 605 goto done;
607 } 606 }
608 607
609 twoints[0] = usblp->dev->bus->busnum; 608 twoints[0] = usblp->dev->bus->busnum;
610 twoints[1] = usblp->dev->devnum; 609 twoints[1] = usblp->dev->devnum;
611 if (copy_to_user((void __user *)arg, 610 if (copy_to_user((void __user *)arg,
612 (unsigned char *)twoints, 611 (unsigned char *)twoints,
613 sizeof(twoints))) { 612 sizeof(twoints))) {
614 retval = -EFAULT; 613 retval = -EFAULT;
615 goto done; 614 goto done;
616 } 615 }
617 616
618 dbg("usblp%d is bus=%d, device=%d", 617 dbg("usblp%d is bus=%d, device=%d",
619 usblp->minor, twoints[0], twoints[1]); 618 usblp->minor, twoints[0], twoints[1]);
620 break; 619 break;
621 620
622 case IOCNR_GET_VID_PID: 621 case IOCNR_GET_VID_PID:
623 if (_IOC_DIR(cmd) != _IOC_READ || 622 if (_IOC_DIR(cmd) != _IOC_READ ||
624 _IOC_SIZE(cmd) < sizeof(twoints)) { 623 _IOC_SIZE(cmd) < sizeof(twoints)) {
625 retval = -EINVAL; 624 retval = -EINVAL;
626 goto done; 625 goto done;
627 } 626 }
628 627
629 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor); 628 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
630 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct); 629 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
631 if (copy_to_user((void __user *)arg, 630 if (copy_to_user((void __user *)arg,
632 (unsigned char *)twoints, 631 (unsigned char *)twoints,
633 sizeof(twoints))) { 632 sizeof(twoints))) {
634 retval = -EFAULT; 633 retval = -EFAULT;
635 goto done; 634 goto done;
636 } 635 }
637 636
638 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X", 637 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
639 usblp->minor, twoints[0], twoints[1]); 638 usblp->minor, twoints[0], twoints[1]);
640 break; 639 break;
641 640
642 case IOCNR_SOFT_RESET: 641 case IOCNR_SOFT_RESET:
643 if (_IOC_DIR(cmd) != _IOC_NONE) { 642 if (_IOC_DIR(cmd) != _IOC_NONE) {
644 retval = -EINVAL; 643 retval = -EINVAL;
645 goto done; 644 goto done;
646 } 645 }
647 retval = usblp_reset(usblp); 646 retval = usblp_reset(usblp);
648 break; 647 break;
649 default: 648 default:
650 retval = -ENOTTY; 649 retval = -ENOTTY;
651 } 650 }
652 else /* old-style ioctl value */ 651 else /* old-style ioctl value */
653 switch (cmd) { 652 switch (cmd) {
654 653
655 case LPGETSTATUS: 654 case LPGETSTATUS:
656 if ((retval = usblp_read_status(usblp, usblp->statusbuf))) { 655 if ((retval = usblp_read_status(usblp, usblp->statusbuf))) {
657 if (printk_ratelimit()) 656 if (printk_ratelimit())
658 printk(KERN_ERR "usblp%d:" 657 printk(KERN_ERR "usblp%d:"
659 "failed reading printer status (%d)\n", 658 "failed reading printer status (%d)\n",
660 usblp->minor, retval); 659 usblp->minor, retval);
661 retval = -EIO; 660 retval = -EIO;
662 goto done; 661 goto done;
663 } 662 }
664 status = *usblp->statusbuf; 663 status = *usblp->statusbuf;
665 if (copy_to_user ((void __user *)arg, &status, sizeof(int))) 664 if (copy_to_user((void __user *)arg, &status, sizeof(int)))
666 retval = -EFAULT; 665 retval = -EFAULT;
667 break; 666 break;
668 667
669 case LPABORT: 668 case LPABORT:
670 if (arg) 669 if (arg)
671 usblp->flags |= LP_ABORT; 670 usblp->flags |= LP_ABORT;
672 else 671 else
673 usblp->flags &= ~LP_ABORT; 672 usblp->flags &= ~LP_ABORT;
674 break; 673 break;
675 674
676 default: 675 default:
677 retval = -ENOTTY; 676 retval = -ENOTTY;
678 } 677 }
679 678
680done: 679done:
681 mutex_unlock (&usblp->mut); 680 mutex_unlock(&usblp->mut);
682 return retval; 681 return retval;
683} 682}
684 683
@@ -840,7 +839,7 @@ static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, lo
840 } 839 }
841 840
842done: 841done:
843 mutex_unlock (&usblp->mut); 842 mutex_unlock(&usblp->mut);
844 return count; 843 return count;
845} 844}
846 845
@@ -1023,7 +1022,7 @@ raise_urb:
1023 * while you are sending print data, and you don't try to query the 1022 * while you are sending print data, and you don't try to query the
1024 * printer status every couple of milliseconds, you will probably be OK. 1023 * printer status every couple of milliseconds, you will probably be OK.
1025 */ 1024 */
1026static unsigned int usblp_quirks (__u16 vendor, __u16 product) 1025static unsigned int usblp_quirks(__u16 vendor, __u16 product)
1027{ 1026{
1028 int i; 1027 int i;
1029 1028
@@ -1031,7 +1030,7 @@ static unsigned int usblp_quirks (__u16 vendor, __u16 product)
1031 if (vendor == quirk_printers[i].vendorId && 1030 if (vendor == quirk_printers[i].vendorId &&
1032 product == quirk_printers[i].productId) 1031 product == quirk_printers[i].productId)
1033 return quirk_printers[i].quirks; 1032 return quirk_printers[i].quirks;
1034 } 1033 }
1035 return 0; 1034 return 0;
1036} 1035}
1037 1036
@@ -1061,7 +1060,7 @@ static struct usb_class_driver usblp_class = {
1061static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf) 1060static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
1062{ 1061{
1063 struct usb_interface *intf = to_usb_interface(dev); 1062 struct usb_interface *intf = to_usb_interface(dev);
1064 struct usblp *usblp = usb_get_intfdata (intf); 1063 struct usblp *usblp = usb_get_intfdata(intf);
1065 1064
1066 if (usblp->device_id_string[0] == 0 && 1065 if (usblp->device_id_string[0] == 0 &&
1067 usblp->device_id_string[1] == 0) 1066 usblp->device_id_string[1] == 0)
@@ -1075,7 +1074,7 @@ static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
1075static int usblp_probe(struct usb_interface *intf, 1074static int usblp_probe(struct usb_interface *intf,
1076 const struct usb_device_id *id) 1075 const struct usb_device_id *id)
1077{ 1076{
1078 struct usb_device *dev = interface_to_usbdev (intf); 1077 struct usb_device *dev = interface_to_usbdev(intf);
1079 struct usblp *usblp; 1078 struct usblp *usblp;
1080 int protocol; 1079 int protocol;
1081 int retval; 1080 int retval;
@@ -1089,7 +1088,7 @@ static int usblp_probe(struct usb_interface *intf,
1089 } 1088 }
1090 usblp->dev = dev; 1089 usblp->dev = dev;
1091 mutex_init(&usblp->wmut); 1090 mutex_init(&usblp->wmut);
1092 mutex_init (&usblp->mut); 1091 mutex_init(&usblp->mut);
1093 spin_lock_init(&usblp->lock); 1092 spin_lock_init(&usblp->lock);
1094 init_waitqueue_head(&usblp->rwait); 1093 init_waitqueue_head(&usblp->rwait);
1095 init_waitqueue_head(&usblp->wwait); 1094 init_waitqueue_head(&usblp->wwait);
@@ -1153,7 +1152,7 @@ static int usblp_probe(struct usb_interface *intf,
1153 usblp_check_status(usblp, 0); 1152 usblp_check_status(usblp, 0);
1154#endif 1153#endif
1155 1154
1156 usb_set_intfdata (intf, usblp); 1155 usb_set_intfdata(intf, usblp);
1157 1156
1158 usblp->present = 1; 1157 usblp->present = 1;
1159 1158
@@ -1177,7 +1176,7 @@ static int usblp_probe(struct usb_interface *intf,
1177 return 0; 1176 return 0;
1178 1177
1179abort_intfdata: 1178abort_intfdata:
1180 usb_set_intfdata (intf, NULL); 1179 usb_set_intfdata(intf, NULL);
1181 device_remove_file(&intf->dev, &dev_attr_ieee1284_id); 1180 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1182abort: 1181abort:
1183 kfree(usblp->readbuf); 1182 kfree(usblp->readbuf);
@@ -1340,35 +1339,35 @@ static int usblp_cache_device_id_string(struct usblp *usblp)
1340 1339
1341static void usblp_disconnect(struct usb_interface *intf) 1340static void usblp_disconnect(struct usb_interface *intf)
1342{ 1341{
1343 struct usblp *usblp = usb_get_intfdata (intf); 1342 struct usblp *usblp = usb_get_intfdata(intf);
1344 1343
1345 usb_deregister_dev(intf, &usblp_class); 1344 usb_deregister_dev(intf, &usblp_class);
1346 1345
1347 if (!usblp || !usblp->dev) { 1346 if (!usblp || !usblp->dev) {
1348 dev_err(&intf->dev, "bogus disconnect\n"); 1347 dev_err(&intf->dev, "bogus disconnect\n");
1349 BUG (); 1348 BUG();
1350 } 1349 }
1351 1350
1352 device_remove_file(&intf->dev, &dev_attr_ieee1284_id); 1351 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1353 1352
1354 mutex_lock (&usblp_mutex); 1353 mutex_lock(&usblp_mutex);
1355 mutex_lock (&usblp->mut); 1354 mutex_lock(&usblp->mut);
1356 usblp->present = 0; 1355 usblp->present = 0;
1357 wake_up(&usblp->wwait); 1356 wake_up(&usblp->wwait);
1358 wake_up(&usblp->rwait); 1357 wake_up(&usblp->rwait);
1359 usb_set_intfdata (intf, NULL); 1358 usb_set_intfdata(intf, NULL);
1360 1359
1361 usblp_unlink_urbs(usblp); 1360 usblp_unlink_urbs(usblp);
1362 mutex_unlock (&usblp->mut); 1361 mutex_unlock(&usblp->mut);
1363 1362
1364 if (!usblp->used) 1363 if (!usblp->used)
1365 usblp_cleanup (usblp); 1364 usblp_cleanup(usblp);
1366 mutex_unlock (&usblp_mutex); 1365 mutex_unlock(&usblp_mutex);
1367} 1366}
1368 1367
1369static int usblp_suspend(struct usb_interface *intf, pm_message_t message) 1368static int usblp_suspend(struct usb_interface *intf, pm_message_t message)
1370{ 1369{
1371 struct usblp *usblp = usb_get_intfdata (intf); 1370 struct usblp *usblp = usb_get_intfdata(intf);
1372 1371
1373 usblp_unlink_urbs(usblp); 1372 usblp_unlink_urbs(usblp);
1374#if 0 /* XXX Do we want this? What if someone is reading, should we fail? */ 1373#if 0 /* XXX Do we want this? What if someone is reading, should we fail? */
@@ -1382,10 +1381,10 @@ static int usblp_suspend(struct usb_interface *intf, pm_message_t message)
1382 1381
1383static int usblp_resume(struct usb_interface *intf) 1382static int usblp_resume(struct usb_interface *intf)
1384{ 1383{
1385 struct usblp *usblp = usb_get_intfdata (intf); 1384 struct usblp *usblp = usb_get_intfdata(intf);
1386 int r; 1385 int r;
1387 1386
1388 r = handle_bidir (usblp); 1387 r = handle_bidir(usblp);
1389 1388
1390 return r; 1389 return r;
1391} 1390}
@@ -1401,7 +1400,7 @@ static const struct usb_device_id usblp_ids[] = {
1401 { } /* Terminating entry */ 1400 { } /* Terminating entry */
1402}; 1401};
1403 1402
1404MODULE_DEVICE_TABLE (usb, usblp_ids); 1403MODULE_DEVICE_TABLE(usb, usblp_ids);
1405 1404
1406static struct usb_driver usblp_driver = { 1405static struct usb_driver usblp_driver = {
1407 .name = "usblp", 1406 .name = "usblp",
@@ -1426,8 +1425,8 @@ static void __exit usblp_exit(void)
1426module_init(usblp_init); 1425module_init(usblp_init);
1427module_exit(usblp_exit); 1426module_exit(usblp_exit);
1428 1427
1429MODULE_AUTHOR( DRIVER_AUTHOR ); 1428MODULE_AUTHOR(DRIVER_AUTHOR);
1430MODULE_DESCRIPTION( DRIVER_DESC ); 1429MODULE_DESCRIPTION(DRIVER_DESC);
1431module_param(proto_bias, int, S_IRUGO | S_IWUSR); 1430module_param(proto_bias, int, S_IRUGO | S_IWUSR);
1432MODULE_PARM_DESC(proto_bias, "Favourite protocol number"); 1431MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1433MODULE_LICENSE("GPL"); 1432MODULE_LICENSE("GPL");
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index c2f62a3993d..f1aaff6202a 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -1668,13 +1668,10 @@ static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
1668 default: 1668 default:
1669 if (intf->dev.driver) 1669 if (intf->dev.driver)
1670 driver = to_usb_driver(intf->dev.driver); 1670 driver = to_usb_driver(intf->dev.driver);
1671 if (driver == NULL || driver->ioctl == NULL) { 1671 if (driver == NULL || driver->unlocked_ioctl == NULL) {
1672 retval = -ENOTTY; 1672 retval = -ENOTTY;
1673 } else { 1673 } else {
1674 /* keep API that guarantees BKL */ 1674 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
1675 lock_kernel();
1676 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
1677 unlock_kernel();
1678 if (retval == -ENOIOCTLCMD) 1675 if (retval == -ENOIOCTLCMD)
1679 retval = -ENOTTY; 1676 retval = -ENOTTY;
1680 } 1677 }
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index a6bd53ace03..d7a4401ef01 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -1742,9 +1742,8 @@ static int usb_runtime_suspend(struct device *dev)
1742 } 1742 }
1743 1743
1744 /* Prevent the parent from suspending immediately after */ 1744 /* Prevent the parent from suspending immediately after */
1745 else if (udev->parent) { 1745 else if (udev->parent)
1746 udev->parent->last_busy = jiffies; 1746 udev->parent->last_busy = jiffies;
1747 }
1748 } 1747 }
1749 1748
1750 /* Runtime suspend for a USB interface doesn't mean anything. */ 1749 /* Runtime suspend for a USB interface doesn't mean anything. */
@@ -1786,21 +1785,19 @@ static int usb_runtime_idle(struct device *dev)
1786 return 0; 1785 return 0;
1787} 1786}
1788 1787
1789static struct dev_pm_ops usb_bus_pm_ops = { 1788static const struct dev_pm_ops usb_bus_pm_ops = {
1790 .runtime_suspend = usb_runtime_suspend, 1789 .runtime_suspend = usb_runtime_suspend,
1791 .runtime_resume = usb_runtime_resume, 1790 .runtime_resume = usb_runtime_resume,
1792 .runtime_idle = usb_runtime_idle, 1791 .runtime_idle = usb_runtime_idle,
1793}; 1792};
1794 1793
1795#else
1796
1797#define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL)
1798
1799#endif /* CONFIG_USB_SUSPEND */ 1794#endif /* CONFIG_USB_SUSPEND */
1800 1795
1801struct bus_type usb_bus_type = { 1796struct bus_type usb_bus_type = {
1802 .name = "usb", 1797 .name = "usb",
1803 .match = usb_device_match, 1798 .match = usb_device_match,
1804 .uevent = usb_uevent, 1799 .uevent = usb_uevent,
1800#ifdef CONFIG_USB_SUSPEND
1805 .pm = &usb_bus_pm_ops, 1801 .pm = &usb_bus_pm_ops,
1802#endif
1806}; 1803};
diff --git a/drivers/usb/core/endpoint.c b/drivers/usb/core/endpoint.c
index 4f84a41ee7a..3788e738e26 100644
--- a/drivers/usb/core/endpoint.c
+++ b/drivers/usb/core/endpoint.c
@@ -96,16 +96,21 @@ static ssize_t show_ep_interval(struct device *dev,
96 96
97 switch (usb_endpoint_type(ep->desc)) { 97 switch (usb_endpoint_type(ep->desc)) {
98 case USB_ENDPOINT_XFER_CONTROL: 98 case USB_ENDPOINT_XFER_CONTROL:
99 if (ep->udev->speed == USB_SPEED_HIGH) /* uframes per NAK */ 99 if (ep->udev->speed == USB_SPEED_HIGH)
100 /* uframes per NAK */
100 interval = ep->desc->bInterval; 101 interval = ep->desc->bInterval;
101 break; 102 break;
103
102 case USB_ENDPOINT_XFER_ISOC: 104 case USB_ENDPOINT_XFER_ISOC:
103 interval = 1 << (ep->desc->bInterval - 1); 105 interval = 1 << (ep->desc->bInterval - 1);
104 break; 106 break;
107
105 case USB_ENDPOINT_XFER_BULK: 108 case USB_ENDPOINT_XFER_BULK:
106 if (ep->udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */ 109 if (ep->udev->speed == USB_SPEED_HIGH && !in)
110 /* uframes per NAK */
107 interval = ep->desc->bInterval; 111 interval = ep->desc->bInterval;
108 break; 112 break;
113
109 case USB_ENDPOINT_XFER_INT: 114 case USB_ENDPOINT_XFER_INT:
110 if (ep->udev->speed == USB_SPEED_HIGH) 115 if (ep->udev->speed == USB_SPEED_HIGH)
111 interval = 1 << (ep->desc->bInterval - 1); 116 interval = 1 << (ep->desc->bInterval - 1);
diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c
index 9a34ccb0a1c..69ecd3c9231 100644
--- a/drivers/usb/core/generic.c
+++ b/drivers/usb/core/generic.c
@@ -105,8 +105,10 @@ int usb_choose_configuration(struct usb_device *udev)
105 /* When the first config's first interface is one of Microsoft's 105 /* When the first config's first interface is one of Microsoft's
106 * pet nonstandard Ethernet-over-USB protocols, ignore it unless 106 * pet nonstandard Ethernet-over-USB protocols, ignore it unless
107 * this kernel has enabled the necessary host side driver. 107 * this kernel has enabled the necessary host side driver.
108 * But: Don't ignore it if it's the only config.
108 */ 109 */
109 if (i == 0 && desc && (is_rndis(desc) || is_activesync(desc))) { 110 if (i == 0 && num_configs > 1 && desc &&
111 (is_rndis(desc) || is_activesync(desc))) {
110#if !defined(CONFIG_USB_NET_RNDIS_HOST) && !defined(CONFIG_USB_NET_RNDIS_HOST_MODULE) 112#if !defined(CONFIG_USB_NET_RNDIS_HOST) && !defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
111 continue; 113 continue;
112#else 114#else
diff --git a/drivers/usb/core/hcd-pci.c b/drivers/usb/core/hcd-pci.c
index 1cf2d1e79a5..c3f98543caa 100644
--- a/drivers/usb/core/hcd-pci.c
+++ b/drivers/usb/core/hcd-pci.c
@@ -66,10 +66,7 @@ static void companion_common(struct pci_dev *pdev, struct usb_hcd *hcd,
66 * vice versa. 66 * vice versa.
67 */ 67 */
68 companion = NULL; 68 companion = NULL;
69 for (;;) { 69 for_each_pci_dev(companion) {
70 companion = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, companion);
71 if (!companion)
72 break;
73 if (companion->bus != pdev->bus || 70 if (companion->bus != pdev->bus ||
74 PCI_SLOT(companion->devfn) != slot) 71 PCI_SLOT(companion->devfn) != slot)
75 continue; 72 continue;
@@ -250,6 +247,9 @@ int usb_hcd_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
250 if (retval != 0) 247 if (retval != 0)
251 goto err4; 248 goto err4;
252 set_hs_companion(dev, hcd); 249 set_hs_companion(dev, hcd);
250
251 if (pci_dev_run_wake(dev))
252 pm_runtime_put_noidle(&dev->dev);
253 return retval; 253 return retval;
254 254
255 err4: 255 err4:
@@ -292,6 +292,17 @@ void usb_hcd_pci_remove(struct pci_dev *dev)
292 if (!hcd) 292 if (!hcd)
293 return; 293 return;
294 294
295 if (pci_dev_run_wake(dev))
296 pm_runtime_get_noresume(&dev->dev);
297
298 /* Fake an interrupt request in order to give the driver a chance
299 * to test whether the controller hardware has been removed (e.g.,
300 * cardbus physical eject).
301 */
302 local_irq_disable();
303 usb_hcd_irq(0, hcd);
304 local_irq_enable();
305
295 usb_remove_hcd(hcd); 306 usb_remove_hcd(hcd);
296 if (hcd->driver->flags & HCD_MEMORY) { 307 if (hcd->driver->flags & HCD_MEMORY) {
297 iounmap(hcd->regs); 308 iounmap(hcd->regs);
@@ -317,12 +328,34 @@ void usb_hcd_pci_shutdown(struct pci_dev *dev)
317 if (!hcd) 328 if (!hcd)
318 return; 329 return;
319 330
320 if (hcd->driver->shutdown) 331 if (test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) &&
332 hcd->driver->shutdown)
321 hcd->driver->shutdown(hcd); 333 hcd->driver->shutdown(hcd);
322} 334}
323EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown); 335EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);
324 336
325#ifdef CONFIG_PM_SLEEP 337#ifdef CONFIG_PM_OPS
338
339#ifdef CONFIG_PPC_PMAC
340static void powermac_set_asic(struct pci_dev *pci_dev, int enable)
341{
342 /* Enanble or disable ASIC clocks for USB */
343 if (machine_is(powermac)) {
344 struct device_node *of_node;
345
346 of_node = pci_device_to_OF_node(pci_dev);
347 if (of_node)
348 pmac_call_feature(PMAC_FTR_USB_ENABLE,
349 of_node, 0, enable);
350 }
351}
352
353#else
354
355static inline void powermac_set_asic(struct pci_dev *pci_dev, int enable)
356{}
357
358#endif /* CONFIG_PPC_PMAC */
326 359
327static int check_root_hub_suspended(struct device *dev) 360static int check_root_hub_suspended(struct device *dev)
328{ 361{
@@ -337,7 +370,7 @@ static int check_root_hub_suspended(struct device *dev)
337 return 0; 370 return 0;
338} 371}
339 372
340static int hcd_pci_suspend(struct device *dev) 373static int suspend_common(struct device *dev, bool do_wakeup)
341{ 374{
342 struct pci_dev *pci_dev = to_pci_dev(dev); 375 struct pci_dev *pci_dev = to_pci_dev(dev);
343 struct usb_hcd *hcd = pci_get_drvdata(pci_dev); 376 struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
@@ -352,13 +385,21 @@ static int hcd_pci_suspend(struct device *dev)
352 if (retval) 385 if (retval)
353 return retval; 386 return retval;
354 387
355 /* We might already be suspended (runtime PM -- not yet written) */
356 if (pci_dev->current_state != PCI_D0)
357 return retval;
358
359 if (hcd->driver->pci_suspend) { 388 if (hcd->driver->pci_suspend) {
360 retval = hcd->driver->pci_suspend(hcd); 389 /* Optimization: Don't suspend if a root-hub wakeup is
390 * pending and it would cause the HCD to wake up anyway.
391 */
392 if (do_wakeup && HCD_WAKEUP_PENDING(hcd))
393 return -EBUSY;
394 retval = hcd->driver->pci_suspend(hcd, do_wakeup);
361 suspend_report_result(hcd->driver->pci_suspend, retval); 395 suspend_report_result(hcd->driver->pci_suspend, retval);
396
397 /* Check again in case wakeup raced with pci_suspend */
398 if (retval == 0 && do_wakeup && HCD_WAKEUP_PENDING(hcd)) {
399 if (hcd->driver->pci_resume)
400 hcd->driver->pci_resume(hcd, false);
401 retval = -EBUSY;
402 }
362 if (retval) 403 if (retval)
363 return retval; 404 return retval;
364 } 405 }
@@ -374,6 +415,48 @@ static int hcd_pci_suspend(struct device *dev)
374 return retval; 415 return retval;
375} 416}
376 417
418static int resume_common(struct device *dev, int event)
419{
420 struct pci_dev *pci_dev = to_pci_dev(dev);
421 struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
422 int retval;
423
424 if (hcd->state != HC_STATE_SUSPENDED) {
425 dev_dbg(dev, "can't resume, not suspended!\n");
426 return 0;
427 }
428
429 retval = pci_enable_device(pci_dev);
430 if (retval < 0) {
431 dev_err(dev, "can't re-enable after resume, %d!\n", retval);
432 return retval;
433 }
434
435 pci_set_master(pci_dev);
436
437 clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
438
439 if (hcd->driver->pci_resume) {
440 if (event != PM_EVENT_AUTO_RESUME)
441 wait_for_companions(pci_dev, hcd);
442
443 retval = hcd->driver->pci_resume(hcd,
444 event == PM_EVENT_RESTORE);
445 if (retval) {
446 dev_err(dev, "PCI post-resume error %d!\n", retval);
447 usb_hc_died(hcd);
448 }
449 }
450 return retval;
451}
452
453#ifdef CONFIG_PM_SLEEP
454
455static int hcd_pci_suspend(struct device *dev)
456{
457 return suspend_common(dev, device_may_wakeup(dev));
458}
459
377static int hcd_pci_suspend_noirq(struct device *dev) 460static int hcd_pci_suspend_noirq(struct device *dev)
378{ 461{
379 struct pci_dev *pci_dev = to_pci_dev(dev); 462 struct pci_dev *pci_dev = to_pci_dev(dev);
@@ -408,16 +491,7 @@ static int hcd_pci_suspend_noirq(struct device *dev)
408 return retval; 491 return retval;
409 } 492 }
410 493
411#ifdef CONFIG_PPC_PMAC 494 powermac_set_asic(pci_dev, 0);
412 /* Disable ASIC clocks for USB */
413 if (machine_is(powermac)) {
414 struct device_node *of_node;
415
416 of_node = pci_device_to_OF_node(pci_dev);
417 if (of_node)
418 pmac_call_feature(PMAC_FTR_USB_ENABLE, of_node, 0, 0);
419 }
420#endif
421 return retval; 495 return retval;
422} 496}
423 497
@@ -425,69 +499,63 @@ static int hcd_pci_resume_noirq(struct device *dev)
425{ 499{
426 struct pci_dev *pci_dev = to_pci_dev(dev); 500 struct pci_dev *pci_dev = to_pci_dev(dev);
427 501
428#ifdef CONFIG_PPC_PMAC 502 powermac_set_asic(pci_dev, 1);
429 /* Reenable ASIC clocks for USB */
430 if (machine_is(powermac)) {
431 struct device_node *of_node;
432
433 of_node = pci_device_to_OF_node(pci_dev);
434 if (of_node)
435 pmac_call_feature(PMAC_FTR_USB_ENABLE,
436 of_node, 0, 1);
437 }
438#endif
439 503
440 /* Go back to D0 and disable remote wakeup */ 504 /* Go back to D0 and disable remote wakeup */
441 pci_back_from_sleep(pci_dev); 505 pci_back_from_sleep(pci_dev);
442 return 0; 506 return 0;
443} 507}
444 508
445static int resume_common(struct device *dev, bool hibernated) 509static int hcd_pci_resume(struct device *dev)
446{ 510{
447 struct pci_dev *pci_dev = to_pci_dev(dev); 511 return resume_common(dev, PM_EVENT_RESUME);
448 struct usb_hcd *hcd = pci_get_drvdata(pci_dev); 512}
449 int retval;
450 513
451 if (hcd->state != HC_STATE_SUSPENDED) { 514static int hcd_pci_restore(struct device *dev)
452 dev_dbg(dev, "can't resume, not suspended!\n"); 515{
453 return 0; 516 return resume_common(dev, PM_EVENT_RESTORE);
454 } 517}
455 518
456 retval = pci_enable_device(pci_dev); 519#else
457 if (retval < 0) {
458 dev_err(dev, "can't re-enable after resume, %d!\n", retval);
459 return retval;
460 }
461 520
462 pci_set_master(pci_dev); 521#define hcd_pci_suspend NULL
522#define hcd_pci_suspend_noirq NULL
523#define hcd_pci_resume_noirq NULL
524#define hcd_pci_resume NULL
525#define hcd_pci_restore NULL
463 526
464 clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); 527#endif /* CONFIG_PM_SLEEP */
465 528
466 if (hcd->driver->pci_resume) { 529#ifdef CONFIG_PM_RUNTIME
467 /* This call should be made only during system resume,
468 * not during runtime resume.
469 */
470 wait_for_companions(pci_dev, hcd);
471 530
472 retval = hcd->driver->pci_resume(hcd, hibernated); 531static int hcd_pci_runtime_suspend(struct device *dev)
473 if (retval) { 532{
474 dev_err(dev, "PCI post-resume error %d!\n", retval); 533 int retval;
475 usb_hc_died(hcd); 534
476 } 535 retval = suspend_common(dev, true);
477 } 536 if (retval == 0)
537 powermac_set_asic(to_pci_dev(dev), 0);
538 dev_dbg(dev, "hcd_pci_runtime_suspend: %d\n", retval);
478 return retval; 539 return retval;
479} 540}
480 541
481static int hcd_pci_resume(struct device *dev) 542static int hcd_pci_runtime_resume(struct device *dev)
482{ 543{
483 return resume_common(dev, false); 544 int retval;
484}
485 545
486static int hcd_pci_restore(struct device *dev) 546 powermac_set_asic(to_pci_dev(dev), 1);
487{ 547 retval = resume_common(dev, PM_EVENT_AUTO_RESUME);
488 return resume_common(dev, true); 548 dev_dbg(dev, "hcd_pci_runtime_resume: %d\n", retval);
549 return retval;
489} 550}
490 551
552#else
553
554#define hcd_pci_runtime_suspend NULL
555#define hcd_pci_runtime_resume NULL
556
557#endif /* CONFIG_PM_RUNTIME */
558
491const struct dev_pm_ops usb_hcd_pci_pm_ops = { 559const struct dev_pm_ops usb_hcd_pci_pm_ops = {
492 .suspend = hcd_pci_suspend, 560 .suspend = hcd_pci_suspend,
493 .suspend_noirq = hcd_pci_suspend_noirq, 561 .suspend_noirq = hcd_pci_suspend_noirq,
@@ -501,7 +569,9 @@ const struct dev_pm_ops usb_hcd_pci_pm_ops = {
501 .poweroff_noirq = hcd_pci_suspend_noirq, 569 .poweroff_noirq = hcd_pci_suspend_noirq,
502 .restore_noirq = hcd_pci_resume_noirq, 570 .restore_noirq = hcd_pci_resume_noirq,
503 .restore = hcd_pci_restore, 571 .restore = hcd_pci_restore,
572 .runtime_suspend = hcd_pci_runtime_suspend,
573 .runtime_resume = hcd_pci_runtime_resume,
504}; 574};
505EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops); 575EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops);
506 576
507#endif /* CONFIG_PM_SLEEP */ 577#endif /* CONFIG_PM_OPS */
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 12742f152f4..5cca00a6d09 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -667,7 +667,7 @@ void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
667 unsigned long flags; 667 unsigned long flags;
668 char buffer[6]; /* Any root hubs with > 31 ports? */ 668 char buffer[6]; /* Any root hubs with > 31 ports? */
669 669
670 if (unlikely(!hcd->rh_registered)) 670 if (unlikely(!hcd->rh_pollable))
671 return; 671 return;
672 if (!hcd->uses_new_polling && !hcd->status_urb) 672 if (!hcd->uses_new_polling && !hcd->status_urb)
673 return; 673 return;
@@ -679,7 +679,7 @@ void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
679 spin_lock_irqsave(&hcd_root_hub_lock, flags); 679 spin_lock_irqsave(&hcd_root_hub_lock, flags);
680 urb = hcd->status_urb; 680 urb = hcd->status_urb;
681 if (urb) { 681 if (urb) {
682 hcd->poll_pending = 0; 682 clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
683 hcd->status_urb = NULL; 683 hcd->status_urb = NULL;
684 urb->actual_length = length; 684 urb->actual_length = length;
685 memcpy(urb->transfer_buffer, buffer, length); 685 memcpy(urb->transfer_buffer, buffer, length);
@@ -690,7 +690,7 @@ void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
690 spin_lock(&hcd_root_hub_lock); 690 spin_lock(&hcd_root_hub_lock);
691 } else { 691 } else {
692 length = 0; 692 length = 0;
693 hcd->poll_pending = 1; 693 set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
694 } 694 }
695 spin_unlock_irqrestore(&hcd_root_hub_lock, flags); 695 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
696 } 696 }
@@ -699,7 +699,7 @@ void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
699 * exceed that limit if HZ is 100. The math is more clunky than 699 * exceed that limit if HZ is 100. The math is more clunky than
700 * maybe expected, this is to make sure that all timers for USB devices 700 * maybe expected, this is to make sure that all timers for USB devices
701 * fire at the same time to give the CPU a break inbetween */ 701 * fire at the same time to give the CPU a break inbetween */
702 if (hcd->uses_new_polling ? hcd->poll_rh : 702 if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) :
703 (length == 0 && hcd->status_urb != NULL)) 703 (length == 0 && hcd->status_urb != NULL))
704 mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); 704 mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
705} 705}
@@ -736,7 +736,7 @@ static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
736 mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); 736 mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
737 737
738 /* If a status change has already occurred, report it ASAP */ 738 /* If a status change has already occurred, report it ASAP */
739 else if (hcd->poll_pending) 739 else if (HCD_POLL_PENDING(hcd))
740 mod_timer(&hcd->rh_timer, jiffies); 740 mod_timer(&hcd->rh_timer, jiffies);
741 retval = 0; 741 retval = 0;
742 done: 742 done:
@@ -1150,8 +1150,7 @@ int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
1150 * finish unlinking the initial failed usb_set_address() 1150 * finish unlinking the initial failed usb_set_address()
1151 * or device descriptor fetch. 1151 * or device descriptor fetch.
1152 */ 1152 */
1153 if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) && 1153 if (!HCD_SAW_IRQ(hcd) && !is_root_hub(urb->dev)) {
1154 !is_root_hub(urb->dev)) {
1155 dev_warn(hcd->self.controller, "Unlink after no-IRQ? " 1154 dev_warn(hcd->self.controller, "Unlink after no-IRQ? "
1156 "Controller is probably using the wrong IRQ.\n"); 1155 "Controller is probably using the wrong IRQ.\n");
1157 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); 1156 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
@@ -1219,6 +1218,11 @@ static int hcd_alloc_coherent(struct usb_bus *bus,
1219{ 1218{
1220 unsigned char *vaddr; 1219 unsigned char *vaddr;
1221 1220
1221 if (*vaddr_handle == NULL) {
1222 WARN_ON_ONCE(1);
1223 return -EFAULT;
1224 }
1225
1222 vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr), 1226 vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
1223 mem_flags, dma_handle); 1227 mem_flags, dma_handle);
1224 if (!vaddr) 1228 if (!vaddr)
@@ -1941,6 +1945,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
1941 1945
1942 dev_dbg(&rhdev->dev, "usb %s%s\n", 1946 dev_dbg(&rhdev->dev, "usb %s%s\n",
1943 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "resume"); 1947 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "resume");
1948 clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
1944 if (!hcd->driver->bus_resume) 1949 if (!hcd->driver->bus_resume)
1945 return -ENOENT; 1950 return -ENOENT;
1946 if (hcd->state == HC_STATE_RUNNING) 1951 if (hcd->state == HC_STATE_RUNNING)
@@ -1994,8 +1999,10 @@ void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
1994 unsigned long flags; 1999 unsigned long flags;
1995 2000
1996 spin_lock_irqsave (&hcd_root_hub_lock, flags); 2001 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1997 if (hcd->rh_registered) 2002 if (hcd->rh_registered) {
2003 set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
1998 queue_work(pm_wq, &hcd->wakeup_work); 2004 queue_work(pm_wq, &hcd->wakeup_work);
2005 }
1999 spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 2006 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
2000} 2007}
2001EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); 2008EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
@@ -2063,8 +2070,7 @@ irqreturn_t usb_hcd_irq (int irq, void *__hcd)
2063 */ 2070 */
2064 local_irq_save(flags); 2071 local_irq_save(flags);
2065 2072
2066 if (unlikely(hcd->state == HC_STATE_HALT || 2073 if (unlikely(hcd->state == HC_STATE_HALT || !HCD_HW_ACCESSIBLE(hcd))) {
2067 !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
2068 rc = IRQ_NONE; 2074 rc = IRQ_NONE;
2069 } else if (hcd->driver->irq(hcd) == IRQ_NONE) { 2075 } else if (hcd->driver->irq(hcd) == IRQ_NONE) {
2070 rc = IRQ_NONE; 2076 rc = IRQ_NONE;
@@ -2079,6 +2085,7 @@ irqreturn_t usb_hcd_irq (int irq, void *__hcd)
2079 local_irq_restore(flags); 2085 local_irq_restore(flags);
2080 return rc; 2086 return rc;
2081} 2087}
2088EXPORT_SYMBOL_GPL(usb_hcd_irq);
2082 2089
2083/*-------------------------------------------------------------------------*/ 2090/*-------------------------------------------------------------------------*/
2084 2091
@@ -2098,7 +2105,7 @@ void usb_hc_died (struct usb_hcd *hcd)
2098 2105
2099 spin_lock_irqsave (&hcd_root_hub_lock, flags); 2106 spin_lock_irqsave (&hcd_root_hub_lock, flags);
2100 if (hcd->rh_registered) { 2107 if (hcd->rh_registered) {
2101 hcd->poll_rh = 0; 2108 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2102 2109
2103 /* make khubd clean up old urbs and devices */ 2110 /* make khubd clean up old urbs and devices */
2104 usb_set_device_state (hcd->self.root_hub, 2111 usb_set_device_state (hcd->self.root_hub,
@@ -2217,6 +2224,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
2217 retval = -ENOMEM; 2224 retval = -ENOMEM;
2218 goto err_allocate_root_hub; 2225 goto err_allocate_root_hub;
2219 } 2226 }
2227 hcd->self.root_hub = rhdev;
2220 2228
2221 switch (hcd->driver->flags & HCD_MASK) { 2229 switch (hcd->driver->flags & HCD_MASK) {
2222 case HCD_USB11: 2230 case HCD_USB11:
@@ -2229,9 +2237,8 @@ int usb_add_hcd(struct usb_hcd *hcd,
2229 rhdev->speed = USB_SPEED_SUPER; 2237 rhdev->speed = USB_SPEED_SUPER;
2230 break; 2238 break;
2231 default: 2239 default:
2232 goto err_allocate_root_hub; 2240 goto err_set_rh_speed;
2233 } 2241 }
2234 hcd->self.root_hub = rhdev;
2235 2242
2236 /* wakeup flag init defaults to "everything works" for root hubs, 2243 /* wakeup flag init defaults to "everything works" for root hubs,
2237 * but drivers can override it in reset() if needed, along with 2244 * but drivers can override it in reset() if needed, along with
@@ -2246,6 +2253,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
2246 dev_err(hcd->self.controller, "can't setup\n"); 2253 dev_err(hcd->self.controller, "can't setup\n");
2247 goto err_hcd_driver_setup; 2254 goto err_hcd_driver_setup;
2248 } 2255 }
2256 hcd->rh_pollable = 1;
2249 2257
2250 /* NOTE: root hub and controller capabilities may not be the same */ 2258 /* NOTE: root hub and controller capabilities may not be the same */
2251 if (device_can_wakeup(hcd->self.controller) 2259 if (device_can_wakeup(hcd->self.controller)
@@ -2300,23 +2308,38 @@ int usb_add_hcd(struct usb_hcd *hcd,
2300 retval); 2308 retval);
2301 goto error_create_attr_group; 2309 goto error_create_attr_group;
2302 } 2310 }
2303 if (hcd->uses_new_polling && hcd->poll_rh) 2311 if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
2304 usb_hcd_poll_rh_status(hcd); 2312 usb_hcd_poll_rh_status(hcd);
2305 return retval; 2313 return retval;
2306 2314
2307error_create_attr_group: 2315error_create_attr_group:
2316 if (HC_IS_RUNNING(hcd->state))
2317 hcd->state = HC_STATE_QUIESCING;
2318 spin_lock_irq(&hcd_root_hub_lock);
2319 hcd->rh_registered = 0;
2320 spin_unlock_irq(&hcd_root_hub_lock);
2321
2322#ifdef CONFIG_USB_SUSPEND
2323 cancel_work_sync(&hcd->wakeup_work);
2324#endif
2308 mutex_lock(&usb_bus_list_lock); 2325 mutex_lock(&usb_bus_list_lock);
2309 usb_disconnect(&hcd->self.root_hub); 2326 usb_disconnect(&rhdev); /* Sets rhdev to NULL */
2310 mutex_unlock(&usb_bus_list_lock); 2327 mutex_unlock(&usb_bus_list_lock);
2311err_register_root_hub: 2328err_register_root_hub:
2329 hcd->rh_pollable = 0;
2330 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2331 del_timer_sync(&hcd->rh_timer);
2312 hcd->driver->stop(hcd); 2332 hcd->driver->stop(hcd);
2333 hcd->state = HC_STATE_HALT;
2334 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2335 del_timer_sync(&hcd->rh_timer);
2313err_hcd_driver_start: 2336err_hcd_driver_start:
2314 if (hcd->irq >= 0) 2337 if (hcd->irq >= 0)
2315 free_irq(irqnum, hcd); 2338 free_irq(irqnum, hcd);
2316err_request_irq: 2339err_request_irq:
2317err_hcd_driver_setup: 2340err_hcd_driver_setup:
2318 hcd->self.root_hub = NULL; 2341err_set_rh_speed:
2319 usb_put_dev(rhdev); 2342 usb_put_dev(hcd->self.root_hub);
2320err_allocate_root_hub: 2343err_allocate_root_hub:
2321 usb_deregister_bus(&hcd->self); 2344 usb_deregister_bus(&hcd->self);
2322err_register_bus: 2345err_register_bus:
@@ -2335,8 +2358,13 @@ EXPORT_SYMBOL_GPL(usb_add_hcd);
2335 */ 2358 */
2336void usb_remove_hcd(struct usb_hcd *hcd) 2359void usb_remove_hcd(struct usb_hcd *hcd)
2337{ 2360{
2361 struct usb_device *rhdev = hcd->self.root_hub;
2362
2338 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state); 2363 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
2339 2364
2365 usb_get_dev(rhdev);
2366 sysfs_remove_group(&rhdev->dev.kobj, &usb_bus_attr_group);
2367
2340 if (HC_IS_RUNNING (hcd->state)) 2368 if (HC_IS_RUNNING (hcd->state))
2341 hcd->state = HC_STATE_QUIESCING; 2369 hcd->state = HC_STATE_QUIESCING;
2342 2370
@@ -2349,19 +2377,30 @@ void usb_remove_hcd(struct usb_hcd *hcd)
2349 cancel_work_sync(&hcd->wakeup_work); 2377 cancel_work_sync(&hcd->wakeup_work);
2350#endif 2378#endif
2351 2379
2352 sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group);
2353 mutex_lock(&usb_bus_list_lock); 2380 mutex_lock(&usb_bus_list_lock);
2354 usb_disconnect(&hcd->self.root_hub); 2381 usb_disconnect(&rhdev); /* Sets rhdev to NULL */
2355 mutex_unlock(&usb_bus_list_lock); 2382 mutex_unlock(&usb_bus_list_lock);
2356 2383
2384 /* Prevent any more root-hub status calls from the timer.
2385 * The HCD might still restart the timer (if a port status change
2386 * interrupt occurs), but usb_hcd_poll_rh_status() won't invoke
2387 * the hub_status_data() callback.
2388 */
2389 hcd->rh_pollable = 0;
2390 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2391 del_timer_sync(&hcd->rh_timer);
2392
2357 hcd->driver->stop(hcd); 2393 hcd->driver->stop(hcd);
2358 hcd->state = HC_STATE_HALT; 2394 hcd->state = HC_STATE_HALT;
2359 2395
2360 hcd->poll_rh = 0; 2396 /* In case the HCD restarted the timer, stop it again. */
2397 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2361 del_timer_sync(&hcd->rh_timer); 2398 del_timer_sync(&hcd->rh_timer);
2362 2399
2363 if (hcd->irq >= 0) 2400 if (hcd->irq >= 0)
2364 free_irq(hcd->irq, hcd); 2401 free_irq(hcd->irq, hcd);
2402
2403 usb_put_dev(hcd->self.root_hub);
2365 usb_deregister_bus(&hcd->self); 2404 usb_deregister_bus(&hcd->self);
2366 hcd_buffer_destroy(hcd); 2405 hcd_buffer_destroy(hcd);
2367} 2406}
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 70cccc75a36..84c1897188d 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -20,6 +20,7 @@
20#include <linux/usb.h> 20#include <linux/usb.h>
21#include <linux/usbdevice_fs.h> 21#include <linux/usbdevice_fs.h>
22#include <linux/usb/hcd.h> 22#include <linux/usb/hcd.h>
23#include <linux/usb/quirks.h>
23#include <linux/kthread.h> 24#include <linux/kthread.h>
24#include <linux/mutex.h> 25#include <linux/mutex.h>
25#include <linux/freezer.h> 26#include <linux/freezer.h>
@@ -1294,6 +1295,7 @@ descriptor_error:
1294 return -ENODEV; 1295 return -ENODEV;
1295} 1296}
1296 1297
1298/* No BKL needed */
1297static int 1299static int
1298hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) 1300hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1299{ 1301{
@@ -1801,7 +1803,6 @@ int usb_new_device(struct usb_device *udev)
1801 pm_runtime_set_active(&udev->dev); 1803 pm_runtime_set_active(&udev->dev);
1802 pm_runtime_enable(&udev->dev); 1804 pm_runtime_enable(&udev->dev);
1803 1805
1804 usb_detect_quirks(udev);
1805 err = usb_enumerate_device(udev); /* Read descriptors */ 1806 err = usb_enumerate_device(udev); /* Read descriptors */
1806 if (err < 0) 1807 if (err < 0)
1807 goto fail; 1808 goto fail;
@@ -2880,7 +2881,9 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2880 } 2881 }
2881 2882
2882 retval = 0; 2883 retval = 0;
2883 2884 /* notify HCD that we have a device connected and addressed */
2885 if (hcd->driver->update_device)
2886 hcd->driver->update_device(hcd, udev);
2884fail: 2887fail:
2885 if (retval) { 2888 if (retval) {
2886 hub_port_disable(hub, port1, 0); 2889 hub_port_disable(hub, port1, 0);
@@ -3111,6 +3114,10 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
3111 if (status < 0) 3114 if (status < 0)
3112 goto loop; 3115 goto loop;
3113 3116
3117 usb_detect_quirks(udev);
3118 if (udev->quirks & USB_QUIRK_DELAY_INIT)
3119 msleep(1000);
3120
3114 /* consecutive bus-powered hubs aren't reliable; they can 3121 /* consecutive bus-powered hubs aren't reliable; they can
3115 * violate the voltage drop budget. if the new child has 3122 * violate the voltage drop budget. if the new child has
3116 * a "powered" LED, users should notice we didn't enable it 3123 * a "powered" LED, users should notice we didn't enable it
@@ -3463,7 +3470,7 @@ static struct usb_driver hub_driver = {
3463 .reset_resume = hub_reset_resume, 3470 .reset_resume = hub_reset_resume,
3464 .pre_reset = hub_pre_reset, 3471 .pre_reset = hub_pre_reset,
3465 .post_reset = hub_post_reset, 3472 .post_reset = hub_post_reset,
3466 .ioctl = hub_ioctl, 3473 .unlocked_ioctl = hub_ioctl,
3467 .id_table = hub_id_table, 3474 .id_table = hub_id_table,
3468 .supports_autosuspend = 1, 3475 .supports_autosuspend = 1,
3469}; 3476};
diff --git a/drivers/usb/core/inode.c b/drivers/usb/core/inode.c
index 1a27618b67d..095fa536669 100644
--- a/drivers/usb/core/inode.c
+++ b/drivers/usb/core/inode.c
@@ -265,13 +265,9 @@ static int remount(struct super_block *sb, int *flags, char *data)
265 return -EINVAL; 265 return -EINVAL;
266 } 266 }
267 267
268 lock_kernel();
269
270 if (usbfs_mount && usbfs_mount->mnt_sb) 268 if (usbfs_mount && usbfs_mount->mnt_sb)
271 update_sb(usbfs_mount->mnt_sb); 269 update_sb(usbfs_mount->mnt_sb);
272 270
273 unlock_kernel();
274
275 return 0; 271 return 0;
276} 272}
277 273
diff --git a/drivers/usb/core/quirks.c b/drivers/usb/core/quirks.c
index db99c084df9..25719da45e3 100644
--- a/drivers/usb/core/quirks.c
+++ b/drivers/usb/core/quirks.c
@@ -38,6 +38,9 @@ static const struct usb_device_id usb_quirk_list[] = {
38 /* Creative SB Audigy 2 NX */ 38 /* Creative SB Audigy 2 NX */
39 { USB_DEVICE(0x041e, 0x3020), .driver_info = USB_QUIRK_RESET_RESUME }, 39 { USB_DEVICE(0x041e, 0x3020), .driver_info = USB_QUIRK_RESET_RESUME },
40 40
41 /* Logitech Harmony 700-series */
42 { USB_DEVICE(0x046d, 0xc122), .driver_info = USB_QUIRK_DELAY_INIT },
43
41 /* Philips PSC805 audio device */ 44 /* Philips PSC805 audio device */
42 { USB_DEVICE(0x0471, 0x0155), .driver_info = USB_QUIRK_RESET_RESUME }, 45 { USB_DEVICE(0x0471, 0x0155), .driver_info = USB_QUIRK_RESET_RESUME },
43 46
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index 7c0555548ac..419e6b34e2f 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -137,6 +137,16 @@ void usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor)
137} 137}
138EXPORT_SYMBOL_GPL(usb_anchor_urb); 138EXPORT_SYMBOL_GPL(usb_anchor_urb);
139 139
140/* Callers must hold anchor->lock */
141static void __usb_unanchor_urb(struct urb *urb, struct usb_anchor *anchor)
142{
143 urb->anchor = NULL;
144 list_del(&urb->anchor_list);
145 usb_put_urb(urb);
146 if (list_empty(&anchor->urb_list))
147 wake_up(&anchor->wait);
148}
149
140/** 150/**
141 * usb_unanchor_urb - unanchors an URB 151 * usb_unanchor_urb - unanchors an URB
142 * @urb: pointer to the urb to anchor 152 * @urb: pointer to the urb to anchor
@@ -156,17 +166,14 @@ void usb_unanchor_urb(struct urb *urb)
156 return; 166 return;
157 167
158 spin_lock_irqsave(&anchor->lock, flags); 168 spin_lock_irqsave(&anchor->lock, flags);
159 if (unlikely(anchor != urb->anchor)) { 169 /*
160 /* we've lost the race to another thread */ 170 * At this point, we could be competing with another thread which
161 spin_unlock_irqrestore(&anchor->lock, flags); 171 * has the same intention. To protect the urb from being unanchored
162 return; 172 * twice, only the winner of the race gets the job.
163 } 173 */
164 urb->anchor = NULL; 174 if (likely(anchor == urb->anchor))
165 list_del(&urb->anchor_list); 175 __usb_unanchor_urb(urb, anchor);
166 spin_unlock_irqrestore(&anchor->lock, flags); 176 spin_unlock_irqrestore(&anchor->lock, flags);
167 usb_put_urb(urb);
168 if (list_empty(&anchor->urb_list))
169 wake_up(&anchor->wait);
170} 177}
171EXPORT_SYMBOL_GPL(usb_unanchor_urb); 178EXPORT_SYMBOL_GPL(usb_unanchor_urb);
172 179
@@ -749,20 +756,11 @@ EXPORT_SYMBOL_GPL(usb_unpoison_anchored_urbs);
749void usb_unlink_anchored_urbs(struct usb_anchor *anchor) 756void usb_unlink_anchored_urbs(struct usb_anchor *anchor)
750{ 757{
751 struct urb *victim; 758 struct urb *victim;
752 unsigned long flags;
753 759
754 spin_lock_irqsave(&anchor->lock, flags); 760 while ((victim = usb_get_from_anchor(anchor)) != NULL) {
755 while (!list_empty(&anchor->urb_list)) {
756 victim = list_entry(anchor->urb_list.prev, struct urb,
757 anchor_list);
758 usb_get_urb(victim);
759 spin_unlock_irqrestore(&anchor->lock, flags);
760 /* this will unanchor the URB */
761 usb_unlink_urb(victim); 761 usb_unlink_urb(victim);
762 usb_put_urb(victim); 762 usb_put_urb(victim);
763 spin_lock_irqsave(&anchor->lock, flags);
764 } 763 }
765 spin_unlock_irqrestore(&anchor->lock, flags);
766} 764}
767EXPORT_SYMBOL_GPL(usb_unlink_anchored_urbs); 765EXPORT_SYMBOL_GPL(usb_unlink_anchored_urbs);
768 766
@@ -799,12 +797,11 @@ struct urb *usb_get_from_anchor(struct usb_anchor *anchor)
799 victim = list_entry(anchor->urb_list.next, struct urb, 797 victim = list_entry(anchor->urb_list.next, struct urb,
800 anchor_list); 798 anchor_list);
801 usb_get_urb(victim); 799 usb_get_urb(victim);
802 spin_unlock_irqrestore(&anchor->lock, flags); 800 __usb_unanchor_urb(victim, anchor);
803 usb_unanchor_urb(victim);
804 } else { 801 } else {
805 spin_unlock_irqrestore(&anchor->lock, flags);
806 victim = NULL; 802 victim = NULL;
807 } 803 }
804 spin_unlock_irqrestore(&anchor->lock, flags);
808 805
809 return victim; 806 return victim;
810} 807}
@@ -826,12 +823,7 @@ void usb_scuttle_anchored_urbs(struct usb_anchor *anchor)
826 while (!list_empty(&anchor->urb_list)) { 823 while (!list_empty(&anchor->urb_list)) {
827 victim = list_entry(anchor->urb_list.prev, struct urb, 824 victim = list_entry(anchor->urb_list.prev, struct urb,
828 anchor_list); 825 anchor_list);
829 usb_get_urb(victim); 826 __usb_unanchor_urb(victim, anchor);
830 spin_unlock_irqrestore(&anchor->lock, flags);
831 /* this may free the URB */
832 usb_unanchor_urb(victim);
833 usb_put_urb(victim);
834 spin_lock_irqsave(&anchor->lock, flags);
835 } 827 }
836 spin_unlock_irqrestore(&anchor->lock, flags); 828 spin_unlock_irqrestore(&anchor->lock, flags);
837} 829}
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 5ae14f6c1e7..fdd4130fbb7 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -317,10 +317,6 @@ static const struct dev_pm_ops usb_device_pm_ops = {
317 .restore = usb_dev_restore, 317 .restore = usb_dev_restore,
318}; 318};
319 319
320#else
321
322#define usb_device_pm_ops (*(struct dev_pm_ops *) NULL)
323
324#endif /* CONFIG_PM */ 320#endif /* CONFIG_PM */
325 321
326 322
@@ -338,7 +334,9 @@ struct device_type usb_device_type = {
338 .release = usb_release_dev, 334 .release = usb_release_dev,
339 .uevent = usb_dev_uevent, 335 .uevent = usb_dev_uevent,
340 .devnode = usb_devnode, 336 .devnode = usb_devnode,
337#ifdef CONFIG_PM
341 .pm = &usb_device_pm_ops, 338 .pm = &usb_device_pm_ops,
339#endif
342}; 340};
343 341
344 342
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 591ae9fde19..cd27f9bde2c 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -714,6 +714,7 @@ config USB_GADGETFS
714config USB_FUNCTIONFS 714config USB_FUNCTIONFS
715 tristate "Function Filesystem (EXPERIMENTAL)" 715 tristate "Function Filesystem (EXPERIMENTAL)"
716 depends on EXPERIMENTAL 716 depends on EXPERIMENTAL
717 select USB_FUNCTIONFS_GENERIC if !(USB_FUNCTIONFS_ETH || USB_FUNCTIONFS_RNDIS)
717 help 718 help
718 The Function Filesystem (FunctioFS) lets one create USB 719 The Function Filesystem (FunctioFS) lets one create USB
719 composite functions in user space in the same way as GadgetFS 720 composite functions in user space in the same way as GadgetFS
@@ -722,31 +723,31 @@ config USB_FUNCTIONFS
722 implemented in kernel space (for instance Ethernet, serial or 723 implemented in kernel space (for instance Ethernet, serial or
723 mass storage) and other are implemented in user space. 724 mass storage) and other are implemented in user space.
724 725
726 If you say "y" or "m" here you will be able what kind of
727 configurations the gadget will provide.
728
725 Say "y" to link the driver statically, or "m" to build 729 Say "y" to link the driver statically, or "m" to build
726 a dynamically linked module called "g_ffs". 730 a dynamically linked module called "g_ffs".
727 731
728config USB_FUNCTIONFS_ETH 732config USB_FUNCTIONFS_ETH
729 bool "Include CDC ECM (Ethernet) function" 733 bool "Include configuration with CDC ECM (Ethernet)"
730 depends on USB_FUNCTIONFS && NET 734 depends on USB_FUNCTIONFS && NET
731 help 735 help
732 Include an CDC ECM (Ethernet) funcion in the CDC ECM (Funcion) 736 Include a configuration with CDC ECM funcion (Ethernet) and the
733 Filesystem. If you also say "y" to the RNDIS query below the 737 Funcion Filesystem.
734 gadget will have two configurations.
735 738
736config USB_FUNCTIONFS_RNDIS 739config USB_FUNCTIONFS_RNDIS
737 bool "Include RNDIS (Ethernet) function" 740 bool "Include configuration with RNDIS (Ethernet)"
738 depends on USB_FUNCTIONFS && NET 741 depends on USB_FUNCTIONFS && NET
739 help 742 help
740 Include an RNDIS (Ethernet) funcion in the Funcion Filesystem. 743 Include a configuration with RNDIS funcion (Ethernet) and the Filesystem.
741 If you also say "y" to the CDC ECM query above the gadget will
742 have two configurations.
743 744
744config USB_FUNCTIONFS_GENERIC 745config USB_FUNCTIONFS_GENERIC
745 bool "Include 'pure' configuration" 746 bool "Include 'pure' configuration"
746 depends on USB_FUNCTIONFS && (USB_FUNCTIONFS_ETH || USB_FUNCTIONFS_RNDIS) 747 depends on USB_FUNCTIONFS
747 help 748 help
748 Include a configuration with FunctionFS and no Ethernet 749 Include a configuration with the Function Filesystem alone with
749 configuration. 750 no Ethernet interface.
750 751
751config USB_FILE_STORAGE 752config USB_FILE_STORAGE
752 tristate "File-backed Storage Gadget" 753 tristate "File-backed Storage Gadget"
@@ -863,6 +864,7 @@ config USB_G_NOKIA
863config USB_G_MULTI 864config USB_G_MULTI
864 tristate "Multifunction Composite Gadget (EXPERIMENTAL)" 865 tristate "Multifunction Composite Gadget (EXPERIMENTAL)"
865 depends on BLOCK && NET 866 depends on BLOCK && NET
867 select USB_G_MULTI_CDC if !USB_G_MULTI_RNDIS
866 help 868 help
867 The Multifunction Composite Gadget provides Ethernet (RNDIS 869 The Multifunction Composite Gadget provides Ethernet (RNDIS
868 and/or CDC Ethernet), mass storage and ACM serial link 870 and/or CDC Ethernet), mass storage and ACM serial link
@@ -913,6 +915,34 @@ config USB_G_HID
913 Say "y" to link the driver statically, or "m" to build a 915 Say "y" to link the driver statically, or "m" to build a
914 dynamically linked module called "g_hid". 916 dynamically linked module called "g_hid".
915 917
918config USB_G_DBGP
919 tristate "EHCI Debug Device Gadget"
920 help
921 This gadget emulates an EHCI Debug device. This is useful when you want
922 to interact with an EHCI Debug Port.
923
924 Say "y" to link the driver statically, or "m" to build a
925 dynamically linked module called "g_dbgp".
926
927if USB_G_DBGP
928choice
929 prompt "EHCI Debug Device mode"
930 default USB_G_DBGP_SERIAL
931
932config USB_G_DBGP_PRINTK
933 depends on USB_G_DBGP
934 bool "printk"
935 help
936 Directly printk() received data. No interaction.
937
938config USB_G_DBGP_SERIAL
939 depends on USB_G_DBGP
940 bool "serial"
941 help
942 Userland can interact using /dev/ttyGSxxx.
943endchoice
944endif
945
916# put drivers that need isochronous transfer support (for audio 946# put drivers that need isochronous transfer support (for audio
917# or video class gadget drivers), or specific hardware, here. 947# or video class gadget drivers), or specific hardware, here.
918config USB_G_WEBCAM 948config USB_G_WEBCAM
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 9bcde110feb..27283df37d0 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -44,6 +44,7 @@ g_printer-objs := printer.o
44g_cdc-objs := cdc2.o 44g_cdc-objs := cdc2.o
45g_multi-objs := multi.o 45g_multi-objs := multi.o
46g_hid-objs := hid.o 46g_hid-objs := hid.o
47g_dbgp-objs := dbgp.o
47g_nokia-objs := nokia.o 48g_nokia-objs := nokia.o
48g_webcam-objs := webcam.o 49g_webcam-objs := webcam.o
49 50
@@ -52,7 +53,6 @@ obj-$(CONFIG_USB_AUDIO) += g_audio.o
52obj-$(CONFIG_USB_ETH) += g_ether.o 53obj-$(CONFIG_USB_ETH) += g_ether.o
53obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o 54obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o
54obj-$(CONFIG_USB_FUNCTIONFS) += g_ffs.o 55obj-$(CONFIG_USB_FUNCTIONFS) += g_ffs.o
55obj-$(CONFIG_USB_ETH_FUNCTIONFS) += g_eth_ffs.o
56obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o 56obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o
57obj-$(CONFIG_USB_MASS_STORAGE) += g_mass_storage.o 57obj-$(CONFIG_USB_MASS_STORAGE) += g_mass_storage.o
58obj-$(CONFIG_USB_G_SERIAL) += g_serial.o 58obj-$(CONFIG_USB_G_SERIAL) += g_serial.o
@@ -60,6 +60,7 @@ obj-$(CONFIG_USB_G_PRINTER) += g_printer.o
60obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o 60obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o
61obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o 61obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o
62obj-$(CONFIG_USB_G_HID) += g_hid.o 62obj-$(CONFIG_USB_G_HID) += g_hid.o
63obj-$(CONFIG_USB_G_DBGP) += g_dbgp.o
63obj-$(CONFIG_USB_G_MULTI) += g_multi.o 64obj-$(CONFIG_USB_G_MULTI) += g_multi.o
64obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o 65obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o
65obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o 66obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o
diff --git a/drivers/usb/gadget/audio.c b/drivers/usb/gadget/audio.c
index a62af7b5909..b744ccd0f34 100644
--- a/drivers/usb/gadget/audio.c
+++ b/drivers/usb/gadget/audio.c
@@ -89,7 +89,7 @@ static const struct usb_descriptor_header *otg_desc[] = {
89 89
90/*-------------------------------------------------------------------------*/ 90/*-------------------------------------------------------------------------*/
91 91
92static int __init audio_do_config(struct usb_configuration *c) 92static int __ref audio_do_config(struct usb_configuration *c)
93{ 93{
94 /* FIXME alloc iConfiguration string, set it in c->strings */ 94 /* FIXME alloc iConfiguration string, set it in c->strings */
95 95
@@ -113,7 +113,7 @@ static struct usb_configuration audio_config_driver = {
113 113
114/*-------------------------------------------------------------------------*/ 114/*-------------------------------------------------------------------------*/
115 115
116static int __init audio_bind(struct usb_composite_dev *cdev) 116static int __ref audio_bind(struct usb_composite_dev *cdev)
117{ 117{
118 int gcnum; 118 int gcnum;
119 int status; 119 int status;
diff --git a/drivers/usb/gadget/cdc2.c b/drivers/usb/gadget/cdc2.c
index 928137d3dbd..1f5ba2fd4c1 100644
--- a/drivers/usb/gadget/cdc2.c
+++ b/drivers/usb/gadget/cdc2.c
@@ -129,7 +129,7 @@ static u8 hostaddr[ETH_ALEN];
129/* 129/*
130 * We _always_ have both CDC ECM and CDC ACM functions. 130 * We _always_ have both CDC ECM and CDC ACM functions.
131 */ 131 */
132static int __init cdc_do_config(struct usb_configuration *c) 132static int __ref cdc_do_config(struct usb_configuration *c)
133{ 133{
134 int status; 134 int status;
135 135
@@ -159,7 +159,7 @@ static struct usb_configuration cdc_config_driver = {
159 159
160/*-------------------------------------------------------------------------*/ 160/*-------------------------------------------------------------------------*/
161 161
162static int __init cdc_bind(struct usb_composite_dev *cdev) 162static int __ref cdc_bind(struct usb_composite_dev *cdev)
163{ 163{
164 int gcnum; 164 int gcnum;
165 struct usb_gadget *gadget = cdev->gadget; 165 struct usb_gadget *gadget = cdev->gadget;
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 391d169f8d0..e483f80822d 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -673,20 +673,83 @@ static int get_string(struct usb_composite_dev *cdev,
673 * string IDs. Drivers for functions, configurations, or gadgets will 673 * string IDs. Drivers for functions, configurations, or gadgets will
674 * then store that ID in the appropriate descriptors and string table. 674 * then store that ID in the appropriate descriptors and string table.
675 * 675 *
676 * All string identifier should be allocated using this routine, to 676 * All string identifier should be allocated using this,
677 * ensure that for example different functions don't wrongly assign 677 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
678 * different meanings to the same identifier. 678 * that for example different functions don't wrongly assign different
679 * meanings to the same identifier.
679 */ 680 */
680int usb_string_id(struct usb_composite_dev *cdev) 681int usb_string_id(struct usb_composite_dev *cdev)
681{ 682{
682 if (cdev->next_string_id < 254) { 683 if (cdev->next_string_id < 254) {
683 /* string id 0 is reserved */ 684 /* string id 0 is reserved by USB spec for list of
685 * supported languages */
686 /* 255 reserved as well? -- mina86 */
684 cdev->next_string_id++; 687 cdev->next_string_id++;
685 return cdev->next_string_id; 688 return cdev->next_string_id;
686 } 689 }
687 return -ENODEV; 690 return -ENODEV;
688} 691}
689 692
693/**
694 * usb_string_ids() - allocate unused string IDs in batch
695 * @cdev: the device whose string descriptor IDs are being allocated
696 * @str: an array of usb_string objects to assign numbers to
697 * Context: single threaded during gadget setup
698 *
699 * @usb_string_ids() is called from bind() callbacks to allocate
700 * string IDs. Drivers for functions, configurations, or gadgets will
701 * then copy IDs from the string table to the appropriate descriptors
702 * and string table for other languages.
703 *
704 * All string identifier should be allocated using this,
705 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
706 * example different functions don't wrongly assign different meanings
707 * to the same identifier.
708 */
709int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
710{
711 int next = cdev->next_string_id;
712
713 for (; str->s; ++str) {
714 if (unlikely(next >= 254))
715 return -ENODEV;
716 str->id = ++next;
717 }
718
719 cdev->next_string_id = next;
720
721 return 0;
722}
723
724/**
725 * usb_string_ids_n() - allocate unused string IDs in batch
726 * @cdev: the device whose string descriptor IDs are being allocated
727 * @n: number of string IDs to allocate
728 * Context: single threaded during gadget setup
729 *
730 * Returns the first requested ID. This ID and next @n-1 IDs are now
731 * valid IDs. At least providind that @n is non zore because if it
732 * is, returns last requested ID which is now very useful information.
733 *
734 * @usb_string_ids_n() is called from bind() callbacks to allocate
735 * string IDs. Drivers for functions, configurations, or gadgets will
736 * then store that ID in the appropriate descriptors and string table.
737 *
738 * All string identifier should be allocated using this,
739 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
740 * example different functions don't wrongly assign different meanings
741 * to the same identifier.
742 */
743int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
744{
745 unsigned next = c->next_string_id;
746 if (unlikely(n > 254 || (unsigned)next + n > 254))
747 return -ENODEV;
748 c->next_string_id += n;
749 return next + 1;
750}
751
752
690/*-------------------------------------------------------------------------*/ 753/*-------------------------------------------------------------------------*/
691 754
692static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) 755static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
@@ -893,6 +956,8 @@ static void composite_disconnect(struct usb_gadget *gadget)
893 spin_lock_irqsave(&cdev->lock, flags); 956 spin_lock_irqsave(&cdev->lock, flags);
894 if (cdev->config) 957 if (cdev->config)
895 reset_config(cdev); 958 reset_config(cdev);
959 if (composite->disconnect)
960 composite->disconnect(cdev);
896 spin_unlock_irqrestore(&cdev->lock, flags); 961 spin_unlock_irqrestore(&cdev->lock, flags);
897} 962}
898 963
diff --git a/drivers/usb/gadget/dbgp.c b/drivers/usb/gadget/dbgp.c
new file mode 100644
index 00000000000..0ed50a2c0a3
--- /dev/null
+++ b/drivers/usb/gadget/dbgp.c
@@ -0,0 +1,434 @@
1/*
2 * dbgp.c -- EHCI Debug Port device gadget
3 *
4 * Copyright (C) 2010 Stephane Duverger
5 *
6 * Released under the GPLv2.
7 *
8 */
9
10/* verbose messages */
11#include <linux/kernel.h>
12#include <linux/device.h>
13#include <linux/usb/ch9.h>
14#include <linux/usb/gadget.h>
15
16/* See comments in "zero.c" */
17#include "epautoconf.c"
18
19#ifdef CONFIG_USB_G_DBGP_SERIAL
20#include "u_serial.c"
21#endif
22
23#define DRIVER_VENDOR_ID 0x0525 /* NetChip */
24#define DRIVER_PRODUCT_ID 0xc0de /* undefined */
25
26#define USB_DEBUG_MAX_PACKET_SIZE 8
27#define DBGP_REQ_EP0_LEN 128
28#define DBGP_REQ_LEN 512
29
30static struct dbgp {
31 struct usb_gadget *gadget;
32 struct usb_request *req;
33 struct usb_ep *i_ep;
34 struct usb_ep *o_ep;
35#ifdef CONFIG_USB_G_DBGP_SERIAL
36 struct gserial *serial;
37#endif
38} dbgp;
39
40static struct usb_device_descriptor device_desc = {
41 .bLength = sizeof device_desc,
42 .bDescriptorType = USB_DT_DEVICE,
43 .bcdUSB = __constant_cpu_to_le16(0x0200),
44 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
45 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_ID),
46 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_ID),
47 .bNumConfigurations = 1,
48};
49
50static struct usb_debug_descriptor dbg_desc = {
51 .bLength = sizeof dbg_desc,
52 .bDescriptorType = USB_DT_DEBUG,
53};
54
55static struct usb_endpoint_descriptor i_desc = {
56 .bLength = USB_DT_ENDPOINT_SIZE,
57 .bDescriptorType = USB_DT_ENDPOINT,
58 .bmAttributes = USB_ENDPOINT_XFER_BULK,
59 .bEndpointAddress = USB_DIR_IN,
60};
61
62static struct usb_endpoint_descriptor o_desc = {
63 .bLength = USB_DT_ENDPOINT_SIZE,
64 .bDescriptorType = USB_DT_ENDPOINT,
65 .bmAttributes = USB_ENDPOINT_XFER_BULK,
66 .bEndpointAddress = USB_DIR_OUT,
67};
68
69#ifdef CONFIG_USB_G_DBGP_PRINTK
70static int dbgp_consume(char *buf, unsigned len)
71{
72 char c;
73
74 if (!len)
75 return 0;
76
77 c = buf[len-1];
78 if (c != 0)
79 buf[len-1] = 0;
80
81 printk(KERN_NOTICE "%s%c", buf, c);
82 return 0;
83}
84
85static void __disable_ep(struct usb_ep *ep)
86{
87 if (ep && ep->driver_data == dbgp.gadget) {
88 usb_ep_disable(ep);
89 ep->driver_data = NULL;
90 }
91}
92
93static void dbgp_disable_ep(void)
94{
95 __disable_ep(dbgp.i_ep);
96 __disable_ep(dbgp.o_ep);
97}
98
99static void dbgp_complete(struct usb_ep *ep, struct usb_request *req)
100{
101 int stp;
102 int err = 0;
103 int status = req->status;
104
105 if (ep == dbgp.i_ep) {
106 stp = 1;
107 goto fail;
108 }
109
110 if (status != 0) {
111 stp = 2;
112 goto release_req;
113 }
114
115 dbgp_consume(req->buf, req->actual);
116
117 req->length = DBGP_REQ_LEN;
118 err = usb_ep_queue(ep, req, GFP_ATOMIC);
119 if (err < 0) {
120 stp = 3;
121 goto release_req;
122 }
123
124 return;
125
126release_req:
127 kfree(req->buf);
128 usb_ep_free_request(dbgp.o_ep, req);
129 dbgp_disable_ep();
130fail:
131 dev_dbg(&dbgp.gadget->dev,
132 "complete: failure (%d:%d) ==> %d\n", stp, err, status);
133}
134
135static int dbgp_enable_ep_req(struct usb_ep *ep)
136{
137 int err, stp;
138 struct usb_request *req;
139
140 req = usb_ep_alloc_request(ep, GFP_KERNEL);
141 if (!req) {
142 err = -ENOMEM;
143 stp = 1;
144 goto fail_1;
145 }
146
147 req->buf = kmalloc(DBGP_REQ_LEN, GFP_KERNEL);
148 if (!req->buf) {
149 err = -ENOMEM;
150 stp = 2;
151 goto fail_2;
152 }
153
154 req->complete = dbgp_complete;
155 req->length = DBGP_REQ_LEN;
156 err = usb_ep_queue(ep, req, GFP_ATOMIC);
157 if (err < 0) {
158 stp = 3;
159 goto fail_3;
160 }
161
162 return 0;
163
164fail_3:
165 kfree(req->buf);
166fail_2:
167 usb_ep_free_request(dbgp.o_ep, req);
168fail_1:
169 dev_dbg(&dbgp.gadget->dev,
170 "enable ep req: failure (%d:%d)\n", stp, err);
171 return err;
172}
173
174static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc)
175{
176 int err = usb_ep_enable(ep, desc);
177 ep->driver_data = dbgp.gadget;
178 return err;
179}
180
181static int dbgp_enable_ep(void)
182{
183 int err, stp;
184
185 err = __enable_ep(dbgp.i_ep, &i_desc);
186 if (err < 0) {
187 stp = 1;
188 goto fail_1;
189 }
190
191 err = __enable_ep(dbgp.o_ep, &o_desc);
192 if (err < 0) {
193 stp = 2;
194 goto fail_2;
195 }
196
197 err = dbgp_enable_ep_req(dbgp.o_ep);
198 if (err < 0) {
199 stp = 3;
200 goto fail_3;
201 }
202
203 return 0;
204
205fail_3:
206 __disable_ep(dbgp.o_ep);
207fail_2:
208 __disable_ep(dbgp.i_ep);
209fail_1:
210 dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err);
211 return err;
212}
213#endif
214
215static void dbgp_disconnect(struct usb_gadget *gadget)
216{
217#ifdef CONFIG_USB_G_DBGP_PRINTK
218 dbgp_disable_ep();
219#else
220 gserial_disconnect(dbgp.serial);
221#endif
222}
223
224static void dbgp_unbind(struct usb_gadget *gadget)
225{
226#ifdef CONFIG_USB_G_DBGP_SERIAL
227 kfree(dbgp.serial);
228#endif
229 if (dbgp.req) {
230 kfree(dbgp.req->buf);
231 usb_ep_free_request(gadget->ep0, dbgp.req);
232 }
233
234 gadget->ep0->driver_data = NULL;
235}
236
237static int __init dbgp_configure_endpoints(struct usb_gadget *gadget)
238{
239 int stp;
240
241 usb_ep_autoconfig_reset(gadget);
242
243 dbgp.i_ep = usb_ep_autoconfig(gadget, &i_desc);
244 if (!dbgp.i_ep) {
245 stp = 1;
246 goto fail_1;
247 }
248
249 dbgp.i_ep->driver_data = gadget;
250 i_desc.wMaxPacketSize =
251 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
252
253 dbgp.o_ep = usb_ep_autoconfig(gadget, &o_desc);
254 if (!dbgp.o_ep) {
255 dbgp.i_ep->driver_data = NULL;
256 stp = 2;
257 goto fail_2;
258 }
259
260 dbgp.o_ep->driver_data = gadget;
261 o_desc.wMaxPacketSize =
262 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
263
264 dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress & 0x7f;
265 dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress & 0x7f;
266
267#ifdef CONFIG_USB_G_DBGP_SERIAL
268 dbgp.serial->in = dbgp.i_ep;
269 dbgp.serial->out = dbgp.o_ep;
270
271 dbgp.serial->in_desc = &i_desc;
272 dbgp.serial->out_desc = &o_desc;
273
274 if (gserial_setup(gadget, 1) < 0) {
275 stp = 3;
276 goto fail_3;
277 }
278
279 return 0;
280
281fail_3:
282 dbgp.o_ep->driver_data = NULL;
283#else
284 return 0;
285#endif
286fail_2:
287 dbgp.i_ep->driver_data = NULL;
288fail_1:
289 dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp);
290 return -ENODEV;
291}
292
293static int __init dbgp_bind(struct usb_gadget *gadget)
294{
295 int err, stp;
296
297 dbgp.gadget = gadget;
298
299 dbgp.req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
300 if (!dbgp.req) {
301 err = -ENOMEM;
302 stp = 1;
303 goto fail;
304 }
305
306 dbgp.req->buf = kmalloc(DBGP_REQ_EP0_LEN, GFP_KERNEL);
307 if (!dbgp.req->buf) {
308 err = -ENOMEM;
309 stp = 2;
310 goto fail;
311 }
312
313 dbgp.req->length = DBGP_REQ_EP0_LEN;
314 gadget->ep0->driver_data = gadget;
315
316#ifdef CONFIG_USB_G_DBGP_SERIAL
317 dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL);
318 if (!dbgp.serial) {
319 stp = 3;
320 err = -ENOMEM;
321 goto fail;
322 }
323#endif
324 err = dbgp_configure_endpoints(gadget);
325 if (err < 0) {
326 stp = 4;
327 goto fail;
328 }
329
330 dev_dbg(&dbgp.gadget->dev, "bind: success\n");
331 return 0;
332
333fail:
334 dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err);
335 dbgp_unbind(gadget);
336 return err;
337}
338
339static void dbgp_setup_complete(struct usb_ep *ep,
340 struct usb_request *req)
341{
342 dev_dbg(&dbgp.gadget->dev, "setup complete: %d, %d/%d\n",
343 req->status, req->actual, req->length);
344}
345
346static int dbgp_setup(struct usb_gadget *gadget,
347 const struct usb_ctrlrequest *ctrl)
348{
349 struct usb_request *req = dbgp.req;
350 u8 request = ctrl->bRequest;
351 u16 value = le16_to_cpu(ctrl->wValue);
352 u16 length = le16_to_cpu(ctrl->wLength);
353 int err = 0;
354 void *data;
355 u16 len;
356
357 gadget->ep0->driver_data = gadget;
358
359 if (request == USB_REQ_GET_DESCRIPTOR) {
360 switch (value>>8) {
361 case USB_DT_DEVICE:
362 dev_dbg(&dbgp.gadget->dev, "setup: desc device\n");
363 len = sizeof device_desc;
364 data = &device_desc;
365 break;
366 case USB_DT_DEBUG:
367 dev_dbg(&dbgp.gadget->dev, "setup: desc debug\n");
368 len = sizeof dbg_desc;
369 data = &dbg_desc;
370 break;
371 default:
372 goto fail;
373 }
374 } else if (request == USB_REQ_SET_FEATURE &&
375 value == USB_DEVICE_DEBUG_MODE) {
376 len = 0;
377 data = NULL;
378 dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n");
379#ifdef CONFIG_USB_G_DBGP_PRINTK
380 err = dbgp_enable_ep();
381#else
382 err = gserial_connect(dbgp.serial, 0);
383#endif
384 if (err < 0)
385 goto fail;
386 } else
387 goto fail;
388
389 if (len >= 0) {
390 req->length = min(length, len);
391 req->zero = len < req->length;
392 if (data && req->length)
393 memcpy(req->buf, data, req->length);
394
395 req->complete = dbgp_setup_complete;
396 return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
397 }
398
399fail:
400 dev_dbg(&dbgp.gadget->dev,
401 "setup: failure req %x v %x\n", request, value);
402 return err;
403}
404
405static struct usb_gadget_driver dbgp_driver = {
406 .function = "dbgp",
407 .speed = USB_SPEED_HIGH,
408 .bind = dbgp_bind,
409 .unbind = dbgp_unbind,
410 .setup = dbgp_setup,
411 .disconnect = dbgp_disconnect,
412 .driver = {
413 .owner = THIS_MODULE,
414 .name = "dbgp"
415 },
416};
417
418static int __init dbgp_init(void)
419{
420 return usb_gadget_register_driver(&dbgp_driver);
421}
422
423static void __exit dbgp_exit(void)
424{
425 usb_gadget_unregister_driver(&dbgp_driver);
426#ifdef CONFIG_USB_G_DBGP_SERIAL
427 gserial_cleanup();
428#endif
429}
430
431MODULE_AUTHOR("Stephane Duverger");
432MODULE_LICENSE("GPL");
433module_init(dbgp_init);
434module_exit(dbgp_exit);
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
index 4f9e578cde9..dc6546248ed 100644
--- a/drivers/usb/gadget/dummy_hcd.c
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -1542,7 +1542,7 @@ static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1542 dum = hcd_to_dummy (hcd); 1542 dum = hcd_to_dummy (hcd);
1543 1543
1544 spin_lock_irqsave (&dum->lock, flags); 1544 spin_lock_irqsave (&dum->lock, flags);
1545 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) 1545 if (!HCD_HW_ACCESSIBLE(hcd))
1546 goto done; 1546 goto done;
1547 1547
1548 if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) { 1548 if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) {
@@ -1588,7 +1588,7 @@ static int dummy_hub_control (
1588 int retval = 0; 1588 int retval = 0;
1589 unsigned long flags; 1589 unsigned long flags;
1590 1590
1591 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) 1591 if (!HCD_HW_ACCESSIBLE(hcd))
1592 return -ETIMEDOUT; 1592 return -ETIMEDOUT;
1593 1593
1594 dum = hcd_to_dummy (hcd); 1594 dum = hcd_to_dummy (hcd);
@@ -1739,7 +1739,7 @@ static int dummy_bus_resume (struct usb_hcd *hcd)
1739 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__); 1739 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
1740 1740
1741 spin_lock_irq (&dum->lock); 1741 spin_lock_irq (&dum->lock);
1742 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { 1742 if (!HCD_HW_ACCESSIBLE(hcd)) {
1743 rc = -ESHUTDOWN; 1743 rc = -ESHUTDOWN;
1744 } else { 1744 } else {
1745 dum->rh_state = DUMMY_RH_RUNNING; 1745 dum->rh_state = DUMMY_RH_RUNNING;
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index 400f80372d9..114fa024c22 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -237,7 +237,7 @@ static u8 hostaddr[ETH_ALEN];
237 * the first one present. That's to make Microsoft's drivers happy, 237 * the first one present. That's to make Microsoft's drivers happy,
238 * and to follow DOCSIS 1.0 (cable modem standard). 238 * and to follow DOCSIS 1.0 (cable modem standard).
239 */ 239 */
240static int __init rndis_do_config(struct usb_configuration *c) 240static int __ref rndis_do_config(struct usb_configuration *c)
241{ 241{
242 /* FIXME alloc iConfiguration string, set it in c->strings */ 242 /* FIXME alloc iConfiguration string, set it in c->strings */
243 243
@@ -270,7 +270,7 @@ MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
270/* 270/*
271 * We _always_ have an ECM, CDC Subset, or EEM configuration. 271 * We _always_ have an ECM, CDC Subset, or EEM configuration.
272 */ 272 */
273static int __init eth_do_config(struct usb_configuration *c) 273static int __ref eth_do_config(struct usb_configuration *c)
274{ 274{
275 /* FIXME alloc iConfiguration string, set it in c->strings */ 275 /* FIXME alloc iConfiguration string, set it in c->strings */
276 276
@@ -297,7 +297,7 @@ static struct usb_configuration eth_config_driver = {
297 297
298/*-------------------------------------------------------------------------*/ 298/*-------------------------------------------------------------------------*/
299 299
300static int __init eth_bind(struct usb_composite_dev *cdev) 300static int __ref eth_bind(struct usb_composite_dev *cdev)
301{ 301{
302 int gcnum; 302 int gcnum;
303 struct usb_gadget *gadget = cdev->gadget; 303 struct usb_gadget *gadget = cdev->gadget;
diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c
index 2aaa0f75c6c..e4f59505520 100644
--- a/drivers/usb/gadget/f_fs.c
+++ b/drivers/usb/gadget/f_fs.c
@@ -714,9 +714,7 @@ static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
714 struct ffs_function *func = ffs->func; 714 struct ffs_function *func = ffs->func;
715 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; 715 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
716 } else if (gadget->ops->ioctl) { 716 } else if (gadget->ops->ioctl) {
717 lock_kernel();
718 ret = gadget->ops->ioctl(gadget, code, value); 717 ret = gadget->ops->ioctl(gadget, code, value);
719 unlock_kernel();
720 } else { 718 } else {
721 ret = -ENOTTY; 719 ret = -ENOTTY;
722 } 720 }
@@ -1377,7 +1375,8 @@ static void ffs_data_reset(struct ffs_data *ffs)
1377 1375
1378static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev) 1376static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1379{ 1377{
1380 unsigned i, count; 1378 struct usb_gadget_strings **lang;
1379 int first_id;
1381 1380
1382 ENTER(); 1381 ENTER();
1383 1382
@@ -1385,7 +1384,9 @@ static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1385 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags))) 1384 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1386 return -EBADFD; 1385 return -EBADFD;
1387 1386
1388 ffs_data_get(ffs); 1387 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1388 if (unlikely(first_id < 0))
1389 return first_id;
1389 1390
1390 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); 1391 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1391 if (unlikely(!ffs->ep0req)) 1392 if (unlikely(!ffs->ep0req))
@@ -1393,25 +1394,16 @@ static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1393 ffs->ep0req->complete = ffs_ep0_complete; 1394 ffs->ep0req->complete = ffs_ep0_complete;
1394 ffs->ep0req->context = ffs; 1395 ffs->ep0req->context = ffs;
1395 1396
1396 /* Get strings identifiers */ 1397 lang = ffs->stringtabs;
1397 for (count = ffs->strings_count, i = 0; i < count; ++i) { 1398 for (lang = ffs->stringtabs; *lang; ++lang) {
1398 struct usb_gadget_strings **lang; 1399 struct usb_string *str = (*lang)->strings;
1399 1400 int id = first_id;
1400 int id = usb_string_id(cdev); 1401 for (; str->s; ++id, ++str)
1401 if (unlikely(id < 0)) { 1402 str->id = id;
1402 usb_ep_free_request(cdev->gadget->ep0, ffs->ep0req);
1403 ffs->ep0req = NULL;
1404 return id;
1405 }
1406
1407 lang = ffs->stringtabs;
1408 do {
1409 (*lang)->strings[i].id = id;
1410 ++lang;
1411 } while (*lang);
1412 } 1403 }
1413 1404
1414 ffs->gadget = cdev->gadget; 1405 ffs->gadget = cdev->gadget;
1406 ffs_data_get(ffs);
1415 return 0; 1407 return 0;
1416} 1408}
1417 1409
@@ -1480,9 +1472,9 @@ static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1480} 1472}
1481 1473
1482 1474
1483static int functionfs_add(struct usb_composite_dev *cdev, 1475static int functionfs_bind_config(struct usb_composite_dev *cdev,
1484 struct usb_configuration *c, 1476 struct usb_configuration *c,
1485 struct ffs_data *ffs) 1477 struct ffs_data *ffs)
1486{ 1478{
1487 struct ffs_function *func; 1479 struct ffs_function *func;
1488 int ret; 1480 int ret;
diff --git a/drivers/usb/gadget/f_hid.c b/drivers/usb/gadget/f_hid.c
index 9447427fcbf..111b85ca7ac 100644
--- a/drivers/usb/gadget/f_hid.c
+++ b/drivers/usb/gadget/f_hid.c
@@ -142,7 +142,7 @@ static struct usb_descriptor_header *hidg_fs_descriptors[] = {
142static ssize_t f_hidg_read(struct file *file, char __user *buffer, 142static ssize_t f_hidg_read(struct file *file, char __user *buffer,
143 size_t count, loff_t *ptr) 143 size_t count, loff_t *ptr)
144{ 144{
145 struct f_hidg *hidg = (struct f_hidg *)file->private_data; 145 struct f_hidg *hidg = file->private_data;
146 char *tmp_buff = NULL; 146 char *tmp_buff = NULL;
147 unsigned long flags; 147 unsigned long flags;
148 148
@@ -200,7 +200,7 @@ static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
200static ssize_t f_hidg_write(struct file *file, const char __user *buffer, 200static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
201 size_t count, loff_t *offp) 201 size_t count, loff_t *offp)
202{ 202{
203 struct f_hidg *hidg = (struct f_hidg *)file->private_data; 203 struct f_hidg *hidg = file->private_data;
204 ssize_t status = -ENOMEM; 204 ssize_t status = -ENOMEM;
205 205
206 if (!access_ok(VERIFY_READ, buffer, count)) 206 if (!access_ok(VERIFY_READ, buffer, count))
@@ -257,7 +257,7 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
257 257
258static unsigned int f_hidg_poll(struct file *file, poll_table *wait) 258static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
259{ 259{
260 struct f_hidg *hidg = (struct f_hidg *)file->private_data; 260 struct f_hidg *hidg = file->private_data;
261 unsigned int ret = 0; 261 unsigned int ret = 0;
262 262
263 poll_wait(file, &hidg->read_queue, wait); 263 poll_wait(file, &hidg->read_queue, wait);
diff --git a/drivers/usb/gadget/f_loopback.c b/drivers/usb/gadget/f_loopback.c
index e91d1b16d9b..43225879c3c 100644
--- a/drivers/usb/gadget/f_loopback.c
+++ b/drivers/usb/gadget/f_loopback.c
@@ -324,7 +324,7 @@ static void loopback_disable(struct usb_function *f)
324 324
325/*-------------------------------------------------------------------------*/ 325/*-------------------------------------------------------------------------*/
326 326
327static int __init loopback_bind_config(struct usb_configuration *c) 327static int __ref loopback_bind_config(struct usb_configuration *c)
328{ 328{
329 struct f_loopback *loop; 329 struct f_loopback *loop;
330 int status; 330 int status;
@@ -346,7 +346,7 @@ static int __init loopback_bind_config(struct usb_configuration *c)
346 return status; 346 return status;
347} 347}
348 348
349static struct usb_configuration loopback_driver = { 349static struct usb_configuration loopback_driver = {
350 .label = "loopback", 350 .label = "loopback",
351 .strings = loopback_strings, 351 .strings = loopback_strings,
352 .bind = loopback_bind_config, 352 .bind = loopback_bind_config,
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index 4ce899c9b16..32cce029f65 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -316,6 +316,27 @@ static const char fsg_string_interface[] = "Mass Storage";
316/*-------------------------------------------------------------------------*/ 316/*-------------------------------------------------------------------------*/
317 317
318struct fsg_dev; 318struct fsg_dev;
319struct fsg_common;
320
321/* FSF callback functions */
322struct fsg_operations {
323 /* Callback function to call when thread exits. If no
324 * callback is set or it returns value lower then zero MSF
325 * will force eject all LUNs it operates on (including those
326 * marked as non-removable or with prevent_medium_removal flag
327 * set). */
328 int (*thread_exits)(struct fsg_common *common);
329
330 /* Called prior to ejection. Negative return means error,
331 * zero means to continue with ejection, positive means not to
332 * eject. */
333 int (*pre_eject)(struct fsg_common *common,
334 struct fsg_lun *lun, int num);
335 /* Called after ejection. Negative return means error, zero
336 * or positive is just a success. */
337 int (*post_eject)(struct fsg_common *common,
338 struct fsg_lun *lun, int num);
339};
319 340
320 341
321/* Data shared by all the FSG instances. */ 342/* Data shared by all the FSG instances. */
@@ -333,7 +354,6 @@ struct fsg_common {
333 struct usb_ep *ep0; /* Copy of gadget->ep0 */ 354 struct usb_ep *ep0; /* Copy of gadget->ep0 */
334 struct usb_request *ep0req; /* Copy of cdev->req */ 355 struct usb_request *ep0req; /* Copy of cdev->req */
335 unsigned int ep0_req_tag; 356 unsigned int ep0_req_tag;
336 const char *ep0req_name;
337 357
338 struct fsg_buffhd *next_buffhd_to_fill; 358 struct fsg_buffhd *next_buffhd_to_fill;
339 struct fsg_buffhd *next_buffhd_to_drain; 359 struct fsg_buffhd *next_buffhd_to_drain;
@@ -369,8 +389,8 @@ struct fsg_common {
369 struct completion thread_notifier; 389 struct completion thread_notifier;
370 struct task_struct *thread_task; 390 struct task_struct *thread_task;
371 391
372 /* Callback function to call when thread exits. */ 392 /* Callback functions. */
373 int (*thread_exits)(struct fsg_common *common); 393 const struct fsg_operations *ops;
374 /* Gadget's private data. */ 394 /* Gadget's private data. */
375 void *private_data; 395 void *private_data;
376 396
@@ -394,12 +414,8 @@ struct fsg_config {
394 const char *lun_name_format; 414 const char *lun_name_format;
395 const char *thread_name; 415 const char *thread_name;
396 416
397 /* Callback function to call when thread exits. If no 417 /* Callback functions. */
398 * callback is set or it returns value lower then zero MSF 418 const struct fsg_operations *ops;
399 * will force eject all LUNs it operates on (including those
400 * marked as non-removable or with prevent_medium_removal flag
401 * set). */
402 int (*thread_exits)(struct fsg_common *common);
403 /* Gadget's private data. */ 419 /* Gadget's private data. */
404 void *private_data; 420 void *private_data;
405 421
@@ -435,6 +451,7 @@ static inline int __fsg_is_set(struct fsg_common *common,
435 if (common->fsg) 451 if (common->fsg)
436 return 1; 452 return 1;
437 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line); 453 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
454 WARN_ON(1);
438 return 0; 455 return 0;
439} 456}
440 457
@@ -623,8 +640,6 @@ static int fsg_setup(struct usb_function *f,
623 640
624 /* Respond with data/status */ 641 /* Respond with data/status */
625 req->length = min((u16)1, w_length); 642 req->length = min((u16)1, w_length);
626 fsg->common->ep0req_name =
627 ctrl->bRequestType & USB_DIR_IN ? "ep0-in" : "ep0-out";
628 return ep0_queue(fsg->common); 643 return ep0_queue(fsg->common);
629 } 644 }
630 645
@@ -1395,43 +1410,55 @@ static int do_start_stop(struct fsg_common *common)
1395 } else if (!curlun->removable) { 1410 } else if (!curlun->removable) {
1396 curlun->sense_data = SS_INVALID_COMMAND; 1411 curlun->sense_data = SS_INVALID_COMMAND;
1397 return -EINVAL; 1412 return -EINVAL;
1398 } 1413 } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
1399 1414 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
1400 loej = common->cmnd[4] & 0x02;
1401 start = common->cmnd[4] & 0x01;
1402
1403 /* eject code from file_storage.c:do_start_stop() */
1404
1405 if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
1406 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
1407 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1415 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1408 return -EINVAL; 1416 return -EINVAL;
1409 } 1417 }
1410 1418
1411 if (!start) { 1419 loej = common->cmnd[4] & 0x02;
1412 /* Are we allowed to unload the media? */ 1420 start = common->cmnd[4] & 0x01;
1413 if (curlun->prevent_medium_removal) {
1414 LDBG(curlun, "unload attempt prevented\n");
1415 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1416 return -EINVAL;
1417 }
1418 if (loej) { /* Simulate an unload/eject */
1419 up_read(&common->filesem);
1420 down_write(&common->filesem);
1421 fsg_lun_close(curlun);
1422 up_write(&common->filesem);
1423 down_read(&common->filesem);
1424 }
1425 } else {
1426 1421
1427 /* Our emulation doesn't support mounting; the medium is 1422 /* Our emulation doesn't support mounting; the medium is
1428 * available for use as soon as it is loaded. */ 1423 * available for use as soon as it is loaded. */
1424 if (start) {
1429 if (!fsg_lun_is_open(curlun)) { 1425 if (!fsg_lun_is_open(curlun)) {
1430 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 1426 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1431 return -EINVAL; 1427 return -EINVAL;
1432 } 1428 }
1429 return 0;
1433 } 1430 }
1434 return 0; 1431
1432 /* Are we allowed to unload the media? */
1433 if (curlun->prevent_medium_removal) {
1434 LDBG(curlun, "unload attempt prevented\n");
1435 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1436 return -EINVAL;
1437 }
1438
1439 if (!loej)
1440 return 0;
1441
1442 /* Simulate an unload/eject */
1443 if (common->ops && common->ops->pre_eject) {
1444 int r = common->ops->pre_eject(common, curlun,
1445 curlun - common->luns);
1446 if (unlikely(r < 0))
1447 return r;
1448 else if (r)
1449 return 0;
1450 }
1451
1452 up_read(&common->filesem);
1453 down_write(&common->filesem);
1454 fsg_lun_close(curlun);
1455 up_write(&common->filesem);
1456 down_read(&common->filesem);
1457
1458 return common->ops && common->ops->post_eject
1459 ? min(0, common->ops->post_eject(common, curlun,
1460 curlun - common->luns))
1461 : 0;
1435} 1462}
1436 1463
1437 1464
@@ -2610,7 +2637,8 @@ static int fsg_main_thread(void *common_)
2610 common->thread_task = NULL; 2637 common->thread_task = NULL;
2611 spin_unlock_irq(&common->lock); 2638 spin_unlock_irq(&common->lock);
2612 2639
2613 if (!common->thread_exits || common->thread_exits(common) < 0) { 2640 if (!common->ops || !common->ops->thread_exits
2641 || common->ops->thread_exits(common) < 0) {
2614 struct fsg_lun *curlun = common->luns; 2642 struct fsg_lun *curlun = common->luns;
2615 unsigned i = common->nluns; 2643 unsigned i = common->nluns;
2616 2644
@@ -2686,6 +2714,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2686 common->free_storage_on_release = 0; 2714 common->free_storage_on_release = 0;
2687 } 2715 }
2688 2716
2717 common->ops = cfg->ops;
2689 common->private_data = cfg->private_data; 2718 common->private_data = cfg->private_data;
2690 2719
2691 common->gadget = gadget; 2720 common->gadget = gadget;
@@ -2807,7 +2836,6 @@ buffhds_first_it:
2807 2836
2808 2837
2809 /* Tell the thread to start working */ 2838 /* Tell the thread to start working */
2810 common->thread_exits = cfg->thread_exits;
2811 common->thread_task = 2839 common->thread_task =
2812 kthread_create(fsg_main_thread, common, 2840 kthread_create(fsg_main_thread, common,
2813 OR(cfg->thread_name, "file-storage")); 2841 OR(cfg->thread_name, "file-storage"));
@@ -2990,9 +3018,9 @@ static struct usb_gadget_strings *fsg_strings_array[] = {
2990 NULL, 3018 NULL,
2991}; 3019};
2992 3020
2993static int fsg_add(struct usb_composite_dev *cdev, 3021static int fsg_bind_config(struct usb_composite_dev *cdev,
2994 struct usb_configuration *c, 3022 struct usb_configuration *c,
2995 struct fsg_common *common) 3023 struct fsg_common *common)
2996{ 3024{
2997 struct fsg_dev *fsg; 3025 struct fsg_dev *fsg;
2998 int rc; 3026 int rc;
@@ -3024,6 +3052,13 @@ static int fsg_add(struct usb_composite_dev *cdev,
3024 return rc; 3052 return rc;
3025} 3053}
3026 3054
3055static inline int __deprecated __maybe_unused
3056fsg_add(struct usb_composite_dev *cdev,
3057 struct usb_configuration *c,
3058 struct fsg_common *common)
3059{
3060 return fsg_bind_config(cdev, c, common);
3061}
3027 3062
3028 3063
3029/************************* Module parameters *************************/ 3064/************************* Module parameters *************************/
@@ -3096,8 +3131,8 @@ fsg_config_from_params(struct fsg_config *cfg,
3096 cfg->product_name = 0; 3131 cfg->product_name = 0;
3097 cfg->release = 0xffff; 3132 cfg->release = 0xffff;
3098 3133
3099 cfg->thread_exits = 0; 3134 cfg->ops = NULL;
3100 cfg->private_data = 0; 3135 cfg->private_data = NULL;
3101 3136
3102 /* Finalise */ 3137 /* Finalise */
3103 cfg->can_stall = params->stall; 3138 cfg->can_stall = params->stall;
diff --git a/drivers/usb/gadget/f_sourcesink.c b/drivers/usb/gadget/f_sourcesink.c
index 6d3cc443d91..685d768f336 100644
--- a/drivers/usb/gadget/f_sourcesink.c
+++ b/drivers/usb/gadget/f_sourcesink.c
@@ -404,7 +404,7 @@ static void sourcesink_disable(struct usb_function *f)
404 404
405/*-------------------------------------------------------------------------*/ 405/*-------------------------------------------------------------------------*/
406 406
407static int __init sourcesink_bind_config(struct usb_configuration *c) 407static int __ref sourcesink_bind_config(struct usb_configuration *c)
408{ 408{
409 struct f_sourcesink *ss; 409 struct f_sourcesink *ss;
410 int status; 410 int status;
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index b49d86e3e45..a857b7ac238 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -56,7 +56,7 @@
56 * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03), 56 * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
57 * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by 57 * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
58 * the optional "protocol" module parameter. In addition, the default 58 * the optional "protocol" module parameter. In addition, the default
59 * Vendor ID, Product ID, and release number can be overridden. 59 * Vendor ID, Product ID, release number and serial number can be overridden.
60 * 60 *
61 * There is support for multiple logical units (LUNs), each of which has 61 * There is support for multiple logical units (LUNs), each of which has
62 * its own backing file. The number of LUNs can be set using the optional 62 * its own backing file. The number of LUNs can be set using the optional
@@ -93,6 +93,8 @@
93 * removable Default false, boolean for removable media 93 * removable Default false, boolean for removable media
94 * luns=N Default N = number of filenames, number of 94 * luns=N Default N = number of filenames, number of
95 * LUNs to support 95 * LUNs to support
96 * nofua=b[,b...] Default false, booleans for ignore FUA flag
97 * in SCSI WRITE(10,12) commands
96 * stall Default determined according to the type of 98 * stall Default determined according to the type of
97 * USB device controller (usually true), 99 * USB device controller (usually true),
98 * boolean to permit the driver to halt 100 * boolean to permit the driver to halt
@@ -106,17 +108,18 @@
106 * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID 108 * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
107 * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID 109 * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
108 * release=0xRRRR Override the USB release number (bcdDevice) 110 * release=0xRRRR Override the USB release number (bcdDevice)
111 * serial=HHHH... Override serial number (string of hex chars)
109 * buflen=N Default N=16384, buffer size used (will be 112 * buflen=N Default N=16384, buffer size used (will be
110 * rounded down to a multiple of 113 * rounded down to a multiple of
111 * PAGE_CACHE_SIZE) 114 * PAGE_CACHE_SIZE)
112 * 115 *
113 * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "ro", 116 * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "ro",
114 * "removable", "luns", "stall", and "cdrom" options are available; default 117 * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
115 * values are used for everything else. 118 * default values are used for everything else.
116 * 119 *
117 * The pathnames of the backing files and the ro settings are available in 120 * The pathnames of the backing files and the ro settings are available in
118 * the attribute files "file" and "ro" in the lun<n> subdirectory of the 121 * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
119 * gadget's sysfs directory. If the "removable" option is set, writing to 122 * the gadget's sysfs directory. If the "removable" option is set, writing to
120 * these files will simulate ejecting/loading the medium (writing an empty 123 * these files will simulate ejecting/loading the medium (writing an empty
121 * line means eject) and adjusting a write-enable tab. Changes to the ro 124 * line means eject) and adjusting a write-enable tab. Changes to the ro
122 * setting are not allowed when the medium is loaded or if CD-ROM emulation 125 * setting are not allowed when the medium is loaded or if CD-ROM emulation
@@ -270,6 +273,8 @@
270 273
271#define DRIVER_DESC "File-backed Storage Gadget" 274#define DRIVER_DESC "File-backed Storage Gadget"
272#define DRIVER_NAME "g_file_storage" 275#define DRIVER_NAME "g_file_storage"
276/* DRIVER_VERSION must be at least 6 characters long, as it is used
277 * to generate a fallback serial number. */
273#define DRIVER_VERSION "20 November 2008" 278#define DRIVER_VERSION "20 November 2008"
274 279
275static char fsg_string_manufacturer[64]; 280static char fsg_string_manufacturer[64];
@@ -301,8 +306,10 @@ MODULE_LICENSE("Dual BSD/GPL");
301static struct { 306static struct {
302 char *file[FSG_MAX_LUNS]; 307 char *file[FSG_MAX_LUNS];
303 int ro[FSG_MAX_LUNS]; 308 int ro[FSG_MAX_LUNS];
309 int nofua[FSG_MAX_LUNS];
304 unsigned int num_filenames; 310 unsigned int num_filenames;
305 unsigned int num_ros; 311 unsigned int num_ros;
312 unsigned int num_nofuas;
306 unsigned int nluns; 313 unsigned int nluns;
307 314
308 int removable; 315 int removable;
@@ -314,6 +321,7 @@ static struct {
314 unsigned short vendor; 321 unsigned short vendor;
315 unsigned short product; 322 unsigned short product;
316 unsigned short release; 323 unsigned short release;
324 char *serial;
317 unsigned int buflen; 325 unsigned int buflen;
318 326
319 int transport_type; 327 int transport_type;
@@ -341,6 +349,10 @@ MODULE_PARM_DESC(file, "names of backing files or devices");
341module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO); 349module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
342MODULE_PARM_DESC(ro, "true to force read-only"); 350MODULE_PARM_DESC(ro, "true to force read-only");
343 351
352module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
353 S_IRUGO);
354MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
355
344module_param_named(luns, mod_data.nluns, uint, S_IRUGO); 356module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
345MODULE_PARM_DESC(luns, "number of LUNs"); 357MODULE_PARM_DESC(luns, "number of LUNs");
346 358
@@ -353,6 +365,8 @@ MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
353module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO); 365module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
354MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk"); 366MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
355 367
368module_param_named(serial, mod_data.serial, charp, S_IRUGO);
369MODULE_PARM_DESC(serial, "USB serial number");
356 370
357/* In the non-TEST version, only the module parameters listed above 371/* In the non-TEST version, only the module parameters listed above
358 * are available. */ 372 * are available. */
@@ -1272,7 +1286,8 @@ static int do_write(struct fsg_dev *fsg)
1272 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1286 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1273 return -EINVAL; 1287 return -EINVAL;
1274 } 1288 }
1275 if (fsg->cmnd[1] & 0x08) { // FUA 1289 /* FUA */
1290 if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
1276 spin_lock(&curlun->filp->f_lock); 1291 spin_lock(&curlun->filp->f_lock);
1277 curlun->filp->f_flags |= O_DSYNC; 1292 curlun->filp->f_flags |= O_DSYNC;
1278 spin_unlock(&curlun->filp->f_lock); 1293 spin_unlock(&curlun->filp->f_lock);
@@ -3126,6 +3141,7 @@ static int fsg_main_thread(void *fsg_)
3126 3141
3127/* The write permissions and store_xxx pointers are set in fsg_bind() */ 3142/* The write permissions and store_xxx pointers are set in fsg_bind() */
3128static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL); 3143static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
3144static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
3129static DEVICE_ATTR(file, 0444, fsg_show_file, NULL); 3145static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
3130 3146
3131 3147
@@ -3197,6 +3213,7 @@ static int __init check_parameters(struct fsg_dev *fsg)
3197{ 3213{
3198 int prot; 3214 int prot;
3199 int gcnum; 3215 int gcnum;
3216 int i;
3200 3217
3201 /* Store the default values */ 3218 /* Store the default values */
3202 mod_data.transport_type = USB_PR_BULK; 3219 mod_data.transport_type = USB_PR_BULK;
@@ -3272,13 +3289,65 @@ static int __init check_parameters(struct fsg_dev *fsg)
3272 ERROR(fsg, "invalid buflen\n"); 3289 ERROR(fsg, "invalid buflen\n");
3273 return -ETOOSMALL; 3290 return -ETOOSMALL;
3274 } 3291 }
3292
3275#endif /* CONFIG_USB_FILE_STORAGE_TEST */ 3293#endif /* CONFIG_USB_FILE_STORAGE_TEST */
3276 3294
3295 /* Serial string handling.
3296 * On a real device, the serial string would be loaded
3297 * from permanent storage. */
3298 if (mod_data.serial) {
3299 const char *ch;
3300 unsigned len = 0;
3301
3302 /* Sanity check :
3303 * The CB[I] specification limits the serial string to
3304 * 12 uppercase hexadecimal characters.
3305 * BBB need at least 12 uppercase hexadecimal characters,
3306 * with a maximum of 126. */
3307 for (ch = mod_data.serial; *ch; ++ch) {
3308 ++len;
3309 if ((*ch < '0' || *ch > '9') &&
3310 (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
3311 WARNING(fsg,
3312 "Invalid serial string character: %c; "
3313 "Failing back to default\n",
3314 *ch);
3315 goto fill_serial;
3316 }
3317 }
3318 if (len > 126 ||
3319 (mod_data.transport_type == USB_PR_BULK && len < 12) ||
3320 (mod_data.transport_type != USB_PR_BULK && len > 12)) {
3321 WARNING(fsg,
3322 "Invalid serial string length; "
3323 "Failing back to default\n");
3324 goto fill_serial;
3325 }
3326 fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
3327 } else {
3328 WARNING(fsg,
3329 "Userspace failed to provide serial number; "
3330 "Failing back to default\n");
3331fill_serial:
3332 /* Serial number not specified or invalid, make our own.
3333 * We just encode it from the driver version string,
3334 * 12 characters to comply with both CB[I] and BBB spec.
3335 * Warning : Two devices running the same kernel will have
3336 * the same fallback serial number. */
3337 for (i = 0; i < 12; i += 2) {
3338 unsigned char c = DRIVER_VERSION[i / 2];
3339
3340 if (!c)
3341 break;
3342 sprintf(&fsg_string_serial[i], "%02X", c);
3343 }
3344 }
3345
3277 return 0; 3346 return 0;
3278} 3347}
3279 3348
3280 3349
3281static int __init fsg_bind(struct usb_gadget *gadget) 3350static int __ref fsg_bind(struct usb_gadget *gadget)
3282{ 3351{
3283 struct fsg_dev *fsg = the_fsg; 3352 struct fsg_dev *fsg = the_fsg;
3284 int rc; 3353 int rc;
@@ -3305,6 +3374,10 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3305 } 3374 }
3306 } 3375 }
3307 3376
3377 /* Only for removable media? */
3378 dev_attr_nofua.attr.mode = 0644;
3379 dev_attr_nofua.store = fsg_store_nofua;
3380
3308 /* Find out how many LUNs there should be */ 3381 /* Find out how many LUNs there should be */
3309 i = mod_data.nluns; 3382 i = mod_data.nluns;
3310 if (i == 0) 3383 if (i == 0)
@@ -3330,6 +3403,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3330 curlun->ro = mod_data.cdrom || mod_data.ro[i]; 3403 curlun->ro = mod_data.cdrom || mod_data.ro[i];
3331 curlun->initially_ro = curlun->ro; 3404 curlun->initially_ro = curlun->ro;
3332 curlun->removable = mod_data.removable; 3405 curlun->removable = mod_data.removable;
3406 curlun->nofua = mod_data.nofua[i];
3333 curlun->dev.release = lun_release; 3407 curlun->dev.release = lun_release;
3334 curlun->dev.parent = &gadget->dev; 3408 curlun->dev.parent = &gadget->dev;
3335 curlun->dev.driver = &fsg_driver.driver; 3409 curlun->dev.driver = &fsg_driver.driver;
@@ -3344,6 +3418,8 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3344 if ((rc = device_create_file(&curlun->dev, 3418 if ((rc = device_create_file(&curlun->dev,
3345 &dev_attr_ro)) != 0 || 3419 &dev_attr_ro)) != 0 ||
3346 (rc = device_create_file(&curlun->dev, 3420 (rc = device_create_file(&curlun->dev,
3421 &dev_attr_nofua)) != 0 ||
3422 (rc = device_create_file(&curlun->dev,
3347 &dev_attr_file)) != 0) { 3423 &dev_attr_file)) != 0) {
3348 device_unregister(&curlun->dev); 3424 device_unregister(&curlun->dev);
3349 goto out; 3425 goto out;
@@ -3447,16 +3523,6 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3447 init_utsname()->sysname, init_utsname()->release, 3523 init_utsname()->sysname, init_utsname()->release,
3448 gadget->name); 3524 gadget->name);
3449 3525
3450 /* On a real device, serial[] would be loaded from permanent
3451 * storage. We just encode it from the driver version string. */
3452 for (i = 0; i < sizeof fsg_string_serial - 2; i += 2) {
3453 unsigned char c = DRIVER_VERSION[i / 2];
3454
3455 if (!c)
3456 break;
3457 sprintf(&fsg_string_serial[i], "%02X", c);
3458 }
3459
3460 fsg->thread_task = kthread_create(fsg_main_thread, fsg, 3526 fsg->thread_task = kthread_create(fsg_main_thread, fsg,
3461 "file-storage-gadget"); 3527 "file-storage-gadget");
3462 if (IS_ERR(fsg->thread_task)) { 3528 if (IS_ERR(fsg->thread_task)) {
@@ -3478,8 +3544,8 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3478 if (IS_ERR(p)) 3544 if (IS_ERR(p))
3479 p = NULL; 3545 p = NULL;
3480 } 3546 }
3481 LINFO(curlun, "ro=%d, file: %s\n", 3547 LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
3482 curlun->ro, (p ? p : "(error)")); 3548 curlun->ro, curlun->nofua, (p ? p : "(error)"));
3483 } 3549 }
3484 } 3550 }
3485 kfree(pathbuf); 3551 kfree(pathbuf);
diff --git a/drivers/usb/gadget/g_ffs.c b/drivers/usb/gadget/g_ffs.c
index d1af253a910..a9474f8d532 100644
--- a/drivers/usb/gadget/g_ffs.c
+++ b/drivers/usb/gadget/g_ffs.c
@@ -32,12 +32,13 @@
32# include "u_ether.c" 32# include "u_ether.c"
33 33
34static u8 gfs_hostaddr[ETH_ALEN]; 34static u8 gfs_hostaddr[ETH_ALEN];
35#else 35# ifdef CONFIG_USB_FUNCTIONFS_ETH
36# if !defined CONFIG_USB_FUNCTIONFS_GENERIC 36static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
37# define CONFIG_USB_FUNCTIONFS_GENERIC
38# endif 37# endif
38#else
39# define gether_cleanup() do { } while (0) 39# define gether_cleanup() do { } while (0)
40# define gether_setup(gadget, hostaddr) ((int)0) 40# define gether_setup(gadget, hostaddr) ((int)0)
41# define gfs_hostaddr NULL
41#endif 42#endif
42 43
43#include "f_fs.c" 44#include "f_fs.c"
@@ -107,15 +108,7 @@ static const struct usb_descriptor_header *gfs_otg_desc[] = {
107enum { 108enum {
108 GFS_STRING_MANUFACTURER_IDX, 109 GFS_STRING_MANUFACTURER_IDX,
109 GFS_STRING_PRODUCT_IDX, 110 GFS_STRING_PRODUCT_IDX,
110#ifdef CONFIG_USB_FUNCTIONFS_RNDIS 111 GFS_STRING_FIRST_CONFIG_IDX,
111 GFS_STRING_RNDIS_CONFIG_IDX,
112#endif
113#ifdef CONFIG_USB_FUNCTIONFS_ETH
114 GFS_STRING_ECM_CONFIG_IDX,
115#endif
116#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
117 GFS_STRING_GENERIC_CONFIG_IDX,
118#endif
119}; 112};
120 113
121static char gfs_manufacturer[50]; 114static char gfs_manufacturer[50];
@@ -126,13 +119,13 @@ static struct usb_string gfs_strings[] = {
126 [GFS_STRING_MANUFACTURER_IDX].s = gfs_manufacturer, 119 [GFS_STRING_MANUFACTURER_IDX].s = gfs_manufacturer,
127 [GFS_STRING_PRODUCT_IDX].s = gfs_driver_desc, 120 [GFS_STRING_PRODUCT_IDX].s = gfs_driver_desc,
128#ifdef CONFIG_USB_FUNCTIONFS_RNDIS 121#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
129 [GFS_STRING_RNDIS_CONFIG_IDX].s = "FunctionFS + RNDIS", 122 { .s = "FunctionFS + RNDIS" },
130#endif 123#endif
131#ifdef CONFIG_USB_FUNCTIONFS_ETH 124#ifdef CONFIG_USB_FUNCTIONFS_ETH
132 [GFS_STRING_ECM_CONFIG_IDX].s = "FunctionFS + ECM", 125 { .s = "FunctionFS + ECM" },
133#endif 126#endif
134#ifdef CONFIG_USB_FUNCTIONFS_GENERIC 127#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
135 [GFS_STRING_GENERIC_CONFIG_IDX].s = "FunctionFS", 128 { .s = "FunctionFS" },
136#endif 129#endif
137 { } /* end of list */ 130 { } /* end of list */
138}; 131};
@@ -146,59 +139,33 @@ static struct usb_gadget_strings *gfs_dev_strings[] = {
146}; 139};
147 140
148 141
142
143struct gfs_configuration {
144 struct usb_configuration c;
145 int (*eth)(struct usb_configuration *c, u8 *ethaddr);
146} gfs_configurations[] = {
149#ifdef CONFIG_USB_FUNCTIONFS_RNDIS 147#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
150static int gfs_do_rndis_config(struct usb_configuration *c); 148 {
151 149 .eth = rndis_bind_config,
152static struct usb_configuration gfs_rndis_config_driver = { 150 },
153 .label = "FunctionFS + RNDIS",
154 .bind = gfs_do_rndis_config,
155 .bConfigurationValue = 1,
156 /* .iConfiguration = DYNAMIC */
157 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
158};
159# define gfs_add_rndis_config(cdev) \
160 usb_add_config(cdev, &gfs_rndis_config_driver)
161#else
162# define gfs_add_rndis_config(cdev) 0
163#endif 151#endif
164 152
165
166#ifdef CONFIG_USB_FUNCTIONFS_ETH 153#ifdef CONFIG_USB_FUNCTIONFS_ETH
167static int gfs_do_ecm_config(struct usb_configuration *c); 154 {
168 155 .eth = eth_bind_config,
169static struct usb_configuration gfs_ecm_config_driver = { 156 },
170 .label = "FunctionFS + ECM",
171 .bind = gfs_do_ecm_config,
172 .bConfigurationValue = 1,
173 /* .iConfiguration = DYNAMIC */
174 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
175};
176# define gfs_add_ecm_config(cdev) \
177 usb_add_config(cdev, &gfs_ecm_config_driver)
178#else
179# define gfs_add_ecm_config(cdev) 0
180#endif 157#endif
181 158
182
183#ifdef CONFIG_USB_FUNCTIONFS_GENERIC 159#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
184static int gfs_do_generic_config(struct usb_configuration *c); 160 {
185 161 },
186static struct usb_configuration gfs_generic_config_driver = {
187 .label = "FunctionFS",
188 .bind = gfs_do_generic_config,
189 .bConfigurationValue = 2,
190 /* .iConfiguration = DYNAMIC */
191 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
192};
193# define gfs_add_generic_config(cdev) \
194 usb_add_config(cdev, &gfs_generic_config_driver)
195#else
196# define gfs_add_generic_config(cdev) 0
197#endif 162#endif
163};
198 164
199 165
200static int gfs_bind(struct usb_composite_dev *cdev); 166static int gfs_bind(struct usb_composite_dev *cdev);
201static int gfs_unbind(struct usb_composite_dev *cdev); 167static int gfs_unbind(struct usb_composite_dev *cdev);
168static int gfs_do_config(struct usb_configuration *c);
202 169
203static struct usb_composite_driver gfs_driver = { 170static struct usb_composite_driver gfs_driver = {
204 .name = gfs_short_name, 171 .name = gfs_short_name,
@@ -267,7 +234,7 @@ static int functionfs_check_dev_callback(const char *dev_name)
267 234
268static int gfs_bind(struct usb_composite_dev *cdev) 235static int gfs_bind(struct usb_composite_dev *cdev)
269{ 236{
270 int ret; 237 int ret, i;
271 238
272 ENTER(); 239 ENTER();
273 240
@@ -284,57 +251,32 @@ static int gfs_bind(struct usb_composite_dev *cdev)
284 snprintf(gfs_manufacturer, sizeof gfs_manufacturer, "%s %s with %s", 251 snprintf(gfs_manufacturer, sizeof gfs_manufacturer, "%s %s with %s",
285 init_utsname()->sysname, init_utsname()->release, 252 init_utsname()->sysname, init_utsname()->release,
286 cdev->gadget->name); 253 cdev->gadget->name);
287 ret = usb_string_id(cdev);
288 if (unlikely(ret < 0))
289 goto error;
290 gfs_strings[GFS_STRING_MANUFACTURER_IDX].id = ret;
291 gfs_dev_desc.iManufacturer = ret;
292
293 ret = usb_string_id(cdev);
294 if (unlikely(ret < 0))
295 goto error;
296 gfs_strings[GFS_STRING_PRODUCT_IDX].id = ret;
297 gfs_dev_desc.iProduct = ret;
298
299#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
300 ret = usb_string_id(cdev);
301 if (unlikely(ret < 0))
302 goto error;
303 gfs_strings[GFS_STRING_RNDIS_CONFIG_IDX].id = ret;
304 gfs_rndis_config_driver.iConfiguration = ret;
305#endif
306 254
307#ifdef CONFIG_USB_FUNCTIONFS_ETH 255 ret = usb_string_ids_tab(cdev, gfs_strings);
308 ret = usb_string_id(cdev);
309 if (unlikely(ret < 0)) 256 if (unlikely(ret < 0))
310 goto error; 257 goto error;
311 gfs_strings[GFS_STRING_ECM_CONFIG_IDX].id = ret;
312 gfs_ecm_config_driver.iConfiguration = ret;
313#endif
314 258
315#ifdef CONFIG_USB_FUNCTIONFS_GENERIC 259 gfs_dev_desc.iManufacturer = gfs_strings[GFS_STRING_MANUFACTURER_IDX].id;
316 ret = usb_string_id(cdev); 260 gfs_dev_desc.iProduct = gfs_strings[GFS_STRING_PRODUCT_IDX].id;
317 if (unlikely(ret < 0))
318 goto error;
319 gfs_strings[GFS_STRING_GENERIC_CONFIG_IDX].id = ret;
320 gfs_generic_config_driver.iConfiguration = ret;
321#endif
322 261
323 ret = functionfs_bind(gfs_ffs_data, cdev); 262 ret = functionfs_bind(gfs_ffs_data, cdev);
324 if (unlikely(ret < 0)) 263 if (unlikely(ret < 0))
325 goto error; 264 goto error;
326 265
327 ret = gfs_add_rndis_config(cdev); 266 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
328 if (unlikely(ret < 0)) 267 struct gfs_configuration *c = gfs_configurations + i;
329 goto error_unbind;
330 268
331 ret = gfs_add_ecm_config(cdev); 269 ret = GFS_STRING_FIRST_CONFIG_IDX + i;
332 if (unlikely(ret < 0)) 270 c->c.label = gfs_strings[ret].s;
333 goto error_unbind; 271 c->c.iConfiguration = gfs_strings[ret].id;
272 c->c.bind = gfs_do_config;
273 c->c.bConfigurationValue = 1 + i;
274 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
334 275
335 ret = gfs_add_generic_config(cdev); 276 ret = usb_add_config(cdev, &c->c);
336 if (unlikely(ret < 0)) 277 if (unlikely(ret < 0))
337 goto error_unbind; 278 goto error_unbind;
279 }
338 280
339 return 0; 281 return 0;
340 282
@@ -368,10 +310,10 @@ static int gfs_unbind(struct usb_composite_dev *cdev)
368} 310}
369 311
370 312
371static int __gfs_do_config(struct usb_configuration *c, 313static int gfs_do_config(struct usb_configuration *c)
372 int (*eth)(struct usb_configuration *c, u8 *ethaddr),
373 u8 *ethaddr)
374{ 314{
315 struct gfs_configuration *gc =
316 container_of(c, struct gfs_configuration, c);
375 int ret; 317 int ret;
376 318
377 if (WARN_ON(!gfs_ffs_data)) 319 if (WARN_ON(!gfs_ffs_data))
@@ -382,13 +324,13 @@ static int __gfs_do_config(struct usb_configuration *c,
382 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; 324 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
383 } 325 }
384 326
385 if (eth) { 327 if (gc->eth) {
386 ret = eth(c, ethaddr); 328 ret = gc->eth(c, gfs_hostaddr);
387 if (unlikely(ret < 0)) 329 if (unlikely(ret < 0))
388 return ret; 330 return ret;
389 } 331 }
390 332
391 ret = functionfs_add(c->cdev, c, gfs_ffs_data); 333 ret = functionfs_bind_config(c->cdev, c, gfs_ffs_data);
392 if (unlikely(ret < 0)) 334 if (unlikely(ret < 0))
393 return ret; 335 return ret;
394 336
@@ -406,32 +348,12 @@ static int __gfs_do_config(struct usb_configuration *c,
406 return 0; 348 return 0;
407} 349}
408 350
409#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
410static int gfs_do_rndis_config(struct usb_configuration *c)
411{
412 ENTER();
413
414 return __gfs_do_config(c, rndis_bind_config, gfs_hostaddr);
415}
416#endif
417 351
418#ifdef CONFIG_USB_FUNCTIONFS_ETH 352#ifdef CONFIG_USB_FUNCTIONFS_ETH
419static int gfs_do_ecm_config(struct usb_configuration *c) 353static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
420{
421 ENTER();
422
423 return __gfs_do_config(c,
424 can_support_ecm(c->cdev->gadget)
425 ? ecm_bind_config : geth_bind_config,
426 gfs_hostaddr);
427}
428#endif
429
430#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
431static int gfs_do_generic_config(struct usb_configuration *c)
432{ 354{
433 ENTER(); 355 return can_support_ecm(c->cdev->gadget)
434 356 ? ecm_bind_config(c, ethaddr)
435 return __gfs_do_config(c, NULL, NULL); 357 : geth_bind_config(c, ethaddr);
436} 358}
437#endif 359#endif
diff --git a/drivers/usb/gadget/gmidi.c b/drivers/usb/gadget/gmidi.c
index b7bf88019b0..1b413a5cc3f 100644
--- a/drivers/usb/gadget/gmidi.c
+++ b/drivers/usb/gadget/gmidi.c
@@ -1157,7 +1157,7 @@ fail:
1157/* 1157/*
1158 * Creates an output endpoint, and initializes output ports. 1158 * Creates an output endpoint, and initializes output ports.
1159 */ 1159 */
1160static int __init gmidi_bind(struct usb_gadget *gadget) 1160static int __ref gmidi_bind(struct usb_gadget *gadget)
1161{ 1161{
1162 struct gmidi_device *dev; 1162 struct gmidi_device *dev;
1163 struct usb_ep *in_ep, *out_ep; 1163 struct usb_ep *in_ep, *out_ep;
diff --git a/drivers/usb/gadget/hid.c b/drivers/usb/gadget/hid.c
index 775722686ed..735495bf841 100644
--- a/drivers/usb/gadget/hid.c
+++ b/drivers/usb/gadget/hid.c
@@ -127,7 +127,7 @@ static struct usb_gadget_strings *dev_strings[] = {
127 127
128/****************************** Configurations ******************************/ 128/****************************** Configurations ******************************/
129 129
130static int __init do_config(struct usb_configuration *c) 130static int __ref do_config(struct usb_configuration *c)
131{ 131{
132 struct hidg_func_node *e; 132 struct hidg_func_node *e;
133 int func = 0, status = 0; 133 int func = 0, status = 0;
@@ -156,7 +156,7 @@ static struct usb_configuration config_driver = {
156 156
157/****************************** Gadget Bind ******************************/ 157/****************************** Gadget Bind ******************************/
158 158
159static int __init hid_bind(struct usb_composite_dev *cdev) 159static int __ref hid_bind(struct usb_composite_dev *cdev)
160{ 160{
161 struct usb_gadget *gadget = cdev->gadget; 161 struct usb_gadget *gadget = cdev->gadget;
162 struct list_head *tmp; 162 struct list_head *tmp;
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index de8a8380350..fc35406fc80 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -1299,11 +1299,9 @@ static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
1299 struct usb_gadget *gadget = dev->gadget; 1299 struct usb_gadget *gadget = dev->gadget;
1300 long ret = -ENOTTY; 1300 long ret = -ENOTTY;
1301 1301
1302 if (gadget->ops->ioctl) { 1302 if (gadget->ops->ioctl)
1303 lock_kernel();
1304 ret = gadget->ops->ioctl (gadget, code, value); 1303 ret = gadget->ops->ioctl (gadget, code, value);
1305 unlock_kernel(); 1304
1306 }
1307 return ret; 1305 return ret;
1308} 1306}
1309 1307
@@ -1867,13 +1865,9 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1867 buf += 4; 1865 buf += 4;
1868 length -= 4; 1866 length -= 4;
1869 1867
1870 kbuf = kmalloc (length, GFP_KERNEL); 1868 kbuf = memdup_user(buf, length);
1871 if (!kbuf) 1869 if (IS_ERR(kbuf))
1872 return -ENOMEM; 1870 return PTR_ERR(kbuf);
1873 if (copy_from_user (kbuf, buf, length)) {
1874 kfree (kbuf);
1875 return -EFAULT;
1876 }
1877 1871
1878 spin_lock_irq (&dev->lock); 1872 spin_lock_irq (&dev->lock);
1879 value = -EINVAL; 1873 value = -EINVAL;
diff --git a/drivers/usb/gadget/langwell_udc.c b/drivers/usb/gadget/langwell_udc.c
index f1a070280c9..d41b69cf508 100644
--- a/drivers/usb/gadget/langwell_udc.c
+++ b/drivers/usb/gadget/langwell_udc.c
@@ -841,9 +841,9 @@ static int langwell_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
841 VDBG(dev, "req->mapped = 0\n"); 841 VDBG(dev, "req->mapped = 0\n");
842 } 842 }
843 843
844 DBG(dev, "%s queue req %p, len %u, buf %p, dma 0x%08x\n", 844 DBG(dev, "%s queue req %p, len %u, buf %p, dma 0x%08llx\n",
845 _ep->name, 845 _ep->name,
846 _req, _req->length, _req->buf, _req->dma); 846 _req, _req->length, _req->buf, (unsigned long long)_req->dma);
847 847
848 _req->status = -EINPROGRESS; 848 _req->status = -EINPROGRESS;
849 _req->actual = 0; 849 _req->actual = 0;
diff --git a/drivers/usb/gadget/mass_storage.c b/drivers/usb/gadget/mass_storage.c
index 705cc1f7632..585f2559484 100644
--- a/drivers/usb/gadget/mass_storage.c
+++ b/drivers/usb/gadget/mass_storage.c
@@ -141,9 +141,14 @@ static int msg_thread_exits(struct fsg_common *common)
141 return 0; 141 return 0;
142} 142}
143 143
144static int __init msg_do_config(struct usb_configuration *c) 144static int __ref msg_do_config(struct usb_configuration *c)
145{ 145{
146 struct fsg_common *common; 146 static const struct fsg_operations ops = {
147 .thread_exits = msg_thread_exits,
148 };
149 static struct fsg_common common;
150
151 struct fsg_common *retp;
147 struct fsg_config config; 152 struct fsg_config config;
148 int ret; 153 int ret;
149 154
@@ -153,13 +158,14 @@ static int __init msg_do_config(struct usb_configuration *c)
153 } 158 }
154 159
155 fsg_config_from_params(&config, &mod_data); 160 fsg_config_from_params(&config, &mod_data);
156 config.thread_exits = msg_thread_exits; 161 config.ops = &ops;
157 common = fsg_common_init(0, c->cdev, &config); 162
158 if (IS_ERR(common)) 163 retp = fsg_common_init(&common, c->cdev, &config);
159 return PTR_ERR(common); 164 if (IS_ERR(retp))
165 return PTR_ERR(retp);
160 166
161 ret = fsg_add(c->cdev, c, common); 167 ret = fsg_bind_config(c->cdev, c, &common);
162 fsg_common_put(common); 168 fsg_common_put(&common);
163 return ret; 169 return ret;
164} 170}
165 171
@@ -176,7 +182,7 @@ static struct usb_configuration msg_config_driver = {
176/****************************** Gadget Bind ******************************/ 182/****************************** Gadget Bind ******************************/
177 183
178 184
179static int __init msg_bind(struct usb_composite_dev *cdev) 185static int __ref msg_bind(struct usb_composite_dev *cdev)
180{ 186{
181 struct usb_gadget *gadget = cdev->gadget; 187 struct usb_gadget *gadget = cdev->gadget;
182 int status; 188 int status;
diff --git a/drivers/usb/gadget/multi.c b/drivers/usb/gadget/multi.c
index a930d7fd7e7..795d7623216 100644
--- a/drivers/usb/gadget/multi.c
+++ b/drivers/usb/gadget/multi.c
@@ -24,6 +24,7 @@
24 24
25#include <linux/kernel.h> 25#include <linux/kernel.h>
26#include <linux/utsname.h> 26#include <linux/utsname.h>
27#include <linux/module.h>
27 28
28 29
29#if defined USB_ETH_RNDIS 30#if defined USB_ETH_RNDIS
@@ -35,14 +36,13 @@
35 36
36 37
37#define DRIVER_DESC "Multifunction Composite Gadget" 38#define DRIVER_DESC "Multifunction Composite Gadget"
38#define DRIVER_VERSION "2009/07/21"
39 39
40/*-------------------------------------------------------------------------*/ 40MODULE_DESCRIPTION(DRIVER_DESC);
41MODULE_AUTHOR("Michal Nazarewicz");
42MODULE_LICENSE("GPL");
41 43
42#define MULTI_VENDOR_NUM 0x0525 /* XXX NetChip */
43#define MULTI_PRODUCT_NUM 0xa4ab /* XXX */
44 44
45/*-------------------------------------------------------------------------*/ 45/***************************** All the files... *****************************/
46 46
47/* 47/*
48 * kbuild is not very cooperative with respect to linking separately 48 * kbuild is not very cooperative with respect to linking separately
@@ -57,6 +57,8 @@
57#include "config.c" 57#include "config.c"
58#include "epautoconf.c" 58#include "epautoconf.c"
59 59
60#include "f_mass_storage.c"
61
60#include "u_serial.c" 62#include "u_serial.c"
61#include "f_acm.c" 63#include "f_acm.c"
62 64
@@ -68,13 +70,24 @@
68#endif 70#endif
69#include "u_ether.c" 71#include "u_ether.c"
70 72
71#undef DBG /* u_ether.c has broken idea about macros */
72#undef VDBG /* so clean up after it */
73#undef ERROR
74#undef INFO
75#include "f_mass_storage.c"
76 73
77/*-------------------------------------------------------------------------*/ 74
75/***************************** Device Descriptor ****************************/
76
77#define MULTI_VENDOR_NUM 0x0525 /* XXX NetChip */
78#define MULTI_PRODUCT_NUM 0xa4ab /* XXX */
79
80
81enum {
82 __MULTI_NO_CONFIG,
83#ifdef CONFIG_USB_G_MULTI_RNDIS
84 MULTI_RNDIS_CONFIG_NUM,
85#endif
86#ifdef CONFIG_USB_G_MULTI_CDC
87 MULTI_CDC_CONFIG_NUM,
88#endif
89};
90
78 91
79static struct usb_device_descriptor device_desc = { 92static struct usb_device_descriptor device_desc = {
80 .bLength = sizeof device_desc, 93 .bLength = sizeof device_desc,
@@ -82,80 +95,82 @@ static struct usb_device_descriptor device_desc = {
82 95
83 .bcdUSB = cpu_to_le16(0x0200), 96 .bcdUSB = cpu_to_le16(0x0200),
84 97
85 /* .bDeviceClass = USB_CLASS_COMM, */ 98 .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
86 /* .bDeviceSubClass = 0, */
87 /* .bDeviceProtocol = 0, */
88 .bDeviceClass = 0xEF,
89 .bDeviceSubClass = 2, 99 .bDeviceSubClass = 2,
90 .bDeviceProtocol = 1, 100 .bDeviceProtocol = 1,
91 /* .bMaxPacketSize0 = f(hardware) */
92 101
93 /* Vendor and product id can be overridden by module parameters. */ 102 /* Vendor and product id can be overridden by module parameters. */
94 .idVendor = cpu_to_le16(MULTI_VENDOR_NUM), 103 .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
95 .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM), 104 .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
96 /* .bcdDevice = f(hardware) */
97 /* .iManufacturer = DYNAMIC */
98 /* .iProduct = DYNAMIC */
99 /* NO SERIAL NUMBER */
100 .bNumConfigurations = 1,
101}; 105};
102 106
103static struct usb_otg_descriptor otg_descriptor = {
104 .bLength = sizeof otg_descriptor,
105 .bDescriptorType = USB_DT_OTG,
106
107 /* REVISIT SRP-only hardware is possible, although
108 * it would not be called "OTG" ...
109 */
110 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
111};
112 107
113static const struct usb_descriptor_header *otg_desc[] = { 108static const struct usb_descriptor_header *otg_desc[] = {
114 (struct usb_descriptor_header *) &otg_descriptor, 109 (struct usb_descriptor_header *) &(struct usb_otg_descriptor){
110 .bLength = sizeof(struct usb_otg_descriptor),
111 .bDescriptorType = USB_DT_OTG,
112
113 /*
114 * REVISIT SRP-only hardware is possible, although
115 * it would not be called "OTG" ...
116 */
117 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
118 },
115 NULL, 119 NULL,
116}; 120};
117 121
118 122
119/* string IDs are assigned dynamically */ 123enum {
120 124 MULTI_STRING_MANUFACTURER_IDX,
121#define STRING_MANUFACTURER_IDX 0 125 MULTI_STRING_PRODUCT_IDX,
122#define STRING_PRODUCT_IDX 1 126#ifdef CONFIG_USB_G_MULTI_RNDIS
127 MULTI_STRING_RNDIS_CONFIG_IDX,
128#endif
129#ifdef CONFIG_USB_G_MULTI_CDC
130 MULTI_STRING_CDC_CONFIG_IDX,
131#endif
132};
123 133
124static char manufacturer[50]; 134static char manufacturer[50];
125 135
126static struct usb_string strings_dev[] = { 136static struct usb_string strings_dev[] = {
127 [STRING_MANUFACTURER_IDX].s = manufacturer, 137 [MULTI_STRING_MANUFACTURER_IDX].s = manufacturer,
128 [STRING_PRODUCT_IDX].s = DRIVER_DESC, 138 [MULTI_STRING_PRODUCT_IDX].s = DRIVER_DESC,
139#ifdef CONFIG_USB_G_MULTI_RNDIS
140 [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
141#endif
142#ifdef CONFIG_USB_G_MULTI_CDC
143 [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM",
144#endif
129 { } /* end of list */ 145 { } /* end of list */
130}; 146};
131 147
132static struct usb_gadget_strings stringtab_dev = {
133 .language = 0x0409, /* en-us */
134 .strings = strings_dev,
135};
136
137static struct usb_gadget_strings *dev_strings[] = { 148static struct usb_gadget_strings *dev_strings[] = {
138 &stringtab_dev, 149 &(struct usb_gadget_strings){
150 .language = 0x0409, /* en-us */
151 .strings = strings_dev,
152 },
139 NULL, 153 NULL,
140}; 154};
141 155
142static u8 hostaddr[ETH_ALEN];
143 156
144 157
145 158
146/****************************** Configurations ******************************/ 159/****************************** Configurations ******************************/
147 160
148static struct fsg_module_parameters mod_data = { 161static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
149 .stall = 1 162FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
150}; 163
151FSG_MODULE_PARAMETERS(/* no prefix */, mod_data); 164static struct fsg_common fsg_common;
165
166static u8 hostaddr[ETH_ALEN];
152 167
153static struct fsg_common *fsg_common;
154 168
169/********** RNDIS **********/
155 170
156#ifdef USB_ETH_RNDIS 171#ifdef USB_ETH_RNDIS
157 172
158static int __init rndis_do_config(struct usb_configuration *c) 173static __ref int rndis_do_config(struct usb_configuration *c)
159{ 174{
160 int ret; 175 int ret;
161 176
@@ -172,26 +187,42 @@ static int __init rndis_do_config(struct usb_configuration *c)
172 if (ret < 0) 187 if (ret < 0)
173 return ret; 188 return ret;
174 189
175 ret = fsg_add(c->cdev, c, fsg_common); 190 ret = fsg_bind_config(c->cdev, c, &fsg_common);
176 if (ret < 0) 191 if (ret < 0)
177 return ret; 192 return ret;
178 193
179 return 0; 194 return 0;
180} 195}
181 196
182static struct usb_configuration rndis_config_driver = { 197static int rndis_config_register(struct usb_composite_dev *cdev)
183 .label = "Multifunction Composite (RNDIS + MS + ACM)", 198{
184 .bind = rndis_do_config, 199 static struct usb_configuration config = {
185 .bConfigurationValue = 2, 200 .bind = rndis_do_config,
186 /* .iConfiguration = DYNAMIC */ 201 .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
187 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 202 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
188}; 203 };
204
205 config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
206 config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
207
208 return usb_add_config(cdev, &config);
209}
210
211#else
212
213static int rndis_config_register(struct usb_composite_dev *cdev)
214{
215 return 0;
216}
189 217
190#endif 218#endif
191 219
220
221/********** CDC ECM **********/
222
192#ifdef CONFIG_USB_G_MULTI_CDC 223#ifdef CONFIG_USB_G_MULTI_CDC
193 224
194static int __init cdc_do_config(struct usb_configuration *c) 225static __ref int cdc_do_config(struct usb_configuration *c)
195{ 226{
196 int ret; 227 int ret;
197 228
@@ -208,20 +239,33 @@ static int __init cdc_do_config(struct usb_configuration *c)
208 if (ret < 0) 239 if (ret < 0)
209 return ret; 240 return ret;
210 241
211 ret = fsg_add(c->cdev, c, fsg_common); 242 ret = fsg_bind_config(c->cdev, c, &fsg_common);
212 if (ret < 0) 243 if (ret < 0)
213 return ret; 244 return ret;
214 245
215 return 0; 246 return 0;
216} 247}
217 248
218static struct usb_configuration cdc_config_driver = { 249static int cdc_config_register(struct usb_composite_dev *cdev)
219 .label = "Multifunction Composite (CDC + MS + ACM)", 250{
220 .bind = cdc_do_config, 251 static struct usb_configuration config = {
221 .bConfigurationValue = 1, 252 .bind = cdc_do_config,
222 /* .iConfiguration = DYNAMIC */ 253 .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
223 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 254 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
224}; 255 };
256
257 config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
258 config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
259
260 return usb_add_config(cdev, &config);
261}
262
263#else
264
265static int cdc_config_register(struct usb_composite_dev *cdev)
266{
267 return 0;
268}
225 269
226#endif 270#endif
227 271
@@ -230,7 +274,7 @@ static struct usb_configuration cdc_config_driver = {
230/****************************** Gadget Bind ******************************/ 274/****************************** Gadget Bind ******************************/
231 275
232 276
233static int __init multi_bind(struct usb_composite_dev *cdev) 277static int __ref multi_bind(struct usb_composite_dev *cdev)
234{ 278{
235 struct usb_gadget *gadget = cdev->gadget; 279 struct usb_gadget *gadget = cdev->gadget;
236 int status, gcnum; 280 int status, gcnum;
@@ -252,67 +296,56 @@ static int __init multi_bind(struct usb_composite_dev *cdev)
252 goto fail0; 296 goto fail0;
253 297
254 /* set up mass storage function */ 298 /* set up mass storage function */
255 fsg_common = fsg_common_from_params(0, cdev, &mod_data); 299 {
256 if (IS_ERR(fsg_common)) { 300 void *retp;
257 status = PTR_ERR(fsg_common); 301 retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
258 goto fail1; 302 if (IS_ERR(retp)) {
303 status = PTR_ERR(retp);
304 goto fail1;
305 }
259 } 306 }
260 307
261 308 /* set bcdDevice */
262 gcnum = usb_gadget_controller_number(gadget); 309 gcnum = usb_gadget_controller_number(gadget);
263 if (gcnum >= 0) 310 if (gcnum >= 0) {
264 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); 311 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
265 else { 312 } else {
266 /* We assume that can_support_ecm() tells the truth; 313 WARNING(cdev, "controller '%s' not recognized\n", gadget->name);
267 * but if the controller isn't recognized at all then
268 * that assumption is a bit more likely to be wrong.
269 */
270 WARNING(cdev, "controller '%s' not recognized\n",
271 gadget->name);
272 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099); 314 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
273 } 315 }
274 316
275 317 /* allocate string descriptor numbers */
276 /* Allocate string descriptor numbers ... note that string
277 * contents can be overridden by the composite_dev glue.
278 */
279
280 /* device descriptor strings: manufacturer, product */
281 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", 318 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
282 init_utsname()->sysname, init_utsname()->release, 319 init_utsname()->sysname, init_utsname()->release,
283 gadget->name); 320 gadget->name);
284 status = usb_string_id(cdev);
285 if (status < 0)
286 goto fail2;
287 strings_dev[STRING_MANUFACTURER_IDX].id = status;
288 device_desc.iManufacturer = status;
289 321
290 status = usb_string_id(cdev); 322 status = usb_string_ids_tab(cdev, strings_dev);
291 if (status < 0) 323 if (unlikely(status < 0))
292 goto fail2; 324 goto fail2;
293 strings_dev[STRING_PRODUCT_IDX].id = status;
294 device_desc.iProduct = status;
295 325
296#ifdef USB_ETH_RNDIS 326 device_desc.iManufacturer =
297 /* register our first configuration */ 327 strings_dev[MULTI_STRING_MANUFACTURER_IDX].id;
298 status = usb_add_config(cdev, &rndis_config_driver); 328 device_desc.iProduct =
299 if (status < 0) 329 strings_dev[MULTI_STRING_PRODUCT_IDX].id;
330
331 /* register configurations */
332 status = rndis_config_register(cdev);
333 if (unlikely(status < 0))
300 goto fail2; 334 goto fail2;
301#endif
302 335
303#ifdef CONFIG_USB_G_MULTI_CDC 336 status = cdc_config_register(cdev);
304 /* register our second configuration */ 337 if (unlikely(status < 0))
305 status = usb_add_config(cdev, &cdc_config_driver);
306 if (status < 0)
307 goto fail2; 338 goto fail2;
308#endif
309 339
310 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n"); 340 /* we're done */
311 fsg_common_put(fsg_common); 341 dev_info(&gadget->dev, DRIVER_DESC "\n");
342 fsg_common_put(&fsg_common);
312 return 0; 343 return 0;
313 344
345
346 /* error recovery */
314fail2: 347fail2:
315 fsg_common_put(fsg_common); 348 fsg_common_put(&fsg_common);
316fail1: 349fail1:
317 gserial_cleanup(); 350 gserial_cleanup();
318fail0: 351fail0:
@@ -339,18 +372,15 @@ static struct usb_composite_driver multi_driver = {
339 .unbind = __exit_p(multi_unbind), 372 .unbind = __exit_p(multi_unbind),
340}; 373};
341 374
342MODULE_DESCRIPTION(DRIVER_DESC);
343MODULE_AUTHOR("Michal Nazarewicz");
344MODULE_LICENSE("GPL");
345 375
346static int __init g_multi_init(void) 376static int __init multi_init(void)
347{ 377{
348 return usb_composite_register(&multi_driver); 378 return usb_composite_register(&multi_driver);
349} 379}
350module_init(g_multi_init); 380module_init(multi_init);
351 381
352static void __exit g_multi_cleanup(void) 382static void __exit multi_exit(void)
353{ 383{
354 usb_composite_unregister(&multi_driver); 384 usb_composite_unregister(&multi_driver);
355} 385}
356module_exit(g_multi_cleanup); 386module_exit(multi_exit);
diff --git a/drivers/usb/gadget/printer.c b/drivers/usb/gadget/printer.c
index 4c3ac5c4223..cf241c371a7 100644
--- a/drivers/usb/gadget/printer.c
+++ b/drivers/usb/gadget/printer.c
@@ -25,7 +25,7 @@
25#include <linux/ioport.h> 25#include <linux/ioport.h>
26#include <linux/sched.h> 26#include <linux/sched.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/smp_lock.h> 28#include <linux/mutex.h>
29#include <linux/errno.h> 29#include <linux/errno.h>
30#include <linux/init.h> 30#include <linux/init.h>
31#include <linux/timer.h> 31#include <linux/timer.h>
@@ -70,6 +70,7 @@
70#define DRIVER_DESC "Printer Gadget" 70#define DRIVER_DESC "Printer Gadget"
71#define DRIVER_VERSION "2007 OCT 06" 71#define DRIVER_VERSION "2007 OCT 06"
72 72
73static DEFINE_MUTEX(printer_mutex);
73static const char shortname [] = "printer"; 74static const char shortname [] = "printer";
74static const char driver_desc [] = DRIVER_DESC; 75static const char driver_desc [] = DRIVER_DESC;
75 76
@@ -476,7 +477,7 @@ printer_open(struct inode *inode, struct file *fd)
476 unsigned long flags; 477 unsigned long flags;
477 int ret = -EBUSY; 478 int ret = -EBUSY;
478 479
479 lock_kernel(); 480 mutex_lock(&printer_mutex);
480 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev); 481 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
481 482
482 spin_lock_irqsave(&dev->lock, flags); 483 spin_lock_irqsave(&dev->lock, flags);
@@ -492,7 +493,7 @@ printer_open(struct inode *inode, struct file *fd)
492 spin_unlock_irqrestore(&dev->lock, flags); 493 spin_unlock_irqrestore(&dev->lock, flags);
493 494
494 DBG(dev, "printer_open returned %x\n", ret); 495 DBG(dev, "printer_open returned %x\n", ret);
495 unlock_kernel(); 496 mutex_unlock(&printer_mutex);
496 return ret; 497 return ret;
497} 498}
498 499
@@ -1346,7 +1347,7 @@ printer_unbind(struct usb_gadget *gadget)
1346 set_gadget_data(gadget, NULL); 1347 set_gadget_data(gadget, NULL);
1347} 1348}
1348 1349
1349static int __init 1350static int __ref
1350printer_bind(struct usb_gadget *gadget) 1351printer_bind(struct usb_gadget *gadget)
1351{ 1352{
1352 struct printer_dev *dev; 1353 struct printer_dev *dev;
diff --git a/drivers/usb/gadget/s3c-hsotg.c b/drivers/usb/gadget/s3c-hsotg.c
index 26193eceb32..521ebed0118 100644
--- a/drivers/usb/gadget/s3c-hsotg.c
+++ b/drivers/usb/gadget/s3c-hsotg.c
@@ -12,6 +12,8 @@
12 * published by the Free Software Foundation. 12 * published by the Free Software Foundation.
13*/ 13*/
14 14
15#define DEBUG
16
15#include <linux/kernel.h> 17#include <linux/kernel.h>
16#include <linux/module.h> 18#include <linux/module.h>
17#include <linux/spinlock.h> 19#include <linux/spinlock.h>
@@ -23,6 +25,7 @@
23#include <linux/delay.h> 25#include <linux/delay.h>
24#include <linux/io.h> 26#include <linux/io.h>
25#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/clk.h>
26 29
27#include <linux/usb/ch9.h> 30#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h> 31#include <linux/usb/gadget.h>
@@ -33,6 +36,7 @@
33#include <plat/regs-usb-hsotg.h> 36#include <plat/regs-usb-hsotg.h>
34#include <mach/regs-sys.h> 37#include <mach/regs-sys.h>
35#include <plat/udc-hs.h> 38#include <plat/udc-hs.h>
39#include <plat/cpu.h>
36 40
37#define DMA_ADDR_INVALID (~((dma_addr_t)0)) 41#define DMA_ADDR_INVALID (~((dma_addr_t)0))
38 42
@@ -91,7 +95,9 @@ struct s3c_hsotg_req;
91 * For periodic IN endpoints, we have fifo_size and fifo_load to try 95 * For periodic IN endpoints, we have fifo_size and fifo_load to try
92 * and keep track of the amount of data in the periodic FIFO for each 96 * and keep track of the amount of data in the periodic FIFO for each
93 * of these as we don't have a status register that tells us how much 97 * of these as we don't have a status register that tells us how much
94 * is in each of them. 98 * is in each of them. (note, this may actually be useless information
99 * as in shared-fifo mode periodic in acts like a single-frame packet
100 * buffer than a fifo)
95 */ 101 */
96struct s3c_hsotg_ep { 102struct s3c_hsotg_ep {
97 struct usb_ep ep; 103 struct usb_ep ep;
@@ -128,6 +134,7 @@ struct s3c_hsotg_ep {
128 * @regs: The memory area mapped for accessing registers. 134 * @regs: The memory area mapped for accessing registers.
129 * @regs_res: The resource that was allocated when claiming register space. 135 * @regs_res: The resource that was allocated when claiming register space.
130 * @irq: The IRQ number we are using 136 * @irq: The IRQ number we are using
137 * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
131 * @debug_root: root directrory for debugfs. 138 * @debug_root: root directrory for debugfs.
132 * @debug_file: main status file for debugfs. 139 * @debug_file: main status file for debugfs.
133 * @debug_fifo: FIFO status file for debugfs. 140 * @debug_fifo: FIFO status file for debugfs.
@@ -145,6 +152,9 @@ struct s3c_hsotg {
145 void __iomem *regs; 152 void __iomem *regs;
146 struct resource *regs_res; 153 struct resource *regs_res;
147 int irq; 154 int irq;
155 struct clk *clk;
156
157 unsigned int dedicated_fifos:1;
148 158
149 struct dentry *debug_root; 159 struct dentry *debug_root;
150 struct dentry *debug_file; 160 struct dentry *debug_file;
@@ -310,11 +320,11 @@ static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
310 hsotg->regs + S3C_GNPTXFSIZ); 320 hsotg->regs + S3C_GNPTXFSIZ);
311 */ 321 */
312 322
313 /* set FIFO sizes to 2048/0x1C0 */ 323 /* set FIFO sizes to 2048/1024 */
314 324
315 writel(2048, hsotg->regs + S3C_GRXFSIZ); 325 writel(2048, hsotg->regs + S3C_GRXFSIZ);
316 writel(S3C_GNPTXFSIZ_NPTxFStAddr(2048) | 326 writel(S3C_GNPTXFSIZ_NPTxFStAddr(2048) |
317 S3C_GNPTXFSIZ_NPTxFDep(0x1C0), 327 S3C_GNPTXFSIZ_NPTxFDep(1024),
318 hsotg->regs + S3C_GNPTXFSIZ); 328 hsotg->regs + S3C_GNPTXFSIZ);
319 329
320 /* arange all the rest of the TX FIFOs, as some versions of this 330 /* arange all the rest of the TX FIFOs, as some versions of this
@@ -464,7 +474,7 @@ static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
464 if (to_write == 0) 474 if (to_write == 0)
465 return 0; 475 return 0;
466 476
467 if (periodic) { 477 if (periodic && !hsotg->dedicated_fifos) {
468 u32 epsize = readl(hsotg->regs + S3C_DIEPTSIZ(hs_ep->index)); 478 u32 epsize = readl(hsotg->regs + S3C_DIEPTSIZ(hs_ep->index));
469 int size_left; 479 int size_left;
470 int size_done; 480 int size_done;
@@ -474,6 +484,14 @@ static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
474 484
475 size_left = S3C_DxEPTSIZ_XferSize_GET(epsize); 485 size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
476 486
487 /* if shared fifo, we cannot write anything until the
488 * previous data has been completely sent.
489 */
490 if (hs_ep->fifo_load != 0) {
491 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
492 return -ENOSPC;
493 }
494
477 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n", 495 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
478 __func__, size_left, 496 __func__, size_left,
479 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size); 497 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
@@ -494,6 +512,11 @@ static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
494 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_PTxFEmp); 512 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
495 return -ENOSPC; 513 return -ENOSPC;
496 } 514 }
515 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
516 can_write = readl(hsotg->regs + S3C_DTXFSTS(hs_ep->index));
517
518 can_write &= 0xffff;
519 can_write *= 4;
497 } else { 520 } else {
498 if (S3C_GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) { 521 if (S3C_GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
499 dev_dbg(hsotg->dev, 522 dev_dbg(hsotg->dev,
@@ -505,6 +528,7 @@ static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
505 } 528 }
506 529
507 can_write = S3C_GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts); 530 can_write = S3C_GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
531 can_write *= 4; /* fifo size is in 32bit quantities. */
508 } 532 }
509 533
510 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, mps %d\n", 534 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, mps %d\n",
@@ -517,6 +541,17 @@ static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
517 if (can_write > 512) 541 if (can_write > 512)
518 can_write = 512; 542 can_write = 512;
519 543
544 /* limit the write to one max-packet size worth of data, but allow
545 * the transfer to return that it did not run out of fifo space
546 * doing it. */
547 if (to_write > hs_ep->ep.maxpacket) {
548 to_write = hs_ep->ep.maxpacket;
549
550 s3c_hsotg_en_gsint(hsotg,
551 periodic ? S3C_GINTSTS_PTxFEmp :
552 S3C_GINTSTS_NPTxFEmp);
553 }
554
520 /* see if we can write data */ 555 /* see if we can write data */
521 556
522 if (to_write > can_write) { 557 if (to_write > can_write) {
@@ -579,12 +614,10 @@ static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
579 maxsize = S3C_DxEPTSIZ_XferSize_LIMIT + 1; 614 maxsize = S3C_DxEPTSIZ_XferSize_LIMIT + 1;
580 maxpkt = S3C_DxEPTSIZ_PktCnt_LIMIT + 1; 615 maxpkt = S3C_DxEPTSIZ_PktCnt_LIMIT + 1;
581 } else { 616 } else {
617 maxsize = 64+64;
582 if (hs_ep->dir_in) { 618 if (hs_ep->dir_in) {
583 /* maxsize = S3C_DIEPTSIZ0_XferSize_LIMIT + 1; */
584 maxsize = 64+64+1;
585 maxpkt = S3C_DIEPTSIZ0_PktCnt_LIMIT + 1; 619 maxpkt = S3C_DIEPTSIZ0_PktCnt_LIMIT + 1;
586 } else { 620 } else {
587 maxsize = 0x3f;
588 maxpkt = 2; 621 maxpkt = 2;
589 } 622 }
590 } 623 }
@@ -1353,6 +1386,9 @@ static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1353 read_ptr = hs_req->req.actual; 1386 read_ptr = hs_req->req.actual;
1354 max_req = hs_req->req.length - read_ptr; 1387 max_req = hs_req->req.length - read_ptr;
1355 1388
1389 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1390 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1391
1356 if (to_read > max_req) { 1392 if (to_read > max_req) {
1357 /* more data appeared than we where willing 1393 /* more data appeared than we where willing
1358 * to deal with in this request. 1394 * to deal with in this request.
@@ -1362,9 +1398,6 @@ static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1362 WARN_ON_ONCE(1); 1398 WARN_ON_ONCE(1);
1363 } 1399 }
1364 1400
1365 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1366 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1367
1368 hs_ep->total_data += to_read; 1401 hs_ep->total_data += to_read;
1369 hs_req->req.actual += to_read; 1402 hs_req->req.actual += to_read;
1370 to_read = DIV_ROUND_UP(to_read, 4); 1403 to_read = DIV_ROUND_UP(to_read, 4);
@@ -1433,9 +1466,11 @@ static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1433static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg, 1466static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1434 int epnum, bool was_setup) 1467 int epnum, bool was_setup)
1435{ 1468{
1469 u32 epsize = readl(hsotg->regs + S3C_DOEPTSIZ(epnum));
1436 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum]; 1470 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1437 struct s3c_hsotg_req *hs_req = hs_ep->req; 1471 struct s3c_hsotg_req *hs_req = hs_ep->req;
1438 struct usb_request *req = &hs_req->req; 1472 struct usb_request *req = &hs_req->req;
1473 unsigned size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
1439 int result = 0; 1474 int result = 0;
1440 1475
1441 if (!hs_req) { 1476 if (!hs_req) {
@@ -1444,9 +1479,7 @@ static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1444 } 1479 }
1445 1480
1446 if (using_dma(hsotg)) { 1481 if (using_dma(hsotg)) {
1447 u32 epsize = readl(hsotg->regs + S3C_DOEPTSIZ(epnum));
1448 unsigned size_done; 1482 unsigned size_done;
1449 unsigned size_left;
1450 1483
1451 /* Calculate the size of the transfer by checking how much 1484 /* Calculate the size of the transfer by checking how much
1452 * is left in the endpoint size register and then working it 1485 * is left in the endpoint size register and then working it
@@ -1456,14 +1489,18 @@ static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1456 * so may overshoot/undershoot the transfer. 1489 * so may overshoot/undershoot the transfer.
1457 */ 1490 */
1458 1491
1459 size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
1460
1461 size_done = hs_ep->size_loaded - size_left; 1492 size_done = hs_ep->size_loaded - size_left;
1462 size_done += hs_ep->last_load; 1493 size_done += hs_ep->last_load;
1463 1494
1464 req->actual = size_done; 1495 req->actual = size_done;
1465 } 1496 }
1466 1497
1498 /* if there is more request to do, schedule new transfer */
1499 if (req->actual < req->length && size_left == 0) {
1500 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1501 return;
1502 }
1503
1467 if (req->actual < req->length && req->short_not_ok) { 1504 if (req->actual < req->length && req->short_not_ok) {
1468 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n", 1505 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1469 __func__, req->actual, req->length); 1506 __func__, req->actual, req->length);
@@ -1758,7 +1795,7 @@ static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1758 if (dir_in) { 1795 if (dir_in) {
1759 s3c_hsotg_complete_in(hsotg, hs_ep); 1796 s3c_hsotg_complete_in(hsotg, hs_ep);
1760 1797
1761 if (idx == 0) 1798 if (idx == 0 && !hs_ep->req)
1762 s3c_hsotg_enqueue_setup(hsotg); 1799 s3c_hsotg_enqueue_setup(hsotg);
1763 } else if (using_dma(hsotg)) { 1800 } else if (using_dma(hsotg)) {
1764 /* We're using DMA, we need to fire an OutDone here 1801 /* We're using DMA, we need to fire an OutDone here
@@ -1818,6 +1855,15 @@ static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1818 __func__, idx); 1855 __func__, idx);
1819 clear |= S3C_DIEPMSK_INTknEPMisMsk; 1856 clear |= S3C_DIEPMSK_INTknEPMisMsk;
1820 } 1857 }
1858
1859 /* FIFO has space or is empty (see GAHBCFG) */
1860 if (hsotg->dedicated_fifos &&
1861 ints & S3C_DIEPMSK_TxFIFOEmpty) {
1862 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
1863 __func__, idx);
1864 s3c_hsotg_trytx(hsotg, hs_ep);
1865 clear |= S3C_DIEPMSK_TxFIFOEmpty;
1866 }
1821 } 1867 }
1822 1868
1823 writel(clear, hsotg->regs + epint_reg); 1869 writel(clear, hsotg->regs + epint_reg);
@@ -2071,17 +2117,12 @@ irq_retry:
2071 kill_all_requests(hsotg, &hsotg->eps[0], -ECONNRESET, true); 2117 kill_all_requests(hsotg, &hsotg->eps[0], -ECONNRESET, true);
2072 2118
2073 /* it seems after a reset we can end up with a situation 2119 /* it seems after a reset we can end up with a situation
2074 * where the TXFIFO still has data in it... try flushing 2120 * where the TXFIFO still has data in it... the docs
2075 * it to remove anything that may still be in it. 2121 * suggest resetting all the fifos, so use the init_fifo
2122 * code to relayout and flush the fifos.
2076 */ 2123 */
2077 2124
2078 if (1) { 2125 s3c_hsotg_init_fifo(hsotg);
2079 writel(S3C_GRSTCTL_TxFNum(0) | S3C_GRSTCTL_TxFFlsh,
2080 hsotg->regs + S3C_GRSTCTL);
2081
2082 dev_info(hsotg->dev, "GNPTXSTS=%08x\n",
2083 readl(hsotg->regs + S3C_GNPTXSTS));
2084 }
2085 2126
2086 s3c_hsotg_enqueue_setup(hsotg); 2127 s3c_hsotg_enqueue_setup(hsotg);
2087 2128
@@ -2274,6 +2315,12 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2274 break; 2315 break;
2275 } 2316 }
2276 2317
2318 /* if the hardware has dedicated fifos, we must give each IN EP
2319 * a unique tx-fifo even if it is non-periodic.
2320 */
2321 if (dir_in && hsotg->dedicated_fifos)
2322 epctrl |= S3C_DxEPCTL_TxFNum(index);
2323
2277 /* for non control endpoints, set PID to D0 */ 2324 /* for non control endpoints, set PID to D0 */
2278 if (index) 2325 if (index)
2279 epctrl |= S3C_DxEPCTL_SetD0PID; 2326 epctrl |= S3C_DxEPCTL_SetD0PID;
@@ -2563,7 +2610,8 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2563 2610
2564 writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk | 2611 writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2565 S3C_DIEPMSK_INTknEPMisMsk | 2612 S3C_DIEPMSK_INTknEPMisMsk |
2566 S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk, 2613 S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk |
2614 ((hsotg->dedicated_fifos) ? S3C_DIEPMSK_TxFIFOEmpty : 0),
2567 hsotg->regs + S3C_DIEPMSK); 2615 hsotg->regs + S3C_DIEPMSK);
2568 2616
2569 /* don't need XferCompl, we get that from RXFIFO in slave mode. In 2617 /* don't need XferCompl, we get that from RXFIFO in slave mode. In
@@ -2732,7 +2780,7 @@ static void __devinit s3c_hsotg_initep(struct s3c_hsotg *hsotg,
2732 */ 2780 */
2733 2781
2734 ptxfifo = readl(hsotg->regs + S3C_DPTXFSIZn(epnum)); 2782 ptxfifo = readl(hsotg->regs + S3C_DPTXFSIZn(epnum));
2735 hs_ep->fifo_size = S3C_DPTXFSIZn_DPTxFSize_GET(ptxfifo); 2783 hs_ep->fifo_size = S3C_DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
2736 2784
2737 /* if we're using dma, we need to set the next-endpoint pointer 2785 /* if we're using dma, we need to set the next-endpoint pointer
2738 * to be something valid. 2786 * to be something valid.
@@ -2753,13 +2801,33 @@ static void __devinit s3c_hsotg_initep(struct s3c_hsotg *hsotg,
2753 */ 2801 */
2754static void s3c_hsotg_otgreset(struct s3c_hsotg *hsotg) 2802static void s3c_hsotg_otgreset(struct s3c_hsotg *hsotg)
2755{ 2803{
2756 u32 osc; 2804 struct clk *xusbxti;
2805 u32 pwr, osc;
2757 2806
2758 writel(0, S3C_PHYPWR); 2807 pwr = readl(S3C_PHYPWR);
2808 pwr &= ~0x19;
2809 writel(pwr, S3C_PHYPWR);
2759 mdelay(1); 2810 mdelay(1);
2760 2811
2761 osc = hsotg->plat->is_osc ? S3C_PHYCLK_EXT_OSC : 0; 2812 osc = hsotg->plat->is_osc ? S3C_PHYCLK_EXT_OSC : 0;
2762 2813
2814 xusbxti = clk_get(hsotg->dev, "xusbxti");
2815 if (xusbxti && !IS_ERR(xusbxti)) {
2816 switch (clk_get_rate(xusbxti)) {
2817 case 12*MHZ:
2818 osc |= S3C_PHYCLK_CLKSEL_12M;
2819 break;
2820 case 24*MHZ:
2821 osc |= S3C_PHYCLK_CLKSEL_24M;
2822 break;
2823 default:
2824 case 48*MHZ:
2825 /* default reference clock */
2826 break;
2827 }
2828 clk_put(xusbxti);
2829 }
2830
2763 writel(osc | 0x10, S3C_PHYCLK); 2831 writel(osc | 0x10, S3C_PHYCLK);
2764 2832
2765 /* issue a full set of resets to the otg and core */ 2833 /* issue a full set of resets to the otg and core */
@@ -2772,6 +2840,8 @@ static void s3c_hsotg_otgreset(struct s3c_hsotg *hsotg)
2772 2840
2773static void s3c_hsotg_init(struct s3c_hsotg *hsotg) 2841static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2774{ 2842{
2843 u32 cfg4;
2844
2775 /* unmask subset of endpoint interrupts */ 2845 /* unmask subset of endpoint interrupts */
2776 2846
2777 writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk | 2847 writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
@@ -2807,6 +2877,14 @@ static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2807 2877
2808 writel(using_dma(hsotg) ? S3C_GAHBCFG_DMAEn : 0x0, 2878 writel(using_dma(hsotg) ? S3C_GAHBCFG_DMAEn : 0x0,
2809 hsotg->regs + S3C_GAHBCFG); 2879 hsotg->regs + S3C_GAHBCFG);
2880
2881 /* check hardware configuration */
2882
2883 cfg4 = readl(hsotg->regs + 0x50);
2884 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
2885
2886 dev_info(hsotg->dev, "%s fifos\n",
2887 hsotg->dedicated_fifos ? "dedicated" : "shared");
2810} 2888}
2811 2889
2812static void s3c_hsotg_dump(struct s3c_hsotg *hsotg) 2890static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
@@ -3181,13 +3259,20 @@ static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3181 hsotg->dev = dev; 3259 hsotg->dev = dev;
3182 hsotg->plat = plat; 3260 hsotg->plat = plat;
3183 3261
3262 hsotg->clk = clk_get(&pdev->dev, "otg");
3263 if (IS_ERR(hsotg->clk)) {
3264 dev_err(dev, "cannot get otg clock\n");
3265 ret = -EINVAL;
3266 goto err_mem;
3267 }
3268
3184 platform_set_drvdata(pdev, hsotg); 3269 platform_set_drvdata(pdev, hsotg);
3185 3270
3186 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 3271 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3187 if (!res) { 3272 if (!res) {
3188 dev_err(dev, "cannot find register resource 0\n"); 3273 dev_err(dev, "cannot find register resource 0\n");
3189 ret = -EINVAL; 3274 ret = -EINVAL;
3190 goto err_mem; 3275 goto err_clk;
3191 } 3276 }
3192 3277
3193 hsotg->regs_res = request_mem_region(res->start, resource_size(res), 3278 hsotg->regs_res = request_mem_region(res->start, resource_size(res),
@@ -3195,7 +3280,7 @@ static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3195 if (!hsotg->regs_res) { 3280 if (!hsotg->regs_res) {
3196 dev_err(dev, "cannot reserve registers\n"); 3281 dev_err(dev, "cannot reserve registers\n");
3197 ret = -ENOENT; 3282 ret = -ENOENT;
3198 goto err_mem; 3283 goto err_clk;
3199 } 3284 }
3200 3285
3201 hsotg->regs = ioremap(res->start, resource_size(res)); 3286 hsotg->regs = ioremap(res->start, resource_size(res));
@@ -3248,6 +3333,8 @@ static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3248 3333
3249 /* reset the system */ 3334 /* reset the system */
3250 3335
3336 clk_enable(hsotg->clk);
3337
3251 s3c_hsotg_gate(pdev, true); 3338 s3c_hsotg_gate(pdev, true);
3252 3339
3253 s3c_hsotg_otgreset(hsotg); 3340 s3c_hsotg_otgreset(hsotg);
@@ -3271,7 +3358,8 @@ err_regs:
3271err_regs_res: 3358err_regs_res:
3272 release_resource(hsotg->regs_res); 3359 release_resource(hsotg->regs_res);
3273 kfree(hsotg->regs_res); 3360 kfree(hsotg->regs_res);
3274 3361err_clk:
3362 clk_put(hsotg->clk);
3275err_mem: 3363err_mem:
3276 kfree(hsotg); 3364 kfree(hsotg);
3277 return ret; 3365 return ret;
@@ -3293,6 +3381,9 @@ static int __devexit s3c_hsotg_remove(struct platform_device *pdev)
3293 3381
3294 s3c_hsotg_gate(pdev, false); 3382 s3c_hsotg_gate(pdev, false);
3295 3383
3384 clk_disable(hsotg->clk);
3385 clk_put(hsotg->clk);
3386
3296 kfree(hsotg); 3387 kfree(hsotg);
3297 return 0; 3388 return 0;
3298} 3389}
diff --git a/drivers/usb/gadget/serial.c b/drivers/usb/gadget/serial.c
index f46a60962da..b22eedbc7dc 100644
--- a/drivers/usb/gadget/serial.c
+++ b/drivers/usb/gadget/serial.c
@@ -137,7 +137,7 @@ MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
137 137
138/*-------------------------------------------------------------------------*/ 138/*-------------------------------------------------------------------------*/
139 139
140static int __init serial_bind_config(struct usb_configuration *c) 140static int __ref serial_bind_config(struct usb_configuration *c)
141{ 141{
142 unsigned i; 142 unsigned i;
143 int status = 0; 143 int status = 0;
@@ -161,7 +161,7 @@ static struct usb_configuration serial_config_driver = {
161 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 161 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
162}; 162};
163 163
164static int __init gs_bind(struct usb_composite_dev *cdev) 164static int __ref gs_bind(struct usb_composite_dev *cdev)
165{ 165{
166 int gcnum; 166 int gcnum;
167 struct usb_gadget *gadget = cdev->gadget; 167 struct usb_gadget *gadget = cdev->gadget;
diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
index 04c462ff0ea..484acfb1a7c 100644
--- a/drivers/usb/gadget/storage_common.c
+++ b/drivers/usb/gadget/storage_common.c
@@ -57,10 +57,12 @@
57#include <asm/unaligned.h> 57#include <asm/unaligned.h>
58 58
59 59
60/* Thanks to NetChip Technologies for donating this product ID. 60/*
61 * Thanks to NetChip Technologies for donating this product ID.
61 * 62 *
62 * DO NOT REUSE THESE IDs with any other driver!! Ever!! 63 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
63 * Instead: allocate your own, using normal USB-IF procedures. */ 64 * Instead: allocate your own, using normal USB-IF procedures.
65 */
64#define FSG_VENDOR_ID 0x0525 /* NetChip */ 66#define FSG_VENDOR_ID 0x0525 /* NetChip */
65#define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */ 67#define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
66 68
@@ -84,14 +86,27 @@
84#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args) 86#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
85#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args) 87#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
86 88
87/* Keep those macros in sync with thos in 89/*
88 * include/linux/ubs/composite.h or else GCC will complain. If they 90 * Keep those macros in sync with those in
91 * include/linux/usb/composite.h or else GCC will complain. If they
89 * are identical (the same names of arguments, white spaces in the 92 * are identical (the same names of arguments, white spaces in the
90 * same places) GCC will allow redefinition otherwise (even if some 93 * same places) GCC will allow redefinition otherwise (even if some
91 * white space is removed or added) warning will be issued. No 94 * white space is removed or added) warning will be issued.
92 * checking if those symbols is defined is performed because warning 95 *
93 * is desired when those macros were defined by someone else to mean 96 * Those macros are needed here because File Storage Gadget does not
94 * something else. */ 97 * include the composite.h header. For composite gadgets those macros
98 * are redundant since composite.h is included any way.
99 *
100 * One could check whether those macros are already defined (which
101 * would indicate composite.h had been included) or not (which would
102 * indicate we were in FSG) but this is not done because a warning is
103 * desired if definitions here differ from the ones in composite.h.
104 *
105 * We want the definitions to match and be the same in File Storage
106 * Gadget as well as Mass Storage Function (and so composite gadgets
107 * using MSF). If someone changes them in composite.h it will produce
108 * a warning in this file when building MSF.
109 */
95#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args) 110#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
96#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args) 111#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
97#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args) 112#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
@@ -269,6 +284,7 @@ struct fsg_lun {
269 unsigned int prevent_medium_removal:1; 284 unsigned int prevent_medium_removal:1;
270 unsigned int registered:1; 285 unsigned int registered:1;
271 unsigned int info_valid:1; 286 unsigned int info_valid:1;
287 unsigned int nofua:1;
272 288
273 u32 sense_data; 289 u32 sense_data;
274 u32 sense_data_info; 290 u32 sense_data_info;
@@ -313,9 +329,11 @@ struct fsg_buffhd {
313 enum fsg_buffer_state state; 329 enum fsg_buffer_state state;
314 struct fsg_buffhd *next; 330 struct fsg_buffhd *next;
315 331
316 /* The NetChip 2280 is faster, and handles some protocol faults 332 /*
333 * The NetChip 2280 is faster, and handles some protocol faults
317 * better, if we don't submit any short bulk-out read requests. 334 * better, if we don't submit any short bulk-out read requests.
318 * So we will record the intended request length here. */ 335 * So we will record the intended request length here.
336 */
319 unsigned int bulk_out_intended_length; 337 unsigned int bulk_out_intended_length;
320 338
321 struct usb_request *inreq; 339 struct usb_request *inreq;
@@ -395,8 +413,10 @@ fsg_intf_desc = {
395 .iInterface = FSG_STRING_INTERFACE, 413 .iInterface = FSG_STRING_INTERFACE,
396}; 414};
397 415
398/* Three full-speed endpoint descriptors: bulk-in, bulk-out, 416/*
399 * and interrupt-in. */ 417 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
418 * interrupt-in.
419 */
400 420
401static struct usb_endpoint_descriptor 421static struct usb_endpoint_descriptor
402fsg_fs_bulk_in_desc = { 422fsg_fs_bulk_in_desc = {
@@ -459,7 +479,7 @@ static struct usb_descriptor_header *fsg_fs_function[] = {
459 * 479 *
460 * That means alternate endpoint descriptors (bigger packets) 480 * That means alternate endpoint descriptors (bigger packets)
461 * and a "device qualifier" ... plus more construction options 481 * and a "device qualifier" ... plus more construction options
462 * for the config descriptor. 482 * for the configuration descriptor.
463 */ 483 */
464static struct usb_endpoint_descriptor 484static struct usb_endpoint_descriptor
465fsg_hs_bulk_in_desc = { 485fsg_hs_bulk_in_desc = {
@@ -547,8 +567,10 @@ static struct usb_gadget_strings fsg_stringtab = {
547 567
548 /*-------------------------------------------------------------------------*/ 568 /*-------------------------------------------------------------------------*/
549 569
550/* If the next two routines are called while the gadget is registered, 570/*
551 * the caller must own fsg->filesem for writing. */ 571 * If the next two routines are called while the gadget is registered,
572 * the caller must own fsg->filesem for writing.
573 */
552 574
553static int fsg_lun_open(struct fsg_lun *curlun, const char *filename) 575static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
554{ 576{
@@ -587,8 +609,10 @@ static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
587 goto out; 609 goto out;
588 } 610 }
589 611
590 /* If we can't read the file, it's no good. 612 /*
591 * If we can't write the file, use it read-only. */ 613 * If we can't read the file, it's no good.
614 * If we can't write the file, use it read-only.
615 */
592 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) { 616 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
593 LINFO(curlun, "file not readable: %s\n", filename); 617 LINFO(curlun, "file not readable: %s\n", filename);
594 goto out; 618 goto out;
@@ -646,8 +670,10 @@ static void fsg_lun_close(struct fsg_lun *curlun)
646 670
647/*-------------------------------------------------------------------------*/ 671/*-------------------------------------------------------------------------*/
648 672
649/* Sync the file data, don't bother with the metadata. 673/*
650 * This code was copied from fs/buffer.c:sys_fdatasync(). */ 674 * Sync the file data, don't bother with the metadata.
675 * This code was copied from fs/buffer.c:sys_fdatasync().
676 */
651static int fsg_lun_fsync_sub(struct fsg_lun *curlun) 677static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
652{ 678{
653 struct file *filp = curlun->filp; 679 struct file *filp = curlun->filp;
@@ -689,6 +715,14 @@ static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
689 : curlun->initially_ro); 715 : curlun->initially_ro);
690} 716}
691 717
718static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
719 char *buf)
720{
721 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
722
723 return sprintf(buf, "%u\n", curlun->nofua);
724}
725
692static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr, 726static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
693 char *buf) 727 char *buf)
694{ 728{
@@ -723,26 +757,47 @@ static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
723 ssize_t rc = count; 757 ssize_t rc = count;
724 struct fsg_lun *curlun = fsg_lun_from_dev(dev); 758 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
725 struct rw_semaphore *filesem = dev_get_drvdata(dev); 759 struct rw_semaphore *filesem = dev_get_drvdata(dev);
726 int i; 760 unsigned long ro;
727 761
728 if (sscanf(buf, "%d", &i) != 1) 762 if (strict_strtoul(buf, 2, &ro))
729 return -EINVAL; 763 return -EINVAL;
730 764
731 /* Allow the write-enable status to change only while the backing file 765 /*
732 * is closed. */ 766 * Allow the write-enable status to change only while the
767 * backing file is closed.
768 */
733 down_read(filesem); 769 down_read(filesem);
734 if (fsg_lun_is_open(curlun)) { 770 if (fsg_lun_is_open(curlun)) {
735 LDBG(curlun, "read-only status change prevented\n"); 771 LDBG(curlun, "read-only status change prevented\n");
736 rc = -EBUSY; 772 rc = -EBUSY;
737 } else { 773 } else {
738 curlun->ro = !!i; 774 curlun->ro = ro;
739 curlun->initially_ro = !!i; 775 curlun->initially_ro = ro;
740 LDBG(curlun, "read-only status set to %d\n", curlun->ro); 776 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
741 } 777 }
742 up_read(filesem); 778 up_read(filesem);
743 return rc; 779 return rc;
744} 780}
745 781
782static ssize_t fsg_store_nofua(struct device *dev,
783 struct device_attribute *attr,
784 const char *buf, size_t count)
785{
786 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
787 unsigned long nofua;
788
789 if (strict_strtoul(buf, 2, &nofua))
790 return -EINVAL;
791
792 /* Sync data when switching from async mode to sync */
793 if (!nofua && curlun->nofua)
794 fsg_lun_fsync_sub(curlun);
795
796 curlun->nofua = nofua;
797
798 return count;
799}
800
746static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr, 801static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
747 const char *buf, size_t count) 802 const char *buf, size_t count)
748{ 803{
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c
index 1da755a1c85..6bb876d6525 100644
--- a/drivers/usb/gadget/u_ether.c
+++ b/drivers/usb/gadget/u_ether.c
@@ -704,17 +704,6 @@ static char *host_addr;
704module_param(host_addr, charp, S_IRUGO); 704module_param(host_addr, charp, S_IRUGO);
705MODULE_PARM_DESC(host_addr, "Host Ethernet Address"); 705MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
706 706
707
708static u8 __init nibble(unsigned char c)
709{
710 if (isdigit(c))
711 return c - '0';
712 c = toupper(c);
713 if (isxdigit(c))
714 return 10 + c - 'A';
715 return 0;
716}
717
718static int get_ether_addr(const char *str, u8 *dev_addr) 707static int get_ether_addr(const char *str, u8 *dev_addr)
719{ 708{
720 if (str) { 709 if (str) {
@@ -725,8 +714,8 @@ static int get_ether_addr(const char *str, u8 *dev_addr)
725 714
726 if ((*str == '.') || (*str == ':')) 715 if ((*str == '.') || (*str == ':'))
727 str++; 716 str++;
728 num = nibble(*str++) << 4; 717 num = hex_to_bin(*str++) << 4;
729 num |= (nibble(*str++)); 718 num |= hex_to_bin(*str++);
730 dev_addr [i] = num; 719 dev_addr [i] = num;
731 } 720 }
732 if (is_valid_ether_addr(dev_addr)) 721 if (is_valid_ether_addr(dev_addr))
diff --git a/drivers/usb/gadget/u_serial.c b/drivers/usb/gadget/u_serial.c
index 3e8dcb5455e..01e5354a4c2 100644
--- a/drivers/usb/gadget/u_serial.c
+++ b/drivers/usb/gadget/u_serial.c
@@ -18,6 +18,7 @@
18/* #define VERBOSE_DEBUG */ 18/* #define VERBOSE_DEBUG */
19 19
20#include <linux/kernel.h> 20#include <linux/kernel.h>
21#include <linux/sched.h>
21#include <linux/interrupt.h> 22#include <linux/interrupt.h>
22#include <linux/device.h> 23#include <linux/device.h>
23#include <linux/delay.h> 24#include <linux/delay.h>
diff --git a/drivers/usb/gadget/webcam.c b/drivers/usb/gadget/webcam.c
index 288d21155ab..de1deb7a3c6 100644
--- a/drivers/usb/gadget/webcam.c
+++ b/drivers/usb/gadget/webcam.c
@@ -308,7 +308,7 @@ static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = {
308 * USB configuration 308 * USB configuration
309 */ 309 */
310 310
311static int __init 311static int __ref
312webcam_config_bind(struct usb_configuration *c) 312webcam_config_bind(struct usb_configuration *c)
313{ 313{
314 return uvc_bind_config(c, uvc_control_cls, uvc_fs_streaming_cls, 314 return uvc_bind_config(c, uvc_control_cls, uvc_fs_streaming_cls,
@@ -330,7 +330,7 @@ webcam_unbind(struct usb_composite_dev *cdev)
330 return 0; 330 return 0;
331} 331}
332 332
333static int __init 333static int __ref
334webcam_bind(struct usb_composite_dev *cdev) 334webcam_bind(struct usb_composite_dev *cdev)
335{ 335{
336 int ret; 336 int ret;
diff --git a/drivers/usb/gadget/zero.c b/drivers/usb/gadget/zero.c
index 807280d069f..cf353920bb1 100644
--- a/drivers/usb/gadget/zero.c
+++ b/drivers/usb/gadget/zero.c
@@ -264,7 +264,7 @@ static void zero_resume(struct usb_composite_dev *cdev)
264 264
265/*-------------------------------------------------------------------------*/ 265/*-------------------------------------------------------------------------*/
266 266
267static int __init zero_bind(struct usb_composite_dev *cdev) 267static int __ref zero_bind(struct usb_composite_dev *cdev)
268{ 268{
269 int gcnum; 269 int gcnum;
270 struct usb_gadget *gadget = cdev->gadget; 270 struct usb_gadget *gadget = cdev->gadget;
diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index f865be2276d..2d926cec072 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -72,8 +72,9 @@ config USB_EHCI_ROOT_HUB_TT
72 from ARC, and has since changed hands a few times. 72 from ARC, and has since changed hands a few times.
73 73
74config USB_EHCI_TT_NEWSCHED 74config USB_EHCI_TT_NEWSCHED
75 bool "Improved Transaction Translator scheduling (EXPERIMENTAL)" 75 bool "Improved Transaction Translator scheduling"
76 depends on USB_EHCI_HCD && EXPERIMENTAL 76 depends on USB_EHCI_HCD
77 default y
77 ---help--- 78 ---help---
78 This changes the periodic scheduling code to fill more of the low 79 This changes the periodic scheduling code to fill more of the low
79 and full speed bandwidth available from the Transaction Translator 80 and full speed bandwidth available from the Transaction Translator
@@ -84,9 +85,11 @@ config USB_EHCI_TT_NEWSCHED
84 If you have multiple periodic low/fullspeed devices connected to a 85 If you have multiple periodic low/fullspeed devices connected to a
85 highspeed USB hub which is connected to a highspeed USB Host 86 highspeed USB hub which is connected to a highspeed USB Host
86 Controller, and some of those devices will not work correctly 87 Controller, and some of those devices will not work correctly
87 (possibly due to "ENOSPC" or "-28" errors), say Y. 88 (possibly due to "ENOSPC" or "-28" errors), say Y. Conversely, if
89 you have only one such device and it doesn't work, you could try
90 saying N.
88 91
89 If unsure, say N. 92 If unsure, say Y.
90 93
91config USB_EHCI_BIG_ENDIAN_MMIO 94config USB_EHCI_BIG_ENDIAN_MMIO
92 bool 95 bool
diff --git a/drivers/usb/host/ehci-au1xxx.c b/drivers/usb/host/ehci-au1xxx.c
index faa61748db7..2baf8a84908 100644
--- a/drivers/usb/host/ehci-au1xxx.c
+++ b/drivers/usb/host/ehci-au1xxx.c
@@ -228,7 +228,7 @@ static int ehci_hcd_au1xxx_drv_suspend(struct device *dev)
228 * the root hub is either suspended or stopped. 228 * the root hub is either suspended or stopped.
229 */ 229 */
230 spin_lock_irqsave(&ehci->lock, flags); 230 spin_lock_irqsave(&ehci->lock, flags);
231 ehci_prepare_ports_for_controller_suspend(ehci); 231 ehci_prepare_ports_for_controller_suspend(ehci, device_may_wakeup(dev));
232 ehci_writel(ehci, 0, &ehci->regs->intr_enable); 232 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
233 (void)ehci_readl(ehci, &ehci->regs->intr_enable); 233 (void)ehci_readl(ehci, &ehci->regs->intr_enable);
234 234
diff --git a/drivers/usb/host/ehci-dbg.c b/drivers/usb/host/ehci-dbg.c
index 874d2000bf9..76b7fd2d838 100644
--- a/drivers/usb/host/ehci-dbg.c
+++ b/drivers/usb/host/ehci-dbg.c
@@ -98,13 +98,18 @@ static void dbg_hcc_params (struct ehci_hcd *ehci, char *label)
98 HCC_64BIT_ADDR(params) ? " 64 bit addr" : ""); 98 HCC_64BIT_ADDR(params) ? " 64 bit addr" : "");
99 } else { 99 } else {
100 ehci_dbg (ehci, 100 ehci_dbg (ehci,
101 "%s hcc_params %04x thresh %d uframes %s%s%s\n", 101 "%s hcc_params %04x thresh %d uframes %s%s%s%s%s%s%s\n",
102 label, 102 label,
103 params, 103 params,
104 HCC_ISOC_THRES(params), 104 HCC_ISOC_THRES(params),
105 HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024", 105 HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
106 HCC_CANPARK(params) ? " park" : "", 106 HCC_CANPARK(params) ? " park" : "",
107 HCC_64BIT_ADDR(params) ? " 64 bit addr" : ""); 107 HCC_64BIT_ADDR(params) ? " 64 bit addr" : "",
108 HCC_LPM(params) ? " LPM" : "",
109 HCC_PER_PORT_CHANGE_EVENT(params) ? " ppce" : "",
110 HCC_HW_PREFETCH(params) ? " hw prefetch" : "",
111 HCC_32FRAME_PERIODIC_LIST(params) ?
112 " 32 peridic list" : "");
108 } 113 }
109} 114}
110#else 115#else
@@ -191,8 +196,9 @@ static int __maybe_unused
191dbg_status_buf (char *buf, unsigned len, const char *label, u32 status) 196dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
192{ 197{
193 return scnprintf (buf, len, 198 return scnprintf (buf, len,
194 "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s", 199 "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s%s",
195 label, label [0] ? " " : "", status, 200 label, label [0] ? " " : "", status,
201 (status & STS_PPCE_MASK) ? " PPCE" : "",
196 (status & STS_ASS) ? " Async" : "", 202 (status & STS_ASS) ? " Async" : "",
197 (status & STS_PSS) ? " Periodic" : "", 203 (status & STS_PSS) ? " Periodic" : "",
198 (status & STS_RECL) ? " Recl" : "", 204 (status & STS_RECL) ? " Recl" : "",
@@ -210,8 +216,9 @@ static int __maybe_unused
210dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable) 216dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
211{ 217{
212 return scnprintf (buf, len, 218 return scnprintf (buf, len,
213 "%s%sintrenable %02x%s%s%s%s%s%s", 219 "%s%sintrenable %02x%s%s%s%s%s%s%s",
214 label, label [0] ? " " : "", enable, 220 label, label [0] ? " " : "", enable,
221 (enable & STS_PPCE_MASK) ? " PPCE" : "",
215 (enable & STS_IAA) ? " IAA" : "", 222 (enable & STS_IAA) ? " IAA" : "",
216 (enable & STS_FATAL) ? " FATAL" : "", 223 (enable & STS_FATAL) ? " FATAL" : "",
217 (enable & STS_FLR) ? " FLR" : "", 224 (enable & STS_FLR) ? " FLR" : "",
@@ -228,9 +235,15 @@ static int
228dbg_command_buf (char *buf, unsigned len, const char *label, u32 command) 235dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
229{ 236{
230 return scnprintf (buf, len, 237 return scnprintf (buf, len,
231 "%s%scommand %06x %s=%d ithresh=%d%s%s%s%s period=%s%s %s", 238 "%s%scommand %07x %s%s%s%s%s%s=%d ithresh=%d%s%s%s%s "
239 "period=%s%s %s",
232 label, label [0] ? " " : "", command, 240 label, label [0] ? " " : "", command,
233 (command & CMD_PARK) ? "park" : "(park)", 241 (command & CMD_HIRD) ? " HIRD" : "",
242 (command & CMD_PPCEE) ? " PPCEE" : "",
243 (command & CMD_FSP) ? " FSP" : "",
244 (command & CMD_ASPE) ? " ASPE" : "",
245 (command & CMD_PSPE) ? " PSPE" : "",
246 (command & CMD_PARK) ? " park" : "(park)",
234 CMD_PARK_CNT (command), 247 CMD_PARK_CNT (command),
235 (command >> 16) & 0x3f, 248 (command >> 16) & 0x3f,
236 (command & CMD_LRESET) ? " LReset" : "", 249 (command & CMD_LRESET) ? " LReset" : "",
@@ -257,11 +270,22 @@ dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
257 } 270 }
258 271
259 return scnprintf (buf, len, 272 return scnprintf (buf, len,
260 "%s%sport %d status %06x%s%s sig=%s%s%s%s%s%s%s%s%s%s", 273 "%s%sport:%d status %06x %d %s%s%s%s%s%s "
274 "sig=%s%s%s%s%s%s%s%s%s%s%s",
261 label, label [0] ? " " : "", port, status, 275 label, label [0] ? " " : "", port, status,
276 status>>25,/*device address */
277 (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_ACK ?
278 " ACK" : "",
279 (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_NYET ?
280 " NYET" : "",
281 (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_STALL ?
282 " STALL" : "",
283 (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_ERR ?
284 " ERR" : "",
262 (status & PORT_POWER) ? " POWER" : "", 285 (status & PORT_POWER) ? " POWER" : "",
263 (status & PORT_OWNER) ? " OWNER" : "", 286 (status & PORT_OWNER) ? " OWNER" : "",
264 sig, 287 sig,
288 (status & PORT_LPM) ? " LPM" : "",
265 (status & PORT_RESET) ? " RESET" : "", 289 (status & PORT_RESET) ? " RESET" : "",
266 (status & PORT_SUSPEND) ? " SUSPEND" : "", 290 (status & PORT_SUSPEND) ? " SUSPEND" : "",
267 (status & PORT_RESUME) ? " RESUME" : "", 291 (status & PORT_RESUME) ? " RESUME" : "",
@@ -330,6 +354,13 @@ static int debug_async_open(struct inode *, struct file *);
330static int debug_periodic_open(struct inode *, struct file *); 354static int debug_periodic_open(struct inode *, struct file *);
331static int debug_registers_open(struct inode *, struct file *); 355static int debug_registers_open(struct inode *, struct file *);
332static int debug_async_open(struct inode *, struct file *); 356static int debug_async_open(struct inode *, struct file *);
357static int debug_lpm_open(struct inode *, struct file *);
358static ssize_t debug_lpm_read(struct file *file, char __user *user_buf,
359 size_t count, loff_t *ppos);
360static ssize_t debug_lpm_write(struct file *file, const char __user *buffer,
361 size_t count, loff_t *ppos);
362static int debug_lpm_close(struct inode *inode, struct file *file);
363
333static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*); 364static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
334static int debug_close(struct inode *, struct file *); 365static int debug_close(struct inode *, struct file *);
335 366
@@ -351,6 +382,13 @@ static const struct file_operations debug_registers_fops = {
351 .read = debug_output, 382 .read = debug_output,
352 .release = debug_close, 383 .release = debug_close,
353}; 384};
385static const struct file_operations debug_lpm_fops = {
386 .owner = THIS_MODULE,
387 .open = debug_lpm_open,
388 .read = debug_lpm_read,
389 .write = debug_lpm_write,
390 .release = debug_lpm_close,
391};
354 392
355static struct dentry *ehci_debug_root; 393static struct dentry *ehci_debug_root;
356 394
@@ -674,7 +712,7 @@ static ssize_t fill_registers_buffer(struct debug_buffer *buf)
674 712
675 spin_lock_irqsave (&ehci->lock, flags); 713 spin_lock_irqsave (&ehci->lock, flags);
676 714
677 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { 715 if (!HCD_HW_ACCESSIBLE(hcd)) {
678 size = scnprintf (next, size, 716 size = scnprintf (next, size,
679 "bus %s, device %s\n" 717 "bus %s, device %s\n"
680 "%s\n" 718 "%s\n"
@@ -917,51 +955,127 @@ static int debug_registers_open(struct inode *inode, struct file *file)
917 return file->private_data ? 0 : -ENOMEM; 955 return file->private_data ? 0 : -ENOMEM;
918} 956}
919 957
958static int debug_lpm_open(struct inode *inode, struct file *file)
959{
960 file->private_data = inode->i_private;
961 return 0;
962}
963
964static int debug_lpm_close(struct inode *inode, struct file *file)
965{
966 return 0;
967}
968
969static ssize_t debug_lpm_read(struct file *file, char __user *user_buf,
970 size_t count, loff_t *ppos)
971{
972 /* TODO: show lpm stats */
973 return 0;
974}
975
976static ssize_t debug_lpm_write(struct file *file, const char __user *user_buf,
977 size_t count, loff_t *ppos)
978{
979 struct usb_hcd *hcd;
980 struct ehci_hcd *ehci;
981 char buf[50];
982 size_t len;
983 u32 temp;
984 unsigned long port;
985 u32 __iomem *portsc ;
986 u32 params;
987
988 hcd = bus_to_hcd(file->private_data);
989 ehci = hcd_to_ehci(hcd);
990
991 len = min(count, sizeof(buf) - 1);
992 if (copy_from_user(buf, user_buf, len))
993 return -EFAULT;
994 buf[len] = '\0';
995 if (len > 0 && buf[len - 1] == '\n')
996 buf[len - 1] = '\0';
997
998 if (strncmp(buf, "enable", 5) == 0) {
999 if (strict_strtoul(buf + 7, 10, &port))
1000 return -EINVAL;
1001 params = ehci_readl(ehci, &ehci->caps->hcs_params);
1002 if (port > HCS_N_PORTS(params)) {
1003 ehci_dbg(ehci, "ERR: LPM on bad port %lu\n", port);
1004 return -ENODEV;
1005 }
1006 portsc = &ehci->regs->port_status[port-1];
1007 temp = ehci_readl(ehci, portsc);
1008 if (!(temp & PORT_DEV_ADDR)) {
1009 ehci_dbg(ehci, "LPM: no device attached\n");
1010 return -ENODEV;
1011 }
1012 temp |= PORT_LPM;
1013 ehci_writel(ehci, temp, portsc);
1014 printk(KERN_INFO "force enable LPM for port %lu\n", port);
1015 } else if (strncmp(buf, "hird=", 5) == 0) {
1016 unsigned long hird;
1017 if (strict_strtoul(buf + 5, 16, &hird))
1018 return -EINVAL;
1019 printk(KERN_INFO "setting hird %s %lu\n", buf + 6, hird);
1020 temp = ehci_readl(ehci, &ehci->regs->command);
1021 temp &= ~CMD_HIRD;
1022 temp |= hird << 24;
1023 ehci_writel(ehci, temp, &ehci->regs->command);
1024 } else if (strncmp(buf, "disable", 7) == 0) {
1025 if (strict_strtoul(buf + 8, 10, &port))
1026 return -EINVAL;
1027 params = ehci_readl(ehci, &ehci->caps->hcs_params);
1028 if (port > HCS_N_PORTS(params)) {
1029 ehci_dbg(ehci, "ERR: LPM off bad port %lu\n", port);
1030 return -ENODEV;
1031 }
1032 portsc = &ehci->regs->port_status[port-1];
1033 temp = ehci_readl(ehci, portsc);
1034 if (!(temp & PORT_DEV_ADDR)) {
1035 ehci_dbg(ehci, "ERR: no device attached\n");
1036 return -ENODEV;
1037 }
1038 temp &= ~PORT_LPM;
1039 ehci_writel(ehci, temp, portsc);
1040 printk(KERN_INFO "disabled LPM for port %lu\n", port);
1041 } else
1042 return -EOPNOTSUPP;
1043 return count;
1044}
1045
920static inline void create_debug_files (struct ehci_hcd *ehci) 1046static inline void create_debug_files (struct ehci_hcd *ehci)
921{ 1047{
922 struct usb_bus *bus = &ehci_to_hcd(ehci)->self; 1048 struct usb_bus *bus = &ehci_to_hcd(ehci)->self;
923 1049
924 ehci->debug_dir = debugfs_create_dir(bus->bus_name, ehci_debug_root); 1050 ehci->debug_dir = debugfs_create_dir(bus->bus_name, ehci_debug_root);
925 if (!ehci->debug_dir) 1051 if (!ehci->debug_dir)
926 goto dir_error; 1052 return;
927 1053
928 ehci->debug_async = debugfs_create_file("async", S_IRUGO, 1054 if (!debugfs_create_file("async", S_IRUGO, ehci->debug_dir, bus,
929 ehci->debug_dir, bus, 1055 &debug_async_fops))
930 &debug_async_fops); 1056 goto file_error;
931 if (!ehci->debug_async) 1057
932 goto async_error; 1058 if (!debugfs_create_file("periodic", S_IRUGO, ehci->debug_dir, bus,
933 1059 &debug_periodic_fops))
934 ehci->debug_periodic = debugfs_create_file("periodic", S_IRUGO, 1060 goto file_error;
935 ehci->debug_dir, bus, 1061
936 &debug_periodic_fops); 1062 if (!debugfs_create_file("registers", S_IRUGO, ehci->debug_dir, bus,
937 if (!ehci->debug_periodic) 1063 &debug_registers_fops))
938 goto periodic_error; 1064 goto file_error;
939 1065
940 ehci->debug_registers = debugfs_create_file("registers", S_IRUGO, 1066 if (!debugfs_create_file("lpm", S_IRUGO|S_IWUGO, ehci->debug_dir, bus,
941 ehci->debug_dir, bus, 1067 &debug_lpm_fops))
942 &debug_registers_fops); 1068 goto file_error;
943 if (!ehci->debug_registers) 1069
944 goto registers_error;
945 return; 1070 return;
946 1071
947registers_error: 1072file_error:
948 debugfs_remove(ehci->debug_periodic); 1073 debugfs_remove_recursive(ehci->debug_dir);
949periodic_error:
950 debugfs_remove(ehci->debug_async);
951async_error:
952 debugfs_remove(ehci->debug_dir);
953dir_error:
954 ehci->debug_periodic = NULL;
955 ehci->debug_async = NULL;
956 ehci->debug_dir = NULL;
957} 1074}
958 1075
959static inline void remove_debug_files (struct ehci_hcd *ehci) 1076static inline void remove_debug_files (struct ehci_hcd *ehci)
960{ 1077{
961 debugfs_remove(ehci->debug_registers); 1078 debugfs_remove_recursive(ehci->debug_dir);
962 debugfs_remove(ehci->debug_periodic);
963 debugfs_remove(ehci->debug_async);
964 debugfs_remove(ehci->debug_dir);
965} 1079}
966 1080
967#endif /* STUB_DEBUG_FILES */ 1081#endif /* STUB_DEBUG_FILES */
diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
index 5cd967d2893..a416421abfa 100644
--- a/drivers/usb/host/ehci-fsl.c
+++ b/drivers/usb/host/ehci-fsl.c
@@ -313,7 +313,8 @@ static int ehci_fsl_drv_suspend(struct device *dev)
313 struct ehci_fsl *ehci_fsl = hcd_to_ehci_fsl(hcd); 313 struct ehci_fsl *ehci_fsl = hcd_to_ehci_fsl(hcd);
314 void __iomem *non_ehci = hcd->regs; 314 void __iomem *non_ehci = hcd->regs;
315 315
316 ehci_prepare_ports_for_controller_suspend(hcd_to_ehci(hcd)); 316 ehci_prepare_ports_for_controller_suspend(hcd_to_ehci(hcd),
317 device_may_wakeup(dev));
317 if (!fsl_deep_sleep()) 318 if (!fsl_deep_sleep())
318 return 0; 319 return 0;
319 320
diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index 6fcffe15a00..ac0f7a4b034 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -36,6 +36,7 @@
36#include <linux/dma-mapping.h> 36#include <linux/dma-mapping.h>
37#include <linux/debugfs.h> 37#include <linux/debugfs.h>
38#include <linux/slab.h> 38#include <linux/slab.h>
39#include <linux/uaccess.h>
39 40
40#include <asm/byteorder.h> 41#include <asm/byteorder.h>
41#include <asm/io.h> 42#include <asm/io.h>
@@ -78,7 +79,13 @@ static const char hcd_name [] = "ehci_hcd";
78#define EHCI_TUNE_RL_TT 0 79#define EHCI_TUNE_RL_TT 0
79#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */ 80#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
80#define EHCI_TUNE_MULT_TT 1 81#define EHCI_TUNE_MULT_TT 1
81#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */ 82/*
83 * Some drivers think it's safe to schedule isochronous transfers more than
84 * 256 ms into the future (partly as a result of an old bug in the scheduling
85 * code). In an attempt to avoid trouble, we will use a minimum scheduling
86 * length of 512 frames instead of 256.
87 */
88#define EHCI_TUNE_FLS 1 /* (medium) 512-frame schedule */
82 89
83#define EHCI_IAA_MSECS 10 /* arbitrary */ 90#define EHCI_IAA_MSECS 10 /* arbitrary */
84#define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */ 91#define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
@@ -100,6 +107,11 @@ static int ignore_oc = 0;
100module_param (ignore_oc, bool, S_IRUGO); 107module_param (ignore_oc, bool, S_IRUGO);
101MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications"); 108MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
102 109
110/* for link power management(LPM) feature */
111static unsigned int hird;
112module_param(hird, int, S_IRUGO);
113MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us\n");
114
103#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT) 115#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
104 116
105/*-------------------------------------------------------------------------*/ 117/*-------------------------------------------------------------------------*/
@@ -304,6 +316,7 @@ static void end_unlink_async(struct ehci_hcd *ehci);
304static void ehci_work(struct ehci_hcd *ehci); 316static void ehci_work(struct ehci_hcd *ehci);
305 317
306#include "ehci-hub.c" 318#include "ehci-hub.c"
319#include "ehci-lpm.c"
307#include "ehci-mem.c" 320#include "ehci-mem.c"
308#include "ehci-q.c" 321#include "ehci-q.c"
309#include "ehci-sched.c" 322#include "ehci-sched.c"
@@ -577,6 +590,11 @@ static int ehci_init(struct usb_hcd *hcd)
577 if (log2_irq_thresh < 0 || log2_irq_thresh > 6) 590 if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
578 log2_irq_thresh = 0; 591 log2_irq_thresh = 0;
579 temp = 1 << (16 + log2_irq_thresh); 592 temp = 1 << (16 + log2_irq_thresh);
593 if (HCC_PER_PORT_CHANGE_EVENT(hcc_params)) {
594 ehci->has_ppcd = 1;
595 ehci_dbg(ehci, "enable per-port change event\n");
596 temp |= CMD_PPCEE;
597 }
580 if (HCC_CANPARK(hcc_params)) { 598 if (HCC_CANPARK(hcc_params)) {
581 /* HW default park == 3, on hardware that supports it (like 599 /* HW default park == 3, on hardware that supports it (like
582 * NVidia and ALI silicon), maximizes throughput on the async 600 * NVidia and ALI silicon), maximizes throughput on the async
@@ -603,10 +621,22 @@ static int ehci_init(struct usb_hcd *hcd)
603 default: BUG(); 621 default: BUG();
604 } 622 }
605 } 623 }
624 if (HCC_LPM(hcc_params)) {
625 /* support link power management EHCI 1.1 addendum */
626 ehci_dbg(ehci, "support lpm\n");
627 ehci->has_lpm = 1;
628 if (hird > 0xf) {
629 ehci_dbg(ehci, "hird %d invalid, use default 0",
630 hird);
631 hird = 0;
632 }
633 temp |= hird << 24;
634 }
606 ehci->command = temp; 635 ehci->command = temp;
607 636
608 /* Accept arbitrarily long scatter-gather lists */ 637 /* Accept arbitrarily long scatter-gather lists */
609 hcd->self.sg_tablesize = ~0; 638 if (!(hcd->driver->flags & HCD_LOCAL_MEM))
639 hcd->self.sg_tablesize = ~0;
610 return 0; 640 return 0;
611} 641}
612 642
@@ -619,7 +649,6 @@ static int ehci_run (struct usb_hcd *hcd)
619 u32 hcc_params; 649 u32 hcc_params;
620 650
621 hcd->uses_new_polling = 1; 651 hcd->uses_new_polling = 1;
622 hcd->poll_rh = 0;
623 652
624 /* EHCI spec section 4.1 */ 653 /* EHCI spec section 4.1 */
625 if ((retval = ehci_reset(ehci)) != 0) { 654 if ((retval = ehci_reset(ehci)) != 0) {
@@ -764,6 +793,7 @@ static irqreturn_t ehci_irq (struct usb_hcd *hcd)
764 /* remote wakeup [4.3.1] */ 793 /* remote wakeup [4.3.1] */
765 if (status & STS_PCD) { 794 if (status & STS_PCD) {
766 unsigned i = HCS_N_PORTS (ehci->hcs_params); 795 unsigned i = HCS_N_PORTS (ehci->hcs_params);
796 u32 ppcd = 0;
767 797
768 /* kick root hub later */ 798 /* kick root hub later */
769 pcd_status = status; 799 pcd_status = status;
@@ -772,9 +802,18 @@ static irqreturn_t ehci_irq (struct usb_hcd *hcd)
772 if (!(cmd & CMD_RUN)) 802 if (!(cmd & CMD_RUN))
773 usb_hcd_resume_root_hub(hcd); 803 usb_hcd_resume_root_hub(hcd);
774 804
805 /* get per-port change detect bits */
806 if (ehci->has_ppcd)
807 ppcd = status >> 16;
808
775 while (i--) { 809 while (i--) {
776 int pstatus = ehci_readl(ehci, 810 int pstatus;
777 &ehci->regs->port_status [i]); 811
812 /* leverage per-port change bits feature */
813 if (ehci->has_ppcd && !(ppcd & (1 << i)))
814 continue;
815 pstatus = ehci_readl(ehci,
816 &ehci->regs->port_status[i]);
778 817
779 if (pstatus & PORT_OWNER) 818 if (pstatus & PORT_OWNER)
780 continue; 819 continue;
diff --git a/drivers/usb/host/ehci-hub.c b/drivers/usb/host/ehci-hub.c
index e7d3d8def28..796ea0c8900 100644
--- a/drivers/usb/host/ehci-hub.c
+++ b/drivers/usb/host/ehci-hub.c
@@ -107,7 +107,7 @@ static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
107} 107}
108 108
109static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci, 109static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
110 bool suspending) 110 bool suspending, bool do_wakeup)
111{ 111{
112 int port; 112 int port;
113 u32 temp; 113 u32 temp;
@@ -117,8 +117,7 @@ static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
117 * when the controller is suspended or resumed. In all other 117 * when the controller is suspended or resumed. In all other
118 * cases they don't need to be changed. 118 * cases they don't need to be changed.
119 */ 119 */
120 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || 120 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
121 device_may_wakeup(ehci_to_hcd(ehci)->self.controller))
122 return; 121 return;
123 122
124 /* clear phy low-power mode before changing wakeup flags */ 123 /* clear phy low-power mode before changing wakeup flags */
@@ -167,6 +166,10 @@ static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
167 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg); 166 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
168 } 167 }
169 } 168 }
169
170 /* Does the root hub have a port wakeup pending? */
171 if (!suspending && (ehci_readl(ehci, &ehci->regs->status) & STS_PCD))
172 usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
170} 173}
171 174
172static int ehci_bus_suspend (struct usb_hcd *hcd) 175static int ehci_bus_suspend (struct usb_hcd *hcd)
@@ -316,7 +319,7 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
316 if (time_before (jiffies, ehci->next_statechange)) 319 if (time_before (jiffies, ehci->next_statechange))
317 msleep(5); 320 msleep(5);
318 spin_lock_irq (&ehci->lock); 321 spin_lock_irq (&ehci->lock);
319 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { 322 if (!HCD_HW_ACCESSIBLE(hcd)) {
320 spin_unlock_irq(&ehci->lock); 323 spin_unlock_irq(&ehci->lock);
321 return -ESHUTDOWN; 324 return -ESHUTDOWN;
322 } 325 }
@@ -603,6 +606,7 @@ ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
603 u32 mask; 606 u32 mask;
604 int ports, i, retval = 1; 607 int ports, i, retval = 1;
605 unsigned long flags; 608 unsigned long flags;
609 u32 ppcd = 0;
606 610
607 /* if !USB_SUSPEND, root hub timers won't get shut down ... */ 611 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
608 if (!HC_IS_RUNNING(hcd->state)) 612 if (!HC_IS_RUNNING(hcd->state))
@@ -632,7 +636,15 @@ ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
632 636
633 /* port N changes (bit N)? */ 637 /* port N changes (bit N)? */
634 spin_lock_irqsave (&ehci->lock, flags); 638 spin_lock_irqsave (&ehci->lock, flags);
639
640 /* get per-port change detect bits */
641 if (ehci->has_ppcd)
642 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
643
635 for (i = 0; i < ports; i++) { 644 for (i = 0; i < ports; i++) {
645 /* leverage per-port change bits feature */
646 if (ehci->has_ppcd && !(ppcd & (1 << i)))
647 continue;
636 temp = ehci_readl(ehci, &ehci->regs->port_status [i]); 648 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
637 649
638 /* 650 /*
@@ -790,6 +802,11 @@ static int ehci_hub_control (
790 status_reg); 802 status_reg);
791 break; 803 break;
792 case USB_PORT_FEAT_C_CONNECTION: 804 case USB_PORT_FEAT_C_CONNECTION:
805 if (ehci->has_lpm) {
806 /* clear PORTSC bits on disconnect */
807 temp &= ~PORT_LPM;
808 temp &= ~PORT_DEV_ADDR;
809 }
793 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC, 810 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
794 status_reg); 811 status_reg);
795 break; 812 break;
diff --git a/drivers/usb/host/ehci-lpm.c b/drivers/usb/host/ehci-lpm.c
new file mode 100644
index 00000000000..b4d4d63c13e
--- /dev/null
+++ b/drivers/usb/host/ehci-lpm.c
@@ -0,0 +1,83 @@
1/* ehci-lpm.c EHCI HCD LPM support code
2 * Copyright (c) 2008 - 2010, Intel Corporation.
3 * Author: Jacob Pan <jacob.jun.pan@intel.com>
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 version 2 as
7 * published by the Free Software Foundation.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17*/
18
19/* this file is part of ehci-hcd.c */
20static int ehci_lpm_set_da(struct ehci_hcd *ehci, int dev_addr, int port_num)
21{
22 u32 __iomem portsc;
23
24 ehci_dbg(ehci, "set dev address %d for port %d\n", dev_addr, port_num);
25 if (port_num > HCS_N_PORTS(ehci->hcs_params)) {
26 ehci_dbg(ehci, "invalid port number %d\n", port_num);
27 return -ENODEV;
28 }
29 portsc = ehci_readl(ehci, &ehci->regs->port_status[port_num-1]);
30 portsc &= ~PORT_DEV_ADDR;
31 portsc |= dev_addr<<25;
32 ehci_writel(ehci, portsc, &ehci->regs->port_status[port_num-1]);
33 return 0;
34}
35
36/*
37 * this function is used to check if the device support LPM
38 * if yes, mark the PORTSC register with PORT_LPM bit
39 */
40static int ehci_lpm_check(struct ehci_hcd *ehci, int port)
41{
42 u32 __iomem *portsc ;
43 u32 val32;
44 int retval;
45
46 portsc = &ehci->regs->port_status[port-1];
47 val32 = ehci_readl(ehci, portsc);
48 if (!(val32 & PORT_DEV_ADDR)) {
49 ehci_dbg(ehci, "LPM: no device attached\n");
50 return -ENODEV;
51 }
52 val32 |= PORT_LPM;
53 ehci_writel(ehci, val32, portsc);
54 msleep(5);
55 val32 |= PORT_SUSPEND;
56 ehci_dbg(ehci, "Sending LPM 0x%08x to port %d\n", val32, port);
57 ehci_writel(ehci, val32, portsc);
58 /* wait for ACK */
59 msleep(10);
60 retval = handshake(ehci, &ehci->regs->port_status[port-1], PORT_SSTS,
61 PORTSC_SUSPEND_STS_ACK, 125);
62 dbg_port(ehci, "LPM", port, val32);
63 if (retval != -ETIMEDOUT) {
64 ehci_dbg(ehci, "LPM: device ACK for LPM\n");
65 val32 |= PORT_LPM;
66 /*
67 * now device should be in L1 sleep, let's wake up the device
68 * so that we can complete enumeration.
69 */
70 ehci_writel(ehci, val32, portsc);
71 msleep(10);
72 val32 |= PORT_RESUME;
73 ehci_writel(ehci, val32, portsc);
74 } else {
75 ehci_dbg(ehci, "LPM: device does not ACK, disable LPM %d\n",
76 retval);
77 val32 &= ~PORT_LPM;
78 retval = -ETIMEDOUT;
79 ehci_writel(ehci, val32, portsc);
80 }
81
82 return retval;
83}
diff --git a/drivers/usb/host/ehci-omap.c b/drivers/usb/host/ehci-omap.c
index 5450e628157..116ae280053 100644
--- a/drivers/usb/host/ehci-omap.c
+++ b/drivers/usb/host/ehci-omap.c
@@ -38,6 +38,7 @@
38#include <linux/gpio.h> 38#include <linux/gpio.h>
39#include <linux/regulator/consumer.h> 39#include <linux/regulator/consumer.h>
40#include <linux/slab.h> 40#include <linux/slab.h>
41#include <linux/usb/ulpi.h>
41#include <plat/usb.h> 42#include <plat/usb.h>
42 43
43/* 44/*
@@ -236,6 +237,35 @@ static void omap_usb_utmi_init(struct ehci_hcd_omap *omap, u8 tll_channel_mask)
236 237
237/*-------------------------------------------------------------------------*/ 238/*-------------------------------------------------------------------------*/
238 239
240static void omap_ehci_soft_phy_reset(struct ehci_hcd_omap *omap, u8 port)
241{
242 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
243 unsigned reg = 0;
244
245 reg = ULPI_FUNC_CTRL_RESET
246 /* FUNCTION_CTRL_SET register */
247 | (ULPI_SET(ULPI_FUNC_CTRL) << EHCI_INSNREG05_ULPI_REGADD_SHIFT)
248 /* Write */
249 | (2 << EHCI_INSNREG05_ULPI_OPSEL_SHIFT)
250 /* PORTn */
251 | ((port + 1) << EHCI_INSNREG05_ULPI_PORTSEL_SHIFT)
252 /* start ULPI access*/
253 | (1 << EHCI_INSNREG05_ULPI_CONTROL_SHIFT);
254
255 ehci_omap_writel(omap->ehci_base, EHCI_INSNREG05_ULPI, reg);
256
257 /* Wait for ULPI access completion */
258 while ((ehci_omap_readl(omap->ehci_base, EHCI_INSNREG05_ULPI)
259 & (1 << EHCI_INSNREG05_ULPI_CONTROL_SHIFT))) {
260 cpu_relax();
261
262 if (time_after(jiffies, timeout)) {
263 dev_dbg(omap->dev, "phy reset operation timed out\n");
264 break;
265 }
266 }
267}
268
239/* omap_start_ehc 269/* omap_start_ehc
240 * - Start the TI USBHOST controller 270 * - Start the TI USBHOST controller
241 */ 271 */
@@ -425,6 +455,12 @@ static int omap_start_ehc(struct ehci_hcd_omap *omap, struct usb_hcd *hcd)
425 gpio_set_value(omap->reset_gpio_port[1], 1); 455 gpio_set_value(omap->reset_gpio_port[1], 1);
426 } 456 }
427 457
458 /* Soft reset the PHY using PHY reset command over ULPI */
459 if (omap->port_mode[0] == EHCI_HCD_OMAP_MODE_PHY)
460 omap_ehci_soft_phy_reset(omap, 0);
461 if (omap->port_mode[1] == EHCI_HCD_OMAP_MODE_PHY)
462 omap_ehci_soft_phy_reset(omap, 1);
463
428 return 0; 464 return 0;
429 465
430err_sys_status: 466err_sys_status:
diff --git a/drivers/usb/host/ehci-pci.c b/drivers/usb/host/ehci-pci.c
index d43d176161a..58b72d741d9 100644
--- a/drivers/usb/host/ehci-pci.c
+++ b/drivers/usb/host/ehci-pci.c
@@ -114,6 +114,7 @@ static int ehci_pci_setup(struct usb_hcd *hcd)
114 break; 114 break;
115 case PCI_VENDOR_ID_INTEL: 115 case PCI_VENDOR_ID_INTEL:
116 ehci->need_io_watchdog = 0; 116 ehci->need_io_watchdog = 0;
117 ehci->fs_i_thresh = 1;
117 if (pdev->device == 0x27cc) { 118 if (pdev->device == 0x27cc) {
118 ehci->broken_periodic = 1; 119 ehci->broken_periodic = 1;
119 ehci_info(ehci, "using broken periodic workaround\n"); 120 ehci_info(ehci, "using broken periodic workaround\n");
@@ -277,7 +278,7 @@ done:
277 * Also they depend on separate root hub suspend/resume. 278 * Also they depend on separate root hub suspend/resume.
278 */ 279 */
279 280
280static int ehci_pci_suspend(struct usb_hcd *hcd) 281static int ehci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
281{ 282{
282 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 283 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
283 unsigned long flags; 284 unsigned long flags;
@@ -291,7 +292,7 @@ static int ehci_pci_suspend(struct usb_hcd *hcd)
291 * the root hub is either suspended or stopped. 292 * the root hub is either suspended or stopped.
292 */ 293 */
293 spin_lock_irqsave (&ehci->lock, flags); 294 spin_lock_irqsave (&ehci->lock, flags);
294 ehci_prepare_ports_for_controller_suspend(ehci); 295 ehci_prepare_ports_for_controller_suspend(ehci, do_wakeup);
295 ehci_writel(ehci, 0, &ehci->regs->intr_enable); 296 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
296 (void)ehci_readl(ehci, &ehci->regs->intr_enable); 297 (void)ehci_readl(ehci, &ehci->regs->intr_enable);
297 298
@@ -361,6 +362,22 @@ static int ehci_pci_resume(struct usb_hcd *hcd, bool hibernated)
361} 362}
362#endif 363#endif
363 364
365static int ehci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
366{
367 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
368 int rc = 0;
369
370 if (!udev->parent) /* udev is root hub itself, impossible */
371 rc = -1;
372 /* we only support lpm device connected to root hub yet */
373 if (ehci->has_lpm && !udev->parent->parent) {
374 rc = ehci_lpm_set_da(ehci, udev->devnum, udev->portnum);
375 if (!rc)
376 rc = ehci_lpm_check(ehci, udev->portnum);
377 }
378 return rc;
379}
380
364static const struct hc_driver ehci_pci_hc_driver = { 381static const struct hc_driver ehci_pci_hc_driver = {
365 .description = hcd_name, 382 .description = hcd_name,
366 .product_desc = "EHCI Host Controller", 383 .product_desc = "EHCI Host Controller",
@@ -407,6 +424,11 @@ static const struct hc_driver ehci_pci_hc_driver = {
407 .relinquish_port = ehci_relinquish_port, 424 .relinquish_port = ehci_relinquish_port,
408 .port_handed_over = ehci_port_handed_over, 425 .port_handed_over = ehci_port_handed_over,
409 426
427 /*
428 * call back when device connected and addressed
429 */
430 .update_device = ehci_update_device,
431
410 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete, 432 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
411}; 433};
412 434
diff --git a/drivers/usb/host/ehci-q.c b/drivers/usb/host/ehci-q.c
index 11a79c4f4a9..233c288e3f9 100644
--- a/drivers/usb/host/ehci-q.c
+++ b/drivers/usb/host/ehci-q.c
@@ -1126,8 +1126,7 @@ submit_async (
1126#endif 1126#endif
1127 1127
1128 spin_lock_irqsave (&ehci->lock, flags); 1128 spin_lock_irqsave (&ehci->lock, flags);
1129 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, 1129 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
1130 &ehci_to_hcd(ehci)->flags))) {
1131 rc = -ESHUTDOWN; 1130 rc = -ESHUTDOWN;
1132 goto done; 1131 goto done;
1133 } 1132 }
diff --git a/drivers/usb/host/ehci-sched.c b/drivers/usb/host/ehci-sched.c
index 805ec633a65..a92526d6e5a 100644
--- a/drivers/usb/host/ehci-sched.c
+++ b/drivers/usb/host/ehci-sched.c
@@ -880,8 +880,7 @@ static int intr_submit (
880 880
881 spin_lock_irqsave (&ehci->lock, flags); 881 spin_lock_irqsave (&ehci->lock, flags);
882 882
883 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, 883 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
884 &ehci_to_hcd(ehci)->flags))) {
885 status = -ESHUTDOWN; 884 status = -ESHUTDOWN;
886 goto done_not_linked; 885 goto done_not_linked;
887 } 886 }
@@ -1075,15 +1074,6 @@ iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
1075 if (stream->ep) 1074 if (stream->ep)
1076 stream->ep->hcpriv = NULL; 1075 stream->ep->hcpriv = NULL;
1077 1076
1078 if (stream->rescheduled) {
1079 ehci_info (ehci, "ep%d%s-iso rescheduled "
1080 "%lu times in %lu seconds\n",
1081 stream->bEndpointAddress, is_in ? "in" : "out",
1082 stream->rescheduled,
1083 ((jiffies - stream->start)/HZ)
1084 );
1085 }
1086
1087 kfree(stream); 1077 kfree(stream);
1088 } 1078 }
1089} 1079}
@@ -1396,30 +1386,25 @@ iso_stream_schedule (
1396 struct ehci_iso_stream *stream 1386 struct ehci_iso_stream *stream
1397) 1387)
1398{ 1388{
1399 u32 now, next, start, period; 1389 u32 now, next, start, period, span;
1400 int status; 1390 int status;
1401 unsigned mod = ehci->periodic_size << 3; 1391 unsigned mod = ehci->periodic_size << 3;
1402 struct ehci_iso_sched *sched = urb->hcpriv; 1392 struct ehci_iso_sched *sched = urb->hcpriv;
1403 struct pci_dev *pdev;
1404 1393
1405 if (sched->span > (mod - SCHEDULE_SLOP)) { 1394 period = urb->interval;
1406 ehci_dbg (ehci, "iso request %p too long\n", urb); 1395 span = sched->span;
1407 status = -EFBIG; 1396 if (!stream->highspeed) {
1408 goto fail; 1397 period <<= 3;
1398 span <<= 3;
1409 } 1399 }
1410 1400
1411 if ((stream->depth + sched->span) > mod) { 1401 if (span > mod - SCHEDULE_SLOP) {
1412 ehci_dbg (ehci, "request %p would overflow (%d+%d>%d)\n", 1402 ehci_dbg (ehci, "iso request %p too long\n", urb);
1413 urb, stream->depth, sched->span, mod);
1414 status = -EFBIG; 1403 status = -EFBIG;
1415 goto fail; 1404 goto fail;
1416 } 1405 }
1417 1406
1418 period = urb->interval; 1407 now = ehci_readl(ehci, &ehci->regs->frame_index) & (mod - 1);
1419 if (!stream->highspeed)
1420 period <<= 3;
1421
1422 now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
1423 1408
1424 /* Typical case: reuse current schedule, stream is still active. 1409 /* Typical case: reuse current schedule, stream is still active.
1425 * Hopefully there are no gaps from the host falling behind 1410 * Hopefully there are no gaps from the host falling behind
@@ -1427,34 +1412,35 @@ iso_stream_schedule (
1427 * slot in the schedule, implicitly assuming URB_ISO_ASAP. 1412 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
1428 */ 1413 */
1429 if (likely (!list_empty (&stream->td_list))) { 1414 if (likely (!list_empty (&stream->td_list))) {
1430 pdev = to_pci_dev(ehci_to_hcd(ehci)->self.controller); 1415 u32 excess;
1431 start = stream->next_uframe;
1432 1416
1433 /* For high speed devices, allow scheduling within the 1417 /* For high speed devices, allow scheduling within the
1434 * isochronous scheduling threshold. For full speed devices, 1418 * isochronous scheduling threshold. For full speed devices
1435 * don't. (Work around for Intel ICH9 bug.) 1419 * and Intel PCI-based controllers, don't (work around for
1420 * Intel ICH9 bug).
1436 */ 1421 */
1437 if (!stream->highspeed && 1422 if (!stream->highspeed && ehci->fs_i_thresh)
1438 pdev->vendor == PCI_VENDOR_ID_INTEL)
1439 next = now + ehci->i_thresh; 1423 next = now + ehci->i_thresh;
1440 else 1424 else
1441 next = now; 1425 next = now;
1442 1426
1443 /* Fell behind (by up to twice the slop amount)? */ 1427 /* Fell behind (by up to twice the slop amount)?
1444 if (((start - next) & (mod - 1)) >= 1428 * We decide based on the time of the last currently-scheduled
1445 mod - 2 * SCHEDULE_SLOP) 1429 * slot, not the time of the next available slot.
1446 start += period * DIV_ROUND_UP( 1430 */
1447 (next - start) & (mod - 1), 1431 excess = (stream->next_uframe - period - next) & (mod - 1);
1448 period); 1432 if (excess >= mod - 2 * SCHEDULE_SLOP)
1449 1433 start = next + excess - mod + period *
1450 /* Tried to schedule too far into the future? */ 1434 DIV_ROUND_UP(mod - excess, period);
1451 if (unlikely(((start - now) & (mod - 1)) + sched->span 1435 else
1452 >= mod - 2 * SCHEDULE_SLOP)) { 1436 start = next + excess + period;
1437 if (start - now >= mod) {
1438 ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
1439 urb, start - now - period, period,
1440 mod);
1453 status = -EFBIG; 1441 status = -EFBIG;
1454 goto fail; 1442 goto fail;
1455 } 1443 }
1456 stream->next_uframe = start;
1457 goto ready;
1458 } 1444 }
1459 1445
1460 /* need to schedule; when's the next (u)frame we could start? 1446 /* need to schedule; when's the next (u)frame we could start?
@@ -1463,51 +1449,60 @@ iso_stream_schedule (
1463 * can also help high bandwidth if the dma and irq loads don't 1449 * can also help high bandwidth if the dma and irq loads don't
1464 * jump until after the queue is primed. 1450 * jump until after the queue is primed.
1465 */ 1451 */
1466 start = SCHEDULE_SLOP + (now & ~0x07); 1452 else {
1467 start %= mod; 1453 start = SCHEDULE_SLOP + (now & ~0x07);
1468 stream->next_uframe = start; 1454
1469 1455 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
1470 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */ 1456
1471 1457 /* find a uframe slot with enough bandwidth */
1472 /* find a uframe slot with enough bandwidth */ 1458 next = start + period;
1473 for (; start < (stream->next_uframe + period); start++) { 1459 for (; start < next; start++) {
1474 int enough_space; 1460
1475 1461 /* check schedule: enough space? */
1476 /* check schedule: enough space? */ 1462 if (stream->highspeed) {
1477 if (stream->highspeed) 1463 if (itd_slot_ok(ehci, mod, start,
1478 enough_space = itd_slot_ok (ehci, mod, start, 1464 stream->usecs, period))
1479 stream->usecs, period); 1465 break;
1480 else { 1466 } else {
1481 if ((start % 8) >= 6) 1467 if ((start % 8) >= 6)
1482 continue; 1468 continue;
1483 enough_space = sitd_slot_ok (ehci, mod, stream, 1469 if (sitd_slot_ok(ehci, mod, stream,
1484 start, sched, period); 1470 start, sched, period))
1471 break;
1472 }
1485 } 1473 }
1486 1474
1487 /* schedule it here if there's enough bandwidth */ 1475 /* no room in the schedule */
1488 if (enough_space) { 1476 if (start == next) {
1489 stream->next_uframe = start % mod; 1477 ehci_dbg(ehci, "iso resched full %p (now %d max %d)\n",
1490 goto ready; 1478 urb, now, now + mod);
1479 status = -ENOSPC;
1480 goto fail;
1491 } 1481 }
1492 } 1482 }
1493 1483
1494 /* no room in the schedule */ 1484 /* Tried to schedule too far into the future? */
1495 ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n", 1485 if (unlikely(start - now + span - period
1496 list_empty (&stream->td_list) ? "" : "re", 1486 >= mod - 2 * SCHEDULE_SLOP)) {
1497 urb, now, now + mod); 1487 ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
1498 status = -ENOSPC; 1488 urb, start - now, span - period,
1489 mod - 2 * SCHEDULE_SLOP);
1490 status = -EFBIG;
1491 goto fail;
1492 }
1499 1493
1500fail: 1494 stream->next_uframe = start & (mod - 1);
1501 iso_sched_free (stream, sched);
1502 urb->hcpriv = NULL;
1503 return status;
1504 1495
1505ready:
1506 /* report high speed start in uframes; full speed, in frames */ 1496 /* report high speed start in uframes; full speed, in frames */
1507 urb->start_frame = stream->next_uframe; 1497 urb->start_frame = stream->next_uframe;
1508 if (!stream->highspeed) 1498 if (!stream->highspeed)
1509 urb->start_frame >>= 3; 1499 urb->start_frame >>= 3;
1510 return 0; 1500 return 0;
1501
1502 fail:
1503 iso_sched_free(stream, sched);
1504 urb->hcpriv = NULL;
1505 return status;
1511} 1506}
1512 1507
1513/*-------------------------------------------------------------------------*/ 1508/*-------------------------------------------------------------------------*/
@@ -1602,7 +1597,7 @@ itd_link_urb (
1602 struct ehci_iso_sched *iso_sched = urb->hcpriv; 1597 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1603 struct ehci_itd *itd; 1598 struct ehci_itd *itd;
1604 1599
1605 next_uframe = stream->next_uframe % mod; 1600 next_uframe = stream->next_uframe & (mod - 1);
1606 1601
1607 if (unlikely (list_empty(&stream->td_list))) { 1602 if (unlikely (list_empty(&stream->td_list))) {
1608 ehci_to_hcd(ehci)->self.bandwidth_allocated 1603 ehci_to_hcd(ehci)->self.bandwidth_allocated
@@ -1613,7 +1608,6 @@ itd_link_urb (
1613 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out", 1608 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1614 urb->interval, 1609 urb->interval,
1615 next_uframe >> 3, next_uframe & 0x7); 1610 next_uframe >> 3, next_uframe & 0x7);
1616 stream->start = jiffies;
1617 } 1611 }
1618 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++; 1612 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1619 1613
@@ -1639,14 +1633,13 @@ itd_link_urb (
1639 itd_patch(ehci, itd, iso_sched, packet, uframe); 1633 itd_patch(ehci, itd, iso_sched, packet, uframe);
1640 1634
1641 next_uframe += stream->interval; 1635 next_uframe += stream->interval;
1642 stream->depth += stream->interval; 1636 next_uframe &= mod - 1;
1643 next_uframe %= mod;
1644 packet++; 1637 packet++;
1645 1638
1646 /* link completed itds into the schedule */ 1639 /* link completed itds into the schedule */
1647 if (((next_uframe >> 3) != frame) 1640 if (((next_uframe >> 3) != frame)
1648 || packet == urb->number_of_packets) { 1641 || packet == urb->number_of_packets) {
1649 itd_link (ehci, frame % ehci->periodic_size, itd); 1642 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
1650 itd = NULL; 1643 itd = NULL;
1651 } 1644 }
1652 } 1645 }
@@ -1695,7 +1688,6 @@ itd_complete (
1695 1688
1696 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]); 1689 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
1697 itd->hw_transaction [uframe] = 0; 1690 itd->hw_transaction [uframe] = 0;
1698 stream->depth -= stream->interval;
1699 1691
1700 /* report transfer status */ 1692 /* report transfer status */
1701 if (unlikely (t & ISO_ERRS)) { 1693 if (unlikely (t & ISO_ERRS)) {
@@ -1815,8 +1807,7 @@ static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
1815 1807
1816 /* schedule ... need to lock */ 1808 /* schedule ... need to lock */
1817 spin_lock_irqsave (&ehci->lock, flags); 1809 spin_lock_irqsave (&ehci->lock, flags);
1818 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, 1810 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
1819 &ehci_to_hcd(ehci)->flags))) {
1820 status = -ESHUTDOWN; 1811 status = -ESHUTDOWN;
1821 goto done_not_linked; 1812 goto done_not_linked;
1822 } 1813 }
@@ -2024,9 +2015,8 @@ sitd_link_urb (
2024 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n", 2015 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
2025 urb->dev->devpath, stream->bEndpointAddress & 0x0f, 2016 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
2026 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out", 2017 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
2027 (next_uframe >> 3) % ehci->periodic_size, 2018 (next_uframe >> 3) & (ehci->periodic_size - 1),
2028 stream->interval, hc32_to_cpu(ehci, stream->splits)); 2019 stream->interval, hc32_to_cpu(ehci, stream->splits));
2029 stream->start = jiffies;
2030 } 2020 }
2031 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++; 2021 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
2032 2022
@@ -2047,13 +2037,12 @@ sitd_link_urb (
2047 sitd->urb = urb; 2037 sitd->urb = urb;
2048 2038
2049 sitd_patch(ehci, stream, sitd, sched, packet); 2039 sitd_patch(ehci, stream, sitd, sched, packet);
2050 sitd_link (ehci, (next_uframe >> 3) % ehci->periodic_size, 2040 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
2051 sitd); 2041 sitd);
2052 2042
2053 next_uframe += stream->interval << 3; 2043 next_uframe += stream->interval << 3;
2054 stream->depth += stream->interval << 3;
2055 } 2044 }
2056 stream->next_uframe = next_uframe % mod; 2045 stream->next_uframe = next_uframe & (mod - 1);
2057 2046
2058 /* don't need that schedule data any more */ 2047 /* don't need that schedule data any more */
2059 iso_sched_free (stream, sched); 2048 iso_sched_free (stream, sched);
@@ -2111,7 +2100,6 @@ sitd_complete (
2111 desc->actual_length = desc->length - SITD_LENGTH(t); 2100 desc->actual_length = desc->length - SITD_LENGTH(t);
2112 urb->actual_length += desc->actual_length; 2101 urb->actual_length += desc->actual_length;
2113 } 2102 }
2114 stream->depth -= stream->interval << 3;
2115 2103
2116 /* handle completion now? */ 2104 /* handle completion now? */
2117 if ((urb_index + 1) != urb->number_of_packets) 2105 if ((urb_index + 1) != urb->number_of_packets)
@@ -2201,8 +2189,7 @@ static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
2201 2189
2202 /* schedule ... need to lock */ 2190 /* schedule ... need to lock */
2203 spin_lock_irqsave (&ehci->lock, flags); 2191 spin_lock_irqsave (&ehci->lock, flags);
2204 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, 2192 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
2205 &ehci_to_hcd(ehci)->flags))) {
2206 status = -ESHUTDOWN; 2193 status = -ESHUTDOWN;
2207 goto done_not_linked; 2194 goto done_not_linked;
2208 } 2195 }
@@ -2263,7 +2250,7 @@ scan_periodic (struct ehci_hcd *ehci)
2263 now_uframe = ehci->next_uframe; 2250 now_uframe = ehci->next_uframe;
2264 if (HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) { 2251 if (HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
2265 clock = ehci_readl(ehci, &ehci->regs->frame_index); 2252 clock = ehci_readl(ehci, &ehci->regs->frame_index);
2266 clock_frame = (clock >> 3) % ehci->periodic_size; 2253 clock_frame = (clock >> 3) & (ehci->periodic_size - 1);
2267 } else { 2254 } else {
2268 clock = now_uframe + mod - 1; 2255 clock = now_uframe + mod - 1;
2269 clock_frame = -1; 2256 clock_frame = -1;
@@ -2272,7 +2259,7 @@ scan_periodic (struct ehci_hcd *ehci)
2272 free_cached_lists(ehci); 2259 free_cached_lists(ehci);
2273 ehci->clock_frame = clock_frame; 2260 ehci->clock_frame = clock_frame;
2274 } 2261 }
2275 clock %= mod; 2262 clock &= mod - 1;
2276 clock_frame = clock >> 3; 2263 clock_frame = clock >> 3;
2277 2264
2278 for (;;) { 2265 for (;;) {
@@ -2361,7 +2348,7 @@ restart:
2361 * frame is current. 2348 * frame is current.
2362 */ 2349 */
2363 if (((frame == clock_frame) || 2350 if (((frame == clock_frame) ||
2364 (((frame + 1) % ehci->periodic_size) 2351 (((frame + 1) & (ehci->periodic_size - 1))
2365 == clock_frame)) 2352 == clock_frame))
2366 && live 2353 && live
2367 && (q.sitd->hw_results & 2354 && (q.sitd->hw_results &
@@ -2428,7 +2415,8 @@ restart:
2428 || ehci->periodic_sched == 0) 2415 || ehci->periodic_sched == 0)
2429 break; 2416 break;
2430 ehci->next_uframe = now_uframe; 2417 ehci->next_uframe = now_uframe;
2431 now = ehci_readl(ehci, &ehci->regs->frame_index) % mod; 2418 now = ehci_readl(ehci, &ehci->regs->frame_index) &
2419 (mod - 1);
2432 if (now_uframe == now) 2420 if (now_uframe == now)
2433 break; 2421 break;
2434 2422
@@ -2441,7 +2429,7 @@ restart:
2441 } 2429 }
2442 } else { 2430 } else {
2443 now_uframe++; 2431 now_uframe++;
2444 now_uframe %= mod; 2432 now_uframe &= mod - 1;
2445 } 2433 }
2446 } 2434 }
2447} 2435}
diff --git a/drivers/usb/host/ehci.h b/drivers/usb/host/ehci.h
index 650a687f285..bde823f704e 100644
--- a/drivers/usb/host/ehci.h
+++ b/drivers/usb/host/ehci.h
@@ -130,6 +130,7 @@ struct ehci_hcd { /* one per controller */
130 unsigned has_amcc_usb23:1; 130 unsigned has_amcc_usb23:1;
131 unsigned need_io_watchdog:1; 131 unsigned need_io_watchdog:1;
132 unsigned broken_periodic:1; 132 unsigned broken_periodic:1;
133 unsigned fs_i_thresh:1; /* Intel iso scheduling */
133 134
134 /* required for usb32 quirk */ 135 /* required for usb32 quirk */
135 #define OHCI_CTRL_HCFS (3 << 6) 136 #define OHCI_CTRL_HCFS (3 << 6)
@@ -140,7 +141,8 @@ struct ehci_hcd { /* one per controller */
140 #define OHCI_HCCTRL_LEN 0x4 141 #define OHCI_HCCTRL_LEN 0x4
141 __hc32 *ohci_hcctrl_reg; 142 __hc32 *ohci_hcctrl_reg;
142 unsigned has_hostpc:1; 143 unsigned has_hostpc:1;
143 144 unsigned has_lpm:1; /* support link power management */
145 unsigned has_ppcd:1; /* support per-port change bits */
144 u8 sbrn; /* packed release number */ 146 u8 sbrn; /* packed release number */
145 147
146 /* irq statistics */ 148 /* irq statistics */
@@ -154,9 +156,6 @@ struct ehci_hcd { /* one per controller */
154 /* debug files */ 156 /* debug files */
155#ifdef DEBUG 157#ifdef DEBUG
156 struct dentry *debug_dir; 158 struct dentry *debug_dir;
157 struct dentry *debug_async;
158 struct dentry *debug_periodic;
159 struct dentry *debug_registers;
160#endif 159#endif
161}; 160};
162 161
@@ -401,15 +400,12 @@ struct ehci_iso_stream {
401 u32 refcount; 400 u32 refcount;
402 u8 bEndpointAddress; 401 u8 bEndpointAddress;
403 u8 highspeed; 402 u8 highspeed;
404 u16 depth; /* depth in uframes */
405 struct list_head td_list; /* queued itds/sitds */ 403 struct list_head td_list; /* queued itds/sitds */
406 struct list_head free_list; /* list of unused itds/sitds */ 404 struct list_head free_list; /* list of unused itds/sitds */
407 struct usb_device *udev; 405 struct usb_device *udev;
408 struct usb_host_endpoint *ep; 406 struct usb_host_endpoint *ep;
409 407
410 /* output of (re)scheduling */ 408 /* output of (re)scheduling */
411 unsigned long start; /* jiffies */
412 unsigned long rescheduled;
413 int next_uframe; 409 int next_uframe;
414 __hc32 splits; 410 __hc32 splits;
415 411
@@ -538,11 +534,11 @@ struct ehci_fstn {
538 534
539/* Prepare the PORTSC wakeup flags during controller suspend/resume */ 535/* Prepare the PORTSC wakeup flags during controller suspend/resume */
540 536
541#define ehci_prepare_ports_for_controller_suspend(ehci) \ 537#define ehci_prepare_ports_for_controller_suspend(ehci, do_wakeup) \
542 ehci_adjust_port_wakeup_flags(ehci, true); 538 ehci_adjust_port_wakeup_flags(ehci, true, do_wakeup);
543 539
544#define ehci_prepare_ports_for_controller_resume(ehci) \ 540#define ehci_prepare_ports_for_controller_resume(ehci) \
545 ehci_adjust_port_wakeup_flags(ehci, false); 541 ehci_adjust_port_wakeup_flags(ehci, false, false);
546 542
547/*-------------------------------------------------------------------------*/ 543/*-------------------------------------------------------------------------*/
548 544
diff --git a/drivers/usb/host/hwa-hc.c b/drivers/usb/host/hwa-hc.c
index 35742f8c7cd..9bfac657572 100644
--- a/drivers/usb/host/hwa-hc.c
+++ b/drivers/usb/host/hwa-hc.c
@@ -159,7 +159,7 @@ static int hwahc_op_start(struct usb_hcd *usb_hcd)
159 goto error_set_cluster_id; 159 goto error_set_cluster_id;
160 160
161 usb_hcd->uses_new_polling = 1; 161 usb_hcd->uses_new_polling = 1;
162 usb_hcd->poll_rh = 1; 162 set_bit(HCD_FLAG_POLL_RH, &usb_hcd->flags);
163 usb_hcd->state = HC_STATE_RUNNING; 163 usb_hcd->state = HC_STATE_RUNNING;
164 result = 0; 164 result = 0;
165out: 165out:
@@ -776,7 +776,7 @@ static int hwahc_probe(struct usb_interface *usb_iface,
776 goto error_alloc; 776 goto error_alloc;
777 } 777 }
778 usb_hcd->wireless = 1; 778 usb_hcd->wireless = 1;
779 usb_hcd->flags |= HCD_FLAG_SAW_IRQ; 779 set_bit(HCD_FLAG_SAW_IRQ, &usb_hcd->flags);
780 wusbhc = usb_hcd_to_wusbhc(usb_hcd); 780 wusbhc = usb_hcd_to_wusbhc(usb_hcd);
781 hwahc = container_of(wusbhc, struct hwahc, wusbhc); 781 hwahc = container_of(wusbhc, struct hwahc, wusbhc);
782 hwahc_init(hwahc); 782 hwahc_init(hwahc);
diff --git a/drivers/usb/host/imx21-hcd.c b/drivers/usb/host/imx21-hcd.c
index caf116c0937..d0abb9b0e67 100644
--- a/drivers/usb/host/imx21-hcd.c
+++ b/drivers/usb/host/imx21-hcd.c
@@ -1521,7 +1521,7 @@ static int imx21_hc_reset(struct usb_hcd *hcd)
1521 return -ETIMEDOUT; 1521 return -ETIMEDOUT;
1522 } 1522 }
1523 spin_unlock_irq(&imx21->lock); 1523 spin_unlock_irq(&imx21->lock);
1524 schedule_timeout(1); 1524 schedule_timeout_uninterruptible(1);
1525 spin_lock_irq(&imx21->lock); 1525 spin_lock_irq(&imx21->lock);
1526 } 1526 }
1527 spin_unlock_irqrestore(&imx21->lock, flags); 1527 spin_unlock_irqrestore(&imx21->lock, flags);
diff --git a/drivers/usb/host/isp1362.h b/drivers/usb/host/isp1362.h
index d995351f9be..0f97820e65b 100644
--- a/drivers/usb/host/isp1362.h
+++ b/drivers/usb/host/isp1362.h
@@ -8,29 +8,7 @@
8/* 8/*
9 * Platform specific compile time options 9 * Platform specific compile time options
10 */ 10 */
11#if defined(CONFIG_ARCH_KARO) 11#if defined(CONFIG_BLACKFIN)
12#include <asm/arch/hardware.h>
13#include <asm/arch/pxa-regs.h>
14#include <asm/arch/karo.h>
15
16#define USE_32BIT 1
17
18
19/* These options are mutually eclusive */
20#define USE_PLATFORM_DELAY 1
21#define USE_NDELAY 0
22/*
23 * MAX_ROOT_PORTS: Number of downstream ports
24 *
25 * The chip has two USB ports, one of which can be configured as
26 * an USB device port, so the value of this constant is implementation
27 * specific.
28 */
29#define MAX_ROOT_PORTS 2
30#define DUMMY_DELAY_ACCESS do {} while (0)
31
32/* insert platform specific definitions for other machines here */
33#elif defined(CONFIG_BLACKFIN)
34 12
35#include <linux/io.h> 13#include <linux/io.h>
36#define USE_32BIT 0 14#define USE_32BIT 0
diff --git a/drivers/usb/host/isp1760-hcd.c b/drivers/usb/host/isp1760-hcd.c
index dbcafa29c77..d1a3dfc9a40 100644
--- a/drivers/usb/host/isp1760-hcd.c
+++ b/drivers/usb/host/isp1760-hcd.c
@@ -482,7 +482,6 @@ static int isp1760_run(struct usb_hcd *hcd)
482 u32 chipid; 482 u32 chipid;
483 483
484 hcd->uses_new_polling = 1; 484 hcd->uses_new_polling = 1;
485 hcd->poll_rh = 0;
486 485
487 hcd->state = HC_STATE_RUNNING; 486 hcd->state = HC_STATE_RUNNING;
488 isp1760_enable_interrupts(hcd); 487 isp1760_enable_interrupts(hcd);
@@ -1450,7 +1449,7 @@ static int isp1760_prepare_enqueue(struct isp1760_hcd *priv, struct urb *urb,
1450 epnum = urb->ep->desc.bEndpointAddress; 1449 epnum = urb->ep->desc.bEndpointAddress;
1451 1450
1452 spin_lock_irqsave(&priv->lock, flags); 1451 spin_lock_irqsave(&priv->lock, flags);
1453 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &priv_to_hcd(priv)->flags)) { 1452 if (!HCD_HW_ACCESSIBLE(priv_to_hcd(priv))) {
1454 rc = -ESHUTDOWN; 1453 rc = -ESHUTDOWN;
1455 goto done; 1454 goto done;
1456 } 1455 }
diff --git a/drivers/usb/host/ohci-dbg.c b/drivers/usb/host/ohci-dbg.c
index 8ad2441b028..36abd2baa3e 100644
--- a/drivers/usb/host/ohci-dbg.c
+++ b/drivers/usb/host/ohci-dbg.c
@@ -645,7 +645,7 @@ static ssize_t fill_registers_buffer(struct debug_buffer *buf)
645 hcd->product_desc, 645 hcd->product_desc,
646 hcd_name); 646 hcd_name);
647 647
648 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { 648 if (!HCD_HW_ACCESSIBLE(hcd)) {
649 size -= scnprintf (next, size, 649 size -= scnprintf (next, size,
650 "SUSPENDED (no register access)\n"); 650 "SUSPENDED (no register access)\n");
651 goto done; 651 goto done;
@@ -687,7 +687,7 @@ static ssize_t fill_registers_buffer(struct debug_buffer *buf)
687 next += temp; 687 next += temp;
688 688
689 temp = scnprintf (next, size, "hub poll timer %s\n", 689 temp = scnprintf (next, size, "hub poll timer %s\n",
690 ohci_to_hcd(ohci)->poll_rh ? "ON" : "off"); 690 HCD_POLL_RH(ohci_to_hcd(ohci)) ? "ON" : "off");
691 size -= temp; 691 size -= temp;
692 next += temp; 692 next += temp;
693 693
diff --git a/drivers/usb/host/ohci-hcd.c b/drivers/usb/host/ohci-hcd.c
index 3ceb097e165..15ae39d6cc2 100644
--- a/drivers/usb/host/ohci-hcd.c
+++ b/drivers/usb/host/ohci-hcd.c
@@ -212,7 +212,7 @@ static int ohci_urb_enqueue (
212 spin_lock_irqsave (&ohci->lock, flags); 212 spin_lock_irqsave (&ohci->lock, flags);
213 213
214 /* don't submit to a dead HC */ 214 /* don't submit to a dead HC */
215 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { 215 if (!HCD_HW_ACCESSIBLE(hcd)) {
216 retval = -ENODEV; 216 retval = -ENODEV;
217 goto fail; 217 goto fail;
218 } 218 }
@@ -684,7 +684,7 @@ retry:
684 } 684 }
685 685
686 /* use rhsc irqs after khubd is fully initialized */ 686 /* use rhsc irqs after khubd is fully initialized */
687 hcd->poll_rh = 1; 687 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
688 hcd->uses_new_polling = 1; 688 hcd->uses_new_polling = 1;
689 689
690 /* start controller operations */ 690 /* start controller operations */
@@ -821,7 +821,7 @@ static irqreturn_t ohci_irq (struct usb_hcd *hcd)
821 else if (ints & OHCI_INTR_RD) { 821 else if (ints & OHCI_INTR_RD) {
822 ohci_vdbg(ohci, "resume detect\n"); 822 ohci_vdbg(ohci, "resume detect\n");
823 ohci_writel(ohci, OHCI_INTR_RD, &regs->intrstatus); 823 ohci_writel(ohci, OHCI_INTR_RD, &regs->intrstatus);
824 hcd->poll_rh = 1; 824 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
825 if (ohci->autostop) { 825 if (ohci->autostop) {
826 spin_lock (&ohci->lock); 826 spin_lock (&ohci->lock);
827 ohci_rh_resume (ohci); 827 ohci_rh_resume (ohci);
diff --git a/drivers/usb/host/ohci-hub.c b/drivers/usb/host/ohci-hub.c
index 65cac8cc892..cddcda95b57 100644
--- a/drivers/usb/host/ohci-hub.c
+++ b/drivers/usb/host/ohci-hub.c
@@ -284,7 +284,7 @@ static int ohci_bus_suspend (struct usb_hcd *hcd)
284 284
285 spin_lock_irq (&ohci->lock); 285 spin_lock_irq (&ohci->lock);
286 286
287 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) 287 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
288 rc = -ESHUTDOWN; 288 rc = -ESHUTDOWN;
289 else 289 else
290 rc = ohci_rh_suspend (ohci, 0); 290 rc = ohci_rh_suspend (ohci, 0);
@@ -302,7 +302,7 @@ static int ohci_bus_resume (struct usb_hcd *hcd)
302 302
303 spin_lock_irq (&ohci->lock); 303 spin_lock_irq (&ohci->lock);
304 304
305 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) 305 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
306 rc = -ESHUTDOWN; 306 rc = -ESHUTDOWN;
307 else 307 else
308 rc = ohci_rh_resume (ohci); 308 rc = ohci_rh_resume (ohci);
@@ -355,6 +355,11 @@ static void ohci_finish_controller_resume(struct usb_hcd *hcd)
355 ohci_readl(ohci, &ohci->regs->intrenable); 355 ohci_readl(ohci, &ohci->regs->intrenable);
356 msleep(20); 356 msleep(20);
357 } 357 }
358
359 /* Does the root hub have a port wakeup pending? */
360 if (ohci_readl(ohci, &ohci->regs->intrstatus) &
361 (OHCI_INTR_RD | OHCI_INTR_RHSC))
362 usb_hcd_resume_root_hub(hcd);
358} 363}
359 364
360/* Carry out polling-, autostop-, and autoresume-related state changes */ 365/* Carry out polling-, autostop-, and autoresume-related state changes */
@@ -364,7 +369,7 @@ static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
364 int poll_rh = 1; 369 int poll_rh = 1;
365 int rhsc_enable; 370 int rhsc_enable;
366 371
367 /* Some broken controllers never turn off RHCS in the interrupt 372 /* Some broken controllers never turn off RHSC in the interrupt
368 * status register. For their sake we won't re-enable RHSC 373 * status register. For their sake we won't re-enable RHSC
369 * interrupts if the interrupt bit is already active. 374 * interrupts if the interrupt bit is already active.
370 */ 375 */
@@ -489,7 +494,7 @@ ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
489 unsigned long flags; 494 unsigned long flags;
490 495
491 spin_lock_irqsave (&ohci->lock, flags); 496 spin_lock_irqsave (&ohci->lock, flags);
492 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) 497 if (!HCD_HW_ACCESSIBLE(hcd))
493 goto done; 498 goto done;
494 499
495 /* undocumented erratum seen on at least rev D */ 500 /* undocumented erratum seen on at least rev D */
@@ -533,8 +538,12 @@ ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
533 } 538 }
534 } 539 }
535 540
536 hcd->poll_rh = ohci_root_hub_state_changes(ohci, changed, 541 if (ohci_root_hub_state_changes(ohci, changed,
537 any_connected, rhsc_status); 542 any_connected, rhsc_status))
543 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
544 else
545 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
546
538 547
539done: 548done:
540 spin_unlock_irqrestore (&ohci->lock, flags); 549 spin_unlock_irqrestore (&ohci->lock, flags);
@@ -701,7 +710,7 @@ static int ohci_hub_control (
701 u32 temp; 710 u32 temp;
702 int retval = 0; 711 int retval = 0;
703 712
704 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) 713 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
705 return -ESHUTDOWN; 714 return -ESHUTDOWN;
706 715
707 switch (typeReq) { 716 switch (typeReq) {
diff --git a/drivers/usb/host/ohci-pci.c b/drivers/usb/host/ohci-pci.c
index b8a1148f248..6bdc8b25a6a 100644
--- a/drivers/usb/host/ohci-pci.c
+++ b/drivers/usb/host/ohci-pci.c
@@ -392,7 +392,7 @@ static int __devinit ohci_pci_start (struct usb_hcd *hcd)
392 392
393#ifdef CONFIG_PM 393#ifdef CONFIG_PM
394 394
395static int ohci_pci_suspend(struct usb_hcd *hcd) 395static int ohci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
396{ 396{
397 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 397 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
398 unsigned long flags; 398 unsigned long flags;
diff --git a/drivers/usb/host/ohci-ssb.c b/drivers/usb/host/ohci-ssb.c
index 23fd6a886bd..48ee6943bf3 100644
--- a/drivers/usb/host/ohci-ssb.c
+++ b/drivers/usb/host/ohci-ssb.c
@@ -93,8 +93,11 @@ static void ssb_ohci_detach(struct ssb_device *dev)
93{ 93{
94 struct usb_hcd *hcd = ssb_get_drvdata(dev); 94 struct usb_hcd *hcd = ssb_get_drvdata(dev);
95 95
96 if (hcd->driver->shutdown)
97 hcd->driver->shutdown(hcd);
96 usb_remove_hcd(hcd); 98 usb_remove_hcd(hcd);
97 iounmap(hcd->regs); 99 iounmap(hcd->regs);
100 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
98 usb_put_hcd(hcd); 101 usb_put_hcd(hcd);
99 ssb_device_disable(dev, 0); 102 ssb_device_disable(dev, 0);
100} 103}
@@ -106,10 +109,52 @@ static int ssb_ohci_attach(struct ssb_device *dev)
106 int err = -ENOMEM; 109 int err = -ENOMEM;
107 u32 tmp, flags = 0; 110 u32 tmp, flags = 0;
108 111
109 if (dev->id.coreid == SSB_DEV_USB11_HOSTDEV) 112 if (dma_set_mask(dev->dma_dev, DMA_BIT_MASK(32)) ||
110 flags |= SSB_OHCI_TMSLOW_HOSTMODE; 113 dma_set_coherent_mask(dev->dma_dev, DMA_BIT_MASK(32)))
114 return -EOPNOTSUPP;
111 115
112 ssb_device_enable(dev, flags); 116 if (dev->id.coreid == SSB_DEV_USB11_HOSTDEV) {
117 /* Put the device into host-mode. */
118 flags |= SSB_OHCI_TMSLOW_HOSTMODE;
119 ssb_device_enable(dev, flags);
120 } else if (dev->id.coreid == SSB_DEV_USB20_HOST) {
121 /*
122 * USB 2.0 special considerations:
123 *
124 * In addition to the standard SSB reset sequence, the Host
125 * Control Register must be programmed to bring the USB core
126 * and various phy components out of reset.
127 */
128 ssb_device_enable(dev, 0);
129 ssb_write32(dev, 0x200, 0x7ff);
130
131 /* Change Flush control reg */
132 tmp = ssb_read32(dev, 0x400);
133 tmp &= ~8;
134 ssb_write32(dev, 0x400, tmp);
135 tmp = ssb_read32(dev, 0x400);
136
137 /* Change Shim control reg */
138 tmp = ssb_read32(dev, 0x304);
139 tmp &= ~0x100;
140 ssb_write32(dev, 0x304, tmp);
141 tmp = ssb_read32(dev, 0x304);
142
143 udelay(1);
144
145 /* Work around for 5354 failures */
146 if (dev->id.revision == 2 && dev->bus->chip_id == 0x5354) {
147 /* Change syn01 reg */
148 tmp = 0x00fe00fe;
149 ssb_write32(dev, 0x894, tmp);
150
151 /* Change syn03 reg */
152 tmp = ssb_read32(dev, 0x89c);
153 tmp |= 0x1;
154 ssb_write32(dev, 0x89c, tmp);
155 }
156 } else
157 ssb_device_enable(dev, 0);
113 158
114 hcd = usb_create_hcd(&ssb_ohci_hc_driver, dev->dev, 159 hcd = usb_create_hcd(&ssb_ohci_hc_driver, dev->dev,
115 dev_name(dev->dev)); 160 dev_name(dev->dev));
@@ -200,6 +245,7 @@ static int ssb_ohci_resume(struct ssb_device *dev)
200static const struct ssb_device_id ssb_ohci_table[] = { 245static const struct ssb_device_id ssb_ohci_table[] = {
201 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB11_HOSTDEV, SSB_ANY_REV), 246 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB11_HOSTDEV, SSB_ANY_REV),
202 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB11_HOST, SSB_ANY_REV), 247 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB11_HOST, SSB_ANY_REV),
248 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB20_HOST, SSB_ANY_REV),
203 SSB_DEVTABLE_END 249 SSB_DEVTABLE_END
204}; 250};
205MODULE_DEVICE_TABLE(ssb, ssb_ohci_table); 251MODULE_DEVICE_TABLE(ssb, ssb_ohci_table);
diff --git a/drivers/usb/host/oxu210hp-hcd.c b/drivers/usb/host/oxu210hp-hcd.c
index de9e1c35da4..8026dc85996 100644
--- a/drivers/usb/host/oxu210hp-hcd.c
+++ b/drivers/usb/host/oxu210hp-hcd.c
@@ -1633,8 +1633,7 @@ static int submit_async(struct oxu_hcd *oxu, struct urb *urb,
1633#endif 1633#endif
1634 1634
1635 spin_lock_irqsave(&oxu->lock, flags); 1635 spin_lock_irqsave(&oxu->lock, flags);
1636 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, 1636 if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) {
1637 &oxu_to_hcd(oxu)->flags))) {
1638 rc = -ESHUTDOWN; 1637 rc = -ESHUTDOWN;
1639 goto done; 1638 goto done;
1640 } 1639 }
@@ -2201,8 +2200,7 @@ static int intr_submit(struct oxu_hcd *oxu, struct urb *urb,
2201 2200
2202 spin_lock_irqsave(&oxu->lock, flags); 2201 spin_lock_irqsave(&oxu->lock, flags);
2203 2202
2204 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, 2203 if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) {
2205 &oxu_to_hcd(oxu)->flags))) {
2206 status = -ESHUTDOWN; 2204 status = -ESHUTDOWN;
2207 goto done; 2205 goto done;
2208 } 2206 }
@@ -2707,7 +2705,6 @@ static int oxu_run(struct usb_hcd *hcd)
2707 u32 temp, hcc_params; 2705 u32 temp, hcc_params;
2708 2706
2709 hcd->uses_new_polling = 1; 2707 hcd->uses_new_polling = 1;
2710 hcd->poll_rh = 0;
2711 2708
2712 /* EHCI spec section 4.1 */ 2709 /* EHCI spec section 4.1 */
2713 retval = ehci_reset(oxu); 2710 retval = ehci_reset(oxu);
diff --git a/drivers/usb/host/sl811-hcd.c b/drivers/usb/host/sl811-hcd.c
index bcf9f0e809d..990f06b89ea 100644
--- a/drivers/usb/host/sl811-hcd.c
+++ b/drivers/usb/host/sl811-hcd.c
@@ -813,8 +813,11 @@ static int sl811h_urb_enqueue(
813#endif 813#endif
814 814
815 /* avoid all allocations within spinlocks */ 815 /* avoid all allocations within spinlocks */
816 if (!hep->hcpriv) 816 if (!hep->hcpriv) {
817 ep = kzalloc(sizeof *ep, mem_flags); 817 ep = kzalloc(sizeof *ep, mem_flags);
818 if (ep == NULL)
819 return -ENOMEM;
820 }
818 821
819 spin_lock_irqsave(&sl811->lock, flags); 822 spin_lock_irqsave(&sl811->lock, flags);
820 823
diff --git a/drivers/usb/host/uhci-debug.c b/drivers/usb/host/uhci-debug.c
index 98cf0b26b96..6e7fb5f38db 100644
--- a/drivers/usb/host/uhci-debug.c
+++ b/drivers/usb/host/uhci-debug.c
@@ -17,7 +17,6 @@
17 17
18#include "uhci-hcd.h" 18#include "uhci-hcd.h"
19 19
20#define uhci_debug_operations (* (const struct file_operations *) NULL)
21static struct dentry *uhci_debugfs_root; 20static struct dentry *uhci_debugfs_root;
22 21
23#ifdef DEBUG 22#ifdef DEBUG
@@ -495,18 +494,16 @@ static int uhci_debug_open(struct inode *inode, struct file *file)
495{ 494{
496 struct uhci_hcd *uhci = inode->i_private; 495 struct uhci_hcd *uhci = inode->i_private;
497 struct uhci_debug *up; 496 struct uhci_debug *up;
498 int ret = -ENOMEM;
499 unsigned long flags; 497 unsigned long flags;
500 498
501 lock_kernel();
502 up = kmalloc(sizeof(*up), GFP_KERNEL); 499 up = kmalloc(sizeof(*up), GFP_KERNEL);
503 if (!up) 500 if (!up)
504 goto out; 501 return -ENOMEM;
505 502
506 up->data = kmalloc(MAX_OUTPUT, GFP_KERNEL); 503 up->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
507 if (!up->data) { 504 if (!up->data) {
508 kfree(up); 505 kfree(up);
509 goto out; 506 return -ENOMEM;
510 } 507 }
511 508
512 up->size = 0; 509 up->size = 0;
@@ -517,10 +514,7 @@ static int uhci_debug_open(struct inode *inode, struct file *file)
517 514
518 file->private_data = up; 515 file->private_data = up;
519 516
520 ret = 0; 517 return 0;
521out:
522 unlock_kernel();
523 return ret;
524} 518}
525 519
526static loff_t uhci_debug_lseek(struct file *file, loff_t off, int whence) 520static loff_t uhci_debug_lseek(struct file *file, loff_t off, int whence)
@@ -528,9 +522,9 @@ static loff_t uhci_debug_lseek(struct file *file, loff_t off, int whence)
528 struct uhci_debug *up; 522 struct uhci_debug *up;
529 loff_t new = -1; 523 loff_t new = -1;
530 524
531 lock_kernel();
532 up = file->private_data; 525 up = file->private_data;
533 526
527 /* XXX: atomic 64bit seek access, but that needs to be fixed in the VFS */
534 switch (whence) { 528 switch (whence) {
535 case 0: 529 case 0:
536 new = off; 530 new = off;
@@ -539,11 +533,10 @@ static loff_t uhci_debug_lseek(struct file *file, loff_t off, int whence)
539 new = file->f_pos + off; 533 new = file->f_pos + off;
540 break; 534 break;
541 } 535 }
542 if (new < 0 || new > up->size) { 536
543 unlock_kernel(); 537 if (new < 0 || new > up->size)
544 return -EINVAL; 538 return -EINVAL;
545 } 539
546 unlock_kernel();
547 return (file->f_pos = new); 540 return (file->f_pos = new);
548} 541}
549 542
@@ -564,7 +557,6 @@ static int uhci_debug_release(struct inode *inode, struct file *file)
564 return 0; 557 return 0;
565} 558}
566 559
567#undef uhci_debug_operations
568static const struct file_operations uhci_debug_operations = { 560static const struct file_operations uhci_debug_operations = {
569 .owner = THIS_MODULE, 561 .owner = THIS_MODULE,
570 .open = uhci_debug_open, 562 .open = uhci_debug_open,
@@ -572,6 +564,7 @@ static const struct file_operations uhci_debug_operations = {
572 .read = uhci_debug_read, 564 .read = uhci_debug_read,
573 .release = uhci_debug_release, 565 .release = uhci_debug_release,
574}; 566};
567#define UHCI_DEBUG_OPS
575 568
576#endif /* CONFIG_DEBUG_FS */ 569#endif /* CONFIG_DEBUG_FS */
577 570
diff --git a/drivers/usb/host/uhci-hcd.c b/drivers/usb/host/uhci-hcd.c
index 6637e52736d..f52d04db28f 100644
--- a/drivers/usb/host/uhci-hcd.c
+++ b/drivers/usb/host/uhci-hcd.c
@@ -140,7 +140,7 @@ static void finish_reset(struct uhci_hcd *uhci)
140 uhci->rh_state = UHCI_RH_RESET; 140 uhci->rh_state = UHCI_RH_RESET;
141 uhci->is_stopped = UHCI_IS_STOPPED; 141 uhci->is_stopped = UHCI_IS_STOPPED;
142 uhci_to_hcd(uhci)->state = HC_STATE_HALT; 142 uhci_to_hcd(uhci)->state = HC_STATE_HALT;
143 uhci_to_hcd(uhci)->poll_rh = 0; 143 clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
144 144
145 uhci->dead = 0; /* Full reset resurrects the controller */ 145 uhci->dead = 0; /* Full reset resurrects the controller */
146} 146}
@@ -176,6 +176,8 @@ static void check_and_reset_hc(struct uhci_hcd *uhci)
176 */ 176 */
177static void configure_hc(struct uhci_hcd *uhci) 177static void configure_hc(struct uhci_hcd *uhci)
178{ 178{
179 struct pci_dev *pdev = to_pci_dev(uhci_dev(uhci));
180
179 /* Set the frame length to the default: 1 ms exactly */ 181 /* Set the frame length to the default: 1 ms exactly */
180 outb(USBSOF_DEFAULT, uhci->io_addr + USBSOF); 182 outb(USBSOF_DEFAULT, uhci->io_addr + USBSOF);
181 183
@@ -191,8 +193,11 @@ static void configure_hc(struct uhci_hcd *uhci)
191 mb(); 193 mb();
192 194
193 /* Enable PIRQ */ 195 /* Enable PIRQ */
194 pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP, 196 pci_write_config_word(pdev, USBLEGSUP, USBLEGSUP_DEFAULT);
195 USBLEGSUP_DEFAULT); 197
198 /* Disable platform-specific non-PME# wakeup */
199 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
200 pci_write_config_byte(pdev, USBRES_INTEL, 0);
196} 201}
197 202
198 203
@@ -344,7 +349,10 @@ __acquires(uhci->lock)
344 /* If interrupts don't work and remote wakeup is enabled then 349 /* If interrupts don't work and remote wakeup is enabled then
345 * the suspended root hub needs to be polled. 350 * the suspended root hub needs to be polled.
346 */ 351 */
347 uhci_to_hcd(uhci)->poll_rh = (!int_enable && wakeup_enable); 352 if (!int_enable && wakeup_enable)
353 set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
354 else
355 clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
348 356
349 uhci_scan_schedule(uhci); 357 uhci_scan_schedule(uhci);
350 uhci_fsbr_off(uhci); 358 uhci_fsbr_off(uhci);
@@ -363,7 +371,7 @@ static void start_rh(struct uhci_hcd *uhci)
363 uhci->io_addr + USBINTR); 371 uhci->io_addr + USBINTR);
364 mb(); 372 mb();
365 uhci->rh_state = UHCI_RH_RUNNING; 373 uhci->rh_state = UHCI_RH_RUNNING;
366 uhci_to_hcd(uhci)->poll_rh = 1; 374 set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
367} 375}
368 376
369static void wakeup_rh(struct uhci_hcd *uhci) 377static void wakeup_rh(struct uhci_hcd *uhci)
@@ -589,7 +597,7 @@ static int uhci_start(struct usb_hcd *hcd)
589 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 597 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
590 int retval = -EBUSY; 598 int retval = -EBUSY;
591 int i; 599 int i;
592 struct dentry *dentry; 600 struct dentry __maybe_unused *dentry;
593 601
594 hcd->uses_new_polling = 1; 602 hcd->uses_new_polling = 1;
595 603
@@ -599,18 +607,16 @@ static int uhci_start(struct usb_hcd *hcd)
599 INIT_LIST_HEAD(&uhci->idle_qh_list); 607 INIT_LIST_HEAD(&uhci->idle_qh_list);
600 init_waitqueue_head(&uhci->waitqh); 608 init_waitqueue_head(&uhci->waitqh);
601 609
602 if (DEBUG_CONFIGURED) { 610#ifdef UHCI_DEBUG_OPS
603 dentry = debugfs_create_file(hcd->self.bus_name, 611 dentry = debugfs_create_file(hcd->self.bus_name,
604 S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root, 612 S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root,
605 uhci, &uhci_debug_operations); 613 uhci, &uhci_debug_operations);
606 if (!dentry) { 614 if (!dentry) {
607 dev_err(uhci_dev(uhci), "couldn't create uhci " 615 dev_err(uhci_dev(uhci), "couldn't create uhci debugfs entry\n");
608 "debugfs entry\n"); 616 return -ENOMEM;
609 retval = -ENOMEM;
610 goto err_create_debug_entry;
611 }
612 uhci->dentry = dentry;
613 } 617 }
618 uhci->dentry = dentry;
619#endif
614 620
615 uhci->frame = dma_alloc_coherent(uhci_dev(uhci), 621 uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
616 UHCI_NUMFRAMES * sizeof(*uhci->frame), 622 UHCI_NUMFRAMES * sizeof(*uhci->frame),
@@ -691,7 +697,9 @@ static int uhci_start(struct usb_hcd *hcd)
691 697
692 configure_hc(uhci); 698 configure_hc(uhci);
693 uhci->is_initialized = 1; 699 uhci->is_initialized = 1;
700 spin_lock_irq(&uhci->lock);
694 start_rh(uhci); 701 start_rh(uhci);
702 spin_unlock_irq(&uhci->lock);
695 return 0; 703 return 0;
696 704
697/* 705/*
@@ -722,7 +730,6 @@ err_alloc_frame_cpu:
722err_alloc_frame: 730err_alloc_frame:
723 debugfs_remove(uhci->dentry); 731 debugfs_remove(uhci->dentry);
724 732
725err_create_debug_entry:
726 return retval; 733 return retval;
727} 734}
728 735
@@ -731,7 +738,7 @@ static void uhci_stop(struct usb_hcd *hcd)
731 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 738 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
732 739
733 spin_lock_irq(&uhci->lock); 740 spin_lock_irq(&uhci->lock);
734 if (test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) && !uhci->dead) 741 if (HCD_HW_ACCESSIBLE(hcd) && !uhci->dead)
735 uhci_hc_died(uhci); 742 uhci_hc_died(uhci);
736 uhci_scan_schedule(uhci); 743 uhci_scan_schedule(uhci);
737 spin_unlock_irq(&uhci->lock); 744 spin_unlock_irq(&uhci->lock);
@@ -748,7 +755,7 @@ static int uhci_rh_suspend(struct usb_hcd *hcd)
748 int rc = 0; 755 int rc = 0;
749 756
750 spin_lock_irq(&uhci->lock); 757 spin_lock_irq(&uhci->lock);
751 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) 758 if (!HCD_HW_ACCESSIBLE(hcd))
752 rc = -ESHUTDOWN; 759 rc = -ESHUTDOWN;
753 else if (uhci->dead) 760 else if (uhci->dead)
754 ; /* Dead controllers tell no tales */ 761 ; /* Dead controllers tell no tales */
@@ -775,7 +782,7 @@ static int uhci_rh_resume(struct usb_hcd *hcd)
775 int rc = 0; 782 int rc = 0;
776 783
777 spin_lock_irq(&uhci->lock); 784 spin_lock_irq(&uhci->lock);
778 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) 785 if (!HCD_HW_ACCESSIBLE(hcd))
779 rc = -ESHUTDOWN; 786 rc = -ESHUTDOWN;
780 else if (!uhci->dead) 787 else if (!uhci->dead)
781 wakeup_rh(uhci); 788 wakeup_rh(uhci);
@@ -783,15 +790,16 @@ static int uhci_rh_resume(struct usb_hcd *hcd)
783 return rc; 790 return rc;
784} 791}
785 792
786static int uhci_pci_suspend(struct usb_hcd *hcd) 793static int uhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
787{ 794{
788 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 795 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
796 struct pci_dev *pdev = to_pci_dev(uhci_dev(uhci));
789 int rc = 0; 797 int rc = 0;
790 798
791 dev_dbg(uhci_dev(uhci), "%s\n", __func__); 799 dev_dbg(uhci_dev(uhci), "%s\n", __func__);
792 800
793 spin_lock_irq(&uhci->lock); 801 spin_lock_irq(&uhci->lock);
794 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) || uhci->dead) 802 if (!HCD_HW_ACCESSIBLE(hcd) || uhci->dead)
795 goto done_okay; /* Already suspended or dead */ 803 goto done_okay; /* Already suspended or dead */
796 804
797 if (uhci->rh_state > UHCI_RH_SUSPENDED) { 805 if (uhci->rh_state > UHCI_RH_SUSPENDED) {
@@ -803,11 +811,15 @@ static int uhci_pci_suspend(struct usb_hcd *hcd)
803 /* All PCI host controllers are required to disable IRQ generation 811 /* All PCI host controllers are required to disable IRQ generation
804 * at the source, so we must turn off PIRQ. 812 * at the source, so we must turn off PIRQ.
805 */ 813 */
806 pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP, 0); 814 pci_write_config_word(pdev, USBLEGSUP, 0);
807 mb(); 815 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
808 hcd->poll_rh = 0; 816
809 817 /* Enable platform-specific non-PME# wakeup */
810 /* FIXME: Enable non-PME# remote wakeup? */ 818 if (do_wakeup) {
819 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
820 pci_write_config_byte(pdev, USBRES_INTEL,
821 USBPORT1EN | USBPORT2EN);
822 }
811 823
812done_okay: 824done_okay:
813 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 825 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
@@ -826,7 +838,6 @@ static int uhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
826 * even if the controller was dead. 838 * even if the controller was dead.
827 */ 839 */
828 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 840 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
829 mb();
830 841
831 spin_lock_irq(&uhci->lock); 842 spin_lock_irq(&uhci->lock);
832 843
@@ -834,8 +845,6 @@ static int uhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
834 if (hibernated) 845 if (hibernated)
835 uhci_hc_died(uhci); 846 uhci_hc_died(uhci);
836 847
837 /* FIXME: Disable non-PME# remote wakeup? */
838
839 /* The firmware or a boot kernel may have changed the controller 848 /* The firmware or a boot kernel may have changed the controller
840 * settings during a system wakeup. Check it and reconfigure 849 * settings during a system wakeup. Check it and reconfigure
841 * to avoid problems. 850 * to avoid problems.
@@ -845,22 +854,20 @@ static int uhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
845 /* If the controller was dead before, it's back alive now */ 854 /* If the controller was dead before, it's back alive now */
846 configure_hc(uhci); 855 configure_hc(uhci);
847 856
848 if (uhci->rh_state == UHCI_RH_RESET) { 857 /* Tell the core if the controller had to be reset */
849 858 if (uhci->rh_state == UHCI_RH_RESET)
850 /* The controller had to be reset */
851 usb_root_hub_lost_power(hcd->self.root_hub); 859 usb_root_hub_lost_power(hcd->self.root_hub);
852 suspend_rh(uhci, UHCI_RH_SUSPENDED);
853 }
854 860
855 spin_unlock_irq(&uhci->lock); 861 spin_unlock_irq(&uhci->lock);
856 862
857 /* If interrupts don't work and remote wakeup is enabled then 863 /* If interrupts don't work and remote wakeup is enabled then
858 * the suspended root hub needs to be polled. 864 * the suspended root hub needs to be polled.
859 */ 865 */
860 if (!uhci->RD_enable && hcd->self.root_hub->do_remote_wakeup) { 866 if (!uhci->RD_enable && hcd->self.root_hub->do_remote_wakeup)
861 hcd->poll_rh = 1; 867 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
862 usb_hcd_poll_rh_status(hcd); 868
863 } 869 /* Does the root hub have a port wakeup pending? */
870 usb_hcd_poll_rh_status(hcd);
864 return 0; 871 return 0;
865} 872}
866#endif 873#endif
diff --git a/drivers/usb/host/uhci-hcd.h b/drivers/usb/host/uhci-hcd.h
index 26bd1b2bcbf..49bf2790f9c 100644
--- a/drivers/usb/host/uhci-hcd.h
+++ b/drivers/usb/host/uhci-hcd.h
@@ -67,12 +67,17 @@
67#define USBPORTSC_RES3 0x4000 /* reserved, write zeroes */ 67#define USBPORTSC_RES3 0x4000 /* reserved, write zeroes */
68#define USBPORTSC_RES4 0x8000 /* reserved, write zeroes */ 68#define USBPORTSC_RES4 0x8000 /* reserved, write zeroes */
69 69
70/* Legacy support register */ 70/* PCI legacy support register */
71#define USBLEGSUP 0xc0 71#define USBLEGSUP 0xc0
72#define USBLEGSUP_DEFAULT 0x2000 /* only PIRQ enable set */ 72#define USBLEGSUP_DEFAULT 0x2000 /* only PIRQ enable set */
73#define USBLEGSUP_RWC 0x8f00 /* the R/WC bits */ 73#define USBLEGSUP_RWC 0x8f00 /* the R/WC bits */
74#define USBLEGSUP_RO 0x5040 /* R/O and reserved bits */ 74#define USBLEGSUP_RO 0x5040 /* R/O and reserved bits */
75 75
76/* PCI Intel-specific resume-enable register */
77#define USBRES_INTEL 0xc4
78#define USBPORT1EN 0x01
79#define USBPORT2EN 0x02
80
76#define UHCI_PTR_BITS cpu_to_le32(0x000F) 81#define UHCI_PTR_BITS cpu_to_le32(0x000F)
77#define UHCI_PTR_TERM cpu_to_le32(0x0001) 82#define UHCI_PTR_TERM cpu_to_le32(0x0001)
78#define UHCI_PTR_QH cpu_to_le32(0x0002) 83#define UHCI_PTR_QH cpu_to_le32(0x0002)
diff --git a/drivers/usb/host/uhci-hub.c b/drivers/usb/host/uhci-hub.c
index 8270055848c..6d59c0f77f2 100644
--- a/drivers/usb/host/uhci-hub.c
+++ b/drivers/usb/host/uhci-hub.c
@@ -190,7 +190,7 @@ static int uhci_hub_status_data(struct usb_hcd *hcd, char *buf)
190 spin_lock_irqsave(&uhci->lock, flags); 190 spin_lock_irqsave(&uhci->lock, flags);
191 191
192 uhci_scan_schedule(uhci); 192 uhci_scan_schedule(uhci);
193 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) || uhci->dead) 193 if (!HCD_HW_ACCESSIBLE(hcd) || uhci->dead)
194 goto done; 194 goto done;
195 uhci_check_ports(uhci); 195 uhci_check_ports(uhci);
196 196
@@ -200,7 +200,7 @@ static int uhci_hub_status_data(struct usb_hcd *hcd, char *buf)
200 case UHCI_RH_SUSPENDING: 200 case UHCI_RH_SUSPENDING:
201 case UHCI_RH_SUSPENDED: 201 case UHCI_RH_SUSPENDED:
202 /* if port change, ask to be resumed */ 202 /* if port change, ask to be resumed */
203 if (status) 203 if (status || uhci->resuming_ports)
204 usb_hcd_resume_root_hub(hcd); 204 usb_hcd_resume_root_hub(hcd);
205 break; 205 break;
206 206
@@ -246,7 +246,7 @@ static int uhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
246 u16 wPortChange, wPortStatus; 246 u16 wPortChange, wPortStatus;
247 unsigned long flags; 247 unsigned long flags;
248 248
249 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) || uhci->dead) 249 if (!HCD_HW_ACCESSIBLE(hcd) || uhci->dead)
250 return -ETIMEDOUT; 250 return -ETIMEDOUT;
251 251
252 spin_lock_irqsave(&uhci->lock, flags); 252 spin_lock_irqsave(&uhci->lock, flags);
diff --git a/drivers/usb/host/uhci-q.c b/drivers/usb/host/uhci-q.c
index acd582c0280..d3ade401848 100644
--- a/drivers/usb/host/uhci-q.c
+++ b/drivers/usb/host/uhci-q.c
@@ -565,7 +565,7 @@ static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
565 qh->unlink_frame = uhci->frame_number; 565 qh->unlink_frame = uhci->frame_number;
566 566
567 /* Force an interrupt so we know when the QH is fully unlinked */ 567 /* Force an interrupt so we know when the QH is fully unlinked */
568 if (list_empty(&uhci->skel_unlink_qh->node)) 568 if (list_empty(&uhci->skel_unlink_qh->node) || uhci->is_stopped)
569 uhci_set_next_interrupt(uhci); 569 uhci_set_next_interrupt(uhci);
570 570
571 /* Move the QH from its old list to the end of the unlinking list */ 571 /* Move the QH from its old list to the end of the unlinking list */
@@ -1667,7 +1667,7 @@ static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
1667 qh->advance_jiffies = jiffies; 1667 qh->advance_jiffies = jiffies;
1668 goto done; 1668 goto done;
1669 } 1669 }
1670 ret = 0; 1670 ret = uhci->is_stopped;
1671 } 1671 }
1672 1672
1673 /* The queue hasn't advanced; check for timeout */ 1673 /* The queue hasn't advanced; check for timeout */
diff --git a/drivers/usb/host/whci/hcd.c b/drivers/usb/host/whci/hcd.c
index e0d3401285c..72b6892fda6 100644
--- a/drivers/usb/host/whci/hcd.c
+++ b/drivers/usb/host/whci/hcd.c
@@ -68,7 +68,7 @@ static int whc_start(struct usb_hcd *usb_hcd)
68 whc_write_wusbcmd(whc, WUSBCMD_RUN, WUSBCMD_RUN); 68 whc_write_wusbcmd(whc, WUSBCMD_RUN, WUSBCMD_RUN);
69 69
70 usb_hcd->uses_new_polling = 1; 70 usb_hcd->uses_new_polling = 1;
71 usb_hcd->poll_rh = 1; 71 set_bit(HCD_FLAG_POLL_RH, &usb_hcd->flags);
72 usb_hcd->state = HC_STATE_RUNNING; 72 usb_hcd->state = HC_STATE_RUNNING;
73 73
74out: 74out:
diff --git a/drivers/usb/host/whci/qset.c b/drivers/usb/host/whci/qset.c
index ab5a14fbfee..dc0ab8382f5 100644
--- a/drivers/usb/host/whci/qset.c
+++ b/drivers/usb/host/whci/qset.c
@@ -475,7 +475,7 @@ static int qset_add_urb_sg(struct whc *whc, struct whc_qset *qset, struct urb *u
475 || (prev_end & (WHCI_PAGE_SIZE-1)) 475 || (prev_end & (WHCI_PAGE_SIZE-1))
476 || (dma_addr & (WHCI_PAGE_SIZE-1)) 476 || (dma_addr & (WHCI_PAGE_SIZE-1))
477 || std->len + WHCI_PAGE_SIZE > QTD_MAX_XFER_SIZE) { 477 || std->len + WHCI_PAGE_SIZE > QTD_MAX_XFER_SIZE) {
478 if (std->len % qset->max_packet != 0) 478 if (std && std->len % qset->max_packet != 0)
479 return -EINVAL; 479 return -EINVAL;
480 std = qset_new_std(whc, qset, urb, mem_flags); 480 std = qset_new_std(whc, qset, urb, mem_flags);
481 if (std == NULL) { 481 if (std == NULL) {
diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 2eb658d2639..4e51343ddff 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -391,49 +391,6 @@ struct xhci_ring *xhci_stream_id_to_ring(
391 return ep->stream_info->stream_rings[stream_id]; 391 return ep->stream_info->stream_rings[stream_id];
392} 392}
393 393
394struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
395 unsigned int slot_id, unsigned int ep_index,
396 unsigned int stream_id)
397{
398 struct xhci_virt_ep *ep;
399
400 ep = &xhci->devs[slot_id]->eps[ep_index];
401 /* Common case: no streams */
402 if (!(ep->ep_state & EP_HAS_STREAMS))
403 return ep->ring;
404
405 if (stream_id == 0) {
406 xhci_warn(xhci,
407 "WARN: Slot ID %u, ep index %u has streams, "
408 "but URB has no stream ID.\n",
409 slot_id, ep_index);
410 return NULL;
411 }
412
413 if (stream_id < ep->stream_info->num_streams)
414 return ep->stream_info->stream_rings[stream_id];
415
416 xhci_warn(xhci,
417 "WARN: Slot ID %u, ep index %u has "
418 "stream IDs 1 to %u allocated, "
419 "but stream ID %u is requested.\n",
420 slot_id, ep_index,
421 ep->stream_info->num_streams - 1,
422 stream_id);
423 return NULL;
424}
425
426/* Get the right ring for the given URB.
427 * If the endpoint supports streams, boundary check the URB's stream ID.
428 * If the endpoint doesn't support streams, return the singular endpoint ring.
429 */
430struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
431 struct urb *urb)
432{
433 return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id,
434 xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id);
435}
436
437#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING 394#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
438static int xhci_test_radix_tree(struct xhci_hcd *xhci, 395static int xhci_test_radix_tree(struct xhci_hcd *xhci,
439 unsigned int num_streams, 396 unsigned int num_streams,
@@ -1112,8 +1069,18 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
1112 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); 1069 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
1113 1070
1114 /* Set up the endpoint ring */ 1071 /* Set up the endpoint ring */
1115 virt_dev->eps[ep_index].new_ring = 1072 /*
1116 xhci_ring_alloc(xhci, 1, true, mem_flags); 1073 * Isochronous endpoint ring needs bigger size because one isoc URB
1074 * carries multiple packets and it will insert multiple tds to the
1075 * ring.
1076 * This should be replaced with dynamic ring resizing in the future.
1077 */
1078 if (usb_endpoint_xfer_isoc(&ep->desc))
1079 virt_dev->eps[ep_index].new_ring =
1080 xhci_ring_alloc(xhci, 8, true, mem_flags);
1081 else
1082 virt_dev->eps[ep_index].new_ring =
1083 xhci_ring_alloc(xhci, 1, true, mem_flags);
1117 if (!virt_dev->eps[ep_index].new_ring) { 1084 if (!virt_dev->eps[ep_index].new_ring) {
1118 /* Attempt to use the ring cache */ 1085 /* Attempt to use the ring cache */
1119 if (virt_dev->num_rings_cached == 0) 1086 if (virt_dev->num_rings_cached == 0)
@@ -1124,6 +1091,7 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
1124 virt_dev->num_rings_cached--; 1091 virt_dev->num_rings_cached--;
1125 xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring); 1092 xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring);
1126 } 1093 }
1094 virt_dev->eps[ep_index].skip = false;
1127 ep_ring = virt_dev->eps[ep_index].new_ring; 1095 ep_ring = virt_dev->eps[ep_index].new_ring;
1128 ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state; 1096 ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state;
1129 1097
@@ -1389,6 +1357,22 @@ struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
1389 return command; 1357 return command;
1390} 1358}
1391 1359
1360void xhci_urb_free_priv(struct xhci_hcd *xhci, struct urb_priv *urb_priv)
1361{
1362 int last;
1363
1364 if (!urb_priv)
1365 return;
1366
1367 last = urb_priv->length - 1;
1368 if (last >= 0) {
1369 int i;
1370 for (i = 0; i <= last; i++)
1371 kfree(urb_priv->td[i]);
1372 }
1373 kfree(urb_priv);
1374}
1375
1392void xhci_free_command(struct xhci_hcd *xhci, 1376void xhci_free_command(struct xhci_hcd *xhci,
1393 struct xhci_command *command) 1377 struct xhci_command *command)
1394{ 1378{
@@ -1588,7 +1572,7 @@ static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
1588 unsigned int num_tests; 1572 unsigned int num_tests;
1589 int i, ret; 1573 int i, ret;
1590 1574
1591 num_tests = sizeof(simple_test_vector) / sizeof(simple_test_vector[0]); 1575 num_tests = ARRAY_SIZE(simple_test_vector);
1592 for (i = 0; i < num_tests; i++) { 1576 for (i = 0; i < num_tests; i++) {
1593 ret = xhci_test_trb_in_td(xhci, 1577 ret = xhci_test_trb_in_td(xhci,
1594 xhci->event_ring->first_seg, 1578 xhci->event_ring->first_seg,
@@ -1601,7 +1585,7 @@ static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
1601 return ret; 1585 return ret;
1602 } 1586 }
1603 1587
1604 num_tests = sizeof(complex_test_vector) / sizeof(complex_test_vector[0]); 1588 num_tests = ARRAY_SIZE(complex_test_vector);
1605 for (i = 0; i < num_tests; i++) { 1589 for (i = 0; i < num_tests; i++) {
1606 ret = xhci_test_trb_in_td(xhci, 1590 ret = xhci_test_trb_in_td(xhci,
1607 complex_test_vector[i].input_seg, 1591 complex_test_vector[i].input_seg,
@@ -1617,6 +1601,29 @@ static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
1617 return 0; 1601 return 0;
1618} 1602}
1619 1603
1604static void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
1605{
1606 u64 temp;
1607 dma_addr_t deq;
1608
1609 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
1610 xhci->event_ring->dequeue);
1611 if (deq == 0 && !in_interrupt())
1612 xhci_warn(xhci, "WARN something wrong with SW event ring "
1613 "dequeue ptr.\n");
1614 /* Update HC event ring dequeue pointer */
1615 temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
1616 temp &= ERST_PTR_MASK;
1617 /* Don't clear the EHB bit (which is RW1C) because
1618 * there might be more events to service.
1619 */
1620 temp &= ~ERST_EHB;
1621 xhci_dbg(xhci, "// Write event ring dequeue pointer, "
1622 "preserving EHB bit\n");
1623 xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
1624 &xhci->ir_set->erst_dequeue);
1625}
1626
1620 1627
1621int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) 1628int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
1622{ 1629{
diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
index 11482b6b938..f7efe025bed 100644
--- a/drivers/usb/host/xhci-pci.c
+++ b/drivers/usb/host/xhci-pci.c
@@ -53,6 +53,7 @@ static int xhci_pci_setup(struct usb_hcd *hcd)
53 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 53 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
54 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 54 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
55 int retval; 55 int retval;
56 u32 temp;
56 57
57 hcd->self.sg_tablesize = TRBS_PER_SEGMENT - 2; 58 hcd->self.sg_tablesize = TRBS_PER_SEGMENT - 2;
58 59
@@ -93,6 +94,14 @@ static int xhci_pci_setup(struct usb_hcd *hcd)
93 return retval; 94 return retval;
94 xhci_dbg(xhci, "Reset complete\n"); 95 xhci_dbg(xhci, "Reset complete\n");
95 96
97 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
98 if (HCC_64BIT_ADDR(temp)) {
99 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
100 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
101 } else {
102 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
103 }
104
96 xhci_dbg(xhci, "Calling HCD init\n"); 105 xhci_dbg(xhci, "Calling HCD init\n");
97 /* Initialize HCD and host controller data structures. */ 106 /* Initialize HCD and host controller data structures. */
98 retval = xhci_init(hcd); 107 retval = xhci_init(hcd);
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index bfc99a93945..bc3f4f42706 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -301,28 +301,6 @@ static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
301 return 1; 301 return 1;
302} 302}
303 303
304void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
305{
306 u64 temp;
307 dma_addr_t deq;
308
309 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
310 xhci->event_ring->dequeue);
311 if (deq == 0 && !in_interrupt())
312 xhci_warn(xhci, "WARN something wrong with SW event ring "
313 "dequeue ptr.\n");
314 /* Update HC event ring dequeue pointer */
315 temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
316 temp &= ERST_PTR_MASK;
317 /* Don't clear the EHB bit (which is RW1C) because
318 * there might be more events to service.
319 */
320 temp &= ~ERST_EHB;
321 xhci_dbg(xhci, "// Write event ring dequeue pointer, preserving EHB bit\n");
322 xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
323 &xhci->ir_set->erst_dequeue);
324}
325
326/* Ring the host controller doorbell after placing a command on the ring */ 304/* Ring the host controller doorbell after placing a command on the ring */
327void xhci_ring_cmd_db(struct xhci_hcd *xhci) 305void xhci_ring_cmd_db(struct xhci_hcd *xhci)
328{ 306{
@@ -359,11 +337,6 @@ static void ring_ep_doorbell(struct xhci_hcd *xhci,
359 field = xhci_readl(xhci, db_addr) & DB_MASK; 337 field = xhci_readl(xhci, db_addr) & DB_MASK;
360 field |= EPI_TO_DB(ep_index) | STREAM_ID_TO_DB(stream_id); 338 field |= EPI_TO_DB(ep_index) | STREAM_ID_TO_DB(stream_id);
361 xhci_writel(xhci, field, db_addr); 339 xhci_writel(xhci, field, db_addr);
362 /* Flush PCI posted writes - FIXME Matthew Wilcox says this
363 * isn't time-critical and we shouldn't make the CPU wait for
364 * the flush.
365 */
366 xhci_readl(xhci, db_addr);
367 } 340 }
368} 341}
369 342
@@ -419,6 +392,50 @@ static struct xhci_segment *find_trb_seg(
419 return cur_seg; 392 return cur_seg;
420} 393}
421 394
395
396static struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
397 unsigned int slot_id, unsigned int ep_index,
398 unsigned int stream_id)
399{
400 struct xhci_virt_ep *ep;
401
402 ep = &xhci->devs[slot_id]->eps[ep_index];
403 /* Common case: no streams */
404 if (!(ep->ep_state & EP_HAS_STREAMS))
405 return ep->ring;
406
407 if (stream_id == 0) {
408 xhci_warn(xhci,
409 "WARN: Slot ID %u, ep index %u has streams, "
410 "but URB has no stream ID.\n",
411 slot_id, ep_index);
412 return NULL;
413 }
414
415 if (stream_id < ep->stream_info->num_streams)
416 return ep->stream_info->stream_rings[stream_id];
417
418 xhci_warn(xhci,
419 "WARN: Slot ID %u, ep index %u has "
420 "stream IDs 1 to %u allocated, "
421 "but stream ID %u is requested.\n",
422 slot_id, ep_index,
423 ep->stream_info->num_streams - 1,
424 stream_id);
425 return NULL;
426}
427
428/* Get the right ring for the given URB.
429 * If the endpoint supports streams, boundary check the URB's stream ID.
430 * If the endpoint doesn't support streams, return the singular endpoint ring.
431 */
432static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
433 struct urb *urb)
434{
435 return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id,
436 xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id);
437}
438
422/* 439/*
423 * Move the xHC's endpoint ring dequeue pointer past cur_td. 440 * Move the xHC's endpoint ring dequeue pointer past cur_td.
424 * Record the new state of the xHC's endpoint ring dequeue segment, 441 * Record the new state of the xHC's endpoint ring dequeue segment,
@@ -578,16 +595,24 @@ static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
578 struct xhci_td *cur_td, int status, char *adjective) 595 struct xhci_td *cur_td, int status, char *adjective)
579{ 596{
580 struct usb_hcd *hcd = xhci_to_hcd(xhci); 597 struct usb_hcd *hcd = xhci_to_hcd(xhci);
598 struct urb *urb;
599 struct urb_priv *urb_priv;
581 600
582 cur_td->urb->hcpriv = NULL; 601 urb = cur_td->urb;
583 usb_hcd_unlink_urb_from_ep(hcd, cur_td->urb); 602 urb_priv = urb->hcpriv;
584 xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, cur_td->urb); 603 urb_priv->td_cnt++;
585 604
586 spin_unlock(&xhci->lock); 605 /* Only giveback urb when this is the last td in urb */
587 usb_hcd_giveback_urb(hcd, cur_td->urb, status); 606 if (urb_priv->td_cnt == urb_priv->length) {
588 kfree(cur_td); 607 usb_hcd_unlink_urb_from_ep(hcd, urb);
589 spin_lock(&xhci->lock); 608 xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, urb);
590 xhci_dbg(xhci, "%s URB given back\n", adjective); 609
610 spin_unlock(&xhci->lock);
611 usb_hcd_giveback_urb(hcd, urb, status);
612 xhci_urb_free_priv(xhci, urb_priv);
613 spin_lock(&xhci->lock);
614 xhci_dbg(xhci, "%s URB given back\n", adjective);
615 }
591} 616}
592 617
593/* 618/*
@@ -1132,7 +1157,6 @@ static void handle_port_status(struct xhci_hcd *xhci,
1132 1157
1133 /* Update event ring dequeue pointer before dropping the lock */ 1158 /* Update event ring dequeue pointer before dropping the lock */
1134 inc_deq(xhci, xhci->event_ring, true); 1159 inc_deq(xhci, xhci->event_ring, true);
1135 xhci_set_hc_event_deq(xhci);
1136 1160
1137 spin_unlock(&xhci->lock); 1161 spin_unlock(&xhci->lock);
1138 /* Pass this up to the core */ 1162 /* Pass this up to the core */
@@ -1258,6 +1282,421 @@ int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
1258} 1282}
1259 1283
1260/* 1284/*
1285 * Finish the td processing, remove the td from td list;
1286 * Return 1 if the urb can be given back.
1287 */
1288static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
1289 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1290 struct xhci_virt_ep *ep, int *status, bool skip)
1291{
1292 struct xhci_virt_device *xdev;
1293 struct xhci_ring *ep_ring;
1294 unsigned int slot_id;
1295 int ep_index;
1296 struct urb *urb = NULL;
1297 struct xhci_ep_ctx *ep_ctx;
1298 int ret = 0;
1299 struct urb_priv *urb_priv;
1300 u32 trb_comp_code;
1301
1302 slot_id = TRB_TO_SLOT_ID(event->flags);
1303 xdev = xhci->devs[slot_id];
1304 ep_index = TRB_TO_EP_ID(event->flags) - 1;
1305 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
1306 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
1307 trb_comp_code = GET_COMP_CODE(event->transfer_len);
1308
1309 if (skip)
1310 goto td_cleanup;
1311
1312 if (trb_comp_code == COMP_STOP_INVAL ||
1313 trb_comp_code == COMP_STOP) {
1314 /* The Endpoint Stop Command completion will take care of any
1315 * stopped TDs. A stopped TD may be restarted, so don't update
1316 * the ring dequeue pointer or take this TD off any lists yet.
1317 */
1318 ep->stopped_td = td;
1319 ep->stopped_trb = event_trb;
1320 return 0;
1321 } else {
1322 if (trb_comp_code == COMP_STALL) {
1323 /* The transfer is completed from the driver's
1324 * perspective, but we need to issue a set dequeue
1325 * command for this stalled endpoint to move the dequeue
1326 * pointer past the TD. We can't do that here because
1327 * the halt condition must be cleared first. Let the
1328 * USB class driver clear the stall later.
1329 */
1330 ep->stopped_td = td;
1331 ep->stopped_trb = event_trb;
1332 ep->stopped_stream = ep_ring->stream_id;
1333 } else if (xhci_requires_manual_halt_cleanup(xhci,
1334 ep_ctx, trb_comp_code)) {
1335 /* Other types of errors halt the endpoint, but the
1336 * class driver doesn't call usb_reset_endpoint() unless
1337 * the error is -EPIPE. Clear the halted status in the
1338 * xHCI hardware manually.
1339 */
1340 xhci_cleanup_halted_endpoint(xhci,
1341 slot_id, ep_index, ep_ring->stream_id,
1342 td, event_trb);
1343 } else {
1344 /* Update ring dequeue pointer */
1345 while (ep_ring->dequeue != td->last_trb)
1346 inc_deq(xhci, ep_ring, false);
1347 inc_deq(xhci, ep_ring, false);
1348 }
1349
1350td_cleanup:
1351 /* Clean up the endpoint's TD list */
1352 urb = td->urb;
1353 urb_priv = urb->hcpriv;
1354
1355 /* Do one last check of the actual transfer length.
1356 * If the host controller said we transferred more data than
1357 * the buffer length, urb->actual_length will be a very big
1358 * number (since it's unsigned). Play it safe and say we didn't
1359 * transfer anything.
1360 */
1361 if (urb->actual_length > urb->transfer_buffer_length) {
1362 xhci_warn(xhci, "URB transfer length is wrong, "
1363 "xHC issue? req. len = %u, "
1364 "act. len = %u\n",
1365 urb->transfer_buffer_length,
1366 urb->actual_length);
1367 urb->actual_length = 0;
1368 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1369 *status = -EREMOTEIO;
1370 else
1371 *status = 0;
1372 }
1373 list_del(&td->td_list);
1374 /* Was this TD slated to be cancelled but completed anyway? */
1375 if (!list_empty(&td->cancelled_td_list))
1376 list_del(&td->cancelled_td_list);
1377
1378 urb_priv->td_cnt++;
1379 /* Giveback the urb when all the tds are completed */
1380 if (urb_priv->td_cnt == urb_priv->length)
1381 ret = 1;
1382 }
1383
1384 return ret;
1385}
1386
1387/*
1388 * Process control tds, update urb status and actual_length.
1389 */
1390static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
1391 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1392 struct xhci_virt_ep *ep, int *status)
1393{
1394 struct xhci_virt_device *xdev;
1395 struct xhci_ring *ep_ring;
1396 unsigned int slot_id;
1397 int ep_index;
1398 struct xhci_ep_ctx *ep_ctx;
1399 u32 trb_comp_code;
1400
1401 slot_id = TRB_TO_SLOT_ID(event->flags);
1402 xdev = xhci->devs[slot_id];
1403 ep_index = TRB_TO_EP_ID(event->flags) - 1;
1404 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
1405 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
1406 trb_comp_code = GET_COMP_CODE(event->transfer_len);
1407
1408 xhci_debug_trb(xhci, xhci->event_ring->dequeue);
1409 switch (trb_comp_code) {
1410 case COMP_SUCCESS:
1411 if (event_trb == ep_ring->dequeue) {
1412 xhci_warn(xhci, "WARN: Success on ctrl setup TRB "
1413 "without IOC set??\n");
1414 *status = -ESHUTDOWN;
1415 } else if (event_trb != td->last_trb) {
1416 xhci_warn(xhci, "WARN: Success on ctrl data TRB "
1417 "without IOC set??\n");
1418 *status = -ESHUTDOWN;
1419 } else {
1420 xhci_dbg(xhci, "Successful control transfer!\n");
1421 *status = 0;
1422 }
1423 break;
1424 case COMP_SHORT_TX:
1425 xhci_warn(xhci, "WARN: short transfer on control ep\n");
1426 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1427 *status = -EREMOTEIO;
1428 else
1429 *status = 0;
1430 break;
1431 default:
1432 if (!xhci_requires_manual_halt_cleanup(xhci,
1433 ep_ctx, trb_comp_code))
1434 break;
1435 xhci_dbg(xhci, "TRB error code %u, "
1436 "halted endpoint index = %u\n",
1437 trb_comp_code, ep_index);
1438 /* else fall through */
1439 case COMP_STALL:
1440 /* Did we transfer part of the data (middle) phase? */
1441 if (event_trb != ep_ring->dequeue &&
1442 event_trb != td->last_trb)
1443 td->urb->actual_length =
1444 td->urb->transfer_buffer_length
1445 - TRB_LEN(event->transfer_len);
1446 else
1447 td->urb->actual_length = 0;
1448
1449 xhci_cleanup_halted_endpoint(xhci,
1450 slot_id, ep_index, 0, td, event_trb);
1451 return finish_td(xhci, td, event_trb, event, ep, status, true);
1452 }
1453 /*
1454 * Did we transfer any data, despite the errors that might have
1455 * happened? I.e. did we get past the setup stage?
1456 */
1457 if (event_trb != ep_ring->dequeue) {
1458 /* The event was for the status stage */
1459 if (event_trb == td->last_trb) {
1460 if (td->urb->actual_length != 0) {
1461 /* Don't overwrite a previously set error code
1462 */
1463 if ((*status == -EINPROGRESS || *status == 0) &&
1464 (td->urb->transfer_flags
1465 & URB_SHORT_NOT_OK))
1466 /* Did we already see a short data
1467 * stage? */
1468 *status = -EREMOTEIO;
1469 } else {
1470 td->urb->actual_length =
1471 td->urb->transfer_buffer_length;
1472 }
1473 } else {
1474 /* Maybe the event was for the data stage? */
1475 if (trb_comp_code != COMP_STOP_INVAL) {
1476 /* We didn't stop on a link TRB in the middle */
1477 td->urb->actual_length =
1478 td->urb->transfer_buffer_length -
1479 TRB_LEN(event->transfer_len);
1480 xhci_dbg(xhci, "Waiting for status "
1481 "stage event\n");
1482 return 0;
1483 }
1484 }
1485 }
1486
1487 return finish_td(xhci, td, event_trb, event, ep, status, false);
1488}
1489
1490/*
1491 * Process isochronous tds, update urb packet status and actual_length.
1492 */
1493static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
1494 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1495 struct xhci_virt_ep *ep, int *status)
1496{
1497 struct xhci_ring *ep_ring;
1498 struct urb_priv *urb_priv;
1499 int idx;
1500 int len = 0;
1501 int skip_td = 0;
1502 union xhci_trb *cur_trb;
1503 struct xhci_segment *cur_seg;
1504 u32 trb_comp_code;
1505
1506 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
1507 trb_comp_code = GET_COMP_CODE(event->transfer_len);
1508 urb_priv = td->urb->hcpriv;
1509 idx = urb_priv->td_cnt;
1510
1511 if (ep->skip) {
1512 /* The transfer is partly done */
1513 *status = -EXDEV;
1514 td->urb->iso_frame_desc[idx].status = -EXDEV;
1515 } else {
1516 /* handle completion code */
1517 switch (trb_comp_code) {
1518 case COMP_SUCCESS:
1519 td->urb->iso_frame_desc[idx].status = 0;
1520 xhci_dbg(xhci, "Successful isoc transfer!\n");
1521 break;
1522 case COMP_SHORT_TX:
1523 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1524 td->urb->iso_frame_desc[idx].status =
1525 -EREMOTEIO;
1526 else
1527 td->urb->iso_frame_desc[idx].status = 0;
1528 break;
1529 case COMP_BW_OVER:
1530 td->urb->iso_frame_desc[idx].status = -ECOMM;
1531 skip_td = 1;
1532 break;
1533 case COMP_BUFF_OVER:
1534 case COMP_BABBLE:
1535 td->urb->iso_frame_desc[idx].status = -EOVERFLOW;
1536 skip_td = 1;
1537 break;
1538 case COMP_STALL:
1539 td->urb->iso_frame_desc[idx].status = -EPROTO;
1540 skip_td = 1;
1541 break;
1542 case COMP_STOP:
1543 case COMP_STOP_INVAL:
1544 break;
1545 default:
1546 td->urb->iso_frame_desc[idx].status = -1;
1547 break;
1548 }
1549 }
1550
1551 /* calc actual length */
1552 if (ep->skip) {
1553 td->urb->iso_frame_desc[idx].actual_length = 0;
1554 return finish_td(xhci, td, event_trb, event, ep, status, true);
1555 }
1556
1557 if (trb_comp_code == COMP_SUCCESS || skip_td == 1) {
1558 td->urb->iso_frame_desc[idx].actual_length =
1559 td->urb->iso_frame_desc[idx].length;
1560 td->urb->actual_length +=
1561 td->urb->iso_frame_desc[idx].length;
1562 } else {
1563 for (cur_trb = ep_ring->dequeue,
1564 cur_seg = ep_ring->deq_seg; cur_trb != event_trb;
1565 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
1566 if ((cur_trb->generic.field[3] &
1567 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_TR_NOOP) &&
1568 (cur_trb->generic.field[3] &
1569 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_LINK))
1570 len +=
1571 TRB_LEN(cur_trb->generic.field[2]);
1572 }
1573 len += TRB_LEN(cur_trb->generic.field[2]) -
1574 TRB_LEN(event->transfer_len);
1575
1576 if (trb_comp_code != COMP_STOP_INVAL) {
1577 td->urb->iso_frame_desc[idx].actual_length = len;
1578 td->urb->actual_length += len;
1579 }
1580 }
1581
1582 if ((idx == urb_priv->length - 1) && *status == -EINPROGRESS)
1583 *status = 0;
1584
1585 return finish_td(xhci, td, event_trb, event, ep, status, false);
1586}
1587
1588/*
1589 * Process bulk and interrupt tds, update urb status and actual_length.
1590 */
1591static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
1592 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1593 struct xhci_virt_ep *ep, int *status)
1594{
1595 struct xhci_ring *ep_ring;
1596 union xhci_trb *cur_trb;
1597 struct xhci_segment *cur_seg;
1598 u32 trb_comp_code;
1599
1600 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
1601 trb_comp_code = GET_COMP_CODE(event->transfer_len);
1602
1603 switch (trb_comp_code) {
1604 case COMP_SUCCESS:
1605 /* Double check that the HW transferred everything. */
1606 if (event_trb != td->last_trb) {
1607 xhci_warn(xhci, "WARN Successful completion "
1608 "on short TX\n");
1609 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1610 *status = -EREMOTEIO;
1611 else
1612 *status = 0;
1613 } else {
1614 if (usb_endpoint_xfer_bulk(&td->urb->ep->desc))
1615 xhci_dbg(xhci, "Successful bulk "
1616 "transfer!\n");
1617 else
1618 xhci_dbg(xhci, "Successful interrupt "
1619 "transfer!\n");
1620 *status = 0;
1621 }
1622 break;
1623 case COMP_SHORT_TX:
1624 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1625 *status = -EREMOTEIO;
1626 else
1627 *status = 0;
1628 break;
1629 default:
1630 /* Others already handled above */
1631 break;
1632 }
1633 dev_dbg(&td->urb->dev->dev,
1634 "ep %#x - asked for %d bytes, "
1635 "%d bytes untransferred\n",
1636 td->urb->ep->desc.bEndpointAddress,
1637 td->urb->transfer_buffer_length,
1638 TRB_LEN(event->transfer_len));
1639 /* Fast path - was this the last TRB in the TD for this URB? */
1640 if (event_trb == td->last_trb) {
1641 if (TRB_LEN(event->transfer_len) != 0) {
1642 td->urb->actual_length =
1643 td->urb->transfer_buffer_length -
1644 TRB_LEN(event->transfer_len);
1645 if (td->urb->transfer_buffer_length <
1646 td->urb->actual_length) {
1647 xhci_warn(xhci, "HC gave bad length "
1648 "of %d bytes left\n",
1649 TRB_LEN(event->transfer_len));
1650 td->urb->actual_length = 0;
1651 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1652 *status = -EREMOTEIO;
1653 else
1654 *status = 0;
1655 }
1656 /* Don't overwrite a previously set error code */
1657 if (*status == -EINPROGRESS) {
1658 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1659 *status = -EREMOTEIO;
1660 else
1661 *status = 0;
1662 }
1663 } else {
1664 td->urb->actual_length =
1665 td->urb->transfer_buffer_length;
1666 /* Ignore a short packet completion if the
1667 * untransferred length was zero.
1668 */
1669 if (*status == -EREMOTEIO)
1670 *status = 0;
1671 }
1672 } else {
1673 /* Slow path - walk the list, starting from the dequeue
1674 * pointer, to get the actual length transferred.
1675 */
1676 td->urb->actual_length = 0;
1677 for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
1678 cur_trb != event_trb;
1679 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
1680 if ((cur_trb->generic.field[3] &
1681 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_TR_NOOP) &&
1682 (cur_trb->generic.field[3] &
1683 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_LINK))
1684 td->urb->actual_length +=
1685 TRB_LEN(cur_trb->generic.field[2]);
1686 }
1687 /* If the ring didn't stop on a Link or No-op TRB, add
1688 * in the actual bytes transferred from the Normal TRB
1689 */
1690 if (trb_comp_code != COMP_STOP_INVAL)
1691 td->urb->actual_length +=
1692 TRB_LEN(cur_trb->generic.field[2]) -
1693 TRB_LEN(event->transfer_len);
1694 }
1695
1696 return finish_td(xhci, td, event_trb, event, ep, status, false);
1697}
1698
1699/*
1261 * If this function returns an error condition, it means it got a Transfer 1700 * If this function returns an error condition, it means it got a Transfer
1262 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address. 1701 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
1263 * At this point, the host controller is probably hosed and should be reset. 1702 * At this point, the host controller is probably hosed and should be reset.
@@ -1276,10 +1715,11 @@ static int handle_tx_event(struct xhci_hcd *xhci,
1276 union xhci_trb *event_trb; 1715 union xhci_trb *event_trb;
1277 struct urb *urb = NULL; 1716 struct urb *urb = NULL;
1278 int status = -EINPROGRESS; 1717 int status = -EINPROGRESS;
1718 struct urb_priv *urb_priv;
1279 struct xhci_ep_ctx *ep_ctx; 1719 struct xhci_ep_ctx *ep_ctx;
1280 u32 trb_comp_code; 1720 u32 trb_comp_code;
1721 int ret = 0;
1281 1722
1282 xhci_dbg(xhci, "In %s\n", __func__);
1283 slot_id = TRB_TO_SLOT_ID(event->flags); 1723 slot_id = TRB_TO_SLOT_ID(event->flags);
1284 xdev = xhci->devs[slot_id]; 1724 xdev = xhci->devs[slot_id];
1285 if (!xdev) { 1725 if (!xdev) {
@@ -1293,51 +1733,16 @@ static int handle_tx_event(struct xhci_hcd *xhci,
1293 ep = &xdev->eps[ep_index]; 1733 ep = &xdev->eps[ep_index];
1294 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer); 1734 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
1295 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); 1735 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
1296 if (!ep_ring || (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) { 1736 if (!ep_ring ||
1737 (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
1297 xhci_err(xhci, "ERROR Transfer event for disabled endpoint " 1738 xhci_err(xhci, "ERROR Transfer event for disabled endpoint "
1298 "or incorrect stream ring\n"); 1739 "or incorrect stream ring\n");
1299 return -ENODEV; 1740 return -ENODEV;
1300 } 1741 }
1301 1742
1302 event_dma = event->buffer; 1743 event_dma = event->buffer;
1303 /* This TRB should be in the TD at the head of this ring's TD list */
1304 xhci_dbg(xhci, "%s - checking for list empty\n", __func__);
1305 if (list_empty(&ep_ring->td_list)) {
1306 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
1307 TRB_TO_SLOT_ID(event->flags), ep_index);
1308 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
1309 (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
1310 xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
1311 urb = NULL;
1312 goto cleanup;
1313 }
1314 xhci_dbg(xhci, "%s - getting list entry\n", __func__);
1315 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
1316
1317 /* Is this a TRB in the currently executing TD? */
1318 xhci_dbg(xhci, "%s - looking for TD\n", __func__);
1319 event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
1320 td->last_trb, event_dma);
1321 xhci_dbg(xhci, "%s - found event_seg = %p\n", __func__, event_seg);
1322 if (!event_seg) {
1323 /* HC is busted, give up! */
1324 xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
1325 return -ESHUTDOWN;
1326 }
1327 event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
1328 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
1329 (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
1330 xhci_dbg(xhci, "Offset 0x00 (buffer lo) = 0x%x\n",
1331 lower_32_bits(event->buffer));
1332 xhci_dbg(xhci, "Offset 0x04 (buffer hi) = 0x%x\n",
1333 upper_32_bits(event->buffer));
1334 xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
1335 (unsigned int) event->transfer_len);
1336 xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
1337 (unsigned int) event->flags);
1338
1339 /* Look for common error cases */
1340 trb_comp_code = GET_COMP_CODE(event->transfer_len); 1744 trb_comp_code = GET_COMP_CODE(event->transfer_len);
1745 /* Look for common error cases */
1341 switch (trb_comp_code) { 1746 switch (trb_comp_code) {
1342 /* Skip codes that require special handling depending on 1747 /* Skip codes that require special handling depending on
1343 * transfer type 1748 * transfer type
@@ -1373,278 +1778,156 @@ static int handle_tx_event(struct xhci_hcd *xhci,
1373 xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n"); 1778 xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
1374 status = -ENOSR; 1779 status = -ENOSR;
1375 break; 1780 break;
1781 case COMP_BW_OVER:
1782 xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n");
1783 break;
1784 case COMP_BUFF_OVER:
1785 xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n");
1786 break;
1787 case COMP_UNDERRUN:
1788 /*
1789 * When the Isoch ring is empty, the xHC will generate
1790 * a Ring Overrun Event for IN Isoch endpoint or Ring
1791 * Underrun Event for OUT Isoch endpoint.
1792 */
1793 xhci_dbg(xhci, "underrun event on endpoint\n");
1794 if (!list_empty(&ep_ring->td_list))
1795 xhci_dbg(xhci, "Underrun Event for slot %d ep %d "
1796 "still with TDs queued?\n",
1797 TRB_TO_SLOT_ID(event->flags), ep_index);
1798 goto cleanup;
1799 case COMP_OVERRUN:
1800 xhci_dbg(xhci, "overrun event on endpoint\n");
1801 if (!list_empty(&ep_ring->td_list))
1802 xhci_dbg(xhci, "Overrun Event for slot %d ep %d "
1803 "still with TDs queued?\n",
1804 TRB_TO_SLOT_ID(event->flags), ep_index);
1805 goto cleanup;
1806 case COMP_MISSED_INT:
1807 /*
1808 * When encounter missed service error, one or more isoc tds
1809 * may be missed by xHC.
1810 * Set skip flag of the ep_ring; Complete the missed tds as
1811 * short transfer when process the ep_ring next time.
1812 */
1813 ep->skip = true;
1814 xhci_dbg(xhci, "Miss service interval error, set skip flag\n");
1815 goto cleanup;
1376 default: 1816 default:
1377 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) { 1817 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
1378 status = 0; 1818 status = 0;
1379 break; 1819 break;
1380 } 1820 }
1381 xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n"); 1821 xhci_warn(xhci, "ERROR Unknown event condition, HC probably "
1382 urb = NULL; 1822 "busted\n");
1383 goto cleanup; 1823 goto cleanup;
1384 } 1824 }
1385 /* Now update the urb's actual_length and give back to the core */
1386 /* Was this a control transfer? */
1387 if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
1388 xhci_debug_trb(xhci, xhci->event_ring->dequeue);
1389 switch (trb_comp_code) {
1390 case COMP_SUCCESS:
1391 if (event_trb == ep_ring->dequeue) {
1392 xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
1393 status = -ESHUTDOWN;
1394 } else if (event_trb != td->last_trb) {
1395 xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
1396 status = -ESHUTDOWN;
1397 } else {
1398 xhci_dbg(xhci, "Successful control transfer!\n");
1399 status = 0;
1400 }
1401 break;
1402 case COMP_SHORT_TX:
1403 xhci_warn(xhci, "WARN: short transfer on control ep\n");
1404 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1405 status = -EREMOTEIO;
1406 else
1407 status = 0;
1408 break;
1409 1825
1410 default: 1826 do {
1411 if (!xhci_requires_manual_halt_cleanup(xhci, 1827 /* This TRB should be in the TD at the head of this ring's
1412 ep_ctx, trb_comp_code)) 1828 * TD list.
1413 break;
1414 xhci_dbg(xhci, "TRB error code %u, "
1415 "halted endpoint index = %u\n",
1416 trb_comp_code, ep_index);
1417 /* else fall through */
1418 case COMP_STALL:
1419 /* Did we transfer part of the data (middle) phase? */
1420 if (event_trb != ep_ring->dequeue &&
1421 event_trb != td->last_trb)
1422 td->urb->actual_length =
1423 td->urb->transfer_buffer_length
1424 - TRB_LEN(event->transfer_len);
1425 else
1426 td->urb->actual_length = 0;
1427
1428 xhci_cleanup_halted_endpoint(xhci,
1429 slot_id, ep_index, 0, td, event_trb);
1430 goto td_cleanup;
1431 }
1432 /*
1433 * Did we transfer any data, despite the errors that might have
1434 * happened? I.e. did we get past the setup stage?
1435 */ 1829 */
1436 if (event_trb != ep_ring->dequeue) { 1830 if (list_empty(&ep_ring->td_list)) {
1437 /* The event was for the status stage */ 1831 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d "
1438 if (event_trb == td->last_trb) { 1832 "with no TDs queued?\n",
1439 if (td->urb->actual_length != 0) { 1833 TRB_TO_SLOT_ID(event->flags), ep_index);
1440 /* Don't overwrite a previously set error code */ 1834 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
1441 if ((status == -EINPROGRESS || 1835 (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
1442 status == 0) && 1836 xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
1443 (td->urb->transfer_flags 1837 if (ep->skip) {
1444 & URB_SHORT_NOT_OK)) 1838 ep->skip = false;
1445 /* Did we already see a short data stage? */ 1839 xhci_dbg(xhci, "td_list is empty while skip "
1446 status = -EREMOTEIO; 1840 "flag set. Clear skip flag.\n");
1447 } else {
1448 td->urb->actual_length =
1449 td->urb->transfer_buffer_length;
1450 }
1451 } else {
1452 /* Maybe the event was for the data stage? */
1453 if (trb_comp_code != COMP_STOP_INVAL) {
1454 /* We didn't stop on a link TRB in the middle */
1455 td->urb->actual_length =
1456 td->urb->transfer_buffer_length -
1457 TRB_LEN(event->transfer_len);
1458 xhci_dbg(xhci, "Waiting for status stage event\n");
1459 urb = NULL;
1460 goto cleanup;
1461 }
1462 } 1841 }
1842 ret = 0;
1843 goto cleanup;
1463 } 1844 }
1464 } else { 1845
1465 switch (trb_comp_code) { 1846 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
1466 case COMP_SUCCESS: 1847 /* Is this a TRB in the currently executing TD? */
1467 /* Double check that the HW transferred everything. */ 1848 event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
1468 if (event_trb != td->last_trb) { 1849 td->last_trb, event_dma);
1469 xhci_warn(xhci, "WARN Successful completion " 1850 if (event_seg && ep->skip) {
1470 "on short TX\n"); 1851 xhci_dbg(xhci, "Found td. Clear skip flag.\n");
1471 if (td->urb->transfer_flags & URB_SHORT_NOT_OK) 1852 ep->skip = false;
1472 status = -EREMOTEIO; 1853 }
1473 else 1854 if (!event_seg &&
1474 status = 0; 1855 (!ep->skip || !usb_endpoint_xfer_isoc(&td->urb->ep->desc))) {
1475 } else { 1856 /* HC is busted, give up! */
1476 if (usb_endpoint_xfer_bulk(&td->urb->ep->desc)) 1857 xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not "
1477 xhci_dbg(xhci, "Successful bulk " 1858 "part of current TD\n");
1478 "transfer!\n"); 1859 return -ESHUTDOWN;
1479 else
1480 xhci_dbg(xhci, "Successful interrupt "
1481 "transfer!\n");
1482 status = 0;
1483 }
1484 break;
1485 case COMP_SHORT_TX:
1486 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1487 status = -EREMOTEIO;
1488 else
1489 status = 0;
1490 break;
1491 default:
1492 /* Others already handled above */
1493 break;
1494 } 1860 }
1495 dev_dbg(&td->urb->dev->dev,
1496 "ep %#x - asked for %d bytes, "
1497 "%d bytes untransferred\n",
1498 td->urb->ep->desc.bEndpointAddress,
1499 td->urb->transfer_buffer_length,
1500 TRB_LEN(event->transfer_len));
1501 /* Fast path - was this the last TRB in the TD for this URB? */
1502 if (event_trb == td->last_trb) {
1503 if (TRB_LEN(event->transfer_len) != 0) {
1504 td->urb->actual_length =
1505 td->urb->transfer_buffer_length -
1506 TRB_LEN(event->transfer_len);
1507 if (td->urb->transfer_buffer_length <
1508 td->urb->actual_length) {
1509 xhci_warn(xhci, "HC gave bad length "
1510 "of %d bytes left\n",
1511 TRB_LEN(event->transfer_len));
1512 td->urb->actual_length = 0;
1513 if (td->urb->transfer_flags &
1514 URB_SHORT_NOT_OK)
1515 status = -EREMOTEIO;
1516 else
1517 status = 0;
1518 }
1519 /* Don't overwrite a previously set error code */
1520 if (status == -EINPROGRESS) {
1521 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1522 status = -EREMOTEIO;
1523 else
1524 status = 0;
1525 }
1526 } else {
1527 td->urb->actual_length = td->urb->transfer_buffer_length;
1528 /* Ignore a short packet completion if the
1529 * untransferred length was zero.
1530 */
1531 if (status == -EREMOTEIO)
1532 status = 0;
1533 }
1534 } else {
1535 /* Slow path - walk the list, starting from the dequeue
1536 * pointer, to get the actual length transferred.
1537 */
1538 union xhci_trb *cur_trb;
1539 struct xhci_segment *cur_seg;
1540 1861
1541 td->urb->actual_length = 0; 1862 if (event_seg) {
1542 for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg; 1863 event_trb = &event_seg->trbs[(event_dma -
1543 cur_trb != event_trb; 1864 event_seg->dma) / sizeof(*event_trb)];
1544 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) { 1865 /*
1545 if ((cur_trb->generic.field[3] & 1866 * No-op TRB should not trigger interrupts.
1546 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_TR_NOOP) && 1867 * If event_trb is a no-op TRB, it means the
1547 (cur_trb->generic.field[3] & 1868 * corresponding TD has been cancelled. Just ignore
1548 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_LINK)) 1869 * the TD.
1549 td->urb->actual_length +=
1550 TRB_LEN(cur_trb->generic.field[2]);
1551 }
1552 /* If the ring didn't stop on a Link or No-op TRB, add
1553 * in the actual bytes transferred from the Normal TRB
1554 */ 1870 */
1555 if (trb_comp_code != COMP_STOP_INVAL) 1871 if ((event_trb->generic.field[3] & TRB_TYPE_BITMASK)
1556 td->urb->actual_length += 1872 == TRB_TYPE(TRB_TR_NOOP)) {
1557 TRB_LEN(cur_trb->generic.field[2]) - 1873 xhci_dbg(xhci, "event_trb is a no-op TRB. "
1558 TRB_LEN(event->transfer_len); 1874 "Skip it\n");
1875 goto cleanup;
1876 }
1559 } 1877 }
1560 } 1878
1561 if (trb_comp_code == COMP_STOP_INVAL || 1879 /* Now update the urb's actual_length and give back to
1562 trb_comp_code == COMP_STOP) { 1880 * the core
1563 /* The Endpoint Stop Command completion will take care of any
1564 * stopped TDs. A stopped TD may be restarted, so don't update
1565 * the ring dequeue pointer or take this TD off any lists yet.
1566 */ 1881 */
1567 ep->stopped_td = td; 1882 if (usb_endpoint_xfer_control(&td->urb->ep->desc))
1568 ep->stopped_trb = event_trb; 1883 ret = process_ctrl_td(xhci, td, event_trb, event, ep,
1569 } else { 1884 &status);
1570 if (trb_comp_code == COMP_STALL) { 1885 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
1571 /* The transfer is completed from the driver's 1886 ret = process_isoc_td(xhci, td, event_trb, event, ep,
1572 * perspective, but we need to issue a set dequeue 1887 &status);
1573 * command for this stalled endpoint to move the dequeue 1888 else
1574 * pointer past the TD. We can't do that here because 1889 ret = process_bulk_intr_td(xhci, td, event_trb, event,
1575 * the halt condition must be cleared first. Let the 1890 ep, &status);
1576 * USB class driver clear the stall later.
1577 */
1578 ep->stopped_td = td;
1579 ep->stopped_trb = event_trb;
1580 ep->stopped_stream = ep_ring->stream_id;
1581 } else if (xhci_requires_manual_halt_cleanup(xhci,
1582 ep_ctx, trb_comp_code)) {
1583 /* Other types of errors halt the endpoint, but the
1584 * class driver doesn't call usb_reset_endpoint() unless
1585 * the error is -EPIPE. Clear the halted status in the
1586 * xHCI hardware manually.
1587 */
1588 xhci_cleanup_halted_endpoint(xhci,
1589 slot_id, ep_index, ep_ring->stream_id, td, event_trb);
1590 } else {
1591 /* Update ring dequeue pointer */
1592 while (ep_ring->dequeue != td->last_trb)
1593 inc_deq(xhci, ep_ring, false);
1594 inc_deq(xhci, ep_ring, false);
1595 }
1596 1891
1597td_cleanup: 1892cleanup:
1598 /* Clean up the endpoint's TD list */ 1893 /*
1599 urb = td->urb; 1894 * Do not update event ring dequeue pointer if ep->skip is set.
1600 /* Do one last check of the actual transfer length. 1895 * Will roll back to continue process missed tds.
1601 * If the host controller said we transferred more data than
1602 * the buffer length, urb->actual_length will be a very big
1603 * number (since it's unsigned). Play it safe and say we didn't
1604 * transfer anything.
1605 */ 1896 */
1606 if (urb->actual_length > urb->transfer_buffer_length) { 1897 if (trb_comp_code == COMP_MISSED_INT || !ep->skip) {
1607 xhci_warn(xhci, "URB transfer length is wrong, " 1898 inc_deq(xhci, xhci->event_ring, true);
1608 "xHC issue? req. len = %u, "
1609 "act. len = %u\n",
1610 urb->transfer_buffer_length,
1611 urb->actual_length);
1612 urb->actual_length = 0;
1613 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1614 status = -EREMOTEIO;
1615 else
1616 status = 0;
1617 } 1899 }
1618 list_del(&td->td_list);
1619 /* Was this TD slated to be cancelled but completed anyway? */
1620 if (!list_empty(&td->cancelled_td_list))
1621 list_del(&td->cancelled_td_list);
1622 1900
1623 /* Leave the TD around for the reset endpoint function to use 1901 if (ret) {
1624 * (but only if it's not a control endpoint, since we already 1902 urb = td->urb;
1625 * queued the Set TR dequeue pointer command for stalled 1903 urb_priv = urb->hcpriv;
1626 * control endpoints). 1904 /* Leave the TD around for the reset endpoint function
1627 */ 1905 * to use(but only if it's not a control endpoint,
1628 if (usb_endpoint_xfer_control(&urb->ep->desc) || 1906 * since we already queued the Set TR dequeue pointer
1629 (trb_comp_code != COMP_STALL && 1907 * command for stalled control endpoints).
1630 trb_comp_code != COMP_BABBLE)) { 1908 */
1631 kfree(td); 1909 if (usb_endpoint_xfer_control(&urb->ep->desc) ||
1910 (trb_comp_code != COMP_STALL &&
1911 trb_comp_code != COMP_BABBLE))
1912 xhci_urb_free_priv(xhci, urb_priv);
1913
1914 usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
1915 xhci_dbg(xhci, "Giveback URB %p, len = %d, "
1916 "status = %d\n",
1917 urb, urb->actual_length, status);
1918 spin_unlock(&xhci->lock);
1919 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
1920 spin_lock(&xhci->lock);
1632 } 1921 }
1633 urb->hcpriv = NULL;
1634 }
1635cleanup:
1636 inc_deq(xhci, xhci->event_ring, true);
1637 xhci_set_hc_event_deq(xhci);
1638 1922
1639 /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */ 1923 /*
1640 if (urb) { 1924 * If ep->skip is set, it means there are missed tds on the
1641 usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb); 1925 * endpoint ring need to take care of.
1642 xhci_dbg(xhci, "Giveback URB %p, len = %d, status = %d\n", 1926 * Process them as short transfer until reach the td pointed by
1643 urb, urb->actual_length, status); 1927 * the event.
1644 spin_unlock(&xhci->lock); 1928 */
1645 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status); 1929 } while (ep->skip && trb_comp_code != COMP_MISSED_INT);
1646 spin_lock(&xhci->lock); 1930
1647 }
1648 return 0; 1931 return 0;
1649} 1932}
1650 1933
@@ -1652,7 +1935,7 @@ cleanup:
1652 * This function handles all OS-owned events on the event ring. It may drop 1935 * This function handles all OS-owned events on the event ring. It may drop
1653 * xhci->lock between event processing (e.g. to pass up port status changes). 1936 * xhci->lock between event processing (e.g. to pass up port status changes).
1654 */ 1937 */
1655void xhci_handle_event(struct xhci_hcd *xhci) 1938static void xhci_handle_event(struct xhci_hcd *xhci)
1656{ 1939{
1657 union xhci_trb *event; 1940 union xhci_trb *event;
1658 int update_ptrs = 1; 1941 int update_ptrs = 1;
@@ -1710,15 +1993,130 @@ void xhci_handle_event(struct xhci_hcd *xhci)
1710 return; 1993 return;
1711 } 1994 }
1712 1995
1713 if (update_ptrs) { 1996 if (update_ptrs)
1714 /* Update SW and HC event ring dequeue pointer */ 1997 /* Update SW event ring dequeue pointer */
1715 inc_deq(xhci, xhci->event_ring, true); 1998 inc_deq(xhci, xhci->event_ring, true);
1716 xhci_set_hc_event_deq(xhci); 1999
1717 }
1718 /* Are there more items on the event ring? */ 2000 /* Are there more items on the event ring? */
1719 xhci_handle_event(xhci); 2001 xhci_handle_event(xhci);
1720} 2002}
1721 2003
2004/*
2005 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
2006 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
2007 * indicators of an event TRB error, but we check the status *first* to be safe.
2008 */
2009irqreturn_t xhci_irq(struct usb_hcd *hcd)
2010{
2011 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2012 u32 status;
2013 union xhci_trb *trb;
2014 u64 temp_64;
2015 union xhci_trb *event_ring_deq;
2016 dma_addr_t deq;
2017
2018 spin_lock(&xhci->lock);
2019 trb = xhci->event_ring->dequeue;
2020 /* Check if the xHC generated the interrupt, or the irq is shared */
2021 status = xhci_readl(xhci, &xhci->op_regs->status);
2022 if (status == 0xffffffff)
2023 goto hw_died;
2024
2025 if (!(status & STS_EINT)) {
2026 spin_unlock(&xhci->lock);
2027 xhci_warn(xhci, "Spurious interrupt.\n");
2028 return IRQ_NONE;
2029 }
2030 xhci_dbg(xhci, "op reg status = %08x\n", status);
2031 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
2032 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
2033 (unsigned long long)
2034 xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
2035 lower_32_bits(trb->link.segment_ptr),
2036 upper_32_bits(trb->link.segment_ptr),
2037 (unsigned int) trb->link.intr_target,
2038 (unsigned int) trb->link.control);
2039
2040 if (status & STS_FATAL) {
2041 xhci_warn(xhci, "WARNING: Host System Error\n");
2042 xhci_halt(xhci);
2043hw_died:
2044 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
2045 spin_unlock(&xhci->lock);
2046 return -ESHUTDOWN;
2047 }
2048
2049 /*
2050 * Clear the op reg interrupt status first,
2051 * so we can receive interrupts from other MSI-X interrupters.
2052 * Write 1 to clear the interrupt status.
2053 */
2054 status |= STS_EINT;
2055 xhci_writel(xhci, status, &xhci->op_regs->status);
2056 /* FIXME when MSI-X is supported and there are multiple vectors */
2057 /* Clear the MSI-X event interrupt status */
2058
2059 if (hcd->irq != -1) {
2060 u32 irq_pending;
2061 /* Acknowledge the PCI interrupt */
2062 irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
2063 irq_pending |= 0x3;
2064 xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending);
2065 }
2066
2067 if (xhci->xhc_state & XHCI_STATE_DYING) {
2068 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
2069 "Shouldn't IRQs be disabled?\n");
2070 /* Clear the event handler busy flag (RW1C);
2071 * the event ring should be empty.
2072 */
2073 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2074 xhci_write_64(xhci, temp_64 | ERST_EHB,
2075 &xhci->ir_set->erst_dequeue);
2076 spin_unlock(&xhci->lock);
2077
2078 return IRQ_HANDLED;
2079 }
2080
2081 event_ring_deq = xhci->event_ring->dequeue;
2082 /* FIXME this should be a delayed service routine
2083 * that clears the EHB.
2084 */
2085 xhci_handle_event(xhci);
2086
2087 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2088 /* If necessary, update the HW's version of the event ring deq ptr. */
2089 if (event_ring_deq != xhci->event_ring->dequeue) {
2090 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
2091 xhci->event_ring->dequeue);
2092 if (deq == 0)
2093 xhci_warn(xhci, "WARN something wrong with SW event "
2094 "ring dequeue ptr.\n");
2095 /* Update HC event ring dequeue pointer */
2096 temp_64 &= ERST_PTR_MASK;
2097 temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
2098 }
2099
2100 /* Clear the event handler busy flag (RW1C); event ring is empty. */
2101 temp_64 |= ERST_EHB;
2102 xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
2103
2104 spin_unlock(&xhci->lock);
2105
2106 return IRQ_HANDLED;
2107}
2108
2109irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
2110{
2111 irqreturn_t ret;
2112
2113 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
2114
2115 ret = xhci_irq(hcd);
2116
2117 return ret;
2118}
2119
1722/**** Endpoint Ring Operations ****/ 2120/**** Endpoint Ring Operations ****/
1723 2121
1724/* 2122/*
@@ -1827,10 +2225,12 @@ static int prepare_transfer(struct xhci_hcd *xhci,
1827 unsigned int stream_id, 2225 unsigned int stream_id,
1828 unsigned int num_trbs, 2226 unsigned int num_trbs,
1829 struct urb *urb, 2227 struct urb *urb,
1830 struct xhci_td **td, 2228 unsigned int td_index,
1831 gfp_t mem_flags) 2229 gfp_t mem_flags)
1832{ 2230{
1833 int ret; 2231 int ret;
2232 struct urb_priv *urb_priv;
2233 struct xhci_td *td;
1834 struct xhci_ring *ep_ring; 2234 struct xhci_ring *ep_ring;
1835 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); 2235 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
1836 2236
@@ -1846,24 +2246,29 @@ static int prepare_transfer(struct xhci_hcd *xhci,
1846 num_trbs, mem_flags); 2246 num_trbs, mem_flags);
1847 if (ret) 2247 if (ret)
1848 return ret; 2248 return ret;
1849 *td = kzalloc(sizeof(struct xhci_td), mem_flags);
1850 if (!*td)
1851 return -ENOMEM;
1852 INIT_LIST_HEAD(&(*td)->td_list);
1853 INIT_LIST_HEAD(&(*td)->cancelled_td_list);
1854 2249
1855 ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb); 2250 urb_priv = urb->hcpriv;
1856 if (unlikely(ret)) { 2251 td = urb_priv->td[td_index];
1857 kfree(*td); 2252
1858 return ret; 2253 INIT_LIST_HEAD(&td->td_list);
2254 INIT_LIST_HEAD(&td->cancelled_td_list);
2255
2256 if (td_index == 0) {
2257 ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
2258 if (unlikely(ret)) {
2259 xhci_urb_free_priv(xhci, urb_priv);
2260 urb->hcpriv = NULL;
2261 return ret;
2262 }
1859 } 2263 }
1860 2264
1861 (*td)->urb = urb; 2265 td->urb = urb;
1862 urb->hcpriv = (void *) (*td);
1863 /* Add this TD to the tail of the endpoint ring's TD list */ 2266 /* Add this TD to the tail of the endpoint ring's TD list */
1864 list_add_tail(&(*td)->td_list, &ep_ring->td_list); 2267 list_add_tail(&td->td_list, &ep_ring->td_list);
1865 (*td)->start_seg = ep_ring->enq_seg; 2268 td->start_seg = ep_ring->enq_seg;
1866 (*td)->first_trb = ep_ring->enqueue; 2269 td->first_trb = ep_ring->enqueue;
2270
2271 urb_priv->td[td_index] = td;
1867 2272
1868 return 0; 2273 return 0;
1869} 2274}
@@ -2002,6 +2407,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2002{ 2407{
2003 struct xhci_ring *ep_ring; 2408 struct xhci_ring *ep_ring;
2004 unsigned int num_trbs; 2409 unsigned int num_trbs;
2410 struct urb_priv *urb_priv;
2005 struct xhci_td *td; 2411 struct xhci_td *td;
2006 struct scatterlist *sg; 2412 struct scatterlist *sg;
2007 int num_sgs; 2413 int num_sgs;
@@ -2022,9 +2428,13 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2022 2428
2023 trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id], 2429 trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
2024 ep_index, urb->stream_id, 2430 ep_index, urb->stream_id,
2025 num_trbs, urb, &td, mem_flags); 2431 num_trbs, urb, 0, mem_flags);
2026 if (trb_buff_len < 0) 2432 if (trb_buff_len < 0)
2027 return trb_buff_len; 2433 return trb_buff_len;
2434
2435 urb_priv = urb->hcpriv;
2436 td = urb_priv->td[0];
2437
2028 /* 2438 /*
2029 * Don't give the first TRB to the hardware (by toggling the cycle bit) 2439 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2030 * until we've finished creating all the other TRBs. The ring's cycle 2440 * until we've finished creating all the other TRBs. The ring's cycle
@@ -2145,6 +2555,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2145 struct urb *urb, int slot_id, unsigned int ep_index) 2555 struct urb *urb, int slot_id, unsigned int ep_index)
2146{ 2556{
2147 struct xhci_ring *ep_ring; 2557 struct xhci_ring *ep_ring;
2558 struct urb_priv *urb_priv;
2148 struct xhci_td *td; 2559 struct xhci_td *td;
2149 int num_trbs; 2560 int num_trbs;
2150 struct xhci_generic_trb *start_trb; 2561 struct xhci_generic_trb *start_trb;
@@ -2190,10 +2601,13 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2190 2601
2191 ret = prepare_transfer(xhci, xhci->devs[slot_id], 2602 ret = prepare_transfer(xhci, xhci->devs[slot_id],
2192 ep_index, urb->stream_id, 2603 ep_index, urb->stream_id,
2193 num_trbs, urb, &td, mem_flags); 2604 num_trbs, urb, 0, mem_flags);
2194 if (ret < 0) 2605 if (ret < 0)
2195 return ret; 2606 return ret;
2196 2607
2608 urb_priv = urb->hcpriv;
2609 td = urb_priv->td[0];
2610
2197 /* 2611 /*
2198 * Don't give the first TRB to the hardware (by toggling the cycle bit) 2612 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2199 * until we've finished creating all the other TRBs. The ring's cycle 2613 * until we've finished creating all the other TRBs. The ring's cycle
@@ -2279,6 +2693,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2279 struct xhci_generic_trb *start_trb; 2693 struct xhci_generic_trb *start_trb;
2280 int start_cycle; 2694 int start_cycle;
2281 u32 field, length_field; 2695 u32 field, length_field;
2696 struct urb_priv *urb_priv;
2282 struct xhci_td *td; 2697 struct xhci_td *td;
2283 2698
2284 ep_ring = xhci_urb_to_transfer_ring(xhci, urb); 2699 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
@@ -2306,10 +2721,13 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2306 num_trbs++; 2721 num_trbs++;
2307 ret = prepare_transfer(xhci, xhci->devs[slot_id], 2722 ret = prepare_transfer(xhci, xhci->devs[slot_id],
2308 ep_index, urb->stream_id, 2723 ep_index, urb->stream_id,
2309 num_trbs, urb, &td, mem_flags); 2724 num_trbs, urb, 0, mem_flags);
2310 if (ret < 0) 2725 if (ret < 0)
2311 return ret; 2726 return ret;
2312 2727
2728 urb_priv = urb->hcpriv;
2729 td = urb_priv->td[0];
2730
2313 /* 2731 /*
2314 * Don't give the first TRB to the hardware (by toggling the cycle bit) 2732 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2315 * until we've finished creating all the other TRBs. The ring's cycle 2733 * until we've finished creating all the other TRBs. The ring's cycle
@@ -2366,6 +2784,224 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2366 return 0; 2784 return 0;
2367} 2785}
2368 2786
2787static int count_isoc_trbs_needed(struct xhci_hcd *xhci,
2788 struct urb *urb, int i)
2789{
2790 int num_trbs = 0;
2791 u64 addr, td_len, running_total;
2792
2793 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
2794 td_len = urb->iso_frame_desc[i].length;
2795
2796 running_total = TRB_MAX_BUFF_SIZE -
2797 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
2798 if (running_total != 0)
2799 num_trbs++;
2800
2801 while (running_total < td_len) {
2802 num_trbs++;
2803 running_total += TRB_MAX_BUFF_SIZE;
2804 }
2805
2806 return num_trbs;
2807}
2808
2809/* This is for isoc transfer */
2810static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2811 struct urb *urb, int slot_id, unsigned int ep_index)
2812{
2813 struct xhci_ring *ep_ring;
2814 struct urb_priv *urb_priv;
2815 struct xhci_td *td;
2816 int num_tds, trbs_per_td;
2817 struct xhci_generic_trb *start_trb;
2818 bool first_trb;
2819 int start_cycle;
2820 u32 field, length_field;
2821 int running_total, trb_buff_len, td_len, td_remain_len, ret;
2822 u64 start_addr, addr;
2823 int i, j;
2824
2825 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
2826
2827 num_tds = urb->number_of_packets;
2828 if (num_tds < 1) {
2829 xhci_dbg(xhci, "Isoc URB with zero packets?\n");
2830 return -EINVAL;
2831 }
2832
2833 if (!in_interrupt())
2834 dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d),"
2835 " addr = %#llx, num_tds = %d\n",
2836 urb->ep->desc.bEndpointAddress,
2837 urb->transfer_buffer_length,
2838 urb->transfer_buffer_length,
2839 (unsigned long long)urb->transfer_dma,
2840 num_tds);
2841
2842 start_addr = (u64) urb->transfer_dma;
2843 start_trb = &ep_ring->enqueue->generic;
2844 start_cycle = ep_ring->cycle_state;
2845
2846 /* Queue the first TRB, even if it's zero-length */
2847 for (i = 0; i < num_tds; i++) {
2848 first_trb = true;
2849
2850 running_total = 0;
2851 addr = start_addr + urb->iso_frame_desc[i].offset;
2852 td_len = urb->iso_frame_desc[i].length;
2853 td_remain_len = td_len;
2854
2855 trbs_per_td = count_isoc_trbs_needed(xhci, urb, i);
2856
2857 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
2858 urb->stream_id, trbs_per_td, urb, i, mem_flags);
2859 if (ret < 0)
2860 return ret;
2861
2862 urb_priv = urb->hcpriv;
2863 td = urb_priv->td[i];
2864
2865 for (j = 0; j < trbs_per_td; j++) {
2866 u32 remainder = 0;
2867 field = 0;
2868
2869 if (first_trb) {
2870 /* Queue the isoc TRB */
2871 field |= TRB_TYPE(TRB_ISOC);
2872 /* Assume URB_ISO_ASAP is set */
2873 field |= TRB_SIA;
2874 if (i > 0)
2875 field |= ep_ring->cycle_state;
2876 first_trb = false;
2877 } else {
2878 /* Queue other normal TRBs */
2879 field |= TRB_TYPE(TRB_NORMAL);
2880 field |= ep_ring->cycle_state;
2881 }
2882
2883 /* Chain all the TRBs together; clear the chain bit in
2884 * the last TRB to indicate it's the last TRB in the
2885 * chain.
2886 */
2887 if (j < trbs_per_td - 1) {
2888 field |= TRB_CHAIN;
2889 } else {
2890 td->last_trb = ep_ring->enqueue;
2891 field |= TRB_IOC;
2892 }
2893
2894 /* Calculate TRB length */
2895 trb_buff_len = TRB_MAX_BUFF_SIZE -
2896 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
2897 if (trb_buff_len > td_remain_len)
2898 trb_buff_len = td_remain_len;
2899
2900 remainder = xhci_td_remainder(td_len - running_total);
2901 length_field = TRB_LEN(trb_buff_len) |
2902 remainder |
2903 TRB_INTR_TARGET(0);
2904 queue_trb(xhci, ep_ring, false, false,
2905 lower_32_bits(addr),
2906 upper_32_bits(addr),
2907 length_field,
2908 /* We always want to know if the TRB was short,
2909 * or we won't get an event when it completes.
2910 * (Unless we use event data TRBs, which are a
2911 * waste of space and HC resources.)
2912 */
2913 field | TRB_ISP);
2914 running_total += trb_buff_len;
2915
2916 addr += trb_buff_len;
2917 td_remain_len -= trb_buff_len;
2918 }
2919
2920 /* Check TD length */
2921 if (running_total != td_len) {
2922 xhci_err(xhci, "ISOC TD length unmatch\n");
2923 return -EINVAL;
2924 }
2925 }
2926
2927 wmb();
2928 start_trb->field[3] |= start_cycle;
2929
2930 ring_ep_doorbell(xhci, slot_id, ep_index, urb->stream_id);
2931 return 0;
2932}
2933
2934/*
2935 * Check transfer ring to guarantee there is enough room for the urb.
2936 * Update ISO URB start_frame and interval.
2937 * Update interval as xhci_queue_intr_tx does. Just use xhci frame_index to
2938 * update the urb->start_frame by now.
2939 * Always assume URB_ISO_ASAP set, and NEVER use urb->start_frame as input.
2940 */
2941int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
2942 struct urb *urb, int slot_id, unsigned int ep_index)
2943{
2944 struct xhci_virt_device *xdev;
2945 struct xhci_ring *ep_ring;
2946 struct xhci_ep_ctx *ep_ctx;
2947 int start_frame;
2948 int xhci_interval;
2949 int ep_interval;
2950 int num_tds, num_trbs, i;
2951 int ret;
2952
2953 xdev = xhci->devs[slot_id];
2954 ep_ring = xdev->eps[ep_index].ring;
2955 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
2956
2957 num_trbs = 0;
2958 num_tds = urb->number_of_packets;
2959 for (i = 0; i < num_tds; i++)
2960 num_trbs += count_isoc_trbs_needed(xhci, urb, i);
2961
2962 /* Check the ring to guarantee there is enough room for the whole urb.
2963 * Do not insert any td of the urb to the ring if the check failed.
2964 */
2965 ret = prepare_ring(xhci, ep_ring, ep_ctx->ep_info & EP_STATE_MASK,
2966 num_trbs, mem_flags);
2967 if (ret)
2968 return ret;
2969
2970 start_frame = xhci_readl(xhci, &xhci->run_regs->microframe_index);
2971 start_frame &= 0x3fff;
2972
2973 urb->start_frame = start_frame;
2974 if (urb->dev->speed == USB_SPEED_LOW ||
2975 urb->dev->speed == USB_SPEED_FULL)
2976 urb->start_frame >>= 3;
2977
2978 xhci_interval = EP_INTERVAL_TO_UFRAMES(ep_ctx->ep_info);
2979 ep_interval = urb->interval;
2980 /* Convert to microframes */
2981 if (urb->dev->speed == USB_SPEED_LOW ||
2982 urb->dev->speed == USB_SPEED_FULL)
2983 ep_interval *= 8;
2984 /* FIXME change this to a warning and a suggestion to use the new API
2985 * to set the polling interval (once the API is added).
2986 */
2987 if (xhci_interval != ep_interval) {
2988 if (!printk_ratelimit())
2989 dev_dbg(&urb->dev->dev, "Driver uses different interval"
2990 " (%d microframe%s) than xHCI "
2991 "(%d microframe%s)\n",
2992 ep_interval,
2993 ep_interval == 1 ? "" : "s",
2994 xhci_interval,
2995 xhci_interval == 1 ? "" : "s");
2996 urb->interval = xhci_interval;
2997 /* Convert back to frames for LS/FS devices */
2998 if (urb->dev->speed == USB_SPEED_LOW ||
2999 urb->dev->speed == USB_SPEED_FULL)
3000 urb->interval /= 8;
3001 }
3002 return xhci_queue_isoc_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
3003}
3004
2369/**** Command Ring Operations ****/ 3005/**** Command Ring Operations ****/
2370 3006
2371/* Generic function for queueing a command TRB on the command ring. 3007/* Generic function for queueing a command TRB on the command ring.
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 3998f72cd0c..d5c550ea3e6 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -20,6 +20,7 @@
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */ 21 */
22 22
23#include <linux/pci.h>
23#include <linux/irq.h> 24#include <linux/irq.h>
24#include <linux/log2.h> 25#include <linux/log2.h>
25#include <linux/module.h> 26#include <linux/module.h>
@@ -171,22 +172,84 @@ int xhci_reset(struct xhci_hcd *xhci)
171 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000); 172 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
172} 173}
173 174
175/*
176 * Free IRQs
177 * free all IRQs request
178 */
179static void xhci_free_irq(struct xhci_hcd *xhci)
180{
181 int i;
182 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
174 183
175#if 0 184 /* return if using legacy interrupt */
176/* Set up MSI-X table for entry 0 (may claim other entries later) */ 185 if (xhci_to_hcd(xhci)->irq >= 0)
177static int xhci_setup_msix(struct xhci_hcd *xhci) 186 return;
187
188 if (xhci->msix_entries) {
189 for (i = 0; i < xhci->msix_count; i++)
190 if (xhci->msix_entries[i].vector)
191 free_irq(xhci->msix_entries[i].vector,
192 xhci_to_hcd(xhci));
193 } else if (pdev->irq >= 0)
194 free_irq(pdev->irq, xhci_to_hcd(xhci));
195
196 return;
197}
198
199/*
200 * Set up MSI
201 */
202static int xhci_setup_msi(struct xhci_hcd *xhci)
178{ 203{
179 int ret; 204 int ret;
205 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
206
207 ret = pci_enable_msi(pdev);
208 if (ret) {
209 xhci_err(xhci, "failed to allocate MSI entry\n");
210 return ret;
211 }
212
213 ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
214 0, "xhci_hcd", xhci_to_hcd(xhci));
215 if (ret) {
216 xhci_err(xhci, "disable MSI interrupt\n");
217 pci_disable_msi(pdev);
218 }
219
220 return ret;
221}
222
223/*
224 * Set up MSI-X
225 */
226static int xhci_setup_msix(struct xhci_hcd *xhci)
227{
228 int i, ret = 0;
180 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 229 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
181 230
182 xhci->msix_count = 0; 231 /*
183 /* XXX: did I do this right? ixgbe does kcalloc for more than one */ 232 * calculate number of msi-x vectors supported.
184 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL); 233 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
234 * with max number of interrupters based on the xhci HCSPARAMS1.
235 * - num_online_cpus: maximum msi-x vectors per CPUs core.
236 * Add additional 1 vector to ensure always available interrupt.
237 */
238 xhci->msix_count = min(num_online_cpus() + 1,
239 HCS_MAX_INTRS(xhci->hcs_params1));
240
241 xhci->msix_entries =
242 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
243 GFP_KERNEL);
185 if (!xhci->msix_entries) { 244 if (!xhci->msix_entries) {
186 xhci_err(xhci, "Failed to allocate MSI-X entries\n"); 245 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
187 return -ENOMEM; 246 return -ENOMEM;
188 } 247 }
189 xhci->msix_entries[0].entry = 0; 248
249 for (i = 0; i < xhci->msix_count; i++) {
250 xhci->msix_entries[i].entry = i;
251 xhci->msix_entries[i].vector = 0;
252 }
190 253
191 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count); 254 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
192 if (ret) { 255 if (ret) {
@@ -194,20 +257,19 @@ static int xhci_setup_msix(struct xhci_hcd *xhci)
194 goto free_entries; 257 goto free_entries;
195 } 258 }
196 259
197 /* 260 for (i = 0; i < xhci->msix_count; i++) {
198 * Pass the xhci pointer value as the request_irq "cookie". 261 ret = request_irq(xhci->msix_entries[i].vector,
199 * If more irqs are added, this will need to be unique for each one. 262 (irq_handler_t)xhci_msi_irq,
200 */ 263 0, "xhci_hcd", xhci_to_hcd(xhci));
201 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0, 264 if (ret)
202 "xHCI", xhci_to_hcd(xhci)); 265 goto disable_msix;
203 if (ret) {
204 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
205 goto disable_msix;
206 } 266 }
207 xhci_dbg(xhci, "Finished setting up MSI-X\n"); 267
208 return 0; 268 return ret;
209 269
210disable_msix: 270disable_msix:
271 xhci_err(xhci, "disable MSI-X interrupt\n");
272 xhci_free_irq(xhci);
211 pci_disable_msix(pdev); 273 pci_disable_msix(pdev);
212free_entries: 274free_entries:
213 kfree(xhci->msix_entries); 275 kfree(xhci->msix_entries);
@@ -215,21 +277,23 @@ free_entries:
215 return ret; 277 return ret;
216} 278}
217 279
218/* XXX: code duplication; can xhci_setup_msix call this? */
219/* Free any IRQs and disable MSI-X */ 280/* Free any IRQs and disable MSI-X */
220static void xhci_cleanup_msix(struct xhci_hcd *xhci) 281static void xhci_cleanup_msix(struct xhci_hcd *xhci)
221{ 282{
222 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 283 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
223 if (!xhci->msix_entries)
224 return;
225 284
226 free_irq(xhci->msix_entries[0].vector, xhci); 285 xhci_free_irq(xhci);
227 pci_disable_msix(pdev); 286
228 kfree(xhci->msix_entries); 287 if (xhci->msix_entries) {
229 xhci->msix_entries = NULL; 288 pci_disable_msix(pdev);
230 xhci_dbg(xhci, "Finished cleaning up MSI-X\n"); 289 kfree(xhci->msix_entries);
290 xhci->msix_entries = NULL;
291 } else {
292 pci_disable_msi(pdev);
293 }
294
295 return;
231} 296}
232#endif
233 297
234/* 298/*
235 * Initialize memory for HCD and xHC (one-time init). 299 * Initialize memory for HCD and xHC (one-time init).
@@ -257,100 +321,8 @@ int xhci_init(struct usb_hcd *hcd)
257 return retval; 321 return retval;
258} 322}
259 323
260/*
261 * Called in interrupt context when there might be work
262 * queued on the event ring
263 *
264 * xhci->lock must be held by caller.
265 */
266static void xhci_work(struct xhci_hcd *xhci)
267{
268 u32 temp;
269 u64 temp_64;
270
271 /*
272 * Clear the op reg interrupt status first,
273 * so we can receive interrupts from other MSI-X interrupters.
274 * Write 1 to clear the interrupt status.
275 */
276 temp = xhci_readl(xhci, &xhci->op_regs->status);
277 temp |= STS_EINT;
278 xhci_writel(xhci, temp, &xhci->op_regs->status);
279 /* FIXME when MSI-X is supported and there are multiple vectors */
280 /* Clear the MSI-X event interrupt status */
281
282 /* Acknowledge the interrupt */
283 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
284 temp |= 0x3;
285 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
286 /* Flush posted writes */
287 xhci_readl(xhci, &xhci->ir_set->irq_pending);
288
289 if (xhci->xhc_state & XHCI_STATE_DYING)
290 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
291 "Shouldn't IRQs be disabled?\n");
292 else
293 /* FIXME this should be a delayed service routine
294 * that clears the EHB.
295 */
296 xhci_handle_event(xhci);
297
298 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
299 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
300 xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
301 /* Flush posted writes -- FIXME is this necessary? */
302 xhci_readl(xhci, &xhci->ir_set->irq_pending);
303}
304
305/*-------------------------------------------------------------------------*/ 324/*-------------------------------------------------------------------------*/
306 325
307/*
308 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
309 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
310 * indicators of an event TRB error, but we check the status *first* to be safe.
311 */
312irqreturn_t xhci_irq(struct usb_hcd *hcd)
313{
314 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
315 u32 temp, temp2;
316 union xhci_trb *trb;
317
318 spin_lock(&xhci->lock);
319 trb = xhci->event_ring->dequeue;
320 /* Check if the xHC generated the interrupt, or the irq is shared */
321 temp = xhci_readl(xhci, &xhci->op_regs->status);
322 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
323 if (temp == 0xffffffff && temp2 == 0xffffffff)
324 goto hw_died;
325
326 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
327 spin_unlock(&xhci->lock);
328 return IRQ_NONE;
329 }
330 xhci_dbg(xhci, "op reg status = %08x\n", temp);
331 xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2);
332 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
333 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
334 (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
335 lower_32_bits(trb->link.segment_ptr),
336 upper_32_bits(trb->link.segment_ptr),
337 (unsigned int) trb->link.intr_target,
338 (unsigned int) trb->link.control);
339
340 if (temp & STS_FATAL) {
341 xhci_warn(xhci, "WARNING: Host System Error\n");
342 xhci_halt(xhci);
343hw_died:
344 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
345 spin_unlock(&xhci->lock);
346 return -ESHUTDOWN;
347 }
348
349 xhci_work(xhci);
350 spin_unlock(&xhci->lock);
351
352 return IRQ_HANDLED;
353}
354 326
355#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING 327#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
356void xhci_event_ring_work(unsigned long arg) 328void xhci_event_ring_work(unsigned long arg)
@@ -423,21 +395,36 @@ int xhci_run(struct usb_hcd *hcd)
423{ 395{
424 u32 temp; 396 u32 temp;
425 u64 temp_64; 397 u64 temp_64;
398 u32 ret;
426 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 399 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
400 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
427 void (*doorbell)(struct xhci_hcd *) = NULL; 401 void (*doorbell)(struct xhci_hcd *) = NULL;
428 402
429 hcd->uses_new_polling = 1; 403 hcd->uses_new_polling = 1;
430 hcd->poll_rh = 0;
431 404
432 xhci_dbg(xhci, "xhci_run\n"); 405 xhci_dbg(xhci, "xhci_run\n");
433#if 0 /* FIXME: MSI not setup yet */ 406 /* unregister the legacy interrupt */
434 /* Do this at the very last minute */ 407 if (hcd->irq)
408 free_irq(hcd->irq, hcd);
409 hcd->irq = -1;
410
435 ret = xhci_setup_msix(xhci); 411 ret = xhci_setup_msix(xhci);
436 if (!ret) 412 if (ret)
437 return ret; 413 /* fall back to msi*/
414 ret = xhci_setup_msi(xhci);
415
416 if (ret) {
417 /* fall back to legacy interrupt*/
418 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
419 hcd->irq_descr, hcd);
420 if (ret) {
421 xhci_err(xhci, "request interrupt %d failed\n",
422 pdev->irq);
423 return ret;
424 }
425 hcd->irq = pdev->irq;
426 }
438 427
439 return -ENOSYS;
440#endif
441#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING 428#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
442 init_timer(&xhci->event_ring_timer); 429 init_timer(&xhci->event_ring_timer);
443 xhci->event_ring_timer.data = (unsigned long) xhci; 430 xhci->event_ring_timer.data = (unsigned long) xhci;
@@ -495,7 +482,6 @@ int xhci_run(struct usb_hcd *hcd)
495 return -ENODEV; 482 return -ENODEV;
496 } 483 }
497 484
498 xhci_dbg(xhci, "// @%p = 0x%x\n", &xhci->op_regs->command, temp);
499 if (doorbell) 485 if (doorbell)
500 (*doorbell)(xhci); 486 (*doorbell)(xhci);
501 if (xhci->quirks & XHCI_NEC_HOST) 487 if (xhci->quirks & XHCI_NEC_HOST)
@@ -522,11 +508,9 @@ void xhci_stop(struct usb_hcd *hcd)
522 spin_lock_irq(&xhci->lock); 508 spin_lock_irq(&xhci->lock);
523 xhci_halt(xhci); 509 xhci_halt(xhci);
524 xhci_reset(xhci); 510 xhci_reset(xhci);
511 xhci_cleanup_msix(xhci);
525 spin_unlock_irq(&xhci->lock); 512 spin_unlock_irq(&xhci->lock);
526 513
527#if 0 /* No MSI yet */
528 xhci_cleanup_msix(xhci);
529#endif
530#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING 514#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
531 /* Tell the event ring poll function not to reschedule */ 515 /* Tell the event ring poll function not to reschedule */
532 xhci->zombie = 1; 516 xhci->zombie = 1;
@@ -560,11 +544,8 @@ void xhci_shutdown(struct usb_hcd *hcd)
560 544
561 spin_lock_irq(&xhci->lock); 545 spin_lock_irq(&xhci->lock);
562 xhci_halt(xhci); 546 xhci_halt(xhci);
563 spin_unlock_irq(&xhci->lock);
564
565#if 0
566 xhci_cleanup_msix(xhci); 547 xhci_cleanup_msix(xhci);
567#endif 548 spin_unlock_irq(&xhci->lock);
568 549
569 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n", 550 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
570 xhci_readl(xhci, &xhci->op_regs->status)); 551 xhci_readl(xhci, &xhci->op_regs->status));
@@ -720,7 +701,8 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
720 unsigned long flags; 701 unsigned long flags;
721 int ret = 0; 702 int ret = 0;
722 unsigned int slot_id, ep_index; 703 unsigned int slot_id, ep_index;
723 704 struct urb_priv *urb_priv;
705 int size, i;
724 706
725 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0) 707 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
726 return -EINVAL; 708 return -EINVAL;
@@ -734,12 +716,36 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
734 ret = -EINVAL; 716 ret = -EINVAL;
735 goto exit; 717 goto exit;
736 } 718 }
737 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { 719 if (!HCD_HW_ACCESSIBLE(hcd)) {
738 if (!in_interrupt()) 720 if (!in_interrupt())
739 xhci_dbg(xhci, "urb submitted during PCI suspend\n"); 721 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
740 ret = -ESHUTDOWN; 722 ret = -ESHUTDOWN;
741 goto exit; 723 goto exit;
742 } 724 }
725
726 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
727 size = urb->number_of_packets;
728 else
729 size = 1;
730
731 urb_priv = kzalloc(sizeof(struct urb_priv) +
732 size * sizeof(struct xhci_td *), mem_flags);
733 if (!urb_priv)
734 return -ENOMEM;
735
736 for (i = 0; i < size; i++) {
737 urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags);
738 if (!urb_priv->td[i]) {
739 urb_priv->length = i;
740 xhci_urb_free_priv(xhci, urb_priv);
741 return -ENOMEM;
742 }
743 }
744
745 urb_priv->length = size;
746 urb_priv->td_cnt = 0;
747 urb->hcpriv = urb_priv;
748
743 if (usb_endpoint_xfer_control(&urb->ep->desc)) { 749 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
744 /* Check to see if the max packet size for the default control 750 /* Check to see if the max packet size for the default control
745 * endpoint changed during FS device enumeration 751 * endpoint changed during FS device enumeration
@@ -788,11 +794,18 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
788 slot_id, ep_index); 794 slot_id, ep_index);
789 spin_unlock_irqrestore(&xhci->lock, flags); 795 spin_unlock_irqrestore(&xhci->lock, flags);
790 } else { 796 } else {
791 ret = -EINVAL; 797 spin_lock_irqsave(&xhci->lock, flags);
798 if (xhci->xhc_state & XHCI_STATE_DYING)
799 goto dying;
800 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
801 slot_id, ep_index);
802 spin_unlock_irqrestore(&xhci->lock, flags);
792 } 803 }
793exit: 804exit:
794 return ret; 805 return ret;
795dying: 806dying:
807 xhci_urb_free_priv(xhci, urb_priv);
808 urb->hcpriv = NULL;
796 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for " 809 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
797 "non-responsive xHCI host.\n", 810 "non-responsive xHCI host.\n",
798 urb->ep->desc.bEndpointAddress, urb); 811 urb->ep->desc.bEndpointAddress, urb);
@@ -800,6 +813,47 @@ dying:
800 return -ESHUTDOWN; 813 return -ESHUTDOWN;
801} 814}
802 815
816/* Get the right ring for the given URB.
817 * If the endpoint supports streams, boundary check the URB's stream ID.
818 * If the endpoint doesn't support streams, return the singular endpoint ring.
819 */
820static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
821 struct urb *urb)
822{
823 unsigned int slot_id;
824 unsigned int ep_index;
825 unsigned int stream_id;
826 struct xhci_virt_ep *ep;
827
828 slot_id = urb->dev->slot_id;
829 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
830 stream_id = urb->stream_id;
831 ep = &xhci->devs[slot_id]->eps[ep_index];
832 /* Common case: no streams */
833 if (!(ep->ep_state & EP_HAS_STREAMS))
834 return ep->ring;
835
836 if (stream_id == 0) {
837 xhci_warn(xhci,
838 "WARN: Slot ID %u, ep index %u has streams, "
839 "but URB has no stream ID.\n",
840 slot_id, ep_index);
841 return NULL;
842 }
843
844 if (stream_id < ep->stream_info->num_streams)
845 return ep->stream_info->stream_rings[stream_id];
846
847 xhci_warn(xhci,
848 "WARN: Slot ID %u, ep index %u has "
849 "stream IDs 1 to %u allocated, "
850 "but stream ID %u is requested.\n",
851 slot_id, ep_index,
852 ep->stream_info->num_streams - 1,
853 stream_id);
854 return NULL;
855}
856
803/* 857/*
804 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop 858 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
805 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC 859 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
@@ -834,9 +888,10 @@ dying:
834int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) 888int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
835{ 889{
836 unsigned long flags; 890 unsigned long flags;
837 int ret; 891 int ret, i;
838 u32 temp; 892 u32 temp;
839 struct xhci_hcd *xhci; 893 struct xhci_hcd *xhci;
894 struct urb_priv *urb_priv;
840 struct xhci_td *td; 895 struct xhci_td *td;
841 unsigned int ep_index; 896 unsigned int ep_index;
842 struct xhci_ring *ep_ring; 897 struct xhci_ring *ep_ring;
@@ -851,12 +906,12 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
851 temp = xhci_readl(xhci, &xhci->op_regs->status); 906 temp = xhci_readl(xhci, &xhci->op_regs->status);
852 if (temp == 0xffffffff) { 907 if (temp == 0xffffffff) {
853 xhci_dbg(xhci, "HW died, freeing TD.\n"); 908 xhci_dbg(xhci, "HW died, freeing TD.\n");
854 td = (struct xhci_td *) urb->hcpriv; 909 urb_priv = urb->hcpriv;
855 910
856 usb_hcd_unlink_urb_from_ep(hcd, urb); 911 usb_hcd_unlink_urb_from_ep(hcd, urb);
857 spin_unlock_irqrestore(&xhci->lock, flags); 912 spin_unlock_irqrestore(&xhci->lock, flags);
858 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN); 913 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN);
859 kfree(td); 914 xhci_urb_free_priv(xhci, urb_priv);
860 return ret; 915 return ret;
861 } 916 }
862 if (xhci->xhc_state & XHCI_STATE_DYING) { 917 if (xhci->xhc_state & XHCI_STATE_DYING) {
@@ -884,9 +939,14 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
884 939
885 xhci_dbg(xhci, "Endpoint ring:\n"); 940 xhci_dbg(xhci, "Endpoint ring:\n");
886 xhci_debug_ring(xhci, ep_ring); 941 xhci_debug_ring(xhci, ep_ring);
887 td = (struct xhci_td *) urb->hcpriv;
888 942
889 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list); 943 urb_priv = urb->hcpriv;
944
945 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
946 td = urb_priv->td[i];
947 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
948 }
949
890 /* Queue a stop endpoint command, but only if this is 950 /* Queue a stop endpoint command, but only if this is
891 * the first cancellation to be handled. 951 * the first cancellation to be handled.
892 */ 952 */
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 6c7e3430ec9..34a60d9f056 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -720,6 +720,14 @@ struct xhci_virt_ep {
720 struct timer_list stop_cmd_timer; 720 struct timer_list stop_cmd_timer;
721 int stop_cmds_pending; 721 int stop_cmds_pending;
722 struct xhci_hcd *xhci; 722 struct xhci_hcd *xhci;
723 /*
724 * Sometimes the xHC can not process isochronous endpoint ring quickly
725 * enough, and it will miss some isoc tds on the ring and generate
726 * a Missed Service Error Event.
727 * Set skip flag when receive a Missed Service Error Event and
728 * process the missed tds on the endpoint ring.
729 */
730 bool skip;
723}; 731};
724 732
725struct xhci_virt_device { 733struct xhci_virt_device {
@@ -911,6 +919,9 @@ struct xhci_event_cmd {
911/* Control transfer TRB specific fields */ 919/* Control transfer TRB specific fields */
912#define TRB_DIR_IN (1<<16) 920#define TRB_DIR_IN (1<<16)
913 921
922/* Isochronous TRB specific fields */
923#define TRB_SIA (1<<31)
924
914struct xhci_generic_trb { 925struct xhci_generic_trb {
915 u32 field[4]; 926 u32 field[4];
916}; 927};
@@ -1082,6 +1093,12 @@ struct xhci_scratchpad {
1082 dma_addr_t *sp_dma_buffers; 1093 dma_addr_t *sp_dma_buffers;
1083}; 1094};
1084 1095
1096struct urb_priv {
1097 int length;
1098 int td_cnt;
1099 struct xhci_td *td[0];
1100};
1101
1085/* 1102/*
1086 * Each segment table entry is 4*32bits long. 1K seems like an ok size: 1103 * Each segment table entry is 4*32bits long. 1K seems like an ok size:
1087 * (1K bytes * 8bytes/bit) / (4*32 bits) = 64 segment entries in the table, 1104 * (1K bytes * 8bytes/bit) / (4*32 bits) = 64 segment entries in the table,
@@ -1130,7 +1147,7 @@ struct xhci_hcd {
1130 int page_size; 1147 int page_size;
1131 /* Valid values are 12 to 20, inclusive */ 1148 /* Valid values are 12 to 20, inclusive */
1132 int page_shift; 1149 int page_shift;
1133 /* only one MSI vector for now, but might need more later */ 1150 /* msi-x vectors */
1134 int msix_count; 1151 int msix_count;
1135 struct msix_entry *msix_entries; 1152 struct msix_entry *msix_entries;
1136 /* data structures */ 1153 /* data structures */
@@ -1327,11 +1344,6 @@ void xhci_setup_no_streams_ep_input_ctx(struct xhci_hcd *xhci,
1327struct xhci_ring *xhci_dma_to_transfer_ring( 1344struct xhci_ring *xhci_dma_to_transfer_ring(
1328 struct xhci_virt_ep *ep, 1345 struct xhci_virt_ep *ep,
1329 u64 address); 1346 u64 address);
1330struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1331 struct urb *urb);
1332struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
1333 unsigned int slot_id, unsigned int ep_index,
1334 unsigned int stream_id);
1335struct xhci_ring *xhci_stream_id_to_ring( 1347struct xhci_ring *xhci_stream_id_to_ring(
1336 struct xhci_virt_device *dev, 1348 struct xhci_virt_device *dev,
1337 unsigned int ep_index, 1349 unsigned int ep_index,
@@ -1339,6 +1351,7 @@ struct xhci_ring *xhci_stream_id_to_ring(
1339struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci, 1351struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
1340 bool allocate_in_ctx, bool allocate_completion, 1352 bool allocate_in_ctx, bool allocate_completion,
1341 gfp_t mem_flags); 1353 gfp_t mem_flags);
1354void xhci_urb_free_priv(struct xhci_hcd *xhci, struct urb_priv *urb_priv);
1342void xhci_free_command(struct xhci_hcd *xhci, 1355void xhci_free_command(struct xhci_hcd *xhci,
1343 struct xhci_command *command); 1356 struct xhci_command *command);
1344 1357
@@ -1358,6 +1371,7 @@ void xhci_stop(struct usb_hcd *hcd);
1358void xhci_shutdown(struct usb_hcd *hcd); 1371void xhci_shutdown(struct usb_hcd *hcd);
1359int xhci_get_frame(struct usb_hcd *hcd); 1372int xhci_get_frame(struct usb_hcd *hcd);
1360irqreturn_t xhci_irq(struct usb_hcd *hcd); 1373irqreturn_t xhci_irq(struct usb_hcd *hcd);
1374irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd);
1361int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev); 1375int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev);
1362void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev); 1376void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev);
1363int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev, 1377int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
@@ -1386,8 +1400,6 @@ struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
1386int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code); 1400int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code);
1387void xhci_ring_cmd_db(struct xhci_hcd *xhci); 1401void xhci_ring_cmd_db(struct xhci_hcd *xhci);
1388void *xhci_setup_one_noop(struct xhci_hcd *xhci); 1402void *xhci_setup_one_noop(struct xhci_hcd *xhci);
1389void xhci_handle_event(struct xhci_hcd *xhci);
1390void xhci_set_hc_event_deq(struct xhci_hcd *xhci);
1391int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id); 1403int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id);
1392int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, 1404int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
1393 u32 slot_id); 1405 u32 slot_id);
@@ -1401,6 +1413,8 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
1401 int slot_id, unsigned int ep_index); 1413 int slot_id, unsigned int ep_index);
1402int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb, 1414int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
1403 int slot_id, unsigned int ep_index); 1415 int slot_id, unsigned int ep_index);
1416int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
1417 struct urb *urb, int slot_id, unsigned int ep_index);
1404int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, 1418int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
1405 u32 slot_id, bool command_must_succeed); 1419 u32 slot_id, bool command_must_succeed);
1406int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, 1420int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
diff --git a/drivers/usb/misc/ftdi-elan.c b/drivers/usb/misc/ftdi-elan.c
index d7e034a5e1f..c5b571050d8 100644
--- a/drivers/usb/misc/ftdi-elan.c
+++ b/drivers/usb/misc/ftdi-elan.c
@@ -648,7 +648,7 @@ static int ftdi_elan_open(struct inode *inode, struct file *file)
648 648
649static int ftdi_elan_release(struct inode *inode, struct file *file) 649static int ftdi_elan_release(struct inode *inode, struct file *file)
650{ 650{
651 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data; 651 struct usb_ftdi *ftdi = file->private_data;
652 if (ftdi == NULL) 652 if (ftdi == NULL)
653 return -ENODEV; 653 return -ENODEV;
654 up(&ftdi->sw_lock); /* decrement the count on our device */ 654 up(&ftdi->sw_lock); /* decrement the count on our device */
@@ -671,7 +671,7 @@ static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
671 int bytes_read = 0; 671 int bytes_read = 0;
672 int retry_on_empty = 10; 672 int retry_on_empty = 10;
673 int retry_on_timeout = 5; 673 int retry_on_timeout = 5;
674 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data; 674 struct usb_ftdi *ftdi = file->private_data;
675 if (ftdi->disconnected > 0) { 675 if (ftdi->disconnected > 0) {
676 return -ENODEV; 676 return -ENODEV;
677 } 677 }
diff --git a/drivers/usb/misc/iowarrior.c b/drivers/usb/misc/iowarrior.c
index 7dc9d3c6998..2de49c8887c 100644
--- a/drivers/usb/misc/iowarrior.c
+++ b/drivers/usb/misc/iowarrior.c
@@ -18,7 +18,7 @@
18#include <linux/init.h> 18#include <linux/init.h>
19#include <linux/slab.h> 19#include <linux/slab.h>
20#include <linux/sched.h> 20#include <linux/sched.h>
21#include <linux/smp_lock.h> 21#include <linux/mutex.h>
22#include <linux/poll.h> 22#include <linux/poll.h>
23#include <linux/usb/iowarrior.h> 23#include <linux/usb/iowarrior.h>
24 24
@@ -61,6 +61,7 @@ MODULE_DESCRIPTION(DRIVER_DESC);
61MODULE_LICENSE("GPL"); 61MODULE_LICENSE("GPL");
62 62
63/* Module parameters */ 63/* Module parameters */
64static DEFINE_MUTEX(iowarrior_mutex);
64static int debug = 0; 65static int debug = 0;
65module_param(debug, bool, 0644); 66module_param(debug, bool, 0644);
66MODULE_PARM_DESC(debug, "debug=1 enables debugging messages"); 67MODULE_PARM_DESC(debug, "debug=1 enables debugging messages");
@@ -282,7 +283,7 @@ static ssize_t iowarrior_read(struct file *file, char __user *buffer,
282 int read_idx; 283 int read_idx;
283 int offset; 284 int offset;
284 285
285 dev = (struct iowarrior *)file->private_data; 286 dev = file->private_data;
286 287
287 /* verify that the device wasn't unplugged */ 288 /* verify that the device wasn't unplugged */
288 if (dev == NULL || !dev->present) 289 if (dev == NULL || !dev->present)
@@ -348,7 +349,7 @@ static ssize_t iowarrior_write(struct file *file,
348 char *buf = NULL; /* for IOW24 and IOW56 we need a buffer */ 349 char *buf = NULL; /* for IOW24 and IOW56 we need a buffer */
349 struct urb *int_out_urb = NULL; 350 struct urb *int_out_urb = NULL;
350 351
351 dev = (struct iowarrior *)file->private_data; 352 dev = file->private_data;
352 353
353 mutex_lock(&dev->mutex); 354 mutex_lock(&dev->mutex);
354 /* verify that the device wasn't unplugged */ 355 /* verify that the device wasn't unplugged */
@@ -483,7 +484,7 @@ static long iowarrior_ioctl(struct file *file, unsigned int cmd,
483 int retval; 484 int retval;
484 int io_res; /* checks for bytes read/written and copy_to/from_user results */ 485 int io_res; /* checks for bytes read/written and copy_to/from_user results */
485 486
486 dev = (struct iowarrior *)file->private_data; 487 dev = file->private_data;
487 if (dev == NULL) { 488 if (dev == NULL) {
488 return -ENODEV; 489 return -ENODEV;
489 } 490 }
@@ -493,7 +494,7 @@ static long iowarrior_ioctl(struct file *file, unsigned int cmd,
493 return -ENOMEM; 494 return -ENOMEM;
494 495
495 /* lock this object */ 496 /* lock this object */
496 lock_kernel(); 497 mutex_lock(&iowarrior_mutex);
497 mutex_lock(&dev->mutex); 498 mutex_lock(&dev->mutex);
498 499
499 /* verify that the device wasn't unplugged */ 500 /* verify that the device wasn't unplugged */
@@ -585,7 +586,7 @@ static long iowarrior_ioctl(struct file *file, unsigned int cmd,
585error_out: 586error_out:
586 /* unlock the device */ 587 /* unlock the device */
587 mutex_unlock(&dev->mutex); 588 mutex_unlock(&dev->mutex);
588 unlock_kernel(); 589 mutex_unlock(&iowarrior_mutex);
589 kfree(buffer); 590 kfree(buffer);
590 return retval; 591 return retval;
591} 592}
@@ -602,12 +603,12 @@ static int iowarrior_open(struct inode *inode, struct file *file)
602 603
603 dbg("%s", __func__); 604 dbg("%s", __func__);
604 605
605 lock_kernel(); 606 mutex_lock(&iowarrior_mutex);
606 subminor = iminor(inode); 607 subminor = iminor(inode);
607 608
608 interface = usb_find_interface(&iowarrior_driver, subminor); 609 interface = usb_find_interface(&iowarrior_driver, subminor);
609 if (!interface) { 610 if (!interface) {
610 unlock_kernel(); 611 mutex_unlock(&iowarrior_mutex);
611 err("%s - error, can't find device for minor %d", __func__, 612 err("%s - error, can't find device for minor %d", __func__,
612 subminor); 613 subminor);
613 return -ENODEV; 614 return -ENODEV;
@@ -617,7 +618,7 @@ static int iowarrior_open(struct inode *inode, struct file *file)
617 dev = usb_get_intfdata(interface); 618 dev = usb_get_intfdata(interface);
618 if (!dev) { 619 if (!dev) {
619 mutex_unlock(&iowarrior_open_disc_lock); 620 mutex_unlock(&iowarrior_open_disc_lock);
620 unlock_kernel(); 621 mutex_unlock(&iowarrior_mutex);
621 return -ENODEV; 622 return -ENODEV;
622 } 623 }
623 624
@@ -644,7 +645,7 @@ static int iowarrior_open(struct inode *inode, struct file *file)
644 645
645out: 646out:
646 mutex_unlock(&dev->mutex); 647 mutex_unlock(&dev->mutex);
647 unlock_kernel(); 648 mutex_unlock(&iowarrior_mutex);
648 return retval; 649 return retval;
649} 650}
650 651
@@ -656,7 +657,7 @@ static int iowarrior_release(struct inode *inode, struct file *file)
656 struct iowarrior *dev; 657 struct iowarrior *dev;
657 int retval = 0; 658 int retval = 0;
658 659
659 dev = (struct iowarrior *)file->private_data; 660 dev = file->private_data;
660 if (dev == NULL) { 661 if (dev == NULL) {
661 return -ENODEV; 662 return -ENODEV;
662 } 663 }
diff --git a/drivers/usb/misc/legousbtower.c b/drivers/usb/misc/legousbtower.c
index 8547bf9e317..6482c6e2e6b 100644
--- a/drivers/usb/misc/legousbtower.c
+++ b/drivers/usb/misc/legousbtower.c
@@ -448,7 +448,7 @@ static int tower_release (struct inode *inode, struct file *file)
448 448
449 dbg(2, "%s: enter", __func__); 449 dbg(2, "%s: enter", __func__);
450 450
451 dev = (struct lego_usb_tower *)file->private_data; 451 dev = file->private_data;
452 452
453 if (dev == NULL) { 453 if (dev == NULL) {
454 dbg(1, "%s: object is NULL", __func__); 454 dbg(1, "%s: object is NULL", __func__);
@@ -597,7 +597,7 @@ static ssize_t tower_read (struct file *file, char __user *buffer, size_t count,
597 597
598 dbg(2, "%s: enter, count = %Zd", __func__, count); 598 dbg(2, "%s: enter, count = %Zd", __func__, count);
599 599
600 dev = (struct lego_usb_tower *)file->private_data; 600 dev = file->private_data;
601 601
602 /* lock this object */ 602 /* lock this object */
603 if (mutex_lock_interruptible(&dev->lock)) { 603 if (mutex_lock_interruptible(&dev->lock)) {
@@ -686,7 +686,7 @@ static ssize_t tower_write (struct file *file, const char __user *buffer, size_t
686 686
687 dbg(2, "%s: enter, count = %Zd", __func__, count); 687 dbg(2, "%s: enter, count = %Zd", __func__, count);
688 688
689 dev = (struct lego_usb_tower *)file->private_data; 689 dev = file->private_data;
690 690
691 /* lock this object */ 691 /* lock this object */
692 if (mutex_lock_interruptible(&dev->lock)) { 692 if (mutex_lock_interruptible(&dev->lock)) {
diff --git a/drivers/usb/misc/rio500.c b/drivers/usb/misc/rio500.c
index a85771b1563..cc13ae61712 100644
--- a/drivers/usb/misc/rio500.c
+++ b/drivers/usb/misc/rio500.c
@@ -32,7 +32,7 @@
32#include <linux/kernel.h> 32#include <linux/kernel.h>
33#include <linux/signal.h> 33#include <linux/signal.h>
34#include <linux/sched.h> 34#include <linux/sched.h>
35#include <linux/smp_lock.h> 35#include <linux/mutex.h>
36#include <linux/errno.h> 36#include <linux/errno.h>
37#include <linux/random.h> 37#include <linux/random.h>
38#include <linux/poll.h> 38#include <linux/poll.h>
@@ -72,6 +72,7 @@ struct rio_usb_data {
72 struct mutex lock; /* general race avoidance */ 72 struct mutex lock; /* general race avoidance */
73}; 73};
74 74
75static DEFINE_MUTEX(rio500_mutex);
75static struct rio_usb_data rio_instance; 76static struct rio_usb_data rio_instance;
76 77
77static int open_rio(struct inode *inode, struct file *file) 78static int open_rio(struct inode *inode, struct file *file)
@@ -79,12 +80,12 @@ static int open_rio(struct inode *inode, struct file *file)
79 struct rio_usb_data *rio = &rio_instance; 80 struct rio_usb_data *rio = &rio_instance;
80 81
81 /* against disconnect() */ 82 /* against disconnect() */
82 lock_kernel(); 83 mutex_lock(&rio500_mutex);
83 mutex_lock(&(rio->lock)); 84 mutex_lock(&(rio->lock));
84 85
85 if (rio->isopen || !rio->present) { 86 if (rio->isopen || !rio->present) {
86 mutex_unlock(&(rio->lock)); 87 mutex_unlock(&(rio->lock));
87 unlock_kernel(); 88 mutex_unlock(&rio500_mutex);
88 return -EBUSY; 89 return -EBUSY;
89 } 90 }
90 rio->isopen = 1; 91 rio->isopen = 1;
@@ -94,7 +95,7 @@ static int open_rio(struct inode *inode, struct file *file)
94 mutex_unlock(&(rio->lock)); 95 mutex_unlock(&(rio->lock));
95 96
96 dev_info(&rio->rio_dev->dev, "Rio opened.\n"); 97 dev_info(&rio->rio_dev->dev, "Rio opened.\n");
97 unlock_kernel(); 98 mutex_unlock(&rio500_mutex);
98 99
99 return 0; 100 return 0;
100} 101}
@@ -491,7 +492,7 @@ static void disconnect_rio(struct usb_interface *intf)
491 struct rio_usb_data *rio = usb_get_intfdata (intf); 492 struct rio_usb_data *rio = usb_get_intfdata (intf);
492 493
493 usb_set_intfdata (intf, NULL); 494 usb_set_intfdata (intf, NULL);
494 lock_kernel(); 495 mutex_lock(&rio500_mutex);
495 if (rio) { 496 if (rio) {
496 usb_deregister_dev(intf, &usb_rio_class); 497 usb_deregister_dev(intf, &usb_rio_class);
497 498
@@ -501,7 +502,7 @@ static void disconnect_rio(struct usb_interface *intf)
501 /* better let it finish - the release will do whats needed */ 502 /* better let it finish - the release will do whats needed */
502 rio->rio_dev = NULL; 503 rio->rio_dev = NULL;
503 mutex_unlock(&(rio->lock)); 504 mutex_unlock(&(rio->lock));
504 unlock_kernel(); 505 mutex_unlock(&rio500_mutex);
505 return; 506 return;
506 } 507 }
507 kfree(rio->ibuf); 508 kfree(rio->ibuf);
@@ -512,7 +513,7 @@ static void disconnect_rio(struct usb_interface *intf)
512 rio->present = 0; 513 rio->present = 0;
513 mutex_unlock(&(rio->lock)); 514 mutex_unlock(&(rio->lock));
514 } 515 }
515 unlock_kernel(); 516 mutex_unlock(&rio500_mutex);
516} 517}
517 518
518static const struct usb_device_id rio_table[] = { 519static const struct usb_device_id rio_table[] = {
diff --git a/drivers/usb/misc/sisusbvga/sisusb.c b/drivers/usb/misc/sisusbvga/sisusb.c
index d25814c172b..70d00e99a4b 100644
--- a/drivers/usb/misc/sisusbvga/sisusb.c
+++ b/drivers/usb/misc/sisusbvga/sisusb.c
@@ -2487,7 +2487,7 @@ sisusb_release(struct inode *inode, struct file *file)
2487{ 2487{
2488 struct sisusb_usb_data *sisusb; 2488 struct sisusb_usb_data *sisusb;
2489 2489
2490 if (!(sisusb = (struct sisusb_usb_data *)file->private_data)) 2490 if (!(sisusb = file->private_data))
2491 return -ENODEV; 2491 return -ENODEV;
2492 2492
2493 mutex_lock(&sisusb->lock); 2493 mutex_lock(&sisusb->lock);
@@ -2519,7 +2519,7 @@ sisusb_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2519 u16 buf16; 2519 u16 buf16;
2520 u32 buf32, address; 2520 u32 buf32, address;
2521 2521
2522 if (!(sisusb = (struct sisusb_usb_data *)file->private_data)) 2522 if (!(sisusb = file->private_data))
2523 return -ENODEV; 2523 return -ENODEV;
2524 2524
2525 mutex_lock(&sisusb->lock); 2525 mutex_lock(&sisusb->lock);
@@ -2661,7 +2661,7 @@ sisusb_write(struct file *file, const char __user *buffer, size_t count,
2661 u16 buf16; 2661 u16 buf16;
2662 u32 buf32, address; 2662 u32 buf32, address;
2663 2663
2664 if (!(sisusb = (struct sisusb_usb_data *)file->private_data)) 2664 if (!(sisusb = file->private_data))
2665 return -ENODEV; 2665 return -ENODEV;
2666 2666
2667 mutex_lock(&sisusb->lock); 2667 mutex_lock(&sisusb->lock);
@@ -2804,7 +2804,7 @@ sisusb_lseek(struct file *file, loff_t offset, int orig)
2804 struct sisusb_usb_data *sisusb; 2804 struct sisusb_usb_data *sisusb;
2805 loff_t ret; 2805 loff_t ret;
2806 2806
2807 if (!(sisusb = (struct sisusb_usb_data *)file->private_data)) 2807 if (!(sisusb = file->private_data))
2808 return -ENODEV; 2808 return -ENODEV;
2809 2809
2810 mutex_lock(&sisusb->lock); 2810 mutex_lock(&sisusb->lock);
@@ -2969,7 +2969,7 @@ sisusb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2969 long retval = 0; 2969 long retval = 0;
2970 u32 __user *argp = (u32 __user *)arg; 2970 u32 __user *argp = (u32 __user *)arg;
2971 2971
2972 if (!(sisusb = (struct sisusb_usb_data *)file->private_data)) 2972 if (!(sisusb = file->private_data))
2973 return -ENODEV; 2973 return -ENODEV;
2974 2974
2975 mutex_lock(&sisusb->lock); 2975 mutex_lock(&sisusb->lock);
diff --git a/drivers/usb/misc/usblcd.c b/drivers/usb/misc/usblcd.c
index 7828c764b32..d00dde19194 100644
--- a/drivers/usb/misc/usblcd.c
+++ b/drivers/usb/misc/usblcd.c
@@ -16,7 +16,6 @@
16#include <linux/kernel.h> 16#include <linux/kernel.h>
17#include <linux/init.h> 17#include <linux/init.h>
18#include <linux/slab.h> 18#include <linux/slab.h>
19#include <linux/smp_lock.h>
20#include <linux/errno.h> 19#include <linux/errno.h>
21#include <linux/mutex.h> 20#include <linux/mutex.h>
22#include <asm/uaccess.h> 21#include <asm/uaccess.h>
@@ -30,6 +29,7 @@
30#define IOCTL_GET_DRV_VERSION 2 29#define IOCTL_GET_DRV_VERSION 2
31 30
32 31
32static DEFINE_MUTEX(lcd_mutex);
33static const struct usb_device_id id_table[] = { 33static const struct usb_device_id id_table[] = {
34 { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, }, 34 { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
35 { }, 35 { },
@@ -74,12 +74,12 @@ static int lcd_open(struct inode *inode, struct file *file)
74 struct usb_interface *interface; 74 struct usb_interface *interface;
75 int subminor, r; 75 int subminor, r;
76 76
77 lock_kernel(); 77 mutex_lock(&lcd_mutex);
78 subminor = iminor(inode); 78 subminor = iminor(inode);
79 79
80 interface = usb_find_interface(&lcd_driver, subminor); 80 interface = usb_find_interface(&lcd_driver, subminor);
81 if (!interface) { 81 if (!interface) {
82 unlock_kernel(); 82 mutex_unlock(&lcd_mutex);
83 err ("USBLCD: %s - error, can't find device for minor %d", 83 err ("USBLCD: %s - error, can't find device for minor %d",
84 __func__, subminor); 84 __func__, subminor);
85 return -ENODEV; 85 return -ENODEV;
@@ -89,7 +89,7 @@ static int lcd_open(struct inode *inode, struct file *file)
89 dev = usb_get_intfdata(interface); 89 dev = usb_get_intfdata(interface);
90 if (!dev) { 90 if (!dev) {
91 mutex_unlock(&open_disc_mutex); 91 mutex_unlock(&open_disc_mutex);
92 unlock_kernel(); 92 mutex_unlock(&lcd_mutex);
93 return -ENODEV; 93 return -ENODEV;
94 } 94 }
95 95
@@ -101,13 +101,13 @@ static int lcd_open(struct inode *inode, struct file *file)
101 r = usb_autopm_get_interface(interface); 101 r = usb_autopm_get_interface(interface);
102 if (r < 0) { 102 if (r < 0) {
103 kref_put(&dev->kref, lcd_delete); 103 kref_put(&dev->kref, lcd_delete);
104 unlock_kernel(); 104 mutex_unlock(&lcd_mutex);
105 return r; 105 return r;
106 } 106 }
107 107
108 /* save our object in the file's private structure */ 108 /* save our object in the file's private structure */
109 file->private_data = dev; 109 file->private_data = dev;
110 unlock_kernel(); 110 mutex_unlock(&lcd_mutex);
111 111
112 return 0; 112 return 0;
113} 113}
@@ -116,7 +116,7 @@ static int lcd_release(struct inode *inode, struct file *file)
116{ 116{
117 struct usb_lcd *dev; 117 struct usb_lcd *dev;
118 118
119 dev = (struct usb_lcd *)file->private_data; 119 dev = file->private_data;
120 if (dev == NULL) 120 if (dev == NULL)
121 return -ENODEV; 121 return -ENODEV;
122 122
@@ -132,7 +132,7 @@ static ssize_t lcd_read(struct file *file, char __user * buffer, size_t count, l
132 int retval = 0; 132 int retval = 0;
133 int bytes_read; 133 int bytes_read;
134 134
135 dev = (struct usb_lcd *)file->private_data; 135 dev = file->private_data;
136 136
137 /* do a blocking bulk read to get data from the device */ 137 /* do a blocking bulk read to get data from the device */
138 retval = usb_bulk_msg(dev->udev, 138 retval = usb_bulk_msg(dev->udev,
@@ -158,20 +158,20 @@ static long lcd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
158 u16 bcdDevice; 158 u16 bcdDevice;
159 char buf[30]; 159 char buf[30];
160 160
161 dev = (struct usb_lcd *)file->private_data; 161 dev = file->private_data;
162 if (dev == NULL) 162 if (dev == NULL)
163 return -ENODEV; 163 return -ENODEV;
164 164
165 switch (cmd) { 165 switch (cmd) {
166 case IOCTL_GET_HARD_VERSION: 166 case IOCTL_GET_HARD_VERSION:
167 lock_kernel(); 167 mutex_lock(&lcd_mutex);
168 bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice); 168 bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice);
169 sprintf(buf,"%1d%1d.%1d%1d", 169 sprintf(buf,"%1d%1d.%1d%1d",
170 (bcdDevice & 0xF000)>>12, 170 (bcdDevice & 0xF000)>>12,
171 (bcdDevice & 0xF00)>>8, 171 (bcdDevice & 0xF00)>>8,
172 (bcdDevice & 0xF0)>>4, 172 (bcdDevice & 0xF0)>>4,
173 (bcdDevice & 0xF)); 173 (bcdDevice & 0xF));
174 unlock_kernel(); 174 mutex_unlock(&lcd_mutex);
175 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0) 175 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
176 return -EFAULT; 176 return -EFAULT;
177 break; 177 break;
@@ -217,7 +217,7 @@ static ssize_t lcd_write(struct file *file, const char __user * user_buffer, siz
217 struct urb *urb = NULL; 217 struct urb *urb = NULL;
218 char *buf = NULL; 218 char *buf = NULL;
219 219
220 dev = (struct usb_lcd *)file->private_data; 220 dev = file->private_data;
221 221
222 /* verify that we actually have some data to write */ 222 /* verify that we actually have some data to write */
223 if (count == 0) 223 if (count == 0)
diff --git a/drivers/usb/misc/usbtest.c b/drivers/usb/misc/usbtest.c
index 16dffe99d9f..eef370eb7a5 100644
--- a/drivers/usb/misc/usbtest.c
+++ b/drivers/usb/misc/usbtest.c
@@ -136,7 +136,7 @@ try_iso:
136 iso_out = e; 136 iso_out = e;
137 } 137 }
138 } 138 }
139 if ((in && out) || (iso_in && iso_out)) 139 if ((in && out) || iso_in || iso_out)
140 goto found; 140 goto found;
141 } 141 }
142 return -EINVAL; 142 return -EINVAL;
@@ -162,6 +162,9 @@ found:
162 dev->in_iso_pipe = usb_rcvisocpipe (udev, 162 dev->in_iso_pipe = usb_rcvisocpipe (udev,
163 iso_in->desc.bEndpointAddress 163 iso_in->desc.bEndpointAddress
164 & USB_ENDPOINT_NUMBER_MASK); 164 & USB_ENDPOINT_NUMBER_MASK);
165 }
166
167 if (iso_out) {
165 dev->iso_out = &iso_out->desc; 168 dev->iso_out = &iso_out->desc;
166 dev->out_iso_pipe = usb_sndisocpipe (udev, 169 dev->out_iso_pipe = usb_sndisocpipe (udev,
167 iso_out->desc.bEndpointAddress 170 iso_out->desc.bEndpointAddress
@@ -1378,7 +1381,6 @@ static void iso_callback (struct urb *urb)
1378 break; 1381 break;
1379 } 1382 }
1380 } 1383 }
1381 simple_free_urb (urb);
1382 1384
1383 ctx->pending--; 1385 ctx->pending--;
1384 if (ctx->pending == 0) { 1386 if (ctx->pending == 0) {
@@ -1495,6 +1497,7 @@ test_iso_queue (struct usbtest_dev *dev, struct usbtest_param *param,
1495 } 1497 }
1496 1498
1497 simple_free_urb (urbs [i]); 1499 simple_free_urb (urbs [i]);
1500 urbs[i] = NULL;
1498 context.pending--; 1501 context.pending--;
1499 context.submit_error = 1; 1502 context.submit_error = 1;
1500 break; 1503 break;
@@ -1504,6 +1507,10 @@ test_iso_queue (struct usbtest_dev *dev, struct usbtest_param *param,
1504 1507
1505 wait_for_completion (&context.done); 1508 wait_for_completion (&context.done);
1506 1509
1510 for (i = 0; i < param->sglen; i++) {
1511 if (urbs[i])
1512 simple_free_urb(urbs[i]);
1513 }
1507 /* 1514 /*
1508 * Isochronous transfers are expected to fail sometimes. As an 1515 * Isochronous transfers are expected to fail sometimes. As an
1509 * arbitrary limit, we will report an error if any submissions 1516 * arbitrary limit, we will report an error if any submissions
@@ -1548,6 +1555,7 @@ fail:
1548 * off just killing the userspace task and waiting for it to exit. 1555 * off just killing the userspace task and waiting for it to exit.
1549 */ 1556 */
1550 1557
1558/* No BKL needed */
1551static int 1559static int
1552usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf) 1560usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
1553{ 1561{
@@ -2170,7 +2178,7 @@ static struct usb_driver usbtest_driver = {
2170 .name = "usbtest", 2178 .name = "usbtest",
2171 .id_table = id_table, 2179 .id_table = id_table,
2172 .probe = usbtest_probe, 2180 .probe = usbtest_probe,
2173 .ioctl = usbtest_ioctl, 2181 .unlocked_ioctl = usbtest_ioctl,
2174 .disconnect = usbtest_disconnect, 2182 .disconnect = usbtest_disconnect,
2175 .suspend = usbtest_suspend, 2183 .suspend = usbtest_suspend,
2176 .resume = usbtest_resume, 2184 .resume = usbtest_resume,
diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
index 61c76b13f0f..44cb37b5a4d 100644
--- a/drivers/usb/mon/mon_bin.c
+++ b/drivers/usb/mon/mon_bin.c
@@ -646,17 +646,14 @@ static int mon_bin_open(struct inode *inode, struct file *file)
646 size_t size; 646 size_t size;
647 int rc; 647 int rc;
648 648
649 lock_kernel();
650 mutex_lock(&mon_lock); 649 mutex_lock(&mon_lock);
651 if ((mbus = mon_bus_lookup(iminor(inode))) == NULL) { 650 if ((mbus = mon_bus_lookup(iminor(inode))) == NULL) {
652 mutex_unlock(&mon_lock); 651 mutex_unlock(&mon_lock);
653 unlock_kernel();
654 return -ENODEV; 652 return -ENODEV;
655 } 653 }
656 if (mbus != &mon_bus0 && mbus->u_bus == NULL) { 654 if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
657 printk(KERN_ERR TAG ": consistency error on open\n"); 655 printk(KERN_ERR TAG ": consistency error on open\n");
658 mutex_unlock(&mon_lock); 656 mutex_unlock(&mon_lock);
659 unlock_kernel();
660 return -ENODEV; 657 return -ENODEV;
661 } 658 }
662 659
@@ -689,7 +686,6 @@ static int mon_bin_open(struct inode *inode, struct file *file)
689 686
690 file->private_data = rp; 687 file->private_data = rp;
691 mutex_unlock(&mon_lock); 688 mutex_unlock(&mon_lock);
692 unlock_kernel();
693 return 0; 689 return 0;
694 690
695err_allocbuff: 691err_allocbuff:
@@ -698,7 +694,6 @@ err_allocvec:
698 kfree(rp); 694 kfree(rp);
699err_alloc: 695err_alloc:
700 mutex_unlock(&mon_lock); 696 mutex_unlock(&mon_lock);
701 unlock_kernel();
702 return rc; 697 return rc;
703} 698}
704 699
@@ -954,7 +949,7 @@ static int mon_bin_queued(struct mon_reader_bin *rp)
954 949
955/* 950/*
956 */ 951 */
957static int mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 952static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
958{ 953{
959 struct mon_reader_bin *rp = file->private_data; 954 struct mon_reader_bin *rp = file->private_data;
960 // struct mon_bus* mbus = rp->r.m_bus; 955 // struct mon_bus* mbus = rp->r.m_bus;
@@ -1009,7 +1004,7 @@ static int mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1009 1004
1010 mutex_lock(&rp->fetch_lock); 1005 mutex_lock(&rp->fetch_lock);
1011 spin_lock_irqsave(&rp->b_lock, flags); 1006 spin_lock_irqsave(&rp->b_lock, flags);
1012 mon_free_buff(rp->b_vec, size/CHUNK_SIZE); 1007 mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
1013 kfree(rp->b_vec); 1008 kfree(rp->b_vec);
1014 rp->b_vec = vec; 1009 rp->b_vec = vec;
1015 rp->b_size = size; 1010 rp->b_size = size;
@@ -1094,19 +1089,6 @@ static int mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1094 return ret; 1089 return ret;
1095} 1090}
1096 1091
1097static long mon_bin_unlocked_ioctl(struct file *file, unsigned int cmd,
1098 unsigned long arg)
1099{
1100 int ret;
1101
1102 lock_kernel();
1103 ret = mon_bin_ioctl(file, cmd, arg);
1104 unlock_kernel();
1105
1106 return ret;
1107}
1108
1109
1110#ifdef CONFIG_COMPAT 1092#ifdef CONFIG_COMPAT
1111static long mon_bin_compat_ioctl(struct file *file, 1093static long mon_bin_compat_ioctl(struct file *file,
1112 unsigned int cmd, unsigned long arg) 1094 unsigned int cmd, unsigned long arg)
@@ -1250,7 +1232,7 @@ static const struct file_operations mon_fops_binary = {
1250 .read = mon_bin_read, 1232 .read = mon_bin_read,
1251 /* .write = mon_text_write, */ 1233 /* .write = mon_text_write, */
1252 .poll = mon_bin_poll, 1234 .poll = mon_bin_poll,
1253 .unlocked_ioctl = mon_bin_unlocked_ioctl, 1235 .unlocked_ioctl = mon_bin_ioctl,
1254#ifdef CONFIG_COMPAT 1236#ifdef CONFIG_COMPAT
1255 .compat_ioctl = mon_bin_compat_ioctl, 1237 .compat_ioctl = mon_bin_compat_ioctl,
1256#endif 1238#endif
diff --git a/drivers/usb/musb/musb_core.c b/drivers/usb/musb/musb_core.c
index 3b795c56221..540c766c4f8 100644
--- a/drivers/usb/musb/musb_core.c
+++ b/drivers/usb/musb/musb_core.c
@@ -704,7 +704,6 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
704#ifdef CONFIG_USB_MUSB_HDRC_HCD 704#ifdef CONFIG_USB_MUSB_HDRC_HCD
705 if (int_usb & MUSB_INTR_CONNECT) { 705 if (int_usb & MUSB_INTR_CONNECT) {
706 struct usb_hcd *hcd = musb_to_hcd(musb); 706 struct usb_hcd *hcd = musb_to_hcd(musb);
707 void __iomem *mbase = musb->mregs;
708 707
709 handled = IRQ_HANDLED; 708 handled = IRQ_HANDLED;
710 musb->is_active = 1; 709 musb->is_active = 1;
@@ -717,9 +716,9 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
717 if (is_peripheral_active(musb)) { 716 if (is_peripheral_active(musb)) {
718 /* REVISIT HNP; just force disconnect */ 717 /* REVISIT HNP; just force disconnect */
719 } 718 }
720 musb_writew(mbase, MUSB_INTRTXE, musb->epmask); 719 musb_writew(musb->mregs, MUSB_INTRTXE, musb->epmask);
721 musb_writew(mbase, MUSB_INTRRXE, musb->epmask & 0xfffe); 720 musb_writew(musb->mregs, MUSB_INTRRXE, musb->epmask & 0xfffe);
722 musb_writeb(mbase, MUSB_INTRUSBE, 0xf7); 721 musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
723#endif 722#endif
724 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED 723 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
725 |USB_PORT_STAT_HIGH_SPEED 724 |USB_PORT_STAT_HIGH_SPEED
diff --git a/drivers/usb/musb/musb_debugfs.c b/drivers/usb/musb/musb_debugfs.c
index bba76af0c0c..c79a5e30d43 100644
--- a/drivers/usb/musb/musb_debugfs.c
+++ b/drivers/usb/musb/musb_debugfs.c
@@ -92,29 +92,29 @@ static const struct musb_register_map musb_regmap[] = {
92 { "LS_EOF1", 0x7E, 8 }, 92 { "LS_EOF1", 0x7E, 8 },
93 { "SOFT_RST", 0x7F, 8 }, 93 { "SOFT_RST", 0x7F, 8 },
94 { "DMA_CNTLch0", 0x204, 16 }, 94 { "DMA_CNTLch0", 0x204, 16 },
95 { "DMA_ADDRch0", 0x208, 16 }, 95 { "DMA_ADDRch0", 0x208, 32 },
96 { "DMA_COUNTch0", 0x20C, 16 }, 96 { "DMA_COUNTch0", 0x20C, 32 },
97 { "DMA_CNTLch1", 0x214, 16 }, 97 { "DMA_CNTLch1", 0x214, 16 },
98 { "DMA_ADDRch1", 0x218, 16 }, 98 { "DMA_ADDRch1", 0x218, 32 },
99 { "DMA_COUNTch1", 0x21C, 16 }, 99 { "DMA_COUNTch1", 0x21C, 32 },
100 { "DMA_CNTLch2", 0x224, 16 }, 100 { "DMA_CNTLch2", 0x224, 16 },
101 { "DMA_ADDRch2", 0x228, 16 }, 101 { "DMA_ADDRch2", 0x228, 32 },
102 { "DMA_COUNTch2", 0x22C, 16 }, 102 { "DMA_COUNTch2", 0x22C, 32 },
103 { "DMA_CNTLch3", 0x234, 16 }, 103 { "DMA_CNTLch3", 0x234, 16 },
104 { "DMA_ADDRch3", 0x238, 16 }, 104 { "DMA_ADDRch3", 0x238, 32 },
105 { "DMA_COUNTch3", 0x23C, 16 }, 105 { "DMA_COUNTch3", 0x23C, 32 },
106 { "DMA_CNTLch4", 0x244, 16 }, 106 { "DMA_CNTLch4", 0x244, 16 },
107 { "DMA_ADDRch4", 0x248, 16 }, 107 { "DMA_ADDRch4", 0x248, 32 },
108 { "DMA_COUNTch4", 0x24C, 16 }, 108 { "DMA_COUNTch4", 0x24C, 32 },
109 { "DMA_CNTLch5", 0x254, 16 }, 109 { "DMA_CNTLch5", 0x254, 16 },
110 { "DMA_ADDRch5", 0x258, 16 }, 110 { "DMA_ADDRch5", 0x258, 32 },
111 { "DMA_COUNTch5", 0x25C, 16 }, 111 { "DMA_COUNTch5", 0x25C, 32 },
112 { "DMA_CNTLch6", 0x264, 16 }, 112 { "DMA_CNTLch6", 0x264, 16 },
113 { "DMA_ADDRch6", 0x268, 16 }, 113 { "DMA_ADDRch6", 0x268, 32 },
114 { "DMA_COUNTch6", 0x26C, 16 }, 114 { "DMA_COUNTch6", 0x26C, 32 },
115 { "DMA_CNTLch7", 0x274, 16 }, 115 { "DMA_CNTLch7", 0x274, 16 },
116 { "DMA_ADDRch7", 0x278, 16 }, 116 { "DMA_ADDRch7", 0x278, 32 },
117 { "DMA_COUNTch7", 0x27C, 16 }, 117 { "DMA_COUNTch7", 0x27C, 32 },
118 { } /* Terminating Entry */ 118 { } /* Terminating Entry */
119}; 119};
120 120
diff --git a/drivers/usb/musb/musb_gadget_ep0.c b/drivers/usb/musb/musb_gadget_ep0.c
index 21b9788d024..59bef8f3a35 100644
--- a/drivers/usb/musb/musb_gadget_ep0.c
+++ b/drivers/usb/musb/musb_gadget_ep0.c
@@ -402,6 +402,9 @@ __acquires(musb->lock)
402 musb->g.a_alt_hnp_support = 1; 402 musb->g.a_alt_hnp_support = 1;
403 break; 403 break;
404#endif 404#endif
405 case USB_DEVICE_DEBUG_MODE:
406 handled = 0;
407 break;
405stall: 408stall:
406 default: 409 default:
407 handled = -EINVAL; 410 handled = -EINVAL;
diff --git a/drivers/usb/musb/musb_virthub.c b/drivers/usb/musb/musb_virthub.c
index 92e85e027cf..43233c397b6 100644
--- a/drivers/usb/musb/musb_virthub.c
+++ b/drivers/usb/musb/musb_virthub.c
@@ -244,7 +244,7 @@ int musb_hub_control(
244 244
245 spin_lock_irqsave(&musb->lock, flags); 245 spin_lock_irqsave(&musb->lock, flags);
246 246
247 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) { 247 if (unlikely(!HCD_HW_ACCESSIBLE(hcd))) {
248 spin_unlock_irqrestore(&musb->lock, flags); 248 spin_unlock_irqrestore(&musb->lock, flags);
249 return -ESHUTDOWN; 249 return -ESHUTDOWN;
250 } 250 }
diff --git a/drivers/usb/musb/musbhsdma.c b/drivers/usb/musb/musbhsdma.c
index dc66e4376d4..6dc107f2524 100644
--- a/drivers/usb/musb/musbhsdma.c
+++ b/drivers/usb/musb/musbhsdma.c
@@ -173,10 +173,7 @@ static int dma_channel_program(struct dma_channel *channel,
173 musb_channel->max_packet_sz = packet_sz; 173 musb_channel->max_packet_sz = packet_sz;
174 channel->status = MUSB_DMA_STATUS_BUSY; 174 channel->status = MUSB_DMA_STATUS_BUSY;
175 175
176 if ((mode == 1) && (len >= packet_sz)) 176 configure_channel(channel, packet_sz, mode, dma_addr, len);
177 configure_channel(channel, packet_sz, 1, dma_addr, len);
178 else
179 configure_channel(channel, packet_sz, 0, dma_addr, len);
180 177
181 return true; 178 return true;
182} 179}
diff --git a/drivers/usb/musb/omap2430.c b/drivers/usb/musb/omap2430.c
index e06d65e36bf..2111a241dd0 100644
--- a/drivers/usb/musb/omap2430.c
+++ b/drivers/usb/musb/omap2430.c
@@ -32,8 +32,6 @@
32#include <linux/clk.h> 32#include <linux/clk.h>
33#include <linux/io.h> 33#include <linux/io.h>
34 34
35#include <plat/mux.h>
36
37#include "musb_core.h" 35#include "musb_core.h"
38#include "omap2430.h" 36#include "omap2430.h"
39 37
@@ -194,10 +192,6 @@ int __init musb_platform_init(struct musb *musb, void *board_data)
194 u32 l; 192 u32 l;
195 struct omap_musb_board_data *data = board_data; 193 struct omap_musb_board_data *data = board_data;
196 194
197#if defined(CONFIG_ARCH_OMAP2430)
198 omap_cfg_reg(AE5_2430_USB0HS_STP);
199#endif
200
201 /* We require some kind of external transceiver, hooked 195 /* We require some kind of external transceiver, hooked
202 * up through ULPI. TWL4030-family PMICs include one, 196 * up through ULPI. TWL4030-family PMICs include one,
203 * which needs a driver, drivers aren't always needed. 197 * which needs a driver, drivers aren't always needed.
diff --git a/drivers/usb/otg/Kconfig b/drivers/usb/otg/Kconfig
index 3d2d3e549bd..3b1289572d7 100644
--- a/drivers/usb/otg/Kconfig
+++ b/drivers/usb/otg/Kconfig
@@ -49,8 +49,6 @@ config USB_ULPI
49 Enable this to support ULPI connected USB OTG transceivers which 49 Enable this to support ULPI connected USB OTG transceivers which
50 are likely found on embedded boards. 50 are likely found on embedded boards.
51 51
52 The only chip currently supported is NXP's ISP1504
53
54config TWL4030_USB 52config TWL4030_USB
55 tristate "TWL4030 USB Transceiver Driver" 53 tristate "TWL4030 USB Transceiver Driver"
56 depends on TWL4030_CORE && REGULATOR_TWL4030 54 depends on TWL4030_CORE && REGULATOR_TWL4030
diff --git a/drivers/usb/otg/ulpi.c b/drivers/usb/otg/ulpi.c
index d331b222ad2..ccc81950822 100644
--- a/drivers/usb/otg/ulpi.c
+++ b/drivers/usb/otg/ulpi.c
@@ -31,30 +31,110 @@
31 31
32#define ULPI_ID(vendor, product) (((vendor) << 16) | (product)) 32#define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
33 33
34#define TR_FLAG(flags, a, b) (((flags) & a) ? b : 0)
35
36/* ULPI hardcoded IDs, used for probing */ 34/* ULPI hardcoded IDs, used for probing */
37static unsigned int ulpi_ids[] = { 35static unsigned int ulpi_ids[] = {
38 ULPI_ID(0x04cc, 0x1504), /* NXP ISP1504 */ 36 ULPI_ID(0x04cc, 0x1504), /* NXP ISP1504 */
37 ULPI_ID(0x0424, 0x0006), /* SMSC USB3319 */
39}; 38};
40 39
41static int ulpi_set_flags(struct otg_transceiver *otg) 40static int ulpi_set_otg_flags(struct otg_transceiver *otg)
42{ 41{
43 unsigned int flags = 0; 42 unsigned int flags = ULPI_OTG_CTRL_DP_PULLDOWN |
43 ULPI_OTG_CTRL_DM_PULLDOWN;
44 44
45 if (otg->flags & USB_OTG_PULLUP_ID) 45 if (otg->flags & ULPI_OTG_ID_PULLUP)
46 flags |= ULPI_OTG_CTRL_ID_PULLUP; 46 flags |= ULPI_OTG_CTRL_ID_PULLUP;
47 47
48 if (otg->flags & USB_OTG_PULLDOWN_DM) 48 /*
49 flags |= ULPI_OTG_CTRL_DM_PULLDOWN; 49 * ULPI Specification rev.1.1 default
50 * for Dp/DmPulldown is enabled.
51 */
52 if (otg->flags & ULPI_OTG_DP_PULLDOWN_DIS)
53 flags &= ~ULPI_OTG_CTRL_DP_PULLDOWN;
50 54
51 if (otg->flags & USB_OTG_PULLDOWN_DP) 55 if (otg->flags & ULPI_OTG_DM_PULLDOWN_DIS)
52 flags |= ULPI_OTG_CTRL_DP_PULLDOWN; 56 flags &= ~ULPI_OTG_CTRL_DM_PULLDOWN;
53 57
54 if (otg->flags & USB_OTG_EXT_VBUS_INDICATOR) 58 if (otg->flags & ULPI_OTG_EXTVBUSIND)
55 flags |= ULPI_OTG_CTRL_EXTVBUSIND; 59 flags |= ULPI_OTG_CTRL_EXTVBUSIND;
56 60
57 return otg_io_write(otg, flags, ULPI_SET(ULPI_OTG_CTRL)); 61 return otg_io_write(otg, flags, ULPI_OTG_CTRL);
62}
63
64static int ulpi_set_fc_flags(struct otg_transceiver *otg)
65{
66 unsigned int flags = 0;
67
68 /*
69 * ULPI Specification rev.1.1 default
70 * for XcvrSelect is Full Speed.
71 */
72 if (otg->flags & ULPI_FC_HS)
73 flags |= ULPI_FUNC_CTRL_HIGH_SPEED;
74 else if (otg->flags & ULPI_FC_LS)
75 flags |= ULPI_FUNC_CTRL_LOW_SPEED;
76 else if (otg->flags & ULPI_FC_FS4LS)
77 flags |= ULPI_FUNC_CTRL_FS4LS;
78 else
79 flags |= ULPI_FUNC_CTRL_FULL_SPEED;
80
81 if (otg->flags & ULPI_FC_TERMSEL)
82 flags |= ULPI_FUNC_CTRL_TERMSELECT;
83
84 /*
85 * ULPI Specification rev.1.1 default
86 * for OpMode is Normal Operation.
87 */
88 if (otg->flags & ULPI_FC_OP_NODRV)
89 flags |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
90 else if (otg->flags & ULPI_FC_OP_DIS_NRZI)
91 flags |= ULPI_FUNC_CTRL_OPMODE_DISABLE_NRZI;
92 else if (otg->flags & ULPI_FC_OP_NSYNC_NEOP)
93 flags |= ULPI_FUNC_CTRL_OPMODE_NOSYNC_NOEOP;
94 else
95 flags |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
96
97 /*
98 * ULPI Specification rev.1.1 default
99 * for SuspendM is Powered.
100 */
101 flags |= ULPI_FUNC_CTRL_SUSPENDM;
102
103 return otg_io_write(otg, flags, ULPI_FUNC_CTRL);
104}
105
106static int ulpi_set_ic_flags(struct otg_transceiver *otg)
107{
108 unsigned int flags = 0;
109
110 if (otg->flags & ULPI_IC_AUTORESUME)
111 flags |= ULPI_IFC_CTRL_AUTORESUME;
112
113 if (otg->flags & ULPI_IC_EXTVBUS_INDINV)
114 flags |= ULPI_IFC_CTRL_EXTERNAL_VBUS;
115
116 if (otg->flags & ULPI_IC_IND_PASSTHRU)
117 flags |= ULPI_IFC_CTRL_PASSTHRU;
118
119 if (otg->flags & ULPI_IC_PROTECT_DIS)
120 flags |= ULPI_IFC_CTRL_PROTECT_IFC_DISABLE;
121
122 return otg_io_write(otg, flags, ULPI_IFC_CTRL);
123}
124
125static int ulpi_set_flags(struct otg_transceiver *otg)
126{
127 int ret;
128
129 ret = ulpi_set_otg_flags(otg);
130 if (ret)
131 return ret;
132
133 ret = ulpi_set_ic_flags(otg);
134 if (ret)
135 return ret;
136
137 return ulpi_set_fc_flags(otg);
58} 138}
59 139
60static int ulpi_init(struct otg_transceiver *otg) 140static int ulpi_init(struct otg_transceiver *otg)
@@ -81,6 +161,31 @@ static int ulpi_init(struct otg_transceiver *otg)
81 return -ENODEV; 161 return -ENODEV;
82} 162}
83 163
164static int ulpi_set_host(struct otg_transceiver *otg, struct usb_bus *host)
165{
166 unsigned int flags = otg_io_read(otg, ULPI_IFC_CTRL);
167
168 if (!host) {
169 otg->host = NULL;
170 return 0;
171 }
172
173 otg->host = host;
174
175 flags &= ~(ULPI_IFC_CTRL_6_PIN_SERIAL_MODE |
176 ULPI_IFC_CTRL_3_PIN_SERIAL_MODE |
177 ULPI_IFC_CTRL_CARKITMODE);
178
179 if (otg->flags & ULPI_IC_6PIN_SERIAL)
180 flags |= ULPI_IFC_CTRL_6_PIN_SERIAL_MODE;
181 else if (otg->flags & ULPI_IC_3PIN_SERIAL)
182 flags |= ULPI_IFC_CTRL_3_PIN_SERIAL_MODE;
183 else if (otg->flags & ULPI_IC_CARKIT)
184 flags |= ULPI_IFC_CTRL_CARKITMODE;
185
186 return otg_io_write(otg, flags, ULPI_IFC_CTRL);
187}
188
84static int ulpi_set_vbus(struct otg_transceiver *otg, bool on) 189static int ulpi_set_vbus(struct otg_transceiver *otg, bool on)
85{ 190{
86 unsigned int flags = otg_io_read(otg, ULPI_OTG_CTRL); 191 unsigned int flags = otg_io_read(otg, ULPI_OTG_CTRL);
@@ -88,14 +193,14 @@ static int ulpi_set_vbus(struct otg_transceiver *otg, bool on)
88 flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT); 193 flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT);
89 194
90 if (on) { 195 if (on) {
91 if (otg->flags & USB_OTG_DRV_VBUS) 196 if (otg->flags & ULPI_OTG_DRVVBUS)
92 flags |= ULPI_OTG_CTRL_DRVVBUS; 197 flags |= ULPI_OTG_CTRL_DRVVBUS;
93 198
94 if (otg->flags & USB_OTG_DRV_VBUS_EXT) 199 if (otg->flags & ULPI_OTG_DRVVBUS_EXT)
95 flags |= ULPI_OTG_CTRL_DRVVBUS_EXT; 200 flags |= ULPI_OTG_CTRL_DRVVBUS_EXT;
96 } 201 }
97 202
98 return otg_io_write(otg, flags, ULPI_SET(ULPI_OTG_CTRL)); 203 return otg_io_write(otg, flags, ULPI_OTG_CTRL);
99} 204}
100 205
101struct otg_transceiver * 206struct otg_transceiver *
@@ -112,6 +217,7 @@ otg_ulpi_create(struct otg_io_access_ops *ops,
112 otg->flags = flags; 217 otg->flags = flags;
113 otg->io_ops = ops; 218 otg->io_ops = ops;
114 otg->init = ulpi_init; 219 otg->init = ulpi_init;
220 otg->set_host = ulpi_set_host;
115 otg->set_vbus = ulpi_set_vbus; 221 otg->set_vbus = ulpi_set_vbus;
116 222
117 return otg; 223 return otg;
diff --git a/drivers/usb/serial/Kconfig b/drivers/usb/serial/Kconfig
index bd8aab0ef1c..916b2b6d765 100644
--- a/drivers/usb/serial/Kconfig
+++ b/drivers/usb/serial/Kconfig
@@ -642,6 +642,15 @@ config USB_SERIAL_ZIO
642 To compile this driver as a module, choose M here: the 642 To compile this driver as a module, choose M here: the
643 module will be called zio. 643 module will be called zio.
644 644
645config USB_SERIAL_SSU100
646 tristate "USB Quatech SSU-100 Single Port Serial Driver"
647 help
648 Say Y here if you want to use the Quatech SSU-100 single
649 port usb to serial adapter.
650
651 To compile this driver as a module, choose M here: the
652 module will be called ssu100.
653
645config USB_SERIAL_DEBUG 654config USB_SERIAL_DEBUG
646 tristate "USB Debugging Device" 655 tristate "USB Debugging Device"
647 help 656 help
diff --git a/drivers/usb/serial/Makefile b/drivers/usb/serial/Makefile
index e54c728c016..40ebe17b6ea 100644
--- a/drivers/usb/serial/Makefile
+++ b/drivers/usb/serial/Makefile
@@ -51,6 +51,7 @@ obj-$(CONFIG_USB_SERIAL_SAFE) += safe_serial.o
51obj-$(CONFIG_USB_SERIAL_SIEMENS_MPI) += siemens_mpi.o 51obj-$(CONFIG_USB_SERIAL_SIEMENS_MPI) += siemens_mpi.o
52obj-$(CONFIG_USB_SERIAL_SIERRAWIRELESS) += sierra.o 52obj-$(CONFIG_USB_SERIAL_SIERRAWIRELESS) += sierra.o
53obj-$(CONFIG_USB_SERIAL_SPCP8X5) += spcp8x5.o 53obj-$(CONFIG_USB_SERIAL_SPCP8X5) += spcp8x5.o
54obj-$(CONFIG_USB_SERIAL_SSU100) += ssu100.o
54obj-$(CONFIG_USB_SERIAL_SYMBOL) += symbolserial.o 55obj-$(CONFIG_USB_SERIAL_SYMBOL) += symbolserial.o
55obj-$(CONFIG_USB_SERIAL_WWAN) += usb_wwan.o 56obj-$(CONFIG_USB_SERIAL_WWAN) += usb_wwan.o
56obj-$(CONFIG_USB_SERIAL_TI) += ti_usb_3410_5052.o 57obj-$(CONFIG_USB_SERIAL_TI) += ti_usb_3410_5052.o
diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index 8b8c7976b4c..2bef4415c19 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -126,6 +126,10 @@ static const struct usb_device_id id_table[] = {
126 { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */ 126 { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
127 { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */ 127 { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
128 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */ 128 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
129 { USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
130 { USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
131 { USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
132 { USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
129 { } /* Terminating Entry */ 133 { } /* Terminating Entry */
130}; 134};
131 135
diff --git a/drivers/usb/serial/digi_acceleport.c b/drivers/usb/serial/digi_acceleport.c
index fd35f73b572..b92070c103c 100644
--- a/drivers/usb/serial/digi_acceleport.c
+++ b/drivers/usb/serial/digi_acceleport.c
@@ -609,8 +609,10 @@ static void digi_wakeup_write_lock(struct work_struct *work)
609static void digi_wakeup_write(struct usb_serial_port *port) 609static void digi_wakeup_write(struct usb_serial_port *port)
610{ 610{
611 struct tty_struct *tty = tty_port_tty_get(&port->port); 611 struct tty_struct *tty = tty_port_tty_get(&port->port);
612 tty_wakeup(tty); 612 if (tty) {
613 tty_kref_put(tty); 613 tty_wakeup(tty);
614 tty_kref_put(tty);
615 }
614} 616}
615 617
616 618
@@ -1682,7 +1684,7 @@ static int digi_read_inb_callback(struct urb *urb)
1682 priv->dp_throttle_restart = 1; 1684 priv->dp_throttle_restart = 1;
1683 1685
1684 /* receive data */ 1686 /* receive data */
1685 if (opcode == DIGI_CMD_RECEIVE_DATA) { 1687 if (tty && opcode == DIGI_CMD_RECEIVE_DATA) {
1686 /* get flag from port_status */ 1688 /* get flag from port_status */
1687 flag = 0; 1689 flag = 0;
1688 1690
@@ -1763,10 +1765,12 @@ static int digi_read_oob_callback(struct urb *urb)
1763 return -1; 1765 return -1;
1764 1766
1765 tty = tty_port_tty_get(&port->port); 1767 tty = tty_port_tty_get(&port->port);
1768
1766 rts = 0; 1769 rts = 0;
1767 rts = tty->termios->c_cflag & CRTSCTS; 1770 if (tty)
1771 rts = tty->termios->c_cflag & CRTSCTS;
1768 1772
1769 if (opcode == DIGI_CMD_READ_INPUT_SIGNALS) { 1773 if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
1770 spin_lock(&priv->dp_port_lock); 1774 spin_lock(&priv->dp_port_lock);
1771 /* convert from digi flags to termiox flags */ 1775 /* convert from digi flags to termiox flags */
1772 if (val & DIGI_READ_INPUT_SIGNALS_CTS) { 1776 if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index e34023ff577..4fc588cc471 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -157,6 +157,9 @@ static struct usb_device_id id_table_combined [] = {
157 { USB_DEVICE(FTDI_VID, FTDI_SCS_DEVICE_5_PID) }, 157 { USB_DEVICE(FTDI_VID, FTDI_SCS_DEVICE_5_PID) },
158 { USB_DEVICE(FTDI_VID, FTDI_SCS_DEVICE_6_PID) }, 158 { USB_DEVICE(FTDI_VID, FTDI_SCS_DEVICE_6_PID) },
159 { USB_DEVICE(FTDI_VID, FTDI_SCS_DEVICE_7_PID) }, 159 { USB_DEVICE(FTDI_VID, FTDI_SCS_DEVICE_7_PID) },
160 { USB_DEVICE(FTDI_VID, FTDI_USINT_CAT_PID) },
161 { USB_DEVICE(FTDI_VID, FTDI_USINT_WKEY_PID) },
162 { USB_DEVICE(FTDI_VID, FTDI_USINT_RS232_PID) },
160 { USB_DEVICE(FTDI_VID, FTDI_ACTZWAVE_PID) }, 163 { USB_DEVICE(FTDI_VID, FTDI_ACTZWAVE_PID) },
161 { USB_DEVICE(FTDI_VID, FTDI_IRTRANS_PID) }, 164 { USB_DEVICE(FTDI_VID, FTDI_IRTRANS_PID) },
162 { USB_DEVICE(FTDI_VID, FTDI_IPLUS_PID) }, 165 { USB_DEVICE(FTDI_VID, FTDI_IPLUS_PID) },
@@ -746,6 +749,7 @@ static struct usb_device_id id_table_combined [] = {
746 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk }, 749 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
747 { USB_DEVICE(FTDI_VID, XVERVE_SIGNALYZER_SH4_PID), 750 { USB_DEVICE(FTDI_VID, XVERVE_SIGNALYZER_SH4_PID),
748 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk }, 751 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
752 { USB_DEVICE(FTDI_VID, SEGWAY_RMP200_PID) },
749 { }, /* Optional parameter entry */ 753 { }, /* Optional parameter entry */
750 { } /* Terminating entry */ 754 { } /* Terminating entry */
751}; 755};
diff --git a/drivers/usb/serial/ftdi_sio_ids.h b/drivers/usb/serial/ftdi_sio_ids.h
index d01946db8fa..6e612c52e76 100644
--- a/drivers/usb/serial/ftdi_sio_ids.h
+++ b/drivers/usb/serial/ftdi_sio_ids.h
@@ -40,6 +40,11 @@
40 40
41#define FTDI_NXTCAM_PID 0xABB8 /* NXTCam for Mindstorms NXT */ 41#define FTDI_NXTCAM_PID 0xABB8 /* NXTCam for Mindstorms NXT */
42 42
43/* US Interface Navigator (http://www.usinterface.com/) */
44#define FTDI_USINT_CAT_PID 0xb810 /* Navigator CAT and 2nd PTT lines */
45#define FTDI_USINT_WKEY_PID 0xb811 /* Navigator WKEY and FSK lines */
46#define FTDI_USINT_RS232_PID 0xb812 /* Navigator RS232 and CONFIG lines */
47
43/* OOCDlink by Joern Kaipf <joernk@web.de> 48/* OOCDlink by Joern Kaipf <joernk@web.de>
44 * (http://www.joernonline.de/dw/doku.php?id=start&idx=projects:oocdlink) */ 49 * (http://www.joernonline.de/dw/doku.php?id=start&idx=projects:oocdlink) */
45#define FTDI_OOCDLINK_PID 0xbaf8 /* Amontec JTAGkey */ 50#define FTDI_OOCDLINK_PID 0xbaf8 /* Amontec JTAGkey */
@@ -1032,3 +1037,8 @@
1032#define XVERVE_SIGNALYZER_SH2_PID 0xBCA2 1037#define XVERVE_SIGNALYZER_SH2_PID 0xBCA2
1033#define XVERVE_SIGNALYZER_SH4_PID 0xBCA4 1038#define XVERVE_SIGNALYZER_SH4_PID 0xBCA4
1034 1039
1040/*
1041 * Segway Robotic Mobility Platform USB interface (using VID 0x0403)
1042 * Submitted by John G. Rogers
1043 */
1044#define SEGWAY_RMP200_PID 0xe729
diff --git a/drivers/usb/serial/generic.c b/drivers/usb/serial/generic.c
index a817ced8283..ca92f67747c 100644
--- a/drivers/usb/serial/generic.c
+++ b/drivers/usb/serial/generic.c
@@ -208,18 +208,23 @@ retry:
208 urb->transfer_buffer_length = count; 208 urb->transfer_buffer_length = count;
209 usb_serial_debug_data(debug, &port->dev, __func__, count, 209 usb_serial_debug_data(debug, &port->dev, __func__, count,
210 urb->transfer_buffer); 210 urb->transfer_buffer);
211 spin_lock_irqsave(&port->lock, flags);
212 port->tx_bytes += count;
213 spin_unlock_irqrestore(&port->lock, flags);
214
215 clear_bit(i, &port->write_urbs_free);
211 result = usb_submit_urb(urb, GFP_ATOMIC); 216 result = usb_submit_urb(urb, GFP_ATOMIC);
212 if (result) { 217 if (result) {
213 dev_err(&port->dev, "%s - error submitting urb: %d\n", 218 dev_err(&port->dev, "%s - error submitting urb: %d\n",
214 __func__, result); 219 __func__, result);
220 set_bit(i, &port->write_urbs_free);
221 spin_lock_irqsave(&port->lock, flags);
222 port->tx_bytes -= count;
223 spin_unlock_irqrestore(&port->lock, flags);
224
215 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags); 225 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
216 return result; 226 return result;
217 } 227 }
218 clear_bit(i, &port->write_urbs_free);
219
220 spin_lock_irqsave(&port->lock, flags);
221 port->tx_bytes += count;
222 spin_unlock_irqrestore(&port->lock, flags);
223 228
224 /* Try sending off another urb, unless in irq context (in which case 229 /* Try sending off another urb, unless in irq context (in which case
225 * there will be no free urb). */ 230 * there will be no free urb). */
diff --git a/drivers/usb/serial/io_ti.c b/drivers/usb/serial/io_ti.c
index 4a6da66d5fd..9d6be304dff 100644
--- a/drivers/usb/serial/io_ti.c
+++ b/drivers/usb/serial/io_ti.c
@@ -1298,7 +1298,7 @@ static int download_fw(struct edgeport_serial *serial)
1298 kfree(header); 1298 kfree(header);
1299 kfree(rom_desc); 1299 kfree(rom_desc);
1300 kfree(ti_manuf_desc); 1300 kfree(ti_manuf_desc);
1301 return status; 1301 return -EINVAL;
1302 } 1302 }
1303 1303
1304 /* verify the write -- must do this in order for 1304 /* verify the write -- must do this in order for
@@ -1321,7 +1321,7 @@ static int download_fw(struct edgeport_serial *serial)
1321 kfree(header); 1321 kfree(header);
1322 kfree(rom_desc); 1322 kfree(rom_desc);
1323 kfree(ti_manuf_desc); 1323 kfree(ti_manuf_desc);
1324 return status; 1324 return -EINVAL;
1325 } 1325 }
1326 1326
1327 kfree(vheader); 1327 kfree(vheader);
diff --git a/drivers/usb/serial/ipaq.c b/drivers/usb/serial/ipaq.c
index 28913fa95fb..4735931b4c7 100644
--- a/drivers/usb/serial/ipaq.c
+++ b/drivers/usb/serial/ipaq.c
@@ -534,7 +534,6 @@ static struct usb_device_id ipaq_id_table [] = {
534 { USB_DEVICE(0x413C, 0x4009) }, /* Dell Axim USB Sync */ 534 { USB_DEVICE(0x413C, 0x4009) }, /* Dell Axim USB Sync */
535 { USB_DEVICE(0x4505, 0x0010) }, /* Smartphone */ 535 { USB_DEVICE(0x4505, 0x0010) }, /* Smartphone */
536 { USB_DEVICE(0x5E04, 0xCE00) }, /* SAGEM Wireless Assistant */ 536 { USB_DEVICE(0x5E04, 0xCE00) }, /* SAGEM Wireless Assistant */
537 { USB_DEVICE(0x0BB4, 0x00CF) }, /* HTC smartphone modems */
538 { } /* Terminating entry */ 537 { } /* Terminating entry */
539}; 538};
540 539
diff --git a/drivers/usb/serial/iuu_phoenix.c b/drivers/usb/serial/iuu_phoenix.c
index 55766a65f0a..12ed594f5f8 100644
--- a/drivers/usb/serial/iuu_phoenix.c
+++ b/drivers/usb/serial/iuu_phoenix.c
@@ -1,6 +1,8 @@
1/* 1/*
2 * Infinity Unlimited USB Phoenix driver 2 * Infinity Unlimited USB Phoenix driver
3 * 3 *
4 * Copyright (C) 2010 James Courtier-Dutton (James@superbug.co.uk)
5
4 * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com) 6 * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
5 * 7 *
6 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás) 8 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
@@ -40,7 +42,7 @@ static int debug;
40/* 42/*
41 * Version Information 43 * Version Information
42 */ 44 */
43#define DRIVER_VERSION "v0.11" 45#define DRIVER_VERSION "v0.12"
44#define DRIVER_DESC "Infinity USB Unlimited Phoenix driver" 46#define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
45 47
46static const struct usb_device_id id_table[] = { 48static const struct usb_device_id id_table[] = {
@@ -81,6 +83,9 @@ struct iuu_private {
81 u8 *dbgbuf; /* debug buffer */ 83 u8 *dbgbuf; /* debug buffer */
82 u8 len; 84 u8 len;
83 int vcc; /* vcc (either 3 or 5 V) */ 85 int vcc; /* vcc (either 3 or 5 V) */
86 u32 baud;
87 u32 boost;
88 u32 clk;
84}; 89};
85 90
86 91
@@ -157,13 +162,14 @@ static int iuu_tiocmset(struct tty_struct *tty, struct file *file,
157 port->number, set, clear); 162 port->number, set, clear);
158 163
159 spin_lock_irqsave(&priv->lock, flags); 164 spin_lock_irqsave(&priv->lock, flags);
160 if (set & TIOCM_RTS)
161 priv->tiostatus = TIOCM_RTS;
162 165
163 if (!(set & TIOCM_RTS) && priv->tiostatus == TIOCM_RTS) { 166 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
164 dbg("%s TIOCMSET RESET called !!!", __func__); 167 dbg("%s TIOCMSET RESET called !!!", __func__);
165 priv->reset = 1; 168 priv->reset = 1;
166 } 169 }
170 if (set & TIOCM_RTS)
171 priv->tiostatus = TIOCM_RTS;
172
167 spin_unlock_irqrestore(&priv->lock, flags); 173 spin_unlock_irqrestore(&priv->lock, flags);
168 return 0; 174 return 0;
169} 175}
@@ -850,20 +856,24 @@ static int iuu_uart_off(struct usb_serial_port *port)
850 return status; 856 return status;
851} 857}
852 858
853static int iuu_uart_baud(struct usb_serial_port *port, u32 baud, 859static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
854 u32 *actual, u8 parity) 860 u32 *actual, u8 parity)
855{ 861{
856 int status; 862 int status;
863 u32 baud;
857 u8 *dataout; 864 u8 *dataout;
858 u8 DataCount = 0; 865 u8 DataCount = 0;
859 u8 T1Frekvens = 0; 866 u8 T1Frekvens = 0;
860 u8 T1reload = 0; 867 u8 T1reload = 0;
861 unsigned int T1FrekvensHZ = 0; 868 unsigned int T1FrekvensHZ = 0;
862 869
870 dbg("%s - enter baud_base=%d", __func__, baud_base);
863 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL); 871 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
864 872
865 if (!dataout) 873 if (!dataout)
866 return -ENOMEM; 874 return -ENOMEM;
875 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
876 baud = baud_base;
867 877
868 if (baud < 1200 || baud > 230400) { 878 if (baud < 1200 || baud > 230400) {
869 kfree(dataout); 879 kfree(dataout);
@@ -947,15 +957,20 @@ static void iuu_set_termios(struct tty_struct *tty,
947 struct usb_serial_port *port, struct ktermios *old_termios) 957 struct usb_serial_port *port, struct ktermios *old_termios)
948{ 958{
949 const u32 supported_mask = CMSPAR|PARENB|PARODD; 959 const u32 supported_mask = CMSPAR|PARENB|PARODD;
950 960 struct iuu_private *priv = usb_get_serial_port_data(port);
951 unsigned int cflag = tty->termios->c_cflag; 961 unsigned int cflag = tty->termios->c_cflag;
952 int status; 962 int status;
953 u32 actual; 963 u32 actual;
954 u32 parity; 964 u32 parity;
955 int csize = CS7; 965 int csize = CS7;
956 int baud = 9600; /* Fixed for the moment */ 966 int baud;
957 u32 newval = cflag & supported_mask; 967 u32 newval = cflag & supported_mask;
958 968
969 /* Just use the ospeed. ispeed should be the same. */
970 baud = tty->termios->c_ospeed;
971
972 dbg("%s - enter c_ospeed or baud=%d", __func__, baud);
973
959 /* compute the parity parameter */ 974 /* compute the parity parameter */
960 parity = 0; 975 parity = 0;
961 if (cflag & CMSPAR) { /* Using mark space */ 976 if (cflag & CMSPAR) { /* Using mark space */
@@ -975,15 +990,15 @@ static void iuu_set_termios(struct tty_struct *tty,
975 990
976 /* set it */ 991 /* set it */
977 status = iuu_uart_baud(port, 992 status = iuu_uart_baud(port,
978 (clockmode == 2) ? 16457 : 9600 * boost / 100, 993 baud * priv->boost / 100,
979 &actual, parity); 994 &actual, parity);
980 995
981 /* set the termios value to the real one, so the user now what has 996 /* set the termios value to the real one, so the user now what has
982 * changed. We support few fields so its easies to copy the old hw 997 * changed. We support few fields so its easies to copy the old hw
983 * settings back over and then adjust them 998 * settings back over and then adjust them
984 */ 999 */
985 if (old_termios) 1000 if (old_termios)
986 tty_termios_copy_hw(tty->termios, old_termios); 1001 tty_termios_copy_hw(tty->termios, old_termios);
987 if (status != 0) /* Set failed - return old bits */ 1002 if (status != 0) /* Set failed - return old bits */
988 return; 1003 return;
989 /* Re-encode speed, parity and csize */ 1004 /* Re-encode speed, parity and csize */
@@ -1017,6 +1032,7 @@ static void iuu_close(struct usb_serial_port *port)
1017 1032
1018static void iuu_init_termios(struct tty_struct *tty) 1033static void iuu_init_termios(struct tty_struct *tty)
1019{ 1034{
1035 dbg("%s - enter", __func__);
1020 *(tty->termios) = tty_std_termios; 1036 *(tty->termios) = tty_std_termios;
1021 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600 1037 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600
1022 | TIOCM_CTS | CSTOPB | PARENB; 1038 | TIOCM_CTS | CSTOPB | PARENB;
@@ -1032,10 +1048,16 @@ static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
1032 struct usb_serial *serial = port->serial; 1048 struct usb_serial *serial = port->serial;
1033 u8 *buf; 1049 u8 *buf;
1034 int result; 1050 int result;
1051 int baud;
1035 u32 actual; 1052 u32 actual;
1036 struct iuu_private *priv = usb_get_serial_port_data(port); 1053 struct iuu_private *priv = usb_get_serial_port_data(port);
1037 1054
1038 dbg("%s - port %d", __func__, port->number); 1055 baud = tty->termios->c_ospeed;
1056 tty->termios->c_ispeed = baud;
1057 /* Re-encode speed */
1058 tty_encode_baud_rate(tty, baud, baud);
1059
1060 dbg("%s - port %d, baud %d", __func__, port->number, baud);
1039 usb_clear_halt(serial->dev, port->write_urb->pipe); 1061 usb_clear_halt(serial->dev, port->write_urb->pipe);
1040 usb_clear_halt(serial->dev, port->read_urb->pipe); 1062 usb_clear_halt(serial->dev, port->read_urb->pipe);
1041 1063
@@ -1070,23 +1092,29 @@ static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
1070 iuu_uart_on(port); 1092 iuu_uart_on(port);
1071 if (boost < 100) 1093 if (boost < 100)
1072 boost = 100; 1094 boost = 100;
1095 priv->boost = boost;
1096 priv->baud = baud;
1073 switch (clockmode) { 1097 switch (clockmode) {
1074 case 2: /* 3.680 Mhz */ 1098 case 2: /* 3.680 Mhz */
1099 priv->clk = IUU_CLK_3680000;
1075 iuu_clk(port, IUU_CLK_3680000 * boost / 100); 1100 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1076 result = 1101 result =
1077 iuu_uart_baud(port, 9600 * boost / 100, &actual, 1102 iuu_uart_baud(port, baud * boost / 100, &actual,
1078 IUU_PARITY_EVEN); 1103 IUU_PARITY_EVEN);
1079 break; 1104 break;
1080 case 3: /* 6.00 Mhz */ 1105 case 3: /* 6.00 Mhz */
1081 iuu_clk(port, IUU_CLK_6000000 * boost / 100); 1106 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1107 priv->clk = IUU_CLK_6000000;
1108 /* Ratio of 6000000 to 3500000 for baud 9600 */
1082 result = 1109 result =
1083 iuu_uart_baud(port, 16457 * boost / 100, &actual, 1110 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1084 IUU_PARITY_EVEN); 1111 IUU_PARITY_EVEN);
1085 break; 1112 break;
1086 default: /* 3.579 Mhz */ 1113 default: /* 3.579 Mhz */
1087 iuu_clk(port, IUU_CLK_3579000 * boost / 100); 1114 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1115 priv->clk = IUU_CLK_3579000;
1088 result = 1116 result =
1089 iuu_uart_baud(port, 9600 * boost / 100, &actual, 1117 iuu_uart_baud(port, baud * boost / 100, &actual,
1090 IUU_PARITY_EVEN); 1118 IUU_PARITY_EVEN);
1091 } 1119 }
1092 1120
diff --git a/drivers/usb/serial/option.c b/drivers/usb/serial/option.c
index 5cd30e4345c..9fc6ea2c681 100644
--- a/drivers/usb/serial/option.c
+++ b/drivers/usb/serial/option.c
@@ -145,7 +145,10 @@ static void option_instat_callback(struct urb *urb);
145#define HUAWEI_PRODUCT_E143D 0x143D 145#define HUAWEI_PRODUCT_E143D 0x143D
146#define HUAWEI_PRODUCT_E143E 0x143E 146#define HUAWEI_PRODUCT_E143E 0x143E
147#define HUAWEI_PRODUCT_E143F 0x143F 147#define HUAWEI_PRODUCT_E143F 0x143F
148#define HUAWEI_PRODUCT_K4505 0x1464
149#define HUAWEI_PRODUCT_K3765 0x1465
148#define HUAWEI_PRODUCT_E14AC 0x14AC 150#define HUAWEI_PRODUCT_E14AC 0x14AC
151#define HUAWEI_PRODUCT_ETS1220 0x1803
149 152
150#define QUANTA_VENDOR_ID 0x0408 153#define QUANTA_VENDOR_ID 0x0408
151#define QUANTA_PRODUCT_Q101 0xEA02 154#define QUANTA_PRODUCT_Q101 0xEA02
@@ -264,9 +267,6 @@ static void option_instat_callback(struct urb *urb);
264#define BANDRICH_PRODUCT_1011 0x1011 267#define BANDRICH_PRODUCT_1011 0x1011
265#define BANDRICH_PRODUCT_1012 0x1012 268#define BANDRICH_PRODUCT_1012 0x1012
266 269
267#define AMOI_VENDOR_ID 0x1614
268#define AMOI_PRODUCT_9508 0x0800
269
270#define QUALCOMM_VENDOR_ID 0x05C6 270#define QUALCOMM_VENDOR_ID 0x05C6
271 271
272#define CMOTECH_VENDOR_ID 0x16d8 272#define CMOTECH_VENDOR_ID 0x16d8
@@ -482,8 +482,10 @@ static const struct usb_device_id option_ids[] = {
482 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143D, 0xff, 0xff, 0xff) }, 482 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143D, 0xff, 0xff, 0xff) },
483 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143E, 0xff, 0xff, 0xff) }, 483 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143E, 0xff, 0xff, 0xff) },
484 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143F, 0xff, 0xff, 0xff) }, 484 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143F, 0xff, 0xff, 0xff) },
485 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K4505, 0xff, 0xff, 0xff) },
486 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3765, 0xff, 0xff, 0xff) },
487 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_ETS1220, 0xff, 0xff, 0xff) },
485 { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E14AC) }, 488 { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E14AC) },
486 { USB_DEVICE(AMOI_VENDOR_ID, AMOI_PRODUCT_9508) },
487 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_V640) }, /* Novatel Merlin V640/XV620 */ 489 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_V640) }, /* Novatel Merlin V640/XV620 */
488 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_V620) }, /* Novatel Merlin V620/S620 */ 490 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_V620) }, /* Novatel Merlin V620/S620 */
489 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_V740) }, /* Novatel Merlin EX720/V740/X720 */ 491 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_V740) }, /* Novatel Merlin EX720/V740/X720 */
@@ -1017,6 +1019,13 @@ static int option_probe(struct usb_serial *serial,
1017 serial->interface->cur_altsetting->desc.bInterfaceClass != 0xff) 1019 serial->interface->cur_altsetting->desc.bInterfaceClass != 0xff)
1018 return -ENODEV; 1020 return -ENODEV;
1019 1021
1022 /* Don't bind network interfaces on Huawei K3765 & K4505 */
1023 if (serial->dev->descriptor.idVendor == HUAWEI_VENDOR_ID &&
1024 (serial->dev->descriptor.idProduct == HUAWEI_PRODUCT_K3765 ||
1025 serial->dev->descriptor.idProduct == HUAWEI_PRODUCT_K4505) &&
1026 serial->interface->cur_altsetting->desc.bInterfaceNumber == 1)
1027 return -ENODEV;
1028
1020 data = serial->private = kzalloc(sizeof(struct usb_wwan_intf_private), GFP_KERNEL); 1029 data = serial->private = kzalloc(sizeof(struct usb_wwan_intf_private), GFP_KERNEL);
1021 1030
1022 if (!data) 1031 if (!data)
diff --git a/drivers/usb/serial/ssu100.c b/drivers/usb/serial/ssu100.c
new file mode 100644
index 00000000000..6e82d4f54bc
--- /dev/null
+++ b/drivers/usb/serial/ssu100.c
@@ -0,0 +1,698 @@
1/*
2 * usb-serial driver for Quatech SSU-100
3 *
4 * based on ftdi_sio.c and the original serqt_usb.c from Quatech
5 *
6 */
7
8#include <linux/errno.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/tty.h>
12#include <linux/tty_driver.h>
13#include <linux/tty_flip.h>
14#include <linux/module.h>
15#include <linux/serial.h>
16#include <linux/usb.h>
17#include <linux/usb/serial.h>
18#include <linux/uaccess.h>
19
20#define QT_OPEN_CLOSE_CHANNEL 0xca
21#define QT_SET_GET_DEVICE 0xc2
22#define QT_SET_GET_REGISTER 0xc0
23#define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
24#define QT_SET_ATF 0xcd
25#define QT_GET_SET_UART 0xc1
26#define QT_TRANSFER_IN 0xc0
27#define QT_HW_FLOW_CONTROL_MASK 0xc5
28#define QT_SW_FLOW_CONTROL_MASK 0xc6
29
30#define MODEM_CTL_REGISTER 0x04
31#define MODEM_STATUS_REGISTER 0x06
32
33
34#define SERIAL_LSR_OE 0x02
35#define SERIAL_LSR_PE 0x04
36#define SERIAL_LSR_FE 0x08
37#define SERIAL_LSR_BI 0x10
38
39#define SERIAL_LSR_TEMT 0x40
40
41#define SERIAL_MCR_DTR 0x01
42#define SERIAL_MCR_RTS 0x02
43#define SERIAL_MCR_LOOP 0x10
44
45#define SERIAL_MSR_CTS 0x10
46#define SERIAL_MSR_CD 0x80
47#define SERIAL_MSR_RI 0x40
48#define SERIAL_MSR_DSR 0x20
49#define SERIAL_MSR_MASK 0xf0
50
51#define SERIAL_CRTSCTS ((SERIAL_MCR_RTS << 8) | SERIAL_MSR_CTS)
52
53#define SERIAL_8_DATA 0x03
54#define SERIAL_7_DATA 0x02
55#define SERIAL_6_DATA 0x01
56#define SERIAL_5_DATA 0x00
57
58#define SERIAL_ODD_PARITY 0X08
59#define SERIAL_EVEN_PARITY 0X18
60
61#define MAX_BAUD_RATE 460800
62
63#define ATC_DISABLED 0x00
64#define DUPMODE_BITS 0xc0
65#define RR_BITS 0x03
66#define LOOPMODE_BITS 0x41
67#define RS232_MODE 0x00
68#define RTSCTS_TO_CONNECTOR 0x40
69#define CLKS_X4 0x02
70#define FULLPWRBIT 0x00000080
71#define NEXT_BOARD_POWER_BIT 0x00000004
72
73static int debug = 1;
74
75/* Version Information */
76#define DRIVER_VERSION "v0.1"
77#define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
78
79#define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
80#define QUATECH_SSU100 0xC020 /* SSU100 */
81
82static const struct usb_device_id id_table[] = {
83 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
84 {} /* Terminating entry */
85};
86
87MODULE_DEVICE_TABLE(usb, id_table);
88
89
90static struct usb_driver ssu100_driver = {
91 .name = "ssu100",
92 .probe = usb_serial_probe,
93 .disconnect = usb_serial_disconnect,
94 .id_table = id_table,
95 .suspend = usb_serial_suspend,
96 .resume = usb_serial_resume,
97 .no_dynamic_id = 1,
98 .supports_autosuspend = 1,
99};
100
101struct ssu100_port_private {
102 u8 shadowLSR;
103 u8 shadowMSR;
104 wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
105 unsigned short max_packet_size;
106};
107
108static void ssu100_release(struct usb_serial *serial)
109{
110 struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port);
111
112 dbg("%s", __func__);
113 kfree(priv);
114}
115
116static inline int ssu100_control_msg(struct usb_device *dev,
117 u8 request, u16 data, u16 index)
118{
119 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
120 request, 0x40, data, index,
121 NULL, 0, 300);
122}
123
124static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
125{
126 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
127
128 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
129}
130
131
132static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
133{
134 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
135 QT_SET_GET_DEVICE, 0xc0, 0, 0,
136 data, 3, 300);
137}
138
139static inline int ssu100_getregister(struct usb_device *dev,
140 unsigned short uart,
141 unsigned short reg,
142 u8 *data)
143{
144 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
145 QT_SET_GET_REGISTER, 0xc0, reg,
146 uart, data, sizeof(*data), 300);
147
148}
149
150
151static inline int ssu100_setregister(struct usb_device *dev,
152 unsigned short uart,
153 u16 data)
154{
155 u16 value = (data << 8) | MODEM_CTL_REGISTER;
156
157 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
158 QT_SET_GET_REGISTER, 0x40, value, uart,
159 NULL, 0, 300);
160
161}
162
163#define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
164#define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
165
166/* these do not deal with device that have more than 1 port */
167static inline int update_mctrl(struct usb_device *dev, unsigned int set,
168 unsigned int clear)
169{
170 unsigned urb_value;
171 int result;
172
173 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
174 dbg("%s - DTR|RTS not being set|cleared", __func__);
175 return 0; /* no change */
176 }
177
178 clear &= ~set; /* 'set' takes precedence over 'clear' */
179 urb_value = 0;
180 if (set & TIOCM_DTR)
181 urb_value |= SERIAL_MCR_DTR;
182 if (set & TIOCM_RTS)
183 urb_value |= SERIAL_MCR_RTS;
184
185 result = ssu100_setregister(dev, 0, urb_value);
186 if (result < 0)
187 dbg("%s Error from MODEM_CTRL urb", __func__);
188
189 return result;
190}
191
192static int ssu100_initdevice(struct usb_device *dev)
193{
194 u8 *data;
195 int result = 0;
196
197 dbg("%s", __func__);
198
199 data = kzalloc(3, GFP_KERNEL);
200 if (!data)
201 return -ENOMEM;
202
203 result = ssu100_getdevice(dev, data);
204 if (result < 0) {
205 dbg("%s - get_device failed %i", __func__, result);
206 goto out;
207 }
208
209 data[1] &= ~FULLPWRBIT;
210
211 result = ssu100_setdevice(dev, data);
212 if (result < 0) {
213 dbg("%s - setdevice failed %i", __func__, result);
214 goto out;
215 }
216
217 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
218 if (result < 0) {
219 dbg("%s - set prebuffer level failed %i", __func__, result);
220 goto out;
221 }
222
223 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
224 if (result < 0) {
225 dbg("%s - set ATFprebuffer level failed %i", __func__, result);
226 goto out;
227 }
228
229 result = ssu100_getdevice(dev, data);
230 if (result < 0) {
231 dbg("%s - get_device failed %i", __func__, result);
232 goto out;
233 }
234
235 data[0] &= ~(RR_BITS | DUPMODE_BITS);
236 data[0] |= CLKS_X4;
237 data[1] &= ~(LOOPMODE_BITS);
238 data[1] |= RS232_MODE;
239
240 result = ssu100_setdevice(dev, data);
241 if (result < 0) {
242 dbg("%s - setdevice failed %i", __func__, result);
243 goto out;
244 }
245
246out: kfree(data);
247 return result;
248
249}
250
251
252static void ssu100_set_termios(struct tty_struct *tty,
253 struct usb_serial_port *port,
254 struct ktermios *old_termios)
255{
256 struct usb_device *dev = port->serial->dev;
257 struct ktermios *termios = tty->termios;
258 u16 baud, divisor, remainder;
259 unsigned int cflag = termios->c_cflag;
260 u16 urb_value = 0; /* will hold the new flags */
261 int result;
262
263 dbg("%s", __func__);
264
265 if (cflag & PARENB) {
266 if (cflag & PARODD)
267 urb_value |= SERIAL_ODD_PARITY;
268 else
269 urb_value |= SERIAL_EVEN_PARITY;
270 }
271
272 switch (cflag & CSIZE) {
273 case CS5:
274 urb_value |= SERIAL_5_DATA;
275 break;
276 case CS6:
277 urb_value |= SERIAL_6_DATA;
278 break;
279 case CS7:
280 urb_value |= SERIAL_7_DATA;
281 break;
282 default:
283 case CS8:
284 urb_value |= SERIAL_8_DATA;
285 break;
286 }
287
288 baud = tty_get_baud_rate(tty);
289 if (!baud)
290 baud = 9600;
291
292 dbg("%s - got baud = %d\n", __func__, baud);
293
294
295 divisor = MAX_BAUD_RATE / baud;
296 remainder = MAX_BAUD_RATE % baud;
297 if (((remainder * 2) >= baud) && (baud != 110))
298 divisor++;
299
300 urb_value = urb_value << 8;
301
302 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
303 if (result < 0)
304 dbg("%s - set uart failed", __func__);
305
306 if (cflag & CRTSCTS)
307 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
308 SERIAL_CRTSCTS, 0);
309 else
310 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
311 0, 0);
312 if (result < 0)
313 dbg("%s - set HW flow control failed", __func__);
314
315 if (I_IXOFF(tty) || I_IXON(tty)) {
316 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
317
318 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
319 x, 0);
320 } else
321 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
322 0, 0);
323
324 if (result < 0)
325 dbg("%s - set SW flow control failed", __func__);
326
327}
328
329
330static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
331{
332 struct usb_device *dev = port->serial->dev;
333 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
334 u8 *data;
335 int result;
336
337 dbg("%s - port %d", __func__, port->number);
338
339 data = kzalloc(2, GFP_KERNEL);
340 if (!data)
341 return -ENOMEM;
342
343 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
344 QT_OPEN_CLOSE_CHANNEL,
345 QT_TRANSFER_IN, 0x01,
346 0, data, 2, 300);
347 if (result < 0) {
348 dbg("%s - open failed %i", __func__, result);
349 kfree(data);
350 return result;
351 }
352
353 priv->shadowLSR = data[0] & (SERIAL_LSR_OE | SERIAL_LSR_PE |
354 SERIAL_LSR_FE | SERIAL_LSR_BI);
355
356 priv->shadowMSR = data[1] & (SERIAL_MSR_CTS | SERIAL_MSR_DSR |
357 SERIAL_MSR_RI | SERIAL_MSR_CD);
358
359 kfree(data);
360
361/* set to 9600 */
362 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
363 if (result < 0)
364 dbg("%s - set uart failed", __func__);
365
366 if (tty)
367 ssu100_set_termios(tty, port, tty->termios);
368
369 return usb_serial_generic_open(tty, port);
370}
371
372static void ssu100_close(struct usb_serial_port *port)
373{
374 dbg("%s", __func__);
375 usb_serial_generic_close(port);
376}
377
378static int get_serial_info(struct usb_serial_port *port,
379 struct serial_struct __user *retinfo)
380{
381 struct serial_struct tmp;
382
383 if (!retinfo)
384 return -EFAULT;
385
386 memset(&tmp, 0, sizeof(tmp));
387 tmp.line = port->serial->minor;
388 tmp.port = 0;
389 tmp.irq = 0;
390 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
391 tmp.xmit_fifo_size = port->bulk_out_size;
392 tmp.baud_base = 9600;
393 tmp.close_delay = 5*HZ;
394 tmp.closing_wait = 30*HZ;
395
396 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
397 return -EFAULT;
398 return 0;
399}
400
401static int ssu100_ioctl(struct tty_struct *tty, struct file *file,
402 unsigned int cmd, unsigned long arg)
403{
404 struct usb_serial_port *port = tty->driver_data;
405 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
406
407 dbg("%s cmd 0x%04x", __func__, cmd);
408
409 switch (cmd) {
410 case TIOCGSERIAL:
411 return get_serial_info(port,
412 (struct serial_struct __user *) arg);
413
414 case TIOCMIWAIT:
415 while (priv != NULL) {
416 u8 prevMSR = priv->shadowMSR & SERIAL_MSR_MASK;
417 interruptible_sleep_on(&priv->delta_msr_wait);
418 /* see if a signal did it */
419 if (signal_pending(current))
420 return -ERESTARTSYS;
421 else {
422 u8 diff = (priv->shadowMSR & SERIAL_MSR_MASK) ^ prevMSR;
423 if (!diff)
424 return -EIO; /* no change => error */
425
426 /* Return 0 if caller wanted to know about
427 these bits */
428
429 if (((arg & TIOCM_RNG) && (diff & SERIAL_MSR_RI)) ||
430 ((arg & TIOCM_DSR) && (diff & SERIAL_MSR_DSR)) ||
431 ((arg & TIOCM_CD) && (diff & SERIAL_MSR_CD)) ||
432 ((arg & TIOCM_CTS) && (diff & SERIAL_MSR_CTS)))
433 return 0;
434 }
435 }
436 return 0;
437
438 default:
439 break;
440 }
441
442 dbg("%s arg not supported", __func__);
443
444 return -ENOIOCTLCMD;
445}
446
447static void ssu100_set_max_packet_size(struct usb_serial_port *port)
448{
449 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
450 struct usb_serial *serial = port->serial;
451 struct usb_device *udev = serial->dev;
452
453 struct usb_interface *interface = serial->interface;
454 struct usb_endpoint_descriptor *ep_desc = &interface->cur_altsetting->endpoint[1].desc;
455
456 unsigned num_endpoints;
457 int i;
458
459 num_endpoints = interface->cur_altsetting->desc.bNumEndpoints;
460 dev_info(&udev->dev, "Number of endpoints %d\n", num_endpoints);
461
462 for (i = 0; i < num_endpoints; i++) {
463 dev_info(&udev->dev, "Endpoint %d MaxPacketSize %d\n", i+1,
464 interface->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
465 ep_desc = &interface->cur_altsetting->endpoint[i].desc;
466 }
467
468 /* set max packet size based on descriptor */
469 priv->max_packet_size = ep_desc->wMaxPacketSize;
470
471 dev_info(&udev->dev, "Setting MaxPacketSize %d\n", priv->max_packet_size);
472}
473
474static int ssu100_attach(struct usb_serial *serial)
475{
476 struct ssu100_port_private *priv;
477 struct usb_serial_port *port = *serial->port;
478
479 dbg("%s", __func__);
480
481 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
482 if (!priv) {
483 dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__,
484 sizeof(*priv));
485 return -ENOMEM;
486 }
487
488 init_waitqueue_head(&priv->delta_msr_wait);
489 usb_set_serial_port_data(port, priv);
490
491 ssu100_set_max_packet_size(port);
492
493 return ssu100_initdevice(serial->dev);
494}
495
496static int ssu100_tiocmget(struct tty_struct *tty, struct file *file)
497{
498 struct usb_serial_port *port = tty->driver_data;
499 struct usb_device *dev = port->serial->dev;
500 u8 *d;
501 int r;
502
503 dbg("%s\n", __func__);
504
505 d = kzalloc(2, GFP_KERNEL);
506 if (!d)
507 return -ENOMEM;
508
509 r = ssu100_getregister(dev, 0, MODEM_CTL_REGISTER, d);
510 if (r < 0)
511 goto mget_out;
512
513 r = ssu100_getregister(dev, 0, MODEM_STATUS_REGISTER, d+1);
514 if (r < 0)
515 goto mget_out;
516
517 r = (d[0] & SERIAL_MCR_DTR ? TIOCM_DTR : 0) |
518 (d[0] & SERIAL_MCR_RTS ? TIOCM_RTS : 0) |
519 (d[1] & SERIAL_MSR_CTS ? TIOCM_CTS : 0) |
520 (d[1] & SERIAL_MSR_CD ? TIOCM_CAR : 0) |
521 (d[1] & SERIAL_MSR_RI ? TIOCM_RI : 0) |
522 (d[1] & SERIAL_MSR_DSR ? TIOCM_DSR : 0);
523
524mget_out:
525 kfree(d);
526 return r;
527}
528
529static int ssu100_tiocmset(struct tty_struct *tty, struct file *file,
530 unsigned int set, unsigned int clear)
531{
532 struct usb_serial_port *port = tty->driver_data;
533 struct usb_device *dev = port->serial->dev;
534
535 dbg("%s\n", __func__);
536 return update_mctrl(dev, set, clear);
537}
538
539static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
540{
541 struct usb_device *dev = port->serial->dev;
542
543 dbg("%s\n", __func__);
544
545 mutex_lock(&port->serial->disc_mutex);
546 if (!port->serial->disconnected) {
547 /* Disable flow control */
548 if (!on &&
549 ssu100_setregister(dev, 0, 0) < 0)
550 dev_err(&port->dev, "error from flowcontrol urb\n");
551 /* drop RTS and DTR */
552 if (on)
553 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
554 else
555 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
556 }
557 mutex_unlock(&port->serial->disc_mutex);
558}
559
560static int ssu100_process_packet(struct tty_struct *tty,
561 struct usb_serial_port *port,
562 struct ssu100_port_private *priv,
563 char *packet, int len)
564{
565 int i;
566 char flag;
567 char *ch;
568
569 dbg("%s - port %d", __func__, port->number);
570
571 if (len < 4) {
572 dbg("%s - malformed packet", __func__);
573 return 0;
574 }
575
576 if ((packet[0] == 0x1b) && (packet[1] == 0x1b) &&
577 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
578 if (packet[2] == 0x00)
579 priv->shadowLSR = packet[3] & (SERIAL_LSR_OE |
580 SERIAL_LSR_PE |
581 SERIAL_LSR_FE |
582 SERIAL_LSR_BI);
583
584 if (packet[2] == 0x01) {
585 priv->shadowMSR = packet[3];
586 wake_up_interruptible(&priv->delta_msr_wait);
587 }
588
589 len -= 4;
590 ch = packet + 4;
591 } else
592 ch = packet;
593
594 if (!len)
595 return 0; /* status only */
596
597 if (port->port.console && port->sysrq) {
598 for (i = 0; i < len; i++, ch++) {
599 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
600 tty_insert_flip_char(tty, *ch, flag);
601 }
602 } else
603 tty_insert_flip_string_fixed_flag(tty, ch, flag, len);
604
605 return len;
606}
607
608static void ssu100_process_read_urb(struct urb *urb)
609{
610 struct usb_serial_port *port = urb->context;
611 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
612 char *data = (char *)urb->transfer_buffer;
613 struct tty_struct *tty;
614 int count = 0;
615 int i;
616 int len;
617
618 dbg("%s", __func__);
619
620 tty = tty_port_tty_get(&port->port);
621 if (!tty)
622 return;
623
624 for (i = 0; i < urb->actual_length; i += priv->max_packet_size) {
625 len = min_t(int, urb->actual_length - i, priv->max_packet_size);
626 count += ssu100_process_packet(tty, port, priv, &data[i], len);
627 }
628
629 if (count)
630 tty_flip_buffer_push(tty);
631 tty_kref_put(tty);
632}
633
634
635static struct usb_serial_driver ssu100_device = {
636 .driver = {
637 .owner = THIS_MODULE,
638 .name = "ssu100",
639 },
640 .description = DRIVER_DESC,
641 .id_table = id_table,
642 .usb_driver = &ssu100_driver,
643 .num_ports = 1,
644 .bulk_in_size = 256,
645 .bulk_out_size = 256,
646 .open = ssu100_open,
647 .close = ssu100_close,
648 .attach = ssu100_attach,
649 .release = ssu100_release,
650 .dtr_rts = ssu100_dtr_rts,
651 .process_read_urb = ssu100_process_read_urb,
652 .tiocmget = ssu100_tiocmget,
653 .tiocmset = ssu100_tiocmset,
654 .ioctl = ssu100_ioctl,
655 .set_termios = ssu100_set_termios,
656};
657
658static int __init ssu100_init(void)
659{
660 int retval;
661
662 dbg("%s", __func__);
663
664 /* register with usb-serial */
665 retval = usb_serial_register(&ssu100_device);
666
667 if (retval)
668 goto failed_usb_sio_register;
669
670 retval = usb_register(&ssu100_driver);
671 if (retval)
672 goto failed_usb_register;
673
674 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
675 DRIVER_DESC "\n");
676
677 return 0;
678
679failed_usb_register:
680 usb_serial_deregister(&ssu100_device);
681failed_usb_sio_register:
682 return retval;
683}
684
685static void __exit ssu100_exit(void)
686{
687 usb_deregister(&ssu100_driver);
688 usb_serial_deregister(&ssu100_device);
689}
690
691module_init(ssu100_init);
692module_exit(ssu100_exit);
693
694MODULE_DESCRIPTION(DRIVER_DESC);
695MODULE_LICENSE("GPL");
696
697module_param(debug, bool, S_IRUGO | S_IWUSR);
698MODULE_PARM_DESC(debug, "Debug enabled or not");
diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index 941c2d409f8..2a982e62963 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -653,6 +653,7 @@ exit:
653 return id; 653 return id;
654} 654}
655 655
656/* Caller must hold table_lock */
656static struct usb_serial_driver *search_serial_device( 657static struct usb_serial_driver *search_serial_device(
657 struct usb_interface *iface) 658 struct usb_interface *iface)
658{ 659{
@@ -718,17 +719,23 @@ int usb_serial_probe(struct usb_interface *interface,
718 int num_ports = 0; 719 int num_ports = 0;
719 int max_endpoints; 720 int max_endpoints;
720 721
721 lock_kernel(); /* guard against unloading a serial driver module */ 722 mutex_lock(&table_lock);
722 type = search_serial_device(interface); 723 type = search_serial_device(interface);
723 if (!type) { 724 if (!type) {
724 unlock_kernel(); 725 mutex_unlock(&table_lock);
725 dbg("none matched"); 726 dbg("none matched");
726 return -ENODEV; 727 return -ENODEV;
727 } 728 }
728 729
730 if (!try_module_get(type->driver.owner)) {
731 mutex_unlock(&table_lock);
732 dev_err(&interface->dev, "module get failed, exiting\n");
733 return -EIO;
734 }
735 mutex_unlock(&table_lock);
736
729 serial = create_serial(dev, interface, type); 737 serial = create_serial(dev, interface, type);
730 if (!serial) { 738 if (!serial) {
731 unlock_kernel();
732 dev_err(&interface->dev, "%s - out of memory\n", __func__); 739 dev_err(&interface->dev, "%s - out of memory\n", __func__);
733 return -ENOMEM; 740 return -ENOMEM;
734 } 741 }
@@ -737,20 +744,11 @@ int usb_serial_probe(struct usb_interface *interface,
737 if (type->probe) { 744 if (type->probe) {
738 const struct usb_device_id *id; 745 const struct usb_device_id *id;
739 746
740 if (!try_module_get(type->driver.owner)) {
741 unlock_kernel();
742 dev_err(&interface->dev,
743 "module get failed, exiting\n");
744 kfree(serial);
745 return -EIO;
746 }
747
748 id = get_iface_id(type, interface); 747 id = get_iface_id(type, interface);
749 retval = type->probe(serial, id); 748 retval = type->probe(serial, id);
750 module_put(type->driver.owner); 749 module_put(type->driver.owner);
751 750
752 if (retval) { 751 if (retval) {
753 unlock_kernel();
754 dbg("sub driver rejected device"); 752 dbg("sub driver rejected device");
755 kfree(serial); 753 kfree(serial);
756 return retval; 754 return retval;
@@ -822,7 +820,6 @@ int usb_serial_probe(struct usb_interface *interface,
822 * properly during a later invocation of usb_serial_probe 820 * properly during a later invocation of usb_serial_probe
823 */ 821 */
824 if (num_bulk_in == 0 || num_bulk_out == 0) { 822 if (num_bulk_in == 0 || num_bulk_out == 0) {
825 unlock_kernel();
826 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n"); 823 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
827 kfree(serial); 824 kfree(serial);
828 return -ENODEV; 825 return -ENODEV;
@@ -835,7 +832,6 @@ int usb_serial_probe(struct usb_interface *interface,
835 if (type == &usb_serial_generic_device) { 832 if (type == &usb_serial_generic_device) {
836 num_ports = num_bulk_out; 833 num_ports = num_bulk_out;
837 if (num_ports == 0) { 834 if (num_ports == 0) {
838 unlock_kernel();
839 dev_err(&interface->dev, 835 dev_err(&interface->dev,
840 "Generic device with no bulk out, not allowed.\n"); 836 "Generic device with no bulk out, not allowed.\n");
841 kfree(serial); 837 kfree(serial);
@@ -847,7 +843,6 @@ int usb_serial_probe(struct usb_interface *interface,
847 /* if this device type has a calc_num_ports function, call it */ 843 /* if this device type has a calc_num_ports function, call it */
848 if (type->calc_num_ports) { 844 if (type->calc_num_ports) {
849 if (!try_module_get(type->driver.owner)) { 845 if (!try_module_get(type->driver.owner)) {
850 unlock_kernel();
851 dev_err(&interface->dev, 846 dev_err(&interface->dev,
852 "module get failed, exiting\n"); 847 "module get failed, exiting\n");
853 kfree(serial); 848 kfree(serial);
@@ -878,7 +873,6 @@ int usb_serial_probe(struct usb_interface *interface,
878 max_endpoints = max(max_endpoints, num_interrupt_out); 873 max_endpoints = max(max_endpoints, num_interrupt_out);
879 max_endpoints = max(max_endpoints, (int)serial->num_ports); 874 max_endpoints = max(max_endpoints, (int)serial->num_ports);
880 serial->num_port_pointers = max_endpoints; 875 serial->num_port_pointers = max_endpoints;
881 unlock_kernel();
882 876
883 dbg("%s - setting up %d port structures for this device", 877 dbg("%s - setting up %d port structures for this device",
884 __func__, max_endpoints); 878 __func__, max_endpoints);
@@ -1077,6 +1071,8 @@ int usb_serial_probe(struct usb_interface *interface,
1077 dev_set_name(&port->dev, "ttyUSB%d", port->number); 1071 dev_set_name(&port->dev, "ttyUSB%d", port->number);
1078 dbg ("%s - registering %s", __func__, dev_name(&port->dev)); 1072 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
1079 port->dev_state = PORT_REGISTERING; 1073 port->dev_state = PORT_REGISTERING;
1074 device_enable_async_suspend(&port->dev);
1075
1080 retval = device_add(&port->dev); 1076 retval = device_add(&port->dev);
1081 if (retval) { 1077 if (retval) {
1082 dev_err(&port->dev, "Error registering port device, " 1078 dev_err(&port->dev, "Error registering port device, "
@@ -1349,6 +1345,7 @@ int usb_serial_register(struct usb_serial_driver *driver)
1349 driver->description = driver->driver.name; 1345 driver->description = driver->driver.name;
1350 1346
1351 /* Add this device to our list of devices */ 1347 /* Add this device to our list of devices */
1348 mutex_lock(&table_lock);
1352 list_add(&driver->driver_list, &usb_serial_driver_list); 1349 list_add(&driver->driver_list, &usb_serial_driver_list);
1353 1350
1354 retval = usb_serial_bus_register(driver); 1351 retval = usb_serial_bus_register(driver);
@@ -1360,6 +1357,7 @@ int usb_serial_register(struct usb_serial_driver *driver)
1360 printk(KERN_INFO "USB Serial support registered for %s\n", 1357 printk(KERN_INFO "USB Serial support registered for %s\n",
1361 driver->description); 1358 driver->description);
1362 1359
1360 mutex_unlock(&table_lock);
1363 return retval; 1361 return retval;
1364} 1362}
1365EXPORT_SYMBOL_GPL(usb_serial_register); 1363EXPORT_SYMBOL_GPL(usb_serial_register);
@@ -1370,8 +1368,10 @@ void usb_serial_deregister(struct usb_serial_driver *device)
1370 /* must be called with BKL held */ 1368 /* must be called with BKL held */
1371 printk(KERN_INFO "USB Serial deregistering driver %s\n", 1369 printk(KERN_INFO "USB Serial deregistering driver %s\n",
1372 device->description); 1370 device->description);
1371 mutex_lock(&table_lock);
1373 list_del(&device->driver_list); 1372 list_del(&device->driver_list);
1374 usb_serial_bus_deregister(device); 1373 usb_serial_bus_deregister(device);
1374 mutex_unlock(&table_lock);
1375} 1375}
1376EXPORT_SYMBOL_GPL(usb_serial_deregister); 1376EXPORT_SYMBOL_GPL(usb_serial_deregister);
1377 1377
diff --git a/drivers/usb/storage/freecom.c b/drivers/usb/storage/freecom.c
index 54cc94277ac..6542ca40d50 100644
--- a/drivers/usb/storage/freecom.c
+++ b/drivers/usb/storage/freecom.c
@@ -269,7 +269,7 @@ static int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
269 /* The firmware will time-out commands after 20 seconds. Some commands 269 /* The firmware will time-out commands after 20 seconds. Some commands
270 * can legitimately take longer than this, so we use a different 270 * can legitimately take longer than this, so we use a different
271 * command that only waits for the interrupt and then sends status, 271 * command that only waits for the interrupt and then sends status,
272 * without having to send a new ATAPI command to the device. 272 * without having to send a new ATAPI command to the device.
273 * 273 *
274 * NOTE: There is some indication that a data transfer after a timeout 274 * NOTE: There is some indication that a data transfer after a timeout
275 * may not work, but that is a condition that should never happen. 275 * may not work, but that is a condition that should never happen.
@@ -324,14 +324,14 @@ static int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
324 324
325 /* Find the length we desire to read. */ 325 /* Find the length we desire to read. */
326 switch (srb->cmnd[0]) { 326 switch (srb->cmnd[0]) {
327 case INQUIRY: 327 case INQUIRY:
328 case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */ 328 case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
329 case MODE_SENSE: 329 case MODE_SENSE:
330 case MODE_SENSE_10: 330 case MODE_SENSE_10:
331 length = le16_to_cpu(fst->Count); 331 length = le16_to_cpu(fst->Count);
332 break; 332 break;
333 default: 333 default:
334 length = scsi_bufflen(srb); 334 length = scsi_bufflen(srb);
335 } 335 }
336 336
337 /* verify that this amount is legal */ 337 /* verify that this amount is legal */
@@ -414,7 +414,7 @@ static int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
414 /* should never hit here -- filtered in usb.c */ 414 /* should never hit here -- filtered in usb.c */
415 US_DEBUGP ("freecom unimplemented direction: %d\n", 415 US_DEBUGP ("freecom unimplemented direction: %d\n",
416 us->srb->sc_data_direction); 416 us->srb->sc_data_direction);
417 // Return fail, SCSI seems to handle this better. 417 /* Return fail, SCSI seems to handle this better. */
418 return USB_STOR_TRANSPORT_FAILED; 418 return USB_STOR_TRANSPORT_FAILED;
419 break; 419 break;
420 } 420 }
@@ -494,8 +494,7 @@ static void pdump (void *ibuffer, int length)
494 offset = 0; 494 offset = 0;
495 } 495 }
496 offset += sprintf (line+offset, "%08x:", i); 496 offset += sprintf (line+offset, "%08x:", i);
497 } 497 } else if ((i & 7) == 0) {
498 else if ((i & 7) == 0) {
499 offset += sprintf (line+offset, " -"); 498 offset += sprintf (line+offset, " -");
500 } 499 }
501 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff); 500 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
diff --git a/drivers/usb/storage/isd200.c b/drivers/usb/storage/isd200.c
index e9cbc1467f7..6b9982cd542 100644
--- a/drivers/usb/storage/isd200.c
+++ b/drivers/usb/storage/isd200.c
@@ -1456,8 +1456,7 @@ static int isd200_init_info(struct us_data *us)
1456 int retStatus = ISD200_GOOD; 1456 int retStatus = ISD200_GOOD;
1457 struct isd200_info *info; 1457 struct isd200_info *info;
1458 1458
1459 info = (struct isd200_info *) 1459 info = kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1460 kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1461 if (!info) 1460 if (!info)
1462 retStatus = ISD200_ERROR; 1461 retStatus = ISD200_ERROR;
1463 else { 1462 else {
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index a7d0bf9d92a..90bb0175a15 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -336,6 +336,7 @@ static int usb_stor_control_thread(void * __us)
336 else { 336 else {
337 US_DEBUG(usb_stor_show_command(us->srb)); 337 US_DEBUG(usb_stor_show_command(us->srb));
338 us->proto_handler(us->srb, us); 338 us->proto_handler(us->srb, us);
339 usb_mark_last_busy(us->pusb_dev);
339 } 340 }
340 341
341 /* lock access to the state */ 342 /* lock access to the state */
@@ -845,6 +846,7 @@ static int usb_stor_scan_thread(void * __us)
845 /* Should we unbind if no devices were detected? */ 846 /* Should we unbind if no devices were detected? */
846 } 847 }
847 848
849 usb_autopm_put_interface(us->pusb_intf);
848 complete_and_exit(&us->scanning_done, 0); 850 complete_and_exit(&us->scanning_done, 0);
849} 851}
850 852
@@ -968,6 +970,7 @@ int usb_stor_probe2(struct us_data *us)
968 goto BadDevice; 970 goto BadDevice;
969 } 971 }
970 972
973 usb_autopm_get_interface_no_resume(us->pusb_intf);
971 wake_up_process(th); 974 wake_up_process(th);
972 975
973 return 0; 976 return 0;
@@ -1040,6 +1043,7 @@ static struct usb_driver usb_storage_driver = {
1040 .pre_reset = usb_stor_pre_reset, 1043 .pre_reset = usb_stor_pre_reset,
1041 .post_reset = usb_stor_post_reset, 1044 .post_reset = usb_stor_post_reset,
1042 .id_table = usb_storage_usb_ids, 1045 .id_table = usb_storage_usb_ids,
1046 .supports_autosuspend = 1,
1043 .soft_unbind = 1, 1047 .soft_unbind = 1,
1044}; 1048};
1045 1049
diff --git a/drivers/usb/usb-skeleton.c b/drivers/usb/usb-skeleton.c
index d110588b56f..552679b8dbd 100644
--- a/drivers/usb/usb-skeleton.c
+++ b/drivers/usb/usb-skeleton.c
@@ -142,7 +142,7 @@ static int skel_release(struct inode *inode, struct file *file)
142{ 142{
143 struct usb_skel *dev; 143 struct usb_skel *dev;
144 144
145 dev = (struct usb_skel *)file->private_data; 145 dev = file->private_data;
146 if (dev == NULL) 146 if (dev == NULL)
147 return -ENODEV; 147 return -ENODEV;
148 148
@@ -162,7 +162,7 @@ static int skel_flush(struct file *file, fl_owner_t id)
162 struct usb_skel *dev; 162 struct usb_skel *dev;
163 int res; 163 int res;
164 164
165 dev = (struct usb_skel *)file->private_data; 165 dev = file->private_data;
166 if (dev == NULL) 166 if (dev == NULL)
167 return -ENODEV; 167 return -ENODEV;
168 168
@@ -246,7 +246,7 @@ static ssize_t skel_read(struct file *file, char *buffer, size_t count,
246 int rv; 246 int rv;
247 bool ongoing_io; 247 bool ongoing_io;
248 248
249 dev = (struct usb_skel *)file->private_data; 249 dev = file->private_data;
250 250
251 /* if we cannot read at all, return EOF */ 251 /* if we cannot read at all, return EOF */
252 if (!dev->bulk_in_urb || !count) 252 if (!dev->bulk_in_urb || !count)
@@ -401,7 +401,7 @@ static ssize_t skel_write(struct file *file, const char *user_buffer,
401 char *buf = NULL; 401 char *buf = NULL;
402 size_t writesize = min(count, (size_t)MAX_TRANSFER); 402 size_t writesize = min(count, (size_t)MAX_TRANSFER);
403 403
404 dev = (struct usb_skel *)file->private_data; 404 dev = file->private_data;
405 405
406 /* verify that we actually have some data to write */ 406 /* verify that we actually have some data to write */
407 if (count == 0) 407 if (count == 0)
diff --git a/drivers/video/console/fbcon.c b/drivers/video/console/fbcon.c
index 3b3f5749af9..26bf7cbfecc 100644
--- a/drivers/video/console/fbcon.c
+++ b/drivers/video/console/fbcon.c
@@ -283,7 +283,8 @@ static inline int fbcon_is_inactive(struct vc_data *vc, struct fb_info *info)
283 struct fbcon_ops *ops = info->fbcon_par; 283 struct fbcon_ops *ops = info->fbcon_par;
284 284
285 return (info->state != FBINFO_STATE_RUNNING || 285 return (info->state != FBINFO_STATE_RUNNING ||
286 vc->vc_mode != KD_TEXT || ops->graphics); 286 vc->vc_mode != KD_TEXT || ops->graphics) &&
287 !vt_force_oops_output(vc);
287} 288}
288 289
289static inline int get_color(struct vc_data *vc, struct fb_info *info, 290static inline int get_color(struct vc_data *vc, struct fb_info *info,
@@ -1073,6 +1074,7 @@ static void fbcon_init(struct vc_data *vc, int init)
1073 if (p->userfont) 1074 if (p->userfont)
1074 charcnt = FNTCHARCNT(p->fontdata); 1075 charcnt = FNTCHARCNT(p->fontdata);
1075 1076
1077 vc->vc_panic_force_write = !!(info->flags & FBINFO_CAN_FORCE_OUTPUT);
1076 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1); 1078 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1077 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800; 1079 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1078 if (charcnt == 256) { 1080 if (charcnt == 256) {
diff --git a/drivers/video/console/vgacon.c b/drivers/video/console/vgacon.c
index 182dd6f8aad..54e32c51361 100644
--- a/drivers/video/console/vgacon.c
+++ b/drivers/video/console/vgacon.c
@@ -1108,7 +1108,6 @@ static int vgacon_do_font_op(struct vgastate *state,char *arg,int set,int ch512)
1108 charmap += 4 * cmapsz; 1108 charmap += 4 * cmapsz;
1109#endif 1109#endif
1110 1110
1111 unlock_kernel();
1112 spin_lock_irq(&vga_lock); 1111 spin_lock_irq(&vga_lock);
1113 /* First, the Sequencer */ 1112 /* First, the Sequencer */
1114 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x1); 1113 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x1);
@@ -1192,7 +1191,6 @@ static int vgacon_do_font_op(struct vgastate *state,char *arg,int set,int ch512)
1192 vga_wattr(state->vgabase, VGA_AR_ENABLE_DISPLAY, 0); 1191 vga_wattr(state->vgabase, VGA_AR_ENABLE_DISPLAY, 0);
1193 } 1192 }
1194 spin_unlock_irq(&vga_lock); 1193 spin_unlock_irq(&vga_lock);
1195 lock_kernel();
1196 return 0; 1194 return 0;
1197} 1195}
1198 1196
diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
index b04b1846893..4d2992aadfb 100644
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -73,6 +73,13 @@ config WM8350_WATCHDOG
73 73
74# ARM Architecture 74# ARM Architecture
75 75
76config ARM_SP805_WATCHDOG
77 tristate "ARM SP805 Watchdog"
78 depends on ARM_AMBA
79 help
80 ARM Primecell SP805 Watchdog timer. This will reboot your system when
81 the timeout is reached.
82
76config AT91RM9200_WATCHDOG 83config AT91RM9200_WATCHDOG
77 tristate "AT91RM9200 watchdog" 84 tristate "AT91RM9200 watchdog"
78 depends on ARCH_AT91RM9200 85 depends on ARCH_AT91RM9200
@@ -401,6 +408,17 @@ config ALIM7101_WDT
401 408
402 Most people will say N. 409 Most people will say N.
403 410
411config F71808E_WDT
412 tristate "Fintek F71808E and F71882FG Watchdog"
413 depends on X86 && EXPERIMENTAL
414 help
415 This is the driver for the hardware watchdog on the Fintek
416 F71808E and F71882FG Super I/O controllers.
417
418 You can compile this driver directly into the kernel, or use
419 it as a module. The module will be called f71808e_wdt.
420
421
404config GEODE_WDT 422config GEODE_WDT
405 tristate "AMD Geode CS5535/CS5536 Watchdog" 423 tristate "AMD Geode CS5535/CS5536 Watchdog"
406 depends on CS5535_MFGPT 424 depends on CS5535_MFGPT
diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
index e30289a5e36..8374503fcc6 100644
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -25,6 +25,7 @@ obj-$(CONFIG_USBPCWATCHDOG) += pcwd_usb.o
25# ALPHA Architecture 25# ALPHA Architecture
26 26
27# ARM Architecture 27# ARM Architecture
28obj-$(CONFIG_ARM_SP805_WATCHDOG) += sp805_wdt.o
28obj-$(CONFIG_AT91RM9200_WATCHDOG) += at91rm9200_wdt.o 29obj-$(CONFIG_AT91RM9200_WATCHDOG) += at91rm9200_wdt.o
29obj-$(CONFIG_AT91SAM9X_WATCHDOG) += at91sam9_wdt.o 30obj-$(CONFIG_AT91SAM9X_WATCHDOG) += at91sam9_wdt.o
30obj-$(CONFIG_OMAP_WATCHDOG) += omap_wdt.o 31obj-$(CONFIG_OMAP_WATCHDOG) += omap_wdt.o
@@ -66,6 +67,7 @@ obj-$(CONFIG_ACQUIRE_WDT) += acquirewdt.o
66obj-$(CONFIG_ADVANTECH_WDT) += advantechwdt.o 67obj-$(CONFIG_ADVANTECH_WDT) += advantechwdt.o
67obj-$(CONFIG_ALIM1535_WDT) += alim1535_wdt.o 68obj-$(CONFIG_ALIM1535_WDT) += alim1535_wdt.o
68obj-$(CONFIG_ALIM7101_WDT) += alim7101_wdt.o 69obj-$(CONFIG_ALIM7101_WDT) += alim7101_wdt.o
70obj-$(CONFIG_F71808E_WDT) += f71808e_wdt.o
69obj-$(CONFIG_GEODE_WDT) += geodewdt.o 71obj-$(CONFIG_GEODE_WDT) += geodewdt.o
70obj-$(CONFIG_SC520_WDT) += sc520_wdt.o 72obj-$(CONFIG_SC520_WDT) += sc520_wdt.o
71obj-$(CONFIG_SBC_FITPC2_WATCHDOG) += sbc_fitpc2_wdt.o 73obj-$(CONFIG_SBC_FITPC2_WATCHDOG) += sbc_fitpc2_wdt.o
diff --git a/drivers/watchdog/f71808e_wdt.c b/drivers/watchdog/f71808e_wdt.c
new file mode 100644
index 00000000000..7e5c266cda4
--- /dev/null
+++ b/drivers/watchdog/f71808e_wdt.c
@@ -0,0 +1,768 @@
1/***************************************************************************
2 * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
3 * Copyright (C) 2007-2009 Hans de Goede <hdegoede@redhat.com> *
4 * Copyright (C) 2010 Giel van Schijndel <me@mortis.eu> *
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 *
18 * Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21
22#include <linux/err.h>
23#include <linux/fs.h>
24#include <linux/init.h>
25#include <linux/io.h>
26#include <linux/ioport.h>
27#include <linux/miscdevice.h>
28#include <linux/module.h>
29#include <linux/mutex.h>
30#include <linux/notifier.h>
31#include <linux/reboot.h>
32#include <linux/uaccess.h>
33#include <linux/watchdog.h>
34
35#define DRVNAME "f71808e_wdt"
36
37#define SIO_F71808FG_LD_WDT 0x07 /* Watchdog timer logical device */
38#define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
39#define SIO_LOCK_KEY 0xAA /* Key to diasble Super-I/O */
40
41#define SIO_REG_LDSEL 0x07 /* Logical device select */
42#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
43#define SIO_REG_DEVREV 0x22 /* Device revision */
44#define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
45#define SIO_REG_ENABLE 0x30 /* Logical device enable */
46#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
47
48#define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
49#define SIO_F71808_ID 0x0901 /* Chipset ID */
50#define SIO_F71858_ID 0x0507 /* Chipset ID */
51#define SIO_F71862_ID 0x0601 /* Chipset ID */
52#define SIO_F71882_ID 0x0541 /* Chipset ID */
53#define SIO_F71889_ID 0x0723 /* Chipset ID */
54
55#define F71882FG_REG_START 0x01
56
57#define F71808FG_REG_WDO_CONF 0xf0
58#define F71808FG_REG_WDT_CONF 0xf5
59#define F71808FG_REG_WD_TIME 0xf6
60
61#define F71808FG_FLAG_WDOUT_EN 7
62
63#define F71808FG_FLAG_WDTMOUT_STS 5
64#define F71808FG_FLAG_WD_EN 5
65#define F71808FG_FLAG_WD_PULSE 4
66#define F71808FG_FLAG_WD_UNIT 3
67
68/* Default values */
69#define WATCHDOG_TIMEOUT 60 /* 1 minute default timeout */
70#define WATCHDOG_MAX_TIMEOUT (60 * 255)
71#define WATCHDOG_PULSE_WIDTH 125 /* 125 ms, default pulse width for
72 watchdog signal */
73
74static unsigned short force_id;
75module_param(force_id, ushort, 0);
76MODULE_PARM_DESC(force_id, "Override the detected device ID");
77
78static const int max_timeout = WATCHDOG_MAX_TIMEOUT;
79static int timeout = 60; /* default timeout in seconds */
80module_param(timeout, int, 0);
81MODULE_PARM_DESC(timeout,
82 "Watchdog timeout in seconds. 1<= timeout <="
83 __MODULE_STRING(WATCHDOG_MAX_TIMEOUT) " (default="
84 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
85
86static unsigned int pulse_width = WATCHDOG_PULSE_WIDTH;
87module_param(pulse_width, uint, 0);
88MODULE_PARM_DESC(pulse_width,
89 "Watchdog signal pulse width. 0(=level), 1 ms, 25 ms, 125 ms or 5000 ms"
90 " (default=" __MODULE_STRING(WATCHDOG_PULSE_WIDTH) ")");
91
92static int nowayout = WATCHDOG_NOWAYOUT;
93module_param(nowayout, bool, 0444);
94MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
95
96static unsigned int start_withtimeout;
97module_param(start_withtimeout, uint, 0);
98MODULE_PARM_DESC(start_withtimeout, "Start watchdog timer on module load with"
99 " given initial timeout. Zero (default) disables this feature.");
100
101enum chips { f71808fg, f71858fg, f71862fg, f71882fg, f71889fg };
102
103static const char *f71808e_names[] = {
104 "f71808fg",
105 "f71858fg",
106 "f71862fg",
107 "f71882fg",
108 "f71889fg",
109};
110
111/* Super-I/O Function prototypes */
112static inline int superio_inb(int base, int reg);
113static inline int superio_inw(int base, int reg);
114static inline void superio_outb(int base, int reg, u8 val);
115static inline void superio_set_bit(int base, int reg, int bit);
116static inline void superio_clear_bit(int base, int reg, int bit);
117static inline int superio_enter(int base);
118static inline void superio_select(int base, int ld);
119static inline void superio_exit(int base);
120
121struct watchdog_data {
122 unsigned short sioaddr;
123 enum chips type;
124 unsigned long opened;
125 struct mutex lock;
126 char expect_close;
127 struct watchdog_info ident;
128
129 unsigned short timeout;
130 u8 timer_val; /* content for the wd_time register */
131 char minutes_mode;
132 u8 pulse_val; /* pulse width flag */
133 char pulse_mode; /* enable pulse output mode? */
134 char caused_reboot; /* last reboot was by the watchdog */
135};
136
137static struct watchdog_data watchdog = {
138 .lock = __MUTEX_INITIALIZER(watchdog.lock),
139};
140
141/* Super I/O functions */
142static inline int superio_inb(int base, int reg)
143{
144 outb(reg, base);
145 return inb(base + 1);
146}
147
148static int superio_inw(int base, int reg)
149{
150 int val;
151 val = superio_inb(base, reg) << 8;
152 val |= superio_inb(base, reg + 1);
153 return val;
154}
155
156static inline void superio_outb(int base, int reg, u8 val)
157{
158 outb(reg, base);
159 outb(val, base + 1);
160}
161
162static inline void superio_set_bit(int base, int reg, int bit)
163{
164 unsigned long val = superio_inb(base, reg);
165 __set_bit(bit, &val);
166 superio_outb(base, reg, val);
167}
168
169static inline void superio_clear_bit(int base, int reg, int bit)
170{
171 unsigned long val = superio_inb(base, reg);
172 __clear_bit(bit, &val);
173 superio_outb(base, reg, val);
174}
175
176static inline int superio_enter(int base)
177{
178 /* Don't step on other drivers' I/O space by accident */
179 if (!request_muxed_region(base, 2, DRVNAME)) {
180 printk(KERN_ERR DRVNAME ": I/O address 0x%04x already in use\n",
181 (int)base);
182 return -EBUSY;
183 }
184
185 /* according to the datasheet the key must be send twice! */
186 outb(SIO_UNLOCK_KEY, base);
187 outb(SIO_UNLOCK_KEY, base);
188
189 return 0;
190}
191
192static inline void superio_select(int base, int ld)
193{
194 outb(SIO_REG_LDSEL, base);
195 outb(ld, base + 1);
196}
197
198static inline void superio_exit(int base)
199{
200 outb(SIO_LOCK_KEY, base);
201 release_region(base, 2);
202}
203
204static int watchdog_set_timeout(int timeout)
205{
206 if (timeout <= 0
207 || timeout > max_timeout) {
208 printk(KERN_ERR DRVNAME ": watchdog timeout out of range\n");
209 return -EINVAL;
210 }
211
212 mutex_lock(&watchdog.lock);
213
214 watchdog.timeout = timeout;
215 if (timeout > 0xff) {
216 watchdog.timer_val = DIV_ROUND_UP(timeout, 60);
217 watchdog.minutes_mode = true;
218 } else {
219 watchdog.timer_val = timeout;
220 watchdog.minutes_mode = false;
221 }
222
223 mutex_unlock(&watchdog.lock);
224
225 return 0;
226}
227
228static int watchdog_set_pulse_width(unsigned int pw)
229{
230 int err = 0;
231
232 mutex_lock(&watchdog.lock);
233
234 if (pw <= 1) {
235 watchdog.pulse_val = 0;
236 } else if (pw <= 25) {
237 watchdog.pulse_val = 1;
238 } else if (pw <= 125) {
239 watchdog.pulse_val = 2;
240 } else if (pw <= 5000) {
241 watchdog.pulse_val = 3;
242 } else {
243 printk(KERN_ERR DRVNAME ": pulse width out of range\n");
244 err = -EINVAL;
245 goto exit_unlock;
246 }
247
248 watchdog.pulse_mode = pw;
249
250exit_unlock:
251 mutex_unlock(&watchdog.lock);
252 return err;
253}
254
255static int watchdog_keepalive(void)
256{
257 int err = 0;
258
259 mutex_lock(&watchdog.lock);
260 err = superio_enter(watchdog.sioaddr);
261 if (err)
262 goto exit_unlock;
263 superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
264
265 if (watchdog.minutes_mode)
266 /* select minutes for timer units */
267 superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
268 F71808FG_FLAG_WD_UNIT);
269 else
270 /* select seconds for timer units */
271 superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
272 F71808FG_FLAG_WD_UNIT);
273
274 /* Set timer value */
275 superio_outb(watchdog.sioaddr, F71808FG_REG_WD_TIME,
276 watchdog.timer_val);
277
278 superio_exit(watchdog.sioaddr);
279
280exit_unlock:
281 mutex_unlock(&watchdog.lock);
282 return err;
283}
284
285static int watchdog_start(void)
286{
287 /* Make sure we don't die as soon as the watchdog is enabled below */
288 int err = watchdog_keepalive();
289 if (err)
290 return err;
291
292 mutex_lock(&watchdog.lock);
293 err = superio_enter(watchdog.sioaddr);
294 if (err)
295 goto exit_unlock;
296 superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
297
298 /* Watchdog pin configuration */
299 switch (watchdog.type) {
300 case f71808fg:
301 /* Set pin 21 to GPIO23/WDTRST#, then to WDTRST# */
302 superio_clear_bit(watchdog.sioaddr, 0x2a, 3);
303 superio_clear_bit(watchdog.sioaddr, 0x2b, 3);
304 break;
305
306 case f71882fg:
307 /* Set pin 56 to WDTRST# */
308 superio_set_bit(watchdog.sioaddr, 0x29, 1);
309 break;
310
311 default:
312 /*
313 * 'default' label to shut up the compiler and catch
314 * programmer errors
315 */
316 err = -ENODEV;
317 goto exit_superio;
318 }
319
320 superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
321 superio_set_bit(watchdog.sioaddr, SIO_REG_ENABLE, 0);
322 superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDO_CONF,
323 F71808FG_FLAG_WDOUT_EN);
324
325 superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
326 F71808FG_FLAG_WD_EN);
327
328 if (watchdog.pulse_mode) {
329 /* Select "pulse" output mode with given duration */
330 u8 wdt_conf = superio_inb(watchdog.sioaddr,
331 F71808FG_REG_WDT_CONF);
332
333 /* Set WD_PSWIDTH bits (1:0) */
334 wdt_conf = (wdt_conf & 0xfc) | (watchdog.pulse_val & 0x03);
335 /* Set WD_PULSE to "pulse" mode */
336 wdt_conf |= BIT(F71808FG_FLAG_WD_PULSE);
337
338 superio_outb(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
339 wdt_conf);
340 } else {
341 /* Select "level" output mode */
342 superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
343 F71808FG_FLAG_WD_PULSE);
344 }
345
346exit_superio:
347 superio_exit(watchdog.sioaddr);
348exit_unlock:
349 mutex_unlock(&watchdog.lock);
350
351 return err;
352}
353
354static int watchdog_stop(void)
355{
356 int err = 0;
357
358 mutex_lock(&watchdog.lock);
359 err = superio_enter(watchdog.sioaddr);
360 if (err)
361 goto exit_unlock;
362 superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
363
364 superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
365 F71808FG_FLAG_WD_EN);
366
367 superio_exit(watchdog.sioaddr);
368
369exit_unlock:
370 mutex_unlock(&watchdog.lock);
371
372 return err;
373}
374
375static int watchdog_get_status(void)
376{
377 int status = 0;
378
379 mutex_lock(&watchdog.lock);
380 status = (watchdog.caused_reboot) ? WDIOF_CARDRESET : 0;
381 mutex_unlock(&watchdog.lock);
382
383 return status;
384}
385
386static bool watchdog_is_running(void)
387{
388 /*
389 * if we fail to determine the watchdog's status assume it to be
390 * running to be on the safe side
391 */
392 bool is_running = true;
393
394 mutex_lock(&watchdog.lock);
395 if (superio_enter(watchdog.sioaddr))
396 goto exit_unlock;
397 superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
398
399 is_running = (superio_inb(watchdog.sioaddr, SIO_REG_ENABLE) & BIT(0))
400 && (superio_inb(watchdog.sioaddr, F71808FG_REG_WDT_CONF)
401 & F71808FG_FLAG_WD_EN);
402
403 superio_exit(watchdog.sioaddr);
404
405exit_unlock:
406 mutex_unlock(&watchdog.lock);
407 return is_running;
408}
409
410/* /dev/watchdog api */
411
412static int watchdog_open(struct inode *inode, struct file *file)
413{
414 int err;
415
416 /* If the watchdog is alive we don't need to start it again */
417 if (test_and_set_bit(0, &watchdog.opened))
418 return -EBUSY;
419
420 err = watchdog_start();
421 if (err) {
422 clear_bit(0, &watchdog.opened);
423 return err;
424 }
425
426 if (nowayout)
427 __module_get(THIS_MODULE);
428
429 watchdog.expect_close = 0;
430 return nonseekable_open(inode, file);
431}
432
433static int watchdog_release(struct inode *inode, struct file *file)
434{
435 clear_bit(0, &watchdog.opened);
436
437 if (!watchdog.expect_close) {
438 watchdog_keepalive();
439 printk(KERN_CRIT DRVNAME
440 ": Unexpected close, not stopping watchdog!\n");
441 } else if (!nowayout) {
442 watchdog_stop();
443 }
444 return 0;
445}
446
447/*
448 * watchdog_write:
449 * @file: file handle to the watchdog
450 * @buf: buffer to write
451 * @count: count of bytes
452 * @ppos: pointer to the position to write. No seeks allowed
453 *
454 * A write to a watchdog device is defined as a keepalive signal. Any
455 * write of data will do, as we we don't define content meaning.
456 */
457
458static ssize_t watchdog_write(struct file *file, const char __user *buf,
459 size_t count, loff_t *ppos)
460{
461 if (count) {
462 if (!nowayout) {
463 size_t i;
464
465 /* In case it was set long ago */
466 bool expect_close = false;
467
468 for (i = 0; i != count; i++) {
469 char c;
470 if (get_user(c, buf + i))
471 return -EFAULT;
472 expect_close = (c == 'V');
473 }
474
475 /* Properly order writes across fork()ed processes */
476 mutex_lock(&watchdog.lock);
477 watchdog.expect_close = expect_close;
478 mutex_unlock(&watchdog.lock);
479 }
480
481 /* someone wrote to us, we should restart timer */
482 watchdog_keepalive();
483 }
484 return count;
485}
486
487/*
488 * watchdog_ioctl:
489 * @inode: inode of the device
490 * @file: file handle to the device
491 * @cmd: watchdog command
492 * @arg: argument pointer
493 *
494 * The watchdog API defines a common set of functions for all watchdogs
495 * according to their available features.
496 */
497static long watchdog_ioctl(struct file *file, unsigned int cmd,
498 unsigned long arg)
499{
500 int status;
501 int new_options;
502 int new_timeout;
503 union {
504 struct watchdog_info __user *ident;
505 int __user *i;
506 } uarg;
507
508 uarg.i = (int __user *)arg;
509
510 switch (cmd) {
511 case WDIOC_GETSUPPORT:
512 return copy_to_user(uarg.ident, &watchdog.ident,
513 sizeof(watchdog.ident)) ? -EFAULT : 0;
514
515 case WDIOC_GETSTATUS:
516 status = watchdog_get_status();
517 if (status < 0)
518 return status;
519 return put_user(status, uarg.i);
520
521 case WDIOC_GETBOOTSTATUS:
522 return put_user(0, uarg.i);
523
524 case WDIOC_SETOPTIONS:
525 if (get_user(new_options, uarg.i))
526 return -EFAULT;
527
528 if (new_options & WDIOS_DISABLECARD)
529 watchdog_stop();
530
531 if (new_options & WDIOS_ENABLECARD)
532 return watchdog_start();
533
534
535 case WDIOC_KEEPALIVE:
536 watchdog_keepalive();
537 return 0;
538
539 case WDIOC_SETTIMEOUT:
540 if (get_user(new_timeout, uarg.i))
541 return -EFAULT;
542
543 if (watchdog_set_timeout(new_timeout))
544 return -EINVAL;
545
546 watchdog_keepalive();
547 /* Fall */
548
549 case WDIOC_GETTIMEOUT:
550 return put_user(watchdog.timeout, uarg.i);
551
552 default:
553 return -ENOTTY;
554
555 }
556}
557
558static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
559 void *unused)
560{
561 if (code == SYS_DOWN || code == SYS_HALT)
562 watchdog_stop();
563 return NOTIFY_DONE;
564}
565
566static const struct file_operations watchdog_fops = {
567 .owner = THIS_MODULE,
568 .llseek = no_llseek,
569 .open = watchdog_open,
570 .release = watchdog_release,
571 .write = watchdog_write,
572 .unlocked_ioctl = watchdog_ioctl,
573};
574
575static struct miscdevice watchdog_miscdev = {
576 .minor = WATCHDOG_MINOR,
577 .name = "watchdog",
578 .fops = &watchdog_fops,
579};
580
581static struct notifier_block watchdog_notifier = {
582 .notifier_call = watchdog_notify_sys,
583};
584
585static int __init watchdog_init(int sioaddr)
586{
587 int wdt_conf, err = 0;
588
589 /* No need to lock watchdog.lock here because no entry points
590 * into the module have been registered yet.
591 */
592 watchdog.sioaddr = sioaddr;
593 watchdog.ident.options = WDIOC_SETTIMEOUT
594 | WDIOF_MAGICCLOSE
595 | WDIOF_KEEPALIVEPING;
596
597 snprintf(watchdog.ident.identity,
598 sizeof(watchdog.ident.identity), "%s watchdog",
599 f71808e_names[watchdog.type]);
600
601 err = superio_enter(sioaddr);
602 if (err)
603 return err;
604 superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
605
606 wdt_conf = superio_inb(sioaddr, F71808FG_REG_WDT_CONF);
607 watchdog.caused_reboot = wdt_conf & F71808FG_FLAG_WDTMOUT_STS;
608
609 superio_exit(sioaddr);
610
611 err = watchdog_set_timeout(timeout);
612 if (err)
613 return err;
614 err = watchdog_set_pulse_width(pulse_width);
615 if (err)
616 return err;
617
618 err = register_reboot_notifier(&watchdog_notifier);
619 if (err)
620 return err;
621
622 err = misc_register(&watchdog_miscdev);
623 if (err) {
624 printk(KERN_ERR DRVNAME
625 ": cannot register miscdev on minor=%d\n",
626 watchdog_miscdev.minor);
627 goto exit_reboot;
628 }
629
630 if (start_withtimeout) {
631 if (start_withtimeout <= 0
632 || start_withtimeout > max_timeout) {
633 printk(KERN_ERR DRVNAME
634 ": starting timeout out of range\n");
635 err = -EINVAL;
636 goto exit_miscdev;
637 }
638
639 err = watchdog_start();
640 if (err) {
641 printk(KERN_ERR DRVNAME
642 ": cannot start watchdog timer\n");
643 goto exit_miscdev;
644 }
645
646 mutex_lock(&watchdog.lock);
647 err = superio_enter(sioaddr);
648 if (err)
649 goto exit_unlock;
650 superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
651
652 if (start_withtimeout > 0xff) {
653 /* select minutes for timer units */
654 superio_set_bit(sioaddr, F71808FG_REG_WDT_CONF,
655 F71808FG_FLAG_WD_UNIT);
656 superio_outb(sioaddr, F71808FG_REG_WD_TIME,
657 DIV_ROUND_UP(start_withtimeout, 60));
658 } else {
659 /* select seconds for timer units */
660 superio_clear_bit(sioaddr, F71808FG_REG_WDT_CONF,
661 F71808FG_FLAG_WD_UNIT);
662 superio_outb(sioaddr, F71808FG_REG_WD_TIME,
663 start_withtimeout);
664 }
665
666 superio_exit(sioaddr);
667 mutex_unlock(&watchdog.lock);
668
669 if (nowayout)
670 __module_get(THIS_MODULE);
671
672 printk(KERN_INFO DRVNAME
673 ": watchdog started with initial timeout of %u sec\n",
674 start_withtimeout);
675 }
676
677 return 0;
678
679exit_unlock:
680 mutex_unlock(&watchdog.lock);
681exit_miscdev:
682 misc_deregister(&watchdog_miscdev);
683exit_reboot:
684 unregister_reboot_notifier(&watchdog_notifier);
685
686 return err;
687}
688
689static int __init f71808e_find(int sioaddr)
690{
691 u16 devid;
692 int err = superio_enter(sioaddr);
693 if (err)
694 return err;
695
696 devid = superio_inw(sioaddr, SIO_REG_MANID);
697 if (devid != SIO_FINTEK_ID) {
698 pr_debug(DRVNAME ": Not a Fintek device\n");
699 err = -ENODEV;
700 goto exit;
701 }
702
703 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
704 switch (devid) {
705 case SIO_F71808_ID:
706 watchdog.type = f71808fg;
707 break;
708 case SIO_F71882_ID:
709 watchdog.type = f71882fg;
710 break;
711 case SIO_F71862_ID:
712 case SIO_F71889_ID:
713 /* These have a watchdog, though it isn't implemented (yet). */
714 err = -ENOSYS;
715 goto exit;
716 case SIO_F71858_ID:
717 /* Confirmed (by datasheet) not to have a watchdog. */
718 err = -ENODEV;
719 goto exit;
720 default:
721 printk(KERN_INFO DRVNAME ": Unrecognized Fintek device: %04x\n",
722 (unsigned int)devid);
723 err = -ENODEV;
724 goto exit;
725 }
726
727 printk(KERN_INFO DRVNAME ": Found %s watchdog chip, revision %d\n",
728 f71808e_names[watchdog.type],
729 (int)superio_inb(sioaddr, SIO_REG_DEVREV));
730exit:
731 superio_exit(sioaddr);
732 return err;
733}
734
735static int __init f71808e_init(void)
736{
737 static const unsigned short addrs[] = { 0x2e, 0x4e };
738 int err = -ENODEV;
739 int i;
740
741 for (i = 0; i < ARRAY_SIZE(addrs); i++) {
742 err = f71808e_find(addrs[i]);
743 if (err == 0)
744 break;
745 }
746 if (i == ARRAY_SIZE(addrs))
747 return err;
748
749 return watchdog_init(addrs[i]);
750}
751
752static void __exit f71808e_exit(void)
753{
754 if (watchdog_is_running()) {
755 printk(KERN_WARNING DRVNAME
756 ": Watchdog timer still running, stopping it\n");
757 watchdog_stop();
758 }
759 misc_deregister(&watchdog_miscdev);
760 unregister_reboot_notifier(&watchdog_notifier);
761}
762
763MODULE_DESCRIPTION("F71808E Watchdog Driver");
764MODULE_AUTHOR("Giel van Schijndel <me@mortis.eu>");
765MODULE_LICENSE("GPL");
766
767module_init(f71808e_init);
768module_exit(f71808e_exit);
diff --git a/drivers/watchdog/hpwdt.c b/drivers/watchdog/hpwdt.c
index 809e7167a62..fd312fc8940 100644
--- a/drivers/watchdog/hpwdt.c
+++ b/drivers/watchdog/hpwdt.c
@@ -246,8 +246,8 @@ static int __devinit cru_detect(unsigned long map_entry,
246 physical_bios_offset); 246 physical_bios_offset);
247 printk(KERN_DEBUG "hpwdt: CRU Length: 0x%lx\n", 247 printk(KERN_DEBUG "hpwdt: CRU Length: 0x%lx\n",
248 cru_length); 248 cru_length);
249 printk(KERN_DEBUG "hpwdt: CRU Mapped Address: 0x%x\n", 249 printk(KERN_DEBUG "hpwdt: CRU Mapped Address: %p\n",
250 (unsigned int)&cru_rom_addr); 250 &cru_rom_addr);
251 } 251 }
252 iounmap(bios32_map); 252 iounmap(bios32_map);
253 return retval; 253 return retval;
diff --git a/drivers/watchdog/s3c2410_wdt.c b/drivers/watchdog/s3c2410_wdt.c
index 300932580de..ae53662c29b 100644
--- a/drivers/watchdog/s3c2410_wdt.c
+++ b/drivers/watchdog/s3c2410_wdt.c
@@ -532,21 +532,22 @@ static int __devinit s3c2410wdt_probe(struct platform_device *pdev)
532 532
533static int __devexit s3c2410wdt_remove(struct platform_device *dev) 533static int __devexit s3c2410wdt_remove(struct platform_device *dev)
534{ 534{
535 s3c2410wdt_cpufreq_deregister(); 535 misc_deregister(&s3c2410wdt_miscdev);
536
537 release_resource(wdt_mem);
538 kfree(wdt_mem);
539 wdt_mem = NULL;
540 536
541 free_irq(wdt_irq->start, dev); 537 s3c2410wdt_cpufreq_deregister();
542 wdt_irq = NULL;
543 538
544 clk_disable(wdt_clock); 539 clk_disable(wdt_clock);
545 clk_put(wdt_clock); 540 clk_put(wdt_clock);
546 wdt_clock = NULL; 541 wdt_clock = NULL;
547 542
543 free_irq(wdt_irq->start, dev);
544 wdt_irq = NULL;
545
548 iounmap(wdt_base); 546 iounmap(wdt_base);
549 misc_deregister(&s3c2410wdt_miscdev); 547
548 release_resource(wdt_mem);
549 kfree(wdt_mem);
550 wdt_mem = NULL;
550 return 0; 551 return 0;
551} 552}
552 553
diff --git a/drivers/watchdog/sch311x_wdt.c b/drivers/watchdog/sch311x_wdt.c
index 9c40f48804f..0461858e07d 100644
--- a/drivers/watchdog/sch311x_wdt.c
+++ b/drivers/watchdog/sch311x_wdt.c
@@ -425,6 +425,8 @@ static int __devinit sch311x_wdt_probe(struct platform_device *pdev)
425 val = therm_trip ? 0x06 : 0x04; 425 val = therm_trip ? 0x06 : 0x04;
426 outb(val, sch311x_wdt_data.runtime_reg + RESGEN); 426 outb(val, sch311x_wdt_data.runtime_reg + RESGEN);
427 427
428 sch311x_wdt_miscdev.parent = dev;
429
428 err = misc_register(&sch311x_wdt_miscdev); 430 err = misc_register(&sch311x_wdt_miscdev);
429 if (err != 0) { 431 if (err != 0) {
430 dev_err(dev, "cannot register miscdev on minor=%d (err=%d)\n", 432 dev_err(dev, "cannot register miscdev on minor=%d (err=%d)\n",
@@ -432,8 +434,6 @@ static int __devinit sch311x_wdt_probe(struct platform_device *pdev)
432 goto exit_release_region3; 434 goto exit_release_region3;
433 } 435 }
434 436
435 sch311x_wdt_miscdev.parent = dev;
436
437 dev_info(dev, 437 dev_info(dev,
438 "SMSC SCH311x WDT initialized. timeout=%d sec (nowayout=%d)\n", 438 "SMSC SCH311x WDT initialized. timeout=%d sec (nowayout=%d)\n",
439 timeout, nowayout); 439 timeout, nowayout);
diff --git a/drivers/watchdog/sp805_wdt.c b/drivers/watchdog/sp805_wdt.c
new file mode 100644
index 00000000000..9127eda2145
--- /dev/null
+++ b/drivers/watchdog/sp805_wdt.c
@@ -0,0 +1,387 @@
1/*
2 * drivers/char/watchdog/sp805-wdt.c
3 *
4 * Watchdog driver for ARM SP805 watchdog module
5 *
6 * Copyright (C) 2010 ST Microelectronics
7 * Viresh Kumar<viresh.kumar@st.com>
8 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2 or later. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
14#include <linux/device.h>
15#include <linux/resource.h>
16#include <linux/amba/bus.h>
17#include <linux/bitops.h>
18#include <linux/clk.h>
19#include <linux/fs.h>
20#include <linux/init.h>
21#include <linux/io.h>
22#include <linux/ioport.h>
23#include <linux/kernel.h>
24#include <linux/math64.h>
25#include <linux/miscdevice.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/slab.h>
29#include <linux/spinlock.h>
30#include <linux/types.h>
31#include <linux/uaccess.h>
32#include <linux/watchdog.h>
33
34/* default timeout in seconds */
35#define DEFAULT_TIMEOUT 60
36
37#define MODULE_NAME "sp805-wdt"
38
39/* watchdog register offsets and masks */
40#define WDTLOAD 0x000
41 #define LOAD_MIN 0x00000001
42 #define LOAD_MAX 0xFFFFFFFF
43#define WDTVALUE 0x004
44#define WDTCONTROL 0x008
45 /* control register masks */
46 #define INT_ENABLE (1 << 0)
47 #define RESET_ENABLE (1 << 1)
48#define WDTINTCLR 0x00C
49#define WDTRIS 0x010
50#define WDTMIS 0x014
51 #define INT_MASK (1 << 0)
52#define WDTLOCK 0xC00
53 #define UNLOCK 0x1ACCE551
54 #define LOCK 0x00000001
55
56/**
57 * struct sp805_wdt: sp805 wdt device structure
58 *
59 * lock: spin lock protecting dev structure and io access
60 * base: base address of wdt
61 * clk: clock structure of wdt
62 * dev: amba device structure of wdt
63 * status: current status of wdt
64 * load_val: load value to be set for current timeout
65 * timeout: current programmed timeout
66 */
67struct sp805_wdt {
68 spinlock_t lock;
69 void __iomem *base;
70 struct clk *clk;
71 struct amba_device *adev;
72 unsigned long status;
73 #define WDT_BUSY 0
74 #define WDT_CAN_BE_CLOSED 1
75 unsigned int load_val;
76 unsigned int timeout;
77};
78
79/* local variables */
80static struct sp805_wdt *wdt;
81static int nowayout = WATCHDOG_NOWAYOUT;
82
83/* This routine finds load value that will reset system in required timout */
84static void wdt_setload(unsigned int timeout)
85{
86 u64 load, rate;
87
88 rate = clk_get_rate(wdt->clk);
89
90 /*
91 * sp805 runs counter with given value twice, after the end of first
92 * counter it gives an interrupt and then starts counter again. If
93 * interrupt already occured then it resets the system. This is why
94 * load is half of what should be required.
95 */
96 load = div_u64(rate, 2) * timeout - 1;
97
98 load = (load > LOAD_MAX) ? LOAD_MAX : load;
99 load = (load < LOAD_MIN) ? LOAD_MIN : load;
100
101 spin_lock(&wdt->lock);
102 wdt->load_val = load;
103 /* roundup timeout to closest positive integer value */
104 wdt->timeout = div_u64((load + 1) * 2 + (rate / 2), rate);
105 spin_unlock(&wdt->lock);
106}
107
108/* returns number of seconds left for reset to occur */
109static u32 wdt_timeleft(void)
110{
111 u64 load, rate;
112
113 rate = clk_get_rate(wdt->clk);
114
115 spin_lock(&wdt->lock);
116 load = readl(wdt->base + WDTVALUE);
117
118 /*If the interrupt is inactive then time left is WDTValue + WDTLoad. */
119 if (!(readl(wdt->base + WDTRIS) & INT_MASK))
120 load += wdt->load_val + 1;
121 spin_unlock(&wdt->lock);
122
123 return div_u64(load, rate);
124}
125
126/* enables watchdog timers reset */
127static void wdt_enable(void)
128{
129 spin_lock(&wdt->lock);
130
131 writel(UNLOCK, wdt->base + WDTLOCK);
132 writel(wdt->load_val, wdt->base + WDTLOAD);
133 writel(INT_MASK, wdt->base + WDTINTCLR);
134 writel(INT_ENABLE | RESET_ENABLE, wdt->base + WDTCONTROL);
135 writel(LOCK, wdt->base + WDTLOCK);
136
137 spin_unlock(&wdt->lock);
138}
139
140/* disables watchdog timers reset */
141static void wdt_disable(void)
142{
143 spin_lock(&wdt->lock);
144
145 writel(UNLOCK, wdt->base + WDTLOCK);
146 writel(0, wdt->base + WDTCONTROL);
147 writel(0, wdt->base + WDTLOAD);
148 writel(LOCK, wdt->base + WDTLOCK);
149
150 spin_unlock(&wdt->lock);
151}
152
153static ssize_t sp805_wdt_write(struct file *file, const char *data,
154 size_t len, loff_t *ppos)
155{
156 if (len) {
157 if (!nowayout) {
158 size_t i;
159
160 clear_bit(WDT_CAN_BE_CLOSED, &wdt->status);
161
162 for (i = 0; i != len; i++) {
163 char c;
164
165 if (get_user(c, data + i))
166 return -EFAULT;
167 /* Check for Magic Close character */
168 if (c == 'V') {
169 set_bit(WDT_CAN_BE_CLOSED,
170 &wdt->status);
171 break;
172 }
173 }
174 }
175 wdt_enable();
176 }
177 return len;
178}
179
180static const struct watchdog_info ident = {
181 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
182 .identity = MODULE_NAME,
183};
184
185static long sp805_wdt_ioctl(struct file *file, unsigned int cmd,
186 unsigned long arg)
187{
188 int ret = -ENOTTY;
189 unsigned int timeout;
190
191 switch (cmd) {
192 case WDIOC_GETSUPPORT:
193 ret = copy_to_user((struct watchdog_info *)arg, &ident,
194 sizeof(ident)) ? -EFAULT : 0;
195 break;
196
197 case WDIOC_GETSTATUS:
198 ret = put_user(0, (int *)arg);
199 break;
200
201 case WDIOC_KEEPALIVE:
202 wdt_enable();
203 ret = 0;
204 break;
205
206 case WDIOC_SETTIMEOUT:
207 ret = get_user(timeout, (unsigned int *)arg);
208 if (ret)
209 break;
210
211 wdt_setload(timeout);
212
213 wdt_enable();
214 /* Fall through */
215
216 case WDIOC_GETTIMEOUT:
217 ret = put_user(wdt->timeout, (unsigned int *)arg);
218 break;
219 case WDIOC_GETTIMELEFT:
220 ret = put_user(wdt_timeleft(), (unsigned int *)arg);
221 break;
222 }
223 return ret;
224}
225
226static int sp805_wdt_open(struct inode *inode, struct file *file)
227{
228 int ret = 0;
229
230 if (test_and_set_bit(WDT_BUSY, &wdt->status))
231 return -EBUSY;
232
233 ret = clk_enable(wdt->clk);
234 if (ret) {
235 dev_err(&wdt->adev->dev, "clock enable fail");
236 goto err;
237 }
238
239 wdt_enable();
240
241 /* can not be closed, once enabled */
242 clear_bit(WDT_CAN_BE_CLOSED, &wdt->status);
243 return nonseekable_open(inode, file);
244
245err:
246 clear_bit(WDT_BUSY, &wdt->status);
247 return ret;
248}
249
250static int sp805_wdt_release(struct inode *inode, struct file *file)
251{
252 if (!test_bit(WDT_CAN_BE_CLOSED, &wdt->status)) {
253 clear_bit(WDT_BUSY, &wdt->status);
254 dev_warn(&wdt->adev->dev, "Device closed unexpectedly\n");
255 return 0;
256 }
257
258 wdt_disable();
259 clk_disable(wdt->clk);
260 clear_bit(WDT_BUSY, &wdt->status);
261
262 return 0;
263}
264
265static const struct file_operations sp805_wdt_fops = {
266 .owner = THIS_MODULE,
267 .llseek = no_llseek,
268 .write = sp805_wdt_write,
269 .unlocked_ioctl = sp805_wdt_ioctl,
270 .open = sp805_wdt_open,
271 .release = sp805_wdt_release,
272};
273
274static struct miscdevice sp805_wdt_miscdev = {
275 .minor = WATCHDOG_MINOR,
276 .name = "watchdog",
277 .fops = &sp805_wdt_fops,
278};
279
280static int __devinit
281sp805_wdt_probe(struct amba_device *adev, struct amba_id *id)
282{
283 int ret = 0;
284
285 if (!request_mem_region(adev->res.start, resource_size(&adev->res),
286 "sp805_wdt")) {
287 dev_warn(&adev->dev, "Failed to get memory region resource\n");
288 ret = -ENOENT;
289 goto err;
290 }
291
292 wdt = kzalloc(sizeof(*wdt), GFP_KERNEL);
293 if (!wdt) {
294 dev_warn(&adev->dev, "Kzalloc failed\n");
295 ret = -ENOMEM;
296 goto err_kzalloc;
297 }
298
299 wdt->clk = clk_get(&adev->dev, NULL);
300 if (IS_ERR(wdt->clk)) {
301 dev_warn(&adev->dev, "Clock not found\n");
302 ret = PTR_ERR(wdt->clk);
303 goto err_clk_get;
304 }
305
306 wdt->base = ioremap(adev->res.start, resource_size(&adev->res));
307 if (!wdt->base) {
308 ret = -ENOMEM;
309 dev_warn(&adev->dev, "ioremap fail\n");
310 goto err_ioremap;
311 }
312
313 wdt->adev = adev;
314 spin_lock_init(&wdt->lock);
315 wdt_setload(DEFAULT_TIMEOUT);
316
317 ret = misc_register(&sp805_wdt_miscdev);
318 if (ret < 0) {
319 dev_warn(&adev->dev, "cannot register misc device\n");
320 goto err_misc_register;
321 }
322
323 dev_info(&adev->dev, "registration successful\n");
324 return 0;
325
326err_misc_register:
327 iounmap(wdt->base);
328err_ioremap:
329 clk_put(wdt->clk);
330err_clk_get:
331 kfree(wdt);
332 wdt = NULL;
333err_kzalloc:
334 release_mem_region(adev->res.start, resource_size(&adev->res));
335err:
336 dev_err(&adev->dev, "Probe Failed!!!\n");
337 return ret;
338}
339
340static int __devexit sp805_wdt_remove(struct amba_device *adev)
341{
342 misc_deregister(&sp805_wdt_miscdev);
343 iounmap(wdt->base);
344 clk_put(wdt->clk);
345 kfree(wdt);
346 release_mem_region(adev->res.start, resource_size(&adev->res));
347
348 return 0;
349}
350
351static struct amba_id sp805_wdt_ids[] __initdata = {
352 {
353 .id = 0x00141805,
354 .mask = 0x00ffffff,
355 },
356 { 0, 0 },
357};
358
359static struct amba_driver sp805_wdt_driver = {
360 .drv = {
361 .name = MODULE_NAME,
362 },
363 .id_table = sp805_wdt_ids,
364 .probe = sp805_wdt_probe,
365 .remove = __devexit_p(sp805_wdt_remove),
366};
367
368static int __init sp805_wdt_init(void)
369{
370 return amba_driver_register(&sp805_wdt_driver);
371}
372module_init(sp805_wdt_init);
373
374static void __exit sp805_wdt_exit(void)
375{
376 amba_driver_unregister(&sp805_wdt_driver);
377}
378module_exit(sp805_wdt_exit);
379
380module_param(nowayout, int, 0);
381MODULE_PARM_DESC(nowayout,
382 "Set to 1 to keep watchdog running after device release");
383
384MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");
385MODULE_DESCRIPTION("ARM SP805 Watchdog Driver");
386MODULE_LICENSE("GPL");
387MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
diff --git a/drivers/watchdog/wdt_pci.c b/drivers/watchdog/wdt_pci.c
index 7b22e3cdbc8..6130c88fa5a 100644
--- a/drivers/watchdog/wdt_pci.c
+++ b/drivers/watchdog/wdt_pci.c
@@ -60,19 +60,6 @@
60 60
61#define PFX "wdt_pci: " 61#define PFX "wdt_pci: "
62 62
63/*
64 * Until Access I/O gets their application for a PCI vendor ID approved,
65 * I don't think that it's appropriate to move these constants into the
66 * regular pci_ids.h file. -- JPN 2000/01/18
67 */
68
69#ifndef PCI_VENDOR_ID_ACCESSIO
70#define PCI_VENDOR_ID_ACCESSIO 0x494f
71#endif
72#ifndef PCI_DEVICE_ID_WDG_CSM
73#define PCI_DEVICE_ID_WDG_CSM 0x22c0
74#endif
75
76/* We can only use 1 card due to the /dev/watchdog restriction */ 63/* We can only use 1 card due to the /dev/watchdog restriction */
77static int dev_count; 64static int dev_count;
78 65
@@ -743,7 +730,7 @@ static void __devexit wdtpci_remove_one(struct pci_dev *pdev)
743static struct pci_device_id wdtpci_pci_tbl[] = { 730static struct pci_device_id wdtpci_pci_tbl[] = {
744 { 731 {
745 .vendor = PCI_VENDOR_ID_ACCESSIO, 732 .vendor = PCI_VENDOR_ID_ACCESSIO,
746 .device = PCI_DEVICE_ID_WDG_CSM, 733 .device = PCI_DEVICE_ID_ACCESSIO_WDG_CSM,
747 .subvendor = PCI_ANY_ID, 734 .subvendor = PCI_ANY_ID,
748 .subdevice = PCI_ANY_ID, 735 .subdevice = PCI_ANY_ID,
749 }, 736 },
diff --git a/drivers/xen/xenbus/xenbus_client.c b/drivers/xen/xenbus/xenbus_client.c
index 7b3e973a1ae..7e49527189b 100644
--- a/drivers/xen/xenbus/xenbus_client.c
+++ b/drivers/xen/xenbus/xenbus_client.c
@@ -133,17 +133,12 @@ int xenbus_watch_pathfmt(struct xenbus_device *dev,
133} 133}
134EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt); 134EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
135 135
136static void xenbus_switch_fatal(struct xenbus_device *, int, int,
137 const char *, ...);
136 138
137/** 139static int
138 * xenbus_switch_state 140__xenbus_switch_state(struct xenbus_device *dev,
139 * @dev: xenbus device 141 enum xenbus_state state, int depth)
140 * @state: new state
141 *
142 * Advertise in the store a change of the given driver to the given new_state.
143 * Return 0 on success, or -errno on error. On error, the device will switch
144 * to XenbusStateClosing, and the error will be saved in the store.
145 */
146int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
147{ 142{
148 /* We check whether the state is currently set to the given value, and 143 /* We check whether the state is currently set to the given value, and
149 if not, then the state is set. We don't want to unconditionally 144 if not, then the state is set. We don't want to unconditionally
@@ -152,35 +147,65 @@ int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
152 to it, as the device will be tearing down, and we don't want to 147 to it, as the device will be tearing down, and we don't want to
153 resurrect that directory. 148 resurrect that directory.
154 149
155 Note that, because of this cached value of our state, this function 150 Note that, because of this cached value of our state, this
156 will not work inside a Xenstore transaction (something it was 151 function will not take a caller's Xenstore transaction
157 trying to in the past) because dev->state would not get reset if 152 (something it was trying to in the past) because dev->state
158 the transaction was aborted. 153 would not get reset if the transaction was aborted.
159
160 */ 154 */
161 155
156 struct xenbus_transaction xbt;
162 int current_state; 157 int current_state;
163 int err; 158 int err, abort;
164 159
165 if (state == dev->state) 160 if (state == dev->state)
166 return 0; 161 return 0;
167 162
168 err = xenbus_scanf(XBT_NIL, dev->nodename, "state", "%d", 163again:
169 &current_state); 164 abort = 1;
170 if (err != 1) 165
166 err = xenbus_transaction_start(&xbt);
167 if (err) {
168 xenbus_switch_fatal(dev, depth, err, "starting transaction");
171 return 0; 169 return 0;
170 }
171
172 err = xenbus_scanf(xbt, dev->nodename, "state", "%d", &current_state);
173 if (err != 1)
174 goto abort;
172 175
173 err = xenbus_printf(XBT_NIL, dev->nodename, "state", "%d", state); 176 err = xenbus_printf(xbt, dev->nodename, "state", "%d", state);
174 if (err) { 177 if (err) {
175 if (state != XenbusStateClosing) /* Avoid looping */ 178 xenbus_switch_fatal(dev, depth, err, "writing new state");
176 xenbus_dev_fatal(dev, err, "writing new state"); 179 goto abort;
177 return err;
178 } 180 }
179 181
180 dev->state = state; 182 abort = 0;
183abort:
184 err = xenbus_transaction_end(xbt, abort);
185 if (err) {
186 if (err == -EAGAIN && !abort)
187 goto again;
188 xenbus_switch_fatal(dev, depth, err, "ending transaction");
189 } else
190 dev->state = state;
181 191
182 return 0; 192 return 0;
183} 193}
194
195/**
196 * xenbus_switch_state
197 * @dev: xenbus device
198 * @state: new state
199 *
200 * Advertise in the store a change of the given driver to the given new_state.
201 * Return 0 on success, or -errno on error. On error, the device will switch
202 * to XenbusStateClosing, and the error will be saved in the store.
203 */
204int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
205{
206 return __xenbus_switch_state(dev, state, 0);
207}
208
184EXPORT_SYMBOL_GPL(xenbus_switch_state); 209EXPORT_SYMBOL_GPL(xenbus_switch_state);
185 210
186int xenbus_frontend_closed(struct xenbus_device *dev) 211int xenbus_frontend_closed(struct xenbus_device *dev)
@@ -284,6 +309,23 @@ void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
284EXPORT_SYMBOL_GPL(xenbus_dev_fatal); 309EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
285 310
286/** 311/**
312 * Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps
313 * avoiding recursion within xenbus_switch_state.
314 */
315static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
316 const char *fmt, ...)
317{
318 va_list ap;
319
320 va_start(ap, fmt);
321 xenbus_va_dev_error(dev, err, fmt, ap);
322 va_end(ap);
323
324 if (!depth)
325 __xenbus_switch_state(dev, XenbusStateClosing, 1);
326}
327
328/**
287 * xenbus_grant_ring 329 * xenbus_grant_ring
288 * @dev: xenbus device 330 * @dev: xenbus device
289 * @ring_mfn: mfn of ring to grant 331 * @ring_mfn: mfn of ring to grant
diff --git a/drivers/zorro/proc.c b/drivers/zorro/proc.c
index 3c7046d7965..cafc5045429 100644
--- a/drivers/zorro/proc.c
+++ b/drivers/zorro/proc.c
@@ -22,8 +22,9 @@ static loff_t
22proc_bus_zorro_lseek(struct file *file, loff_t off, int whence) 22proc_bus_zorro_lseek(struct file *file, loff_t off, int whence)
23{ 23{
24 loff_t new = -1; 24 loff_t new = -1;
25 struct inode *inode = file->f_path.dentry->d_inode;
25 26
26 lock_kernel(); 27 mutex_lock(&inode->i_mutex);
27 switch (whence) { 28 switch (whence) {
28 case 0: 29 case 0:
29 new = off; 30 new = off;
@@ -35,12 +36,12 @@ proc_bus_zorro_lseek(struct file *file, loff_t off, int whence)
35 new = sizeof(struct ConfigDev) + off; 36 new = sizeof(struct ConfigDev) + off;
36 break; 37 break;
37 } 38 }
38 if (new < 0 || new > sizeof(struct ConfigDev)) { 39 if (new < 0 || new > sizeof(struct ConfigDev))
39 unlock_kernel(); 40 new = -EINVAL;
40 return -EINVAL; 41 else
41 } 42 file->f_pos = new;
42 unlock_kernel(); 43 mutex_unlock(&inode->i_mutex);
43 return (file->f_pos = new); 44 return new;
44} 45}
45 46
46static ssize_t 47static ssize_t
@@ -67,7 +68,7 @@ proc_bus_zorro_read(struct file *file, char __user *buf, size_t nbytes, loff_t *
67 cd.cd_BoardAddr = (void *)zorro_resource_start(z); 68 cd.cd_BoardAddr = (void *)zorro_resource_start(z);
68 cd.cd_BoardSize = zorro_resource_len(z); 69 cd.cd_BoardSize = zorro_resource_len(z);
69 70
70 if (copy_to_user(buf, &cd, nbytes)) 71 if (copy_to_user(buf, (void *)&cd + pos, nbytes))
71 return -EFAULT; 72 return -EFAULT;
72 *ppos += nbytes; 73 *ppos += nbytes;
73 74