aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
authorDmitry Torokhov <dtor_core@ameritech.net>2006-04-02 00:08:05 -0500
committerDmitry Torokhov <dtor_core@ameritech.net>2006-04-02 00:08:05 -0500
commit95d465fd750897ab32462a6702fbfe1b122cbbc0 (patch)
tree65c38b2f11c51bb6932e44dd6c92f15b0091abfe /drivers/usb
parent642fde17dceceb56c7ba2762733ac688666ae657 (diff)
parent683aa4012f53b2ada0f430487e05d37b0d94e90a (diff)
Manual merge with Linus.
Conflicts: arch/powerpc/kernel/setup-common.c drivers/input/keyboard/hil_kbd.c drivers/input/mouse/hil_ptr.c
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/Kconfig11
-rw-r--r--drivers/usb/Makefile16
-rw-r--r--drivers/usb/class/Kconfig47
-rw-r--r--drivers/usb/class/Makefile2
-rw-r--r--drivers/usb/class/audio.c3869
-rw-r--r--drivers/usb/class/audio.h110
-rw-r--r--drivers/usb/class/cdc-acm.c23
-rw-r--r--drivers/usb/class/usb-midi.c2153
-rw-r--r--drivers/usb/class/usb-midi.h164
-rw-r--r--drivers/usb/class/usblp.c15
-rw-r--r--drivers/usb/core/devices.c7
-rw-r--r--drivers/usb/core/devio.c24
-rw-r--r--drivers/usb/core/driver.c6
-rw-r--r--drivers/usb/core/file.c6
-rw-r--r--drivers/usb/core/hcd-pci.c15
-rw-r--r--drivers/usb/core/hcd.c153
-rw-r--r--drivers/usb/core/hcd.h4
-rw-r--r--drivers/usb/core/hub.c45
-rw-r--r--drivers/usb/core/message.c17
-rw-r--r--drivers/usb/core/notify.c66
-rw-r--r--drivers/usb/core/usb.c5
-rw-r--r--drivers/usb/gadget/Kconfig17
-rw-r--r--drivers/usb/gadget/Makefile1
-rw-r--r--drivers/usb/gadget/at91_udc.c1773
-rw-r--r--drivers/usb/gadget/at91_udc.h181
-rw-r--r--drivers/usb/gadget/dummy_hcd.c3
-rw-r--r--drivers/usb/gadget/ether.c53
-rw-r--r--drivers/usb/gadget/file_storage.c4
-rw-r--r--drivers/usb/gadget/gadget_chips.h30
-rw-r--r--drivers/usb/gadget/goku_udc.c3
-rw-r--r--drivers/usb/gadget/inode.c12
-rw-r--r--drivers/usb/gadget/lh7a40x_udc.c3
-rw-r--r--drivers/usb/gadget/net2280.c3
-rw-r--r--drivers/usb/gadget/omap_udc.c6
-rw-r--r--drivers/usb/gadget/pxa2xx_udc.c3
-rw-r--r--drivers/usb/gadget/serial.c9
-rw-r--r--drivers/usb/gadget/zero.c15
-rw-r--r--drivers/usb/host/Kconfig2
-rw-r--r--drivers/usb/host/ehci-au1xxx.c297
-rw-r--r--drivers/usb/host/ehci-fsl.c366
-rw-r--r--drivers/usb/host/ehci-fsl.h37
-rw-r--r--drivers/usb/host/ehci-hcd.c13
-rw-r--r--drivers/usb/host/ehci-hub.c4
-rw-r--r--drivers/usb/host/ehci-mem.c11
-rw-r--r--drivers/usb/host/ehci-pci.c25
-rw-r--r--drivers/usb/host/ehci-q.c17
-rw-r--r--drivers/usb/host/ehci-sched.c20
-rw-r--r--drivers/usb/host/ehci.h18
-rw-r--r--drivers/usb/host/hc_crisv10.c12
-rw-r--r--drivers/usb/host/isp116x-hcd.c5
-rw-r--r--drivers/usb/host/ohci-at91.c306
-rw-r--r--drivers/usb/host/ohci-au1xxx.c102
-rw-r--r--drivers/usb/host/ohci-hcd.c54
-rw-r--r--drivers/usb/host/ohci-hub.c12
-rw-r--r--drivers/usb/host/ohci-omap.c9
-rw-r--r--drivers/usb/host/ohci-pci.c15
-rw-r--r--drivers/usb/host/ohci-s3c2410.c2
-rw-r--r--drivers/usb/host/sl811-hcd.c3
-rw-r--r--drivers/usb/host/uhci-debug.c356
-rw-r--r--drivers/usb/host/uhci-hcd.c127
-rw-r--r--drivers/usb/host/uhci-hcd.h188
-rw-r--r--drivers/usb/host/uhci-hub.c21
-rw-r--r--drivers/usb/host/uhci-q.c1294
-rw-r--r--drivers/usb/image/mdc800.c67
-rw-r--r--drivers/usb/image/microtek.c2
-rw-r--r--drivers/usb/input/ati_remote.c2
-rw-r--r--drivers/usb/input/hid-core.c177
-rw-r--r--drivers/usb/input/hid-lgff.c6
-rw-r--r--drivers/usb/input/hid-tmff.c3
-rw-r--r--drivers/usb/input/hid.h10
-rw-r--r--drivers/usb/input/hiddev.c6
-rw-r--r--drivers/usb/media/Kconfig226
-rw-r--r--drivers/usb/media/Makefile19
-rw-r--r--drivers/usb/media/dabfirmware.h1408
-rw-r--r--drivers/usb/media/dabusb.c874
-rw-r--r--drivers/usb/media/dabusb.h85
-rw-r--r--drivers/usb/media/dsbr100.c429
-rw-r--r--drivers/usb/media/et61x251.h220
-rw-r--r--drivers/usb/media/et61x251_core.c2605
-rw-r--r--drivers/usb/media/et61x251_sensor.h115
-rw-r--r--drivers/usb/media/et61x251_tas5130d1b.c137
-rw-r--r--drivers/usb/media/ibmcam.c3932
-rw-r--r--drivers/usb/media/konicawc.c978
-rw-r--r--drivers/usb/media/ov511.c5935
-rw-r--r--drivers/usb/media/ov511.h569
-rw-r--r--drivers/usb/media/pwc/Makefile20
-rw-r--r--drivers/usb/media/pwc/philips.txt236
-rw-r--r--drivers/usb/media/pwc/pwc-ctrl.c1542
-rw-r--r--drivers/usb/media/pwc/pwc-if.c2210
-rw-r--r--drivers/usb/media/pwc/pwc-ioctl.h292
-rw-r--r--drivers/usb/media/pwc/pwc-kiara.c318
-rw-r--r--drivers/usb/media/pwc/pwc-kiara.h45
-rw-r--r--drivers/usb/media/pwc/pwc-misc.c140
-rw-r--r--drivers/usb/media/pwc/pwc-nala.h66
-rw-r--r--drivers/usb/media/pwc/pwc-timon.c316
-rw-r--r--drivers/usb/media/pwc/pwc-timon.h61
-rw-r--r--drivers/usb/media/pwc/pwc-uncompress.c146
-rw-r--r--drivers/usb/media/pwc/pwc-uncompress.h41
-rw-r--r--drivers/usb/media/pwc/pwc.h272
-rw-r--r--drivers/usb/media/se401.c1437
-rw-r--r--drivers/usb/media/se401.h233
-rw-r--r--drivers/usb/media/sn9c102.h204
-rw-r--r--drivers/usb/media/sn9c102_core.c2889
-rw-r--r--drivers/usb/media/sn9c102_hv7131d.c271
-rw-r--r--drivers/usb/media/sn9c102_mi0343.c363
-rw-r--r--drivers/usb/media/sn9c102_ov7630.c396
-rw-r--r--drivers/usb/media/sn9c102_pas106b.c307
-rw-r--r--drivers/usb/media/sn9c102_pas202bcb.c293
-rw-r--r--drivers/usb/media/sn9c102_sensor.h388
-rw-r--r--drivers/usb/media/sn9c102_tas5110c1b.c155
-rw-r--r--drivers/usb/media/sn9c102_tas5130d1b.c165
-rw-r--r--drivers/usb/media/stv680.c1510
-rw-r--r--drivers/usb/media/stv680.h227
-rw-r--r--drivers/usb/media/ultracam.c679
-rw-r--r--drivers/usb/media/usbvideo.c2191
-rw-r--r--drivers/usb/media/usbvideo.h393
-rw-r--r--drivers/usb/media/vicam.c1409
-rw-r--r--drivers/usb/media/w9968cf.c3689
-rw-r--r--drivers/usb/media/w9968cf.h338
-rw-r--r--drivers/usb/media/w9968cf_decoder.h86
-rw-r--r--drivers/usb/media/w9968cf_vpp.h40
-rw-r--r--drivers/usb/misc/auerswald.c6
-rw-r--r--drivers/usb/misc/cytherm.c3
-rw-r--r--drivers/usb/misc/idmouse.c28
-rw-r--r--drivers/usb/misc/ldusb.c14
-rw-r--r--drivers/usb/misc/legousbtower.c11
-rw-r--r--drivers/usb/misc/phidgetkit.c9
-rw-r--r--drivers/usb/misc/phidgetservo.c3
-rw-r--r--drivers/usb/misc/sisusbvga/sisusb.c5
-rw-r--r--drivers/usb/misc/sisusbvga/sisusb.h8
-rw-r--r--drivers/usb/misc/usblcd.c3
-rw-r--r--drivers/usb/misc/usbled.c3
-rw-r--r--drivers/usb/misc/usbtest.c9
-rw-r--r--drivers/usb/mon/mon_main.c22
-rw-r--r--drivers/usb/mon/mon_text.c24
-rw-r--r--drivers/usb/mon/usb_mon.h2
-rw-r--r--drivers/usb/net/pegasus.c1
-rw-r--r--drivers/usb/net/pegasus.h26
-rw-r--r--drivers/usb/net/rtl8150.c4
-rw-r--r--drivers/usb/net/zaurus.c2
-rw-r--r--drivers/usb/net/zd1201.c11
-rw-r--r--drivers/usb/serial/Kconfig7
-rw-r--r--drivers/usb/serial/Makefile1
-rw-r--r--drivers/usb/serial/cp2101.c7
-rw-r--r--drivers/usb/serial/cypress_m8.c73
-rw-r--r--drivers/usb/serial/cypress_m8.h5
-rw-r--r--drivers/usb/serial/ftdi_sio.c4
-rw-r--r--drivers/usb/serial/ftdi_sio.h7
-rw-r--r--drivers/usb/serial/garmin_gps.c3
-rw-r--r--drivers/usb/serial/io_edgeport.c3
-rw-r--r--drivers/usb/serial/io_ti.c6
-rw-r--r--drivers/usb/serial/ir-usb.c3
-rw-r--r--drivers/usb/serial/keyspan.c6
-rw-r--r--drivers/usb/serial/kobil_sct.c16
-rw-r--r--drivers/usb/serial/mct_u232.c3
-rw-r--r--drivers/usb/serial/navman.c157
-rw-r--r--drivers/usb/serial/omninet.c10
-rw-r--r--drivers/usb/serial/option.c5
-rw-r--r--drivers/usb/serial/pl2303.c8
-rw-r--r--drivers/usb/serial/pl2303.h4
-rw-r--r--drivers/usb/serial/ti_usb_3410_5052.c3
-rw-r--r--drivers/usb/serial/usb-serial.c6
-rw-r--r--drivers/usb/serial/visor.c3
-rw-r--r--drivers/usb/storage/datafab.c3
-rw-r--r--drivers/usb/storage/isd200.c10
-rw-r--r--drivers/usb/storage/jumpshot.c3
-rw-r--r--drivers/usb/storage/scsiglue.c9
-rw-r--r--drivers/usb/storage/sddr55.c3
-rw-r--r--drivers/usb/storage/shuttle_usbat.c3
-rw-r--r--drivers/usb/storage/unusual_devs.h32
-rw-r--r--drivers/usb/storage/usb.c25
-rw-r--r--drivers/usb/storage/usb.h5
172 files changed, 5016 insertions, 48974 deletions
diff --git a/drivers/usb/Kconfig b/drivers/usb/Kconfig
index 85dacc92545a..7fdbc5dad5fd 100644
--- a/drivers/usb/Kconfig
+++ b/drivers/usb/Kconfig
@@ -10,6 +10,7 @@ menu "USB support"
10config USB_ARCH_HAS_HCD 10config USB_ARCH_HAS_HCD
11 boolean 11 boolean
12 default y if USB_ARCH_HAS_OHCI 12 default y if USB_ARCH_HAS_OHCI
13 default y if USB_ARCH_HAS_EHCI
13 default y if ARM # SL-811 14 default y if ARM # SL-811
14 default PCI 15 default PCI
15 16
@@ -22,6 +23,7 @@ config USB_ARCH_HAS_OHCI
22 default y if ARCH_LH7A404 23 default y if ARCH_LH7A404
23 default y if ARCH_S3C2410 24 default y if ARCH_S3C2410
24 default y if PXA27x 25 default y if PXA27x
26 default y if ARCH_AT91RM9200
25 # PPC: 27 # PPC:
26 default y if STB03xxx 28 default y if STB03xxx
27 default y if PPC_MPC52xx 29 default y if PPC_MPC52xx
@@ -30,6 +32,13 @@ config USB_ARCH_HAS_OHCI
30 # more: 32 # more:
31 default PCI 33 default PCI
32 34
35# some non-PCI hcds implement EHCI
36config USB_ARCH_HAS_EHCI
37 boolean
38 default y if PPC_83xx
39 default y if SOC_AU1200
40 default PCI
41
33# ARM SA1111 chips have a non-PCI based "OHCI-compatible" USB host interface. 42# ARM SA1111 chips have a non-PCI based "OHCI-compatible" USB host interface.
34config USB 43config USB
35 tristate "Support for Host-side USB" 44 tristate "Support for Host-side USB"
@@ -78,8 +87,6 @@ source "drivers/usb/input/Kconfig"
78 87
79source "drivers/usb/image/Kconfig" 88source "drivers/usb/image/Kconfig"
80 89
81source "drivers/usb/media/Kconfig"
82
83source "drivers/usb/net/Kconfig" 90source "drivers/usb/net/Kconfig"
84 91
85source "drivers/usb/mon/Kconfig" 92source "drivers/usb/mon/Kconfig"
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index 36e476dd9123..9b7d9769fdcc 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -15,10 +15,9 @@ obj-$(CONFIG_USB_OHCI_HCD) += host/
15obj-$(CONFIG_USB_UHCI_HCD) += host/ 15obj-$(CONFIG_USB_UHCI_HCD) += host/
16obj-$(CONFIG_USB_SL811_HCD) += host/ 16obj-$(CONFIG_USB_SL811_HCD) += host/
17obj-$(CONFIG_ETRAX_USB_HOST) += host/ 17obj-$(CONFIG_ETRAX_USB_HOST) += host/
18obj-$(CONFIG_USB_OHCI_AT91) += host/
18 19
19obj-$(CONFIG_USB_ACM) += class/ 20obj-$(CONFIG_USB_ACM) += class/
20obj-$(CONFIG_USB_AUDIO) += class/
21obj-$(CONFIG_USB_MIDI) += class/
22obj-$(CONFIG_USB_PRINTER) += class/ 21obj-$(CONFIG_USB_PRINTER) += class/
23 22
24obj-$(CONFIG_USB_STORAGE) += storage/ 23obj-$(CONFIG_USB_STORAGE) += storage/
@@ -36,19 +35,6 @@ obj-$(CONFIG_USB_WACOM) += input/
36obj-$(CONFIG_USB_ACECAD) += input/ 35obj-$(CONFIG_USB_ACECAD) += input/
37obj-$(CONFIG_USB_XPAD) += input/ 36obj-$(CONFIG_USB_XPAD) += input/
38 37
39obj-$(CONFIG_USB_DABUSB) += media/
40obj-$(CONFIG_USB_DSBR) += media/
41obj-$(CONFIG_USB_ET61X251) += media/
42obj-$(CONFIG_USB_IBMCAM) += media/
43obj-$(CONFIG_USB_KONICAWC) += media/
44obj-$(CONFIG_USB_OV511) += media/
45obj-$(CONFIG_USB_PWC) += media/
46obj-$(CONFIG_USB_SE401) += media/
47obj-$(CONFIG_USB_SN9C102) += media/
48obj-$(CONFIG_USB_STV680) += media/
49obj-$(CONFIG_USB_VICAM) += media/
50obj-$(CONFIG_USB_W9968CF) += media/
51
52obj-$(CONFIG_USB_CATC) += net/ 38obj-$(CONFIG_USB_CATC) += net/
53obj-$(CONFIG_USB_KAWETH) += net/ 39obj-$(CONFIG_USB_KAWETH) += net/
54obj-$(CONFIG_USB_PEGASUS) += net/ 40obj-$(CONFIG_USB_PEGASUS) += net/
diff --git a/drivers/usb/class/Kconfig b/drivers/usb/class/Kconfig
index ef105a92a7bd..3a9102d2591b 100644
--- a/drivers/usb/class/Kconfig
+++ b/drivers/usb/class/Kconfig
@@ -4,53 +4,6 @@
4comment "USB Device Class drivers" 4comment "USB Device Class drivers"
5 depends on USB 5 depends on USB
6 6
7config OBSOLETE_OSS_USB_DRIVER
8 bool "Obsolete OSS USB drivers"
9 depends on USB && SOUND
10 help
11 This option enables support for the obsolete USB Audio and Midi
12 drivers that are scheduled for removal in the near future since
13 there are ALSA drivers for the same hardware.
14
15 Please contact Adrian Bunk <bunk@stusta.de> if you had to
16 say Y here because of missing support in the ALSA drivers.
17
18 If unsure, say N.
19
20config USB_AUDIO
21 tristate "USB Audio support"
22 depends on USB && SOUND && OBSOLETE_OSS_USB_DRIVER
23 help
24 Say Y here if you want to connect USB audio equipment such as
25 speakers to your computer's USB port. You only need this if you use
26 the OSS sound driver; ALSA has its own option for usb audio support.
27
28 To compile this driver as a module, choose M here: the
29 module will be called audio.
30
31config USB_MIDI
32 tristate "USB MIDI support"
33 depends on USB && SOUND && OBSOLETE_OSS_USB_DRIVER
34 ---help---
35 Say Y here if you want to connect a USB MIDI device to your
36 computer's USB port. You only need this if you use the OSS
37 sound system; USB MIDI devices are supported by ALSA's USB
38 audio driver. This driver is for devices that comply with
39 'Universal Serial Bus Device Class Definition for MIDI Device'.
40
41 The following devices are known to work:
42 * Steinberg USB2MIDI
43 * Roland MPU64
44 * Roland PC-300
45 * Roland SC8850
46 * Roland UM-1
47 * Roland UM-2
48 * Roland UA-100
49 * Yamaha MU1000
50
51 To compile this driver as a module, choose M here: the
52 module will be called usb-midi.
53
54config USB_ACM 7config USB_ACM
55 tristate "USB Modem (CDC ACM) support" 8 tristate "USB Modem (CDC ACM) support"
56 depends on USB 9 depends on USB
diff --git a/drivers/usb/class/Makefile b/drivers/usb/class/Makefile
index 229471247751..cc391e6c2af8 100644
--- a/drivers/usb/class/Makefile
+++ b/drivers/usb/class/Makefile
@@ -4,6 +4,4 @@
4# 4#
5 5
6obj-$(CONFIG_USB_ACM) += cdc-acm.o 6obj-$(CONFIG_USB_ACM) += cdc-acm.o
7obj-$(CONFIG_USB_AUDIO) += audio.o
8obj-$(CONFIG_USB_MIDI) += usb-midi.o
9obj-$(CONFIG_USB_PRINTER) += usblp.o 7obj-$(CONFIG_USB_PRINTER) += usblp.o
diff --git a/drivers/usb/class/audio.c b/drivers/usb/class/audio.c
deleted file mode 100644
index 3ad9ee8b84a9..000000000000
--- a/drivers/usb/class/audio.c
+++ /dev/null
@@ -1,3869 +0,0 @@
1/*****************************************************************************/
2
3/*
4 * audio.c -- USB Audio Class driver
5 *
6 * Copyright (C) 1999, 2000, 2001, 2003, 2004
7 * Alan Cox (alan@lxorguk.ukuu.org.uk)
8 * Thomas Sailer (sailer@ife.ee.ethz.ch)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * Debugging:
16 * Use the 'lsusb' utility to dump the descriptors.
17 *
18 * 1999-09-07: Alan Cox
19 * Parsing Audio descriptor patch
20 * 1999-09-08: Thomas Sailer
21 * Added OSS compatible data io functions; both parts of the
22 * driver remain to be glued together
23 * 1999-09-10: Thomas Sailer
24 * Beautified the driver. Added sample format conversions.
25 * Still not properly glued with the parsing code.
26 * The parsing code seems to have its problems btw,
27 * Since it parses all available configs but doesn't
28 * store which iface/altsetting belongs to which config.
29 * 1999-09-20: Thomas Sailer
30 * Threw out Alan's parsing code and implemented my own one.
31 * You cannot reasonnably linearly parse audio descriptors,
32 * especially the AudioClass descriptors have to be considered
33 * pointer lists. Mixer parsing untested, due to lack of device.
34 * First stab at synch pipe implementation, the Dallas USB DAC
35 * wants to use an Asynch out pipe. usb_audio_state now basically
36 * only contains lists of mixer and wave devices. We can therefore
37 * now have multiple mixer/wave devices per USB device.
38 * 1999-10-28: Thomas Sailer
39 * Converted to URB API. Fixed a taskstate/wakeup semantics mistake
40 * that made the driver consume all available CPU cycles.
41 * Now runs stable on UHCI-Acher/Fliegl/Sailer.
42 * 1999-10-31: Thomas Sailer
43 * Audio can now be unloaded if it is not in use by any mixer
44 * or dsp client (formerly you had to disconnect the audio devices
45 * from the USB port)
46 * Finally, about three months after ordering, my "Maxxtro SPK222"
47 * speakers arrived, isn't disdata a great mail order company 8-)
48 * Parse class specific endpoint descriptor of the audiostreaming
49 * interfaces and take the endpoint attributes from there.
50 * Unbelievably, the Philips USB DAC has a sampling rate range
51 * of over a decade, yet does not support the sampling rate control!
52 * No wonder it sounds so bad, has very audible sampling rate
53 * conversion distortion. Don't try to listen to it using
54 * decent headphones!
55 * "Let's make things better" -> but please Philips start with your
56 * own stuff!!!!
57 * 1999-11-02: Thomas Sailer
58 * It takes the Philips boxes several seconds to acquire synchronisation
59 * that means they won't play short sounds. Should probably maintain
60 * the ISO datastream even if there's nothing to play.
61 * Fix counting the total_bytes counter, RealPlayer G2 depends on it.
62 * 1999-12-20: Thomas Sailer
63 * Fix bad bug in conversion to per interface probing.
64 * disconnect was called multiple times for the audio device,
65 * leading to a premature freeing of the audio structures
66 * 2000-05-13: Thomas Sailer
67 * I don't remember who changed the find_format routine,
68 * but the change was completely broken for the Dallas
69 * chip. Anyway taking sampling rate into account in find_format
70 * is bad and should not be done unless there are devices with
71 * completely broken audio descriptors. Unless someone shows
72 * me such a descriptor, I will not allow find_format to
73 * take the sampling rate into account.
74 * Also, the former find_format made:
75 * - mpg123 play mono instead of stereo
76 * - sox completely fail for wav's with sample rates < 44.1kHz
77 * for the Dallas chip.
78 * Also fix a rather long standing problem with applications that
79 * use "small" writes producing no sound at all.
80 * 2000-05-15: Thomas Sailer
81 * My fears came true, the Philips camera indeed has pretty stupid
82 * audio descriptors.
83 * 2000-05-17: Thomas Sailer
84 * Nemsoft spotted my stupid last minute change, thanks
85 * 2000-05-19: Thomas Sailer
86 * Fixed FEATURE_UNIT thinkos found thanks to the KC Technology
87 * Xtend device. Basically the driver treated FEATURE_UNIT's sourced
88 * by mono terminals as stereo.
89 * 2000-05-20: Thomas Sailer
90 * SELECTOR support (and thus selecting record channels from the mixer).
91 * Somewhat peculiar due to OSS interface limitations. Only works
92 * for channels where a "slider" is already in front of it (i.e.
93 * a MIXER unit or a FEATURE unit with volume capability).
94 * 2000-11-26: Thomas Sailer
95 * Workaround for Dallas DS4201. The DS4201 uses PCM8 as format tag for
96 * its 8 bit modes, but expects signed data (and should therefore have used PCM).
97 * 2001-03-10: Thomas Sailer
98 * provide abs function, prevent picking up a bogus kernel macro
99 * for abs. Bug report by Andrew Morton <andrewm@uow.edu.au>
100 * 2001-06-16: Bryce Nesbitt <bryce@obviously.com>
101 * Fix SNDCTL_DSP_STEREO API violation
102 * 2003-04-08: Oliver Neukum (oliver@neukum.name):
103 * Setting a configuration is done by usbcore and must not be overridden
104 * 2004-02-27: Workaround for broken synch descriptors
105 * 2004-03-07: Alan Stern <stern@rowland.harvard.edu>
106 * Add usb_ifnum_to_if() and usb_altnum_to_altsetting() support.
107 * Use the in-memory descriptors instead of reading them from the device.
108 *
109 */
110
111/*
112 * Strategy:
113 *
114 * Alan Cox and Thomas Sailer are starting to dig at opposite ends and
115 * are hoping to meet in the middle, just like tunnel diggers :)
116 * Alan tackles the descriptor parsing, Thomas the actual data IO and the
117 * OSS compatible interface.
118 *
119 * Data IO implementation issues
120 *
121 * A mmap'able ring buffer per direction is implemented, because
122 * almost every OSS app expects it. It is however impractical to
123 * transmit/receive USB data directly into and out of the ring buffer,
124 * due to alignment and synchronisation issues. Instead, the ring buffer
125 * feeds a constant time delay line that handles the USB issues.
126 *
127 * Now we first try to find an alternate setting that exactly matches
128 * the sample format requested by the user. If we find one, we do not
129 * need to perform any sample rate conversions. If there is no matching
130 * altsetting, we choose the closest one and perform sample format
131 * conversions. We never do sample rate conversion; these are too
132 * expensive to be performed in the kernel.
133 *
134 * Current status: no known HCD-specific issues.
135 *
136 * Generally: Due to the brokenness of the Audio Class spec
137 * it seems generally impossible to write a generic Audio Class driver,
138 * so a reasonable driver should implement the features that are actually
139 * used.
140 *
141 * Parsing implementation issues
142 *
143 * One cannot reasonably parse the AudioClass descriptors linearly.
144 * Therefore the current implementation features routines to look
145 * for a specific descriptor in the descriptor list.
146 *
147 * How does the parsing work? First, all interfaces are searched
148 * for an AudioControl class interface. If found, the config descriptor
149 * that belongs to the current configuration is searched and
150 * the HEADER descriptor is found. It contains a list of
151 * all AudioStreaming and MIDIStreaming devices. This list is then walked,
152 * and all AudioStreaming interfaces are classified into input and output
153 * interfaces (according to the endpoint0 direction in altsetting1) (MIDIStreaming
154 * is currently not supported). The input & output list is then used
155 * to group inputs and outputs together and issued pairwise to the
156 * AudioStreaming class parser. Finally, all OUTPUT_TERMINAL descriptors
157 * are walked and issued to the mixer construction routine.
158 *
159 * The AudioStreaming parser simply enumerates all altsettings belonging
160 * to the specified interface. It looks for AS_GENERAL and FORMAT_TYPE
161 * class specific descriptors to extract the sample format/sample rate
162 * data. Only sample format types PCM and PCM8 are supported right now, and
163 * only FORMAT_TYPE_I is handled. The isochronous data endpoint needs to
164 * be the first endpoint of the interface, and the optional synchronisation
165 * isochronous endpoint the second one.
166 *
167 * Mixer construction works as follows: The various TERMINAL and UNIT
168 * descriptors span a tree from the root (OUTPUT_TERMINAL) through the
169 * intermediate nodes (UNITs) to the leaves (INPUT_TERMINAL). We walk
170 * that tree in a depth first manner. FEATURE_UNITs may contribute volume,
171 * bass and treble sliders to the mixer, MIXER_UNITs volume sliders.
172 * The terminal type encoded in the INPUT_TERMINALs feeds a heuristic
173 * to determine "meaningful" OSS slider numbers, however we will see
174 * how well this works in practice. Other features are not used at the
175 * moment, they seem less often used. Also, it seems difficult at least
176 * to construct recording source switches from SELECTOR_UNITs, but
177 * since there are not many USB ADC's available, we leave that for later.
178 */
179
180/*****************************************************************************/
181
182#include <linux/kernel.h>
183#include <linux/slab.h>
184#include <linux/string.h>
185#include <linux/timer.h>
186#include <linux/sched.h>
187#include <linux/smp_lock.h>
188#include <linux/module.h>
189#include <linux/sound.h>
190#include <linux/soundcard.h>
191#include <linux/list.h>
192#include <linux/vmalloc.h>
193#include <linux/init.h>
194#include <linux/poll.h>
195#include <linux/bitops.h>
196#include <asm/uaccess.h>
197#include <asm/io.h>
198#include <linux/usb.h>
199
200#include "audio.h"
201
202/*
203 * Version Information
204 */
205#define DRIVER_VERSION "v1.0.0"
206#define DRIVER_AUTHOR "Alan Cox <alan@lxorguk.ukuu.org.uk>, Thomas Sailer (sailer@ife.ee.ethz.ch)"
207#define DRIVER_DESC "USB Audio Class driver"
208
209#define AUDIO_DEBUG 1
210
211#define SND_DEV_DSP16 5
212
213#define dprintk(x)
214
215/* --------------------------------------------------------------------- */
216
217/*
218 * Linked list of all audio devices...
219 */
220static struct list_head audiodevs = LIST_HEAD_INIT(audiodevs);
221static DECLARE_MUTEX(open_sem);
222
223/*
224 * wait queue for processes wanting to open an USB audio device
225 */
226static DECLARE_WAIT_QUEUE_HEAD(open_wait);
227
228
229#define MAXFORMATS MAX_ALT
230#define DMABUFSHIFT 17 /* 128k worth of DMA buffer */
231#define NRSGBUF (1U<<(DMABUFSHIFT-PAGE_SHIFT))
232
233/*
234 * This influences:
235 * - Latency
236 * - Interrupt rate
237 * - Synchronisation behaviour
238 * Don't touch this if you don't understand all of the above.
239 */
240#define DESCFRAMES 5
241#define SYNCFRAMES DESCFRAMES
242
243#define MIXFLG_STEREOIN 1
244#define MIXFLG_STEREOOUT 2
245
246struct mixerchannel {
247 __u16 value;
248 __u16 osschannel; /* number of the OSS channel */
249 __s16 minval, maxval;
250 __u16 slctunitid;
251 __u8 unitid;
252 __u8 selector;
253 __u8 chnum;
254 __u8 flags;
255};
256
257struct audioformat {
258 unsigned int format;
259 unsigned int sratelo;
260 unsigned int sratehi;
261 unsigned char altsetting;
262 unsigned char attributes;
263};
264
265struct dmabuf {
266 /* buffer data format */
267 unsigned int format;
268 unsigned int srate;
269 /* physical buffer */
270 unsigned char *sgbuf[NRSGBUF];
271 unsigned bufsize;
272 unsigned numfrag;
273 unsigned fragshift;
274 unsigned wrptr, rdptr;
275 unsigned total_bytes;
276 int count;
277 unsigned error; /* over/underrun */
278 wait_queue_head_t wait;
279 /* redundant, but makes calculations easier */
280 unsigned fragsize;
281 unsigned dmasize;
282 /* OSS stuff */
283 unsigned mapped:1;
284 unsigned ready:1;
285 unsigned ossfragshift;
286 int ossmaxfrags;
287 unsigned subdivision;
288};
289
290struct usb_audio_state;
291
292#define FLG_URB0RUNNING 1
293#define FLG_URB1RUNNING 2
294#define FLG_SYNC0RUNNING 4
295#define FLG_SYNC1RUNNING 8
296#define FLG_RUNNING 16
297#define FLG_CONNECTED 32
298
299struct my_data_urb {
300 struct urb *urb;
301};
302
303struct my_sync_urb {
304 struct urb *urb;
305};
306
307
308struct usb_audiodev {
309 struct list_head list;
310 struct usb_audio_state *state;
311
312 /* soundcore stuff */
313 int dev_audio;
314
315 /* wave stuff */
316 mode_t open_mode;
317 spinlock_t lock; /* DMA buffer access spinlock */
318
319 struct usbin {
320 int interface; /* Interface number, -1 means not used */
321 unsigned int format; /* USB data format */
322 unsigned int datapipe; /* the data input pipe */
323 unsigned int syncpipe; /* the synchronisation pipe - 0 for anything but adaptive IN mode */
324 unsigned int syncinterval; /* P for adaptive IN mode, 0 otherwise */
325 unsigned int freqn; /* nominal sampling rate in USB format, i.e. fs/1000 in Q10.14 */
326 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
327 unsigned int phase; /* phase accumulator */
328 unsigned int flags; /* see FLG_ defines */
329
330 struct my_data_urb durb[2]; /* ISO descriptors for the data endpoint */
331 struct my_sync_urb surb[2]; /* ISO sync pipe descriptor if needed */
332
333 struct dmabuf dma;
334 } usbin;
335
336 struct usbout {
337 int interface; /* Interface number, -1 means not used */
338 unsigned int format; /* USB data format */
339 unsigned int datapipe; /* the data input pipe */
340 unsigned int syncpipe; /* the synchronisation pipe - 0 for anything but asynchronous OUT mode */
341 unsigned int syncinterval; /* P for asynchronous OUT mode, 0 otherwise */
342 unsigned int freqn; /* nominal sampling rate in USB format, i.e. fs/1000 in Q10.14 */
343 unsigned int freqm; /* momentary sampling rate in USB format, i.e. fs/1000 in Q10.14 */
344 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
345 unsigned int phase; /* phase accumulator */
346 unsigned int flags; /* see FLG_ defines */
347
348 struct my_data_urb durb[2]; /* ISO descriptors for the data endpoint */
349 struct my_sync_urb surb[2]; /* ISO sync pipe descriptor if needed */
350
351 struct dmabuf dma;
352 } usbout;
353
354
355 unsigned int numfmtin, numfmtout;
356 struct audioformat fmtin[MAXFORMATS];
357 struct audioformat fmtout[MAXFORMATS];
358};
359
360struct usb_mixerdev {
361 struct list_head list;
362 struct usb_audio_state *state;
363
364 /* soundcore stuff */
365 int dev_mixer;
366
367 unsigned char iface; /* interface number of the AudioControl interface */
368
369 /* USB format descriptions */
370 unsigned int numch, modcnt;
371
372 /* mixch is last and gets allocated dynamically */
373 struct mixerchannel ch[0];
374};
375
376struct usb_audio_state {
377 struct list_head audiodev;
378
379 /* USB device */
380 struct usb_device *usbdev;
381
382 struct list_head audiolist;
383 struct list_head mixerlist;
384
385 unsigned count; /* usage counter; NOTE: the usb stack is also considered a user */
386};
387
388/* private audio format extensions */
389#define AFMT_STEREO 0x80000000
390#define AFMT_ISSTEREO(x) ((x) & AFMT_STEREO)
391#define AFMT_IS16BIT(x) ((x) & (AFMT_S16_LE|AFMT_S16_BE|AFMT_U16_LE|AFMT_U16_BE))
392#define AFMT_ISUNSIGNED(x) ((x) & (AFMT_U8|AFMT_U16_LE|AFMT_U16_BE))
393#define AFMT_BYTESSHIFT(x) ((AFMT_ISSTEREO(x) ? 1 : 0) + (AFMT_IS16BIT(x) ? 1 : 0))
394#define AFMT_BYTES(x) (1<<AFMT_BYTESSHFIT(x))
395
396/* --------------------------------------------------------------------- */
397
398static inline unsigned ld2(unsigned int x)
399{
400 unsigned r = 0;
401
402 if (x >= 0x10000) {
403 x >>= 16;
404 r += 16;
405 }
406 if (x >= 0x100) {
407 x >>= 8;
408 r += 8;
409 }
410 if (x >= 0x10) {
411 x >>= 4;
412 r += 4;
413 }
414 if (x >= 4) {
415 x >>= 2;
416 r += 2;
417 }
418 if (x >= 2)
419 r++;
420 return r;
421}
422
423/* --------------------------------------------------------------------- */
424
425/*
426 * OSS compatible ring buffer management. The ring buffer may be mmap'ed into
427 * an application address space.
428 *
429 * I first used the rvmalloc stuff copied from bttv. Alan Cox did not like it, so
430 * we now use an array of pointers to a single page each. This saves us the
431 * kernel page table manipulations, but we have to do a page table alike mechanism
432 * (though only one indirection) in software.
433 */
434
435static void dmabuf_release(struct dmabuf *db)
436{
437 unsigned int nr;
438 void *p;
439
440 for(nr = 0; nr < NRSGBUF; nr++) {
441 if (!(p = db->sgbuf[nr]))
442 continue;
443 ClearPageReserved(virt_to_page(p));
444 free_page((unsigned long)p);
445 db->sgbuf[nr] = NULL;
446 }
447 db->mapped = db->ready = 0;
448}
449
450static int dmabuf_init(struct dmabuf *db)
451{
452 unsigned int nr, bytepersec, bufs;
453 void *p;
454
455 /* initialize some fields */
456 db->rdptr = db->wrptr = db->total_bytes = db->count = db->error = 0;
457 /* calculate required buffer size */
458 bytepersec = db->srate << AFMT_BYTESSHIFT(db->format);
459 bufs = 1U << DMABUFSHIFT;
460 if (db->ossfragshift) {
461 if ((1000 << db->ossfragshift) < bytepersec)
462 db->fragshift = ld2(bytepersec/1000);
463 else
464 db->fragshift = db->ossfragshift;
465 } else {
466 db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
467 if (db->fragshift < 3)
468 db->fragshift = 3;
469 }
470 db->numfrag = bufs >> db->fragshift;
471 while (db->numfrag < 4 && db->fragshift > 3) {
472 db->fragshift--;
473 db->numfrag = bufs >> db->fragshift;
474 }
475 db->fragsize = 1 << db->fragshift;
476 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
477 db->numfrag = db->ossmaxfrags;
478 db->dmasize = db->numfrag << db->fragshift;
479 for(nr = 0; nr < NRSGBUF; nr++) {
480 if (!db->sgbuf[nr]) {
481 p = (void *)get_zeroed_page(GFP_KERNEL);
482 if (!p)
483 return -ENOMEM;
484 db->sgbuf[nr] = p;
485 SetPageReserved(virt_to_page(p));
486 }
487 memset(db->sgbuf[nr], AFMT_ISUNSIGNED(db->format) ? 0x80 : 0, PAGE_SIZE);
488 if ((nr << PAGE_SHIFT) >= db->dmasize)
489 break;
490 }
491 db->bufsize = nr << PAGE_SHIFT;
492 db->ready = 1;
493 dprintk((KERN_DEBUG "usbaudio: dmabuf_init bytepersec %d bufs %d ossfragshift %d ossmaxfrags %d "
494 "fragshift %d fragsize %d numfrag %d dmasize %d bufsize %d fmt 0x%x srate %d\n",
495 bytepersec, bufs, db->ossfragshift, db->ossmaxfrags, db->fragshift, db->fragsize,
496 db->numfrag, db->dmasize, db->bufsize, db->format, db->srate));
497 return 0;
498}
499
500static int dmabuf_mmap(struct vm_area_struct *vma, struct dmabuf *db, unsigned long start, unsigned long size, pgprot_t prot)
501{
502 unsigned int nr;
503
504 if (!db->ready || db->mapped || (start | size) & (PAGE_SIZE-1) || size > db->bufsize)
505 return -EINVAL;
506 size >>= PAGE_SHIFT;
507 for(nr = 0; nr < size; nr++)
508 if (!db->sgbuf[nr])
509 return -EINVAL;
510 db->mapped = 1;
511 for(nr = 0; nr < size; nr++) {
512 unsigned long pfn;
513
514 pfn = virt_to_phys(db->sgbuf[nr]) >> PAGE_SHIFT;
515 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, prot))
516 return -EAGAIN;
517 start += PAGE_SIZE;
518 }
519 return 0;
520}
521
522static void dmabuf_copyin(struct dmabuf *db, const void *buffer, unsigned int size)
523{
524 unsigned int pgrem, rem;
525
526 db->total_bytes += size;
527 for (;;) {
528 if (size <= 0)
529 return;
530 pgrem = ((~db->wrptr) & (PAGE_SIZE-1)) + 1;
531 if (pgrem > size)
532 pgrem = size;
533 rem = db->dmasize - db->wrptr;
534 if (pgrem > rem)
535 pgrem = rem;
536 memcpy((db->sgbuf[db->wrptr >> PAGE_SHIFT]) + (db->wrptr & (PAGE_SIZE-1)), buffer, pgrem);
537 size -= pgrem;
538 buffer += pgrem;
539 db->wrptr += pgrem;
540 if (db->wrptr >= db->dmasize)
541 db->wrptr = 0;
542 }
543}
544
545static void dmabuf_copyout(struct dmabuf *db, void *buffer, unsigned int size)
546{
547 unsigned int pgrem, rem;
548
549 db->total_bytes += size;
550 for (;;) {
551 if (size <= 0)
552 return;
553 pgrem = ((~db->rdptr) & (PAGE_SIZE-1)) + 1;
554 if (pgrem > size)
555 pgrem = size;
556 rem = db->dmasize - db->rdptr;
557 if (pgrem > rem)
558 pgrem = rem;
559 memcpy(buffer, (db->sgbuf[db->rdptr >> PAGE_SHIFT]) + (db->rdptr & (PAGE_SIZE-1)), pgrem);
560 size -= pgrem;
561 buffer += pgrem;
562 db->rdptr += pgrem;
563 if (db->rdptr >= db->dmasize)
564 db->rdptr = 0;
565 }
566}
567
568static int dmabuf_copyin_user(struct dmabuf *db, unsigned int ptr, const void __user *buffer, unsigned int size)
569{
570 unsigned int pgrem, rem;
571
572 if (!db->ready || db->mapped)
573 return -EINVAL;
574 for (;;) {
575 if (size <= 0)
576 return 0;
577 pgrem = ((~ptr) & (PAGE_SIZE-1)) + 1;
578 if (pgrem > size)
579 pgrem = size;
580 rem = db->dmasize - ptr;
581 if (pgrem > rem)
582 pgrem = rem;
583 if (copy_from_user((db->sgbuf[ptr >> PAGE_SHIFT]) + (ptr & (PAGE_SIZE-1)), buffer, pgrem))
584 return -EFAULT;
585 size -= pgrem;
586 buffer += pgrem;
587 ptr += pgrem;
588 if (ptr >= db->dmasize)
589 ptr = 0;
590 }
591}
592
593static int dmabuf_copyout_user(struct dmabuf *db, unsigned int ptr, void __user *buffer, unsigned int size)
594{
595 unsigned int pgrem, rem;
596
597 if (!db->ready || db->mapped)
598 return -EINVAL;
599 for (;;) {
600 if (size <= 0)
601 return 0;
602 pgrem = ((~ptr) & (PAGE_SIZE-1)) + 1;
603 if (pgrem > size)
604 pgrem = size;
605 rem = db->dmasize - ptr;
606 if (pgrem > rem)
607 pgrem = rem;
608 if (copy_to_user(buffer, (db->sgbuf[ptr >> PAGE_SHIFT]) + (ptr & (PAGE_SIZE-1)), pgrem))
609 return -EFAULT;
610 size -= pgrem;
611 buffer += pgrem;
612 ptr += pgrem;
613 if (ptr >= db->dmasize)
614 ptr = 0;
615 }
616}
617
618/* --------------------------------------------------------------------- */
619/*
620 * USB I/O code. We do sample format conversion if necessary
621 */
622
623static void usbin_stop(struct usb_audiodev *as)
624{
625 struct usbin *u = &as->usbin;
626 unsigned long flags;
627 unsigned int i, notkilled = 1;
628
629 spin_lock_irqsave(&as->lock, flags);
630 u->flags &= ~FLG_RUNNING;
631 i = u->flags;
632 spin_unlock_irqrestore(&as->lock, flags);
633 while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) {
634 if (notkilled)
635 schedule_timeout_interruptible(1);
636 else
637 schedule_timeout_uninterruptible(1);
638 spin_lock_irqsave(&as->lock, flags);
639 i = u->flags;
640 spin_unlock_irqrestore(&as->lock, flags);
641 if (notkilled && signal_pending(current)) {
642 if (i & FLG_URB0RUNNING)
643 usb_kill_urb(u->durb[0].urb);
644 if (i & FLG_URB1RUNNING)
645 usb_kill_urb(u->durb[1].urb);
646 if (i & FLG_SYNC0RUNNING)
647 usb_kill_urb(u->surb[0].urb);
648 if (i & FLG_SYNC1RUNNING)
649 usb_kill_urb(u->surb[1].urb);
650 notkilled = 0;
651 }
652 }
653 set_current_state(TASK_RUNNING);
654 kfree(u->durb[0].urb->transfer_buffer);
655 kfree(u->durb[1].urb->transfer_buffer);
656 kfree(u->surb[0].urb->transfer_buffer);
657 kfree(u->surb[1].urb->transfer_buffer);
658 u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer =
659 u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL;
660}
661
662static inline void usbin_release(struct usb_audiodev *as)
663{
664 usbin_stop(as);
665}
666
667static void usbin_disc(struct usb_audiodev *as)
668{
669 struct usbin *u = &as->usbin;
670
671 unsigned long flags;
672
673 spin_lock_irqsave(&as->lock, flags);
674 u->flags &= ~(FLG_RUNNING | FLG_CONNECTED);
675 spin_unlock_irqrestore(&as->lock, flags);
676 usbin_stop(as);
677}
678
679static void conversion(const void *ibuf, unsigned int ifmt, void *obuf, unsigned int ofmt, void *tmp, unsigned int scnt)
680{
681 unsigned int cnt, i;
682 __s16 *sp, *sp2, s;
683 unsigned char *bp;
684
685 cnt = scnt;
686 if (AFMT_ISSTEREO(ifmt))
687 cnt <<= 1;
688 sp = ((__s16 *)tmp) + cnt;
689 switch (ifmt & ~AFMT_STEREO) {
690 case AFMT_U8:
691 for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) {
692 bp--;
693 sp--;
694 *sp = (*bp ^ 0x80) << 8;
695 }
696 break;
697
698 case AFMT_S8:
699 for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) {
700 bp--;
701 sp--;
702 *sp = *bp << 8;
703 }
704 break;
705
706 case AFMT_U16_LE:
707 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
708 bp -= 2;
709 sp--;
710 *sp = (bp[0] | (bp[1] << 8)) ^ 0x8000;
711 }
712 break;
713
714 case AFMT_U16_BE:
715 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
716 bp -= 2;
717 sp--;
718 *sp = (bp[1] | (bp[0] << 8)) ^ 0x8000;
719 }
720 break;
721
722 case AFMT_S16_LE:
723 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
724 bp -= 2;
725 sp--;
726 *sp = bp[0] | (bp[1] << 8);
727 }
728 break;
729
730 case AFMT_S16_BE:
731 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
732 bp -= 2;
733 sp--;
734 *sp = bp[1] | (bp[0] << 8);
735 }
736 break;
737 }
738 if (!AFMT_ISSTEREO(ifmt) && AFMT_ISSTEREO(ofmt)) {
739 /* expand from mono to stereo */
740 for (sp = ((__s16 *)tmp)+scnt, sp2 = ((__s16 *)tmp)+2*scnt, i = 0; i < scnt; i++) {
741 sp--;
742 sp2 -= 2;
743 sp2[0] = sp2[1] = sp[0];
744 }
745 }
746 if (AFMT_ISSTEREO(ifmt) && !AFMT_ISSTEREO(ofmt)) {
747 /* contract from stereo to mono */
748 for (sp = sp2 = ((__s16 *)tmp), i = 0; i < scnt; i++, sp++, sp2 += 2)
749 sp[0] = (sp2[0] + sp2[1]) >> 1;
750 }
751 cnt = scnt;
752 if (AFMT_ISSTEREO(ofmt))
753 cnt <<= 1;
754 sp = ((__s16 *)tmp);
755 bp = ((unsigned char *)obuf);
756 switch (ofmt & ~AFMT_STEREO) {
757 case AFMT_U8:
758 for (i = 0; i < cnt; i++, sp++, bp++)
759 *bp = (*sp >> 8) ^ 0x80;
760 break;
761
762 case AFMT_S8:
763 for (i = 0; i < cnt; i++, sp++, bp++)
764 *bp = *sp >> 8;
765 break;
766
767 case AFMT_U16_LE:
768 for (i = 0; i < cnt; i++, sp++, bp += 2) {
769 s = *sp;
770 bp[0] = s;
771 bp[1] = (s >> 8) ^ 0x80;
772 }
773 break;
774
775 case AFMT_U16_BE:
776 for (i = 0; i < cnt; i++, sp++, bp += 2) {
777 s = *sp;
778 bp[1] = s;
779 bp[0] = (s >> 8) ^ 0x80;
780 }
781 break;
782
783 case AFMT_S16_LE:
784 for (i = 0; i < cnt; i++, sp++, bp += 2) {
785 s = *sp;
786 bp[0] = s;
787 bp[1] = s >> 8;
788 }
789 break;
790
791 case AFMT_S16_BE:
792 for (i = 0; i < cnt; i++, sp++, bp += 2) {
793 s = *sp;
794 bp[1] = s;
795 bp[0] = s >> 8;
796 }
797 break;
798 }
799
800}
801
802static void usbin_convert(struct usbin *u, unsigned char *buffer, unsigned int samples)
803{
804 union {
805 __s16 s[64];
806 unsigned char b[0];
807 } tmp;
808 unsigned int scnt, maxs, ufmtsh, dfmtsh;
809
810 ufmtsh = AFMT_BYTESSHIFT(u->format);
811 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
812 maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64;
813 while (samples > 0) {
814 scnt = samples;
815 if (scnt > maxs)
816 scnt = maxs;
817 conversion(buffer, u->format, tmp.b, u->dma.format, tmp.b, scnt);
818 dmabuf_copyin(&u->dma, tmp.b, scnt << dfmtsh);
819 buffer += scnt << ufmtsh;
820 samples -= scnt;
821 }
822}
823
824static int usbin_prepare_desc(struct usbin *u, struct urb *urb)
825{
826 unsigned int i, maxsize, offs;
827
828 maxsize = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
829 //printk(KERN_DEBUG "usbin_prepare_desc: maxsize %d freq 0x%x format 0x%x\n", maxsize, u->freqn, u->format);
830 for (i = offs = 0; i < DESCFRAMES; i++, offs += maxsize) {
831 urb->iso_frame_desc[i].length = maxsize;
832 urb->iso_frame_desc[i].offset = offs;
833 }
834 urb->interval = 1;
835 return 0;
836}
837
838/*
839 * return value: 0 if descriptor should be restarted, -1 otherwise
840 * convert sample format on the fly if necessary
841 */
842static int usbin_retire_desc(struct usbin *u, struct urb *urb)
843{
844 unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, dmafree;
845 unsigned char *cp;
846
847 ufmtsh = AFMT_BYTESSHIFT(u->format);
848 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
849 for (i = 0; i < DESCFRAMES; i++) {
850 cp = ((unsigned char *)urb->transfer_buffer) + urb->iso_frame_desc[i].offset;
851 if (urb->iso_frame_desc[i].status) {
852 dprintk((KERN_DEBUG "usbin_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
853 continue;
854 }
855 scnt = urb->iso_frame_desc[i].actual_length >> ufmtsh;
856 if (!scnt)
857 continue;
858 cnt = scnt << dfmtsh;
859 if (!u->dma.mapped) {
860 dmafree = u->dma.dmasize - u->dma.count;
861 if (cnt > dmafree) {
862 scnt = dmafree >> dfmtsh;
863 cnt = scnt << dfmtsh;
864 err++;
865 }
866 }
867 u->dma.count += cnt;
868 if (u->format == u->dma.format) {
869 /* we do not need format conversion */
870 dprintk((KERN_DEBUG "usbaudio: no sample format conversion\n"));
871 dmabuf_copyin(&u->dma, cp, cnt);
872 } else {
873 /* we need sampling format conversion */
874 dprintk((KERN_DEBUG "usbaudio: sample format conversion %x != %x\n", u->format, u->dma.format));
875 usbin_convert(u, cp, scnt);
876 }
877 }
878 if (err)
879 u->dma.error++;
880 if (u->dma.count >= (signed)u->dma.fragsize)
881 wake_up(&u->dma.wait);
882 return err ? -1 : 0;
883}
884
885static void usbin_completed(struct urb *urb, struct pt_regs *regs)
886{
887 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
888 struct usbin *u = &as->usbin;
889 unsigned long flags;
890 unsigned int mask;
891 int suret = 0;
892
893#if 0
894 printk(KERN_DEBUG "usbin_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
895#endif
896 if (urb == u->durb[0].urb)
897 mask = FLG_URB0RUNNING;
898 else if (urb == u->durb[1].urb)
899 mask = FLG_URB1RUNNING;
900 else {
901 mask = 0;
902 printk(KERN_ERR "usbin_completed: panic: unknown URB\n");
903 }
904 urb->dev = as->state->usbdev;
905 spin_lock_irqsave(&as->lock, flags);
906 if (!usbin_retire_desc(u, urb) &&
907 u->flags & FLG_RUNNING &&
908 !usbin_prepare_desc(u, urb) &&
909 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
910 u->flags |= mask;
911 } else {
912 u->flags &= ~(mask | FLG_RUNNING);
913 wake_up(&u->dma.wait);
914 printk(KERN_DEBUG "usbin_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret);
915 }
916 spin_unlock_irqrestore(&as->lock, flags);
917}
918
919/*
920 * we output sync data
921 */
922static int usbin_sync_prepare_desc(struct usbin *u, struct urb *urb)
923{
924 unsigned char *cp = urb->transfer_buffer;
925 unsigned int i, offs;
926
927 for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3, cp += 3) {
928 urb->iso_frame_desc[i].length = 3;
929 urb->iso_frame_desc[i].offset = offs;
930 cp[0] = u->freqn;
931 cp[1] = u->freqn >> 8;
932 cp[2] = u->freqn >> 16;
933 }
934 urb->interval = 1;
935 return 0;
936}
937
938/*
939 * return value: 0 if descriptor should be restarted, -1 otherwise
940 */
941static int usbin_sync_retire_desc(struct usbin *u, struct urb *urb)
942{
943 unsigned int i;
944
945 for (i = 0; i < SYNCFRAMES; i++)
946 if (urb->iso_frame_desc[0].status)
947 dprintk((KERN_DEBUG "usbin_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
948 return 0;
949}
950
951static void usbin_sync_completed(struct urb *urb, struct pt_regs *regs)
952{
953 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
954 struct usbin *u = &as->usbin;
955 unsigned long flags;
956 unsigned int mask;
957 int suret = 0;
958
959#if 0
960 printk(KERN_DEBUG "usbin_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
961#endif
962 if (urb == u->surb[0].urb)
963 mask = FLG_SYNC0RUNNING;
964 else if (urb == u->surb[1].urb)
965 mask = FLG_SYNC1RUNNING;
966 else {
967 mask = 0;
968 printk(KERN_ERR "usbin_sync_completed: panic: unknown URB\n");
969 }
970 urb->dev = as->state->usbdev;
971 spin_lock_irqsave(&as->lock, flags);
972 if (!usbin_sync_retire_desc(u, urb) &&
973 u->flags & FLG_RUNNING &&
974 !usbin_sync_prepare_desc(u, urb) &&
975 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
976 u->flags |= mask;
977 } else {
978 u->flags &= ~(mask | FLG_RUNNING);
979 wake_up(&u->dma.wait);
980 dprintk((KERN_DEBUG "usbin_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
981 }
982 spin_unlock_irqrestore(&as->lock, flags);
983}
984
985static int usbin_start(struct usb_audiodev *as)
986{
987 struct usb_device *dev = as->state->usbdev;
988 struct usbin *u = &as->usbin;
989 struct urb *urb;
990 unsigned long flags;
991 unsigned int maxsze, bufsz;
992
993#if 0
994 printk(KERN_DEBUG "usbin_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n",
995 dev->devnum, u->format, u->dma.format, u->dma.srate);
996#endif
997 /* allocate USB storage if not already done */
998 spin_lock_irqsave(&as->lock, flags);
999 if (!(u->flags & FLG_CONNECTED)) {
1000 spin_unlock_irqrestore(&as->lock, flags);
1001 return -EIO;
1002 }
1003 if (!(u->flags & FLG_RUNNING)) {
1004 spin_unlock_irqrestore(&as->lock, flags);
1005 u->freqn = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */
1006 u->freqmax = u->freqn + (u->freqn >> 2);
1007 u->phase = 0;
1008 maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
1009 bufsz = DESCFRAMES * maxsze;
1010 kfree(u->durb[0].urb->transfer_buffer);
1011 u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1012 u->durb[0].urb->transfer_buffer_length = bufsz;
1013 kfree(u->durb[1].urb->transfer_buffer);
1014 u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1015 u->durb[1].urb->transfer_buffer_length = bufsz;
1016 if (u->syncpipe) {
1017 kfree(u->surb[0].urb->transfer_buffer);
1018 u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1019 u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES;
1020 kfree(u->surb[1].urb->transfer_buffer);
1021 u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1022 u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES;
1023 }
1024 if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer ||
1025 (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) {
1026 printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum);
1027 return 0;
1028 }
1029 spin_lock_irqsave(&as->lock, flags);
1030 }
1031 if (u->dma.count >= u->dma.dmasize && !u->dma.mapped) {
1032 spin_unlock_irqrestore(&as->lock, flags);
1033 return 0;
1034 }
1035 u->flags |= FLG_RUNNING;
1036 if (!(u->flags & FLG_URB0RUNNING)) {
1037 urb = u->durb[0].urb;
1038 urb->dev = dev;
1039 urb->pipe = u->datapipe;
1040 urb->transfer_flags = URB_ISO_ASAP;
1041 urb->number_of_packets = DESCFRAMES;
1042 urb->context = as;
1043 urb->complete = usbin_completed;
1044 if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1045 u->flags |= FLG_URB0RUNNING;
1046 else
1047 u->flags &= ~FLG_RUNNING;
1048 }
1049 if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) {
1050 urb = u->durb[1].urb;
1051 urb->dev = dev;
1052 urb->pipe = u->datapipe;
1053 urb->transfer_flags = URB_ISO_ASAP;
1054 urb->number_of_packets = DESCFRAMES;
1055 urb->context = as;
1056 urb->complete = usbin_completed;
1057 if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1058 u->flags |= FLG_URB1RUNNING;
1059 else
1060 u->flags &= ~FLG_RUNNING;
1061 }
1062 if (u->syncpipe) {
1063 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) {
1064 urb = u->surb[0].urb;
1065 urb->dev = dev;
1066 urb->pipe = u->syncpipe;
1067 urb->transfer_flags = URB_ISO_ASAP;
1068 urb->number_of_packets = SYNCFRAMES;
1069 urb->context = as;
1070 urb->complete = usbin_sync_completed;
1071 /* stride: u->syncinterval */
1072 if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1073 u->flags |= FLG_SYNC0RUNNING;
1074 else
1075 u->flags &= ~FLG_RUNNING;
1076 }
1077 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) {
1078 urb = u->surb[1].urb;
1079 urb->dev = dev;
1080 urb->pipe = u->syncpipe;
1081 urb->transfer_flags = URB_ISO_ASAP;
1082 urb->number_of_packets = SYNCFRAMES;
1083 urb->context = as;
1084 urb->complete = usbin_sync_completed;
1085 /* stride: u->syncinterval */
1086 if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1087 u->flags |= FLG_SYNC1RUNNING;
1088 else
1089 u->flags &= ~FLG_RUNNING;
1090 }
1091 }
1092 spin_unlock_irqrestore(&as->lock, flags);
1093 return 0;
1094}
1095
1096static void usbout_stop(struct usb_audiodev *as)
1097{
1098 struct usbout *u = &as->usbout;
1099 unsigned long flags;
1100 unsigned int i, notkilled = 1;
1101
1102 spin_lock_irqsave(&as->lock, flags);
1103 u->flags &= ~FLG_RUNNING;
1104 i = u->flags;
1105 spin_unlock_irqrestore(&as->lock, flags);
1106 while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) {
1107 if (notkilled)
1108 schedule_timeout_interruptible(1);
1109 else
1110 schedule_timeout_uninterruptible(1);
1111 spin_lock_irqsave(&as->lock, flags);
1112 i = u->flags;
1113 spin_unlock_irqrestore(&as->lock, flags);
1114 if (notkilled && signal_pending(current)) {
1115 if (i & FLG_URB0RUNNING)
1116 usb_kill_urb(u->durb[0].urb);
1117 if (i & FLG_URB1RUNNING)
1118 usb_kill_urb(u->durb[1].urb);
1119 if (i & FLG_SYNC0RUNNING)
1120 usb_kill_urb(u->surb[0].urb);
1121 if (i & FLG_SYNC1RUNNING)
1122 usb_kill_urb(u->surb[1].urb);
1123 notkilled = 0;
1124 }
1125 }
1126 set_current_state(TASK_RUNNING);
1127 kfree(u->durb[0].urb->transfer_buffer);
1128 kfree(u->durb[1].urb->transfer_buffer);
1129 kfree(u->surb[0].urb->transfer_buffer);
1130 kfree(u->surb[1].urb->transfer_buffer);
1131 u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer =
1132 u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL;
1133}
1134
1135static inline void usbout_release(struct usb_audiodev *as)
1136{
1137 usbout_stop(as);
1138}
1139
1140static void usbout_disc(struct usb_audiodev *as)
1141{
1142 struct usbout *u = &as->usbout;
1143 unsigned long flags;
1144
1145 spin_lock_irqsave(&as->lock, flags);
1146 u->flags &= ~(FLG_RUNNING | FLG_CONNECTED);
1147 spin_unlock_irqrestore(&as->lock, flags);
1148 usbout_stop(as);
1149}
1150
1151static void usbout_convert(struct usbout *u, unsigned char *buffer, unsigned int samples)
1152{
1153 union {
1154 __s16 s[64];
1155 unsigned char b[0];
1156 } tmp;
1157 unsigned int scnt, maxs, ufmtsh, dfmtsh;
1158
1159 ufmtsh = AFMT_BYTESSHIFT(u->format);
1160 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
1161 maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64;
1162 while (samples > 0) {
1163 scnt = samples;
1164 if (scnt > maxs)
1165 scnt = maxs;
1166 dmabuf_copyout(&u->dma, tmp.b, scnt << dfmtsh);
1167 conversion(tmp.b, u->dma.format, buffer, u->format, tmp.b, scnt);
1168 buffer += scnt << ufmtsh;
1169 samples -= scnt;
1170 }
1171}
1172
1173static int usbout_prepare_desc(struct usbout *u, struct urb *urb)
1174{
1175 unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, offs;
1176 unsigned char *cp = urb->transfer_buffer;
1177
1178 ufmtsh = AFMT_BYTESSHIFT(u->format);
1179 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
1180 for (i = offs = 0; i < DESCFRAMES; i++) {
1181 urb->iso_frame_desc[i].offset = offs;
1182 u->phase = (u->phase & 0x3fff) + u->freqm;
1183 scnt = u->phase >> 14;
1184 if (!scnt) {
1185 urb->iso_frame_desc[i].length = 0;
1186 continue;
1187 }
1188 cnt = scnt << dfmtsh;
1189 if (!u->dma.mapped) {
1190 if (cnt > u->dma.count) {
1191 scnt = u->dma.count >> dfmtsh;
1192 cnt = scnt << dfmtsh;
1193 err++;
1194 }
1195 u->dma.count -= cnt;
1196 } else
1197 u->dma.count += cnt;
1198 if (u->format == u->dma.format) {
1199 /* we do not need format conversion */
1200 dmabuf_copyout(&u->dma, cp, cnt);
1201 } else {
1202 /* we need sampling format conversion */
1203 usbout_convert(u, cp, scnt);
1204 }
1205 cnt = scnt << ufmtsh;
1206 urb->iso_frame_desc[i].length = cnt;
1207 offs += cnt;
1208 cp += cnt;
1209 }
1210 urb->interval = 1;
1211 if (err)
1212 u->dma.error++;
1213 if (u->dma.mapped) {
1214 if (u->dma.count >= (signed)u->dma.fragsize)
1215 wake_up(&u->dma.wait);
1216 } else {
1217 if ((signed)u->dma.dmasize >= u->dma.count + (signed)u->dma.fragsize)
1218 wake_up(&u->dma.wait);
1219 }
1220 return err ? -1 : 0;
1221}
1222
1223/*
1224 * return value: 0 if descriptor should be restarted, -1 otherwise
1225 */
1226static int usbout_retire_desc(struct usbout *u, struct urb *urb)
1227{
1228 unsigned int i;
1229
1230 for (i = 0; i < DESCFRAMES; i++) {
1231 if (urb->iso_frame_desc[i].status) {
1232 dprintk((KERN_DEBUG "usbout_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
1233 continue;
1234 }
1235 }
1236 return 0;
1237}
1238
1239static void usbout_completed(struct urb *urb, struct pt_regs *regs)
1240{
1241 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
1242 struct usbout *u = &as->usbout;
1243 unsigned long flags;
1244 unsigned int mask;
1245 int suret = 0;
1246
1247#if 0
1248 printk(KERN_DEBUG "usbout_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
1249#endif
1250 if (urb == u->durb[0].urb)
1251 mask = FLG_URB0RUNNING;
1252 else if (urb == u->durb[1].urb)
1253 mask = FLG_URB1RUNNING;
1254 else {
1255 mask = 0;
1256 printk(KERN_ERR "usbout_completed: panic: unknown URB\n");
1257 }
1258 urb->dev = as->state->usbdev;
1259 spin_lock_irqsave(&as->lock, flags);
1260 if (!usbout_retire_desc(u, urb) &&
1261 u->flags & FLG_RUNNING &&
1262 !usbout_prepare_desc(u, urb) &&
1263 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
1264 u->flags |= mask;
1265 } else {
1266 u->flags &= ~(mask | FLG_RUNNING);
1267 wake_up(&u->dma.wait);
1268 dprintk((KERN_DEBUG "usbout_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
1269 }
1270 spin_unlock_irqrestore(&as->lock, flags);
1271}
1272
1273static int usbout_sync_prepare_desc(struct usbout *u, struct urb *urb)
1274{
1275 unsigned int i, offs;
1276
1277 for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3) {
1278 urb->iso_frame_desc[i].length = 3;
1279 urb->iso_frame_desc[i].offset = offs;
1280 }
1281 urb->interval = 1;
1282 return 0;
1283}
1284
1285/*
1286 * return value: 0 if descriptor should be restarted, -1 otherwise
1287 */
1288static int usbout_sync_retire_desc(struct usbout *u, struct urb *urb)
1289{
1290 unsigned char *cp = urb->transfer_buffer;
1291 unsigned int f, i;
1292
1293 for (i = 0; i < SYNCFRAMES; i++, cp += 3) {
1294 if (urb->iso_frame_desc[i].status) {
1295 dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
1296 continue;
1297 }
1298 if (urb->iso_frame_desc[i].actual_length < 3) {
1299 dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u length %d\n", i, urb->iso_frame_desc[i].actual_length));
1300 continue;
1301 }
1302 f = cp[0] | (cp[1] << 8) | (cp[2] << 16);
1303 if (abs(f - u->freqn) > (u->freqn >> 3) || f > u->freqmax) {
1304 printk(KERN_WARNING "usbout_sync_retire_desc: requested frequency %u (nominal %u) out of range!\n", f, u->freqn);
1305 continue;
1306 }
1307 u->freqm = f;
1308 }
1309 return 0;
1310}
1311
1312static void usbout_sync_completed(struct urb *urb, struct pt_regs *regs)
1313{
1314 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
1315 struct usbout *u = &as->usbout;
1316 unsigned long flags;
1317 unsigned int mask;
1318 int suret = 0;
1319
1320#if 0
1321 printk(KERN_DEBUG "usbout_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
1322#endif
1323 if (urb == u->surb[0].urb)
1324 mask = FLG_SYNC0RUNNING;
1325 else if (urb == u->surb[1].urb)
1326 mask = FLG_SYNC1RUNNING;
1327 else {
1328 mask = 0;
1329 printk(KERN_ERR "usbout_sync_completed: panic: unknown URB\n");
1330 }
1331 urb->dev = as->state->usbdev;
1332 spin_lock_irqsave(&as->lock, flags);
1333 if (!usbout_sync_retire_desc(u, urb) &&
1334 u->flags & FLG_RUNNING &&
1335 !usbout_sync_prepare_desc(u, urb) &&
1336 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
1337 u->flags |= mask;
1338 } else {
1339 u->flags &= ~(mask | FLG_RUNNING);
1340 wake_up(&u->dma.wait);
1341 dprintk((KERN_DEBUG "usbout_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
1342 }
1343 spin_unlock_irqrestore(&as->lock, flags);
1344}
1345
1346static int usbout_start(struct usb_audiodev *as)
1347{
1348 struct usb_device *dev = as->state->usbdev;
1349 struct usbout *u = &as->usbout;
1350 struct urb *urb;
1351 unsigned long flags;
1352 unsigned int maxsze, bufsz;
1353
1354#if 0
1355 printk(KERN_DEBUG "usbout_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n",
1356 dev->devnum, u->format, u->dma.format, u->dma.srate);
1357#endif
1358 /* allocate USB storage if not already done */
1359 spin_lock_irqsave(&as->lock, flags);
1360 if (!(u->flags & FLG_CONNECTED)) {
1361 spin_unlock_irqrestore(&as->lock, flags);
1362 return -EIO;
1363 }
1364 if (!(u->flags & FLG_RUNNING)) {
1365 spin_unlock_irqrestore(&as->lock, flags);
1366 u->freqn = u->freqm = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */
1367 u->freqmax = u->freqn + (u->freqn >> 2);
1368 u->phase = 0;
1369 maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
1370 bufsz = DESCFRAMES * maxsze;
1371 kfree(u->durb[0].urb->transfer_buffer);
1372 u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1373 u->durb[0].urb->transfer_buffer_length = bufsz;
1374 kfree(u->durb[1].urb->transfer_buffer);
1375 u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1376 u->durb[1].urb->transfer_buffer_length = bufsz;
1377 if (u->syncpipe) {
1378 kfree(u->surb[0].urb->transfer_buffer);
1379 u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1380 u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES;
1381 kfree(u->surb[1].urb->transfer_buffer);
1382 u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1383 u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES;
1384 }
1385 if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer ||
1386 (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) {
1387 printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum);
1388 return 0;
1389 }
1390 spin_lock_irqsave(&as->lock, flags);
1391 }
1392 if (u->dma.count <= 0 && !u->dma.mapped) {
1393 spin_unlock_irqrestore(&as->lock, flags);
1394 return 0;
1395 }
1396 u->flags |= FLG_RUNNING;
1397 if (!(u->flags & FLG_URB0RUNNING)) {
1398 urb = u->durb[0].urb;
1399 urb->dev = dev;
1400 urb->pipe = u->datapipe;
1401 urb->transfer_flags = URB_ISO_ASAP;
1402 urb->number_of_packets = DESCFRAMES;
1403 urb->context = as;
1404 urb->complete = usbout_completed;
1405 if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1406 u->flags |= FLG_URB0RUNNING;
1407 else
1408 u->flags &= ~FLG_RUNNING;
1409 }
1410 if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) {
1411 urb = u->durb[1].urb;
1412 urb->dev = dev;
1413 urb->pipe = u->datapipe;
1414 urb->transfer_flags = URB_ISO_ASAP;
1415 urb->number_of_packets = DESCFRAMES;
1416 urb->context = as;
1417 urb->complete = usbout_completed;
1418 if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1419 u->flags |= FLG_URB1RUNNING;
1420 else
1421 u->flags &= ~FLG_RUNNING;
1422 }
1423 if (u->syncpipe) {
1424 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) {
1425 urb = u->surb[0].urb;
1426 urb->dev = dev;
1427 urb->pipe = u->syncpipe;
1428 urb->transfer_flags = URB_ISO_ASAP;
1429 urb->number_of_packets = SYNCFRAMES;
1430 urb->context = as;
1431 urb->complete = usbout_sync_completed;
1432 /* stride: u->syncinterval */
1433 if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1434 u->flags |= FLG_SYNC0RUNNING;
1435 else
1436 u->flags &= ~FLG_RUNNING;
1437 }
1438 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) {
1439 urb = u->surb[1].urb;
1440 urb->dev = dev;
1441 urb->pipe = u->syncpipe;
1442 urb->transfer_flags = URB_ISO_ASAP;
1443 urb->number_of_packets = SYNCFRAMES;
1444 urb->context = as;
1445 urb->complete = usbout_sync_completed;
1446 /* stride: u->syncinterval */
1447 if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1448 u->flags |= FLG_SYNC1RUNNING;
1449 else
1450 u->flags &= ~FLG_RUNNING;
1451 }
1452 }
1453 spin_unlock_irqrestore(&as->lock, flags);
1454 return 0;
1455}
1456
1457/* --------------------------------------------------------------------- */
1458
1459static unsigned int format_goodness(struct audioformat *afp, unsigned int fmt, unsigned int srate)
1460{
1461 unsigned int g = 0;
1462
1463 if (srate < afp->sratelo)
1464 g += afp->sratelo - srate;
1465 if (srate > afp->sratehi)
1466 g += srate - afp->sratehi;
1467 if (AFMT_ISSTEREO(afp->format) && !AFMT_ISSTEREO(fmt))
1468 g += 0x100000;
1469 if (!AFMT_ISSTEREO(afp->format) && AFMT_ISSTEREO(fmt))
1470 g += 0x400000;
1471 if (AFMT_IS16BIT(afp->format) && !AFMT_IS16BIT(fmt))
1472 g += 0x100000;
1473 if (!AFMT_IS16BIT(afp->format) && AFMT_IS16BIT(fmt))
1474 g += 0x400000;
1475 return g;
1476}
1477
1478static int find_format(struct audioformat *afp, unsigned int nr, unsigned int fmt, unsigned int srate)
1479{
1480 unsigned int i, g, gb = ~0;
1481 int j = -1; /* default to failure */
1482
1483 /* find "best" format (according to format_goodness) */
1484 for (i = 0; i < nr; i++) {
1485 g = format_goodness(&afp[i], fmt, srate);
1486 if (g >= gb)
1487 continue;
1488 j = i;
1489 gb = g;
1490 }
1491 return j;
1492}
1493
1494static int set_format_in(struct usb_audiodev *as)
1495{
1496 struct usb_device *dev = as->state->usbdev;
1497 struct usb_host_interface *alts;
1498 struct usb_interface *iface;
1499 struct usbin *u = &as->usbin;
1500 struct dmabuf *d = &u->dma;
1501 struct audioformat *fmt;
1502 unsigned int ep;
1503 unsigned char data[3];
1504 int fmtnr, ret;
1505
1506 iface = usb_ifnum_to_if(dev, u->interface);
1507 if (!iface)
1508 return 0;
1509
1510 fmtnr = find_format(as->fmtin, as->numfmtin, d->format, d->srate);
1511 if (fmtnr < 0) {
1512 printk(KERN_ERR "usbaudio: set_format_in(): failed to find desired format/speed combination.\n");
1513 return -1;
1514 }
1515
1516 fmt = as->fmtin + fmtnr;
1517 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
1518 u->format = fmt->format;
1519 u->datapipe = usb_rcvisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf);
1520 u->syncpipe = u->syncinterval = 0;
1521 if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x08) {
1522 if (alts->desc.bNumEndpoints < 2 ||
1523 alts->endpoint[1].desc.bmAttributes != 0x01 ||
1524 alts->endpoint[1].desc.bSynchAddress != 0 ||
1525 alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress & 0x7f)) {
1526 printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims adaptive in "
1527 "but has invalid synch pipe; treating as asynchronous in\n",
1528 dev->devnum, u->interface, fmt->altsetting);
1529 } else {
1530 u->syncpipe = usb_sndisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf);
1531 u->syncinterval = alts->endpoint[1].desc.bRefresh;
1532 }
1533 }
1534 if (d->srate < fmt->sratelo)
1535 d->srate = fmt->sratelo;
1536 if (d->srate > fmt->sratehi)
1537 d->srate = fmt->sratehi;
1538 dprintk((KERN_DEBUG "usbaudio: set_format_in: usb_set_interface %u %u\n",
1539 u->interface, fmt->altsetting));
1540 if (usb_set_interface(dev, alts->desc.bInterfaceNumber, fmt->altsetting) < 0) {
1541 printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n",
1542 dev->devnum, u->interface, fmt->altsetting);
1543 return -1;
1544 }
1545 if (fmt->sratelo == fmt->sratehi)
1546 return 0;
1547 ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN);
1548 /* if endpoint has pitch control, enable it */
1549 if (fmt->attributes & 0x02) {
1550 data[0] = 1;
1551 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1552 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1553 printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n",
1554 ret, dev->devnum, u->interface, ep, d->srate);
1555 return -1;
1556 }
1557 }
1558 /* if endpoint has sampling rate control, set it */
1559 if (fmt->attributes & 0x01) {
1560 data[0] = d->srate;
1561 data[1] = d->srate >> 8;
1562 data[2] = d->srate >> 16;
1563 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1564 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1565 printk(KERN_ERR "usbaudio: failure (error %d) to set input sampling frequency device %d interface %u endpoint 0x%x to %u\n",
1566 ret, dev->devnum, u->interface, ep, d->srate);
1567 return -1;
1568 }
1569 if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1570 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1571 printk(KERN_ERR "usbaudio: failure (error %d) to get input sampling frequency device %d interface %u endpoint 0x%x\n",
1572 ret, dev->devnum, u->interface, ep);
1573 return -1;
1574 }
1575 dprintk((KERN_DEBUG "usbaudio: set_format_in: device %d interface %d altsetting %d srate req: %u real %u\n",
1576 dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16)));
1577 d->srate = data[0] | (data[1] << 8) | (data[2] << 16);
1578 }
1579 dprintk((KERN_DEBUG "usbaudio: set_format_in: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate));
1580 return 0;
1581}
1582
1583static int set_format_out(struct usb_audiodev *as)
1584{
1585 struct usb_device *dev = as->state->usbdev;
1586 struct usb_host_interface *alts;
1587 struct usb_interface *iface;
1588 struct usbout *u = &as->usbout;
1589 struct dmabuf *d = &u->dma;
1590 struct audioformat *fmt;
1591 unsigned int ep;
1592 unsigned char data[3];
1593 int fmtnr, ret;
1594
1595 iface = usb_ifnum_to_if(dev, u->interface);
1596 if (!iface)
1597 return 0;
1598
1599 fmtnr = find_format(as->fmtout, as->numfmtout, d->format, d->srate);
1600 if (fmtnr < 0) {
1601 printk(KERN_ERR "usbaudio: set_format_out(): failed to find desired format/speed combination.\n");
1602 return -1;
1603 }
1604
1605 fmt = as->fmtout + fmtnr;
1606 u->format = fmt->format;
1607 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
1608 u->datapipe = usb_sndisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf);
1609 u->syncpipe = u->syncinterval = 0;
1610 if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x04) {
1611#if 0
1612 printk(KERN_DEBUG "bNumEndpoints 0x%02x endpoint[1].bmAttributes 0x%02x\n"
1613 KERN_DEBUG "endpoint[1].bSynchAddress 0x%02x endpoint[1].bEndpointAddress 0x%02x\n"
1614 KERN_DEBUG "endpoint[0].bSynchAddress 0x%02x\n", alts->bNumEndpoints,
1615 alts->endpoint[1].bmAttributes, alts->endpoint[1].bSynchAddress,
1616 alts->endpoint[1].bEndpointAddress, alts->endpoint[0].bSynchAddress);
1617#endif
1618 if (alts->desc.bNumEndpoints < 2 ||
1619 alts->endpoint[1].desc.bmAttributes != 0x01 ||
1620 alts->endpoint[1].desc.bSynchAddress != 0 ||
1621 alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress | 0x80)) {
1622 printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims asynch out "
1623 "but has invalid synch pipe; treating as adaptive out\n",
1624 dev->devnum, u->interface, fmt->altsetting);
1625 } else {
1626 u->syncpipe = usb_rcvisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf);
1627 u->syncinterval = alts->endpoint[1].desc.bRefresh;
1628 }
1629 }
1630 if (d->srate < fmt->sratelo)
1631 d->srate = fmt->sratelo;
1632 if (d->srate > fmt->sratehi)
1633 d->srate = fmt->sratehi;
1634 dprintk((KERN_DEBUG "usbaudio: set_format_out: usb_set_interface %u %u\n",
1635 u->interface, fmt->altsetting));
1636 if (usb_set_interface(dev, u->interface, fmt->altsetting) < 0) {
1637 printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n",
1638 dev->devnum, u->interface, fmt->altsetting);
1639 return -1;
1640 }
1641 if (fmt->sratelo == fmt->sratehi)
1642 return 0;
1643 ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN);
1644 /* if endpoint has pitch control, enable it */
1645 if (fmt->attributes & 0x02) {
1646 data[0] = 1;
1647 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1648 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1649 printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n",
1650 ret, dev->devnum, u->interface, ep, d->srate);
1651 return -1;
1652 }
1653 }
1654 /* if endpoint has sampling rate control, set it */
1655 if (fmt->attributes & 0x01) {
1656 data[0] = d->srate;
1657 data[1] = d->srate >> 8;
1658 data[2] = d->srate >> 16;
1659 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1660 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1661 printk(KERN_ERR "usbaudio: failure (error %d) to set output sampling frequency device %d interface %u endpoint 0x%x to %u\n",
1662 ret, dev->devnum, u->interface, ep, d->srate);
1663 return -1;
1664 }
1665 if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1666 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1667 printk(KERN_ERR "usbaudio: failure (error %d) to get output sampling frequency device %d interface %u endpoint 0x%x\n",
1668 ret, dev->devnum, u->interface, ep);
1669 return -1;
1670 }
1671 dprintk((KERN_DEBUG "usbaudio: set_format_out: device %d interface %d altsetting %d srate req: %u real %u\n",
1672 dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16)));
1673 d->srate = data[0] | (data[1] << 8) | (data[2] << 16);
1674 }
1675 dprintk((KERN_DEBUG "usbaudio: set_format_out: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate));
1676 return 0;
1677}
1678
1679static int set_format(struct usb_audiodev *s, unsigned int fmode, unsigned int fmt, unsigned int srate)
1680{
1681 int ret1 = 0, ret2 = 0;
1682
1683 if (!(fmode & (FMODE_READ|FMODE_WRITE)))
1684 return -EINVAL;
1685 if (fmode & FMODE_READ) {
1686 usbin_stop(s);
1687 s->usbin.dma.ready = 0;
1688 if (fmt == AFMT_QUERY)
1689 fmt = s->usbin.dma.format;
1690 else
1691 s->usbin.dma.format = fmt;
1692 if (!srate)
1693 srate = s->usbin.dma.srate;
1694 else
1695 s->usbin.dma.srate = srate;
1696 }
1697 if (fmode & FMODE_WRITE) {
1698 usbout_stop(s);
1699 s->usbout.dma.ready = 0;
1700 if (fmt == AFMT_QUERY)
1701 fmt = s->usbout.dma.format;
1702 else
1703 s->usbout.dma.format = fmt;
1704 if (!srate)
1705 srate = s->usbout.dma.srate;
1706 else
1707 s->usbout.dma.srate = srate;
1708 }
1709 if (fmode & FMODE_READ)
1710 ret1 = set_format_in(s);
1711 if (fmode & FMODE_WRITE)
1712 ret2 = set_format_out(s);
1713 return ret1 ? ret1 : ret2;
1714}
1715
1716/* --------------------------------------------------------------------- */
1717
1718static int wrmixer(struct usb_mixerdev *ms, unsigned mixch, unsigned value)
1719{
1720 struct usb_device *dev = ms->state->usbdev;
1721 unsigned char data[2];
1722 struct mixerchannel *ch;
1723 int v1, v2, v3;
1724
1725 if (mixch >= ms->numch)
1726 return -1;
1727 ch = &ms->ch[mixch];
1728 v3 = ch->maxval - ch->minval;
1729 v1 = value & 0xff;
1730 v2 = (value >> 8) & 0xff;
1731 if (v1 > 100)
1732 v1 = 100;
1733 if (v2 > 100)
1734 v2 = 100;
1735 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1736 v2 = v1;
1737 ch->value = v1 | (v2 << 8);
1738 v1 = (v1 * v3) / 100 + ch->minval;
1739 v2 = (v2 * v3) / 100 + ch->minval;
1740 switch (ch->selector) {
1741 case 0: /* mixer unit request */
1742 data[0] = v1;
1743 data[1] = v1 >> 8;
1744 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1745 (ch->chnum << 8) | 1, ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1746 goto err;
1747 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1748 return 0;
1749 data[0] = v2;
1750 data[1] = v2 >> 8;
1751 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1752 ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)),
1753 ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1754 goto err;
1755 return 0;
1756
1757 /* various feature unit controls */
1758 case VOLUME_CONTROL:
1759 data[0] = v1;
1760 data[1] = v1 >> 8;
1761 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1762 (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1763 goto err;
1764 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1765 return 0;
1766 data[0] = v2;
1767 data[1] = v2 >> 8;
1768 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1769 (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1770 goto err;
1771 return 0;
1772
1773 case BASS_CONTROL:
1774 case MID_CONTROL:
1775 case TREBLE_CONTROL:
1776 data[0] = v1 >> 8;
1777 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1778 (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 1, 1000) < 0)
1779 goto err;
1780 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1781 return 0;
1782 data[0] = v2 >> 8;
1783 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1784 (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 1, 1000) < 0)
1785 goto err;
1786 return 0;
1787
1788 default:
1789 return -1;
1790 }
1791 return 0;
1792
1793 err:
1794 printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n",
1795 dev->devnum, ms->iface, ch->unitid, ch->chnum, ch->selector);
1796 return -1;
1797}
1798
1799static int get_rec_src(struct usb_mixerdev *ms)
1800{
1801 struct usb_device *dev = ms->state->usbdev;
1802 unsigned int mask = 0, retmask = 0;
1803 unsigned int i, j;
1804 unsigned char buf;
1805 int err = 0;
1806
1807 for (i = 0; i < ms->numch; i++) {
1808 if (!ms->ch[i].slctunitid || (mask & (1 << i)))
1809 continue;
1810 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1811 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, 1000) < 0) {
1812 err = -EIO;
1813 printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n",
1814 dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff);
1815 continue;
1816 }
1817 for (j = i; j < ms->numch; j++) {
1818 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1819 continue;
1820 mask |= 1 << j;
1821 if (buf == (ms->ch[j].slctunitid >> 8))
1822 retmask |= 1 << ms->ch[j].osschannel;
1823 }
1824 }
1825 if (err)
1826 return -EIO;
1827 return retmask;
1828}
1829
1830static int set_rec_src(struct usb_mixerdev *ms, int srcmask)
1831{
1832 struct usb_device *dev = ms->state->usbdev;
1833 unsigned int mask = 0, smask, bmask;
1834 unsigned int i, j;
1835 unsigned char buf;
1836 int err = 0;
1837
1838 for (i = 0; i < ms->numch; i++) {
1839 if (!ms->ch[i].slctunitid || (mask & (1 << i)))
1840 continue;
1841 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1842 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, 1000) < 0) {
1843 err = -EIO;
1844 printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n",
1845 dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff);
1846 continue;
1847 }
1848 /* first generate smask */
1849 smask = bmask = 0;
1850 for (j = i; j < ms->numch; j++) {
1851 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1852 continue;
1853 smask |= 1 << ms->ch[j].osschannel;
1854 if (buf == (ms->ch[j].slctunitid >> 8))
1855 bmask |= 1 << ms->ch[j].osschannel;
1856 mask |= 1 << j;
1857 }
1858 /* check for multiple set sources */
1859 j = hweight32(srcmask & smask);
1860 if (j == 0)
1861 continue;
1862 if (j > 1)
1863 srcmask &= ~bmask;
1864 for (j = i; j < ms->numch; j++) {
1865 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1866 continue;
1867 if (!(srcmask & (1 << ms->ch[j].osschannel)))
1868 continue;
1869 buf = ms->ch[j].slctunitid >> 8;
1870 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1871 0, ms->iface | (ms->ch[j].slctunitid << 8), &buf, 1, 1000) < 0) {
1872 err = -EIO;
1873 printk(KERN_ERR "usbaudio: selector write request device %u if %u unit %u failed\n",
1874 dev->devnum, ms->iface, ms->ch[j].slctunitid & 0xff);
1875 continue;
1876 }
1877 }
1878 }
1879 return err ? -EIO : 0;
1880}
1881
1882/* --------------------------------------------------------------------- */
1883
1884/*
1885 * should be called with open_sem hold, so that no new processes
1886 * look at the audio device to be destroyed
1887 */
1888
1889static void release(struct usb_audio_state *s)
1890{
1891 struct usb_audiodev *as;
1892 struct usb_mixerdev *ms;
1893
1894 s->count--;
1895 if (s->count) {
1896 up(&open_sem);
1897 return;
1898 }
1899 up(&open_sem);
1900 wake_up(&open_wait);
1901 while (!list_empty(&s->audiolist)) {
1902 as = list_entry(s->audiolist.next, struct usb_audiodev, list);
1903 list_del(&as->list);
1904 usbin_release(as);
1905 usbout_release(as);
1906 dmabuf_release(&as->usbin.dma);
1907 dmabuf_release(&as->usbout.dma);
1908 usb_free_urb(as->usbin.durb[0].urb);
1909 usb_free_urb(as->usbin.durb[1].urb);
1910 usb_free_urb(as->usbin.surb[0].urb);
1911 usb_free_urb(as->usbin.surb[1].urb);
1912 usb_free_urb(as->usbout.durb[0].urb);
1913 usb_free_urb(as->usbout.durb[1].urb);
1914 usb_free_urb(as->usbout.surb[0].urb);
1915 usb_free_urb(as->usbout.surb[1].urb);
1916 kfree(as);
1917 }
1918 while (!list_empty(&s->mixerlist)) {
1919 ms = list_entry(s->mixerlist.next, struct usb_mixerdev, list);
1920 list_del(&ms->list);
1921 kfree(ms);
1922 }
1923 kfree(s);
1924}
1925
1926static inline int prog_dmabuf_in(struct usb_audiodev *as)
1927{
1928 usbin_stop(as);
1929 return dmabuf_init(&as->usbin.dma);
1930}
1931
1932static inline int prog_dmabuf_out(struct usb_audiodev *as)
1933{
1934 usbout_stop(as);
1935 return dmabuf_init(&as->usbout.dma);
1936}
1937
1938/* --------------------------------------------------------------------- */
1939
1940static int usb_audio_open_mixdev(struct inode *inode, struct file *file)
1941{
1942 unsigned int minor = iminor(inode);
1943 struct usb_mixerdev *ms;
1944 struct usb_audio_state *s;
1945
1946 down(&open_sem);
1947 list_for_each_entry(s, &audiodevs, audiodev) {
1948 list_for_each_entry(ms, &s->mixerlist, list) {
1949 if (ms->dev_mixer == minor)
1950 goto mixer_found;
1951 }
1952 }
1953 up(&open_sem);
1954 return -ENODEV;
1955
1956 mixer_found:
1957 if (!s->usbdev) {
1958 up(&open_sem);
1959 return -EIO;
1960 }
1961 file->private_data = ms;
1962 s->count++;
1963
1964 up(&open_sem);
1965 return nonseekable_open(inode, file);
1966}
1967
1968static int usb_audio_release_mixdev(struct inode *inode, struct file *file)
1969{
1970 struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data;
1971 struct usb_audio_state *s;
1972
1973 lock_kernel();
1974 s = ms->state;
1975 down(&open_sem);
1976 release(s);
1977 unlock_kernel();
1978 return 0;
1979}
1980
1981static int usb_audio_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1982{
1983 struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data;
1984 int i, j, val;
1985 int __user *user_arg = (int __user *)arg;
1986
1987 if (!ms->state->usbdev)
1988 return -ENODEV;
1989
1990 if (cmd == SOUND_MIXER_INFO) {
1991 mixer_info info;
1992
1993 memset(&info, 0, sizeof(info));
1994 strncpy(info.id, "USB_AUDIO", sizeof(info.id));
1995 strncpy(info.name, "USB Audio Class Driver", sizeof(info.name));
1996 info.modify_counter = ms->modcnt;
1997 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1998 return -EFAULT;
1999 return 0;
2000 }
2001 if (cmd == SOUND_OLD_MIXER_INFO) {
2002 _old_mixer_info info;
2003
2004 memset(&info, 0, sizeof(info));
2005 strncpy(info.id, "USB_AUDIO", sizeof(info.id));
2006 strncpy(info.name, "USB Audio Class Driver", sizeof(info.name));
2007 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
2008 return -EFAULT;
2009 return 0;
2010 }
2011 if (cmd == OSS_GETVERSION)
2012 return put_user(SOUND_VERSION, user_arg);
2013 if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int))
2014 return -EINVAL;
2015 if (_IOC_DIR(cmd) == _IOC_READ) {
2016 switch (_IOC_NR(cmd)) {
2017 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
2018 val = get_rec_src(ms);
2019 if (val < 0)
2020 return val;
2021 return put_user(val, user_arg);
2022
2023 case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
2024 for (val = i = 0; i < ms->numch; i++)
2025 val |= 1 << ms->ch[i].osschannel;
2026 return put_user(val, user_arg);
2027
2028 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
2029 for (val = i = 0; i < ms->numch; i++)
2030 if (ms->ch[i].slctunitid)
2031 val |= 1 << ms->ch[i].osschannel;
2032 return put_user(val, user_arg);
2033
2034 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
2035 for (val = i = 0; i < ms->numch; i++)
2036 if (ms->ch[i].flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))
2037 val |= 1 << ms->ch[i].osschannel;
2038 return put_user(val, user_arg);
2039
2040 case SOUND_MIXER_CAPS:
2041 return put_user(SOUND_CAP_EXCL_INPUT, user_arg);
2042
2043 default:
2044 i = _IOC_NR(cmd);
2045 if (i >= SOUND_MIXER_NRDEVICES)
2046 return -EINVAL;
2047 for (j = 0; j < ms->numch; j++) {
2048 if (ms->ch[j].osschannel == i) {
2049 return put_user(ms->ch[j].value, user_arg);
2050 }
2051 }
2052 return -EINVAL;
2053 }
2054 }
2055 if (_IOC_DIR(cmd) != (_IOC_READ|_IOC_WRITE))
2056 return -EINVAL;
2057 ms->modcnt++;
2058 switch (_IOC_NR(cmd)) {
2059 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
2060 if (get_user(val, user_arg))
2061 return -EFAULT;
2062 return set_rec_src(ms, val);
2063
2064 default:
2065 i = _IOC_NR(cmd);
2066 if (i >= SOUND_MIXER_NRDEVICES)
2067 return -EINVAL;
2068 for (j = 0; j < ms->numch && ms->ch[j].osschannel != i; j++);
2069 if (j >= ms->numch)
2070 return -EINVAL;
2071 if (get_user(val, user_arg))
2072 return -EFAULT;
2073 if (wrmixer(ms, j, val))
2074 return -EIO;
2075 return put_user(ms->ch[j].value, user_arg);
2076 }
2077}
2078
2079static /*const*/ struct file_operations usb_mixer_fops = {
2080 .owner = THIS_MODULE,
2081 .llseek = no_llseek,
2082 .ioctl = usb_audio_ioctl_mixdev,
2083 .open = usb_audio_open_mixdev,
2084 .release = usb_audio_release_mixdev,
2085};
2086
2087/* --------------------------------------------------------------------- */
2088
2089static int drain_out(struct usb_audiodev *as, int nonblock)
2090{
2091 DECLARE_WAITQUEUE(wait, current);
2092 unsigned long flags;
2093 int count, tmo;
2094
2095 if (as->usbout.dma.mapped || !as->usbout.dma.ready)
2096 return 0;
2097 usbout_start(as);
2098 add_wait_queue(&as->usbout.dma.wait, &wait);
2099 for (;;) {
2100 __set_current_state(TASK_INTERRUPTIBLE);
2101 spin_lock_irqsave(&as->lock, flags);
2102 count = as->usbout.dma.count;
2103 spin_unlock_irqrestore(&as->lock, flags);
2104 if (count <= 0)
2105 break;
2106 if (signal_pending(current))
2107 break;
2108 if (nonblock) {
2109 remove_wait_queue(&as->usbout.dma.wait, &wait);
2110 set_current_state(TASK_RUNNING);
2111 return -EBUSY;
2112 }
2113 tmo = 3 * HZ * count / as->usbout.dma.srate;
2114 tmo >>= AFMT_BYTESSHIFT(as->usbout.dma.format);
2115 if (!schedule_timeout(tmo + 1)) {
2116 printk(KERN_DEBUG "usbaudio: dma timed out??\n");
2117 break;
2118 }
2119 }
2120 remove_wait_queue(&as->usbout.dma.wait, &wait);
2121 set_current_state(TASK_RUNNING);
2122 if (signal_pending(current))
2123 return -ERESTARTSYS;
2124 return 0;
2125}
2126
2127/* --------------------------------------------------------------------- */
2128
2129static ssize_t usb_audio_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2130{
2131 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2132 DECLARE_WAITQUEUE(wait, current);
2133 ssize_t ret = 0;
2134 unsigned long flags;
2135 unsigned int ptr;
2136 int cnt, err;
2137
2138 if (as->usbin.dma.mapped)
2139 return -ENXIO;
2140 if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as)))
2141 return ret;
2142 if (!access_ok(VERIFY_WRITE, buffer, count))
2143 return -EFAULT;
2144 add_wait_queue(&as->usbin.dma.wait, &wait);
2145 while (count > 0) {
2146 spin_lock_irqsave(&as->lock, flags);
2147 ptr = as->usbin.dma.rdptr;
2148 cnt = as->usbin.dma.count;
2149 /* set task state early to avoid wakeup races */
2150 if (cnt <= 0)
2151 __set_current_state(TASK_INTERRUPTIBLE);
2152 spin_unlock_irqrestore(&as->lock, flags);
2153 if (cnt > count)
2154 cnt = count;
2155 if (cnt <= 0) {
2156 if (usbin_start(as)) {
2157 if (!ret)
2158 ret = -ENODEV;
2159 break;
2160 }
2161 if (file->f_flags & O_NONBLOCK) {
2162 if (!ret)
2163 ret = -EAGAIN;
2164 break;
2165 }
2166 schedule();
2167 if (signal_pending(current)) {
2168 if (!ret)
2169 ret = -ERESTARTSYS;
2170 break;
2171 }
2172 continue;
2173 }
2174 if ((err = dmabuf_copyout_user(&as->usbin.dma, ptr, buffer, cnt))) {
2175 if (!ret)
2176 ret = err;
2177 break;
2178 }
2179 ptr += cnt;
2180 if (ptr >= as->usbin.dma.dmasize)
2181 ptr -= as->usbin.dma.dmasize;
2182 spin_lock_irqsave(&as->lock, flags);
2183 as->usbin.dma.rdptr = ptr;
2184 as->usbin.dma.count -= cnt;
2185 spin_unlock_irqrestore(&as->lock, flags);
2186 count -= cnt;
2187 buffer += cnt;
2188 ret += cnt;
2189 }
2190 __set_current_state(TASK_RUNNING);
2191 remove_wait_queue(&as->usbin.dma.wait, &wait);
2192 return ret;
2193}
2194
2195static ssize_t usb_audio_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2196{
2197 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2198 DECLARE_WAITQUEUE(wait, current);
2199 ssize_t ret = 0;
2200 unsigned long flags;
2201 unsigned int ptr;
2202 unsigned int start_thr;
2203 int cnt, err;
2204
2205 if (as->usbout.dma.mapped)
2206 return -ENXIO;
2207 if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as)))
2208 return ret;
2209 if (!access_ok(VERIFY_READ, buffer, count))
2210 return -EFAULT;
2211 start_thr = (as->usbout.dma.srate << AFMT_BYTESSHIFT(as->usbout.dma.format)) / (1000 / (3 * DESCFRAMES));
2212 add_wait_queue(&as->usbout.dma.wait, &wait);
2213 while (count > 0) {
2214#if 0
2215 printk(KERN_DEBUG "usb_audio_write: count %u dma: count %u rdptr %u wrptr %u dmasize %u fragsize %u flags 0x%02x taskst 0x%lx\n",
2216 count, as->usbout.dma.count, as->usbout.dma.rdptr, as->usbout.dma.wrptr, as->usbout.dma.dmasize, as->usbout.dma.fragsize,
2217 as->usbout.flags, current->state);
2218#endif
2219 spin_lock_irqsave(&as->lock, flags);
2220 if (as->usbout.dma.count < 0) {
2221 as->usbout.dma.count = 0;
2222 as->usbout.dma.rdptr = as->usbout.dma.wrptr;
2223 }
2224 ptr = as->usbout.dma.wrptr;
2225 cnt = as->usbout.dma.dmasize - as->usbout.dma.count;
2226 /* set task state early to avoid wakeup races */
2227 if (cnt <= 0)
2228 __set_current_state(TASK_INTERRUPTIBLE);
2229 spin_unlock_irqrestore(&as->lock, flags);
2230 if (cnt > count)
2231 cnt = count;
2232 if (cnt <= 0) {
2233 if (usbout_start(as)) {
2234 if (!ret)
2235 ret = -ENODEV;
2236 break;
2237 }
2238 if (file->f_flags & O_NONBLOCK) {
2239 if (!ret)
2240 ret = -EAGAIN;
2241 break;
2242 }
2243 schedule();
2244 if (signal_pending(current)) {
2245 if (!ret)
2246 ret = -ERESTARTSYS;
2247 break;
2248 }
2249 continue;
2250 }
2251 if ((err = dmabuf_copyin_user(&as->usbout.dma, ptr, buffer, cnt))) {
2252 if (!ret)
2253 ret = err;
2254 break;
2255 }
2256 ptr += cnt;
2257 if (ptr >= as->usbout.dma.dmasize)
2258 ptr -= as->usbout.dma.dmasize;
2259 spin_lock_irqsave(&as->lock, flags);
2260 as->usbout.dma.wrptr = ptr;
2261 as->usbout.dma.count += cnt;
2262 spin_unlock_irqrestore(&as->lock, flags);
2263 count -= cnt;
2264 buffer += cnt;
2265 ret += cnt;
2266 if (as->usbout.dma.count >= start_thr && usbout_start(as)) {
2267 if (!ret)
2268 ret = -ENODEV;
2269 break;
2270 }
2271 }
2272 __set_current_state(TASK_RUNNING);
2273 remove_wait_queue(&as->usbout.dma.wait, &wait);
2274 return ret;
2275}
2276
2277/* Called without the kernel lock - fine */
2278static unsigned int usb_audio_poll(struct file *file, struct poll_table_struct *wait)
2279{
2280 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2281 unsigned long flags;
2282 unsigned int mask = 0;
2283
2284 if (file->f_mode & FMODE_WRITE) {
2285 if (!as->usbout.dma.ready)
2286 prog_dmabuf_out(as);
2287 poll_wait(file, &as->usbout.dma.wait, wait);
2288 }
2289 if (file->f_mode & FMODE_READ) {
2290 if (!as->usbin.dma.ready)
2291 prog_dmabuf_in(as);
2292 poll_wait(file, &as->usbin.dma.wait, wait);
2293 }
2294 spin_lock_irqsave(&as->lock, flags);
2295 if (file->f_mode & FMODE_READ) {
2296 if (as->usbin.dma.count >= (signed)as->usbin.dma.fragsize)
2297 mask |= POLLIN | POLLRDNORM;
2298 }
2299 if (file->f_mode & FMODE_WRITE) {
2300 if (as->usbout.dma.mapped) {
2301 if (as->usbout.dma.count >= (signed)as->usbout.dma.fragsize)
2302 mask |= POLLOUT | POLLWRNORM;
2303 } else {
2304 if ((signed)as->usbout.dma.dmasize >= as->usbout.dma.count + (signed)as->usbout.dma.fragsize)
2305 mask |= POLLOUT | POLLWRNORM;
2306 }
2307 }
2308 spin_unlock_irqrestore(&as->lock, flags);
2309 return mask;
2310}
2311
2312static int usb_audio_mmap(struct file *file, struct vm_area_struct *vma)
2313{
2314 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2315 struct dmabuf *db;
2316 int ret = -EINVAL;
2317
2318 lock_kernel();
2319 if (vma->vm_flags & VM_WRITE) {
2320 if ((ret = prog_dmabuf_out(as)) != 0)
2321 goto out;
2322 db = &as->usbout.dma;
2323 } else if (vma->vm_flags & VM_READ) {
2324 if ((ret = prog_dmabuf_in(as)) != 0)
2325 goto out;
2326 db = &as->usbin.dma;
2327 } else
2328 goto out;
2329
2330 ret = -EINVAL;
2331 if (vma->vm_pgoff != 0)
2332 goto out;
2333
2334 ret = dmabuf_mmap(vma, db, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot);
2335out:
2336 unlock_kernel();
2337 return ret;
2338}
2339
2340static int usb_audio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2341{
2342 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2343 struct usb_audio_state *s = as->state;
2344 int __user *user_arg = (int __user *)arg;
2345 unsigned long flags;
2346 audio_buf_info abinfo;
2347 count_info cinfo;
2348 int val = 0;
2349 int val2, mapped, ret;
2350
2351 if (!s->usbdev)
2352 return -EIO;
2353 mapped = ((file->f_mode & FMODE_WRITE) && as->usbout.dma.mapped) ||
2354 ((file->f_mode & FMODE_READ) && as->usbin.dma.mapped);
2355#if 0
2356 if (arg)
2357 get_user(val, (int *)arg);
2358 printk(KERN_DEBUG "usbaudio: usb_audio_ioctl cmd=%x arg=%lx *arg=%d\n", cmd, arg, val)
2359#endif
2360 switch (cmd) {
2361 case OSS_GETVERSION:
2362 return put_user(SOUND_VERSION, user_arg);
2363
2364 case SNDCTL_DSP_SYNC:
2365 if (file->f_mode & FMODE_WRITE)
2366 return drain_out(as, 0/*file->f_flags & O_NONBLOCK*/);
2367 return 0;
2368
2369 case SNDCTL_DSP_SETDUPLEX:
2370 return 0;
2371
2372 case SNDCTL_DSP_GETCAPS:
2373 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER |
2374 DSP_CAP_MMAP | DSP_CAP_BATCH, user_arg);
2375
2376 case SNDCTL_DSP_RESET:
2377 if (file->f_mode & FMODE_WRITE) {
2378 usbout_stop(as);
2379 as->usbout.dma.rdptr = as->usbout.dma.wrptr = as->usbout.dma.count = as->usbout.dma.total_bytes = 0;
2380 }
2381 if (file->f_mode & FMODE_READ) {
2382 usbin_stop(as);
2383 as->usbin.dma.rdptr = as->usbin.dma.wrptr = as->usbin.dma.count = as->usbin.dma.total_bytes = 0;
2384 }
2385 return 0;
2386
2387 case SNDCTL_DSP_SPEED:
2388 if (get_user(val, user_arg))
2389 return -EFAULT;
2390 if (val >= 0) {
2391 if (val < 4000)
2392 val = 4000;
2393 if (val > 100000)
2394 val = 100000;
2395 if (set_format(as, file->f_mode, AFMT_QUERY, val))
2396 return -EIO;
2397 }
2398 return put_user((file->f_mode & FMODE_READ) ?
2399 as->usbin.dma.srate : as->usbout.dma.srate,
2400 user_arg);
2401
2402 case SNDCTL_DSP_STEREO:
2403 if (get_user(val, user_arg))
2404 return -EFAULT;
2405 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2406 if (val)
2407 val2 |= AFMT_STEREO;
2408 else
2409 val2 &= ~AFMT_STEREO;
2410 if (set_format(as, file->f_mode, val2, 0))
2411 return -EIO;
2412 return 0;
2413
2414 case SNDCTL_DSP_CHANNELS:
2415 if (get_user(val, user_arg))
2416 return -EFAULT;
2417 if (val != 0) {
2418 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2419 if (val == 1)
2420 val2 &= ~AFMT_STEREO;
2421 else
2422 val2 |= AFMT_STEREO;
2423 if (set_format(as, file->f_mode, val2, 0))
2424 return -EIO;
2425 }
2426 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2427 return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg);
2428
2429 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
2430 return put_user(AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE |
2431 AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE, user_arg);
2432
2433 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
2434 if (get_user(val, user_arg))
2435 return -EFAULT;
2436 if (val != AFMT_QUERY) {
2437 if (hweight32(val) != 1)
2438 return -EINVAL;
2439 if (!(val & (AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE |
2440 AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE)))
2441 return -EINVAL;
2442 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2443 val |= val2 & AFMT_STEREO;
2444 if (set_format(as, file->f_mode, val, 0))
2445 return -EIO;
2446 }
2447 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2448 return put_user(val2 & ~AFMT_STEREO, user_arg);
2449
2450 case SNDCTL_DSP_POST:
2451 return 0;
2452
2453 case SNDCTL_DSP_GETTRIGGER:
2454 val = 0;
2455 if (file->f_mode & FMODE_READ && as->usbin.flags & FLG_RUNNING)
2456 val |= PCM_ENABLE_INPUT;
2457 if (file->f_mode & FMODE_WRITE && as->usbout.flags & FLG_RUNNING)
2458 val |= PCM_ENABLE_OUTPUT;
2459 return put_user(val, user_arg);
2460
2461 case SNDCTL_DSP_SETTRIGGER:
2462 if (get_user(val, user_arg))
2463 return -EFAULT;
2464 if (file->f_mode & FMODE_READ) {
2465 if (val & PCM_ENABLE_INPUT) {
2466 if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as)))
2467 return ret;
2468 if (usbin_start(as))
2469 return -ENODEV;
2470 } else
2471 usbin_stop(as);
2472 }
2473 if (file->f_mode & FMODE_WRITE) {
2474 if (val & PCM_ENABLE_OUTPUT) {
2475 if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as)))
2476 return ret;
2477 if (usbout_start(as))
2478 return -ENODEV;
2479 } else
2480 usbout_stop(as);
2481 }
2482 return 0;
2483
2484 case SNDCTL_DSP_GETOSPACE:
2485 if (!(file->f_mode & FMODE_WRITE))
2486 return -EINVAL;
2487 if (!(as->usbout.flags & FLG_RUNNING) && (val = prog_dmabuf_out(as)) != 0)
2488 return val;
2489 spin_lock_irqsave(&as->lock, flags);
2490 abinfo.fragsize = as->usbout.dma.fragsize;
2491 abinfo.bytes = as->usbout.dma.dmasize - as->usbout.dma.count;
2492 abinfo.fragstotal = as->usbout.dma.numfrag;
2493 abinfo.fragments = abinfo.bytes >> as->usbout.dma.fragshift;
2494 spin_unlock_irqrestore(&as->lock, flags);
2495 return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2496
2497 case SNDCTL_DSP_GETISPACE:
2498 if (!(file->f_mode & FMODE_READ))
2499 return -EINVAL;
2500 if (!(as->usbin.flags & FLG_RUNNING) && (val = prog_dmabuf_in(as)) != 0)
2501 return val;
2502 spin_lock_irqsave(&as->lock, flags);
2503 abinfo.fragsize = as->usbin.dma.fragsize;
2504 abinfo.bytes = as->usbin.dma.count;
2505 abinfo.fragstotal = as->usbin.dma.numfrag;
2506 abinfo.fragments = abinfo.bytes >> as->usbin.dma.fragshift;
2507 spin_unlock_irqrestore(&as->lock, flags);
2508 return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2509
2510 case SNDCTL_DSP_NONBLOCK:
2511 file->f_flags |= O_NONBLOCK;
2512 return 0;
2513
2514 case SNDCTL_DSP_GETODELAY:
2515 if (!(file->f_mode & FMODE_WRITE))
2516 return -EINVAL;
2517 spin_lock_irqsave(&as->lock, flags);
2518 val = as->usbout.dma.count;
2519 spin_unlock_irqrestore(&as->lock, flags);
2520 return put_user(val, user_arg);
2521
2522 case SNDCTL_DSP_GETIPTR:
2523 if (!(file->f_mode & FMODE_READ))
2524 return -EINVAL;
2525 spin_lock_irqsave(&as->lock, flags);
2526 cinfo.bytes = as->usbin.dma.total_bytes;
2527 cinfo.blocks = as->usbin.dma.count >> as->usbin.dma.fragshift;
2528 cinfo.ptr = as->usbin.dma.wrptr;
2529 if (as->usbin.dma.mapped)
2530 as->usbin.dma.count &= as->usbin.dma.fragsize-1;
2531 spin_unlock_irqrestore(&as->lock, flags);
2532 if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo)))
2533 return -EFAULT;
2534 return 0;
2535
2536 case SNDCTL_DSP_GETOPTR:
2537 if (!(file->f_mode & FMODE_WRITE))
2538 return -EINVAL;
2539 spin_lock_irqsave(&as->lock, flags);
2540 cinfo.bytes = as->usbout.dma.total_bytes;
2541 cinfo.blocks = as->usbout.dma.count >> as->usbout.dma.fragshift;
2542 cinfo.ptr = as->usbout.dma.rdptr;
2543 if (as->usbout.dma.mapped)
2544 as->usbout.dma.count &= as->usbout.dma.fragsize-1;
2545 spin_unlock_irqrestore(&as->lock, flags);
2546 if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo)))
2547 return -EFAULT;
2548 return 0;
2549
2550 case SNDCTL_DSP_GETBLKSIZE:
2551 if (file->f_mode & FMODE_WRITE) {
2552 if ((val = prog_dmabuf_out(as)))
2553 return val;
2554 return put_user(as->usbout.dma.fragsize, user_arg);
2555 }
2556 if ((val = prog_dmabuf_in(as)))
2557 return val;
2558 return put_user(as->usbin.dma.fragsize, user_arg);
2559
2560 case SNDCTL_DSP_SETFRAGMENT:
2561 if (get_user(val, user_arg))
2562 return -EFAULT;
2563 if (file->f_mode & FMODE_READ) {
2564 as->usbin.dma.ossfragshift = val & 0xffff;
2565 as->usbin.dma.ossmaxfrags = (val >> 16) & 0xffff;
2566 if (as->usbin.dma.ossfragshift < 4)
2567 as->usbin.dma.ossfragshift = 4;
2568 if (as->usbin.dma.ossfragshift > 15)
2569 as->usbin.dma.ossfragshift = 15;
2570 if (as->usbin.dma.ossmaxfrags < 4)
2571 as->usbin.dma.ossmaxfrags = 4;
2572 }
2573 if (file->f_mode & FMODE_WRITE) {
2574 as->usbout.dma.ossfragshift = val & 0xffff;
2575 as->usbout.dma.ossmaxfrags = (val >> 16) & 0xffff;
2576 if (as->usbout.dma.ossfragshift < 4)
2577 as->usbout.dma.ossfragshift = 4;
2578 if (as->usbout.dma.ossfragshift > 15)
2579 as->usbout.dma.ossfragshift = 15;
2580 if (as->usbout.dma.ossmaxfrags < 4)
2581 as->usbout.dma.ossmaxfrags = 4;
2582 }
2583 return 0;
2584
2585 case SNDCTL_DSP_SUBDIVIDE:
2586 if ((file->f_mode & FMODE_READ && as->usbin.dma.subdivision) ||
2587 (file->f_mode & FMODE_WRITE && as->usbout.dma.subdivision))
2588 return -EINVAL;
2589 if (get_user(val, user_arg))
2590 return -EFAULT;
2591 if (val != 1 && val != 2 && val != 4)
2592 return -EINVAL;
2593 if (file->f_mode & FMODE_READ)
2594 as->usbin.dma.subdivision = val;
2595 if (file->f_mode & FMODE_WRITE)
2596 as->usbout.dma.subdivision = val;
2597 return 0;
2598
2599 case SOUND_PCM_READ_RATE:
2600 return put_user((file->f_mode & FMODE_READ) ?
2601 as->usbin.dma.srate : as->usbout.dma.srate,
2602 user_arg);
2603
2604 case SOUND_PCM_READ_CHANNELS:
2605 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2606 return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg);
2607
2608 case SOUND_PCM_READ_BITS:
2609 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2610 return put_user(AFMT_IS16BIT(val2) ? 16 : 8, user_arg);
2611
2612 case SOUND_PCM_WRITE_FILTER:
2613 case SNDCTL_DSP_SETSYNCRO:
2614 case SOUND_PCM_READ_FILTER:
2615 return -EINVAL;
2616 }
2617 dprintk((KERN_DEBUG "usbaudio: usb_audio_ioctl - no command found\n"));
2618 return -ENOIOCTLCMD;
2619}
2620
2621static int usb_audio_open(struct inode *inode, struct file *file)
2622{
2623 unsigned int minor = iminor(inode);
2624 DECLARE_WAITQUEUE(wait, current);
2625 struct usb_audiodev *as;
2626 struct usb_audio_state *s;
2627
2628 for (;;) {
2629 down(&open_sem);
2630 list_for_each_entry(s, &audiodevs, audiodev) {
2631 list_for_each_entry(as, &s->audiolist, list) {
2632 if (!((as->dev_audio ^ minor) & ~0xf))
2633 goto device_found;
2634 }
2635 }
2636 up(&open_sem);
2637 return -ENODEV;
2638
2639 device_found:
2640 if (!s->usbdev) {
2641 up(&open_sem);
2642 return -EIO;
2643 }
2644 /* wait for device to become free */
2645 if (!(as->open_mode & file->f_mode))
2646 break;
2647 if (file->f_flags & O_NONBLOCK) {
2648 up(&open_sem);
2649 return -EBUSY;
2650 }
2651 __set_current_state(TASK_INTERRUPTIBLE);
2652 add_wait_queue(&open_wait, &wait);
2653 up(&open_sem);
2654 schedule();
2655 __set_current_state(TASK_RUNNING);
2656 remove_wait_queue(&open_wait, &wait);
2657 if (signal_pending(current))
2658 return -ERESTARTSYS;
2659 }
2660 if (file->f_mode & FMODE_READ)
2661 as->usbin.dma.ossfragshift = as->usbin.dma.ossmaxfrags = as->usbin.dma.subdivision = 0;
2662 if (file->f_mode & FMODE_WRITE)
2663 as->usbout.dma.ossfragshift = as->usbout.dma.ossmaxfrags = as->usbout.dma.subdivision = 0;
2664 if (set_format(as, file->f_mode, ((minor & 0xf) == SND_DEV_DSP16) ? AFMT_S16_LE : AFMT_U8 /* AFMT_ULAW */, 8000)) {
2665 up(&open_sem);
2666 return -EIO;
2667 }
2668 file->private_data = as;
2669 as->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2670 s->count++;
2671 up(&open_sem);
2672 return nonseekable_open(inode, file);
2673}
2674
2675static int usb_audio_release(struct inode *inode, struct file *file)
2676{
2677 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2678 struct usb_audio_state *s;
2679 struct usb_device *dev;
2680
2681 lock_kernel();
2682 s = as->state;
2683 dev = s->usbdev;
2684 if (file->f_mode & FMODE_WRITE)
2685 drain_out(as, file->f_flags & O_NONBLOCK);
2686 down(&open_sem);
2687 if (file->f_mode & FMODE_WRITE) {
2688 usbout_stop(as);
2689 if (dev && as->usbout.interface >= 0)
2690 usb_set_interface(dev, as->usbout.interface, 0);
2691 dmabuf_release(&as->usbout.dma);
2692 usbout_release(as);
2693 }
2694 if (file->f_mode & FMODE_READ) {
2695 usbin_stop(as);
2696 if (dev && as->usbin.interface >= 0)
2697 usb_set_interface(dev, as->usbin.interface, 0);
2698 dmabuf_release(&as->usbin.dma);
2699 usbin_release(as);
2700 }
2701 as->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
2702 release(s);
2703 wake_up(&open_wait);
2704 unlock_kernel();
2705 return 0;
2706}
2707
2708static /*const*/ struct file_operations usb_audio_fops = {
2709 .owner = THIS_MODULE,
2710 .llseek = no_llseek,
2711 .read = usb_audio_read,
2712 .write = usb_audio_write,
2713 .poll = usb_audio_poll,
2714 .ioctl = usb_audio_ioctl,
2715 .mmap = usb_audio_mmap,
2716 .open = usb_audio_open,
2717 .release = usb_audio_release,
2718};
2719
2720/* --------------------------------------------------------------------- */
2721
2722static int usb_audio_probe(struct usb_interface *iface,
2723 const struct usb_device_id *id);
2724static void usb_audio_disconnect(struct usb_interface *iface);
2725
2726static struct usb_device_id usb_audio_ids [] = {
2727 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2728 .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = 1},
2729 { } /* Terminating entry */
2730};
2731
2732MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2733
2734static struct usb_driver usb_audio_driver = {
2735 .name = "audio",
2736 .probe = usb_audio_probe,
2737 .disconnect = usb_audio_disconnect,
2738 .id_table = usb_audio_ids,
2739};
2740
2741static void *find_descriptor(void *descstart, unsigned int desclen, void *after,
2742 u8 dtype, int iface, int altsetting)
2743{
2744 u8 *p, *end, *next;
2745 int ifc = -1, as = -1;
2746
2747 p = descstart;
2748 end = p + desclen;
2749 for (; p < end;) {
2750 if (p[0] < 2)
2751 return NULL;
2752 next = p + p[0];
2753 if (next > end)
2754 return NULL;
2755 if (p[1] == USB_DT_INTERFACE) {
2756 /* minimum length of interface descriptor */
2757 if (p[0] < 9)
2758 return NULL;
2759 ifc = p[2];
2760 as = p[3];
2761 }
2762 if (p[1] == dtype && (!after || (void *)p > after) &&
2763 (iface == -1 || iface == ifc) && (altsetting == -1 || altsetting == as)) {
2764 return p;
2765 }
2766 p = next;
2767 }
2768 return NULL;
2769}
2770
2771static void *find_csinterface_descriptor(void *descstart, unsigned int desclen, void *after, u8 dsubtype, int iface, int altsetting)
2772{
2773 unsigned char *p;
2774
2775 p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, altsetting);
2776 while (p) {
2777 if (p[0] >= 3 && p[2] == dsubtype)
2778 return p;
2779 p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, altsetting);
2780 }
2781 return NULL;
2782}
2783
2784static void *find_audiocontrol_unit(void *descstart, unsigned int desclen, void *after, u8 unit, int iface)
2785{
2786 unsigned char *p;
2787
2788 p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, -1);
2789 while (p) {
2790 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
2791 return p;
2792 p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, -1);
2793 }
2794 return NULL;
2795}
2796
2797static void usb_audio_parsestreaming(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, int asifin, int asifout)
2798{
2799 struct usb_device *dev = s->usbdev;
2800 struct usb_audiodev *as;
2801 struct usb_host_interface *alts;
2802 struct usb_interface *iface;
2803 struct audioformat *fp;
2804 unsigned char *fmt, *csep;
2805 unsigned int i, j, k, format, idx;
2806
2807 if (!(as = kmalloc(sizeof(struct usb_audiodev), GFP_KERNEL)))
2808 return;
2809 memset(as, 0, sizeof(struct usb_audiodev));
2810 init_waitqueue_head(&as->usbin.dma.wait);
2811 init_waitqueue_head(&as->usbout.dma.wait);
2812 spin_lock_init(&as->lock);
2813 as->usbin.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2814 as->usbin.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2815 as->usbin.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2816 as->usbin.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2817 as->usbout.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2818 as->usbout.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2819 as->usbout.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2820 as->usbout.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2821 if ((!as->usbin.durb[0].urb) ||
2822 (!as->usbin.durb[1].urb) ||
2823 (!as->usbin.surb[0].urb) ||
2824 (!as->usbin.surb[1].urb) ||
2825 (!as->usbout.durb[0].urb) ||
2826 (!as->usbout.durb[1].urb) ||
2827 (!as->usbout.surb[0].urb) ||
2828 (!as->usbout.surb[1].urb)) {
2829 usb_free_urb(as->usbin.durb[0].urb);
2830 usb_free_urb(as->usbin.durb[1].urb);
2831 usb_free_urb(as->usbin.surb[0].urb);
2832 usb_free_urb(as->usbin.surb[1].urb);
2833 usb_free_urb(as->usbout.durb[0].urb);
2834 usb_free_urb(as->usbout.durb[1].urb);
2835 usb_free_urb(as->usbout.surb[0].urb);
2836 usb_free_urb(as->usbout.surb[1].urb);
2837 kfree(as);
2838 return;
2839 }
2840 as->state = s;
2841 as->usbin.interface = asifin;
2842 as->usbout.interface = asifout;
2843 /* search for input formats */
2844 if (asifin >= 0) {
2845 as->usbin.flags = FLG_CONNECTED;
2846 iface = usb_ifnum_to_if(dev, asifin);
2847 for (idx = 0; idx < iface->num_altsetting; idx++) {
2848 alts = &iface->altsetting[idx];
2849 i = alts->desc.bAlternateSetting;
2850 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2)
2851 continue;
2852 if (alts->desc.bNumEndpoints < 1) {
2853 if (i != 0) { /* altsetting 0 has no endpoints (Section B.3.4.1) */
2854 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n",
2855 dev->devnum, asifin, i);
2856 }
2857 continue;
2858 }
2859 if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 ||
2860 !(alts->endpoint[0].desc.bEndpointAddress & 0x80)) {
2861 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous in\n",
2862 dev->devnum, asifin, i);
2863 continue;
2864 }
2865 fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifin, i);
2866 if (!fmt) {
2867 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2868 dev->devnum, asifin, i);
2869 continue;
2870 }
2871 if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) {
2872 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n",
2873 dev->devnum, asifin, i);
2874 continue;
2875 }
2876 format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8);
2877 fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifin, i);
2878 if (!fmt) {
2879 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2880 dev->devnum, asifin, i);
2881 continue;
2882 }
2883 if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) {
2884 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n",
2885 dev->devnum, asifin, i);
2886 continue;
2887 }
2888 if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) {
2889 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n",
2890 dev->devnum, asifin, i, fmt[4], fmt[5]);
2891 continue;
2892 }
2893 csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifin, i);
2894 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2895 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n",
2896 dev->devnum, asifin, i);
2897 continue;
2898 }
2899 if (as->numfmtin >= MAXFORMATS)
2900 continue;
2901 fp = &as->fmtin[as->numfmtin++];
2902 if (fmt[5] == 2)
2903 format &= (AFMT_U16_LE | AFMT_S16_LE);
2904 else
2905 format &= (AFMT_U8 | AFMT_S8);
2906 if (fmt[4] == 2)
2907 format |= AFMT_STEREO;
2908 fp->format = format;
2909 fp->altsetting = i;
2910 fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16);
2911 printk(KERN_INFO "usbaudio: valid input sample rate %u\n", fp->sratelo);
2912 for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) {
2913 k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16);
2914 printk(KERN_INFO "usbaudio: valid input sample rate %u\n", k);
2915 if (k > fp->sratehi)
2916 fp->sratehi = k;
2917 if (k < fp->sratelo)
2918 fp->sratelo = k;
2919 }
2920 fp->attributes = csep[3];
2921 printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n",
2922 dev->devnum, asifin, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes);
2923 }
2924 }
2925 /* search for output formats */
2926 if (asifout >= 0) {
2927 as->usbout.flags = FLG_CONNECTED;
2928 iface = usb_ifnum_to_if(dev, asifout);
2929 for (idx = 0; idx < iface->num_altsetting; idx++) {
2930 alts = &iface->altsetting[idx];
2931 i = alts->desc.bAlternateSetting;
2932 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2)
2933 continue;
2934 if (alts->desc.bNumEndpoints < 1) {
2935 /* altsetting 0 should never have iso EPs */
2936 if (i != 0)
2937 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n",
2938 dev->devnum, asifout, i);
2939 continue;
2940 }
2941 if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 ||
2942 (alts->endpoint[0].desc.bEndpointAddress & 0x80)) {
2943 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous out\n",
2944 dev->devnum, asifout, i);
2945 continue;
2946 }
2947 /* See USB audio formats manual, section 2 */
2948 fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifout, i);
2949 if (!fmt) {
2950 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2951 dev->devnum, asifout, i);
2952 continue;
2953 }
2954 if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) {
2955 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n",
2956 dev->devnum, asifout, i);
2957 continue;
2958 }
2959 format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8);
2960 /* Dallas DS4201 workaround */
2961 if (le16_to_cpu(dev->descriptor.idVendor) == 0x04fa &&
2962 le16_to_cpu(dev->descriptor.idProduct) == 0x4201)
2963 format = (AFMT_S16_LE | AFMT_S8);
2964 fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifout, i);
2965 if (!fmt) {
2966 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2967 dev->devnum, asifout, i);
2968 continue;
2969 }
2970 if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) {
2971 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n",
2972 dev->devnum, asifout, i);
2973 continue;
2974 }
2975 if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) {
2976 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n",
2977 dev->devnum, asifout, i, fmt[4], fmt[5]);
2978 continue;
2979 }
2980 csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifout, i);
2981 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2982 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n",
2983 dev->devnum, asifout, i);
2984 continue;
2985 }
2986 if (as->numfmtout >= MAXFORMATS)
2987 continue;
2988 fp = &as->fmtout[as->numfmtout++];
2989 if (fmt[5] == 2)
2990 format &= (AFMT_U16_LE | AFMT_S16_LE);
2991 else
2992 format &= (AFMT_U8 | AFMT_S8);
2993 if (fmt[4] == 2)
2994 format |= AFMT_STEREO;
2995 fp->format = format;
2996 fp->altsetting = i;
2997 fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16);
2998 printk(KERN_INFO "usbaudio: valid output sample rate %u\n", fp->sratelo);
2999 for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) {
3000 k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16);
3001 printk(KERN_INFO "usbaudio: valid output sample rate %u\n", k);
3002 if (k > fp->sratehi)
3003 fp->sratehi = k;
3004 if (k < fp->sratelo)
3005 fp->sratelo = k;
3006 }
3007 fp->attributes = csep[3];
3008 printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n",
3009 dev->devnum, asifout, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes);
3010 }
3011 }
3012 if (as->numfmtin == 0 && as->numfmtout == 0) {
3013 usb_free_urb(as->usbin.durb[0].urb);
3014 usb_free_urb(as->usbin.durb[1].urb);
3015 usb_free_urb(as->usbin.surb[0].urb);
3016 usb_free_urb(as->usbin.surb[1].urb);
3017 usb_free_urb(as->usbout.durb[0].urb);
3018 usb_free_urb(as->usbout.durb[1].urb);
3019 usb_free_urb(as->usbout.surb[0].urb);
3020 usb_free_urb(as->usbout.surb[1].urb);
3021 kfree(as);
3022 return;
3023 }
3024 if ((as->dev_audio = register_sound_dsp(&usb_audio_fops, -1)) < 0) {
3025 printk(KERN_ERR "usbaudio: cannot register dsp\n");
3026 usb_free_urb(as->usbin.durb[0].urb);
3027 usb_free_urb(as->usbin.durb[1].urb);
3028 usb_free_urb(as->usbin.surb[0].urb);
3029 usb_free_urb(as->usbin.surb[1].urb);
3030 usb_free_urb(as->usbout.durb[0].urb);
3031 usb_free_urb(as->usbout.durb[1].urb);
3032 usb_free_urb(as->usbout.surb[0].urb);
3033 usb_free_urb(as->usbout.surb[1].urb);
3034 kfree(as);
3035 return;
3036 }
3037 printk(KERN_INFO "usbaudio: registered dsp 14,%d\n", as->dev_audio);
3038 /* everything successful */
3039 list_add_tail(&as->list, &s->audiolist);
3040}
3041
3042struct consmixstate {
3043 struct usb_audio_state *s;
3044 unsigned char *buffer;
3045 unsigned int buflen;
3046 unsigned int ctrlif;
3047 struct mixerchannel mixch[SOUND_MIXER_NRDEVICES];
3048 unsigned int nrmixch;
3049 unsigned int mixchmask;
3050 unsigned long unitbitmap[32/sizeof(unsigned long)];
3051 /* return values */
3052 unsigned int nrchannels;
3053 unsigned int termtype;
3054 unsigned int chconfig;
3055};
3056
3057static struct mixerchannel *getmixchannel(struct consmixstate *state, unsigned int nr)
3058{
3059 struct mixerchannel *c;
3060
3061 if (nr >= SOUND_MIXER_NRDEVICES) {
3062 printk(KERN_ERR "usbaudio: invalid OSS mixer channel %u\n", nr);
3063 return NULL;
3064 }
3065 if (!(state->mixchmask & (1 << nr))) {
3066 printk(KERN_WARNING "usbaudio: OSS mixer channel %u already in use\n", nr);
3067 return NULL;
3068 }
3069 c = &state->mixch[state->nrmixch++];
3070 c->osschannel = nr;
3071 state->mixchmask &= ~(1 << nr);
3072 return c;
3073}
3074
3075static unsigned int getvolchannel(struct consmixstate *state)
3076{
3077 unsigned int u;
3078
3079 if ((state->termtype & 0xff00) == 0x0000 && (state->mixchmask & SOUND_MASK_VOLUME))
3080 return SOUND_MIXER_VOLUME;
3081 if ((state->termtype & 0xff00) == 0x0100) {
3082 if (state->mixchmask & SOUND_MASK_PCM)
3083 return SOUND_MIXER_PCM;
3084 if (state->mixchmask & SOUND_MASK_ALTPCM)
3085 return SOUND_MIXER_ALTPCM;
3086 }
3087 if ((state->termtype & 0xff00) == 0x0200 && (state->mixchmask & SOUND_MASK_MIC))
3088 return SOUND_MIXER_MIC;
3089 if ((state->termtype & 0xff00) == 0x0300 && (state->mixchmask & SOUND_MASK_SPEAKER))
3090 return SOUND_MIXER_SPEAKER;
3091 if ((state->termtype & 0xff00) == 0x0500) {
3092 if (state->mixchmask & SOUND_MASK_PHONEIN)
3093 return SOUND_MIXER_PHONEIN;
3094 if (state->mixchmask & SOUND_MASK_PHONEOUT)
3095 return SOUND_MIXER_PHONEOUT;
3096 }
3097 if (state->termtype >= 0x710 && state->termtype <= 0x711 && (state->mixchmask & SOUND_MASK_RADIO))
3098 return SOUND_MIXER_RADIO;
3099 if (state->termtype >= 0x709 && state->termtype <= 0x70f && (state->mixchmask & SOUND_MASK_VIDEO))
3100 return SOUND_MIXER_VIDEO;
3101 u = ffs(state->mixchmask & (SOUND_MASK_LINE | SOUND_MASK_CD | SOUND_MASK_LINE1 | SOUND_MASK_LINE2 | SOUND_MASK_LINE3 |
3102 SOUND_MASK_DIGITAL1 | SOUND_MASK_DIGITAL2 | SOUND_MASK_DIGITAL3));
3103 return u-1;
3104}
3105
3106static void prepmixch(struct consmixstate *state)
3107{
3108 struct usb_device *dev = state->s->usbdev;
3109 struct mixerchannel *ch;
3110 unsigned char *buf;
3111 __s16 v1;
3112 unsigned int v2, v3;
3113
3114 if (!state->nrmixch || state->nrmixch > SOUND_MIXER_NRDEVICES)
3115 return;
3116 buf = kmalloc(sizeof(*buf) * 2, GFP_KERNEL);
3117 if (!buf) {
3118 printk(KERN_ERR "prepmixch: out of memory\n") ;
3119 return;
3120 }
3121
3122 ch = &state->mixch[state->nrmixch-1];
3123 switch (ch->selector) {
3124 case 0: /* mixer unit request */
3125 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3126 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3127 goto err;
3128 ch->minval = buf[0] | (buf[1] << 8);
3129 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3130 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3131 goto err;
3132 ch->maxval = buf[0] | (buf[1] << 8);
3133 v2 = ch->maxval - ch->minval;
3134 if (!v2)
3135 v2 = 1;
3136 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3137 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3138 goto err;
3139 v1 = buf[0] | (buf[1] << 8);
3140 v3 = v1 - ch->minval;
3141 v3 = 100 * v3 / v2;
3142 if (v3 > 100)
3143 v3 = 100;
3144 ch->value = v3;
3145 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
3146 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3147 ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)),
3148 state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3149 goto err;
3150 v1 = buf[0] | (buf[1] << 8);
3151 v3 = v1 - ch->minval;
3152 v3 = 100 * v3 / v2;
3153 if (v3 > 100)
3154 v3 = 100;
3155 }
3156 ch->value |= v3 << 8;
3157 break;
3158
3159 /* various feature unit controls */
3160 case VOLUME_CONTROL:
3161 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3162 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3163 goto err;
3164 ch->minval = buf[0] | (buf[1] << 8);
3165 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3166 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3167 goto err;
3168 ch->maxval = buf[0] | (buf[1] << 8);
3169 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3170 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3171 goto err;
3172 v1 = buf[0] | (buf[1] << 8);
3173 v2 = ch->maxval - ch->minval;
3174 v3 = v1 - ch->minval;
3175 if (!v2)
3176 v2 = 1;
3177 v3 = 100 * v3 / v2;
3178 if (v3 > 100)
3179 v3 = 100;
3180 ch->value = v3;
3181 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
3182 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3183 (ch->selector << 8) | (ch->chnum + 1), state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3184 goto err;
3185 v1 = buf[0] | (buf[1] << 8);
3186 v3 = v1 - ch->minval;
3187 v3 = 100 * v3 / v2;
3188 if (v3 > 100)
3189 v3 = 100;
3190 }
3191 ch->value |= v3 << 8;
3192 break;
3193
3194 case BASS_CONTROL:
3195 case MID_CONTROL:
3196 case TREBLE_CONTROL:
3197 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3198 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3199 goto err;
3200 ch->minval = buf[0] << 8;
3201 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3202 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3203 goto err;
3204 ch->maxval = buf[0] << 8;
3205 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3206 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3207 goto err;
3208 v1 = buf[0] << 8;
3209 v2 = ch->maxval - ch->minval;
3210 v3 = v1 - ch->minval;
3211 if (!v2)
3212 v2 = 1;
3213 v3 = 100 * v3 / v2;
3214 if (v3 > 100)
3215 v3 = 100;
3216 ch->value = v3;
3217 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
3218 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3219 (ch->selector << 8) | (ch->chnum + 1), state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3220 goto err;
3221 v1 = buf[0] << 8;
3222 v3 = v1 - ch->minval;
3223 v3 = 100 * v3 / v2;
3224 if (v3 > 100)
3225 v3 = 100;
3226 }
3227 ch->value |= v3 << 8;
3228 break;
3229
3230 default:
3231 goto err;
3232 }
3233
3234 freebuf:
3235 kfree(buf);
3236 return;
3237 err:
3238 printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n",
3239 dev->devnum, state->ctrlif, ch->unitid, ch->chnum, ch->selector);
3240 if (state->nrmixch)
3241 state->nrmixch--;
3242 goto freebuf;
3243}
3244
3245
3246static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid);
3247
3248static inline int checkmixbmap(unsigned char *bmap, unsigned char flg, unsigned int inidx, unsigned int numoch)
3249{
3250 unsigned int idx;
3251
3252 idx = inidx*numoch;
3253 if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7))))
3254 return 0;
3255 if (!(flg & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
3256 return 1;
3257 idx = (inidx+!!(flg & MIXFLG_STEREOIN))*numoch+!!(flg & MIXFLG_STEREOOUT);
3258 if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7))))
3259 return 0;
3260 return 1;
3261}
3262
3263static void usb_audio_mixerunit(struct consmixstate *state, unsigned char *mixer)
3264{
3265 unsigned int nroutch = mixer[5+mixer[4]];
3266 unsigned int chidx[SOUND_MIXER_NRDEVICES+1];
3267 unsigned int termt[SOUND_MIXER_NRDEVICES];
3268 unsigned char flg = (nroutch >= 2) ? MIXFLG_STEREOOUT : 0;
3269 unsigned char *bmap = &mixer[9+mixer[4]];
3270 unsigned int bmapsize;
3271 struct mixerchannel *ch;
3272 unsigned int i;
3273
3274 if (!mixer[4]) {
3275 printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor\n", mixer[3]);
3276 return;
3277 }
3278 if (mixer[4] > SOUND_MIXER_NRDEVICES) {
3279 printk(KERN_ERR "usbaudio: mixer unit %u: too many input pins\n", mixer[3]);
3280 return;
3281 }
3282 chidx[0] = 0;
3283 for (i = 0; i < mixer[4]; i++) {
3284 usb_audio_recurseunit(state, mixer[5+i]);
3285 chidx[i+1] = chidx[i] + state->nrchannels;
3286 termt[i] = state->termtype;
3287 }
3288 state->termtype = 0;
3289 state->chconfig = mixer[6+mixer[4]] | (mixer[7+mixer[4]] << 8);
3290 bmapsize = (nroutch * chidx[mixer[4]] + 7) >> 3;
3291 bmap += bmapsize - 1;
3292 if (mixer[0] < 10+mixer[4]+bmapsize) {
3293 printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor (bitmap too small)\n", mixer[3]);
3294 return;
3295 }
3296 for (i = 0; i < mixer[4]; i++) {
3297 state->termtype = termt[i];
3298 if (chidx[i+1]-chidx[i] >= 2) {
3299 flg |= MIXFLG_STEREOIN;
3300 if (checkmixbmap(bmap, flg, chidx[i], nroutch)) {
3301 ch = getmixchannel(state, getvolchannel(state));
3302 if (ch) {
3303 ch->unitid = mixer[3];
3304 ch->selector = 0;
3305 ch->chnum = chidx[i]+1;
3306 ch->flags = flg;
3307 prepmixch(state);
3308 }
3309 continue;
3310 }
3311 }
3312 flg &= ~MIXFLG_STEREOIN;
3313 if (checkmixbmap(bmap, flg, chidx[i], nroutch)) {
3314 ch = getmixchannel(state, getvolchannel(state));
3315 if (ch) {
3316 ch->unitid = mixer[3];
3317 ch->selector = 0;
3318 ch->chnum = chidx[i]+1;
3319 ch->flags = flg;
3320 prepmixch(state);
3321 }
3322 }
3323 }
3324 state->termtype = 0;
3325}
3326
3327static struct mixerchannel *slctsrc_findunit(struct consmixstate *state, __u8 unitid)
3328{
3329 unsigned int i;
3330
3331 for (i = 0; i < state->nrmixch; i++)
3332 if (state->mixch[i].unitid == unitid)
3333 return &state->mixch[i];
3334 return NULL;
3335}
3336
3337static void usb_audio_selectorunit(struct consmixstate *state, unsigned char *selector)
3338{
3339 unsigned int chnum, i, mixch;
3340 struct mixerchannel *mch;
3341
3342 if (!selector[4]) {
3343 printk(KERN_ERR "usbaudio: unit %u invalid SELECTOR_UNIT descriptor\n", selector[3]);
3344 return;
3345 }
3346 mixch = state->nrmixch;
3347 usb_audio_recurseunit(state, selector[5]);
3348 if (state->nrmixch != mixch) {
3349 mch = &state->mixch[state->nrmixch-1];
3350 mch->slctunitid = selector[3] | (1 << 8);
3351 } else if ((mch = slctsrc_findunit(state, selector[5]))) {
3352 mch->slctunitid = selector[3] | (1 << 8);
3353 } else {
3354 printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel 1\n", selector[3]);
3355 }
3356 chnum = state->nrchannels;
3357 for (i = 1; i < selector[4]; i++) {
3358 mixch = state->nrmixch;
3359 usb_audio_recurseunit(state, selector[5+i]);
3360 if (chnum != state->nrchannels) {
3361 printk(KERN_ERR "usbaudio: selector unit %u: input pins with varying channel numbers\n", selector[3]);
3362 state->termtype = 0;
3363 state->chconfig = 0;
3364 state->nrchannels = 0;
3365 return;
3366 }
3367 if (state->nrmixch != mixch) {
3368 mch = &state->mixch[state->nrmixch-1];
3369 mch->slctunitid = selector[3] | ((i + 1) << 8);
3370 } else if ((mch = slctsrc_findunit(state, selector[5+i]))) {
3371 mch->slctunitid = selector[3] | ((i + 1) << 8);
3372 } else {
3373 printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel %u\n", selector[3], i+1);
3374 }
3375 }
3376 state->termtype = 0;
3377 state->chconfig = 0;
3378}
3379
3380/* in the future we might try to handle 3D etc. effect units */
3381
3382static void usb_audio_processingunit(struct consmixstate *state, unsigned char *proc)
3383{
3384 unsigned int i;
3385
3386 for (i = 0; i < proc[6]; i++)
3387 usb_audio_recurseunit(state, proc[7+i]);
3388 state->nrchannels = proc[7+proc[6]];
3389 state->termtype = 0;
3390 state->chconfig = proc[8+proc[6]] | (proc[9+proc[6]] << 8);
3391}
3392
3393
3394/* See Audio Class Spec, section 4.3.2.5 */
3395static void usb_audio_featureunit(struct consmixstate *state, unsigned char *ftr)
3396{
3397 struct mixerchannel *ch;
3398 unsigned short chftr, mchftr;
3399#if 0
3400 struct usb_device *dev = state->s->usbdev;
3401 unsigned char data[1];
3402#endif
3403 unsigned char nr_logical_channels, i;
3404
3405 usb_audio_recurseunit(state, ftr[4]);
3406
3407 if (ftr[5] == 0 ) {
3408 printk(KERN_ERR "usbaudio: wrong controls size in feature unit %u\n",ftr[3]);
3409 return;
3410 }
3411
3412 if (state->nrchannels == 0) {
3413 printk(KERN_ERR "usbaudio: feature unit %u source has no channels\n", ftr[3]);
3414 return;
3415 }
3416 if (state->nrchannels > 2)
3417 printk(KERN_WARNING "usbaudio: feature unit %u: OSS mixer interface does not support more than 2 channels\n", ftr[3]);
3418
3419 nr_logical_channels=(ftr[0]-7)/ftr[5]-1;
3420
3421 if (nr_logical_channels != state->nrchannels) {
3422 printk(KERN_WARNING "usbaudio: warning: found %d of %d logical channels.\n", state->nrchannels,nr_logical_channels);
3423
3424 if (state->nrchannels == 1 && nr_logical_channels==0) {
3425 printk(KERN_INFO "usbaudio: assuming the channel found is the master channel (got a Philips camera?). Should be fine.\n");
3426 } else if (state->nrchannels == 1 && nr_logical_channels==2) {
3427 printk(KERN_INFO "usbaudio: assuming that a stereo channel connected directly to a mixer is missing in search (got Labtec headset?). Should be fine.\n");
3428 state->nrchannels=nr_logical_channels;
3429 } else {
3430 printk(KERN_WARNING "usbaudio: no idea what's going on..., contact linux-usb-devel@lists.sourceforge.net\n");
3431 }
3432 }
3433
3434 /* There is always a master channel */
3435 mchftr = ftr[6];
3436 /* Binary AND over logical channels if they exist */
3437 if (nr_logical_channels) {
3438 chftr = ftr[6+ftr[5]];
3439 for (i = 2; i <= nr_logical_channels; i++)
3440 chftr &= ftr[6+i*ftr[5]];
3441 } else {
3442 chftr = 0;
3443 }
3444
3445 /* volume control */
3446 if (chftr & 2) {
3447 ch = getmixchannel(state, getvolchannel(state));
3448 if (ch) {
3449 ch->unitid = ftr[3];
3450 ch->selector = VOLUME_CONTROL;
3451 ch->chnum = 1;
3452 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3453 prepmixch(state);
3454 }
3455 } else if (mchftr & 2) {
3456 ch = getmixchannel(state, getvolchannel(state));
3457 if (ch) {
3458 ch->unitid = ftr[3];
3459 ch->selector = VOLUME_CONTROL;
3460 ch->chnum = 0;
3461 ch->flags = 0;
3462 prepmixch(state);
3463 }
3464 }
3465 /* bass control */
3466 if (chftr & 4) {
3467 ch = getmixchannel(state, SOUND_MIXER_BASS);
3468 if (ch) {
3469 ch->unitid = ftr[3];
3470 ch->selector = BASS_CONTROL;
3471 ch->chnum = 1;
3472 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3473 prepmixch(state);
3474 }
3475 } else if (mchftr & 4) {
3476 ch = getmixchannel(state, SOUND_MIXER_BASS);
3477 if (ch) {
3478 ch->unitid = ftr[3];
3479 ch->selector = BASS_CONTROL;
3480 ch->chnum = 0;
3481 ch->flags = 0;
3482 prepmixch(state);
3483 }
3484 }
3485 /* treble control */
3486 if (chftr & 16) {
3487 ch = getmixchannel(state, SOUND_MIXER_TREBLE);
3488 if (ch) {
3489 ch->unitid = ftr[3];
3490 ch->selector = TREBLE_CONTROL;
3491 ch->chnum = 1;
3492 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3493 prepmixch(state);
3494 }
3495 } else if (mchftr & 16) {
3496 ch = getmixchannel(state, SOUND_MIXER_TREBLE);
3497 if (ch) {
3498 ch->unitid = ftr[3];
3499 ch->selector = TREBLE_CONTROL;
3500 ch->chnum = 0;
3501 ch->flags = 0;
3502 prepmixch(state);
3503 }
3504 }
3505#if 0
3506 /* if there are mute controls, unmute them */
3507 /* does not seem to be necessary, and the Dallas chip does not seem to support the "all" channel (255) */
3508 if ((chftr & 1) || (mchftr & 1)) {
3509 printk(KERN_DEBUG "usbaudio: unmuting feature unit %u interface %u\n", ftr[3], state->ctrlif);
3510 data[0] = 0;
3511 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
3512 (MUTE_CONTROL << 8) | 0xff, state->ctrlif | (ftr[3] << 8), data, 1, 1000) < 0)
3513 printk(KERN_WARNING "usbaudio: failure to unmute feature unit %u interface %u\n", ftr[3], state->ctrlif);
3514 }
3515#endif
3516}
3517
3518static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid)
3519{
3520 unsigned char *p1;
3521 unsigned int i, j;
3522
3523 if (test_and_set_bit(unitid, state->unitbitmap)) {
3524 printk(KERN_INFO "usbaudio: mixer path revisits unit %d\n", unitid);
3525 return;
3526 }
3527 p1 = find_audiocontrol_unit(state->buffer, state->buflen, NULL, unitid, state->ctrlif);
3528 if (!p1) {
3529 printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
3530 return;
3531 }
3532 state->nrchannels = 0;
3533 state->termtype = 0;
3534 state->chconfig = 0;
3535 switch (p1[2]) {
3536 case INPUT_TERMINAL:
3537 if (p1[0] < 12) {
3538 printk(KERN_ERR "usbaudio: unit %u: invalid INPUT_TERMINAL descriptor\n", unitid);
3539 return;
3540 }
3541 state->nrchannels = p1[7];
3542 state->termtype = p1[4] | (p1[5] << 8);
3543 state->chconfig = p1[8] | (p1[9] << 8);
3544 return;
3545
3546 case MIXER_UNIT:
3547 if (p1[0] < 10 || p1[0] < 10+p1[4]) {
3548 printk(KERN_ERR "usbaudio: unit %u: invalid MIXER_UNIT descriptor\n", unitid);
3549 return;
3550 }
3551 usb_audio_mixerunit(state, p1);
3552 return;
3553
3554 case SELECTOR_UNIT:
3555 if (p1[0] < 6 || p1[0] < 6+p1[4]) {
3556 printk(KERN_ERR "usbaudio: unit %u: invalid SELECTOR_UNIT descriptor\n", unitid);
3557 return;
3558 }
3559 usb_audio_selectorunit(state, p1);
3560 return;
3561
3562 case FEATURE_UNIT: /* See USB Audio Class Spec 4.3.2.5 */
3563 if (p1[0] < 7 || p1[0] < 7+p1[5]) {
3564 printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
3565 return;
3566 }
3567 usb_audio_featureunit(state, p1);
3568 return;
3569
3570 case PROCESSING_UNIT:
3571 if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) {
3572 printk(KERN_ERR "usbaudio: unit %u: invalid PROCESSING_UNIT descriptor\n", unitid);
3573 return;
3574 }
3575 usb_audio_processingunit(state, p1);
3576 return;
3577
3578 case EXTENSION_UNIT:
3579 if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) {
3580 printk(KERN_ERR "usbaudio: unit %u: invalid EXTENSION_UNIT descriptor\n", unitid);
3581 return;
3582 }
3583 for (j = i = 0; i < p1[6]; i++) {
3584 usb_audio_recurseunit(state, p1[7+i]);
3585 if (!i)
3586 j = state->termtype;
3587 else if (j != state->termtype)
3588 j = 0;
3589 }
3590 state->nrchannels = p1[7+p1[6]];
3591 state->chconfig = p1[8+p1[6]] | (p1[9+p1[6]] << 8);
3592 state->termtype = j;
3593 return;
3594
3595 default:
3596 printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
3597 return;
3598 }
3599}
3600
3601static void usb_audio_constructmixer(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif, unsigned char *oterm)
3602{
3603 struct usb_mixerdev *ms;
3604 struct consmixstate state;
3605
3606 memset(&state, 0, sizeof(state));
3607 state.s = s;
3608 state.nrmixch = 0;
3609 state.mixchmask = ~0;
3610 state.buffer = buffer;
3611 state.buflen = buflen;
3612 state.ctrlif = ctrlif;
3613 set_bit(oterm[3], state.unitbitmap); /* mark terminal ID as visited */
3614 printk(KERN_DEBUG "usbaudio: constructing mixer for Terminal %u type 0x%04x\n",
3615 oterm[3], oterm[4] | (oterm[5] << 8));
3616 usb_audio_recurseunit(&state, oterm[7]);
3617 if (!state.nrmixch) {
3618 printk(KERN_INFO "usbaudio: no mixer controls found for Terminal %u\n", oterm[3]);
3619 return;
3620 }
3621 if (!(ms = kmalloc(sizeof(struct usb_mixerdev)+state.nrmixch*sizeof(struct mixerchannel), GFP_KERNEL)))
3622 return;
3623 memset(ms, 0, sizeof(struct usb_mixerdev));
3624 memcpy(&ms->ch, &state.mixch, state.nrmixch*sizeof(struct mixerchannel));
3625 ms->state = s;
3626 ms->iface = ctrlif;
3627 ms->numch = state.nrmixch;
3628 if ((ms->dev_mixer = register_sound_mixer(&usb_mixer_fops, -1)) < 0) {
3629 printk(KERN_ERR "usbaudio: cannot register mixer\n");
3630 kfree(ms);
3631 return;
3632 }
3633 printk(KERN_INFO "usbaudio: registered mixer 14,%d\n", ms->dev_mixer);
3634 list_add_tail(&ms->list, &s->mixerlist);
3635}
3636
3637/* arbitrary limit, we won't check more interfaces than this */
3638#define USB_MAXINTERFACES 32
3639
3640static struct usb_audio_state *usb_audio_parsecontrol(struct usb_device *dev, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif)
3641{
3642 struct usb_audio_state *s;
3643 struct usb_interface *iface;
3644 struct usb_host_interface *alt;
3645 unsigned char ifin[USB_MAXINTERFACES], ifout[USB_MAXINTERFACES];
3646 unsigned char *p1;
3647 unsigned int i, j, k, numifin = 0, numifout = 0;
3648
3649 if (!(s = kmalloc(sizeof(struct usb_audio_state), GFP_KERNEL)))
3650 return NULL;
3651 memset(s, 0, sizeof(struct usb_audio_state));
3652 INIT_LIST_HEAD(&s->audiolist);
3653 INIT_LIST_HEAD(&s->mixerlist);
3654 s->usbdev = dev;
3655 s->count = 1;
3656
3657 /* find audiocontrol interface */
3658 if (!(p1 = find_csinterface_descriptor(buffer, buflen, NULL, HEADER, ctrlif, -1))) {
3659 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u no HEADER found\n",
3660 dev->devnum, ctrlif);
3661 goto ret;
3662 }
3663 if (p1[0] < 8 + p1[7]) {
3664 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u HEADER error\n",
3665 dev->devnum, ctrlif);
3666 goto ret;
3667 }
3668 if (!p1[7])
3669 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has no AudioStreaming and MidiStreaming interfaces\n",
3670 dev->devnum, ctrlif);
3671 for (i = 0; i < p1[7]; i++) {
3672 j = p1[8+i];
3673 iface = usb_ifnum_to_if(dev, j);
3674 if (!iface) {
3675 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u does not exist\n",
3676 dev->devnum, ctrlif, j);
3677 continue;
3678 }
3679 if (iface->num_altsetting == 1) {
3680 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u has only 1 altsetting.\n", dev->devnum, ctrlif);
3681 continue;
3682 }
3683 alt = usb_altnum_to_altsetting(iface, 0);
3684 if (!alt) {
3685 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 0\n",
3686 dev->devnum, ctrlif, j);
3687 continue;
3688 }
3689 if (alt->desc.bInterfaceClass != USB_CLASS_AUDIO) {
3690 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u is not an AudioClass interface\n",
3691 dev->devnum, ctrlif, j);
3692 continue;
3693 }
3694 if (alt->desc.bInterfaceSubClass == 3) {
3695 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u interface %u MIDIStreaming not supported\n",
3696 dev->devnum, ctrlif, j);
3697 continue;
3698 }
3699 if (alt->desc.bInterfaceSubClass != 2) {
3700 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u invalid AudioClass subtype\n",
3701 dev->devnum, ctrlif, j);
3702 continue;
3703 }
3704 if (alt->desc.bNumEndpoints > 0) {
3705 /* Check all endpoints; should they all have a bandwidth of 0 ? */
3706 for (k = 0; k < alt->desc.bNumEndpoints; k++) {
3707 if (le16_to_cpu(alt->endpoint[k].desc.wMaxPacketSize) > 0) {
3708 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u endpoint %d does not have 0 bandwidth at alt[0]\n", dev->devnum, ctrlif, k);
3709 break;
3710 }
3711 }
3712 if (k < alt->desc.bNumEndpoints)
3713 continue;
3714 }
3715
3716 alt = usb_altnum_to_altsetting(iface, 1);
3717 if (!alt) {
3718 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 1\n",
3719 dev->devnum, ctrlif, j);
3720 continue;
3721 }
3722 if (alt->desc.bNumEndpoints < 1) {
3723 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no endpoint\n",
3724 dev->devnum, ctrlif, j);
3725 continue;
3726 }
3727 /* note: this requires the data endpoint to be ep0 and the optional sync
3728 ep to be ep1, which seems to be the case */
3729 if (alt->endpoint[0].desc.bEndpointAddress & USB_DIR_IN) {
3730 if (numifin < USB_MAXINTERFACES) {
3731 ifin[numifin++] = j;
3732 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1);
3733 }
3734 } else {
3735 if (numifout < USB_MAXINTERFACES) {
3736 ifout[numifout++] = j;
3737 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1);
3738 }
3739 }
3740 }
3741 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has %u input and %u output AudioStreaming interfaces\n",
3742 dev->devnum, ctrlif, numifin, numifout);
3743 for (i = 0; i < numifin && i < numifout; i++)
3744 usb_audio_parsestreaming(s, buffer, buflen, ifin[i], ifout[i]);
3745 for (j = i; j < numifin; j++)
3746 usb_audio_parsestreaming(s, buffer, buflen, ifin[i], -1);
3747 for (j = i; j < numifout; j++)
3748 usb_audio_parsestreaming(s, buffer, buflen, -1, ifout[i]);
3749 /* now walk through all OUTPUT_TERMINAL descriptors to search for mixers */
3750 p1 = find_csinterface_descriptor(buffer, buflen, NULL, OUTPUT_TERMINAL, ctrlif, -1);
3751 while (p1) {
3752 if (p1[0] >= 9)
3753 usb_audio_constructmixer(s, buffer, buflen, ctrlif, p1);
3754 p1 = find_csinterface_descriptor(buffer, buflen, p1, OUTPUT_TERMINAL, ctrlif, -1);
3755 }
3756
3757ret:
3758 if (list_empty(&s->audiolist) && list_empty(&s->mixerlist)) {
3759 kfree(s);
3760 return NULL;
3761 }
3762 /* everything successful */
3763 down(&open_sem);
3764 list_add_tail(&s->audiodev, &audiodevs);
3765 up(&open_sem);
3766 printk(KERN_DEBUG "usb_audio_parsecontrol: usb_audio_state at %p\n", s);
3767 return s;
3768}
3769
3770/* we only care for the currently active configuration */
3771
3772static int usb_audio_probe(struct usb_interface *intf,
3773 const struct usb_device_id *id)
3774{
3775 struct usb_device *dev = interface_to_usbdev (intf);
3776 struct usb_audio_state *s;
3777 unsigned char *buffer;
3778 unsigned int buflen;
3779
3780#if 0
3781 printk(KERN_DEBUG "usbaudio: Probing if %i: IC %x, ISC %x\n", ifnum,
3782 config->interface[ifnum].altsetting[0].desc.bInterfaceClass,
3783 config->interface[ifnum].altsetting[0].desc.bInterfaceSubClass);
3784#endif
3785
3786 /*
3787 * audiocontrol interface found
3788 * find which configuration number is active
3789 */
3790 buffer = dev->rawdescriptors[dev->actconfig - dev->config];
3791 buflen = le16_to_cpu(dev->actconfig->desc.wTotalLength);
3792 s = usb_audio_parsecontrol(dev, buffer, buflen, intf->altsetting->desc.bInterfaceNumber);
3793 if (s) {
3794 usb_set_intfdata (intf, s);
3795 return 0;
3796 }
3797 return -ENODEV;
3798}
3799
3800
3801/* a revoke facility would make things simpler */
3802
3803static void usb_audio_disconnect(struct usb_interface *intf)
3804{
3805 struct usb_audio_state *s = usb_get_intfdata (intf);
3806 struct usb_audiodev *as;
3807 struct usb_mixerdev *ms;
3808
3809 if (!s)
3810 return;
3811
3812 /* we get called with -1 for every audiostreaming interface registered */
3813 if (s == (struct usb_audio_state *)-1) {
3814 dprintk((KERN_DEBUG "usbaudio: note, usb_audio_disconnect called with -1\n"));
3815 return;
3816 }
3817 if (!s->usbdev) {
3818 dprintk((KERN_DEBUG "usbaudio: error, usb_audio_disconnect already called for %p!\n", s));
3819 return;
3820 }
3821 down(&open_sem);
3822 list_del_init(&s->audiodev);
3823 s->usbdev = NULL;
3824 usb_set_intfdata (intf, NULL);
3825
3826 /* deregister all audio and mixer devices, so no new processes can open this device */
3827 list_for_each_entry(as, &s->audiolist, list) {
3828 usbin_disc(as);
3829 usbout_disc(as);
3830 wake_up(&as->usbin.dma.wait);
3831 wake_up(&as->usbout.dma.wait);
3832 if (as->dev_audio >= 0) {
3833 unregister_sound_dsp(as->dev_audio);
3834 printk(KERN_INFO "usbaudio: unregister dsp 14,%d\n", as->dev_audio);
3835 }
3836 as->dev_audio = -1;
3837 }
3838 list_for_each_entry(ms, &s->mixerlist, list) {
3839 if (ms->dev_mixer >= 0) {
3840 unregister_sound_mixer(ms->dev_mixer);
3841 printk(KERN_INFO "usbaudio: unregister mixer 14,%d\n", ms->dev_mixer);
3842 }
3843 ms->dev_mixer = -1;
3844 }
3845 release(s);
3846 wake_up(&open_wait);
3847}
3848
3849static int __init usb_audio_init(void)
3850{
3851 int result = usb_register(&usb_audio_driver);
3852 if (result == 0)
3853 info(DRIVER_VERSION ":" DRIVER_DESC);
3854 return result;
3855}
3856
3857
3858static void __exit usb_audio_cleanup(void)
3859{
3860 usb_deregister(&usb_audio_driver);
3861}
3862
3863module_init(usb_audio_init);
3864module_exit(usb_audio_cleanup);
3865
3866MODULE_AUTHOR( DRIVER_AUTHOR );
3867MODULE_DESCRIPTION( DRIVER_DESC );
3868MODULE_LICENSE("GPL");
3869
diff --git a/drivers/usb/class/audio.h b/drivers/usb/class/audio.h
deleted file mode 100644
index 45916eb12103..000000000000
--- a/drivers/usb/class/audio.h
+++ /dev/null
@@ -1,110 +0,0 @@
1#define CS_AUDIO_UNDEFINED 0x20
2#define CS_AUDIO_DEVICE 0x21
3#define CS_AUDIO_CONFIGURATION 0x22
4#define CS_AUDIO_STRING 0x23
5#define CS_AUDIO_INTERFACE 0x24
6#define CS_AUDIO_ENDPOINT 0x25
7
8#define HEADER 0x01
9#define INPUT_TERMINAL 0x02
10#define OUTPUT_TERMINAL 0x03
11#define MIXER_UNIT 0x04
12#define SELECTOR_UNIT 0x05
13#define FEATURE_UNIT 0x06
14#define PROCESSING_UNIT 0x07
15#define EXTENSION_UNIT 0x08
16
17#define AS_GENERAL 0x01
18#define FORMAT_TYPE 0x02
19#define FORMAT_SPECIFIC 0x03
20
21#define EP_GENERAL 0x01
22
23#define MAX_CHAN 9
24#define MAX_FREQ 16
25#define MAX_IFACE 8
26#define MAX_FORMAT 8
27#define MAX_ALT 32 /* Sorry, we need quite a few for the Philips webcams */
28
29struct usb_audio_terminal
30{
31 u8 flags;
32 u8 assoc;
33 u16 type; /* Mic etc */
34 u8 channels;
35 u8 source;
36 u16 chancfg;
37};
38
39struct usb_audio_format
40{
41 u8 type;
42 u8 channels;
43 u8 num_freq;
44 u8 sfz;
45 u8 bits;
46 u16 freq[MAX_FREQ];
47};
48
49struct usb_audio_interface
50{
51 u8 terminal;
52 u8 delay;
53 u16 num_formats;
54 u16 format_type;
55 u8 flags;
56 u8 idleconf; /* Idle config */
57#define AU_IFACE_FOUND 1
58 struct usb_audio_format format[MAX_FORMAT];
59};
60
61struct usb_audio_device
62{
63 struct list_head list;
64 u8 mixer;
65 u8 selector;
66 void *irq_handle;
67 u8 num_channels;
68 u8 num_dsp_iface;
69 u8 channel_map[MAX_CHAN];
70 struct usb_audio_terminal terminal[MAX_CHAN];
71 struct usb_audio_interface interface[MAX_IFACE][MAX_ALT];
72};
73
74
75
76/* Audio Class specific Request Codes */
77
78#define SET_CUR 0x01
79#define GET_CUR 0x81
80#define SET_MIN 0x02
81#define GET_MIN 0x82
82#define SET_MAX 0x03
83#define GET_MAX 0x83
84#define SET_RES 0x04
85#define GET_RES 0x84
86#define SET_MEM 0x05
87#define GET_MEM 0x85
88#define GET_STAT 0xff
89
90/* Terminal Control Selectors */
91
92#define COPY_PROTECT_CONTROL 0x01
93
94/* Feature Unit Control Selectors */
95
96#define MUTE_CONTROL 0x01
97#define VOLUME_CONTROL 0x02
98#define BASS_CONTROL 0x03
99#define MID_CONTROL 0x04
100#define TREBLE_CONTROL 0x05
101#define GRAPHIC_EQUALIZER_CONTROL 0x06
102#define AUTOMATIC_GAIN_CONTROL 0x07
103#define DELAY_CONTROL 0x08
104#define BASS_BOOST_CONTROL 0x09
105#define LOUDNESS_CONTROL 0x0a
106
107/* Endpoint Control Selectors */
108
109#define SAMPLING_FREQ_CONTROL 0x01
110#define PITCH_CONTROL 0x02
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index 97bdeb1c2181..6dd339f4c0fc 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -60,6 +60,7 @@
60#include <linux/tty_flip.h> 60#include <linux/tty_flip.h>
61#include <linux/module.h> 61#include <linux/module.h>
62#include <linux/smp_lock.h> 62#include <linux/smp_lock.h>
63#include <linux/mutex.h>
63#include <asm/uaccess.h> 64#include <asm/uaccess.h>
64#include <linux/usb.h> 65#include <linux/usb.h>
65#include <linux/usb_cdc.h> 66#include <linux/usb_cdc.h>
@@ -80,7 +81,7 @@ static struct usb_driver acm_driver;
80static struct tty_driver *acm_tty_driver; 81static struct tty_driver *acm_tty_driver;
81static struct acm *acm_table[ACM_TTY_MINORS]; 82static struct acm *acm_table[ACM_TTY_MINORS];
82 83
83static DECLARE_MUTEX(open_sem); 84static DEFINE_MUTEX(open_mutex);
84 85
85#define ACM_READY(acm) (acm && acm->dev && acm->used) 86#define ACM_READY(acm) (acm && acm->dev && acm->used)
86 87
@@ -431,8 +432,8 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
431 int rv = -EINVAL; 432 int rv = -EINVAL;
432 int i; 433 int i;
433 dbg("Entering acm_tty_open.\n"); 434 dbg("Entering acm_tty_open.\n");
434 435
435 down(&open_sem); 436 mutex_lock(&open_mutex);
436 437
437 acm = acm_table[tty->index]; 438 acm = acm_table[tty->index];
438 if (!acm || !acm->dev) 439 if (!acm || !acm->dev)
@@ -474,14 +475,14 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
474 475
475done: 476done:
476err_out: 477err_out:
477 up(&open_sem); 478 mutex_unlock(&open_mutex);
478 return rv; 479 return rv;
479 480
480full_bailout: 481full_bailout:
481 usb_kill_urb(acm->ctrlurb); 482 usb_kill_urb(acm->ctrlurb);
482bail_out: 483bail_out:
483 acm->used--; 484 acm->used--;
484 up(&open_sem); 485 mutex_unlock(&open_mutex);
485 return -EIO; 486 return -EIO;
486} 487}
487 488
@@ -507,7 +508,7 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
507 if (!acm || !acm->used) 508 if (!acm || !acm->used)
508 return; 509 return;
509 510
510 down(&open_sem); 511 mutex_lock(&open_mutex);
511 if (!--acm->used) { 512 if (!--acm->used) {
512 if (acm->dev) { 513 if (acm->dev) {
513 acm_set_control(acm, acm->ctrlout = 0); 514 acm_set_control(acm, acm->ctrlout = 0);
@@ -518,7 +519,7 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
518 } else 519 } else
519 acm_tty_unregister(acm); 520 acm_tty_unregister(acm);
520 } 521 }
521 up(&open_sem); 522 mutex_unlock(&open_mutex);
522} 523}
523 524
524static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) 525static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
@@ -1013,9 +1014,9 @@ static void acm_disconnect(struct usb_interface *intf)
1013 return; 1014 return;
1014 } 1015 }
1015 1016
1016 down(&open_sem); 1017 mutex_lock(&open_mutex);
1017 if (!usb_get_intfdata(intf)) { 1018 if (!usb_get_intfdata(intf)) {
1018 up(&open_sem); 1019 mutex_unlock(&open_mutex);
1019 return; 1020 return;
1020 } 1021 }
1021 acm->dev = NULL; 1022 acm->dev = NULL;
@@ -1045,11 +1046,11 @@ static void acm_disconnect(struct usb_interface *intf)
1045 1046
1046 if (!acm->used) { 1047 if (!acm->used) {
1047 acm_tty_unregister(acm); 1048 acm_tty_unregister(acm);
1048 up(&open_sem); 1049 mutex_unlock(&open_mutex);
1049 return; 1050 return;
1050 } 1051 }
1051 1052
1052 up(&open_sem); 1053 mutex_unlock(&open_mutex);
1053 1054
1054 if (acm->tty) 1055 if (acm->tty)
1055 tty_hangup(acm->tty); 1056 tty_hangup(acm->tty);
diff --git a/drivers/usb/class/usb-midi.c b/drivers/usb/class/usb-midi.c
deleted file mode 100644
index f13f004d311f..000000000000
--- a/drivers/usb/class/usb-midi.c
+++ /dev/null
@@ -1,2153 +0,0 @@
1/*
2 usb-midi.c -- USB-MIDI driver
3
4 Copyright (C) 2001
5 NAGANO Daisuke <breeze.nagano@nifty.ne.jp>
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, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 This driver is based on:
22 - 'Universal Serial Bus Device Class Definition for MIDI Device'
23 - linux/drivers/sound/es1371.c, linux/drivers/usb/audio.c
24 - alsa/lowlevel/pci/cs64xx.c
25 - umidi.c for NetBSD
26 */
27
28/* ------------------------------------------------------------------------- */
29
30
31#include <linux/module.h>
32#include <linux/kernel.h>
33#include <linux/sched.h>
34#include <linux/list.h>
35#include <linux/slab.h>
36#include <linux/usb.h>
37#include <linux/poll.h>
38#include <linux/sound.h>
39#include <linux/init.h>
40#include <asm/semaphore.h>
41
42#include "usb-midi.h"
43
44/* ------------------------------------------------------------------------- */
45
46/* More verbose on syslog */
47#undef MIDI_DEBUG
48
49#define MIDI_IN_BUFSIZ 1024
50
51#define HAVE_SUPPORT_USB_MIDI_CLASS
52
53#undef HAVE_SUPPORT_ALSA
54
55/* ------------------------------------------------------------------------- */
56
57static int singlebyte = 0;
58module_param(singlebyte, int, 0);
59MODULE_PARM_DESC(singlebyte,"Enable sending MIDI messages with single message packet");
60
61static int maxdevices = 4;
62module_param(maxdevices, int, 0);
63MODULE_PARM_DESC(maxdevices,"Max number of allocatable MIDI device");
64
65static int uvendor = -1;
66module_param(uvendor, int, 0);
67MODULE_PARM_DESC(uvendor, "The USB Vendor ID of a semi-compliant interface");
68
69static int uproduct = -1;
70module_param(uproduct, int, 0);
71MODULE_PARM_DESC(uproduct, "The USB Product ID of a semi-compliant interface");
72
73static int uinterface = -1;
74module_param(uinterface, int, 0);
75MODULE_PARM_DESC(uinterface, "The Interface number of a semi-compliant interface");
76
77static int ualt = -1;
78module_param(ualt, int, 0);
79MODULE_PARM_DESC(ualt, "The optional alternative setting of a semi-compliant interface");
80
81static int umin = -1;
82module_param(umin, int, 0);
83MODULE_PARM_DESC(umin, "The input endpoint of a semi-compliant interface");
84
85static int umout = -1;
86module_param(umout, int, 0);
87MODULE_PARM_DESC(umout, "The output endpoint of a semi-compliant interface");
88
89static int ucable = -1;
90module_param(ucable, int, 0);
91MODULE_PARM_DESC(ucable, "The cable number used for a semi-compliant interface");
92
93/** Note -- the usb_string() returns only Latin-1 characters.
94 * (unicode chars <= 255). To support Japanese, a unicode16LE-to-EUC or
95 * unicode16LE-to-JIS routine is needed to wrap around usb_get_string().
96 **/
97static unsigned short ulangid = 0x0409; /** 0x0411 for Japanese **/
98module_param(ulangid, ushort, 0);
99MODULE_PARM_DESC(ulangid, "The optional preferred USB Language ID for all devices");
100
101MODULE_AUTHOR("NAGANO Daisuke <breeze.nagano@nifty.ne.jp>");
102MODULE_DESCRIPTION("USB-MIDI driver");
103MODULE_LICENSE("GPL");
104
105/* ------------------------------------------------------------------------- */
106
107/** MIDIStreaming Class-Specific Interface Descriptor Subtypes **/
108
109#define MS_DESCRIPTOR_UNDEFINED 0
110#define MS_HEADER 1
111#define MIDI_IN_JACK 2
112#define MIDI_OUT_JACK 3
113/* Spec reads: ELEMENT */
114#define ELEMENT_DESCRIPTOR 4
115
116#define MS_HEADER_LENGTH 7
117
118/** MIDIStreaming Class-Specific Endpoint Descriptor Subtypes **/
119
120#define DESCRIPTOR_UNDEFINED 0
121/* Spec reads: MS_GENERAL */
122#define MS_GENERAL_ENDPOINT 1
123
124/** MIDIStreaming MIDI IN and OUT Jack Types **/
125
126#define JACK_TYPE_UNDEFINED 0
127/* Spec reads: EMBEDDED */
128#define EMBEDDED_JACK 1
129/* Spec reads: EXTERNAL */
130#define EXTERNAL_JACK 2
131
132
133/* structure summary
134
135 usb_midi_state usb_device
136 | |
137 *| *| per ep
138 in_ep out_ep
139 | |
140 *| *| per cable
141 min mout
142 | | (cable to device pairing magic)
143 | |
144 usb_midi_dev dev_id (major,minor) == file->private_data
145
146*/
147
148/* usb_midi_state: corresponds to a USB-MIDI module */
149struct usb_midi_state {
150 struct list_head mididev;
151
152 struct usb_device *usbdev;
153
154 struct list_head midiDevList;
155 struct list_head inEndpointList;
156 struct list_head outEndpointList;
157
158 spinlock_t lock;
159
160 unsigned int count; /* usage counter */
161};
162
163/* midi_out_endpoint: corresponds to an output endpoint */
164struct midi_out_endpoint {
165 struct list_head list;
166
167 struct usb_device *usbdev;
168 int endpoint;
169 spinlock_t lock;
170 wait_queue_head_t wait;
171
172 unsigned char *buf;
173 int bufWrPtr;
174 int bufSize;
175
176 struct urb *urb;
177};
178
179/* midi_in_endpoint: corresponds to an input endpoint */
180struct midi_in_endpoint {
181 struct list_head list;
182
183 struct usb_device *usbdev;
184 int endpoint;
185 spinlock_t lock;
186 wait_queue_head_t wait;
187
188 struct usb_mididev *cables[16]; // cables open for read
189 int readers; // number of cables open for read
190
191 struct urb *urb;
192 unsigned char *recvBuf;
193 int recvBufSize;
194 int urbSubmitted; //FIXME: == readers > 0
195};
196
197/* usb_mididev: corresponds to a logical device */
198struct usb_mididev {
199 struct list_head list;
200
201 struct usb_midi_state *midi;
202 int dev_midi;
203 mode_t open_mode;
204
205 struct {
206 struct midi_in_endpoint *ep;
207 int cableId;
208
209// as we are pushing data from usb_bulk_read to usb_midi_read,
210// we need a larger, cyclic buffer here.
211 unsigned char buf[MIDI_IN_BUFSIZ];
212 int bufRdPtr;
213 int bufWrPtr;
214 int bufRemains;
215 } min;
216
217 struct {
218 struct midi_out_endpoint *ep;
219 int cableId;
220
221 unsigned char buf[3];
222 int bufPtr;
223 int bufRemains;
224
225 int isInExclusive;
226 unsigned char lastEvent;
227 } mout;
228
229 int singlebyte;
230};
231
232/** Map the high nybble of MIDI voice messages to number of Message bytes.
233 * High nyble ranges from 0x8 to 0xe
234 */
235
236static int remains_80e0[] = {
237 3, /** 0x8X Note Off **/
238 3, /** 0x9X Note On **/
239 3, /** 0xAX Poly-key pressure **/
240 3, /** 0xBX Control Change **/
241 2, /** 0xCX Program Change **/
242 2, /** 0xDX Channel pressure **/
243 3 /** 0xEX PitchBend Change **/
244};
245
246/** Map the messages to a number of Message bytes.
247 *
248 **/
249static int remains_f0f6[] = {
250 0, /** 0xF0 **/
251 2, /** 0XF1 **/
252 3, /** 0XF2 **/
253 2, /** 0XF3 **/
254 2, /** 0XF4 (Undefined by MIDI Spec, and subject to change) **/
255 2, /** 0XF5 (Undefined by MIDI Spec, and subject to change) **/
256 1 /** 0XF6 **/
257};
258
259/** Map the messages to a CIN (Code Index Number).
260 *
261 **/
262static int cin_f0ff[] = {
263 4, /** 0xF0 System Exclusive Message Start (special cases may be 6 or 7) */
264 2, /** 0xF1 **/
265 3, /** 0xF2 **/
266 2, /** 0xF3 **/
267 2, /** 0xF4 **/
268 2, /** 0xF5 **/
269 5, /** 0xF6 **/
270 5, /** 0xF7 End of System Exclusive Message (May be 6 or 7) **/
271 5, /** 0xF8 **/
272 5, /** 0xF9 **/
273 5, /** 0xFA **/
274 5, /** 0xFB **/
275 5, /** 0xFC **/
276 5, /** 0xFD **/
277 5, /** 0xFE **/
278 5 /** 0xFF **/
279};
280
281/** Map MIDIStreaming Event packet Code Index Number (low nybble of byte 0)
282 * to the number of bytes of valid MIDI data.
283 *
284 * CIN of 0 and 1 are NOT USED in MIDIStreaming 1.0.
285 *
286 **/
287static int cin_to_len[] = {
288 0, 0, 2, 3,
289 3, 1, 2, 3,
290 3, 3, 3, 3,
291 2, 2, 3, 1
292};
293
294
295/* ------------------------------------------------------------------------- */
296
297static struct list_head mididevs = LIST_HEAD_INIT(mididevs);
298
299static DECLARE_MUTEX(open_sem);
300static DECLARE_WAIT_QUEUE_HEAD(open_wait);
301
302
303/* ------------------------------------------------------------------------- */
304
305static void usb_write_callback(struct urb *urb, struct pt_regs *regs)
306{
307 struct midi_out_endpoint *ep = (struct midi_out_endpoint *)urb->context;
308
309 if ( waitqueue_active( &ep->wait ) )
310 wake_up_interruptible( &ep->wait );
311}
312
313
314static int usb_write( struct midi_out_endpoint *ep, unsigned char *buf, int len )
315{
316 struct usb_device *d;
317 int pipe;
318 int ret = 0;
319 int status;
320 int maxretry = 50;
321
322 DECLARE_WAITQUEUE(wait,current);
323 init_waitqueue_head(&ep->wait);
324
325 d = ep->usbdev;
326 pipe = usb_sndbulkpipe(d, ep->endpoint);
327 usb_fill_bulk_urb( ep->urb, d, pipe, (unsigned char*)buf, len,
328 usb_write_callback, ep );
329
330 status = usb_submit_urb(ep->urb, GFP_KERNEL);
331
332 if (status) {
333 printk(KERN_ERR "usbmidi: Cannot submit urb (%d)\n",status);
334 ret = -EIO;
335 goto error;
336 }
337
338 add_wait_queue( &ep->wait, &wait );
339 set_current_state( TASK_INTERRUPTIBLE );
340
341 while( ep->urb->status == -EINPROGRESS ) {
342 if ( maxretry-- < 0 ) {
343 printk(KERN_ERR "usbmidi: usb_bulk_msg timed out\n");
344 ret = -ETIME;
345 break;
346 }
347 interruptible_sleep_on_timeout( &ep->wait, 10 );
348 }
349 set_current_state( TASK_RUNNING );
350 remove_wait_queue( &ep->wait, &wait );
351
352error:
353 return ret;
354}
355
356
357/** Copy data from URB to In endpoint buf.
358 * Discard if CIN == 0 or CIN = 1.
359 *
360 *
361 **/
362
363static void usb_bulk_read(struct urb *urb, struct pt_regs *regs)
364{
365 struct midi_in_endpoint *ep = (struct midi_in_endpoint *)(urb->context);
366 unsigned char *data = urb->transfer_buffer;
367 int i, j, wake;
368
369 if ( !ep->urbSubmitted ) {
370 return;
371 }
372
373 if ( (urb->status == 0) && (urb->actual_length > 0) ) {
374 wake = 0;
375 spin_lock( &ep->lock );
376
377 for(j = 0; j < urb->actual_length; j += 4) {
378 int cin = (data[j]>>0)&0xf;
379 int cab = (data[j]>>4)&0xf;
380 struct usb_mididev *cable = ep->cables[cab];
381 if ( cable ) {
382 int len = cin_to_len[cin]; /** length of MIDI data **/
383 for (i = 0; i < len; i++) {
384 cable->min.buf[cable->min.bufWrPtr] = data[1+i+j];
385 cable->min.bufWrPtr = (cable->min.bufWrPtr+1)%MIDI_IN_BUFSIZ;
386 if (cable->min.bufRemains < MIDI_IN_BUFSIZ)
387 cable->min.bufRemains += 1;
388 else /** need to drop data **/
389 cable->min.bufRdPtr += (cable->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
390 wake = 1;
391 }
392 }
393 }
394
395 spin_unlock ( &ep->lock );
396 if ( wake ) {
397 wake_up( &ep->wait );
398 }
399 }
400
401 /* urb->dev must be reinitialized on 2.4.x kernels */
402 urb->dev = ep->usbdev;
403
404 urb->actual_length = 0;
405 usb_submit_urb(urb, GFP_ATOMIC);
406}
407
408
409
410/* ------------------------------------------------------------------------- */
411
412/* This routine must be called with spin_lock */
413
414/** Wrapper around usb_write().
415 * This routine must be called with spin_lock held on ep.
416 * Called by midiWrite(), putOneMidiEvent(), and usb_midi_write();
417 **/
418static int flush_midi_buffer( struct midi_out_endpoint *ep )
419{
420 int ret=0;
421
422 if ( ep->bufWrPtr > 0 ) {
423 ret = usb_write( ep, ep->buf, ep->bufWrPtr );
424 ep->bufWrPtr = 0;
425 }
426
427 return ret;
428}
429
430
431/* ------------------------------------------------------------------------- */
432
433
434/** Given a MIDI Event, determine size of data to be attached to
435 * USB-MIDI packet.
436 * Returns 1, 2 or 3.
437 * Called by midiWrite();
438 * Uses remains_80e0 and remains_f0f6;
439 **/
440static int get_remains(int event)
441{
442 int ret;
443
444 if ( event < 0x80 ) {
445 ret = 1;
446 } else if ( event < 0xf0 ) {
447 ret = remains_80e0[((event-0x80)>>4)&0x0f];
448 } else if ( event < 0xf7 ) {
449 ret = remains_f0f6[event-0xf0];
450 } else {
451 ret = 1;
452 }
453
454 return ret;
455}
456
457/** Given the output MIDI data in the output buffer, computes a reasonable
458 * CIN.
459 * Called by putOneMidiEvent().
460 **/
461static int get_CIN( struct usb_mididev *m )
462{
463 int cin;
464
465 if ( m->mout.buf[0] == 0xf7 ) {
466 cin = 5;
467 }
468 else if ( m->mout.buf[1] == 0xf7 ) {
469 cin = 6;
470 }
471 else if ( m->mout.buf[2] == 0xf7 ) {
472 cin = 7;
473 }
474 else {
475 if ( m->mout.isInExclusive == 1 ) {
476 cin = 4;
477 } else if ( m->mout.buf[0] < 0x80 ) {
478 /** One byte that we know nothing about. **/
479 cin = 0xF;
480 } else if ( m->mout.buf[0] < 0xf0 ) {
481 /** MIDI Voice messages 0x8X to 0xEX map to cin 0x8 to 0xE. **/
482 cin = (m->mout.buf[0]>>4)&0x0f;
483 }
484 else {
485 /** Special lookup table exists for real-time events. **/
486 cin = cin_f0ff[m->mout.buf[0]-0xf0];
487 }
488 }
489
490 return cin;
491}
492
493
494/* ------------------------------------------------------------------------- */
495
496
497
498/** Move data to USB endpoint buffer.
499 *
500 **/
501static int put_one_midi_event(struct usb_mididev *m)
502{
503 int cin;
504 unsigned long flags;
505 struct midi_out_endpoint *ep = m->mout.ep;
506 int ret=0;
507
508 cin = get_CIN( m );
509 if ( cin > 0x0f || cin < 0 ) {
510 return -EINVAL;
511 }
512
513 spin_lock_irqsave( &ep->lock, flags );
514 ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | cin;
515 ep->buf[ep->bufWrPtr++] = m->mout.buf[0];
516 ep->buf[ep->bufWrPtr++] = m->mout.buf[1];
517 ep->buf[ep->bufWrPtr++] = m->mout.buf[2];
518 if ( ep->bufWrPtr >= ep->bufSize ) {
519 ret = flush_midi_buffer( ep );
520 }
521 spin_unlock_irqrestore( &ep->lock, flags);
522
523 m->mout.buf[0] = m->mout.buf[1] = m->mout.buf[2] = 0;
524 m->mout.bufPtr = 0;
525
526 return ret;
527}
528
529/** Write the MIDI message v on the midi device.
530 * Called by usb_midi_write();
531 * Responsible for packaging a MIDI data stream into USB-MIDI packets.
532 **/
533
534static int midi_write( struct usb_mididev *m, int v )
535{
536 unsigned long flags;
537 struct midi_out_endpoint *ep = m->mout.ep;
538 int ret=0;
539 unsigned char c = (unsigned char)v;
540 unsigned char sysrt_buf[4];
541
542 if ( m->singlebyte != 0 ) {
543 /** Simple code to handle the single-byte USB-MIDI protocol. */
544 spin_lock_irqsave( &ep->lock, flags );
545 if ( ep->bufWrPtr+4 > ep->bufSize ) {
546 ret = flush_midi_buffer( ep );
547 if ( !ret ) {
548 spin_unlock_irqrestore( &ep->lock, flags );
549 return ret;
550 }
551 }
552 ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | 0x0f; /* single byte */
553 ep->buf[ep->bufWrPtr++] = c;
554 ep->buf[ep->bufWrPtr++] = 0;
555 ep->buf[ep->bufWrPtr++] = 0;
556 if ( ep->bufWrPtr >= ep->bufSize ) {
557 ret = flush_midi_buffer( ep );
558 }
559 spin_unlock_irqrestore( &ep->lock, flags );
560
561 return ret;
562 }
563 /** Normal USB-MIDI protocol begins here. */
564
565 if ( c > 0xf7 ) { /* system: Realtime messages */
566 /** Realtime messages are written IMMEDIATELY. */
567 sysrt_buf[0] = (m->mout.cableId<<4) | 0x0f;
568 sysrt_buf[1] = c;
569 sysrt_buf[2] = 0;
570 sysrt_buf[3] = 0;
571 spin_lock_irqsave( &ep->lock, flags );
572 ret = usb_write( ep, sysrt_buf, 4 );
573 spin_unlock_irqrestore( &ep->lock, flags );
574 /* m->mout.lastEvent = 0; */
575
576 return ret;
577 }
578
579 if ( c >= 0x80 ) {
580 if ( c < 0xf0 ) {
581 m->mout.lastEvent = c;
582 m->mout.isInExclusive = 0;
583 m->mout.bufRemains = get_remains(c);
584 } else if ( c == 0xf0 ) {
585 /* m->mout.lastEvent = 0; */
586 m->mout.isInExclusive = 1;
587 m->mout.bufRemains = get_remains(c);
588 } else if ( c == 0xf7 && m->mout.isInExclusive == 1 ) {
589 /* m->mout.lastEvent = 0; */
590 m->mout.isInExclusive = 0;
591 m->mout.bufRemains = 1;
592 } else if ( c > 0xf0 ) {
593 /* m->mout.lastEvent = 0; */
594 m->mout.isInExclusive = 0;
595 m->mout.bufRemains = get_remains(c);
596 }
597
598 } else if ( m->mout.bufRemains == 0 && m->mout.isInExclusive == 0 ) {
599 if ( m->mout.lastEvent == 0 ) {
600 return 0; /* discard, waiting for the first event */
601 }
602 /** track status **/
603 m->mout.buf[0] = m->mout.lastEvent;
604 m->mout.bufPtr = 1;
605 m->mout.bufRemains = get_remains(m->mout.lastEvent)-1;
606 }
607
608 m->mout.buf[m->mout.bufPtr++] = c;
609 m->mout.bufRemains--;
610 if ( m->mout.bufRemains == 0 || m->mout.bufPtr >= 3) {
611 ret = put_one_midi_event(m);
612 }
613
614 return ret;
615}
616
617
618/* ------------------------------------------------------------------------- */
619
620/** Basic operation on /dev/midiXX as registered through struct file_operations.
621 *
622 * Basic contract: Used to change the current read/write position in a file.
623 * On success, the non-negative position is reported.
624 * On failure, the negative of an error code is reported.
625 *
626 * Because a MIDIStream is not a file, all seek operations are doomed to fail.
627 *
628 **/
629static loff_t usb_midi_llseek(struct file *file, loff_t offset, int origin)
630{
631 /** Tell user you cannot seek on a PIPE-like device. **/
632 return -ESPIPE;
633}
634
635
636/** Basic operation on /dev/midiXX as registered through struct file_operations.
637 *
638 * Basic contract: Block until count bytes have been read or an error occurs.
639 *
640 **/
641
642static ssize_t usb_midi_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
643{
644 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
645 struct midi_in_endpoint *ep = m->min.ep;
646 ssize_t ret;
647 DECLARE_WAITQUEUE(wait, current);
648
649 if ( !access_ok(VERIFY_READ, buffer, count) ) {
650 return -EFAULT;
651 }
652 if ( count == 0 ) {
653 return 0;
654 }
655
656 add_wait_queue( &ep->wait, &wait );
657 ret = 0;
658 while( count > 0 ) {
659 int cnt;
660 int d = (int)count;
661
662 cnt = m->min.bufRemains;
663 if ( cnt > d ) {
664 cnt = d;
665 }
666
667 if ( cnt <= 0 ) {
668 if ( file->f_flags & O_NONBLOCK ) {
669 if (!ret)
670 ret = -EAGAIN;
671 break;
672 }
673 __set_current_state(TASK_INTERRUPTIBLE);
674 schedule();
675 if (signal_pending(current)) {
676 if(!ret)
677 ret=-ERESTARTSYS;
678 break;
679 }
680 continue;
681 }
682
683 {
684 int i;
685 unsigned long flags; /* used to synchronize access to the endpoint */
686 spin_lock_irqsave( &ep->lock, flags );
687 for (i = 0; i < cnt; i++) {
688 if ( copy_to_user( buffer+i, m->min.buf+m->min.bufRdPtr, 1 ) ) {
689 if ( !ret )
690 ret = -EFAULT;
691 break;
692 }
693 m->min.bufRdPtr = (m->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
694 m->min.bufRemains -= 1;
695 }
696 spin_unlock_irqrestore( &ep->lock, flags );
697 }
698
699 count-=cnt;
700 buffer+=cnt;
701 ret+=cnt;
702
703 break;
704 }
705
706 remove_wait_queue( &ep->wait, &wait );
707 set_current_state(TASK_RUNNING);
708
709 return ret;
710}
711
712
713/** Basic operation on /dev/midiXX as registered through struct file_operations.
714 *
715 * Basic Contract: Take MIDI data byte-by-byte and pass it to
716 * writeMidi() which packages MIDI data into USB-MIDI stream.
717 * Then flushMidiData() is called to ensure all bytes have been written
718 * in a timely fashion.
719 *
720 **/
721
722static ssize_t usb_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
723{
724 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
725 ssize_t ret;
726 unsigned long int flags;
727
728 if ( !access_ok(VERIFY_READ, buffer, count) ) {
729 return -EFAULT;
730 }
731 if ( count == 0 ) {
732 return 0;
733 }
734
735 ret = 0;
736 while( count > 0 ) {
737 unsigned char c;
738
739 if (copy_from_user((unsigned char *)&c, buffer, 1)) {
740 if ( ret == 0 )
741 ret = -EFAULT;
742 break;
743 }
744 if( midi_write(m, (int)c) ) {
745 if ( ret == 0 )
746 ret = -EFAULT;
747 break;
748 }
749 count--;
750 buffer++;
751 ret++;
752 }
753
754 spin_lock_irqsave( &m->mout.ep->lock, flags );
755 if ( flush_midi_buffer(m->mout.ep) < 0 ) {
756 ret = -EFAULT;
757 }
758 spin_unlock_irqrestore( &m->mout.ep->lock, flags );
759
760 return ret;
761}
762
763/** Basic operation on /dev/midiXX as registered through struct file_operations.
764 *
765 * Basic contract: Wait (spin) until ready to read or write on the file.
766 *
767 **/
768static unsigned int usb_midi_poll(struct file *file, struct poll_table_struct *wait)
769{
770 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
771 struct midi_in_endpoint *iep = m->min.ep;
772 struct midi_out_endpoint *oep = m->mout.ep;
773 unsigned long flags;
774 unsigned int mask = 0;
775
776 if ( file->f_mode & FMODE_READ ) {
777 poll_wait( file, &iep->wait, wait );
778 spin_lock_irqsave( &iep->lock, flags );
779 if ( m->min.bufRemains > 0 )
780 mask |= POLLIN | POLLRDNORM;
781 spin_unlock_irqrestore( &iep->lock, flags );
782 }
783
784 if ( file->f_mode & FMODE_WRITE ) {
785 poll_wait( file, &oep->wait, wait );
786 spin_lock_irqsave( &oep->lock, flags );
787 if ( oep->bufWrPtr < oep->bufSize )
788 mask |= POLLOUT | POLLWRNORM;
789 spin_unlock_irqrestore( &oep->lock, flags );
790 }
791
792 return mask;
793}
794
795
796/** Basic operation on /dev/midiXX as registered through struct file_operations.
797 *
798 * Basic contract: This is always the first operation performed on the
799 * device node. If no method is defined, the open succeeds without any
800 * notification given to the module.
801 *
802 **/
803
804static int usb_midi_open(struct inode *inode, struct file *file)
805{
806 int minor = iminor(inode);
807 DECLARE_WAITQUEUE(wait, current);
808 struct usb_midi_state *s;
809 struct usb_mididev *m;
810 unsigned long flags;
811 int succeed = 0;
812
813#if 0
814 printk(KERN_INFO "usb-midi: Open minor= %d.\n", minor);
815#endif
816
817 for(;;) {
818 down(&open_sem);
819 list_for_each_entry(s, &mididevs, mididev) {
820 list_for_each_entry(m, &s->midiDevList, list) {
821 if ( !((m->dev_midi ^ minor) & ~0xf) )
822 goto device_found;
823 }
824 }
825 up(&open_sem);
826 return -ENODEV;
827
828 device_found:
829 if ( !s->usbdev ) {
830 up(&open_sem);
831 return -EIO;
832 }
833 if ( !(m->open_mode & file->f_mode) ) {
834 break;
835 }
836 if ( file->f_flags & O_NONBLOCK ) {
837 up(&open_sem);
838 return -EBUSY;
839 }
840 __set_current_state(TASK_INTERRUPTIBLE);
841 add_wait_queue( &open_wait, &wait );
842 up(&open_sem);
843 schedule();
844 remove_wait_queue( &open_wait, &wait );
845 if ( signal_pending(current) ) {
846 return -ERESTARTSYS;
847 }
848 }
849
850 file->private_data = m;
851 spin_lock_irqsave( &s->lock, flags );
852
853 if ( !(m->open_mode & (FMODE_READ | FMODE_WRITE)) ) {
854 //FIXME: intented semantics unclear here
855 m->min.bufRdPtr = 0;
856 m->min.bufWrPtr = 0;
857 m->min.bufRemains = 0;
858 spin_lock_init(&m->min.ep->lock);
859
860 m->mout.bufPtr = 0;
861 m->mout.bufRemains = 0;
862 m->mout.isInExclusive = 0;
863 m->mout.lastEvent = 0;
864 spin_lock_init(&m->mout.ep->lock);
865 }
866
867 if ( (file->f_mode & FMODE_READ) && m->min.ep != NULL ) {
868 unsigned long int flagsep;
869 spin_lock_irqsave( &m->min.ep->lock, flagsep );
870 m->min.ep->cables[m->min.cableId] = m;
871 m->min.ep->readers += 1;
872 m->min.bufRdPtr = 0;
873 m->min.bufWrPtr = 0;
874 m->min.bufRemains = 0;
875 spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
876
877 if ( !(m->min.ep->urbSubmitted)) {
878
879 /* urb->dev must be reinitialized on 2.4.x kernels */
880 m->min.ep->urb->dev = m->min.ep->usbdev;
881
882 if ( usb_submit_urb(m->min.ep->urb, GFP_ATOMIC) ) {
883 printk(KERN_ERR "usbmidi: Cannot submit urb for MIDI-IN\n");
884 }
885 m->min.ep->urbSubmitted = 1;
886 }
887 m->open_mode |= FMODE_READ;
888 succeed = 1;
889 }
890
891 if ( (file->f_mode & FMODE_WRITE) && m->mout.ep != NULL ) {
892 m->mout.bufPtr = 0;
893 m->mout.bufRemains = 0;
894 m->mout.isInExclusive = 0;
895 m->mout.lastEvent = 0;
896 m->open_mode |= FMODE_WRITE;
897 succeed = 1;
898 }
899
900 spin_unlock_irqrestore( &s->lock, flags );
901
902 s->count++;
903 up(&open_sem);
904
905 /** Changed to prevent extra increments to USE_COUNT. **/
906 if (!succeed) {
907 return -EBUSY;
908 }
909
910#if 0
911 printk(KERN_INFO "usb-midi: Open Succeeded. minor= %d.\n", minor);
912#endif
913
914 return nonseekable_open(inode, file); /** Success. **/
915}
916
917
918/** Basic operation on /dev/midiXX as registered through struct file_operations.
919 *
920 * Basic contract: Close an opened file and deallocate anything we allocated.
921 * Like open(), this can be missing. If open set file->private_data,
922 * release() must clear it.
923 *
924 **/
925
926static int usb_midi_release(struct inode *inode, struct file *file)
927{
928 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
929 struct usb_midi_state *s = (struct usb_midi_state *)m->midi;
930
931#if 0
932 printk(KERN_INFO "usb-midi: Close.\n");
933#endif
934
935 down(&open_sem);
936
937 if ( m->open_mode & FMODE_WRITE ) {
938 m->open_mode &= ~FMODE_WRITE;
939 usb_kill_urb( m->mout.ep->urb );
940 }
941
942 if ( m->open_mode & FMODE_READ ) {
943 unsigned long int flagsep;
944 spin_lock_irqsave( &m->min.ep->lock, flagsep );
945 m->min.ep->cables[m->min.cableId] = NULL; // discard cable
946 m->min.ep->readers -= 1;
947 m->open_mode &= ~FMODE_READ;
948 if ( m->min.ep->readers == 0 &&
949 m->min.ep->urbSubmitted ) {
950 m->min.ep->urbSubmitted = 0;
951 usb_kill_urb(m->min.ep->urb);
952 }
953 spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
954 }
955
956 s->count--;
957
958 up(&open_sem);
959 wake_up(&open_wait);
960
961 file->private_data = NULL;
962 return 0;
963}
964
965static struct file_operations usb_midi_fops = {
966 .owner = THIS_MODULE,
967 .llseek = usb_midi_llseek,
968 .read = usb_midi_read,
969 .write = usb_midi_write,
970 .poll = usb_midi_poll,
971 .open = usb_midi_open,
972 .release = usb_midi_release,
973};
974
975/* ------------------------------------------------------------------------- */
976
977/** Returns filled midi_in_endpoint structure or null on failure.
978 *
979 * Parameters:
980 * d - a usb_device
981 * endPoint - An usb endpoint in the range 0 to 15.
982 * Called by allocUsbMidiDev();
983 *
984 **/
985
986static struct midi_in_endpoint *alloc_midi_in_endpoint( struct usb_device *d, int endPoint )
987{
988 struct midi_in_endpoint *ep;
989 int bufSize;
990 int pipe;
991
992 endPoint &= 0x0f; /* Silently force endPoint to lie in range 0 to 15. */
993
994 pipe = usb_rcvbulkpipe( d, endPoint );
995 bufSize = usb_maxpacket( d, pipe, 0 );
996 /* usb_pipein() = ! usb_pipeout() = true for an in Endpoint */
997
998 ep = (struct midi_in_endpoint *)kmalloc(sizeof(struct midi_in_endpoint), GFP_KERNEL);
999 if ( !ep ) {
1000 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint\n");
1001 return NULL;
1002 }
1003 memset( ep, 0, sizeof(struct midi_in_endpoint) );
1004// this sets cables[] and readers to 0, too.
1005// for (i=0; i<16; i++) ep->cables[i] = 0; // discard cable
1006// ep->readers = 0;
1007
1008 ep->endpoint = endPoint;
1009
1010 ep->recvBuf = (unsigned char *)kmalloc(sizeof(unsigned char)*(bufSize), GFP_KERNEL);
1011 if ( !ep->recvBuf ) {
1012 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint buffer\n");
1013 kfree(ep);
1014 return NULL;
1015 }
1016
1017 ep->urb = usb_alloc_urb(0, GFP_KERNEL); /* no ISO */
1018 if ( !ep->urb ) {
1019 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint urb\n");
1020 kfree(ep->recvBuf);
1021 kfree(ep);
1022 return NULL;
1023 }
1024 usb_fill_bulk_urb( ep->urb, d,
1025 usb_rcvbulkpipe(d, endPoint),
1026 (unsigned char *)ep->recvBuf, bufSize,
1027 usb_bulk_read, ep );
1028
1029 /* ep->bufRdPtr = 0; */
1030 /* ep->bufWrPtr = 0; */
1031 /* ep->bufRemains = 0; */
1032 /* ep->urbSubmitted = 0; */
1033 ep->recvBufSize = bufSize;
1034
1035 init_waitqueue_head(&ep->wait);
1036
1037 return ep;
1038}
1039
1040static int remove_midi_in_endpoint( struct midi_in_endpoint *min )
1041{
1042 usb_kill_urb( min->urb );
1043 usb_free_urb( min->urb );
1044 kfree( min->recvBuf );
1045 kfree( min );
1046
1047 return 0;
1048}
1049
1050/** Returns filled midi_out_endpoint structure or null on failure.
1051 *
1052 * Parameters:
1053 * d - a usb_device
1054 * endPoint - An usb endpoint in the range 0 to 15.
1055 * Called by allocUsbMidiDev();
1056 *
1057 **/
1058static struct midi_out_endpoint *alloc_midi_out_endpoint( struct usb_device *d, int endPoint )
1059{
1060 struct midi_out_endpoint *ep = NULL;
1061 int pipe;
1062 int bufSize;
1063
1064 endPoint &= 0x0f;
1065 pipe = usb_sndbulkpipe( d, endPoint );
1066 bufSize = usb_maxpacket( d, pipe, 1 );
1067
1068 ep = (struct midi_out_endpoint *)kmalloc(sizeof(struct midi_out_endpoint), GFP_KERNEL);
1069 if ( !ep ) {
1070 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint\n");
1071 return NULL;
1072 }
1073 memset( ep, 0, sizeof(struct midi_out_endpoint) );
1074
1075 ep->endpoint = endPoint;
1076 ep->buf = (unsigned char *)kmalloc(sizeof(unsigned char)*bufSize, GFP_KERNEL);
1077 if ( !ep->buf ) {
1078 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint buffer\n");
1079 kfree(ep);
1080 return NULL;
1081 }
1082
1083 ep->urb = usb_alloc_urb(0, GFP_KERNEL); /* no ISO */
1084 if ( !ep->urb ) {
1085 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint urb\n");
1086 kfree(ep->buf);
1087 kfree(ep);
1088 return NULL;
1089 }
1090
1091 ep->bufSize = bufSize;
1092 /* ep->bufWrPtr = 0; */
1093
1094 init_waitqueue_head(&ep->wait);
1095
1096 return ep;
1097}
1098
1099
1100static int remove_midi_out_endpoint( struct midi_out_endpoint *mout )
1101{
1102 usb_kill_urb( mout->urb );
1103 usb_free_urb( mout->urb );
1104 kfree( mout->buf );
1105 kfree( mout );
1106
1107 return 0;
1108}
1109
1110
1111/** Returns a filled usb_mididev structure, registered as a Linux MIDI device.
1112 *
1113 * Returns null if memory is not available or the device cannot be registered.
1114 * Called by allocUsbMidiDev();
1115 *
1116 **/
1117static struct usb_mididev *allocMidiDev(
1118 struct usb_midi_state *s,
1119 struct midi_in_endpoint *min,
1120 struct midi_out_endpoint *mout,
1121 int inCableId,
1122 int outCableId )
1123{
1124 struct usb_mididev *m;
1125
1126 m = (struct usb_mididev *)kmalloc(sizeof(struct usb_mididev), GFP_KERNEL);
1127 if (!m) {
1128 printk(KERN_ERR "usbmidi: no memory for midi device\n");
1129 return NULL;
1130 }
1131
1132 memset(m, 0, sizeof(struct usb_mididev));
1133
1134 if ((m->dev_midi = register_sound_midi(&usb_midi_fops, -1)) < 0) {
1135 printk(KERN_ERR "usbmidi: cannot register midi device\n");
1136 kfree(m);
1137 return NULL;
1138 }
1139
1140 m->midi = s;
1141 /* m->open_mode = 0; */
1142
1143 if ( min ) {
1144 m->min.ep = min;
1145 m->min.ep->usbdev = s->usbdev;
1146 m->min.cableId = inCableId;
1147 }
1148 /* m->min.bufPtr = 0; */
1149 /* m->min.bufRemains = 0; */
1150
1151 if ( mout ) {
1152 m->mout.ep = mout;
1153 m->mout.ep->usbdev = s->usbdev;
1154 m->mout.cableId = outCableId;
1155 }
1156 /* m->mout.bufPtr = 0; */
1157 /* m->mout.bufRemains = 0; */
1158 /* m->mout.isInExclusive = 0; */
1159 /* m->mout.lastEvent = 0; */
1160
1161 m->singlebyte = singlebyte;
1162
1163 return m;
1164}
1165
1166
1167static void release_midi_device( struct usb_midi_state *s )
1168{
1169 struct usb_mididev *m;
1170 struct midi_in_endpoint *min;
1171 struct midi_out_endpoint *mout;
1172
1173 if ( s->count > 0 ) {
1174 up(&open_sem);
1175 return;
1176 }
1177 up( &open_sem );
1178 wake_up( &open_wait );
1179
1180 while(!list_empty(&s->inEndpointList)) {
1181 min = list_entry(s->inEndpointList.next, struct midi_in_endpoint, list);
1182 list_del(&min->list);
1183 remove_midi_in_endpoint(min);
1184 }
1185
1186 while(!list_empty(&s->outEndpointList)) {
1187 mout = list_entry(s->outEndpointList.next, struct midi_out_endpoint, list);
1188 list_del(&mout->list);
1189 remove_midi_out_endpoint(mout);
1190 }
1191
1192 while(!list_empty(&s->midiDevList)) {
1193 m = list_entry(s->midiDevList.next, struct usb_mididev, list);
1194 list_del(&m->list);
1195 kfree(m);
1196 }
1197
1198 kfree(s);
1199
1200 return;
1201}
1202
1203
1204/* ------------------------------------------------------------------------- */
1205
1206/** Utility routine to find a descriptor in a dump of many descriptors.
1207 * Returns start of descriptor or NULL if not found.
1208 * descStart pointer to list of interfaces.
1209 * descLength length (in bytes) of dump
1210 * after (ignored if NULL) this routine returns only descriptors after "after"
1211 * dtype (mandatory) The descriptor type.
1212 * iface (ignored if -1) returns descriptor at/following given interface
1213 * altSetting (ignored if -1) returns descriptor at/following given altSetting
1214 *
1215 *
1216 * Called by parseDescriptor(), find_csinterface_descriptor();
1217 *
1218 */
1219static void *find_descriptor( void *descStart, unsigned int descLength, void *after, unsigned char dtype, int iface, int altSetting )
1220{
1221 unsigned char *p, *end, *next;
1222 int interfaceNumber = -1, altSet = -1;
1223
1224 p = descStart;
1225 end = p + descLength;
1226 for( ; p < end; ) {
1227 if ( p[0] < 2 )
1228 return NULL;
1229 next = p + p[0];
1230 if ( next > end )
1231 return NULL;
1232 if ( p[1] == USB_DT_INTERFACE ) {
1233 if ( p[0] < USB_DT_INTERFACE_SIZE )
1234 return NULL;
1235 interfaceNumber = p[2];
1236 altSet = p[3];
1237 }
1238 if ( p[1] == dtype &&
1239 ( !after || ( p > (unsigned char *)after) ) &&
1240 ( ( iface == -1) || (iface == interfaceNumber) ) &&
1241 ( (altSetting == -1) || (altSetting == altSet) )) {
1242 return p;
1243 }
1244 p = next;
1245 }
1246 return NULL;
1247}
1248
1249/** Utility to find a class-specific interface descriptor.
1250 * dsubtype is a descriptor subtype
1251 * Called by parseDescriptor();
1252 **/
1253static void *find_csinterface_descriptor(void *descStart, unsigned int descLength, void *after, u8 dsubtype, int iface, int altSetting)
1254{
1255 unsigned char *p;
1256
1257 p = find_descriptor( descStart, descLength, after, USB_DT_CS_INTERFACE, iface, altSetting );
1258 while ( p ) {
1259 if ( p[0] >= 3 && p[2] == dsubtype )
1260 return p;
1261 p = find_descriptor( descStart, descLength, p, USB_DT_CS_INTERFACE,
1262 iface, altSetting );
1263 }
1264 return NULL;
1265}
1266
1267
1268/** The magic of making a new usb_midi_device from config happens here.
1269 *
1270 * The caller is responsible for free-ing this return value (if not NULL).
1271 *
1272 **/
1273static struct usb_midi_device *parse_descriptor( struct usb_device *d, unsigned char *buffer, int bufSize, unsigned int ifnum , unsigned int altSetting, int quirks)
1274{
1275 struct usb_midi_device *u;
1276 unsigned char *p1;
1277 unsigned char *p2;
1278 unsigned char *next;
1279 int iep, oep;
1280 int length;
1281 unsigned long longBits;
1282 int pins, nbytes, offset, shift, jack;
1283#ifdef HAVE_JACK_STRINGS
1284 /** Jacks can have associated names. **/
1285 unsigned char jack2string[256];
1286#endif
1287
1288 u = NULL;
1289 /* find audiocontrol interface */
1290 p1 = find_csinterface_descriptor( buffer, bufSize, NULL,
1291 MS_HEADER, ifnum, altSetting);
1292
1293 if ( !p1 ) {
1294 goto error_end;
1295 }
1296
1297 if ( p1[0] < MS_HEADER_LENGTH ) {
1298 goto error_end;
1299 }
1300
1301 /* Assume success. Since the device corresponds to USB-MIDI spec, we assume
1302 that the rest of the USB 2.0 spec is obeyed. */
1303
1304 u = (struct usb_midi_device *)kmalloc( sizeof(struct usb_midi_device), GFP_KERNEL );
1305 if ( !u ) {
1306 return NULL;
1307 }
1308 u->deviceName = NULL;
1309 u->idVendor = le16_to_cpu(d->descriptor.idVendor);
1310 u->idProduct = le16_to_cpu(d->descriptor.idProduct);
1311 u->interface = ifnum;
1312 u->altSetting = altSetting;
1313 u->in[0].endpoint = -1;
1314 u->in[0].cableId = -1;
1315 u->out[0].endpoint = -1;
1316 u->out[0].cableId = -1;
1317
1318
1319 printk(KERN_INFO "usb-midi: Found MIDIStreaming device corresponding to Release %d.%02d of spec.\n",
1320 (p1[4] >> 4) * 10 + (p1[4] & 0x0f ),
1321 (p1[3] >> 4) * 10 + (p1[3] & 0x0f )
1322 );
1323
1324 length = p1[5] | (p1[6] << 8);
1325
1326#ifdef HAVE_JACK_STRINGS
1327 memset(jack2string, 0, sizeof(unsigned char) * 256);
1328#endif
1329
1330 length -= p1[0];
1331 for (p2 = p1 + p1[0]; length > 0; p2 = next) {
1332 next = p2 + p2[0];
1333 length -= p2[0];
1334
1335 if (p2[0] < 2 )
1336 break;
1337 if (p2[1] != USB_DT_CS_INTERFACE)
1338 break;
1339 if (p2[2] == MIDI_IN_JACK && p2[0] >= 6 ) {
1340 jack = p2[4];
1341#ifdef HAVE_JACK_STRINGS
1342 jack2string[jack] = p2[5];
1343#endif
1344 printk(KERN_INFO "usb-midi: Found IN Jack 0x%02x %s\n",
1345 jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL" );
1346 } else if ( p2[2] == MIDI_OUT_JACK && p2[0] >= 6) {
1347 pins = p2[5];
1348 if ( p2[0] < (6 + 2 * pins) )
1349 continue;
1350 jack = p2[4];
1351#ifdef HAVE_JACK_STRINGS
1352 jack2string[jack] = p2[5 + 2 * pins];
1353#endif
1354 printk(KERN_INFO "usb-midi: Found OUT Jack 0x%02x %s, %d pins\n",
1355 jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL", pins );
1356 } else if ( p2[2] == ELEMENT_DESCRIPTOR && p2[0] >= 10) {
1357 pins = p2[4];
1358 if ( p2[0] < (9 + 2 * pins ) )
1359 continue;
1360 nbytes = p2[8 + 2 * pins ];
1361 if ( p2[0] < (10 + 2 * pins + nbytes) )
1362 continue;
1363 longBits = 0L;
1364 for ( offset = 0, shift = 0; offset < nbytes && offset < 8; offset ++, shift += 8) {
1365 longBits |= ((long)(p2[9 + 2 * pins + offset])) << shift;
1366 }
1367 jack = p2[3];
1368#ifdef HAVE_JACK_STRINGS
1369 jack2string[jack] = p2[9 + 2 * pins + nbytes];
1370#endif
1371 printk(KERN_INFO "usb-midi: Found ELEMENT 0x%02x, %d/%d pins in/out, bits: 0x%016lx\n",
1372 jack, pins, (int)(p2[5 + 2 * pins]), (long)longBits );
1373 } else {
1374 }
1375 }
1376
1377 iep=0;
1378 oep=0;
1379
1380 if (quirks==0) {
1381 /* MIDISTREAM */
1382 p2 = NULL;
1383 for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1384 ifnum, altSetting ); p1; p1 = next ) {
1385 next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1386 ifnum, altSetting );
1387 p2 = find_descriptor(buffer, bufSize, p1, USB_DT_CS_ENDPOINT,
1388 ifnum, altSetting );
1389
1390 if ( p2 && next && ( p2 > next ) )
1391 p2 = NULL;
1392
1393 if ( p1[0] < 9 || !p2 || p2[0] < 4 )
1394 continue;
1395
1396 if ( (p1[2] & 0x80) == 0x80 ) {
1397 if ( iep < 15 ) {
1398 pins = p2[3]; /* not pins -- actually "cables" */
1399 if ( pins > 16 )
1400 pins = 16;
1401 u->in[iep].endpoint = p1[2];
1402 u->in[iep].cableId = ( 1 << pins ) - 1;
1403 if ( u->in[iep].cableId )
1404 iep ++;
1405 if ( iep < 15 ) {
1406 u->in[iep].endpoint = -1;
1407 u->in[iep].cableId = -1;
1408 }
1409 }
1410 } else {
1411 if ( oep < 15 ) {
1412 pins = p2[3]; /* not pins -- actually "cables" */
1413 if ( pins > 16 )
1414 pins = 16;
1415 u->out[oep].endpoint = p1[2];
1416 u->out[oep].cableId = ( 1 << pins ) - 1;
1417 if ( u->out[oep].cableId )
1418 oep ++;
1419 if ( oep < 15 ) {
1420 u->out[oep].endpoint = -1;
1421 u->out[oep].cableId = -1;
1422 }
1423 }
1424 }
1425
1426 }
1427 } else if (quirks==1) {
1428 /* YAMAHA quirks */
1429 for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1430 ifnum, altSetting ); p1; p1 = next ) {
1431 next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1432 ifnum, altSetting );
1433
1434 if ( p1[0] < 7 )
1435 continue;
1436
1437 if ( (p1[2] & 0x80) == 0x80 ) {
1438 if ( iep < 15 ) {
1439 pins = iep+1;
1440 if ( pins > 16 )
1441 pins = 16;
1442 u->in[iep].endpoint = p1[2];
1443 u->in[iep].cableId = ( 1 << pins ) - 1;
1444 if ( u->in[iep].cableId )
1445 iep ++;
1446 if ( iep < 15 ) {
1447 u->in[iep].endpoint = -1;
1448 u->in[iep].cableId = -1;
1449 }
1450 }
1451 } else {
1452 if ( oep < 15 ) {
1453 pins = oep+1;
1454 u->out[oep].endpoint = p1[2];
1455 u->out[oep].cableId = ( 1 << pins ) - 1;
1456 if ( u->out[oep].cableId )
1457 oep ++;
1458 if ( oep < 15 ) {
1459 u->out[oep].endpoint = -1;
1460 u->out[oep].cableId = -1;
1461 }
1462 }
1463 }
1464
1465 }
1466 }
1467
1468 if ( !iep && ! oep ) {
1469 goto error_end;
1470 }
1471
1472 return u;
1473
1474error_end:
1475 kfree(u);
1476 return NULL;
1477}
1478
1479/* ------------------------------------------------------------------------- */
1480
1481/** Returns number between 0 and 16.
1482 *
1483 **/
1484static int on_bits( unsigned short v )
1485{
1486 int i;
1487 int ret=0;
1488
1489 for ( i=0 ; i<16 ; i++ ) {
1490 if ( v & (1<<i) )
1491 ret++;
1492 }
1493
1494 return ret;
1495}
1496
1497
1498/** USB-device will be interrogated for altSetting.
1499 *
1500 * Returns negative on error.
1501 * Called by allocUsbMidiDev();
1502 *
1503 **/
1504
1505static int get_alt_setting( struct usb_device *d, int ifnum )
1506{
1507 int alts, alt=0;
1508 struct usb_interface *iface;
1509 struct usb_host_interface *interface;
1510 struct usb_endpoint_descriptor *ep;
1511 int epin, epout;
1512 int i;
1513
1514 iface = usb_ifnum_to_if( d, ifnum );
1515 alts = iface->num_altsetting;
1516
1517 for ( alt=0 ; alt<alts ; alt++ ) {
1518 interface = &iface->altsetting[alt];
1519 epin = -1;
1520 epout = -1;
1521
1522 for ( i=0 ; i<interface->desc.bNumEndpoints ; i++ ) {
1523 ep = &interface->endpoint[i].desc;
1524 if ( (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ) {
1525 continue;
1526 }
1527 if ( (ep->bEndpointAddress & USB_DIR_IN) && epin < 0 ) {
1528 epin = i;
1529 } else if ( epout < 0 ) {
1530 epout = i;
1531 }
1532 if ( epin >= 0 && epout >= 0 ) {
1533 return interface->desc.bAlternateSetting;
1534 }
1535 }
1536 }
1537
1538 return -ENODEV;
1539}
1540
1541
1542/* ------------------------------------------------------------------------- */
1543
1544
1545/** Returns 0 if successful in allocating and registering internal structures.
1546 * Returns negative on failure.
1547 * Calls allocMidiDev which additionally registers /dev/midiXX devices.
1548 * Writes messages on success to indicate which /dev/midiXX is which physical
1549 * endpoint.
1550 *
1551 **/
1552static int alloc_usb_midi_device( struct usb_device *d, struct usb_midi_state *s, struct usb_midi_device *u )
1553{
1554 struct usb_mididev **mdevs=NULL;
1555 struct midi_in_endpoint *mins[15], *min;
1556 struct midi_out_endpoint *mouts[15], *mout;
1557 int inDevs=0, outDevs=0;
1558 int inEndpoints=0, outEndpoints=0;
1559 int inEndpoint, outEndpoint;
1560 int inCableId, outCableId;
1561 int i;
1562 int devices = 0;
1563 int alt = 0;
1564
1565 /* Obtain altSetting or die.. */
1566 alt = u->altSetting;
1567 if ( alt < 0 ) {
1568 alt = get_alt_setting( d, u->interface );
1569 }
1570 if ( alt < 0 )
1571 return -ENXIO;
1572
1573 /* Configure interface */
1574 if ( usb_set_interface( d, u->interface, alt ) < 0 ) {
1575 return -ENXIO;
1576 }
1577
1578 for ( i = 0 ; i < 15 ; i++ ) {
1579 mins[i] = NULL;
1580 mouts[i] = NULL;
1581 }
1582
1583 /* Begin Allocation */
1584 while( inEndpoints < 15
1585 && inDevs < maxdevices
1586 && u->in[inEndpoints].cableId >= 0 ) {
1587 inDevs += on_bits((unsigned short)u->in[inEndpoints].cableId);
1588 mins[inEndpoints] = alloc_midi_in_endpoint( d, u->in[inEndpoints].endpoint );
1589 if ( mins[inEndpoints] == NULL )
1590 goto error_end;
1591 inEndpoints++;
1592 }
1593
1594 while( outEndpoints < 15
1595 && outDevs < maxdevices
1596 && u->out[outEndpoints].cableId >= 0 ) {
1597 outDevs += on_bits((unsigned short)u->out[outEndpoints].cableId);
1598 mouts[outEndpoints] = alloc_midi_out_endpoint( d, u->out[outEndpoints].endpoint );
1599 if ( mouts[outEndpoints] == NULL )
1600 goto error_end;
1601 outEndpoints++;
1602 }
1603
1604 devices = inDevs > outDevs ? inDevs : outDevs;
1605 devices = maxdevices > devices ? devices : maxdevices;
1606
1607 /* obtain space for device name (iProduct) if not known. */
1608 if ( ! u->deviceName ) {
1609 mdevs = (struct usb_mididev **)
1610 kmalloc(sizeof(struct usb_mididevs *)*devices
1611 + sizeof(char) * 256, GFP_KERNEL);
1612 } else {
1613 mdevs = (struct usb_mididev **)
1614 kmalloc(sizeof(struct usb_mididevs *)*devices, GFP_KERNEL);
1615 }
1616
1617 if ( !mdevs ) {
1618 /* devices = 0; */
1619 /* mdevs = NULL; */
1620 goto error_end;
1621 }
1622 for ( i=0 ; i<devices ; i++ ) {
1623 mdevs[i] = NULL;
1624 }
1625
1626 /* obtain device name (iProduct) if not known. */
1627 if ( ! u->deviceName ) {
1628 u->deviceName = (char *) (mdevs + devices);
1629 if ( ! d->have_langid && d->descriptor.iProduct) {
1630 alt = usb_get_string(d, 0, 0, u->deviceName, 250);
1631 if (alt < 0) {
1632 printk(KERN_INFO "error getting string descriptor 0 (error=%d)\n", alt);
1633 } else if (u->deviceName[0] < 4) {
1634 printk(KERN_INFO "string descriptor 0 too short (length = %d)\n", alt);
1635 } else {
1636 printk(KERN_INFO "string descriptor 0 found (length = %d)\n", alt);
1637 for(; alt >= 4; alt -= 2) {
1638 i = u->deviceName[alt-2] | (u->deviceName[alt-1]<< 8);
1639 printk(KERN_INFO "usb-midi: langid(%d) 0x%04x\n",
1640 (alt-4) >> 1, i);
1641 if ( ( ( i ^ ulangid ) & 0xff ) == 0 ) {
1642 d->have_langid = 1;
1643 d->string_langid = i;
1644 printk(KERN_INFO "usb-midi: langid(match) 0x%04x\n", i);
1645 if ( i == ulangid )
1646 break;
1647 }
1648 }
1649 }
1650 }
1651 u->deviceName[0] = (char) 0;
1652 if (d->descriptor.iProduct) {
1653 printk(KERN_INFO "usb-midi: fetchString(%d)\n", d->descriptor.iProduct);
1654 alt = usb_string(d, d->descriptor.iProduct, u->deviceName, 255);
1655 if( alt < 0 ) {
1656 u->deviceName[0] = (char) 0;
1657 }
1658 printk(KERN_INFO "usb-midi: fetchString = %d\n", alt);
1659 }
1660 /* Failsafe */
1661 if ( !u->deviceName[0] ) {
1662 if (le16_to_cpu(d->descriptor.idVendor) == USB_VENDOR_ID_ROLAND ) {
1663 strcpy(u->deviceName, "Unknown Roland");
1664 } else if (le16_to_cpu(d->descriptor.idVendor) == USB_VENDOR_ID_STEINBERG ) {
1665 strcpy(u->deviceName, "Unknown Steinberg");
1666 } else if (le16_to_cpu(d->descriptor.idVendor) == USB_VENDOR_ID_YAMAHA ) {
1667 strcpy(u->deviceName, "Unknown Yamaha");
1668 } else {
1669 strcpy(u->deviceName, "Unknown");
1670 }
1671 }
1672 }
1673
1674 inEndpoint = 0; inCableId = -1;
1675 outEndpoint = 0; outCableId = -1;
1676
1677 for ( i=0 ; i<devices ; i++ ) {
1678 for ( inCableId ++ ;
1679 inEndpoint <15
1680 && mins[inEndpoint]
1681 && !(u->in[inEndpoint].cableId & (1<<inCableId)) ;
1682 inCableId++ ) {
1683 if ( inCableId >= 16 ) {
1684 inEndpoint ++;
1685 inCableId = 0;
1686 }
1687 }
1688 min = mins[inEndpoint];
1689 for ( outCableId ++ ;
1690 outEndpoint <15
1691 && mouts[outEndpoint]
1692 && !(u->out[outEndpoint].cableId & (1<<outCableId)) ;
1693 outCableId++ ) {
1694 if ( outCableId >= 16 ) {
1695 outEndpoint ++;
1696 outCableId = 0;
1697 }
1698 }
1699 mout = mouts[outEndpoint];
1700
1701 mdevs[i] = allocMidiDev( s, min, mout, inCableId, outCableId );
1702 if ( mdevs[i] == NULL )
1703 goto error_end;
1704
1705 }
1706
1707 /* Success! */
1708 for ( i=0 ; i<devices ; i++ ) {
1709 list_add_tail( &mdevs[i]->list, &s->midiDevList );
1710 }
1711 for ( i=0 ; i<inEndpoints ; i++ ) {
1712 list_add_tail( &mins[i]->list, &s->inEndpointList );
1713 }
1714 for ( i=0 ; i<outEndpoints ; i++ ) {
1715 list_add_tail( &mouts[i]->list, &s->outEndpointList );
1716 }
1717
1718 printk(KERN_INFO "usbmidi: found [ %s ] (0x%04x:0x%04x), attached:\n", u->deviceName, u->idVendor, u->idProduct );
1719 for ( i=0 ; i<devices ; i++ ) {
1720 int dm = (mdevs[i]->dev_midi-2)>>4;
1721 if ( mdevs[i]->mout.ep != NULL && mdevs[i]->min.ep != NULL ) {
1722 printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%2d) out (ep:%02x cid:%2d bufsiz:%2d)\n",
1723 dm,
1724 mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize,
1725 mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1726 } else if ( mdevs[i]->min.ep != NULL ) {
1727 printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%02d)\n",
1728 dm,
1729 mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize);
1730 } else if ( mdevs[i]->mout.ep != NULL ) {
1731 printk(KERN_INFO "usbmidi: /dev/midi%02d: out (ep:%02x cid:%2d bufsiz:%02d)\n",
1732 dm,
1733 mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1734 }
1735 }
1736
1737 kfree(mdevs);
1738 return 0;
1739
1740 error_end:
1741 if ( mdevs != NULL ) {
1742 for ( i=0 ; i<devices ; i++ ) {
1743 if ( mdevs[i] != NULL ) {
1744 unregister_sound_midi( mdevs[i]->dev_midi );
1745 kfree(mdevs[i]);
1746 }
1747 }
1748 kfree(mdevs);
1749 }
1750
1751 for ( i=0 ; i<15 ; i++ ) {
1752 if ( mins[i] != NULL ) {
1753 remove_midi_in_endpoint( mins[i] );
1754 }
1755 if ( mouts[i] != NULL ) {
1756 remove_midi_out_endpoint( mouts[i] );
1757 }
1758 }
1759
1760 return -ENOMEM;
1761}
1762
1763/* ------------------------------------------------------------------------- */
1764
1765/** Attempt to scan YAMAHA's device descriptor and detect correct values of
1766 * them.
1767 * Return 0 on succes, negative on failure.
1768 * Called by usb_midi_probe();
1769 **/
1770
1771static int detect_yamaha_device( struct usb_device *d,
1772 struct usb_interface *iface, unsigned int ifnum,
1773 struct usb_midi_state *s)
1774{
1775 struct usb_host_interface *interface;
1776 struct usb_midi_device *u;
1777 unsigned char *buffer;
1778 int bufSize;
1779 int i;
1780 int alts=-1;
1781 int ret;
1782
1783 if (le16_to_cpu(d->descriptor.idVendor) != USB_VENDOR_ID_YAMAHA) {
1784 return -EINVAL;
1785 }
1786
1787 for ( i=0 ; i < iface->num_altsetting; i++ ) {
1788 interface = iface->altsetting + i;
1789
1790 if ( interface->desc.bInterfaceClass != 255 ||
1791 interface->desc.bInterfaceSubClass != 0 )
1792 continue;
1793 alts = interface->desc.bAlternateSetting;
1794 }
1795 if ( alts == -1 ) {
1796 return -EINVAL;
1797 }
1798
1799 printk(KERN_INFO "usb-midi: Found YAMAHA USB-MIDI device on dev %04x:%04x, iface %d\n",
1800 le16_to_cpu(d->descriptor.idVendor),
1801 le16_to_cpu(d->descriptor.idProduct), ifnum);
1802
1803 i = d->actconfig - d->config;
1804 buffer = d->rawdescriptors[i];
1805 bufSize = le16_to_cpu(d->actconfig->desc.wTotalLength);
1806
1807 u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 1);
1808 if ( u == NULL ) {
1809 return -EINVAL;
1810 }
1811
1812 ret = alloc_usb_midi_device( d, s, u );
1813
1814 kfree(u);
1815
1816 return ret;
1817}
1818
1819
1820/** Scan table of known devices which are only partially compliant with
1821 * the MIDIStreaming specification.
1822 * Called by usb_midi_probe();
1823 *
1824 **/
1825
1826static int detect_vendor_specific_device( struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s )
1827{
1828 struct usb_midi_device *u;
1829 int i;
1830 int ret = -ENXIO;
1831
1832 for ( i=0; i<VENDOR_SPECIFIC_USB_MIDI_DEVICES ; i++ ) {
1833 u=&(usb_midi_devices[i]);
1834
1835 if ( le16_to_cpu(d->descriptor.idVendor) != u->idVendor ||
1836 le16_to_cpu(d->descriptor.idProduct) != u->idProduct ||
1837 ifnum != u->interface )
1838 continue;
1839
1840 ret = alloc_usb_midi_device( d, s, u );
1841 break;
1842 }
1843
1844 return ret;
1845}
1846
1847
1848/** Attempt to match any config of an interface to a MIDISTREAMING interface.
1849 * Returns 0 on success, negative on failure.
1850 * Called by usb_midi_probe();
1851 **/
1852static int detect_midi_subclass(struct usb_device *d,
1853 struct usb_interface *iface, unsigned int ifnum,
1854 struct usb_midi_state *s)
1855{
1856 struct usb_host_interface *interface;
1857 struct usb_midi_device *u;
1858 unsigned char *buffer;
1859 int bufSize;
1860 int i;
1861 int alts=-1;
1862 int ret;
1863
1864 for ( i=0 ; i < iface->num_altsetting; i++ ) {
1865 interface = iface->altsetting + i;
1866
1867 if ( interface->desc.bInterfaceClass != USB_CLASS_AUDIO ||
1868 interface->desc.bInterfaceSubClass != USB_SUBCLASS_MIDISTREAMING )
1869 continue;
1870 alts = interface->desc.bAlternateSetting;
1871 }
1872 if ( alts == -1 ) {
1873 return -EINVAL;
1874 }
1875
1876 printk(KERN_INFO "usb-midi: Found MIDISTREAMING on dev %04x:%04x, iface %d\n",
1877 le16_to_cpu(d->descriptor.idVendor),
1878 le16_to_cpu(d->descriptor.idProduct), ifnum);
1879
1880
1881 /* From USB Spec v2.0, Section 9.5.
1882 If the class or vendor specific descriptors use the same format
1883 as standard descriptors (e.g., start with a length byte and
1884 followed by a type byte), they must be returned interleaved with
1885 standard descriptors in the configuration information returned by
1886 a GetDescriptor(Configuration) request. In this case, the class
1887 or vendor-specific descriptors must follow a related standard
1888 descriptor they modify or extend.
1889 */
1890
1891 i = d->actconfig - d->config;
1892 buffer = d->rawdescriptors[i];
1893 bufSize = le16_to_cpu(d->actconfig->desc.wTotalLength);
1894
1895 u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 0);
1896 if ( u == NULL ) {
1897 return -EINVAL;
1898 }
1899
1900 ret = alloc_usb_midi_device( d, s, u );
1901
1902 kfree(u);
1903
1904 return ret;
1905}
1906
1907
1908/** When user has requested a specific device, match it exactly.
1909 *
1910 * Uses uvendor, uproduct, uinterface, ualt, umin, umout and ucable.
1911 * Called by usb_midi_probe();
1912 *
1913 **/
1914static int detect_by_hand(struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s)
1915{
1916 struct usb_midi_device u;
1917
1918 if ( le16_to_cpu(d->descriptor.idVendor) != uvendor ||
1919 le16_to_cpu(d->descriptor.idProduct) != uproduct ||
1920 ifnum != uinterface ) {
1921 return -EINVAL;
1922 }
1923
1924 if ( ualt < 0 )
1925 ualt = -1;
1926
1927 if ( umin < 0 || umin > 15 )
1928 umin = 0x01 | USB_DIR_IN;
1929 if ( umout < 0 || umout > 15 )
1930 umout = 0x01;
1931 if ( ucable < 0 || ucable > 15 )
1932 ucable = 0;
1933
1934 u.deviceName = NULL; /* A flag for alloc_usb_midi_device to get device
1935 name from device. */
1936 u.idVendor = uvendor;
1937 u.idProduct = uproduct;
1938 u.interface = uinterface;
1939 u.altSetting = ualt;
1940
1941 u.in[0].endpoint = umin;
1942 u.in[0].cableId = (1<<ucable);
1943
1944 u.out[0].endpoint = umout;
1945 u.out[0].cableId = (1<<ucable);
1946
1947 return alloc_usb_midi_device( d, s, &u );
1948}
1949
1950
1951
1952/* ------------------------------------------------------------------------- */
1953
1954static int usb_midi_probe(struct usb_interface *intf,
1955 const struct usb_device_id *id)
1956{
1957 struct usb_midi_state *s;
1958 struct usb_device *dev = interface_to_usbdev(intf);
1959 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1960
1961 s = (struct usb_midi_state *)kmalloc(sizeof(struct usb_midi_state), GFP_KERNEL);
1962 if ( !s )
1963 return -ENOMEM;
1964
1965 memset( s, 0, sizeof(struct usb_midi_state) );
1966 INIT_LIST_HEAD(&s->midiDevList);
1967 INIT_LIST_HEAD(&s->inEndpointList);
1968 INIT_LIST_HEAD(&s->outEndpointList);
1969 s->usbdev = dev;
1970 s->count = 0;
1971 spin_lock_init(&s->lock);
1972
1973 if (
1974 detect_by_hand( dev, ifnum, s ) &&
1975 detect_midi_subclass( dev, intf, ifnum, s ) &&
1976 detect_vendor_specific_device( dev, ifnum, s ) &&
1977 detect_yamaha_device( dev, intf, ifnum, s) ) {
1978 kfree(s);
1979 return -EIO;
1980 }
1981
1982 down(&open_sem);
1983 list_add_tail(&s->mididev, &mididevs);
1984 up(&open_sem);
1985
1986 usb_set_intfdata (intf, s);
1987 return 0;
1988}
1989
1990
1991static void usb_midi_disconnect(struct usb_interface *intf)
1992{
1993 struct usb_midi_state *s = usb_get_intfdata (intf);
1994 struct usb_mididev *m;
1995
1996 if ( !s )
1997 return;
1998
1999 if ( s == (struct usb_midi_state *)-1 ) {
2000 return;
2001 }
2002 if ( !s->usbdev ) {
2003 return;
2004 }
2005 down(&open_sem);
2006 list_del(&s->mididev);
2007 INIT_LIST_HEAD(&s->mididev);
2008 s->usbdev = NULL;
2009 usb_set_intfdata (intf, NULL);
2010
2011 list_for_each_entry(m, &s->midiDevList, list) {
2012 wake_up(&(m->min.ep->wait));
2013 wake_up(&(m->mout.ep->wait));
2014 if ( m->dev_midi >= 0 ) {
2015 unregister_sound_midi(m->dev_midi);
2016 }
2017 m->dev_midi = -1;
2018 }
2019 release_midi_device(s);
2020 wake_up(&open_wait);
2021}
2022
2023/* we want to look at all devices by hand */
2024static struct usb_device_id id_table[] = {
2025 {.driver_info = 42},
2026 {}
2027};
2028
2029static struct usb_driver usb_midi_driver = {
2030 .name = "midi",
2031 .probe = usb_midi_probe,
2032 .disconnect = usb_midi_disconnect,
2033 .id_table = id_table,
2034};
2035
2036/* ------------------------------------------------------------------------- */
2037
2038static int __init usb_midi_init(void)
2039{
2040 return usb_register(&usb_midi_driver);
2041}
2042
2043static void __exit usb_midi_exit(void)
2044{
2045 usb_deregister(&usb_midi_driver);
2046}
2047
2048module_init(usb_midi_init) ;
2049module_exit(usb_midi_exit) ;
2050
2051#ifdef HAVE_ALSA_SUPPORT
2052#define SNDRV_MAIN_OBJECT_FILE
2053#include "../../include/driver.h"
2054#include "../../include/control.h"
2055#include "../../include/info.h"
2056#include "../../include/cs46xx.h"
2057
2058/* ------------------------------------------------------------------------- */
2059
2060static int snd_usbmidi_input_close(snd_rawmidi_substream_t * substream)
2061{
2062 return 0;
2063}
2064
2065static int snd_usbmidi_input_open(snd_rawmidi_substream_t * substream )
2066{
2067 return 0;
2068}
2069
2070static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t * substream, int up)
2071{
2072 return 0;
2073}
2074
2075
2076/* ------------------------------------------------------------------------- */
2077
2078static int snd_usbmidi_output_close(snd_rawmidi_substream_t * substream)
2079{
2080 return 0;
2081}
2082
2083static int snd_usbmidi_output_open(snd_rawmidi_substream_t * substream)
2084{
2085 return 0;
2086}
2087
2088static void snd_usb_midi_output_trigger(snd_rawmidi_substream_t * substream,
2089 int up)
2090{
2091 return 0;
2092}
2093
2094/* ------------------------------------------------------------------------- */
2095
2096static snd_rawmidi_ops_t snd_usbmidi_output =
2097{
2098 .open = snd_usbmidi_output_open,
2099 .close = snd_usbmidi_output_close,
2100 .trigger = snd_usbmidi_output_trigger,
2101};
2102static snd_rawmidi_ops_t snd_usbmidi_input =
2103{
2104 .open = snd_usbmidi_input_open,
2105 .close = snd_usbmidi_input_close,
2106 .trigger = snd_usbmidi_input_trigger,
2107};
2108
2109int snd_usbmidi_midi(cs46xx_t *chip, int device, snd_rawmidi_t **rrawmidi)
2110{
2111 snd_rawmidi_t *rmidi;
2112 int err;
2113
2114 if (rrawmidi)
2115 *rrawmidi = NULL;
2116 if ((err = snd_rawmidi_new(chip->card, "USB-MIDI", device, 1, 1, &rmidi)) < 0)
2117 return err;
2118 strcpy(rmidi->name, "USB-MIDI");
2119
2120 snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output );
2121 snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input );
2122
2123 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
2124
2125 rmidi->private_data = chip;
2126 chip->rmidi = rmidi;
2127 if (rrawmidi)
2128 *rrawmidi = NULL;
2129
2130 return 0;
2131}
2132
2133int snd_usbmidi_create( snd_card_t * card,
2134 struct pci_dev * pci,
2135 usbmidi_t ** rchip )
2136{
2137 usbmidi_t *chip;
2138 int err, idx;
2139 snd_region_t *region;
2140 static snd_device_opt_t ops = {
2141 .dev_free = snd_usbmidi_dev_free,
2142 };
2143
2144 *rchip = NULL;
2145 chip = snd_magic_kcalloc( usbmidi_t, 0, GFP_KERNEL );
2146 if ( chip == NULL )
2147 return -ENOMEM;
2148}
2149
2150EXPORT_SYMBOL(snd_usbmidi_create);
2151EXPORT_SYMBOL(snd_usbmidi_midi);
2152#endif /* HAVE_ALSA_SUPPORT */
2153
diff --git a/drivers/usb/class/usb-midi.h b/drivers/usb/class/usb-midi.h
deleted file mode 100644
index 358cdef8492e..000000000000
--- a/drivers/usb/class/usb-midi.h
+++ /dev/null
@@ -1,164 +0,0 @@
1/*
2 usb-midi.h -- USB-MIDI driver
3
4 Copyright (C) 2001
5 NAGANO Daisuke <breeze.nagano@nifty.ne.jp>
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, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22/* ------------------------------------------------------------------------- */
23
24#ifndef _USB_MIDI_H_
25#define _USB_MIDI_H_
26
27#ifndef USB_SUBCLASS_MIDISTREAMING
28#define USB_SUBCLASS_MIDISTREAMING 3
29#endif
30
31/* ------------------------------------------------------------------------- */
32/* Roland MIDI Devices */
33
34#define USB_VENDOR_ID_ROLAND 0x0582
35#define USBMIDI_ROLAND_UA100G 0x0000
36#define USBMIDI_ROLAND_MPU64 0x0002
37#define USBMIDI_ROLAND_SC8850 0x0003
38#define USBMIDI_ROLAND_SC8820 0x0007
39#define USBMIDI_ROLAND_UM2 0x0005
40#define USBMIDI_ROLAND_UM1 0x0009
41#define USBMIDI_ROLAND_PC300 0x0008
42
43/* YAMAHA MIDI Devices */
44#define USB_VENDOR_ID_YAMAHA 0x0499
45#define USBMIDI_YAMAHA_MU1000 0x1001
46
47/* Steinberg MIDI Devices */
48#define USB_VENDOR_ID_STEINBERG 0x0763
49#define USBMIDI_STEINBERG_USB2MIDI 0x1001
50
51/* Mark of the Unicorn MIDI Devices */
52#define USB_VENDOR_ID_MOTU 0x07fd
53#define USBMIDI_MOTU_FASTLANE 0x0001
54
55/* ------------------------------------------------------------------------- */
56/* Supported devices */
57
58struct usb_midi_endpoint {
59 int endpoint;
60 int cableId; /* if bit-n == 1 then cableId-n is enabled (n: 0 - 15) */
61};
62
63struct usb_midi_device {
64 char *deviceName;
65
66 u16 idVendor;
67 u16 idProduct;
68 int interface;
69 int altSetting; /* -1: auto detect */
70
71 struct usb_midi_endpoint in[15];
72 struct usb_midi_endpoint out[15];
73};
74
75static struct usb_midi_device usb_midi_devices[] = {
76 { /* Roland UM-1 */
77 "Roland UM-1",
78 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM1, 2, -1,
79 { { 0x81, 1 }, {-1, -1} },
80 { { 0x01, 1,}, {-1, -1} },
81 },
82
83 { /* Roland UM-2 */
84 "Roland UM-2" ,
85 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM2, 2, -1,
86 { { 0x81, 3 }, {-1, -1} },
87 { { 0x01, 3,}, {-1, -1} },
88 },
89
90/** Next entry courtesy research by Michael Minn <michael@michaelminn.com> **/
91 { /* Roland UA-100 */
92 "Roland UA-100",
93 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UA100G, 2, -1,
94 { { 0x82, 7 }, {-1, -1} }, /** cables 0,1 and 2 for SYSEX **/
95 { { 0x02, 7 }, {-1, -1} },
96 },
97
98/** Next entry courtesy research by Michael Minn <michael@michaelminn.com> **/
99 { /* Roland SC8850 */
100 "Roland SC8850",
101 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8850, 2, -1,
102 { { 0x81, 0x3f }, {-1, -1} },
103 { { 0x01, 0x3f }, {-1, -1} },
104 },
105
106 { /* Roland SC8820 */
107 "Roland SC8820",
108 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8820, 2, -1,
109 { { 0x81, 0x13 }, {-1, -1} },
110 { { 0x01, 0x13 }, {-1, -1} },
111 },
112
113 { /* Roland SC8820 */
114 "Roland SC8820",
115 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8820, 2, -1,
116 { { 0x81, 17 }, {-1, -1} },
117 { { 0x01, 17 }, {-1, -1} },
118 },
119
120 { /* YAMAHA MU1000 */
121 "YAMAHA MU1000",
122 USB_VENDOR_ID_YAMAHA, USBMIDI_YAMAHA_MU1000, 0, -1,
123 { { 0x81, 1 }, {-1, -1} },
124 { { 0x01, 15 }, {-1, -1} },
125 },
126 { /* Roland PC-300 */
127 "Roland PC-300",
128 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_PC300, 2, -1,
129 { { 0x81, 1 }, {-1, -1} },
130 { { 0x01, 1 }, {-1, -1} },
131 },
132 { /* MOTU Fastlane USB */
133 "MOTU Fastlane USB",
134 USB_VENDOR_ID_MOTU, USBMIDI_MOTU_FASTLANE, 1, 0,
135 { { 0x82, 3 }, {-1, -1} },
136 { { 0x02, 3 }, {-1, -1} },
137 }
138};
139
140#define VENDOR_SPECIFIC_USB_MIDI_DEVICES (sizeof(usb_midi_devices)/sizeof(struct usb_midi_device))
141
142/* for Hot-Plugging */
143
144static struct usb_device_id usb_midi_ids [] = {
145 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
146 .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING},
147 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM1 ) },
148 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM2 ) },
149 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UA100G ) },
150 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_PC300 ) },
151 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8850 ) },
152 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8820 ) },
153 { USB_DEVICE( USB_VENDOR_ID_YAMAHA, USBMIDI_YAMAHA_MU1000 ) },
154 { USB_DEVICE( USB_VENDOR_ID_MOTU, USBMIDI_MOTU_FASTLANE ) },
155/* { USB_DEVICE( USB_VENDOR_ID_STEINBERG, USBMIDI_STEINBERG_USB2MIDI ) },*/
156 { } /* Terminating entry */
157};
158
159MODULE_DEVICE_TABLE (usb, usb_midi_ids);
160
161/* ------------------------------------------------------------------------- */
162#endif /* _USB_MIDI_H_ */
163
164
diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c
index d34848ac30b0..48dee4b8d8e5 100644
--- a/drivers/usb/class/usblp.c
+++ b/drivers/usb/class/usblp.c
@@ -55,6 +55,7 @@
55#include <linux/init.h> 55#include <linux/init.h>
56#include <linux/slab.h> 56#include <linux/slab.h>
57#include <linux/lp.h> 57#include <linux/lp.h>
58#include <linux/mutex.h>
58#undef DEBUG 59#undef DEBUG
59#include <linux/usb.h> 60#include <linux/usb.h>
60 61
@@ -223,7 +224,7 @@ static int usblp_cache_device_id_string(struct usblp *usblp);
223 224
224/* forward reference to make our lives easier */ 225/* forward reference to make our lives easier */
225static struct usb_driver usblp_driver; 226static struct usb_driver usblp_driver;
226static DECLARE_MUTEX(usblp_sem); /* locks the existence of usblp's */ 227static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
227 228
228/* 229/*
229 * Functions for usblp control messages. 230 * Functions for usblp control messages.
@@ -351,7 +352,7 @@ static int usblp_open(struct inode *inode, struct file *file)
351 if (minor < 0) 352 if (minor < 0)
352 return -ENODEV; 353 return -ENODEV;
353 354
354 down (&usblp_sem); 355 mutex_lock (&usblp_mutex);
355 356
356 retval = -ENODEV; 357 retval = -ENODEV;
357 intf = usb_find_interface(&usblp_driver, minor); 358 intf = usb_find_interface(&usblp_driver, minor);
@@ -399,7 +400,7 @@ static int usblp_open(struct inode *inode, struct file *file)
399 } 400 }
400 } 401 }
401out: 402out:
402 up (&usblp_sem); 403 mutex_unlock (&usblp_mutex);
403 return retval; 404 return retval;
404} 405}
405 406
@@ -425,13 +426,13 @@ static int usblp_release(struct inode *inode, struct file *file)
425{ 426{
426 struct usblp *usblp = file->private_data; 427 struct usblp *usblp = file->private_data;
427 428
428 down (&usblp_sem); 429 mutex_lock (&usblp_mutex);
429 usblp->used = 0; 430 usblp->used = 0;
430 if (usblp->present) { 431 if (usblp->present) {
431 usblp_unlink_urbs(usblp); 432 usblp_unlink_urbs(usblp);
432 } else /* finish cleanup from disconnect */ 433 } else /* finish cleanup from disconnect */
433 usblp_cleanup (usblp); 434 usblp_cleanup (usblp);
434 up (&usblp_sem); 435 mutex_unlock (&usblp_mutex);
435 return 0; 436 return 0;
436} 437}
437 438
@@ -1152,7 +1153,7 @@ static void usblp_disconnect(struct usb_interface *intf)
1152 1153
1153 device_remove_file(&intf->dev, &dev_attr_ieee1284_id); 1154 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1154 1155
1155 down (&usblp_sem); 1156 mutex_lock (&usblp_mutex);
1156 down (&usblp->sem); 1157 down (&usblp->sem);
1157 usblp->present = 0; 1158 usblp->present = 0;
1158 usb_set_intfdata (intf, NULL); 1159 usb_set_intfdata (intf, NULL);
@@ -1166,7 +1167,7 @@ static void usblp_disconnect(struct usb_interface *intf)
1166 1167
1167 if (!usblp->used) 1168 if (!usblp->used)
1168 usblp_cleanup (usblp); 1169 usblp_cleanup (usblp);
1169 up (&usblp_sem); 1170 mutex_unlock (&usblp_mutex);
1170} 1171}
1171 1172
1172static struct usb_device_id usblp_ids [] = { 1173static struct usb_device_id usblp_ids [] = {
diff --git a/drivers/usb/core/devices.c b/drivers/usb/core/devices.c
index 2684e15b813b..c0f37343a276 100644
--- a/drivers/usb/core/devices.c
+++ b/drivers/usb/core/devices.c
@@ -57,6 +57,7 @@
57#include <linux/usb.h> 57#include <linux/usb.h>
58#include <linux/smp_lock.h> 58#include <linux/smp_lock.h>
59#include <linux/usbdevice_fs.h> 59#include <linux/usbdevice_fs.h>
60#include <linux/mutex.h>
60#include <asm/uaccess.h> 61#include <asm/uaccess.h>
61 62
62#include "usb.h" 63#include "usb.h"
@@ -570,7 +571,7 @@ static ssize_t usb_device_read(struct file *file, char __user *buf, size_t nbyte
570 if (!access_ok(VERIFY_WRITE, buf, nbytes)) 571 if (!access_ok(VERIFY_WRITE, buf, nbytes))
571 return -EFAULT; 572 return -EFAULT;
572 573
573 down (&usb_bus_list_lock); 574 mutex_lock(&usb_bus_list_lock);
574 /* print devices for all busses */ 575 /* print devices for all busses */
575 list_for_each_entry(bus, &usb_bus_list, bus_list) { 576 list_for_each_entry(bus, &usb_bus_list, bus_list) {
576 /* recurse through all children of the root hub */ 577 /* recurse through all children of the root hub */
@@ -580,12 +581,12 @@ static ssize_t usb_device_read(struct file *file, char __user *buf, size_t nbyte
580 ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos, bus->root_hub, bus, 0, 0, 0); 581 ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos, bus->root_hub, bus, 0, 0, 0);
581 usb_unlock_device(bus->root_hub); 582 usb_unlock_device(bus->root_hub);
582 if (ret < 0) { 583 if (ret < 0) {
583 up(&usb_bus_list_lock); 584 mutex_unlock(&usb_bus_list_lock);
584 return ret; 585 return ret;
585 } 586 }
586 total_written += ret; 587 total_written += ret;
587 } 588 }
588 up (&usb_bus_list_lock); 589 mutex_unlock(&usb_bus_list_lock);
589 return total_written; 590 return total_written;
590} 591}
591 592
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 2b68998fe4b3..545da37afca7 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -134,26 +134,21 @@ static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, l
134 } 134 }
135 135
136 if (pos < sizeof(struct usb_device_descriptor)) { 136 if (pos < sizeof(struct usb_device_descriptor)) {
137 struct usb_device_descriptor *desc = kmalloc(sizeof(*desc), GFP_KERNEL); 137 struct usb_device_descriptor temp_desc ; /* 18 bytes - fits on the stack */
138 if (!desc) { 138
139 ret = -ENOMEM; 139 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
140 goto err; 140 le16_to_cpus(&temp_desc.bcdUSB);
141 } 141 le16_to_cpus(&temp_desc.idVendor);
142 memcpy(desc, &dev->descriptor, sizeof(dev->descriptor)); 142 le16_to_cpus(&temp_desc.idProduct);
143 le16_to_cpus(&desc->bcdUSB); 143 le16_to_cpus(&temp_desc.bcdDevice);
144 le16_to_cpus(&desc->idVendor);
145 le16_to_cpus(&desc->idProduct);
146 le16_to_cpus(&desc->bcdDevice);
147 144
148 len = sizeof(struct usb_device_descriptor) - pos; 145 len = sizeof(struct usb_device_descriptor) - pos;
149 if (len > nbytes) 146 if (len > nbytes)
150 len = nbytes; 147 len = nbytes;
151 if (copy_to_user(buf, ((char *)desc) + pos, len)) { 148 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
152 kfree(desc);
153 ret = -EFAULT; 149 ret = -EFAULT;
154 goto err; 150 goto err;
155 } 151 }
156 kfree(desc);
157 152
158 *ppos += len; 153 *ppos += len;
159 buf += len; 154 buf += len;
@@ -498,7 +493,8 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsig
498{ 493{
499 int ret = 0; 494 int ret = 0;
500 495
501 if (ps->dev->state != USB_STATE_CONFIGURED) 496 if (ps->dev->state != USB_STATE_ADDRESS
497 && ps->dev->state != USB_STATE_CONFIGURED)
502 return -EHOSTUNREACH; 498 return -EHOSTUNREACH;
503 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype)) 499 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
504 return 0; 500 return 0;
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index dce9d987f0fc..c196f3845305 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -378,7 +378,7 @@ const struct usb_device_id *usb_match_id(struct usb_interface *interface,
378 378
379 return NULL; 379 return NULL;
380} 380}
381EXPORT_SYMBOL(usb_match_id); 381EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
382 382
383int usb_device_match(struct device *dev, struct device_driver *drv) 383int usb_device_match(struct device *dev, struct device_driver *drv)
384{ 384{
@@ -446,7 +446,7 @@ int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
446 446
447 return retval; 447 return retval;
448} 448}
449EXPORT_SYMBOL(usb_register_driver); 449EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
450 450
451/** 451/**
452 * usb_deregister - unregister a USB driver 452 * usb_deregister - unregister a USB driver
@@ -469,4 +469,4 @@ void usb_deregister(struct usb_driver *driver)
469 469
470 usbfs_update_special(); 470 usbfs_update_special();
471} 471}
472EXPORT_SYMBOL(usb_deregister); 472EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
diff --git a/drivers/usb/core/file.c b/drivers/usb/core/file.c
index 37b13368c814..b263a54a13c0 100644
--- a/drivers/usb/core/file.c
+++ b/drivers/usb/core/file.c
@@ -24,15 +24,15 @@
24#include "usb.h" 24#include "usb.h"
25 25
26#define MAX_USB_MINORS 256 26#define MAX_USB_MINORS 256
27static struct file_operations *usb_minors[MAX_USB_MINORS]; 27static const struct file_operations *usb_minors[MAX_USB_MINORS];
28static DEFINE_SPINLOCK(minor_lock); 28static DEFINE_SPINLOCK(minor_lock);
29 29
30static int usb_open(struct inode * inode, struct file * file) 30static int usb_open(struct inode * inode, struct file * file)
31{ 31{
32 int minor = iminor(inode); 32 int minor = iminor(inode);
33 struct file_operations *c; 33 const struct file_operations *c;
34 int err = -ENODEV; 34 int err = -ENODEV;
35 struct file_operations *old_fops, *new_fops = NULL; 35 const struct file_operations *old_fops, *new_fops = NULL;
36 36
37 spin_lock (&minor_lock); 37 spin_lock (&minor_lock);
38 c = usb_minors[minor]; 38 c = usb_minors[minor];
diff --git a/drivers/usb/core/hcd-pci.c b/drivers/usb/core/hcd-pci.c
index 29b5b2a6e183..0d2193b69235 100644
--- a/drivers/usb/core/hcd-pci.c
+++ b/drivers/usb/core/hcd-pci.c
@@ -264,14 +264,19 @@ int usb_hcd_pci_suspend (struct pci_dev *dev, pm_message_t message)
264 */ 264 */
265 retval = pci_set_power_state (dev, PCI_D3hot); 265 retval = pci_set_power_state (dev, PCI_D3hot);
266 if (retval == 0) { 266 if (retval == 0) {
267 dev_dbg (hcd->self.controller, "--> PCI D3\n"); 267 int wake = device_can_wakeup(&hcd->self.root_hub->dev);
268
269 wake = wake && device_may_wakeup(hcd->self.controller);
270
271 dev_dbg (hcd->self.controller, "--> PCI D3%s\n",
272 wake ? "/wakeup" : "");
268 273
269 /* Ignore these return values. We rely on pci code to 274 /* Ignore these return values. We rely on pci code to
270 * reject requests the hardware can't implement, rather 275 * reject requests the hardware can't implement, rather
271 * than coding the same thing. 276 * than coding the same thing.
272 */ 277 */
273 (void) pci_enable_wake (dev, PCI_D3hot, hcd->remote_wakeup); 278 (void) pci_enable_wake (dev, PCI_D3hot, wake);
274 (void) pci_enable_wake (dev, PCI_D3cold, hcd->remote_wakeup); 279 (void) pci_enable_wake (dev, PCI_D3cold, wake);
275 } else { 280 } else {
276 dev_dbg (&dev->dev, "PCI D3 suspend fail, %d\n", 281 dev_dbg (&dev->dev, "PCI D3 suspend fail, %d\n",
277 retval); 282 retval);
@@ -291,7 +296,7 @@ done:
291 296
292#ifdef CONFIG_PPC_PMAC 297#ifdef CONFIG_PPC_PMAC
293 /* Disable ASIC clocks for USB */ 298 /* Disable ASIC clocks for USB */
294 if (_machine == _MACH_Pmac) { 299 if (machine_is(powermac)) {
295 struct device_node *of_node; 300 struct device_node *of_node;
296 301
297 of_node = pci_device_to_OF_node (dev); 302 of_node = pci_device_to_OF_node (dev);
@@ -326,7 +331,7 @@ int usb_hcd_pci_resume (struct pci_dev *dev)
326 331
327#ifdef CONFIG_PPC_PMAC 332#ifdef CONFIG_PPC_PMAC
328 /* Reenable ASIC clocks for USB */ 333 /* Reenable ASIC clocks for USB */
329 if (_machine == _MACH_Pmac) { 334 if (machine_is(powermac)) {
330 struct device_node *of_node; 335 struct device_node *of_node;
331 336
332 of_node = pci_device_to_OF_node (dev); 337 of_node = pci_device_to_OF_node (dev);
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 0018bbc4de34..fbd938d4ea58 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -34,6 +34,7 @@
34#include <asm/scatterlist.h> 34#include <asm/scatterlist.h>
35#include <linux/device.h> 35#include <linux/device.h>
36#include <linux/dma-mapping.h> 36#include <linux/dma-mapping.h>
37#include <linux/mutex.h>
37#include <asm/irq.h> 38#include <asm/irq.h>
38#include <asm/byteorder.h> 39#include <asm/byteorder.h>
39 40
@@ -93,7 +94,7 @@ struct usb_busmap {
93static struct usb_busmap busmap; 94static struct usb_busmap busmap;
94 95
95/* used when updating list of hcds */ 96/* used when updating list of hcds */
96DECLARE_MUTEX (usb_bus_list_lock); /* exported only for usbfs */ 97DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */
97EXPORT_SYMBOL_GPL (usb_bus_list_lock); 98EXPORT_SYMBOL_GPL (usb_bus_list_lock);
98 99
99/* used for controlling access to virtual root hubs */ 100/* used for controlling access to virtual root hubs */
@@ -366,21 +367,39 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
366 367
367 /* DEVICE REQUESTS */ 368 /* DEVICE REQUESTS */
368 369
370 /* The root hub's remote wakeup enable bit is implemented using
371 * driver model wakeup flags. If this system supports wakeup
372 * through USB, userspace may change the default "allow wakeup"
373 * policy through sysfs or these calls.
374 *
375 * Most root hubs support wakeup from downstream devices, for
376 * runtime power management (disabling USB clocks and reducing
377 * VBUS power usage). However, not all of them do so; silicon,
378 * board, and BIOS bugs here are not uncommon, so these can't
379 * be treated quite like external hubs.
380 *
381 * Likewise, not all root hubs will pass wakeup events upstream,
382 * to wake up the whole system. So don't assume root hub and
383 * controller capabilities are identical.
384 */
385
369 case DeviceRequest | USB_REQ_GET_STATUS: 386 case DeviceRequest | USB_REQ_GET_STATUS:
370 tbuf [0] = (hcd->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP) 387 tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev)
388 << USB_DEVICE_REMOTE_WAKEUP)
371 | (1 << USB_DEVICE_SELF_POWERED); 389 | (1 << USB_DEVICE_SELF_POWERED);
372 tbuf [1] = 0; 390 tbuf [1] = 0;
373 len = 2; 391 len = 2;
374 break; 392 break;
375 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: 393 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
376 if (wValue == USB_DEVICE_REMOTE_WAKEUP) 394 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
377 hcd->remote_wakeup = 0; 395 device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
378 else 396 else
379 goto error; 397 goto error;
380 break; 398 break;
381 case DeviceOutRequest | USB_REQ_SET_FEATURE: 399 case DeviceOutRequest | USB_REQ_SET_FEATURE:
382 if (hcd->can_wakeup && wValue == USB_DEVICE_REMOTE_WAKEUP) 400 if (device_can_wakeup(&hcd->self.root_hub->dev)
383 hcd->remote_wakeup = 1; 401 && wValue == USB_DEVICE_REMOTE_WAKEUP)
402 device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
384 else 403 else
385 goto error; 404 goto error;
386 break; 405 break;
@@ -409,7 +428,7 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
409 bufp = fs_rh_config_descriptor; 428 bufp = fs_rh_config_descriptor;
410 len = sizeof fs_rh_config_descriptor; 429 len = sizeof fs_rh_config_descriptor;
411 } 430 }
412 if (hcd->can_wakeup) 431 if (device_can_wakeup(&hcd->self.root_hub->dev))
413 patch_wakeup = 1; 432 patch_wakeup = 1;
414 break; 433 break;
415 case USB_DT_STRING << 8: 434 case USB_DT_STRING << 8:
@@ -761,14 +780,14 @@ static int usb_register_bus(struct usb_bus *bus)
761{ 780{
762 int busnum; 781 int busnum;
763 782
764 down (&usb_bus_list_lock); 783 mutex_lock(&usb_bus_list_lock);
765 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1); 784 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
766 if (busnum < USB_MAXBUS) { 785 if (busnum < USB_MAXBUS) {
767 set_bit (busnum, busmap.busmap); 786 set_bit (busnum, busmap.busmap);
768 bus->busnum = busnum; 787 bus->busnum = busnum;
769 } else { 788 } else {
770 printk (KERN_ERR "%s: too many buses\n", usbcore_name); 789 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
771 up(&usb_bus_list_lock); 790 mutex_unlock(&usb_bus_list_lock);
772 return -E2BIG; 791 return -E2BIG;
773 } 792 }
774 793
@@ -776,7 +795,7 @@ static int usb_register_bus(struct usb_bus *bus)
776 bus->controller, "usb_host%d", busnum); 795 bus->controller, "usb_host%d", busnum);
777 if (IS_ERR(bus->class_dev)) { 796 if (IS_ERR(bus->class_dev)) {
778 clear_bit(busnum, busmap.busmap); 797 clear_bit(busnum, busmap.busmap);
779 up(&usb_bus_list_lock); 798 mutex_unlock(&usb_bus_list_lock);
780 return PTR_ERR(bus->class_dev); 799 return PTR_ERR(bus->class_dev);
781 } 800 }
782 801
@@ -784,7 +803,7 @@ static int usb_register_bus(struct usb_bus *bus)
784 803
785 /* Add it to the local list of buses */ 804 /* Add it to the local list of buses */
786 list_add (&bus->bus_list, &usb_bus_list); 805 list_add (&bus->bus_list, &usb_bus_list);
787 up (&usb_bus_list_lock); 806 mutex_unlock(&usb_bus_list_lock);
788 807
789 usb_notify_add_bus(bus); 808 usb_notify_add_bus(bus);
790 809
@@ -809,9 +828,9 @@ static void usb_deregister_bus (struct usb_bus *bus)
809 * controller code, as well as having it call this when cleaning 828 * controller code, as well as having it call this when cleaning
810 * itself up 829 * itself up
811 */ 830 */
812 down (&usb_bus_list_lock); 831 mutex_lock(&usb_bus_list_lock);
813 list_del (&bus->bus_list); 832 list_del (&bus->bus_list);
814 up (&usb_bus_list_lock); 833 mutex_unlock(&usb_bus_list_lock);
815 834
816 usb_notify_remove_bus(bus); 835 usb_notify_remove_bus(bus);
817 836
@@ -822,18 +841,17 @@ static void usb_deregister_bus (struct usb_bus *bus)
822 841
823/** 842/**
824 * register_root_hub - called by usb_add_hcd() to register a root hub 843 * register_root_hub - called by usb_add_hcd() to register a root hub
825 * @usb_dev: the usb root hub device to be registered.
826 * @hcd: host controller for this root hub 844 * @hcd: host controller for this root hub
827 * 845 *
828 * This function registers the root hub with the USB subsystem. It sets up 846 * This function registers the root hub with the USB subsystem. It sets up
829 * the device properly in the device tree and stores the root_hub pointer 847 * the device properly in the device tree and then calls usb_new_device()
830 * in the bus structure, then calls usb_new_device() to register the usb 848 * to register the usb device. It also assigns the root hub's USB address
831 * device. It also assigns the root hub's USB address (always 1). 849 * (always 1).
832 */ 850 */
833static int register_root_hub (struct usb_device *usb_dev, 851static int register_root_hub(struct usb_hcd *hcd)
834 struct usb_hcd *hcd)
835{ 852{
836 struct device *parent_dev = hcd->self.controller; 853 struct device *parent_dev = hcd->self.controller;
854 struct usb_device *usb_dev = hcd->self.root_hub;
837 const int devnum = 1; 855 const int devnum = 1;
838 int retval; 856 int retval;
839 857
@@ -844,14 +862,12 @@ static int register_root_hub (struct usb_device *usb_dev,
844 set_bit (devnum, usb_dev->bus->devmap.devicemap); 862 set_bit (devnum, usb_dev->bus->devmap.devicemap);
845 usb_set_device_state(usb_dev, USB_STATE_ADDRESS); 863 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
846 864
847 down (&usb_bus_list_lock); 865 mutex_lock(&usb_bus_list_lock);
848 usb_dev->bus->root_hub = usb_dev;
849 866
850 usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64); 867 usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
851 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE); 868 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
852 if (retval != sizeof usb_dev->descriptor) { 869 if (retval != sizeof usb_dev->descriptor) {
853 usb_dev->bus->root_hub = NULL; 870 mutex_unlock(&usb_bus_list_lock);
854 up (&usb_bus_list_lock);
855 dev_dbg (parent_dev, "can't read %s device descriptor %d\n", 871 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
856 usb_dev->dev.bus_id, retval); 872 usb_dev->dev.bus_id, retval);
857 return (retval < 0) ? retval : -EMSGSIZE; 873 return (retval < 0) ? retval : -EMSGSIZE;
@@ -859,11 +875,10 @@ static int register_root_hub (struct usb_device *usb_dev,
859 875
860 retval = usb_new_device (usb_dev); 876 retval = usb_new_device (usb_dev);
861 if (retval) { 877 if (retval) {
862 usb_dev->bus->root_hub = NULL;
863 dev_err (parent_dev, "can't register root hub for %s, %d\n", 878 dev_err (parent_dev, "can't register root hub for %s, %d\n",
864 usb_dev->dev.bus_id, retval); 879 usb_dev->dev.bus_id, retval);
865 } 880 }
866 up (&usb_bus_list_lock); 881 mutex_unlock(&usb_bus_list_lock);
867 882
868 if (retval == 0) { 883 if (retval == 0) {
869 spin_lock_irq (&hcd_root_hub_lock); 884 spin_lock_irq (&hcd_root_hub_lock);
@@ -1090,7 +1105,6 @@ static void urb_unlink (struct urb *urb)
1090 spin_lock_irqsave (&hcd_data_lock, flags); 1105 spin_lock_irqsave (&hcd_data_lock, flags);
1091 list_del_init (&urb->urb_list); 1106 list_del_init (&urb->urb_list);
1092 spin_unlock_irqrestore (&hcd_data_lock, flags); 1107 spin_unlock_irqrestore (&hcd_data_lock, flags);
1093 usb_put_dev (urb->dev);
1094} 1108}
1095 1109
1096 1110
@@ -1130,7 +1144,6 @@ static int hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
1130 case HC_STATE_RUNNING: 1144 case HC_STATE_RUNNING:
1131 case HC_STATE_RESUMING: 1145 case HC_STATE_RESUMING:
1132doit: 1146doit:
1133 usb_get_dev (urb->dev);
1134 list_add_tail (&urb->urb_list, &ep->urb_list); 1147 list_add_tail (&urb->urb_list, &ep->urb_list);
1135 status = 0; 1148 status = 0;
1136 break; 1149 break;
@@ -1771,12 +1784,10 @@ int usb_add_hcd(struct usb_hcd *hcd,
1771 1784
1772 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 1785 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1773 1786
1774 /* till now HC has been in an indeterminate state ... */ 1787 /* HC is in reset state, but accessible. Now do the one-time init,
1775 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) { 1788 * bottom up so that hcds can customize the root hubs before khubd
1776 dev_err(hcd->self.controller, "can't reset\n"); 1789 * starts talking to them. (Note, bus id is assigned early too.)
1777 return retval; 1790 */
1778 }
1779
1780 if ((retval = hcd_buffer_create(hcd)) != 0) { 1791 if ((retval = hcd_buffer_create(hcd)) != 0) {
1781 dev_dbg(hcd->self.controller, "pool alloc failed\n"); 1792 dev_dbg(hcd->self.controller, "pool alloc failed\n");
1782 return retval; 1793 return retval;
@@ -1785,6 +1796,36 @@ int usb_add_hcd(struct usb_hcd *hcd,
1785 if ((retval = usb_register_bus(&hcd->self)) < 0) 1796 if ((retval = usb_register_bus(&hcd->self)) < 0)
1786 goto err_register_bus; 1797 goto err_register_bus;
1787 1798
1799 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
1800 dev_err(hcd->self.controller, "unable to allocate root hub\n");
1801 retval = -ENOMEM;
1802 goto err_allocate_root_hub;
1803 }
1804 rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
1805 USB_SPEED_FULL;
1806 hcd->self.root_hub = rhdev;
1807
1808 /* "reset" is misnamed; its role is now one-time init. the controller
1809 * should already have been reset (and boot firmware kicked off etc).
1810 */
1811 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
1812 dev_err(hcd->self.controller, "can't setup\n");
1813 goto err_hcd_driver_setup;
1814 }
1815
1816 /* wakeup flag init is in transition; for now we can't rely on PCI to
1817 * initialize these bits properly, so we let reset() override it.
1818 * This init should _precede_ the reset() once PCI behaves.
1819 */
1820 device_init_wakeup(&rhdev->dev,
1821 device_can_wakeup(hcd->self.controller));
1822
1823 /* NOTE: root hub and controller capabilities may not be the same */
1824 if (device_can_wakeup(hcd->self.controller)
1825 && device_can_wakeup(&hcd->self.root_hub->dev))
1826 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
1827
1828 /* enable irqs just before we start the controller */
1788 if (hcd->driver->irq) { 1829 if (hcd->driver->irq) {
1789 char buf[8], *bufp = buf; 1830 char buf[8], *bufp = buf;
1790 1831
@@ -1816,56 +1857,32 @@ int usb_add_hcd(struct usb_hcd *hcd,
1816 (unsigned long long)hcd->rsrc_start); 1857 (unsigned long long)hcd->rsrc_start);
1817 } 1858 }
1818 1859
1819 /* Allocate the root hub before calling hcd->driver->start(),
1820 * but don't register it until afterward so that the hardware
1821 * is running.
1822 */
1823 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
1824 dev_err(hcd->self.controller, "unable to allocate root hub\n");
1825 retval = -ENOMEM;
1826 goto err_allocate_root_hub;
1827 }
1828
1829 /* Although in principle hcd->driver->start() might need to use rhdev,
1830 * none of the current drivers do.
1831 */
1832 if ((retval = hcd->driver->start(hcd)) < 0) { 1860 if ((retval = hcd->driver->start(hcd)) < 0) {
1833 dev_err(hcd->self.controller, "startup error %d\n", retval); 1861 dev_err(hcd->self.controller, "startup error %d\n", retval);
1834 goto err_hcd_driver_start; 1862 goto err_hcd_driver_start;
1835 } 1863 }
1836 1864
1837 /* hcd->driver->start() reported can_wakeup, probably with 1865 /* starting here, usbcore will pay attention to this root hub */
1838 * assistance from board's boot firmware.
1839 * NOTE: normal devices won't enable wakeup by default.
1840 */
1841 if (hcd->can_wakeup)
1842 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
1843 hcd->remote_wakeup = hcd->can_wakeup;
1844
1845 rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
1846 USB_SPEED_FULL;
1847 rhdev->bus_mA = min(500u, hcd->power_budget); 1866 rhdev->bus_mA = min(500u, hcd->power_budget);
1848 if ((retval = register_root_hub(rhdev, hcd)) != 0) 1867 if ((retval = register_root_hub(hcd)) != 0)
1849 goto err_register_root_hub; 1868 goto err_register_root_hub;
1850 1869
1851 if (hcd->uses_new_polling && hcd->poll_rh) 1870 if (hcd->uses_new_polling && hcd->poll_rh)
1852 usb_hcd_poll_rh_status(hcd); 1871 usb_hcd_poll_rh_status(hcd);
1853 return retval; 1872 return retval;
1854 1873
1855 err_register_root_hub: 1874err_register_root_hub:
1856 hcd->driver->stop(hcd); 1875 hcd->driver->stop(hcd);
1857 1876err_hcd_driver_start:
1858 err_hcd_driver_start:
1859 usb_put_dev(rhdev);
1860
1861 err_allocate_root_hub:
1862 if (hcd->irq >= 0) 1877 if (hcd->irq >= 0)
1863 free_irq(irqnum, hcd); 1878 free_irq(irqnum, hcd);
1864 1879err_request_irq:
1865 err_request_irq: 1880err_hcd_driver_setup:
1881 hcd->self.root_hub = NULL;
1882 usb_put_dev(rhdev);
1883err_allocate_root_hub:
1866 usb_deregister_bus(&hcd->self); 1884 usb_deregister_bus(&hcd->self);
1867 1885err_register_bus:
1868 err_register_bus:
1869 hcd_buffer_destroy(hcd); 1886 hcd_buffer_destroy(hcd);
1870 return retval; 1887 return retval;
1871} 1888}
@@ -1891,9 +1908,9 @@ void usb_remove_hcd(struct usb_hcd *hcd)
1891 hcd->rh_registered = 0; 1908 hcd->rh_registered = 0;
1892 spin_unlock_irq (&hcd_root_hub_lock); 1909 spin_unlock_irq (&hcd_root_hub_lock);
1893 1910
1894 down(&usb_bus_list_lock); 1911 mutex_lock(&usb_bus_list_lock);
1895 usb_disconnect(&hcd->self.root_hub); 1912 usb_disconnect(&hcd->self.root_hub);
1896 up(&usb_bus_list_lock); 1913 mutex_unlock(&usb_bus_list_lock);
1897 1914
1898 hcd->poll_rh = 0; 1915 hcd->poll_rh = 0;
1899 del_timer_sync(&hcd->rh_timer); 1916 del_timer_sync(&hcd->rh_timer);
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index 591b5aad1a18..7022aafb2ae8 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -78,8 +78,6 @@ struct usb_hcd { /* usb_bus.hcpriv points to this */
78#define HCD_FLAG_HW_ACCESSIBLE 0x00000001 78#define HCD_FLAG_HW_ACCESSIBLE 0x00000001
79#define HCD_FLAG_SAW_IRQ 0x00000002 79#define HCD_FLAG_SAW_IRQ 0x00000002
80 80
81 unsigned can_wakeup:1; /* hw supports wakeup? */
82 unsigned remote_wakeup:1;/* sw should use wakeup? */
83 unsigned rh_registered:1;/* is root hub registered? */ 81 unsigned rh_registered:1;/* is root hub registered? */
84 82
85 /* The next flag is a stopgap, to be removed when all the HCDs 83 /* The next flag is a stopgap, to be removed when all the HCDs
@@ -364,7 +362,7 @@ extern void usb_set_device_state(struct usb_device *udev,
364/* exported only within usbcore */ 362/* exported only within usbcore */
365 363
366extern struct list_head usb_bus_list; 364extern struct list_head usb_bus_list;
367extern struct semaphore usb_bus_list_lock; 365extern struct mutex usb_bus_list_lock;
368extern wait_queue_head_t usb_kill_urb_queue; 366extern wait_queue_head_t usb_kill_urb_queue;
369 367
370extern struct usb_bus *usb_bus_get (struct usb_bus *bus); 368extern struct usb_bus *usb_bus_get (struct usb_bus *bus);
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 650d5ee5871b..8e65f7a237e4 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -22,6 +22,7 @@
22#include <linux/usb.h> 22#include <linux/usb.h>
23#include <linux/usbdevice_fs.h> 23#include <linux/usbdevice_fs.h>
24#include <linux/kthread.h> 24#include <linux/kthread.h>
25#include <linux/mutex.h>
25 26
26#include <asm/semaphore.h> 27#include <asm/semaphore.h>
27#include <asm/uaccess.h> 28#include <asm/uaccess.h>
@@ -1005,12 +1006,18 @@ void usb_set_device_state(struct usb_device *udev,
1005 ; /* do nothing */ 1006 ; /* do nothing */
1006 else if (new_state != USB_STATE_NOTATTACHED) { 1007 else if (new_state != USB_STATE_NOTATTACHED) {
1007 udev->state = new_state; 1008 udev->state = new_state;
1008 if (new_state == USB_STATE_CONFIGURED) 1009
1009 device_init_wakeup(&udev->dev, 1010 /* root hub wakeup capabilities are managed out-of-band
1010 (udev->actconfig->desc.bmAttributes 1011 * and may involve silicon errata ... ignore them here.
1011 & USB_CONFIG_ATT_WAKEUP)); 1012 */
1012 else if (new_state != USB_STATE_SUSPENDED) 1013 if (udev->parent) {
1013 device_init_wakeup(&udev->dev, 0); 1014 if (new_state == USB_STATE_CONFIGURED)
1015 device_init_wakeup(&udev->dev,
1016 (udev->actconfig->desc.bmAttributes
1017 & USB_CONFIG_ATT_WAKEUP));
1018 else if (new_state != USB_STATE_SUSPENDED)
1019 device_init_wakeup(&udev->dev, 0);
1020 }
1014 } else 1021 } else
1015 recursively_mark_NOTATTACHED(udev); 1022 recursively_mark_NOTATTACHED(udev);
1016 spin_unlock_irqrestore(&device_state_lock, flags); 1023 spin_unlock_irqrestore(&device_state_lock, flags);
@@ -1172,8 +1179,11 @@ static int choose_configuration(struct usb_device *udev)
1172 c = udev->config; 1179 c = udev->config;
1173 num_configs = udev->descriptor.bNumConfigurations; 1180 num_configs = udev->descriptor.bNumConfigurations;
1174 for (i = 0; i < num_configs; (i++, c++)) { 1181 for (i = 0; i < num_configs; (i++, c++)) {
1175 struct usb_interface_descriptor *desc = 1182 struct usb_interface_descriptor *desc = NULL;
1176 &c->intf_cache[0]->altsetting->desc; 1183
1184 /* It's possible that a config has no interfaces! */
1185 if (c->desc.bNumInterfaces > 0)
1186 desc = &c->intf_cache[0]->altsetting->desc;
1177 1187
1178 /* 1188 /*
1179 * HP's USB bus-powered keyboard has only one configuration 1189 * HP's USB bus-powered keyboard has only one configuration
@@ -1208,7 +1218,8 @@ static int choose_configuration(struct usb_device *udev)
1208 /* If the first config's first interface is COMM/2/0xff 1218 /* If the first config's first interface is COMM/2/0xff
1209 * (MSFT RNDIS), rule it out unless Linux has host-side 1219 * (MSFT RNDIS), rule it out unless Linux has host-side
1210 * RNDIS support. */ 1220 * RNDIS support. */
1211 if (i == 0 && desc->bInterfaceClass == USB_CLASS_COMM 1221 if (i == 0 && desc
1222 && desc->bInterfaceClass == USB_CLASS_COMM
1212 && desc->bInterfaceSubClass == 2 1223 && desc->bInterfaceSubClass == 2
1213 && desc->bInterfaceProtocol == 0xff) { 1224 && desc->bInterfaceProtocol == 0xff) {
1214#ifndef CONFIG_USB_NET_RNDIS 1225#ifndef CONFIG_USB_NET_RNDIS
@@ -1224,8 +1235,8 @@ static int choose_configuration(struct usb_device *udev)
1224 * than a vendor-specific driver. */ 1235 * than a vendor-specific driver. */
1225 else if (udev->descriptor.bDeviceClass != 1236 else if (udev->descriptor.bDeviceClass !=
1226 USB_CLASS_VENDOR_SPEC && 1237 USB_CLASS_VENDOR_SPEC &&
1227 desc->bInterfaceClass != 1238 (!desc || desc->bInterfaceClass !=
1228 USB_CLASS_VENDOR_SPEC) { 1239 USB_CLASS_VENDOR_SPEC)) {
1229 best = c; 1240 best = c;
1230 break; 1241 break;
1231 } 1242 }
@@ -1876,18 +1887,18 @@ int usb_resume_device(struct usb_device *udev)
1876 if (udev->state == USB_STATE_NOTATTACHED) 1887 if (udev->state == USB_STATE_NOTATTACHED)
1877 return -ENODEV; 1888 return -ENODEV;
1878 1889
1879#ifdef CONFIG_USB_SUSPEND
1880 /* selective resume of one downstream hub-to-device port */ 1890 /* selective resume of one downstream hub-to-device port */
1881 if (udev->parent) { 1891 if (udev->parent) {
1892#ifdef CONFIG_USB_SUSPEND
1882 if (udev->state == USB_STATE_SUSPENDED) { 1893 if (udev->state == USB_STATE_SUSPENDED) {
1883 // NOTE swsusp may bork us, device state being wrong... 1894 // NOTE swsusp may bork us, device state being wrong...
1884 // NOTE this fails if parent is also suspended... 1895 // NOTE this fails if parent is also suspended...
1885 status = hub_port_resume(hdev_to_hub(udev->parent), 1896 status = hub_port_resume(hdev_to_hub(udev->parent),
1886 udev->portnum, udev); 1897 udev->portnum, udev);
1887 } else 1898 } else
1899#endif
1888 status = 0; 1900 status = 0;
1889 } else 1901 } else
1890#endif
1891 status = finish_device_resume(udev); 1902 status = finish_device_resume(udev);
1892 if (status < 0) 1903 if (status < 0)
1893 dev_dbg(&udev->dev, "can't resume, status %d\n", 1904 dev_dbg(&udev->dev, "can't resume, status %d\n",
@@ -2162,7 +2173,7 @@ static int
2162hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, 2173hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2163 int retry_counter) 2174 int retry_counter)
2164{ 2175{
2165 static DECLARE_MUTEX(usb_address0_sem); 2176 static DEFINE_MUTEX(usb_address0_mutex);
2166 2177
2167 struct usb_device *hdev = hub->hdev; 2178 struct usb_device *hdev = hub->hdev;
2168 int i, j, retval; 2179 int i, j, retval;
@@ -2183,7 +2194,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2183 if (oldspeed == USB_SPEED_LOW) 2194 if (oldspeed == USB_SPEED_LOW)
2184 delay = HUB_LONG_RESET_TIME; 2195 delay = HUB_LONG_RESET_TIME;
2185 2196
2186 down(&usb_address0_sem); 2197 mutex_lock(&usb_address0_mutex);
2187 2198
2188 /* Reset the device; full speed may morph to high speed */ 2199 /* Reset the device; full speed may morph to high speed */
2189 retval = hub_port_reset(hub, port1, udev, delay); 2200 retval = hub_port_reset(hub, port1, udev, delay);
@@ -2381,7 +2392,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2381fail: 2392fail:
2382 if (retval) 2393 if (retval)
2383 hub_port_disable(hub, port1, 0); 2394 hub_port_disable(hub, port1, 0);
2384 up(&usb_address0_sem); 2395 mutex_unlock(&usb_address0_mutex);
2385 return retval; 2396 return retval;
2386} 2397}
2387 2398
@@ -3017,7 +3028,7 @@ int usb_reset_device(struct usb_device *udev)
3017 parent_hub = hdev_to_hub(parent_hdev); 3028 parent_hub = hdev_to_hub(parent_hdev);
3018 3029
3019 /* If we're resetting an active hub, take some special actions */ 3030 /* If we're resetting an active hub, take some special actions */
3020 if (udev->actconfig && 3031 if (udev->actconfig && udev->actconfig->desc.bNumInterfaces > 0 &&
3021 udev->actconfig->interface[0]->dev.driver == 3032 udev->actconfig->interface[0]->dev.driver ==
3022 &hub_driver.driver && 3033 &hub_driver.driver &&
3023 (hub = hdev_to_hub(udev)) != NULL) { 3034 (hub = hdev_to_hub(udev)) != NULL) {
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 7135e542679d..08fb20f06f3e 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -631,8 +631,8 @@ int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char
631 * Returns the number of bytes received on success, or else the status code 631 * Returns the number of bytes received on success, or else the status code
632 * returned by the underlying usb_control_msg() call. 632 * returned by the underlying usb_control_msg() call.
633 */ 633 */
634int usb_get_string(struct usb_device *dev, unsigned short langid, 634static int usb_get_string(struct usb_device *dev, unsigned short langid,
635 unsigned char index, void *buf, int size) 635 unsigned char index, void *buf, int size)
636{ 636{
637 int i; 637 int i;
638 int result; 638 int result;
@@ -1388,11 +1388,13 @@ free_interfaces:
1388 if (dev->state != USB_STATE_ADDRESS) 1388 if (dev->state != USB_STATE_ADDRESS)
1389 usb_disable_device (dev, 1); // Skip ep0 1389 usb_disable_device (dev, 1); // Skip ep0
1390 1390
1391 i = dev->bus_mA - cp->desc.bMaxPower * 2; 1391 if (cp) {
1392 if (i < 0) 1392 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1393 dev_warn(&dev->dev, "new config #%d exceeds power " 1393 if (i < 0)
1394 "limit by %dmA\n", 1394 dev_warn(&dev->dev, "new config #%d exceeds power "
1395 configuration, -i); 1395 "limit by %dmA\n",
1396 configuration, -i);
1397 }
1396 1398
1397 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 1399 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1398 USB_REQ_SET_CONFIGURATION, 0, configuration, 0, 1400 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
@@ -1488,7 +1490,6 @@ EXPORT_SYMBOL(usb_sg_wait);
1488// synchronous control message convenience routines 1490// synchronous control message convenience routines
1489EXPORT_SYMBOL(usb_get_descriptor); 1491EXPORT_SYMBOL(usb_get_descriptor);
1490EXPORT_SYMBOL(usb_get_status); 1492EXPORT_SYMBOL(usb_get_status);
1491EXPORT_SYMBOL(usb_get_string);
1492EXPORT_SYMBOL(usb_string); 1493EXPORT_SYMBOL(usb_string);
1493 1494
1494// synchronous calls that also maintain usbcore state 1495// synchronous calls that also maintain usbcore state
diff --git a/drivers/usb/core/notify.c b/drivers/usb/core/notify.c
index fbbebab52fbd..fe0ed54fa0ae 100644
--- a/drivers/usb/core/notify.c
+++ b/drivers/usb/core/notify.c
@@ -13,59 +13,10 @@
13#include <linux/kernel.h> 13#include <linux/kernel.h>
14#include <linux/notifier.h> 14#include <linux/notifier.h>
15#include <linux/usb.h> 15#include <linux/usb.h>
16#include <linux/mutex.h>
16#include "usb.h" 17#include "usb.h"
17 18
18 19static BLOCKING_NOTIFIER_HEAD(usb_notifier_list);
19static struct notifier_block *usb_notifier_list;
20static DECLARE_MUTEX(usb_notifier_lock);
21
22static void usb_notifier_chain_register(struct notifier_block **list,
23 struct notifier_block *n)
24{
25 down(&usb_notifier_lock);
26 while (*list) {
27 if (n->priority > (*list)->priority)
28 break;
29 list = &((*list)->next);
30 }
31 n->next = *list;
32 *list = n;
33 up(&usb_notifier_lock);
34}
35
36static void usb_notifier_chain_unregister(struct notifier_block **nl,
37 struct notifier_block *n)
38{
39 down(&usb_notifier_lock);
40 while ((*nl)!=NULL) {
41 if ((*nl)==n) {
42 *nl = n->next;
43 goto exit;
44 }
45 nl=&((*nl)->next);
46 }
47exit:
48 up(&usb_notifier_lock);
49}
50
51static int usb_notifier_call_chain(struct notifier_block **n,
52 unsigned long val, void *v)
53{
54 int ret=NOTIFY_DONE;
55 struct notifier_block *nb = *n;
56
57 down(&usb_notifier_lock);
58 while (nb) {
59 ret = nb->notifier_call(nb,val,v);
60 if (ret&NOTIFY_STOP_MASK) {
61 goto exit;
62 }
63 nb = nb->next;
64 }
65exit:
66 up(&usb_notifier_lock);
67 return ret;
68}
69 20
70/** 21/**
71 * usb_register_notify - register a notifier callback whenever a usb change happens 22 * usb_register_notify - register a notifier callback whenever a usb change happens
@@ -75,7 +26,7 @@ exit:
75 */ 26 */
76void usb_register_notify(struct notifier_block *nb) 27void usb_register_notify(struct notifier_block *nb)
77{ 28{
78 usb_notifier_chain_register(&usb_notifier_list, nb); 29 blocking_notifier_chain_register(&usb_notifier_list, nb);
79} 30}
80EXPORT_SYMBOL_GPL(usb_register_notify); 31EXPORT_SYMBOL_GPL(usb_register_notify);
81 32
@@ -88,27 +39,28 @@ EXPORT_SYMBOL_GPL(usb_register_notify);
88 */ 39 */
89void usb_unregister_notify(struct notifier_block *nb) 40void usb_unregister_notify(struct notifier_block *nb)
90{ 41{
91 usb_notifier_chain_unregister(&usb_notifier_list, nb); 42 blocking_notifier_chain_unregister(&usb_notifier_list, nb);
92} 43}
93EXPORT_SYMBOL_GPL(usb_unregister_notify); 44EXPORT_SYMBOL_GPL(usb_unregister_notify);
94 45
95 46
96void usb_notify_add_device(struct usb_device *udev) 47void usb_notify_add_device(struct usb_device *udev)
97{ 48{
98 usb_notifier_call_chain(&usb_notifier_list, USB_DEVICE_ADD, udev); 49 blocking_notifier_call_chain(&usb_notifier_list, USB_DEVICE_ADD, udev);
99} 50}
100 51
101void usb_notify_remove_device(struct usb_device *udev) 52void usb_notify_remove_device(struct usb_device *udev)
102{ 53{
103 usb_notifier_call_chain(&usb_notifier_list, USB_DEVICE_REMOVE, udev); 54 blocking_notifier_call_chain(&usb_notifier_list,
55 USB_DEVICE_REMOVE, udev);
104} 56}
105 57
106void usb_notify_add_bus(struct usb_bus *ubus) 58void usb_notify_add_bus(struct usb_bus *ubus)
107{ 59{
108 usb_notifier_call_chain(&usb_notifier_list, USB_BUS_ADD, ubus); 60 blocking_notifier_call_chain(&usb_notifier_list, USB_BUS_ADD, ubus);
109} 61}
110 62
111void usb_notify_remove_bus(struct usb_bus *ubus) 63void usb_notify_remove_bus(struct usb_bus *ubus)
112{ 64{
113 usb_notifier_call_chain(&usb_notifier_list, USB_BUS_REMOVE, ubus); 65 blocking_notifier_call_chain(&usb_notifier_list, USB_BUS_REMOVE, ubus);
114} 66}
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 13d1d367f7f1..d7352aa73b5e 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -33,6 +33,7 @@
33#include <linux/errno.h> 33#include <linux/errno.h>
34#include <linux/smp_lock.h> 34#include <linux/smp_lock.h>
35#include <linux/usb.h> 35#include <linux/usb.h>
36#include <linux/mutex.h>
36 37
37#include <asm/io.h> 38#include <asm/io.h>
38#include <asm/scatterlist.h> 39#include <asm/scatterlist.h>
@@ -639,7 +640,7 @@ struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
639 struct usb_bus *bus; 640 struct usb_bus *bus;
640 struct usb_device *dev = NULL; 641 struct usb_device *dev = NULL;
641 642
642 down(&usb_bus_list_lock); 643 mutex_lock(&usb_bus_list_lock);
643 for (buslist = usb_bus_list.next; 644 for (buslist = usb_bus_list.next;
644 buslist != &usb_bus_list; 645 buslist != &usb_bus_list;
645 buslist = buslist->next) { 646 buslist = buslist->next) {
@@ -653,7 +654,7 @@ struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
653 goto exit; 654 goto exit;
654 } 655 }
655exit: 656exit:
656 up(&usb_bus_list_lock); 657 mutex_unlock(&usb_bus_list_lock);
657 return dev; 658 return dev;
658} 659}
659 660
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index ff075a53c8d6..d80f71877d68 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -187,6 +187,23 @@ config USB_OTG
187 187
188 Select this only if your OMAP board has a Mini-AB connector. 188 Select this only if your OMAP board has a Mini-AB connector.
189 189
190config USB_GADGET_AT91
191 boolean "AT91 USB Device Port"
192 depends on ARCH_AT91RM9200
193 select USB_GADGET_SELECTED
194 help
195 Many Atmel AT91 processors (such as the AT91RM2000) have a
196 full speed USB Device Port with support for five configurable
197 endpoints (plus endpoint zero).
198
199 Say "y" to link the driver statically, or "m" to build a
200 dynamically linked module called "at91_udc" and force all
201 gadget drivers to also be dynamically linked.
202
203config USB_AT91
204 tristate
205 depends on USB_GADGET_AT91
206 default USB_GADGET
190 207
191config USB_GADGET_DUMMY_HCD 208config USB_GADGET_DUMMY_HCD
192 boolean "Dummy HCD (DEVELOPMENT)" 209 boolean "Dummy HCD (DEVELOPMENT)"
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index d5fd04d886e6..5a28e61392ec 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_USB_PXA2XX) += pxa2xx_udc.o
7obj-$(CONFIG_USB_GOKU) += goku_udc.o 7obj-$(CONFIG_USB_GOKU) += goku_udc.o
8obj-$(CONFIG_USB_OMAP) += omap_udc.o 8obj-$(CONFIG_USB_OMAP) += omap_udc.o
9obj-$(CONFIG_USB_LH7A40X) += lh7a40x_udc.o 9obj-$(CONFIG_USB_LH7A40X) += lh7a40x_udc.o
10obj-$(CONFIG_USB_AT91) += at91_udc.o
10 11
11# 12#
12# USB gadget drivers 13# USB gadget drivers
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
new file mode 100644
index 000000000000..865858cfd1c2
--- /dev/null
+++ b/drivers/usb/gadget/at91_udc.c
@@ -0,0 +1,1773 @@
1/*
2 * at91_udc -- driver for at91-series USB peripheral controller
3 *
4 * Copyright (C) 2004 by Thomas Rathbone
5 * Copyright (C) 2005 by HP Labs
6 * Copyright (C) 2005 by David Brownell
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
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23
24#undef DEBUG
25#undef VERBOSE
26#undef PACKET_TRACE
27
28#include <linux/config.h>
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/platform_device.h>
32#include <linux/delay.h>
33#include <linux/ioport.h>
34#include <linux/sched.h>
35#include <linux/slab.h>
36#include <linux/smp_lock.h>
37#include <linux/errno.h>
38#include <linux/init.h>
39#include <linux/list.h>
40#include <linux/interrupt.h>
41#include <linux/proc_fs.h>
42#include <linux/clk.h>
43#include <linux/usb_ch9.h>
44#include <linux/usb_gadget.h>
45
46#include <asm/byteorder.h>
47#include <asm/io.h>
48#include <asm/irq.h>
49#include <asm/system.h>
50#include <asm/mach-types.h>
51
52#include <asm/arch/hardware.h>
53#include <asm/arch/gpio.h>
54#include <asm/arch/board.h>
55
56#include "at91_udc.h"
57
58
59/*
60 * This controller is simple and PIO-only. It's used in many AT91-series
61 * ARMv4T controllers, including the at91rm9200 (arm920T, with MMU),
62 * at91sam9261 (arm926ejs, with MMU), and several no-mmu versions.
63 *
64 * This driver expects the board has been wired with two GPIOs suppporting
65 * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
66 * testing hasn't covered such cases.) The pullup is most important; it
67 * provides software control over whether the host enumerates the device.
68 * The VBUS sensing helps during enumeration, and allows both USB clocks
69 * (and the transceiver) to stay gated off until they're necessary, saving
70 * power. During USB suspend, the 48 MHz clock is gated off.
71 */
72
73#define DRIVER_VERSION "8 March 2005"
74
75static const char driver_name [] = "at91_udc";
76static const char ep0name[] = "ep0";
77
78/*-------------------------------------------------------------------------*/
79
80/*
81 * Read from a UDP register.
82 */
83static inline unsigned long at91_udp_read(unsigned int reg)
84{
85 void __iomem *udp_base = (void __iomem *)AT91_VA_BASE_UDP;
86
87 return __raw_readl(udp_base + reg);
88}
89
90/*
91 * Write to a UDP register.
92 */
93static inline void at91_udp_write(unsigned int reg, unsigned long value)
94{
95 void __iomem *udp_base = (void __iomem *)AT91_VA_BASE_UDP;
96
97 __raw_writel(value, udp_base + reg);
98}
99
100/*-------------------------------------------------------------------------*/
101
102#ifdef CONFIG_USB_GADGET_DEBUG_FILES
103
104#include <linux/seq_file.h>
105
106static const char debug_filename[] = "driver/udc";
107
108#define FOURBITS "%s%s%s%s"
109#define EIGHTBITS FOURBITS FOURBITS
110
111static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
112{
113 static char *types[] = {
114 "control", "out-iso", "out-bulk", "out-int",
115 "BOGUS", "in-iso", "in-bulk", "in-int"};
116
117 u32 csr;
118 struct at91_request *req;
119 unsigned long flags;
120
121 local_irq_save(flags);
122
123 csr = __raw_readl(ep->creg);
124
125 /* NOTE: not collecting per-endpoint irq statistics... */
126
127 seq_printf(s, "\n");
128 seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
129 ep->ep.name, ep->ep.maxpacket,
130 ep->is_in ? "in" : "out",
131 ep->is_iso ? " iso" : "",
132 ep->is_pingpong
133 ? (ep->fifo_bank ? "pong" : "ping")
134 : "",
135 ep->stopped ? " stopped" : "");
136 seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
137 csr,
138 (csr & 0x07ff0000) >> 16,
139 (csr & (1 << 15)) ? "enabled" : "disabled",
140 (csr & (1 << 11)) ? "DATA1" : "DATA0",
141 types[(csr & 0x700) >> 8],
142
143 /* iff type is control then print current direction */
144 (!(csr & 0x700))
145 ? ((csr & (1 << 7)) ? " IN" : " OUT")
146 : "",
147 (csr & (1 << 6)) ? " rxdatabk1" : "",
148 (csr & (1 << 5)) ? " forcestall" : "",
149 (csr & (1 << 4)) ? " txpktrdy" : "",
150
151 (csr & (1 << 3)) ? " stallsent" : "",
152 (csr & (1 << 2)) ? " rxsetup" : "",
153 (csr & (1 << 1)) ? " rxdatabk0" : "",
154 (csr & (1 << 0)) ? " txcomp" : "");
155 if (list_empty (&ep->queue))
156 seq_printf(s, "\t(queue empty)\n");
157
158 else list_for_each_entry (req, &ep->queue, queue) {
159 unsigned length = req->req.actual;
160
161 seq_printf(s, "\treq %p len %d/%d buf %p\n",
162 &req->req, length,
163 req->req.length, req->req.buf);
164 }
165 local_irq_restore(flags);
166}
167
168static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
169{
170 int i;
171
172 seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
173 (mask & (1 << 13)) ? " wakeup" : "",
174 (mask & (1 << 12)) ? " endbusres" : "",
175
176 (mask & (1 << 11)) ? " sofint" : "",
177 (mask & (1 << 10)) ? " extrsm" : "",
178 (mask & (1 << 9)) ? " rxrsm" : "",
179 (mask & (1 << 8)) ? " rxsusp" : "");
180 for (i = 0; i < 8; i++) {
181 if (mask & (1 << i))
182 seq_printf(s, " ep%d", i);
183 }
184 seq_printf(s, "\n");
185}
186
187static int proc_udc_show(struct seq_file *s, void *unused)
188{
189 struct at91_udc *udc = s->private;
190 struct at91_ep *ep;
191 u32 tmp;
192
193 seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
194
195 seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
196 udc->vbus ? "present" : "off",
197 udc->enabled
198 ? (udc->vbus ? "active" : "enabled")
199 : "disabled",
200 udc->selfpowered ? "self" : "VBUS",
201 udc->suspended ? ", suspended" : "",
202 udc->driver ? udc->driver->driver.name : "(none)");
203
204 /* don't access registers when interface isn't clocked */
205 if (!udc->clocked) {
206 seq_printf(s, "(not clocked)\n");
207 return 0;
208 }
209
210 tmp = at91_udp_read(AT91_UDP_FRM_NUM);
211 seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
212 (tmp & AT91_UDP_FRM_OK) ? " ok" : "",
213 (tmp & AT91_UDP_FRM_ERR) ? " err" : "",
214 (tmp & AT91_UDP_NUM));
215
216 tmp = at91_udp_read(AT91_UDP_GLB_STAT);
217 seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
218 (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
219 (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
220 (tmp & AT91_UDP_ESR) ? " esr" : "",
221 (tmp & AT91_UDP_CONFG) ? " confg" : "",
222 (tmp & AT91_UDP_FADDEN) ? " fadden" : "");
223
224 tmp = at91_udp_read(AT91_UDP_FADDR);
225 seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp,
226 (tmp & AT91_UDP_FEN) ? " fen" : "",
227 (tmp & AT91_UDP_FADD));
228
229 proc_irq_show(s, "imr ", at91_udp_read(AT91_UDP_IMR));
230 proc_irq_show(s, "isr ", at91_udp_read(AT91_UDP_ISR));
231
232 if (udc->enabled && udc->vbus) {
233 proc_ep_show(s, &udc->ep[0]);
234 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
235 if (ep->desc)
236 proc_ep_show(s, ep);
237 }
238 }
239 return 0;
240}
241
242static int proc_udc_open(struct inode *inode, struct file *file)
243{
244 return single_open(file, proc_udc_show, PDE(inode)->data);
245}
246
247static struct file_operations proc_ops = {
248 .open = proc_udc_open,
249 .read = seq_read,
250 .llseek = seq_lseek,
251 .release = single_release,
252};
253
254static void create_debug_file(struct at91_udc *udc)
255{
256 struct proc_dir_entry *pde;
257
258 pde = create_proc_entry (debug_filename, 0, NULL);
259 udc->pde = pde;
260 if (pde == NULL)
261 return;
262
263 pde->proc_fops = &proc_ops;
264 pde->data = udc;
265}
266
267static void remove_debug_file(struct at91_udc *udc)
268{
269 if (udc->pde)
270 remove_proc_entry(debug_filename, NULL);
271}
272
273#else
274
275static inline void create_debug_file(struct at91_udc *udc) {}
276static inline void remove_debug_file(struct at91_udc *udc) {}
277
278#endif
279
280
281/*-------------------------------------------------------------------------*/
282
283static void done(struct at91_ep *ep, struct at91_request *req, int status)
284{
285 unsigned stopped = ep->stopped;
286
287 list_del_init(&req->queue);
288 if (req->req.status == -EINPROGRESS)
289 req->req.status = status;
290 else
291 status = req->req.status;
292 if (status && status != -ESHUTDOWN)
293 VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
294
295 ep->stopped = 1;
296 req->req.complete(&ep->ep, &req->req);
297 ep->stopped = stopped;
298
299 /* ep0 is always ready; other endpoints need a non-empty queue */
300 if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
301 at91_udp_write(AT91_UDP_IDR, ep->int_mask);
302}
303
304/*-------------------------------------------------------------------------*/
305
306/* bits indicating OUT fifo has data ready */
307#define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
308
309/*
310 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
311 * back most of the value you just read (because of side effects, including
312 * bits that may change after reading and before writing).
313 *
314 * Except when changing a specific bit, always write values which:
315 * - clear SET_FX bits (setting them could change something)
316 * - set CLR_FX bits (clearing them could change something)
317 *
318 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
319 * that shouldn't normally be changed.
320 */
321#define SET_FX (AT91_UDP_TXPKTRDY)
322#define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
323
324/* pull OUT packet data from the endpoint's fifo */
325static int read_fifo (struct at91_ep *ep, struct at91_request *req)
326{
327 u32 __iomem *creg = ep->creg;
328 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
329 u32 csr;
330 u8 *buf;
331 unsigned int count, bufferspace, is_done;
332
333 buf = req->req.buf + req->req.actual;
334 bufferspace = req->req.length - req->req.actual;
335
336 /*
337 * there might be nothing to read if ep_queue() calls us,
338 * or if we already emptied both pingpong buffers
339 */
340rescan:
341 csr = __raw_readl(creg);
342 if ((csr & RX_DATA_READY) == 0)
343 return 0;
344
345 count = (csr & AT91_UDP_RXBYTECNT) >> 16;
346 if (count > ep->ep.maxpacket)
347 count = ep->ep.maxpacket;
348 if (count > bufferspace) {
349 DBG("%s buffer overflow\n", ep->ep.name);
350 req->req.status = -EOVERFLOW;
351 count = bufferspace;
352 }
353 __raw_readsb(dreg, buf, count);
354
355 /* release and swap pingpong mem bank */
356 csr |= CLR_FX;
357 if (ep->is_pingpong) {
358 if (ep->fifo_bank == 0) {
359 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
360 ep->fifo_bank = 1;
361 } else {
362 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
363 ep->fifo_bank = 0;
364 }
365 } else
366 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
367 __raw_writel(csr, creg);
368
369 req->req.actual += count;
370 is_done = (count < ep->ep.maxpacket);
371 if (count == bufferspace)
372 is_done = 1;
373
374 PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
375 is_done ? " (done)" : "");
376
377 /*
378 * avoid extra trips through IRQ logic for packets already in
379 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
380 */
381 if (is_done)
382 done(ep, req, 0);
383 else if (ep->is_pingpong) {
384 bufferspace -= count;
385 buf += count;
386 goto rescan;
387 }
388
389 return is_done;
390}
391
392/* load fifo for an IN packet */
393static int write_fifo(struct at91_ep *ep, struct at91_request *req)
394{
395 u32 __iomem *creg = ep->creg;
396 u32 csr = __raw_readl(creg);
397 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
398 unsigned total, count, is_last;
399
400 /*
401 * TODO: allow for writing two packets to the fifo ... that'll
402 * reduce the amount of IN-NAKing, but probably won't affect
403 * throughput much. (Unlike preventing OUT-NAKing!)
404 */
405
406 /*
407 * If ep_queue() calls us, the queue is empty and possibly in
408 * odd states like TXCOMP not yet cleared (we do it, saving at
409 * least one IRQ) or the fifo not yet being free. Those aren't
410 * issues normally (IRQ handler fast path).
411 */
412 if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
413 if (csr & AT91_UDP_TXCOMP) {
414 csr |= CLR_FX;
415 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
416 __raw_writel(csr, creg);
417 csr = __raw_readl(creg);
418 }
419 if (csr & AT91_UDP_TXPKTRDY)
420 return 0;
421 }
422
423 total = req->req.length - req->req.actual;
424 if (ep->ep.maxpacket < total) {
425 count = ep->ep.maxpacket;
426 is_last = 0;
427 } else {
428 count = total;
429 is_last = (count < ep->ep.maxpacket) || !req->req.zero;
430 }
431
432 /*
433 * Write the packet, maybe it's a ZLP.
434 *
435 * NOTE: incrementing req->actual before we receive the ACK means
436 * gadget driver IN bytecounts can be wrong in fault cases. That's
437 * fixable with PIO drivers like this one (save "count" here, and
438 * do the increment later on TX irq), but not for most DMA hardware.
439 *
440 * So all gadget drivers must accept that potential error. Some
441 * hardware supports precise fifo status reporting, letting them
442 * recover when the actual bytecount matters (e.g. for USB Test
443 * and Measurement Class devices).
444 */
445 __raw_writesb(dreg, req->req.buf + req->req.actual, count);
446 csr &= ~SET_FX;
447 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
448 __raw_writel(csr, creg);
449 req->req.actual += count;
450
451 PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
452 is_last ? " (done)" : "");
453 if (is_last)
454 done(ep, req, 0);
455 return is_last;
456}
457
458static void nuke(struct at91_ep *ep, int status)
459{
460 struct at91_request *req;
461
462 // terminer chaque requete dans la queue
463 ep->stopped = 1;
464 if (list_empty(&ep->queue))
465 return;
466
467 VDBG("%s %s\n", __FUNCTION__, ep->ep.name);
468 while (!list_empty(&ep->queue)) {
469 req = list_entry(ep->queue.next, struct at91_request, queue);
470 done(ep, req, status);
471 }
472}
473
474/*-------------------------------------------------------------------------*/
475
476static int at91_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
477{
478 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
479 struct at91_udc *dev = ep->udc;
480 u16 maxpacket;
481 u32 tmp;
482 unsigned long flags;
483
484 if (!_ep || !ep
485 || !desc || ep->desc
486 || _ep->name == ep0name
487 || desc->bDescriptorType != USB_DT_ENDPOINT
488 || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0
489 || maxpacket > ep->maxpacket) {
490 DBG("bad ep or descriptor\n");
491 return -EINVAL;
492 }
493
494 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
495 DBG("bogus device state\n");
496 return -ESHUTDOWN;
497 }
498
499 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
500 switch (tmp) {
501 case USB_ENDPOINT_XFER_CONTROL:
502 DBG("only one control endpoint\n");
503 return -EINVAL;
504 case USB_ENDPOINT_XFER_INT:
505 if (maxpacket > 64)
506 goto bogus_max;
507 break;
508 case USB_ENDPOINT_XFER_BULK:
509 switch (maxpacket) {
510 case 8:
511 case 16:
512 case 32:
513 case 64:
514 goto ok;
515 }
516bogus_max:
517 DBG("bogus maxpacket %d\n", maxpacket);
518 return -EINVAL;
519 case USB_ENDPOINT_XFER_ISOC:
520 if (!ep->is_pingpong) {
521 DBG("iso requires double buffering\n");
522 return -EINVAL;
523 }
524 break;
525 }
526
527ok:
528 local_irq_save(flags);
529
530 /* initialize endpoint to match this descriptor */
531 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
532 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
533 ep->stopped = 0;
534 if (ep->is_in)
535 tmp |= 0x04;
536 tmp <<= 8;
537 tmp |= AT91_UDP_EPEDS;
538 __raw_writel(tmp, ep->creg);
539
540 ep->desc = desc;
541 ep->ep.maxpacket = maxpacket;
542
543 /*
544 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
545 * since endpoint resets don't reset hw pingpong state.
546 */
547 at91_udp_write(AT91_UDP_RST_EP, ep->int_mask);
548 at91_udp_write(AT91_UDP_RST_EP, 0);
549
550 local_irq_restore(flags);
551 return 0;
552}
553
554static int at91_ep_disable (struct usb_ep * _ep)
555{
556 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
557 unsigned long flags;
558
559 if (ep == &ep->udc->ep[0])
560 return -EINVAL;
561
562 local_irq_save(flags);
563
564 nuke(ep, -ESHUTDOWN);
565
566 /* restore the endpoint's pristine config */
567 ep->desc = NULL;
568 ep->ep.maxpacket = ep->maxpacket;
569
570 /* reset fifos and endpoint */
571 if (ep->udc->clocked) {
572 at91_udp_write(AT91_UDP_RST_EP, ep->int_mask);
573 at91_udp_write(AT91_UDP_RST_EP, 0);
574 __raw_writel(0, ep->creg);
575 }
576
577 local_irq_restore(flags);
578 return 0;
579}
580
581/*
582 * this is a PIO-only driver, so there's nothing
583 * interesting for request or buffer allocation.
584 */
585
586static struct usb_request *at91_ep_alloc_request (struct usb_ep *_ep, unsigned int gfp_flags)
587{
588 struct at91_request *req;
589
590 req = kcalloc(1, sizeof (struct at91_request), SLAB_KERNEL);
591 if (!req)
592 return NULL;
593
594 INIT_LIST_HEAD(&req->queue);
595 return &req->req;
596}
597
598static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
599{
600 struct at91_request *req;
601
602 req = container_of(_req, struct at91_request, req);
603 BUG_ON(!list_empty(&req->queue));
604 kfree(req);
605}
606
607static void *at91_ep_alloc_buffer(
608 struct usb_ep *_ep,
609 unsigned bytes,
610 dma_addr_t *dma,
611 gfp_t gfp_flags)
612{
613 *dma = ~0;
614 return kmalloc(bytes, gfp_flags);
615}
616
617static void at91_ep_free_buffer(
618 struct usb_ep *ep,
619 void *buf,
620 dma_addr_t dma,
621 unsigned bytes)
622{
623 kfree(buf);
624}
625
626static int at91_ep_queue(struct usb_ep *_ep,
627 struct usb_request *_req, gfp_t gfp_flags)
628{
629 struct at91_request *req;
630 struct at91_ep *ep;
631 struct at91_udc *dev;
632 int status;
633 unsigned long flags;
634
635 req = container_of(_req, struct at91_request, req);
636 ep = container_of(_ep, struct at91_ep, ep);
637
638 if (!_req || !_req->complete
639 || !_req->buf || !list_empty(&req->queue)) {
640 DBG("invalid request\n");
641 return -EINVAL;
642 }
643
644 if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
645 DBG("invalid ep\n");
646 return -EINVAL;
647 }
648
649 dev = ep->udc;
650
651 if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
652 DBG("invalid device\n");
653 return -EINVAL;
654 }
655
656 _req->status = -EINPROGRESS;
657 _req->actual = 0;
658
659 local_irq_save(flags);
660
661 /* try to kickstart any empty and idle queue */
662 if (list_empty(&ep->queue) && !ep->stopped) {
663 int is_ep0;
664
665 /*
666 * If this control request has a non-empty DATA stage, this
667 * will start that stage. It works just like a non-control
668 * request (until the status stage starts, maybe early).
669 *
670 * If the data stage is empty, then this starts a successful
671 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
672 */
673 is_ep0 = (ep->ep.name == ep0name);
674 if (is_ep0) {
675 u32 tmp;
676
677 if (!dev->req_pending) {
678 status = -EINVAL;
679 goto done;
680 }
681
682 /*
683 * defer changing CONFG until after the gadget driver
684 * reconfigures the endpoints.
685 */
686 if (dev->wait_for_config_ack) {
687 tmp = at91_udp_read(AT91_UDP_GLB_STAT);
688 tmp ^= AT91_UDP_CONFG;
689 VDBG("toggle config\n");
690 at91_udp_write(AT91_UDP_GLB_STAT, tmp);
691 }
692 if (req->req.length == 0) {
693ep0_in_status:
694 PACKET("ep0 in/status\n");
695 status = 0;
696 tmp = __raw_readl(ep->creg);
697 tmp &= ~SET_FX;
698 tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
699 __raw_writel(tmp, ep->creg);
700 dev->req_pending = 0;
701 goto done;
702 }
703 }
704
705 if (ep->is_in)
706 status = write_fifo(ep, req);
707 else {
708 status = read_fifo(ep, req);
709
710 /* IN/STATUS stage is otherwise triggered by irq */
711 if (status && is_ep0)
712 goto ep0_in_status;
713 }
714 } else
715 status = 0;
716
717 if (req && !status) {
718 list_add_tail (&req->queue, &ep->queue);
719 at91_udp_write(AT91_UDP_IER, ep->int_mask);
720 }
721done:
722 local_irq_restore(flags);
723 return (status < 0) ? status : 0;
724}
725
726static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
727{
728 struct at91_ep *ep;
729 struct at91_request *req;
730
731 ep = container_of(_ep, struct at91_ep, ep);
732 if (!_ep || ep->ep.name == ep0name)
733 return -EINVAL;
734
735 /* make sure it's actually queued on this endpoint */
736 list_for_each_entry (req, &ep->queue, queue) {
737 if (&req->req == _req)
738 break;
739 }
740 if (&req->req != _req)
741 return -EINVAL;
742
743 done(ep, req, -ECONNRESET);
744 return 0;
745}
746
747static int at91_ep_set_halt(struct usb_ep *_ep, int value)
748{
749 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
750 u32 __iomem *creg;
751 u32 csr;
752 unsigned long flags;
753 int status = 0;
754
755 if (!_ep || ep->is_iso || !ep->udc->clocked)
756 return -EINVAL;
757
758 creg = ep->creg;
759 local_irq_save(flags);
760
761 csr = __raw_readl(creg);
762
763 /*
764 * fail with still-busy IN endpoints, ensuring correct sequencing
765 * of data tx then stall. note that the fifo rx bytecount isn't
766 * completely accurate as a tx bytecount.
767 */
768 if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
769 status = -EAGAIN;
770 else {
771 csr |= CLR_FX;
772 csr &= ~SET_FX;
773 if (value) {
774 csr |= AT91_UDP_FORCESTALL;
775 VDBG("halt %s\n", ep->ep.name);
776 } else {
777 at91_udp_write(AT91_UDP_RST_EP, ep->int_mask);
778 at91_udp_write(AT91_UDP_RST_EP, 0);
779 csr &= ~AT91_UDP_FORCESTALL;
780 }
781 __raw_writel(csr, creg);
782 }
783
784 local_irq_restore(flags);
785 return status;
786}
787
788static struct usb_ep_ops at91_ep_ops = {
789 .enable = at91_ep_enable,
790 .disable = at91_ep_disable,
791 .alloc_request = at91_ep_alloc_request,
792 .free_request = at91_ep_free_request,
793 .alloc_buffer = at91_ep_alloc_buffer,
794 .free_buffer = at91_ep_free_buffer,
795 .queue = at91_ep_queue,
796 .dequeue = at91_ep_dequeue,
797 .set_halt = at91_ep_set_halt,
798 // there's only imprecise fifo status reporting
799};
800
801/*-------------------------------------------------------------------------*/
802
803static int at91_get_frame(struct usb_gadget *gadget)
804{
805 if (!to_udc(gadget)->clocked)
806 return -EINVAL;
807 return at91_udp_read(AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
808}
809
810static int at91_wakeup(struct usb_gadget *gadget)
811{
812 struct at91_udc *udc = to_udc(gadget);
813 u32 glbstate;
814 int status = -EINVAL;
815 unsigned long flags;
816
817 DBG("%s\n", __FUNCTION__ );
818 local_irq_save(flags);
819
820 if (!udc->clocked || !udc->suspended)
821 goto done;
822
823 /* NOTE: some "early versions" handle ESR differently ... */
824
825 glbstate = at91_udp_read(AT91_UDP_GLB_STAT);
826 if (!(glbstate & AT91_UDP_ESR))
827 goto done;
828 glbstate |= AT91_UDP_ESR;
829 at91_udp_write(AT91_UDP_GLB_STAT, glbstate);
830
831done:
832 local_irq_restore(flags);
833 return status;
834}
835
836/* reinit == restore inital software state */
837static void udc_reinit(struct at91_udc *udc)
838{
839 u32 i;
840
841 INIT_LIST_HEAD(&udc->gadget.ep_list);
842 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
843
844 for (i = 0; i < NUM_ENDPOINTS; i++) {
845 struct at91_ep *ep = &udc->ep[i];
846
847 if (i != 0)
848 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
849 ep->desc = NULL;
850 ep->stopped = 0;
851 ep->fifo_bank = 0;
852 ep->ep.maxpacket = ep->maxpacket;
853 // initialiser une queue par endpoint
854 INIT_LIST_HEAD(&ep->queue);
855 }
856}
857
858static void stop_activity(struct at91_udc *udc)
859{
860 struct usb_gadget_driver *driver = udc->driver;
861 int i;
862
863 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
864 driver = NULL;
865 udc->gadget.speed = USB_SPEED_UNKNOWN;
866
867 for (i = 0; i < NUM_ENDPOINTS; i++) {
868 struct at91_ep *ep = &udc->ep[i];
869 ep->stopped = 1;
870 nuke(ep, -ESHUTDOWN);
871 }
872 if (driver)
873 driver->disconnect(&udc->gadget);
874
875 udc_reinit(udc);
876}
877
878static void clk_on(struct at91_udc *udc)
879{
880 if (udc->clocked)
881 return;
882 udc->clocked = 1;
883 clk_enable(udc->iclk);
884 clk_enable(udc->fclk);
885}
886
887static void clk_off(struct at91_udc *udc)
888{
889 if (!udc->clocked)
890 return;
891 udc->clocked = 0;
892 udc->gadget.speed = USB_SPEED_UNKNOWN;
893 clk_disable(udc->iclk);
894 clk_disable(udc->fclk);
895}
896
897/*
898 * activate/deactivate link with host; minimize power usage for
899 * inactive links by cutting clocks and transceiver power.
900 */
901static void pullup(struct at91_udc *udc, int is_on)
902{
903 if (!udc->enabled || !udc->vbus)
904 is_on = 0;
905 DBG("%sactive\n", is_on ? "" : "in");
906 if (is_on) {
907 clk_on(udc);
908 at91_udp_write(AT91_UDP_TXVC, 0);
909 at91_set_gpio_value(udc->board.pullup_pin, 1);
910 } else {
911 stop_activity(udc);
912 at91_udp_write(AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
913 at91_set_gpio_value(udc->board.pullup_pin, 0);
914 clk_off(udc);
915
916 // REVISIT: with transceiver disabled, will D- float
917 // so that a host would falsely detect a device?
918 }
919}
920
921/* vbus is here! turn everything on that's ready */
922static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
923{
924 struct at91_udc *udc = to_udc(gadget);
925 unsigned long flags;
926
927 // VDBG("vbus %s\n", is_active ? "on" : "off");
928 local_irq_save(flags);
929 udc->vbus = (is_active != 0);
930 pullup(udc, is_active);
931 local_irq_restore(flags);
932 return 0;
933}
934
935static int at91_pullup(struct usb_gadget *gadget, int is_on)
936{
937 struct at91_udc *udc = to_udc(gadget);
938 unsigned long flags;
939
940 local_irq_save(flags);
941 udc->enabled = is_on = !!is_on;
942 pullup(udc, is_on);
943 local_irq_restore(flags);
944 return 0;
945}
946
947static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
948{
949 struct at91_udc *udc = to_udc(gadget);
950 unsigned long flags;
951
952 local_irq_save(flags);
953 udc->selfpowered = (is_on != 0);
954 local_irq_restore(flags);
955 return 0;
956}
957
958static const struct usb_gadget_ops at91_udc_ops = {
959 .get_frame = at91_get_frame,
960 .wakeup = at91_wakeup,
961 .set_selfpowered = at91_set_selfpowered,
962 .vbus_session = at91_vbus_session,
963 .pullup = at91_pullup,
964
965 /*
966 * VBUS-powered devices may also also want to support bigger
967 * power budgets after an appropriate SET_CONFIGURATION.
968 */
969 // .vbus_power = at91_vbus_power,
970};
971
972/*-------------------------------------------------------------------------*/
973
974static int handle_ep(struct at91_ep *ep)
975{
976 struct at91_request *req;
977 u32 __iomem *creg = ep->creg;
978 u32 csr = __raw_readl(creg);
979
980 if (!list_empty(&ep->queue))
981 req = list_entry(ep->queue.next,
982 struct at91_request, queue);
983 else
984 req = NULL;
985
986 if (ep->is_in) {
987 if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
988 csr |= CLR_FX;
989 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
990 __raw_writel(csr, creg);
991 }
992 if (req)
993 return write_fifo(ep, req);
994
995 } else {
996 if (csr & AT91_UDP_STALLSENT) {
997 /* STALLSENT bit == ISOERR */
998 if (ep->is_iso && req)
999 req->req.status = -EILSEQ;
1000 csr |= CLR_FX;
1001 csr &= ~(SET_FX | AT91_UDP_STALLSENT);
1002 __raw_writel(csr, creg);
1003 csr = __raw_readl(creg);
1004 }
1005 if (req && (csr & RX_DATA_READY))
1006 return read_fifo(ep, req);
1007 }
1008 return 0;
1009}
1010
1011union setup {
1012 u8 raw[8];
1013 struct usb_ctrlrequest r;
1014};
1015
1016static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
1017{
1018 u32 __iomem *creg = ep->creg;
1019 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1020 unsigned rxcount, i = 0;
1021 u32 tmp;
1022 union setup pkt;
1023 int status = 0;
1024
1025 /* read and ack SETUP; hard-fail for bogus packets */
1026 rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
1027 if (likely(rxcount == 8)) {
1028 while (rxcount--)
1029 pkt.raw[i++] = __raw_readb(dreg);
1030 if (pkt.r.bRequestType & USB_DIR_IN) {
1031 csr |= AT91_UDP_DIR;
1032 ep->is_in = 1;
1033 } else {
1034 csr &= ~AT91_UDP_DIR;
1035 ep->is_in = 0;
1036 }
1037 } else {
1038 // REVISIT this happens sometimes under load; why??
1039 ERR("SETUP len %d, csr %08x\n", rxcount, csr);
1040 status = -EINVAL;
1041 }
1042 csr |= CLR_FX;
1043 csr &= ~(SET_FX | AT91_UDP_RXSETUP);
1044 __raw_writel(csr, creg);
1045 udc->wait_for_addr_ack = 0;
1046 udc->wait_for_config_ack = 0;
1047 ep->stopped = 0;
1048 if (unlikely(status != 0))
1049 goto stall;
1050
1051#define w_index le16_to_cpu(pkt.r.wIndex)
1052#define w_value le16_to_cpu(pkt.r.wValue)
1053#define w_length le16_to_cpu(pkt.r.wLength)
1054
1055 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1056 pkt.r.bRequestType, pkt.r.bRequest,
1057 w_value, w_index, w_length);
1058
1059 /*
1060 * A few standard requests get handled here, ones that touch
1061 * hardware ... notably for device and endpoint features.
1062 */
1063 udc->req_pending = 1;
1064 csr = __raw_readl(creg);
1065 csr |= CLR_FX;
1066 csr &= ~SET_FX;
1067 switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
1068
1069 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1070 | USB_REQ_SET_ADDRESS:
1071 __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
1072 udc->addr = w_value;
1073 udc->wait_for_addr_ack = 1;
1074 udc->req_pending = 0;
1075 /* FADDR is set later, when we ack host STATUS */
1076 return;
1077
1078 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1079 | USB_REQ_SET_CONFIGURATION:
1080 tmp = at91_udp_read(AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
1081 if (pkt.r.wValue)
1082 udc->wait_for_config_ack = (tmp == 0);
1083 else
1084 udc->wait_for_config_ack = (tmp != 0);
1085 if (udc->wait_for_config_ack)
1086 VDBG("wait for config\n");
1087 /* CONFG is toggled later, if gadget driver succeeds */
1088 break;
1089
1090 /*
1091 * Hosts may set or clear remote wakeup status, and
1092 * devices may report they're VBUS powered.
1093 */
1094 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1095 | USB_REQ_GET_STATUS:
1096 tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
1097 if (at91_udp_read(AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
1098 tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1099 PACKET("get device status\n");
1100 __raw_writeb(tmp, dreg);
1101 __raw_writeb(0, dreg);
1102 goto write_in;
1103 /* then STATUS starts later, automatically */
1104 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1105 | USB_REQ_SET_FEATURE:
1106 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1107 goto stall;
1108 tmp = at91_udp_read(AT91_UDP_GLB_STAT);
1109 tmp |= AT91_UDP_ESR;
1110 at91_udp_write(AT91_UDP_GLB_STAT, tmp);
1111 goto succeed;
1112 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1113 | USB_REQ_CLEAR_FEATURE:
1114 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1115 goto stall;
1116 tmp = at91_udp_read(AT91_UDP_GLB_STAT);
1117 tmp &= ~AT91_UDP_ESR;
1118 at91_udp_write(AT91_UDP_GLB_STAT, tmp);
1119 goto succeed;
1120
1121 /*
1122 * Interfaces have no feature settings; this is pretty useless.
1123 * we won't even insist the interface exists...
1124 */
1125 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1126 | USB_REQ_GET_STATUS:
1127 PACKET("get interface status\n");
1128 __raw_writeb(0, dreg);
1129 __raw_writeb(0, dreg);
1130 goto write_in;
1131 /* then STATUS starts later, automatically */
1132 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1133 | USB_REQ_SET_FEATURE:
1134 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1135 | USB_REQ_CLEAR_FEATURE:
1136 goto stall;
1137
1138 /*
1139 * Hosts may clear bulk/intr endpoint halt after the gadget
1140 * driver sets it (not widely used); or set it (for testing)
1141 */
1142 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1143 | USB_REQ_GET_STATUS:
1144 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1145 ep = &udc->ep[tmp];
1146 if (tmp > NUM_ENDPOINTS || (tmp && !ep->desc))
1147 goto stall;
1148
1149 if (tmp) {
1150 if ((w_index & USB_DIR_IN)) {
1151 if (!ep->is_in)
1152 goto stall;
1153 } else if (ep->is_in)
1154 goto stall;
1155 }
1156 PACKET("get %s status\n", ep->ep.name);
1157 if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
1158 tmp = (1 << USB_ENDPOINT_HALT);
1159 else
1160 tmp = 0;
1161 __raw_writeb(tmp, dreg);
1162 __raw_writeb(0, dreg);
1163 goto write_in;
1164 /* then STATUS starts later, automatically */
1165 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1166 | USB_REQ_SET_FEATURE:
1167 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1168 ep = &udc->ep[tmp];
1169 if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS)
1170 goto stall;
1171 if (!ep->desc || ep->is_iso)
1172 goto stall;
1173 if ((w_index & USB_DIR_IN)) {
1174 if (!ep->is_in)
1175 goto stall;
1176 } else if (ep->is_in)
1177 goto stall;
1178
1179 tmp = __raw_readl(ep->creg);
1180 tmp &= ~SET_FX;
1181 tmp |= CLR_FX | AT91_UDP_FORCESTALL;
1182 __raw_writel(tmp, ep->creg);
1183 goto succeed;
1184 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1185 | USB_REQ_CLEAR_FEATURE:
1186 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1187 ep = &udc->ep[tmp];
1188 if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS)
1189 goto stall;
1190 if (tmp == 0)
1191 goto succeed;
1192 if (!ep->desc || ep->is_iso)
1193 goto stall;
1194 if ((w_index & USB_DIR_IN)) {
1195 if (!ep->is_in)
1196 goto stall;
1197 } else if (ep->is_in)
1198 goto stall;
1199
1200 at91_udp_write(AT91_UDP_RST_EP, ep->int_mask);
1201 at91_udp_write(AT91_UDP_RST_EP, 0);
1202 tmp = __raw_readl(ep->creg);
1203 tmp |= CLR_FX;
1204 tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
1205 __raw_writel(tmp, ep->creg);
1206 if (!list_empty(&ep->queue))
1207 handle_ep(ep);
1208 goto succeed;
1209 }
1210
1211#undef w_value
1212#undef w_index
1213#undef w_length
1214
1215 /* pass request up to the gadget driver */
1216 status = udc->driver->setup(&udc->gadget, &pkt.r);
1217 if (status < 0) {
1218stall:
1219 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1220 pkt.r.bRequestType, pkt.r.bRequest, status);
1221 csr |= AT91_UDP_FORCESTALL;
1222 __raw_writel(csr, creg);
1223 udc->req_pending = 0;
1224 }
1225 return;
1226
1227succeed:
1228 /* immediate successful (IN) STATUS after zero length DATA */
1229 PACKET("ep0 in/status\n");
1230write_in:
1231 csr |= AT91_UDP_TXPKTRDY;
1232 __raw_writel(csr, creg);
1233 udc->req_pending = 0;
1234 return;
1235}
1236
1237static void handle_ep0(struct at91_udc *udc)
1238{
1239 struct at91_ep *ep0 = &udc->ep[0];
1240 u32 __iomem *creg = ep0->creg;
1241 u32 csr = __raw_readl(creg);
1242 struct at91_request *req;
1243
1244 if (unlikely(csr & AT91_UDP_STALLSENT)) {
1245 nuke(ep0, -EPROTO);
1246 udc->req_pending = 0;
1247 csr |= CLR_FX;
1248 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
1249 __raw_writel(csr, creg);
1250 VDBG("ep0 stalled\n");
1251 csr = __raw_readl(creg);
1252 }
1253 if (csr & AT91_UDP_RXSETUP) {
1254 nuke(ep0, 0);
1255 udc->req_pending = 0;
1256 handle_setup(udc, ep0, csr);
1257 return;
1258 }
1259
1260 if (list_empty(&ep0->queue))
1261 req = NULL;
1262 else
1263 req = list_entry(ep0->queue.next, struct at91_request, queue);
1264
1265 /* host ACKed an IN packet that we sent */
1266 if (csr & AT91_UDP_TXCOMP) {
1267 csr |= CLR_FX;
1268 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
1269
1270 /* write more IN DATA? */
1271 if (req && ep0->is_in) {
1272 if (handle_ep(ep0))
1273 udc->req_pending = 0;
1274
1275 /*
1276 * Ack after:
1277 * - last IN DATA packet (including GET_STATUS)
1278 * - IN/STATUS for OUT DATA
1279 * - IN/STATUS for any zero-length DATA stage
1280 * except for the IN DATA case, the host should send
1281 * an OUT status later, which we'll ack.
1282 */
1283 } else {
1284 udc->req_pending = 0;
1285 __raw_writel(csr, creg);
1286
1287 /*
1288 * SET_ADDRESS takes effect only after the STATUS
1289 * (to the original address) gets acked.
1290 */
1291 if (udc->wait_for_addr_ack) {
1292 u32 tmp;
1293
1294 at91_udp_write(AT91_UDP_FADDR, AT91_UDP_FEN | udc->addr);
1295 tmp = at91_udp_read(AT91_UDP_GLB_STAT);
1296 tmp &= ~AT91_UDP_FADDEN;
1297 if (udc->addr)
1298 tmp |= AT91_UDP_FADDEN;
1299 at91_udp_write(AT91_UDP_GLB_STAT, tmp);
1300
1301 udc->wait_for_addr_ack = 0;
1302 VDBG("address %d\n", udc->addr);
1303 }
1304 }
1305 }
1306
1307 /* OUT packet arrived ... */
1308 else if (csr & AT91_UDP_RX_DATA_BK0) {
1309 csr |= CLR_FX;
1310 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
1311
1312 /* OUT DATA stage */
1313 if (!ep0->is_in) {
1314 if (req) {
1315 if (handle_ep(ep0)) {
1316 /* send IN/STATUS */
1317 PACKET("ep0 in/status\n");
1318 csr = __raw_readl(creg);
1319 csr &= ~SET_FX;
1320 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
1321 __raw_writel(csr, creg);
1322 udc->req_pending = 0;
1323 }
1324 } else if (udc->req_pending) {
1325 /*
1326 * AT91 hardware has a hard time with this
1327 * "deferred response" mode for control-OUT
1328 * transfers. (For control-IN it's fine.)
1329 *
1330 * The normal solution leaves OUT data in the
1331 * fifo until the gadget driver is ready.
1332 * We couldn't do that here without disabling
1333 * the IRQ that tells about SETUP packets,
1334 * e.g. when the host gets impatient...
1335 *
1336 * Working around it by copying into a buffer
1337 * would almost be a non-deferred response,
1338 * except that it wouldn't permit reliable
1339 * stalling of the request. Instead, demand
1340 * that gadget drivers not use this mode.
1341 */
1342 DBG("no control-OUT deferred responses!\n");
1343 __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
1344 udc->req_pending = 0;
1345 }
1346
1347 /* STATUS stage for control-IN; ack. */
1348 } else {
1349 PACKET("ep0 out/status ACK\n");
1350 __raw_writel(csr, creg);
1351
1352 /* "early" status stage */
1353 if (req)
1354 done(ep0, req, 0);
1355 }
1356 }
1357}
1358
1359static irqreturn_t at91_udc_irq (int irq, void *_udc, struct pt_regs *r)
1360{
1361 struct at91_udc *udc = _udc;
1362 u32 rescans = 5;
1363
1364 while (rescans--) {
1365 u32 status = at91_udp_read(AT91_UDP_ISR);
1366
1367 status &= at91_udp_read(AT91_UDP_IMR);
1368 if (!status)
1369 break;
1370
1371 /* USB reset irq: not maskable */
1372 if (status & AT91_UDP_ENDBUSRES) {
1373 at91_udp_write(AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
1374 at91_udp_write(AT91_UDP_IER, MINIMUS_INTERRUPTUS);
1375 /* Atmel code clears this irq twice */
1376 at91_udp_write(AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1377 at91_udp_write(AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1378 VDBG("end bus reset\n");
1379 udc->addr = 0;
1380 stop_activity(udc);
1381
1382 /* enable ep0 */
1383 at91_udp_write(AT91_UDP_CSR(0), AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
1384 udc->gadget.speed = USB_SPEED_FULL;
1385 udc->suspended = 0;
1386 at91_udp_write(AT91_UDP_IER, AT91_UDP_EP(0));
1387
1388 /*
1389 * NOTE: this driver keeps clocks off unless the
1390 * USB host is present. That saves power, and also
1391 * eliminates IRQs (reset, resume, suspend) that can
1392 * otherwise flood from the controller. If your
1393 * board doesn't support VBUS detection, suspend and
1394 * resume irq logic may need more attention...
1395 */
1396
1397 /* host initiated suspend (3+ms bus idle) */
1398 } else if (status & AT91_UDP_RXSUSP) {
1399 at91_udp_write(AT91_UDP_IDR, AT91_UDP_RXSUSP);
1400 at91_udp_write(AT91_UDP_IER, AT91_UDP_RXRSM);
1401 at91_udp_write(AT91_UDP_ICR, AT91_UDP_RXSUSP);
1402 // VDBG("bus suspend\n");
1403 if (udc->suspended)
1404 continue;
1405 udc->suspended = 1;
1406
1407 /*
1408 * NOTE: when suspending a VBUS-powered device, the
1409 * gadget driver should switch into slow clock mode
1410 * and then into standby to avoid drawing more than
1411 * 500uA power (2500uA for some high-power configs).
1412 */
1413 if (udc->driver && udc->driver->suspend)
1414 udc->driver->suspend(&udc->gadget);
1415
1416 /* host initiated resume */
1417 } else if (status & AT91_UDP_RXRSM) {
1418 at91_udp_write(AT91_UDP_IDR, AT91_UDP_RXRSM);
1419 at91_udp_write(AT91_UDP_IER, AT91_UDP_RXSUSP);
1420 at91_udp_write(AT91_UDP_ICR, AT91_UDP_RXRSM);
1421 // VDBG("bus resume\n");
1422 if (!udc->suspended)
1423 continue;
1424 udc->suspended = 0;
1425
1426 /*
1427 * NOTE: for a VBUS-powered device, the gadget driver
1428 * would normally want to switch out of slow clock
1429 * mode into normal mode.
1430 */
1431 if (udc->driver && udc->driver->resume)
1432 udc->driver->resume(&udc->gadget);
1433
1434 /* endpoint IRQs are cleared by handling them */
1435 } else {
1436 int i;
1437 unsigned mask = 1;
1438 struct at91_ep *ep = &udc->ep[1];
1439
1440 if (status & mask)
1441 handle_ep0(udc);
1442 for (i = 1; i < NUM_ENDPOINTS; i++) {
1443 mask <<= 1;
1444 if (status & mask)
1445 handle_ep(ep);
1446 ep++;
1447 }
1448 }
1449 }
1450
1451 return IRQ_HANDLED;
1452}
1453
1454/*-------------------------------------------------------------------------*/
1455
1456static struct at91_udc controller = {
1457 .gadget = {
1458 .ops = &at91_udc_ops,
1459 .ep0 = &controller.ep[0].ep,
1460 .name = driver_name,
1461 .dev = {
1462 .bus_id = "gadget"
1463 }
1464 },
1465 .ep[0] = {
1466 .ep = {
1467 .name = ep0name,
1468 .ops = &at91_ep_ops,
1469 },
1470 .udc = &controller,
1471 .maxpacket = 8,
1472 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(0)),
1473 .int_mask = 1 << 0,
1474 },
1475 .ep[1] = {
1476 .ep = {
1477 .name = "ep1",
1478 .ops = &at91_ep_ops,
1479 },
1480 .udc = &controller,
1481 .is_pingpong = 1,
1482 .maxpacket = 64,
1483 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(1)),
1484 .int_mask = 1 << 1,
1485 },
1486 .ep[2] = {
1487 .ep = {
1488 .name = "ep2",
1489 .ops = &at91_ep_ops,
1490 },
1491 .udc = &controller,
1492 .is_pingpong = 1,
1493 .maxpacket = 64,
1494 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(2)),
1495 .int_mask = 1 << 2,
1496 },
1497 .ep[3] = {
1498 .ep = {
1499 /* could actually do bulk too */
1500 .name = "ep3-int",
1501 .ops = &at91_ep_ops,
1502 },
1503 .udc = &controller,
1504 .maxpacket = 8,
1505 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(3)),
1506 .int_mask = 1 << 3,
1507 },
1508 .ep[4] = {
1509 .ep = {
1510 .name = "ep4",
1511 .ops = &at91_ep_ops,
1512 },
1513 .udc = &controller,
1514 .is_pingpong = 1,
1515 .maxpacket = 256,
1516 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(4)),
1517 .int_mask = 1 << 4,
1518 },
1519 .ep[5] = {
1520 .ep = {
1521 .name = "ep5",
1522 .ops = &at91_ep_ops,
1523 },
1524 .udc = &controller,
1525 .is_pingpong = 1,
1526 .maxpacket = 256,
1527 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(5)),
1528 .int_mask = 1 << 5,
1529 },
1530 /* ep6 and ep7 are also reserved */
1531};
1532
1533static irqreturn_t at91_vbus_irq(int irq, void *_udc, struct pt_regs *r)
1534{
1535 struct at91_udc *udc = _udc;
1536 unsigned value;
1537
1538 /* vbus needs at least brief debouncing */
1539 udelay(10);
1540 value = at91_get_gpio_value(udc->board.vbus_pin);
1541 if (value != udc->vbus)
1542 at91_vbus_session(&udc->gadget, value);
1543
1544 return IRQ_HANDLED;
1545}
1546
1547int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1548{
1549 struct at91_udc *udc = &controller;
1550 int retval;
1551
1552 if (!driver
1553 || driver->speed != USB_SPEED_FULL
1554 || !driver->bind
1555 || !driver->unbind
1556 || !driver->setup) {
1557 DBG("bad parameter.\n");
1558 return -EINVAL;
1559 }
1560
1561 if (udc->driver) {
1562 DBG("UDC already has a gadget driver\n");
1563 return -EBUSY;
1564 }
1565
1566 udc->driver = driver;
1567 udc->gadget.dev.driver = &driver->driver;
1568 udc->gadget.dev.driver_data = &driver->driver;
1569 udc->enabled = 1;
1570 udc->selfpowered = 1;
1571
1572 retval = driver->bind(&udc->gadget);
1573 if (retval) {
1574 DBG("driver->bind() returned %d\n", retval);
1575 udc->driver = NULL;
1576 return retval;
1577 }
1578
1579 local_irq_disable();
1580 pullup(udc, 1);
1581 local_irq_enable();
1582
1583 DBG("bound to %s\n", driver->driver.name);
1584 return 0;
1585}
1586EXPORT_SYMBOL (usb_gadget_register_driver);
1587
1588int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
1589{
1590 struct at91_udc *udc = &controller;
1591
1592 if (!driver || driver != udc->driver)
1593 return -EINVAL;
1594
1595 local_irq_disable();
1596 udc->enabled = 0;
1597 pullup(udc, 0);
1598 local_irq_enable();
1599
1600 driver->unbind(&udc->gadget);
1601 udc->driver = NULL;
1602
1603 DBG("unbound from %s\n", driver->driver.name);
1604 return 0;
1605}
1606EXPORT_SYMBOL (usb_gadget_unregister_driver);
1607
1608/*-------------------------------------------------------------------------*/
1609
1610static void at91udc_shutdown(struct platform_device *dev)
1611{
1612 /* force disconnect on reboot */
1613 pullup(platform_get_drvdata(dev), 0);
1614}
1615
1616static int __devinit at91udc_probe(struct platform_device *pdev)
1617{
1618 struct device *dev = &pdev->dev;
1619 struct at91_udc *udc;
1620 int retval;
1621
1622 if (!dev->platform_data) {
1623 /* small (so we copy it) but critical! */
1624 DBG("missing platform_data\n");
1625 return -ENODEV;
1626 }
1627
1628 if (!request_mem_region(AT91_BASE_UDP, SZ_16K, driver_name)) {
1629 DBG("someone's using UDC memory\n");
1630 return -EBUSY;
1631 }
1632
1633 /* init software state */
1634 udc = &controller;
1635 udc->gadget.dev.parent = dev;
1636 udc->board = *(struct at91_udc_data *) dev->platform_data;
1637 udc->pdev = pdev;
1638 udc_reinit(udc);
1639 udc->enabled = 0;
1640
1641 /* get interface and function clocks */
1642 udc->iclk = clk_get(dev, "udc_clk");
1643 udc->fclk = clk_get(dev, "udpck");
1644 if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
1645 DBG("clocks missing\n");
1646 return -ENODEV;
1647 }
1648
1649 retval = device_register(&udc->gadget.dev);
1650 if (retval < 0)
1651 goto fail0;
1652
1653 /* disable everything until there's a gadget driver and vbus */
1654 pullup(udc, 0);
1655
1656 /* request UDC and maybe VBUS irqs */
1657 if (request_irq(AT91_ID_UDP, at91_udc_irq, SA_INTERRUPT, driver_name, udc)) {
1658 DBG("request irq %d failed\n", AT91_ID_UDP);
1659 retval = -EBUSY;
1660 goto fail1;
1661 }
1662 if (udc->board.vbus_pin > 0) {
1663 if (request_irq(udc->board.vbus_pin, at91_vbus_irq, SA_INTERRUPT, driver_name, udc)) {
1664 DBG("request vbus irq %d failed\n", udc->board.vbus_pin);
1665 free_irq(AT91_ID_UDP, udc);
1666 retval = -EBUSY;
1667 goto fail1;
1668 }
1669 } else {
1670 DBG("no VBUS detection, assuming always-on\n");
1671 udc->vbus = 1;
1672 }
1673 dev_set_drvdata(dev, udc);
1674 create_debug_file(udc);
1675
1676 INFO("%s version %s\n", driver_name, DRIVER_VERSION);
1677 return 0;
1678
1679fail1:
1680 device_unregister(&udc->gadget.dev);
1681fail0:
1682 release_mem_region(AT91_VA_BASE_UDP, SZ_16K);
1683 DBG("%s probe failed, %d\n", driver_name, retval);
1684 return retval;
1685}
1686
1687static int __devexit at91udc_remove(struct platform_device *dev)
1688{
1689 struct at91_udc *udc = platform_get_drvdata(dev);
1690
1691 DBG("remove\n");
1692
1693 pullup(udc, 0);
1694
1695 if (udc->driver != 0)
1696 usb_gadget_unregister_driver(udc->driver);
1697
1698 remove_debug_file(udc);
1699 if (udc->board.vbus_pin > 0)
1700 free_irq(udc->board.vbus_pin, udc);
1701 free_irq(AT91_ID_UDP, udc);
1702 device_unregister(&udc->gadget.dev);
1703 release_mem_region(AT91_BASE_UDP, SZ_16K);
1704
1705 clk_put(udc->iclk);
1706 clk_put(udc->fclk);
1707
1708 return 0;
1709}
1710
1711#ifdef CONFIG_PM
1712static int at91udc_suspend(struct platform_device *dev, u32 state, u32 level)
1713{
1714 struct at91_udc *udc = platform_get_drvdata(dev);
1715
1716 /*
1717 * The "safe" suspend transitions are opportunistic ... e.g. when
1718 * the USB link is suspended (48MHz clock autogated off), or when
1719 * it's disconnected (programmatically gated off, elsewhere).
1720 * Then we can suspend, and the chip can enter slow clock mode.
1721 *
1722 * The problem case is some component (user mode?) suspending this
1723 * device while it's active, with the 48 MHz clock in use. There
1724 * are two basic approaches: (a) veto suspend levels involving slow
1725 * clock mode, (b) disconnect, so 48 MHz will no longer be in use
1726 * and we can enter slow clock mode. This uses (b) for now, since
1727 * it's simplest until AT91 PM exists and supports the other option.
1728 */
1729 if (udc->vbus && !udc->suspended)
1730 pullup(udc, 0);
1731 return 0;
1732}
1733
1734static int at91udc_resume(struct platform_device *dev, u32 level)
1735{
1736 struct at91_udc *udc = platform_get_drvdata(dev);
1737
1738 /* maybe reconnect to host; if so, clocks on */
1739 pullup(udc, 1);
1740 return 0;
1741}
1742#else
1743#define at91udc_suspend NULL
1744#define at91udc_resume NULL
1745#endif
1746
1747static struct platform_driver at91_udc = {
1748 .probe = at91udc_probe,
1749 .remove = __devexit_p(at91udc_remove),
1750 .shutdown = at91udc_shutdown,
1751 .suspend = at91udc_suspend,
1752 .resume = at91udc_resume,
1753 .driver = {
1754 .name = (char *) driver_name,
1755 .owner = THIS_MODULE,
1756 },
1757};
1758
1759static int __devinit udc_init_module(void)
1760{
1761 return platform_driver_register(&at91_udc);
1762}
1763module_init(udc_init_module);
1764
1765static void __devexit udc_exit_module(void)
1766{
1767 platform_driver_unregister(&at91_udc);
1768}
1769module_exit(udc_exit_module);
1770
1771MODULE_DESCRIPTION("AT91RM9200 udc driver");
1772MODULE_AUTHOR("Thomas Rathbone, David Brownell");
1773MODULE_LICENSE("GPL");
diff --git a/drivers/usb/gadget/at91_udc.h b/drivers/usb/gadget/at91_udc.h
new file mode 100644
index 000000000000..5a4799cedd19
--- /dev/null
+++ b/drivers/usb/gadget/at91_udc.h
@@ -0,0 +1,181 @@
1/*
2 * Copyright (C) 2004 by Thomas Rathbone, HP Labs
3 * Copyright (C) 2005 by Ivan Kokshaysky
4 * Copyright (C) 2006 by SAN People
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#ifndef AT91_UDC_H
23#define AT91_UDC_H
24
25/*
26 * USB Device Port (UDP) registers.
27 * Based on AT91RM9200 datasheet revision E.
28 */
29
30#define AT91_UDP_FRM_NUM 0x00 /* Frame Number Register */
31#define AT91_UDP_NUM (0x7ff << 0) /* Frame Number */
32#define AT91_UDP_FRM_ERR (1 << 16) /* Frame Error */
33#define AT91_UDP_FRM_OK (1 << 17) /* Frame OK */
34
35#define AT91_UDP_GLB_STAT 0x04 /* Global State Register */
36#define AT91_UDP_FADDEN (1 << 0) /* Function Address Enable */
37#define AT91_UDP_CONFG (1 << 1) /* Configured */
38#define AT91_UDP_ESR (1 << 2) /* Enable Send Resume */
39#define AT91_UDP_RSMINPR (1 << 3) /* Resume has been sent */
40#define AT91_UDP_RMWUPE (1 << 4) /* Remote Wake Up Enable */
41
42#define AT91_UDP_FADDR 0x08 /* Function Address Register */
43#define AT91_UDP_FADD (0x7f << 0) /* Function Address Value */
44#define AT91_UDP_FEN (1 << 8) /* Function Enable */
45
46#define AT91_UDP_IER 0x10 /* Interrupt Enable Register */
47#define AT91_UDP_IDR 0x14 /* Interrupt Disable Register */
48#define AT91_UDP_IMR 0x18 /* Interrupt Mask Register */
49
50#define AT91_UDP_ISR 0x1c /* Interrupt Status Register */
51#define AT91_UDP_EP(n) (1 << (n)) /* Endpoint Interrupt Status */
52#define AT91_UDP_RXSUSP (1 << 8) /* USB Suspend Interrupt Status */
53#define AT91_UDP_RXRSM (1 << 9) /* USB Resume Interrupt Status */
54#define AT91_UDP_EXTRSM (1 << 10) /* External Resume Interrupt Status */
55#define AT91_UDP_SOFINT (1 << 11) /* Start of Frame Interrupt Status */
56#define AT91_UDP_ENDBUSRES (1 << 12) /* End of Bus Reset Interrpt Status */
57#define AT91_UDP_WAKEUP (1 << 13) /* USB Wakeup Interrupt Status */
58
59#define AT91_UDP_ICR 0x20 /* Interrupt Clear Register */
60#define AT91_UDP_RST_EP 0x28 /* Reset Endpoint Register */
61
62#define AT91_UDP_CSR(n) (0x30+((n)*4)) /* Endpoint Control/Status Registers 0-7 */
63#define AT91_UDP_TXCOMP (1 << 0) /* Generates IN packet with data previously written in DPR */
64#define AT91_UDP_RX_DATA_BK0 (1 << 1) /* Receive Data Bank 0 */
65#define AT91_UDP_RXSETUP (1 << 2) /* Send STALL to the host */
66#define AT91_UDP_STALLSENT (1 << 3) /* Stall Sent / Isochronous error (Isochronous endpoints) */
67#define AT91_UDP_TXPKTRDY (1 << 4) /* Transmit Packet Ready */
68#define AT91_UDP_FORCESTALL (1 << 5) /* Force Stall */
69#define AT91_UDP_RX_DATA_BK1 (1 << 6) /* Receive Data Bank 1 */
70#define AT91_UDP_DIR (1 << 7) /* Transfer Direction */
71#define AT91_UDP_EPTYPE (7 << 8) /* Endpoint Type */
72#define AT91_UDP_EPTYPE_CTRL (0 << 8)
73#define AT91_UDP_EPTYPE_ISO_OUT (1 << 8)
74#define AT91_UDP_EPTYPE_BULK_OUT (2 << 8)
75#define AT91_UDP_EPTYPE_INT_OUT (3 << 8)
76#define AT91_UDP_EPTYPE_ISO_IN (5 << 8)
77#define AT91_UDP_EPTYPE_BULK_IN (6 << 8)
78#define AT91_UDP_EPTYPE_INT_IN (7 << 8)
79#define AT91_UDP_DTGLE (1 << 11) /* Data Toggle */
80#define AT91_UDP_EPEDS (1 << 15) /* Endpoint Enable/Disable */
81#define AT91_UDP_RXBYTECNT (0x7ff << 16) /* Number of bytes in FIFO */
82
83#define AT91_UDP_FDR(n) (0x50+((n)*4)) /* Endpoint FIFO Data Registers 0-7 */
84
85#define AT91_UDP_TXVC 0x74 /* Transceiver Control Register */
86#define AT91_UDP_TXVC_TXVDIS (1 << 8) /* Transceiver Disable */
87
88
89/*-------------------------------------------------------------------------*/
90
91/*
92 * controller driver data structures
93 */
94
95#define NUM_ENDPOINTS 6
96
97/*
98 * hardware won't disable bus reset, or resume while the controller
99 * is suspended ... watching suspend helps keep the logic symmetric.
100 */
101#define MINIMUS_INTERRUPTUS \
102 (AT91_UDP_ENDBUSRES | AT91_UDP_RXRSM | AT91_UDP_RXSUSP)
103
104struct at91_ep {
105 struct usb_ep ep;
106 struct list_head queue;
107 struct at91_udc *udc;
108 void __iomem *creg;
109
110 unsigned maxpacket:16;
111 u8 int_mask;
112 unsigned is_pingpong:1;
113
114 unsigned stopped:1;
115 unsigned is_in:1;
116 unsigned is_iso:1;
117 unsigned fifo_bank:1;
118
119 const struct usb_endpoint_descriptor
120 *desc;
121};
122
123/*
124 * driver is non-SMP, and just blocks IRQs whenever it needs
125 * access protection for chip registers or driver state
126 */
127struct at91_udc {
128 struct usb_gadget gadget;
129 struct at91_ep ep[NUM_ENDPOINTS];
130 struct usb_gadget_driver *driver;
131 unsigned vbus:1;
132 unsigned enabled:1;
133 unsigned clocked:1;
134 unsigned suspended:1;
135 unsigned req_pending:1;
136 unsigned wait_for_addr_ack:1;
137 unsigned wait_for_config_ack:1;
138 unsigned selfpowered:1;
139 u8 addr;
140 struct at91_udc_data board;
141 struct clk *iclk, *fclk;
142 struct platform_device *pdev;
143 struct proc_dir_entry *pde;
144};
145
146static inline struct at91_udc *to_udc(struct usb_gadget *g)
147{
148 return container_of(g, struct at91_udc, gadget);
149}
150
151struct at91_request {
152 struct usb_request req;
153 struct list_head queue;
154};
155
156/*-------------------------------------------------------------------------*/
157
158#ifdef DEBUG
159#define DBG(stuff...) printk(KERN_DEBUG "udc: " stuff)
160#else
161#define DBG(stuff...) do{}while(0)
162#endif
163
164#ifdef VERBOSE
165# define VDBG DBG
166#else
167# define VDBG(stuff...) do{}while(0)
168#endif
169
170#ifdef PACKET_TRACE
171# define PACKET VDBG
172#else
173# define PACKET(stuff...) do{}while(0)
174#endif
175
176#define ERR(stuff...) printk(KERN_ERR "udc: " stuff)
177#define WARN(stuff...) printk(KERN_WARNING "udc: " stuff)
178#define INFO(stuff...) printk(KERN_INFO "udc: " stuff)
179
180#endif
181
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
index 9734cb76dd6c..42ce41d71b7f 100644
--- a/drivers/usb/gadget/dummy_hcd.c
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -478,10 +478,9 @@ dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
478 return NULL; 478 return NULL;
479 ep = usb_ep_to_dummy_ep (_ep); 479 ep = usb_ep_to_dummy_ep (_ep);
480 480
481 req = kmalloc (sizeof *req, mem_flags); 481 req = kzalloc(sizeof(*req), mem_flags);
482 if (!req) 482 if (!req)
483 return NULL; 483 return NULL;
484 memset (req, 0, sizeof *req);
485 INIT_LIST_HEAD (&req->queue); 484 INIT_LIST_HEAD (&req->queue);
486 return &req->req; 485 return &req->req;
487} 486}
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index afc84cfb61f9..c3d8e5c5bf28 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -182,33 +182,37 @@ struct eth_dev {
182 * parameters are in UTF-8 (superset of ASCII's 7 bit characters). 182 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
183 */ 183 */
184 184
185static ushort __initdata idVendor; 185static ushort idVendor;
186module_param(idVendor, ushort, S_IRUGO); 186module_param(idVendor, ushort, S_IRUGO);
187MODULE_PARM_DESC(idVendor, "USB Vendor ID"); 187MODULE_PARM_DESC(idVendor, "USB Vendor ID");
188 188
189static ushort __initdata idProduct; 189static ushort idProduct;
190module_param(idProduct, ushort, S_IRUGO); 190module_param(idProduct, ushort, S_IRUGO);
191MODULE_PARM_DESC(idProduct, "USB Product ID"); 191MODULE_PARM_DESC(idProduct, "USB Product ID");
192 192
193static ushort __initdata bcdDevice; 193static ushort bcdDevice;
194module_param(bcdDevice, ushort, S_IRUGO); 194module_param(bcdDevice, ushort, S_IRUGO);
195MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); 195MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
196 196
197static char *__initdata iManufacturer; 197static char *iManufacturer;
198module_param(iManufacturer, charp, S_IRUGO); 198module_param(iManufacturer, charp, S_IRUGO);
199MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); 199MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
200 200
201static char *__initdata iProduct; 201static char *iProduct;
202module_param(iProduct, charp, S_IRUGO); 202module_param(iProduct, charp, S_IRUGO);
203MODULE_PARM_DESC(iProduct, "USB Product string"); 203MODULE_PARM_DESC(iProduct, "USB Product string");
204 204
205static char *iSerialNumber;
206module_param(iSerialNumber, charp, S_IRUGO);
207MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
208
205/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */ 209/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
206static char *__initdata dev_addr; 210static char *dev_addr;
207module_param(dev_addr, charp, S_IRUGO); 211module_param(dev_addr, charp, S_IRUGO);
208MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); 212MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
209 213
210/* this address is invisible to ifconfig */ 214/* this address is invisible to ifconfig */
211static char *__initdata host_addr; 215static char *host_addr;
212module_param(host_addr, charp, S_IRUGO); 216module_param(host_addr, charp, S_IRUGO);
213MODULE_PARM_DESC(host_addr, "Host Ethernet Address"); 217MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
214 218
@@ -253,6 +257,14 @@ MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
253#define DEV_CONFIG_CDC 257#define DEV_CONFIG_CDC
254#endif 258#endif
255 259
260#ifdef CONFIG_USB_GADGET_MUSBHSFC
261#define DEV_CONFIG_CDC
262#endif
263
264#ifdef CONFIG_USB_GADGET_MUSBHDRC
265#define DEV_CONFIG_CDC
266#endif
267
256 268
257/* For CDC-incapable hardware, choose the simple cdc subset. 269/* For CDC-incapable hardware, choose the simple cdc subset.
258 * Anything that talks bulk (without notable bugs) can do this. 270 * Anything that talks bulk (without notable bugs) can do this.
@@ -395,6 +407,7 @@ static inline int BITRATE(struct usb_gadget *g)
395#define STRING_CDC 7 407#define STRING_CDC 7
396#define STRING_SUBSET 8 408#define STRING_SUBSET 8
397#define STRING_RNDIS 9 409#define STRING_RNDIS 9
410#define STRING_SERIALNUMBER 10
398 411
399/* holds our biggest descriptor (or RNDIS response) */ 412/* holds our biggest descriptor (or RNDIS response) */
400#define USB_BUFSIZ 256 413#define USB_BUFSIZ 256
@@ -862,6 +875,7 @@ static inline void __init hs_subset_descriptors(void)
862 875
863static char manufacturer [50]; 876static char manufacturer [50];
864static char product_desc [40] = DRIVER_DESC; 877static char product_desc [40] = DRIVER_DESC;
878static char serial_number [20];
865 879
866#ifdef DEV_CONFIG_CDC 880#ifdef DEV_CONFIG_CDC
867/* address that the host will use ... usually assigned at random */ 881/* address that the host will use ... usually assigned at random */
@@ -872,6 +886,7 @@ static char ethaddr [2 * ETH_ALEN + 1];
872static struct usb_string strings [] = { 886static struct usb_string strings [] = {
873 { STRING_MANUFACTURER, manufacturer, }, 887 { STRING_MANUFACTURER, manufacturer, },
874 { STRING_PRODUCT, product_desc, }, 888 { STRING_PRODUCT, product_desc, },
889 { STRING_SERIALNUMBER, serial_number, },
875 { STRING_DATA, "Ethernet Data", }, 890 { STRING_DATA, "Ethernet Data", },
876#ifdef DEV_CONFIG_CDC 891#ifdef DEV_CONFIG_CDC
877 { STRING_CDC, "CDC Ethernet", }, 892 { STRING_CDC, "CDC Ethernet", },
@@ -1549,7 +1564,8 @@ static int eth_change_mtu (struct net_device *net, int new_mtu)
1549{ 1564{
1550 struct eth_dev *dev = netdev_priv(net); 1565 struct eth_dev *dev = netdev_priv(net);
1551 1566
1552 // FIXME if rndis, don't change while link's live 1567 if (dev->rndis)
1568 return -EBUSY;
1553 1569
1554 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN) 1570 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1555 return -ERANGE; 1571 return -ERANGE;
@@ -2116,7 +2132,7 @@ eth_req_free (struct usb_ep *ep, struct usb_request *req)
2116} 2132}
2117 2133
2118 2134
2119static void 2135static void __exit
2120eth_unbind (struct usb_gadget *gadget) 2136eth_unbind (struct usb_gadget *gadget)
2121{ 2137{
2122 struct eth_dev *dev = get_gadget_data (gadget); 2138 struct eth_dev *dev = get_gadget_data (gadget);
@@ -2153,7 +2169,7 @@ static u8 __init nibble (unsigned char c)
2153 return 0; 2169 return 0;
2154} 2170}
2155 2171
2156static void __init get_ether_addr (const char *str, u8 *dev_addr) 2172static int __init get_ether_addr(const char *str, u8 *dev_addr)
2157{ 2173{
2158 if (str) { 2174 if (str) {
2159 unsigned i; 2175 unsigned i;
@@ -2168,9 +2184,10 @@ static void __init get_ether_addr (const char *str, u8 *dev_addr)
2168 dev_addr [i] = num; 2184 dev_addr [i] = num;
2169 } 2185 }
2170 if (is_valid_ether_addr (dev_addr)) 2186 if (is_valid_ether_addr (dev_addr))
2171 return; 2187 return 0;
2172 } 2188 }
2173 random_ether_addr(dev_addr); 2189 random_ether_addr(dev_addr);
2190 return 1;
2174} 2191}
2175 2192
2176static int __init 2193static int __init
@@ -2268,6 +2285,10 @@ eth_bind (struct usb_gadget *gadget)
2268 strlcpy (manufacturer, iManufacturer, sizeof manufacturer); 2285 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2269 if (iProduct) 2286 if (iProduct)
2270 strlcpy (product_desc, iProduct, sizeof product_desc); 2287 strlcpy (product_desc, iProduct, sizeof product_desc);
2288 if (iSerialNumber) {
2289 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2290 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2291 }
2271 2292
2272 /* all we really need is bulk IN/OUT */ 2293 /* all we really need is bulk IN/OUT */
2273 usb_ep_autoconfig_reset (gadget); 2294 usb_ep_autoconfig_reset (gadget);
@@ -2377,9 +2398,13 @@ autoconf_fail:
2377 * The host side address is used with CDC and RNDIS, and commonly 2398 * The host side address is used with CDC and RNDIS, and commonly
2378 * ends up in a persistent config database. 2399 * ends up in a persistent config database.
2379 */ 2400 */
2380 get_ether_addr(dev_addr, net->dev_addr); 2401 if (get_ether_addr(dev_addr, net->dev_addr))
2402 dev_warn(&gadget->dev,
2403 "using random %s ethernet address\n", "self");
2381 if (cdc || rndis) { 2404 if (cdc || rndis) {
2382 get_ether_addr(host_addr, dev->host_mac); 2405 if (get_ether_addr(host_addr, dev->host_mac))
2406 dev_warn(&gadget->dev,
2407 "using random %s ethernet address\n", "host");
2383#ifdef DEV_CONFIG_CDC 2408#ifdef DEV_CONFIG_CDC
2384 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X", 2409 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2385 dev->host_mac [0], dev->host_mac [1], 2410 dev->host_mac [0], dev->host_mac [1],
@@ -2523,7 +2548,7 @@ static struct usb_gadget_driver eth_driver = {
2523 2548
2524 .function = (char *) driver_desc, 2549 .function = (char *) driver_desc,
2525 .bind = eth_bind, 2550 .bind = eth_bind,
2526 .unbind = eth_unbind, 2551 .unbind = __exit_p(eth_unbind),
2527 2552
2528 .setup = eth_setup, 2553 .setup = eth_setup,
2529 .disconnect = eth_disconnect, 2554 .disconnect = eth_disconnect,
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index de59c58896d6..cf3be299e353 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -3678,7 +3678,7 @@ static void lun_release(struct device *dev)
3678 kref_put(&fsg->ref, fsg_release); 3678 kref_put(&fsg->ref, fsg_release);
3679} 3679}
3680 3680
3681static void fsg_unbind(struct usb_gadget *gadget) 3681static void __exit fsg_unbind(struct usb_gadget *gadget)
3682{ 3682{
3683 struct fsg_dev *fsg = get_gadget_data(gadget); 3683 struct fsg_dev *fsg = get_gadget_data(gadget);
3684 int i; 3684 int i;
@@ -4064,7 +4064,7 @@ static struct usb_gadget_driver fsg_driver = {
4064#endif 4064#endif
4065 .function = (char *) longname, 4065 .function = (char *) longname,
4066 .bind = fsg_bind, 4066 .bind = fsg_bind,
4067 .unbind = fsg_unbind, 4067 .unbind = __exit_p(fsg_unbind),
4068 .disconnect = fsg_disconnect, 4068 .disconnect = fsg_disconnect,
4069 .setup = fsg_setup, 4069 .setup = fsg_setup,
4070 .suspend = fsg_suspend, 4070 .suspend = fsg_suspend,
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
index 8cbae21d84b9..c4081407171f 100644
--- a/drivers/usb/gadget/gadget_chips.h
+++ b/drivers/usb/gadget/gadget_chips.h
@@ -3,9 +3,9 @@
3 * gadget drivers or other code that needs to deal with them, and which 3 * gadget drivers or other code that needs to deal with them, and which
4 * autoconfigures instead of using early binding to the hardware. 4 * autoconfigures instead of using early binding to the hardware.
5 * 5 *
6 * This could eventually work like the ARM mach_is_*() stuff, driven by 6 * This SHOULD eventually work like the ARM mach_is_*() stuff, driven by
7 * some config file that gets updated as new hardware is supported. 7 * some config file that gets updated as new hardware is supported.
8 * (And avoiding the runtime comparisons in typical one-choice cases.) 8 * (And avoiding all runtime comparisons in typical one-choice configs!)
9 * 9 *
10 * NOTE: some of these controller drivers may not be available yet. 10 * NOTE: some of these controller drivers may not be available yet.
11 */ 11 */
@@ -93,6 +93,26 @@
93#define gadget_is_imx(g) 0 93#define gadget_is_imx(g) 0
94#endif 94#endif
95 95
96/* Mentor high speed function controller */
97#ifdef CONFIG_USB_GADGET_MUSBHSFC
98#define gadget_is_musbhsfc(g) !strcmp("musbhsfc_udc", (g)->name)
99#else
100#define gadget_is_musbhsfc(g) 0
101#endif
102
103/* Mentor high speed "dual role" controller, peripheral mode */
104#ifdef CONFIG_USB_GADGET_MUSBHDRC
105#define gadget_is_musbhdrc(g) !strcmp("musbhdrc_udc", (g)->name)
106#else
107#define gadget_is_musbhdrc(g) 0
108#endif
109
110#ifdef CONFIG_USB_GADGET_MPC8272
111#define gadget_is_mpc8272(g) !strcmp("mpc8272_udc", (g)->name)
112#else
113#define gadget_is_mpc8272(g) 0
114#endif
115
96// CONFIG_USB_GADGET_SX2 116// CONFIG_USB_GADGET_SX2
97// CONFIG_USB_GADGET_AU1X00 117// CONFIG_USB_GADGET_AU1X00
98// ... 118// ...
@@ -143,5 +163,11 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
143 return 0x13; 163 return 0x13;
144 else if (gadget_is_imx(gadget)) 164 else if (gadget_is_imx(gadget))
145 return 0x14; 165 return 0x14;
166 else if (gadget_is_musbhsfc(gadget))
167 return 0x15;
168 else if (gadget_is_musbhdrc(gadget))
169 return 0x16;
170 else if (gadget_is_mpc8272(gadget))
171 return 0x17;
146 return -ENOENT; 172 return -ENOENT;
147} 173}
diff --git a/drivers/usb/gadget/goku_udc.c b/drivers/usb/gadget/goku_udc.c
index b0f3cd63e3b9..66b81bbf6bee 100644
--- a/drivers/usb/gadget/goku_udc.c
+++ b/drivers/usb/gadget/goku_udc.c
@@ -275,11 +275,10 @@ goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
275 275
276 if (!_ep) 276 if (!_ep)
277 return NULL; 277 return NULL;
278 req = kmalloc(sizeof *req, gfp_flags); 278 req = kzalloc(sizeof *req, gfp_flags);
279 if (!req) 279 if (!req)
280 return NULL; 280 return NULL;
281 281
282 memset(req, 0, sizeof *req);
283 req->req.dma = DMA_ADDR_INVALID; 282 req->req.dma = DMA_ADDR_INVALID;
284 INIT_LIST_HEAD(&req->queue); 283 INIT_LIST_HEAD(&req->queue);
285 return &req->req; 284 return &req->req;
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index 0aab7d24c768..3f618ce6998d 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -170,10 +170,9 @@ static struct dev_data *dev_new (void)
170{ 170{
171 struct dev_data *dev; 171 struct dev_data *dev;
172 172
173 dev = kmalloc (sizeof *dev, GFP_KERNEL); 173 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
174 if (!dev) 174 if (!dev)
175 return NULL; 175 return NULL;
176 memset (dev, 0, sizeof *dev);
177 dev->state = STATE_DEV_DISABLED; 176 dev->state = STATE_DEV_DISABLED;
178 atomic_set (&dev->count, 1); 177 atomic_set (&dev->count, 1);
179 spin_lock_init (&dev->lock); 178 spin_lock_init (&dev->lock);
@@ -1582,7 +1581,7 @@ restart:
1582 1581
1583static struct inode * 1582static struct inode *
1584gadgetfs_create_file (struct super_block *sb, char const *name, 1583gadgetfs_create_file (struct super_block *sb, char const *name,
1585 void *data, struct file_operations *fops, 1584 void *data, const struct file_operations *fops,
1586 struct dentry **dentry_p); 1585 struct dentry **dentry_p);
1587 1586
1588static int activate_ep_files (struct dev_data *dev) 1587static int activate_ep_files (struct dev_data *dev)
@@ -1592,10 +1591,9 @@ static int activate_ep_files (struct dev_data *dev)
1592 gadget_for_each_ep (ep, dev->gadget) { 1591 gadget_for_each_ep (ep, dev->gadget) {
1593 struct ep_data *data; 1592 struct ep_data *data;
1594 1593
1595 data = kmalloc (sizeof *data, GFP_KERNEL); 1594 data = kzalloc(sizeof(*data), GFP_KERNEL);
1596 if (!data) 1595 if (!data)
1597 goto enomem; 1596 goto enomem;
1598 memset (data, 0, sizeof data);
1599 data->state = STATE_EP_DISABLED; 1597 data->state = STATE_EP_DISABLED;
1600 init_MUTEX (&data->lock); 1598 init_MUTEX (&data->lock);
1601 init_waitqueue_head (&data->wait); 1599 init_waitqueue_head (&data->wait);
@@ -1957,7 +1955,7 @@ module_param (default_perm, uint, 0644);
1957 1955
1958static struct inode * 1956static struct inode *
1959gadgetfs_make_inode (struct super_block *sb, 1957gadgetfs_make_inode (struct super_block *sb,
1960 void *data, struct file_operations *fops, 1958 void *data, const struct file_operations *fops,
1961 int mode) 1959 int mode)
1962{ 1960{
1963 struct inode *inode = new_inode (sb); 1961 struct inode *inode = new_inode (sb);
@@ -1981,7 +1979,7 @@ gadgetfs_make_inode (struct super_block *sb,
1981 */ 1979 */
1982static struct inode * 1980static struct inode *
1983gadgetfs_create_file (struct super_block *sb, char const *name, 1981gadgetfs_create_file (struct super_block *sb, char const *name,
1984 void *data, struct file_operations *fops, 1982 void *data, const struct file_operations *fops,
1985 struct dentry **dentry_p) 1983 struct dentry **dentry_p)
1986{ 1984{
1987 struct dentry *dentry; 1985 struct dentry *dentry;
diff --git a/drivers/usb/gadget/lh7a40x_udc.c b/drivers/usb/gadget/lh7a40x_udc.c
index 1a362c5e7f3d..0d3424eda038 100644
--- a/drivers/usb/gadget/lh7a40x_udc.c
+++ b/drivers/usb/gadget/lh7a40x_udc.c
@@ -1114,11 +1114,10 @@ static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep,
1114 1114
1115 DEBUG("%s, %p\n", __FUNCTION__, ep); 1115 DEBUG("%s, %p\n", __FUNCTION__, ep);
1116 1116
1117 req = kmalloc(sizeof *req, gfp_flags); 1117 req = kzalloc(sizeof(*req), gfp_flags);
1118 if (!req) 1118 if (!req)
1119 return 0; 1119 return 0;
1120 1120
1121 memset(req, 0, sizeof *req);
1122 INIT_LIST_HEAD(&req->queue); 1121 INIT_LIST_HEAD(&req->queue);
1123 1122
1124 return &req->req; 1123 return &req->req;
diff --git a/drivers/usb/gadget/net2280.c b/drivers/usb/gadget/net2280.c
index 67b13ab2f3f5..fb73dc100535 100644
--- a/drivers/usb/gadget/net2280.c
+++ b/drivers/usb/gadget/net2280.c
@@ -386,11 +386,10 @@ net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
386 return NULL; 386 return NULL;
387 ep = container_of (_ep, struct net2280_ep, ep); 387 ep = container_of (_ep, struct net2280_ep, ep);
388 388
389 req = kmalloc (sizeof *req, gfp_flags); 389 req = kzalloc(sizeof(*req), gfp_flags);
390 if (!req) 390 if (!req)
391 return NULL; 391 return NULL;
392 392
393 memset (req, 0, sizeof *req);
394 req->req.dma = DMA_ADDR_INVALID; 393 req->req.dma = DMA_ADDR_INVALID;
395 INIT_LIST_HEAD (&req->queue); 394 INIT_LIST_HEAD (&req->queue);
396 395
diff --git a/drivers/usb/gadget/omap_udc.c b/drivers/usb/gadget/omap_udc.c
index a8972d7c97be..fbea51448909 100644
--- a/drivers/usb/gadget/omap_udc.c
+++ b/drivers/usb/gadget/omap_udc.c
@@ -273,9 +273,8 @@ omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
273{ 273{
274 struct omap_req *req; 274 struct omap_req *req;
275 275
276 req = kmalloc(sizeof *req, gfp_flags); 276 req = kzalloc(sizeof(*req), gfp_flags);
277 if (req) { 277 if (req) {
278 memset (req, 0, sizeof *req);
279 req->req.dma = DMA_ADDR_INVALID; 278 req->req.dma = DMA_ADDR_INVALID;
280 INIT_LIST_HEAD (&req->queue); 279 INIT_LIST_HEAD (&req->queue);
281 } 280 }
@@ -2586,11 +2585,10 @@ omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv)
2586 /* UDC_PULLUP_EN gates the chip clock */ 2585 /* UDC_PULLUP_EN gates the chip clock */
2587 // OTG_SYSCON_1_REG |= DEV_IDLE_EN; 2586 // OTG_SYSCON_1_REG |= DEV_IDLE_EN;
2588 2587
2589 udc = kmalloc (sizeof *udc, SLAB_KERNEL); 2588 udc = kzalloc(sizeof(*udc), SLAB_KERNEL);
2590 if (!udc) 2589 if (!udc)
2591 return -ENOMEM; 2590 return -ENOMEM;
2592 2591
2593 memset(udc, 0, sizeof *udc);
2594 spin_lock_init (&udc->lock); 2592 spin_lock_init (&udc->lock);
2595 2593
2596 udc->gadget.ops = &omap_gadget_ops; 2594 udc->gadget.ops = &omap_gadget_ops;
diff --git a/drivers/usb/gadget/pxa2xx_udc.c b/drivers/usb/gadget/pxa2xx_udc.c
index bb028c5b8952..680f7fc5b171 100644
--- a/drivers/usb/gadget/pxa2xx_udc.c
+++ b/drivers/usb/gadget/pxa2xx_udc.c
@@ -335,11 +335,10 @@ pxa2xx_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
335{ 335{
336 struct pxa2xx_request *req; 336 struct pxa2xx_request *req;
337 337
338 req = kmalloc (sizeof *req, gfp_flags); 338 req = kzalloc(sizeof(*req), gfp_flags);
339 if (!req) 339 if (!req)
340 return NULL; 340 return NULL;
341 341
342 memset (req, 0, sizeof *req);
343 INIT_LIST_HEAD (&req->queue); 342 INIT_LIST_HEAD (&req->queue);
344 return &req->req; 343 return &req->req;
345} 344}
diff --git a/drivers/usb/gadget/serial.c b/drivers/usb/gadget/serial.c
index ba9acd531024..b992546c394d 100644
--- a/drivers/usb/gadget/serial.c
+++ b/drivers/usb/gadget/serial.c
@@ -369,7 +369,7 @@ static struct usb_gadget_driver gs_gadget_driver = {
369#endif /* CONFIG_USB_GADGET_DUALSPEED */ 369#endif /* CONFIG_USB_GADGET_DUALSPEED */
370 .function = GS_LONG_NAME, 370 .function = GS_LONG_NAME,
371 .bind = gs_bind, 371 .bind = gs_bind,
372 .unbind = gs_unbind, 372 .unbind = __exit_p(gs_unbind),
373 .setup = gs_setup, 373 .setup = gs_setup,
374 .disconnect = gs_disconnect, 374 .disconnect = gs_disconnect,
375 .driver = { 375 .driver = {
@@ -1413,7 +1413,7 @@ requeue:
1413 * Called on module load. Allocates and initializes the device 1413 * Called on module load. Allocates and initializes the device
1414 * structure and a control request. 1414 * structure and a control request.
1415 */ 1415 */
1416static int gs_bind(struct usb_gadget *gadget) 1416static int __init gs_bind(struct usb_gadget *gadget)
1417{ 1417{
1418 int ret; 1418 int ret;
1419 struct usb_ep *ep; 1419 struct usb_ep *ep;
@@ -1538,7 +1538,7 @@ autoconf_fail:
1538 * Called on module unload. Frees the control request and device 1538 * Called on module unload. Frees the control request and device
1539 * structure. 1539 * structure.
1540 */ 1540 */
1541static void gs_unbind(struct usb_gadget *gadget) 1541static void __exit gs_unbind(struct usb_gadget *gadget)
1542{ 1542{
1543 struct gs_dev *dev = get_gadget_data(gadget); 1543 struct gs_dev *dev = get_gadget_data(gadget);
1544 1544
@@ -2178,10 +2178,9 @@ static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
2178 return -EIO; 2178 return -EIO;
2179 2179
2180 for (i=0; i<GS_NUM_PORTS; i++) { 2180 for (i=0; i<GS_NUM_PORTS; i++) {
2181 if ((port=(struct gs_port *)kmalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL) 2181 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2182 return -ENOMEM; 2182 return -ENOMEM;
2183 2183
2184 memset(port, 0, sizeof(struct gs_port));
2185 port->port_dev = dev; 2184 port->port_dev = dev;
2186 port->port_num = i; 2185 port->port_num = i;
2187 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE); 2186 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
diff --git a/drivers/usb/gadget/zero.c b/drivers/usb/gadget/zero.c
index ae7a1c0f5748..51424f66a765 100644
--- a/drivers/usb/gadget/zero.c
+++ b/drivers/usb/gadget/zero.c
@@ -1119,7 +1119,7 @@ zero_autoresume (unsigned long _dev)
1119 1119
1120/*-------------------------------------------------------------------------*/ 1120/*-------------------------------------------------------------------------*/
1121 1121
1122static void 1122static void __exit
1123zero_unbind (struct usb_gadget *gadget) 1123zero_unbind (struct usb_gadget *gadget)
1124{ 1124{
1125 struct zero_dev *dev = get_gadget_data (gadget); 1125 struct zero_dev *dev = get_gadget_data (gadget);
@@ -1136,7 +1136,7 @@ zero_unbind (struct usb_gadget *gadget)
1136 set_gadget_data (gadget, NULL); 1136 set_gadget_data (gadget, NULL);
1137} 1137}
1138 1138
1139static int 1139static int __init
1140zero_bind (struct usb_gadget *gadget) 1140zero_bind (struct usb_gadget *gadget)
1141{ 1141{
1142 struct zero_dev *dev; 1142 struct zero_dev *dev;
@@ -1188,10 +1188,9 @@ autoconf_fail:
1188 1188
1189 1189
1190 /* ok, we made sense of the hardware ... */ 1190 /* ok, we made sense of the hardware ... */
1191 dev = kmalloc (sizeof *dev, SLAB_KERNEL); 1191 dev = kzalloc(sizeof(*dev), SLAB_KERNEL);
1192 if (!dev) 1192 if (!dev)
1193 return -ENOMEM; 1193 return -ENOMEM;
1194 memset (dev, 0, sizeof *dev);
1195 spin_lock_init (&dev->lock); 1194 spin_lock_init (&dev->lock);
1196 dev->gadget = gadget; 1195 dev->gadget = gadget;
1197 set_gadget_data (gadget, dev); 1196 set_gadget_data (gadget, dev);
@@ -1224,12 +1223,6 @@ autoconf_fail:
1224 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 1223 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1225 } 1224 }
1226 1225
1227 if (gadget->is_otg) {
1228 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1229 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1230 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1231 }
1232
1233 usb_gadget_set_selfpowered (gadget); 1226 usb_gadget_set_selfpowered (gadget);
1234 1227
1235 init_timer (&dev->resume); 1228 init_timer (&dev->resume);
@@ -1294,7 +1287,7 @@ static struct usb_gadget_driver zero_driver = {
1294#endif 1287#endif
1295 .function = (char *) longname, 1288 .function = (char *) longname,
1296 .bind = zero_bind, 1289 .bind = zero_bind,
1297 .unbind = zero_unbind, 1290 .unbind = __exit_p(zero_unbind),
1298 1291
1299 .setup = zero_setup, 1292 .setup = zero_setup,
1300 .disconnect = zero_disconnect, 1293 .disconnect = zero_disconnect,
diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index be3fd9bce573..e27b79a3c05f 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -6,7 +6,7 @@ comment "USB Host Controller Drivers"
6 6
7config USB_EHCI_HCD 7config USB_EHCI_HCD
8 tristate "EHCI HCD (USB 2.0) support" 8 tristate "EHCI HCD (USB 2.0) support"
9 depends on USB && PCI 9 depends on USB && USB_ARCH_HAS_EHCI
10 ---help--- 10 ---help---
11 The Enhanced Host Controller Interface (EHCI) is standard for USB 2.0 11 The Enhanced Host Controller Interface (EHCI) is standard for USB 2.0
12 "high speed" (480 Mbit/sec, 60 Mbyte/sec) host controller hardware. 12 "high speed" (480 Mbit/sec, 60 Mbyte/sec) host controller hardware.
diff --git a/drivers/usb/host/ehci-au1xxx.c b/drivers/usb/host/ehci-au1xxx.c
new file mode 100644
index 000000000000..63eadeec1324
--- /dev/null
+++ b/drivers/usb/host/ehci-au1xxx.c
@@ -0,0 +1,297 @@
1/*
2 * EHCI HCD (Host Controller Driver) for USB.
3 *
4 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
5 *
6 * Bus Glue for AMD Alchemy Au1xxx
7 *
8 * Based on "ohci-au1xxx.c" by Matt Porter <mporter@kernel.crashing.org>
9 *
10 * Modified for AMD Alchemy Au1200 EHC
11 * by K.Boge <karsten.boge@amd.com>
12 *
13 * This file is licenced under the GPL.
14 */
15
16#include <linux/platform_device.h>
17#include <asm/mach-au1x00/au1000.h>
18
19#ifndef CONFIG_SOC_AU1200
20#error "this Alchemy chip doesn't have EHCI"
21#else /* Au1200 */
22
23#define USB_HOST_CONFIG (USB_MSR_BASE + USB_MSR_MCFG)
24#define USB_MCFG_PFEN (1<<31)
25#define USB_MCFG_RDCOMB (1<<30)
26#define USB_MCFG_SSDEN (1<<23)
27#define USB_MCFG_PHYPLLEN (1<<19)
28#define USB_MCFG_EHCCLKEN (1<<17)
29#define USB_MCFG_UCAM (1<<7)
30#define USB_MCFG_EBMEN (1<<3)
31#define USB_MCFG_EMEMEN (1<<2)
32
33#define USBH_ENABLE_CE (USB_MCFG_PHYPLLEN | USB_MCFG_EHCCLKEN)
34
35#ifdef CONFIG_DMA_COHERENT
36#define USBH_ENABLE_INIT (USBH_ENABLE_CE \
37 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
38 | USB_MCFG_SSDEN | USB_MCFG_UCAM \
39 | USB_MCFG_EBMEN | USB_MCFG_EMEMEN)
40#else
41#define USBH_ENABLE_INIT (USBH_ENABLE_CE \
42 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
43 | USB_MCFG_SSDEN \
44 | USB_MCFG_EBMEN | USB_MCFG_EMEMEN)
45#endif
46#define USBH_DISABLE (USB_MCFG_EBMEN | USB_MCFG_EMEMEN)
47
48#endif /* Au1200 */
49
50extern int usb_disabled(void);
51
52/*-------------------------------------------------------------------------*/
53
54static void au1xxx_start_ehc(struct platform_device *dev)
55{
56 pr_debug(__FILE__ ": starting Au1xxx EHCI USB Controller\n");
57
58 /* write HW defaults again in case Yamon cleared them */
59 if (au_readl(USB_HOST_CONFIG) == 0) {
60 au_writel(0x00d02000, USB_HOST_CONFIG);
61 au_readl(USB_HOST_CONFIG);
62 udelay(1000);
63 }
64 /* enable host controller */
65 au_writel(USBH_ENABLE_CE | au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
66 au_readl(USB_HOST_CONFIG);
67 udelay(1000);
68 au_writel(USBH_ENABLE_INIT | au_readl(USB_HOST_CONFIG),
69 USB_HOST_CONFIG);
70 au_readl(USB_HOST_CONFIG);
71 udelay(1000);
72
73 pr_debug(__FILE__ ": Clock to USB host has been enabled\n");
74}
75
76static void au1xxx_stop_ehc(struct platform_device *dev)
77{
78 pr_debug(__FILE__ ": stopping Au1xxx EHCI USB Controller\n");
79
80 /* Disable mem */
81 au_writel(~USBH_DISABLE & au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
82 udelay(1000);
83 /* Disable clock */
84 au_writel(~USB_MCFG_EHCCLKEN & au_readl(USB_HOST_CONFIG),
85 USB_HOST_CONFIG);
86 au_readl(USB_HOST_CONFIG);
87}
88
89/*-------------------------------------------------------------------------*/
90
91/* configure so an HC device and id are always provided */
92/* always called with process context; sleeping is OK */
93
94/**
95 * usb_ehci_au1xxx_probe - initialize Au1xxx-based HCDs
96 * Context: !in_interrupt()
97 *
98 * Allocates basic resources for this USB host controller, and
99 * then invokes the start() method for the HCD associated with it
100 * through the hotplug entry's driver_data.
101 *
102 */
103int usb_ehci_au1xxx_probe(const struct hc_driver *driver,
104 struct usb_hcd **hcd_out, struct platform_device *dev)
105{
106 int retval;
107 struct usb_hcd *hcd;
108 struct ehci_hcd *ehci;
109
110#if defined(CONFIG_SOC_AU1200) && defined(CONFIG_DMA_COHERENT)
111
112 /* Au1200 AB USB does not support coherent memory */
113 if (!(read_c0_prid() & 0xff)) {
114 pr_info("%s: this is chip revision AB!\n", dev->dev.name);
115 pr_info("%s: update your board or re-configure the kernel\n",
116 dev->dev.name);
117 return -ENODEV;
118 }
119#endif
120
121 au1xxx_start_ehc(dev);
122
123 if (dev->resource[1].flags != IORESOURCE_IRQ) {
124 pr_debug("resource[1] is not IORESOURCE_IRQ");
125 retval = -ENOMEM;
126 }
127 hcd = usb_create_hcd(driver, &dev->dev, "Au1xxx");
128 if (!hcd)
129 return -ENOMEM;
130 hcd->rsrc_start = dev->resource[0].start;
131 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1;
132
133 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
134 pr_debug("request_mem_region failed");
135 retval = -EBUSY;
136 goto err1;
137 }
138
139 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
140 if (!hcd->regs) {
141 pr_debug("ioremap failed");
142 retval = -ENOMEM;
143 goto err2;
144 }
145
146 ehci = hcd_to_ehci(hcd);
147 ehci->caps = hcd->regs;
148 ehci->regs = hcd->regs + HC_LENGTH(readl(&ehci->caps->hc_capbase));
149 /* cache this readonly data; minimize chip reads */
150 ehci->hcs_params = readl(&ehci->caps->hcs_params);
151
152 /* ehci_hcd_init(hcd_to_ehci(hcd)); */
153
154 retval =
155 usb_add_hcd(hcd, dev->resource[1].start, SA_INTERRUPT | SA_SHIRQ);
156 if (retval == 0)
157 return retval;
158
159 au1xxx_stop_ehc(dev);
160 iounmap(hcd->regs);
161err2:
162 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
163err1:
164 usb_put_hcd(hcd);
165 return retval;
166}
167
168/* may be called without controller electrically present */
169/* may be called with controller, bus, and devices active */
170
171/**
172 * usb_ehci_hcd_au1xxx_remove - shutdown processing for Au1xxx-based HCDs
173 * @dev: USB Host Controller being removed
174 * Context: !in_interrupt()
175 *
176 * Reverses the effect of usb_ehci_hcd_au1xxx_probe(), first invoking
177 * the HCD's stop() method. It is always called from a thread
178 * context, normally "rmmod", "apmd", or something similar.
179 *
180 */
181void usb_ehci_au1xxx_remove(struct usb_hcd *hcd, struct platform_device *dev)
182{
183 usb_remove_hcd(hcd);
184 iounmap(hcd->regs);
185 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
186 usb_put_hcd(hcd);
187 au1xxx_stop_ehc(dev);
188}
189
190/*-------------------------------------------------------------------------*/
191
192static const struct hc_driver ehci_au1xxx_hc_driver = {
193 .description = hcd_name,
194 .product_desc = "Au1xxx EHCI",
195 .hcd_priv_size = sizeof(struct ehci_hcd),
196
197 /*
198 * generic hardware linkage
199 */
200 .irq = ehci_irq,
201 .flags = HCD_MEMORY | HCD_USB2,
202
203 /*
204 * basic lifecycle operations
205 */
206 .reset = ehci_init,
207 .start = ehci_run,
208 .stop = ehci_stop,
209
210 /*
211 * managing i/o requests and associated device resources
212 */
213 .urb_enqueue = ehci_urb_enqueue,
214 .urb_dequeue = ehci_urb_dequeue,
215 .endpoint_disable = ehci_endpoint_disable,
216
217 /*
218 * scheduling support
219 */
220 .get_frame_number = ehci_get_frame,
221
222 /*
223 * root hub support
224 */
225 .hub_status_data = ehci_hub_status_data,
226 .hub_control = ehci_hub_control,
227#ifdef CONFIG_PM
228 .hub_suspend = ehci_hub_suspend,
229 .hub_resume = ehci_hub_resume,
230#endif
231};
232
233/*-------------------------------------------------------------------------*/
234
235static int ehci_hcd_au1xxx_drv_probe(struct device *dev)
236{
237 struct platform_device *pdev = to_platform_device(dev);
238 struct usb_hcd *hcd = NULL;
239 int ret;
240
241 pr_debug("In ehci_hcd_au1xxx_drv_probe\n");
242
243 if (usb_disabled())
244 return -ENODEV;
245
246 ret = usb_ehci_au1xxx_probe(&ehci_au1xxx_hc_driver, &hcd, pdev);
247 return ret;
248}
249
250static int ehci_hcd_au1xxx_drv_remove(struct device *dev)
251{
252 struct platform_device *pdev = to_platform_device(dev);
253 struct usb_hcd *hcd = dev_get_drvdata(dev);
254
255 usb_ehci_au1xxx_remove(hcd, pdev);
256 return 0;
257}
258
259 /*TBD*/
260/*static int ehci_hcd_au1xxx_drv_suspend(struct device *dev)
261{
262 struct platform_device *pdev = to_platform_device(dev);
263 struct usb_hcd *hcd = dev_get_drvdata(dev);
264
265 return 0;
266}
267static int ehci_hcd_au1xxx_drv_resume(struct device *dev)
268{
269 struct platform_device *pdev = to_platform_device(dev);
270 struct usb_hcd *hcd = dev_get_drvdata(dev);
271
272 return 0;
273}
274*/
275static struct device_driver ehci_hcd_au1xxx_driver = {
276 .name = "au1xxx-ehci",
277 .bus = &platform_bus_type,
278 .probe = ehci_hcd_au1xxx_drv_probe,
279 .remove = ehci_hcd_au1xxx_drv_remove,
280 /*.suspend = ehci_hcd_au1xxx_drv_suspend, */
281 /*.resume = ehci_hcd_au1xxx_drv_resume, */
282};
283
284static int __init ehci_hcd_au1xxx_init(void)
285{
286 pr_debug(DRIVER_INFO " (Au1xxx)\n");
287
288 return driver_register(&ehci_hcd_au1xxx_driver);
289}
290
291static void __exit ehci_hcd_au1xxx_cleanup(void)
292{
293 driver_unregister(&ehci_hcd_au1xxx_driver);
294}
295
296module_init(ehci_hcd_au1xxx_init);
297module_exit(ehci_hcd_au1xxx_cleanup);
diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
new file mode 100644
index 000000000000..f985f121a245
--- /dev/null
+++ b/drivers/usb/host/ehci-fsl.c
@@ -0,0 +1,366 @@
1/*
2 * (C) Copyright David Brownell 2000-2002
3 * Copyright (c) 2005 MontaVista Software
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * Ported to 834x by Randy Vinson <rvinson@mvista.com> using code provided
20 * by Hunter Wu.
21 */
22
23#include <linux/platform_device.h>
24#include <linux/fsl_devices.h>
25
26#include "ehci-fsl.h"
27
28/* FIXME: Power Managment is un-ported so temporarily disable it */
29#undef CONFIG_PM
30
31/* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
32
33/* configure so an HC device and id are always provided */
34/* always called with process context; sleeping is OK */
35
36/**
37 * usb_hcd_fsl_probe - initialize FSL-based HCDs
38 * @drvier: Driver to be used for this HCD
39 * @pdev: USB Host Controller being probed
40 * Context: !in_interrupt()
41 *
42 * Allocates basic resources for this USB host controller.
43 *
44 */
45int usb_hcd_fsl_probe(const struct hc_driver *driver,
46 struct platform_device *pdev)
47{
48 struct fsl_usb2_platform_data *pdata;
49 struct usb_hcd *hcd;
50 struct resource *res;
51 int irq;
52 int retval;
53 unsigned int temp;
54
55 pr_debug("initializing FSL-SOC USB Controller\n");
56
57 /* Need platform data for setup */
58 pdata = (struct fsl_usb2_platform_data *)pdev->dev.platform_data;
59 if (!pdata) {
60 dev_err(&pdev->dev,
61 "No platform data for %s.\n", pdev->dev.bus_id);
62 return -ENODEV;
63 }
64
65 /*
66 * This is a host mode driver, verify that we're supposed to be
67 * in host mode.
68 */
69 if (!((pdata->operating_mode == FSL_USB2_DR_HOST) ||
70 (pdata->operating_mode == FSL_USB2_MPH_HOST))) {
71 dev_err(&pdev->dev,
72 "Non Host Mode configured for %s. Wrong driver linked.\n",
73 pdev->dev.bus_id);
74 return -ENODEV;
75 }
76
77 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
78 if (!res) {
79 dev_err(&pdev->dev,
80 "Found HC with no IRQ. Check %s setup!\n",
81 pdev->dev.bus_id);
82 return -ENODEV;
83 }
84 irq = res->start;
85
86 hcd = usb_create_hcd(driver, &pdev->dev, pdev->dev.bus_id);
87 if (!hcd) {
88 retval = -ENOMEM;
89 goto err1;
90 }
91
92 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
93 if (!res) {
94 dev_err(&pdev->dev,
95 "Found HC with no register addr. Check %s setup!\n",
96 pdev->dev.bus_id);
97 retval = -ENODEV;
98 goto err2;
99 }
100 hcd->rsrc_start = res->start;
101 hcd->rsrc_len = res->end - res->start + 1;
102 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
103 driver->description)) {
104 dev_dbg(&pdev->dev, "controller already in use\n");
105 retval = -EBUSY;
106 goto err2;
107 }
108 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
109
110 if (hcd->regs == NULL) {
111 dev_dbg(&pdev->dev, "error mapping memory\n");
112 retval = -EFAULT;
113 goto err3;
114 }
115
116 /* Enable USB controller */
117 temp = in_be32(hcd->regs + 0x500);
118 out_be32(hcd->regs + 0x500, temp | 0x4);
119
120 /* Set to Host mode */
121 temp = in_le32(hcd->regs + 0x1a8);
122 out_le32(hcd->regs + 0x1a8, temp | 0x3);
123
124 retval = usb_add_hcd(hcd, irq, SA_SHIRQ);
125 if (retval != 0)
126 goto err4;
127 return retval;
128
129 err4:
130 iounmap(hcd->regs);
131 err3:
132 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
133 err2:
134 usb_put_hcd(hcd);
135 err1:
136 dev_err(&pdev->dev, "init %s fail, %d\n", pdev->dev.bus_id, retval);
137 return retval;
138}
139
140/* may be called without controller electrically present */
141/* may be called with controller, bus, and devices active */
142
143/**
144 * usb_hcd_fsl_remove - shutdown processing for FSL-based HCDs
145 * @dev: USB Host Controller being removed
146 * Context: !in_interrupt()
147 *
148 * Reverses the effect of usb_hcd_fsl_probe().
149 *
150 */
151void usb_hcd_fsl_remove(struct usb_hcd *hcd, struct platform_device *pdev)
152{
153 usb_remove_hcd(hcd);
154 iounmap(hcd->regs);
155 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
156 usb_put_hcd(hcd);
157}
158
159static void mpc83xx_setup_phy(struct ehci_hcd *ehci,
160 enum fsl_usb2_phy_modes phy_mode,
161 unsigned int port_offset)
162{
163 u32 portsc = 0;
164 switch (phy_mode) {
165 case FSL_USB2_PHY_ULPI:
166 portsc |= PORT_PTS_ULPI;
167 break;
168 case FSL_USB2_PHY_SERIAL:
169 portsc |= PORT_PTS_SERIAL;
170 break;
171 case FSL_USB2_PHY_UTMI_WIDE:
172 portsc |= PORT_PTS_PTW;
173 /* fall through */
174 case FSL_USB2_PHY_UTMI:
175 portsc |= PORT_PTS_UTMI;
176 break;
177 case FSL_USB2_PHY_NONE:
178 break;
179 }
180 writel(portsc, &ehci->regs->port_status[port_offset]);
181}
182
183static void mpc83xx_usb_setup(struct usb_hcd *hcd)
184{
185 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
186 struct fsl_usb2_platform_data *pdata;
187 void __iomem *non_ehci = hcd->regs;
188
189 pdata =
190 (struct fsl_usb2_platform_data *)hcd->self.controller->
191 platform_data;
192 /* Enable PHY interface in the control reg. */
193 out_be32(non_ehci + FSL_SOC_USB_CTRL, 0x00000004);
194 out_be32(non_ehci + FSL_SOC_USB_SNOOP1, 0x0000001b);
195
196 if (pdata->operating_mode == FSL_USB2_DR_HOST)
197 mpc83xx_setup_phy(ehci, pdata->phy_mode, 0);
198
199 if (pdata->operating_mode == FSL_USB2_MPH_HOST) {
200 unsigned int chip, rev, svr;
201
202 svr = mfspr(SPRN_SVR);
203 chip = svr >> 16;
204 rev = (svr >> 4) & 0xf;
205
206 /* Deal with USB Erratum #14 on MPC834x Rev 1.0 & 1.1 chips */
207 if ((rev == 1) && (chip >= 0x8050) && (chip <= 0x8055))
208 ehci->has_fsl_port_bug = 1;
209
210 if (pdata->port_enables & FSL_USB2_PORT0_ENABLED)
211 mpc83xx_setup_phy(ehci, pdata->phy_mode, 0);
212 if (pdata->port_enables & FSL_USB2_PORT1_ENABLED)
213 mpc83xx_setup_phy(ehci, pdata->phy_mode, 1);
214 }
215
216 /* put controller in host mode. */
217 writel(0x00000003, non_ehci + FSL_SOC_USB_USBMODE);
218 out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x0000000c);
219 out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000040);
220 out_be32(non_ehci + FSL_SOC_USB_SICTRL, 0x00000001);
221}
222
223/* called after powerup, by probe or system-pm "wakeup" */
224static int ehci_fsl_reinit(struct ehci_hcd *ehci)
225{
226 mpc83xx_usb_setup(ehci_to_hcd(ehci));
227 ehci_port_power(ehci, 0);
228
229 return 0;
230}
231
232/* called during probe() after chip reset completes */
233static int ehci_fsl_setup(struct usb_hcd *hcd)
234{
235 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
236 int retval;
237
238 /* EHCI registers start at offset 0x100 */
239 ehci->caps = hcd->regs + 0x100;
240 ehci->regs = hcd->regs + 0x100 +
241 HC_LENGTH(readl(&ehci->caps->hc_capbase));
242 dbg_hcs_params(ehci, "reset");
243 dbg_hcc_params(ehci, "reset");
244
245 /* cache this readonly data; minimize chip reads */
246 ehci->hcs_params = readl(&ehci->caps->hcs_params);
247
248 retval = ehci_halt(ehci);
249 if (retval)
250 return retval;
251
252 /* data structure init */
253 retval = ehci_init(hcd);
254 if (retval)
255 return retval;
256
257 ehci->is_tdi_rh_tt = 1;
258
259 ehci->sbrn = 0x20;
260
261 ehci_reset(ehci);
262
263 retval = ehci_fsl_reinit(ehci);
264 return retval;
265}
266
267static const struct hc_driver ehci_fsl_hc_driver = {
268 .description = hcd_name,
269 .product_desc = "Freescale On-Chip EHCI Host Controller",
270 .hcd_priv_size = sizeof(struct ehci_hcd),
271
272 /*
273 * generic hardware linkage
274 */
275 .irq = ehci_irq,
276 .flags = HCD_USB2,
277
278 /*
279 * basic lifecycle operations
280 */
281 .reset = ehci_fsl_setup,
282 .start = ehci_run,
283#ifdef CONFIG_PM
284 .suspend = ehci_bus_suspend,
285 .resume = ehci_bus_resume,
286#endif
287 .stop = ehci_stop,
288
289 /*
290 * managing i/o requests and associated device resources
291 */
292 .urb_enqueue = ehci_urb_enqueue,
293 .urb_dequeue = ehci_urb_dequeue,
294 .endpoint_disable = ehci_endpoint_disable,
295
296 /*
297 * scheduling support
298 */
299 .get_frame_number = ehci_get_frame,
300
301 /*
302 * root hub support
303 */
304 .hub_status_data = ehci_hub_status_data,
305 .hub_control = ehci_hub_control,
306 .bus_suspend = ehci_bus_suspend,
307 .bus_resume = ehci_bus_resume,
308};
309
310static int ehci_fsl_drv_probe(struct platform_device *pdev)
311{
312 if (usb_disabled())
313 return -ENODEV;
314
315 return usb_hcd_fsl_probe(&ehci_fsl_hc_driver, pdev);
316}
317
318static int ehci_fsl_drv_remove(struct platform_device *pdev)
319{
320 struct usb_hcd *hcd = platform_get_drvdata(pdev);
321
322 usb_hcd_fsl_remove(hcd, pdev);
323
324 return 0;
325}
326
327static struct platform_driver ehci_fsl_dr_driver = {
328 .probe = ehci_fsl_drv_probe,
329 .remove = ehci_fsl_drv_remove,
330 .driver = {
331 .name = "fsl-usb2-dr",
332 },
333};
334
335static struct platform_driver ehci_fsl_mph_driver = {
336 .probe = ehci_fsl_drv_probe,
337 .remove = ehci_fsl_drv_remove,
338 .driver = {
339 .name = "fsl-usb2-mph",
340 },
341};
342
343static int __init ehci_fsl_init(void)
344{
345 int retval;
346
347 pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
348 hcd_name,
349 sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
350 sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
351
352 retval = platform_driver_register(&ehci_fsl_dr_driver);
353 if (retval)
354 return retval;
355
356 return platform_driver_register(&ehci_fsl_mph_driver);
357}
358
359static void __exit ehci_fsl_cleanup(void)
360{
361 platform_driver_unregister(&ehci_fsl_mph_driver);
362 platform_driver_unregister(&ehci_fsl_dr_driver);
363}
364
365module_init(ehci_fsl_init);
366module_exit(ehci_fsl_cleanup);
diff --git a/drivers/usb/host/ehci-fsl.h b/drivers/usb/host/ehci-fsl.h
new file mode 100644
index 000000000000..caac0d1967d0
--- /dev/null
+++ b/drivers/usb/host/ehci-fsl.h
@@ -0,0 +1,37 @@
1/* Copyright (c) 2005 freescale semiconductor
2 * Copyright (c) 2005 MontaVista Software
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18#ifndef _EHCI_FSL_H
19#define _EHCI_FSL_H
20
21/* offsets for the non-ehci registers in the FSL SOC USB controller */
22#define FSL_SOC_USB_ULPIVP 0x170
23#define FSL_SOC_USB_PORTSC1 0x184
24#define PORT_PTS_MSK (3<<30)
25#define PORT_PTS_UTMI (0<<30)
26#define PORT_PTS_ULPI (2<<30)
27#define PORT_PTS_SERIAL (3<<30)
28#define PORT_PTS_PTW (1<<28)
29#define FSL_SOC_USB_PORTSC2 0x188
30#define FSL_SOC_USB_USBMODE 0x1a8
31#define FSL_SOC_USB_SNOOP1 0x400 /* NOTE: big-endian */
32#define FSL_SOC_USB_SNOOP2 0x404 /* NOTE: big-endian */
33#define FSL_SOC_USB_AGECNTTHRSH 0x408 /* NOTE: big-endian */
34#define FSL_SOC_USB_SICTRL 0x40c /* NOTE: big-endian */
35#define FSL_SOC_USB_PRICTRL 0x410 /* NOTE: big-endian */
36#define FSL_SOC_USB_CTRL 0x500 /* NOTE: big-endian */
37#endif /* _EHCI_FSL_H */
diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index 9dd3d14c64f3..79f2d8b9bfb6 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -889,8 +889,19 @@ MODULE_LICENSE ("GPL");
889 889
890#ifdef CONFIG_PCI 890#ifdef CONFIG_PCI
891#include "ehci-pci.c" 891#include "ehci-pci.c"
892#define EHCI_BUS_GLUED
892#endif 893#endif
893 894
894#if !defined(CONFIG_PCI) 895#ifdef CONFIG_PPC_83xx
896#include "ehci-fsl.c"
897#define EHCI_BUS_GLUED
898#endif
899
900#ifdef CONFIG_SOC_AU1X00
901#include "ehci-au1xxx.c"
902#define EHCI_BUS_GLUED
903#endif
904
905#ifndef EHCI_BUS_GLUED
895#error "missing bus glue for ehci-hcd" 906#error "missing bus glue for ehci-hcd"
896#endif 907#endif
diff --git a/drivers/usb/host/ehci-hub.c b/drivers/usb/host/ehci-hub.c
index 69b0b9be7a64..d03e3cad5ca8 100644
--- a/drivers/usb/host/ehci-hub.c
+++ b/drivers/usb/host/ehci-hub.c
@@ -359,6 +359,8 @@ static int ehci_hub_control (
359 case USB_PORT_FEAT_SUSPEND: 359 case USB_PORT_FEAT_SUSPEND:
360 if (temp & PORT_RESET) 360 if (temp & PORT_RESET)
361 goto error; 361 goto error;
362 if (ehci->no_selective_suspend)
363 break;
362 if (temp & PORT_SUSPEND) { 364 if (temp & PORT_SUSPEND) {
363 if ((temp & PORT_PE) == 0) 365 if ((temp & PORT_PE) == 0)
364 goto error; 366 goto error;
@@ -514,6 +516,8 @@ static int ehci_hub_control (
514 temp &= ~PORT_RWC_BITS; 516 temp &= ~PORT_RWC_BITS;
515 switch (wValue) { 517 switch (wValue) {
516 case USB_PORT_FEAT_SUSPEND: 518 case USB_PORT_FEAT_SUSPEND:
519 if (ehci->no_selective_suspend)
520 break;
517 if ((temp & PORT_PE) == 0 521 if ((temp & PORT_PE) == 0
518 || (temp & PORT_RESET) != 0) 522 || (temp & PORT_RESET) != 0)
519 goto error; 523 goto error;
diff --git a/drivers/usb/host/ehci-mem.c b/drivers/usb/host/ehci-mem.c
index 91c2ab43cbcc..766061e0260a 100644
--- a/drivers/usb/host/ehci-mem.c
+++ b/drivers/usb/host/ehci-mem.c
@@ -75,7 +75,6 @@ static void qh_destroy (struct kref *kref)
75 } 75 }
76 if (qh->dummy) 76 if (qh->dummy)
77 ehci_qtd_free (ehci, qh->dummy); 77 ehci_qtd_free (ehci, qh->dummy);
78 usb_put_dev (qh->dev);
79 dma_pool_free (ehci->qh_pool, qh, qh->qh_dma); 78 dma_pool_free (ehci->qh_pool, qh, qh->qh_dma);
80} 79}
81 80
@@ -221,13 +220,9 @@ static int ehci_mem_init (struct ehci_hcd *ehci, gfp_t flags)
221 ehci->periodic [i] = EHCI_LIST_END; 220 ehci->periodic [i] = EHCI_LIST_END;
222 221
223 /* software shadow of hardware table */ 222 /* software shadow of hardware table */
224 ehci->pshadow = kmalloc (ehci->periodic_size * sizeof (void *), flags); 223 ehci->pshadow = kcalloc(ehci->periodic_size, sizeof(void *), flags);
225 if (ehci->pshadow == NULL) { 224 if (ehci->pshadow != NULL)
226 goto fail; 225 return 0;
227 }
228 memset (ehci->pshadow, 0, ehci->periodic_size * sizeof (void *));
229
230 return 0;
231 226
232fail: 227fail:
233 ehci_dbg (ehci, "couldn't init memory\n"); 228 ehci_dbg (ehci, "couldn't init memory\n");
diff --git a/drivers/usb/host/ehci-pci.c b/drivers/usb/host/ehci-pci.c
index 3a6687df5594..1e03f1a5a5fd 100644
--- a/drivers/usb/host/ehci-pci.c
+++ b/drivers/usb/host/ehci-pci.c
@@ -106,11 +106,11 @@ static int ehci_pci_setup(struct usb_hcd *hcd)
106 } 106 }
107 break; 107 break;
108 case PCI_VENDOR_ID_NVIDIA: 108 case PCI_VENDOR_ID_NVIDIA:
109 switch (pdev->device) {
109 /* NVidia reports that certain chips don't handle 110 /* NVidia reports that certain chips don't handle
110 * QH, ITD, or SITD addresses above 2GB. (But TD, 111 * QH, ITD, or SITD addresses above 2GB. (But TD,
111 * data buffer, and periodic schedule are normal.) 112 * data buffer, and periodic schedule are normal.)
112 */ 113 */
113 switch (pdev->device) {
114 case 0x003c: /* MCP04 */ 114 case 0x003c: /* MCP04 */
115 case 0x005b: /* CK804 */ 115 case 0x005b: /* CK804 */
116 case 0x00d8: /* CK8 */ 116 case 0x00d8: /* CK8 */
@@ -120,6 +120,14 @@ static int ehci_pci_setup(struct usb_hcd *hcd)
120 ehci_warn(ehci, "can't enable NVidia " 120 ehci_warn(ehci, "can't enable NVidia "
121 "workaround for >2GB RAM\n"); 121 "workaround for >2GB RAM\n");
122 break; 122 break;
123 /* Some NForce2 chips have problems with selective suspend;
124 * fixed in newer silicon.
125 */
126 case 0x0068:
127 pci_read_config_dword(pdev, PCI_REVISION_ID, &temp);
128 if ((temp & 0xff) < 0xa4)
129 ehci->no_selective_suspend = 1;
130 break;
123 } 131 }
124 break; 132 break;
125 } 133 }
@@ -163,6 +171,21 @@ static int ehci_pci_setup(struct usb_hcd *hcd)
163 device_init_wakeup(&pdev->dev, 1); 171 device_init_wakeup(&pdev->dev, 1);
164 } 172 }
165 173
174#ifdef CONFIG_USB_SUSPEND
175 /* REVISIT: the controller works fine for wakeup iff the root hub
176 * itself is "globally" suspended, but usbcore currently doesn't
177 * understand such things.
178 *
179 * System suspend currently expects to be able to suspend the entire
180 * device tree, device-at-a-time. If we failed selective suspend
181 * reports, system suspend would fail; so the root hub code must claim
182 * success. That's lying to usbcore, and it matters for for runtime
183 * PM scenarios with selective suspend and remote wakeup...
184 */
185 if (ehci->no_selective_suspend && device_can_wakeup(&pdev->dev))
186 ehci_warn(ehci, "selective suspend/wakeup unavailable\n");
187#endif
188
166 retval = ehci_pci_reinit(ehci, pdev); 189 retval = ehci_pci_reinit(ehci, pdev);
167done: 190done:
168 return retval; 191 return retval;
diff --git a/drivers/usb/host/ehci-q.c b/drivers/usb/host/ehci-q.c
index 9b13bf2fa98d..e469221e7ec3 100644
--- a/drivers/usb/host/ehci-q.c
+++ b/drivers/usb/host/ehci-q.c
@@ -702,7 +702,7 @@ qh_make (
702 } 702 }
703 703
704 /* support for tt scheduling, and access to toggles */ 704 /* support for tt scheduling, and access to toggles */
705 qh->dev = usb_get_dev (urb->dev); 705 qh->dev = urb->dev;
706 706
707 /* using TT? */ 707 /* using TT? */
708 switch (urb->dev->speed) { 708 switch (urb->dev->speed) {
@@ -721,7 +721,14 @@ qh_make (
721 info1 |= maxp << 16; 721 info1 |= maxp << 16;
722 722
723 info2 |= (EHCI_TUNE_MULT_TT << 30); 723 info2 |= (EHCI_TUNE_MULT_TT << 30);
724 info2 |= urb->dev->ttport << 23; 724
725 /* Some Freescale processors have an erratum in which the
726 * port number in the queue head was 0..N-1 instead of 1..N.
727 */
728 if (ehci_has_fsl_portno_bug(ehci))
729 info2 |= (urb->dev->ttport-1) << 23;
730 else
731 info2 |= urb->dev->ttport << 23;
725 732
726 /* set the address of the TT; for TDI's integrated 733 /* set the address of the TT; for TDI's integrated
727 * root hub tt, leave it zeroed. 734 * root hub tt, leave it zeroed.
@@ -1015,12 +1022,14 @@ static void start_unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
1015 /* stop async schedule right now? */ 1022 /* stop async schedule right now? */
1016 if (unlikely (qh == ehci->async)) { 1023 if (unlikely (qh == ehci->async)) {
1017 /* can't get here without STS_ASS set */ 1024 /* can't get here without STS_ASS set */
1018 if (ehci_to_hcd(ehci)->state != HC_STATE_HALT) { 1025 if (ehci_to_hcd(ehci)->state != HC_STATE_HALT
1026 && !ehci->reclaim) {
1027 /* ... and CMD_IAAD clear */
1019 writel (cmd & ~CMD_ASE, &ehci->regs->command); 1028 writel (cmd & ~CMD_ASE, &ehci->regs->command);
1020 wmb (); 1029 wmb ();
1021 // handshake later, if we need to 1030 // handshake later, if we need to
1031 timer_action_done (ehci, TIMER_ASYNC_OFF);
1022 } 1032 }
1023 timer_action_done (ehci, TIMER_ASYNC_OFF);
1024 return; 1033 return;
1025 } 1034 }
1026 1035
diff --git a/drivers/usb/host/ehci-sched.c b/drivers/usb/host/ehci-sched.c
index ebcca9700671..5871944e6145 100644
--- a/drivers/usb/host/ehci-sched.c
+++ b/drivers/usb/host/ehci-sched.c
@@ -707,6 +707,7 @@ iso_stream_init (
707 } else { 707 } else {
708 u32 addr; 708 u32 addr;
709 int think_time; 709 int think_time;
710 int hs_transfers;
710 711
711 addr = dev->ttport << 24; 712 addr = dev->ttport << 24;
712 if (!ehci_is_TDI(ehci) 713 if (!ehci_is_TDI(ehci)
@@ -719,6 +720,7 @@ iso_stream_init (
719 think_time = dev->tt ? dev->tt->think_time : 0; 720 think_time = dev->tt ? dev->tt->think_time : 0;
720 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time ( 721 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
721 dev->speed, is_input, 1, maxp)); 722 dev->speed, is_input, 1, maxp));
723 hs_transfers = max (1u, (maxp + 187) / 188);
722 if (is_input) { 724 if (is_input) {
723 u32 tmp; 725 u32 tmp;
724 726
@@ -727,12 +729,11 @@ iso_stream_init (
727 stream->usecs = HS_USECS_ISO (1); 729 stream->usecs = HS_USECS_ISO (1);
728 stream->raw_mask = 1; 730 stream->raw_mask = 1;
729 731
730 /* pessimistic c-mask */ 732 /* c-mask as specified in USB 2.0 11.18.4 3.c */
731 tmp = usb_calc_bus_time (USB_SPEED_FULL, 1, 0, maxp) 733 tmp = (1 << (hs_transfers + 2)) - 1;
732 / (125 * 1000); 734 stream->raw_mask |= tmp << (8 + 2);
733 stream->raw_mask |= 3 << (tmp + 9);
734 } else 735 } else
735 stream->raw_mask = smask_out [maxp / 188]; 736 stream->raw_mask = smask_out [hs_transfers - 1];
736 bandwidth = stream->usecs + stream->c_usecs; 737 bandwidth = stream->usecs + stream->c_usecs;
737 bandwidth /= 1 << (interval + 2); 738 bandwidth /= 1 << (interval + 2);
738 739
@@ -863,9 +864,8 @@ iso_sched_alloc (unsigned packets, gfp_t mem_flags)
863 int size = sizeof *iso_sched; 864 int size = sizeof *iso_sched;
864 865
865 size += packets * sizeof (struct ehci_iso_packet); 866 size += packets * sizeof (struct ehci_iso_packet);
866 iso_sched = kmalloc (size, mem_flags); 867 iso_sched = kzalloc(size, mem_flags);
867 if (likely (iso_sched != NULL)) { 868 if (likely (iso_sched != NULL)) {
868 memset(iso_sched, 0, size);
869 INIT_LIST_HEAD (&iso_sched->td_list); 869 INIT_LIST_HEAD (&iso_sched->td_list);
870 } 870 }
871 return iso_sched; 871 return iso_sched;
@@ -1398,7 +1398,7 @@ itd_complete (
1398 */ 1398 */
1399 1399
1400 /* give urb back to the driver ... can be out-of-order */ 1400 /* give urb back to the driver ... can be out-of-order */
1401 dev = usb_get_dev (urb->dev); 1401 dev = urb->dev;
1402 ehci_urb_done (ehci, urb, regs); 1402 ehci_urb_done (ehci, urb, regs);
1403 urb = NULL; 1403 urb = NULL;
1404 1404
@@ -1417,7 +1417,6 @@ itd_complete (
1417 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out"); 1417 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1418 } 1418 }
1419 iso_stream_put (ehci, stream); 1419 iso_stream_put (ehci, stream);
1420 usb_put_dev (dev);
1421 1420
1422 return 1; 1421 return 1;
1423} 1422}
@@ -1764,7 +1763,7 @@ sitd_complete (
1764 */ 1763 */
1765 1764
1766 /* give urb back to the driver */ 1765 /* give urb back to the driver */
1767 dev = usb_get_dev (urb->dev); 1766 dev = urb->dev;
1768 ehci_urb_done (ehci, urb, regs); 1767 ehci_urb_done (ehci, urb, regs);
1769 urb = NULL; 1768 urb = NULL;
1770 1769
@@ -1783,7 +1782,6 @@ sitd_complete (
1783 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out"); 1782 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1784 } 1783 }
1785 iso_stream_put (ehci, stream); 1784 iso_stream_put (ehci, stream);
1786 usb_put_dev (dev);
1787 1785
1788 return 1; 1786 return 1;
1789} 1787}
diff --git a/drivers/usb/host/ehci.h b/drivers/usb/host/ehci.h
index 18e257c2bdb5..679c1cdcc915 100644
--- a/drivers/usb/host/ehci.h
+++ b/drivers/usb/host/ehci.h
@@ -88,7 +88,12 @@ struct ehci_hcd { /* one per controller */
88 unsigned long next_statechange; 88 unsigned long next_statechange;
89 u32 command; 89 u32 command;
90 90
91 /* SILICON QUIRKS */
91 unsigned is_tdi_rh_tt:1; /* TDI roothub with TT */ 92 unsigned is_tdi_rh_tt:1; /* TDI roothub with TT */
93 unsigned no_selective_suspend:1;
94 unsigned has_fsl_port_bug:1; /* FreeScale */
95
96 u8 sbrn; /* packed release number */
92 97
93 /* irq statistics */ 98 /* irq statistics */
94#ifdef EHCI_STATS 99#ifdef EHCI_STATS
@@ -97,7 +102,6 @@ struct ehci_hcd { /* one per controller */
97#else 102#else
98# define COUNT(x) do {} while (0) 103# define COUNT(x) do {} while (0)
99#endif 104#endif
100 u8 sbrn; /* packed release number */
101}; 105};
102 106
103/* convert between an HCD pointer and the corresponding EHCI_HCD */ 107/* convert between an HCD pointer and the corresponding EHCI_HCD */
@@ -638,6 +642,18 @@ ehci_port_speed(struct ehci_hcd *ehci, unsigned int portsc)
638 642
639/*-------------------------------------------------------------------------*/ 643/*-------------------------------------------------------------------------*/
640 644
645#ifdef CONFIG_PPC_83xx
646/* Some Freescale processors have an erratum in which the TT
647 * port number in the queue head was 0..N-1 instead of 1..N.
648 */
649#define ehci_has_fsl_portno_bug(e) ((e)->has_fsl_port_bug)
650#else
651#define ehci_has_fsl_portno_bug(e) (0)
652#endif
653
654
655/*-------------------------------------------------------------------------*/
656
641#ifndef DEBUG 657#ifndef DEBUG
642#define STUB_DEBUG_FILES 658#define STUB_DEBUG_FILES
643#endif /* DEBUG */ 659#endif /* DEBUG */
diff --git a/drivers/usb/host/hc_crisv10.c b/drivers/usb/host/hc_crisv10.c
index 641268d7e6f3..2fe7fd19437b 100644
--- a/drivers/usb/host/hc_crisv10.c
+++ b/drivers/usb/host/hc_crisv10.c
@@ -2137,10 +2137,9 @@ static int etrax_usb_submit_bulk_urb(struct urb *urb)
2137 urb->status = -EINPROGRESS; 2137 urb->status = -EINPROGRESS;
2138 2138
2139 /* Setup the hcpriv data. */ 2139 /* Setup the hcpriv data. */
2140 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG); 2140 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2141 assert(urb_priv != NULL); 2141 assert(urb_priv != NULL);
2142 /* This sets rx_offset to 0. */ 2142 /* This sets rx_offset to 0. */
2143 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2144 urb_priv->urb_state = NOT_STARTED; 2143 urb_priv->urb_state = NOT_STARTED;
2145 urb->hcpriv = urb_priv; 2144 urb->hcpriv = urb_priv;
2146 2145
@@ -2475,10 +2474,9 @@ static int etrax_usb_submit_ctrl_urb(struct urb *urb)
2475 urb->status = -EINPROGRESS; 2474 urb->status = -EINPROGRESS;
2476 2475
2477 /* Setup the hcpriv data. */ 2476 /* Setup the hcpriv data. */
2478 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG); 2477 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2479 assert(urb_priv != NULL); 2478 assert(urb_priv != NULL);
2480 /* This sets rx_offset to 0. */ 2479 /* This sets rx_offset to 0. */
2481 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2482 urb_priv->urb_state = NOT_STARTED; 2480 urb_priv->urb_state = NOT_STARTED;
2483 urb->hcpriv = urb_priv; 2481 urb->hcpriv = urb_priv;
2484 2482
@@ -2767,9 +2765,8 @@ static void etrax_usb_add_to_intr_sb_list(struct urb *urb, int epid)
2767 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)); 2765 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2768 interval = urb->interval; 2766 interval = urb->interval;
2769 2767
2770 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG); 2768 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2771 assert(urb_priv != NULL); 2769 assert(urb_priv != NULL);
2772 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2773 urb->hcpriv = urb_priv; 2770 urb->hcpriv = urb_priv;
2774 2771
2775 first_ep = &TxIntrEPList[0]; 2772 first_ep = &TxIntrEPList[0];
@@ -2997,9 +2994,8 @@ static void etrax_usb_add_to_isoc_sb_list(struct urb *urb, int epid)
2997 2994
2998 prev_sb_desc = next_sb_desc = temp_sb_desc = NULL; 2995 prev_sb_desc = next_sb_desc = temp_sb_desc = NULL;
2999 2996
3000 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), GFP_ATOMIC); 2997 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), GFP_ATOMIC);
3001 assert(urb_priv != NULL); 2998 assert(urb_priv != NULL);
3002 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
3003 2999
3004 urb->hcpriv = urb_priv; 3000 urb->hcpriv = urb_priv;
3005 urb_priv->epid = epid; 3001 urb_priv->epid = epid;
diff --git a/drivers/usb/host/isp116x-hcd.c b/drivers/usb/host/isp116x-hcd.c
index 972ce04889f8..e99210b7909b 100644
--- a/drivers/usb/host/isp116x-hcd.c
+++ b/drivers/usb/host/isp116x-hcd.c
@@ -724,7 +724,7 @@ static int isp116x_urb_enqueue(struct usb_hcd *hcd,
724 ep = hep->hcpriv; 724 ep = hep->hcpriv;
725 else { 725 else {
726 INIT_LIST_HEAD(&ep->schedule); 726 INIT_LIST_HEAD(&ep->schedule);
727 ep->udev = usb_get_dev(udev); 727 ep->udev = udev;
728 ep->epnum = epnum; 728 ep->epnum = epnum;
729 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out); 729 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
730 usb_settoggle(udev, epnum, is_out, 0); 730 usb_settoggle(udev, epnum, is_out, 0);
@@ -891,7 +891,6 @@ static void isp116x_endpoint_disable(struct usb_hcd *hcd,
891 if (!list_empty(&hep->urb_list)) 891 if (!list_empty(&hep->urb_list))
892 WARN("ep %p not empty?\n", ep); 892 WARN("ep %p not empty?\n", ep);
893 893
894 usb_put_dev(ep->udev);
895 kfree(ep); 894 kfree(ep);
896 hep->hcpriv = NULL; 895 hep->hcpriv = NULL;
897} 896}
@@ -1553,7 +1552,7 @@ static struct hc_driver isp116x_hc_driver = {
1553 1552
1554/*----------------------------------------------------------------*/ 1553/*----------------------------------------------------------------*/
1555 1554
1556static int __init_or_module isp116x_remove(struct platform_device *pdev) 1555static int isp116x_remove(struct platform_device *pdev)
1557{ 1556{
1558 struct usb_hcd *hcd = platform_get_drvdata(pdev); 1557 struct usb_hcd *hcd = platform_get_drvdata(pdev);
1559 struct isp116x *isp116x; 1558 struct isp116x *isp116x;
diff --git a/drivers/usb/host/ohci-at91.c b/drivers/usb/host/ohci-at91.c
new file mode 100644
index 000000000000..980030d684d5
--- /dev/null
+++ b/drivers/usb/host/ohci-at91.c
@@ -0,0 +1,306 @@
1/*
2 * OHCI HCD (Host Controller Driver) for USB.
3 *
4 * Copyright (C) 2004 SAN People (Pty) Ltd.
5 * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
6 *
7 * AT91RM9200 Bus Glue
8 *
9 * Based on fragments of 2.4 driver by Rick Bronson.
10 * Based on ohci-omap.c
11 *
12 * This file is licenced under the GPL.
13 */
14
15#include <linux/clk.h>
16#include <linux/platform_device.h>
17
18#include <asm/mach-types.h>
19#include <asm/hardware.h>
20#include <asm/arch/board.h>
21
22#ifndef CONFIG_ARCH_AT91RM9200
23#error "This file is AT91RM9200 bus glue. CONFIG_ARCH_AT91RM9200 must be defined."
24#endif
25
26/* interface and function clocks */
27static struct clk *iclk, *fclk;
28
29extern int usb_disabled(void);
30
31/*-------------------------------------------------------------------------*/
32
33static void at91_start_hc(struct platform_device *pdev)
34{
35 struct usb_hcd *hcd = platform_get_drvdata(pdev);
36 struct ohci_regs __iomem *regs = hcd->regs;
37
38 dev_dbg(&pdev->dev, "starting AT91RM9200 OHCI USB Controller\n");
39
40 /*
41 * Start the USB clocks.
42 */
43 clk_enable(iclk);
44 clk_enable(fclk);
45
46 /*
47 * The USB host controller must remain in reset.
48 */
49 writel(0, &regs->control);
50}
51
52static void at91_stop_hc(struct platform_device *pdev)
53{
54 struct usb_hcd *hcd = platform_get_drvdata(pdev);
55 struct ohci_regs __iomem *regs = hcd->regs;
56
57 dev_dbg(&pdev->dev, "stopping AT91RM9200 OHCI USB Controller\n");
58
59 /*
60 * Put the USB host controller into reset.
61 */
62 writel(0, &regs->control);
63
64 /*
65 * Stop the USB clocks.
66 */
67 clk_disable(fclk);
68 clk_disable(iclk);
69}
70
71
72/*-------------------------------------------------------------------------*/
73
74static int usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
75
76/* configure so an HC device and id are always provided */
77/* always called with process context; sleeping is OK */
78
79
80/**
81 * usb_hcd_at91_probe - initialize AT91RM9200-based HCDs
82 * Context: !in_interrupt()
83 *
84 * Allocates basic resources for this USB host controller, and
85 * then invokes the start() method for the HCD associated with it
86 * through the hotplug entry's driver_data.
87 *
88 * Store this function in the HCD's struct pci_driver as probe().
89 */
90int usb_hcd_at91_probe (const struct hc_driver *driver, struct platform_device *pdev)
91{
92 int retval;
93 struct usb_hcd *hcd = NULL;
94
95 if (pdev->num_resources != 2) {
96 pr_debug("hcd probe: invalid num_resources");
97 return -ENODEV;
98 }
99
100 if ((pdev->resource[0].flags != IORESOURCE_MEM) || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
101 pr_debug("hcd probe: invalid resource type\n");
102 return -ENODEV;
103 }
104
105 hcd = usb_create_hcd(driver, &pdev->dev, "at91rm9200");
106 if (!hcd)
107 return -ENOMEM;
108 hcd->rsrc_start = pdev->resource[0].start;
109 hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
110
111 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
112 pr_debug("request_mem_region failed\n");
113 retval = -EBUSY;
114 goto err1;
115 }
116
117 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
118 if (!hcd->regs) {
119 pr_debug("ioremap failed\n");
120 retval = -EIO;
121 goto err2;
122 }
123
124 iclk = clk_get(&pdev->dev, "ohci_clk");
125 fclk = clk_get(&pdev->dev, "uhpck");
126
127 at91_start_hc(pdev);
128 ohci_hcd_init(hcd_to_ohci(hcd));
129
130 retval = usb_add_hcd(hcd, pdev->resource[1].start, SA_INTERRUPT);
131 if (retval == 0)
132 return retval;
133
134 /* Error handling */
135 at91_stop_hc(pdev);
136
137 clk_put(fclk);
138 clk_put(iclk);
139
140 iounmap(hcd->regs);
141
142 err2:
143 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
144
145 err1:
146 usb_put_hcd(hcd);
147 return retval;
148}
149
150
151/* may be called without controller electrically present */
152/* may be called with controller, bus, and devices active */
153
154/**
155 * usb_hcd_at91_remove - shutdown processing for AT91RM9200-based HCDs
156 * @dev: USB Host Controller being removed
157 * Context: !in_interrupt()
158 *
159 * Reverses the effect of usb_hcd_at91_probe(), first invoking
160 * the HCD's stop() method. It is always called from a thread
161 * context, normally "rmmod", "apmd", or something similar.
162 *
163 */
164static int usb_hcd_at91_remove (struct usb_hcd *hcd, struct platform_device *pdev)
165{
166 usb_remove_hcd(hcd);
167 at91_stop_hc(pdev);
168 iounmap(hcd->regs);
169 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
170
171 clk_put(fclk);
172 clk_put(iclk);
173 fclk = iclk = NULL;
174
175 dev_set_drvdata(&pdev->dev, NULL);
176 return 0;
177}
178
179/*-------------------------------------------------------------------------*/
180
181static int __devinit
182ohci_at91_start (struct usb_hcd *hcd)
183{
184// struct at91_ohci_data *board = hcd->self.controller->platform_data;
185 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
186 int ret;
187
188 if ((ret = ohci_init(ohci)) < 0)
189 return ret;
190
191 if ((ret = ohci_run(ohci)) < 0) {
192 err("can't start %s", hcd->self.bus_name);
193 ohci_stop(hcd);
194 return ret;
195 }
196// hcd->self.root_hub->maxchild = board->ports;
197 return 0;
198}
199
200/*-------------------------------------------------------------------------*/
201
202static const struct hc_driver ohci_at91_hc_driver = {
203 .description = hcd_name,
204 .product_desc = "AT91RM9200 OHCI",
205 .hcd_priv_size = sizeof(struct ohci_hcd),
206
207 /*
208 * generic hardware linkage
209 */
210 .irq = ohci_irq,
211 .flags = HCD_USB11 | HCD_MEMORY,
212
213 /*
214 * basic lifecycle operations
215 */
216 .start = ohci_at91_start,
217 .stop = ohci_stop,
218
219 /*
220 * managing i/o requests and associated device resources
221 */
222 .urb_enqueue = ohci_urb_enqueue,
223 .urb_dequeue = ohci_urb_dequeue,
224 .endpoint_disable = ohci_endpoint_disable,
225
226 /*
227 * scheduling support
228 */
229 .get_frame_number = ohci_get_frame,
230
231 /*
232 * root hub support
233 */
234 .hub_status_data = ohci_hub_status_data,
235 .hub_control = ohci_hub_control,
236
237#ifdef CONFIG_PM
238 .hub_suspend = ohci_hub_suspend,
239 .hub_resume = ohci_hub_resume,
240#endif
241 .start_port_reset = ohci_start_port_reset,
242};
243
244/*-------------------------------------------------------------------------*/
245
246static int ohci_hcd_at91_drv_probe(struct platform_device *dev)
247{
248 return usb_hcd_at91_probe(&ohci_at91_hc_driver, dev);
249}
250
251static int ohci_hcd_at91_drv_remove(struct platform_device *dev)
252{
253 return usb_hcd_at91_remove(platform_get_drvdata(dev), dev);
254}
255
256#ifdef CONFIG_PM
257static int ohci_hcd_at91_drv_suspend(struct platform_device *dev, u32 state, u32 level)
258{
259 printk("%s(%s:%d): not implemented yet\n",
260 __func__, __FILE__, __LINE__);
261
262 clk_disable(fclk);
263
264 return 0;
265}
266
267static int ohci_hcd_at91_drv_resume(struct platform_device *dev, u32 state)
268{
269 printk("%s(%s:%d): not implemented yet\n",
270 __func__, __FILE__, __LINE__);
271
272 clk_enable(fclk);
273
274 return 0;
275}
276#else
277#define ohci_hcd_at91_drv_suspend NULL
278#define ohci_hcd_at91_drv_resume NULL
279#endif
280
281static struct platform_driver ohci_hcd_at91_driver = {
282 .probe = ohci_hcd_at91_drv_probe,
283 .remove = ohci_hcd_at91_drv_remove,
284 .suspend = ohci_hcd_at91_drv_suspend,
285 .resume = ohci_hcd_at91_drv_resume,
286 .driver = {
287 .name = "at91rm9200-ohci",
288 .owner = THIS_MODULE,
289 },
290};
291
292static int __init ohci_hcd_at91_init (void)
293{
294 if (usb_disabled())
295 return -ENODEV;
296
297 return platform_driver_register(&ohci_hcd_at91_driver);
298}
299
300static void __exit ohci_hcd_at91_cleanup (void)
301{
302 platform_driver_unregister(&ohci_hcd_at91_driver);
303}
304
305module_init (ohci_hcd_at91_init);
306module_exit (ohci_hcd_at91_cleanup);
diff --git a/drivers/usb/host/ohci-au1xxx.c b/drivers/usb/host/ohci-au1xxx.c
index db280ca7b7a0..a1c8b3b2fcc7 100644
--- a/drivers/usb/host/ohci-au1xxx.c
+++ b/drivers/usb/host/ohci-au1xxx.c
@@ -23,6 +23,8 @@
23 23
24#include <asm/mach-au1x00/au1000.h> 24#include <asm/mach-au1x00/au1000.h>
25 25
26#ifndef CONFIG_SOC_AU1200
27
26#define USBH_ENABLE_BE (1<<0) 28#define USBH_ENABLE_BE (1<<0)
27#define USBH_ENABLE_C (1<<1) 29#define USBH_ENABLE_C (1<<1)
28#define USBH_ENABLE_E (1<<2) 30#define USBH_ENABLE_E (1<<2)
@@ -37,21 +39,68 @@
37#error not byte order defined 39#error not byte order defined
38#endif 40#endif
39 41
42#else /* Au1200 */
43
44#define USB_HOST_CONFIG (USB_MSR_BASE + USB_MSR_MCFG)
45#define USB_MCFG_PFEN (1<<31)
46#define USB_MCFG_RDCOMB (1<<30)
47#define USB_MCFG_SSDEN (1<<23)
48#define USB_MCFG_OHCCLKEN (1<<16)
49#define USB_MCFG_UCAM (1<<7)
50#define USB_MCFG_OBMEN (1<<1)
51#define USB_MCFG_OMEMEN (1<<0)
52
53#define USBH_ENABLE_CE USB_MCFG_OHCCLKEN
54#ifdef CONFIG_DMA_COHERENT
55#define USBH_ENABLE_INIT (USB_MCFG_OHCCLKEN \
56 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
57 | USB_MCFG_SSDEN | USB_MCFG_UCAM \
58 | USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
59#else
60#define USBH_ENABLE_INIT (USB_MCFG_OHCCLKEN \
61 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
62 | USB_MCFG_SSDEN \
63 | USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
64#endif
65#define USBH_DISABLE (USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
66
67#endif /* Au1200 */
68
40extern int usb_disabled(void); 69extern int usb_disabled(void);
41 70
42/*-------------------------------------------------------------------------*/ 71/*-------------------------------------------------------------------------*/
43 72
44static void au1xxx_start_hc(struct platform_device *dev) 73static void au1xxx_start_ohc(struct platform_device *dev)
45{ 74{
46 printk(KERN_DEBUG __FILE__ 75 printk(KERN_DEBUG __FILE__
47 ": starting Au1xxx OHCI USB Controller\n"); 76 ": starting Au1xxx OHCI USB Controller\n");
48 77
49 /* enable host controller */ 78 /* enable host controller */
79
80#ifndef CONFIG_SOC_AU1200
81
50 au_writel(USBH_ENABLE_CE, USB_HOST_CONFIG); 82 au_writel(USBH_ENABLE_CE, USB_HOST_CONFIG);
51 udelay(1000); 83 udelay(1000);
52 au_writel(USBH_ENABLE_INIT, USB_HOST_CONFIG); 84 au_writel(USBH_ENABLE_INIT, USB_HOST_CONFIG);
53 udelay(1000); 85 udelay(1000);
54 86
87#else /* Au1200 */
88
89 /* write HW defaults again in case Yamon cleared them */
90 if (au_readl(USB_HOST_CONFIG) == 0) {
91 au_writel(0x00d02000, USB_HOST_CONFIG);
92 au_readl(USB_HOST_CONFIG);
93 udelay(1000);
94 }
95 au_writel(USBH_ENABLE_CE | au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
96 au_readl(USB_HOST_CONFIG);
97 udelay(1000);
98 au_writel(USBH_ENABLE_INIT | au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
99 au_readl(USB_HOST_CONFIG);
100 udelay(1000);
101
102#endif /* Au1200 */
103
55 /* wait for reset complete (read register twice; see au1500 errata) */ 104 /* wait for reset complete (read register twice; see au1500 errata) */
56 while (au_readl(USB_HOST_CONFIG), 105 while (au_readl(USB_HOST_CONFIG),
57 !(au_readl(USB_HOST_CONFIG) & USBH_ENABLE_RD)) 106 !(au_readl(USB_HOST_CONFIG) & USBH_ENABLE_RD))
@@ -61,13 +110,25 @@ static void au1xxx_start_hc(struct platform_device *dev)
61 ": Clock to USB host has been enabled \n"); 110 ": Clock to USB host has been enabled \n");
62} 111}
63 112
64static void au1xxx_stop_hc(struct platform_device *dev) 113static void au1xxx_stop_ohc(struct platform_device *dev)
65{ 114{
66 printk(KERN_DEBUG __FILE__ 115 printk(KERN_DEBUG __FILE__
67 ": stopping Au1xxx OHCI USB Controller\n"); 116 ": stopping Au1xxx OHCI USB Controller\n");
68 117
118#ifndef CONFIG_SOC_AU1200
119
69 /* Disable clock */ 120 /* Disable clock */
70 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_ENABLE_CE, USB_HOST_CONFIG); 121 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_ENABLE_CE, USB_HOST_CONFIG);
122
123#else /* Au1200 */
124
125 /* Disable mem */
126 au_writel(~USBH_DISABLE & au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
127 udelay(1000);
128 /* Disable clock */
129 au_writel(~USBH_ENABLE_CE & au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
130 au_readl(USB_HOST_CONFIG);
131#endif /* Au1200 */
71} 132}
72 133
73 134
@@ -78,7 +139,7 @@ static void au1xxx_stop_hc(struct platform_device *dev)
78 139
79 140
80/** 141/**
81 * usb_hcd_au1xxx_probe - initialize Au1xxx-based HCDs 142 * usb_ohci_au1xxx_probe - initialize Au1xxx-based HCDs
82 * Context: !in_interrupt() 143 * Context: !in_interrupt()
83 * 144 *
84 * Allocates basic resources for this USB host controller, and 145 * Allocates basic resources for this USB host controller, and
@@ -86,14 +147,25 @@ static void au1xxx_stop_hc(struct platform_device *dev)
86 * through the hotplug entry's driver_data. 147 * through the hotplug entry's driver_data.
87 * 148 *
88 */ 149 */
89int usb_hcd_au1xxx_probe (const struct hc_driver *driver, 150static int usb_ohci_au1xxx_probe(const struct hc_driver *driver,
90 struct platform_device *dev) 151 struct platform_device *dev)
91{ 152{
92 int retval; 153 int retval;
93 struct usb_hcd *hcd; 154 struct usb_hcd *hcd;
94 155
95 if(dev->resource[1].flags != IORESOURCE_IRQ) { 156#if defined(CONFIG_SOC_AU1200) && defined(CONFIG_DMA_COHERENT)
96 pr_debug ("resource[1] is not IORESOURCE_IRQ"); 157 /* Au1200 AB USB does not support coherent memory */
158 if (!(read_c0_prid() & 0xff)) {
159 pr_info("%s: this is chip revision AB !!\n",
160 dev->dev.name);
161 pr_info("%s: update your board or re-configure the kernel\n",
162 dev->dev.name);
163 return -ENODEV;
164 }
165#endif
166
167 if (dev->resource[1].flags != IORESOURCE_IRQ) {
168 pr_debug("resource[1] is not IORESOURCE_IRQ\n");
97 return -ENOMEM; 169 return -ENOMEM;
98 } 170 }
99 171
@@ -104,26 +176,26 @@ int usb_hcd_au1xxx_probe (const struct hc_driver *driver,
104 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1; 176 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1;
105 177
106 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) { 178 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
107 pr_debug("request_mem_region failed"); 179 pr_debug("request_mem_region failed\n");
108 retval = -EBUSY; 180 retval = -EBUSY;
109 goto err1; 181 goto err1;
110 } 182 }
111 183
112 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len); 184 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
113 if (!hcd->regs) { 185 if (!hcd->regs) {
114 pr_debug("ioremap failed"); 186 pr_debug("ioremap failed\n");
115 retval = -ENOMEM; 187 retval = -ENOMEM;
116 goto err2; 188 goto err2;
117 } 189 }
118 190
119 au1xxx_start_hc(dev); 191 au1xxx_start_ohc(dev);
120 ohci_hcd_init(hcd_to_ohci(hcd)); 192 ohci_hcd_init(hcd_to_ohci(hcd));
121 193
122 retval = usb_add_hcd(hcd, dev->resource[1].start, SA_INTERRUPT); 194 retval = usb_add_hcd(hcd, dev->resource[1].start, SA_INTERRUPT | SA_SHIRQ);
123 if (retval == 0) 195 if (retval == 0)
124 return retval; 196 return retval;
125 197
126 au1xxx_stop_hc(dev); 198 au1xxx_stop_ohc(dev);
127 iounmap(hcd->regs); 199 iounmap(hcd->regs);
128 err2: 200 err2:
129 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 201 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
@@ -146,10 +218,10 @@ int usb_hcd_au1xxx_probe (const struct hc_driver *driver,
146 * context, normally "rmmod", "apmd", or something similar. 218 * context, normally "rmmod", "apmd", or something similar.
147 * 219 *
148 */ 220 */
149void usb_hcd_au1xxx_remove (struct usb_hcd *hcd, struct platform_device *dev) 221static void usb_ohci_au1xxx_remove(struct usb_hcd *hcd, struct platform_device *dev)
150{ 222{
151 usb_remove_hcd(hcd); 223 usb_remove_hcd(hcd);
152 au1xxx_stop_hc(dev); 224 au1xxx_stop_ohc(dev);
153 iounmap(hcd->regs); 225 iounmap(hcd->regs);
154 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 226 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
155 usb_put_hcd(hcd); 227 usb_put_hcd(hcd);
@@ -235,7 +307,7 @@ static int ohci_hcd_au1xxx_drv_probe(struct platform_device *pdev)
235 if (usb_disabled()) 307 if (usb_disabled())
236 return -ENODEV; 308 return -ENODEV;
237 309
238 ret = usb_hcd_au1xxx_probe(&ohci_au1xxx_hc_driver, pdev); 310 ret = usb_ohci_au1xxx_probe(&ohci_au1xxx_hc_driver, pdev);
239 return ret; 311 return ret;
240} 312}
241 313
@@ -243,7 +315,7 @@ static int ohci_hcd_au1xxx_drv_remove(struct platform_device *pdev)
243{ 315{
244 struct usb_hcd *hcd = platform_get_drvdata(pdev); 316 struct usb_hcd *hcd = platform_get_drvdata(pdev);
245 317
246 usb_hcd_au1xxx_remove(hcd, pdev); 318 usb_ohci_au1xxx_remove(hcd, pdev);
247 return 0; 319 return 0;
248} 320}
249 /*TBD*/ 321 /*TBD*/
diff --git a/drivers/usb/host/ohci-hcd.c b/drivers/usb/host/ohci-hcd.c
index a4b12404ae08..544f7589912f 100644
--- a/drivers/usb/host/ohci-hcd.c
+++ b/drivers/usb/host/ohci-hcd.c
@@ -443,11 +443,16 @@ ohci_reboot (struct notifier_block *block, unsigned long code, void *null)
443static int ohci_init (struct ohci_hcd *ohci) 443static int ohci_init (struct ohci_hcd *ohci)
444{ 444{
445 int ret; 445 int ret;
446 struct usb_hcd *hcd = ohci_to_hcd(ohci);
446 447
447 disable (ohci); 448 disable (ohci);
448 ohci->regs = ohci_to_hcd(ohci)->regs; 449 ohci->regs = hcd->regs;
449 ohci->next_statechange = jiffies; 450 ohci->next_statechange = jiffies;
450 451
452 /* REVISIT this BIOS handshake is now moved into PCI "quirks", and
453 * was never needed for most non-PCI systems ... remove the code?
454 */
455
451#ifndef IR_DISABLE 456#ifndef IR_DISABLE
452 /* SMM owns the HC? not for long! */ 457 /* SMM owns the HC? not for long! */
453 if (!no_handshake && ohci_readl (ohci, 458 if (!no_handshake && ohci_readl (ohci,
@@ -478,8 +483,10 @@ static int ohci_init (struct ohci_hcd *ohci)
478 483
479 /* Disable HC interrupts */ 484 /* Disable HC interrupts */
480 ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable); 485 ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
481 // flush the writes 486
482 (void) ohci_readl (ohci, &ohci->regs->control); 487 /* flush the writes, and save key bits like RWC */
488 if (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_RWC)
489 ohci->hc_control |= OHCI_CTRL_RWC;
483 490
484 /* Read the number of ports unless overridden */ 491 /* Read the number of ports unless overridden */
485 if (ohci->num_ports == 0) 492 if (ohci->num_ports == 0)
@@ -488,16 +495,19 @@ static int ohci_init (struct ohci_hcd *ohci)
488 if (ohci->hcca) 495 if (ohci->hcca)
489 return 0; 496 return 0;
490 497
491 ohci->hcca = dma_alloc_coherent (ohci_to_hcd(ohci)->self.controller, 498 ohci->hcca = dma_alloc_coherent (hcd->self.controller,
492 sizeof *ohci->hcca, &ohci->hcca_dma, 0); 499 sizeof *ohci->hcca, &ohci->hcca_dma, 0);
493 if (!ohci->hcca) 500 if (!ohci->hcca)
494 return -ENOMEM; 501 return -ENOMEM;
495 502
496 if ((ret = ohci_mem_init (ohci)) < 0) 503 if ((ret = ohci_mem_init (ohci)) < 0)
497 ohci_stop (ohci_to_hcd(ohci)); 504 ohci_stop (hcd);
505 else {
506 register_reboot_notifier (&ohci->reboot_notifier);
507 create_debug_files (ohci);
508 }
498 509
499 return ret; 510 return ret;
500
501} 511}
502 512
503/*-------------------------------------------------------------------------*/ 513/*-------------------------------------------------------------------------*/
@@ -510,6 +520,7 @@ static int ohci_run (struct ohci_hcd *ohci)
510{ 520{
511 u32 mask, temp; 521 u32 mask, temp;
512 int first = ohci->fminterval == 0; 522 int first = ohci->fminterval == 0;
523 struct usb_hcd *hcd = ohci_to_hcd(ohci);
513 524
514 disable (ohci); 525 disable (ohci);
515 526
@@ -525,18 +536,17 @@ static int ohci_run (struct ohci_hcd *ohci)
525 /* also: power/overcurrent flags in roothub.a */ 536 /* also: power/overcurrent flags in roothub.a */
526 } 537 }
527 538
528 /* Reset USB nearly "by the book". RemoteWakeupConnected 539 /* Reset USB nearly "by the book". RemoteWakeupConnected was
529 * saved if boot firmware (BIOS/SMM/...) told us it's connected 540 * saved if boot firmware (BIOS/SMM/...) told us it's connected,
530 * (for OHCI integrated on mainboard, it normally is) 541 * or if bus glue did the same (e.g. for PCI add-in cards with
542 * PCI PM support).
531 */ 543 */
532 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
533 ohci_dbg (ohci, "resetting from state '%s', control = 0x%x\n", 544 ohci_dbg (ohci, "resetting from state '%s', control = 0x%x\n",
534 hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS), 545 hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
535 ohci->hc_control); 546 ohci_readl (ohci, &ohci->regs->control));
536 547 if ((ohci->hc_control & OHCI_CTRL_RWC) != 0
537 if (ohci->hc_control & OHCI_CTRL_RWC 548 && !device_may_wakeup(hcd->self.controller))
538 && !(ohci->flags & OHCI_QUIRK_AMD756)) 549 device_init_wakeup(hcd->self.controller, 1);
539 ohci_to_hcd(ohci)->can_wakeup = 1;
540 550
541 switch (ohci->hc_control & OHCI_CTRL_HCFS) { 551 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
542 case OHCI_USB_OPER: 552 case OHCI_USB_OPER:
@@ -632,7 +642,7 @@ retry:
632 ohci->hc_control &= OHCI_CTRL_RWC; 642 ohci->hc_control &= OHCI_CTRL_RWC;
633 ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER; 643 ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
634 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 644 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
635 ohci_to_hcd(ohci)->state = HC_STATE_RUNNING; 645 hcd->state = HC_STATE_RUNNING;
636 646
637 /* wake on ConnectStatusChange, matching external hubs */ 647 /* wake on ConnectStatusChange, matching external hubs */
638 ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status); 648 ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status);
@@ -667,15 +677,10 @@ retry:
667 677
668 // POTPGT delay is bits 24-31, in 2 ms units. 678 // POTPGT delay is bits 24-31, in 2 ms units.
669 mdelay ((temp >> 23) & 0x1fe); 679 mdelay ((temp >> 23) & 0x1fe);
670 ohci_to_hcd(ohci)->state = HC_STATE_RUNNING; 680 hcd->state = HC_STATE_RUNNING;
671 681
672 ohci_dump (ohci, 1); 682 ohci_dump (ohci, 1);
673 683
674 if (ohci_to_hcd(ohci)->self.root_hub == NULL) {
675 register_reboot_notifier (&ohci->reboot_notifier);
676 create_debug_files (ohci);
677 }
678
679 return 0; 684 return 0;
680} 685}
681 686
@@ -905,6 +910,10 @@ MODULE_LICENSE ("GPL");
905#include "ohci-ppc-soc.c" 910#include "ohci-ppc-soc.c"
906#endif 911#endif
907 912
913#ifdef CONFIG_ARCH_AT91RM9200
914#include "ohci-at91.c"
915#endif
916
908#if !(defined(CONFIG_PCI) \ 917#if !(defined(CONFIG_PCI) \
909 || defined(CONFIG_SA1111) \ 918 || defined(CONFIG_SA1111) \
910 || defined(CONFIG_ARCH_S3C2410) \ 919 || defined(CONFIG_ARCH_S3C2410) \
@@ -913,6 +922,7 @@ MODULE_LICENSE ("GPL");
913 || defined (CONFIG_PXA27x) \ 922 || defined (CONFIG_PXA27x) \
914 || defined (CONFIG_SOC_AU1X00) \ 923 || defined (CONFIG_SOC_AU1X00) \
915 || defined (CONFIG_USB_OHCI_HCD_PPC_SOC) \ 924 || defined (CONFIG_USB_OHCI_HCD_PPC_SOC) \
925 || defined (CONFIG_ARCH_AT91RM9200) \
916 ) 926 )
917#error "missing bus glue for ohci-hcd" 927#error "missing bus glue for ohci-hcd"
918#endif 928#endif
diff --git a/drivers/usb/host/ohci-hub.c b/drivers/usb/host/ohci-hub.c
index 4b2226d77b34..0bb972b58336 100644
--- a/drivers/usb/host/ohci-hub.c
+++ b/drivers/usb/host/ohci-hub.c
@@ -107,7 +107,7 @@ static int ohci_bus_suspend (struct usb_hcd *hcd)
107 &ohci->regs->intrstatus); 107 &ohci->regs->intrstatus);
108 108
109 /* maybe resume can wake root hub */ 109 /* maybe resume can wake root hub */
110 if (hcd->remote_wakeup) 110 if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev))
111 ohci->hc_control |= OHCI_CTRL_RWE; 111 ohci->hc_control |= OHCI_CTRL_RWE;
112 else 112 else
113 ohci->hc_control &= ~OHCI_CTRL_RWE; 113 ohci->hc_control &= ~OHCI_CTRL_RWE;
@@ -246,9 +246,9 @@ static int ohci_bus_resume (struct usb_hcd *hcd)
246 (void) ohci_readl (ohci, &ohci->regs->control); 246 (void) ohci_readl (ohci, &ohci->regs->control);
247 msleep (3); 247 msleep (3);
248 248
249 temp = OHCI_CONTROL_INIT | OHCI_USB_OPER; 249 temp = ohci->hc_control;
250 if (hcd->can_wakeup) 250 temp &= OHCI_CTRL_RWC;
251 temp |= OHCI_CTRL_RWC; 251 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
252 ohci->hc_control = temp; 252 ohci->hc_control = temp;
253 ohci_writel (ohci, temp, &ohci->regs->control); 253 ohci_writel (ohci, temp, &ohci->regs->control);
254 (void) ohci_readl (ohci, &ohci->regs->control); 254 (void) ohci_readl (ohci, &ohci->regs->control);
@@ -302,7 +302,7 @@ ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
302{ 302{
303 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 303 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
304 int i, changed = 0, length = 1; 304 int i, changed = 0, length = 1;
305 int can_suspend = hcd->can_wakeup; 305 int can_suspend = device_may_wakeup(&hcd->self.root_hub->dev);
306 unsigned long flags; 306 unsigned long flags;
307 307
308 spin_lock_irqsave (&ohci->lock, flags); 308 spin_lock_irqsave (&ohci->lock, flags);
@@ -354,7 +354,7 @@ ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
354 */ 354 */
355 if (!(status & RH_PS_CCS)) 355 if (!(status & RH_PS_CCS))
356 continue; 356 continue;
357 if ((status & RH_PS_PSS) && hcd->remote_wakeup) 357 if ((status & RH_PS_PSS) && can_suspend)
358 continue; 358 continue;
359 can_suspend = 0; 359 can_suspend = 0;
360 } 360 }
diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
index 3785b3f7df1b..ca19abe01c53 100644
--- a/drivers/usb/host/ohci-omap.c
+++ b/drivers/usb/host/ohci-omap.c
@@ -286,7 +286,7 @@ void usb_hcd_omap_remove (struct usb_hcd *, struct platform_device *);
286int usb_hcd_omap_probe (const struct hc_driver *driver, 286int usb_hcd_omap_probe (const struct hc_driver *driver,
287 struct platform_device *pdev) 287 struct platform_device *pdev)
288{ 288{
289 int retval; 289 int retval, irq;
290 struct usb_hcd *hcd = 0; 290 struct usb_hcd *hcd = 0;
291 struct ohci_hcd *ohci; 291 struct ohci_hcd *ohci;
292 292
@@ -329,7 +329,12 @@ int usb_hcd_omap_probe (const struct hc_driver *driver,
329 if (retval < 0) 329 if (retval < 0)
330 goto err2; 330 goto err2;
331 331
332 retval = usb_add_hcd(hcd, platform_get_irq(pdev, 0), SA_INTERRUPT); 332 irq = platform_get_irq(pdev, 0);
333 if (irq < 0) {
334 retval = -ENXIO;
335 goto err2;
336 }
337 retval = usb_add_hcd(hcd, irq, SA_INTERRUPT);
333 if (retval == 0) 338 if (retval == 0)
334 return retval; 339 return retval;
335 340
diff --git a/drivers/usb/host/ohci-pci.c b/drivers/usb/host/ohci-pci.c
index 1b09dde068e1..1bfe96f4d045 100644
--- a/drivers/usb/host/ohci-pci.c
+++ b/drivers/usb/host/ohci-pci.c
@@ -35,7 +35,10 @@ ohci_pci_start (struct usb_hcd *hcd)
35 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 35 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
36 int ret; 36 int ret;
37 37
38 if(hcd->self.controller && hcd->self.controller->bus == &pci_bus_type) { 38 /* REVISIT this whole block should move to reset(), which handles
39 * all the other one-time init.
40 */
41 if (hcd->self.controller) {
39 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 42 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
40 43
41 /* AMD 756, for most chips (early revs), corrupts register 44 /* AMD 756, for most chips (early revs), corrupts register
@@ -45,7 +48,8 @@ ohci_pci_start (struct usb_hcd *hcd)
45 && pdev->device == 0x740c) { 48 && pdev->device == 0x740c) {
46 ohci->flags = OHCI_QUIRK_AMD756; 49 ohci->flags = OHCI_QUIRK_AMD756;
47 ohci_dbg (ohci, "AMD756 erratum 4 workaround\n"); 50 ohci_dbg (ohci, "AMD756 erratum 4 workaround\n");
48 // also somewhat erratum 10 (suspend/resume issues) 51 /* also erratum 10 (suspend/resume issues) */
52 device_init_wakeup(&hcd->self.root_hub->dev, 0);
49 } 53 }
50 54
51 /* FIXME for some of the early AMD 760 southbridges, OHCI 55 /* FIXME for some of the early AMD 760 southbridges, OHCI
@@ -88,6 +92,13 @@ ohci_pci_start (struct usb_hcd *hcd)
88 ohci_dbg (ohci, 92 ohci_dbg (ohci,
89 "enabled Compaq ZFMicro chipset quirk\n"); 93 "enabled Compaq ZFMicro chipset quirk\n");
90 } 94 }
95
96 /* RWC may not be set for add-in PCI cards, since boot
97 * firmware probably ignored them. This transfers PCI
98 * PM wakeup capabilities (once the PCI layer is fixed).
99 */
100 if (device_may_wakeup(&pdev->dev))
101 ohci->hc_control |= OHCI_CTRL_RWC;
91 } 102 }
92 103
93 /* NOTE: there may have already been a first reset, to 104 /* NOTE: there may have already been a first reset, to
diff --git a/drivers/usb/host/ohci-s3c2410.c b/drivers/usb/host/ohci-s3c2410.c
index 372527a83593..682bf2215660 100644
--- a/drivers/usb/host/ohci-s3c2410.c
+++ b/drivers/usb/host/ohci-s3c2410.c
@@ -158,7 +158,7 @@ static int ohci_s3c2410_hub_control (
158 "s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n", 158 "s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
159 hcd, typeReq, wValue, wIndex, buf, wLength); 159 hcd, typeReq, wValue, wIndex, buf, wLength);
160 160
161 /* if we are only an humble host without any special capabilites 161 /* if we are only an humble host without any special capabilities
162 * process the request straight away and exit */ 162 * process the request straight away and exit */
163 163
164 if (info == NULL) { 164 if (info == NULL) {
diff --git a/drivers/usb/host/sl811-hcd.c b/drivers/usb/host/sl811-hcd.c
index 517360b77d8e..a92343052751 100644
--- a/drivers/usb/host/sl811-hcd.c
+++ b/drivers/usb/host/sl811-hcd.c
@@ -853,7 +853,7 @@ static int sl811h_urb_enqueue(
853 853
854 } else { 854 } else {
855 INIT_LIST_HEAD(&ep->schedule); 855 INIT_LIST_HEAD(&ep->schedule);
856 ep->udev = usb_get_dev(udev); 856 ep->udev = udev;
857 ep->epnum = epnum; 857 ep->epnum = epnum;
858 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out); 858 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
859 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE; 859 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
@@ -1052,7 +1052,6 @@ sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
1052 if (!list_empty(&hep->urb_list)) 1052 if (!list_empty(&hep->urb_list))
1053 WARN("ep %p not empty?\n", ep); 1053 WARN("ep %p not empty?\n", ep);
1054 1054
1055 usb_put_dev(ep->udev);
1056 kfree(ep); 1055 kfree(ep);
1057 hep->hcpriv = NULL; 1056 hep->hcpriv = NULL;
1058} 1057}
diff --git a/drivers/usb/host/uhci-debug.c b/drivers/usb/host/uhci-debug.c
index 5832953086f8..e1239319655c 100644
--- a/drivers/usb/host/uhci-debug.c
+++ b/drivers/usb/host/uhci-debug.c
@@ -17,10 +17,13 @@
17 17
18#include "uhci-hcd.h" 18#include "uhci-hcd.h"
19 19
20static struct dentry *uhci_debugfs_root = NULL; 20#define uhci_debug_operations (* (struct file_operations *) NULL)
21static struct dentry *uhci_debugfs_root;
22
23#ifdef DEBUG
21 24
22/* Handle REALLY large printks so we don't overflow buffers */ 25/* Handle REALLY large printks so we don't overflow buffers */
23static inline void lprintk(char *buf) 26static void lprintk(char *buf)
24{ 27{
25 char *p; 28 char *p;
26 29
@@ -90,13 +93,59 @@ static int uhci_show_td(struct uhci_td *td, char *buf, int len, int space)
90 return out - buf; 93 return out - buf;
91} 94}
92 95
93static int uhci_show_qh(struct uhci_qh *qh, char *buf, int len, int space) 96static int uhci_show_urbp(struct urb_priv *urbp, char *buf, int len, int space)
94{ 97{
95 char *out = buf; 98 char *out = buf;
96 struct urb_priv *urbp;
97 struct list_head *head, *tmp;
98 struct uhci_td *td; 99 struct uhci_td *td;
99 int i = 0, checked = 0, prevactive = 0; 100 int i, nactive, ninactive;
101
102 if (len < 200)
103 return 0;
104
105 out += sprintf(out, "urb_priv [%p] ", urbp);
106 out += sprintf(out, "urb [%p] ", urbp->urb);
107 out += sprintf(out, "qh [%p] ", urbp->qh);
108 out += sprintf(out, "Dev=%d ", usb_pipedevice(urbp->urb->pipe));
109 out += sprintf(out, "EP=%x(%s) ", usb_pipeendpoint(urbp->urb->pipe),
110 (usb_pipein(urbp->urb->pipe) ? "IN" : "OUT"));
111
112 switch (usb_pipetype(urbp->urb->pipe)) {
113 case PIPE_ISOCHRONOUS: out += sprintf(out, "ISO"); break;
114 case PIPE_INTERRUPT: out += sprintf(out, "INT"); break;
115 case PIPE_BULK: out += sprintf(out, "BLK"); break;
116 case PIPE_CONTROL: out += sprintf(out, "CTL"); break;
117 }
118
119 out += sprintf(out, "%s", (urbp->fsbr ? " FSBR" : ""));
120
121 if (urbp->urb->status != -EINPROGRESS)
122 out += sprintf(out, " Status=%d", urbp->urb->status);
123 out += sprintf(out, "\n");
124
125 i = nactive = ninactive = 0;
126 list_for_each_entry(td, &urbp->td_list, list) {
127 if (++i <= 10 || debug > 2) {
128 out += sprintf(out, "%*s%d: ", space + 2, "", i);
129 out += uhci_show_td(td, out, len - (out - buf), 0);
130 } else {
131 if (td_status(td) & TD_CTRL_ACTIVE)
132 ++nactive;
133 else
134 ++ninactive;
135 }
136 }
137 if (nactive + ninactive > 0)
138 out += sprintf(out, "%*s[skipped %d inactive and %d active "
139 "TDs]\n",
140 space, "", ninactive, nactive);
141
142 return out - buf;
143}
144
145static int uhci_show_qh(struct uhci_qh *qh, char *buf, int len, int space)
146{
147 char *out = buf;
148 int i, nurbs;
100 __le32 element = qh_element(qh); 149 __le32 element = qh_element(qh);
101 150
102 /* Try to make sure there's enough memory */ 151 /* Try to make sure there's enough memory */
@@ -118,86 +167,40 @@ static int uhci_show_qh(struct uhci_qh *qh, char *buf, int len, int space)
118 if (!(element & ~(UHCI_PTR_QH | UHCI_PTR_DEPTH))) 167 if (!(element & ~(UHCI_PTR_QH | UHCI_PTR_DEPTH)))
119 out += sprintf(out, "%*s Element is NULL (bug?)\n", space, ""); 168 out += sprintf(out, "%*s Element is NULL (bug?)\n", space, "");
120 169
121 if (!qh->urbp) { 170 if (list_empty(&qh->queue)) {
122 out += sprintf(out, "%*s urbp == NULL\n", space, ""); 171 out += sprintf(out, "%*s queue is empty\n", space, "");
123 goto out; 172 } else {
124 } 173 struct urb_priv *urbp = list_entry(qh->queue.next,
125 174 struct urb_priv, node);
126 urbp = qh->urbp; 175 struct uhci_td *td = list_entry(urbp->td_list.next,
127 176 struct uhci_td, list);
128 head = &urbp->td_list; 177
129 tmp = head->next; 178 if (cpu_to_le32(td->dma_handle) != (element & ~UHCI_PTR_BITS))
130 179 out += sprintf(out, "%*s Element != First TD\n",
131 td = list_entry(tmp, struct uhci_td, list); 180 space, "");
132 181 i = nurbs = 0;
133 if (cpu_to_le32(td->dma_handle) != (element & ~UHCI_PTR_BITS)) 182 list_for_each_entry(urbp, &qh->queue, node) {
134 out += sprintf(out, "%*s Element != First TD\n", space, ""); 183 if (++i <= 10)
135 184 out += uhci_show_urbp(urbp, out,
136 while (tmp != head) { 185 len - (out - buf), space + 2);
137 struct uhci_td *td = list_entry(tmp, struct uhci_td, list); 186 else
138 187 ++nurbs;
139 tmp = tmp->next;
140
141 out += sprintf(out, "%*s%d: ", space + 2, "", i++);
142 out += uhci_show_td(td, out, len - (out - buf), 0);
143
144 if (i > 10 && !checked && prevactive && tmp != head &&
145 debug <= 2) {
146 struct list_head *ntmp = tmp;
147 struct uhci_td *ntd = td;
148 int active = 1, ni = i;
149
150 checked = 1;
151
152 while (ntmp != head && ntmp->next != head && active) {
153 ntd = list_entry(ntmp, struct uhci_td, list);
154
155 ntmp = ntmp->next;
156
157 active = td_status(ntd) & TD_CTRL_ACTIVE;
158
159 ni++;
160 }
161
162 if (active && ni > i) {
163 out += sprintf(out, "%*s[skipped %d active TDs]\n", space, "", ni - i);
164 tmp = ntmp;
165 td = ntd;
166 i = ni;
167 }
168 } 188 }
169 189 if (nurbs > 0)
170 prevactive = td_status(td) & TD_CTRL_ACTIVE; 190 out += sprintf(out, "%*s Skipped %d URBs\n",
191 space, "", nurbs);
171 } 192 }
172 193
173 if (list_empty(&urbp->queue_list) || urbp->queued) 194 if (qh->udev) {
174 goto out; 195 out += sprintf(out, "%*s Dummy TD\n", space, "");
175 196 out += uhci_show_td(qh->dummy_td, out, len - (out - buf), 0);
176 out += sprintf(out, "%*sQueued QHs:\n", -space, "--");
177
178 head = &urbp->queue_list;
179 tmp = head->next;
180
181 while (tmp != head) {
182 struct urb_priv *nurbp = list_entry(tmp, struct urb_priv,
183 queue_list);
184 tmp = tmp->next;
185
186 out += uhci_show_qh(nurbp->qh, out, len - (out - buf), space);
187 } 197 }
188 198
189out:
190 return out - buf; 199 return out - buf;
191} 200}
192 201
193#define show_frame_num() \
194 if (!shown) { \
195 shown = 1; \
196 out += sprintf(out, "- Frame %d\n", i); \
197 }
198
199#ifdef CONFIG_PROC_FS
200static const char * const qh_names[] = { 202static const char * const qh_names[] = {
203 "skel_unlink_qh", "skel_iso_qh",
201 "skel_int128_qh", "skel_int64_qh", 204 "skel_int128_qh", "skel_int64_qh",
202 "skel_int32_qh", "skel_int16_qh", 205 "skel_int32_qh", "skel_int16_qh",
203 "skel_int8_qh", "skel_int4_qh", 206 "skel_int8_qh", "skel_int4_qh",
@@ -206,12 +209,6 @@ static const char * const qh_names[] = {
206 "skel_bulk_qh", "skel_term_qh" 209 "skel_bulk_qh", "skel_term_qh"
207}; 210};
208 211
209#define show_qh_name() \
210 if (!shown) { \
211 shown = 1; \
212 out += sprintf(out, "- %s\n", qh_names[i]); \
213 }
214
215static int uhci_show_sc(int port, unsigned short status, char *buf, int len) 212static int uhci_show_sc(int port, unsigned short status, char *buf, int len)
216{ 213{
217 char *out = buf; 214 char *out = buf;
@@ -321,139 +318,29 @@ static int uhci_show_status(struct uhci_hcd *uhci, char *buf, int len)
321 return out - buf; 318 return out - buf;
322} 319}
323 320
324static int uhci_show_urbp(struct uhci_hcd *uhci, struct urb_priv *urbp, char *buf, int len)
325{
326 struct list_head *tmp;
327 char *out = buf;
328 int count = 0;
329
330 if (len < 200)
331 return 0;
332
333 out += sprintf(out, "urb_priv [%p] ", urbp);
334 out += sprintf(out, "urb [%p] ", urbp->urb);
335 out += sprintf(out, "qh [%p] ", urbp->qh);
336 out += sprintf(out, "Dev=%d ", usb_pipedevice(urbp->urb->pipe));
337 out += sprintf(out, "EP=%x(%s) ", usb_pipeendpoint(urbp->urb->pipe), (usb_pipein(urbp->urb->pipe) ? "IN" : "OUT"));
338
339 switch (usb_pipetype(urbp->urb->pipe)) {
340 case PIPE_ISOCHRONOUS: out += sprintf(out, "ISO "); break;
341 case PIPE_INTERRUPT: out += sprintf(out, "INT "); break;
342 case PIPE_BULK: out += sprintf(out, "BLK "); break;
343 case PIPE_CONTROL: out += sprintf(out, "CTL "); break;
344 }
345
346 out += sprintf(out, "%s", (urbp->fsbr ? "FSBR " : ""));
347 out += sprintf(out, "%s", (urbp->fsbr_timeout ? "FSBR_TO " : ""));
348
349 if (urbp->urb->status != -EINPROGRESS)
350 out += sprintf(out, "Status=%d ", urbp->urb->status);
351 //out += sprintf(out, "FSBRtime=%lx ",urbp->fsbrtime);
352
353 count = 0;
354 list_for_each(tmp, &urbp->td_list)
355 count++;
356 out += sprintf(out, "TDs=%d ",count);
357
358 if (urbp->queued)
359 out += sprintf(out, "queued\n");
360 else {
361 count = 0;
362 list_for_each(tmp, &urbp->queue_list)
363 count++;
364 out += sprintf(out, "queued URBs=%d\n", count);
365 }
366
367 return out - buf;
368}
369
370static int uhci_show_lists(struct uhci_hcd *uhci, char *buf, int len)
371{
372 char *out = buf;
373 struct list_head *head, *tmp;
374 int count;
375
376 out += sprintf(out, "Main list URBs:");
377 if (list_empty(&uhci->urb_list))
378 out += sprintf(out, " Empty\n");
379 else {
380 out += sprintf(out, "\n");
381 count = 0;
382 head = &uhci->urb_list;
383 tmp = head->next;
384 while (tmp != head) {
385 struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
386
387 out += sprintf(out, " %d: ", ++count);
388 out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
389 tmp = tmp->next;
390 }
391 }
392
393 out += sprintf(out, "Remove list URBs:");
394 if (list_empty(&uhci->urb_remove_list))
395 out += sprintf(out, " Empty\n");
396 else {
397 out += sprintf(out, "\n");
398 count = 0;
399 head = &uhci->urb_remove_list;
400 tmp = head->next;
401 while (tmp != head) {
402 struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
403
404 out += sprintf(out, " %d: ", ++count);
405 out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
406 tmp = tmp->next;
407 }
408 }
409
410 out += sprintf(out, "Complete list URBs:");
411 if (list_empty(&uhci->complete_list))
412 out += sprintf(out, " Empty\n");
413 else {
414 out += sprintf(out, "\n");
415 count = 0;
416 head = &uhci->complete_list;
417 tmp = head->next;
418 while (tmp != head) {
419 struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
420
421 out += sprintf(out, " %d: ", ++count);
422 out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
423 tmp = tmp->next;
424 }
425 }
426
427 return out - buf;
428}
429
430static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len) 321static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
431{ 322{
432 unsigned long flags;
433 char *out = buf; 323 char *out = buf;
434 int i, j; 324 int i, j;
435 struct uhci_qh *qh; 325 struct uhci_qh *qh;
436 struct uhci_td *td; 326 struct uhci_td *td;
437 struct list_head *tmp, *head; 327 struct list_head *tmp, *head;
438 328
439 spin_lock_irqsave(&uhci->lock, flags);
440
441 out += uhci_show_root_hub_state(uhci, out, len - (out - buf)); 329 out += uhci_show_root_hub_state(uhci, out, len - (out - buf));
442 out += sprintf(out, "HC status\n"); 330 out += sprintf(out, "HC status\n");
443 out += uhci_show_status(uhci, out, len - (out - buf)); 331 out += uhci_show_status(uhci, out, len - (out - buf));
332 if (debug <= 1)
333 return out - buf;
444 334
445 out += sprintf(out, "Frame List\n"); 335 out += sprintf(out, "Frame List\n");
446 for (i = 0; i < UHCI_NUMFRAMES; ++i) { 336 for (i = 0; i < UHCI_NUMFRAMES; ++i) {
447 int shown = 0;
448 td = uhci->frame_cpu[i]; 337 td = uhci->frame_cpu[i];
449 if (!td) 338 if (!td)
450 continue; 339 continue;
451 340
452 if (td->dma_handle != (dma_addr_t)uhci->frame[i]) { 341 out += sprintf(out, "- Frame %d\n", i); \
453 show_frame_num(); 342 if (td->dma_handle != (dma_addr_t)uhci->frame[i])
454 out += sprintf(out, " frame list does not match td->dma_handle!\n"); 343 out += sprintf(out, " frame list does not match td->dma_handle!\n");
455 }
456 show_frame_num();
457 344
458 head = &td->fl_list; 345 head = &td->fl_list;
459 tmp = head; 346 tmp = head;
@@ -467,14 +354,11 @@ static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
467 out += sprintf(out, "Skeleton QHs\n"); 354 out += sprintf(out, "Skeleton QHs\n");
468 355
469 for (i = 0; i < UHCI_NUM_SKELQH; ++i) { 356 for (i = 0; i < UHCI_NUM_SKELQH; ++i) {
470 int shown = 0; 357 int cnt = 0;
471 358
472 qh = uhci->skelqh[i]; 359 qh = uhci->skelqh[i];
473 360 out += sprintf(out, "- %s\n", qh_names[i]); \
474 if (debug > 1) { 361 out += uhci_show_qh(qh, out, len - (out - buf), 4);
475 show_qh_name();
476 out += uhci_show_qh(qh, out, len - (out - buf), 4);
477 }
478 362
479 /* Last QH is the Terminating QH, it's different */ 363 /* Last QH is the Terminating QH, it's different */
480 if (i == UHCI_NUM_SKELQH - 1) { 364 if (i == UHCI_NUM_SKELQH - 1) {
@@ -487,53 +371,37 @@ static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
487 continue; 371 continue;
488 } 372 }
489 373
490 j = (i < 7) ? 7 : i+1; /* Next skeleton */ 374 j = (i < 9) ? 9 : i+1; /* Next skeleton */
491 if (list_empty(&qh->list)) { 375 head = &qh->node;
492 if (i < UHCI_NUM_SKELQH - 1) {
493 if (qh->link !=
494 (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH)) {
495 show_qh_name();
496 out += sprintf(out, " skeleton QH not linked to next skeleton QH!\n");
497 }
498 }
499
500 continue;
501 }
502
503 show_qh_name();
504
505 head = &qh->list;
506 tmp = head->next; 376 tmp = head->next;
507 377
508 while (tmp != head) { 378 while (tmp != head) {
509 qh = list_entry(tmp, struct uhci_qh, list); 379 qh = list_entry(tmp, struct uhci_qh, node);
510
511 tmp = tmp->next; 380 tmp = tmp->next;
512 381 if (++cnt <= 10)
513 out += uhci_show_qh(qh, out, len - (out - buf), 4); 382 out += uhci_show_qh(qh, out,
383 len - (out - buf), 4);
514 } 384 }
385 if ((cnt -= 10) > 0)
386 out += sprintf(out, " Skipped %d QHs\n", cnt);
515 387
516 if (i < UHCI_NUM_SKELQH - 1) { 388 if (i > 1 && i < UHCI_NUM_SKELQH - 1) {
517 if (qh->link != 389 if (qh->link !=
518 (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH)) 390 (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH))
519 out += sprintf(out, " last QH not linked to next skeleton!\n"); 391 out += sprintf(out, " last QH not linked to next skeleton!\n");
520 } 392 }
521 } 393 }
522 394
523 if (debug > 2)
524 out += uhci_show_lists(uhci, out, len - (out - buf));
525
526 spin_unlock_irqrestore(&uhci->lock, flags);
527
528 return out - buf; 395 return out - buf;
529} 396}
530 397
398#ifdef CONFIG_DEBUG_FS
399
531#define MAX_OUTPUT (64 * 1024) 400#define MAX_OUTPUT (64 * 1024)
532 401
533struct uhci_debug { 402struct uhci_debug {
534 int size; 403 int size;
535 char *data; 404 char *data;
536 struct uhci_hcd *uhci;
537}; 405};
538 406
539static int uhci_debug_open(struct inode *inode, struct file *file) 407static int uhci_debug_open(struct inode *inode, struct file *file)
@@ -541,6 +409,7 @@ static int uhci_debug_open(struct inode *inode, struct file *file)
541 struct uhci_hcd *uhci = inode->u.generic_ip; 409 struct uhci_hcd *uhci = inode->u.generic_ip;
542 struct uhci_debug *up; 410 struct uhci_debug *up;
543 int ret = -ENOMEM; 411 int ret = -ENOMEM;
412 unsigned long flags;
544 413
545 lock_kernel(); 414 lock_kernel();
546 up = kmalloc(sizeof(*up), GFP_KERNEL); 415 up = kmalloc(sizeof(*up), GFP_KERNEL);
@@ -553,7 +422,11 @@ static int uhci_debug_open(struct inode *inode, struct file *file)
553 goto out; 422 goto out;
554 } 423 }
555 424
556 up->size = uhci_sprint_schedule(uhci, up->data, MAX_OUTPUT); 425 up->size = 0;
426 spin_lock_irqsave(&uhci->lock, flags);
427 if (uhci->is_initialized)
428 up->size = uhci_sprint_schedule(uhci, up->data, MAX_OUTPUT);
429 spin_unlock_irqrestore(&uhci->lock, flags);
557 430
558 file->private_data = up; 431 file->private_data = up;
559 432
@@ -604,15 +477,32 @@ static int uhci_debug_release(struct inode *inode, struct file *file)
604 return 0; 477 return 0;
605} 478}
606 479
480#undef uhci_debug_operations
607static struct file_operations uhci_debug_operations = { 481static struct file_operations uhci_debug_operations = {
482 .owner = THIS_MODULE,
608 .open = uhci_debug_open, 483 .open = uhci_debug_open,
609 .llseek = uhci_debug_lseek, 484 .llseek = uhci_debug_lseek,
610 .read = uhci_debug_read, 485 .read = uhci_debug_read,
611 .release = uhci_debug_release, 486 .release = uhci_debug_release,
612}; 487};
613 488
614#else /* CONFIG_DEBUG_FS */ 489#endif /* CONFIG_DEBUG_FS */
615 490
616#define uhci_debug_operations (* (struct file_operations *) NULL) 491#else /* DEBUG */
492
493static inline void lprintk(char *buf)
494{}
495
496static inline int uhci_show_qh(struct uhci_qh *qh, char *buf,
497 int len, int space)
498{
499 return 0;
500}
501
502static inline int uhci_sprint_schedule(struct uhci_hcd *uhci,
503 char *buf, int len)
504{
505 return 0;
506}
617 507
618#endif 508#endif
diff --git a/drivers/usb/host/uhci-hcd.c b/drivers/usb/host/uhci-hcd.c
index dfe121d35887..4edb8330c440 100644
--- a/drivers/usb/host/uhci-hcd.c
+++ b/drivers/usb/host/uhci-hcd.c
@@ -54,7 +54,7 @@
54/* 54/*
55 * Version Information 55 * Version Information
56 */ 56 */
57#define DRIVER_VERSION "v2.3" 57#define DRIVER_VERSION "v3.0"
58#define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, \ 58#define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, \
59Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, \ 59Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, \
60Alan Stern" 60Alan Stern"
@@ -68,12 +68,16 @@ Alan Stern"
68 * debug = 3, show all TDs in URBs when dumping 68 * debug = 3, show all TDs in URBs when dumping
69 */ 69 */
70#ifdef DEBUG 70#ifdef DEBUG
71#define DEBUG_CONFIGURED 1
71static int debug = 1; 72static int debug = 1;
72#else
73static int debug = 0;
74#endif
75module_param(debug, int, S_IRUGO | S_IWUSR); 73module_param(debug, int, S_IRUGO | S_IWUSR);
76MODULE_PARM_DESC(debug, "Debug level"); 74MODULE_PARM_DESC(debug, "Debug level");
75
76#else
77#define DEBUG_CONFIGURED 0
78#define debug 0
79#endif
80
77static char *errbuf; 81static char *errbuf;
78#define ERRBUF_LEN (32 * 1024) 82#define ERRBUF_LEN (32 * 1024)
79 83
@@ -338,6 +342,12 @@ static irqreturn_t uhci_irq(struct usb_hcd *hcd, struct pt_regs *regs)
338 dev_err(uhci_dev(uhci), 342 dev_err(uhci_dev(uhci),
339 "host controller halted, " 343 "host controller halted, "
340 "very bad!\n"); 344 "very bad!\n");
345 if (debug > 1 && errbuf) {
346 /* Print the schedule for debugging */
347 uhci_sprint_schedule(uhci,
348 errbuf, ERRBUF_LEN);
349 lprintk(errbuf);
350 }
341 hc_died(uhci); 351 hc_died(uhci);
342 352
343 /* Force a callback in case there are 353 /* Force a callback in case there are
@@ -376,6 +386,14 @@ static void release_uhci(struct uhci_hcd *uhci)
376{ 386{
377 int i; 387 int i;
378 388
389 if (DEBUG_CONFIGURED) {
390 spin_lock_irq(&uhci->lock);
391 uhci->is_initialized = 0;
392 spin_unlock_irq(&uhci->lock);
393
394 debugfs_remove(uhci->dentry);
395 }
396
379 for (i = 0; i < UHCI_NUM_SKELQH; i++) 397 for (i = 0; i < UHCI_NUM_SKELQH; i++)
380 uhci_free_qh(uhci, uhci->skelqh[i]); 398 uhci_free_qh(uhci, uhci->skelqh[i]);
381 399
@@ -390,8 +408,6 @@ static void release_uhci(struct uhci_hcd *uhci)
390 dma_free_coherent(uhci_dev(uhci), 408 dma_free_coherent(uhci_dev(uhci),
391 UHCI_NUMFRAMES * sizeof(*uhci->frame), 409 UHCI_NUMFRAMES * sizeof(*uhci->frame),
392 uhci->frame, uhci->frame_dma_handle); 410 uhci->frame, uhci->frame_dma_handle);
393
394 debugfs_remove(uhci->dentry);
395} 411}
396 412
397static int uhci_reset(struct usb_hcd *hcd) 413static int uhci_reset(struct usb_hcd *hcd)
@@ -474,33 +490,29 @@ static int uhci_start(struct usb_hcd *hcd)
474 490
475 hcd->uses_new_polling = 1; 491 hcd->uses_new_polling = 1;
476 492
477 dentry = debugfs_create_file(hcd->self.bus_name,
478 S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root, uhci,
479 &uhci_debug_operations);
480 if (!dentry) {
481 dev_err(uhci_dev(uhci),
482 "couldn't create uhci debugfs entry\n");
483 retval = -ENOMEM;
484 goto err_create_debug_entry;
485 }
486 uhci->dentry = dentry;
487
488 uhci->fsbr = 0; 493 uhci->fsbr = 0;
489 uhci->fsbrtimeout = 0; 494 uhci->fsbrtimeout = 0;
490 495
491 spin_lock_init(&uhci->lock); 496 spin_lock_init(&uhci->lock);
492 INIT_LIST_HEAD(&uhci->qh_remove_list);
493 497
494 INIT_LIST_HEAD(&uhci->td_remove_list); 498 INIT_LIST_HEAD(&uhci->td_remove_list);
495 499 INIT_LIST_HEAD(&uhci->idle_qh_list);
496 INIT_LIST_HEAD(&uhci->urb_remove_list);
497
498 INIT_LIST_HEAD(&uhci->urb_list);
499
500 INIT_LIST_HEAD(&uhci->complete_list);
501 500
502 init_waitqueue_head(&uhci->waitqh); 501 init_waitqueue_head(&uhci->waitqh);
503 502
503 if (DEBUG_CONFIGURED) {
504 dentry = debugfs_create_file(hcd->self.bus_name,
505 S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root,
506 uhci, &uhci_debug_operations);
507 if (!dentry) {
508 dev_err(uhci_dev(uhci), "couldn't create uhci "
509 "debugfs entry\n");
510 retval = -ENOMEM;
511 goto err_create_debug_entry;
512 }
513 uhci->dentry = dentry;
514 }
515
504 uhci->frame = dma_alloc_coherent(uhci_dev(uhci), 516 uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
505 UHCI_NUMFRAMES * sizeof(*uhci->frame), 517 UHCI_NUMFRAMES * sizeof(*uhci->frame),
506 &uhci->frame_dma_handle, 0); 518 &uhci->frame_dma_handle, 0);
@@ -540,7 +552,7 @@ static int uhci_start(struct usb_hcd *hcd)
540 } 552 }
541 553
542 for (i = 0; i < UHCI_NUM_SKELQH; i++) { 554 for (i = 0; i < UHCI_NUM_SKELQH; i++) {
543 uhci->skelqh[i] = uhci_alloc_qh(uhci); 555 uhci->skelqh[i] = uhci_alloc_qh(uhci, NULL, NULL);
544 if (!uhci->skelqh[i]) { 556 if (!uhci->skelqh[i]) {
545 dev_err(uhci_dev(uhci), "unable to allocate QH\n"); 557 dev_err(uhci_dev(uhci), "unable to allocate QH\n");
546 goto err_alloc_skelqh; 558 goto err_alloc_skelqh;
@@ -557,13 +569,17 @@ static int uhci_start(struct usb_hcd *hcd)
557 uhci->skel_int16_qh->link = 569 uhci->skel_int16_qh->link =
558 uhci->skel_int8_qh->link = 570 uhci->skel_int8_qh->link =
559 uhci->skel_int4_qh->link = 571 uhci->skel_int4_qh->link =
560 uhci->skel_int2_qh->link = 572 uhci->skel_int2_qh->link = UHCI_PTR_QH |
561 cpu_to_le32(uhci->skel_int1_qh->dma_handle) | UHCI_PTR_QH; 573 cpu_to_le32(uhci->skel_int1_qh->dma_handle);
562 uhci->skel_int1_qh->link = cpu_to_le32(uhci->skel_ls_control_qh->dma_handle) | UHCI_PTR_QH; 574
563 575 uhci->skel_int1_qh->link = UHCI_PTR_QH |
564 uhci->skel_ls_control_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH; 576 cpu_to_le32(uhci->skel_ls_control_qh->dma_handle);
565 uhci->skel_fs_control_qh->link = cpu_to_le32(uhci->skel_bulk_qh->dma_handle) | UHCI_PTR_QH; 577 uhci->skel_ls_control_qh->link = UHCI_PTR_QH |
566 uhci->skel_bulk_qh->link = cpu_to_le32(uhci->skel_term_qh->dma_handle) | UHCI_PTR_QH; 578 cpu_to_le32(uhci->skel_fs_control_qh->dma_handle);
579 uhci->skel_fs_control_qh->link = UHCI_PTR_QH |
580 cpu_to_le32(uhci->skel_bulk_qh->dma_handle);
581 uhci->skel_bulk_qh->link = UHCI_PTR_QH |
582 cpu_to_le32(uhci->skel_term_qh->dma_handle);
567 583
568 /* This dummy TD is to work around a bug in Intel PIIX controllers */ 584 /* This dummy TD is to work around a bug in Intel PIIX controllers */
569 uhci_fill_td(uhci->term_td, 0, uhci_explen(0) | 585 uhci_fill_td(uhci->term_td, 0, uhci_explen(0) |
@@ -589,15 +605,15 @@ static int uhci_start(struct usb_hcd *hcd)
589 605
590 /* 606 /*
591 * ffs (Find First bit Set) does exactly what we need: 607 * ffs (Find First bit Set) does exactly what we need:
592 * 1,3,5,... => ffs = 0 => use skel_int2_qh = skelqh[6], 608 * 1,3,5,... => ffs = 0 => use skel_int2_qh = skelqh[8],
593 * 2,6,10,... => ffs = 1 => use skel_int4_qh = skelqh[5], etc. 609 * 2,6,10,... => ffs = 1 => use skel_int4_qh = skelqh[7], etc.
594 * ffs > 6 => not on any high-period queue, so use 610 * ffs >= 7 => not on any high-period queue, so use
595 * skel_int1_qh = skelqh[7]. 611 * skel_int1_qh = skelqh[9].
596 * Add UHCI_NUMFRAMES to insure at least one bit is set. 612 * Add UHCI_NUMFRAMES to insure at least one bit is set.
597 */ 613 */
598 irq = 6 - (int) __ffs(i + UHCI_NUMFRAMES); 614 irq = 8 - (int) __ffs(i + UHCI_NUMFRAMES);
599 if (irq < 0) 615 if (irq <= 1)
600 irq = 7; 616 irq = 9;
601 617
602 /* Only place we don't use the frame list routines */ 618 /* Only place we don't use the frame list routines */
603 uhci->frame[i] = UHCI_PTR_QH | 619 uhci->frame[i] = UHCI_PTR_QH |
@@ -611,6 +627,7 @@ static int uhci_start(struct usb_hcd *hcd)
611 mb(); 627 mb();
612 628
613 configure_hc(uhci); 629 configure_hc(uhci);
630 uhci->is_initialized = 1;
614 start_rh(uhci); 631 start_rh(uhci);
615 return 0; 632 return 0;
616 633
@@ -767,13 +784,30 @@ static int uhci_resume(struct usb_hcd *hcd)
767} 784}
768#endif 785#endif
769 786
770/* Wait until all the URBs for a particular device/endpoint are gone */ 787/* Wait until a particular device/endpoint's QH is idle, and free it */
771static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd, 788static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
772 struct usb_host_endpoint *ep) 789 struct usb_host_endpoint *hep)
773{ 790{
774 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 791 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
792 struct uhci_qh *qh;
793
794 spin_lock_irq(&uhci->lock);
795 qh = (struct uhci_qh *) hep->hcpriv;
796 if (qh == NULL)
797 goto done;
798
799 while (qh->state != QH_STATE_IDLE) {
800 ++uhci->num_waiting;
801 spin_unlock_irq(&uhci->lock);
802 wait_event_interruptible(uhci->waitqh,
803 qh->state == QH_STATE_IDLE);
804 spin_lock_irq(&uhci->lock);
805 --uhci->num_waiting;
806 }
775 807
776 wait_event_interruptible(uhci->waitqh, list_empty(&ep->urb_list)); 808 uhci_free_qh(uhci, qh);
809done:
810 spin_unlock_irq(&uhci->lock);
777} 811}
778 812
779static int uhci_hcd_get_frame_number(struct usb_hcd *hcd) 813static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
@@ -857,16 +891,15 @@ static int __init uhci_hcd_init(void)
857 if (usb_disabled()) 891 if (usb_disabled())
858 return -ENODEV; 892 return -ENODEV;
859 893
860 if (debug) { 894 if (DEBUG_CONFIGURED) {
861 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL); 895 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
862 if (!errbuf) 896 if (!errbuf)
863 goto errbuf_failed; 897 goto errbuf_failed;
898 uhci_debugfs_root = debugfs_create_dir("uhci", NULL);
899 if (!uhci_debugfs_root)
900 goto debug_failed;
864 } 901 }
865 902
866 uhci_debugfs_root = debugfs_create_dir("uhci", NULL);
867 if (!uhci_debugfs_root)
868 goto debug_failed;
869
870 uhci_up_cachep = kmem_cache_create("uhci_urb_priv", 903 uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
871 sizeof(struct urb_priv), 0, 0, NULL, NULL); 904 sizeof(struct urb_priv), 0, 0, NULL, NULL);
872 if (!uhci_up_cachep) 905 if (!uhci_up_cachep)
diff --git a/drivers/usb/host/uhci-hcd.h b/drivers/usb/host/uhci-hcd.h
index 8b4b887a7d41..4a69c7eb09bd 100644
--- a/drivers/usb/host/uhci-hcd.h
+++ b/drivers/usb/host/uhci-hcd.h
@@ -28,8 +28,9 @@
28#define USBSTS_USBINT 0x0001 /* Interrupt due to IOC */ 28#define USBSTS_USBINT 0x0001 /* Interrupt due to IOC */
29#define USBSTS_ERROR 0x0002 /* Interrupt due to error */ 29#define USBSTS_ERROR 0x0002 /* Interrupt due to error */
30#define USBSTS_RD 0x0004 /* Resume Detect */ 30#define USBSTS_RD 0x0004 /* Resume Detect */
31#define USBSTS_HSE 0x0008 /* Host System Error - basically PCI problems */ 31#define USBSTS_HSE 0x0008 /* Host System Error: PCI problems */
32#define USBSTS_HCPE 0x0010 /* Host Controller Process Error - the scripts were buggy */ 32#define USBSTS_HCPE 0x0010 /* Host Controller Process Error:
33 * the schedule is buggy */
33#define USBSTS_HCH 0x0020 /* HC Halted */ 34#define USBSTS_HCH 0x0020 /* HC Halted */
34 35
35/* Interrupt enable register */ 36/* Interrupt enable register */
@@ -47,7 +48,8 @@
47/* USB port status and control registers */ 48/* USB port status and control registers */
48#define USBPORTSC1 16 49#define USBPORTSC1 16
49#define USBPORTSC2 18 50#define USBPORTSC2 18
50#define USBPORTSC_CCS 0x0001 /* Current Connect Status ("device present") */ 51#define USBPORTSC_CCS 0x0001 /* Current Connect Status
52 * ("device present") */
51#define USBPORTSC_CSC 0x0002 /* Connect Status Change */ 53#define USBPORTSC_CSC 0x0002 /* Connect Status Change */
52#define USBPORTSC_PE 0x0004 /* Port Enable */ 54#define USBPORTSC_PE 0x0004 /* Port Enable */
53#define USBPORTSC_PEC 0x0008 /* Port Enable Change */ 55#define USBPORTSC_PEC 0x0008 /* Port Enable Change */
@@ -71,15 +73,16 @@
71#define USBLEGSUP_RWC 0x8f00 /* the R/WC bits */ 73#define USBLEGSUP_RWC 0x8f00 /* the R/WC bits */
72#define USBLEGSUP_RO 0x5040 /* R/O and reserved bits */ 74#define USBLEGSUP_RO 0x5040 /* R/O and reserved bits */
73 75
74#define UHCI_PTR_BITS cpu_to_le32(0x000F) 76#define UHCI_PTR_BITS __constant_cpu_to_le32(0x000F)
75#define UHCI_PTR_TERM cpu_to_le32(0x0001) 77#define UHCI_PTR_TERM __constant_cpu_to_le32(0x0001)
76#define UHCI_PTR_QH cpu_to_le32(0x0002) 78#define UHCI_PTR_QH __constant_cpu_to_le32(0x0002)
77#define UHCI_PTR_DEPTH cpu_to_le32(0x0004) 79#define UHCI_PTR_DEPTH __constant_cpu_to_le32(0x0004)
78#define UHCI_PTR_BREADTH cpu_to_le32(0x0000) 80#define UHCI_PTR_BREADTH __constant_cpu_to_le32(0x0000)
79 81
80#define UHCI_NUMFRAMES 1024 /* in the frame list [array] */ 82#define UHCI_NUMFRAMES 1024 /* in the frame list [array] */
81#define UHCI_MAX_SOF_NUMBER 2047 /* in an SOF packet */ 83#define UHCI_MAX_SOF_NUMBER 2047 /* in an SOF packet */
82#define CAN_SCHEDULE_FRAMES 1000 /* how far future frames can be scheduled */ 84#define CAN_SCHEDULE_FRAMES 1000 /* how far in the future frames
85 * can be scheduled */
83 86
84 87
85/* 88/*
@@ -87,38 +90,59 @@
87 */ 90 */
88 91
89/* 92/*
90 * One role of a QH is to hold a queue of TDs for some endpoint. Each QH is 93 * One role of a QH is to hold a queue of TDs for some endpoint. One QH goes
91 * used with one URB, and qh->element (updated by the HC) is either: 94 * with each endpoint, and qh->element (updated by the HC) is either:
92 * - the next unprocessed TD for the URB, or 95 * - the next unprocessed TD in the endpoint's queue, or
93 * - UHCI_PTR_TERM (when there's no more traffic for this endpoint), or 96 * - UHCI_PTR_TERM (when there's no more traffic for this endpoint).
94 * - the QH for the next URB queued to the same endpoint.
95 * 97 *
96 * The other role of a QH is to serve as a "skeleton" framelist entry, so we 98 * The other role of a QH is to serve as a "skeleton" framelist entry, so we
97 * can easily splice a QH for some endpoint into the schedule at the right 99 * can easily splice a QH for some endpoint into the schedule at the right
98 * place. Then qh->element is UHCI_PTR_TERM. 100 * place. Then qh->element is UHCI_PTR_TERM.
99 * 101 *
100 * In the frame list, qh->link maintains a list of QHs seen by the HC: 102 * In the schedule, qh->link maintains a list of QHs seen by the HC:
101 * skel1 --> ep1-qh --> ep2-qh --> ... --> skel2 --> ... 103 * skel1 --> ep1-qh --> ep2-qh --> ... --> skel2 --> ...
104 *
105 * qh->node is the software equivalent of qh->link. The differences
106 * are that the software list is doubly-linked and QHs in the UNLINKING
107 * state are on the software list but not the hardware schedule.
108 *
109 * For bookkeeping purposes we maintain QHs even for Isochronous endpoints,
110 * but they never get added to the hardware schedule.
102 */ 111 */
112#define QH_STATE_IDLE 1 /* QH is not being used */
113#define QH_STATE_UNLINKING 2 /* QH has been removed from the
114 * schedule but the hardware may
115 * still be using it */
116#define QH_STATE_ACTIVE 3 /* QH is on the schedule */
117
103struct uhci_qh { 118struct uhci_qh {
104 /* Hardware fields */ 119 /* Hardware fields */
105 __le32 link; /* Next queue */ 120 __le32 link; /* Next QH in the schedule */
106 __le32 element; /* Queue element pointer */ 121 __le32 element; /* Queue element (TD) pointer */
107 122
108 /* Software fields */ 123 /* Software fields */
109 dma_addr_t dma_handle; 124 dma_addr_t dma_handle;
110 125
111 struct urb_priv *urbp; 126 struct list_head node; /* Node in the list of QHs */
127 struct usb_host_endpoint *hep; /* Endpoint information */
128 struct usb_device *udev;
129 struct list_head queue; /* Queue of urbps for this QH */
130 struct uhci_qh *skel; /* Skeleton for this QH */
131 struct uhci_td *dummy_td; /* Dummy TD to end the queue */
112 132
113 struct list_head list; 133 unsigned int unlink_frame; /* When the QH was unlinked */
114 struct list_head remove_list; 134 int state; /* QH_STATE_xxx; see above */
135
136 unsigned int initial_toggle:1; /* Endpoint's current toggle value */
137 unsigned int needs_fixup:1; /* Must fix the TD toggle values */
138 unsigned int is_stopped:1; /* Queue was stopped by an error */
115} __attribute__((aligned(16))); 139} __attribute__((aligned(16)));
116 140
117/* 141/*
118 * We need a special accessor for the element pointer because it is 142 * We need a special accessor for the element pointer because it is
119 * subject to asynchronous updates by the controller. 143 * subject to asynchronous updates by the controller.
120 */ 144 */
121static __le32 inline qh_element(struct uhci_qh *qh) { 145static inline __le32 qh_element(struct uhci_qh *qh) {
122 __le32 element = qh->element; 146 __le32 element = qh->element;
123 147
124 barrier(); 148 barrier();
@@ -149,11 +173,13 @@ static __le32 inline qh_element(struct uhci_qh *qh) {
149#define TD_CTRL_ACTLEN_MASK 0x7FF /* actual length, encoded as n - 1 */ 173#define TD_CTRL_ACTLEN_MASK 0x7FF /* actual length, encoded as n - 1 */
150 174
151#define TD_CTRL_ANY_ERROR (TD_CTRL_STALLED | TD_CTRL_DBUFERR | \ 175#define TD_CTRL_ANY_ERROR (TD_CTRL_STALLED | TD_CTRL_DBUFERR | \
152 TD_CTRL_BABBLE | TD_CTRL_CRCTIME | TD_CTRL_BITSTUFF) 176 TD_CTRL_BABBLE | TD_CTRL_CRCTIME | \
177 TD_CTRL_BITSTUFF)
153 178
154#define uhci_maxerr(err) ((err) << TD_CTRL_C_ERR_SHIFT) 179#define uhci_maxerr(err) ((err) << TD_CTRL_C_ERR_SHIFT)
155#define uhci_status_bits(ctrl_sts) ((ctrl_sts) & 0xF60000) 180#define uhci_status_bits(ctrl_sts) ((ctrl_sts) & 0xF60000)
156#define uhci_actual_length(ctrl_sts) (((ctrl_sts) + 1) & TD_CTRL_ACTLEN_MASK) /* 1-based */ 181#define uhci_actual_length(ctrl_sts) (((ctrl_sts) + 1) & \
182 TD_CTRL_ACTLEN_MASK) /* 1-based */
157 183
158/* 184/*
159 * for TD <info>: (a.k.a. Token) 185 * for TD <info>: (a.k.a. Token)
@@ -163,7 +189,7 @@ static __le32 inline qh_element(struct uhci_qh *qh) {
163#define TD_TOKEN_TOGGLE_SHIFT 19 189#define TD_TOKEN_TOGGLE_SHIFT 19
164#define TD_TOKEN_TOGGLE (1 << 19) 190#define TD_TOKEN_TOGGLE (1 << 19)
165#define TD_TOKEN_EXPLEN_SHIFT 21 191#define TD_TOKEN_EXPLEN_SHIFT 21
166#define TD_TOKEN_EXPLEN_MASK 0x7FF /* expected length, encoded as n - 1 */ 192#define TD_TOKEN_EXPLEN_MASK 0x7FF /* expected length, encoded as n-1 */
167#define TD_TOKEN_PID_MASK 0xFF 193#define TD_TOKEN_PID_MASK 0xFF
168 194
169#define uhci_explen(len) ((((len) - 1) & TD_TOKEN_EXPLEN_MASK) << \ 195#define uhci_explen(len) ((((len) - 1) & TD_TOKEN_EXPLEN_MASK) << \
@@ -187,7 +213,7 @@ static __le32 inline qh_element(struct uhci_qh *qh) {
187 * sw space after the TD entry. 213 * sw space after the TD entry.
188 * 214 *
189 * td->link points to either another TD (not necessarily for the same urb or 215 * td->link points to either another TD (not necessarily for the same urb or
190 * even the same endpoint), or nothing (PTR_TERM), or a QH (for queued urbs). 216 * even the same endpoint), or nothing (PTR_TERM), or a QH.
191 */ 217 */
192struct uhci_td { 218struct uhci_td {
193 /* Hardware fields */ 219 /* Hardware fields */
@@ -210,7 +236,7 @@ struct uhci_td {
210 * We need a special accessor for the control/status word because it is 236 * We need a special accessor for the control/status word because it is
211 * subject to asynchronous updates by the controller. 237 * subject to asynchronous updates by the controller.
212 */ 238 */
213static u32 inline td_status(struct uhci_td *td) { 239static inline u32 td_status(struct uhci_td *td) {
214 __le32 status = td->status; 240 __le32 status = td->status;
215 241
216 barrier(); 242 barrier();
@@ -223,17 +249,14 @@ static u32 inline td_status(struct uhci_td *td) {
223 */ 249 */
224 250
225/* 251/*
226 * The UHCI driver places Interrupt, Control and Bulk into QHs both 252 * The UHCI driver uses QHs with Interrupt, Control and Bulk URBs for
227 * to group together TDs for one transfer, and also to facilitate queuing 253 * automatic queuing. To make it easy to insert entries into the schedule,
228 * of URBs. To make it easy to insert entries into the schedule, we have 254 * we have a skeleton of QHs for each predefined Interrupt latency,
229 * a skeleton of QHs for each predefined Interrupt latency, low-speed 255 * low-speed control, full-speed control, bulk, and terminating QH
230 * control, full-speed control and terminating QH (see explanation for 256 * (see explanation for the terminating QH below).
231 * the terminating QH below).
232 * 257 *
233 * When we want to add a new QH, we add it to the end of the list for the 258 * When we want to add a new QH, we add it to the end of the list for the
234 * skeleton QH. 259 * skeleton QH. For instance, the schedule list can look like this:
235 *
236 * For instance, the queue can look like this:
237 * 260 *
238 * skel int128 QH 261 * skel int128 QH
239 * dev 1 interrupt QH 262 * dev 1 interrupt QH
@@ -256,26 +279,31 @@ static u32 inline td_status(struct uhci_td *td) {
256 * - To loop back to the full-speed control queue for full-speed bandwidth 279 * - To loop back to the full-speed control queue for full-speed bandwidth
257 * reclamation. 280 * reclamation.
258 * 281 *
259 * Isochronous transfers are stored before the start of the skeleton 282 * There's a special skeleton QH for Isochronous QHs. It never appears
260 * schedule and don't use QHs. While the UHCI spec doesn't forbid the 283 * on the schedule, and Isochronous TDs go on the schedule before the
261 * use of QHs for Isochronous, it doesn't use them either. And the spec 284 * the skeleton QHs. The hardware accesses them directly rather than
262 * says that queues never advance on an error completion status, which 285 * through their QH, which is used only for bookkeeping purposes.
263 * makes them totally unsuitable for Isochronous transfers. 286 * While the UHCI spec doesn't forbid the use of QHs for Isochronous,
287 * it doesn't use them either. And the spec says that queues never
288 * advance on an error completion status, which makes them totally
289 * unsuitable for Isochronous transfers.
264 */ 290 */
265 291
266#define UHCI_NUM_SKELQH 12 292#define UHCI_NUM_SKELQH 14
267#define skel_int128_qh skelqh[0] 293#define skel_unlink_qh skelqh[0]
268#define skel_int64_qh skelqh[1] 294#define skel_iso_qh skelqh[1]
269#define skel_int32_qh skelqh[2] 295#define skel_int128_qh skelqh[2]
270#define skel_int16_qh skelqh[3] 296#define skel_int64_qh skelqh[3]
271#define skel_int8_qh skelqh[4] 297#define skel_int32_qh skelqh[4]
272#define skel_int4_qh skelqh[5] 298#define skel_int16_qh skelqh[5]
273#define skel_int2_qh skelqh[6] 299#define skel_int8_qh skelqh[6]
274#define skel_int1_qh skelqh[7] 300#define skel_int4_qh skelqh[7]
275#define skel_ls_control_qh skelqh[8] 301#define skel_int2_qh skelqh[8]
276#define skel_fs_control_qh skelqh[9] 302#define skel_int1_qh skelqh[9]
277#define skel_bulk_qh skelqh[10] 303#define skel_ls_control_qh skelqh[10]
278#define skel_term_qh skelqh[11] 304#define skel_fs_control_qh skelqh[11]
305#define skel_bulk_qh skelqh[12]
306#define skel_term_qh skelqh[13]
279 307
280/* 308/*
281 * Search tree for determining where <interval> fits in the skelqh[] 309 * Search tree for determining where <interval> fits in the skelqh[]
@@ -293,21 +321,21 @@ static inline int __interval_to_skel(int interval)
293 if (interval < 16) { 321 if (interval < 16) {
294 if (interval < 4) { 322 if (interval < 4) {
295 if (interval < 2) 323 if (interval < 2)
296 return 7; /* int1 for 0-1 ms */ 324 return 9; /* int1 for 0-1 ms */
297 return 6; /* int2 for 2-3 ms */ 325 return 8; /* int2 for 2-3 ms */
298 } 326 }
299 if (interval < 8) 327 if (interval < 8)
300 return 5; /* int4 for 4-7 ms */ 328 return 7; /* int4 for 4-7 ms */
301 return 4; /* int8 for 8-15 ms */ 329 return 6; /* int8 for 8-15 ms */
302 } 330 }
303 if (interval < 64) { 331 if (interval < 64) {
304 if (interval < 32) 332 if (interval < 32)
305 return 3; /* int16 for 16-31 ms */ 333 return 5; /* int16 for 16-31 ms */
306 return 2; /* int32 for 32-63 ms */ 334 return 4; /* int32 for 32-63 ms */
307 } 335 }
308 if (interval < 128) 336 if (interval < 128)
309 return 1; /* int64 for 64-127 ms */ 337 return 3; /* int64 for 64-127 ms */
310 return 0; /* int128 for 128-255 ms (Max.) */ 338 return 2; /* int128 for 128-255 ms (Max.) */
311} 339}
312 340
313 341
@@ -360,15 +388,16 @@ struct uhci_hcd {
360 388
361 struct uhci_td *term_td; /* Terminating TD, see UHCI bug */ 389 struct uhci_td *term_td; /* Terminating TD, see UHCI bug */
362 struct uhci_qh *skelqh[UHCI_NUM_SKELQH]; /* Skeleton QHs */ 390 struct uhci_qh *skelqh[UHCI_NUM_SKELQH]; /* Skeleton QHs */
391 struct uhci_qh *next_qh; /* Next QH to scan */
363 392
364 spinlock_t lock; 393 spinlock_t lock;
365 394
366 dma_addr_t frame_dma_handle; /* Hardware frame list */ 395 dma_addr_t frame_dma_handle; /* Hardware frame list */
367 __le32 *frame; 396 __le32 *frame;
368 void **frame_cpu; /* CPU's frame list */ 397 void **frame_cpu; /* CPU's frame list */
369 398
370 int fsbr; /* Full-speed bandwidth reclamation */ 399 int fsbr; /* Full-speed bandwidth reclamation */
371 unsigned long fsbrtimeout; /* FSBR delay */ 400 unsigned long fsbrtimeout; /* FSBR delay */
372 401
373 enum uhci_rh_state rh_state; 402 enum uhci_rh_state rh_state;
374 unsigned long auto_stop_time; /* When to AUTO_STOP */ 403 unsigned long auto_stop_time; /* When to AUTO_STOP */
@@ -382,6 +411,7 @@ struct uhci_hcd {
382 unsigned int hc_inaccessible:1; /* HC is suspended or dead */ 411 unsigned int hc_inaccessible:1; /* HC is suspended or dead */
383 unsigned int working_RD:1; /* Suspended root hub doesn't 412 unsigned int working_RD:1; /* Suspended root hub doesn't
384 need to be polled */ 413 need to be polled */
414 unsigned int is_initialized:1; /* Data structure is usable */
385 415
386 /* Support for port suspend/resume/reset */ 416 /* Support for port suspend/resume/reset */
387 unsigned long port_c_suspend; /* Bit-arrays of ports */ 417 unsigned long port_c_suspend; /* Bit-arrays of ports */
@@ -389,27 +419,16 @@ struct uhci_hcd {
389 unsigned long resuming_ports; 419 unsigned long resuming_ports;
390 unsigned long ports_timeout; /* Time to stop signalling */ 420 unsigned long ports_timeout; /* Time to stop signalling */
391 421
392 /* Main list of URBs currently controlled by this HC */
393 struct list_head urb_list;
394
395 /* List of QHs that are done, but waiting to be unlinked (race) */
396 struct list_head qh_remove_list;
397 unsigned int qh_remove_age; /* Age in frames */
398
399 /* List of TDs that are done, but waiting to be freed (race) */ 422 /* List of TDs that are done, but waiting to be freed (race) */
400 struct list_head td_remove_list; 423 struct list_head td_remove_list;
401 unsigned int td_remove_age; /* Age in frames */ 424 unsigned int td_remove_age; /* Age in frames */
402 425
403 /* List of asynchronously unlinked URBs */ 426 struct list_head idle_qh_list; /* Where the idle QHs live */
404 struct list_head urb_remove_list;
405 unsigned int urb_remove_age; /* Age in frames */
406
407 /* List of URBs awaiting completion callback */
408 struct list_head complete_list;
409 427
410 int rh_numports; /* Number of root-hub ports */ 428 int rh_numports; /* Number of root-hub ports */
411 429
412 wait_queue_head_t waitqh; /* endpoint_disable waiters */ 430 wait_queue_head_t waitqh; /* endpoint_disable waiters */
431 int num_waiting; /* Number of waiters */
413}; 432};
414 433
415/* Convert between a usb_hcd pointer and the corresponding uhci_hcd */ 434/* Convert between a usb_hcd pointer and the corresponding uhci_hcd */
@@ -429,7 +448,7 @@ static inline struct usb_hcd *uhci_to_hcd(struct uhci_hcd *uhci)
429 * Private per-URB data 448 * Private per-URB data
430 */ 449 */
431struct urb_priv { 450struct urb_priv {
432 struct list_head urb_list; 451 struct list_head node; /* Node in the QH's urbp list */
433 452
434 struct urb *urb; 453 struct urb *urb;
435 454
@@ -437,15 +456,8 @@ struct urb_priv {
437 struct list_head td_list; 456 struct list_head td_list;
438 457
439 unsigned fsbr : 1; /* URB turned on FSBR */ 458 unsigned fsbr : 1; /* URB turned on FSBR */
440 unsigned fsbr_timeout : 1; /* URB timed out on FSBR */ 459 unsigned short_transfer : 1; /* URB got a short transfer, no
441 unsigned queued : 1; /* QH was queued (not linked in) */ 460 * need to rescan */
442 unsigned short_control_packet : 1; /* If we get a short packet during */
443 /* a control transfer, retrigger */
444 /* the status phase */
445
446 unsigned long fsbrtime; /* In jiffies */
447
448 struct list_head queue_list;
449}; 461};
450 462
451 463
diff --git a/drivers/usb/host/uhci-hub.c b/drivers/usb/host/uhci-hub.c
index a71e48a66805..152971d16769 100644
--- a/drivers/usb/host/uhci-hub.c
+++ b/drivers/usb/host/uhci-hub.c
@@ -99,6 +99,21 @@ static void uhci_finish_suspend(struct uhci_hcd *uhci, int port,
99 } 99 }
100} 100}
101 101
102/* Wait for the UHCI controller in HP's iLO2 server management chip.
103 * It can take up to 250 us to finish a reset and set the CSC bit.
104 */
105static void wait_for_HP(unsigned long port_addr)
106{
107 int i;
108
109 for (i = 10; i < 250; i += 10) {
110 if (inw(port_addr) & USBPORTSC_CSC)
111 return;
112 udelay(10);
113 }
114 /* Log a warning? */
115}
116
102static void uhci_check_ports(struct uhci_hcd *uhci) 117static void uhci_check_ports(struct uhci_hcd *uhci)
103{ 118{
104 unsigned int port; 119 unsigned int port;
@@ -113,6 +128,12 @@ static void uhci_check_ports(struct uhci_hcd *uhci)
113 CLR_RH_PORTSTAT(USBPORTSC_PR); 128 CLR_RH_PORTSTAT(USBPORTSC_PR);
114 udelay(10); 129 udelay(10);
115 130
131 /* HP's server management chip requires
132 * a longer delay. */
133 if (to_pci_dev(uhci_dev(uhci))->vendor ==
134 PCI_VENDOR_ID_HP)
135 wait_for_HP(port_addr);
136
116 /* If the port was enabled before, turning 137 /* If the port was enabled before, turning
117 * reset on caused a port enable change. 138 * reset on caused a port enable change.
118 * Turning reset off causes a port connect 139 * Turning reset off causes a port connect
diff --git a/drivers/usb/host/uhci-q.c b/drivers/usb/host/uhci-q.c
index 782398045f9f..a06d84c19e13 100644
--- a/drivers/usb/host/uhci-q.c
+++ b/drivers/usb/host/uhci-q.c
@@ -13,13 +13,9 @@
13 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface 13 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
14 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com). 14 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
15 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c) 15 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
16 * (C) Copyright 2004 Alan Stern, stern@rowland.harvard.edu 16 * (C) Copyright 2004-2005 Alan Stern, stern@rowland.harvard.edu
17 */ 17 */
18 18
19static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
20static void uhci_unlink_generic(struct uhci_hcd *uhci, struct urb *urb);
21static void uhci_remove_pending_urbps(struct uhci_hcd *uhci);
22static void uhci_free_pending_qhs(struct uhci_hcd *uhci);
23static void uhci_free_pending_tds(struct uhci_hcd *uhci); 19static void uhci_free_pending_tds(struct uhci_hcd *uhci);
24 20
25/* 21/*
@@ -30,7 +26,7 @@ static void uhci_free_pending_tds(struct uhci_hcd *uhci);
30 * games with the FSBR code to make sure we get the correct order in all 26 * games with the FSBR code to make sure we get the correct order in all
31 * the cases. I don't think it's worth the effort 27 * the cases. I don't think it's worth the effort
32 */ 28 */
33static inline void uhci_set_next_interrupt(struct uhci_hcd *uhci) 29static void uhci_set_next_interrupt(struct uhci_hcd *uhci)
34{ 30{
35 if (uhci->is_stopped) 31 if (uhci->is_stopped)
36 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies); 32 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
@@ -42,12 +38,6 @@ static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
42 uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC); 38 uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
43} 39}
44 40
45static inline void uhci_moveto_complete(struct uhci_hcd *uhci,
46 struct urb_priv *urbp)
47{
48 list_move_tail(&urbp->urb_list, &uhci->complete_list);
49}
50
51static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci) 41static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
52{ 42{
53 dma_addr_t dma_handle; 43 dma_addr_t dma_handle;
@@ -58,10 +48,6 @@ static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
58 return NULL; 48 return NULL;
59 49
60 td->dma_handle = dma_handle; 50 td->dma_handle = dma_handle;
61
62 td->link = UHCI_PTR_TERM;
63 td->buffer = 0;
64
65 td->frame = -1; 51 td->frame = -1;
66 52
67 INIT_LIST_HEAD(&td->list); 53 INIT_LIST_HEAD(&td->list);
@@ -71,6 +57,18 @@ static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
71 return td; 57 return td;
72} 58}
73 59
60static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
61{
62 if (!list_empty(&td->list))
63 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
64 if (!list_empty(&td->remove_list))
65 dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
66 if (!list_empty(&td->fl_list))
67 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
68
69 dma_pool_free(uhci->td_pool, td, td->dma_handle);
70}
71
74static inline void uhci_fill_td(struct uhci_td *td, u32 status, 72static inline void uhci_fill_td(struct uhci_td *td, u32 status,
75 u32 token, u32 buffer) 73 u32 token, u32 buffer)
76{ 74{
@@ -82,7 +80,8 @@ static inline void uhci_fill_td(struct uhci_td *td, u32 status,
82/* 80/*
83 * We insert Isochronous URBs directly into the frame list at the beginning 81 * We insert Isochronous URBs directly into the frame list at the beginning
84 */ 82 */
85static void uhci_insert_td_frame_list(struct uhci_hcd *uhci, struct uhci_td *td, unsigned framenum) 83static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
84 struct uhci_td *td, unsigned framenum)
86{ 85{
87 framenum &= (UHCI_NUMFRAMES - 1); 86 framenum &= (UHCI_NUMFRAMES - 1);
88 87
@@ -108,7 +107,7 @@ static void uhci_insert_td_frame_list(struct uhci_hcd *uhci, struct uhci_td *td,
108 } 107 }
109} 108}
110 109
111static inline void uhci_remove_td_frame_list(struct uhci_hcd *uhci, 110static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
112 struct uhci_td *td) 111 struct uhci_td *td)
113{ 112{
114 /* If it's not inserted, don't remove it */ 113 /* If it's not inserted, don't remove it */
@@ -139,48 +138,21 @@ static inline void uhci_remove_td_frame_list(struct uhci_hcd *uhci,
139 td->frame = -1; 138 td->frame = -1;
140} 139}
141 140
142static void unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb) 141/*
142 * Remove all the TDs for an Isochronous URB from the frame list
143 */
144static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
143{ 145{
144 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; 146 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
145 struct uhci_td *td; 147 struct uhci_td *td;
146 148
147 list_for_each_entry(td, &urbp->td_list, list) 149 list_for_each_entry(td, &urbp->td_list, list)
148 uhci_remove_td_frame_list(uhci, td); 150 uhci_remove_td_from_frame_list(uhci, td);
149 wmb(); 151 wmb();
150} 152}
151 153
152/* 154static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
153 * Inserts a td list into qh. 155 struct usb_device *udev, struct usb_host_endpoint *hep)
154 */
155static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct urb *urb, __le32 breadth)
156{
157 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
158 struct uhci_td *td;
159 __le32 *plink;
160
161 /* Ordering isn't important here yet since the QH hasn't been */
162 /* inserted into the schedule yet */
163 plink = &qh->element;
164 list_for_each_entry(td, &urbp->td_list, list) {
165 *plink = cpu_to_le32(td->dma_handle) | breadth;
166 plink = &td->link;
167 }
168 *plink = UHCI_PTR_TERM;
169}
170
171static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
172{
173 if (!list_empty(&td->list))
174 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
175 if (!list_empty(&td->remove_list))
176 dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
177 if (!list_empty(&td->fl_list))
178 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
179
180 dma_pool_free(uhci->td_pool, td, td->dma_handle);
181}
182
183static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci)
184{ 156{
185 dma_addr_t dma_handle; 157 dma_addr_t dma_handle;
186 struct uhci_qh *qh; 158 struct uhci_qh *qh;
@@ -194,256 +166,217 @@ static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci)
194 qh->element = UHCI_PTR_TERM; 166 qh->element = UHCI_PTR_TERM;
195 qh->link = UHCI_PTR_TERM; 167 qh->link = UHCI_PTR_TERM;
196 168
197 qh->urbp = NULL; 169 INIT_LIST_HEAD(&qh->queue);
198 170 INIT_LIST_HEAD(&qh->node);
199 INIT_LIST_HEAD(&qh->list);
200 INIT_LIST_HEAD(&qh->remove_list);
201 171
172 if (udev) { /* Normal QH */
173 qh->dummy_td = uhci_alloc_td(uhci);
174 if (!qh->dummy_td) {
175 dma_pool_free(uhci->qh_pool, qh, dma_handle);
176 return NULL;
177 }
178 qh->state = QH_STATE_IDLE;
179 qh->hep = hep;
180 qh->udev = udev;
181 hep->hcpriv = qh;
182
183 } else { /* Skeleton QH */
184 qh->state = QH_STATE_ACTIVE;
185 qh->udev = NULL;
186 }
202 return qh; 187 return qh;
203} 188}
204 189
205static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 190static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
206{ 191{
207 if (!list_empty(&qh->list)) 192 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
193 if (!list_empty(&qh->queue))
208 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh); 194 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
209 if (!list_empty(&qh->remove_list))
210 dev_warn(uhci_dev(uhci), "qh %p still in remove_list!\n", qh);
211 195
196 list_del(&qh->node);
197 if (qh->udev) {
198 qh->hep->hcpriv = NULL;
199 uhci_free_td(uhci, qh->dummy_td);
200 }
212 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle); 201 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
213} 202}
214 203
215/* 204/*
216 * Append this urb's qh after the last qh in skelqh->list 205 * When the currently executing URB is dequeued, save its current toggle value
217 *
218 * Note that urb_priv.queue_list doesn't have a separate queue head;
219 * it's a ring with every element "live".
220 */ 206 */
221static void uhci_insert_qh(struct uhci_hcd *uhci, struct uhci_qh *skelqh, struct urb *urb) 207static void uhci_save_toggle(struct uhci_qh *qh, struct urb *urb)
222{ 208{
223 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 209 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
224 struct urb_priv *turbp; 210 struct uhci_td *td;
225 struct uhci_qh *lqh;
226 211
227 /* Grab the last QH */ 212 /* If the QH element pointer is UHCI_PTR_TERM then then currently
228 lqh = list_entry(skelqh->list.prev, struct uhci_qh, list); 213 * executing URB has already been unlinked, so this one isn't it. */
214 if (qh_element(qh) == UHCI_PTR_TERM ||
215 qh->queue.next != &urbp->node)
216 return;
217 qh->element = UHCI_PTR_TERM;
229 218
230 /* Point to the next skelqh */ 219 /* Only bulk and interrupt pipes have to worry about toggles */
231 urbp->qh->link = lqh->link; 220 if (!(usb_pipetype(urb->pipe) == PIPE_BULK ||
232 wmb(); /* Ordering is important */ 221 usb_pipetype(urb->pipe) == PIPE_INTERRUPT))
222 return;
233 223
234 /* 224 /* Find the first active TD; that's the device's toggle state */
235 * Patch QHs for previous endpoint's queued URBs? HC goes 225 list_for_each_entry(td, &urbp->td_list, list) {
236 * here next, not to the next skelqh it now points to. 226 if (td_status(td) & TD_CTRL_ACTIVE) {
237 * 227 qh->needs_fixup = 1;
238 * lqh --> td ... --> qh ... --> td --> qh ... --> td 228 qh->initial_toggle = uhci_toggle(td_token(td));
239 * | | | 229 return;
240 * v v v 230 }
241 * +<----------------+-----------------+
242 * v
243 * newqh --> td ... --> td
244 * |
245 * v
246 * ...
247 *
248 * The HC could see (and use!) any of these as we write them.
249 */
250 lqh->link = cpu_to_le32(urbp->qh->dma_handle) | UHCI_PTR_QH;
251 if (lqh->urbp) {
252 list_for_each_entry(turbp, &lqh->urbp->queue_list, queue_list)
253 turbp->qh->link = lqh->link;
254 } 231 }
255 232
256 list_add_tail(&urbp->qh->list, &skelqh->list); 233 WARN_ON(1);
257} 234}
258 235
259/* 236/*
260 * Start removal of QH from schedule; it finishes next frame. 237 * Fix up the data toggles for URBs in a queue, when one of them
261 * TDs should be unlinked before this is called. 238 * terminates early (short transfer, error, or dequeued).
262 */ 239 */
263static void uhci_remove_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 240static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
264{ 241{
265 struct uhci_qh *pqh; 242 struct urb_priv *urbp = NULL;
266 __le32 newlink; 243 struct uhci_td *td;
267 244 unsigned int toggle = qh->initial_toggle;
268 if (!qh) 245 unsigned int pipe;
269 return; 246
270 247 /* Fixups for a short transfer start with the second URB in the
271 /* 248 * queue (the short URB is the first). */
272 * Only go through the hoops if it's actually linked in 249 if (skip_first)
273 */ 250 urbp = list_entry(qh->queue.next, struct urb_priv, node);
274 if (!list_empty(&qh->list)) { 251
275 252 /* When starting with the first URB, if the QH element pointer is
276 /* If our queue is nonempty, make the next URB the head */ 253 * still valid then we know the URB's toggles are okay. */
277 if (!list_empty(&qh->urbp->queue_list)) { 254 else if (qh_element(qh) != UHCI_PTR_TERM)
278 struct urb_priv *nurbp; 255 toggle = 2;
279 256
280 nurbp = list_entry(qh->urbp->queue_list.next, 257 /* Fix up the toggle for the URBs in the queue. Normally this
281 struct urb_priv, queue_list); 258 * loop won't run more than once: When an error or short transfer
282 nurbp->queued = 0; 259 * occurs, the queue usually gets emptied. */
283 list_add(&nurbp->qh->list, &qh->list); 260 urbp = list_prepare_entry(urbp, &qh->queue, node);
284 newlink = cpu_to_le32(nurbp->qh->dma_handle) | UHCI_PTR_QH; 261 list_for_each_entry_continue(urbp, &qh->queue, node) {
285 } else 262
286 newlink = qh->link; 263 /* If the first TD has the right toggle value, we don't
287 264 * need to change any toggles in this URB */
288 /* Fix up the previous QH's queue to link to either 265 td = list_entry(urbp->td_list.next, struct uhci_td, list);
289 * the new head of this queue or the start of the 266 if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
290 * next endpoint's queue. */ 267 td = list_entry(urbp->td_list.next, struct uhci_td,
291 pqh = list_entry(qh->list.prev, struct uhci_qh, list); 268 list);
292 pqh->link = newlink; 269 toggle = uhci_toggle(td_token(td)) ^ 1;
293 if (pqh->urbp) { 270
294 struct urb_priv *turbp; 271 /* Otherwise all the toggles in the URB have to be switched */
295 272 } else {
296 list_for_each_entry(turbp, &pqh->urbp->queue_list, 273 list_for_each_entry(td, &urbp->td_list, list) {
297 queue_list) 274 td->token ^= __constant_cpu_to_le32(
298 turbp->qh->link = newlink; 275 TD_TOKEN_TOGGLE);
276 toggle ^= 1;
277 }
299 } 278 }
300 wmb();
301
302 /* Leave qh->link in case the HC is on the QH now, it will */
303 /* continue the rest of the schedule */
304 qh->element = UHCI_PTR_TERM;
305
306 list_del_init(&qh->list);
307 }
308
309 list_del_init(&qh->urbp->queue_list);
310 qh->urbp = NULL;
311
312 uhci_get_current_frame_number(uhci);
313 if (uhci->frame_number + uhci->is_stopped != uhci->qh_remove_age) {
314 uhci_free_pending_qhs(uhci);
315 uhci->qh_remove_age = uhci->frame_number;
316 } 279 }
317 280
318 /* Check to see if the remove list is empty. Set the IOC bit */ 281 wmb();
319 /* to force an interrupt so we can remove the QH */ 282 pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
320 if (list_empty(&uhci->qh_remove_list)) 283 usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
321 uhci_set_next_interrupt(uhci); 284 usb_pipeout(pipe), toggle);
322 285 qh->needs_fixup = 0;
323 list_add(&qh->remove_list, &uhci->qh_remove_list);
324} 286}
325 287
326static int uhci_fixup_toggle(struct urb *urb, unsigned int toggle) 288/*
289 * Put a QH on the schedule in both hardware and software
290 */
291static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
327{ 292{
328 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 293 struct uhci_qh *pqh;
329 struct uhci_td *td;
330
331 list_for_each_entry(td, &urbp->td_list, list) {
332 if (toggle)
333 td->token |= cpu_to_le32(TD_TOKEN_TOGGLE);
334 else
335 td->token &= ~cpu_to_le32(TD_TOKEN_TOGGLE);
336
337 toggle ^= 1;
338 }
339
340 return toggle;
341}
342 294
343/* This function will append one URB's QH to another URB's QH. This is for */ 295 WARN_ON(list_empty(&qh->queue));
344/* queuing interrupt, control or bulk transfers */
345static void uhci_append_queued_urb(struct uhci_hcd *uhci, struct urb *eurb, struct urb *urb)
346{
347 struct urb_priv *eurbp, *urbp, *furbp, *lurbp;
348 struct uhci_td *lltd;
349 296
350 eurbp = eurb->hcpriv; 297 /* Set the element pointer if it isn't set already.
351 urbp = urb->hcpriv; 298 * This isn't needed for Isochronous queues, but it doesn't hurt. */
299 if (qh_element(qh) == UHCI_PTR_TERM) {
300 struct urb_priv *urbp = list_entry(qh->queue.next,
301 struct urb_priv, node);
302 struct uhci_td *td = list_entry(urbp->td_list.next,
303 struct uhci_td, list);
352 304
353 /* Find the first URB in the queue */ 305 qh->element = cpu_to_le32(td->dma_handle);
354 furbp = eurbp;
355 if (eurbp->queued) {
356 list_for_each_entry(furbp, &eurbp->queue_list, queue_list)
357 if (!furbp->queued)
358 break;
359 } 306 }
360 307
361 lurbp = list_entry(furbp->queue_list.prev, struct urb_priv, queue_list); 308 if (qh->state == QH_STATE_ACTIVE)
362 309 return;
363 lltd = list_entry(lurbp->td_list.prev, struct uhci_td, list); 310 qh->state = QH_STATE_ACTIVE;
364 311
365 /* Control transfers always start with toggle 0 */ 312 /* Move the QH from its old list to the end of the appropriate
366 if (!usb_pipecontrol(urb->pipe)) 313 * skeleton's list */
367 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), 314 if (qh == uhci->next_qh)
368 usb_pipeout(urb->pipe), 315 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
369 uhci_fixup_toggle(urb, 316 node);
370 uhci_toggle(td_token(lltd)) ^ 1)); 317 list_move_tail(&qh->node, &qh->skel->node);
371 318
372 /* All qhs in the queue need to link to the next queue */ 319 /* Link it into the schedule */
373 urbp->qh->link = eurbp->qh->link; 320 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
374 321 qh->link = pqh->link;
375 wmb(); /* Make sure we flush everything */ 322 wmb();
376 323 pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
377 lltd->link = cpu_to_le32(urbp->qh->dma_handle) | UHCI_PTR_QH;
378
379 list_add_tail(&urbp->queue_list, &furbp->queue_list);
380
381 urbp->queued = 1;
382} 324}
383 325
384static void uhci_delete_queued_urb(struct uhci_hcd *uhci, struct urb *urb) 326/*
327 * Take a QH off the hardware schedule
328 */
329static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
385{ 330{
386 struct urb_priv *urbp, *nurbp, *purbp, *turbp; 331 struct uhci_qh *pqh;
387 struct uhci_td *pltd;
388 unsigned int toggle;
389
390 urbp = urb->hcpriv;
391 332
392 if (list_empty(&urbp->queue_list)) 333 if (qh->state == QH_STATE_UNLINKING)
393 return; 334 return;
335 WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
336 qh->state = QH_STATE_UNLINKING;
394 337
395 nurbp = list_entry(urbp->queue_list.next, struct urb_priv, queue_list); 338 /* Unlink the QH from the schedule and record when we did it */
339 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
340 pqh->link = qh->link;
341 mb();
396 342
397 /* 343 uhci_get_current_frame_number(uhci);
398 * Fix up the toggle for the following URBs in the queue. 344 qh->unlink_frame = uhci->frame_number;
399 * Only needed for bulk and interrupt: control and isochronous
400 * endpoints don't propagate toggles between messages.
401 */
402 if (usb_pipebulk(urb->pipe) || usb_pipeint(urb->pipe)) {
403 if (!urbp->queued)
404 /* We just set the toggle in uhci_unlink_generic */
405 toggle = usb_gettoggle(urb->dev,
406 usb_pipeendpoint(urb->pipe),
407 usb_pipeout(urb->pipe));
408 else {
409 /* If we're in the middle of the queue, grab the */
410 /* toggle from the TD previous to us */
411 purbp = list_entry(urbp->queue_list.prev,
412 struct urb_priv, queue_list);
413 pltd = list_entry(purbp->td_list.prev,
414 struct uhci_td, list);
415 toggle = uhci_toggle(td_token(pltd)) ^ 1;
416 }
417 345
418 list_for_each_entry(turbp, &urbp->queue_list, queue_list) { 346 /* Force an interrupt so we know when the QH is fully unlinked */
419 if (!turbp->queued) 347 if (list_empty(&uhci->skel_unlink_qh->node))
420 break; 348 uhci_set_next_interrupt(uhci);
421 toggle = uhci_fixup_toggle(turbp->urb, toggle);
422 }
423 349
424 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), 350 /* Move the QH from its old list to the end of the unlinking list */
425 usb_pipeout(urb->pipe), toggle); 351 if (qh == uhci->next_qh)
426 } 352 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
353 node);
354 list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
355}
427 356
428 if (urbp->queued) { 357/*
429 /* We're somewhere in the middle (or end). The case where 358 * When we and the controller are through with a QH, it becomes IDLE.
430 * we're at the head is handled in uhci_remove_qh(). */ 359 * This happens when a QH has been off the schedule (on the unlinking
431 purbp = list_entry(urbp->queue_list.prev, struct urb_priv, 360 * list) for more than one frame, or when an error occurs while adding
432 queue_list); 361 * the first URB onto a new QH.
362 */
363static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
364{
365 WARN_ON(qh->state == QH_STATE_ACTIVE);
433 366
434 pltd = list_entry(purbp->td_list.prev, struct uhci_td, list); 367 if (qh == uhci->next_qh)
435 if (nurbp->queued) 368 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
436 pltd->link = cpu_to_le32(nurbp->qh->dma_handle) | UHCI_PTR_QH; 369 node);
437 else 370 list_move(&qh->node, &uhci->idle_qh_list);
438 /* The next URB happens to be the beginning, so */ 371 qh->state = QH_STATE_IDLE;
439 /* we're the last, end the chain */
440 pltd->link = UHCI_PTR_TERM;
441 }
442 372
443 /* urbp->queue_list is handled in uhci_remove_qh() */ 373 /* If anyone is waiting for a QH to become idle, wake them up */
374 if (uhci->num_waiting)
375 wake_up_all(&uhci->waitqh);
444} 376}
445 377
446static struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, struct urb *urb) 378static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
379 struct urb *urb)
447{ 380{
448 struct urb_priv *urbp; 381 struct urb_priv *urbp;
449 382
@@ -453,16 +386,11 @@ static struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, struct urb *u
453 386
454 memset((void *)urbp, 0, sizeof(*urbp)); 387 memset((void *)urbp, 0, sizeof(*urbp));
455 388
456 urbp->fsbrtime = jiffies;
457 urbp->urb = urb; 389 urbp->urb = urb;
390 urb->hcpriv = urbp;
458 391
392 INIT_LIST_HEAD(&urbp->node);
459 INIT_LIST_HEAD(&urbp->td_list); 393 INIT_LIST_HEAD(&urbp->td_list);
460 INIT_LIST_HEAD(&urbp->queue_list);
461 INIT_LIST_HEAD(&urbp->urb_list);
462
463 list_add_tail(&urbp->urb_list, &uhci->urb_list);
464
465 urb->hcpriv = urbp;
466 394
467 return urbp; 395 return urbp;
468} 396}
@@ -482,18 +410,14 @@ static void uhci_remove_td_from_urb(struct uhci_td *td)
482 list_del_init(&td->list); 410 list_del_init(&td->list);
483} 411}
484 412
485static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb) 413static void uhci_free_urb_priv(struct uhci_hcd *uhci,
414 struct urb_priv *urbp)
486{ 415{
487 struct uhci_td *td, *tmp; 416 struct uhci_td *td, *tmp;
488 struct urb_priv *urbp;
489
490 urbp = (struct urb_priv *)urb->hcpriv;
491 if (!urbp)
492 return;
493 417
494 if (!list_empty(&urbp->urb_list)) 418 if (!list_empty(&urbp->node))
495 dev_warn(uhci_dev(uhci), "urb %p still on uhci->urb_list " 419 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
496 "or uhci->remove_list!\n", urb); 420 urbp->urb);
497 421
498 uhci_get_current_frame_number(uhci); 422 uhci_get_current_frame_number(uhci);
499 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) { 423 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) {
@@ -502,7 +426,7 @@ static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb)
502 } 426 }
503 427
504 /* Check to see if the remove list is empty. Set the IOC bit */ 428 /* Check to see if the remove list is empty. Set the IOC bit */
505 /* to force an interrupt so we can remove the TDs*/ 429 /* to force an interrupt so we can remove the TDs. */
506 if (list_empty(&uhci->td_remove_list)) 430 if (list_empty(&uhci->td_remove_list))
507 uhci_set_next_interrupt(uhci); 431 uhci_set_next_interrupt(uhci);
508 432
@@ -511,7 +435,7 @@ static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb)
511 list_add(&td->remove_list, &uhci->td_remove_list); 435 list_add(&td->remove_list, &uhci->td_remove_list);
512 } 436 }
513 437
514 urb->hcpriv = NULL; 438 urbp->urb->hcpriv = NULL;
515 kmem_cache_free(uhci_up_cachep, urbp); 439 kmem_cache_free(uhci_up_cachep, urbp);
516} 440}
517 441
@@ -570,34 +494,33 @@ static int uhci_map_status(int status, int dir_out)
570/* 494/*
571 * Control transfers 495 * Control transfers
572 */ 496 */
573static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 497static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
498 struct uhci_qh *qh)
574{ 499{
575 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
576 struct uhci_td *td; 500 struct uhci_td *td;
577 struct uhci_qh *qh, *skelqh;
578 unsigned long destination, status; 501 unsigned long destination, status;
579 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)); 502 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
580 int len = urb->transfer_buffer_length; 503 int len = urb->transfer_buffer_length;
581 dma_addr_t data = urb->transfer_dma; 504 dma_addr_t data = urb->transfer_dma;
505 __le32 *plink;
582 506
583 /* The "pipe" thing contains the destination in bits 8--18 */ 507 /* The "pipe" thing contains the destination in bits 8--18 */
584 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP; 508 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
585 509
586 /* 3 errors */ 510 /* 3 errors, dummy TD remains inactive */
587 status = TD_CTRL_ACTIVE | uhci_maxerr(3); 511 status = uhci_maxerr(3);
588 if (urb->dev->speed == USB_SPEED_LOW) 512 if (urb->dev->speed == USB_SPEED_LOW)
589 status |= TD_CTRL_LS; 513 status |= TD_CTRL_LS;
590 514
591 /* 515 /*
592 * Build the TD for the control request setup packet 516 * Build the TD for the control request setup packet
593 */ 517 */
594 td = uhci_alloc_td(uhci); 518 td = qh->dummy_td;
595 if (!td)
596 return -ENOMEM;
597
598 uhci_add_td_to_urb(urb, td); 519 uhci_add_td_to_urb(urb, td);
599 uhci_fill_td(td, status, destination | uhci_explen(8), 520 uhci_fill_td(td, status, destination | uhci_explen(8),
600 urb->setup_dma); 521 urb->setup_dma);
522 plink = &td->link;
523 status |= TD_CTRL_ACTIVE;
601 524
602 /* 525 /*
603 * If direction is "send", change the packet ID from SETUP (0x2D) 526 * If direction is "send", change the packet ID from SETUP (0x2D)
@@ -615,21 +538,20 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
615 * Build the DATA TDs 538 * Build the DATA TDs
616 */ 539 */
617 while (len > 0) { 540 while (len > 0) {
618 int pktsze = len; 541 int pktsze = min(len, maxsze);
619
620 if (pktsze > maxsze)
621 pktsze = maxsze;
622 542
623 td = uhci_alloc_td(uhci); 543 td = uhci_alloc_td(uhci);
624 if (!td) 544 if (!td)
625 return -ENOMEM; 545 goto nomem;
546 *plink = cpu_to_le32(td->dma_handle);
626 547
627 /* Alternate Data0/1 (start with Data1) */ 548 /* Alternate Data0/1 (start with Data1) */
628 destination ^= TD_TOKEN_TOGGLE; 549 destination ^= TD_TOKEN_TOGGLE;
629 550
630 uhci_add_td_to_urb(urb, td); 551 uhci_add_td_to_urb(urb, td);
631 uhci_fill_td(td, status, destination | uhci_explen(pktsze), 552 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
632 data); 553 data);
554 plink = &td->link;
633 555
634 data += pktsze; 556 data += pktsze;
635 len -= pktsze; 557 len -= pktsze;
@@ -640,7 +562,8 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
640 */ 562 */
641 td = uhci_alloc_td(uhci); 563 td = uhci_alloc_td(uhci);
642 if (!td) 564 if (!td)
643 return -ENOMEM; 565 goto nomem;
566 *plink = cpu_to_le32(td->dma_handle);
644 567
645 /* 568 /*
646 * It's IN if the pipe is an output pipe or we're not expecting 569 * It's IN if the pipe is an output pipe or we're not expecting
@@ -658,16 +581,21 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
658 581
659 uhci_add_td_to_urb(urb, td); 582 uhci_add_td_to_urb(urb, td);
660 uhci_fill_td(td, status | TD_CTRL_IOC, 583 uhci_fill_td(td, status | TD_CTRL_IOC,
661 destination | uhci_explen(0), 0); 584 destination | uhci_explen(0), 0);
662 585 plink = &td->link;
663 qh = uhci_alloc_qh(uhci);
664 if (!qh)
665 return -ENOMEM;
666 586
667 urbp->qh = qh; 587 /*
668 qh->urbp = urbp; 588 * Build the new dummy TD and activate the old one
589 */
590 td = uhci_alloc_td(uhci);
591 if (!td)
592 goto nomem;
593 *plink = cpu_to_le32(td->dma_handle);
669 594
670 uhci_insert_tds_in_qh(qh, urb, UHCI_PTR_BREADTH); 595 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
596 wmb();
597 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
598 qh->dummy_td = td;
671 599
672 /* Low-speed transfers get a different queue, and won't hog the bus. 600 /* Low-speed transfers get a different queue, and won't hog the bus.
673 * Also, some devices enumerate better without FSBR; the easiest way 601 * Also, some devices enumerate better without FSBR; the easiest way
@@ -675,18 +603,17 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
675 * isn't in the CONFIGURED state. */ 603 * isn't in the CONFIGURED state. */
676 if (urb->dev->speed == USB_SPEED_LOW || 604 if (urb->dev->speed == USB_SPEED_LOW ||
677 urb->dev->state != USB_STATE_CONFIGURED) 605 urb->dev->state != USB_STATE_CONFIGURED)
678 skelqh = uhci->skel_ls_control_qh; 606 qh->skel = uhci->skel_ls_control_qh;
679 else { 607 else {
680 skelqh = uhci->skel_fs_control_qh; 608 qh->skel = uhci->skel_fs_control_qh;
681 uhci_inc_fsbr(uhci, urb); 609 uhci_inc_fsbr(uhci, urb);
682 } 610 }
611 return 0;
683 612
684 if (eurb) 613nomem:
685 uhci_append_queued_urb(uhci, eurb, urb); 614 /* Remove the dummy TD from the td_list so it doesn't get freed */
686 else 615 uhci_remove_td_from_urb(qh->dummy_td);
687 uhci_insert_qh(uhci, skelqh, urb); 616 return -ENOMEM;
688
689 return -EINPROGRESS;
690} 617}
691 618
692/* 619/*
@@ -703,7 +630,7 @@ static int usb_control_retrigger_status(struct uhci_hcd *uhci, struct urb *urb)
703 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 630 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
704 struct uhci_td *td; 631 struct uhci_td *td;
705 632
706 urbp->short_control_packet = 1; 633 urbp->short_transfer = 1;
707 634
708 td = list_entry(urbp->td_list.prev, struct uhci_td, list); 635 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
709 urbp->qh->element = cpu_to_le32(td->dma_handle); 636 urbp->qh->element = cpu_to_le32(td->dma_handle);
@@ -720,16 +647,14 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
720 unsigned int status; 647 unsigned int status;
721 int ret = 0; 648 int ret = 0;
722 649
723 if (list_empty(&urbp->td_list))
724 return -EINVAL;
725
726 head = &urbp->td_list; 650 head = &urbp->td_list;
727 651 if (urbp->short_transfer) {
728 if (urbp->short_control_packet) {
729 tmp = head->prev; 652 tmp = head->prev;
730 goto status_stage; 653 goto status_stage;
731 } 654 }
732 655
656 urb->actual_length = 0;
657
733 tmp = head->next; 658 tmp = head->next;
734 td = list_entry(tmp, struct uhci_td, list); 659 td = list_entry(tmp, struct uhci_td, list);
735 660
@@ -742,8 +667,6 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
742 if (status) 667 if (status)
743 goto td_error; 668 goto td_error;
744 669
745 urb->actual_length = 0;
746
747 /* The rest of the TDs (but the last) are data */ 670 /* The rest of the TDs (but the last) are data */
748 tmp = tmp->next; 671 tmp = tmp->next;
749 while (tmp != head && tmp->next != head) { 672 while (tmp != head && tmp->next != head) {
@@ -770,10 +693,7 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
770 goto err; 693 goto err;
771 } 694 }
772 695
773 if (uhci_packetid(td_token(td)) == USB_PID_IN) 696 return usb_control_retrigger_status(uhci, urb);
774 return usb_control_retrigger_status(uhci, urb);
775 else
776 return 0;
777 } 697 }
778 } 698 }
779 699
@@ -814,34 +734,40 @@ err:
814 if (errbuf) { 734 if (errbuf) {
815 /* Print the chain for debugging purposes */ 735 /* Print the chain for debugging purposes */
816 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0); 736 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
817
818 lprintk(errbuf); 737 lprintk(errbuf);
819 } 738 }
820 } 739 }
821 740
741 /* Note that the queue has stopped */
742 urbp->qh->element = UHCI_PTR_TERM;
743 urbp->qh->is_stopped = 1;
822 return ret; 744 return ret;
823} 745}
824 746
825/* 747/*
826 * Common submit for bulk and interrupt 748 * Common submit for bulk and interrupt
827 */ 749 */
828static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb, struct uhci_qh *skelqh) 750static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
751 struct uhci_qh *qh)
829{ 752{
830 struct uhci_td *td; 753 struct uhci_td *td;
831 struct uhci_qh *qh;
832 unsigned long destination, status; 754 unsigned long destination, status;
833 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)); 755 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
834 int len = urb->transfer_buffer_length; 756 int len = urb->transfer_buffer_length;
835 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
836 dma_addr_t data = urb->transfer_dma; 757 dma_addr_t data = urb->transfer_dma;
758 __le32 *plink;
759 unsigned int toggle;
837 760
838 if (len < 0) 761 if (len < 0)
839 return -EINVAL; 762 return -EINVAL;
840 763
841 /* The "pipe" thing contains the destination in bits 8--18 */ 764 /* The "pipe" thing contains the destination in bits 8--18 */
842 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe); 765 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
766 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
767 usb_pipeout(urb->pipe));
843 768
844 status = uhci_maxerr(3) | TD_CTRL_ACTIVE; 769 /* 3 errors, dummy TD remains inactive */
770 status = uhci_maxerr(3);
845 if (urb->dev->speed == USB_SPEED_LOW) 771 if (urb->dev->speed == USB_SPEED_LOW)
846 status |= TD_CTRL_LS; 772 status |= TD_CTRL_LS;
847 if (usb_pipein(urb->pipe)) 773 if (usb_pipein(urb->pipe))
@@ -850,30 +776,34 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
850 /* 776 /*
851 * Build the DATA TDs 777 * Build the DATA TDs
852 */ 778 */
779 plink = NULL;
780 td = qh->dummy_td;
853 do { /* Allow zero length packets */ 781 do { /* Allow zero length packets */
854 int pktsze = maxsze; 782 int pktsze = maxsze;
855 783
856 if (pktsze >= len) { 784 if (len <= pktsze) { /* The last packet */
857 pktsze = len; 785 pktsze = len;
858 if (!(urb->transfer_flags & URB_SHORT_NOT_OK)) 786 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
859 status &= ~TD_CTRL_SPD; 787 status &= ~TD_CTRL_SPD;
860 } 788 }
861 789
862 td = uhci_alloc_td(uhci); 790 if (plink) {
863 if (!td) 791 td = uhci_alloc_td(uhci);
864 return -ENOMEM; 792 if (!td)
865 793 goto nomem;
794 *plink = cpu_to_le32(td->dma_handle);
795 }
866 uhci_add_td_to_urb(urb, td); 796 uhci_add_td_to_urb(urb, td);
867 uhci_fill_td(td, status, destination | uhci_explen(pktsze) | 797 uhci_fill_td(td, status,
868 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), 798 destination | uhci_explen(pktsze) |
869 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE_SHIFT), 799 (toggle << TD_TOKEN_TOGGLE_SHIFT),
870 data); 800 data);
801 plink = &td->link;
802 status |= TD_CTRL_ACTIVE;
871 803
872 data += pktsze; 804 data += pktsze;
873 len -= maxsze; 805 len -= maxsze;
874 806 toggle ^= 1;
875 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe),
876 usb_pipeout(urb->pipe));
877 } while (len > 0); 807 } while (len > 0);
878 808
879 /* 809 /*
@@ -883,20 +813,22 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
883 * however, if transfer_length == 0, the zero packet was already 813 * however, if transfer_length == 0, the zero packet was already
884 * prepared above. 814 * prepared above.
885 */ 815 */
886 if (usb_pipeout(urb->pipe) && (urb->transfer_flags & URB_ZERO_PACKET) && 816 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
887 !len && urb->transfer_buffer_length) { 817 usb_pipeout(urb->pipe) && len == 0 &&
818 urb->transfer_buffer_length > 0) {
888 td = uhci_alloc_td(uhci); 819 td = uhci_alloc_td(uhci);
889 if (!td) 820 if (!td)
890 return -ENOMEM; 821 goto nomem;
822 *plink = cpu_to_le32(td->dma_handle);
891 823
892 uhci_add_td_to_urb(urb, td); 824 uhci_add_td_to_urb(urb, td);
893 uhci_fill_td(td, status, destination | uhci_explen(0) | 825 uhci_fill_td(td, status,
894 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), 826 destination | uhci_explen(0) |
895 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE_SHIFT), 827 (toggle << TD_TOKEN_TOGGLE_SHIFT),
896 data); 828 data);
829 plink = &td->link;
897 830
898 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), 831 toggle ^= 1;
899 usb_pipeout(urb->pipe));
900 } 832 }
901 833
902 /* Set the interrupt-on-completion flag on the last packet. 834 /* Set the interrupt-on-completion flag on the last packet.
@@ -905,24 +837,29 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
905 * fast side but not enough to justify delaying an interrupt 837 * fast side but not enough to justify delaying an interrupt
906 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT 838 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
907 * flag setting. */ 839 * flag setting. */
908 td->status |= cpu_to_le32(TD_CTRL_IOC); 840 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
909 841
910 qh = uhci_alloc_qh(uhci); 842 /*
911 if (!qh) 843 * Build the new dummy TD and activate the old one
912 return -ENOMEM; 844 */
913 845 td = uhci_alloc_td(uhci);
914 urbp->qh = qh; 846 if (!td)
915 qh->urbp = urbp; 847 goto nomem;
848 *plink = cpu_to_le32(td->dma_handle);
916 849
917 /* Always breadth first */ 850 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
918 uhci_insert_tds_in_qh(qh, urb, UHCI_PTR_BREADTH); 851 wmb();
852 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
853 qh->dummy_td = td;
919 854
920 if (eurb) 855 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
921 uhci_append_queued_urb(uhci, eurb, urb); 856 usb_pipeout(urb->pipe), toggle);
922 else 857 return 0;
923 uhci_insert_qh(uhci, skelqh, urb);
924 858
925 return -EINPROGRESS; 859nomem:
860 /* Remove the dummy TD from the td_list so it doesn't get freed */
861 uhci_remove_td_from_urb(qh->dummy_td);
862 return -ENOMEM;
926} 863}
927 864
928/* 865/*
@@ -954,8 +891,27 @@ static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
954 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 891 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
955 ret = -EREMOTEIO; 892 ret = -EREMOTEIO;
956 goto err; 893 goto err;
957 } else 894 }
958 return 0; 895
896 /*
897 * This URB stopped short of its end. We have to
898 * fix up the toggles of the following URBs on the
899 * queue and restart the queue.
900 *
901 * Do this only the first time we encounter the
902 * short URB.
903 */
904 if (!urbp->short_transfer) {
905 urbp->short_transfer = 1;
906 urbp->qh->initial_toggle =
907 uhci_toggle(td_token(td)) ^ 1;
908 uhci_fixup_toggles(urbp->qh, 1);
909
910 td = list_entry(urbp->td_list.prev,
911 struct uhci_td, list);
912 urbp->qh->element = td->link;
913 }
914 break;
959 } 915 }
960 } 916 }
961 917
@@ -964,31 +920,30 @@ static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
964td_error: 920td_error:
965 ret = uhci_map_status(status, uhci_packetout(td_token(td))); 921 ret = uhci_map_status(status, uhci_packetout(td_token(td)));
966 922
967err:
968 /*
969 * Enable this chunk of code if you want to see some more debugging.
970 * But be careful, it has the tendancy to starve out khubd and prevent
971 * disconnects from happening successfully if you have a slow debug
972 * log interface (like a serial console.
973 */
974#if 0
975 if ((debug == 1 && ret != -EPIPE) || debug > 1) { 923 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
976 /* Some debugging code */ 924 /* Some debugging code */
977 dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n", 925 dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n",
978 __FUNCTION__, status); 926 __FUNCTION__, status);
979 927
980 if (errbuf) { 928 if (debug > 1 && errbuf) {
981 /* Print the chain for debugging purposes */ 929 /* Print the chain for debugging purposes */
982 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0); 930 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
983
984 lprintk(errbuf); 931 lprintk(errbuf);
985 } 932 }
986 } 933 }
987#endif 934err:
935
936 /* Note that the queue has stopped and save the next toggle value */
937 urbp->qh->element = UHCI_PTR_TERM;
938 urbp->qh->is_stopped = 1;
939 urbp->qh->needs_fixup = 1;
940 urbp->qh->initial_toggle = uhci_toggle(td_token(td)) ^
941 (ret == -EREMOTEIO);
988 return ret; 942 return ret;
989} 943}
990 944
991static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 945static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
946 struct uhci_qh *qh)
992{ 947{
993 int ret; 948 int ret;
994 949
@@ -996,95 +951,60 @@ static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, struc
996 if (urb->dev->speed == USB_SPEED_LOW) 951 if (urb->dev->speed == USB_SPEED_LOW)
997 return -EINVAL; 952 return -EINVAL;
998 953
999 ret = uhci_submit_common(uhci, urb, eurb, uhci->skel_bulk_qh); 954 qh->skel = uhci->skel_bulk_qh;
1000 if (ret == -EINPROGRESS) 955 ret = uhci_submit_common(uhci, urb, qh);
956 if (ret == 0)
1001 uhci_inc_fsbr(uhci, urb); 957 uhci_inc_fsbr(uhci, urb);
1002
1003 return ret; 958 return ret;
1004} 959}
1005 960
1006static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 961static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
962 struct uhci_qh *qh)
1007{ 963{
1008 /* USB 1.1 interrupt transfers only involve one packet per interval; 964 /* USB 1.1 interrupt transfers only involve one packet per interval.
1009 * that's the uhci_submit_common() "breadth first" policy. Drivers 965 * Drivers can submit URBs of any length, but longer ones will need
1010 * can submit urbs of any length, but longer ones might need many 966 * multiple intervals to complete.
1011 * intervals to complete.
1012 */ 967 */
1013 return uhci_submit_common(uhci, urb, eurb, uhci->skelqh[__interval_to_skel(urb->interval)]); 968 qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
969 return uhci_submit_common(uhci, urb, qh);
1014} 970}
1015 971
1016/* 972/*
1017 * Isochronous transfers 973 * Isochronous transfers
1018 */ 974 */
1019static int isochronous_find_limits(struct uhci_hcd *uhci, struct urb *urb, unsigned int *start, unsigned int *end) 975static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
1020{ 976 struct uhci_qh *qh)
1021 struct urb *last_urb = NULL;
1022 struct urb_priv *up;
1023 int ret = 0;
1024
1025 list_for_each_entry(up, &uhci->urb_list, urb_list) {
1026 struct urb *u = up->urb;
1027
1028 /* look for pending URBs with identical pipe handle */
1029 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) &&
1030 (u->status == -EINPROGRESS) && (u != urb)) {
1031 if (!last_urb)
1032 *start = u->start_frame;
1033 last_urb = u;
1034 }
1035 }
1036
1037 if (last_urb) {
1038 *end = (last_urb->start_frame + last_urb->number_of_packets *
1039 last_urb->interval) & (UHCI_NUMFRAMES-1);
1040 ret = 0;
1041 } else
1042 ret = -1; /* no previous urb found */
1043
1044 return ret;
1045}
1046
1047static int isochronous_find_start(struct uhci_hcd *uhci, struct urb *urb)
1048{ 977{
1049 int limits; 978 struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
1050 unsigned int start = 0, end = 0; 979 int i, frame;
980 unsigned long destination, status;
981 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1051 982
1052 if (urb->number_of_packets > 900) /* 900? Why? */ 983 if (urb->number_of_packets > 900) /* 900? Why? */
1053 return -EFBIG; 984 return -EFBIG;
1054 985
1055 limits = isochronous_find_limits(uhci, urb, &start, &end); 986 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
987 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1056 988
989 /* Figure out the starting frame number */
1057 if (urb->transfer_flags & URB_ISO_ASAP) { 990 if (urb->transfer_flags & URB_ISO_ASAP) {
1058 if (limits) { 991 if (list_empty(&qh->queue)) {
1059 uhci_get_current_frame_number(uhci); 992 uhci_get_current_frame_number(uhci);
1060 urb->start_frame = (uhci->frame_number + 10) 993 urb->start_frame = (uhci->frame_number + 10);
1061 & (UHCI_NUMFRAMES - 1); 994
1062 } else 995 } else { /* Go right after the last one */
1063 urb->start_frame = end; 996 struct urb *last_urb;
997
998 last_urb = list_entry(qh->queue.prev,
999 struct urb_priv, node)->urb;
1000 urb->start_frame = (last_urb->start_frame +
1001 last_urb->number_of_packets *
1002 last_urb->interval);
1003 }
1064 } else { 1004 } else {
1065 urb->start_frame &= (UHCI_NUMFRAMES - 1);
1066 /* FIXME: Sanity check */ 1005 /* FIXME: Sanity check */
1067 } 1006 }
1068 1007 urb->start_frame &= (UHCI_NUMFRAMES - 1);
1069 return 0;
1070}
1071
1072/*
1073 * Isochronous transfers
1074 */
1075static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1076{
1077 struct uhci_td *td;
1078 int i, ret, frame;
1079 int status, destination;
1080 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1081
1082 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
1083 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1084
1085 ret = isochronous_find_start(uhci, urb);
1086 if (ret)
1087 return ret;
1088 1008
1089 for (i = 0; i < urb->number_of_packets; i++) { 1009 for (i = 0; i < urb->number_of_packets; i++) {
1090 td = uhci_alloc_td(uhci); 1010 td = uhci_alloc_td(uhci);
@@ -1092,20 +1012,25 @@ static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1092 return -ENOMEM; 1012 return -ENOMEM;
1093 1013
1094 uhci_add_td_to_urb(urb, td); 1014 uhci_add_td_to_urb(urb, td);
1095 uhci_fill_td(td, status, destination | uhci_explen(urb->iso_frame_desc[i].length), 1015 uhci_fill_td(td, status, destination |
1096 urb->transfer_dma + urb->iso_frame_desc[i].offset); 1016 uhci_explen(urb->iso_frame_desc[i].length),
1097 1017 urb->transfer_dma +
1098 if (i + 1 >= urb->number_of_packets) 1018 urb->iso_frame_desc[i].offset);
1099 td->status |= cpu_to_le32(TD_CTRL_IOC);
1100 } 1019 }
1101 1020
1021 /* Set the interrupt-on-completion flag on the last packet. */
1022 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
1023
1024 qh->skel = uhci->skel_iso_qh;
1025
1026 /* Add the TDs to the frame list */
1102 frame = urb->start_frame; 1027 frame = urb->start_frame;
1103 list_for_each_entry(td, &urbp->td_list, list) { 1028 list_for_each_entry(td, &urbp->td_list, list) {
1104 uhci_insert_td_frame_list(uhci, td, frame); 1029 uhci_insert_td_in_frame_list(uhci, td, frame);
1105 frame += urb->interval; 1030 frame += urb->interval;
1106 } 1031 }
1107 1032
1108 return -EINPROGRESS; 1033 return 0;
1109} 1034}
1110 1035
1111static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb) 1036static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
@@ -1139,80 +1064,67 @@ static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1139 1064
1140 i++; 1065 i++;
1141 } 1066 }
1142 unlink_isochronous_tds(uhci, urb);
1143 1067
1144 return ret; 1068 return ret;
1145} 1069}
1146 1070
1147static struct urb *uhci_find_urb_ep(struct uhci_hcd *uhci, struct urb *urb)
1148{
1149 struct urb_priv *up;
1150
1151 /* We don't match Isoc transfers since they are special */
1152 if (usb_pipeisoc(urb->pipe))
1153 return NULL;
1154
1155 list_for_each_entry(up, &uhci->urb_list, urb_list) {
1156 struct urb *u = up->urb;
1157
1158 if (u->dev == urb->dev && u->status == -EINPROGRESS) {
1159 /* For control, ignore the direction */
1160 if (usb_pipecontrol(urb->pipe) &&
1161 (u->pipe & ~USB_DIR_IN) == (urb->pipe & ~USB_DIR_IN))
1162 return u;
1163 else if (u->pipe == urb->pipe)
1164 return u;
1165 }
1166 }
1167
1168 return NULL;
1169}
1170
1171static int uhci_urb_enqueue(struct usb_hcd *hcd, 1071static int uhci_urb_enqueue(struct usb_hcd *hcd,
1172 struct usb_host_endpoint *ep, 1072 struct usb_host_endpoint *hep,
1173 struct urb *urb, gfp_t mem_flags) 1073 struct urb *urb, gfp_t mem_flags)
1174{ 1074{
1175 int ret; 1075 int ret;
1176 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 1076 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1177 unsigned long flags; 1077 unsigned long flags;
1178 struct urb *eurb; 1078 struct urb_priv *urbp;
1079 struct uhci_qh *qh;
1179 int bustime; 1080 int bustime;
1180 1081
1181 spin_lock_irqsave(&uhci->lock, flags); 1082 spin_lock_irqsave(&uhci->lock, flags);
1182 1083
1183 ret = urb->status; 1084 ret = urb->status;
1184 if (ret != -EINPROGRESS) /* URB already unlinked! */ 1085 if (ret != -EINPROGRESS) /* URB already unlinked! */
1185 goto out; 1086 goto done;
1186 1087
1187 eurb = uhci_find_urb_ep(uhci, urb); 1088 ret = -ENOMEM;
1089 urbp = uhci_alloc_urb_priv(uhci, urb);
1090 if (!urbp)
1091 goto done;
1188 1092
1189 if (!uhci_alloc_urb_priv(uhci, urb)) { 1093 if (hep->hcpriv)
1190 ret = -ENOMEM; 1094 qh = (struct uhci_qh *) hep->hcpriv;
1191 goto out; 1095 else {
1096 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1097 if (!qh)
1098 goto err_no_qh;
1192 } 1099 }
1100 urbp->qh = qh;
1193 1101
1194 switch (usb_pipetype(urb->pipe)) { 1102 switch (usb_pipetype(urb->pipe)) {
1195 case PIPE_CONTROL: 1103 case PIPE_CONTROL:
1196 ret = uhci_submit_control(uhci, urb, eurb); 1104 ret = uhci_submit_control(uhci, urb, qh);
1105 break;
1106 case PIPE_BULK:
1107 ret = uhci_submit_bulk(uhci, urb, qh);
1197 break; 1108 break;
1198 case PIPE_INTERRUPT: 1109 case PIPE_INTERRUPT:
1199 if (!eurb) { 1110 if (list_empty(&qh->queue)) {
1200 bustime = usb_check_bandwidth(urb->dev, urb); 1111 bustime = usb_check_bandwidth(urb->dev, urb);
1201 if (bustime < 0) 1112 if (bustime < 0)
1202 ret = bustime; 1113 ret = bustime;
1203 else { 1114 else {
1204 ret = uhci_submit_interrupt(uhci, urb, eurb); 1115 ret = uhci_submit_interrupt(uhci, urb, qh);
1205 if (ret == -EINPROGRESS) 1116 if (ret == 0)
1206 usb_claim_bandwidth(urb->dev, urb, bustime, 0); 1117 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1207 } 1118 }
1208 } else { /* inherit from parent */ 1119 } else { /* inherit from parent */
1209 urb->bandwidth = eurb->bandwidth; 1120 struct urb_priv *eurbp;
1210 ret = uhci_submit_interrupt(uhci, urb, eurb); 1121
1122 eurbp = list_entry(qh->queue.prev, struct urb_priv,
1123 node);
1124 urb->bandwidth = eurbp->urb->bandwidth;
1125 ret = uhci_submit_interrupt(uhci, urb, qh);
1211 } 1126 }
1212 break; 1127 break;
1213 case PIPE_BULK:
1214 ret = uhci_submit_bulk(uhci, urb, eurb);
1215 break;
1216 case PIPE_ISOCHRONOUS: 1128 case PIPE_ISOCHRONOUS:
1217 bustime = usb_check_bandwidth(urb->dev, urb); 1129 bustime = usb_check_bandwidth(urb->dev, urb);
1218 if (bustime < 0) { 1130 if (bustime < 0) {
@@ -1220,221 +1132,208 @@ static int uhci_urb_enqueue(struct usb_hcd *hcd,
1220 break; 1132 break;
1221 } 1133 }
1222 1134
1223 ret = uhci_submit_isochronous(uhci, urb); 1135 ret = uhci_submit_isochronous(uhci, urb, qh);
1224 if (ret == -EINPROGRESS) 1136 if (ret == 0)
1225 usb_claim_bandwidth(urb->dev, urb, bustime, 1); 1137 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1226 break; 1138 break;
1227 } 1139 }
1140 if (ret != 0)
1141 goto err_submit_failed;
1142
1143 /* Add this URB to the QH */
1144 urbp->qh = qh;
1145 list_add_tail(&urbp->node, &qh->queue);
1228 1146
1229 if (ret != -EINPROGRESS) { 1147 /* If the new URB is the first and only one on this QH then either
1230 /* Submit failed, so delete it from the urb_list */ 1148 * the QH is new and idle or else it's unlinked and waiting to
1231 struct urb_priv *urbp = urb->hcpriv; 1149 * become idle, so we can activate it right away. */
1150 if (qh->queue.next == &urbp->node)
1151 uhci_activate_qh(uhci, qh);
1152 goto done;
1232 1153
1233 list_del_init(&urbp->urb_list); 1154err_submit_failed:
1234 uhci_destroy_urb_priv(uhci, urb); 1155 if (qh->state == QH_STATE_IDLE)
1235 } else 1156 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1236 ret = 0;
1237 1157
1238out: 1158err_no_qh:
1159 uhci_free_urb_priv(uhci, urbp);
1160
1161done:
1239 spin_unlock_irqrestore(&uhci->lock, flags); 1162 spin_unlock_irqrestore(&uhci->lock, flags);
1240 return ret; 1163 return ret;
1241} 1164}
1242 1165
1243/* 1166static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1244 * Return the result of a transfer
1245 */
1246static void uhci_transfer_result(struct uhci_hcd *uhci, struct urb *urb)
1247{ 1167{
1248 int ret = -EINPROGRESS; 1168 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1169 unsigned long flags;
1249 struct urb_priv *urbp; 1170 struct urb_priv *urbp;
1250 1171
1251 spin_lock(&urb->lock); 1172 spin_lock_irqsave(&uhci->lock, flags);
1173 urbp = urb->hcpriv;
1174 if (!urbp) /* URB was never linked! */
1175 goto done;
1176
1177 /* Remove Isochronous TDs from the frame list ASAP */
1178 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1179 uhci_unlink_isochronous_tds(uhci, urb);
1180 uhci_unlink_qh(uhci, urbp->qh);
1181
1182done:
1183 spin_unlock_irqrestore(&uhci->lock, flags);
1184 return 0;
1185}
1252 1186
1253 urbp = (struct urb_priv *)urb->hcpriv; 1187/*
1188 * Finish unlinking an URB and give it back
1189 */
1190static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
1191 struct urb *urb, struct pt_regs *regs)
1192__releases(uhci->lock)
1193__acquires(uhci->lock)
1194{
1195 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1254 1196
1255 if (urb->status != -EINPROGRESS) /* URB already dequeued */ 1197 /* Isochronous TDs get unlinked directly from the frame list */
1256 goto out; 1198 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1199 uhci_unlink_isochronous_tds(uhci, urb);
1200
1201 /* If the URB isn't first on its queue, adjust the link pointer
1202 * of the last TD in the previous URB. */
1203 else if (qh->queue.next != &urbp->node) {
1204 struct urb_priv *purbp;
1205 struct uhci_td *ptd, *ltd;
1206
1207 purbp = list_entry(urbp->node.prev, struct urb_priv, node);
1208 ptd = list_entry(purbp->td_list.prev, struct uhci_td,
1209 list);
1210 ltd = list_entry(urbp->td_list.prev, struct uhci_td,
1211 list);
1212 ptd->link = ltd->link;
1213 }
1257 1214
1258 switch (usb_pipetype(urb->pipe)) { 1215 /* Take the URB off the QH's queue. If the queue is now empty,
1259 case PIPE_CONTROL: 1216 * this is a perfect time for a toggle fixup. */
1260 ret = uhci_result_control(uhci, urb); 1217 list_del_init(&urbp->node);
1261 break; 1218 if (list_empty(&qh->queue) && qh->needs_fixup) {
1262 case PIPE_BULK: 1219 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1263 case PIPE_INTERRUPT: 1220 usb_pipeout(urb->pipe), qh->initial_toggle);
1264 ret = uhci_result_common(uhci, urb); 1221 qh->needs_fixup = 0;
1265 break;
1266 case PIPE_ISOCHRONOUS:
1267 ret = uhci_result_isochronous(uhci, urb);
1268 break;
1269 } 1222 }
1270 1223
1271 if (ret == -EINPROGRESS) 1224 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1272 goto out; 1225 uhci_free_urb_priv(uhci, urbp);
1273 urb->status = ret;
1274 1226
1275 switch (usb_pipetype(urb->pipe)) { 1227 switch (usb_pipetype(urb->pipe)) {
1276 case PIPE_CONTROL:
1277 case PIPE_BULK:
1278 case PIPE_ISOCHRONOUS: 1228 case PIPE_ISOCHRONOUS:
1279 /* Release bandwidth for Interrupt or Isoc. transfers */ 1229 /* Release bandwidth for Interrupt or Isoc. transfers */
1280 if (urb->bandwidth) 1230 if (urb->bandwidth)
1281 usb_release_bandwidth(urb->dev, urb, 1); 1231 usb_release_bandwidth(urb->dev, urb, 1);
1282 uhci_unlink_generic(uhci, urb);
1283 break; 1232 break;
1284 case PIPE_INTERRUPT: 1233 case PIPE_INTERRUPT:
1285 /* Release bandwidth for Interrupt or Isoc. transfers */ 1234 /* Release bandwidth for Interrupt or Isoc. transfers */
1286 /* Make sure we don't release if we have a queued URB */ 1235 /* Make sure we don't release if we have a queued URB */
1287 if (list_empty(&urbp->queue_list) && urb->bandwidth) 1236 if (list_empty(&qh->queue) && urb->bandwidth)
1288 usb_release_bandwidth(urb->dev, urb, 0); 1237 usb_release_bandwidth(urb->dev, urb, 0);
1289 else 1238 else
1290 /* bandwidth was passed on to queued URB, */ 1239 /* bandwidth was passed on to queued URB, */
1291 /* so don't let usb_unlink_urb() release it */ 1240 /* so don't let usb_unlink_urb() release it */
1292 urb->bandwidth = 0; 1241 urb->bandwidth = 0;
1293 uhci_unlink_generic(uhci, urb);
1294 break; 1242 break;
1295 default:
1296 dev_info(uhci_dev(uhci), "%s: unknown pipe type %d "
1297 "for urb %p\n",
1298 __FUNCTION__, usb_pipetype(urb->pipe), urb);
1299 } 1243 }
1300 1244
1301 /* Move it from uhci->urb_list to uhci->complete_list */ 1245 spin_unlock(&uhci->lock);
1302 uhci_moveto_complete(uhci, urbp); 1246 usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs);
1303 1247 spin_lock(&uhci->lock);
1304out:
1305 spin_unlock(&urb->lock);
1306}
1307
1308static void uhci_unlink_generic(struct uhci_hcd *uhci, struct urb *urb)
1309{
1310 struct list_head *head;
1311 struct uhci_td *td;
1312 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1313 int prevactive = 0;
1314
1315 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1316 1248
1317 /* 1249 /* If the queue is now empty, we can unlink the QH and give up its
1318 * Now we need to find out what the last successful toggle was 1250 * reserved bandwidth. */
1319 * so we can update the local data toggle for the next transfer 1251 if (list_empty(&qh->queue)) {
1320 * 1252 uhci_unlink_qh(uhci, qh);
1321 * There are 2 ways the last successful completed TD is found:
1322 *
1323 * 1) The TD is NOT active and the actual length < expected length
1324 * 2) The TD is NOT active and it's the last TD in the chain
1325 *
1326 * and a third way the first uncompleted TD is found:
1327 *
1328 * 3) The TD is active and the previous TD is NOT active
1329 *
1330 * Control and Isochronous ignore the toggle, so this is safe
1331 * for all types
1332 *
1333 * FIXME: The toggle fixups won't be 100% reliable until we
1334 * change over to using a single queue for each endpoint and
1335 * stop the queue before unlinking.
1336 */
1337 head = &urbp->td_list;
1338 list_for_each_entry(td, head, list) {
1339 unsigned int ctrlstat = td_status(td);
1340 1253
1341 if (!(ctrlstat & TD_CTRL_ACTIVE) && 1254 /* Bandwidth stuff not yet implemented */
1342 (uhci_actual_length(ctrlstat) <
1343 uhci_expected_length(td_token(td)) ||
1344 td->list.next == head))
1345 usb_settoggle(urb->dev, uhci_endpoint(td_token(td)),
1346 uhci_packetout(td_token(td)),
1347 uhci_toggle(td_token(td)) ^ 1);
1348 else if ((ctrlstat & TD_CTRL_ACTIVE) && !prevactive)
1349 usb_settoggle(urb->dev, uhci_endpoint(td_token(td)),
1350 uhci_packetout(td_token(td)),
1351 uhci_toggle(td_token(td)));
1352
1353 prevactive = ctrlstat & TD_CTRL_ACTIVE;
1354 } 1255 }
1355
1356 uhci_delete_queued_urb(uhci, urb);
1357
1358 /* The interrupt loop will reclaim the QHs */
1359 uhci_remove_qh(uhci, urbp->qh);
1360 urbp->qh = NULL;
1361} 1256}
1362 1257
1363static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb) 1258/*
1259 * Scan the URBs in a QH's queue
1260 */
1261#define QH_FINISHED_UNLINKING(qh) \
1262 (qh->state == QH_STATE_UNLINKING && \
1263 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1264
1265static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh,
1266 struct pt_regs *regs)
1364{ 1267{
1365 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1366 unsigned long flags;
1367 struct urb_priv *urbp; 1268 struct urb_priv *urbp;
1269 struct urb *urb;
1270 int status;
1368 1271
1369 spin_lock_irqsave(&uhci->lock, flags); 1272 while (!list_empty(&qh->queue)) {
1370 urbp = urb->hcpriv; 1273 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1371 if (!urbp) /* URB was never linked! */ 1274 urb = urbp->urb;
1372 goto done;
1373 list_del_init(&urbp->urb_list);
1374
1375 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1376 unlink_isochronous_tds(uhci, urb);
1377 uhci_unlink_generic(uhci, urb);
1378
1379 uhci_get_current_frame_number(uhci);
1380 if (uhci->frame_number + uhci->is_stopped != uhci->urb_remove_age) {
1381 uhci_remove_pending_urbps(uhci);
1382 uhci->urb_remove_age = uhci->frame_number;
1383 }
1384
1385 /* If we're the first, set the next interrupt bit */
1386 if (list_empty(&uhci->urb_remove_list))
1387 uhci_set_next_interrupt(uhci);
1388 list_add_tail(&urbp->urb_list, &uhci->urb_remove_list);
1389
1390done:
1391 spin_unlock_irqrestore(&uhci->lock, flags);
1392 return 0;
1393}
1394 1275
1395static int uhci_fsbr_timeout(struct uhci_hcd *uhci, struct urb *urb) 1276 switch (usb_pipetype(urb->pipe)) {
1396{ 1277 case PIPE_CONTROL:
1397 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 1278 status = uhci_result_control(uhci, urb);
1398 struct list_head *head; 1279 break;
1399 struct uhci_td *td; 1280 case PIPE_ISOCHRONOUS:
1400 int count = 0; 1281 status = uhci_result_isochronous(uhci, urb);
1401 1282 break;
1402 uhci_dec_fsbr(uhci, urb); 1283 default: /* PIPE_BULK or PIPE_INTERRUPT */
1284 status = uhci_result_common(uhci, urb);
1285 break;
1286 }
1287 if (status == -EINPROGRESS)
1288 break;
1403 1289
1404 urbp->fsbr_timeout = 1; 1290 spin_lock(&urb->lock);
1291 if (urb->status == -EINPROGRESS) /* Not dequeued */
1292 urb->status = status;
1293 else
1294 status = -ECONNRESET;
1295 spin_unlock(&urb->lock);
1405 1296
1406 /* 1297 /* Dequeued but completed URBs can't be given back unless
1407 * Ideally we would want to fix qh->element as well, but it's 1298 * the QH is stopped or has finished unlinking. */
1408 * read/write by the HC, so that can introduce a race. It's not 1299 if (status == -ECONNRESET &&
1409 * really worth the hassle 1300 !(qh->is_stopped || QH_FINISHED_UNLINKING(qh)))
1410 */ 1301 return;
1411 1302
1412 head = &urbp->td_list; 1303 uhci_giveback_urb(uhci, qh, urb, regs);
1413 list_for_each_entry(td, head, list) { 1304 if (qh->is_stopped)
1414 /* 1305 break;
1415 * Make sure we don't do the last one (since it'll have the
1416 * TERM bit set) as well as we skip every so many TDs to
1417 * make sure it doesn't hog the bandwidth
1418 */
1419 if (td->list.next != head && (count % DEPTH_INTERVAL) ==
1420 (DEPTH_INTERVAL - 1))
1421 td->link |= UHCI_PTR_DEPTH;
1422
1423 count++;
1424 } 1306 }
1425 1307
1426 return 0; 1308 /* If the QH is neither stopped nor finished unlinking (normal case),
1427} 1309 * our work here is done. */
1428 1310 restart:
1429static void uhci_free_pending_qhs(struct uhci_hcd *uhci) 1311 if (!(qh->is_stopped || QH_FINISHED_UNLINKING(qh)))
1430{ 1312 return;
1431 struct uhci_qh *qh, *tmp;
1432
1433 list_for_each_entry_safe(qh, tmp, &uhci->qh_remove_list, remove_list) {
1434 list_del_init(&qh->remove_list);
1435 1313
1436 uhci_free_qh(uhci, qh); 1314 /* Otherwise give back each of the dequeued URBs */
1315 list_for_each_entry(urbp, &qh->queue, node) {
1316 urb = urbp->urb;
1317 if (urb->status != -EINPROGRESS) {
1318 uhci_save_toggle(qh, urb);
1319 uhci_giveback_urb(uhci, qh, urb, regs);
1320 goto restart;
1321 }
1322 }
1323 qh->is_stopped = 0;
1324
1325 /* There are no more dequeued URBs. If there are still URBs on the
1326 * queue, the QH can now be re-activated. */
1327 if (!list_empty(&qh->queue)) {
1328 if (qh->needs_fixup)
1329 uhci_fixup_toggles(qh, 0);
1330 uhci_activate_qh(uhci, qh);
1437 } 1331 }
1332
1333 /* The queue is empty. The QH can become idle if it is fully
1334 * unlinked. */
1335 else if (QH_FINISHED_UNLINKING(qh))
1336 uhci_make_qh_idle(uhci, qh);
1438} 1337}
1439 1338
1440static void uhci_free_pending_tds(struct uhci_hcd *uhci) 1339static void uhci_free_pending_tds(struct uhci_hcd *uhci)
@@ -1448,43 +1347,13 @@ static void uhci_free_pending_tds(struct uhci_hcd *uhci)
1448 } 1347 }
1449} 1348}
1450 1349
1451static void 1350/*
1452uhci_finish_urb(struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs) 1351 * Process events in the schedule, but only in one thread at a time
1453__releases(uhci->lock) 1352 */
1454__acquires(uhci->lock)
1455{
1456 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1457
1458 uhci_destroy_urb_priv(uhci, urb);
1459
1460 spin_unlock(&uhci->lock);
1461 usb_hcd_giveback_urb(hcd, urb, regs);
1462 spin_lock(&uhci->lock);
1463}
1464
1465static void uhci_finish_completion(struct uhci_hcd *uhci, struct pt_regs *regs)
1466{
1467 struct urb_priv *urbp, *tmp;
1468
1469 list_for_each_entry_safe(urbp, tmp, &uhci->complete_list, urb_list) {
1470 struct urb *urb = urbp->urb;
1471
1472 list_del_init(&urbp->urb_list);
1473 uhci_finish_urb(uhci_to_hcd(uhci), urb, regs);
1474 }
1475}
1476
1477static void uhci_remove_pending_urbps(struct uhci_hcd *uhci)
1478{
1479
1480 /* Splice the urb_remove_list onto the end of the complete_list */
1481 list_splice_init(&uhci->urb_remove_list, uhci->complete_list.prev);
1482}
1483
1484/* Process events in the schedule, but only in one thread at a time */
1485static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs) 1353static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1486{ 1354{
1487 struct urb_priv *urbp, *tmp; 1355 int i;
1356 struct uhci_qh *qh;
1488 1357
1489 /* Don't allow re-entrant calls */ 1358 /* Don't allow re-entrant calls */
1490 if (uhci->scan_in_progress) { 1359 if (uhci->scan_in_progress) {
@@ -1498,60 +1367,39 @@ static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1498 uhci_clear_next_interrupt(uhci); 1367 uhci_clear_next_interrupt(uhci);
1499 uhci_get_current_frame_number(uhci); 1368 uhci_get_current_frame_number(uhci);
1500 1369
1501 if (uhci->frame_number + uhci->is_stopped != uhci->qh_remove_age)
1502 uhci_free_pending_qhs(uhci);
1503 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) 1370 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age)
1504 uhci_free_pending_tds(uhci); 1371 uhci_free_pending_tds(uhci);
1505 if (uhci->frame_number + uhci->is_stopped != uhci->urb_remove_age)
1506 uhci_remove_pending_urbps(uhci);
1507
1508 /* Walk the list of pending URBs to see which ones completed
1509 * (must be _safe because uhci_transfer_result() dequeues URBs) */
1510 list_for_each_entry_safe(urbp, tmp, &uhci->urb_list, urb_list) {
1511 struct urb *urb = urbp->urb;
1512 1372
1513 /* Checks the status and does all of the magic necessary */ 1373 /* Go through all the QH queues and process the URBs in each one */
1514 uhci_transfer_result(uhci, urb); 1374 for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
1515 } 1375 uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
1516 uhci_finish_completion(uhci, regs); 1376 struct uhci_qh, node);
1517 1377 while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
1518 /* If the controller is stopped, we can finish these off right now */ 1378 uhci->next_qh = list_entry(qh->node.next,
1519 if (uhci->is_stopped) { 1379 struct uhci_qh, node);
1520 uhci_free_pending_qhs(uhci); 1380 uhci_scan_qh(uhci, qh, regs);
1521 uhci_free_pending_tds(uhci); 1381 }
1522 uhci_remove_pending_urbps(uhci);
1523 } 1382 }
1524 1383
1525 if (uhci->need_rescan) 1384 if (uhci->need_rescan)
1526 goto rescan; 1385 goto rescan;
1527 uhci->scan_in_progress = 0; 1386 uhci->scan_in_progress = 0;
1528 1387
1529 if (list_empty(&uhci->urb_remove_list) && 1388 /* If the controller is stopped, we can finish these off right now */
1530 list_empty(&uhci->td_remove_list) && 1389 if (uhci->is_stopped)
1531 list_empty(&uhci->qh_remove_list)) 1390 uhci_free_pending_tds(uhci);
1391
1392 if (list_empty(&uhci->td_remove_list) &&
1393 list_empty(&uhci->skel_unlink_qh->node))
1532 uhci_clear_next_interrupt(uhci); 1394 uhci_clear_next_interrupt(uhci);
1533 else 1395 else
1534 uhci_set_next_interrupt(uhci); 1396 uhci_set_next_interrupt(uhci);
1535
1536 /* Wake up anyone waiting for an URB to complete */
1537 wake_up_all(&uhci->waitqh);
1538} 1397}
1539 1398
1540static void check_fsbr(struct uhci_hcd *uhci) 1399static void check_fsbr(struct uhci_hcd *uhci)
1541{ 1400{
1542 struct urb_priv *up; 1401 /* For now, don't scan URBs for FSBR timeouts.
1543 1402 * Add it back in later... */
1544 list_for_each_entry(up, &uhci->urb_list, urb_list) {
1545 struct urb *u = up->urb;
1546
1547 spin_lock(&u->lock);
1548
1549 /* Check if the FSBR timed out */
1550 if (up->fsbr && !up->fsbr_timeout && time_after_eq(jiffies, up->fsbrtime + IDLE_TIMEOUT))
1551 uhci_fsbr_timeout(uhci, u);
1552
1553 spin_unlock(&u->lock);
1554 }
1555 1403
1556 /* Really disable FSBR */ 1404 /* Really disable FSBR */
1557 if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) { 1405 if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) {
diff --git a/drivers/usb/image/mdc800.c b/drivers/usb/image/mdc800.c
index 049871145d63..08daf400f985 100644
--- a/drivers/usb/image/mdc800.c
+++ b/drivers/usb/image/mdc800.c
@@ -96,6 +96,7 @@
96#include <linux/module.h> 96#include <linux/module.h>
97#include <linux/smp_lock.h> 97#include <linux/smp_lock.h>
98#include <linux/wait.h> 98#include <linux/wait.h>
99#include <linux/mutex.h>
99 100
100#include <linux/usb.h> 101#include <linux/usb.h>
101#include <linux/fs.h> 102#include <linux/fs.h>
@@ -169,7 +170,7 @@ struct mdc800_data
169 int out_count; // Bytes in the buffer 170 int out_count; // Bytes in the buffer
170 171
171 int open; // Camera device open ? 172 int open; // Camera device open ?
172 struct semaphore io_lock; // IO -lock 173 struct mutex io_lock; // IO -lock
173 174
174 char in [8]; // Command Input Buffer 175 char in [8]; // Command Input Buffer
175 int in_count; 176 int in_count;
@@ -497,7 +498,7 @@ static int mdc800_usb_probe (struct usb_interface *intf,
497 498
498 info ("Found Mustek MDC800 on USB."); 499 info ("Found Mustek MDC800 on USB.");
499 500
500 down (&mdc800->io_lock); 501 mutex_lock(&mdc800->io_lock);
501 502
502 retval = usb_register_dev(intf, &mdc800_class); 503 retval = usb_register_dev(intf, &mdc800_class);
503 if (retval) { 504 if (retval) {
@@ -542,7 +543,7 @@ static int mdc800_usb_probe (struct usb_interface *intf,
542 543
543 mdc800->state=READY; 544 mdc800->state=READY;
544 545
545 up (&mdc800->io_lock); 546 mutex_unlock(&mdc800->io_lock);
546 547
547 usb_set_intfdata(intf, mdc800); 548 usb_set_intfdata(intf, mdc800);
548 return 0; 549 return 0;
@@ -620,7 +621,7 @@ static int mdc800_device_open (struct inode* inode, struct file *file)
620 int retval=0; 621 int retval=0;
621 int errn=0; 622 int errn=0;
622 623
623 down (&mdc800->io_lock); 624 mutex_lock(&mdc800->io_lock);
624 625
625 if (mdc800->state == NOT_CONNECTED) 626 if (mdc800->state == NOT_CONNECTED)
626 { 627 {
@@ -656,7 +657,7 @@ static int mdc800_device_open (struct inode* inode, struct file *file)
656 dbg ("Mustek MDC800 device opened."); 657 dbg ("Mustek MDC800 device opened.");
657 658
658error_out: 659error_out:
659 up (&mdc800->io_lock); 660 mutex_unlock(&mdc800->io_lock);
660 return errn; 661 return errn;
661} 662}
662 663
@@ -669,7 +670,7 @@ static int mdc800_device_release (struct inode* inode, struct file *file)
669 int retval=0; 670 int retval=0;
670 dbg ("Mustek MDC800 device closed."); 671 dbg ("Mustek MDC800 device closed.");
671 672
672 down (&mdc800->io_lock); 673 mutex_lock(&mdc800->io_lock);
673 if (mdc800->open && (mdc800->state != NOT_CONNECTED)) 674 if (mdc800->open && (mdc800->state != NOT_CONNECTED))
674 { 675 {
675 usb_kill_urb(mdc800->irq_urb); 676 usb_kill_urb(mdc800->irq_urb);
@@ -682,7 +683,7 @@ static int mdc800_device_release (struct inode* inode, struct file *file)
682 retval=-EIO; 683 retval=-EIO;
683 } 684 }
684 685
685 up(&mdc800->io_lock); 686 mutex_unlock(&mdc800->io_lock);
686 return retval; 687 return retval;
687} 688}
688 689
@@ -695,21 +696,21 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
695 size_t left=len, sts=len; /* single transfer size */ 696 size_t left=len, sts=len; /* single transfer size */
696 char __user *ptr = buf; 697 char __user *ptr = buf;
697 698
698 down (&mdc800->io_lock); 699 mutex_lock(&mdc800->io_lock);
699 if (mdc800->state == NOT_CONNECTED) 700 if (mdc800->state == NOT_CONNECTED)
700 { 701 {
701 up (&mdc800->io_lock); 702 mutex_unlock(&mdc800->io_lock);
702 return -EBUSY; 703 return -EBUSY;
703 } 704 }
704 if (mdc800->state == WORKING) 705 if (mdc800->state == WORKING)
705 { 706 {
706 warn ("Illegal State \"working\" reached during read ?!"); 707 warn ("Illegal State \"working\" reached during read ?!");
707 up (&mdc800->io_lock); 708 mutex_unlock(&mdc800->io_lock);
708 return -EBUSY; 709 return -EBUSY;
709 } 710 }
710 if (!mdc800->open) 711 if (!mdc800->open)
711 { 712 {
712 up (&mdc800->io_lock); 713 mutex_unlock(&mdc800->io_lock);
713 return -EBUSY; 714 return -EBUSY;
714 } 715 }
715 716
@@ -717,7 +718,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
717 { 718 {
718 if (signal_pending (current)) 719 if (signal_pending (current))
719 { 720 {
720 up (&mdc800->io_lock); 721 mutex_unlock(&mdc800->io_lock);
721 return -EINTR; 722 return -EINTR;
722 } 723 }
723 724
@@ -736,7 +737,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
736 if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL)) 737 if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL))
737 { 738 {
738 err ("Can't submit download urb (status=%i)",mdc800->download_urb->status); 739 err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
739 up (&mdc800->io_lock); 740 mutex_unlock(&mdc800->io_lock);
740 return len-left; 741 return len-left;
741 } 742 }
742 wait_event_timeout(mdc800->download_wait, mdc800->downloaded, 743 wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
@@ -745,14 +746,14 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
745 if (mdc800->download_urb->status != 0) 746 if (mdc800->download_urb->status != 0)
746 { 747 {
747 err ("request download-bytes fails (status=%i)",mdc800->download_urb->status); 748 err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
748 up (&mdc800->io_lock); 749 mutex_unlock(&mdc800->io_lock);
749 return len-left; 750 return len-left;
750 } 751 }
751 } 752 }
752 else 753 else
753 { 754 {
754 /* No more bytes -> that's an error*/ 755 /* No more bytes -> that's an error*/
755 up (&mdc800->io_lock); 756 mutex_unlock(&mdc800->io_lock);
756 return -EIO; 757 return -EIO;
757 } 758 }
758 } 759 }
@@ -761,7 +762,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
761 /* Copy Bytes */ 762 /* Copy Bytes */
762 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr], 763 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
763 sts)) { 764 sts)) {
764 up(&mdc800->io_lock); 765 mutex_unlock(&mdc800->io_lock);
765 return -EFAULT; 766 return -EFAULT;
766 } 767 }
767 ptr+=sts; 768 ptr+=sts;
@@ -770,7 +771,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
770 } 771 }
771 } 772 }
772 773
773 up (&mdc800->io_lock); 774 mutex_unlock(&mdc800->io_lock);
774 return len-left; 775 return len-left;
775} 776}
776 777
@@ -785,15 +786,15 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
785{ 786{
786 size_t i=0; 787 size_t i=0;
787 788
788 down (&mdc800->io_lock); 789 mutex_lock(&mdc800->io_lock);
789 if (mdc800->state != READY) 790 if (mdc800->state != READY)
790 { 791 {
791 up (&mdc800->io_lock); 792 mutex_unlock(&mdc800->io_lock);
792 return -EBUSY; 793 return -EBUSY;
793 } 794 }
794 if (!mdc800->open ) 795 if (!mdc800->open )
795 { 796 {
796 up (&mdc800->io_lock); 797 mutex_unlock(&mdc800->io_lock);
797 return -EBUSY; 798 return -EBUSY;
798 } 799 }
799 800
@@ -802,13 +803,13 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
802 unsigned char c; 803 unsigned char c;
803 if (signal_pending (current)) 804 if (signal_pending (current))
804 { 805 {
805 up (&mdc800->io_lock); 806 mutex_unlock(&mdc800->io_lock);
806 return -EINTR; 807 return -EINTR;
807 } 808 }
808 809
809 if(get_user(c, buf+i)) 810 if(get_user(c, buf+i))
810 { 811 {
811 up(&mdc800->io_lock); 812 mutex_unlock(&mdc800->io_lock);
812 return -EFAULT; 813 return -EFAULT;
813 } 814 }
814 815
@@ -829,7 +830,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
829 } 830 }
830 else 831 else
831 { 832 {
832 up (&mdc800->io_lock); 833 mutex_unlock(&mdc800->io_lock);
833 return -EIO; 834 return -EIO;
834 } 835 }
835 836
@@ -841,7 +842,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
841 if (mdc800_usb_waitForIRQ (0,TO_GET_READY)) 842 if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
842 { 843 {
843 err ("Camera didn't get ready.\n"); 844 err ("Camera didn't get ready.\n");
844 up (&mdc800->io_lock); 845 mutex_unlock(&mdc800->io_lock);
845 return -EIO; 846 return -EIO;
846 } 847 }
847 848
@@ -853,7 +854,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
853 if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL)) 854 if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL))
854 { 855 {
855 err ("submitting write urb fails (status=%i)", mdc800->write_urb->status); 856 err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
856 up (&mdc800->io_lock); 857 mutex_unlock(&mdc800->io_lock);
857 return -EIO; 858 return -EIO;
858 } 859 }
859 wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000); 860 wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
@@ -861,7 +862,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
861 if (mdc800->state == WORKING) 862 if (mdc800->state == WORKING)
862 { 863 {
863 usb_kill_urb(mdc800->write_urb); 864 usb_kill_urb(mdc800->write_urb);
864 up (&mdc800->io_lock); 865 mutex_unlock(&mdc800->io_lock);
865 return -EIO; 866 return -EIO;
866 } 867 }
867 868
@@ -873,7 +874,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
873 { 874 {
874 err ("call 0x07 before 0x05,0x3e"); 875 err ("call 0x07 before 0x05,0x3e");
875 mdc800->state=READY; 876 mdc800->state=READY;
876 up (&mdc800->io_lock); 877 mutex_unlock(&mdc800->io_lock);
877 return -EIO; 878 return -EIO;
878 } 879 }
879 mdc800->pic_len=-1; 880 mdc800->pic_len=-1;
@@ -892,7 +893,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
892 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ)) 893 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
893 { 894 {
894 err ("requesting answer from irq fails"); 895 err ("requesting answer from irq fails");
895 up (&mdc800->io_lock); 896 mutex_unlock(&mdc800->io_lock);
896 return -EIO; 897 return -EIO;
897 } 898 }
898 899
@@ -920,7 +921,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
920 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND)) 921 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
921 { 922 {
922 err ("Command Timeout."); 923 err ("Command Timeout.");
923 up (&mdc800->io_lock); 924 mutex_unlock(&mdc800->io_lock);
924 return -EIO; 925 return -EIO;
925 } 926 }
926 } 927 }
@@ -930,7 +931,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
930 } 931 }
931 i++; 932 i++;
932 } 933 }
933 up (&mdc800->io_lock); 934 mutex_unlock(&mdc800->io_lock);
934 return i; 935 return i;
935} 936}
936 937
@@ -978,15 +979,13 @@ static int __init usb_mdc800_init (void)
978{ 979{
979 int retval = -ENODEV; 980 int retval = -ENODEV;
980 /* Allocate Memory */ 981 /* Allocate Memory */
981 mdc800=kmalloc (sizeof (struct mdc800_data), GFP_KERNEL); 982 mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
982 if (!mdc800) 983 if (!mdc800)
983 goto cleanup_on_fail; 984 goto cleanup_on_fail;
984 985
985 memset(mdc800, 0, sizeof(struct mdc800_data));
986 mdc800->dev = NULL; 986 mdc800->dev = NULL;
987 mdc800->open=0;
988 mdc800->state=NOT_CONNECTED; 987 mdc800->state=NOT_CONNECTED;
989 init_MUTEX (&mdc800->io_lock); 988 mutex_init (&mdc800->io_lock);
990 989
991 init_waitqueue_head (&mdc800->irq_wait); 990 init_waitqueue_head (&mdc800->irq_wait);
992 init_waitqueue_head (&mdc800->write_wait); 991 init_waitqueue_head (&mdc800->write_wait);
diff --git a/drivers/usb/image/microtek.c b/drivers/usb/image/microtek.c
index 28538db9eaf3..2a0e18a48748 100644
--- a/drivers/usb/image/microtek.c
+++ b/drivers/usb/image/microtek.c
@@ -360,7 +360,7 @@ static int mts_scsi_host_reset (Scsi_Cmnd *srb)
360 rc = usb_lock_device_for_reset(desc->usb_dev, desc->usb_intf); 360 rc = usb_lock_device_for_reset(desc->usb_dev, desc->usb_intf);
361 if (rc < 0) 361 if (rc < 0)
362 return FAILED; 362 return FAILED;
363 result = usb_reset_device(desc->usb_dev);; 363 result = usb_reset_device(desc->usb_dev);
364 if (rc) 364 if (rc)
365 usb_unlock_device(desc->usb_dev); 365 usb_unlock_device(desc->usb_dev);
366 return result ? FAILED : SUCCESS; 366 return result ? FAILED : SUCCESS;
diff --git a/drivers/usb/input/ati_remote.c b/drivers/usb/input/ati_remote.c
index f7bdc506e613..99f986cb6e95 100644
--- a/drivers/usb/input/ati_remote.c
+++ b/drivers/usb/input/ati_remote.c
@@ -159,8 +159,6 @@ static const char accel[] = { 1, 2, 4, 6, 9, 13, 20 };
159 */ 159 */
160#define FILTER_TIME (HZ / 20) 160#define FILTER_TIME (HZ / 20)
161 161
162static DECLARE_MUTEX(disconnect_sem);
163
164struct ati_remote { 162struct ati_remote {
165 struct input_dev *idev; 163 struct input_dev *idev;
166 struct usb_device *udev; 164 struct usb_device *udev;
diff --git a/drivers/usb/input/hid-core.c b/drivers/usb/input/hid-core.c
index 07a012f88772..d4bf1701046b 100644
--- a/drivers/usb/input/hid-core.c
+++ b/drivers/usb/input/hid-core.c
@@ -66,9 +66,8 @@ static struct hid_report *hid_register_report(struct hid_device *device, unsigne
66 if (report_enum->report_id_hash[id]) 66 if (report_enum->report_id_hash[id])
67 return report_enum->report_id_hash[id]; 67 return report_enum->report_id_hash[id];
68 68
69 if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL))) 69 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
70 return NULL; 70 return NULL;
71 memset(report, 0, sizeof(struct hid_report));
72 71
73 if (id != 0) 72 if (id != 0)
74 report_enum->numbered = 1; 73 report_enum->numbered = 1;
@@ -97,12 +96,9 @@ static struct hid_field *hid_register_field(struct hid_report *report, unsigned
97 return NULL; 96 return NULL;
98 } 97 }
99 98
100 if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage) 99 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
101 + values * sizeof(unsigned), GFP_KERNEL))) return NULL; 100 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
102 101
103 memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
104 + values * sizeof(unsigned));
105
106 field->index = report->maxfield++; 102 field->index = report->maxfield++;
107 report->field[field->index] = field; 103 report->field[field->index] = field;
108 field->usage = (struct hid_usage *)(field + 1); 104 field->usage = (struct hid_usage *)(field + 1);
@@ -651,17 +647,14 @@ static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
651 hid_parser_reserved 647 hid_parser_reserved
652 }; 648 };
653 649
654 if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL))) 650 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
655 return NULL; 651 return NULL;
656 memset(device, 0, sizeof(struct hid_device));
657 652
658 if (!(device->collection = kmalloc(sizeof(struct hid_collection) * 653 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
659 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) { 654 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
660 kfree(device); 655 kfree(device);
661 return NULL; 656 return NULL;
662 } 657 }
663 memset(device->collection, 0, sizeof(struct hid_collection) *
664 HID_DEFAULT_NUM_COLLECTIONS);
665 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS; 658 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
666 659
667 for (i = 0; i < HID_REPORT_TYPES; i++) 660 for (i = 0; i < HID_REPORT_TYPES; i++)
@@ -675,13 +668,12 @@ static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
675 memcpy(device->rdesc, start, size); 668 memcpy(device->rdesc, start, size);
676 device->rsize = size; 669 device->rsize = size;
677 670
678 if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) { 671 if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
679 kfree(device->rdesc); 672 kfree(device->rdesc);
680 kfree(device->collection); 673 kfree(device->collection);
681 kfree(device); 674 kfree(device);
682 return NULL; 675 return NULL;
683 } 676 }
684 memset(parser, 0, sizeof(struct hid_parser));
685 parser->device = device; 677 parser->device = device;
686 678
687 end = start + size; 679 end = start + size;
@@ -911,6 +903,99 @@ static int hid_input_report(int type, struct urb *urb, int interrupt, struct pt_
911} 903}
912 904
913/* 905/*
906 * Input submission and I/O error handler.
907 */
908
909static void hid_io_error(struct hid_device *hid);
910
911/* Start up the input URB */
912static int hid_start_in(struct hid_device *hid)
913{
914 unsigned long flags;
915 int rc = 0;
916
917 spin_lock_irqsave(&hid->inlock, flags);
918 if (hid->open > 0 && !test_bit(HID_SUSPENDED, &hid->iofl) &&
919 !test_and_set_bit(HID_IN_RUNNING, &hid->iofl)) {
920 rc = usb_submit_urb(hid->urbin, GFP_ATOMIC);
921 if (rc != 0)
922 clear_bit(HID_IN_RUNNING, &hid->iofl);
923 }
924 spin_unlock_irqrestore(&hid->inlock, flags);
925 return rc;
926}
927
928/* I/O retry timer routine */
929static void hid_retry_timeout(unsigned long _hid)
930{
931 struct hid_device *hid = (struct hid_device *) _hid;
932
933 dev_dbg(&hid->intf->dev, "retrying intr urb\n");
934 if (hid_start_in(hid))
935 hid_io_error(hid);
936}
937
938/* Workqueue routine to reset the device */
939static void hid_reset(void *_hid)
940{
941 struct hid_device *hid = (struct hid_device *) _hid;
942 int rc_lock, rc;
943
944 dev_dbg(&hid->intf->dev, "resetting device\n");
945 rc = rc_lock = usb_lock_device_for_reset(hid->dev, hid->intf);
946 if (rc_lock >= 0) {
947 rc = usb_reset_device(hid->dev);
948 if (rc_lock)
949 usb_unlock_device(hid->dev);
950 }
951 clear_bit(HID_RESET_PENDING, &hid->iofl);
952
953 if (rc == 0) {
954 hid->retry_delay = 0;
955 if (hid_start_in(hid))
956 hid_io_error(hid);
957 } else if (!(rc == -ENODEV || rc == -EHOSTUNREACH || rc == -EINTR))
958 err("can't reset device, %s-%s/input%d, status %d",
959 hid->dev->bus->bus_name,
960 hid->dev->devpath,
961 hid->ifnum, rc);
962}
963
964/* Main I/O error handler */
965static void hid_io_error(struct hid_device *hid)
966{
967 unsigned long flags;
968
969 spin_lock_irqsave(&hid->inlock, flags);
970
971 /* Stop when disconnected */
972 if (usb_get_intfdata(hid->intf) == NULL)
973 goto done;
974
975 /* When an error occurs, retry at increasing intervals */
976 if (hid->retry_delay == 0) {
977 hid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
978 hid->stop_retry = jiffies + msecs_to_jiffies(1000);
979 } else if (hid->retry_delay < 100)
980 hid->retry_delay *= 2;
981
982 if (time_after(jiffies, hid->stop_retry)) {
983
984 /* Retries failed, so do a port reset */
985 if (!test_and_set_bit(HID_RESET_PENDING, &hid->iofl)) {
986 if (schedule_work(&hid->reset_work))
987 goto done;
988 clear_bit(HID_RESET_PENDING, &hid->iofl);
989 }
990 }
991
992 mod_timer(&hid->io_retry,
993 jiffies + msecs_to_jiffies(hid->retry_delay));
994done:
995 spin_unlock_irqrestore(&hid->inlock, flags);
996}
997
998/*
914 * Input interrupt completion handler. 999 * Input interrupt completion handler.
915 */ 1000 */
916 1001
@@ -921,25 +1006,35 @@ static void hid_irq_in(struct urb *urb, struct pt_regs *regs)
921 1006
922 switch (urb->status) { 1007 switch (urb->status) {
923 case 0: /* success */ 1008 case 0: /* success */
1009 hid->retry_delay = 0;
924 hid_input_report(HID_INPUT_REPORT, urb, 1, regs); 1010 hid_input_report(HID_INPUT_REPORT, urb, 1, regs);
925 break; 1011 break;
926 case -ECONNRESET: /* unlink */ 1012 case -ECONNRESET: /* unlink */
927 case -ENOENT: 1013 case -ENOENT:
928 case -EPERM:
929 case -ESHUTDOWN: /* unplug */ 1014 case -ESHUTDOWN: /* unplug */
930 case -EILSEQ: /* unplug timeout on uhci */ 1015 clear_bit(HID_IN_RUNNING, &hid->iofl);
931 return; 1016 return;
1017 case -EILSEQ: /* protocol error or unplug */
1018 case -EPROTO: /* protocol error or unplug */
932 case -ETIMEDOUT: /* NAK */ 1019 case -ETIMEDOUT: /* NAK */
933 break; 1020 clear_bit(HID_IN_RUNNING, &hid->iofl);
1021 hid_io_error(hid);
1022 return;
934 default: /* error */ 1023 default: /* error */
935 warn("input irq status %d received", urb->status); 1024 warn("input irq status %d received", urb->status);
936 } 1025 }
937 1026
938 status = usb_submit_urb(urb, SLAB_ATOMIC); 1027 status = usb_submit_urb(urb, SLAB_ATOMIC);
939 if (status) 1028 if (status) {
940 err("can't resubmit intr, %s-%s/input%d, status %d", 1029 clear_bit(HID_IN_RUNNING, &hid->iofl);
941 hid->dev->bus->bus_name, hid->dev->devpath, 1030 if (status != -EPERM) {
942 hid->ifnum, status); 1031 err("can't resubmit intr, %s-%s/input%d, status %d",
1032 hid->dev->bus->bus_name,
1033 hid->dev->devpath,
1034 hid->ifnum, status);
1035 hid_io_error(hid);
1036 }
1037 }
943} 1038}
944 1039
945/* 1040/*
@@ -1101,8 +1196,9 @@ static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
1101 case 0: /* success */ 1196 case 0: /* success */
1102 break; 1197 break;
1103 case -ESHUTDOWN: /* unplug */ 1198 case -ESHUTDOWN: /* unplug */
1104 case -EILSEQ: /* unplug timeout on uhci */
1105 unplug = 1; 1199 unplug = 1;
1200 case -EILSEQ: /* protocol error or unplug */
1201 case -EPROTO: /* protocol error or unplug */
1106 case -ECONNRESET: /* unlink */ 1202 case -ECONNRESET: /* unlink */
1107 case -ENOENT: 1203 case -ENOENT:
1108 break; 1204 break;
@@ -1119,7 +1215,7 @@ static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
1119 1215
1120 if (hid->outhead != hid->outtail) { 1216 if (hid->outhead != hid->outtail) {
1121 if (hid_submit_out(hid)) { 1217 if (hid_submit_out(hid)) {
1122 clear_bit(HID_OUT_RUNNING, &hid->iofl);; 1218 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1123 wake_up(&hid->wait); 1219 wake_up(&hid->wait);
1124 } 1220 }
1125 spin_unlock_irqrestore(&hid->outlock, flags); 1221 spin_unlock_irqrestore(&hid->outlock, flags);
@@ -1149,8 +1245,9 @@ static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
1149 hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, 0, regs); 1245 hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, 0, regs);
1150 break; 1246 break;
1151 case -ESHUTDOWN: /* unplug */ 1247 case -ESHUTDOWN: /* unplug */
1152 case -EILSEQ: /* unplug timectrl on uhci */
1153 unplug = 1; 1248 unplug = 1;
1249 case -EILSEQ: /* protocol error or unplug */
1250 case -EPROTO: /* protocol error or unplug */
1154 case -ECONNRESET: /* unlink */ 1251 case -ECONNRESET: /* unlink */
1155 case -ENOENT: 1252 case -ENOENT:
1156 case -EPIPE: /* report not available */ 1253 case -EPIPE: /* report not available */
@@ -1263,14 +1360,9 @@ static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
1263 1360
1264int hid_open(struct hid_device *hid) 1361int hid_open(struct hid_device *hid)
1265{ 1362{
1266 if (hid->open++) 1363 ++hid->open;
1267 return 0; 1364 if (hid_start_in(hid))
1268 1365 hid_io_error(hid);
1269 hid->urbin->dev = hid->dev;
1270
1271 if (usb_submit_urb(hid->urbin, GFP_KERNEL))
1272 return -EIO;
1273
1274 return 0; 1366 return 0;
1275} 1367}
1276 1368
@@ -1460,6 +1552,9 @@ void hid_init_reports(struct hid_device *hid)
1460#define USB_VENDOR_ID_HP 0x03f0 1552#define USB_VENDOR_ID_HP 0x03f0
1461#define USB_DEVICE_ID_HP_USBHUB_KB 0x020c 1553#define USB_DEVICE_ID_HP_USBHUB_KB 0x020c
1462 1554
1555#define USB_VENDOR_ID_CREATIVELABS 0x062a
1556#define USB_DEVICE_ID_CREATIVELABS_SILVERCREST 0x0201
1557
1463/* 1558/*
1464 * Alphabetically sorted blacklist by quirk type. 1559 * Alphabetically sorted blacklist by quirk type.
1465 */ 1560 */
@@ -1576,6 +1671,7 @@ static const struct hid_blacklist {
1576 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET }, 1671 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
1577 { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET}, 1672 { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET},
1578 { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET}, 1673 { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET},
1674 { USB_VENDOR_ID_CREATIVELABS, USB_DEVICE_ID_CREATIVELABS_SILVERCREST, HID_QUIRK_NOGET },
1579 { USB_VENDOR_ID_HP, USB_DEVICE_ID_HP_USBHUB_KB, HID_QUIRK_NOGET }, 1675 { USB_VENDOR_ID_HP, USB_DEVICE_ID_HP_USBHUB_KB, HID_QUIRK_NOGET },
1580 { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET }, 1676 { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
1581 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT }, 1677 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
@@ -1795,6 +1891,10 @@ static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1795 1891
1796 init_waitqueue_head(&hid->wait); 1892 init_waitqueue_head(&hid->wait);
1797 1893
1894 INIT_WORK(&hid->reset_work, hid_reset, hid);
1895 setup_timer(&hid->io_retry, hid_retry_timeout, (unsigned long) hid);
1896
1897 spin_lock_init(&hid->inlock);
1798 spin_lock_init(&hid->outlock); 1898 spin_lock_init(&hid->outlock);
1799 spin_lock_init(&hid->ctrllock); 1899 spin_lock_init(&hid->ctrllock);
1800 1900
@@ -1863,11 +1963,16 @@ static void hid_disconnect(struct usb_interface *intf)
1863 if (!hid) 1963 if (!hid)
1864 return; 1964 return;
1865 1965
1966 spin_lock_irq(&hid->inlock); /* Sync with error handler */
1866 usb_set_intfdata(intf, NULL); 1967 usb_set_intfdata(intf, NULL);
1968 spin_unlock_irq(&hid->inlock);
1867 usb_kill_urb(hid->urbin); 1969 usb_kill_urb(hid->urbin);
1868 usb_kill_urb(hid->urbout); 1970 usb_kill_urb(hid->urbout);
1869 usb_kill_urb(hid->urbctrl); 1971 usb_kill_urb(hid->urbctrl);
1870 1972
1973 del_timer_sync(&hid->io_retry);
1974 flush_scheduled_work();
1975
1871 if (hid->claimed & HID_CLAIMED_INPUT) 1976 if (hid->claimed & HID_CLAIMED_INPUT)
1872 hidinput_disconnect(hid); 1977 hidinput_disconnect(hid);
1873 if (hid->claimed & HID_CLAIMED_HIDDEV) 1978 if (hid->claimed & HID_CLAIMED_HIDDEV)
@@ -1942,6 +2047,10 @@ static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1942{ 2047{
1943 struct hid_device *hid = usb_get_intfdata (intf); 2048 struct hid_device *hid = usb_get_intfdata (intf);
1944 2049
2050 spin_lock_irq(&hid->inlock); /* Sync with error handler */
2051 set_bit(HID_SUSPENDED, &hid->iofl);
2052 spin_unlock_irq(&hid->inlock);
2053 del_timer(&hid->io_retry);
1945 usb_kill_urb(hid->urbin); 2054 usb_kill_urb(hid->urbin);
1946 dev_dbg(&intf->dev, "suspend\n"); 2055 dev_dbg(&intf->dev, "suspend\n");
1947 return 0; 2056 return 0;
@@ -1952,10 +2061,8 @@ static int hid_resume(struct usb_interface *intf)
1952 struct hid_device *hid = usb_get_intfdata (intf); 2061 struct hid_device *hid = usb_get_intfdata (intf);
1953 int status; 2062 int status;
1954 2063
1955 if (hid->open) 2064 clear_bit(HID_SUSPENDED, &hid->iofl);
1956 status = usb_submit_urb(hid->urbin, GFP_NOIO); 2065 status = hid_start_in(hid);
1957 else
1958 status = 0;
1959 dev_dbg(&intf->dev, "resume status %d\n", status); 2066 dev_dbg(&intf->dev, "resume status %d\n", status);
1960 return status; 2067 return status;
1961} 2068}
diff --git a/drivers/usb/input/hid-lgff.c b/drivers/usb/input/hid-lgff.c
index f82c9c9e5d51..f07d44357ff1 100644
--- a/drivers/usb/input/hid-lgff.c
+++ b/drivers/usb/input/hid-lgff.c
@@ -154,10 +154,9 @@ int hid_lgff_init(struct hid_device* hid)
154 return -1; 154 return -1;
155 } 155 }
156 156
157 private = kmalloc(sizeof(struct lgff_device), GFP_KERNEL); 157 private = kzalloc(sizeof(struct lgff_device), GFP_KERNEL);
158 if (!private) 158 if (!private)
159 return -1; 159 return -1;
160 memset(private, 0, sizeof(struct lgff_device));
161 hid->ff_private = private; 160 hid->ff_private = private;
162 161
163 /* Input init */ 162 /* Input init */
@@ -228,13 +227,12 @@ static struct hid_report* hid_lgff_duplicate_report(struct hid_report* report)
228 } 227 }
229 *ret->field[0] = *report->field[0]; 228 *ret->field[0] = *report->field[0];
230 229
231 ret->field[0]->value = kmalloc(sizeof(s32[8]), GFP_KERNEL); 230 ret->field[0]->value = kzalloc(sizeof(s32[8]), GFP_KERNEL);
232 if (!ret->field[0]->value) { 231 if (!ret->field[0]->value) {
233 kfree(ret->field[0]); 232 kfree(ret->field[0]);
234 kfree(ret); 233 kfree(ret);
235 return NULL; 234 return NULL;
236 } 235 }
237 memset(ret->field[0]->value, 0, sizeof(s32[8]));
238 236
239 return ret; 237 return ret;
240} 238}
diff --git a/drivers/usb/input/hid-tmff.c b/drivers/usb/input/hid-tmff.c
index 023fd5ac31c8..534425c69c0a 100644
--- a/drivers/usb/input/hid-tmff.c
+++ b/drivers/usb/input/hid-tmff.c
@@ -113,11 +113,10 @@ int hid_tmff_init(struct hid_device *hid)
113 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 113 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
114 struct input_dev *input_dev = hidinput->input; 114 struct input_dev *input_dev = hidinput->input;
115 115
116 private = kmalloc(sizeof(struct tmff_device), GFP_KERNEL); 116 private = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
117 if (!private) 117 if (!private)
118 return -ENOMEM; 118 return -ENOMEM;
119 119
120 memset(private, 0, sizeof(struct tmff_device));
121 hid->ff_private = private; 120 hid->ff_private = private;
122 121
123 /* Find the report to use */ 122 /* Find the report to use */
diff --git a/drivers/usb/input/hid.h b/drivers/usb/input/hid.h
index 8b0d4346ce9c..4e1b784fe527 100644
--- a/drivers/usb/input/hid.h
+++ b/drivers/usb/input/hid.h
@@ -31,6 +31,8 @@
31#include <linux/types.h> 31#include <linux/types.h>
32#include <linux/slab.h> 32#include <linux/slab.h>
33#include <linux/list.h> 33#include <linux/list.h>
34#include <linux/timer.h>
35#include <linux/workqueue.h>
34 36
35/* 37/*
36 * USB HID (Human Interface Device) interface class code 38 * USB HID (Human Interface Device) interface class code
@@ -370,6 +372,9 @@ struct hid_control_fifo {
370 372
371#define HID_CTRL_RUNNING 1 373#define HID_CTRL_RUNNING 1
372#define HID_OUT_RUNNING 2 374#define HID_OUT_RUNNING 2
375#define HID_IN_RUNNING 3
376#define HID_RESET_PENDING 4
377#define HID_SUSPENDED 5
373 378
374struct hid_input { 379struct hid_input {
375 struct list_head list; 380 struct list_head list;
@@ -393,12 +398,17 @@ struct hid_device { /* device report descriptor */
393 int ifnum; /* USB interface number */ 398 int ifnum; /* USB interface number */
394 399
395 unsigned long iofl; /* I/O flags (CTRL_RUNNING, OUT_RUNNING) */ 400 unsigned long iofl; /* I/O flags (CTRL_RUNNING, OUT_RUNNING) */
401 struct timer_list io_retry; /* Retry timer */
402 unsigned long stop_retry; /* Time to give up, in jiffies */
403 unsigned int retry_delay; /* Delay length in ms */
404 struct work_struct reset_work; /* Task context for resets */
396 405
397 unsigned int bufsize; /* URB buffer size */ 406 unsigned int bufsize; /* URB buffer size */
398 407
399 struct urb *urbin; /* Input URB */ 408 struct urb *urbin; /* Input URB */
400 char *inbuf; /* Input buffer */ 409 char *inbuf; /* Input buffer */
401 dma_addr_t inbuf_dma; /* Input buffer dma */ 410 dma_addr_t inbuf_dma; /* Input buffer dma */
411 spinlock_t inlock; /* Input fifo spinlock */
402 412
403 struct urb *urbctrl; /* Control URB */ 413 struct urb *urbctrl; /* Control URB */
404 struct usb_ctrlrequest *cr; /* Control request struct */ 414 struct usb_ctrlrequest *cr; /* Control request struct */
diff --git a/drivers/usb/input/hiddev.c b/drivers/usb/input/hiddev.c
index 925f5aba06f5..6dd666696178 100644
--- a/drivers/usb/input/hiddev.c
+++ b/drivers/usb/input/hiddev.c
@@ -257,9 +257,8 @@ static int hiddev_open(struct inode * inode, struct file * file) {
257 if (i >= HIDDEV_MINORS || !hiddev_table[i]) 257 if (i >= HIDDEV_MINORS || !hiddev_table[i])
258 return -ENODEV; 258 return -ENODEV;
259 259
260 if (!(list = kmalloc(sizeof(struct hiddev_list), GFP_KERNEL))) 260 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
261 return -ENOMEM; 261 return -ENOMEM;
262 memset(list, 0, sizeof(struct hiddev_list));
263 262
264 list->hiddev = hiddev_table[i]; 263 list->hiddev = hiddev_table[i];
265 list->next = hiddev_table[i]->list; 264 list->next = hiddev_table[i]->list;
@@ -754,9 +753,8 @@ int hiddev_connect(struct hid_device *hid)
754 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0) 753 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
755 return -1; 754 return -1;
756 755
757 if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL))) 756 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
758 return -1; 757 return -1;
759 memset(hiddev, 0, sizeof(struct hiddev));
760 758
761 retval = usb_register_dev(hid->intf, &hiddev_class); 759 retval = usb_register_dev(hid->intf, &hiddev_class);
762 if (retval) { 760 if (retval) {
diff --git a/drivers/usb/media/Kconfig b/drivers/usb/media/Kconfig
deleted file mode 100644
index 0d3d2cc5d7be..000000000000
--- a/drivers/usb/media/Kconfig
+++ /dev/null
@@ -1,226 +0,0 @@
1#
2# USB Multimedia device configuration
3#
4comment "USB Multimedia devices"
5 depends on USB
6
7config USB_DABUSB
8 tristate "DABUSB driver"
9 depends on USB
10 ---help---
11 A Digital Audio Broadcasting (DAB) Receiver for USB and Linux
12 brought to you by the DAB-Team
13 <http://wwwbode.cs.tum.edu/Par/arch/dab/>. This driver can be taken
14 as an example for URB-based bulk, control, and isochronous
15 transactions. URB's are explained in
16 <file:Documentation/usb/URB.txt>.
17
18 To compile this driver as a module, choose M here: the
19 module will be called dabusb.
20
21comment "Video4Linux support is needed for USB Multimedia device support"
22 depends on USB && VIDEO_DEV=n
23
24config USB_VICAM
25 tristate "USB 3com HomeConnect (aka vicam) support (EXPERIMENTAL)"
26 depends on USB && VIDEO_DEV && EXPERIMENTAL
27 ---help---
28 Say Y here if you have 3com homeconnect camera (vicam).
29
30 This driver uses the Video For Linux API. You must say Y or M to
31 "Video For Linux" (under Multimedia Devices) to use this driver.
32 Information on this API and pointers to "v4l" programs may be found
33 at <file:Documentation/video4linux/API.html>.
34
35 To compile this driver as a module, choose M here: the
36 module will be called vicam.
37
38config USB_DSBR
39 tristate "D-Link USB FM radio support (EXPERIMENTAL)"
40 depends on USB && VIDEO_DEV && EXPERIMENTAL
41 ---help---
42 Say Y here if you want to connect this type of radio to your
43 computer's USB port. Note that the audio is not digital, and
44 you must connect the line out connector to a sound card or a
45 set of speakers.
46
47 This driver uses the Video For Linux API. You must enable
48 (Y or M in config) Video For Linux (under Character Devices)
49 to use this driver. Information on this API and pointers to
50 "v4l" programs may be found at
51 <file:Documentation/video4linux/API.html>.
52
53 To compile this driver as a module, choose M here: the
54 module will be called dsbr100.
55
56config USB_ET61X251
57 tristate "USB ET61X[12]51 PC Camera Controller support"
58 depends on USB && VIDEO_DEV
59 ---help---
60 Say Y here if you want support for cameras based on Etoms ET61X151
61 or ET61X251 PC Camera Controllers.
62
63 See <file:Documentation/usb/et61x251.txt> for more informations.
64
65 This driver uses the Video For Linux API. You must say Y or M to
66 "Video For Linux" to use this driver.
67
68 To compile this driver as a module, choose M here: the
69 module will be called et61x251.
70
71config USB_IBMCAM
72 tristate "USB IBM (Xirlink) C-it Camera support"
73 depends on USB && VIDEO_DEV
74 ---help---
75 Say Y here if you want to connect a IBM "C-It" camera, also known as
76 "Xirlink PC Camera" to your computer's USB port. For more
77 information, read <file:Documentation/usb/ibmcam.txt>.
78
79 This driver uses the Video For Linux API. You must enable
80 (Y or M in config) Video For Linux (under Character Devices)
81 to use this driver. Information on this API and pointers to
82 "v4l" programs may be found at
83 <file:Documentation/video4linux/API.html>.
84
85 To compile this driver as a module, choose M here: the
86 module will be called ibmcam.
87
88 This camera has several configuration options which
89 can be specified when you load the module. Read
90 <file:Documentation/usb/ibmcam.txt> to learn more.
91
92config USB_KONICAWC
93 tristate "USB Konica Webcam support"
94 depends on USB && VIDEO_DEV
95 ---help---
96 Say Y here if you want support for webcams based on a Konica
97 chipset. This is known to work with the Intel YC76 webcam.
98
99 This driver uses the Video For Linux API. You must enable
100 (Y or M in config) Video For Linux (under Character Devices)
101 to use this driver. Information on this API and pointers to
102 "v4l" programs may be found at
103 <file:Documentation/video4linux/API.html>.
104
105 To compile this driver as a module, choose M here: the
106 module will be called konicawc.
107
108config USB_OV511
109 tristate "USB OV511 Camera support"
110 depends on USB && VIDEO_DEV
111 ---help---
112 Say Y here if you want to connect this type of camera to your
113 computer's USB port. See <file:Documentation/usb/ov511.txt> for more
114 information and for a list of supported cameras.
115
116 This driver uses the Video For Linux API. You must say Y or M to
117 "Video For Linux" (under Character Devices) to use this driver.
118 Information on this API and pointers to "v4l" programs may be found
119 at <file:Documentation/video4linux/API.html>.
120
121 To compile this driver as a module, choose M here: the
122 module will be called ov511.
123
124config USB_SE401
125 tristate "USB SE401 Camera support"
126 depends on USB && VIDEO_DEV
127 ---help---
128 Say Y here if you want to connect this type of camera to your
129 computer's USB port. See <file:Documentation/usb/se401.txt> for more
130 information and for a list of supported cameras.
131
132 This driver uses the Video For Linux API. You must say Y or M to
133 "Video For Linux" (under Multimedia Devices) to use this driver.
134 Information on this API and pointers to "v4l" programs may be found
135 at <file:Documentation/video4linux/API.html>.
136
137 To compile this driver as a module, choose M here: the
138 module will be called se401.
139
140config USB_SN9C102
141 tristate "USB SN9C10x PC Camera Controller support"
142 depends on USB && VIDEO_DEV
143 ---help---
144 Say Y here if you want support for cameras based on SONiX SN9C101,
145 SN9C102 or SN9C103 PC Camera Controllers.
146
147 See <file:Documentation/usb/sn9c102.txt> for more informations.
148
149 This driver uses the Video For Linux API. You must say Y or M to
150 "Video For Linux" to use this driver.
151
152 To compile this driver as a module, choose M here: the
153 module will be called sn9c102.
154
155config USB_STV680
156 tristate "USB STV680 (Pencam) Camera support"
157 depends on USB && VIDEO_DEV
158 ---help---
159 Say Y here if you want to connect this type of camera to your
160 computer's USB port. This includes the Pencam line of cameras.
161 See <file:Documentation/usb/stv680.txt> for more information and for
162 a list of supported cameras.
163
164 This driver uses the Video For Linux API. You must say Y or M to
165 "Video For Linux" (under Multimedia Devices) to use this driver.
166 Information on this API and pointers to "v4l" programs may be found
167 at <file:Documentation/video4linux/API.html>.
168
169 To compile this driver as a module, choose M here: the
170 module will be called stv680.
171
172config USB_W9968CF
173 tristate "USB W996[87]CF JPEG Dual Mode Camera support"
174 depends on USB && VIDEO_DEV && I2C && VIDEO_OVCAMCHIP
175 ---help---
176 Say Y here if you want support for cameras based on OV681 or
177 Winbond W9967CF/W9968CF JPEG USB Dual Mode Camera Chips.
178
179 This driver has an optional plugin, which is distributed as a
180 separate module only (released under GPL). It allows to use higher
181 resolutions and framerates, but cannot be included in the official
182 Linux kernel for performance purposes.
183
184 See <file:Documentation/usb/w9968cf.txt> for more informations.
185
186 This driver uses the Video For Linux and the I2C APIs. It needs the
187 OmniVision Camera Chip support as well. You must say Y or M to
188 "Video For Linux", "I2C Support" and "OmniVision Camera Chip
189 support" to use this driver.
190
191 To compile this driver as a module, choose M here: the
192 module will be called w9968cf.
193
194config USB_PWC
195 tristate "USB Philips Cameras"
196 depends on USB && VIDEO_DEV
197 ---help---
198 Say Y or M here if you want to use one of these Philips & OEM
199 webcams:
200 * Philips PCA645, PCA646
201 * Philips PCVC675, PCVC680, PCVC690
202 * Philips PCVC720/40, PCVC730, PCVC740, PCVC750
203 * Askey VC010
204 * Logitech QuickCam Pro 3000, 4000, 'Zoom', 'Notebook Pro'
205 and 'Orbit'/'Sphere'
206 * Samsung MPC-C10, MPC-C30
207 * Creative Webcam 5, Pro Ex
208 * SOTEC Afina Eye
209 * Visionite VCS-UC300, VCS-UM100
210
211 The PCA635, PCVC665 and PCVC720/20 are not supported by this driver
212 and never will be, but the 665 and 720/20 are supported by other
213 drivers.
214
215 See <file:Documentation/usb/philips.txt> for more information and
216 installation instructions.
217
218 The built-in microphone is enabled by selecting USB Audio support.
219
220 This driver uses the Video For Linux API. You must say Y or M to
221 "Video For Linux" (under Character Devices) to use this driver.
222 Information on this API and pointers to "v4l" programs may be found
223 at <file:Documentation/video4linux/API.html>.
224
225 To compile this driver as a module, choose M here: the
226 module will be called pwc.
diff --git a/drivers/usb/media/Makefile b/drivers/usb/media/Makefile
deleted file mode 100644
index 3957aa1be0f2..000000000000
--- a/drivers/usb/media/Makefile
+++ /dev/null
@@ -1,19 +0,0 @@
1#
2# Makefile for USB Media drivers
3#
4
5sn9c102-objs := sn9c102_core.o sn9c102_hv7131d.o sn9c102_mi0343.o sn9c102_ov7630.o sn9c102_pas106b.o sn9c102_pas202bcb.o sn9c102_tas5110c1b.o sn9c102_tas5130d1b.o
6et61x251-objs := et61x251_core.o et61x251_tas5130d1b.o
7
8obj-$(CONFIG_USB_DABUSB) += dabusb.o
9obj-$(CONFIG_USB_DSBR) += dsbr100.o
10obj-$(CONFIG_USB_ET61X251) += et61x251.o
11obj-$(CONFIG_USB_IBMCAM) += ibmcam.o usbvideo.o ultracam.o
12obj-$(CONFIG_USB_KONICAWC) += konicawc.o usbvideo.o
13obj-$(CONFIG_USB_OV511) += ov511.o
14obj-$(CONFIG_USB_SE401) += se401.o
15obj-$(CONFIG_USB_SN9C102) += sn9c102.o
16obj-$(CONFIG_USB_STV680) += stv680.o
17obj-$(CONFIG_USB_VICAM) += vicam.o usbvideo.o
18obj-$(CONFIG_USB_W9968CF) += w9968cf.o
19obj-$(CONFIG_USB_PWC) += pwc/
diff --git a/drivers/usb/media/dabfirmware.h b/drivers/usb/media/dabfirmware.h
deleted file mode 100644
index d14d803566a3..000000000000
--- a/drivers/usb/media/dabfirmware.h
+++ /dev/null
@@ -1,1408 +0,0 @@
1/*
2 * dabdata.h - dab usb firmware and bitstream data
3 */
4
5static INTEL_HEX_RECORD firmware[] = {
6
7{ 2, 0x0000, 0, {0x21,0x57} },
8{ 3, 0x0003, 0, {0x02,0x01,0x66} },
9{ 3, 0x000b, 0, {0x02,0x01,0x66} },
10{ 3, 0x0013, 0, {0x02,0x01,0x66} },
11{ 3, 0x001b, 0, {0x02,0x01,0x66} },
12{ 3, 0x0023, 0, {0x02,0x01,0x66} },
13{ 3, 0x002b, 0, {0x02,0x01,0x66} },
14{ 3, 0x0033, 0, {0x02,0x03,0x0f} },
15{ 3, 0x003b, 0, {0x02,0x01,0x66} },
16{ 3, 0x0043, 0, {0x02,0x01,0x00} },
17{ 3, 0x004b, 0, {0x02,0x01,0x66} },
18{ 3, 0x0053, 0, {0x02,0x01,0x66} },
19{ 3, 0x005b, 0, {0x02,0x04,0xbd} },
20{ 3, 0x0063, 0, {0x02,0x01,0x67} },
21{ 3, 0x0100, 0, {0x02,0x0c,0x5a} },
22{ 3, 0x0104, 0, {0x02,0x01,0xed} },
23{ 3, 0x0108, 0, {0x02,0x02,0x51} },
24{ 3, 0x010c, 0, {0x02,0x02,0x7c} },
25{ 3, 0x0110, 0, {0x02,0x02,0xe4} },
26{ 1, 0x0114, 0, {0x32} },
27{ 1, 0x0118, 0, {0x32} },
28{ 3, 0x011c, 0, {0x02,0x05,0xfd} },
29{ 3, 0x0120, 0, {0x02,0x00,0x00} },
30{ 3, 0x0124, 0, {0x02,0x00,0x00} },
31{ 3, 0x0128, 0, {0x02,0x04,0x3c} },
32{ 3, 0x012c, 0, {0x02,0x04,0x6a} },
33{ 3, 0x0130, 0, {0x02,0x00,0x00} },
34{ 3, 0x0134, 0, {0x02,0x00,0x00} },
35{ 3, 0x0138, 0, {0x02,0x00,0x00} },
36{ 3, 0x013c, 0, {0x02,0x00,0x00} },
37{ 3, 0x0140, 0, {0x02,0x00,0x00} },
38{ 3, 0x0144, 0, {0x02,0x00,0x00} },
39{ 3, 0x0148, 0, {0x02,0x00,0x00} },
40{ 3, 0x014c, 0, {0x02,0x00,0x00} },
41{ 3, 0x0150, 0, {0x02,0x00,0x00} },
42{ 3, 0x0154, 0, {0x02,0x00,0x00} },
43{ 10, 0x0157, 0, {0x75,0x81,0x7f,0xe5,0x82,0x60,0x03,0x02,0x01,0x61} },
44{ 5, 0x0161, 0, {0x12,0x07,0x6f,0x21,0x64} },
45{ 1, 0x0166, 0, {0x32} },
46{ 14, 0x0167, 0, {0xc0,0xd0,0xc0,0x86,0xc0,0x82,0xc0,0x83,0xc0,0xe0,0x90,0x7f,0x97,0xe0} },
47{ 14, 0x0175, 0, {0x44,0x80,0xf0,0x90,0x7f,0x69,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0} },
48{ 14, 0x0183, 0, {0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0} },
49{ 14, 0x0191, 0, {0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0x90,0x7f,0x97,0xe0} },
50{ 3, 0x019f, 0, {0x55,0x7f,0xf0} },
51{ 14, 0x01a2, 0, {0x90,0x7f,0x9a,0xe0,0x30,0xe4,0x23,0x90,0x7f,0x68,0xf0,0xf0,0xf0,0xf0} },
52{ 14, 0x01b0, 0, {0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0} },
53{ 14, 0x01be, 0, {0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0} },
54{ 14, 0x01cc, 0, {0xe5,0xd8,0xc2,0xe3,0xf5,0xd8,0xd0,0xe0,0xd0,0x83,0xd0,0x82,0xd0,0x86} },
55{ 3, 0x01da, 0, {0xd0,0xd0,0x32} },
56{ 8, 0x01dd, 0, {0x75,0x86,0x00,0x90,0xff,0xc3,0x7c,0x05} },
57{ 7, 0x01e5, 0, {0xa3,0xe5,0x82,0x45,0x83,0x70,0xf9} },
58{ 1, 0x01ec, 0, {0x22} },
59{ 14, 0x01ed, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0x02,0xc0,0x03,0xc0,0xd0} },
60{ 14, 0x01fb, 0, {0x75,0xd0,0x00,0xc0,0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe4,0xf5,0x91} },
61{ 13, 0x0209, 0, {0x90,0x88,0x00,0xe0,0xf5,0x41,0x90,0x7f,0xab,0x74,0x02,0xf0,0x90} },
62{ 9, 0x0216, 0, {0x7f,0xab,0x74,0x02,0xf0,0xe5,0x32,0x60,0x21} },
63{ 4, 0x021f, 0, {0x7a,0x00,0x7b,0x00} },
64{ 11, 0x0223, 0, {0xc3,0xea,0x94,0x18,0xeb,0x64,0x80,0x94,0x80,0x50,0x12} },
65{ 14, 0x022e, 0, {0x90,0x7f,0x69,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0x0a,0xba,0x00} },
66{ 2, 0x023c, 0, {0x01,0x0b} },
67{ 2, 0x023e, 0, {0x80,0xe3} },
68{ 2, 0x0240, 0, {0xd0,0x86} },
69{ 14, 0x0242, 0, {0xd0,0xd0,0xd0,0x03,0xd0,0x02,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0} },
70{ 1, 0x0250, 0, {0x32} },
71{ 14, 0x0251, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0xd0,0x75,0xd0,0x00,0xc0} },
72{ 14, 0x025f, 0, {0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe4,0xf5,0x91,0x90,0x7f,0xab,0x74} },
73{ 4, 0x026d, 0, {0x04,0xf0,0xd0,0x86} },
74{ 11, 0x0271, 0, {0xd0,0xd0,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
75{ 14, 0x027c, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0x02,0xc0,0x03,0xc0,0x04} },
76{ 14, 0x028a, 0, {0xc0,0x05,0xc0,0x06,0xc0,0x07,0xc0,0x00,0xc0,0x01,0xc0,0xd0,0x75,0xd0} },
77{ 13, 0x0298, 0, {0x00,0xc0,0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe4,0xf5,0x91,0x90} },
78{ 12, 0x02a5, 0, {0x7f,0xab,0x74,0x08,0xf0,0x75,0x6e,0x00,0x75,0x6f,0x02,0x12} },
79{ 6, 0x02b1, 0, {0x11,0x44,0x75,0x70,0x39,0x75} },
80{ 6, 0x02b7, 0, {0x71,0x0c,0x75,0x72,0x02,0x12} },
81{ 12, 0x02bd, 0, {0x11,0x75,0x90,0x7f,0xd6,0xe4,0xf0,0x75,0xd8,0x20,0xd0,0x86} },
82{ 14, 0x02c9, 0, {0xd0,0xd0,0xd0,0x01,0xd0,0x00,0xd0,0x07,0xd0,0x06,0xd0,0x05,0xd0,0x04} },
83{ 13, 0x02d7, 0, {0xd0,0x03,0xd0,0x02,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
84{ 14, 0x02e4, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0xd0,0x75,0xd0,0x00,0xc0} },
85{ 14, 0x02f2, 0, {0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe4,0xf5,0x91,0x90,0x7f,0xab,0x74} },
86{ 4, 0x0300, 0, {0x10,0xf0,0xd0,0x86} },
87{ 11, 0x0304, 0, {0xd0,0xd0,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
88{ 14, 0x030f, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0x02,0xc0,0x03,0xc0,0x04} },
89{ 14, 0x031d, 0, {0xc0,0x05,0xc0,0x06,0xc0,0x07,0xc0,0x00,0xc0,0x01,0xc0,0xd0,0x75,0xd0} },
90{ 12, 0x032b, 0, {0x00,0xc0,0x86,0x75,0x86,0x00,0x75,0x6e,0x00,0x75,0x6f,0x02} },
91{ 7, 0x0337, 0, {0x12,0x11,0x44,0x75,0x70,0x40,0x75} },
92{ 6, 0x033e, 0, {0x71,0x0c,0x75,0x72,0x02,0x12} },
93{ 14, 0x0344, 0, {0x11,0x75,0x90,0x7f,0xd6,0x74,0x02,0xf0,0x90,0x7f,0xd6,0x74,0x06,0xf0} },
94{ 5, 0x0352, 0, {0x75,0xd8,0x10,0xd0,0x86} },
95{ 14, 0x0357, 0, {0xd0,0xd0,0xd0,0x01,0xd0,0x00,0xd0,0x07,0xd0,0x06,0xd0,0x05,0xd0,0x04} },
96{ 13, 0x0365, 0, {0xd0,0x03,0xd0,0x02,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
97{ 13, 0x0372, 0, {0x90,0x7f,0xa5,0x74,0x80,0xf0,0x90,0x7f,0xa6,0x74,0x9a,0xf0,0x12} },
98{ 12, 0x037f, 0, {0x10,0x1b,0x90,0x7f,0xa6,0xe5,0x42,0xf0,0x12,0x10,0x1b,0x90} },
99{ 13, 0x038b, 0, {0x7f,0xa6,0xe5,0x43,0xf0,0x12,0x10,0x1b,0x90,0x7f,0xa5,0x74,0x40} },
100{ 1, 0x0398, 0, {0xf0} },
101{ 1, 0x0399, 0, {0x22} },
102{ 13, 0x039a, 0, {0x90,0x7f,0xa5,0x74,0x80,0xf0,0x90,0x7f,0xa6,0x74,0x9a,0xf0,0x12} },
103{ 12, 0x03a7, 0, {0x10,0x1b,0x90,0x7f,0xa6,0xe5,0x44,0xf0,0x12,0x10,0x1b,0x90} },
104{ 12, 0x03b3, 0, {0x7f,0xa6,0xe5,0x45,0xf0,0x12,0x10,0x1b,0x90,0x7f,0xa6,0xe5} },
105{ 11, 0x03bf, 0, {0x46,0xf0,0x12,0x10,0x1b,0x90,0x7f,0xa5,0x74,0x40,0xf0} },
106{ 1, 0x03ca, 0, {0x22} },
107{ 10, 0x03cb, 0, {0x75,0x44,0x02,0x75,0x45,0x00,0x75,0x46,0x00,0x12} },
108{ 9, 0x03d5, 0, {0x03,0x9a,0x75,0x42,0x03,0x75,0x43,0x00,0x12} },
109{ 2, 0x03de, 0, {0x03,0x72} },
110{ 1, 0x03e0, 0, {0x22} },
111{ 12, 0x03e1, 0, {0x90,0x88,0x00,0xe5,0x36,0xf0,0x90,0x88,0x00,0x74,0x10,0x25} },
112{ 9, 0x03ed, 0, {0x36,0xf0,0x12,0x01,0xdd,0x75,0x42,0x01,0x75} },
113{ 9, 0x03f6, 0, {0x43,0x18,0x12,0x03,0x72,0x75,0x44,0x02,0x75} },
114{ 9, 0x03ff, 0,{0x45,0x00,0x75,0x46,0x00,0x12,0x03,0x9a,0x75} },
115{ 8, 0x0408, 0,{0x42,0x03,0x75,0x43,0x44,0x12,0x03,0x72} },
116{ 1, 0x0410, 0,{0x22} },
117{ 14, 0x0411, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0xd0,0x75,0xd0,0x00,0xc0} },
118{ 14, 0x041f, 0, {0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe4,0xf5,0x91,0x90,0x7f,0xaa,0x74} },
119{ 4, 0x042d, 0, {0x02,0xf0,0xd0,0x86} },
120{ 11, 0x0431, 0, {0xd0,0xd0,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
121{ 14, 0x043c, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0xd0,0x75,0xd0,0x00,0xc0} },
122{ 14, 0x044a, 0, {0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe4,0xf5,0x91,0x90,0x7f,0xa9,0x74} },
123{ 7, 0x0458, 0, {0x04,0xf0,0x75,0x30,0x01,0xd0,0x86} },
124{ 11, 0x045f, 0, {0xd0,0xd0,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
125{ 14, 0x046a, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0xd0,0x75,0xd0,0x00,0xc0} },
126{ 14, 0x0478, 0, {0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe4,0xf5,0x91,0x90,0x7f,0xaa,0x74} },
127{ 7, 0x0486, 0, {0x04,0xf0,0x75,0x31,0x01,0xd0,0x86} },
128{ 11, 0x048d, 0, {0xd0,0xd0,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
129{ 14, 0x0498, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0xd0,0x75,0xd0,0x00,0xc0} },
130{ 12, 0x04a6, 0, {0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe5,0xf5,0x91,0xd0,0x86} },
131{ 11, 0x04b2, 0, {0xd0,0xd0,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
132{ 14, 0x04bd, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0xd0,0x75,0xd0,0x00,0xc0} },
133{ 12, 0x04cb, 0, {0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe7,0xf5,0x91,0xd0,0x86} },
134{ 11, 0x04d7, 0, {0xd0,0xd0,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
135{ 12, 0x04e2, 0, {0x90,0x7f,0xea,0xe0,0xfa,0x8a,0x20,0x90,0x7f,0x96,0xe4,0xf0} },
136{ 1, 0x04ee, 0, {0x22} },
137{ 7, 0x04ef, 0, {0x90,0x7f,0xea,0xe0,0xfa,0x8a,0x21} },
138{ 1, 0x04f6, 0, {0x22} },
139{ 14, 0x04f7, 0, {0x90,0x17,0x13,0xe0,0xfa,0x90,0x17,0x15,0xe0,0xfb,0x74,0x80,0x2a,0xfa} },
140{ 14, 0x0505, 0, {0x74,0x80,0x2b,0xfb,0xea,0x03,0x03,0x54,0x3f,0xfc,0xea,0xc4,0x23,0x54} },
141{ 14, 0x0513, 0, {0x1f,0xfa,0x2c,0xfa,0xeb,0x03,0x03,0x54,0x3f,0xfc,0xeb,0xc4,0x23,0x54} },
142{ 11, 0x0521, 0, {0x1f,0xfb,0x2c,0xfb,0x90,0x17,0x0a,0xe0,0xfc,0x60,0x02} },
143{ 2, 0x052c, 0, {0x7a,0x00} },
144{ 7, 0x052e, 0, {0x90,0x17,0x0c,0xe0,0xfc,0x60,0x02} },
145{ 2, 0x0535, 0, {0x7b,0x00} },
146{ 11, 0x0537, 0, {0xea,0x2b,0xfc,0xc3,0x13,0xf5,0x3a,0x75,0x44,0x02,0x8b} },
147{ 7, 0x0542, 0, {0x45,0x8a,0x46,0x12,0x03,0x9a,0x75} },
148{ 9, 0x0549, 0, {0x6e,0x08,0x75,0x6f,0x00,0x12,0x11,0x44,0x75} },
149{ 4, 0x0552, 0, {0x70,0x47,0x75,0x71} },
150{ 8, 0x0556, 0, {0x0c,0x75,0x72,0x02,0x12,0x11,0x75,0x85} },
151{ 5, 0x055e, 0, {0x3a,0x73,0x12,0x11,0xa0} },
152{ 1, 0x0563, 0, {0x22} },
153{ 14, 0x0564, 0, {0x90,0x7f,0x96,0xe0,0xfa,0x90,0x7f,0x96,0x74,0x80,0x65,0x02,0xf0,0x90} },
154{ 14, 0x0572, 0, {0x7f,0xeb,0xe0,0xfa,0x90,0x7f,0xea,0xe0,0xfb,0x90,0x7f,0xef,0xe0,0xfc} },
155{ 14, 0x0580, 0, {0x33,0x95,0xe0,0xfd,0x8c,0x05,0x7c,0x00,0x90,0x7f,0xee,0xe0,0xfe,0x33} },
156{ 14, 0x058e, 0, {0x95,0xe0,0xff,0xec,0x2e,0xfc,0xed,0x3f,0xfd,0x90,0x7f,0xe9,0xe0,0xfe} },
157{ 5, 0x059c, 0, {0xbe,0x01,0x02,0x80,0x03} },
158{ 3, 0x05a1, 0, {0x02,0x05,0xf9} },
159{ 6, 0x05a4, 0, {0xbc,0x01,0x21,0xbd,0x00,0x1e} },
160{ 14, 0x05aa, 0, {0xea,0xc4,0x03,0x54,0xf8,0xfc,0xeb,0x25,0xe0,0xfd,0x2c,0x24,0x00,0xfc} },
161{ 14, 0x05b8, 0, {0xe4,0x34,0x17,0xfd,0x90,0x7e,0xc0,0xe0,0xfe,0x8c,0x82,0x8d,0x83,0xf0} },
162{ 2, 0x05c6, 0, {0x80,0x31} },
163{ 14, 0x05c8, 0, {0xea,0xc4,0x03,0x54,0xf8,0xfa,0xeb,0x25,0xe0,0xfb,0x2a,0xfa,0x24,0x00} },
164{ 14, 0x05d6, 0, {0xfb,0xe4,0x34,0x17,0xfc,0x90,0x7e,0xc0,0xe0,0xfd,0x8b,0x82,0x8c,0x83} },
165{ 14, 0x05e4, 0, {0xf0,0x74,0x01,0x2a,0x24,0x00,0xfa,0xe4,0x34,0x17,0xfb,0x90,0x7e,0xc1} },
166{ 7, 0x05f2, 0, {0xe0,0xfc,0x8a,0x82,0x8b,0x83,0xf0} },
167{ 3, 0x05f9, 0, {0x75,0x38,0x01} },
168{ 1, 0x05fc, 0, {0x22} },
169{ 14, 0x05fd, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0x02,0xc0,0x03,0xc0,0x04} },
170{ 14, 0x060b, 0, {0xc0,0x05,0xc0,0x06,0xc0,0x07,0xc0,0x00,0xc0,0x01,0xc0,0xd0,0x75,0xd0} },
171{ 13, 0x0619, 0, {0x00,0xc0,0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe4,0xf5,0x91,0x90} },
172{ 13, 0x0626, 0, {0x7f,0xaa,0x74,0x01,0xf0,0x12,0x05,0x64,0x75,0x37,0x00,0xd0,0x86} },
173{ 14, 0x0633, 0, {0xd0,0xd0,0xd0,0x01,0xd0,0x00,0xd0,0x07,0xd0,0x06,0xd0,0x05,0xd0,0x04} },
174{ 13, 0x0641, 0, {0xd0,0x03,0xd0,0x02,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
175{ 14, 0x064e, 0, {0x90,0x7f,0xeb,0xe0,0xfa,0x90,0x7f,0xea,0xe0,0xfb,0x90,0x7f,0xee,0xe0} },
176{ 14, 0x065c, 0, {0xfc,0x33,0x95,0xe0,0xfd,0x90,0x7f,0x96,0xe0,0xfe,0x90,0x7f,0x96,0x74} },
177{ 14, 0x066a, 0, {0x80,0x65,0x06,0xf0,0x90,0x7f,0x00,0x74,0x01,0xf0,0xea,0xc4,0x03,0x54} },
178{ 14, 0x0678, 0, {0xf8,0xfe,0xeb,0x25,0xe0,0xfb,0x2e,0xfe,0x24,0x00,0xfb,0xe4,0x34,0x17} },
179{ 14, 0x0686, 0, {0xff,0x8b,0x82,0x8f,0x83,0xe0,0xfb,0x74,0x01,0x2e,0x24,0x00,0xfe,0xe4} },
180{ 14, 0x0694, 0, {0x34,0x17,0xff,0x8e,0x82,0x8f,0x83,0xe0,0xfe,0x90,0x7f,0xe9,0xe0,0xff} },
181{ 3, 0x06a2, 0, {0xbf,0x81,0x0a} },
182{ 10, 0x06a5, 0, {0x90,0x7f,0x00,0xeb,0xf0,0x90,0x7f,0x01,0xee,0xf0} },
183{ 8, 0x06af, 0, {0x90,0x7f,0xe9,0xe0,0xfb,0xbb,0x82,0x1a} },
184{ 3, 0x06b7, 0, {0xba,0x01,0x0c} },
185{ 12, 0x06ba, 0, {0x90,0x7f,0x00,0xe4,0xf0,0x90,0x7f,0x01,0xe4,0xf0,0x80,0x0b} },
186{ 11, 0x06c6, 0, {0x90,0x7f,0x00,0xe4,0xf0,0x90,0x7f,0x01,0x74,0xb5,0xf0} },
187{ 8, 0x06d1, 0, {0x90,0x7f,0xe9,0xe0,0xfb,0xbb,0x83,0x1b} },
188{ 3, 0x06d9, 0, {0xba,0x01,0x0d} },
189{ 13, 0x06dc, 0, {0x90,0x7f,0x00,0x74,0x01,0xf0,0x90,0x7f,0x01,0xe4,0xf0,0x80,0x0b} },
190{ 11, 0x06e9, 0, {0x90,0x7f,0x00,0xe4,0xf0,0x90,0x7f,0x01,0x74,0x12,0xf0} },
191{ 8, 0x06f4, 0, {0x90,0x7f,0xe9,0xe0,0xfb,0xbb,0x84,0x1c} },
192{ 3, 0x06fc, 0, {0xba,0x01,0x0d} },
193{ 13, 0x06ff, 0, {0x90,0x7f,0x00,0x74,0x01,0xf0,0x90,0x7f,0x01,0xe4,0xf0,0x80,0x0c} },
194{ 12, 0x070c, 0, {0x90,0x7f,0x00,0x74,0x80,0xf0,0x90,0x7f,0x01,0x74,0x01,0xf0} },
195{ 5, 0x0718, 0, {0x90,0x7f,0xb5,0xec,0xf0} },
196{ 1, 0x071d, 0, {0x22} },
197{ 12, 0x071e, 0, {0x75,0x36,0x0d,0x90,0x88,0x00,0x74,0x1d,0xf0,0x75,0x6b,0x80} },
198{ 10, 0x072a, 0, {0x75,0x6c,0x3c,0x12,0x10,0xe2,0x75,0x6b,0x80,0x75} },
199{ 9, 0x0734, 0, {0x6c,0x0f,0x12,0x10,0xe2,0x75,0x6b,0x80,0x75} },
200{ 9, 0x073d, 0, {0x6c,0x06,0x12,0x10,0xe2,0x75,0x6b,0x80,0x75} },
201{ 7, 0x0746, 0, {0x6c,0x01,0x12,0x10,0xe2,0x7a,0x00} },
202{ 3, 0x074d, 0, {0xba,0xff,0x00} },
203{ 2, 0x0750, 0, {0x50,0x0a} },
204{ 10, 0x0752, 0, {0xc0,0x02,0x12,0x01,0xdd,0xd0,0x02,0x0a,0x80,0xf1} },
205{ 10, 0x075c, 0, {0x75,0x6b,0x80,0x75,0x6c,0x3c,0x12,0x10,0xe2,0x75} },
206{ 8, 0x0766, 0, {0x6b,0x80,0x75,0x6c,0x0f,0x12,0x10,0xe2} },
207{ 1, 0x076e, 0, {0x22} },
208{ 14, 0x076f, 0, {0x90,0x7f,0xa1,0xe4,0xf0,0x90,0x7f,0xaf,0x74,0x01,0xf0,0x90,0x7f,0x92} },
209{ 14, 0x077d, 0, {0x74,0x02,0xf0,0x75,0x8e,0x31,0x75,0x89,0x21,0x75,0x88,0x00,0x75,0xc8} },
210{ 14, 0x078b, 0, {0x00,0x75,0x8d,0x40,0x75,0x98,0x40,0x75,0xc0,0x40,0x75,0x87,0x00,0x75} },
211{ 9, 0x0799, 0, {0x20,0x00,0x75,0x21,0x00,0x75,0x22,0x00,0x75} },
212{ 5, 0x07a2, 0, {0x23,0x00,0x75,0x47,0x00} },
213{ 7, 0x07a7, 0, {0xc3,0xe5,0x47,0x94,0x20,0x50,0x11} },
214{ 13, 0x07ae, 0, {0xe5,0x47,0x24,0x00,0xf5,0x82,0xe4,0x34,0x17,0xf5,0x83,0xe4,0xf0} },
215{ 4, 0x07bb, 0, {0x05,0x47,0x80,0xe8} },
216{ 9, 0x07bf, 0, {0xe4,0xf5,0x40,0xf5,0x3f,0xe4,0xf5,0x3c,0xf5} },
217{ 7, 0x07c8, 0, {0x3b,0xe4,0xf5,0x3e,0xf5,0x3d,0x75} },
218{ 11, 0x07cf, 0, {0x32,0x00,0x75,0x37,0x00,0x75,0x39,0x00,0x90,0x7f,0x93} },
219{ 14, 0x07da, 0, {0x74,0x3c,0xf0,0x90,0x7f,0x9c,0x74,0xff,0xf0,0x90,0x7f,0x96,0x74,0x80} },
220{ 14, 0x07e8, 0, {0xf0,0x90,0x7f,0x94,0x74,0x70,0xf0,0x90,0x7f,0x9d,0x74,0x8f,0xf0,0x90} },
221{ 14, 0x07f6, 0, {0x7f,0x97,0xe4,0xf0,0x90,0x7f,0x95,0x74,0xc2,0xf0,0x90,0x7f,0x98,0x74} },
222{ 14, 0x0804, 0, {0x28,0xf0,0x90,0x7f,0x9e,0x74,0x28,0xf0,0x90,0x7f,0xf0,0xe4,0xf0,0x90} },
223{ 14, 0x0812, 0, {0x7f,0xf1,0xe4,0xf0,0x90,0x7f,0xf2,0xe4,0xf0,0x90,0x7f,0xf3,0xe4,0xf0} },
224{ 14, 0x0820, 0, {0x90,0x7f,0xf4,0xe4,0xf0,0x90,0x7f,0xf5,0xe4,0xf0,0x90,0x7f,0xf6,0xe4} },
225{ 14, 0x082e, 0, {0xf0,0x90,0x7f,0xf7,0xe4,0xf0,0x90,0x7f,0xf8,0xe4,0xf0,0x90,0x7f,0xf9} },
226{ 14, 0x083c, 0, {0x74,0x38,0xf0,0x90,0x7f,0xfa,0x74,0xa0,0xf0,0x90,0x7f,0xfb,0x74,0xa0} },
227{ 14, 0x084a, 0, {0xf0,0x90,0x7f,0xfc,0x74,0xa0,0xf0,0x90,0x7f,0xfd,0x74,0xa0,0xf0,0x90} },
228{ 14, 0x0858, 0, {0x7f,0xfe,0x74,0xa0,0xf0,0x90,0x7f,0xff,0x74,0xa0,0xf0,0x90,0x7f,0xe0} },
229{ 14, 0x0866, 0, {0x74,0x03,0xf0,0x90,0x7f,0xe1,0x74,0x01,0xf0,0x90,0x7f,0xdd,0x74,0x80} },
230{ 11, 0x0874, 0, {0xf0,0x12,0x12,0x43,0x12,0x07,0x1e,0x7a,0x00,0x7b,0x00} },
231{ 9, 0x087f, 0, {0xc3,0xea,0x94,0x1e,0xeb,0x94,0x00,0x50,0x17} },
232{ 12, 0x0888, 0, {0x90,0x88,0x00,0xe0,0xf5,0x47,0x90,0x88,0x0b,0xe0,0xf5,0x47} },
233{ 9, 0x0894, 0, {0x90,0x7f,0x68,0xf0,0x0a,0xba,0x00,0x01,0x0b} },
234{ 2, 0x089d, 0, {0x80,0xe0} },
235{ 12, 0x089f, 0, {0x12,0x03,0xe1,0x90,0x7f,0xd6,0xe4,0xf0,0x7a,0x00,0x7b,0x00} },
236{ 13, 0x08ab, 0, {0x8a,0x04,0x8b,0x05,0xc3,0xea,0x94,0xe0,0xeb,0x94,0x2e,0x50,0x1a} },
237{ 14, 0x08b8, 0, {0xc0,0x02,0xc0,0x03,0xc0,0x04,0xc0,0x05,0x12,0x01,0xdd,0xd0,0x05,0xd0} },
238{ 10, 0x08c6, 0, {0x04,0xd0,0x03,0xd0,0x02,0x0a,0xba,0x00,0x01,0x0b} },
239{ 2, 0x08d0, 0, {0x80,0xd9} },
240{ 13, 0x08d2, 0, {0x90,0x7f,0xd6,0x74,0x02,0xf0,0x90,0x7f,0xd6,0x74,0x06,0xf0,0x90} },
241{ 14, 0x08df, 0, {0x7f,0xde,0x74,0x05,0xf0,0x90,0x7f,0xdf,0x74,0x05,0xf0,0x90,0x7f,0xac} },
242{ 14, 0x08ed, 0, {0xe4,0xf0,0x90,0x7f,0xad,0x74,0x05,0xf0,0x75,0xa8,0x80,0x75,0xf8,0x10} },
243{ 13, 0x08fb, 0, {0x90,0x7f,0xae,0x74,0x0b,0xf0,0x90,0x7f,0xe2,0x74,0x88,0xf0,0x90} },
244{ 12, 0x0908, 0, {0x7f,0xab,0x74,0x08,0xf0,0x75,0xe8,0x11,0x75,0x32,0x01,0x75} },
245{ 12, 0x0914, 0, {0x31,0x00,0x75,0x30,0x00,0xc0,0x04,0xc0,0x05,0x12,0x04,0xf7} },
246{ 10, 0x0920, 0, {0xd0,0x05,0xd0,0x04,0x75,0x34,0x00,0x75,0x35,0x01} },
247{ 13, 0x092a, 0, {0x90,0x7f,0xae,0x74,0x03,0xf0,0x8c,0x02,0xba,0x00,0x02,0x80,0x03} },
248{ 3, 0x0937, 0, {0x02,0x0a,0x3f} },
249{ 12, 0x093a, 0, {0x85,0x33,0x34,0x90,0x7f,0x9d,0x74,0x8f,0xf0,0x90,0x7f,0x97} },
250{ 14, 0x0946, 0, {0x74,0x08,0xf0,0x90,0x7f,0x9d,0x74,0x88,0xf0,0x90,0x7f,0x9a,0xe0,0xfa} },
251{ 12, 0x0954, 0, {0x74,0x05,0x5a,0xf5,0x33,0x90,0x7f,0x9d,0x74,0x8f,0xf0,0x90} },
252{ 13, 0x0960, 0, {0x7f,0x97,0x74,0x02,0xf0,0x90,0x7f,0x9d,0x74,0x82,0xf0,0xe5,0x33} },
253{ 13, 0x096d, 0, {0x25,0xe0,0xfa,0x90,0x7f,0x9a,0xe0,0x54,0x05,0xfb,0x4a,0xf5,0x33} },
254{ 2, 0x097a, 0, {0x60,0x0c} },
255{ 12, 0x097c, 0, {0x90,0x7f,0x96,0xe0,0xfa,0x90,0x7f,0x96,0x74,0x80,0x4a,0xf0} },
256{ 11, 0x0988, 0, {0x75,0x6e,0x00,0x75,0x6f,0x00,0xc0,0x04,0xc0,0x05,0x12} },
257{ 14, 0x0993, 0, {0x11,0x44,0xd0,0x05,0xd0,0x04,0x90,0x17,0x13,0xe0,0xfa,0x74,0x80,0x2a} },
258{ 6, 0x09a1, 0, {0xfa,0xe5,0x33,0xb4,0x04,0x29} },
259{ 3, 0x09a7, 0, {0xba,0xa0,0x00} },
260{ 2, 0x09aa, 0, {0x50,0x24} },
261{ 13, 0x09ac, 0, {0x90,0x17,0x13,0xe0,0x04,0xfb,0x0b,0x90,0x17,0x13,0xeb,0xf0,0x90} },
262{ 14, 0x09b9, 0, {0x17,0x13,0xe0,0xfb,0x90,0x17,0x15,0xf0,0xc0,0x02,0xc0,0x04,0xc0,0x05} },
263{ 9, 0x09c7, 0, {0x12,0x04,0xf7,0xd0,0x05,0xd0,0x04,0xd0,0x02} },
264{ 5, 0x09d0, 0, {0xe5,0x33,0xb4,0x02,0x26} },
265{ 6, 0x09d5, 0, {0xc3,0x74,0x04,0x9a,0x50,0x20} },
266{ 13, 0x09db, 0, {0x90,0x17,0x13,0xe0,0xfa,0x1a,0x1a,0x90,0x17,0x13,0xea,0xf0,0x90} },
267{ 13, 0x09e8, 0, {0x17,0x13,0xe0,0xfa,0x90,0x17,0x15,0xf0,0xc0,0x04,0xc0,0x05,0x12} },
268{ 6, 0x09f5, 0, {0x04,0xf7,0xd0,0x05,0xd0,0x04} },
269{ 5, 0x09fb, 0, {0xe5,0x33,0xb4,0x08,0x1d} },
270{ 4, 0x0a00, 0, {0xe5,0x34,0x70,0x19} },
271{ 10, 0x0a04, 0, {0x74,0x01,0x25,0x35,0x54,0x0f,0xf5,0x35,0x85,0x35} },
272{ 12, 0x0a0e, 0, {0x75,0x75,0x76,0x00,0xc0,0x04,0xc0,0x05,0x12,0x13,0xfe,0xd0} },
273{ 3, 0x0a1a, 0, {0x05,0xd0,0x04} },
274{ 5, 0x0a1d, 0, {0xe5,0x33,0xb4,0x01,0x1d} },
275{ 4, 0x0a22, 0, {0xe5,0x34,0x70,0x19} },
276{ 10, 0x0a26, 0, {0xe5,0x35,0x24,0xff,0x54,0x0f,0xf5,0x35,0x85,0x35} },
277{ 12, 0x0a30, 0, {0x75,0x75,0x76,0x00,0xc0,0x04,0xc0,0x05,0x12,0x13,0xfe,0xd0} },
278{ 3, 0x0a3c, 0, {0x05,0xd0,0x04} },
279{ 14, 0x0a3f, 0, {0xc0,0x04,0xc0,0x05,0x12,0x01,0xdd,0xd0,0x05,0xd0,0x04,0x90,0x7f,0x96} },
280{ 14, 0x0a4d, 0, {0xe0,0xfa,0x90,0x7f,0x96,0x74,0x7f,0x5a,0xf0,0x90,0x7f,0x97,0x74,0x08} },
281{ 10, 0x0a5b, 0, {0xf0,0xc3,0xec,0x94,0x00,0xed,0x94,0x02,0x40,0x08} },
282{ 8, 0x0a65, 0, {0x90,0x7f,0x96,0xe0,0xfa,0x20,0xe6,0x08} },
283{ 8, 0x0a6d, 0, {0xc3,0xe4,0x9c,0x74,0x08,0x9d,0x50,0x13} },
284{ 14, 0x0a75, 0, {0x90,0x7f,0x96,0xe0,0xfa,0x90,0x7f,0x96,0x74,0x40,0x65,0x02,0xf0,0x7c} },
285{ 5, 0x0a83, 0, {0x00,0x7d,0x00,0x80,0x05} },
286{ 5, 0x0a88, 0, {0x0c,0xbc,0x00,0x01,0x0d} },
287{ 5, 0x0a8d, 0, {0xe5,0x38,0xb4,0x01,0x0e} },
288{ 13, 0x0a92, 0, {0xc0,0x04,0xc0,0x05,0x12,0x04,0xf7,0xd0,0x05,0xd0,0x04,0x75,0x38} },
289{ 1, 0x0a9f, 0, {0x00} },
290{ 7, 0x0aa0, 0, {0xe5,0x31,0x70,0x03,0x02,0x09,0x2a} },
291{ 10, 0x0aa7, 0, {0x90,0x7f,0xc9,0xe0,0xfa,0x70,0x03,0x02,0x0c,0x2d} },
292{ 14, 0x0ab1, 0, {0x90,0x7f,0x96,0xe0,0xfa,0x90,0x7f,0x96,0x74,0x80,0x65,0x02,0xf0,0x90} },
293{ 9, 0x0abf, 0, {0x7d,0xc0,0xe0,0xfa,0xba,0x2c,0x02,0x80,0x03} },
294{ 3, 0x0ac8, 0, {0x02,0x0b,0x36} },
295{ 5, 0x0acb, 0, {0x75,0x32,0x00,0x7b,0x00} },
296{ 3, 0x0ad0, 0, {0xbb,0x64,0x00} },
297{ 2, 0x0ad3, 0, {0x50,0x1c} },
298{ 14, 0x0ad5, 0, {0xc0,0x02,0xc0,0x03,0xc0,0x04,0xc0,0x05,0x12,0x01,0xdd,0xd0,0x05,0xd0} },
299{ 13, 0x0ae3, 0, {0x04,0xd0,0x03,0xd0,0x02,0x90,0x88,0x0f,0xe0,0xf5,0x47,0x0b,0x80} },
300{ 1, 0x0af0, 0, {0xdf} },
301{ 13, 0x0af1, 0, {0xc0,0x02,0xc0,0x04,0xc0,0x05,0x12,0x07,0x1e,0x12,0x03,0xe1,0x12} },
302{ 12, 0x0afe, 0, {0x04,0xf7,0xd0,0x05,0xd0,0x04,0xd0,0x02,0x75,0x6e,0x00,0x75} },
303{ 13, 0x0b0a, 0, {0x6f,0x01,0xc0,0x02,0xc0,0x04,0xc0,0x05,0x12,0x11,0x44,0xd0,0x05} },
304{ 9, 0x0b17, 0, {0xd0,0x04,0xd0,0x02,0x75,0x70,0x4d,0x75,0x71} },
305{ 11, 0x0b20, 0, {0x0c,0x75,0x72,0x02,0xc0,0x02,0xc0,0x04,0xc0,0x05,0x12} },
306{ 11, 0x0b2b, 0, {0x11,0x75,0xd0,0x05,0xd0,0x04,0xd0,0x02,0x02,0x0c,0x2d} },
307{ 3, 0x0b36, 0, {0xba,0x2a,0x3b} },
308{ 13, 0x0b39, 0, {0x90,0x7f,0x98,0x74,0x20,0xf0,0xc0,0x02,0xc0,0x04,0xc0,0x05,0x12} },
309{ 14, 0x0b46, 0, {0x01,0xdd,0xd0,0x05,0xd0,0x04,0xd0,0x02,0x90,0x7f,0x98,0x74,0x28,0xf0} },
310{ 2, 0x0b54, 0, {0x7b,0x00} },
311{ 3, 0x0b56, 0, {0xbb,0x0a,0x00} },
312{ 5, 0x0b59, 0, {0x40,0x03,0x02,0x0c,0x2d} },
313{ 14, 0x0b5e, 0, {0xc0,0x02,0xc0,0x03,0xc0,0x04,0xc0,0x05,0x12,0x01,0xdd,0xd0,0x05,0xd0} },
314{ 8, 0x0b6c, 0, {0x04,0xd0,0x03,0xd0,0x02,0x0b,0x80,0xe2} },
315{ 3, 0x0b74, 0, {0xba,0x2b,0x1a} },
316{ 8, 0x0b77, 0, {0x90,0x7f,0xc9,0xe0,0xfb,0xbb,0x40,0x12} },
317{ 14, 0x0b7f, 0, {0xc0,0x02,0xc0,0x04,0xc0,0x05,0x12,0x12,0x05,0xd0,0x05,0xd0,0x04,0xd0} },
318{ 4, 0x0b8d, 0, {0x02,0x02,0x0c,0x2d} },
319{ 3, 0x0b91, 0, {0xba,0x10,0x1f} },
320{ 14, 0x0b94, 0, {0x90,0x7f,0x96,0xe0,0xfb,0x90,0x7f,0x96,0x74,0x80,0x65,0x03,0xf0,0xc0} },
321{ 14, 0x0ba2, 0, {0x02,0xc0,0x04,0xc0,0x05,0x12,0x10,0x3d,0xd0,0x05,0xd0,0x04,0xd0,0x02} },
322{ 3, 0x0bb0, 0, {0x02,0x0c,0x2d} },
323{ 3, 0x0bb3, 0, {0xba,0x11,0x12} },
324{ 14, 0x0bb6, 0, {0xc0,0x02,0xc0,0x04,0xc0,0x05,0x12,0x10,0x6a,0xd0,0x05,0xd0,0x04,0xd0} },
325{ 4, 0x0bc4, 0, {0x02,0x02,0x0c,0x2d} },
326{ 3, 0x0bc8, 0, {0xba,0x12,0x12} },
327{ 14, 0x0bcb, 0, {0xc0,0x02,0xc0,0x04,0xc0,0x05,0x12,0x10,0x8f,0xd0,0x05,0xd0,0x04,0xd0} },
328{ 4, 0x0bd9, 0, {0x02,0x02,0x0c,0x2d} },
329{ 3, 0x0bdd, 0, {0xba,0x13,0x0b} },
330{ 11, 0x0be0, 0, {0x90,0x7d,0xc1,0xe0,0xfb,0x90,0x88,0x00,0xf0,0x80,0x42} },
331{ 3, 0x0beb, 0, {0xba,0x14,0x11} },
332{ 14, 0x0bee, 0, {0xc0,0x02,0xc0,0x04,0xc0,0x05,0x12,0x11,0xdd,0xd0,0x05,0xd0,0x04,0xd0} },
333{ 3, 0x0bfc, 0, {0x02,0x80,0x2e} },
334{ 3, 0x0bff, 0, {0xba,0x15,0x1d} },
335{ 12, 0x0c02, 0, {0x90,0x7d,0xc1,0xe0,0xf5,0x75,0x90,0x7d,0xc2,0xe0,0xf5,0x76} },
336{ 14, 0x0c0e, 0, {0xc0,0x02,0xc0,0x04,0xc0,0x05,0x12,0x13,0xfe,0xd0,0x05,0xd0,0x04,0xd0} },
337{ 3, 0x0c1c, 0, {0x02,0x80,0x0e} },
338{ 3, 0x0c1f, 0, {0xba,0x16,0x0b} },
339{ 11, 0x0c22, 0, {0xc0,0x04,0xc0,0x05,0x12,0x13,0xa3,0xd0,0x05,0xd0,0x04} },
340{ 11, 0x0c2d, 0, {0x90,0x7f,0xc9,0xe4,0xf0,0x75,0x31,0x00,0x02,0x09,0x2a} },
341{ 1, 0x0c38, 0, {0x22} },
342{ 7, 0x0c39, 0, {0x53,0x55,0x50,0x45,0x4e,0x44,0x00} },
343{ 7, 0x0c40, 0, {0x52,0x45,0x53,0x55,0x4d,0x45,0x00} },
344{ 6, 0x0c47, 0, {0x20,0x56,0x6f,0x6c,0x20,0x00} },
345{ 13, 0x0c4d, 0, {0x44,0x41,0x42,0x55,0x53,0x42,0x20,0x76,0x31,0x2e,0x30,0x30,0x00} },
346{ 14, 0x0c5a, 0, {0xc0,0xe0,0xc0,0xf0,0xc0,0x82,0xc0,0x83,0xc0,0x02,0xc0,0x03,0xc0,0x04} },
347{ 14, 0x0c68, 0, {0xc0,0x05,0xc0,0x06,0xc0,0x07,0xc0,0x00,0xc0,0x01,0xc0,0xd0,0x75,0xd0} },
348{ 13, 0x0c76, 0, {0x00,0xc0,0x86,0x75,0x86,0x00,0xe5,0x91,0xc2,0xe4,0xf5,0x91,0x90} },
349{ 14, 0x0c83, 0, {0x7f,0xab,0x74,0x01,0xf0,0x90,0x7f,0xe8,0xe0,0xfa,0x90,0x7f,0xe9,0xe0} },
350{ 6, 0x0c91, 0, {0xfb,0xbb,0x00,0x02,0x80,0x03} },
351{ 3, 0x0c97, 0, {0x02,0x0d,0x38} },
352{ 3, 0x0c9a, 0, {0xba,0x80,0x14} },
353{ 14, 0x0c9d, 0, {0x90,0x7f,0x00,0x74,0x01,0xf0,0x90,0x7f,0x01,0xe4,0xf0,0x90,0x7f,0xb5} },
354{ 6, 0x0cab, 0, {0x74,0x02,0xf0,0x02,0x0e,0xcd} },
355{ 5, 0x0cb1, 0, {0xba,0x82,0x02,0x80,0x03} },
356{ 3, 0x0cb6, 0, {0x02,0x0d,0x1d} },
357{ 8, 0x0cb9, 0, {0x90,0x7f,0xec,0xe0,0xfc,0xbc,0x01,0x00} },
358{ 2, 0x0cc1, 0, {0x40,0x21} },
359{ 6, 0x0cc3, 0, {0xc3,0x74,0x07,0x9c,0x40,0x1b} },
360{ 14, 0x0cc9, 0, {0xec,0x24,0xff,0x25,0xe0,0xfd,0x24,0xc6,0xf5,0x82,0xe4,0x34,0x7f,0xf5} },
361{ 13, 0x0cd7, 0, {0x83,0xe0,0xfd,0x53,0x05,0x01,0x90,0x7f,0x00,0xed,0xf0,0x80,0x2b} },
362{ 3, 0x0ce4, 0, {0xbc,0x81,0x00} },
363{ 2, 0x0ce7, 0, {0x40,0x21} },
364{ 6, 0x0ce9, 0, {0xc3,0x74,0x87,0x9c,0x40,0x1b} },
365{ 14, 0x0cef, 0, {0xec,0x24,0x7f,0x25,0xe0,0xfc,0x24,0xb6,0xf5,0x82,0xe4,0x34,0x7f,0xf5} },
366{ 13, 0x0cfd, 0, {0x83,0xe0,0xfc,0x53,0x04,0x01,0x90,0x7f,0x00,0xec,0xf0,0x80,0x05} },
367{ 5, 0x0d0a, 0, {0x90,0x7f,0x00,0xe4,0xf0} },
368{ 14, 0x0d0f, 0, {0x90,0x7f,0x01,0xe4,0xf0,0x90,0x7f,0xb5,0x74,0x02,0xf0,0x02,0x0e,0xcd} },
369{ 5, 0x0d1d, 0, {0xba,0x81,0x02,0x80,0x03} },
370{ 3, 0x0d22, 0, {0x02,0x0e,0xc5} },
371{ 14, 0x0d25, 0, {0x90,0x7f,0x00,0xe4,0xf0,0x90,0x7f,0x01,0xe4,0xf0,0x90,0x7f,0xb5,0x74} },
372{ 5, 0x0d33, 0, {0x02,0xf0,0x02,0x0e,0xcd} },
373{ 3, 0x0d38, 0, {0xbb,0x01,0x2d} },
374{ 6, 0x0d3b, 0, {0xba,0x00,0x03,0x02,0x0e,0xcd} },
375{ 3, 0x0d41, 0, {0xba,0x02,0x11} },
376{ 13, 0x0d44, 0, {0x75,0x59,0x00,0xc0,0x02,0xc0,0x03,0x12,0x0e,0xf0,0xd0,0x03,0xd0} },
377{ 4, 0x0d51, 0, {0x02,0x02,0x0e,0xcd} },
378{ 5, 0x0d55, 0, {0xba,0x21,0x02,0x80,0x03} },
379{ 3, 0x0d5a, 0, {0x02,0x0e,0xcd} },
380{ 11, 0x0d5d, 0, {0x75,0x37,0x01,0x90,0x7f,0xc5,0xe4,0xf0,0x02,0x0e,0xcd} },
381{ 3, 0x0d68, 0, {0xbb,0x03,0x1f} },
382{ 6, 0x0d6b, 0, {0xba,0x00,0x03,0x02,0x0e,0xcd} },
383{ 5, 0x0d71, 0, {0xba,0x02,0x02,0x80,0x03} },
384{ 3, 0x0d76, 0, {0x02,0x0e,0xcd} },
385{ 13, 0x0d79, 0, {0x75,0x59,0x01,0xc0,0x02,0xc0,0x03,0x12,0x0e,0xf0,0xd0,0x03,0xd0} },
386{ 4, 0x0d86, 0, {0x02,0x02,0x0e,0xcd} },
387{ 3, 0x0d8a, 0, {0xbb,0x06,0x54} },
388{ 5, 0x0d8d, 0, {0xba,0x80,0x02,0x80,0x03} },
389{ 3, 0x0d92, 0, {0x02,0x0e,0xc5} },
390{ 8, 0x0d95, 0, {0x90,0x7f,0xeb,0xe0,0xfc,0xbc,0x01,0x15} },
391{ 12, 0x0d9d, 0, {0x7c,0xfb,0x7d,0x0f,0x8d,0x06,0x7f,0x00,0x90,0x7f,0xd4,0xee} },
392{ 9, 0x0da9, 0, {0xf0,0x90,0x7f,0xd5,0xec,0xf0,0x02,0x0e,0xcd} },
393{ 10, 0x0db2, 0, {0x90,0x7f,0xeb,0xe0,0xfc,0xbc,0x02,0x02,0x80,0x03} },
394{ 3, 0x0dbc, 0, {0x02,0x0e,0xc5} },
395{ 10, 0x0dbf, 0, {0x90,0x7f,0xea,0xe0,0xfc,0xbc,0x00,0x02,0x80,0x03} },
396{ 3, 0x0dc9, 0, {0x02,0x0e,0xc5} },
397{ 12, 0x0dcc, 0, {0x7c,0x3b,0x7d,0x0f,0x8d,0x06,0x7f,0x00,0x90,0x7f,0xd4,0xee} },
398{ 9, 0x0dd8, 0, {0xf0,0x90,0x7f,0xd5,0xec,0xf0,0x02,0x0e,0xcd} },
399{ 6, 0x0de1, 0, {0xbb,0x07,0x03,0x02,0x0e,0xc5} },
400{ 3, 0x0de7, 0, {0xbb,0x08,0x10} },
401{ 13, 0x0dea, 0, {0xac,0x48,0x90,0x7f,0x00,0xec,0xf0,0x90,0x7f,0xb5,0x74,0x01,0xf0} },
402{ 3, 0x0df7, 0, {0x02,0x0e,0xcd} },
403{ 3, 0x0dfa, 0, {0xbb,0x09,0x31} },
404{ 5, 0x0dfd, 0, {0xba,0x00,0x02,0x80,0x03} },
405{ 3, 0x0e02, 0, {0x02,0x0e,0xc5} },
406{ 14, 0x0e05, 0, {0x90,0x7f,0xea,0xe0,0xfc,0xc3,0x74,0x01,0x9c,0x50,0x03,0x02,0x0e,0xc5} },
407{ 8, 0x0e13, 0, {0x90,0x7f,0xea,0xe0,0xfc,0xbc,0x00,0x0a} },
408{ 10, 0x0e1b, 0, {0x90,0x17,0x21,0xe4,0xf0,0x90,0x17,0x22,0xe4,0xf0} },
409{ 9, 0x0e25, 0, {0x90,0x7f,0xea,0xe0,0xf5,0x48,0x02,0x0e,0xcd} },
410{ 3, 0x0e2e, 0, {0xbb,0x0a,0x27} },
411{ 5, 0x0e31, 0, {0xba,0x81,0x02,0x80,0x03} },
412{ 3, 0x0e36, 0, {0x02,0x0e,0xc5} },
413{ 14, 0x0e39, 0, {0x90,0x7f,0xec,0xe0,0xfa,0x24,0x20,0xfa,0xe4,0x34,0x17,0xfc,0x8a,0x82} },
414{ 14, 0x0e47, 0, {0x8c,0x83,0xe0,0xfa,0x90,0x7f,0x00,0xf0,0x90,0x7f,0xb5,0x74,0x01,0xf0} },
415{ 3, 0x0e55, 0, {0x02,0x0e,0xcd} },
416{ 5, 0x0e58, 0, {0xbb,0x0b,0x02,0x80,0x03} },
417{ 3, 0x0e5d, 0, {0x02,0x0e,0xa9} },
418{ 13, 0x0e60, 0, {0x90,0x17,0x20,0xe4,0xf0,0x90,0x7f,0xec,0xe0,0xfa,0xba,0x01,0x1a} },
419{ 8, 0x0e6d, 0, {0x90,0x7f,0xed,0xe0,0xfa,0xba,0x00,0x12} },
420{ 14, 0x0e75, 0, {0x90,0x7f,0xea,0xe0,0xfa,0x90,0x17,0x21,0xf0,0xc0,0x03,0x12,0x04,0xe2} },
421{ 4, 0x0e83, 0, {0xd0,0x03,0x80,0x46} },
422{ 8, 0x0e87, 0, {0x90,0x7f,0xec,0xe0,0xfa,0xba,0x02,0x3e} },
423{ 8, 0x0e8f, 0, {0x90,0x7f,0xed,0xe0,0xfa,0xba,0x00,0x36} },
424{ 13, 0x0e97, 0, {0xc0,0x03,0x12,0x04,0xef,0xd0,0x03,0x90,0x7f,0xea,0xe0,0xfa,0x90} },
425{ 5, 0x0ea4, 0, {0x17,0x22,0xf0,0x80,0x24} },
426{ 5, 0x0ea9, 0, {0xbb,0x12,0x02,0x80,0x17} },
427{ 5, 0x0eae, 0, {0xbb,0x81,0x02,0x80,0x0d} },
428{ 5, 0x0eb3, 0, {0xbb,0x83,0x02,0x80,0x08} },
429{ 5, 0x0eb8, 0, {0xbb,0x82,0x02,0x80,0x03} },
430{ 3, 0x0ebd, 0, {0xbb,0x84,0x05} },
431{ 5, 0x0ec0, 0, {0x12,0x06,0x4e,0x80,0x08} },
432{ 8, 0x0ec5, 0, {0x90,0x7f,0xb4,0x74,0x03,0xf0,0x80,0x06} },
433{ 6, 0x0ecd, 0, {0x90,0x7f,0xb4,0x74,0x02,0xf0} },
434{ 2, 0x0ed3, 0, {0xd0,0x86} },
435{ 14, 0x0ed5, 0, {0xd0,0xd0,0xd0,0x01,0xd0,0x00,0xd0,0x07,0xd0,0x06,0xd0,0x05,0xd0,0x04} },
436{ 13, 0x0ee3, 0, {0xd0,0x03,0xd0,0x02,0xd0,0x83,0xd0,0x82,0xd0,0xf0,0xd0,0xe0,0x32} },
437{ 11, 0x0ef0, 0, {0x90,0x7f,0xec,0xe0,0xf5,0x5a,0xc3,0x94,0x01,0x40,0x1d} },
438{ 7, 0x0efb, 0, {0xc3,0x74,0x07,0x95,0x5a,0x40,0x16} },
439{ 13, 0x0f02, 0, {0xe5,0x5a,0x24,0xff,0x25,0xe0,0xfa,0x24,0xc6,0xf5,0x82,0xe4,0x34} },
440{ 9, 0x0f0f, 0, {0x7f,0xf5,0x83,0xaa,0x59,0xea,0xf0,0x80,0x22} },
441{ 7, 0x0f18, 0, {0xc3,0xe5,0x5a,0x94,0x81,0x40,0x1b} },
442{ 7, 0x0f1f, 0, {0xc3,0x74,0x87,0x95,0x5a,0x40,0x14} },
443{ 13, 0x0f26, 0, {0xe5,0x5a,0x24,0xff,0x25,0xe0,0xfa,0x24,0xb6,0xf5,0x82,0xe4,0x34} },
444{ 7, 0x0f33, 0, {0x7f,0xf5,0x83,0xaa,0x59,0xea,0xf0} },
445{ 1, 0x0f3a, 0, {0x22} },
446{ 14, 0x0f3b, 0, {0x09,0x02,0xba,0x00,0x03,0x01,0x00,0x40,0x00,0x09,0x04,0x00,0x00,0x00} },
447{ 14, 0x0f49, 0, {0x01,0x01,0x00,0x00,0x09,0x24,0x01,0x00,0x01,0x3d,0x00,0x01,0x01,0x0c} },
448{ 14, 0x0f57, 0, {0x24,0x02,0x01,0x10,0x07,0x00,0x02,0x03,0x00,0x00,0x00,0x0d,0x24,0x06} },
449{ 14, 0x0f65, 0, {0x03,0x01,0x02,0x15,0x00,0x03,0x00,0x03,0x00,0x00,0x09,0x24,0x03,0x02} },
450{ 14, 0x0f73, 0, {0x01,0x01,0x00,0x01,0x00,0x09,0x24,0x03,0x04,0x02,0x03,0x00,0x03,0x00} },
451{ 14, 0x0f81, 0, {0x09,0x24,0x03,0x05,0x03,0x06,0x00,0x01,0x00,0x09,0x04,0x01,0x00,0x00} },
452{ 14, 0x0f8f, 0, {0x01,0x02,0x00,0x00,0x09,0x04,0x01,0x01,0x01,0x01,0x02,0x00,0x00,0x07} },
453{ 14, 0x0f9d, 0, {0x24,0x01,0x02,0x01,0x01,0x00,0x0b,0x24,0x02,0x01,0x02,0x02,0x10,0x01} },
454{ 14, 0x0fab, 0, {0x80,0xbb,0x00,0x09,0x05,0x88,0x05,0x00,0x01,0x01,0x00,0x00,0x07,0x25} },
455{ 14, 0x0fb9, 0, {0x01,0x00,0x00,0x00,0x00,0x09,0x04,0x02,0x00,0x02,0x00,0x00,0x00,0x00} },
456{ 14, 0x0fc7, 0, {0x07,0x05,0x82,0x02,0x40,0x00,0x00,0x07,0x05,0x02,0x02,0x40,0x00,0x00} },
457{ 14, 0x0fd5, 0, {0x09,0x04,0x02,0x01,0x03,0x00,0x00,0x00,0x00,0x07,0x05,0x82,0x02,0x40} },
458{ 14, 0x0fe3, 0, {0x00,0x00,0x07,0x05,0x02,0x02,0x40,0x00,0x00,0x09,0x05,0x89,0x05,0xa0} },
459{ 10, 0x0ff1, 0, {0x01,0x01,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0x00} },
460{ 14, 0x0ffb, 0, {0x12,0x01,0x00,0x01,0x00,0x00,0x00,0x40,0x47,0x05,0x99,0x99,0x00,0x01} },
461{ 14, 0x1009, 0, {0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x02,0xba} },
462{ 4, 0x1017, 0, {0x00,0x03,0x01,0x00} },
463{ 2, 0x101b, 0, {0x7a,0x00} },
464{ 3, 0x101d, 0, {0xba,0x05,0x00} },
465{ 2, 0x1020, 0, {0x50,0x17} },
466{ 8, 0x1022, 0, {0x90,0x7f,0xa5,0xe0,0xfb,0x30,0xe0,0x05} },
467{ 5, 0x102a, 0, {0x90,0x00,0x01,0x80,0x0d} },
468{ 10, 0x102f, 0, {0xc0,0x02,0x12,0x01,0xdd,0xd0,0x02,0x0a,0x80,0xe4} },
469{ 3, 0x1039, 0, {0x90,0x00,0x01} },
470{ 1, 0x103c, 0, {0x22} },
471{ 14, 0x103d, 0, {0x90,0x7d,0xc1,0xe0,0xf9,0xa3,0xe0,0xfa,0xa3,0xe0,0xfb,0x7c,0x00,0x7d} },
472{ 4, 0x104b, 0, {0x7e,0xeb,0x60,0x12} },
473{ 14, 0x104f, 0, {0x89,0x82,0x8a,0x83,0xe0,0xa3,0xa9,0x82,0xaa,0x83,0x8c,0x82,0x8d,0x83} },
474{ 4, 0x105d, 0, {0xf0,0x0c,0xdb,0xee} },
475{ 8, 0x1061, 0, {0x90,0x7d,0xc3,0xe0,0x90,0x7f,0xb9,0xf0} },
476{ 1, 0x1069, 0, {0x22} },
477{ 14, 0x106a, 0, {0x90,0x7d,0xc1,0xe0,0xf9,0xa3,0xe0,0xfa,0xa3,0xe0,0xfb,0x7c,0xc4,0x7d} },
478{ 4, 0x1078, 0, {0x7d,0xeb,0x60,0xe5} },
479{ 14, 0x107c, 0, {0x8c,0x82,0x8d,0x83,0xe0,0x0c,0x89,0x82,0x8a,0x83,0xf0,0xa3,0xa9,0x82} },
480{ 4, 0x108a, 0, {0xaa,0x83,0xdb,0xee} },
481{ 1, 0x108e, 0, {0x22} },
482{ 14, 0x108f, 0, {0x90,0x7f,0xa5,0x74,0x80,0xf0,0x05,0x86,0x90,0x7d,0xc1,0xe0,0x05,0x86} },
483{ 14, 0x109d, 0, {0xa3,0xf0,0x12,0x10,0x1b,0x90,0x7f,0xa6,0x05,0x86,0xa3,0xa3,0xe0,0xf9} },
484{ 5, 0x10ab, 0, {0x60,0x16,0xa3,0x05,0x86} },
485{ 13, 0x10b0, 0, {0x90,0x7f,0xa6,0x05,0x86,0xe0,0xa3,0x05,0x86,0xf0,0xc0,0x01,0x12} },
486{ 6, 0x10bd, 0, {0x10,0x1b,0xd0,0x01,0xd9,0xed} },
487{ 6, 0x10c3, 0, {0x90,0x7f,0xa5,0x74,0x40,0xf0} },
488{ 1, 0x10c9, 0, {0x22} },
489{ 8, 0x10ca, 0, {0x90,0x88,0x02,0x74,0x01,0xf0,0x7a,0x00} },
490{ 3, 0x10d2, 0, {0xba,0xff,0x00} },
491{ 2, 0x10d5, 0, {0x50,0x0a} },
492{ 10, 0x10d7, 0, {0xc0,0x02,0x12,0x01,0xdd,0xd0,0x02,0x0a,0x80,0xf1} },
493{ 1, 0x10e1, 0, {0x22} },
494{ 5, 0x10e2, 0, {0xe5,0x6b,0xb4,0xc0,0x08} },
495{ 8, 0x10e7, 0, {0x90,0x88,0x03,0xe5,0x6c,0xf0,0x80,0x06} },
496{ 6, 0x10ef, 0, {0x90,0x88,0x02,0xe5,0x6c,0xf0} },
497{ 4, 0x10f5, 0, {0x7a,0x00,0x7b,0x00} },
498{ 11, 0x10f9, 0, {0xc3,0xea,0x94,0x32,0xeb,0x64,0x80,0x94,0x80,0x50,0x07} },
499{ 5, 0x1104, 0, {0x0a,0xba,0x00,0x01,0x0b} },
500{ 2, 0x1109, 0, {0x80,0xee} },
501{ 1, 0x110b, 0, {0x22} },
502{ 10, 0x110c, 0, {0x90,0x88,0x03,0xe5,0x6d,0xf0,0x05,0x39,0x7a,0x00} },
503{ 3, 0x1116, 0, {0xba,0x28,0x00} },
504{ 2, 0x1119, 0, {0x50,0x03} },
505{ 3, 0x111b, 0, {0x0a,0x80,0xf8} },
506{ 5, 0x111e, 0, {0xe5,0x39,0xb4,0x10,0x08} },
507{ 8, 0x1123, 0, {0x90,0x88,0x02,0x74,0xc0,0xf0,0x80,0x0e} },
508{ 5, 0x112b, 0, {0xe5,0x39,0xb4,0x20,0x09} },
509{ 9, 0x1130, 0, {0x90,0x88,0x02,0x74,0x80,0xf0,0x75,0x39,0x00} },
510{ 2, 0x1139, 0, {0x7a,0x00} },
511{ 3, 0x113b, 0, {0xba,0x28,0x00} },
512{ 2, 0x113e, 0, {0x50,0x03} },
513{ 3, 0x1140, 0, {0x0a,0x80,0xf8} },
514{ 1, 0x1143, 0, {0x22} },
515{ 4, 0x1144, 0, {0xe5,0x6f,0x60,0x02} },
516{ 2, 0x1148, 0, {0x80,0x07} },
517{ 7, 0x114a, 0, {0x7a,0x00,0x75,0x39,0x00,0x80,0x05} },
518{ 5, 0x1151, 0, {0x7a,0x40,0x75,0x39,0x10} },
519{ 9, 0x1156, 0, {0xe5,0x6e,0x2a,0xfa,0xe5,0x6e,0x25,0x39,0xf5} },
520{ 10, 0x115f, 0, {0x39,0x90,0x88,0x02,0x74,0x80,0x2a,0xf0,0x7a,0x00} },
521{ 8, 0x1169, 0, {0xc3,0xea,0x64,0x80,0x94,0xa8,0x50,0x03} },
522{ 3, 0x1171, 0, {0x0a,0x80,0xf5} },
523{ 1, 0x1174, 0, {0x22} },
524{ 6, 0x1175, 0, {0xaa,0x70,0xab,0x71,0xac,0x72} },
525{ 12, 0x117b, 0, {0x8a,0x82,0x8b,0x83,0x8c,0xf0,0x12,0x14,0xee,0xfd,0x60,0x18} },
526{ 13, 0x1187, 0, {0x8d,0x6d,0xc0,0x02,0xc0,0x03,0xc0,0x04,0x12,0x11,0x0c,0xd0,0x04} },
527{ 9, 0x1194, 0, {0xd0,0x03,0xd0,0x02,0x0a,0xba,0x00,0x01,0x0b} },
528{ 2, 0x119d, 0, {0x80,0xdc} },
529{ 1, 0x119f, 0, {0x22} },
530{ 13, 0x11a0, 0, {0xe5,0x73,0xc4,0x54,0x0f,0xfa,0x53,0x02,0x0f,0xc3,0x74,0x09,0x9a} },
531{ 2, 0x11ad, 0, {0x50,0x06} },
532{ 6, 0x11af, 0, {0x74,0x37,0x2a,0xfb,0x80,0x04} },
533{ 4, 0x11b5, 0, {0x74,0x30,0x2a,0xfb} },
534{ 12, 0x11b9, 0, {0x8b,0x6d,0xc0,0x03,0x12,0x11,0x0c,0xd0,0x03,0xaa,0x73,0x53} },
535{ 8, 0x11c5, 0, {0x02,0x0f,0xc3,0x74,0x09,0x9a,0x50,0x06} },
536{ 6, 0x11cd, 0, {0x74,0x37,0x2a,0xfb,0x80,0x04} },
537{ 4, 0x11d3, 0, {0x74,0x30,0x2a,0xfb} },
538{ 5, 0x11d7, 0, {0x8b,0x6d,0x12,0x11,0x0c} },
539{ 1, 0x11dc, 0, {0x22} },
540{ 7, 0x11dd, 0, {0x90,0x7d,0xc3,0xe0,0xfa,0x60,0x0f} },
541{ 12, 0x11e4, 0, {0x90,0x7d,0xc1,0xe0,0xf5,0x6e,0x90,0x7d,0xc2,0xe0,0xf5,0x6f} },
542{ 3, 0x11f0, 0, {0x12,0x11,0x44} },
543{ 12, 0x11f3, 0, {0x90,0x7d,0xff,0xe4,0xf0,0x75,0x70,0xc4,0x75,0x71,0x7d,0x75} },
544{ 5, 0x11ff, 0, {0x72,0x01,0x12,0x11,0x75} },
545{ 1, 0x1204, 0, {0x22} },
546{ 2, 0x1205, 0, {0x7a,0x04} },
547{ 3, 0x1207, 0, {0xba,0x40,0x00} },
548{ 2, 0x120a, 0, {0x50,0x36} },
549{ 14, 0x120c, 0, {0xea,0x24,0xc0,0xf5,0x82,0xe4,0x34,0x7d,0xf5,0x83,0xe0,0xfb,0x7c,0x00} },
550{ 3, 0x121a, 0, {0xbc,0x08,0x00} },
551{ 2, 0x121d, 0, {0x50,0x20} },
552{ 6, 0x121f, 0, {0x8b,0x05,0xed,0x30,0xe7,0x0b} },
553{ 11, 0x1225, 0, {0x90,0x7f,0x96,0x74,0x42,0xf0,0x74,0xc3,0xf0,0x80,0x08} },
554{ 8, 0x1230, 0, {0x90,0x7f,0x96,0xe4,0xf0,0x74,0x81,0xf0} },
555{ 7, 0x1238, 0, {0xeb,0x25,0xe0,0xfb,0x0c,0x80,0xdb} },
556{ 3, 0x123f, 0, {0x0a,0x80,0xc5} },
557{ 1, 0x1242, 0, {0x22} },
558{ 4, 0x1243, 0, {0x7a,0x00,0x7b,0xef} },
559{ 3, 0x1247, 0, {0xba,0x10,0x00} },
560{ 2, 0x124a, 0, {0x50,0x20} },
561{ 14, 0x124c, 0, {0x74,0x11,0x2b,0xfb,0x24,0x00,0xfc,0xe4,0x34,0x18,0xfd,0x8c,0x82,0x8d} },
562{ 14, 0x125a, 0, {0x83,0xe4,0xf0,0xea,0x24,0x00,0xf5,0x82,0xe4,0x34,0x19,0xf5,0x83,0xe4} },
563{ 4, 0x1268, 0, {0xf0,0x0a,0x80,0xdb} },
564{ 1, 0x126c, 0, {0x22} },
565{ 14, 0x126d, 0, {0x74,0xf8,0x24,0x00,0xf5,0x82,0x74,0x03,0x34,0x84,0xf5,0x83,0xe4,0xf0} },
566{ 14, 0x127b, 0, {0x74,0xf9,0x24,0x00,0xf5,0x82,0x74,0x03,0x34,0x84,0xf5,0x83,0xe4,0xf0} },
567{ 14, 0x1289, 0, {0x74,0xfa,0x24,0x00,0xf5,0x82,0x74,0x03,0x34,0x84,0xf5,0x83,0xe4,0xf0} },
568{ 14, 0x1297, 0, {0x74,0xfb,0x24,0x00,0xf5,0x82,0x74,0x03,0x34,0x84,0xf5,0x83,0xe4,0xf0} },
569{ 14, 0x12a5, 0, {0x74,0xff,0x24,0x00,0xf5,0x82,0x74,0x03,0x34,0x84,0xf5,0x83,0xe4,0xf0} },
570{ 1, 0x12b3, 0, {0x22} },
571{ 14, 0x12b4, 0, {0x12,0x03,0xcb,0x12,0x12,0x6d,0x7a,0xc0,0x7b,0x87,0x7c,0x01,0x74,0x01} },
572{ 14, 0x12c2, 0, {0x2a,0xfd,0xe4,0x3b,0xfe,0x8c,0x07,0x8a,0x82,0x8b,0x83,0x8c,0xf0,0x74} },
573{ 14, 0x12d0, 0, {0x01,0x12,0x14,0xbf,0x2d,0xfa,0xe4,0x3e,0xfb,0x8f,0x04,0x8d,0x82,0x8e} },
574{ 14, 0x12de, 0, {0x83,0x8f,0xf0,0x74,0x06,0x12,0x14,0xbf,0x74,0x01,0x2a,0xfd,0xe4,0x3b} },
575{ 14, 0x12ec, 0, {0xfe,0x8c,0x07,0x8a,0x82,0x8b,0x83,0x8c,0xf0,0xe4,0x12,0x14,0xbf,0x74} },
576{ 14, 0x12fa, 0, {0x01,0x2d,0xfa,0xe4,0x3e,0xfb,0x8f,0x04,0x8d,0x82,0x8e,0x83,0x8f,0xf0} },
577{ 14, 0x1308, 0, {0x74,0x0b,0x12,0x14,0xbf,0x74,0x01,0x2a,0xfd,0xe4,0x3b,0xfe,0x8c,0x07} },
578{ 14, 0x1316, 0, {0x8a,0x82,0x8b,0x83,0x8c,0xf0,0x74,0x08,0x12,0x14,0xbf,0x74,0x01,0x2d} },
579{ 14, 0x1324, 0, {0xfa,0xe4,0x3e,0xfb,0x8f,0x04,0x8d,0x82,0x8e,0x83,0x8f,0xf0,0x74,0x01} },
580{ 14, 0x1332, 0, {0x12,0x14,0xbf,0x2a,0xfd,0xe4,0x3b,0xfe,0x8c,0x07,0x8a,0x82,0x8b,0x83} },
581{ 14, 0x1340, 0, {0x8c,0xf0,0xe4,0x12,0x14,0xbf,0x74,0x01,0x2d,0xfa,0xe4,0x3e,0xfb,0x8f} },
582{ 14, 0x134e, 0, {0x04,0x8d,0x82,0x8e,0x83,0x8f,0xf0,0x74,0x03,0x12,0x14,0xbf,0x7d,0x00} },
583{ 3, 0x135c, 0, {0xbd,0x06,0x00} },
584{ 2, 0x135f, 0, {0x50,0x12} },
585{ 11, 0x1361, 0, {0x8a,0x82,0x8b,0x83,0x8c,0xf0,0x0a,0xba,0x00,0x01,0x0b} },
586{ 7, 0x136c, 0, {0xe4,0x12,0x14,0xbf,0x0d,0x80,0xe9} },
587{ 13, 0x1373, 0, {0x8a,0x82,0x8b,0x83,0x8c,0xf0,0xe5,0x74,0x12,0x14,0xbf,0x74,0xf9} },
588{ 14, 0x1380, 0, {0x24,0x00,0xf5,0x82,0x74,0x03,0x34,0x84,0xf5,0x83,0x74,0x0f,0xf0,0x74} },
589{ 14, 0x138e, 0, {0xfe,0x24,0x00,0xf5,0x82,0x74,0x03,0x34,0x84,0xf5,0x83,0x74,0x01,0xf0} },
590{ 6, 0x139c, 0, {0x12,0x03,0xe1,0x12,0x04,0xf7} },
591{ 1, 0x13a2, 0, {0x22} },
592{ 13, 0x13a3, 0, {0x90,0x7d,0xc1,0xe0,0xfa,0x24,0x00,0xfb,0xe4,0x34,0x19,0xfc,0x90} },
593{ 14, 0x13b0, 0, {0x7d,0xc2,0xe0,0xfd,0x8b,0x82,0x8c,0x83,0xf0,0x75,0xf0,0x11,0xea,0xa4} },
594{ 3, 0x13be, 0, {0xfa,0x7b,0x00} },
595{ 3, 0x13c1, 0, {0xbb,0x10,0x00} },
596{ 2, 0x13c4, 0, {0x50,0x24} },
597{ 14, 0x13c6, 0, {0xea,0x24,0x00,0xfc,0xe4,0x34,0x18,0xfd,0xeb,0x2c,0xfc,0xe4,0x3d,0xfd} },
598{ 14, 0x13d4, 0, {0x74,0x04,0x2b,0x24,0xc0,0xf5,0x82,0xe4,0x34,0x7d,0xf5,0x83,0xe0,0xfe} },
599{ 8, 0x13e2, 0, {0x8c,0x82,0x8d,0x83,0xf0,0x0b,0x80,0xd7} },
600{ 14, 0x13ea, 0, {0xea,0x24,0x00,0xfa,0xe4,0x34,0x18,0xfb,0x74,0x10,0x2a,0xf5,0x82,0xe4} },
601{ 5, 0x13f8, 0, {0x3b,0xf5,0x83,0xe4,0xf0} },
602{ 1, 0x13fd, 0, {0x22} },
603{ 4, 0x13fe, 0, {0xe5,0x76,0x60,0x02} },
604{ 2, 0x1402, 0, {0x80,0x16} },
605{ 12, 0x1404, 0, {0x74,0x0f,0x55,0x75,0xfa,0x8a,0x75,0x24,0x00,0xf5,0x82,0xe4} },
606{ 10, 0x1410, 0, {0x34,0x19,0xf5,0x83,0xe0,0xf5,0x74,0x12,0x12,0xb4} },
607{ 10, 0x141a, 0, {0x12,0x10,0xca,0x75,0x6e,0x00,0x75,0x6f,0x00,0x12} },
608{ 6, 0x1424, 0, {0x11,0x44,0x75,0x70,0xb9,0x75} },
609{ 6, 0x142a, 0, {0x71,0x14,0x75,0x72,0x02,0x12} },
610{ 11, 0x1430, 0, {0x11,0x75,0xe5,0x76,0xb4,0x02,0x04,0x74,0x01,0x80,0x01} },
611{ 1, 0x143b, 0, {0xe4} },
612{ 3, 0x143c, 0, {0xfa,0x70,0x0f} },
613{ 12, 0x143f, 0, {0x74,0x01,0x25,0x75,0xf5,0x73,0xc0,0x02,0x12,0x11,0xa0,0xd0} },
614{ 3, 0x144b, 0, {0x02,0x80,0x0a} },
615{ 10, 0x144e, 0, {0x85,0x75,0x73,0xc0,0x02,0x12,0x11,0xa0,0xd0,0x02} },
616{ 12, 0x1458, 0, {0x75,0x6e,0x00,0x75,0x6f,0x01,0xc0,0x02,0x12,0x11,0x44,0xd0} },
617{ 4, 0x1464, 0, {0x02,0xea,0x70,0x1a} },
618{ 13, 0x1468, 0, {0x75,0xf0,0x11,0xe5,0x75,0xa4,0xfa,0x24,0x00,0xfa,0xe4,0x34,0x18} },
619{ 9, 0x1475, 0, {0xfb,0x8a,0x70,0x8b,0x71,0x75,0x72,0x01,0x12} },
620{ 4, 0x147e, 0, {0x11,0x75,0x80,0x36} },
621{ 2, 0x1482, 0, {0x7a,0x00} },
622{ 3, 0x1484, 0, {0xba,0x10,0x00} },
623{ 2, 0x1487, 0, {0x50,0x2f} },
624{ 13, 0x1489, 0, {0xea,0x24,0x00,0xf5,0x82,0xe4,0x34,0x19,0xf5,0x83,0xe0,0xfb,0xe5} },
625{ 4, 0x1496, 0, {0x75,0xb5,0x03,0x1b} },
626{ 14, 0x149a, 0, {0x75,0xf0,0x11,0xea,0xa4,0xfb,0x24,0x00,0xfb,0xe4,0x34,0x18,0xfc,0x8b} },
627{ 9, 0x14a8, 0, {0x70,0x8c,0x71,0x75,0x72,0x01,0xc0,0x02,0x12} },
628{ 4, 0x14b1, 0, {0x11,0x75,0xd0,0x02} },
629{ 3, 0x14b5, 0, {0x0a,0x80,0xcc} },
630{ 1, 0x14b8, 0, {0x22} },
631{ 6, 0x14b9, 0, {0x50,0x72,0x6f,0x67,0x20,0x00} },
632{ 14, 0x14bf, 0, {0xc8,0xc0,0xe0,0xc8,0xc0,0xe0,0xe5,0xf0,0x60,0x0b,0x14,0x60,0x0f,0x14} },
633{ 7, 0x14cd, 0, {0x60,0x11,0x14,0x60,0x12,0x80,0x15} },
634{ 7, 0x14d4, 0, {0xd0,0xe0,0xa8,0x82,0xf6,0x80,0x0e} },
635{ 5, 0x14db, 0, {0xd0,0xe0,0xf0,0x80,0x09} },
636{ 4, 0x14e0, 0, {0xd0,0xe0,0x80,0x05} },
637{ 5, 0x14e4, 0, {0xd0,0xe0,0xa8,0x82,0xf2} },
638{ 4, 0x14e9, 0, {0xc8,0xd0,0xe0,0xc8} },
639{ 1, 0x14ed, 0, {0x22} },
640{ 14, 0x14ee, 0, {0xc8,0xc0,0xe0,0xe5,0xf0,0x60,0x0d,0x14,0x60,0x0f,0x14,0x60,0x0f,0x14} },
641{ 6, 0x14fc, 0, {0x60,0x10,0x74,0xff,0x80,0x0f} },
642{ 5, 0x1502, 0, {0xa8,0x82,0xe6,0x80,0x0a} },
643{ 3, 0x1507, 0, {0xe0,0x80,0x07} },
644{ 4, 0x150a, 0, {0xe4,0x93,0x80,0x03} },
645{ 3, 0x150e, 0, {0xa8,0x82,0xe2} },
646{ 4, 0x1511, 0, {0xf8,0xd0,0xe0,0xc8} },
647{ 1, 0x1515, 0, {0x22} },
648{ 0, 0x0000, 1, {0} }
649
650};
651
652static unsigned char bitstream[] = {
653
6540x00,0x09,0x0F,0xF0,0x0F,0xF0,0x0F,0xF0, 0x0F,0xF0,0x00,0x00,0x01,0x61,0x00,0x0D,
6550x64,0x61,0x62,0x75,0x73,0x62,0x74,0x72, 0x2E,0x6E,0x63,0x64,0x00,0x62,0x00,0x0B,
6560x73,0x31,0x30,0x78,0x6C,0x76,0x71,0x31, 0x30,0x30,0x00,0x63,0x00,0x0B,0x31,0x39,
6570x39,0x39,0x2F,0x30,0x39,0x2F,0x32,0x34, 0x00,0x64,0x00,0x09,0x31,0x30,0x3A,0x34,
6580x32,0x3A,0x34,0x36,0x00,0x65,0x00,0x00, 0x2E,0xC0,0xFF,0x20,0x17,0x5F,0x9F,0x5B,
6590xFE,0xFB,0xBB,0xB7,0xBB,0xBB,0xFB,0xBF, 0xAF,0xEF,0xFB,0xDF,0xB7,0xFB,0xFB,0x7F,
6600xBF,0xB7,0xEF,0xF2,0xFF,0xFB,0xFE,0xFF, 0xFF,0xEF,0xFF,0xFE,0xFF,0xBF,0xFF,0xFF,
6610xFF,0xFF,0xAF,0xFF,0xFA,0xFF,0xFF,0xFF, 0xC9,0xFF,0xFF,0xFF,0xDF,0xFF,0xFF,0xFF,
6620xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFB,0xFF,0xA3,0xFF,0xFB,
6630xFE,0xFF,0xBF,0xEF,0xE3,0xFE,0xFF,0xBF, 0xE3,0xFE,0xFF,0xBF,0x6F,0xFB,0xF6,0xFF,
6640xBF,0xFF,0x47,0xFF,0xFF,0x9F,0xEE,0xF9, 0xFE,0xCF,0x9F,0xEF,0xFB,0xCF,0x9B,0xEE,
6650xF8,0xFE,0xEF,0x8F,0xEE,0xFB,0xFE,0x0B, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
6660xFF,0xFF,0xBF,0xFF,0xFF,0xFB,0xFF,0xFF, 0xBF,0xFF,0xFF,0xFC,0x17,0xFF,0xFF,0xFF,
6670xFF,0xFF,0xFF,0x7F,0xFF,0xFF,0xFF,0x7F, 0xFF,0xFF,0xFB,0xFF,0xFF,0x7F,0xFF,0xFF,
6680xFC,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFB,0xFF,0xFF,0xFF,0xFF,
6690xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x5F,0xFF, 0xFF,0xFD,0xFF,0xFF,0xDB,0xFF,0xFD,0xFF,
6700x77,0xFF,0xFD,0xFF,0xFF,0xDF,0xFE,0xFD, 0xFF,0xFF,0xF2,0xFF,0xFF,0xFF,0xFF,0xFF,
6710xFF,0xFD,0xFF,0xFF,0xFF,0xFD,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE1,
6720x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0x3F,0xFF,0xFF,
6730xFF,0xFF,0xFF,0xFF,0xE3,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xBF,
6740xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0x67,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
6750x7F,0xFF,0xFF,0xFF,0x7F,0xFF,0xFF,0xFF, 0xFF,0xFF,0xDF,0xFF,0xFF,0xFF,0x2F,0xFF,
6760xF3,0xFD,0xFF,0x7F,0xDE,0xF7,0xFD,0xFF, 0x7F,0xF7,0x7D,0xFF,0x7F,0xDF,0xF7,0xBD,
6770xFF,0x7F,0xFF,0x1F,0xFF,0xEF,0xFB,0xFE, 0xFF,0xBF,0xEF,0xFB,0xFE,0xFF,0xEF,0xFB,
6780xFE,0xFF,0xBF,0xEF,0xFB,0xFE,0xFF,0xFF, 0x3F,0xFE,0x7F,0x9F,0xE7,0xF9,0xFE,0x7F,
6790x9F,0xE7,0xFA,0x7F,0x9F,0xE7,0xF9,0xFE, 0x7F,0x9F,0xE7,0xFF,0xFC,0x7F,0xBF,0xBF,
6800xEF,0xFB,0xFE,0xFF,0xBF,0xEF,0xFB,0xB7, 0xBF,0xEF,0xFB,0xFE,0xFF,0xBF,0xEF,0xFB,
6810xFF,0xE0,0xFD,0xF9,0xFE,0x7F,0x9F,0xE7, 0xF9,0xFE,0x7F,0x9D,0xF9,0xFE,0x7D,0x9D,
6820xE7,0xF9,0xFE,0x7F,0x9F,0xED,0xED,0xFF, 0xFD,0xFF,0x7F,0xDF,0xF7,0xFD,0xFF,0x7F,
6830xDF,0xFD,0xFF,0x7F,0xDF,0xF7,0xFD,0xFF, 0x7F,0xDF,0xFF,0x9B,0xFF,0xEF,0xFB,0xFE,
6840xFB,0xBF,0xEF,0xBB,0xFE,0xFF,0xAF,0xBB, 0xBE,0xFF,0xBF,0xEF,0xFB,0xFE,0xFF,0xFF,
6850xB7,0xBF,0xDB,0xF6,0xBD,0xBF,0x6B,0xDB, 0xF6,0xF9,0xBF,0x5B,0xD6,0xF9,0xBF,0x6F,
6860xDB,0xF6,0xFD,0xBF,0xFF,0x0E,0xFF,0xFF, 0xFF,0xFF,0x5F,0xFF,0xF7,0xFF,0xFF,0x7F,
6870xF7,0xBD,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xDF,0x9F,0xFF,0xFF,0xFF,0xFE,0xFF,
6880xFF,0xEF,0xFE,0xFE,0xFF,0xFF,0x77,0xFF, 0xFB,0xFB,0xFF,0xFF,0xFF,0xFF,0xF8,0x3F,
6890xFF,0xFD,0xFF,0xFF,0xFF,0xFD,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
6900xFF,0xFF,0xFF,0xF4,0x7F,0xFF,0xFE,0xFD, 0xBE,0xFF,0xDF,0xFE,0xFF,0xFF,0xEF,0x7F,
6910xFF,0xCF,0xFF,0xCF,0xFF,0xFF,0xFF,0xDF, 0xE6,0xFF,0xFF,0x7F,0xDF,0xF7,0xDD,0x7F,
6920x7F,0xDF,0xF7,0xFF,0x7F,0xDF,0xD7,0xFD, 0xFF,0x7F,0xDF,0xF7,0xFF,0xCD,0xFF,0xF2,
6930xFF,0xFF,0x4F,0x7F,0xF4,0xFF,0xFF,0xFF, 0xE7,0xEF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
6940xFF,0xFF,0xBB,0xFF,0xEF,0xFF,0xFE,0xFF, 0xFF,0xFF,0xEF,0xFF,0xFF,0xEF,0xFF,0xFB,
6950xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x65, 0xEF,0xFF,0xFF,0x7F,0xFF,0xFD,0xEF,0xFF,
6960xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFE,0xCF,0xDF,0xFE,0xFF,
6970xFF,0xFB,0xFF,0xFF,0xFF,0xFF,0xF3,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
6980xFE,0xDF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
6990xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xBF,0xFF, 0xFF,0xFF,0xE3,0x7F,0xFF,0xFF,0xFF,0xFF,
7000xFF,0xFF,0xEF,0xEB,0xFF,0xFE,0xBF,0xFF, 0xEB,0xFF,0xFC,0x7F,0xFF,0xFF,0xFF,0xEE,
7010xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xDD,0xFF, 0xD6,0xFF,0xFD,0xBF,0xFF,0xFB,0xFF,0xFE,
7020xFD,0xFF,0xFF,0xFD,0xEF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xDE,0xFF,0xFF,0xFF,0xFF,
7030xFF,0xFF,0xBF,0xFF,0xFD,0xFF,0x7F,0xBF, 0xFF,0x5F,0xDF,0xFF,0xFF,0xBF,0x77,0xFF,
7040xFF,0xFF,0x7F,0xD7,0xFF,0xFF,0xFF,0xFF, 0xFF,0xC3,0xFF,0xFF,0xFF,0xFF,0xDF,0xEF,
7050xFF,0xFF,0xFE,0xFB,0xFF,0xFF,0xDF,0xBF, 0xFF,0xFF,0xFF,0xFF,0xED,0xFF,0xB7,0xFF,
7060xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
7070xFF,0xFF,0xFF,0xAF,0x7F,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
7080xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xDF,0xBF,0xDF,0xF3,0xFD,0xFB,0xFF,0x5B,
7090xFD,0xFF,0xBF,0xEF,0xF7,0xFF,0xFF,0x7D, 0xFF,0xFF,0xFF,0xFF,0xF8,0x3B,0xFF,0xBF,
7100x6F,0xFF,0xFE,0xFF,0xBF,0xFF,0xEB,0x7D, 0xFF,0xEF,0xFB,0xFE,0xFF,0xFF,0xFF,0xFF,
7110xFF,0xF2,0x7F,0xFC,0xFF,0x3F,0xDF,0xED, 0xFE,0xFF,0xFF,0xFF,0xFF,0xEF,0x5F,0xF7,
7120xB5,0xFF,0xEF,0xFF,0xFF,0xFF,0xE0,0x3F, 0x9F,0x9E,0xFF,0xFF,0xEF,0xFF,0xDF,0xFF,
7130xBF,0x5F,0xBF,0xCF,0xF3,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0x69,0xAF,0x33,0xFD,0xFF,
7140xFB,0xFF,0xFF,0xFF,0xFF,0xFC,0xFF,0x7F, 0xD9,0xFF,0xDF,0xFF,0xFF,0xFF,0xFF,0xF5,
7150xA3,0xDF,0x6E,0xDE,0xFF,0xFF,0xBD,0xFF, 0xFF,0xFE,0xFF,0xFF,0xFF,0xFE,0xE7,0xFD,
7160xFF,0xFF,0xFF,0xF9,0xEF,0xC6,0xFE,0xB7, 0xAD,0xE5,0xF9,0xFF,0xFF,0xFF,0xCF,0xFF,
7170xFF,0xFF,0xCD,0xFB,0x7F,0xFF,0xFF,0xFF, 0xF9,0xF6,0x0F,0xDF,0xEC,0xCF,0x7F,0xFF,
7180xFB,0x7F,0xFF,0xFF,0xFF,0xFD,0xFF,0xFE, 0xF9,0xFD,0x7F,0xFF,0x7F,0xFF,0xF9,0x5B,
7190xFF,0x73,0xDC,0xFD,0x7B,0xDF,0xFF,0xFF, 0xFF,0x7B,0xFF,0xFF,0xF7,0x53,0xD6,0xFF,
7200xFF,0xFF,0xFF,0xD8,0x9F,0xFE,0xFF,0xEF, 0x7F,0xEE,0xFF,0xFF,0xFF,0xFB,0xED,0xED,
7210xFD,0xFF,0xFE,0xFF,0xFF,0xFB,0x7F,0xFF, 0xE2,0x7F,0xFF,0x6F,0xD8,0x57,0xF7,0xFF,
7220xFF,0xFF,0xDF,0xFF,0xE8,0xFF,0xFF,0xFD, 0xFF,0xFF,0xFC,0x7F,0xFF,0xE4,0xFF,0xFB,
7230xEF,0xFB,0xFE,0xDF,0xB7,0xED,0xFF,0xFE, 0xDF,0x7F,0xFF,0xFE,0x7F,0xB7,0xFF,0xFF,
7240xFF,0xFF,0x89,0xFF,0xFF,0xCF,0xF3,0xFE, 0x7F,0xFF,0xEF,0xFF,0xFE,0x7E,0x7F,0xFB,
7250xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF1, 0xFF,0xEB,0x7A,0xD5,0xBF,0x6F,0xDB,0xBE,
7260xFD,0xB7,0xD8,0xF6,0xE5,0xBF,0x6F,0xFB, 0xFE,0xF5,0xBD,0x7E,0x06,0xFF,0xDF,0xF7,
7270xFB,0xF6,0xFF,0x3F,0xFF,0xDB,0xFF,0xFF, 0x6F,0xFB,0xF7,0xFF,0xFF,0xFF,0xFB,0xFE,
7280xF7,0xAF,0xFF,0xB7,0xED,0xEF,0xF7,0xFE, 0xFF,0xFF,0xDF,0xFF,0xFE,0xFF,0xEF,0xFF,
7290xFF,0xFF,0xFF,0xBF,0xF7,0xFC,0x1F,0xEE, 0xFB,0xFE,0xBD,0xFF,0x7F,0x5F,0xD7,0xFD,
7300xFB,0x43,0xFF,0xFF,0xFD,0xFF,0x5F,0xFF, 0xF7,0xFF,0xF9,0x3F,0xFF,0xCF,0xF3,0xFD,
7310xF7,0x7E,0xEF,0xA7,0xF9,0xFE,0x8F,0xA7, 0xE9,0xF3,0x7E,0x9F,0xFB,0xF8,0xFF,0xFF,
7320x3F,0xFD,0x7F,0x5F,0xDF,0xFD,0xFF,0xFF, 0x5F,0xFF,0xFD,0x5F,0xFF,0xFF,0x7F,0xFD,
7330x7F,0xFD,0x9F,0xFF,0xE0,0xFF,0xFA,0xF8, 0xBE,0x6F,0x9F,0xE6,0xF8,0xBE,0x3F,0x9A,
7340xF9,0xBE,0x6F,0x9F,0xE2,0xF9,0xFE,0x6F, 0x9F,0xF9,0xFF,0xF5,0xFD,0x7F,0xCF,0xDF,
7350xFD,0xFD,0x7F,0xFF,0xF5,0xFF,0xFF,0xFF, 0xF7,0xF5,0xFD,0x0F,0xDB,0xFF,0xD3,0xFF,
7360xEB,0xFA,0xFF,0xFF,0xBF,0xFF,0xFA,0xFF, 0xFF,0xCB,0xFB,0xFE,0xFF,0xFF,0xEB,0xFA,
7370xFE,0xFF,0xFF,0xB7,0xFF,0xFF,0xFF,0xFF, 0xBF,0xFF,0xDF,0xF5,0xFF,0xFF,0xD7,0xFF,
7380xFF,0xFF,0xDF,0xD7,0xF5,0xFF,0x7F,0xFE, 0x4F,0xFF,0xFD,0xFF,0x7F,0x7F,0xFF,0xAD,
7390xEB,0xFB,0xFF,0xAD,0xFF,0xFF,0xFF,0xFF, 0xAF,0xEB,0xFB,0xFF,0xFC,0x0D,0xFF,0xFF,
7400xDF,0xD2,0xFD,0xFF,0xFF,0xFD,0xF6,0xFF, 0xFF,0x7F,0xFF,0xFF,0x1F,0xFF,0xFF,0xFF,
7410xFF,0xFB,0x3F,0x7D,0xEB,0x32,0xFE,0xBF, 0x2F,0xEB,0xFA,0xAE,0xBD,0xE0,0xFA,0x7E,
7420xBF,0xAD,0xEB,0xFA,0xFE,0xBF,0xF5,0x7F, 0xFF,0xDE,0xFE,0xE3,0xFB,0xFF,0xFF,0xFF,
7430xDF,0xEF,0x4F,0xDF,0xFF,0x7F,0xDF,0xFF, 0xF7,0xFF,0xFF,0xF8,0x7F,0xFF,0xFF,0xEF,
7440xFB,0xFF,0xFF,0xFF,0xEF,0xFF,0xFF,0xDF, 0xED,0xFB,0xDF,0xFF,0xBF,0xFF,0xFF,0xFF,
7450x81,0xFF,0xFF,0xFF,0xFF,0x3F,0xFF,0xFF, 0xFF,0xFF,0xFE,0xDD,0xFE,0xEF,0xFD,0xFF,
7460xFF,0xFB,0xFE,0xF7,0xFF,0x93,0xFD,0xFB, 0x7E,0xFF,0xFE,0x87,0xE9,0xFF,0x7F,0xB3,
7470x9F,0xFE,0xFE,0xFF,0xAF,0xFD,0xFE,0x7E, 0x3F,0xFE,0x67,0xFF,0xFF,0xF7,0xFF,0xFF,
7480xFC,0xF7,0xDF,0xFD,0xFF,0x7F,0xFF,0xFF, 0x7F,0x6D,0xFF,0xFF,0xFE,0xFF,0xFF,0x2F,
7490xFF,0xBF,0xFF,0xFF,0xEE,0xFF,0xBE,0xFF, 0xFF,0xFE,0xFF,0xEF,0xFF,0xFF,0xFE,0xFF,
7500xEF,0xFF,0xFF,0xFA,0x5F,0xFF,0xFF,0xFB, 0xFF,0xFF,0xEF,0xFF,0xFB,0xFE,0xFD,0xFF,
7510xFE,0xFF,0xFB,0xFF,0xFF,0xFF,0x7F,0xFF, 0xFE,0xBF,0xDF,0xFF,0xFB,0xFF,0xFF,0xF7,
7520xFC,0xFD,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F, 0xFF,0xFF,0xFF,0xFF,0xFF,0xF2,0x7F,0xFF,
7530xFF,0xFF,0xFF,0x7F,0xFF,0xFF,0xFF,0xFF, 0xF3,0xFF,0xFF,0xFF,0xEF,0xFB,0xFF,0xFF,
7540xFF,0xDF,0xE2,0xFF,0xFF,0xFB,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFB,0xE7,0xFF,0xFD,
7550xFF,0xFF,0xFF,0xBF,0xFF,0xFF,0xFF,0xED, 0xEF,0xFD,0xFF,0xFF,0xDF,0xD7,0xF5,0xFD,
7560x7F,0x5D,0xFD,0xFF,0x7F,0xDF,0x97,0xF4, 0xFD,0x7B,0x5F,0xFF,0xC9,0xFF,0xFB,0xFE,
7570xFF,0xBF,0xFF,0x5F,0xFF,0xFF,0xF7,0xFF, 0xEF,0xFD,0xFF,0xEF,0xFF,0xFF,0xFF,0xFF,
7580xFF,0xF7,0xFF,0xD7,0xFD,0x7D,0x7F,0xFF, 0xFF,0xFF,0xFF,0xEF,0xDF,0xF7,0xFD,0xFF,
7590xBB,0xFF,0xFF,0x7F,0xFF,0xFE,0xE3,0xFF, 0xF9,0xFE,0x7F,0xBF,0xEF,0xFB,0xFE,0xFF,
7600xBF,0xF9,0xFE,0xFF,0x9F,0xEF,0xF9,0xFE, 0xFF,0xBF,0xF3,0xDA,0xFF,0x37,0xCD,0xF3,
7610x7C,0xDF,0x37,0xCD,0xF3,0x7F,0x37,0xCD, 0xF3,0x7C,0xDF,0x37,0xCC,0xF3,0x7F,0x5A,
7620xBD,0xF6,0xFD,0xBF,0x6F,0xDB,0xF6,0xFD, 0xBF,0x6F,0xDE,0xFD,0xBF,0x6F,0xDB,0xF6,
7630xFD,0xBF,0x6F,0xFE,0xF1,0x6F,0xEB,0x7A, 0xDE,0xB7,0xAD,0xEB,0x7A,0xDE,0xB7,0xAF,
7640x7A,0xDE,0xB7,0xAD,0xEB,0x7A,0xDE,0xB7, 0xFF,0x7E,0xFF,0xFE,0xCD,0xB3,0x6C,0xDB,
7650x36,0xCD,0xB3,0x6C,0xDE,0xCD,0xB3,0x6C, 0xDB,0x36,0xCD,0xB3,0x6C,0xDF,0xC9,0xBF,
7660xF7,0xBD,0xEF,0x7A,0x9E,0xA7,0xA9,0xEA, 0x7A,0xB7,0xBD,0xEA,0x7B,0xDE,0xA7,0xBD,
7670xCA,0x72,0x8D,0x91,0xFF,0xEF,0xFB,0xFE, 0xFF,0xBF,0xEF,0xFB,0xFE,0xF7,0xEF,0xFB,
7680xFE,0xFF,0xBF,0xEF,0xFB,0xFE,0xFF,0xFE, 0x87,0xFF,0xF6,0xFD,0xBF,0x6F,0xDB,0xF6,
7690xFD,0xBF,0x6F,0xF6,0xFD,0xBF,0x6F,0xDB, 0xF6,0xFD,0xBF,0x6F,0xFE,0x4F,0xFF,0xBF,
7700xEF,0xBB,0xEE,0xFB,0xBE,0xEF,0xBB,0xEF, 0xBE,0xEF,0xBB,0xEE,0xFB,0xBE,0xEF,0xBB,
7710xEF,0xFC,0x5F,0xFF,0xFF,0xFF,0x3F,0xCF, 0xF3,0xFC,0xFF,0x3F,0xCF,0xFC,0xFF,0x3F,
7720xCF,0xF3,0xFC,0xFF,0x3F,0xCF,0xFD,0x9F, 0xFE,0xBF,0xAF,0xEB,0xFA,0xFE,0xBF,0xAF,
7730xEB,0xFE,0xBF,0xAF,0xEB,0xFA,0xFE,0xBF, 0xAF,0xEB,0xFF,0xE1,0x6F,0xFD,0xFF,0x7F,
7740xDF,0xF7,0xFD,0xFF,0x7F,0xDF,0xFD,0xFF, 0x7F,0xDF,0xF7,0xFD,0xFF,0x7F,0xDF,0xFF,
7750x7A,0xBF,0xFB,0xFE,0xDF,0xB7,0xED,0xFB, 0x7E,0xDF,0xB7,0xFB,0x7E,0xDF,0xB7,0xED,
7760xFB,0x7E,0xDF,0xB7,0xFF,0xC9,0xFF,0xFF, 0xBF,0xEF,0xFB,0xFE,0xFF,0xBF,0xEF,0xFB,
7770xFF,0xBF,0xEF,0xFB,0xFE,0xFF,0xBF,0xEE, 0xFB,0xFE,0xBB,0xFF,0xFE,0xFF,0xBF,0xEF,
7780xFB,0xFE,0xFF,0xBF,0xEF,0xFE,0xFF,0xBF, 0xEF,0xFB,0xFE,0xFF,0x3F,0xCF,0xFF,0xE7,
7790xFE,0xFF,0xF5,0xFD,0x77,0x5D,0xD7,0x35, 0xDD,0x77,0xD7,0xF5,0xCD,0x7B,0x5D,0xD7,
7800xF5,0xDD,0x77,0xFE,0x27,0xFF,0xFF,0x8B, 0xE2,0xF8,0xBE,0x2F,0x8B,0xE2,0xF9,0xAF,
7810x8B,0xE2,0xF8,0xBE,0x2F,0x8B,0xE2,0xF9, 0xFE,0x1F,0xFF,0x5F,0xD7,0xF5,0xFD,0x7F,
7820x5F,0xD7,0xF5,0xFF,0x5F,0xD7,0xF5,0xFD, 0x7F,0x5F,0xD7,0xF5,0xFF,0xFA,0x3F,0xFE,
7830xBF,0xAF,0xEB,0xFA,0xFE,0xBF,0xAF,0xEB, 0xEC,0xBF,0xAF,0xEB,0xFA,0xFE,0xBF,0xAF,
7840xEB,0xFF,0xFE,0x7F,0xFD,0x7F,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
7850xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE6, 0xFF,0xFA,0xDF,0xF7,0xFD,0xFF,0x7F,0xDF,
7860xF7,0xFC,0xFF,0xDF,0xF7,0xFD,0xFF,0x7F, 0xDF,0xF7,0xFD,0xFF,0xF5,0xFF,0xFF,0xFF,
7870xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFB,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
7880xFF,0x02,0xFF,0xFE,0xBF,0xAB,0xEB,0xFA, 0xBE,0xBF,0x23,0xEB,0xDE,0x1F,0xAF,0xEA,
7890xFA,0xFE,0xAF,0xAF,0xEB,0xFD,0x97,0xFF, 0xF3,0xFC,0x7B,0x1F,0xCF,0xF1,0xFC,0x7F,
7900x1F,0xF1,0xFC,0x77,0x1F,0xCD,0xF1,0xFC, 0xFF,0x1F,0xFE,0x87,0xFF,0xAF,0xEF,0xFA,
7910xFE,0xFF,0xAF,0xEF,0xFA,0xFD,0xBF,0x2B, 0xFB,0x7E,0xBF,0xBF,0xEB,0xFB,0xFB,0xFB,
7920xDF,0xFF,0xFB,0xF7,0xFF,0xFF,0x7F,0xF7, 0xF7,0xFF,0xFD,0xDF,0xFE,0xFC,0xDF,0xFF,
7930xDF,0xFF,0xFD,0xFF,0xDA,0xBF,0xFF,0xBB, 0xEF,0xFB,0xF9,0xFF,0xBE,0xEF,0xFB,0xFB,
7940xBF,0xEF,0xFB,0xFE,0xFF,0xBF,0xEF,0xFB, 0xFF,0xF7,0x7F,0xFD,0xD7,0xFF,0xFF,0x7F,
7950xFF,0xFF,0xFF,0xFE,0xF7,0xFF,0xFE,0xFF, 0xF7,0xFF,0xFF,0x7F,0xFF,0xFF,0xEC,0xFF,
7960xFF,0xFE,0xDF,0xBF,0xFF,0xFB,0xFE,0xFF, 0xBB,0x68,0xAE,0x1F,0xAE,0xFB,0xFB,0xFF,
7970xFF,0xBF,0xFF,0xD5,0xFF,0x7F,0xFF,0xFF, 0xF7,0xFE,0xFE,0xFF,0xBF,0xEF,0x9F,0xFD,
7980x7F,0xFF,0xCB,0xFF,0xFF,0xDF,0xFF,0xFF, 0xBB,0xF7,0xBF,0xFF,0xFF,0xFF,0xFF,0xDF,
7990xFF,0xBF,0xFB,0xFF,0xFF,0xFF,0xDE,0x3F, 0xFF,0xFF,0xFF,0xFF,0xFF,0xA7,0xFF,0xFF,
8000xFF,0xFF,0xEF,0xFF,0x7F,0xFB,0xFD,0xFB, 0x7F,0xFF,0xFF,0xFF,0xFF,0xCF,0xF3,0x7C,
8010xFF,0x7F,0x8D,0x7F,0xFF,0xFF,0xFF,0xFF, 0xFB,0xFF,0xF7,0xFB,0xFE,0xFD,0xFF,0xFF,
8020xFF,0xFF,0xF7,0xFD,0xFF,0x7F,0xFD,0x1F, 0xFD,0xFF,0xFF,0xFF,0xFF,0xBF,0xDF,0xFF,
8030xFF,0xFE,0x5C,0xFF,0x6D,0xFF,0x7F,0xAB, 0xE7,0xF1,0xFF,0xFD,0x9F,0xFF,0xFF,0xAD,
8040xEB,0x7A,0x3F,0x1F,0xFF,0xFF,0xFE,0xBF, 0xAF,0xF3,0xDE,0xF5,0xFF,0x8F,0xFB,0xDF,
8050xE6,0x7F,0xFF,0xDF,0xF3,0xFD,0xFF,0x7E, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFD,0xF7,0xF3,
8060x7F,0xDF,0xF7,0xEF,0xFF,0xF6,0x3F,0x9F, 0xDF,0xFF,0xFF,0xEE,0xFF,0xFF,0xEF,0xFB,
8070xFF,0xFF,0xF9,0xFB,0xFE,0x4F,0xBF,0xEF, 0xBB,0xFF,0x69,0xAF,0xAF,0xFC,0xFF,0x3F,
8080xDD,0xFF,0xFC,0xBF,0x8F,0xFF,0xFD,0xF3, 0xBF,0xED,0x9E,0xFC,0xBF,0x6F,0xF5,0xD3,
8090xDF,0xFF,0xDB,0xD6,0xF5,0xEF,0xFD,0xFE, 0xFF,0xB9,0xFF,0x1F,0xD2,0xA9,0xAF,0xFF,
8100xDB,0xF7,0xBF,0xEF,0x46,0xFF,0xFF,0xAD, 0xEB,0x7A,0xDF,0xEF,0xF7,0xFF,0x7F,0xF7,
8110x9F,0xED,0xFF,0x7F,0xFF,0xAD,0xEB,0x7F, 0xF5,0x6F,0xFF,0xFD,0xFB,0xD6,0xF4,0xF7,
8120xFB,0xF9,0x7E,0x7F,0xFF,0x5F,0xC2,0xFE, 0xBF,0xFD,0xFB,0x33,0xDF,0xF9,0x5B,0xFF,
8130xFF,0xDD,0x67,0x7D,0xCF,0xEF,0xDB,0xEC, 0xFF,0x77,0xDD,0xF7,0xFD,0xFF,0xFF,0xDE,
8140xA7,0xBF,0xD4,0x9F,0xFF,0xFF,0xBF,0xEF, 0xFE,0xFF,0xDF,0xEF,0xBB,0xFF,0xFF,0xEF,
8150xEB,0xFA,0xFF,0xEF,0xBD,0xFB,0xFF,0xE2, 0x7F,0xFF,0xDF,0xDF,0xF7,0xFD,0xBF,0xBB,
8160x73,0xF7,0xFD,0x7F,0xDF,0xDE,0xF7,0xBF, 0xEA,0xDB,0xF6,0xFF,0xD6,0xFF,0xFF,0x66,
8170xFF,0xBE,0xFF,0xBF,0x6B,0xD9,0xF6,0xDF, 0xFF,0xFB,0x7E,0x7F,0xB7,0x7E,0xFF,0xFE,
8180xFF,0xCD,0xFF,0xFE,0x7F,0xFF,0xFC,0xFD, 0x3F,0xFB,0xFB,0xF7,0xFF,0xFF,0xFB,0xF6,
8190x7D,0xFE,0x7F,0xFF,0xFC,0xFF,0xB9,0xFF, 0xF9,0xFA,0xFE,0xBF,0xAF,0x5B,0xD6,0xED,
8200xAD,0x7B,0xF6,0xF9,0xBF,0xEF,0xF8,0xFA, 0xFE,0xBF,0xFE,0xE6,0xFF,0xFF,0xF7,0xFD,
8210xFF,0x7F,0xBF,0xEF,0xF3,0xFF,0xFF,0x6F, 0xF7,0xFE,0xFF,0xFF,0xF7,0xFD,0xFE,0xF7,
8220xEF,0xFF,0xFB,0xEF,0xFB,0x7E,0xDE,0xFE, 0xFF,0xBF,0xFF,0xFE,0xFF,0xFF,0xFB,0xFF,
8230xFF,0xEF,0xFB,0x6F,0xFC,0x1F,0xFE,0xE7, 0xFF,0xFF,0xFF,0xEF,0xFF,0xD3,0xB4,0xBB,
8240xFF,0xFF,0xFD,0xBF,0x6F,0xE3,0xFE,0xFF, 0xBF,0xFC,0xBF,0xF7,0xCF,0xF7,0xFD,0xFF,
8250x2F,0xDF,0xAB,0xEA,0xFF,0xDF,0xE7,0xEA, 0x9A,0xAF,0xEF,0xFB,0xFE,0xFF,0xF5,0x3F,
8260xFD,0x7E,0xFF,0xD7,0xF5,0xFB,0xFF,0xFD, 0xF7,0xFF,0x7F,0xFE,0xF7,0xFD,0xFF,0xD7,
8270xFF,0xD7,0x7F,0xEE,0x7F,0xFA,0x79,0xFE, 0x2F,0x8B,0xE6,0xF9,0xFE,0x3F,0x9E,0xF9,
8280xBE,0x2F,0x0B,0xE7,0xF9,0xFE,0x2F,0x9F, 0xFD,0xFF,0xFE,0x7D,0x7F,0x5F,0xD7,0xFF,
8290xFF,0x7F,0xFF,0xFD,0xFF,0x7F,0x5F,0x97, 0xFF,0xFD,0x7F,0x5F,0xFF,0xE3,0xFF,0xFF,
8300xFA,0xFE,0xBF,0xAF,0xFB,0xFB,0xFF,0xFF, 0xCF,0xEB,0xFE,0xBF,0xAF,0xFF,0xFA,0xFE,
8310xBF,0xFF,0x87,0xFF,0xFF,0xF5,0xFF,0xFF, 0xFF,0xFF,0xFD,0xFF,0x7F,0xFF,0xFF,0xFF,
8320xFB,0xFF,0xFF,0xF5,0xFF,0xFF,0xFE,0x0F, 0xFF,0xFD,0xEB,0xFF,0xFF,0xF7,0xFF,0xEF,
8330x7B,0xDF,0xFE,0xFF,0xFF,0xDF,0xF7,0xFD, 0xEB,0x7F,0xDF,0xFF,0x5F,0xFF,0xFF,0xFF,
8340xFF,0xFD,0xBF,0xFF,0x7E,0xFA,0xBF,0xC7, 0xDB,0xF7,0xBD,0x3F,0xFB,0xFF,0xF6,0xFF,
8350xFA,0xAF,0xFF,0xEB,0xFA,0xFE,0x3F,0x2F, 0xEA,0xFA,0x3E,0xAD,0xC9,0xBA,0xF6,0xAD,
8360xAF,0xEB,0xFA,0xF6,0xBF,0xFE,0x7F,0xFF, 0xFF,0xFD,0xFF,0xF1,0x7F,0x3F,0xCF,0xF1,
8370xEF,0xFF,0x7F,0xFF,0xBC,0xDF,0xDF,0xF7, 0xDD,0xFF,0xE0,0x7F,0xFF,0xFF,0xFE,0xFF,
8380xFA,0xEC,0xBB,0x7F,0x5F,0xFF,0xFB,0xEC, 0xFF,0xEF,0xB7,0xFF,0xF7,0xFF,0xFF,0xB5,
8390xFF,0xFF,0x7F,0xFF,0xFF,0xFF,0xEE,0xDF, 0x5F,0xDF,0xDE,0xFF,0xAE,0xE7,0x77,0xFF,
8400xFF,0xDF,0xF7,0xFF,0xE3,0xFF,0xFA,0xBB, 0xFE,0xFF,0xAF,0xFD,0xFB,0xFE,0xBF,0xAB,
8410xF9,0xFE,0xFF,0xBF,0x7F,0xBF,0xFE,0xBD, 0xFE,0xD7,0xFF,0x9F,0xFD,0xFF,0xBE,0xEF,
8420xFF,0xEE,0xFD,0xBB,0x5B,0xEF,0xFF,0x7F, 0xEF,0xFF,0xEF,0xFF,0x7F,0xFF,0x4F,0xFF,
8430xEF,0xFB,0xBC,0xFC,0xFF,0xFF,0xFF,0xFE, 0xFE,0xFD,0xFA,0xFE,0xFB,0xFF,0xFD,0xF3,
8440xFB,0xFF,0xF8,0x5F,0xFF,0xFF,0xD7,0xF5, 0xFD,0xDF,0xEF,0xFF,0xF3,0xDC,0x5F,0xCE,
8450xF5,0xBD,0xFF,0xFF,0xD7,0xFF,0xFF,0xF9, 0x3F,0xFF,0xDF,0xF7,0xFF,0xFE,0xFF,0xFD,
8460xFF,0xFB,0xFF,0xF7,0xB9,0x7D,0xFE,0xDF, 0xFF,0xFF,0xFF,0xFF,0xF9,0x7F,0xFF,0xFE,
8470xFF,0xFF,0x7F,0xFF,0xFE,0xFF,0xFF,0xF7, 0xF6,0xFF,0xBF,0xF1,0xF8,0xFF,0xFF,0xFF,
8480xFF,0xE0,0xFF,0xFF,0xFF,0xFF,0xF9,0xFF, 0xFF,0xFF,0xFF,0xFF,0xEF,0xEF,0xFF,0xFF,
8490x9B,0xFB,0x7F,0xFF,0xFF,0xFF,0xC1,0xFF, 0xDF,0xFF,0x3F,0x5F,0xD7,0xBF,0xEF,0xBB,
8500xDE,0xEE,0xFF,0x7F,0xDF,0xFF,0xFE,0xF5, 0x7F,0xDF,0xFF,0x99,0xFF,0xFF,0xFA,0xFF,
8510xBF,0xFD,0xEB,0x7A,0xFF,0xB7,0xFE,0xFE, 0xFF,0xFF,0xEF,0xFF,0xFF,0xFD,0xBF,0xFF,
8520x97,0xFF,0xFD,0xF7,0xFF,0x7F,0xF7,0xFF, 0xFF,0xFD,0x5F,0xFE,0xF3,0xF9,0xDF,0xDF,
8530xFF,0xFF,0xFC,0xFF,0xFF,0x83,0xFF,0xFF, 0xFE,0xFF,0x9E,0xEC,0xFB,0xEE,0xFF,0x9F,
8540xBF,0xEF,0xFF,0xFE,0xED,0x7B,0xFF,0xFF, 0xFF,0xF1,0x5A,0xFF,0xFF,0xFD,0xFF,0x7C,
8550x69,0x3B,0xDF,0xFF,0x7F,0x1F,0xDF,0xFF, 0xFD,0xBA,0xFF,0xFF,0xFB,0xFF,0x5B,0xBD,
8560xFF,0xFF,0xFF,0xFF,0xD7,0xB6,0xED,0xE9, 0xFF,0xD6,0xBD,0x6F,0x5F,0xFB,0xFF,0xEF,
8570xFF,0x5F,0xFE,0xF6,0x6F,0xFF,0xFF,0xFF, 0xFF,0xF7,0xEB,0x7A,0xDF,0xFF,0x9F,0x7F,
8580x7F,0xFF,0xB7,0xFF,0xFF,0xFE,0xDF,0xFF, 0x6C,0xFF,0xFB,0xFF,0xBB,0x6F,0xEB,0xFE,
8590xCC,0xF7,0xA5,0xFA,0x5C,0xF5,0x75,0xBB, 0xB7,0xDF,0xFE,0x6F,0x5F,0xC5,0xBF,0xFD,
8600x7B,0xFE,0xFF,0x95,0xE7,0x29,0xCF,0x4F, 0xF5,0x91,0xEE,0x6B,0xDF,0xEF,0xFD,0x54,
8610xF5,0xBD,0xB1,0xFF,0xEF,0xEE,0xFB,0xBE, 0xBF,0xAF,0xFE,0xDE,0xBD,0x6F,0xDA,0xF2,
8620xFF,0xAF,0xBE,0xFF,0xFF,0xFD,0x7E,0xA7, 0xFF,0xF7,0xFF,0xBF,0xEF,0x7B,0xF6,0xFD,
8630xBD,0x4A,0xF2,0x85,0x85,0xBF,0x5B,0xFE, 0xB5,0xFD,0xFA,0xFF,0x4F,0xFF,0xFE,0xDF,
8640xFF,0xED,0xFF,0xBF,0xFF,0xBF,0x7F,0xFE, 0xFF,0xB7,0x6D,0xFF,0xF7,0xBF,0xBF,0xEF,
8650xFD,0x1F,0xFF,0xFE,0x7D,0xFF,0x67,0xFF, 0xFF,0xFF,0x3F,0x7F,0xFE,0xBF,0xFF,0xE7,
8660xDF,0xE7,0xFF,0xEF,0x6B,0xFC,0x1F,0xFF, 0xBF,0xEF,0xFB,0xFE,0xDE,0xBF,0xAF,0xFA,
8670xFF,0xB6,0xEF,0xF9,0xFE,0xFF,0x8F,0xEF, 0xDB,0xEF,0xAB,0x6F,0xFB,0xFE,0xFF,0xFF,
8680xEF,0xFD,0xFF,0x7F,0xFF,0xFF,0xDE,0xFF, 0xFF,0xEF,0xFF,0xFF,0xFF,0x3F,0xFF,0x6C,
8690xFF,0xBF,0xFB,0xFF,0xFE,0xFF,0xFB,0xFE, 0xDF,0xFF,0xFF,0xEF,0xFF,0xFF,0xBF,0xFF,
8700xFF,0xFE,0xFB,0xFF,0xD5,0x7F,0xFF,0xFF, 0xEF,0xFB,0xFF,0xFF,0xBF,0xEF,0x43,0xB5,
8710xFD,0x6F,0xCF,0xD6,0xBE,0x3F,0x7F,0xDB, 0xFE,0xC3,0xFF,0xFD,0xFF,0xAF,0xEB,0xFB,
8720xFC,0xFF,0x3E,0xEF,0xE8,0xFA,0xBD,0xCD, 0xAA,0xFE,0xFE,0x7D,0xCF,0xFF,0xB7,0xFF,
8730xF7,0xFF,0xFF,0xFF,0xFD,0xFF,0x75,0xCD, 0x52,0xD7,0xFD,0xFB,0xF7,0xDD,0xFB,0xEF,
8740xEB,0xFF,0xFF,0x4F,0xFF,0xBF,0x9F,0xE7, 0xF9,0xFC,0x7F,0x8B,0xC3,0xF9,0xAF,0x8F,
8750xE7,0xE9,0xBE,0x7F,0x9F,0xE6,0xF9,0xFC, 0x5F,0xFF,0xFF,0xF7,0xFD,0xFF,0x7A,0x5F,
8760xD7,0xED,0xFF,0xFF,0xD7,0xFF,0xDD,0x7F, 0xE7,0xFF,0xFC,0xFF,0xFC,0x3F,0xFF,0xFF,
8770xFF,0xFB,0xFF,0xFE,0xBF,0xAF,0xFF,0xFD, 0xFF,0xEF,0xFF,0xEB,0xFF,0xFF,0xFF,0xFF,
8780xFF,0xF7,0x7F,0xFF,0x7F,0xDF,0xFF,0xFD, 0xFD,0x7F,0xFE,0xF7,0xFD,0x7F,0xDF,0xFF,
8790xFD,0xFF,0xFF,0xDF,0xFB,0xFF,0xEE,0xFF, 0xFB,0xFF,0xF7,0xFD,0xFF,0x7A,0xDF,0xF5,
8800xFD,0xFA,0xDF,0xF7,0xFC,0xFF,0x7F,0xDF, 0xBF,0xED,0xFF,0xC9,0xFF,0xDF,0xFF,0xBF,
8810x2F,0xFB,0xFF,0xBC,0xAD,0xFF,0xF7,0xFF, 0xFF,0xEF,0xD3,0xFF,0x7D,0xBF,0x6F,0xFF,
8820xFA,0xFF,0xFE,0xBF,0xAE,0xEA,0xFA,0xBE, 0xAD,0xA5,0xEB,0xCE,0xBF,0xA7,0xEB,0x5A,
8830xDE,0xBD,0xAF,0x6B,0xFD,0x57,0xFF,0xFF, 0xF4,0x7F,0x1F,0x7F,0xFD,0xFF,0x7F,0x36,
8840xF0,0xDF,0x79,0xFF,0xFF,0xFF,0xF7,0xFD, 0xBF,0xFF,0x87,0xFF,0xFB,0xF3,0xFC,0xFF,
8850xFF,0xFF,0xFF,0x7E,0xFF,0xBF,0xDF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFD,0xBF,0xF8,0x9F,
8860xFF,0xFF,0xFF,0xFF,0xBF,0xFF,0xFF,0xFD, 0xF7,0xFC,0xBD,0xFF,0xFE,0xFF,0xFF,0xFF,
8870xFF,0xFF,0xFB,0xF9,0xBF,0xFF,0xFF,0xEB, 0xE2,0xFE,0xFF,0xBF,0xEF,0xA9,0xBA,0x2F,
8880xEB,0xF9,0xFE,0x77,0xDF,0xF7,0xFF,0xFF, 0xF9,0x7F,0xFF,0xFF,0x7F,0xEF,0xD7,0xFF,
8890xFD,0xFF,0xFB,0xF5,0xFF,0xBF,0x6F,0xDF, 0xFF,0xFF,0xFD,0xFF,0xFF,0xF0,0xFF,0xFF,
8900xFF,0x3F,0xCF,0xFF,0xBA,0xEE,0x9B,0xBF, 0xEE,0xD7,0xFE,0xCD,0xEF,0xFF,0xDF,0xBF,
8910xFF,0xFF,0xC5,0xFF,0xFF,0xFD,0x7F,0x4F, 0xFD,0xF6,0xD9,0xFF,0x4F,0xD6,0xFD,0xBF,
8920x6E,0xFF,0xFF,0xF4,0x7F,0xFF,0x7F,0x8B, 0xFF,0xFF,0xFF,0xFF,0xF7,0xFF,0xF9,0xFE,
8930x37,0xFF,0xD9,0xFB,0xF5,0xAF,0xFD,0xFF, 0xFF,0xFB,0xFF,0xFF,0x07,0xFF,0xFF,0xFF,
8940xFB,0xF7,0xFF,0xFD,0xFF,0x7C,0xFA,0x7E, 0x4F,0xFC,0xDF,0x1D,0xC7,0xFF,0xFF,0xFF,
8950xFF,0xAE,0xFF,0xFF,0xFF,0xFF,0xFD,0xFB, 0xFF,0xFF,0xFE,0xFE,0xFC,0xFF,0x7F,0x7F,
8960xBF,0xEF,0xFE,0xFF,0xFF,0xFF,0x5F,0xFD, 0xFF,0xFF,0xFF,0xFD,0x6F,0x5A,0xD7,0x7B,
8970xBE,0x5F,0xFE,0x39,0xFF,0xF7,0xFF,0xF7, 0xFD,0xFE,0xAA,0x1F,0xFF,0xFF,0xFF,0xFF,
8980xFE,0xFE,0xAB,0xAF,0xFD,0xFE,0xBF,0xFF, 0xF7,0xFF,0x7F,0xFE,0x8F,0xE3,0xFB,0xEE,
8990x7F,0xFF,0xFF,0xFF,0xFF,0xEB,0xFB,0xFF, 0xFD,0xBF,0xEF,0xDF,0xFF,0xFF,0xFF,0xFF,
9000xFF,0xFF,0xFF,0xFB,0xE4,0x3F,0xFF,0xDF, 0xFF,0xFF,0xFF,0xFF,0xF3,0xEF,0xBB,0xFB,
9010xBF,0xEF,0xBB,0xFF,0xD7,0xBF,0xFF,0xFF, 0xFF,0x29,0xAF,0xF7,0xFF,0xFF,0xFB,0xFF,
9020xFB,0xE6,0xFF,0x0F,0xFB,0x3F,0xDF,0x0F, 0xFF,0xAF,0xFF,0xFF,0xFF,0xF5,0xC3,0xDF,
9030x5F,0xFF,0xFF,0xFF,0xFE,0x6B,0xCA,0xBE, 0xBC,0xFF,0x9F,0xF2,0xBF,0xFF,0xFE,0xFA,
9040xFF,0xFF,0xEF,0x16,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFC,0xDF,0x97,0xFD,0x79,0xFF,0x37,
9050xE7,0x7F,0xFF,0xFF,0xB5,0xFF,0xFF,0xF6, 0x2F,0xFF,0xFD,0xFB,0xFE,0xFF,0xFF,0xFD,
9060x5F,0x57,0x5F,0xFF,0xDB,0x52,0xDF,0xFF, 0xFD,0xBF,0xFF,0xFF,0xFC,0xDB,0xFF,0x7B,
9070xB5,0xFD,0x7F,0xFF,0x71,0x9C,0x6E,0xFF, 0xF6,0x35,0xA5,0x9B,0xFF,0xFF,0xFD,0xFF,
9080xFF,0xDB,0x9E,0x7F,0xFE,0xEF,0xFB,0xFF, 0xFF,0xBD,0xEF,0xFF,0xDE,0xB7,0xF9,0x4B,
9090xFF,0xF5,0xEF,0xFF,0xFF,0xFF,0xE8,0x7E, 0xFF,0xEA,0xDF,0xF7,0xFF,0xFD,0x69,0x5B,
9100xFC,0x9F,0xEF,0x78,0xD6,0xFF,0xEB,0xEF, 0xFF,0xFF,0xFF,0xE8,0xFF,0xFF,0xED,0xFF,
9110xFF,0xFF,0xFF,0xE3,0xF9,0xF6,0xBF,0xFF, 0xFF,0xFE,0xDF,0xFF,0x7F,0xFF,0xFF,0xFF,
9120xD1,0xFF,0xFF,0xE7,0xFF,0xFF,0xFF,0xFF, 0xE7,0xF9,0xFF,0xBF,0x7F,0xD9,0xFF,0xFD,
9130xFE,0x7F,0xFF,0xFE,0xFF,0xF9,0xFF,0xFB, 0xD6,0xDF,0xBF,0xEF,0x5B,0xD6,0xFF,0xBF,
9140xFB,0xF6,0xFF,0xBF,0xEF,0xF8,0xF6,0xDD, 0xBE,0xFE,0x16,0xFF,0xBF,0xEF,0xFF,0xFE,
9150xFF,0xBF,0xEF,0xFF,0xFF,0xFF,0x6F,0xFB, 0xFF,0xFF,0xFF,0x6F,0xF3,0xFF,0xF7,0xEF,
9160xFB,0xFF,0xBF,0xFF,0xEF,0xFE,0xFF,0xBF, 0xFF,0xFF,0xFF,0xBE,0xBF,0xFF,0xEF,0xFF,
9170x7F,0xEF,0xFF,0xFD,0x17,0xFB,0x7B,0xFF, 0xFF,0xFD,0x7F,0xDB,0xF6,0xF4,0x7F,0xFA,
9180xFE,0xF5,0xBF,0xEB,0xE3,0xF7,0xFF,0xFF, 0xE9,0xBF,0xFF,0xAF,0xF7,0xFD,0xF3,0x7E,
9190x8F,0xA3,0xEA,0xFF,0xCB,0xF3,0xEE,0xFF, 0xBF,0xEF,0xF7,0xF9,0xFF,0xFE,0x7F,0xFF,
9200xFF,0xFF,0xFF,0xF5,0xFB,0xF6,0xFF,0xF5, 0x2F,0xFE,0xFB,0xD7,0xBF,0xFF,0xBE,0xDF,
9210x9F,0xFF,0xF0,0xFF,0xFF,0xF9,0xFE,0x7F, 0x8F,0xA3,0xF8,0xFE,0x6F,0x9F,0xF9,0xF6,
9220x2F,0x9F,0xE7,0xF9,0xFE,0x2F,0x9F,0xE1, 0xFF,0xFF,0xFF,0x7F,0xDF,0xF7,0xF5,0xFD,
9230x7F,0x7F,0xF5,0xFF,0x9F,0x5F,0xFB,0xFE, 0xFF,0x7F,0xFF,0xFF,0xCB,0xFF,0xFF,0xFB,
9240xFE,0xFF,0xBF,0xAF,0xFB,0xFE,0xFF,0xDF, 0xFE,0xFE,0xBF,0xF7,0xFF,0xFF,0xFF,0xFF,
9250xFF,0xC7,0xFF,0xFF,0xFD,0xFF,0x7F,0xDD, 0xF7,0xFD,0xFF,0xFF,0xD7,0xFF,0xFD,0x7F,
9260xFF,0xFB,0xFD,0xFF,0xFF,0xFE,0xEF,0x7F, 0xFD,0xEF,0xFB,0xFE,0xFB,0xFD,0xFF,0x7F,
9270xDF,0xFD,0xFF,0x7A,0xDF,0xF7,0xFD,0xFF, 0xFF,0xFF,0xFF,0x1F,0xFF,0xFF,0xD3,0xF7,
9280xFF,0xFF,0x6F,0xDB,0xFF,0xFF,0xEF,0xCB, 0xF4,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
9290x29,0xFF,0xE8,0xDA,0x76,0x9F,0xAF,0x6A, 0xDA,0xFE,0x35,0xEB,0xDA,0xD6,0xBF,0xAB,
9300xEB,0x7A,0xDE,0xBF,0xD7,0x7F,0xFF,0xFE, 0xFF,0xBF,0xEF,0xFD,0xDF,0x77,0xBF,0xFD,
9310x37,0xEF,0xFF,0xEF,0xFF,0x3F,0xFF,0xFF, 0xFF,0xFE,0x7F,0xFF,0xFF,0xFF,0xF7,0x7E,
9320xDF,0xFF,0xFF,0xFF,0xFA,0xB7,0x7F,0xFF, 0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0x89,0xFF,
9330xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0x9F,0xFB,0xFF,0xFF,0xFF,0xE7,0xFF,
9340xFF,0xFF,0xFF,0xAA,0xFF,0xAB,0xFB,0xFA, 0xEF,0xBF,0xFF,0xDF,0xFA,0x7B,0xB9,0xFE,
9350xFE,0xFF,0xFD,0xFF,0xF7,0xFE,0x3F,0xFF, 0xB7,0xFF,0xF7,0xEE,0xFF,0x7F,0xEF,0xFF,
9360xFF,0x7F,0xFF,0x1F,0xFB,0xFF,0xBF,0xFB, 0xFE,0xFF,0xBD,0xFF,0xFF,0x2F,0xFF,0xBF,
9370xFF,0x7F,0xDF,0xFA,0xFF,0xFF,0xFC,0xEE, 0xF5,0xF3,0xBE,0xFB,0x0F,0xEF,0xF3,0xBE,
9380xEF,0xFC,0x5F,0xFF,0x5A,0xFF,0xF7,0xDF, 0xFF,0xFF,0xFE,0xD5,0xFC,0x5F,0xFB,0xF2,
9390xFF,0xFF,0x2F,0xBB,0xF3,0xFF,0xFF,0xBF, 0xFF,0xEF,0xFF,0xEF,0xFF,0xFF,0xFF,0xFF,
9400xBF,0xFF,0xFF,0xFD,0x7B,0xFF,0xDF,0xB9, 0xFF,0xFB,0xFF,0xD8,0x7F,0xFF,0xFF,0xFF,
9410xFB,0xFF,0xFC,0x7F,0x1F,0xBF,0xE0,0xDF, 0xF7,0xEF,0xFF,0xFD,0x7F,0xFE,0xDF,0xFF,
9420xE0,0xFF,0xFF,0xFD,0xEF,0xFB,0xFF,0xFE, 0xF7,0xDF,0xFF,0xEB,0x5F,0xFF,0xF7,0xFF,
9430xFF,0xFF,0xFF,0xBF,0xFF,0xFD,0xFF,0xFD, 0xFF,0xFF,0xFF,0xF7,0xFD,0xFF,0x3B,0xDC,
9440xFD,0x6D,0x7B,0x5F,0x57,0xF5,0xFD,0x7F, 0x5F,0xFF,0xB1,0xFF,0xEB,0xFF,0xFF,0xFF,
9450xFB,0xFB,0xFE,0xFF,0xBF,0xFB,0xBE,0xFF, 0xBF,0xEF,0xFB,0xFE,0xFF,0xAF,0xFE,0xF7,
9460xDF,0xDF,0xFF,0xFF,0xFF,0x7F,0xCF,0xF3, 0xF8,0xFF,0xD7,0xFB,0xFF,0x5F,0xBF,0xF7,
9470xFB,0xFF,0x7F,0xFE,0x23,0xFF,0xFF,0xFE, 0x7F,0xF3,0xFF,0xFB,0xFE,0xFF,0xFF,0xF3,
9480xFF,0xFF,0xF5,0xF9,0xFF,0x3F,0xFF,0xFF, 0xF0,0x9A,0xFF,0xBE,0x7F,0xFF,0xFC,0xF9,
9490xFF,0xFD,0xAF,0xEB,0xFE,0xBF,0xFF,0xCF, 0xF3,0xFE,0x7F,0xFF,0xFF,0x5B,0xBD,0xFF,
9500xBC,0xEB,0xFF,0xD7,0xD4,0xAF,0xAF,0xFD, 0xFF,0xCF,0xF7,0xFD,0xFF,0x7F,0xDF,0xF7,
9510xFD,0xFE,0xFF,0x6F,0xFF,0xFB,0xFF,0xFF, 0xFF,0xFD,0x7F,0x5E,0xFD,0xBF,0xDB,0xF6,
9520xFD,0xBF,0x6F,0xFB,0xEE,0xFD,0xFF,0x7A, 0xFF,0xFA,0xFB,0xFF,0x3F,0xFB,0xB7,0x5F,
9530xD6,0xF7,0x1F,0x71,0xDC,0x77,0x1D,0xC7, 0x31,0xDC,0x77,0xDF,0xF9,0xBF,0xF5,0x5B,
9540xF4,0xD7,0x9D,0xAE,0xFF,0xBF,0xFD,0xBF, 0xDB,0xF6,0xFD,0xBF,0x6F,0xDB,0xF6,0xFE,
9550x3D,0x81,0xFF,0xEB,0xFE,0xFE,0xFE,0xFF, 0xEB,0x7A,0xDF,0x7D,0x77,0x7D,0xF5,0x79,
9560xDF,0x57,0xDD,0xF5,0x7D,0x7E,0xE6,0xFF, 0xD6,0x3F,0xBF,0x7F,0xFF,0xD4,0xF5,0x3F,
9570xBF,0xFB,0xBE,0xEF,0xB3,0xEE,0xFB,0x9E, 0xEF,0xBB,0xFE,0x8B,0xFF,0xFE,0xDF,0xB7,
9580xED,0xFF,0xF7,0xFD,0xFE,0xFF,0xEF,0xBB, 0xEE,0xFF,0xBE,0xEF,0xBB,0xEE,0xEB,0xFC,
9590x1F,0xFF,0xFF,0xFD,0xFF,0xE7,0xFF,0xF7, 0xFD,0xFF,0xEF,0xFE,0xFF,0xBF,0xEF,0xFB,
9600xFE,0xFF,0xBF,0xEB,0xFA,0x1F,0xFF,0xB7, 0xEF,0x5B,0xFE,0xFF,0xAF,0xEB,0xDD,0xE7,
9610xDE,0x77,0x9D,0xE7,0x79,0xDE,0x77,0x9D, 0xBF,0xE6,0x6F,0xFF,0xFE,0xFF,0xBF,0xEF,
9620xFB,0xFE,0xFD,0xBF,0x6F,0xF6,0xFD,0xBF, 0x6F,0xDB,0xF6,0xFD,0xBF,0xFF,0x7E,0xFF,
9630xFF,0xFB,0xFE,0xFE,0xFF,0xEF,0xFB,0xFD, 0xEF,0x7E,0xF7,0xBD,0xEF,0x7B,0xDE,0xF7,
9640xBD,0xEF,0xFF,0xD5,0xFF,0xBF,0xFF,0xEF, 0xFE,0xFF,0xFC,0x3F,0x0F,0xE7,0xFE,0x7F,
9650x9F,0xE7,0xF9,0xFE,0x7F,0x9F,0xE7,0xFE, 0xF3,0xFF,0xFE,0xDF,0xAD,0xDF,0x67,0xEE,
9660xFB,0xBF,0xEF,0xFE,0xFF,0xBF,0xEF,0xFB, 0xFE,0xFF,0xBF,0xEF,0xFF,0x23,0xFF,0xFF,
9670xFF,0xFF,0x7F,0xFF,0xF3,0xBC,0xDB,0xFE, 0xFB,0xFF,0xFB,0xBE,0xF7,0xFB,0xFF,0x7F,
9680xDF,0xFF,0xCF,0xFB,0xFF,0x9F,0xE3,0xF9, 0xBE,0x3F,0x8F,0xE7,0x79,0xFF,0x9D,0xE7,
9690xF9,0xFE,0x7F,0x9F,0xE7,0xF9,0xFE,0x5F, 0xFF,0xCF,0xF7,0xFF,0xFF,0xFF,0xDF,0xF7,
9700xFE,0x7F,0xE7,0xF9,0xFE,0x7F,0xFF,0xFF, 0xFB,0xFE,0xFF,0xFF,0xBF,0xFF,0xBF,0xBF,
9710xFF,0xFE,0xFF,0xBF,0xEF,0xFF,0xFD,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xF7,0xFD,0xFF,
9720xFF,0x3F,0xFF,0xBF,0xFF,0xF7,0xFF,0xFF, 0x7F,0xDF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
9730xFF,0xFF,0xFF,0xFF,0xFF,0xE8,0xEF,0xFF, 0x5F,0xF7,0xBF,0xF9,0xFE,0xDF,0xB7,0xFD,
9740xFF,0xDF,0xF7,0xFD,0xFF,0x7F,0xDF,0xF7, 0xFD,0xFF,0xDD,0xFF,0xF2,0xFF,0xBF,0xFF,
9750xFF,0xBF,0xFF,0xFF,0x2F,0xF2,0xFF,0xBF, 0x2F,0x7B,0xD2,0xF7,0xBF,0x2F,0xFF,0xBB,
9760xFF,0xEE,0x8F,0xAF,0xEB,0xFA,0xFE,0x3F, 0xA7,0x69,0xCE,0x8F,0xA4,0xEA,0xFA,0xEE,
9770xB7,0xAE,0xEB,0xFD,0xC7,0xFF,0xF7,0xF7, 0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x3E,0xF3,
9780x74,0xFF,0x3F,0x4F,0xFF,0xE7,0xFF,0x3F, 0xFE,0xA7,0xFF,0xFF,0xDF,0xF7,0xB7,0xFF,
9790xF7,0xFF,0xBA,0xEF,0x37,0xEB,0xFB,0xFE, 0xBF,0xFB,0xFE,0xF3,0xFF,0xF9,0xDF,0xFF,
9800xBF,0xFF,0xFF,0xFF,0xBF,0xFF,0xFF,0xFF, 0xFD,0xDF,0xFF,0xFD,0xFF,0xFF,0xFB,0xFE,
9810xFD,0xFF,0xFB,0xBF,0xFE,0x3F,0xED,0xFF, 0xDF,0xBE,0x3D,0xA7,0xFB,0xFA,0x3F,0xE6,
9820xE1,0xFE,0xFE,0x3F,0xEF,0xE3,0xDF,0xF5, 0x7F,0xFE,0xFF,0x7E,0xFF,0xFF,0xFF,0xFF,
9830xEF,0x6F,0xF6,0xFF,0x7D,0xEF,0xD7,0xDE, 0xFF,0x7D,0xEF,0xFF,0xF2,0xFF,0xFF,0xFF,
9840xFF,0xFF,0xFF,0x7B,0xDE,0xFB,0xE6,0xEE, 0xEF,0x37,0x6E,0xF3,0x7E,0xEB,0x37,0xEF,
9850xFF,0xC1,0xFF,0xFE,0xFF,0xF7,0xEF,0xFF, 0xFF,0xFF,0xBF,0x3F,0xD2,0xDF,0xBF,0x2F,
9860x7B,0xE2,0xFF,0xFE,0x3B,0xBD,0xDB,0xFF, 0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xEF,0xFE,
9870xFF,0xFB,0xFF,0xFF,0xBF,0xFF,0xFB,0xDF, 0xFF,0xBF,0xFF,0xB7,0xFF,0xFF,0xBF,0xEF,
9880xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0F,0xFF, 0x7F,0xFF,0x1F,0xEF,0xF1,0xFD,0xFF,0xF6,
9890xAF,0xFF,0xFF,0xFF,0xFF,0xFF,0xEF,0xFF, 0xFF,0xFF,0xFE,0x9F,0xFF,0xFF,0xFF,0x77,
9900xEF,0xF7,0xFB,0xFF,0xFE,0x5F,0xFF,0xFF, 0xBF,0xCF,0xFB,0xF7,0xDD,0xF7,0xF5,0xFF,
9910x5F,0xD5,0xF5,0xFD,0x7F,0x5F,0xD7,0xF5, 0xFF,0xFB,0x0F,0xFF,0xFF,0xA9,0xEA,0x7A,
9920xFF,0xAF,0x8F,0xFE,0xDF,0xAF,0xEF,0xFB, 0xFE,0xFF,0xBF,0xEF,0xFB,0xDF,0xE5,0x5F,
9930xFF,0xFF,0xFF,0xFF,0xFF,0xBD,0x57,0xFF, 0xFF,0x6F,0x77,0xBF,0xF7,0xFB,0xFF,0x7F,
9940xBF,0xF7,0xFF,0xFC,0xBF,0xFF,0x9F,0xFF, 0xFF,0xEF,0xFF,0xFE,0xFF,0xFF,0xFF,0x1F,
9950xCF,0xFF,0xFC,0xFF,0xFF,0xFF,0xFF,0xFB, 0x65,0xAF,0xF3,0x7C,0xFF,0x3F,0xDF,0xFF,
9960xFD,0xE9,0xFE,0x7F,0xE7,0xFF,0xFE,0x7F, 0xFF,0xFF,0xFF,0xFF,0xFD,0xE3,0xDF,0xFB,
9970xDB,0xF6,0xFD,0xEF,0x5B,0xFB,0xFF,0xDF, 0xFC,0xFF,0x3F,0xDF,0xF3,0xFD,0xFF,0x7F,
9980xDF,0xEF,0x66,0xFF,0xDF,0xAD,0xEB,0x7A, 0xDE,0xF7,0xF7,0xE7,0xD9,0xFD,0x9F,0x67,
9990xD9,0xF6,0x7D,0x9F,0xE7,0xDF,0xF5,0x47, 0xFD,0x65,0x5B,0xD6,0xF4,0xFE,0xFF,0xEF,
10000xFF,0x6D,0xF6,0xDD,0xB7,0x6D,0xDB,0x76, 0xDC,0xB7,0x7D,0xFA,0x9B,0xF6,0x6D,0x9D,
10010x67,0x59,0xDF,0xF7,0xDD,0xFF,0xEB,0xFE, 0xBF,0xAF,0xEB,0xFA,0xFE,0xBF,0xAF,0xE3,
10020xD1,0x9F,0xFF,0xBD,0xBF,0xEF,0xFE,0xF7, 0xBF,0xBF,0xF7,0xD7,0x7F,0xDD,0xF7,0x9D,
10030xDF,0x7F,0xDF,0xF7,0xFF,0xE0,0x7F,0xFD, 0xC1,0xDF,0xF7,0xFD,0xC7,0x7F,0x7F,0xFB,
10040xFF,0xBB,0xEC,0xFB,0x3E,0xFF,0xBF,0xEC, 0xFB,0xFF,0xD8,0x7F,0xBF,0x6C,0xFF,0xBE,
10050xFF,0xBF,0xED,0xFF,0xEF,0xFE,0xFB,0xBF, 0xEF,0xFB,0xFE,0xFF,0xBF,0xEE,0xFF,0xC5,
10060xFF,0xAF,0x6F,0xFF,0xFC,0xFD,0x3F,0xE7, 0xFF,0xFE,0xFF,0xEF,0xFB,0xFE,0xFF,0xBF,
10070xEF,0xFB,0xFE,0xBF,0x89,0xFE,0xFA,0xBA, 0xFE,0xBF,0xAF,0xFB,0xF6,0xF5,0xD9,0x7D,
10080x97,0x65,0xD9,0x74,0x5D,0x97,0x65,0xD3, 0xFE,0xD6,0xFF,0xBF,0xF7,0xFD,0xFF,0x7F,
10090xBF,0xCF,0xFB,0xFE,0xFF,0xEF,0xFB,0xFE, 0xFF,0xBF,0xEF,0xFB,0xFF,0xF6,0x8F,0xFB,
10100xFF,0xEF,0xFB,0x7E,0xDB,0xFE,0xFF,0xBE, 0xEF,0xEE,0xFB,0xBE,0xEF,0xBB,0xEE,0xFB,
10110xBE,0xFF,0xFF,0xDF,0xFF,0x43,0xFF,0xFF, 0xFB,0xEF,0x5F,0xB7,0xFE,0x7F,0xE7,0xF9,
10120xFE,0x7F,0x9F,0xE7,0xF9,0xFE,0x7F,0xF9, 0xBF,0xFE,0xAF,0x77,0xFD,0xFF,0x2F,0xAF,
10130xA7,0xFE,0xFF,0xEF,0xFB,0xFE,0xFF,0xBF, 0xEF,0xFB,0xFE,0xFF,0xF1,0x7F,0xEF,0xDF,
10140xFF,0x97,0xF5,0xEF,0xFF,0xDF,0xFF,0xFF, 0xBF,0xFF,0xBF,0xFF,0xFF,0xFE,0xFF,0xFF,
10150xFF,0xE0,0xFF,0xFF,0xF9,0xFE,0x2F,0x8B, 0xE3,0xF8,0xBE,0x77,0x9F,0xF9,0xDA,0x77,
10160x9D,0xE7,0x79,0xDE,0x77,0x9F,0xDD,0xFF, 0xFD,0xFD,0x7F,0x5F,0xD7,0xFD,0xFF,0x7F,
10170xE7,0xFE,0x7F,0x97,0xE7,0xFB,0xFE,0xFF, 0xBF,0xEF,0xFF,0xAB,0xFF,0xEF,0xFA,0xFE,
10180xBF,0xAF,0xFF,0xFA,0xFF,0xFF,0xDF,0xFF, 0xFB,0xFF,0xF7,0xFD,0xFF,0x7F,0xDF,0xFF,
10190x67,0xFF,0xF7,0xF5,0xFF,0xFF,0xFF,0xDF, 0xFD,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
10200xFF,0xFF,0xFF,0xFF,0xFF,0xEF,0xFF,0xBD, 0xEB,0xFF,0xFF,0xF7,0xAD,0xEB,0xFF,0xDF,
10210xFD,0xFF,0x3F,0xDF,0xF7,0xFD,0xFF,0x7F, 0xDF,0xFF,0x5F,0xFF,0xF7,0xFF,0xFF,0xFD,
10220xBF,0xFF,0xCB,0xF4,0xFF,0x7F,0xD3,0xF7, 0xFD,0x3F,0x7F,0xD3,0xF7,0xFF,0xFC,0x3F,
10230xFF,0xEA,0xFA,0xBE,0xAF,0xAB,0xEB,0xBA, 0xF4,0x95,0x6B,0x52,0xD4,0xAD,0x2F,0x4A,
10240xD2,0xF6,0xBF,0xD2,0x7F,0xF7,0x3F,0xFF, 0xFF,0xF3,0x7F,0xFF,0xFF,0xF7,0xFF,0xBA,
10250xDF,0xFB,0xFD,0xFF,0xBF,0xFF,0xFB,0xFF, 0xF8,0x7F,0xEA,0xFF,0xFE,0xFE,0xDF,0xFF,
10260xF7,0xFF,0x7F,0xBB,0xFF,0xFF,0xBF,0xDF, 0xFB,0xFF,0xFF,0xBF,0xFF,0xB1,0x7F,0xFF,
10270xFB,0xEF,0xFF,0xFF,0xFF,0xFF,0xFF,0xBF, 0xCF,0xFE,0xFF,0xFF,0xEF,0xFF,0xF7,0xFF,
10280xFF,0xFF,0xF1,0xFF,0x69,0xBE,0xFA,0xBF, 0xAF,0xE2,0xFF,0xFE,0xFD,0xAF,0xF3,0xFE,
10290xFF,0xBF,0xEF,0xFB,0xFC,0xFF,0xFF,0x07, 0xFD,0x95,0xDB,0xDF,0x7F,0xDF,0xAF,0xFF,
10300xF7,0xAF,0x36,0xFE,0xBF,0x65,0xEB,0xF6, 0xFE,0x9F,0x6F,0xFE,0x07,0xFF,0xCF,0xFF,
10310xF8,0xFE,0xFF,0xCF,0xFF,0xF6,0xFA,0xE7, 0xFB,0xFE,0xFF,0xBB,0xED,0xF9,0xFF,0xFF,
10320xFF,0x5F,0xFF,0xFF,0xFF,0x75,0xFF,0xEF, 0x7E,0xFD,0xE0,0xE8,0x5E,0xD3,0xE5,0xF9,
10330x3E,0x5F,0xD7,0xF7,0xFF,0xFA,0x2F,0xFB, 0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0x7F,
10340x7F,0xD7,0xF5,0x7D,0x5F,0x57,0xD5,0xF5, 0xEF,0xFF,0xF3,0x7F,0xFC,0x7F,0xFF,0xC7,
10350xF1,0xFF,0xFF,0x1F,0xCF,0xB0,0xFF,0x3F, 0xCF,0xF3,0xFC,0xFF,0x3F,0xCE,0xFF,0xE4,
10360xFF,0xDF,0x7F,0xFE,0xF7,0xBB,0xFF,0xFF, 0xDF,0xEF,0xEE,0xFF,0xBF,0xEF,0xFB,0xFE,
10370xBF,0xBF,0xEF,0xFF,0xD1,0xFF,0xFF,0xFF, 0xFD,0xFB,0xFF,0xFD,0xFF,0xFB,0x9F,0xE9,
10380xFE,0x7F,0x9F,0xE7,0xF9,0xFE,0x7F,0xBF, 0xFF,0xB3,0xFF,0xFF,0xF7,0xFF,0xFF,0xAF,
10390xF7,0xFF,0xB6,0x3F,0xEB,0xFA,0xFE,0xBF, 0xAF,0xEB,0xFA,0xFE,0xBF,0xFE,0xA7,0xFF,
10400xFF,0xFF,0xFF,0xFF,0xF7,0xFF,0xFF,0xFF, 0xFE,0x9F,0xF7,0xF9,0xFF,0x7F,0x9F,0xE7,
10410xFF,0xFF,0xFE,0xAF,0x6F,0xFF,0xFF,0xFF, 0x9F,0xFF,0xDF,0xFF,0x7D,0x5F,0xDD,0xFF,
10420xFB,0xBF,0xE7,0xBB,0xFF,0xFB,0xDF,0x6D, 0x5F,0x7E,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
10430xEB,0xF7,0xFF,0xE7,0xEF,0xF7,0xFF,0xFF, 0x7F,0xFF,0xF7,0xFF,0xFC,0x8F,0xFF,0xEF,
10440xFD,0xFE,0xFF,0xBE,0xF4,0xF2,0x7D,0xD7, 0xCF,0xFF,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
10450xFF,0xCF,0x6B,0xFF,0xBF,0x3F,0xFB,0xF2, 0xFC,0x7F,0xEB,0xFF,0x9F,0xFA,0xFF,0xFF,
10460x3F,0xFF,0xF3,0xFF,0xFF,0xFD,0x70,0xF7, 0xFF,0xFF,0xBF,0xFF,0xFB,0xD7,0xFE,0xF5,
10470x77,0xFF,0x15,0xDD,0x77,0xFD,0xFF,0x7F, 0xDF,0xF7,0xFB,0xCD,0xBF,0xFF,0xFD,0xFF,
10480xFF,0xDF,0x37,0xCD,0xF9,0xEC,0xFE,0xEF, 0xBB,0xF4,0xFB,0x3F,0x4F,0xB3,0xFF,0xFD,
10490xCB,0xFF,0xE9,0x7E,0x54,0x9F,0xE5,0x4B, 0xB7,0xFF,0xDD,0x7D,0xC7,0x71,0xDD,0x77,
10500x5D,0xD7,0x75,0xCD,0x7F,0xD6,0xFF,0xD3, 0xF6,0xF9,0x3F,0x6D,0x95,0xAF,0x7F,0xFE,
10510xFF,0xEF,0xFB,0xFE,0xFF,0xBF,0xEF,0xFB, 0xFE,0xF6,0xC7,0xFF,0xAD,0x7B,0xCA,0xFF,
10520xBF,0xBF,0xEF,0xFD,0xE3,0xDF,0xB7,0xED, 0xFB,0x7E,0xDF,0x37,0xED,0xE3,0xFB,0xDF,
10530xFF,0x52,0x5C,0x15,0xFD,0xCF,0x7F,0xDF, 0xFE,0xEF,0xEF,0xFB,0xFE,0xFF,0xBF,0xEC,
10540x7B,0xFE,0xFF,0xFE,0x3E,0x7F,0xDA,0xF7, 0xFD,0xFF,0x7F,0xFF,0xFF,0xFB,0xEF,0xBB,
10550x6F,0xFB,0xFE,0xFF,0xBF,0xEF,0xFB,0xFF, 0xF7,0x7D,0xFF,0xD8,0xFF,0xFD,0xBF,0x7F,
10560xFB,0xFF,0xFF,0x9F,0xFB,0xFE,0x7F,0x9F, 0xE7,0xF9,0xFE,0x7F,0x9F,0xEA,0x7F,0xF6,
10570xBF,0xBD,0x6A,0x5A,0xF6,0xE5,0xBF,0x77, 0x5F,0x6D,0xDD,0x77,0x5D,0xD7,0x75,0xDD,
10580x77,0xFF,0xA5,0xBF,0xCF,0xFB,0xFF,0xFF, 0xBF,0xCF,0xFB,0xFD,0xFF,0xBF,0xF3,0xFE,
10590xFF,0xBF,0xEF,0xFB,0xFE,0xFF,0xFD,0xAB, 0xFF,0xBF,0xBF,0xFF,0xFB,0xFF,0x7F,0xEF,
10600xFF,0xBE,0xFB,0xEE,0xFB,0xBE,0xEF,0xBB, 0xEE,0xFB,0xBF,0xFF,0xB5,0xFF,0xD0,0xBC,
10610xFD,0x2F,0x4B,0xF7,0xFF,0xFF,0x9F,0xF9, 0xFE,0x7F,0x9F,0xE7,0xF9,0xFE,0x7F,0x9F,
10620xFA,0x8F,0xFD,0xAB,0xFA,0xDA,0xBF,0xAF, 0xB3,0xFD,0xFF,0xBF,0xFB,0xFE,0xFF,0xBF,
10630xEF,0xFB,0xFE,0xF7,0xBF,0xFF,0x9F,0xFF, 0x77,0xF7,0xBD,0xFD,0x77,0xDF,0xFF,0x7E,
10640xDF,0xED,0xBB,0xFE,0xFF,0xBE,0xEF,0xFB, 0xFE,0xFF,0xFA,0x3F,0xFF,0xBE,0x6F,0x8F,
10650xE6,0xF9,0xFE,0x7F,0x9F,0xC7,0xFE,0x7F, 0x9F,0xE7,0xF9,0xFE,0x7F,0x9F,0xE7,0xFB,
10660x7F,0xFF,0x7F,0xCF,0xFF,0xFD,0xFF,0xFF, 0xDF,0xFB,0xAF,0xBF,0xEF,0xFF,0xFE,0xFF,
10670x9F,0xEF,0xFB,0xFF,0xFC,0xFF,0xFB,0xFE, 0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xF7,
10680xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xF5,0xFF,0xFF,0xFF,0x3F,0xDF,0xF7,
10690xFF,0xFF,0x7F,0xEF,0xFE,0xFF,0xBF,0xFF, 0xFB,0xFF,0xFF,0xBF,0xEF,0xFF,0xB3,0x7F,
10700xFF,0x7B,0x5E,0xF7,0xFD,0xFF,0x7B,0x7F, 0xF7,0xFF,0x7F,0xDF,0xF7,0xFD,0xFF,0x7F,
10710xDF,0xF7,0xFF,0x17,0xFF,0xFF,0xFF,0x7F, 0xFF,0xFF,0xDD,0xF6,0xFC,0xBF,0xCB,0xF2,
10720xBC,0xBF,0x2F,0xCB,0xF2,0xFC,0xBF,0xFE, 0x8F,0xFF,0xFA,0x7E,0xBF,0xA7,0xEB,0xDA,
10730xFC,0xBF,0xAF,0x7A,0xFE,0xBF,0xAF,0xEA, 0xFA,0xFE,0xBF,0xAF,0xF4,0xDF,0xFE,0xFF,
10740xF3,0x3C,0x7F,0x3E,0xFF,0xCF,0xF8,0xBF, 0x8F,0xE3,0xF8,0xFE,0x3F,0x8F,0xE7,0xE8,
10750xFF,0xFC,0x9F,0xFF,0xFF,0xCF,0xEB,0xB3, 0xE7,0xFB,0x7B,0xF3,0xFE,0xFF,0xCF,0xDB,
10760xFB,0xFB,0xBF,0x6F,0x6F,0xDF,0xEC,0x7F, 0xFF,0xFF,0xF7,0xFD,0xFD,0xFF,0xFF,0xFF,
10770xFF,0xB2,0xBF,0xFF,0xDE,0xFD,0xBD,0xEF, 0xFB,0xF6,0xDF,0xEA,0xE7,0xDB,0xFE,0xBB,
10780xFF,0xEB,0xFB,0xBF,0x9F,0x8F,0xE8,0xFE, 0x3F,0x8F,0xA3,0xF8,0xFE,0x3F,0x8F,0xFF,
10790xF8,0x7E,0xFD,0xFD,0x7F,0xFF,0xFB,0xCD, 0xFF,0xFD,0xFF,0x5F,0xEF,0xFD,0xFF,0xFF,
10800xDF,0xF7,0xFD,0xFF,0xBE,0x90,0xFF,0xFF, 0xEE,0xFF,0x3F,0xBF,0xF3,0xBB,0xFE,0xB7,
10810xAB,0xFA,0xFE,0xAF,0xAD,0xEA,0xFA,0xDE, 0xAB,0xFF,0x63,0xFF,0xFE,0xF2,0xFF,0xB3,
10820xFF,0xDF,0xEE,0x7D,0xFF,0x03,0xF1,0xF4, 0x3F,0x1F,0xC3,0xF1,0xEC,0x7F,0xFE,0x6F,
10830xFF,0xFB,0xFB,0xFF,0x9F,0xFF,0xBF,0xFF, 0x7B,0x5F,0xFD,0xFF,0xDF,0xF7,0xFD,0xFD,
10840x7F,0x7F,0xDF,0xFE,0xCF,0xFB,0xFF,0xFF, 0xAF,0xFB,0xFF,0x1F,0xEF,0xA5,0xFD,0xBF,
10850xDF,0xFB,0x7D,0xFF,0xBF,0xDF,0xFB,0xFF, 0xFD,0x3B,0xFF,0xFF,0xFF,0xFF,0xFF,0xFD,
10860xAF,0xF3,0xFF,0xFB,0x7F,0xBF,0xD7,0xFB, 0xBF,0x7F,0xBB,0xF7,0xFF,0xF8,0x7F,0xFF,
10870xFA,0x5F,0xD7,0xFF,0xDF,0x7F,0xEF,0xFF, 0xFF,0x7F,0xDB,0xF7,0xFD,0xFF,0x7F,0xDF,
10880xB7,0xFB,0xEC,0xFF,0xFF,0xF7,0xBF,0xEF, 0xFD,0xFC,0xFB,0xFF,0xEF,0xF0,0xFE,0x3F,
10890x8F,0xE3,0xF8,0xFE,0x3F,0x8F,0xEF,0x8D, 0xFF,0xFF,0xEF,0x7F,0xBF,0xFF,0xFB,0xFF,
10900xDB,0xBF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xEF,0xD8,0xFF,0x2E,0x7F,
10910xBE,0xEF,0xFE,0x6E,0xFF,0xBF,0xF9,0xFF, 0xFF,0xF3,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
10920xFC,0x66,0xBE,0x47,0xF3,0x7F,0xDF,0xFE, 0x87,0x9F,0xFF,0xFF,0xFF,0xFF,0xE7,0xFF,
10930xFF,0xFF,0xFF,0xFF,0xFF,0xD6,0x6F,0x7C, 0xFB,0x4F,0xD2,0xFF,0xFD,0x2B,0xFE,0xFF,
10940xFF,0xFD,0x5F,0xD7,0xD5,0xF5,0x7D,0xFF, 0xFF,0xFF,0xBF,0x9B,0xFF,0xFF,0xDF,0xB7,
10950xFF,0xFF,0xDF,0xFF,0x3F,0xCF,0xFE,0x7F, 0xBF,0xEF,0xFB,0xFC,0xFF,0x3F,0xFF,0xD9,
10960xBF,0xFE,0x97,0xEC,0x8F,0xB7,0xFE,0x9B, 0x7D,0xFD,0xB7,0xDD,0x77,0x1D,0xC7,0x71,
10970xDD,0x77,0x5D,0xD7,0xF3,0x6F,0xFD,0x3F, 0x73,0xDD,0xAF,0xFD,0x7A,0xFF,0xFF,0xAF,
10980xFE,0xFD,0xBF,0xEF,0xFB,0xFE,0xFF,0xBF, 0xEF,0x66,0x7F,0xFF,0xFF,0xBF,0xBF,0xFF,
10990xFB,0xFF,0xF7,0xDF,0xFD,0xFB,0x7D,0xDF, 0xB7,0xCD,0xF3,0x7C,0x5F,0x3F,0x91,0x3F,
11000xFF,0x3D,0xEF,0x7B,0xFF,0xFC,0xFF,0xCA, 0xEF,0xFE,0xFF,0xBD,0xEF,0xFB,0x1E,0xE7,
11010xBB,0xEC,0x7F,0xB3,0xFF,0xFD,0x9F,0xFF, 0xFF,0xFE,0xFF,0xFF,0x7F,0xBF,0xFB,0xFE,
11020xFF,0xBF,0xEF,0xFB,0xEE,0xFB,0xBF,0xDF, 0x67,0xFF,0xFF,0xBF,0xEF,0xDB,0xFF,0xBC,
11030xFE,0x7F,0xFB,0xFF,0x9F,0xEF,0xF9,0xFE, 0x7F,0x9F,0xE7,0xF9,0xFE,0x87,0xFF,0xEE,
11040xFB,0xBE,0xE5,0xBF,0xEF,0xF9,0xD7,0x65, 0xF7,0xDD,0xE7,0x7D,0xDF,0x77,0x5D,0xD7,
11050x7F,0xF8,0x9B,0xFE,0xFF,0xBF,0xEF,0xFB, 0xFF,0xFF,0xBF,0xEF,0xFB,0xFF,0x7F,0xCF,
11060xF3,0xFC,0xFF,0xBF,0xEF,0xFF,0xDB,0x3F, 0xEF,0xFB,0xFE,0xFF,0xDF,0xFF,0xFE,0xFB,
11070xBB,0xEF,0xBF,0xEF,0xBB,0xEE,0xFB,0xBE, 0xEF,0xBB,0xFF,0xFC,0x7F,0xFD,0x3B,0x5B,
11080xD6,0xE5,0xFD,0x4F,0xC3,0xFB,0xFF,0xBF, 0xEF,0xFB,0xFE,0xFF,0xBF,0xEF,0xFB,0xFF,
11090xB4,0xFF,0xFA,0xBC,0x8F,0xB2,0xE9,0xD2, 0x2E,0xCF,0xFB,0xFF,0xBF,0xEF,0xFB,0xFE,
11100xFF,0xBF,0xEF,0xFB,0xFF,0xEC,0xFF,0xFD, 0xFD,0x7F,0xDF,0xF7,0xE4,0xDF,0x5F,0xFF,
11110xFF,0xFB,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xC3,0xFF,0xEF,0xE6,0xF8,0xFE,
11120x3F,0x8B,0x83,0xF9,0xFE,0x7F,0xE7,0xF9, 0xFE,0x7F,0x9F,0xE7,0xF9,0xFE,0x7F,0x17,
11130xFD,0xFF,0xFF,0xFF,0x7F,0x5F,0xF7,0x2C, 0xFF,0xFF,0xFF,0xFE,0x7F,0xFF,0xE7,0xF9,
11140xFE,0x7F,0x9F,0xFE,0x2F,0xFF,0xFF,0xEF, 0xFF,0xFE,0xBF,0xEF,0xAD,0xFF,0xFF,0x7F,
11150xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFE,0xDF,0xFF,0xDF,0xFF,0xFD,0xFD,0x7F,
11160xDF,0xF7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFA,0x3F,0xFE,
11170xF7,0xFD,0xEF,0x7A,0xFF,0xB1,0xBD,0xFF, 0x7F,0xF7,0xFD,0xFF,0x7F,0xDF,0xF7,0xFD,
11180xFF,0x7F,0xF3,0x27,0xFF,0xDF,0xFF,0xDD, 0xFF,0xFC,0x9B,0xFF,0xCB,0xFC,0xBF,0x2F,
11190xCB,0xF2,0xFC,0xBF,0x2F,0xC9,0xFF,0xDE, 0xFF,0xDF,0xAF,0xEB,0xDA,0xFE,0xBB,0xAF,
11200xEB,0xF8,0xF7,0xAF,0xE8,0xFA,0xFE,0xBF, 0xAF,0xEB,0xF2,0xFF,0xFD,0xFF,0xFF,0xEF,
11210xBD,0xD7,0xBF,0xFF,0xFF,0xDE,0x8F,0xB8, 0xDE,0x37,0x8D,0xA3,0x78,0xDA,0x3F,0x8F,
11220xFF,0xA1,0xFF,0xFF,0xFB,0xFB,0xFF,0xFF, 0xFF,0xFF,0xA7,0xBD,0xFB,0x76,0xFD,0xBF,
11230xEF,0xDB,0xFE,0xBB,0xBF,0xFE,0x27,0x7F, 0xFF,0xFE,0xFE,0xFD,0xF5,0xFF,0xEF,0xF5,
11240xDF,0x1F,0xE7,0xFD,0xFF,0x7F,0xDF,0xF7, 0xFD,0xFF,0xFF,0xCD,0xFD,0xAE,0xFF,0xFA,
11250x3E,0x3F,0xAB,0xFD,0xF8,0x7E,0x8F,0xE3, 0xF8,0xFE,0x3E,0x8F,0xE3,0xF8,0xFF,0xFE,
11260x1F,0xEF,0xDF,0xBF,0xFE,0xDE,0xDF,0xD9, 0xFF,0xDF,0xBC,0xFF,0xFF,0x7F,0xFF,0xEF,
11270xFD,0x7F,0xDF,0xF7,0xF9,0x3F,0xFE,0xFF, 0xFF,0x6F,0xFE,0xDE,0xBF,0xF7,0xED,0xEA,
11280xFD,0x8F,0x83,0xF8,0xEA,0x3F,0x8F,0xEF, 0xFF,0xF4,0x7F,0xFF,0xEF,0xEF,0x7B,0xF3,
11290xF1,0x5F,0xFF,0xFF,0xF1,0x3B,0x7F,0xDF, 0xF7,0xFD,0xFF,0xFF,0xFF,0xFF,0xE0,0xFF,
11300xFF,0xFF,0xF7,0xFF,0x6F,0xFF,0x7F,0xFF, 0xFF,0xF7,0xDE,0xF7,0xBF,0xEF,0xFB,0xF7,
11310xFD,0xFF,0xFF,0xF5,0xFA,0xFF,0xFF,0xFB, 0xE7,0xFF,0xF3,0xF8,0x7F,0xF3,0xDF,0xFF,
11320xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x1F,0xEF, 0xBB,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFD,
11330xFF,0x7F,0xFF,0x9F,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xCF,0xFF,0x37,0xFF,0xFF,
11340x7F,0xDF,0x77,0x5D,0xE7,0xFC,0xFF,0xBF, 0xF7,0xF5,0xFB,0xFF,0xFF,0xD7,0xF5,0xFB,
11350xFF,0xFF,0x45,0xFD,0x7F,0xEA,0xFD,0xBE, 0xBF,0xDF,0xF7,0xFF,0xFF,0xDB,0xFB,0xFE,
11360xFF,0xBF,0xEF,0xFF,0xFF,0xFF,0xFB,0x5F, 0x7F,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,
11370xFF,0xFE,0xFF,0xEF,0xFD,0xFF,0x7F,0xDF, 0xFF,0xEF,0xFB,0xF8,0x0F,0xF3,0xFF,0xF9,
11380x2E,0xFB,0xFE,0xFC,0xF3,0xEF,0xFF,0xFF, 0xBF,0xFF,0xFB,0xE7,0xFF,0xFE,0x7E,0xFF,
11390xC0,0x6B,0xCF,0xFF,0x34,0xDF,0xF1,0xFD, 0xFF,0xEF,0xFF,0xFF,0xFF,0xDF,0xF7,0xFD,
11400xCF,0x7F,0x9C,0xFD,0xFD,0x6C,0xF7,0xFF, 0xF6,0xFD,0xEB,0x2B,0x9F,0xFF,0xFC,0xFE,
11410x7E,0xFF,0xFF,0xFF,0xFF,0xD7,0xF3,0xF7, 0xFF,0xFB,0xE1,0xBF,0xFF,0xEB,0x7A,0xDE,
11420xD7,0xFB,0xFF,0xF9,0xFE,0xFF,0xFF,0xF3, 0xDE,0x7F,0xFD,0xE7,0x7F,0xFF,0xFD,0xBB,
11430xFF,0xFF,0x7E,0xCC,0xF6,0xAF,0x5F,0x7F, 0xFE,0xF4,0x7D,0xF7,0xFD,0xBB,0x6E,0xDB,
11440xB7,0xFF,0xF7,0xDF,0x66,0xFF,0xFF,0xF7, 0x3D,0xCF,0xDE,0xBD,0xFF,0xFF,0xDE,0xDB,
11450x8D,0xF7,0x7E,0xDF,0xB7,0xEF,0x7F,0xFF, 0xF6,0x87,0xFF,0xFF,0xEF,0xFE,0xDE,0xBF,
11460xFF,0xFF,0xFF,0xBB,0xEF,0xFD,0xFF,0x7B, 0xDE,0xF7,0x3F,0xFF,0xBF,0xFB,0xDB,0xFF,
11470xF2,0xB6,0xFD,0xBD,0x7F,0xE7,0xFF,0xFF, 0xFF,0x6F,0xF7,0xFF,0xFF,0xFF,0xFE,0x77,
11480xFF,0xBF,0xF8,0xAF,0xFF,0xDF,0xBF,0xFF, 0xBF,0x7F,0xFB,0xFF,0xFF,0xFF,0xDB,0xFE,
11490xFF,0xBF,0xFF,0xFA,0xFF,0xFD,0xFF,0xF6, 0x7F,0xFF,0x9F,0xFF,0xFF,0x3F,0xEF,0xF8,
11500xEE,0x7E,0x9F,0xBA,0xFE,0xBF,0x8F,0xEF, 0xFE,0xFE,0xF9,0xFF,0xFA,0x7F,0xFE,0x7E,
11510xBF,0xAF,0xFB,0x96,0xFD,0x9F,0xEF,0x5E, 0x65,0xBE,0xEF,0x5B,0xB6,0xFF,0xBE,0xE3,
11520xFF,0xB5,0xBF,0xFF,0xFD,0xFF,0x7F,0xFF, 0xEF,0xDF,0xFE,0xFF,0xBF,0xFB,0xFE,0xFF,
11530xBF,0xCF,0xFF,0xFF,0xFF,0xFD,0x9B,0xFF, 0xFE,0xFB,0xFE,0xDF,0xFF,0x7F,0xFF,0xF7,
11540xFE,0xFF,0xDF,0xFB,0xFB,0xFE,0xFF,0xFF, 0xFF,0xFF,0xFF,0xB7,0xFE,0xFA,0xFF,0xAB,
11550xEF,0xFF,0xFD,0xB5,0x7B,0x7F,0xFB,0xF7, 0xFD,0xFF,0xFF,0xDD,0xFF,0xEF,0x8F,0xFF,
11560x2F,0xFF,0xFB,0x7C,0xFF,0x3F,0xDF,0x73, 0xEB,0xFE,0x3F,0xFF,0xEF,0xFB,0xFE,0xFF,
11570xEF,0xFD,0xFF,0xBF,0xFD,0x0F,0xFF,0xFF, 0xFF,0xF5,0xF9,0xFF,0x7F,0xD7,0xFD,0xFF,
11580xDF,0xFF,0xF7,0xFB,0xFF,0x7F,0xBF,0xFF, 0xFF,0xF0,0x9F,0xFF,0xFE,0x7F,0x8B,0xE3,
11590xF9,0xDE,0x27,0x9B,0xE6,0xBE,0x7F,0x9B, 0xC3,0xF8,0xDE,0x7F,0x9D,0xE7,0xFE,0x7F,
11600xFF,0xFF,0x5F,0xD7,0xFF,0xFF,0xFF,0x4F, 0xFB,0xFF,0xFF,0x7F,0xFF,0xAF,0xFF,0x9F,
11610x7F,0xFB,0xFF,0xE8,0xFF,0xFF,0xFE,0xBF, 0xAF,0xFF,0xFF,0xFE,0xBF,0xEF,0xF7,0xFF,
11620xBF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF7,0xFF, 0xFC,0xFF,0xFF,0xFD,0x7F,0xFF,0xFF,0xFF,
11630xFD,0x3F,0xCF,0xFF,0xFF,0xFF,0xFF,0xF7, 0xFF,0xFD,0x7F,0xFF,0xFF,0x93,0xFF,0xFF,
11640x7A,0xDF,0xF7,0xFF,0xFF,0x7B,0x7F,0xB7, 0xEF,0xFF,0xFF,0xFD,0xBF,0xFD,0xFB,0xFF,
11650xF7,0xFF,0xD7,0xFF,0xFF,0xFF,0xFC,0x9F, 0x6F,0xCB,0xFF,0xF4,0xBB,0xDF,0xD6,0xFD,
11660xBF,0x2F,0xD3,0xF7,0xFF,0xDF,0xFF,0xCF, 0xFF,0xFA,0xBE,0xBD,0xAF,0x6A,0xDA,0xBE,
11670xBB,0xAB,0x3A,0xBE,0x2D,0xAE,0xEB,0xDA, 0xF6,0x3F,0xAD,0xF5,0xDD,0xFF,0xCF,0xF1,
11680xFF,0xF9,0x7F,0xFF,0x73,0xFE,0xFF,0xCF, 0xC3,0xF4,0xF7,0x2F,0xF3,0xFF,0xFC,0xFF,
11690x7C,0x1F,0xFF,0x3F,0x4F,0xFF,0x7E,0xFF, 0xEF,0xBD,0xF6,0xFE,0xFF,0x2B,0xEF,0xDC,
11700xFB,0xFD,0xFF,0xFB,0xFF,0xEA,0x7B,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFB,0xF7,0xDF,0xFF,
11710xE3,0x7D,0xFF,0xB7,0xFF,0xBF,0xFF,0xFF, 0xDF,0xFF,0xF8,0xFF,0xBF,0xFF,0xBF,0xEB,
11720xE7,0xFA,0xFE,0x3D,0xBF,0xE9,0xFC,0xBF, 0xFF,0xFA,0xFB,0xFE,0xFF,0xFF,0xFF,0xD9,
11730xFF,0xFF,0xFF,0xF6,0x7F,0xFF,0xF6,0x7D, 0xFF,0xDF,0xCF,0xFD,0xBF,0xFB,0xEF,0x7E,
11740xFF,0x7F,0xFF,0xFF,0xD3,0xFF,0xFD,0xFB, 0xFF,0xFB,0xFF,0xFF,0xFF,0xEF,0xFF,0xBF,
11750xFE,0xFF,0xF7,0xEF,0xFF,0xFF,0xFF,0xFB, 0xFF,0x87,0xFF,0xFD,0xFF,0xFF,0xFF,0xFF,
11760x7B,0xFE,0xFF,0xFE,0x3B,0xF7,0xF7,0xFF, 0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0x0F,0xFF,
11770xFF,0xFF,0xFF,0xFB,0xFF,0xFF,0xFF,0xF7, 0xFF,0xFF,0xAD,0xFF,0xFE,0xF7,0xFF,0xFF,
11780x5F,0xFF,0xFF,0xDF,0xFF,0xFD,0xFF,0xF5, 0xFF,0xDF,0xFF,0xBD,0xFF,0xE9,0xFF,0xC7,
11790xF3,0xFF,0xFF,0xF7,0xFF,0xF3,0xFF,0xF8, 0x3B,0xFF,0xFF,0x7B,0xDF,0xBF,0xFB,0xEF,
11800xFB,0xFF,0xFB,0xF7,0xF7,0xBB,0xFF,0xFF, 0xFF,0xFF,0xFB,0xFF,0xFE,0x7F,0xF3,0x7F,
11810x5E,0xB7,0xBF,0xFD,0x7F,0xFF,0xF9,0x7F, 0xFB,0xFF,0xEB,0xFD,0x7F,0x7F,0xFF,0xEF,
11820xFB,0xE0,0x3F,0xFE,0xBF,0xBF,0xDF,0xFF, 0x7E,0xFF,0xF7,0xFF,0xFF,0xFE,0xBF,0xFF,
11830xDB,0x78,0xFF,0xFF,0xFF,0xEE,0xA1,0xBF, 0xF5,0xDE,0xFB,0xF7,0xFF,0xFB,0xFF,0xFF,
11840xFF,0xFF,0xFB,0xFF,0xFF,0xD7,0xFF,0xFF, 0xFF,0xFF,0xEF,0xF0,0xFF,0xFF,0xFF,0xF3,
11850xF7,0xFF,0xEF,0xFF,0xE7,0xCF,0xFF,0xFB, 0xFF,0xEF,0xFF,0xFF,0x9F,0x9F,0xEF,0xFC,
11860x16,0xBF,0xFE,0xF3,0xE4,0xFF,0xFF,0xC6, 0xFF,0xE7,0xFF,0xFF,0xFD,0xFF,0xBF,0xFF,
11870xFF,0x3F,0xFF,0xBF,0xD6,0xAF,0x7F,0xFE, 0x6B,0x7E,0x7F,0xFF,0xAF,0xFF,0xFF,0xBF,
11880xFF,0x5F,0xFF,0xFE,0xFF,0xFF,0xFE,0xFF, 0xFF,0xBD,0xDB,0xFF,0xFE,0x5F,0xF2,0xFF,
11890xFF,0x5F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xEF,0x7F,0xFF,0xFF,0xFF,0xFF,0xDE,0xBF,
11900xFF,0xFF,0xEF,0xFB,0x77,0xFE,0xBD,0x7F, 0x5F,0xFF,0xFF,0xFF,0xDF,0x6F,0xED,0xFF,
11910xFD,0xFF,0x7F,0xFD,0x6F,0xFF,0xFF,0x77, 0xDA,0xCF,0xFD,0x5F,0xFF,0xBF,0xFF,0xFF,
11920xDF,0x7F,0xFF,0xFB,0xFF,0xFF,0xFF,0xFF, 0x66,0x7F,0xFF,0xFE,0xBF,0xE7,0xBF,0xFA,
11930xFF,0xFE,0xFF,0xFF,0xFF,0xDF,0xFF,0x59, 0xEF,0xFF,0xEF,0xFB,0x7F,0x89,0xFF,0xFF,
11940xE9,0xFF,0x6F,0xFF,0xF5,0xFF,0xFF,0xFF, 0xFF,0xFF,0x7F,0xF2,0xF7,0xFF,0xFF,0xEF,
11950xF8,0x7F,0xFB,0xFF,0xFD,0xFF,0xFF,0xD9, 0xFF,0xEF,0xBB,0xFF,0xFF,0xFF,0xBF,0xEF,
11960xDE,0xFF,0xFF,0x9F,0x7F,0xDF,0xFF,0xF7, 0xFF,0xFF,0xFF,0xFF,0xDF,0xFF,0xFF,0xAF,
11970xFF,0xFF,0xF7,0x3F,0xEB,0x9F,0xFE,0x7F, 0x9E,0x7F,0x9F,0xFE,0x87,0xFF,0xED,0xDB,
11980x56,0xFF,0xBF,0xAF,0x0B,0xD2,0xFF,0xEF, 0xDB,0x6E,0x7D,0xBD,0x6F,0xF8,0xFE,0x3F,
11990xFA,0x5B,0xFF,0xFD,0xBF,0xEF,0xFF,0xBF, 0x6F,0xDB,0xE6,0xFF,0xFF,0x3F,0xFF,0xDF,
12000xFE,0xFF,0xFF,0xFF,0xFF,0xDA,0x3F,0xFF, 0xFB,0xFE,0xFE,0xFF,0xFF,0xDF,0xF7,0xBD,
12010xFF,0xFD,0xFF,0xFE,0xFF,0xFB,0xFF,0xFF, 0xFF,0xFF,0xF1,0x5F,0xFD,0x9F,0xDF,0xFD,
12020xFF,0xFD,0x7F,0xFF,0xFF,0xFF,0xFF,0x76, 0xFA,0xFF,0xFF,0x7F,0xE3,0xF8,0xFF,0xAE,
12030xFF,0xFB,0x7E,0x9D,0x73,0xFF,0xFA,0x7F, 0xDF,0xFF,0xFF,0x7F,0xFF,0xFB,0xCD,0xFF,
12040x7F,0xEF,0xFB,0xFF,0xFD,0xFF,0xF7,0x7F, 0x7F,0xEF,0xFF,0xED,0xFF,0xFF,0xFF,0xB5,
12050xFF,0xBF,0xFF,0xBF,0xFD,0xEF,0xDB,0xF7, 0xFF,0x93,0xFF,0xEF,0xE2,0xF9,0xBE,0x7F,
12060x8B,0xE7,0xF9,0xFE,0x6B,0xE7,0xF9,0xFE, 0x7F,0x9F,0xE7,0xF9,0xFE,0x7F,0x47,0xFF,
12070xFF,0xFD,0xFF,0x9F,0xFF,0xD7,0xFF,0xFF, 0xFF,0xFF,0xF5,0xFF,0x9F,0xFF,0xF7,0xFE,
12080xFF,0xBF,0xFE,0x6F,0xFF,0xFF,0xFB,0xFF, 0xFF,0xFF,0xAF,0xFF,0xFF,0xFF,0x7F,0xFB,
12090xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFD, 0xDF,0xFF,0xFF,0xF7,0xFF,0xFF,0xFF,0xDF,
12100xFF,0xFF,0xFF,0x5F,0xFF,0xFF,0xFF,0xFF, 0x5F,0xFB,0xFE,0xFF,0xF8,0x37,0xFF,0xFF,
12110xEF,0xFF,0x7F,0xFE,0xBF,0xFF,0xFF,0xFE, 0xBF,0xFF,0xFF,0x7F,0xFF,0xBF,0xFD,0xFF,
12120x7F,0xFA,0x7F,0xFF,0xFF,0x6F,0xFF,0xFF, 0x7D,0xFF,0xCF,0xFF,0xFF,0xFF,0x4F,0xFF,
12130xF2,0xFF,0xFF,0xFF,0xFF,0xFF,0xFA,0xBF, 0xFF,0xAE,0xEB,0xFA,0xFE,0xBB,0xAD,0xEB,
12140xFA,0xF7,0xAF,0x6B,0xFA,0xF6,0xBF,0x25, 0xE9,0xF2,0x7F,0x45,0xFF,0xFF,0xFD,0xF7,
12150xF7,0xBF,0xFF,0xDF,0xFF,0xFF,0xBF,0xFB, 0xFF,0xDF,0xF3,0xFF,0xF7,0x3F,0xCF,0xFF,
12160xA1,0xFF,0xFF,0xBF,0xE7,0xFF,0xFF,0x7F, 0xFF,0x3D,0xFF,0xFF,0xFF,0xF7,0xFF,0x2F,
12170xFF,0xFB,0xF5,0x7F,0xFE,0x57,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF7,
12180x3F,0xFF,0xFE,0xFF,0xFF,0xFF,0xFD,0xFE, 0xF7,0xEE,0xAF,0xFE,0xEE,0xE7,0xFA,0xFF,
12190xFE,0x9D,0xF9,0x5E,0xFE,0xFF,0xEB,0xFF, 0xFF,0xDF,0xA7,0xFF,0xFF,0xFF,0xFC,0xDB,
12200xFF,0xFF,0xFF,0x7E,0xFB,0xFF,0xFF,0xEF, 0xFB,0xFD,0xFF,0xDB,0xFF,0xFF,0xFF,0xEF,
12210xFF,0xFF,0xFF,0xFD,0xBF,0xFE,0xBF,0xFF, 0x6F,0x7F,0xFF,0xF7,0xFF,0xFF,0xF9,0xFF,
12220xF7,0xFF,0xBF,0xDE,0xF7,0xFF,0xFF,0xFF, 0xFA,0x7F,0xFD,0xBF,0x5F,0xFF,0xFF,0xBF,
12230xFF,0xED,0xFF,0xF7,0xBF,0xFF,0xFF,0xEF, 0xFF,0xDF,0xFF,0xFF,0xFF,0xE6,0xFF,0xFB,
12240x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xF7,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xEB,0xFF,
12250xFD,0xFF,0xF5,0xFF,0xF6,0x7F,0xDF,0xBD, 0xCF,0xFF,0xFF,0xFF,0xFF,0xDF,0xFF,0xFF,
12260xFF,0xF9,0xFF,0xFF,0xFF,0xFF,0xFF,0xE3, 0xFF,0xEE,0xBF,0xFF,0x7D,0xEF,0xFE,0xFF,
12270xFF,0xFF,0xBF,0xFF,0xFF,0xFF,0xFF,0xFE, 0xFF,0xFF,0xFF,0xFF,0xE7,0xFF,0xB5,0xAE,
12280xFF,0xFF,0xB6,0xFE,0xBF,0xFF,0xFF,0xBF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
12290xFF,0x27,0xFF,0xEF,0xFE,0x7F,0xDF,0xFF, 0x7E,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
12300xFF,0xFF,0xFD,0xFF,0xF7,0xF9,0x9F,0xFF, 0x5F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,
12310xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0x0F,0xFF,0xE7,0xBF,0xFE,
12320xFF,0xBF,0xFF,0xFF,0xFF,0xFF,0xFC,0xBF, 0xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xC4,
12330x6B,0xFF,0x29,0x1F,0xFB,0xAF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xEF,0x1B,0xFE,0xFF,0xFC,
12340x6F,0xFF,0xFF,0xFD,0x6A,0xF7,0xD7,0xF5, 0xBF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,
12350xFE,0xBF,0xFF,0xFF,0xFA,0xFF,0xFF,0xF7, 0xFB,0xDD,0xBF,0xFF,0xE7,0xFF,0xFF,0xFF,
12360xFF,0xFF,0xFF,0xFF,0xFF,0xFD,0x7F,0xFF, 0xFF,0xF5,0xFF,0xFF,0xF7,0xFD,0xB3,0xEF,
12370xFD,0x7E,0x5D,0xFF,0xFD,0xFF,0xFF,0xFF, 0xFD,0x7F,0xD2,0xF5,0xFB,0x7E,0xCB,0xB7,
12380xFF,0xFF,0xFF,0xC6,0xFF,0xFD,0xEE,0x63, 0xFF,0xFF,0xFF,0xFF,0xFF,0xF6,0xFD,0x65,
12390x5B,0xDF,0xFF,0xD5,0xFF,0xFF,0xFF,0xF6, 0xE7,0xBF,0xF7,0xA9,0xFF,0xFF,0xED,0xFF,
12400xFF,0xFF,0xFF,0xFF,0xEB,0xFF,0xFF,0xFF, 0xAF,0xFF,0xFF,0xFF,0xF8,0x1B,0xFF,0xE3,
12410xD0,0xBF,0xFF,0xE1,0xFF,0xFF,0xFF,0xFF, 0xFF,0xD7,0xFF,0xFF,0xFF,0x5F,0xFF,0xFF,
12420xFF,0xFF,0xAF,0xFF,0xDB,0x76,0xBF,0xFF, 0x7F,0xFF,0xBF,0xEF,0xFE,0xFF,0xBF,0xEF,
12430xFB,0xFE,0xFF,0xFF,0xFF,0xBF,0xF2,0x7F, 0xFF,0x9F,0xFE,0xBD,0xFE,0x7F,0xFF,0xFF,
12440xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xF7,0x3F,0xEC,0x7F,0xF6,0x95,0xBB,
12450xEF,0xF8,0xFE,0xFC,0xBF,0x2F,0xDA,0xFC, 0xBF,0x2F,0xCB,0xF2,0xFC,0xBF,0xEF,0xFF,
12460xA9,0xBF,0xCF,0xFB,0xFF,0xFF,0xFF,0xFE, 0xDD,0xB7,0x6D,0xF6,0xD9,0xB6,0x6D,0x9B,
12470x76,0xD9,0xBF,0xFB,0xFD,0xA3,0xFF,0xBF, 0xEF,0xFF,0xEF,0xFF,0xFF,0xFF,0x7F,0xDF,
12480xFD,0xEF,0x7B,0xDE,0xF7,0xFD,0xEF,0x7F, 0xFF,0xFF,0x05,0xFF,0xFA,0xFE,0x7F,0xEF,
12490xE3,0xFF,0xFF,0xFD,0x7F,0xFF,0xFF,0xFF, 0xFF,0x5F,0xFF,0xFF,0xFD,0x7F,0xFB,0xAF,
12500xFF,0x63,0xC8,0xFF,0xBF,0xEF,0xFF,0xFF, 0xFA,0x7F,0xFF,0xFF,0xFF,0xFE,0x9F,0xF7,
12510xFF,0xFA,0xBF,0xFE,0x9F,0xFB,0x7F,0xFF, 0xFF,0xEF,0xD7,0xFF,0xFF,0xF5,0xFF,0xFF,
12520xFF,0xFF,0xFD,0x7F,0xFF,0xFF,0xBF,0xFF, 0xF9,0xBF,0xFF,0xBE,0x27,0x9F,0xE7,0xF9,
12530xFE,0x7F,0x8B,0xE7,0xFE,0x7F,0x9F,0xE2, 0xF9,0xFE,0x7F,0x9F,0xE7,0xF1,0x7F,0xFF,
12540xFF,0xFF,0xFB,0xFE,0xFF,0xFF,0xFF,0xD7, 0xFF,0xFF,0xFF,0xFF,0xF5,0xFF,0xFF,0xFF,
12550xD7,0xFF,0xFA,0xFF,0xFE,0xFF,0xFF,0xFF, 0xFD,0xFF,0xFF,0xFF,0xAF,0xF7,0xFF,0xFF,
12560xFF,0xEB,0xFF,0xFF,0xFF,0xAF,0xFF,0xC4, 0xFF,0xF7,0xFF,0xFF,0xEF,0xFF,0xFF,0xFF,
12570xFF,0x5F,0xFF,0xFF,0xFF,0xFF,0xD7,0xFF, 0xFF,0xFF,0xFF,0xFF,0xEB,0xFF,0xFB,0x7A,
12580xDF,0xF7,0xFD,0xFF,0xFF,0xFE,0xBF,0xFF, 0xFF,0x7F,0xFF,0xAF,0xFF,0xFF,0xFF,0xF7,
12590xEF,0xE3,0xFF,0xDD,0xD2,0xFF,0xDF,0xFF, 0xFF,0xF2,0xFC,0xBF,0xCB,0xF6,0xFD,0xBF,
12600x2F,0xCB,0xFF,0x7F,0xDF,0xDE,0xAF,0xFF, 0xDA,0xEE,0xBF,0xAF,0xE9,0xFA,0xF4,0xBD,
12610xAF,0x5A,0xAE,0xBB,0xAB,0x6B,0xDA,0xDE, 0xBF,0xAD,0xD7,0x5E,0xFF,0xFF,0xBF,0xFC,
12620xFF,0xDF,0xFD,0xFF,0xFF,0xFF,0xFF,0xDF, 0xF7,0xFF,0xFF,0xFF,0xFF,0xFD,0xFF,0xFA,
12630x1F,0xFF,0xFE,0xFB,0xEF,0xBF,0xFD,0xFF, 0xFD,0xBD,0x77,0xFF,0xFF,0xFF,0xFF,0x9D,
12640xEF,0xFF,0xFF,0xFF,0xEF,0x7D,0xFF,0xFB, 0xFE,0xEF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF7,
12650xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xEE, 0xBF,0xE4,0xFB,0xFF,0xFE,0x3F,0xFE,0xFF,
12660xFF,0xFF,0xFF,0xAF,0xEA,0xFE,0xBF,0xAF, 0xEB,0xFA,0xFE,0xFF,0xFF,0xFF,0x55,0xF6,
12670xFF,0xFE,0xF7,0xFF,0x7F,0xFF,0xEB,0xF7, 0x5F,0xC5,0xFD,0x7F,0x5F,0xD7,0xF5,0xFF,
12680x6F,0xFB,0xFF,0x8A,0xFF,0xFF,0xFF,0xFF, 0xEB,0xFF,0xFF,0xFF,0xFF,0xFB,0xBF,0xBF,
12690xEF,0xFB,0xFF,0xFF,0xFF,0xFF,0xFB,0xFF, 0x77,0xDF,0xFB,0xFF,0xFD,0x7F,0xEF,0xFF,
12700xFF,0xFF,0xBF,0x7F,0xFF,0xDF,0xBF,0xFF, 0xFB,0xFF,0xFF,0xFF,0xFE,0xEF,0xDF,0xFF,
12710xFE,0xFF,0x9F,0xEF,0x7D,0xFF,0xF7,0xFF, 0x7F,0xFF,0xFF,0xDF,0xF7,0xFD,0xFF,0xEF,
12720xDF,0xFF,0xDF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFD,0xFF,0xFF,0xFB,
12730xFD,0xFF,0xBF,0xDF,0xD1,0xFF,0xF8,0x3B, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
12740x7E,0xDB,0xFD,0xFF,0x77,0xDB,0xB7,0x7D, 0xBF,0xFB,0xFF,0xF8,0x7F,0xED,0x7B,0x5E,
12750xFF,0xFE,0xFF,0xFF,0x4F,0xD7,0xFD,0x7F, 0xDF,0xD7,0xF5,0xFF,0x7F,0xFF,0xFF,0xFF,
12760xF2,0x3F,0xFE,0xFF,0xBF,0xFF,0xFF,0xFF, 0xFF,0xBF,0xEF,0xFE,0xFF,0x3B,0xEE,0xFF,
12770xFC,0xEF,0xFF,0xFF,0xFF,0x85,0xFF,0xFD, 0xFE,0xFF,0xF5,0xFF,0xFF,0xFE,0xFF,0xDF,
12780xFB,0xFF,0x5F,0xBF,0xFF,0xFD,0xFF,0xFF, 0xFF,0xFF,0xA8,0xFF,0xFF,0x9F,0x9E,0xFF,
12790xFF,0xFF,0x7F,0xF3,0xFF,0xFF,0xCF,0xFF, 0xF7,0xFD,0xFF,0x7F,0xFF,0xFF,0xFC,0x16,
12800xBF,0xCF,0xA3,0xE5,0xEF,0x7F,0xFF,0xF3, 0xE4,0xFF,0xCF,0x93,0xFC,0xFF,0x3F,0xCF,
12810xFF,0xFF,0xFF,0xD6,0x0F,0x7D,0xBF,0x6E, 0xFB,0xF4,0xFC,0xAF,0x6D,0xDB,0x77,0xB7,
12820x6D,0xDB,0xF6,0xFD,0xBF,0xFF,0xFF,0xFF, 0xBF,0x9B,0xFA,0xDE,0xB7,0xB7,0xED,0xF9,
12830x7E,0xB7,0xAC,0xEB,0xD6,0xB3,0xAD,0xEB, 0x7A,0xDF,0xFF,0xFF,0xFF,0xD8,0xBF,0xFF,
12840xB7,0xED,0x9F,0x6F,0xDD,0xF7,0x68,0xDB, 0x37,0xB3,0x6C,0xDB,0x36,0xCD,0xB3,0x7F,
12850xFF,0x7F,0xF5,0x6F,0xFD,0xEF,0x79,0x3D, 0xF7,0x93,0xE4,0x7A,0x9E,0xAD,0xEA,0x7A,
12860x9E,0xF7,0xBD,0xEF,0xFF,0xFF,0xFF,0x76, 0x7F,0xFB,0xC6,0xFF,0xBB,0xEF,0xDA,0xFE,
12870xFD,0xBF,0xFB,0xFE,0xFF,0xBF,0xEF,0xFB, 0xFF,0xFF,0xFB,0xFF,0xA5,0xFF,0xFD,0xAB,
12880x6F,0x78,0xDE,0x17,0x8F,0x79,0xDF,0xFD, 0xFF,0x7F,0xDF,0xF7,0xFD,0xFF,0xFF,0xFB,
12890xFF,0xFB,0xFF,0xEF,0xFB,0xEF,0xFB,0xFE, 0xFF,0xBB,0xDA,0xF3,0xEF,0x3B,0xCE,0xF3,
12900xBC,0xEF,0x3F,0xCF,0xDF,0xFF,0xB7,0xFF, 0xFF,0xFF,0xCF,0x73,0xFF,0xBF,0xEF,0xFF,
12910xF3,0xFF,0x3F,0xCF,0xF3,0xFC,0xFF,0x3D, 0xCF,0x9F,0xFE,0x07,0xFF,0xAF,0xEB,0xFE,
12920xFD,0xBF,0xEF,0xEB,0xFA,0xFF,0xAF,0xEB, 0xFA,0xFE,0xBF,0xAF,0xFB,0xFE,0x3F,0xFB,
12930x9B,0xFF,0x7F,0xDF,0xFF,0xF3,0xFE,0xFF, 0xDE,0xF7,0xBF,0x7B,0xDE,0xF7,0xBD,0xEF,
12940x7B,0xFE,0xFF,0xFF,0xDF,0x3F,0xFE,0xFF, 0xB7,0xFF,0xEF,0xF7,0xFF,0xBF,0xED,0xFE,
12950xDF,0xB7,0xED,0xFB,0x7E,0xDF,0xFF,0xFF, 0xFF,0xFD,0x5F,0xEF,0xEB,0xFA,0xFE,0xF5,
12960xBF,0x6F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFE,0xF8,0xFF,0xA8,0xFF,
12970xFF,0xBF,0xEF,0xFB,0x6A,0xFB,0xB7,0xEF, 0xFB,0xFF,0xBF,0xEF,0xFB,0xFE,0xFF,0xBF,
12980xEF,0xFB,0xFF,0xE0,0xFF,0xFF,0xFD,0x7F, 0x5C,0xD7,0x7D,0xDF,0xF3,0x5C,0xF5,0xCD,
12990x73,0x5E,0xD7,0xB5,0xFD,0x7F,0xEF,0xFF, 0xDB,0xFF,0xFF,0xE2,0xF8,0xBE,0x2F,0x8F,
13000xE7,0xF8,0xBE,0x6B,0xE2,0xF8,0xBE,0x2F, 0x8B,0xE2,0xF9,0xFE,0x7F,0xE7,0xFF,0xD7,
13010xF5,0xFD,0x7F,0xFF,0xF7,0xF5,0xFD,0x7F, 0xD7,0xF5,0xFD,0x7F,0x5F,0xD7,0xF5,0xFF,
13020xFF,0xFF,0x8F,0xFF,0xAF,0xEB,0xFA,0xFF, 0xFF,0xBF,0xEB,0xFA,0xFF,0x2F,0xEB,0xFA,
13030xFE,0xBF,0xAF,0xEB,0xFF,0xFF,0xFE,0x5F, 0xFF,0x5F,0xFF,0xFF,0xFD,0xFF,0xFF,0xD7,
13040xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xBF,0xFE,0xB7,0xFD,
13050xFF,0x7E,0xDF,0xF7,0xAD,0xFF,0x7F,0xF7, 0xFD,0xFF,0x7F,0xDF,0xF7,0xFD,0xFF,0x7F,
13060xF6,0x7F,0xFF,0xFF,0xFF,0xDB,0xF6,0xFC, 0xAF,0xFF,0xFF,0xFF,0xFF,0xF7,0xFF,0xFF,
13070xFF,0xFF,0xFF,0xFF,0xFF,0xEC,0xBF,0xFF, 0xAF,0xEB,0xFA,0xF6,0xAB,0x8F,0xEB,0xFA,
13080xF7,0xA5,0xEB,0xFA,0xBE,0xBF,0xAF,0xEB, 0xFA,0xFF,0x6D,0xFF,0xFF,0x7F,0xDF,0x33,
13090xDD,0xFF,0x7F,0xFE,0xF7,0xFC,0x7F,0xFB, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xA9,
13100xFF,0xFD,0xFF,0xFF,0xFE,0xFF,0xFF,0xDF, 0xFF,0xFF,0xEF,0xEF,0xFD,0xFF,0x7F,0xFF,
13110xFF,0xFF,0xFF,0xFE,0xA7,0xFF,0xFF,0xFF, 0x77,0xDF,0xF7,0xFD,0x9F,0x7F,0xFE,0x77,
13120xEF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xAF,0xBF,0xAF,0xFF,0xF9,0xBE,0xBF,
13130x8F,0xFB,0xFE,0xFE,0xEF,0xFB,0xFE,0xFF, 0xBF,0xEF,0xFB,0xFF,0xFF,0xFD,0xDF,0x6F,
13140xEF,0xFF,0x7F,0xFF,0xBF,0xBF,0xDF,0xFF, 0xFC,0xFF,0xDF,0xF7,0xFD,0xEF,0x7F,0xDF,
13150xFF,0xFF,0xFF,0x3F,0xF6,0xFF,0xCF,0xFF, 0xDB,0xFB,0xF7,0xFF,0xEB,0x7A,0xFF,0xFF,
13160xFF,0xBF,0xEF,0xFB,0xFF,0xFF,0xFF,0xFE, 0x6D,0xFD,0xFF,0x5F,0xFB,0xFF,0xFF,0xF7,
13170xFF,0x5F,0xF5,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xF8,0xFF,0xFB,0xFF,
13180xFF,0xFD,0xFF,0xFF,0xFF,0xFF,0xE7,0xF6, 0xBF,0xFF,0xFF,0xFF,0xFF,0xFB,0xFF,0xFF,
13190xFF,0xC9,0xFF,0xFF,0xFF,0xBD,0xFF,0xBF, 0xAF,0xEF,0xEF,0x3F,0xD1,0xFC,0x7F,0xFB,
13200xC7,0xFF,0xFF,0xFF,0xFF,0xFF,0xE3,0xFF, 0xFF,0xFF,0xFF,0xFD,0xFF,0xFF,0x77,0xFF,
13210xDF,0xB7,0xFD,0xF7,0xFD,0xF7,0xFF,0xFF, 0xFF,0xFF,0xFF,0x57,0xFF,0xF7,0xA5,0xFD,
13220x3F,0xDF,0xBF,0xBF,0xFE,0x7F,0xFF,0xFF, 0xFF,0xDF,0xFA,0xFD,0xFF,0xFF,0xFF,0xFE,
13230x87,0xFF,0xE9,0xFF,0xFE,0xEF,0xBF,0xEF, 0xFE,0xFE,0xFF,0xEF,0xFF,0xFF,0xFF,0xFF,
13240xFF,0xFF,0xFF,0xFF,0xFA,0x9F,0xFF,0x3F, 0xFF,0xFD,0xFD,0x57,0xDF,0xFD,0xF3,0xFF,
13250xDF,0xFD,0xFF,0x5F,0xDF,0xF5,0xFD,0xFF, 0xFF,0xF9,0x8F,0xFF,0xFF,0xFF,0xEE,0x7F,
13260xFF,0xFF,0xBF,0x5E,0xFE,0xEC,0xFB,0x3F, 0x7F,0x9F,0xEF,0xF9,0xFF,0xFF,0xCD,0x6B,
13270xFF,0xFF,0xFF,0xC5,0xF3,0xFC,0xFA,0x38, 0xFF,0xAF,0x3F,0xEE,0x7F,0x9F,0xFF,0xD9,
13280xFF,0xFF,0xFD,0x7A,0xF7,0xFF,0xF3,0xFF, 0xAF,0x6F,0xDB,0xF2,0xB9,0xE9,0xFB,0xFF,
13290xFF,0xFF,0xFE,0xFF,0xFF,0xEF,0xFF,0xFB, 0xC5,0xBF,0xFF,0xEF,0xFF,0x5E,0xB7,0xAD,
13300xCD,0x79,0x7C,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFD,0x93,0xFF,0xEF,
13310xEA,0xFE,0xBF,0xEF,0x5B,0xD2,0xCD,0xF5, 0x6D,0x77,0xDF,0xF7,0xFD,0xFF,0x7F,0xDF,
13320xFF,0xFF,0x66,0xFF,0xD5,0x65,0x7D,0x5F, 0x75,0x9D,0x65,0x7F,0xD6,0xFB,0x4F,0xFF,
13330xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF6,0xC7, 0xFF,0xBF,0xEF,0xFA,0xFE,0xFF,0xBF,0xEB,
13340xFF,0xDF,0xFF,0x7E,0xFF,0xFF,0xEF,0xFD, 0x7E,0xD7,0xFF,0x78,0xDF,0xFF,0x5F,0xDF,
13350xF5,0xBF,0x7F,0xDF,0xC5,0xFF,0x3F,0xF6, 0x7E,0xFF,0x0F,0xEF,0xF2,0x3E,0xBF,0xFF,
13360xFB,0x3F,0xFF,0xFB,0x7F,0xFF,0xB3,0xFE, 0xFB,0xF6,0xFD,0xFF,0xDA,0xF7,0xFD,0xFF,
13370x7F,0xDF,0xF7,0xBF,0xFF,0xFA,0x7F,0xFF, 0xFF,0xFF,0xFF,0x9F,0xFF,0xF3,0xDC,0xF9,
13380xBF,0xCE,0xE7,0xF9,0xFE,0x7F,0x9F,0xE7, 0xFF,0xFF,0xE2,0x7F,0xFE,0xFF,0xBF,0xEF,
13390xEB,0xFA,0xFF,0x9F,0x67,0x1E,0xFF,0x8F, 0xE7,0xF8,0xFE,0x7F,0x8F,0xEF,0xFF,0xBD,
13400xBF,0xFF,0xFB,0xFF,0xFF,0xDF,0xF7,0xFF, 0xFC,0xFF,0xBF,0xFF,0xFF,0xFF,0xFF,0xFF,
13410xFF,0xFF,0xFF,0xFD,0xB3,0xFF,0xFF,0xEF, 0xFF,0xFF,0xBF,0xED,0xFF,0xFB,0xEE,0xFE,
13420xFF,0xFF,0xEF,0xFF,0xFE,0xFF,0xFF,0xFF, 0xFF,0xB5,0xFF,0xB7,0xFD,0xFD,0x6E,0xFF,
13430xFF,0xFE,0xFD,0x2F,0xD8,0xFE,0xBF,0x8F, 0xEB,0xF9,0xFE,0x3F,0xFF,0xFA,0xCF,0xFF,
13440xE7,0xD9,0xFA,0xBF,0xDF,0x77,0xFC,0xFB, 0x3F,0xAB,0xFE,0xFF,0xBF,0xEF,0xFB,0xFE,
13450xFF,0xFF,0xEE,0x1F,0xFF,0xDF,0xF7,0xFF, 0xFF,0xFF,0x5F,0x97,0x35,0xBF,0x5E,0xFE,
13460xBF,0xEF,0xFF,0xF7,0xFD,0xFF,0xFF,0xFA, 0xBF,0xFF,0xBE,0x6F,0x9F,0xE7,0xF8,0xBE,
13470x2F,0x8B,0x66,0x94,0x7D,0x9D,0xE7,0xF9, 0xFE,0x7F,0x9F,0xE7,0xF1,0x7F,0xFF,0xFF,
13480xFF,0xF7,0xF5,0xFD,0x7F,0x5F,0xFB,0xFD, 0x9E,0xFF,0xFB,0xFE,0xFF,0xFF,0xEF,0xFF,
13490xFF,0xA0,0xFF,0xFF,0xFF,0xBF,0xEF,0xEB, 0xFA,0xFE,0xBF,0xB7,0xF7,0xF7,0xFF,0xFF,
13500xFD,0xFF,0xFF,0xFF,0xFF,0xFF,0xDD,0xFF, 0xFD,0xFF,0xFF,0xFF,0xD7,0xFF,0xFF,0xFF,
13510x7F,0xF5,0xFF,0xFF,0xEF,0xFF,0xFF,0xFF, 0xBF,0xFF,0xFF,0xAB,0xFE,0xFB,0xFE,0xFF,
13520xF7,0xAF,0xFF,0xFF,0xDE,0xF7,0xEB,0x5F, 0xDF,0xF7,0xFD,0xFF,0x7F,0xDF,0xFF,0xFF,
13530xB3,0xFF,0xC9,0xFE,0xFF,0xFF,0xFF,0xFF, 0xD6,0xFF,0xFF,0xCB,0xFF,0xFF,0xDF,0xFF,
13540xFF,0xFF,0xFF,0xFF,0xFC,0x8F,0xFF,0xBA, 0xBE,0xBF,0xAF,0xEB,0x78,0xFE,0xB7,0xAD,
13550x3A,0xFE,0xB7,0xAF,0xEB,0x7A,0xFE,0xBF, 0xAF,0xFF,0x9F,0xFF,0xFF,0xDF,0xFC,0xFF,
13560xFF,0xFE,0xC3,0xFE,0xFF,0xFF,0x33,0xFC, 0xFF,0xBF,0xDF,0xF3,0xFF,0xFF,0xBB,0x9F,
13570xFF,0xFF,0xFF,0xEB,0xDF,0xFF,0xFF,0xAF, 0xF7,0x6F,0xF9,0xBF,0xEF,0xFD,0xFF,0xFF,
13580xFF,0xFF,0xFF,0xE3,0x7F,0xFF,0xFF,0xFF, 0xFB,0xFF,0xFF,0xBF,0xFD,0xFB,0xF7,0xFF,
13590xDF,0xF7,0xFF,0xFE,0xEF,0x5F,0xBD,0xFF, 0xFA,0xFF,0xF8,0xFF,0xBF,0xAF,0xFB,0xFE,
13600xFE,0x3F,0xEF,0xE8,0xFF,0xDF,0xF3,0xFD, 0xFF,0xFF,0xFF,0xFF,0xFF,0xED,0xFF,0xFB,
13610xFD,0xFF,0xAF,0xFF,0xFF,0xFE,0xFE,0xBF, 0xDB,0xFF,0xFF,0xFF,0xBF,0xFF,0xDF,0xFF,
13620xFD,0xFF,0xCB,0xFF,0xFF,0xFF,0xFF,0xFF, 0xBF,0x6F,0xFF,0x7F,0xB7,0xB3,0xFF,0xFF,
13630xDF,0xFF,0xFB,0xEF,0xFF,0xFF,0xFF,0x07, 0xFF,0xFB,0xFF,0xFF,0xFF,0xED,0xFF,0xF5,
13640x7C,0xFF,0x7F,0xFE,0xFF,0xFF,0xEF,0xCF, 0xFF,0xFB,0xFF,0xFF,0x2F,0xFF,0xFF,0xFF,
13650xFF,0xF3,0xFF,0xFB,0xFF,0xFE,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xBF,0xFF,0xFF,0xFF,
13660xFD,0x1B,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFE,0x7C,0xFF,0xFF,0xFF,0xFF,
13670xEF,0xFF,0xFF,0xFF,0xFF,0xFB,0xBF,0x7F, 0xFD,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
13680xDB,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFD, 0xFF,0xFF,0xF0,0x7F,0xFF,0xFF,0xFF,0xFF,
13690xFF,0xFF,0xFF,0xFF,0xFF,0xFB,0xFF,0xDF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFD,0xBF,0xFE,
13700x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xEF,0xFE,0xFF,0xBF,0xFF,0xFF,0xFF,
13710xFF,0xFF,0xEF,0xFA,0xB5,0xFF,0xFF,0xFF, 0xF7,0xF7,0xFF,0xFF,0xFF,0xFF,0xDF,0xFB,
13720xFC,0xFF,0xFF,0xFE,0xFF,0x7F,0xDF,0xBF, 0xFF,0xCB,0xBF,0xF9,0xFE,0x7F,0x9F,0xE7,
13730xF9,0xFE,0x7F,0x97,0xE1,0xFE,0x79,0x9F, 0xE7,0xFD,0xFE,0x7F,0xDF,0xFE,0x37,0xFF,
13740xFB,0xDE,0xDE,0xBD,0xEF,0xF3,0xFE,0xFB, 0xAF,0xEB,0xFE,0xFF,0xFF,0xCF,0xFF,0xFE,
13750xFF,0xBF,0xFF,0x8F,0xFF,0xEF,0xFB,0xFE, 0xFF,0xBF,0xE7,0xF9,0x5E,0x7F,0xEF,0xFB,
13760xDA,0xFF,0xBF,0xEF,0xFB,0xFE,0xFF,0xFD, 0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xDF,
13770xFF,0xFF,0x7F,0xFF,0xFF,0xF7,0xFB,0x7F, 0xFF,0xFF,0xFF,0xFF,0xFC,0x3F,0xFF,0xBF,
13780xEF,0xFB,0xFE,0xFF,0xBF,0xEF,0x7B,0x7F, 0xBF,0xEF,0xFB,0xFE,0xFF,0xB5,0xEF,0xFB,
13790xBF,0xFA,0x7F,0xFC,0xFF,0x3F,0xCF,0xF3, 0xFC,0xFF,0x3F,0xCF,0xBC,0xFF,0x3F,0xEF,
13800xF3,0xFC,0xFE,0x3F,0xCF,0xFF,0xEE,0xEF, 0xFB,0xFE,0xFF,0xBF,0xEF,0xFB,0x6A,0xD7,
13810xB7,0xFB,0xF8,0xFF,0xB7,0xEF,0xBA,0xFE, 0xFF,0xBF,0x7F,0xE9,0xFF,0xF9,0x7E,0x5F,
13820x97,0xE5,0xF9,0xFE,0x7F,0xBF,0xF9,0x7E, 0x5F,0x9F,0xE5,0xFB,0xFE,0x5F,0xB7,0xFF,
13830xA3,0xFF,0xF7,0xFD,0xFF,0x7F,0xDF,0xF7, 0xFD,0xFF,0x5E,0xF7,0x7D,0xFF,0x77,0xDF,
13840xF7,0xFD,0xFF,0x7F,0xFF,0xD7,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFD,0xDF,0xFB,0x7F,
13850xFF,0xFF,0xEF,0xFF,0xFE,0xFB,0xFF,0xFF, 0xBF,0xFE,0x8F,0xFF,0xDF,0xF7,0xFD,0xFD,
13860x7F,0xDF,0xF7,0xFD,0x3E,0xDF,0xF5,0xBD, 0xFF,0x7F,0xDF,0xF7,0xFD,0xF7,0xFF,0x9F,
13870xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFD,0xFF,0xBE,0xFF,0xFF,0xFF,0xFF,
13880xFF,0xFF,0xFF,0xFD,0x3F,0xFF,0xDF,0xF7, 0xFD,0xFF,0x7F,0xDF,0xF7,0xFD,0xFF,0xCF,
13890x77,0xFC,0xFF,0x5F,0xDF,0xF7,0xFD,0xFF, 0xF4,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
13900xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFD,0xFF,0xFF,0xFF,0xEE,0xFF,0xFF,
13910xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xED,0xFB,0xFF,0xFF,0xBF,0xFF,0xFF,0xFF,
13920xFF,0xFF,0xE9,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFB,0xFF,0xFF,0xFF,0xD3,0xFF,0xFF,
13930xBF,0x3F,0xFB,0xFF,0xFF,0xFF,0xFB,0xF3, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
13940xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xF7, 0xFF,0xFF,0xFF,0xFF,0x17,0xFF,0xFF,0xFF,
13950xDF,0xFF,0xFD,0xFF,0xFF,0xFF,0xFF,0xFF, 0xDF,0xDF,0xFF,0xFD,0xFF,0xFF,0xDF,0xF7,
13960xFF,0x4F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFD,
13970xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0x9F,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
13980xFD,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0xFF, 0xFF,0xFF,0x7A,0x3F,0xFF,0xFF,0xFF,0xFF,
13990xFF,0xFF,0xFF,0x7F,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF2,
14000x7F,0xFF,0xFB,0xFE,0xFF,0xBF,0xEF,0xF8, 0xFE,0xFF,0xBF,0xFB,0xFE,0xFF,0x8F,0xEC,
14010xFB,0xFE,0xFF,0xBF,0xF8,0xF7,0xFE,0xFF, 0xBF,0xEF,0xFB,0xFE,0xFD,0xBF,0xCF,0xEC,
14020xFF,0x3F,0xEF,0xDB,0xF8,0xFF,0xBF,0xCF, 0xFF,0xF9,0xFF,0xFF,0xBF,0xFF,0xFB,0xFF,
14030xFF,0xFF,0xEF,0xFB,0xDF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xBF,0xFF,0xFF,0xFF,0xBB,0xFF,
14040xEF,0xFB,0xFE,0xEF,0xBF,0xEE,0xEB,0xFB, 0xFE,0xFF,0xEF,0xFE,0xEE,0xBF,0xFE,0xEB,
14050xFF,0xEF,0xFF,0x17,0xFF,0x7E,0xEB,0xBB, 0xFE,0xBF,0xBE,0xFB,0xEF,0x5B,0xF7,0xBD,
14060xFB,0xCF,0xBF,0xBF,0xBB,0xFB,0x7E,0xCC, 0xEF,0xFF
1407
1408};
diff --git a/drivers/usb/media/dabusb.c b/drivers/usb/media/dabusb.c
deleted file mode 100644
index 18d8eaf408d5..000000000000
--- a/drivers/usb/media/dabusb.c
+++ /dev/null
@@ -1,874 +0,0 @@
1/*****************************************************************************/
2
3/*
4 * dabusb.c -- dab usb driver.
5 *
6 * Copyright (C) 1999 Deti Fliegl (deti@fliegl.de)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 *
23 *
24 * $Id: dabusb.c,v 1.54 2000/07/24 21:39:39 deti Exp $
25 *
26 */
27
28/*****************************************************************************/
29
30#include <linux/module.h>
31#include <linux/socket.h>
32#include <linux/list.h>
33#include <linux/vmalloc.h>
34#include <linux/slab.h>
35#include <linux/init.h>
36#include <asm/uaccess.h>
37#include <asm/atomic.h>
38#include <linux/delay.h>
39#include <linux/usb.h>
40#include <linux/smp_lock.h>
41
42#include "dabusb.h"
43#include "dabfirmware.h"
44
45/*
46 * Version Information
47 */
48#define DRIVER_VERSION "v1.54"
49#define DRIVER_AUTHOR "Deti Fliegl, deti@fliegl.de"
50#define DRIVER_DESC "DAB-USB Interface Driver for Linux (c)1999"
51
52/* --------------------------------------------------------------------- */
53
54#ifdef CONFIG_USB_DYNAMIC_MINORS
55#define NRDABUSB 256
56#else
57#define NRDABUSB 4
58#endif
59
60/*-------------------------------------------------------------------*/
61
62static dabusb_t dabusb[NRDABUSB];
63static int buffers = 256;
64static struct usb_driver dabusb_driver;
65
66/*-------------------------------------------------------------------*/
67
68static int dabusb_add_buf_tail (pdabusb_t s, struct list_head *dst, struct list_head *src)
69{
70 unsigned long flags;
71 struct list_head *tmp;
72 int ret = 0;
73
74 spin_lock_irqsave (&s->lock, flags);
75
76 if (list_empty (src)) {
77 // no elements in source buffer
78 ret = -1;
79 goto err;
80 }
81 tmp = src->next;
82 list_move_tail (tmp, dst);
83
84 err: spin_unlock_irqrestore (&s->lock, flags);
85 return ret;
86}
87/*-------------------------------------------------------------------*/
88#ifdef DEBUG
89static void dump_urb (struct urb *urb)
90{
91 dbg("urb :%p", urb);
92 dbg("dev :%p", urb->dev);
93 dbg("pipe :%08X", urb->pipe);
94 dbg("status :%d", urb->status);
95 dbg("transfer_flags :%08X", urb->transfer_flags);
96 dbg("transfer_buffer :%p", urb->transfer_buffer);
97 dbg("transfer_buffer_length:%d", urb->transfer_buffer_length);
98 dbg("actual_length :%d", urb->actual_length);
99 dbg("setup_packet :%p", urb->setup_packet);
100 dbg("start_frame :%d", urb->start_frame);
101 dbg("number_of_packets :%d", urb->number_of_packets);
102 dbg("interval :%d", urb->interval);
103 dbg("error_count :%d", urb->error_count);
104 dbg("context :%p", urb->context);
105 dbg("complete :%p", urb->complete);
106}
107#endif
108/*-------------------------------------------------------------------*/
109static int dabusb_cancel_queue (pdabusb_t s, struct list_head *q)
110{
111 unsigned long flags;
112 pbuff_t b;
113
114 dbg("dabusb_cancel_queue");
115
116 spin_lock_irqsave (&s->lock, flags);
117
118 list_for_each_entry(b, q, buff_list) {
119#ifdef DEBUG
120 dump_urb(b->purb);
121#endif
122 usb_unlink_urb (b->purb);
123 }
124 spin_unlock_irqrestore (&s->lock, flags);
125 return 0;
126}
127/*-------------------------------------------------------------------*/
128static int dabusb_free_queue (struct list_head *q)
129{
130 struct list_head *tmp;
131 struct list_head *p;
132 pbuff_t b;
133
134 dbg("dabusb_free_queue");
135 for (p = q->next; p != q;) {
136 b = list_entry (p, buff_t, buff_list);
137
138#ifdef DEBUG
139 dump_urb(b->purb);
140#endif
141 kfree(b->purb->transfer_buffer);
142 usb_free_urb(b->purb);
143 tmp = p->next;
144 list_del (p);
145 kfree (b);
146 p = tmp;
147 }
148
149 return 0;
150}
151/*-------------------------------------------------------------------*/
152static int dabusb_free_buffers (pdabusb_t s)
153{
154 unsigned long flags;
155 dbg("dabusb_free_buffers");
156
157 spin_lock_irqsave(&s->lock, flags);
158
159 dabusb_free_queue (&s->free_buff_list);
160 dabusb_free_queue (&s->rec_buff_list);
161
162 spin_unlock_irqrestore(&s->lock, flags);
163
164 s->got_mem = 0;
165 return 0;
166}
167/*-------------------------------------------------------------------*/
168static void dabusb_iso_complete (struct urb *purb, struct pt_regs *regs)
169{
170 pbuff_t b = purb->context;
171 pdabusb_t s = b->s;
172 int i;
173 int len;
174 int dst = 0;
175 void *buf = purb->transfer_buffer;
176
177 dbg("dabusb_iso_complete");
178
179 // process if URB was not killed
180 if (purb->status != -ENOENT) {
181 unsigned int pipe = usb_rcvisocpipe (purb->dev, _DABUSB_ISOPIPE);
182 int pipesize = usb_maxpacket (purb->dev, pipe, usb_pipeout (pipe));
183 for (i = 0; i < purb->number_of_packets; i++)
184 if (!purb->iso_frame_desc[i].status) {
185 len = purb->iso_frame_desc[i].actual_length;
186 if (len <= pipesize) {
187 memcpy (buf + dst, buf + purb->iso_frame_desc[i].offset, len);
188 dst += len;
189 }
190 else
191 err("dabusb_iso_complete: invalid len %d", len);
192 }
193 else
194 warn("dabusb_iso_complete: corrupted packet status: %d", purb->iso_frame_desc[i].status);
195 if (dst != purb->actual_length)
196 err("dst!=purb->actual_length:%d!=%d", dst, purb->actual_length);
197 }
198
199 if (atomic_dec_and_test (&s->pending_io) && !s->remove_pending && s->state != _stopped) {
200 s->overruns++;
201 err("overrun (%d)", s->overruns);
202 }
203 wake_up (&s->wait);
204}
205/*-------------------------------------------------------------------*/
206static int dabusb_alloc_buffers (pdabusb_t s)
207{
208 int buffers = 0;
209 pbuff_t b;
210 unsigned int pipe = usb_rcvisocpipe (s->usbdev, _DABUSB_ISOPIPE);
211 int pipesize = usb_maxpacket (s->usbdev, pipe, usb_pipeout (pipe));
212 int packets = _ISOPIPESIZE / pipesize;
213 int transfer_buffer_length = packets * pipesize;
214 int i;
215
216 dbg("dabusb_alloc_buffers pipesize:%d packets:%d transfer_buffer_len:%d",
217 pipesize, packets, transfer_buffer_length);
218
219 while (buffers < (s->total_buffer_size << 10)) {
220 b = (pbuff_t) kmalloc (sizeof (buff_t), GFP_KERNEL);
221 if (!b) {
222 err("kmalloc(sizeof(buff_t))==NULL");
223 goto err;
224 }
225 memset (b, 0, sizeof (buff_t));
226 b->s = s;
227 b->purb = usb_alloc_urb(packets, GFP_KERNEL);
228 if (!b->purb) {
229 err("usb_alloc_urb == NULL");
230 kfree (b);
231 goto err;
232 }
233
234 b->purb->transfer_buffer = kmalloc (transfer_buffer_length, GFP_KERNEL);
235 if (!b->purb->transfer_buffer) {
236 kfree (b->purb);
237 kfree (b);
238 err("kmalloc(%d)==NULL", transfer_buffer_length);
239 goto err;
240 }
241
242 b->purb->transfer_buffer_length = transfer_buffer_length;
243 b->purb->number_of_packets = packets;
244 b->purb->complete = dabusb_iso_complete;
245 b->purb->context = b;
246 b->purb->dev = s->usbdev;
247 b->purb->pipe = pipe;
248 b->purb->transfer_flags = URB_ISO_ASAP;
249
250 for (i = 0; i < packets; i++) {
251 b->purb->iso_frame_desc[i].offset = i * pipesize;
252 b->purb->iso_frame_desc[i].length = pipesize;
253 }
254
255 buffers += transfer_buffer_length;
256 list_add_tail (&b->buff_list, &s->free_buff_list);
257 }
258 s->got_mem = buffers;
259
260 return 0;
261
262 err:
263 dabusb_free_buffers (s);
264 return -ENOMEM;
265}
266/*-------------------------------------------------------------------*/
267static int dabusb_bulk (pdabusb_t s, pbulk_transfer_t pb)
268{
269 int ret;
270 unsigned int pipe;
271 int actual_length;
272
273 dbg("dabusb_bulk");
274
275 if (!pb->pipe)
276 pipe = usb_rcvbulkpipe (s->usbdev, 2);
277 else
278 pipe = usb_sndbulkpipe (s->usbdev, 2);
279
280 ret=usb_bulk_msg(s->usbdev, pipe, pb->data, pb->size, &actual_length, 100);
281 if(ret<0) {
282 err("dabusb: usb_bulk_msg failed(%d)",ret);
283
284 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
285 err("set_interface failed");
286 return -EINVAL;
287 }
288
289 }
290
291 if( ret == -EPIPE ) {
292 warn("CLEAR_FEATURE request to remove STALL condition.");
293 if(usb_clear_halt(s->usbdev, usb_pipeendpoint(pipe)))
294 err("request failed");
295 }
296
297 pb->size = actual_length;
298 return ret;
299}
300/* --------------------------------------------------------------------- */
301static int dabusb_writemem (pdabusb_t s, int pos, unsigned char *data, int len)
302{
303 int ret;
304 unsigned char *transfer_buffer = kmalloc (len, GFP_KERNEL);
305
306 if (!transfer_buffer) {
307 err("dabusb_writemem: kmalloc(%d) failed.", len);
308 return -ENOMEM;
309 }
310
311 memcpy (transfer_buffer, data, len);
312
313 ret=usb_control_msg(s->usbdev, usb_sndctrlpipe( s->usbdev, 0 ), 0xa0, 0x40, pos, 0, transfer_buffer, len, 300);
314
315 kfree (transfer_buffer);
316 return ret;
317}
318/* --------------------------------------------------------------------- */
319static int dabusb_8051_reset (pdabusb_t s, unsigned char reset_bit)
320{
321 dbg("dabusb_8051_reset: %d",reset_bit);
322 return dabusb_writemem (s, CPUCS_REG, &reset_bit, 1);
323}
324/* --------------------------------------------------------------------- */
325static int dabusb_loadmem (pdabusb_t s, const char *fname)
326{
327 int ret;
328 PINTEL_HEX_RECORD ptr = firmware;
329
330 dbg("Enter dabusb_loadmem (internal)");
331
332 ret = dabusb_8051_reset (s, 1);
333 while (ptr->Type == 0) {
334
335 dbg("dabusb_writemem: %04X %p %d)", ptr->Address, ptr->Data, ptr->Length);
336
337 ret = dabusb_writemem (s, ptr->Address, ptr->Data, ptr->Length);
338 if (ret < 0) {
339 err("dabusb_writemem failed (%d %04X %p %d)", ret, ptr->Address, ptr->Data, ptr->Length);
340 break;
341 }
342 ptr++;
343 }
344 ret = dabusb_8051_reset (s, 0);
345
346 dbg("dabusb_loadmem: exit");
347
348 return ret;
349}
350/* --------------------------------------------------------------------- */
351static int dabusb_fpga_clear (pdabusb_t s, pbulk_transfer_t b)
352{
353 b->size = 4;
354 b->data[0] = 0x2a;
355 b->data[1] = 0;
356 b->data[2] = 0;
357 b->data[3] = 0;
358
359 dbg("dabusb_fpga_clear");
360
361 return dabusb_bulk (s, b);
362}
363/* --------------------------------------------------------------------- */
364static int dabusb_fpga_init (pdabusb_t s, pbulk_transfer_t b)
365{
366 b->size = 4;
367 b->data[0] = 0x2c;
368 b->data[1] = 0;
369 b->data[2] = 0;
370 b->data[3] = 0;
371
372 dbg("dabusb_fpga_init");
373
374 return dabusb_bulk (s, b);
375}
376/* --------------------------------------------------------------------- */
377static int dabusb_fpga_download (pdabusb_t s, const char *fname)
378{
379 pbulk_transfer_t b = kmalloc (sizeof (bulk_transfer_t), GFP_KERNEL);
380 unsigned int blen, n;
381 int ret;
382 unsigned char *buf = bitstream;
383
384 dbg("Enter dabusb_fpga_download (internal)");
385
386 if (!b) {
387 err("kmalloc(sizeof(bulk_transfer_t))==NULL");
388 return -ENOMEM;
389 }
390
391 b->pipe = 1;
392 ret = dabusb_fpga_clear (s, b);
393 mdelay (10);
394 blen = buf[73] + (buf[72] << 8);
395
396 dbg("Bitstream len: %i", blen);
397
398 b->data[0] = 0x2b;
399 b->data[1] = 0;
400 b->data[2] = 0;
401 b->data[3] = 60;
402
403 for (n = 0; n <= blen + 60; n += 60) {
404 // some cclks for startup
405 b->size = 64;
406 memcpy (b->data + 4, buf + 74 + n, 60);
407 ret = dabusb_bulk (s, b);
408 if (ret < 0) {
409 err("dabusb_bulk failed.");
410 break;
411 }
412 mdelay (1);
413 }
414
415 ret = dabusb_fpga_init (s, b);
416 kfree (b);
417
418 dbg("exit dabusb_fpga_download");
419
420 return ret;
421}
422
423static int dabusb_stop (pdabusb_t s)
424{
425 dbg("dabusb_stop");
426
427 s->state = _stopped;
428 dabusb_cancel_queue (s, &s->rec_buff_list);
429
430 dbg("pending_io: %d", s->pending_io.counter);
431
432 s->pending_io.counter = 0;
433 return 0;
434}
435
436static int dabusb_startrek (pdabusb_t s)
437{
438 if (!s->got_mem && s->state != _started) {
439
440 dbg("dabusb_startrek");
441
442 if (dabusb_alloc_buffers (s) < 0)
443 return -ENOMEM;
444 dabusb_stop (s);
445 s->state = _started;
446 s->readptr = 0;
447 }
448
449 if (!list_empty (&s->free_buff_list)) {
450 pbuff_t end;
451 int ret;
452
453 while (!dabusb_add_buf_tail (s, &s->rec_buff_list, &s->free_buff_list)) {
454
455 dbg("submitting: end:%p s->rec_buff_list:%p", s->rec_buff_list.prev, &s->rec_buff_list);
456
457 end = list_entry (s->rec_buff_list.prev, buff_t, buff_list);
458
459 ret = usb_submit_urb (end->purb, GFP_KERNEL);
460 if (ret) {
461 err("usb_submit_urb returned:%d", ret);
462 if (dabusb_add_buf_tail (s, &s->free_buff_list, &s->rec_buff_list))
463 err("startrek: dabusb_add_buf_tail failed");
464 break;
465 }
466 else
467 atomic_inc (&s->pending_io);
468 }
469 dbg("pending_io: %d",s->pending_io.counter);
470 }
471
472 return 0;
473}
474
475static ssize_t dabusb_read (struct file *file, char __user *buf, size_t count, loff_t * ppos)
476{
477 pdabusb_t s = (pdabusb_t) file->private_data;
478 unsigned long flags;
479 unsigned ret = 0;
480 int rem;
481 int cnt;
482 pbuff_t b;
483 struct urb *purb = NULL;
484
485 dbg("dabusb_read");
486
487 if (*ppos)
488 return -ESPIPE;
489
490 if (s->remove_pending)
491 return -EIO;
492
493
494 if (!s->usbdev)
495 return -EIO;
496
497 while (count > 0) {
498 dabusb_startrek (s);
499
500 spin_lock_irqsave (&s->lock, flags);
501
502 if (list_empty (&s->rec_buff_list)) {
503
504 spin_unlock_irqrestore(&s->lock, flags);
505
506 err("error: rec_buf_list is empty");
507 goto err;
508 }
509
510 b = list_entry (s->rec_buff_list.next, buff_t, buff_list);
511 purb = b->purb;
512
513 spin_unlock_irqrestore(&s->lock, flags);
514
515 if (purb->status == -EINPROGRESS) {
516 if (file->f_flags & O_NONBLOCK) // return nonblocking
517 {
518 if (!ret)
519 ret = -EAGAIN;
520 goto err;
521 }
522
523 interruptible_sleep_on (&s->wait);
524
525 if (signal_pending (current)) {
526 if (!ret)
527 ret = -ERESTARTSYS;
528 goto err;
529 }
530
531 spin_lock_irqsave (&s->lock, flags);
532
533 if (list_empty (&s->rec_buff_list)) {
534 spin_unlock_irqrestore(&s->lock, flags);
535 err("error: still no buffer available.");
536 goto err;
537 }
538 spin_unlock_irqrestore(&s->lock, flags);
539 s->readptr = 0;
540 }
541 if (s->remove_pending) {
542 ret = -EIO;
543 goto err;
544 }
545
546 rem = purb->actual_length - s->readptr; // set remaining bytes to copy
547
548 if (count >= rem)
549 cnt = rem;
550 else
551 cnt = count;
552
553 dbg("copy_to_user:%p %p %d",buf, purb->transfer_buffer + s->readptr, cnt);
554
555 if (copy_to_user (buf, purb->transfer_buffer + s->readptr, cnt)) {
556 err("read: copy_to_user failed");
557 if (!ret)
558 ret = -EFAULT;
559 goto err;
560 }
561
562 s->readptr += cnt;
563 count -= cnt;
564 buf += cnt;
565 ret += cnt;
566
567 if (s->readptr == purb->actual_length) {
568 // finished, take next buffer
569 if (dabusb_add_buf_tail (s, &s->free_buff_list, &s->rec_buff_list))
570 err("read: dabusb_add_buf_tail failed");
571 s->readptr = 0;
572 }
573 }
574 err: //up(&s->mutex);
575 return ret;
576}
577
578static int dabusb_open (struct inode *inode, struct file *file)
579{
580 int devnum = iminor(inode);
581 pdabusb_t s;
582
583 if (devnum < DABUSB_MINOR || devnum >= (DABUSB_MINOR + NRDABUSB))
584 return -EIO;
585
586 s = &dabusb[devnum - DABUSB_MINOR];
587
588 dbg("dabusb_open");
589 down (&s->mutex);
590
591 while (!s->usbdev || s->opened) {
592 up (&s->mutex);
593
594 if (file->f_flags & O_NONBLOCK) {
595 return -EBUSY;
596 }
597 msleep_interruptible(500);
598
599 if (signal_pending (current)) {
600 return -EAGAIN;
601 }
602 down (&s->mutex);
603 }
604 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
605 up(&s->mutex);
606 err("set_interface failed");
607 return -EINVAL;
608 }
609 s->opened = 1;
610 up (&s->mutex);
611
612 file->f_pos = 0;
613 file->private_data = s;
614
615 return nonseekable_open(inode, file);
616}
617
618static int dabusb_release (struct inode *inode, struct file *file)
619{
620 pdabusb_t s = (pdabusb_t) file->private_data;
621
622 dbg("dabusb_release");
623
624 down (&s->mutex);
625 dabusb_stop (s);
626 dabusb_free_buffers (s);
627 up (&s->mutex);
628
629 if (!s->remove_pending) {
630 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0)
631 err("set_interface failed");
632 }
633 else
634 wake_up (&s->remove_ok);
635
636 s->opened = 0;
637 return 0;
638}
639
640static int dabusb_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
641{
642 pdabusb_t s = (pdabusb_t) file->private_data;
643 pbulk_transfer_t pbulk;
644 int ret = 0;
645 int version = DABUSB_VERSION;
646
647 dbg("dabusb_ioctl");
648
649 if (s->remove_pending)
650 return -EIO;
651
652 down (&s->mutex);
653
654 if (!s->usbdev) {
655 up (&s->mutex);
656 return -EIO;
657 }
658
659 switch (cmd) {
660
661 case IOCTL_DAB_BULK:
662 pbulk = (pbulk_transfer_t) kmalloc (sizeof (bulk_transfer_t), GFP_KERNEL);
663
664 if (!pbulk) {
665 ret = -ENOMEM;
666 break;
667 }
668
669 if (copy_from_user (pbulk, (void __user *) arg, sizeof (bulk_transfer_t))) {
670 ret = -EFAULT;
671 kfree (pbulk);
672 break;
673 }
674
675 ret=dabusb_bulk (s, pbulk);
676 if(ret==0)
677 if (copy_to_user((void __user *)arg, pbulk,
678 sizeof(bulk_transfer_t)))
679 ret = -EFAULT;
680 kfree (pbulk);
681 break;
682
683 case IOCTL_DAB_OVERRUNS:
684 ret = put_user (s->overruns, (unsigned int __user *) arg);
685 break;
686
687 case IOCTL_DAB_VERSION:
688 ret = put_user (version, (unsigned int __user *) arg);
689 break;
690
691 default:
692 ret = -ENOIOCTLCMD;
693 break;
694 }
695 up (&s->mutex);
696 return ret;
697}
698
699static struct file_operations dabusb_fops =
700{
701 .owner = THIS_MODULE,
702 .llseek = no_llseek,
703 .read = dabusb_read,
704 .ioctl = dabusb_ioctl,
705 .open = dabusb_open,
706 .release = dabusb_release,
707};
708
709static struct usb_class_driver dabusb_class = {
710 .name = "dabusb%d",
711 .fops = &dabusb_fops,
712 .minor_base = DABUSB_MINOR,
713};
714
715
716/* --------------------------------------------------------------------- */
717static int dabusb_probe (struct usb_interface *intf,
718 const struct usb_device_id *id)
719{
720 struct usb_device *usbdev = interface_to_usbdev(intf);
721 int retval;
722 pdabusb_t s;
723
724 dbg("dabusb: probe: vendor id 0x%x, device id 0x%x ifnum:%d",
725 le16_to_cpu(usbdev->descriptor.idVendor),
726 le16_to_cpu(usbdev->descriptor.idProduct),
727 intf->altsetting->desc.bInterfaceNumber);
728
729 /* We don't handle multiple configurations */
730 if (usbdev->descriptor.bNumConfigurations != 1)
731 return -ENODEV;
732
733 if (intf->altsetting->desc.bInterfaceNumber != _DABUSB_IF &&
734 le16_to_cpu(usbdev->descriptor.idProduct) == 0x9999)
735 return -ENODEV;
736
737
738
739 s = &dabusb[intf->minor];
740
741 down (&s->mutex);
742 s->remove_pending = 0;
743 s->usbdev = usbdev;
744 s->devnum = intf->minor;
745
746 if (usb_reset_configuration (usbdev) < 0) {
747 err("reset_configuration failed");
748 goto reject;
749 }
750 if (le16_to_cpu(usbdev->descriptor.idProduct) == 0x2131) {
751 dabusb_loadmem (s, NULL);
752 goto reject;
753 }
754 else {
755 dabusb_fpga_download (s, NULL);
756
757 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0) {
758 err("set_interface failed");
759 goto reject;
760 }
761 }
762 dbg("bound to interface: %d", intf->altsetting->desc.bInterfaceNumber);
763 usb_set_intfdata (intf, s);
764 up (&s->mutex);
765
766 retval = usb_register_dev(intf, &dabusb_class);
767 if (retval) {
768 usb_set_intfdata (intf, NULL);
769 return -ENOMEM;
770 }
771
772 return 0;
773
774 reject:
775 up (&s->mutex);
776 s->usbdev = NULL;
777 return -ENODEV;
778}
779
780static void dabusb_disconnect (struct usb_interface *intf)
781{
782 wait_queue_t __wait;
783 pdabusb_t s = usb_get_intfdata (intf);
784
785 dbg("dabusb_disconnect");
786
787 init_waitqueue_entry(&__wait, current);
788
789 usb_set_intfdata (intf, NULL);
790 if (s) {
791 usb_deregister_dev (intf, &dabusb_class);
792 s->remove_pending = 1;
793 wake_up (&s->wait);
794 add_wait_queue(&s->remove_ok, &__wait);
795 set_current_state(TASK_UNINTERRUPTIBLE);
796 if (s->state == _started)
797 schedule();
798 current->state = TASK_RUNNING;
799 remove_wait_queue(&s->remove_ok, &__wait);
800
801 s->usbdev = NULL;
802 s->overruns = 0;
803 }
804}
805
806static struct usb_device_id dabusb_ids [] = {
807 // { USB_DEVICE(0x0547, 0x2131) }, /* An2131 chip, no boot ROM */
808 { USB_DEVICE(0x0547, 0x9999) },
809 { } /* Terminating entry */
810};
811
812MODULE_DEVICE_TABLE (usb, dabusb_ids);
813
814static struct usb_driver dabusb_driver = {
815 .name = "dabusb",
816 .probe = dabusb_probe,
817 .disconnect = dabusb_disconnect,
818 .id_table = dabusb_ids,
819};
820
821/* --------------------------------------------------------------------- */
822
823static int __init dabusb_init (void)
824{
825 int retval;
826 unsigned u;
827
828 /* initialize struct */
829 for (u = 0; u < NRDABUSB; u++) {
830 pdabusb_t s = &dabusb[u];
831 memset (s, 0, sizeof (dabusb_t));
832 init_MUTEX (&s->mutex);
833 s->usbdev = NULL;
834 s->total_buffer_size = buffers;
835 init_waitqueue_head (&s->wait);
836 init_waitqueue_head (&s->remove_ok);
837 spin_lock_init (&s->lock);
838 INIT_LIST_HEAD (&s->free_buff_list);
839 INIT_LIST_HEAD (&s->rec_buff_list);
840 }
841
842 /* register misc device */
843 retval = usb_register(&dabusb_driver);
844 if (retval)
845 goto out;
846
847 dbg("dabusb_init: driver registered");
848
849 info(DRIVER_VERSION ":" DRIVER_DESC);
850
851out:
852 return retval;
853}
854
855static void __exit dabusb_cleanup (void)
856{
857 dbg("dabusb_cleanup");
858
859 usb_deregister (&dabusb_driver);
860}
861
862/* --------------------------------------------------------------------- */
863
864MODULE_AUTHOR( DRIVER_AUTHOR );
865MODULE_DESCRIPTION( DRIVER_DESC );
866MODULE_LICENSE("GPL");
867
868module_param(buffers, int, 0);
869MODULE_PARM_DESC (buffers, "Number of buffers (default=256)");
870
871module_init (dabusb_init);
872module_exit (dabusb_cleanup);
873
874/* --------------------------------------------------------------------- */
diff --git a/drivers/usb/media/dabusb.h b/drivers/usb/media/dabusb.h
deleted file mode 100644
index 10b666e43abc..000000000000
--- a/drivers/usb/media/dabusb.h
+++ /dev/null
@@ -1,85 +0,0 @@
1#define _BULK_DATA_LEN 64
2typedef struct
3{
4 unsigned char data[_BULK_DATA_LEN];
5 unsigned int size;
6 unsigned int pipe;
7}bulk_transfer_t,*pbulk_transfer_t;
8
9#define DABUSB_MINOR 240 /* some unassigned USB minor */
10#define DABUSB_VERSION 0x1000
11#define IOCTL_DAB_BULK _IOWR('d', 0x30, bulk_transfer_t)
12#define IOCTL_DAB_OVERRUNS _IOR('d', 0x15, int)
13#define IOCTL_DAB_VERSION _IOR('d', 0x3f, int)
14
15#ifdef __KERNEL__
16
17typedef enum { _stopped=0, _started } driver_state_t;
18
19typedef struct
20{
21 struct semaphore mutex;
22 struct usb_device *usbdev;
23 wait_queue_head_t wait;
24 wait_queue_head_t remove_ok;
25 spinlock_t lock;
26 atomic_t pending_io;
27 driver_state_t state;
28 int remove_pending;
29 int got_mem;
30 int total_buffer_size;
31 unsigned int overruns;
32 int readptr;
33 int opened;
34 int devnum;
35 struct list_head free_buff_list;
36 struct list_head rec_buff_list;
37} dabusb_t,*pdabusb_t;
38
39typedef struct
40{
41 pdabusb_t s;
42 struct urb *purb;
43 struct list_head buff_list;
44} buff_t,*pbuff_t;
45
46typedef struct
47{
48 wait_queue_head_t wait;
49} bulk_completion_context_t, *pbulk_completion_context_t;
50
51
52#define _DABUSB_IF 2
53#define _DABUSB_ISOPIPE 0x09
54#define _ISOPIPESIZE 16384
55
56#define _BULK_DATA_LEN 64
57// Vendor specific request code for Anchor Upload/Download
58// This one is implemented in the core
59#define ANCHOR_LOAD_INTERNAL 0xA0
60
61// EZ-USB Control and Status Register. Bit 0 controls 8051 reset
62#define CPUCS_REG 0x7F92
63#define _TOTAL_BUFFERS 384
64
65#define MAX_INTEL_HEX_RECORD_LENGTH 16
66
67#ifndef _BYTE_DEFINED
68#define _BYTE_DEFINED
69typedef unsigned char BYTE;
70#endif // !_BYTE_DEFINED
71
72#ifndef _WORD_DEFINED
73#define _WORD_DEFINED
74typedef unsigned short WORD;
75#endif // !_WORD_DEFINED
76
77typedef struct _INTEL_HEX_RECORD
78{
79 BYTE Length;
80 WORD Address;
81 BYTE Type;
82 BYTE Data[MAX_INTEL_HEX_RECORD_LENGTH];
83} INTEL_HEX_RECORD, *PINTEL_HEX_RECORD;
84
85#endif
diff --git a/drivers/usb/media/dsbr100.c b/drivers/usb/media/dsbr100.c
deleted file mode 100644
index 25646804d5be..000000000000
--- a/drivers/usb/media/dsbr100.c
+++ /dev/null
@@ -1,429 +0,0 @@
1/* A driver for the D-Link DSB-R100 USB radio. The R100 plugs
2 into both the USB and an analog audio input, so this thing
3 only deals with initialisation and frequency setting, the
4 audio data has to be handled by a sound driver.
5
6 Major issue: I can't find out where the device reports the signal
7 strength, and indeed the windows software appearantly just looks
8 at the stereo indicator as well. So, scanning will only find
9 stereo stations. Sad, but I can't help it.
10
11 Also, the windows program sends oodles of messages over to the
12 device, and I couldn't figure out their meaning. My suspicion
13 is that they don't have any:-)
14
15 You might find some interesting stuff about this module at
16 http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr
17
18 Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
19
20 This program is free software; you can redistribute it and/or modify
21 it under the terms of the GNU General Public License as published by
22 the Free Software Foundation; either version 2 of the License, or
23 (at your option) any later version.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33
34 History:
35
36 Version 0.40:
37 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
38
39 Version 0.30:
40 Markus: Updates for 2.5.x kernel and more ISO compliant source
41
42 Version 0.25:
43 PSL and Markus: Cleanup, radio now doesn't stop on device close
44
45 Version 0.24:
46 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
47 right. Some minor cleanup, improved standalone compilation
48
49 Version 0.23:
50 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
51
52 Version 0.22:
53 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
54 thanks to Mike Cox for pointing the problem out.
55
56 Version 0.21:
57 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
58 to adhere to Documentation/CodingStyle
59
60 Version 0.2:
61 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
62 Markus: Copyright clarification
63
64 Version 0.01: Markus: initial release
65
66*/
67
68
69#include <linux/kernel.h>
70#include <linux/module.h>
71#include <linux/init.h>
72#include <linux/slab.h>
73#include <linux/input.h>
74#include <linux/videodev.h>
75#include <linux/usb.h>
76#include <linux/smp_lock.h>
77
78/*
79 * Version Information
80 */
81#define DRIVER_VERSION "v0.40"
82#define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
83#define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
84
85#define DSB100_VENDOR 0x04b4
86#define DSB100_PRODUCT 0x1002
87
88/* Commands the device appears to understand */
89#define DSB100_TUNE 1
90#define DSB100_ONOFF 2
91
92#define TB_LEN 16
93
94/* Frequency limits in MHz -- these are European values. For Japanese
95devices, that would be 76 and 91. */
96#define FREQ_MIN 87.5
97#define FREQ_MAX 108.0
98#define FREQ_MUL 16000
99
100
101static int usb_dsbr100_probe(struct usb_interface *intf,
102 const struct usb_device_id *id);
103static void usb_dsbr100_disconnect(struct usb_interface *intf);
104static int usb_dsbr100_ioctl(struct inode *inode, struct file *file,
105 unsigned int cmd, unsigned long arg);
106static int usb_dsbr100_open(struct inode *inode, struct file *file);
107static int usb_dsbr100_close(struct inode *inode, struct file *file);
108
109static int radio_nr = -1;
110module_param(radio_nr, int, 0);
111
112/* Data for one (physical) device */
113typedef struct {
114 struct usb_device *usbdev;
115 struct video_device *videodev;
116 unsigned char transfer_buffer[TB_LEN];
117 int curfreq;
118 int stereo;
119 int users;
120 int removed;
121} dsbr100_device;
122
123
124/* File system interface */
125static struct file_operations usb_dsbr100_fops = {
126 .owner = THIS_MODULE,
127 .open = usb_dsbr100_open,
128 .release = usb_dsbr100_close,
129 .ioctl = usb_dsbr100_ioctl,
130 .compat_ioctl = v4l_compat_ioctl32,
131 .llseek = no_llseek,
132};
133
134/* V4L interface */
135static struct video_device dsbr100_videodev_template=
136{
137 .owner = THIS_MODULE,
138 .name = "D-Link DSB-R 100",
139 .type = VID_TYPE_TUNER,
140 .hardware = VID_HARDWARE_AZTECH,
141 .fops = &usb_dsbr100_fops,
142 .release = video_device_release,
143};
144
145static struct usb_device_id usb_dsbr100_device_table [] = {
146 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
147 { } /* Terminating entry */
148};
149
150MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
151
152/* USB subsystem interface */
153static struct usb_driver usb_dsbr100_driver = {
154 .name = "dsbr100",
155 .probe = usb_dsbr100_probe,
156 .disconnect = usb_dsbr100_disconnect,
157 .id_table = usb_dsbr100_device_table,
158};
159
160/* Low-level device interface begins here */
161
162/* switch on radio */
163static int dsbr100_start(dsbr100_device *radio)
164{
165 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
166 USB_REQ_GET_STATUS,
167 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
168 0x00, 0xC7, radio->transfer_buffer, 8, 300)<0 ||
169 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
170 DSB100_ONOFF,
171 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
172 0x01, 0x00, radio->transfer_buffer, 8, 300)<0)
173 return -1;
174 return (radio->transfer_buffer)[0];
175}
176
177
178/* switch off radio */
179static int dsbr100_stop(dsbr100_device *radio)
180{
181 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
182 USB_REQ_GET_STATUS,
183 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
184 0x16, 0x1C, radio->transfer_buffer, 8, 300)<0 ||
185 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
186 DSB100_ONOFF,
187 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
188 0x00, 0x00, radio->transfer_buffer, 8, 300)<0)
189 return -1;
190 return (radio->transfer_buffer)[0];
191}
192
193/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
194static int dsbr100_setfreq(dsbr100_device *radio, int freq)
195{
196 freq = (freq/16*80)/1000+856;
197 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
198 DSB100_TUNE,
199 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
200 (freq>>8)&0x00ff, freq&0xff,
201 radio->transfer_buffer, 8, 300)<0 ||
202 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
203 USB_REQ_GET_STATUS,
204 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
205 0x96, 0xB7, radio->transfer_buffer, 8, 300)<0 ||
206 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
207 USB_REQ_GET_STATUS,
208 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
209 0x00, 0x24, radio->transfer_buffer, 8, 300)<0) {
210 radio->stereo = -1;
211 return -1;
212 }
213 radio->stereo = ! ((radio->transfer_buffer)[0]&0x01);
214 return (radio->transfer_buffer)[0];
215}
216
217/* return the device status. This is, in effect, just whether it
218sees a stereo signal or not. Pity. */
219static void dsbr100_getstat(dsbr100_device *radio)
220{
221 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
222 USB_REQ_GET_STATUS,
223 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
224 0x00 , 0x24, radio->transfer_buffer, 8, 300)<0)
225 radio->stereo = -1;
226 else
227 radio->stereo = ! (radio->transfer_buffer[0]&0x01);
228}
229
230
231/* USB subsystem interface begins here */
232
233/* check if the device is present and register with v4l and
234usb if it is */
235static int usb_dsbr100_probe(struct usb_interface *intf,
236 const struct usb_device_id *id)
237{
238 dsbr100_device *radio;
239
240 if (!(radio = kmalloc(sizeof(dsbr100_device), GFP_KERNEL)))
241 return -ENOMEM;
242 if (!(radio->videodev = video_device_alloc())) {
243 kfree(radio);
244 return -ENOMEM;
245 }
246 memcpy(radio->videodev, &dsbr100_videodev_template,
247 sizeof(dsbr100_videodev_template));
248 radio->removed = 0;
249 radio->users = 0;
250 radio->usbdev = interface_to_usbdev(intf);
251 radio->curfreq = FREQ_MIN*FREQ_MUL;
252 video_set_drvdata(radio->videodev, radio);
253 if (video_register_device(radio->videodev, VFL_TYPE_RADIO,
254 radio_nr)) {
255 warn("Could not register video device");
256 video_device_release(radio->videodev);
257 kfree(radio);
258 return -EIO;
259 }
260 usb_set_intfdata(intf, radio);
261 return 0;
262}
263
264/* handle unplugging of the device, release data structures
265if nothing keeps us from doing it. If something is still
266keeping us busy, the release callback of v4l will take care
267of releasing it. stv680.c does not relase its private
268data, so I don't do this here either. Checking out the
269code I'd expect I better did that, but if there's a memory
270leak here it's tiny (~50 bytes per disconnect) */
271static void usb_dsbr100_disconnect(struct usb_interface *intf)
272{
273 dsbr100_device *radio = usb_get_intfdata(intf);
274
275 usb_set_intfdata (intf, NULL);
276 if (radio) {
277 video_unregister_device(radio->videodev);
278 radio->videodev = NULL;
279 if (radio->users) {
280 kfree(radio);
281 } else {
282 radio->removed = 1;
283 }
284 }
285}
286
287
288/* Video for Linux interface */
289
290static int usb_dsbr100_do_ioctl(struct inode *inode, struct file *file,
291 unsigned int cmd, void *arg)
292{
293 dsbr100_device *radio=video_get_drvdata(video_devdata(file));
294
295 if (!radio)
296 return -EIO;
297
298 switch(cmd) {
299 case VIDIOCGCAP: {
300 struct video_capability *v = arg;
301
302 memset(v, 0, sizeof(*v));
303 v->type = VID_TYPE_TUNER;
304 v->channels = 1;
305 v->audios = 1;
306 strcpy(v->name, "D-Link R-100 USB FM Radio");
307 return 0;
308 }
309 case VIDIOCGTUNER: {
310 struct video_tuner *v = arg;
311
312 dsbr100_getstat(radio);
313 if(v->tuner) /* Only 1 tuner */
314 return -EINVAL;
315 v->rangelow = FREQ_MIN*FREQ_MUL;
316 v->rangehigh = FREQ_MAX*FREQ_MUL;
317 v->flags = VIDEO_TUNER_LOW;
318 v->mode = VIDEO_MODE_AUTO;
319 v->signal = radio->stereo*0x7000;
320 /* Don't know how to get signal strength */
321 v->flags |= VIDEO_TUNER_STEREO_ON*radio->stereo;
322 strcpy(v->name, "DSB R-100");
323 return 0;
324 }
325 case VIDIOCSTUNER: {
326 struct video_tuner *v = arg;
327
328 if(v->tuner!=0)
329 return -EINVAL;
330 /* Only 1 tuner so no setting needed ! */
331 return 0;
332 }
333 case VIDIOCGFREQ: {
334 int *freq = arg;
335
336 if (radio->curfreq==-1)
337 return -EINVAL;
338 *freq = radio->curfreq;
339 return 0;
340 }
341 case VIDIOCSFREQ: {
342 int *freq = arg;
343
344 radio->curfreq = *freq;
345 if (dsbr100_setfreq(radio, radio->curfreq)==-1)
346 warn("Set frequency failed");
347 return 0;
348 }
349 case VIDIOCGAUDIO: {
350 struct video_audio *v = arg;
351
352 memset(v, 0, sizeof(*v));
353 v->flags |= VIDEO_AUDIO_MUTABLE;
354 v->mode = VIDEO_SOUND_STEREO;
355 v->volume = 1;
356 v->step = 1;
357 strcpy(v->name, "Radio");
358 return 0;
359 }
360 case VIDIOCSAUDIO: {
361 struct video_audio *v = arg;
362
363 if (v->audio)
364 return -EINVAL;
365 if (v->flags&VIDEO_AUDIO_MUTE) {
366 if (dsbr100_stop(radio)==-1)
367 warn("Radio did not respond properly");
368 }
369 else
370 if (dsbr100_start(radio)==-1)
371 warn("Radio did not respond properly");
372 return 0;
373 }
374 default:
375 return -ENOIOCTLCMD;
376 }
377}
378
379static int usb_dsbr100_ioctl(struct inode *inode, struct file *file,
380 unsigned int cmd, unsigned long arg)
381{
382 return video_usercopy(inode, file, cmd, arg, usb_dsbr100_do_ioctl);
383}
384
385static int usb_dsbr100_open(struct inode *inode, struct file *file)
386{
387 dsbr100_device *radio=video_get_drvdata(video_devdata(file));
388
389 radio->users = 1;
390 if (dsbr100_start(radio)<0) {
391 warn("Radio did not start up properly");
392 radio->users = 0;
393 return -EIO;
394 }
395 dsbr100_setfreq(radio, radio->curfreq);
396 return 0;
397}
398
399static int usb_dsbr100_close(struct inode *inode, struct file *file)
400{
401 dsbr100_device *radio=video_get_drvdata(video_devdata(file));
402
403 if (!radio)
404 return -ENODEV;
405 radio->users = 0;
406 if (radio->removed) {
407 kfree(radio);
408 }
409 return 0;
410}
411
412static int __init dsbr100_init(void)
413{
414 int retval = usb_register(&usb_dsbr100_driver);
415 info(DRIVER_VERSION ":" DRIVER_DESC);
416 return retval;
417}
418
419static void __exit dsbr100_exit(void)
420{
421 usb_deregister(&usb_dsbr100_driver);
422}
423
424module_init (dsbr100_init);
425module_exit (dsbr100_exit);
426
427MODULE_AUTHOR( DRIVER_AUTHOR );
428MODULE_DESCRIPTION( DRIVER_DESC );
429MODULE_LICENSE("GPL");
diff --git a/drivers/usb/media/et61x251.h b/drivers/usb/media/et61x251.h
deleted file mode 100644
index 652238f329f3..000000000000
--- a/drivers/usb/media/et61x251.h
+++ /dev/null
@@ -1,220 +0,0 @@
1/***************************************************************************
2 * V4L2 driver for ET61X[12]51 PC Camera Controllers *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _ET61X251_H_
22#define _ET61X251_H_
23
24#include <linux/version.h>
25#include <linux/usb.h>
26#include <linux/videodev2.h>
27#include <media/v4l2-common.h>
28#include <linux/device.h>
29#include <linux/list.h>
30#include <linux/spinlock.h>
31#include <linux/time.h>
32#include <linux/wait.h>
33#include <linux/types.h>
34#include <linux/param.h>
35#include <linux/rwsem.h>
36#include <asm/semaphore.h>
37
38#include "et61x251_sensor.h"
39
40/*****************************************************************************/
41
42#define ET61X251_DEBUG
43#define ET61X251_DEBUG_LEVEL 2
44#define ET61X251_MAX_DEVICES 64
45#define ET61X251_PRESERVE_IMGSCALE 0
46#define ET61X251_FORCE_MUNMAP 0
47#define ET61X251_MAX_FRAMES 32
48#define ET61X251_COMPRESSION_QUALITY 0
49#define ET61X251_URBS 2
50#define ET61X251_ISO_PACKETS 7
51#define ET61X251_ALTERNATE_SETTING 13
52#define ET61X251_URB_TIMEOUT msecs_to_jiffies(2 * ET61X251_ISO_PACKETS)
53#define ET61X251_CTRL_TIMEOUT 100
54
55/*****************************************************************************/
56
57static const struct usb_device_id et61x251_id_table[] = {
58 { USB_DEVICE(0x102c, 0x6151), },
59 { USB_DEVICE(0x102c, 0x6251), },
60 { USB_DEVICE(0x102c, 0x6253), },
61 { USB_DEVICE(0x102c, 0x6254), },
62 { USB_DEVICE(0x102c, 0x6255), },
63 { USB_DEVICE(0x102c, 0x6256), },
64 { USB_DEVICE(0x102c, 0x6257), },
65 { USB_DEVICE(0x102c, 0x6258), },
66 { USB_DEVICE(0x102c, 0x6259), },
67 { USB_DEVICE(0x102c, 0x625a), },
68 { USB_DEVICE(0x102c, 0x625b), },
69 { USB_DEVICE(0x102c, 0x625c), },
70 { USB_DEVICE(0x102c, 0x625d), },
71 { USB_DEVICE(0x102c, 0x625e), },
72 { USB_DEVICE(0x102c, 0x625f), },
73 { USB_DEVICE(0x102c, 0x6260), },
74 { USB_DEVICE(0x102c, 0x6261), },
75 { USB_DEVICE(0x102c, 0x6262), },
76 { USB_DEVICE(0x102c, 0x6263), },
77 { USB_DEVICE(0x102c, 0x6264), },
78 { USB_DEVICE(0x102c, 0x6265), },
79 { USB_DEVICE(0x102c, 0x6266), },
80 { USB_DEVICE(0x102c, 0x6267), },
81 { USB_DEVICE(0x102c, 0x6268), },
82 { USB_DEVICE(0x102c, 0x6269), },
83 { }
84};
85
86ET61X251_SENSOR_TABLE
87
88/*****************************************************************************/
89
90enum et61x251_frame_state {
91 F_UNUSED,
92 F_QUEUED,
93 F_GRABBING,
94 F_DONE,
95 F_ERROR,
96};
97
98struct et61x251_frame_t {
99 void* bufmem;
100 struct v4l2_buffer buf;
101 enum et61x251_frame_state state;
102 struct list_head frame;
103 unsigned long vma_use_count;
104};
105
106enum et61x251_dev_state {
107 DEV_INITIALIZED = 0x01,
108 DEV_DISCONNECTED = 0x02,
109 DEV_MISCONFIGURED = 0x04,
110};
111
112enum et61x251_io_method {
113 IO_NONE,
114 IO_READ,
115 IO_MMAP,
116};
117
118enum et61x251_stream_state {
119 STREAM_OFF,
120 STREAM_INTERRUPT,
121 STREAM_ON,
122};
123
124struct et61x251_sysfs_attr {
125 u8 reg, i2c_reg;
126};
127
128struct et61x251_module_param {
129 u8 force_munmap;
130};
131
132static DECLARE_MUTEX(et61x251_sysfs_lock);
133static DECLARE_RWSEM(et61x251_disconnect);
134
135struct et61x251_device {
136 struct video_device* v4ldev;
137
138 struct et61x251_sensor* sensor;
139
140 struct usb_device* usbdev;
141 struct urb* urb[ET61X251_URBS];
142 void* transfer_buffer[ET61X251_URBS];
143 u8* control_buffer;
144
145 struct et61x251_frame_t *frame_current, frame[ET61X251_MAX_FRAMES];
146 struct list_head inqueue, outqueue;
147 u32 frame_count, nbuffers, nreadbuffers;
148
149 enum et61x251_io_method io;
150 enum et61x251_stream_state stream;
151
152 struct v4l2_jpegcompression compression;
153
154 struct et61x251_sysfs_attr sysfs;
155 struct et61x251_module_param module_param;
156
157 enum et61x251_dev_state state;
158 u8 users;
159
160 struct semaphore dev_sem, fileop_sem;
161 spinlock_t queue_lock;
162 wait_queue_head_t open, wait_frame, wait_stream;
163};
164
165/*****************************************************************************/
166
167void
168et61x251_attach_sensor(struct et61x251_device* cam,
169 struct et61x251_sensor* sensor)
170{
171 cam->sensor = sensor;
172 cam->sensor->usbdev = cam->usbdev;
173}
174
175/*****************************************************************************/
176
177#undef DBG
178#undef KDBG
179#ifdef ET61X251_DEBUG
180# define DBG(level, fmt, args...) \
181do { \
182 if (debug >= (level)) { \
183 if ((level) == 1) \
184 dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
185 else if ((level) == 2) \
186 dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
187 else if ((level) >= 3) \
188 dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
189 __FUNCTION__, __LINE__ , ## args); \
190 } \
191} while (0)
192# define KDBG(level, fmt, args...) \
193do { \
194 if (debug >= (level)) { \
195 if ((level) == 1 || (level) == 2) \
196 pr_info("et61x251: " fmt "\n", ## args); \
197 else if ((level) == 3) \
198 pr_debug("et61x251: [%s:%d] " fmt "\n", __FUNCTION__, \
199 __LINE__ , ## args); \
200 } \
201} while (0)
202# define V4LDBG(level, name, cmd) \
203do { \
204 if (debug >= (level)) \
205 v4l_print_ioctl(name, cmd); \
206} while (0)
207#else
208# define DBG(level, fmt, args...) do {;} while(0)
209# define KDBG(level, fmt, args...) do {;} while(0)
210# define V4LDBG(level, name, cmd) do {;} while(0)
211#endif
212
213#undef PDBG
214#define PDBG(fmt, args...) \
215dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args)
216
217#undef PDBGG
218#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
219
220#endif /* _ET61X251_H_ */
diff --git a/drivers/usb/media/et61x251_core.c b/drivers/usb/media/et61x251_core.c
deleted file mode 100644
index 2c0171a5ad62..000000000000
--- a/drivers/usb/media/et61x251_core.c
+++ /dev/null
@@ -1,2605 +0,0 @@
1/***************************************************************************
2 * V4L2 driver for ET61X[12]51 PC Camera Controllers *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/param.h>
25#include <linux/moduleparam.h>
26#include <linux/errno.h>
27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/device.h>
30#include <linux/fs.h>
31#include <linux/delay.h>
32#include <linux/stddef.h>
33#include <linux/compiler.h>
34#include <linux/ioctl.h>
35#include <linux/poll.h>
36#include <linux/stat.h>
37#include <linux/mm.h>
38#include <linux/vmalloc.h>
39#include <linux/page-flags.h>
40#include <linux/byteorder/generic.h>
41#include <asm/page.h>
42#include <asm/uaccess.h>
43
44#include "et61x251.h"
45
46/*****************************************************************************/
47
48#define ET61X251_MODULE_NAME "V4L2 driver for ET61X[12]51 " \
49 "PC Camera Controllers"
50#define ET61X251_MODULE_AUTHOR "(C) 2006 Luca Risolia"
51#define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
52#define ET61X251_MODULE_LICENSE "GPL"
53#define ET61X251_MODULE_VERSION "1:1.01"
54#define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 1)
55
56/*****************************************************************************/
57
58MODULE_DEVICE_TABLE(usb, et61x251_id_table);
59
60MODULE_AUTHOR(ET61X251_MODULE_AUTHOR " " ET61X251_AUTHOR_EMAIL);
61MODULE_DESCRIPTION(ET61X251_MODULE_NAME);
62MODULE_VERSION(ET61X251_MODULE_VERSION);
63MODULE_LICENSE(ET61X251_MODULE_LICENSE);
64
65static short video_nr[] = {[0 ... ET61X251_MAX_DEVICES-1] = -1};
66module_param_array(video_nr, short, NULL, 0444);
67MODULE_PARM_DESC(video_nr,
68 "\n<-1|n[,...]> Specify V4L2 minor mode number."
69 "\n -1 = use next available (default)"
70 "\n n = use minor number n (integer >= 0)"
71 "\nYou can specify up to "
72 __MODULE_STRING(ET61X251_MAX_DEVICES) " cameras this way."
73 "\nFor example:"
74 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
75 "\nthe second registered camera and use auto for the first"
76 "\none and for every other camera."
77 "\n");
78
79static short force_munmap[] = {[0 ... ET61X251_MAX_DEVICES-1] =
80 ET61X251_FORCE_MUNMAP};
81module_param_array(force_munmap, bool, NULL, 0444);
82MODULE_PARM_DESC(force_munmap,
83 "\n<0|1[,...]> Force the application to unmap previously"
84 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
85 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
86 "\nthis feature. This parameter is specific for each"
87 "\ndetected camera."
88 "\n 0 = do not force memory unmapping"
89 "\n 1 = force memory unmapping (save memory)"
90 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
91 "\n");
92
93#ifdef ET61X251_DEBUG
94static unsigned short debug = ET61X251_DEBUG_LEVEL;
95module_param(debug, ushort, 0644);
96MODULE_PARM_DESC(debug,
97 "\n<n> Debugging information level, from 0 to 3:"
98 "\n0 = none (use carefully)"
99 "\n1 = critical errors"
100 "\n2 = significant informations"
101 "\n3 = more verbose messages"
102 "\nLevel 3 is useful for testing only, when only "
103 "one device is used."
104 "\nDefault value is "__MODULE_STRING(ET61X251_DEBUG_LEVEL)"."
105 "\n");
106#endif
107
108/*****************************************************************************/
109
110static u32
111et61x251_request_buffers(struct et61x251_device* cam, u32 count,
112 enum et61x251_io_method io)
113{
114 struct v4l2_pix_format* p = &(cam->sensor->pix_format);
115 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds);
116 const size_t imagesize = cam->module_param.force_munmap ||
117 io == IO_READ ?
118 (p->width * p->height * p->priv) / 8 :
119 (r->width * r->height * p->priv) / 8;
120 void* buff = NULL;
121 u32 i;
122
123 if (count > ET61X251_MAX_FRAMES)
124 count = ET61X251_MAX_FRAMES;
125
126 cam->nbuffers = count;
127 while (cam->nbuffers > 0) {
128 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
129 break;
130 cam->nbuffers--;
131 }
132
133 for (i = 0; i < cam->nbuffers; i++) {
134 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
135 cam->frame[i].buf.index = i;
136 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
137 cam->frame[i].buf.length = imagesize;
138 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
139 cam->frame[i].buf.sequence = 0;
140 cam->frame[i].buf.field = V4L2_FIELD_NONE;
141 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
142 cam->frame[i].buf.flags = 0;
143 }
144
145 return cam->nbuffers;
146}
147
148
149static void et61x251_release_buffers(struct et61x251_device* cam)
150{
151 if (cam->nbuffers) {
152 vfree(cam->frame[0].bufmem);
153 cam->nbuffers = 0;
154 }
155 cam->frame_current = NULL;
156}
157
158
159static void et61x251_empty_framequeues(struct et61x251_device* cam)
160{
161 u32 i;
162
163 INIT_LIST_HEAD(&cam->inqueue);
164 INIT_LIST_HEAD(&cam->outqueue);
165
166 for (i = 0; i < ET61X251_MAX_FRAMES; i++) {
167 cam->frame[i].state = F_UNUSED;
168 cam->frame[i].buf.bytesused = 0;
169 }
170}
171
172
173static void et61x251_requeue_outqueue(struct et61x251_device* cam)
174{
175 struct et61x251_frame_t *i;
176
177 list_for_each_entry(i, &cam->outqueue, frame) {
178 i->state = F_QUEUED;
179 list_add(&i->frame, &cam->inqueue);
180 }
181
182 INIT_LIST_HEAD(&cam->outqueue);
183}
184
185
186static void et61x251_queue_unusedframes(struct et61x251_device* cam)
187{
188 unsigned long lock_flags;
189 u32 i;
190
191 for (i = 0; i < cam->nbuffers; i++)
192 if (cam->frame[i].state == F_UNUSED) {
193 cam->frame[i].state = F_QUEUED;
194 spin_lock_irqsave(&cam->queue_lock, lock_flags);
195 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
196 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
197 }
198}
199
200/*****************************************************************************/
201
202int et61x251_write_reg(struct et61x251_device* cam, u8 value, u16 index)
203{
204 struct usb_device* udev = cam->usbdev;
205 u8* buff = cam->control_buffer;
206 int res;
207
208 *buff = value;
209
210 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
211 0, index, buff, 1, ET61X251_CTRL_TIMEOUT);
212 if (res < 0) {
213 DBG(3, "Failed to write a register (value 0x%02X, index "
214 "0x%02X, error %d)", value, index, res);
215 return -1;
216 }
217
218 return 0;
219}
220
221
222int et61x251_read_reg(struct et61x251_device* cam, u16 index)
223{
224 struct usb_device* udev = cam->usbdev;
225 u8* buff = cam->control_buffer;
226 int res;
227
228 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
229 0, index, buff, 1, ET61X251_CTRL_TIMEOUT);
230 if (res < 0)
231 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
232 index, res);
233
234 return (res >= 0) ? (int)(*buff) : -1;
235}
236
237
238static int
239et61x251_i2c_wait(struct et61x251_device* cam, struct et61x251_sensor* sensor)
240{
241 int i, r;
242
243 for (i = 1; i <= 8; i++) {
244 if (sensor->interface == ET61X251_I2C_3WIRES) {
245 r = et61x251_read_reg(cam, 0x8e);
246 if (!(r & 0x02) && (r >= 0))
247 return 0;
248 } else {
249 r = et61x251_read_reg(cam, 0x8b);
250 if (!(r & 0x01) && (r >= 0))
251 return 0;
252 }
253 if (r < 0)
254 return -EIO;
255 udelay(8*8); /* minimum for sensors at 400kHz */
256 }
257
258 return -EBUSY;
259}
260
261
262int
263et61x251_i2c_try_read(struct et61x251_device* cam,
264 struct et61x251_sensor* sensor, u8 address)
265{
266 struct usb_device* udev = cam->usbdev;
267 u8* data = cam->control_buffer;
268 int err = 0, res;
269
270 data[0] = address;
271 data[1] = cam->sensor->i2c_slave_id;
272 data[2] = cam->sensor->rsta | 0x10;
273 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
274 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
275 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
276 if (res < 0)
277 err += res;
278
279 err += et61x251_i2c_wait(cam, sensor);
280
281 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
282 0, 0x80, data, 8, ET61X251_CTRL_TIMEOUT);
283 if (res < 0)
284 err += res;
285
286 if (err)
287 DBG(3, "I2C read failed for %s image sensor", sensor->name);
288
289 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[0]);
290
291 return err ? -1 : (int)data[0];
292}
293
294
295int
296et61x251_i2c_try_write(struct et61x251_device* cam,
297 struct et61x251_sensor* sensor, u8 address, u8 value)
298{
299 struct usb_device* udev = cam->usbdev;
300 u8* data = cam->control_buffer;
301 int err = 0, res;
302
303 data[0] = address;
304 data[1] = cam->sensor->i2c_slave_id;
305 data[2] = cam->sensor->rsta | 0x12;
306 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
307 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
308 if (res < 0)
309 err += res;
310
311 data[0] = value;
312 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
313 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
314 if (res < 0)
315 err += res;
316
317 err += et61x251_i2c_wait(cam, sensor);
318
319 if (err)
320 DBG(3, "I2C write failed for %s image sensor", sensor->name);
321
322 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address, value);
323
324 return err ? -1 : 0;
325}
326
327
328int
329et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
330 u8 data3, u8 data4, u8 data5, u8 data6, u8 data7,
331 u8 data8, u8 address)
332{
333 struct usb_device* udev = cam->usbdev;
334 u8* data = cam->control_buffer;
335 int err = 0, res;
336
337 if (!cam->sensor)
338 return -1;
339
340 data[0] = data2;
341 data[1] = data3;
342 data[2] = data4;
343 data[3] = data5;
344 data[4] = data6;
345 data[5] = data7;
346 data[6] = data8;
347 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
348 0, 0x81, data, n-1, ET61X251_CTRL_TIMEOUT);
349 if (res < 0)
350 err += res;
351
352 data[0] = address;
353 data[1] = cam->sensor->i2c_slave_id;
354 data[2] = cam->sensor->rsta | 0x02 | (n << 4);
355 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
356 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
357 if (res < 0)
358 err += res;
359
360 /* Start writing through the serial interface */
361 data[0] = data1;
362 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
363 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
364 if (res < 0)
365 err += res;
366
367 err += et61x251_i2c_wait(cam, cam->sensor);
368
369 if (err)
370 DBG(3, "I2C raw write failed for %s image sensor",
371 cam->sensor->name);
372
373 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
374 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
375 " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n, address,
376 data1, data2, data3, data4, data5, data6, data7, data8);
377
378 return err ? -1 : 0;
379
380}
381
382
383int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
384{
385 if (!cam->sensor)
386 return -1;
387
388 return et61x251_i2c_try_read(cam, cam->sensor, address);
389}
390
391
392int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value)
393{
394 if (!cam->sensor)
395 return -1;
396
397 return et61x251_i2c_try_write(cam, cam->sensor, address, value);
398}
399
400/*****************************************************************************/
401
402static void et61x251_urb_complete(struct urb *urb, struct pt_regs* regs)
403{
404 struct et61x251_device* cam = urb->context;
405 struct et61x251_frame_t** f;
406 size_t imagesize;
407 u8 i;
408 int err = 0;
409
410 if (urb->status == -ENOENT)
411 return;
412
413 f = &cam->frame_current;
414
415 if (cam->stream == STREAM_INTERRUPT) {
416 cam->stream = STREAM_OFF;
417 if ((*f))
418 (*f)->state = F_QUEUED;
419 DBG(3, "Stream interrupted");
420 wake_up_interruptible(&cam->wait_stream);
421 }
422
423 if (cam->state & DEV_DISCONNECTED)
424 return;
425
426 if (cam->state & DEV_MISCONFIGURED) {
427 wake_up_interruptible(&cam->wait_frame);
428 return;
429 }
430
431 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
432 goto resubmit_urb;
433
434 if (!(*f))
435 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t,
436 frame);
437
438 imagesize = (cam->sensor->pix_format.width *
439 cam->sensor->pix_format.height *
440 cam->sensor->pix_format.priv) / 8;
441
442 for (i = 0; i < urb->number_of_packets; i++) {
443 unsigned int len, status;
444 void *pos;
445 u8* b1, * b2, sof;
446 const u8 VOID_BYTES = 6;
447 size_t imglen;
448
449 len = urb->iso_frame_desc[i].actual_length;
450 status = urb->iso_frame_desc[i].status;
451 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
452
453 if (status) {
454 DBG(3, "Error in isochronous frame");
455 (*f)->state = F_ERROR;
456 continue;
457 }
458
459 b1 = pos++;
460 b2 = pos++;
461 sof = ((*b1 & 0x3f) == 63);
462 imglen = ((*b1 & 0xc0) << 2) | *b2;
463
464 PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
465 len, i, imglen);
466
467 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
468start_of_frame:
469 if (sof) {
470 (*f)->state = F_GRABBING;
471 (*f)->buf.bytesused = 0;
472 do_gettimeofday(&(*f)->buf.timestamp);
473 pos += 22;
474 DBG(3, "SOF detected: new video frame");
475 }
476
477 if ((*f)->state == F_GRABBING) {
478 if (sof && (*f)->buf.bytesused) {
479 if (cam->sensor->pix_format.pixelformat ==
480 V4L2_PIX_FMT_ET61X251)
481 goto end_of_frame;
482 else {
483 DBG(3, "Not expected SOF detected "
484 "after %lu bytes",
485 (unsigned long)(*f)->buf.bytesused);
486 (*f)->state = F_ERROR;
487 continue;
488 }
489 }
490
491 if ((*f)->buf.bytesused + imglen > imagesize) {
492 DBG(3, "Video frame size exceeded");
493 (*f)->state = F_ERROR;
494 continue;
495 }
496
497 pos += VOID_BYTES;
498
499 memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, imglen);
500 (*f)->buf.bytesused += imglen;
501
502 if ((*f)->buf.bytesused == imagesize) {
503 u32 b;
504end_of_frame:
505 b = (*f)->buf.bytesused;
506 (*f)->state = F_DONE;
507 (*f)->buf.sequence= ++cam->frame_count;
508 spin_lock(&cam->queue_lock);
509 list_move_tail(&(*f)->frame, &cam->outqueue);
510 if (!list_empty(&cam->inqueue))
511 (*f) = list_entry(cam->inqueue.next,
512 struct et61x251_frame_t,
513 frame);
514 else
515 (*f) = NULL;
516 spin_unlock(&cam->queue_lock);
517 DBG(3, "Video frame captured: : %lu bytes",
518 (unsigned long)(b));
519
520 if (!(*f))
521 goto resubmit_urb;
522
523 if (sof &&
524 cam->sensor->pix_format.pixelformat ==
525 V4L2_PIX_FMT_ET61X251)
526 goto start_of_frame;
527 }
528 }
529 }
530
531resubmit_urb:
532 urb->dev = cam->usbdev;
533 err = usb_submit_urb(urb, GFP_ATOMIC);
534 if (err < 0 && err != -EPERM) {
535 cam->state |= DEV_MISCONFIGURED;
536 DBG(1, "usb_submit_urb() failed");
537 }
538
539 wake_up_interruptible(&cam->wait_frame);
540}
541
542
543static int et61x251_start_transfer(struct et61x251_device* cam)
544{
545 struct usb_device *udev = cam->usbdev;
546 struct urb* urb;
547 const unsigned int wMaxPacketSize[] = {0, 256, 384, 512, 640, 768, 832,
548 864, 896, 920, 956, 980, 1000,
549 1022};
550 const unsigned int psz = wMaxPacketSize[ET61X251_ALTERNATE_SETTING];
551 s8 i, j;
552 int err = 0;
553
554 for (i = 0; i < ET61X251_URBS; i++) {
555 cam->transfer_buffer[i] = kzalloc(ET61X251_ISO_PACKETS * psz,
556 GFP_KERNEL);
557 if (!cam->transfer_buffer[i]) {
558 err = -ENOMEM;
559 DBG(1, "Not enough memory");
560 goto free_buffers;
561 }
562 }
563
564 for (i = 0; i < ET61X251_URBS; i++) {
565 urb = usb_alloc_urb(ET61X251_ISO_PACKETS, GFP_KERNEL);
566 cam->urb[i] = urb;
567 if (!urb) {
568 err = -ENOMEM;
569 DBG(1, "usb_alloc_urb() failed");
570 goto free_urbs;
571 }
572 urb->dev = udev;
573 urb->context = cam;
574 urb->pipe = usb_rcvisocpipe(udev, 1);
575 urb->transfer_flags = URB_ISO_ASAP;
576 urb->number_of_packets = ET61X251_ISO_PACKETS;
577 urb->complete = et61x251_urb_complete;
578 urb->transfer_buffer = cam->transfer_buffer[i];
579 urb->transfer_buffer_length = psz * ET61X251_ISO_PACKETS;
580 urb->interval = 1;
581 for (j = 0; j < ET61X251_ISO_PACKETS; j++) {
582 urb->iso_frame_desc[j].offset = psz * j;
583 urb->iso_frame_desc[j].length = psz;
584 }
585 }
586
587 err = et61x251_write_reg(cam, 0x01, 0x03);
588 err = et61x251_write_reg(cam, 0x00, 0x03);
589 err = et61x251_write_reg(cam, 0x08, 0x03);
590 if (err) {
591 err = -EIO;
592 DBG(1, "I/O hardware error");
593 goto free_urbs;
594 }
595
596 err = usb_set_interface(udev, 0, ET61X251_ALTERNATE_SETTING);
597 if (err) {
598 DBG(1, "usb_set_interface() failed");
599 goto free_urbs;
600 }
601
602 cam->frame_current = NULL;
603
604 for (i = 0; i < ET61X251_URBS; i++) {
605 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
606 if (err) {
607 for (j = i-1; j >= 0; j--)
608 usb_kill_urb(cam->urb[j]);
609 DBG(1, "usb_submit_urb() failed, error %d", err);
610 goto free_urbs;
611 }
612 }
613
614 return 0;
615
616free_urbs:
617 for (i = 0; (i < ET61X251_URBS) && cam->urb[i]; i++)
618 usb_free_urb(cam->urb[i]);
619
620free_buffers:
621 for (i = 0; (i < ET61X251_URBS) && cam->transfer_buffer[i]; i++)
622 kfree(cam->transfer_buffer[i]);
623
624 return err;
625}
626
627
628static int et61x251_stop_transfer(struct et61x251_device* cam)
629{
630 struct usb_device *udev = cam->usbdev;
631 s8 i;
632 int err = 0;
633
634 if (cam->state & DEV_DISCONNECTED)
635 return 0;
636
637 for (i = ET61X251_URBS-1; i >= 0; i--) {
638 usb_kill_urb(cam->urb[i]);
639 usb_free_urb(cam->urb[i]);
640 kfree(cam->transfer_buffer[i]);
641 }
642
643 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
644 if (err)
645 DBG(3, "usb_set_interface() failed");
646
647 return err;
648}
649
650
651static int et61x251_stream_interrupt(struct et61x251_device* cam)
652{
653 int err = 0;
654
655 cam->stream = STREAM_INTERRUPT;
656 err = wait_event_timeout(cam->wait_stream,
657 (cam->stream == STREAM_OFF) ||
658 (cam->state & DEV_DISCONNECTED),
659 ET61X251_URB_TIMEOUT);
660 if (cam->state & DEV_DISCONNECTED)
661 return -ENODEV;
662 else if (err) {
663 cam->state |= DEV_MISCONFIGURED;
664 DBG(1, "URB timeout reached. The camera is misconfigured. To "
665 "use it, close and open /dev/video%d again.",
666 cam->v4ldev->minor);
667 return err;
668 }
669
670 return 0;
671}
672
673/*****************************************************************************/
674
675#ifdef CONFIG_VIDEO_ADV_DEBUG
676static u8 et61x251_strtou8(const char* buff, size_t len, ssize_t* count)
677{
678 char str[5];
679 char* endp;
680 unsigned long val;
681
682 if (len < 4) {
683 strncpy(str, buff, len);
684 str[len+1] = '\0';
685 } else {
686 strncpy(str, buff, 4);
687 str[4] = '\0';
688 }
689
690 val = simple_strtoul(str, &endp, 0);
691
692 *count = 0;
693 if (val <= 0xff)
694 *count = (ssize_t)(endp - str);
695 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
696 *count += 1;
697
698 return (u8)val;
699}
700
701/*
702 NOTE 1: being inside one of the following methods implies that the v4l
703 device exists for sure (see kobjects and reference counters)
704 NOTE 2: buffers are PAGE_SIZE long
705*/
706
707static ssize_t et61x251_show_reg(struct class_device* cd, char* buf)
708{
709 struct et61x251_device* cam;
710 ssize_t count;
711
712 if (down_interruptible(&et61x251_sysfs_lock))
713 return -ERESTARTSYS;
714
715 cam = video_get_drvdata(to_video_device(cd));
716 if (!cam) {
717 up(&et61x251_sysfs_lock);
718 return -ENODEV;
719 }
720
721 count = sprintf(buf, "%u\n", cam->sysfs.reg);
722
723 up(&et61x251_sysfs_lock);
724
725 return count;
726}
727
728
729static ssize_t
730et61x251_store_reg(struct class_device* cd, const char* buf, size_t len)
731{
732 struct et61x251_device* cam;
733 u8 index;
734 ssize_t count;
735
736 if (down_interruptible(&et61x251_sysfs_lock))
737 return -ERESTARTSYS;
738
739 cam = video_get_drvdata(to_video_device(cd));
740 if (!cam) {
741 up(&et61x251_sysfs_lock);
742 return -ENODEV;
743 }
744
745 index = et61x251_strtou8(buf, len, &count);
746 if (index > 0x8e || !count) {
747 up(&et61x251_sysfs_lock);
748 return -EINVAL;
749 }
750
751 cam->sysfs.reg = index;
752
753 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
754 DBG(3, "Written bytes: %zd", count);
755
756 up(&et61x251_sysfs_lock);
757
758 return count;
759}
760
761
762static ssize_t et61x251_show_val(struct class_device* cd, char* buf)
763{
764 struct et61x251_device* cam;
765 ssize_t count;
766 int val;
767
768 if (down_interruptible(&et61x251_sysfs_lock))
769 return -ERESTARTSYS;
770
771 cam = video_get_drvdata(to_video_device(cd));
772 if (!cam) {
773 up(&et61x251_sysfs_lock);
774 return -ENODEV;
775 }
776
777 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
778 up(&et61x251_sysfs_lock);
779 return -EIO;
780 }
781
782 count = sprintf(buf, "%d\n", val);
783
784 DBG(3, "Read bytes: %zd", count);
785
786 up(&et61x251_sysfs_lock);
787
788 return count;
789}
790
791
792static ssize_t
793et61x251_store_val(struct class_device* cd, const char* buf, size_t len)
794{
795 struct et61x251_device* cam;
796 u8 value;
797 ssize_t count;
798 int err;
799
800 if (down_interruptible(&et61x251_sysfs_lock))
801 return -ERESTARTSYS;
802
803 cam = video_get_drvdata(to_video_device(cd));
804 if (!cam) {
805 up(&et61x251_sysfs_lock);
806 return -ENODEV;
807 }
808
809 value = et61x251_strtou8(buf, len, &count);
810 if (!count) {
811 up(&et61x251_sysfs_lock);
812 return -EINVAL;
813 }
814
815 err = et61x251_write_reg(cam, value, cam->sysfs.reg);
816 if (err) {
817 up(&et61x251_sysfs_lock);
818 return -EIO;
819 }
820
821 DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
822 cam->sysfs.reg, value);
823 DBG(3, "Written bytes: %zd", count);
824
825 up(&et61x251_sysfs_lock);
826
827 return count;
828}
829
830
831static ssize_t et61x251_show_i2c_reg(struct class_device* cd, char* buf)
832{
833 struct et61x251_device* cam;
834 ssize_t count;
835
836 if (down_interruptible(&et61x251_sysfs_lock))
837 return -ERESTARTSYS;
838
839 cam = video_get_drvdata(to_video_device(cd));
840 if (!cam) {
841 up(&et61x251_sysfs_lock);
842 return -ENODEV;
843 }
844
845 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
846
847 DBG(3, "Read bytes: %zd", count);
848
849 up(&et61x251_sysfs_lock);
850
851 return count;
852}
853
854
855static ssize_t
856et61x251_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
857{
858 struct et61x251_device* cam;
859 u8 index;
860 ssize_t count;
861
862 if (down_interruptible(&et61x251_sysfs_lock))
863 return -ERESTARTSYS;
864
865 cam = video_get_drvdata(to_video_device(cd));
866 if (!cam) {
867 up(&et61x251_sysfs_lock);
868 return -ENODEV;
869 }
870
871 index = et61x251_strtou8(buf, len, &count);
872 if (!count) {
873 up(&et61x251_sysfs_lock);
874 return -EINVAL;
875 }
876
877 cam->sysfs.i2c_reg = index;
878
879 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
880 DBG(3, "Written bytes: %zd", count);
881
882 up(&et61x251_sysfs_lock);
883
884 return count;
885}
886
887
888static ssize_t et61x251_show_i2c_val(struct class_device* cd, char* buf)
889{
890 struct et61x251_device* cam;
891 ssize_t count;
892 int val;
893
894 if (down_interruptible(&et61x251_sysfs_lock))
895 return -ERESTARTSYS;
896
897 cam = video_get_drvdata(to_video_device(cd));
898 if (!cam) {
899 up(&et61x251_sysfs_lock);
900 return -ENODEV;
901 }
902
903 if (!(cam->sensor->sysfs_ops & ET61X251_I2C_READ)) {
904 up(&et61x251_sysfs_lock);
905 return -ENOSYS;
906 }
907
908 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
909 up(&et61x251_sysfs_lock);
910 return -EIO;
911 }
912
913 count = sprintf(buf, "%d\n", val);
914
915 DBG(3, "Read bytes: %zd", count);
916
917 up(&et61x251_sysfs_lock);
918
919 return count;
920}
921
922
923static ssize_t
924et61x251_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
925{
926 struct et61x251_device* cam;
927 u8 value;
928 ssize_t count;
929 int err;
930
931 if (down_interruptible(&et61x251_sysfs_lock))
932 return -ERESTARTSYS;
933
934 cam = video_get_drvdata(to_video_device(cd));
935 if (!cam) {
936 up(&et61x251_sysfs_lock);
937 return -ENODEV;
938 }
939
940 if (!(cam->sensor->sysfs_ops & ET61X251_I2C_READ)) {
941 up(&et61x251_sysfs_lock);
942 return -ENOSYS;
943 }
944
945 value = et61x251_strtou8(buf, len, &count);
946 if (!count) {
947 up(&et61x251_sysfs_lock);
948 return -EINVAL;
949 }
950
951 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
952 if (err) {
953 up(&et61x251_sysfs_lock);
954 return -EIO;
955 }
956
957 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
958 cam->sysfs.i2c_reg, value);
959 DBG(3, "Written bytes: %zd", count);
960
961 up(&et61x251_sysfs_lock);
962
963 return count;
964}
965
966
967static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
968 et61x251_show_reg, et61x251_store_reg);
969static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
970 et61x251_show_val, et61x251_store_val);
971static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
972 et61x251_show_i2c_reg, et61x251_store_i2c_reg);
973static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
974 et61x251_show_i2c_val, et61x251_store_i2c_val);
975
976
977static void et61x251_create_sysfs(struct et61x251_device* cam)
978{
979 struct video_device *v4ldev = cam->v4ldev;
980
981 video_device_create_file(v4ldev, &class_device_attr_reg);
982 video_device_create_file(v4ldev, &class_device_attr_val);
983 if (cam->sensor && cam->sensor->sysfs_ops) {
984 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
985 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
986 }
987}
988#endif /* CONFIG_VIDEO_ADV_DEBUG */
989
990/*****************************************************************************/
991
992static int
993et61x251_set_pix_format(struct et61x251_device* cam,
994 struct v4l2_pix_format* pix)
995{
996 int r, err = 0;
997
998 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
999 err += r;
1000 if (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1001 err += et61x251_write_reg(cam, r & 0xfd, 0x12);
1002 else
1003 err += et61x251_write_reg(cam, r | 0x02, 0x12);
1004
1005 return err ? -EIO : 0;
1006}
1007
1008
1009static int
1010et61x251_set_compression(struct et61x251_device* cam,
1011 struct v4l2_jpegcompression* compression)
1012{
1013 int r, err = 0;
1014
1015 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1016 err += r;
1017 if (compression->quality == 0)
1018 err += et61x251_write_reg(cam, r & 0xfb, 0x12);
1019 else
1020 err += et61x251_write_reg(cam, r | 0x04, 0x12);
1021
1022 return err ? -EIO : 0;
1023}
1024
1025
1026static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
1027{
1028 int r = 0, err = 0;
1029
1030 r = et61x251_read_reg(cam, 0x12);
1031 if (r < 0)
1032 err += r;
1033
1034 if (scale == 1)
1035 err += et61x251_write_reg(cam, r & ~0x01, 0x12);
1036 else if (scale == 2)
1037 err += et61x251_write_reg(cam, r | 0x01, 0x12);
1038
1039 if (err)
1040 return -EIO;
1041
1042 PDBGG("Scaling factor: %u", scale);
1043
1044 return 0;
1045}
1046
1047
1048static int
1049et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1050{
1051 struct et61x251_sensor* s = cam->sensor;
1052 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
1053 s->active_pixel.left),
1054 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
1055 s->active_pixel.top),
1056 fmw_length = (u16)(rect->width),
1057 fmw_height = (u16)(rect->height);
1058 int err = 0;
1059
1060 err += et61x251_write_reg(cam, fmw_sx & 0xff, 0x69);
1061 err += et61x251_write_reg(cam, fmw_sy & 0xff, 0x6a);
1062 err += et61x251_write_reg(cam, fmw_length & 0xff, 0x6b);
1063 err += et61x251_write_reg(cam, fmw_height & 0xff, 0x6c);
1064 err += et61x251_write_reg(cam, (fmw_sx >> 8) | ((fmw_sy & 0x300) >> 6)
1065 | ((fmw_length & 0x300) >> 4)
1066 | ((fmw_height & 0x300) >> 2), 0x6d);
1067 if (err)
1068 return -EIO;
1069
1070 PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1071 fmw_sx, fmw_sy, fmw_length, fmw_height);
1072
1073 return 0;
1074}
1075
1076
1077static int et61x251_init(struct et61x251_device* cam)
1078{
1079 struct et61x251_sensor* s = cam->sensor;
1080 struct v4l2_control ctrl;
1081 struct v4l2_queryctrl *qctrl;
1082 struct v4l2_rect* rect;
1083 u8 i = 0;
1084 int err = 0;
1085
1086 if (!(cam->state & DEV_INITIALIZED)) {
1087 init_waitqueue_head(&cam->open);
1088 qctrl = s->qctrl;
1089 rect = &(s->cropcap.defrect);
1090 cam->compression.quality = ET61X251_COMPRESSION_QUALITY;
1091 } else { /* use current values */
1092 qctrl = s->_qctrl;
1093 rect = &(s->_rect);
1094 }
1095
1096 err += et61x251_set_scale(cam, rect->width / s->pix_format.width);
1097 err += et61x251_set_crop(cam, rect);
1098 if (err)
1099 return err;
1100
1101 if (s->init) {
1102 err = s->init(cam);
1103 if (err) {
1104 DBG(3, "Sensor initialization failed");
1105 return err;
1106 }
1107 }
1108
1109 err += et61x251_set_compression(cam, &cam->compression);
1110 err += et61x251_set_pix_format(cam, &s->pix_format);
1111 if (s->set_pix_format)
1112 err += s->set_pix_format(cam, &s->pix_format);
1113 if (err)
1114 return err;
1115
1116 if (s->pix_format.pixelformat == V4L2_PIX_FMT_ET61X251)
1117 DBG(3, "Compressed video format is active, quality %d",
1118 cam->compression.quality);
1119 else
1120 DBG(3, "Uncompressed video format is active");
1121
1122 if (s->set_crop)
1123 if ((err = s->set_crop(cam, rect))) {
1124 DBG(3, "set_crop() failed");
1125 return err;
1126 }
1127
1128 if (s->set_ctrl) {
1129 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1130 if (s->qctrl[i].id != 0 &&
1131 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1132 ctrl.id = s->qctrl[i].id;
1133 ctrl.value = qctrl[i].default_value;
1134 err = s->set_ctrl(cam, &ctrl);
1135 if (err) {
1136 DBG(3, "Set %s control failed",
1137 s->qctrl[i].name);
1138 return err;
1139 }
1140 DBG(3, "Image sensor supports '%s' control",
1141 s->qctrl[i].name);
1142 }
1143 }
1144
1145 if (!(cam->state & DEV_INITIALIZED)) {
1146 init_MUTEX(&cam->fileop_sem);
1147 spin_lock_init(&cam->queue_lock);
1148 init_waitqueue_head(&cam->wait_frame);
1149 init_waitqueue_head(&cam->wait_stream);
1150 cam->nreadbuffers = 2;
1151 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1152 memcpy(&(s->_rect), &(s->cropcap.defrect),
1153 sizeof(struct v4l2_rect));
1154 cam->state |= DEV_INITIALIZED;
1155 }
1156
1157 DBG(2, "Initialization succeeded");
1158 return 0;
1159}
1160
1161
1162static void et61x251_release_resources(struct et61x251_device* cam)
1163{
1164 down(&et61x251_sysfs_lock);
1165
1166 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1167 video_set_drvdata(cam->v4ldev, NULL);
1168 video_unregister_device(cam->v4ldev);
1169
1170 up(&et61x251_sysfs_lock);
1171
1172 kfree(cam->control_buffer);
1173}
1174
1175/*****************************************************************************/
1176
1177static int et61x251_open(struct inode* inode, struct file* filp)
1178{
1179 struct et61x251_device* cam;
1180 int err = 0;
1181
1182 /*
1183 This is the only safe way to prevent race conditions with
1184 disconnect
1185 */
1186 if (!down_read_trylock(&et61x251_disconnect))
1187 return -ERESTARTSYS;
1188
1189 cam = video_get_drvdata(video_devdata(filp));
1190
1191 if (down_interruptible(&cam->dev_sem)) {
1192 up_read(&et61x251_disconnect);
1193 return -ERESTARTSYS;
1194 }
1195
1196 if (cam->users) {
1197 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1198 if ((filp->f_flags & O_NONBLOCK) ||
1199 (filp->f_flags & O_NDELAY)) {
1200 err = -EWOULDBLOCK;
1201 goto out;
1202 }
1203 up(&cam->dev_sem);
1204 err = wait_event_interruptible_exclusive(cam->open,
1205 cam->state & DEV_DISCONNECTED
1206 || !cam->users);
1207 if (err) {
1208 up_read(&et61x251_disconnect);
1209 return err;
1210 }
1211 if (cam->state & DEV_DISCONNECTED) {
1212 up_read(&et61x251_disconnect);
1213 return -ENODEV;
1214 }
1215 down(&cam->dev_sem);
1216 }
1217
1218
1219 if (cam->state & DEV_MISCONFIGURED) {
1220 err = et61x251_init(cam);
1221 if (err) {
1222 DBG(1, "Initialization failed again. "
1223 "I will retry on next open().");
1224 goto out;
1225 }
1226 cam->state &= ~DEV_MISCONFIGURED;
1227 }
1228
1229 if ((err = et61x251_start_transfer(cam)))
1230 goto out;
1231
1232 filp->private_data = cam;
1233 cam->users++;
1234 cam->io = IO_NONE;
1235 cam->stream = STREAM_OFF;
1236 cam->nbuffers = 0;
1237 cam->frame_count = 0;
1238 et61x251_empty_framequeues(cam);
1239
1240 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1241
1242out:
1243 up(&cam->dev_sem);
1244 up_read(&et61x251_disconnect);
1245 return err;
1246}
1247
1248
1249static int et61x251_release(struct inode* inode, struct file* filp)
1250{
1251 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1252
1253 down(&cam->dev_sem); /* prevent disconnect() to be called */
1254
1255 et61x251_stop_transfer(cam);
1256
1257 et61x251_release_buffers(cam);
1258
1259 if (cam->state & DEV_DISCONNECTED) {
1260 et61x251_release_resources(cam);
1261 up(&cam->dev_sem);
1262 kfree(cam);
1263 return 0;
1264 }
1265
1266 cam->users--;
1267 wake_up_interruptible_nr(&cam->open, 1);
1268
1269 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1270
1271 up(&cam->dev_sem);
1272
1273 return 0;
1274}
1275
1276
1277static ssize_t
1278et61x251_read(struct file* filp, char __user * buf,
1279 size_t count, loff_t* f_pos)
1280{
1281 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1282 struct et61x251_frame_t* f, * i;
1283 unsigned long lock_flags;
1284 int err = 0;
1285
1286 if (down_interruptible(&cam->fileop_sem))
1287 return -ERESTARTSYS;
1288
1289 if (cam->state & DEV_DISCONNECTED) {
1290 DBG(1, "Device not present");
1291 up(&cam->fileop_sem);
1292 return -ENODEV;
1293 }
1294
1295 if (cam->state & DEV_MISCONFIGURED) {
1296 DBG(1, "The camera is misconfigured. Close and open it "
1297 "again.");
1298 up(&cam->fileop_sem);
1299 return -EIO;
1300 }
1301
1302 if (cam->io == IO_MMAP) {
1303 DBG(3, "Close and open the device again to choose the read "
1304 "method");
1305 up(&cam->fileop_sem);
1306 return -EINVAL;
1307 }
1308
1309 if (cam->io == IO_NONE) {
1310 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1311 IO_READ)) {
1312 DBG(1, "read() failed, not enough memory");
1313 up(&cam->fileop_sem);
1314 return -ENOMEM;
1315 }
1316 cam->io = IO_READ;
1317 cam->stream = STREAM_ON;
1318 }
1319
1320 if (list_empty(&cam->inqueue)) {
1321 if (!list_empty(&cam->outqueue))
1322 et61x251_empty_framequeues(cam);
1323 et61x251_queue_unusedframes(cam);
1324 }
1325
1326 if (!count) {
1327 up(&cam->fileop_sem);
1328 return 0;
1329 }
1330
1331 if (list_empty(&cam->outqueue)) {
1332 if (filp->f_flags & O_NONBLOCK) {
1333 up(&cam->fileop_sem);
1334 return -EAGAIN;
1335 }
1336 err = wait_event_interruptible
1337 ( cam->wait_frame,
1338 (!list_empty(&cam->outqueue)) ||
1339 (cam->state & DEV_DISCONNECTED) ||
1340 (cam->state & DEV_MISCONFIGURED) );
1341 if (err) {
1342 up(&cam->fileop_sem);
1343 return err;
1344 }
1345 if (cam->state & DEV_DISCONNECTED) {
1346 up(&cam->fileop_sem);
1347 return -ENODEV;
1348 }
1349 if (cam->state & DEV_MISCONFIGURED) {
1350 up(&cam->fileop_sem);
1351 return -EIO;
1352 }
1353 }
1354
1355 f = list_entry(cam->outqueue.prev, struct et61x251_frame_t, frame);
1356
1357 if (count > f->buf.bytesused)
1358 count = f->buf.bytesused;
1359
1360 if (copy_to_user(buf, f->bufmem, count)) {
1361 err = -EFAULT;
1362 goto exit;
1363 }
1364 *f_pos += count;
1365
1366exit:
1367 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1368 list_for_each_entry(i, &cam->outqueue, frame)
1369 i->state = F_UNUSED;
1370 INIT_LIST_HEAD(&cam->outqueue);
1371 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1372
1373 et61x251_queue_unusedframes(cam);
1374
1375 PDBGG("Frame #%lu, bytes read: %zu",
1376 (unsigned long)f->buf.index, count);
1377
1378 up(&cam->fileop_sem);
1379
1380 return err ? err : count;
1381}
1382
1383
1384static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1385{
1386 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1387 struct et61x251_frame_t* f;
1388 unsigned long lock_flags;
1389 unsigned int mask = 0;
1390
1391 if (down_interruptible(&cam->fileop_sem))
1392 return POLLERR;
1393
1394 if (cam->state & DEV_DISCONNECTED) {
1395 DBG(1, "Device not present");
1396 goto error;
1397 }
1398
1399 if (cam->state & DEV_MISCONFIGURED) {
1400 DBG(1, "The camera is misconfigured. Close and open it "
1401 "again.");
1402 goto error;
1403 }
1404
1405 if (cam->io == IO_NONE) {
1406 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1407 IO_READ)) {
1408 DBG(1, "poll() failed, not enough memory");
1409 goto error;
1410 }
1411 cam->io = IO_READ;
1412 cam->stream = STREAM_ON;
1413 }
1414
1415 if (cam->io == IO_READ) {
1416 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1417 list_for_each_entry(f, &cam->outqueue, frame)
1418 f->state = F_UNUSED;
1419 INIT_LIST_HEAD(&cam->outqueue);
1420 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1421 et61x251_queue_unusedframes(cam);
1422 }
1423
1424 poll_wait(filp, &cam->wait_frame, wait);
1425
1426 if (!list_empty(&cam->outqueue))
1427 mask |= POLLIN | POLLRDNORM;
1428
1429 up(&cam->fileop_sem);
1430
1431 return mask;
1432
1433error:
1434 up(&cam->fileop_sem);
1435 return POLLERR;
1436}
1437
1438
1439static void et61x251_vm_open(struct vm_area_struct* vma)
1440{
1441 struct et61x251_frame_t* f = vma->vm_private_data;
1442 f->vma_use_count++;
1443}
1444
1445
1446static void et61x251_vm_close(struct vm_area_struct* vma)
1447{
1448 /* NOTE: buffers are not freed here */
1449 struct et61x251_frame_t* f = vma->vm_private_data;
1450 f->vma_use_count--;
1451}
1452
1453
1454static struct vm_operations_struct et61x251_vm_ops = {
1455 .open = et61x251_vm_open,
1456 .close = et61x251_vm_close,
1457};
1458
1459
1460static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1461{
1462 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1463 unsigned long size = vma->vm_end - vma->vm_start,
1464 start = vma->vm_start;
1465 void *pos;
1466 u32 i;
1467
1468 if (down_interruptible(&cam->fileop_sem))
1469 return -ERESTARTSYS;
1470
1471 if (cam->state & DEV_DISCONNECTED) {
1472 DBG(1, "Device not present");
1473 up(&cam->fileop_sem);
1474 return -ENODEV;
1475 }
1476
1477 if (cam->state & DEV_MISCONFIGURED) {
1478 DBG(1, "The camera is misconfigured. Close and open it "
1479 "again.");
1480 up(&cam->fileop_sem);
1481 return -EIO;
1482 }
1483
1484 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1485 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1486 up(&cam->fileop_sem);
1487 return -EINVAL;
1488 }
1489
1490 for (i = 0; i < cam->nbuffers; i++) {
1491 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1492 break;
1493 }
1494 if (i == cam->nbuffers) {
1495 up(&cam->fileop_sem);
1496 return -EINVAL;
1497 }
1498
1499 vma->vm_flags |= VM_IO;
1500 vma->vm_flags |= VM_RESERVED;
1501
1502 pos = cam->frame[i].bufmem;
1503 while (size > 0) { /* size is page-aligned */
1504 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1505 up(&cam->fileop_sem);
1506 return -EAGAIN;
1507 }
1508 start += PAGE_SIZE;
1509 pos += PAGE_SIZE;
1510 size -= PAGE_SIZE;
1511 }
1512
1513 vma->vm_ops = &et61x251_vm_ops;
1514 vma->vm_private_data = &cam->frame[i];
1515
1516 et61x251_vm_open(vma);
1517
1518 up(&cam->fileop_sem);
1519
1520 return 0;
1521}
1522
1523/*****************************************************************************/
1524
1525static int
1526et61x251_vidioc_querycap(struct et61x251_device* cam, void __user * arg)
1527{
1528 struct v4l2_capability cap = {
1529 .driver = "et61x251",
1530 .version = ET61X251_MODULE_VERSION_CODE,
1531 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1532 V4L2_CAP_STREAMING,
1533 };
1534
1535 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1536 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1537 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1538 sizeof(cap.bus_info));
1539
1540 if (copy_to_user(arg, &cap, sizeof(cap)))
1541 return -EFAULT;
1542
1543 return 0;
1544}
1545
1546
1547static int
1548et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1549{
1550 struct v4l2_input i;
1551
1552 if (copy_from_user(&i, arg, sizeof(i)))
1553 return -EFAULT;
1554
1555 if (i.index)
1556 return -EINVAL;
1557
1558 memset(&i, 0, sizeof(i));
1559 strcpy(i.name, "Camera");
1560
1561 if (copy_to_user(arg, &i, sizeof(i)))
1562 return -EFAULT;
1563
1564 return 0;
1565}
1566
1567
1568static int
1569et61x251_vidioc_gs_input(struct et61x251_device* cam, void __user * arg)
1570{
1571 int index;
1572
1573 if (copy_from_user(&index, arg, sizeof(index)))
1574 return -EFAULT;
1575
1576 if (index != 0)
1577 return -EINVAL;
1578
1579 return 0;
1580}
1581
1582
1583static int
1584et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1585{
1586 struct et61x251_sensor* s = cam->sensor;
1587 struct v4l2_queryctrl qc;
1588 u8 i;
1589
1590 if (copy_from_user(&qc, arg, sizeof(qc)))
1591 return -EFAULT;
1592
1593 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1594 if (qc.id && qc.id == s->qctrl[i].id) {
1595 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1596 if (copy_to_user(arg, &qc, sizeof(qc)))
1597 return -EFAULT;
1598 return 0;
1599 }
1600
1601 return -EINVAL;
1602}
1603
1604
1605static int
1606et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
1607{
1608 struct et61x251_sensor* s = cam->sensor;
1609 struct v4l2_control ctrl;
1610 int err = 0;
1611 u8 i;
1612
1613 if (!s->get_ctrl && !s->set_ctrl)
1614 return -EINVAL;
1615
1616 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1617 return -EFAULT;
1618
1619 if (!s->get_ctrl) {
1620 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1621 if (ctrl.id == s->qctrl[i].id) {
1622 ctrl.value = s->_qctrl[i].default_value;
1623 goto exit;
1624 }
1625 return -EINVAL;
1626 } else
1627 err = s->get_ctrl(cam, &ctrl);
1628
1629exit:
1630 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1631 return -EFAULT;
1632
1633 return err;
1634}
1635
1636
1637static int
1638et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1639{
1640 struct et61x251_sensor* s = cam->sensor;
1641 struct v4l2_control ctrl;
1642 u8 i;
1643 int err = 0;
1644
1645 if (!s->set_ctrl)
1646 return -EINVAL;
1647
1648 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1649 return -EFAULT;
1650
1651 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1652 if (ctrl.id == s->qctrl[i].id) {
1653 if (ctrl.value < s->qctrl[i].minimum ||
1654 ctrl.value > s->qctrl[i].maximum)
1655 return -ERANGE;
1656 ctrl.value -= ctrl.value % s->qctrl[i].step;
1657 break;
1658 }
1659
1660 if ((err = s->set_ctrl(cam, &ctrl)))
1661 return err;
1662
1663 s->_qctrl[i].default_value = ctrl.value;
1664
1665 return 0;
1666}
1667
1668
1669static int
1670et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1671{
1672 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1673
1674 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1675 cc->pixelaspect.numerator = 1;
1676 cc->pixelaspect.denominator = 1;
1677
1678 if (copy_to_user(arg, cc, sizeof(*cc)))
1679 return -EFAULT;
1680
1681 return 0;
1682}
1683
1684
1685static int
1686et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1687{
1688 struct et61x251_sensor* s = cam->sensor;
1689 struct v4l2_crop crop = {
1690 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1691 };
1692
1693 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1694
1695 if (copy_to_user(arg, &crop, sizeof(crop)))
1696 return -EFAULT;
1697
1698 return 0;
1699}
1700
1701
1702static int
1703et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
1704{
1705 struct et61x251_sensor* s = cam->sensor;
1706 struct v4l2_crop crop;
1707 struct v4l2_rect* rect;
1708 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1709 struct v4l2_pix_format* pix_format = &(s->pix_format);
1710 u8 scale;
1711 const enum et61x251_stream_state stream = cam->stream;
1712 const u32 nbuffers = cam->nbuffers;
1713 u32 i;
1714 int err = 0;
1715
1716 if (copy_from_user(&crop, arg, sizeof(crop)))
1717 return -EFAULT;
1718
1719 rect = &(crop.c);
1720
1721 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1722 return -EINVAL;
1723
1724 if (cam->module_param.force_munmap)
1725 for (i = 0; i < cam->nbuffers; i++)
1726 if (cam->frame[i].vma_use_count) {
1727 DBG(3, "VIDIOC_S_CROP failed. "
1728 "Unmap the buffers first.");
1729 return -EINVAL;
1730 }
1731
1732 /* Preserve R,G or B origin */
1733 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1734 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1735
1736 if (rect->width < 4)
1737 rect->width = 4;
1738 if (rect->height < 4)
1739 rect->height = 4;
1740 if (rect->width > bounds->width)
1741 rect->width = bounds->width;
1742 if (rect->height > bounds->height)
1743 rect->height = bounds->height;
1744 if (rect->left < bounds->left)
1745 rect->left = bounds->left;
1746 if (rect->top < bounds->top)
1747 rect->top = bounds->top;
1748 if (rect->left + rect->width > bounds->left + bounds->width)
1749 rect->left = bounds->left+bounds->width - rect->width;
1750 if (rect->top + rect->height > bounds->top + bounds->height)
1751 rect->top = bounds->top+bounds->height - rect->height;
1752
1753 rect->width &= ~3L;
1754 rect->height &= ~3L;
1755
1756 if (ET61X251_PRESERVE_IMGSCALE) {
1757 /* Calculate the actual scaling factor */
1758 u32 a, b;
1759 a = rect->width * rect->height;
1760 b = pix_format->width * pix_format->height;
1761 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1762 } else
1763 scale = 1;
1764
1765 if (cam->stream == STREAM_ON)
1766 if ((err = et61x251_stream_interrupt(cam)))
1767 return err;
1768
1769 if (copy_to_user(arg, &crop, sizeof(crop))) {
1770 cam->stream = stream;
1771 return -EFAULT;
1772 }
1773
1774 if (cam->module_param.force_munmap || cam->io == IO_READ)
1775 et61x251_release_buffers(cam);
1776
1777 err = et61x251_set_crop(cam, rect);
1778 if (s->set_crop)
1779 err += s->set_crop(cam, rect);
1780 err += et61x251_set_scale(cam, scale);
1781
1782 if (err) { /* atomic, no rollback in ioctl() */
1783 cam->state |= DEV_MISCONFIGURED;
1784 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1785 "use the camera, close and open /dev/video%d again.",
1786 cam->v4ldev->minor);
1787 return -EIO;
1788 }
1789
1790 s->pix_format.width = rect->width/scale;
1791 s->pix_format.height = rect->height/scale;
1792 memcpy(&(s->_rect), rect, sizeof(*rect));
1793
1794 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1795 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
1796 cam->state |= DEV_MISCONFIGURED;
1797 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1798 "use the camera, close and open /dev/video%d again.",
1799 cam->v4ldev->minor);
1800 return -ENOMEM;
1801 }
1802
1803 if (cam->io == IO_READ)
1804 et61x251_empty_framequeues(cam);
1805 else if (cam->module_param.force_munmap)
1806 et61x251_requeue_outqueue(cam);
1807
1808 cam->stream = stream;
1809
1810 return 0;
1811}
1812
1813
1814static int
1815et61x251_vidioc_enum_fmt(struct et61x251_device* cam, void __user * arg)
1816{
1817 struct v4l2_fmtdesc fmtd;
1818
1819 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1820 return -EFAULT;
1821
1822 if (fmtd.index == 0) {
1823 strcpy(fmtd.description, "bayer rgb");
1824 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1825 } else if (fmtd.index == 1) {
1826 strcpy(fmtd.description, "compressed");
1827 fmtd.pixelformat = V4L2_PIX_FMT_ET61X251;
1828 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1829 } else
1830 return -EINVAL;
1831
1832 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1833 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1834
1835 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1836 return -EFAULT;
1837
1838 return 0;
1839}
1840
1841
1842static int
1843et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
1844{
1845 struct v4l2_format format;
1846 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
1847
1848 if (copy_from_user(&format, arg, sizeof(format)))
1849 return -EFAULT;
1850
1851 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1852 return -EINVAL;
1853
1854 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_ET61X251)
1855 ? 0 : (pfmt->width * pfmt->priv) / 8;
1856 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1857 pfmt->field = V4L2_FIELD_NONE;
1858 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1859
1860 if (copy_to_user(arg, &format, sizeof(format)))
1861 return -EFAULT;
1862
1863 return 0;
1864}
1865
1866
1867static int
1868et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
1869 void __user * arg)
1870{
1871 struct et61x251_sensor* s = cam->sensor;
1872 struct v4l2_format format;
1873 struct v4l2_pix_format* pix;
1874 struct v4l2_pix_format* pfmt = &(s->pix_format);
1875 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1876 struct v4l2_rect rect;
1877 u8 scale;
1878 const enum et61x251_stream_state stream = cam->stream;
1879 const u32 nbuffers = cam->nbuffers;
1880 u32 i;
1881 int err = 0;
1882
1883 if (copy_from_user(&format, arg, sizeof(format)))
1884 return -EFAULT;
1885
1886 pix = &(format.fmt.pix);
1887
1888 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1889 return -EINVAL;
1890
1891 memcpy(&rect, &(s->_rect), sizeof(rect));
1892
1893 { /* calculate the actual scaling factor */
1894 u32 a, b;
1895 a = rect.width * rect.height;
1896 b = pix->width * pix->height;
1897 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1898 }
1899
1900 rect.width = scale * pix->width;
1901 rect.height = scale * pix->height;
1902
1903 if (rect.width < 4)
1904 rect.width = 4;
1905 if (rect.height < 4)
1906 rect.height = 4;
1907 if (rect.width > bounds->left + bounds->width - rect.left)
1908 rect.width = bounds->left + bounds->width - rect.left;
1909 if (rect.height > bounds->top + bounds->height - rect.top)
1910 rect.height = bounds->top + bounds->height - rect.top;
1911
1912 rect.width &= ~3L;
1913 rect.height &= ~3L;
1914
1915 { /* adjust the scaling factor */
1916 u32 a, b;
1917 a = rect.width * rect.height;
1918 b = pix->width * pix->height;
1919 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1920 }
1921
1922 pix->width = rect.width / scale;
1923 pix->height = rect.height / scale;
1924
1925 if (pix->pixelformat != V4L2_PIX_FMT_ET61X251 &&
1926 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
1927 pix->pixelformat = pfmt->pixelformat;
1928 pix->priv = pfmt->priv; /* bpp */
1929 pix->colorspace = pfmt->colorspace;
1930 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1931 ? 0 : (pix->width * pix->priv) / 8;
1932 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
1933 pix->field = V4L2_FIELD_NONE;
1934
1935 if (cmd == VIDIOC_TRY_FMT) {
1936 if (copy_to_user(arg, &format, sizeof(format)))
1937 return -EFAULT;
1938 return 0;
1939 }
1940
1941 if (cam->module_param.force_munmap)
1942 for (i = 0; i < cam->nbuffers; i++)
1943 if (cam->frame[i].vma_use_count) {
1944 DBG(3, "VIDIOC_S_FMT failed. "
1945 "Unmap the buffers first.");
1946 return -EINVAL;
1947 }
1948
1949 if (cam->stream == STREAM_ON)
1950 if ((err = et61x251_stream_interrupt(cam)))
1951 return err;
1952
1953 if (copy_to_user(arg, &format, sizeof(format))) {
1954 cam->stream = stream;
1955 return -EFAULT;
1956 }
1957
1958 if (cam->module_param.force_munmap || cam->io == IO_READ)
1959 et61x251_release_buffers(cam);
1960
1961 err += et61x251_set_pix_format(cam, pix);
1962 err += et61x251_set_crop(cam, &rect);
1963 if (s->set_pix_format)
1964 err += s->set_pix_format(cam, pix);
1965 if (s->set_crop)
1966 err += s->set_crop(cam, &rect);
1967 err += et61x251_set_scale(cam, scale);
1968
1969 if (err) { /* atomic, no rollback in ioctl() */
1970 cam->state |= DEV_MISCONFIGURED;
1971 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
1972 "use the camera, close and open /dev/video%d again.",
1973 cam->v4ldev->minor);
1974 return -EIO;
1975 }
1976
1977 memcpy(pfmt, pix, sizeof(*pix));
1978 memcpy(&(s->_rect), &rect, sizeof(rect));
1979
1980 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1981 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
1982 cam->state |= DEV_MISCONFIGURED;
1983 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
1984 "use the camera, close and open /dev/video%d again.",
1985 cam->v4ldev->minor);
1986 return -ENOMEM;
1987 }
1988
1989 if (cam->io == IO_READ)
1990 et61x251_empty_framequeues(cam);
1991 else if (cam->module_param.force_munmap)
1992 et61x251_requeue_outqueue(cam);
1993
1994 cam->stream = stream;
1995
1996 return 0;
1997}
1998
1999
2000static int
2001et61x251_vidioc_g_jpegcomp(struct et61x251_device* cam, void __user * arg)
2002{
2003 if (copy_to_user(arg, &cam->compression,
2004 sizeof(cam->compression)))
2005 return -EFAULT;
2006
2007 return 0;
2008}
2009
2010
2011static int
2012et61x251_vidioc_s_jpegcomp(struct et61x251_device* cam, void __user * arg)
2013{
2014 struct v4l2_jpegcompression jc;
2015 const enum et61x251_stream_state stream = cam->stream;
2016 int err = 0;
2017
2018 if (copy_from_user(&jc, arg, sizeof(jc)))
2019 return -EFAULT;
2020
2021 if (jc.quality != 0 && jc.quality != 1)
2022 return -EINVAL;
2023
2024 if (cam->stream == STREAM_ON)
2025 if ((err = et61x251_stream_interrupt(cam)))
2026 return err;
2027
2028 err += et61x251_set_compression(cam, &jc);
2029 if (err) { /* atomic, no rollback in ioctl() */
2030 cam->state |= DEV_MISCONFIGURED;
2031 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2032 "problems. To use the camera, close and open "
2033 "/dev/video%d again.", cam->v4ldev->minor);
2034 return -EIO;
2035 }
2036
2037 cam->compression.quality = jc.quality;
2038
2039 cam->stream = stream;
2040
2041 return 0;
2042}
2043
2044
2045static int
2046et61x251_vidioc_reqbufs(struct et61x251_device* cam, void __user * arg)
2047{
2048 struct v4l2_requestbuffers rb;
2049 u32 i;
2050 int err;
2051
2052 if (copy_from_user(&rb, arg, sizeof(rb)))
2053 return -EFAULT;
2054
2055 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2056 rb.memory != V4L2_MEMORY_MMAP)
2057 return -EINVAL;
2058
2059 if (cam->io == IO_READ) {
2060 DBG(3, "Close and open the device again to choose the mmap "
2061 "I/O method");
2062 return -EINVAL;
2063 }
2064
2065 for (i = 0; i < cam->nbuffers; i++)
2066 if (cam->frame[i].vma_use_count) {
2067 DBG(3, "VIDIOC_REQBUFS failed. "
2068 "Previous buffers are still mapped.");
2069 return -EINVAL;
2070 }
2071
2072 if (cam->stream == STREAM_ON)
2073 if ((err = et61x251_stream_interrupt(cam)))
2074 return err;
2075
2076 et61x251_empty_framequeues(cam);
2077
2078 et61x251_release_buffers(cam);
2079 if (rb.count)
2080 rb.count = et61x251_request_buffers(cam, rb.count, IO_MMAP);
2081
2082 if (copy_to_user(arg, &rb, sizeof(rb))) {
2083 et61x251_release_buffers(cam);
2084 cam->io = IO_NONE;
2085 return -EFAULT;
2086 }
2087
2088 cam->io = rb.count ? IO_MMAP : IO_NONE;
2089
2090 return 0;
2091}
2092
2093
2094static int
2095et61x251_vidioc_querybuf(struct et61x251_device* cam, void __user * arg)
2096{
2097 struct v4l2_buffer b;
2098
2099 if (copy_from_user(&b, arg, sizeof(b)))
2100 return -EFAULT;
2101
2102 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2103 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2104 return -EINVAL;
2105
2106 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2107
2108 if (cam->frame[b.index].vma_use_count)
2109 b.flags |= V4L2_BUF_FLAG_MAPPED;
2110
2111 if (cam->frame[b.index].state == F_DONE)
2112 b.flags |= V4L2_BUF_FLAG_DONE;
2113 else if (cam->frame[b.index].state != F_UNUSED)
2114 b.flags |= V4L2_BUF_FLAG_QUEUED;
2115
2116 if (copy_to_user(arg, &b, sizeof(b)))
2117 return -EFAULT;
2118
2119 return 0;
2120}
2121
2122
2123static int
2124et61x251_vidioc_qbuf(struct et61x251_device* cam, void __user * arg)
2125{
2126 struct v4l2_buffer b;
2127 unsigned long lock_flags;
2128
2129 if (copy_from_user(&b, arg, sizeof(b)))
2130 return -EFAULT;
2131
2132 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2133 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2134 return -EINVAL;
2135
2136 if (cam->frame[b.index].state != F_UNUSED)
2137 return -EINVAL;
2138
2139 cam->frame[b.index].state = F_QUEUED;
2140
2141 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2142 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2143 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2144
2145 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2146
2147 return 0;
2148}
2149
2150
2151static int
2152et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2153 void __user * arg)
2154{
2155 struct v4l2_buffer b;
2156 struct et61x251_frame_t *f;
2157 unsigned long lock_flags;
2158 int err = 0;
2159
2160 if (copy_from_user(&b, arg, sizeof(b)))
2161 return -EFAULT;
2162
2163 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2164 return -EINVAL;
2165
2166 if (list_empty(&cam->outqueue)) {
2167 if (cam->stream == STREAM_OFF)
2168 return -EINVAL;
2169 if (filp->f_flags & O_NONBLOCK)
2170 return -EAGAIN;
2171 err = wait_event_interruptible
2172 ( cam->wait_frame,
2173 (!list_empty(&cam->outqueue)) ||
2174 (cam->state & DEV_DISCONNECTED) ||
2175 (cam->state & DEV_MISCONFIGURED) );
2176 if (err)
2177 return err;
2178 if (cam->state & DEV_DISCONNECTED)
2179 return -ENODEV;
2180 if (cam->state & DEV_MISCONFIGURED)
2181 return -EIO;
2182 }
2183
2184 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2185 f = list_entry(cam->outqueue.next, struct et61x251_frame_t, frame);
2186 list_del(cam->outqueue.next);
2187 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2188
2189 f->state = F_UNUSED;
2190
2191 memcpy(&b, &f->buf, sizeof(b));
2192 if (f->vma_use_count)
2193 b.flags |= V4L2_BUF_FLAG_MAPPED;
2194
2195 if (copy_to_user(arg, &b, sizeof(b)))
2196 return -EFAULT;
2197
2198 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2199
2200 return 0;
2201}
2202
2203
2204static int
2205et61x251_vidioc_streamon(struct et61x251_device* cam, void __user * arg)
2206{
2207 int type;
2208
2209 if (copy_from_user(&type, arg, sizeof(type)))
2210 return -EFAULT;
2211
2212 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2213 return -EINVAL;
2214
2215 if (list_empty(&cam->inqueue))
2216 return -EINVAL;
2217
2218 cam->stream = STREAM_ON;
2219
2220 DBG(3, "Stream on");
2221
2222 return 0;
2223}
2224
2225
2226static int
2227et61x251_vidioc_streamoff(struct et61x251_device* cam, void __user * arg)
2228{
2229 int type, err;
2230
2231 if (copy_from_user(&type, arg, sizeof(type)))
2232 return -EFAULT;
2233
2234 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2235 return -EINVAL;
2236
2237 if (cam->stream == STREAM_ON)
2238 if ((err = et61x251_stream_interrupt(cam)))
2239 return err;
2240
2241 et61x251_empty_framequeues(cam);
2242
2243 DBG(3, "Stream off");
2244
2245 return 0;
2246}
2247
2248
2249static int
2250et61x251_vidioc_g_parm(struct et61x251_device* cam, void __user * arg)
2251{
2252 struct v4l2_streamparm sp;
2253
2254 if (copy_from_user(&sp, arg, sizeof(sp)))
2255 return -EFAULT;
2256
2257 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2258 return -EINVAL;
2259
2260 sp.parm.capture.extendedmode = 0;
2261 sp.parm.capture.readbuffers = cam->nreadbuffers;
2262
2263 if (copy_to_user(arg, &sp, sizeof(sp)))
2264 return -EFAULT;
2265
2266 return 0;
2267}
2268
2269
2270static int
2271et61x251_vidioc_s_parm(struct et61x251_device* cam, void __user * arg)
2272{
2273 struct v4l2_streamparm sp;
2274
2275 if (copy_from_user(&sp, arg, sizeof(sp)))
2276 return -EFAULT;
2277
2278 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2279 return -EINVAL;
2280
2281 sp.parm.capture.extendedmode = 0;
2282
2283 if (sp.parm.capture.readbuffers == 0)
2284 sp.parm.capture.readbuffers = cam->nreadbuffers;
2285
2286 if (sp.parm.capture.readbuffers > ET61X251_MAX_FRAMES)
2287 sp.parm.capture.readbuffers = ET61X251_MAX_FRAMES;
2288
2289 if (copy_to_user(arg, &sp, sizeof(sp)))
2290 return -EFAULT;
2291
2292 cam->nreadbuffers = sp.parm.capture.readbuffers;
2293
2294 return 0;
2295}
2296
2297
2298static int et61x251_ioctl_v4l2(struct inode* inode, struct file* filp,
2299 unsigned int cmd, void __user * arg)
2300{
2301 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2302
2303 switch (cmd) {
2304
2305 case VIDIOC_QUERYCAP:
2306 return et61x251_vidioc_querycap(cam, arg);
2307
2308 case VIDIOC_ENUMINPUT:
2309 return et61x251_vidioc_enuminput(cam, arg);
2310
2311 case VIDIOC_G_INPUT:
2312 case VIDIOC_S_INPUT:
2313 return et61x251_vidioc_gs_input(cam, arg);
2314
2315 case VIDIOC_QUERYCTRL:
2316 return et61x251_vidioc_query_ctrl(cam, arg);
2317
2318 case VIDIOC_G_CTRL:
2319 return et61x251_vidioc_g_ctrl(cam, arg);
2320
2321 case VIDIOC_S_CTRL_OLD:
2322 case VIDIOC_S_CTRL:
2323 return et61x251_vidioc_s_ctrl(cam, arg);
2324
2325 case VIDIOC_CROPCAP_OLD:
2326 case VIDIOC_CROPCAP:
2327 return et61x251_vidioc_cropcap(cam, arg);
2328
2329 case VIDIOC_G_CROP:
2330 return et61x251_vidioc_g_crop(cam, arg);
2331
2332 case VIDIOC_S_CROP:
2333 return et61x251_vidioc_s_crop(cam, arg);
2334
2335 case VIDIOC_ENUM_FMT:
2336 return et61x251_vidioc_enum_fmt(cam, arg);
2337
2338 case VIDIOC_G_FMT:
2339 return et61x251_vidioc_g_fmt(cam, arg);
2340
2341 case VIDIOC_TRY_FMT:
2342 case VIDIOC_S_FMT:
2343 return et61x251_vidioc_try_s_fmt(cam, cmd, arg);
2344
2345 case VIDIOC_G_JPEGCOMP:
2346 return et61x251_vidioc_g_jpegcomp(cam, arg);
2347
2348 case VIDIOC_S_JPEGCOMP:
2349 return et61x251_vidioc_s_jpegcomp(cam, arg);
2350
2351 case VIDIOC_REQBUFS:
2352 return et61x251_vidioc_reqbufs(cam, arg);
2353
2354 case VIDIOC_QUERYBUF:
2355 return et61x251_vidioc_querybuf(cam, arg);
2356
2357 case VIDIOC_QBUF:
2358 return et61x251_vidioc_qbuf(cam, arg);
2359
2360 case VIDIOC_DQBUF:
2361 return et61x251_vidioc_dqbuf(cam, filp, arg);
2362
2363 case VIDIOC_STREAMON:
2364 return et61x251_vidioc_streamon(cam, arg);
2365
2366 case VIDIOC_STREAMOFF:
2367 return et61x251_vidioc_streamoff(cam, arg);
2368
2369 case VIDIOC_G_PARM:
2370 return et61x251_vidioc_g_parm(cam, arg);
2371
2372 case VIDIOC_S_PARM_OLD:
2373 case VIDIOC_S_PARM:
2374 return et61x251_vidioc_s_parm(cam, arg);
2375
2376 case VIDIOC_G_STD:
2377 case VIDIOC_S_STD:
2378 case VIDIOC_QUERYSTD:
2379 case VIDIOC_ENUMSTD:
2380 case VIDIOC_QUERYMENU:
2381 return -EINVAL;
2382
2383 default:
2384 return -EINVAL;
2385
2386 }
2387}
2388
2389
2390static int et61x251_ioctl(struct inode* inode, struct file* filp,
2391 unsigned int cmd, unsigned long arg)
2392{
2393 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2394 int err = 0;
2395
2396 if (down_interruptible(&cam->fileop_sem))
2397 return -ERESTARTSYS;
2398
2399 if (cam->state & DEV_DISCONNECTED) {
2400 DBG(1, "Device not present");
2401 up(&cam->fileop_sem);
2402 return -ENODEV;
2403 }
2404
2405 if (cam->state & DEV_MISCONFIGURED) {
2406 DBG(1, "The camera is misconfigured. Close and open it "
2407 "again.");
2408 up(&cam->fileop_sem);
2409 return -EIO;
2410 }
2411
2412 V4LDBG(3, "et61x251", cmd);
2413
2414 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2415
2416 up(&cam->fileop_sem);
2417
2418 return err;
2419}
2420
2421
2422static struct file_operations et61x251_fops = {
2423 .owner = THIS_MODULE,
2424 .open = et61x251_open,
2425 .release = et61x251_release,
2426 .ioctl = et61x251_ioctl,
2427 .read = et61x251_read,
2428 .poll = et61x251_poll,
2429 .mmap = et61x251_mmap,
2430 .llseek = no_llseek,
2431};
2432
2433/*****************************************************************************/
2434
2435/* It exists a single interface only. We do not need to validate anything. */
2436static int
2437et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2438{
2439 struct usb_device *udev = interface_to_usbdev(intf);
2440 struct et61x251_device* cam;
2441 static unsigned int dev_nr = 0;
2442 unsigned int i;
2443 int err = 0;
2444
2445 if (!(cam = kzalloc(sizeof(struct et61x251_device), GFP_KERNEL)))
2446 return -ENOMEM;
2447
2448 cam->usbdev = udev;
2449
2450 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2451 DBG(1, "kmalloc() failed");
2452 err = -ENOMEM;
2453 goto fail;
2454 }
2455
2456 if (!(cam->v4ldev = video_device_alloc())) {
2457 DBG(1, "video_device_alloc() failed");
2458 err = -ENOMEM;
2459 goto fail;
2460 }
2461
2462 init_MUTEX(&cam->dev_sem);
2463
2464 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2465 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct);
2466
2467 for (i = 0; et61x251_sensor_table[i]; i++) {
2468 err = et61x251_sensor_table[i](cam);
2469 if (!err)
2470 break;
2471 }
2472
2473 if (!err && cam->sensor)
2474 DBG(2, "%s image sensor detected", cam->sensor->name);
2475 else {
2476 DBG(1, "No supported image sensor detected");
2477 err = -ENODEV;
2478 goto fail;
2479 }
2480
2481 if (et61x251_init(cam)) {
2482 DBG(1, "Initialization failed. I will retry on open().");
2483 cam->state |= DEV_MISCONFIGURED;
2484 }
2485
2486 strcpy(cam->v4ldev->name, "ET61X[12]51 PC Camera");
2487 cam->v4ldev->owner = THIS_MODULE;
2488 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2489 cam->v4ldev->hardware = 0;
2490 cam->v4ldev->fops = &et61x251_fops;
2491 cam->v4ldev->minor = video_nr[dev_nr];
2492 cam->v4ldev->release = video_device_release;
2493 video_set_drvdata(cam->v4ldev, cam);
2494
2495 down(&cam->dev_sem);
2496
2497 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2498 video_nr[dev_nr]);
2499 if (err) {
2500 DBG(1, "V4L2 device registration failed");
2501 if (err == -ENFILE && video_nr[dev_nr] == -1)
2502 DBG(1, "Free /dev/videoX node not found");
2503 video_nr[dev_nr] = -1;
2504 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2505 up(&cam->dev_sem);
2506 goto fail;
2507 }
2508
2509 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2510
2511 cam->module_param.force_munmap = force_munmap[dev_nr];
2512
2513 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2514
2515#ifdef CONFIG_VIDEO_ADV_DEBUG
2516 et61x251_create_sysfs(cam);
2517 DBG(2, "Optional device control through 'sysfs' interface ready");
2518#endif
2519
2520 usb_set_intfdata(intf, cam);
2521
2522 up(&cam->dev_sem);
2523
2524 return 0;
2525
2526fail:
2527 if (cam) {
2528 kfree(cam->control_buffer);
2529 if (cam->v4ldev)
2530 video_device_release(cam->v4ldev);
2531 kfree(cam);
2532 }
2533 return err;
2534}
2535
2536
2537static void et61x251_usb_disconnect(struct usb_interface* intf)
2538{
2539 struct et61x251_device* cam = usb_get_intfdata(intf);
2540
2541 if (!cam)
2542 return;
2543
2544 down_write(&et61x251_disconnect);
2545
2546 down(&cam->dev_sem);
2547
2548 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2549
2550 wake_up_interruptible_all(&cam->open);
2551
2552 if (cam->users) {
2553 DBG(2, "Device /dev/video%d is open! Deregistration and "
2554 "memory deallocation are deferred on close.",
2555 cam->v4ldev->minor);
2556 cam->state |= DEV_MISCONFIGURED;
2557 et61x251_stop_transfer(cam);
2558 cam->state |= DEV_DISCONNECTED;
2559 wake_up_interruptible(&cam->wait_frame);
2560 wake_up_interruptible(&cam->wait_stream);
2561 } else {
2562 cam->state |= DEV_DISCONNECTED;
2563 et61x251_release_resources(cam);
2564 }
2565
2566 up(&cam->dev_sem);
2567
2568 if (!cam->users)
2569 kfree(cam);
2570
2571 up_write(&et61x251_disconnect);
2572}
2573
2574
2575static struct usb_driver et61x251_usb_driver = {
2576 .name = "et61x251",
2577 .id_table = et61x251_id_table,
2578 .probe = et61x251_usb_probe,
2579 .disconnect = et61x251_usb_disconnect,
2580};
2581
2582/*****************************************************************************/
2583
2584static int __init et61x251_module_init(void)
2585{
2586 int err = 0;
2587
2588 KDBG(2, ET61X251_MODULE_NAME " v" ET61X251_MODULE_VERSION);
2589 KDBG(3, ET61X251_MODULE_AUTHOR);
2590
2591 if ((err = usb_register(&et61x251_usb_driver)))
2592 KDBG(1, "usb_register() failed");
2593
2594 return err;
2595}
2596
2597
2598static void __exit et61x251_module_exit(void)
2599{
2600 usb_deregister(&et61x251_usb_driver);
2601}
2602
2603
2604module_init(et61x251_module_init);
2605module_exit(et61x251_module_exit);
diff --git a/drivers/usb/media/et61x251_sensor.h b/drivers/usb/media/et61x251_sensor.h
deleted file mode 100644
index b9df91062fc0..000000000000
--- a/drivers/usb/media/et61x251_sensor.h
+++ /dev/null
@@ -1,115 +0,0 @@
1/***************************************************************************
2 * API for image sensors connected to ET61X[12]51 PC Camera Controllers *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _ET61X251_SENSOR_H_
22#define _ET61X251_SENSOR_H_
23
24#include <linux/usb.h>
25#include <linux/videodev.h>
26#include <linux/device.h>
27#include <linux/stddef.h>
28#include <linux/errno.h>
29#include <asm/types.h>
30
31struct et61x251_device;
32struct et61x251_sensor;
33
34/*****************************************************************************/
35
36extern int et61x251_probe_tas5130d1b(struct et61x251_device* cam);
37
38#define ET61X251_SENSOR_TABLE \
39/* Weak detections must go at the end of the list */ \
40static int (*et61x251_sensor_table[])(struct et61x251_device*) = { \
41 &et61x251_probe_tas5130d1b, \
42 NULL, \
43};
44
45extern void
46et61x251_attach_sensor(struct et61x251_device* cam,
47 struct et61x251_sensor* sensor);
48
49/*****************************************************************************/
50
51extern int et61x251_write_reg(struct et61x251_device*, u8 value, u16 index);
52extern int et61x251_read_reg(struct et61x251_device*, u16 index);
53extern int et61x251_i2c_write(struct et61x251_device*, u8 address, u8 value);
54extern int et61x251_i2c_read(struct et61x251_device*, u8 address);
55extern int et61x251_i2c_try_write(struct et61x251_device*,
56 struct et61x251_sensor*, u8 address,
57 u8 value);
58extern int et61x251_i2c_try_read(struct et61x251_device*,
59 struct et61x251_sensor*, u8 address);
60extern int et61x251_i2c_raw_write(struct et61x251_device*, u8 n, u8 data1,
61 u8 data2, u8 data3, u8 data4, u8 data5,
62 u8 data6, u8 data7, u8 data8, u8 address);
63
64/*****************************************************************************/
65
66enum et61x251_i2c_sysfs_ops {
67 ET61X251_I2C_READ = 0x01,
68 ET61X251_I2C_WRITE = 0x02,
69};
70
71enum et61x251_i2c_interface {
72 ET61X251_I2C_2WIRES,
73 ET61X251_I2C_3WIRES,
74};
75
76/* Repeat start condition when RSTA is high */
77enum et61x251_i2c_rsta {
78 ET61X251_I2C_RSTA_STOP = 0x00, /* stop then start */
79 ET61X251_I2C_RSTA_REPEAT = 0x01, /* repeat start */
80};
81
82#define ET61X251_MAX_CTRLS V4L2_CID_LASTP1-V4L2_CID_BASE+10
83
84struct et61x251_sensor {
85 char name[32];
86
87 enum et61x251_i2c_sysfs_ops sysfs_ops;
88
89 enum et61x251_i2c_interface interface;
90 u8 i2c_slave_id;
91 enum et61x251_i2c_rsta rsta;
92 struct v4l2_rect active_pixel; /* left and top define FVSX and FVSY */
93
94 struct v4l2_queryctrl qctrl[ET61X251_MAX_CTRLS];
95 struct v4l2_cropcap cropcap;
96 struct v4l2_pix_format pix_format;
97
98 int (*init)(struct et61x251_device* cam);
99 int (*get_ctrl)(struct et61x251_device* cam,
100 struct v4l2_control* ctrl);
101 int (*set_ctrl)(struct et61x251_device* cam,
102 const struct v4l2_control* ctrl);
103 int (*set_crop)(struct et61x251_device* cam,
104 const struct v4l2_rect* rect);
105 int (*set_pix_format)(struct et61x251_device* cam,
106 const struct v4l2_pix_format* pix);
107
108 const struct usb_device* usbdev;
109
110 /* Private */
111 struct v4l2_queryctrl _qctrl[ET61X251_MAX_CTRLS];
112 struct v4l2_rect _rect;
113};
114
115#endif /* _ET61X251_SENSOR_H_ */
diff --git a/drivers/usb/media/et61x251_tas5130d1b.c b/drivers/usb/media/et61x251_tas5130d1b.c
deleted file mode 100644
index 65f1ae9cf2b3..000000000000
--- a/drivers/usb/media/et61x251_tas5130d1b.c
+++ /dev/null
@@ -1,137 +0,0 @@
1/***************************************************************************
2 * Plug-in for TAS5130D1B image sensor connected to the ET61X[12]51 *
3 * PC Camera Controllers *
4 * *
5 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#include "et61x251_sensor.h"
23
24
25static int tas5130d1b_init(struct et61x251_device* cam)
26{
27 int err = 0;
28
29 err += et61x251_write_reg(cam, 0x14, 0x01);
30 err += et61x251_write_reg(cam, 0x1b, 0x02);
31 err += et61x251_write_reg(cam, 0x02, 0x12);
32 err += et61x251_write_reg(cam, 0x0e, 0x60);
33 err += et61x251_write_reg(cam, 0x80, 0x61);
34 err += et61x251_write_reg(cam, 0xf0, 0x62);
35 err += et61x251_write_reg(cam, 0x03, 0x63);
36 err += et61x251_write_reg(cam, 0x14, 0x64);
37 err += et61x251_write_reg(cam, 0xf4, 0x65);
38 err += et61x251_write_reg(cam, 0x01, 0x66);
39 err += et61x251_write_reg(cam, 0x05, 0x67);
40 err += et61x251_write_reg(cam, 0x8f, 0x68);
41 err += et61x251_write_reg(cam, 0x0f, 0x8d);
42 err += et61x251_write_reg(cam, 0x08, 0x8e);
43
44 return err;
45}
46
47
48static int tas5130d1b_set_ctrl(struct et61x251_device* cam,
49 const struct v4l2_control* ctrl)
50{
51 int err = 0;
52
53 switch (ctrl->id) {
54 case V4L2_CID_GAIN:
55 err += et61x251_i2c_raw_write(cam, 2, 0x20,
56 0xf6-ctrl->value, 0, 0, 0,
57 0, 0, 0, 0);
58 break;
59 case V4L2_CID_EXPOSURE:
60 err += et61x251_i2c_raw_write(cam, 2, 0x40,
61 0x47-ctrl->value, 0, 0, 0,
62 0, 0, 0, 0);
63 break;
64 default:
65 return -EINVAL;
66 }
67
68 return err ? -EIO : 0;
69}
70
71
72static struct et61x251_sensor tas5130d1b = {
73 .name = "TAS5130D1B",
74 .interface = ET61X251_I2C_3WIRES,
75 .rsta = ET61X251_I2C_RSTA_STOP,
76 .active_pixel = {
77 .left = 106,
78 .top = 13,
79 },
80 .init = &tas5130d1b_init,
81 .qctrl = {
82 {
83 .id = V4L2_CID_GAIN,
84 .type = V4L2_CTRL_TYPE_INTEGER,
85 .name = "global gain",
86 .minimum = 0x00,
87 .maximum = 0xf6,
88 .step = 0x02,
89 .default_value = 0x0d,
90 .flags = 0,
91 },
92 {
93 .id = V4L2_CID_EXPOSURE,
94 .type = V4L2_CTRL_TYPE_INTEGER,
95 .name = "exposure",
96 .minimum = 0x00,
97 .maximum = 0x47,
98 .step = 0x01,
99 .default_value = 0x23,
100 .flags = 0,
101 },
102 },
103 .set_ctrl = &tas5130d1b_set_ctrl,
104 .cropcap = {
105 .bounds = {
106 .left = 0,
107 .top = 0,
108 .width = 640,
109 .height = 480,
110 },
111 .defrect = {
112 .left = 0,
113 .top = 0,
114 .width = 640,
115 .height = 480,
116 },
117 },
118 .pix_format = {
119 .width = 640,
120 .height = 480,
121 .pixelformat = V4L2_PIX_FMT_SBGGR8,
122 .priv = 8,
123 },
124};
125
126
127int et61x251_probe_tas5130d1b(struct et61x251_device* cam)
128{
129 /* This sensor has no identifiers, so let's attach it anyway */
130 et61x251_attach_sensor(cam, &tas5130d1b);
131
132 /* Sensor detection is based on USB pid/vid */
133 if (le16_to_cpu(tas5130d1b.usbdev->descriptor.idProduct) != 0x6251)
134 return -ENODEV;
135
136 return 0;
137}
diff --git a/drivers/usb/media/ibmcam.c b/drivers/usb/media/ibmcam.c
deleted file mode 100644
index a42c22294124..000000000000
--- a/drivers/usb/media/ibmcam.c
+++ /dev/null
@@ -1,3932 +0,0 @@
1/*
2 * USB IBM C-It Video Camera driver
3 *
4 * Supports Xirlink C-It Video Camera, IBM PC Camera,
5 * IBM NetCamera and Veo Stingray.
6 *
7 * This driver is based on earlier work of:
8 *
9 * (C) Copyright 1999 Johannes Erdfelt
10 * (C) Copyright 1999 Randy Dunlap
11 *
12 * 5/24/00 Removed optional (and unnecessary) locking of the driver while
13 * the device remains plugged in. Corrected race conditions in ibmcam_open
14 * and ibmcam_probe() routines using this as a guideline:
15 */
16
17#include <linux/kernel.h>
18#include <linux/sched.h>
19#include <linux/module.h>
20#include <linux/init.h>
21
22#include "usbvideo.h"
23
24#define IBMCAM_VENDOR_ID 0x0545
25#define IBMCAM_PRODUCT_ID 0x8080
26#define NETCAM_PRODUCT_ID 0x8002 /* IBM NetCamera, close to model 2 */
27#define VEO_800C_PRODUCT_ID 0x800C /* Veo Stingray, repackaged Model 2 */
28#define VEO_800D_PRODUCT_ID 0x800D /* Veo Stingray, repackaged Model 4 */
29
30#define MAX_IBMCAM 4 /* How many devices we allow to connect */
31#define USES_IBMCAM_PUTPIXEL 0 /* 0=Fast/oops 1=Slow/secure */
32
33/* Header signatures */
34
35/* Model 1 header: 00 FF 00 xx */
36#define HDRSIG_MODEL1_128x96 0x06 /* U Y V Y ... */
37#define HDRSIG_MODEL1_176x144 0x0e /* U Y V Y ... */
38#define HDRSIG_MODEL1_352x288 0x00 /* V Y U Y ... */
39
40#define IBMCAM_MODEL_1 1 /* XVP-501, 3 interfaces, rev. 0.02 */
41#define IBMCAM_MODEL_2 2 /* KSX-X9903, 2 interfaces, rev. 3.0a */
42#define IBMCAM_MODEL_3 3 /* KSX-X9902, 2 interfaces, rev. 3.01 */
43#define IBMCAM_MODEL_4 4 /* IBM NetCamera, 0545/8002/3.0a */
44
45/* Video sizes supported */
46#define VIDEOSIZE_128x96 VIDEOSIZE(128, 96)
47#define VIDEOSIZE_176x144 VIDEOSIZE(176,144)
48#define VIDEOSIZE_352x288 VIDEOSIZE(352,288)
49#define VIDEOSIZE_320x240 VIDEOSIZE(320,240)
50#define VIDEOSIZE_352x240 VIDEOSIZE(352,240)
51#define VIDEOSIZE_640x480 VIDEOSIZE(640,480)
52#define VIDEOSIZE_160x120 VIDEOSIZE(160,120)
53
54/* Video sizes supported */
55enum {
56 SIZE_128x96 = 0,
57 SIZE_160x120,
58 SIZE_176x144,
59 SIZE_320x240,
60 SIZE_352x240,
61 SIZE_352x288,
62 SIZE_640x480,
63 /* Add/remove/rearrange items before this line */
64 SIZE_LastItem
65};
66
67/*
68 * This structure lives in uvd->user field.
69 */
70typedef struct {
71 int initialized; /* Had we already sent init sequence? */
72 int camera_model; /* What type of IBM camera we got? */
73 int has_hdr;
74} ibmcam_t;
75#define IBMCAM_T(uvd) ((ibmcam_t *)((uvd)->user_data))
76
77static struct usbvideo *cams;
78
79static int debug;
80
81static int flags; /* = FLAGS_DISPLAY_HINTS | FLAGS_OVERLAY_STATS; */
82
83static const int min_canvasWidth = 8;
84static const int min_canvasHeight = 4;
85
86static int lighting = 1; /* Medium */
87
88#define SHARPNESS_MIN 0
89#define SHARPNESS_MAX 6
90static int sharpness = 4; /* Low noise, good details */
91
92#define FRAMERATE_MIN 0
93#define FRAMERATE_MAX 6
94static int framerate = -1;
95
96static int size = SIZE_352x288;
97
98/*
99 * Here we define several initialization variables. They may
100 * be used to automatically set color, hue, brightness and
101 * contrast to desired values. This is particularly useful in
102 * case of webcams (which have no controls and no on-screen
103 * output) and also when a client V4L software is used that
104 * does not have some of those controls. In any case it's
105 * good to have startup values as options.
106 *
107 * These values are all in [0..255] range. This simplifies
108 * operation. Note that actual values of V4L variables may
109 * be scaled up (as much as << 8). User can see that only
110 * on overlay output, however, or through a V4L client.
111 */
112static int init_brightness = 128;
113static int init_contrast = 192;
114static int init_color = 128;
115static int init_hue = 128;
116static int hue_correction = 128;
117
118/* Settings for camera model 2 */
119static int init_model2_rg2 = -1;
120static int init_model2_sat = -1;
121static int init_model2_yb = -1;
122
123/* 01.01.08 - Added for RCA video in support -LO */
124/* Settings for camera model 3 */
125static int init_model3_input = 0;
126
127module_param(debug, int, 0);
128MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
129module_param(flags, int, 0);
130MODULE_PARM_DESC(flags, "Bitfield: 0=VIDIOCSYNC, 1=B/W, 2=show hints, 3=show stats, 4=test pattern, 5=separate frames, 6=clean frames");
131module_param(framerate, int, 0);
132MODULE_PARM_DESC(framerate, "Framerate setting: 0=slowest, 6=fastest (default=2)");
133module_param(lighting, int, 0);
134MODULE_PARM_DESC(lighting, "Photosensitivity: 0=bright, 1=medium (default), 2=low light");
135module_param(sharpness, int, 0);
136MODULE_PARM_DESC(sharpness, "Model1 noise reduction: 0=smooth, 6=sharp (default=4)");
137module_param(size, int, 0);
138MODULE_PARM_DESC(size, "Image size: 0=128x96 1=160x120 2=176x144 3=320x240 4=352x240 5=352x288 6=640x480 (default=5)");
139module_param(init_brightness, int, 0);
140MODULE_PARM_DESC(init_brightness, "Brightness preconfiguration: 0-255 (default=128)");
141module_param(init_contrast, int, 0);
142MODULE_PARM_DESC(init_contrast, "Contrast preconfiguration: 0-255 (default=192)");
143module_param(init_color, int, 0);
144MODULE_PARM_DESC(init_color, "Color preconfiguration: 0-255 (default=128)");
145module_param(init_hue, int, 0);
146MODULE_PARM_DESC(init_hue, "Hue preconfiguration: 0-255 (default=128)");
147module_param(hue_correction, int, 0);
148MODULE_PARM_DESC(hue_correction, "YUV colorspace regulation: 0-255 (default=128)");
149
150module_param(init_model2_rg2, int, 0);
151MODULE_PARM_DESC(init_model2_rg2, "Model2 preconfiguration: 0-255 (default=47)");
152module_param(init_model2_sat, int, 0);
153MODULE_PARM_DESC(init_model2_sat, "Model2 preconfiguration: 0-255 (default=52)");
154module_param(init_model2_yb, int, 0);
155MODULE_PARM_DESC(init_model2_yb, "Model2 preconfiguration: 0-255 (default=160)");
156
157/* 01.01.08 - Added for RCA video in support -LO */
158module_param(init_model3_input, int, 0);
159MODULE_PARM_DESC(init_model3_input, "Model3 input: 0=CCD 1=RCA");
160
161MODULE_AUTHOR ("Dmitri");
162MODULE_DESCRIPTION ("IBM/Xirlink C-it USB Camera Driver for Linux (c) 2000");
163MODULE_LICENSE("GPL");
164
165/* Still mysterious i2c commands */
166static const unsigned short unknown_88 = 0x0088;
167static const unsigned short unknown_89 = 0x0089;
168static const unsigned short bright_3x[3] = { 0x0031, 0x0032, 0x0033 };
169static const unsigned short contrast_14 = 0x0014;
170static const unsigned short light_27 = 0x0027;
171static const unsigned short sharp_13 = 0x0013;
172
173/* i2c commands for Model 2 cameras */
174static const unsigned short mod2_brightness = 0x001a; /* $5b .. $ee; default=$5a */
175static const unsigned short mod2_set_framerate = 0x001c; /* 0 (fast).. $1F (slow) */
176static const unsigned short mod2_color_balance_rg2 = 0x001e; /* 0 (red) .. $7F (green) */
177static const unsigned short mod2_saturation = 0x0020; /* 0 (b/w) - $7F (full color) */
178static const unsigned short mod2_color_balance_yb = 0x0022; /* 0..$7F, $50 is about right */
179static const unsigned short mod2_hue = 0x0024; /* 0..$7F, $70 is about right */
180static const unsigned short mod2_sensitivity = 0x0028; /* 0 (min) .. $1F (max) */
181
182struct struct_initData {
183 unsigned char req;
184 unsigned short value;
185 unsigned short index;
186};
187
188/*
189 * ibmcam_size_to_videosize()
190 *
191 * This procedure converts module option 'size' into the actual
192 * videosize_t that defines the image size in pixels. We need
193 * simplified 'size' because user wants a simple enumerated list
194 * of choices, not an infinite set of possibilities.
195 */
196static videosize_t ibmcam_size_to_videosize(int size)
197{
198 videosize_t vs = VIDEOSIZE_352x288;
199 RESTRICT_TO_RANGE(size, 0, (SIZE_LastItem-1));
200 switch (size) {
201 case SIZE_128x96:
202 vs = VIDEOSIZE_128x96;
203 break;
204 case SIZE_160x120:
205 vs = VIDEOSIZE_160x120;
206 break;
207 case SIZE_176x144:
208 vs = VIDEOSIZE_176x144;
209 break;
210 case SIZE_320x240:
211 vs = VIDEOSIZE_320x240;
212 break;
213 case SIZE_352x240:
214 vs = VIDEOSIZE_352x240;
215 break;
216 case SIZE_352x288:
217 vs = VIDEOSIZE_352x288;
218 break;
219 case SIZE_640x480:
220 vs = VIDEOSIZE_640x480;
221 break;
222 default:
223 err("size=%d. is not valid", size);
224 break;
225 }
226 return vs;
227}
228
229/*
230 * ibmcam_find_header()
231 *
232 * Locate one of supported header markers in the queue.
233 * Once found, remove all preceding bytes AND the marker (4 bytes)
234 * from the data pump queue. Whatever follows must be video lines.
235 *
236 * History:
237 * 1/21/00 Created.
238 */
239static enum ParseState ibmcam_find_header(struct uvd *uvd) /* FIXME: Add frame here */
240{
241 struct usbvideo_frame *frame;
242 ibmcam_t *icam;
243
244 if ((uvd->curframe) < 0 || (uvd->curframe >= USBVIDEO_NUMFRAMES)) {
245 err("ibmcam_find_header: Illegal frame %d.", uvd->curframe);
246 return scan_EndParse;
247 }
248 icam = IBMCAM_T(uvd);
249 assert(icam != NULL);
250 frame = &uvd->frame[uvd->curframe];
251 icam->has_hdr = 0;
252 switch (icam->camera_model) {
253 case IBMCAM_MODEL_1:
254 {
255 const int marker_len = 4;
256 while (RingQueue_GetLength(&uvd->dp) >= marker_len) {
257 if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
258 (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xFF) &&
259 (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00))
260 {
261#if 0 /* This code helps to detect new frame markers */
262 info("Header sig: 00 FF 00 %02X", RING_QUEUE_PEEK(&uvd->dp, 3));
263#endif
264 frame->header = RING_QUEUE_PEEK(&uvd->dp, 3);
265 if ((frame->header == HDRSIG_MODEL1_128x96) ||
266 (frame->header == HDRSIG_MODEL1_176x144) ||
267 (frame->header == HDRSIG_MODEL1_352x288))
268 {
269#if 0
270 info("Header found.");
271#endif
272 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, marker_len);
273 icam->has_hdr = 1;
274 break;
275 }
276 }
277 /* If we are still here then this doesn't look like a header */
278 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
279 }
280 break;
281 }
282 case IBMCAM_MODEL_2:
283case IBMCAM_MODEL_4:
284 {
285 int marker_len = 0;
286 switch (uvd->videosize) {
287 case VIDEOSIZE_176x144:
288 marker_len = 10;
289 break;
290 default:
291 marker_len = 2;
292 break;
293 }
294 while (RingQueue_GetLength(&uvd->dp) >= marker_len) {
295 if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
296 (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xFF))
297 {
298#if 0
299 info("Header found.");
300#endif
301 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, marker_len);
302 icam->has_hdr = 1;
303 frame->header = HDRSIG_MODEL1_176x144;
304 break;
305 }
306 /* If we are still here then this doesn't look like a header */
307 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
308 }
309 break;
310 }
311 case IBMCAM_MODEL_3:
312 { /*
313 * Headers: (one precedes every frame). nc=no compression,
314 * bq=best quality bf=best frame rate.
315 *
316 * 176x144: 00 FF 02 { 0A=nc CA=bq EA=bf }
317 * 320x240: 00 FF 02 { 08=nc 28=bq 68=bf }
318 * 640x480: 00 FF 03 { 08=nc 28=bq 68=bf }
319 *
320 * Bytes '00 FF' seem to indicate header. Other two bytes
321 * encode the frame type. This is a set of bit fields that
322 * encode image size, compression type etc. These fields
323 * do NOT contain frame number because all frames carry
324 * the same header.
325 */
326 const int marker_len = 4;
327 while (RingQueue_GetLength(&uvd->dp) >= marker_len) {
328 if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
329 (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xFF) &&
330 (RING_QUEUE_PEEK(&uvd->dp, 2) != 0xFF))
331 {
332 /*
333 * Combine 2 bytes of frame type into one
334 * easy to use value
335 */
336 unsigned long byte3, byte4;
337
338 byte3 = RING_QUEUE_PEEK(&uvd->dp, 2);
339 byte4 = RING_QUEUE_PEEK(&uvd->dp, 3);
340 frame->header = (byte3 << 8) | byte4;
341#if 0
342 info("Header found.");
343#endif
344 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, marker_len);
345 icam->has_hdr = 1;
346 break;
347 }
348 /* If we are still here then this doesn't look like a header */
349 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
350 }
351 break;
352 }
353 default:
354 break;
355 }
356 if (!icam->has_hdr) {
357 if (uvd->debug > 2)
358 info("Skipping frame, no header");
359 return scan_EndParse;
360 }
361
362 /* Header found */
363 icam->has_hdr = 1;
364 uvd->stats.header_count++;
365 frame->scanstate = ScanState_Lines;
366 frame->curline = 0;
367
368 if (flags & FLAGS_FORCE_TESTPATTERN) {
369 usbvideo_TestPattern(uvd, 1, 1);
370 return scan_NextFrame;
371 }
372 return scan_Continue;
373}
374
375/*
376 * ibmcam_parse_lines()
377 *
378 * Parse one line (interlaced) from the buffer, put
379 * decoded RGB value into the current frame buffer
380 * and add the written number of bytes (RGB) to
381 * the *pcopylen.
382 *
383 * History:
384 * 21-Jan-2000 Created.
385 * 12-Oct-2000 Reworked to reflect interlaced nature of the data.
386 */
387static enum ParseState ibmcam_parse_lines(
388 struct uvd *uvd,
389 struct usbvideo_frame *frame,
390 long *pcopylen)
391{
392 unsigned char *f;
393 ibmcam_t *icam;
394 unsigned int len, scanLength, scanHeight, order_uv, order_yc;
395 int v4l_linesize; /* V4L line offset */
396 const int hue_corr = (uvd->vpic.hue - 0x8000) >> 10; /* -32..+31 */
397 const int hue2_corr = (hue_correction - 128) / 4; /* -32..+31 */
398 const int ccm = 128; /* Color correction median - see below */
399 int y, u, v, i, frame_done=0, color_corr;
400 static unsigned char lineBuffer[640*3];
401 unsigned const char *chromaLine, *lumaLine;
402
403 assert(uvd != NULL);
404 assert(frame != NULL);
405 icam = IBMCAM_T(uvd);
406 assert(icam != NULL);
407 color_corr = (uvd->vpic.colour - 0x8000) >> 8; /* -128..+127 = -ccm..+(ccm-1)*/
408 RESTRICT_TO_RANGE(color_corr, -ccm, ccm+1);
409
410 v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
411
412 if (IBMCAM_T(uvd)->camera_model == IBMCAM_MODEL_4) {
413 /* Model 4 frame markers do not carry image size identification */
414 switch (uvd->videosize) {
415 case VIDEOSIZE_128x96:
416 case VIDEOSIZE_160x120:
417 case VIDEOSIZE_176x144:
418 scanLength = VIDEOSIZE_X(uvd->videosize);
419 scanHeight = VIDEOSIZE_Y(uvd->videosize);
420 break;
421 default:
422 err("ibmcam_parse_lines: Wrong mode.");
423 return scan_Out;
424 }
425 order_yc = 1; /* order_yc: true=Yc false=cY ('c'=either U or V) */
426 order_uv = 1; /* Always true in this algorithm */
427 } else {
428 switch (frame->header) {
429 case HDRSIG_MODEL1_128x96:
430 scanLength = 128;
431 scanHeight = 96;
432 order_uv = 1; /* U Y V Y ... */
433 break;
434 case HDRSIG_MODEL1_176x144:
435 scanLength = 176;
436 scanHeight = 144;
437 order_uv = 1; /* U Y V Y ... */
438 break;
439 case HDRSIG_MODEL1_352x288:
440 scanLength = 352;
441 scanHeight = 288;
442 order_uv = 0; /* Y V Y V ... */
443 break;
444 default:
445 err("Unknown header signature 00 FF 00 %02lX", frame->header);
446 return scan_NextFrame;
447 }
448 /* order_yc: true=Yc false=cY ('c'=either U or V) */
449 order_yc = (IBMCAM_T(uvd)->camera_model == IBMCAM_MODEL_2);
450 }
451
452 len = scanLength * 3;
453 assert(len <= sizeof(lineBuffer));
454
455 /*
456 * Lines are organized this way:
457 *
458 * I420:
459 * ~~~~
460 * <scanLength->
461 * ___________________________________
462 * |-----Y-----|---UVUVUV...UVUV-----| \
463 * |-----------+---------------------| \
464 * |<-- 176 -->|<------ 176*2 ------>| Total 72. lines (interlaced)
465 * |... ... | ... | /
466 * |<-- 352 -->|<------ 352*2 ------>| Total 144. lines (interlaced)
467 * |___________|_____________________| /
468 * \ \
469 * lumaLine chromaLine
470 */
471
472 /* Make sure there's enough data for the entire line */
473 if (RingQueue_GetLength(&uvd->dp) < len)
474 return scan_Out;
475
476 /* Suck one line out of the ring queue */
477 RingQueue_Dequeue(&uvd->dp, lineBuffer, len);
478
479 /*
480 * Make sure that our writing into output buffer
481 * will not exceed the buffer. Mind that we may write
482 * not into current output scanline but in several after
483 * it as well (if we enlarge image vertically.)
484 */
485 if ((frame->curline + 2) >= VIDEOSIZE_Y(frame->request))
486 return scan_NextFrame;
487
488 /*
489 * Now we are sure that entire line (representing all 'scanLength'
490 * pixels from the camera) is available in the buffer. We
491 * start copying the line left-aligned to the V4L buffer.
492 * If the camera line is shorter then we should pad the V4L
493 * buffer with something (black) to complete the line.
494 */
495 assert(frame->data != NULL);
496 f = frame->data + (v4l_linesize * frame->curline);
497
498 /*
499 * To obtain chrominance data from the 'chromaLine' use this:
500 * v = chromaLine[0]; // 0-1:[0], 2-3:[4], 4-5:[8]...
501 * u = chromaLine[2]; // 0-1:[2], 2-3:[6], 4-5:[10]...
502 *
503 * Indices must be calculated this way:
504 * v_index = (i >> 1) << 2;
505 * u_index = (i >> 1) << 2 + 2;
506 *
507 * where 'i' is the column number [0..VIDEOSIZE_X(frame->request)-1]
508 */
509 lumaLine = lineBuffer;
510 chromaLine = lineBuffer + scanLength;
511 for (i = 0; i < VIDEOSIZE_X(frame->request); i++)
512 {
513 unsigned char rv, gv, bv; /* RGB components */
514
515 /* Check for various visual debugging hints (colorized pixels) */
516 if ((flags & FLAGS_DISPLAY_HINTS) && (icam->has_hdr)) {
517 /*
518 * This is bad and should not happen. This means that
519 * we somehow overshoot the line and encountered new
520 * frame! Obviously our camera/V4L frame size is out
521 * of whack. This cyan dot will help you to figure
522 * out where exactly the new frame arrived.
523 */
524 if (icam->has_hdr == 1) {
525 bv = 0; /* Yellow marker */
526 gv = 0xFF;
527 rv = 0xFF;
528 } else {
529 bv = 0xFF; /* Cyan marker */
530 gv = 0xFF;
531 rv = 0;
532 }
533 icam->has_hdr = 0;
534 goto make_pixel;
535 }
536
537 /*
538 * Check if we are still in range. We may be out of range if our
539 * V4L canvas is wider or taller than the camera "native" image.
540 * Then we quickly fill the remainder of the line with zeros to
541 * make black color and quit the horizontal scanning loop.
542 */
543 if (((frame->curline + 2) >= scanHeight) || (i >= scanLength)) {
544 const int j = i * V4L_BYTES_PER_PIXEL;
545#if USES_IBMCAM_PUTPIXEL
546 /* Refresh 'f' because we don't use it much with PUTPIXEL */
547 f = frame->data + (v4l_linesize * frame->curline) + j;
548#endif
549 memset(f, 0, v4l_linesize - j);
550 break;
551 }
552
553 y = lumaLine[i];
554 if (flags & FLAGS_MONOCHROME) /* Use monochrome for debugging */
555 rv = gv = bv = y;
556 else {
557 int off_0, off_2;
558
559 off_0 = (i >> 1) << 2;
560 off_2 = off_0 + 2;
561
562 if (order_yc) {
563 off_0++;
564 off_2++;
565 }
566 if (!order_uv) {
567 off_0 += 2;
568 off_2 -= 2;
569 }
570 u = chromaLine[off_0] + hue_corr;
571 v = chromaLine[off_2] + hue2_corr;
572
573 /* Apply color correction */
574 if (color_corr != 0) {
575 /* Magnify up to 2 times, reduce down to zero saturation */
576 u = 128 + ((ccm + color_corr) * (u - 128)) / ccm;
577 v = 128 + ((ccm + color_corr) * (v - 128)) / ccm;
578 }
579 YUV_TO_RGB_BY_THE_BOOK(y, u, v, rv, gv, bv);
580 }
581
582 make_pixel:
583 /*
584 * The purpose of creating the pixel here, in one,
585 * dedicated place is that we may need to make the
586 * pixel wider and taller than it actually is. This
587 * may be used if camera generates small frames for
588 * sake of frame rate (or any other reason.)
589 *
590 * The output data consists of B, G, R bytes
591 * (in this order).
592 */
593#if USES_IBMCAM_PUTPIXEL
594 RGB24_PUTPIXEL(frame, i, frame->curline, rv, gv, bv);
595#else
596 *f++ = bv;
597 *f++ = gv;
598 *f++ = rv;
599#endif
600 /*
601 * Typically we do not decide within a legitimate frame
602 * that we want to end the frame. However debugging code
603 * may detect marker of new frame within the data. Then
604 * this condition activates. The 'data' pointer is already
605 * pointing at the new marker, so we'd better leave it as is.
606 */
607 if (frame_done)
608 break; /* End scanning of lines */
609 }
610 /*
611 * Account for number of bytes that we wrote into output V4L frame.
612 * We do it here, after we are done with the scanline, because we
613 * may fill more than one output scanline if we do vertical
614 * enlargement.
615 */
616 frame->curline += 2;
617 if (pcopylen != NULL)
618 *pcopylen += 2 * v4l_linesize;
619 frame->deinterlace = Deinterlace_FillOddLines;
620
621 if (frame_done || (frame->curline >= VIDEOSIZE_Y(frame->request)))
622 return scan_NextFrame;
623 else
624 return scan_Continue;
625}
626
627/*
628 * ibmcam_model2_320x240_parse_lines()
629 *
630 * This procedure deals with a weird RGB format that is produced by IBM
631 * camera model 2 in modes 320x240 and above; 'x' below is 159 or 175,
632 * depending on horizontal size of the picture:
633 *
634 * <--- 160 or 176 pairs of RA,RB bytes ----->
635 * *-----------------------------------------* \
636 * | RA0 | RB0 | RA1 | RB1 | ... | RAx | RBx | \ This is pair of horizontal lines,
637 * |-----+-----+-----+-----+ ... +-----+-----| *- or one interlaced line, total
638 * | B0 | G0 | B1 | G1 | ... | Bx | Gx | / 120 or 144 such pairs which yield
639 * |=====+=====+=====+=====+ ... +=====+=====| / 240 or 288 lines after deinterlacing.
640 *
641 * Each group of FOUR bytes (RAi, RBi, Bi, Gi) where i=0..frame_width/2-1
642 * defines ONE pixel. Therefore this format yields 176x144 "decoded"
643 * resolution at best. I do not know why camera sends such format - the
644 * previous model (1) just used interlaced I420 and everyone was happy.
645 *
646 * I do not know what is the difference between RAi and RBi bytes. Both
647 * seemingly represent R component, but slightly vary in value (so that
648 * the picture looks a bit colored if one or another is used). I use
649 * them both as R component in attempt to at least partially recover the
650 * lost resolution.
651 */
652static enum ParseState ibmcam_model2_320x240_parse_lines(
653 struct uvd *uvd,
654 struct usbvideo_frame *frame,
655 long *pcopylen)
656{
657 unsigned char *f, *la, *lb;
658 unsigned int len;
659 int v4l_linesize; /* V4L line offset */
660 int i, j, frame_done=0, color_corr;
661 int scanLength, scanHeight;
662 static unsigned char lineBuffer[352*2];
663
664 switch (uvd->videosize) {
665 case VIDEOSIZE_320x240:
666 case VIDEOSIZE_352x240:
667 case VIDEOSIZE_352x288:
668 scanLength = VIDEOSIZE_X(uvd->videosize);
669 scanHeight = VIDEOSIZE_Y(uvd->videosize);
670 break;
671 default:
672 err("ibmcam_model2_320x240_parse_lines: Wrong mode.");
673 return scan_Out;
674 }
675
676 color_corr = (uvd->vpic.colour) >> 8; /* 0..+255 */
677 v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
678
679 len = scanLength * 2; /* See explanation above */
680 assert(len <= sizeof(lineBuffer));
681
682 /* Make sure there's enough data for the entire line */
683 if (RingQueue_GetLength(&uvd->dp) < len)
684 return scan_Out;
685
686 /* Suck one line out of the ring queue */
687 RingQueue_Dequeue(&uvd->dp, lineBuffer, len);
688
689 /*
690 * Make sure that our writing into output buffer
691 * will not exceed the buffer. Mind that we may write
692 * not into current output scanline but in several after
693 * it as well (if we enlarge image vertically.)
694 */
695 if ((frame->curline + 2) >= VIDEOSIZE_Y(frame->request))
696 return scan_NextFrame;
697
698 la = lineBuffer;
699 lb = lineBuffer + scanLength;
700
701 /*
702 * Now we are sure that entire line (representing all
703 * VIDEOSIZE_X(frame->request)
704 * pixels from the camera) is available in the scratch buffer. We
705 * start copying the line left-aligned to the V4L buffer (which
706 * might be larger - not smaller, hopefully). If the camera
707 * line is shorter then we should pad the V4L buffer with something
708 * (black in this case) to complete the line.
709 */
710 f = frame->data + (v4l_linesize * frame->curline);
711
712 /* Fill the 2-line strip */
713 for (i = 0; i < VIDEOSIZE_X(frame->request); i++) {
714 int y, rv, gv, bv; /* RGB components */
715
716 j = i & (~1);
717
718 /* Check for various visual debugging hints (colorized pixels) */
719 if ((flags & FLAGS_DISPLAY_HINTS) && (IBMCAM_T(uvd)->has_hdr)) {
720 if (IBMCAM_T(uvd)->has_hdr == 1) {
721 bv = 0; /* Yellow marker */
722 gv = 0xFF;
723 rv = 0xFF;
724 } else {
725 bv = 0xFF; /* Cyan marker */
726 gv = 0xFF;
727 rv = 0;
728 }
729 IBMCAM_T(uvd)->has_hdr = 0;
730 goto make_pixel;
731 }
732
733 /*
734 * Check if we are still in range. We may be out of range if our
735 * V4L canvas is wider or taller than the camera "native" image.
736 * Then we quickly fill the remainder of the line with zeros to
737 * make black color and quit the horizontal scanning loop.
738 */
739 if (((frame->curline + 2) >= scanHeight) || (i >= scanLength)) {
740 const int j = i * V4L_BYTES_PER_PIXEL;
741#if USES_IBMCAM_PUTPIXEL
742 /* Refresh 'f' because we don't use it much with PUTPIXEL */
743 f = frame->data + (v4l_linesize * frame->curline) + j;
744#endif
745 memset(f, 0, v4l_linesize - j);
746 break;
747 }
748
749 /*
750 * Here I use RA and RB components, one per physical pixel.
751 * This causes fine vertical grid on the picture but may improve
752 * horizontal resolution. If you prefer replicating, use this:
753 * rv = la[j + 0]; ... or ... rv = la[j + 1];
754 * then the pixel will be replicated.
755 */
756 rv = la[i];
757 gv = lb[j + 1];
758 bv = lb[j + 0];
759
760 y = (rv + gv + bv) / 3; /* Brightness (badly calculated) */
761
762 if (flags & FLAGS_MONOCHROME) /* Use monochrome for debugging */
763 rv = gv = bv = y;
764 else if (color_corr != 128) {
765
766 /* Calculate difference between color and brightness */
767 rv -= y;
768 gv -= y;
769 bv -= y;
770
771 /* Scale differences */
772 rv = (rv * color_corr) / 128;
773 gv = (gv * color_corr) / 128;
774 bv = (bv * color_corr) / 128;
775
776 /* Reapply brightness */
777 rv += y;
778 gv += y;
779 bv += y;
780
781 /* Watch for overflows */
782 RESTRICT_TO_RANGE(rv, 0, 255);
783 RESTRICT_TO_RANGE(gv, 0, 255);
784 RESTRICT_TO_RANGE(bv, 0, 255);
785 }
786
787 make_pixel:
788 RGB24_PUTPIXEL(frame, i, frame->curline, rv, gv, bv);
789 }
790 /*
791 * Account for number of bytes that we wrote into output V4L frame.
792 * We do it here, after we are done with the scanline, because we
793 * may fill more than one output scanline if we do vertical
794 * enlargement.
795 */
796 frame->curline += 2;
797 *pcopylen += v4l_linesize * 2;
798 frame->deinterlace = Deinterlace_FillOddLines;
799
800 if (frame_done || (frame->curline >= VIDEOSIZE_Y(frame->request)))
801 return scan_NextFrame;
802 else
803 return scan_Continue;
804}
805
806static enum ParseState ibmcam_model3_parse_lines(
807 struct uvd *uvd,
808 struct usbvideo_frame *frame,
809 long *pcopylen)
810{
811 unsigned char *data;
812 const unsigned char *color;
813 unsigned int len;
814 int v4l_linesize; /* V4L line offset */
815 const int hue_corr = (uvd->vpic.hue - 0x8000) >> 10; /* -32..+31 */
816 const int hue2_corr = (hue_correction - 128) / 4; /* -32..+31 */
817 const int ccm = 128; /* Color correction median - see below */
818 int i, u, v, rw, data_w=0, data_h=0, color_corr;
819 static unsigned char lineBuffer[640*3];
820
821 color_corr = (uvd->vpic.colour - 0x8000) >> 8; /* -128..+127 = -ccm..+(ccm-1)*/
822 RESTRICT_TO_RANGE(color_corr, -ccm, ccm+1);
823
824 v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
825
826 /* The header tells us what sort of data is in this frame */
827 switch (frame->header) {
828 /*
829 * Uncompressed modes (that are easy to decode).
830 */
831 case 0x0308:
832 data_w = 640;
833 data_h = 480;
834 break;
835 case 0x0208:
836 data_w = 320;
837 data_h = 240;
838 break;
839 case 0x020A:
840 data_w = 160;
841 data_h = 120;
842 break;
843 /*
844 * Compressed modes (ViCE - that I don't know how to decode).
845 */
846 case 0x0328: /* 640x480, best quality compression */
847 case 0x0368: /* 640x480, best frame rate compression */
848 case 0x0228: /* 320x240, best quality compression */
849 case 0x0268: /* 320x240, best frame rate compression */
850 case 0x02CA: /* 160x120, best quality compression */
851 case 0x02EA: /* 160x120, best frame rate compression */
852 /* Do nothing with this - not supported */
853 err("Unsupported mode $%04lx", frame->header);
854 return scan_NextFrame;
855 default:
856 /* Catch unknown headers, may help in learning new headers */
857 err("Strange frame->header=$%08lx", frame->header);
858 return scan_NextFrame;
859 }
860
861 /*
862 * Make sure that our writing into output buffer
863 * will not exceed the buffer. Note that we may write
864 * not into current output scanline but in several after
865 * it as well (if we enlarge image vertically.)
866 */
867 if ((frame->curline + 1) >= data_h) {
868 if (uvd->debug >= 3)
869 info("Reached line %d. (frame is done)", frame->curline);
870 return scan_NextFrame;
871 }
872
873 /* Make sure there's enough data for the entire line */
874 len = 3 * data_w; /* <y-data> <uv-data> */
875 assert(len <= sizeof(lineBuffer));
876
877 /* Make sure there's enough data for the entire line */
878 if (RingQueue_GetLength(&uvd->dp) < len)
879 return scan_Out;
880
881 /* Suck one line out of the ring queue */
882 RingQueue_Dequeue(&uvd->dp, lineBuffer, len);
883
884 data = lineBuffer;
885 color = data + data_w; /* Point to where color planes begin */
886
887 /* Bottom-to-top scanning */
888 rw = (int)VIDEOSIZE_Y(frame->request) - (int)(frame->curline) - 1;
889 RESTRICT_TO_RANGE(rw, 0, VIDEOSIZE_Y(frame->request)-1);
890
891 for (i = 0; i < VIDEOSIZE_X(frame->request); i++) {
892 int y, rv, gv, bv; /* RGB components */
893
894 if (i < data_w) {
895 y = data[i]; /* Luminosity is the first line */
896
897 /* Apply static color correction */
898 u = color[i*2] + hue_corr;
899 v = color[i*2 + 1] + hue2_corr;
900
901 /* Apply color correction */
902 if (color_corr != 0) {
903 /* Magnify up to 2 times, reduce down to zero saturation */
904 u = 128 + ((ccm + color_corr) * (u - 128)) / ccm;
905 v = 128 + ((ccm + color_corr) * (v - 128)) / ccm;
906 }
907 } else
908 y = 0, u = v = 128;
909
910 YUV_TO_RGB_BY_THE_BOOK(y, u, v, rv, gv, bv);
911 RGB24_PUTPIXEL(frame, i, rw, rv, gv, bv); /* Done by deinterlacing now */
912 }
913 frame->deinterlace = Deinterlace_FillEvenLines;
914
915 /*
916 * Account for number of bytes that we wrote into output V4L frame.
917 * We do it here, after we are done with the scanline, because we
918 * may fill more than one output scanline if we do vertical
919 * enlargement.
920 */
921 frame->curline += 2;
922 *pcopylen += 2 * v4l_linesize;
923
924 if (frame->curline >= VIDEOSIZE_Y(frame->request)) {
925 if (uvd->debug >= 3) {
926 info("All requested lines (%ld.) done.",
927 VIDEOSIZE_Y(frame->request));
928 }
929 return scan_NextFrame;
930 } else
931 return scan_Continue;
932}
933
934/*
935 * ibmcam_model4_128x96_parse_lines()
936 *
937 * This decoder is for one strange data format that is produced by Model 4
938 * camera only in 128x96 mode. This is RGB format and here is its description.
939 * First of all, this is non-interlaced stream, meaning that all scan lines
940 * are present in the datastream. There are 96 consecutive blocks of data
941 * that describe all 96 lines of the image. Each block is 5*128 bytes long
942 * and carries R, G, B components. The format of the block is shown in the
943 * code below. First 128*2 bytes are interleaved R and G components. Then
944 * we have a gap (junk data) 64 bytes long. Then follow B and something
945 * else, also interleaved (this makes another 128*2 bytes). After that
946 * probably another 64 bytes of junk follow.
947 *
948 * History:
949 * 10-Feb-2001 Created.
950 */
951static enum ParseState ibmcam_model4_128x96_parse_lines(
952 struct uvd *uvd,
953 struct usbvideo_frame *frame,
954 long *pcopylen)
955{
956 const unsigned char *data_rv, *data_gv, *data_bv;
957 unsigned int len;
958 int i, v4l_linesize; /* V4L line offset */
959 const int data_w=128, data_h=96;
960 static unsigned char lineBuffer[128*5];
961
962 v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
963
964 /*
965 * Make sure that our writing into output buffer
966 * will not exceed the buffer. Note that we may write
967 * not into current output scanline but in several after
968 * it as well (if we enlarge image vertically.)
969 */
970 if ((frame->curline + 1) >= data_h) {
971 if (uvd->debug >= 3)
972 info("Reached line %d. (frame is done)", frame->curline);
973 return scan_NextFrame;
974 }
975
976 /*
977 * RGRGRG .... RGRG_____________B?B?B? ... B?B?____________
978 * <---- 128*2 ---><---- 64 ---><--- 128*2 ---><--- 64 --->
979 */
980
981 /* Make sure there's enough data for the entire line */
982 len = 5 * data_w;
983 assert(len <= sizeof(lineBuffer));
984
985 /* Make sure there's enough data for the entire line */
986 if (RingQueue_GetLength(&uvd->dp) < len)
987 return scan_Out;
988
989 /* Suck one line out of the ring queue */
990 RingQueue_Dequeue(&uvd->dp, lineBuffer, len);
991
992 data_rv = lineBuffer;
993 data_gv = lineBuffer + 1;
994 data_bv = lineBuffer + data_w*2 + data_w/2;
995 for (i = 0; i < VIDEOSIZE_X(frame->request); i++) {
996 int rv, gv, bv; /* RGB components */
997 if (i < data_w) {
998 const int j = i * 2;
999 gv = data_rv[j];
1000 rv = data_gv[j];
1001 bv = data_bv[j];
1002 if (flags & FLAGS_MONOCHROME) {
1003 unsigned long y;
1004 y = rv + gv + bv;
1005 y /= 3;
1006 if (y > 0xFF)
1007 y = 0xFF;
1008 rv = gv = bv = (unsigned char) y;
1009 }
1010 } else {
1011 rv = gv = bv = 0;
1012 }
1013 RGB24_PUTPIXEL(frame, i, frame->curline, rv, gv, bv);
1014 }
1015 frame->deinterlace = Deinterlace_None;
1016 frame->curline++;
1017 *pcopylen += v4l_linesize;
1018
1019 if (frame->curline >= VIDEOSIZE_Y(frame->request)) {
1020 if (uvd->debug >= 3) {
1021 info("All requested lines (%ld.) done.",
1022 VIDEOSIZE_Y(frame->request));
1023 }
1024 return scan_NextFrame;
1025 } else
1026 return scan_Continue;
1027}
1028
1029/*
1030 * ibmcam_ProcessIsocData()
1031 *
1032 * Generic routine to parse the ring queue data. It employs either
1033 * ibmcam_find_header() or ibmcam_parse_lines() to do most
1034 * of work.
1035 *
1036 * History:
1037 * 1/21/00 Created.
1038 */
1039static void ibmcam_ProcessIsocData(struct uvd *uvd,
1040 struct usbvideo_frame *frame)
1041{
1042 enum ParseState newstate;
1043 long copylen = 0;
1044 int mod = IBMCAM_T(uvd)->camera_model;
1045
1046 while (1) {
1047 newstate = scan_Out;
1048 if (RingQueue_GetLength(&uvd->dp) > 0) {
1049 if (frame->scanstate == ScanState_Scanning) {
1050 newstate = ibmcam_find_header(uvd);
1051 } else if (frame->scanstate == ScanState_Lines) {
1052 if ((mod == IBMCAM_MODEL_2) &&
1053 ((uvd->videosize == VIDEOSIZE_352x288) ||
1054 (uvd->videosize == VIDEOSIZE_320x240) ||
1055 (uvd->videosize == VIDEOSIZE_352x240)))
1056 {
1057 newstate = ibmcam_model2_320x240_parse_lines(
1058 uvd, frame, &copylen);
1059 } else if (mod == IBMCAM_MODEL_4) {
1060 /*
1061 * Model 4 cameras (IBM NetCamera) use Model 2 decoder (RGB)
1062 * for 320x240 and above; 160x120 and 176x144 uses Model 1
1063 * decoder (YUV), and 128x96 mode uses ???
1064 */
1065 if ((uvd->videosize == VIDEOSIZE_352x288) ||
1066 (uvd->videosize == VIDEOSIZE_320x240) ||
1067 (uvd->videosize == VIDEOSIZE_352x240))
1068 {
1069 newstate = ibmcam_model2_320x240_parse_lines(uvd, frame, &copylen);
1070 } else if (uvd->videosize == VIDEOSIZE_128x96) {
1071 newstate = ibmcam_model4_128x96_parse_lines(uvd, frame, &copylen);
1072 } else {
1073 newstate = ibmcam_parse_lines(uvd, frame, &copylen);
1074 }
1075 } else if (mod == IBMCAM_MODEL_3) {
1076 newstate = ibmcam_model3_parse_lines(uvd, frame, &copylen);
1077 } else {
1078 newstate = ibmcam_parse_lines(uvd, frame, &copylen);
1079 }
1080 }
1081 }
1082 if (newstate == scan_Continue)
1083 continue;
1084 else if ((newstate == scan_NextFrame) || (newstate == scan_Out))
1085 break;
1086 else
1087 return; /* scan_EndParse */
1088 }
1089
1090 if (newstate == scan_NextFrame) {
1091 frame->frameState = FrameState_Done;
1092 uvd->curframe = -1;
1093 uvd->stats.frame_num++;
1094 if ((mod == IBMCAM_MODEL_2) || (mod == IBMCAM_MODEL_4)) {
1095 /* Need software contrast adjustment for those cameras */
1096 frame->flags |= USBVIDEO_FRAME_FLAG_SOFTWARE_CONTRAST;
1097 }
1098 }
1099
1100 /* Update the frame's uncompressed length. */
1101 frame->seqRead_Length += copylen;
1102
1103#if 0
1104 {
1105 static unsigned char j=0;
1106 memset(frame->data, j++, uvd->max_frame_size);
1107 frame->frameState = FrameState_Ready;
1108 }
1109#endif
1110}
1111
1112/*
1113 * ibmcam_veio()
1114 *
1115 * History:
1116 * 1/27/00 Added check for dev == NULL; this happens if camera is unplugged.
1117 */
1118static int ibmcam_veio(
1119 struct uvd *uvd,
1120 unsigned char req,
1121 unsigned short value,
1122 unsigned short index)
1123{
1124 static const char proc[] = "ibmcam_veio";
1125 unsigned char cp[8] /* = { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef } */;
1126 int i;
1127
1128 if (!CAMERA_IS_OPERATIONAL(uvd))
1129 return 0;
1130
1131 if (req == 1) {
1132 i = usb_control_msg(
1133 uvd->dev,
1134 usb_rcvctrlpipe(uvd->dev, 0),
1135 req,
1136 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1137 value,
1138 index,
1139 cp,
1140 sizeof(cp),
1141 1000);
1142#if 0
1143 info("USB => %02x%02x%02x%02x%02x%02x%02x%02x "
1144 "(req=$%02x val=$%04x ind=$%04x)",
1145 cp[0],cp[1],cp[2],cp[3],cp[4],cp[5],cp[6],cp[7],
1146 req, value, index);
1147#endif
1148 } else {
1149 i = usb_control_msg(
1150 uvd->dev,
1151 usb_sndctrlpipe(uvd->dev, 0),
1152 req,
1153 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1154 value,
1155 index,
1156 NULL,
1157 0,
1158 1000);
1159 }
1160 if (i < 0) {
1161 err("%s: ERROR=%d. Camera stopped; Reconnect or reload driver.",
1162 proc, i);
1163 uvd->last_error = i;
1164 }
1165 return i;
1166}
1167
1168/*
1169 * ibmcam_calculate_fps()
1170 *
1171 * This procedure roughly calculates the real frame rate based
1172 * on FPS code (framerate=NNN option). Actual FPS differs
1173 * slightly depending on lighting conditions, so that actual frame
1174 * rate is determined by the camera. Since I don't know how to ask
1175 * the camera what FPS is now I have to use the FPS code instead.
1176 *
1177 * The FPS code is in range [0..6], 0 is slowest, 6 is fastest.
1178 * Corresponding real FPS should be in range [3..30] frames per second.
1179 * The conversion formula is obvious:
1180 *
1181 * real_fps = 3 + (fps_code * 4.5)
1182 *
1183 * History:
1184 * 1/18/00 Created.
1185 */
1186static int ibmcam_calculate_fps(struct uvd *uvd)
1187{
1188 return 3 + framerate*4 + framerate/2;
1189}
1190
1191/*
1192 * ibmcam_send_FF_04_02()
1193 *
1194 * This procedure sends magic 3-command prefix to the camera.
1195 * The purpose of this prefix is not known.
1196 *
1197 * History:
1198 * 1/2/00 Created.
1199 */
1200static void ibmcam_send_FF_04_02(struct uvd *uvd)
1201{
1202 ibmcam_veio(uvd, 0, 0x00FF, 0x0127);
1203 ibmcam_veio(uvd, 0, 0x0004, 0x0124);
1204 ibmcam_veio(uvd, 0, 0x0002, 0x0124);
1205}
1206
1207static void ibmcam_send_00_04_06(struct uvd *uvd)
1208{
1209 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
1210 ibmcam_veio(uvd, 0, 0x0004, 0x0124);
1211 ibmcam_veio(uvd, 0, 0x0006, 0x0124);
1212}
1213
1214static void ibmcam_send_x_00(struct uvd *uvd, unsigned short x)
1215{
1216 ibmcam_veio(uvd, 0, x, 0x0127);
1217 ibmcam_veio(uvd, 0, 0x0000, 0x0124);
1218}
1219
1220static void ibmcam_send_x_00_05(struct uvd *uvd, unsigned short x)
1221{
1222 ibmcam_send_x_00(uvd, x);
1223 ibmcam_veio(uvd, 0, 0x0005, 0x0124);
1224}
1225
1226static void ibmcam_send_x_00_05_02(struct uvd *uvd, unsigned short x)
1227{
1228 ibmcam_veio(uvd, 0, x, 0x0127);
1229 ibmcam_veio(uvd, 0, 0x0000, 0x0124);
1230 ibmcam_veio(uvd, 0, 0x0005, 0x0124);
1231 ibmcam_veio(uvd, 0, 0x0002, 0x0124);
1232}
1233
1234static void ibmcam_send_x_01_00_05(struct uvd *uvd, unsigned short x)
1235{
1236 ibmcam_veio(uvd, 0, x, 0x0127);
1237 ibmcam_veio(uvd, 0, 0x0001, 0x0124);
1238 ibmcam_veio(uvd, 0, 0x0000, 0x0124);
1239 ibmcam_veio(uvd, 0, 0x0005, 0x0124);
1240}
1241
1242static void ibmcam_send_x_00_05_02_01(struct uvd *uvd, unsigned short x)
1243{
1244 ibmcam_veio(uvd, 0, x, 0x0127);
1245 ibmcam_veio(uvd, 0, 0x0000, 0x0124);
1246 ibmcam_veio(uvd, 0, 0x0005, 0x0124);
1247 ibmcam_veio(uvd, 0, 0x0002, 0x0124);
1248 ibmcam_veio(uvd, 0, 0x0001, 0x0124);
1249}
1250
1251static void ibmcam_send_x_00_05_02_08_01(struct uvd *uvd, unsigned short x)
1252{
1253 ibmcam_veio(uvd, 0, x, 0x0127);
1254 ibmcam_veio(uvd, 0, 0x0000, 0x0124);
1255 ibmcam_veio(uvd, 0, 0x0005, 0x0124);
1256 ibmcam_veio(uvd, 0, 0x0002, 0x0124);
1257 ibmcam_veio(uvd, 0, 0x0008, 0x0124);
1258 ibmcam_veio(uvd, 0, 0x0001, 0x0124);
1259}
1260
1261static void ibmcam_Packet_Format1(struct uvd *uvd, unsigned char fkey, unsigned char val)
1262{
1263 ibmcam_send_x_01_00_05(uvd, unknown_88);
1264 ibmcam_send_x_00_05(uvd, fkey);
1265 ibmcam_send_x_00_05_02_08_01(uvd, val);
1266 ibmcam_send_x_00_05(uvd, unknown_88);
1267 ibmcam_send_x_00_05_02_01(uvd, fkey);
1268 ibmcam_send_x_00_05(uvd, unknown_89);
1269 ibmcam_send_x_00(uvd, fkey);
1270 ibmcam_send_00_04_06(uvd);
1271 ibmcam_veio(uvd, 1, 0x0000, 0x0126);
1272 ibmcam_send_FF_04_02(uvd);
1273}
1274
1275static void ibmcam_PacketFormat2(struct uvd *uvd, unsigned char fkey, unsigned char val)
1276{
1277 ibmcam_send_x_01_00_05 (uvd, unknown_88);
1278 ibmcam_send_x_00_05 (uvd, fkey);
1279 ibmcam_send_x_00_05_02 (uvd, val);
1280}
1281
1282static void ibmcam_model2_Packet2(struct uvd *uvd)
1283{
1284 ibmcam_veio(uvd, 0, 0x00ff, 0x012d);
1285 ibmcam_veio(uvd, 0, 0xfea3, 0x0124);
1286}
1287
1288static void ibmcam_model2_Packet1(struct uvd *uvd, unsigned short v1, unsigned short v2)
1289{
1290 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
1291 ibmcam_veio(uvd, 0, 0x00ff, 0x012e);
1292 ibmcam_veio(uvd, 0, v1, 0x012f);
1293 ibmcam_veio(uvd, 0, 0x00ff, 0x0130);
1294 ibmcam_veio(uvd, 0, 0xc719, 0x0124);
1295 ibmcam_veio(uvd, 0, v2, 0x0127);
1296
1297 ibmcam_model2_Packet2(uvd);
1298}
1299
1300/*
1301 * ibmcam_model3_Packet1()
1302 *
1303 * 00_0078_012d
1304 * 00_0097_012f
1305 * 00_d141_0124
1306 * 00_0096_0127
1307 * 00_fea8_0124
1308*/
1309static void ibmcam_model3_Packet1(struct uvd *uvd, unsigned short v1, unsigned short v2)
1310{
1311 ibmcam_veio(uvd, 0, 0x0078, 0x012d);
1312 ibmcam_veio(uvd, 0, v1, 0x012f);
1313 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
1314 ibmcam_veio(uvd, 0, v2, 0x0127);
1315 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
1316}
1317
1318static void ibmcam_model4_BrightnessPacket(struct uvd *uvd, int i)
1319{
1320 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
1321 ibmcam_veio(uvd, 0, 0x0026, 0x012f);
1322 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
1323 ibmcam_veio(uvd, 0, i, 0x0127);
1324 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
1325 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
1326 ibmcam_veio(uvd, 0, 0x0038, 0x012d);
1327 ibmcam_veio(uvd, 0, 0x0004, 0x012f);
1328 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
1329 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
1330}
1331
1332/*
1333 * ibmcam_adjust_contrast()
1334 *
1335 * The contrast value changes from 0 (high contrast) to 15 (low contrast).
1336 * This is in reverse to usual order of things (such as TV controls), so
1337 * we reverse it again here.
1338 *
1339 * TODO: we probably don't need to send the setup 5 times...
1340 *
1341 * History:
1342 * 1/2/00 Created.
1343 */
1344static void ibmcam_adjust_contrast(struct uvd *uvd)
1345{
1346 unsigned char a_contrast = uvd->vpic.contrast >> 12;
1347 unsigned char new_contrast;
1348
1349 if (a_contrast >= 16)
1350 a_contrast = 15;
1351 new_contrast = 15 - a_contrast;
1352 if (new_contrast == uvd->vpic_old.contrast)
1353 return;
1354 uvd->vpic_old.contrast = new_contrast;
1355 switch (IBMCAM_T(uvd)->camera_model) {
1356 case IBMCAM_MODEL_1:
1357 {
1358 const int ntries = 5;
1359 int i;
1360 for (i=0; i < ntries; i++) {
1361 ibmcam_Packet_Format1(uvd, contrast_14, new_contrast);
1362 ibmcam_send_FF_04_02(uvd);
1363 }
1364 break;
1365 }
1366 case IBMCAM_MODEL_2:
1367 case IBMCAM_MODEL_4:
1368 /* Models 2, 4 do not have this control; implemented in software. */
1369 break;
1370 case IBMCAM_MODEL_3:
1371 { /* Preset hardware values */
1372 static const struct {
1373 unsigned short cv1;
1374 unsigned short cv2;
1375 unsigned short cv3;
1376 } cv[7] = {
1377 { 0x05, 0x05, 0x0f }, /* Minimum */
1378 { 0x04, 0x04, 0x16 },
1379 { 0x02, 0x03, 0x16 },
1380 { 0x02, 0x08, 0x16 },
1381 { 0x01, 0x0c, 0x16 },
1382 { 0x01, 0x0e, 0x16 },
1383 { 0x01, 0x10, 0x16 } /* Maximum */
1384 };
1385 int i = a_contrast / 2;
1386 RESTRICT_TO_RANGE(i, 0, 6);
1387 ibmcam_veio(uvd, 0, 0x0000, 0x010c); /* Stop */
1388 ibmcam_model3_Packet1(uvd, 0x0067, cv[i].cv1);
1389 ibmcam_model3_Packet1(uvd, 0x005b, cv[i].cv2);
1390 ibmcam_model3_Packet1(uvd, 0x005c, cv[i].cv3);
1391 ibmcam_veio(uvd, 0, 0x0001, 0x0114);
1392 ibmcam_veio(uvd, 0, 0x00c0, 0x010c); /* Go! */
1393 usb_clear_halt(uvd->dev, usb_rcvisocpipe(uvd->dev, uvd->video_endp));
1394 break;
1395 }
1396 default:
1397 break;
1398 }
1399}
1400
1401/*
1402 * ibmcam_change_lighting_conditions()
1403 *
1404 * Camera model 1:
1405 * We have 3 levels of lighting conditions: 0=Bright, 1=Medium, 2=Low.
1406 *
1407 * Camera model 2:
1408 * We have 16 levels of lighting, 0 for bright light and up to 15 for
1409 * low light. But values above 5 or so are useless because camera is
1410 * not really capable to produce anything worth viewing at such light.
1411 * This setting may be altered only in certain camera state.
1412 *
1413 * Low lighting forces slower FPS. Lighting is set as a module parameter.
1414 *
1415 * History:
1416 * 1/5/00 Created.
1417 * 2/20/00 Added support for Model 2 cameras.
1418 */
1419static void ibmcam_change_lighting_conditions(struct uvd *uvd)
1420{
1421 static const char proc[] = "ibmcam_change_lighting_conditions";
1422
1423 if (debug > 0)
1424 info("%s: Set lighting to %hu.", proc, lighting);
1425
1426 switch (IBMCAM_T(uvd)->camera_model) {
1427 case IBMCAM_MODEL_1:
1428 {
1429 const int ntries = 5;
1430 int i;
1431 for (i=0; i < ntries; i++)
1432 ibmcam_Packet_Format1(uvd, light_27, (unsigned short) lighting);
1433 break;
1434 }
1435 case IBMCAM_MODEL_2:
1436#if 0
1437 /*
1438 * This command apparently requires camera to be stopped. My
1439 * experiments showed that it -is- possible to alter the lighting
1440 * conditions setting "on the fly", but why bother? This setting does
1441 * not work reliably in all cases, so I decided simply to leave the
1442 * setting where Xirlink put it - in the camera setup phase. This code
1443 * is commented out because it does not work at -any- moment, so its
1444 * presence makes no sense. You may use it for experiments.
1445 */
1446 ibmcam_veio(uvd, 0, 0x0000, 0x010c); /* Stop camera */
1447 ibmcam_model2_Packet1(uvd, mod2_sensitivity, lighting);
1448 ibmcam_veio(uvd, 0, 0x00c0, 0x010c); /* Start camera */
1449#endif
1450 break;
1451 case IBMCAM_MODEL_3:
1452 case IBMCAM_MODEL_4:
1453 default:
1454 break;
1455 }
1456}
1457
1458/*
1459 * ibmcam_set_sharpness()
1460 *
1461 * Cameras model 1 have internal smoothing feature. It is controlled by value in
1462 * range [0..6], where 0 is most smooth and 6 is most sharp (raw image, I guess).
1463 * Recommended value is 4. Cameras model 2 do not have this feature at all.
1464 */
1465static void ibmcam_set_sharpness(struct uvd *uvd)
1466{
1467 static const char proc[] = "ibmcam_set_sharpness";
1468
1469 switch (IBMCAM_T(uvd)->camera_model) {
1470 case IBMCAM_MODEL_1:
1471 {
1472 static const unsigned short sa[] = { 0x11, 0x13, 0x16, 0x18, 0x1a, 0x8, 0x0a };
1473 unsigned short i, sv;
1474
1475 RESTRICT_TO_RANGE(sharpness, SHARPNESS_MIN, SHARPNESS_MAX);
1476 if (debug > 0)
1477 info("%s: Set sharpness to %hu.", proc, sharpness);
1478
1479 sv = sa[sharpness - SHARPNESS_MIN];
1480 for (i=0; i < 2; i++) {
1481 ibmcam_send_x_01_00_05 (uvd, unknown_88);
1482 ibmcam_send_x_00_05 (uvd, sharp_13);
1483 ibmcam_send_x_00_05_02 (uvd, sv);
1484 }
1485 break;
1486 }
1487 case IBMCAM_MODEL_2:
1488 case IBMCAM_MODEL_4:
1489 /* Models 2, 4 do not have this control */
1490 break;
1491 case IBMCAM_MODEL_3:
1492 { /*
1493 * "Use a table of magic numbers.
1494 * This setting doesn't really change much.
1495 * But that's how Windows does it."
1496 */
1497 static const struct {
1498 unsigned short sv1;
1499 unsigned short sv2;
1500 unsigned short sv3;
1501 unsigned short sv4;
1502 } sv[7] = {
1503 { 0x00, 0x00, 0x05, 0x14 }, /* Smoothest */
1504 { 0x01, 0x04, 0x05, 0x14 },
1505 { 0x02, 0x04, 0x05, 0x14 },
1506 { 0x03, 0x04, 0x05, 0x14 },
1507 { 0x03, 0x05, 0x05, 0x14 },
1508 { 0x03, 0x06, 0x05, 0x14 },
1509 { 0x03, 0x07, 0x05, 0x14 } /* Sharpest */
1510 };
1511 RESTRICT_TO_RANGE(sharpness, SHARPNESS_MIN, SHARPNESS_MAX);
1512 RESTRICT_TO_RANGE(sharpness, 0, 6);
1513 ibmcam_veio(uvd, 0, 0x0000, 0x010c); /* Stop */
1514 ibmcam_model3_Packet1(uvd, 0x0060, sv[sharpness].sv1);
1515 ibmcam_model3_Packet1(uvd, 0x0061, sv[sharpness].sv2);
1516 ibmcam_model3_Packet1(uvd, 0x0062, sv[sharpness].sv3);
1517 ibmcam_model3_Packet1(uvd, 0x0063, sv[sharpness].sv4);
1518 ibmcam_veio(uvd, 0, 0x0001, 0x0114);
1519 ibmcam_veio(uvd, 0, 0x00c0, 0x010c); /* Go! */
1520 usb_clear_halt(uvd->dev, usb_rcvisocpipe(uvd->dev, uvd->video_endp));
1521 ibmcam_veio(uvd, 0, 0x0001, 0x0113);
1522 break;
1523 }
1524 default:
1525 break;
1526 }
1527}
1528
1529/*
1530 * ibmcam_set_brightness()
1531 *
1532 * This procedure changes brightness of the picture.
1533 */
1534static void ibmcam_set_brightness(struct uvd *uvd)
1535{
1536 static const char proc[] = "ibmcam_set_brightness";
1537 static const unsigned short n = 1;
1538
1539 if (debug > 0)
1540 info("%s: Set brightness to %hu.", proc, uvd->vpic.brightness);
1541
1542 switch (IBMCAM_T(uvd)->camera_model) {
1543 case IBMCAM_MODEL_1:
1544 {
1545 unsigned short i, j, bv[3];
1546 bv[0] = bv[1] = bv[2] = uvd->vpic.brightness >> 10;
1547 if (bv[0] == (uvd->vpic_old.brightness >> 10))
1548 return;
1549 uvd->vpic_old.brightness = bv[0];
1550 for (j=0; j < 3; j++)
1551 for (i=0; i < n; i++)
1552 ibmcam_Packet_Format1(uvd, bright_3x[j], bv[j]);
1553 break;
1554 }
1555 case IBMCAM_MODEL_2:
1556 {
1557 unsigned short i, j;
1558 i = uvd->vpic.brightness >> 12; /* 0 .. 15 */
1559 j = 0x60 + i * ((0xee - 0x60) / 16); /* 0x60 .. 0xee or so */
1560 if (uvd->vpic_old.brightness == j)
1561 break;
1562 uvd->vpic_old.brightness = j;
1563 ibmcam_model2_Packet1(uvd, mod2_brightness, j);
1564 break;
1565 }
1566 case IBMCAM_MODEL_3:
1567 {
1568 /* Model 3: Brightness range 'i' in [0x0C..0x3F] */
1569 unsigned short i =
1570 0x0C + (uvd->vpic.brightness / (0xFFFF / (0x3F - 0x0C + 1)));
1571 RESTRICT_TO_RANGE(i, 0x0C, 0x3F);
1572 if (uvd->vpic_old.brightness == i)
1573 break;
1574 uvd->vpic_old.brightness = i;
1575 ibmcam_veio(uvd, 0, 0x0000, 0x010c); /* Stop */
1576 ibmcam_model3_Packet1(uvd, 0x0036, i);
1577 ibmcam_veio(uvd, 0, 0x0001, 0x0114);
1578 ibmcam_veio(uvd, 0, 0x00c0, 0x010c); /* Go! */
1579 usb_clear_halt(uvd->dev, usb_rcvisocpipe(uvd->dev, uvd->video_endp));
1580 ibmcam_veio(uvd, 0, 0x0001, 0x0113);
1581 break;
1582 }
1583 case IBMCAM_MODEL_4:
1584 {
1585 /* Model 4: Brightness range 'i' in [0x04..0xb4] */
1586 unsigned short i = 0x04 + (uvd->vpic.brightness / (0xFFFF / (0xb4 - 0x04 + 1)));
1587 RESTRICT_TO_RANGE(i, 0x04, 0xb4);
1588 if (uvd->vpic_old.brightness == i)
1589 break;
1590 uvd->vpic_old.brightness = i;
1591 ibmcam_model4_BrightnessPacket(uvd, i);
1592 break;
1593 }
1594 default:
1595 break;
1596 }
1597}
1598
1599static void ibmcam_set_hue(struct uvd *uvd)
1600{
1601 switch (IBMCAM_T(uvd)->camera_model) {
1602 case IBMCAM_MODEL_2:
1603 {
1604 unsigned short hue = uvd->vpic.hue >> 9; /* 0 .. 7F */
1605 if (uvd->vpic_old.hue == hue)
1606 return;
1607 uvd->vpic_old.hue = hue;
1608 ibmcam_model2_Packet1(uvd, mod2_hue, hue);
1609 /* ibmcam_model2_Packet1(uvd, mod2_saturation, sat); */
1610 break;
1611 }
1612 case IBMCAM_MODEL_3:
1613 {
1614#if 0 /* This seems not to work. No problem, will fix programmatically */
1615 unsigned short hue = 0x05 + (uvd->vpic.hue / (0xFFFF / (0x37 - 0x05 + 1)));
1616 RESTRICT_TO_RANGE(hue, 0x05, 0x37);
1617 if (uvd->vpic_old.hue == hue)
1618 return;
1619 uvd->vpic_old.hue = hue;
1620 ibmcam_veio(uvd, 0, 0x0000, 0x010c); /* Stop */
1621 ibmcam_model3_Packet1(uvd, 0x007e, hue);
1622 ibmcam_veio(uvd, 0, 0x0001, 0x0114);
1623 ibmcam_veio(uvd, 0, 0x00c0, 0x010c); /* Go! */
1624 usb_clear_halt(uvd->dev, usb_rcvisocpipe(uvd->dev, uvd->video_endp));
1625 ibmcam_veio(uvd, 0, 0x0001, 0x0113);
1626#endif
1627 break;
1628 }
1629 case IBMCAM_MODEL_4:
1630 {
1631 unsigned short r_gain, g_gain, b_gain, hue;
1632
1633 /*
1634 * I am not sure r/g/b_gain variables exactly control gain
1635 * of those channels. Most likely they subtly change some
1636 * very internal image processing settings in the camera.
1637 * In any case, here is what they do, and feel free to tweak:
1638 *
1639 * r_gain: seriously affects red gain
1640 * g_gain: seriously affects green gain
1641 * b_gain: seriously affects blue gain
1642 * hue: changes average color from violet (0) to red (0xFF)
1643 *
1644 * These settings are preset for a decent white balance in
1645 * 320x240, 352x288 modes. Low-res modes exhibit higher contrast
1646 * and therefore may need different values here.
1647 */
1648 hue = 20 + (uvd->vpic.hue >> 9);
1649 switch (uvd->videosize) {
1650 case VIDEOSIZE_128x96:
1651 r_gain = 90;
1652 g_gain = 166;
1653 b_gain = 175;
1654 break;
1655 case VIDEOSIZE_160x120:
1656 r_gain = 70;
1657 g_gain = 166;
1658 b_gain = 185;
1659 break;
1660 case VIDEOSIZE_176x144:
1661 r_gain = 160;
1662 g_gain = 175;
1663 b_gain = 185;
1664 break;
1665 default:
1666 r_gain = 120;
1667 g_gain = 166;
1668 b_gain = 175;
1669 break;
1670 }
1671 RESTRICT_TO_RANGE(hue, 1, 0x7f);
1672
1673 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
1674 ibmcam_veio(uvd, 0, 0x001e, 0x012f);
1675 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
1676 ibmcam_veio(uvd, 0, g_gain, 0x0127); /* Green gain */
1677 ibmcam_veio(uvd, 0, r_gain, 0x012e); /* Red gain */
1678 ibmcam_veio(uvd, 0, b_gain, 0x0130); /* Blue gain */
1679 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
1680 ibmcam_veio(uvd, 0, hue, 0x012d); /* Hue */
1681 ibmcam_veio(uvd, 0, 0xf545, 0x0124);
1682 break;
1683 }
1684 default:
1685 break;
1686 }
1687}
1688
1689/*
1690 * ibmcam_adjust_picture()
1691 *
1692 * This procedure gets called from V4L interface to update picture settings.
1693 * Here we change brightness and contrast.
1694 */
1695static void ibmcam_adjust_picture(struct uvd *uvd)
1696{
1697 ibmcam_adjust_contrast(uvd);
1698 ibmcam_set_brightness(uvd);
1699 ibmcam_set_hue(uvd);
1700}
1701
1702static int ibmcam_model1_setup(struct uvd *uvd)
1703{
1704 const int ntries = 5;
1705 int i;
1706
1707 ibmcam_veio(uvd, 1, 0x00, 0x0128);
1708 ibmcam_veio(uvd, 1, 0x00, 0x0100);
1709 ibmcam_veio(uvd, 0, 0x01, 0x0100); /* LED On */
1710 ibmcam_veio(uvd, 1, 0x00, 0x0100);
1711 ibmcam_veio(uvd, 0, 0x81, 0x0100); /* LED Off */
1712 ibmcam_veio(uvd, 1, 0x00, 0x0100);
1713 ibmcam_veio(uvd, 0, 0x01, 0x0100); /* LED On */
1714 ibmcam_veio(uvd, 0, 0x01, 0x0108);
1715
1716 ibmcam_veio(uvd, 0, 0x03, 0x0112);
1717 ibmcam_veio(uvd, 1, 0x00, 0x0115);
1718 ibmcam_veio(uvd, 0, 0x06, 0x0115);
1719 ibmcam_veio(uvd, 1, 0x00, 0x0116);
1720 ibmcam_veio(uvd, 0, 0x44, 0x0116);
1721 ibmcam_veio(uvd, 1, 0x00, 0x0116);
1722 ibmcam_veio(uvd, 0, 0x40, 0x0116);
1723 ibmcam_veio(uvd, 1, 0x00, 0x0115);
1724 ibmcam_veio(uvd, 0, 0x0e, 0x0115);
1725 ibmcam_veio(uvd, 0, 0x19, 0x012c);
1726
1727 ibmcam_Packet_Format1(uvd, 0x00, 0x1e);
1728 ibmcam_Packet_Format1(uvd, 0x39, 0x0d);
1729 ibmcam_Packet_Format1(uvd, 0x39, 0x09);
1730 ibmcam_Packet_Format1(uvd, 0x3b, 0x00);
1731 ibmcam_Packet_Format1(uvd, 0x28, 0x22);
1732 ibmcam_Packet_Format1(uvd, light_27, 0);
1733 ibmcam_Packet_Format1(uvd, 0x2b, 0x1f);
1734 ibmcam_Packet_Format1(uvd, 0x39, 0x08);
1735
1736 for (i=0; i < ntries; i++)
1737 ibmcam_Packet_Format1(uvd, 0x2c, 0x00);
1738
1739 for (i=0; i < ntries; i++)
1740 ibmcam_Packet_Format1(uvd, 0x30, 0x14);
1741
1742 ibmcam_PacketFormat2(uvd, 0x39, 0x02);
1743 ibmcam_PacketFormat2(uvd, 0x01, 0xe1);
1744 ibmcam_PacketFormat2(uvd, 0x02, 0xcd);
1745 ibmcam_PacketFormat2(uvd, 0x03, 0xcd);
1746 ibmcam_PacketFormat2(uvd, 0x04, 0xfa);
1747 ibmcam_PacketFormat2(uvd, 0x3f, 0xff);
1748 ibmcam_PacketFormat2(uvd, 0x39, 0x00);
1749
1750 ibmcam_PacketFormat2(uvd, 0x39, 0x02);
1751 ibmcam_PacketFormat2(uvd, 0x0a, 0x37);
1752 ibmcam_PacketFormat2(uvd, 0x0b, 0xb8);
1753 ibmcam_PacketFormat2(uvd, 0x0c, 0xf3);
1754 ibmcam_PacketFormat2(uvd, 0x0d, 0xe3);
1755 ibmcam_PacketFormat2(uvd, 0x0e, 0x0d);
1756 ibmcam_PacketFormat2(uvd, 0x0f, 0xf2);
1757 ibmcam_PacketFormat2(uvd, 0x10, 0xd5);
1758 ibmcam_PacketFormat2(uvd, 0x11, 0xba);
1759 ibmcam_PacketFormat2(uvd, 0x12, 0x53);
1760 ibmcam_PacketFormat2(uvd, 0x3f, 0xff);
1761 ibmcam_PacketFormat2(uvd, 0x39, 0x00);
1762
1763 ibmcam_PacketFormat2(uvd, 0x39, 0x02);
1764 ibmcam_PacketFormat2(uvd, 0x16, 0x00);
1765 ibmcam_PacketFormat2(uvd, 0x17, 0x28);
1766 ibmcam_PacketFormat2(uvd, 0x18, 0x7d);
1767 ibmcam_PacketFormat2(uvd, 0x19, 0xbe);
1768 ibmcam_PacketFormat2(uvd, 0x3f, 0xff);
1769 ibmcam_PacketFormat2(uvd, 0x39, 0x00);
1770
1771 for (i=0; i < ntries; i++)
1772 ibmcam_Packet_Format1(uvd, 0x00, 0x18);
1773 for (i=0; i < ntries; i++)
1774 ibmcam_Packet_Format1(uvd, 0x13, 0x18);
1775 for (i=0; i < ntries; i++)
1776 ibmcam_Packet_Format1(uvd, 0x14, 0x06);
1777
1778 /* This is default brightness */
1779 for (i=0; i < ntries; i++)
1780 ibmcam_Packet_Format1(uvd, 0x31, 0x37);
1781 for (i=0; i < ntries; i++)
1782 ibmcam_Packet_Format1(uvd, 0x32, 0x46);
1783 for (i=0; i < ntries; i++)
1784 ibmcam_Packet_Format1(uvd, 0x33, 0x55);
1785
1786 ibmcam_Packet_Format1(uvd, 0x2e, 0x04);
1787 for (i=0; i < ntries; i++)
1788 ibmcam_Packet_Format1(uvd, 0x2d, 0x04);
1789 for (i=0; i < ntries; i++)
1790 ibmcam_Packet_Format1(uvd, 0x29, 0x80);
1791 ibmcam_Packet_Format1(uvd, 0x2c, 0x01);
1792 ibmcam_Packet_Format1(uvd, 0x30, 0x17);
1793 ibmcam_Packet_Format1(uvd, 0x39, 0x08);
1794 for (i=0; i < ntries; i++)
1795 ibmcam_Packet_Format1(uvd, 0x34, 0x00);
1796
1797 ibmcam_veio(uvd, 0, 0x00, 0x0101);
1798 ibmcam_veio(uvd, 0, 0x00, 0x010a);
1799
1800 switch (uvd->videosize) {
1801 case VIDEOSIZE_128x96:
1802 ibmcam_veio(uvd, 0, 0x80, 0x0103);
1803 ibmcam_veio(uvd, 0, 0x60, 0x0105);
1804 ibmcam_veio(uvd, 0, 0x0c, 0x010b);
1805 ibmcam_veio(uvd, 0, 0x04, 0x011b); /* Same everywhere */
1806 ibmcam_veio(uvd, 0, 0x0b, 0x011d);
1807 ibmcam_veio(uvd, 0, 0x00, 0x011e); /* Same everywhere */
1808 ibmcam_veio(uvd, 0, 0x00, 0x0129);
1809 break;
1810 case VIDEOSIZE_176x144:
1811 ibmcam_veio(uvd, 0, 0xb0, 0x0103);
1812 ibmcam_veio(uvd, 0, 0x8f, 0x0105);
1813 ibmcam_veio(uvd, 0, 0x06, 0x010b);
1814 ibmcam_veio(uvd, 0, 0x04, 0x011b); /* Same everywhere */
1815 ibmcam_veio(uvd, 0, 0x0d, 0x011d);
1816 ibmcam_veio(uvd, 0, 0x00, 0x011e); /* Same everywhere */
1817 ibmcam_veio(uvd, 0, 0x03, 0x0129);
1818 break;
1819 case VIDEOSIZE_352x288:
1820 ibmcam_veio(uvd, 0, 0xb0, 0x0103);
1821 ibmcam_veio(uvd, 0, 0x90, 0x0105);
1822 ibmcam_veio(uvd, 0, 0x02, 0x010b);
1823 ibmcam_veio(uvd, 0, 0x04, 0x011b); /* Same everywhere */
1824 ibmcam_veio(uvd, 0, 0x05, 0x011d);
1825 ibmcam_veio(uvd, 0, 0x00, 0x011e); /* Same everywhere */
1826 ibmcam_veio(uvd, 0, 0x00, 0x0129);
1827 break;
1828 }
1829
1830 ibmcam_veio(uvd, 0, 0xff, 0x012b);
1831
1832 /* This is another brightness - don't know why */
1833 for (i=0; i < ntries; i++)
1834 ibmcam_Packet_Format1(uvd, 0x31, 0xc3);
1835 for (i=0; i < ntries; i++)
1836 ibmcam_Packet_Format1(uvd, 0x32, 0xd2);
1837 for (i=0; i < ntries; i++)
1838 ibmcam_Packet_Format1(uvd, 0x33, 0xe1);
1839
1840 /* Default contrast */
1841 for (i=0; i < ntries; i++)
1842 ibmcam_Packet_Format1(uvd, contrast_14, 0x0a);
1843
1844 /* Default sharpness */
1845 for (i=0; i < 2; i++)
1846 ibmcam_PacketFormat2(uvd, sharp_13, 0x1a); /* Level 4 FIXME */
1847
1848 /* Default lighting conditions */
1849 ibmcam_Packet_Format1(uvd, light_27, lighting); /* 0=Bright 2=Low */
1850
1851 /* Assorted init */
1852
1853 switch (uvd->videosize) {
1854 case VIDEOSIZE_128x96:
1855 ibmcam_Packet_Format1(uvd, 0x2b, 0x1e);
1856 ibmcam_veio(uvd, 0, 0xc9, 0x0119); /* Same everywhere */
1857 ibmcam_veio(uvd, 0, 0x80, 0x0109); /* Same everywhere */
1858 ibmcam_veio(uvd, 0, 0x36, 0x0102);
1859 ibmcam_veio(uvd, 0, 0x1a, 0x0104);
1860 ibmcam_veio(uvd, 0, 0x04, 0x011a); /* Same everywhere */
1861 ibmcam_veio(uvd, 0, 0x2b, 0x011c);
1862 ibmcam_veio(uvd, 0, 0x23, 0x012a); /* Same everywhere */
1863#if 0
1864 ibmcam_veio(uvd, 0, 0x00, 0x0106);
1865 ibmcam_veio(uvd, 0, 0x38, 0x0107);
1866#else
1867 ibmcam_veio(uvd, 0, 0x02, 0x0106);
1868 ibmcam_veio(uvd, 0, 0x2a, 0x0107);
1869#endif
1870 break;
1871 case VIDEOSIZE_176x144:
1872 ibmcam_Packet_Format1(uvd, 0x2b, 0x1e);
1873 ibmcam_veio(uvd, 0, 0xc9, 0x0119); /* Same everywhere */
1874 ibmcam_veio(uvd, 0, 0x80, 0x0109); /* Same everywhere */
1875 ibmcam_veio(uvd, 0, 0x04, 0x0102);
1876 ibmcam_veio(uvd, 0, 0x02, 0x0104);
1877 ibmcam_veio(uvd, 0, 0x04, 0x011a); /* Same everywhere */
1878 ibmcam_veio(uvd, 0, 0x2b, 0x011c);
1879 ibmcam_veio(uvd, 0, 0x23, 0x012a); /* Same everywhere */
1880 ibmcam_veio(uvd, 0, 0x01, 0x0106);
1881 ibmcam_veio(uvd, 0, 0xca, 0x0107);
1882 break;
1883 case VIDEOSIZE_352x288:
1884 ibmcam_Packet_Format1(uvd, 0x2b, 0x1f);
1885 ibmcam_veio(uvd, 0, 0xc9, 0x0119); /* Same everywhere */
1886 ibmcam_veio(uvd, 0, 0x80, 0x0109); /* Same everywhere */
1887 ibmcam_veio(uvd, 0, 0x08, 0x0102);
1888 ibmcam_veio(uvd, 0, 0x01, 0x0104);
1889 ibmcam_veio(uvd, 0, 0x04, 0x011a); /* Same everywhere */
1890 ibmcam_veio(uvd, 0, 0x2f, 0x011c);
1891 ibmcam_veio(uvd, 0, 0x23, 0x012a); /* Same everywhere */
1892 ibmcam_veio(uvd, 0, 0x03, 0x0106);
1893 ibmcam_veio(uvd, 0, 0xf6, 0x0107);
1894 break;
1895 }
1896 return (CAMERA_IS_OPERATIONAL(uvd) ? 0 : -EFAULT);
1897}
1898
1899static int ibmcam_model2_setup(struct uvd *uvd)
1900{
1901 ibmcam_veio(uvd, 0, 0x0000, 0x0100); /* LED on */
1902 ibmcam_veio(uvd, 1, 0x0000, 0x0116);
1903 ibmcam_veio(uvd, 0, 0x0060, 0x0116);
1904 ibmcam_veio(uvd, 0, 0x0002, 0x0112);
1905 ibmcam_veio(uvd, 0, 0x00bc, 0x012c);
1906 ibmcam_veio(uvd, 0, 0x0008, 0x012b);
1907 ibmcam_veio(uvd, 0, 0x0000, 0x0108);
1908 ibmcam_veio(uvd, 0, 0x0001, 0x0133);
1909 ibmcam_veio(uvd, 0, 0x0001, 0x0102);
1910 switch (uvd->videosize) {
1911 case VIDEOSIZE_176x144:
1912 ibmcam_veio(uvd, 0, 0x002c, 0x0103); /* All except 320x240 */
1913 ibmcam_veio(uvd, 0, 0x0000, 0x0104); /* Same */
1914 ibmcam_veio(uvd, 0, 0x0024, 0x0105); /* 176x144, 352x288 */
1915 ibmcam_veio(uvd, 0, 0x00b9, 0x010a); /* Unique to this mode */
1916 ibmcam_veio(uvd, 0, 0x0038, 0x0119); /* Unique to this mode */
1917 ibmcam_veio(uvd, 0, 0x0003, 0x0106); /* Same */
1918 ibmcam_veio(uvd, 0, 0x0090, 0x0107); /* Unique to every mode*/
1919 break;
1920 case VIDEOSIZE_320x240:
1921 ibmcam_veio(uvd, 0, 0x0028, 0x0103); /* Unique to this mode */
1922 ibmcam_veio(uvd, 0, 0x0000, 0x0104); /* Same */
1923 ibmcam_veio(uvd, 0, 0x001e, 0x0105); /* 320x240, 352x240 */
1924 ibmcam_veio(uvd, 0, 0x0039, 0x010a); /* All except 176x144 */
1925 ibmcam_veio(uvd, 0, 0x0070, 0x0119); /* All except 176x144 */
1926 ibmcam_veio(uvd, 0, 0x0003, 0x0106); /* Same */
1927 ibmcam_veio(uvd, 0, 0x0098, 0x0107); /* Unique to every mode*/
1928 break;
1929 case VIDEOSIZE_352x240:
1930 ibmcam_veio(uvd, 0, 0x002c, 0x0103); /* All except 320x240 */
1931 ibmcam_veio(uvd, 0, 0x0000, 0x0104); /* Same */
1932 ibmcam_veio(uvd, 0, 0x001e, 0x0105); /* 320x240, 352x240 */
1933 ibmcam_veio(uvd, 0, 0x0039, 0x010a); /* All except 176x144 */
1934 ibmcam_veio(uvd, 0, 0x0070, 0x0119); /* All except 176x144 */
1935 ibmcam_veio(uvd, 0, 0x0003, 0x0106); /* Same */
1936 ibmcam_veio(uvd, 0, 0x00da, 0x0107); /* Unique to every mode*/
1937 break;
1938 case VIDEOSIZE_352x288:
1939 ibmcam_veio(uvd, 0, 0x002c, 0x0103); /* All except 320x240 */
1940 ibmcam_veio(uvd, 0, 0x0000, 0x0104); /* Same */
1941 ibmcam_veio(uvd, 0, 0x0024, 0x0105); /* 176x144, 352x288 */
1942 ibmcam_veio(uvd, 0, 0x0039, 0x010a); /* All except 176x144 */
1943 ibmcam_veio(uvd, 0, 0x0070, 0x0119); /* All except 176x144 */
1944 ibmcam_veio(uvd, 0, 0x0003, 0x0106); /* Same */
1945 ibmcam_veio(uvd, 0, 0x00fe, 0x0107); /* Unique to every mode*/
1946 break;
1947 }
1948 return (CAMERA_IS_OPERATIONAL(uvd) ? 0 : -EFAULT);
1949}
1950
1951/*
1952 * ibmcam_model1_setup_after_video_if()
1953 *
1954 * This code adds finishing touches to the video data interface.
1955 * Here we configure the frame rate and turn on the LED.
1956 */
1957static void ibmcam_model1_setup_after_video_if(struct uvd *uvd)
1958{
1959 unsigned short internal_frame_rate;
1960
1961 RESTRICT_TO_RANGE(framerate, FRAMERATE_MIN, FRAMERATE_MAX);
1962 internal_frame_rate = FRAMERATE_MAX - framerate; /* 0=Fast 6=Slow */
1963 ibmcam_veio(uvd, 0, 0x01, 0x0100); /* LED On */
1964 ibmcam_veio(uvd, 0, internal_frame_rate, 0x0111);
1965 ibmcam_veio(uvd, 0, 0x01, 0x0114);
1966 ibmcam_veio(uvd, 0, 0xc0, 0x010c);
1967}
1968
1969static void ibmcam_model2_setup_after_video_if(struct uvd *uvd)
1970{
1971 unsigned short setup_model2_rg2, setup_model2_sat, setup_model2_yb;
1972
1973 ibmcam_veio(uvd, 0, 0x0000, 0x0100); /* LED on */
1974
1975 switch (uvd->videosize) {
1976 case VIDEOSIZE_176x144:
1977 ibmcam_veio(uvd, 0, 0x0050, 0x0111);
1978 ibmcam_veio(uvd, 0, 0x00d0, 0x0111);
1979 break;
1980 case VIDEOSIZE_320x240:
1981 case VIDEOSIZE_352x240:
1982 case VIDEOSIZE_352x288:
1983 ibmcam_veio(uvd, 0, 0x0040, 0x0111);
1984 ibmcam_veio(uvd, 0, 0x00c0, 0x0111);
1985 break;
1986 }
1987 ibmcam_veio(uvd, 0, 0x009b, 0x010f);
1988 ibmcam_veio(uvd, 0, 0x00bb, 0x010f);
1989
1990 /*
1991 * Hardware settings, may affect CMOS sensor; not user controls!
1992 * -------------------------------------------------------------
1993 * 0x0004: no effect
1994 * 0x0006: hardware effect
1995 * 0x0008: no effect
1996 * 0x000a: stops video stream, probably important h/w setting
1997 * 0x000c: changes color in hardware manner (not user setting)
1998 * 0x0012: changes number of colors (does not affect speed)
1999 * 0x002a: no effect
2000 * 0x002c: hardware setting (related to scan lines)
2001 * 0x002e: stops video stream, probably important h/w setting
2002 */
2003 ibmcam_model2_Packet1(uvd, 0x000a, 0x005c);
2004 ibmcam_model2_Packet1(uvd, 0x0004, 0x0000);
2005 ibmcam_model2_Packet1(uvd, 0x0006, 0x00fb);
2006 ibmcam_model2_Packet1(uvd, 0x0008, 0x0000);
2007 ibmcam_model2_Packet1(uvd, 0x000c, 0x0009);
2008 ibmcam_model2_Packet1(uvd, 0x0012, 0x000a);
2009 ibmcam_model2_Packet1(uvd, 0x002a, 0x0000);
2010 ibmcam_model2_Packet1(uvd, 0x002c, 0x0000);
2011 ibmcam_model2_Packet1(uvd, 0x002e, 0x0008);
2012
2013 /*
2014 * Function 0x0030 pops up all over the place. Apparently
2015 * it is a hardware control register, with every bit assigned to
2016 * do something.
2017 */
2018 ibmcam_model2_Packet1(uvd, 0x0030, 0x0000);
2019
2020 /*
2021 * Magic control of CMOS sensor. Only lower values like
2022 * 0-3 work, and picture shifts left or right. Don't change.
2023 */
2024 switch (uvd->videosize) {
2025 case VIDEOSIZE_176x144:
2026 ibmcam_model2_Packet1(uvd, 0x0014, 0x0002);
2027 ibmcam_model2_Packet1(uvd, 0x0016, 0x0002); /* Horizontal shift */
2028 ibmcam_model2_Packet1(uvd, 0x0018, 0x004a); /* Another hardware setting */
2029 break;
2030 case VIDEOSIZE_320x240:
2031 ibmcam_model2_Packet1(uvd, 0x0014, 0x0009);
2032 ibmcam_model2_Packet1(uvd, 0x0016, 0x0005); /* Horizontal shift */
2033 ibmcam_model2_Packet1(uvd, 0x0018, 0x0044); /* Another hardware setting */
2034 break;
2035 case VIDEOSIZE_352x240:
2036 /* This mode doesn't work as Windows programs it; changed to work */
2037 ibmcam_model2_Packet1(uvd, 0x0014, 0x0009); /* Windows sets this to 8 */
2038 ibmcam_model2_Packet1(uvd, 0x0016, 0x0003); /* Horizontal shift */
2039 ibmcam_model2_Packet1(uvd, 0x0018, 0x0044); /* Windows sets this to 0x0045 */
2040 break;
2041 case VIDEOSIZE_352x288:
2042 ibmcam_model2_Packet1(uvd, 0x0014, 0x0003);
2043 ibmcam_model2_Packet1(uvd, 0x0016, 0x0002); /* Horizontal shift */
2044 ibmcam_model2_Packet1(uvd, 0x0018, 0x004a); /* Another hardware setting */
2045 break;
2046 }
2047
2048 ibmcam_model2_Packet1(uvd, mod2_brightness, 0x005a);
2049
2050 /*
2051 * We have our own frame rate setting varying from 0 (slowest) to 6 (fastest).
2052 * The camera model 2 allows frame rate in range [0..0x1F] where 0 is also the
2053 * slowest setting. However for all practical reasons high settings make no
2054 * sense because USB is not fast enough to support high FPS. Be aware that
2055 * the picture datastream will be severely disrupted if you ask for
2056 * frame rate faster than allowed for the video size - see below:
2057 *
2058 * Allowable ranges (obtained experimentally on OHCI, K6-3, 450 MHz):
2059 * -----------------------------------------------------------------
2060 * 176x144: [6..31]
2061 * 320x240: [8..31]
2062 * 352x240: [10..31]
2063 * 352x288: [16..31] I have to raise lower threshold for stability...
2064 *
2065 * As usual, slower FPS provides better sensitivity.
2066 */
2067 {
2068 short hw_fps=31, i_framerate;
2069
2070 RESTRICT_TO_RANGE(framerate, FRAMERATE_MIN, FRAMERATE_MAX);
2071 i_framerate = FRAMERATE_MAX - framerate + FRAMERATE_MIN;
2072 switch (uvd->videosize) {
2073 case VIDEOSIZE_176x144:
2074 hw_fps = 6 + i_framerate*4;
2075 break;
2076 case VIDEOSIZE_320x240:
2077 hw_fps = 8 + i_framerate*3;
2078 break;
2079 case VIDEOSIZE_352x240:
2080 hw_fps = 10 + i_framerate*2;
2081 break;
2082 case VIDEOSIZE_352x288:
2083 hw_fps = 28 + i_framerate/2;
2084 break;
2085 }
2086 if (uvd->debug > 0)
2087 info("Framerate (hardware): %hd.", hw_fps);
2088 RESTRICT_TO_RANGE(hw_fps, 0, 31);
2089 ibmcam_model2_Packet1(uvd, mod2_set_framerate, hw_fps);
2090 }
2091
2092 /*
2093 * This setting does not visibly affect pictures; left it here
2094 * because it was present in Windows USB data stream. This function
2095 * does not allow arbitrary values and apparently is a bit mask, to
2096 * be activated only at appropriate time. Don't change it randomly!
2097 */
2098 switch (uvd->videosize) {
2099 case VIDEOSIZE_176x144:
2100 ibmcam_model2_Packet1(uvd, 0x0026, 0x00c2);
2101 break;
2102 case VIDEOSIZE_320x240:
2103 ibmcam_model2_Packet1(uvd, 0x0026, 0x0044);
2104 break;
2105 case VIDEOSIZE_352x240:
2106 ibmcam_model2_Packet1(uvd, 0x0026, 0x0046);
2107 break;
2108 case VIDEOSIZE_352x288:
2109 ibmcam_model2_Packet1(uvd, 0x0026, 0x0048);
2110 break;
2111 }
2112
2113 ibmcam_model2_Packet1(uvd, mod2_sensitivity, lighting);
2114
2115 if (init_model2_rg2 >= 0) {
2116 RESTRICT_TO_RANGE(init_model2_rg2, 0, 255);
2117 setup_model2_rg2 = init_model2_rg2;
2118 } else
2119 setup_model2_rg2 = 0x002f;
2120
2121 if (init_model2_sat >= 0) {
2122 RESTRICT_TO_RANGE(init_model2_sat, 0, 255);
2123 setup_model2_sat = init_model2_sat;
2124 } else
2125 setup_model2_sat = 0x0034;
2126
2127 if (init_model2_yb >= 0) {
2128 RESTRICT_TO_RANGE(init_model2_yb, 0, 255);
2129 setup_model2_yb = init_model2_yb;
2130 } else
2131 setup_model2_yb = 0x00a0;
2132
2133 ibmcam_model2_Packet1(uvd, mod2_color_balance_rg2, setup_model2_rg2);
2134 ibmcam_model2_Packet1(uvd, mod2_saturation, setup_model2_sat);
2135 ibmcam_model2_Packet1(uvd, mod2_color_balance_yb, setup_model2_yb);
2136 ibmcam_model2_Packet1(uvd, mod2_hue, uvd->vpic.hue >> 9); /* 0 .. 7F */;
2137
2138 /* Hardware control command */
2139 ibmcam_model2_Packet1(uvd, 0x0030, 0x0004);
2140
2141 ibmcam_veio(uvd, 0, 0x00c0, 0x010c); /* Go camera, go! */
2142 usb_clear_halt(uvd->dev, usb_rcvisocpipe(uvd->dev, uvd->video_endp));
2143}
2144
2145static void ibmcam_model4_setup_after_video_if(struct uvd *uvd)
2146{
2147 switch (uvd->videosize) {
2148 case VIDEOSIZE_128x96:
2149 ibmcam_veio(uvd, 0, 0x0000, 0x0100);
2150 ibmcam_veio(uvd, 0, 0x00c0, 0x0111);
2151 ibmcam_veio(uvd, 0, 0x00bc, 0x012c);
2152 ibmcam_veio(uvd, 0, 0x0080, 0x012b);
2153 ibmcam_veio(uvd, 0, 0x0000, 0x0108);
2154 ibmcam_veio(uvd, 0, 0x0001, 0x0133);
2155 ibmcam_veio(uvd, 0, 0x009b, 0x010f);
2156 ibmcam_veio(uvd, 0, 0x00bb, 0x010f);
2157 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2158 ibmcam_veio(uvd, 0, 0x0038, 0x012f);
2159 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2160 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2161 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2162 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2163 ibmcam_veio(uvd, 0, 0x000a, 0x012f);
2164 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2165 ibmcam_veio(uvd, 0, 0x005c, 0x0127);
2166 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2167 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2168 ibmcam_veio(uvd, 0, 0x0004, 0x012f);
2169 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2170 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2171 ibmcam_veio(uvd, 0, 0x00fb, 0x012e);
2172 ibmcam_veio(uvd, 0, 0x0000, 0x0130);
2173 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2174 ibmcam_veio(uvd, 0, 0x00aa, 0x012f);
2175 ibmcam_veio(uvd, 0, 0xd055, 0x0124);
2176 ibmcam_veio(uvd, 0, 0x000c, 0x0127);
2177 ibmcam_veio(uvd, 0, 0x0009, 0x012e);
2178 ibmcam_veio(uvd, 0, 0xaa28, 0x0124);
2179 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2180 ibmcam_veio(uvd, 0, 0x0012, 0x012f);
2181 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2182 ibmcam_veio(uvd, 0, 0x0008, 0x0127);
2183 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2184 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2185 ibmcam_veio(uvd, 0, 0x002a, 0x012d);
2186 ibmcam_veio(uvd, 0, 0x0000, 0x012f);
2187 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2188 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
2189 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2190 ibmcam_veio(uvd, 0, 0x0034, 0x012f);
2191 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2192 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2193 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2194 ibmcam_veio(uvd, 0, 0x0070, 0x0119);
2195 ibmcam_veio(uvd, 0, 0x00d2, 0x0107);
2196 ibmcam_veio(uvd, 0, 0x0003, 0x0106);
2197 ibmcam_veio(uvd, 0, 0x005e, 0x0107);
2198 ibmcam_veio(uvd, 0, 0x0003, 0x0106);
2199 ibmcam_veio(uvd, 0, 0x00d0, 0x0111);
2200 ibmcam_veio(uvd, 0, 0x0039, 0x010a);
2201 ibmcam_veio(uvd, 0, 0x0001, 0x0102);
2202 ibmcam_veio(uvd, 0, 0x0028, 0x0103);
2203 ibmcam_veio(uvd, 0, 0x0000, 0x0104);
2204 ibmcam_veio(uvd, 0, 0x001e, 0x0105);
2205 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2206 ibmcam_veio(uvd, 0, 0x0016, 0x012f);
2207 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2208 ibmcam_veio(uvd, 0, 0x000a, 0x0127);
2209 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2210 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2211 ibmcam_veio(uvd, 0, 0x0014, 0x012d);
2212 ibmcam_veio(uvd, 0, 0x0008, 0x012f);
2213 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2214 ibmcam_veio(uvd, 0, 0x00aa, 0x012e);
2215 ibmcam_veio(uvd, 0, 0x001a, 0x0130);
2216 ibmcam_veio(uvd, 0, 0x8a0a, 0x0124);
2217 ibmcam_veio(uvd, 0, 0x005a, 0x012d);
2218 ibmcam_veio(uvd, 0, 0x9545, 0x0124);
2219 ibmcam_veio(uvd, 0, 0x00aa, 0x0127);
2220 ibmcam_veio(uvd, 0, 0x0018, 0x012e);
2221 ibmcam_veio(uvd, 0, 0x0043, 0x0130);
2222 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2223 ibmcam_veio(uvd, 0, 0x00aa, 0x012f);
2224 ibmcam_veio(uvd, 0, 0xd055, 0x0124);
2225 ibmcam_veio(uvd, 0, 0x001c, 0x0127);
2226 ibmcam_veio(uvd, 0, 0x00eb, 0x012e);
2227 ibmcam_veio(uvd, 0, 0xaa28, 0x0124);
2228 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2229 ibmcam_veio(uvd, 0, 0x0032, 0x012f);
2230 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2231 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2232 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2233 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2234 ibmcam_veio(uvd, 0, 0x0036, 0x012d);
2235 ibmcam_veio(uvd, 0, 0x0008, 0x012f);
2236 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2237 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
2238 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2239 ibmcam_veio(uvd, 0, 0x001e, 0x012f);
2240 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2241 ibmcam_veio(uvd, 0, 0x0017, 0x0127);
2242 ibmcam_veio(uvd, 0, 0x0013, 0x012e);
2243 ibmcam_veio(uvd, 0, 0x0031, 0x0130);
2244 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2245 ibmcam_veio(uvd, 0, 0x0017, 0x012d);
2246 ibmcam_veio(uvd, 0, 0x0078, 0x012f);
2247 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2248 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2249 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2250 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2251 ibmcam_veio(uvd, 0, 0x0038, 0x012f);
2252 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2253 ibmcam_veio(uvd, 0, 0x0004, 0x0127);
2254 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2255 ibmcam_veio(uvd, 0, 0x00c0, 0x010c);
2256 break;
2257 case VIDEOSIZE_160x120:
2258 ibmcam_veio(uvd, 0, 0x0000, 0x0100);
2259 ibmcam_veio(uvd, 0, 0x00c0, 0x0111);
2260 ibmcam_veio(uvd, 0, 0x00bc, 0x012c);
2261 ibmcam_veio(uvd, 0, 0x0080, 0x012b);
2262 ibmcam_veio(uvd, 0, 0x0000, 0x0108);
2263 ibmcam_veio(uvd, 0, 0x0001, 0x0133);
2264 ibmcam_veio(uvd, 0, 0x009b, 0x010f);
2265 ibmcam_veio(uvd, 0, 0x00bb, 0x010f);
2266 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2267 ibmcam_veio(uvd, 0, 0x0038, 0x012f);
2268 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2269 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2270 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2271 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2272 ibmcam_veio(uvd, 0, 0x000a, 0x012f);
2273 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2274 ibmcam_veio(uvd, 0, 0x005c, 0x0127);
2275 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2276 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2277 ibmcam_veio(uvd, 0, 0x0004, 0x012f);
2278 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2279 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2280 ibmcam_veio(uvd, 0, 0x00fb, 0x012e);
2281 ibmcam_veio(uvd, 0, 0x0000, 0x0130);
2282 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2283 ibmcam_veio(uvd, 0, 0x00aa, 0x012f);
2284 ibmcam_veio(uvd, 0, 0xd055, 0x0124);
2285 ibmcam_veio(uvd, 0, 0x000c, 0x0127);
2286 ibmcam_veio(uvd, 0, 0x0009, 0x012e);
2287 ibmcam_veio(uvd, 0, 0xaa28, 0x0124);
2288 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2289 ibmcam_veio(uvd, 0, 0x0012, 0x012f);
2290 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2291 ibmcam_veio(uvd, 0, 0x0008, 0x0127);
2292 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2293 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2294 ibmcam_veio(uvd, 0, 0x002a, 0x012d);
2295 ibmcam_veio(uvd, 0, 0x0000, 0x012f);
2296 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2297 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
2298 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2299 ibmcam_veio(uvd, 0, 0x0034, 0x012f);
2300 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2301 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2302 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2303 ibmcam_veio(uvd, 0, 0x0038, 0x0119);
2304 ibmcam_veio(uvd, 0, 0x00d8, 0x0107);
2305 ibmcam_veio(uvd, 0, 0x0002, 0x0106);
2306 ibmcam_veio(uvd, 0, 0x00d0, 0x0111);
2307 ibmcam_veio(uvd, 0, 0x00b9, 0x010a);
2308 ibmcam_veio(uvd, 0, 0x0001, 0x0102);
2309 ibmcam_veio(uvd, 0, 0x0028, 0x0103);
2310 ibmcam_veio(uvd, 0, 0x0000, 0x0104);
2311 ibmcam_veio(uvd, 0, 0x001e, 0x0105);
2312 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2313 ibmcam_veio(uvd, 0, 0x0016, 0x012f);
2314 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2315 ibmcam_veio(uvd, 0, 0x000b, 0x0127);
2316 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2317 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2318 ibmcam_veio(uvd, 0, 0x0014, 0x012d);
2319 ibmcam_veio(uvd, 0, 0x0008, 0x012f);
2320 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2321 ibmcam_veio(uvd, 0, 0x00aa, 0x012e);
2322 ibmcam_veio(uvd, 0, 0x001a, 0x0130);
2323 ibmcam_veio(uvd, 0, 0x8a0a, 0x0124);
2324 ibmcam_veio(uvd, 0, 0x005a, 0x012d);
2325 ibmcam_veio(uvd, 0, 0x9545, 0x0124);
2326 ibmcam_veio(uvd, 0, 0x00aa, 0x0127);
2327 ibmcam_veio(uvd, 0, 0x0018, 0x012e);
2328 ibmcam_veio(uvd, 0, 0x0043, 0x0130);
2329 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2330 ibmcam_veio(uvd, 0, 0x00aa, 0x012f);
2331 ibmcam_veio(uvd, 0, 0xd055, 0x0124);
2332 ibmcam_veio(uvd, 0, 0x001c, 0x0127);
2333 ibmcam_veio(uvd, 0, 0x00c7, 0x012e);
2334 ibmcam_veio(uvd, 0, 0xaa28, 0x0124);
2335 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2336 ibmcam_veio(uvd, 0, 0x0032, 0x012f);
2337 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2338 ibmcam_veio(uvd, 0, 0x0025, 0x0127);
2339 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2340 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2341 ibmcam_veio(uvd, 0, 0x0036, 0x012d);
2342 ibmcam_veio(uvd, 0, 0x0008, 0x012f);
2343 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2344 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
2345 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2346 ibmcam_veio(uvd, 0, 0x001e, 0x012f);
2347 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2348 ibmcam_veio(uvd, 0, 0x0048, 0x0127);
2349 ibmcam_veio(uvd, 0, 0x0035, 0x012e);
2350 ibmcam_veio(uvd, 0, 0x00d0, 0x0130);
2351 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2352 ibmcam_veio(uvd, 0, 0x0048, 0x012d);
2353 ibmcam_veio(uvd, 0, 0x0090, 0x012f);
2354 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2355 ibmcam_veio(uvd, 0, 0x0001, 0x0127);
2356 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2357 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2358 ibmcam_veio(uvd, 0, 0x0038, 0x012f);
2359 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2360 ibmcam_veio(uvd, 0, 0x0004, 0x0127);
2361 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2362 ibmcam_veio(uvd, 0, 0x00c0, 0x010c);
2363 break;
2364 case VIDEOSIZE_176x144:
2365 ibmcam_veio(uvd, 0, 0x0000, 0x0100);
2366 ibmcam_veio(uvd, 0, 0x00c0, 0x0111);
2367 ibmcam_veio(uvd, 0, 0x00bc, 0x012c);
2368 ibmcam_veio(uvd, 0, 0x0080, 0x012b);
2369 ibmcam_veio(uvd, 0, 0x0000, 0x0108);
2370 ibmcam_veio(uvd, 0, 0x0001, 0x0133);
2371 ibmcam_veio(uvd, 0, 0x009b, 0x010f);
2372 ibmcam_veio(uvd, 0, 0x00bb, 0x010f);
2373 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2374 ibmcam_veio(uvd, 0, 0x0038, 0x012f);
2375 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2376 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2377 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2378 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2379 ibmcam_veio(uvd, 0, 0x000a, 0x012f);
2380 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2381 ibmcam_veio(uvd, 0, 0x005c, 0x0127);
2382 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2383 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2384 ibmcam_veio(uvd, 0, 0x0004, 0x012f);
2385 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2386 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2387 ibmcam_veio(uvd, 0, 0x00fb, 0x012e);
2388 ibmcam_veio(uvd, 0, 0x0000, 0x0130);
2389 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2390 ibmcam_veio(uvd, 0, 0x00aa, 0x012f);
2391 ibmcam_veio(uvd, 0, 0xd055, 0x0124);
2392 ibmcam_veio(uvd, 0, 0x000c, 0x0127);
2393 ibmcam_veio(uvd, 0, 0x0009, 0x012e);
2394 ibmcam_veio(uvd, 0, 0xaa28, 0x0124);
2395 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2396 ibmcam_veio(uvd, 0, 0x0012, 0x012f);
2397 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2398 ibmcam_veio(uvd, 0, 0x0008, 0x0127);
2399 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2400 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2401 ibmcam_veio(uvd, 0, 0x002a, 0x012d);
2402 ibmcam_veio(uvd, 0, 0x0000, 0x012f);
2403 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2404 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
2405 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2406 ibmcam_veio(uvd, 0, 0x0034, 0x012f);
2407 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2408 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2409 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2410 ibmcam_veio(uvd, 0, 0x0038, 0x0119);
2411 ibmcam_veio(uvd, 0, 0x00d6, 0x0107);
2412 ibmcam_veio(uvd, 0, 0x0003, 0x0106);
2413 ibmcam_veio(uvd, 0, 0x0018, 0x0107);
2414 ibmcam_veio(uvd, 0, 0x0003, 0x0106);
2415 ibmcam_veio(uvd, 0, 0x00d0, 0x0111);
2416 ibmcam_veio(uvd, 0, 0x00b9, 0x010a);
2417 ibmcam_veio(uvd, 0, 0x0001, 0x0102);
2418 ibmcam_veio(uvd, 0, 0x002c, 0x0103);
2419 ibmcam_veio(uvd, 0, 0x0000, 0x0104);
2420 ibmcam_veio(uvd, 0, 0x0024, 0x0105);
2421 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2422 ibmcam_veio(uvd, 0, 0x0016, 0x012f);
2423 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2424 ibmcam_veio(uvd, 0, 0x0007, 0x0127);
2425 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2426 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2427 ibmcam_veio(uvd, 0, 0x0014, 0x012d);
2428 ibmcam_veio(uvd, 0, 0x0001, 0x012f);
2429 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2430 ibmcam_veio(uvd, 0, 0x00aa, 0x012e);
2431 ibmcam_veio(uvd, 0, 0x001a, 0x0130);
2432 ibmcam_veio(uvd, 0, 0x8a0a, 0x0124);
2433 ibmcam_veio(uvd, 0, 0x005e, 0x012d);
2434 ibmcam_veio(uvd, 0, 0x9545, 0x0124);
2435 ibmcam_veio(uvd, 0, 0x00aa, 0x0127);
2436 ibmcam_veio(uvd, 0, 0x0018, 0x012e);
2437 ibmcam_veio(uvd, 0, 0x0049, 0x0130);
2438 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2439 ibmcam_veio(uvd, 0, 0x00aa, 0x012f);
2440 ibmcam_veio(uvd, 0, 0xd055, 0x0124);
2441 ibmcam_veio(uvd, 0, 0x001c, 0x0127);
2442 ibmcam_veio(uvd, 0, 0x00c7, 0x012e);
2443 ibmcam_veio(uvd, 0, 0xaa28, 0x0124);
2444 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2445 ibmcam_veio(uvd, 0, 0x0032, 0x012f);
2446 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2447 ibmcam_veio(uvd, 0, 0x0028, 0x0127);
2448 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2449 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2450 ibmcam_veio(uvd, 0, 0x0036, 0x012d);
2451 ibmcam_veio(uvd, 0, 0x0008, 0x012f);
2452 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2453 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
2454 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2455 ibmcam_veio(uvd, 0, 0x001e, 0x012f);
2456 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2457 ibmcam_veio(uvd, 0, 0x0010, 0x0127);
2458 ibmcam_veio(uvd, 0, 0x0013, 0x012e);
2459 ibmcam_veio(uvd, 0, 0x002a, 0x0130);
2460 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2461 ibmcam_veio(uvd, 0, 0x0010, 0x012d);
2462 ibmcam_veio(uvd, 0, 0x006d, 0x012f);
2463 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2464 ibmcam_veio(uvd, 0, 0x0001, 0x0127);
2465 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2466 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2467 ibmcam_veio(uvd, 0, 0x0038, 0x012f);
2468 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2469 ibmcam_veio(uvd, 0, 0x0004, 0x0127);
2470 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2471 ibmcam_veio(uvd, 0, 0x00c0, 0x010c);
2472 break;
2473 case VIDEOSIZE_320x240:
2474 ibmcam_veio(uvd, 0, 0x0000, 0x0100);
2475 ibmcam_veio(uvd, 0, 0x00c0, 0x0111);
2476 ibmcam_veio(uvd, 0, 0x00bc, 0x012c);
2477 ibmcam_veio(uvd, 0, 0x0080, 0x012b);
2478 ibmcam_veio(uvd, 0, 0x0000, 0x0108);
2479 ibmcam_veio(uvd, 0, 0x0001, 0x0133);
2480 ibmcam_veio(uvd, 0, 0x009b, 0x010f);
2481 ibmcam_veio(uvd, 0, 0x00bb, 0x010f);
2482 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2483 ibmcam_veio(uvd, 0, 0x0038, 0x012f);
2484 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2485 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2486 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2487 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2488 ibmcam_veio(uvd, 0, 0x000a, 0x012f);
2489 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2490 ibmcam_veio(uvd, 0, 0x005c, 0x0127);
2491 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2492 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2493 ibmcam_veio(uvd, 0, 0x0004, 0x012f);
2494 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2495 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2496 ibmcam_veio(uvd, 0, 0x00fb, 0x012e);
2497 ibmcam_veio(uvd, 0, 0x0000, 0x0130);
2498 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2499 ibmcam_veio(uvd, 0, 0x00aa, 0x012f);
2500 ibmcam_veio(uvd, 0, 0xd055, 0x0124);
2501 ibmcam_veio(uvd, 0, 0x000c, 0x0127);
2502 ibmcam_veio(uvd, 0, 0x0009, 0x012e);
2503 ibmcam_veio(uvd, 0, 0xaa28, 0x0124);
2504 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2505 ibmcam_veio(uvd, 0, 0x0012, 0x012f);
2506 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2507 ibmcam_veio(uvd, 0, 0x0008, 0x0127);
2508 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2509 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2510 ibmcam_veio(uvd, 0, 0x002a, 0x012d);
2511 ibmcam_veio(uvd, 0, 0x0000, 0x012f);
2512 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2513 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
2514 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2515 ibmcam_veio(uvd, 0, 0x0034, 0x012f);
2516 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2517 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2518 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2519 ibmcam_veio(uvd, 0, 0x0070, 0x0119);
2520 ibmcam_veio(uvd, 0, 0x00d2, 0x0107);
2521 ibmcam_veio(uvd, 0, 0x0003, 0x0106);
2522 ibmcam_veio(uvd, 0, 0x005e, 0x0107);
2523 ibmcam_veio(uvd, 0, 0x0003, 0x0106);
2524 ibmcam_veio(uvd, 0, 0x00d0, 0x0111);
2525 ibmcam_veio(uvd, 0, 0x0039, 0x010a);
2526 ibmcam_veio(uvd, 0, 0x0001, 0x0102);
2527 ibmcam_veio(uvd, 0, 0x0028, 0x0103);
2528 ibmcam_veio(uvd, 0, 0x0000, 0x0104);
2529 ibmcam_veio(uvd, 0, 0x001e, 0x0105);
2530 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2531 ibmcam_veio(uvd, 0, 0x0016, 0x012f);
2532 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2533 ibmcam_veio(uvd, 0, 0x000a, 0x0127);
2534 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2535 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2536 ibmcam_veio(uvd, 0, 0x0014, 0x012d);
2537 ibmcam_veio(uvd, 0, 0x0008, 0x012f);
2538 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2539 ibmcam_veio(uvd, 0, 0x00aa, 0x012e);
2540 ibmcam_veio(uvd, 0, 0x001a, 0x0130);
2541 ibmcam_veio(uvd, 0, 0x8a0a, 0x0124);
2542 ibmcam_veio(uvd, 0, 0x005a, 0x012d);
2543 ibmcam_veio(uvd, 0, 0x9545, 0x0124);
2544 ibmcam_veio(uvd, 0, 0x00aa, 0x0127);
2545 ibmcam_veio(uvd, 0, 0x0018, 0x012e);
2546 ibmcam_veio(uvd, 0, 0x0043, 0x0130);
2547 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2548 ibmcam_veio(uvd, 0, 0x00aa, 0x012f);
2549 ibmcam_veio(uvd, 0, 0xd055, 0x0124);
2550 ibmcam_veio(uvd, 0, 0x001c, 0x0127);
2551 ibmcam_veio(uvd, 0, 0x00eb, 0x012e);
2552 ibmcam_veio(uvd, 0, 0xaa28, 0x0124);
2553 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2554 ibmcam_veio(uvd, 0, 0x0032, 0x012f);
2555 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2556 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2557 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2558 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2559 ibmcam_veio(uvd, 0, 0x0036, 0x012d);
2560 ibmcam_veio(uvd, 0, 0x0008, 0x012f);
2561 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2562 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
2563 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2564 ibmcam_veio(uvd, 0, 0x001e, 0x012f);
2565 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2566 ibmcam_veio(uvd, 0, 0x0017, 0x0127);
2567 ibmcam_veio(uvd, 0, 0x0013, 0x012e);
2568 ibmcam_veio(uvd, 0, 0x0031, 0x0130);
2569 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2570 ibmcam_veio(uvd, 0, 0x0017, 0x012d);
2571 ibmcam_veio(uvd, 0, 0x0078, 0x012f);
2572 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2573 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2574 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2575 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2576 ibmcam_veio(uvd, 0, 0x0038, 0x012f);
2577 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2578 ibmcam_veio(uvd, 0, 0x0004, 0x0127);
2579 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2580 ibmcam_veio(uvd, 0, 0x00c0, 0x010c);
2581 break;
2582 case VIDEOSIZE_352x288:
2583 ibmcam_veio(uvd, 0, 0x0000, 0x0100);
2584 ibmcam_veio(uvd, 0, 0x00c0, 0x0111);
2585 ibmcam_veio(uvd, 0, 0x00bc, 0x012c);
2586 ibmcam_veio(uvd, 0, 0x0080, 0x012b);
2587 ibmcam_veio(uvd, 0, 0x0000, 0x0108);
2588 ibmcam_veio(uvd, 0, 0x0001, 0x0133);
2589 ibmcam_veio(uvd, 0, 0x009b, 0x010f);
2590 ibmcam_veio(uvd, 0, 0x00bb, 0x010f);
2591 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2592 ibmcam_veio(uvd, 0, 0x0038, 0x012f);
2593 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2594 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2595 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2596 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2597 ibmcam_veio(uvd, 0, 0x000a, 0x012f);
2598 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2599 ibmcam_veio(uvd, 0, 0x005c, 0x0127);
2600 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2601 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2602 ibmcam_veio(uvd, 0, 0x0004, 0x012f);
2603 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2604 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2605 ibmcam_veio(uvd, 0, 0x00fb, 0x012e);
2606 ibmcam_veio(uvd, 0, 0x0000, 0x0130);
2607 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2608 ibmcam_veio(uvd, 0, 0x00aa, 0x012f);
2609 ibmcam_veio(uvd, 0, 0xd055, 0x0124);
2610 ibmcam_veio(uvd, 0, 0x000c, 0x0127);
2611 ibmcam_veio(uvd, 0, 0x0009, 0x012e);
2612 ibmcam_veio(uvd, 0, 0xaa28, 0x0124);
2613 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2614 ibmcam_veio(uvd, 0, 0x0012, 0x012f);
2615 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2616 ibmcam_veio(uvd, 0, 0x0008, 0x0127);
2617 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2618 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2619 ibmcam_veio(uvd, 0, 0x002a, 0x012d);
2620 ibmcam_veio(uvd, 0, 0x0000, 0x012f);
2621 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2622 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
2623 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2624 ibmcam_veio(uvd, 0, 0x0034, 0x012f);
2625 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2626 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2627 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2628 ibmcam_veio(uvd, 0, 0x0070, 0x0119);
2629 ibmcam_veio(uvd, 0, 0x00f2, 0x0107);
2630 ibmcam_veio(uvd, 0, 0x0003, 0x0106);
2631 ibmcam_veio(uvd, 0, 0x008c, 0x0107);
2632 ibmcam_veio(uvd, 0, 0x0003, 0x0106);
2633 ibmcam_veio(uvd, 0, 0x00c0, 0x0111);
2634 ibmcam_veio(uvd, 0, 0x0039, 0x010a);
2635 ibmcam_veio(uvd, 0, 0x0001, 0x0102);
2636 ibmcam_veio(uvd, 0, 0x002c, 0x0103);
2637 ibmcam_veio(uvd, 0, 0x0000, 0x0104);
2638 ibmcam_veio(uvd, 0, 0x0024, 0x0105);
2639 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2640 ibmcam_veio(uvd, 0, 0x0016, 0x012f);
2641 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2642 ibmcam_veio(uvd, 0, 0x0006, 0x0127);
2643 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2644 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2645 ibmcam_veio(uvd, 0, 0x0014, 0x012d);
2646 ibmcam_veio(uvd, 0, 0x0002, 0x012f);
2647 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2648 ibmcam_veio(uvd, 0, 0x00aa, 0x012e);
2649 ibmcam_veio(uvd, 0, 0x001a, 0x0130);
2650 ibmcam_veio(uvd, 0, 0x8a0a, 0x0124);
2651 ibmcam_veio(uvd, 0, 0x005e, 0x012d);
2652 ibmcam_veio(uvd, 0, 0x9545, 0x0124);
2653 ibmcam_veio(uvd, 0, 0x00aa, 0x0127);
2654 ibmcam_veio(uvd, 0, 0x0018, 0x012e);
2655 ibmcam_veio(uvd, 0, 0x0049, 0x0130);
2656 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2657 ibmcam_veio(uvd, 0, 0x00aa, 0x012f);
2658 ibmcam_veio(uvd, 0, 0xd055, 0x0124);
2659 ibmcam_veio(uvd, 0, 0x001c, 0x0127);
2660 ibmcam_veio(uvd, 0, 0x00cf, 0x012e);
2661 ibmcam_veio(uvd, 0, 0xaa28, 0x0124);
2662 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2663 ibmcam_veio(uvd, 0, 0x0032, 0x012f);
2664 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2665 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2666 ibmcam_veio(uvd, 0, 0x00aa, 0x0130);
2667 ibmcam_veio(uvd, 0, 0x82a8, 0x0124);
2668 ibmcam_veio(uvd, 0, 0x0036, 0x012d);
2669 ibmcam_veio(uvd, 0, 0x0008, 0x012f);
2670 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2671 ibmcam_veio(uvd, 0, 0xfffa, 0x0124);
2672 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2673 ibmcam_veio(uvd, 0, 0x001e, 0x012f);
2674 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2675 ibmcam_veio(uvd, 0, 0x0010, 0x0127);
2676 ibmcam_veio(uvd, 0, 0x0013, 0x012e);
2677 ibmcam_veio(uvd, 0, 0x0025, 0x0130);
2678 ibmcam_veio(uvd, 0, 0x8a28, 0x0124);
2679 ibmcam_veio(uvd, 0, 0x0010, 0x012d);
2680 ibmcam_veio(uvd, 0, 0x0048, 0x012f);
2681 ibmcam_veio(uvd, 0, 0xd145, 0x0124);
2682 ibmcam_veio(uvd, 0, 0x0000, 0x0127);
2683 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2684 ibmcam_veio(uvd, 0, 0x00aa, 0x012d);
2685 ibmcam_veio(uvd, 0, 0x0038, 0x012f);
2686 ibmcam_veio(uvd, 0, 0xd141, 0x0124);
2687 ibmcam_veio(uvd, 0, 0x0004, 0x0127);
2688 ibmcam_veio(uvd, 0, 0xfea8, 0x0124);
2689 ibmcam_veio(uvd, 0, 0x00c0, 0x010c);
2690 break;
2691 }
2692 usb_clear_halt(uvd->dev, usb_rcvisocpipe(uvd->dev, uvd->video_endp));
2693}
2694
2695static void ibmcam_model3_setup_after_video_if(struct uvd *uvd)
2696{
2697 int i;
2698 /*
2699 * 01.01.08 - Added for RCA video in support -LO
2700 * This struct is used to init the Model3 cam to use the RCA video in port
2701 * instead of the CCD sensor.
2702 */
2703 static const struct struct_initData initData[] = {
2704 {0, 0x0000, 0x010c},
2705 {0, 0x0006, 0x012c},
2706 {0, 0x0078, 0x012d},
2707 {0, 0x0046, 0x012f},
2708 {0, 0xd141, 0x0124},
2709 {0, 0x0000, 0x0127},
2710 {0, 0xfea8, 0x0124},
2711 {1, 0x0000, 0x0116},
2712 {0, 0x0064, 0x0116},
2713 {1, 0x0000, 0x0115},
2714 {0, 0x0003, 0x0115},
2715 {0, 0x0008, 0x0123},
2716 {0, 0x0000, 0x0117},
2717 {0, 0x0000, 0x0112},
2718 {0, 0x0080, 0x0100},
2719 {0, 0x0000, 0x0100},
2720 {1, 0x0000, 0x0116},
2721 {0, 0x0060, 0x0116},
2722 {0, 0x0002, 0x0112},
2723 {0, 0x0000, 0x0123},
2724 {0, 0x0001, 0x0117},
2725 {0, 0x0040, 0x0108},
2726 {0, 0x0019, 0x012c},
2727 {0, 0x0040, 0x0116},
2728 {0, 0x000a, 0x0115},
2729 {0, 0x000b, 0x0115},
2730 {0, 0x0078, 0x012d},
2731 {0, 0x0046, 0x012f},
2732 {0, 0xd141, 0x0124},
2733 {0, 0x0000, 0x0127},
2734 {0, 0xfea8, 0x0124},
2735 {0, 0x0064, 0x0116},
2736 {0, 0x0000, 0x0115},
2737 {0, 0x0001, 0x0115},
2738 {0, 0xffff, 0x0124},
2739 {0, 0xfff9, 0x0124},
2740 {0, 0x0086, 0x0127},
2741 {0, 0xfff8, 0x0124},
2742 {0, 0xfffd, 0x0124},
2743 {0, 0x00aa, 0x0127},
2744 {0, 0xfff8, 0x0124},
2745 {0, 0xfffd, 0x0124},
2746 {0, 0x0000, 0x0127},
2747 {0, 0xfff8, 0x0124},
2748 {0, 0xfffd, 0x0124},
2749 {0, 0xfffa, 0x0124},
2750 {0, 0xffff, 0x0124},
2751 {0, 0xfff9, 0x0124},
2752 {0, 0x0086, 0x0127},
2753 {0, 0xfff8, 0x0124},
2754 {0, 0xfffd, 0x0124},
2755 {0, 0x00f2, 0x0127},
2756 {0, 0xfff8, 0x0124},
2757 {0, 0xfffd, 0x0124},
2758 {0, 0x000f, 0x0127},
2759 {0, 0xfff8, 0x0124},
2760 {0, 0xfffd, 0x0124},
2761 {0, 0xfffa, 0x0124},
2762 {0, 0xffff, 0x0124},
2763 {0, 0xfff9, 0x0124},
2764 {0, 0x0086, 0x0127},
2765 {0, 0xfff8, 0x0124},
2766 {0, 0xfffd, 0x0124},
2767 {0, 0x00f8, 0x0127},
2768 {0, 0xfff8, 0x0124},
2769 {0, 0xfffd, 0x0124},
2770 {0, 0x00fc, 0x0127},
2771 {0, 0xfff8, 0x0124},
2772 {0, 0xfffd, 0x0124},
2773 {0, 0xfffa, 0x0124},
2774 {0, 0xffff, 0x0124},
2775 {0, 0xfff9, 0x0124},
2776 {0, 0x0086, 0x0127},
2777 {0, 0xfff8, 0x0124},
2778 {0, 0xfffd, 0x0124},
2779 {0, 0x00f9, 0x0127},
2780 {0, 0xfff8, 0x0124},
2781 {0, 0xfffd, 0x0124},
2782 {0, 0x003c, 0x0127},
2783 {0, 0xfff8, 0x0124},
2784 {0, 0xfffd, 0x0124},
2785 {0, 0xfffa, 0x0124},
2786 {0, 0xffff, 0x0124},
2787 {0, 0xfff9, 0x0124},
2788 {0, 0x0086, 0x0127},
2789 {0, 0xfff8, 0x0124},
2790 {0, 0xfffd, 0x0124},
2791 {0, 0x0027, 0x0127},
2792 {0, 0xfff8, 0x0124},
2793 {0, 0xfffd, 0x0124},
2794 {0, 0x0019, 0x0127},
2795 {0, 0xfff8, 0x0124},
2796 {0, 0xfffd, 0x0124},
2797 {0, 0xfffa, 0x0124},
2798 {0, 0xfff9, 0x0124},
2799 {0, 0x0086, 0x0127},
2800 {0, 0xfff8, 0x0124},
2801 {0, 0xfffd, 0x0124},
2802 {0, 0x0037, 0x0127},
2803 {0, 0xfff8, 0x0124},
2804 {0, 0xfffd, 0x0124},
2805 {0, 0x0000, 0x0127},
2806 {0, 0xfff8, 0x0124},
2807 {0, 0xfffd, 0x0124},
2808 {0, 0x0021, 0x0127},
2809 {0, 0xfff8, 0x0124},
2810 {0, 0xfffd, 0x0124},
2811 {0, 0xfffa, 0x0124},
2812 {0, 0xfff9, 0x0124},
2813 {0, 0x0086, 0x0127},
2814 {0, 0xfff8, 0x0124},
2815 {0, 0xfffd, 0x0124},
2816 {0, 0x0038, 0x0127},
2817 {0, 0xfff8, 0x0124},
2818 {0, 0xfffd, 0x0124},
2819 {0, 0x0006, 0x0127},
2820 {0, 0xfff8, 0x0124},
2821 {0, 0xfffd, 0x0124},
2822 {0, 0x0045, 0x0127},
2823 {0, 0xfff8, 0x0124},
2824 {0, 0xfffd, 0x0124},
2825 {0, 0xfffa, 0x0124},
2826 {0, 0xfff9, 0x0124},
2827 {0, 0x0086, 0x0127},
2828 {0, 0xfff8, 0x0124},
2829 {0, 0xfffd, 0x0124},
2830 {0, 0x0037, 0x0127},
2831 {0, 0xfff8, 0x0124},
2832 {0, 0xfffd, 0x0124},
2833 {0, 0x0001, 0x0127},
2834 {0, 0xfff8, 0x0124},
2835 {0, 0xfffd, 0x0124},
2836 {0, 0x002a, 0x0127},
2837 {0, 0xfff8, 0x0124},
2838 {0, 0xfffd, 0x0124},
2839 {0, 0xfffa, 0x0124},
2840 {0, 0xfff9, 0x0124},
2841 {0, 0x0086, 0x0127},
2842 {0, 0xfff8, 0x0124},
2843 {0, 0xfffd, 0x0124},
2844 {0, 0x0038, 0x0127},
2845 {0, 0xfff8, 0x0124},
2846 {0, 0xfffd, 0x0124},
2847 {0, 0x0000, 0x0127},
2848 {0, 0xfff8, 0x0124},
2849 {0, 0xfffd, 0x0124},
2850 {0, 0x000e, 0x0127},
2851 {0, 0xfff8, 0x0124},
2852 {0, 0xfffd, 0x0124},
2853 {0, 0xfffa, 0x0124},
2854 {0, 0xfff9, 0x0124},
2855 {0, 0x0086, 0x0127},
2856 {0, 0xfff8, 0x0124},
2857 {0, 0xfffd, 0x0124},
2858 {0, 0x0037, 0x0127},
2859 {0, 0xfff8, 0x0124},
2860 {0, 0xfffd, 0x0124},
2861 {0, 0x0001, 0x0127},
2862 {0, 0xfff8, 0x0124},
2863 {0, 0xfffd, 0x0124},
2864 {0, 0x002b, 0x0127},
2865 {0, 0xfff8, 0x0124},
2866 {0, 0xfffd, 0x0124},
2867 {0, 0xfffa, 0x0124},
2868 {0, 0xfff9, 0x0124},
2869 {0, 0x0086, 0x0127},
2870 {0, 0xfff8, 0x0124},
2871 {0, 0xfffd, 0x0124},
2872 {0, 0x0038, 0x0127},
2873 {0, 0xfff8, 0x0124},
2874 {0, 0xfffd, 0x0124},
2875 {0, 0x0001, 0x0127},
2876 {0, 0xfff8, 0x0124},
2877 {0, 0xfffd, 0x0124},
2878 {0, 0x00f4, 0x0127},
2879 {0, 0xfff8, 0x0124},
2880 {0, 0xfffd, 0x0124},
2881 {0, 0xfffa, 0x0124},
2882 {0, 0xfff9, 0x0124},
2883 {0, 0x0086, 0x0127},
2884 {0, 0xfff8, 0x0124},
2885 {0, 0xfffd, 0x0124},
2886 {0, 0x0037, 0x0127},
2887 {0, 0xfff8, 0x0124},
2888 {0, 0xfffd, 0x0124},
2889 {0, 0x0001, 0x0127},
2890 {0, 0xfff8, 0x0124},
2891 {0, 0xfffd, 0x0124},
2892 {0, 0x002c, 0x0127},
2893 {0, 0xfff8, 0x0124},
2894 {0, 0xfffd, 0x0124},
2895 {0, 0xfffa, 0x0124},
2896 {0, 0xfff9, 0x0124},
2897 {0, 0x0086, 0x0127},
2898 {0, 0xfff8, 0x0124},
2899 {0, 0xfffd, 0x0124},
2900 {0, 0x0038, 0x0127},
2901 {0, 0xfff8, 0x0124},
2902 {0, 0xfffd, 0x0124},
2903 {0, 0x0001, 0x0127},
2904 {0, 0xfff8, 0x0124},
2905 {0, 0xfffd, 0x0124},
2906 {0, 0x0004, 0x0127},
2907 {0, 0xfff8, 0x0124},
2908 {0, 0xfffd, 0x0124},
2909 {0, 0xfffa, 0x0124},
2910 {0, 0xfff9, 0x0124},
2911 {0, 0x0086, 0x0127},
2912 {0, 0xfff8, 0x0124},
2913 {0, 0xfffd, 0x0124},
2914 {0, 0x0037, 0x0127},
2915 {0, 0xfff8, 0x0124},
2916 {0, 0xfffd, 0x0124},
2917 {0, 0x0001, 0x0127},
2918 {0, 0xfff8, 0x0124},
2919 {0, 0xfffd, 0x0124},
2920 {0, 0x002d, 0x0127},
2921 {0, 0xfff8, 0x0124},
2922 {0, 0xfffd, 0x0124},
2923 {0, 0xfffa, 0x0124},
2924 {0, 0xfff9, 0x0124},
2925 {0, 0x0086, 0x0127},
2926 {0, 0xfff8, 0x0124},
2927 {0, 0xfffd, 0x0124},
2928 {0, 0x0038, 0x0127},
2929 {0, 0xfff8, 0x0124},
2930 {0, 0xfffd, 0x0124},
2931 {0, 0x0000, 0x0127},
2932 {0, 0xfff8, 0x0124},
2933 {0, 0xfffd, 0x0124},
2934 {0, 0x0014, 0x0127},
2935 {0, 0xfff8, 0x0124},
2936 {0, 0xfffd, 0x0124},
2937 {0, 0xfffa, 0x0124},
2938 {0, 0xfff9, 0x0124},
2939 {0, 0x0086, 0x0127},
2940 {0, 0xfff8, 0x0124},
2941 {0, 0xfffd, 0x0124},
2942 {0, 0x0037, 0x0127},
2943 {0, 0xfff8, 0x0124},
2944 {0, 0xfffd, 0x0124},
2945 {0, 0x0001, 0x0127},
2946 {0, 0xfff8, 0x0124},
2947 {0, 0xfffd, 0x0124},
2948 {0, 0x002e, 0x0127},
2949 {0, 0xfff8, 0x0124},
2950 {0, 0xfffd, 0x0124},
2951 {0, 0xfffa, 0x0124},
2952 {0, 0xfff9, 0x0124},
2953 {0, 0x0086, 0x0127},
2954 {0, 0xfff8, 0x0124},
2955 {0, 0xfffd, 0x0124},
2956 {0, 0x0038, 0x0127},
2957 {0, 0xfff8, 0x0124},
2958 {0, 0xfffd, 0x0124},
2959 {0, 0x0003, 0x0127},
2960 {0, 0xfff8, 0x0124},
2961 {0, 0xfffd, 0x0124},
2962 {0, 0x0000, 0x0127},
2963 {0, 0xfff8, 0x0124},
2964 {0, 0xfffd, 0x0124},
2965 {0, 0xfffa, 0x0124},
2966 {0, 0xfff9, 0x0124},
2967 {0, 0x0086, 0x0127},
2968 {0, 0xfff8, 0x0124},
2969 {0, 0xfffd, 0x0124},
2970 {0, 0x0037, 0x0127},
2971 {0, 0xfff8, 0x0124},
2972 {0, 0xfffd, 0x0124},
2973 {0, 0x0001, 0x0127},
2974 {0, 0xfff8, 0x0124},
2975 {0, 0xfffd, 0x0124},
2976 {0, 0x002f, 0x0127},
2977 {0, 0xfff8, 0x0124},
2978 {0, 0xfffd, 0x0124},
2979 {0, 0xfffa, 0x0124},
2980 {0, 0xfff9, 0x0124},
2981 {0, 0x0086, 0x0127},
2982 {0, 0xfff8, 0x0124},
2983 {0, 0xfffd, 0x0124},
2984 {0, 0x0038, 0x0127},
2985 {0, 0xfff8, 0x0124},
2986 {0, 0xfffd, 0x0124},
2987 {0, 0x0003, 0x0127},
2988 {0, 0xfff8, 0x0124},
2989 {0, 0xfffd, 0x0124},
2990 {0, 0x0014, 0x0127},
2991 {0, 0xfff8, 0x0124},
2992 {0, 0xfffd, 0x0124},
2993 {0, 0xfffa, 0x0124},
2994 {0, 0xfff9, 0x0124},
2995 {0, 0x0086, 0x0127},
2996 {0, 0xfff8, 0x0124},
2997 {0, 0xfffd, 0x0124},
2998 {0, 0x0037, 0x0127},
2999 {0, 0xfff8, 0x0124},
3000 {0, 0xfffd, 0x0124},
3001 {0, 0x0001, 0x0127},
3002 {0, 0xfff8, 0x0124},
3003 {0, 0xfffd, 0x0124},
3004 {0, 0x0040, 0x0127},
3005 {0, 0xfff8, 0x0124},
3006 {0, 0xfffd, 0x0124},
3007 {0, 0xfffa, 0x0124},
3008 {0, 0xfff9, 0x0124},
3009 {0, 0x0086, 0x0127},
3010 {0, 0xfff8, 0x0124},
3011 {0, 0xfffd, 0x0124},
3012 {0, 0x0038, 0x0127},
3013 {0, 0xfff8, 0x0124},
3014 {0, 0xfffd, 0x0124},
3015 {0, 0x0000, 0x0127},
3016 {0, 0xfff8, 0x0124},
3017 {0, 0xfffd, 0x0124},
3018 {0, 0x0040, 0x0127},
3019 {0, 0xfff8, 0x0124},
3020 {0, 0xfffd, 0x0124},
3021 {0, 0xfffa, 0x0124},
3022 {0, 0xfff9, 0x0124},
3023 {0, 0x0086, 0x0127},
3024 {0, 0xfff8, 0x0124},
3025 {0, 0xfffd, 0x0124},
3026 {0, 0x0037, 0x0127},
3027 {0, 0xfff8, 0x0124},
3028 {0, 0xfffd, 0x0124},
3029 {0, 0x0001, 0x0127},
3030 {0, 0xfff8, 0x0124},
3031 {0, 0xfffd, 0x0124},
3032 {0, 0x0053, 0x0127},
3033 {0, 0xfff8, 0x0124},
3034 {0, 0xfffd, 0x0124},
3035 {0, 0xfffa, 0x0124},
3036 {0, 0xfff9, 0x0124},
3037 {0, 0x0086, 0x0127},
3038 {0, 0xfff8, 0x0124},
3039 {0, 0xfffd, 0x0124},
3040 {0, 0x0038, 0x0127},
3041 {0, 0xfff8, 0x0124},
3042 {0, 0xfffd, 0x0124},
3043 {0, 0x0000, 0x0127},
3044 {0, 0xfff8, 0x0124},
3045 {0, 0xfffd, 0x0124},
3046 {0, 0x0038, 0x0127},
3047 {0, 0xfff8, 0x0124},
3048 {0, 0xfffd, 0x0124},
3049 {0, 0xfffa, 0x0124},
3050 {0, 0x0000, 0x0101},
3051 {0, 0x00a0, 0x0103},
3052 {0, 0x0078, 0x0105},
3053 {0, 0x0000, 0x010a},
3054 {0, 0x0024, 0x010b},
3055 {0, 0x0028, 0x0119},
3056 {0, 0x0088, 0x011b},
3057 {0, 0x0002, 0x011d},
3058 {0, 0x0003, 0x011e},
3059 {0, 0x0000, 0x0129},
3060 {0, 0x00fc, 0x012b},
3061 {0, 0x0008, 0x0102},
3062 {0, 0x0000, 0x0104},
3063 {0, 0x0008, 0x011a},
3064 {0, 0x0028, 0x011c},
3065 {0, 0x0021, 0x012a},
3066 {0, 0x0000, 0x0118},
3067 {0, 0x0000, 0x0132},
3068 {0, 0x0000, 0x0109},
3069 {0, 0xfff9, 0x0124},
3070 {0, 0x0086, 0x0127},
3071 {0, 0xfff8, 0x0124},
3072 {0, 0xfffd, 0x0124},
3073 {0, 0x0037, 0x0127},
3074 {0, 0xfff8, 0x0124},
3075 {0, 0xfffd, 0x0124},
3076 {0, 0x0001, 0x0127},
3077 {0, 0xfff8, 0x0124},
3078 {0, 0xfffd, 0x0124},
3079 {0, 0x0031, 0x0127},
3080 {0, 0xfff8, 0x0124},
3081 {0, 0xfffd, 0x0124},
3082 {0, 0xfffa, 0x0124},
3083 {0, 0xfff9, 0x0124},
3084 {0, 0x0086, 0x0127},
3085 {0, 0xfff8, 0x0124},
3086 {0, 0xfffd, 0x0124},
3087 {0, 0x0038, 0x0127},
3088 {0, 0xfff8, 0x0124},
3089 {0, 0xfffd, 0x0124},
3090 {0, 0x0000, 0x0127},
3091 {0, 0xfff8, 0x0124},
3092 {0, 0xfffd, 0x0124},
3093 {0, 0x0000, 0x0127},
3094 {0, 0xfff8, 0x0124},
3095 {0, 0xfffd, 0x0124},
3096 {0, 0xfffa, 0x0124},
3097 {0, 0xfff9, 0x0124},
3098 {0, 0x0086, 0x0127},
3099 {0, 0xfff8, 0x0124},
3100 {0, 0xfffd, 0x0124},
3101 {0, 0x0037, 0x0127},
3102 {0, 0xfff8, 0x0124},
3103 {0, 0xfffd, 0x0124},
3104 {0, 0x0001, 0x0127},
3105 {0, 0xfff8, 0x0124},
3106 {0, 0xfffd, 0x0124},
3107 {0, 0x0040, 0x0127},
3108 {0, 0xfff8, 0x0124},
3109 {0, 0xfffd, 0x0124},
3110 {0, 0xfffa, 0x0124},
3111 {0, 0xfff9, 0x0124},
3112 {0, 0x0086, 0x0127},
3113 {0, 0xfff8, 0x0124},
3114 {0, 0xfffd, 0x0124},
3115 {0, 0x0038, 0x0127},
3116 {0, 0xfff8, 0x0124},
3117 {0, 0xfffd, 0x0124},
3118 {0, 0x0000, 0x0127},
3119 {0, 0xfff8, 0x0124},
3120 {0, 0xfffd, 0x0124},
3121 {0, 0x0040, 0x0127},
3122 {0, 0xfff8, 0x0124},
3123 {0, 0xfffd, 0x0124},
3124 {0, 0xfffa, 0x0124},
3125 {0, 0xfff9, 0x0124},
3126 {0, 0x0086, 0x0127},
3127 {0, 0xfff8, 0x0124},
3128 {0, 0xfffd, 0x0124},
3129 {0, 0x0037, 0x0127},
3130 {0, 0xfff8, 0x0124},
3131 {0, 0xfffd, 0x0124},
3132 {0, 0x0000, 0x0127},
3133 {0, 0xfff8, 0x0124},
3134 {0, 0xfffd, 0x0124},
3135 {0, 0x00dc, 0x0127},
3136 {0, 0xfff8, 0x0124},
3137 {0, 0xfffd, 0x0124},
3138 {0, 0xfffa, 0x0124},
3139 {0, 0xfff9, 0x0124},
3140 {0, 0x0086, 0x0127},
3141 {0, 0xfff8, 0x0124},
3142 {0, 0xfffd, 0x0124},
3143 {0, 0x0038, 0x0127},
3144 {0, 0xfff8, 0x0124},
3145 {0, 0xfffd, 0x0124},
3146 {0, 0x0000, 0x0127},
3147 {0, 0xfff8, 0x0124},
3148 {0, 0xfffd, 0x0124},
3149 {0, 0x0000, 0x0127},
3150 {0, 0xfff8, 0x0124},
3151 {0, 0xfffd, 0x0124},
3152 {0, 0xfffa, 0x0124},
3153 {0, 0xfff9, 0x0124},
3154 {0, 0x0086, 0x0127},
3155 {0, 0xfff8, 0x0124},
3156 {0, 0xfffd, 0x0124},
3157 {0, 0x0037, 0x0127},
3158 {0, 0xfff8, 0x0124},
3159 {0, 0xfffd, 0x0124},
3160 {0, 0x0001, 0x0127},
3161 {0, 0xfff8, 0x0124},
3162 {0, 0xfffd, 0x0124},
3163 {0, 0x0032, 0x0127},
3164 {0, 0xfff8, 0x0124},
3165 {0, 0xfffd, 0x0124},
3166 {0, 0xfffa, 0x0124},
3167 {0, 0xfff9, 0x0124},
3168 {0, 0x0086, 0x0127},
3169 {0, 0xfff8, 0x0124},
3170 {0, 0xfffd, 0x0124},
3171 {0, 0x0038, 0x0127},
3172 {0, 0xfff8, 0x0124},
3173 {0, 0xfffd, 0x0124},
3174 {0, 0x0001, 0x0127},
3175 {0, 0xfff8, 0x0124},
3176 {0, 0xfffd, 0x0124},
3177 {0, 0x0020, 0x0127},
3178 {0, 0xfff8, 0x0124},
3179 {0, 0xfffd, 0x0124},
3180 {0, 0xfffa, 0x0124},
3181 {0, 0xfff9, 0x0124},
3182 {0, 0x0086, 0x0127},
3183 {0, 0xfff8, 0x0124},
3184 {0, 0xfffd, 0x0124},
3185 {0, 0x0037, 0x0127},
3186 {0, 0xfff8, 0x0124},
3187 {0, 0xfffd, 0x0124},
3188 {0, 0x0001, 0x0127},
3189 {0, 0xfff8, 0x0124},
3190 {0, 0xfffd, 0x0124},
3191 {0, 0x0040, 0x0127},
3192 {0, 0xfff8, 0x0124},
3193 {0, 0xfffd, 0x0124},
3194 {0, 0xfffa, 0x0124},
3195 {0, 0xfff9, 0x0124},
3196 {0, 0x0086, 0x0127},
3197 {0, 0xfff8, 0x0124},
3198 {0, 0xfffd, 0x0124},
3199 {0, 0x0038, 0x0127},
3200 {0, 0xfff8, 0x0124},
3201 {0, 0xfffd, 0x0124},
3202 {0, 0x0000, 0x0127},
3203 {0, 0xfff8, 0x0124},
3204 {0, 0xfffd, 0x0124},
3205 {0, 0x0040, 0x0127},
3206 {0, 0xfff8, 0x0124},
3207 {0, 0xfffd, 0x0124},
3208 {0, 0xfffa, 0x0124},
3209 {0, 0xfff9, 0x0124},
3210 {0, 0x0086, 0x0127},
3211 {0, 0xfff8, 0x0124},
3212 {0, 0xfffd, 0x0124},
3213 {0, 0x0037, 0x0127},
3214 {0, 0xfff8, 0x0124},
3215 {0, 0xfffd, 0x0124},
3216 {0, 0x0000, 0x0127},
3217 {0, 0xfff8, 0x0124},
3218 {0, 0xfffd, 0x0124},
3219 {0, 0x0030, 0x0127},
3220 {0, 0xfff8, 0x0124},
3221 {0, 0xfffd, 0x0124},
3222 {0, 0xfffa, 0x0124},
3223 {0, 0xfff9, 0x0124},
3224 {0, 0x0086, 0x0127},
3225 {0, 0xfff8, 0x0124},
3226 {0, 0xfffd, 0x0124},
3227 {0, 0x0038, 0x0127},
3228 {0, 0xfff8, 0x0124},
3229 {0, 0xfffd, 0x0124},
3230 {0, 0x0008, 0x0127},
3231 {0, 0xfff8, 0x0124},
3232 {0, 0xfffd, 0x0124},
3233 {0, 0x0000, 0x0127},
3234 {0, 0xfff8, 0x0124},
3235 {0, 0xfffd, 0x0124},
3236 {0, 0xfffa, 0x0124},
3237 {0, 0x0003, 0x0106},
3238 {0, 0x0062, 0x0107},
3239 {0, 0x0003, 0x0111},
3240 };
3241#define NUM_INIT_DATA
3242
3243 unsigned short compression = 0; /* 0=none, 7=best frame rate */
3244 int f_rate; /* 0=Fastest 7=slowest */
3245
3246 if (IBMCAM_T(uvd)->initialized)
3247 return;
3248
3249 /* Internal frame rate is controlled by f_rate value */
3250 f_rate = 7 - framerate;
3251 RESTRICT_TO_RANGE(f_rate, 0, 7);
3252
3253 ibmcam_veio(uvd, 0, 0x0000, 0x0100);
3254 ibmcam_veio(uvd, 1, 0x0000, 0x0116);
3255 ibmcam_veio(uvd, 0, 0x0060, 0x0116);
3256 ibmcam_veio(uvd, 0, 0x0002, 0x0112);
3257 ibmcam_veio(uvd, 0, 0x0000, 0x0123);
3258 ibmcam_veio(uvd, 0, 0x0001, 0x0117);
3259 ibmcam_veio(uvd, 0, 0x0040, 0x0108);
3260 ibmcam_veio(uvd, 0, 0x0019, 0x012c);
3261 ibmcam_veio(uvd, 0, 0x0060, 0x0116);
3262 ibmcam_veio(uvd, 0, 0x0002, 0x0115);
3263 ibmcam_veio(uvd, 0, 0x0003, 0x0115);
3264 ibmcam_veio(uvd, 1, 0x0000, 0x0115);
3265 ibmcam_veio(uvd, 0, 0x000b, 0x0115);
3266 ibmcam_model3_Packet1(uvd, 0x000a, 0x0040);
3267 ibmcam_model3_Packet1(uvd, 0x000b, 0x00f6);
3268 ibmcam_model3_Packet1(uvd, 0x000c, 0x0002);
3269 ibmcam_model3_Packet1(uvd, 0x000d, 0x0020);
3270 ibmcam_model3_Packet1(uvd, 0x000e, 0x0033);
3271 ibmcam_model3_Packet1(uvd, 0x000f, 0x0007);
3272 ibmcam_model3_Packet1(uvd, 0x0010, 0x0000);
3273 ibmcam_model3_Packet1(uvd, 0x0011, 0x0070);
3274 ibmcam_model3_Packet1(uvd, 0x0012, 0x0030);
3275 ibmcam_model3_Packet1(uvd, 0x0013, 0x0000);
3276 ibmcam_model3_Packet1(uvd, 0x0014, 0x0001);
3277 ibmcam_model3_Packet1(uvd, 0x0015, 0x0001);
3278 ibmcam_model3_Packet1(uvd, 0x0016, 0x0001);
3279 ibmcam_model3_Packet1(uvd, 0x0017, 0x0001);
3280 ibmcam_model3_Packet1(uvd, 0x0018, 0x0000);
3281 ibmcam_model3_Packet1(uvd, 0x001e, 0x00c3);
3282 ibmcam_model3_Packet1(uvd, 0x0020, 0x0000);
3283 ibmcam_model3_Packet1(uvd, 0x0028, 0x0010);
3284 ibmcam_model3_Packet1(uvd, 0x0029, 0x0054);
3285 ibmcam_model3_Packet1(uvd, 0x002a, 0x0013);
3286 ibmcam_model3_Packet1(uvd, 0x002b, 0x0007);
3287 ibmcam_model3_Packet1(uvd, 0x002d, 0x0028);
3288 ibmcam_model3_Packet1(uvd, 0x002e, 0x0000);
3289 ibmcam_model3_Packet1(uvd, 0x0031, 0x0000);
3290 ibmcam_model3_Packet1(uvd, 0x0032, 0x0000);
3291 ibmcam_model3_Packet1(uvd, 0x0033, 0x0000);
3292 ibmcam_model3_Packet1(uvd, 0x0034, 0x0000);
3293 ibmcam_model3_Packet1(uvd, 0x0035, 0x0038);
3294 ibmcam_model3_Packet1(uvd, 0x003a, 0x0001);
3295 ibmcam_model3_Packet1(uvd, 0x003c, 0x001e);
3296 ibmcam_model3_Packet1(uvd, 0x003f, 0x000a);
3297 ibmcam_model3_Packet1(uvd, 0x0041, 0x0000);
3298 ibmcam_model3_Packet1(uvd, 0x0046, 0x003f);
3299 ibmcam_model3_Packet1(uvd, 0x0047, 0x0000);
3300 ibmcam_model3_Packet1(uvd, 0x0050, 0x0005);
3301 ibmcam_model3_Packet1(uvd, 0x0052, 0x001a);
3302 ibmcam_model3_Packet1(uvd, 0x0053, 0x0003);
3303 ibmcam_model3_Packet1(uvd, 0x005a, 0x006b);
3304 ibmcam_model3_Packet1(uvd, 0x005d, 0x001e);
3305 ibmcam_model3_Packet1(uvd, 0x005e, 0x0030);
3306 ibmcam_model3_Packet1(uvd, 0x005f, 0x0041);
3307 ibmcam_model3_Packet1(uvd, 0x0064, 0x0008);
3308 ibmcam_model3_Packet1(uvd, 0x0065, 0x0015);
3309 ibmcam_model3_Packet1(uvd, 0x0068, 0x000f);
3310 ibmcam_model3_Packet1(uvd, 0x0079, 0x0000);
3311 ibmcam_model3_Packet1(uvd, 0x007a, 0x0000);
3312 ibmcam_model3_Packet1(uvd, 0x007c, 0x003f);
3313 ibmcam_model3_Packet1(uvd, 0x0082, 0x000f);
3314 ibmcam_model3_Packet1(uvd, 0x0085, 0x0000);
3315 ibmcam_model3_Packet1(uvd, 0x0099, 0x0000);
3316 ibmcam_model3_Packet1(uvd, 0x009b, 0x0023);
3317 ibmcam_model3_Packet1(uvd, 0x009c, 0x0022);
3318 ibmcam_model3_Packet1(uvd, 0x009d, 0x0096);
3319 ibmcam_model3_Packet1(uvd, 0x009e, 0x0096);
3320 ibmcam_model3_Packet1(uvd, 0x009f, 0x000a);
3321
3322 switch (uvd->videosize) {
3323 case VIDEOSIZE_160x120:
3324 ibmcam_veio(uvd, 0, 0x0000, 0x0101); /* Same on 176x144, 320x240 */
3325 ibmcam_veio(uvd, 0, 0x00a0, 0x0103); /* Same on 176x144, 320x240 */
3326 ibmcam_veio(uvd, 0, 0x0078, 0x0105); /* Same on 176x144, 320x240 */
3327 ibmcam_veio(uvd, 0, 0x0000, 0x010a); /* Same */
3328 ibmcam_veio(uvd, 0, 0x0024, 0x010b); /* Differs everywhere */
3329 ibmcam_veio(uvd, 0, 0x00a9, 0x0119);
3330 ibmcam_veio(uvd, 0, 0x0016, 0x011b);
3331 ibmcam_veio(uvd, 0, 0x0002, 0x011d); /* Same on 176x144, 320x240 */
3332 ibmcam_veio(uvd, 0, 0x0003, 0x011e); /* Same on 176x144, 640x480 */
3333 ibmcam_veio(uvd, 0, 0x0000, 0x0129); /* Same */
3334 ibmcam_veio(uvd, 0, 0x00fc, 0x012b); /* Same */
3335 ibmcam_veio(uvd, 0, 0x0018, 0x0102);
3336 ibmcam_veio(uvd, 0, 0x0004, 0x0104);
3337 ibmcam_veio(uvd, 0, 0x0004, 0x011a);
3338 ibmcam_veio(uvd, 0, 0x0028, 0x011c);
3339 ibmcam_veio(uvd, 0, 0x0022, 0x012a); /* Same */
3340 ibmcam_veio(uvd, 0, 0x0000, 0x0118);
3341 ibmcam_veio(uvd, 0, 0x0000, 0x0132);
3342 ibmcam_model3_Packet1(uvd, 0x0021, 0x0001); /* Same */
3343 ibmcam_veio(uvd, 0, compression, 0x0109);
3344 break;
3345 case VIDEOSIZE_320x240:
3346 ibmcam_veio(uvd, 0, 0x0000, 0x0101); /* Same on 176x144, 320x240 */
3347 ibmcam_veio(uvd, 0, 0x00a0, 0x0103); /* Same on 176x144, 320x240 */
3348 ibmcam_veio(uvd, 0, 0x0078, 0x0105); /* Same on 176x144, 320x240 */
3349 ibmcam_veio(uvd, 0, 0x0000, 0x010a); /* Same */
3350 ibmcam_veio(uvd, 0, 0x0028, 0x010b); /* Differs everywhere */
3351 ibmcam_veio(uvd, 0, 0x0002, 0x011d); /* Same */
3352 ibmcam_veio(uvd, 0, 0x0000, 0x011e);
3353 ibmcam_veio(uvd, 0, 0x0000, 0x0129); /* Same */
3354 ibmcam_veio(uvd, 0, 0x00fc, 0x012b); /* Same */
3355 /* 4 commands from 160x120 skipped */
3356 ibmcam_veio(uvd, 0, 0x0022, 0x012a); /* Same */
3357 ibmcam_model3_Packet1(uvd, 0x0021, 0x0001); /* Same */
3358 ibmcam_veio(uvd, 0, compression, 0x0109);
3359 ibmcam_veio(uvd, 0, 0x00d9, 0x0119);
3360 ibmcam_veio(uvd, 0, 0x0006, 0x011b);
3361 ibmcam_veio(uvd, 0, 0x0021, 0x0102); /* Same on 320x240, 640x480 */
3362 ibmcam_veio(uvd, 0, 0x0010, 0x0104);
3363 ibmcam_veio(uvd, 0, 0x0004, 0x011a);
3364 ibmcam_veio(uvd, 0, 0x003f, 0x011c);
3365 ibmcam_veio(uvd, 0, 0x001c, 0x0118);
3366 ibmcam_veio(uvd, 0, 0x0000, 0x0132);
3367 break;
3368 case VIDEOSIZE_640x480:
3369 ibmcam_veio(uvd, 0, 0x00f0, 0x0105);
3370 ibmcam_veio(uvd, 0, 0x0000, 0x010a); /* Same */
3371 ibmcam_veio(uvd, 0, 0x0038, 0x010b); /* Differs everywhere */
3372 ibmcam_veio(uvd, 0, 0x00d9, 0x0119); /* Same on 320x240, 640x480 */
3373 ibmcam_veio(uvd, 0, 0x0006, 0x011b); /* Same on 320x240, 640x480 */
3374 ibmcam_veio(uvd, 0, 0x0004, 0x011d); /* NC */
3375 ibmcam_veio(uvd, 0, 0x0003, 0x011e); /* Same on 176x144, 640x480 */
3376 ibmcam_veio(uvd, 0, 0x0000, 0x0129); /* Same */
3377 ibmcam_veio(uvd, 0, 0x00fc, 0x012b); /* Same */
3378 ibmcam_veio(uvd, 0, 0x0021, 0x0102); /* Same on 320x240, 640x480 */
3379 ibmcam_veio(uvd, 0, 0x0016, 0x0104); /* NC */
3380 ibmcam_veio(uvd, 0, 0x0004, 0x011a); /* Same on 320x240, 640x480 */
3381 ibmcam_veio(uvd, 0, 0x003f, 0x011c); /* Same on 320x240, 640x480 */
3382 ibmcam_veio(uvd, 0, 0x0022, 0x012a); /* Same */
3383 ibmcam_veio(uvd, 0, 0x001c, 0x0118); /* Same on 320x240, 640x480 */
3384 ibmcam_model3_Packet1(uvd, 0x0021, 0x0001); /* Same */
3385 ibmcam_veio(uvd, 0, compression, 0x0109);
3386 ibmcam_veio(uvd, 0, 0x0040, 0x0101);
3387 ibmcam_veio(uvd, 0, 0x0040, 0x0103);
3388 ibmcam_veio(uvd, 0, 0x0000, 0x0132); /* Same on 320x240, 640x480 */
3389 break;
3390 }
3391 ibmcam_model3_Packet1(uvd, 0x007e, 0x000e); /* Hue */
3392 ibmcam_model3_Packet1(uvd, 0x0036, 0x0011); /* Brightness */
3393 ibmcam_model3_Packet1(uvd, 0x0060, 0x0002); /* Sharpness */
3394 ibmcam_model3_Packet1(uvd, 0x0061, 0x0004); /* Sharpness */
3395 ibmcam_model3_Packet1(uvd, 0x0062, 0x0005); /* Sharpness */
3396 ibmcam_model3_Packet1(uvd, 0x0063, 0x0014); /* Sharpness */
3397 ibmcam_model3_Packet1(uvd, 0x0096, 0x00a0); /* Red gain */
3398 ibmcam_model3_Packet1(uvd, 0x0097, 0x0096); /* Blue gain */
3399 ibmcam_model3_Packet1(uvd, 0x0067, 0x0001); /* Contrast */
3400 ibmcam_model3_Packet1(uvd, 0x005b, 0x000c); /* Contrast */
3401 ibmcam_model3_Packet1(uvd, 0x005c, 0x0016); /* Contrast */
3402 ibmcam_model3_Packet1(uvd, 0x0098, 0x000b);
3403 ibmcam_model3_Packet1(uvd, 0x002c, 0x0003); /* Was 1, broke 640x480 */
3404 ibmcam_model3_Packet1(uvd, 0x002f, 0x002a);
3405 ibmcam_model3_Packet1(uvd, 0x0030, 0x0029);
3406 ibmcam_model3_Packet1(uvd, 0x0037, 0x0002);
3407 ibmcam_model3_Packet1(uvd, 0x0038, 0x0059);
3408 ibmcam_model3_Packet1(uvd, 0x003d, 0x002e);
3409 ibmcam_model3_Packet1(uvd, 0x003e, 0x0028);
3410 ibmcam_model3_Packet1(uvd, 0x0078, 0x0005);
3411 ibmcam_model3_Packet1(uvd, 0x007b, 0x0011);
3412 ibmcam_model3_Packet1(uvd, 0x007d, 0x004b);
3413 ibmcam_model3_Packet1(uvd, 0x007f, 0x0022);
3414 ibmcam_model3_Packet1(uvd, 0x0080, 0x000c);
3415 ibmcam_model3_Packet1(uvd, 0x0081, 0x000b);
3416 ibmcam_model3_Packet1(uvd, 0x0083, 0x00fd);
3417 ibmcam_model3_Packet1(uvd, 0x0086, 0x000b);
3418 ibmcam_model3_Packet1(uvd, 0x0087, 0x000b);
3419 ibmcam_model3_Packet1(uvd, 0x007e, 0x000e);
3420 ibmcam_model3_Packet1(uvd, 0x0096, 0x00a0); /* Red gain */
3421 ibmcam_model3_Packet1(uvd, 0x0097, 0x0096); /* Blue gain */
3422 ibmcam_model3_Packet1(uvd, 0x0098, 0x000b);
3423
3424 switch (uvd->videosize) {
3425 case VIDEOSIZE_160x120:
3426 ibmcam_veio(uvd, 0, 0x0002, 0x0106);
3427 ibmcam_veio(uvd, 0, 0x0008, 0x0107);
3428 ibmcam_veio(uvd, 0, f_rate, 0x0111); /* Frame rate */
3429 ibmcam_model3_Packet1(uvd, 0x001f, 0x0000); /* Same */
3430 ibmcam_model3_Packet1(uvd, 0x0039, 0x001f); /* Same */
3431 ibmcam_model3_Packet1(uvd, 0x003b, 0x003c); /* Same */
3432 ibmcam_model3_Packet1(uvd, 0x0040, 0x000a);
3433 ibmcam_model3_Packet1(uvd, 0x0051, 0x000a);
3434 break;
3435 case VIDEOSIZE_320x240:
3436 ibmcam_veio(uvd, 0, 0x0003, 0x0106);
3437 ibmcam_veio(uvd, 0, 0x0062, 0x0107);
3438 ibmcam_veio(uvd, 0, f_rate, 0x0111); /* Frame rate */
3439 ibmcam_model3_Packet1(uvd, 0x001f, 0x0000); /* Same */
3440 ibmcam_model3_Packet1(uvd, 0x0039, 0x001f); /* Same */
3441 ibmcam_model3_Packet1(uvd, 0x003b, 0x003c); /* Same */
3442 ibmcam_model3_Packet1(uvd, 0x0040, 0x0008);
3443 ibmcam_model3_Packet1(uvd, 0x0051, 0x000b);
3444 break;
3445 case VIDEOSIZE_640x480:
3446 ibmcam_veio(uvd, 0, 0x0002, 0x0106); /* Adjustments */
3447 ibmcam_veio(uvd, 0, 0x00b4, 0x0107); /* Adjustments */
3448 ibmcam_veio(uvd, 0, f_rate, 0x0111); /* Frame rate */
3449 ibmcam_model3_Packet1(uvd, 0x001f, 0x0002); /* !Same */
3450 ibmcam_model3_Packet1(uvd, 0x0039, 0x003e); /* !Same */
3451 ibmcam_model3_Packet1(uvd, 0x0040, 0x0008);
3452 ibmcam_model3_Packet1(uvd, 0x0051, 0x000a);
3453 break;
3454 }
3455
3456 /* 01.01.08 - Added for RCA video in support -LO */
3457 if(init_model3_input) {
3458 if (debug > 0)
3459 info("Setting input to RCA.");
3460 for (i=0; i < ARRAY_SIZE(initData); i++) {
3461 ibmcam_veio(uvd, initData[i].req, initData[i].value, initData[i].index);
3462 }
3463 }
3464
3465 ibmcam_veio(uvd, 0, 0x0001, 0x0114);
3466 ibmcam_veio(uvd, 0, 0x00c0, 0x010c);
3467 usb_clear_halt(uvd->dev, usb_rcvisocpipe(uvd->dev, uvd->video_endp));
3468}
3469
3470/*
3471 * ibmcam_video_stop()
3472 *
3473 * This code tells camera to stop streaming. The interface remains
3474 * configured and bandwidth - claimed.
3475 */
3476static void ibmcam_video_stop(struct uvd *uvd)
3477{
3478 switch (IBMCAM_T(uvd)->camera_model) {
3479 case IBMCAM_MODEL_1:
3480 ibmcam_veio(uvd, 0, 0x00, 0x010c);
3481 ibmcam_veio(uvd, 0, 0x00, 0x010c);
3482 ibmcam_veio(uvd, 0, 0x01, 0x0114);
3483 ibmcam_veio(uvd, 0, 0xc0, 0x010c);
3484 ibmcam_veio(uvd, 0, 0x00, 0x010c);
3485 ibmcam_send_FF_04_02(uvd);
3486 ibmcam_veio(uvd, 1, 0x00, 0x0100);
3487 ibmcam_veio(uvd, 0, 0x81, 0x0100); /* LED Off */
3488 break;
3489 case IBMCAM_MODEL_2:
3490case IBMCAM_MODEL_4:
3491 ibmcam_veio(uvd, 0, 0x0000, 0x010c); /* Stop the camera */
3492
3493 ibmcam_model2_Packet1(uvd, 0x0030, 0x0004);
3494
3495 ibmcam_veio(uvd, 0, 0x0080, 0x0100); /* LED Off */
3496 ibmcam_veio(uvd, 0, 0x0020, 0x0111);
3497 ibmcam_veio(uvd, 0, 0x00a0, 0x0111);
3498
3499 ibmcam_model2_Packet1(uvd, 0x0030, 0x0002);
3500
3501 ibmcam_veio(uvd, 0, 0x0020, 0x0111);
3502 ibmcam_veio(uvd, 0, 0x0000, 0x0112);
3503 break;
3504 case IBMCAM_MODEL_3:
3505#if 1
3506 ibmcam_veio(uvd, 0, 0x0000, 0x010c);
3507
3508 /* Here we are supposed to select video interface alt. setting 0 */
3509 ibmcam_veio(uvd, 0, 0x0006, 0x012c);
3510
3511 ibmcam_model3_Packet1(uvd, 0x0046, 0x0000);
3512
3513 ibmcam_veio(uvd, 1, 0x0000, 0x0116);
3514 ibmcam_veio(uvd, 0, 0x0064, 0x0116);
3515 ibmcam_veio(uvd, 1, 0x0000, 0x0115);
3516 ibmcam_veio(uvd, 0, 0x0003, 0x0115);
3517 ibmcam_veio(uvd, 0, 0x0008, 0x0123);
3518 ibmcam_veio(uvd, 0, 0x0000, 0x0117);
3519 ibmcam_veio(uvd, 0, 0x0000, 0x0112);
3520 ibmcam_veio(uvd, 0, 0x0080, 0x0100);
3521 IBMCAM_T(uvd)->initialized = 0;
3522#endif
3523 break;
3524 } /* switch */
3525}
3526
3527/*
3528 * ibmcam_reinit_iso()
3529 *
3530 * This procedure sends couple of commands to the camera and then
3531 * resets the video pipe. This sequence was observed to reinit the
3532 * camera or, at least, to initiate ISO data stream.
3533 *
3534 * History:
3535 * 1/2/00 Created.
3536 */
3537static void ibmcam_reinit_iso(struct uvd *uvd, int do_stop)
3538{
3539 switch (IBMCAM_T(uvd)->camera_model) {
3540 case IBMCAM_MODEL_1:
3541 if (do_stop)
3542 ibmcam_video_stop(uvd);
3543 ibmcam_veio(uvd, 0, 0x0001, 0x0114);
3544 ibmcam_veio(uvd, 0, 0x00c0, 0x010c);
3545 usb_clear_halt(uvd->dev, usb_rcvisocpipe(uvd->dev, uvd->video_endp));
3546 ibmcam_model1_setup_after_video_if(uvd);
3547 break;
3548 case IBMCAM_MODEL_2:
3549 ibmcam_model2_setup_after_video_if(uvd);
3550 break;
3551 case IBMCAM_MODEL_3:
3552 ibmcam_video_stop(uvd);
3553 ibmcam_model3_setup_after_video_if(uvd);
3554 break;
3555 case IBMCAM_MODEL_4:
3556 ibmcam_model4_setup_after_video_if(uvd);
3557 break;
3558 }
3559}
3560
3561static void ibmcam_video_start(struct uvd *uvd)
3562{
3563 ibmcam_change_lighting_conditions(uvd);
3564 ibmcam_set_sharpness(uvd);
3565 ibmcam_reinit_iso(uvd, 0);
3566}
3567
3568/*
3569 * Return negative code on failure, 0 on success.
3570 */
3571static int ibmcam_setup_on_open(struct uvd *uvd)
3572{
3573 int setup_ok = 0; /* Success by default */
3574 /* Send init sequence only once, it's large! */
3575 if (!IBMCAM_T(uvd)->initialized) { /* FIXME rename */
3576 switch (IBMCAM_T(uvd)->camera_model) {
3577 case IBMCAM_MODEL_1:
3578 setup_ok = ibmcam_model1_setup(uvd);
3579 break;
3580 case IBMCAM_MODEL_2:
3581 setup_ok = ibmcam_model2_setup(uvd);
3582 break;
3583 case IBMCAM_MODEL_3:
3584 case IBMCAM_MODEL_4:
3585 /* We do all setup when Isoc stream is requested */
3586 break;
3587 }
3588 IBMCAM_T(uvd)->initialized = (setup_ok != 0);
3589 }
3590 return setup_ok;
3591}
3592
3593static void ibmcam_configure_video(struct uvd *uvd)
3594{
3595 if (uvd == NULL)
3596 return;
3597
3598 RESTRICT_TO_RANGE(init_brightness, 0, 255);
3599 RESTRICT_TO_RANGE(init_contrast, 0, 255);
3600 RESTRICT_TO_RANGE(init_color, 0, 255);
3601 RESTRICT_TO_RANGE(init_hue, 0, 255);
3602 RESTRICT_TO_RANGE(hue_correction, 0, 255);
3603
3604 memset(&uvd->vpic, 0, sizeof(uvd->vpic));
3605 memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
3606
3607 uvd->vpic.colour = init_color << 8;
3608 uvd->vpic.hue = init_hue << 8;
3609 uvd->vpic.brightness = init_brightness << 8;
3610 uvd->vpic.contrast = init_contrast << 8;
3611 uvd->vpic.whiteness = 105 << 8; /* This one isn't used */
3612 uvd->vpic.depth = 24;
3613 uvd->vpic.palette = VIDEO_PALETTE_RGB24;
3614
3615 memset(&uvd->vcap, 0, sizeof(uvd->vcap));
3616 strcpy(uvd->vcap.name, "IBM USB Camera");
3617 uvd->vcap.type = VID_TYPE_CAPTURE;
3618 uvd->vcap.channels = 1;
3619 uvd->vcap.audios = 0;
3620 uvd->vcap.maxwidth = VIDEOSIZE_X(uvd->canvas);
3621 uvd->vcap.maxheight = VIDEOSIZE_Y(uvd->canvas);
3622 uvd->vcap.minwidth = min_canvasWidth;
3623 uvd->vcap.minheight = min_canvasHeight;
3624
3625 memset(&uvd->vchan, 0, sizeof(uvd->vchan));
3626 uvd->vchan.flags = 0;
3627 uvd->vchan.tuners = 0;
3628 uvd->vchan.channel = 0;
3629 uvd->vchan.type = VIDEO_TYPE_CAMERA;
3630 strcpy(uvd->vchan.name, "Camera");
3631}
3632
3633/*
3634 * ibmcam_probe()
3635 *
3636 * This procedure queries device descriptor and accepts the interface
3637 * if it looks like IBM C-it camera.
3638 *
3639 * History:
3640 * 22-Jan-2000 Moved camera init code to ibmcam_open()
3641 * 27=Jan-2000 Changed to use static structures, added locking.
3642 * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
3643 * 03-Jul-2000 Fixed endianness bug.
3644 * 12-Nov-2000 Reworked to comply with new probe() signature.
3645 * 23-Jan-2001 Added compatibility with 2.2.x kernels.
3646 */
3647static int ibmcam_probe(struct usb_interface *intf, const struct usb_device_id *devid)
3648{
3649 struct usb_device *dev = interface_to_usbdev(intf);
3650 struct uvd *uvd = NULL;
3651 int ix, i, nas, model=0, canvasX=0, canvasY=0;
3652 int actInterface=-1, inactInterface=-1, maxPS=0;
3653 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
3654 unsigned char video_ep = 0;
3655
3656 if (debug >= 1)
3657 info("ibmcam_probe(%p,%u.)", intf, ifnum);
3658
3659 /* We don't handle multi-config cameras */
3660 if (dev->descriptor.bNumConfigurations != 1)
3661 return -ENODEV;
3662
3663 /* Check the version/revision */
3664 switch (le16_to_cpu(dev->descriptor.bcdDevice)) {
3665 case 0x0002:
3666 if (ifnum != 2)
3667 return -ENODEV;
3668 model = IBMCAM_MODEL_1;
3669 break;
3670 case 0x030A:
3671 if (ifnum != 0)
3672 return -ENODEV;
3673 if ((le16_to_cpu(dev->descriptor.idProduct) == NETCAM_PRODUCT_ID) ||
3674 (le16_to_cpu(dev->descriptor.idProduct) == VEO_800D_PRODUCT_ID))
3675 model = IBMCAM_MODEL_4;
3676 else
3677 model = IBMCAM_MODEL_2;
3678 break;
3679 case 0x0301:
3680 if (ifnum != 0)
3681 return -ENODEV;
3682 model = IBMCAM_MODEL_3;
3683 break;
3684 default:
3685 err("IBM camera with revision 0x%04x is not supported.",
3686 le16_to_cpu(dev->descriptor.bcdDevice));
3687 return -ENODEV;
3688 }
3689
3690 /* Print detailed info on what we found so far */
3691 do {
3692 char *brand = NULL;
3693 switch (le16_to_cpu(dev->descriptor.idProduct)) {
3694 case NETCAM_PRODUCT_ID:
3695 brand = "IBM NetCamera";
3696 break;
3697 case VEO_800C_PRODUCT_ID:
3698 brand = "Veo Stingray [800C]";
3699 break;
3700 case VEO_800D_PRODUCT_ID:
3701 brand = "Veo Stingray [800D]";
3702 break;
3703 case IBMCAM_PRODUCT_ID:
3704 default:
3705 brand = "IBM PC Camera"; /* a.k.a. Xirlink C-It */
3706 break;
3707 }
3708 info("%s USB camera found (model %d, rev. 0x%04x)",
3709 brand, model, le16_to_cpu(dev->descriptor.bcdDevice));
3710 } while (0);
3711
3712 /* Validate found interface: must have one ISO endpoint */
3713 nas = intf->num_altsetting;
3714 if (debug > 0)
3715 info("Number of alternate settings=%d.", nas);
3716 if (nas < 2) {
3717 err("Too few alternate settings for this camera!");
3718 return -ENODEV;
3719 }
3720 /* Validate all alternate settings */
3721 for (ix=0; ix < nas; ix++) {
3722 const struct usb_host_interface *interface;
3723 const struct usb_endpoint_descriptor *endpoint;
3724
3725 interface = &intf->altsetting[ix];
3726 i = interface->desc.bAlternateSetting;
3727 if (interface->desc.bNumEndpoints != 1) {
3728 err("Interface %d. has %u. endpoints!",
3729 ifnum, (unsigned)(interface->desc.bNumEndpoints));
3730 return -ENODEV;
3731 }
3732 endpoint = &interface->endpoint[0].desc;
3733 if (video_ep == 0)
3734 video_ep = endpoint->bEndpointAddress;
3735 else if (video_ep != endpoint->bEndpointAddress) {
3736 err("Alternate settings have different endpoint addresses!");
3737 return -ENODEV;
3738 }
3739 if ((endpoint->bmAttributes & 0x03) != 0x01) {
3740 err("Interface %d. has non-ISO endpoint!", ifnum);
3741 return -ENODEV;
3742 }
3743 if ((endpoint->bEndpointAddress & 0x80) == 0) {
3744 err("Interface %d. has ISO OUT endpoint!", ifnum);
3745 return -ENODEV;
3746 }
3747 if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
3748 if (inactInterface < 0)
3749 inactInterface = i;
3750 else {
3751 err("More than one inactive alt. setting!");
3752 return -ENODEV;
3753 }
3754 } else {
3755 if (actInterface < 0) {
3756 actInterface = i;
3757 maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
3758 if (debug > 0)
3759 info("Active setting=%d. maxPS=%d.", i, maxPS);
3760 } else
3761 err("More than one active alt. setting! Ignoring #%d.", i);
3762 }
3763 }
3764 if ((maxPS <= 0) || (actInterface < 0) || (inactInterface < 0)) {
3765 err("Failed to recognize the camera!");
3766 return -ENODEV;
3767 }
3768
3769 /* Validate options */
3770 switch (model) {
3771 case IBMCAM_MODEL_1:
3772 RESTRICT_TO_RANGE(lighting, 0, 2);
3773 RESTRICT_TO_RANGE(size, SIZE_128x96, SIZE_352x288);
3774 if (framerate < 0)
3775 framerate = 2;
3776 canvasX = 352;
3777 canvasY = 288;
3778 break;
3779 case IBMCAM_MODEL_2:
3780 RESTRICT_TO_RANGE(lighting, 0, 15);
3781 RESTRICT_TO_RANGE(size, SIZE_176x144, SIZE_352x240);
3782 if (framerate < 0)
3783 framerate = 2;
3784 canvasX = 352;
3785 canvasY = 240;
3786 break;
3787 case IBMCAM_MODEL_3:
3788 RESTRICT_TO_RANGE(lighting, 0, 15); /* FIXME */
3789 switch (size) {
3790 case SIZE_160x120:
3791 canvasX = 160;
3792 canvasY = 120;
3793 if (framerate < 0)
3794 framerate = 2;
3795 RESTRICT_TO_RANGE(framerate, 0, 5);
3796 break;
3797 default:
3798 info("IBM camera: using 320x240");
3799 size = SIZE_320x240;
3800 /* No break here */
3801 case SIZE_320x240:
3802 canvasX = 320;
3803 canvasY = 240;
3804 if (framerate < 0)
3805 framerate = 3;
3806 RESTRICT_TO_RANGE(framerate, 0, 5);
3807 break;
3808 case SIZE_640x480:
3809 canvasX = 640;
3810 canvasY = 480;
3811 framerate = 0; /* Slowest, and maybe even that is too fast */
3812 break;
3813 }
3814 break;
3815 case IBMCAM_MODEL_4:
3816 RESTRICT_TO_RANGE(lighting, 0, 2);
3817 switch (size) {
3818 case SIZE_128x96:
3819 canvasX = 128;
3820 canvasY = 96;
3821 break;
3822 case SIZE_160x120:
3823 canvasX = 160;
3824 canvasY = 120;
3825 break;
3826 default:
3827 info("IBM NetCamera: using 176x144");
3828 size = SIZE_176x144;
3829 /* No break here */
3830 case SIZE_176x144:
3831 canvasX = 176;
3832 canvasY = 144;
3833 break;
3834 case SIZE_320x240:
3835 canvasX = 320;
3836 canvasY = 240;
3837 break;
3838 case SIZE_352x288:
3839 canvasX = 352;
3840 canvasY = 288;
3841 break;
3842 }
3843 break;
3844 default:
3845 err("IBM camera: Model %d. not supported!", model);
3846 return -ENODEV;
3847 }
3848
3849 uvd = usbvideo_AllocateDevice(cams);
3850 if (uvd != NULL) {
3851 /* Here uvd is a fully allocated uvd object */
3852 uvd->flags = flags;
3853 uvd->debug = debug;
3854 uvd->dev = dev;
3855 uvd->iface = ifnum;
3856 uvd->ifaceAltInactive = inactInterface;
3857 uvd->ifaceAltActive = actInterface;
3858 uvd->video_endp = video_ep;
3859 uvd->iso_packet_len = maxPS;
3860 uvd->paletteBits = 1L << VIDEO_PALETTE_RGB24;
3861 uvd->defaultPalette = VIDEO_PALETTE_RGB24;
3862 uvd->canvas = VIDEOSIZE(canvasX, canvasY);
3863 uvd->videosize = ibmcam_size_to_videosize(size);
3864
3865 /* Initialize ibmcam-specific data */
3866 assert(IBMCAM_T(uvd) != NULL);
3867 IBMCAM_T(uvd)->camera_model = model;
3868 IBMCAM_T(uvd)->initialized = 0;
3869
3870 ibmcam_configure_video(uvd);
3871
3872 i = usbvideo_RegisterVideoDevice(uvd);
3873 if (i != 0) {
3874 err("usbvideo_RegisterVideoDevice() failed.");
3875 uvd = NULL;
3876 }
3877 }
3878 usb_set_intfdata (intf, uvd);
3879 return 0;
3880}
3881
3882
3883static struct usb_device_id id_table[] = {
3884 { USB_DEVICE_VER(IBMCAM_VENDOR_ID, IBMCAM_PRODUCT_ID, 0x0002, 0x0002) }, /* Model 1 */
3885 { USB_DEVICE_VER(IBMCAM_VENDOR_ID, IBMCAM_PRODUCT_ID, 0x030a, 0x030a) }, /* Model 2 */
3886 { USB_DEVICE_VER(IBMCAM_VENDOR_ID, IBMCAM_PRODUCT_ID, 0x0301, 0x0301) }, /* Model 3 */
3887 { USB_DEVICE_VER(IBMCAM_VENDOR_ID, NETCAM_PRODUCT_ID, 0x030a, 0x030a) }, /* Model 4 */
3888 { USB_DEVICE_VER(IBMCAM_VENDOR_ID, VEO_800C_PRODUCT_ID, 0x030a, 0x030a) }, /* Model 2 */
3889 { USB_DEVICE_VER(IBMCAM_VENDOR_ID, VEO_800D_PRODUCT_ID, 0x030a, 0x030a) }, /* Model 4 */
3890 { } /* Terminating entry */
3891};
3892
3893/*
3894 * ibmcam_init()
3895 *
3896 * This code is run to initialize the driver.
3897 *
3898 * History:
3899 * 1/27/00 Reworked to use statically allocated ibmcam structures.
3900 * 21/10/00 Completely redesigned to use usbvideo services.
3901 */
3902static int __init ibmcam_init(void)
3903{
3904 struct usbvideo_cb cbTbl;
3905 memset(&cbTbl, 0, sizeof(cbTbl));
3906 cbTbl.probe = ibmcam_probe;
3907 cbTbl.setupOnOpen = ibmcam_setup_on_open;
3908 cbTbl.videoStart = ibmcam_video_start;
3909 cbTbl.videoStop = ibmcam_video_stop;
3910 cbTbl.processData = ibmcam_ProcessIsocData;
3911 cbTbl.postProcess = usbvideo_DeinterlaceFrame;
3912 cbTbl.adjustPicture = ibmcam_adjust_picture;
3913 cbTbl.getFPS = ibmcam_calculate_fps;
3914 return usbvideo_register(
3915 &cams,
3916 MAX_IBMCAM,
3917 sizeof(ibmcam_t),
3918 "ibmcam",
3919 &cbTbl,
3920 THIS_MODULE,
3921 id_table);
3922}
3923
3924static void __exit ibmcam_cleanup(void)
3925{
3926 usbvideo_Deregister(&cams);
3927}
3928
3929MODULE_DEVICE_TABLE(usb, id_table);
3930
3931module_init(ibmcam_init);
3932module_exit(ibmcam_cleanup);
diff --git a/drivers/usb/media/konicawc.c b/drivers/usb/media/konicawc.c
deleted file mode 100644
index e2ede583518f..000000000000
--- a/drivers/usb/media/konicawc.c
+++ /dev/null
@@ -1,978 +0,0 @@
1/*
2 * konicawc.c - konica webcam driver
3 *
4 * Author: Simon Evans <spse@secret.org.uk>
5 *
6 * Copyright (C) 2002 Simon Evans
7 *
8 * Licence: GPL
9 *
10 * Driver for USB webcams based on Konica chipset. This
11 * chipset is used in Intel YC76 camera.
12 *
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/input.h>
19#include <linux/usb_input.h>
20
21#include "usbvideo.h"
22
23#define MAX_BRIGHTNESS 108
24#define MAX_CONTRAST 108
25#define MAX_SATURATION 108
26#define MAX_SHARPNESS 108
27#define MAX_WHITEBAL 372
28#define MAX_SPEED 6
29
30
31#define MAX_CAMERAS 1
32
33#define DRIVER_VERSION "v1.4"
34#define DRIVER_DESC "Konica Webcam driver"
35
36enum ctrl_req {
37 SetWhitebal = 0x01,
38 SetBrightness = 0x02,
39 SetSharpness = 0x03,
40 SetContrast = 0x04,
41 SetSaturation = 0x05,
42};
43
44
45enum frame_sizes {
46 SIZE_160X120 = 0,
47 SIZE_160X136 = 1,
48 SIZE_176X144 = 2,
49 SIZE_320X240 = 3,
50
51};
52
53#define MAX_FRAME_SIZE SIZE_320X240
54
55static struct usbvideo *cams;
56
57#ifdef CONFIG_USB_DEBUG
58static int debug;
59#define DEBUG(n, format, arg...) \
60 if (n <= debug) { \
61 printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __FUNCTION__ , ## arg); \
62 }
63#else
64#define DEBUG(n, arg...)
65static const int debug = 0;
66#endif
67
68
69/* Some default values for initial camera settings,
70 can be set by modprobe */
71
72static int size;
73static int speed = 6; /* Speed (fps) 0 (slowest) to 6 (fastest) */
74static int brightness = MAX_BRIGHTNESS/2;
75static int contrast = MAX_CONTRAST/2;
76static int saturation = MAX_SATURATION/2;
77static int sharpness = MAX_SHARPNESS/2;
78static int whitebal = 3*(MAX_WHITEBAL/4);
79
80static const int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
81
82/* These FPS speeds are from the windows config box. They are
83 * indexed on size (0-2) and speed (0-6). Divide by 3 to get the
84 * real fps.
85 */
86
87static const int spd_to_fps[][7] = { { 24, 40, 48, 60, 72, 80, 100 },
88 { 24, 40, 48, 60, 72, 80, 100 },
89 { 18, 30, 36, 45, 54, 60, 75 },
90 { 6, 10, 12, 15, 18, 21, 25 } };
91
92struct cam_size {
93 u16 width;
94 u16 height;
95 u8 cmd;
96};
97
98static const struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
99 { 160, 136, 0xa },
100 { 176, 144, 0x4 },
101 { 320, 240, 0x5 } };
102
103struct konicawc {
104 u8 brightness; /* camera uses 0 - 9, x11 for real value */
105 u8 contrast; /* as above */
106 u8 saturation; /* as above */
107 u8 sharpness; /* as above */
108 u8 white_bal; /* 0 - 33, x11 for real value */
109 u8 speed; /* Stored as 0 - 6, used as index in spd_to_* (above) */
110 u8 size; /* Frame Size */
111 int height;
112 int width;
113 struct urb *sts_urb[USBVIDEO_NUMSBUF];
114 u8 sts_buf[USBVIDEO_NUMSBUF][FRAMES_PER_DESC];
115 struct urb *last_data_urb;
116 int lastframe;
117 int cur_frame_size; /* number of bytes in current frame size */
118 int maxline; /* number of lines per frame */
119 int yplanesz; /* Number of bytes in the Y plane */
120 unsigned int buttonsts:1;
121#ifdef CONFIG_INPUT
122 struct input_dev *input;
123 char input_physname[64];
124#endif
125};
126
127
128#define konicawc_set_misc(uvd, req, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, req, value, index, NULL, 0)
129#define konicawc_get_misc(uvd, req, value, index, buf, sz) konicawc_ctrl_msg(uvd, USB_DIR_IN, req, value, index, buf, sz)
130#define konicawc_set_value(uvd, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, 2, value, index, NULL, 0)
131
132
133static int konicawc_ctrl_msg(struct uvd *uvd, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
134{
135 int retval = usb_control_msg(uvd->dev,
136 dir ? usb_rcvctrlpipe(uvd->dev, 0) : usb_sndctrlpipe(uvd->dev, 0),
137 request, 0x40 | dir, value, index, buf, len, 1000);
138 return retval < 0 ? retval : 0;
139}
140
141
142static inline void konicawc_camera_on(struct uvd *uvd)
143{
144 DEBUG(0, "camera on");
145 konicawc_set_misc(uvd, 0x2, 1, 0x0b);
146}
147
148
149static inline void konicawc_camera_off(struct uvd *uvd)
150{
151 DEBUG(0, "camera off");
152 konicawc_set_misc(uvd, 0x2, 0, 0x0b);
153}
154
155
156static void konicawc_set_camera_size(struct uvd *uvd)
157{
158 struct konicawc *cam = (struct konicawc *)uvd->user_data;
159
160 konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
161 cam->width = camera_sizes[cam->size].width;
162 cam->height = camera_sizes[cam->size].height;
163 cam->yplanesz = cam->height * cam->width;
164 cam->cur_frame_size = (cam->yplanesz * 3) / 2;
165 cam->maxline = cam->yplanesz / 256;
166 uvd->videosize = VIDEOSIZE(cam->width, cam->height);
167}
168
169
170static int konicawc_setup_on_open(struct uvd *uvd)
171{
172 struct konicawc *cam = (struct konicawc *)uvd->user_data;
173
174 DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
175 cam->brightness * 11);
176 konicawc_set_value(uvd, cam->brightness, SetBrightness);
177 DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
178 cam->white_bal * 11);
179 konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
180 DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
181 cam->contrast * 11);
182 konicawc_set_value(uvd, cam->contrast, SetContrast);
183 DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
184 cam->saturation * 11);
185 konicawc_set_value(uvd, cam->saturation, SetSaturation);
186 DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
187 cam->sharpness * 11);
188 konicawc_set_value(uvd, cam->sharpness, SetSharpness);
189 konicawc_set_camera_size(uvd);
190 cam->lastframe = -2;
191 cam->buttonsts = 0;
192 return 0;
193}
194
195
196static void konicawc_adjust_picture(struct uvd *uvd)
197{
198 struct konicawc *cam = (struct konicawc *)uvd->user_data;
199
200 konicawc_camera_off(uvd);
201 DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
202 uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
203 if(cam->brightness != uvd->vpic.brightness / 11) {
204 cam->brightness = uvd->vpic.brightness / 11;
205 DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
206 cam->brightness * 11);
207 konicawc_set_value(uvd, cam->brightness, SetBrightness);
208 }
209
210 DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
211 uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
212 if(cam->contrast != uvd->vpic.contrast / 11) {
213 cam->contrast = uvd->vpic.contrast / 11;
214 DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
215 cam->contrast * 11);
216 konicawc_set_value(uvd, cam->contrast, SetContrast);
217 }
218 konicawc_camera_on(uvd);
219}
220
221#ifdef CONFIG_INPUT
222
223static void konicawc_register_input(struct konicawc *cam, struct usb_device *dev)
224{
225 struct input_dev *input_dev;
226
227 usb_make_path(dev, cam->input_physname, sizeof(cam->input_physname));
228 strncat(cam->input_physname, "/input0", sizeof(cam->input_physname));
229
230 cam->input = input_dev = input_allocate_device();
231 if (!input_dev) {
232 warn("Not enough memory for camera's input device\n");
233 return;
234 }
235
236 input_dev->name = "Konicawc snapshot button";
237 input_dev->phys = cam->input_physname;
238 usb_to_input_id(dev, &input_dev->id);
239 input_dev->cdev.dev = &dev->dev;
240
241 input_dev->evbit[0] = BIT(EV_KEY);
242 input_dev->keybit[LONG(BTN_0)] = BIT(BTN_0);
243
244 input_dev->private = cam;
245
246 input_register_device(cam->input);
247}
248
249static void konicawc_unregister_input(struct konicawc *cam)
250{
251 if (cam->input) {
252 input_unregister_device(cam->input);
253 cam->input = NULL;
254 }
255}
256
257static void konicawc_report_buttonstat(struct konicawc *cam)
258{
259 if (cam->input) {
260 input_report_key(cam->input, BTN_0, cam->buttonsts);
261 input_sync(cam->input);
262 }
263}
264
265#else
266
267static inline void konicawc_register_input(struct konicawc *cam, struct usb_device *dev) { }
268static inline void konicawc_unregister_input(struct konicawc *cam) { }
269static inline void konicawc_report_buttonstat(struct konicawc *cam) { }
270
271#endif /* CONFIG_INPUT */
272
273static int konicawc_compress_iso(struct uvd *uvd, struct urb *dataurb, struct urb *stsurb)
274{
275 char *cdata;
276 int i, totlen = 0;
277 unsigned char *status = stsurb->transfer_buffer;
278 int keep = 0, discard = 0, bad = 0;
279 struct konicawc *cam = (struct konicawc *)uvd->user_data;
280
281 for (i = 0; i < dataurb->number_of_packets; i++) {
282 int button = cam->buttonsts;
283 unsigned char sts;
284 int n = dataurb->iso_frame_desc[i].actual_length;
285 int st = dataurb->iso_frame_desc[i].status;
286 cdata = dataurb->transfer_buffer +
287 dataurb->iso_frame_desc[i].offset;
288
289 /* Detect and ignore errored packets */
290 if (st < 0) {
291 DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
292 i, n, st);
293 uvd->stats.iso_err_count++;
294 continue;
295 }
296
297 /* Detect and ignore empty packets */
298 if (n <= 0) {
299 uvd->stats.iso_skip_count++;
300 continue;
301 }
302
303 /* See what the status data said about the packet */
304 sts = *(status+stsurb->iso_frame_desc[i].offset);
305
306 /* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
307 * otherwise:
308 * bit 0 0: keep packet
309 * 1: drop packet (padding data)
310 *
311 * bit 4 0 button not clicked
312 * 1 button clicked
313 * button is used to `take a picture' (in software)
314 */
315
316 if(sts < 0x80) {
317 button = !!(sts & 0x40);
318 sts &= ~0x40;
319 }
320
321 /* work out the button status, but don't do
322 anything with it for now */
323
324 if(button != cam->buttonsts) {
325 DEBUG(2, "button: %sclicked", button ? "" : "un");
326 cam->buttonsts = button;
327 konicawc_report_buttonstat(cam);
328 }
329
330 if(sts == 0x01) { /* drop frame */
331 discard++;
332 continue;
333 }
334
335 if((sts > 0x01) && (sts < 0x80)) {
336 info("unknown status %2.2x", sts);
337 bad++;
338 continue;
339 }
340 if(!sts && cam->lastframe == -2) {
341 DEBUG(2, "dropping frame looking for image start");
342 continue;
343 }
344
345 keep++;
346 if(sts & 0x80) { /* frame start */
347 unsigned char marker[] = { 0, 0xff, 0, 0x00 };
348
349 if(cam->lastframe == -2) {
350 DEBUG(2, "found initial image");
351 cam->lastframe = -1;
352 }
353
354 marker[3] = sts & 0x7F;
355 RingQueue_Enqueue(&uvd->dp, marker, 4);
356 totlen += 4;
357 }
358
359 totlen += n; /* Little local accounting */
360 RingQueue_Enqueue(&uvd->dp, cdata, n);
361 }
362 DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
363 keep, discard, bad, totlen);
364 return totlen;
365}
366
367
368static void resubmit_urb(struct uvd *uvd, struct urb *urb)
369{
370 int i, ret;
371 for (i = 0; i < FRAMES_PER_DESC; i++) {
372 urb->iso_frame_desc[i].status = 0;
373 }
374 urb->dev = uvd->dev;
375 urb->status = 0;
376 ret = usb_submit_urb(urb, GFP_ATOMIC);
377 DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
378 if(ret)
379 err("usb_submit_urb error (%d)", ret);
380
381}
382
383
384static void konicawc_isoc_irq(struct urb *urb, struct pt_regs *regs)
385{
386 struct uvd *uvd = urb->context;
387 struct konicawc *cam = (struct konicawc *)uvd->user_data;
388
389 /* We don't want to do anything if we are about to be removed! */
390 if (!CAMERA_IS_OPERATIONAL(uvd))
391 return;
392
393 if (!uvd->streaming) {
394 DEBUG(1, "Not streaming, but interrupt!");
395 return;
396 }
397
398 DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
399
400 uvd->stats.urb_count++;
401
402 if (urb->transfer_buffer_length > 32) {
403 cam->last_data_urb = urb;
404 return;
405 }
406 /* Copy the data received into ring queue */
407 if(cam->last_data_urb) {
408 int len = 0;
409 if(urb->start_frame != cam->last_data_urb->start_frame)
410 err("Lost sync on frames");
411 else if (!urb->status && !cam->last_data_urb->status)
412 len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
413
414 resubmit_urb(uvd, cam->last_data_urb);
415 resubmit_urb(uvd, urb);
416 cam->last_data_urb = NULL;
417 uvd->stats.urb_length = len;
418 uvd->stats.data_count += len;
419 if(len)
420 RingQueue_WakeUpInterruptible(&uvd->dp);
421 return;
422 }
423 return;
424}
425
426
427static int konicawc_start_data(struct uvd *uvd)
428{
429 struct usb_device *dev = uvd->dev;
430 int i, errFlag;
431 struct konicawc *cam = (struct konicawc *)uvd->user_data;
432 int pktsz;
433 struct usb_interface *intf;
434 struct usb_host_interface *interface = NULL;
435
436 intf = usb_ifnum_to_if(dev, uvd->iface);
437 if (intf)
438 interface = usb_altnum_to_altsetting(intf,
439 spd_to_iface[cam->speed]);
440 if (!interface)
441 return -ENXIO;
442 pktsz = le16_to_cpu(interface->endpoint[1].desc.wMaxPacketSize);
443 DEBUG(1, "pktsz = %d", pktsz);
444 if (!CAMERA_IS_OPERATIONAL(uvd)) {
445 err("Camera is not operational");
446 return -EFAULT;
447 }
448 uvd->curframe = -1;
449 konicawc_camera_on(uvd);
450 /* Alternate interface 1 is is the biggest frame size */
451 i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
452 if (i < 0) {
453 err("usb_set_interface error");
454 uvd->last_error = i;
455 return -EBUSY;
456 }
457
458 /* We double buffer the Iso lists */
459 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
460 int j, k;
461 struct urb *urb = uvd->sbuf[i].urb;
462 urb->dev = dev;
463 urb->context = uvd;
464 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
465 urb->interval = 1;
466 urb->transfer_flags = URB_ISO_ASAP;
467 urb->transfer_buffer = uvd->sbuf[i].data;
468 urb->complete = konicawc_isoc_irq;
469 urb->number_of_packets = FRAMES_PER_DESC;
470 urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
471 for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
472 urb->iso_frame_desc[j].offset = k;
473 urb->iso_frame_desc[j].length = pktsz;
474 }
475
476 urb = cam->sts_urb[i];
477 urb->dev = dev;
478 urb->context = uvd;
479 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp-1);
480 urb->interval = 1;
481 urb->transfer_flags = URB_ISO_ASAP;
482 urb->transfer_buffer = cam->sts_buf[i];
483 urb->complete = konicawc_isoc_irq;
484 urb->number_of_packets = FRAMES_PER_DESC;
485 urb->transfer_buffer_length = FRAMES_PER_DESC;
486 for (j=0; j < FRAMES_PER_DESC; j++) {
487 urb->iso_frame_desc[j].offset = j;
488 urb->iso_frame_desc[j].length = 1;
489 }
490 }
491
492 cam->last_data_urb = NULL;
493
494 /* Submit all URBs */
495 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
496 errFlag = usb_submit_urb(cam->sts_urb[i], GFP_KERNEL);
497 if (errFlag)
498 err("usb_submit_isoc(%d) ret %d", i, errFlag);
499
500 errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
501 if (errFlag)
502 err ("usb_submit_isoc(%d) ret %d", i, errFlag);
503 }
504
505 uvd->streaming = 1;
506 DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
507 return 0;
508}
509
510
511static void konicawc_stop_data(struct uvd *uvd)
512{
513 int i, j;
514 struct konicawc *cam;
515
516 if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
517 return;
518
519 konicawc_camera_off(uvd);
520 uvd->streaming = 0;
521 cam = (struct konicawc *)uvd->user_data;
522 cam->last_data_urb = NULL;
523
524 /* Unschedule all of the iso td's */
525 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
526 usb_kill_urb(uvd->sbuf[i].urb);
527 usb_kill_urb(cam->sts_urb[i]);
528 }
529
530 if (!uvd->remove_pending) {
531 /* Set packet size to 0 */
532 j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
533 if (j < 0) {
534 err("usb_set_interface() error %d.", j);
535 uvd->last_error = j;
536 }
537 }
538}
539
540
541static void konicawc_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
542{
543 struct konicawc *cam = (struct konicawc *)uvd->user_data;
544 int maxline = cam->maxline;
545 int yplanesz = cam->yplanesz;
546
547 assert(frame != NULL);
548
549 DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
550 DEBUG(3, "Frame state = %d", frame->scanstate);
551
552 if(frame->scanstate == ScanState_Scanning) {
553 int drop = 0;
554 int curframe;
555 int fdrops = 0;
556 DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
557 while(RingQueue_GetLength(&uvd->dp) >= 4) {
558 if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
559 (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
560 (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
561 (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
562 curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
563 if(cam->lastframe >= 0) {
564 fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
565 fdrops--;
566 if(fdrops) {
567 info("Dropped %d frames (%d -> %d)", fdrops,
568 cam->lastframe, curframe);
569 }
570 }
571 cam->lastframe = curframe;
572 frame->curline = 0;
573 frame->scanstate = ScanState_Lines;
574 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
575 break;
576 }
577 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
578 drop++;
579 }
580 if(drop)
581 DEBUG(2, "dropped %d bytes looking for new frame", drop);
582 }
583
584 if(frame->scanstate == ScanState_Scanning)
585 return;
586
587 /* Try to move data from queue into frame buffer
588 * We get data in blocks of 384 bytes made up of:
589 * 256 Y, 64 U, 64 V.
590 * This needs to be written out as a Y plane, a U plane and a V plane.
591 */
592
593 while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
594 /* Y */
595 RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
596 /* U */
597 RingQueue_Dequeue(&uvd->dp, frame->data + yplanesz + (frame->curline * 64), 64);
598 /* V */
599 RingQueue_Dequeue(&uvd->dp, frame->data + (5 * yplanesz)/4 + (frame->curline * 64), 64);
600 frame->seqRead_Length += 384;
601 frame->curline++;
602 }
603 /* See if we filled the frame */
604 if (frame->curline == maxline) {
605 DEBUG(5, "got whole frame");
606
607 frame->frameState = FrameState_Done_Hold;
608 frame->curline = 0;
609 uvd->curframe = -1;
610 uvd->stats.frame_num++;
611 }
612}
613
614
615static int konicawc_find_fps(int size, int fps)
616{
617 int i;
618
619 fps *= 3;
620 DEBUG(1, "konica_find_fps: size = %d fps = %d", size, fps);
621 if(fps <= spd_to_fps[size][0])
622 return 0;
623
624 if(fps >= spd_to_fps[size][MAX_SPEED])
625 return MAX_SPEED;
626
627 for(i = 0; i < MAX_SPEED; i++) {
628 if((fps >= spd_to_fps[size][i]) && (fps <= spd_to_fps[size][i+1])) {
629 DEBUG(2, "fps %d between %d and %d", fps, i, i+1);
630 if( (fps - spd_to_fps[size][i]) < (spd_to_fps[size][i+1] - fps))
631 return i;
632 else
633 return i+1;
634 }
635 }
636 return MAX_SPEED+1;
637}
638
639
640static int konicawc_set_video_mode(struct uvd *uvd, struct video_window *vw)
641{
642 struct konicawc *cam = (struct konicawc *)uvd->user_data;
643 int newspeed = cam->speed;
644 int newsize;
645 int x = vw->width;
646 int y = vw->height;
647 int fps = vw->flags;
648
649 if(x > 0 && y > 0) {
650 DEBUG(2, "trying to find size %d,%d", x, y);
651 for(newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
652 if((camera_sizes[newsize].width == x) && (camera_sizes[newsize].height == y))
653 break;
654 }
655 } else {
656 newsize = cam->size;
657 }
658
659 if(newsize > MAX_FRAME_SIZE) {
660 DEBUG(1, "couldn't find size %d,%d", x, y);
661 return -EINVAL;
662 }
663
664 if(fps > 0) {
665 DEBUG(1, "trying to set fps to %d", fps);
666 newspeed = konicawc_find_fps(newsize, fps);
667 DEBUG(1, "find_fps returned %d (%d)", newspeed, spd_to_fps[newsize][newspeed]);
668 }
669
670 if(newspeed > MAX_SPEED)
671 return -EINVAL;
672
673 DEBUG(1, "setting size to %d speed to %d", newsize, newspeed);
674 if((newsize == cam->size) && (newspeed == cam->speed)) {
675 DEBUG(1, "Nothing to do");
676 return 0;
677 }
678 DEBUG(0, "setting to %dx%d @ %d fps", camera_sizes[newsize].width,
679 camera_sizes[newsize].height, spd_to_fps[newsize][newspeed]/3);
680
681 konicawc_stop_data(uvd);
682 uvd->ifaceAltActive = spd_to_iface[newspeed];
683 DEBUG(1, "new interface = %d", uvd->ifaceAltActive);
684 cam->speed = newspeed;
685
686 if(cam->size != newsize) {
687 cam->size = newsize;
688 konicawc_set_camera_size(uvd);
689 }
690
691 /* Flush the input queue and clear any current frame in progress */
692
693 RingQueue_Flush(&uvd->dp);
694 cam->lastframe = -2;
695 if(uvd->curframe != -1) {
696 uvd->frame[uvd->curframe].curline = 0;
697 uvd->frame[uvd->curframe].seqRead_Length = 0;
698 uvd->frame[uvd->curframe].seqRead_Index = 0;
699 }
700
701 konicawc_start_data(uvd);
702 return 0;
703}
704
705
706static int konicawc_calculate_fps(struct uvd *uvd)
707{
708 struct konicawc *cam = uvd->user_data;
709 return spd_to_fps[cam->size][cam->speed]/3;
710}
711
712
713static void konicawc_configure_video(struct uvd *uvd)
714{
715 struct konicawc *cam = (struct konicawc *)uvd->user_data;
716 u8 buf[2];
717
718 memset(&uvd->vpic, 0, sizeof(uvd->vpic));
719 memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
720
721 RESTRICT_TO_RANGE(brightness, 0, MAX_BRIGHTNESS);
722 RESTRICT_TO_RANGE(contrast, 0, MAX_CONTRAST);
723 RESTRICT_TO_RANGE(saturation, 0, MAX_SATURATION);
724 RESTRICT_TO_RANGE(sharpness, 0, MAX_SHARPNESS);
725 RESTRICT_TO_RANGE(whitebal, 0, MAX_WHITEBAL);
726
727 cam->brightness = brightness / 11;
728 cam->contrast = contrast / 11;
729 cam->saturation = saturation / 11;
730 cam->sharpness = sharpness / 11;
731 cam->white_bal = whitebal / 11;
732
733 uvd->vpic.colour = 108;
734 uvd->vpic.hue = 108;
735 uvd->vpic.brightness = brightness;
736 uvd->vpic.contrast = contrast;
737 uvd->vpic.whiteness = whitebal;
738 uvd->vpic.depth = 6;
739 uvd->vpic.palette = VIDEO_PALETTE_YUV420P;
740
741 memset(&uvd->vcap, 0, sizeof(uvd->vcap));
742 strcpy(uvd->vcap.name, "Konica Webcam");
743 uvd->vcap.type = VID_TYPE_CAPTURE;
744 uvd->vcap.channels = 1;
745 uvd->vcap.audios = 0;
746 uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
747 uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
748 uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
749 uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
750
751 memset(&uvd->vchan, 0, sizeof(uvd->vchan));
752 uvd->vchan.flags = 0 ;
753 uvd->vchan.tuners = 0;
754 uvd->vchan.channel = 0;
755 uvd->vchan.type = VIDEO_TYPE_CAMERA;
756 strcpy(uvd->vchan.name, "Camera");
757
758 /* Talk to device */
759 DEBUG(1, "device init");
760 if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
761 DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
762 if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
763 DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
764 if(konicawc_set_misc(uvd, 0x2, 0, 0xd))
765 DEBUG(2, "2,0,d failed");
766 DEBUG(1, "setting initial values");
767}
768
769static int konicawc_probe(struct usb_interface *intf, const struct usb_device_id *devid)
770{
771 struct usb_device *dev = interface_to_usbdev(intf);
772 struct uvd *uvd = NULL;
773 int ix, i, nas;
774 int actInterface=-1, inactInterface=-1, maxPS=0;
775 unsigned char video_ep = 0;
776
777 DEBUG(1, "konicawc_probe(%p)", intf);
778
779 /* We don't handle multi-config cameras */
780 if (dev->descriptor.bNumConfigurations != 1)
781 return -ENODEV;
782
783 info("Konica Webcam (rev. 0x%04x)", le16_to_cpu(dev->descriptor.bcdDevice));
784 RESTRICT_TO_RANGE(speed, 0, MAX_SPEED);
785
786 /* Validate found interface: must have one ISO endpoint */
787 nas = intf->num_altsetting;
788 if (nas != 8) {
789 err("Incorrect number of alternate settings (%d) for this camera!", nas);
790 return -ENODEV;
791 }
792 /* Validate all alternate settings */
793 for (ix=0; ix < nas; ix++) {
794 const struct usb_host_interface *interface;
795 const struct usb_endpoint_descriptor *endpoint;
796
797 interface = &intf->altsetting[ix];
798 i = interface->desc.bAlternateSetting;
799 if (interface->desc.bNumEndpoints != 2) {
800 err("Interface %d. has %u. endpoints!",
801 interface->desc.bInterfaceNumber,
802 (unsigned)(interface->desc.bNumEndpoints));
803 return -ENODEV;
804 }
805 endpoint = &interface->endpoint[1].desc;
806 DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
807 endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize));
808 if (video_ep == 0)
809 video_ep = endpoint->bEndpointAddress;
810 else if (video_ep != endpoint->bEndpointAddress) {
811 err("Alternate settings have different endpoint addresses!");
812 return -ENODEV;
813 }
814 if ((endpoint->bmAttributes & 0x03) != 0x01) {
815 err("Interface %d. has non-ISO endpoint!",
816 interface->desc.bInterfaceNumber);
817 return -ENODEV;
818 }
819 if ((endpoint->bEndpointAddress & 0x80) == 0) {
820 err("Interface %d. has ISO OUT endpoint!",
821 interface->desc.bInterfaceNumber);
822 return -ENODEV;
823 }
824 if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
825 if (inactInterface < 0)
826 inactInterface = i;
827 else {
828 err("More than one inactive alt. setting!");
829 return -ENODEV;
830 }
831 } else {
832 if (i == spd_to_iface[speed]) {
833 /* This one is the requested one */
834 actInterface = i;
835 }
836 }
837 if (le16_to_cpu(endpoint->wMaxPacketSize) > maxPS)
838 maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
839 }
840 if(actInterface == -1) {
841 err("Cant find required endpoint");
842 return -ENODEV;
843 }
844
845 DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
846
847 uvd = usbvideo_AllocateDevice(cams);
848 if (uvd != NULL) {
849 struct konicawc *cam = (struct konicawc *)(uvd->user_data);
850 /* Here uvd is a fully allocated uvd object */
851 for(i = 0; i < USBVIDEO_NUMSBUF; i++) {
852 cam->sts_urb[i] = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
853 if(cam->sts_urb[i] == NULL) {
854 while(i--) {
855 usb_free_urb(cam->sts_urb[i]);
856 }
857 err("can't allocate urbs");
858 return -ENOMEM;
859 }
860 }
861 cam->speed = speed;
862 RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
863 cam->width = camera_sizes[size].width;
864 cam->height = camera_sizes[size].height;
865 cam->size = size;
866
867 uvd->flags = 0;
868 uvd->debug = debug;
869 uvd->dev = dev;
870 uvd->iface = intf->altsetting->desc.bInterfaceNumber;
871 uvd->ifaceAltInactive = inactInterface;
872 uvd->ifaceAltActive = actInterface;
873 uvd->video_endp = video_ep;
874 uvd->iso_packet_len = maxPS;
875 uvd->paletteBits = 1L << VIDEO_PALETTE_YUV420P;
876 uvd->defaultPalette = VIDEO_PALETTE_YUV420P;
877 uvd->canvas = VIDEOSIZE(320, 240);
878 uvd->videosize = VIDEOSIZE(cam->width, cam->height);
879
880 /* Initialize konicawc specific data */
881 konicawc_configure_video(uvd);
882
883 i = usbvideo_RegisterVideoDevice(uvd);
884 uvd->max_frame_size = (320 * 240 * 3)/2;
885 if (i != 0) {
886 err("usbvideo_RegisterVideoDevice() failed.");
887 uvd = NULL;
888 }
889
890 konicawc_register_input(cam, dev);
891 }
892
893 if (uvd) {
894 usb_set_intfdata (intf, uvd);
895 return 0;
896 }
897 return -EIO;
898}
899
900
901static void konicawc_free_uvd(struct uvd *uvd)
902{
903 int i;
904 struct konicawc *cam = (struct konicawc *)uvd->user_data;
905
906 konicawc_unregister_input(cam);
907
908 for (i = 0; i < USBVIDEO_NUMSBUF; i++) {
909 usb_free_urb(cam->sts_urb[i]);
910 cam->sts_urb[i] = NULL;
911 }
912}
913
914
915static struct usb_device_id id_table[] = {
916 { USB_DEVICE(0x04c8, 0x0720) }, /* Intel YC 76 */
917 { } /* Terminating entry */
918};
919
920
921static int __init konicawc_init(void)
922{
923 struct usbvideo_cb cbTbl;
924 info(DRIVER_DESC " " DRIVER_VERSION);
925 memset(&cbTbl, 0, sizeof(cbTbl));
926 cbTbl.probe = konicawc_probe;
927 cbTbl.setupOnOpen = konicawc_setup_on_open;
928 cbTbl.processData = konicawc_process_isoc;
929 cbTbl.getFPS = konicawc_calculate_fps;
930 cbTbl.setVideoMode = konicawc_set_video_mode;
931 cbTbl.startDataPump = konicawc_start_data;
932 cbTbl.stopDataPump = konicawc_stop_data;
933 cbTbl.adjustPicture = konicawc_adjust_picture;
934 cbTbl.userFree = konicawc_free_uvd;
935 return usbvideo_register(
936 &cams,
937 MAX_CAMERAS,
938 sizeof(struct konicawc),
939 "konicawc",
940 &cbTbl,
941 THIS_MODULE,
942 id_table);
943}
944
945
946static void __exit konicawc_cleanup(void)
947{
948 usbvideo_Deregister(&cams);
949}
950
951
952MODULE_DEVICE_TABLE(usb, id_table);
953
954MODULE_LICENSE("GPL");
955MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
956MODULE_DESCRIPTION(DRIVER_DESC);
957module_param(speed, int, 0);
958MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
959module_param(size, int, 0);
960MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
961module_param(brightness, int, 0);
962MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
963module_param(contrast, int, 0);
964MODULE_PARM_DESC(contrast, "Initial contrast 0 - 108");
965module_param(saturation, int, 0);
966MODULE_PARM_DESC(saturation, "Initial saturation 0 - 108");
967module_param(sharpness, int, 0);
968MODULE_PARM_DESC(sharpness, "Initial brightness 0 - 108");
969module_param(whitebal, int, 0);
970MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
971
972#ifdef CONFIG_USB_DEBUG
973module_param(debug, int, S_IRUGO | S_IWUSR);
974MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
975#endif
976
977module_init(konicawc_init);
978module_exit(konicawc_cleanup);
diff --git a/drivers/usb/media/ov511.c b/drivers/usb/media/ov511.c
deleted file mode 100644
index 51e9cc06f7e3..000000000000
--- a/drivers/usb/media/ov511.c
+++ /dev/null
@@ -1,5935 +0,0 @@
1/*
2 * OmniVision OV511 Camera-to-USB Bridge Driver
3 *
4 * Copyright (c) 1999-2003 Mark W. McClelland
5 * Original decompression code Copyright 1998-2000 OmniVision Technologies
6 * Many improvements by Bret Wallach <bwallac1@san.rr.com>
7 * Color fixes by by Orion Sky Lawlor <olawlor@acm.org> (2/26/2000)
8 * Snapshot code by Kevin Moore
9 * OV7620 fixes by Charl P. Botha <cpbotha@ieee.org>
10 * Changes by Claudio Matsuoka <claudio@conectiva.com>
11 * Original SAA7111A code by Dave Perks <dperks@ibm.net>
12 * URB error messages from pwc driver by Nemosoft
13 * generic_ioctl() code from videodev.c by Gerd Knorr and Alan Cox
14 * Memory management (rvmalloc) code from bttv driver, by Gerd Knorr and others
15 *
16 * Based on the Linux CPiA driver written by Peter Pregler,
17 * Scott J. Bertin and Johannes Erdfelt.
18 *
19 * Please see the file: Documentation/usb/ov511.txt
20 * and the website at: http://alpha.dyndns.org/ov511
21 * for more info.
22 *
23 * This program is free software; you can redistribute it and/or modify it
24 * under the terms of the GNU General Public License as published by the
25 * Free Software Foundation; either version 2 of the License, or (at your
26 * option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful, but
29 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
30 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
31 * for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software Foundation,
35 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 */
37
38#include <linux/config.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/vmalloc.h>
42#include <linux/slab.h>
43#include <linux/ctype.h>
44#include <linux/pagemap.h>
45#include <asm/semaphore.h>
46#include <asm/processor.h>
47#include <linux/mm.h>
48#include <linux/device.h>
49
50#if defined (__i386__)
51 #include <asm/cpufeature.h>
52#endif
53
54#include "ov511.h"
55
56/*
57 * Version Information
58 */
59#define DRIVER_VERSION "v1.64 for Linux 2.5"
60#define EMAIL "mark@alpha.dyndns.org"
61#define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org> & Bret Wallach \
62 & Orion Sky Lawlor <olawlor@acm.org> & Kevin Moore & Charl P. Botha \
63 <cpbotha@ieee.org> & Claudio Matsuoka <claudio@conectiva.com>"
64#define DRIVER_DESC "ov511 USB Camera Driver"
65
66#define OV511_I2C_RETRIES 3
67#define ENABLE_Y_QUANTABLE 1
68#define ENABLE_UV_QUANTABLE 1
69
70#define OV511_MAX_UNIT_VIDEO 16
71
72/* Pixel count * bytes per YUV420 pixel (1.5) */
73#define MAX_FRAME_SIZE(w, h) ((w) * (h) * 3 / 2)
74
75#define MAX_DATA_SIZE(w, h) (MAX_FRAME_SIZE(w, h) + sizeof(struct timeval))
76
77/* Max size * bytes per YUV420 pixel (1.5) + one extra isoc frame for safety */
78#define MAX_RAW_DATA_SIZE(w, h) ((w) * (h) * 3 / 2 + 1024)
79
80#define FATAL_ERROR(rc) ((rc) < 0 && (rc) != -EPERM)
81
82/**********************************************************************
83 * Module Parameters
84 * (See ov511.txt for detailed descriptions of these)
85 **********************************************************************/
86
87/* These variables (and all static globals) default to zero */
88static int autobright = 1;
89static int autogain = 1;
90static int autoexp = 1;
91static int debug;
92static int snapshot;
93static int cams = 1;
94static int compress;
95static int testpat;
96static int dumppix;
97static int led = 1;
98static int dump_bridge;
99static int dump_sensor;
100static int printph;
101static int phy = 0x1f;
102static int phuv = 0x05;
103static int pvy = 0x06;
104static int pvuv = 0x06;
105static int qhy = 0x14;
106static int qhuv = 0x03;
107static int qvy = 0x04;
108static int qvuv = 0x04;
109static int lightfreq;
110static int bandingfilter;
111static int clockdiv = -1;
112static int packetsize = -1;
113static int framedrop = -1;
114static int fastset;
115static int force_palette;
116static int backlight;
117static int unit_video[OV511_MAX_UNIT_VIDEO];
118static int remove_zeros;
119static int mirror;
120static int ov518_color;
121
122module_param(autobright, int, 0);
123MODULE_PARM_DESC(autobright, "Sensor automatically changes brightness");
124module_param(autogain, int, 0);
125MODULE_PARM_DESC(autogain, "Sensor automatically changes gain");
126module_param(autoexp, int, 0);
127MODULE_PARM_DESC(autoexp, "Sensor automatically changes exposure");
128module_param(debug, int, 0);
129MODULE_PARM_DESC(debug,
130 "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=max");
131module_param(snapshot, int, 0);
132MODULE_PARM_DESC(snapshot, "Enable snapshot mode");
133module_param(cams, int, 0);
134MODULE_PARM_DESC(cams, "Number of simultaneous cameras");
135module_param(compress, int, 0);
136MODULE_PARM_DESC(compress, "Turn on compression");
137module_param(testpat, int, 0);
138MODULE_PARM_DESC(testpat,
139 "Replace image with vertical bar testpattern (only partially working)");
140module_param(dumppix, int, 0);
141MODULE_PARM_DESC(dumppix, "Dump raw pixel data");
142module_param(led, int, 0);
143MODULE_PARM_DESC(led,
144 "LED policy (OV511+ or later). 0=off, 1=on (default), 2=auto (on when open)");
145module_param(dump_bridge, int, 0);
146MODULE_PARM_DESC(dump_bridge, "Dump the bridge registers");
147module_param(dump_sensor, int, 0);
148MODULE_PARM_DESC(dump_sensor, "Dump the sensor registers");
149module_param(printph, int, 0);
150MODULE_PARM_DESC(printph, "Print frame start/end headers");
151module_param(phy, int, 0);
152MODULE_PARM_DESC(phy, "Prediction range (horiz. Y)");
153module_param(phuv, int, 0);
154MODULE_PARM_DESC(phuv, "Prediction range (horiz. UV)");
155module_param(pvy, int, 0);
156MODULE_PARM_DESC(pvy, "Prediction range (vert. Y)");
157module_param(pvuv, int, 0);
158MODULE_PARM_DESC(pvuv, "Prediction range (vert. UV)");
159module_param(qhy, int, 0);
160MODULE_PARM_DESC(qhy, "Quantization threshold (horiz. Y)");
161module_param(qhuv, int, 0);
162MODULE_PARM_DESC(qhuv, "Quantization threshold (horiz. UV)");
163module_param(qvy, int, 0);
164MODULE_PARM_DESC(qvy, "Quantization threshold (vert. Y)");
165module_param(qvuv, int, 0);
166MODULE_PARM_DESC(qvuv, "Quantization threshold (vert. UV)");
167module_param(lightfreq, int, 0);
168MODULE_PARM_DESC(lightfreq,
169 "Light frequency. Set to 50 or 60 Hz, or zero for default settings");
170module_param(bandingfilter, int, 0);
171MODULE_PARM_DESC(bandingfilter,
172 "Enable banding filter (to reduce effects of fluorescent lighting)");
173module_param(clockdiv, int, 0);
174MODULE_PARM_DESC(clockdiv, "Force pixel clock divisor to a specific value");
175module_param(packetsize, int, 0);
176MODULE_PARM_DESC(packetsize, "Force a specific isoc packet size");
177module_param(framedrop, int, 0);
178MODULE_PARM_DESC(framedrop, "Force a specific frame drop register setting");
179module_param(fastset, int, 0);
180MODULE_PARM_DESC(fastset, "Allows picture settings to take effect immediately");
181module_param(force_palette, int, 0);
182MODULE_PARM_DESC(force_palette, "Force the palette to a specific value");
183module_param(backlight, int, 0);
184MODULE_PARM_DESC(backlight, "For objects that are lit from behind");
185static int num_uv;
186module_param_array(unit_video, int, &num_uv, 0);
187MODULE_PARM_DESC(unit_video,
188 "Force use of specific minor number(s). 0 is not allowed.");
189module_param(remove_zeros, int, 0);
190MODULE_PARM_DESC(remove_zeros,
191 "Remove zero-padding from uncompressed incoming data");
192module_param(mirror, int, 0);
193MODULE_PARM_DESC(mirror, "Reverse image horizontally");
194module_param(ov518_color, int, 0);
195MODULE_PARM_DESC(ov518_color, "Enable OV518 color (experimental)");
196
197MODULE_AUTHOR(DRIVER_AUTHOR);
198MODULE_DESCRIPTION(DRIVER_DESC);
199MODULE_LICENSE("GPL");
200
201/**********************************************************************
202 * Miscellaneous Globals
203 **********************************************************************/
204
205static struct usb_driver ov511_driver;
206
207/* Number of times to retry a failed I2C transaction. Increase this if you
208 * are getting "Failed to read sensor ID..." */
209static const int i2c_detect_tries = 5;
210
211static struct usb_device_id device_table [] = {
212 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511) },
213 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511PLUS) },
214 { USB_DEVICE(VEND_OMNIVISION, PROD_OV518) },
215 { USB_DEVICE(VEND_OMNIVISION, PROD_OV518PLUS) },
216 { USB_DEVICE(VEND_MATTEL, PROD_ME2CAM) },
217 { } /* Terminating entry */
218};
219
220MODULE_DEVICE_TABLE (usb, device_table);
221
222static unsigned char yQuanTable511[] = OV511_YQUANTABLE;
223static unsigned char uvQuanTable511[] = OV511_UVQUANTABLE;
224static unsigned char yQuanTable518[] = OV518_YQUANTABLE;
225static unsigned char uvQuanTable518[] = OV518_UVQUANTABLE;
226
227/**********************************************************************
228 * Symbolic Names
229 **********************************************************************/
230
231/* Known OV511-based cameras */
232static struct symbolic_list camlist[] = {
233 { 0, "Generic Camera (no ID)" },
234 { 1, "Mustek WCam 3X" },
235 { 3, "D-Link DSB-C300" },
236 { 4, "Generic OV511/OV7610" },
237 { 5, "Puretek PT-6007" },
238 { 6, "Lifeview USB Life TV (NTSC)" },
239 { 21, "Creative Labs WebCam 3" },
240 { 22, "Lifeview USB Life TV (PAL D/K+B/G)" },
241 { 36, "Koala-Cam" },
242 { 38, "Lifeview USB Life TV (PAL)" },
243 { 41, "Samsung Anycam MPC-M10" },
244 { 43, "Mtekvision Zeca MV402" },
245 { 46, "Suma eON" },
246 { 70, "Lifeview USB Life TV (PAL/SECAM)" },
247 { 100, "Lifeview RoboCam" },
248 { 102, "AverMedia InterCam Elite" },
249 { 112, "MediaForte MV300" }, /* or OV7110 evaluation kit */
250 { 134, "Ezonics EZCam II" },
251 { 192, "Webeye 2000B" },
252 { 253, "Alpha Vision Tech. AlphaCam SE" },
253 { -1, NULL }
254};
255
256/* Video4Linux1 Palettes */
257static struct symbolic_list v4l1_plist[] = {
258 { VIDEO_PALETTE_GREY, "GREY" },
259 { VIDEO_PALETTE_HI240, "HI240" },
260 { VIDEO_PALETTE_RGB565, "RGB565" },
261 { VIDEO_PALETTE_RGB24, "RGB24" },
262 { VIDEO_PALETTE_RGB32, "RGB32" },
263 { VIDEO_PALETTE_RGB555, "RGB555" },
264 { VIDEO_PALETTE_YUV422, "YUV422" },
265 { VIDEO_PALETTE_YUYV, "YUYV" },
266 { VIDEO_PALETTE_UYVY, "UYVY" },
267 { VIDEO_PALETTE_YUV420, "YUV420" },
268 { VIDEO_PALETTE_YUV411, "YUV411" },
269 { VIDEO_PALETTE_RAW, "RAW" },
270 { VIDEO_PALETTE_YUV422P,"YUV422P" },
271 { VIDEO_PALETTE_YUV411P,"YUV411P" },
272 { VIDEO_PALETTE_YUV420P,"YUV420P" },
273 { VIDEO_PALETTE_YUV410P,"YUV410P" },
274 { -1, NULL }
275};
276
277static struct symbolic_list brglist[] = {
278 { BRG_OV511, "OV511" },
279 { BRG_OV511PLUS, "OV511+" },
280 { BRG_OV518, "OV518" },
281 { BRG_OV518PLUS, "OV518+" },
282 { -1, NULL }
283};
284
285static struct symbolic_list senlist[] = {
286 { SEN_OV76BE, "OV76BE" },
287 { SEN_OV7610, "OV7610" },
288 { SEN_OV7620, "OV7620" },
289 { SEN_OV7620AE, "OV7620AE" },
290 { SEN_OV6620, "OV6620" },
291 { SEN_OV6630, "OV6630" },
292 { SEN_OV6630AE, "OV6630AE" },
293 { SEN_OV6630AF, "OV6630AF" },
294 { SEN_OV8600, "OV8600" },
295 { SEN_KS0127, "KS0127" },
296 { SEN_KS0127B, "KS0127B" },
297 { SEN_SAA7111A, "SAA7111A" },
298 { -1, NULL }
299};
300
301/* URB error codes: */
302static struct symbolic_list urb_errlist[] = {
303 { -ENOSR, "Buffer error (overrun)" },
304 { -EPIPE, "Stalled (device not responding)" },
305 { -EOVERFLOW, "Babble (bad cable?)" },
306 { -EPROTO, "Bit-stuff error (bad cable?)" },
307 { -EILSEQ, "CRC/Timeout" },
308 { -ETIMEDOUT, "NAK (device does not respond)" },
309 { -1, NULL }
310};
311
312/**********************************************************************
313 * Memory management
314 **********************************************************************/
315static void *
316rvmalloc(unsigned long size)
317{
318 void *mem;
319 unsigned long adr;
320
321 size = PAGE_ALIGN(size);
322 mem = vmalloc_32(size);
323 if (!mem)
324 return NULL;
325
326 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
327 adr = (unsigned long) mem;
328 while (size > 0) {
329 SetPageReserved(vmalloc_to_page((void *)adr));
330 adr += PAGE_SIZE;
331 size -= PAGE_SIZE;
332 }
333
334 return mem;
335}
336
337static void
338rvfree(void *mem, unsigned long size)
339{
340 unsigned long adr;
341
342 if (!mem)
343 return;
344
345 adr = (unsigned long) mem;
346 while ((long) size > 0) {
347 ClearPageReserved(vmalloc_to_page((void *)adr));
348 adr += PAGE_SIZE;
349 size -= PAGE_SIZE;
350 }
351 vfree(mem);
352}
353
354/**********************************************************************
355 *
356 * Register I/O
357 *
358 **********************************************************************/
359
360/* Write an OV51x register */
361static int
362reg_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
363{
364 int rc;
365
366 PDEBUG(5, "0x%02X:0x%02X", reg, value);
367
368 down(&ov->cbuf_lock);
369 ov->cbuf[0] = value;
370 rc = usb_control_msg(ov->dev,
371 usb_sndctrlpipe(ov->dev, 0),
372 (ov->bclass == BCL_OV518)?1:2 /* REG_IO */,
373 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
374 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
375 up(&ov->cbuf_lock);
376
377 if (rc < 0)
378 err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc));
379
380 return rc;
381}
382
383/* Read from an OV51x register */
384/* returns: negative is error, pos or zero is data */
385static int
386reg_r(struct usb_ov511 *ov, unsigned char reg)
387{
388 int rc;
389
390 down(&ov->cbuf_lock);
391 rc = usb_control_msg(ov->dev,
392 usb_rcvctrlpipe(ov->dev, 0),
393 (ov->bclass == BCL_OV518)?1:3 /* REG_IO */,
394 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
395 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
396
397 if (rc < 0) {
398 err("reg read: error %d: %s", rc, symbolic(urb_errlist, rc));
399 } else {
400 rc = ov->cbuf[0];
401 PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]);
402 }
403
404 up(&ov->cbuf_lock);
405
406 return rc;
407}
408
409/*
410 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
411 * the same position as 1's in "mask" are cleared and set to "value". Bits
412 * that are in the same position as 0's in "mask" are preserved, regardless
413 * of their respective state in "value".
414 */
415static int
416reg_w_mask(struct usb_ov511 *ov,
417 unsigned char reg,
418 unsigned char value,
419 unsigned char mask)
420{
421 int ret;
422 unsigned char oldval, newval;
423
424 ret = reg_r(ov, reg);
425 if (ret < 0)
426 return ret;
427
428 oldval = (unsigned char) ret;
429 oldval &= (~mask); /* Clear the masked bits */
430 value &= mask; /* Enforce mask on value */
431 newval = oldval | value; /* Set the desired bits */
432
433 return (reg_w(ov, reg, newval));
434}
435
436/*
437 * Writes multiple (n) byte value to a single register. Only valid with certain
438 * registers (0x30 and 0xc4 - 0xce).
439 */
440static int
441ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
442{
443 int rc;
444
445 PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n);
446
447 down(&ov->cbuf_lock);
448
449 *((__le32 *)ov->cbuf) = __cpu_to_le32(val);
450
451 rc = usb_control_msg(ov->dev,
452 usb_sndctrlpipe(ov->dev, 0),
453 1 /* REG_IO */,
454 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
455 0, (__u16)reg, ov->cbuf, n, 1000);
456 up(&ov->cbuf_lock);
457
458 if (rc < 0)
459 err("reg write multiple: error %d: %s", rc,
460 symbolic(urb_errlist, rc));
461
462 return rc;
463}
464
465static int
466ov511_upload_quan_tables(struct usb_ov511 *ov)
467{
468 unsigned char *pYTable = yQuanTable511;
469 unsigned char *pUVTable = uvQuanTable511;
470 unsigned char val0, val1;
471 int i, rc, reg = R511_COMP_LUT_BEGIN;
472
473 PDEBUG(4, "Uploading quantization tables");
474
475 for (i = 0; i < OV511_QUANTABLESIZE / 2; i++) {
476 if (ENABLE_Y_QUANTABLE) {
477 val0 = *pYTable++;
478 val1 = *pYTable++;
479 val0 &= 0x0f;
480 val1 &= 0x0f;
481 val0 |= val1 << 4;
482 rc = reg_w(ov, reg, val0);
483 if (rc < 0)
484 return rc;
485 }
486
487 if (ENABLE_UV_QUANTABLE) {
488 val0 = *pUVTable++;
489 val1 = *pUVTable++;
490 val0 &= 0x0f;
491 val1 &= 0x0f;
492 val0 |= val1 << 4;
493 rc = reg_w(ov, reg + OV511_QUANTABLESIZE/2, val0);
494 if (rc < 0)
495 return rc;
496 }
497
498 reg++;
499 }
500
501 return 0;
502}
503
504/* OV518 quantization tables are 8x4 (instead of 8x8) */
505static int
506ov518_upload_quan_tables(struct usb_ov511 *ov)
507{
508 unsigned char *pYTable = yQuanTable518;
509 unsigned char *pUVTable = uvQuanTable518;
510 unsigned char val0, val1;
511 int i, rc, reg = R511_COMP_LUT_BEGIN;
512
513 PDEBUG(4, "Uploading quantization tables");
514
515 for (i = 0; i < OV518_QUANTABLESIZE / 2; i++) {
516 if (ENABLE_Y_QUANTABLE) {
517 val0 = *pYTable++;
518 val1 = *pYTable++;
519 val0 &= 0x0f;
520 val1 &= 0x0f;
521 val0 |= val1 << 4;
522 rc = reg_w(ov, reg, val0);
523 if (rc < 0)
524 return rc;
525 }
526
527 if (ENABLE_UV_QUANTABLE) {
528 val0 = *pUVTable++;
529 val1 = *pUVTable++;
530 val0 &= 0x0f;
531 val1 &= 0x0f;
532 val0 |= val1 << 4;
533 rc = reg_w(ov, reg + OV518_QUANTABLESIZE/2, val0);
534 if (rc < 0)
535 return rc;
536 }
537
538 reg++;
539 }
540
541 return 0;
542}
543
544static int
545ov51x_reset(struct usb_ov511 *ov, unsigned char reset_type)
546{
547 int rc;
548
549 /* Setting bit 0 not allowed on 518/518Plus */
550 if (ov->bclass == BCL_OV518)
551 reset_type &= 0xfe;
552
553 PDEBUG(4, "Reset: type=0x%02X", reset_type);
554
555 rc = reg_w(ov, R51x_SYS_RESET, reset_type);
556 rc = reg_w(ov, R51x_SYS_RESET, 0);
557
558 if (rc < 0)
559 err("reset: command failed");
560
561 return rc;
562}
563
564/**********************************************************************
565 *
566 * Low-level I2C I/O functions
567 *
568 **********************************************************************/
569
570/* NOTE: Do not call this function directly!
571 * The OV518 I2C I/O procedure is different, hence, this function.
572 * This is normally only called from i2c_w(). Note that this function
573 * always succeeds regardless of whether the sensor is present and working.
574 */
575static int
576ov518_i2c_write_internal(struct usb_ov511 *ov,
577 unsigned char reg,
578 unsigned char value)
579{
580 int rc;
581
582 PDEBUG(5, "0x%02X:0x%02X", reg, value);
583
584 /* Select camera register */
585 rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
586 if (rc < 0)
587 return rc;
588
589 /* Write "value" to I2C data port of OV511 */
590 rc = reg_w(ov, R51x_I2C_DATA, value);
591 if (rc < 0)
592 return rc;
593
594 /* Initiate 3-byte write cycle */
595 rc = reg_w(ov, R518_I2C_CTL, 0x01);
596 if (rc < 0)
597 return rc;
598
599 return 0;
600}
601
602/* NOTE: Do not call this function directly! */
603static int
604ov511_i2c_write_internal(struct usb_ov511 *ov,
605 unsigned char reg,
606 unsigned char value)
607{
608 int rc, retries;
609
610 PDEBUG(5, "0x%02X:0x%02X", reg, value);
611
612 /* Three byte write cycle */
613 for (retries = OV511_I2C_RETRIES; ; ) {
614 /* Select camera register */
615 rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
616 if (rc < 0)
617 break;
618
619 /* Write "value" to I2C data port of OV511 */
620 rc = reg_w(ov, R51x_I2C_DATA, value);
621 if (rc < 0)
622 break;
623
624 /* Initiate 3-byte write cycle */
625 rc = reg_w(ov, R511_I2C_CTL, 0x01);
626 if (rc < 0)
627 break;
628
629 /* Retry until idle */
630 do
631 rc = reg_r(ov, R511_I2C_CTL);
632 while (rc > 0 && ((rc&1) == 0));
633 if (rc < 0)
634 break;
635
636 /* Ack? */
637 if ((rc&2) == 0) {
638 rc = 0;
639 break;
640 }
641#if 0
642 /* I2C abort */
643 reg_w(ov, R511_I2C_CTL, 0x10);
644#endif
645 if (--retries < 0) {
646 err("i2c write retries exhausted");
647 rc = -1;
648 break;
649 }
650 }
651
652 return rc;
653}
654
655/* NOTE: Do not call this function directly!
656 * The OV518 I2C I/O procedure is different, hence, this function.
657 * This is normally only called from i2c_r(). Note that this function
658 * always succeeds regardless of whether the sensor is present and working.
659 */
660static int
661ov518_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
662{
663 int rc, value;
664
665 /* Select camera register */
666 rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
667 if (rc < 0)
668 return rc;
669
670 /* Initiate 2-byte write cycle */
671 rc = reg_w(ov, R518_I2C_CTL, 0x03);
672 if (rc < 0)
673 return rc;
674
675 /* Initiate 2-byte read cycle */
676 rc = reg_w(ov, R518_I2C_CTL, 0x05);
677 if (rc < 0)
678 return rc;
679
680 value = reg_r(ov, R51x_I2C_DATA);
681
682 PDEBUG(5, "0x%02X:0x%02X", reg, value);
683
684 return value;
685}
686
687/* NOTE: Do not call this function directly!
688 * returns: negative is error, pos or zero is data */
689static int
690ov511_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
691{
692 int rc, value, retries;
693
694 /* Two byte write cycle */
695 for (retries = OV511_I2C_RETRIES; ; ) {
696 /* Select camera register */
697 rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
698 if (rc < 0)
699 return rc;
700
701 /* Initiate 2-byte write cycle */
702 rc = reg_w(ov, R511_I2C_CTL, 0x03);
703 if (rc < 0)
704 return rc;
705
706 /* Retry until idle */
707 do
708 rc = reg_r(ov, R511_I2C_CTL);
709 while (rc > 0 && ((rc&1) == 0));
710 if (rc < 0)
711 return rc;
712
713 if ((rc&2) == 0) /* Ack? */
714 break;
715
716 /* I2C abort */
717 reg_w(ov, R511_I2C_CTL, 0x10);
718
719 if (--retries < 0) {
720 err("i2c write retries exhausted");
721 return -1;
722 }
723 }
724
725 /* Two byte read cycle */
726 for (retries = OV511_I2C_RETRIES; ; ) {
727 /* Initiate 2-byte read cycle */
728 rc = reg_w(ov, R511_I2C_CTL, 0x05);
729 if (rc < 0)
730 return rc;
731
732 /* Retry until idle */
733 do
734 rc = reg_r(ov, R511_I2C_CTL);
735 while (rc > 0 && ((rc&1) == 0));
736 if (rc < 0)
737 return rc;
738
739 if ((rc&2) == 0) /* Ack? */
740 break;
741
742 /* I2C abort */
743 rc = reg_w(ov, R511_I2C_CTL, 0x10);
744 if (rc < 0)
745 return rc;
746
747 if (--retries < 0) {
748 err("i2c read retries exhausted");
749 return -1;
750 }
751 }
752
753 value = reg_r(ov, R51x_I2C_DATA);
754
755 PDEBUG(5, "0x%02X:0x%02X", reg, value);
756
757 /* This is needed to make i2c_w() work */
758 rc = reg_w(ov, R511_I2C_CTL, 0x05);
759 if (rc < 0)
760 return rc;
761
762 return value;
763}
764
765/* returns: negative is error, pos or zero is data */
766static int
767i2c_r(struct usb_ov511 *ov, unsigned char reg)
768{
769 int rc;
770
771 down(&ov->i2c_lock);
772
773 if (ov->bclass == BCL_OV518)
774 rc = ov518_i2c_read_internal(ov, reg);
775 else
776 rc = ov511_i2c_read_internal(ov, reg);
777
778 up(&ov->i2c_lock);
779
780 return rc;
781}
782
783static int
784i2c_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
785{
786 int rc;
787
788 down(&ov->i2c_lock);
789
790 if (ov->bclass == BCL_OV518)
791 rc = ov518_i2c_write_internal(ov, reg, value);
792 else
793 rc = ov511_i2c_write_internal(ov, reg, value);
794
795 up(&ov->i2c_lock);
796
797 return rc;
798}
799
800/* Do not call this function directly! */
801static int
802ov51x_i2c_write_mask_internal(struct usb_ov511 *ov,
803 unsigned char reg,
804 unsigned char value,
805 unsigned char mask)
806{
807 int rc;
808 unsigned char oldval, newval;
809
810 if (mask == 0xff) {
811 newval = value;
812 } else {
813 if (ov->bclass == BCL_OV518)
814 rc = ov518_i2c_read_internal(ov, reg);
815 else
816 rc = ov511_i2c_read_internal(ov, reg);
817 if (rc < 0)
818 return rc;
819
820 oldval = (unsigned char) rc;
821 oldval &= (~mask); /* Clear the masked bits */
822 value &= mask; /* Enforce mask on value */
823 newval = oldval | value; /* Set the desired bits */
824 }
825
826 if (ov->bclass == BCL_OV518)
827 return (ov518_i2c_write_internal(ov, reg, newval));
828 else
829 return (ov511_i2c_write_internal(ov, reg, newval));
830}
831
832/* Writes bits at positions specified by mask to an I2C reg. Bits that are in
833 * the same position as 1's in "mask" are cleared and set to "value". Bits
834 * that are in the same position as 0's in "mask" are preserved, regardless
835 * of their respective state in "value".
836 */
837static int
838i2c_w_mask(struct usb_ov511 *ov,
839 unsigned char reg,
840 unsigned char value,
841 unsigned char mask)
842{
843 int rc;
844
845 down(&ov->i2c_lock);
846 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
847 up(&ov->i2c_lock);
848
849 return rc;
850}
851
852/* Set the read and write slave IDs. The "slave" argument is the write slave,
853 * and the read slave will be set to (slave + 1). ov->i2c_lock should be held
854 * when calling this. This should not be called from outside the i2c I/O
855 * functions.
856 */
857static int
858i2c_set_slave_internal(struct usb_ov511 *ov, unsigned char slave)
859{
860 int rc;
861
862 rc = reg_w(ov, R51x_I2C_W_SID, slave);
863 if (rc < 0)
864 return rc;
865
866 rc = reg_w(ov, R51x_I2C_R_SID, slave + 1);
867 if (rc < 0)
868 return rc;
869
870 return 0;
871}
872
873/* Write to a specific I2C slave ID and register, using the specified mask */
874static int
875i2c_w_slave(struct usb_ov511 *ov,
876 unsigned char slave,
877 unsigned char reg,
878 unsigned char value,
879 unsigned char mask)
880{
881 int rc = 0;
882
883 down(&ov->i2c_lock);
884
885 /* Set new slave IDs */
886 rc = i2c_set_slave_internal(ov, slave);
887 if (rc < 0)
888 goto out;
889
890 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
891
892out:
893 /* Restore primary IDs */
894 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
895 err("Couldn't restore primary I2C slave");
896
897 up(&ov->i2c_lock);
898 return rc;
899}
900
901/* Read from a specific I2C slave ID and register */
902static int
903i2c_r_slave(struct usb_ov511 *ov,
904 unsigned char slave,
905 unsigned char reg)
906{
907 int rc;
908
909 down(&ov->i2c_lock);
910
911 /* Set new slave IDs */
912 rc = i2c_set_slave_internal(ov, slave);
913 if (rc < 0)
914 goto out;
915
916 if (ov->bclass == BCL_OV518)
917 rc = ov518_i2c_read_internal(ov, reg);
918 else
919 rc = ov511_i2c_read_internal(ov, reg);
920
921out:
922 /* Restore primary IDs */
923 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
924 err("Couldn't restore primary I2C slave");
925
926 up(&ov->i2c_lock);
927 return rc;
928}
929
930/* Sets I2C read and write slave IDs. Returns <0 for error */
931static int
932ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
933{
934 int rc;
935
936 down(&ov->i2c_lock);
937
938 rc = i2c_set_slave_internal(ov, sid);
939 if (rc < 0)
940 goto out;
941
942 // FIXME: Is this actually necessary?
943 rc = ov51x_reset(ov, OV511_RESET_NOREGS);
944out:
945 up(&ov->i2c_lock);
946 return rc;
947}
948
949static int
950write_regvals(struct usb_ov511 *ov, struct ov511_regvals * pRegvals)
951{
952 int rc;
953
954 while (pRegvals->bus != OV511_DONE_BUS) {
955 if (pRegvals->bus == OV511_REG_BUS) {
956 if ((rc = reg_w(ov, pRegvals->reg, pRegvals->val)) < 0)
957 return rc;
958 } else if (pRegvals->bus == OV511_I2C_BUS) {
959 if ((rc = i2c_w(ov, pRegvals->reg, pRegvals->val)) < 0)
960 return rc;
961 } else {
962 err("Bad regval array");
963 return -1;
964 }
965 pRegvals++;
966 }
967 return 0;
968}
969
970#ifdef OV511_DEBUG
971static void
972dump_i2c_range(struct usb_ov511 *ov, int reg1, int regn)
973{
974 int i, rc;
975
976 for (i = reg1; i <= regn; i++) {
977 rc = i2c_r(ov, i);
978 info("Sensor[0x%02X] = 0x%02X", i, rc);
979 }
980}
981
982static void
983dump_i2c_regs(struct usb_ov511 *ov)
984{
985 info("I2C REGS");
986 dump_i2c_range(ov, 0x00, 0x7C);
987}
988
989static void
990dump_reg_range(struct usb_ov511 *ov, int reg1, int regn)
991{
992 int i, rc;
993
994 for (i = reg1; i <= regn; i++) {
995 rc = reg_r(ov, i);
996 info("OV511[0x%02X] = 0x%02X", i, rc);
997 }
998}
999
1000static void
1001ov511_dump_regs(struct usb_ov511 *ov)
1002{
1003 info("CAMERA INTERFACE REGS");
1004 dump_reg_range(ov, 0x10, 0x1f);
1005 info("DRAM INTERFACE REGS");
1006 dump_reg_range(ov, 0x20, 0x23);
1007 info("ISO FIFO REGS");
1008 dump_reg_range(ov, 0x30, 0x31);
1009 info("PIO REGS");
1010 dump_reg_range(ov, 0x38, 0x39);
1011 dump_reg_range(ov, 0x3e, 0x3e);
1012 info("I2C REGS");
1013 dump_reg_range(ov, 0x40, 0x49);
1014 info("SYSTEM CONTROL REGS");
1015 dump_reg_range(ov, 0x50, 0x55);
1016 dump_reg_range(ov, 0x5e, 0x5f);
1017 info("OmniCE REGS");
1018 dump_reg_range(ov, 0x70, 0x79);
1019 /* NOTE: Quantization tables are not readable. You will get the value
1020 * in reg. 0x79 for every table register */
1021 dump_reg_range(ov, 0x80, 0x9f);
1022 dump_reg_range(ov, 0xa0, 0xbf);
1023
1024}
1025
1026static void
1027ov518_dump_regs(struct usb_ov511 *ov)
1028{
1029 info("VIDEO MODE REGS");
1030 dump_reg_range(ov, 0x20, 0x2f);
1031 info("DATA PUMP AND SNAPSHOT REGS");
1032 dump_reg_range(ov, 0x30, 0x3f);
1033 info("I2C REGS");
1034 dump_reg_range(ov, 0x40, 0x4f);
1035 info("SYSTEM CONTROL AND VENDOR REGS");
1036 dump_reg_range(ov, 0x50, 0x5f);
1037 info("60 - 6F");
1038 dump_reg_range(ov, 0x60, 0x6f);
1039 info("70 - 7F");
1040 dump_reg_range(ov, 0x70, 0x7f);
1041 info("Y QUANTIZATION TABLE");
1042 dump_reg_range(ov, 0x80, 0x8f);
1043 info("UV QUANTIZATION TABLE");
1044 dump_reg_range(ov, 0x90, 0x9f);
1045 info("A0 - BF");
1046 dump_reg_range(ov, 0xa0, 0xbf);
1047 info("CBR");
1048 dump_reg_range(ov, 0xc0, 0xcf);
1049}
1050#endif
1051
1052/*****************************************************************************/
1053
1054/* Temporarily stops OV511 from functioning. Must do this before changing
1055 * registers while the camera is streaming */
1056static inline int
1057ov51x_stop(struct usb_ov511 *ov)
1058{
1059 PDEBUG(4, "stopping");
1060 ov->stopped = 1;
1061 if (ov->bclass == BCL_OV518)
1062 return (reg_w_mask(ov, R51x_SYS_RESET, 0x3a, 0x3a));
1063 else
1064 return (reg_w(ov, R51x_SYS_RESET, 0x3d));
1065}
1066
1067/* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
1068 * actually stopped (for performance). */
1069static inline int
1070ov51x_restart(struct usb_ov511 *ov)
1071{
1072 if (ov->stopped) {
1073 PDEBUG(4, "restarting");
1074 ov->stopped = 0;
1075
1076 /* Reinitialize the stream */
1077 if (ov->bclass == BCL_OV518)
1078 reg_w(ov, 0x2f, 0x80);
1079
1080 return (reg_w(ov, R51x_SYS_RESET, 0x00));
1081 }
1082
1083 return 0;
1084}
1085
1086/* Sleeps until no frames are active. Returns !0 if got signal */
1087static int
1088ov51x_wait_frames_inactive(struct usb_ov511 *ov)
1089{
1090 return wait_event_interruptible(ov->wq, ov->curframe < 0);
1091}
1092
1093/* Resets the hardware snapshot button */
1094static void
1095ov51x_clear_snapshot(struct usb_ov511 *ov)
1096{
1097 if (ov->bclass == BCL_OV511) {
1098 reg_w(ov, R51x_SYS_SNAP, 0x00);
1099 reg_w(ov, R51x_SYS_SNAP, 0x02);
1100 reg_w(ov, R51x_SYS_SNAP, 0x00);
1101 } else if (ov->bclass == BCL_OV518) {
1102 warn("snapshot reset not supported yet on OV518(+)");
1103 } else {
1104 err("clear snap: invalid bridge type");
1105 }
1106}
1107
1108#if 0
1109/* Checks the status of the snapshot button. Returns 1 if it was pressed since
1110 * it was last cleared, and zero in all other cases (including errors) */
1111static int
1112ov51x_check_snapshot(struct usb_ov511 *ov)
1113{
1114 int ret, status = 0;
1115
1116 if (ov->bclass == BCL_OV511) {
1117 ret = reg_r(ov, R51x_SYS_SNAP);
1118 if (ret < 0) {
1119 err("Error checking snspshot status (%d)", ret);
1120 } else if (ret & 0x08) {
1121 status = 1;
1122 }
1123 } else if (ov->bclass == BCL_OV518) {
1124 warn("snapshot check not supported yet on OV518(+)");
1125 } else {
1126 err("check snap: invalid bridge type");
1127 }
1128
1129 return status;
1130}
1131#endif
1132
1133/* This does an initial reset of an OmniVision sensor and ensures that I2C
1134 * is synchronized. Returns <0 for failure.
1135 */
1136static int
1137init_ov_sensor(struct usb_ov511 *ov)
1138{
1139 int i, success;
1140
1141 /* Reset the sensor */
1142 if (i2c_w(ov, 0x12, 0x80) < 0)
1143 return -EIO;
1144
1145 /* Wait for it to initialize */
1146 msleep(150);
1147
1148 for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
1149 if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
1150 (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
1151 success = 1;
1152 continue;
1153 }
1154
1155 /* Reset the sensor */
1156 if (i2c_w(ov, 0x12, 0x80) < 0)
1157 return -EIO;
1158 /* Wait for it to initialize */
1159 msleep(150);
1160 /* Dummy read to sync I2C */
1161 if (i2c_r(ov, 0x00) < 0)
1162 return -EIO;
1163 }
1164
1165 if (!success)
1166 return -EIO;
1167
1168 PDEBUG(1, "I2C synced in %d attempt(s)", i);
1169
1170 return 0;
1171}
1172
1173static int
1174ov511_set_packet_size(struct usb_ov511 *ov, int size)
1175{
1176 int alt, mult;
1177
1178 if (ov51x_stop(ov) < 0)
1179 return -EIO;
1180
1181 mult = size >> 5;
1182
1183 if (ov->bridge == BRG_OV511) {
1184 if (size == 0)
1185 alt = OV511_ALT_SIZE_0;
1186 else if (size == 257)
1187 alt = OV511_ALT_SIZE_257;
1188 else if (size == 513)
1189 alt = OV511_ALT_SIZE_513;
1190 else if (size == 769)
1191 alt = OV511_ALT_SIZE_769;
1192 else if (size == 993)
1193 alt = OV511_ALT_SIZE_993;
1194 else {
1195 err("Set packet size: invalid size (%d)", size);
1196 return -EINVAL;
1197 }
1198 } else if (ov->bridge == BRG_OV511PLUS) {
1199 if (size == 0)
1200 alt = OV511PLUS_ALT_SIZE_0;
1201 else if (size == 33)
1202 alt = OV511PLUS_ALT_SIZE_33;
1203 else if (size == 129)
1204 alt = OV511PLUS_ALT_SIZE_129;
1205 else if (size == 257)
1206 alt = OV511PLUS_ALT_SIZE_257;
1207 else if (size == 385)
1208 alt = OV511PLUS_ALT_SIZE_385;
1209 else if (size == 513)
1210 alt = OV511PLUS_ALT_SIZE_513;
1211 else if (size == 769)
1212 alt = OV511PLUS_ALT_SIZE_769;
1213 else if (size == 961)
1214 alt = OV511PLUS_ALT_SIZE_961;
1215 else {
1216 err("Set packet size: invalid size (%d)", size);
1217 return -EINVAL;
1218 }
1219 } else {
1220 err("Set packet size: Invalid bridge type");
1221 return -EINVAL;
1222 }
1223
1224 PDEBUG(3, "%d, mult=%d, alt=%d", size, mult, alt);
1225
1226 if (reg_w(ov, R51x_FIFO_PSIZE, mult) < 0)
1227 return -EIO;
1228
1229 if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
1230 err("Set packet size: set interface error");
1231 return -EBUSY;
1232 }
1233
1234 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
1235 return -EIO;
1236
1237 ov->packet_size = size;
1238
1239 if (ov51x_restart(ov) < 0)
1240 return -EIO;
1241
1242 return 0;
1243}
1244
1245/* Note: Unlike the OV511/OV511+, the size argument does NOT include the
1246 * optional packet number byte. The actual size *is* stored in ov->packet_size,
1247 * though. */
1248static int
1249ov518_set_packet_size(struct usb_ov511 *ov, int size)
1250{
1251 int alt;
1252
1253 if (ov51x_stop(ov) < 0)
1254 return -EIO;
1255
1256 if (ov->bclass == BCL_OV518) {
1257 if (size == 0)
1258 alt = OV518_ALT_SIZE_0;
1259 else if (size == 128)
1260 alt = OV518_ALT_SIZE_128;
1261 else if (size == 256)
1262 alt = OV518_ALT_SIZE_256;
1263 else if (size == 384)
1264 alt = OV518_ALT_SIZE_384;
1265 else if (size == 512)
1266 alt = OV518_ALT_SIZE_512;
1267 else if (size == 640)
1268 alt = OV518_ALT_SIZE_640;
1269 else if (size == 768)
1270 alt = OV518_ALT_SIZE_768;
1271 else if (size == 896)
1272 alt = OV518_ALT_SIZE_896;
1273 else {
1274 err("Set packet size: invalid size (%d)", size);
1275 return -EINVAL;
1276 }
1277 } else {
1278 err("Set packet size: Invalid bridge type");
1279 return -EINVAL;
1280 }
1281
1282 PDEBUG(3, "%d, alt=%d", size, alt);
1283
1284 ov->packet_size = size;
1285 if (size > 0) {
1286 /* Program ISO FIFO size reg (packet number isn't included) */
1287 ov518_reg_w32(ov, 0x30, size, 2);
1288
1289 if (ov->packet_numbering)
1290 ++ov->packet_size;
1291 }
1292
1293 if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
1294 err("Set packet size: set interface error");
1295 return -EBUSY;
1296 }
1297
1298 /* Initialize the stream */
1299 if (reg_w(ov, 0x2f, 0x80) < 0)
1300 return -EIO;
1301
1302 if (ov51x_restart(ov) < 0)
1303 return -EIO;
1304
1305 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
1306 return -EIO;
1307
1308 return 0;
1309}
1310
1311/* Upload compression params and quantization tables. Returns 0 for success. */
1312static int
1313ov511_init_compression(struct usb_ov511 *ov)
1314{
1315 int rc = 0;
1316
1317 if (!ov->compress_inited) {
1318 reg_w(ov, 0x70, phy);
1319 reg_w(ov, 0x71, phuv);
1320 reg_w(ov, 0x72, pvy);
1321 reg_w(ov, 0x73, pvuv);
1322 reg_w(ov, 0x74, qhy);
1323 reg_w(ov, 0x75, qhuv);
1324 reg_w(ov, 0x76, qvy);
1325 reg_w(ov, 0x77, qvuv);
1326
1327 if (ov511_upload_quan_tables(ov) < 0) {
1328 err("Error uploading quantization tables");
1329 rc = -EIO;
1330 goto out;
1331 }
1332 }
1333
1334 ov->compress_inited = 1;
1335out:
1336 return rc;
1337}
1338
1339/* Upload compression params and quantization tables. Returns 0 for success. */
1340static int
1341ov518_init_compression(struct usb_ov511 *ov)
1342{
1343 int rc = 0;
1344
1345 if (!ov->compress_inited) {
1346 if (ov518_upload_quan_tables(ov) < 0) {
1347 err("Error uploading quantization tables");
1348 rc = -EIO;
1349 goto out;
1350 }
1351 }
1352
1353 ov->compress_inited = 1;
1354out:
1355 return rc;
1356}
1357
1358/* -------------------------------------------------------------------------- */
1359
1360/* Sets sensor's contrast setting to "val" */
1361static int
1362sensor_set_contrast(struct usb_ov511 *ov, unsigned short val)
1363{
1364 int rc;
1365
1366 PDEBUG(3, "%d", val);
1367
1368 if (ov->stop_during_set)
1369 if (ov51x_stop(ov) < 0)
1370 return -EIO;
1371
1372 switch (ov->sensor) {
1373 case SEN_OV7610:
1374 case SEN_OV6620:
1375 {
1376 rc = i2c_w(ov, OV7610_REG_CNT, val >> 8);
1377 if (rc < 0)
1378 goto out;
1379 break;
1380 }
1381 case SEN_OV6630:
1382 {
1383 rc = i2c_w_mask(ov, OV7610_REG_CNT, val >> 12, 0x0f);
1384 if (rc < 0)
1385 goto out;
1386 break;
1387 }
1388 case SEN_OV7620:
1389 {
1390 unsigned char ctab[] = {
1391 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
1392 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
1393 };
1394
1395 /* Use Y gamma control instead. Bit 0 enables it. */
1396 rc = i2c_w(ov, 0x64, ctab[val>>12]);
1397 if (rc < 0)
1398 goto out;
1399 break;
1400 }
1401 case SEN_SAA7111A:
1402 {
1403 rc = i2c_w(ov, 0x0b, val >> 9);
1404 if (rc < 0)
1405 goto out;
1406 break;
1407 }
1408 default:
1409 {
1410 PDEBUG(3, "Unsupported with this sensor");
1411 rc = -EPERM;
1412 goto out;
1413 }
1414 }
1415
1416 rc = 0; /* Success */
1417 ov->contrast = val;
1418out:
1419 if (ov51x_restart(ov) < 0)
1420 return -EIO;
1421
1422 return rc;
1423}
1424
1425/* Gets sensor's contrast setting */
1426static int
1427sensor_get_contrast(struct usb_ov511 *ov, unsigned short *val)
1428{
1429 int rc;
1430
1431 switch (ov->sensor) {
1432 case SEN_OV7610:
1433 case SEN_OV6620:
1434 rc = i2c_r(ov, OV7610_REG_CNT);
1435 if (rc < 0)
1436 return rc;
1437 else
1438 *val = rc << 8;
1439 break;
1440 case SEN_OV6630:
1441 rc = i2c_r(ov, OV7610_REG_CNT);
1442 if (rc < 0)
1443 return rc;
1444 else
1445 *val = rc << 12;
1446 break;
1447 case SEN_OV7620:
1448 /* Use Y gamma reg instead. Bit 0 is the enable bit. */
1449 rc = i2c_r(ov, 0x64);
1450 if (rc < 0)
1451 return rc;
1452 else
1453 *val = (rc & 0xfe) << 8;
1454 break;
1455 case SEN_SAA7111A:
1456 *val = ov->contrast;
1457 break;
1458 default:
1459 PDEBUG(3, "Unsupported with this sensor");
1460 return -EPERM;
1461 }
1462
1463 PDEBUG(3, "%d", *val);
1464 ov->contrast = *val;
1465
1466 return 0;
1467}
1468
1469/* -------------------------------------------------------------------------- */
1470
1471/* Sets sensor's brightness setting to "val" */
1472static int
1473sensor_set_brightness(struct usb_ov511 *ov, unsigned short val)
1474{
1475 int rc;
1476
1477 PDEBUG(4, "%d", val);
1478
1479 if (ov->stop_during_set)
1480 if (ov51x_stop(ov) < 0)
1481 return -EIO;
1482
1483 switch (ov->sensor) {
1484 case SEN_OV7610:
1485 case SEN_OV76BE:
1486 case SEN_OV6620:
1487 case SEN_OV6630:
1488 rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
1489 if (rc < 0)
1490 goto out;
1491 break;
1492 case SEN_OV7620:
1493 /* 7620 doesn't like manual changes when in auto mode */
1494 if (!ov->auto_brt) {
1495 rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
1496 if (rc < 0)
1497 goto out;
1498 }
1499 break;
1500 case SEN_SAA7111A:
1501 rc = i2c_w(ov, 0x0a, val >> 8);
1502 if (rc < 0)
1503 goto out;
1504 break;
1505 default:
1506 PDEBUG(3, "Unsupported with this sensor");
1507 rc = -EPERM;
1508 goto out;
1509 }
1510
1511 rc = 0; /* Success */
1512 ov->brightness = val;
1513out:
1514 if (ov51x_restart(ov) < 0)
1515 return -EIO;
1516
1517 return rc;
1518}
1519
1520/* Gets sensor's brightness setting */
1521static int
1522sensor_get_brightness(struct usb_ov511 *ov, unsigned short *val)
1523{
1524 int rc;
1525
1526 switch (ov->sensor) {
1527 case SEN_OV7610:
1528 case SEN_OV76BE:
1529 case SEN_OV7620:
1530 case SEN_OV6620:
1531 case SEN_OV6630:
1532 rc = i2c_r(ov, OV7610_REG_BRT);
1533 if (rc < 0)
1534 return rc;
1535 else
1536 *val = rc << 8;
1537 break;
1538 case SEN_SAA7111A:
1539 *val = ov->brightness;
1540 break;
1541 default:
1542 PDEBUG(3, "Unsupported with this sensor");
1543 return -EPERM;
1544 }
1545
1546 PDEBUG(3, "%d", *val);
1547 ov->brightness = *val;
1548
1549 return 0;
1550}
1551
1552/* -------------------------------------------------------------------------- */
1553
1554/* Sets sensor's saturation (color intensity) setting to "val" */
1555static int
1556sensor_set_saturation(struct usb_ov511 *ov, unsigned short val)
1557{
1558 int rc;
1559
1560 PDEBUG(3, "%d", val);
1561
1562 if (ov->stop_during_set)
1563 if (ov51x_stop(ov) < 0)
1564 return -EIO;
1565
1566 switch (ov->sensor) {
1567 case SEN_OV7610:
1568 case SEN_OV76BE:
1569 case SEN_OV6620:
1570 case SEN_OV6630:
1571 rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
1572 if (rc < 0)
1573 goto out;
1574 break;
1575 case SEN_OV7620:
1576// /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
1577// rc = ov_i2c_write(ov->dev, 0x62, (val >> 9) & 0x7e);
1578// if (rc < 0)
1579// goto out;
1580 rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
1581 if (rc < 0)
1582 goto out;
1583 break;
1584 case SEN_SAA7111A:
1585 rc = i2c_w(ov, 0x0c, val >> 9);
1586 if (rc < 0)
1587 goto out;
1588 break;
1589 default:
1590 PDEBUG(3, "Unsupported with this sensor");
1591 rc = -EPERM;
1592 goto out;
1593 }
1594
1595 rc = 0; /* Success */
1596 ov->colour = val;
1597out:
1598 if (ov51x_restart(ov) < 0)
1599 return -EIO;
1600
1601 return rc;
1602}
1603
1604/* Gets sensor's saturation (color intensity) setting */
1605static int
1606sensor_get_saturation(struct usb_ov511 *ov, unsigned short *val)
1607{
1608 int rc;
1609
1610 switch (ov->sensor) {
1611 case SEN_OV7610:
1612 case SEN_OV76BE:
1613 case SEN_OV6620:
1614 case SEN_OV6630:
1615 rc = i2c_r(ov, OV7610_REG_SAT);
1616 if (rc < 0)
1617 return rc;
1618 else
1619 *val = rc << 8;
1620 break;
1621 case SEN_OV7620:
1622// /* Use UV gamma reg instead. Bits 0 & 7 are reserved. */
1623// rc = i2c_r(ov, 0x62);
1624// if (rc < 0)
1625// return rc;
1626// else
1627// *val = (rc & 0x7e) << 9;
1628 rc = i2c_r(ov, OV7610_REG_SAT);
1629 if (rc < 0)
1630 return rc;
1631 else
1632 *val = rc << 8;
1633 break;
1634 case SEN_SAA7111A:
1635 *val = ov->colour;
1636 break;
1637 default:
1638 PDEBUG(3, "Unsupported with this sensor");
1639 return -EPERM;
1640 }
1641
1642 PDEBUG(3, "%d", *val);
1643 ov->colour = *val;
1644
1645 return 0;
1646}
1647
1648/* -------------------------------------------------------------------------- */
1649
1650/* Sets sensor's hue (red/blue balance) setting to "val" */
1651static int
1652sensor_set_hue(struct usb_ov511 *ov, unsigned short val)
1653{
1654 int rc;
1655
1656 PDEBUG(3, "%d", val);
1657
1658 if (ov->stop_during_set)
1659 if (ov51x_stop(ov) < 0)
1660 return -EIO;
1661
1662 switch (ov->sensor) {
1663 case SEN_OV7610:
1664 case SEN_OV6620:
1665 case SEN_OV6630:
1666 rc = i2c_w(ov, OV7610_REG_RED, 0xFF - (val >> 8));
1667 if (rc < 0)
1668 goto out;
1669
1670 rc = i2c_w(ov, OV7610_REG_BLUE, val >> 8);
1671 if (rc < 0)
1672 goto out;
1673 break;
1674 case SEN_OV7620:
1675// Hue control is causing problems. I will enable it once it's fixed.
1676#if 0
1677 rc = i2c_w(ov, 0x7a, (unsigned char)(val >> 8) + 0xb);
1678 if (rc < 0)
1679 goto out;
1680
1681 rc = i2c_w(ov, 0x79, (unsigned char)(val >> 8) + 0xb);
1682 if (rc < 0)
1683 goto out;
1684#endif
1685 break;
1686 case SEN_SAA7111A:
1687 rc = i2c_w(ov, 0x0d, (val + 32768) >> 8);
1688 if (rc < 0)
1689 goto out;
1690 break;
1691 default:
1692 PDEBUG(3, "Unsupported with this sensor");
1693 rc = -EPERM;
1694 goto out;
1695 }
1696
1697 rc = 0; /* Success */
1698 ov->hue = val;
1699out:
1700 if (ov51x_restart(ov) < 0)
1701 return -EIO;
1702
1703 return rc;
1704}
1705
1706/* Gets sensor's hue (red/blue balance) setting */
1707static int
1708sensor_get_hue(struct usb_ov511 *ov, unsigned short *val)
1709{
1710 int rc;
1711
1712 switch (ov->sensor) {
1713 case SEN_OV7610:
1714 case SEN_OV6620:
1715 case SEN_OV6630:
1716 rc = i2c_r(ov, OV7610_REG_BLUE);
1717 if (rc < 0)
1718 return rc;
1719 else
1720 *val = rc << 8;
1721 break;
1722 case SEN_OV7620:
1723 rc = i2c_r(ov, 0x7a);
1724 if (rc < 0)
1725 return rc;
1726 else
1727 *val = rc << 8;
1728 break;
1729 case SEN_SAA7111A:
1730 *val = ov->hue;
1731 break;
1732 default:
1733 PDEBUG(3, "Unsupported with this sensor");
1734 return -EPERM;
1735 }
1736
1737 PDEBUG(3, "%d", *val);
1738 ov->hue = *val;
1739
1740 return 0;
1741}
1742
1743/* -------------------------------------------------------------------------- */
1744
1745static int
1746sensor_set_picture(struct usb_ov511 *ov, struct video_picture *p)
1747{
1748 int rc;
1749
1750 PDEBUG(4, "sensor_set_picture");
1751
1752 ov->whiteness = p->whiteness;
1753
1754 /* Don't return error if a setting is unsupported, or rest of settings
1755 * will not be performed */
1756
1757 rc = sensor_set_contrast(ov, p->contrast);
1758 if (FATAL_ERROR(rc))
1759 return rc;
1760
1761 rc = sensor_set_brightness(ov, p->brightness);
1762 if (FATAL_ERROR(rc))
1763 return rc;
1764
1765 rc = sensor_set_saturation(ov, p->colour);
1766 if (FATAL_ERROR(rc))
1767 return rc;
1768
1769 rc = sensor_set_hue(ov, p->hue);
1770 if (FATAL_ERROR(rc))
1771 return rc;
1772
1773 return 0;
1774}
1775
1776static int
1777sensor_get_picture(struct usb_ov511 *ov, struct video_picture *p)
1778{
1779 int rc;
1780
1781 PDEBUG(4, "sensor_get_picture");
1782
1783 /* Don't return error if a setting is unsupported, or rest of settings
1784 * will not be performed */
1785
1786 rc = sensor_get_contrast(ov, &(p->contrast));
1787 if (FATAL_ERROR(rc))
1788 return rc;
1789
1790 rc = sensor_get_brightness(ov, &(p->brightness));
1791 if (FATAL_ERROR(rc))
1792 return rc;
1793
1794 rc = sensor_get_saturation(ov, &(p->colour));
1795 if (FATAL_ERROR(rc))
1796 return rc;
1797
1798 rc = sensor_get_hue(ov, &(p->hue));
1799 if (FATAL_ERROR(rc))
1800 return rc;
1801
1802 p->whiteness = 105 << 8;
1803
1804 return 0;
1805}
1806
1807#if 0
1808// FIXME: Exposure range is only 0x00-0x7f in interlace mode
1809/* Sets current exposure for sensor. This only has an effect if auto-exposure
1810 * is off */
1811static inline int
1812sensor_set_exposure(struct usb_ov511 *ov, unsigned char val)
1813{
1814 int rc;
1815
1816 PDEBUG(3, "%d", val);
1817
1818 if (ov->stop_during_set)
1819 if (ov51x_stop(ov) < 0)
1820 return -EIO;
1821
1822 switch (ov->sensor) {
1823 case SEN_OV6620:
1824 case SEN_OV6630:
1825 case SEN_OV7610:
1826 case SEN_OV7620:
1827 case SEN_OV76BE:
1828 case SEN_OV8600:
1829 rc = i2c_w(ov, 0x10, val);
1830 if (rc < 0)
1831 goto out;
1832
1833 break;
1834 case SEN_KS0127:
1835 case SEN_KS0127B:
1836 case SEN_SAA7111A:
1837 PDEBUG(3, "Unsupported with this sensor");
1838 return -EPERM;
1839 default:
1840 err("Sensor not supported for set_exposure");
1841 return -EINVAL;
1842 }
1843
1844 rc = 0; /* Success */
1845 ov->exposure = val;
1846out:
1847 if (ov51x_restart(ov) < 0)
1848 return -EIO;
1849
1850 return rc;
1851}
1852#endif
1853
1854/* Gets current exposure level from sensor, regardless of whether it is under
1855 * manual control. */
1856static int
1857sensor_get_exposure(struct usb_ov511 *ov, unsigned char *val)
1858{
1859 int rc;
1860
1861 switch (ov->sensor) {
1862 case SEN_OV7610:
1863 case SEN_OV6620:
1864 case SEN_OV6630:
1865 case SEN_OV7620:
1866 case SEN_OV76BE:
1867 case SEN_OV8600:
1868 rc = i2c_r(ov, 0x10);
1869 if (rc < 0)
1870 return rc;
1871 else
1872 *val = rc;
1873 break;
1874 case SEN_KS0127:
1875 case SEN_KS0127B:
1876 case SEN_SAA7111A:
1877 val = NULL;
1878 PDEBUG(3, "Unsupported with this sensor");
1879 return -EPERM;
1880 default:
1881 err("Sensor not supported for get_exposure");
1882 return -EINVAL;
1883 }
1884
1885 PDEBUG(3, "%d", *val);
1886 ov->exposure = *val;
1887
1888 return 0;
1889}
1890
1891/* Turns on or off the LED. Only has an effect with OV511+/OV518(+) */
1892static void
1893ov51x_led_control(struct usb_ov511 *ov, int enable)
1894{
1895 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
1896
1897 if (ov->bridge == BRG_OV511PLUS)
1898 reg_w(ov, R511_SYS_LED_CTL, enable ? 1 : 0);
1899 else if (ov->bclass == BCL_OV518)
1900 reg_w_mask(ov, R518_GPIO_OUT, enable ? 0x02 : 0x00, 0x02);
1901
1902 return;
1903}
1904
1905/* Matches the sensor's internal frame rate to the lighting frequency.
1906 * Valid frequencies are:
1907 * 50 - 50Hz, for European and Asian lighting
1908 * 60 - 60Hz, for American lighting
1909 *
1910 * Tested with: OV7610, OV7620, OV76BE, OV6620
1911 * Unsupported: KS0127, KS0127B, SAA7111A
1912 * Returns: 0 for success
1913 */
1914static int
1915sensor_set_light_freq(struct usb_ov511 *ov, int freq)
1916{
1917 int sixty;
1918
1919 PDEBUG(4, "%d Hz", freq);
1920
1921 if (freq == 60)
1922 sixty = 1;
1923 else if (freq == 50)
1924 sixty = 0;
1925 else {
1926 err("Invalid light freq (%d Hz)", freq);
1927 return -EINVAL;
1928 }
1929
1930 switch (ov->sensor) {
1931 case SEN_OV7610:
1932 i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
1933 i2c_w(ov, 0x2b, sixty?0x00:0xac);
1934 i2c_w_mask(ov, 0x13, 0x10, 0x10);
1935 i2c_w_mask(ov, 0x13, 0x00, 0x10);
1936 break;
1937 case SEN_OV7620:
1938 case SEN_OV76BE:
1939 case SEN_OV8600:
1940 i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
1941 i2c_w(ov, 0x2b, sixty?0x00:0xac);
1942 i2c_w_mask(ov, 0x76, 0x01, 0x01);
1943 break;
1944 case SEN_OV6620:
1945 case SEN_OV6630:
1946 i2c_w(ov, 0x2b, sixty?0xa8:0x28);
1947 i2c_w(ov, 0x2a, sixty?0x84:0xa4);
1948 break;
1949 case SEN_KS0127:
1950 case SEN_KS0127B:
1951 case SEN_SAA7111A:
1952 PDEBUG(5, "Unsupported with this sensor");
1953 return -EPERM;
1954 default:
1955 err("Sensor not supported for set_light_freq");
1956 return -EINVAL;
1957 }
1958
1959 ov->lightfreq = freq;
1960
1961 return 0;
1962}
1963
1964/* If enable is true, turn on the sensor's banding filter, otherwise turn it
1965 * off. This filter tries to reduce the pattern of horizontal light/dark bands
1966 * caused by some (usually fluorescent) lighting. The light frequency must be
1967 * set either before or after enabling it with ov51x_set_light_freq().
1968 *
1969 * Tested with: OV7610, OV7620, OV76BE, OV6620.
1970 * Unsupported: KS0127, KS0127B, SAA7111A
1971 * Returns: 0 for success
1972 */
1973static int
1974sensor_set_banding_filter(struct usb_ov511 *ov, int enable)
1975{
1976 int rc;
1977
1978 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
1979
1980 if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
1981 || ov->sensor == SEN_SAA7111A) {
1982 PDEBUG(5, "Unsupported with this sensor");
1983 return -EPERM;
1984 }
1985
1986 rc = i2c_w_mask(ov, 0x2d, enable?0x04:0x00, 0x04);
1987 if (rc < 0)
1988 return rc;
1989
1990 ov->bandfilt = enable;
1991
1992 return 0;
1993}
1994
1995/* If enable is true, turn on the sensor's auto brightness control, otherwise
1996 * turn it off.
1997 *
1998 * Unsupported: KS0127, KS0127B, SAA7111A
1999 * Returns: 0 for success
2000 */
2001static int
2002sensor_set_auto_brightness(struct usb_ov511 *ov, int enable)
2003{
2004 int rc;
2005
2006 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2007
2008 if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
2009 || ov->sensor == SEN_SAA7111A) {
2010 PDEBUG(5, "Unsupported with this sensor");
2011 return -EPERM;
2012 }
2013
2014 rc = i2c_w_mask(ov, 0x2d, enable?0x10:0x00, 0x10);
2015 if (rc < 0)
2016 return rc;
2017
2018 ov->auto_brt = enable;
2019
2020 return 0;
2021}
2022
2023/* If enable is true, turn on the sensor's auto exposure control, otherwise
2024 * turn it off.
2025 *
2026 * Unsupported: KS0127, KS0127B, SAA7111A
2027 * Returns: 0 for success
2028 */
2029static int
2030sensor_set_auto_exposure(struct usb_ov511 *ov, int enable)
2031{
2032 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2033
2034 switch (ov->sensor) {
2035 case SEN_OV7610:
2036 i2c_w_mask(ov, 0x29, enable?0x00:0x80, 0x80);
2037 break;
2038 case SEN_OV6620:
2039 case SEN_OV7620:
2040 case SEN_OV76BE:
2041 case SEN_OV8600:
2042 i2c_w_mask(ov, 0x13, enable?0x01:0x00, 0x01);
2043 break;
2044 case SEN_OV6630:
2045 i2c_w_mask(ov, 0x28, enable?0x00:0x10, 0x10);
2046 break;
2047 case SEN_KS0127:
2048 case SEN_KS0127B:
2049 case SEN_SAA7111A:
2050 PDEBUG(5, "Unsupported with this sensor");
2051 return -EPERM;
2052 default:
2053 err("Sensor not supported for set_auto_exposure");
2054 return -EINVAL;
2055 }
2056
2057 ov->auto_exp = enable;
2058
2059 return 0;
2060}
2061
2062/* Modifies the sensor's exposure algorithm to allow proper exposure of objects
2063 * that are illuminated from behind.
2064 *
2065 * Tested with: OV6620, OV7620
2066 * Unsupported: OV7610, OV76BE, KS0127, KS0127B, SAA7111A
2067 * Returns: 0 for success
2068 */
2069static int
2070sensor_set_backlight(struct usb_ov511 *ov, int enable)
2071{
2072 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2073
2074 switch (ov->sensor) {
2075 case SEN_OV7620:
2076 case SEN_OV8600:
2077 i2c_w_mask(ov, 0x68, enable?0xe0:0xc0, 0xe0);
2078 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2079 i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
2080 break;
2081 case SEN_OV6620:
2082 i2c_w_mask(ov, 0x4e, enable?0xe0:0xc0, 0xe0);
2083 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2084 i2c_w_mask(ov, 0x0e, enable?0x80:0x00, 0x80);
2085 break;
2086 case SEN_OV6630:
2087 i2c_w_mask(ov, 0x4e, enable?0x80:0x60, 0xe0);
2088 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2089 i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
2090 break;
2091 case SEN_OV7610:
2092 case SEN_OV76BE:
2093 case SEN_KS0127:
2094 case SEN_KS0127B:
2095 case SEN_SAA7111A:
2096 PDEBUG(5, "Unsupported with this sensor");
2097 return -EPERM;
2098 default:
2099 err("Sensor not supported for set_backlight");
2100 return -EINVAL;
2101 }
2102
2103 ov->backlight = enable;
2104
2105 return 0;
2106}
2107
2108static int
2109sensor_set_mirror(struct usb_ov511 *ov, int enable)
2110{
2111 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2112
2113 switch (ov->sensor) {
2114 case SEN_OV6620:
2115 case SEN_OV6630:
2116 case SEN_OV7610:
2117 case SEN_OV7620:
2118 case SEN_OV76BE:
2119 case SEN_OV8600:
2120 i2c_w_mask(ov, 0x12, enable?0x40:0x00, 0x40);
2121 break;
2122 case SEN_KS0127:
2123 case SEN_KS0127B:
2124 case SEN_SAA7111A:
2125 PDEBUG(5, "Unsupported with this sensor");
2126 return -EPERM;
2127 default:
2128 err("Sensor not supported for set_mirror");
2129 return -EINVAL;
2130 }
2131
2132 ov->mirror = enable;
2133
2134 return 0;
2135}
2136
2137/* Returns number of bits per pixel (regardless of where they are located;
2138 * planar or not), or zero for unsupported format.
2139 */
2140static inline int
2141get_depth(int palette)
2142{
2143 switch (palette) {
2144 case VIDEO_PALETTE_GREY: return 8;
2145 case VIDEO_PALETTE_YUV420: return 12;
2146 case VIDEO_PALETTE_YUV420P: return 12; /* Planar */
2147 default: return 0; /* Invalid format */
2148 }
2149}
2150
2151/* Bytes per frame. Used by read(). Return of 0 indicates error */
2152static inline long int
2153get_frame_length(struct ov511_frame *frame)
2154{
2155 if (!frame)
2156 return 0;
2157 else
2158 return ((frame->width * frame->height
2159 * get_depth(frame->format)) >> 3);
2160}
2161
2162static int
2163mode_init_ov_sensor_regs(struct usb_ov511 *ov, int width, int height,
2164 int mode, int sub_flag, int qvga)
2165{
2166 int clock;
2167
2168 /******** Mode (VGA/QVGA) and sensor specific regs ********/
2169
2170 switch (ov->sensor) {
2171 case SEN_OV7610:
2172 i2c_w(ov, 0x14, qvga?0x24:0x04);
2173// FIXME: Does this improve the image quality or frame rate?
2174#if 0
2175 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2176 i2c_w(ov, 0x24, 0x10);
2177 i2c_w(ov, 0x25, qvga?0x40:0x8a);
2178 i2c_w(ov, 0x2f, qvga?0x30:0xb0);
2179 i2c_w(ov, 0x35, qvga?0x1c:0x9c);
2180#endif
2181 break;
2182 case SEN_OV7620:
2183// i2c_w(ov, 0x2b, 0x00);
2184 i2c_w(ov, 0x14, qvga?0xa4:0x84);
2185 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2186 i2c_w(ov, 0x24, qvga?0x20:0x3a);
2187 i2c_w(ov, 0x25, qvga?0x30:0x60);
2188 i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
2189 i2c_w_mask(ov, 0x67, qvga?0xf0:0x90, 0xf0);
2190 i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
2191 break;
2192 case SEN_OV76BE:
2193// i2c_w(ov, 0x2b, 0x00);
2194 i2c_w(ov, 0x14, qvga?0xa4:0x84);
2195// FIXME: Enable this once 7620AE uses 7620 initial settings
2196#if 0
2197 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2198 i2c_w(ov, 0x24, qvga?0x20:0x3a);
2199 i2c_w(ov, 0x25, qvga?0x30:0x60);
2200 i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
2201 i2c_w_mask(ov, 0x67, qvga?0xb0:0x90, 0xf0);
2202 i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
2203#endif
2204 break;
2205 case SEN_OV6620:
2206 i2c_w(ov, 0x14, qvga?0x24:0x04);
2207 break;
2208 case SEN_OV6630:
2209 i2c_w(ov, 0x14, qvga?0xa0:0x80);
2210 break;
2211 default:
2212 err("Invalid sensor");
2213 return -EINVAL;
2214 }
2215
2216 /******** Palette-specific regs ********/
2217
2218 if (mode == VIDEO_PALETTE_GREY) {
2219 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2220 /* these aren't valid on the OV6620/OV7620/6630? */
2221 i2c_w_mask(ov, 0x0e, 0x40, 0x40);
2222 }
2223
2224 if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
2225 && ov518_color) {
2226 i2c_w_mask(ov, 0x12, 0x00, 0x10);
2227 i2c_w_mask(ov, 0x13, 0x00, 0x20);
2228 } else {
2229 i2c_w_mask(ov, 0x13, 0x20, 0x20);
2230 }
2231 } else {
2232 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2233 /* not valid on the OV6620/OV7620/6630? */
2234 i2c_w_mask(ov, 0x0e, 0x00, 0x40);
2235 }
2236
2237 /* The OV518 needs special treatment. Although both the OV518
2238 * and the OV6630 support a 16-bit video bus, only the 8 bit Y
2239 * bus is actually used. The UV bus is tied to ground.
2240 * Therefore, the OV6630 needs to be in 8-bit multiplexed
2241 * output mode */
2242
2243 if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
2244 && ov518_color) {
2245 i2c_w_mask(ov, 0x12, 0x10, 0x10);
2246 i2c_w_mask(ov, 0x13, 0x20, 0x20);
2247 } else {
2248 i2c_w_mask(ov, 0x13, 0x00, 0x20);
2249 }
2250 }
2251
2252 /******** Clock programming ********/
2253
2254 /* The OV6620 needs special handling. This prevents the
2255 * severe banding that normally occurs */
2256 if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630)
2257 {
2258 /* Clock down */
2259
2260 i2c_w(ov, 0x2a, 0x04);
2261
2262 if (ov->compress) {
2263// clock = 0; /* This ensures the highest frame rate */
2264 clock = 3;
2265 } else if (clockdiv == -1) { /* If user didn't override it */
2266 clock = 3; /* Gives better exposure time */
2267 } else {
2268 clock = clockdiv;
2269 }
2270
2271 PDEBUG(4, "Setting clock divisor to %d", clock);
2272
2273 i2c_w(ov, 0x11, clock);
2274
2275 i2c_w(ov, 0x2a, 0x84);
2276 /* This next setting is critical. It seems to improve
2277 * the gain or the contrast. The "reserved" bits seem
2278 * to have some effect in this case. */
2279 i2c_w(ov, 0x2d, 0x85);
2280 }
2281 else
2282 {
2283 if (ov->compress) {
2284 clock = 1; /* This ensures the highest frame rate */
2285 } else if (clockdiv == -1) { /* If user didn't override it */
2286 /* Calculate and set the clock divisor */
2287 clock = ((sub_flag ? ov->subw * ov->subh
2288 : width * height)
2289 * (mode == VIDEO_PALETTE_GREY ? 2 : 3) / 2)
2290 / 66000;
2291 } else {
2292 clock = clockdiv;
2293 }
2294
2295 PDEBUG(4, "Setting clock divisor to %d", clock);
2296
2297 i2c_w(ov, 0x11, clock);
2298 }
2299
2300 /******** Special Features ********/
2301
2302 if (framedrop >= 0)
2303 i2c_w(ov, 0x16, framedrop);
2304
2305 /* Test Pattern */
2306 i2c_w_mask(ov, 0x12, (testpat?0x02:0x00), 0x02);
2307
2308 /* Enable auto white balance */
2309 i2c_w_mask(ov, 0x12, 0x04, 0x04);
2310
2311 // This will go away as soon as ov51x_mode_init_sensor_regs()
2312 // is fully tested.
2313 /* 7620/6620/6630? don't have register 0x35, so play it safe */
2314 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2315 if (width == 640 && height == 480)
2316 i2c_w(ov, 0x35, 0x9e);
2317 else
2318 i2c_w(ov, 0x35, 0x1e);
2319 }
2320
2321 return 0;
2322}
2323
2324static int
2325set_ov_sensor_window(struct usb_ov511 *ov, int width, int height, int mode,
2326 int sub_flag)
2327{
2328 int ret;
2329 int hwsbase, hwebase, vwsbase, vwebase, hwsize, vwsize;
2330 int hoffset, voffset, hwscale = 0, vwscale = 0;
2331
2332 /* The different sensor ICs handle setting up of window differently.
2333 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!!! */
2334 switch (ov->sensor) {
2335 case SEN_OV7610:
2336 case SEN_OV76BE:
2337 hwsbase = 0x38;
2338 hwebase = 0x3a;
2339 vwsbase = vwebase = 0x05;
2340 break;
2341 case SEN_OV6620:
2342 case SEN_OV6630:
2343 hwsbase = 0x38;
2344 hwebase = 0x3a;
2345 vwsbase = 0x05;
2346 vwebase = 0x06;
2347 break;
2348 case SEN_OV7620:
2349 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
2350 hwebase = 0x2f;
2351 vwsbase = vwebase = 0x05;
2352 break;
2353 default:
2354 err("Invalid sensor");
2355 return -EINVAL;
2356 }
2357
2358 if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630) {
2359 /* Note: OV518(+) does downsample on its own) */
2360 if ((width > 176 && height > 144)
2361 || ov->bclass == BCL_OV518) { /* CIF */
2362 ret = mode_init_ov_sensor_regs(ov, width, height,
2363 mode, sub_flag, 0);
2364 if (ret < 0)
2365 return ret;
2366 hwscale = 1;
2367 vwscale = 1; /* The datasheet says 0; it's wrong */
2368 hwsize = 352;
2369 vwsize = 288;
2370 } else if (width > 176 || height > 144) {
2371 err("Illegal dimensions");
2372 return -EINVAL;
2373 } else { /* QCIF */
2374 ret = mode_init_ov_sensor_regs(ov, width, height,
2375 mode, sub_flag, 1);
2376 if (ret < 0)
2377 return ret;
2378 hwsize = 176;
2379 vwsize = 144;
2380 }
2381 } else {
2382 if (width > 320 && height > 240) { /* VGA */
2383 ret = mode_init_ov_sensor_regs(ov, width, height,
2384 mode, sub_flag, 0);
2385 if (ret < 0)
2386 return ret;
2387 hwscale = 2;
2388 vwscale = 1;
2389 hwsize = 640;
2390 vwsize = 480;
2391 } else if (width > 320 || height > 240) {
2392 err("Illegal dimensions");
2393 return -EINVAL;
2394 } else { /* QVGA */
2395 ret = mode_init_ov_sensor_regs(ov, width, height,
2396 mode, sub_flag, 1);
2397 if (ret < 0)
2398 return ret;
2399 hwscale = 1;
2400 hwsize = 320;
2401 vwsize = 240;
2402 }
2403 }
2404
2405 /* Center the window */
2406 hoffset = ((hwsize - width) / 2) >> hwscale;
2407 voffset = ((vwsize - height) / 2) >> vwscale;
2408
2409 /* FIXME! - This needs to be changed to support 160x120 and 6620!!! */
2410 if (sub_flag) {
2411 i2c_w(ov, 0x17, hwsbase+(ov->subx>>hwscale));
2412 i2c_w(ov, 0x18, hwebase+((ov->subx+ov->subw)>>hwscale));
2413 i2c_w(ov, 0x19, vwsbase+(ov->suby>>vwscale));
2414 i2c_w(ov, 0x1a, vwebase+((ov->suby+ov->subh)>>vwscale));
2415 } else {
2416 i2c_w(ov, 0x17, hwsbase + hoffset);
2417 i2c_w(ov, 0x18, hwebase + hoffset + (hwsize>>hwscale));
2418 i2c_w(ov, 0x19, vwsbase + voffset);
2419 i2c_w(ov, 0x1a, vwebase + voffset + (vwsize>>vwscale));
2420 }
2421
2422#ifdef OV511_DEBUG
2423 if (dump_sensor)
2424 dump_i2c_regs(ov);
2425#endif
2426
2427 return 0;
2428}
2429
2430/* Set up the OV511/OV511+ with the given image parameters.
2431 *
2432 * Do not put any sensor-specific code in here (including I2C I/O functions)
2433 */
2434static int
2435ov511_mode_init_regs(struct usb_ov511 *ov,
2436 int width, int height, int mode, int sub_flag)
2437{
2438 int hsegs, vsegs;
2439
2440 if (sub_flag) {
2441 width = ov->subw;
2442 height = ov->subh;
2443 }
2444
2445 PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
2446 width, height, mode, sub_flag);
2447
2448 // FIXME: This should be moved to a 7111a-specific function once
2449 // subcapture is dealt with properly
2450 if (ov->sensor == SEN_SAA7111A) {
2451 if (width == 320 && height == 240) {
2452 /* No need to do anything special */
2453 } else if (width == 640 && height == 480) {
2454 /* Set the OV511 up as 320x480, but keep the
2455 * V4L resolution as 640x480 */
2456 width = 320;
2457 } else {
2458 err("SAA7111A only allows 320x240 or 640x480");
2459 return -EINVAL;
2460 }
2461 }
2462
2463 /* Make sure width and height are a multiple of 8 */
2464 if (width % 8 || height % 8) {
2465 err("Invalid size (%d, %d) (mode = %d)", width, height, mode);
2466 return -EINVAL;
2467 }
2468
2469 if (width < ov->minwidth || height < ov->minheight) {
2470 err("Requested dimensions are too small");
2471 return -EINVAL;
2472 }
2473
2474 if (ov51x_stop(ov) < 0)
2475 return -EIO;
2476
2477 if (mode == VIDEO_PALETTE_GREY) {
2478 reg_w(ov, R511_CAM_UV_EN, 0x00);
2479 reg_w(ov, R511_SNAP_UV_EN, 0x00);
2480 reg_w(ov, R511_SNAP_OPTS, 0x01);
2481 } else {
2482 reg_w(ov, R511_CAM_UV_EN, 0x01);
2483 reg_w(ov, R511_SNAP_UV_EN, 0x01);
2484 reg_w(ov, R511_SNAP_OPTS, 0x03);
2485 }
2486
2487 /* Here I'm assuming that snapshot size == image size.
2488 * I hope that's always true. --claudio
2489 */
2490 hsegs = (width >> 3) - 1;
2491 vsegs = (height >> 3) - 1;
2492
2493 reg_w(ov, R511_CAM_PXCNT, hsegs);
2494 reg_w(ov, R511_CAM_LNCNT, vsegs);
2495 reg_w(ov, R511_CAM_PXDIV, 0x00);
2496 reg_w(ov, R511_CAM_LNDIV, 0x00);
2497
2498 /* YUV420, low pass filter on */
2499 reg_w(ov, R511_CAM_OPTS, 0x03);
2500
2501 /* Snapshot additions */
2502 reg_w(ov, R511_SNAP_PXCNT, hsegs);
2503 reg_w(ov, R511_SNAP_LNCNT, vsegs);
2504 reg_w(ov, R511_SNAP_PXDIV, 0x00);
2505 reg_w(ov, R511_SNAP_LNDIV, 0x00);
2506
2507 if (ov->compress) {
2508 /* Enable Y and UV quantization and compression */
2509 reg_w(ov, R511_COMP_EN, 0x07);
2510 reg_w(ov, R511_COMP_LUT_EN, 0x03);
2511 ov51x_reset(ov, OV511_RESET_OMNICE);
2512 }
2513
2514 if (ov51x_restart(ov) < 0)
2515 return -EIO;
2516
2517 return 0;
2518}
2519
2520/* Sets up the OV518/OV518+ with the given image parameters
2521 *
2522 * OV518 needs a completely different approach, until we can figure out what
2523 * the individual registers do. Also, only 15 FPS is supported now.
2524 *
2525 * Do not put any sensor-specific code in here (including I2C I/O functions)
2526 */
2527static int
2528ov518_mode_init_regs(struct usb_ov511 *ov,
2529 int width, int height, int mode, int sub_flag)
2530{
2531 int hsegs, vsegs, hi_res;
2532
2533 if (sub_flag) {
2534 width = ov->subw;
2535 height = ov->subh;
2536 }
2537
2538 PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
2539 width, height, mode, sub_flag);
2540
2541 if (width % 16 || height % 8) {
2542 err("Invalid size (%d, %d)", width, height);
2543 return -EINVAL;
2544 }
2545
2546 if (width < ov->minwidth || height < ov->minheight) {
2547 err("Requested dimensions are too small");
2548 return -EINVAL;
2549 }
2550
2551 if (width >= 320 && height >= 240) {
2552 hi_res = 1;
2553 } else if (width >= 320 || height >= 240) {
2554 err("Invalid width/height combination (%d, %d)", width, height);
2555 return -EINVAL;
2556 } else {
2557 hi_res = 0;
2558 }
2559
2560 if (ov51x_stop(ov) < 0)
2561 return -EIO;
2562
2563 /******** Set the mode ********/
2564
2565 reg_w(ov, 0x2b, 0);
2566 reg_w(ov, 0x2c, 0);
2567 reg_w(ov, 0x2d, 0);
2568 reg_w(ov, 0x2e, 0);
2569 reg_w(ov, 0x3b, 0);
2570 reg_w(ov, 0x3c, 0);
2571 reg_w(ov, 0x3d, 0);
2572 reg_w(ov, 0x3e, 0);
2573
2574 if (ov->bridge == BRG_OV518 && ov518_color) {
2575 /* OV518 needs U and V swapped */
2576 i2c_w_mask(ov, 0x15, 0x00, 0x01);
2577
2578 if (mode == VIDEO_PALETTE_GREY) {
2579 /* Set 16-bit input format (UV data are ignored) */
2580 reg_w_mask(ov, 0x20, 0x00, 0x08);
2581
2582 /* Set 8-bit (4:0:0) output format */
2583 reg_w_mask(ov, 0x28, 0x00, 0xf0);
2584 reg_w_mask(ov, 0x38, 0x00, 0xf0);
2585 } else {
2586 /* Set 8-bit (YVYU) input format */
2587 reg_w_mask(ov, 0x20, 0x08, 0x08);
2588
2589 /* Set 12-bit (4:2:0) output format */
2590 reg_w_mask(ov, 0x28, 0x80, 0xf0);
2591 reg_w_mask(ov, 0x38, 0x80, 0xf0);
2592 }
2593 } else {
2594 reg_w(ov, 0x28, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
2595 reg_w(ov, 0x38, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
2596 }
2597
2598 hsegs = width / 16;
2599 vsegs = height / 4;
2600
2601 reg_w(ov, 0x29, hsegs);
2602 reg_w(ov, 0x2a, vsegs);
2603
2604 reg_w(ov, 0x39, hsegs);
2605 reg_w(ov, 0x3a, vsegs);
2606
2607 /* Windows driver does this here; who knows why */
2608 reg_w(ov, 0x2f, 0x80);
2609
2610 /******** Set the framerate (to 15 FPS) ********/
2611
2612 /* Mode independent, but framerate dependent, regs */
2613 reg_w(ov, 0x51, 0x02); /* Clock divider; lower==faster */
2614 reg_w(ov, 0x22, 0x18);
2615 reg_w(ov, 0x23, 0xff);
2616
2617 if (ov->bridge == BRG_OV518PLUS)
2618 reg_w(ov, 0x21, 0x19);
2619 else
2620 reg_w(ov, 0x71, 0x19); /* Compression-related? */
2621
2622 // FIXME: Sensor-specific
2623 /* Bit 5 is what matters here. Of course, it is "reserved" */
2624 i2c_w(ov, 0x54, 0x23);
2625
2626 reg_w(ov, 0x2f, 0x80);
2627
2628 if (ov->bridge == BRG_OV518PLUS) {
2629 reg_w(ov, 0x24, 0x94);
2630 reg_w(ov, 0x25, 0x90);
2631 ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
2632 ov518_reg_w32(ov, 0xc6, 540, 2); /* 21ch */
2633 ov518_reg_w32(ov, 0xc7, 540, 2); /* 21ch */
2634 ov518_reg_w32(ov, 0xc8, 108, 2); /* 6ch */
2635 ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
2636 ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
2637 ov518_reg_w32(ov, 0xcc, 2400, 2); /* 960h */
2638 ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
2639 ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
2640 } else {
2641 reg_w(ov, 0x24, 0x9f);
2642 reg_w(ov, 0x25, 0x90);
2643 ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
2644 ov518_reg_w32(ov, 0xc6, 500, 2); /* 1f4h */
2645 ov518_reg_w32(ov, 0xc7, 500, 2); /* 1f4h */
2646 ov518_reg_w32(ov, 0xc8, 142, 2); /* 8eh */
2647 ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
2648 ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
2649 ov518_reg_w32(ov, 0xcc, 2000, 2); /* 7d0h */
2650 ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
2651 ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
2652 }
2653
2654 reg_w(ov, 0x2f, 0x80);
2655
2656 if (ov51x_restart(ov) < 0)
2657 return -EIO;
2658
2659 /* Reset it just for good measure */
2660 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
2661 return -EIO;
2662
2663 return 0;
2664}
2665
2666/* This is a wrapper around the OV511, OV518, and sensor specific functions */
2667static int
2668mode_init_regs(struct usb_ov511 *ov,
2669 int width, int height, int mode, int sub_flag)
2670{
2671 int rc = 0;
2672
2673 if (!ov || !ov->dev)
2674 return -EFAULT;
2675
2676 if (ov->bclass == BCL_OV518) {
2677 rc = ov518_mode_init_regs(ov, width, height, mode, sub_flag);
2678 } else {
2679 rc = ov511_mode_init_regs(ov, width, height, mode, sub_flag);
2680 }
2681
2682 if (FATAL_ERROR(rc))
2683 return rc;
2684
2685 switch (ov->sensor) {
2686 case SEN_OV7610:
2687 case SEN_OV7620:
2688 case SEN_OV76BE:
2689 case SEN_OV8600:
2690 case SEN_OV6620:
2691 case SEN_OV6630:
2692 rc = set_ov_sensor_window(ov, width, height, mode, sub_flag);
2693 break;
2694 case SEN_KS0127:
2695 case SEN_KS0127B:
2696 err("KS0127-series decoders not supported yet");
2697 rc = -EINVAL;
2698 break;
2699 case SEN_SAA7111A:
2700// rc = mode_init_saa_sensor_regs(ov, width, height, mode,
2701// sub_flag);
2702
2703 PDEBUG(1, "SAA status = 0x%02X", i2c_r(ov, 0x1f));
2704 break;
2705 default:
2706 err("Unknown sensor");
2707 rc = -EINVAL;
2708 }
2709
2710 if (FATAL_ERROR(rc))
2711 return rc;
2712
2713 /* Sensor-independent settings */
2714 rc = sensor_set_auto_brightness(ov, ov->auto_brt);
2715 if (FATAL_ERROR(rc))
2716 return rc;
2717
2718 rc = sensor_set_auto_exposure(ov, ov->auto_exp);
2719 if (FATAL_ERROR(rc))
2720 return rc;
2721
2722 rc = sensor_set_banding_filter(ov, bandingfilter);
2723 if (FATAL_ERROR(rc))
2724 return rc;
2725
2726 if (ov->lightfreq) {
2727 rc = sensor_set_light_freq(ov, lightfreq);
2728 if (FATAL_ERROR(rc))
2729 return rc;
2730 }
2731
2732 rc = sensor_set_backlight(ov, ov->backlight);
2733 if (FATAL_ERROR(rc))
2734 return rc;
2735
2736 rc = sensor_set_mirror(ov, ov->mirror);
2737 if (FATAL_ERROR(rc))
2738 return rc;
2739
2740 return 0;
2741}
2742
2743/* This sets the default image parameters. This is useful for apps that use
2744 * read() and do not set these.
2745 */
2746static int
2747ov51x_set_default_params(struct usb_ov511 *ov)
2748{
2749 int i;
2750
2751 /* Set default sizes in case IOCTL (VIDIOCMCAPTURE) is not used
2752 * (using read() instead). */
2753 for (i = 0; i < OV511_NUMFRAMES; i++) {
2754 ov->frame[i].width = ov->maxwidth;
2755 ov->frame[i].height = ov->maxheight;
2756 ov->frame[i].bytes_read = 0;
2757 if (force_palette)
2758 ov->frame[i].format = force_palette;
2759 else
2760 ov->frame[i].format = VIDEO_PALETTE_YUV420;
2761
2762 ov->frame[i].depth = get_depth(ov->frame[i].format);
2763 }
2764
2765 PDEBUG(3, "%dx%d, %s", ov->maxwidth, ov->maxheight,
2766 symbolic(v4l1_plist, ov->frame[0].format));
2767
2768 /* Initialize to max width/height, YUV420 or RGB24 (if supported) */
2769 if (mode_init_regs(ov, ov->maxwidth, ov->maxheight,
2770 ov->frame[0].format, 0) < 0)
2771 return -EINVAL;
2772
2773 return 0;
2774}
2775
2776/**********************************************************************
2777 *
2778 * Video decoder stuff
2779 *
2780 **********************************************************************/
2781
2782/* Set analog input port of decoder */
2783static int
2784decoder_set_input(struct usb_ov511 *ov, int input)
2785{
2786 PDEBUG(4, "port %d", input);
2787
2788 switch (ov->sensor) {
2789 case SEN_SAA7111A:
2790 {
2791 /* Select mode */
2792 i2c_w_mask(ov, 0x02, input, 0x07);
2793 /* Bypass chrominance trap for modes 4..7 */
2794 i2c_w_mask(ov, 0x09, (input > 3) ? 0x80:0x00, 0x80);
2795 break;
2796 }
2797 default:
2798 return -EINVAL;
2799 }
2800
2801 return 0;
2802}
2803
2804/* Get ASCII name of video input */
2805static int
2806decoder_get_input_name(struct usb_ov511 *ov, int input, char *name)
2807{
2808 switch (ov->sensor) {
2809 case SEN_SAA7111A:
2810 {
2811 if (input < 0 || input > 7)
2812 return -EINVAL;
2813 else if (input < 4)
2814 sprintf(name, "CVBS-%d", input);
2815 else // if (input < 8)
2816 sprintf(name, "S-Video-%d", input - 4);
2817 break;
2818 }
2819 default:
2820 sprintf(name, "%s", "Camera");
2821 }
2822
2823 return 0;
2824}
2825
2826/* Set norm (NTSC, PAL, SECAM, AUTO) */
2827static int
2828decoder_set_norm(struct usb_ov511 *ov, int norm)
2829{
2830 PDEBUG(4, "%d", norm);
2831
2832 switch (ov->sensor) {
2833 case SEN_SAA7111A:
2834 {
2835 int reg_8, reg_e;
2836
2837 if (norm == VIDEO_MODE_NTSC) {
2838 reg_8 = 0x40; /* 60 Hz */
2839 reg_e = 0x00; /* NTSC M / PAL BGHI */
2840 } else if (norm == VIDEO_MODE_PAL) {
2841 reg_8 = 0x00; /* 50 Hz */
2842 reg_e = 0x00; /* NTSC M / PAL BGHI */
2843 } else if (norm == VIDEO_MODE_AUTO) {
2844 reg_8 = 0x80; /* Auto field detect */
2845 reg_e = 0x00; /* NTSC M / PAL BGHI */
2846 } else if (norm == VIDEO_MODE_SECAM) {
2847 reg_8 = 0x00; /* 50 Hz */
2848 reg_e = 0x50; /* SECAM / PAL 4.43 */
2849 } else {
2850 return -EINVAL;
2851 }
2852
2853 i2c_w_mask(ov, 0x08, reg_8, 0xc0);
2854 i2c_w_mask(ov, 0x0e, reg_e, 0x70);
2855 break;
2856 }
2857 default:
2858 return -EINVAL;
2859 }
2860
2861 return 0;
2862}
2863
2864/**********************************************************************
2865 *
2866 * Raw data parsing
2867 *
2868 **********************************************************************/
2869
2870/* Copies a 64-byte segment at pIn to an 8x8 block at pOut. The width of the
2871 * image at pOut is specified by w.
2872 */
2873static inline void
2874make_8x8(unsigned char *pIn, unsigned char *pOut, int w)
2875{
2876 unsigned char *pOut1 = pOut;
2877 int x, y;
2878
2879 for (y = 0; y < 8; y++) {
2880 pOut1 = pOut;
2881 for (x = 0; x < 8; x++) {
2882 *pOut1++ = *pIn++;
2883 }
2884 pOut += w;
2885 }
2886}
2887
2888/*
2889 * For RAW BW (YUV 4:0:0) images, data show up in 256 byte segments.
2890 * The segments represent 4 squares of 8x8 pixels as follows:
2891 *
2892 * 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
2893 * 8 9 ... 15 72 73 ... 79 200 201 ... 207
2894 * ... ... ...
2895 * 56 57 ... 63 120 121 ... 127 248 249 ... 255
2896 *
2897 */
2898static void
2899yuv400raw_to_yuv400p(struct ov511_frame *frame,
2900 unsigned char *pIn0, unsigned char *pOut0)
2901{
2902 int x, y;
2903 unsigned char *pIn, *pOut, *pOutLine;
2904
2905 /* Copy Y */
2906 pIn = pIn0;
2907 pOutLine = pOut0;
2908 for (y = 0; y < frame->rawheight - 1; y += 8) {
2909 pOut = pOutLine;
2910 for (x = 0; x < frame->rawwidth - 1; x += 8) {
2911 make_8x8(pIn, pOut, frame->rawwidth);
2912 pIn += 64;
2913 pOut += 8;
2914 }
2915 pOutLine += 8 * frame->rawwidth;
2916 }
2917}
2918
2919/*
2920 * For YUV 4:2:0 images, the data show up in 384 byte segments.
2921 * The first 64 bytes of each segment are U, the next 64 are V. The U and
2922 * V are arranged as follows:
2923 *
2924 * 0 1 ... 7
2925 * 8 9 ... 15
2926 * ...
2927 * 56 57 ... 63
2928 *
2929 * U and V are shipped at half resolution (1 U,V sample -> one 2x2 block).
2930 *
2931 * The next 256 bytes are full resolution Y data and represent 4 squares
2932 * of 8x8 pixels as follows:
2933 *
2934 * 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
2935 * 8 9 ... 15 72 73 ... 79 200 201 ... 207
2936 * ... ... ...
2937 * 56 57 ... 63 120 121 ... 127 ... 248 249 ... 255
2938 *
2939 * Note that the U and V data in one segment represent a 16 x 16 pixel
2940 * area, but the Y data represent a 32 x 8 pixel area. If the width is not an
2941 * even multiple of 32, the extra 8x8 blocks within a 32x8 block belong to the
2942 * next horizontal stripe.
2943 *
2944 * If dumppix module param is set, _parse_data just dumps the incoming segments,
2945 * verbatim, in order, into the frame. When used with vidcat -f ppm -s 640x480
2946 * this puts the data on the standard output and can be analyzed with the
2947 * parseppm.c utility I wrote. That's a much faster way for figuring out how
2948 * these data are scrambled.
2949 */
2950
2951/* Converts from raw, uncompressed segments at pIn0 to a YUV420P frame at pOut0.
2952 *
2953 * FIXME: Currently only handles width and height that are multiples of 16
2954 */
2955static void
2956yuv420raw_to_yuv420p(struct ov511_frame *frame,
2957 unsigned char *pIn0, unsigned char *pOut0)
2958{
2959 int k, x, y;
2960 unsigned char *pIn, *pOut, *pOutLine;
2961 const unsigned int a = frame->rawwidth * frame->rawheight;
2962 const unsigned int w = frame->rawwidth / 2;
2963
2964 /* Copy U and V */
2965 pIn = pIn0;
2966 pOutLine = pOut0 + a;
2967 for (y = 0; y < frame->rawheight - 1; y += 16) {
2968 pOut = pOutLine;
2969 for (x = 0; x < frame->rawwidth - 1; x += 16) {
2970 make_8x8(pIn, pOut, w);
2971 make_8x8(pIn + 64, pOut + a/4, w);
2972 pIn += 384;
2973 pOut += 8;
2974 }
2975 pOutLine += 8 * w;
2976 }
2977
2978 /* Copy Y */
2979 pIn = pIn0 + 128;
2980 pOutLine = pOut0;
2981 k = 0;
2982 for (y = 0; y < frame->rawheight - 1; y += 8) {
2983 pOut = pOutLine;
2984 for (x = 0; x < frame->rawwidth - 1; x += 8) {
2985 make_8x8(pIn, pOut, frame->rawwidth);
2986 pIn += 64;
2987 pOut += 8;
2988 if ((++k) > 3) {
2989 k = 0;
2990 pIn += 128;
2991 }
2992 }
2993 pOutLine += 8 * frame->rawwidth;
2994 }
2995}
2996
2997/**********************************************************************
2998 *
2999 * Decompression
3000 *
3001 **********************************************************************/
3002
3003static int
3004request_decompressor(struct usb_ov511 *ov)
3005{
3006 if (ov->bclass == BCL_OV511 || ov->bclass == BCL_OV518) {
3007 err("No decompressor available");
3008 } else {
3009 err("Unknown bridge");
3010 }
3011
3012 return -ENOSYS;
3013}
3014
3015static void
3016decompress(struct usb_ov511 *ov, struct ov511_frame *frame,
3017 unsigned char *pIn0, unsigned char *pOut0)
3018{
3019 if (!ov->decomp_ops)
3020 if (request_decompressor(ov))
3021 return;
3022
3023}
3024
3025/**********************************************************************
3026 *
3027 * Format conversion
3028 *
3029 **********************************************************************/
3030
3031/* Fuses even and odd fields together, and doubles width.
3032 * INPUT: an odd field followed by an even field at pIn0, in YUV planar format
3033 * OUTPUT: a normal YUV planar image, with correct aspect ratio
3034 */
3035static void
3036deinterlace(struct ov511_frame *frame, int rawformat,
3037 unsigned char *pIn0, unsigned char *pOut0)
3038{
3039 const int fieldheight = frame->rawheight / 2;
3040 const int fieldpix = fieldheight * frame->rawwidth;
3041 const int w = frame->width;
3042 int x, y;
3043 unsigned char *pInEven, *pInOdd, *pOut;
3044
3045 PDEBUG(5, "fieldheight=%d", fieldheight);
3046
3047 if (frame->rawheight != frame->height) {
3048 err("invalid height");
3049 return;
3050 }
3051
3052 if ((frame->rawwidth * 2) != frame->width) {
3053 err("invalid width");
3054 return;
3055 }
3056
3057 /* Y */
3058 pInOdd = pIn0;
3059 pInEven = pInOdd + fieldpix;
3060 pOut = pOut0;
3061 for (y = 0; y < fieldheight; y++) {
3062 for (x = 0; x < frame->rawwidth; x++) {
3063 *pOut = *pInEven;
3064 *(pOut+1) = *pInEven++;
3065 *(pOut+w) = *pInOdd;
3066 *(pOut+w+1) = *pInOdd++;
3067 pOut += 2;
3068 }
3069 pOut += w;
3070 }
3071
3072 if (rawformat == RAWFMT_YUV420) {
3073 /* U */
3074 pInOdd = pIn0 + fieldpix * 2;
3075 pInEven = pInOdd + fieldpix / 4;
3076 for (y = 0; y < fieldheight / 2; y++) {
3077 for (x = 0; x < frame->rawwidth / 2; x++) {
3078 *pOut = *pInEven;
3079 *(pOut+1) = *pInEven++;
3080 *(pOut+w/2) = *pInOdd;
3081 *(pOut+w/2+1) = *pInOdd++;
3082 pOut += 2;
3083 }
3084 pOut += w/2;
3085 }
3086 /* V */
3087 pInOdd = pIn0 + fieldpix * 2 + fieldpix / 2;
3088 pInEven = pInOdd + fieldpix / 4;
3089 for (y = 0; y < fieldheight / 2; y++) {
3090 for (x = 0; x < frame->rawwidth / 2; x++) {
3091 *pOut = *pInEven;
3092 *(pOut+1) = *pInEven++;
3093 *(pOut+w/2) = *pInOdd;
3094 *(pOut+w/2+1) = *pInOdd++;
3095 pOut += 2;
3096 }
3097 pOut += w/2;
3098 }
3099 }
3100}
3101
3102static void
3103ov51x_postprocess_grey(struct usb_ov511 *ov, struct ov511_frame *frame)
3104{
3105 /* Deinterlace frame, if necessary */
3106 if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
3107 if (frame->compressed)
3108 decompress(ov, frame, frame->rawdata,
3109 frame->tempdata);
3110 else
3111 yuv400raw_to_yuv400p(frame, frame->rawdata,
3112 frame->tempdata);
3113
3114 deinterlace(frame, RAWFMT_YUV400, frame->tempdata,
3115 frame->data);
3116 } else {
3117 if (frame->compressed)
3118 decompress(ov, frame, frame->rawdata,
3119 frame->data);
3120 else
3121 yuv400raw_to_yuv400p(frame, frame->rawdata,
3122 frame->data);
3123 }
3124}
3125
3126/* Process raw YUV420 data into standard YUV420P */
3127static void
3128ov51x_postprocess_yuv420(struct usb_ov511 *ov, struct ov511_frame *frame)
3129{
3130 /* Deinterlace frame, if necessary */
3131 if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
3132 if (frame->compressed)
3133 decompress(ov, frame, frame->rawdata, frame->tempdata);
3134 else
3135 yuv420raw_to_yuv420p(frame, frame->rawdata,
3136 frame->tempdata);
3137
3138 deinterlace(frame, RAWFMT_YUV420, frame->tempdata,
3139 frame->data);
3140 } else {
3141 if (frame->compressed)
3142 decompress(ov, frame, frame->rawdata, frame->data);
3143 else
3144 yuv420raw_to_yuv420p(frame, frame->rawdata,
3145 frame->data);
3146 }
3147}
3148
3149/* Post-processes the specified frame. This consists of:
3150 * 1. Decompress frame, if necessary
3151 * 2. Deinterlace frame and scale to proper size, if necessary
3152 * 3. Convert from YUV planar to destination format, if necessary
3153 * 4. Fix the RGB offset, if necessary
3154 */
3155static void
3156ov51x_postprocess(struct usb_ov511 *ov, struct ov511_frame *frame)
3157{
3158 if (dumppix) {
3159 memset(frame->data, 0,
3160 MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
3161 PDEBUG(4, "Dumping %d bytes", frame->bytes_recvd);
3162 memcpy(frame->data, frame->rawdata, frame->bytes_recvd);
3163 } else {
3164 switch (frame->format) {
3165 case VIDEO_PALETTE_GREY:
3166 ov51x_postprocess_grey(ov, frame);
3167 break;
3168 case VIDEO_PALETTE_YUV420:
3169 case VIDEO_PALETTE_YUV420P:
3170 ov51x_postprocess_yuv420(ov, frame);
3171 break;
3172 default:
3173 err("Cannot convert data to %s",
3174 symbolic(v4l1_plist, frame->format));
3175 }
3176 }
3177}
3178
3179/**********************************************************************
3180 *
3181 * OV51x data transfer, IRQ handler
3182 *
3183 **********************************************************************/
3184
3185static inline void
3186ov511_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
3187{
3188 int num, offset;
3189 int pnum = in[ov->packet_size - 1]; /* Get packet number */
3190 int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
3191 struct ov511_frame *frame = &ov->frame[ov->curframe];
3192 struct timeval *ts;
3193
3194 /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
3195 * byte non-zero. The EOF packet has image width/height in the
3196 * 10th and 11th bytes. The 9th byte is given as follows:
3197 *
3198 * bit 7: EOF
3199 * 6: compression enabled
3200 * 5: 422/420/400 modes
3201 * 4: 422/420/400 modes
3202 * 3: 1
3203 * 2: snapshot button on
3204 * 1: snapshot frame
3205 * 0: even/odd field
3206 */
3207
3208 if (printph) {
3209 info("ph(%3d): %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x",
3210 pnum, in[0], in[1], in[2], in[3], in[4], in[5], in[6],
3211 in[7], in[8], in[9], in[10], in[11]);
3212 }
3213
3214 /* Check for SOF/EOF packet */
3215 if ((in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) ||
3216 (~in[8] & 0x08))
3217 goto check_middle;
3218
3219 /* Frame end */
3220 if (in[8] & 0x80) {
3221 ts = (struct timeval *)(frame->data
3222 + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
3223 do_gettimeofday(ts);
3224
3225 /* Get the actual frame size from the EOF header */
3226 frame->rawwidth = ((int)(in[9]) + 1) * 8;
3227 frame->rawheight = ((int)(in[10]) + 1) * 8;
3228
3229 PDEBUG(4, "Frame end, frame=%d, pnum=%d, w=%d, h=%d, recvd=%d",
3230 ov->curframe, pnum, frame->rawwidth, frame->rawheight,
3231 frame->bytes_recvd);
3232
3233 /* Validate the header data */
3234 RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
3235 RESTRICT_TO_RANGE(frame->rawheight, ov->minheight,
3236 ov->maxheight);
3237
3238 /* Don't allow byte count to exceed buffer size */
3239 RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
3240
3241 if (frame->scanstate == STATE_LINES) {
3242 int nextf;
3243
3244 frame->grabstate = FRAME_DONE;
3245 wake_up_interruptible(&frame->wq);
3246
3247 /* If next frame is ready or grabbing,
3248 * point to it */
3249 nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
3250 if (ov->frame[nextf].grabstate == FRAME_READY
3251 || ov->frame[nextf].grabstate == FRAME_GRABBING) {
3252 ov->curframe = nextf;
3253 ov->frame[nextf].scanstate = STATE_SCANNING;
3254 } else {
3255 if (frame->grabstate == FRAME_DONE) {
3256 PDEBUG(4, "** Frame done **");
3257 } else {
3258 PDEBUG(4, "Frame not ready? state = %d",
3259 ov->frame[nextf].grabstate);
3260 }
3261
3262 ov->curframe = -1;
3263 }
3264 } else {
3265 PDEBUG(5, "Frame done, but not scanning");
3266 }
3267 /* Image corruption caused by misplaced frame->segment = 0
3268 * fixed by carlosf@conectiva.com.br
3269 */
3270 } else {
3271 /* Frame start */
3272 PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
3273
3274 /* Check to see if it's a snapshot frame */
3275 /* FIXME?? Should the snapshot reset go here? Performance? */
3276 if (in[8] & 0x02) {
3277 frame->snapshot = 1;
3278 PDEBUG(3, "snapshot detected");
3279 }
3280
3281 frame->scanstate = STATE_LINES;
3282 frame->bytes_recvd = 0;
3283 frame->compressed = in[8] & 0x40;
3284 }
3285
3286check_middle:
3287 /* Are we in a frame? */
3288 if (frame->scanstate != STATE_LINES) {
3289 PDEBUG(5, "Not in a frame; packet skipped");
3290 return;
3291 }
3292
3293 /* If frame start, skip header */
3294 if (frame->bytes_recvd == 0)
3295 offset = 9;
3296 else
3297 offset = 0;
3298
3299 num = n - offset - 1;
3300
3301 /* Dump all data exactly as received */
3302 if (dumppix == 2) {
3303 frame->bytes_recvd += n - 1;
3304 if (frame->bytes_recvd <= max_raw)
3305 memcpy(frame->rawdata + frame->bytes_recvd - (n - 1),
3306 in, n - 1);
3307 else
3308 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3309 frame->bytes_recvd - max_raw);
3310 } else if (!frame->compressed && !remove_zeros) {
3311 frame->bytes_recvd += num;
3312 if (frame->bytes_recvd <= max_raw)
3313 memcpy(frame->rawdata + frame->bytes_recvd - num,
3314 in + offset, num);
3315 else
3316 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3317 frame->bytes_recvd - max_raw);
3318 } else { /* Remove all-zero FIFO lines (aligned 32-byte blocks) */
3319 int b, read = 0, allzero, copied = 0;
3320 if (offset) {
3321 frame->bytes_recvd += 32 - offset; // Bytes out
3322 memcpy(frame->rawdata, in + offset, 32 - offset);
3323 read += 32;
3324 }
3325
3326 while (read < n - 1) {
3327 allzero = 1;
3328 for (b = 0; b < 32; b++) {
3329 if (in[read + b]) {
3330 allzero = 0;
3331 break;
3332 }
3333 }
3334
3335 if (allzero) {
3336 /* Don't copy it */
3337 } else {
3338 if (frame->bytes_recvd + copied + 32 <= max_raw)
3339 {
3340 memcpy(frame->rawdata
3341 + frame->bytes_recvd + copied,
3342 in + read, 32);
3343 copied += 32;
3344 } else {
3345 PDEBUG(3, "Raw data buffer overrun!!");
3346 }
3347 }
3348 read += 32;
3349 }
3350
3351 frame->bytes_recvd += copied;
3352 }
3353}
3354
3355static inline void
3356ov518_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
3357{
3358 int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
3359 struct ov511_frame *frame = &ov->frame[ov->curframe];
3360 struct timeval *ts;
3361
3362 /* Don't copy the packet number byte */
3363 if (ov->packet_numbering)
3364 --n;
3365
3366 /* A false positive here is likely, until OVT gives me
3367 * the definitive SOF/EOF format */
3368 if ((!(in[0] | in[1] | in[2] | in[3] | in[5])) && in[6]) {
3369 if (printph) {
3370 info("ph: %2x %2x %2x %2x %2x %2x %2x %2x", in[0],
3371 in[1], in[2], in[3], in[4], in[5], in[6], in[7]);
3372 }
3373
3374 if (frame->scanstate == STATE_LINES) {
3375 PDEBUG(4, "Detected frame end/start");
3376 goto eof;
3377 } else { //scanstate == STATE_SCANNING
3378 /* Frame start */
3379 PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
3380 goto sof;
3381 }
3382 } else {
3383 goto check_middle;
3384 }
3385
3386eof:
3387 ts = (struct timeval *)(frame->data
3388 + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
3389 do_gettimeofday(ts);
3390
3391 PDEBUG(4, "Frame end, curframe = %d, hw=%d, vw=%d, recvd=%d",
3392 ov->curframe,
3393 (int)(in[9]), (int)(in[10]), frame->bytes_recvd);
3394
3395 // FIXME: Since we don't know the header formats yet,
3396 // there is no way to know what the actual image size is
3397 frame->rawwidth = frame->width;
3398 frame->rawheight = frame->height;
3399
3400 /* Validate the header data */
3401 RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
3402 RESTRICT_TO_RANGE(frame->rawheight, ov->minheight, ov->maxheight);
3403
3404 /* Don't allow byte count to exceed buffer size */
3405 RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
3406
3407 if (frame->scanstate == STATE_LINES) {
3408 int nextf;
3409
3410 frame->grabstate = FRAME_DONE;
3411 wake_up_interruptible(&frame->wq);
3412
3413 /* If next frame is ready or grabbing,
3414 * point to it */
3415 nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
3416 if (ov->frame[nextf].grabstate == FRAME_READY
3417 || ov->frame[nextf].grabstate == FRAME_GRABBING) {
3418 ov->curframe = nextf;
3419 ov->frame[nextf].scanstate = STATE_SCANNING;
3420 frame = &ov->frame[nextf];
3421 } else {
3422 if (frame->grabstate == FRAME_DONE) {
3423 PDEBUG(4, "** Frame done **");
3424 } else {
3425 PDEBUG(4, "Frame not ready? state = %d",
3426 ov->frame[nextf].grabstate);
3427 }
3428
3429 ov->curframe = -1;
3430 PDEBUG(4, "SOF dropped (no active frame)");
3431 return; /* Nowhere to store this frame */
3432 }
3433 }
3434sof:
3435 PDEBUG(4, "Starting capture on frame %d", frame->framenum);
3436
3437// Snapshot not reverse-engineered yet.
3438#if 0
3439 /* Check to see if it's a snapshot frame */
3440 /* FIXME?? Should the snapshot reset go here? Performance? */
3441 if (in[8] & 0x02) {
3442 frame->snapshot = 1;
3443 PDEBUG(3, "snapshot detected");
3444 }
3445#endif
3446 frame->scanstate = STATE_LINES;
3447 frame->bytes_recvd = 0;
3448 frame->compressed = 1;
3449
3450check_middle:
3451 /* Are we in a frame? */
3452 if (frame->scanstate != STATE_LINES) {
3453 PDEBUG(4, "scanstate: no SOF yet");
3454 return;
3455 }
3456
3457 /* Dump all data exactly as received */
3458 if (dumppix == 2) {
3459 frame->bytes_recvd += n;
3460 if (frame->bytes_recvd <= max_raw)
3461 memcpy(frame->rawdata + frame->bytes_recvd - n, in, n);
3462 else
3463 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3464 frame->bytes_recvd - max_raw);
3465 } else {
3466 /* All incoming data are divided into 8-byte segments. If the
3467 * segment contains all zero bytes, it must be skipped. These
3468 * zero-segments allow the OV518 to mainain a constant data rate
3469 * regardless of the effectiveness of the compression. Segments
3470 * are aligned relative to the beginning of each isochronous
3471 * packet. The first segment in each image is a header (the
3472 * decompressor skips it later).
3473 */
3474
3475 int b, read = 0, allzero, copied = 0;
3476
3477 while (read < n) {
3478 allzero = 1;
3479 for (b = 0; b < 8; b++) {
3480 if (in[read + b]) {
3481 allzero = 0;
3482 break;
3483 }
3484 }
3485
3486 if (allzero) {
3487 /* Don't copy it */
3488 } else {
3489 if (frame->bytes_recvd + copied + 8 <= max_raw)
3490 {
3491 memcpy(frame->rawdata
3492 + frame->bytes_recvd + copied,
3493 in + read, 8);
3494 copied += 8;
3495 } else {
3496 PDEBUG(3, "Raw data buffer overrun!!");
3497 }
3498 }
3499 read += 8;
3500 }
3501 frame->bytes_recvd += copied;
3502 }
3503}
3504
3505static void
3506ov51x_isoc_irq(struct urb *urb, struct pt_regs *regs)
3507{
3508 int i;
3509 struct usb_ov511 *ov;
3510 struct ov511_sbuf *sbuf;
3511
3512 if (!urb->context) {
3513 PDEBUG(4, "no context");
3514 return;
3515 }
3516
3517 sbuf = urb->context;
3518 ov = sbuf->ov;
3519
3520 if (!ov || !ov->dev || !ov->user) {
3521 PDEBUG(4, "no device, or not open");
3522 return;
3523 }
3524
3525 if (!ov->streaming) {
3526 PDEBUG(4, "hmmm... not streaming, but got interrupt");
3527 return;
3528 }
3529
3530 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
3531 PDEBUG(4, "URB unlinked");
3532 return;
3533 }
3534
3535 if (urb->status != -EINPROGRESS && urb->status != 0) {
3536 err("ERROR: urb->status=%d: %s", urb->status,
3537 symbolic(urb_errlist, urb->status));
3538 }
3539
3540 /* Copy the data received into our frame buffer */
3541 PDEBUG(5, "sbuf[%d]: Moving %d packets", sbuf->n,
3542 urb->number_of_packets);
3543 for (i = 0; i < urb->number_of_packets; i++) {
3544 /* Warning: Don't call *_move_data() if no frame active! */
3545 if (ov->curframe >= 0) {
3546 int n = urb->iso_frame_desc[i].actual_length;
3547 int st = urb->iso_frame_desc[i].status;
3548 unsigned char *cdata;
3549
3550 urb->iso_frame_desc[i].actual_length = 0;
3551 urb->iso_frame_desc[i].status = 0;
3552
3553 cdata = urb->transfer_buffer
3554 + urb->iso_frame_desc[i].offset;
3555
3556 if (!n) {
3557 PDEBUG(4, "Zero-length packet");
3558 continue;
3559 }
3560
3561 if (st)
3562 PDEBUG(2, "data error: [%d] len=%d, status=%d",
3563 i, n, st);
3564
3565 if (ov->bclass == BCL_OV511)
3566 ov511_move_data(ov, cdata, n);
3567 else if (ov->bclass == BCL_OV518)
3568 ov518_move_data(ov, cdata, n);
3569 else
3570 err("Unknown bridge device (%d)", ov->bridge);
3571
3572 } else if (waitqueue_active(&ov->wq)) {
3573 wake_up_interruptible(&ov->wq);
3574 }
3575 }
3576
3577 /* Resubmit this URB */
3578 urb->dev = ov->dev;
3579 if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
3580 err("usb_submit_urb() ret %d", i);
3581
3582 return;
3583}
3584
3585/****************************************************************************
3586 *
3587 * Stream initialization and termination
3588 *
3589 ***************************************************************************/
3590
3591static int
3592ov51x_init_isoc(struct usb_ov511 *ov)
3593{
3594 struct urb *urb;
3595 int fx, err, n, size;
3596
3597 PDEBUG(3, "*** Initializing capture ***");
3598
3599 ov->curframe = -1;
3600
3601 if (ov->bridge == BRG_OV511) {
3602 if (cams == 1)
3603 size = 993;
3604 else if (cams == 2)
3605 size = 513;
3606 else if (cams == 3 || cams == 4)
3607 size = 257;
3608 else {
3609 err("\"cams\" parameter too high!");
3610 return -1;
3611 }
3612 } else if (ov->bridge == BRG_OV511PLUS) {
3613 if (cams == 1)
3614 size = 961;
3615 else if (cams == 2)
3616 size = 513;
3617 else if (cams == 3 || cams == 4)
3618 size = 257;
3619 else if (cams >= 5 && cams <= 8)
3620 size = 129;
3621 else if (cams >= 9 && cams <= 31)
3622 size = 33;
3623 else {
3624 err("\"cams\" parameter too high!");
3625 return -1;
3626 }
3627 } else if (ov->bclass == BCL_OV518) {
3628 if (cams == 1)
3629 size = 896;
3630 else if (cams == 2)
3631 size = 512;
3632 else if (cams == 3 || cams == 4)
3633 size = 256;
3634 else if (cams >= 5 && cams <= 8)
3635 size = 128;
3636 else {
3637 err("\"cams\" parameter too high!");
3638 return -1;
3639 }
3640 } else {
3641 err("invalid bridge type");
3642 return -1;
3643 }
3644
3645 // FIXME: OV518 is hardcoded to 15 FPS (alternate 5) for now
3646 if (ov->bclass == BCL_OV518) {
3647 if (packetsize == -1) {
3648 ov518_set_packet_size(ov, 640);
3649 } else {
3650 info("Forcing packet size to %d", packetsize);
3651 ov518_set_packet_size(ov, packetsize);
3652 }
3653 } else {
3654 if (packetsize == -1) {
3655 ov511_set_packet_size(ov, size);
3656 } else {
3657 info("Forcing packet size to %d", packetsize);
3658 ov511_set_packet_size(ov, packetsize);
3659 }
3660 }
3661
3662 for (n = 0; n < OV511_NUMSBUF; n++) {
3663 urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
3664 if (!urb) {
3665 err("init isoc: usb_alloc_urb ret. NULL");
3666 return -ENOMEM;
3667 }
3668 ov->sbuf[n].urb = urb;
3669 urb->dev = ov->dev;
3670 urb->context = &ov->sbuf[n];
3671 urb->pipe = usb_rcvisocpipe(ov->dev, OV511_ENDPOINT_ADDRESS);
3672 urb->transfer_flags = URB_ISO_ASAP;
3673 urb->transfer_buffer = ov->sbuf[n].data;
3674 urb->complete = ov51x_isoc_irq;
3675 urb->number_of_packets = FRAMES_PER_DESC;
3676 urb->transfer_buffer_length = ov->packet_size * FRAMES_PER_DESC;
3677 urb->interval = 1;
3678 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
3679 urb->iso_frame_desc[fx].offset = ov->packet_size * fx;
3680 urb->iso_frame_desc[fx].length = ov->packet_size;
3681 }
3682 }
3683
3684 ov->streaming = 1;
3685
3686 for (n = 0; n < OV511_NUMSBUF; n++) {
3687 ov->sbuf[n].urb->dev = ov->dev;
3688 err = usb_submit_urb(ov->sbuf[n].urb, GFP_KERNEL);
3689 if (err) {
3690 err("init isoc: usb_submit_urb(%d) ret %d", n, err);
3691 return err;
3692 }
3693 }
3694
3695 return 0;
3696}
3697
3698static void
3699ov51x_unlink_isoc(struct usb_ov511 *ov)
3700{
3701 int n;
3702
3703 /* Unschedule all of the iso td's */
3704 for (n = OV511_NUMSBUF - 1; n >= 0; n--) {
3705 if (ov->sbuf[n].urb) {
3706 usb_kill_urb(ov->sbuf[n].urb);
3707 usb_free_urb(ov->sbuf[n].urb);
3708 ov->sbuf[n].urb = NULL;
3709 }
3710 }
3711}
3712
3713static void
3714ov51x_stop_isoc(struct usb_ov511 *ov)
3715{
3716 if (!ov->streaming || !ov->dev)
3717 return;
3718
3719 PDEBUG(3, "*** Stopping capture ***");
3720
3721 if (ov->bclass == BCL_OV518)
3722 ov518_set_packet_size(ov, 0);
3723 else
3724 ov511_set_packet_size(ov, 0);
3725
3726 ov->streaming = 0;
3727
3728 ov51x_unlink_isoc(ov);
3729}
3730
3731static int
3732ov51x_new_frame(struct usb_ov511 *ov, int framenum)
3733{
3734 struct ov511_frame *frame;
3735 int newnum;
3736
3737 PDEBUG(4, "ov->curframe = %d, framenum = %d", ov->curframe, framenum);
3738
3739 if (!ov->dev)
3740 return -1;
3741
3742 /* If we're not grabbing a frame right now and the other frame is */
3743 /* ready to be grabbed into, then use it instead */
3744 if (ov->curframe == -1) {
3745 newnum = (framenum - 1 + OV511_NUMFRAMES) % OV511_NUMFRAMES;
3746 if (ov->frame[newnum].grabstate == FRAME_READY)
3747 framenum = newnum;
3748 } else
3749 return 0;
3750
3751 frame = &ov->frame[framenum];
3752
3753 PDEBUG(4, "framenum = %d, width = %d, height = %d", framenum,
3754 frame->width, frame->height);
3755
3756 frame->grabstate = FRAME_GRABBING;
3757 frame->scanstate = STATE_SCANNING;
3758 frame->snapshot = 0;
3759
3760 ov->curframe = framenum;
3761
3762 /* Make sure it's not too big */
3763 if (frame->width > ov->maxwidth)
3764 frame->width = ov->maxwidth;
3765
3766 frame->width &= ~7L; /* Multiple of 8 */
3767
3768 if (frame->height > ov->maxheight)
3769 frame->height = ov->maxheight;
3770
3771 frame->height &= ~3L; /* Multiple of 4 */
3772
3773 return 0;
3774}
3775
3776/****************************************************************************
3777 *
3778 * Buffer management
3779 *
3780 ***************************************************************************/
3781
3782/*
3783 * - You must acquire buf_lock before entering this function.
3784 * - Because this code will free any non-null pointer, you must be sure to null
3785 * them if you explicitly free them somewhere else!
3786 */
3787static void
3788ov51x_do_dealloc(struct usb_ov511 *ov)
3789{
3790 int i;
3791 PDEBUG(4, "entered");
3792
3793 if (ov->fbuf) {
3794 rvfree(ov->fbuf, OV511_NUMFRAMES
3795 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
3796 ov->fbuf = NULL;
3797 }
3798
3799 vfree(ov->rawfbuf);
3800 ov->rawfbuf = NULL;
3801
3802 vfree(ov->tempfbuf);
3803 ov->tempfbuf = NULL;
3804
3805 for (i = 0; i < OV511_NUMSBUF; i++) {
3806 kfree(ov->sbuf[i].data);
3807 ov->sbuf[i].data = NULL;
3808 }
3809
3810 for (i = 0; i < OV511_NUMFRAMES; i++) {
3811 ov->frame[i].data = NULL;
3812 ov->frame[i].rawdata = NULL;
3813 ov->frame[i].tempdata = NULL;
3814 if (ov->frame[i].compbuf) {
3815 free_page((unsigned long) ov->frame[i].compbuf);
3816 ov->frame[i].compbuf = NULL;
3817 }
3818 }
3819
3820 PDEBUG(4, "buffer memory deallocated");
3821 ov->buf_state = BUF_NOT_ALLOCATED;
3822 PDEBUG(4, "leaving");
3823}
3824
3825static int
3826ov51x_alloc(struct usb_ov511 *ov)
3827{
3828 int i;
3829 const int w = ov->maxwidth;
3830 const int h = ov->maxheight;
3831 const int data_bufsize = OV511_NUMFRAMES * MAX_DATA_SIZE(w, h);
3832 const int raw_bufsize = OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h);
3833
3834 PDEBUG(4, "entered");
3835 down(&ov->buf_lock);
3836
3837 if (ov->buf_state == BUF_ALLOCATED)
3838 goto out;
3839
3840 ov->fbuf = rvmalloc(data_bufsize);
3841 if (!ov->fbuf)
3842 goto error;
3843
3844 ov->rawfbuf = vmalloc(raw_bufsize);
3845 if (!ov->rawfbuf)
3846 goto error;
3847
3848 memset(ov->rawfbuf, 0, raw_bufsize);
3849
3850 ov->tempfbuf = vmalloc(raw_bufsize);
3851 if (!ov->tempfbuf)
3852 goto error;
3853
3854 memset(ov->tempfbuf, 0, raw_bufsize);
3855
3856 for (i = 0; i < OV511_NUMSBUF; i++) {
3857 ov->sbuf[i].data = kmalloc(FRAMES_PER_DESC *
3858 MAX_FRAME_SIZE_PER_DESC, GFP_KERNEL);
3859 if (!ov->sbuf[i].data)
3860 goto error;
3861
3862 PDEBUG(4, "sbuf[%d] @ %p", i, ov->sbuf[i].data);
3863 }
3864
3865 for (i = 0; i < OV511_NUMFRAMES; i++) {
3866 ov->frame[i].data = ov->fbuf + i * MAX_DATA_SIZE(w, h);
3867 ov->frame[i].rawdata = ov->rawfbuf
3868 + i * MAX_RAW_DATA_SIZE(w, h);
3869 ov->frame[i].tempdata = ov->tempfbuf
3870 + i * MAX_RAW_DATA_SIZE(w, h);
3871
3872 ov->frame[i].compbuf =
3873 (unsigned char *) __get_free_page(GFP_KERNEL);
3874 if (!ov->frame[i].compbuf)
3875 goto error;
3876
3877 PDEBUG(4, "frame[%d] @ %p", i, ov->frame[i].data);
3878 }
3879
3880 ov->buf_state = BUF_ALLOCATED;
3881out:
3882 up(&ov->buf_lock);
3883 PDEBUG(4, "leaving");
3884 return 0;
3885error:
3886 ov51x_do_dealloc(ov);
3887 up(&ov->buf_lock);
3888 PDEBUG(4, "errored");
3889 return -ENOMEM;
3890}
3891
3892static void
3893ov51x_dealloc(struct usb_ov511 *ov)
3894{
3895 PDEBUG(4, "entered");
3896 down(&ov->buf_lock);
3897 ov51x_do_dealloc(ov);
3898 up(&ov->buf_lock);
3899 PDEBUG(4, "leaving");
3900}
3901
3902/****************************************************************************
3903 *
3904 * V4L 1 API
3905 *
3906 ***************************************************************************/
3907
3908static int
3909ov51x_v4l1_open(struct inode *inode, struct file *file)
3910{
3911 struct video_device *vdev = video_devdata(file);
3912 struct usb_ov511 *ov = video_get_drvdata(vdev);
3913 int err, i;
3914
3915 PDEBUG(4, "opening");
3916
3917 down(&ov->lock);
3918
3919 err = -EBUSY;
3920 if (ov->user)
3921 goto out;
3922
3923 ov->sub_flag = 0;
3924
3925 /* In case app doesn't set them... */
3926 err = ov51x_set_default_params(ov);
3927 if (err < 0)
3928 goto out;
3929
3930 /* Make sure frames are reset */
3931 for (i = 0; i < OV511_NUMFRAMES; i++) {
3932 ov->frame[i].grabstate = FRAME_UNUSED;
3933 ov->frame[i].bytes_read = 0;
3934 }
3935
3936 /* If compression is on, make sure now that a
3937 * decompressor can be loaded */
3938 if (ov->compress && !ov->decomp_ops) {
3939 err = request_decompressor(ov);
3940 if (err && !dumppix)
3941 goto out;
3942 }
3943
3944 err = ov51x_alloc(ov);
3945 if (err < 0)
3946 goto out;
3947
3948 err = ov51x_init_isoc(ov);
3949 if (err) {
3950 ov51x_dealloc(ov);
3951 goto out;
3952 }
3953
3954 ov->user++;
3955 file->private_data = vdev;
3956
3957 if (ov->led_policy == LED_AUTO)
3958 ov51x_led_control(ov, 1);
3959
3960out:
3961 up(&ov->lock);
3962 return err;
3963}
3964
3965static int
3966ov51x_v4l1_close(struct inode *inode, struct file *file)
3967{
3968 struct video_device *vdev = file->private_data;
3969 struct usb_ov511 *ov = video_get_drvdata(vdev);
3970
3971 PDEBUG(4, "ov511_close");
3972
3973 down(&ov->lock);
3974
3975 ov->user--;
3976 ov51x_stop_isoc(ov);
3977
3978 if (ov->led_policy == LED_AUTO)
3979 ov51x_led_control(ov, 0);
3980
3981 if (ov->dev)
3982 ov51x_dealloc(ov);
3983
3984 up(&ov->lock);
3985
3986 /* Device unplugged while open. Only a minimum of unregistration is done
3987 * here; the disconnect callback already did the rest. */
3988 if (!ov->dev) {
3989 down(&ov->cbuf_lock);
3990 kfree(ov->cbuf);
3991 ov->cbuf = NULL;
3992 up(&ov->cbuf_lock);
3993
3994 ov51x_dealloc(ov);
3995 kfree(ov);
3996 ov = NULL;
3997 }
3998
3999 file->private_data = NULL;
4000 return 0;
4001}
4002
4003/* Do not call this function directly! */
4004static int
4005ov51x_v4l1_ioctl_internal(struct inode *inode, struct file *file,
4006 unsigned int cmd, void *arg)
4007{
4008 struct video_device *vdev = file->private_data;
4009 struct usb_ov511 *ov = video_get_drvdata(vdev);
4010 PDEBUG(5, "IOCtl: 0x%X", cmd);
4011
4012 if (!ov->dev)
4013 return -EIO;
4014
4015 switch (cmd) {
4016 case VIDIOCGCAP:
4017 {
4018 struct video_capability *b = arg;
4019
4020 PDEBUG(4, "VIDIOCGCAP");
4021
4022 memset(b, 0, sizeof(struct video_capability));
4023 sprintf(b->name, "%s USB Camera",
4024 symbolic(brglist, ov->bridge));
4025 b->type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE;
4026 b->channels = ov->num_inputs;
4027 b->audios = 0;
4028 b->maxwidth = ov->maxwidth;
4029 b->maxheight = ov->maxheight;
4030 b->minwidth = ov->minwidth;
4031 b->minheight = ov->minheight;
4032
4033 return 0;
4034 }
4035 case VIDIOCGCHAN:
4036 {
4037 struct video_channel *v = arg;
4038
4039 PDEBUG(4, "VIDIOCGCHAN");
4040
4041 if ((unsigned)(v->channel) >= ov->num_inputs) {
4042 err("Invalid channel (%d)", v->channel);
4043 return -EINVAL;
4044 }
4045
4046 v->norm = ov->norm;
4047 v->type = VIDEO_TYPE_CAMERA;
4048 v->flags = 0;
4049// v->flags |= (ov->has_decoder) ? VIDEO_VC_NORM : 0;
4050 v->tuners = 0;
4051 decoder_get_input_name(ov, v->channel, v->name);
4052
4053 return 0;
4054 }
4055 case VIDIOCSCHAN:
4056 {
4057 struct video_channel *v = arg;
4058 int err;
4059
4060 PDEBUG(4, "VIDIOCSCHAN");
4061
4062 /* Make sure it's not a camera */
4063 if (!ov->has_decoder) {
4064 if (v->channel == 0)
4065 return 0;
4066 else
4067 return -EINVAL;
4068 }
4069
4070 if (v->norm != VIDEO_MODE_PAL &&
4071 v->norm != VIDEO_MODE_NTSC &&
4072 v->norm != VIDEO_MODE_SECAM &&
4073 v->norm != VIDEO_MODE_AUTO) {
4074 err("Invalid norm (%d)", v->norm);
4075 return -EINVAL;
4076 }
4077
4078 if ((unsigned)(v->channel) >= ov->num_inputs) {
4079 err("Invalid channel (%d)", v->channel);
4080 return -EINVAL;
4081 }
4082
4083 err = decoder_set_input(ov, v->channel);
4084 if (err)
4085 return err;
4086
4087 err = decoder_set_norm(ov, v->norm);
4088 if (err)
4089 return err;
4090
4091 return 0;
4092 }
4093 case VIDIOCGPICT:
4094 {
4095 struct video_picture *p = arg;
4096
4097 PDEBUG(4, "VIDIOCGPICT");
4098
4099 memset(p, 0, sizeof(struct video_picture));
4100 if (sensor_get_picture(ov, p))
4101 return -EIO;
4102
4103 /* Can we get these from frame[0]? -claudio? */
4104 p->depth = ov->frame[0].depth;
4105 p->palette = ov->frame[0].format;
4106
4107 return 0;
4108 }
4109 case VIDIOCSPICT:
4110 {
4111 struct video_picture *p = arg;
4112 int i, rc;
4113
4114 PDEBUG(4, "VIDIOCSPICT");
4115
4116 if (!get_depth(p->palette))
4117 return -EINVAL;
4118
4119 if (sensor_set_picture(ov, p))
4120 return -EIO;
4121
4122 if (force_palette && p->palette != force_palette) {
4123 info("Palette rejected (%s)",
4124 symbolic(v4l1_plist, p->palette));
4125 return -EINVAL;
4126 }
4127
4128 // FIXME: Format should be independent of frames
4129 if (p->palette != ov->frame[0].format) {
4130 PDEBUG(4, "Detected format change");
4131
4132 rc = ov51x_wait_frames_inactive(ov);
4133 if (rc)
4134 return rc;
4135
4136 mode_init_regs(ov, ov->frame[0].width,
4137 ov->frame[0].height, p->palette, ov->sub_flag);
4138 }
4139
4140 PDEBUG(4, "Setting depth=%d, palette=%s",
4141 p->depth, symbolic(v4l1_plist, p->palette));
4142
4143 for (i = 0; i < OV511_NUMFRAMES; i++) {
4144 ov->frame[i].depth = p->depth;
4145 ov->frame[i].format = p->palette;
4146 }
4147
4148 return 0;
4149 }
4150 case VIDIOCGCAPTURE:
4151 {
4152 int *vf = arg;
4153
4154 PDEBUG(4, "VIDIOCGCAPTURE");
4155
4156 ov->sub_flag = *vf;
4157 return 0;
4158 }
4159 case VIDIOCSCAPTURE:
4160 {
4161 struct video_capture *vc = arg;
4162
4163 PDEBUG(4, "VIDIOCSCAPTURE");
4164
4165 if (vc->flags)
4166 return -EINVAL;
4167 if (vc->decimation)
4168 return -EINVAL;
4169
4170 vc->x &= ~3L;
4171 vc->y &= ~1L;
4172 vc->y &= ~31L;
4173
4174 if (vc->width == 0)
4175 vc->width = 32;
4176
4177 vc->height /= 16;
4178 vc->height *= 16;
4179 if (vc->height == 0)
4180 vc->height = 16;
4181
4182 ov->subx = vc->x;
4183 ov->suby = vc->y;
4184 ov->subw = vc->width;
4185 ov->subh = vc->height;
4186
4187 return 0;
4188 }
4189 case VIDIOCSWIN:
4190 {
4191 struct video_window *vw = arg;
4192 int i, rc;
4193
4194 PDEBUG(4, "VIDIOCSWIN: %dx%d", vw->width, vw->height);
4195
4196#if 0
4197 if (vw->flags)
4198 return -EINVAL;
4199 if (vw->clipcount)
4200 return -EINVAL;
4201 if (vw->height != ov->maxheight)
4202 return -EINVAL;
4203 if (vw->width != ov->maxwidth)
4204 return -EINVAL;
4205#endif
4206
4207 rc = ov51x_wait_frames_inactive(ov);
4208 if (rc)
4209 return rc;
4210
4211 rc = mode_init_regs(ov, vw->width, vw->height,
4212 ov->frame[0].format, ov->sub_flag);
4213 if (rc < 0)
4214 return rc;
4215
4216 for (i = 0; i < OV511_NUMFRAMES; i++) {
4217 ov->frame[i].width = vw->width;
4218 ov->frame[i].height = vw->height;
4219 }
4220
4221 return 0;
4222 }
4223 case VIDIOCGWIN:
4224 {
4225 struct video_window *vw = arg;
4226
4227 memset(vw, 0, sizeof(struct video_window));
4228 vw->x = 0; /* FIXME */
4229 vw->y = 0;
4230 vw->width = ov->frame[0].width;
4231 vw->height = ov->frame[0].height;
4232 vw->flags = 30;
4233
4234 PDEBUG(4, "VIDIOCGWIN: %dx%d", vw->width, vw->height);
4235
4236 return 0;
4237 }
4238 case VIDIOCGMBUF:
4239 {
4240 struct video_mbuf *vm = arg;
4241 int i;
4242
4243 PDEBUG(4, "VIDIOCGMBUF");
4244
4245 memset(vm, 0, sizeof(struct video_mbuf));
4246 vm->size = OV511_NUMFRAMES
4247 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
4248 vm->frames = OV511_NUMFRAMES;
4249
4250 vm->offsets[0] = 0;
4251 for (i = 1; i < OV511_NUMFRAMES; i++) {
4252 vm->offsets[i] = vm->offsets[i-1]
4253 + MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
4254 }
4255
4256 return 0;
4257 }
4258 case VIDIOCMCAPTURE:
4259 {
4260 struct video_mmap *vm = arg;
4261 int rc, depth;
4262 unsigned int f = vm->frame;
4263
4264 PDEBUG(4, "VIDIOCMCAPTURE: frame: %d, %dx%d, %s", f, vm->width,
4265 vm->height, symbolic(v4l1_plist, vm->format));
4266
4267 depth = get_depth(vm->format);
4268 if (!depth) {
4269 PDEBUG(2, "VIDIOCMCAPTURE: invalid format (%s)",
4270 symbolic(v4l1_plist, vm->format));
4271 return -EINVAL;
4272 }
4273
4274 if (f >= OV511_NUMFRAMES) {
4275 err("VIDIOCMCAPTURE: invalid frame (%d)", f);
4276 return -EINVAL;
4277 }
4278
4279 if (vm->width > ov->maxwidth
4280 || vm->height > ov->maxheight) {
4281 err("VIDIOCMCAPTURE: requested dimensions too big");
4282 return -EINVAL;
4283 }
4284
4285 if (ov->frame[f].grabstate == FRAME_GRABBING) {
4286 PDEBUG(4, "VIDIOCMCAPTURE: already grabbing");
4287 return -EBUSY;
4288 }
4289
4290 if (force_palette && (vm->format != force_palette)) {
4291 PDEBUG(2, "palette rejected (%s)",
4292 symbolic(v4l1_plist, vm->format));
4293 return -EINVAL;
4294 }
4295
4296 if ((ov->frame[f].width != vm->width) ||
4297 (ov->frame[f].height != vm->height) ||
4298 (ov->frame[f].format != vm->format) ||
4299 (ov->frame[f].sub_flag != ov->sub_flag) ||
4300 (ov->frame[f].depth != depth)) {
4301 PDEBUG(4, "VIDIOCMCAPTURE: change in image parameters");
4302
4303 rc = ov51x_wait_frames_inactive(ov);
4304 if (rc)
4305 return rc;
4306
4307 rc = mode_init_regs(ov, vm->width, vm->height,
4308 vm->format, ov->sub_flag);
4309#if 0
4310 if (rc < 0) {
4311 PDEBUG(1, "Got error while initializing regs ");
4312 return ret;
4313 }
4314#endif
4315 ov->frame[f].width = vm->width;
4316 ov->frame[f].height = vm->height;
4317 ov->frame[f].format = vm->format;
4318 ov->frame[f].sub_flag = ov->sub_flag;
4319 ov->frame[f].depth = depth;
4320 }
4321
4322 /* Mark it as ready */
4323 ov->frame[f].grabstate = FRAME_READY;
4324
4325 PDEBUG(4, "VIDIOCMCAPTURE: renewing frame %d", f);
4326
4327 return ov51x_new_frame(ov, f);
4328 }
4329 case VIDIOCSYNC:
4330 {
4331 unsigned int fnum = *((unsigned int *) arg);
4332 struct ov511_frame *frame;
4333 int rc;
4334
4335 if (fnum >= OV511_NUMFRAMES) {
4336 err("VIDIOCSYNC: invalid frame (%d)", fnum);
4337 return -EINVAL;
4338 }
4339
4340 frame = &ov->frame[fnum];
4341
4342 PDEBUG(4, "syncing to frame %d, grabstate = %d", fnum,
4343 frame->grabstate);
4344
4345 switch (frame->grabstate) {
4346 case FRAME_UNUSED:
4347 return -EINVAL;
4348 case FRAME_READY:
4349 case FRAME_GRABBING:
4350 case FRAME_ERROR:
4351redo:
4352 if (!ov->dev)
4353 return -EIO;
4354
4355 rc = wait_event_interruptible(frame->wq,
4356 (frame->grabstate == FRAME_DONE)
4357 || (frame->grabstate == FRAME_ERROR));
4358
4359 if (rc)
4360 return rc;
4361
4362 if (frame->grabstate == FRAME_ERROR) {
4363 if ((rc = ov51x_new_frame(ov, fnum)) < 0)
4364 return rc;
4365 goto redo;
4366 }
4367 /* Fall through */
4368 case FRAME_DONE:
4369 if (ov->snap_enabled && !frame->snapshot) {
4370 if ((rc = ov51x_new_frame(ov, fnum)) < 0)
4371 return rc;
4372 goto redo;
4373 }
4374
4375 frame->grabstate = FRAME_UNUSED;
4376
4377 /* Reset the hardware snapshot button */
4378 /* FIXME - Is this the best place for this? */
4379 if ((ov->snap_enabled) && (frame->snapshot)) {
4380 frame->snapshot = 0;
4381 ov51x_clear_snapshot(ov);
4382 }
4383
4384 /* Decompression, format conversion, etc... */
4385 ov51x_postprocess(ov, frame);
4386
4387 break;
4388 } /* end switch */
4389
4390 return 0;
4391 }
4392 case VIDIOCGFBUF:
4393 {
4394 struct video_buffer *vb = arg;
4395
4396 PDEBUG(4, "VIDIOCGFBUF");
4397
4398 memset(vb, 0, sizeof(struct video_buffer));
4399
4400 return 0;
4401 }
4402 case VIDIOCGUNIT:
4403 {
4404 struct video_unit *vu = arg;
4405
4406 PDEBUG(4, "VIDIOCGUNIT");
4407
4408 memset(vu, 0, sizeof(struct video_unit));
4409
4410 vu->video = ov->vdev->minor;
4411 vu->vbi = VIDEO_NO_UNIT;
4412 vu->radio = VIDEO_NO_UNIT;
4413 vu->audio = VIDEO_NO_UNIT;
4414 vu->teletext = VIDEO_NO_UNIT;
4415
4416 return 0;
4417 }
4418 case OV511IOC_WI2C:
4419 {
4420 struct ov511_i2c_struct *w = arg;
4421
4422 return i2c_w_slave(ov, w->slave, w->reg, w->value, w->mask);
4423 }
4424 case OV511IOC_RI2C:
4425 {
4426 struct ov511_i2c_struct *r = arg;
4427 int rc;
4428
4429 rc = i2c_r_slave(ov, r->slave, r->reg);
4430 if (rc < 0)
4431 return rc;
4432
4433 r->value = rc;
4434 return 0;
4435 }
4436 default:
4437 PDEBUG(3, "Unsupported IOCtl: 0x%X", cmd);
4438 return -ENOIOCTLCMD;
4439 } /* end switch */
4440
4441 return 0;
4442}
4443
4444static int
4445ov51x_v4l1_ioctl(struct inode *inode, struct file *file,
4446 unsigned int cmd, unsigned long arg)
4447{
4448 struct video_device *vdev = file->private_data;
4449 struct usb_ov511 *ov = video_get_drvdata(vdev);
4450 int rc;
4451
4452 if (down_interruptible(&ov->lock))
4453 return -EINTR;
4454
4455 rc = video_usercopy(inode, file, cmd, arg, ov51x_v4l1_ioctl_internal);
4456
4457 up(&ov->lock);
4458 return rc;
4459}
4460
4461static ssize_t
4462ov51x_v4l1_read(struct file *file, char __user *buf, size_t cnt, loff_t *ppos)
4463{
4464 struct video_device *vdev = file->private_data;
4465 int noblock = file->f_flags&O_NONBLOCK;
4466 unsigned long count = cnt;
4467 struct usb_ov511 *ov = video_get_drvdata(vdev);
4468 int i, rc = 0, frmx = -1;
4469 struct ov511_frame *frame;
4470
4471 if (down_interruptible(&ov->lock))
4472 return -EINTR;
4473
4474 PDEBUG(4, "%ld bytes, noblock=%d", count, noblock);
4475
4476 if (!vdev || !buf) {
4477 rc = -EFAULT;
4478 goto error;
4479 }
4480
4481 if (!ov->dev) {
4482 rc = -EIO;
4483 goto error;
4484 }
4485
4486// FIXME: Only supports two frames
4487 /* See if a frame is completed, then use it. */
4488 if (ov->frame[0].grabstate >= FRAME_DONE) /* _DONE or _ERROR */
4489 frmx = 0;
4490 else if (ov->frame[1].grabstate >= FRAME_DONE)/* _DONE or _ERROR */
4491 frmx = 1;
4492
4493 /* If nonblocking we return immediately */
4494 if (noblock && (frmx == -1)) {
4495 rc = -EAGAIN;
4496 goto error;
4497 }
4498
4499 /* If no FRAME_DONE, look for a FRAME_GRABBING state. */
4500 /* See if a frame is in process (grabbing), then use it. */
4501 if (frmx == -1) {
4502 if (ov->frame[0].grabstate == FRAME_GRABBING)
4503 frmx = 0;
4504 else if (ov->frame[1].grabstate == FRAME_GRABBING)
4505 frmx = 1;
4506 }
4507
4508 /* If no frame is active, start one. */
4509 if (frmx == -1) {
4510 if ((rc = ov51x_new_frame(ov, frmx = 0))) {
4511 err("read: ov51x_new_frame error");
4512 goto error;
4513 }
4514 }
4515
4516 frame = &ov->frame[frmx];
4517
4518restart:
4519 if (!ov->dev) {
4520 rc = -EIO;
4521 goto error;
4522 }
4523
4524 /* Wait while we're grabbing the image */
4525 PDEBUG(4, "Waiting image grabbing");
4526 rc = wait_event_interruptible(frame->wq,
4527 (frame->grabstate == FRAME_DONE)
4528 || (frame->grabstate == FRAME_ERROR));
4529
4530 if (rc)
4531 goto error;
4532
4533 PDEBUG(4, "Got image, frame->grabstate = %d", frame->grabstate);
4534 PDEBUG(4, "bytes_recvd = %d", frame->bytes_recvd);
4535
4536 if (frame->grabstate == FRAME_ERROR) {
4537 frame->bytes_read = 0;
4538 err("** ick! ** Errored frame %d", ov->curframe);
4539 if (ov51x_new_frame(ov, frmx)) {
4540 err("read: ov51x_new_frame error");
4541 goto error;
4542 }
4543 goto restart;
4544 }
4545
4546
4547 /* Repeat until we get a snapshot frame */
4548 if (ov->snap_enabled)
4549 PDEBUG(4, "Waiting snapshot frame");
4550 if (ov->snap_enabled && !frame->snapshot) {
4551 frame->bytes_read = 0;
4552 if ((rc = ov51x_new_frame(ov, frmx))) {
4553 err("read: ov51x_new_frame error");
4554 goto error;
4555 }
4556 goto restart;
4557 }
4558
4559 /* Clear the snapshot */
4560 if (ov->snap_enabled && frame->snapshot) {
4561 frame->snapshot = 0;
4562 ov51x_clear_snapshot(ov);
4563 }
4564
4565 /* Decompression, format conversion, etc... */
4566 ov51x_postprocess(ov, frame);
4567
4568 PDEBUG(4, "frmx=%d, bytes_read=%ld, length=%ld", frmx,
4569 frame->bytes_read,
4570 get_frame_length(frame));
4571
4572 /* copy bytes to user space; we allow for partials reads */
4573// if ((count + frame->bytes_read)
4574// > get_frame_length((struct ov511_frame *)frame))
4575// count = frame->scanlength - frame->bytes_read;
4576
4577 /* FIXME - count hardwired to be one frame... */
4578 count = get_frame_length(frame);
4579
4580 PDEBUG(4, "Copy to user space: %ld bytes", count);
4581 if ((i = copy_to_user(buf, frame->data + frame->bytes_read, count))) {
4582 PDEBUG(4, "Copy failed! %d bytes not copied", i);
4583 rc = -EFAULT;
4584 goto error;
4585 }
4586
4587 frame->bytes_read += count;
4588 PDEBUG(4, "{copy} count used=%ld, new bytes_read=%ld",
4589 count, frame->bytes_read);
4590
4591 /* If all data have been read... */
4592 if (frame->bytes_read
4593 >= get_frame_length(frame)) {
4594 frame->bytes_read = 0;
4595
4596// FIXME: Only supports two frames
4597 /* Mark it as available to be used again. */
4598 ov->frame[frmx].grabstate = FRAME_UNUSED;
4599 if ((rc = ov51x_new_frame(ov, !frmx))) {
4600 err("ov51x_new_frame returned error");
4601 goto error;
4602 }
4603 }
4604
4605 PDEBUG(4, "read finished, returning %ld (sweet)", count);
4606
4607 up(&ov->lock);
4608 return count;
4609
4610error:
4611 up(&ov->lock);
4612 return rc;
4613}
4614
4615static int
4616ov51x_v4l1_mmap(struct file *file, struct vm_area_struct *vma)
4617{
4618 struct video_device *vdev = file->private_data;
4619 unsigned long start = vma->vm_start;
4620 unsigned long size = vma->vm_end - vma->vm_start;
4621 struct usb_ov511 *ov = video_get_drvdata(vdev);
4622 unsigned long page, pos;
4623
4624 if (ov->dev == NULL)
4625 return -EIO;
4626
4627 PDEBUG(4, "mmap: %ld (%lX) bytes", size, size);
4628
4629 if (size > (((OV511_NUMFRAMES
4630 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight)
4631 + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))))
4632 return -EINVAL;
4633
4634 if (down_interruptible(&ov->lock))
4635 return -EINTR;
4636
4637 pos = (unsigned long)ov->fbuf;
4638 while (size > 0) {
4639 page = vmalloc_to_pfn((void *)pos);
4640 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
4641 up(&ov->lock);
4642 return -EAGAIN;
4643 }
4644 start += PAGE_SIZE;
4645 pos += PAGE_SIZE;
4646 if (size > PAGE_SIZE)
4647 size -= PAGE_SIZE;
4648 else
4649 size = 0;
4650 }
4651
4652 up(&ov->lock);
4653 return 0;
4654}
4655
4656static struct file_operations ov511_fops = {
4657 .owner = THIS_MODULE,
4658 .open = ov51x_v4l1_open,
4659 .release = ov51x_v4l1_close,
4660 .read = ov51x_v4l1_read,
4661 .mmap = ov51x_v4l1_mmap,
4662 .ioctl = ov51x_v4l1_ioctl,
4663 .compat_ioctl = v4l_compat_ioctl32,
4664 .llseek = no_llseek,
4665};
4666
4667static struct video_device vdev_template = {
4668 .owner = THIS_MODULE,
4669 .name = "OV511 USB Camera",
4670 .type = VID_TYPE_CAPTURE,
4671 .hardware = VID_HARDWARE_OV511,
4672 .fops = &ov511_fops,
4673 .release = video_device_release,
4674 .minor = -1,
4675};
4676
4677/****************************************************************************
4678 *
4679 * OV511 and sensor configuration
4680 *
4681 ***************************************************************************/
4682
4683/* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
4684 * the same register settings as the OV7610, since they are very similar.
4685 */
4686static int
4687ov7xx0_configure(struct usb_ov511 *ov)
4688{
4689 int i, success;
4690 int rc;
4691
4692 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
4693 *
4694 * Register 0x0f in the 7610 has the following effects:
4695 *
4696 * 0x85 (AEC method 1): Best overall, good contrast range
4697 * 0x45 (AEC method 2): Very overexposed
4698 * 0xa5 (spec sheet default): Ok, but the black level is
4699 * shifted resulting in loss of contrast
4700 * 0x05 (old driver setting): very overexposed, too much
4701 * contrast
4702 */
4703 static struct ov511_regvals aRegvalsNorm7610[] = {
4704 { OV511_I2C_BUS, 0x10, 0xff },
4705 { OV511_I2C_BUS, 0x16, 0x06 },
4706 { OV511_I2C_BUS, 0x28, 0x24 },
4707 { OV511_I2C_BUS, 0x2b, 0xac },
4708 { OV511_I2C_BUS, 0x12, 0x00 },
4709 { OV511_I2C_BUS, 0x38, 0x81 },
4710 { OV511_I2C_BUS, 0x28, 0x24 }, /* 0c */
4711 { OV511_I2C_BUS, 0x0f, 0x85 }, /* lg's setting */
4712 { OV511_I2C_BUS, 0x15, 0x01 },
4713 { OV511_I2C_BUS, 0x20, 0x1c },
4714 { OV511_I2C_BUS, 0x23, 0x2a },
4715 { OV511_I2C_BUS, 0x24, 0x10 },
4716 { OV511_I2C_BUS, 0x25, 0x8a },
4717 { OV511_I2C_BUS, 0x26, 0xa2 },
4718 { OV511_I2C_BUS, 0x27, 0xc2 },
4719 { OV511_I2C_BUS, 0x2a, 0x04 },
4720 { OV511_I2C_BUS, 0x2c, 0xfe },
4721 { OV511_I2C_BUS, 0x2d, 0x93 },
4722 { OV511_I2C_BUS, 0x30, 0x71 },
4723 { OV511_I2C_BUS, 0x31, 0x60 },
4724 { OV511_I2C_BUS, 0x32, 0x26 },
4725 { OV511_I2C_BUS, 0x33, 0x20 },
4726 { OV511_I2C_BUS, 0x34, 0x48 },
4727 { OV511_I2C_BUS, 0x12, 0x24 },
4728 { OV511_I2C_BUS, 0x11, 0x01 },
4729 { OV511_I2C_BUS, 0x0c, 0x24 },
4730 { OV511_I2C_BUS, 0x0d, 0x24 },
4731 { OV511_DONE_BUS, 0x0, 0x00 },
4732 };
4733
4734 static struct ov511_regvals aRegvalsNorm7620[] = {
4735 { OV511_I2C_BUS, 0x00, 0x00 },
4736 { OV511_I2C_BUS, 0x01, 0x80 },
4737 { OV511_I2C_BUS, 0x02, 0x80 },
4738 { OV511_I2C_BUS, 0x03, 0xc0 },
4739 { OV511_I2C_BUS, 0x06, 0x60 },
4740 { OV511_I2C_BUS, 0x07, 0x00 },
4741 { OV511_I2C_BUS, 0x0c, 0x24 },
4742 { OV511_I2C_BUS, 0x0c, 0x24 },
4743 { OV511_I2C_BUS, 0x0d, 0x24 },
4744 { OV511_I2C_BUS, 0x11, 0x01 },
4745 { OV511_I2C_BUS, 0x12, 0x24 },
4746 { OV511_I2C_BUS, 0x13, 0x01 },
4747 { OV511_I2C_BUS, 0x14, 0x84 },
4748 { OV511_I2C_BUS, 0x15, 0x01 },
4749 { OV511_I2C_BUS, 0x16, 0x03 },
4750 { OV511_I2C_BUS, 0x17, 0x2f },
4751 { OV511_I2C_BUS, 0x18, 0xcf },
4752 { OV511_I2C_BUS, 0x19, 0x06 },
4753 { OV511_I2C_BUS, 0x1a, 0xf5 },
4754 { OV511_I2C_BUS, 0x1b, 0x00 },
4755 { OV511_I2C_BUS, 0x20, 0x18 },
4756 { OV511_I2C_BUS, 0x21, 0x80 },
4757 { OV511_I2C_BUS, 0x22, 0x80 },
4758 { OV511_I2C_BUS, 0x23, 0x00 },
4759 { OV511_I2C_BUS, 0x26, 0xa2 },
4760 { OV511_I2C_BUS, 0x27, 0xea },
4761 { OV511_I2C_BUS, 0x28, 0x20 },
4762 { OV511_I2C_BUS, 0x29, 0x00 },
4763 { OV511_I2C_BUS, 0x2a, 0x10 },
4764 { OV511_I2C_BUS, 0x2b, 0x00 },
4765 { OV511_I2C_BUS, 0x2c, 0x88 },
4766 { OV511_I2C_BUS, 0x2d, 0x91 },
4767 { OV511_I2C_BUS, 0x2e, 0x80 },
4768 { OV511_I2C_BUS, 0x2f, 0x44 },
4769 { OV511_I2C_BUS, 0x60, 0x27 },
4770 { OV511_I2C_BUS, 0x61, 0x02 },
4771 { OV511_I2C_BUS, 0x62, 0x5f },
4772 { OV511_I2C_BUS, 0x63, 0xd5 },
4773 { OV511_I2C_BUS, 0x64, 0x57 },
4774 { OV511_I2C_BUS, 0x65, 0x83 },
4775 { OV511_I2C_BUS, 0x66, 0x55 },
4776 { OV511_I2C_BUS, 0x67, 0x92 },
4777 { OV511_I2C_BUS, 0x68, 0xcf },
4778 { OV511_I2C_BUS, 0x69, 0x76 },
4779 { OV511_I2C_BUS, 0x6a, 0x22 },
4780 { OV511_I2C_BUS, 0x6b, 0x00 },
4781 { OV511_I2C_BUS, 0x6c, 0x02 },
4782 { OV511_I2C_BUS, 0x6d, 0x44 },
4783 { OV511_I2C_BUS, 0x6e, 0x80 },
4784 { OV511_I2C_BUS, 0x6f, 0x1d },
4785 { OV511_I2C_BUS, 0x70, 0x8b },
4786 { OV511_I2C_BUS, 0x71, 0x00 },
4787 { OV511_I2C_BUS, 0x72, 0x14 },
4788 { OV511_I2C_BUS, 0x73, 0x54 },
4789 { OV511_I2C_BUS, 0x74, 0x00 },
4790 { OV511_I2C_BUS, 0x75, 0x8e },
4791 { OV511_I2C_BUS, 0x76, 0x00 },
4792 { OV511_I2C_BUS, 0x77, 0xff },
4793 { OV511_I2C_BUS, 0x78, 0x80 },
4794 { OV511_I2C_BUS, 0x79, 0x80 },
4795 { OV511_I2C_BUS, 0x7a, 0x80 },
4796 { OV511_I2C_BUS, 0x7b, 0xe2 },
4797 { OV511_I2C_BUS, 0x7c, 0x00 },
4798 { OV511_DONE_BUS, 0x0, 0x00 },
4799 };
4800
4801 PDEBUG(4, "starting configuration");
4802
4803 /* This looks redundant, but is necessary for WebCam 3 */
4804 ov->primary_i2c_slave = OV7xx0_SID;
4805 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
4806 return -1;
4807
4808 if (init_ov_sensor(ov) >= 0) {
4809 PDEBUG(1, "OV7xx0 sensor initalized (method 1)");
4810 } else {
4811 /* Reset the 76xx */
4812 if (i2c_w(ov, 0x12, 0x80) < 0)
4813 return -1;
4814
4815 /* Wait for it to initialize */
4816 msleep(150);
4817
4818 i = 0;
4819 success = 0;
4820 while (i <= i2c_detect_tries) {
4821 if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
4822 (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
4823 success = 1;
4824 break;
4825 } else {
4826 i++;
4827 }
4828 }
4829
4830// Was (i == i2c_detect_tries) previously. This obviously used to always report
4831// success. Whether anyone actually depended on that bug is unknown
4832 if ((i >= i2c_detect_tries) && (success == 0)) {
4833 err("Failed to read sensor ID. You might not have an");
4834 err("OV7610/20, or it may be not responding. Report");
4835 err("this to " EMAIL);
4836 err("This is only a warning. You can attempt to use");
4837 err("your camera anyway");
4838// Only issue a warning for now
4839// return -1;
4840 } else {
4841 PDEBUG(1, "OV7xx0 initialized (method 2, %dx)", i+1);
4842 }
4843 }
4844
4845 /* Detect sensor (sub)type */
4846 rc = i2c_r(ov, OV7610_REG_COM_I);
4847
4848 if (rc < 0) {
4849 err("Error detecting sensor type");
4850 return -1;
4851 } else if ((rc & 3) == 3) {
4852 info("Sensor is an OV7610");
4853 ov->sensor = SEN_OV7610;
4854 } else if ((rc & 3) == 1) {
4855 /* I don't know what's different about the 76BE yet. */
4856 if (i2c_r(ov, 0x15) & 1)
4857 info("Sensor is an OV7620AE");
4858 else
4859 info("Sensor is an OV76BE");
4860
4861 /* OV511+ will return all zero isoc data unless we
4862 * configure the sensor as a 7620. Someone needs to
4863 * find the exact reg. setting that causes this. */
4864 if (ov->bridge == BRG_OV511PLUS) {
4865 info("Enabling 511+/7620AE workaround");
4866 ov->sensor = SEN_OV7620;
4867 } else {
4868 ov->sensor = SEN_OV76BE;
4869 }
4870 } else if ((rc & 3) == 0) {
4871 info("Sensor is an OV7620");
4872 ov->sensor = SEN_OV7620;
4873 } else {
4874 err("Unknown image sensor version: %d", rc & 3);
4875 return -1;
4876 }
4877
4878 if (ov->sensor == SEN_OV7620) {
4879 PDEBUG(4, "Writing 7620 registers");
4880 if (write_regvals(ov, aRegvalsNorm7620))
4881 return -1;
4882 } else {
4883 PDEBUG(4, "Writing 7610 registers");
4884 if (write_regvals(ov, aRegvalsNorm7610))
4885 return -1;
4886 }
4887
4888 /* Set sensor-specific vars */
4889 ov->maxwidth = 640;
4890 ov->maxheight = 480;
4891 ov->minwidth = 64;
4892 ov->minheight = 48;
4893
4894 // FIXME: These do not match the actual settings yet
4895 ov->brightness = 0x80 << 8;
4896 ov->contrast = 0x80 << 8;
4897 ov->colour = 0x80 << 8;
4898 ov->hue = 0x80 << 8;
4899
4900 return 0;
4901}
4902
4903/* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
4904static int
4905ov6xx0_configure(struct usb_ov511 *ov)
4906{
4907 int rc;
4908
4909 static struct ov511_regvals aRegvalsNorm6x20[] = {
4910 { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
4911 { OV511_I2C_BUS, 0x11, 0x01 },
4912 { OV511_I2C_BUS, 0x03, 0x60 },
4913 { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
4914 { OV511_I2C_BUS, 0x07, 0xa8 },
4915 /* The ratio of 0x0c and 0x0d controls the white point */
4916 { OV511_I2C_BUS, 0x0c, 0x24 },
4917 { OV511_I2C_BUS, 0x0d, 0x24 },
4918 { OV511_I2C_BUS, 0x0f, 0x15 }, /* COMS */
4919 { OV511_I2C_BUS, 0x10, 0x75 }, /* AEC Exposure time */
4920 { OV511_I2C_BUS, 0x12, 0x24 }, /* Enable AGC */
4921 { OV511_I2C_BUS, 0x14, 0x04 },
4922 /* 0x16: 0x06 helps frame stability with moving objects */
4923 { OV511_I2C_BUS, 0x16, 0x06 },
4924// { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
4925 { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
4926 /* 0x28: 0x05 Selects RGB format if RGB on */
4927 { OV511_I2C_BUS, 0x28, 0x05 },
4928 { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
4929// { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
4930 { OV511_I2C_BUS, 0x2d, 0x99 },
4931 { OV511_I2C_BUS, 0x33, 0xa0 }, /* Color Processing Parameter */
4932 { OV511_I2C_BUS, 0x34, 0xd2 }, /* Max A/D range */
4933 { OV511_I2C_BUS, 0x38, 0x8b },
4934 { OV511_I2C_BUS, 0x39, 0x40 },
4935
4936 { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
4937 { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
4938 { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
4939
4940 { OV511_I2C_BUS, 0x3d, 0x80 },
4941 /* These next two registers (0x4a, 0x4b) are undocumented. They
4942 * control the color balance */
4943 { OV511_I2C_BUS, 0x4a, 0x80 },
4944 { OV511_I2C_BUS, 0x4b, 0x80 },
4945 { OV511_I2C_BUS, 0x4d, 0xd2 }, /* This reduces noise a bit */
4946 { OV511_I2C_BUS, 0x4e, 0xc1 },
4947 { OV511_I2C_BUS, 0x4f, 0x04 },
4948// Do 50-53 have any effect?
4949// Toggle 0x12[2] off and on here?
4950 { OV511_DONE_BUS, 0x0, 0x00 }, /* END MARKER */
4951 };
4952
4953 static struct ov511_regvals aRegvalsNorm6x30[] = {
4954 /*OK*/ { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
4955 { OV511_I2C_BUS, 0x11, 0x00 },
4956 /*OK*/ { OV511_I2C_BUS, 0x03, 0x60 },
4957 /*0A?*/ { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
4958 { OV511_I2C_BUS, 0x07, 0xa8 },
4959 /* The ratio of 0x0c and 0x0d controls the white point */
4960 /*OK*/ { OV511_I2C_BUS, 0x0c, 0x24 },
4961 /*OK*/ { OV511_I2C_BUS, 0x0d, 0x24 },
4962 /*A*/ { OV511_I2C_BUS, 0x0e, 0x20 },
4963// /*04?*/ { OV511_I2C_BUS, 0x14, 0x80 },
4964 { OV511_I2C_BUS, 0x16, 0x03 },
4965// /*OK*/ { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
4966 // 21 & 22? The suggested values look wrong. Go with default
4967 /*A*/ { OV511_I2C_BUS, 0x23, 0xc0 },
4968 /*A*/ { OV511_I2C_BUS, 0x25, 0x9a }, // Check this against default
4969// /*OK*/ { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
4970
4971 /* 0x28: 0x05 Selects RGB format if RGB on */
4972// /*04?*/ { OV511_I2C_BUS, 0x28, 0x05 },
4973// /*04?*/ { OV511_I2C_BUS, 0x28, 0x45 }, // DEBUG: Tristate UV bus
4974
4975 /*OK*/ { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
4976// /*OK*/ { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
4977 { OV511_I2C_BUS, 0x2d, 0x99 },
4978// /*A*/ { OV511_I2C_BUS, 0x33, 0x26 }, // Reserved bits on 6620
4979// /*d2?*/ { OV511_I2C_BUS, 0x34, 0x03 }, /* Max A/D range */
4980// /*8b?*/ { OV511_I2C_BUS, 0x38, 0x83 },
4981// /*40?*/ { OV511_I2C_BUS, 0x39, 0xc0 }, // 6630 adds bit 7
4982// { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
4983// { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
4984// { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
4985 { OV511_I2C_BUS, 0x3d, 0x80 },
4986// /*A*/ { OV511_I2C_BUS, 0x3f, 0x0e },
4987
4988 /* These next two registers (0x4a, 0x4b) are undocumented. They
4989 * control the color balance */
4990// /*OK?*/ { OV511_I2C_BUS, 0x4a, 0x80 }, // Check these
4991// /*OK?*/ { OV511_I2C_BUS, 0x4b, 0x80 },
4992 { OV511_I2C_BUS, 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
4993 /*c1?*/ { OV511_I2C_BUS, 0x4e, 0x40 },
4994
4995 /* UV average mode, color killer: strongest */
4996 { OV511_I2C_BUS, 0x4f, 0x07 },
4997
4998 { OV511_I2C_BUS, 0x54, 0x23 }, /* Max AGC gain: 18dB */
4999 { OV511_I2C_BUS, 0x57, 0x81 }, /* (default) */
5000 { OV511_I2C_BUS, 0x59, 0x01 }, /* AGC dark current comp: +1 */
5001 { OV511_I2C_BUS, 0x5a, 0x2c }, /* (undocumented) */
5002 { OV511_I2C_BUS, 0x5b, 0x0f }, /* AWB chrominance levels */
5003// { OV511_I2C_BUS, 0x5c, 0x10 },
5004 { OV511_DONE_BUS, 0x0, 0x00 }, /* END MARKER */
5005 };
5006
5007 PDEBUG(4, "starting sensor configuration");
5008
5009 if (init_ov_sensor(ov) < 0) {
5010 err("Failed to read sensor ID. You might not have an OV6xx0,");
5011 err("or it may be not responding. Report this to " EMAIL);
5012 return -1;
5013 } else {
5014 PDEBUG(1, "OV6xx0 sensor detected");
5015 }
5016
5017 /* Detect sensor (sub)type */
5018 rc = i2c_r(ov, OV7610_REG_COM_I);
5019
5020 if (rc < 0) {
5021 err("Error detecting sensor type");
5022 return -1;
5023 }
5024
5025 if ((rc & 3) == 0) {
5026 ov->sensor = SEN_OV6630;
5027 info("Sensor is an OV6630");
5028 } else if ((rc & 3) == 1) {
5029 ov->sensor = SEN_OV6620;
5030 info("Sensor is an OV6620");
5031 } else if ((rc & 3) == 2) {
5032 ov->sensor = SEN_OV6630;
5033 info("Sensor is an OV6630AE");
5034 } else if ((rc & 3) == 3) {
5035 ov->sensor = SEN_OV6630;
5036 info("Sensor is an OV6630AF");
5037 }
5038
5039 /* Set sensor-specific vars */
5040 ov->maxwidth = 352;
5041 ov->maxheight = 288;
5042 ov->minwidth = 64;
5043 ov->minheight = 48;
5044
5045 // FIXME: These do not match the actual settings yet
5046 ov->brightness = 0x80 << 8;
5047 ov->contrast = 0x80 << 8;
5048 ov->colour = 0x80 << 8;
5049 ov->hue = 0x80 << 8;
5050
5051 if (ov->sensor == SEN_OV6620) {
5052 PDEBUG(4, "Writing 6x20 registers");
5053 if (write_regvals(ov, aRegvalsNorm6x20))
5054 return -1;
5055 } else {
5056 PDEBUG(4, "Writing 6x30 registers");
5057 if (write_regvals(ov, aRegvalsNorm6x30))
5058 return -1;
5059 }
5060
5061 return 0;
5062}
5063
5064/* This initializes the KS0127 and KS0127B video decoders. */
5065static int
5066ks0127_configure(struct usb_ov511 *ov)
5067{
5068 int rc;
5069
5070// FIXME: I don't know how to sync or reset it yet
5071#if 0
5072 if (ov51x_init_ks_sensor(ov) < 0) {
5073 err("Failed to initialize the KS0127");
5074 return -1;
5075 } else {
5076 PDEBUG(1, "KS012x(B) sensor detected");
5077 }
5078#endif
5079
5080 /* Detect decoder subtype */
5081 rc = i2c_r(ov, 0x00);
5082 if (rc < 0) {
5083 err("Error detecting sensor type");
5084 return -1;
5085 } else if (rc & 0x08) {
5086 rc = i2c_r(ov, 0x3d);
5087 if (rc < 0) {
5088 err("Error detecting sensor type");
5089 return -1;
5090 } else if ((rc & 0x0f) == 0) {
5091 info("Sensor is a KS0127");
5092 ov->sensor = SEN_KS0127;
5093 } else if ((rc & 0x0f) == 9) {
5094 info("Sensor is a KS0127B Rev. A");
5095 ov->sensor = SEN_KS0127B;
5096 }
5097 } else {
5098 err("Error: Sensor is an unsupported KS0122");
5099 return -1;
5100 }
5101
5102 /* Set sensor-specific vars */
5103 ov->maxwidth = 640;
5104 ov->maxheight = 480;
5105 ov->minwidth = 64;
5106 ov->minheight = 48;
5107
5108 // FIXME: These do not match the actual settings yet
5109 ov->brightness = 0x80 << 8;
5110 ov->contrast = 0x80 << 8;
5111 ov->colour = 0x80 << 8;
5112 ov->hue = 0x80 << 8;
5113
5114 /* This device is not supported yet. Bail out now... */
5115 err("This sensor is not supported yet.");
5116 return -1;
5117
5118 return 0;
5119}
5120
5121/* This initializes the SAA7111A video decoder. */
5122static int
5123saa7111a_configure(struct usb_ov511 *ov)
5124{
5125 int rc;
5126
5127 /* Since there is no register reset command, all registers must be
5128 * written, otherwise gives erratic results */
5129 static struct ov511_regvals aRegvalsNormSAA7111A[] = {
5130 { OV511_I2C_BUS, 0x06, 0xce },
5131 { OV511_I2C_BUS, 0x07, 0x00 },
5132 { OV511_I2C_BUS, 0x10, 0x44 }, /* YUV422, 240/286 lines */
5133 { OV511_I2C_BUS, 0x0e, 0x01 }, /* NTSC M or PAL BGHI */
5134 { OV511_I2C_BUS, 0x00, 0x00 },
5135 { OV511_I2C_BUS, 0x01, 0x00 },
5136 { OV511_I2C_BUS, 0x03, 0x23 },
5137 { OV511_I2C_BUS, 0x04, 0x00 },
5138 { OV511_I2C_BUS, 0x05, 0x00 },
5139 { OV511_I2C_BUS, 0x08, 0xc8 }, /* Auto field freq */
5140 { OV511_I2C_BUS, 0x09, 0x01 }, /* Chrom. trap off, APER=0.25 */
5141 { OV511_I2C_BUS, 0x0a, 0x80 }, /* BRIG=128 */
5142 { OV511_I2C_BUS, 0x0b, 0x40 }, /* CONT=1.0 */
5143 { OV511_I2C_BUS, 0x0c, 0x40 }, /* SATN=1.0 */
5144 { OV511_I2C_BUS, 0x0d, 0x00 }, /* HUE=0 */
5145 { OV511_I2C_BUS, 0x0f, 0x00 },
5146 { OV511_I2C_BUS, 0x11, 0x0c },
5147 { OV511_I2C_BUS, 0x12, 0x00 },
5148 { OV511_I2C_BUS, 0x13, 0x00 },
5149 { OV511_I2C_BUS, 0x14, 0x00 },
5150 { OV511_I2C_BUS, 0x15, 0x00 },
5151 { OV511_I2C_BUS, 0x16, 0x00 },
5152 { OV511_I2C_BUS, 0x17, 0x00 },
5153 { OV511_I2C_BUS, 0x02, 0xc0 }, /* Composite input 0 */
5154 { OV511_DONE_BUS, 0x0, 0x00 },
5155 };
5156
5157// FIXME: I don't know how to sync or reset it yet
5158#if 0
5159 if (ov51x_init_saa_sensor(ov) < 0) {
5160 err("Failed to initialize the SAA7111A");
5161 return -1;
5162 } else {
5163 PDEBUG(1, "SAA7111A sensor detected");
5164 }
5165#endif
5166
5167 /* 640x480 not supported with PAL */
5168 if (ov->pal) {
5169 ov->maxwidth = 320;
5170 ov->maxheight = 240; /* Even field only */
5171 } else {
5172 ov->maxwidth = 640;
5173 ov->maxheight = 480; /* Even/Odd fields */
5174 }
5175
5176 ov->minwidth = 320;
5177 ov->minheight = 240; /* Even field only */
5178
5179 ov->has_decoder = 1;
5180 ov->num_inputs = 8;
5181 ov->norm = VIDEO_MODE_AUTO;
5182 ov->stop_during_set = 0; /* Decoder guarantees stable image */
5183
5184 /* Decoder doesn't change these values, so we use these instead of
5185 * acutally reading the registers (which doesn't work) */
5186 ov->brightness = 0x80 << 8;
5187 ov->contrast = 0x40 << 9;
5188 ov->colour = 0x40 << 9;
5189 ov->hue = 32768;
5190
5191 PDEBUG(4, "Writing SAA7111A registers");
5192 if (write_regvals(ov, aRegvalsNormSAA7111A))
5193 return -1;
5194
5195 /* Detect version of decoder. This must be done after writing the
5196 * initial regs or the decoder will lock up. */
5197 rc = i2c_r(ov, 0x00);
5198
5199 if (rc < 0) {
5200 err("Error detecting sensor version");
5201 return -1;
5202 } else {
5203 info("Sensor is an SAA7111A (version 0x%x)", rc);
5204 ov->sensor = SEN_SAA7111A;
5205 }
5206
5207 // FIXME: Fix this for OV518(+)
5208 /* Latch to negative edge of clock. Otherwise, we get incorrect
5209 * colors and jitter in the digital signal. */
5210 if (ov->bclass == BCL_OV511)
5211 reg_w(ov, 0x11, 0x00);
5212 else
5213 warn("SAA7111A not yet supported with OV518/OV518+");
5214
5215 return 0;
5216}
5217
5218/* This initializes the OV511/OV511+ and the sensor */
5219static int
5220ov511_configure(struct usb_ov511 *ov)
5221{
5222 static struct ov511_regvals aRegvalsInit511[] = {
5223 { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
5224 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5225 { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
5226 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5227 { OV511_REG_BUS, R51x_SYS_RESET, 0x3f },
5228 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5229 { OV511_REG_BUS, R51x_SYS_RESET, 0x3d },
5230 { OV511_DONE_BUS, 0x0, 0x00},
5231 };
5232
5233 static struct ov511_regvals aRegvalsNorm511[] = {
5234 { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0x01 },
5235 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5236 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
5237 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5238 { OV511_REG_BUS, R511_FIFO_OPTS, 0x1f },
5239 { OV511_REG_BUS, R511_COMP_EN, 0x00 },
5240 { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
5241 { OV511_DONE_BUS, 0x0, 0x00 },
5242 };
5243
5244 static struct ov511_regvals aRegvalsNorm511Plus[] = {
5245 { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0xff },
5246 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5247 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
5248 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5249 { OV511_REG_BUS, R511_FIFO_OPTS, 0xff },
5250 { OV511_REG_BUS, R511_COMP_EN, 0x00 },
5251 { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
5252 { OV511_DONE_BUS, 0x0, 0x00 },
5253 };
5254
5255 PDEBUG(4, "");
5256
5257 ov->customid = reg_r(ov, R511_SYS_CUST_ID);
5258 if (ov->customid < 0) {
5259 err("Unable to read camera bridge registers");
5260 goto error;
5261 }
5262
5263 PDEBUG (1, "CustomID = %d", ov->customid);
5264 ov->desc = symbolic(camlist, ov->customid);
5265 info("model: %s", ov->desc);
5266
5267 if (0 == strcmp(ov->desc, NOT_DEFINED_STR)) {
5268 err("Camera type (%d) not recognized", ov->customid);
5269 err("Please notify " EMAIL " of the name,");
5270 err("manufacturer, model, and this number of your camera.");
5271 err("Also include the output of the detection process.");
5272 }
5273
5274 if (ov->customid == 70) /* USB Life TV (PAL/SECAM) */
5275 ov->pal = 1;
5276
5277 if (write_regvals(ov, aRegvalsInit511))
5278 goto error;
5279
5280 if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
5281 ov51x_led_control(ov, 0);
5282
5283 /* The OV511+ has undocumented bits in the flow control register.
5284 * Setting it to 0xff fixes the corruption with moving objects. */
5285 if (ov->bridge == BRG_OV511) {
5286 if (write_regvals(ov, aRegvalsNorm511))
5287 goto error;
5288 } else if (ov->bridge == BRG_OV511PLUS) {
5289 if (write_regvals(ov, aRegvalsNorm511Plus))
5290 goto error;
5291 } else {
5292 err("Invalid bridge");
5293 }
5294
5295 if (ov511_init_compression(ov))
5296 goto error;
5297
5298 ov->packet_numbering = 1;
5299 ov511_set_packet_size(ov, 0);
5300
5301 ov->snap_enabled = snapshot;
5302
5303 /* Test for 7xx0 */
5304 PDEBUG(3, "Testing for 0V7xx0");
5305 ov->primary_i2c_slave = OV7xx0_SID;
5306 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
5307 goto error;
5308
5309 if (i2c_w(ov, 0x12, 0x80) < 0) {
5310 /* Test for 6xx0 */
5311 PDEBUG(3, "Testing for 0V6xx0");
5312 ov->primary_i2c_slave = OV6xx0_SID;
5313 if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
5314 goto error;
5315
5316 if (i2c_w(ov, 0x12, 0x80) < 0) {
5317 /* Test for 8xx0 */
5318 PDEBUG(3, "Testing for 0V8xx0");
5319 ov->primary_i2c_slave = OV8xx0_SID;
5320 if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
5321 goto error;
5322
5323 if (i2c_w(ov, 0x12, 0x80) < 0) {
5324 /* Test for SAA7111A */
5325 PDEBUG(3, "Testing for SAA7111A");
5326 ov->primary_i2c_slave = SAA7111A_SID;
5327 if (ov51x_set_slave_ids(ov, SAA7111A_SID) < 0)
5328 goto error;
5329
5330 if (i2c_w(ov, 0x0d, 0x00) < 0) {
5331 /* Test for KS0127 */
5332 PDEBUG(3, "Testing for KS0127");
5333 ov->primary_i2c_slave = KS0127_SID;
5334 if (ov51x_set_slave_ids(ov, KS0127_SID) < 0)
5335 goto error;
5336
5337 if (i2c_w(ov, 0x10, 0x00) < 0) {
5338 err("Can't determine sensor slave IDs");
5339 goto error;
5340 } else {
5341 if (ks0127_configure(ov) < 0) {
5342 err("Failed to configure KS0127");
5343 goto error;
5344 }
5345 }
5346 } else {
5347 if (saa7111a_configure(ov) < 0) {
5348 err("Failed to configure SAA7111A");
5349 goto error;
5350 }
5351 }
5352 } else {
5353 err("Detected unsupported OV8xx0 sensor");
5354 goto error;
5355 }
5356 } else {
5357 if (ov6xx0_configure(ov) < 0) {
5358 err("Failed to configure OV6xx0");
5359 goto error;
5360 }
5361 }
5362 } else {
5363 if (ov7xx0_configure(ov) < 0) {
5364 err("Failed to configure OV7xx0");
5365 goto error;
5366 }
5367 }
5368
5369 return 0;
5370
5371error:
5372 err("OV511 Config failed");
5373
5374 return -EBUSY;
5375}
5376
5377/* This initializes the OV518/OV518+ and the sensor */
5378static int
5379ov518_configure(struct usb_ov511 *ov)
5380{
5381 /* For 518 and 518+ */
5382 static struct ov511_regvals aRegvalsInit518[] = {
5383 { OV511_REG_BUS, R51x_SYS_RESET, 0x40 },
5384 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5385 { OV511_REG_BUS, R51x_SYS_RESET, 0x3e },
5386 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5387 { OV511_REG_BUS, R51x_SYS_RESET, 0x00 },
5388 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5389 { OV511_REG_BUS, 0x46, 0x00 },
5390 { OV511_REG_BUS, 0x5d, 0x03 },
5391 { OV511_DONE_BUS, 0x0, 0x00},
5392 };
5393
5394 static struct ov511_regvals aRegvalsNorm518[] = {
5395 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 }, /* Reset */
5396 { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 }, /* Enable */
5397 { OV511_REG_BUS, 0x31, 0x0f },
5398 { OV511_REG_BUS, 0x5d, 0x03 },
5399 { OV511_REG_BUS, 0x24, 0x9f },
5400 { OV511_REG_BUS, 0x25, 0x90 },
5401 { OV511_REG_BUS, 0x20, 0x00 },
5402 { OV511_REG_BUS, 0x51, 0x04 },
5403 { OV511_REG_BUS, 0x71, 0x19 },
5404 { OV511_DONE_BUS, 0x0, 0x00 },
5405 };
5406
5407 static struct ov511_regvals aRegvalsNorm518Plus[] = {
5408 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 }, /* Reset */
5409 { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 }, /* Enable */
5410 { OV511_REG_BUS, 0x31, 0x0f },
5411 { OV511_REG_BUS, 0x5d, 0x03 },
5412 { OV511_REG_BUS, 0x24, 0x9f },
5413 { OV511_REG_BUS, 0x25, 0x90 },
5414 { OV511_REG_BUS, 0x20, 0x60 },
5415 { OV511_REG_BUS, 0x51, 0x02 },
5416 { OV511_REG_BUS, 0x71, 0x19 },
5417 { OV511_REG_BUS, 0x40, 0xff },
5418 { OV511_REG_BUS, 0x41, 0x42 },
5419 { OV511_REG_BUS, 0x46, 0x00 },
5420 { OV511_REG_BUS, 0x33, 0x04 },
5421 { OV511_REG_BUS, 0x21, 0x19 },
5422 { OV511_REG_BUS, 0x3f, 0x10 },
5423 { OV511_DONE_BUS, 0x0, 0x00 },
5424 };
5425
5426 PDEBUG(4, "");
5427
5428 /* First 5 bits of custom ID reg are a revision ID on OV518 */
5429 info("Device revision %d", 0x1F & reg_r(ov, R511_SYS_CUST_ID));
5430
5431 /* Give it the default description */
5432 ov->desc = symbolic(camlist, 0);
5433
5434 if (write_regvals(ov, aRegvalsInit518))
5435 goto error;
5436
5437 /* Set LED GPIO pin to output mode */
5438 if (reg_w_mask(ov, 0x57, 0x00, 0x02) < 0)
5439 goto error;
5440
5441 /* LED is off by default with OV518; have to explicitly turn it on */
5442 if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
5443 ov51x_led_control(ov, 0);
5444 else
5445 ov51x_led_control(ov, 1);
5446
5447 /* Don't require compression if dumppix is enabled; otherwise it's
5448 * required. OV518 has no uncompressed mode, to save RAM. */
5449 if (!dumppix && !ov->compress) {
5450 ov->compress = 1;
5451 warn("Compression required with OV518...enabling");
5452 }
5453
5454 if (ov->bridge == BRG_OV518) {
5455 if (write_regvals(ov, aRegvalsNorm518))
5456 goto error;
5457 } else if (ov->bridge == BRG_OV518PLUS) {
5458 if (write_regvals(ov, aRegvalsNorm518Plus))
5459 goto error;
5460 } else {
5461 err("Invalid bridge");
5462 }
5463
5464 if (reg_w(ov, 0x2f, 0x80) < 0)
5465 goto error;
5466
5467 if (ov518_init_compression(ov))
5468 goto error;
5469
5470 if (ov->bridge == BRG_OV518)
5471 {
5472 struct usb_interface *ifp;
5473 struct usb_host_interface *alt;
5474 __u16 mxps = 0;
5475
5476 ifp = usb_ifnum_to_if(ov->dev, 0);
5477 if (ifp) {
5478 alt = usb_altnum_to_altsetting(ifp, 7);
5479 if (alt)
5480 mxps = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
5481 }
5482
5483 /* Some OV518s have packet numbering by default, some don't */
5484 if (mxps == 897)
5485 ov->packet_numbering = 1;
5486 else
5487 ov->packet_numbering = 0;
5488 } else {
5489 /* OV518+ has packet numbering turned on by default */
5490 ov->packet_numbering = 1;
5491 }
5492
5493 ov518_set_packet_size(ov, 0);
5494
5495 ov->snap_enabled = snapshot;
5496
5497 /* Test for 76xx */
5498 ov->primary_i2c_slave = OV7xx0_SID;
5499 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
5500 goto error;
5501
5502 /* The OV518 must be more aggressive about sensor detection since
5503 * I2C write will never fail if the sensor is not present. We have
5504 * to try to initialize the sensor to detect its presence */
5505
5506 if (init_ov_sensor(ov) < 0) {
5507 /* Test for 6xx0 */
5508 ov->primary_i2c_slave = OV6xx0_SID;
5509 if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
5510 goto error;
5511
5512 if (init_ov_sensor(ov) < 0) {
5513 /* Test for 8xx0 */
5514 ov->primary_i2c_slave = OV8xx0_SID;
5515 if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
5516 goto error;
5517
5518 if (init_ov_sensor(ov) < 0) {
5519 err("Can't determine sensor slave IDs");
5520 goto error;
5521 } else {
5522 err("Detected unsupported OV8xx0 sensor");
5523 goto error;
5524 }
5525 } else {
5526 if (ov6xx0_configure(ov) < 0) {
5527 err("Failed to configure OV6xx0");
5528 goto error;
5529 }
5530 }
5531 } else {
5532 if (ov7xx0_configure(ov) < 0) {
5533 err("Failed to configure OV7xx0");
5534 goto error;
5535 }
5536 }
5537
5538 ov->maxwidth = 352;
5539 ov->maxheight = 288;
5540
5541 // The OV518 cannot go as low as the sensor can
5542 ov->minwidth = 160;
5543 ov->minheight = 120;
5544
5545 return 0;
5546
5547error:
5548 err("OV518 Config failed");
5549
5550 return -EBUSY;
5551}
5552
5553/****************************************************************************
5554 * sysfs
5555 ***************************************************************************/
5556
5557static inline struct usb_ov511 *cd_to_ov(struct class_device *cd)
5558{
5559 struct video_device *vdev = to_video_device(cd);
5560 return video_get_drvdata(vdev);
5561}
5562
5563static ssize_t show_custom_id(struct class_device *cd, char *buf)
5564{
5565 struct usb_ov511 *ov = cd_to_ov(cd);
5566 return sprintf(buf, "%d\n", ov->customid);
5567}
5568static CLASS_DEVICE_ATTR(custom_id, S_IRUGO, show_custom_id, NULL);
5569
5570static ssize_t show_model(struct class_device *cd, char *buf)
5571{
5572 struct usb_ov511 *ov = cd_to_ov(cd);
5573 return sprintf(buf, "%s\n", ov->desc);
5574}
5575static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
5576
5577static ssize_t show_bridge(struct class_device *cd, char *buf)
5578{
5579 struct usb_ov511 *ov = cd_to_ov(cd);
5580 return sprintf(buf, "%s\n", symbolic(brglist, ov->bridge));
5581}
5582static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_bridge, NULL);
5583
5584static ssize_t show_sensor(struct class_device *cd, char *buf)
5585{
5586 struct usb_ov511 *ov = cd_to_ov(cd);
5587 return sprintf(buf, "%s\n", symbolic(senlist, ov->sensor));
5588}
5589static CLASS_DEVICE_ATTR(sensor, S_IRUGO, show_sensor, NULL);
5590
5591static ssize_t show_brightness(struct class_device *cd, char *buf)
5592{
5593 struct usb_ov511 *ov = cd_to_ov(cd);
5594 unsigned short x;
5595
5596 if (!ov->dev)
5597 return -ENODEV;
5598 sensor_get_brightness(ov, &x);
5599 return sprintf(buf, "%d\n", x >> 8);
5600}
5601static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
5602
5603static ssize_t show_saturation(struct class_device *cd, char *buf)
5604{
5605 struct usb_ov511 *ov = cd_to_ov(cd);
5606 unsigned short x;
5607
5608 if (!ov->dev)
5609 return -ENODEV;
5610 sensor_get_saturation(ov, &x);
5611 return sprintf(buf, "%d\n", x >> 8);
5612}
5613static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
5614
5615static ssize_t show_contrast(struct class_device *cd, char *buf)
5616{
5617 struct usb_ov511 *ov = cd_to_ov(cd);
5618 unsigned short x;
5619
5620 if (!ov->dev)
5621 return -ENODEV;
5622 sensor_get_contrast(ov, &x);
5623 return sprintf(buf, "%d\n", x >> 8);
5624}
5625static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
5626
5627static ssize_t show_hue(struct class_device *cd, char *buf)
5628{
5629 struct usb_ov511 *ov = cd_to_ov(cd);
5630 unsigned short x;
5631
5632 if (!ov->dev)
5633 return -ENODEV;
5634 sensor_get_hue(ov, &x);
5635 return sprintf(buf, "%d\n", x >> 8);
5636}
5637static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
5638
5639static ssize_t show_exposure(struct class_device *cd, char *buf)
5640{
5641 struct usb_ov511 *ov = cd_to_ov(cd);
5642 unsigned char exp;
5643
5644 if (!ov->dev)
5645 return -ENODEV;
5646 sensor_get_exposure(ov, &exp);
5647 return sprintf(buf, "%d\n", exp >> 8);
5648}
5649static CLASS_DEVICE_ATTR(exposure, S_IRUGO, show_exposure, NULL);
5650
5651static void ov_create_sysfs(struct video_device *vdev)
5652{
5653 video_device_create_file(vdev, &class_device_attr_custom_id);
5654 video_device_create_file(vdev, &class_device_attr_model);
5655 video_device_create_file(vdev, &class_device_attr_bridge);
5656 video_device_create_file(vdev, &class_device_attr_sensor);
5657 video_device_create_file(vdev, &class_device_attr_brightness);
5658 video_device_create_file(vdev, &class_device_attr_saturation);
5659 video_device_create_file(vdev, &class_device_attr_contrast);
5660 video_device_create_file(vdev, &class_device_attr_hue);
5661 video_device_create_file(vdev, &class_device_attr_exposure);
5662}
5663
5664/****************************************************************************
5665 * USB routines
5666 ***************************************************************************/
5667
5668static int
5669ov51x_probe(struct usb_interface *intf, const struct usb_device_id *id)
5670{
5671 struct usb_device *dev = interface_to_usbdev(intf);
5672 struct usb_interface_descriptor *idesc;
5673 struct usb_ov511 *ov;
5674 int i;
5675
5676 PDEBUG(1, "probing for device...");
5677
5678 /* We don't handle multi-config cameras */
5679 if (dev->descriptor.bNumConfigurations != 1)
5680 return -ENODEV;
5681
5682 idesc = &intf->cur_altsetting->desc;
5683
5684 if (idesc->bInterfaceClass != 0xFF)
5685 return -ENODEV;
5686 if (idesc->bInterfaceSubClass != 0x00)
5687 return -ENODEV;
5688
5689 if ((ov = kmalloc(sizeof(*ov), GFP_KERNEL)) == NULL) {
5690 err("couldn't kmalloc ov struct");
5691 goto error_out;
5692 }
5693
5694 memset(ov, 0, sizeof(*ov));
5695
5696 ov->dev = dev;
5697 ov->iface = idesc->bInterfaceNumber;
5698 ov->led_policy = led;
5699 ov->compress = compress;
5700 ov->lightfreq = lightfreq;
5701 ov->num_inputs = 1; /* Video decoder init functs. change this */
5702 ov->stop_during_set = !fastset;
5703 ov->backlight = backlight;
5704 ov->mirror = mirror;
5705 ov->auto_brt = autobright;
5706 ov->auto_gain = autogain;
5707 ov->auto_exp = autoexp;
5708
5709 switch (le16_to_cpu(dev->descriptor.idProduct)) {
5710 case PROD_OV511:
5711 ov->bridge = BRG_OV511;
5712 ov->bclass = BCL_OV511;
5713 break;
5714 case PROD_OV511PLUS:
5715 ov->bridge = BRG_OV511PLUS;
5716 ov->bclass = BCL_OV511;
5717 break;
5718 case PROD_OV518:
5719 ov->bridge = BRG_OV518;
5720 ov->bclass = BCL_OV518;
5721 break;
5722 case PROD_OV518PLUS:
5723 ov->bridge = BRG_OV518PLUS;
5724 ov->bclass = BCL_OV518;
5725 break;
5726 case PROD_ME2CAM:
5727 if (le16_to_cpu(dev->descriptor.idVendor) != VEND_MATTEL)
5728 goto error;
5729 ov->bridge = BRG_OV511PLUS;
5730 ov->bclass = BCL_OV511;
5731 break;
5732 default:
5733 err("Unknown product ID 0x%04x", le16_to_cpu(dev->descriptor.idProduct));
5734 goto error;
5735 }
5736
5737 info("USB %s video device found", symbolic(brglist, ov->bridge));
5738
5739 init_waitqueue_head(&ov->wq);
5740
5741 init_MUTEX(&ov->lock); /* to 1 == available */
5742 init_MUTEX(&ov->buf_lock);
5743 init_MUTEX(&ov->param_lock);
5744 init_MUTEX(&ov->i2c_lock);
5745 init_MUTEX(&ov->cbuf_lock);
5746
5747 ov->buf_state = BUF_NOT_ALLOCATED;
5748
5749 if (usb_make_path(dev, ov->usb_path, OV511_USB_PATH_LEN) < 0) {
5750 err("usb_make_path error");
5751 goto error;
5752 }
5753
5754 /* Allocate control transfer buffer. */
5755 /* Must be kmalloc()'ed, for DMA compatibility */
5756 ov->cbuf = kmalloc(OV511_CBUF_SIZE, GFP_KERNEL);
5757 if (!ov->cbuf)
5758 goto error;
5759
5760 if (ov->bclass == BCL_OV518) {
5761 if (ov518_configure(ov) < 0)
5762 goto error;
5763 } else {
5764 if (ov511_configure(ov) < 0)
5765 goto error;
5766 }
5767
5768 for (i = 0; i < OV511_NUMFRAMES; i++) {
5769 ov->frame[i].framenum = i;
5770 init_waitqueue_head(&ov->frame[i].wq);
5771 }
5772
5773 for (i = 0; i < OV511_NUMSBUF; i++) {
5774 ov->sbuf[i].ov = ov;
5775 spin_lock_init(&ov->sbuf[i].lock);
5776 ov->sbuf[i].n = i;
5777 }
5778
5779 /* Unnecessary? (This is done on open(). Need to make sure variables
5780 * are properly initialized without this before removing it, though). */
5781 if (ov51x_set_default_params(ov) < 0)
5782 goto error;
5783
5784#ifdef OV511_DEBUG
5785 if (dump_bridge) {
5786 if (ov->bclass == BCL_OV511)
5787 ov511_dump_regs(ov);
5788 else
5789 ov518_dump_regs(ov);
5790 }
5791#endif
5792
5793 ov->vdev = video_device_alloc();
5794 if (!ov->vdev)
5795 goto error;
5796
5797 memcpy(ov->vdev, &vdev_template, sizeof(*ov->vdev));
5798 ov->vdev->dev = &dev->dev;
5799 video_set_drvdata(ov->vdev, ov);
5800
5801 for (i = 0; i < OV511_MAX_UNIT_VIDEO; i++) {
5802 /* Minor 0 cannot be specified; assume user wants autodetect */
5803 if (unit_video[i] == 0)
5804 break;
5805
5806 if (video_register_device(ov->vdev, VFL_TYPE_GRABBER,
5807 unit_video[i]) >= 0) {
5808 break;
5809 }
5810 }
5811
5812 /* Use the next available one */
5813 if ((ov->vdev->minor == -1) &&
5814 video_register_device(ov->vdev, VFL_TYPE_GRABBER, -1) < 0) {
5815 err("video_register_device failed");
5816 goto error;
5817 }
5818
5819 info("Device at %s registered to minor %d", ov->usb_path,
5820 ov->vdev->minor);
5821
5822 usb_set_intfdata(intf, ov);
5823 ov_create_sysfs(ov->vdev);
5824 return 0;
5825
5826error:
5827 if (ov->vdev) {
5828 if (-1 == ov->vdev->minor)
5829 video_device_release(ov->vdev);
5830 else
5831 video_unregister_device(ov->vdev);
5832 ov->vdev = NULL;
5833 }
5834
5835 if (ov->cbuf) {
5836 down(&ov->cbuf_lock);
5837 kfree(ov->cbuf);
5838 ov->cbuf = NULL;
5839 up(&ov->cbuf_lock);
5840 }
5841
5842 kfree(ov);
5843 ov = NULL;
5844
5845error_out:
5846 err("Camera initialization failed");
5847 return -EIO;
5848}
5849
5850static void
5851ov51x_disconnect(struct usb_interface *intf)
5852{
5853 struct usb_ov511 *ov = usb_get_intfdata(intf);
5854 int n;
5855
5856 PDEBUG(3, "");
5857
5858 usb_set_intfdata (intf, NULL);
5859
5860 if (!ov)
5861 return;
5862
5863 if (ov->vdev)
5864 video_unregister_device(ov->vdev);
5865
5866 for (n = 0; n < OV511_NUMFRAMES; n++)
5867 ov->frame[n].grabstate = FRAME_ERROR;
5868
5869 ov->curframe = -1;
5870
5871 /* This will cause the process to request another frame */
5872 for (n = 0; n < OV511_NUMFRAMES; n++)
5873 wake_up_interruptible(&ov->frame[n].wq);
5874
5875 wake_up_interruptible(&ov->wq);
5876
5877 ov->streaming = 0;
5878 ov51x_unlink_isoc(ov);
5879
5880 ov->dev = NULL;
5881
5882 /* Free the memory */
5883 if (ov && !ov->user) {
5884 down(&ov->cbuf_lock);
5885 kfree(ov->cbuf);
5886 ov->cbuf = NULL;
5887 up(&ov->cbuf_lock);
5888
5889 ov51x_dealloc(ov);
5890 kfree(ov);
5891 ov = NULL;
5892 }
5893
5894 PDEBUG(3, "Disconnect complete");
5895}
5896
5897static struct usb_driver ov511_driver = {
5898 .name = "ov511",
5899 .id_table = device_table,
5900 .probe = ov51x_probe,
5901 .disconnect = ov51x_disconnect
5902};
5903
5904/****************************************************************************
5905 *
5906 * Module routines
5907 *
5908 ***************************************************************************/
5909
5910static int __init
5911usb_ov511_init(void)
5912{
5913 int retval;
5914
5915 retval = usb_register(&ov511_driver);
5916 if (retval)
5917 goto out;
5918
5919 info(DRIVER_VERSION " : " DRIVER_DESC);
5920
5921out:
5922 return retval;
5923}
5924
5925static void __exit
5926usb_ov511_exit(void)
5927{
5928 usb_deregister(&ov511_driver);
5929 info("driver deregistered");
5930
5931}
5932
5933module_init(usb_ov511_init);
5934module_exit(usb_ov511_exit);
5935
diff --git a/drivers/usb/media/ov511.h b/drivers/usb/media/ov511.h
deleted file mode 100644
index 086509a137c6..000000000000
--- a/drivers/usb/media/ov511.h
+++ /dev/null
@@ -1,569 +0,0 @@
1#ifndef __LINUX_OV511_H
2#define __LINUX_OV511_H
3
4#include <asm/uaccess.h>
5#include <linux/videodev.h>
6#include <linux/smp_lock.h>
7#include <linux/usb.h>
8
9#define OV511_DEBUG /* Turn on debug messages */
10
11#ifdef OV511_DEBUG
12 #define PDEBUG(level, fmt, args...) \
13 if (debug >= (level)) info("[%s:%d] " fmt, \
14 __FUNCTION__, __LINE__ , ## args)
15#else
16 #define PDEBUG(level, fmt, args...) do {} while(0)
17#endif
18
19/* This macro restricts an int variable to an inclusive range */
20#define RESTRICT_TO_RANGE(v,mi,ma) { \
21 if ((v) < (mi)) (v) = (mi); \
22 else if ((v) > (ma)) (v) = (ma); \
23}
24
25/* --------------------------------- */
26/* DEFINES FOR OV511 AND OTHER CHIPS */
27/* --------------------------------- */
28
29/* USB IDs */
30#define VEND_OMNIVISION 0x05A9
31#define PROD_OV511 0x0511
32#define PROD_OV511PLUS 0xA511
33#define PROD_OV518 0x0518
34#define PROD_OV518PLUS 0xA518
35
36#define VEND_MATTEL 0x0813
37#define PROD_ME2CAM 0x0002
38
39/* --------------------------------- */
40/* OV51x REGISTER MNEMONICS */
41/* --------------------------------- */
42
43/* Camera interface register numbers */
44#define R511_CAM_DELAY 0x10
45#define R511_CAM_EDGE 0x11
46#define R511_CAM_PXCNT 0x12
47#define R511_CAM_LNCNT 0x13
48#define R511_CAM_PXDIV 0x14
49#define R511_CAM_LNDIV 0x15
50#define R511_CAM_UV_EN 0x16
51#define R511_CAM_LINE_MODE 0x17
52#define R511_CAM_OPTS 0x18
53
54/* Snapshot mode camera interface register numbers */
55#define R511_SNAP_FRAME 0x19
56#define R511_SNAP_PXCNT 0x1A
57#define R511_SNAP_LNCNT 0x1B
58#define R511_SNAP_PXDIV 0x1C
59#define R511_SNAP_LNDIV 0x1D
60#define R511_SNAP_UV_EN 0x1E
61#define R511_SNAP_OPTS 0x1F
62
63/* DRAM register numbers */
64#define R511_DRAM_FLOW_CTL 0x20
65#define R511_DRAM_ARCP 0x21
66#define R511_DRAM_MRC 0x22
67#define R511_DRAM_RFC 0x23
68
69/* ISO FIFO register numbers */
70#define R51x_FIFO_PSIZE 0x30 /* 2 bytes wide w/ OV518(+) */
71#define R511_FIFO_OPTS 0x31
72
73/* Parallel IO register numbers */
74#define R511_PIO_OPTS 0x38
75#define R511_PIO_DATA 0x39
76#define R511_PIO_BIST 0x3E
77#define R518_GPIO_IN 0x55 /* OV518(+) only */
78#define R518_GPIO_OUT 0x56 /* OV518(+) only */
79#define R518_GPIO_CTL 0x57 /* OV518(+) only */
80#define R518_GPIO_PULSE_IN 0x58 /* OV518(+) only */
81#define R518_GPIO_PULSE_CLEAR 0x59 /* OV518(+) only */
82#define R518_GPIO_PULSE_POL 0x5a /* OV518(+) only */
83#define R518_GPIO_PULSE_EN 0x5b /* OV518(+) only */
84#define R518_GPIO_RESET 0x5c /* OV518(+) only */
85
86/* I2C registers */
87#define R511_I2C_CTL 0x40
88#define R518_I2C_CTL 0x47 /* OV518(+) only */
89#define R51x_I2C_W_SID 0x41
90#define R51x_I2C_SADDR_3 0x42
91#define R51x_I2C_SADDR_2 0x43
92#define R51x_I2C_R_SID 0x44
93#define R51x_I2C_DATA 0x45
94#define R51x_I2C_CLOCK 0x46
95#define R51x_I2C_TIMEOUT 0x47
96
97/* I2C snapshot registers */
98#define R511_SI2C_SADDR_3 0x48
99#define R511_SI2C_DATA 0x49
100
101/* System control registers */
102#define R51x_SYS_RESET 0x50
103 /* Reset type definitions */
104#define OV511_RESET_UDC 0x01
105#define OV511_RESET_I2C 0x02
106#define OV511_RESET_FIFO 0x04
107#define OV511_RESET_OMNICE 0x08
108#define OV511_RESET_DRAM 0x10
109#define OV511_RESET_CAM_INT 0x20
110#define OV511_RESET_OV511 0x40
111#define OV511_RESET_NOREGS 0x3F /* All but OV511 & regs */
112#define OV511_RESET_ALL 0x7F
113
114#define R511_SYS_CLOCK_DIV 0x51
115#define R51x_SYS_SNAP 0x52
116#define R51x_SYS_INIT 0x53
117#define R511_SYS_PWR_CLK 0x54 /* OV511+/OV518(+) only */
118#define R511_SYS_LED_CTL 0x55 /* OV511+ only */
119#define R511_SYS_USER 0x5E
120#define R511_SYS_CUST_ID 0x5F
121
122/* OmniCE (compression) registers */
123#define R511_COMP_PHY 0x70
124#define R511_COMP_PHUV 0x71
125#define R511_COMP_PVY 0x72
126#define R511_COMP_PVUV 0x73
127#define R511_COMP_QHY 0x74
128#define R511_COMP_QHUV 0x75
129#define R511_COMP_QVY 0x76
130#define R511_COMP_QVUV 0x77
131#define R511_COMP_EN 0x78
132#define R511_COMP_LUT_EN 0x79
133#define R511_COMP_LUT_BEGIN 0x80
134
135/* --------------------------------- */
136/* ALTERNATE NUMBERS */
137/* --------------------------------- */
138
139/* Alternate numbers for various max packet sizes (OV511 only) */
140#define OV511_ALT_SIZE_992 0
141#define OV511_ALT_SIZE_993 1
142#define OV511_ALT_SIZE_768 2
143#define OV511_ALT_SIZE_769 3
144#define OV511_ALT_SIZE_512 4
145#define OV511_ALT_SIZE_513 5
146#define OV511_ALT_SIZE_257 6
147#define OV511_ALT_SIZE_0 7
148
149/* Alternate numbers for various max packet sizes (OV511+ only) */
150#define OV511PLUS_ALT_SIZE_0 0
151#define OV511PLUS_ALT_SIZE_33 1
152#define OV511PLUS_ALT_SIZE_129 2
153#define OV511PLUS_ALT_SIZE_257 3
154#define OV511PLUS_ALT_SIZE_385 4
155#define OV511PLUS_ALT_SIZE_513 5
156#define OV511PLUS_ALT_SIZE_769 6
157#define OV511PLUS_ALT_SIZE_961 7
158
159/* Alternate numbers for various max packet sizes (OV518(+) only) */
160#define OV518_ALT_SIZE_0 0
161#define OV518_ALT_SIZE_128 1
162#define OV518_ALT_SIZE_256 2
163#define OV518_ALT_SIZE_384 3
164#define OV518_ALT_SIZE_512 4
165#define OV518_ALT_SIZE_640 5
166#define OV518_ALT_SIZE_768 6
167#define OV518_ALT_SIZE_896 7
168
169/* --------------------------------- */
170/* OV7610 REGISTER MNEMONICS */
171/* --------------------------------- */
172
173/* OV7610 registers */
174#define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
175#define OV7610_REG_BLUE 0x01 /* blue channel balance */
176#define OV7610_REG_RED 0x02 /* red channel balance */
177#define OV7610_REG_SAT 0x03 /* saturation */
178 /* 04 reserved */
179#define OV7610_REG_CNT 0x05 /* Y contrast */
180#define OV7610_REG_BRT 0x06 /* Y brightness */
181 /* 08-0b reserved */
182#define OV7610_REG_BLUE_BIAS 0x0C /* blue channel bias (5:0) */
183#define OV7610_REG_RED_BIAS 0x0D /* read channel bias (5:0) */
184#define OV7610_REG_GAMMA_COEFF 0x0E /* gamma settings */
185#define OV7610_REG_WB_RANGE 0x0F /* AEC/ALC/S-AWB settings */
186#define OV7610_REG_EXP 0x10 /* manual exposure setting */
187#define OV7610_REG_CLOCK 0x11 /* polarity/clock prescaler */
188#define OV7610_REG_COM_A 0x12 /* misc common regs */
189#define OV7610_REG_COM_B 0x13 /* misc common regs */
190#define OV7610_REG_COM_C 0x14 /* misc common regs */
191#define OV7610_REG_COM_D 0x15 /* misc common regs */
192#define OV7610_REG_FIELD_DIVIDE 0x16 /* field interval/mode settings */
193#define OV7610_REG_HWIN_START 0x17 /* horizontal window start */
194#define OV7610_REG_HWIN_END 0x18 /* horizontal window end */
195#define OV7610_REG_VWIN_START 0x19 /* vertical window start */
196#define OV7610_REG_VWIN_END 0x1A /* vertical window end */
197#define OV7610_REG_PIXEL_SHIFT 0x1B /* pixel shift */
198#define OV7610_REG_ID_HIGH 0x1C /* manufacturer ID MSB */
199#define OV7610_REG_ID_LOW 0x1D /* manufacturer ID LSB */
200 /* 0e-0f reserved */
201#define OV7610_REG_COM_E 0x20 /* misc common regs */
202#define OV7610_REG_YOFFSET 0x21 /* Y channel offset */
203#define OV7610_REG_UOFFSET 0x22 /* U channel offset */
204 /* 23 reserved */
205#define OV7610_REG_ECW 0x24 /* Exposure white level for AEC */
206#define OV7610_REG_ECB 0x25 /* Exposure black level for AEC */
207#define OV7610_REG_COM_F 0x26 /* misc settings */
208#define OV7610_REG_COM_G 0x27 /* misc settings */
209#define OV7610_REG_COM_H 0x28 /* misc settings */
210#define OV7610_REG_COM_I 0x29 /* misc settings */
211#define OV7610_REG_FRAMERATE_H 0x2A /* frame rate MSB + misc */
212#define OV7610_REG_FRAMERATE_L 0x2B /* frame rate LSB */
213#define OV7610_REG_ALC 0x2C /* Auto Level Control settings */
214#define OV7610_REG_COM_J 0x2D /* misc settings */
215#define OV7610_REG_VOFFSET 0x2E /* V channel offset adjustment */
216#define OV7610_REG_ARRAY_BIAS 0x2F /* Array bias -- don't change */
217 /* 30-32 reserved */
218#define OV7610_REG_YGAMMA 0x33 /* misc gamma settings (7:6) */
219#define OV7610_REG_BIAS_ADJUST 0x34 /* misc bias settings */
220#define OV7610_REG_COM_L 0x35 /* misc settings */
221 /* 36-37 reserved */
222#define OV7610_REG_COM_K 0x38 /* misc registers */
223
224/* --------------------------------- */
225/* I2C ADDRESSES */
226/* --------------------------------- */
227
228#define OV7xx0_SID 0x42
229#define OV6xx0_SID 0xC0
230#define OV8xx0_SID 0xA0
231#define KS0127_SID 0xD8
232#define SAA7111A_SID 0x48
233
234/* --------------------------------- */
235/* MISCELLANEOUS DEFINES */
236/* --------------------------------- */
237
238#define I2C_CLOCK_PRESCALER 0x03
239
240#define FRAMES_PER_DESC 10 /* FIXME - What should this be? */
241#define MAX_FRAME_SIZE_PER_DESC 993 /* For statically allocated stuff */
242#define PIXELS_PER_SEG 256 /* Pixels per segment */
243
244#define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
245
246#define OV511_NUMFRAMES 2
247#if OV511_NUMFRAMES > VIDEO_MAX_FRAME
248 #error "OV511_NUMFRAMES is too high"
249#endif
250
251#define OV511_NUMSBUF 2
252
253/* Control transfers use up to 4 bytes */
254#define OV511_CBUF_SIZE 4
255
256/* Size of usb_make_path() buffer */
257#define OV511_USB_PATH_LEN 64
258
259/* Bridge types */
260enum {
261 BRG_UNKNOWN,
262 BRG_OV511,
263 BRG_OV511PLUS,
264 BRG_OV518,
265 BRG_OV518PLUS,
266};
267
268/* Bridge classes */
269enum {
270 BCL_UNKNOWN,
271 BCL_OV511,
272 BCL_OV518,
273};
274
275/* Sensor types */
276enum {
277 SEN_UNKNOWN,
278 SEN_OV76BE,
279 SEN_OV7610,
280 SEN_OV7620,
281 SEN_OV7620AE,
282 SEN_OV6620,
283 SEN_OV6630,
284 SEN_OV6630AE,
285 SEN_OV6630AF,
286 SEN_OV8600,
287 SEN_KS0127,
288 SEN_KS0127B,
289 SEN_SAA7111A,
290};
291
292enum {
293 STATE_SCANNING, /* Scanning for start */
294 STATE_HEADER, /* Parsing header */
295 STATE_LINES, /* Parsing lines */
296};
297
298/* Buffer states */
299enum {
300 BUF_NOT_ALLOCATED,
301 BUF_ALLOCATED,
302};
303
304/* --------- Definition of ioctl interface --------- */
305
306#define OV511_INTERFACE_VER 101
307
308/* LED options */
309enum {
310 LED_OFF,
311 LED_ON,
312 LED_AUTO,
313};
314
315/* Raw frame formats */
316enum {
317 RAWFMT_INVALID,
318 RAWFMT_YUV400,
319 RAWFMT_YUV420,
320 RAWFMT_YUV422,
321 RAWFMT_GBR422,
322};
323
324struct ov511_i2c_struct {
325 unsigned char slave; /* Write slave ID (read ID - 1) */
326 unsigned char reg; /* Index of register */
327 unsigned char value; /* User sets this w/ write, driver does w/ read */
328 unsigned char mask; /* Bits to be changed. Not used with read ops */
329};
330
331/* ioctls */
332#define OV511IOC_WI2C _IOW('v', BASE_VIDIOCPRIVATE + 5, \
333 struct ov511_i2c_struct)
334#define OV511IOC_RI2C _IOWR('v', BASE_VIDIOCPRIVATE + 6, \
335 struct ov511_i2c_struct)
336/* ------------- End IOCTL interface -------------- */
337
338struct usb_ov511; /* Forward declaration */
339
340struct ov511_sbuf {
341 struct usb_ov511 *ov;
342 unsigned char *data;
343 struct urb *urb;
344 spinlock_t lock;
345 int n;
346};
347
348enum {
349 FRAME_UNUSED, /* Unused (no MCAPTURE) */
350 FRAME_READY, /* Ready to start grabbing */
351 FRAME_GRABBING, /* In the process of being grabbed into */
352 FRAME_DONE, /* Finished grabbing, but not been synced yet */
353 FRAME_ERROR, /* Something bad happened while processing */
354};
355
356struct ov511_regvals {
357 enum {
358 OV511_DONE_BUS,
359 OV511_REG_BUS,
360 OV511_I2C_BUS,
361 } bus;
362 unsigned char reg;
363 unsigned char val;
364};
365
366struct ov511_frame {
367 int framenum; /* Index of this frame */
368 unsigned char *data; /* Frame buffer */
369 unsigned char *tempdata; /* Temp buffer for multi-stage conversions */
370 unsigned char *rawdata; /* Raw camera data buffer */
371 unsigned char *compbuf; /* Temp buffer for decompressor */
372
373 int depth; /* Bytes per pixel */
374 int width; /* Width application is expecting */
375 int height; /* Height application is expecting */
376
377 int rawwidth; /* Actual width of frame sent from camera */
378 int rawheight; /* Actual height of frame sent from camera */
379
380 int sub_flag; /* Sub-capture mode for this frame? */
381 unsigned int format; /* Format for this frame */
382 int compressed; /* Is frame compressed? */
383
384 volatile int grabstate; /* State of grabbing */
385 int scanstate; /* State of scanning */
386
387 int bytes_recvd; /* Number of image bytes received from camera */
388
389 long bytes_read; /* Amount that has been read() */
390
391 wait_queue_head_t wq; /* Processes waiting */
392
393 int snapshot; /* True if frame was a snapshot */
394};
395
396#define DECOMP_INTERFACE_VER 4
397
398/* Compression module operations */
399struct ov51x_decomp_ops {
400 int (*decomp_400)(unsigned char *, unsigned char *, unsigned char *,
401 int, int, int);
402 int (*decomp_420)(unsigned char *, unsigned char *, unsigned char *,
403 int, int, int);
404 int (*decomp_422)(unsigned char *, unsigned char *, unsigned char *,
405 int, int, int);
406 struct module *owner;
407};
408
409struct usb_ov511 {
410 struct video_device *vdev;
411 struct usb_device *dev;
412
413 int customid;
414 char *desc;
415 unsigned char iface;
416 char usb_path[OV511_USB_PATH_LEN];
417
418 /* Determined by sensor type */
419 int maxwidth;
420 int maxheight;
421 int minwidth;
422 int minheight;
423
424 int brightness;
425 int colour;
426 int contrast;
427 int hue;
428 int whiteness;
429 int exposure;
430 int auto_brt; /* Auto brightness enabled flag */
431 int auto_gain; /* Auto gain control enabled flag */
432 int auto_exp; /* Auto exposure enabled flag */
433 int backlight; /* Backlight exposure algorithm flag */
434 int mirror; /* Image is reversed horizontally */
435
436 int led_policy; /* LED: off|on|auto; OV511+ only */
437
438 struct semaphore lock; /* Serializes user-accessible operations */
439 int user; /* user count for exclusive use */
440
441 int streaming; /* Are we streaming Isochronous? */
442 int grabbing; /* Are we grabbing? */
443
444 int compress; /* Should the next frame be compressed? */
445 int compress_inited; /* Are compression params uploaded? */
446
447 int lightfreq; /* Power (lighting) frequency */
448 int bandfilt; /* Banding filter enabled flag */
449
450 unsigned char *fbuf; /* Videodev buffer area */
451 unsigned char *tempfbuf; /* Temporary (intermediate) buffer area */
452 unsigned char *rawfbuf; /* Raw camera data buffer area */
453
454 int sub_flag; /* Pix Array subcapture on flag */
455 int subx; /* Pix Array subcapture x offset */
456 int suby; /* Pix Array subcapture y offset */
457 int subw; /* Pix Array subcapture width */
458 int subh; /* Pix Array subcapture height */
459
460 int curframe; /* Current receiving sbuf */
461 struct ov511_frame frame[OV511_NUMFRAMES];
462
463 struct ov511_sbuf sbuf[OV511_NUMSBUF];
464
465 wait_queue_head_t wq; /* Processes waiting */
466
467 int snap_enabled; /* Snapshot mode enabled */
468
469 int bridge; /* Type of bridge (BRG_*) */
470 int bclass; /* Class of bridge (BCL_*) */
471 int sensor; /* Type of image sensor chip (SEN_*) */
472
473 int packet_size; /* Frame size per isoc desc */
474 int packet_numbering; /* Is ISO frame numbering enabled? */
475
476 struct semaphore param_lock; /* params lock for this camera */
477
478 /* Framebuffer/sbuf management */
479 int buf_state;
480 struct semaphore buf_lock;
481
482 struct ov51x_decomp_ops *decomp_ops;
483
484 /* Stop streaming while changing picture settings */
485 int stop_during_set;
486
487 int stopped; /* Streaming is temporarily paused */
488
489 /* Video decoder stuff */
490 int input; /* Composite, S-VIDEO, etc... */
491 int num_inputs; /* Number of inputs */
492 int norm; /* NTSC / PAL / SECAM */
493 int has_decoder; /* Device has a video decoder */
494 int pal; /* Device is designed for PAL resolution */
495
496 /* I2C interface */
497 struct semaphore i2c_lock; /* Protect I2C controller regs */
498 unsigned char primary_i2c_slave; /* I2C write id of sensor */
499
500 /* Control transaction stuff */
501 unsigned char *cbuf; /* Buffer for payload */
502 struct semaphore cbuf_lock;
503};
504
505/* Used to represent a list of values and their respective symbolic names */
506struct symbolic_list {
507 int num;
508 char *name;
509};
510
511#define NOT_DEFINED_STR "Unknown"
512
513/* Returns the name of the matching element in the symbolic_list array. The
514 * end of the list must be marked with an element that has a NULL name.
515 */
516static inline char *
517symbolic(struct symbolic_list list[], int num)
518{
519 int i;
520
521 for (i = 0; list[i].name != NULL; i++)
522 if (list[i].num == num)
523 return (list[i].name);
524
525 return (NOT_DEFINED_STR);
526}
527
528/* Compression stuff */
529
530#define OV511_QUANTABLESIZE 64
531#define OV518_QUANTABLESIZE 32
532
533#define OV511_YQUANTABLE { \
534 0, 1, 1, 2, 2, 3, 3, 4, \
535 1, 1, 1, 2, 2, 3, 4, 4, \
536 1, 1, 2, 2, 3, 4, 4, 4, \
537 2, 2, 2, 3, 4, 4, 4, 4, \
538 2, 2, 3, 4, 4, 5, 5, 5, \
539 3, 3, 4, 4, 5, 5, 5, 5, \
540 3, 4, 4, 4, 5, 5, 5, 5, \
541 4, 4, 4, 4, 5, 5, 5, 5 \
542}
543
544#define OV511_UVQUANTABLE { \
545 0, 2, 2, 3, 4, 4, 4, 4, \
546 2, 2, 2, 4, 4, 4, 4, 4, \
547 2, 2, 3, 4, 4, 4, 4, 4, \
548 3, 4, 4, 4, 4, 4, 4, 4, \
549 4, 4, 4, 4, 4, 4, 4, 4, \
550 4, 4, 4, 4, 4, 4, 4, 4, \
551 4, 4, 4, 4, 4, 4, 4, 4, \
552 4, 4, 4, 4, 4, 4, 4, 4 \
553}
554
555#define OV518_YQUANTABLE { \
556 5, 4, 5, 6, 6, 7, 7, 7, \
557 5, 5, 5, 5, 6, 7, 7, 7, \
558 6, 6, 6, 6, 7, 7, 7, 8, \
559 7, 7, 6, 7, 7, 7, 8, 8 \
560}
561
562#define OV518_UVQUANTABLE { \
563 6, 6, 6, 7, 7, 7, 7, 7, \
564 6, 6, 6, 7, 7, 7, 7, 7, \
565 6, 6, 6, 7, 7, 7, 7, 8, \
566 7, 7, 7, 7, 7, 7, 8, 8 \
567}
568
569#endif
diff --git a/drivers/usb/media/pwc/Makefile b/drivers/usb/media/pwc/Makefile
deleted file mode 100644
index 2d93a775011a..000000000000
--- a/drivers/usb/media/pwc/Makefile
+++ /dev/null
@@ -1,20 +0,0 @@
1ifneq ($(KERNELRELEASE),)
2
3pwc-objs := pwc-if.o pwc-misc.o pwc-ctrl.o pwc-uncompress.o pwc-timon.o pwc-kiara.o
4
5obj-$(CONFIG_USB_PWC) += pwc.o
6
7else
8
9KDIR := /lib/modules/$(shell uname -r)/build
10PWD := $(shell pwd)
11
12default:
13 $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
14
15endif
16
17clean:
18 rm -f *.[oas] .*.flags *.ko .*.cmd .*.d .*.tmp *.mod.c
19 rm -rf .tmp_versions
20
diff --git a/drivers/usb/media/pwc/philips.txt b/drivers/usb/media/pwc/philips.txt
deleted file mode 100644
index 04a640d723ed..000000000000
--- a/drivers/usb/media/pwc/philips.txt
+++ /dev/null
@@ -1,236 +0,0 @@
1This file contains some additional information for the Philips and OEM webcams.
2E-mail: webcam@smcc.demon.nl Last updated: 2004-01-19
3Site: http://www.smcc.demon.nl/webcam/
4
5As of this moment, the following cameras are supported:
6 * Philips PCA645
7 * Philips PCA646
8 * Philips PCVC675
9 * Philips PCVC680
10 * Philips PCVC690
11 * Philips PCVC720/40
12 * Philips PCVC730
13 * Philips PCVC740
14 * Philips PCVC750
15 * Askey VC010
16 * Creative Labs Webcam 5
17 * Creative Labs Webcam Pro Ex
18 * Logitech QuickCam 3000 Pro
19 * Logitech QuickCam 4000 Pro
20 * Logitech QuickCam Notebook Pro
21 * Logitech QuickCam Zoom
22 * Logitech QuickCam Orbit
23 * Logitech QuickCam Sphere
24 * Samsung MPC-C10
25 * Samsung MPC-C30
26 * Sotec Afina Eye
27 * AME CU-001
28 * Visionite VCS-UM100
29 * Visionite VCS-UC300
30
31The main webpage for the Philips driver is at the address above. It contains
32a lot of extra information, a FAQ, and the binary plugin 'PWCX'. This plugin
33contains decompression routines that allow you to use higher image sizes and
34framerates; in addition the webcam uses less bandwidth on the USB bus (handy
35if you want to run more than 1 camera simultaneously). These routines fall
36under a NDA, and may therefor not be distributed as source; however, its use
37is completely optional.
38
39You can build this code either into your kernel, or as a module. I recommend
40the latter, since it makes troubleshooting a lot easier. The built-in
41microphone is supported through the USB Audio class.
42
43When you load the module you can set some default settings for the
44camera; some programs depend on a particular image-size or -format and
45don't know how to set it properly in the driver. The options are:
46
47size
48 Can be one of 'sqcif', 'qsif', 'qcif', 'sif', 'cif' or
49 'vga', for an image size of resp. 128x96, 160x120, 176x144,
50 320x240, 352x288 and 640x480 (of course, only for those cameras that
51 support these resolutions).
52
53fps
54 Specifies the desired framerate. Is an integer in the range of 4-30.
55
56fbufs
57 This paramter specifies the number of internal buffers to use for storing
58 frames from the cam. This will help if the process that reads images from
59 the cam is a bit slow or momentarely busy. However, on slow machines it
60 only introduces lag, so choose carefully. The default is 3, which is
61 reasonable. You can set it between 2 and 5.
62
63mbufs
64 This is an integer between 1 and 10. It will tell the module the number of
65 buffers to reserve for mmap(), VIDIOCCGMBUF, VIDIOCMCAPTURE and friends.
66 The default is 2, which is adequate for most applications (double
67 buffering).
68
69 Should you experience a lot of 'Dumping frame...' messages during
70 grabbing with a tool that uses mmap(), you might want to increase if.
71 However, it doesn't really buffer images, it just gives you a bit more
72 slack when your program is behind. But you need a multi-threaded or
73 forked program to really take advantage of these buffers.
74
75 The absolute maximum is 10, but don't set it too high! Every buffer takes
76 up 460 KB of RAM, so unless you have a lot of memory setting this to
77 something more than 4 is an absolute waste. This memory is only
78 allocated during open(), so nothing is wasted when the camera is not in
79 use.
80
81power_save
82 When power_save is enabled (set to 1), the module will try to shut down
83 the cam on close() and re-activate on open(). This will save power and
84 turn off the LED. Not all cameras support this though (the 645 and 646
85 don't have power saving at all), and some models don't work either (they
86 will shut down, but never wake up). Consider this experimental. By
87 default this option is disabled.
88
89compression (only useful with the plugin)
90 With this option you can control the compression factor that the camera
91 uses to squeeze the image through the USB bus. You can set the
92 parameter between 0 and 3:
93 0 = prefer uncompressed images; if the requested mode is not available
94 in an uncompressed format, the driver will silently switch to low
95 compression.
96 1 = low compression.
97 2 = medium compression.
98 3 = high compression.
99
100 High compression takes less bandwidth of course, but it could also
101 introduce some unwanted artefacts. The default is 2, medium compression.
102 See the FAQ on the website for an overview of which modes require
103 compression.
104
105 The compression parameter does not apply to the 645 and 646 cameras
106 and OEM models derived from those (only a few). Most cams honour this
107 parameter.
108
109leds
110 This settings takes 2 integers, that define the on/off time for the LED
111 (in milliseconds). One of the interesting things that you can do with
112 this is let the LED blink while the camera is in use. This:
113
114 leds=500,500
115
116 will blink the LED once every second. But with:
117
118 leds=0,0
119
120 the LED never goes on, making it suitable for silent surveillance.
121
122 By default the camera's LED is on solid while in use, and turned off
123 when the camera is not used anymore.
124
125 This parameter works only with the ToUCam range of cameras (720, 730, 740,
126 750) and OEMs. For other cameras this command is silently ignored, and
127 the LED cannot be controlled.
128
129 Finally: this parameters does not take effect UNTIL the first time you
130 open the camera device. Until then, the LED remains on.
131
132dev_hint
133 A long standing problem with USB devices is their dynamic nature: you
134 never know what device a camera gets assigned; it depends on module load
135 order, the hub configuration, the order in which devices are plugged in,
136 and the phase of the moon (i.e. it can be random). With this option you
137 can give the driver a hint as to what video device node (/dev/videoX) it
138 should use with a specific camera. This is also handy if you have two
139 cameras of the same model.
140
141 A camera is specified by its type (the number from the camera model,
142 like PCA645, PCVC750VC, etc) and optionally the serial number (visible
143 in /proc/bus/usb/devices). A hint consists of a string with the following
144 format:
145
146 [type[.serialnumber]:]node
147
148 The square brackets mean that both the type and the serialnumber are
149 optional, but a serialnumber cannot be specified without a type (which
150 would be rather pointless). The serialnumber is separated from the type
151 by a '.'; the node number by a ':'.
152
153 This somewhat cryptic syntax is best explained by a few examples:
154
155 dev_hint=3,5 The first detected cam gets assigned
156 /dev/video3, the second /dev/video5. Any
157 other cameras will get the first free
158 available slot (see below).
159
160 dev_hint=645:1,680:2 The PCA645 camera will get /dev/video1,
161 and a PCVC680 /dev/video2.
162
163 dev_hint=645.0123:3,645.4567:0 The PCA645 camera with serialnumber
164 0123 goes to /dev/video3, the same
165 camera model with the 4567 serial
166 gets /dev/video0.
167
168 dev_hint=750:1,4,5,6 The PCVC750 camera will get /dev/video1, the
169 next 3 Philips cams will use /dev/video4
170 through /dev/video6.
171
172 Some points worth knowing:
173 - Serialnumbers are case sensitive and must be written full, including
174 leading zeroes (it's treated as a string).
175 - If a device node is already occupied, registration will fail and
176 the webcam is not available.
177 - You can have up to 64 video devices; be sure to make enough device
178 nodes in /dev if you want to spread the numbers (this does not apply
179 to devfs). After /dev/video9 comes /dev/video10 (not /dev/videoA).
180 - If a camera does not match any dev_hint, it will simply get assigned
181 the first available device node, just as it used to be.
182
183trace
184 In order to better detect problems, it is now possible to turn on a
185 'trace' of some of the calls the module makes; it logs all items in your
186 kernel log at debug level.
187
188 The trace variable is a bitmask; each bit represents a certain feature.
189 If you want to trace something, look up the bit value(s) in the table
190 below, add the values together and supply that to the trace variable.
191
192 Value Value Description Default
193 (dec) (hex)
194 1 0x1 Module initialization; this will log messages On
195 while loading and unloading the module
196
197 2 0x2 probe() and disconnect() traces On
198
199 4 0x4 Trace open() and close() calls Off
200
201 8 0x8 read(), mmap() and associated ioctl() calls Off
202
203 16 0x10 Memory allocation of buffers, etc. Off
204
205 32 0x20 Showing underflow, overflow and Dumping frame On
206 messages
207
208 64 0x40 Show viewport and image sizes Off
209
210 128 0x80 PWCX debugging Off
211
212 For example, to trace the open() & read() fuctions, sum 8 + 4 = 12,
213 so you would supply trace=12 during insmod or modprobe. If
214 you want to turn the initialization and probing tracing off, set trace=0.
215 The default value for trace is 35 (0x23).
216
217
218
219Example:
220
221 # modprobe pwc size=cif fps=15 power_save=1
222
223The fbufs, mbufs and trace parameters are global and apply to all connected
224cameras. Each camera has its own set of buffers.
225
226size and fps only specify defaults when you open() the device; this is to
227accommodate some tools that don't set the size. You can change these
228settings after open() with the Video4Linux ioctl() calls. The default of
229defaults is QCIF size at 10 fps.
230
231The compression parameter is semiglobal; it sets the initial compression
232preference for all camera's, but this parameter can be set per camera with
233the VIDIOCPWCSCQUAL ioctl() call.
234
235All parameters are optional.
236
diff --git a/drivers/usb/media/pwc/pwc-ctrl.c b/drivers/usb/media/pwc/pwc-ctrl.c
deleted file mode 100644
index 3ebb6e9cdf92..000000000000
--- a/drivers/usb/media/pwc/pwc-ctrl.c
+++ /dev/null
@@ -1,1542 +0,0 @@
1/* Driver for Philips webcam
2 Functions that send various control messages to the webcam, including
3 video modes.
4 (C) 1999-2003 Nemosoft Unv.
5 (C) 2004 Luc Saillard (luc@saillard.org)
6
7 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
8 driver and thus may have bugs that are not present in the original version.
9 Please send bug reports and support requests to <luc@saillard.org>.
10
11 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
12 driver and thus may have bugs that are not present in the original version.
13 Please send bug reports and support requests to <luc@saillard.org>.
14 The decompression routines have been implemented by reverse-engineering the
15 Nemosoft binary pwcx module. Caveat emptor.
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30*/
31
32/*
33 Changes
34 2001/08/03 Alvarado Added methods for changing white balance and
35 red/green gains
36 */
37
38/* Control functions for the cam; brightness, contrast, video mode, etc. */
39
40#ifdef __KERNEL__
41#include <asm/uaccess.h>
42#endif
43#include <asm/errno.h>
44#include <linux/version.h>
45
46#include "pwc.h"
47#include "pwc-ioctl.h"
48#include "pwc-uncompress.h"
49#include "pwc-kiara.h"
50#include "pwc-timon.h"
51
52/* Request types: video */
53#define SET_LUM_CTL 0x01
54#define GET_LUM_CTL 0x02
55#define SET_CHROM_CTL 0x03
56#define GET_CHROM_CTL 0x04
57#define SET_STATUS_CTL 0x05
58#define GET_STATUS_CTL 0x06
59#define SET_EP_STREAM_CTL 0x07
60#define GET_EP_STREAM_CTL 0x08
61#define SET_MPT_CTL 0x0D
62#define GET_MPT_CTL 0x0E
63
64/* Selectors for the Luminance controls [GS]ET_LUM_CTL */
65#define AGC_MODE_FORMATTER 0x2000
66#define PRESET_AGC_FORMATTER 0x2100
67#define SHUTTER_MODE_FORMATTER 0x2200
68#define PRESET_SHUTTER_FORMATTER 0x2300
69#define PRESET_CONTOUR_FORMATTER 0x2400
70#define AUTO_CONTOUR_FORMATTER 0x2500
71#define BACK_LIGHT_COMPENSATION_FORMATTER 0x2600
72#define CONTRAST_FORMATTER 0x2700
73#define DYNAMIC_NOISE_CONTROL_FORMATTER 0x2800
74#define FLICKERLESS_MODE_FORMATTER 0x2900
75#define AE_CONTROL_SPEED 0x2A00
76#define BRIGHTNESS_FORMATTER 0x2B00
77#define GAMMA_FORMATTER 0x2C00
78
79/* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
80#define WB_MODE_FORMATTER 0x1000
81#define AWB_CONTROL_SPEED_FORMATTER 0x1100
82#define AWB_CONTROL_DELAY_FORMATTER 0x1200
83#define PRESET_MANUAL_RED_GAIN_FORMATTER 0x1300
84#define PRESET_MANUAL_BLUE_GAIN_FORMATTER 0x1400
85#define COLOUR_MODE_FORMATTER 0x1500
86#define SATURATION_MODE_FORMATTER1 0x1600
87#define SATURATION_MODE_FORMATTER2 0x1700
88
89/* Selectors for the Status controls [GS]ET_STATUS_CTL */
90#define SAVE_USER_DEFAULTS_FORMATTER 0x0200
91#define RESTORE_USER_DEFAULTS_FORMATTER 0x0300
92#define RESTORE_FACTORY_DEFAULTS_FORMATTER 0x0400
93#define READ_AGC_FORMATTER 0x0500
94#define READ_SHUTTER_FORMATTER 0x0600
95#define READ_RED_GAIN_FORMATTER 0x0700
96#define READ_BLUE_GAIN_FORMATTER 0x0800
97#define SENSOR_TYPE_FORMATTER1 0x0C00
98#define READ_RAW_Y_MEAN_FORMATTER 0x3100
99#define SET_POWER_SAVE_MODE_FORMATTER 0x3200
100#define MIRROR_IMAGE_FORMATTER 0x3300
101#define LED_FORMATTER 0x3400
102#define SENSOR_TYPE_FORMATTER2 0x3700
103
104/* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
105#define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
106
107/* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
108#define PT_RELATIVE_CONTROL_FORMATTER 0x01
109#define PT_RESET_CONTROL_FORMATTER 0x02
110#define PT_STATUS_FORMATTER 0x03
111
112static const char *size2name[PSZ_MAX] =
113{
114 "subQCIF",
115 "QSIF",
116 "QCIF",
117 "SIF",
118 "CIF",
119 "VGA",
120};
121
122/********/
123
124/* Entries for the Nala (645/646) camera; the Nala doesn't have compression
125 preferences, so you either get compressed or non-compressed streams.
126
127 An alternate value of 0 means this mode is not available at all.
128 */
129
130struct Nala_table_entry {
131 char alternate; /* USB alternate setting */
132 int compressed; /* Compressed yes/no */
133
134 unsigned char mode[3]; /* precomputed mode table */
135};
136
137static struct Nala_table_entry Nala_table[PSZ_MAX][8] =
138{
139#include "pwc-nala.h"
140};
141
142
143/****************************************************************************/
144
145
146#define SendControlMsg(request, value, buflen) \
147 usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0), \
148 request, \
149 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
150 value, \
151 pdev->vcinterface, \
152 &buf, buflen, 500)
153
154#define RecvControlMsg(request, value, buflen) \
155 usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0), \
156 request, \
157 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
158 value, \
159 pdev->vcinterface, \
160 &buf, buflen, 500)
161
162
163#if PWC_DEBUG
164void pwc_hexdump(void *p, int len)
165{
166 int i;
167 unsigned char *s;
168 char buf[100], *d;
169
170 s = (unsigned char *)p;
171 d = buf;
172 *d = '\0';
173 Debug("Doing hexdump @ %p, %d bytes.\n", p, len);
174 for (i = 0; i < len; i++) {
175 d += sprintf(d, "%02X ", *s++);
176 if ((i & 0xF) == 0xF) {
177 Debug("%s\n", buf);
178 d = buf;
179 *d = '\0';
180 }
181 }
182 if ((i & 0xF) != 0)
183 Debug("%s\n", buf);
184}
185#endif
186
187static inline int send_video_command(struct usb_device *udev, int index, void *buf, int buflen)
188{
189 return usb_control_msg(udev,
190 usb_sndctrlpipe(udev, 0),
191 SET_EP_STREAM_CTL,
192 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
193 VIDEO_OUTPUT_CONTROL_FORMATTER,
194 index,
195 buf, buflen, 1000);
196}
197
198
199
200static inline int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
201{
202 unsigned char buf[3];
203 int ret, fps;
204 struct Nala_table_entry *pEntry;
205 int frames2frames[31] =
206 { /* closest match of framerate */
207 0, 0, 0, 0, 4, /* 0-4 */
208 5, 5, 7, 7, 10, /* 5-9 */
209 10, 10, 12, 12, 15, /* 10-14 */
210 15, 15, 15, 20, 20, /* 15-19 */
211 20, 20, 20, 24, 24, /* 20-24 */
212 24, 24, 24, 24, 24, /* 25-29 */
213 24 /* 30 */
214 };
215 int frames2table[31] =
216 { 0, 0, 0, 0, 0, /* 0-4 */
217 1, 1, 1, 2, 2, /* 5-9 */
218 3, 3, 4, 4, 4, /* 10-14 */
219 5, 5, 5, 5, 5, /* 15-19 */
220 6, 6, 6, 6, 7, /* 20-24 */
221 7, 7, 7, 7, 7, /* 25-29 */
222 7 /* 30 */
223 };
224
225 if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
226 return -EINVAL;
227 frames = frames2frames[frames];
228 fps = frames2table[frames];
229 pEntry = &Nala_table[size][fps];
230 if (pEntry->alternate == 0)
231 return -EINVAL;
232
233 if (pEntry->compressed)
234 return -ENOENT; /* Not supported. */
235
236 memcpy(buf, pEntry->mode, 3);
237 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 3);
238 if (ret < 0) {
239 Debug("Failed to send video command... %d\n", ret);
240 return ret;
241 }
242 if (pEntry->compressed && pdev->vpalette != VIDEO_PALETTE_RAW)
243 {
244 switch(pdev->type) {
245 case 645:
246 case 646:
247/* pwc_dec1_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
248 break;
249
250 case 675:
251 case 680:
252 case 690:
253 case 720:
254 case 730:
255 case 740:
256 case 750:
257/* pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
258 break;
259 }
260 }
261
262 pdev->cmd_len = 3;
263 memcpy(pdev->cmd_buf, buf, 3);
264
265 /* Set various parameters */
266 pdev->vframes = frames;
267 pdev->vsize = size;
268 pdev->valternate = pEntry->alternate;
269 pdev->image = pwc_image_sizes[size];
270 pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
271 if (pEntry->compressed) {
272 if (pdev->release < 5) { /* 4 fold compression */
273 pdev->vbandlength = 528;
274 pdev->frame_size /= 4;
275 }
276 else {
277 pdev->vbandlength = 704;
278 pdev->frame_size /= 3;
279 }
280 }
281 else
282 pdev->vbandlength = 0;
283 return 0;
284}
285
286
287static inline int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
288{
289 unsigned char buf[13];
290 const struct Timon_table_entry *pChoose;
291 int ret, fps;
292
293 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
294 return -EINVAL;
295 if (size == PSZ_VGA && frames > 15)
296 return -EINVAL;
297 fps = (frames / 5) - 1;
298
299 /* Find a supported framerate with progressively higher compression ratios
300 if the preferred ratio is not available.
301 */
302 pChoose = NULL;
303 while (compression <= 3) {
304 pChoose = &Timon_table[size][fps][compression];
305 if (pChoose->alternate != 0)
306 break;
307 compression++;
308 }
309 if (pChoose == NULL || pChoose->alternate == 0)
310 return -ENOENT; /* Not supported. */
311
312 memcpy(buf, pChoose->mode, 13);
313 if (snapshot)
314 buf[0] |= 0x80;
315 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 13);
316 if (ret < 0)
317 return ret;
318
319/* if (pChoose->bandlength > 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
320 pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
321
322 pdev->cmd_len = 13;
323 memcpy(pdev->cmd_buf, buf, 13);
324
325 /* Set various parameters */
326 pdev->vframes = frames;
327 pdev->vsize = size;
328 pdev->vsnapshot = snapshot;
329 pdev->valternate = pChoose->alternate;
330 pdev->image = pwc_image_sizes[size];
331 pdev->vbandlength = pChoose->bandlength;
332 if (pChoose->bandlength > 0)
333 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
334 else
335 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
336 return 0;
337}
338
339
340static inline int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
341{
342 const struct Kiara_table_entry *pChoose = NULL;
343 int fps, ret;
344 unsigned char buf[12];
345 struct Kiara_table_entry RawEntry = {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
346
347 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
348 return -EINVAL;
349 if (size == PSZ_VGA && frames > 15)
350 return -EINVAL;
351 fps = (frames / 5) - 1;
352
353 /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
354 if (size == PSZ_VGA && frames == 5 && snapshot)
355 {
356 /* Only available in case the raw palette is selected or
357 we have the decompressor available. This mode is
358 only available in compressed form
359 */
360 if (pdev->vpalette == VIDEO_PALETTE_RAW)
361 {
362 Info("Choosing VGA/5 BAYER mode (%d).\n", pdev->vpalette);
363 pChoose = &RawEntry;
364 }
365 else
366 {
367 Info("VGA/5 BAYER mode _must_ have a decompressor available, or use RAW palette.\n");
368 }
369 }
370 else
371 {
372 /* Find a supported framerate with progressively higher compression ratios
373 if the preferred ratio is not available.
374 Skip this step when using RAW modes.
375 */
376 while (compression <= 3) {
377 pChoose = &Kiara_table[size][fps][compression];
378 if (pChoose->alternate != 0)
379 break;
380 compression++;
381 }
382 }
383 if (pChoose == NULL || pChoose->alternate == 0)
384 return -ENOENT; /* Not supported. */
385
386 Debug("Using alternate setting %d.\n", pChoose->alternate);
387
388 /* usb_control_msg won't take staticly allocated arrays as argument?? */
389 memcpy(buf, pChoose->mode, 12);
390 if (snapshot)
391 buf[0] |= 0x80;
392
393 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
394 ret = send_video_command(pdev->udev, 4 /* pdev->vendpoint */, buf, 12);
395 if (ret < 0)
396 return ret;
397
398/* if (pChoose->bandlength > 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
399 pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
400
401 pdev->cmd_len = 12;
402 memcpy(pdev->cmd_buf, buf, 12);
403 /* All set and go */
404 pdev->vframes = frames;
405 pdev->vsize = size;
406 pdev->vsnapshot = snapshot;
407 pdev->valternate = pChoose->alternate;
408 pdev->image = pwc_image_sizes[size];
409 pdev->vbandlength = pChoose->bandlength;
410 if (pdev->vbandlength > 0)
411 pdev->frame_size = (pdev->vbandlength * pdev->image.y) / 4;
412 else
413 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
414 return 0;
415}
416
417
418
419static void pwc_set_image_buffer_size(struct pwc_device *pdev)
420{
421 int i, factor = 0, filler = 0;
422
423 /* for PALETTE_YUV420P */
424 switch(pdev->vpalette)
425 {
426 case VIDEO_PALETTE_YUV420P:
427 factor = 6;
428 filler = 128;
429 break;
430 case VIDEO_PALETTE_RAW:
431 factor = 6; /* can be uncompressed YUV420P */
432 filler = 0;
433 break;
434 }
435
436 /* Set sizes in bytes */
437 pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
438 pdev->view.size = pdev->view.x * pdev->view.y * factor / 4;
439
440 /* Align offset, or you'll get some very weird results in
441 YUV420 mode... x must be multiple of 4 (to get the Y's in
442 place), and y even (or you'll mixup U & V). This is less of a
443 problem for YUV420P.
444 */
445 pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
446 pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
447
448 /* Fill buffers with gray or black */
449 for (i = 0; i < MAX_IMAGES; i++) {
450 if (pdev->image_ptr[i] != NULL)
451 memset(pdev->image_ptr[i], filler, pdev->view.size);
452 }
453}
454
455
456
457/**
458 @pdev: device structure
459 @width: viewport width
460 @height: viewport height
461 @frame: framerate, in fps
462 @compression: preferred compression ratio
463 @snapshot: snapshot mode or streaming
464 */
465int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
466{
467 int ret, size;
468
469 Trace(TRACE_FLOW, "set_video_mode(%dx%d @ %d, palette %d).\n", width, height, frames, pdev->vpalette);
470 size = pwc_decode_size(pdev, width, height);
471 if (size < 0) {
472 Debug("Could not find suitable size.\n");
473 return -ERANGE;
474 }
475 Debug("decode_size = %d.\n", size);
476
477 ret = -EINVAL;
478 switch(pdev->type) {
479 case 645:
480 case 646:
481 ret = set_video_mode_Nala(pdev, size, frames);
482 break;
483
484 case 675:
485 case 680:
486 case 690:
487 ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
488 break;
489
490 case 720:
491 case 730:
492 case 740:
493 case 750:
494 ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
495 break;
496 }
497 if (ret < 0) {
498 if (ret == -ENOENT)
499 Info("Video mode %s@%d fps is only supported with the decompressor module (pwcx).\n", size2name[size], frames);
500 else {
501 Err("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
502 }
503 return ret;
504 }
505 pdev->view.x = width;
506 pdev->view.y = height;
507 pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
508 pwc_set_image_buffer_size(pdev);
509 Trace(TRACE_SIZE, "Set viewport to %dx%d, image size is %dx%d.\n", width, height, pwc_image_sizes[size].x, pwc_image_sizes[size].y);
510 return 0;
511}
512
513
514/* BRIGHTNESS */
515
516int pwc_get_brightness(struct pwc_device *pdev)
517{
518 char buf;
519 int ret;
520
521 ret = RecvControlMsg(GET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
522 if (ret < 0)
523 return ret;
524 return buf << 9;
525}
526
527int pwc_set_brightness(struct pwc_device *pdev, int value)
528{
529 char buf;
530
531 if (value < 0)
532 value = 0;
533 if (value > 0xffff)
534 value = 0xffff;
535 buf = (value >> 9) & 0x7f;
536 return SendControlMsg(SET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
537}
538
539/* CONTRAST */
540
541int pwc_get_contrast(struct pwc_device *pdev)
542{
543 char buf;
544 int ret;
545
546 ret = RecvControlMsg(GET_LUM_CTL, CONTRAST_FORMATTER, 1);
547 if (ret < 0)
548 return ret;
549 return buf << 10;
550}
551
552int pwc_set_contrast(struct pwc_device *pdev, int value)
553{
554 char buf;
555
556 if (value < 0)
557 value = 0;
558 if (value > 0xffff)
559 value = 0xffff;
560 buf = (value >> 10) & 0x3f;
561 return SendControlMsg(SET_LUM_CTL, CONTRAST_FORMATTER, 1);
562}
563
564/* GAMMA */
565
566int pwc_get_gamma(struct pwc_device *pdev)
567{
568 char buf;
569 int ret;
570
571 ret = RecvControlMsg(GET_LUM_CTL, GAMMA_FORMATTER, 1);
572 if (ret < 0)
573 return ret;
574 return buf << 11;
575}
576
577int pwc_set_gamma(struct pwc_device *pdev, int value)
578{
579 char buf;
580
581 if (value < 0)
582 value = 0;
583 if (value > 0xffff)
584 value = 0xffff;
585 buf = (value >> 11) & 0x1f;
586 return SendControlMsg(SET_LUM_CTL, GAMMA_FORMATTER, 1);
587}
588
589
590/* SATURATION */
591
592int pwc_get_saturation(struct pwc_device *pdev)
593{
594 char buf;
595 int ret;
596
597 if (pdev->type < 675)
598 return -1;
599 ret = RecvControlMsg(GET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
600 if (ret < 0)
601 return ret;
602 return 32768 + buf * 327;
603}
604
605int pwc_set_saturation(struct pwc_device *pdev, int value)
606{
607 char buf;
608
609 if (pdev->type < 675)
610 return -EINVAL;
611 if (value < 0)
612 value = 0;
613 if (value > 0xffff)
614 value = 0xffff;
615 /* saturation ranges from -100 to +100 */
616 buf = (value - 32768) / 327;
617 return SendControlMsg(SET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
618}
619
620/* AGC */
621
622static inline int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
623{
624 char buf;
625 int ret;
626
627 if (mode)
628 buf = 0x0; /* auto */
629 else
630 buf = 0xff; /* fixed */
631
632 ret = SendControlMsg(SET_LUM_CTL, AGC_MODE_FORMATTER, 1);
633
634 if (!mode && ret >= 0) {
635 if (value < 0)
636 value = 0;
637 if (value > 0xffff)
638 value = 0xffff;
639 buf = (value >> 10) & 0x3F;
640 ret = SendControlMsg(SET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
641 }
642 if (ret < 0)
643 return ret;
644 return 0;
645}
646
647static inline int pwc_get_agc(struct pwc_device *pdev, int *value)
648{
649 unsigned char buf;
650 int ret;
651
652 ret = RecvControlMsg(GET_LUM_CTL, AGC_MODE_FORMATTER, 1);
653 if (ret < 0)
654 return ret;
655
656 if (buf != 0) { /* fixed */
657 ret = RecvControlMsg(GET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
658 if (ret < 0)
659 return ret;
660 if (buf > 0x3F)
661 buf = 0x3F;
662 *value = (buf << 10);
663 }
664 else { /* auto */
665 ret = RecvControlMsg(GET_STATUS_CTL, READ_AGC_FORMATTER, 1);
666 if (ret < 0)
667 return ret;
668 /* Gah... this value ranges from 0x00 ... 0x9F */
669 if (buf > 0x9F)
670 buf = 0x9F;
671 *value = -(48 + buf * 409);
672 }
673
674 return 0;
675}
676
677static inline int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
678{
679 char buf[2];
680 int speed, ret;
681
682
683 if (mode)
684 buf[0] = 0x0; /* auto */
685 else
686 buf[0] = 0xff; /* fixed */
687
688 ret = SendControlMsg(SET_LUM_CTL, SHUTTER_MODE_FORMATTER, 1);
689
690 if (!mode && ret >= 0) {
691 if (value < 0)
692 value = 0;
693 if (value > 0xffff)
694 value = 0xffff;
695 switch(pdev->type) {
696 case 675:
697 case 680:
698 case 690:
699 /* speed ranges from 0x0 to 0x290 (656) */
700 speed = (value / 100);
701 buf[1] = speed >> 8;
702 buf[0] = speed & 0xff;
703 break;
704 case 720:
705 case 730:
706 case 740:
707 case 750:
708 /* speed seems to range from 0x0 to 0xff */
709 buf[1] = 0;
710 buf[0] = value >> 8;
711 break;
712 }
713
714 ret = SendControlMsg(SET_LUM_CTL, PRESET_SHUTTER_FORMATTER, 2);
715 }
716 return ret;
717}
718
719
720/* POWER */
721
722int pwc_camera_power(struct pwc_device *pdev, int power)
723{
724 char buf;
725
726 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
727 return 0; /* Not supported by Nala or Timon < release 6 */
728
729 if (power)
730 buf = 0x00; /* active */
731 else
732 buf = 0xFF; /* power save */
733 return SendControlMsg(SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER, 1);
734}
735
736
737
738/* private calls */
739
740static inline int pwc_restore_user(struct pwc_device *pdev)
741{
742 char buf; /* dummy */
743 return SendControlMsg(SET_STATUS_CTL, RESTORE_USER_DEFAULTS_FORMATTER, 0);
744}
745
746static inline int pwc_save_user(struct pwc_device *pdev)
747{
748 char buf; /* dummy */
749 return SendControlMsg(SET_STATUS_CTL, SAVE_USER_DEFAULTS_FORMATTER, 0);
750}
751
752static inline int pwc_restore_factory(struct pwc_device *pdev)
753{
754 char buf; /* dummy */
755 return SendControlMsg(SET_STATUS_CTL, RESTORE_FACTORY_DEFAULTS_FORMATTER, 0);
756}
757
758 /* ************************************************* */
759 /* Patch by Alvarado: (not in the original version */
760
761 /*
762 * the camera recognizes modes from 0 to 4:
763 *
764 * 00: indoor (incandescant lighting)
765 * 01: outdoor (sunlight)
766 * 02: fluorescent lighting
767 * 03: manual
768 * 04: auto
769 */
770static inline int pwc_set_awb(struct pwc_device *pdev, int mode)
771{
772 char buf;
773 int ret;
774
775 if (mode < 0)
776 mode = 0;
777
778 if (mode > 4)
779 mode = 4;
780
781 buf = mode & 0x07; /* just the lowest three bits */
782
783 ret = SendControlMsg(SET_CHROM_CTL, WB_MODE_FORMATTER, 1);
784
785 if (ret < 0)
786 return ret;
787 return 0;
788}
789
790static inline int pwc_get_awb(struct pwc_device *pdev)
791{
792 unsigned char buf;
793 int ret;
794
795 ret = RecvControlMsg(GET_CHROM_CTL, WB_MODE_FORMATTER, 1);
796
797 if (ret < 0)
798 return ret;
799 return buf;
800}
801
802static inline int pwc_set_red_gain(struct pwc_device *pdev, int value)
803{
804 unsigned char buf;
805
806 if (value < 0)
807 value = 0;
808 if (value > 0xffff)
809 value = 0xffff;
810 /* only the msb is considered */
811 buf = value >> 8;
812 return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
813}
814
815static inline int pwc_get_red_gain(struct pwc_device *pdev, int *value)
816{
817 unsigned char buf;
818 int ret;
819
820 ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
821 if (ret < 0)
822 return ret;
823 *value = buf << 8;
824 return 0;
825}
826
827
828static inline int pwc_set_blue_gain(struct pwc_device *pdev, int value)
829{
830 unsigned char buf;
831
832 if (value < 0)
833 value = 0;
834 if (value > 0xffff)
835 value = 0xffff;
836 /* only the msb is considered */
837 buf = value >> 8;
838 return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
839}
840
841static inline int pwc_get_blue_gain(struct pwc_device *pdev, int *value)
842{
843 unsigned char buf;
844 int ret;
845
846 ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
847 if (ret < 0)
848 return ret;
849 *value = buf << 8;
850 return 0;
851}
852
853
854/* The following two functions are different, since they only read the
855 internal red/blue gains, which may be different from the manual
856 gains set or read above.
857 */
858static inline int pwc_read_red_gain(struct pwc_device *pdev, int *value)
859{
860 unsigned char buf;
861 int ret;
862
863 ret = RecvControlMsg(GET_STATUS_CTL, READ_RED_GAIN_FORMATTER, 1);
864 if (ret < 0)
865 return ret;
866 *value = buf << 8;
867 return 0;
868}
869
870static inline int pwc_read_blue_gain(struct pwc_device *pdev, int *value)
871{
872 unsigned char buf;
873 int ret;
874
875 ret = RecvControlMsg(GET_STATUS_CTL, READ_BLUE_GAIN_FORMATTER, 1);
876 if (ret < 0)
877 return ret;
878 *value = buf << 8;
879 return 0;
880}
881
882
883static inline int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
884{
885 unsigned char buf;
886
887 /* useful range is 0x01..0x20 */
888 buf = speed / 0x7f0;
889 return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
890}
891
892static inline int pwc_get_wb_speed(struct pwc_device *pdev, int *value)
893{
894 unsigned char buf;
895 int ret;
896
897 ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
898 if (ret < 0)
899 return ret;
900 *value = buf * 0x7f0;
901 return 0;
902}
903
904
905static inline int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
906{
907 unsigned char buf;
908
909 /* useful range is 0x01..0x3F */
910 buf = (delay >> 10);
911 return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
912}
913
914static inline int pwc_get_wb_delay(struct pwc_device *pdev, int *value)
915{
916 unsigned char buf;
917 int ret;
918
919 ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
920 if (ret < 0)
921 return ret;
922 *value = buf << 10;
923 return 0;
924}
925
926
927int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
928{
929 unsigned char buf[2];
930
931 if (pdev->type < 730)
932 return 0;
933 on_value /= 100;
934 off_value /= 100;
935 if (on_value < 0)
936 on_value = 0;
937 if (on_value > 0xff)
938 on_value = 0xff;
939 if (off_value < 0)
940 off_value = 0;
941 if (off_value > 0xff)
942 off_value = 0xff;
943
944 buf[0] = on_value;
945 buf[1] = off_value;
946
947 return SendControlMsg(SET_STATUS_CTL, LED_FORMATTER, 2);
948}
949
950static int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
951{
952 unsigned char buf[2];
953 int ret;
954
955 if (pdev->type < 730) {
956 *on_value = -1;
957 *off_value = -1;
958 return 0;
959 }
960
961 ret = RecvControlMsg(GET_STATUS_CTL, LED_FORMATTER, 2);
962 if (ret < 0)
963 return ret;
964 *on_value = buf[0] * 100;
965 *off_value = buf[1] * 100;
966 return 0;
967}
968
969static inline int pwc_set_contour(struct pwc_device *pdev, int contour)
970{
971 unsigned char buf;
972 int ret;
973
974 if (contour < 0)
975 buf = 0xff; /* auto contour on */
976 else
977 buf = 0x0; /* auto contour off */
978 ret = SendControlMsg(SET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
979 if (ret < 0)
980 return ret;
981
982 if (contour < 0)
983 return 0;
984 if (contour > 0xffff)
985 contour = 0xffff;
986
987 buf = (contour >> 10); /* contour preset is [0..3f] */
988 ret = SendControlMsg(SET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
989 if (ret < 0)
990 return ret;
991 return 0;
992}
993
994static inline int pwc_get_contour(struct pwc_device *pdev, int *contour)
995{
996 unsigned char buf;
997 int ret;
998
999 ret = RecvControlMsg(GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
1000 if (ret < 0)
1001 return ret;
1002
1003 if (buf == 0) {
1004 /* auto mode off, query current preset value */
1005 ret = RecvControlMsg(GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
1006 if (ret < 0)
1007 return ret;
1008 *contour = buf << 10;
1009 }
1010 else
1011 *contour = -1;
1012 return 0;
1013}
1014
1015
1016static inline int pwc_set_backlight(struct pwc_device *pdev, int backlight)
1017{
1018 unsigned char buf;
1019
1020 if (backlight)
1021 buf = 0xff;
1022 else
1023 buf = 0x0;
1024 return SendControlMsg(SET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
1025}
1026
1027static inline int pwc_get_backlight(struct pwc_device *pdev, int *backlight)
1028{
1029 int ret;
1030 unsigned char buf;
1031
1032 ret = RecvControlMsg(GET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
1033 if (ret < 0)
1034 return ret;
1035 *backlight = buf;
1036 return 0;
1037}
1038
1039
1040static inline int pwc_set_flicker(struct pwc_device *pdev, int flicker)
1041{
1042 unsigned char buf;
1043
1044 if (flicker)
1045 buf = 0xff;
1046 else
1047 buf = 0x0;
1048 return SendControlMsg(SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1049}
1050
1051static inline int pwc_get_flicker(struct pwc_device *pdev, int *flicker)
1052{
1053 int ret;
1054 unsigned char buf;
1055
1056 ret = RecvControlMsg(GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1057 if (ret < 0)
1058 return ret;
1059 *flicker = buf;
1060 return 0;
1061}
1062
1063
1064static inline int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
1065{
1066 unsigned char buf;
1067
1068 if (noise < 0)
1069 noise = 0;
1070 if (noise > 3)
1071 noise = 3;
1072 buf = noise;
1073 return SendControlMsg(SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1074}
1075
1076static inline int pwc_get_dynamic_noise(struct pwc_device *pdev, int *noise)
1077{
1078 int ret;
1079 unsigned char buf;
1080
1081 ret = RecvControlMsg(GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1082 if (ret < 0)
1083 return ret;
1084 *noise = buf;
1085 return 0;
1086}
1087
1088static int pwc_mpt_reset(struct pwc_device *pdev, int flags)
1089{
1090 unsigned char buf;
1091
1092 buf = flags & 0x03; // only lower two bits are currently used
1093 return SendControlMsg(SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, 1);
1094}
1095
1096static inline int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
1097{
1098 unsigned char buf[4];
1099
1100 /* set new relative angle; angles are expressed in degrees * 100,
1101 but cam as .5 degree resolution, hence divide by 200. Also
1102 the angle must be multiplied by 64 before it's send to
1103 the cam (??)
1104 */
1105 pan = 64 * pan / 100;
1106 tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
1107 buf[0] = pan & 0xFF;
1108 buf[1] = (pan >> 8) & 0xFF;
1109 buf[2] = tilt & 0xFF;
1110 buf[3] = (tilt >> 8) & 0xFF;
1111 return SendControlMsg(SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, 4);
1112}
1113
1114static inline int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
1115{
1116 int ret;
1117 unsigned char buf[5];
1118
1119 ret = RecvControlMsg(GET_MPT_CTL, PT_STATUS_FORMATTER, 5);
1120 if (ret < 0)
1121 return ret;
1122 status->status = buf[0] & 0x7; // 3 bits are used for reporting
1123 status->time_pan = (buf[1] << 8) + buf[2];
1124 status->time_tilt = (buf[3] << 8) + buf[4];
1125 return 0;
1126}
1127
1128
1129int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
1130{
1131 unsigned char buf;
1132 int ret = -1, request;
1133
1134 if (pdev->type < 675)
1135 request = SENSOR_TYPE_FORMATTER1;
1136 else if (pdev->type < 730)
1137 return -1; /* The Vesta series doesn't have this call */
1138 else
1139 request = SENSOR_TYPE_FORMATTER2;
1140
1141 ret = RecvControlMsg(GET_STATUS_CTL, request, 1);
1142 if (ret < 0)
1143 return ret;
1144 if (pdev->type < 675)
1145 *sensor = buf | 0x100;
1146 else
1147 *sensor = buf;
1148 return 0;
1149}
1150
1151
1152 /* End of Add-Ons */
1153 /* ************************************************* */
1154
1155
1156int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1157{
1158 int ret = 0;
1159
1160 switch(cmd) {
1161 case VIDIOCPWCRUSER:
1162 {
1163 if (pwc_restore_user(pdev))
1164 ret = -EINVAL;
1165 break;
1166 }
1167
1168 case VIDIOCPWCSUSER:
1169 {
1170 if (pwc_save_user(pdev))
1171 ret = -EINVAL;
1172 break;
1173 }
1174
1175 case VIDIOCPWCFACTORY:
1176 {
1177 if (pwc_restore_factory(pdev))
1178 ret = -EINVAL;
1179 break;
1180 }
1181
1182 case VIDIOCPWCSCQUAL:
1183 {
1184 int *qual = arg;
1185
1186 if (*qual < 0 || *qual > 3)
1187 ret = -EINVAL;
1188 else
1189 ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, *qual, pdev->vsnapshot);
1190 if (ret >= 0)
1191 pdev->vcompression = *qual;
1192 break;
1193 }
1194
1195 case VIDIOCPWCGCQUAL:
1196 {
1197 int *qual = arg;
1198 *qual = pdev->vcompression;
1199 break;
1200 }
1201
1202 case VIDIOCPWCPROBE:
1203 {
1204 struct pwc_probe *probe = arg;
1205 strcpy(probe->name, pdev->vdev->name);
1206 probe->type = pdev->type;
1207 break;
1208 }
1209
1210 case VIDIOCPWCGSERIAL:
1211 {
1212 struct pwc_serial *serial = arg;
1213 strcpy(serial->serial, pdev->serial);
1214 break;
1215 }
1216
1217 case VIDIOCPWCSAGC:
1218 {
1219 int *agc = arg;
1220 if (pwc_set_agc(pdev, *agc < 0 ? 1 : 0, *agc))
1221 ret = -EINVAL;
1222 break;
1223 }
1224
1225 case VIDIOCPWCGAGC:
1226 {
1227 int *agc = arg;
1228
1229 if (pwc_get_agc(pdev, agc))
1230 ret = -EINVAL;
1231 break;
1232 }
1233
1234 case VIDIOCPWCSSHUTTER:
1235 {
1236 int *shutter_speed = arg;
1237 ret = pwc_set_shutter_speed(pdev, *shutter_speed < 0 ? 1 : 0, *shutter_speed);
1238 break;
1239 }
1240
1241 case VIDIOCPWCSAWB:
1242 {
1243 struct pwc_whitebalance *wb = arg;
1244
1245 ret = pwc_set_awb(pdev, wb->mode);
1246 if (ret >= 0 && wb->mode == PWC_WB_MANUAL) {
1247 pwc_set_red_gain(pdev, wb->manual_red);
1248 pwc_set_blue_gain(pdev, wb->manual_blue);
1249 }
1250 break;
1251 }
1252
1253 case VIDIOCPWCGAWB:
1254 {
1255 struct pwc_whitebalance *wb = arg;
1256
1257 memset(wb, 0, sizeof(struct pwc_whitebalance));
1258 wb->mode = pwc_get_awb(pdev);
1259 if (wb->mode < 0)
1260 ret = -EINVAL;
1261 else {
1262 if (wb->mode == PWC_WB_MANUAL) {
1263 ret = pwc_get_red_gain(pdev, &wb->manual_red);
1264 if (ret < 0)
1265 break;
1266 ret = pwc_get_blue_gain(pdev, &wb->manual_blue);
1267 if (ret < 0)
1268 break;
1269 }
1270 if (wb->mode == PWC_WB_AUTO) {
1271 ret = pwc_read_red_gain(pdev, &wb->read_red);
1272 if (ret < 0)
1273 break;
1274 ret = pwc_read_blue_gain(pdev, &wb->read_blue);
1275 if (ret < 0)
1276 break;
1277 }
1278 }
1279 break;
1280 }
1281
1282 case VIDIOCPWCSAWBSPEED:
1283 {
1284 struct pwc_wb_speed *wbs = arg;
1285
1286 if (wbs->control_speed > 0) {
1287 ret = pwc_set_wb_speed(pdev, wbs->control_speed);
1288 }
1289 if (wbs->control_delay > 0) {
1290 ret = pwc_set_wb_delay(pdev, wbs->control_delay);
1291 }
1292 break;
1293 }
1294
1295 case VIDIOCPWCGAWBSPEED:
1296 {
1297 struct pwc_wb_speed *wbs = arg;
1298
1299 ret = pwc_get_wb_speed(pdev, &wbs->control_speed);
1300 if (ret < 0)
1301 break;
1302 ret = pwc_get_wb_delay(pdev, &wbs->control_delay);
1303 if (ret < 0)
1304 break;
1305 break;
1306 }
1307
1308 case VIDIOCPWCSLED:
1309 {
1310 struct pwc_leds *leds = arg;
1311 ret = pwc_set_leds(pdev, leds->led_on, leds->led_off);
1312 break;
1313 }
1314
1315
1316 case VIDIOCPWCGLED:
1317 {
1318 struct pwc_leds *leds = arg;
1319 ret = pwc_get_leds(pdev, &leds->led_on, &leds->led_off);
1320 break;
1321 }
1322
1323 case VIDIOCPWCSCONTOUR:
1324 {
1325 int *contour = arg;
1326 ret = pwc_set_contour(pdev, *contour);
1327 break;
1328 }
1329
1330 case VIDIOCPWCGCONTOUR:
1331 {
1332 int *contour = arg;
1333 ret = pwc_get_contour(pdev, contour);
1334 break;
1335 }
1336
1337 case VIDIOCPWCSBACKLIGHT:
1338 {
1339 int *backlight = arg;
1340 ret = pwc_set_backlight(pdev, *backlight);
1341 break;
1342 }
1343
1344 case VIDIOCPWCGBACKLIGHT:
1345 {
1346 int *backlight = arg;
1347 ret = pwc_get_backlight(pdev, backlight);
1348 break;
1349 }
1350
1351 case VIDIOCPWCSFLICKER:
1352 {
1353 int *flicker = arg;
1354 ret = pwc_set_flicker(pdev, *flicker);
1355 break;
1356 }
1357
1358 case VIDIOCPWCGFLICKER:
1359 {
1360 int *flicker = arg;
1361 ret = pwc_get_flicker(pdev, flicker);
1362 break;
1363 }
1364
1365 case VIDIOCPWCSDYNNOISE:
1366 {
1367 int *dynnoise = arg;
1368 ret = pwc_set_dynamic_noise(pdev, *dynnoise);
1369 break;
1370 }
1371
1372 case VIDIOCPWCGDYNNOISE:
1373 {
1374 int *dynnoise = arg;
1375 ret = pwc_get_dynamic_noise(pdev, dynnoise);
1376 break;
1377 }
1378
1379 case VIDIOCPWCGREALSIZE:
1380 {
1381 struct pwc_imagesize *size = arg;
1382 size->width = pdev->image.x;
1383 size->height = pdev->image.y;
1384 break;
1385 }
1386
1387 case VIDIOCPWCMPTRESET:
1388 {
1389 if (pdev->features & FEATURE_MOTOR_PANTILT)
1390 {
1391 int *flags = arg;
1392
1393 ret = pwc_mpt_reset(pdev, *flags);
1394 if (ret >= 0)
1395 {
1396 pdev->pan_angle = 0;
1397 pdev->tilt_angle = 0;
1398 }
1399 }
1400 else
1401 {
1402 ret = -ENXIO;
1403 }
1404 break;
1405 }
1406
1407 case VIDIOCPWCMPTGRANGE:
1408 {
1409 if (pdev->features & FEATURE_MOTOR_PANTILT)
1410 {
1411 struct pwc_mpt_range *range = arg;
1412 *range = pdev->angle_range;
1413 }
1414 else
1415 {
1416 ret = -ENXIO;
1417 }
1418 break;
1419 }
1420
1421 case VIDIOCPWCMPTSANGLE:
1422 {
1423 int new_pan, new_tilt;
1424
1425 if (pdev->features & FEATURE_MOTOR_PANTILT)
1426 {
1427 struct pwc_mpt_angles *angles = arg;
1428 /* The camera can only set relative angles, so
1429 do some calculations when getting an absolute angle .
1430 */
1431 if (angles->absolute)
1432 {
1433 new_pan = angles->pan;
1434 new_tilt = angles->tilt;
1435 }
1436 else
1437 {
1438 new_pan = pdev->pan_angle + angles->pan;
1439 new_tilt = pdev->tilt_angle + angles->tilt;
1440 }
1441 /* check absolute ranges */
1442 if (new_pan < pdev->angle_range.pan_min ||
1443 new_pan > pdev->angle_range.pan_max ||
1444 new_tilt < pdev->angle_range.tilt_min ||
1445 new_tilt > pdev->angle_range.tilt_max)
1446 {
1447 ret = -ERANGE;
1448 }
1449 else
1450 {
1451 /* go to relative range, check again */
1452 new_pan -= pdev->pan_angle;
1453 new_tilt -= pdev->tilt_angle;
1454 /* angles are specified in degrees * 100, thus the limit = 36000 */
1455 if (new_pan < -36000 || new_pan > 36000 || new_tilt < -36000 || new_tilt > 36000)
1456 ret = -ERANGE;
1457 }
1458 if (ret == 0) /* no errors so far */
1459 {
1460 ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
1461 if (ret >= 0)
1462 {
1463 pdev->pan_angle += new_pan;
1464 pdev->tilt_angle += new_tilt;
1465 }
1466 if (ret == -EPIPE) /* stall -> out of range */
1467 ret = -ERANGE;
1468 }
1469 }
1470 else
1471 {
1472 ret = -ENXIO;
1473 }
1474 break;
1475 }
1476
1477 case VIDIOCPWCMPTGANGLE:
1478 {
1479
1480 if (pdev->features & FEATURE_MOTOR_PANTILT)
1481 {
1482 struct pwc_mpt_angles *angles = arg;
1483
1484 angles->absolute = 1;
1485 angles->pan = pdev->pan_angle;
1486 angles->tilt = pdev->tilt_angle;
1487 }
1488 else
1489 {
1490 ret = -ENXIO;
1491 }
1492 break;
1493 }
1494
1495 case VIDIOCPWCMPTSTATUS:
1496 {
1497 if (pdev->features & FEATURE_MOTOR_PANTILT)
1498 {
1499 struct pwc_mpt_status *status = arg;
1500 ret = pwc_mpt_get_status(pdev, status);
1501 }
1502 else
1503 {
1504 ret = -ENXIO;
1505 }
1506 break;
1507 }
1508
1509 case VIDIOCPWCGVIDCMD:
1510 {
1511 struct pwc_video_command *cmd = arg;
1512
1513 cmd->type = pdev->type;
1514 cmd->release = pdev->release;
1515 cmd->command_len = pdev->cmd_len;
1516 memcpy(&cmd->command_buf, pdev->cmd_buf, pdev->cmd_len);
1517 cmd->bandlength = pdev->vbandlength;
1518 cmd->frame_size = pdev->frame_size;
1519 break;
1520 }
1521 /*
1522 case VIDIOCPWCGVIDTABLE:
1523 {
1524 struct pwc_table_init_buffer *table = arg;
1525 table->len = pdev->cmd_len;
1526 memcpy(&table->buffer, pdev->decompress_data, pdev->decompressor->table_size);
1527 break;
1528 }
1529 */
1530
1531 default:
1532 ret = -ENOIOCTLCMD;
1533 break;
1534 }
1535
1536 if (ret > 0)
1537 return 0;
1538 return ret;
1539}
1540
1541
1542
diff --git a/drivers/usb/media/pwc/pwc-if.c b/drivers/usb/media/pwc/pwc-if.c
deleted file mode 100644
index 4f9b0dc6fd7b..000000000000
--- a/drivers/usb/media/pwc/pwc-if.c
+++ /dev/null
@@ -1,2210 +0,0 @@
1/* Linux driver for Philips webcam
2 USB and Video4Linux interface part.
3 (C) 1999-2004 Nemosoft Unv.
4 (C) 2004 Luc Saillard (luc@saillard.org)
5
6 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
7 driver and thus may have bugs that are not present in the original version.
8 Please send bug reports and support requests to <luc@saillard.org>.
9 The decompression routines have been implemented by reverse-engineering the
10 Nemosoft binary pwcx module. Caveat emptor.
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 This code forms the interface between the USB layers and the Philips
30 specific stuff. Some adanved stuff of the driver falls under an
31 NDA, signed between me and Philips B.V., Eindhoven, the Netherlands, and
32 is thus not distributed in source form. The binary pwcx.o module
33 contains the code that falls under the NDA.
34
35 In case you're wondering: 'pwc' stands for "Philips WebCam", but
36 I really didn't want to type 'philips_web_cam' every time (I'm lazy as
37 any Linux kernel hacker, but I don't like uncomprehensible abbreviations
38 without explanation).
39
40 Oh yes, convention: to disctinguish between all the various pointers to
41 device-structures, I use these names for the pointer variables:
42 udev: struct usb_device *
43 vdev: struct video_device *
44 pdev: struct pwc_devive *
45*/
46
47/* Contributors:
48 - Alvarado: adding whitebalance code
49 - Alistar Moire: QuickCam 3000 Pro device/product ID
50 - Tony Hoyle: Creative Labs Webcam 5 device/product ID
51 - Mark Burazin: solving hang in VIDIOCSYNC when camera gets unplugged
52 - Jk Fang: Sotec Afina Eye ID
53 - Xavier Roche: QuickCam Pro 4000 ID
54 - Jens Knudsen: QuickCam Zoom ID
55 - J. Debert: QuickCam for Notebooks ID
56*/
57
58#include <linux/errno.h>
59#include <linux/init.h>
60#include <linux/mm.h>
61#include <linux/module.h>
62#include <linux/poll.h>
63#include <linux/slab.h>
64#include <linux/vmalloc.h>
65#include <linux/version.h>
66#include <asm/io.h>
67
68#include "pwc.h"
69#include "pwc-ioctl.h"
70#include "pwc-kiara.h"
71#include "pwc-timon.h"
72#include "pwc-uncompress.h"
73
74/* Function prototypes and driver templates */
75
76/* hotplug device table support */
77static struct usb_device_id pwc_device_table [] = {
78 { USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
79 { USB_DEVICE(0x0471, 0x0303) },
80 { USB_DEVICE(0x0471, 0x0304) },
81 { USB_DEVICE(0x0471, 0x0307) },
82 { USB_DEVICE(0x0471, 0x0308) },
83 { USB_DEVICE(0x0471, 0x030C) },
84 { USB_DEVICE(0x0471, 0x0310) },
85 { USB_DEVICE(0x0471, 0x0311) },
86 { USB_DEVICE(0x0471, 0x0312) },
87 { USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
88 { USB_DEVICE(0x069A, 0x0001) }, /* Askey */
89 { USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
90 { USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
91 { USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
92 { USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom (old model) */
93 { USB_DEVICE(0x046D, 0x08B4) }, /* Logitech QuickCam Zoom (new model) */
94 { USB_DEVICE(0x046D, 0x08B5) }, /* Logitech QuickCam Orbit/Sphere */
95 { USB_DEVICE(0x046D, 0x08B6) }, /* Logitech (reserved) */
96 { USB_DEVICE(0x046D, 0x08B7) }, /* Logitech (reserved) */
97 { USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
98 { USB_DEVICE(0x055D, 0x9000) }, /* Samsung */
99 { USB_DEVICE(0x055D, 0x9001) },
100 { USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
101 { USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
102 { USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
103 { USB_DEVICE(0x06BE, 0x8116) }, /* new Afina Eye */
104 { USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
105 { USB_DEVICE(0x0d81, 0x1900) },
106 { }
107};
108MODULE_DEVICE_TABLE(usb, pwc_device_table);
109
110static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id);
111static void usb_pwc_disconnect(struct usb_interface *intf);
112
113static struct usb_driver pwc_driver = {
114 .name = "Philips webcam", /* name */
115 .id_table = pwc_device_table,
116 .probe = usb_pwc_probe, /* probe() */
117 .disconnect = usb_pwc_disconnect, /* disconnect() */
118};
119
120#define MAX_DEV_HINTS 20
121#define MAX_ISOC_ERRORS 20
122
123static int default_size = PSZ_QCIF;
124static int default_fps = 10;
125static int default_fbufs = 3; /* Default number of frame buffers */
126static int default_mbufs = 2; /* Default number of mmap() buffers */
127 int pwc_trace = TRACE_MODULE | TRACE_FLOW | TRACE_PWCX;
128static int power_save = 0;
129static int led_on = 100, led_off = 0; /* defaults to LED that is on while in use */
130static int pwc_preferred_compression = 2; /* 0..3 = uncompressed..high */
131static struct {
132 int type;
133 char serial_number[30];
134 int device_node;
135 struct pwc_device *pdev;
136} device_hint[MAX_DEV_HINTS];
137
138/***/
139
140static int pwc_video_open(struct inode *inode, struct file *file);
141static int pwc_video_close(struct inode *inode, struct file *file);
142static ssize_t pwc_video_read(struct file *file, char __user * buf,
143 size_t count, loff_t *ppos);
144static unsigned int pwc_video_poll(struct file *file, poll_table *wait);
145static int pwc_video_ioctl(struct inode *inode, struct file *file,
146 unsigned int ioctlnr, unsigned long arg);
147static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma);
148
149static struct file_operations pwc_fops = {
150 .owner = THIS_MODULE,
151 .open = pwc_video_open,
152 .release = pwc_video_close,
153 .read = pwc_video_read,
154 .poll = pwc_video_poll,
155 .mmap = pwc_video_mmap,
156 .ioctl = pwc_video_ioctl,
157 .compat_ioctl = v4l_compat_ioctl32,
158 .llseek = no_llseek,
159};
160static struct video_device pwc_template = {
161 .owner = THIS_MODULE,
162 .name = "Philips Webcam", /* Filled in later */
163 .type = VID_TYPE_CAPTURE,
164 .hardware = VID_HARDWARE_PWC,
165 .release = video_device_release,
166 .fops = &pwc_fops,
167 .minor = -1,
168};
169
170/***************************************************************************/
171
172/* Okay, this is some magic that I worked out and the reasoning behind it...
173
174 The biggest problem with any USB device is of course: "what to do
175 when the user unplugs the device while it is in use by an application?"
176 We have several options:
177 1) Curse them with the 7 plagues when they do (requires divine intervention)
178 2) Tell them not to (won't work: they'll do it anyway)
179 3) Oops the kernel (this will have a negative effect on a user's uptime)
180 4) Do something sensible.
181
182 Of course, we go for option 4.
183
184 It happens that this device will be linked to two times, once from
185 usb_device and once from the video_device in their respective 'private'
186 pointers. This is done when the device is probed() and all initialization
187 succeeded. The pwc_device struct links back to both structures.
188
189 When a device is unplugged while in use it will be removed from the
190 list of known USB devices; I also de-register it as a V4L device, but
191 unfortunately I can't free the memory since the struct is still in use
192 by the file descriptor. This free-ing is then deferend until the first
193 opportunity. Crude, but it works.
194
195 A small 'advantage' is that if a user unplugs the cam and plugs it back
196 in, it should get assigned the same video device minor, but unfortunately
197 it's non-trivial to re-link the cam back to the video device... (that
198 would surely be magic! :))
199*/
200
201/***************************************************************************/
202/* Private functions */
203
204/* Here we want the physical address of the memory.
205 * This is used when initializing the contents of the area.
206 */
207static inline unsigned long kvirt_to_pa(unsigned long adr)
208{
209 unsigned long kva, ret;
210
211 kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
212 kva |= adr & (PAGE_SIZE-1); /* restore the offset */
213 ret = __pa(kva);
214 return ret;
215}
216
217static void * rvmalloc(unsigned long size)
218{
219 void * mem;
220 unsigned long adr;
221
222 size=PAGE_ALIGN(size);
223 mem=vmalloc_32(size);
224 if (mem)
225 {
226 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
227 adr=(unsigned long) mem;
228 while (size > 0)
229 {
230 SetPageReserved(vmalloc_to_page((void *)adr));
231 adr+=PAGE_SIZE;
232 size-=PAGE_SIZE;
233 }
234 }
235 return mem;
236}
237
238static void rvfree(void * mem, unsigned long size)
239{
240 unsigned long adr;
241
242 if (mem)
243 {
244 adr=(unsigned long) mem;
245 while ((long) size > 0)
246 {
247 ClearPageReserved(vmalloc_to_page((void *)adr));
248 adr+=PAGE_SIZE;
249 size-=PAGE_SIZE;
250 }
251 vfree(mem);
252 }
253}
254
255
256
257
258static int pwc_allocate_buffers(struct pwc_device *pdev)
259{
260 int i;
261 void *kbuf;
262
263 Trace(TRACE_MEMORY, ">> pwc_allocate_buffers(pdev = 0x%p)\n", pdev);
264
265 if (pdev == NULL)
266 return -ENXIO;
267
268#ifdef PWC_MAGIC
269 if (pdev->magic != PWC_MAGIC) {
270 Err("allocate_buffers(): magic failed.\n");
271 return -ENXIO;
272 }
273#endif
274 /* Allocate Isochronous pipe buffers */
275 for (i = 0; i < MAX_ISO_BUFS; i++) {
276 if (pdev->sbuf[i].data == NULL) {
277 kbuf = kmalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
278 if (kbuf == NULL) {
279 Err("Failed to allocate iso buffer %d.\n", i);
280 return -ENOMEM;
281 }
282 Trace(TRACE_MEMORY, "Allocated iso buffer at %p.\n", kbuf);
283 pdev->sbuf[i].data = kbuf;
284 memset(kbuf, 0, ISO_BUFFER_SIZE);
285 }
286 }
287
288 /* Allocate frame buffer structure */
289 if (pdev->fbuf == NULL) {
290 kbuf = kmalloc(default_fbufs * sizeof(struct pwc_frame_buf), GFP_KERNEL);
291 if (kbuf == NULL) {
292 Err("Failed to allocate frame buffer structure.\n");
293 return -ENOMEM;
294 }
295 Trace(TRACE_MEMORY, "Allocated frame buffer structure at %p.\n", kbuf);
296 pdev->fbuf = kbuf;
297 memset(kbuf, 0, default_fbufs * sizeof(struct pwc_frame_buf));
298 }
299 /* create frame buffers, and make circular ring */
300 for (i = 0; i < default_fbufs; i++) {
301 if (pdev->fbuf[i].data == NULL) {
302 kbuf = vmalloc(PWC_FRAME_SIZE); /* need vmalloc since frame buffer > 128K */
303 if (kbuf == NULL) {
304 Err("Failed to allocate frame buffer %d.\n", i);
305 return -ENOMEM;
306 }
307 Trace(TRACE_MEMORY, "Allocated frame buffer %d at %p.\n", i, kbuf);
308 pdev->fbuf[i].data = kbuf;
309 memset(kbuf, 128, PWC_FRAME_SIZE);
310 }
311 }
312
313 /* Allocate decompressor table space */
314 kbuf = NULL;
315 switch (pdev->type)
316 {
317 case 675:
318 case 680:
319 case 690:
320 case 720:
321 case 730:
322 case 740:
323 case 750:
324#if 0
325 Trace(TRACE_MEMORY,"private_data(%zu)\n",sizeof(struct pwc_dec23_private));
326 kbuf = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL); /* Timon & Kiara */
327 break;
328 case 645:
329 case 646:
330 /* TODO & FIXME */
331 kbuf = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL);
332 break;
333#endif
334 ;
335 }
336 pdev->decompress_data = kbuf;
337
338 /* Allocate image buffer; double buffer for mmap() */
339 kbuf = rvmalloc(default_mbufs * pdev->len_per_image);
340 if (kbuf == NULL) {
341 Err("Failed to allocate image buffer(s). needed (%d)\n",default_mbufs * pdev->len_per_image);
342 return -ENOMEM;
343 }
344 Trace(TRACE_MEMORY, "Allocated image buffer at %p.\n", kbuf);
345 pdev->image_data = kbuf;
346 for (i = 0; i < default_mbufs; i++)
347 pdev->image_ptr[i] = kbuf + i * pdev->len_per_image;
348 for (; i < MAX_IMAGES; i++)
349 pdev->image_ptr[i] = NULL;
350
351 kbuf = NULL;
352
353 Trace(TRACE_MEMORY, "<< pwc_allocate_buffers()\n");
354 return 0;
355}
356
357static void pwc_free_buffers(struct pwc_device *pdev)
358{
359 int i;
360
361 Trace(TRACE_MEMORY, "Entering free_buffers(%p).\n", pdev);
362
363 if (pdev == NULL)
364 return;
365#ifdef PWC_MAGIC
366 if (pdev->magic != PWC_MAGIC) {
367 Err("free_buffers(): magic failed.\n");
368 return;
369 }
370#endif
371
372 /* Release Iso-pipe buffers */
373 for (i = 0; i < MAX_ISO_BUFS; i++)
374 if (pdev->sbuf[i].data != NULL) {
375 Trace(TRACE_MEMORY, "Freeing ISO buffer at %p.\n", pdev->sbuf[i].data);
376 kfree(pdev->sbuf[i].data);
377 pdev->sbuf[i].data = NULL;
378 }
379
380 /* The same for frame buffers */
381 if (pdev->fbuf != NULL) {
382 for (i = 0; i < default_fbufs; i++) {
383 if (pdev->fbuf[i].data != NULL) {
384 Trace(TRACE_MEMORY, "Freeing frame buffer %d at %p.\n", i, pdev->fbuf[i].data);
385 vfree(pdev->fbuf[i].data);
386 pdev->fbuf[i].data = NULL;
387 }
388 }
389 kfree(pdev->fbuf);
390 pdev->fbuf = NULL;
391 }
392
393 /* Intermediate decompression buffer & tables */
394 if (pdev->decompress_data != NULL) {
395 Trace(TRACE_MEMORY, "Freeing decompression buffer at %p.\n", pdev->decompress_data);
396 kfree(pdev->decompress_data);
397 pdev->decompress_data = NULL;
398 }
399 pdev->decompressor = NULL;
400
401 /* Release image buffers */
402 if (pdev->image_data != NULL) {
403 Trace(TRACE_MEMORY, "Freeing image buffer at %p.\n", pdev->image_data);
404 rvfree(pdev->image_data, default_mbufs * pdev->len_per_image);
405 }
406 pdev->image_data = NULL;
407
408 Trace(TRACE_MEMORY, "Leaving free_buffers().\n");
409}
410
411/* The frame & image buffer mess.
412
413 Yes, this is a mess. Well, it used to be simple, but alas... In this
414 module, 3 buffers schemes are used to get the data from the USB bus to
415 the user program. The first scheme involves the ISO buffers (called thus
416 since they transport ISO data from the USB controller), and not really
417 interesting. Suffices to say the data from this buffer is quickly
418 gathered in an interrupt handler (pwc_isoc_handler) and placed into the
419 frame buffer.
420
421 The frame buffer is the second scheme, and is the central element here.
422 It collects the data from a single frame from the camera (hence, the
423 name). Frames are delimited by the USB camera with a short USB packet,
424 so that's easy to detect. The frame buffers form a list that is filled
425 by the camera+USB controller and drained by the user process through
426 either read() or mmap().
427
428 The image buffer is the third scheme, in which frames are decompressed
429 and converted into planar format. For mmap() there is more than
430 one image buffer available.
431
432 The frame buffers provide the image buffering. In case the user process
433 is a bit slow, this introduces lag and some undesired side-effects.
434 The problem arises when the frame buffer is full. I used to drop the last
435 frame, which makes the data in the queue stale very quickly. But dropping
436 the frame at the head of the queue proved to be a litte bit more difficult.
437 I tried a circular linked scheme, but this introduced more problems than
438 it solved.
439
440 Because filling and draining are completely asynchronous processes, this
441 requires some fiddling with pointers and mutexes.
442
443 Eventually, I came up with a system with 2 lists: an 'empty' frame list
444 and a 'full' frame list:
445 * Initially, all frame buffers but one are on the 'empty' list; the one
446 remaining buffer is our initial fill frame.
447 * If a frame is needed for filling, we try to take it from the 'empty'
448 list, unless that list is empty, in which case we take the buffer at
449 the head of the 'full' list.
450 * When our fill buffer has been filled, it is appended to the 'full'
451 list.
452 * If a frame is needed by read() or mmap(), it is taken from the head of
453 the 'full' list, handled, and then appended to the 'empty' list. If no
454 buffer is present on the 'full' list, we wait.
455 The advantage is that the buffer that is currently being decompressed/
456 converted, is on neither list, and thus not in our way (any other scheme
457 I tried had the problem of old data lingering in the queue).
458
459 Whatever strategy you choose, it always remains a tradeoff: with more
460 frame buffers the chances of a missed frame are reduced. On the other
461 hand, on slower machines it introduces lag because the queue will
462 always be full.
463 */
464
465/**
466 \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
467 */
468static inline int pwc_next_fill_frame(struct pwc_device *pdev)
469{
470 int ret;
471 unsigned long flags;
472
473 ret = 0;
474 spin_lock_irqsave(&pdev->ptrlock, flags);
475 if (pdev->fill_frame != NULL) {
476 /* append to 'full' list */
477 if (pdev->full_frames == NULL) {
478 pdev->full_frames = pdev->fill_frame;
479 pdev->full_frames_tail = pdev->full_frames;
480 }
481 else {
482 pdev->full_frames_tail->next = pdev->fill_frame;
483 pdev->full_frames_tail = pdev->fill_frame;
484 }
485 }
486 if (pdev->empty_frames != NULL) {
487 /* We have empty frames available. That's easy */
488 pdev->fill_frame = pdev->empty_frames;
489 pdev->empty_frames = pdev->empty_frames->next;
490 }
491 else {
492 /* Hmm. Take it from the full list */
493#if PWC_DEBUG
494 /* sanity check */
495 if (pdev->full_frames == NULL) {
496 Err("Neither empty or full frames available!\n");
497 spin_unlock_irqrestore(&pdev->ptrlock, flags);
498 return -EINVAL;
499 }
500#endif
501 pdev->fill_frame = pdev->full_frames;
502 pdev->full_frames = pdev->full_frames->next;
503 ret = 1;
504 }
505 pdev->fill_frame->next = NULL;
506#if PWC_DEBUG
507 Trace(TRACE_SEQUENCE, "Assigning sequence number %d.\n", pdev->sequence);
508 pdev->fill_frame->sequence = pdev->sequence++;
509#endif
510 spin_unlock_irqrestore(&pdev->ptrlock, flags);
511 return ret;
512}
513
514
515/**
516 \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.
517
518 If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
519 */
520static void pwc_reset_buffers(struct pwc_device *pdev)
521{
522 int i;
523 unsigned long flags;
524
525 spin_lock_irqsave(&pdev->ptrlock, flags);
526 pdev->full_frames = NULL;
527 pdev->full_frames_tail = NULL;
528 for (i = 0; i < default_fbufs; i++) {
529 pdev->fbuf[i].filled = 0;
530 if (i > 0)
531 pdev->fbuf[i].next = &pdev->fbuf[i - 1];
532 else
533 pdev->fbuf->next = NULL;
534 }
535 pdev->empty_frames = &pdev->fbuf[default_fbufs - 1];
536 pdev->empty_frames_tail = pdev->fbuf;
537 pdev->read_frame = NULL;
538 pdev->fill_frame = pdev->empty_frames;
539 pdev->empty_frames = pdev->empty_frames->next;
540
541 pdev->image_read_pos = 0;
542 pdev->fill_image = 0;
543 spin_unlock_irqrestore(&pdev->ptrlock, flags);
544}
545
546
547/**
548 \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
549 */
550static int pwc_handle_frame(struct pwc_device *pdev)
551{
552 int ret = 0;
553 unsigned long flags;
554
555 spin_lock_irqsave(&pdev->ptrlock, flags);
556 /* First grab our read_frame; this is removed from all lists, so
557 we can release the lock after this without problems */
558 if (pdev->read_frame != NULL) {
559 /* This can't theoretically happen */
560 Err("Huh? Read frame still in use?\n");
561 }
562 else {
563 if (pdev->full_frames == NULL) {
564 Err("Woops. No frames ready.\n");
565 }
566 else {
567 pdev->read_frame = pdev->full_frames;
568 pdev->full_frames = pdev->full_frames->next;
569 pdev->read_frame->next = NULL;
570 }
571
572 if (pdev->read_frame != NULL) {
573#if PWC_DEBUG
574 Trace(TRACE_SEQUENCE, "Decompressing frame %d\n", pdev->read_frame->sequence);
575#endif
576 /* Decompression is a lenghty process, so it's outside of the lock.
577 This gives the isoc_handler the opportunity to fill more frames
578 in the mean time.
579 */
580 spin_unlock_irqrestore(&pdev->ptrlock, flags);
581 ret = pwc_decompress(pdev);
582 spin_lock_irqsave(&pdev->ptrlock, flags);
583
584 /* We're done with read_buffer, tack it to the end of the empty buffer list */
585 if (pdev->empty_frames == NULL) {
586 pdev->empty_frames = pdev->read_frame;
587 pdev->empty_frames_tail = pdev->empty_frames;
588 }
589 else {
590 pdev->empty_frames_tail->next = pdev->read_frame;
591 pdev->empty_frames_tail = pdev->read_frame;
592 }
593 pdev->read_frame = NULL;
594 }
595 }
596 spin_unlock_irqrestore(&pdev->ptrlock, flags);
597 return ret;
598}
599
600/**
601 \brief Advance pointers of image buffer (after each user request)
602*/
603static inline void pwc_next_image(struct pwc_device *pdev)
604{
605 pdev->image_used[pdev->fill_image] = 0;
606 pdev->fill_image = (pdev->fill_image + 1) % default_mbufs;
607}
608
609
610/* This gets called for the Isochronous pipe (video). This is done in
611 * interrupt time, so it has to be fast, not crash, and not stall. Neat.
612 */
613static void pwc_isoc_handler(struct urb *urb, struct pt_regs *regs)
614{
615 struct pwc_device *pdev;
616 int i, fst, flen;
617 int awake;
618 struct pwc_frame_buf *fbuf;
619 unsigned char *fillptr = NULL, *iso_buf = NULL;
620
621 awake = 0;
622 pdev = (struct pwc_device *)urb->context;
623 if (pdev == NULL) {
624 Err("isoc_handler() called with NULL device?!\n");
625 return;
626 }
627#ifdef PWC_MAGIC
628 if (pdev->magic != PWC_MAGIC) {
629 Err("isoc_handler() called with bad magic!\n");
630 return;
631 }
632#endif
633 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
634 Trace(TRACE_OPEN, "pwc_isoc_handler(): URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
635 return;
636 }
637 if (urb->status != -EINPROGRESS && urb->status != 0) {
638 const char *errmsg;
639
640 errmsg = "Unknown";
641 switch(urb->status) {
642 case -ENOSR: errmsg = "Buffer error (overrun)"; break;
643 case -EPIPE: errmsg = "Stalled (device not responding)"; break;
644 case -EOVERFLOW: errmsg = "Babble (bad cable?)"; break;
645 case -EPROTO: errmsg = "Bit-stuff error (bad cable?)"; break;
646 case -EILSEQ: errmsg = "CRC/Timeout (could be anything)"; break;
647 case -ETIMEDOUT: errmsg = "NAK (device does not respond)"; break;
648 }
649 Trace(TRACE_FLOW, "pwc_isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
650 /* Give up after a number of contiguous errors on the USB bus.
651 Appearantly something is wrong so we simulate an unplug event.
652 */
653 if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
654 {
655 Info("Too many ISOC errors, bailing out.\n");
656 pdev->error_status = EIO;
657 awake = 1;
658 wake_up_interruptible(&pdev->frameq);
659 }
660 goto handler_end; // ugly, but practical
661 }
662
663 fbuf = pdev->fill_frame;
664 if (fbuf == NULL) {
665 Err("pwc_isoc_handler without valid fill frame.\n");
666 awake = 1;
667 goto handler_end;
668 }
669 else {
670 fillptr = fbuf->data + fbuf->filled;
671 }
672
673 /* Reset ISOC error counter. We did get here, after all. */
674 pdev->visoc_errors = 0;
675
676 /* vsync: 0 = don't copy data
677 1 = sync-hunt
678 2 = synched
679 */
680 /* Compact data */
681 for (i = 0; i < urb->number_of_packets; i++) {
682 fst = urb->iso_frame_desc[i].status;
683 flen = urb->iso_frame_desc[i].actual_length;
684 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
685 if (fst == 0) {
686 if (flen > 0) { /* if valid data... */
687 if (pdev->vsync > 0) { /* ...and we are not sync-hunting... */
688 pdev->vsync = 2;
689
690 /* ...copy data to frame buffer, if possible */
691 if (flen + fbuf->filled > pdev->frame_total_size) {
692 Trace(TRACE_FLOW, "Frame buffer overflow (flen = %d, frame_total_size = %d).\n", flen, pdev->frame_total_size);
693 pdev->vsync = 0; /* Hmm, let's wait for an EOF (end-of-frame) */
694 pdev->vframes_error++;
695 }
696 else {
697 memmove(fillptr, iso_buf, flen);
698 fillptr += flen;
699 }
700 }
701 fbuf->filled += flen;
702 } /* ..flen > 0 */
703
704 if (flen < pdev->vlast_packet_size) {
705 /* Shorter packet... We probably have the end of an image-frame;
706 wake up read() process and let select()/poll() do something.
707 Decompression is done in user time over there.
708 */
709 if (pdev->vsync == 2) {
710 /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
711 frames on the USB wire after an exposure change. This conditition is
712 however detected in the cam and a bit is set in the header.
713 */
714 if (pdev->type == 730) {
715 unsigned char *ptr = (unsigned char *)fbuf->data;
716
717 if (ptr[1] == 1 && ptr[0] & 0x10) {
718#if PWC_DEBUG
719 Debug("Hyundai CMOS sensor bug. Dropping frame %d.\n", fbuf->sequence);
720#endif
721 pdev->drop_frames += 2;
722 pdev->vframes_error++;
723 }
724 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
725 if (ptr[0] & 0x01)
726 Info("Snapshot button pressed.\n");
727 else
728 Info("Snapshot button released.\n");
729 }
730 if ((ptr[0] ^ pdev->vmirror) & 0x02) {
731 if (ptr[0] & 0x02)
732 Info("Image is mirrored.\n");
733 else
734 Info("Image is normal.\n");
735 }
736 pdev->vmirror = ptr[0] & 0x03;
737 /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
738 after a short frame; this condition is filtered out specifically. A 4 byte
739 frame doesn't make sense anyway.
740 So we get either this sequence:
741 drop_bit set -> 4 byte frame -> short frame -> good frame
742 Or this one:
743 drop_bit set -> short frame -> good frame
744 So we drop either 3 or 2 frames in all!
745 */
746 if (fbuf->filled == 4)
747 pdev->drop_frames++;
748 }
749
750 /* In case we were instructed to drop the frame, do so silently.
751 The buffer pointers are not updated either (but the counters are reset below).
752 */
753 if (pdev->drop_frames > 0)
754 pdev->drop_frames--;
755 else {
756 /* Check for underflow first */
757 if (fbuf->filled < pdev->frame_total_size) {
758 Trace(TRACE_FLOW, "Frame buffer underflow (%d bytes); discarded.\n", fbuf->filled);
759 pdev->vframes_error++;
760 }
761 else {
762 /* Send only once per EOF */
763 awake = 1; /* delay wake_ups */
764
765 /* Find our next frame to fill. This will always succeed, since we
766 * nick a frame from either empty or full list, but if we had to
767 * take it from the full list, it means a frame got dropped.
768 */
769 if (pwc_next_fill_frame(pdev)) {
770 pdev->vframes_dumped++;
771 if ((pdev->vframe_count > FRAME_LOWMARK) && (pwc_trace & TRACE_FLOW)) {
772 if (pdev->vframes_dumped < 20)
773 Trace(TRACE_FLOW, "Dumping frame %d.\n", pdev->vframe_count);
774 if (pdev->vframes_dumped == 20)
775 Trace(TRACE_FLOW, "Dumping frame %d (last message).\n", pdev->vframe_count);
776 }
777 }
778 fbuf = pdev->fill_frame;
779 }
780 } /* !drop_frames */
781 pdev->vframe_count++;
782 }
783 fbuf->filled = 0;
784 fillptr = fbuf->data;
785 pdev->vsync = 1;
786 } /* .. flen < last_packet_size */
787 pdev->vlast_packet_size = flen;
788 } /* ..status == 0 */
789#if PWC_DEBUG
790 /* This is normally not interesting to the user, unless you are really debugging something */
791 else {
792 static int iso_error = 0;
793 iso_error++;
794 if (iso_error < 20)
795 Trace(TRACE_FLOW, "Iso frame %d of USB has error %d\n", i, fst);
796 }
797#endif
798 }
799
800handler_end:
801 if (awake)
802 wake_up_interruptible(&pdev->frameq);
803
804 urb->dev = pdev->udev;
805 i = usb_submit_urb(urb, GFP_ATOMIC);
806 if (i != 0)
807 Err("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
808}
809
810
811static int pwc_isoc_init(struct pwc_device *pdev)
812{
813 struct usb_device *udev;
814 struct urb *urb;
815 int i, j, ret;
816
817 struct usb_interface *intf;
818 struct usb_host_interface *idesc = NULL;
819
820 if (pdev == NULL)
821 return -EFAULT;
822 if (pdev->iso_init)
823 return 0;
824 pdev->vsync = 0;
825 udev = pdev->udev;
826
827 /* Get the current alternate interface, adjust packet size */
828 if (!udev->actconfig)
829 return -EFAULT;
830#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5)
831 idesc = &udev->actconfig->interface[0]->altsetting[pdev->valternate];
832#else
833 intf = usb_ifnum_to_if(udev, 0);
834 if (intf)
835 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
836#endif
837
838 if (!idesc)
839 return -EFAULT;
840
841 /* Search video endpoint */
842 pdev->vmax_packet_size = -1;
843 for (i = 0; i < idesc->desc.bNumEndpoints; i++)
844 if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
845 pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
846 break;
847 }
848
849 if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
850 Err("Failed to find packet size for video endpoint in current alternate setting.\n");
851 return -ENFILE; /* Odd error, that should be noticeable */
852 }
853
854 /* Set alternate interface */
855 ret = 0;
856 Trace(TRACE_OPEN, "Setting alternate interface %d\n", pdev->valternate);
857 ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
858 if (ret < 0)
859 return ret;
860
861 for (i = 0; i < MAX_ISO_BUFS; i++) {
862 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
863 if (urb == NULL) {
864 Err("Failed to allocate urb %d\n", i);
865 ret = -ENOMEM;
866 break;
867 }
868 pdev->sbuf[i].urb = urb;
869 Trace(TRACE_MEMORY, "Allocated URB at 0x%p\n", urb);
870 }
871 if (ret) {
872 /* De-allocate in reverse order */
873 while (i >= 0) {
874 if (pdev->sbuf[i].urb != NULL)
875 usb_free_urb(pdev->sbuf[i].urb);
876 pdev->sbuf[i].urb = NULL;
877 i--;
878 }
879 return ret;
880 }
881
882 /* init URB structure */
883 for (i = 0; i < MAX_ISO_BUFS; i++) {
884 urb = pdev->sbuf[i].urb;
885
886 urb->interval = 1; // devik
887 urb->dev = udev;
888 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
889 urb->transfer_flags = URB_ISO_ASAP;
890 urb->transfer_buffer = pdev->sbuf[i].data;
891 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
892 urb->complete = pwc_isoc_handler;
893 urb->context = pdev;
894 urb->start_frame = 0;
895 urb->number_of_packets = ISO_FRAMES_PER_DESC;
896 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
897 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
898 urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
899 }
900 }
901
902 /* link */
903 for (i = 0; i < MAX_ISO_BUFS; i++) {
904 ret = usb_submit_urb(pdev->sbuf[i].urb, GFP_KERNEL);
905 if (ret)
906 Err("isoc_init() submit_urb %d failed with error %d\n", i, ret);
907 else
908 Trace(TRACE_MEMORY, "URB 0x%p submitted.\n", pdev->sbuf[i].urb);
909 }
910
911 /* All is done... */
912 pdev->iso_init = 1;
913 Trace(TRACE_OPEN, "<< pwc_isoc_init()\n");
914 return 0;
915}
916
917static void pwc_isoc_cleanup(struct pwc_device *pdev)
918{
919 int i;
920
921 Trace(TRACE_OPEN, ">> pwc_isoc_cleanup()\n");
922 if (pdev == NULL)
923 return;
924
925 /* Unlinking ISOC buffers one by one */
926 for (i = 0; i < MAX_ISO_BUFS; i++) {
927 struct urb *urb;
928
929 urb = pdev->sbuf[i].urb;
930 if (urb != 0) {
931 if (pdev->iso_init) {
932 Trace(TRACE_MEMORY, "Unlinking URB %p\n", urb);
933 usb_kill_urb(urb);
934 }
935 Trace(TRACE_MEMORY, "Freeing URB\n");
936 usb_free_urb(urb);
937 pdev->sbuf[i].urb = NULL;
938 }
939 }
940
941 /* Stop camera, but only if we are sure the camera is still there (unplug
942 is signalled by EPIPE)
943 */
944 if (pdev->error_status && pdev->error_status != EPIPE) {
945 Trace(TRACE_OPEN, "Setting alternate interface 0.\n");
946 usb_set_interface(pdev->udev, 0, 0);
947 }
948
949 pdev->iso_init = 0;
950 Trace(TRACE_OPEN, "<< pwc_isoc_cleanup()\n");
951}
952
953int pwc_try_video_mode(struct pwc_device *pdev, int width, int height, int new_fps, int new_compression, int new_snapshot)
954{
955 int ret, start;
956
957 /* Stop isoc stuff */
958 pwc_isoc_cleanup(pdev);
959 /* Reset parameters */
960 pwc_reset_buffers(pdev);
961 /* Try to set video mode... */
962 start = ret = pwc_set_video_mode(pdev, width, height, new_fps, new_compression, new_snapshot);
963 if (ret) {
964 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 1 failed.\n");
965 /* That failed... restore old mode (we know that worked) */
966 start = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
967 if (start) {
968 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 2 failed.\n");
969 }
970 }
971 if (start == 0)
972 {
973 if (pwc_isoc_init(pdev) < 0)
974 {
975 Info("Failed to restart ISOC transfers in pwc_try_video_mode.\n");
976 ret = -EAGAIN; /* let's try again, who knows if it works a second time */
977 }
978 }
979 pdev->drop_frames++; /* try to avoid garbage during switch */
980 return ret; /* Return original error code */
981}
982
983
984/***************************************************************************/
985/* Video4Linux functions */
986
987static int pwc_video_open(struct inode *inode, struct file *file)
988{
989 int i;
990 struct video_device *vdev = video_devdata(file);
991 struct pwc_device *pdev;
992
993 Trace(TRACE_OPEN, ">> video_open called(vdev = 0x%p).\n", vdev);
994
995 pdev = (struct pwc_device *)vdev->priv;
996 if (pdev == NULL)
997 BUG();
998 if (pdev->vopen)
999 return -EBUSY;
1000
1001 down(&pdev->modlock);
1002 if (!pdev->usb_init) {
1003 Trace(TRACE_OPEN, "Doing first time initialization.\n");
1004 pdev->usb_init = 1;
1005
1006 if (pwc_trace & TRACE_OPEN)
1007 {
1008 /* Query sensor type */
1009 const char *sensor_type = NULL;
1010 int ret;
1011
1012 ret = pwc_get_cmos_sensor(pdev, &i);
1013 if (ret >= 0)
1014 {
1015 switch(i) {
1016 case 0x00: sensor_type = "Hyundai CMOS sensor"; break;
1017 case 0x20: sensor_type = "Sony CCD sensor + TDA8787"; break;
1018 case 0x2E: sensor_type = "Sony CCD sensor + Exas 98L59"; break;
1019 case 0x2F: sensor_type = "Sony CCD sensor + ADI 9804"; break;
1020 case 0x30: sensor_type = "Sharp CCD sensor + TDA8787"; break;
1021 case 0x3E: sensor_type = "Sharp CCD sensor + Exas 98L59"; break;
1022 case 0x3F: sensor_type = "Sharp CCD sensor + ADI 9804"; break;
1023 case 0x40: sensor_type = "UPA 1021 sensor"; break;
1024 case 0x100: sensor_type = "VGA sensor"; break;
1025 case 0x101: sensor_type = "PAL MR sensor"; break;
1026 default: sensor_type = "unknown type of sensor"; break;
1027 }
1028 }
1029 if (sensor_type != NULL)
1030 Info("This %s camera is equipped with a %s (%d).\n", pdev->vdev->name, sensor_type, i);
1031 }
1032 }
1033
1034 /* Turn on camera */
1035 if (power_save) {
1036 i = pwc_camera_power(pdev, 1);
1037 if (i < 0)
1038 Info("Failed to restore power to the camera! (%d)\n", i);
1039 }
1040 /* Set LED on/off time */
1041 if (pwc_set_leds(pdev, led_on, led_off) < 0)
1042 Info("Failed to set LED on/off time.\n");
1043
1044 pwc_construct(pdev); /* set min/max sizes correct */
1045
1046 /* So far, so good. Allocate memory. */
1047 i = pwc_allocate_buffers(pdev);
1048 if (i < 0) {
1049 Trace(TRACE_OPEN, "Failed to allocate buffer memory.\n");
1050 up(&pdev->modlock);
1051 return i;
1052 }
1053
1054 /* Reset buffers & parameters */
1055 pwc_reset_buffers(pdev);
1056 for (i = 0; i < default_mbufs; i++)
1057 pdev->image_used[i] = 0;
1058 pdev->vframe_count = 0;
1059 pdev->vframes_dumped = 0;
1060 pdev->vframes_error = 0;
1061 pdev->visoc_errors = 0;
1062 pdev->error_status = 0;
1063#if PWC_DEBUG
1064 pdev->sequence = 0;
1065#endif
1066 pwc_construct(pdev); /* set min/max sizes correct */
1067
1068 /* Set some defaults */
1069 pdev->vsnapshot = 0;
1070
1071 /* Start iso pipe for video; first try the last used video size
1072 (or the default one); if that fails try QCIF/10 or QSIF/10;
1073 it that fails too, give up.
1074 */
1075 i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
1076 if (i) {
1077 Trace(TRACE_OPEN, "First attempt at set_video_mode failed.\n");
1078 if (pdev->type == 730 || pdev->type == 740 || pdev->type == 750)
1079 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QSIF].x, pwc_image_sizes[PSZ_QSIF].y, 10, pdev->vcompression, 0);
1080 else
1081 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QCIF].x, pwc_image_sizes[PSZ_QCIF].y, 10, pdev->vcompression, 0);
1082 }
1083 if (i) {
1084 Trace(TRACE_OPEN, "Second attempt at set_video_mode failed.\n");
1085 up(&pdev->modlock);
1086 return i;
1087 }
1088
1089 i = pwc_isoc_init(pdev);
1090 if (i) {
1091 Trace(TRACE_OPEN, "Failed to init ISOC stuff = %d.\n", i);
1092 up(&pdev->modlock);
1093 return i;
1094 }
1095
1096 pdev->vopen++;
1097 file->private_data = vdev;
1098 up(&pdev->modlock);
1099 Trace(TRACE_OPEN, "<< video_open() returns 0.\n");
1100 return 0;
1101}
1102
1103/* Note that all cleanup is done in the reverse order as in _open */
1104static int pwc_video_close(struct inode *inode, struct file *file)
1105{
1106 struct video_device *vdev = file->private_data;
1107 struct pwc_device *pdev;
1108 int i;
1109
1110 Trace(TRACE_OPEN, ">> video_close called(vdev = 0x%p).\n", vdev);
1111
1112 pdev = (struct pwc_device *)vdev->priv;
1113 if (pdev->vopen == 0)
1114 Info("video_close() called on closed device?\n");
1115
1116 /* Dump statistics, but only if a reasonable amount of frames were
1117 processed (to prevent endless log-entries in case of snap-shot
1118 programs)
1119 */
1120 if (pdev->vframe_count > 20)
1121 Info("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
1122
1123 switch (pdev->type)
1124 {
1125 case 675:
1126 case 680:
1127 case 690:
1128 case 720:
1129 case 730:
1130 case 740:
1131 case 750:
1132/* pwc_dec23_exit(); *//* Timon & Kiara */
1133 break;
1134 case 645:
1135 case 646:
1136/* pwc_dec1_exit(); */
1137 break;
1138 }
1139
1140 pwc_isoc_cleanup(pdev);
1141 pwc_free_buffers(pdev);
1142
1143 /* Turn off LEDS and power down camera, but only when not unplugged */
1144 if (pdev->error_status != EPIPE) {
1145 /* Turn LEDs off */
1146 if (pwc_set_leds(pdev, 0, 0) < 0)
1147 Info("Failed to set LED on/off time.\n");
1148 if (power_save) {
1149 i = pwc_camera_power(pdev, 0);
1150 if (i < 0)
1151 Err("Failed to power down camera (%d)\n", i);
1152 }
1153 }
1154 pdev->vopen = 0;
1155 Trace(TRACE_OPEN, "<< video_close()\n");
1156 return 0;
1157}
1158
1159/*
1160 * FIXME: what about two parallel reads ????
1161 * ANSWER: Not supported. You can't open the device more than once,
1162 despite what the V4L1 interface says. First, I don't see
1163 the need, second there's no mechanism of alerting the
1164 2nd/3rd/... process of events like changing image size.
1165 And I don't see the point of blocking that for the
1166 2nd/3rd/... process.
1167 In multi-threaded environments reading parallel from any
1168 device is tricky anyhow.
1169 */
1170
1171static ssize_t pwc_video_read(struct file *file, char __user * buf,
1172 size_t count, loff_t *ppos)
1173{
1174 struct video_device *vdev = file->private_data;
1175 struct pwc_device *pdev;
1176 int noblock = file->f_flags & O_NONBLOCK;
1177 DECLARE_WAITQUEUE(wait, current);
1178 int bytes_to_read;
1179
1180 Trace(TRACE_READ, "video_read(0x%p, %p, %zu) called.\n", vdev, buf, count);
1181 if (vdev == NULL)
1182 return -EFAULT;
1183 pdev = vdev->priv;
1184 if (pdev == NULL)
1185 return -EFAULT;
1186 if (pdev->error_status)
1187 return -pdev->error_status; /* Something happened, report what. */
1188
1189 /* In case we're doing partial reads, we don't have to wait for a frame */
1190 if (pdev->image_read_pos == 0) {
1191 /* Do wait queueing according to the (doc)book */
1192 add_wait_queue(&pdev->frameq, &wait);
1193 while (pdev->full_frames == NULL) {
1194 /* Check for unplugged/etc. here */
1195 if (pdev->error_status) {
1196 remove_wait_queue(&pdev->frameq, &wait);
1197 set_current_state(TASK_RUNNING);
1198 return -pdev->error_status ;
1199 }
1200 if (noblock) {
1201 remove_wait_queue(&pdev->frameq, &wait);
1202 set_current_state(TASK_RUNNING);
1203 return -EWOULDBLOCK;
1204 }
1205 if (signal_pending(current)) {
1206 remove_wait_queue(&pdev->frameq, &wait);
1207 set_current_state(TASK_RUNNING);
1208 return -ERESTARTSYS;
1209 }
1210 schedule();
1211 set_current_state(TASK_INTERRUPTIBLE);
1212 }
1213 remove_wait_queue(&pdev->frameq, &wait);
1214 set_current_state(TASK_RUNNING);
1215
1216 /* Decompress and release frame */
1217 if (pwc_handle_frame(pdev))
1218 return -EFAULT;
1219 }
1220
1221 Trace(TRACE_READ, "Copying data to user space.\n");
1222 if (pdev->vpalette == VIDEO_PALETTE_RAW)
1223 bytes_to_read = pdev->frame_size;
1224 else
1225 bytes_to_read = pdev->view.size;
1226
1227 /* copy bytes to user space; we allow for partial reads */
1228 if (count + pdev->image_read_pos > bytes_to_read)
1229 count = bytes_to_read - pdev->image_read_pos;
1230 if (copy_to_user(buf, pdev->image_ptr[pdev->fill_image] + pdev->image_read_pos, count))
1231 return -EFAULT;
1232 pdev->image_read_pos += count;
1233 if (pdev->image_read_pos >= bytes_to_read) { /* All data has been read */
1234 pdev->image_read_pos = 0;
1235 pwc_next_image(pdev);
1236 }
1237 return count;
1238}
1239
1240static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
1241{
1242 struct video_device *vdev = file->private_data;
1243 struct pwc_device *pdev;
1244
1245 if (vdev == NULL)
1246 return -EFAULT;
1247 pdev = vdev->priv;
1248 if (pdev == NULL)
1249 return -EFAULT;
1250
1251 poll_wait(file, &pdev->frameq, wait);
1252 if (pdev->error_status)
1253 return POLLERR;
1254 if (pdev->full_frames != NULL) /* we have frames waiting */
1255 return (POLLIN | POLLRDNORM);
1256
1257 return 0;
1258}
1259
1260static int pwc_video_do_ioctl(struct inode *inode, struct file *file,
1261 unsigned int cmd, void *arg)
1262{
1263 struct video_device *vdev = file->private_data;
1264 struct pwc_device *pdev;
1265 DECLARE_WAITQUEUE(wait, current);
1266
1267 if (vdev == NULL)
1268 return -EFAULT;
1269 pdev = vdev->priv;
1270 if (pdev == NULL)
1271 return -EFAULT;
1272
1273 switch (cmd) {
1274 /* Query cabapilities */
1275 case VIDIOCGCAP:
1276 {
1277 struct video_capability *caps = arg;
1278
1279 strcpy(caps->name, vdev->name);
1280 caps->type = VID_TYPE_CAPTURE;
1281 caps->channels = 1;
1282 caps->audios = 1;
1283 caps->minwidth = pdev->view_min.x;
1284 caps->minheight = pdev->view_min.y;
1285 caps->maxwidth = pdev->view_max.x;
1286 caps->maxheight = pdev->view_max.y;
1287 break;
1288 }
1289
1290 /* Channel functions (simulate 1 channel) */
1291 case VIDIOCGCHAN:
1292 {
1293 struct video_channel *v = arg;
1294
1295 if (v->channel != 0)
1296 return -EINVAL;
1297 v->flags = 0;
1298 v->tuners = 0;
1299 v->type = VIDEO_TYPE_CAMERA;
1300 strcpy(v->name, "Webcam");
1301 return 0;
1302 }
1303
1304 case VIDIOCSCHAN:
1305 {
1306 /* The spec says the argument is an integer, but
1307 the bttv driver uses a video_channel arg, which
1308 makes sense becasue it also has the norm flag.
1309 */
1310 struct video_channel *v = arg;
1311 if (v->channel != 0)
1312 return -EINVAL;
1313 return 0;
1314 }
1315
1316
1317 /* Picture functions; contrast etc. */
1318 case VIDIOCGPICT:
1319 {
1320 struct video_picture *p = arg;
1321 int val;
1322
1323 val = pwc_get_brightness(pdev);
1324 if (val >= 0)
1325 p->brightness = val;
1326 else
1327 p->brightness = 0xffff;
1328 val = pwc_get_contrast(pdev);
1329 if (val >= 0)
1330 p->contrast = val;
1331 else
1332 p->contrast = 0xffff;
1333 /* Gamma, Whiteness, what's the difference? :) */
1334 val = pwc_get_gamma(pdev);
1335 if (val >= 0)
1336 p->whiteness = val;
1337 else
1338 p->whiteness = 0xffff;
1339 val = pwc_get_saturation(pdev);
1340 if (val >= 0)
1341 p->colour = val;
1342 else
1343 p->colour = 0xffff;
1344 p->depth = 24;
1345 p->palette = pdev->vpalette;
1346 p->hue = 0xFFFF; /* N/A */
1347 break;
1348 }
1349
1350 case VIDIOCSPICT:
1351 {
1352 struct video_picture *p = arg;
1353 /*
1354 * FIXME: Suppose we are mid read
1355 ANSWER: No problem: the firmware of the camera
1356 can handle brightness/contrast/etc
1357 changes at _any_ time, and the palette
1358 is used exactly once in the uncompress
1359 routine.
1360 */
1361 pwc_set_brightness(pdev, p->brightness);
1362 pwc_set_contrast(pdev, p->contrast);
1363 pwc_set_gamma(pdev, p->whiteness);
1364 pwc_set_saturation(pdev, p->colour);
1365 if (p->palette && p->palette != pdev->vpalette) {
1366 switch (p->palette) {
1367 case VIDEO_PALETTE_YUV420P:
1368 case VIDEO_PALETTE_RAW:
1369 pdev->vpalette = p->palette;
1370 return pwc_try_video_mode(pdev, pdev->image.x, pdev->image.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1371 break;
1372 default:
1373 return -EINVAL;
1374 break;
1375 }
1376 }
1377 break;
1378 }
1379
1380 /* Window/size parameters */
1381 case VIDIOCGWIN:
1382 {
1383 struct video_window *vw = arg;
1384
1385 vw->x = 0;
1386 vw->y = 0;
1387 vw->width = pdev->view.x;
1388 vw->height = pdev->view.y;
1389 vw->chromakey = 0;
1390 vw->flags = (pdev->vframes << PWC_FPS_SHIFT) |
1391 (pdev->vsnapshot ? PWC_FPS_SNAPSHOT : 0);
1392 break;
1393 }
1394
1395 case VIDIOCSWIN:
1396 {
1397 struct video_window *vw = arg;
1398 int fps, snapshot, ret;
1399
1400 fps = (vw->flags & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
1401 snapshot = vw->flags & PWC_FPS_SNAPSHOT;
1402 if (fps == 0)
1403 fps = pdev->vframes;
1404 if (pdev->view.x == vw->width && pdev->view.y && fps == pdev->vframes && snapshot == pdev->vsnapshot)
1405 return 0;
1406 ret = pwc_try_video_mode(pdev, vw->width, vw->height, fps, pdev->vcompression, snapshot);
1407 if (ret)
1408 return ret;
1409 break;
1410 }
1411
1412 /* We don't have overlay support (yet) */
1413 case VIDIOCGFBUF:
1414 {
1415 struct video_buffer *vb = arg;
1416
1417 memset(vb,0,sizeof(*vb));
1418 break;
1419 }
1420
1421 /* mmap() functions */
1422 case VIDIOCGMBUF:
1423 {
1424 /* Tell the user program how much memory is needed for a mmap() */
1425 struct video_mbuf *vm = arg;
1426 int i;
1427
1428 memset(vm, 0, sizeof(*vm));
1429 vm->size = default_mbufs * pdev->len_per_image;
1430 vm->frames = default_mbufs; /* double buffering should be enough for most applications */
1431 for (i = 0; i < default_mbufs; i++)
1432 vm->offsets[i] = i * pdev->len_per_image;
1433 break;
1434 }
1435
1436 case VIDIOCMCAPTURE:
1437 {
1438 /* Start capture into a given image buffer (called 'frame' in video_mmap structure) */
1439 struct video_mmap *vm = arg;
1440
1441 Trace(TRACE_READ, "VIDIOCMCAPTURE: %dx%d, frame %d, format %d\n", vm->width, vm->height, vm->frame, vm->format);
1442 if (vm->frame < 0 || vm->frame >= default_mbufs)
1443 return -EINVAL;
1444
1445 /* xawtv is nasty. It probes the available palettes
1446 by setting a very small image size and trying
1447 various palettes... The driver doesn't support
1448 such small images, so I'm working around it.
1449 */
1450 if (vm->format)
1451 {
1452 switch (vm->format)
1453 {
1454 case VIDEO_PALETTE_YUV420P:
1455 case VIDEO_PALETTE_RAW:
1456 break;
1457 default:
1458 return -EINVAL;
1459 break;
1460 }
1461 }
1462
1463 if ((vm->width != pdev->view.x || vm->height != pdev->view.y) &&
1464 (vm->width >= pdev->view_min.x && vm->height >= pdev->view_min.y)) {
1465 int ret;
1466
1467 Trace(TRACE_OPEN, "VIDIOCMCAPTURE: changing size to please xawtv :-(.\n");
1468 ret = pwc_try_video_mode(pdev, vm->width, vm->height, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1469 if (ret)
1470 return ret;
1471 } /* ... size mismatch */
1472
1473 /* FIXME: should we lock here? */
1474 if (pdev->image_used[vm->frame])
1475 return -EBUSY; /* buffer wasn't available. Bummer */
1476 pdev->image_used[vm->frame] = 1;
1477
1478 /* Okay, we're done here. In the SYNC call we wait until a
1479 frame comes available, then expand image into the given
1480 buffer.
1481 In contrast to the CPiA cam the Philips cams deliver a
1482 constant stream, almost like a grabber card. Also,
1483 we have separate buffers for the rawdata and the image,
1484 meaning we can nearly always expand into the requested buffer.
1485 */
1486 Trace(TRACE_READ, "VIDIOCMCAPTURE done.\n");
1487 break;
1488 }
1489
1490 case VIDIOCSYNC:
1491 {
1492 /* The doc says: "Whenever a buffer is used it should
1493 call VIDIOCSYNC to free this frame up and continue."
1494
1495 The only odd thing about this whole procedure is
1496 that MCAPTURE flags the buffer as "in use", and
1497 SYNC immediately unmarks it, while it isn't
1498 after SYNC that you know that the buffer actually
1499 got filled! So you better not start a CAPTURE in
1500 the same frame immediately (use double buffering).
1501 This is not a problem for this cam, since it has
1502 extra intermediate buffers, but a hardware
1503 grabber card will then overwrite the buffer
1504 you're working on.
1505 */
1506 int *mbuf = arg;
1507 int ret;
1508
1509 Trace(TRACE_READ, "VIDIOCSYNC called (%d).\n", *mbuf);
1510
1511 /* bounds check */
1512 if (*mbuf < 0 || *mbuf >= default_mbufs)
1513 return -EINVAL;
1514 /* check if this buffer was requested anyway */
1515 if (pdev->image_used[*mbuf] == 0)
1516 return -EINVAL;
1517
1518 /* Add ourselves to the frame wait-queue.
1519
1520 FIXME: needs auditing for safety.
1521 QUESTION: In what respect? I think that using the
1522 frameq is safe now.
1523 */
1524 add_wait_queue(&pdev->frameq, &wait);
1525 while (pdev->full_frames == NULL) {
1526 if (pdev->error_status) {
1527 remove_wait_queue(&pdev->frameq, &wait);
1528 set_current_state(TASK_RUNNING);
1529 return -pdev->error_status;
1530 }
1531
1532 if (signal_pending(current)) {
1533 remove_wait_queue(&pdev->frameq, &wait);
1534 set_current_state(TASK_RUNNING);
1535 return -ERESTARTSYS;
1536 }
1537 schedule();
1538 set_current_state(TASK_INTERRUPTIBLE);
1539 }
1540 remove_wait_queue(&pdev->frameq, &wait);
1541 set_current_state(TASK_RUNNING);
1542
1543 /* The frame is ready. Expand in the image buffer
1544 requested by the user. I don't care if you
1545 mmap() 5 buffers and request data in this order:
1546 buffer 4 2 3 0 1 2 3 0 4 3 1 . . .
1547 Grabber hardware may not be so forgiving.
1548 */
1549 Trace(TRACE_READ, "VIDIOCSYNC: frame ready.\n");
1550 pdev->fill_image = *mbuf; /* tell in which buffer we want the image to be expanded */
1551 /* Decompress, etc */
1552 ret = pwc_handle_frame(pdev);
1553 pdev->image_used[*mbuf] = 0;
1554 if (ret)
1555 return -EFAULT;
1556 break;
1557 }
1558
1559 case VIDIOCGAUDIO:
1560 {
1561 struct video_audio *v = arg;
1562
1563 strcpy(v->name, "Microphone");
1564 v->audio = -1; /* unknown audio minor */
1565 v->flags = 0;
1566 v->mode = VIDEO_SOUND_MONO;
1567 v->volume = 0;
1568 v->bass = 0;
1569 v->treble = 0;
1570 v->balance = 0x8000;
1571 v->step = 1;
1572 break;
1573 }
1574
1575 case VIDIOCSAUDIO:
1576 {
1577 /* Dummy: nothing can be set */
1578 break;
1579 }
1580
1581 case VIDIOCGUNIT:
1582 {
1583 struct video_unit *vu = arg;
1584
1585 vu->video = pdev->vdev->minor & 0x3F;
1586 vu->audio = -1; /* not known yet */
1587 vu->vbi = -1;
1588 vu->radio = -1;
1589 vu->teletext = -1;
1590 break;
1591 }
1592 default:
1593 return pwc_ioctl(pdev, cmd, arg);
1594 } /* ..switch */
1595 return 0;
1596}
1597
1598static int pwc_video_ioctl(struct inode *inode, struct file *file,
1599 unsigned int cmd, unsigned long arg)
1600{
1601 return video_usercopy(inode, file, cmd, arg, pwc_video_do_ioctl);
1602}
1603
1604
1605static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
1606{
1607 struct video_device *vdev = file->private_data;
1608 struct pwc_device *pdev;
1609 unsigned long start = vma->vm_start;
1610 unsigned long size = vma->vm_end-vma->vm_start;
1611 unsigned long page, pos;
1612
1613 Trace(TRACE_MEMORY, "mmap(0x%p, 0x%lx, %lu) called.\n", vdev, start, size);
1614 pdev = vdev->priv;
1615
1616 vma->vm_flags |= VM_IO;
1617
1618 pos = (unsigned long)pdev->image_data;
1619 while (size > 0) {
1620 page = vmalloc_to_pfn((void *)pos);
1621 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1622 return -EAGAIN;
1623
1624 start += PAGE_SIZE;
1625 pos += PAGE_SIZE;
1626 if (size > PAGE_SIZE)
1627 size -= PAGE_SIZE;
1628 else
1629 size = 0;
1630 }
1631
1632 return 0;
1633}
1634
1635/***************************************************************************/
1636/* USB functions */
1637
1638/* This function gets called when a new device is plugged in or the usb core
1639 * is loaded.
1640 */
1641
1642static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
1643{
1644 struct usb_device *udev = interface_to_usbdev(intf);
1645 struct pwc_device *pdev = NULL;
1646 int vendor_id, product_id, type_id;
1647 int i, hint;
1648 int features = 0;
1649 int video_nr = -1; /* default: use next available device */
1650 char serial_number[30], *name;
1651
1652 /* Check if we can handle this device */
1653 Trace(TRACE_PROBE, "probe() called [%04X %04X], if %d\n",
1654 le16_to_cpu(udev->descriptor.idVendor),
1655 le16_to_cpu(udev->descriptor.idProduct),
1656 intf->altsetting->desc.bInterfaceNumber);
1657
1658 /* the interfaces are probed one by one. We are only interested in the
1659 video interface (0) now.
1660 Interface 1 is the Audio Control, and interface 2 Audio itself.
1661 */
1662 if (intf->altsetting->desc.bInterfaceNumber > 0)
1663 return -ENODEV;
1664
1665 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
1666 product_id = le16_to_cpu(udev->descriptor.idProduct);
1667
1668 if (vendor_id == 0x0471) {
1669 switch (product_id) {
1670 case 0x0302:
1671 Info("Philips PCA645VC USB webcam detected.\n");
1672 name = "Philips 645 webcam";
1673 type_id = 645;
1674 break;
1675 case 0x0303:
1676 Info("Philips PCA646VC USB webcam detected.\n");
1677 name = "Philips 646 webcam";
1678 type_id = 646;
1679 break;
1680 case 0x0304:
1681 Info("Askey VC010 type 2 USB webcam detected.\n");
1682 name = "Askey VC010 webcam";
1683 type_id = 646;
1684 break;
1685 case 0x0307:
1686 Info("Philips PCVC675K (Vesta) USB webcam detected.\n");
1687 name = "Philips 675 webcam";
1688 type_id = 675;
1689 break;
1690 case 0x0308:
1691 Info("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
1692 name = "Philips 680 webcam";
1693 type_id = 680;
1694 break;
1695 case 0x030C:
1696 Info("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
1697 name = "Philips 690 webcam";
1698 type_id = 690;
1699 break;
1700 case 0x0310:
1701 Info("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
1702 name = "Philips 730 webcam";
1703 type_id = 730;
1704 break;
1705 case 0x0311:
1706 Info("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
1707 name = "Philips 740 webcam";
1708 type_id = 740;
1709 break;
1710 case 0x0312:
1711 Info("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
1712 name = "Philips 750 webcam";
1713 type_id = 750;
1714 break;
1715 case 0x0313:
1716 Info("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
1717 name = "Philips 720K/40 webcam";
1718 type_id = 720;
1719 break;
1720 default:
1721 return -ENODEV;
1722 break;
1723 }
1724 }
1725 else if (vendor_id == 0x069A) {
1726 switch(product_id) {
1727 case 0x0001:
1728 Info("Askey VC010 type 1 USB webcam detected.\n");
1729 name = "Askey VC010 webcam";
1730 type_id = 645;
1731 break;
1732 default:
1733 return -ENODEV;
1734 break;
1735 }
1736 }
1737 else if (vendor_id == 0x046d) {
1738 switch(product_id) {
1739 case 0x08b0:
1740 Info("Logitech QuickCam Pro 3000 USB webcam detected.\n");
1741 name = "Logitech QuickCam Pro 3000";
1742 type_id = 740; /* CCD sensor */
1743 break;
1744 case 0x08b1:
1745 Info("Logitech QuickCam Notebook Pro USB webcam detected.\n");
1746 name = "Logitech QuickCam Notebook Pro";
1747 type_id = 740; /* CCD sensor */
1748 break;
1749 case 0x08b2:
1750 Info("Logitech QuickCam 4000 Pro USB webcam detected.\n");
1751 name = "Logitech QuickCam Pro 4000";
1752 type_id = 740; /* CCD sensor */
1753 break;
1754 case 0x08b3:
1755 Info("Logitech QuickCam Zoom USB webcam detected.\n");
1756 name = "Logitech QuickCam Zoom";
1757 type_id = 740; /* CCD sensor */
1758 break;
1759 case 0x08B4:
1760 Info("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
1761 name = "Logitech QuickCam Zoom";
1762 type_id = 740; /* CCD sensor */
1763 break;
1764 case 0x08b5:
1765 Info("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
1766 name = "Logitech QuickCam Orbit";
1767 type_id = 740; /* CCD sensor */
1768 features |= FEATURE_MOTOR_PANTILT;
1769 break;
1770 case 0x08b6:
1771 case 0x08b7:
1772 case 0x08b8:
1773 Info("Logitech QuickCam detected (reserved ID).\n");
1774 name = "Logitech QuickCam (res.)";
1775 type_id = 730; /* Assuming CMOS */
1776 break;
1777 default:
1778 return -ENODEV;
1779 break;
1780 }
1781 }
1782 else if (vendor_id == 0x055d) {
1783 /* I don't know the difference between the C10 and the C30;
1784 I suppose the difference is the sensor, but both cameras
1785 work equally well with a type_id of 675
1786 */
1787 switch(product_id) {
1788 case 0x9000:
1789 Info("Samsung MPC-C10 USB webcam detected.\n");
1790 name = "Samsung MPC-C10";
1791 type_id = 675;
1792 break;
1793 case 0x9001:
1794 Info("Samsung MPC-C30 USB webcam detected.\n");
1795 name = "Samsung MPC-C30";
1796 type_id = 675;
1797 break;
1798 default:
1799 return -ENODEV;
1800 break;
1801 }
1802 }
1803 else if (vendor_id == 0x041e) {
1804 switch(product_id) {
1805 case 0x400c:
1806 Info("Creative Labs Webcam 5 detected.\n");
1807 name = "Creative Labs Webcam 5";
1808 type_id = 730;
1809 break;
1810 case 0x4011:
1811 Info("Creative Labs Webcam Pro Ex detected.\n");
1812 name = "Creative Labs Webcam Pro Ex";
1813 type_id = 740;
1814 break;
1815 default:
1816 return -ENODEV;
1817 break;
1818 }
1819 }
1820 else if (vendor_id == 0x04cc) {
1821 switch(product_id) {
1822 case 0x8116:
1823 Info("Sotec Afina Eye USB webcam detected.\n");
1824 name = "Sotec Afina Eye";
1825 type_id = 730;
1826 break;
1827 default:
1828 return -ENODEV;
1829 break;
1830 }
1831 }
1832 else if (vendor_id == 0x06be) {
1833 switch(product_id) {
1834 case 0x8116:
1835 /* This is essentially the same cam as the Sotec Afina Eye */
1836 Info("AME Co. Afina Eye USB webcam detected.\n");
1837 name = "AME Co. Afina Eye";
1838 type_id = 750;
1839 break;
1840 default:
1841 return -ENODEV;
1842 break;
1843 }
1844
1845 }
1846 else if (vendor_id == 0x0d81) {
1847 switch(product_id) {
1848 case 0x1900:
1849 Info("Visionite VCS-UC300 USB webcam detected.\n");
1850 name = "Visionite VCS-UC300";
1851 type_id = 740; /* CCD sensor */
1852 break;
1853 case 0x1910:
1854 Info("Visionite VCS-UM100 USB webcam detected.\n");
1855 name = "Visionite VCS-UM100";
1856 type_id = 730; /* CMOS sensor */
1857 break;
1858 default:
1859 return -ENODEV;
1860 break;
1861 }
1862 }
1863 else
1864 return -ENODEV; /* Not any of the know types; but the list keeps growing. */
1865
1866 memset(serial_number, 0, 30);
1867 usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1868 Trace(TRACE_PROBE, "Device serial number is %s\n", serial_number);
1869
1870 if (udev->descriptor.bNumConfigurations > 1)
1871 Info("Warning: more than 1 configuration available.\n");
1872
1873 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1874 pdev = kmalloc(sizeof(struct pwc_device), GFP_KERNEL);
1875 if (pdev == NULL) {
1876 Err("Oops, could not allocate memory for pwc_device.\n");
1877 return -ENOMEM;
1878 }
1879 memset(pdev, 0, sizeof(struct pwc_device));
1880 pdev->type = type_id;
1881 pdev->vsize = default_size;
1882 pdev->vframes = default_fps;
1883 strcpy(pdev->serial, serial_number);
1884 pdev->features = features;
1885 if (vendor_id == 0x046D && product_id == 0x08B5)
1886 {
1887 /* Logitech QuickCam Orbit
1888 The ranges have been determined experimentally; they may differ from cam to cam.
1889 Also, the exact ranges left-right and up-down are different for my cam
1890 */
1891 pdev->angle_range.pan_min = -7000;
1892 pdev->angle_range.pan_max = 7000;
1893 pdev->angle_range.tilt_min = -3000;
1894 pdev->angle_range.tilt_max = 2500;
1895 }
1896
1897 init_MUTEX(&pdev->modlock);
1898 spin_lock_init(&pdev->ptrlock);
1899
1900 pdev->udev = udev;
1901 init_waitqueue_head(&pdev->frameq);
1902 pdev->vcompression = pwc_preferred_compression;
1903
1904 /* Allocate video_device structure */
1905 pdev->vdev = video_device_alloc();
1906 if (pdev->vdev == 0)
1907 {
1908 Err("Err, cannot allocate video_device struture. Failing probe.");
1909 kfree(pdev);
1910 return -ENOMEM;
1911 }
1912 memcpy(pdev->vdev, &pwc_template, sizeof(pwc_template));
1913 strcpy(pdev->vdev->name, name);
1914 pdev->vdev->owner = THIS_MODULE;
1915 video_set_drvdata(pdev->vdev, pdev);
1916
1917 pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1918 Trace(TRACE_PROBE, "Release: %04x\n", pdev->release);
1919
1920 /* Now search device_hint[] table for a match, so we can hint a node number. */
1921 for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
1922 if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
1923 (device_hint[hint].pdev == NULL)) {
1924 /* so far, so good... try serial number */
1925 if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
1926 /* match! */
1927 video_nr = device_hint[hint].device_node;
1928 Trace(TRACE_PROBE, "Found hint, will try to register as /dev/video%d\n", video_nr);
1929 break;
1930 }
1931 }
1932 }
1933
1934 pdev->vdev->release = video_device_release;
1935 i = video_register_device(pdev->vdev, VFL_TYPE_GRABBER, video_nr);
1936 if (i < 0) {
1937 Err("Failed to register as video device (%d).\n", i);
1938 video_device_release(pdev->vdev); /* Drip... drip... drip... */
1939 kfree(pdev); /* Oops, no memory leaks please */
1940 return -EIO;
1941 }
1942 else {
1943 Info("Registered as /dev/video%d.\n", pdev->vdev->minor & 0x3F);
1944 }
1945
1946 /* occupy slot */
1947 if (hint < MAX_DEV_HINTS)
1948 device_hint[hint].pdev = pdev;
1949
1950 Trace(TRACE_PROBE, "probe() function returning struct at 0x%p.\n", pdev);
1951 usb_set_intfdata (intf, pdev);
1952 return 0;
1953}
1954
1955/* The user janked out the cable... */
1956static void usb_pwc_disconnect(struct usb_interface *intf)
1957{
1958 struct pwc_device *pdev;
1959 int hint;
1960
1961 lock_kernel();
1962 pdev = usb_get_intfdata (intf);
1963 usb_set_intfdata (intf, NULL);
1964 if (pdev == NULL) {
1965 Err("pwc_disconnect() Called without private pointer.\n");
1966 goto disconnect_out;
1967 }
1968 if (pdev->udev == NULL) {
1969 Err("pwc_disconnect() already called for %p\n", pdev);
1970 goto disconnect_out;
1971 }
1972 if (pdev->udev != interface_to_usbdev(intf)) {
1973 Err("pwc_disconnect() Woops: pointer mismatch udev/pdev.\n");
1974 goto disconnect_out;
1975 }
1976#ifdef PWC_MAGIC
1977 if (pdev->magic != PWC_MAGIC) {
1978 Err("pwc_disconnect() Magic number failed. Consult your scrolls and try again.\n");
1979 goto disconnect_out;
1980 }
1981#endif
1982
1983 /* We got unplugged; this is signalled by an EPIPE error code */
1984 if (pdev->vopen) {
1985 Info("Disconnected while webcam is in use!\n");
1986 pdev->error_status = EPIPE;
1987 }
1988
1989 /* Alert waiting processes */
1990 wake_up_interruptible(&pdev->frameq);
1991 /* Wait until device is closed */
1992 while (pdev->vopen)
1993 schedule();
1994 /* Device is now closed, so we can safely unregister it */
1995 Trace(TRACE_PROBE, "Unregistering video device in disconnect().\n");
1996 video_unregister_device(pdev->vdev);
1997
1998 /* Free memory (don't set pdev to 0 just yet) */
1999 kfree(pdev);
2000
2001disconnect_out:
2002 /* search device_hint[] table if we occupy a slot, by any chance */
2003 for (hint = 0; hint < MAX_DEV_HINTS; hint++)
2004 if (device_hint[hint].pdev == pdev)
2005 device_hint[hint].pdev = NULL;
2006
2007 unlock_kernel();
2008}
2009
2010
2011/* *grunt* We have to do atoi ourselves :-( */
2012static int pwc_atoi(const char *s)
2013{
2014 int k = 0;
2015
2016 k = 0;
2017 while (*s != '\0' && *s >= '0' && *s <= '9') {
2018 k = 10 * k + (*s - '0');
2019 s++;
2020 }
2021 return k;
2022}
2023
2024
2025/*
2026 * Initialization code & module stuff
2027 */
2028
2029static char size[10];
2030static int fps = 0;
2031static int fbufs = 0;
2032static int mbufs = 0;
2033static int trace = -1;
2034static int compression = -1;
2035static int leds[2] = { -1, -1 };
2036static char *dev_hint[MAX_DEV_HINTS] = { };
2037
2038module_param_string(size, size, sizeof(size), 0);
2039MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
2040module_param(fps, int, 0000);
2041MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
2042module_param(fbufs, int, 0000);
2043MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
2044module_param(mbufs, int, 0000);
2045MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
2046module_param(trace, int, 0000);
2047MODULE_PARM_DESC(trace, "For debugging purposes");
2048module_param(power_save, bool, 0000);
2049MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
2050module_param(compression, int, 0000);
2051MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
2052module_param_array(leds, int, NULL, 0000);
2053MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
2054module_param_array(dev_hint, charp, NULL, 0000);
2055MODULE_PARM_DESC(dev_hint, "Device node hints");
2056
2057MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
2058MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
2059MODULE_LICENSE("GPL");
2060
2061static int __init usb_pwc_init(void)
2062{
2063 int i, sz;
2064 char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
2065
2066 Info("Philips webcam module version " PWC_VERSION " loaded.\n");
2067 Info("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
2068 Info("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
2069 Info("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
2070
2071 if (fps) {
2072 if (fps < 4 || fps > 30) {
2073 Err("Framerate out of bounds (4-30).\n");
2074 return -EINVAL;
2075 }
2076 default_fps = fps;
2077 Info("Default framerate set to %d.\n", default_fps);
2078 }
2079
2080 if (size[0]) {
2081 /* string; try matching with array */
2082 for (sz = 0; sz < PSZ_MAX; sz++) {
2083 if (!strcmp(sizenames[sz], size)) { /* Found! */
2084 default_size = sz;
2085 break;
2086 }
2087 }
2088 if (sz == PSZ_MAX) {
2089 Err("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
2090 return -EINVAL;
2091 }
2092 Info("Default image size set to %s [%dx%d].\n", sizenames[default_size], pwc_image_sizes[default_size].x, pwc_image_sizes[default_size].y);
2093 }
2094 if (mbufs) {
2095 if (mbufs < 1 || mbufs > MAX_IMAGES) {
2096 Err("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
2097 return -EINVAL;
2098 }
2099 default_mbufs = mbufs;
2100 Info("Number of image buffers set to %d.\n", default_mbufs);
2101 }
2102 if (fbufs) {
2103 if (fbufs < 2 || fbufs > MAX_FRAMES) {
2104 Err("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
2105 return -EINVAL;
2106 }
2107 default_fbufs = fbufs;
2108 Info("Number of frame buffers set to %d.\n", default_fbufs);
2109 }
2110 if (trace >= 0) {
2111 Info("Trace options: 0x%04x\n", trace);
2112 pwc_trace = trace;
2113 }
2114 if (compression >= 0) {
2115 if (compression > 3) {
2116 Err("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
2117 return -EINVAL;
2118 }
2119 pwc_preferred_compression = compression;
2120 Info("Preferred compression set to %d.\n", pwc_preferred_compression);
2121 }
2122 if (power_save)
2123 Info("Enabling power save on open/close.\n");
2124 if (leds[0] >= 0)
2125 led_on = leds[0];
2126 if (leds[1] >= 0)
2127 led_off = leds[1];
2128
2129 /* Big device node whoopla. Basically, it allows you to assign a
2130 device node (/dev/videoX) to a camera, based on its type
2131 & serial number. The format is [type[.serialnumber]:]node.
2132
2133 Any camera that isn't matched by these rules gets the next
2134 available free device node.
2135 */
2136 for (i = 0; i < MAX_DEV_HINTS; i++) {
2137 char *s, *colon, *dot;
2138
2139 /* This loop also initializes the array */
2140 device_hint[i].pdev = NULL;
2141 s = dev_hint[i];
2142 if (s != NULL && *s != '\0') {
2143 device_hint[i].type = -1; /* wildcard */
2144 strcpy(device_hint[i].serial_number, "*");
2145
2146 /* parse string: chop at ':' & '/' */
2147 colon = dot = s;
2148 while (*colon != '\0' && *colon != ':')
2149 colon++;
2150 while (*dot != '\0' && *dot != '.')
2151 dot++;
2152 /* Few sanity checks */
2153 if (*dot != '\0' && dot > colon) {
2154 Err("Malformed camera hint: the colon must be after the dot.\n");
2155 return -EINVAL;
2156 }
2157
2158 if (*colon == '\0') {
2159 /* No colon */
2160 if (*dot != '\0') {
2161 Err("Malformed camera hint: no colon + device node given.\n");
2162 return -EINVAL;
2163 }
2164 else {
2165 /* No type or serial number specified, just a number. */
2166 device_hint[i].device_node = pwc_atoi(s);
2167 }
2168 }
2169 else {
2170 /* There's a colon, so we have at least a type and a device node */
2171 device_hint[i].type = pwc_atoi(s);
2172 device_hint[i].device_node = pwc_atoi(colon + 1);
2173 if (*dot != '\0') {
2174 /* There's a serial number as well */
2175 int k;
2176
2177 dot++;
2178 k = 0;
2179 while (*dot != ':' && k < 29) {
2180 device_hint[i].serial_number[k++] = *dot;
2181 dot++;
2182 }
2183 device_hint[i].serial_number[k] = '\0';
2184 }
2185 }
2186#if PWC_DEBUG
2187 Debug("device_hint[%d]:\n", i);
2188 Debug(" type : %d\n", device_hint[i].type);
2189 Debug(" serial# : %s\n", device_hint[i].serial_number);
2190 Debug(" node : %d\n", device_hint[i].device_node);
2191#endif
2192 }
2193 else
2194 device_hint[i].type = 0; /* not filled */
2195 } /* ..for MAX_DEV_HINTS */
2196
2197 Trace(TRACE_PROBE, "Registering driver at address 0x%p.\n", &pwc_driver);
2198 return usb_register(&pwc_driver);
2199}
2200
2201static void __exit usb_pwc_exit(void)
2202{
2203 Trace(TRACE_MODULE, "Deregistering driver.\n");
2204 usb_deregister(&pwc_driver);
2205 Info("Philips webcam module removed.\n");
2206}
2207
2208module_init(usb_pwc_init);
2209module_exit(usb_pwc_exit);
2210
diff --git a/drivers/usb/media/pwc/pwc-ioctl.h b/drivers/usb/media/pwc/pwc-ioctl.h
deleted file mode 100644
index 5f9cb08bc02e..000000000000
--- a/drivers/usb/media/pwc/pwc-ioctl.h
+++ /dev/null
@@ -1,292 +0,0 @@
1#ifndef PWC_IOCTL_H
2#define PWC_IOCTL_H
3
4/* (C) 2001-2004 Nemosoft Unv.
5 (C) 2004 Luc Saillard (luc@saillard.org)
6
7 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
8 driver and thus may have bugs that are not present in the original version.
9 Please send bug reports and support requests to <luc@saillard.org>.
10 The decompression routines have been implemented by reverse-engineering the
11 Nemosoft binary pwcx module. Caveat emptor.
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26*/
27
28/* This is pwc-ioctl.h belonging to PWC 8.12.1
29 It contains structures and defines to communicate from user space
30 directly to the driver.
31 */
32
33/*
34 Changes
35 2001/08/03 Alvarado Added ioctl constants to access methods for
36 changing white balance and red/blue gains
37 2002/12/15 G. H. Fernandez-Toribio VIDIOCGREALSIZE
38 2003/12/13 Nemosft Unv. Some modifications to make interfacing to
39 PWCX easier
40 */
41
42/* These are private ioctl() commands, specific for the Philips webcams.
43 They contain functions not found in other webcams, and settings not
44 specified in the Video4Linux API.
45
46 The #define names are built up like follows:
47 VIDIOC VIDeo IOCtl prefix
48 PWC Philps WebCam
49 G optional: Get
50 S optional: Set
51 ... the function
52 */
53
54
55 /* Enumeration of image sizes */
56#define PSZ_SQCIF 0x00
57#define PSZ_QSIF 0x01
58#define PSZ_QCIF 0x02
59#define PSZ_SIF 0x03
60#define PSZ_CIF 0x04
61#define PSZ_VGA 0x05
62#define PSZ_MAX 6
63
64
65/* The frame rate is encoded in the video_window.flags parameter using
66 the upper 16 bits, since some flags are defined nowadays. The following
67 defines provide a mask and shift to filter out this value.
68
69 In 'Snapshot' mode the camera freezes its automatic exposure and colour
70 balance controls.
71 */
72#define PWC_FPS_SHIFT 16
73#define PWC_FPS_MASK 0x00FF0000
74#define PWC_FPS_FRMASK 0x003F0000
75#define PWC_FPS_SNAPSHOT 0x00400000
76
77
78/* structure for transferring x & y coordinates */
79struct pwc_coord
80{
81 int x, y; /* guess what */
82 int size; /* size, or offset */
83};
84
85
86/* Used with VIDIOCPWCPROBE */
87struct pwc_probe
88{
89 char name[32];
90 int type;
91};
92
93struct pwc_serial
94{
95 char serial[30]; /* String with serial number. Contains terminating 0 */
96};
97
98/* pwc_whitebalance.mode values */
99#define PWC_WB_INDOOR 0
100#define PWC_WB_OUTDOOR 1
101#define PWC_WB_FL 2
102#define PWC_WB_MANUAL 3
103#define PWC_WB_AUTO 4
104
105/* Used with VIDIOCPWC[SG]AWB (Auto White Balance).
106 Set mode to one of the PWC_WB_* values above.
107 *red and *blue are the respective gains of these colour components inside
108 the camera; range 0..65535
109 When 'mode' == PWC_WB_MANUAL, 'manual_red' and 'manual_blue' are set or read;
110 otherwise undefined.
111 'read_red' and 'read_blue' are read-only.
112*/
113struct pwc_whitebalance
114{
115 int mode;
116 int manual_red, manual_blue; /* R/W */
117 int read_red, read_blue; /* R/O */
118};
119
120/*
121 'control_speed' and 'control_delay' are used in automatic whitebalance mode,
122 and tell the camera how fast it should react to changes in lighting, and
123 with how much delay. Valid values are 0..65535.
124*/
125struct pwc_wb_speed
126{
127 int control_speed;
128 int control_delay;
129
130};
131
132/* Used with VIDIOCPWC[SG]LED */
133struct pwc_leds
134{
135 int led_on; /* Led on-time; range = 0..25000 */
136 int led_off; /* Led off-time; range = 0..25000 */
137};
138
139/* Image size (used with GREALSIZE) */
140struct pwc_imagesize
141{
142 int width;
143 int height;
144};
145
146/* Defines and structures for Motorized Pan & Tilt */
147#define PWC_MPT_PAN 0x01
148#define PWC_MPT_TILT 0x02
149#define PWC_MPT_TIMEOUT 0x04 /* for status */
150
151/* Set angles; when absolute != 0, the angle is absolute and the
152 driver calculates the relative offset for you. This can only
153 be used with VIDIOCPWCSANGLE; VIDIOCPWCGANGLE always returns
154 absolute angles.
155 */
156struct pwc_mpt_angles
157{
158 int absolute; /* write-only */
159 int pan; /* degrees * 100 */
160 int tilt; /* degress * 100 */
161};
162
163/* Range of angles of the camera, both horizontally and vertically.
164 */
165struct pwc_mpt_range
166{
167 int pan_min, pan_max; /* degrees * 100 */
168 int tilt_min, tilt_max;
169};
170
171struct pwc_mpt_status
172{
173 int status;
174 int time_pan;
175 int time_tilt;
176};
177
178
179/* This is used for out-of-kernel decompression. With it, you can get
180 all the necessary information to initialize and use the decompressor
181 routines in standalone applications.
182 */
183struct pwc_video_command
184{
185 int type; /* camera type (645, 675, 730, etc.) */
186 int release; /* release number */
187
188 int size; /* one of PSZ_* */
189 int alternate;
190 int command_len; /* length of USB video command */
191 unsigned char command_buf[13]; /* Actual USB video command */
192 int bandlength; /* >0 = compressed */
193 int frame_size; /* Size of one (un)compressed frame */
194};
195
196/* Flags for PWCX subroutines. Not all modules honour all flags. */
197#define PWCX_FLAG_PLANAR 0x0001
198#define PWCX_FLAG_BAYER 0x0008
199
200
201/* IOCTL definitions */
202
203 /* Restore user settings */
204#define VIDIOCPWCRUSER _IO('v', 192)
205 /* Save user settings */
206#define VIDIOCPWCSUSER _IO('v', 193)
207 /* Restore factory settings */
208#define VIDIOCPWCFACTORY _IO('v', 194)
209
210 /* You can manipulate the compression factor. A compression preference of 0
211 means use uncompressed modes when available; 1 is low compression, 2 is
212 medium and 3 is high compression preferred. Of course, the higher the
213 compression, the lower the bandwidth used but more chance of artefacts
214 in the image. The driver automatically chooses a higher compression when
215 the preferred mode is not available.
216 */
217 /* Set preferred compression quality (0 = uncompressed, 3 = highest compression) */
218#define VIDIOCPWCSCQUAL _IOW('v', 195, int)
219 /* Get preferred compression quality */
220#define VIDIOCPWCGCQUAL _IOR('v', 195, int)
221
222
223/* Retrieve serial number of camera */
224#define VIDIOCPWCGSERIAL _IOR('v', 198, struct pwc_serial)
225
226 /* This is a probe function; since so many devices are supported, it
227 becomes difficult to include all the names in programs that want to
228 check for the enhanced Philips stuff. So in stead, try this PROBE;
229 it returns a structure with the original name, and the corresponding
230 Philips type.
231 To use, fill the structure with zeroes, call PROBE and if that succeeds,
232 compare the name with that returned from VIDIOCGCAP; they should be the
233 same. If so, you can be assured it is a Philips (OEM) cam and the type
234 is valid.
235 */
236#define VIDIOCPWCPROBE _IOR('v', 199, struct pwc_probe)
237
238 /* Set AGC (Automatic Gain Control); int < 0 = auto, 0..65535 = fixed */
239#define VIDIOCPWCSAGC _IOW('v', 200, int)
240 /* Get AGC; int < 0 = auto; >= 0 = fixed, range 0..65535 */
241#define VIDIOCPWCGAGC _IOR('v', 200, int)
242 /* Set shutter speed; int < 0 = auto; >= 0 = fixed, range 0..65535 */
243#define VIDIOCPWCSSHUTTER _IOW('v', 201, int)
244
245 /* Color compensation (Auto White Balance) */
246#define VIDIOCPWCSAWB _IOW('v', 202, struct pwc_whitebalance)
247#define VIDIOCPWCGAWB _IOR('v', 202, struct pwc_whitebalance)
248
249 /* Auto WB speed */
250#define VIDIOCPWCSAWBSPEED _IOW('v', 203, struct pwc_wb_speed)
251#define VIDIOCPWCGAWBSPEED _IOR('v', 203, struct pwc_wb_speed)
252
253 /* LEDs on/off/blink; int range 0..65535 */
254#define VIDIOCPWCSLED _IOW('v', 205, struct pwc_leds)
255#define VIDIOCPWCGLED _IOR('v', 205, struct pwc_leds)
256
257 /* Contour (sharpness); int < 0 = auto, 0..65536 = fixed */
258#define VIDIOCPWCSCONTOUR _IOW('v', 206, int)
259#define VIDIOCPWCGCONTOUR _IOR('v', 206, int)
260
261 /* Backlight compensation; 0 = off, otherwise on */
262#define VIDIOCPWCSBACKLIGHT _IOW('v', 207, int)
263#define VIDIOCPWCGBACKLIGHT _IOR('v', 207, int)
264
265 /* Flickerless mode; = 0 off, otherwise on */
266#define VIDIOCPWCSFLICKER _IOW('v', 208, int)
267#define VIDIOCPWCGFLICKER _IOR('v', 208, int)
268
269 /* Dynamic noise reduction; 0 off, 3 = high noise reduction */
270#define VIDIOCPWCSDYNNOISE _IOW('v', 209, int)
271#define VIDIOCPWCGDYNNOISE _IOR('v', 209, int)
272
273 /* Real image size as used by the camera; tells you whether or not there's a gray border around the image */
274#define VIDIOCPWCGREALSIZE _IOR('v', 210, struct pwc_imagesize)
275
276 /* Motorized pan & tilt functions */
277#define VIDIOCPWCMPTRESET _IOW('v', 211, int)
278#define VIDIOCPWCMPTGRANGE _IOR('v', 211, struct pwc_mpt_range)
279#define VIDIOCPWCMPTSANGLE _IOW('v', 212, struct pwc_mpt_angles)
280#define VIDIOCPWCMPTGANGLE _IOR('v', 212, struct pwc_mpt_angles)
281#define VIDIOCPWCMPTSTATUS _IOR('v', 213, struct pwc_mpt_status)
282
283 /* Get the USB set-video command; needed for initializing libpwcx */
284#define VIDIOCPWCGVIDCMD _IOR('v', 215, struct pwc_video_command)
285struct pwc_table_init_buffer {
286 int len;
287 char *buffer;
288
289};
290#define VIDIOCPWCGVIDTABLE _IOR('v', 216, struct pwc_table_init_buffer)
291
292#endif
diff --git a/drivers/usb/media/pwc/pwc-kiara.c b/drivers/usb/media/pwc/pwc-kiara.c
deleted file mode 100644
index c498c68bace1..000000000000
--- a/drivers/usb/media/pwc/pwc-kiara.c
+++ /dev/null
@@ -1,318 +0,0 @@
1/* Linux driver for Philips webcam
2 (C) 2004 Luc Saillard (luc@saillard.org)
3
4 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
5 driver and thus may have bugs that are not present in the original version.
6 Please send bug reports and support requests to <luc@saillard.org>.
7 The decompression routines have been implemented by reverse-engineering the
8 Nemosoft binary pwcx module. Caveat emptor.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25
26/* This tables contains entries for the 730/740/750 (Kiara) camera, with
27 4 different qualities (no compression, low, medium, high).
28 It lists the bandwidth requirements for said mode by its alternate interface
29 number. An alternate of 0 means that the mode is unavailable.
30
31 There are 6 * 4 * 4 entries:
32 6 different resolutions subqcif, qsif, qcif, sif, cif, vga
33 6 framerates: 5, 10, 15, 20, 25, 30
34 4 compression modi: none, low, medium, high
35
36 When an uncompressed mode is not available, the next available compressed mode
37 will be chosen (unless the decompressor is absent). Sometimes there are only
38 1 or 2 compressed modes available; in that case entries are duplicated.
39*/
40
41
42#include "pwc-kiara.h"
43#include "pwc-uncompress.h"
44
45const struct Kiara_table_entry Kiara_table[PSZ_MAX][6][4] =
46{
47 /* SQCIF */
48 {
49 /* 5 fps */
50 {
51 {0, },
52 {0, },
53 {0, },
54 {0, },
55 },
56 /* 10 fps */
57 {
58 {0, },
59 {0, },
60 {0, },
61 {0, },
62 },
63 /* 15 fps */
64 {
65 {0, },
66 {0, },
67 {0, },
68 {0, },
69 },
70 /* 20 fps */
71 {
72 {0, },
73 {0, },
74 {0, },
75 {0, },
76 },
77 /* 25 fps */
78 {
79 {0, },
80 {0, },
81 {0, },
82 {0, },
83 },
84 /* 30 fps */
85 {
86 {0, },
87 {0, },
88 {0, },
89 {0, },
90 },
91 },
92 /* QSIF */
93 {
94 /* 5 fps */
95 {
96 {1, 146, 0, {0x1D, 0xF4, 0x30, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x92, 0x00, 0x80}},
97 {1, 146, 0, {0x1D, 0xF4, 0x30, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x92, 0x00, 0x80}},
98 {1, 146, 0, {0x1D, 0xF4, 0x30, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x92, 0x00, 0x80}},
99 {1, 146, 0, {0x1D, 0xF4, 0x30, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x92, 0x00, 0x80}},
100 },
101 /* 10 fps */
102 {
103 {2, 291, 0, {0x1C, 0xF4, 0x30, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x23, 0x01, 0x80}},
104 {1, 192, 630, {0x14, 0xF4, 0x30, 0x13, 0xA9, 0x12, 0xE1, 0x17, 0x08, 0xC0, 0x00, 0x80}},
105 {1, 192, 630, {0x14, 0xF4, 0x30, 0x13, 0xA9, 0x12, 0xE1, 0x17, 0x08, 0xC0, 0x00, 0x80}},
106 {1, 192, 630, {0x14, 0xF4, 0x30, 0x13, 0xA9, 0x12, 0xE1, 0x17, 0x08, 0xC0, 0x00, 0x80}},
107 },
108 /* 15 fps */
109 {
110 {3, 437, 0, {0x1B, 0xF4, 0x30, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0xB5, 0x01, 0x80}},
111 {2, 292, 640, {0x13, 0xF4, 0x30, 0x13, 0xF7, 0x13, 0x2F, 0x13, 0x20, 0x24, 0x01, 0x80}},
112 {2, 292, 640, {0x13, 0xF4, 0x30, 0x13, 0xF7, 0x13, 0x2F, 0x13, 0x20, 0x24, 0x01, 0x80}},
113 {1, 192, 420, {0x13, 0xF4, 0x30, 0x0D, 0x1B, 0x0C, 0x53, 0x1E, 0x18, 0xC0, 0x00, 0x80}},
114 },
115 /* 20 fps */
116 {
117 {4, 589, 0, {0x1A, 0xF4, 0x30, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x4D, 0x02, 0x80}},
118 {3, 448, 730, {0x12, 0xF4, 0x30, 0x16, 0xC9, 0x16, 0x01, 0x0E, 0x18, 0xC0, 0x01, 0x80}},
119 {2, 292, 476, {0x12, 0xF4, 0x30, 0x0E, 0xD8, 0x0E, 0x10, 0x19, 0x18, 0x24, 0x01, 0x80}},
120 {1, 192, 312, {0x12, 0xF4, 0x50, 0x09, 0xB3, 0x08, 0xEB, 0x1E, 0x18, 0xC0, 0x00, 0x80}},
121 },
122 /* 25 fps */
123 {
124 {5, 703, 0, {0x19, 0xF4, 0x30, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0xBF, 0x02, 0x80}},
125 {3, 447, 610, {0x11, 0xF4, 0x30, 0x13, 0x0B, 0x12, 0x43, 0x14, 0x28, 0xBF, 0x01, 0x80}},
126 {2, 292, 398, {0x11, 0xF4, 0x50, 0x0C, 0x6C, 0x0B, 0xA4, 0x1E, 0x28, 0x24, 0x01, 0x80}},
127 {1, 193, 262, {0x11, 0xF4, 0x50, 0x08, 0x23, 0x07, 0x5B, 0x1E, 0x28, 0xC1, 0x00, 0x80}},
128 },
129 /* 30 fps */
130 {
131 {8, 874, 0, {0x18, 0xF4, 0x30, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x6A, 0x03, 0x80}},
132 {5, 704, 730, {0x10, 0xF4, 0x30, 0x16, 0xC9, 0x16, 0x01, 0x0E, 0x28, 0xC0, 0x02, 0x80}},
133 {3, 448, 492, {0x10, 0xF4, 0x30, 0x0F, 0x5D, 0x0E, 0x95, 0x15, 0x28, 0xC0, 0x01, 0x80}},
134 {2, 292, 320, {0x10, 0xF4, 0x50, 0x09, 0xFB, 0x09, 0x33, 0x1E, 0x28, 0x24, 0x01, 0x80}},
135 },
136 },
137 /* QCIF */
138 {
139 /* 5 fps */
140 {
141 {0, },
142 {0, },
143 {0, },
144 {0, },
145 },
146 /* 10 fps */
147 {
148 {0, },
149 {0, },
150 {0, },
151 {0, },
152 },
153 /* 15 fps */
154 {
155 {0, },
156 {0, },
157 {0, },
158 {0, },
159 },
160 /* 20 fps */
161 {
162 {0, },
163 {0, },
164 {0, },
165 {0, },
166 },
167 /* 25 fps */
168 {
169 {0, },
170 {0, },
171 {0, },
172 {0, },
173 },
174 /* 30 fps */
175 {
176 {0, },
177 {0, },
178 {0, },
179 {0, },
180 },
181 },
182 /* SIF */
183 {
184 /* 5 fps */
185 {
186 {4, 582, 0, {0x0D, 0xF4, 0x30, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x46, 0x02, 0x80}},
187 {3, 387, 1276, {0x05, 0xF4, 0x30, 0x27, 0xD8, 0x26, 0x48, 0x03, 0x10, 0x83, 0x01, 0x80}},
188 {2, 291, 960, {0x05, 0xF4, 0x30, 0x1D, 0xF2, 0x1C, 0x62, 0x04, 0x10, 0x23, 0x01, 0x80}},
189 {1, 191, 630, {0x05, 0xF4, 0x50, 0x13, 0xA9, 0x12, 0x19, 0x05, 0x18, 0xBF, 0x00, 0x80}},
190 },
191 /* 10 fps */
192 {
193 {0, },
194 {6, 775, 1278, {0x04, 0xF4, 0x30, 0x27, 0xE8, 0x26, 0x58, 0x05, 0x30, 0x07, 0x03, 0x80}},
195 {3, 447, 736, {0x04, 0xF4, 0x30, 0x16, 0xFB, 0x15, 0x6B, 0x05, 0x28, 0xBF, 0x01, 0x80}},
196 {2, 292, 480, {0x04, 0xF4, 0x70, 0x0E, 0xF9, 0x0D, 0x69, 0x09, 0x28, 0x24, 0x01, 0x80}},
197 },
198 /* 15 fps */
199 {
200 {0, },
201 {9, 955, 1050, {0x03, 0xF4, 0x30, 0x20, 0xCF, 0x1F, 0x3F, 0x06, 0x48, 0xBB, 0x03, 0x80}},
202 {4, 592, 650, {0x03, 0xF4, 0x30, 0x14, 0x44, 0x12, 0xB4, 0x08, 0x30, 0x50, 0x02, 0x80}},
203 {3, 448, 492, {0x03, 0xF4, 0x50, 0x0F, 0x52, 0x0D, 0xC2, 0x09, 0x38, 0xC0, 0x01, 0x80}},
204 },
205 /* 20 fps */
206 {
207 {0, },
208 {9, 958, 782, {0x02, 0xF4, 0x30, 0x18, 0x6A, 0x16, 0xDA, 0x0B, 0x58, 0xBE, 0x03, 0x80}},
209 {5, 703, 574, {0x02, 0xF4, 0x50, 0x11, 0xE7, 0x10, 0x57, 0x0B, 0x40, 0xBF, 0x02, 0x80}},
210 {3, 446, 364, {0x02, 0xF4, 0x90, 0x0B, 0x5C, 0x09, 0xCC, 0x0E, 0x38, 0xBE, 0x01, 0x80}},
211 },
212 /* 25 fps */
213 {
214 {0, },
215 {9, 958, 654, {0x01, 0xF4, 0x30, 0x14, 0x66, 0x12, 0xD6, 0x0B, 0x50, 0xBE, 0x03, 0x80}},
216 {6, 776, 530, {0x01, 0xF4, 0x50, 0x10, 0x8C, 0x0E, 0xFC, 0x0C, 0x48, 0x08, 0x03, 0x80}},
217 {4, 592, 404, {0x01, 0xF4, 0x70, 0x0C, 0x96, 0x0B, 0x06, 0x0B, 0x48, 0x50, 0x02, 0x80}},
218 },
219 /* 30 fps */
220 {
221 {0, },
222 {9, 957, 526, {0x00, 0xF4, 0x50, 0x10, 0x68, 0x0E, 0xD8, 0x0D, 0x58, 0xBD, 0x03, 0x80}},
223 {6, 775, 426, {0x00, 0xF4, 0x70, 0x0D, 0x48, 0x0B, 0xB8, 0x0F, 0x50, 0x07, 0x03, 0x80}},
224 {4, 590, 324, {0x00, 0x7A, 0x88, 0x0A, 0x1C, 0x08, 0xB4, 0x0E, 0x50, 0x4E, 0x02, 0x80}},
225 },
226 },
227 /* CIF */
228 {
229 /* 5 fps */
230 {
231 {0, },
232 {0, },
233 {0, },
234 {0, },
235 },
236 /* 10 fps */
237 {
238 {0, },
239 {0, },
240 {0, },
241 {0, },
242 },
243 /* 15 fps */
244 {
245 {0, },
246 {0, },
247 {0, },
248 {0, },
249 },
250 /* 20 fps */
251 {
252 {0, },
253 {0, },
254 {0, },
255 {0, },
256 },
257 /* 25 fps */
258 {
259 {0, },
260 {0, },
261 {0, },
262 {0, },
263 },
264 /* 30 fps */
265 {
266 {0, },
267 {0, },
268 {0, },
269 {0, },
270 },
271 },
272 /* VGA */
273 {
274 /* 5 fps */
275 {
276 {0, },
277 {6, 773, 1272, {0x25, 0xF4, 0x30, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}},
278 {4, 592, 976, {0x25, 0xF4, 0x50, 0x1E, 0x78, 0x1B, 0x58, 0x03, 0x30, 0x50, 0x02, 0x80}},
279 {3, 448, 738, {0x25, 0xF4, 0x90, 0x17, 0x0C, 0x13, 0xEC, 0x04, 0x30, 0xC0, 0x01, 0x80}},
280 },
281 /* 10 fps */
282 {
283 {0, },
284 {9, 956, 788, {0x24, 0xF4, 0x70, 0x18, 0x9C, 0x15, 0x7C, 0x03, 0x48, 0xBC, 0x03, 0x80}},
285 {6, 776, 640, {0x24, 0xF4, 0xB0, 0x13, 0xFC, 0x11, 0x2C, 0x04, 0x48, 0x08, 0x03, 0x80}},
286 {4, 592, 488, {0x24, 0x7A, 0xE8, 0x0F, 0x3C, 0x0C, 0x6C, 0x06, 0x48, 0x50, 0x02, 0x80}},
287 },
288 /* 15 fps */
289 {
290 {0, },
291 {9, 957, 526, {0x23, 0x7A, 0xE8, 0x10, 0x68, 0x0D, 0x98, 0x06, 0x58, 0xBD, 0x03, 0x80}},
292 {9, 957, 526, {0x23, 0x7A, 0xE8, 0x10, 0x68, 0x0D, 0x98, 0x06, 0x58, 0xBD, 0x03, 0x80}},
293 {8, 895, 492, {0x23, 0x7A, 0xE8, 0x0F, 0x5D, 0x0C, 0x8D, 0x06, 0x58, 0x7F, 0x03, 0x80}},
294 },
295 /* 20 fps */
296 {
297 {0, },
298 {0, },
299 {0, },
300 {0, },
301 },
302 /* 25 fps */
303 {
304 {0, },
305 {0, },
306 {0, },
307 {0, },
308 },
309 /* 30 fps */
310 {
311 {0, },
312 {0, },
313 {0, },
314 {0, },
315 },
316 },
317};
318
diff --git a/drivers/usb/media/pwc/pwc-kiara.h b/drivers/usb/media/pwc/pwc-kiara.h
deleted file mode 100644
index 12929abbb1f0..000000000000
--- a/drivers/usb/media/pwc/pwc-kiara.h
+++ /dev/null
@@ -1,45 +0,0 @@
1/* Linux driver for Philips webcam
2 (C) 2004 Luc Saillard (luc@saillard.org)
3
4 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
5 driver and thus may have bugs that are not present in the original version.
6 Please send bug reports and support requests to <luc@saillard.org>.
7 The decompression routines have been implemented by reverse-engineering the
8 Nemosoft binary pwcx module. Caveat emptor.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25/* Entries for the Kiara (730/740/750) camera */
26
27#ifndef PWC_KIARA_H
28#define PWC_KIARA_H
29
30#include "pwc-ioctl.h"
31
32struct Kiara_table_entry
33{
34 char alternate; /* USB alternate interface */
35 unsigned short packetsize; /* Normal packet size */
36 unsigned short bandlength; /* Bandlength when decompressing */
37 unsigned char mode[12]; /* precomputed mode settings for cam */
38};
39
40const extern struct Kiara_table_entry Kiara_table[PSZ_MAX][6][4];
41const extern unsigned int KiaraRomTable[8][2][16][8];
42
43#endif
44
45
diff --git a/drivers/usb/media/pwc/pwc-misc.c b/drivers/usb/media/pwc/pwc-misc.c
deleted file mode 100644
index b7a4bd3524c7..000000000000
--- a/drivers/usb/media/pwc/pwc-misc.c
+++ /dev/null
@@ -1,140 +0,0 @@
1/* Linux driver for Philips webcam
2 Various miscellaneous functions and tables.
3 (C) 1999-2003 Nemosoft Unv.
4 (C) 2004 Luc Saillard (luc@saillard.org)
5
6 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
7 driver and thus may have bugs that are not present in the original version.
8 Please send bug reports and support requests to <luc@saillard.org>.
9 The decompression routines have been implemented by reverse-engineering the
10 Nemosoft binary pwcx module. Caveat emptor.
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#include <linux/slab.h>
28
29#include "pwc.h"
30
31struct pwc_coord pwc_image_sizes[PSZ_MAX] =
32{
33 { 128, 96, 0 },
34 { 160, 120, 0 },
35 { 176, 144, 0 },
36 { 320, 240, 0 },
37 { 352, 288, 0 },
38 { 640, 480, 0 },
39};
40
41/* x,y -> PSZ_ */
42int pwc_decode_size(struct pwc_device *pdev, int width, int height)
43{
44 int i, find;
45
46 /* Make sure we don't go beyond our max size.
47 NB: we have different limits for RAW and normal modes. In case
48 you don't have the decompressor loaded or use RAW mode,
49 the maximum viewable size is smaller.
50 */
51 if (pdev->vpalette == VIDEO_PALETTE_RAW)
52 {
53 if (width > pdev->abs_max.x || height > pdev->abs_max.y)
54 {
55 Debug("VIDEO_PALETTE_RAW: going beyond abs_max.\n");
56 return -1;
57 }
58 }
59 else
60 {
61 if (width > pdev->view_max.x || height > pdev->view_max.y)
62 {
63 Debug("VIDEO_PALETTE_ not RAW: going beyond view_max.\n");
64 return -1;
65 }
66 }
67
68 /* Find the largest size supported by the camera that fits into the
69 requested size.
70 */
71 find = -1;
72 for (i = 0; i < PSZ_MAX; i++) {
73 if (pdev->image_mask & (1 << i)) {
74 if (pwc_image_sizes[i].x <= width && pwc_image_sizes[i].y <= height)
75 find = i;
76 }
77 }
78 return find;
79}
80
81/* initialize variables depending on type and decompressor*/
82void pwc_construct(struct pwc_device *pdev)
83{
84 switch(pdev->type) {
85 case 645:
86 case 646:
87 pdev->view_min.x = 128;
88 pdev->view_min.y = 96;
89 pdev->view_max.x = 352;
90 pdev->view_max.y = 288;
91 pdev->abs_max.x = 352;
92 pdev->abs_max.y = 288;
93 pdev->image_mask = 1 << PSZ_SQCIF | 1 << PSZ_QCIF | 1 << PSZ_CIF;
94 pdev->vcinterface = 2;
95 pdev->vendpoint = 4;
96 pdev->frame_header_size = 0;
97 pdev->frame_trailer_size = 0;
98 break;
99 case 675:
100 case 680:
101 case 690:
102 pdev->view_min.x = 128;
103 pdev->view_min.y = 96;
104 /* Anthill bug #38: PWC always reports max size, even without PWCX */
105 pdev->view_max.x = 640;
106 pdev->view_max.y = 480;
107 pdev->image_mask = 1 << PSZ_SQCIF | 1 << PSZ_QSIF | 1 << PSZ_QCIF | 1 << PSZ_SIF | 1 << PSZ_CIF | 1 << PSZ_VGA;
108 pdev->abs_max.x = 640;
109 pdev->abs_max.y = 480;
110 pdev->vcinterface = 3;
111 pdev->vendpoint = 4;
112 pdev->frame_header_size = 0;
113 pdev->frame_trailer_size = 0;
114 break;
115 case 720:
116 case 730:
117 case 740:
118 case 750:
119 pdev->view_min.x = 160;
120 pdev->view_min.y = 120;
121 pdev->view_max.x = 640;
122 pdev->view_max.y = 480;
123 pdev->image_mask = 1 << PSZ_QSIF | 1 << PSZ_SIF | 1 << PSZ_VGA;
124 pdev->abs_max.x = 640;
125 pdev->abs_max.y = 480;
126 pdev->vcinterface = 3;
127 pdev->vendpoint = 5;
128 pdev->frame_header_size = TOUCAM_HEADER_SIZE;
129 pdev->frame_trailer_size = TOUCAM_TRAILER_SIZE;
130 break;
131 }
132 Debug("type = %d\n",pdev->type);
133 pdev->vpalette = VIDEO_PALETTE_YUV420P; /* default */
134 pdev->view_min.size = pdev->view_min.x * pdev->view_min.y;
135 pdev->view_max.size = pdev->view_max.x * pdev->view_max.y;
136 /* length of image, in YUV format; always allocate enough memory. */
137 pdev->len_per_image = (pdev->abs_max.x * pdev->abs_max.y * 3) / 2;
138}
139
140
diff --git a/drivers/usb/media/pwc/pwc-nala.h b/drivers/usb/media/pwc/pwc-nala.h
deleted file mode 100644
index e6c5cb69d03b..000000000000
--- a/drivers/usb/media/pwc/pwc-nala.h
+++ /dev/null
@@ -1,66 +0,0 @@
1 /* SQCIF */
2 {
3 {0, 0, {0x04, 0x01, 0x03}},
4 {8, 0, {0x05, 0x01, 0x03}},
5 {7, 0, {0x08, 0x01, 0x03}},
6 {7, 0, {0x0A, 0x01, 0x03}},
7 {6, 0, {0x0C, 0x01, 0x03}},
8 {5, 0, {0x0F, 0x01, 0x03}},
9 {4, 0, {0x14, 0x01, 0x03}},
10 {3, 0, {0x18, 0x01, 0x03}},
11 },
12 /* QSIF */
13 {
14 {0},
15 {0},
16 {0},
17 {0},
18 {0},
19 {0},
20 {0},
21 {0},
22 },
23 /* QCIF */
24 {
25 {0, 0, {0x04, 0x01, 0x02}},
26 {8, 0, {0x05, 0x01, 0x02}},
27 {7, 0, {0x08, 0x01, 0x02}},
28 {6, 0, {0x0A, 0x01, 0x02}},
29 {5, 0, {0x0C, 0x01, 0x02}},
30 {4, 0, {0x0F, 0x01, 0x02}},
31 {1, 0, {0x14, 0x01, 0x02}},
32 {1, 0, {0x18, 0x01, 0x02}},
33 },
34 /* SIF */
35 {
36 {0},
37 {0},
38 {0},
39 {0},
40 {0},
41 {0},
42 {0},
43 {0},
44 },
45 /* CIF */
46 {
47 {4, 0, {0x04, 0x01, 0x01}},
48 {7, 1, {0x05, 0x03, 0x01}},
49 {6, 1, {0x08, 0x03, 0x01}},
50 {4, 1, {0x0A, 0x03, 0x01}},
51 {3, 1, {0x0C, 0x03, 0x01}},
52 {2, 1, {0x0F, 0x03, 0x01}},
53 {0},
54 {0},
55 },
56 /* VGA */
57 {
58 {0},
59 {0},
60 {0},
61 {0},
62 {0},
63 {0},
64 {0},
65 {0},
66 },
diff --git a/drivers/usb/media/pwc/pwc-timon.c b/drivers/usb/media/pwc/pwc-timon.c
deleted file mode 100644
index dee967173d6c..000000000000
--- a/drivers/usb/media/pwc/pwc-timon.c
+++ /dev/null
@@ -1,316 +0,0 @@
1/* Linux driver for Philips webcam
2 (C) 2004 Luc Saillard (luc@saillard.org)
3
4 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
5 driver and thus may have bugs that are not present in the original version.
6 Please send bug reports and support requests to <luc@saillard.org>.
7 The decompression routines have been implemented by reverse-engineering the
8 Nemosoft binary pwcx module. Caveat emptor.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25
26/* This tables contains entries for the 675/680/690 (Timon) camera, with
27 4 different qualities (no compression, low, medium, high).
28 It lists the bandwidth requirements for said mode by its alternate interface
29 number. An alternate of 0 means that the mode is unavailable.
30
31 There are 6 * 4 * 4 entries:
32 6 different resolutions subqcif, qsif, qcif, sif, cif, vga
33 6 framerates: 5, 10, 15, 20, 25, 30
34 4 compression modi: none, low, medium, high
35
36 When an uncompressed mode is not available, the next available compressed mode
37 will be chosen (unless the decompressor is absent). Sometimes there are only
38 1 or 2 compressed modes available; in that case entries are duplicated.
39*/
40
41#include "pwc-timon.h"
42
43const struct Timon_table_entry Timon_table[PSZ_MAX][6][4] =
44{
45 /* SQCIF */
46 {
47 /* 5 fps */
48 {
49 {1, 140, 0, {0x05, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x8C, 0xFC, 0x80, 0x02}},
50 {1, 140, 0, {0x05, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x8C, 0xFC, 0x80, 0x02}},
51 {1, 140, 0, {0x05, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x8C, 0xFC, 0x80, 0x02}},
52 {1, 140, 0, {0x05, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x8C, 0xFC, 0x80, 0x02}},
53 },
54 /* 10 fps */
55 {
56 {2, 280, 0, {0x04, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x18, 0xA9, 0x80, 0x02}},
57 {2, 280, 0, {0x04, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x18, 0xA9, 0x80, 0x02}},
58 {2, 280, 0, {0x04, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x18, 0xA9, 0x80, 0x02}},
59 {2, 280, 0, {0x04, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x18, 0xA9, 0x80, 0x02}},
60 },
61 /* 15 fps */
62 {
63 {3, 410, 0, {0x03, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x9A, 0x71, 0x80, 0x02}},
64 {3, 410, 0, {0x03, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x9A, 0x71, 0x80, 0x02}},
65 {3, 410, 0, {0x03, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x9A, 0x71, 0x80, 0x02}},
66 {3, 410, 0, {0x03, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x9A, 0x71, 0x80, 0x02}},
67 },
68 /* 20 fps */
69 {
70 {4, 559, 0, {0x02, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x2F, 0x56, 0x80, 0x02}},
71 {4, 559, 0, {0x02, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x2F, 0x56, 0x80, 0x02}},
72 {4, 559, 0, {0x02, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x2F, 0x56, 0x80, 0x02}},
73 {4, 559, 0, {0x02, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x2F, 0x56, 0x80, 0x02}},
74 },
75 /* 25 fps */
76 {
77 {5, 659, 0, {0x01, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x93, 0x46, 0x80, 0x02}},
78 {5, 659, 0, {0x01, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x93, 0x46, 0x80, 0x02}},
79 {5, 659, 0, {0x01, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x93, 0x46, 0x80, 0x02}},
80 {5, 659, 0, {0x01, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x93, 0x46, 0x80, 0x02}},
81 },
82 /* 30 fps */
83 {
84 {7, 838, 0, {0x00, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x46, 0x3B, 0x80, 0x02}},
85 {7, 838, 0, {0x00, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x46, 0x3B, 0x80, 0x02}},
86 {7, 838, 0, {0x00, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x46, 0x3B, 0x80, 0x02}},
87 {7, 838, 0, {0x00, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x46, 0x3B, 0x80, 0x02}},
88 },
89 },
90 /* QSIF */
91 {
92 /* 5 fps */
93 {
94 {1, 146, 0, {0x2D, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x92, 0xFC, 0xC0, 0x02}},
95 {1, 146, 0, {0x2D, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x92, 0xFC, 0xC0, 0x02}},
96 {1, 146, 0, {0x2D, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x92, 0xFC, 0xC0, 0x02}},
97 {1, 146, 0, {0x2D, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x92, 0xFC, 0xC0, 0x02}},
98 },
99 /* 10 fps */
100 {
101 {2, 291, 0, {0x2C, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x23, 0xA1, 0xC0, 0x02}},
102 {1, 191, 630, {0x2C, 0xF4, 0x05, 0x13, 0xA9, 0x12, 0xE1, 0x17, 0x08, 0xBF, 0xF4, 0xC0, 0x02}},
103 {1, 191, 630, {0x2C, 0xF4, 0x05, 0x13, 0xA9, 0x12, 0xE1, 0x17, 0x08, 0xBF, 0xF4, 0xC0, 0x02}},
104 {1, 191, 630, {0x2C, 0xF4, 0x05, 0x13, 0xA9, 0x12, 0xE1, 0x17, 0x08, 0xBF, 0xF4, 0xC0, 0x02}},
105 },
106 /* 15 fps */
107 {
108 {3, 437, 0, {0x2B, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0xB5, 0x6D, 0xC0, 0x02}},
109 {2, 291, 640, {0x2B, 0xF4, 0x05, 0x13, 0xF7, 0x13, 0x2F, 0x13, 0x08, 0x23, 0xA1, 0xC0, 0x02}},
110 {2, 291, 640, {0x2B, 0xF4, 0x05, 0x13, 0xF7, 0x13, 0x2F, 0x13, 0x08, 0x23, 0xA1, 0xC0, 0x02}},
111 {1, 191, 420, {0x2B, 0xF4, 0x0D, 0x0D, 0x1B, 0x0C, 0x53, 0x1E, 0x08, 0xBF, 0xF4, 0xC0, 0x02}},
112 },
113 /* 20 fps */
114 {
115 {4, 588, 0, {0x2A, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x4C, 0x52, 0xC0, 0x02}},
116 {3, 447, 730, {0x2A, 0xF4, 0x05, 0x16, 0xC9, 0x16, 0x01, 0x0E, 0x18, 0xBF, 0x69, 0xC0, 0x02}},
117 {2, 292, 476, {0x2A, 0xF4, 0x0D, 0x0E, 0xD8, 0x0E, 0x10, 0x19, 0x18, 0x24, 0xA1, 0xC0, 0x02}},
118 {1, 192, 312, {0x2A, 0xF4, 0x1D, 0x09, 0xB3, 0x08, 0xEB, 0x1E, 0x18, 0xC0, 0xF4, 0xC0, 0x02}},
119 },
120 /* 25 fps */
121 {
122 {5, 703, 0, {0x29, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0xBF, 0x42, 0xC0, 0x02}},
123 {3, 447, 610, {0x29, 0xF4, 0x05, 0x13, 0x0B, 0x12, 0x43, 0x14, 0x18, 0xBF, 0x69, 0xC0, 0x02}},
124 {2, 292, 398, {0x29, 0xF4, 0x0D, 0x0C, 0x6C, 0x0B, 0xA4, 0x1E, 0x18, 0x24, 0xA1, 0xC0, 0x02}},
125 {1, 192, 262, {0x29, 0xF4, 0x25, 0x08, 0x23, 0x07, 0x5B, 0x1E, 0x18, 0xC0, 0xF4, 0xC0, 0x02}},
126 },
127 /* 30 fps */
128 {
129 {8, 873, 0, {0x28, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x69, 0x37, 0xC0, 0x02}},
130 {5, 704, 774, {0x28, 0xF4, 0x05, 0x18, 0x21, 0x17, 0x59, 0x0F, 0x18, 0xC0, 0x42, 0xC0, 0x02}},
131 {3, 448, 492, {0x28, 0xF4, 0x05, 0x0F, 0x5D, 0x0E, 0x95, 0x15, 0x18, 0xC0, 0x69, 0xC0, 0x02}},
132 {2, 291, 320, {0x28, 0xF4, 0x1D, 0x09, 0xFB, 0x09, 0x33, 0x1E, 0x18, 0x23, 0xA1, 0xC0, 0x02}},
133 },
134 },
135 /* QCIF */
136 {
137 /* 5 fps */
138 {
139 {1, 193, 0, {0x0D, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0xC1, 0xF4, 0xC0, 0x02}},
140 {1, 193, 0, {0x0D, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0xC1, 0xF4, 0xC0, 0x02}},
141 {1, 193, 0, {0x0D, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0xC1, 0xF4, 0xC0, 0x02}},
142 {1, 193, 0, {0x0D, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0xC1, 0xF4, 0xC0, 0x02}},
143 },
144 /* 10 fps */
145 {
146 {3, 385, 0, {0x0C, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x81, 0x79, 0xC0, 0x02}},
147 {2, 291, 800, {0x0C, 0xF4, 0x05, 0x18, 0xF4, 0x18, 0x18, 0x11, 0x08, 0x23, 0xA1, 0xC0, 0x02}},
148 {2, 291, 800, {0x0C, 0xF4, 0x05, 0x18, 0xF4, 0x18, 0x18, 0x11, 0x08, 0x23, 0xA1, 0xC0, 0x02}},
149 {1, 194, 532, {0x0C, 0xF4, 0x05, 0x10, 0x9A, 0x0F, 0xBE, 0x1B, 0x08, 0xC2, 0xF0, 0xC0, 0x02}},
150 },
151 /* 15 fps */
152 {
153 {4, 577, 0, {0x0B, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x41, 0x52, 0xC0, 0x02}},
154 {3, 447, 818, {0x0B, 0xF4, 0x05, 0x19, 0x89, 0x18, 0xAD, 0x0F, 0x10, 0xBF, 0x69, 0xC0, 0x02}},
155 {2, 292, 534, {0x0B, 0xF4, 0x05, 0x10, 0xA3, 0x0F, 0xC7, 0x19, 0x10, 0x24, 0xA1, 0xC0, 0x02}},
156 {1, 195, 356, {0x0B, 0xF4, 0x15, 0x0B, 0x11, 0x0A, 0x35, 0x1E, 0x10, 0xC3, 0xF0, 0xC0, 0x02}},
157 },
158 /* 20 fps */
159 {
160 {6, 776, 0, {0x0A, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x08, 0x3F, 0xC0, 0x02}},
161 {4, 591, 804, {0x0A, 0xF4, 0x05, 0x19, 0x1E, 0x18, 0x42, 0x0F, 0x18, 0x4F, 0x4E, 0xC0, 0x02}},
162 {3, 447, 608, {0x0A, 0xF4, 0x05, 0x12, 0xFD, 0x12, 0x21, 0x15, 0x18, 0xBF, 0x69, 0xC0, 0x02}},
163 {2, 291, 396, {0x0A, 0xF4, 0x15, 0x0C, 0x5E, 0x0B, 0x82, 0x1E, 0x18, 0x23, 0xA1, 0xC0, 0x02}},
164 },
165 /* 25 fps */
166 {
167 {9, 928, 0, {0x09, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0xA0, 0x33, 0xC0, 0x02}},
168 {5, 703, 800, {0x09, 0xF4, 0x05, 0x18, 0xF4, 0x18, 0x18, 0x10, 0x18, 0xBF, 0x42, 0xC0, 0x02}},
169 {3, 447, 508, {0x09, 0xF4, 0x0D, 0x0F, 0xD2, 0x0E, 0xF6, 0x1B, 0x18, 0xBF, 0x69, 0xC0, 0x02}},
170 {2, 292, 332, {0x09, 0xF4, 0x1D, 0x0A, 0x5A, 0x09, 0x7E, 0x1E, 0x18, 0x24, 0xA1, 0xC0, 0x02}},
171 },
172 /* 30 fps */
173 {
174 {0, },
175 {9, 956, 876, {0x08, 0xF4, 0x05, 0x1B, 0x58, 0x1A, 0x7C, 0x0E, 0x20, 0xBC, 0x33, 0x10, 0x02}},
176 {4, 592, 542, {0x08, 0xF4, 0x05, 0x10, 0xE4, 0x10, 0x08, 0x17, 0x20, 0x50, 0x4E, 0x10, 0x02}},
177 {2, 291, 266, {0x08, 0xF4, 0x25, 0x08, 0x48, 0x07, 0x6C, 0x1E, 0x20, 0x23, 0xA1, 0x10, 0x02}},
178 },
179 },
180 /* SIF */
181 {
182 /* 5 fps */
183 {
184 {4, 582, 0, {0x35, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x46, 0x52, 0x60, 0x02}},
185 {3, 387, 1276, {0x35, 0xF4, 0x05, 0x27, 0xD8, 0x26, 0x48, 0x03, 0x10, 0x83, 0x79, 0x60, 0x02}},
186 {2, 291, 960, {0x35, 0xF4, 0x0D, 0x1D, 0xF2, 0x1C, 0x62, 0x04, 0x10, 0x23, 0xA1, 0x60, 0x02}},
187 {1, 191, 630, {0x35, 0xF4, 0x1D, 0x13, 0xA9, 0x12, 0x19, 0x05, 0x08, 0xBF, 0xF4, 0x60, 0x02}},
188 },
189 /* 10 fps */
190 {
191 {0, },
192 {6, 775, 1278, {0x34, 0xF4, 0x05, 0x27, 0xE8, 0x26, 0x58, 0x05, 0x30, 0x07, 0x3F, 0x10, 0x02}},
193 {3, 447, 736, {0x34, 0xF4, 0x15, 0x16, 0xFB, 0x15, 0x6B, 0x05, 0x18, 0xBF, 0x69, 0x10, 0x02}},
194 {2, 291, 480, {0x34, 0xF4, 0x2D, 0x0E, 0xF9, 0x0D, 0x69, 0x09, 0x18, 0x23, 0xA1, 0x10, 0x02}},
195 },
196 /* 15 fps */
197 {
198 {0, },
199 {9, 955, 1050, {0x33, 0xF4, 0x05, 0x20, 0xCF, 0x1F, 0x3F, 0x06, 0x48, 0xBB, 0x33, 0x10, 0x02}},
200 {4, 591, 650, {0x33, 0xF4, 0x15, 0x14, 0x44, 0x12, 0xB4, 0x08, 0x30, 0x4F, 0x4E, 0x10, 0x02}},
201 {3, 448, 492, {0x33, 0xF4, 0x25, 0x0F, 0x52, 0x0D, 0xC2, 0x09, 0x28, 0xC0, 0x69, 0x10, 0x02}},
202 },
203 /* 20 fps */
204 {
205 {0, },
206 {9, 958, 782, {0x32, 0xF4, 0x0D, 0x18, 0x6A, 0x16, 0xDA, 0x0B, 0x58, 0xBE, 0x33, 0xD0, 0x02}},
207 {5, 703, 574, {0x32, 0xF4, 0x1D, 0x11, 0xE7, 0x10, 0x57, 0x0B, 0x40, 0xBF, 0x42, 0xD0, 0x02}},
208 {3, 446, 364, {0x32, 0xF4, 0x3D, 0x0B, 0x5C, 0x09, 0xCC, 0x0E, 0x30, 0xBE, 0x69, 0xD0, 0x02}},
209 },
210 /* 25 fps */
211 {
212 {0, },
213 {9, 958, 654, {0x31, 0xF4, 0x15, 0x14, 0x66, 0x12, 0xD6, 0x0B, 0x50, 0xBE, 0x33, 0x90, 0x02}},
214 {6, 776, 530, {0x31, 0xF4, 0x25, 0x10, 0x8C, 0x0E, 0xFC, 0x0C, 0x48, 0x08, 0x3F, 0x90, 0x02}},
215 {4, 592, 404, {0x31, 0xF4, 0x35, 0x0C, 0x96, 0x0B, 0x06, 0x0B, 0x38, 0x50, 0x4E, 0x90, 0x02}},
216 },
217 /* 30 fps */
218 {
219 {0, },
220 {9, 957, 526, {0x30, 0xF4, 0x25, 0x10, 0x68, 0x0E, 0xD8, 0x0D, 0x58, 0xBD, 0x33, 0x60, 0x02}},
221 {6, 775, 426, {0x30, 0xF4, 0x35, 0x0D, 0x48, 0x0B, 0xB8, 0x0F, 0x50, 0x07, 0x3F, 0x60, 0x02}},
222 {4, 590, 324, {0x30, 0x7A, 0x4B, 0x0A, 0x1C, 0x08, 0xB4, 0x0E, 0x40, 0x4E, 0x52, 0x60, 0x02}},
223 },
224 },
225 /* CIF */
226 {
227 /* 5 fps */
228 {
229 {6, 771, 0, {0x15, 0xF4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x3F, 0x80, 0x02}},
230 {4, 465, 1278, {0x15, 0xF4, 0x05, 0x27, 0xEE, 0x26, 0x36, 0x03, 0x18, 0xD1, 0x65, 0x80, 0x02}},
231 {2, 291, 800, {0x15, 0xF4, 0x15, 0x18, 0xF4, 0x17, 0x3C, 0x05, 0x18, 0x23, 0xA1, 0x80, 0x02}},
232 {1, 193, 528, {0x15, 0xF4, 0x2D, 0x10, 0x7E, 0x0E, 0xC6, 0x0A, 0x18, 0xC1, 0xF4, 0x80, 0x02}},
233 },
234 /* 10 fps */
235 {
236 {0, },
237 {9, 932, 1278, {0x14, 0xF4, 0x05, 0x27, 0xEE, 0x26, 0x36, 0x04, 0x30, 0xA4, 0x33, 0x10, 0x02}},
238 {4, 591, 812, {0x14, 0xF4, 0x15, 0x19, 0x56, 0x17, 0x9E, 0x06, 0x28, 0x4F, 0x4E, 0x10, 0x02}},
239 {2, 291, 400, {0x14, 0xF4, 0x3D, 0x0C, 0x7A, 0x0A, 0xC2, 0x0E, 0x28, 0x23, 0xA1, 0x10, 0x02}},
240 },
241 /* 15 fps */
242 {
243 {0, },
244 {9, 956, 876, {0x13, 0xF4, 0x0D, 0x1B, 0x58, 0x19, 0xA0, 0x05, 0x38, 0xBC, 0x33, 0x60, 0x02}},
245 {5, 703, 644, {0x13, 0xF4, 0x1D, 0x14, 0x1C, 0x12, 0x64, 0x08, 0x38, 0xBF, 0x42, 0x60, 0x02}},
246 {3, 448, 410, {0x13, 0xF4, 0x3D, 0x0C, 0xC4, 0x0B, 0x0C, 0x0E, 0x38, 0xC0, 0x69, 0x60, 0x02}},
247 },
248 /* 20 fps */
249 {
250 {0, },
251 {9, 956, 650, {0x12, 0xF4, 0x1D, 0x14, 0x4A, 0x12, 0x92, 0x09, 0x48, 0xBC, 0x33, 0x10, 0x03}},
252 {6, 776, 528, {0x12, 0xF4, 0x2D, 0x10, 0x7E, 0x0E, 0xC6, 0x0A, 0x40, 0x08, 0x3F, 0x10, 0x03}},
253 {4, 591, 402, {0x12, 0xF4, 0x3D, 0x0C, 0x8F, 0x0A, 0xD7, 0x0E, 0x40, 0x4F, 0x4E, 0x10, 0x03}},
254 },
255 /* 25 fps */
256 {
257 {0, },
258 {9, 956, 544, {0x11, 0xF4, 0x25, 0x10, 0xF4, 0x0F, 0x3C, 0x0A, 0x48, 0xBC, 0x33, 0xC0, 0x02}},
259 {7, 840, 478, {0x11, 0xF4, 0x2D, 0x0E, 0xEB, 0x0D, 0x33, 0x0B, 0x48, 0x48, 0x3B, 0xC0, 0x02}},
260 {5, 703, 400, {0x11, 0xF4, 0x3D, 0x0C, 0x7A, 0x0A, 0xC2, 0x0E, 0x48, 0xBF, 0x42, 0xC0, 0x02}},
261 },
262 /* 30 fps */
263 {
264 {0, },
265 {9, 956, 438, {0x10, 0xF4, 0x35, 0x0D, 0xAC, 0x0B, 0xF4, 0x0D, 0x50, 0xBC, 0x33, 0x10, 0x02}},
266 {7, 838, 384, {0x10, 0xF4, 0x45, 0x0B, 0xFD, 0x0A, 0x45, 0x0F, 0x50, 0x46, 0x3B, 0x10, 0x02}},
267 {6, 773, 354, {0x10, 0x7A, 0x4B, 0x0B, 0x0C, 0x09, 0x80, 0x10, 0x50, 0x05, 0x3F, 0x10, 0x02}},
268 },
269 },
270 /* VGA */
271 {
272 /* 5 fps */
273 {
274 {0, },
275 {6, 773, 1272, {0x1D, 0xF4, 0x15, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x3F, 0x10, 0x02}},
276 {4, 592, 976, {0x1D, 0xF4, 0x25, 0x1E, 0x78, 0x1B, 0x58, 0x03, 0x30, 0x50, 0x4E, 0x10, 0x02}},
277 {3, 448, 738, {0x1D, 0xF4, 0x3D, 0x17, 0x0C, 0x13, 0xEC, 0x04, 0x30, 0xC0, 0x69, 0x10, 0x02}},
278 },
279 /* 10 fps */
280 {
281 {0, },
282 {9, 956, 788, {0x1C, 0xF4, 0x35, 0x18, 0x9C, 0x15, 0x7C, 0x03, 0x48, 0xBC, 0x33, 0x10, 0x02}},
283 {6, 776, 640, {0x1C, 0x7A, 0x53, 0x13, 0xFC, 0x11, 0x2C, 0x04, 0x48, 0x08, 0x3F, 0x10, 0x02}},
284 {4, 592, 488, {0x1C, 0x7A, 0x6B, 0x0F, 0x3C, 0x0C, 0x6C, 0x06, 0x48, 0x50, 0x4E, 0x10, 0x02}},
285 },
286 /* 15 fps */
287 {
288 {0, },
289 {9, 957, 526, {0x1B, 0x7A, 0x63, 0x10, 0x68, 0x0D, 0x98, 0x06, 0x58, 0xBD, 0x33, 0x80, 0x02}},
290 {9, 957, 526, {0x1B, 0x7A, 0x63, 0x10, 0x68, 0x0D, 0x98, 0x06, 0x58, 0xBD, 0x33, 0x80, 0x02}},
291 {8, 895, 492, {0x1B, 0x7A, 0x6B, 0x0F, 0x5D, 0x0C, 0x8D, 0x06, 0x58, 0x7F, 0x37, 0x80, 0x02}},
292 },
293 /* 20 fps */
294 {
295 {0, },
296 {0, },
297 {0, },
298 {0, },
299 },
300 /* 25 fps */
301 {
302 {0, },
303 {0, },
304 {0, },
305 {0, },
306 },
307 /* 30 fps */
308 {
309 {0, },
310 {0, },
311 {0, },
312 {0, },
313 },
314 },
315};
316
diff --git a/drivers/usb/media/pwc/pwc-timon.h b/drivers/usb/media/pwc/pwc-timon.h
deleted file mode 100644
index a86b3782a081..000000000000
--- a/drivers/usb/media/pwc/pwc-timon.h
+++ /dev/null
@@ -1,61 +0,0 @@
1/* Linux driver for Philips webcam
2 (C) 2004 Luc Saillard (luc@saillard.org)
3
4 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
5 driver and thus may have bugs that are not present in the original version.
6 Please send bug reports and support requests to <luc@saillard.org>.
7 The decompression routines have been implemented by reverse-engineering the
8 Nemosoft binary pwcx module. Caveat emptor.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25
26
27/* This tables contains entries for the 675/680/690 (Timon) camera, with
28 4 different qualities (no compression, low, medium, high).
29 It lists the bandwidth requirements for said mode by its alternate interface
30 number. An alternate of 0 means that the mode is unavailable.
31
32 There are 6 * 4 * 4 entries:
33 6 different resolutions subqcif, qsif, qcif, sif, cif, vga
34 6 framerates: 5, 10, 15, 20, 25, 30
35 4 compression modi: none, low, medium, high
36
37 When an uncompressed mode is not available, the next available compressed mode
38 will be chosen (unless the decompressor is absent). Sometimes there are only
39 1 or 2 compressed modes available; in that case entries are duplicated.
40*/
41
42#ifndef PWC_TIMON_H
43#define PWC_TIMON_H
44
45#include "pwc-ioctl.h"
46
47struct Timon_table_entry
48{
49 char alternate; /* USB alternate interface */
50 unsigned short packetsize; /* Normal packet size */
51 unsigned short bandlength; /* Bandlength when decompressing */
52 unsigned char mode[13]; /* precomputed mode settings for cam */
53};
54
55const extern struct Timon_table_entry Timon_table[PSZ_MAX][6][4];
56const extern unsigned int TimonRomTable [16][2][16][8];
57
58
59#endif
60
61
diff --git a/drivers/usb/media/pwc/pwc-uncompress.c b/drivers/usb/media/pwc/pwc-uncompress.c
deleted file mode 100644
index ef4204eab6c4..000000000000
--- a/drivers/usb/media/pwc/pwc-uncompress.c
+++ /dev/null
@@ -1,146 +0,0 @@
1/* Linux driver for Philips webcam
2 Decompression frontend.
3 (C) 1999-2003 Nemosoft Unv.
4 (C) 2004 Luc Saillard (luc@saillard.org)
5
6 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
7 driver and thus may have bugs that are not present in the original version.
8 Please send bug reports and support requests to <luc@saillard.org>.
9 The decompression routines have been implemented by reverse-engineering the
10 Nemosoft binary pwcx module. Caveat emptor.
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#include <asm/current.h>
28#include <asm/types.h>
29
30#include "pwc.h"
31#include "pwc-uncompress.h"
32
33int pwc_decompress(struct pwc_device *pdev)
34{
35 struct pwc_frame_buf *fbuf;
36 int n, line, col, stride;
37 void *yuv, *image;
38 u16 *src;
39 u16 *dsty, *dstu, *dstv;
40
41 if (pdev == NULL)
42 return -EFAULT;
43#if defined(__KERNEL__) && defined(PWC_MAGIC)
44 if (pdev->magic != PWC_MAGIC) {
45 Err("pwc_decompress(): magic failed.\n");
46 return -EFAULT;
47 }
48#endif
49
50 fbuf = pdev->read_frame;
51 if (fbuf == NULL)
52 return -EFAULT;
53 image = pdev->image_ptr[pdev->fill_image];
54 if (!image)
55 return -EFAULT;
56
57 yuv = fbuf->data + pdev->frame_header_size; /* Skip header */
58
59 /* Raw format; that's easy... */
60 if (pdev->vpalette == VIDEO_PALETTE_RAW)
61 {
62 memcpy(image, yuv, pdev->frame_size);
63 return 0;
64 }
65
66 if (pdev->vbandlength == 0) {
67 /* Uncompressed mode. We copy the data into the output buffer,
68 using the viewport size (which may be larger than the image
69 size). Unfortunately we have to do a bit of byte stuffing
70 to get the desired output format/size.
71 */
72 /*
73 * We do some byte shuffling here to go from the
74 * native format to YUV420P.
75 */
76 src = (u16 *)yuv;
77 n = pdev->view.x * pdev->view.y;
78
79 /* offset in Y plane */
80 stride = pdev->view.x * pdev->offset.y + pdev->offset.x;
81 dsty = (u16 *)(image + stride);
82
83 /* offsets in U/V planes */
84 stride = pdev->view.x * pdev->offset.y / 4 + pdev->offset.x / 2;
85 dstu = (u16 *)(image + n + stride);
86 dstv = (u16 *)(image + n + n / 4 + stride);
87
88 /* increment after each line */
89 stride = (pdev->view.x - pdev->image.x) / 2; /* u16 is 2 bytes */
90
91 for (line = 0; line < pdev->image.y; line++) {
92 for (col = 0; col < pdev->image.x; col += 4) {
93 *dsty++ = *src++;
94 *dsty++ = *src++;
95 if (line & 1)
96 *dstv++ = *src++;
97 else
98 *dstu++ = *src++;
99 }
100 dsty += stride;
101 if (line & 1)
102 dstv += (stride >> 1);
103 else
104 dstu += (stride >> 1);
105 }
106 }
107 else {
108 /* Compressed; the decompressor routines will write the data
109 in planar format immediately.
110 */
111 int flags;
112
113 flags = PWCX_FLAG_PLANAR;
114 if (pdev->vsize == PSZ_VGA && pdev->vframes == 5 && pdev->vsnapshot)
115 {
116 printk(KERN_ERR "pwc: Mode Bayer is not supported for now\n");
117 flags |= PWCX_FLAG_BAYER;
118 return -ENXIO; /* No such device or address: missing decompressor */
119 }
120
121#if 0
122 switch (pdev->type)
123 {
124 case 675:
125 case 680:
126 case 690:
127 case 720:
128 case 730:
129 case 740:
130 case 750:
131 pwc_dec23_decompress(&pdev->image, &pdev->view,
132 &pdev->offset, yuv, image, flags,
133 pdev->decompress_data, pdev->vbandlength);
134 break;
135 case 645:
136 case 646:
137 /* TODO & FIXME */
138 return -ENXIO; /* Missing decompressor */
139 break;
140 }
141#endif
142 }
143 return 0;
144}
145
146
diff --git a/drivers/usb/media/pwc/pwc-uncompress.h b/drivers/usb/media/pwc/pwc-uncompress.h
deleted file mode 100644
index d3b9250e4ed3..000000000000
--- a/drivers/usb/media/pwc/pwc-uncompress.h
+++ /dev/null
@@ -1,41 +0,0 @@
1/* (C) 1999-2003 Nemosoft Unv.
2 (C) 2004 Luc Saillard (luc@saillard.org)
3
4 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
5 driver and thus may have bugs that are not present in the original version.
6 Please send bug reports and support requests to <luc@saillard.org>.
7 The decompression routines have been implemented by reverse-engineering the
8 Nemosoft binary pwcx module. Caveat emptor.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25/* This file is the bridge between the kernel module and the plugin; it
26 describes the structures and datatypes used in both modules. Any
27 significant change should be reflected by increasing the
28 pwc_decompressor_version major number.
29 */
30#ifndef PWC_UNCOMPRESS_H
31#define PWC_UNCOMPRESS_H
32
33#include <linux/config.h>
34
35#include "pwc-ioctl.h"
36
37/* from pwc-dec.h */
38#define PWCX_FLAG_PLANAR 0x0001
39/* */
40
41#endif
diff --git a/drivers/usb/media/pwc/pwc.h b/drivers/usb/media/pwc/pwc.h
deleted file mode 100644
index 6dd76bb3dff1..000000000000
--- a/drivers/usb/media/pwc/pwc.h
+++ /dev/null
@@ -1,272 +0,0 @@
1/* (C) 1999-2003 Nemosoft Unv.
2 (C) 2004 Luc Saillard (luc@saillard.org)
3
4 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
5 driver and thus may have bugs that are not present in the original version.
6 Please send bug reports and support requests to <luc@saillard.org>.
7 The decompression routines have been implemented by reverse-engineering the
8 Nemosoft binary pwcx module. Caveat emptor.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25#ifndef PWC_H
26#define PWC_H
27
28#include <linux/config.h>
29#include <linux/module.h>
30#include <linux/usb.h>
31#include <linux/spinlock.h>
32#include <linux/videodev.h>
33#include <linux/wait.h>
34#include <linux/smp_lock.h>
35#include <asm/semaphore.h>
36#include <asm/errno.h>
37
38#include "pwc-uncompress.h"
39#include "pwc-ioctl.h"
40
41/* Defines and structures for the Philips webcam */
42/* Used for checking memory corruption/pointer validation */
43#define PWC_MAGIC 0x89DC10ABUL
44#undef PWC_MAGIC
45
46/* Turn some debugging options on/off */
47#define PWC_DEBUG 0
48
49/* Trace certain actions in the driver */
50#define TRACE_MODULE 0x0001
51#define TRACE_PROBE 0x0002
52#define TRACE_OPEN 0x0004
53#define TRACE_READ 0x0008
54#define TRACE_MEMORY 0x0010
55#define TRACE_FLOW 0x0020
56#define TRACE_SIZE 0x0040
57#define TRACE_PWCX 0x0080
58#define TRACE_SEQUENCE 0x1000
59
60#define Trace(R, A...) if (pwc_trace & R) printk(KERN_DEBUG PWC_NAME " " A)
61#define Debug(A...) printk(KERN_DEBUG PWC_NAME " " A)
62#define Info(A...) printk(KERN_INFO PWC_NAME " " A)
63#define Err(A...) printk(KERN_ERR PWC_NAME " " A)
64
65
66/* Defines for ToUCam cameras */
67#define TOUCAM_HEADER_SIZE 8
68#define TOUCAM_TRAILER_SIZE 4
69
70#define FEATURE_MOTOR_PANTILT 0x0001
71
72/* Version block */
73#define PWC_MAJOR 9
74#define PWC_MINOR 0
75#define PWC_VERSION "9.0.2-unofficial"
76#define PWC_NAME "pwc"
77
78/* Turn certain features on/off */
79#define PWC_INT_PIPE 0
80
81/* Ignore errors in the first N frames, to allow for startup delays */
82#define FRAME_LOWMARK 5
83
84/* Size and number of buffers for the ISO pipe. */
85#define MAX_ISO_BUFS 2
86#define ISO_FRAMES_PER_DESC 10
87#define ISO_MAX_FRAME_SIZE 960
88#define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
89
90/* Frame buffers: contains compressed or uncompressed video data. */
91#define MAX_FRAMES 5
92/* Maximum size after decompression is 640x480 YUV data, 1.5 * 640 * 480 */
93#define PWC_FRAME_SIZE (460800 + TOUCAM_HEADER_SIZE + TOUCAM_TRAILER_SIZE)
94
95/* Absolute maximum number of buffers available for mmap() */
96#define MAX_IMAGES 10
97
98/* The following structures were based on cpia.h. Why reinvent the wheel? :-) */
99struct pwc_iso_buf
100{
101 void *data;
102 int length;
103 int read;
104 struct urb *urb;
105};
106
107/* intermediate buffers with raw data from the USB cam */
108struct pwc_frame_buf
109{
110 void *data;
111 volatile int filled; /* number of bytes filled */
112 struct pwc_frame_buf *next; /* list */
113#if PWC_DEBUG
114 int sequence; /* Sequence number */
115#endif
116};
117
118struct pwc_device
119{
120 struct video_device *vdev;
121#ifdef PWC_MAGIC
122 int magic;
123#endif
124 /* Pointer to our usb_device */
125 struct usb_device *udev;
126
127 int type; /* type of cam (645, 646, 675, 680, 690, 720, 730, 740, 750) */
128 int release; /* release number */
129 int features; /* feature bits */
130 char serial[30]; /* serial number (string) */
131 int error_status; /* set when something goes wrong with the cam (unplugged, USB errors) */
132 int usb_init; /* set when the cam has been initialized over USB */
133
134 /*** Video data ***/
135 int vopen; /* flag */
136 int vendpoint; /* video isoc endpoint */
137 int vcinterface; /* video control interface */
138 int valternate; /* alternate interface needed */
139 int vframes, vsize; /* frames-per-second & size (see PSZ_*) */
140 int vpalette; /* palette: 420P, RAW or RGBBAYER */
141 int vframe_count; /* received frames */
142 int vframes_dumped; /* counter for dumped frames */
143 int vframes_error; /* frames received in error */
144 int vmax_packet_size; /* USB maxpacket size */
145 int vlast_packet_size; /* for frame synchronisation */
146 int visoc_errors; /* number of contiguous ISOC errors */
147 int vcompression; /* desired compression factor */
148 int vbandlength; /* compressed band length; 0 is uncompressed */
149 char vsnapshot; /* snapshot mode */
150 char vsync; /* used by isoc handler */
151 char vmirror; /* for ToUCaM series */
152
153 int cmd_len;
154 unsigned char cmd_buf[13];
155
156 /* The image acquisition requires 3 to 4 steps:
157 1. data is gathered in short packets from the USB controller
158 2. data is synchronized and packed into a frame buffer
159 3a. in case data is compressed, decompress it directly into image buffer
160 3b. in case data is uncompressed, copy into image buffer with viewport
161 4. data is transferred to the user process
162
163 Note that MAX_ISO_BUFS != MAX_FRAMES != MAX_IMAGES....
164 We have in effect a back-to-back-double-buffer system.
165 */
166 /* 1: isoc */
167 struct pwc_iso_buf sbuf[MAX_ISO_BUFS];
168 char iso_init;
169
170 /* 2: frame */
171 struct pwc_frame_buf *fbuf; /* all frames */
172 struct pwc_frame_buf *empty_frames, *empty_frames_tail; /* all empty frames */
173 struct pwc_frame_buf *full_frames, *full_frames_tail; /* all filled frames */
174 struct pwc_frame_buf *fill_frame; /* frame currently being filled */
175 struct pwc_frame_buf *read_frame; /* frame currently read by user process */
176 int frame_header_size, frame_trailer_size;
177 int frame_size;
178 int frame_total_size; /* including header & trailer */
179 int drop_frames;
180#if PWC_DEBUG
181 int sequence; /* Debugging aid */
182#endif
183
184 /* 3: decompression */
185 struct pwc_decompressor *decompressor; /* function block with decompression routines */
186 void *decompress_data; /* private data for decompression engine */
187
188 /* 4: image */
189 /* We have an 'image' and a 'view', where 'image' is the fixed-size image
190 as delivered by the camera, and 'view' is the size requested by the
191 program. The camera image is centered in this viewport, laced with
192 a gray or black border. view_min <= image <= view <= view_max;
193 */
194 int image_mask; /* bitmask of supported sizes */
195 struct pwc_coord view_min, view_max; /* minimum and maximum viewable sizes */
196 struct pwc_coord abs_max; /* maximum supported size with compression */
197 struct pwc_coord image, view; /* image and viewport size */
198 struct pwc_coord offset; /* offset within the viewport */
199
200 void *image_data; /* total buffer, which is subdivided into ... */
201 void *image_ptr[MAX_IMAGES]; /* ...several images... */
202 int fill_image; /* ...which are rotated. */
203 int len_per_image; /* length per image */
204 int image_read_pos; /* In case we read data in pieces, keep track of were we are in the imagebuffer */
205 int image_used[MAX_IMAGES]; /* For MCAPTURE and SYNC */
206
207 struct semaphore modlock; /* to prevent races in video_open(), etc */
208 spinlock_t ptrlock; /* for manipulating the buffer pointers */
209
210 /*** motorized pan/tilt feature */
211 struct pwc_mpt_range angle_range;
212 int pan_angle; /* in degrees * 100 */
213 int tilt_angle; /* absolute angle; 0,0 is home position */
214
215 /*** Misc. data ***/
216 wait_queue_head_t frameq; /* When waiting for a frame to finish... */
217#if PWC_INT_PIPE
218 void *usb_int_handler; /* for the interrupt endpoint */
219#endif
220};
221
222
223#ifdef __cplusplus
224extern "C" {
225#endif
226
227/* Global variable */
228extern int pwc_trace;
229
230/** functions in pwc-if.c */
231int pwc_try_video_mode(struct pwc_device *pdev, int width, int height, int new_fps, int new_compression, int new_snapshot);
232
233/** Functions in pwc-misc.c */
234/* sizes in pixels */
235extern struct pwc_coord pwc_image_sizes[PSZ_MAX];
236
237int pwc_decode_size(struct pwc_device *pdev, int width, int height);
238void pwc_construct(struct pwc_device *pdev);
239
240/** Functions in pwc-ctrl.c */
241/* Request a certain video mode. Returns < 0 if not possible */
242extern int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot);
243
244/* Various controls; should be obvious. Value 0..65535, or < 0 on error */
245extern int pwc_get_brightness(struct pwc_device *pdev);
246extern int pwc_set_brightness(struct pwc_device *pdev, int value);
247extern int pwc_get_contrast(struct pwc_device *pdev);
248extern int pwc_set_contrast(struct pwc_device *pdev, int value);
249extern int pwc_get_gamma(struct pwc_device *pdev);
250extern int pwc_set_gamma(struct pwc_device *pdev, int value);
251extern int pwc_get_saturation(struct pwc_device *pdev);
252extern int pwc_set_saturation(struct pwc_device *pdev, int value);
253extern int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value);
254extern int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor);
255
256/* Power down or up the camera; not supported by all models */
257extern int pwc_camera_power(struct pwc_device *pdev, int power);
258
259/* Private ioctl()s; see pwc-ioctl.h */
260extern int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg);
261
262
263/** pwc-uncompress.c */
264/* Expand frame to image, possibly including decompression. Uses read_frame and fill_image */
265extern int pwc_decompress(struct pwc_device *pdev);
266
267#ifdef __cplusplus
268}
269#endif
270
271
272#endif
diff --git a/drivers/usb/media/se401.c b/drivers/usb/media/se401.c
deleted file mode 100644
index 2ba562285fda..000000000000
--- a/drivers/usb/media/se401.c
+++ /dev/null
@@ -1,1437 +0,0 @@
1/*
2 * Endpoints (formerly known as AOX) se401 USB Camera Driver
3 *
4 * Copyright (c) 2000 Jeroen B. Vreeken (pe1rxq@amsat.org)
5 *
6 * Still somewhat based on the Linux ov511 driver.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * 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 Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 *
23 * Thanks to Endpoints Inc. (www.endpoints.com) for making documentation on
24 * their chipset available and supporting me while writing this driver.
25 * - Jeroen Vreeken
26 */
27
28static const char version[] = "0.24";
29
30#include <linux/config.h>
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/vmalloc.h>
34#include <linux/slab.h>
35#include <linux/pagemap.h>
36#include <linux/usb.h>
37#include "se401.h"
38
39static int flickerless=0;
40static int video_nr = -1;
41
42static struct usb_device_id device_table [] = {
43 { USB_DEVICE(0x03e8, 0x0004) },/* Endpoints/Aox SE401 */
44 { USB_DEVICE(0x0471, 0x030b) },/* Philips PCVC665K */
45 { USB_DEVICE(0x047d, 0x5001) },/* Kensington 67014 */
46 { USB_DEVICE(0x047d, 0x5002) },/* Kensington 6701(5/7) */
47 { USB_DEVICE(0x047d, 0x5003) },/* Kensington 67016 */
48 { }
49};
50
51MODULE_DEVICE_TABLE(usb, device_table);
52
53MODULE_AUTHOR("Jeroen Vreeken <pe1rxq@amsat.org>");
54MODULE_DESCRIPTION("SE401 USB Camera Driver");
55MODULE_LICENSE("GPL");
56module_param(flickerless, int, 0);
57MODULE_PARM_DESC(flickerless, "Net frequency to adjust exposure time to (0/50/60)");
58module_param(video_nr, int, 0);
59
60static struct usb_driver se401_driver;
61
62
63/**********************************************************************
64 *
65 * Memory management
66 *
67 **********************************************************************/
68static void *rvmalloc(unsigned long size)
69{
70 void *mem;
71 unsigned long adr;
72
73 size = PAGE_ALIGN(size);
74 mem = vmalloc_32(size);
75 if (!mem)
76 return NULL;
77
78 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
79 adr = (unsigned long) mem;
80 while (size > 0) {
81 SetPageReserved(vmalloc_to_page((void *)adr));
82 adr += PAGE_SIZE;
83 size -= PAGE_SIZE;
84 }
85
86 return mem;
87}
88
89static void rvfree(void *mem, unsigned long size)
90{
91 unsigned long adr;
92
93 if (!mem)
94 return;
95
96 adr = (unsigned long) mem;
97 while ((long) size > 0) {
98 ClearPageReserved(vmalloc_to_page((void *)adr));
99 adr += PAGE_SIZE;
100 size -= PAGE_SIZE;
101 }
102 vfree(mem);
103}
104
105
106
107/****************************************************************************
108 *
109 * se401 register read/write functions
110 *
111 ***************************************************************************/
112
113static int se401_sndctrl(int set, struct usb_se401 *se401, unsigned short req,
114 unsigned short value, unsigned char *cp, int size)
115{
116 return usb_control_msg (
117 se401->dev,
118 set ? usb_sndctrlpipe(se401->dev, 0) : usb_rcvctrlpipe(se401->dev, 0),
119 req,
120 (set ? USB_DIR_OUT : USB_DIR_IN) | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
121 value,
122 0,
123 cp,
124 size,
125 1000
126 );
127}
128
129static int se401_set_feature(struct usb_se401 *se401, unsigned short selector,
130 unsigned short param)
131{
132 /* specs say that the selector (address) should go in the value field
133 and the param in index, but in the logs of the windows driver they do
134 this the other way around...
135 */
136 return usb_control_msg (
137 se401->dev,
138 usb_sndctrlpipe(se401->dev, 0),
139 SE401_REQ_SET_EXT_FEATURE,
140 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
141 param,
142 selector,
143 NULL,
144 0,
145 1000
146 );
147}
148
149static unsigned short se401_get_feature(struct usb_se401 *se401,
150 unsigned short selector)
151{
152 /* For 'set' the selecetor should be in index, not sure if the spec is
153 wrong here to....
154 */
155 unsigned char cp[2];
156 usb_control_msg (
157 se401->dev,
158 usb_rcvctrlpipe(se401->dev, 0),
159 SE401_REQ_GET_EXT_FEATURE,
160 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
161 0,
162 selector,
163 cp,
164 2,
165 1000
166 );
167 return cp[0]+cp[1]*256;
168}
169
170/****************************************************************************
171 *
172 * Camera control
173 *
174 ***************************************************************************/
175
176
177static int se401_send_pict(struct usb_se401 *se401)
178{
179 se401_set_feature(se401, HV7131_REG_TITL, se401->expose_l);/* integration time low */
180 se401_set_feature(se401, HV7131_REG_TITM, se401->expose_m);/* integration time mid */
181 se401_set_feature(se401, HV7131_REG_TITU, se401->expose_h);/* integration time mid */
182 se401_set_feature(se401, HV7131_REG_ARLV, se401->resetlevel);/* reset level value */
183 se401_set_feature(se401, HV7131_REG_ARCG, se401->rgain);/* red color gain */
184 se401_set_feature(se401, HV7131_REG_AGCG, se401->ggain);/* green color gain */
185 se401_set_feature(se401, HV7131_REG_ABCG, se401->bgain);/* blue color gain */
186
187 return 0;
188}
189
190static void se401_set_exposure(struct usb_se401 *se401, int brightness)
191{
192 int integration=brightness<<5;
193
194 if (flickerless==50) {
195 integration=integration-integration%106667;
196 }
197 if (flickerless==60) {
198 integration=integration-integration%88889;
199 }
200 se401->brightness=integration>>5;
201 se401->expose_h=(integration>>16)&0xff;
202 se401->expose_m=(integration>>8)&0xff;
203 se401->expose_l=integration&0xff;
204}
205
206static int se401_get_pict(struct usb_se401 *se401, struct video_picture *p)
207{
208 p->brightness=se401->brightness;
209 if (se401->enhance) {
210 p->whiteness=32768;
211 } else {
212 p->whiteness=0;
213 }
214 p->colour=65535;
215 p->contrast=65535;
216 p->hue=se401->rgain<<10;
217 p->palette=se401->palette;
218 p->depth=3; /* rgb24 */
219 return 0;
220}
221
222
223static int se401_set_pict(struct usb_se401 *se401, struct video_picture *p)
224{
225 if (p->palette != VIDEO_PALETTE_RGB24)
226 return 1;
227 se401->palette=p->palette;
228 if (p->hue!=se401->hue) {
229 se401->rgain= p->hue>>10;
230 se401->bgain= 0x40-(p->hue>>10);
231 se401->hue=p->hue;
232 }
233 if (p->brightness!=se401->brightness) {
234 se401_set_exposure(se401, p->brightness);
235 }
236 if (p->whiteness>=32768) {
237 se401->enhance=1;
238 } else {
239 se401->enhance=0;
240 }
241 se401_send_pict(se401);
242 se401_send_pict(se401);
243 return 0;
244}
245
246/*
247 Hyundai have some really nice docs about this and other sensor related
248 stuff on their homepage: www.hei.co.kr
249*/
250static void se401_auto_resetlevel(struct usb_se401 *se401)
251{
252 unsigned int ahrc, alrc;
253 int oldreset=se401->resetlevel;
254
255 /* For some reason this normally read-only register doesn't get reset
256 to zero after reading them just once...
257 */
258 se401_get_feature(se401, HV7131_REG_HIREFNOH);
259 se401_get_feature(se401, HV7131_REG_HIREFNOL);
260 se401_get_feature(se401, HV7131_REG_LOREFNOH);
261 se401_get_feature(se401, HV7131_REG_LOREFNOL);
262 ahrc=256*se401_get_feature(se401, HV7131_REG_HIREFNOH) +
263 se401_get_feature(se401, HV7131_REG_HIREFNOL);
264 alrc=256*se401_get_feature(se401, HV7131_REG_LOREFNOH) +
265 se401_get_feature(se401, HV7131_REG_LOREFNOL);
266
267 /* Not an exact science, but it seems to work pretty well... */
268 if (alrc > 10) {
269 while (alrc>=10 && se401->resetlevel < 63) {
270 se401->resetlevel++;
271 alrc /=2;
272 }
273 } else if (ahrc > 20) {
274 while (ahrc>=20 && se401->resetlevel > 0) {
275 se401->resetlevel--;
276 ahrc /=2;
277 }
278 }
279 if (se401->resetlevel!=oldreset)
280 se401_set_feature(se401, HV7131_REG_ARLV, se401->resetlevel);
281
282 return;
283}
284
285/* irq handler for snapshot button */
286static void se401_button_irq(struct urb *urb, struct pt_regs *regs)
287{
288 struct usb_se401 *se401 = urb->context;
289 int status;
290
291 if (!se401->dev) {
292 info("ohoh: device vapourished");
293 return;
294 }
295
296 switch (urb->status) {
297 case 0:
298 /* success */
299 break;
300 case -ECONNRESET:
301 case -ENOENT:
302 case -ESHUTDOWN:
303 /* this urb is terminated, clean up */
304 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
305 return;
306 default:
307 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
308 goto exit;
309 }
310
311 if (urb->actual_length >=2) {
312 if (se401->button)
313 se401->buttonpressed=1;
314 }
315exit:
316 status = usb_submit_urb (urb, GFP_ATOMIC);
317 if (status)
318 err ("%s - usb_submit_urb failed with result %d",
319 __FUNCTION__, status);
320}
321
322static void se401_video_irq(struct urb *urb, struct pt_regs *regs)
323{
324 struct usb_se401 *se401 = urb->context;
325 int length = urb->actual_length;
326
327 /* ohoh... */
328 if (!se401->streaming)
329 return;
330
331 if (!se401->dev) {
332 info ("ohoh: device vapourished");
333 return;
334 }
335
336 /* 0 sized packets happen if we are to fast, but sometimes the camera
337 keeps sending them forever...
338 */
339 if (length && !urb->status) {
340 se401->nullpackets=0;
341 switch(se401->scratch[se401->scratch_next].state) {
342 case BUFFER_READY:
343 case BUFFER_BUSY: {
344 se401->dropped++;
345 break;
346 }
347 case BUFFER_UNUSED: {
348 memcpy(se401->scratch[se401->scratch_next].data, (unsigned char *)urb->transfer_buffer, length);
349 se401->scratch[se401->scratch_next].state=BUFFER_READY;
350 se401->scratch[se401->scratch_next].offset=se401->bayeroffset;
351 se401->scratch[se401->scratch_next].length=length;
352 if (waitqueue_active(&se401->wq)) {
353 wake_up_interruptible(&se401->wq);
354 }
355 se401->scratch_overflow=0;
356 se401->scratch_next++;
357 if (se401->scratch_next>=SE401_NUMSCRATCH)
358 se401->scratch_next=0;
359 break;
360 }
361 }
362 se401->bayeroffset+=length;
363 if (se401->bayeroffset>=se401->cheight*se401->cwidth) {
364 se401->bayeroffset=0;
365 }
366 } else {
367 se401->nullpackets++;
368 if (se401->nullpackets > SE401_MAX_NULLPACKETS) {
369 if (waitqueue_active(&se401->wq)) {
370 wake_up_interruptible(&se401->wq);
371 }
372 }
373 }
374
375 /* Resubmit urb for new data */
376 urb->status=0;
377 urb->dev=se401->dev;
378 if(usb_submit_urb(urb, GFP_KERNEL))
379 info("urb burned down");
380 return;
381}
382
383static void se401_send_size(struct usb_se401 *se401, int width, int height)
384{
385 int i=0;
386 int mode=0x03; /* No compression */
387 int sendheight=height;
388 int sendwidth=width;
389
390 /* JangGu compression can only be used with the camera supported sizes,
391 but bayer seems to work with any size that fits on the sensor.
392 We check if we can use compression with the current size with either
393 4 or 16 times subcapturing, if not we use uncompressed bayer data
394 but this will result in cutouts of the maximum size....
395 */
396 while (i<se401->sizes && !(se401->width[i]==width && se401->height[i]==height))
397 i++;
398 while (i<se401->sizes) {
399 if (se401->width[i]==width*2 && se401->height[i]==height*2) {
400 sendheight=se401->height[i];
401 sendwidth=se401->width[i];
402 mode=0x40;
403 }
404 if (se401->width[i]==width*4 && se401->height[i]==height*4) {
405 sendheight=se401->height[i];
406 sendwidth=se401->width[i];
407 mode=0x42;
408 }
409 i++;
410 }
411
412 se401_sndctrl(1, se401, SE401_REQ_SET_WIDTH, sendwidth, NULL, 0);
413 se401_sndctrl(1, se401, SE401_REQ_SET_HEIGHT, sendheight, NULL, 0);
414 se401_set_feature(se401, SE401_OPERATINGMODE, mode);
415
416 if (mode==0x03) {
417 se401->format=FMT_BAYER;
418 } else {
419 se401->format=FMT_JANGGU;
420 }
421
422 return;
423}
424
425/*
426 In this function se401_send_pict is called several times,
427 for some reason (depending on the state of the sensor and the phase of
428 the moon :) doing this only in either place doesn't always work...
429*/
430static int se401_start_stream(struct usb_se401 *se401)
431{
432 struct urb *urb;
433 int err=0, i;
434 se401->streaming=1;
435
436 se401_sndctrl(1, se401, SE401_REQ_CAMERA_POWER, 1, NULL, 0);
437 se401_sndctrl(1, se401, SE401_REQ_LED_CONTROL, 1, NULL, 0);
438
439 /* Set picture settings */
440 se401_set_feature(se401, HV7131_REG_MODE_B, 0x05);/*windowed + pix intg */
441 se401_send_pict(se401);
442
443 se401_send_size(se401, se401->cwidth, se401->cheight);
444
445 se401_sndctrl(1, se401, SE401_REQ_START_CONTINUOUS_CAPTURE, 0, NULL, 0);
446
447 /* Do some memory allocation */
448 for (i=0; i<SE401_NUMFRAMES; i++) {
449 se401->frame[i].data=se401->fbuf + i * se401->maxframesize;
450 se401->frame[i].curpix=0;
451 }
452 for (i=0; i<SE401_NUMSBUF; i++) {
453 se401->sbuf[i].data=kmalloc(SE401_PACKETSIZE, GFP_KERNEL);
454 }
455
456 se401->bayeroffset=0;
457 se401->scratch_next=0;
458 se401->scratch_use=0;
459 se401->scratch_overflow=0;
460 for (i=0; i<SE401_NUMSCRATCH; i++) {
461 se401->scratch[i].data=kmalloc(SE401_PACKETSIZE, GFP_KERNEL);
462 se401->scratch[i].state=BUFFER_UNUSED;
463 }
464
465 for (i=0; i<SE401_NUMSBUF; i++) {
466 urb=usb_alloc_urb(0, GFP_KERNEL);
467 if(!urb)
468 return -ENOMEM;
469
470 usb_fill_bulk_urb(urb, se401->dev,
471 usb_rcvbulkpipe(se401->dev, SE401_VIDEO_ENDPOINT),
472 se401->sbuf[i].data, SE401_PACKETSIZE,
473 se401_video_irq,
474 se401);
475
476 se401->urb[i]=urb;
477
478 err=usb_submit_urb(se401->urb[i], GFP_KERNEL);
479 if(err)
480 err("urb burned down");
481 }
482
483 se401->framecount=0;
484
485 return 0;
486}
487
488static int se401_stop_stream(struct usb_se401 *se401)
489{
490 int i;
491
492 if (!se401->streaming || !se401->dev)
493 return 1;
494
495 se401->streaming=0;
496
497 se401_sndctrl(1, se401, SE401_REQ_STOP_CONTINUOUS_CAPTURE, 0, NULL, 0);
498
499 se401_sndctrl(1, se401, SE401_REQ_LED_CONTROL, 0, NULL, 0);
500 se401_sndctrl(1, se401, SE401_REQ_CAMERA_POWER, 0, NULL, 0);
501
502 for (i=0; i<SE401_NUMSBUF; i++) if (se401->urb[i]) {
503 usb_kill_urb(se401->urb[i]);
504 usb_free_urb(se401->urb[i]);
505 se401->urb[i]=NULL;
506 kfree(se401->sbuf[i].data);
507 }
508 for (i=0; i<SE401_NUMSCRATCH; i++) {
509 kfree(se401->scratch[i].data);
510 se401->scratch[i].data=NULL;
511 }
512
513 return 0;
514}
515
516static int se401_set_size(struct usb_se401 *se401, int width, int height)
517{
518 int wasstreaming=se401->streaming;
519 /* Check to see if we need to change */
520 if (se401->cwidth==width && se401->cheight==height)
521 return 0;
522
523 /* Check for a valid mode */
524 if (!width || !height)
525 return 1;
526 if ((width & 1) || (height & 1))
527 return 1;
528 if (width>se401->width[se401->sizes-1])
529 return 1;
530 if (height>se401->height[se401->sizes-1])
531 return 1;
532
533 /* Stop a current stream and start it again at the new size */
534 if (wasstreaming)
535 se401_stop_stream(se401);
536 se401->cwidth=width;
537 se401->cheight=height;
538 if (wasstreaming)
539 se401_start_stream(se401);
540 return 0;
541}
542
543
544/****************************************************************************
545 *
546 * Video Decoding
547 *
548 ***************************************************************************/
549
550/*
551 This shouldn't really be done in a v4l driver....
552 But it does make the image look a lot more usable.
553 Basically it lifts the dark pixels more than the light pixels.
554*/
555static inline void enhance_picture(unsigned char *frame, int len)
556{
557 while (len--) {
558 *frame=(((*frame^255)*(*frame^255))/255)^255;
559 frame++;
560 }
561}
562
563static inline void decode_JangGu_integrate(struct usb_se401 *se401, int data)
564{
565 struct se401_frame *frame=&se401->frame[se401->curframe];
566 int linelength=se401->cwidth*3;
567
568 if (frame->curlinepix >= linelength) {
569 frame->curlinepix=0;
570 frame->curline+=linelength;
571 }
572
573 /* First three are absolute, all others relative.
574 * Format is rgb from right to left (mirrorred image),
575 * we flip it to get bgr from left to right. */
576 if (frame->curlinepix < 3) {
577 *(frame->curline-frame->curlinepix)=1+data*4;
578 } else {
579 *(frame->curline-frame->curlinepix)=
580 *(frame->curline-frame->curlinepix+3)+data*4;
581 }
582 frame->curlinepix++;
583}
584
585static inline void decode_JangGu_vlc (struct usb_se401 *se401, unsigned char *data, int bit_exp, int packetlength)
586{
587 int pos=0;
588 int vlc_cod=0;
589 int vlc_size=0;
590 int vlc_data=0;
591 int bit_cur;
592 int bit;
593 data+=4;
594 while (pos < packetlength) {
595 bit_cur=8;
596 while (bit_cur && bit_exp) {
597 bit=((*data)>>(bit_cur-1))&1;
598 if (!vlc_cod) {
599 if (bit) {
600 vlc_size++;
601 } else {
602 if (!vlc_size) {
603 decode_JangGu_integrate(se401, 0);
604 } else {
605 vlc_cod=2;
606 vlc_data=0;
607 }
608 }
609 } else {
610 if (vlc_cod==2) {
611 if (!bit)
612 vlc_data = -(1<<vlc_size) + 1;
613 vlc_cod--;
614 }
615 vlc_size--;
616 vlc_data+=bit<<vlc_size;
617 if (!vlc_size) {
618 decode_JangGu_integrate(se401, vlc_data);
619 vlc_cod=0;
620 }
621 }
622 bit_cur--;
623 bit_exp--;
624 }
625 pos++;
626 data++;
627 }
628}
629
630static inline void decode_JangGu (struct usb_se401 *se401, struct se401_scratch *buffer)
631{
632 unsigned char *data=buffer->data;
633 int len=buffer->length;
634 int bit_exp=0, pix_exp=0, frameinfo=0, packetlength=0, size;
635 int datapos=0;
636
637 /* New image? */
638 if (!se401->frame[se401->curframe].curpix) {
639 se401->frame[se401->curframe].curlinepix=0;
640 se401->frame[se401->curframe].curline=
641 se401->frame[se401->curframe].data+
642 se401->cwidth*3-1;
643 if (se401->frame[se401->curframe].grabstate==FRAME_READY)
644 se401->frame[se401->curframe].grabstate=FRAME_GRABBING;
645 se401->vlcdatapos=0;
646 }
647 while (datapos < len) {
648 size=1024-se401->vlcdatapos;
649 if (size+datapos > len)
650 size=len-datapos;
651 memcpy(se401->vlcdata+se401->vlcdatapos, data+datapos, size);
652 se401->vlcdatapos+=size;
653 packetlength=0;
654 if (se401->vlcdatapos >= 4) {
655 bit_exp=se401->vlcdata[3]+(se401->vlcdata[2]<<8);
656 pix_exp=se401->vlcdata[1]+((se401->vlcdata[0]&0x3f)<<8);
657 frameinfo=se401->vlcdata[0]&0xc0;
658 packetlength=((bit_exp+47)>>4)<<1;
659 if (packetlength > 1024) {
660 se401->vlcdatapos=0;
661 datapos=len;
662 packetlength=0;
663 se401->error++;
664 se401->frame[se401->curframe].curpix=0;
665 }
666 }
667 if (packetlength && se401->vlcdatapos >= packetlength) {
668 decode_JangGu_vlc(se401, se401->vlcdata, bit_exp, packetlength);
669 se401->frame[se401->curframe].curpix+=pix_exp*3;
670 datapos+=size-(se401->vlcdatapos-packetlength);
671 se401->vlcdatapos=0;
672 if (se401->frame[se401->curframe].curpix>=se401->cwidth*se401->cheight*3) {
673 if (se401->frame[se401->curframe].curpix==se401->cwidth*se401->cheight*3) {
674 if (se401->frame[se401->curframe].grabstate==FRAME_GRABBING) {
675 se401->frame[se401->curframe].grabstate=FRAME_DONE;
676 se401->framecount++;
677 se401->readcount++;
678 }
679 if (se401->frame[(se401->curframe+1)&(SE401_NUMFRAMES-1)].grabstate==FRAME_READY) {
680 se401->curframe=(se401->curframe+1) & (SE401_NUMFRAMES-1);
681 }
682 } else {
683 se401->error++;
684 }
685 se401->frame[se401->curframe].curpix=0;
686 datapos=len;
687 }
688 } else {
689 datapos+=size;
690 }
691 }
692}
693
694static inline void decode_bayer (struct usb_se401 *se401, struct se401_scratch *buffer)
695{
696 unsigned char *data=buffer->data;
697 int len=buffer->length;
698 int offset=buffer->offset;
699 int datasize=se401->cwidth*se401->cheight;
700 struct se401_frame *frame=&se401->frame[se401->curframe];
701
702 unsigned char *framedata=frame->data, *curline, *nextline;
703 int width=se401->cwidth;
704 int blineoffset=0, bline;
705 int linelength=width*3, i;
706
707
708 if (frame->curpix==0) {
709 if (frame->grabstate==FRAME_READY) {
710 frame->grabstate=FRAME_GRABBING;
711 }
712 frame->curline=framedata+linelength;
713 frame->curlinepix=0;
714 }
715
716 if (offset!=frame->curpix) {
717 /* Regard frame as lost :( */
718 frame->curpix=0;
719 se401->error++;
720 return;
721 }
722
723 /* Check if we have to much data */
724 if (frame->curpix+len > datasize) {
725 len=datasize-frame->curpix;
726 }
727 if (se401->cheight%4)
728 blineoffset=1;
729 bline=frame->curpix/se401->cwidth+blineoffset;
730
731 curline=frame->curline;
732 nextline=curline+linelength;
733 if (nextline >= framedata+datasize*3)
734 nextline=curline;
735 while (len) {
736 if (frame->curlinepix>=width) {
737 frame->curlinepix-=width;
738 bline=frame->curpix/width+blineoffset;
739 curline+=linelength*2;
740 nextline+=linelength*2;
741 if (curline >= framedata+datasize*3) {
742 frame->curlinepix++;
743 curline-=3;
744 nextline-=3;
745 len--;
746 data++;
747 frame->curpix++;
748 }
749 if (nextline >= framedata+datasize*3)
750 nextline=curline;
751 }
752 if ((bline&1)) {
753 if ((frame->curlinepix&1)) {
754 *(curline+2)=*data;
755 *(curline-1)=*data;
756 *(nextline+2)=*data;
757 *(nextline-1)=*data;
758 } else {
759 *(curline+1)=
760 (*(curline+1)+*data)/2;
761 *(curline-2)=
762 (*(curline-2)+*data)/2;
763 *(nextline+1)=*data;
764 *(nextline-2)=*data;
765 }
766 } else {
767 if ((frame->curlinepix&1)) {
768 *(curline+1)=
769 (*(curline+1)+*data)/2;
770 *(curline-2)=
771 (*(curline-2)+*data)/2;
772 *(nextline+1)=*data;
773 *(nextline-2)=*data;
774 } else {
775 *curline=*data;
776 *(curline-3)=*data;
777 *nextline=*data;
778 *(nextline-3)=*data;
779 }
780 }
781 frame->curlinepix++;
782 curline-=3;
783 nextline-=3;
784 len--;
785 data++;
786 frame->curpix++;
787 }
788 frame->curline=curline;
789
790 if (frame->curpix>=datasize) {
791 /* Fix the top line */
792 framedata+=linelength;
793 for (i=0; i<linelength; i++) {
794 framedata--;
795 *framedata=*(framedata+linelength);
796 }
797 /* Fix the left side (green is already present) */
798 for (i=0; i<se401->cheight; i++) {
799 *framedata=*(framedata+3);
800 *(framedata+1)=*(framedata+4);
801 *(framedata+2)=*(framedata+5);
802 framedata+=linelength;
803 }
804 frame->curpix=0;
805 frame->grabstate=FRAME_DONE;
806 se401->framecount++;
807 se401->readcount++;
808 if (se401->frame[(se401->curframe+1)&(SE401_NUMFRAMES-1)].grabstate==FRAME_READY) {
809 se401->curframe=(se401->curframe+1) & (SE401_NUMFRAMES-1);
810 }
811 }
812}
813
814static int se401_newframe(struct usb_se401 *se401, int framenr)
815{
816 DECLARE_WAITQUEUE(wait, current);
817 int errors=0;
818
819 while (se401->streaming &&
820 (se401->frame[framenr].grabstate==FRAME_READY ||
821 se401->frame[framenr].grabstate==FRAME_GRABBING) ) {
822 if(!se401->frame[framenr].curpix) {
823 errors++;
824 }
825 wait_interruptible(
826 se401->scratch[se401->scratch_use].state!=BUFFER_READY,
827 &se401->wq,
828 &wait
829 );
830 if (se401->nullpackets > SE401_MAX_NULLPACKETS) {
831 se401->nullpackets=0;
832 info("to many null length packets, restarting capture");
833 se401_stop_stream(se401);
834 se401_start_stream(se401);
835 } else {
836 if (se401->scratch[se401->scratch_use].state!=BUFFER_READY) {
837 se401->frame[framenr].grabstate=FRAME_ERROR;
838 return -EIO;
839 }
840 se401->scratch[se401->scratch_use].state=BUFFER_BUSY;
841 if (se401->format==FMT_JANGGU) {
842 decode_JangGu(se401, &se401->scratch[se401->scratch_use]);
843 } else {
844 decode_bayer(se401, &se401->scratch[se401->scratch_use]);
845 }
846 se401->scratch[se401->scratch_use].state=BUFFER_UNUSED;
847 se401->scratch_use++;
848 if (se401->scratch_use>=SE401_NUMSCRATCH)
849 se401->scratch_use=0;
850 if (errors > SE401_MAX_ERRORS) {
851 errors=0;
852 info("to much errors, restarting capture");
853 se401_stop_stream(se401);
854 se401_start_stream(se401);
855 }
856 }
857 }
858
859 if (se401->frame[framenr].grabstate==FRAME_DONE)
860 if (se401->enhance)
861 enhance_picture(se401->frame[framenr].data, se401->cheight*se401->cwidth*3);
862 return 0;
863}
864
865static void usb_se401_remove_disconnected (struct usb_se401 *se401)
866{
867 int i;
868
869 se401->dev = NULL;
870
871 for (i=0; i<SE401_NUMSBUF; i++)
872 if (se401->urb[i]) {
873 usb_kill_urb(se401->urb[i]);
874 usb_free_urb(se401->urb[i]);
875 se401->urb[i] = NULL;
876 kfree(se401->sbuf[i].data);
877 }
878 for (i=0; i<SE401_NUMSCRATCH; i++) {
879 kfree(se401->scratch[i].data);
880 }
881 if (se401->inturb) {
882 usb_kill_urb(se401->inturb);
883 usb_free_urb(se401->inturb);
884 }
885 info("%s disconnected", se401->camera_name);
886
887 /* Free the memory */
888 kfree(se401->width);
889 kfree(se401->height);
890 kfree(se401);
891}
892
893
894
895/****************************************************************************
896 *
897 * Video4Linux
898 *
899 ***************************************************************************/
900
901
902static int se401_open(struct inode *inode, struct file *file)
903{
904 struct video_device *dev = video_devdata(file);
905 struct usb_se401 *se401 = (struct usb_se401 *)dev;
906 int err = 0;
907
908 if (se401->user)
909 return -EBUSY;
910 se401->fbuf = rvmalloc(se401->maxframesize * SE401_NUMFRAMES);
911 if (se401->fbuf)
912 file->private_data = dev;
913 else
914 err = -ENOMEM;
915 se401->user = !err;
916
917 return err;
918}
919
920static int se401_close(struct inode *inode, struct file *file)
921{
922 struct video_device *dev = file->private_data;
923 struct usb_se401 *se401 = (struct usb_se401 *)dev;
924 int i;
925
926 rvfree(se401->fbuf, se401->maxframesize * SE401_NUMFRAMES);
927 if (se401->removed) {
928 usb_se401_remove_disconnected(se401);
929 info("device unregistered");
930 } else {
931 for (i=0; i<SE401_NUMFRAMES; i++)
932 se401->frame[i].grabstate=FRAME_UNUSED;
933 if (se401->streaming)
934 se401_stop_stream(se401);
935 se401->user=0;
936 }
937 file->private_data = NULL;
938 return 0;
939}
940
941static int se401_do_ioctl(struct inode *inode, struct file *file,
942 unsigned int cmd, void *arg)
943{
944 struct video_device *vdev = file->private_data;
945 struct usb_se401 *se401 = (struct usb_se401 *)vdev;
946
947 if (!se401->dev)
948 return -EIO;
949
950 switch (cmd) {
951 case VIDIOCGCAP:
952 {
953 struct video_capability *b = arg;
954 strcpy(b->name, se401->camera_name);
955 b->type = VID_TYPE_CAPTURE;
956 b->channels = 1;
957 b->audios = 0;
958 b->maxwidth = se401->width[se401->sizes-1];
959 b->maxheight = se401->height[se401->sizes-1];
960 b->minwidth = se401->width[0];
961 b->minheight = se401->height[0];
962 return 0;
963 }
964 case VIDIOCGCHAN:
965 {
966 struct video_channel *v = arg;
967
968 if (v->channel != 0)
969 return -EINVAL;
970 v->flags = 0;
971 v->tuners = 0;
972 v->type = VIDEO_TYPE_CAMERA;
973 strcpy(v->name, "Camera");
974 return 0;
975 }
976 case VIDIOCSCHAN:
977 {
978 struct video_channel *v = arg;
979
980 if (v->channel != 0)
981 return -EINVAL;
982 return 0;
983 }
984 case VIDIOCGPICT:
985 {
986 struct video_picture *p = arg;
987
988 se401_get_pict(se401, p);
989 return 0;
990 }
991 case VIDIOCSPICT:
992 {
993 struct video_picture *p = arg;
994
995 if (se401_set_pict(se401, p))
996 return -EINVAL;
997 return 0;
998 }
999 case VIDIOCSWIN:
1000 {
1001 struct video_window *vw = arg;
1002
1003 if (vw->flags)
1004 return -EINVAL;
1005 if (vw->clipcount)
1006 return -EINVAL;
1007 if (se401_set_size(se401, vw->width, vw->height))
1008 return -EINVAL;
1009 return 0;
1010 }
1011 case VIDIOCGWIN:
1012 {
1013 struct video_window *vw = arg;
1014
1015 vw->x = 0; /* FIXME */
1016 vw->y = 0;
1017 vw->chromakey = 0;
1018 vw->flags = 0;
1019 vw->clipcount = 0;
1020 vw->width = se401->cwidth;
1021 vw->height = se401->cheight;
1022 return 0;
1023 }
1024 case VIDIOCGMBUF:
1025 {
1026 struct video_mbuf *vm = arg;
1027 int i;
1028
1029 memset(vm, 0, sizeof(*vm));
1030 vm->size = SE401_NUMFRAMES * se401->maxframesize;
1031 vm->frames = SE401_NUMFRAMES;
1032 for (i=0; i<SE401_NUMFRAMES; i++)
1033 vm->offsets[i] = se401->maxframesize * i;
1034 return 0;
1035 }
1036 case VIDIOCMCAPTURE:
1037 {
1038 struct video_mmap *vm = arg;
1039
1040 if (vm->format != VIDEO_PALETTE_RGB24)
1041 return -EINVAL;
1042 if (vm->frame >= SE401_NUMFRAMES)
1043 return -EINVAL;
1044 if (se401->frame[vm->frame].grabstate != FRAME_UNUSED)
1045 return -EBUSY;
1046
1047 /* Is this according to the v4l spec??? */
1048 if (se401_set_size(se401, vm->width, vm->height))
1049 return -EINVAL;
1050 se401->frame[vm->frame].grabstate=FRAME_READY;
1051
1052 if (!se401->streaming)
1053 se401_start_stream(se401);
1054
1055 /* Set the picture properties */
1056 if (se401->framecount==0)
1057 se401_send_pict(se401);
1058 /* Calibrate the reset level after a few frames. */
1059 if (se401->framecount%20==1)
1060 se401_auto_resetlevel(se401);
1061
1062 return 0;
1063 }
1064 case VIDIOCSYNC:
1065 {
1066 int *frame = arg;
1067 int ret=0;
1068
1069 if(*frame <0 || *frame >= SE401_NUMFRAMES)
1070 return -EINVAL;
1071
1072 ret=se401_newframe(se401, *frame);
1073 se401->frame[*frame].grabstate=FRAME_UNUSED;
1074 return ret;
1075 }
1076 case VIDIOCGFBUF:
1077 {
1078 struct video_buffer *vb = arg;
1079
1080 memset(vb, 0, sizeof(*vb));
1081 return 0;
1082 }
1083 case VIDIOCKEY:
1084 return 0;
1085 case VIDIOCCAPTURE:
1086 return -EINVAL;
1087 case VIDIOCSFBUF:
1088 return -EINVAL;
1089 case VIDIOCGTUNER:
1090 case VIDIOCSTUNER:
1091 return -EINVAL;
1092 case VIDIOCGFREQ:
1093 case VIDIOCSFREQ:
1094 return -EINVAL;
1095 case VIDIOCGAUDIO:
1096 case VIDIOCSAUDIO:
1097 return -EINVAL;
1098 default:
1099 return -ENOIOCTLCMD;
1100 } /* end switch */
1101
1102 return 0;
1103}
1104
1105static int se401_ioctl(struct inode *inode, struct file *file,
1106 unsigned int cmd, unsigned long arg)
1107{
1108 return video_usercopy(inode, file, cmd, arg, se401_do_ioctl);
1109}
1110
1111static ssize_t se401_read(struct file *file, char __user *buf,
1112 size_t count, loff_t *ppos)
1113{
1114 int realcount=count, ret=0;
1115 struct video_device *dev = file->private_data;
1116 struct usb_se401 *se401 = (struct usb_se401 *)dev;
1117
1118
1119 if (se401->dev == NULL)
1120 return -EIO;
1121 if (realcount > se401->cwidth*se401->cheight*3)
1122 realcount=se401->cwidth*se401->cheight*3;
1123
1124 /* Shouldn't happen: */
1125 if (se401->frame[0].grabstate==FRAME_GRABBING)
1126 return -EBUSY;
1127 se401->frame[0].grabstate=FRAME_READY;
1128 se401->frame[1].grabstate=FRAME_UNUSED;
1129 se401->curframe=0;
1130
1131 if (!se401->streaming)
1132 se401_start_stream(se401);
1133
1134 /* Set the picture properties */
1135 if (se401->framecount==0)
1136 se401_send_pict(se401);
1137 /* Calibrate the reset level after a few frames. */
1138 if (se401->framecount%20==1)
1139 se401_auto_resetlevel(se401);
1140
1141 ret=se401_newframe(se401, 0);
1142
1143 se401->frame[0].grabstate=FRAME_UNUSED;
1144 if (ret)
1145 return ret;
1146 if (copy_to_user(buf, se401->frame[0].data, realcount))
1147 return -EFAULT;
1148
1149 return realcount;
1150}
1151
1152static int se401_mmap(struct file *file, struct vm_area_struct *vma)
1153{
1154 struct video_device *dev = file->private_data;
1155 struct usb_se401 *se401 = (struct usb_se401 *)dev;
1156 unsigned long start = vma->vm_start;
1157 unsigned long size = vma->vm_end-vma->vm_start;
1158 unsigned long page, pos;
1159
1160 down(&se401->lock);
1161
1162 if (se401->dev == NULL) {
1163 up(&se401->lock);
1164 return -EIO;
1165 }
1166 if (size > (((SE401_NUMFRAMES * se401->maxframesize) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))) {
1167 up(&se401->lock);
1168 return -EINVAL;
1169 }
1170 pos = (unsigned long)se401->fbuf;
1171 while (size > 0) {
1172 page = vmalloc_to_pfn((void *)pos);
1173 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
1174 up(&se401->lock);
1175 return -EAGAIN;
1176 }
1177 start += PAGE_SIZE;
1178 pos += PAGE_SIZE;
1179 if (size > PAGE_SIZE)
1180 size -= PAGE_SIZE;
1181 else
1182 size = 0;
1183 }
1184 up(&se401->lock);
1185
1186 return 0;
1187}
1188
1189static struct file_operations se401_fops = {
1190 .owner = THIS_MODULE,
1191 .open = se401_open,
1192 .release = se401_close,
1193 .read = se401_read,
1194 .mmap = se401_mmap,
1195 .ioctl = se401_ioctl,
1196 .compat_ioctl = v4l_compat_ioctl32,
1197 .llseek = no_llseek,
1198};
1199static struct video_device se401_template = {
1200 .owner = THIS_MODULE,
1201 .name = "se401 USB camera",
1202 .type = VID_TYPE_CAPTURE,
1203 .hardware = VID_HARDWARE_SE401,
1204 .fops = &se401_fops,
1205};
1206
1207
1208
1209/***************************/
1210static int se401_init(struct usb_se401 *se401, int button)
1211{
1212 int i=0, rc;
1213 unsigned char cp[0x40];
1214 char temp[200];
1215
1216 /* led on */
1217 se401_sndctrl(1, se401, SE401_REQ_LED_CONTROL, 1, NULL, 0);
1218
1219 /* get camera descriptor */
1220 rc=se401_sndctrl(0, se401, SE401_REQ_GET_CAMERA_DESCRIPTOR, 0, cp, sizeof(cp));
1221 if (cp[1]!=0x41) {
1222 err("Wrong descriptor type");
1223 return 1;
1224 }
1225 sprintf (temp, "ExtraFeatures: %d", cp[3]);
1226
1227 se401->sizes=cp[4]+cp[5]*256;
1228 se401->width=kmalloc(se401->sizes*sizeof(int), GFP_KERNEL);
1229 if (!se401->width)
1230 return 1;
1231 se401->height=kmalloc(se401->sizes*sizeof(int), GFP_KERNEL);
1232 if (!se401->height) {
1233 kfree(se401->width);
1234 return 1;
1235 }
1236 for (i=0; i<se401->sizes; i++) {
1237 se401->width[i]=cp[6+i*4+0]+cp[6+i*4+1]*256;
1238 se401->height[i]=cp[6+i*4+2]+cp[6+i*4+3]*256;
1239 }
1240 sprintf (temp, "%s Sizes:", temp);
1241 for (i=0; i<se401->sizes; i++) {
1242 sprintf(temp, "%s %dx%d", temp, se401->width[i], se401->height[i]);
1243 }
1244 info("%s", temp);
1245 se401->maxframesize=se401->width[se401->sizes-1]*se401->height[se401->sizes-1]*3;
1246
1247 rc=se401_sndctrl(0, se401, SE401_REQ_GET_WIDTH, 0, cp, sizeof(cp));
1248 se401->cwidth=cp[0]+cp[1]*256;
1249 rc=se401_sndctrl(0, se401, SE401_REQ_GET_HEIGHT, 0, cp, sizeof(cp));
1250 se401->cheight=cp[0]+cp[1]*256;
1251
1252 if (!cp[2] && SE401_FORMAT_BAYER) {
1253 err("Bayer format not supported!");
1254 return 1;
1255 }
1256 /* set output mode (BAYER) */
1257 se401_sndctrl(1, se401, SE401_REQ_SET_OUTPUT_MODE, SE401_FORMAT_BAYER, NULL, 0);
1258
1259 rc=se401_sndctrl(0, se401, SE401_REQ_GET_BRT, 0, cp, sizeof(cp));
1260 se401->brightness=cp[0]+cp[1]*256;
1261 /* some default values */
1262 se401->resetlevel=0x2d;
1263 se401->rgain=0x20;
1264 se401->ggain=0x20;
1265 se401->bgain=0x20;
1266 se401_set_exposure(se401, 20000);
1267 se401->palette=VIDEO_PALETTE_RGB24;
1268 se401->enhance=1;
1269 se401->dropped=0;
1270 se401->error=0;
1271 se401->framecount=0;
1272 se401->readcount=0;
1273
1274 /* Start interrupt transfers for snapshot button */
1275 if (button) {
1276 se401->inturb=usb_alloc_urb(0, GFP_KERNEL);
1277 if (!se401->inturb) {
1278 info("Allocation of inturb failed");
1279 return 1;
1280 }
1281 usb_fill_int_urb(se401->inturb, se401->dev,
1282 usb_rcvintpipe(se401->dev, SE401_BUTTON_ENDPOINT),
1283 &se401->button, sizeof(se401->button),
1284 se401_button_irq,
1285 se401,
1286 8
1287 );
1288 if (usb_submit_urb(se401->inturb, GFP_KERNEL)) {
1289 info("int urb burned down");
1290 return 1;
1291 }
1292 } else
1293 se401->inturb=NULL;
1294
1295 /* Flash the led */
1296 se401_sndctrl(1, se401, SE401_REQ_CAMERA_POWER, 1, NULL, 0);
1297 se401_sndctrl(1, se401, SE401_REQ_LED_CONTROL, 1, NULL, 0);
1298 se401_sndctrl(1, se401, SE401_REQ_CAMERA_POWER, 0, NULL, 0);
1299 se401_sndctrl(1, se401, SE401_REQ_LED_CONTROL, 0, NULL, 0);
1300
1301 return 0;
1302}
1303
1304static int se401_probe(struct usb_interface *intf,
1305 const struct usb_device_id *id)
1306{
1307 struct usb_device *dev = interface_to_usbdev(intf);
1308 struct usb_interface_descriptor *interface;
1309 struct usb_se401 *se401;
1310 char *camera_name=NULL;
1311 int button=1;
1312
1313 /* We don't handle multi-config cameras */
1314 if (dev->descriptor.bNumConfigurations != 1)
1315 return -ENODEV;
1316
1317 interface = &intf->cur_altsetting->desc;
1318
1319 /* Is it an se401? */
1320 if (le16_to_cpu(dev->descriptor.idVendor) == 0x03e8 &&
1321 le16_to_cpu(dev->descriptor.idProduct) == 0x0004) {
1322 camera_name="Endpoints/Aox SE401";
1323 } else if (le16_to_cpu(dev->descriptor.idVendor) == 0x0471 &&
1324 le16_to_cpu(dev->descriptor.idProduct) == 0x030b) {
1325 camera_name="Philips PCVC665K";
1326 } else if (le16_to_cpu(dev->descriptor.idVendor) == 0x047d &&
1327 le16_to_cpu(dev->descriptor.idProduct) == 0x5001) {
1328 camera_name="Kensington VideoCAM 67014";
1329 } else if (le16_to_cpu(dev->descriptor.idVendor) == 0x047d &&
1330 le16_to_cpu(dev->descriptor.idProduct) == 0x5002) {
1331 camera_name="Kensington VideoCAM 6701(5/7)";
1332 } else if (le16_to_cpu(dev->descriptor.idVendor) == 0x047d &&
1333 le16_to_cpu(dev->descriptor.idProduct) == 0x5003) {
1334 camera_name="Kensington VideoCAM 67016";
1335 button=0;
1336 } else
1337 return -ENODEV;
1338
1339 /* Checking vendor/product should be enough, but what the hell */
1340 if (interface->bInterfaceClass != 0x00)
1341 return -ENODEV;
1342 if (interface->bInterfaceSubClass != 0x00)
1343 return -ENODEV;
1344
1345 /* We found one */
1346 info("SE401 camera found: %s", camera_name);
1347
1348 if ((se401 = kmalloc(sizeof(*se401), GFP_KERNEL)) == NULL) {
1349 err("couldn't kmalloc se401 struct");
1350 return -ENOMEM;
1351 }
1352
1353 memset(se401, 0, sizeof(*se401));
1354
1355 se401->dev = dev;
1356 se401->iface = interface->bInterfaceNumber;
1357 se401->camera_name = camera_name;
1358
1359 info("firmware version: %02x", le16_to_cpu(dev->descriptor.bcdDevice) & 255);
1360
1361 if (se401_init(se401, button)) {
1362 kfree(se401);
1363 return -EIO;
1364 }
1365
1366 memcpy(&se401->vdev, &se401_template, sizeof(se401_template));
1367 memcpy(se401->vdev.name, se401->camera_name, strlen(se401->camera_name));
1368 init_waitqueue_head(&se401->wq);
1369 init_MUTEX(&se401->lock);
1370 wmb();
1371
1372 if (video_register_device(&se401->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
1373 kfree(se401);
1374 err("video_register_device failed");
1375 return -EIO;
1376 }
1377 info("registered new video device: video%d", se401->vdev.minor);
1378
1379 usb_set_intfdata (intf, se401);
1380 return 0;
1381}
1382
1383static void se401_disconnect(struct usb_interface *intf)
1384{
1385 struct usb_se401 *se401 = usb_get_intfdata (intf);
1386
1387 usb_set_intfdata (intf, NULL);
1388 if (se401) {
1389 video_unregister_device(&se401->vdev);
1390 if (!se401->user){
1391 usb_se401_remove_disconnected(se401);
1392 } else {
1393 se401->frame[0].grabstate = FRAME_ERROR;
1394 se401->frame[0].grabstate = FRAME_ERROR;
1395
1396 se401->streaming = 0;
1397
1398 wake_up_interruptible(&se401->wq);
1399 se401->removed = 1;
1400 }
1401 }
1402}
1403
1404static struct usb_driver se401_driver = {
1405 .name = "se401",
1406 .id_table = device_table,
1407 .probe = se401_probe,
1408 .disconnect = se401_disconnect,
1409};
1410
1411
1412
1413/****************************************************************************
1414 *
1415 * Module routines
1416 *
1417 ***************************************************************************/
1418
1419static int __init usb_se401_init(void)
1420{
1421 info("SE401 usb camera driver version %s registering", version);
1422 if (flickerless)
1423 if (flickerless!=50 && flickerless!=60) {
1424 info("Invallid flickerless value, use 0, 50 or 60.");
1425 return -1;
1426 }
1427 return usb_register(&se401_driver);
1428}
1429
1430static void __exit usb_se401_exit(void)
1431{
1432 usb_deregister(&se401_driver);
1433 info("SE401 driver deregistered");
1434}
1435
1436module_init(usb_se401_init);
1437module_exit(usb_se401_exit);
diff --git a/drivers/usb/media/se401.h b/drivers/usb/media/se401.h
deleted file mode 100644
index 2e5846f1eb20..000000000000
--- a/drivers/usb/media/se401.h
+++ /dev/null
@@ -1,233 +0,0 @@
1
2#ifndef __LINUX_se401_H
3#define __LINUX_se401_H
4
5#include <asm/uaccess.h>
6#include <linux/videodev.h>
7#include <linux/smp_lock.h>
8
9#define se401_DEBUG /* Turn on debug messages */
10
11#ifdef se401_DEBUG
12# define PDEBUG(level, fmt, args...) \
13if (debug >= level) info("[" __PRETTY_FUNCTION__ ":%d] " fmt, __LINE__ , ## args)
14#else
15# define PDEBUG(level, fmt, args...) do {} while(0)
16#endif
17
18/* An almost drop-in replacement for sleep_on_interruptible */
19#define wait_interruptible(test, queue, wait) \
20{ \
21 add_wait_queue(queue, wait); \
22 set_current_state(TASK_INTERRUPTIBLE); \
23 if (test) \
24 schedule(); \
25 remove_wait_queue(queue, wait); \
26 set_current_state(TASK_RUNNING); \
27 if (signal_pending(current)) \
28 break; \
29}
30
31#define SE401_REQ_GET_CAMERA_DESCRIPTOR 0x06
32#define SE401_REQ_START_CONTINUOUS_CAPTURE 0x41
33#define SE401_REQ_STOP_CONTINUOUS_CAPTURE 0x42
34#define SE401_REQ_CAPTURE_FRAME 0x43
35#define SE401_REQ_GET_BRT 0x44
36#define SE401_REQ_SET_BRT 0x45
37#define SE401_REQ_GET_WIDTH 0x4c
38#define SE401_REQ_SET_WIDTH 0x4d
39#define SE401_REQ_GET_HEIGHT 0x4e
40#define SE401_REQ_SET_HEIGHT 0x4f
41#define SE401_REQ_GET_OUTPUT_MODE 0x50
42#define SE401_REQ_SET_OUTPUT_MODE 0x51
43#define SE401_REQ_GET_EXT_FEATURE 0x52
44#define SE401_REQ_SET_EXT_FEATURE 0x53
45#define SE401_REQ_CAMERA_POWER 0x56
46#define SE401_REQ_LED_CONTROL 0x57
47#define SE401_REQ_BIOS 0xff
48
49#define SE401_BIOS_READ 0x07
50
51#define SE401_FORMAT_BAYER 0x40
52
53/* Hyundai hv7131b registers
54 7121 and 7141 should be the same (haven't really checked...) */
55/* Mode registers: */
56#define HV7131_REG_MODE_A 0x00
57#define HV7131_REG_MODE_B 0x01
58#define HV7131_REG_MODE_C 0x02
59/* Frame registers: */
60#define HV7131_REG_FRSU 0x10
61#define HV7131_REG_FRSL 0x11
62#define HV7131_REG_FCSU 0x12
63#define HV7131_REG_FCSL 0x13
64#define HV7131_REG_FWHU 0x14
65#define HV7131_REG_FWHL 0x15
66#define HV7131_REG_FWWU 0x16
67#define HV7131_REG_FWWL 0x17
68/* Timing registers: */
69#define HV7131_REG_THBU 0x20
70#define HV7131_REG_THBL 0x21
71#define HV7131_REG_TVBU 0x22
72#define HV7131_REG_TVBL 0x23
73#define HV7131_REG_TITU 0x25
74#define HV7131_REG_TITM 0x26
75#define HV7131_REG_TITL 0x27
76#define HV7131_REG_TMCD 0x28
77/* Adjust Registers: */
78#define HV7131_REG_ARLV 0x30
79#define HV7131_REG_ARCG 0x31
80#define HV7131_REG_AGCG 0x32
81#define HV7131_REG_ABCG 0x33
82#define HV7131_REG_APBV 0x34
83#define HV7131_REG_ASLP 0x54
84/* Offset Registers: */
85#define HV7131_REG_OFSR 0x50
86#define HV7131_REG_OFSG 0x51
87#define HV7131_REG_OFSB 0x52
88/* REset level statistics registers: */
89#define HV7131_REG_LOREFNOH 0x57
90#define HV7131_REG_LOREFNOL 0x58
91#define HV7131_REG_HIREFNOH 0x59
92#define HV7131_REG_HIREFNOL 0x5a
93
94/* se401 registers */
95#define SE401_OPERATINGMODE 0x2000
96
97
98/* size of usb transfers */
99#define SE401_PACKETSIZE 4096
100/* number of queued bulk transfers to use, should be about 8 */
101#define SE401_NUMSBUF 1
102/* read the usb specs for this one :) */
103#define SE401_VIDEO_ENDPOINT 1
104#define SE401_BUTTON_ENDPOINT 2
105/* number of frames supported by the v4l part */
106#define SE401_NUMFRAMES 2
107/* scratch buffers for passing data to the decoders */
108#define SE401_NUMSCRATCH 32
109/* maximum amount of data in a JangGu packet */
110#define SE401_VLCDATALEN 1024
111/* number of nul sized packets to receive before kicking the camera */
112#define SE401_MAX_NULLPACKETS 4000
113/* number of decoding errors before kicking the camera */
114#define SE401_MAX_ERRORS 200
115
116struct usb_device;
117
118struct se401_sbuf {
119 unsigned char *data;
120};
121
122enum {
123 FRAME_UNUSED, /* Unused (no MCAPTURE) */
124 FRAME_READY, /* Ready to start grabbing */
125 FRAME_GRABBING, /* In the process of being grabbed into */
126 FRAME_DONE, /* Finished grabbing, but not been synced yet */
127 FRAME_ERROR, /* Something bad happened while processing */
128};
129
130enum {
131 FMT_BAYER,
132 FMT_JANGGU,
133};
134
135enum {
136 BUFFER_UNUSED,
137 BUFFER_READY,
138 BUFFER_BUSY,
139 BUFFER_DONE,
140};
141
142struct se401_scratch {
143 unsigned char *data;
144 volatile int state;
145 int offset;
146 int length;
147};
148
149struct se401_frame {
150 unsigned char *data; /* Frame buffer */
151
152 volatile int grabstate; /* State of grabbing */
153
154 unsigned char *curline;
155 int curlinepix;
156 int curpix;
157};
158
159struct usb_se401 {
160 struct video_device vdev;
161
162 /* Device structure */
163 struct usb_device *dev;
164
165 unsigned char iface;
166
167 char *camera_name;
168
169 int change;
170 int brightness;
171 int hue;
172 int rgain;
173 int ggain;
174 int bgain;
175 int expose_h;
176 int expose_m;
177 int expose_l;
178 int resetlevel;
179
180 int enhance;
181
182 int format;
183 int sizes;
184 int *width;
185 int *height;
186 int cwidth; /* current width */
187 int cheight; /* current height */
188 int palette;
189 int maxframesize;
190 int cframesize; /* current framesize */
191
192 struct semaphore lock;
193 int user; /* user count for exclusive use */
194 int removed; /* device disconnected */
195
196 int streaming; /* Are we streaming video? */
197
198 char *fbuf; /* Videodev buffer area */
199
200 struct urb *urb[SE401_NUMSBUF];
201 struct urb *inturb;
202
203 int button;
204 int buttonpressed;
205
206 int curframe; /* Current receiving frame */
207 struct se401_frame frame[SE401_NUMFRAMES];
208 int readcount;
209 int framecount;
210 int error;
211 int dropped;
212
213 int scratch_next;
214 int scratch_use;
215 int scratch_overflow;
216 struct se401_scratch scratch[SE401_NUMSCRATCH];
217
218 /* Decoder specific data: */
219 unsigned char vlcdata[SE401_VLCDATALEN];
220 int vlcdatapos;
221 int bayeroffset;
222
223 struct se401_sbuf sbuf[SE401_NUMSBUF];
224
225 wait_queue_head_t wq; /* Processes waiting */
226
227 int nullpackets;
228};
229
230
231
232#endif
233
diff --git a/drivers/usb/media/sn9c102.h b/drivers/usb/media/sn9c102.h
deleted file mode 100644
index 17d60c1eea7e..000000000000
--- a/drivers/usb/media/sn9c102.h
+++ /dev/null
@@ -1,204 +0,0 @@
1/***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers *
3 * *
4 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _SN9C102_H_
22#define _SN9C102_H_
23
24#include <linux/version.h>
25#include <linux/usb.h>
26#include <linux/videodev2.h>
27#include <media/v4l2-common.h>
28#include <linux/device.h>
29#include <linux/list.h>
30#include <linux/spinlock.h>
31#include <linux/time.h>
32#include <linux/wait.h>
33#include <linux/types.h>
34#include <linux/param.h>
35#include <linux/rwsem.h>
36#include <asm/semaphore.h>
37
38#include "sn9c102_sensor.h"
39
40/*****************************************************************************/
41
42#define SN9C102_DEBUG
43#define SN9C102_DEBUG_LEVEL 2
44#define SN9C102_MAX_DEVICES 64
45#define SN9C102_PRESERVE_IMGSCALE 0
46#define SN9C102_FORCE_MUNMAP 0
47#define SN9C102_MAX_FRAMES 32
48#define SN9C102_URBS 2
49#define SN9C102_ISO_PACKETS 7
50#define SN9C102_ALTERNATE_SETTING 8
51#define SN9C102_URB_TIMEOUT msecs_to_jiffies(2 * SN9C102_ISO_PACKETS)
52#define SN9C102_CTRL_TIMEOUT 300
53
54/*****************************************************************************/
55
56enum sn9c102_bridge {
57 BRIDGE_SN9C101 = 0x01,
58 BRIDGE_SN9C102 = 0x02,
59 BRIDGE_SN9C103 = 0x04,
60};
61
62SN9C102_ID_TABLE
63SN9C102_SENSOR_TABLE
64
65enum sn9c102_frame_state {
66 F_UNUSED,
67 F_QUEUED,
68 F_GRABBING,
69 F_DONE,
70 F_ERROR,
71};
72
73struct sn9c102_frame_t {
74 void* bufmem;
75 struct v4l2_buffer buf;
76 enum sn9c102_frame_state state;
77 struct list_head frame;
78 unsigned long vma_use_count;
79};
80
81enum sn9c102_dev_state {
82 DEV_INITIALIZED = 0x01,
83 DEV_DISCONNECTED = 0x02,
84 DEV_MISCONFIGURED = 0x04,
85};
86
87enum sn9c102_io_method {
88 IO_NONE,
89 IO_READ,
90 IO_MMAP,
91};
92
93enum sn9c102_stream_state {
94 STREAM_OFF,
95 STREAM_INTERRUPT,
96 STREAM_ON,
97};
98
99typedef char sn9c103_sof_header_t[18];
100typedef char sn9c102_sof_header_t[12];
101typedef char sn9c102_eof_header_t[4];
102
103struct sn9c102_sysfs_attr {
104 u8 reg, i2c_reg;
105 sn9c103_sof_header_t frame_header;
106};
107
108struct sn9c102_module_param {
109 u8 force_munmap;
110};
111
112static DECLARE_MUTEX(sn9c102_sysfs_lock);
113static DECLARE_RWSEM(sn9c102_disconnect);
114
115struct sn9c102_device {
116 struct video_device* v4ldev;
117
118 enum sn9c102_bridge bridge;
119 struct sn9c102_sensor* sensor;
120
121 struct usb_device* usbdev;
122 struct urb* urb[SN9C102_URBS];
123 void* transfer_buffer[SN9C102_URBS];
124 u8* control_buffer;
125
126 struct sn9c102_frame_t *frame_current, frame[SN9C102_MAX_FRAMES];
127 struct list_head inqueue, outqueue;
128 u32 frame_count, nbuffers, nreadbuffers;
129
130 enum sn9c102_io_method io;
131 enum sn9c102_stream_state stream;
132
133 struct v4l2_jpegcompression compression;
134
135 struct sn9c102_sysfs_attr sysfs;
136 sn9c103_sof_header_t sof_header;
137 u16 reg[63];
138
139 struct sn9c102_module_param module_param;
140
141 enum sn9c102_dev_state state;
142 u8 users;
143
144 struct semaphore dev_sem, fileop_sem;
145 spinlock_t queue_lock;
146 wait_queue_head_t open, wait_frame, wait_stream;
147};
148
149/*****************************************************************************/
150
151void
152sn9c102_attach_sensor(struct sn9c102_device* cam,
153 struct sn9c102_sensor* sensor)
154{
155 cam->sensor = sensor;
156 cam->sensor->usbdev = cam->usbdev;
157}
158
159/*****************************************************************************/
160
161#undef DBG
162#undef KDBG
163#ifdef SN9C102_DEBUG
164# define DBG(level, fmt, args...) \
165do { \
166 if (debug >= (level)) { \
167 if ((level) == 1) \
168 dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
169 else if ((level) == 2) \
170 dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
171 else if ((level) >= 3) \
172 dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
173 __FUNCTION__, __LINE__ , ## args); \
174 } \
175} while (0)
176# define V4LDBG(level, name, cmd) \
177do { \
178 if (debug >= (level)) \
179 v4l_print_ioctl(name, cmd); \
180} while (0)
181# define KDBG(level, fmt, args...) \
182do { \
183 if (debug >= (level)) { \
184 if ((level) == 1 || (level) == 2) \
185 pr_info("sn9c102: " fmt "\n", ## args); \
186 else if ((level) == 3) \
187 pr_debug("sn9c102: [%s:%d] " fmt "\n", __FUNCTION__, \
188 __LINE__ , ## args); \
189 } \
190} while (0)
191#else
192# define DBG(level, fmt, args...) do {;} while(0)
193# define V4LDBG(level, name, cmd) do {;} while(0)
194# define KDBG(level, fmt, args...) do {;} while(0)
195#endif
196
197#undef PDBG
198#define PDBG(fmt, args...) \
199dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args)
200
201#undef PDBGG
202#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
203
204#endif /* _SN9C102_H_ */
diff --git a/drivers/usb/media/sn9c102_core.c b/drivers/usb/media/sn9c102_core.c
deleted file mode 100644
index c81397e4714b..000000000000
--- a/drivers/usb/media/sn9c102_core.c
+++ /dev/null
@@ -1,2889 +0,0 @@
1/***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers *
3 * *
4 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/param.h>
25#include <linux/moduleparam.h>
26#include <linux/errno.h>
27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/device.h>
30#include <linux/fs.h>
31#include <linux/delay.h>
32#include <linux/stddef.h>
33#include <linux/compiler.h>
34#include <linux/ioctl.h>
35#include <linux/poll.h>
36#include <linux/stat.h>
37#include <linux/mm.h>
38#include <linux/vmalloc.h>
39#include <linux/page-flags.h>
40#include <linux/byteorder/generic.h>
41#include <asm/page.h>
42#include <asm/uaccess.h>
43
44#include "sn9c102.h"
45
46/*****************************************************************************/
47
48#define SN9C102_MODULE_NAME "V4L2 driver for SN9C10x PC Camera Controllers"
49#define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia"
50#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
51#define SN9C102_MODULE_LICENSE "GPL"
52#define SN9C102_MODULE_VERSION "1:1.26"
53#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 26)
54
55/*****************************************************************************/
56
57MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
58
59MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
60MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
61MODULE_VERSION(SN9C102_MODULE_VERSION);
62MODULE_LICENSE(SN9C102_MODULE_LICENSE);
63
64static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
65module_param_array(video_nr, short, NULL, 0444);
66MODULE_PARM_DESC(video_nr,
67 "\n<-1|n[,...]> Specify V4L2 minor mode number."
68 "\n -1 = use next available (default)"
69 "\n n = use minor number n (integer >= 0)"
70 "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
71 " cameras this way."
72 "\nFor example:"
73 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
74 "\nthe second camera and use auto for the first"
75 "\none and for every other camera."
76 "\n");
77
78static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
79 SN9C102_FORCE_MUNMAP};
80module_param_array(force_munmap, bool, NULL, 0444);
81MODULE_PARM_DESC(force_munmap,
82 "\n<0|1[,...]> Force the application to unmap previously"
83 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
84 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
85 "\nthis feature. This parameter is specific for each"
86 "\ndetected camera."
87 "\n 0 = do not force memory unmapping"
88 "\n 1 = force memory unmapping (save memory)"
89 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
90 "\n");
91
92#ifdef SN9C102_DEBUG
93static unsigned short debug = SN9C102_DEBUG_LEVEL;
94module_param(debug, ushort, 0644);
95MODULE_PARM_DESC(debug,
96 "\n<n> Debugging information level, from 0 to 3:"
97 "\n0 = none (use carefully)"
98 "\n1 = critical errors"
99 "\n2 = significant informations"
100 "\n3 = more verbose messages"
101 "\nLevel 3 is useful for testing only, when only "
102 "one device is used."
103 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
104 "\n");
105#endif
106
107/*****************************************************************************/
108
109static sn9c102_sof_header_t sn9c102_sof_header[] = {
110 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x00},
111 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
112};
113
114static sn9c103_sof_header_t sn9c103_sof_header[] = {
115 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x20},
116};
117
118static sn9c102_eof_header_t sn9c102_eof_header[] = {
119 {0x00, 0x00, 0x00, 0x00},
120 {0x40, 0x00, 0x00, 0x00},
121 {0x80, 0x00, 0x00, 0x00},
122 {0xc0, 0x00, 0x00, 0x00},
123};
124
125/*****************************************************************************/
126
127static u32
128sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
129 enum sn9c102_io_method io)
130{
131 struct v4l2_pix_format* p = &(cam->sensor->pix_format);
132 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds);
133 const size_t imagesize = cam->module_param.force_munmap ||
134 io == IO_READ ?
135 (p->width * p->height * p->priv) / 8 :
136 (r->width * r->height * p->priv) / 8;
137 void* buff = NULL;
138 u32 i;
139
140 if (count > SN9C102_MAX_FRAMES)
141 count = SN9C102_MAX_FRAMES;
142
143 cam->nbuffers = count;
144 while (cam->nbuffers > 0) {
145 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
146 break;
147 cam->nbuffers--;
148 }
149
150 for (i = 0; i < cam->nbuffers; i++) {
151 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
152 cam->frame[i].buf.index = i;
153 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
154 cam->frame[i].buf.length = imagesize;
155 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
156 cam->frame[i].buf.sequence = 0;
157 cam->frame[i].buf.field = V4L2_FIELD_NONE;
158 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
159 cam->frame[i].buf.flags = 0;
160 }
161
162 return cam->nbuffers;
163}
164
165
166static void sn9c102_release_buffers(struct sn9c102_device* cam)
167{
168 if (cam->nbuffers) {
169 vfree(cam->frame[0].bufmem);
170 cam->nbuffers = 0;
171 }
172 cam->frame_current = NULL;
173}
174
175
176static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
177{
178 u32 i;
179
180 INIT_LIST_HEAD(&cam->inqueue);
181 INIT_LIST_HEAD(&cam->outqueue);
182
183 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
184 cam->frame[i].state = F_UNUSED;
185 cam->frame[i].buf.bytesused = 0;
186 }
187}
188
189
190static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
191{
192 struct sn9c102_frame_t *i;
193
194 list_for_each_entry(i, &cam->outqueue, frame) {
195 i->state = F_QUEUED;
196 list_add(&i->frame, &cam->inqueue);
197 }
198
199 INIT_LIST_HEAD(&cam->outqueue);
200}
201
202
203static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
204{
205 unsigned long lock_flags;
206 u32 i;
207
208 for (i = 0; i < cam->nbuffers; i++)
209 if (cam->frame[i].state == F_UNUSED) {
210 cam->frame[i].state = F_QUEUED;
211 spin_lock_irqsave(&cam->queue_lock, lock_flags);
212 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
213 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
214 }
215}
216
217/*****************************************************************************/
218
219int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index)
220{
221 struct usb_device* udev = cam->usbdev;
222 int i, res;
223
224 if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg))
225 return -1;
226
227 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
228 index, 0, buff, sizeof(buff),
229 SN9C102_CTRL_TIMEOUT*sizeof(buff));
230 if (res < 0) {
231 DBG(3, "Failed to write registers (index 0x%02X, error %d)",
232 index, res);
233 return -1;
234 }
235
236 for (i = 0; i < sizeof(buff); i++)
237 cam->reg[index+i] = buff[i];
238
239 return 0;
240}
241
242
243int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
244{
245 struct usb_device* udev = cam->usbdev;
246 u8* buff = cam->control_buffer;
247 int res;
248
249 if (index >= ARRAY_SIZE(cam->reg))
250 return -1;
251
252 *buff = value;
253
254 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
255 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
256 if (res < 0) {
257 DBG(3, "Failed to write a register (value 0x%02X, index "
258 "0x%02X, error %d)", value, index, res);
259 return -1;
260 }
261
262 cam->reg[index] = value;
263
264 return 0;
265}
266
267
268/* NOTE: reading some registers always returns 0 */
269static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
270{
271 struct usb_device* udev = cam->usbdev;
272 u8* buff = cam->control_buffer;
273 int res;
274
275 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
276 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
277 if (res < 0)
278 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
279 index, res);
280
281 return (res >= 0) ? (int)(*buff) : -1;
282}
283
284
285int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
286{
287 if (index >= ARRAY_SIZE(cam->reg))
288 return -1;
289
290 return cam->reg[index];
291}
292
293
294static int
295sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
296{
297 int i, r;
298
299 for (i = 1; i <= 5; i++) {
300 r = sn9c102_read_reg(cam, 0x08);
301 if (r < 0)
302 return -EIO;
303 if (r & 0x04)
304 return 0;
305 if (sensor->frequency & SN9C102_I2C_400KHZ)
306 udelay(5*16);
307 else
308 udelay(16*16);
309 }
310 return -EBUSY;
311}
312
313
314static int
315sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
316 struct sn9c102_sensor* sensor)
317{
318 int r;
319 r = sn9c102_read_reg(cam, 0x08);
320 return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
321}
322
323
324static int
325sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
326 struct sn9c102_sensor* sensor)
327{
328 int r;
329 r = sn9c102_read_reg(cam, 0x08);
330 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
331}
332
333
334int
335sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
336 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
337 u8 n, u8 buffer[])
338{
339 struct usb_device* udev = cam->usbdev;
340 u8* data = cam->control_buffer;
341 int err = 0, res;
342
343 /* Write cycle */
344 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
345 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
346 data[1] = data0; /* I2C slave id */
347 data[2] = data1; /* address */
348 data[7] = 0x10;
349 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
350 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
351 if (res < 0)
352 err += res;
353
354 err += sn9c102_i2c_wait(cam, sensor);
355
356 /* Read cycle - n bytes */
357 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
358 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
359 (n << 4) | 0x02;
360 data[1] = data0;
361 data[7] = 0x10;
362 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
363 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
364 if (res < 0)
365 err += res;
366
367 err += sn9c102_i2c_wait(cam, sensor);
368
369 /* The first read byte will be placed in data[4] */
370 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
371 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
372 if (res < 0)
373 err += res;
374
375 err += sn9c102_i2c_detect_read_error(cam, sensor);
376
377 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
378 data[4]);
379
380 if (err) {
381 DBG(3, "I2C read failed for %s image sensor", sensor->name);
382 return -1;
383 }
384
385 if (buffer)
386 memcpy(buffer, data, sizeof(buffer));
387
388 return (int)data[4];
389}
390
391
392int
393sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
394 struct sn9c102_sensor* sensor, u8 n, u8 data0,
395 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
396{
397 struct usb_device* udev = cam->usbdev;
398 u8* data = cam->control_buffer;
399 int err = 0, res;
400
401 /* Write cycle. It usually is address + value */
402 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
403 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
404 | ((n - 1) << 4);
405 data[1] = data0;
406 data[2] = data1;
407 data[3] = data2;
408 data[4] = data3;
409 data[5] = data4;
410 data[6] = data5;
411 data[7] = 0x14;
412 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
413 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
414 if (res < 0)
415 err += res;
416
417 err += sn9c102_i2c_wait(cam, sensor);
418 err += sn9c102_i2c_detect_write_error(cam, sensor);
419
420 if (err)
421 DBG(3, "I2C write failed for %s image sensor", sensor->name);
422
423 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
424 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
425 n, data0, data1, data2, data3, data4, data5);
426
427 return err ? -1 : 0;
428}
429
430
431int
432sn9c102_i2c_try_read(struct sn9c102_device* cam,
433 struct sn9c102_sensor* sensor, u8 address)
434{
435 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
436 address, 1, NULL);
437}
438
439
440int
441sn9c102_i2c_try_write(struct sn9c102_device* cam,
442 struct sn9c102_sensor* sensor, u8 address, u8 value)
443{
444 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
445 sensor->i2c_slave_id, address,
446 value, 0, 0, 0);
447}
448
449
450int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
451{
452 if (!cam->sensor)
453 return -1;
454
455 return sn9c102_i2c_try_read(cam, cam->sensor, address);
456}
457
458
459int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
460{
461 if (!cam->sensor)
462 return -1;
463
464 return sn9c102_i2c_try_write(cam, cam->sensor, address, value);
465}
466
467/*****************************************************************************/
468
469static void*
470sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
471{
472 size_t soflen = 0, i;
473 u8 j, n = 0;
474
475 switch (cam->bridge) {
476 case BRIDGE_SN9C101:
477 case BRIDGE_SN9C102:
478 soflen = sizeof(sn9c102_sof_header_t);
479 n = sizeof(sn9c102_sof_header) / soflen;
480 break;
481 case BRIDGE_SN9C103:
482 soflen = sizeof(sn9c103_sof_header_t);
483 n = sizeof(sn9c103_sof_header) / soflen;
484 }
485
486 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
487 for (j = 0; j < n; j++)
488 /* The invariable part of the header is 6 bytes long */
489 if ((cam->bridge != BRIDGE_SN9C103 &&
490 !memcmp(mem + i, sn9c102_sof_header[j], 6)) ||
491 (cam->bridge == BRIDGE_SN9C103 &&
492 !memcmp(mem + i, sn9c103_sof_header[j], 6))) {
493 memcpy(cam->sof_header, mem + i, soflen);
494 /* Skip the header */
495 return mem + i + soflen;
496 }
497
498 return NULL;
499}
500
501
502static void*
503sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
504{
505 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
506 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
507
508 if (cam->sensor->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
509 return NULL; /* EOF header does not exist in compressed data */
510
511 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
512 for (j = 0; j < n; j++)
513 if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
514 return mem + i;
515
516 return NULL;
517}
518
519
520static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
521{
522 struct sn9c102_device* cam = urb->context;
523 struct sn9c102_frame_t** f;
524 size_t imagesize, soflen;
525 u8 i;
526 int err = 0;
527
528 if (urb->status == -ENOENT)
529 return;
530
531 f = &cam->frame_current;
532
533 if (cam->stream == STREAM_INTERRUPT) {
534 cam->stream = STREAM_OFF;
535 if ((*f))
536 (*f)->state = F_QUEUED;
537 DBG(3, "Stream interrupted");
538 wake_up_interruptible(&cam->wait_stream);
539 }
540
541 if (cam->state & DEV_DISCONNECTED)
542 return;
543
544 if (cam->state & DEV_MISCONFIGURED) {
545 wake_up_interruptible(&cam->wait_frame);
546 return;
547 }
548
549 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
550 goto resubmit_urb;
551
552 if (!(*f))
553 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
554 frame);
555
556 imagesize = (cam->sensor->pix_format.width *
557 cam->sensor->pix_format.height *
558 cam->sensor->pix_format.priv) / 8;
559
560 soflen = (cam->bridge) == BRIDGE_SN9C103 ?
561 sizeof(sn9c103_sof_header_t) :
562 sizeof(sn9c102_sof_header_t);
563
564 for (i = 0; i < urb->number_of_packets; i++) {
565 unsigned int img, len, status;
566 void *pos, *sof, *eof;
567
568 len = urb->iso_frame_desc[i].actual_length;
569 status = urb->iso_frame_desc[i].status;
570 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
571
572 if (status) {
573 DBG(3, "Error in isochronous frame");
574 (*f)->state = F_ERROR;
575 continue;
576 }
577
578 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
579
580redo:
581 sof = sn9c102_find_sof_header(cam, pos, len);
582 if (!sof) {
583 eof = sn9c102_find_eof_header(cam, pos, len);
584 if ((*f)->state == F_GRABBING) {
585end_of_frame:
586 img = len;
587
588 if (eof)
589 img = (eof > pos) ? eof - pos - 1 : 0;
590
591 if ((*f)->buf.bytesused+img > imagesize) {
592 u32 b = (*f)->buf.bytesused + img -
593 imagesize;
594 img = imagesize - (*f)->buf.bytesused;
595 DBG(3, "Expected EOF not found: "
596 "video frame cut");
597 if (eof)
598 DBG(3, "Exceeded limit: +%u "
599 "bytes", (unsigned)(b));
600 }
601
602 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
603 img);
604
605 if ((*f)->buf.bytesused == 0)
606 do_gettimeofday(&(*f)->buf.timestamp);
607
608 (*f)->buf.bytesused += img;
609
610 if ((*f)->buf.bytesused == imagesize ||
611 (cam->sensor->pix_format.pixelformat ==
612 V4L2_PIX_FMT_SN9C10X && eof)) {
613 u32 b = (*f)->buf.bytesused;
614 (*f)->state = F_DONE;
615 (*f)->buf.sequence= ++cam->frame_count;
616 spin_lock(&cam->queue_lock);
617 list_move_tail(&(*f)->frame,
618 &cam->outqueue);
619 if (!list_empty(&cam->inqueue))
620 (*f) = list_entry(
621 cam->inqueue.next,
622 struct sn9c102_frame_t,
623 frame );
624 else
625 (*f) = NULL;
626 spin_unlock(&cam->queue_lock);
627 memcpy(cam->sysfs.frame_header,
628 cam->sof_header, soflen);
629 DBG(3, "Video frame captured: %lu "
630 "bytes", (unsigned long)(b));
631
632 if (!(*f))
633 goto resubmit_urb;
634
635 } else if (eof) {
636 (*f)->state = F_ERROR;
637 DBG(3, "Not expected EOF after %lu "
638 "bytes of image data",
639 (unsigned long)
640 ((*f)->buf.bytesused));
641 }
642
643 if (sof) /* (1) */
644 goto start_of_frame;
645
646 } else if (eof) {
647 DBG(3, "EOF without SOF");
648 continue;
649
650 } else {
651 PDBGG("Ignoring pointless isochronous frame");
652 continue;
653 }
654
655 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
656start_of_frame:
657 (*f)->state = F_GRABBING;
658 (*f)->buf.bytesused = 0;
659 len -= (sof - pos);
660 pos = sof;
661 DBG(3, "SOF detected: new video frame");
662 if (len)
663 goto redo;
664
665 } else if ((*f)->state == F_GRABBING) {
666 eof = sn9c102_find_eof_header(cam, pos, len);
667 if (eof && eof < sof)
668 goto end_of_frame; /* (1) */
669 else {
670 if (cam->sensor->pix_format.pixelformat ==
671 V4L2_PIX_FMT_SN9C10X) {
672 eof = sof - soflen;
673 goto end_of_frame;
674 } else {
675 DBG(3, "SOF before expected EOF after "
676 "%lu bytes of image data",
677 (unsigned long)
678 ((*f)->buf.bytesused));
679 goto start_of_frame;
680 }
681 }
682 }
683 }
684
685resubmit_urb:
686 urb->dev = cam->usbdev;
687 err = usb_submit_urb(urb, GFP_ATOMIC);
688 if (err < 0 && err != -EPERM) {
689 cam->state |= DEV_MISCONFIGURED;
690 DBG(1, "usb_submit_urb() failed");
691 }
692
693 wake_up_interruptible(&cam->wait_frame);
694}
695
696
697static int sn9c102_start_transfer(struct sn9c102_device* cam)
698{
699 struct usb_device *udev = cam->usbdev;
700 struct urb* urb;
701 const unsigned int sn9c102_wMaxPacketSize[] = {0, 128, 256, 384, 512,
702 680, 800, 900, 1023};
703 const unsigned int sn9c103_wMaxPacketSize[] = {0, 128, 256, 384, 512,
704 680, 800, 900, 1003};
705 const unsigned int psz = (cam->bridge == BRIDGE_SN9C103) ?
706 sn9c103_wMaxPacketSize[SN9C102_ALTERNATE_SETTING] :
707 sn9c102_wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
708 s8 i, j;
709 int err = 0;
710
711 for (i = 0; i < SN9C102_URBS; i++) {
712 cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
713 GFP_KERNEL);
714 if (!cam->transfer_buffer[i]) {
715 err = -ENOMEM;
716 DBG(1, "Not enough memory");
717 goto free_buffers;
718 }
719 }
720
721 for (i = 0; i < SN9C102_URBS; i++) {
722 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
723 cam->urb[i] = urb;
724 if (!urb) {
725 err = -ENOMEM;
726 DBG(1, "usb_alloc_urb() failed");
727 goto free_urbs;
728 }
729 urb->dev = udev;
730 urb->context = cam;
731 urb->pipe = usb_rcvisocpipe(udev, 1);
732 urb->transfer_flags = URB_ISO_ASAP;
733 urb->number_of_packets = SN9C102_ISO_PACKETS;
734 urb->complete = sn9c102_urb_complete;
735 urb->transfer_buffer = cam->transfer_buffer[i];
736 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
737 urb->interval = 1;
738 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
739 urb->iso_frame_desc[j].offset = psz * j;
740 urb->iso_frame_desc[j].length = psz;
741 }
742 }
743
744 /* Enable video */
745 if (!(cam->reg[0x01] & 0x04)) {
746 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
747 if (err) {
748 err = -EIO;
749 DBG(1, "I/O hardware error");
750 goto free_urbs;
751 }
752 }
753
754 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
755 if (err) {
756 DBG(1, "usb_set_interface() failed");
757 goto free_urbs;
758 }
759
760 cam->frame_current = NULL;
761
762 for (i = 0; i < SN9C102_URBS; i++) {
763 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
764 if (err) {
765 for (j = i-1; j >= 0; j--)
766 usb_kill_urb(cam->urb[j]);
767 DBG(1, "usb_submit_urb() failed, error %d", err);
768 goto free_urbs;
769 }
770 }
771
772 return 0;
773
774free_urbs:
775 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
776 usb_free_urb(cam->urb[i]);
777
778free_buffers:
779 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
780 kfree(cam->transfer_buffer[i]);
781
782 return err;
783}
784
785
786static int sn9c102_stop_transfer(struct sn9c102_device* cam)
787{
788 struct usb_device *udev = cam->usbdev;
789 s8 i;
790 int err = 0;
791
792 if (cam->state & DEV_DISCONNECTED)
793 return 0;
794
795 for (i = SN9C102_URBS-1; i >= 0; i--) {
796 usb_kill_urb(cam->urb[i]);
797 usb_free_urb(cam->urb[i]);
798 kfree(cam->transfer_buffer[i]);
799 }
800
801 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
802 if (err)
803 DBG(3, "usb_set_interface() failed");
804
805 return err;
806}
807
808
809static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
810{
811 int err = 0;
812
813 cam->stream = STREAM_INTERRUPT;
814 err = wait_event_timeout(cam->wait_stream,
815 (cam->stream == STREAM_OFF) ||
816 (cam->state & DEV_DISCONNECTED),
817 SN9C102_URB_TIMEOUT);
818 if (cam->state & DEV_DISCONNECTED)
819 return -ENODEV;
820 else if (err) {
821 cam->state |= DEV_MISCONFIGURED;
822 DBG(1, "The camera is misconfigured. To use it, close and "
823 "open /dev/video%d again.", cam->v4ldev->minor);
824 return err;
825 }
826
827 return 0;
828}
829
830/*****************************************************************************/
831
832#ifdef CONFIG_VIDEO_ADV_DEBUG
833static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
834{
835 char str[5];
836 char* endp;
837 unsigned long val;
838
839 if (len < 4) {
840 strncpy(str, buff, len);
841 str[len+1] = '\0';
842 } else {
843 strncpy(str, buff, 4);
844 str[4] = '\0';
845 }
846
847 val = simple_strtoul(str, &endp, 0);
848
849 *count = 0;
850 if (val <= 0xff)
851 *count = (ssize_t)(endp - str);
852 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
853 *count += 1;
854
855 return (u8)val;
856}
857
858/*
859 NOTE 1: being inside one of the following methods implies that the v4l
860 device exists for sure (see kobjects and reference counters)
861 NOTE 2: buffers are PAGE_SIZE long
862*/
863
864static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
865{
866 struct sn9c102_device* cam;
867 ssize_t count;
868
869 if (down_interruptible(&sn9c102_sysfs_lock))
870 return -ERESTARTSYS;
871
872 cam = video_get_drvdata(to_video_device(cd));
873 if (!cam) {
874 up(&sn9c102_sysfs_lock);
875 return -ENODEV;
876 }
877
878 count = sprintf(buf, "%u\n", cam->sysfs.reg);
879
880 up(&sn9c102_sysfs_lock);
881
882 return count;
883}
884
885
886static ssize_t
887sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
888{
889 struct sn9c102_device* cam;
890 u8 index;
891 ssize_t count;
892
893 if (down_interruptible(&sn9c102_sysfs_lock))
894 return -ERESTARTSYS;
895
896 cam = video_get_drvdata(to_video_device(cd));
897 if (!cam) {
898 up(&sn9c102_sysfs_lock);
899 return -ENODEV;
900 }
901
902 index = sn9c102_strtou8(buf, len, &count);
903 if (index > 0x1f || !count) {
904 up(&sn9c102_sysfs_lock);
905 return -EINVAL;
906 }
907
908 cam->sysfs.reg = index;
909
910 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg);
911 DBG(3, "Written bytes: %zd", count);
912
913 up(&sn9c102_sysfs_lock);
914
915 return count;
916}
917
918
919static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
920{
921 struct sn9c102_device* cam;
922 ssize_t count;
923 int val;
924
925 if (down_interruptible(&sn9c102_sysfs_lock))
926 return -ERESTARTSYS;
927
928 cam = video_get_drvdata(to_video_device(cd));
929 if (!cam) {
930 up(&sn9c102_sysfs_lock);
931 return -ENODEV;
932 }
933
934 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
935 up(&sn9c102_sysfs_lock);
936 return -EIO;
937 }
938
939 count = sprintf(buf, "%d\n", val);
940
941 DBG(3, "Read bytes: %zd", count);
942
943 up(&sn9c102_sysfs_lock);
944
945 return count;
946}
947
948
949static ssize_t
950sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
951{
952 struct sn9c102_device* cam;
953 u8 value;
954 ssize_t count;
955 int err;
956
957 if (down_interruptible(&sn9c102_sysfs_lock))
958 return -ERESTARTSYS;
959
960 cam = video_get_drvdata(to_video_device(cd));
961 if (!cam) {
962 up(&sn9c102_sysfs_lock);
963 return -ENODEV;
964 }
965
966 value = sn9c102_strtou8(buf, len, &count);
967 if (!count) {
968 up(&sn9c102_sysfs_lock);
969 return -EINVAL;
970 }
971
972 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
973 if (err) {
974 up(&sn9c102_sysfs_lock);
975 return -EIO;
976 }
977
978 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
979 cam->sysfs.reg, value);
980 DBG(3, "Written bytes: %zd", count);
981
982 up(&sn9c102_sysfs_lock);
983
984 return count;
985}
986
987
988static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
989{
990 struct sn9c102_device* cam;
991 ssize_t count;
992
993 if (down_interruptible(&sn9c102_sysfs_lock))
994 return -ERESTARTSYS;
995
996 cam = video_get_drvdata(to_video_device(cd));
997 if (!cam) {
998 up(&sn9c102_sysfs_lock);
999 return -ENODEV;
1000 }
1001
1002 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
1003
1004 DBG(3, "Read bytes: %zd", count);
1005
1006 up(&sn9c102_sysfs_lock);
1007
1008 return count;
1009}
1010
1011
1012static ssize_t
1013sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1014{
1015 struct sn9c102_device* cam;
1016 u8 index;
1017 ssize_t count;
1018
1019 if (down_interruptible(&sn9c102_sysfs_lock))
1020 return -ERESTARTSYS;
1021
1022 cam = video_get_drvdata(to_video_device(cd));
1023 if (!cam) {
1024 up(&sn9c102_sysfs_lock);
1025 return -ENODEV;
1026 }
1027
1028 index = sn9c102_strtou8(buf, len, &count);
1029 if (!count) {
1030 up(&sn9c102_sysfs_lock);
1031 return -EINVAL;
1032 }
1033
1034 cam->sysfs.i2c_reg = index;
1035
1036 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1037 DBG(3, "Written bytes: %zd", count);
1038
1039 up(&sn9c102_sysfs_lock);
1040
1041 return count;
1042}
1043
1044
1045static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1046{
1047 struct sn9c102_device* cam;
1048 ssize_t count;
1049 int val;
1050
1051 if (down_interruptible(&sn9c102_sysfs_lock))
1052 return -ERESTARTSYS;
1053
1054 cam = video_get_drvdata(to_video_device(cd));
1055 if (!cam) {
1056 up(&sn9c102_sysfs_lock);
1057 return -ENODEV;
1058 }
1059
1060 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_READ)) {
1061 up(&sn9c102_sysfs_lock);
1062 return -ENOSYS;
1063 }
1064
1065 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1066 up(&sn9c102_sysfs_lock);
1067 return -EIO;
1068 }
1069
1070 count = sprintf(buf, "%d\n", val);
1071
1072 DBG(3, "Read bytes: %zd", count);
1073
1074 up(&sn9c102_sysfs_lock);
1075
1076 return count;
1077}
1078
1079
1080static ssize_t
1081sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1082{
1083 struct sn9c102_device* cam;
1084 u8 value;
1085 ssize_t count;
1086 int err;
1087
1088 if (down_interruptible(&sn9c102_sysfs_lock))
1089 return -ERESTARTSYS;
1090
1091 cam = video_get_drvdata(to_video_device(cd));
1092 if (!cam) {
1093 up(&sn9c102_sysfs_lock);
1094 return -ENODEV;
1095 }
1096
1097 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_WRITE)) {
1098 up(&sn9c102_sysfs_lock);
1099 return -ENOSYS;
1100 }
1101
1102 value = sn9c102_strtou8(buf, len, &count);
1103 if (!count) {
1104 up(&sn9c102_sysfs_lock);
1105 return -EINVAL;
1106 }
1107
1108 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1109 if (err) {
1110 up(&sn9c102_sysfs_lock);
1111 return -EIO;
1112 }
1113
1114 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1115 cam->sysfs.i2c_reg, value);
1116 DBG(3, "Written bytes: %zd", count);
1117
1118 up(&sn9c102_sysfs_lock);
1119
1120 return count;
1121}
1122
1123
1124static ssize_t
1125sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1126{
1127 struct sn9c102_device* cam;
1128 enum sn9c102_bridge bridge;
1129 ssize_t res = 0;
1130 u8 value;
1131 ssize_t count;
1132
1133 if (down_interruptible(&sn9c102_sysfs_lock))
1134 return -ERESTARTSYS;
1135
1136 cam = video_get_drvdata(to_video_device(cd));
1137 if (!cam) {
1138 up(&sn9c102_sysfs_lock);
1139 return -ENODEV;
1140 }
1141
1142 bridge = cam->bridge;
1143
1144 up(&sn9c102_sysfs_lock);
1145
1146 value = sn9c102_strtou8(buf, len, &count);
1147 if (!count)
1148 return -EINVAL;
1149
1150 switch (bridge) {
1151 case BRIDGE_SN9C101:
1152 case BRIDGE_SN9C102:
1153 if (value > 0x0f)
1154 return -EINVAL;
1155 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1156 res = sn9c102_store_val(cd, buf, len);
1157 break;
1158 case BRIDGE_SN9C103:
1159 if (value > 0x7f)
1160 return -EINVAL;
1161 if ((res = sn9c102_store_reg(cd, "0x04", 4)) >= 0)
1162 res = sn9c102_store_val(cd, buf, len);
1163 break;
1164 }
1165
1166 return res;
1167}
1168
1169
1170static ssize_t
1171sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1172{
1173 ssize_t res = 0;
1174 u8 value;
1175 ssize_t count;
1176
1177 value = sn9c102_strtou8(buf, len, &count);
1178 if (!count || value > 0x7f)
1179 return -EINVAL;
1180
1181 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1182 res = sn9c102_store_val(cd, buf, len);
1183
1184 return res;
1185}
1186
1187
1188static ssize_t
1189sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1190{
1191 ssize_t res = 0;
1192 u8 value;
1193 ssize_t count;
1194
1195 value = sn9c102_strtou8(buf, len, &count);
1196 if (!count || value > 0x7f)
1197 return -EINVAL;
1198
1199 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1200 res = sn9c102_store_val(cd, buf, len);
1201
1202 return res;
1203}
1204
1205
1206static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1207{
1208 struct sn9c102_device* cam;
1209 ssize_t count;
1210
1211 cam = video_get_drvdata(to_video_device(cd));
1212 if (!cam)
1213 return -ENODEV;
1214
1215 count = sizeof(cam->sysfs.frame_header);
1216 memcpy(buf, cam->sysfs.frame_header, count);
1217
1218 DBG(3, "Frame header, read bytes: %zd", count);
1219
1220 return count;
1221}
1222
1223
1224static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1225 sn9c102_show_reg, sn9c102_store_reg);
1226static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1227 sn9c102_show_val, sn9c102_store_val);
1228static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1229 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1230static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1231 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1232static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1233static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1234static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1235static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1236 sn9c102_show_frame_header, NULL);
1237
1238
1239static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1240{
1241 struct video_device *v4ldev = cam->v4ldev;
1242
1243 video_device_create_file(v4ldev, &class_device_attr_reg);
1244 video_device_create_file(v4ldev, &class_device_attr_val);
1245 video_device_create_file(v4ldev, &class_device_attr_frame_header);
1246 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
1247 video_device_create_file(v4ldev, &class_device_attr_green);
1248 else if (cam->bridge == BRIDGE_SN9C103) {
1249 video_device_create_file(v4ldev, &class_device_attr_blue);
1250 video_device_create_file(v4ldev, &class_device_attr_red);
1251 }
1252 if (cam->sensor && cam->sensor->sysfs_ops) {
1253 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1254 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1255 }
1256}
1257#endif /* CONFIG_VIDEO_ADV_DEBUG */
1258
1259/*****************************************************************************/
1260
1261static int
1262sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1263{
1264 int err = 0;
1265
1266 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
1267 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, 0x18);
1268 else
1269 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, 0x18);
1270
1271 return err ? -EIO : 0;
1272}
1273
1274
1275static int
1276sn9c102_set_compression(struct sn9c102_device* cam,
1277 struct v4l2_jpegcompression* compression)
1278{
1279 int err = 0;
1280
1281 if (compression->quality == 0)
1282 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, 0x17);
1283 else if (compression->quality == 1)
1284 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, 0x17);
1285
1286 return err ? -EIO : 0;
1287}
1288
1289
1290static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1291{
1292 u8 r = 0;
1293 int err = 0;
1294
1295 if (scale == 1)
1296 r = cam->reg[0x18] & 0xcf;
1297 else if (scale == 2) {
1298 r = cam->reg[0x18] & 0xcf;
1299 r |= 0x10;
1300 } else if (scale == 4)
1301 r = cam->reg[0x18] | 0x20;
1302
1303 err += sn9c102_write_reg(cam, r, 0x18);
1304 if (err)
1305 return -EIO;
1306
1307 PDBGG("Scaling factor: %u", scale);
1308
1309 return 0;
1310}
1311
1312
1313static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1314{
1315 struct sn9c102_sensor* s = cam->sensor;
1316 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1317 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1318 h_size = (u8)(rect->width / 16),
1319 v_size = (u8)(rect->height / 16);
1320 int err = 0;
1321
1322 err += sn9c102_write_reg(cam, h_start, 0x12);
1323 err += sn9c102_write_reg(cam, v_start, 0x13);
1324 err += sn9c102_write_reg(cam, h_size, 0x15);
1325 err += sn9c102_write_reg(cam, v_size, 0x16);
1326 if (err)
1327 return -EIO;
1328
1329 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1330 "%u %u %u %u", h_start, v_start, h_size, v_size);
1331
1332 return 0;
1333}
1334
1335
1336static int sn9c102_init(struct sn9c102_device* cam)
1337{
1338 struct sn9c102_sensor* s = cam->sensor;
1339 struct v4l2_control ctrl;
1340 struct v4l2_queryctrl *qctrl;
1341 struct v4l2_rect* rect;
1342 u8 i = 0;
1343 int err = 0;
1344
1345 if (!(cam->state & DEV_INITIALIZED)) {
1346 init_waitqueue_head(&cam->open);
1347 qctrl = s->qctrl;
1348 rect = &(s->cropcap.defrect);
1349 } else { /* use current values */
1350 qctrl = s->_qctrl;
1351 rect = &(s->_rect);
1352 }
1353
1354 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1355 err += sn9c102_set_crop(cam, rect);
1356 if (err)
1357 return err;
1358
1359 if (s->init) {
1360 err = s->init(cam);
1361 if (err) {
1362 DBG(3, "Sensor initialization failed");
1363 return err;
1364 }
1365 }
1366
1367 if (!(cam->state & DEV_INITIALIZED))
1368 cam->compression.quality = cam->reg[0x17] & 0x01 ? 0 : 1;
1369 else
1370 err += sn9c102_set_compression(cam, &cam->compression);
1371 err += sn9c102_set_pix_format(cam, &s->pix_format);
1372 if (s->set_pix_format)
1373 err += s->set_pix_format(cam, &s->pix_format);
1374 if (err)
1375 return err;
1376
1377 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1378 DBG(3, "Compressed video format is active, quality %d",
1379 cam->compression.quality);
1380 else
1381 DBG(3, "Uncompressed video format is active");
1382
1383 if (s->set_crop)
1384 if ((err = s->set_crop(cam, rect))) {
1385 DBG(3, "set_crop() failed");
1386 return err;
1387 }
1388
1389 if (s->set_ctrl) {
1390 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1391 if (s->qctrl[i].id != 0 &&
1392 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1393 ctrl.id = s->qctrl[i].id;
1394 ctrl.value = qctrl[i].default_value;
1395 err = s->set_ctrl(cam, &ctrl);
1396 if (err) {
1397 DBG(3, "Set %s control failed",
1398 s->qctrl[i].name);
1399 return err;
1400 }
1401 DBG(3, "Image sensor supports '%s' control",
1402 s->qctrl[i].name);
1403 }
1404 }
1405
1406 if (!(cam->state & DEV_INITIALIZED)) {
1407 init_MUTEX(&cam->fileop_sem);
1408 spin_lock_init(&cam->queue_lock);
1409 init_waitqueue_head(&cam->wait_frame);
1410 init_waitqueue_head(&cam->wait_stream);
1411 cam->nreadbuffers = 2;
1412 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1413 memcpy(&(s->_rect), &(s->cropcap.defrect),
1414 sizeof(struct v4l2_rect));
1415 cam->state |= DEV_INITIALIZED;
1416 }
1417
1418 DBG(2, "Initialization succeeded");
1419 return 0;
1420}
1421
1422
1423static void sn9c102_release_resources(struct sn9c102_device* cam)
1424{
1425 down(&sn9c102_sysfs_lock);
1426
1427 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1428 video_set_drvdata(cam->v4ldev, NULL);
1429 video_unregister_device(cam->v4ldev);
1430
1431 up(&sn9c102_sysfs_lock);
1432
1433 kfree(cam->control_buffer);
1434}
1435
1436/*****************************************************************************/
1437
1438static int sn9c102_open(struct inode* inode, struct file* filp)
1439{
1440 struct sn9c102_device* cam;
1441 int err = 0;
1442
1443 /*
1444 This is the only safe way to prevent race conditions with
1445 disconnect
1446 */
1447 if (!down_read_trylock(&sn9c102_disconnect))
1448 return -ERESTARTSYS;
1449
1450 cam = video_get_drvdata(video_devdata(filp));
1451
1452 if (down_interruptible(&cam->dev_sem)) {
1453 up_read(&sn9c102_disconnect);
1454 return -ERESTARTSYS;
1455 }
1456
1457 if (cam->users) {
1458 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1459 if ((filp->f_flags & O_NONBLOCK) ||
1460 (filp->f_flags & O_NDELAY)) {
1461 err = -EWOULDBLOCK;
1462 goto out;
1463 }
1464 up(&cam->dev_sem);
1465 err = wait_event_interruptible_exclusive(cam->open,
1466 cam->state & DEV_DISCONNECTED
1467 || !cam->users);
1468 if (err) {
1469 up_read(&sn9c102_disconnect);
1470 return err;
1471 }
1472 if (cam->state & DEV_DISCONNECTED) {
1473 up_read(&sn9c102_disconnect);
1474 return -ENODEV;
1475 }
1476 down(&cam->dev_sem);
1477 }
1478
1479
1480 if (cam->state & DEV_MISCONFIGURED) {
1481 err = sn9c102_init(cam);
1482 if (err) {
1483 DBG(1, "Initialization failed again. "
1484 "I will retry on next open().");
1485 goto out;
1486 }
1487 cam->state &= ~DEV_MISCONFIGURED;
1488 }
1489
1490 if ((err = sn9c102_start_transfer(cam)))
1491 goto out;
1492
1493 filp->private_data = cam;
1494 cam->users++;
1495 cam->io = IO_NONE;
1496 cam->stream = STREAM_OFF;
1497 cam->nbuffers = 0;
1498 cam->frame_count = 0;
1499 sn9c102_empty_framequeues(cam);
1500
1501 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1502
1503out:
1504 up(&cam->dev_sem);
1505 up_read(&sn9c102_disconnect);
1506 return err;
1507}
1508
1509
1510static int sn9c102_release(struct inode* inode, struct file* filp)
1511{
1512 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1513
1514 down(&cam->dev_sem); /* prevent disconnect() to be called */
1515
1516 sn9c102_stop_transfer(cam);
1517
1518 sn9c102_release_buffers(cam);
1519
1520 if (cam->state & DEV_DISCONNECTED) {
1521 sn9c102_release_resources(cam);
1522 up(&cam->dev_sem);
1523 kfree(cam);
1524 return 0;
1525 }
1526
1527 cam->users--;
1528 wake_up_interruptible_nr(&cam->open, 1);
1529
1530 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1531
1532 up(&cam->dev_sem);
1533
1534 return 0;
1535}
1536
1537
1538static ssize_t
1539sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1540{
1541 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1542 struct sn9c102_frame_t* f, * i;
1543 unsigned long lock_flags;
1544 int err = 0;
1545
1546 if (down_interruptible(&cam->fileop_sem))
1547 return -ERESTARTSYS;
1548
1549 if (cam->state & DEV_DISCONNECTED) {
1550 DBG(1, "Device not present");
1551 up(&cam->fileop_sem);
1552 return -ENODEV;
1553 }
1554
1555 if (cam->state & DEV_MISCONFIGURED) {
1556 DBG(1, "The camera is misconfigured. Close and open it "
1557 "again.");
1558 up(&cam->fileop_sem);
1559 return -EIO;
1560 }
1561
1562 if (cam->io == IO_MMAP) {
1563 DBG(3, "Close and open the device again to choose "
1564 "the read method");
1565 up(&cam->fileop_sem);
1566 return -EINVAL;
1567 }
1568
1569 if (cam->io == IO_NONE) {
1570 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1571 DBG(1, "read() failed, not enough memory");
1572 up(&cam->fileop_sem);
1573 return -ENOMEM;
1574 }
1575 cam->io = IO_READ;
1576 cam->stream = STREAM_ON;
1577 }
1578
1579 if (list_empty(&cam->inqueue)) {
1580 if (!list_empty(&cam->outqueue))
1581 sn9c102_empty_framequeues(cam);
1582 sn9c102_queue_unusedframes(cam);
1583 }
1584
1585 if (!count) {
1586 up(&cam->fileop_sem);
1587 return 0;
1588 }
1589
1590 if (list_empty(&cam->outqueue)) {
1591 if (filp->f_flags & O_NONBLOCK) {
1592 up(&cam->fileop_sem);
1593 return -EAGAIN;
1594 }
1595 err = wait_event_interruptible
1596 ( cam->wait_frame,
1597 (!list_empty(&cam->outqueue)) ||
1598 (cam->state & DEV_DISCONNECTED) ||
1599 (cam->state & DEV_MISCONFIGURED) );
1600 if (err) {
1601 up(&cam->fileop_sem);
1602 return err;
1603 }
1604 if (cam->state & DEV_DISCONNECTED) {
1605 up(&cam->fileop_sem);
1606 return -ENODEV;
1607 }
1608 if (cam->state & DEV_MISCONFIGURED) {
1609 up(&cam->fileop_sem);
1610 return -EIO;
1611 }
1612 }
1613
1614 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1615
1616 if (count > f->buf.bytesused)
1617 count = f->buf.bytesused;
1618
1619 if (copy_to_user(buf, f->bufmem, count)) {
1620 err = -EFAULT;
1621 goto exit;
1622 }
1623 *f_pos += count;
1624
1625exit:
1626 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1627 list_for_each_entry(i, &cam->outqueue, frame)
1628 i->state = F_UNUSED;
1629 INIT_LIST_HEAD(&cam->outqueue);
1630 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1631
1632 sn9c102_queue_unusedframes(cam);
1633
1634 PDBGG("Frame #%lu, bytes read: %zu",
1635 (unsigned long)f->buf.index, count);
1636
1637 up(&cam->fileop_sem);
1638
1639 return count;
1640}
1641
1642
1643static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1644{
1645 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1646 struct sn9c102_frame_t* f;
1647 unsigned long lock_flags;
1648 unsigned int mask = 0;
1649
1650 if (down_interruptible(&cam->fileop_sem))
1651 return POLLERR;
1652
1653 if (cam->state & DEV_DISCONNECTED) {
1654 DBG(1, "Device not present");
1655 goto error;
1656 }
1657
1658 if (cam->state & DEV_MISCONFIGURED) {
1659 DBG(1, "The camera is misconfigured. Close and open it "
1660 "again.");
1661 goto error;
1662 }
1663
1664 if (cam->io == IO_NONE) {
1665 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1666 IO_READ)) {
1667 DBG(1, "poll() failed, not enough memory");
1668 goto error;
1669 }
1670 cam->io = IO_READ;
1671 cam->stream = STREAM_ON;
1672 }
1673
1674 if (cam->io == IO_READ) {
1675 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1676 list_for_each_entry(f, &cam->outqueue, frame)
1677 f->state = F_UNUSED;
1678 INIT_LIST_HEAD(&cam->outqueue);
1679 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1680 sn9c102_queue_unusedframes(cam);
1681 }
1682
1683 poll_wait(filp, &cam->wait_frame, wait);
1684
1685 if (!list_empty(&cam->outqueue))
1686 mask |= POLLIN | POLLRDNORM;
1687
1688 up(&cam->fileop_sem);
1689
1690 return mask;
1691
1692error:
1693 up(&cam->fileop_sem);
1694 return POLLERR;
1695}
1696
1697
1698static void sn9c102_vm_open(struct vm_area_struct* vma)
1699{
1700 struct sn9c102_frame_t* f = vma->vm_private_data;
1701 f->vma_use_count++;
1702}
1703
1704
1705static void sn9c102_vm_close(struct vm_area_struct* vma)
1706{
1707 /* NOTE: buffers are not freed here */
1708 struct sn9c102_frame_t* f = vma->vm_private_data;
1709 f->vma_use_count--;
1710}
1711
1712
1713static struct vm_operations_struct sn9c102_vm_ops = {
1714 .open = sn9c102_vm_open,
1715 .close = sn9c102_vm_close,
1716};
1717
1718
1719static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1720{
1721 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1722 unsigned long size = vma->vm_end - vma->vm_start,
1723 start = vma->vm_start;
1724 void *pos;
1725 u32 i;
1726
1727 if (down_interruptible(&cam->fileop_sem))
1728 return -ERESTARTSYS;
1729
1730 if (cam->state & DEV_DISCONNECTED) {
1731 DBG(1, "Device not present");
1732 up(&cam->fileop_sem);
1733 return -ENODEV;
1734 }
1735
1736 if (cam->state & DEV_MISCONFIGURED) {
1737 DBG(1, "The camera is misconfigured. Close and open it "
1738 "again.");
1739 up(&cam->fileop_sem);
1740 return -EIO;
1741 }
1742
1743 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1744 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1745 up(&cam->fileop_sem);
1746 return -EINVAL;
1747 }
1748
1749 for (i = 0; i < cam->nbuffers; i++) {
1750 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1751 break;
1752 }
1753 if (i == cam->nbuffers) {
1754 up(&cam->fileop_sem);
1755 return -EINVAL;
1756 }
1757
1758 vma->vm_flags |= VM_IO;
1759 vma->vm_flags |= VM_RESERVED;
1760
1761 pos = cam->frame[i].bufmem;
1762 while (size > 0) { /* size is page-aligned */
1763 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1764 up(&cam->fileop_sem);
1765 return -EAGAIN;
1766 }
1767 start += PAGE_SIZE;
1768 pos += PAGE_SIZE;
1769 size -= PAGE_SIZE;
1770 }
1771
1772 vma->vm_ops = &sn9c102_vm_ops;
1773 vma->vm_private_data = &cam->frame[i];
1774
1775 sn9c102_vm_open(vma);
1776
1777 up(&cam->fileop_sem);
1778
1779 return 0;
1780}
1781
1782/*****************************************************************************/
1783
1784static int
1785sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
1786{
1787 struct v4l2_capability cap = {
1788 .driver = "sn9c102",
1789 .version = SN9C102_MODULE_VERSION_CODE,
1790 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1791 V4L2_CAP_STREAMING,
1792 };
1793
1794 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1795 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1796 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1797 sizeof(cap.bus_info));
1798
1799 if (copy_to_user(arg, &cap, sizeof(cap)))
1800 return -EFAULT;
1801
1802 return 0;
1803}
1804
1805
1806static int
1807sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1808{
1809 struct v4l2_input i;
1810
1811 if (copy_from_user(&i, arg, sizeof(i)))
1812 return -EFAULT;
1813
1814 if (i.index)
1815 return -EINVAL;
1816
1817 memset(&i, 0, sizeof(i));
1818 strcpy(i.name, "Camera");
1819
1820 if (copy_to_user(arg, &i, sizeof(i)))
1821 return -EFAULT;
1822
1823 return 0;
1824}
1825
1826
1827static int
1828sn9c102_vidioc_gs_input(struct sn9c102_device* cam, void __user * arg)
1829{
1830 int index;
1831
1832 if (copy_from_user(&index, arg, sizeof(index)))
1833 return -EFAULT;
1834
1835 if (index != 0)
1836 return -EINVAL;
1837
1838 return 0;
1839}
1840
1841
1842static int
1843sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1844{
1845 struct sn9c102_sensor* s = cam->sensor;
1846 struct v4l2_queryctrl qc;
1847 u8 i;
1848
1849 if (copy_from_user(&qc, arg, sizeof(qc)))
1850 return -EFAULT;
1851
1852 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1853 if (qc.id && qc.id == s->qctrl[i].id) {
1854 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1855 if (copy_to_user(arg, &qc, sizeof(qc)))
1856 return -EFAULT;
1857 return 0;
1858 }
1859
1860 return -EINVAL;
1861}
1862
1863
1864static int
1865sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
1866{
1867 struct sn9c102_sensor* s = cam->sensor;
1868 struct v4l2_control ctrl;
1869 int err = 0;
1870 u8 i;
1871
1872 if (!s->get_ctrl && !s->set_ctrl)
1873 return -EINVAL;
1874
1875 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1876 return -EFAULT;
1877
1878 if (!s->get_ctrl) {
1879 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1880 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
1881 ctrl.value = s->_qctrl[i].default_value;
1882 goto exit;
1883 }
1884 return -EINVAL;
1885 } else
1886 err = s->get_ctrl(cam, &ctrl);
1887
1888exit:
1889 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1890 return -EFAULT;
1891
1892 return err;
1893}
1894
1895
1896static int
1897sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1898{
1899 struct sn9c102_sensor* s = cam->sensor;
1900 struct v4l2_control ctrl;
1901 u8 i;
1902 int err = 0;
1903
1904 if (!s->set_ctrl)
1905 return -EINVAL;
1906
1907 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1908 return -EFAULT;
1909
1910 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1911 if (ctrl.id == s->qctrl[i].id) {
1912 if (ctrl.value < s->qctrl[i].minimum ||
1913 ctrl.value > s->qctrl[i].maximum)
1914 return -ERANGE;
1915 ctrl.value -= ctrl.value % s->qctrl[i].step;
1916 break;
1917 }
1918
1919 if ((err = s->set_ctrl(cam, &ctrl)))
1920 return err;
1921
1922 s->_qctrl[i].default_value = ctrl.value;
1923
1924 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
1925 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
1926
1927 return 0;
1928}
1929
1930
1931static int
1932sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1933{
1934 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1935
1936 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1937 cc->pixelaspect.numerator = 1;
1938 cc->pixelaspect.denominator = 1;
1939
1940 if (copy_to_user(arg, cc, sizeof(*cc)))
1941 return -EFAULT;
1942
1943 return 0;
1944}
1945
1946
1947static int
1948sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
1949{
1950 struct sn9c102_sensor* s = cam->sensor;
1951 struct v4l2_crop crop = {
1952 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1953 };
1954
1955 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1956
1957 if (copy_to_user(arg, &crop, sizeof(crop)))
1958 return -EFAULT;
1959
1960 return 0;
1961}
1962
1963
1964static int
1965sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
1966{
1967 struct sn9c102_sensor* s = cam->sensor;
1968 struct v4l2_crop crop;
1969 struct v4l2_rect* rect;
1970 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1971 struct v4l2_pix_format* pix_format = &(s->pix_format);
1972 u8 scale;
1973 const enum sn9c102_stream_state stream = cam->stream;
1974 const u32 nbuffers = cam->nbuffers;
1975 u32 i;
1976 int err = 0;
1977
1978 if (copy_from_user(&crop, arg, sizeof(crop)))
1979 return -EFAULT;
1980
1981 rect = &(crop.c);
1982
1983 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1984 return -EINVAL;
1985
1986 if (cam->module_param.force_munmap)
1987 for (i = 0; i < cam->nbuffers; i++)
1988 if (cam->frame[i].vma_use_count) {
1989 DBG(3, "VIDIOC_S_CROP failed. "
1990 "Unmap the buffers first.");
1991 return -EINVAL;
1992 }
1993
1994 /* Preserve R,G or B origin */
1995 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1996 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1997
1998 if (rect->width < 16)
1999 rect->width = 16;
2000 if (rect->height < 16)
2001 rect->height = 16;
2002 if (rect->width > bounds->width)
2003 rect->width = bounds->width;
2004 if (rect->height > bounds->height)
2005 rect->height = bounds->height;
2006 if (rect->left < bounds->left)
2007 rect->left = bounds->left;
2008 if (rect->top < bounds->top)
2009 rect->top = bounds->top;
2010 if (rect->left + rect->width > bounds->left + bounds->width)
2011 rect->left = bounds->left+bounds->width - rect->width;
2012 if (rect->top + rect->height > bounds->top + bounds->height)
2013 rect->top = bounds->top+bounds->height - rect->height;
2014
2015 rect->width &= ~15L;
2016 rect->height &= ~15L;
2017
2018 if (SN9C102_PRESERVE_IMGSCALE) {
2019 /* Calculate the actual scaling factor */
2020 u32 a, b;
2021 a = rect->width * rect->height;
2022 b = pix_format->width * pix_format->height;
2023 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2024 } else
2025 scale = 1;
2026
2027 if (cam->stream == STREAM_ON)
2028 if ((err = sn9c102_stream_interrupt(cam)))
2029 return err;
2030
2031 if (copy_to_user(arg, &crop, sizeof(crop))) {
2032 cam->stream = stream;
2033 return -EFAULT;
2034 }
2035
2036 if (cam->module_param.force_munmap || cam->io == IO_READ)
2037 sn9c102_release_buffers(cam);
2038
2039 err = sn9c102_set_crop(cam, rect);
2040 if (s->set_crop)
2041 err += s->set_crop(cam, rect);
2042 err += sn9c102_set_scale(cam, scale);
2043
2044 if (err) { /* atomic, no rollback in ioctl() */
2045 cam->state |= DEV_MISCONFIGURED;
2046 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2047 "use the camera, close and open /dev/video%d again.",
2048 cam->v4ldev->minor);
2049 return -EIO;
2050 }
2051
2052 s->pix_format.width = rect->width/scale;
2053 s->pix_format.height = rect->height/scale;
2054 memcpy(&(s->_rect), rect, sizeof(*rect));
2055
2056 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2057 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2058 cam->state |= DEV_MISCONFIGURED;
2059 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2060 "use the camera, close and open /dev/video%d again.",
2061 cam->v4ldev->minor);
2062 return -ENOMEM;
2063 }
2064
2065 if (cam->io == IO_READ)
2066 sn9c102_empty_framequeues(cam);
2067 else if (cam->module_param.force_munmap)
2068 sn9c102_requeue_outqueue(cam);
2069
2070 cam->stream = stream;
2071
2072 return 0;
2073}
2074
2075
2076static int
2077sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2078{
2079 struct v4l2_fmtdesc fmtd;
2080
2081 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2082 return -EFAULT;
2083
2084 if (fmtd.index == 0) {
2085 strcpy(fmtd.description, "bayer rgb");
2086 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2087 } else if (fmtd.index == 1) {
2088 strcpy(fmtd.description, "compressed");
2089 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2090 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2091 } else
2092 return -EINVAL;
2093
2094 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2095 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2096
2097 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2098 return -EFAULT;
2099
2100 return 0;
2101}
2102
2103
2104static int
2105sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2106{
2107 struct v4l2_format format;
2108 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
2109
2110 if (copy_from_user(&format, arg, sizeof(format)))
2111 return -EFAULT;
2112
2113 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2114 return -EINVAL;
2115
2116 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
2117 ? 0 : (pfmt->width * pfmt->priv) / 8;
2118 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2119 pfmt->field = V4L2_FIELD_NONE;
2120 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2121
2122 if (copy_to_user(arg, &format, sizeof(format)))
2123 return -EFAULT;
2124
2125 return 0;
2126}
2127
2128
2129static int
2130sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2131 void __user * arg)
2132{
2133 struct sn9c102_sensor* s = cam->sensor;
2134 struct v4l2_format format;
2135 struct v4l2_pix_format* pix;
2136 struct v4l2_pix_format* pfmt = &(s->pix_format);
2137 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2138 struct v4l2_rect rect;
2139 u8 scale;
2140 const enum sn9c102_stream_state stream = cam->stream;
2141 const u32 nbuffers = cam->nbuffers;
2142 u32 i;
2143 int err = 0;
2144
2145 if (copy_from_user(&format, arg, sizeof(format)))
2146 return -EFAULT;
2147
2148 pix = &(format.fmt.pix);
2149
2150 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2151 return -EINVAL;
2152
2153 memcpy(&rect, &(s->_rect), sizeof(rect));
2154
2155 { /* calculate the actual scaling factor */
2156 u32 a, b;
2157 a = rect.width * rect.height;
2158 b = pix->width * pix->height;
2159 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2160 }
2161
2162 rect.width = scale * pix->width;
2163 rect.height = scale * pix->height;
2164
2165 if (rect.width < 16)
2166 rect.width = 16;
2167 if (rect.height < 16)
2168 rect.height = 16;
2169 if (rect.width > bounds->left + bounds->width - rect.left)
2170 rect.width = bounds->left + bounds->width - rect.left;
2171 if (rect.height > bounds->top + bounds->height - rect.top)
2172 rect.height = bounds->top + bounds->height - rect.top;
2173
2174 rect.width &= ~15L;
2175 rect.height &= ~15L;
2176
2177 { /* adjust the scaling factor */
2178 u32 a, b;
2179 a = rect.width * rect.height;
2180 b = pix->width * pix->height;
2181 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2182 }
2183
2184 pix->width = rect.width / scale;
2185 pix->height = rect.height / scale;
2186
2187 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2188 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2189 pix->pixelformat = pfmt->pixelformat;
2190 pix->priv = pfmt->priv; /* bpp */
2191 pix->colorspace = pfmt->colorspace;
2192 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2193 ? 0 : (pix->width * pix->priv) / 8;
2194 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2195 pix->field = V4L2_FIELD_NONE;
2196
2197 if (cmd == VIDIOC_TRY_FMT) {
2198 if (copy_to_user(arg, &format, sizeof(format)))
2199 return -EFAULT;
2200 return 0;
2201 }
2202
2203 if (cam->module_param.force_munmap)
2204 for (i = 0; i < cam->nbuffers; i++)
2205 if (cam->frame[i].vma_use_count) {
2206 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2207 "buffers first.");
2208 return -EINVAL;
2209 }
2210
2211 if (cam->stream == STREAM_ON)
2212 if ((err = sn9c102_stream_interrupt(cam)))
2213 return err;
2214
2215 if (copy_to_user(arg, &format, sizeof(format))) {
2216 cam->stream = stream;
2217 return -EFAULT;
2218 }
2219
2220 if (cam->module_param.force_munmap || cam->io == IO_READ)
2221 sn9c102_release_buffers(cam);
2222
2223 err += sn9c102_set_pix_format(cam, pix);
2224 err += sn9c102_set_crop(cam, &rect);
2225 if (s->set_pix_format)
2226 err += s->set_pix_format(cam, pix);
2227 if (s->set_crop)
2228 err += s->set_crop(cam, &rect);
2229 err += sn9c102_set_scale(cam, scale);
2230
2231 if (err) { /* atomic, no rollback in ioctl() */
2232 cam->state |= DEV_MISCONFIGURED;
2233 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2234 "use the camera, close and open /dev/video%d again.",
2235 cam->v4ldev->minor);
2236 return -EIO;
2237 }
2238
2239 memcpy(pfmt, pix, sizeof(*pix));
2240 memcpy(&(s->_rect), &rect, sizeof(rect));
2241
2242 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2243 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2244 cam->state |= DEV_MISCONFIGURED;
2245 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2246 "use the camera, close and open /dev/video%d again.",
2247 cam->v4ldev->minor);
2248 return -ENOMEM;
2249 }
2250
2251 if (cam->io == IO_READ)
2252 sn9c102_empty_framequeues(cam);
2253 else if (cam->module_param.force_munmap)
2254 sn9c102_requeue_outqueue(cam);
2255
2256 cam->stream = stream;
2257
2258 return 0;
2259}
2260
2261
2262static int
2263sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2264{
2265 if (copy_to_user(arg, &cam->compression,
2266 sizeof(cam->compression)))
2267 return -EFAULT;
2268
2269 return 0;
2270}
2271
2272
2273static int
2274sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2275{
2276 struct v4l2_jpegcompression jc;
2277 const enum sn9c102_stream_state stream = cam->stream;
2278 int err = 0;
2279
2280 if (copy_from_user(&jc, arg, sizeof(jc)))
2281 return -EFAULT;
2282
2283 if (jc.quality != 0 && jc.quality != 1)
2284 return -EINVAL;
2285
2286 if (cam->stream == STREAM_ON)
2287 if ((err = sn9c102_stream_interrupt(cam)))
2288 return err;
2289
2290 err += sn9c102_set_compression(cam, &jc);
2291 if (err) { /* atomic, no rollback in ioctl() */
2292 cam->state |= DEV_MISCONFIGURED;
2293 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2294 "problems. To use the camera, close and open "
2295 "/dev/video%d again.", cam->v4ldev->minor);
2296 return -EIO;
2297 }
2298
2299 cam->compression.quality = jc.quality;
2300
2301 cam->stream = stream;
2302
2303 return 0;
2304}
2305
2306
2307static int
2308sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2309{
2310 struct v4l2_requestbuffers rb;
2311 u32 i;
2312 int err;
2313
2314 if (copy_from_user(&rb, arg, sizeof(rb)))
2315 return -EFAULT;
2316
2317 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2318 rb.memory != V4L2_MEMORY_MMAP)
2319 return -EINVAL;
2320
2321 if (cam->io == IO_READ) {
2322 DBG(3, "Close and open the device again to choose the mmap "
2323 "I/O method");
2324 return -EINVAL;
2325 }
2326
2327 for (i = 0; i < cam->nbuffers; i++)
2328 if (cam->frame[i].vma_use_count) {
2329 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2330 "still mapped.");
2331 return -EINVAL;
2332 }
2333
2334 if (cam->stream == STREAM_ON)
2335 if ((err = sn9c102_stream_interrupt(cam)))
2336 return err;
2337
2338 sn9c102_empty_framequeues(cam);
2339
2340 sn9c102_release_buffers(cam);
2341 if (rb.count)
2342 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
2343
2344 if (copy_to_user(arg, &rb, sizeof(rb))) {
2345 sn9c102_release_buffers(cam);
2346 cam->io = IO_NONE;
2347 return -EFAULT;
2348 }
2349
2350 cam->io = rb.count ? IO_MMAP : IO_NONE;
2351
2352 return 0;
2353}
2354
2355
2356static int
2357sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2358{
2359 struct v4l2_buffer b;
2360
2361 if (copy_from_user(&b, arg, sizeof(b)))
2362 return -EFAULT;
2363
2364 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2365 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2366 return -EINVAL;
2367
2368 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2369
2370 if (cam->frame[b.index].vma_use_count)
2371 b.flags |= V4L2_BUF_FLAG_MAPPED;
2372
2373 if (cam->frame[b.index].state == F_DONE)
2374 b.flags |= V4L2_BUF_FLAG_DONE;
2375 else if (cam->frame[b.index].state != F_UNUSED)
2376 b.flags |= V4L2_BUF_FLAG_QUEUED;
2377
2378 if (copy_to_user(arg, &b, sizeof(b)))
2379 return -EFAULT;
2380
2381 return 0;
2382}
2383
2384
2385static int
2386sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2387{
2388 struct v4l2_buffer b;
2389 unsigned long lock_flags;
2390
2391 if (copy_from_user(&b, arg, sizeof(b)))
2392 return -EFAULT;
2393
2394 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2395 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2396 return -EINVAL;
2397
2398 if (cam->frame[b.index].state != F_UNUSED)
2399 return -EINVAL;
2400
2401 cam->frame[b.index].state = F_QUEUED;
2402
2403 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2404 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2405 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2406
2407 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2408
2409 return 0;
2410}
2411
2412
2413static int
2414sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2415 void __user * arg)
2416{
2417 struct v4l2_buffer b;
2418 struct sn9c102_frame_t *f;
2419 unsigned long lock_flags;
2420 int err = 0;
2421
2422 if (copy_from_user(&b, arg, sizeof(b)))
2423 return -EFAULT;
2424
2425 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2426 return -EINVAL;
2427
2428 if (list_empty(&cam->outqueue)) {
2429 if (cam->stream == STREAM_OFF)
2430 return -EINVAL;
2431 if (filp->f_flags & O_NONBLOCK)
2432 return -EAGAIN;
2433 err = wait_event_interruptible
2434 ( cam->wait_frame,
2435 (!list_empty(&cam->outqueue)) ||
2436 (cam->state & DEV_DISCONNECTED) ||
2437 (cam->state & DEV_MISCONFIGURED) );
2438 if (err)
2439 return err;
2440 if (cam->state & DEV_DISCONNECTED)
2441 return -ENODEV;
2442 if (cam->state & DEV_MISCONFIGURED)
2443 return -EIO;
2444 }
2445
2446 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2447 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2448 list_del(cam->outqueue.next);
2449 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2450
2451 f->state = F_UNUSED;
2452
2453 memcpy(&b, &f->buf, sizeof(b));
2454 if (f->vma_use_count)
2455 b.flags |= V4L2_BUF_FLAG_MAPPED;
2456
2457 if (copy_to_user(arg, &b, sizeof(b)))
2458 return -EFAULT;
2459
2460 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2461
2462 return 0;
2463}
2464
2465
2466static int
2467sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2468{
2469 int type;
2470
2471 if (copy_from_user(&type, arg, sizeof(type)))
2472 return -EFAULT;
2473
2474 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2475 return -EINVAL;
2476
2477 if (list_empty(&cam->inqueue))
2478 return -EINVAL;
2479
2480 cam->stream = STREAM_ON;
2481
2482 DBG(3, "Stream on");
2483
2484 return 0;
2485}
2486
2487
2488static int
2489sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2490{
2491 int type, err;
2492
2493 if (copy_from_user(&type, arg, sizeof(type)))
2494 return -EFAULT;
2495
2496 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2497 return -EINVAL;
2498
2499 if (cam->stream == STREAM_ON)
2500 if ((err = sn9c102_stream_interrupt(cam)))
2501 return err;
2502
2503 sn9c102_empty_framequeues(cam);
2504
2505 DBG(3, "Stream off");
2506
2507 return 0;
2508}
2509
2510
2511static int
2512sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2513{
2514 struct v4l2_streamparm sp;
2515
2516 if (copy_from_user(&sp, arg, sizeof(sp)))
2517 return -EFAULT;
2518
2519 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2520 return -EINVAL;
2521
2522 sp.parm.capture.extendedmode = 0;
2523 sp.parm.capture.readbuffers = cam->nreadbuffers;
2524
2525 if (copy_to_user(arg, &sp, sizeof(sp)))
2526 return -EFAULT;
2527
2528 return 0;
2529}
2530
2531
2532static int
2533sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2534{
2535 struct v4l2_streamparm sp;
2536
2537 if (copy_from_user(&sp, arg, sizeof(sp)))
2538 return -EFAULT;
2539
2540 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2541 return -EINVAL;
2542
2543 sp.parm.capture.extendedmode = 0;
2544
2545 if (sp.parm.capture.readbuffers == 0)
2546 sp.parm.capture.readbuffers = cam->nreadbuffers;
2547
2548 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2549 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2550
2551 if (copy_to_user(arg, &sp, sizeof(sp)))
2552 return -EFAULT;
2553
2554 cam->nreadbuffers = sp.parm.capture.readbuffers;
2555
2556 return 0;
2557}
2558
2559
2560static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
2561 unsigned int cmd, void __user * arg)
2562{
2563 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2564
2565 switch (cmd) {
2566
2567 case VIDIOC_QUERYCAP:
2568 return sn9c102_vidioc_querycap(cam, arg);
2569
2570 case VIDIOC_ENUMINPUT:
2571 return sn9c102_vidioc_enuminput(cam, arg);
2572
2573 case VIDIOC_G_INPUT:
2574 case VIDIOC_S_INPUT:
2575 return sn9c102_vidioc_gs_input(cam, arg);
2576
2577 case VIDIOC_QUERYCTRL:
2578 return sn9c102_vidioc_query_ctrl(cam, arg);
2579
2580 case VIDIOC_G_CTRL:
2581 return sn9c102_vidioc_g_ctrl(cam, arg);
2582
2583 case VIDIOC_S_CTRL_OLD:
2584 case VIDIOC_S_CTRL:
2585 return sn9c102_vidioc_s_ctrl(cam, arg);
2586
2587 case VIDIOC_CROPCAP_OLD:
2588 case VIDIOC_CROPCAP:
2589 return sn9c102_vidioc_cropcap(cam, arg);
2590
2591 case VIDIOC_G_CROP:
2592 return sn9c102_vidioc_g_crop(cam, arg);
2593
2594 case VIDIOC_S_CROP:
2595 return sn9c102_vidioc_s_crop(cam, arg);
2596
2597 case VIDIOC_ENUM_FMT:
2598 return sn9c102_vidioc_enum_fmt(cam, arg);
2599
2600 case VIDIOC_G_FMT:
2601 return sn9c102_vidioc_g_fmt(cam, arg);
2602
2603 case VIDIOC_TRY_FMT:
2604 case VIDIOC_S_FMT:
2605 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
2606
2607 case VIDIOC_G_JPEGCOMP:
2608 return sn9c102_vidioc_g_jpegcomp(cam, arg);
2609
2610 case VIDIOC_S_JPEGCOMP:
2611 return sn9c102_vidioc_s_jpegcomp(cam, arg);
2612
2613 case VIDIOC_REQBUFS:
2614 return sn9c102_vidioc_reqbufs(cam, arg);
2615
2616 case VIDIOC_QUERYBUF:
2617 return sn9c102_vidioc_querybuf(cam, arg);
2618
2619 case VIDIOC_QBUF:
2620 return sn9c102_vidioc_qbuf(cam, arg);
2621
2622 case VIDIOC_DQBUF:
2623 return sn9c102_vidioc_dqbuf(cam, filp, arg);
2624
2625 case VIDIOC_STREAMON:
2626 return sn9c102_vidioc_streamon(cam, arg);
2627
2628 case VIDIOC_STREAMOFF:
2629 return sn9c102_vidioc_streamoff(cam, arg);
2630
2631 case VIDIOC_G_PARM:
2632 return sn9c102_vidioc_g_parm(cam, arg);
2633
2634 case VIDIOC_S_PARM_OLD:
2635 case VIDIOC_S_PARM:
2636 return sn9c102_vidioc_s_parm(cam, arg);
2637
2638 case VIDIOC_G_STD:
2639 case VIDIOC_S_STD:
2640 case VIDIOC_QUERYSTD:
2641 case VIDIOC_ENUMSTD:
2642 case VIDIOC_QUERYMENU:
2643 return -EINVAL;
2644
2645 default:
2646 return -EINVAL;
2647
2648 }
2649}
2650
2651
2652static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2653 unsigned int cmd, unsigned long arg)
2654{
2655 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2656 int err = 0;
2657
2658 if (down_interruptible(&cam->fileop_sem))
2659 return -ERESTARTSYS;
2660
2661 if (cam->state & DEV_DISCONNECTED) {
2662 DBG(1, "Device not present");
2663 up(&cam->fileop_sem);
2664 return -ENODEV;
2665 }
2666
2667 if (cam->state & DEV_MISCONFIGURED) {
2668 DBG(1, "The camera is misconfigured. Close and open it "
2669 "again.");
2670 up(&cam->fileop_sem);
2671 return -EIO;
2672 }
2673
2674 V4LDBG(3, "sn9c102", cmd);
2675
2676 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2677
2678 up(&cam->fileop_sem);
2679
2680 return err;
2681}
2682
2683/*****************************************************************************/
2684
2685static struct file_operations sn9c102_fops = {
2686 .owner = THIS_MODULE,
2687 .open = sn9c102_open,
2688 .release = sn9c102_release,
2689 .ioctl = sn9c102_ioctl,
2690 .read = sn9c102_read,
2691 .poll = sn9c102_poll,
2692 .mmap = sn9c102_mmap,
2693 .llseek = no_llseek,
2694};
2695
2696/*****************************************************************************/
2697
2698/* It exists a single interface only. We do not need to validate anything. */
2699static int
2700sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2701{
2702 struct usb_device *udev = interface_to_usbdev(intf);
2703 struct sn9c102_device* cam;
2704 static unsigned int dev_nr = 0;
2705 unsigned int i;
2706 int err = 0, r;
2707
2708 if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2709 return -ENOMEM;
2710
2711 cam->usbdev = udev;
2712
2713 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2714 DBG(1, "kmalloc() failed");
2715 err = -ENOMEM;
2716 goto fail;
2717 }
2718
2719 if (!(cam->v4ldev = video_device_alloc())) {
2720 DBG(1, "video_device_alloc() failed");
2721 err = -ENOMEM;
2722 goto fail;
2723 }
2724
2725 init_MUTEX(&cam->dev_sem);
2726
2727 r = sn9c102_read_reg(cam, 0x00);
2728 if (r < 0 || r != 0x10) {
2729 DBG(1, "Sorry, this is not a SN9C10x based camera "
2730 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2731 err = -ENODEV;
2732 goto fail;
2733 }
2734
2735 cam->bridge = (id->idProduct & 0xffc0) == 0x6080 ?
2736 BRIDGE_SN9C103 : BRIDGE_SN9C102;
2737 switch (cam->bridge) {
2738 case BRIDGE_SN9C101:
2739 case BRIDGE_SN9C102:
2740 DBG(2, "SN9C10[12] PC Camera Controller detected "
2741 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2742 break;
2743 case BRIDGE_SN9C103:
2744 DBG(2, "SN9C103 PC Camera Controller detected "
2745 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2746 break;
2747 }
2748
2749 for (i = 0; sn9c102_sensor_table[i]; i++) {
2750 err = sn9c102_sensor_table[i](cam);
2751 if (!err)
2752 break;
2753 }
2754
2755 if (!err && cam->sensor) {
2756 DBG(2, "%s image sensor detected", cam->sensor->name);
2757 DBG(3, "Support for %s maintained by %s",
2758 cam->sensor->name, cam->sensor->maintainer);
2759 } else {
2760 DBG(1, "No supported image sensor detected");
2761 err = -ENODEV;
2762 goto fail;
2763 }
2764
2765 if (sn9c102_init(cam)) {
2766 DBG(1, "Initialization failed. I will retry on open().");
2767 cam->state |= DEV_MISCONFIGURED;
2768 }
2769
2770 strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
2771 cam->v4ldev->owner = THIS_MODULE;
2772 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2773 cam->v4ldev->hardware = 0;
2774 cam->v4ldev->fops = &sn9c102_fops;
2775 cam->v4ldev->minor = video_nr[dev_nr];
2776 cam->v4ldev->release = video_device_release;
2777 video_set_drvdata(cam->v4ldev, cam);
2778
2779 down(&cam->dev_sem);
2780
2781 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2782 video_nr[dev_nr]);
2783 if (err) {
2784 DBG(1, "V4L2 device registration failed");
2785 if (err == -ENFILE && video_nr[dev_nr] == -1)
2786 DBG(1, "Free /dev/videoX node not found");
2787 video_nr[dev_nr] = -1;
2788 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2789 up(&cam->dev_sem);
2790 goto fail;
2791 }
2792
2793 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2794
2795 cam->module_param.force_munmap = force_munmap[dev_nr];
2796
2797 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2798
2799#ifdef CONFIG_VIDEO_ADV_DEBUG
2800 sn9c102_create_sysfs(cam);
2801 DBG(2, "Optional device control through 'sysfs' interface ready");
2802#endif
2803
2804 usb_set_intfdata(intf, cam);
2805
2806 up(&cam->dev_sem);
2807
2808 return 0;
2809
2810fail:
2811 if (cam) {
2812 kfree(cam->control_buffer);
2813 if (cam->v4ldev)
2814 video_device_release(cam->v4ldev);
2815 kfree(cam);
2816 }
2817 return err;
2818}
2819
2820
2821static void sn9c102_usb_disconnect(struct usb_interface* intf)
2822{
2823 struct sn9c102_device* cam = usb_get_intfdata(intf);
2824
2825 if (!cam)
2826 return;
2827
2828 down_write(&sn9c102_disconnect);
2829
2830 down(&cam->dev_sem);
2831
2832 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2833
2834 wake_up_interruptible_all(&cam->open);
2835
2836 if (cam->users) {
2837 DBG(2, "Device /dev/video%d is open! Deregistration and "
2838 "memory deallocation are deferred on close.",
2839 cam->v4ldev->minor);
2840 cam->state |= DEV_MISCONFIGURED;
2841 sn9c102_stop_transfer(cam);
2842 cam->state |= DEV_DISCONNECTED;
2843 wake_up_interruptible(&cam->wait_frame);
2844 wake_up_interruptible(&cam->wait_stream);
2845 } else {
2846 cam->state |= DEV_DISCONNECTED;
2847 sn9c102_release_resources(cam);
2848 }
2849
2850 up(&cam->dev_sem);
2851
2852 if (!cam->users)
2853 kfree(cam);
2854
2855 up_write(&sn9c102_disconnect);
2856}
2857
2858
2859static struct usb_driver sn9c102_usb_driver = {
2860 .name = "sn9c102",
2861 .id_table = sn9c102_id_table,
2862 .probe = sn9c102_usb_probe,
2863 .disconnect = sn9c102_usb_disconnect,
2864};
2865
2866/*****************************************************************************/
2867
2868static int __init sn9c102_module_init(void)
2869{
2870 int err = 0;
2871
2872 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
2873 KDBG(3, SN9C102_MODULE_AUTHOR);
2874
2875 if ((err = usb_register(&sn9c102_usb_driver)))
2876 KDBG(1, "usb_register() failed");
2877
2878 return err;
2879}
2880
2881
2882static void __exit sn9c102_module_exit(void)
2883{
2884 usb_deregister(&sn9c102_usb_driver);
2885}
2886
2887
2888module_init(sn9c102_module_init);
2889module_exit(sn9c102_module_exit);
diff --git a/drivers/usb/media/sn9c102_hv7131d.c b/drivers/usb/media/sn9c102_hv7131d.c
deleted file mode 100644
index 46c12ec3ca62..000000000000
--- a/drivers/usb/media/sn9c102_hv7131d.c
+++ /dev/null
@@ -1,271 +0,0 @@
1/***************************************************************************
2 * Plug-in for HV7131D image sensor connected to the SN9C10x PC Camera *
3 * Controllers *
4 * *
5 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#include "sn9c102_sensor.h"
23
24
25static struct sn9c102_sensor hv7131d;
26
27
28static int hv7131d_init(struct sn9c102_device* cam)
29{
30 int err = 0;
31
32 err += sn9c102_write_reg(cam, 0x00, 0x10);
33 err += sn9c102_write_reg(cam, 0x00, 0x11);
34 err += sn9c102_write_reg(cam, 0x00, 0x14);
35 err += sn9c102_write_reg(cam, 0x60, 0x17);
36 err += sn9c102_write_reg(cam, 0x0e, 0x18);
37 err += sn9c102_write_reg(cam, 0xf2, 0x19);
38
39 err += sn9c102_i2c_write(cam, 0x01, 0x04);
40 err += sn9c102_i2c_write(cam, 0x02, 0x00);
41 err += sn9c102_i2c_write(cam, 0x28, 0x00);
42
43 return err;
44}
45
46
47static int hv7131d_get_ctrl(struct sn9c102_device* cam,
48 struct v4l2_control* ctrl)
49{
50 switch (ctrl->id) {
51 case V4L2_CID_EXPOSURE:
52 {
53 int r1 = sn9c102_i2c_read(cam, 0x26),
54 r2 = sn9c102_i2c_read(cam, 0x27);
55 if (r1 < 0 || r2 < 0)
56 return -EIO;
57 ctrl->value = (r1 << 8) | (r2 & 0xff);
58 }
59 return 0;
60 case V4L2_CID_RED_BALANCE:
61 if ((ctrl->value = sn9c102_i2c_read(cam, 0x31)) < 0)
62 return -EIO;
63 ctrl->value = 0x3f - (ctrl->value & 0x3f);
64 return 0;
65 case V4L2_CID_BLUE_BALANCE:
66 if ((ctrl->value = sn9c102_i2c_read(cam, 0x33)) < 0)
67 return -EIO;
68 ctrl->value = 0x3f - (ctrl->value & 0x3f);
69 return 0;
70 case SN9C102_V4L2_CID_GREEN_BALANCE:
71 if ((ctrl->value = sn9c102_i2c_read(cam, 0x32)) < 0)
72 return -EIO;
73 ctrl->value = 0x3f - (ctrl->value & 0x3f);
74 return 0;
75 case SN9C102_V4L2_CID_RESET_LEVEL:
76 if ((ctrl->value = sn9c102_i2c_read(cam, 0x30)) < 0)
77 return -EIO;
78 ctrl->value &= 0x3f;
79 return 0;
80 case SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE:
81 if ((ctrl->value = sn9c102_i2c_read(cam, 0x34)) < 0)
82 return -EIO;
83 ctrl->value &= 0x07;
84 return 0;
85 default:
86 return -EINVAL;
87 }
88}
89
90
91static int hv7131d_set_ctrl(struct sn9c102_device* cam,
92 const struct v4l2_control* ctrl)
93{
94 int err = 0;
95
96 switch (ctrl->id) {
97 case V4L2_CID_EXPOSURE:
98 err += sn9c102_i2c_write(cam, 0x26, ctrl->value >> 8);
99 err += sn9c102_i2c_write(cam, 0x27, ctrl->value & 0xff);
100 break;
101 case V4L2_CID_RED_BALANCE:
102 err += sn9c102_i2c_write(cam, 0x31, 0x3f - ctrl->value);
103 break;
104 case V4L2_CID_BLUE_BALANCE:
105 err += sn9c102_i2c_write(cam, 0x33, 0x3f - ctrl->value);
106 break;
107 case SN9C102_V4L2_CID_GREEN_BALANCE:
108 err += sn9c102_i2c_write(cam, 0x32, 0x3f - ctrl->value);
109 break;
110 case SN9C102_V4L2_CID_RESET_LEVEL:
111 err += sn9c102_i2c_write(cam, 0x30, ctrl->value);
112 break;
113 case SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE:
114 err += sn9c102_i2c_write(cam, 0x34, ctrl->value);
115 break;
116 default:
117 return -EINVAL;
118 }
119
120 return err ? -EIO : 0;
121}
122
123
124static int hv7131d_set_crop(struct sn9c102_device* cam,
125 const struct v4l2_rect* rect)
126{
127 struct sn9c102_sensor* s = &hv7131d;
128 int err = 0;
129 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 2,
130 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 2;
131
132 err += sn9c102_write_reg(cam, h_start, 0x12);
133 err += sn9c102_write_reg(cam, v_start, 0x13);
134
135 return err;
136}
137
138
139static int hv7131d_set_pix_format(struct sn9c102_device* cam,
140 const struct v4l2_pix_format* pix)
141{
142 int err = 0;
143
144 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
145 err += sn9c102_write_reg(cam, 0x42, 0x19);
146 else
147 err += sn9c102_write_reg(cam, 0xf2, 0x19);
148
149 return err;
150}
151
152
153static struct sn9c102_sensor hv7131d = {
154 .name = "HV7131D",
155 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
156 .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
157 .frequency = SN9C102_I2C_100KHZ,
158 .interface = SN9C102_I2C_2WIRES,
159 .i2c_slave_id = 0x11,
160 .init = &hv7131d_init,
161 .qctrl = {
162 {
163 .id = V4L2_CID_EXPOSURE,
164 .type = V4L2_CTRL_TYPE_INTEGER,
165 .name = "exposure",
166 .minimum = 0x0250,
167 .maximum = 0xffff,
168 .step = 0x0001,
169 .default_value = 0x0250,
170 .flags = 0,
171 },
172 {
173 .id = V4L2_CID_RED_BALANCE,
174 .type = V4L2_CTRL_TYPE_INTEGER,
175 .name = "red balance",
176 .minimum = 0x00,
177 .maximum = 0x3f,
178 .step = 0x01,
179 .default_value = 0x00,
180 .flags = 0,
181 },
182 {
183 .id = V4L2_CID_BLUE_BALANCE,
184 .type = V4L2_CTRL_TYPE_INTEGER,
185 .name = "blue balance",
186 .minimum = 0x00,
187 .maximum = 0x3f,
188 .step = 0x01,
189 .default_value = 0x20,
190 .flags = 0,
191 },
192 {
193 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
194 .type = V4L2_CTRL_TYPE_INTEGER,
195 .name = "green balance",
196 .minimum = 0x00,
197 .maximum = 0x3f,
198 .step = 0x01,
199 .default_value = 0x1e,
200 .flags = 0,
201 },
202 {
203 .id = SN9C102_V4L2_CID_RESET_LEVEL,
204 .type = V4L2_CTRL_TYPE_INTEGER,
205 .name = "reset level",
206 .minimum = 0x19,
207 .maximum = 0x3f,
208 .step = 0x01,
209 .default_value = 0x30,
210 .flags = 0,
211 },
212 {
213 .id = SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE,
214 .type = V4L2_CTRL_TYPE_INTEGER,
215 .name = "pixel bias voltage",
216 .minimum = 0x00,
217 .maximum = 0x07,
218 .step = 0x01,
219 .default_value = 0x02,
220 .flags = 0,
221 },
222 },
223 .get_ctrl = &hv7131d_get_ctrl,
224 .set_ctrl = &hv7131d_set_ctrl,
225 .cropcap = {
226 .bounds = {
227 .left = 0,
228 .top = 0,
229 .width = 640,
230 .height = 480,
231 },
232 .defrect = {
233 .left = 0,
234 .top = 0,
235 .width = 640,
236 .height = 480,
237 },
238 },
239 .set_crop = &hv7131d_set_crop,
240 .pix_format = {
241 .width = 640,
242 .height = 480,
243 .pixelformat = V4L2_PIX_FMT_SBGGR8,
244 .priv = 8,
245 },
246 .set_pix_format = &hv7131d_set_pix_format
247};
248
249
250int sn9c102_probe_hv7131d(struct sn9c102_device* cam)
251{
252 int r0 = 0, r1 = 0, err = 0;
253
254 err += sn9c102_write_reg(cam, 0x01, 0x01);
255 err += sn9c102_write_reg(cam, 0x00, 0x01);
256 err += sn9c102_write_reg(cam, 0x28, 0x17);
257 if (err)
258 return -EIO;
259
260 r0 = sn9c102_i2c_try_read(cam, &hv7131d, 0x00);
261 r1 = sn9c102_i2c_try_read(cam, &hv7131d, 0x01);
262 if (r0 < 0 || r1 < 0)
263 return -EIO;
264
265 if (r0 != 0x00 && r1 != 0x04)
266 return -ENODEV;
267
268 sn9c102_attach_sensor(cam, &hv7131d);
269
270 return 0;
271}
diff --git a/drivers/usb/media/sn9c102_mi0343.c b/drivers/usb/media/sn9c102_mi0343.c
deleted file mode 100644
index d9aa7a61095d..000000000000
--- a/drivers/usb/media/sn9c102_mi0343.c
+++ /dev/null
@@ -1,363 +0,0 @@
1/***************************************************************************
2 * Plug-in for MI-0343 image sensor connected to the SN9C10x PC Camera *
3 * Controllers *
4 * *
5 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#include "sn9c102_sensor.h"
23
24
25static struct sn9c102_sensor mi0343;
26static u8 mi0343_i2c_data[5+1];
27
28
29static int mi0343_init(struct sn9c102_device* cam)
30{
31 int err = 0;
32
33 err += sn9c102_write_reg(cam, 0x00, 0x10);
34 err += sn9c102_write_reg(cam, 0x00, 0x11);
35 err += sn9c102_write_reg(cam, 0x0a, 0x14);
36 err += sn9c102_write_reg(cam, 0x40, 0x01);
37 err += sn9c102_write_reg(cam, 0x20, 0x17);
38 err += sn9c102_write_reg(cam, 0x07, 0x18);
39 err += sn9c102_write_reg(cam, 0xa0, 0x19);
40
41 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4, mi0343.i2c_slave_id,
42 0x0d, 0x00, 0x01, 0, 0);
43 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4, mi0343.i2c_slave_id,
44 0x0d, 0x00, 0x00, 0, 0);
45 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4, mi0343.i2c_slave_id,
46 0x03, 0x01, 0xe1, 0, 0);
47 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4, mi0343.i2c_slave_id,
48 0x04, 0x02, 0x81, 0, 0);
49 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4, mi0343.i2c_slave_id,
50 0x05, 0x00, 0x17, 0, 0);
51 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4, mi0343.i2c_slave_id,
52 0x06, 0x00, 0x11, 0, 0);
53 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4, mi0343.i2c_slave_id,
54 0x62, 0x04, 0x9a, 0, 0);
55
56 return err;
57}
58
59
60static int mi0343_get_ctrl(struct sn9c102_device* cam,
61 struct v4l2_control* ctrl)
62{
63 switch (ctrl->id) {
64 case V4L2_CID_EXPOSURE:
65 if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id,
66 0x09, 2+1, mi0343_i2c_data) < 0)
67 return -EIO;
68 ctrl->value = mi0343_i2c_data[2];
69 return 0;
70 case V4L2_CID_GAIN:
71 if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id,
72 0x35, 2+1, mi0343_i2c_data) < 0)
73 return -EIO;
74 break;
75 case V4L2_CID_HFLIP:
76 if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id,
77 0x20, 2+1, mi0343_i2c_data) < 0)
78 return -EIO;
79 ctrl->value = mi0343_i2c_data[3] & 0x20 ? 1 : 0;
80 return 0;
81 case V4L2_CID_VFLIP:
82 if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id,
83 0x20, 2+1, mi0343_i2c_data) < 0)
84 return -EIO;
85 ctrl->value = mi0343_i2c_data[3] & 0x80 ? 1 : 0;
86 return 0;
87 case V4L2_CID_RED_BALANCE:
88 if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id,
89 0x2d, 2+1, mi0343_i2c_data) < 0)
90 return -EIO;
91 break;
92 case V4L2_CID_BLUE_BALANCE:
93 if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id,
94 0x2c, 2+1, mi0343_i2c_data) < 0)
95 return -EIO;
96 break;
97 case SN9C102_V4L2_CID_GREEN_BALANCE:
98 if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id,
99 0x2e, 2+1, mi0343_i2c_data) < 0)
100 return -EIO;
101 break;
102 default:
103 return -EINVAL;
104 }
105
106 switch (ctrl->id) {
107 case V4L2_CID_GAIN:
108 case V4L2_CID_RED_BALANCE:
109 case V4L2_CID_BLUE_BALANCE:
110 case SN9C102_V4L2_CID_GREEN_BALANCE:
111 ctrl->value = mi0343_i2c_data[3] | (mi0343_i2c_data[2] << 8);
112 if (ctrl->value >= 0x10 && ctrl->value <= 0x3f)
113 ctrl->value -= 0x10;
114 else if (ctrl->value >= 0x60 && ctrl->value <= 0x7f)
115 ctrl->value -= 0x60;
116 else if (ctrl->value >= 0xe0 && ctrl->value <= 0xff)
117 ctrl->value -= 0xe0;
118 }
119
120 return 0;
121}
122
123
124static int mi0343_set_ctrl(struct sn9c102_device* cam,
125 const struct v4l2_control* ctrl)
126{
127 u16 reg = 0;
128 int err = 0;
129
130 switch (ctrl->id) {
131 case V4L2_CID_GAIN:
132 case V4L2_CID_RED_BALANCE:
133 case V4L2_CID_BLUE_BALANCE:
134 case SN9C102_V4L2_CID_GREEN_BALANCE:
135 if (ctrl->value <= (0x3f-0x10))
136 reg = 0x10 + ctrl->value;
137 else if (ctrl->value <= ((0x3f-0x10) + (0x7f-0x60)))
138 reg = 0x60 + (ctrl->value - (0x3f-0x10));
139 else
140 reg = 0xe0 + (ctrl->value - (0x3f-0x10) - (0x7f-0x60));
141 break;
142 }
143
144 switch (ctrl->id) {
145 case V4L2_CID_EXPOSURE:
146 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4,
147 mi0343.i2c_slave_id,
148 0x09, ctrl->value, 0x00,
149 0, 0);
150 break;
151 case V4L2_CID_GAIN:
152 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4,
153 mi0343.i2c_slave_id,
154 0x35, reg >> 8, reg & 0xff,
155 0, 0);
156 break;
157 case V4L2_CID_HFLIP:
158 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4,
159 mi0343.i2c_slave_id,
160 0x20, ctrl->value ? 0x40:0x00,
161 ctrl->value ? 0x20:0x00,
162 0, 0);
163 break;
164 case V4L2_CID_VFLIP:
165 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4,
166 mi0343.i2c_slave_id,
167 0x20, ctrl->value ? 0x80:0x00,
168 ctrl->value ? 0x80:0x00,
169 0, 0);
170 break;
171 case V4L2_CID_RED_BALANCE:
172 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4,
173 mi0343.i2c_slave_id,
174 0x2d, reg >> 8, reg & 0xff,
175 0, 0);
176 break;
177 case V4L2_CID_BLUE_BALANCE:
178 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4,
179 mi0343.i2c_slave_id,
180 0x2c, reg >> 8, reg & 0xff,
181 0, 0);
182 break;
183 case SN9C102_V4L2_CID_GREEN_BALANCE:
184 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4,
185 mi0343.i2c_slave_id,
186 0x2b, reg >> 8, reg & 0xff,
187 0, 0);
188 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4,
189 mi0343.i2c_slave_id,
190 0x2e, reg >> 8, reg & 0xff,
191 0, 0);
192 break;
193 default:
194 return -EINVAL;
195 }
196
197 return err ? -EIO : 0;
198}
199
200
201static int mi0343_set_crop(struct sn9c102_device* cam,
202 const struct v4l2_rect* rect)
203{
204 struct sn9c102_sensor* s = &mi0343;
205 int err = 0;
206 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 0,
207 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 2;
208
209 err += sn9c102_write_reg(cam, h_start, 0x12);
210 err += sn9c102_write_reg(cam, v_start, 0x13);
211
212 return err;
213}
214
215
216static int mi0343_set_pix_format(struct sn9c102_device* cam,
217 const struct v4l2_pix_format* pix)
218{
219 int err = 0;
220
221 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) {
222 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4,
223 mi0343.i2c_slave_id,
224 0x0a, 0x00, 0x03, 0, 0);
225 err += sn9c102_write_reg(cam, 0x20, 0x19);
226 } else {
227 err += sn9c102_i2c_try_raw_write(cam, &mi0343, 4,
228 mi0343.i2c_slave_id,
229 0x0a, 0x00, 0x05, 0, 0);
230 err += sn9c102_write_reg(cam, 0xa0, 0x19);
231 }
232
233 return err;
234}
235
236
237static struct sn9c102_sensor mi0343 = {
238 .name = "MI-0343",
239 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
240 .frequency = SN9C102_I2C_100KHZ,
241 .interface = SN9C102_I2C_2WIRES,
242 .i2c_slave_id = 0x5d,
243 .init = &mi0343_init,
244 .qctrl = {
245 {
246 .id = V4L2_CID_EXPOSURE,
247 .type = V4L2_CTRL_TYPE_INTEGER,
248 .name = "exposure",
249 .minimum = 0x00,
250 .maximum = 0x0f,
251 .step = 0x01,
252 .default_value = 0x06,
253 .flags = 0,
254 },
255 {
256 .id = V4L2_CID_GAIN,
257 .type = V4L2_CTRL_TYPE_INTEGER,
258 .name = "global gain",
259 .minimum = 0x00,
260 .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),/*0x6d*/
261 .step = 0x01,
262 .default_value = 0x00,
263 .flags = 0,
264 },
265 {
266 .id = V4L2_CID_HFLIP,
267 .type = V4L2_CTRL_TYPE_BOOLEAN,
268 .name = "horizontal mirror",
269 .minimum = 0,
270 .maximum = 1,
271 .step = 1,
272 .default_value = 0,
273 .flags = 0,
274 },
275 {
276 .id = V4L2_CID_VFLIP,
277 .type = V4L2_CTRL_TYPE_BOOLEAN,
278 .name = "vertical mirror",
279 .minimum = 0,
280 .maximum = 1,
281 .step = 1,
282 .default_value = 0,
283 .flags = 0,
284 },
285 {
286 .id = V4L2_CID_RED_BALANCE,
287 .type = V4L2_CTRL_TYPE_INTEGER,
288 .name = "red balance",
289 .minimum = 0x00,
290 .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),
291 .step = 0x01,
292 .default_value = 0x00,
293 .flags = 0,
294 },
295 {
296 .id = V4L2_CID_BLUE_BALANCE,
297 .type = V4L2_CTRL_TYPE_INTEGER,
298 .name = "blue balance",
299 .minimum = 0x00,
300 .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),
301 .step = 0x01,
302 .default_value = 0x00,
303 .flags = 0,
304 },
305 {
306 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
307 .type = V4L2_CTRL_TYPE_INTEGER,
308 .name = "green balance",
309 .minimum = 0x00,
310 .maximum = ((0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0)),
311 .step = 0x01,
312 .default_value = 0x00,
313 .flags = 0,
314 },
315 },
316 .get_ctrl = &mi0343_get_ctrl,
317 .set_ctrl = &mi0343_set_ctrl,
318 .cropcap = {
319 .bounds = {
320 .left = 0,
321 .top = 0,
322 .width = 640,
323 .height = 480,
324 },
325 .defrect = {
326 .left = 0,
327 .top = 0,
328 .width = 640,
329 .height = 480,
330 },
331 },
332 .set_crop = &mi0343_set_crop,
333 .pix_format = {
334 .width = 640,
335 .height = 480,
336 .pixelformat = V4L2_PIX_FMT_SBGGR8,
337 .priv = 8,
338 },
339 .set_pix_format = &mi0343_set_pix_format
340};
341
342
343int sn9c102_probe_mi0343(struct sn9c102_device* cam)
344{
345 int err = 0;
346
347 err += sn9c102_write_reg(cam, 0x01, 0x01);
348 err += sn9c102_write_reg(cam, 0x00, 0x01);
349 err += sn9c102_write_reg(cam, 0x28, 0x17);
350 if (err)
351 return -EIO;
352
353 if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id, 0x00,
354 2, mi0343_i2c_data) < 0)
355 return -EIO;
356
357 if (mi0343_i2c_data[4] != 0x32 && mi0343_i2c_data[3] != 0xe3)
358 return -ENODEV;
359
360 sn9c102_attach_sensor(cam, &mi0343);
361
362 return 0;
363}
diff --git a/drivers/usb/media/sn9c102_ov7630.c b/drivers/usb/media/sn9c102_ov7630.c
deleted file mode 100644
index 4a36519b5af4..000000000000
--- a/drivers/usb/media/sn9c102_ov7630.c
+++ /dev/null
@@ -1,396 +0,0 @@
1/***************************************************************************
2 * Plug-in for OV7630 image sensor connected to the SN9C10x PC Camera *
3 * Controllers *
4 * *
5 * Copyright (C) 2005-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#include "sn9c102_sensor.h"
23
24
25static struct sn9c102_sensor ov7630;
26
27
28static int ov7630_init(struct sn9c102_device* cam)
29{
30 int err = 0;
31
32 err += sn9c102_write_reg(cam, 0x00, 0x14);
33 err += sn9c102_write_reg(cam, 0x60, 0x17);
34 err += sn9c102_write_reg(cam, 0x0f, 0x18);
35 err += sn9c102_write_reg(cam, 0x50, 0x19);
36
37 err += sn9c102_i2c_write(cam, 0x12, 0x8d);
38 err += sn9c102_i2c_write(cam, 0x11, 0x00);
39 err += sn9c102_i2c_write(cam, 0x15, 0x34);
40 err += sn9c102_i2c_write(cam, 0x16, 0x03);
41 err += sn9c102_i2c_write(cam, 0x17, 0x1c);
42 err += sn9c102_i2c_write(cam, 0x18, 0xbd);
43 err += sn9c102_i2c_write(cam, 0x19, 0x06);
44 err += sn9c102_i2c_write(cam, 0x1a, 0xf6);
45 err += sn9c102_i2c_write(cam, 0x1b, 0x04);
46 err += sn9c102_i2c_write(cam, 0x20, 0x44);
47 err += sn9c102_i2c_write(cam, 0x23, 0xee);
48 err += sn9c102_i2c_write(cam, 0x26, 0xa0);
49 err += sn9c102_i2c_write(cam, 0x27, 0x9a);
50 err += sn9c102_i2c_write(cam, 0x28, 0x20);
51 err += sn9c102_i2c_write(cam, 0x29, 0x30);
52 err += sn9c102_i2c_write(cam, 0x2f, 0x3d);
53 err += sn9c102_i2c_write(cam, 0x30, 0x24);
54 err += sn9c102_i2c_write(cam, 0x32, 0x86);
55 err += sn9c102_i2c_write(cam, 0x60, 0xa9);
56 err += sn9c102_i2c_write(cam, 0x61, 0x42);
57 err += sn9c102_i2c_write(cam, 0x65, 0x00);
58 err += sn9c102_i2c_write(cam, 0x69, 0x38);
59 err += sn9c102_i2c_write(cam, 0x6f, 0x88);
60 err += sn9c102_i2c_write(cam, 0x70, 0x0b);
61 err += sn9c102_i2c_write(cam, 0x71, 0x00);
62 err += sn9c102_i2c_write(cam, 0x74, 0x21);
63 err += sn9c102_i2c_write(cam, 0x7d, 0xf7);
64
65 return err;
66}
67
68
69static int ov7630_set_ctrl(struct sn9c102_device* cam,
70 const struct v4l2_control* ctrl)
71{
72 int err = 0;
73
74 switch (ctrl->id) {
75 case V4L2_CID_EXPOSURE:
76 err += sn9c102_i2c_write(cam, 0x10, ctrl->value >> 2);
77 err += sn9c102_i2c_write(cam, 0x76, ctrl->value & 0x03);
78 break;
79 case V4L2_CID_RED_BALANCE:
80 err += sn9c102_i2c_write(cam, 0x02, ctrl->value);
81 break;
82 case V4L2_CID_BLUE_BALANCE:
83 err += sn9c102_i2c_write(cam, 0x03, ctrl->value);
84 break;
85 case V4L2_CID_GAIN:
86 err += sn9c102_i2c_write(cam, 0x00, ctrl->value);
87 break;
88 case V4L2_CID_CONTRAST:
89 err += ctrl->value ? sn9c102_i2c_write(cam, 0x05,
90 (ctrl->value-1) | 0x20)
91 : sn9c102_i2c_write(cam, 0x05, 0x00);
92 break;
93 case V4L2_CID_BRIGHTNESS:
94 err += sn9c102_i2c_write(cam, 0x06, ctrl->value);
95 break;
96 case V4L2_CID_SATURATION:
97 err += sn9c102_i2c_write(cam, 0x03, ctrl->value << 4);
98 break;
99 case V4L2_CID_HUE:
100 err += ctrl->value ? sn9c102_i2c_write(cam, 0x04,
101 (ctrl->value-1) | 0x20)
102 : sn9c102_i2c_write(cam, 0x04, 0x00);
103 break;
104 case V4L2_CID_DO_WHITE_BALANCE:
105 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
106 break;
107 case V4L2_CID_WHITENESS:
108 err += sn9c102_i2c_write(cam, 0x0d, ctrl->value);
109 break;
110 case V4L2_CID_AUTO_WHITE_BALANCE:
111 err += sn9c102_i2c_write(cam, 0x12, (ctrl->value << 2) | 0x09);
112 break;
113 case V4L2_CID_AUTOGAIN:
114 err += sn9c102_i2c_write(cam, 0x13, ctrl->value);
115 break;
116 case V4L2_CID_VFLIP:
117 err += sn9c102_i2c_write(cam, 0x75, 0x0e | (ctrl->value << 7));
118 break;
119 case V4L2_CID_BLACK_LEVEL:
120 err += sn9c102_i2c_write(cam, 0x25, ctrl->value);
121 break;
122 case SN9C102_V4L2_CID_BRIGHT_LEVEL:
123 err += sn9c102_i2c_write(cam, 0x24, ctrl->value);
124 break;
125 case SN9C102_V4L2_CID_GAMMA:
126 err += sn9c102_i2c_write(cam, 0x14, (ctrl->value << 2) | 0x80);
127 break;
128 case SN9C102_V4L2_CID_BAND_FILTER:
129 err += sn9c102_i2c_write(cam, 0x2d, ctrl->value << 2);
130 break;
131 default:
132 return -EINVAL;
133 }
134
135 return err ? -EIO : 0;
136}
137
138
139static int ov7630_set_crop(struct sn9c102_device* cam,
140 const struct v4l2_rect* rect)
141{
142 struct sn9c102_sensor* s = &ov7630;
143 int err = 0;
144 u8 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1;
145
146 err += sn9c102_write_reg(cam, v_start, 0x13);
147
148 return err;
149}
150
151
152static int ov7630_set_pix_format(struct sn9c102_device* cam,
153 const struct v4l2_pix_format* pix)
154{
155 int err = 0;
156
157 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
158 err += sn9c102_write_reg(cam, 0x20, 0x19);
159 else
160 err += sn9c102_write_reg(cam, 0x50, 0x19);
161
162 return err;
163}
164
165
166static struct sn9c102_sensor ov7630 = {
167 .name = "OV7630",
168 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
169 .sysfs_ops = SN9C102_I2C_WRITE,
170 .frequency = SN9C102_I2C_100KHZ,
171 .interface = SN9C102_I2C_2WIRES,
172 .i2c_slave_id = 0x21,
173 .init = &ov7630_init,
174 .qctrl = {
175 {
176 .id = V4L2_CID_GAIN,
177 .type = V4L2_CTRL_TYPE_INTEGER,
178 .name = "global gain",
179 .minimum = 0x00,
180 .maximum = 0x3f,
181 .step = 0x01,
182 .default_value = 0x14,
183 .flags = 0,
184 },
185 {
186 .id = V4L2_CID_HUE,
187 .type = V4L2_CTRL_TYPE_INTEGER,
188 .name = "hue",
189 .minimum = 0x00,
190 .maximum = 0x1f+1,
191 .step = 0x01,
192 .default_value = 0x00,
193 .flags = 0,
194 },
195 {
196 .id = V4L2_CID_SATURATION,
197 .type = V4L2_CTRL_TYPE_INTEGER,
198 .name = "saturation",
199 .minimum = 0x00,
200 .maximum = 0x0f,
201 .step = 0x01,
202 .default_value = 0x08,
203 .flags = 0,
204 },
205 {
206 .id = V4L2_CID_CONTRAST,
207 .type = V4L2_CTRL_TYPE_INTEGER,
208 .name = "contrast",
209 .minimum = 0x00,
210 .maximum = 0x1f+1,
211 .step = 0x01,
212 .default_value = 0x00,
213 .flags = 0,
214 },
215 {
216 .id = V4L2_CID_EXPOSURE,
217 .type = V4L2_CTRL_TYPE_INTEGER,
218 .name = "exposure",
219 .minimum = 0x000,
220 .maximum = 0x3ff,
221 .step = 0x001,
222 .default_value = 0x83<<2,
223 .flags = 0,
224 },
225 {
226 .id = V4L2_CID_RED_BALANCE,
227 .type = V4L2_CTRL_TYPE_INTEGER,
228 .name = "red balance",
229 .minimum = 0x00,
230 .maximum = 0xff,
231 .step = 0x01,
232 .default_value = 0x3a,
233 .flags = 0,
234 },
235 {
236 .id = V4L2_CID_BLUE_BALANCE,
237 .type = V4L2_CTRL_TYPE_INTEGER,
238 .name = "blue balance",
239 .minimum = 0x00,
240 .maximum = 0xff,
241 .step = 0x01,
242 .default_value = 0x77,
243 .flags = 0,
244 },
245 {
246 .id = V4L2_CID_BRIGHTNESS,
247 .type = V4L2_CTRL_TYPE_INTEGER,
248 .name = "brightness",
249 .minimum = 0x00,
250 .maximum = 0xff,
251 .step = 0x01,
252 .default_value = 0xa0,
253 .flags = 0,
254 },
255 {
256 .id = V4L2_CID_DO_WHITE_BALANCE,
257 .type = V4L2_CTRL_TYPE_INTEGER,
258 .name = "white balance background: blue",
259 .minimum = 0x00,
260 .maximum = 0x3f,
261 .step = 0x01,
262 .default_value = 0x20,
263 .flags = 0,
264 },
265 {
266 .id = V4L2_CID_WHITENESS,
267 .type = V4L2_CTRL_TYPE_INTEGER,
268 .name = "white balance background: red",
269 .minimum = 0x00,
270 .maximum = 0x3f,
271 .step = 0x01,
272 .default_value = 0x20,
273 .flags = 0,
274 },
275 {
276 .id = V4L2_CID_AUTO_WHITE_BALANCE,
277 .type = V4L2_CTRL_TYPE_BOOLEAN,
278 .name = "auto white balance",
279 .minimum = 0x00,
280 .maximum = 0x01,
281 .step = 0x01,
282 .default_value = 0x01,
283 .flags = 0,
284 },
285 {
286 .id = V4L2_CID_AUTOGAIN,
287 .type = V4L2_CTRL_TYPE_INTEGER,
288 .name = "gain & exposure mode",
289 .minimum = 0x00,
290 .maximum = 0x03,
291 .step = 0x01,
292 .default_value = 0x00,
293 .flags = 0,
294 },
295 {
296 .id = V4L2_CID_VFLIP,
297 .type = V4L2_CTRL_TYPE_BOOLEAN,
298 .name = "vertical flip",
299 .minimum = 0x00,
300 .maximum = 0x01,
301 .step = 0x01,
302 .default_value = 0x01,
303 .flags = 0,
304 },
305 {
306 .id = V4L2_CID_BLACK_LEVEL,
307 .type = V4L2_CTRL_TYPE_INTEGER,
308 .name = "black pixel ratio",
309 .minimum = 0x01,
310 .maximum = 0x9a,
311 .step = 0x01,
312 .default_value = 0x8a,
313 .flags = 0,
314 },
315 {
316 .id = SN9C102_V4L2_CID_BRIGHT_LEVEL,
317 .type = V4L2_CTRL_TYPE_INTEGER,
318 .name = "bright pixel ratio",
319 .minimum = 0x01,
320 .maximum = 0x9a,
321 .step = 0x01,
322 .default_value = 0x10,
323 .flags = 0,
324 },
325 {
326 .id = SN9C102_V4L2_CID_BAND_FILTER,
327 .type = V4L2_CTRL_TYPE_BOOLEAN,
328 .name = "band filter",
329 .minimum = 0x00,
330 .maximum = 0x01,
331 .step = 0x01,
332 .default_value = 0x00,
333 .flags = 0,
334 },
335 {
336 .id = SN9C102_V4L2_CID_GAMMA,
337 .type = V4L2_CTRL_TYPE_BOOLEAN,
338 .name = "rgb gamma",
339 .minimum = 0x00,
340 .maximum = 0x01,
341 .step = 0x01,
342 .default_value = 0x00,
343 .flags = 0,
344 },
345 },
346 .set_ctrl = &ov7630_set_ctrl,
347 .cropcap = {
348 .bounds = {
349 .left = 0,
350 .top = 0,
351 .width = 640,
352 .height = 480,
353 },
354 .defrect = {
355 .left = 0,
356 .top = 0,
357 .width = 640,
358 .height = 480,
359 },
360 },
361 .set_crop = &ov7630_set_crop,
362 .pix_format = {
363 .width = 640,
364 .height = 480,
365 .pixelformat = V4L2_PIX_FMT_SBGGR8,
366 .priv = 8,
367 },
368 .set_pix_format = &ov7630_set_pix_format
369};
370
371
372int sn9c102_probe_ov7630(struct sn9c102_device* cam)
373{
374 int err = 0;
375
376 sn9c102_attach_sensor(cam, &ov7630);
377
378 if (le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x602c &&
379 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x602d &&
380 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x608f &&
381 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x60b0)
382 return -ENODEV;
383
384 err += sn9c102_write_reg(cam, 0x01, 0x01);
385 err += sn9c102_write_reg(cam, 0x00, 0x01);
386 err += sn9c102_write_reg(cam, 0x28, 0x17);
387
388 if (err)
389 return -EIO;
390
391 err += sn9c102_i2c_write(cam, 0x0b, 0);
392 if (err)
393 return -ENODEV;
394
395 return 0;
396}
diff --git a/drivers/usb/media/sn9c102_pas106b.c b/drivers/usb/media/sn9c102_pas106b.c
deleted file mode 100644
index b1dee78abe04..000000000000
--- a/drivers/usb/media/sn9c102_pas106b.c
+++ /dev/null
@@ -1,307 +0,0 @@
1/***************************************************************************
2 * Plug-in for PAS106B image sensor connected to the SN9C10x PC Camera *
3 * Controllers *
4 * *
5 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#include <linux/delay.h>
23#include "sn9c102_sensor.h"
24
25
26static struct sn9c102_sensor pas106b;
27
28
29static int pas106b_init(struct sn9c102_device* cam)
30{
31 int err = 0;
32
33 err += sn9c102_write_reg(cam, 0x00, 0x10);
34 err += sn9c102_write_reg(cam, 0x00, 0x11);
35 err += sn9c102_write_reg(cam, 0x00, 0x14);
36 err += sn9c102_write_reg(cam, 0x20, 0x17);
37 err += sn9c102_write_reg(cam, 0x20, 0x19);
38 err += sn9c102_write_reg(cam, 0x09, 0x18);
39
40 err += sn9c102_i2c_write(cam, 0x02, 0x0c);
41 err += sn9c102_i2c_write(cam, 0x05, 0x5a);
42 err += sn9c102_i2c_write(cam, 0x06, 0x88);
43 err += sn9c102_i2c_write(cam, 0x07, 0x80);
44 err += sn9c102_i2c_write(cam, 0x10, 0x06);
45 err += sn9c102_i2c_write(cam, 0x11, 0x06);
46 err += sn9c102_i2c_write(cam, 0x12, 0x00);
47 err += sn9c102_i2c_write(cam, 0x14, 0x02);
48 err += sn9c102_i2c_write(cam, 0x13, 0x01);
49
50 msleep(400);
51
52 return err;
53}
54
55
56static int pas106b_get_ctrl(struct sn9c102_device* cam,
57 struct v4l2_control* ctrl)
58{
59 switch (ctrl->id) {
60 case V4L2_CID_EXPOSURE:
61 {
62 int r1 = sn9c102_i2c_read(cam, 0x03),
63 r2 = sn9c102_i2c_read(cam, 0x04);
64 if (r1 < 0 || r2 < 0)
65 return -EIO;
66 ctrl->value = (r1 << 4) | (r2 & 0x0f);
67 }
68 return 0;
69 case V4L2_CID_RED_BALANCE:
70 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0)
71 return -EIO;
72 ctrl->value &= 0x1f;
73 return 0;
74 case V4L2_CID_BLUE_BALANCE:
75 if ((ctrl->value = sn9c102_i2c_read(cam, 0x09)) < 0)
76 return -EIO;
77 ctrl->value &= 0x1f;
78 return 0;
79 case V4L2_CID_GAIN:
80 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0e)) < 0)
81 return -EIO;
82 ctrl->value &= 0x1f;
83 return 0;
84 case V4L2_CID_CONTRAST:
85 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0f)) < 0)
86 return -EIO;
87 ctrl->value &= 0x07;
88 return 0;
89 case SN9C102_V4L2_CID_GREEN_BALANCE:
90 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0a)) < 0)
91 return -EIO;
92 ctrl->value = (ctrl->value & 0x1f) << 1;
93 return 0;
94 case SN9C102_V4L2_CID_DAC_MAGNITUDE:
95 if ((ctrl->value = sn9c102_i2c_read(cam, 0x08)) < 0)
96 return -EIO;
97 ctrl->value &= 0xf8;
98 return 0;
99 default:
100 return -EINVAL;
101 }
102}
103
104
105static int pas106b_set_ctrl(struct sn9c102_device* cam,
106 const struct v4l2_control* ctrl)
107{
108 int err = 0;
109
110 switch (ctrl->id) {
111 case V4L2_CID_EXPOSURE:
112 err += sn9c102_i2c_write(cam, 0x03, ctrl->value >> 4);
113 err += sn9c102_i2c_write(cam, 0x04, ctrl->value & 0x0f);
114 break;
115 case V4L2_CID_RED_BALANCE:
116 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
117 break;
118 case V4L2_CID_BLUE_BALANCE:
119 err += sn9c102_i2c_write(cam, 0x09, ctrl->value);
120 break;
121 case V4L2_CID_GAIN:
122 err += sn9c102_i2c_write(cam, 0x0e, ctrl->value);
123 break;
124 case V4L2_CID_CONTRAST:
125 err += sn9c102_i2c_write(cam, 0x0f, ctrl->value);
126 break;
127 case SN9C102_V4L2_CID_GREEN_BALANCE:
128 err += sn9c102_i2c_write(cam, 0x0a, ctrl->value >> 1);
129 err += sn9c102_i2c_write(cam, 0x0b, ctrl->value >> 1);
130 break;
131 case SN9C102_V4L2_CID_DAC_MAGNITUDE:
132 err += sn9c102_i2c_write(cam, 0x08, ctrl->value << 3);
133 break;
134 default:
135 return -EINVAL;
136 }
137 err += sn9c102_i2c_write(cam, 0x13, 0x01);
138
139 return err ? -EIO : 0;
140}
141
142
143static int pas106b_set_crop(struct sn9c102_device* cam,
144 const struct v4l2_rect* rect)
145{
146 struct sn9c102_sensor* s = &pas106b;
147 int err = 0;
148 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4,
149 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
150
151 err += sn9c102_write_reg(cam, h_start, 0x12);
152 err += sn9c102_write_reg(cam, v_start, 0x13);
153
154 return err;
155}
156
157
158static int pas106b_set_pix_format(struct sn9c102_device* cam,
159 const struct v4l2_pix_format* pix)
160{
161 int err = 0;
162
163 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
164 err += sn9c102_write_reg(cam, 0x2c, 0x17);
165 else
166 err += sn9c102_write_reg(cam, 0x20, 0x17);
167
168 return err;
169}
170
171
172static struct sn9c102_sensor pas106b = {
173 .name = "PAS106B",
174 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
175 .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
176 .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ,
177 .interface = SN9C102_I2C_2WIRES,
178 .i2c_slave_id = 0x40,
179 .init = &pas106b_init,
180 .qctrl = {
181 {
182 .id = V4L2_CID_EXPOSURE,
183 .type = V4L2_CTRL_TYPE_INTEGER,
184 .name = "exposure",
185 .minimum = 0x125,
186 .maximum = 0xfff,
187 .step = 0x001,
188 .default_value = 0x140,
189 .flags = 0,
190 },
191 {
192 .id = V4L2_CID_GAIN,
193 .type = V4L2_CTRL_TYPE_INTEGER,
194 .name = "global gain",
195 .minimum = 0x00,
196 .maximum = 0x1f,
197 .step = 0x01,
198 .default_value = 0x0d,
199 .flags = 0,
200 },
201 {
202 .id = V4L2_CID_CONTRAST,
203 .type = V4L2_CTRL_TYPE_INTEGER,
204 .name = "contrast",
205 .minimum = 0x00,
206 .maximum = 0x07,
207 .step = 0x01,
208 .default_value = 0x00, /* 0x00~0x03 have same effect */
209 .flags = 0,
210 },
211 {
212 .id = V4L2_CID_RED_BALANCE,
213 .type = V4L2_CTRL_TYPE_INTEGER,
214 .name = "red balance",
215 .minimum = 0x00,
216 .maximum = 0x1f,
217 .step = 0x01,
218 .default_value = 0x04,
219 .flags = 0,
220 },
221 {
222 .id = V4L2_CID_BLUE_BALANCE,
223 .type = V4L2_CTRL_TYPE_INTEGER,
224 .name = "blue balance",
225 .minimum = 0x00,
226 .maximum = 0x1f,
227 .step = 0x01,
228 .default_value = 0x06,
229 .flags = 0,
230 },
231 {
232 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
233 .type = V4L2_CTRL_TYPE_INTEGER,
234 .name = "green balance",
235 .minimum = 0x00,
236 .maximum = 0x3e,
237 .step = 0x02,
238 .default_value = 0x02,
239 .flags = 0,
240 },
241 {
242 .id = SN9C102_V4L2_CID_DAC_MAGNITUDE,
243 .type = V4L2_CTRL_TYPE_INTEGER,
244 .name = "DAC magnitude",
245 .minimum = 0x00,
246 .maximum = 0x1f,
247 .step = 0x01,
248 .default_value = 0x01,
249 .flags = 0,
250 },
251 },
252 .get_ctrl = &pas106b_get_ctrl,
253 .set_ctrl = &pas106b_set_ctrl,
254 .cropcap = {
255 .bounds = {
256 .left = 0,
257 .top = 0,
258 .width = 352,
259 .height = 288,
260 },
261 .defrect = {
262 .left = 0,
263 .top = 0,
264 .width = 352,
265 .height = 288,
266 },
267 },
268 .set_crop = &pas106b_set_crop,
269 .pix_format = {
270 .width = 352,
271 .height = 288,
272 .pixelformat = V4L2_PIX_FMT_SBGGR8,
273 .priv = 8, /* we use this field as 'bits per pixel' */
274 },
275 .set_pix_format = &pas106b_set_pix_format
276};
277
278
279int sn9c102_probe_pas106b(struct sn9c102_device* cam)
280{
281 int r0 = 0, r1 = 0, err = 0;
282 unsigned int pid = 0;
283
284 /*
285 Minimal initialization to enable the I2C communication
286 NOTE: do NOT change the values!
287 */
288 err += sn9c102_write_reg(cam, 0x01, 0x01); /* sensor power down */
289 err += sn9c102_write_reg(cam, 0x00, 0x01); /* sensor power on */
290 err += sn9c102_write_reg(cam, 0x28, 0x17); /* sensor clock at 24 MHz */
291 if (err)
292 return -EIO;
293
294 r0 = sn9c102_i2c_try_read(cam, &pas106b, 0x00);
295 r1 = sn9c102_i2c_try_read(cam, &pas106b, 0x01);
296
297 if (r0 < 0 || r1 < 0)
298 return -EIO;
299
300 pid = (r0 << 11) | ((r1 & 0xf0) >> 4);
301 if (pid != 0x007)
302 return -ENODEV;
303
304 sn9c102_attach_sensor(cam, &pas106b);
305
306 return 0;
307}
diff --git a/drivers/usb/media/sn9c102_pas202bcb.c b/drivers/usb/media/sn9c102_pas202bcb.c
deleted file mode 100644
index 5ca54c7daaf2..000000000000
--- a/drivers/usb/media/sn9c102_pas202bcb.c
+++ /dev/null
@@ -1,293 +0,0 @@
1/***************************************************************************
2 * Plug-in for PAS202BCB image sensor connected to the SN9C10x PC Camera *
3 * Controllers *
4 * *
5 * Copyright (C) 2004 by Carlos Eduardo Medaglia Dyonisio *
6 * <medaglia@undl.org.br> *
7 * http://cadu.homelinux.com:8080/ *
8 * *
9 * DAC Magnitude, exposure and green gain controls added by *
10 * Luca Risolia <luca.risolia@studio.unibo.it> *
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU General Public License *
23 * along with this program; if not, write to the Free Software *
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
25 ***************************************************************************/
26
27#include <linux/delay.h>
28#include "sn9c102_sensor.h"
29
30
31static struct sn9c102_sensor pas202bcb;
32
33
34static int pas202bcb_init(struct sn9c102_device* cam)
35{
36 int err = 0;
37
38 err += sn9c102_write_reg(cam, 0x00, 0x10);
39 err += sn9c102_write_reg(cam, 0x00, 0x11);
40 err += sn9c102_write_reg(cam, 0x00, 0x14);
41 err += sn9c102_write_reg(cam, 0x20, 0x17);
42 err += sn9c102_write_reg(cam, 0x30, 0x19);
43 err += sn9c102_write_reg(cam, 0x09, 0x18);
44
45 err += sn9c102_i2c_write(cam, 0x02, 0x14);
46 err += sn9c102_i2c_write(cam, 0x03, 0x40);
47 err += sn9c102_i2c_write(cam, 0x0d, 0x2c);
48 err += sn9c102_i2c_write(cam, 0x0e, 0x01);
49 err += sn9c102_i2c_write(cam, 0x0f, 0xa9);
50 err += sn9c102_i2c_write(cam, 0x10, 0x08);
51 err += sn9c102_i2c_write(cam, 0x13, 0x63);
52 err += sn9c102_i2c_write(cam, 0x15, 0x70);
53 err += sn9c102_i2c_write(cam, 0x11, 0x01);
54
55 msleep(400);
56
57 return err;
58}
59
60
61static int pas202bcb_get_ctrl(struct sn9c102_device* cam,
62 struct v4l2_control* ctrl)
63{
64 switch (ctrl->id) {
65 case V4L2_CID_EXPOSURE:
66 {
67 int r1 = sn9c102_i2c_read(cam, 0x04),
68 r2 = sn9c102_i2c_read(cam, 0x05);
69 if (r1 < 0 || r2 < 0)
70 return -EIO;
71 ctrl->value = (r1 << 6) | (r2 & 0x3f);
72 }
73 return 0;
74 case V4L2_CID_RED_BALANCE:
75 if ((ctrl->value = sn9c102_i2c_read(cam, 0x09)) < 0)
76 return -EIO;
77 ctrl->value &= 0x0f;
78 return 0;
79 case V4L2_CID_BLUE_BALANCE:
80 if ((ctrl->value = sn9c102_i2c_read(cam, 0x07)) < 0)
81 return -EIO;
82 ctrl->value &= 0x0f;
83 return 0;
84 case V4L2_CID_GAIN:
85 if ((ctrl->value = sn9c102_i2c_read(cam, 0x10)) < 0)
86 return -EIO;
87 ctrl->value &= 0x1f;
88 return 0;
89 case SN9C102_V4L2_CID_GREEN_BALANCE:
90 if ((ctrl->value = sn9c102_i2c_read(cam, 0x08)) < 0)
91 return -EIO;
92 ctrl->value &= 0x0f;
93 return 0;
94 case SN9C102_V4L2_CID_DAC_MAGNITUDE:
95 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0)
96 return -EIO;
97 return 0;
98 default:
99 return -EINVAL;
100 }
101}
102
103
104static int pas202bcb_set_pix_format(struct sn9c102_device* cam,
105 const struct v4l2_pix_format* pix)
106{
107 int err = 0;
108
109 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
110 err += sn9c102_write_reg(cam, 0x24, 0x17);
111 else
112 err += sn9c102_write_reg(cam, 0x20, 0x17);
113
114 return err;
115}
116
117
118static int pas202bcb_set_ctrl(struct sn9c102_device* cam,
119 const struct v4l2_control* ctrl)
120{
121 int err = 0;
122
123 switch (ctrl->id) {
124 case V4L2_CID_EXPOSURE:
125 err += sn9c102_i2c_write(cam, 0x04, ctrl->value >> 6);
126 err += sn9c102_i2c_write(cam, 0x05, ctrl->value & 0x3f);
127 break;
128 case V4L2_CID_RED_BALANCE:
129 err += sn9c102_i2c_write(cam, 0x09, ctrl->value);
130 break;
131 case V4L2_CID_BLUE_BALANCE:
132 err += sn9c102_i2c_write(cam, 0x07, ctrl->value);
133 break;
134 case V4L2_CID_GAIN:
135 err += sn9c102_i2c_write(cam, 0x10, ctrl->value);
136 break;
137 case SN9C102_V4L2_CID_GREEN_BALANCE:
138 err += sn9c102_i2c_write(cam, 0x08, ctrl->value);
139 break;
140 case SN9C102_V4L2_CID_DAC_MAGNITUDE:
141 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
142 break;
143 default:
144 return -EINVAL;
145 }
146 err += sn9c102_i2c_write(cam, 0x11, 0x01);
147
148 return err ? -EIO : 0;
149}
150
151
152static int pas202bcb_set_crop(struct sn9c102_device* cam,
153 const struct v4l2_rect* rect)
154{
155 struct sn9c102_sensor* s = &pas202bcb;
156 int err = 0;
157 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4,
158 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
159
160 err += sn9c102_write_reg(cam, h_start, 0x12);
161 err += sn9c102_write_reg(cam, v_start, 0x13);
162
163 return err;
164}
165
166
167static struct sn9c102_sensor pas202bcb = {
168 .name = "PAS202BCB",
169 .maintainer = "Carlos Eduardo Medaglia Dyonisio "
170 "<medaglia@undl.org.br>",
171 .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
172 .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ,
173 .interface = SN9C102_I2C_2WIRES,
174 .i2c_slave_id = 0x40,
175 .init = &pas202bcb_init,
176 .qctrl = {
177 {
178 .id = V4L2_CID_EXPOSURE,
179 .type = V4L2_CTRL_TYPE_INTEGER,
180 .name = "exposure",
181 .minimum = 0x01e5,
182 .maximum = 0x3fff,
183 .step = 0x0001,
184 .default_value = 0x01e5,
185 .flags = 0,
186 },
187 {
188 .id = V4L2_CID_GAIN,
189 .type = V4L2_CTRL_TYPE_INTEGER,
190 .name = "global gain",
191 .minimum = 0x00,
192 .maximum = 0x1f,
193 .step = 0x01,
194 .default_value = 0x0c,
195 .flags = 0,
196 },
197 {
198 .id = V4L2_CID_RED_BALANCE,
199 .type = V4L2_CTRL_TYPE_INTEGER,
200 .name = "red balance",
201 .minimum = 0x00,
202 .maximum = 0x0f,
203 .step = 0x01,
204 .default_value = 0x01,
205 .flags = 0,
206 },
207 {
208 .id = V4L2_CID_BLUE_BALANCE,
209 .type = V4L2_CTRL_TYPE_INTEGER,
210 .name = "blue balance",
211 .minimum = 0x00,
212 .maximum = 0x0f,
213 .step = 0x01,
214 .default_value = 0x05,
215 .flags = 0,
216 },
217 {
218 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
219 .type = V4L2_CTRL_TYPE_INTEGER,
220 .name = "green balance",
221 .minimum = 0x00,
222 .maximum = 0x0f,
223 .step = 0x01,
224 .default_value = 0x00,
225 .flags = 0,
226 },
227 {
228 .id = SN9C102_V4L2_CID_DAC_MAGNITUDE,
229 .type = V4L2_CTRL_TYPE_INTEGER,
230 .name = "DAC magnitude",
231 .minimum = 0x00,
232 .maximum = 0xff,
233 .step = 0x01,
234 .default_value = 0x04,
235 .flags = 0,
236 },
237 },
238 .get_ctrl = &pas202bcb_get_ctrl,
239 .set_ctrl = &pas202bcb_set_ctrl,
240 .cropcap = {
241 .bounds = {
242 .left = 0,
243 .top = 0,
244 .width = 640,
245 .height = 480,
246 },
247 .defrect = {
248 .left = 0,
249 .top = 0,
250 .width = 640,
251 .height = 480,
252 },
253 },
254 .set_crop = &pas202bcb_set_crop,
255 .pix_format = {
256 .width = 640,
257 .height = 480,
258 .pixelformat = V4L2_PIX_FMT_SBGGR8,
259 .priv = 8,
260 },
261 .set_pix_format = &pas202bcb_set_pix_format
262};
263
264
265int sn9c102_probe_pas202bcb(struct sn9c102_device* cam)
266{
267 int r0 = 0, r1 = 0, err = 0;
268 unsigned int pid = 0;
269
270 /*
271 * Minimal initialization to enable the I2C communication
272 * NOTE: do NOT change the values!
273 */
274 err += sn9c102_write_reg(cam, 0x01, 0x01); /* sensor power down */
275 err += sn9c102_write_reg(cam, 0x40, 0x01); /* sensor power on */
276 err += sn9c102_write_reg(cam, 0x28, 0x17); /* sensor clock at 24 MHz */
277 if (err)
278 return -EIO;
279
280 r0 = sn9c102_i2c_try_read(cam, &pas202bcb, 0x00);
281 r1 = sn9c102_i2c_try_read(cam, &pas202bcb, 0x01);
282
283 if (r0 < 0 || r1 < 0)
284 return -EIO;
285
286 pid = (r0 << 4) | ((r1 & 0xf0) >> 4);
287 if (pid != 0x017)
288 return -ENODEV;
289
290 sn9c102_attach_sensor(cam, &pas202bcb);
291
292 return 0;
293}
diff --git a/drivers/usb/media/sn9c102_sensor.h b/drivers/usb/media/sn9c102_sensor.h
deleted file mode 100644
index 7d953b24f2f2..000000000000
--- a/drivers/usb/media/sn9c102_sensor.h
+++ /dev/null
@@ -1,388 +0,0 @@
1/***************************************************************************
2 * API for image sensors connected to the SN9C10x PC Camera Controllers *
3 * *
4 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _SN9C102_SENSOR_H_
22#define _SN9C102_SENSOR_H_
23
24#include <linux/usb.h>
25#include <linux/videodev.h>
26#include <linux/device.h>
27#include <linux/stddef.h>
28#include <linux/errno.h>
29#include <asm/types.h>
30
31struct sn9c102_device;
32struct sn9c102_sensor;
33
34/*****************************************************************************/
35
36/*
37 OVERVIEW.
38 This is a small interface that allows you to add support for any CCD/CMOS
39 image sensors connected to the SN9C10X bridges. The entire API is documented
40 below. In the most general case, to support a sensor there are three steps
41 you have to follow:
42 1) define the main "sn9c102_sensor" structure by setting the basic fields;
43 2) write a probing function to be called by the core module when the USB
44 camera is recognized, then add both the USB ids and the name of that
45 function to the two corresponding tables SENSOR_TABLE and ID_TABLE (see
46 below);
47 3) implement the methods that you want/need (and fill the rest of the main
48 structure accordingly).
49 "sn9c102_pas106b.c" is an example of all this stuff. Remember that you do
50 NOT need to touch the source code of the core module for the things to work
51 properly, unless you find bugs or flaws in it. Finally, do not forget to
52 read the V4L2 API for completeness.
53*/
54
55/*****************************************************************************/
56
57/*
58 Probing functions: on success, you must attach the sensor to the camera
59 by calling sn9c102_attach_sensor() provided below.
60 To enable the I2C communication, you might need to perform a really basic
61 initialization of the SN9C10X chip by using the write function declared
62 ahead.
63 Functions must return 0 on success, the appropriate error otherwise.
64*/
65extern int sn9c102_probe_hv7131d(struct sn9c102_device* cam);
66extern int sn9c102_probe_mi0343(struct sn9c102_device* cam);
67extern int sn9c102_probe_ov7630(struct sn9c102_device* cam);
68extern int sn9c102_probe_pas106b(struct sn9c102_device* cam);
69extern int sn9c102_probe_pas202bcb(struct sn9c102_device* cam);
70extern int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam);
71extern int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam);
72
73/*
74 Add the above entries to this table. Be sure to add the entry in the right
75 place, since, on failure, the next probing routine is called according to
76 the order of the list below, from top to bottom.
77*/
78#define SN9C102_SENSOR_TABLE \
79static int (*sn9c102_sensor_table[])(struct sn9c102_device*) = { \
80 &sn9c102_probe_mi0343, /* strong detection based on SENSOR ids */ \
81 &sn9c102_probe_pas106b, /* strong detection based on SENSOR ids */ \
82 &sn9c102_probe_pas202bcb, /* strong detection based on SENSOR ids */ \
83 &sn9c102_probe_hv7131d, /* strong detection based on SENSOR ids */ \
84 &sn9c102_probe_ov7630, /* detection mostly based on USB pid/vid */ \
85 &sn9c102_probe_tas5110c1b, /* detection based on USB pid/vid */ \
86 &sn9c102_probe_tas5130d1b, /* detection based on USB pid/vid */ \
87 NULL, \
88};
89
90/* Attach a probed sensor to the camera. */
91extern void
92sn9c102_attach_sensor(struct sn9c102_device* cam,
93 struct sn9c102_sensor* sensor);
94
95/*
96 Each SN9C10x camera has proper PID/VID identifiers.
97 SN9C103 supports multiple interfaces, but we only handle the video class
98 interface.
99*/
100#define SN9C102_USB_DEVICE(vend, prod, intclass) \
101 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
102 USB_DEVICE_ID_MATCH_INT_CLASS, \
103 .idVendor = (vend), \
104 .idProduct = (prod), \
105 .bInterfaceClass = (intclass)
106
107#define SN9C102_ID_TABLE \
108static const struct usb_device_id sn9c102_id_table[] = { \
109 { USB_DEVICE(0x0c45, 0x6001), }, /* TAS5110C1B */ \
110 { USB_DEVICE(0x0c45, 0x6005), }, /* TAS5110C1B */ \
111 { USB_DEVICE(0x0c45, 0x6009), }, /* PAS106B */ \
112 { USB_DEVICE(0x0c45, 0x600d), }, /* PAS106B */ \
113 { USB_DEVICE(0x0c45, 0x6024), }, \
114 { USB_DEVICE(0x0c45, 0x6025), }, /* TAS5130D1B and TAS5110C1B */ \
115 { USB_DEVICE(0x0c45, 0x6028), }, /* PAS202BCB */ \
116 { USB_DEVICE(0x0c45, 0x6029), }, /* PAS106B */ \
117 { USB_DEVICE(0x0c45, 0x602a), }, /* HV7131D */ \
118 { USB_DEVICE(0x0c45, 0x602b), }, /* MI-0343 */ \
119 { USB_DEVICE(0x0c45, 0x602c), }, /* OV7630 */ \
120 { USB_DEVICE(0x0c45, 0x602d), }, \
121 { USB_DEVICE(0x0c45, 0x602e), }, /* OV7630 */ \
122 { USB_DEVICE(0x0c45, 0x6030), }, /* MI03x */ \
123 { SN9C102_USB_DEVICE(0x0c45, 0x6080, 0xff), }, \
124 { SN9C102_USB_DEVICE(0x0c45, 0x6082, 0xff), }, /* MI0343 & MI0360 */ \
125 { SN9C102_USB_DEVICE(0x0c45, 0x6083, 0xff), }, /* HV7131[D|E1] */ \
126 { SN9C102_USB_DEVICE(0x0c45, 0x6088, 0xff), }, \
127 { SN9C102_USB_DEVICE(0x0c45, 0x608a, 0xff), }, \
128 { SN9C102_USB_DEVICE(0x0c45, 0x608b, 0xff), }, \
129 { SN9C102_USB_DEVICE(0x0c45, 0x608c, 0xff), }, /* HV7131x */ \
130 { SN9C102_USB_DEVICE(0x0c45, 0x608e, 0xff), }, /* CIS-VF10 */ \
131 { SN9C102_USB_DEVICE(0x0c45, 0x608f, 0xff), }, /* OV7630 */ \
132 { SN9C102_USB_DEVICE(0x0c45, 0x60a0, 0xff), }, \
133 { SN9C102_USB_DEVICE(0x0c45, 0x60a2, 0xff), }, \
134 { SN9C102_USB_DEVICE(0x0c45, 0x60a3, 0xff), }, \
135 { SN9C102_USB_DEVICE(0x0c45, 0x60a8, 0xff), }, /* PAS106B */ \
136 { SN9C102_USB_DEVICE(0x0c45, 0x60aa, 0xff), }, /* TAS5130D1B */ \
137 { SN9C102_USB_DEVICE(0x0c45, 0x60ab, 0xff), }, /* TAS5110C1B */ \
138 { SN9C102_USB_DEVICE(0x0c45, 0x60ac, 0xff), }, \
139 { SN9C102_USB_DEVICE(0x0c45, 0x60ae, 0xff), }, \
140 { SN9C102_USB_DEVICE(0x0c45, 0x60af, 0xff), }, /* PAS202BCB */ \
141 { SN9C102_USB_DEVICE(0x0c45, 0x60b0, 0xff), }, /* OV7630 (?) */ \
142 { SN9C102_USB_DEVICE(0x0c45, 0x60b2, 0xff), }, \
143 { SN9C102_USB_DEVICE(0x0c45, 0x60b3, 0xff), }, \
144 { SN9C102_USB_DEVICE(0x0c45, 0x60b8, 0xff), }, \
145 { SN9C102_USB_DEVICE(0x0c45, 0x60ba, 0xff), }, \
146 { SN9C102_USB_DEVICE(0x0c45, 0x60bb, 0xff), }, \
147 { SN9C102_USB_DEVICE(0x0c45, 0x60bc, 0xff), }, \
148 { SN9C102_USB_DEVICE(0x0c45, 0x60be, 0xff), }, \
149 { } \
150};
151
152/*****************************************************************************/
153
154/*
155 Read/write routines: they always return -1 on error, 0 or the read value
156 otherwise. NOTE that a real read operation is not supported by the SN9C10X
157 chip for some of its registers. To work around this problem, a pseudo-read
158 call is provided instead: it returns the last successfully written value
159 on the register (0 if it has never been written), the usual -1 on error.
160*/
161
162/* The "try" I2C I/O versions are used when probing the sensor */
163extern int sn9c102_i2c_try_write(struct sn9c102_device*,struct sn9c102_sensor*,
164 u8 address, u8 value);
165extern int sn9c102_i2c_try_read(struct sn9c102_device*,struct sn9c102_sensor*,
166 u8 address);
167
168/*
169 These must be used if and only if the sensor doesn't implement the standard
170 I2C protocol. There are a number of good reasons why you must use the
171 single-byte versions of these functions: do not abuse. The first function
172 writes n bytes, from data0 to datan, to registers 0x09 - 0x09+n of SN9C10X
173 chip. The second one programs the registers 0x09 and 0x10 with data0 and
174 data1, and places the n bytes read from the sensor register table in the
175 buffer pointed by 'buffer'. Both the functions return -1 on error; the write
176 version returns 0 on success, while the read version returns the first read
177 byte.
178*/
179extern int sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
180 struct sn9c102_sensor* sensor, u8 n,
181 u8 data0, u8 data1, u8 data2, u8 data3,
182 u8 data4, u8 data5);
183extern int sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
184 struct sn9c102_sensor* sensor, u8 data0,
185 u8 data1, u8 n, u8 buffer[]);
186
187/* To be used after the sensor struct has been attached to the camera struct */
188extern int sn9c102_i2c_write(struct sn9c102_device*, u8 address, u8 value);
189extern int sn9c102_i2c_read(struct sn9c102_device*, u8 address);
190
191/* I/O on registers in the bridge. Could be used by the sensor methods too */
192extern int sn9c102_write_regs(struct sn9c102_device*, u8* buff, u16 index);
193extern int sn9c102_write_reg(struct sn9c102_device*, u8 value, u16 index);
194extern int sn9c102_pread_reg(struct sn9c102_device*, u16 index);
195
196/*
197 NOTE: there are no exported debugging functions. To uniform the output you
198 must use the dev_info()/dev_warn()/dev_err() macros defined in device.h,
199 already included here, the argument being the struct device '&usbdev->dev'
200 of the sensor structure. Do NOT use these macros before the sensor is
201 attached or the kernel will crash! However, you should not need to notify
202 the user about common errors or other messages, since this is done by the
203 master module.
204*/
205
206/*****************************************************************************/
207
208enum sn9c102_i2c_sysfs_ops {
209 SN9C102_I2C_READ = 0x01,
210 SN9C102_I2C_WRITE = 0x02,
211};
212
213enum sn9c102_i2c_frequency { /* sensors may support both the frequencies */
214 SN9C102_I2C_100KHZ = 0x01,
215 SN9C102_I2C_400KHZ = 0x02,
216};
217
218enum sn9c102_i2c_interface {
219 SN9C102_I2C_2WIRES,
220 SN9C102_I2C_3WIRES,
221};
222
223#define SN9C102_MAX_CTRLS V4L2_CID_LASTP1-V4L2_CID_BASE+10
224
225struct sn9c102_sensor {
226 char name[32], /* sensor name */
227 maintainer[64]; /* name of the mantainer <email> */
228
229 /* Supported operations through the 'sysfs' interface */
230 enum sn9c102_i2c_sysfs_ops sysfs_ops;
231
232 /*
233 These sensor capabilities must be provided if the SN9C10X controller
234 needs to communicate through the sensor serial interface by using
235 at least one of the i2c functions available.
236 */
237 enum sn9c102_i2c_frequency frequency;
238 enum sn9c102_i2c_interface interface;
239
240 /*
241 This identifier must be provided if the image sensor implements
242 the standard I2C protocol.
243 */
244 u8 i2c_slave_id; /* reg. 0x09 */
245
246 /*
247 NOTE: Where not noted,most of the functions below are not mandatory.
248 Set to null if you do not implement them. If implemented,
249 they must return 0 on success, the proper error otherwise.
250 */
251
252 int (*init)(struct sn9c102_device* cam);
253 /*
254 This function will be called after the sensor has been attached.
255 It should be used to initialize the sensor only, but may also
256 configure part of the SN9C10X chip if necessary. You don't need to
257 setup picture settings like brightness, contrast, etc.. here, if
258 the corrisponding controls are implemented (see below), since
259 they are adjusted in the core driver by calling the set_ctrl()
260 method after init(), where the arguments are the default values
261 specified in the v4l2_queryctrl list of supported controls;
262 Same suggestions apply for other settings, _if_ the corresponding
263 methods are present; if not, the initialization must configure the
264 sensor according to the default configuration structures below.
265 */
266
267 struct v4l2_queryctrl qctrl[SN9C102_MAX_CTRLS];
268 /*
269 Optional list of default controls, defined as indicated in the
270 V4L2 API. Menu type controls are not handled by this interface.
271 */
272
273 int (*get_ctrl)(struct sn9c102_device* cam, struct v4l2_control* ctrl);
274 int (*set_ctrl)(struct sn9c102_device* cam,
275 const struct v4l2_control* ctrl);
276 /*
277 You must implement at least the set_ctrl method if you have defined
278 the list above. The returned value must follow the V4L2
279 specifications for the VIDIOC_G|C_CTRL ioctls. V4L2_CID_H|VCENTER
280 are not supported by this driver, so do not implement them. Also,
281 you don't have to check whether the passed values are out of bounds,
282 given that this is done by the core module.
283 */
284
285 struct v4l2_cropcap cropcap;
286 /*
287 Think the image sensor as a grid of R,G,B monochromatic pixels
288 disposed according to a particular Bayer pattern, which describes
289 the complete array of pixels, from (0,0) to (xmax, ymax). We will
290 use this coordinate system from now on. It is assumed the sensor
291 chip can be programmed to capture/transmit a subsection of that
292 array of pixels: we will call this subsection "active window".
293 It is not always true that the largest achievable active window can
294 cover the whole array of pixels. The V4L2 API defines another
295 area called "source rectangle", which, in turn, is a subrectangle of
296 the active window. The SN9C10X chip is always programmed to read the
297 source rectangle.
298 The bounds of both the active window and the source rectangle are
299 specified in the cropcap substructures 'bounds' and 'defrect'.
300 By default, the source rectangle should cover the largest possible
301 area. Again, it is not always true that the largest source rectangle
302 can cover the entire active window, although it is a rare case for
303 the hardware we have. The bounds of the source rectangle _must_ be
304 multiple of 16 and must use the same coordinate system as indicated
305 before; their centers shall align initially.
306 If necessary, the sensor chip must be initialized during init() to
307 set the bounds of the active sensor window; however, by default, it
308 usually covers the largest achievable area (maxwidth x maxheight)
309 of pixels, so no particular initialization is needed, if you have
310 defined the correct default bounds in the structures.
311 See the V4L2 API for further details.
312 NOTE: once you have defined the bounds of the active window
313 (struct cropcap.bounds) you must not change them.anymore.
314 Only 'bounds' and 'defrect' fields are mandatory, other fields
315 will be ignored.
316 */
317
318 int (*set_crop)(struct sn9c102_device* cam,
319 const struct v4l2_rect* rect);
320 /*
321 To be called on VIDIOC_C_SETCROP. The core module always calls a
322 default routine which configures the appropriate SN9C10X regs (also
323 scaling), but you may need to override/adjust specific stuff.
324 'rect' contains width and height values that are multiple of 16: in
325 case you override the default function, you always have to program
326 the chip to match those values; on error return the corresponding
327 error code without rolling back.
328 NOTE: in case, you must program the SN9C10X chip to get rid of
329 blank pixels or blank lines at the _start_ of each line or
330 frame after each HSYNC or VSYNC, so that the image starts with
331 real RGB data (see regs 0x12, 0x13) (having set H_SIZE and,
332 V_SIZE you don't have to care about blank pixels or blank
333 lines at the end of each line or frame).
334 */
335
336 struct v4l2_pix_format pix_format;
337 /*
338 What you have to define here are: 1) initial 'width' and 'height' of
339 the target rectangle 2) the initial 'pixelformat', which can be
340 either V4L2_PIX_FMT_SN9C10X (for compressed video) or
341 V4L2_PIX_FMT_SBGGR8 3) 'priv', which we'll be used to indicate the
342 number of bits per pixel for uncompressed video, 8 or 9 (despite the
343 current value of 'pixelformat').
344 NOTE 1: both 'width' and 'height' _must_ be either 1/1 or 1/2 or 1/4
345 of cropcap.defrect.width and cropcap.defrect.height. I
346 suggest 1/1.
347 NOTE 2: The initial compression quality is defined by the first bit
348 of reg 0x17 during the initialization of the image sensor.
349 NOTE 3: as said above, you have to program the SN9C10X chip to get
350 rid of any blank pixels, so that the output of the sensor
351 matches the RGB bayer sequence (i.e. BGBGBG...GRGRGR).
352 */
353
354 int (*set_pix_format)(struct sn9c102_device* cam,
355 const struct v4l2_pix_format* pix);
356 /*
357 To be called on VIDIOC_S_FMT, when switching from the SBGGR8 to
358 SN9C10X pixel format or viceversa. On error return the corresponding
359 error code without rolling back.
360 */
361
362 const struct usb_device* usbdev;
363 /*
364 Points to the usb_device struct after the sensor is attached.
365 Do not touch unless you know what you are doing.
366 */
367
368 /*
369 Do NOT write to the data below, it's READ ONLY. It is used by the
370 core module to store successfully updated values of the above
371 settings, for rollbacks..etc..in case of errors during atomic I/O
372 */
373 struct v4l2_queryctrl _qctrl[SN9C102_MAX_CTRLS];
374 struct v4l2_rect _rect;
375};
376
377/*****************************************************************************/
378
379/* Private ioctl's for control settings supported by some image sensors */
380#define SN9C102_V4L2_CID_DAC_MAGNITUDE V4L2_CID_PRIVATE_BASE
381#define SN9C102_V4L2_CID_GREEN_BALANCE V4L2_CID_PRIVATE_BASE + 1
382#define SN9C102_V4L2_CID_RESET_LEVEL V4L2_CID_PRIVATE_BASE + 2
383#define SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE V4L2_CID_PRIVATE_BASE + 3
384#define SN9C102_V4L2_CID_GAMMA V4L2_CID_PRIVATE_BASE + 4
385#define SN9C102_V4L2_CID_BAND_FILTER V4L2_CID_PRIVATE_BASE + 5
386#define SN9C102_V4L2_CID_BRIGHT_LEVEL V4L2_CID_PRIVATE_BASE + 6
387
388#endif /* _SN9C102_SENSOR_H_ */
diff --git a/drivers/usb/media/sn9c102_tas5110c1b.c b/drivers/usb/media/sn9c102_tas5110c1b.c
deleted file mode 100644
index 32ddf236cafe..000000000000
--- a/drivers/usb/media/sn9c102_tas5110c1b.c
+++ /dev/null
@@ -1,155 +0,0 @@
1/***************************************************************************
2 * Plug-in for TAS5110C1B image sensor connected to the SN9C10x PC Camera *
3 * Controllers *
4 * *
5 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#include "sn9c102_sensor.h"
23
24
25static struct sn9c102_sensor tas5110c1b;
26
27
28static int tas5110c1b_init(struct sn9c102_device* cam)
29{
30 int err = 0;
31
32 err += sn9c102_write_reg(cam, 0x01, 0x01);
33 err += sn9c102_write_reg(cam, 0x44, 0x01);
34 err += sn9c102_write_reg(cam, 0x00, 0x10);
35 err += sn9c102_write_reg(cam, 0x00, 0x11);
36 err += sn9c102_write_reg(cam, 0x0a, 0x14);
37 err += sn9c102_write_reg(cam, 0x60, 0x17);
38 err += sn9c102_write_reg(cam, 0x06, 0x18);
39 err += sn9c102_write_reg(cam, 0xfb, 0x19);
40
41 err += sn9c102_i2c_write(cam, 0xc0, 0x80);
42
43 return err;
44}
45
46
47static int tas5110c1b_set_ctrl(struct sn9c102_device* cam,
48 const struct v4l2_control* ctrl)
49{
50 int err = 0;
51
52 switch (ctrl->id) {
53 case V4L2_CID_GAIN:
54 err += sn9c102_i2c_write(cam, 0x20, 0xf6 - ctrl->value);
55 break;
56 default:
57 return -EINVAL;
58 }
59
60 return err ? -EIO : 0;
61}
62
63
64static int tas5110c1b_set_crop(struct sn9c102_device* cam,
65 const struct v4l2_rect* rect)
66{
67 struct sn9c102_sensor* s = &tas5110c1b;
68 int err = 0;
69 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 69,
70 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 9;
71
72 err += sn9c102_write_reg(cam, h_start, 0x12);
73 err += sn9c102_write_reg(cam, v_start, 0x13);
74
75 /* Don't change ! */
76 err += sn9c102_write_reg(cam, 0x14, 0x1a);
77 err += sn9c102_write_reg(cam, 0x0a, 0x1b);
78 err += sn9c102_write_reg(cam, sn9c102_pread_reg(cam, 0x19), 0x19);
79
80 return err;
81}
82
83
84static int tas5110c1b_set_pix_format(struct sn9c102_device* cam,
85 const struct v4l2_pix_format* pix)
86{
87 int err = 0;
88
89 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
90 err += sn9c102_write_reg(cam, 0x2b, 0x19);
91 else
92 err += sn9c102_write_reg(cam, 0xfb, 0x19);
93
94 return err;
95}
96
97
98static struct sn9c102_sensor tas5110c1b = {
99 .name = "TAS5110C1B",
100 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
101 .sysfs_ops = SN9C102_I2C_WRITE,
102 .frequency = SN9C102_I2C_100KHZ,
103 .interface = SN9C102_I2C_3WIRES,
104 .init = &tas5110c1b_init,
105 .qctrl = {
106 {
107 .id = V4L2_CID_GAIN,
108 .type = V4L2_CTRL_TYPE_INTEGER,
109 .name = "global gain",
110 .minimum = 0x00,
111 .maximum = 0xf6,
112 .step = 0x01,
113 .default_value = 0x40,
114 .flags = 0,
115 },
116 },
117 .set_ctrl = &tas5110c1b_set_ctrl,
118 .cropcap = {
119 .bounds = {
120 .left = 0,
121 .top = 0,
122 .width = 352,
123 .height = 288,
124 },
125 .defrect = {
126 .left = 0,
127 .top = 0,
128 .width = 352,
129 .height = 288,
130 },
131 },
132 .set_crop = &tas5110c1b_set_crop,
133 .pix_format = {
134 .width = 352,
135 .height = 288,
136 .pixelformat = V4L2_PIX_FMT_SBGGR8,
137 .priv = 8,
138 },
139 .set_pix_format = &tas5110c1b_set_pix_format
140};
141
142
143int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam)
144{
145 /* This sensor has no identifiers, so let's attach it anyway */
146 sn9c102_attach_sensor(cam, &tas5110c1b);
147
148 /* Sensor detection is based on USB pid/vid */
149 if (le16_to_cpu(tas5110c1b.usbdev->descriptor.idProduct) != 0x6001 &&
150 le16_to_cpu(tas5110c1b.usbdev->descriptor.idProduct) != 0x6005 &&
151 le16_to_cpu(tas5110c1b.usbdev->descriptor.idProduct) != 0x60ab)
152 return -ENODEV;
153
154 return 0;
155}
diff --git a/drivers/usb/media/sn9c102_tas5130d1b.c b/drivers/usb/media/sn9c102_tas5130d1b.c
deleted file mode 100644
index a0728f0ae00c..000000000000
--- a/drivers/usb/media/sn9c102_tas5130d1b.c
+++ /dev/null
@@ -1,165 +0,0 @@
1/***************************************************************************
2 * Plug-in for TAS5130D1B image sensor connected to the SN9C10x PC Camera *
3 * Controllers *
4 * *
5 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#include "sn9c102_sensor.h"
23
24
25static struct sn9c102_sensor tas5130d1b;
26
27
28static int tas5130d1b_init(struct sn9c102_device* cam)
29{
30 int err = 0;
31
32 err += sn9c102_write_reg(cam, 0x01, 0x01);
33 err += sn9c102_write_reg(cam, 0x20, 0x17);
34 err += sn9c102_write_reg(cam, 0x04, 0x01);
35 err += sn9c102_write_reg(cam, 0x01, 0x10);
36 err += sn9c102_write_reg(cam, 0x00, 0x11);
37 err += sn9c102_write_reg(cam, 0x00, 0x14);
38 err += sn9c102_write_reg(cam, 0x60, 0x17);
39 err += sn9c102_write_reg(cam, 0x07, 0x18);
40
41 return err;
42}
43
44
45static int tas5130d1b_set_ctrl(struct sn9c102_device* cam,
46 const struct v4l2_control* ctrl)
47{
48 int err = 0;
49
50 switch (ctrl->id) {
51 case V4L2_CID_GAIN:
52 err += sn9c102_i2c_write(cam, 0x20, 0xf6 - ctrl->value);
53 break;
54 case V4L2_CID_EXPOSURE:
55 err += sn9c102_i2c_write(cam, 0x40, 0x47 - ctrl->value);
56 break;
57 default:
58 return -EINVAL;
59 }
60
61 return err ? -EIO : 0;
62}
63
64
65static int tas5130d1b_set_crop(struct sn9c102_device* cam,
66 const struct v4l2_rect* rect)
67{
68 struct sn9c102_sensor* s = &tas5130d1b;
69 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 104,
70 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 12;
71 int err = 0;
72
73 err += sn9c102_write_reg(cam, h_start, 0x12);
74 err += sn9c102_write_reg(cam, v_start, 0x13);
75
76 /* Do NOT change! */
77 err += sn9c102_write_reg(cam, 0x1f, 0x1a);
78 err += sn9c102_write_reg(cam, 0x1a, 0x1b);
79 err += sn9c102_write_reg(cam, sn9c102_pread_reg(cam, 0x19), 0x19);
80
81 return err;
82}
83
84
85static int tas5130d1b_set_pix_format(struct sn9c102_device* cam,
86 const struct v4l2_pix_format* pix)
87{
88 int err = 0;
89
90 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
91 err += sn9c102_write_reg(cam, 0x63, 0x19);
92 else
93 err += sn9c102_write_reg(cam, 0xf3, 0x19);
94
95 return err;
96}
97
98
99static struct sn9c102_sensor tas5130d1b = {
100 .name = "TAS5130D1B",
101 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
102 .sysfs_ops = SN9C102_I2C_WRITE,
103 .frequency = SN9C102_I2C_100KHZ,
104 .interface = SN9C102_I2C_3WIRES,
105 .init = &tas5130d1b_init,
106 .qctrl = {
107 {
108 .id = V4L2_CID_GAIN,
109 .type = V4L2_CTRL_TYPE_INTEGER,
110 .name = "global gain",
111 .minimum = 0x00,
112 .maximum = 0xf6,
113 .step = 0x02,
114 .default_value = 0x00,
115 .flags = 0,
116 },
117 {
118 .id = V4L2_CID_EXPOSURE,
119 .type = V4L2_CTRL_TYPE_INTEGER,
120 .name = "exposure",
121 .minimum = 0x00,
122 .maximum = 0x47,
123 .step = 0x01,
124 .default_value = 0x00,
125 .flags = 0,
126 },
127 },
128 .set_ctrl = &tas5130d1b_set_ctrl,
129 .cropcap = {
130 .bounds = {
131 .left = 0,
132 .top = 0,
133 .width = 640,
134 .height = 480,
135 },
136 .defrect = {
137 .left = 0,
138 .top = 0,
139 .width = 640,
140 .height = 480,
141 },
142 },
143 .set_crop = &tas5130d1b_set_crop,
144 .pix_format = {
145 .width = 640,
146 .height = 480,
147 .pixelformat = V4L2_PIX_FMT_SBGGR8,
148 .priv = 8,
149 },
150 .set_pix_format = &tas5130d1b_set_pix_format
151};
152
153
154int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam)
155{
156 /* This sensor has no identifiers, so let's attach it anyway */
157 sn9c102_attach_sensor(cam, &tas5130d1b);
158
159 /* Sensor detection is based on USB pid/vid */
160 if (le16_to_cpu(tas5130d1b.usbdev->descriptor.idProduct) != 0x6025 &&
161 le16_to_cpu(tas5130d1b.usbdev->descriptor.idProduct) != 0x60aa)
162 return -ENODEV;
163
164 return 0;
165}
diff --git a/drivers/usb/media/stv680.c b/drivers/usb/media/stv680.c
deleted file mode 100644
index b497a6a0a206..000000000000
--- a/drivers/usb/media/stv680.c
+++ /dev/null
@@ -1,1510 +0,0 @@
1/*
2 * STV0680 USB Camera Driver, by Kevin Sisson (kjsisson@bellsouth.net)
3 *
4 * Thanks to STMicroelectronics for information on the usb commands, and
5 * to Steve Miller at STM for his help and encouragement while I was
6 * writing this driver.
7 *
8 * This driver is based heavily on the
9 * Endpoints (formerly known as AOX) se401 USB Camera Driver
10 * Copyright (c) 2000 Jeroen B. Vreeken (pe1rxq@amsat.org)
11 *
12 * Still somewhat based on the Linux ov511 driver.
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 * History:
29 * ver 0.1 October, 2001. Initial attempt.
30 *
31 * ver 0.2 November, 2001. Fixed asbility to resize, added brightness
32 * function, made more stable (?)
33 *
34 * ver 0.21 Nov, 2001. Added gamma correction and white balance,
35 * due to Alexander Schwartz. Still trying to
36 * improve stablility. Moved stuff into stv680.h
37 *
38 * ver 0.22 Nov, 2001. Added sharpen function (by Michael Sweet,
39 * mike@easysw.com) from GIMP, also used in pencam.
40 * Simple, fast, good integer math routine.
41 *
42 * ver 0.23 Dec, 2001 (gkh)
43 * Took out sharpen function, ran code through
44 * Lindent, and did other minor tweaks to get
45 * things to work properly with 2.5.1
46 *
47 * ver 0.24 Jan, 2002 (kjs)
48 * Fixed the problem with webcam crashing after
49 * two pictures. Changed the way pic is halved to
50 * improve quality. Got rid of green line around
51 * frame. Fix brightness reset when changing size
52 * bug. Adjusted gamma filters slightly.
53 *
54 * ver 0.25 Jan, 2002 (kjs)
55 * Fixed a bug in which the driver sometimes attempted
56 * to set to a non-supported size. This allowed
57 * gnomemeeting to work.
58 * Fixed proc entry removal bug.
59 */
60
61#include <linux/config.h>
62#include <linux/module.h>
63#include <linux/init.h>
64#include <linux/vmalloc.h>
65#include <linux/slab.h>
66#include <linux/pagemap.h>
67#include <linux/errno.h>
68#include <linux/videodev.h>
69#include <linux/usb.h>
70
71#include "stv680.h"
72
73static int video_nr = -1;
74static int swapRGB = 0; /* default for auto sleect */
75static int swapRGB_on = 0; /* default to allow auto select; -1=swap never, +1= swap always */
76
77static unsigned int debug = 0;
78
79#define PDEBUG(level, fmt, args...) \
80 do { \
81 if (debug >= level) \
82 info("[%s:%d] " fmt, __FUNCTION__, __LINE__ , ## args); \
83 } while (0)
84
85
86/*
87 * Version Information
88 */
89#define DRIVER_VERSION "v0.25"
90#define DRIVER_AUTHOR "Kevin Sisson <kjsisson@bellsouth.net>"
91#define DRIVER_DESC "STV0680 USB Camera Driver"
92
93MODULE_AUTHOR (DRIVER_AUTHOR);
94MODULE_DESCRIPTION (DRIVER_DESC);
95MODULE_LICENSE ("GPL");
96module_param(debug, int, S_IRUGO | S_IWUSR);
97MODULE_PARM_DESC (debug, "Debug enabled or not");
98module_param(swapRGB_on, int, 0);
99MODULE_PARM_DESC (swapRGB_on, "Red/blue swap: 1=always, 0=auto, -1=never");
100module_param(video_nr, int, 0);
101
102/********************************************************************
103 *
104 * Memory management
105 *
106 * This is a shameless copy from the USB-cpia driver (linux kernel
107 * version 2.3.29 or so, I have no idea what this code actually does ;).
108 * Actually it seems to be a copy of a shameless copy of the bttv-driver.
109 * Or that is a copy of a shameless copy of ... (To the powers: is there
110 * no generic kernel-function to do this sort of stuff?)
111 *
112 * Yes, it was a shameless copy from the bttv-driver. IIRC, Alan says
113 * there will be one, but apparentely not yet -jerdfelt
114 *
115 * So I copied it again for the ov511 driver -claudio
116 *
117 * Same for the se401 driver -Jeroen
118 *
119 * And the STV0680 driver - Kevin
120 ********************************************************************/
121static void *rvmalloc (unsigned long size)
122{
123 void *mem;
124 unsigned long adr;
125
126 size = PAGE_ALIGN(size);
127 mem = vmalloc_32 (size);
128 if (!mem)
129 return NULL;
130
131 memset (mem, 0, size); /* Clear the ram out, no junk to the user */
132 adr = (unsigned long) mem;
133 while (size > 0) {
134 SetPageReserved(vmalloc_to_page((void *)adr));
135 adr += PAGE_SIZE;
136 size -= PAGE_SIZE;
137 }
138 return mem;
139}
140
141static void rvfree (void *mem, unsigned long size)
142{
143 unsigned long adr;
144
145 if (!mem)
146 return;
147
148 adr = (unsigned long) mem;
149 while ((long) size > 0) {
150 ClearPageReserved(vmalloc_to_page((void *)adr));
151 adr += PAGE_SIZE;
152 size -= PAGE_SIZE;
153 }
154 vfree (mem);
155}
156
157
158/*********************************************************************
159 * pencam read/write functions
160 ********************************************************************/
161
162static int stv_sndctrl (int set, struct usb_stv *stv680, unsigned short req, unsigned short value, unsigned char *buffer, int size)
163{
164 int ret = -1;
165
166 switch (set) {
167 case 0: /* 0xc1 */
168 ret = usb_control_msg (stv680->udev,
169 usb_rcvctrlpipe (stv680->udev, 0),
170 req,
171 (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
172 value, 0, buffer, size, PENCAM_TIMEOUT);
173 break;
174
175 case 1: /* 0x41 */
176 ret = usb_control_msg (stv680->udev,
177 usb_sndctrlpipe (stv680->udev, 0),
178 req,
179 (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
180 value, 0, buffer, size, PENCAM_TIMEOUT);
181 break;
182
183 case 2: /* 0x80 */
184 ret = usb_control_msg (stv680->udev,
185 usb_rcvctrlpipe (stv680->udev, 0),
186 req,
187 (USB_DIR_IN | USB_RECIP_DEVICE),
188 value, 0, buffer, size, PENCAM_TIMEOUT);
189 break;
190
191 case 3: /* 0x40 */
192 ret = usb_control_msg (stv680->udev,
193 usb_sndctrlpipe (stv680->udev, 0),
194 req,
195 (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE),
196 value, 0, buffer, size, PENCAM_TIMEOUT);
197 break;
198
199 }
200 if ((ret < 0) && (req != 0x0a)) {
201 PDEBUG (1, "STV(e): usb_control_msg error %i, request = 0x%x, error = %i", set, req, ret);
202 }
203 return ret;
204}
205
206static int stv_set_config (struct usb_stv *dev, int configuration, int interface, int alternate)
207{
208
209 if (configuration != dev->udev->actconfig->desc.bConfigurationValue
210 || usb_reset_configuration (dev->udev) < 0) {
211 PDEBUG (1, "STV(e): FAILED to reset configuration %i", configuration);
212 return -1;
213 }
214 if (usb_set_interface (dev->udev, interface, alternate) < 0) {
215 PDEBUG (1, "STV(e): FAILED to set alternate interface %i", alternate);
216 return -1;
217 }
218 return 0;
219}
220
221static int stv_stop_video (struct usb_stv *dev)
222{
223 int i;
224 unsigned char *buf;
225
226 buf = kmalloc (40, GFP_KERNEL);
227 if (buf == NULL) {
228 PDEBUG (0, "STV(e): Out of (small buf) memory");
229 return -1;
230 }
231
232 /* this is a high priority command; it stops all lower order commands */
233 if ((i = stv_sndctrl (1, dev, 0x04, 0x0000, buf, 0x0)) < 0) {
234 i = stv_sndctrl (0, dev, 0x80, 0, buf, 0x02); /* Get Last Error; 2 = busy */
235 PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buf[0], buf[1]);
236 } else {
237 PDEBUG (1, "STV(i): Camera reset to idle mode.");
238 }
239
240 if ((i = stv_set_config (dev, 1, 0, 0)) < 0)
241 PDEBUG (1, "STV(e): Reset config during exit failed");
242
243 /* get current mode */
244 buf[0] = 0xf0;
245 if ((i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08)) != 0x08) /* get mode */
246 PDEBUG (0, "STV(e): Stop_video: problem setting original mode");
247 if (dev->origMode != buf[0]) {
248 memset (buf, 0, 8);
249 buf[0] = (unsigned char) dev->origMode;
250 if ((i = stv_sndctrl (3, dev, 0x07, 0x0100, buf, 0x08)) != 0x08) {
251 PDEBUG (0, "STV(e): Stop_video: Set_Camera_Mode failed");
252 i = -1;
253 }
254 buf[0] = 0xf0;
255 i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08);
256 if ((i != 0x08) || (buf[0] != dev->origMode)) {
257 PDEBUG (0, "STV(e): camera NOT set to original resolution.");
258 i = -1;
259 } else
260 PDEBUG (0, "STV(i): Camera set to original resolution");
261 }
262 /* origMode */
263 kfree(buf);
264 return i;
265}
266
267static int stv_set_video_mode (struct usb_stv *dev)
268{
269 int i, stop_video = 1;
270 unsigned char *buf;
271
272 buf = kmalloc (40, GFP_KERNEL);
273 if (buf == NULL) {
274 PDEBUG (0, "STV(e): Out of (small buf) memory");
275 return -1;
276 }
277
278 if ((i = stv_set_config (dev, 1, 0, 0)) < 0) {
279 kfree(buf);
280 return i;
281 }
282
283 i = stv_sndctrl (2, dev, 0x06, 0x0100, buf, 0x12);
284 if (!(i > 0) && (buf[8] == 0x53) && (buf[9] == 0x05)) {
285 PDEBUG (1, "STV(e): Could not get descriptor 0100.");
286 goto error;
287 }
288
289 /* set alternate interface 1 */
290 if ((i = stv_set_config (dev, 1, 0, 1)) < 0)
291 goto error;
292
293 if ((i = stv_sndctrl (0, dev, 0x85, 0, buf, 0x10)) != 0x10)
294 goto error;
295 PDEBUG (1, "STV(i): Setting video mode.");
296 /* Switch to Video mode: 0x0100 = VGA (640x480), 0x0000 = CIF (352x288) 0x0300 = QVGA (320x240) */
297 if ((i = stv_sndctrl (1, dev, 0x09, dev->VideoMode, buf, 0x0)) < 0) {
298 stop_video = 0;
299 goto error;
300 }
301 goto exit;
302
303error:
304 kfree(buf);
305 if (stop_video == 1)
306 stv_stop_video (dev);
307 return -1;
308
309exit:
310 kfree(buf);
311 return 0;
312}
313
314static int stv_init (struct usb_stv *stv680)
315{
316 int i = 0;
317 unsigned char *buffer;
318 unsigned long int bufsize;
319
320 buffer = kmalloc (40, GFP_KERNEL);
321 if (buffer == NULL) {
322 PDEBUG (0, "STV(e): Out of (small buf) memory");
323 return -1;
324 }
325 memset (buffer, 0, 40);
326 udelay (100);
327
328 /* set config 1, interface 0, alternate 0 */
329 if ((i = stv_set_config (stv680, 1, 0, 0)) < 0) {
330 kfree(buffer);
331 PDEBUG (0, "STV(e): set config 1,0,0 failed");
332 return -1;
333 }
334 /* ping camera to be sure STV0680 is present */
335 if ((i = stv_sndctrl (0, stv680, 0x88, 0x5678, buffer, 0x02)) != 0x02)
336 goto error;
337 if ((buffer[0] != 0x56) || (buffer[1] != 0x78)) {
338 PDEBUG (1, "STV(e): camera ping failed!!");
339 goto error;
340 }
341
342 /* get camera descriptor */
343 if ((i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x09)) != 0x09)
344 goto error;
345 i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x22);
346 if (!(i >= 0) && (buffer[7] == 0xa0) && (buffer[8] == 0x23)) {
347 PDEBUG (1, "STV(e): Could not get descriptor 0200.");
348 goto error;
349 }
350 if ((i = stv_sndctrl (0, stv680, 0x8a, 0, buffer, 0x02)) != 0x02)
351 goto error;
352 if ((i = stv_sndctrl (0, stv680, 0x8b, 0, buffer, 0x24)) != 0x24)
353 goto error;
354 if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
355 goto error;
356
357 stv680->SupportedModes = buffer[7];
358 i = stv680->SupportedModes;
359 stv680->CIF = 0;
360 stv680->VGA = 0;
361 stv680->QVGA = 0;
362 if (i & 1)
363 stv680->CIF = 1;
364 if (i & 2)
365 stv680->VGA = 1;
366 if (i & 8)
367 stv680->QVGA = 1;
368 if (stv680->SupportedModes == 0) {
369 PDEBUG (0, "STV(e): There are NO supported STV680 modes!!");
370 i = -1;
371 goto error;
372 } else {
373 if (stv680->CIF)
374 PDEBUG (0, "STV(i): CIF is supported");
375 if (stv680->QVGA)
376 PDEBUG (0, "STV(i): QVGA is supported");
377 }
378 /* FW rev, ASIC rev, sensor ID */
379 PDEBUG (1, "STV(i): Firmware rev is %i.%i", buffer[0], buffer[1]);
380 PDEBUG (1, "STV(i): ASIC rev is %i.%i", buffer[2], buffer[3]);
381 PDEBUG (1, "STV(i): Sensor ID is %i", (buffer[4]*16) + (buffer[5]>>4));
382
383 /* set alternate interface 1 */
384 if ((i = stv_set_config (stv680, 1, 0, 1)) < 0)
385 goto error;
386
387 if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
388 goto error;
389 if ((i = stv_sndctrl (0, stv680, 0x8d, 0, buffer, 0x08)) != 0x08)
390 goto error;
391 i = buffer[3];
392 PDEBUG (0, "STV(i): Camera has %i pictures.", i);
393
394 /* get current mode */
395 if ((i = stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08)) != 0x08)
396 goto error;
397 stv680->origMode = buffer[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
398
399 /* This will attemp CIF mode, if supported. If not, set to QVGA */
400 memset (buffer, 0, 8);
401 if (stv680->CIF)
402 buffer[0] = 0x00;
403 else if (stv680->QVGA)
404 buffer[0] = 0x03;
405 if ((i = stv_sndctrl (3, stv680, 0x07, 0x0100, buffer, 0x08)) != 0x08) {
406 PDEBUG (0, "STV(i): Set_Camera_Mode failed");
407 i = -1;
408 goto error;
409 }
410 buffer[0] = 0xf0;
411 stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08);
412 if (((stv680->CIF == 1) && (buffer[0] != 0x00)) || ((stv680->QVGA == 1) && (buffer[0] != 0x03))) {
413 PDEBUG (0, "STV(e): Error setting camera video mode!");
414 i = -1;
415 goto error;
416 } else {
417 if (buffer[0] == 0) {
418 stv680->VideoMode = 0x0000;
419 PDEBUG (0, "STV(i): Video Mode set to CIF");
420 }
421 if (buffer[0] == 0x03) {
422 stv680->VideoMode = 0x0300;
423 PDEBUG (0, "STV(i): Video Mode set to QVGA");
424 }
425 }
426 if ((i = stv_sndctrl (0, stv680, 0x8f, 0, buffer, 0x10)) != 0x10)
427 goto error;
428 bufsize = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3]);
429 stv680->cwidth = (buffer[4] << 8) | (buffer[5]); /* ->camera = 322, 356, 644 */
430 stv680->cheight = (buffer[6] << 8) | (buffer[7]); /* ->camera = 242, 292, 484 */
431 stv680->origGain = buffer[12];
432
433 goto exit;
434
435error:
436 i = stv_sndctrl (0, stv680, 0x80, 0, buffer, 0x02); /* Get Last Error */
437 PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buffer[0], buffer[1]);
438 kfree(buffer);
439 return -1;
440
441exit:
442 kfree(buffer);
443
444 /* video = 320x240, 352x288 */
445 if (stv680->CIF == 1) {
446 stv680->maxwidth = 352;
447 stv680->maxheight = 288;
448 stv680->vwidth = 352;
449 stv680->vheight = 288;
450 }
451 if (stv680->QVGA == 1) {
452 stv680->maxwidth = 320;
453 stv680->maxheight = 240;
454 stv680->vwidth = 320;
455 stv680->vheight = 240;
456 }
457
458 stv680->rawbufsize = bufsize; /* must be ./. by 8 */
459 stv680->maxframesize = bufsize * 3; /* RGB size */
460 PDEBUG (2, "STV(i): cwidth = %i, cheight = %i", stv680->cwidth, stv680->cheight);
461 PDEBUG (1, "STV(i): width = %i, height = %i, rawbufsize = %li", stv680->vwidth, stv680->vheight, stv680->rawbufsize);
462
463 /* some default values */
464 stv680->bulk_in_endpointAddr = 0x82;
465 stv680->dropped = 0;
466 stv680->error = 0;
467 stv680->framecount = 0;
468 stv680->readcount = 0;
469 stv680->streaming = 0;
470 /* bright, white, colour, hue, contrast are set by software, not in stv0680 */
471 stv680->brightness = 32767;
472 stv680->chgbright = 0;
473 stv680->whiteness = 0; /* only for greyscale */
474 stv680->colour = 32767;
475 stv680->contrast = 32767;
476 stv680->hue = 32767;
477 stv680->palette = STV_VIDEO_PALETTE;
478 stv680->depth = 24; /* rgb24 bits */
479 if ((swapRGB_on == 0) && (swapRGB == 0))
480 PDEBUG (1, "STV(i): swapRGB is (auto) OFF");
481 else if ((swapRGB_on == 0) && (swapRGB == 1))
482 PDEBUG (1, "STV(i): swapRGB is (auto) ON");
483 else if (swapRGB_on == 1)
484 PDEBUG (1, "STV(i): swapRGB is (forced) ON");
485 else if (swapRGB_on == -1)
486 PDEBUG (1, "STV(i): swapRGB is (forced) OFF");
487
488 if (stv_set_video_mode (stv680) < 0) {
489 PDEBUG (0, "STV(e): Could not set video mode in stv_init");
490 return -1;
491 }
492
493 return 0;
494}
495
496/***************** last of pencam routines *******************/
497
498/****************************************************************************
499 * sysfs
500 ***************************************************************************/
501#define stv680_file(name, variable, field) \
502static ssize_t show_##name(struct class_device *class_dev, char *buf) \
503{ \
504 struct video_device *vdev = to_video_device(class_dev); \
505 struct usb_stv *stv = video_get_drvdata(vdev); \
506 return sprintf(buf, field, stv->variable); \
507} \
508static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
509
510stv680_file(model, camera_name, "%s\n");
511stv680_file(in_use, user, "%d\n");
512stv680_file(streaming, streaming, "%d\n");
513stv680_file(palette, palette, "%i\n");
514stv680_file(frames_total, readcount, "%d\n");
515stv680_file(frames_read, framecount, "%d\n");
516stv680_file(packets_dropped, dropped, "%d\n");
517stv680_file(decoding_errors, error, "%d\n");
518
519static void stv680_create_sysfs_files(struct video_device *vdev)
520{
521 video_device_create_file(vdev, &class_device_attr_model);
522 video_device_create_file(vdev, &class_device_attr_in_use);
523 video_device_create_file(vdev, &class_device_attr_streaming);
524 video_device_create_file(vdev, &class_device_attr_palette);
525 video_device_create_file(vdev, &class_device_attr_frames_total);
526 video_device_create_file(vdev, &class_device_attr_frames_read);
527 video_device_create_file(vdev, &class_device_attr_packets_dropped);
528 video_device_create_file(vdev, &class_device_attr_decoding_errors);
529}
530
531static void stv680_remove_sysfs_files(struct video_device *vdev)
532{
533 video_device_remove_file(vdev, &class_device_attr_model);
534 video_device_remove_file(vdev, &class_device_attr_in_use);
535 video_device_remove_file(vdev, &class_device_attr_streaming);
536 video_device_remove_file(vdev, &class_device_attr_palette);
537 video_device_remove_file(vdev, &class_device_attr_frames_total);
538 video_device_remove_file(vdev, &class_device_attr_frames_read);
539 video_device_remove_file(vdev, &class_device_attr_packets_dropped);
540 video_device_remove_file(vdev, &class_device_attr_decoding_errors);
541}
542
543/********************************************************************
544 * Camera control
545 *******************************************************************/
546
547static int stv680_get_pict (struct usb_stv *stv680, struct video_picture *p)
548{
549 /* This sets values for v4l interface. max/min = 65535/0 */
550
551 p->brightness = stv680->brightness;
552 p->whiteness = stv680->whiteness; /* greyscale */
553 p->colour = stv680->colour;
554 p->contrast = stv680->contrast;
555 p->hue = stv680->hue;
556 p->palette = stv680->palette;
557 p->depth = stv680->depth;
558 return 0;
559}
560
561static int stv680_set_pict (struct usb_stv *stv680, struct video_picture *p)
562{
563 /* See above stv680_get_pict */
564
565 if (p->palette != STV_VIDEO_PALETTE) {
566 PDEBUG (2, "STV(e): Palette set error in _set_pic");
567 return 1;
568 }
569
570 if (stv680->brightness != p->brightness) {
571 stv680->chgbright = 1;
572 stv680->brightness = p->brightness;
573 }
574
575 stv680->whiteness = p->whiteness; /* greyscale */
576 stv680->colour = p->colour;
577 stv680->contrast = p->contrast;
578 stv680->hue = p->hue;
579 stv680->palette = p->palette;
580 stv680->depth = p->depth;
581
582 return 0;
583}
584
585static void stv680_video_irq (struct urb *urb, struct pt_regs *regs)
586{
587 struct usb_stv *stv680 = urb->context;
588 int length = urb->actual_length;
589
590 if (length < stv680->rawbufsize)
591 PDEBUG (2, "STV(i): Lost data in transfer: exp %li, got %i", stv680->rawbufsize, length);
592
593 /* ohoh... */
594 if (!stv680->streaming)
595 return;
596
597 if (!stv680->udev) {
598 PDEBUG (0, "STV(e): device vapourished in video_irq");
599 return;
600 }
601
602 /* 0 sized packets happen if we are to fast, but sometimes the camera
603 keeps sending them forever...
604 */
605 if (length && !urb->status) {
606 stv680->nullpackets = 0;
607 switch (stv680->scratch[stv680->scratch_next].state) {
608 case BUFFER_READY:
609 case BUFFER_BUSY:
610 stv680->dropped++;
611 break;
612
613 case BUFFER_UNUSED:
614 memcpy (stv680->scratch[stv680->scratch_next].data,
615 (unsigned char *) urb->transfer_buffer, length);
616 stv680->scratch[stv680->scratch_next].state = BUFFER_READY;
617 stv680->scratch[stv680->scratch_next].length = length;
618 if (waitqueue_active (&stv680->wq)) {
619 wake_up_interruptible (&stv680->wq);
620 }
621 stv680->scratch_overflow = 0;
622 stv680->scratch_next++;
623 if (stv680->scratch_next >= STV680_NUMSCRATCH)
624 stv680->scratch_next = 0;
625 break;
626 } /* switch */
627 } else {
628 stv680->nullpackets++;
629 if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
630 if (waitqueue_active (&stv680->wq)) {
631 wake_up_interruptible (&stv680->wq);
632 }
633 }
634 } /* if - else */
635
636 /* Resubmit urb for new data */
637 urb->status = 0;
638 urb->dev = stv680->udev;
639 if (usb_submit_urb (urb, GFP_ATOMIC))
640 PDEBUG (0, "STV(e): urb burned down in video irq");
641 return;
642} /* _video_irq */
643
644static int stv680_start_stream (struct usb_stv *stv680)
645{
646 struct urb *urb;
647 int err = 0, i;
648
649 stv680->streaming = 1;
650
651 /* Do some memory allocation */
652 for (i = 0; i < STV680_NUMFRAMES; i++) {
653 stv680->frame[i].data = stv680->fbuf + i * stv680->maxframesize;
654 stv680->frame[i].curpix = 0;
655 }
656 /* packet size = 4096 */
657 for (i = 0; i < STV680_NUMSBUF; i++) {
658 stv680->sbuf[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
659 if (stv680->sbuf[i].data == NULL) {
660 PDEBUG (0, "STV(e): Could not kmalloc raw data buffer %i", i);
661 return -1;
662 }
663 }
664
665 stv680->scratch_next = 0;
666 stv680->scratch_use = 0;
667 stv680->scratch_overflow = 0;
668 for (i = 0; i < STV680_NUMSCRATCH; i++) {
669 stv680->scratch[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
670 if (stv680->scratch[i].data == NULL) {
671 PDEBUG (0, "STV(e): Could not kmalloc raw scratch buffer %i", i);
672 return -1;
673 }
674 stv680->scratch[i].state = BUFFER_UNUSED;
675 }
676
677 for (i = 0; i < STV680_NUMSBUF; i++) {
678 urb = usb_alloc_urb (0, GFP_KERNEL);
679 if (!urb)
680 return -ENOMEM;
681
682 /* sbuf is urb->transfer_buffer, later gets memcpyed to scratch */
683 usb_fill_bulk_urb (urb, stv680->udev,
684 usb_rcvbulkpipe (stv680->udev, stv680->bulk_in_endpointAddr),
685 stv680->sbuf[i].data, stv680->rawbufsize,
686 stv680_video_irq, stv680);
687 stv680->urb[i] = urb;
688 err = usb_submit_urb (stv680->urb[i], GFP_KERNEL);
689 if (err)
690 PDEBUG (0, "STV(e): urb burned down in start stream");
691 } /* i STV680_NUMSBUF */
692
693 stv680->framecount = 0;
694 return 0;
695}
696
697static int stv680_stop_stream (struct usb_stv *stv680)
698{
699 int i;
700
701 if (!stv680->streaming || !stv680->udev)
702 return 1;
703
704 stv680->streaming = 0;
705
706 for (i = 0; i < STV680_NUMSBUF; i++)
707 if (stv680->urb[i]) {
708 usb_kill_urb (stv680->urb[i]);
709 usb_free_urb (stv680->urb[i]);
710 stv680->urb[i] = NULL;
711 kfree(stv680->sbuf[i].data);
712 }
713 for (i = 0; i < STV680_NUMSCRATCH; i++) {
714 kfree(stv680->scratch[i].data);
715 stv680->scratch[i].data = NULL;
716 }
717
718 return 0;
719}
720
721static int stv680_set_size (struct usb_stv *stv680, int width, int height)
722{
723 int wasstreaming = stv680->streaming;
724
725 /* Check to see if we need to change */
726 if ((stv680->vwidth == width) && (stv680->vheight == height))
727 return 0;
728
729 PDEBUG (1, "STV(i): size request for %i x %i", width, height);
730 /* Check for a valid mode */
731 if ((!width || !height) || ((width & 1) || (height & 1))) {
732 PDEBUG (1, "STV(e): set_size error: request: v.width = %i, v.height = %i actual: stv.width = %i, stv.height = %i", width, height, stv680->vwidth, stv680->vheight);
733 return 1;
734 }
735
736 if ((width < (stv680->maxwidth / 2)) || (height < (stv680->maxheight / 2))) {
737 width = stv680->maxwidth / 2;
738 height = stv680->maxheight / 2;
739 } else if ((width >= 158) && (width <= 166) && (stv680->QVGA == 1)) {
740 width = 160;
741 height = 120;
742 } else if ((width >= 172) && (width <= 180) && (stv680->CIF == 1)) {
743 width = 176;
744 height = 144;
745 } else if ((width >= 318) && (width <= 350) && (stv680->QVGA == 1)) {
746 width = 320;
747 height = 240;
748 } else if ((width >= 350) && (width <= 358) && (stv680->CIF == 1)) {
749 width = 352;
750 height = 288;
751 } else {
752 PDEBUG (1, "STV(e): request for non-supported size: request: v.width = %i, v.height = %i actual: stv.width = %i, stv.height = %i", width, height, stv680->vwidth, stv680->vheight);
753 return 1;
754 }
755
756 /* Stop a current stream and start it again at the new size */
757 if (wasstreaming)
758 stv680_stop_stream (stv680);
759 stv680->vwidth = width;
760 stv680->vheight = height;
761 PDEBUG (1, "STV(i): size set to %i x %i", stv680->vwidth, stv680->vheight);
762 if (wasstreaming)
763 stv680_start_stream (stv680);
764
765 return 0;
766}
767
768/**********************************************************************
769 * Video Decoding
770 **********************************************************************/
771
772/******* routines from the pencam program; hey, they work! ********/
773
774/*
775 * STV0680 Vision Camera Chipset Driver
776 * Copyright (C) 2000 Adam Harrison <adam@antispin.org>
777*/
778
779#define RED 0
780#define GREEN 1
781#define BLUE 2
782#define AD(x, y, w) (((y)*(w)+(x))*3)
783
784static void bayer_unshuffle (struct usb_stv *stv680, struct stv680_scratch *buffer)
785{
786 int x, y, i;
787 int w = stv680->cwidth;
788 int vw = stv680->cwidth, vh = stv680->cheight;
789 unsigned int p = 0;
790 int colour = 0, bayer = 0;
791 unsigned char *raw = buffer->data;
792 struct stv680_frame *frame = &stv680->frame[stv680->curframe];
793 unsigned char *output = frame->data;
794 unsigned char *temp = frame->data;
795 int offset = buffer->offset;
796
797 if (frame->curpix == 0) {
798 if (frame->grabstate == FRAME_READY) {
799 frame->grabstate = FRAME_GRABBING;
800 }
801 }
802 if (offset != frame->curpix) { /* Regard frame as lost :( */
803 frame->curpix = 0;
804 stv680->error++;
805 return;
806 }
807
808 if ((stv680->vwidth == 320) || (stv680->vwidth == 160)) {
809 vw = 320;
810 vh = 240;
811 }
812 if ((stv680->vwidth == 352) || (stv680->vwidth == 176)) {
813 vw = 352;
814 vh = 288;
815 }
816
817 memset (output, 0, 3 * vw * vh); /* clear output matrix. */
818
819 for (y = 0; y < vh; y++) {
820 for (x = 0; x < vw; x++) {
821 if (x & 1)
822 p = *(raw + y * w + (x >> 1));
823 else
824 p = *(raw + y * w + (x >> 1) + (w >> 1));
825
826 if (y & 1)
827 bayer = 2;
828 else
829 bayer = 0;
830 if (x & 1)
831 bayer++;
832
833 switch (bayer) {
834 case 0:
835 case 3:
836 colour = 1;
837 break;
838 case 1:
839 colour = 0;
840 break;
841 case 2:
842 colour = 2;
843 break;
844 }
845 i = (y * vw + x) * 3;
846 *(output + i + colour) = (unsigned char) p;
847 } /* for x */
848
849 } /* for y */
850
851 /****** gamma correction plus hardcoded white balance */
852 /* Thanks to Alexander Schwartx <alexander.schwartx@gmx.net> for this code.
853 Correction values red[], green[], blue[], are generated by
854 (pow(i/256.0, GAMMA)*255.0)*white balanceRGB where GAMMA=0.55, 1<i<255.
855 White balance (RGB)= 1.0, 1.17, 1.48. Values are calculated as double float and
856 converted to unsigned char. Values are in stv680.h */
857
858 for (y = 0; y < vh; y++) {
859 for (x = 0; x < vw; x++) {
860 i = (y * vw + x) * 3;
861 *(output + i) = red[*(output + i)];
862 *(output + i + 1) = green[*(output + i + 1)];
863 *(output + i + 2) = blue[*(output + i + 2)];
864 }
865 }
866
867 /****** bayer demosaic ******/
868 for (y = 1; y < (vh - 1); y++) {
869 for (x = 1; x < (vw - 1); x++) { /* work out pixel type */
870 if (y & 1)
871 bayer = 0;
872 else
873 bayer = 2;
874 if (!(x & 1))
875 bayer++;
876
877 switch (bayer) {
878 case 0: /* green. blue lr, red tb */
879 *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y, vw) + BLUE) + (int) *(output + AD (x + 1, y, vw) + BLUE)) >> 1;
880 *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x, y - 1, vw) + RED) + (int) *(output + AD (x, y + 1, vw) + RED)) >> 1;
881 break;
882
883 case 1: /* blue. green lrtb, red diagonals */
884 *(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
885 *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y - 1, vw) + RED) + (int) *(output + AD (x - 1, y + 1, vw) + RED) + (int) *(output + AD (x + 1, y - 1, vw) + RED) + (int) *(output + AD (x + 1, y + 1, vw) + RED)) >> 2;
886 break;
887
888 case 2: /* red. green lrtb, blue diagonals */
889 *(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
890 *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y - 1, vw) + BLUE) + (int) *(output + AD (x + 1, y - 1, vw) + BLUE) + (int) *(output + AD (x - 1, y + 1, vw) + BLUE) + (int) *(output + AD (x + 1, y + 1, vw) + BLUE)) >> 2;
891 break;
892
893 case 3: /* green. red lr, blue tb */
894 *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y, vw) + RED) + (int) *(output + AD (x + 1, y, vw) + RED)) >> 1;
895 *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x, y - 1, vw) + BLUE) + (int) *(output + AD (x, y + 1, vw) + BLUE)) >> 1;
896 break;
897 } /* switch */
898 } /* for x */
899 } /* for y - end demosaic */
900
901 /* fix top and bottom row, left and right side */
902 i = vw * 3;
903 memcpy (output, (output + i), i);
904 memcpy ((output + (vh * i)), (output + ((vh - 1) * i)), i);
905 for (y = 0; y < vh; y++) {
906 i = y * vw * 3;
907 memcpy ((output + i), (output + i + 3), 3);
908 memcpy ((output + i + (vw * 3)), (output + i + (vw - 1) * 3), 3);
909 }
910
911 /* process all raw data, then trim to size if necessary */
912 if ((stv680->vwidth == 160) || (stv680->vwidth == 176)) {
913 i = 0;
914 for (y = 0; y < vh; y++) {
915 if (!(y & 1)) {
916 for (x = 0; x < vw; x++) {
917 p = (y * vw + x) * 3;
918 if (!(x & 1)) {
919 *(output + i) = *(output + p);
920 *(output + i + 1) = *(output + p + 1);
921 *(output + i + 2) = *(output + p + 2);
922 i += 3;
923 }
924 } /* for x */
925 }
926 } /* for y */
927 }
928 /* reset to proper width */
929 if ((stv680->vwidth == 160)) {
930 vw = 160;
931 vh = 120;
932 }
933 if ((stv680->vwidth == 176)) {
934 vw = 176;
935 vh = 144;
936 }
937
938 /* output is RGB; some programs want BGR */
939 /* swapRGB_on=0 -> program decides; swapRGB_on=1, always swap */
940 /* swapRGB_on=-1, never swap */
941 if (((swapRGB == 1) && (swapRGB_on != -1)) || (swapRGB_on == 1)) {
942 for (y = 0; y < vh; y++) {
943 for (x = 0; x < vw; x++) {
944 i = (y * vw + x) * 3;
945 *(temp) = *(output + i);
946 *(output + i) = *(output + i + 2);
947 *(output + i + 2) = *(temp);
948 }
949 }
950 }
951 /* brightness */
952 if (stv680->chgbright == 1) {
953 if (stv680->brightness >= 32767) {
954 p = (stv680->brightness - 32767) / 256;
955 for (x = 0; x < (vw * vh * 3); x++) {
956 if ((*(output + x) + (unsigned char) p) > 255)
957 *(output + x) = 255;
958 else
959 *(output + x) += (unsigned char) p;
960 } /* for */
961 } else {
962 p = (32767 - stv680->brightness) / 256;
963 for (x = 0; x < (vw * vh * 3); x++) {
964 if ((unsigned char) p > *(output + x))
965 *(output + x) = 0;
966 else
967 *(output + x) -= (unsigned char) p;
968 } /* for */
969 } /* else */
970 }
971 /* if */
972 frame->curpix = 0;
973 frame->curlinepix = 0;
974 frame->grabstate = FRAME_DONE;
975 stv680->framecount++;
976 stv680->readcount++;
977 if (stv680->frame[(stv680->curframe + 1) & (STV680_NUMFRAMES - 1)].grabstate == FRAME_READY) {
978 stv680->curframe = (stv680->curframe + 1) & (STV680_NUMFRAMES - 1);
979 }
980
981} /* bayer_unshuffle */
982
983/******* end routines from the pencam program *********/
984
985static int stv680_newframe (struct usb_stv *stv680, int framenr)
986{
987 int errors = 0;
988
989 while (stv680->streaming && (stv680->frame[framenr].grabstate == FRAME_READY || stv680->frame[framenr].grabstate == FRAME_GRABBING)) {
990 if (!stv680->frame[framenr].curpix) {
991 errors++;
992 }
993 wait_event_interruptible (stv680->wq, (stv680->scratch[stv680->scratch_use].state == BUFFER_READY));
994
995 if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
996 stv680->nullpackets = 0;
997 PDEBUG (2, "STV(i): too many null length packets, restarting capture");
998 stv680_stop_stream (stv680);
999 stv680_start_stream (stv680);
1000 } else {
1001 if (stv680->scratch[stv680->scratch_use].state != BUFFER_READY) {
1002 stv680->frame[framenr].grabstate = FRAME_ERROR;
1003 PDEBUG (2, "STV(e): FRAME_ERROR in _newframe");
1004 return -EIO;
1005 }
1006 stv680->scratch[stv680->scratch_use].state = BUFFER_BUSY;
1007
1008 bayer_unshuffle (stv680, &stv680->scratch[stv680->scratch_use]);
1009
1010 stv680->scratch[stv680->scratch_use].state = BUFFER_UNUSED;
1011 stv680->scratch_use++;
1012 if (stv680->scratch_use >= STV680_NUMSCRATCH)
1013 stv680->scratch_use = 0;
1014 if (errors > STV680_MAX_ERRORS) {
1015 errors = 0;
1016 PDEBUG (2, "STV(i): too many errors, restarting capture");
1017 stv680_stop_stream (stv680);
1018 stv680_start_stream (stv680);
1019 }
1020 } /* else */
1021 } /* while */
1022 return 0;
1023}
1024
1025/*********************************************************************
1026 * Video4Linux
1027 *********************************************************************/
1028
1029static int stv_open (struct inode *inode, struct file *file)
1030{
1031 struct video_device *dev = video_devdata(file);
1032 struct usb_stv *stv680 = video_get_drvdata(dev);
1033 int err = 0;
1034
1035 /* we are called with the BKL held */
1036 stv680->user = 1;
1037 err = stv_init (stv680); /* main initialization routine for camera */
1038
1039 if (err >= 0) {
1040 stv680->fbuf = rvmalloc (stv680->maxframesize * STV680_NUMFRAMES);
1041 if (!stv680->fbuf) {
1042 PDEBUG (0, "STV(e): Could not rvmalloc frame bufer");
1043 err = -ENOMEM;
1044 }
1045 file->private_data = dev;
1046 }
1047 if (err)
1048 stv680->user = 0;
1049
1050 return err;
1051}
1052
1053static int stv_close (struct inode *inode, struct file *file)
1054{
1055 struct video_device *dev = file->private_data;
1056 struct usb_stv *stv680 = video_get_drvdata(dev);
1057 int i;
1058
1059 for (i = 0; i < STV680_NUMFRAMES; i++)
1060 stv680->frame[i].grabstate = FRAME_UNUSED;
1061 if (stv680->streaming)
1062 stv680_stop_stream (stv680);
1063
1064 if ((i = stv_stop_video (stv680)) < 0)
1065 PDEBUG (1, "STV(e): stop_video failed in stv_close");
1066
1067 rvfree (stv680->fbuf, stv680->maxframesize * STV680_NUMFRAMES);
1068 stv680->user = 0;
1069
1070 if (stv680->removed) {
1071 kfree(stv680);
1072 stv680 = NULL;
1073 PDEBUG (0, "STV(i): device unregistered");
1074 }
1075 file->private_data = NULL;
1076 return 0;
1077}
1078
1079static int stv680_do_ioctl (struct inode *inode, struct file *file,
1080 unsigned int cmd, void *arg)
1081{
1082 struct video_device *vdev = file->private_data;
1083 struct usb_stv *stv680 = video_get_drvdata(vdev);
1084
1085 if (!stv680->udev)
1086 return -EIO;
1087
1088 switch (cmd) {
1089 case VIDIOCGCAP:{
1090 struct video_capability *b = arg;
1091
1092 strcpy (b->name, stv680->camera_name);
1093 b->type = VID_TYPE_CAPTURE;
1094 b->channels = 1;
1095 b->audios = 0;
1096 b->maxwidth = stv680->maxwidth;
1097 b->maxheight = stv680->maxheight;
1098 b->minwidth = stv680->maxwidth / 2;
1099 b->minheight = stv680->maxheight / 2;
1100 return 0;
1101 }
1102 case VIDIOCGCHAN:{
1103 struct video_channel *v = arg;
1104
1105 if (v->channel != 0)
1106 return -EINVAL;
1107 v->flags = 0;
1108 v->tuners = 0;
1109 v->type = VIDEO_TYPE_CAMERA;
1110 strcpy (v->name, "STV Camera");
1111 return 0;
1112 }
1113 case VIDIOCSCHAN:{
1114 struct video_channel *v = arg;
1115 if (v->channel != 0)
1116 return -EINVAL;
1117 return 0;
1118 }
1119 case VIDIOCGPICT:{
1120 struct video_picture *p = arg;
1121
1122 stv680_get_pict (stv680, p);
1123 return 0;
1124 }
1125 case VIDIOCSPICT:{
1126 struct video_picture *p = arg;
1127
1128 if (stv680_set_pict (stv680, p))
1129 return -EINVAL;
1130 return 0;
1131 }
1132 case VIDIOCSWIN:{
1133 struct video_window *vw = arg;
1134
1135 if (vw->flags)
1136 return -EINVAL;
1137 if (vw->clipcount)
1138 return -EINVAL;
1139 if (vw->width != stv680->vwidth) {
1140 if (stv680_set_size (stv680, vw->width, vw->height)) {
1141 PDEBUG (2, "STV(e): failed (from user) set size in VIDIOCSWIN");
1142 return -EINVAL;
1143 }
1144 }
1145 return 0;
1146 }
1147 case VIDIOCGWIN:{
1148 struct video_window *vw = arg;
1149
1150 vw->x = 0; /* FIXME */
1151 vw->y = 0;
1152 vw->chromakey = 0;
1153 vw->flags = 0;
1154 vw->clipcount = 0;
1155 vw->width = stv680->vwidth;
1156 vw->height = stv680->vheight;
1157 return 0;
1158 }
1159 case VIDIOCGMBUF:{
1160 struct video_mbuf *vm = arg;
1161 int i;
1162
1163 memset (vm, 0, sizeof (*vm));
1164 vm->size = STV680_NUMFRAMES * stv680->maxframesize;
1165 vm->frames = STV680_NUMFRAMES;
1166 for (i = 0; i < STV680_NUMFRAMES; i++)
1167 vm->offsets[i] = stv680->maxframesize * i;
1168 return 0;
1169 }
1170 case VIDIOCMCAPTURE:{
1171 struct video_mmap *vm = arg;
1172
1173 if (vm->format != STV_VIDEO_PALETTE) {
1174 PDEBUG (2, "STV(i): VIDIOCMCAPTURE vm.format (%i) != VIDEO_PALETTE (%i)",
1175 vm->format, STV_VIDEO_PALETTE);
1176 if ((vm->format == 3) && (swapRGB_on == 0)) {
1177 PDEBUG (2, "STV(i): VIDIOCMCAPTURE swapRGB is (auto) ON");
1178 /* this may fix those apps (e.g., xawtv) that want BGR */
1179 swapRGB = 1;
1180 }
1181 return -EINVAL;
1182 }
1183 if (vm->frame >= STV680_NUMFRAMES) {
1184 PDEBUG (2, "STV(e): VIDIOCMCAPTURE vm.frame > NUMFRAMES");
1185 return -EINVAL;
1186 }
1187 if ((stv680->frame[vm->frame].grabstate == FRAME_ERROR)
1188 || (stv680->frame[vm->frame].grabstate == FRAME_GRABBING)) {
1189 PDEBUG (2, "STV(e): VIDIOCMCAPTURE grabstate (%i) error",
1190 stv680->frame[vm->frame].grabstate);
1191 return -EBUSY;
1192 }
1193 /* Is this according to the v4l spec??? */
1194 if (stv680->vwidth != vm->width) {
1195 if (stv680_set_size (stv680, vm->width, vm->height)) {
1196 PDEBUG (2, "STV(e): VIDIOCMCAPTURE set_size failed");
1197 return -EINVAL;
1198 }
1199 }
1200 stv680->frame[vm->frame].grabstate = FRAME_READY;
1201
1202 if (!stv680->streaming)
1203 stv680_start_stream (stv680);
1204
1205 return 0;
1206 }
1207 case VIDIOCSYNC:{
1208 int *frame = arg;
1209 int ret = 0;
1210
1211 if (*frame < 0 || *frame >= STV680_NUMFRAMES) {
1212 PDEBUG (2, "STV(e): Bad frame # in VIDIOCSYNC");
1213 return -EINVAL;
1214 }
1215 ret = stv680_newframe (stv680, *frame);
1216 stv680->frame[*frame].grabstate = FRAME_UNUSED;
1217 return ret;
1218 }
1219 case VIDIOCGFBUF:{
1220 struct video_buffer *vb = arg;
1221
1222 memset (vb, 0, sizeof (*vb));
1223 return 0;
1224 }
1225 case VIDIOCKEY:
1226 return 0;
1227 case VIDIOCCAPTURE:
1228 {
1229 PDEBUG (2, "STV(e): VIDIOCCAPTURE failed");
1230 return -EINVAL;
1231 }
1232 case VIDIOCSFBUF:
1233 case VIDIOCGTUNER:
1234 case VIDIOCSTUNER:
1235 case VIDIOCGFREQ:
1236 case VIDIOCSFREQ:
1237 case VIDIOCGAUDIO:
1238 case VIDIOCSAUDIO:
1239 return -EINVAL;
1240 default:
1241 return -ENOIOCTLCMD;
1242 } /* end switch */
1243
1244 return 0;
1245}
1246
1247static int stv680_ioctl(struct inode *inode, struct file *file,
1248 unsigned int cmd, unsigned long arg)
1249{
1250 return video_usercopy(inode, file, cmd, arg, stv680_do_ioctl);
1251}
1252
1253static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
1254{
1255 struct video_device *dev = file->private_data;
1256 struct usb_stv *stv680 = video_get_drvdata(dev);
1257 unsigned long start = vma->vm_start;
1258 unsigned long size = vma->vm_end-vma->vm_start;
1259 unsigned long page, pos;
1260
1261 down (&stv680->lock);
1262
1263 if (stv680->udev == NULL) {
1264 up (&stv680->lock);
1265 return -EIO;
1266 }
1267 if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1)
1268 & ~(PAGE_SIZE - 1))) {
1269 up (&stv680->lock);
1270 return -EINVAL;
1271 }
1272 pos = (unsigned long) stv680->fbuf;
1273 while (size > 0) {
1274 page = vmalloc_to_pfn((void *)pos);
1275 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
1276 up (&stv680->lock);
1277 return -EAGAIN;
1278 }
1279 start += PAGE_SIZE;
1280 pos += PAGE_SIZE;
1281 if (size > PAGE_SIZE)
1282 size -= PAGE_SIZE;
1283 else
1284 size = 0;
1285 }
1286 up (&stv680->lock);
1287
1288 return 0;
1289}
1290
1291static ssize_t stv680_read (struct file *file, char __user *buf,
1292 size_t count, loff_t *ppos)
1293{
1294 struct video_device *dev = file->private_data;
1295 unsigned long int realcount = count;
1296 int ret = 0;
1297 struct usb_stv *stv680 = video_get_drvdata(dev);
1298 unsigned long int i;
1299
1300 if (STV680_NUMFRAMES != 2) {
1301 PDEBUG (0, "STV(e): STV680_NUMFRAMES needs to be 2!");
1302 return -1;
1303 }
1304 if (stv680->udev == NULL)
1305 return -EIO;
1306 if (realcount > (stv680->vwidth * stv680->vheight * 3))
1307 realcount = stv680->vwidth * stv680->vheight * 3;
1308
1309 /* Shouldn't happen: */
1310 if (stv680->frame[0].grabstate == FRAME_GRABBING) {
1311 PDEBUG (2, "STV(e): FRAME_GRABBING in stv680_read");
1312 return -EBUSY;
1313 }
1314 stv680->frame[0].grabstate = FRAME_READY;
1315 stv680->frame[1].grabstate = FRAME_UNUSED;
1316 stv680->curframe = 0;
1317
1318 if (!stv680->streaming)
1319 stv680_start_stream (stv680);
1320
1321 if (!stv680->streaming) {
1322 ret = stv680_newframe (stv680, 0); /* ret should = 0 */
1323 }
1324
1325 ret = stv680_newframe (stv680, 0);
1326
1327 if (!ret) {
1328 if ((i = copy_to_user (buf, stv680->frame[0].data, realcount)) != 0) {
1329 PDEBUG (2, "STV(e): copy_to_user frame 0 failed, ret count = %li", i);
1330 return -EFAULT;
1331 }
1332 } else {
1333 realcount = ret;
1334 }
1335 stv680->frame[0].grabstate = FRAME_UNUSED;
1336 return realcount;
1337} /* stv680_read */
1338
1339static struct file_operations stv680_fops = {
1340 .owner = THIS_MODULE,
1341 .open = stv_open,
1342 .release = stv_close,
1343 .read = stv680_read,
1344 .mmap = stv680_mmap,
1345 .ioctl = stv680_ioctl,
1346 .compat_ioctl = v4l_compat_ioctl32,
1347 .llseek = no_llseek,
1348};
1349static struct video_device stv680_template = {
1350 .owner = THIS_MODULE,
1351 .name = "STV0680 USB camera",
1352 .type = VID_TYPE_CAPTURE,
1353 .hardware = VID_HARDWARE_SE401,
1354 .fops = &stv680_fops,
1355 .release = video_device_release,
1356 .minor = -1,
1357};
1358
1359static int stv680_probe (struct usb_interface *intf, const struct usb_device_id *id)
1360{
1361 struct usb_device *dev = interface_to_usbdev(intf);
1362 struct usb_host_interface *interface;
1363 struct usb_stv *stv680 = NULL;
1364 char *camera_name = NULL;
1365 int retval = 0;
1366
1367 /* We don't handle multi-config cameras */
1368 if (dev->descriptor.bNumConfigurations != 1) {
1369 PDEBUG (0, "STV(e): Number of Configurations != 1");
1370 return -ENODEV;
1371 }
1372
1373 interface = &intf->altsetting[0];
1374 /* Is it a STV680? */
1375 if ((le16_to_cpu(dev->descriptor.idVendor) == USB_PENCAM_VENDOR_ID) &&
1376 (le16_to_cpu(dev->descriptor.idProduct) == USB_PENCAM_PRODUCT_ID)) {
1377 camera_name = "STV0680";
1378 PDEBUG (0, "STV(i): STV0680 camera found.");
1379 } else if ((le16_to_cpu(dev->descriptor.idVendor) == USB_CREATIVEGOMINI_VENDOR_ID) &&
1380 (le16_to_cpu(dev->descriptor.idProduct) == USB_CREATIVEGOMINI_PRODUCT_ID)) {
1381 camera_name = "Creative WebCam Go Mini";
1382 PDEBUG (0, "STV(i): Creative WebCam Go Mini found.");
1383 } else {
1384 PDEBUG (0, "STV(e): Vendor/Product ID do not match STV0680 or Creative WebCam Go Mini values.");
1385 PDEBUG (0, "STV(e): Check that the STV0680 or Creative WebCam Go Mini camera is connected to the computer.");
1386 retval = -ENODEV;
1387 goto error;
1388 }
1389 /* We found one */
1390 if ((stv680 = kmalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) {
1391 PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct.");
1392 retval = -ENOMEM;
1393 goto error;
1394 }
1395
1396 memset (stv680, 0, sizeof (*stv680));
1397
1398 stv680->udev = dev;
1399 stv680->camera_name = camera_name;
1400
1401 stv680->vdev = video_device_alloc();
1402 if (!stv680->vdev) {
1403 retval = -ENOMEM;
1404 goto error;
1405 }
1406 memcpy(stv680->vdev, &stv680_template, sizeof(stv680_template));
1407 stv680->vdev->dev = &intf->dev;
1408 video_set_drvdata(stv680->vdev, stv680);
1409
1410 memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name));
1411 init_waitqueue_head (&stv680->wq);
1412 init_MUTEX (&stv680->lock);
1413 wmb ();
1414
1415 if (video_register_device (stv680->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
1416 PDEBUG (0, "STV(e): video_register_device failed");
1417 retval = -EIO;
1418 goto error_vdev;
1419 }
1420 PDEBUG (0, "STV(i): registered new video device: video%d", stv680->vdev->minor);
1421
1422 usb_set_intfdata (intf, stv680);
1423 stv680_create_sysfs_files(stv680->vdev);
1424 return 0;
1425
1426error_vdev:
1427 video_device_release(stv680->vdev);
1428error:
1429 kfree(stv680);
1430 return retval;
1431}
1432
1433static inline void usb_stv680_remove_disconnected (struct usb_stv *stv680)
1434{
1435 int i;
1436
1437 stv680->udev = NULL;
1438 stv680->frame[0].grabstate = FRAME_ERROR;
1439 stv680->frame[1].grabstate = FRAME_ERROR;
1440 stv680->streaming = 0;
1441
1442 wake_up_interruptible (&stv680->wq);
1443
1444 for (i = 0; i < STV680_NUMSBUF; i++)
1445 if (stv680->urb[i]) {
1446 usb_kill_urb (stv680->urb[i]);
1447 usb_free_urb (stv680->urb[i]);
1448 stv680->urb[i] = NULL;
1449 kfree(stv680->sbuf[i].data);
1450 }
1451 for (i = 0; i < STV680_NUMSCRATCH; i++)
1452 kfree(stv680->scratch[i].data);
1453 PDEBUG (0, "STV(i): %s disconnected", stv680->camera_name);
1454
1455 /* Free the memory */
1456 kfree(stv680);
1457}
1458
1459static void stv680_disconnect (struct usb_interface *intf)
1460{
1461 struct usb_stv *stv680 = usb_get_intfdata (intf);
1462
1463 usb_set_intfdata (intf, NULL);
1464
1465 if (stv680) {
1466 /* We don't want people trying to open up the device */
1467 if (stv680->vdev) {
1468 stv680_remove_sysfs_files(stv680->vdev);
1469 video_unregister_device(stv680->vdev);
1470 stv680->vdev = NULL;
1471 }
1472 if (!stv680->user) {
1473 usb_stv680_remove_disconnected (stv680);
1474 } else {
1475 stv680->removed = 1;
1476 }
1477 }
1478}
1479
1480static struct usb_driver stv680_driver = {
1481 .name = "stv680",
1482 .probe = stv680_probe,
1483 .disconnect = stv680_disconnect,
1484 .id_table = device_table
1485};
1486
1487/********************************************************************
1488 * Module routines
1489 ********************************************************************/
1490
1491static int __init usb_stv680_init (void)
1492{
1493 if (usb_register (&stv680_driver) < 0) {
1494 PDEBUG (0, "STV(e): Could not setup STV0680 driver");
1495 return -1;
1496 }
1497 PDEBUG (0, "STV(i): usb camera driver version %s registering", DRIVER_VERSION);
1498
1499 info(DRIVER_DESC " " DRIVER_VERSION);
1500 return 0;
1501}
1502
1503static void __exit usb_stv680_exit (void)
1504{
1505 usb_deregister (&stv680_driver);
1506 PDEBUG (0, "STV(i): driver deregistered");
1507}
1508
1509module_init (usb_stv680_init);
1510module_exit (usb_stv680_exit);
diff --git a/drivers/usb/media/stv680.h b/drivers/usb/media/stv680.h
deleted file mode 100644
index b0551cdb280b..000000000000
--- a/drivers/usb/media/stv680.h
+++ /dev/null
@@ -1,227 +0,0 @@
1/****************************************************************************
2 *
3 * Filename: stv680.h
4 *
5 * Description:
6 * This is a USB driver for STV0680 based usb video cameras.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 ****************************************************************************/
23
24/* size of usb transfers */
25#define STV680_PACKETSIZE 4096
26
27/* number of queued bulk transfers to use, may have problems if > 1 */
28#define STV680_NUMSBUF 1
29
30/* number of frames supported by the v4l part */
31#define STV680_NUMFRAMES 2
32
33/* scratch buffers for passing data to the decoders: 2 or 4 are good */
34#define STV680_NUMSCRATCH 2
35
36/* number of nul sized packets to receive before kicking the camera */
37#define STV680_MAX_NULLPACKETS 200
38
39/* number of decoding errors before kicking the camera */
40#define STV680_MAX_ERRORS 100
41
42#define USB_PENCAM_VENDOR_ID 0x0553
43#define USB_PENCAM_PRODUCT_ID 0x0202
44
45#define USB_CREATIVEGOMINI_VENDOR_ID 0x041e
46#define USB_CREATIVEGOMINI_PRODUCT_ID 0x4007
47
48#define PENCAM_TIMEOUT 1000
49/* fmt 4 */
50#define STV_VIDEO_PALETTE VIDEO_PALETTE_RGB24
51
52static struct usb_device_id device_table[] = {
53 {USB_DEVICE (USB_PENCAM_VENDOR_ID, USB_PENCAM_PRODUCT_ID)},
54 {USB_DEVICE (USB_CREATIVEGOMINI_VENDOR_ID, USB_CREATIVEGOMINI_PRODUCT_ID)},
55 {}
56};
57MODULE_DEVICE_TABLE (usb, device_table);
58
59struct stv680_sbuf {
60 unsigned char *data;
61};
62
63enum {
64 FRAME_UNUSED, /* Unused (no MCAPTURE) */
65 FRAME_READY, /* Ready to start grabbing */
66 FRAME_GRABBING, /* In the process of being grabbed into */
67 FRAME_DONE, /* Finished grabbing, but not been synced yet */
68 FRAME_ERROR, /* Something bad happened while processing */
69};
70
71enum {
72 BUFFER_UNUSED,
73 BUFFER_READY,
74 BUFFER_BUSY,
75 BUFFER_DONE,
76};
77
78/* raw camera data <- sbuf (urb transfer buf) */
79struct stv680_scratch {
80 unsigned char *data;
81 volatile int state;
82 int offset;
83 int length;
84};
85
86/* processed data for display ends up here, after bayer */
87struct stv680_frame {
88 unsigned char *data; /* Frame buffer */
89 volatile int grabstate; /* State of grabbing */
90 unsigned char *curline;
91 int curlinepix;
92 int curpix;
93};
94
95/* this is almost the video structure uvd_t, with extra parameters for stv */
96struct usb_stv {
97 struct video_device *vdev;
98
99 struct usb_device *udev;
100
101 unsigned char bulk_in_endpointAddr; /* __u8 the address of the bulk in endpoint */
102 char *camera_name;
103
104 unsigned int VideoMode; /* 0x0100 = VGA, 0x0000 = CIF, 0x0300 = QVGA */
105 int SupportedModes;
106 int CIF;
107 int VGA;
108 int QVGA;
109 int cwidth; /* camera width */
110 int cheight; /* camera height */
111 int maxwidth; /* max video width */
112 int maxheight; /* max video height */
113 int vwidth; /* current width for video window */
114 int vheight; /* current height for video window */
115 unsigned long int rawbufsize;
116 unsigned long int maxframesize; /* rawbufsize * 3 for RGB */
117
118 int origGain;
119 int origMode; /* original camera mode */
120
121 struct semaphore lock; /* to lock the structure */
122 int user; /* user count for exclusive use */
123 int removed; /* device disconnected */
124 int streaming; /* Are we streaming video? */
125 char *fbuf; /* Videodev buffer area */
126 struct urb *urb[STV680_NUMSBUF]; /* # of queued bulk transfers */
127 int curframe; /* Current receiving frame */
128 struct stv680_frame frame[STV680_NUMFRAMES]; /* # frames supported by v4l part */
129 int readcount;
130 int framecount;
131 int error;
132 int dropped;
133 int scratch_next;
134 int scratch_use;
135 int scratch_overflow;
136 struct stv680_scratch scratch[STV680_NUMSCRATCH]; /* for decoders */
137 struct stv680_sbuf sbuf[STV680_NUMSBUF];
138
139 unsigned int brightness;
140 unsigned int chgbright;
141 unsigned int whiteness;
142 unsigned int colour;
143 unsigned int contrast;
144 unsigned int hue;
145 unsigned int palette;
146 unsigned int depth; /* rgb24 in bits */
147
148 wait_queue_head_t wq; /* Processes waiting */
149
150 int nullpackets;
151};
152
153
154static const unsigned char red[256] = {
155 0, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
156 18, 18, 18, 18, 18, 18, 18, 25, 30, 35, 38, 42,
157 44, 47, 50, 53, 54, 57, 59, 61, 63, 65, 67, 69,
158 71, 71, 73, 75, 77, 78, 80, 81, 82, 84, 85, 87,
159 88, 89, 90, 91, 93, 94, 95, 97, 98, 98, 99, 101,
160 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
161 114, 115, 116, 116, 117, 118, 119, 120, 121, 122, 123, 124,
162 125, 125, 126, 127, 128, 129, 129, 130, 131, 132, 133, 134,
163 134, 135, 135, 136, 137, 138, 139, 140, 140, 141, 142, 143,
164 143, 143, 144, 145, 146, 147, 147, 148, 149, 150, 150, 151,
165 152, 152, 152, 153, 154, 154, 155, 156, 157, 157, 158, 159,
166 159, 160, 161, 161, 161, 162, 163, 163, 164, 165, 165, 166,
167 167, 167, 168, 168, 169, 170, 170, 170, 171, 171, 172, 173,
168 173, 174, 174, 175, 176, 176, 177, 178, 178, 179, 179, 179,
169 180, 180, 181, 181, 182, 183, 183, 184, 184, 185, 185, 186,
170 187, 187, 188, 188, 188, 188, 189, 190, 190, 191, 191, 192,
171 192, 193, 193, 194, 195, 195, 196, 196, 197, 197, 197, 197,
172 198, 198, 199, 199, 200, 201, 201, 202, 202, 203, 203, 204,
173 204, 205, 205, 206, 206, 206, 206, 207, 207, 208, 208, 209,
174 209, 210, 210, 211, 211, 212, 212, 213, 213, 214, 214, 215,
175 215, 215, 215, 216, 216, 217, 217, 218, 218, 218, 219, 219,
176 220, 220, 221, 221
177};
178
179static const unsigned char green[256] = {
180 0, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
181 21, 21, 21, 21, 21, 21, 21, 28, 34, 39, 43, 47,
182 50, 53, 56, 59, 61, 64, 66, 68, 71, 73, 75, 77,
183 79, 80, 82, 84, 86, 87, 89, 91, 92, 94, 95, 97,
184 98, 100, 101, 102, 104, 105, 106, 108, 109, 110, 111, 113,
185 114, 115, 116, 117, 118, 120, 121, 122, 123, 124, 125, 126,
186 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
187 139, 140, 141, 142, 143, 144, 144, 145, 146, 147, 148, 149,
188 150, 151, 151, 152, 153, 154, 155, 156, 156, 157, 158, 159,
189 160, 160, 161, 162, 163, 164, 164, 165, 166, 167, 167, 168,
190 169, 170, 170, 171, 172, 172, 173, 174, 175, 175, 176, 177,
191 177, 178, 179, 179, 180, 181, 182, 182, 183, 184, 184, 185,
192 186, 186, 187, 187, 188, 189, 189, 190, 191, 191, 192, 193,
193 193, 194, 194, 195, 196, 196, 197, 198, 198, 199, 199, 200,
194 201, 201, 202, 202, 203, 204, 204, 205, 205, 206, 206, 207,
195 208, 208, 209, 209, 210, 210, 211, 212, 212, 213, 213, 214,
196 214, 215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 220,
197 221, 221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227,
198 227, 228, 228, 229, 229, 230, 230, 231, 231, 232, 232, 233,
199 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238, 239,
200 239, 240, 240, 241, 241, 242, 242, 243, 243, 243, 244, 244,
201 245, 245, 246, 246
202};
203
204static const unsigned char blue[256] = {
205 0, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
206 23, 23, 23, 23, 23, 23, 23, 30, 37, 42, 47, 51,
207 55, 58, 61, 64, 67, 70, 72, 74, 78, 80, 82, 84,
208 86, 88, 90, 92, 94, 95, 97, 100, 101, 103, 104, 106,
209 107, 110, 111, 112, 114, 115, 116, 118, 119, 121, 122, 124,
210 125, 126, 127, 128, 129, 132, 133, 134, 135, 136, 137, 138,
211 139, 140, 141, 143, 144, 145, 146, 147, 148, 149, 150, 151,
212 152, 154, 155, 156, 157, 158, 158, 159, 160, 161, 162, 163,
213 165, 166, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174,
214 176, 176, 177, 178, 179, 180, 180, 181, 182, 183, 183, 184,
215 185, 187, 187, 188, 189, 189, 190, 191, 192, 192, 193, 194,
216 194, 195, 196, 196, 198, 199, 200, 200, 201, 202, 202, 203,
217 204, 204, 205, 205, 206, 207, 207, 209, 210, 210, 211, 212,
218 212, 213, 213, 214, 215, 215, 216, 217, 217, 218, 218, 220,
219 221, 221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227,
220 228, 228, 229, 229, 231, 231, 232, 233, 233, 234, 234, 235,
221 235, 236, 236, 237, 238, 238, 239, 239, 240, 240, 242, 242,
222 243, 243, 244, 244, 245, 246, 246, 247, 247, 248, 248, 249,
223 249, 250, 250, 251, 251, 253, 253, 254, 254, 255, 255, 255,
224 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
225 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
226 255, 255, 255, 255
227};
diff --git a/drivers/usb/media/ultracam.c b/drivers/usb/media/ultracam.c
deleted file mode 100644
index 75ff755224df..000000000000
--- a/drivers/usb/media/ultracam.c
+++ /dev/null
@@ -1,679 +0,0 @@
1/*
2 * USB NB Camera driver
3 *
4 * HISTORY:
5 * 25-Dec-2002 Dmitri Removed lighting, sharpness parameters, methods.
6 */
7
8#include <linux/kernel.h>
9#include <linux/sched.h>
10#include <linux/module.h>
11#include <linux/init.h>
12
13#include "usbvideo.h"
14
15#define ULTRACAM_VENDOR_ID 0x0461
16#define ULTRACAM_PRODUCT_ID 0x0813
17
18#define MAX_CAMERAS 4 /* How many devices we allow to connect */
19
20/*
21 * This structure lives in uvd_t->user field.
22 */
23typedef struct {
24 int initialized; /* Had we already sent init sequence? */
25 int camera_model; /* What type of IBM camera we got? */
26 int has_hdr;
27} ultracam_t;
28#define ULTRACAM_T(uvd) ((ultracam_t *)((uvd)->user_data))
29
30static struct usbvideo *cams = NULL;
31
32static int debug = 0;
33
34static int flags = 0; /* FLAGS_DISPLAY_HINTS | FLAGS_OVERLAY_STATS; */
35
36static const int min_canvasWidth = 8;
37static const int min_canvasHeight = 4;
38
39#define FRAMERATE_MIN 0
40#define FRAMERATE_MAX 6
41static int framerate = -1;
42
43/*
44 * Here we define several initialization variables. They may
45 * be used to automatically set color, hue, brightness and
46 * contrast to desired values. This is particularly useful in
47 * case of webcams (which have no controls and no on-screen
48 * output) and also when a client V4L software is used that
49 * does not have some of those controls. In any case it's
50 * good to have startup values as options.
51 *
52 * These values are all in [0..255] range. This simplifies
53 * operation. Note that actual values of V4L variables may
54 * be scaled up (as much as << 8). User can see that only
55 * on overlay output, however, or through a V4L client.
56 */
57static int init_brightness = 128;
58static int init_contrast = 192;
59static int init_color = 128;
60static int init_hue = 128;
61static int hue_correction = 128;
62
63module_param(debug, int, S_IRUGO | S_IWUSR);
64MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
65module_param(flags, int, 0);
66MODULE_PARM_DESC(flags,
67 "Bitfield: 0=VIDIOCSYNC, "
68 "1=B/W, "
69 "2=show hints, "
70 "3=show stats, "
71 "4=test pattern, "
72 "5=separate frames, "
73 "6=clean frames");
74module_param(framerate, int, 0);
75MODULE_PARM_DESC(framerate, "Framerate setting: 0=slowest, 6=fastest (default=2)");
76
77module_param(init_brightness, int, 0);
78MODULE_PARM_DESC(init_brightness, "Brightness preconfiguration: 0-255 (default=128)");
79module_param(init_contrast, int, 0);
80MODULE_PARM_DESC(init_contrast, "Contrast preconfiguration: 0-255 (default=192)");
81module_param(init_color, int, 0);
82MODULE_PARM_DESC(init_color, "Color preconfiguration: 0-255 (default=128)");
83module_param(init_hue, int, 0);
84MODULE_PARM_DESC(init_hue, "Hue preconfiguration: 0-255 (default=128)");
85module_param(hue_correction, int, 0);
86MODULE_PARM_DESC(hue_correction, "YUV colorspace regulation: 0-255 (default=128)");
87
88/*
89 * ultracam_ProcessIsocData()
90 *
91 * Generic routine to parse the ring queue data. It employs either
92 * ultracam_find_header() or ultracam_parse_lines() to do most
93 * of work.
94 *
95 * 02-Nov-2000 First (mostly dummy) version.
96 * 06-Nov-2000 Rewrote to dump all data into frame.
97 */
98static void ultracam_ProcessIsocData(struct uvd *uvd, struct usbvideo_frame *frame)
99{
100 int n;
101
102 assert(uvd != NULL);
103 assert(frame != NULL);
104
105 /* Try to move data from queue into frame buffer */
106 n = RingQueue_GetLength(&uvd->dp);
107 if (n > 0) {
108 int m;
109 /* See how much spare we have left */
110 m = uvd->max_frame_size - frame->seqRead_Length;
111 if (n > m)
112 n = m;
113 /* Now move that much data into frame buffer */
114 RingQueue_Dequeue(
115 &uvd->dp,
116 frame->data + frame->seqRead_Length,
117 m);
118 frame->seqRead_Length += m;
119 }
120 /* See if we filled the frame */
121 if (frame->seqRead_Length >= uvd->max_frame_size) {
122 frame->frameState = FrameState_Done;
123 uvd->curframe = -1;
124 uvd->stats.frame_num++;
125 }
126}
127
128/*
129 * ultracam_veio()
130 *
131 * History:
132 * 1/27/00 Added check for dev == NULL; this happens if camera is unplugged.
133 */
134static int ultracam_veio(
135 struct uvd *uvd,
136 unsigned char req,
137 unsigned short value,
138 unsigned short index,
139 int is_out)
140{
141 static const char proc[] = "ultracam_veio";
142 unsigned char cp[8] /* = { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef } */;
143 int i;
144
145 if (!CAMERA_IS_OPERATIONAL(uvd))
146 return 0;
147
148 if (!is_out) {
149 i = usb_control_msg(
150 uvd->dev,
151 usb_rcvctrlpipe(uvd->dev, 0),
152 req,
153 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
154 value,
155 index,
156 cp,
157 sizeof(cp),
158 1000);
159#if 1
160 info("USB => %02x%02x%02x%02x%02x%02x%02x%02x "
161 "(req=$%02x val=$%04x ind=$%04x)",
162 cp[0],cp[1],cp[2],cp[3],cp[4],cp[5],cp[6],cp[7],
163 req, value, index);
164#endif
165 } else {
166 i = usb_control_msg(
167 uvd->dev,
168 usb_sndctrlpipe(uvd->dev, 0),
169 req,
170 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
171 value,
172 index,
173 NULL,
174 0,
175 1000);
176 }
177 if (i < 0) {
178 err("%s: ERROR=%d. Camera stopped; Reconnect or reload driver.",
179 proc, i);
180 uvd->last_error = i;
181 }
182 return i;
183}
184
185/*
186 * ultracam_calculate_fps()
187 */
188static int ultracam_calculate_fps(struct uvd *uvd)
189{
190 return 3 + framerate*4 + framerate/2;
191}
192
193/*
194 * ultracam_adjust_contrast()
195 */
196static void ultracam_adjust_contrast(struct uvd *uvd)
197{
198}
199
200/*
201 * ultracam_set_brightness()
202 *
203 * This procedure changes brightness of the picture.
204 */
205static void ultracam_set_brightness(struct uvd *uvd)
206{
207}
208
209static void ultracam_set_hue(struct uvd *uvd)
210{
211}
212
213/*
214 * ultracam_adjust_picture()
215 *
216 * This procedure gets called from V4L interface to update picture settings.
217 * Here we change brightness and contrast.
218 */
219static void ultracam_adjust_picture(struct uvd *uvd)
220{
221 ultracam_adjust_contrast(uvd);
222 ultracam_set_brightness(uvd);
223 ultracam_set_hue(uvd);
224}
225
226/*
227 * ultracam_video_stop()
228 *
229 * This code tells camera to stop streaming. The interface remains
230 * configured and bandwidth - claimed.
231 */
232static void ultracam_video_stop(struct uvd *uvd)
233{
234}
235
236/*
237 * ultracam_reinit_iso()
238 *
239 * This procedure sends couple of commands to the camera and then
240 * resets the video pipe. This sequence was observed to reinit the
241 * camera or, at least, to initiate ISO data stream.
242 */
243static void ultracam_reinit_iso(struct uvd *uvd, int do_stop)
244{
245}
246
247static void ultracam_video_start(struct uvd *uvd)
248{
249 ultracam_reinit_iso(uvd, 0);
250}
251
252static int ultracam_resetPipe(struct uvd *uvd)
253{
254 usb_clear_halt(uvd->dev, uvd->video_endp);
255 return 0;
256}
257
258static int ultracam_alternateSetting(struct uvd *uvd, int setting)
259{
260 static const char proc[] = "ultracam_alternateSetting";
261 int i;
262 i = usb_set_interface(uvd->dev, uvd->iface, setting);
263 if (i < 0) {
264 err("%s: usb_set_interface error", proc);
265 uvd->last_error = i;
266 return -EBUSY;
267 }
268 return 0;
269}
270
271/*
272 * Return negative code on failure, 0 on success.
273 */
274static int ultracam_setup_on_open(struct uvd *uvd)
275{
276 int setup_ok = 0; /* Success by default */
277 /* Send init sequence only once, it's large! */
278 if (!ULTRACAM_T(uvd)->initialized) {
279 ultracam_alternateSetting(uvd, 0x04);
280 ultracam_alternateSetting(uvd, 0x00);
281 ultracam_veio(uvd, 0x02, 0x0004, 0x000b, 1);
282 ultracam_veio(uvd, 0x02, 0x0001, 0x0005, 1);
283 ultracam_veio(uvd, 0x02, 0x8000, 0x0000, 1);
284 ultracam_veio(uvd, 0x00, 0x0000, 0x0000, 1);
285 ultracam_veio(uvd, 0x00, 0x00b0, 0x0001, 1);
286 ultracam_veio(uvd, 0x00, 0x0000, 0x0002, 1);
287 ultracam_veio(uvd, 0x00, 0x000c, 0x0003, 1);
288 ultracam_veio(uvd, 0x00, 0x000b, 0x0004, 1);
289 ultracam_veio(uvd, 0x00, 0x0000, 0x0005, 1);
290 ultracam_veio(uvd, 0x00, 0x0000, 0x0006, 1);
291 ultracam_veio(uvd, 0x00, 0x0079, 0x0007, 1);
292 ultracam_veio(uvd, 0x00, 0x003b, 0x0008, 1);
293 ultracam_veio(uvd, 0x00, 0x0002, 0x000f, 1);
294 ultracam_veio(uvd, 0x00, 0x0001, 0x0010, 1);
295 ultracam_veio(uvd, 0x00, 0x0000, 0x0011, 1);
296 ultracam_veio(uvd, 0x00, 0x0000, 0x00bf, 1);
297 ultracam_veio(uvd, 0x00, 0x0001, 0x00c0, 1);
298 ultracam_veio(uvd, 0x00, 0x0010, 0x00cb, 1);
299 ultracam_veio(uvd, 0x01, 0x00a4, 0x0001, 1);
300 ultracam_veio(uvd, 0x01, 0x0010, 0x0002, 1);
301 ultracam_veio(uvd, 0x01, 0x0066, 0x0007, 1);
302 ultracam_veio(uvd, 0x01, 0x000b, 0x0008, 1);
303 ultracam_veio(uvd, 0x01, 0x0034, 0x0009, 1);
304 ultracam_veio(uvd, 0x01, 0x0000, 0x000a, 1);
305 ultracam_veio(uvd, 0x01, 0x002e, 0x000b, 1);
306 ultracam_veio(uvd, 0x01, 0x00d6, 0x000c, 1);
307 ultracam_veio(uvd, 0x01, 0x00fc, 0x000d, 1);
308 ultracam_veio(uvd, 0x01, 0x00f1, 0x000e, 1);
309 ultracam_veio(uvd, 0x01, 0x00da, 0x000f, 1);
310 ultracam_veio(uvd, 0x01, 0x0036, 0x0010, 1);
311 ultracam_veio(uvd, 0x01, 0x000b, 0x0011, 1);
312 ultracam_veio(uvd, 0x01, 0x0001, 0x0012, 1);
313 ultracam_veio(uvd, 0x01, 0x0000, 0x0013, 1);
314 ultracam_veio(uvd, 0x01, 0x0000, 0x0014, 1);
315 ultracam_veio(uvd, 0x01, 0x0087, 0x0051, 1);
316 ultracam_veio(uvd, 0x01, 0x0040, 0x0052, 1);
317 ultracam_veio(uvd, 0x01, 0x0058, 0x0053, 1);
318 ultracam_veio(uvd, 0x01, 0x0040, 0x0054, 1);
319 ultracam_veio(uvd, 0x01, 0x0000, 0x0040, 1);
320 ultracam_veio(uvd, 0x01, 0x0010, 0x0041, 1);
321 ultracam_veio(uvd, 0x01, 0x0020, 0x0042, 1);
322 ultracam_veio(uvd, 0x01, 0x0030, 0x0043, 1);
323 ultracam_veio(uvd, 0x01, 0x0040, 0x0044, 1);
324 ultracam_veio(uvd, 0x01, 0x0050, 0x0045, 1);
325 ultracam_veio(uvd, 0x01, 0x0060, 0x0046, 1);
326 ultracam_veio(uvd, 0x01, 0x0070, 0x0047, 1);
327 ultracam_veio(uvd, 0x01, 0x0080, 0x0048, 1);
328 ultracam_veio(uvd, 0x01, 0x0090, 0x0049, 1);
329 ultracam_veio(uvd, 0x01, 0x00a0, 0x004a, 1);
330 ultracam_veio(uvd, 0x01, 0x00b0, 0x004b, 1);
331 ultracam_veio(uvd, 0x01, 0x00c0, 0x004c, 1);
332 ultracam_veio(uvd, 0x01, 0x00d0, 0x004d, 1);
333 ultracam_veio(uvd, 0x01, 0x00e0, 0x004e, 1);
334 ultracam_veio(uvd, 0x01, 0x00f0, 0x004f, 1);
335 ultracam_veio(uvd, 0x01, 0x00ff, 0x0050, 1);
336 ultracam_veio(uvd, 0x01, 0x0000, 0x0056, 1);
337 ultracam_veio(uvd, 0x00, 0x0080, 0x00c1, 1);
338 ultracam_veio(uvd, 0x00, 0x0000, 0x00c2, 1);
339 ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
340 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
341 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
342 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
343 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
344 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
345 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
346 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
347 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
348 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
349 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
350 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
351 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
352 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
353 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
354 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
355 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
356 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
357 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
358 ultracam_veio(uvd, 0x00, 0x0080, 0x00c1, 1);
359 ultracam_veio(uvd, 0x00, 0x0004, 0x00c2, 1);
360 ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
361 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
362 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
363 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
364 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
365 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
366 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
367 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
368 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
369 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
370 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
371 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
372 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
373 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
374 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
375 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
376 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
377 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
378 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
379 ultracam_veio(uvd, 0x00, 0x0002, 0x00c1, 1);
380 ultracam_veio(uvd, 0x00, 0x0020, 0x00c2, 1);
381 ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
382 ultracam_veio(uvd, 0x00, 0x0000, 0x00c3, 1);
383 ultracam_veio(uvd, 0x00, 0x0000, 0x00c4, 1);
384 ultracam_veio(uvd, 0x00, 0x0000, 0x00c5, 1);
385 ultracam_veio(uvd, 0x00, 0x0000, 0x00c6, 1);
386 ultracam_veio(uvd, 0x00, 0x0000, 0x00c7, 1);
387 ultracam_veio(uvd, 0x00, 0x0000, 0x00c8, 1);
388 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
389 ultracam_veio(uvd, 0x00, 0x0000, 0x00c3, 1);
390 ultracam_veio(uvd, 0x00, 0x0000, 0x00c4, 1);
391 ultracam_veio(uvd, 0x00, 0x0000, 0x00c5, 1);
392 ultracam_veio(uvd, 0x00, 0x0000, 0x00c6, 1);
393 ultracam_veio(uvd, 0x00, 0x0000, 0x00c7, 1);
394 ultracam_veio(uvd, 0x00, 0x0000, 0x00c8, 1);
395 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
396 ultracam_veio(uvd, 0x00, 0x0040, 0x00c1, 1);
397 ultracam_veio(uvd, 0x00, 0x0017, 0x00c2, 1);
398 ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
399 ultracam_veio(uvd, 0x00, 0x0000, 0x00c3, 1);
400 ultracam_veio(uvd, 0x00, 0x0000, 0x00c4, 1);
401 ultracam_veio(uvd, 0x00, 0x0000, 0x00c5, 1);
402 ultracam_veio(uvd, 0x00, 0x0000, 0x00c6, 1);
403 ultracam_veio(uvd, 0x00, 0x0000, 0x00c7, 1);
404 ultracam_veio(uvd, 0x00, 0x0000, 0x00c8, 1);
405 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
406 ultracam_veio(uvd, 0x00, 0x0000, 0x00c3, 1);
407 ultracam_veio(uvd, 0x00, 0x0000, 0x00c4, 1);
408 ultracam_veio(uvd, 0x00, 0x0000, 0x00c5, 1);
409 ultracam_veio(uvd, 0x00, 0x0000, 0x00c6, 1);
410 ultracam_veio(uvd, 0x00, 0x0000, 0x00c7, 1);
411 ultracam_veio(uvd, 0x00, 0x0000, 0x00c8, 1);
412 ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
413 ultracam_veio(uvd, 0x00, 0x00c0, 0x00c1, 1);
414 ultracam_veio(uvd, 0x00, 0x0000, 0x00c2, 1);
415 ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
416 ultracam_veio(uvd, 0x02, 0xc040, 0x0001, 1);
417 ultracam_veio(uvd, 0x01, 0x0000, 0x0008, 0);
418 ultracam_veio(uvd, 0x01, 0x0000, 0x0009, 0);
419 ultracam_veio(uvd, 0x01, 0x0000, 0x000a, 0);
420 ultracam_veio(uvd, 0x01, 0x0000, 0x000b, 0);
421 ultracam_veio(uvd, 0x01, 0x0000, 0x000c, 0);
422 ultracam_veio(uvd, 0x01, 0x0000, 0x000d, 0);
423 ultracam_veio(uvd, 0x01, 0x0000, 0x000e, 0);
424 ultracam_veio(uvd, 0x01, 0x0000, 0x000f, 0);
425 ultracam_veio(uvd, 0x01, 0x0000, 0x0010, 0);
426 ultracam_veio(uvd, 0x01, 0x000b, 0x0008, 1);
427 ultracam_veio(uvd, 0x01, 0x0034, 0x0009, 1);
428 ultracam_veio(uvd, 0x01, 0x0000, 0x000a, 1);
429 ultracam_veio(uvd, 0x01, 0x002e, 0x000b, 1);
430 ultracam_veio(uvd, 0x01, 0x00d6, 0x000c, 1);
431 ultracam_veio(uvd, 0x01, 0x00fc, 0x000d, 1);
432 ultracam_veio(uvd, 0x01, 0x00f1, 0x000e, 1);
433 ultracam_veio(uvd, 0x01, 0x00da, 0x000f, 1);
434 ultracam_veio(uvd, 0x01, 0x0036, 0x0010, 1);
435 ultracam_veio(uvd, 0x01, 0x0000, 0x0001, 0);
436 ultracam_veio(uvd, 0x01, 0x0064, 0x0001, 1);
437 ultracam_veio(uvd, 0x01, 0x0059, 0x0051, 1);
438 ultracam_veio(uvd, 0x01, 0x003f, 0x0052, 1);
439 ultracam_veio(uvd, 0x01, 0x0094, 0x0053, 1);
440 ultracam_veio(uvd, 0x01, 0x00ff, 0x0011, 1);
441 ultracam_veio(uvd, 0x01, 0x0003, 0x0012, 1);
442 ultracam_veio(uvd, 0x01, 0x00f7, 0x0013, 1);
443 ultracam_veio(uvd, 0x00, 0x0009, 0x0011, 1);
444 ultracam_veio(uvd, 0x00, 0x0000, 0x0001, 1);
445 ultracam_veio(uvd, 0x00, 0x0000, 0x0000, 1);
446 ultracam_veio(uvd, 0x00, 0x0020, 0x00c1, 1);
447 ultracam_veio(uvd, 0x00, 0x0010, 0x00c2, 1);
448 ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
449 ultracam_alternateSetting(uvd, 0x04);
450 ultracam_veio(uvd, 0x02, 0x0000, 0x0001, 1);
451 ultracam_veio(uvd, 0x02, 0x0000, 0x0001, 1);
452 ultracam_veio(uvd, 0x02, 0x0000, 0x0006, 1);
453 ultracam_veio(uvd, 0x02, 0x9000, 0x0007, 1);
454 ultracam_veio(uvd, 0x02, 0x0042, 0x0001, 1);
455 ultracam_veio(uvd, 0x02, 0x0000, 0x000b, 0);
456 ultracam_resetPipe(uvd);
457 ULTRACAM_T(uvd)->initialized = (setup_ok != 0);
458 }
459 return setup_ok;
460}
461
462static void ultracam_configure_video(struct uvd *uvd)
463{
464 if (uvd == NULL)
465 return;
466
467 RESTRICT_TO_RANGE(init_brightness, 0, 255);
468 RESTRICT_TO_RANGE(init_contrast, 0, 255);
469 RESTRICT_TO_RANGE(init_color, 0, 255);
470 RESTRICT_TO_RANGE(init_hue, 0, 255);
471 RESTRICT_TO_RANGE(hue_correction, 0, 255);
472
473 memset(&uvd->vpic, 0, sizeof(uvd->vpic));
474 memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
475
476 uvd->vpic.colour = init_color << 8;
477 uvd->vpic.hue = init_hue << 8;
478 uvd->vpic.brightness = init_brightness << 8;
479 uvd->vpic.contrast = init_contrast << 8;
480 uvd->vpic.whiteness = 105 << 8; /* This one isn't used */
481 uvd->vpic.depth = 24;
482 uvd->vpic.palette = VIDEO_PALETTE_RGB24;
483
484 memset(&uvd->vcap, 0, sizeof(uvd->vcap));
485 strcpy(uvd->vcap.name, "IBM Ultra Camera");
486 uvd->vcap.type = VID_TYPE_CAPTURE;
487 uvd->vcap.channels = 1;
488 uvd->vcap.audios = 0;
489 uvd->vcap.maxwidth = VIDEOSIZE_X(uvd->canvas);
490 uvd->vcap.maxheight = VIDEOSIZE_Y(uvd->canvas);
491 uvd->vcap.minwidth = min_canvasWidth;
492 uvd->vcap.minheight = min_canvasHeight;
493
494 memset(&uvd->vchan, 0, sizeof(uvd->vchan));
495 uvd->vchan.flags = 0;
496 uvd->vchan.tuners = 0;
497 uvd->vchan.channel = 0;
498 uvd->vchan.type = VIDEO_TYPE_CAMERA;
499 strcpy(uvd->vchan.name, "Camera");
500}
501
502/*
503 * ultracam_probe()
504 *
505 * This procedure queries device descriptor and accepts the interface
506 * if it looks like our camera.
507 *
508 * History:
509 * 12-Nov-2000 Reworked to comply with new probe() signature.
510 * 23-Jan-2001 Added compatibility with 2.2.x kernels.
511 */
512static int ultracam_probe(struct usb_interface *intf, const struct usb_device_id *devid)
513{
514 struct usb_device *dev = interface_to_usbdev(intf);
515 struct uvd *uvd = NULL;
516 int ix, i, nas;
517 int actInterface=-1, inactInterface=-1, maxPS=0;
518 unsigned char video_ep = 0;
519
520 if (debug >= 1)
521 info("ultracam_probe(%p)", intf);
522
523 /* We don't handle multi-config cameras */
524 if (dev->descriptor.bNumConfigurations != 1)
525 return -ENODEV;
526
527 info("IBM Ultra camera found (rev. 0x%04x)",
528 le16_to_cpu(dev->descriptor.bcdDevice));
529
530 /* Validate found interface: must have one ISO endpoint */
531 nas = intf->num_altsetting;
532 if (debug > 0)
533 info("Number of alternate settings=%d.", nas);
534 if (nas < 8) {
535 err("Too few alternate settings for this camera!");
536 return -ENODEV;
537 }
538 /* Validate all alternate settings */
539 for (ix=0; ix < nas; ix++) {
540 const struct usb_host_interface *interface;
541 const struct usb_endpoint_descriptor *endpoint;
542
543 interface = &intf->altsetting[ix];
544 i = interface->desc.bAlternateSetting;
545 if (interface->desc.bNumEndpoints != 1) {
546 err("Interface %d. has %u. endpoints!",
547 interface->desc.bInterfaceNumber,
548 (unsigned)(interface->desc.bNumEndpoints));
549 return -ENODEV;
550 }
551 endpoint = &interface->endpoint[0].desc;
552 if (video_ep == 0)
553 video_ep = endpoint->bEndpointAddress;
554 else if (video_ep != endpoint->bEndpointAddress) {
555 err("Alternate settings have different endpoint addresses!");
556 return -ENODEV;
557 }
558 if ((endpoint->bmAttributes & 0x03) != 0x01) {
559 err("Interface %d. has non-ISO endpoint!",
560 interface->desc.bInterfaceNumber);
561 return -ENODEV;
562 }
563 if ((endpoint->bEndpointAddress & 0x80) == 0) {
564 err("Interface %d. has ISO OUT endpoint!",
565 interface->desc.bInterfaceNumber);
566 return -ENODEV;
567 }
568 if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
569 if (inactInterface < 0)
570 inactInterface = i;
571 else {
572 err("More than one inactive alt. setting!");
573 return -ENODEV;
574 }
575 } else {
576 if (actInterface < 0) {
577 actInterface = i;
578 maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
579 if (debug > 0)
580 info("Active setting=%d. maxPS=%d.", i, maxPS);
581 } else {
582 /* Got another active alt. setting */
583 if (maxPS < le16_to_cpu(endpoint->wMaxPacketSize)) {
584 /* This one is better! */
585 actInterface = i;
586 maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
587 if (debug > 0) {
588 info("Even better ctive setting=%d. maxPS=%d.",
589 i, maxPS);
590 }
591 }
592 }
593 }
594 }
595 if ((maxPS <= 0) || (actInterface < 0) || (inactInterface < 0)) {
596 err("Failed to recognize the camera!");
597 return -ENODEV;
598 }
599
600 uvd = usbvideo_AllocateDevice(cams);
601 if (uvd != NULL) {
602 /* Here uvd is a fully allocated uvd object */
603 uvd->flags = flags;
604 uvd->debug = debug;
605 uvd->dev = dev;
606 uvd->iface = intf->altsetting->desc.bInterfaceNumber;
607 uvd->ifaceAltInactive = inactInterface;
608 uvd->ifaceAltActive = actInterface;
609 uvd->video_endp = video_ep;
610 uvd->iso_packet_len = maxPS;
611 uvd->paletteBits = 1L << VIDEO_PALETTE_RGB24;
612 uvd->defaultPalette = VIDEO_PALETTE_RGB24;
613 uvd->canvas = VIDEOSIZE(640, 480); /* FIXME */
614 uvd->videosize = uvd->canvas; /* ultracam_size_to_videosize(size);*/
615
616 /* Initialize ibmcam-specific data */
617 assert(ULTRACAM_T(uvd) != NULL);
618 ULTRACAM_T(uvd)->camera_model = 0; /* Not used yet */
619 ULTRACAM_T(uvd)->initialized = 0;
620
621 ultracam_configure_video(uvd);
622
623 i = usbvideo_RegisterVideoDevice(uvd);
624 if (i != 0) {
625 err("usbvideo_RegisterVideoDevice() failed.");
626 uvd = NULL;
627 }
628 }
629
630 if (uvd) {
631 usb_set_intfdata (intf, uvd);
632 return 0;
633 }
634 return -EIO;
635}
636
637
638static struct usb_device_id id_table[] = {
639 { USB_DEVICE(ULTRACAM_VENDOR_ID, ULTRACAM_PRODUCT_ID) },
640 { } /* Terminating entry */
641};
642
643/*
644 * ultracam_init()
645 *
646 * This code is run to initialize the driver.
647 */
648static int __init ultracam_init(void)
649{
650 struct usbvideo_cb cbTbl;
651 memset(&cbTbl, 0, sizeof(cbTbl));
652 cbTbl.probe = ultracam_probe;
653 cbTbl.setupOnOpen = ultracam_setup_on_open;
654 cbTbl.videoStart = ultracam_video_start;
655 cbTbl.videoStop = ultracam_video_stop;
656 cbTbl.processData = ultracam_ProcessIsocData;
657 cbTbl.postProcess = usbvideo_DeinterlaceFrame;
658 cbTbl.adjustPicture = ultracam_adjust_picture;
659 cbTbl.getFPS = ultracam_calculate_fps;
660 return usbvideo_register(
661 &cams,
662 MAX_CAMERAS,
663 sizeof(ultracam_t),
664 "ultracam",
665 &cbTbl,
666 THIS_MODULE,
667 id_table);
668}
669
670static void __exit ultracam_cleanup(void)
671{
672 usbvideo_Deregister(&cams);
673}
674
675MODULE_DEVICE_TABLE(usb, id_table);
676MODULE_LICENSE("GPL");
677
678module_init(ultracam_init);
679module_exit(ultracam_cleanup);
diff --git a/drivers/usb/media/usbvideo.c b/drivers/usb/media/usbvideo.c
deleted file mode 100644
index 63a72e550a1b..000000000000
--- a/drivers/usb/media/usbvideo.c
+++ /dev/null
@@ -1,2191 +0,0 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2, or (at your option)
5 * any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17#include <linux/kernel.h>
18#include <linux/sched.h>
19#include <linux/list.h>
20#include <linux/slab.h>
21#include <linux/module.h>
22#include <linux/mm.h>
23#include <linux/smp_lock.h>
24#include <linux/vmalloc.h>
25#include <linux/init.h>
26#include <linux/spinlock.h>
27
28#include <asm/io.h>
29
30#include "usbvideo.h"
31
32#if defined(MAP_NR)
33#define virt_to_page(v) MAP_NR(v) /* Kernels 2.2.x */
34#endif
35
36static int video_nr = -1;
37module_param(video_nr, int, 0);
38
39/*
40 * Local prototypes.
41 */
42static void usbvideo_Disconnect(struct usb_interface *intf);
43static void usbvideo_CameraRelease(struct uvd *uvd);
44
45static int usbvideo_v4l_ioctl(struct inode *inode, struct file *file,
46 unsigned int cmd, unsigned long arg);
47static int usbvideo_v4l_mmap(struct file *file, struct vm_area_struct *vma);
48static int usbvideo_v4l_open(struct inode *inode, struct file *file);
49static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf,
50 size_t count, loff_t *ppos);
51static int usbvideo_v4l_close(struct inode *inode, struct file *file);
52
53static int usbvideo_StartDataPump(struct uvd *uvd);
54static void usbvideo_StopDataPump(struct uvd *uvd);
55static int usbvideo_GetFrame(struct uvd *uvd, int frameNum);
56static int usbvideo_NewFrame(struct uvd *uvd, int framenum);
57static void usbvideo_SoftwareContrastAdjustment(struct uvd *uvd,
58 struct usbvideo_frame *frame);
59
60/*******************************/
61/* Memory management functions */
62/*******************************/
63static void *usbvideo_rvmalloc(unsigned long size)
64{
65 void *mem;
66 unsigned long adr;
67
68 size = PAGE_ALIGN(size);
69 mem = vmalloc_32(size);
70 if (!mem)
71 return NULL;
72
73 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
74 adr = (unsigned long) mem;
75 while (size > 0) {
76 SetPageReserved(vmalloc_to_page((void *)adr));
77 adr += PAGE_SIZE;
78 size -= PAGE_SIZE;
79 }
80
81 return mem;
82}
83
84static void usbvideo_rvfree(void *mem, unsigned long size)
85{
86 unsigned long adr;
87
88 if (!mem)
89 return;
90
91 adr = (unsigned long) mem;
92 while ((long) size > 0) {
93 ClearPageReserved(vmalloc_to_page((void *)adr));
94 adr += PAGE_SIZE;
95 size -= PAGE_SIZE;
96 }
97 vfree(mem);
98}
99
100static void RingQueue_Initialize(struct RingQueue *rq)
101{
102 assert(rq != NULL);
103 init_waitqueue_head(&rq->wqh);
104}
105
106static void RingQueue_Allocate(struct RingQueue *rq, int rqLen)
107{
108 /* Make sure the requested size is a power of 2 and
109 round up if necessary. This allows index wrapping
110 using masks rather than modulo */
111
112 int i = 1;
113 assert(rq != NULL);
114 assert(rqLen > 0);
115
116 while(rqLen >> i)
117 i++;
118 if(rqLen != 1 << (i-1))
119 rqLen = 1 << i;
120
121 rq->length = rqLen;
122 rq->ri = rq->wi = 0;
123 rq->queue = usbvideo_rvmalloc(rq->length);
124 assert(rq->queue != NULL);
125}
126
127static int RingQueue_IsAllocated(const struct RingQueue *rq)
128{
129 if (rq == NULL)
130 return 0;
131 return (rq->queue != NULL) && (rq->length > 0);
132}
133
134static void RingQueue_Free(struct RingQueue *rq)
135{
136 assert(rq != NULL);
137 if (RingQueue_IsAllocated(rq)) {
138 usbvideo_rvfree(rq->queue, rq->length);
139 rq->queue = NULL;
140 rq->length = 0;
141 }
142}
143
144int RingQueue_Dequeue(struct RingQueue *rq, unsigned char *dst, int len)
145{
146 int rql, toread;
147
148 assert(rq != NULL);
149 assert(dst != NULL);
150
151 rql = RingQueue_GetLength(rq);
152 if(!rql)
153 return 0;
154
155 /* Clip requested length to available data */
156 if(len > rql)
157 len = rql;
158
159 toread = len;
160 if(rq->ri > rq->wi) {
161 /* Read data from tail */
162 int read = (toread < (rq->length - rq->ri)) ? toread : rq->length - rq->ri;
163 memcpy(dst, rq->queue + rq->ri, read);
164 toread -= read;
165 dst += read;
166 rq->ri = (rq->ri + read) & (rq->length-1);
167 }
168 if(toread) {
169 /* Read data from head */
170 memcpy(dst, rq->queue + rq->ri, toread);
171 rq->ri = (rq->ri + toread) & (rq->length-1);
172 }
173 return len;
174}
175
176EXPORT_SYMBOL(RingQueue_Dequeue);
177
178int RingQueue_Enqueue(struct RingQueue *rq, const unsigned char *cdata, int n)
179{
180 int enqueued = 0;
181
182 assert(rq != NULL);
183 assert(cdata != NULL);
184 assert(rq->length > 0);
185 while (n > 0) {
186 int m, q_avail;
187
188 /* Calculate the largest chunk that fits the tail of the ring */
189 q_avail = rq->length - rq->wi;
190 if (q_avail <= 0) {
191 rq->wi = 0;
192 q_avail = rq->length;
193 }
194 m = n;
195 assert(q_avail > 0);
196 if (m > q_avail)
197 m = q_avail;
198
199 memcpy(rq->queue + rq->wi, cdata, m);
200 RING_QUEUE_ADVANCE_INDEX(rq, wi, m);
201 cdata += m;
202 enqueued += m;
203 n -= m;
204 }
205 return enqueued;
206}
207
208EXPORT_SYMBOL(RingQueue_Enqueue);
209
210static void RingQueue_InterruptibleSleepOn(struct RingQueue *rq)
211{
212 assert(rq != NULL);
213 interruptible_sleep_on(&rq->wqh);
214}
215
216void RingQueue_WakeUpInterruptible(struct RingQueue *rq)
217{
218 assert(rq != NULL);
219 if (waitqueue_active(&rq->wqh))
220 wake_up_interruptible(&rq->wqh);
221}
222
223EXPORT_SYMBOL(RingQueue_WakeUpInterruptible);
224
225void RingQueue_Flush(struct RingQueue *rq)
226{
227 assert(rq != NULL);
228 rq->ri = 0;
229 rq->wi = 0;
230}
231
232EXPORT_SYMBOL(RingQueue_Flush);
233
234
235/*
236 * usbvideo_VideosizeToString()
237 *
238 * This procedure converts given videosize value to readable string.
239 *
240 * History:
241 * 07-Aug-2000 Created.
242 * 19-Oct-2000 Reworked for usbvideo module.
243 */
244static void usbvideo_VideosizeToString(char *buf, int bufLen, videosize_t vs)
245{
246 char tmp[40];
247 int n;
248
249 n = 1 + sprintf(tmp, "%ldx%ld", VIDEOSIZE_X(vs), VIDEOSIZE_Y(vs));
250 assert(n < sizeof(tmp));
251 if ((buf == NULL) || (bufLen < n))
252 err("usbvideo_VideosizeToString: buffer is too small.");
253 else
254 memmove(buf, tmp, n);
255}
256
257/*
258 * usbvideo_OverlayChar()
259 *
260 * History:
261 * 01-Feb-2000 Created.
262 */
263static void usbvideo_OverlayChar(struct uvd *uvd, struct usbvideo_frame *frame,
264 int x, int y, int ch)
265{
266 static const unsigned short digits[16] = {
267 0xF6DE, /* 0 */
268 0x2492, /* 1 */
269 0xE7CE, /* 2 */
270 0xE79E, /* 3 */
271 0xB792, /* 4 */
272 0xF39E, /* 5 */
273 0xF3DE, /* 6 */
274 0xF492, /* 7 */
275 0xF7DE, /* 8 */
276 0xF79E, /* 9 */
277 0x77DA, /* a */
278 0xD75C, /* b */
279 0xF24E, /* c */
280 0xD6DC, /* d */
281 0xF34E, /* e */
282 0xF348 /* f */
283 };
284 unsigned short digit;
285 int ix, iy;
286
287 if ((uvd == NULL) || (frame == NULL))
288 return;
289
290 if (ch >= '0' && ch <= '9')
291 ch -= '0';
292 else if (ch >= 'A' && ch <= 'F')
293 ch = 10 + (ch - 'A');
294 else if (ch >= 'a' && ch <= 'f')
295 ch = 10 + (ch - 'a');
296 else
297 return;
298 digit = digits[ch];
299
300 for (iy=0; iy < 5; iy++) {
301 for (ix=0; ix < 3; ix++) {
302 if (digit & 0x8000) {
303 if (uvd->paletteBits & (1L << VIDEO_PALETTE_RGB24)) {
304/* TODO */ RGB24_PUTPIXEL(frame, x+ix, y+iy, 0xFF, 0xFF, 0xFF);
305 }
306 }
307 digit = digit << 1;
308 }
309 }
310}
311
312/*
313 * usbvideo_OverlayString()
314 *
315 * History:
316 * 01-Feb-2000 Created.
317 */
318static void usbvideo_OverlayString(struct uvd *uvd, struct usbvideo_frame *frame,
319 int x, int y, const char *str)
320{
321 while (*str) {
322 usbvideo_OverlayChar(uvd, frame, x, y, *str);
323 str++;
324 x += 4; /* 3 pixels character + 1 space */
325 }
326}
327
328/*
329 * usbvideo_OverlayStats()
330 *
331 * Overlays important debugging information.
332 *
333 * History:
334 * 01-Feb-2000 Created.
335 */
336static void usbvideo_OverlayStats(struct uvd *uvd, struct usbvideo_frame *frame)
337{
338 const int y_diff = 8;
339 char tmp[16];
340 int x = 10, y=10;
341 long i, j, barLength;
342 const int qi_x1 = 60, qi_y1 = 10;
343 const int qi_x2 = VIDEOSIZE_X(frame->request) - 10, qi_h = 10;
344
345 /* Call the user callback, see if we may proceed after that */
346 if (VALID_CALLBACK(uvd, overlayHook)) {
347 if (GET_CALLBACK(uvd, overlayHook)(uvd, frame) < 0)
348 return;
349 }
350
351 /*
352 * We draw a (mostly) hollow rectangle with qi_xxx coordinates.
353 * Left edge symbolizes the queue index 0; right edge symbolizes
354 * the full capacity of the queue.
355 */
356 barLength = qi_x2 - qi_x1 - 2;
357 if ((barLength > 10) && (uvd->paletteBits & (1L << VIDEO_PALETTE_RGB24))) {
358/* TODO */ long u_lo, u_hi, q_used;
359 long m_ri, m_wi, m_lo, m_hi;
360
361 /*
362 * Determine fill zones (used areas of the queue):
363 * 0 xxxxxxx u_lo ...... uvd->dp.ri xxxxxxxx u_hi ..... uvd->dp.length
364 *
365 * if u_lo < 0 then there is no first filler.
366 */
367
368 q_used = RingQueue_GetLength(&uvd->dp);
369 if ((uvd->dp.ri + q_used) >= uvd->dp.length) {
370 u_hi = uvd->dp.length;
371 u_lo = (q_used + uvd->dp.ri) & (uvd->dp.length-1);
372 } else {
373 u_hi = (q_used + uvd->dp.ri);
374 u_lo = -1;
375 }
376
377 /* Convert byte indices into screen units */
378 m_ri = qi_x1 + ((barLength * uvd->dp.ri) / uvd->dp.length);
379 m_wi = qi_x1 + ((barLength * uvd->dp.wi) / uvd->dp.length);
380 m_lo = (u_lo > 0) ? (qi_x1 + ((barLength * u_lo) / uvd->dp.length)) : -1;
381 m_hi = qi_x1 + ((barLength * u_hi) / uvd->dp.length);
382
383 for (j=qi_y1; j < (qi_y1 + qi_h); j++) {
384 for (i=qi_x1; i < qi_x2; i++) {
385 /* Draw border lines */
386 if ((j == qi_y1) || (j == (qi_y1 + qi_h - 1)) ||
387 (i == qi_x1) || (i == (qi_x2 - 1))) {
388 RGB24_PUTPIXEL(frame, i, j, 0xFF, 0xFF, 0xFF);
389 continue;
390 }
391 /* For all other points the Y coordinate does not matter */
392 if ((i >= m_ri) && (i <= (m_ri + 3))) {
393 RGB24_PUTPIXEL(frame, i, j, 0x00, 0xFF, 0x00);
394 } else if ((i >= m_wi) && (i <= (m_wi + 3))) {
395 RGB24_PUTPIXEL(frame, i, j, 0xFF, 0x00, 0x00);
396 } else if ((i < m_lo) || ((i > m_ri) && (i < m_hi)))
397 RGB24_PUTPIXEL(frame, i, j, 0x00, 0x00, 0xFF);
398 }
399 }
400 }
401
402 sprintf(tmp, "%8lx", uvd->stats.frame_num);
403 usbvideo_OverlayString(uvd, frame, x, y, tmp);
404 y += y_diff;
405
406 sprintf(tmp, "%8lx", uvd->stats.urb_count);
407 usbvideo_OverlayString(uvd, frame, x, y, tmp);
408 y += y_diff;
409
410 sprintf(tmp, "%8lx", uvd->stats.urb_length);
411 usbvideo_OverlayString(uvd, frame, x, y, tmp);
412 y += y_diff;
413
414 sprintf(tmp, "%8lx", uvd->stats.data_count);
415 usbvideo_OverlayString(uvd, frame, x, y, tmp);
416 y += y_diff;
417
418 sprintf(tmp, "%8lx", uvd->stats.header_count);
419 usbvideo_OverlayString(uvd, frame, x, y, tmp);
420 y += y_diff;
421
422 sprintf(tmp, "%8lx", uvd->stats.iso_skip_count);
423 usbvideo_OverlayString(uvd, frame, x, y, tmp);
424 y += y_diff;
425
426 sprintf(tmp, "%8lx", uvd->stats.iso_err_count);
427 usbvideo_OverlayString(uvd, frame, x, y, tmp);
428 y += y_diff;
429
430 sprintf(tmp, "%8x", uvd->vpic.colour);
431 usbvideo_OverlayString(uvd, frame, x, y, tmp);
432 y += y_diff;
433
434 sprintf(tmp, "%8x", uvd->vpic.hue);
435 usbvideo_OverlayString(uvd, frame, x, y, tmp);
436 y += y_diff;
437
438 sprintf(tmp, "%8x", uvd->vpic.brightness >> 8);
439 usbvideo_OverlayString(uvd, frame, x, y, tmp);
440 y += y_diff;
441
442 sprintf(tmp, "%8x", uvd->vpic.contrast >> 12);
443 usbvideo_OverlayString(uvd, frame, x, y, tmp);
444 y += y_diff;
445
446 sprintf(tmp, "%8d", uvd->vpic.whiteness >> 8);
447 usbvideo_OverlayString(uvd, frame, x, y, tmp);
448 y += y_diff;
449}
450
451/*
452 * usbvideo_ReportStatistics()
453 *
454 * This procedure prints packet and transfer statistics.
455 *
456 * History:
457 * 14-Jan-2000 Corrected default multiplier.
458 */
459static void usbvideo_ReportStatistics(const struct uvd *uvd)
460{
461 if ((uvd != NULL) && (uvd->stats.urb_count > 0)) {
462 unsigned long allPackets, badPackets, goodPackets, percent;
463 allPackets = uvd->stats.urb_count * CAMERA_URB_FRAMES;
464 badPackets = uvd->stats.iso_skip_count + uvd->stats.iso_err_count;
465 goodPackets = allPackets - badPackets;
466 /* Calculate percentage wisely, remember integer limits */
467 assert(allPackets != 0);
468 if (goodPackets < (((unsigned long)-1)/100))
469 percent = (100 * goodPackets) / allPackets;
470 else
471 percent = goodPackets / (allPackets / 100);
472 info("Packet Statistics: Total=%lu. Empty=%lu. Usage=%lu%%",
473 allPackets, badPackets, percent);
474 if (uvd->iso_packet_len > 0) {
475 unsigned long allBytes, xferBytes;
476 char multiplier = ' ';
477 allBytes = allPackets * uvd->iso_packet_len;
478 xferBytes = uvd->stats.data_count;
479 assert(allBytes != 0);
480 if (xferBytes < (((unsigned long)-1)/100))
481 percent = (100 * xferBytes) / allBytes;
482 else
483 percent = xferBytes / (allBytes / 100);
484 /* Scale xferBytes for easy reading */
485 if (xferBytes > 10*1024) {
486 xferBytes /= 1024;
487 multiplier = 'K';
488 if (xferBytes > 10*1024) {
489 xferBytes /= 1024;
490 multiplier = 'M';
491 if (xferBytes > 10*1024) {
492 xferBytes /= 1024;
493 multiplier = 'G';
494 if (xferBytes > 10*1024) {
495 xferBytes /= 1024;
496 multiplier = 'T';
497 }
498 }
499 }
500 }
501 info("Transfer Statistics: Transferred=%lu%cB Usage=%lu%%",
502 xferBytes, multiplier, percent);
503 }
504 }
505}
506
507/*
508 * usbvideo_TestPattern()
509 *
510 * Procedure forms a test pattern (yellow grid on blue background).
511 *
512 * Parameters:
513 * fullframe: if TRUE then entire frame is filled, otherwise the procedure
514 * continues from the current scanline.
515 * pmode 0: fill the frame with solid blue color (like on VCR or TV)
516 * 1: Draw a colored grid
517 *
518 * History:
519 * 01-Feb-2000 Created.
520 */
521void usbvideo_TestPattern(struct uvd *uvd, int fullframe, int pmode)
522{
523 struct usbvideo_frame *frame;
524 int num_cell = 0;
525 int scan_length = 0;
526 static int num_pass = 0;
527
528 if (uvd == NULL) {
529 err("%s: uvd == NULL", __FUNCTION__);
530 return;
531 }
532 if ((uvd->curframe < 0) || (uvd->curframe >= USBVIDEO_NUMFRAMES)) {
533 err("%s: uvd->curframe=%d.", __FUNCTION__, uvd->curframe);
534 return;
535 }
536
537 /* Grab the current frame */
538 frame = &uvd->frame[uvd->curframe];
539
540 /* Optionally start at the beginning */
541 if (fullframe) {
542 frame->curline = 0;
543 frame->seqRead_Length = 0;
544 }
545#if 0
546 { /* For debugging purposes only */
547 char tmp[20];
548 usbvideo_VideosizeToString(tmp, sizeof(tmp), frame->request);
549 info("testpattern: frame=%s", tmp);
550 }
551#endif
552 /* Form every scan line */
553 for (; frame->curline < VIDEOSIZE_Y(frame->request); frame->curline++) {
554 int i;
555 unsigned char *f = frame->data +
556 (VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL * frame->curline);
557 for (i=0; i < VIDEOSIZE_X(frame->request); i++) {
558 unsigned char cb=0x80;
559 unsigned char cg = 0;
560 unsigned char cr = 0;
561
562 if (pmode == 1) {
563 if (frame->curline % 32 == 0)
564 cb = 0, cg = cr = 0xFF;
565 else if (i % 32 == 0) {
566 if (frame->curline % 32 == 1)
567 num_cell++;
568 cb = 0, cg = cr = 0xFF;
569 } else {
570 cb = ((num_cell*7) + num_pass) & 0xFF;
571 cg = ((num_cell*5) + num_pass*2) & 0xFF;
572 cr = ((num_cell*3) + num_pass*3) & 0xFF;
573 }
574 } else {
575 /* Just the blue screen */
576 }
577
578 *f++ = cb;
579 *f++ = cg;
580 *f++ = cr;
581 scan_length += 3;
582 }
583 }
584
585 frame->frameState = FrameState_Done;
586 frame->seqRead_Length += scan_length;
587 ++num_pass;
588
589 /* We do this unconditionally, regardless of FLAGS_OVERLAY_STATS */
590 usbvideo_OverlayStats(uvd, frame);
591}
592
593EXPORT_SYMBOL(usbvideo_TestPattern);
594
595
596#ifdef DEBUG
597/*
598 * usbvideo_HexDump()
599 *
600 * A debugging tool. Prints hex dumps.
601 *
602 * History:
603 * 29-Jul-2000 Added printing of offsets.
604 */
605void usbvideo_HexDump(const unsigned char *data, int len)
606{
607 const int bytes_per_line = 32;
608 char tmp[128]; /* 32*3 + 5 */
609 int i, k;
610
611 for (i=k=0; len > 0; i++, len--) {
612 if (i > 0 && ((i % bytes_per_line) == 0)) {
613 printk("%s\n", tmp);
614 k=0;
615 }
616 if ((i % bytes_per_line) == 0)
617 k += sprintf(&tmp[k], "%04x: ", i);
618 k += sprintf(&tmp[k], "%02x ", data[i]);
619 }
620 if (k > 0)
621 printk("%s\n", tmp);
622}
623
624EXPORT_SYMBOL(usbvideo_HexDump);
625
626#endif
627
628/* ******************************************************************** */
629
630/* XXX: this piece of crap really wants some error handling.. */
631static void usbvideo_ClientIncModCount(struct uvd *uvd)
632{
633 if (uvd == NULL) {
634 err("%s: uvd == NULL", __FUNCTION__);
635 return;
636 }
637 if (uvd->handle == NULL) {
638 err("%s: uvd->handle == NULL", __FUNCTION__);
639 return;
640 }
641 if (uvd->handle->md_module == NULL) {
642 err("%s: uvd->handle->md_module == NULL", __FUNCTION__);
643 return;
644 }
645 if (!try_module_get(uvd->handle->md_module)) {
646 err("%s: try_module_get() == 0", __FUNCTION__);
647 return;
648 }
649}
650
651static void usbvideo_ClientDecModCount(struct uvd *uvd)
652{
653 if (uvd == NULL) {
654 err("%s: uvd == NULL", __FUNCTION__);
655 return;
656 }
657 if (uvd->handle == NULL) {
658 err("%s: uvd->handle == NULL", __FUNCTION__);
659 return;
660 }
661 if (uvd->handle->md_module == NULL) {
662 err("%s: uvd->handle->md_module == NULL", __FUNCTION__);
663 return;
664 }
665 module_put(uvd->handle->md_module);
666}
667
668int usbvideo_register(
669 struct usbvideo **pCams,
670 const int num_cams,
671 const int num_extra,
672 const char *driverName,
673 const struct usbvideo_cb *cbTbl,
674 struct module *md,
675 const struct usb_device_id *id_table)
676{
677 struct usbvideo *cams;
678 int i, base_size, result;
679
680 /* Check parameters for sanity */
681 if ((num_cams <= 0) || (pCams == NULL) || (cbTbl == NULL)) {
682 err("%s: Illegal call", __FUNCTION__);
683 return -EINVAL;
684 }
685
686 /* Check registration callback - must be set! */
687 if (cbTbl->probe == NULL) {
688 err("%s: probe() is required!", __FUNCTION__);
689 return -EINVAL;
690 }
691
692 base_size = num_cams * sizeof(struct uvd) + sizeof(struct usbvideo);
693 cams = (struct usbvideo *) kmalloc(base_size, GFP_KERNEL);
694 if (cams == NULL) {
695 err("Failed to allocate %d. bytes for usbvideo struct", base_size);
696 return -ENOMEM;
697 }
698 dbg("%s: Allocated $%p (%d. bytes) for %d. cameras",
699 __FUNCTION__, cams, base_size, num_cams);
700 memset(cams, 0, base_size);
701
702 /* Copy callbacks, apply defaults for those that are not set */
703 memmove(&cams->cb, cbTbl, sizeof(cams->cb));
704 if (cams->cb.getFrame == NULL)
705 cams->cb.getFrame = usbvideo_GetFrame;
706 if (cams->cb.disconnect == NULL)
707 cams->cb.disconnect = usbvideo_Disconnect;
708 if (cams->cb.startDataPump == NULL)
709 cams->cb.startDataPump = usbvideo_StartDataPump;
710 if (cams->cb.stopDataPump == NULL)
711 cams->cb.stopDataPump = usbvideo_StopDataPump;
712
713 cams->num_cameras = num_cams;
714 cams->cam = (struct uvd *) &cams[1];
715 cams->md_module = md;
716 if (cams->md_module == NULL)
717 warn("%s: module == NULL!", __FUNCTION__);
718 init_MUTEX(&cams->lock); /* to 1 == available */
719
720 for (i = 0; i < num_cams; i++) {
721 struct uvd *up = &cams->cam[i];
722
723 up->handle = cams;
724
725 /* Allocate user_data separately because of kmalloc's limits */
726 if (num_extra > 0) {
727 up->user_size = num_cams * num_extra;
728 up->user_data = kmalloc(up->user_size, GFP_KERNEL);
729 if (up->user_data == NULL) {
730 err("%s: Failed to allocate user_data (%d. bytes)",
731 __FUNCTION__, up->user_size);
732 while (i) {
733 up = &cams->cam[--i];
734 kfree(up->user_data);
735 }
736 kfree(cams);
737 return -ENOMEM;
738 }
739 dbg("%s: Allocated cams[%d].user_data=$%p (%d. bytes)",
740 __FUNCTION__, i, up->user_data, up->user_size);
741 }
742 }
743
744 /*
745 * Register ourselves with USB stack.
746 */
747 strcpy(cams->drvName, (driverName != NULL) ? driverName : "Unknown");
748 cams->usbdrv.name = cams->drvName;
749 cams->usbdrv.probe = cams->cb.probe;
750 cams->usbdrv.disconnect = cams->cb.disconnect;
751 cams->usbdrv.id_table = id_table;
752
753 /*
754 * Update global handle to usbvideo. This is very important
755 * because probe() can be called before usb_register() returns.
756 * If the handle is not yet updated then the probe() will fail.
757 */
758 *pCams = cams;
759 result = usb_register(&cams->usbdrv);
760 if (result) {
761 for (i = 0; i < num_cams; i++) {
762 struct uvd *up = &cams->cam[i];
763 kfree(up->user_data);
764 }
765 kfree(cams);
766 }
767
768 return result;
769}
770
771EXPORT_SYMBOL(usbvideo_register);
772
773/*
774 * usbvideo_Deregister()
775 *
776 * Procedure frees all usbvideo and user data structures. Be warned that
777 * if you had some dynamically allocated components in ->user field then
778 * you should free them before calling here.
779 */
780void usbvideo_Deregister(struct usbvideo **pCams)
781{
782 struct usbvideo *cams;
783 int i;
784
785 if (pCams == NULL) {
786 err("%s: pCams == NULL", __FUNCTION__);
787 return;
788 }
789 cams = *pCams;
790 if (cams == NULL) {
791 err("%s: cams == NULL", __FUNCTION__);
792 return;
793 }
794
795 dbg("%s: Deregistering %s driver.", __FUNCTION__, cams->drvName);
796 usb_deregister(&cams->usbdrv);
797
798 dbg("%s: Deallocating cams=$%p (%d. cameras)", __FUNCTION__, cams, cams->num_cameras);
799 for (i=0; i < cams->num_cameras; i++) {
800 struct uvd *up = &cams->cam[i];
801 int warning = 0;
802
803 if (up->user_data != NULL) {
804 if (up->user_size <= 0)
805 ++warning;
806 } else {
807 if (up->user_size > 0)
808 ++warning;
809 }
810 if (warning) {
811 err("%s: Warning: user_data=$%p user_size=%d.",
812 __FUNCTION__, up->user_data, up->user_size);
813 } else {
814 dbg("%s: Freeing %d. $%p->user_data=$%p",
815 __FUNCTION__, i, up, up->user_data);
816 kfree(up->user_data);
817 }
818 }
819 /* Whole array was allocated in one chunk */
820 dbg("%s: Freed %d uvd structures",
821 __FUNCTION__, cams->num_cameras);
822 kfree(cams);
823 *pCams = NULL;
824}
825
826EXPORT_SYMBOL(usbvideo_Deregister);
827
828/*
829 * usbvideo_Disconnect()
830 *
831 * This procedure stops all driver activity. Deallocation of
832 * the interface-private structure (pointed by 'ptr') is done now
833 * (if we don't have any open files) or later, when those files
834 * are closed. After that driver should be removable.
835 *
836 * This code handles surprise removal. The uvd->user is a counter which
837 * increments on open() and decrements on close(). If we see here that
838 * this counter is not 0 then we have a client who still has us opened.
839 * We set uvd->remove_pending flag as early as possible, and after that
840 * all access to the camera will gracefully fail. These failures should
841 * prompt client to (eventually) close the video device, and then - in
842 * usbvideo_v4l_close() - we decrement uvd->uvd_used and usage counter.
843 *
844 * History:
845 * 22-Jan-2000 Added polling of MOD_IN_USE to delay removal until all users gone.
846 * 27-Jan-2000 Reworked to allow pending disconnects; see xxx_close()
847 * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
848 * 19-Oct-2000 Moved to usbvideo module.
849 */
850static void usbvideo_Disconnect(struct usb_interface *intf)
851{
852 struct uvd *uvd = usb_get_intfdata (intf);
853 int i;
854
855 if (uvd == NULL) {
856 err("%s($%p): Illegal call.", __FUNCTION__, intf);
857 return;
858 }
859
860 usb_set_intfdata (intf, NULL);
861
862 usbvideo_ClientIncModCount(uvd);
863 if (uvd->debug > 0)
864 info("%s(%p.)", __FUNCTION__, intf);
865
866 down(&uvd->lock);
867 uvd->remove_pending = 1; /* Now all ISO data will be ignored */
868
869 /* At this time we ask to cancel outstanding URBs */
870 GET_CALLBACK(uvd, stopDataPump)(uvd);
871
872 for (i=0; i < USBVIDEO_NUMSBUF; i++)
873 usb_free_urb(uvd->sbuf[i].urb);
874
875 usb_put_dev(uvd->dev);
876 uvd->dev = NULL; /* USB device is no more */
877
878 video_unregister_device(&uvd->vdev);
879 if (uvd->debug > 0)
880 info("%s: Video unregistered.", __FUNCTION__);
881
882 if (uvd->user)
883 info("%s: In use, disconnect pending.", __FUNCTION__);
884 else
885 usbvideo_CameraRelease(uvd);
886 up(&uvd->lock);
887 info("USB camera disconnected.");
888
889 usbvideo_ClientDecModCount(uvd);
890}
891
892/*
893 * usbvideo_CameraRelease()
894 *
895 * This code does final release of uvd. This happens
896 * after the device is disconnected -and- all clients
897 * closed their files.
898 *
899 * History:
900 * 27-Jan-2000 Created.
901 */
902static void usbvideo_CameraRelease(struct uvd *uvd)
903{
904 if (uvd == NULL) {
905 err("%s: Illegal call", __FUNCTION__);
906 return;
907 }
908
909 RingQueue_Free(&uvd->dp);
910 if (VALID_CALLBACK(uvd, userFree))
911 GET_CALLBACK(uvd, userFree)(uvd);
912 uvd->uvd_used = 0; /* This is atomic, no need to take mutex */
913}
914
915/*
916 * usbvideo_find_struct()
917 *
918 * This code searches the array of preallocated (static) structures
919 * and returns index of the first one that isn't in use. Returns -1
920 * if there are no free structures.
921 *
922 * History:
923 * 27-Jan-2000 Created.
924 */
925static int usbvideo_find_struct(struct usbvideo *cams)
926{
927 int u, rv = -1;
928
929 if (cams == NULL) {
930 err("No usbvideo handle?");
931 return -1;
932 }
933 down(&cams->lock);
934 for (u = 0; u < cams->num_cameras; u++) {
935 struct uvd *uvd = &cams->cam[u];
936 if (!uvd->uvd_used) /* This one is free */
937 {
938 uvd->uvd_used = 1; /* In use now */
939 init_MUTEX(&uvd->lock); /* to 1 == available */
940 uvd->dev = NULL;
941 rv = u;
942 break;
943 }
944 }
945 up(&cams->lock);
946 return rv;
947}
948
949static struct file_operations usbvideo_fops = {
950 .owner = THIS_MODULE,
951 .open = usbvideo_v4l_open,
952 .release =usbvideo_v4l_close,
953 .read = usbvideo_v4l_read,
954 .mmap = usbvideo_v4l_mmap,
955 .ioctl = usbvideo_v4l_ioctl,
956 .compat_ioctl = v4l_compat_ioctl32,
957 .llseek = no_llseek,
958};
959static const struct video_device usbvideo_template = {
960 .owner = THIS_MODULE,
961 .type = VID_TYPE_CAPTURE,
962 .hardware = VID_HARDWARE_CPIA,
963 .fops = &usbvideo_fops,
964};
965
966struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams)
967{
968 int i, devnum;
969 struct uvd *uvd = NULL;
970
971 if (cams == NULL) {
972 err("No usbvideo handle?");
973 return NULL;
974 }
975
976 devnum = usbvideo_find_struct(cams);
977 if (devnum == -1) {
978 err("IBM USB camera driver: Too many devices!");
979 return NULL;
980 }
981 uvd = &cams->cam[devnum];
982 dbg("Device entry #%d. at $%p", devnum, uvd);
983
984 /* Not relying upon caller we increase module counter ourselves */
985 usbvideo_ClientIncModCount(uvd);
986
987 down(&uvd->lock);
988 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
989 uvd->sbuf[i].urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
990 if (uvd->sbuf[i].urb == NULL) {
991 err("usb_alloc_urb(%d.) failed.", FRAMES_PER_DESC);
992 uvd->uvd_used = 0;
993 uvd = NULL;
994 goto allocate_done;
995 }
996 }
997 uvd->user=0;
998 uvd->remove_pending = 0;
999 uvd->last_error = 0;
1000 RingQueue_Initialize(&uvd->dp);
1001
1002 /* Initialize video device structure */
1003 uvd->vdev = usbvideo_template;
1004 sprintf(uvd->vdev.name, "%.20s USB Camera", cams->drvName);
1005 /*
1006 * The client is free to overwrite those because we
1007 * return control to the client's probe function right now.
1008 */
1009allocate_done:
1010 up (&uvd->lock);
1011 usbvideo_ClientDecModCount(uvd);
1012 return uvd;
1013}
1014
1015EXPORT_SYMBOL(usbvideo_AllocateDevice);
1016
1017int usbvideo_RegisterVideoDevice(struct uvd *uvd)
1018{
1019 char tmp1[20], tmp2[20]; /* Buffers for printing */
1020
1021 if (uvd == NULL) {
1022 err("%s: Illegal call.", __FUNCTION__);
1023 return -EINVAL;
1024 }
1025 if (uvd->video_endp == 0) {
1026 info("%s: No video endpoint specified; data pump disabled.", __FUNCTION__);
1027 }
1028 if (uvd->paletteBits == 0) {
1029 err("%s: No palettes specified!", __FUNCTION__);
1030 return -EINVAL;
1031 }
1032 if (uvd->defaultPalette == 0) {
1033 info("%s: No default palette!", __FUNCTION__);
1034 }
1035
1036 uvd->max_frame_size = VIDEOSIZE_X(uvd->canvas) *
1037 VIDEOSIZE_Y(uvd->canvas) * V4L_BYTES_PER_PIXEL;
1038 usbvideo_VideosizeToString(tmp1, sizeof(tmp1), uvd->videosize);
1039 usbvideo_VideosizeToString(tmp2, sizeof(tmp2), uvd->canvas);
1040
1041 if (uvd->debug > 0) {
1042 info("%s: iface=%d. endpoint=$%02x paletteBits=$%08lx",
1043 __FUNCTION__, uvd->iface, uvd->video_endp, uvd->paletteBits);
1044 }
1045 if (video_register_device(&uvd->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
1046 err("%s: video_register_device failed", __FUNCTION__);
1047 return -EPIPE;
1048 }
1049 if (uvd->debug > 1) {
1050 info("%s: video_register_device() successful", __FUNCTION__);
1051 }
1052 if (uvd->dev == NULL) {
1053 err("%s: uvd->dev == NULL", __FUNCTION__);
1054 return -EINVAL;
1055 }
1056
1057 info("%s on /dev/video%d: canvas=%s videosize=%s",
1058 (uvd->handle != NULL) ? uvd->handle->drvName : "???",
1059 uvd->vdev.minor, tmp2, tmp1);
1060
1061 usb_get_dev(uvd->dev);
1062 return 0;
1063}
1064
1065EXPORT_SYMBOL(usbvideo_RegisterVideoDevice);
1066
1067/* ******************************************************************** */
1068
1069static int usbvideo_v4l_mmap(struct file *file, struct vm_area_struct *vma)
1070{
1071 struct uvd *uvd = file->private_data;
1072 unsigned long start = vma->vm_start;
1073 unsigned long size = vma->vm_end-vma->vm_start;
1074 unsigned long page, pos;
1075
1076 if (!CAMERA_IS_OPERATIONAL(uvd))
1077 return -EFAULT;
1078
1079 if (size > (((USBVIDEO_NUMFRAMES * uvd->max_frame_size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))
1080 return -EINVAL;
1081
1082 pos = (unsigned long) uvd->fbuf;
1083 while (size > 0) {
1084 page = vmalloc_to_pfn((void *)pos);
1085 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1086 return -EAGAIN;
1087
1088 start += PAGE_SIZE;
1089 pos += PAGE_SIZE;
1090 if (size > PAGE_SIZE)
1091 size -= PAGE_SIZE;
1092 else
1093 size = 0;
1094 }
1095
1096 return 0;
1097}
1098
1099/*
1100 * usbvideo_v4l_open()
1101 *
1102 * This is part of Video 4 Linux API. The driver can be opened by one
1103 * client only (checks internal counter 'uvdser'). The procedure
1104 * then allocates buffers needed for video processing.
1105 *
1106 * History:
1107 * 22-Jan-2000 Rewrote, moved scratch buffer allocation here. Now the
1108 * camera is also initialized here (once per connect), at
1109 * expense of V4L client (it waits on open() call).
1110 * 27-Jan-2000 Used USBVIDEO_NUMSBUF as number of URB buffers.
1111 * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
1112 */
1113static int usbvideo_v4l_open(struct inode *inode, struct file *file)
1114{
1115 struct video_device *dev = video_devdata(file);
1116 struct uvd *uvd = (struct uvd *) dev;
1117 const int sb_size = FRAMES_PER_DESC * uvd->iso_packet_len;
1118 int i, errCode = 0;
1119
1120 if (uvd->debug > 1)
1121 info("%s($%p)", __FUNCTION__, dev);
1122
1123 usbvideo_ClientIncModCount(uvd);
1124 down(&uvd->lock);
1125
1126 if (uvd->user) {
1127 err("%s: Someone tried to open an already opened device!", __FUNCTION__);
1128 errCode = -EBUSY;
1129 } else {
1130 /* Clear statistics */
1131 memset(&uvd->stats, 0, sizeof(uvd->stats));
1132
1133 /* Clean pointers so we know if we allocated something */
1134 for (i=0; i < USBVIDEO_NUMSBUF; i++)
1135 uvd->sbuf[i].data = NULL;
1136
1137 /* Allocate memory for the frame buffers */
1138 uvd->fbuf_size = USBVIDEO_NUMFRAMES * uvd->max_frame_size;
1139 uvd->fbuf = usbvideo_rvmalloc(uvd->fbuf_size);
1140 RingQueue_Allocate(&uvd->dp, RING_QUEUE_SIZE);
1141 if ((uvd->fbuf == NULL) ||
1142 (!RingQueue_IsAllocated(&uvd->dp))) {
1143 err("%s: Failed to allocate fbuf or dp", __FUNCTION__);
1144 errCode = -ENOMEM;
1145 } else {
1146 /* Allocate all buffers */
1147 for (i=0; i < USBVIDEO_NUMFRAMES; i++) {
1148 uvd->frame[i].frameState = FrameState_Unused;
1149 uvd->frame[i].data = uvd->fbuf + i*(uvd->max_frame_size);
1150 /*
1151 * Set default sizes in case IOCTL (VIDIOCMCAPTURE)
1152 * is not used (using read() instead).
1153 */
1154 uvd->frame[i].canvas = uvd->canvas;
1155 uvd->frame[i].seqRead_Index = 0;
1156 }
1157 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1158 uvd->sbuf[i].data = kmalloc(sb_size, GFP_KERNEL);
1159 if (uvd->sbuf[i].data == NULL) {
1160 errCode = -ENOMEM;
1161 break;
1162 }
1163 }
1164 }
1165 if (errCode != 0) {
1166 /* Have to free all that memory */
1167 if (uvd->fbuf != NULL) {
1168 usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size);
1169 uvd->fbuf = NULL;
1170 }
1171 RingQueue_Free(&uvd->dp);
1172 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1173 kfree(uvd->sbuf[i].data);
1174 uvd->sbuf[i].data = NULL;
1175 }
1176 }
1177 }
1178
1179 /* If so far no errors then we shall start the camera */
1180 if (errCode == 0) {
1181 /* Start data pump if we have valid endpoint */
1182 if (uvd->video_endp != 0)
1183 errCode = GET_CALLBACK(uvd, startDataPump)(uvd);
1184 if (errCode == 0) {
1185 if (VALID_CALLBACK(uvd, setupOnOpen)) {
1186 if (uvd->debug > 1)
1187 info("%s: setupOnOpen callback", __FUNCTION__);
1188 errCode = GET_CALLBACK(uvd, setupOnOpen)(uvd);
1189 if (errCode < 0) {
1190 err("%s: setupOnOpen callback failed (%d.).",
1191 __FUNCTION__, errCode);
1192 } else if (uvd->debug > 1) {
1193 info("%s: setupOnOpen callback successful", __FUNCTION__);
1194 }
1195 }
1196 if (errCode == 0) {
1197 uvd->settingsAdjusted = 0;
1198 if (uvd->debug > 1)
1199 info("%s: Open succeeded.", __FUNCTION__);
1200 uvd->user++;
1201 file->private_data = uvd;
1202 }
1203 }
1204 }
1205 up(&uvd->lock);
1206 if (errCode != 0)
1207 usbvideo_ClientDecModCount(uvd);
1208 if (uvd->debug > 0)
1209 info("%s: Returning %d.", __FUNCTION__, errCode);
1210 return errCode;
1211}
1212
1213/*
1214 * usbvideo_v4l_close()
1215 *
1216 * This is part of Video 4 Linux API. The procedure
1217 * stops streaming and deallocates all buffers that were earlier
1218 * allocated in usbvideo_v4l_open().
1219 *
1220 * History:
1221 * 22-Jan-2000 Moved scratch buffer deallocation here.
1222 * 27-Jan-2000 Used USBVIDEO_NUMSBUF as number of URB buffers.
1223 * 24-May-2000 Moved MOD_DEC_USE_COUNT outside of code that can sleep.
1224 */
1225static int usbvideo_v4l_close(struct inode *inode, struct file *file)
1226{
1227 struct video_device *dev = file->private_data;
1228 struct uvd *uvd = (struct uvd *) dev;
1229 int i;
1230
1231 if (uvd->debug > 1)
1232 info("%s($%p)", __FUNCTION__, dev);
1233
1234 down(&uvd->lock);
1235 GET_CALLBACK(uvd, stopDataPump)(uvd);
1236 usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size);
1237 uvd->fbuf = NULL;
1238 RingQueue_Free(&uvd->dp);
1239
1240 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1241 kfree(uvd->sbuf[i].data);
1242 uvd->sbuf[i].data = NULL;
1243 }
1244
1245#if USBVIDEO_REPORT_STATS
1246 usbvideo_ReportStatistics(uvd);
1247#endif
1248
1249 uvd->user--;
1250 if (uvd->remove_pending) {
1251 if (uvd->debug > 0)
1252 info("usbvideo_v4l_close: Final disconnect.");
1253 usbvideo_CameraRelease(uvd);
1254 }
1255 up(&uvd->lock);
1256 usbvideo_ClientDecModCount(uvd);
1257
1258 if (uvd->debug > 1)
1259 info("%s: Completed.", __FUNCTION__);
1260 file->private_data = NULL;
1261 return 0;
1262}
1263
1264/*
1265 * usbvideo_v4l_ioctl()
1266 *
1267 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
1268 *
1269 * History:
1270 * 22-Jan-2000 Corrected VIDIOCSPICT to reject unsupported settings.
1271 */
1272static int usbvideo_v4l_do_ioctl(struct inode *inode, struct file *file,
1273 unsigned int cmd, void *arg)
1274{
1275 struct uvd *uvd = file->private_data;
1276
1277 if (!CAMERA_IS_OPERATIONAL(uvd))
1278 return -EIO;
1279
1280 switch (cmd) {
1281 case VIDIOCGCAP:
1282 {
1283 struct video_capability *b = arg;
1284 *b = uvd->vcap;
1285 return 0;
1286 }
1287 case VIDIOCGCHAN:
1288 {
1289 struct video_channel *v = arg;
1290 *v = uvd->vchan;
1291 return 0;
1292 }
1293 case VIDIOCSCHAN:
1294 {
1295 struct video_channel *v = arg;
1296 if (v->channel != 0)
1297 return -EINVAL;
1298 return 0;
1299 }
1300 case VIDIOCGPICT:
1301 {
1302 struct video_picture *pic = arg;
1303 *pic = uvd->vpic;
1304 return 0;
1305 }
1306 case VIDIOCSPICT:
1307 {
1308 struct video_picture *pic = arg;
1309 /*
1310 * Use temporary 'video_picture' structure to preserve our
1311 * own settings (such as color depth, palette) that we
1312 * aren't allowing everyone (V4L client) to change.
1313 */
1314 uvd->vpic.brightness = pic->brightness;
1315 uvd->vpic.hue = pic->hue;
1316 uvd->vpic.colour = pic->colour;
1317 uvd->vpic.contrast = pic->contrast;
1318 uvd->settingsAdjusted = 0; /* Will force new settings */
1319 return 0;
1320 }
1321 case VIDIOCSWIN:
1322 {
1323 struct video_window *vw = arg;
1324
1325 if(VALID_CALLBACK(uvd, setVideoMode)) {
1326 return GET_CALLBACK(uvd, setVideoMode)(uvd, vw);
1327 }
1328
1329 if (vw->flags)
1330 return -EINVAL;
1331 if (vw->clipcount)
1332 return -EINVAL;
1333 if (vw->width != VIDEOSIZE_X(uvd->canvas))
1334 return -EINVAL;
1335 if (vw->height != VIDEOSIZE_Y(uvd->canvas))
1336 return -EINVAL;
1337
1338 return 0;
1339 }
1340 case VIDIOCGWIN:
1341 {
1342 struct video_window *vw = arg;
1343
1344 vw->x = 0;
1345 vw->y = 0;
1346 vw->width = VIDEOSIZE_X(uvd->videosize);
1347 vw->height = VIDEOSIZE_Y(uvd->videosize);
1348 vw->chromakey = 0;
1349 if (VALID_CALLBACK(uvd, getFPS))
1350 vw->flags = GET_CALLBACK(uvd, getFPS)(uvd);
1351 else
1352 vw->flags = 10; /* FIXME: do better! */
1353 return 0;
1354 }
1355 case VIDIOCGMBUF:
1356 {
1357 struct video_mbuf *vm = arg;
1358 int i;
1359
1360 memset(vm, 0, sizeof(*vm));
1361 vm->size = uvd->max_frame_size * USBVIDEO_NUMFRAMES;
1362 vm->frames = USBVIDEO_NUMFRAMES;
1363 for(i = 0; i < USBVIDEO_NUMFRAMES; i++)
1364 vm->offsets[i] = i * uvd->max_frame_size;
1365
1366 return 0;
1367 }
1368 case VIDIOCMCAPTURE:
1369 {
1370 struct video_mmap *vm = arg;
1371
1372 if (uvd->debug >= 1) {
1373 info("VIDIOCMCAPTURE: frame=%d. size=%dx%d, format=%d.",
1374 vm->frame, vm->width, vm->height, vm->format);
1375 }
1376 /*
1377 * Check if the requested size is supported. If the requestor
1378 * requests too big a frame then we may be tricked into accessing
1379 * outside of own preallocated frame buffer (in uvd->frame).
1380 * This will cause oops or a security hole. Theoretically, we
1381 * could only clamp the size down to acceptable bounds, but then
1382 * we'd need to figure out how to insert our smaller buffer into
1383 * larger caller's buffer... this is not an easy question. So we
1384 * here just flatly reject too large requests, assuming that the
1385 * caller will resubmit with smaller size. Callers should know
1386 * what size we support (returned by VIDIOCGCAP). However vidcat,
1387 * for one, does not care and allows to ask for any size.
1388 */
1389 if ((vm->width > VIDEOSIZE_X(uvd->canvas)) ||
1390 (vm->height > VIDEOSIZE_Y(uvd->canvas))) {
1391 if (uvd->debug > 0) {
1392 info("VIDIOCMCAPTURE: Size=%dx%d too large; "
1393 "allowed only up to %ldx%ld", vm->width, vm->height,
1394 VIDEOSIZE_X(uvd->canvas), VIDEOSIZE_Y(uvd->canvas));
1395 }
1396 return -EINVAL;
1397 }
1398 /* Check if the palette is supported */
1399 if (((1L << vm->format) & uvd->paletteBits) == 0) {
1400 if (uvd->debug > 0) {
1401 info("VIDIOCMCAPTURE: format=%d. not supported"
1402 " (paletteBits=$%08lx)",
1403 vm->format, uvd->paletteBits);
1404 }
1405 return -EINVAL;
1406 }
1407 if ((vm->frame < 0) || (vm->frame >= USBVIDEO_NUMFRAMES)) {
1408 err("VIDIOCMCAPTURE: vm.frame=%d. !E [0-%d]", vm->frame, USBVIDEO_NUMFRAMES-1);
1409 return -EINVAL;
1410 }
1411 if (uvd->frame[vm->frame].frameState == FrameState_Grabbing) {
1412 /* Not an error - can happen */
1413 }
1414 uvd->frame[vm->frame].request = VIDEOSIZE(vm->width, vm->height);
1415 uvd->frame[vm->frame].palette = vm->format;
1416
1417 /* Mark it as ready */
1418 uvd->frame[vm->frame].frameState = FrameState_Ready;
1419
1420 return usbvideo_NewFrame(uvd, vm->frame);
1421 }
1422 case VIDIOCSYNC:
1423 {
1424 int *frameNum = arg;
1425 int ret;
1426
1427 if (*frameNum < 0 || *frameNum >= USBVIDEO_NUMFRAMES)
1428 return -EINVAL;
1429
1430 if (uvd->debug >= 1)
1431 info("VIDIOCSYNC: syncing to frame %d.", *frameNum);
1432 if (uvd->flags & FLAGS_NO_DECODING)
1433 ret = usbvideo_GetFrame(uvd, *frameNum);
1434 else if (VALID_CALLBACK(uvd, getFrame)) {
1435 ret = GET_CALLBACK(uvd, getFrame)(uvd, *frameNum);
1436 if ((ret < 0) && (uvd->debug >= 1)) {
1437 err("VIDIOCSYNC: getFrame() returned %d.", ret);
1438 }
1439 } else {
1440 err("VIDIOCSYNC: getFrame is not set");
1441 ret = -EFAULT;
1442 }
1443
1444 /*
1445 * The frame is in FrameState_Done_Hold state. Release it
1446 * right now because its data is already mapped into
1447 * the user space and it's up to the application to
1448 * make use of it until it asks for another frame.
1449 */
1450 uvd->frame[*frameNum].frameState = FrameState_Unused;
1451 return ret;
1452 }
1453 case VIDIOCGFBUF:
1454 {
1455 struct video_buffer *vb = arg;
1456
1457 memset(vb, 0, sizeof(*vb));
1458 return 0;
1459 }
1460 case VIDIOCKEY:
1461 return 0;
1462
1463 case VIDIOCCAPTURE:
1464 return -EINVAL;
1465
1466 case VIDIOCSFBUF:
1467
1468 case VIDIOCGTUNER:
1469 case VIDIOCSTUNER:
1470
1471 case VIDIOCGFREQ:
1472 case VIDIOCSFREQ:
1473
1474 case VIDIOCGAUDIO:
1475 case VIDIOCSAUDIO:
1476 return -EINVAL;
1477
1478 default:
1479 return -ENOIOCTLCMD;
1480 }
1481 return 0;
1482}
1483
1484static int usbvideo_v4l_ioctl(struct inode *inode, struct file *file,
1485 unsigned int cmd, unsigned long arg)
1486{
1487 return video_usercopy(inode, file, cmd, arg, usbvideo_v4l_do_ioctl);
1488}
1489
1490/*
1491 * usbvideo_v4l_read()
1492 *
1493 * This is mostly boring stuff. We simply ask for a frame and when it
1494 * arrives copy all the video data from it into user space. There is
1495 * no obvious need to override this method.
1496 *
1497 * History:
1498 * 20-Oct-2000 Created.
1499 * 01-Nov-2000 Added mutex (uvd->lock).
1500 */
1501static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf,
1502 size_t count, loff_t *ppos)
1503{
1504 struct uvd *uvd = file->private_data;
1505 int noblock = file->f_flags & O_NONBLOCK;
1506 int frmx = -1, i;
1507 struct usbvideo_frame *frame;
1508
1509 if (!CAMERA_IS_OPERATIONAL(uvd) || (buf == NULL))
1510 return -EFAULT;
1511
1512 if (uvd->debug >= 1)
1513 info("%s: %Zd. bytes, noblock=%d.", __FUNCTION__, count, noblock);
1514
1515 down(&uvd->lock);
1516
1517 /* See if a frame is completed, then use it. */
1518 for(i = 0; i < USBVIDEO_NUMFRAMES; i++) {
1519 if ((uvd->frame[i].frameState == FrameState_Done) ||
1520 (uvd->frame[i].frameState == FrameState_Done_Hold) ||
1521 (uvd->frame[i].frameState == FrameState_Error)) {
1522 frmx = i;
1523 break;
1524 }
1525 }
1526
1527 /* FIXME: If we don't start a frame here then who ever does? */
1528 if (noblock && (frmx == -1)) {
1529 count = -EAGAIN;
1530 goto read_done;
1531 }
1532
1533 /*
1534 * If no FrameState_Done, look for a FrameState_Grabbing state.
1535 * See if a frame is in process (grabbing), then use it.
1536 * We will need to wait until it becomes cooked, of course.
1537 */
1538 if (frmx == -1) {
1539 for(i = 0; i < USBVIDEO_NUMFRAMES; i++) {
1540 if (uvd->frame[i].frameState == FrameState_Grabbing) {
1541 frmx = i;
1542 break;
1543 }
1544 }
1545 }
1546
1547 /*
1548 * If no frame is active, start one. We don't care which one
1549 * it will be, so #0 is as good as any.
1550 * In read access mode we don't have convenience of VIDIOCMCAPTURE
1551 * to specify the requested palette (video format) on per-frame
1552 * basis. This means that we have to return data in -some- format
1553 * and just hope that the client knows what to do with it.
1554 * The default format is configured in uvd->defaultPalette field
1555 * as one of VIDEO_PALETTE_xxx values. We stuff it into the new
1556 * frame and initiate the frame filling process.
1557 */
1558 if (frmx == -1) {
1559 if (uvd->defaultPalette == 0) {
1560 err("%s: No default palette; don't know what to do!", __FUNCTION__);
1561 count = -EFAULT;
1562 goto read_done;
1563 }
1564 frmx = 0;
1565 /*
1566 * We have no per-frame control over video size.
1567 * Therefore we only can use whatever size was
1568 * specified as default.
1569 */
1570 uvd->frame[frmx].request = uvd->videosize;
1571 uvd->frame[frmx].palette = uvd->defaultPalette;
1572 uvd->frame[frmx].frameState = FrameState_Ready;
1573 usbvideo_NewFrame(uvd, frmx);
1574 /* Now frame 0 is supposed to start filling... */
1575 }
1576
1577 /*
1578 * Get a pointer to the active frame. It is either previously
1579 * completed frame or frame in progress but not completed yet.
1580 */
1581 frame = &uvd->frame[frmx];
1582
1583 /*
1584 * Sit back & wait until the frame gets filled and postprocessed.
1585 * If we fail to get the picture [in time] then return the error.
1586 * In this call we specify that we want the frame to be waited for,
1587 * postprocessed and switched into FrameState_Done_Hold state. This
1588 * state is used to hold the frame as "fully completed" between
1589 * subsequent partial reads of the same frame.
1590 */
1591 if (frame->frameState != FrameState_Done_Hold) {
1592 long rv = -EFAULT;
1593 if (uvd->flags & FLAGS_NO_DECODING)
1594 rv = usbvideo_GetFrame(uvd, frmx);
1595 else if (VALID_CALLBACK(uvd, getFrame))
1596 rv = GET_CALLBACK(uvd, getFrame)(uvd, frmx);
1597 else
1598 err("getFrame is not set");
1599 if ((rv != 0) || (frame->frameState != FrameState_Done_Hold)) {
1600 count = rv;
1601 goto read_done;
1602 }
1603 }
1604
1605 /*
1606 * Copy bytes to user space. We allow for partial reads, which
1607 * means that the user application can request read less than
1608 * the full frame size. It is up to the application to issue
1609 * subsequent calls until entire frame is read.
1610 *
1611 * First things first, make sure we don't copy more than we
1612 * have - even if the application wants more. That would be
1613 * a big security embarassment!
1614 */
1615 if ((count + frame->seqRead_Index) > frame->seqRead_Length)
1616 count = frame->seqRead_Length - frame->seqRead_Index;
1617
1618 /*
1619 * Copy requested amount of data to user space. We start
1620 * copying from the position where we last left it, which
1621 * will be zero for a new frame (not read before).
1622 */
1623 if (copy_to_user(buf, frame->data + frame->seqRead_Index, count)) {
1624 count = -EFAULT;
1625 goto read_done;
1626 }
1627
1628 /* Update last read position */
1629 frame->seqRead_Index += count;
1630 if (uvd->debug >= 1) {
1631 err("%s: {copy} count used=%Zd, new seqRead_Index=%ld",
1632 __FUNCTION__, count, frame->seqRead_Index);
1633 }
1634
1635 /* Finally check if the frame is done with and "release" it */
1636 if (frame->seqRead_Index >= frame->seqRead_Length) {
1637 /* All data has been read */
1638 frame->seqRead_Index = 0;
1639
1640 /* Mark it as available to be used again. */
1641 uvd->frame[frmx].frameState = FrameState_Unused;
1642 if (usbvideo_NewFrame(uvd, (frmx + 1) % USBVIDEO_NUMFRAMES)) {
1643 err("%s: usbvideo_NewFrame failed.", __FUNCTION__);
1644 }
1645 }
1646read_done:
1647 up(&uvd->lock);
1648 return count;
1649}
1650
1651/*
1652 * Make all of the blocks of data contiguous
1653 */
1654static int usbvideo_CompressIsochronous(struct uvd *uvd, struct urb *urb)
1655{
1656 char *cdata;
1657 int i, totlen = 0;
1658
1659 for (i = 0; i < urb->number_of_packets; i++) {
1660 int n = urb->iso_frame_desc[i].actual_length;
1661 int st = urb->iso_frame_desc[i].status;
1662
1663 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1664
1665 /* Detect and ignore errored packets */
1666 if (st < 0) {
1667 if (uvd->debug >= 1)
1668 err("Data error: packet=%d. len=%d. status=%d.", i, n, st);
1669 uvd->stats.iso_err_count++;
1670 continue;
1671 }
1672
1673 /* Detect and ignore empty packets */
1674 if (n <= 0) {
1675 uvd->stats.iso_skip_count++;
1676 continue;
1677 }
1678 totlen += n; /* Little local accounting */
1679 RingQueue_Enqueue(&uvd->dp, cdata, n);
1680 }
1681 return totlen;
1682}
1683
1684static void usbvideo_IsocIrq(struct urb *urb, struct pt_regs *regs)
1685{
1686 int i, ret, len;
1687 struct uvd *uvd = urb->context;
1688
1689 /* We don't want to do anything if we are about to be removed! */
1690 if (!CAMERA_IS_OPERATIONAL(uvd))
1691 return;
1692#if 0
1693 if (urb->actual_length > 0) {
1694 info("urb=$%p status=%d. errcount=%d. length=%d.",
1695 urb, urb->status, urb->error_count, urb->actual_length);
1696 } else {
1697 static int c = 0;
1698 if (c++ % 100 == 0)
1699 info("No Isoc data");
1700 }
1701#endif
1702
1703 if (!uvd->streaming) {
1704 if (uvd->debug >= 1)
1705 info("Not streaming, but interrupt!");
1706 return;
1707 }
1708
1709 uvd->stats.urb_count++;
1710 if (urb->actual_length <= 0)
1711 goto urb_done_with;
1712
1713 /* Copy the data received into ring queue */
1714 len = usbvideo_CompressIsochronous(uvd, urb);
1715 uvd->stats.urb_length = len;
1716 if (len <= 0)
1717 goto urb_done_with;
1718
1719 /* Here we got some data */
1720 uvd->stats.data_count += len;
1721 RingQueue_WakeUpInterruptible(&uvd->dp);
1722
1723urb_done_with:
1724 for (i = 0; i < FRAMES_PER_DESC; i++) {
1725 urb->iso_frame_desc[i].status = 0;
1726 urb->iso_frame_desc[i].actual_length = 0;
1727 }
1728 urb->status = 0;
1729 urb->dev = uvd->dev;
1730 ret = usb_submit_urb (urb, GFP_KERNEL);
1731 if(ret)
1732 err("usb_submit_urb error (%d)", ret);
1733 return;
1734}
1735
1736/*
1737 * usbvideo_StartDataPump()
1738 *
1739 * History:
1740 * 27-Jan-2000 Used ibmcam->iface, ibmcam->ifaceAltActive instead
1741 * of hardcoded values. Simplified by using for loop,
1742 * allowed any number of URBs.
1743 */
1744static int usbvideo_StartDataPump(struct uvd *uvd)
1745{
1746 struct usb_device *dev = uvd->dev;
1747 int i, errFlag;
1748
1749 if (uvd->debug > 1)
1750 info("%s($%p)", __FUNCTION__, uvd);
1751
1752 if (!CAMERA_IS_OPERATIONAL(uvd)) {
1753 err("%s: Camera is not operational", __FUNCTION__);
1754 return -EFAULT;
1755 }
1756 uvd->curframe = -1;
1757
1758 /* Alternate interface 1 is is the biggest frame size */
1759 i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
1760 if (i < 0) {
1761 err("%s: usb_set_interface error", __FUNCTION__);
1762 uvd->last_error = i;
1763 return -EBUSY;
1764 }
1765 if (VALID_CALLBACK(uvd, videoStart))
1766 GET_CALLBACK(uvd, videoStart)(uvd);
1767 else
1768 err("%s: videoStart not set", __FUNCTION__);
1769
1770 /* We double buffer the Iso lists */
1771 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1772 int j, k;
1773 struct urb *urb = uvd->sbuf[i].urb;
1774 urb->dev = dev;
1775 urb->context = uvd;
1776 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
1777 urb->interval = 1;
1778 urb->transfer_flags = URB_ISO_ASAP;
1779 urb->transfer_buffer = uvd->sbuf[i].data;
1780 urb->complete = usbvideo_IsocIrq;
1781 urb->number_of_packets = FRAMES_PER_DESC;
1782 urb->transfer_buffer_length = uvd->iso_packet_len * FRAMES_PER_DESC;
1783 for (j=k=0; j < FRAMES_PER_DESC; j++, k += uvd->iso_packet_len) {
1784 urb->iso_frame_desc[j].offset = k;
1785 urb->iso_frame_desc[j].length = uvd->iso_packet_len;
1786 }
1787 }
1788
1789 /* Submit all URBs */
1790 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1791 errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
1792 if (errFlag)
1793 err("%s: usb_submit_isoc(%d) ret %d", __FUNCTION__, i, errFlag);
1794 }
1795
1796 uvd->streaming = 1;
1797 if (uvd->debug > 1)
1798 info("%s: streaming=1 video_endp=$%02x", __FUNCTION__, uvd->video_endp);
1799 return 0;
1800}
1801
1802/*
1803 * usbvideo_StopDataPump()
1804 *
1805 * This procedure stops streaming and deallocates URBs. Then it
1806 * activates zero-bandwidth alt. setting of the video interface.
1807 *
1808 * History:
1809 * 22-Jan-2000 Corrected order of actions to work after surprise removal.
1810 * 27-Jan-2000 Used uvd->iface, uvd->ifaceAltInactive instead of hardcoded values.
1811 */
1812static void usbvideo_StopDataPump(struct uvd *uvd)
1813{
1814 int i, j;
1815
1816 if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
1817 return;
1818
1819 if (uvd->debug > 1)
1820 info("%s($%p)", __FUNCTION__, uvd);
1821
1822 /* Unschedule all of the iso td's */
1823 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1824 usb_kill_urb(uvd->sbuf[i].urb);
1825 }
1826 if (uvd->debug > 1)
1827 info("%s: streaming=0", __FUNCTION__);
1828 uvd->streaming = 0;
1829
1830 if (!uvd->remove_pending) {
1831 /* Invoke minidriver's magic to stop the camera */
1832 if (VALID_CALLBACK(uvd, videoStop))
1833 GET_CALLBACK(uvd, videoStop)(uvd);
1834 else
1835 err("%s: videoStop not set", __FUNCTION__);
1836
1837 /* Set packet size to 0 */
1838 j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
1839 if (j < 0) {
1840 err("%s: usb_set_interface() error %d.", __FUNCTION__, j);
1841 uvd->last_error = j;
1842 }
1843 }
1844}
1845
1846/*
1847 * usbvideo_NewFrame()
1848 *
1849 * History:
1850 * 29-Mar-00 Added copying of previous frame into the current one.
1851 * 6-Aug-00 Added model 3 video sizes, removed redundant width, height.
1852 */
1853static int usbvideo_NewFrame(struct uvd *uvd, int framenum)
1854{
1855 struct usbvideo_frame *frame;
1856 int n;
1857
1858 if (uvd->debug > 1)
1859 info("usbvideo_NewFrame($%p,%d.)", uvd, framenum);
1860
1861 /* If we're not grabbing a frame right now and the other frame is */
1862 /* ready to be grabbed into, then use it instead */
1863 if (uvd->curframe != -1)
1864 return 0;
1865
1866 /* If necessary we adjust picture settings between frames */
1867 if (!uvd->settingsAdjusted) {
1868 if (VALID_CALLBACK(uvd, adjustPicture))
1869 GET_CALLBACK(uvd, adjustPicture)(uvd);
1870 uvd->settingsAdjusted = 1;
1871 }
1872
1873 n = (framenum + 1) % USBVIDEO_NUMFRAMES;
1874 if (uvd->frame[n].frameState == FrameState_Ready)
1875 framenum = n;
1876
1877 frame = &uvd->frame[framenum];
1878
1879 frame->frameState = FrameState_Grabbing;
1880 frame->scanstate = ScanState_Scanning;
1881 frame->seqRead_Length = 0; /* Accumulated in xxx_parse_data() */
1882 frame->deinterlace = Deinterlace_None;
1883 frame->flags = 0; /* No flags yet, up to minidriver (or us) to set them */
1884 uvd->curframe = framenum;
1885
1886 /*
1887 * Normally we would want to copy previous frame into the current one
1888 * before we even start filling it with data; this allows us to stop
1889 * filling at any moment; top portion of the frame will be new and
1890 * bottom portion will stay as it was in previous frame. If we don't
1891 * do that then missing chunks of video stream will result in flickering
1892 * portions of old data whatever it was before.
1893 *
1894 * If we choose not to copy previous frame (to, for example, save few
1895 * bus cycles - the frame can be pretty large!) then we have an option
1896 * to clear the frame before using. If we experience losses in this
1897 * mode then missing picture will be black (no flickering).
1898 *
1899 * Finally, if user chooses not to clean the current frame before
1900 * filling it with data then the old data will be visible if we fail
1901 * to refill entire frame with new data.
1902 */
1903 if (!(uvd->flags & FLAGS_SEPARATE_FRAMES)) {
1904 /* This copies previous frame into this one to mask losses */
1905 int prev = (framenum - 1 + USBVIDEO_NUMFRAMES) % USBVIDEO_NUMFRAMES;
1906 memmove(frame->data, uvd->frame[prev].data, uvd->max_frame_size);
1907 } else {
1908 if (uvd->flags & FLAGS_CLEAN_FRAMES) {
1909 /* This provides a "clean" frame but slows things down */
1910 memset(frame->data, 0, uvd->max_frame_size);
1911 }
1912 }
1913 return 0;
1914}
1915
1916/*
1917 * usbvideo_CollectRawData()
1918 *
1919 * This procedure can be used instead of 'processData' callback if you
1920 * only want to dump the raw data from the camera into the output
1921 * device (frame buffer). You can look at it with V4L client, but the
1922 * image will be unwatchable. The main purpose of this code and of the
1923 * mode FLAGS_NO_DECODING is debugging and capturing of datastreams from
1924 * new, unknown cameras. This procedure will be automatically invoked
1925 * instead of the specified callback handler when uvd->flags has bit
1926 * FLAGS_NO_DECODING set. Therefore, any regular build of any driver
1927 * based on usbvideo can use this feature at any time.
1928 */
1929static void usbvideo_CollectRawData(struct uvd *uvd, struct usbvideo_frame *frame)
1930{
1931 int n;
1932
1933 assert(uvd != NULL);
1934 assert(frame != NULL);
1935
1936 /* Try to move data from queue into frame buffer */
1937 n = RingQueue_GetLength(&uvd->dp);
1938 if (n > 0) {
1939 int m;
1940 /* See how much space we have left */
1941 m = uvd->max_frame_size - frame->seqRead_Length;
1942 if (n > m)
1943 n = m;
1944 /* Now move that much data into frame buffer */
1945 RingQueue_Dequeue(
1946 &uvd->dp,
1947 frame->data + frame->seqRead_Length,
1948 m);
1949 frame->seqRead_Length += m;
1950 }
1951 /* See if we filled the frame */
1952 if (frame->seqRead_Length >= uvd->max_frame_size) {
1953 frame->frameState = FrameState_Done;
1954 uvd->curframe = -1;
1955 uvd->stats.frame_num++;
1956 }
1957}
1958
1959static int usbvideo_GetFrame(struct uvd *uvd, int frameNum)
1960{
1961 struct usbvideo_frame *frame = &uvd->frame[frameNum];
1962
1963 if (uvd->debug >= 2)
1964 info("%s($%p,%d.)", __FUNCTION__, uvd, frameNum);
1965
1966 switch (frame->frameState) {
1967 case FrameState_Unused:
1968 if (uvd->debug >= 2)
1969 info("%s: FrameState_Unused", __FUNCTION__);
1970 return -EINVAL;
1971 case FrameState_Ready:
1972 case FrameState_Grabbing:
1973 case FrameState_Error:
1974 {
1975 int ntries, signalPending;
1976 redo:
1977 if (!CAMERA_IS_OPERATIONAL(uvd)) {
1978 if (uvd->debug >= 2)
1979 info("%s: Camera is not operational (1)", __FUNCTION__);
1980 return -EIO;
1981 }
1982 ntries = 0;
1983 do {
1984 RingQueue_InterruptibleSleepOn(&uvd->dp);
1985 signalPending = signal_pending(current);
1986 if (!CAMERA_IS_OPERATIONAL(uvd)) {
1987 if (uvd->debug >= 2)
1988 info("%s: Camera is not operational (2)", __FUNCTION__);
1989 return -EIO;
1990 }
1991 assert(uvd->fbuf != NULL);
1992 if (signalPending) {
1993 if (uvd->debug >= 2)
1994 info("%s: Signal=$%08x", __FUNCTION__, signalPending);
1995 if (uvd->flags & FLAGS_RETRY_VIDIOCSYNC) {
1996 usbvideo_TestPattern(uvd, 1, 0);
1997 uvd->curframe = -1;
1998 uvd->stats.frame_num++;
1999 if (uvd->debug >= 2)
2000 info("%s: Forced test pattern screen", __FUNCTION__);
2001 return 0;
2002 } else {
2003 /* Standard answer: Interrupted! */
2004 if (uvd->debug >= 2)
2005 info("%s: Interrupted!", __FUNCTION__);
2006 return -EINTR;
2007 }
2008 } else {
2009 /* No signals - we just got new data in dp queue */
2010 if (uvd->flags & FLAGS_NO_DECODING)
2011 usbvideo_CollectRawData(uvd, frame);
2012 else if (VALID_CALLBACK(uvd, processData))
2013 GET_CALLBACK(uvd, processData)(uvd, frame);
2014 else
2015 err("%s: processData not set", __FUNCTION__);
2016 }
2017 } while (frame->frameState == FrameState_Grabbing);
2018 if (uvd->debug >= 2) {
2019 info("%s: Grabbing done; state=%d. (%lu. bytes)",
2020 __FUNCTION__, frame->frameState, frame->seqRead_Length);
2021 }
2022 if (frame->frameState == FrameState_Error) {
2023 int ret = usbvideo_NewFrame(uvd, frameNum);
2024 if (ret < 0) {
2025 err("%s: usbvideo_NewFrame() failed (%d.)", __FUNCTION__, ret);
2026 return ret;
2027 }
2028 goto redo;
2029 }
2030 /* Note that we fall through to meet our destiny below */
2031 }
2032 case FrameState_Done:
2033 /*
2034 * Do all necessary postprocessing of data prepared in
2035 * "interrupt" code and the collecting code above. The
2036 * frame gets marked as FrameState_Done by queue parsing code.
2037 * This status means that we collected enough data and
2038 * most likely processed it as we went through. However
2039 * the data may need postprocessing, such as deinterlacing
2040 * or picture adjustments implemented in software (horror!)
2041 *
2042 * As soon as the frame becomes "final" it gets promoted to
2043 * FrameState_Done_Hold status where it will remain until the
2044 * caller consumed all the video data from the frame. Then
2045 * the empty shell of ex-frame is thrown out for dogs to eat.
2046 * But we, worried about pets, will recycle the frame!
2047 */
2048 uvd->stats.frame_num++;
2049 if ((uvd->flags & FLAGS_NO_DECODING) == 0) {
2050 if (VALID_CALLBACK(uvd, postProcess))
2051 GET_CALLBACK(uvd, postProcess)(uvd, frame);
2052 if (frame->flags & USBVIDEO_FRAME_FLAG_SOFTWARE_CONTRAST)
2053 usbvideo_SoftwareContrastAdjustment(uvd, frame);
2054 }
2055 frame->frameState = FrameState_Done_Hold;
2056 if (uvd->debug >= 2)
2057 info("%s: Entered FrameState_Done_Hold state.", __FUNCTION__);
2058 return 0;
2059
2060 case FrameState_Done_Hold:
2061 /*
2062 * We stay in this state indefinitely until someone external,
2063 * like ioctl() or read() call finishes digesting the frame
2064 * data. Then it will mark the frame as FrameState_Unused and
2065 * it will be released back into the wild to roam freely.
2066 */
2067 if (uvd->debug >= 2)
2068 info("%s: FrameState_Done_Hold state.", __FUNCTION__);
2069 return 0;
2070 }
2071
2072 /* Catch-all for other cases. We shall not be here. */
2073 err("%s: Invalid state %d.", __FUNCTION__, frame->frameState);
2074 frame->frameState = FrameState_Unused;
2075 return 0;
2076}
2077
2078/*
2079 * usbvideo_DeinterlaceFrame()
2080 *
2081 * This procedure deinterlaces the given frame. Some cameras produce
2082 * only half of scanlines - sometimes only even lines, sometimes only
2083 * odd lines. The deinterlacing method is stored in frame->deinterlace
2084 * variable.
2085 *
2086 * Here we scan the frame vertically and replace missing scanlines with
2087 * average between surrounding ones - before and after. If we have no
2088 * line above then we just copy next line. Similarly, if we need to
2089 * create a last line then preceding line is used.
2090 */
2091void usbvideo_DeinterlaceFrame(struct uvd *uvd, struct usbvideo_frame *frame)
2092{
2093 if ((uvd == NULL) || (frame == NULL))
2094 return;
2095
2096 if ((frame->deinterlace == Deinterlace_FillEvenLines) ||
2097 (frame->deinterlace == Deinterlace_FillOddLines))
2098 {
2099 const int v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
2100 int i = (frame->deinterlace == Deinterlace_FillEvenLines) ? 0 : 1;
2101
2102 for (; i < VIDEOSIZE_Y(frame->request); i += 2) {
2103 const unsigned char *fs1, *fs2;
2104 unsigned char *fd;
2105 int ip, in, j; /* Previous and next lines */
2106
2107 /*
2108 * Need to average lines before and after 'i'.
2109 * If we go out of bounds seeking those lines then
2110 * we point back to existing line.
2111 */
2112 ip = i - 1; /* First, get rough numbers */
2113 in = i + 1;
2114
2115 /* Now validate */
2116 if (ip < 0)
2117 ip = in;
2118 if (in >= VIDEOSIZE_Y(frame->request))
2119 in = ip;
2120
2121 /* Sanity check */
2122 if ((ip < 0) || (in < 0) ||
2123 (ip >= VIDEOSIZE_Y(frame->request)) ||
2124 (in >= VIDEOSIZE_Y(frame->request)))
2125 {
2126 err("Error: ip=%d. in=%d. req.height=%ld.",
2127 ip, in, VIDEOSIZE_Y(frame->request));
2128 break;
2129 }
2130
2131 /* Now we need to average lines 'ip' and 'in' to produce line 'i' */
2132 fs1 = frame->data + (v4l_linesize * ip);
2133 fs2 = frame->data + (v4l_linesize * in);
2134 fd = frame->data + (v4l_linesize * i);
2135
2136 /* Average lines around destination */
2137 for (j=0; j < v4l_linesize; j++) {
2138 fd[j] = (unsigned char)((((unsigned) fs1[j]) +
2139 ((unsigned)fs2[j])) >> 1);
2140 }
2141 }
2142 }
2143
2144 /* Optionally display statistics on the screen */
2145 if (uvd->flags & FLAGS_OVERLAY_STATS)
2146 usbvideo_OverlayStats(uvd, frame);
2147}
2148
2149EXPORT_SYMBOL(usbvideo_DeinterlaceFrame);
2150
2151/*
2152 * usbvideo_SoftwareContrastAdjustment()
2153 *
2154 * This code adjusts the contrast of the frame, assuming RGB24 format.
2155 * As most software image processing, this job is CPU-intensive.
2156 * Get a camera that supports hardware adjustment!
2157 *
2158 * History:
2159 * 09-Feb-2001 Created.
2160 */
2161static void usbvideo_SoftwareContrastAdjustment(struct uvd *uvd,
2162 struct usbvideo_frame *frame)
2163{
2164 int i, j, v4l_linesize;
2165 signed long adj;
2166 const int ccm = 128; /* Color correction median - see below */
2167
2168 if ((uvd == NULL) || (frame == NULL)) {
2169 err("%s: Illegal call.", __FUNCTION__);
2170 return;
2171 }
2172 adj = (uvd->vpic.contrast - 0x8000) >> 8; /* -128..+127 = -ccm..+(ccm-1)*/
2173 RESTRICT_TO_RANGE(adj, -ccm, ccm+1);
2174 if (adj == 0) {
2175 /* In rare case of no adjustment */
2176 return;
2177 }
2178 v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
2179 for (i=0; i < VIDEOSIZE_Y(frame->request); i++) {
2180 unsigned char *fd = frame->data + (v4l_linesize * i);
2181 for (j=0; j < v4l_linesize; j++) {
2182 signed long v = (signed long) fd[j];
2183 /* Magnify up to 2 times, reduce down to zero */
2184 v = 128 + ((ccm + adj) * (v - 128)) / ccm;
2185 RESTRICT_TO_RANGE(v, 0, 0xFF); /* Must flatten tails */
2186 fd[j] = (unsigned char) v;
2187 }
2188 }
2189}
2190
2191MODULE_LICENSE("GPL");
diff --git a/drivers/usb/media/usbvideo.h b/drivers/usb/media/usbvideo.h
deleted file mode 100644
index 6c390a1f981b..000000000000
--- a/drivers/usb/media/usbvideo.h
+++ /dev/null
@@ -1,393 +0,0 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2, or (at your option)
5 * any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16#ifndef usbvideo_h
17#define usbvideo_h
18
19#include <linux/config.h>
20#include <linux/videodev.h>
21#include <linux/usb.h>
22
23/* Most helpful debugging aid */
24#define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__))))
25
26#define USBVIDEO_REPORT_STATS 1 /* Set to 0 to block statistics on close */
27
28/* Bit flags (options) */
29#define FLAGS_RETRY_VIDIOCSYNC (1 << 0)
30#define FLAGS_MONOCHROME (1 << 1)
31#define FLAGS_DISPLAY_HINTS (1 << 2)
32#define FLAGS_OVERLAY_STATS (1 << 3)
33#define FLAGS_FORCE_TESTPATTERN (1 << 4)
34#define FLAGS_SEPARATE_FRAMES (1 << 5)
35#define FLAGS_CLEAN_FRAMES (1 << 6)
36#define FLAGS_NO_DECODING (1 << 7)
37
38/* Bit flags for frames (apply to the frame where they are specified) */
39#define USBVIDEO_FRAME_FLAG_SOFTWARE_CONTRAST (1 << 0)
40
41/* Camera capabilities (maximum) */
42#define CAMERA_URB_FRAMES 32
43#define CAMERA_MAX_ISO_PACKET 1023 /* 1022 actually sent by camera */
44#define FRAMES_PER_DESC (CAMERA_URB_FRAMES)
45#define FRAME_SIZE_PER_DESC (CAMERA_MAX_ISO_PACKET)
46
47/* This macro restricts an int variable to an inclusive range */
48#define RESTRICT_TO_RANGE(v,mi,ma) { if ((v) < (mi)) (v) = (mi); else if ((v) > (ma)) (v) = (ma); }
49
50#define V4L_BYTES_PER_PIXEL 3 /* Because we produce RGB24 */
51
52/*
53 * Use this macro to construct constants for different video sizes.
54 * We have to deal with different video sizes that have to be
55 * configured in the device or compared against when we receive
56 * a data. Normally one would define a bunch of VIDEOSIZE_x_by_y
57 * #defines and that's the end of story. However this solution
58 * does not allow to convert between real pixel sizes and the
59 * constant (integer) value that may be used to tag a frame or
60 * whatever. The set of macros below constructs videosize constants
61 * from the pixel size and allows to reconstruct the pixel size
62 * from the combined value later.
63 */
64#define VIDEOSIZE(x,y) (((x) & 0xFFFFL) | (((y) & 0xFFFFL) << 16))
65#define VIDEOSIZE_X(vs) ((vs) & 0xFFFFL)
66#define VIDEOSIZE_Y(vs) (((vs) >> 16) & 0xFFFFL)
67typedef unsigned long videosize_t;
68
69/*
70 * This macro checks if the camera is still operational. The 'uvd'
71 * pointer must be valid, uvd->dev must be valid, we are not
72 * removing the device and the device has not erred on us.
73 */
74#define CAMERA_IS_OPERATIONAL(uvd) (\
75 (uvd != NULL) && \
76 ((uvd)->dev != NULL) && \
77 ((uvd)->last_error == 0) && \
78 (!(uvd)->remove_pending))
79
80/*
81 * We use macros to do YUV -> RGB conversion because this is
82 * very important for speed and totally unimportant for size.
83 *
84 * YUV -> RGB Conversion
85 * ---------------------
86 *
87 * B = 1.164*(Y-16) + 2.018*(V-128)
88 * G = 1.164*(Y-16) - 0.813*(U-128) - 0.391*(V-128)
89 * R = 1.164*(Y-16) + 1.596*(U-128)
90 *
91 * If you fancy integer arithmetics (as you should), hear this:
92 *
93 * 65536*B = 76284*(Y-16) + 132252*(V-128)
94 * 65536*G = 76284*(Y-16) - 53281*(U-128) - 25625*(V-128)
95 * 65536*R = 76284*(Y-16) + 104595*(U-128)
96 *
97 * Make sure the output values are within [0..255] range.
98 */
99#define LIMIT_RGB(x) (((x) < 0) ? 0 : (((x) > 255) ? 255 : (x)))
100#define YUV_TO_RGB_BY_THE_BOOK(my,mu,mv,mr,mg,mb) { \
101 int mm_y, mm_yc, mm_u, mm_v, mm_r, mm_g, mm_b; \
102 mm_y = (my) - 16; \
103 mm_u = (mu) - 128; \
104 mm_v = (mv) - 128; \
105 mm_yc= mm_y * 76284; \
106 mm_b = (mm_yc + 132252*mm_v ) >> 16; \
107 mm_g = (mm_yc - 53281*mm_u - 25625*mm_v ) >> 16; \
108 mm_r = (mm_yc + 104595*mm_u ) >> 16; \
109 mb = LIMIT_RGB(mm_b); \
110 mg = LIMIT_RGB(mm_g); \
111 mr = LIMIT_RGB(mm_r); \
112}
113
114#define RING_QUEUE_SIZE (128*1024) /* Must be a power of 2 */
115#define RING_QUEUE_ADVANCE_INDEX(rq,ind,n) (rq)->ind = ((rq)->ind + (n)) & ((rq)->length-1)
116#define RING_QUEUE_DEQUEUE_BYTES(rq,n) RING_QUEUE_ADVANCE_INDEX(rq,ri,n)
117#define RING_QUEUE_PEEK(rq,ofs) ((rq)->queue[((ofs) + (rq)->ri) & ((rq)->length-1)])
118
119struct RingQueue {
120 unsigned char *queue; /* Data from the Isoc data pump */
121 int length; /* How many bytes allocated for the queue */
122 int wi; /* That's where we write */
123 int ri; /* Read from here until you hit write index */
124 wait_queue_head_t wqh; /* Processes waiting */
125};
126
127enum ScanState {
128 ScanState_Scanning, /* Scanning for header */
129 ScanState_Lines /* Parsing lines */
130};
131
132/* Completion states of the data parser */
133enum ParseState {
134 scan_Continue, /* Just parse next item */
135 scan_NextFrame, /* Frame done, send it to V4L */
136 scan_Out, /* Not enough data for frame */
137 scan_EndParse /* End parsing */
138};
139
140enum FrameState {
141 FrameState_Unused, /* Unused (no MCAPTURE) */
142 FrameState_Ready, /* Ready to start grabbing */
143 FrameState_Grabbing, /* In the process of being grabbed into */
144 FrameState_Done, /* Finished grabbing, but not been synced yet */
145 FrameState_Done_Hold, /* Are syncing or reading */
146 FrameState_Error, /* Something bad happened while processing */
147};
148
149/*
150 * Some frames may contain only even or odd lines. This type
151 * specifies what type of deinterlacing is required.
152 */
153enum Deinterlace {
154 Deinterlace_None=0,
155 Deinterlace_FillOddLines,
156 Deinterlace_FillEvenLines
157};
158
159#define USBVIDEO_NUMFRAMES 2 /* How many frames we work with */
160#define USBVIDEO_NUMSBUF 2 /* How many URBs linked in a ring */
161
162/* This structure represents one Isoc request - URB and buffer */
163struct usbvideo_sbuf {
164 char *data;
165 struct urb *urb;
166};
167
168struct usbvideo_frame {
169 char *data; /* Frame buffer */
170 unsigned long header; /* Significant bits from the header */
171
172 videosize_t canvas; /* The canvas (max. image) allocated */
173 videosize_t request; /* That's what the application asked for */
174 unsigned short palette; /* The desired format */
175
176 enum FrameState frameState;/* State of grabbing */
177 enum ScanState scanstate; /* State of scanning */
178 enum Deinterlace deinterlace;
179 int flags; /* USBVIDEO_FRAME_FLAG_xxx bit flags */
180
181 int curline; /* Line of frame we're working on */
182
183 long seqRead_Length; /* Raw data length of frame */
184 long seqRead_Index; /* Amount of data that has been already read */
185
186 void *user; /* Additional data that user may need */
187};
188
189/* Statistics that can be overlaid on screen */
190struct usbvideo_statistics {
191 unsigned long frame_num; /* Sequential number of the frame */
192 unsigned long urb_count; /* How many URBs we received so far */
193 unsigned long urb_length; /* Length of last URB */
194 unsigned long data_count; /* How many bytes we received */
195 unsigned long header_count; /* How many frame headers we found */
196 unsigned long iso_skip_count; /* How many empty ISO packets received */
197 unsigned long iso_err_count; /* How many bad ISO packets received */
198};
199
200struct usbvideo;
201
202struct uvd {
203 struct video_device vdev; /* Must be the first field! */
204 struct usb_device *dev;
205 struct usbvideo *handle; /* Points back to the struct usbvideo */
206 void *user_data; /* Camera-dependent data */
207 int user_size; /* Size of that camera-dependent data */
208 int debug; /* Debug level for usbvideo */
209 unsigned char iface; /* Video interface number */
210 unsigned char video_endp;
211 unsigned char ifaceAltActive;
212 unsigned char ifaceAltInactive; /* Alt settings */
213 unsigned long flags; /* FLAGS_USBVIDEO_xxx */
214 unsigned long paletteBits; /* Which palettes we accept? */
215 unsigned short defaultPalette; /* What palette to use for read() */
216 struct semaphore lock;
217 int user; /* user count for exclusive use */
218
219 videosize_t videosize; /* Current setting */
220 videosize_t canvas; /* This is the width,height of the V4L canvas */
221 int max_frame_size; /* Bytes in one video frame */
222
223 int uvd_used; /* Is this structure in use? */
224 int streaming; /* Are we streaming Isochronous? */
225 int grabbing; /* Are we grabbing? */
226 int settingsAdjusted; /* Have we adjusted contrast etc.? */
227 int last_error; /* What calamity struck us? */
228
229 char *fbuf; /* Videodev buffer area */
230 int fbuf_size; /* Videodev buffer size */
231
232 int curframe;
233 int iso_packet_len; /* Videomode-dependent, saves bus bandwidth */
234
235 struct RingQueue dp; /* Isoc data pump */
236 struct usbvideo_frame frame[USBVIDEO_NUMFRAMES];
237 struct usbvideo_sbuf sbuf[USBVIDEO_NUMSBUF];
238
239 volatile int remove_pending; /* If set then about to exit */
240
241 struct video_picture vpic, vpic_old; /* Picture settings */
242 struct video_capability vcap; /* Video capabilities */
243 struct video_channel vchan; /* May be used for tuner support */
244 struct usbvideo_statistics stats;
245 char videoName[32]; /* Holds name like "video7" */
246};
247
248/*
249 * usbvideo callbacks (virtual methods). They are set when usbvideo
250 * services are registered. All of these default to NULL, except those
251 * that default to usbvideo-provided methods.
252 */
253struct usbvideo_cb {
254 int (*probe)(struct usb_interface *, const struct usb_device_id *);
255 void (*userFree)(struct uvd *);
256 void (*disconnect)(struct usb_interface *);
257 int (*setupOnOpen)(struct uvd *);
258 void (*videoStart)(struct uvd *);
259 void (*videoStop)(struct uvd *);
260 void (*processData)(struct uvd *, struct usbvideo_frame *);
261 void (*postProcess)(struct uvd *, struct usbvideo_frame *);
262 void (*adjustPicture)(struct uvd *);
263 int (*getFPS)(struct uvd *);
264 int (*overlayHook)(struct uvd *, struct usbvideo_frame *);
265 int (*getFrame)(struct uvd *, int);
266 int (*startDataPump)(struct uvd *uvd);
267 void (*stopDataPump)(struct uvd *uvd);
268 int (*setVideoMode)(struct uvd *uvd, struct video_window *vw);
269};
270
271struct usbvideo {
272 int num_cameras; /* As allocated */
273 struct usb_driver usbdrv; /* Interface to the USB stack */
274 char drvName[80]; /* Driver name */
275 struct semaphore lock; /* Mutex protecting camera structures */
276 struct usbvideo_cb cb; /* Table of callbacks (virtual methods) */
277 struct video_device vdt; /* Video device template */
278 struct uvd *cam; /* Array of camera structures */
279 struct module *md_module; /* Minidriver module */
280};
281
282
283/*
284 * This macro retrieves callback address from the struct uvd object.
285 * No validity checks are done here, so be sure to check the
286 * callback beforehand with VALID_CALLBACK.
287 */
288#define GET_CALLBACK(uvd,cbName) ((uvd)->handle->cb.cbName)
289
290/*
291 * This macro returns either callback pointer or NULL. This is safe
292 * macro, meaning that most of components of data structures involved
293 * may be NULL - this only results in NULL being returned. You may
294 * wish to use this macro to make sure that the callback is callable.
295 * However keep in mind that those checks take time.
296 */
297#define VALID_CALLBACK(uvd,cbName) ((((uvd) != NULL) && \
298 ((uvd)->handle != NULL)) ? GET_CALLBACK(uvd,cbName) : NULL)
299
300int RingQueue_Dequeue(struct RingQueue *rq, unsigned char *dst, int len);
301int RingQueue_Enqueue(struct RingQueue *rq, const unsigned char *cdata, int n);
302void RingQueue_WakeUpInterruptible(struct RingQueue *rq);
303void RingQueue_Flush(struct RingQueue *rq);
304
305static inline int RingQueue_GetLength(const struct RingQueue *rq)
306{
307 return (rq->wi - rq->ri + rq->length) & (rq->length-1);
308}
309
310static inline int RingQueue_GetFreeSpace(const struct RingQueue *rq)
311{
312 return rq->length - RingQueue_GetLength(rq);
313}
314
315void usbvideo_DrawLine(
316 struct usbvideo_frame *frame,
317 int x1, int y1,
318 int x2, int y2,
319 unsigned char cr, unsigned char cg, unsigned char cb);
320void usbvideo_HexDump(const unsigned char *data, int len);
321void usbvideo_SayAndWait(const char *what);
322void usbvideo_TestPattern(struct uvd *uvd, int fullframe, int pmode);
323
324/* Memory allocation routines */
325unsigned long usbvideo_kvirt_to_pa(unsigned long adr);
326
327int usbvideo_register(
328 struct usbvideo **pCams,
329 const int num_cams,
330 const int num_extra,
331 const char *driverName,
332 const struct usbvideo_cb *cbTable,
333 struct module *md,
334 const struct usb_device_id *id_table);
335struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams);
336int usbvideo_RegisterVideoDevice(struct uvd *uvd);
337void usbvideo_Deregister(struct usbvideo **uvt);
338
339int usbvideo_v4l_initialize(struct video_device *dev);
340
341void usbvideo_DeinterlaceFrame(struct uvd *uvd, struct usbvideo_frame *frame);
342
343/*
344 * This code performs bounds checking - use it when working with
345 * new formats, or else you may get oopses all over the place.
346 * If pixel falls out of bounds then it gets shoved back (as close
347 * to place of offence as possible) and is painted bright red.
348 *
349 * There are two important concepts: frame width, height and
350 * V4L canvas width, height. The former is the area requested by
351 * the application -for this very frame-. The latter is the largest
352 * possible frame that we can serve (we advertise that via V4L ioctl).
353 * The frame data is expected to be formatted as lines of length
354 * VIDEOSIZE_X(fr->request), total VIDEOSIZE_Y(frame->request) lines.
355 */
356static inline void RGB24_PUTPIXEL(
357 struct usbvideo_frame *fr,
358 int ix, int iy,
359 unsigned char vr,
360 unsigned char vg,
361 unsigned char vb)
362{
363 register unsigned char *pf;
364 int limiter = 0, mx, my;
365 mx = ix;
366 my = iy;
367 if (mx < 0) {
368 mx=0;
369 limiter++;
370 } else if (mx >= VIDEOSIZE_X((fr)->request)) {
371 mx= VIDEOSIZE_X((fr)->request) - 1;
372 limiter++;
373 }
374 if (my < 0) {
375 my = 0;
376 limiter++;
377 } else if (my >= VIDEOSIZE_Y((fr)->request)) {
378 my = VIDEOSIZE_Y((fr)->request) - 1;
379 limiter++;
380 }
381 pf = (fr)->data + V4L_BYTES_PER_PIXEL*((iy)*VIDEOSIZE_X((fr)->request) + (ix));
382 if (limiter) {
383 *pf++ = 0;
384 *pf++ = 0;
385 *pf++ = 0xFF;
386 } else {
387 *pf++ = (vb);
388 *pf++ = (vg);
389 *pf++ = (vr);
390 }
391}
392
393#endif /* usbvideo_h */
diff --git a/drivers/usb/media/vicam.c b/drivers/usb/media/vicam.c
deleted file mode 100644
index 5df144073871..000000000000
--- a/drivers/usb/media/vicam.c
+++ /dev/null
@@ -1,1409 +0,0 @@
1/*
2 * USB ViCam WebCam driver
3 * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
4 * Christopher L Cheney (ccheney@cheney.cx),
5 * Pavel Machek (pavel@suse.cz),
6 * John Tyner (jtyner@cs.ucr.edu),
7 * Monroe Williams (monroe@pobox.com)
8 *
9 * Supports 3COM HomeConnect PC Digital WebCam
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * This source code is based heavily on the CPiA webcam driver which was
26 * written by Peter Pregler, Scott J. Bertin and Johannes Erdfelt
27 *
28 * Portions of this code were also copied from usbvideo.c
29 *
30 * Special thanks to the the whole team at Sourceforge for help making
31 * this driver become a reality. Notably:
32 * Andy Armstrong who reverse engineered the color encoding and
33 * Pavel Machek and Chris Cheney who worked on reverse engineering the
34 * camera controls and wrote the first generation driver.
35 */
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/videodev.h>
41#include <linux/usb.h>
42#include <linux/vmalloc.h>
43#include <linux/slab.h>
44#include <linux/proc_fs.h>
45#include "usbvideo.h"
46
47// #define VICAM_DEBUG
48
49#ifdef VICAM_DEBUG
50#define ADBG(lineno,fmt,args...) printk(fmt, jiffies, __FUNCTION__, lineno, ##args)
51#define DBG(fmt,args...) ADBG((__LINE__),KERN_DEBUG __FILE__"(%ld):%s (%d):"fmt,##args)
52#else
53#define DBG(fmn,args...) do {} while(0)
54#endif
55
56#define DRIVER_AUTHOR "Joe Burks, jburks@wavicle.org"
57#define DRIVER_DESC "ViCam WebCam Driver"
58
59/* Define these values to match your device */
60#define USB_VICAM_VENDOR_ID 0x04c1
61#define USB_VICAM_PRODUCT_ID 0x009d
62
63#define VICAM_BYTES_PER_PIXEL 3
64#define VICAM_MAX_READ_SIZE (512*242+128)
65#define VICAM_MAX_FRAME_SIZE (VICAM_BYTES_PER_PIXEL*320*240)
66#define VICAM_FRAMES 2
67
68#define VICAM_HEADER_SIZE 64
69
70#define clamp( x, l, h ) max_t( __typeof__( x ), \
71 ( l ), \
72 min_t( __typeof__( x ), \
73 ( h ), \
74 ( x ) ) )
75
76/* Not sure what all the bytes in these char
77 * arrays do, but they're necessary to make
78 * the camera work.
79 */
80
81static unsigned char setup1[] = {
82 0xB6, 0xC3, 0x1F, 0x00, 0x02, 0x64, 0xE7, 0x67,
83 0xFD, 0xFF, 0x0E, 0xC0, 0xE7, 0x09, 0xDE, 0x00,
84 0x8E, 0x00, 0xC0, 0x09, 0x40, 0x03, 0xC0, 0x17,
85 0x44, 0x03, 0x4B, 0xAF, 0xC0, 0x07, 0x00, 0x00,
86 0x4B, 0xAF, 0x97, 0xCF, 0x00, 0x00
87};
88
89static unsigned char setup2[] = {
90 0xB6, 0xC3, 0x03, 0x00, 0x03, 0x64, 0x18, 0x00,
91 0x00, 0x00
92};
93
94static unsigned char setup3[] = {
95 0xB6, 0xC3, 0x01, 0x00, 0x06, 0x64, 0x00, 0x00
96};
97
98static unsigned char setup4[] = {
99 0xB6, 0xC3, 0x8F, 0x06, 0x02, 0x64, 0xE7, 0x07,
100 0x00, 0x00, 0x08, 0xC0, 0xE7, 0x07, 0x00, 0x00,
101 0x3E, 0xC0, 0xE7, 0x07, 0x54, 0x01, 0xAA, 0x00,
102 0xE7, 0x07, 0xC8, 0x05, 0xB6, 0x00, 0xE7, 0x07,
103 0x42, 0x01, 0xD2, 0x00, 0xE7, 0x07, 0x7C, 0x00,
104 0x16, 0x00, 0xE7, 0x07, 0x56, 0x00, 0x18, 0x00,
105 0xE7, 0x07, 0x06, 0x00, 0x92, 0xC0, 0xE7, 0x07,
106 0x00, 0x00, 0x1E, 0xC0, 0xE7, 0x07, 0xFF, 0xFF,
107 0x22, 0xC0, 0xE7, 0x07, 0x04, 0x00, 0x24, 0xC0,
108 0xE7, 0x07, 0xEC, 0x27, 0x28, 0xC0, 0xE7, 0x07,
109 0x16, 0x01, 0x8E, 0x00, 0xE7, 0x87, 0x01, 0x00,
110 0x0E, 0xC0, 0x97, 0xCF, 0xD7, 0x09, 0x00, 0xC0,
111 0xE7, 0x77, 0x01, 0x00, 0x92, 0xC0, 0x09, 0xC1,
112 0xE7, 0x09, 0xFE, 0x05, 0x24, 0x01, 0xE7, 0x09,
113 0x04, 0x06, 0x26, 0x01, 0xE7, 0x07, 0x07, 0x00,
114 0x92, 0xC0, 0xE7, 0x05, 0x00, 0xC0, 0xC0, 0xDF,
115 0x97, 0xCF, 0x17, 0x00, 0x57, 0x00, 0x17, 0x02,
116 0xD7, 0x09, 0x00, 0xC0, 0xE7, 0x77, 0x01, 0x00,
117 0x92, 0xC0, 0x0A, 0xC1, 0xE7, 0x57, 0xFF, 0xFF,
118 0xFA, 0x05, 0x0D, 0xC0, 0xE7, 0x57, 0x00, 0x00,
119 0xFA, 0x05, 0x0F, 0xC0, 0x9F, 0xAF, 0xC6, 0x00,
120 0xE7, 0x05, 0x00, 0xC0, 0xC8, 0x05, 0xC1, 0x05,
121 0xC0, 0x05, 0xC0, 0xDF, 0x97, 0xCF, 0x27, 0xDA,
122 0xFA, 0x05, 0xEF, 0x07, 0x01, 0x00, 0x0B, 0x06,
123 0x73, 0xCF, 0x9F, 0xAF, 0x78, 0x01, 0x9F, 0xAF,
124 0x1A, 0x03, 0x6E, 0xCF, 0xE7, 0x09, 0xFC, 0x05,
125 0x24, 0x01, 0xE7, 0x09, 0x02, 0x06, 0x26, 0x01,
126 0xE7, 0x07, 0x07, 0x00, 0x92, 0xC0, 0xE7, 0x09,
127 0xFC, 0x05, 0xFE, 0x05, 0xE7, 0x09, 0x02, 0x06,
128 0x04, 0x06, 0xE7, 0x09, 0x00, 0x06, 0xFC, 0x05,
129 0xE7, 0x09, 0xFE, 0x05, 0x00, 0x06, 0x27, 0xDA,
130 0xFA, 0x05, 0xE7, 0x57, 0x01, 0x00, 0xFA, 0x05,
131 0x02, 0xCA, 0x04, 0xC0, 0x97, 0xCF, 0x9F, 0xAF,
132 0x66, 0x05, 0x97, 0xCF, 0xE7, 0x07, 0x40, 0x00,
133 0x02, 0x06, 0xC8, 0x09, 0xFC, 0x05, 0x9F, 0xAF,
134 0xDA, 0x02, 0x97, 0xCF, 0xCF, 0x17, 0x02, 0x00,
135 0xEF, 0x57, 0x81, 0x00, 0x09, 0x06, 0x9F, 0xA0,
136 0xB6, 0x01, 0xEF, 0x57, 0x80, 0x00, 0x09, 0x06,
137 0x9F, 0xA0, 0x40, 0x02, 0xEF, 0x57, 0x01, 0x00,
138 0x0B, 0x06, 0x9F, 0xA0, 0x46, 0x03, 0xE7, 0x07,
139 0x01, 0x00, 0x0A, 0xC0, 0x46, 0xAF, 0x47, 0xAF,
140 0x9F, 0xAF, 0x40, 0x02, 0xE7, 0x07, 0x2E, 0x00,
141 0x0A, 0xC0, 0xEF, 0x87, 0x80, 0x00, 0x09, 0x06,
142 0x97, 0xCF, 0x00, 0x0E, 0x01, 0x00, 0xC0, 0x57,
143 0x51, 0x00, 0x9F, 0xC0, 0x9E, 0x02, 0xC0, 0x57,
144 0x50, 0x00, 0x20, 0xC0, 0xC0, 0x57, 0x55, 0x00,
145 0x12, 0xC0, 0xC0, 0x57, 0x56, 0x00, 0x9F, 0xC0,
146 0x72, 0x02, 0x9F, 0xCF, 0xD6, 0x02, 0xC1, 0x0B,
147 0x08, 0x06, 0x01, 0xD0, 0x6F, 0x90, 0x08, 0x06,
148 0xC0, 0x07, 0x08, 0x00, 0xC1, 0x0B, 0x08, 0x06,
149 0x9F, 0xAF, 0x28, 0x05, 0x97, 0xCF, 0x2F, 0x0E,
150 0x02, 0x00, 0x08, 0x06, 0xC0, 0x07, 0x08, 0x00,
151 0xC1, 0x0B, 0x08, 0x06, 0x9F, 0xAF, 0x28, 0x05,
152 0x9F, 0xCF, 0xD6, 0x02, 0x2F, 0x0E, 0x02, 0x00,
153 0x09, 0x06, 0xEF, 0x87, 0x80, 0x00, 0x09, 0x06,
154 0x9F, 0xCF, 0xD6, 0x02, 0xEF, 0x67, 0x7F, 0xFF,
155 0x09, 0x06, 0xE7, 0x67, 0xFF, 0xFD, 0x22, 0xC0,
156 0xE7, 0x67, 0xEF, 0xFF, 0x24, 0xC0, 0xE7, 0x87,
157 0x10, 0x00, 0x28, 0xC0, 0x9F, 0xAF, 0xB8, 0x05,
158 0xE7, 0x87, 0xE0, 0x21, 0x24, 0xC0, 0x9F, 0xAF,
159 0xA8, 0x05, 0xE7, 0x87, 0x08, 0x00, 0x24, 0xC0,
160 0xE7, 0x67, 0xDF, 0xFF, 0x24, 0xC0, 0xC8, 0x07,
161 0x0A, 0x00, 0xC0, 0x07, 0x00, 0x00, 0xC1, 0x07,
162 0x01, 0x00, 0x9F, 0xAF, 0x28, 0x05, 0x9F, 0xAF,
163 0xB8, 0x05, 0xC0, 0x07, 0x9E, 0x00, 0x9F, 0xAF,
164 0x44, 0x05, 0xE7, 0x67, 0xFF, 0xFE, 0x24, 0xC0,
165 0xC0, 0x09, 0x20, 0xC0, 0xE7, 0x87, 0x00, 0x01,
166 0x24, 0xC0, 0xC0, 0x77, 0x00, 0x02, 0x0F, 0xC1,
167 0xE7, 0x67, 0xF7, 0xFF, 0x24, 0xC0, 0xE7, 0x67,
168 0xF7, 0xFF, 0x24, 0xC0, 0xE7, 0x87, 0x08, 0x00,
169 0x24, 0xC0, 0x08, 0xDA, 0x5E, 0xC1, 0xEF, 0x07,
170 0x80, 0x00, 0x09, 0x06, 0x97, 0xCF, 0xEF, 0x07,
171 0x01, 0x00, 0x0A, 0x06, 0x97, 0xCF, 0xEF, 0x07,
172 0x00, 0x00, 0x0B, 0x06, 0xEF, 0x07, 0x00, 0x00,
173 0x0A, 0x06, 0xEF, 0x67, 0x7F, 0xFF, 0x09, 0x06,
174 0xEF, 0x07, 0x00, 0x00, 0x0D, 0x06, 0xE7, 0x67,
175 0xEF, 0xFF, 0x28, 0xC0, 0xE7, 0x67, 0x17, 0xD8,
176 0x24, 0xC0, 0xE7, 0x07, 0x00, 0x00, 0x1E, 0xC0,
177 0xE7, 0x07, 0xFF, 0xFF, 0x22, 0xC0, 0x97, 0xCF,
178 0xC8, 0x07, 0x0E, 0x06, 0x9F, 0xAF, 0xDA, 0x02,
179 0xE7, 0x07, 0x00, 0x00, 0xF2, 0x05, 0xE7, 0x07,
180 0x10, 0x00, 0xF6, 0x05, 0xE7, 0x07, 0x0E, 0x06,
181 0xF4, 0x05, 0xE7, 0x07, 0xD6, 0x02, 0xF8, 0x05,
182 0xC8, 0x07, 0xF2, 0x05, 0xC1, 0x07, 0x00, 0x80,
183 0x50, 0xAF, 0x97, 0xCF, 0x2F, 0x0C, 0x02, 0x00,
184 0x07, 0x06, 0x2F, 0x0C, 0x04, 0x00, 0x06, 0x06,
185 0xE7, 0x07, 0x00, 0x00, 0xF2, 0x05, 0xE7, 0x07,
186 0x10, 0x00, 0xF6, 0x05, 0xE7, 0x07, 0xE2, 0x05,
187 0xF4, 0x05, 0xE7, 0x07, 0xCE, 0x02, 0xF8, 0x05,
188 0xC8, 0x07, 0xF2, 0x05, 0xC1, 0x07, 0x00, 0x80,
189 0x51, 0xAF, 0x97, 0xCF, 0x9F, 0xAF, 0x66, 0x04,
190 0x9F, 0xAF, 0x1A, 0x03, 0x59, 0xAF, 0x97, 0xCF,
191 0xC0, 0x07, 0x0E, 0x00, 0xC1, 0x0B, 0x0C, 0x06,
192 0x41, 0xD1, 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07,
193 0x3C, 0x00, 0x9F, 0xAF, 0x44, 0x05, 0x68, 0x00,
194 0xC0, 0x07, 0x3B, 0x00, 0x9F, 0xAF, 0x44, 0x05,
195 0x6F, 0x00, 0x0C, 0x06, 0x68, 0x00, 0xE0, 0x07,
196 0x04, 0x01, 0xE8, 0x0B, 0x0A, 0x06, 0xE8, 0x07,
197 0x00, 0x00, 0xE0, 0x07, 0x00, 0x02, 0xE0, 0x07,
198 0xEC, 0x01, 0xE0, 0x07, 0xFC, 0xFF, 0x97, 0xCF,
199 0xE7, 0x07, 0xFF, 0xFF, 0xFA, 0x05, 0xEF, 0x07,
200 0x00, 0x00, 0x0B, 0x06, 0xE7, 0x07, 0x0E, 0x06,
201 0x24, 0x01, 0xE7, 0x07, 0x0E, 0x06, 0xFE, 0x05,
202 0xE7, 0x07, 0x40, 0x00, 0x26, 0x01, 0xE7, 0x07,
203 0x40, 0x00, 0x04, 0x06, 0xE7, 0x07, 0x07, 0x00,
204 0x92, 0xC0, 0x97, 0xCF, 0xEF, 0x07, 0x02, 0x00,
205 0x0B, 0x06, 0x9F, 0xAF, 0x78, 0x01, 0xEF, 0x77,
206 0x80, 0x00, 0x07, 0x06, 0x9F, 0xC0, 0x14, 0x04,
207 0xEF, 0x77, 0x01, 0x00, 0x07, 0x06, 0x37, 0xC0,
208 0xEF, 0x77, 0x01, 0x00, 0x0D, 0x06, 0x0F, 0xC1,
209 0xEF, 0x07, 0x01, 0x00, 0x0D, 0x06, 0xC0, 0x07,
210 0x02, 0x00, 0xC1, 0x07, 0x30, 0x00, 0x9F, 0xAF,
211 0x28, 0x05, 0xC0, 0x07, 0x01, 0x00, 0xC1, 0x07,
212 0x02, 0x00, 0x9F, 0xAF, 0x28, 0x05, 0xC8, 0x07,
213 0xFF, 0x4F, 0x9F, 0xAF, 0xA8, 0x05, 0xC0, 0x07,
214 0x38, 0x00, 0x9F, 0xAF, 0x44, 0x05, 0xC1, 0x77,
215 0x03, 0x00, 0x02, 0xC1, 0x08, 0xDA, 0x75, 0xC1,
216 0xC1, 0x77, 0x01, 0x00, 0x0A, 0xC1, 0xC0, 0x07,
217 0x01, 0x00, 0xC1, 0x07, 0x02, 0x00, 0x9F, 0xAF,
218 0x28, 0x05, 0xEF, 0x07, 0x01, 0x00, 0x06, 0x06,
219 0x2C, 0xCF, 0xC0, 0x07, 0x01, 0x00, 0xC1, 0x07,
220 0x04, 0x00, 0x9F, 0xAF, 0x28, 0x05, 0xEF, 0x07,
221 0x00, 0x00, 0x06, 0x06, 0x22, 0xCF, 0xEF, 0x07,
222 0x00, 0x00, 0x0D, 0x06, 0xEF, 0x57, 0x01, 0x00,
223 0x06, 0x06, 0x1B, 0xC0, 0xC0, 0x07, 0x01, 0x00,
224 0xC1, 0x07, 0x01, 0x00, 0x9F, 0xAF, 0x28, 0x05,
225 0xC0, 0x07, 0x02, 0x00, 0xC1, 0x07, 0x30, 0x00,
226 0x9F, 0xAF, 0x28, 0x05, 0xC8, 0x07, 0xFF, 0x4F,
227 0x9F, 0xAF, 0xA8, 0x05, 0xC0, 0x07, 0x38, 0x00,
228 0x9F, 0xAF, 0x44, 0x05, 0xC1, 0x67, 0x03, 0x00,
229 0xC1, 0x57, 0x03, 0x00, 0x02, 0xC0, 0x08, 0xDA,
230 0x73, 0xC1, 0xC0, 0x07, 0x02, 0x00, 0xC1, 0x07,
231 0x12, 0x00, 0xEF, 0x57, 0x00, 0x00, 0x06, 0x06,
232 0x02, 0xC0, 0xC1, 0x07, 0x23, 0x00, 0x9F, 0xAF,
233 0x28, 0x05, 0xC0, 0x07, 0x14, 0x00, 0xC1, 0x0B,
234 0xEA, 0x05, 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07,
235 0x3E, 0x00, 0x9F, 0xAF, 0x0A, 0x05, 0xE7, 0x09,
236 0xE4, 0x05, 0xFA, 0x05, 0x27, 0xD8, 0xFA, 0x05,
237 0xE7, 0x07, 0x0E, 0x06, 0xFC, 0x05, 0xE7, 0x07,
238 0x4E, 0x06, 0x00, 0x06, 0xE7, 0x07, 0x40, 0x00,
239 0x02, 0x06, 0x9F, 0xAF, 0x66, 0x05, 0x9F, 0xAF,
240 0xC6, 0x00, 0x97, 0xCF, 0xC1, 0x0B, 0xE2, 0x05,
241 0x41, 0xD0, 0x01, 0xD2, 0xC1, 0x17, 0x23, 0x00,
242 0x9F, 0xAF, 0xDC, 0x04, 0xC0, 0x07, 0x04, 0x00,
243 0xC1, 0x0B, 0xE3, 0x05, 0x9F, 0xAF, 0x28, 0x05,
244 0xC0, 0x07, 0x06, 0x00, 0xC1, 0x09, 0xE6, 0x05,
245 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07, 0x07, 0x00,
246 0xC1, 0x09, 0xE6, 0x05, 0xC1, 0xD1, 0x9F, 0xAF,
247 0x28, 0x05, 0xC0, 0x07, 0x0B, 0x00, 0xC1, 0x09,
248 0xE8, 0x05, 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07,
249 0x0C, 0x00, 0xC1, 0x09, 0xE8, 0x05, 0xC1, 0xD1,
250 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07, 0x0D, 0x00,
251 0xC1, 0x07, 0x09, 0x00, 0x9F, 0xAF, 0x28, 0x05,
252 0xC0, 0x07, 0x03, 0x00, 0xC1, 0x07, 0x32, 0x00,
253 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07, 0x0F, 0x00,
254 0xC1, 0x07, 0x00, 0x00, 0x9F, 0xAF, 0x28, 0x05,
255 0x97, 0xCF, 0xE7, 0x67, 0xFF, 0xD9, 0x24, 0xC0,
256 0xC8, 0x07, 0x0A, 0x00, 0x40, 0x00, 0xC0, 0x67,
257 0x00, 0x02, 0x27, 0x80, 0x24, 0xC0, 0xE7, 0x87,
258 0x00, 0x04, 0x24, 0xC0, 0xE7, 0x67, 0xFF, 0xF9,
259 0x24, 0xC0, 0x01, 0xD2, 0x08, 0xDA, 0x72, 0xC1,
260 0xE7, 0x87, 0x00, 0x20, 0x24, 0xC0, 0x97, 0xCF,
261 0x27, 0x00, 0x1E, 0xC0, 0xE7, 0x87, 0xFF, 0x00,
262 0x22, 0xC0, 0xE7, 0x67, 0x7F, 0xFF, 0x24, 0xC0,
263 0xE7, 0x87, 0x80, 0x00, 0x24, 0xC0, 0xE7, 0x87,
264 0x80, 0x00, 0x24, 0xC0, 0x97, 0xCF, 0x9F, 0xAF,
265 0x0A, 0x05, 0x67, 0x00, 0x1E, 0xC0, 0xE7, 0x67,
266 0xBF, 0xFF, 0x24, 0xC0, 0xE7, 0x87, 0x40, 0x00,
267 0x24, 0xC0, 0xE7, 0x87, 0x40, 0x00, 0x24, 0xC0,
268 0x97, 0xCF, 0x9F, 0xAF, 0x0A, 0x05, 0xE7, 0x67,
269 0x00, 0xFF, 0x22, 0xC0, 0xE7, 0x67, 0xFF, 0xFE,
270 0x24, 0xC0, 0xE7, 0x67, 0xFF, 0xFE, 0x24, 0xC0,
271 0xC1, 0x09, 0x20, 0xC0, 0xE7, 0x87, 0x00, 0x01,
272 0x24, 0xC0, 0x97, 0xCF, 0xC0, 0x07, 0x40, 0x00,
273 0xC8, 0x09, 0xFC, 0x05, 0xE7, 0x67, 0x00, 0xFF,
274 0x22, 0xC0, 0xE7, 0x67, 0xFF, 0xFE, 0x24, 0xC0,
275 0xE7, 0x67, 0xBF, 0xFF, 0x24, 0xC0, 0xE7, 0x67,
276 0xBF, 0xFF, 0x24, 0xC0, 0x00, 0xDA, 0xE8, 0x09,
277 0x20, 0xC0, 0xE7, 0x87, 0x40, 0x00, 0x24, 0xC0,
278 0xE7, 0x87, 0x40, 0x00, 0x24, 0xC0, 0x00, 0xDA,
279 0xE8, 0x09, 0x20, 0xC0, 0x6D, 0xC1, 0xE7, 0x87,
280 0x00, 0x01, 0x24, 0xC0, 0x97, 0xCF, 0xE7, 0x07,
281 0x32, 0x00, 0x12, 0xC0, 0xE7, 0x77, 0x00, 0x80,
282 0x12, 0xC0, 0x7C, 0xC0, 0x97, 0xCF, 0xE7, 0x07,
283 0x20, 0x4E, 0x12, 0xC0, 0xE7, 0x77, 0x00, 0x80,
284 0x12, 0xC0, 0x7C, 0xC0, 0x97, 0xCF, 0x09, 0x02,
285 0x19, 0x00, 0x01, 0x01, 0x00, 0x80, 0x96, 0x09,
286 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
287 0x07, 0x05, 0x81, 0x02, 0x40, 0x00, 0x00, 0x00,
288 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
290 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
295 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
296 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
297 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
298 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
299 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
300 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
302 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
304 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
305 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310};
311
312static unsigned char setup5[] = {
313 0xB6, 0xC3, 0x2F, 0x01, 0x03, 0x64, 0x0E, 0x00,
314 0x14, 0x00, 0x1A, 0x00, 0x20, 0x00, 0x26, 0x00,
315 0x4A, 0x00, 0x64, 0x00, 0x6A, 0x00, 0x92, 0x00,
316 0x9A, 0x00, 0xA0, 0x00, 0xB2, 0x00, 0xB8, 0x00,
317 0xBE, 0x00, 0xC2, 0x00, 0xC8, 0x00, 0xCE, 0x00,
318 0xDC, 0x00, 0xDA, 0x00, 0xE2, 0x00, 0xE0, 0x00,
319 0xE8, 0x00, 0xE6, 0x00, 0xEE, 0x00, 0xEC, 0x00,
320 0xF2, 0x00, 0xF8, 0x00, 0x02, 0x01, 0x0A, 0x01,
321 0x0E, 0x01, 0x12, 0x01, 0x1E, 0x01, 0x22, 0x01,
322 0x28, 0x01, 0x2C, 0x01, 0x32, 0x01, 0x36, 0x01,
323 0x44, 0x01, 0x50, 0x01, 0x5E, 0x01, 0x72, 0x01,
324 0x76, 0x01, 0x7A, 0x01, 0x80, 0x01, 0x88, 0x01,
325 0x8C, 0x01, 0x94, 0x01, 0x9C, 0x01, 0xA0, 0x01,
326 0xA4, 0x01, 0xAA, 0x01, 0xB0, 0x01, 0xB4, 0x01,
327 0xBA, 0x01, 0xD0, 0x01, 0xDA, 0x01, 0xF6, 0x01,
328 0xFA, 0x01, 0x02, 0x02, 0x34, 0x02, 0x3C, 0x02,
329 0x44, 0x02, 0x4A, 0x02, 0x50, 0x02, 0x56, 0x02,
330 0x74, 0x02, 0x78, 0x02, 0x7E, 0x02, 0x84, 0x02,
331 0x8A, 0x02, 0x88, 0x02, 0x90, 0x02, 0x8E, 0x02,
332 0x94, 0x02, 0xA2, 0x02, 0xA8, 0x02, 0xAE, 0x02,
333 0xB4, 0x02, 0xBA, 0x02, 0xB8, 0x02, 0xC0, 0x02,
334 0xBE, 0x02, 0xC4, 0x02, 0xD0, 0x02, 0xD4, 0x02,
335 0xE0, 0x02, 0xE6, 0x02, 0xEE, 0x02, 0xF8, 0x02,
336 0xFC, 0x02, 0x06, 0x03, 0x1E, 0x03, 0x24, 0x03,
337 0x28, 0x03, 0x30, 0x03, 0x2E, 0x03, 0x3C, 0x03,
338 0x4A, 0x03, 0x4E, 0x03, 0x54, 0x03, 0x58, 0x03,
339 0x5E, 0x03, 0x66, 0x03, 0x6E, 0x03, 0x7A, 0x03,
340 0x86, 0x03, 0x8E, 0x03, 0x96, 0x03, 0xB2, 0x03,
341 0xB8, 0x03, 0xC6, 0x03, 0xCC, 0x03, 0xD4, 0x03,
342 0xDA, 0x03, 0xE8, 0x03, 0xF4, 0x03, 0xFC, 0x03,
343 0x04, 0x04, 0x20, 0x04, 0x2A, 0x04, 0x32, 0x04,
344 0x36, 0x04, 0x3E, 0x04, 0x44, 0x04, 0x42, 0x04,
345 0x48, 0x04, 0x4E, 0x04, 0x4C, 0x04, 0x54, 0x04,
346 0x52, 0x04, 0x5A, 0x04, 0x5E, 0x04, 0x62, 0x04,
347 0x68, 0x04, 0x74, 0x04, 0x7C, 0x04, 0x80, 0x04,
348 0x88, 0x04, 0x8C, 0x04, 0x94, 0x04, 0x9A, 0x04,
349 0xA2, 0x04, 0xA6, 0x04, 0xAE, 0x04, 0xB4, 0x04,
350 0xC0, 0x04, 0xCC, 0x04, 0xD8, 0x04, 0x2A, 0x05,
351 0x46, 0x05, 0x6C, 0x05, 0x00, 0x00
352};
353
354/* rvmalloc / rvfree copied from usbvideo.c
355 *
356 * Not sure why these are not yet non-statics which I can reference through
357 * usbvideo.h the same as it is in 2.4.20. I bet this will get fixed sometime
358 * in the future.
359 *
360*/
361static void *rvmalloc(unsigned long size)
362{
363 void *mem;
364 unsigned long adr;
365
366 size = PAGE_ALIGN(size);
367 mem = vmalloc_32(size);
368 if (!mem)
369 return NULL;
370
371 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
372 adr = (unsigned long) mem;
373 while (size > 0) {
374 SetPageReserved(vmalloc_to_page((void *)adr));
375 adr += PAGE_SIZE;
376 size -= PAGE_SIZE;
377 }
378
379 return mem;
380}
381
382static void rvfree(void *mem, unsigned long size)
383{
384 unsigned long adr;
385
386 if (!mem)
387 return;
388
389 adr = (unsigned long) mem;
390 while ((long) size > 0) {
391 ClearPageReserved(vmalloc_to_page((void *)adr));
392 adr += PAGE_SIZE;
393 size -= PAGE_SIZE;
394 }
395 vfree(mem);
396}
397
398struct vicam_camera {
399 u16 shutter_speed; // capture shutter speed
400 u16 gain; // capture gain
401
402 u8 *raw_image; // raw data captured from the camera
403 u8 *framebuf; // processed data in RGB24 format
404 u8 *cntrlbuf; // area used to send control msgs
405
406 struct video_device vdev; // v4l video device
407 struct usb_device *udev; // usb device
408
409 /* guard against simultaneous accesses to the camera */
410 struct semaphore cam_lock;
411
412 int is_initialized;
413 u8 open_count;
414 u8 bulkEndpoint;
415 int needsDummyRead;
416
417#if defined(CONFIG_VIDEO_PROC_FS)
418 struct proc_dir_entry *proc_dir;
419#endif
420
421};
422
423static int vicam_probe( struct usb_interface *intf, const struct usb_device_id *id);
424static void vicam_disconnect(struct usb_interface *intf);
425static void read_frame(struct vicam_camera *cam, int framenum);
426static void vicam_decode_color(const u8 *, u8 *);
427
428static int __send_control_msg(struct vicam_camera *cam,
429 u8 request,
430 u16 value,
431 u16 index,
432 unsigned char *cp,
433 u16 size)
434{
435 int status;
436
437 /* cp must be memory that has been allocated by kmalloc */
438
439 status = usb_control_msg(cam->udev,
440 usb_sndctrlpipe(cam->udev, 0),
441 request,
442 USB_DIR_OUT | USB_TYPE_VENDOR |
443 USB_RECIP_DEVICE, value, index,
444 cp, size, 1000);
445
446 status = min(status, 0);
447
448 if (status < 0) {
449 printk(KERN_INFO "Failed sending control message, error %d.\n",
450 status);
451 }
452
453 return status;
454}
455
456static int send_control_msg(struct vicam_camera *cam,
457 u8 request,
458 u16 value,
459 u16 index,
460 unsigned char *cp,
461 u16 size)
462{
463 int status = -ENODEV;
464 down(&cam->cam_lock);
465 if (cam->udev) {
466 status = __send_control_msg(cam, request, value,
467 index, cp, size);
468 }
469 up(&cam->cam_lock);
470 return status;
471}
472static int
473initialize_camera(struct vicam_camera *cam)
474{
475 const struct {
476 u8 *data;
477 u32 size;
478 } firmware[] = {
479 { .data = setup1, .size = sizeof(setup1) },
480 { .data = setup2, .size = sizeof(setup2) },
481 { .data = setup3, .size = sizeof(setup3) },
482 { .data = setup4, .size = sizeof(setup4) },
483 { .data = setup5, .size = sizeof(setup5) },
484 { .data = setup3, .size = sizeof(setup3) },
485 { .data = NULL, .size = 0 }
486 };
487
488 int err, i;
489
490 for (i = 0, err = 0; firmware[i].data && !err; i++) {
491 memcpy(cam->cntrlbuf, firmware[i].data, firmware[i].size);
492
493 err = send_control_msg(cam, 0xff, 0, 0,
494 cam->cntrlbuf, firmware[i].size);
495 }
496
497 return err;
498}
499
500static int
501set_camera_power(struct vicam_camera *cam, int state)
502{
503 int status;
504
505 if ((status = send_control_msg(cam, 0x50, state, 0, NULL, 0)) < 0)
506 return status;
507
508 if (state) {
509 send_control_msg(cam, 0x55, 1, 0, NULL, 0);
510 }
511
512 return 0;
513}
514
515static int
516vicam_ioctl(struct inode *inode, struct file *file, unsigned int ioctlnr, unsigned long arg)
517{
518 void __user *user_arg = (void __user *)arg;
519 struct vicam_camera *cam = file->private_data;
520 int retval = 0;
521
522 if (!cam)
523 return -ENODEV;
524
525 switch (ioctlnr) {
526 /* query capabilities */
527 case VIDIOCGCAP:
528 {
529 struct video_capability b;
530
531 DBG("VIDIOCGCAP\n");
532 memset(&b, 0, sizeof(b));
533 strcpy(b.name, "ViCam-based Camera");
534 b.type = VID_TYPE_CAPTURE;
535 b.channels = 1;
536 b.audios = 0;
537 b.maxwidth = 320; /* VIDEOSIZE_CIF */
538 b.maxheight = 240;
539 b.minwidth = 320; /* VIDEOSIZE_48_48 */
540 b.minheight = 240;
541
542 if (copy_to_user(user_arg, &b, sizeof(b)))
543 retval = -EFAULT;
544
545 break;
546 }
547 /* get/set video source - we are a camera and nothing else */
548 case VIDIOCGCHAN:
549 {
550 struct video_channel v;
551
552 DBG("VIDIOCGCHAN\n");
553 if (copy_from_user(&v, user_arg, sizeof(v))) {
554 retval = -EFAULT;
555 break;
556 }
557 if (v.channel != 0) {
558 retval = -EINVAL;
559 break;
560 }
561
562 v.channel = 0;
563 strcpy(v.name, "Camera");
564 v.tuners = 0;
565 v.flags = 0;
566 v.type = VIDEO_TYPE_CAMERA;
567 v.norm = 0;
568
569 if (copy_to_user(user_arg, &v, sizeof(v)))
570 retval = -EFAULT;
571 break;
572 }
573
574 case VIDIOCSCHAN:
575 {
576 int v;
577
578 if (copy_from_user(&v, user_arg, sizeof(v)))
579 retval = -EFAULT;
580 DBG("VIDIOCSCHAN %d\n", v);
581
582 if (retval == 0 && v != 0)
583 retval = -EINVAL;
584
585 break;
586 }
587
588 /* image properties */
589 case VIDIOCGPICT:
590 {
591 struct video_picture vp;
592 DBG("VIDIOCGPICT\n");
593 memset(&vp, 0, sizeof (struct video_picture));
594 vp.brightness = cam->gain << 8;
595 vp.depth = 24;
596 vp.palette = VIDEO_PALETTE_RGB24;
597 if (copy_to_user(user_arg, &vp, sizeof (struct video_picture)))
598 retval = -EFAULT;
599 break;
600 }
601
602 case VIDIOCSPICT:
603 {
604 struct video_picture vp;
605
606 if (copy_from_user(&vp, user_arg, sizeof(vp))) {
607 retval = -EFAULT;
608 break;
609 }
610
611 DBG("VIDIOCSPICT depth = %d, pal = %d\n", vp.depth,
612 vp.palette);
613
614 cam->gain = vp.brightness >> 8;
615
616 if (vp.depth != 24
617 || vp.palette != VIDEO_PALETTE_RGB24)
618 retval = -EINVAL;
619
620 break;
621 }
622
623 /* get/set capture window */
624 case VIDIOCGWIN:
625 {
626 struct video_window vw;
627 vw.x = 0;
628 vw.y = 0;
629 vw.width = 320;
630 vw.height = 240;
631 vw.chromakey = 0;
632 vw.flags = 0;
633 vw.clips = NULL;
634 vw.clipcount = 0;
635
636 DBG("VIDIOCGWIN\n");
637
638 if (copy_to_user(user_arg, (void *)&vw, sizeof(vw)))
639 retval = -EFAULT;
640
641 // I'm not sure what the deal with a capture window is, it is very poorly described
642 // in the doc. So I won't support it now.
643 break;
644 }
645
646 case VIDIOCSWIN:
647 {
648
649 struct video_window vw;
650
651 if (copy_from_user(&vw, user_arg, sizeof(vw))) {
652 retval = -EFAULT;
653 break;
654 }
655
656 DBG("VIDIOCSWIN %d x %d\n", vw.width, vw.height);
657
658 if ( vw.width != 320 || vw.height != 240 )
659 retval = -EFAULT;
660
661 break;
662 }
663
664 /* mmap interface */
665 case VIDIOCGMBUF:
666 {
667 struct video_mbuf vm;
668 int i;
669
670 DBG("VIDIOCGMBUF\n");
671 memset(&vm, 0, sizeof (vm));
672 vm.size =
673 VICAM_MAX_FRAME_SIZE * VICAM_FRAMES;
674 vm.frames = VICAM_FRAMES;
675 for (i = 0; i < VICAM_FRAMES; i++)
676 vm.offsets[i] = VICAM_MAX_FRAME_SIZE * i;
677
678 if (copy_to_user(user_arg, (void *)&vm, sizeof(vm)))
679 retval = -EFAULT;
680
681 break;
682 }
683
684 case VIDIOCMCAPTURE:
685 {
686 struct video_mmap vm;
687 // int video_size;
688
689 if (copy_from_user((void *)&vm, user_arg, sizeof(vm))) {
690 retval = -EFAULT;
691 break;
692 }
693
694 DBG("VIDIOCMCAPTURE frame=%d, height=%d, width=%d, format=%d.\n",vm.frame,vm.width,vm.height,vm.format);
695
696 if ( vm.frame >= VICAM_FRAMES || vm.format != VIDEO_PALETTE_RGB24 )
697 retval = -EINVAL;
698
699 // in theory right here we'd start the image capturing
700 // (fill in a bulk urb and submit it asynchronously)
701 //
702 // Instead we're going to do a total hack job for now and
703 // retrieve the frame in VIDIOCSYNC
704
705 break;
706 }
707
708 case VIDIOCSYNC:
709 {
710 int frame;
711
712 if (copy_from_user((void *)&frame, user_arg, sizeof(int))) {
713 retval = -EFAULT;
714 break;
715 }
716 DBG("VIDIOCSYNC: %d\n", frame);
717
718 read_frame(cam, frame);
719 vicam_decode_color(cam->raw_image,
720 cam->framebuf +
721 frame * VICAM_MAX_FRAME_SIZE );
722
723 break;
724 }
725
726 /* pointless to implement overlay with this camera */
727 case VIDIOCCAPTURE:
728 case VIDIOCGFBUF:
729 case VIDIOCSFBUF:
730 case VIDIOCKEY:
731 retval = -EINVAL;
732 break;
733
734 /* tuner interface - we have none */
735 case VIDIOCGTUNER:
736 case VIDIOCSTUNER:
737 case VIDIOCGFREQ:
738 case VIDIOCSFREQ:
739 retval = -EINVAL;
740 break;
741
742 /* audio interface - we have none */
743 case VIDIOCGAUDIO:
744 case VIDIOCSAUDIO:
745 retval = -EINVAL;
746 break;
747 default:
748 retval = -ENOIOCTLCMD;
749 break;
750 }
751
752 return retval;
753}
754
755static int
756vicam_open(struct inode *inode, struct file *file)
757{
758 struct video_device *dev = video_devdata(file);
759 struct vicam_camera *cam =
760 (struct vicam_camera *) dev->priv;
761 DBG("open\n");
762
763 if (!cam) {
764 printk(KERN_ERR
765 "vicam video_device improperly initialized");
766 }
767
768 /* the videodev_lock held above us protects us from
769 * simultaneous opens...for now. we probably shouldn't
770 * rely on this fact forever.
771 */
772
773 if (cam->open_count > 0) {
774 printk(KERN_INFO
775 "vicam_open called on already opened camera");
776 return -EBUSY;
777 }
778
779 cam->raw_image = kmalloc(VICAM_MAX_READ_SIZE, GFP_KERNEL);
780 if (!cam->raw_image) {
781 return -ENOMEM;
782 }
783
784 cam->framebuf = rvmalloc(VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
785 if (!cam->framebuf) {
786 kfree(cam->raw_image);
787 return -ENOMEM;
788 }
789
790 cam->cntrlbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
791 if (!cam->cntrlbuf) {
792 kfree(cam->raw_image);
793 rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
794 return -ENOMEM;
795 }
796
797 // First upload firmware, then turn the camera on
798
799 if (!cam->is_initialized) {
800 initialize_camera(cam);
801
802 cam->is_initialized = 1;
803 }
804
805 set_camera_power(cam, 1);
806
807 cam->needsDummyRead = 1;
808 cam->open_count++;
809
810 file->private_data = cam;
811
812 return 0;
813}
814
815static int
816vicam_close(struct inode *inode, struct file *file)
817{
818 struct vicam_camera *cam = file->private_data;
819 int open_count;
820 struct usb_device *udev;
821
822 DBG("close\n");
823
824 /* it's not the end of the world if
825 * we fail to turn the camera off.
826 */
827
828 set_camera_power(cam, 0);
829
830 kfree(cam->raw_image);
831 rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
832 kfree(cam->cntrlbuf);
833
834 down(&cam->cam_lock);
835
836 cam->open_count--;
837 open_count = cam->open_count;
838 udev = cam->udev;
839
840 up(&cam->cam_lock);
841
842 if (!open_count && !udev) {
843 kfree(cam);
844 }
845
846 return 0;
847}
848
849static void vicam_decode_color(const u8 *data, u8 *rgb)
850{
851 /* vicam_decode_color - Convert from Vicam Y-Cr-Cb to RGB
852 * Copyright (C) 2002 Monroe Williams (monroe@pobox.com)
853 */
854
855 int i, prevY, nextY;
856
857 prevY = 512;
858 nextY = 512;
859
860 data += VICAM_HEADER_SIZE;
861
862 for( i = 0; i < 240; i++, data += 512 ) {
863 const int y = ( i * 242 ) / 240;
864
865 int j, prevX, nextX;
866 int Y, Cr, Cb;
867
868 if ( y == 242 - 1 ) {
869 nextY = -512;
870 }
871
872 prevX = 1;
873 nextX = 1;
874
875 for ( j = 0; j < 320; j++, rgb += 3 ) {
876 const int x = ( j * 512 ) / 320;
877 const u8 * const src = &data[x];
878
879 if ( x == 512 - 1 ) {
880 nextX = -1;
881 }
882
883 Cr = ( src[prevX] - src[0] ) +
884 ( src[nextX] - src[0] );
885 Cr /= 2;
886
887 Cb = ( src[prevY] - src[prevX + prevY] ) +
888 ( src[prevY] - src[nextX + prevY] ) +
889 ( src[nextY] - src[prevX + nextY] ) +
890 ( src[nextY] - src[nextX + nextY] );
891 Cb /= 4;
892
893 Y = 1160 * ( src[0] + ( Cr / 2 ) - 16 );
894
895 if ( i & 1 ) {
896 int Ct = Cr;
897 Cr = Cb;
898 Cb = Ct;
899 }
900
901 if ( ( x ^ i ) & 1 ) {
902 Cr = -Cr;
903 Cb = -Cb;
904 }
905
906 rgb[0] = clamp( ( ( Y + ( 2017 * Cb ) ) +
907 500 ) / 900, 0, 255 );
908 rgb[1] = clamp( ( ( Y - ( 392 * Cb ) -
909 ( 813 * Cr ) ) +
910 500 ) / 1000, 0, 255 );
911 rgb[2] = clamp( ( ( Y + ( 1594 * Cr ) ) +
912 500 ) / 1300, 0, 255 );
913
914 prevX = -1;
915 }
916
917 prevY = -512;
918 }
919}
920
921static void
922read_frame(struct vicam_camera *cam, int framenum)
923{
924 unsigned char *request = cam->cntrlbuf;
925 int realShutter;
926 int n;
927 int actual_length;
928
929 if (cam->needsDummyRead) {
930 cam->needsDummyRead = 0;
931 read_frame(cam, framenum);
932 }
933
934 memset(request, 0, 16);
935 request[0] = cam->gain; // 0 = 0% gain, FF = 100% gain
936
937 request[1] = 0; // 512x242 capture
938
939 request[2] = 0x90; // the function of these two bytes
940 request[3] = 0x07; // is not yet understood
941
942 if (cam->shutter_speed > 60) {
943 // Short exposure
944 realShutter =
945 ((-15631900 / cam->shutter_speed) + 260533) / 1000;
946 request[4] = realShutter & 0xFF;
947 request[5] = (realShutter >> 8) & 0xFF;
948 request[6] = 0x03;
949 request[7] = 0x01;
950 } else {
951 // Long exposure
952 realShutter = 15600 / cam->shutter_speed - 1;
953 request[4] = 0;
954 request[5] = 0;
955 request[6] = realShutter & 0xFF;
956 request[7] = realShutter >> 8;
957 }
958
959 // Per John Markus Bjørndalen, byte at index 8 causes problems if it isn't 0
960 request[8] = 0;
961 // bytes 9-15 do not seem to affect exposure or image quality
962
963 down(&cam->cam_lock);
964
965 if (!cam->udev) {
966 goto done;
967 }
968
969 n = __send_control_msg(cam, 0x51, 0x80, 0, request, 16);
970
971 if (n < 0) {
972 printk(KERN_ERR
973 " Problem sending frame capture control message");
974 goto done;
975 }
976
977 n = usb_bulk_msg(cam->udev,
978 usb_rcvbulkpipe(cam->udev, cam->bulkEndpoint),
979 cam->raw_image,
980 512 * 242 + 128, &actual_length, 10000);
981
982 if (n < 0) {
983 printk(KERN_ERR "Problem during bulk read of frame data: %d\n",
984 n);
985 }
986
987 done:
988 up(&cam->cam_lock);
989}
990
991static ssize_t
992vicam_read( struct file *file, char __user *buf, size_t count, loff_t *ppos )
993{
994 struct vicam_camera *cam = file->private_data;
995
996 DBG("read %d bytes.\n", (int) count);
997
998 if (*ppos >= VICAM_MAX_FRAME_SIZE) {
999 *ppos = 0;
1000 return 0;
1001 }
1002
1003 if (*ppos == 0) {
1004 read_frame(cam, 0);
1005 vicam_decode_color(cam->raw_image,
1006 cam->framebuf +
1007 0 * VICAM_MAX_FRAME_SIZE);
1008 }
1009
1010 count = min_t(size_t, count, VICAM_MAX_FRAME_SIZE - *ppos);
1011
1012 if (copy_to_user(buf, &cam->framebuf[*ppos], count)) {
1013 count = -EFAULT;
1014 } else {
1015 *ppos += count;
1016 }
1017
1018 if (count == VICAM_MAX_FRAME_SIZE) {
1019 *ppos = 0;
1020 }
1021
1022 return count;
1023}
1024
1025
1026static int
1027vicam_mmap(struct file *file, struct vm_area_struct *vma)
1028{
1029 // TODO: allocate the raw frame buffer if necessary
1030 unsigned long page, pos;
1031 unsigned long start = vma->vm_start;
1032 unsigned long size = vma->vm_end-vma->vm_start;
1033 struct vicam_camera *cam = file->private_data;
1034
1035 if (!cam)
1036 return -ENODEV;
1037
1038 DBG("vicam_mmap: %ld\n", size);
1039
1040 /* We let mmap allocate as much as it wants because Linux was adding 2048 bytes
1041 * to the size the application requested for mmap and it was screwing apps up.
1042 if (size > VICAM_FRAMES*VICAM_MAX_FRAME_SIZE)
1043 return -EINVAL;
1044 */
1045
1046 pos = (unsigned long)cam->framebuf;
1047 while (size > 0) {
1048 page = vmalloc_to_pfn((void *)pos);
1049 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1050 return -EAGAIN;
1051
1052 start += PAGE_SIZE;
1053 pos += PAGE_SIZE;
1054 if (size > PAGE_SIZE)
1055 size -= PAGE_SIZE;
1056 else
1057 size = 0;
1058 }
1059
1060 return 0;
1061}
1062
1063#if defined(CONFIG_VIDEO_PROC_FS)
1064
1065static struct proc_dir_entry *vicam_proc_root = NULL;
1066
1067static int vicam_read_helper(char *page, char **start, off_t off,
1068 int count, int *eof, int value)
1069{
1070 char *out = page;
1071 int len;
1072
1073 out += sprintf(out, "%d",value);
1074
1075 len = out - page;
1076 len -= off;
1077 if (len < count) {
1078 *eof = 1;
1079 if (len <= 0)
1080 return 0;
1081 } else
1082 len = count;
1083
1084 *start = page + off;
1085 return len;
1086}
1087
1088static int vicam_read_proc_shutter(char *page, char **start, off_t off,
1089 int count, int *eof, void *data)
1090{
1091 return vicam_read_helper(page,start,off,count,eof,
1092 ((struct vicam_camera *)data)->shutter_speed);
1093}
1094
1095static int vicam_read_proc_gain(char *page, char **start, off_t off,
1096 int count, int *eof, void *data)
1097{
1098 return vicam_read_helper(page,start,off,count,eof,
1099 ((struct vicam_camera *)data)->gain);
1100}
1101
1102static int
1103vicam_write_proc_shutter(struct file *file, const char *buffer,
1104 unsigned long count, void *data)
1105{
1106 u16 stmp;
1107 char kbuf[8];
1108 struct vicam_camera *cam = (struct vicam_camera *) data;
1109
1110 if (count > 6)
1111 return -EINVAL;
1112
1113 if (copy_from_user(kbuf, buffer, count))
1114 return -EFAULT;
1115
1116 stmp = (u16) simple_strtoul(kbuf, NULL, 10);
1117 if (stmp < 4 || stmp > 32000)
1118 return -EINVAL;
1119
1120 cam->shutter_speed = stmp;
1121
1122 return count;
1123}
1124
1125static int
1126vicam_write_proc_gain(struct file *file, const char *buffer,
1127 unsigned long count, void *data)
1128{
1129 u16 gtmp;
1130 char kbuf[8];
1131
1132 struct vicam_camera *cam = (struct vicam_camera *) data;
1133
1134 if (count > 4)
1135 return -EINVAL;
1136
1137 if (copy_from_user(kbuf, buffer, count))
1138 return -EFAULT;
1139
1140 gtmp = (u16) simple_strtoul(kbuf, NULL, 10);
1141 if (gtmp > 255)
1142 return -EINVAL;
1143 cam->gain = gtmp;
1144
1145 return count;
1146}
1147
1148static void
1149vicam_create_proc_root(void)
1150{
1151 vicam_proc_root = proc_mkdir("video/vicam", NULL);
1152
1153 if (vicam_proc_root)
1154 vicam_proc_root->owner = THIS_MODULE;
1155 else
1156 printk(KERN_ERR
1157 "could not create /proc entry for vicam!");
1158}
1159
1160static void
1161vicam_destroy_proc_root(void)
1162{
1163 if (vicam_proc_root)
1164 remove_proc_entry("video/vicam", 0);
1165}
1166
1167static void
1168vicam_create_proc_entry(struct vicam_camera *cam)
1169{
1170 char name[64];
1171 struct proc_dir_entry *ent;
1172
1173 DBG(KERN_INFO "vicam: creating proc entry\n");
1174
1175 if (!vicam_proc_root || !cam) {
1176 printk(KERN_INFO
1177 "vicam: could not create proc entry, %s pointer is null.\n",
1178 (!cam ? "camera" : "root"));
1179 return;
1180 }
1181
1182 sprintf(name, "video%d", cam->vdev.minor);
1183
1184 cam->proc_dir = proc_mkdir(name, vicam_proc_root);
1185
1186 if ( !cam->proc_dir )
1187 return; // FIXME: We should probably return an error here
1188
1189 ent = create_proc_entry("shutter", S_IFREG | S_IRUGO | S_IWUSR,
1190 cam->proc_dir);
1191 if (ent) {
1192 ent->data = cam;
1193 ent->read_proc = vicam_read_proc_shutter;
1194 ent->write_proc = vicam_write_proc_shutter;
1195 ent->size = 64;
1196 }
1197
1198 ent = create_proc_entry("gain", S_IFREG | S_IRUGO | S_IWUSR,
1199 cam->proc_dir);
1200 if (ent) {
1201 ent->data = cam;
1202 ent->read_proc = vicam_read_proc_gain;
1203 ent->write_proc = vicam_write_proc_gain;
1204 ent->size = 64;
1205 }
1206}
1207
1208static void
1209vicam_destroy_proc_entry(void *ptr)
1210{
1211 struct vicam_camera *cam = (struct vicam_camera *) ptr;
1212 char name[16];
1213
1214 if ( !cam->proc_dir )
1215 return;
1216
1217 sprintf(name, "video%d", cam->vdev.minor);
1218 remove_proc_entry("shutter", cam->proc_dir);
1219 remove_proc_entry("gain", cam->proc_dir);
1220 remove_proc_entry(name,vicam_proc_root);
1221 cam->proc_dir = NULL;
1222
1223}
1224
1225#else
1226static inline void vicam_create_proc_root(void) { }
1227static inline void vicam_destroy_proc_root(void) { }
1228static inline void vicam_create_proc_entry(struct vicam_camera *cam) { }
1229static inline void vicam_destroy_proc_entry(void *ptr) { }
1230#endif
1231
1232static struct file_operations vicam_fops = {
1233 .owner = THIS_MODULE,
1234 .open = vicam_open,
1235 .release = vicam_close,
1236 .read = vicam_read,
1237 .mmap = vicam_mmap,
1238 .ioctl = vicam_ioctl,
1239 .compat_ioctl = v4l_compat_ioctl32,
1240 .llseek = no_llseek,
1241};
1242
1243static struct video_device vicam_template = {
1244 .owner = THIS_MODULE,
1245 .name = "ViCam-based USB Camera",
1246 .type = VID_TYPE_CAPTURE,
1247 .hardware = VID_HARDWARE_VICAM,
1248 .fops = &vicam_fops,
1249 .minor = -1,
1250};
1251
1252/* table of devices that work with this driver */
1253static struct usb_device_id vicam_table[] = {
1254 {USB_DEVICE(USB_VICAM_VENDOR_ID, USB_VICAM_PRODUCT_ID)},
1255 {} /* Terminating entry */
1256};
1257
1258MODULE_DEVICE_TABLE(usb, vicam_table);
1259
1260static struct usb_driver vicam_driver = {
1261 .name = "vicam",
1262 .probe = vicam_probe,
1263 .disconnect = vicam_disconnect,
1264 .id_table = vicam_table
1265};
1266
1267/**
1268 * vicam_probe
1269 * @intf: the interface
1270 * @id: the device id
1271 *
1272 * Called by the usb core when a new device is connected that it thinks
1273 * this driver might be interested in.
1274 */
1275static int
1276vicam_probe( struct usb_interface *intf, const struct usb_device_id *id)
1277{
1278 struct usb_device *dev = interface_to_usbdev(intf);
1279 int bulkEndpoint = 0;
1280 const struct usb_host_interface *interface;
1281 const struct usb_endpoint_descriptor *endpoint;
1282 struct vicam_camera *cam;
1283
1284 printk(KERN_INFO "ViCam based webcam connected\n");
1285
1286 interface = intf->cur_altsetting;
1287
1288 DBG(KERN_DEBUG "Interface %d. has %u. endpoints!\n",
1289 interface->desc.bInterfaceNumber, (unsigned) (interface->desc.bNumEndpoints));
1290 endpoint = &interface->endpoint[0].desc;
1291
1292 if ((endpoint->bEndpointAddress & 0x80) &&
1293 ((endpoint->bmAttributes & 3) == 0x02)) {
1294 /* we found a bulk in endpoint */
1295 bulkEndpoint = endpoint->bEndpointAddress;
1296 } else {
1297 printk(KERN_ERR
1298 "No bulk in endpoint was found ?! (this is bad)\n");
1299 }
1300
1301 if ((cam =
1302 kmalloc(sizeof (struct vicam_camera), GFP_KERNEL)) == NULL) {
1303 printk(KERN_WARNING
1304 "could not allocate kernel memory for vicam_camera struct\n");
1305 return -ENOMEM;
1306 }
1307
1308 memset(cam, 0, sizeof (struct vicam_camera));
1309
1310 cam->shutter_speed = 15;
1311
1312 init_MUTEX(&cam->cam_lock);
1313
1314 memcpy(&cam->vdev, &vicam_template,
1315 sizeof (vicam_template));
1316 cam->vdev.priv = cam; // sort of a reverse mapping for those functions that get vdev only
1317
1318 cam->udev = dev;
1319 cam->bulkEndpoint = bulkEndpoint;
1320
1321 if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1) == -1) {
1322 kfree(cam);
1323 printk(KERN_WARNING "video_register_device failed\n");
1324 return -EIO;
1325 }
1326
1327 vicam_create_proc_entry(cam);
1328
1329 printk(KERN_INFO "ViCam webcam driver now controlling video device %d\n",cam->vdev.minor);
1330
1331 usb_set_intfdata (intf, cam);
1332
1333 return 0;
1334}
1335
1336static void
1337vicam_disconnect(struct usb_interface *intf)
1338{
1339 int open_count;
1340 struct vicam_camera *cam = usb_get_intfdata (intf);
1341 usb_set_intfdata (intf, NULL);
1342
1343 /* we must unregister the device before taking its
1344 * cam_lock. This is because the video open call
1345 * holds the same lock as video unregister. if we
1346 * unregister inside of the cam_lock and open also
1347 * uses the cam_lock, we get deadlock.
1348 */
1349
1350 video_unregister_device(&cam->vdev);
1351
1352 /* stop the camera from being used */
1353
1354 down(&cam->cam_lock);
1355
1356 /* mark the camera as gone */
1357
1358 cam->udev = NULL;
1359
1360 vicam_destroy_proc_entry(cam);
1361
1362 /* the only thing left to do is synchronize with
1363 * our close/release function on who should release
1364 * the camera memory. if there are any users using the
1365 * camera, it's their job. if there are no users,
1366 * it's ours.
1367 */
1368
1369 open_count = cam->open_count;
1370
1371 up(&cam->cam_lock);
1372
1373 if (!open_count) {
1374 kfree(cam);
1375 }
1376
1377 printk(KERN_DEBUG "ViCam-based WebCam disconnected\n");
1378}
1379
1380/*
1381 */
1382static int __init
1383usb_vicam_init(void)
1384{
1385 int retval;
1386 DBG(KERN_INFO "ViCam-based WebCam driver startup\n");
1387 vicam_create_proc_root();
1388 retval = usb_register(&vicam_driver);
1389 if (retval)
1390 printk(KERN_WARNING "usb_register failed!\n");
1391 return retval;
1392}
1393
1394static void __exit
1395usb_vicam_exit(void)
1396{
1397 DBG(KERN_INFO
1398 "ViCam-based WebCam driver shutdown\n");
1399
1400 usb_deregister(&vicam_driver);
1401 vicam_destroy_proc_root();
1402}
1403
1404module_init(usb_vicam_init);
1405module_exit(usb_vicam_exit);
1406
1407MODULE_AUTHOR(DRIVER_AUTHOR);
1408MODULE_DESCRIPTION(DRIVER_DESC);
1409MODULE_LICENSE("GPL");
diff --git a/drivers/usb/media/w9968cf.c b/drivers/usb/media/w9968cf.c
deleted file mode 100644
index 9937fc64c8bf..000000000000
--- a/drivers/usb/media/w9968cf.c
+++ /dev/null
@@ -1,3689 +0,0 @@
1/***************************************************************************
2 * Video4Linux driver for W996[87]CF JPEG USB Dual Mode Camera Chip. *
3 * *
4 * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * - Memory management code from bttv driver by Ralph Metzler, *
7 * Marcus Metzler and Gerd Knorr. *
8 * - I2C interface to kernel, high-level image sensor control routines and *
9 * some symbolic names from OV511 driver by Mark W. McClelland. *
10 * - Low-level I2C fast write function by Piotr Czerczak. *
11 * - Low-level I2C read function by Frederic Jouault. *
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
22 * *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the Free Software *
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
26 ***************************************************************************/
27
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/kmod.h>
31#include <linux/init.h>
32#include <linux/fs.h>
33#include <linux/vmalloc.h>
34#include <linux/slab.h>
35#include <linux/mm.h>
36#include <linux/string.h>
37#include <linux/errno.h>
38#include <linux/sched.h>
39#include <linux/ioctl.h>
40#include <linux/delay.h>
41#include <linux/stddef.h>
42#include <asm/page.h>
43#include <asm/uaccess.h>
44#include <linux/page-flags.h>
45#include <linux/moduleparam.h>
46
47#include "w9968cf.h"
48#include "w9968cf_decoder.h"
49
50
51
52/****************************************************************************
53 * Module macros and parameters *
54 ****************************************************************************/
55
56MODULE_DEVICE_TABLE(usb, winbond_id_table);
57
58MODULE_AUTHOR(W9968CF_MODULE_AUTHOR" "W9968CF_AUTHOR_EMAIL);
59MODULE_DESCRIPTION(W9968CF_MODULE_NAME);
60MODULE_VERSION(W9968CF_MODULE_VERSION);
61MODULE_LICENSE(W9968CF_MODULE_LICENSE);
62MODULE_SUPPORTED_DEVICE("Video");
63
64static int ovmod_load = W9968CF_OVMOD_LOAD;
65static unsigned short simcams = W9968CF_SIMCAMS;
66static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
67static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
68 W9968CF_PACKET_SIZE};
69static unsigned short max_buffers[] = {[0 ... W9968CF_MAX_DEVICES-1] =
70 W9968CF_BUFFERS};
71static int double_buffer[] = {[0 ... W9968CF_MAX_DEVICES-1] =
72 W9968CF_DOUBLE_BUFFER};
73static int clamping[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLAMPING};
74static unsigned short filter_type[]= {[0 ... W9968CF_MAX_DEVICES-1] =
75 W9968CF_FILTER_TYPE};
76static int largeview[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_LARGEVIEW};
77static unsigned short decompression[] = {[0 ... W9968CF_MAX_DEVICES-1] =
78 W9968CF_DECOMPRESSION};
79static int upscaling[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_UPSCALING};
80static unsigned short force_palette[] = {[0 ... W9968CF_MAX_DEVICES-1] = 0};
81static int force_rgb[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_FORCE_RGB};
82static int autobright[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOBRIGHT};
83static int autoexp[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOEXP};
84static unsigned short lightfreq[] = {[0 ... W9968CF_MAX_DEVICES-1] =
85 W9968CF_LIGHTFREQ};
86static int bandingfilter[] = {[0 ... W9968CF_MAX_DEVICES-1]=
87 W9968CF_BANDINGFILTER};
88static short clockdiv[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLOCKDIV};
89static int backlight[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_BACKLIGHT};
90static int mirror[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_MIRROR};
91static int monochrome[] = {[0 ... W9968CF_MAX_DEVICES-1]=W9968CF_MONOCHROME};
92static unsigned int brightness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
93 W9968CF_BRIGHTNESS};
94static unsigned int hue[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_HUE};
95static unsigned int colour[]={[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_COLOUR};
96static unsigned int contrast[] = {[0 ... W9968CF_MAX_DEVICES-1] =
97 W9968CF_CONTRAST};
98static unsigned int whiteness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
99 W9968CF_WHITENESS};
100#ifdef W9968CF_DEBUG
101static unsigned short debug = W9968CF_DEBUG_LEVEL;
102static int specific_debug = W9968CF_SPECIFIC_DEBUG;
103#endif
104
105static unsigned int param_nv[24]; /* number of values per parameter */
106
107#ifdef CONFIG_KMOD
108module_param(ovmod_load, bool, 0644);
109#endif
110module_param(simcams, ushort, 0644);
111module_param_array(video_nr, short, &param_nv[0], 0444);
112module_param_array(packet_size, uint, &param_nv[1], 0444);
113module_param_array(max_buffers, ushort, &param_nv[2], 0444);
114module_param_array(double_buffer, bool, &param_nv[3], 0444);
115module_param_array(clamping, bool, &param_nv[4], 0444);
116module_param_array(filter_type, ushort, &param_nv[5], 0444);
117module_param_array(largeview, bool, &param_nv[6], 0444);
118module_param_array(decompression, ushort, &param_nv[7], 0444);
119module_param_array(upscaling, bool, &param_nv[8], 0444);
120module_param_array(force_palette, ushort, &param_nv[9], 0444);
121module_param_array(force_rgb, ushort, &param_nv[10], 0444);
122module_param_array(autobright, bool, &param_nv[11], 0444);
123module_param_array(autoexp, bool, &param_nv[12], 0444);
124module_param_array(lightfreq, ushort, &param_nv[13], 0444);
125module_param_array(bandingfilter, bool, &param_nv[14], 0444);
126module_param_array(clockdiv, short, &param_nv[15], 0444);
127module_param_array(backlight, bool, &param_nv[16], 0444);
128module_param_array(mirror, bool, &param_nv[17], 0444);
129module_param_array(monochrome, bool, &param_nv[18], 0444);
130module_param_array(brightness, uint, &param_nv[19], 0444);
131module_param_array(hue, uint, &param_nv[20], 0444);
132module_param_array(colour, uint, &param_nv[21], 0444);
133module_param_array(contrast, uint, &param_nv[22], 0444);
134module_param_array(whiteness, uint, &param_nv[23], 0444);
135#ifdef W9968CF_DEBUG
136module_param(debug, ushort, 0644);
137module_param(specific_debug, bool, 0644);
138#endif
139
140#ifdef CONFIG_KMOD
141MODULE_PARM_DESC(ovmod_load,
142 "\n<0|1> Automatic 'ovcamchip' module loading."
143 "\n0 disabled, 1 enabled."
144 "\nIf enabled,'insmod' searches for the required 'ovcamchip'"
145 "\nmodule in the system, according to its configuration, and"
146 "\nattempts to load that module automatically. This action is"
147 "\nperformed once as soon as the 'w9968cf' module is loaded"
148 "\ninto memory."
149 "\nDefault value is "__MODULE_STRING(W9968CF_OVMOD_LOAD)"."
150 "\n");
151#endif
152MODULE_PARM_DESC(simcams,
153 "\n<n> Number of cameras allowed to stream simultaneously."
154 "\nn may vary from 0 to "
155 __MODULE_STRING(W9968CF_MAX_DEVICES)"."
156 "\nDefault value is "__MODULE_STRING(W9968CF_SIMCAMS)"."
157 "\n");
158MODULE_PARM_DESC(video_nr,
159 "\n<-1|n[,...]> Specify V4L minor mode number."
160 "\n -1 = use next available (default)"
161 "\n n = use minor number n (integer >= 0)"
162 "\nYou can specify up to "__MODULE_STRING(W9968CF_MAX_DEVICES)
163 " cameras this way."
164 "\nFor example:"
165 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
166 "\nthe second camera and use auto for the first"
167 "\none and for every other camera."
168 "\n");
169MODULE_PARM_DESC(packet_size,
170 "\n<n[,...]> Specify the maximum data payload"
171 "\nsize in bytes for alternate settings, for each device."
172 "\nn is scaled between 63 and 1023 "
173 "(default is "__MODULE_STRING(W9968CF_PACKET_SIZE)")."
174 "\n");
175MODULE_PARM_DESC(max_buffers,
176 "\n<n[,...]> For advanced users."
177 "\nSpecify the maximum number of video frame buffers"
178 "\nto allocate for each device, from 2 to "
179 __MODULE_STRING(W9968CF_MAX_BUFFERS)
180 ". (default is "__MODULE_STRING(W9968CF_BUFFERS)")."
181 "\n");
182MODULE_PARM_DESC(double_buffer,
183 "\n<0|1[,...]> "
184 "Hardware double buffering: 0 disabled, 1 enabled."
185 "\nIt should be enabled if you want smooth video output: if"
186 "\nyou obtain out of sync. video, disable it, or try to"
187 "\ndecrease the 'clockdiv' module parameter value."
188 "\nDefault value is "__MODULE_STRING(W9968CF_DOUBLE_BUFFER)
189 " for every device."
190 "\n");
191MODULE_PARM_DESC(clamping,
192 "\n<0|1[,...]> Video data clamping: 0 disabled, 1 enabled."
193 "\nDefault value is "__MODULE_STRING(W9968CF_CLAMPING)
194 " for every device."
195 "\n");
196MODULE_PARM_DESC(filter_type,
197 "\n<0|1|2[,...]> Video filter type."
198 "\n0 none, 1 (1-2-1) 3-tap filter, "
199 "2 (2-3-6-3-2) 5-tap filter."
200 "\nDefault value is "__MODULE_STRING(W9968CF_FILTER_TYPE)
201 " for every device."
202 "\nThe filter is used to reduce noise and aliasing artifacts"
203 "\nproduced by the CCD or CMOS image sensor, and the scaling"
204 " process."
205 "\n");
206MODULE_PARM_DESC(largeview,
207 "\n<0|1[,...]> Large view: 0 disabled, 1 enabled."
208 "\nDefault value is "__MODULE_STRING(W9968CF_LARGEVIEW)
209 " for every device."
210 "\n");
211MODULE_PARM_DESC(upscaling,
212 "\n<0|1[,...]> Software scaling (for non-compressed video):"
213 "\n0 disabled, 1 enabled."
214 "\nDisable it if you have a slow CPU or you don't have"
215 " enough memory."
216 "\nDefault value is "__MODULE_STRING(W9968CF_UPSCALING)
217 " for every device."
218 "\nIf 'w9968cf-vpp' is not present, this parameter is"
219 " set to 0."
220 "\n");
221MODULE_PARM_DESC(decompression,
222 "\n<0|1|2[,...]> Software video decompression:"
223 "\n- 0 disables decompression (doesn't allow formats needing"
224 " decompression)"
225 "\n- 1 forces decompression (allows formats needing"
226 " decompression only);"
227 "\n- 2 allows any permitted formats."
228 "\nFormats supporting compressed video are YUV422P and"
229 " YUV420P/YUV420 "
230 "\nin any resolutions where both width and height are "
231 "a multiple of 16."
232 "\nDefault value is "__MODULE_STRING(W9968CF_DECOMPRESSION)
233 " for every device."
234 "\nIf 'w9968cf-vpp' is not present, forcing decompression is "
235 "\nnot allowed; in this case this parameter is set to 2."
236 "\n");
237MODULE_PARM_DESC(force_palette,
238 "\n<0"
239 "|" __MODULE_STRING(VIDEO_PALETTE_UYVY)
240 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420)
241 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422P)
242 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420P)
243 "|" __MODULE_STRING(VIDEO_PALETTE_YUYV)
244 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422)
245 "|" __MODULE_STRING(VIDEO_PALETTE_GREY)
246 "|" __MODULE_STRING(VIDEO_PALETTE_RGB555)
247 "|" __MODULE_STRING(VIDEO_PALETTE_RGB565)
248 "|" __MODULE_STRING(VIDEO_PALETTE_RGB24)
249 "|" __MODULE_STRING(VIDEO_PALETTE_RGB32)
250 "[,...]>"
251 " Force picture palette."
252 "\nIn order:"
253 "\n- 0 allows any of the following formats:"
254 "\n- UYVY 16 bpp - Original video, compression disabled"
255 "\n- YUV420 12 bpp - Original video, compression enabled"
256 "\n- YUV422P 16 bpp - Original video, compression enabled"
257 "\n- YUV420P 12 bpp - Original video, compression enabled"
258 "\n- YUVY 16 bpp - Software conversion from UYVY"
259 "\n- YUV422 16 bpp - Software conversion from UYVY"
260 "\n- GREY 8 bpp - Software conversion from UYVY"
261 "\n- RGB555 16 bpp - Software conversion from UYVY"
262 "\n- RGB565 16 bpp - Software conversion from UYVY"
263 "\n- RGB24 24 bpp - Software conversion from UYVY"
264 "\n- RGB32 32 bpp - Software conversion from UYVY"
265 "\nWhen not 0, this parameter will override 'decompression'."
266 "\nDefault value is 0 for every device."
267 "\nInitial palette is "
268 __MODULE_STRING(W9968CF_PALETTE_DECOMP_ON)"."
269 "\nIf 'w9968cf-vpp' is not present, this parameter is"
270 " set to 9 (UYVY)."
271 "\n");
272MODULE_PARM_DESC(force_rgb,
273 "\n<0|1[,...]> Read RGB video data instead of BGR:"
274 "\n 1 = use RGB component ordering."
275 "\n 0 = use BGR component ordering."
276 "\nThis parameter has effect when using RGBX palettes only."
277 "\nDefault value is "__MODULE_STRING(W9968CF_FORCE_RGB)
278 " for every device."
279 "\n");
280MODULE_PARM_DESC(autobright,
281 "\n<0|1[,...]> Image sensor automatically changes brightness:"
282 "\n 0 = no, 1 = yes"
283 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOBRIGHT)
284 " for every device."
285 "\n");
286MODULE_PARM_DESC(autoexp,
287 "\n<0|1[,...]> Image sensor automatically changes exposure:"
288 "\n 0 = no, 1 = yes"
289 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOEXP)
290 " for every device."
291 "\n");
292MODULE_PARM_DESC(lightfreq,
293 "\n<50|60[,...]> Light frequency in Hz:"
294 "\n 50 for European and Asian lighting,"
295 " 60 for American lighting."
296 "\nDefault value is "__MODULE_STRING(W9968CF_LIGHTFREQ)
297 " for every device."
298 "\n");
299MODULE_PARM_DESC(bandingfilter,
300 "\n<0|1[,...]> Banding filter to reduce effects of"
301 " fluorescent lighting:"
302 "\n 0 disabled, 1 enabled."
303 "\nThis filter tries to reduce the pattern of horizontal"
304 "\nlight/dark bands caused by some (usually fluorescent)"
305 " lighting."
306 "\nDefault value is "__MODULE_STRING(W9968CF_BANDINGFILTER)
307 " for every device."
308 "\n");
309MODULE_PARM_DESC(clockdiv,
310 "\n<-1|n[,...]> "
311 "Force pixel clock divisor to a specific value (for experts):"
312 "\n n may vary from 0 to 127."
313 "\n -1 for automatic value."
314 "\nSee also the 'double_buffer' module parameter."
315 "\nDefault value is "__MODULE_STRING(W9968CF_CLOCKDIV)
316 " for every device."
317 "\n");
318MODULE_PARM_DESC(backlight,
319 "\n<0|1[,...]> Objects are lit from behind:"
320 "\n 0 = no, 1 = yes"
321 "\nDefault value is "__MODULE_STRING(W9968CF_BACKLIGHT)
322 " for every device."
323 "\n");
324MODULE_PARM_DESC(mirror,
325 "\n<0|1[,...]> Reverse image horizontally:"
326 "\n 0 = no, 1 = yes"
327 "\nDefault value is "__MODULE_STRING(W9968CF_MIRROR)
328 " for every device."
329 "\n");
330MODULE_PARM_DESC(monochrome,
331 "\n<0|1[,...]> Use image sensor as monochrome sensor:"
332 "\n 0 = no, 1 = yes"
333 "\nNot all the sensors support monochrome color."
334 "\nDefault value is "__MODULE_STRING(W9968CF_MONOCHROME)
335 " for every device."
336 "\n");
337MODULE_PARM_DESC(brightness,
338 "\n<n[,...]> Set picture brightness (0-65535)."
339 "\nDefault value is "__MODULE_STRING(W9968CF_BRIGHTNESS)
340 " for every device."
341 "\nThis parameter has no effect if 'autobright' is enabled."
342 "\n");
343MODULE_PARM_DESC(hue,
344 "\n<n[,...]> Set picture hue (0-65535)."
345 "\nDefault value is "__MODULE_STRING(W9968CF_HUE)
346 " for every device."
347 "\n");
348MODULE_PARM_DESC(colour,
349 "\n<n[,...]> Set picture saturation (0-65535)."
350 "\nDefault value is "__MODULE_STRING(W9968CF_COLOUR)
351 " for every device."
352 "\n");
353MODULE_PARM_DESC(contrast,
354 "\n<n[,...]> Set picture contrast (0-65535)."
355 "\nDefault value is "__MODULE_STRING(W9968CF_CONTRAST)
356 " for every device."
357 "\n");
358MODULE_PARM_DESC(whiteness,
359 "\n<n[,...]> Set picture whiteness (0-65535)."
360 "\nDefault value is "__MODULE_STRING(W9968CF_WHITENESS)
361 " for every device."
362 "\n");
363#ifdef W9968CF_DEBUG
364MODULE_PARM_DESC(debug,
365 "\n<n> Debugging information level, from 0 to 6:"
366 "\n0 = none (use carefully)"
367 "\n1 = critical errors"
368 "\n2 = significant informations"
369 "\n3 = configuration or general messages"
370 "\n4 = warnings"
371 "\n5 = called functions"
372 "\n6 = function internals"
373 "\nLevel 5 and 6 are useful for testing only, when only "
374 "one device is used."
375 "\nDefault value is "__MODULE_STRING(W9968CF_DEBUG_LEVEL)"."
376 "\n");
377MODULE_PARM_DESC(specific_debug,
378 "\n<0|1> Enable or disable specific debugging messages:"
379 "\n0 = print messages concerning every level"
380 " <= 'debug' level."
381 "\n1 = print messages concerning the level"
382 " indicated by 'debug'."
383 "\nDefault value is "
384 __MODULE_STRING(W9968CF_SPECIFIC_DEBUG)"."
385 "\n");
386#endif /* W9968CF_DEBUG */
387
388
389
390/****************************************************************************
391 * Some prototypes *
392 ****************************************************************************/
393
394/* Video4linux interface */
395static struct file_operations w9968cf_fops;
396static int w9968cf_open(struct inode*, struct file*);
397static int w9968cf_release(struct inode*, struct file*);
398static int w9968cf_mmap(struct file*, struct vm_area_struct*);
399static int w9968cf_ioctl(struct inode*, struct file*, unsigned, unsigned long);
400static ssize_t w9968cf_read(struct file*, char __user *, size_t, loff_t*);
401static int w9968cf_v4l_ioctl(struct inode*, struct file*, unsigned int,
402 void __user *);
403
404/* USB-specific */
405static int w9968cf_start_transfer(struct w9968cf_device*);
406static int w9968cf_stop_transfer(struct w9968cf_device*);
407static int w9968cf_write_reg(struct w9968cf_device*, u16 value, u16 index);
408static int w9968cf_read_reg(struct w9968cf_device*, u16 index);
409static int w9968cf_write_fsb(struct w9968cf_device*, u16* data);
410static int w9968cf_write_sb(struct w9968cf_device*, u16 value);
411static int w9968cf_read_sb(struct w9968cf_device*);
412static int w9968cf_upload_quantizationtables(struct w9968cf_device*);
413static void w9968cf_urb_complete(struct urb *urb, struct pt_regs *regs);
414
415/* Low-level I2C (SMBus) I/O */
416static int w9968cf_smbus_start(struct w9968cf_device*);
417static int w9968cf_smbus_stop(struct w9968cf_device*);
418static int w9968cf_smbus_write_byte(struct w9968cf_device*, u8 v);
419static int w9968cf_smbus_read_byte(struct w9968cf_device*, u8* v);
420static int w9968cf_smbus_write_ack(struct w9968cf_device*);
421static int w9968cf_smbus_read_ack(struct w9968cf_device*);
422static int w9968cf_smbus_refresh_bus(struct w9968cf_device*);
423static int w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
424 u16 address, u8* value);
425static int w9968cf_i2c_adap_read_byte_data(struct w9968cf_device*, u16 address,
426 u8 subaddress, u8* value);
427static int w9968cf_i2c_adap_write_byte(struct w9968cf_device*,
428 u16 address, u8 subaddress);
429static int w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device*,
430 u16 address, u8 subaddress,
431 u8 value);
432
433/* I2C interface to kernel */
434static int w9968cf_i2c_init(struct w9968cf_device*);
435static int w9968cf_i2c_smbus_xfer(struct i2c_adapter*, u16 addr,
436 unsigned short flags, char read_write,
437 u8 command, int size, union i2c_smbus_data*);
438static u32 w9968cf_i2c_func(struct i2c_adapter*);
439static int w9968cf_i2c_attach_inform(struct i2c_client*);
440static int w9968cf_i2c_detach_inform(struct i2c_client*);
441static int w9968cf_i2c_control(struct i2c_adapter*, unsigned int cmd,
442 unsigned long arg);
443
444/* Memory management */
445static void* rvmalloc(unsigned long size);
446static void rvfree(void *mem, unsigned long size);
447static void w9968cf_deallocate_memory(struct w9968cf_device*);
448static int w9968cf_allocate_memory(struct w9968cf_device*);
449
450/* High-level image sensor control functions */
451static int w9968cf_sensor_set_control(struct w9968cf_device*,int cid,int val);
452static int w9968cf_sensor_get_control(struct w9968cf_device*,int cid,int *val);
453static int w9968cf_sensor_cmd(struct w9968cf_device*,
454 unsigned int cmd, void *arg);
455static int w9968cf_sensor_init(struct w9968cf_device*);
456static int w9968cf_sensor_update_settings(struct w9968cf_device*);
457static int w9968cf_sensor_get_picture(struct w9968cf_device*);
458static int w9968cf_sensor_update_picture(struct w9968cf_device*,
459 struct video_picture pict);
460
461/* Other helper functions */
462static void w9968cf_configure_camera(struct w9968cf_device*,struct usb_device*,
463 enum w9968cf_model_id,
464 const unsigned short dev_nr);
465static void w9968cf_adjust_configuration(struct w9968cf_device*);
466static int w9968cf_turn_on_led(struct w9968cf_device*);
467static int w9968cf_init_chip(struct w9968cf_device*);
468static inline u16 w9968cf_valid_palette(u16 palette);
469static inline u16 w9968cf_valid_depth(u16 palette);
470static inline u8 w9968cf_need_decompression(u16 palette);
471static int w9968cf_set_picture(struct w9968cf_device*, struct video_picture);
472static int w9968cf_set_window(struct w9968cf_device*, struct video_window);
473static int w9968cf_postprocess_frame(struct w9968cf_device*,
474 struct w9968cf_frame_t*);
475static int w9968cf_adjust_window_size(struct w9968cf_device*, u16* w, u16* h);
476static void w9968cf_init_framelist(struct w9968cf_device*);
477static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
478static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
479static void w9968cf_release_resources(struct w9968cf_device*);
480
481
482
483/****************************************************************************
484 * Symbolic names *
485 ****************************************************************************/
486
487/* Used to represent a list of values and their respective symbolic names */
488struct w9968cf_symbolic_list {
489 const int num;
490 const char *name;
491};
492
493/*--------------------------------------------------------------------------
494 Returns the name of the matching element in the symbolic_list array. The
495 end of the list must be marked with an element that has a NULL name.
496 --------------------------------------------------------------------------*/
497static inline const char *
498symbolic(struct w9968cf_symbolic_list list[], const int num)
499{
500 int i;
501
502 for (i = 0; list[i].name != NULL; i++)
503 if (list[i].num == num)
504 return (list[i].name);
505
506 return "Unknown";
507}
508
509static struct w9968cf_symbolic_list camlist[] = {
510 { W9968CF_MOD_GENERIC, "W996[87]CF JPEG USB Dual Mode Camera" },
511 { W9968CF_MOD_CLVBWGP, "Creative Labs Video Blaster WebCam Go Plus" },
512
513 /* Other cameras (having the same descriptors as Generic W996[87]CF) */
514 { W9968CF_MOD_ADPVDMA, "Aroma Digi Pen VGA Dual Mode ADG-5000" },
515 { W9986CF_MOD_AAU, "AVerMedia AVerTV USB" },
516 { W9968CF_MOD_CLVBWG, "Creative Labs Video Blaster WebCam Go" },
517 { W9968CF_MOD_LL, "Lebon LDC-035A" },
518 { W9968CF_MOD_EEEMC, "Ezonics EZ-802 EZMega Cam" },
519 { W9968CF_MOD_OOE, "OmniVision OV8610-EDE" },
520 { W9968CF_MOD_ODPVDMPC, "OPCOM Digi Pen VGA Dual Mode Pen Camera" },
521 { W9968CF_MOD_PDPII, "Pretec Digi Pen-II" },
522 { W9968CF_MOD_PDP480, "Pretec DigiPen-480" },
523
524 { -1, NULL }
525};
526
527static struct w9968cf_symbolic_list senlist[] = {
528 { CC_OV76BE, "OV76BE" },
529 { CC_OV7610, "OV7610" },
530 { CC_OV7620, "OV7620" },
531 { CC_OV7620AE, "OV7620AE" },
532 { CC_OV6620, "OV6620" },
533 { CC_OV6630, "OV6630" },
534 { CC_OV6630AE, "OV6630AE" },
535 { CC_OV6630AF, "OV6630AF" },
536 { -1, NULL }
537};
538
539/* Video4Linux1 palettes */
540static struct w9968cf_symbolic_list v4l1_plist[] = {
541 { VIDEO_PALETTE_GREY, "GREY" },
542 { VIDEO_PALETTE_HI240, "HI240" },
543 { VIDEO_PALETTE_RGB565, "RGB565" },
544 { VIDEO_PALETTE_RGB24, "RGB24" },
545 { VIDEO_PALETTE_RGB32, "RGB32" },
546 { VIDEO_PALETTE_RGB555, "RGB555" },
547 { VIDEO_PALETTE_YUV422, "YUV422" },
548 { VIDEO_PALETTE_YUYV, "YUYV" },
549 { VIDEO_PALETTE_UYVY, "UYVY" },
550 { VIDEO_PALETTE_YUV420, "YUV420" },
551 { VIDEO_PALETTE_YUV411, "YUV411" },
552 { VIDEO_PALETTE_RAW, "RAW" },
553 { VIDEO_PALETTE_YUV422P, "YUV422P" },
554 { VIDEO_PALETTE_YUV411P, "YUV411P" },
555 { VIDEO_PALETTE_YUV420P, "YUV420P" },
556 { VIDEO_PALETTE_YUV410P, "YUV410P" },
557 { -1, NULL }
558};
559
560/* Decoder error codes: */
561static struct w9968cf_symbolic_list decoder_errlist[] = {
562 { W9968CF_DEC_ERR_CORRUPTED_DATA, "Corrupted data" },
563 { W9968CF_DEC_ERR_BUF_OVERFLOW, "Buffer overflow" },
564 { W9968CF_DEC_ERR_NO_SOI, "SOI marker not found" },
565 { W9968CF_DEC_ERR_NO_SOF0, "SOF0 marker not found" },
566 { W9968CF_DEC_ERR_NO_SOS, "SOS marker not found" },
567 { W9968CF_DEC_ERR_NO_EOI, "EOI marker not found" },
568 { -1, NULL }
569};
570
571/* URB error codes: */
572static struct w9968cf_symbolic_list urb_errlist[] = {
573 { -ENOMEM, "No memory for allocation of internal structures" },
574 { -ENOSPC, "The host controller's bandwidth is already consumed" },
575 { -ENOENT, "URB was canceled by unlink_urb" },
576 { -EXDEV, "ISO transfer only partially completed" },
577 { -EAGAIN, "Too match scheduled for the future" },
578 { -ENXIO, "URB already queued" },
579 { -EFBIG, "Too much ISO frames requested" },
580 { -ENOSR, "Buffer error (overrun)" },
581 { -EPIPE, "Specified endpoint is stalled (device not responding)"},
582 { -EOVERFLOW, "Babble (bad cable?)" },
583 { -EPROTO, "Bit-stuff error (bad cable?)" },
584 { -EILSEQ, "CRC/Timeout" },
585 { -ETIMEDOUT, "NAK (device does not respond)" },
586 { -1, NULL }
587};
588
589
590
591/****************************************************************************
592 * Memory management functions *
593 ****************************************************************************/
594static void* rvmalloc(unsigned long size)
595{
596 void* mem;
597 unsigned long adr;
598
599 size = PAGE_ALIGN(size);
600 mem = vmalloc_32(size);
601 if (!mem)
602 return NULL;
603
604 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
605 adr = (unsigned long) mem;
606 while (size > 0) {
607 SetPageReserved(vmalloc_to_page((void *)adr));
608 adr += PAGE_SIZE;
609 size -= PAGE_SIZE;
610 }
611
612 return mem;
613}
614
615
616static void rvfree(void* mem, unsigned long size)
617{
618 unsigned long adr;
619
620 if (!mem)
621 return;
622
623 adr = (unsigned long) mem;
624 while ((long) size > 0) {
625 ClearPageReserved(vmalloc_to_page((void *)adr));
626 adr += PAGE_SIZE;
627 size -= PAGE_SIZE;
628 }
629 vfree(mem);
630}
631
632
633/*--------------------------------------------------------------------------
634 Deallocate previously allocated memory.
635 --------------------------------------------------------------------------*/
636static void w9968cf_deallocate_memory(struct w9968cf_device* cam)
637{
638 u8 i;
639
640 /* Free the isochronous transfer buffers */
641 for (i = 0; i < W9968CF_URBS; i++) {
642 kfree(cam->transfer_buffer[i]);
643 cam->transfer_buffer[i] = NULL;
644 }
645
646 /* Free temporary frame buffer */
647 if (cam->frame_tmp.buffer) {
648 rvfree(cam->frame_tmp.buffer, cam->frame_tmp.size);
649 cam->frame_tmp.buffer = NULL;
650 }
651
652 /* Free helper buffer */
653 if (cam->frame_vpp.buffer) {
654 rvfree(cam->frame_vpp.buffer, cam->frame_vpp.size);
655 cam->frame_vpp.buffer = NULL;
656 }
657
658 /* Free video frame buffers */
659 if (cam->frame[0].buffer) {
660 rvfree(cam->frame[0].buffer, cam->nbuffers*cam->frame[0].size);
661 cam->frame[0].buffer = NULL;
662 }
663
664 cam->nbuffers = 0;
665
666 DBG(5, "Memory successfully deallocated")
667}
668
669
670/*--------------------------------------------------------------------------
671 Allocate memory buffers for USB transfers and video frames.
672 This function is called by open() only.
673 Return 0 on success, a negative number otherwise.
674 --------------------------------------------------------------------------*/
675static int w9968cf_allocate_memory(struct w9968cf_device* cam)
676{
677 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
678 void* buff = NULL;
679 unsigned long hw_bufsize, vpp_bufsize;
680 u8 i, bpp;
681
682 /* NOTE: Deallocation is done elsewhere in case of error */
683
684 /* Calculate the max amount of raw data per frame from the device */
685 hw_bufsize = cam->maxwidth*cam->maxheight*2;
686
687 /* Calculate the max buf. size needed for post-processing routines */
688 bpp = (w9968cf_vpp) ? 4 : 2;
689 if (cam->upscaling)
690 vpp_bufsize = max(W9968CF_MAX_WIDTH*W9968CF_MAX_HEIGHT*bpp,
691 cam->maxwidth*cam->maxheight*bpp);
692 else
693 vpp_bufsize = cam->maxwidth*cam->maxheight*bpp;
694
695 /* Allocate memory for the isochronous transfer buffers */
696 for (i = 0; i < W9968CF_URBS; i++) {
697 if (!(cam->transfer_buffer[i] =
698 kmalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
699 DBG(1, "Couldn't allocate memory for the isochronous "
700 "transfer buffers (%u bytes)",
701 p_size * W9968CF_ISO_PACKETS)
702 return -ENOMEM;
703 }
704 memset(cam->transfer_buffer[i], 0, W9968CF_ISO_PACKETS*p_size);
705 }
706
707 /* Allocate memory for the temporary frame buffer */
708 if (!(cam->frame_tmp.buffer = rvmalloc(hw_bufsize))) {
709 DBG(1, "Couldn't allocate memory for the temporary "
710 "video frame buffer (%lu bytes)", hw_bufsize)
711 return -ENOMEM;
712 }
713 cam->frame_tmp.size = hw_bufsize;
714 cam->frame_tmp.number = -1;
715
716 /* Allocate memory for the helper buffer */
717 if (w9968cf_vpp) {
718 if (!(cam->frame_vpp.buffer = rvmalloc(vpp_bufsize))) {
719 DBG(1, "Couldn't allocate memory for the helper buffer"
720 " (%lu bytes)", vpp_bufsize)
721 return -ENOMEM;
722 }
723 cam->frame_vpp.size = vpp_bufsize;
724 } else
725 cam->frame_vpp.buffer = NULL;
726
727 /* Allocate memory for video frame buffers */
728 cam->nbuffers = cam->max_buffers;
729 while (cam->nbuffers >= 2) {
730 if ((buff = rvmalloc(cam->nbuffers * vpp_bufsize)))
731 break;
732 else
733 cam->nbuffers--;
734 }
735
736 if (!buff) {
737 DBG(1, "Couldn't allocate memory for the video frame buffers")
738 cam->nbuffers = 0;
739 return -ENOMEM;
740 }
741
742 if (cam->nbuffers != cam->max_buffers)
743 DBG(2, "Couldn't allocate memory for %u video frame buffers. "
744 "Only memory for %u buffers has been allocated",
745 cam->max_buffers, cam->nbuffers)
746
747 for (i = 0; i < cam->nbuffers; i++) {
748 cam->frame[i].buffer = buff + i*vpp_bufsize;
749 cam->frame[i].size = vpp_bufsize;
750 cam->frame[i].number = i;
751 /* Circular list */
752 if (i != cam->nbuffers-1)
753 cam->frame[i].next = &cam->frame[i+1];
754 else
755 cam->frame[i].next = &cam->frame[0];
756 cam->frame[i].status = F_UNUSED;
757 }
758
759 DBG(5, "Memory successfully allocated")
760 return 0;
761}
762
763
764
765/****************************************************************************
766 * USB-specific functions *
767 ****************************************************************************/
768
769/*--------------------------------------------------------------------------
770 This is an handler function which is called after the URBs are completed.
771 It collects multiple data packets coming from the camera by putting them
772 into frame buffers: one or more zero data length data packets are used to
773 mark the end of a video frame; the first non-zero data packet is the start
774 of the next video frame; if an error is encountered in a packet, the entire
775 video frame is discarded and grabbed again.
776 If there are no requested frames in the FIFO list, packets are collected into
777 a temporary buffer.
778 --------------------------------------------------------------------------*/
779static void w9968cf_urb_complete(struct urb *urb, struct pt_regs *regs)
780{
781 struct w9968cf_device* cam = (struct w9968cf_device*)urb->context;
782 struct w9968cf_frame_t** f;
783 unsigned int len, status;
784 void* pos;
785 u8 i;
786 int err = 0;
787
788 if ((!cam->streaming) || cam->disconnected) {
789 DBG(4, "Got interrupt, but not streaming")
790 return;
791 }
792
793 /* "(*f)" will be used instead of "cam->frame_current" */
794 f = &cam->frame_current;
795
796 /* If a frame has been requested and we are grabbing into
797 the temporary frame, we'll switch to that requested frame */
798 if ((*f) == &cam->frame_tmp && *cam->requested_frame) {
799 if (cam->frame_tmp.status == F_GRABBING) {
800 w9968cf_pop_frame(cam, &cam->frame_current);
801 (*f)->status = F_GRABBING;
802 (*f)->length = cam->frame_tmp.length;
803 memcpy((*f)->buffer, cam->frame_tmp.buffer,
804 (*f)->length);
805 DBG(6, "Switched from temp. frame to frame #%d",
806 (*f)->number)
807 }
808 }
809
810 for (i = 0; i < urb->number_of_packets; i++) {
811 len = urb->iso_frame_desc[i].actual_length;
812 status = urb->iso_frame_desc[i].status;
813 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
814
815 if (status && len != 0) {
816 DBG(4, "URB failed, error in data packet "
817 "(error #%u, %s)",
818 status, symbolic(urb_errlist, status))
819 (*f)->status = F_ERROR;
820 continue;
821 }
822
823 if (len) { /* start of frame */
824
825 if ((*f)->status == F_UNUSED) {
826 (*f)->status = F_GRABBING;
827 (*f)->length = 0;
828 }
829
830 /* Buffer overflows shouldn't happen, however...*/
831 if ((*f)->length + len > (*f)->size) {
832 DBG(4, "Buffer overflow: bad data packets")
833 (*f)->status = F_ERROR;
834 }
835
836 if ((*f)->status == F_GRABBING) {
837 memcpy((*f)->buffer + (*f)->length, pos, len);
838 (*f)->length += len;
839 }
840
841 } else if ((*f)->status == F_GRABBING) { /* end of frame */
842
843 DBG(6, "Frame #%d successfully grabbed", (*f)->number)
844
845 if (cam->vpp_flag & VPP_DECOMPRESSION) {
846 err = w9968cf_vpp->check_headers((*f)->buffer,
847 (*f)->length);
848 if (err) {
849 DBG(4, "Skip corrupted frame: %s",
850 symbolic(decoder_errlist, err))
851 (*f)->status = F_UNUSED;
852 continue; /* grab this frame again */
853 }
854 }
855
856 (*f)->status = F_READY;
857 (*f)->queued = 0;
858
859 /* Take a pointer to the new frame from the FIFO list.
860 If the list is empty,we'll use the temporary frame*/
861 if (*cam->requested_frame)
862 w9968cf_pop_frame(cam, &cam->frame_current);
863 else {
864 cam->frame_current = &cam->frame_tmp;
865 (*f)->status = F_UNUSED;
866 }
867
868 } else if ((*f)->status == F_ERROR)
869 (*f)->status = F_UNUSED; /* grab it again */
870
871 PDBGG("Frame length %lu | pack.#%u | pack.len. %u | state %d",
872 (unsigned long)(*f)->length, i, len, (*f)->status)
873
874 } /* end for */
875
876 /* Resubmit this URB */
877 urb->dev = cam->usbdev;
878 urb->status = 0;
879 spin_lock(&cam->urb_lock);
880 if (cam->streaming)
881 if ((err = usb_submit_urb(urb, GFP_ATOMIC))) {
882 cam->misconfigured = 1;
883 DBG(1, "Couldn't resubmit the URB: error %d, %s",
884 err, symbolic(urb_errlist, err))
885 }
886 spin_unlock(&cam->urb_lock);
887
888 /* Wake up the user process */
889 wake_up_interruptible(&cam->wait_queue);
890}
891
892
893/*---------------------------------------------------------------------------
894 Setup the URB structures for the isochronous transfer.
895 Submit the URBs so that the data transfer begins.
896 Return 0 on success, a negative number otherwise.
897 ---------------------------------------------------------------------------*/
898static int w9968cf_start_transfer(struct w9968cf_device* cam)
899{
900 struct usb_device *udev = cam->usbdev;
901 struct urb* urb;
902 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
903 u16 w, h, d;
904 int vidcapt;
905 u32 t_size;
906 int err = 0;
907 s8 i, j;
908
909 for (i = 0; i < W9968CF_URBS; i++) {
910 urb = usb_alloc_urb(W9968CF_ISO_PACKETS, GFP_KERNEL);
911 cam->urb[i] = urb;
912 if (!urb) {
913 for (j = 0; j < i; j++)
914 usb_free_urb(cam->urb[j]);
915 DBG(1, "Couldn't allocate the URB structures")
916 return -ENOMEM;
917 }
918
919 urb->dev = udev;
920 urb->context = (void*)cam;
921 urb->pipe = usb_rcvisocpipe(udev, 1);
922 urb->transfer_flags = URB_ISO_ASAP;
923 urb->number_of_packets = W9968CF_ISO_PACKETS;
924 urb->complete = w9968cf_urb_complete;
925 urb->transfer_buffer = cam->transfer_buffer[i];
926 urb->transfer_buffer_length = p_size*W9968CF_ISO_PACKETS;
927 urb->interval = 1;
928 for (j = 0; j < W9968CF_ISO_PACKETS; j++) {
929 urb->iso_frame_desc[j].offset = p_size*j;
930 urb->iso_frame_desc[j].length = p_size;
931 }
932 }
933
934 /* Transfer size per frame, in WORD ! */
935 d = cam->hw_depth;
936 w = cam->hw_width;
937 h = cam->hw_height;
938
939 t_size = (w*h*d)/16;
940
941 err = w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
942 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
943
944 /* Transfer size */
945 err += w9968cf_write_reg(cam, t_size & 0xffff, 0x3d); /* low bits */
946 err += w9968cf_write_reg(cam, t_size >> 16, 0x3e); /* high bits */
947
948 if (cam->vpp_flag & VPP_DECOMPRESSION)
949 err += w9968cf_upload_quantizationtables(cam);
950
951 vidcapt = w9968cf_read_reg(cam, 0x16); /* read picture settings */
952 err += w9968cf_write_reg(cam, vidcapt|0x8000, 0x16); /* capt. enable */
953
954 err += usb_set_interface(udev, 0, cam->altsetting);
955 err += w9968cf_write_reg(cam, 0x8a05, 0x3c); /* USB FIFO enable */
956
957 if (err || (vidcapt < 0)) {
958 for (i = 0; i < W9968CF_URBS; i++)
959 usb_free_urb(cam->urb[i]);
960 DBG(1, "Couldn't tell the camera to start the data transfer")
961 return err;
962 }
963
964 w9968cf_init_framelist(cam);
965
966 /* Begin to grab into the temporary buffer */
967 cam->frame_tmp.status = F_UNUSED;
968 cam->frame_tmp.queued = 0;
969 cam->frame_current = &cam->frame_tmp;
970
971 if (!(cam->vpp_flag & VPP_DECOMPRESSION))
972 DBG(5, "Isochronous transfer size: %lu bytes/frame",
973 (unsigned long)t_size*2)
974
975 DBG(5, "Starting the isochronous transfer...")
976
977 cam->streaming = 1;
978
979 /* Submit the URBs */
980 for (i = 0; i < W9968CF_URBS; i++) {
981 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
982 if (err) {
983 cam->streaming = 0;
984 for (j = i-1; j >= 0; j--) {
985 usb_kill_urb(cam->urb[j]);
986 usb_free_urb(cam->urb[j]);
987 }
988 DBG(1, "Couldn't send a transfer request to the "
989 "USB core (error #%d, %s)", err,
990 symbolic(urb_errlist, err))
991 return err;
992 }
993 }
994
995 return 0;
996}
997
998
999/*--------------------------------------------------------------------------
1000 Stop the isochronous transfer and set alternate setting to 0 (0Mb/s).
1001 Return 0 on success, a negative number otherwise.
1002 --------------------------------------------------------------------------*/
1003static int w9968cf_stop_transfer(struct w9968cf_device* cam)
1004{
1005 struct usb_device *udev = cam->usbdev;
1006 unsigned long lock_flags;
1007 int err = 0;
1008 s8 i;
1009
1010 if (!cam->streaming)
1011 return 0;
1012
1013 /* This avoids race conditions with usb_submit_urb()
1014 in the URB completition handler */
1015 spin_lock_irqsave(&cam->urb_lock, lock_flags);
1016 cam->streaming = 0;
1017 spin_unlock_irqrestore(&cam->urb_lock, lock_flags);
1018
1019 for (i = W9968CF_URBS-1; i >= 0; i--)
1020 if (cam->urb[i]) {
1021 usb_kill_urb(cam->urb[i]);
1022 usb_free_urb(cam->urb[i]);
1023 cam->urb[i] = NULL;
1024 }
1025
1026 if (cam->disconnected)
1027 goto exit;
1028
1029 err = w9968cf_write_reg(cam, 0x0a05, 0x3c); /* stop USB transfer */
1030 err += usb_set_interface(udev, 0, 0); /* 0 Mb/s */
1031 err += w9968cf_write_reg(cam, 0x0000, 0x39); /* disable JPEG encoder */
1032 err += w9968cf_write_reg(cam, 0x0000, 0x16); /* stop video capture */
1033
1034 if (err) {
1035 DBG(2, "Failed to tell the camera to stop the isochronous "
1036 "transfer. However this is not a critical error.")
1037 return -EIO;
1038 }
1039
1040exit:
1041 DBG(5, "Isochronous transfer stopped")
1042 return 0;
1043}
1044
1045
1046/*--------------------------------------------------------------------------
1047 Write a W9968CF register.
1048 Return 0 on success, -1 otherwise.
1049 --------------------------------------------------------------------------*/
1050static int w9968cf_write_reg(struct w9968cf_device* cam, u16 value, u16 index)
1051{
1052 struct usb_device* udev = cam->usbdev;
1053 int res;
1054
1055 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
1056 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1057 value, index, NULL, 0, W9968CF_USB_CTRL_TIMEOUT);
1058
1059 if (res < 0)
1060 DBG(4, "Failed to write a register "
1061 "(value 0x%04X, index 0x%02X, error #%d, %s)",
1062 value, index, res, symbolic(urb_errlist, res))
1063
1064 return (res >= 0) ? 0 : -1;
1065}
1066
1067
1068/*--------------------------------------------------------------------------
1069 Read a W9968CF register.
1070 Return the register value on success, -1 otherwise.
1071 --------------------------------------------------------------------------*/
1072static int w9968cf_read_reg(struct w9968cf_device* cam, u16 index)
1073{
1074 struct usb_device* udev = cam->usbdev;
1075 u16* buff = cam->control_buffer;
1076 int res;
1077
1078 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 1,
1079 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1080 0, index, buff, 2, W9968CF_USB_CTRL_TIMEOUT);
1081
1082 if (res < 0)
1083 DBG(4, "Failed to read a register "
1084 "(index 0x%02X, error #%d, %s)",
1085 index, res, symbolic(urb_errlist, res))
1086
1087 return (res >= 0) ? (int)(*buff) : -1;
1088}
1089
1090
1091/*--------------------------------------------------------------------------
1092 Write 64-bit data to the fast serial bus registers.
1093 Return 0 on success, -1 otherwise.
1094 --------------------------------------------------------------------------*/
1095static int w9968cf_write_fsb(struct w9968cf_device* cam, u16* data)
1096{
1097 struct usb_device* udev = cam->usbdev;
1098 u16 value;
1099 int res;
1100
1101 value = *data++;
1102
1103 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
1104 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1105 value, 0x06, data, 6, W9968CF_USB_CTRL_TIMEOUT);
1106
1107 if (res < 0)
1108 DBG(4, "Failed to write the FSB registers "
1109 "(error #%d, %s)", res, symbolic(urb_errlist, res))
1110
1111 return (res >= 0) ? 0 : -1;
1112}
1113
1114
1115/*--------------------------------------------------------------------------
1116 Write data to the serial bus control register.
1117 Return 0 on success, a negative number otherwise.
1118 --------------------------------------------------------------------------*/
1119static int w9968cf_write_sb(struct w9968cf_device* cam, u16 value)
1120{
1121 int err = 0;
1122
1123 err = w9968cf_write_reg(cam, value, 0x01);
1124 udelay(W9968CF_I2C_BUS_DELAY);
1125
1126 return err;
1127}
1128
1129
1130/*--------------------------------------------------------------------------
1131 Read data from the serial bus control register.
1132 Return 0 on success, a negative number otherwise.
1133 --------------------------------------------------------------------------*/
1134static int w9968cf_read_sb(struct w9968cf_device* cam)
1135{
1136 int v = 0;
1137
1138 v = w9968cf_read_reg(cam, 0x01);
1139 udelay(W9968CF_I2C_BUS_DELAY);
1140
1141 return v;
1142}
1143
1144
1145/*--------------------------------------------------------------------------
1146 Upload quantization tables for the JPEG compression.
1147 This function is called by w9968cf_start_transfer().
1148 Return 0 on success, a negative number otherwise.
1149 --------------------------------------------------------------------------*/
1150static int w9968cf_upload_quantizationtables(struct w9968cf_device* cam)
1151{
1152 u16 a, b;
1153 int err = 0, i, j;
1154
1155 err += w9968cf_write_reg(cam, 0x0010, 0x39); /* JPEG clock enable */
1156
1157 for (i = 0, j = 0; i < 32; i++, j += 2) {
1158 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j+1]) << 8);
1159 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j+1]) << 8);
1160 err += w9968cf_write_reg(cam, a, 0x40+i);
1161 err += w9968cf_write_reg(cam, b, 0x60+i);
1162 }
1163 err += w9968cf_write_reg(cam, 0x0012, 0x39); /* JPEG encoder enable */
1164
1165 return err;
1166}
1167
1168
1169
1170/****************************************************************************
1171 * Low-level I2C I/O functions. *
1172 * The adapter supports the following I2C transfer functions: *
1173 * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
1174 * i2c_adap_read_byte_data() *
1175 * i2c_adap_read_byte() *
1176 ****************************************************************************/
1177
1178static int w9968cf_smbus_start(struct w9968cf_device* cam)
1179{
1180 int err = 0;
1181
1182 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1183 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1184
1185 return err;
1186}
1187
1188
1189static int w9968cf_smbus_stop(struct w9968cf_device* cam)
1190{
1191 int err = 0;
1192
1193 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1194 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1195
1196 return err;
1197}
1198
1199
1200static int w9968cf_smbus_write_byte(struct w9968cf_device* cam, u8 v)
1201{
1202 u8 bit;
1203 int err = 0, sda;
1204
1205 for (bit = 0 ; bit < 8 ; bit++) {
1206 sda = (v & 0x80) ? 2 : 0;
1207 v <<= 1;
1208 /* SDE=1, SDA=sda, SCL=0 */
1209 err += w9968cf_write_sb(cam, 0x10 | sda);
1210 /* SDE=1, SDA=sda, SCL=1 */
1211 err += w9968cf_write_sb(cam, 0x11 | sda);
1212 /* SDE=1, SDA=sda, SCL=0 */
1213 err += w9968cf_write_sb(cam, 0x10 | sda);
1214 }
1215
1216 return err;
1217}
1218
1219
1220static int w9968cf_smbus_read_byte(struct w9968cf_device* cam, u8* v)
1221{
1222 u8 bit;
1223 int err = 0;
1224
1225 *v = 0;
1226 for (bit = 0 ; bit < 8 ; bit++) {
1227 *v <<= 1;
1228 err += w9968cf_write_sb(cam, 0x0013);
1229 *v |= (w9968cf_read_sb(cam) & 0x0008) ? 1 : 0;
1230 err += w9968cf_write_sb(cam, 0x0012);
1231 }
1232
1233 return err;
1234}
1235
1236
1237static int w9968cf_smbus_write_ack(struct w9968cf_device* cam)
1238{
1239 int err = 0;
1240
1241 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1242 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1243 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1244
1245 return err;
1246}
1247
1248
1249static int w9968cf_smbus_read_ack(struct w9968cf_device* cam)
1250{
1251 int err = 0, sda;
1252
1253 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1254 sda = (w9968cf_read_sb(cam) & 0x08) ? 1 : 0; /* sda = SDA */
1255 err += w9968cf_write_sb(cam, 0x0012); /* SDE=1, SDA=1, SCL=0 */
1256 if (sda < 0)
1257 err += sda;
1258 if (sda == 1) {
1259 DBG(6, "Couldn't receive the ACK")
1260 err += -1;
1261 }
1262
1263 return err;
1264}
1265
1266
1267/* This seems to refresh the communication through the serial bus */
1268static int w9968cf_smbus_refresh_bus(struct w9968cf_device* cam)
1269{
1270 int err = 0, j;
1271
1272 for (j = 1; j <= 10; j++) {
1273 err = w9968cf_write_reg(cam, 0x0020, 0x01);
1274 err += w9968cf_write_reg(cam, 0x0000, 0x01);
1275 if (err)
1276 break;
1277 }
1278
1279 return err;
1280}
1281
1282
1283/* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
1284static int
1285w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device* cam,
1286 u16 address, u8 subaddress,u8 value)
1287{
1288 u16* data = cam->data_buffer;
1289 int err = 0;
1290
1291 err += w9968cf_smbus_refresh_bus(cam);
1292
1293 /* Enable SBUS outputs */
1294 err += w9968cf_write_sb(cam, 0x0020);
1295
1296 data[0] = 0x082f | ((address & 0x80) ? 0x1500 : 0x0);
1297 data[0] |= (address & 0x40) ? 0x4000 : 0x0;
1298 data[1] = 0x2082 | ((address & 0x40) ? 0x0005 : 0x0);
1299 data[1] |= (address & 0x20) ? 0x0150 : 0x0;
1300 data[1] |= (address & 0x10) ? 0x5400 : 0x0;
1301 data[2] = 0x8208 | ((address & 0x08) ? 0x0015 : 0x0);
1302 data[2] |= (address & 0x04) ? 0x0540 : 0x0;
1303 data[2] |= (address & 0x02) ? 0x5000 : 0x0;
1304 data[3] = 0x1d20 | ((address & 0x02) ? 0x0001 : 0x0);
1305 data[3] |= (address & 0x01) ? 0x0054 : 0x0;
1306
1307 err += w9968cf_write_fsb(cam, data);
1308
1309 data[0] = 0x8208 | ((subaddress & 0x80) ? 0x0015 : 0x0);
1310 data[0] |= (subaddress & 0x40) ? 0x0540 : 0x0;
1311 data[0] |= (subaddress & 0x20) ? 0x5000 : 0x0;
1312 data[1] = 0x0820 | ((subaddress & 0x20) ? 0x0001 : 0x0);
1313 data[1] |= (subaddress & 0x10) ? 0x0054 : 0x0;
1314 data[1] |= (subaddress & 0x08) ? 0x1500 : 0x0;
1315 data[1] |= (subaddress & 0x04) ? 0x4000 : 0x0;
1316 data[2] = 0x2082 | ((subaddress & 0x04) ? 0x0005 : 0x0);
1317 data[2] |= (subaddress & 0x02) ? 0x0150 : 0x0;
1318 data[2] |= (subaddress & 0x01) ? 0x5400 : 0x0;
1319 data[3] = 0x001d;
1320
1321 err += w9968cf_write_fsb(cam, data);
1322
1323 data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
1324 data[0] |= (value & 0x40) ? 0x0540 : 0x0;
1325 data[0] |= (value & 0x20) ? 0x5000 : 0x0;
1326 data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
1327 data[1] |= (value & 0x10) ? 0x0054 : 0x0;
1328 data[1] |= (value & 0x08) ? 0x1500 : 0x0;
1329 data[1] |= (value & 0x04) ? 0x4000 : 0x0;
1330 data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
1331 data[2] |= (value & 0x02) ? 0x0150 : 0x0;
1332 data[2] |= (value & 0x01) ? 0x5400 : 0x0;
1333 data[3] = 0xfe1d;
1334
1335 err += w9968cf_write_fsb(cam, data);
1336
1337 /* Disable SBUS outputs */
1338 err += w9968cf_write_sb(cam, 0x0000);
1339
1340 if (!err)
1341 DBG(5, "I2C write byte data done, addr.0x%04X, subaddr.0x%02X "
1342 "value 0x%02X", address, subaddress, value)
1343 else
1344 DBG(5, "I2C write byte data failed, addr.0x%04X, "
1345 "subaddr.0x%02X, value 0x%02X",
1346 address, subaddress, value)
1347
1348 return err;
1349}
1350
1351
1352/* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */
1353static int
1354w9968cf_i2c_adap_read_byte_data(struct w9968cf_device* cam,
1355 u16 address, u8 subaddress,
1356 u8* value)
1357{
1358 int err = 0;
1359
1360 /* Serial data enable */
1361 err += w9968cf_write_sb(cam, 0x0013); /* don't change ! */
1362
1363 err += w9968cf_smbus_start(cam);
1364 err += w9968cf_smbus_write_byte(cam, address);
1365 err += w9968cf_smbus_read_ack(cam);
1366 err += w9968cf_smbus_write_byte(cam, subaddress);
1367 err += w9968cf_smbus_read_ack(cam);
1368 err += w9968cf_smbus_stop(cam);
1369 err += w9968cf_smbus_start(cam);
1370 err += w9968cf_smbus_write_byte(cam, address + 1);
1371 err += w9968cf_smbus_read_ack(cam);
1372 err += w9968cf_smbus_read_byte(cam, value);
1373 err += w9968cf_smbus_write_ack(cam);
1374 err += w9968cf_smbus_stop(cam);
1375
1376 /* Serial data disable */
1377 err += w9968cf_write_sb(cam, 0x0000);
1378
1379 if (!err)
1380 DBG(5, "I2C read byte data done, addr.0x%04X, "
1381 "subaddr.0x%02X, value 0x%02X",
1382 address, subaddress, *value)
1383 else
1384 DBG(5, "I2C read byte data failed, addr.0x%04X, "
1385 "subaddr.0x%02X, wrong value 0x%02X",
1386 address, subaddress, *value)
1387
1388 return err;
1389}
1390
1391
1392/* SMBus protocol: S Addr+1 Rd [A] [Value] NA P */
1393static int
1394w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
1395 u16 address, u8* value)
1396{
1397 int err = 0;
1398
1399 /* Serial data enable */
1400 err += w9968cf_write_sb(cam, 0x0013);
1401
1402 err += w9968cf_smbus_start(cam);
1403 err += w9968cf_smbus_write_byte(cam, address + 1);
1404 err += w9968cf_smbus_read_ack(cam);
1405 err += w9968cf_smbus_read_byte(cam, value);
1406 err += w9968cf_smbus_write_ack(cam);
1407 err += w9968cf_smbus_stop(cam);
1408
1409 /* Serial data disable */
1410 err += w9968cf_write_sb(cam, 0x0000);
1411
1412 if (!err)
1413 DBG(5, "I2C read byte done, addr.0x%04X, "
1414 "value 0x%02X", address, *value)
1415 else
1416 DBG(5, "I2C read byte failed, addr.0x%04X, "
1417 "wrong value 0x%02X", address, *value)
1418
1419 return err;
1420}
1421
1422
1423/* SMBus protocol: S Addr Wr [A] Value [A] P */
1424static int
1425w9968cf_i2c_adap_write_byte(struct w9968cf_device* cam,
1426 u16 address, u8 value)
1427{
1428 DBG(4, "i2c_write_byte() is an unsupported transfer mode")
1429 return -EINVAL;
1430}
1431
1432
1433
1434/****************************************************************************
1435 * I2C interface to kernel *
1436 ****************************************************************************/
1437
1438static int
1439w9968cf_i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1440 unsigned short flags, char read_write, u8 command,
1441 int size, union i2c_smbus_data *data)
1442{
1443 struct w9968cf_device* cam = i2c_get_adapdata(adapter);
1444 u8 i;
1445 int err = 0;
1446
1447 switch (addr) {
1448 case OV6xx0_SID:
1449 case OV7xx0_SID:
1450 break;
1451 default:
1452 DBG(4, "Rejected slave ID 0x%04X", addr)
1453 return -EINVAL;
1454 }
1455
1456 if (size == I2C_SMBUS_BYTE) {
1457 /* Why addr <<= 1? See OVXXX0_SID defines in ovcamchip.h */
1458 addr <<= 1;
1459
1460 if (read_write == I2C_SMBUS_WRITE)
1461 err = w9968cf_i2c_adap_write_byte(cam, addr, command);
1462 else if (read_write == I2C_SMBUS_READ)
1463 err = w9968cf_i2c_adap_read_byte(cam,addr,&data->byte);
1464
1465 } else if (size == I2C_SMBUS_BYTE_DATA) {
1466 addr <<= 1;
1467
1468 if (read_write == I2C_SMBUS_WRITE)
1469 err = w9968cf_i2c_adap_fastwrite_byte_data(cam, addr,
1470 command, data->byte);
1471 else if (read_write == I2C_SMBUS_READ) {
1472 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1473 err = w9968cf_i2c_adap_read_byte_data(cam,addr,
1474 command, &data->byte);
1475 if (err) {
1476 if (w9968cf_smbus_refresh_bus(cam)) {
1477 err = -EIO;
1478 break;
1479 }
1480 } else
1481 break;
1482 }
1483
1484 } else
1485 return -EINVAL;
1486
1487 } else {
1488 DBG(4, "Unsupported I2C transfer mode (%d)", size)
1489 return -EINVAL;
1490 }
1491
1492 return err;
1493}
1494
1495
1496static u32 w9968cf_i2c_func(struct i2c_adapter* adap)
1497{
1498 return I2C_FUNC_SMBUS_READ_BYTE |
1499 I2C_FUNC_SMBUS_READ_BYTE_DATA |
1500 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
1501}
1502
1503
1504static int w9968cf_i2c_attach_inform(struct i2c_client* client)
1505{
1506 struct w9968cf_device* cam = i2c_get_adapdata(client->adapter);
1507 int id = client->driver->id, err = 0;
1508
1509 if (id == I2C_DRIVERID_OVCAMCHIP) {
1510 cam->sensor_client = client;
1511 err = w9968cf_sensor_init(cam);
1512 if (err) {
1513 cam->sensor_client = NULL;
1514 return err;
1515 }
1516 } else {
1517 DBG(4, "Rejected client [%s] with driver [%s]",
1518 client->name, client->driver->driver.name)
1519 return -EINVAL;
1520 }
1521
1522 DBG(5, "I2C attach client [%s] with driver [%s]",
1523 client->name, client->driver->driver.name)
1524
1525 return 0;
1526}
1527
1528
1529static int w9968cf_i2c_detach_inform(struct i2c_client* client)
1530{
1531 struct w9968cf_device* cam = i2c_get_adapdata(client->adapter);
1532
1533 if (cam->sensor_client == client)
1534 cam->sensor_client = NULL;
1535
1536 DBG(5, "I2C detach client [%s]", client->name)
1537
1538 return 0;
1539}
1540
1541
1542static int
1543w9968cf_i2c_control(struct i2c_adapter* adapter, unsigned int cmd,
1544 unsigned long arg)
1545{
1546 return 0;
1547}
1548
1549
1550static int w9968cf_i2c_init(struct w9968cf_device* cam)
1551{
1552 int err = 0;
1553
1554 static struct i2c_algorithm algo = {
1555 .smbus_xfer = w9968cf_i2c_smbus_xfer,
1556 .algo_control = w9968cf_i2c_control,
1557 .functionality = w9968cf_i2c_func,
1558 };
1559
1560 static struct i2c_adapter adap = {
1561 .id = I2C_HW_SMBUS_W9968CF,
1562 .class = I2C_CLASS_CAM_DIGITAL,
1563 .owner = THIS_MODULE,
1564 .client_register = w9968cf_i2c_attach_inform,
1565 .client_unregister = w9968cf_i2c_detach_inform,
1566 .algo = &algo,
1567 };
1568
1569 memcpy(&cam->i2c_adapter, &adap, sizeof(struct i2c_adapter));
1570 strcpy(cam->i2c_adapter.name, "w9968cf");
1571 i2c_set_adapdata(&cam->i2c_adapter, cam);
1572
1573 DBG(6, "Registering I2C adapter with kernel...")
1574
1575 err = i2c_add_adapter(&cam->i2c_adapter);
1576 if (err)
1577 DBG(1, "Failed to register the I2C adapter")
1578 else
1579 DBG(5, "I2C adapter registered")
1580
1581 return err;
1582}
1583
1584
1585
1586/****************************************************************************
1587 * Helper functions *
1588 ****************************************************************************/
1589
1590/*--------------------------------------------------------------------------
1591 Turn on the LED on some webcams. A beep should be heard too.
1592 Return 0 on success, a negative number otherwise.
1593 --------------------------------------------------------------------------*/
1594static int w9968cf_turn_on_led(struct w9968cf_device* cam)
1595{
1596 int err = 0;
1597
1598 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power-down */
1599 err += w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
1600 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
1601 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* serial bus, SDS high */
1602 err += w9968cf_write_reg(cam, 0x0000, 0x01); /* serial bus, SDS low */
1603 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* ..high 'beep-beep' */
1604
1605 if (err)
1606 DBG(2, "Couldn't turn on the LED")
1607
1608 DBG(5, "LED turned on")
1609
1610 return err;
1611}
1612
1613
1614/*--------------------------------------------------------------------------
1615 Write some registers for the device initialization.
1616 This function is called once on open().
1617 Return 0 on success, a negative number otherwise.
1618 --------------------------------------------------------------------------*/
1619static int w9968cf_init_chip(struct w9968cf_device* cam)
1620{
1621 unsigned long hw_bufsize = cam->maxwidth*cam->maxheight*2,
1622 y0 = 0x0000,
1623 u0 = y0 + hw_bufsize/2,
1624 v0 = u0 + hw_bufsize/4,
1625 y1 = v0 + hw_bufsize/4,
1626 u1 = y1 + hw_bufsize/2,
1627 v1 = u1 + hw_bufsize/4;
1628 int err = 0;
1629
1630 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power off */
1631 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* power on */
1632
1633 err += w9968cf_write_reg(cam, 0x405d, 0x03); /* DRAM timings */
1634 err += w9968cf_write_reg(cam, 0x0030, 0x04); /* SDRAM timings */
1635
1636 err += w9968cf_write_reg(cam, y0 & 0xffff, 0x20); /* Y buf.0, low */
1637 err += w9968cf_write_reg(cam, y0 >> 16, 0x21); /* Y buf.0, high */
1638 err += w9968cf_write_reg(cam, u0 & 0xffff, 0x24); /* U buf.0, low */
1639 err += w9968cf_write_reg(cam, u0 >> 16, 0x25); /* U buf.0, high */
1640 err += w9968cf_write_reg(cam, v0 & 0xffff, 0x28); /* V buf.0, low */
1641 err += w9968cf_write_reg(cam, v0 >> 16, 0x29); /* V buf.0, high */
1642
1643 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x22); /* Y buf.1, low */
1644 err += w9968cf_write_reg(cam, y1 >> 16, 0x23); /* Y buf.1, high */
1645 err += w9968cf_write_reg(cam, u1 & 0xffff, 0x26); /* U buf.1, low */
1646 err += w9968cf_write_reg(cam, u1 >> 16, 0x27); /* U buf.1, high */
1647 err += w9968cf_write_reg(cam, v1 & 0xffff, 0x2a); /* V buf.1, low */
1648 err += w9968cf_write_reg(cam, v1 >> 16, 0x2b); /* V buf.1, high */
1649
1650 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x32); /* JPEG buf 0 low */
1651 err += w9968cf_write_reg(cam, y1 >> 16, 0x33); /* JPEG buf 0 high */
1652
1653 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x34); /* JPEG buf 1 low */
1654 err += w9968cf_write_reg(cam, y1 >> 16, 0x35); /* JPEG bug 1 high */
1655
1656 err += w9968cf_write_reg(cam, 0x0000, 0x36);/* JPEG restart interval */
1657 err += w9968cf_write_reg(cam, 0x0804, 0x37);/*JPEG VLE FIFO threshold*/
1658 err += w9968cf_write_reg(cam, 0x0000, 0x38);/* disable hw up-scaling */
1659 err += w9968cf_write_reg(cam, 0x0000, 0x3f); /* JPEG/MCTL test data */
1660
1661 err += w9968cf_set_picture(cam, cam->picture); /* this before */
1662 err += w9968cf_set_window(cam, cam->window);
1663
1664 if (err)
1665 DBG(1, "Chip initialization failed")
1666 else
1667 DBG(5, "Chip successfully initialized")
1668
1669 return err;
1670}
1671
1672
1673/*--------------------------------------------------------------------------
1674 Return non-zero if the palette is supported, 0 otherwise.
1675 --------------------------------------------------------------------------*/
1676static inline u16 w9968cf_valid_palette(u16 palette)
1677{
1678 u8 i = 0;
1679 while (w9968cf_formatlist[i].palette != 0) {
1680 if (palette == w9968cf_formatlist[i].palette)
1681 return palette;
1682 i++;
1683 }
1684 return 0;
1685}
1686
1687
1688/*--------------------------------------------------------------------------
1689 Return the depth corresponding to the given palette.
1690 Palette _must_ be supported !
1691 --------------------------------------------------------------------------*/
1692static inline u16 w9968cf_valid_depth(u16 palette)
1693{
1694 u8 i=0;
1695 while (w9968cf_formatlist[i].palette != palette)
1696 i++;
1697
1698 return w9968cf_formatlist[i].depth;
1699}
1700
1701
1702/*--------------------------------------------------------------------------
1703 Return non-zero if the format requires decompression, 0 otherwise.
1704 --------------------------------------------------------------------------*/
1705static inline u8 w9968cf_need_decompression(u16 palette)
1706{
1707 u8 i = 0;
1708 while (w9968cf_formatlist[i].palette != 0) {
1709 if (palette == w9968cf_formatlist[i].palette)
1710 return w9968cf_formatlist[i].compression;
1711 i++;
1712 }
1713 return 0;
1714}
1715
1716
1717/*--------------------------------------------------------------------------
1718 Change the picture settings of the camera.
1719 Return 0 on success, a negative number otherwise.
1720 --------------------------------------------------------------------------*/
1721static int
1722w9968cf_set_picture(struct w9968cf_device* cam, struct video_picture pict)
1723{
1724 u16 fmt, hw_depth, hw_palette, reg_v = 0x0000;
1725 int err = 0;
1726
1727 /* Make sure we are using a valid depth */
1728 pict.depth = w9968cf_valid_depth(pict.palette);
1729
1730 fmt = pict.palette;
1731
1732 hw_depth = pict.depth; /* depth used by the winbond chip */
1733 hw_palette = pict.palette; /* palette used by the winbond chip */
1734
1735 /* VS & HS polarities */
1736 reg_v = (cam->vs_polarity << 12) | (cam->hs_polarity << 11);
1737
1738 switch (fmt)
1739 {
1740 case VIDEO_PALETTE_UYVY:
1741 reg_v |= 0x0000;
1742 cam->vpp_flag = VPP_NONE;
1743 break;
1744 case VIDEO_PALETTE_YUV422P:
1745 reg_v |= 0x0002;
1746 cam->vpp_flag = VPP_DECOMPRESSION;
1747 break;
1748 case VIDEO_PALETTE_YUV420:
1749 case VIDEO_PALETTE_YUV420P:
1750 reg_v |= 0x0003;
1751 cam->vpp_flag = VPP_DECOMPRESSION;
1752 break;
1753 case VIDEO_PALETTE_YUYV:
1754 case VIDEO_PALETTE_YUV422:
1755 reg_v |= 0x0000;
1756 cam->vpp_flag = VPP_SWAP_YUV_BYTES;
1757 hw_palette = VIDEO_PALETTE_UYVY;
1758 break;
1759 /* Original video is used instead of RGBX palettes.
1760 Software conversion later. */
1761 case VIDEO_PALETTE_GREY:
1762 case VIDEO_PALETTE_RGB555:
1763 case VIDEO_PALETTE_RGB565:
1764 case VIDEO_PALETTE_RGB24:
1765 case VIDEO_PALETTE_RGB32:
1766 reg_v |= 0x0000; /* UYVY 16 bit is used */
1767 hw_depth = 16;
1768 hw_palette = VIDEO_PALETTE_UYVY;
1769 cam->vpp_flag = VPP_UYVY_TO_RGBX;
1770 break;
1771 }
1772
1773 /* NOTE: due to memory issues, it is better to disable the hardware
1774 double buffering during compression */
1775 if (cam->double_buffer && !(cam->vpp_flag & VPP_DECOMPRESSION))
1776 reg_v |= 0x0080;
1777
1778 if (cam->clamping)
1779 reg_v |= 0x0020;
1780
1781 if (cam->filter_type == 1)
1782 reg_v |= 0x0008;
1783 else if (cam->filter_type == 2)
1784 reg_v |= 0x000c;
1785
1786 if ((err = w9968cf_write_reg(cam, reg_v, 0x16)))
1787 goto error;
1788
1789 if ((err = w9968cf_sensor_update_picture(cam, pict)))
1790 goto error;
1791
1792 /* If all went well, update the device data structure */
1793 memcpy(&cam->picture, &pict, sizeof(pict));
1794 cam->hw_depth = hw_depth;
1795 cam->hw_palette = hw_palette;
1796
1797 /* Settings changed, so we clear the frame buffers */
1798 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1799
1800 DBG(4, "Palette is %s, depth is %u bpp",
1801 symbolic(v4l1_plist, pict.palette), pict.depth)
1802
1803 return 0;
1804
1805error:
1806 DBG(1, "Failed to change picture settings")
1807 return err;
1808}
1809
1810
1811/*--------------------------------------------------------------------------
1812 Change the capture area size of the camera.
1813 This function _must_ be called _after_ w9968cf_set_picture().
1814 Return 0 on success, a negative number otherwise.
1815 --------------------------------------------------------------------------*/
1816static int
1817w9968cf_set_window(struct w9968cf_device* cam, struct video_window win)
1818{
1819 u16 x, y, w, h, scx, scy, cw, ch, ax, ay;
1820 unsigned long fw, fh;
1821 struct ovcamchip_window s_win;
1822 int err = 0;
1823
1824 /* Work around to avoid FP arithmetics */
1825 #define __SC(x) ((x) << 10)
1826 #define __UNSC(x) ((x) >> 10)
1827
1828 /* Make sure we are using a supported resolution */
1829 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
1830 (u16*)&win.height)))
1831 goto error;
1832
1833 /* Scaling factors */
1834 fw = __SC(win.width) / cam->maxwidth;
1835 fh = __SC(win.height) / cam->maxheight;
1836
1837 /* Set up the width and height values used by the chip */
1838 if ((win.width > cam->maxwidth) || (win.height > cam->maxheight)) {
1839 cam->vpp_flag |= VPP_UPSCALE;
1840 /* Calculate largest w,h mantaining the same w/h ratio */
1841 w = (fw >= fh) ? cam->maxwidth : __SC(win.width)/fh;
1842 h = (fw >= fh) ? __SC(win.height)/fw : cam->maxheight;
1843 if (w < cam->minwidth) /* just in case */
1844 w = cam->minwidth;
1845 if (h < cam->minheight) /* just in case */
1846 h = cam->minheight;
1847 } else {
1848 cam->vpp_flag &= ~VPP_UPSCALE;
1849 w = win.width;
1850 h = win.height;
1851 }
1852
1853 /* x,y offsets of the cropped area */
1854 scx = cam->start_cropx;
1855 scy = cam->start_cropy;
1856
1857 /* Calculate cropped area manteining the right w/h ratio */
1858 if (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE)) {
1859 cw = (fw >= fh) ? cam->maxwidth : __SC(win.width)/fh;
1860 ch = (fw >= fh) ? __SC(win.height)/fw : cam->maxheight;
1861 } else {
1862 cw = w;
1863 ch = h;
1864 }
1865
1866 /* Setup the window of the sensor */
1867 s_win.format = VIDEO_PALETTE_UYVY;
1868 s_win.width = cam->maxwidth;
1869 s_win.height = cam->maxheight;
1870 s_win.quarter = 0; /* full progressive video */
1871
1872 /* Center it */
1873 s_win.x = (s_win.width - cw) / 2;
1874 s_win.y = (s_win.height - ch) / 2;
1875
1876 /* Clock divisor */
1877 if (cam->clockdiv >= 0)
1878 s_win.clockdiv = cam->clockdiv; /* manual override */
1879 else
1880 switch (cam->sensor) {
1881 case CC_OV6620:
1882 s_win.clockdiv = 0;
1883 break;
1884 case CC_OV6630:
1885 s_win.clockdiv = 0;
1886 break;
1887 case CC_OV76BE:
1888 case CC_OV7610:
1889 case CC_OV7620:
1890 s_win.clockdiv = 0;
1891 break;
1892 default:
1893 s_win.clockdiv = W9968CF_DEF_CLOCKDIVISOR;
1894 }
1895
1896 /* We have to scale win.x and win.y offsets */
1897 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1898 || (cam->vpp_flag & VPP_UPSCALE) ) {
1899 ax = __SC(win.x)/fw;
1900 ay = __SC(win.y)/fh;
1901 } else {
1902 ax = win.x;
1903 ay = win.y;
1904 }
1905
1906 if ((ax + cw) > cam->maxwidth)
1907 ax = cam->maxwidth - cw;
1908
1909 if ((ay + ch) > cam->maxheight)
1910 ay = cam->maxheight - ch;
1911
1912 /* Adjust win.x, win.y */
1913 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1914 || (cam->vpp_flag & VPP_UPSCALE) ) {
1915 win.x = __UNSC(ax*fw);
1916 win.y = __UNSC(ay*fh);
1917 } else {
1918 win.x = ax;
1919 win.y = ay;
1920 }
1921
1922 /* Offsets used by the chip */
1923 x = ax + s_win.x;
1924 y = ay + s_win.y;
1925
1926 /* Go ! */
1927 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_MODE, &s_win)))
1928 goto error;
1929
1930 err += w9968cf_write_reg(cam, scx + x, 0x10);
1931 err += w9968cf_write_reg(cam, scy + y, 0x11);
1932 err += w9968cf_write_reg(cam, scx + x + cw, 0x12);
1933 err += w9968cf_write_reg(cam, scy + y + ch, 0x13);
1934 err += w9968cf_write_reg(cam, w, 0x14);
1935 err += w9968cf_write_reg(cam, h, 0x15);
1936
1937 /* JPEG width & height */
1938 err += w9968cf_write_reg(cam, w, 0x30);
1939 err += w9968cf_write_reg(cam, h, 0x31);
1940
1941 /* Y & UV frame buffer strides (in WORD) */
1942 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1943 err += w9968cf_write_reg(cam, w/2, 0x2c);
1944 err += w9968cf_write_reg(cam, w/4, 0x2d);
1945 } else
1946 err += w9968cf_write_reg(cam, w, 0x2c);
1947
1948 if (err)
1949 goto error;
1950
1951 /* If all went well, update the device data structure */
1952 memcpy(&cam->window, &win, sizeof(win));
1953 cam->hw_width = w;
1954 cam->hw_height = h;
1955
1956 /* Settings changed, so we clear the frame buffers */
1957 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1958
1959 DBG(4, "The capture area is %dx%d, Offset (x,y)=(%u,%u)",
1960 win.width, win.height, win.x, win.y)
1961
1962 PDBGG("x=%u ,y=%u, w=%u, h=%u, ax=%u, ay=%u, s_win.x=%u, s_win.y=%u, "
1963 "cw=%u, ch=%u, win.x=%u, win.y=%u, win.width=%u, win.height=%u",
1964 x, y, w, h, ax, ay, s_win.x, s_win.y, cw, ch, win.x, win.y,
1965 win.width, win.height)
1966
1967 return 0;
1968
1969error:
1970 DBG(1, "Failed to change the capture area size")
1971 return err;
1972}
1973
1974
1975/*--------------------------------------------------------------------------
1976 Adjust the asked values for window width and height.
1977 Return 0 on success, -1 otherwise.
1978 --------------------------------------------------------------------------*/
1979static int
1980w9968cf_adjust_window_size(struct w9968cf_device* cam, u16* width, u16* height)
1981{
1982 u16 maxw, maxh;
1983
1984 if ((*width < cam->minwidth) || (*height < cam->minheight))
1985 return -ERANGE;
1986
1987 maxw = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1988 w9968cf_vpp ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
1989 : cam->maxwidth;
1990 maxh = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1991 w9968cf_vpp ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
1992 : cam->maxheight;
1993
1994 if (*width > maxw)
1995 *width = maxw;
1996 if (*height > maxh)
1997 *height = maxh;
1998
1999 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2000 *width &= ~15L; /* multiple of 16 */
2001 *height &= ~15L;
2002 }
2003
2004 PDBGG("Window size adjusted w=%u, h=%u ", *width, *height)
2005
2006 return 0;
2007}
2008
2009
2010/*--------------------------------------------------------------------------
2011 Initialize the FIFO list of requested frames.
2012 --------------------------------------------------------------------------*/
2013static void w9968cf_init_framelist(struct w9968cf_device* cam)
2014{
2015 u8 i;
2016
2017 for (i = 0; i < cam->nbuffers; i++) {
2018 cam->requested_frame[i] = NULL;
2019 cam->frame[i].queued = 0;
2020 cam->frame[i].status = F_UNUSED;
2021 }
2022}
2023
2024
2025/*--------------------------------------------------------------------------
2026 Add a frame in the FIFO list of requested frames.
2027 This function is called in process context.
2028 --------------------------------------------------------------------------*/
2029static void w9968cf_push_frame(struct w9968cf_device* cam, u8 f_num)
2030{
2031 u8 f;
2032 unsigned long lock_flags;
2033
2034 spin_lock_irqsave(&cam->flist_lock, lock_flags);
2035
2036 for (f=0; cam->requested_frame[f] != NULL; f++);
2037 cam->requested_frame[f] = &cam->frame[f_num];
2038 cam->frame[f_num].queued = 1;
2039 cam->frame[f_num].status = F_UNUSED; /* clear the status */
2040
2041 spin_unlock_irqrestore(&cam->flist_lock, lock_flags);
2042
2043 DBG(6, "Frame #%u pushed into the FIFO list. Position %u", f_num, f)
2044}
2045
2046
2047/*--------------------------------------------------------------------------
2048 Read, store and remove the first pointer in the FIFO list of requested
2049 frames. This function is called in interrupt context.
2050 --------------------------------------------------------------------------*/
2051static void
2052w9968cf_pop_frame(struct w9968cf_device* cam, struct w9968cf_frame_t** framep)
2053{
2054 u8 i;
2055
2056 spin_lock(&cam->flist_lock);
2057
2058 *framep = cam->requested_frame[0];
2059
2060 /* Shift the list of pointers */
2061 for (i = 0; i < cam->nbuffers-1; i++)
2062 cam->requested_frame[i] = cam->requested_frame[i+1];
2063 cam->requested_frame[i] = NULL;
2064
2065 spin_unlock(&cam->flist_lock);
2066
2067 DBG(6,"Popped frame #%d from the list", (*framep)->number)
2068}
2069
2070
2071/*--------------------------------------------------------------------------
2072 High-level video post-processing routine on grabbed frames.
2073 Return 0 on success, a negative number otherwise.
2074 --------------------------------------------------------------------------*/
2075static int
2076w9968cf_postprocess_frame(struct w9968cf_device* cam,
2077 struct w9968cf_frame_t* fr)
2078{
2079 void *pIn = fr->buffer, *pOut = cam->frame_vpp.buffer, *tmp;
2080 u16 w = cam->window.width,
2081 h = cam->window.height,
2082 d = cam->picture.depth,
2083 fmt = cam->picture.palette,
2084 rgb = cam->force_rgb,
2085 hw_w = cam->hw_width,
2086 hw_h = cam->hw_height,
2087 hw_d = cam->hw_depth;
2088 int err = 0;
2089
2090 #define _PSWAP(pIn, pOut) {tmp = (pIn); (pIn) = (pOut); (pOut) = tmp;}
2091
2092 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2093 memcpy(pOut, pIn, fr->length);
2094 _PSWAP(pIn, pOut)
2095 err = w9968cf_vpp->decode(pIn, fr->length, hw_w, hw_h, pOut);
2096 PDBGG("Compressed frame length: %lu",(unsigned long)fr->length)
2097 fr->length = (hw_w*hw_h*hw_d)/8;
2098 _PSWAP(pIn, pOut)
2099 if (err) {
2100 DBG(4, "An error occurred while decoding the frame: "
2101 "%s", symbolic(decoder_errlist, err))
2102 return err;
2103 } else
2104 DBG(6, "Frame decoded")
2105 }
2106
2107 if (cam->vpp_flag & VPP_SWAP_YUV_BYTES) {
2108 w9968cf_vpp->swap_yuvbytes(pIn, fr->length);
2109 DBG(6, "Original UYVY component ordering changed")
2110 }
2111
2112 if (cam->vpp_flag & VPP_UPSCALE) {
2113 w9968cf_vpp->scale_up(pIn, pOut, hw_w, hw_h, hw_d, w, h);
2114 fr->length = (w*h*hw_d)/8;
2115 _PSWAP(pIn, pOut)
2116 DBG(6, "Vertical up-scaling done: %u,%u,%ubpp->%u,%u",
2117 hw_w, hw_h, hw_d, w, h)
2118 }
2119
2120 if (cam->vpp_flag & VPP_UYVY_TO_RGBX) {
2121 w9968cf_vpp->uyvy_to_rgbx(pIn, fr->length, pOut, fmt, rgb);
2122 fr->length = (w*h*d)/8;
2123 _PSWAP(pIn, pOut)
2124 DBG(6, "UYVY-16bit to %s conversion done",
2125 symbolic(v4l1_plist, fmt))
2126 }
2127
2128 if (pOut == fr->buffer)
2129 memcpy(fr->buffer, cam->frame_vpp.buffer, fr->length);
2130
2131 return 0;
2132}
2133
2134
2135
2136/****************************************************************************
2137 * Image sensor control routines *
2138 ****************************************************************************/
2139
2140static int
2141w9968cf_sensor_set_control(struct w9968cf_device* cam, int cid, int val)
2142{
2143 struct ovcamchip_control ctl;
2144 int err;
2145
2146 ctl.id = cid;
2147 ctl.value = val;
2148
2149 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ctl);
2150
2151 return err;
2152}
2153
2154
2155static int
2156w9968cf_sensor_get_control(struct w9968cf_device* cam, int cid, int* val)
2157{
2158 struct ovcamchip_control ctl;
2159 int err;
2160
2161 ctl.id = cid;
2162
2163 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ctl);
2164 if (!err)
2165 *val = ctl.value;
2166
2167 return err;
2168}
2169
2170
2171static int
2172w9968cf_sensor_cmd(struct w9968cf_device* cam, unsigned int cmd, void* arg)
2173{
2174 struct i2c_client* c = cam->sensor_client;
2175 int rc = 0;
2176
2177 if (!c || !c->driver || !c->driver->command)
2178 return -EINVAL;
2179
2180 rc = c->driver->command(c, cmd, arg);
2181 /* The I2C driver returns -EPERM on non-supported controls */
2182 return (rc < 0 && rc != -EPERM) ? rc : 0;
2183}
2184
2185
2186/*--------------------------------------------------------------------------
2187 Update some settings of the image sensor.
2188 Returns: 0 on success, a negative number otherwise.
2189 --------------------------------------------------------------------------*/
2190static int w9968cf_sensor_update_settings(struct w9968cf_device* cam)
2191{
2192 int err = 0;
2193
2194 /* Auto brightness */
2195 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOBRIGHT,
2196 cam->auto_brt);
2197 if (err)
2198 return err;
2199
2200 /* Auto exposure */
2201 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOEXP,
2202 cam->auto_exp);
2203 if (err)
2204 return err;
2205
2206 /* Banding filter */
2207 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BANDFILT,
2208 cam->bandfilt);
2209 if (err)
2210 return err;
2211
2212 /* Light frequency */
2213 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_FREQ,
2214 cam->lightfreq);
2215 if (err)
2216 return err;
2217
2218 /* Back light */
2219 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BACKLIGHT,
2220 cam->backlight);
2221 if (err)
2222 return err;
2223
2224 /* Mirror */
2225 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_MIRROR,
2226 cam->mirror);
2227 if (err)
2228 return err;
2229
2230 return 0;
2231}
2232
2233
2234/*--------------------------------------------------------------------------
2235 Get some current picture settings from the image sensor and update the
2236 internal 'picture' structure of the camera.
2237 Returns: 0 on success, a negative number otherwise.
2238 --------------------------------------------------------------------------*/
2239static int w9968cf_sensor_get_picture(struct w9968cf_device* cam)
2240{
2241 int err, v;
2242
2243 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_CONT, &v);
2244 if (err)
2245 return err;
2246 cam->picture.contrast = v;
2247
2248 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_BRIGHT, &v);
2249 if (err)
2250 return err;
2251 cam->picture.brightness = v;
2252
2253 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_SAT, &v);
2254 if (err)
2255 return err;
2256 cam->picture.colour = v;
2257
2258 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_HUE, &v);
2259 if (err)
2260 return err;
2261 cam->picture.hue = v;
2262
2263 DBG(5, "Got picture settings from the image sensor")
2264
2265 PDBGG("Brightness, contrast, hue, colour, whiteness are "
2266 "%u,%u,%u,%u,%u", cam->picture.brightness,cam->picture.contrast,
2267 cam->picture.hue, cam->picture.colour, cam->picture.whiteness)
2268
2269 return 0;
2270}
2271
2272
2273/*--------------------------------------------------------------------------
2274 Update picture settings of the image sensor.
2275 Returns: 0 on success, a negative number otherwise.
2276 --------------------------------------------------------------------------*/
2277static int
2278w9968cf_sensor_update_picture(struct w9968cf_device* cam,
2279 struct video_picture pict)
2280{
2281 int err = 0;
2282
2283 if ((!cam->sensor_initialized)
2284 || pict.contrast != cam->picture.contrast) {
2285 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_CONT,
2286 pict.contrast);
2287 if (err)
2288 goto fail;
2289 DBG(4, "Contrast changed from %u to %u",
2290 cam->picture.contrast, pict.contrast)
2291 cam->picture.contrast = pict.contrast;
2292 }
2293
2294 if (((!cam->sensor_initialized) ||
2295 pict.brightness != cam->picture.brightness) && (!cam->auto_brt)) {
2296 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BRIGHT,
2297 pict.brightness);
2298 if (err)
2299 goto fail;
2300 DBG(4, "Brightness changed from %u to %u",
2301 cam->picture.brightness, pict.brightness)
2302 cam->picture.brightness = pict.brightness;
2303 }
2304
2305 if ((!cam->sensor_initialized) || pict.colour != cam->picture.colour) {
2306 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_SAT,
2307 pict.colour);
2308 if (err)
2309 goto fail;
2310 DBG(4, "Colour changed from %u to %u",
2311 cam->picture.colour, pict.colour)
2312 cam->picture.colour = pict.colour;
2313 }
2314
2315 if ((!cam->sensor_initialized) || pict.hue != cam->picture.hue) {
2316 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_HUE,
2317 pict.hue);
2318 if (err)
2319 goto fail;
2320 DBG(4, "Hue changed from %u to %u",
2321 cam->picture.hue, pict.hue)
2322 cam->picture.hue = pict.hue;
2323 }
2324
2325 return 0;
2326
2327fail:
2328 DBG(4, "Failed to change sensor picture setting")
2329 return err;
2330}
2331
2332
2333
2334/****************************************************************************
2335 * Camera configuration *
2336 ****************************************************************************/
2337
2338/*--------------------------------------------------------------------------
2339 This function is called when a supported image sensor is detected.
2340 Return 0 if the initialization succeeds, a negative number otherwise.
2341 --------------------------------------------------------------------------*/
2342static int w9968cf_sensor_init(struct w9968cf_device* cam)
2343{
2344 int err = 0;
2345
2346 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_INITIALIZE,
2347 &cam->monochrome)))
2348 goto error;
2349
2350 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE,
2351 &cam->sensor)))
2352 goto error;
2353
2354 /* NOTE: Make sure width and height are a multiple of 16 */
2355 switch (cam->sensor_client->addr) {
2356 case OV6xx0_SID:
2357 cam->maxwidth = 352;
2358 cam->maxheight = 288;
2359 cam->minwidth = 64;
2360 cam->minheight = 48;
2361 break;
2362 case OV7xx0_SID:
2363 cam->maxwidth = 640;
2364 cam->maxheight = 480;
2365 cam->minwidth = 64;
2366 cam->minheight = 48;
2367 break;
2368 default:
2369 DBG(1, "Not supported image sensor detected for %s",
2370 symbolic(camlist, cam->id))
2371 return -EINVAL;
2372 }
2373
2374 /* These values depend on the ones in the ovxxx0.c sources */
2375 switch (cam->sensor) {
2376 case CC_OV7620:
2377 cam->start_cropx = 287;
2378 cam->start_cropy = 35;
2379 /* Seems to work around a bug in the image sensor */
2380 cam->vs_polarity = 1;
2381 cam->hs_polarity = 1;
2382 break;
2383 default:
2384 cam->start_cropx = 320;
2385 cam->start_cropy = 35;
2386 cam->vs_polarity = 1;
2387 cam->hs_polarity = 0;
2388 }
2389
2390 if ((err = w9968cf_sensor_update_settings(cam)))
2391 goto error;
2392
2393 if ((err = w9968cf_sensor_update_picture(cam, cam->picture)))
2394 goto error;
2395
2396 cam->sensor_initialized = 1;
2397
2398 DBG(2, "%s image sensor initialized", symbolic(senlist, cam->sensor))
2399 return 0;
2400
2401error:
2402 cam->sensor_initialized = 0;
2403 cam->sensor = CC_UNKNOWN;
2404 DBG(1, "Image sensor initialization failed for %s (/dev/video%d). "
2405 "Try to detach and attach this device again",
2406 symbolic(camlist, cam->id), cam->v4ldev->minor)
2407 return err;
2408}
2409
2410
2411/*--------------------------------------------------------------------------
2412 Fill some basic fields in the main device data structure.
2413 This function is called once on w9968cf_usb_probe() for each recognized
2414 camera.
2415 --------------------------------------------------------------------------*/
2416static void
2417w9968cf_configure_camera(struct w9968cf_device* cam,
2418 struct usb_device* udev,
2419 enum w9968cf_model_id mod_id,
2420 const unsigned short dev_nr)
2421{
2422 init_MUTEX(&cam->fileop_sem);
2423 init_waitqueue_head(&cam->open);
2424 spin_lock_init(&cam->urb_lock);
2425 spin_lock_init(&cam->flist_lock);
2426
2427 cam->users = 0;
2428 cam->disconnected = 0;
2429 cam->id = mod_id;
2430 cam->sensor = CC_UNKNOWN;
2431 cam->sensor_initialized = 0;
2432
2433 /* Calculate the alternate setting number (from 1 to 16)
2434 according to the 'packet_size' module parameter */
2435 if (packet_size[dev_nr] < W9968CF_MIN_PACKET_SIZE)
2436 packet_size[dev_nr] = W9968CF_MIN_PACKET_SIZE;
2437 for (cam->altsetting = 1;
2438 packet_size[dev_nr] < wMaxPacketSize[cam->altsetting-1];
2439 cam->altsetting++);
2440
2441 cam->max_buffers = (max_buffers[dev_nr] < 2 ||
2442 max_buffers[dev_nr] > W9968CF_MAX_BUFFERS)
2443 ? W9968CF_BUFFERS : (u8)max_buffers[dev_nr];
2444
2445 cam->double_buffer = (double_buffer[dev_nr] == 0 ||
2446 double_buffer[dev_nr] == 1)
2447 ? (u8)double_buffer[dev_nr]:W9968CF_DOUBLE_BUFFER;
2448
2449 cam->clamping = (clamping[dev_nr] == 0 || clamping[dev_nr] == 1)
2450 ? (u8)clamping[dev_nr] : W9968CF_CLAMPING;
2451
2452 cam->filter_type = (filter_type[dev_nr] == 0 ||
2453 filter_type[dev_nr] == 1 ||
2454 filter_type[dev_nr] == 2)
2455 ? (u8)filter_type[dev_nr] : W9968CF_FILTER_TYPE;
2456
2457 cam->capture = 1;
2458
2459 cam->largeview = (largeview[dev_nr] == 0 || largeview[dev_nr] == 1)
2460 ? (u8)largeview[dev_nr] : W9968CF_LARGEVIEW;
2461
2462 cam->decompression = (decompression[dev_nr] == 0 ||
2463 decompression[dev_nr] == 1 ||
2464 decompression[dev_nr] == 2)
2465 ? (u8)decompression[dev_nr]:W9968CF_DECOMPRESSION;
2466
2467 cam->upscaling = (upscaling[dev_nr] == 0 ||
2468 upscaling[dev_nr] == 1)
2469 ? (u8)upscaling[dev_nr] : W9968CF_UPSCALING;
2470
2471 cam->auto_brt = (autobright[dev_nr] == 0 || autobright[dev_nr] == 1)
2472 ? (u8)autobright[dev_nr] : W9968CF_AUTOBRIGHT;
2473
2474 cam->auto_exp = (autoexp[dev_nr] == 0 || autoexp[dev_nr] == 1)
2475 ? (u8)autoexp[dev_nr] : W9968CF_AUTOEXP;
2476
2477 cam->lightfreq = (lightfreq[dev_nr] == 50 || lightfreq[dev_nr] == 60)
2478 ? (u8)lightfreq[dev_nr] : W9968CF_LIGHTFREQ;
2479
2480 cam->bandfilt = (bandingfilter[dev_nr] == 0 ||
2481 bandingfilter[dev_nr] == 1)
2482 ? (u8)bandingfilter[dev_nr] : W9968CF_BANDINGFILTER;
2483
2484 cam->backlight = (backlight[dev_nr] == 0 || backlight[dev_nr] == 1)
2485 ? (u8)backlight[dev_nr] : W9968CF_BACKLIGHT;
2486
2487 cam->clockdiv = (clockdiv[dev_nr] == -1 || clockdiv[dev_nr] >= 0)
2488 ? (s8)clockdiv[dev_nr] : W9968CF_CLOCKDIV;
2489
2490 cam->mirror = (mirror[dev_nr] == 0 || mirror[dev_nr] == 1)
2491 ? (u8)mirror[dev_nr] : W9968CF_MIRROR;
2492
2493 cam->monochrome = (monochrome[dev_nr] == 0 || monochrome[dev_nr] == 1)
2494 ? monochrome[dev_nr] : W9968CF_MONOCHROME;
2495
2496 cam->picture.brightness = (u16)brightness[dev_nr];
2497 cam->picture.hue = (u16)hue[dev_nr];
2498 cam->picture.colour = (u16)colour[dev_nr];
2499 cam->picture.contrast = (u16)contrast[dev_nr];
2500 cam->picture.whiteness = (u16)whiteness[dev_nr];
2501 if (w9968cf_valid_palette((u16)force_palette[dev_nr])) {
2502 cam->picture.palette = (u16)force_palette[dev_nr];
2503 cam->force_palette = 1;
2504 } else {
2505 cam->force_palette = 0;
2506 if (cam->decompression == 0)
2507 cam->picture.palette = W9968CF_PALETTE_DECOMP_OFF;
2508 else if (cam->decompression == 1)
2509 cam->picture.palette = W9968CF_PALETTE_DECOMP_FORCE;
2510 else
2511 cam->picture.palette = W9968CF_PALETTE_DECOMP_ON;
2512 }
2513 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2514
2515 cam->force_rgb = (force_rgb[dev_nr] == 0 || force_rgb[dev_nr] == 1)
2516 ? (u8)force_rgb[dev_nr] : W9968CF_FORCE_RGB;
2517
2518 cam->window.x = 0;
2519 cam->window.y = 0;
2520 cam->window.width = W9968CF_WIDTH;
2521 cam->window.height = W9968CF_HEIGHT;
2522 cam->window.chromakey = 0;
2523 cam->window.clipcount = 0;
2524 cam->window.flags = 0;
2525
2526 DBG(3, "%s configured with settings #%u:",
2527 symbolic(camlist, cam->id), dev_nr)
2528
2529 DBG(3, "- Data packet size for USB isochrnous transfer: %u bytes",
2530 wMaxPacketSize[cam->altsetting-1])
2531
2532 DBG(3, "- Number of requested video frame buffers: %u",
2533 cam->max_buffers)
2534
2535 if (cam->double_buffer)
2536 DBG(3, "- Hardware double buffering enabled")
2537 else
2538 DBG(3, "- Hardware double buffering disabled")
2539
2540 if (cam->filter_type == 0)
2541 DBG(3, "- Video filtering disabled")
2542 else if (cam->filter_type == 1)
2543 DBG(3, "- Video filtering enabled: type 1-2-1")
2544 else if (cam->filter_type == 2)
2545 DBG(3, "- Video filtering enabled: type 2-3-6-3-2")
2546
2547 if (cam->clamping)
2548 DBG(3, "- Video data clamping (CCIR-601 format) enabled")
2549 else
2550 DBG(3, "- Video data clamping (CCIR-601 format) disabled")
2551
2552 if (cam->largeview)
2553 DBG(3, "- Large view enabled")
2554 else
2555 DBG(3, "- Large view disabled")
2556
2557 if ((cam->decompression) == 0 && (!cam->force_palette))
2558 DBG(3, "- Decompression disabled")
2559 else if ((cam->decompression) == 1 && (!cam->force_palette))
2560 DBG(3, "- Decompression forced")
2561 else if ((cam->decompression) == 2 && (!cam->force_palette))
2562 DBG(3, "- Decompression allowed")
2563
2564 if (cam->upscaling)
2565 DBG(3, "- Software image scaling enabled")
2566 else
2567 DBG(3, "- Software image scaling disabled")
2568
2569 if (cam->force_palette)
2570 DBG(3, "- Image palette forced to %s",
2571 symbolic(v4l1_plist, cam->picture.palette))
2572
2573 if (cam->force_rgb)
2574 DBG(3, "- RGB component ordering will be used instead of BGR")
2575
2576 if (cam->auto_brt)
2577 DBG(3, "- Auto brightness enabled")
2578 else
2579 DBG(3, "- Auto brightness disabled")
2580
2581 if (cam->auto_exp)
2582 DBG(3, "- Auto exposure enabled")
2583 else
2584 DBG(3, "- Auto exposure disabled")
2585
2586 if (cam->backlight)
2587 DBG(3, "- Backlight exposure algorithm enabled")
2588 else
2589 DBG(3, "- Backlight exposure algorithm disabled")
2590
2591 if (cam->mirror)
2592 DBG(3, "- Mirror enabled")
2593 else
2594 DBG(3, "- Mirror disabled")
2595
2596 if (cam->bandfilt)
2597 DBG(3, "- Banding filter enabled")
2598 else
2599 DBG(3, "- Banding filter disabled")
2600
2601 DBG(3, "- Power lighting frequency: %u", cam->lightfreq)
2602
2603 if (cam->clockdiv == -1)
2604 DBG(3, "- Automatic clock divisor enabled")
2605 else
2606 DBG(3, "- Clock divisor: %d", cam->clockdiv)
2607
2608 if (cam->monochrome)
2609 DBG(3, "- Image sensor used as monochrome")
2610 else
2611 DBG(3, "- Image sensor not used as monochrome")
2612}
2613
2614
2615/*--------------------------------------------------------------------------
2616 If the video post-processing module is not loaded, some parameters
2617 must be overridden.
2618 --------------------------------------------------------------------------*/
2619static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2620{
2621 if (!w9968cf_vpp) {
2622 if (cam->decompression == 1) {
2623 cam->decompression = 2;
2624 DBG(2, "Video post-processing module not found: "
2625 "'decompression' parameter forced to 2")
2626 }
2627 if (cam->upscaling) {
2628 cam->upscaling = 0;
2629 DBG(2, "Video post-processing module not found: "
2630 "'upscaling' parameter forced to 0")
2631 }
2632 if (cam->picture.palette != VIDEO_PALETTE_UYVY) {
2633 cam->force_palette = 0;
2634 DBG(2, "Video post-processing module not found: "
2635 "'force_palette' parameter forced to 0")
2636 }
2637 cam->picture.palette = VIDEO_PALETTE_UYVY;
2638 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2639 }
2640}
2641
2642
2643/*--------------------------------------------------------------------------
2644 Release the resources used by the driver.
2645 This function is called on disconnect
2646 (or on close if deallocation has been deferred)
2647 --------------------------------------------------------------------------*/
2648static void w9968cf_release_resources(struct w9968cf_device* cam)
2649{
2650 down(&w9968cf_devlist_sem);
2651
2652 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->minor)
2653
2654 video_unregister_device(cam->v4ldev);
2655 list_del(&cam->v4llist);
2656 i2c_del_adapter(&cam->i2c_adapter);
2657 w9968cf_deallocate_memory(cam);
2658 kfree(cam->control_buffer);
2659 kfree(cam->data_buffer);
2660
2661 up(&w9968cf_devlist_sem);
2662}
2663
2664
2665
2666/****************************************************************************
2667 * Video4Linux interface *
2668 ****************************************************************************/
2669
2670static int w9968cf_open(struct inode* inode, struct file* filp)
2671{
2672 struct w9968cf_device* cam;
2673 int err;
2674
2675 /* This the only safe way to prevent race conditions with disconnect */
2676 if (!down_read_trylock(&w9968cf_disconnect))
2677 return -ERESTARTSYS;
2678
2679 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2680
2681 down(&cam->dev_sem);
2682
2683 if (cam->sensor == CC_UNKNOWN) {
2684 DBG(2, "No supported image sensor has been detected by the "
2685 "'ovcamchip' module for the %s (/dev/video%d). Make "
2686 "sure it is loaded *before* (re)connecting the camera.",
2687 symbolic(camlist, cam->id), cam->v4ldev->minor)
2688 up(&cam->dev_sem);
2689 up_read(&w9968cf_disconnect);
2690 return -ENODEV;
2691 }
2692
2693 if (cam->users) {
2694 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
2695 symbolic(camlist, cam->id),cam->v4ldev->minor,cam->command)
2696 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
2697 up(&cam->dev_sem);
2698 up_read(&w9968cf_disconnect);
2699 return -EWOULDBLOCK;
2700 }
2701 up(&cam->dev_sem);
2702 err = wait_event_interruptible_exclusive(cam->open,
2703 cam->disconnected ||
2704 !cam->users);
2705 if (err) {
2706 up_read(&w9968cf_disconnect);
2707 return err;
2708 }
2709 if (cam->disconnected) {
2710 up_read(&w9968cf_disconnect);
2711 return -ENODEV;
2712 }
2713 down(&cam->dev_sem);
2714 }
2715
2716 DBG(5, "Opening '%s', /dev/video%d ...",
2717 symbolic(camlist, cam->id), cam->v4ldev->minor)
2718
2719 cam->streaming = 0;
2720 cam->misconfigured = 0;
2721
2722 w9968cf_adjust_configuration(cam);
2723
2724 if ((err = w9968cf_allocate_memory(cam)))
2725 goto deallocate_memory;
2726
2727 if ((err = w9968cf_init_chip(cam)))
2728 goto deallocate_memory;
2729
2730 if ((err = w9968cf_start_transfer(cam)))
2731 goto deallocate_memory;
2732
2733 filp->private_data = cam;
2734
2735 cam->users++;
2736 strcpy(cam->command, current->comm);
2737
2738 init_waitqueue_head(&cam->wait_queue);
2739
2740 DBG(5, "Video device is open")
2741
2742 up(&cam->dev_sem);
2743 up_read(&w9968cf_disconnect);
2744
2745 return 0;
2746
2747deallocate_memory:
2748 w9968cf_deallocate_memory(cam);
2749 DBG(2, "Failed to open the video device")
2750 up(&cam->dev_sem);
2751 up_read(&w9968cf_disconnect);
2752 return err;
2753}
2754
2755
2756static int w9968cf_release(struct inode* inode, struct file* filp)
2757{
2758 struct w9968cf_device* cam;
2759
2760 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2761
2762 down(&cam->dev_sem); /* prevent disconnect() to be called */
2763
2764 w9968cf_stop_transfer(cam);
2765
2766 if (cam->disconnected) {
2767 w9968cf_release_resources(cam);
2768 up(&cam->dev_sem);
2769 kfree(cam);
2770 return 0;
2771 }
2772
2773 cam->users--;
2774 w9968cf_deallocate_memory(cam);
2775 wake_up_interruptible_nr(&cam->open, 1);
2776
2777 DBG(5, "Video device closed")
2778 up(&cam->dev_sem);
2779 return 0;
2780}
2781
2782
2783static ssize_t
2784w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2785{
2786 struct w9968cf_device* cam;
2787 struct w9968cf_frame_t* fr;
2788 int err = 0;
2789
2790 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2791
2792 if (filp->f_flags & O_NONBLOCK)
2793 return -EWOULDBLOCK;
2794
2795 if (down_interruptible(&cam->fileop_sem))
2796 return -ERESTARTSYS;
2797
2798 if (cam->disconnected) {
2799 DBG(2, "Device not present")
2800 up(&cam->fileop_sem);
2801 return -ENODEV;
2802 }
2803
2804 if (cam->misconfigured) {
2805 DBG(2, "The camera is misconfigured. Close and open it again.")
2806 up(&cam->fileop_sem);
2807 return -EIO;
2808 }
2809
2810 if (!cam->frame[0].queued)
2811 w9968cf_push_frame(cam, 0);
2812
2813 if (!cam->frame[1].queued)
2814 w9968cf_push_frame(cam, 1);
2815
2816 err = wait_event_interruptible(cam->wait_queue,
2817 cam->frame[0].status == F_READY ||
2818 cam->frame[1].status == F_READY ||
2819 cam->disconnected);
2820 if (err) {
2821 up(&cam->fileop_sem);
2822 return err;
2823 }
2824 if (cam->disconnected) {
2825 up(&cam->fileop_sem);
2826 return -ENODEV;
2827 }
2828
2829 fr = (cam->frame[0].status == F_READY) ? &cam->frame[0]:&cam->frame[1];
2830
2831 if (w9968cf_vpp)
2832 w9968cf_postprocess_frame(cam, fr);
2833
2834 if (count > fr->length)
2835 count = fr->length;
2836
2837 if (copy_to_user(buf, fr->buffer, count)) {
2838 fr->status = F_UNUSED;
2839 up(&cam->fileop_sem);
2840 return -EFAULT;
2841 }
2842 *f_pos += count;
2843
2844 fr->status = F_UNUSED;
2845
2846 DBG(5, "%zu bytes read", count)
2847
2848 up(&cam->fileop_sem);
2849 return count;
2850}
2851
2852
2853static int w9968cf_mmap(struct file* filp, struct vm_area_struct *vma)
2854{
2855 struct w9968cf_device* cam = (struct w9968cf_device*)
2856 video_get_drvdata(video_devdata(filp));
2857 unsigned long vsize = vma->vm_end - vma->vm_start,
2858 psize = cam->nbuffers * cam->frame[0].size,
2859 start = vma->vm_start,
2860 pos = (unsigned long)cam->frame[0].buffer,
2861 page;
2862
2863 if (cam->disconnected) {
2864 DBG(2, "Device not present")
2865 return -ENODEV;
2866 }
2867
2868 if (cam->misconfigured) {
2869 DBG(2, "The camera is misconfigured. Close and open it again")
2870 return -EIO;
2871 }
2872
2873 PDBGG("mmapping %lu bytes...", vsize)
2874
2875 if (vsize > psize - (vma->vm_pgoff << PAGE_SHIFT))
2876 return -EINVAL;
2877
2878 while (vsize > 0) {
2879 page = vmalloc_to_pfn((void *)pos);
2880 if (remap_pfn_range(vma, start, page + vma->vm_pgoff,
2881 PAGE_SIZE, vma->vm_page_prot))
2882 return -EAGAIN;
2883 start += PAGE_SIZE;
2884 pos += PAGE_SIZE;
2885 vsize -= PAGE_SIZE;
2886 }
2887
2888 DBG(5, "mmap method successfully called")
2889 return 0;
2890}
2891
2892
2893static int
2894w9968cf_ioctl(struct inode* inode, struct file* filp,
2895 unsigned int cmd, unsigned long arg)
2896{
2897 struct w9968cf_device* cam;
2898 int err;
2899
2900 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2901
2902 if (down_interruptible(&cam->fileop_sem))
2903 return -ERESTARTSYS;
2904
2905 if (cam->disconnected) {
2906 DBG(2, "Device not present")
2907 up(&cam->fileop_sem);
2908 return -ENODEV;
2909 }
2910
2911 if (cam->misconfigured) {
2912 DBG(2, "The camera is misconfigured. Close and open it again.")
2913 up(&cam->fileop_sem);
2914 return -EIO;
2915 }
2916
2917 err = w9968cf_v4l_ioctl(inode, filp, cmd, (void __user *)arg);
2918
2919 up(&cam->fileop_sem);
2920 return err;
2921}
2922
2923
2924static int w9968cf_v4l_ioctl(struct inode* inode, struct file* filp,
2925 unsigned int cmd, void __user * arg)
2926{
2927 struct w9968cf_device* cam;
2928 const char* v4l1_ioctls[] = {
2929 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER",
2930 "GPICT", "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF",
2931 "SFBUF", "KEY", "GFREQ", "SFREQ", "GAUDIO", "SAUDIO",
2932 "SYNC", "MCAPTURE", "GMBUF", "GUNIT", "GCAPTURE", "SCAPTURE",
2933 "SPLAYMODE", "SWRITEMODE", "GPLAYINFO", "SMICROCODE",
2934 "GVBIFMT", "SVBIFMT"
2935 };
2936
2937 #define V4L1_IOCTL(cmd) \
2938 ((_IOC_NR((cmd)) < ARRAY_SIZE(v4l1_ioctls)) ? \
2939 v4l1_ioctls[_IOC_NR((cmd))] : "?")
2940
2941 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2942
2943 switch (cmd) {
2944
2945 case VIDIOCGCAP: /* get video capability */
2946 {
2947 struct video_capability cap = {
2948 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
2949 .channels = 1,
2950 .audios = 0,
2951 .minwidth = cam->minwidth,
2952 .minheight = cam->minheight,
2953 };
2954 sprintf(cap.name, "W996[87]CF USB Camera #%d",
2955 cam->v4ldev->minor);
2956 cap.maxwidth = (cam->upscaling && w9968cf_vpp)
2957 ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
2958 : cam->maxwidth;
2959 cap.maxheight = (cam->upscaling && w9968cf_vpp)
2960 ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
2961 : cam->maxheight;
2962
2963 if (copy_to_user(arg, &cap, sizeof(cap)))
2964 return -EFAULT;
2965
2966 DBG(5, "VIDIOCGCAP successfully called")
2967 return 0;
2968 }
2969
2970 case VIDIOCGCHAN: /* get video channel informations */
2971 {
2972 struct video_channel chan;
2973 if (copy_from_user(&chan, arg, sizeof(chan)))
2974 return -EFAULT;
2975
2976 if (chan.channel != 0)
2977 return -EINVAL;
2978
2979 strcpy(chan.name, "Camera");
2980 chan.tuners = 0;
2981 chan.flags = 0;
2982 chan.type = VIDEO_TYPE_CAMERA;
2983 chan.norm = VIDEO_MODE_AUTO;
2984
2985 if (copy_to_user(arg, &chan, sizeof(chan)))
2986 return -EFAULT;
2987
2988 DBG(5, "VIDIOCGCHAN successfully called")
2989 return 0;
2990 }
2991
2992 case VIDIOCSCHAN: /* set active channel */
2993 {
2994 struct video_channel chan;
2995
2996 if (copy_from_user(&chan, arg, sizeof(chan)))
2997 return -EFAULT;
2998
2999 if (chan.channel != 0)
3000 return -EINVAL;
3001
3002 DBG(5, "VIDIOCSCHAN successfully called")
3003 return 0;
3004 }
3005
3006 case VIDIOCGPICT: /* get image properties of the picture */
3007 {
3008 if (w9968cf_sensor_get_picture(cam))
3009 return -EIO;
3010
3011 if (copy_to_user(arg, &cam->picture, sizeof(cam->picture)))
3012 return -EFAULT;
3013
3014 DBG(5, "VIDIOCGPICT successfully called")
3015 return 0;
3016 }
3017
3018 case VIDIOCSPICT: /* change picture settings */
3019 {
3020 struct video_picture pict;
3021 int err = 0;
3022
3023 if (copy_from_user(&pict, arg, sizeof(pict)))
3024 return -EFAULT;
3025
3026 if ( (cam->force_palette || !w9968cf_vpp)
3027 && pict.palette != cam->picture.palette ) {
3028 DBG(4, "Palette %s rejected: only %s is allowed",
3029 symbolic(v4l1_plist, pict.palette),
3030 symbolic(v4l1_plist, cam->picture.palette))
3031 return -EINVAL;
3032 }
3033
3034 if (!w9968cf_valid_palette(pict.palette)) {
3035 DBG(4, "Palette %s not supported. VIDIOCSPICT failed",
3036 symbolic(v4l1_plist, pict.palette))
3037 return -EINVAL;
3038 }
3039
3040 if (!cam->force_palette) {
3041 if (cam->decompression == 0) {
3042 if (w9968cf_need_decompression(pict.palette)) {
3043 DBG(4, "Decompression disabled: palette %s is not "
3044 "allowed. VIDIOCSPICT failed",
3045 symbolic(v4l1_plist, pict.palette))
3046 return -EINVAL;
3047 }
3048 } else if (cam->decompression == 1) {
3049 if (!w9968cf_need_decompression(pict.palette)) {
3050 DBG(4, "Decompression forced: palette %s is not "
3051 "allowed. VIDIOCSPICT failed",
3052 symbolic(v4l1_plist, pict.palette))
3053 return -EINVAL;
3054 }
3055 }
3056 }
3057
3058 if (pict.depth != w9968cf_valid_depth(pict.palette)) {
3059 DBG(4, "Requested depth %u bpp is not valid for %s "
3060 "palette: ignored and changed to %u bpp",
3061 pict.depth, symbolic(v4l1_plist, pict.palette),
3062 w9968cf_valid_depth(pict.palette))
3063 pict.depth = w9968cf_valid_depth(pict.palette);
3064 }
3065
3066 if (pict.palette != cam->picture.palette) {
3067 if(*cam->requested_frame
3068 || cam->frame_current->queued) {
3069 err = wait_event_interruptible
3070 ( cam->wait_queue,
3071 cam->disconnected ||
3072 (!*cam->requested_frame &&
3073 !cam->frame_current->queued) );
3074 if (err)
3075 return err;
3076 if (cam->disconnected)
3077 return -ENODEV;
3078 }
3079
3080 if (w9968cf_stop_transfer(cam))
3081 goto ioctl_fail;
3082
3083 if (w9968cf_set_picture(cam, pict))
3084 goto ioctl_fail;
3085
3086 if (w9968cf_start_transfer(cam))
3087 goto ioctl_fail;
3088
3089 } else if (w9968cf_sensor_update_picture(cam, pict))
3090 return -EIO;
3091
3092
3093 DBG(5, "VIDIOCSPICT successfully called")
3094 return 0;
3095 }
3096
3097 case VIDIOCSWIN: /* set capture area */
3098 {
3099 struct video_window win;
3100 int err = 0;
3101
3102 if (copy_from_user(&win, arg, sizeof(win)))
3103 return -EFAULT;
3104
3105 DBG(6, "VIDIOCSWIN called: clipcount=%d, flags=%u, "
3106 "x=%u, y=%u, %ux%u", win.clipcount, win.flags,
3107 win.x, win.y, win.width, win.height)
3108
3109 if (win.clipcount != 0 || win.flags != 0)
3110 return -EINVAL;
3111
3112 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
3113 (u16*)&win.height))) {
3114 DBG(4, "Resolution not supported (%ux%u). "
3115 "VIDIOCSWIN failed", win.width, win.height)
3116 return err;
3117 }
3118
3119 if (win.x != cam->window.x ||
3120 win.y != cam->window.y ||
3121 win.width != cam->window.width ||
3122 win.height != cam->window.height) {
3123 if(*cam->requested_frame
3124 || cam->frame_current->queued) {
3125 err = wait_event_interruptible
3126 ( cam->wait_queue,
3127 cam->disconnected ||
3128 (!*cam->requested_frame &&
3129 !cam->frame_current->queued) );
3130 if (err)
3131 return err;
3132 if (cam->disconnected)
3133 return -ENODEV;
3134 }
3135
3136 if (w9968cf_stop_transfer(cam))
3137 goto ioctl_fail;
3138
3139 /* This _must_ be called before set_window() */
3140 if (w9968cf_set_picture(cam, cam->picture))
3141 goto ioctl_fail;
3142
3143 if (w9968cf_set_window(cam, win))
3144 goto ioctl_fail;
3145
3146 if (w9968cf_start_transfer(cam))
3147 goto ioctl_fail;
3148 }
3149
3150 DBG(5, "VIDIOCSWIN successfully called. ")
3151 return 0;
3152 }
3153
3154 case VIDIOCGWIN: /* get current window properties */
3155 {
3156 if (copy_to_user(arg,&cam->window,sizeof(struct video_window)))
3157 return -EFAULT;
3158
3159 DBG(5, "VIDIOCGWIN successfully called")
3160 return 0;
3161 }
3162
3163 case VIDIOCGMBUF: /* request for memory (mapped) buffer */
3164 {
3165 struct video_mbuf mbuf;
3166 u8 i;
3167
3168 mbuf.size = cam->nbuffers * cam->frame[0].size;
3169 mbuf.frames = cam->nbuffers;
3170 for (i = 0; i < cam->nbuffers; i++)
3171 mbuf.offsets[i] = (unsigned long)cam->frame[i].buffer -
3172 (unsigned long)cam->frame[0].buffer;
3173
3174 if (copy_to_user(arg, &mbuf, sizeof(mbuf)))
3175 return -EFAULT;
3176
3177 DBG(5, "VIDIOCGMBUF successfully called")
3178 return 0;
3179 }
3180
3181 case VIDIOCMCAPTURE: /* start the capture to a frame */
3182 {
3183 struct video_mmap mmap;
3184 struct w9968cf_frame_t* fr;
3185 int err = 0;
3186
3187 if (copy_from_user(&mmap, arg, sizeof(mmap)))
3188 return -EFAULT;
3189
3190 DBG(6, "VIDIOCMCAPTURE called: frame #%u, format=%s, %dx%d",
3191 mmap.frame, symbolic(v4l1_plist, mmap.format),
3192 mmap.width, mmap.height)
3193
3194 if (mmap.frame >= cam->nbuffers) {
3195 DBG(4, "Invalid frame number (%u). "
3196 "VIDIOCMCAPTURE failed", mmap.frame)
3197 return -EINVAL;
3198 }
3199
3200 if (mmap.format!=cam->picture.palette &&
3201 (cam->force_palette || !w9968cf_vpp)) {
3202 DBG(4, "Palette %s rejected: only %s is allowed",
3203 symbolic(v4l1_plist, mmap.format),
3204 symbolic(v4l1_plist, cam->picture.palette))
3205 return -EINVAL;
3206 }
3207
3208 if (!w9968cf_valid_palette(mmap.format)) {
3209 DBG(4, "Palette %s not supported. "
3210 "VIDIOCMCAPTURE failed",
3211 symbolic(v4l1_plist, mmap.format))
3212 return -EINVAL;
3213 }
3214
3215 if (!cam->force_palette) {
3216 if (cam->decompression == 0) {
3217 if (w9968cf_need_decompression(mmap.format)) {
3218 DBG(4, "Decompression disabled: palette %s is not "
3219 "allowed. VIDIOCSPICT failed",
3220 symbolic(v4l1_plist, mmap.format))
3221 return -EINVAL;
3222 }
3223 } else if (cam->decompression == 1) {
3224 if (!w9968cf_need_decompression(mmap.format)) {
3225 DBG(4, "Decompression forced: palette %s is not "
3226 "allowed. VIDIOCSPICT failed",
3227 symbolic(v4l1_plist, mmap.format))
3228 return -EINVAL;
3229 }
3230 }
3231 }
3232
3233 if ((err = w9968cf_adjust_window_size(cam, (u16*)&mmap.width,
3234 (u16*)&mmap.height))) {
3235 DBG(4, "Resolution not supported (%dx%d). "
3236 "VIDIOCMCAPTURE failed",
3237 mmap.width, mmap.height)
3238 return err;
3239 }
3240
3241 fr = &cam->frame[mmap.frame];
3242
3243 if (mmap.width != cam->window.width ||
3244 mmap.height != cam->window.height ||
3245 mmap.format != cam->picture.palette) {
3246
3247 struct video_window win;
3248 struct video_picture pict;
3249
3250 if(*cam->requested_frame
3251 || cam->frame_current->queued) {
3252 DBG(6, "VIDIOCMCAPTURE. Change settings for "
3253 "frame #%u: %dx%d, format %s. Wait...",
3254 mmap.frame, mmap.width, mmap.height,
3255 symbolic(v4l1_plist, mmap.format))
3256 err = wait_event_interruptible
3257 ( cam->wait_queue,
3258 cam->disconnected ||
3259 (!*cam->requested_frame &&
3260 !cam->frame_current->queued) );
3261 if (err)
3262 return err;
3263 if (cam->disconnected)
3264 return -ENODEV;
3265 }
3266
3267 memcpy(&win, &cam->window, sizeof(win));
3268 memcpy(&pict, &cam->picture, sizeof(pict));
3269 win.width = mmap.width;
3270 win.height = mmap.height;
3271 pict.palette = mmap.format;
3272
3273 if (w9968cf_stop_transfer(cam))
3274 goto ioctl_fail;
3275
3276 /* This before set_window */
3277 if (w9968cf_set_picture(cam, pict))
3278 goto ioctl_fail;
3279
3280 if (w9968cf_set_window(cam, win))
3281 goto ioctl_fail;
3282
3283 if (w9968cf_start_transfer(cam))
3284 goto ioctl_fail;
3285
3286 } else if (fr->queued) {
3287
3288 DBG(6, "Wait until frame #%u is free", mmap.frame)
3289
3290 err = wait_event_interruptible(cam->wait_queue,
3291 cam->disconnected ||
3292 (!fr->queued));
3293 if (err)
3294 return err;
3295 if (cam->disconnected)
3296 return -ENODEV;
3297 }
3298
3299 w9968cf_push_frame(cam, mmap.frame);
3300 DBG(5, "VIDIOCMCAPTURE(%u): successfully called", mmap.frame)
3301 return 0;
3302 }
3303
3304 case VIDIOCSYNC: /* wait until the capture of a frame is finished */
3305 {
3306 unsigned int f_num;
3307 struct w9968cf_frame_t* fr;
3308 int err = 0;
3309
3310 if (copy_from_user(&f_num, arg, sizeof(f_num)))
3311 return -EFAULT;
3312
3313 if (f_num >= cam->nbuffers) {
3314 DBG(4, "Invalid frame number (%u). "
3315 "VIDIOCMCAPTURE failed", f_num)
3316 return -EINVAL;
3317 }
3318
3319 DBG(6, "VIDIOCSYNC called for frame #%u", f_num)
3320
3321 fr = &cam->frame[f_num];
3322
3323 switch (fr->status) {
3324 case F_UNUSED:
3325 if (!fr->queued) {
3326 DBG(4, "VIDIOSYNC: Frame #%u not requested!",
3327 f_num)
3328 return -EFAULT;
3329 }
3330 case F_ERROR:
3331 case F_GRABBING:
3332 err = wait_event_interruptible(cam->wait_queue,
3333 (fr->status == F_READY)
3334 || cam->disconnected);
3335 if (err)
3336 return err;
3337 if (cam->disconnected)
3338 return -ENODEV;
3339 break;
3340 case F_READY:
3341 break;
3342 }
3343
3344 if (w9968cf_vpp)
3345 w9968cf_postprocess_frame(cam, fr);
3346
3347 fr->status = F_UNUSED;
3348
3349 DBG(5, "VIDIOCSYNC(%u) successfully called", f_num)
3350 return 0;
3351 }
3352
3353 case VIDIOCGUNIT:/* report the unit numbers of the associated devices*/
3354 {
3355 struct video_unit unit = {
3356 .video = cam->v4ldev->minor,
3357 .vbi = VIDEO_NO_UNIT,
3358 .radio = VIDEO_NO_UNIT,
3359 .audio = VIDEO_NO_UNIT,
3360 .teletext = VIDEO_NO_UNIT,
3361 };
3362
3363 if (copy_to_user(arg, &unit, sizeof(unit)))
3364 return -EFAULT;
3365
3366 DBG(5, "VIDIOCGUNIT successfully called")
3367 return 0;
3368 }
3369
3370 case VIDIOCKEY:
3371 return 0;
3372
3373 case VIDIOCGFBUF:
3374 {
3375 if (clear_user(arg, sizeof(struct video_buffer)))
3376 return -EFAULT;
3377
3378 DBG(5, "VIDIOCGFBUF successfully called")
3379 return 0;
3380 }
3381
3382 case VIDIOCGTUNER:
3383 {
3384 struct video_tuner tuner;
3385 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3386 return -EFAULT;
3387
3388 if (tuner.tuner != 0)
3389 return -EINVAL;
3390
3391 strcpy(tuner.name, "no_tuner");
3392 tuner.rangelow = 0;
3393 tuner.rangehigh = 0;
3394 tuner.flags = VIDEO_TUNER_NORM;
3395 tuner.mode = VIDEO_MODE_AUTO;
3396 tuner.signal = 0xffff;
3397
3398 if (copy_to_user(arg, &tuner, sizeof(tuner)))
3399 return -EFAULT;
3400
3401 DBG(5, "VIDIOCGTUNER successfully called")
3402 return 0;
3403 }
3404
3405 case VIDIOCSTUNER:
3406 {
3407 struct video_tuner tuner;
3408 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3409 return -EFAULT;
3410
3411 if (tuner.tuner != 0)
3412 return -EINVAL;
3413
3414 if (tuner.mode != VIDEO_MODE_AUTO)
3415 return -EINVAL;
3416
3417 DBG(5, "VIDIOCSTUNER successfully called")
3418 return 0;
3419 }
3420
3421 case VIDIOCSFBUF:
3422 case VIDIOCCAPTURE:
3423 case VIDIOCGFREQ:
3424 case VIDIOCSFREQ:
3425 case VIDIOCGAUDIO:
3426 case VIDIOCSAUDIO:
3427 case VIDIOCSPLAYMODE:
3428 case VIDIOCSWRITEMODE:
3429 case VIDIOCGPLAYINFO:
3430 case VIDIOCSMICROCODE:
3431 case VIDIOCGVBIFMT:
3432 case VIDIOCSVBIFMT:
3433 DBG(4, "Unsupported V4L1 IOCtl: VIDIOC%s "
3434 "(type 0x%01X, "
3435 "n. 0x%01X, "
3436 "dir. 0x%01X, "
3437 "size 0x%02X)",
3438 V4L1_IOCTL(cmd),
3439 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3440
3441 return -EINVAL;
3442
3443 default:
3444 DBG(4, "Invalid V4L1 IOCtl: VIDIOC%s "
3445 "type 0x%01X, "
3446 "n. 0x%01X, "
3447 "dir. 0x%01X, "
3448 "size 0x%02X",
3449 V4L1_IOCTL(cmd),
3450 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3451
3452 return -ENOIOCTLCMD;
3453
3454 } /* end of switch */
3455
3456ioctl_fail:
3457 cam->misconfigured = 1;
3458 DBG(1, "VIDIOC%s failed because of hardware problems. "
3459 "To use the camera, close and open it again.", V4L1_IOCTL(cmd))
3460 return -EFAULT;
3461}
3462
3463
3464static struct file_operations w9968cf_fops = {
3465 .owner = THIS_MODULE,
3466 .open = w9968cf_open,
3467 .release = w9968cf_release,
3468 .read = w9968cf_read,
3469 .ioctl = w9968cf_ioctl,
3470 .compat_ioctl = v4l_compat_ioctl32,
3471 .mmap = w9968cf_mmap,
3472 .llseek = no_llseek,
3473};
3474
3475
3476
3477/****************************************************************************
3478 * USB probe and V4L registration, disconnect and id_table[] definition *
3479 ****************************************************************************/
3480
3481static int
3482w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3483{
3484 struct usb_device *udev = interface_to_usbdev(intf);
3485 struct w9968cf_device* cam;
3486 int err = 0;
3487 enum w9968cf_model_id mod_id;
3488 struct list_head* ptr;
3489 u8 sc = 0; /* number of simultaneous cameras */
3490 static unsigned short dev_nr = 0; /* we are handling device number n */
3491
3492 if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[0].idVendor &&
3493 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[0].idProduct)
3494 mod_id = W9968CF_MOD_CLVBWGP; /* see camlist[] table */
3495 else if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[1].idVendor &&
3496 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[1].idProduct)
3497 mod_id = W9968CF_MOD_GENERIC; /* see camlist[] table */
3498 else
3499 return -ENODEV;
3500
3501 cam = (struct w9968cf_device*)
3502 kmalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
3503 if (!cam)
3504 return -ENOMEM;
3505
3506 memset(cam, 0, sizeof(*cam));
3507
3508 init_MUTEX(&cam->dev_sem);
3509 down(&cam->dev_sem);
3510
3511 cam->usbdev = udev;
3512 /* NOTE: a local copy is used to avoid possible race conditions */
3513 memcpy(&cam->dev, &udev->dev, sizeof(struct device));
3514
3515 DBG(2, "%s detected", symbolic(camlist, mod_id))
3516
3517 if (simcams > W9968CF_MAX_DEVICES)
3518 simcams = W9968CF_SIMCAMS;
3519
3520 /* How many cameras are connected ? */
3521 down(&w9968cf_devlist_sem);
3522 list_for_each(ptr, &w9968cf_dev_list)
3523 sc++;
3524 up(&w9968cf_devlist_sem);
3525
3526 if (sc >= simcams) {
3527 DBG(2, "Device rejected: too many connected cameras "
3528 "(max. %u)", simcams)
3529 err = -EPERM;
3530 goto fail;
3531 }
3532
3533
3534 /* Allocate 2 bytes of memory for camera control USB transfers */
3535 if (!(cam->control_buffer = kmalloc(2, GFP_KERNEL))) {
3536 DBG(1,"Couldn't allocate memory for camera control transfers")
3537 err = -ENOMEM;
3538 goto fail;
3539 }
3540 memset(cam->control_buffer, 0, 2);
3541
3542 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
3543 if (!(cam->data_buffer = kmalloc(8, GFP_KERNEL))) {
3544 DBG(1, "Couldn't allocate memory for data "
3545 "transfers to the FSB")
3546 err = -ENOMEM;
3547 goto fail;
3548 }
3549 memset(cam->data_buffer, 0, 8);
3550
3551 /* Register the V4L device */
3552 cam->v4ldev = video_device_alloc();
3553 if (!cam->v4ldev) {
3554 DBG(1, "Could not allocate memory for a V4L structure")
3555 err = -ENOMEM;
3556 goto fail;
3557 }
3558
3559 strcpy(cam->v4ldev->name, symbolic(camlist, mod_id));
3560 cam->v4ldev->owner = THIS_MODULE;
3561 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
3562 cam->v4ldev->hardware = VID_HARDWARE_W9968CF;
3563 cam->v4ldev->fops = &w9968cf_fops;
3564 cam->v4ldev->minor = video_nr[dev_nr];
3565 cam->v4ldev->release = video_device_release;
3566 video_set_drvdata(cam->v4ldev, cam);
3567 cam->v4ldev->dev = &cam->dev;
3568
3569 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
3570 video_nr[dev_nr]);
3571 if (err) {
3572 DBG(1, "V4L device registration failed")
3573 if (err == -ENFILE && video_nr[dev_nr] == -1)
3574 DBG(2, "Couldn't find a free /dev/videoX node")
3575 video_nr[dev_nr] = -1;
3576 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3577 goto fail;
3578 }
3579
3580 DBG(2, "V4L device registered as /dev/video%d", cam->v4ldev->minor)
3581
3582 /* Set some basic constants */
3583 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3584
3585 /* Add a new entry into the list of V4L registered devices */
3586 down(&w9968cf_devlist_sem);
3587 list_add(&cam->v4llist, &w9968cf_dev_list);
3588 up(&w9968cf_devlist_sem);
3589 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3590
3591 w9968cf_turn_on_led(cam);
3592
3593 w9968cf_i2c_init(cam);
3594
3595 usb_set_intfdata(intf, cam);
3596 up(&cam->dev_sem);
3597 return 0;
3598
3599fail: /* Free unused memory */
3600 kfree(cam->control_buffer);
3601 kfree(cam->data_buffer);
3602 if (cam->v4ldev)
3603 video_device_release(cam->v4ldev);
3604 up(&cam->dev_sem);
3605 kfree(cam);
3606 return err;
3607}
3608
3609
3610static void w9968cf_usb_disconnect(struct usb_interface* intf)
3611{
3612 struct w9968cf_device* cam =
3613 (struct w9968cf_device*)usb_get_intfdata(intf);
3614
3615 down_write(&w9968cf_disconnect);
3616
3617 if (cam) {
3618 /* Prevent concurrent accesses to data */
3619 down(&cam->dev_sem);
3620
3621 cam->disconnected = 1;
3622
3623 DBG(2, "Disconnecting %s...", symbolic(camlist, cam->id))
3624
3625 wake_up_interruptible_all(&cam->open);
3626
3627 if (cam->users) {
3628 DBG(2, "The device is open (/dev/video%d)! "
3629 "Process name: %s. Deregistration and memory "
3630 "deallocation are deferred on close.",
3631 cam->v4ldev->minor, cam->command)
3632 cam->misconfigured = 1;
3633 w9968cf_stop_transfer(cam);
3634 wake_up_interruptible(&cam->wait_queue);
3635 } else
3636 w9968cf_release_resources(cam);
3637
3638 up(&cam->dev_sem);
3639
3640 if (!cam->users)
3641 kfree(cam);
3642 }
3643
3644 up_write(&w9968cf_disconnect);
3645}
3646
3647
3648static struct usb_driver w9968cf_usb_driver = {
3649 .name = "w9968cf",
3650 .id_table = winbond_id_table,
3651 .probe = w9968cf_usb_probe,
3652 .disconnect = w9968cf_usb_disconnect,
3653};
3654
3655
3656
3657/****************************************************************************
3658 * Module init, exit and intermodule communication *
3659 ****************************************************************************/
3660
3661static int __init w9968cf_module_init(void)
3662{
3663 int err;
3664
3665 KDBG(2, W9968CF_MODULE_NAME" "W9968CF_MODULE_VERSION)
3666 KDBG(3, W9968CF_MODULE_AUTHOR)
3667
3668 if (ovmod_load)
3669 request_module("ovcamchip");
3670
3671 if ((err = usb_register(&w9968cf_usb_driver)))
3672 return err;
3673
3674 return 0;
3675}
3676
3677
3678static void __exit w9968cf_module_exit(void)
3679{
3680 /* w9968cf_usb_disconnect() will be called */
3681 usb_deregister(&w9968cf_usb_driver);
3682
3683 KDBG(2, W9968CF_MODULE_NAME" deregistered")
3684}
3685
3686
3687module_init(w9968cf_module_init);
3688module_exit(w9968cf_module_exit);
3689
diff --git a/drivers/usb/media/w9968cf.h b/drivers/usb/media/w9968cf.h
deleted file mode 100644
index 47a6ff794171..000000000000
--- a/drivers/usb/media/w9968cf.h
+++ /dev/null
@@ -1,338 +0,0 @@
1/***************************************************************************
2 * Video4Linux driver for W996[87]CF JPEG USB Dual Mode Camera Chip. *
3 * *
4 * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _W9968CF_H_
22#define _W9968CF_H_
23
24#include <linux/videodev.h>
25#include <linux/usb.h>
26#include <linux/i2c.h>
27#include <linux/device.h>
28#include <linux/spinlock.h>
29#include <linux/list.h>
30#include <linux/wait.h>
31#include <linux/config.h>
32#include <linux/param.h>
33#include <linux/types.h>
34#include <linux/rwsem.h>
35#include <asm/semaphore.h>
36
37#include <media/ovcamchip.h>
38
39#include "w9968cf_vpp.h"
40
41
42/****************************************************************************
43 * Default values *
44 ****************************************************************************/
45
46#define W9968CF_OVMOD_LOAD 1 /* automatic 'ovcamchip' module loading */
47#define W9968CF_VPPMOD_LOAD 1 /* automatic 'w9968cf-vpp' module loading */
48
49/* Comment/uncomment the following line to enable/disable debugging messages */
50#define W9968CF_DEBUG
51
52/* These have effect only if W9968CF_DEBUG is defined */
53#define W9968CF_DEBUG_LEVEL 2 /* from 0 to 6. 0 for no debug informations */
54#define W9968CF_SPECIFIC_DEBUG 0 /* 0 or 1 */
55
56#define W9968CF_MAX_DEVICES 32
57#define W9968CF_SIMCAMS W9968CF_MAX_DEVICES /* simultaneous cameras */
58
59#define W9968CF_MAX_BUFFERS 32
60#define W9968CF_BUFFERS 2 /* n. of frame buffers from 2 to MAX_BUFFERS */
61
62/* Maximum data payload sizes in bytes for alternate settings */
63static const u16 wMaxPacketSize[] = {1023, 959, 895, 831, 767, 703, 639, 575,
64 511, 447, 383, 319, 255, 191, 127, 63};
65#define W9968CF_PACKET_SIZE 1023 /* according to wMaxPacketSizes[] */
66#define W9968CF_MIN_PACKET_SIZE 63 /* minimum value */
67#define W9968CF_ISO_PACKETS 5 /* n.of packets for isochronous transfers */
68#define W9968CF_USB_CTRL_TIMEOUT 1000 /* timeout (ms) for usb control commands */
69#define W9968CF_URBS 2 /* n. of scheduled URBs for ISO transfer */
70
71#define W9968CF_I2C_BUS_DELAY 4 /* delay in us for I2C bit r/w operations */
72#define W9968CF_I2C_RW_RETRIES 15 /* number of max I2C r/w retries */
73
74/* Available video formats */
75struct w9968cf_format {
76 const u16 palette;
77 const u16 depth;
78 const u8 compression;
79};
80
81static const struct w9968cf_format w9968cf_formatlist[] = {
82 { VIDEO_PALETTE_UYVY, 16, 0 }, /* original video */
83 { VIDEO_PALETTE_YUV422P, 16, 1 }, /* with JPEG compression */
84 { VIDEO_PALETTE_YUV420P, 12, 1 }, /* with JPEG compression */
85 { VIDEO_PALETTE_YUV420, 12, 1 }, /* same as YUV420P */
86 { VIDEO_PALETTE_YUYV, 16, 0 }, /* software conversion */
87 { VIDEO_PALETTE_YUV422, 16, 0 }, /* software conversion */
88 { VIDEO_PALETTE_GREY, 8, 0 }, /* software conversion */
89 { VIDEO_PALETTE_RGB555, 16, 0 }, /* software conversion */
90 { VIDEO_PALETTE_RGB565, 16, 0 }, /* software conversion */
91 { VIDEO_PALETTE_RGB24, 24, 0 }, /* software conversion */
92 { VIDEO_PALETTE_RGB32, 32, 0 }, /* software conversion */
93 { 0, 0, 0 } /* 0 is a terminating entry */
94};
95
96#define W9968CF_DECOMPRESSION 2 /* decomp:0=disable,1=force,2=any formats */
97#define W9968CF_PALETTE_DECOMP_OFF VIDEO_PALETTE_UYVY /* when decomp=0 */
98#define W9968CF_PALETTE_DECOMP_FORCE VIDEO_PALETTE_YUV420P /* when decomp=1 */
99#define W9968CF_PALETTE_DECOMP_ON VIDEO_PALETTE_UYVY /* when decomp=2 */
100
101#define W9968CF_FORCE_RGB 0 /* read RGB instead of BGR, yes=1/no=0 */
102
103#define W9968CF_MAX_WIDTH 800 /* Has effect if up-scaling is on */
104#define W9968CF_MAX_HEIGHT 600 /* Has effect if up-scaling is on */
105#define W9968CF_WIDTH 320 /* from 128 to 352, multiple of 16 */
106#define W9968CF_HEIGHT 240 /* from 96 to 288, multiple of 16 */
107
108#define W9968CF_CLAMPING 0 /* 0 disable, 1 enable video data clamping */
109#define W9968CF_FILTER_TYPE 0 /* 0 disable 1 (1-2-1), 2 (2-3-6-3-2) */
110#define W9968CF_DOUBLE_BUFFER 1 /* 0 disable, 1 enable double buffer */
111#define W9968CF_LARGEVIEW 1 /* 0 disable, 1 enable */
112#define W9968CF_UPSCALING 0 /* 0 disable, 1 enable */
113
114#define W9968CF_MONOCHROME 0 /* 0 not monochrome, 1 monochrome sensor */
115#define W9968CF_BRIGHTNESS 31000 /* from 0 to 65535 */
116#define W9968CF_HUE 32768 /* from 0 to 65535 */
117#define W9968CF_COLOUR 32768 /* from 0 to 65535 */
118#define W9968CF_CONTRAST 50000 /* from 0 to 65535 */
119#define W9968CF_WHITENESS 32768 /* from 0 to 65535 */
120
121#define W9968CF_AUTOBRIGHT 0 /* 0 disable, 1 enable automatic brightness */
122#define W9968CF_AUTOEXP 1 /* 0 disable, 1 enable automatic exposure */
123#define W9968CF_LIGHTFREQ 50 /* light frequency. 50Hz (Europe) or 60Hz */
124#define W9968CF_BANDINGFILTER 0 /* 0 disable, 1 enable banding filter */
125#define W9968CF_BACKLIGHT 0 /* 0 or 1, 1=object is lit from behind */
126#define W9968CF_MIRROR 0 /* 0 or 1 [don't] reverse image horizontally*/
127
128#define W9968CF_CLOCKDIV -1 /* -1 = automatic clock divisor */
129#define W9968CF_DEF_CLOCKDIVISOR 0 /* default sensor clock divisor value */
130
131
132/****************************************************************************
133 * Globals *
134 ****************************************************************************/
135
136#define W9968CF_MODULE_NAME "V4L driver for W996[87]CF JPEG USB " \
137 "Dual Mode Camera Chip"
138#define W9968CF_MODULE_VERSION "1:1.33-basic"
139#define W9968CF_MODULE_AUTHOR "(C) 2002-2004 Luca Risolia"
140#define W9968CF_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
141#define W9968CF_MODULE_LICENSE "GPL"
142
143static const struct usb_device_id winbond_id_table[] = {
144 {
145 /* Creative Labs Video Blaster WebCam Go Plus */
146 USB_DEVICE(0x041e, 0x4003),
147 .driver_info = (unsigned long)"w9968cf",
148 },
149 {
150 /* Generic W996[87]CF JPEG USB Dual Mode Camera */
151 USB_DEVICE(0x1046, 0x9967),
152 .driver_info = (unsigned long)"w9968cf",
153 },
154 { } /* terminating entry */
155};
156
157/* W996[87]CF camera models, internal ids: */
158enum w9968cf_model_id {
159 W9968CF_MOD_GENERIC = 1, /* Generic W996[87]CF based device */
160 W9968CF_MOD_CLVBWGP = 11,/*Creative Labs Video Blaster WebCam Go Plus*/
161 W9968CF_MOD_ADPVDMA = 21, /* Aroma Digi Pen VGA Dual Mode ADG-5000 */
162 W9986CF_MOD_AAU = 31, /* AVerMedia AVerTV USB */
163 W9968CF_MOD_CLVBWG = 34, /* Creative Labs Video Blaster WebCam Go */
164 W9968CF_MOD_LL = 37, /* Lebon LDC-035A */
165 W9968CF_MOD_EEEMC = 40, /* Ezonics EZ-802 EZMega Cam */
166 W9968CF_MOD_OOE = 42, /* OmniVision OV8610-EDE */
167 W9968CF_MOD_ODPVDMPC = 43,/* OPCOM Digi Pen VGA Dual Mode Pen Camera */
168 W9968CF_MOD_PDPII = 46, /* Pretec Digi Pen-II */
169 W9968CF_MOD_PDP480 = 49, /* Pretec DigiPen-480 */
170};
171
172enum w9968cf_frame_status {
173 F_READY, /* finished grabbing & ready to be read/synced */
174 F_GRABBING, /* in the process of being grabbed into */
175 F_ERROR, /* something bad happened while processing */
176 F_UNUSED /* unused (no VIDIOCMCAPTURE) */
177};
178
179struct w9968cf_frame_t {
180 void* buffer;
181 unsigned long size;
182 u32 length;
183 int number;
184 enum w9968cf_frame_status status;
185 struct w9968cf_frame_t* next;
186 u8 queued;
187};
188
189enum w9968cf_vpp_flag {
190 VPP_NONE = 0x00,
191 VPP_UPSCALE = 0x01,
192 VPP_SWAP_YUV_BYTES = 0x02,
193 VPP_DECOMPRESSION = 0x04,
194 VPP_UYVY_TO_RGBX = 0x08,
195};
196
197static struct w9968cf_vpp_t* w9968cf_vpp;
198static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
199
200static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
201static DECLARE_MUTEX(w9968cf_devlist_sem); /* semaphore for list traversal */
202
203static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
204
205/* Main device driver structure */
206struct w9968cf_device {
207 struct device dev; /* device structure */
208
209 enum w9968cf_model_id id; /* private device identifier */
210
211 struct video_device* v4ldev; /* -> V4L structure */
212 struct list_head v4llist; /* entry of the list of V4L cameras */
213
214 struct usb_device* usbdev; /* -> main USB structure */
215 struct urb* urb[W9968CF_URBS]; /* -> USB request block structs */
216 void* transfer_buffer[W9968CF_URBS]; /* -> ISO transfer buffers */
217 u16* control_buffer; /* -> buffer for control req.*/
218 u16* data_buffer; /* -> data to send to the FSB */
219
220 struct w9968cf_frame_t frame[W9968CF_MAX_BUFFERS];
221 struct w9968cf_frame_t frame_tmp; /* temporary frame */
222 struct w9968cf_frame_t frame_vpp; /* helper frame.*/
223 struct w9968cf_frame_t* frame_current; /* -> frame being grabbed */
224 struct w9968cf_frame_t* requested_frame[W9968CF_MAX_BUFFERS];
225
226 u8 max_buffers, /* number of requested buffers */
227 force_palette, /* yes=1/no=0 */
228 force_rgb, /* read RGB instead of BGR, yes=1, no=0 */
229 double_buffer, /* hardware double buffering yes=1/no=0 */
230 clamping, /* video data clamping yes=1/no=0 */
231 filter_type, /* 0=disabled, 1=3 tap, 2=5 tap filter */
232 capture, /* 0=disabled, 1=enabled */
233 largeview, /* 0=disabled, 1=enabled */
234 decompression, /* 0=disabled, 1=forced, 2=allowed */
235 upscaling; /* software image scaling, 0=enabled, 1=disabled */
236
237 struct video_picture picture; /* current picture settings */
238 struct video_window window; /* current window settings */
239
240 u16 hw_depth, /* depth (used by the chip) */
241 hw_palette, /* palette (used by the chip) */
242 hw_width, /* width (used by the chip) */
243 hw_height, /* height (used by the chip) */
244 hs_polarity, /* 0=negative sync pulse, 1=positive sync pulse */
245 vs_polarity, /* 0=negative sync pulse, 1=positive sync pulse */
246 start_cropx, /* pixels from HS inactive edge to 1st cropped pixel*/
247 start_cropy; /* pixels from VS inactive edge to 1st cropped pixel*/
248
249 enum w9968cf_vpp_flag vpp_flag; /* post-processing routines in use */
250
251 u8 nbuffers, /* number of allocated frame buffers */
252 altsetting, /* camera alternate setting */
253 disconnected, /* flag: yes=1, no=0 */
254 misconfigured, /* flag: yes=1, no=0 */
255 users, /* flag: number of users holding the device */
256 streaming; /* flag: yes=1, no=0 */
257
258 u8 sensor_initialized; /* flag: yes=1, no=0 */
259
260 /* Determined by the image sensor type: */
261 int sensor, /* type of image sensor chip (CC_*) */
262 monochrome; /* image sensor is (probably) monochrome */
263 u16 maxwidth, /* maximum width supported by the image sensor */
264 maxheight, /* maximum height supported by the image sensor */
265 minwidth, /* minimum width supported by the image sensor */
266 minheight; /* minimum height supported by the image sensor */
267 u8 auto_brt, /* auto brightness enabled flag */
268 auto_exp, /* auto exposure enabled flag */
269 backlight, /* backlight exposure algorithm flag */
270 mirror, /* image is reversed horizontally */
271 lightfreq, /* power (lighting) frequency */
272 bandfilt; /* banding filter enabled flag */
273 s8 clockdiv; /* clock divisor */
274
275 /* I2C interface to kernel */
276 struct i2c_adapter i2c_adapter;
277 struct i2c_client* sensor_client;
278
279 /* Locks */
280 struct semaphore dev_sem, /* for probe, disconnect,open and close */
281 fileop_sem; /* for read and ioctl */
282 spinlock_t urb_lock, /* for submit_urb() and unlink_urb() */
283 flist_lock; /* for requested frame list accesses */
284 wait_queue_head_t open, wait_queue;
285
286 char command[16]; /* name of the program holding the device */
287};
288
289
290/****************************************************************************
291 * Macros for debugging *
292 ****************************************************************************/
293
294#undef DBG
295#undef KDBG
296#ifdef W9968CF_DEBUG
297/* For device specific debugging messages */
298# define DBG(level, fmt, args...) \
299{ \
300 if ( ((specific_debug) && (debug == (level))) || \
301 ((!specific_debug) && (debug >= (level))) ) { \
302 if ((level) == 1) \
303 dev_err(&cam->dev, fmt "\n", ## args); \
304 else if ((level) == 2 || (level) == 3) \
305 dev_info(&cam->dev, fmt "\n", ## args); \
306 else if ((level) == 4) \
307 dev_warn(&cam->dev, fmt "\n", ## args); \
308 else if ((level) >= 5) \
309 dev_info(&cam->dev, "[%s:%d] " fmt "\n", \
310 __FUNCTION__, __LINE__ , ## args); \
311 } \
312}
313/* For generic kernel (not device specific) messages */
314# define KDBG(level, fmt, args...) \
315{ \
316 if ( ((specific_debug) && (debug == (level))) || \
317 ((!specific_debug) && (debug >= (level))) ) { \
318 if ((level) >= 1 && (level) <= 4) \
319 pr_info("w9968cf: " fmt "\n", ## args); \
320 else if ((level) >= 5) \
321 pr_debug("w9968cf: [%s:%d] " fmt "\n", __FUNCTION__, \
322 __LINE__ , ## args); \
323 } \
324}
325#else
326 /* Not debugging: nothing */
327# define DBG(level, fmt, args...) do {;} while(0);
328# define KDBG(level, fmt, args...) do {;} while(0);
329#endif
330
331#undef PDBG
332#define PDBG(fmt, args...) \
333dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args);
334
335#undef PDBGG
336#define PDBGG(fmt, args...) do {;} while(0); /* nothing: it's a placeholder */
337
338#endif /* _W9968CF_H_ */
diff --git a/drivers/usb/media/w9968cf_decoder.h b/drivers/usb/media/w9968cf_decoder.h
deleted file mode 100644
index 31faccbe8f03..000000000000
--- a/drivers/usb/media/w9968cf_decoder.h
+++ /dev/null
@@ -1,86 +0,0 @@
1/***************************************************************************
2 * Video decoder for the W996[87]CF driver for Linux. *
3 * *
4 * Copyright (C) 2003 2004 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _W9968CF_DECODER_H_
22#define _W9968CF_DECODER_H_
23
24/* Comment/uncomment this for high/low quality of compressed video */
25#define W9968CF_DEC_FAST_LOWQUALITY_VIDEO
26
27#ifdef W9968CF_DEC_FAST_LOWQUALITY_VIDEO
28static const unsigned char Y_QUANTABLE[64] = {
29 16, 11, 10, 16, 24, 40, 51, 61,
30 12, 12, 14, 19, 26, 58, 60, 55,
31 14, 13, 16, 24, 40, 57, 69, 56,
32 14, 17, 22, 29, 51, 87, 80, 62,
33 18, 22, 37, 56, 68, 109, 103, 77,
34 24, 35, 55, 64, 81, 104, 113, 92,
35 49, 64, 78, 87, 103, 121, 120, 101,
36 72, 92, 95, 98, 112, 100, 103, 99
37};
38
39static const unsigned char UV_QUANTABLE[64] = {
40 17, 18, 24, 47, 99, 99, 99, 99,
41 18, 21, 26, 66, 99, 99, 99, 99,
42 24, 26, 56, 99, 99, 99, 99, 99,
43 47, 66, 99, 99, 99, 99, 99, 99,
44 99, 99, 99, 99, 99, 99, 99, 99,
45 99, 99, 99, 99, 99, 99, 99, 99,
46 99, 99, 99, 99, 99, 99, 99, 99,
47 99, 99, 99, 99, 99, 99, 99, 99
48};
49#else
50static const unsigned char Y_QUANTABLE[64] = {
51 8, 5, 5, 8, 12, 20, 25, 30,
52 6, 6, 7, 9, 13, 29, 30, 27,
53 7, 6, 8, 12, 20, 28, 34, 28,
54 7, 8, 11, 14, 25, 43, 40, 31,
55 9, 11, 18, 28, 34, 54, 51, 38,
56 12, 17, 27, 32, 40, 52, 56, 46,
57 24, 32, 39, 43, 51, 60, 60, 50,
58 36, 46, 47, 49, 56, 50, 51, 49
59};
60
61static const unsigned char UV_QUANTABLE[64] = {
62 8, 9, 12, 23, 49, 49, 49, 49,
63 9, 10, 13, 33, 49, 49, 49, 49,
64 12, 13, 28, 49, 49, 49, 49, 49,
65 23, 33, 49, 49, 49, 49, 49, 49,
66 49, 49, 49, 49, 49, 49, 49, 49,
67 49, 49, 49, 49, 49, 49, 49, 49,
68 49, 49, 49, 49, 49, 49, 49, 49,
69 49, 49, 49, 49, 49, 49, 49, 49
70};
71#endif
72
73#define W9968CF_DEC_ERR_CORRUPTED_DATA -1
74#define W9968CF_DEC_ERR_BUF_OVERFLOW -2
75#define W9968CF_DEC_ERR_NO_SOI -3
76#define W9968CF_DEC_ERR_NO_SOF0 -4
77#define W9968CF_DEC_ERR_NO_SOS -5
78#define W9968CF_DEC_ERR_NO_EOI -6
79
80extern void w9968cf_init_decoder(void);
81extern int w9968cf_check_headers(const unsigned char* Pin,
82 const unsigned long BUF_SIZE);
83extern int w9968cf_decode(const char* Pin, const unsigned long BUF_SIZE,
84 const unsigned W, const unsigned H, char* Pout);
85
86#endif /* _W9968CF_DECODER_H_ */
diff --git a/drivers/usb/media/w9968cf_vpp.h b/drivers/usb/media/w9968cf_vpp.h
deleted file mode 100644
index f3b91b782671..000000000000
--- a/drivers/usb/media/w9968cf_vpp.h
+++ /dev/null
@@ -1,40 +0,0 @@
1/***************************************************************************
2 * Interface for video post-processing functions for the W996[87]CF driver *
3 * for Linux. *
4 * *
5 * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#ifndef _W9968CF_VPP_H_
23#define _W9968CF_VPP_H_
24
25#include <linux/module.h>
26#include <asm/types.h>
27
28struct w9968cf_vpp_t {
29 struct module* owner;
30 int (*check_headers)(const unsigned char*, const unsigned long);
31 int (*decode)(const char*, const unsigned long, const unsigned,
32 const unsigned, char*);
33 void (*swap_yuvbytes)(void*, unsigned long);
34 void (*uyvy_to_rgbx)(u8*, unsigned long, u8*, u16, u8);
35 void (*scale_up)(u8*, u8*, u16, u16, u16, u16, u16);
36
37 u8 busy; /* read-only flag: module is/is not in use */
38};
39
40#endif /* _W9968CF_VPP_H_ */
diff --git a/drivers/usb/misc/auerswald.c b/drivers/usb/misc/auerswald.c
index ad2f4cccd388..1fef36e71c57 100644
--- a/drivers/usb/misc/auerswald.c
+++ b/drivers/usb/misc/auerswald.c
@@ -570,10 +570,9 @@ static int auerchain_setup (pauerchain_t acp, unsigned int numElements)
570 570
571 /* fill the list of free elements */ 571 /* fill the list of free elements */
572 for (;numElements; numElements--) { 572 for (;numElements; numElements--) {
573 acep = (pauerchainelement_t) kmalloc (sizeof (auerchainelement_t), GFP_KERNEL); 573 acep = kzalloc(sizeof(auerchainelement_t), GFP_KERNEL);
574 if (!acep) 574 if (!acep)
575 goto ac_fail; 575 goto ac_fail;
576 memset (acep, 0, sizeof (auerchainelement_t));
577 INIT_LIST_HEAD (&acep->list); 576 INIT_LIST_HEAD (&acep->list);
578 list_add_tail (&acep->list, &acp->free_list); 577 list_add_tail (&acep->list, &acp->free_list);
579 } 578 }
@@ -761,10 +760,9 @@ static int auerbuf_setup (pauerbufctl_t bcp, unsigned int numElements, unsigned
761 760
762 /* fill the list of free elements */ 761 /* fill the list of free elements */
763 for (;numElements; numElements--) { 762 for (;numElements; numElements--) {
764 bep = (pauerbuf_t) kmalloc (sizeof (auerbuf_t), GFP_KERNEL); 763 bep = kzalloc(sizeof(auerbuf_t), GFP_KERNEL);
765 if (!bep) 764 if (!bep)
766 goto bl_fail; 765 goto bl_fail;
767 memset (bep, 0, sizeof (auerbuf_t));
768 bep->list = bcp; 766 bep->list = bcp;
769 INIT_LIST_HEAD (&bep->buff_list); 767 INIT_LIST_HEAD (&bep->buff_list);
770 bep->bufp = kmalloc (bufsize, GFP_KERNEL); 768 bep->bufp = kmalloc (bufsize, GFP_KERNEL);
diff --git a/drivers/usb/misc/cytherm.c b/drivers/usb/misc/cytherm.c
index 6671317b495f..a04204292aa3 100644
--- a/drivers/usb/misc/cytherm.c
+++ b/drivers/usb/misc/cytherm.c
@@ -351,12 +351,11 @@ static int cytherm_probe(struct usb_interface *interface,
351 struct usb_cytherm *dev = NULL; 351 struct usb_cytherm *dev = NULL;
352 int retval = -ENOMEM; 352 int retval = -ENOMEM;
353 353
354 dev = kmalloc (sizeof(struct usb_cytherm), GFP_KERNEL); 354 dev = kzalloc (sizeof(struct usb_cytherm), GFP_KERNEL);
355 if (dev == NULL) { 355 if (dev == NULL) {
356 dev_err (&interface->dev, "Out of memory\n"); 356 dev_err (&interface->dev, "Out of memory\n");
357 goto error; 357 goto error;
358 } 358 }
359 memset (dev, 0x00, sizeof (*dev));
360 359
361 dev->udev = usb_get_dev(udev); 360 dev->udev = usb_get_dev(udev);
362 361
diff --git a/drivers/usb/misc/idmouse.c b/drivers/usb/misc/idmouse.c
index d8cde1017985..d0b167256699 100644
--- a/drivers/usb/misc/idmouse.c
+++ b/drivers/usb/misc/idmouse.c
@@ -25,6 +25,7 @@
25#include <linux/module.h> 25#include <linux/module.h>
26#include <linux/smp_lock.h> 26#include <linux/smp_lock.h>
27#include <linux/completion.h> 27#include <linux/completion.h>
28#include <linux/mutex.h>
28#include <asm/uaccess.h> 29#include <asm/uaccess.h>
29#include <linux/usb.h> 30#include <linux/usb.h>
30 31
@@ -121,7 +122,7 @@ static struct usb_driver idmouse_driver = {
121}; 122};
122 123
123/* prevent races between open() and disconnect() */ 124/* prevent races between open() and disconnect() */
124static DECLARE_MUTEX(disconnect_sem); 125static DEFINE_MUTEX(disconnect_mutex);
125 126
126static int idmouse_create_image(struct usb_idmouse *dev) 127static int idmouse_create_image(struct usb_idmouse *dev)
127{ 128{
@@ -213,18 +214,18 @@ static int idmouse_open(struct inode *inode, struct file *file)
213 int result = 0; 214 int result = 0;
214 215
215 /* prevent disconnects */ 216 /* prevent disconnects */
216 down(&disconnect_sem); 217 mutex_lock(&disconnect_mutex);
217 218
218 /* get the interface from minor number and driver information */ 219 /* get the interface from minor number and driver information */
219 interface = usb_find_interface (&idmouse_driver, iminor (inode)); 220 interface = usb_find_interface (&idmouse_driver, iminor (inode));
220 if (!interface) { 221 if (!interface) {
221 up(&disconnect_sem); 222 mutex_unlock(&disconnect_mutex);
222 return -ENODEV; 223 return -ENODEV;
223 } 224 }
224 /* get the device information block from the interface */ 225 /* get the device information block from the interface */
225 dev = usb_get_intfdata(interface); 226 dev = usb_get_intfdata(interface);
226 if (!dev) { 227 if (!dev) {
227 up(&disconnect_sem); 228 mutex_unlock(&disconnect_mutex);
228 return -ENODEV; 229 return -ENODEV;
229 } 230 }
230 231
@@ -258,7 +259,7 @@ error:
258 up(&dev->sem); 259 up(&dev->sem);
259 260
260 /* unlock the disconnect semaphore */ 261 /* unlock the disconnect semaphore */
261 up(&disconnect_sem); 262 mutex_unlock(&disconnect_mutex);
262 return result; 263 return result;
263} 264}
264 265
@@ -267,12 +268,12 @@ static int idmouse_release(struct inode *inode, struct file *file)
267 struct usb_idmouse *dev; 268 struct usb_idmouse *dev;
268 269
269 /* prevent a race condition with open() */ 270 /* prevent a race condition with open() */
270 down(&disconnect_sem); 271 mutex_lock(&disconnect_mutex);
271 272
272 dev = (struct usb_idmouse *) file->private_data; 273 dev = (struct usb_idmouse *) file->private_data;
273 274
274 if (dev == NULL) { 275 if (dev == NULL) {
275 up(&disconnect_sem); 276 mutex_unlock(&disconnect_mutex);
276 return -ENODEV; 277 return -ENODEV;
277 } 278 }
278 279
@@ -282,7 +283,7 @@ static int idmouse_release(struct inode *inode, struct file *file)
282 /* are we really open? */ 283 /* are we really open? */
283 if (dev->open <= 0) { 284 if (dev->open <= 0) {
284 up(&dev->sem); 285 up(&dev->sem);
285 up(&disconnect_sem); 286 mutex_unlock(&disconnect_mutex);
286 return -ENODEV; 287 return -ENODEV;
287 } 288 }
288 289
@@ -292,12 +293,12 @@ static int idmouse_release(struct inode *inode, struct file *file)
292 /* the device was unplugged before the file was released */ 293 /* the device was unplugged before the file was released */
293 up(&dev->sem); 294 up(&dev->sem);
294 idmouse_delete(dev); 295 idmouse_delete(dev);
295 up(&disconnect_sem); 296 mutex_unlock(&disconnect_mutex);
296 return 0; 297 return 0;
297 } 298 }
298 299
299 up(&dev->sem); 300 up(&dev->sem);
300 up(&disconnect_sem); 301 mutex_unlock(&disconnect_mutex);
301 return 0; 302 return 0;
302} 303}
303 304
@@ -340,10 +341,9 @@ static int idmouse_probe(struct usb_interface *interface,
340 return -ENODEV; 341 return -ENODEV;
341 342
342 /* allocate memory for our device state and initialize it */ 343 /* allocate memory for our device state and initialize it */
343 dev = kmalloc(sizeof(*dev), GFP_KERNEL); 344 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
344 if (dev == NULL) 345 if (dev == NULL)
345 return -ENOMEM; 346 return -ENOMEM;
346 memset(dev, 0x00, sizeof(*dev));
347 347
348 init_MUTEX(&dev->sem); 348 init_MUTEX(&dev->sem);
349 dev->udev = udev; 349 dev->udev = udev;
@@ -400,7 +400,7 @@ static void idmouse_disconnect(struct usb_interface *interface)
400 struct usb_idmouse *dev; 400 struct usb_idmouse *dev;
401 401
402 /* prevent races with open() */ 402 /* prevent races with open() */
403 down(&disconnect_sem); 403 mutex_lock(&disconnect_mutex);
404 404
405 /* get device structure */ 405 /* get device structure */
406 dev = usb_get_intfdata(interface); 406 dev = usb_get_intfdata(interface);
@@ -422,7 +422,7 @@ static void idmouse_disconnect(struct usb_interface *interface)
422 if (!dev->open) 422 if (!dev->open)
423 idmouse_delete(dev); 423 idmouse_delete(dev);
424 424
425 up(&disconnect_sem); 425 mutex_unlock(&disconnect_mutex);
426 426
427 info("%s disconnected", DRIVER_DESC); 427 info("%s disconnected", DRIVER_DESC);
428} 428}
diff --git a/drivers/usb/misc/ldusb.c b/drivers/usb/misc/ldusb.c
index e2d1198623eb..966acb474f67 100644
--- a/drivers/usb/misc/ldusb.c
+++ b/drivers/usb/misc/ldusb.c
@@ -33,6 +33,7 @@
33#include <linux/init.h> 33#include <linux/init.h>
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/module.h> 35#include <linux/module.h>
36#include <linux/mutex.h>
36 37
37#include <asm/uaccess.h> 38#include <asm/uaccess.h>
38#include <linux/input.h> 39#include <linux/input.h>
@@ -172,7 +173,7 @@ struct ld_usb {
172}; 173};
173 174
174/* prevent races between open() and disconnect() */ 175/* prevent races between open() and disconnect() */
175static DECLARE_MUTEX(disconnect_sem); 176static DEFINE_MUTEX(disconnect_mutex);
176 177
177static struct usb_driver ld_usb_driver; 178static struct usb_driver ld_usb_driver;
178 179
@@ -293,7 +294,7 @@ static int ld_usb_open(struct inode *inode, struct file *file)
293 nonseekable_open(inode, file); 294 nonseekable_open(inode, file);
294 subminor = iminor(inode); 295 subminor = iminor(inode);
295 296
296 down(&disconnect_sem); 297 mutex_lock(&disconnect_mutex);
297 298
298 interface = usb_find_interface(&ld_usb_driver, subminor); 299 interface = usb_find_interface(&ld_usb_driver, subminor);
299 300
@@ -355,7 +356,7 @@ unlock_exit:
355 up(&dev->sem); 356 up(&dev->sem);
356 357
357unlock_disconnect_exit: 358unlock_disconnect_exit:
358 up(&disconnect_sem); 359 mutex_unlock(&disconnect_mutex);
359 360
360 return retval; 361 return retval;
361} 362}
@@ -626,12 +627,11 @@ static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *
626 627
627 /* allocate memory for our device state and intialize it */ 628 /* allocate memory for our device state and intialize it */
628 629
629 dev = kmalloc(sizeof(*dev), GFP_KERNEL); 630 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
630 if (dev == NULL) { 631 if (dev == NULL) {
631 dev_err(&intf->dev, "Out of memory\n"); 632 dev_err(&intf->dev, "Out of memory\n");
632 goto exit; 633 goto exit;
633 } 634 }
634 memset(dev, 0x00, sizeof(*dev));
635 init_MUTEX(&dev->sem); 635 init_MUTEX(&dev->sem);
636 dev->intf = intf; 636 dev->intf = intf;
637 init_waitqueue_head(&dev->read_wait); 637 init_waitqueue_head(&dev->read_wait);
@@ -741,7 +741,7 @@ static void ld_usb_disconnect(struct usb_interface *intf)
741 struct ld_usb *dev; 741 struct ld_usb *dev;
742 int minor; 742 int minor;
743 743
744 down(&disconnect_sem); 744 mutex_lock(&disconnect_mutex);
745 745
746 dev = usb_get_intfdata(intf); 746 dev = usb_get_intfdata(intf);
747 usb_set_intfdata(intf, NULL); 747 usb_set_intfdata(intf, NULL);
@@ -762,7 +762,7 @@ static void ld_usb_disconnect(struct usb_interface *intf)
762 up(&dev->sem); 762 up(&dev->sem);
763 } 763 }
764 764
765 up(&disconnect_sem); 765 mutex_unlock(&disconnect_mutex);
766 766
767 dev_info(&intf->dev, "LD USB Device #%d now disconnected\n", 767 dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
768 (minor - USB_LD_MINOR_BASE)); 768 (minor - USB_LD_MINOR_BASE));
diff --git a/drivers/usb/misc/legousbtower.c b/drivers/usb/misc/legousbtower.c
index 1336745b8f55..779bcf0373ad 100644
--- a/drivers/usb/misc/legousbtower.c
+++ b/drivers/usb/misc/legousbtower.c
@@ -83,6 +83,7 @@
83#include <linux/module.h> 83#include <linux/module.h>
84#include <linux/smp_lock.h> 84#include <linux/smp_lock.h>
85#include <linux/completion.h> 85#include <linux/completion.h>
86#include <linux/mutex.h>
86#include <asm/uaccess.h> 87#include <asm/uaccess.h>
87#include <linux/usb.h> 88#include <linux/usb.h>
88#include <linux/poll.h> 89#include <linux/poll.h>
@@ -256,7 +257,7 @@ static void tower_disconnect (struct usb_interface *interface);
256 257
257 258
258/* prevent races between open() and disconnect */ 259/* prevent races between open() and disconnect */
259static DECLARE_MUTEX (disconnect_sem); 260static DEFINE_MUTEX (disconnect_mutex);
260 261
261/* file operations needed when we register this driver */ 262/* file operations needed when we register this driver */
262static struct file_operations tower_fops = { 263static struct file_operations tower_fops = {
@@ -349,7 +350,7 @@ static int tower_open (struct inode *inode, struct file *file)
349 nonseekable_open(inode, file); 350 nonseekable_open(inode, file);
350 subminor = iminor(inode); 351 subminor = iminor(inode);
351 352
352 down (&disconnect_sem); 353 mutex_lock (&disconnect_mutex);
353 354
354 interface = usb_find_interface (&tower_driver, subminor); 355 interface = usb_find_interface (&tower_driver, subminor);
355 356
@@ -427,7 +428,7 @@ unlock_exit:
427 up (&dev->sem); 428 up (&dev->sem);
428 429
429unlock_disconnect_exit: 430unlock_disconnect_exit:
430 up (&disconnect_sem); 431 mutex_unlock (&disconnect_mutex);
431 432
432 dbg(2, "%s: leave, return value %d ", __FUNCTION__, retval); 433 dbg(2, "%s: leave, return value %d ", __FUNCTION__, retval);
433 434
@@ -1005,7 +1006,7 @@ static void tower_disconnect (struct usb_interface *interface)
1005 1006
1006 dbg(2, "%s: enter", __FUNCTION__); 1007 dbg(2, "%s: enter", __FUNCTION__);
1007 1008
1008 down (&disconnect_sem); 1009 mutex_lock (&disconnect_mutex);
1009 1010
1010 dev = usb_get_intfdata (interface); 1011 dev = usb_get_intfdata (interface);
1011 usb_set_intfdata (interface, NULL); 1012 usb_set_intfdata (interface, NULL);
@@ -1027,7 +1028,7 @@ static void tower_disconnect (struct usb_interface *interface)
1027 up (&dev->sem); 1028 up (&dev->sem);
1028 } 1029 }
1029 1030
1030 up (&disconnect_sem); 1031 mutex_unlock (&disconnect_mutex);
1031 1032
1032 info("LEGO USB Tower #%d now disconnected", (minor - LEGO_USB_TOWER_MINOR_BASE)); 1033 info("LEGO USB Tower #%d now disconnected", (minor - LEGO_USB_TOWER_MINOR_BASE));
1033 1034
diff --git a/drivers/usb/misc/phidgetkit.c b/drivers/usb/misc/phidgetkit.c
index 605a3c87e05c..997db5d8e35b 100644
--- a/drivers/usb/misc/phidgetkit.c
+++ b/drivers/usb/misc/phidgetkit.c
@@ -88,7 +88,7 @@ static int change_outputs(struct phidget_interfacekit *kit, int output_num, int
88 int retval; 88 int retval;
89 int n; 89 int n;
90 90
91 buffer = kmalloc(4, GFP_KERNEL); 91 buffer = kzalloc(4, GFP_KERNEL);
92 if (!buffer) { 92 if (!buffer) {
93 dev_err(&kit->udev->dev, "%s - out of memory\n", 93 dev_err(&kit->udev->dev, "%s - out of memory\n",
94 __FUNCTION__); 94 __FUNCTION__);
@@ -96,7 +96,6 @@ static int change_outputs(struct phidget_interfacekit *kit, int output_num, int
96 } 96 }
97 97
98 kit->outputs[output_num] = enable; 98 kit->outputs[output_num] = enable;
99 memset(buffer, 0, 4);
100 for (n=0; n<8; n++) { 99 for (n=0; n<8; n++) {
101 if (kit->outputs[n]) { 100 if (kit->outputs[n]) {
102 buffer[0] |= 1 << n; 101 buffer[0] |= 1 << n;
@@ -192,7 +191,7 @@ static ssize_t set_backlight(struct device *dev, struct device_attribute *attr,
192 unsigned char *buffer; 191 unsigned char *buffer;
193 int retval = -ENOMEM; 192 int retval = -ENOMEM;
194 193
195 buffer = kmalloc(8, GFP_KERNEL); 194 buffer = kzalloc(8, GFP_KERNEL);
196 if (!buffer) { 195 if (!buffer) {
197 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__); 196 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
198 goto exit; 197 goto exit;
@@ -202,7 +201,6 @@ static ssize_t set_backlight(struct device *dev, struct device_attribute *attr,
202 retval = -EINVAL; 201 retval = -EINVAL;
203 goto exit; 202 goto exit;
204 } 203 }
205 memset(buffer, 0x00, 8);
206 if (enabled) 204 if (enabled)
207 buffer[0] = 0x01; 205 buffer[0] = 0x01;
208 buffer[7] = 0x11; 206 buffer[7] = 0x11;
@@ -406,12 +404,11 @@ static int interfacekit_probe(struct usb_interface *intf, const struct usb_devic
406 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); 404 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
407 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); 405 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
408 406
409 kit = kmalloc(sizeof(*kit), GFP_KERNEL); 407 kit = kzalloc(sizeof(*kit), GFP_KERNEL);
410 if (kit == NULL) { 408 if (kit == NULL) {
411 dev_err(&intf->dev, "%s - out of memory\n", __FUNCTION__); 409 dev_err(&intf->dev, "%s - out of memory\n", __FUNCTION__);
412 return -ENOMEM; 410 return -ENOMEM;
413 } 411 }
414 memset(kit, 0, sizeof(*kit));
415 kit->ifkit = ifkit; 412 kit->ifkit = ifkit;
416 413
417 kit->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &kit->data_dma); 414 kit->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &kit->data_dma);
diff --git a/drivers/usb/misc/phidgetservo.c b/drivers/usb/misc/phidgetservo.c
index b3418d2bcc69..5a040c205eed 100644
--- a/drivers/usb/misc/phidgetservo.c
+++ b/drivers/usb/misc/phidgetservo.c
@@ -252,12 +252,11 @@ servo_probe(struct usb_interface *interface, const struct usb_device_id *id)
252 struct usb_device *udev = interface_to_usbdev(interface); 252 struct usb_device *udev = interface_to_usbdev(interface);
253 struct phidget_servo *dev; 253 struct phidget_servo *dev;
254 254
255 dev = kmalloc(sizeof (struct phidget_servo), GFP_KERNEL); 255 dev = kzalloc(sizeof (struct phidget_servo), GFP_KERNEL);
256 if (dev == NULL) { 256 if (dev == NULL) {
257 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__); 257 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
258 return -ENOMEM; 258 return -ENOMEM;
259 } 259 }
260 memset(dev, 0x00, sizeof (*dev));
261 260
262 dev->udev = usb_get_dev(udev); 261 dev->udev = usb_get_dev(udev);
263 dev->type = id->driver_info; 262 dev->type = id->driver_info;
diff --git a/drivers/usb/misc/sisusbvga/sisusb.c b/drivers/usb/misc/sisusbvga/sisusb.c
index 3260d595441f..196c8794a73c 100644
--- a/drivers/usb/misc/sisusbvga/sisusb.c
+++ b/drivers/usb/misc/sisusbvga/sisusb.c
@@ -3188,7 +3188,7 @@ sisusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
3188 break; 3188 break;
3189 3189
3190 default: 3190 default:
3191 retval = -EINVAL; 3191 retval = -ENOTTY;
3192 break; 3192 break;
3193 } 3193 }
3194 3194
@@ -3251,12 +3251,11 @@ static int sisusb_probe(struct usb_interface *intf,
3251 dev->devnum); 3251 dev->devnum);
3252 3252
3253 /* Allocate memory for our private */ 3253 /* Allocate memory for our private */
3254 if (!(sisusb = kmalloc(sizeof(*sisusb), GFP_KERNEL))) { 3254 if (!(sisusb = kzalloc(sizeof(*sisusb), GFP_KERNEL))) {
3255 printk(KERN_ERR 3255 printk(KERN_ERR
3256 "sisusb: Failed to allocate memory for private data\n"); 3256 "sisusb: Failed to allocate memory for private data\n");
3257 return -ENOMEM; 3257 return -ENOMEM;
3258 } 3258 }
3259 memset(sisusb, 0, sizeof(*sisusb));
3260 kref_init(&sisusb->kref); 3259 kref_init(&sisusb->kref);
3261 3260
3262 init_MUTEX(&(sisusb->lock)); 3261 init_MUTEX(&(sisusb->lock));
diff --git a/drivers/usb/misc/sisusbvga/sisusb.h b/drivers/usb/misc/sisusbvga/sisusb.h
index 1d7a77cc7c4a..a716825d1f9b 100644
--- a/drivers/usb/misc/sisusbvga/sisusb.h
+++ b/drivers/usb/misc/sisusbvga/sisusb.h
@@ -37,24 +37,16 @@
37#ifndef _SISUSB_H_ 37#ifndef _SISUSB_H_
38#define _SISUSB_H_ 38#define _SISUSB_H_
39 39
40#include <linux/version.h>
41#ifdef CONFIG_COMPAT 40#ifdef CONFIG_COMPAT
42#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,10)
43#include <linux/ioctl32.h>
44#define SISUSB_OLD_CONFIG_COMPAT
45#else
46#define SISUSB_NEW_CONFIG_COMPAT 41#define SISUSB_NEW_CONFIG_COMPAT
47#endif 42#endif
48#endif
49 43
50/* For older kernels, support for text consoles is by default 44/* For older kernels, support for text consoles is by default
51 * off. To ensable text console support, change the following: 45 * off. To ensable text console support, change the following:
52 */ 46 */
53#if 0 47#if 0
54#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,13)
55#define CONFIG_USB_SISUSBVGA_CON 48#define CONFIG_USB_SISUSBVGA_CON
56#endif 49#endif
57#endif
58 50
59/* Version Information */ 51/* Version Information */
60 52
diff --git a/drivers/usb/misc/usblcd.c b/drivers/usb/misc/usblcd.c
index cc3dae3f34e0..c82c402285a0 100644
--- a/drivers/usb/misc/usblcd.c
+++ b/drivers/usb/misc/usblcd.c
@@ -270,12 +270,11 @@ static int lcd_probe(struct usb_interface *interface, const struct usb_device_id
270 int retval = -ENOMEM; 270 int retval = -ENOMEM;
271 271
272 /* allocate memory for our device state and initialize it */ 272 /* allocate memory for our device state and initialize it */
273 dev = kmalloc(sizeof(*dev), GFP_KERNEL); 273 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
274 if (dev == NULL) { 274 if (dev == NULL) {
275 err("Out of memory"); 275 err("Out of memory");
276 goto error; 276 goto error;
277 } 277 }
278 memset(dev, 0x00, sizeof(*dev));
279 kref_init(&dev->kref); 278 kref_init(&dev->kref);
280 279
281 dev->udev = usb_get_dev(interface_to_usbdev(interface)); 280 dev->udev = usb_get_dev(interface_to_usbdev(interface));
diff --git a/drivers/usb/misc/usbled.c b/drivers/usb/misc/usbled.c
index 877b081a3a6e..f441964132c0 100644
--- a/drivers/usb/misc/usbled.c
+++ b/drivers/usb/misc/usbled.c
@@ -106,12 +106,11 @@ static int led_probe(struct usb_interface *interface, const struct usb_device_id
106 struct usb_led *dev = NULL; 106 struct usb_led *dev = NULL;
107 int retval = -ENOMEM; 107 int retval = -ENOMEM;
108 108
109 dev = kmalloc(sizeof(struct usb_led), GFP_KERNEL); 109 dev = kzalloc(sizeof(struct usb_led), GFP_KERNEL);
110 if (dev == NULL) { 110 if (dev == NULL) {
111 dev_err(&interface->dev, "Out of memory\n"); 111 dev_err(&interface->dev, "Out of memory\n");
112 goto error; 112 goto error;
113 } 113 }
114 memset (dev, 0x00, sizeof (*dev));
115 114
116 dev->udev = usb_get_dev(udev); 115 dev->udev = usb_get_dev(udev);
117 116
diff --git a/drivers/usb/misc/usbtest.c b/drivers/usb/misc/usbtest.c
index 84fa1728f052..9d59b901841c 100644
--- a/drivers/usb/misc/usbtest.c
+++ b/drivers/usb/misc/usbtest.c
@@ -382,12 +382,11 @@ alloc_sglist (int nents, int max, int vary)
382 for (i = 0; i < nents; i++) { 382 for (i = 0; i < nents; i++) {
383 char *buf; 383 char *buf;
384 384
385 buf = kmalloc (size, SLAB_KERNEL); 385 buf = kzalloc (size, SLAB_KERNEL);
386 if (!buf) { 386 if (!buf) {
387 free_sglist (sg, i); 387 free_sglist (sg, i);
388 return NULL; 388 return NULL;
389 } 389 }
390 memset (buf, 0, size);
391 390
392 /* kmalloc pages are always physically contiguous! */ 391 /* kmalloc pages are always physically contiguous! */
393 sg_init_one(&sg[i], buf, size); 392 sg_init_one(&sg[i], buf, size);
@@ -842,10 +841,9 @@ test_ctrl_queue (struct usbtest_dev *dev, struct usbtest_param *param)
842 * as with bulk/intr sglists, sglen is the queue depth; it also 841 * as with bulk/intr sglists, sglen is the queue depth; it also
843 * controls which subtests run (more tests than sglen) or rerun. 842 * controls which subtests run (more tests than sglen) or rerun.
844 */ 843 */
845 urb = kmalloc (param->sglen * sizeof (struct urb *), SLAB_KERNEL); 844 urb = kcalloc(param->sglen, sizeof(struct urb *), SLAB_KERNEL);
846 if (!urb) 845 if (!urb)
847 return -ENOMEM; 846 return -ENOMEM;
848 memset (urb, 0, param->sglen * sizeof (struct urb *));
849 for (i = 0; i < param->sglen; i++) { 847 for (i = 0; i < param->sglen; i++) {
850 int pipe = usb_rcvctrlpipe (udev, 0); 848 int pipe = usb_rcvctrlpipe (udev, 0);
851 unsigned len; 849 unsigned len;
@@ -1865,10 +1863,9 @@ usbtest_probe (struct usb_interface *intf, const struct usb_device_id *id)
1865 } 1863 }
1866#endif 1864#endif
1867 1865
1868 dev = kmalloc (sizeof *dev, SLAB_KERNEL); 1866 dev = kzalloc(sizeof(*dev), SLAB_KERNEL);
1869 if (!dev) 1867 if (!dev)
1870 return -ENOMEM; 1868 return -ENOMEM;
1871 memset (dev, 0, sizeof *dev);
1872 info = (struct usbtest_info *) id->driver_info; 1869 info = (struct usbtest_info *) id->driver_info;
1873 dev->info = info; 1870 dev->info = info;
1874 init_MUTEX (&dev->sem); 1871 init_MUTEX (&dev->sem);
diff --git a/drivers/usb/mon/mon_main.c b/drivers/usb/mon/mon_main.c
index c34944c75047..6ecc27302211 100644
--- a/drivers/usb/mon/mon_main.c
+++ b/drivers/usb/mon/mon_main.c
@@ -12,6 +12,7 @@
12#include <linux/debugfs.h> 12#include <linux/debugfs.h>
13#include <linux/smp_lock.h> 13#include <linux/smp_lock.h>
14#include <linux/notifier.h> 14#include <linux/notifier.h>
15#include <linux/mutex.h>
15 16
16#include "usb_mon.h" 17#include "usb_mon.h"
17#include "../core/hcd.h" 18#include "../core/hcd.h"
@@ -23,7 +24,7 @@ static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
23static void mon_bus_drop(struct kref *r); 24static void mon_bus_drop(struct kref *r);
24static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus); 25static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus);
25 26
26DECLARE_MUTEX(mon_lock); 27DEFINE_MUTEX(mon_lock);
27 28
28static struct dentry *mon_dir; /* /dbg/usbmon */ 29static struct dentry *mon_dir; /* /dbg/usbmon */
29static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */ 30static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
@@ -196,14 +197,14 @@ static void mon_bus_remove(struct usb_bus *ubus)
196{ 197{
197 struct mon_bus *mbus = ubus->mon_bus; 198 struct mon_bus *mbus = ubus->mon_bus;
198 199
199 down(&mon_lock); 200 mutex_lock(&mon_lock);
200 list_del(&mbus->bus_link); 201 list_del(&mbus->bus_link);
201 debugfs_remove(mbus->dent_t); 202 debugfs_remove(mbus->dent_t);
202 debugfs_remove(mbus->dent_s); 203 debugfs_remove(mbus->dent_s);
203 204
204 mon_dissolve(mbus, ubus); 205 mon_dissolve(mbus, ubus);
205 kref_put(&mbus->ref, mon_bus_drop); 206 kref_put(&mbus->ref, mon_bus_drop);
206 up(&mon_lock); 207 mutex_unlock(&mon_lock);
207} 208}
208 209
209static int mon_notify(struct notifier_block *self, unsigned long action, 210static int mon_notify(struct notifier_block *self, unsigned long action,
@@ -276,9 +277,8 @@ static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
276 char name[NAMESZ]; 277 char name[NAMESZ];
277 int rc; 278 int rc;
278 279
279 if ((mbus = kmalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL) 280 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
280 goto err_alloc; 281 goto err_alloc;
281 memset(mbus, 0, sizeof(struct mon_bus));
282 kref_init(&mbus->ref); 282 kref_init(&mbus->ref);
283 spin_lock_init(&mbus->lock); 283 spin_lock_init(&mbus->lock);
284 INIT_LIST_HEAD(&mbus->r_list); 284 INIT_LIST_HEAD(&mbus->r_list);
@@ -307,9 +307,9 @@ static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
307 goto err_create_s; 307 goto err_create_s;
308 mbus->dent_s = d; 308 mbus->dent_s = d;
309 309
310 down(&mon_lock); 310 mutex_lock(&mon_lock);
311 list_add_tail(&mbus->bus_link, &mon_buses); 311 list_add_tail(&mbus->bus_link, &mon_buses);
312 up(&mon_lock); 312 mutex_unlock(&mon_lock);
313 return; 313 return;
314 314
315err_create_s: 315err_create_s:
@@ -347,11 +347,11 @@ static int __init mon_init(void)
347 347
348 usb_register_notify(&mon_nb); 348 usb_register_notify(&mon_nb);
349 349
350 down(&usb_bus_list_lock); 350 mutex_lock(&usb_bus_list_lock);
351 list_for_each_entry (ubus, &usb_bus_list, bus_list) { 351 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
352 mon_bus_init(mondir, ubus); 352 mon_bus_init(mondir, ubus);
353 } 353 }
354 up(&usb_bus_list_lock); 354 mutex_unlock(&usb_bus_list_lock);
355 return 0; 355 return 0;
356} 356}
357 357
@@ -363,7 +363,7 @@ static void __exit mon_exit(void)
363 usb_unregister_notify(&mon_nb); 363 usb_unregister_notify(&mon_nb);
364 usb_mon_deregister(); 364 usb_mon_deregister();
365 365
366 down(&mon_lock); 366 mutex_lock(&mon_lock);
367 while (!list_empty(&mon_buses)) { 367 while (!list_empty(&mon_buses)) {
368 p = mon_buses.next; 368 p = mon_buses.next;
369 mbus = list_entry(p, struct mon_bus, bus_link); 369 mbus = list_entry(p, struct mon_bus, bus_link);
@@ -387,7 +387,7 @@ static void __exit mon_exit(void)
387 mon_dissolve(mbus, mbus->u_bus); 387 mon_dissolve(mbus, mbus->u_bus);
388 kref_put(&mbus->ref, mon_bus_drop); 388 kref_put(&mbus->ref, mon_bus_drop);
389 } 389 }
390 up(&mon_lock); 390 mutex_unlock(&mon_lock);
391 391
392 debugfs_remove(mon_dir); 392 debugfs_remove(mon_dir);
393} 393}
diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c
index 611612146ae9..ac043ec2b8dc 100644
--- a/drivers/usb/mon/mon_text.c
+++ b/drivers/usb/mon/mon_text.c
@@ -8,6 +8,7 @@
8#include <linux/list.h> 8#include <linux/list.h>
9#include <linux/usb.h> 9#include <linux/usb.h>
10#include <linux/time.h> 10#include <linux/time.h>
11#include <linux/mutex.h>
11#include <asm/uaccess.h> 12#include <asm/uaccess.h>
12 13
13#include "usb_mon.h" 14#include "usb_mon.h"
@@ -54,7 +55,7 @@ struct mon_reader_text {
54 wait_queue_head_t wait; 55 wait_queue_head_t wait;
55 int printf_size; 56 int printf_size;
56 char *printf_buf; 57 char *printf_buf;
57 struct semaphore printf_lock; 58 struct mutex printf_lock;
58 59
59 char slab_name[SLAB_NAME_SZ]; 60 char slab_name[SLAB_NAME_SZ];
60}; 61};
@@ -208,19 +209,18 @@ static int mon_text_open(struct inode *inode, struct file *file)
208 struct mon_reader_text *rp; 209 struct mon_reader_text *rp;
209 int rc; 210 int rc;
210 211
211 down(&mon_lock); 212 mutex_lock(&mon_lock);
212 mbus = inode->u.generic_ip; 213 mbus = inode->u.generic_ip;
213 ubus = mbus->u_bus; 214 ubus = mbus->u_bus;
214 215
215 rp = kmalloc(sizeof(struct mon_reader_text), GFP_KERNEL); 216 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
216 if (rp == NULL) { 217 if (rp == NULL) {
217 rc = -ENOMEM; 218 rc = -ENOMEM;
218 goto err_alloc; 219 goto err_alloc;
219 } 220 }
220 memset(rp, 0, sizeof(struct mon_reader_text));
221 INIT_LIST_HEAD(&rp->e_list); 221 INIT_LIST_HEAD(&rp->e_list);
222 init_waitqueue_head(&rp->wait); 222 init_waitqueue_head(&rp->wait);
223 init_MUTEX(&rp->printf_lock); 223 mutex_init(&rp->printf_lock);
224 224
225 rp->printf_size = PRINTF_DFL; 225 rp->printf_size = PRINTF_DFL;
226 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL); 226 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
@@ -247,7 +247,7 @@ static int mon_text_open(struct inode *inode, struct file *file)
247 mon_reader_add(mbus, &rp->r); 247 mon_reader_add(mbus, &rp->r);
248 248
249 file->private_data = rp; 249 file->private_data = rp;
250 up(&mon_lock); 250 mutex_unlock(&mon_lock);
251 return 0; 251 return 0;
252 252
253// err_busy: 253// err_busy:
@@ -257,7 +257,7 @@ err_slab:
257err_alloc_pr: 257err_alloc_pr:
258 kfree(rp); 258 kfree(rp);
259err_alloc: 259err_alloc:
260 up(&mon_lock); 260 mutex_unlock(&mon_lock);
261 return rc; 261 return rc;
262} 262}
263 263
@@ -301,7 +301,7 @@ static ssize_t mon_text_read(struct file *file, char __user *buf,
301 set_current_state(TASK_RUNNING); 301 set_current_state(TASK_RUNNING);
302 remove_wait_queue(&rp->wait, &waita); 302 remove_wait_queue(&rp->wait, &waita);
303 303
304 down(&rp->printf_lock); 304 mutex_lock(&rp->printf_lock);
305 cnt = 0; 305 cnt = 0;
306 pbuf = rp->printf_buf; 306 pbuf = rp->printf_buf;
307 limit = rp->printf_size; 307 limit = rp->printf_size;
@@ -358,7 +358,7 @@ static ssize_t mon_text_read(struct file *file, char __user *buf,
358 358
359 if (copy_to_user(buf, rp->printf_buf, cnt)) 359 if (copy_to_user(buf, rp->printf_buf, cnt))
360 cnt = -EFAULT; 360 cnt = -EFAULT;
361 up(&rp->printf_lock); 361 mutex_unlock(&rp->printf_lock);
362 kmem_cache_free(rp->e_slab, ep); 362 kmem_cache_free(rp->e_slab, ep);
363 return cnt; 363 return cnt;
364} 364}
@@ -371,12 +371,12 @@ static int mon_text_release(struct inode *inode, struct file *file)
371 struct list_head *p; 371 struct list_head *p;
372 struct mon_event_text *ep; 372 struct mon_event_text *ep;
373 373
374 down(&mon_lock); 374 mutex_lock(&mon_lock);
375 mbus = inode->u.generic_ip; 375 mbus = inode->u.generic_ip;
376 376
377 if (mbus->nreaders <= 0) { 377 if (mbus->nreaders <= 0) {
378 printk(KERN_ERR TAG ": consistency error on close\n"); 378 printk(KERN_ERR TAG ": consistency error on close\n");
379 up(&mon_lock); 379 mutex_unlock(&mon_lock);
380 return 0; 380 return 0;
381 } 381 }
382 mon_reader_del(mbus, &rp->r); 382 mon_reader_del(mbus, &rp->r);
@@ -402,7 +402,7 @@ static int mon_text_release(struct inode *inode, struct file *file)
402 kfree(rp->printf_buf); 402 kfree(rp->printf_buf);
403 kfree(rp); 403 kfree(rp);
404 404
405 up(&mon_lock); 405 mutex_unlock(&mon_lock);
406 return 0; 406 return 0;
407} 407}
408 408
diff --git a/drivers/usb/mon/usb_mon.h b/drivers/usb/mon/usb_mon.h
index 4be0f9346071..8e0613c350cc 100644
--- a/drivers/usb/mon/usb_mon.h
+++ b/drivers/usb/mon/usb_mon.h
@@ -49,7 +49,7 @@ void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r);
49 */ 49 */
50extern char mon_dmapeek(unsigned char *dst, dma_addr_t dma_addr, int len); 50extern char mon_dmapeek(unsigned char *dst, dma_addr_t dma_addr, int len);
51 51
52extern struct semaphore mon_lock; 52extern struct mutex mon_lock;
53 53
54extern struct file_operations mon_fops_text; 54extern struct file_operations mon_fops_text;
55extern struct file_operations mon_fops_stat; 55extern struct file_operations mon_fops_stat;
diff --git a/drivers/usb/net/pegasus.c b/drivers/usb/net/pegasus.c
index 156a2f1cb39a..5b6675684567 100644
--- a/drivers/usb/net/pegasus.c
+++ b/drivers/usb/net/pegasus.c
@@ -524,6 +524,7 @@ static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
524 ret = set_registers(pegasus, EthCtrl0, 3, data); 524 ret = set_registers(pegasus, EthCtrl0, 3, data);
525 525
526 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS || 526 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
527 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
527 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) { 528 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
528 u16 auxmode; 529 u16 auxmode;
529 read_mii_word(pegasus, 0, 0x1b, &auxmode); 530 read_mii_word(pegasus, 0, 0x1b, &auxmode);
diff --git a/drivers/usb/net/pegasus.h b/drivers/usb/net/pegasus.h
index 9fbd59b55cb6..a54752ce1493 100644
--- a/drivers/usb/net/pegasus.h
+++ b/drivers/usb/net/pegasus.h
@@ -25,7 +25,6 @@
25#define PHY_READ 0x40 25#define PHY_READ 0x40
26#define PHY_WRITE 0x20 26#define PHY_WRITE 0x20
27#define DEFAULT_GPIO_RESET 0x24 27#define DEFAULT_GPIO_RESET 0x24
28#define LINKSYS_GPIO_RESET 0x24
29#define DEFAULT_GPIO_SET 0x26 28#define DEFAULT_GPIO_SET 0x26
30 29
31#define PEGASUS_PRESENT 0x00000001 30#define PEGASUS_PRESENT 0x00000001
@@ -140,6 +139,7 @@ struct usb_eth_dev {
140#define VENDOR_KINGSTON 0x0951 139#define VENDOR_KINGSTON 0x0951
141#define VENDOR_LANEED 0x056e 140#define VENDOR_LANEED 0x056e
142#define VENDOR_LINKSYS 0x066b 141#define VENDOR_LINKSYS 0x066b
142#define VENDOR_LINKSYS2 0x077b
143#define VENDOR_MELCO 0x0411 143#define VENDOR_MELCO 0x0411
144#define VENDOR_MICROSOFT 0x045e 144#define VENDOR_MICROSOFT 0x045e
145#define VENDOR_MOBILITY 0x1342 145#define VENDOR_MOBILITY 0x1342
@@ -218,15 +218,15 @@ PEGASUS_DEV( "Corega FEter USB-TX", VENDOR_COREGA, 0x0004,
218PEGASUS_DEV( "Corega FEter USB-TXS", VENDOR_COREGA, 0x000d, 218PEGASUS_DEV( "Corega FEter USB-TXS", VENDOR_COREGA, 0x000d,
219 DEFAULT_GPIO_RESET | PEGASUS_II ) 219 DEFAULT_GPIO_RESET | PEGASUS_II )
220PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4001, 220PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4001,
221 LINKSYS_GPIO_RESET ) 221 DEFAULT_GPIO_RESET )
222PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4002, 222PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4002,
223 LINKSYS_GPIO_RESET ) 223 DEFAULT_GPIO_RESET )
224PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4102, 224PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4102,
225 LINKSYS_GPIO_RESET | PEGASUS_II ) 225 DEFAULT_GPIO_RESET | PEGASUS_II )
226PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x400b, 226PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x400b,
227 LINKSYS_GPIO_RESET | PEGASUS_II ) 227 DEFAULT_GPIO_RESET | PEGASUS_II )
228PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x200c, 228PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x200c,
229 LINKSYS_GPIO_RESET | PEGASUS_II ) 229 DEFAULT_GPIO_RESET | PEGASUS_II )
230PEGASUS_DEV( "D-Link DSB-650TX(PNA)", VENDOR_DLINK, 0x4003, 230PEGASUS_DEV( "D-Link DSB-650TX(PNA)", VENDOR_DLINK, 0x4003,
231 DEFAULT_GPIO_RESET | HAS_HOME_PNA ) 231 DEFAULT_GPIO_RESET | HAS_HOME_PNA )
232PEGASUS_DEV( "D-Link DSB-650", VENDOR_DLINK, 0xabc1, 232PEGASUS_DEV( "D-Link DSB-650", VENDOR_DLINK, 0xabc1,
@@ -260,17 +260,19 @@ PEGASUS_DEV( "LANEED USB Ethernet LD-USB/T", VENDOR_LANEED, 0xabc1,
260PEGASUS_DEV( "LANEED USB Ethernet LD-USB/TX", VENDOR_LANEED, 0x200c, 260PEGASUS_DEV( "LANEED USB Ethernet LD-USB/TX", VENDOR_LANEED, 0x200c,
261 DEFAULT_GPIO_RESET | PEGASUS_II ) 261 DEFAULT_GPIO_RESET | PEGASUS_II )
262PEGASUS_DEV( "Linksys USB10TX", VENDOR_LINKSYS, 0x2202, 262PEGASUS_DEV( "Linksys USB10TX", VENDOR_LINKSYS, 0x2202,
263 LINKSYS_GPIO_RESET ) 263 DEFAULT_GPIO_RESET )
264PEGASUS_DEV( "Linksys USB100TX", VENDOR_LINKSYS, 0x2203, 264PEGASUS_DEV( "Linksys USB100TX", VENDOR_LINKSYS, 0x2203,
265 LINKSYS_GPIO_RESET ) 265 DEFAULT_GPIO_RESET )
266PEGASUS_DEV( "Linksys USB100TX", VENDOR_LINKSYS, 0x2204, 266PEGASUS_DEV( "Linksys USB100TX", VENDOR_LINKSYS, 0x2204,
267 LINKSYS_GPIO_RESET | HAS_HOME_PNA ) 267 DEFAULT_GPIO_RESET | HAS_HOME_PNA )
268PEGASUS_DEV( "Linksys USB10T Ethernet Adapter", VENDOR_LINKSYS, 0x2206, 268PEGASUS_DEV( "Linksys USB10T Ethernet Adapter", VENDOR_LINKSYS, 0x2206,
269 LINKSYS_GPIO_RESET | PEGASUS_II) 269 DEFAULT_GPIO_RESET | PEGASUS_II)
270PEGASUS_DEV( "Linksys USBVPN1", VENDOR_LINKSYS2, 0x08b4,
271 DEFAULT_GPIO_RESET )
270PEGASUS_DEV( "Linksys USB USB100TX", VENDOR_LINKSYS, 0x400b, 272PEGASUS_DEV( "Linksys USB USB100TX", VENDOR_LINKSYS, 0x400b,
271 LINKSYS_GPIO_RESET | PEGASUS_II ) 273 DEFAULT_GPIO_RESET | PEGASUS_II )
272PEGASUS_DEV( "Linksys USB10TX", VENDOR_LINKSYS, 0x200c, 274PEGASUS_DEV( "Linksys USB10TX", VENDOR_LINKSYS, 0x200c,
273 LINKSYS_GPIO_RESET | PEGASUS_II ) 275 DEFAULT_GPIO_RESET | PEGASUS_II )
274PEGASUS_DEV( "MELCO/BUFFALO LUA-TX", VENDOR_MELCO, 0x0001, 276PEGASUS_DEV( "MELCO/BUFFALO LUA-TX", VENDOR_MELCO, 0x0001,
275 DEFAULT_GPIO_RESET ) 277 DEFAULT_GPIO_RESET )
276PEGASUS_DEV( "MELCO/BUFFALO LUA-TX", VENDOR_MELCO, 0x0005, 278PEGASUS_DEV( "MELCO/BUFFALO LUA-TX", VENDOR_MELCO, 0x0005,
diff --git a/drivers/usb/net/rtl8150.c b/drivers/usb/net/rtl8150.c
index 8ca52be23976..1bbbae283c0b 100644
--- a/drivers/usb/net/rtl8150.c
+++ b/drivers/usb/net/rtl8150.c
@@ -880,7 +880,6 @@ static int rtl8150_probe(struct usb_interface *intf,
880 } 880 }
881 fill_skb_pool(dev); 881 fill_skb_pool(dev);
882 set_ethernet_addr(dev); 882 set_ethernet_addr(dev);
883 info("%s: rtl8150 is detected", netdev->name);
884 883
885 usb_set_intfdata(intf, dev); 884 usb_set_intfdata(intf, dev);
886 SET_NETDEV_DEV(netdev, &intf->dev); 885 SET_NETDEV_DEV(netdev, &intf->dev);
@@ -888,6 +887,9 @@ static int rtl8150_probe(struct usb_interface *intf,
888 err("couldn't register the device"); 887 err("couldn't register the device");
889 goto out2; 888 goto out2;
890 } 889 }
890
891 info("%s: rtl8150 is detected", netdev->name);
892
891 return 0; 893 return 0;
892 894
893out2: 895out2:
diff --git a/drivers/usb/net/zaurus.c b/drivers/usb/net/zaurus.c
index 9c5ab251370c..f7ac9d6b9856 100644
--- a/drivers/usb/net/zaurus.c
+++ b/drivers/usb/net/zaurus.c
@@ -217,7 +217,7 @@ static int blan_mdlm_bind(struct usbnet *dev, struct usb_interface *intf)
217 * with devices that use it and those that don't. 217 * with devices that use it and those that don't.
218 */ 218 */
219 if ((detail->bDetailData[1] & ~0x02) != 0x01) { 219 if ((detail->bDetailData[1] & ~0x02) != 0x01) {
220 /* bmDataCapabilites == 0 would be fine too, 220 /* bmDataCapabilities == 0 would be fine too,
221 * but framing is minidriver-coupled for now. 221 * but framing is minidriver-coupled for now.
222 */ 222 */
223bad_detail: 223bad_detail:
diff --git a/drivers/usb/net/zd1201.c b/drivers/usb/net/zd1201.c
index f3a8e2807c3b..9b1e4ed1d07e 100644
--- a/drivers/usb/net/zd1201.c
+++ b/drivers/usb/net/zd1201.c
@@ -621,10 +621,9 @@ static int zd1201_drvr_start(struct zd1201 *zd)
621 __le16 zdmax; 621 __le16 zdmax;
622 unsigned char *buffer; 622 unsigned char *buffer;
623 623
624 buffer = kmalloc(ZD1201_RXSIZE, GFP_KERNEL); 624 buffer = kzalloc(ZD1201_RXSIZE, GFP_KERNEL);
625 if (!buffer) 625 if (!buffer)
626 return -ENOMEM; 626 return -ENOMEM;
627 memset(buffer, 0, ZD1201_RXSIZE);
628 627
629 usb_fill_bulk_urb(zd->rx_urb, zd->usb, 628 usb_fill_bulk_urb(zd->rx_urb, zd->usb,
630 usb_rcvbulkpipe(zd->usb, zd->endp_in), buffer, ZD1201_RXSIZE, 629 usb_rcvbulkpipe(zd->usb, zd->endp_in), buffer, ZD1201_RXSIZE,
@@ -1737,6 +1736,7 @@ static const struct iw_handler_def zd1201_iw_handlers = {
1737 .standard = (iw_handler *)zd1201_iw_handler, 1736 .standard = (iw_handler *)zd1201_iw_handler,
1738 .private = (iw_handler *)zd1201_private_handler, 1737 .private = (iw_handler *)zd1201_private_handler,
1739 .private_args = (struct iw_priv_args *) zd1201_private_args, 1738 .private_args = (struct iw_priv_args *) zd1201_private_args,
1739 .get_wireless_stats = zd1201_get_wireless_stats,
1740}; 1740};
1741 1741
1742static int zd1201_probe(struct usb_interface *interface, 1742static int zd1201_probe(struct usb_interface *interface,
@@ -1750,11 +1750,9 @@ static int zd1201_probe(struct usb_interface *interface,
1750 1750
1751 usb = interface_to_usbdev(interface); 1751 usb = interface_to_usbdev(interface);
1752 1752
1753 zd = kmalloc(sizeof(struct zd1201), GFP_KERNEL); 1753 zd = kzalloc(sizeof(struct zd1201), GFP_KERNEL);
1754 if (!zd) { 1754 if (!zd)
1755 return -ENOMEM; 1755 return -ENOMEM;
1756 }
1757 memset(zd, 0, sizeof(struct zd1201));
1758 zd->ap = ap; 1756 zd->ap = ap;
1759 zd->usb = usb; 1757 zd->usb = usb;
1760 zd->removed = 0; 1758 zd->removed = 0;
@@ -1799,7 +1797,6 @@ static int zd1201_probe(struct usb_interface *interface,
1799 zd->dev->open = zd1201_net_open; 1797 zd->dev->open = zd1201_net_open;
1800 zd->dev->stop = zd1201_net_stop; 1798 zd->dev->stop = zd1201_net_stop;
1801 zd->dev->get_stats = zd1201_get_stats; 1799 zd->dev->get_stats = zd1201_get_stats;
1802 zd->dev->get_wireless_stats = zd1201_get_wireless_stats;
1803 zd->dev->wireless_handlers = 1800 zd->dev->wireless_handlers =
1804 (struct iw_handler_def *)&zd1201_iw_handlers; 1801 (struct iw_handler_def *)&zd1201_iw_handlers;
1805 zd->dev->hard_start_xmit = zd1201_hard_start_xmit; 1802 zd->dev->hard_start_xmit = zd1201_hard_start_xmit;
diff --git a/drivers/usb/serial/Kconfig b/drivers/usb/serial/Kconfig
index be5dc80836c3..5a8a2c91c2b2 100644
--- a/drivers/usb/serial/Kconfig
+++ b/drivers/usb/serial/Kconfig
@@ -403,6 +403,13 @@ config USB_SERIAL_MCT_U232
403 To compile this driver as a module, choose M here: the 403 To compile this driver as a module, choose M here: the
404 module will be called mct_u232. 404 module will be called mct_u232.
405 405
406config USB_SERIAL_NAVMAN
407 tristate "USB Navman GPS device"
408 depends on USB_SERIAL
409 help
410 To compile this driver as a module, choose M here: the
411 module will be called navman.
412
406config USB_SERIAL_PL2303 413config USB_SERIAL_PL2303
407 tristate "USB Prolific 2303 Single Port Serial Driver" 414 tristate "USB Prolific 2303 Single Port Serial Driver"
408 depends on USB_SERIAL 415 depends on USB_SERIAL
diff --git a/drivers/usb/serial/Makefile b/drivers/usb/serial/Makefile
index f0b04420cea1..f7fe4172efed 100644
--- a/drivers/usb/serial/Makefile
+++ b/drivers/usb/serial/Makefile
@@ -32,6 +32,7 @@ obj-$(CONFIG_USB_SERIAL_KEYSPAN_PDA) += keyspan_pda.o
32obj-$(CONFIG_USB_SERIAL_KLSI) += kl5kusb105.o 32obj-$(CONFIG_USB_SERIAL_KLSI) += kl5kusb105.o
33obj-$(CONFIG_USB_SERIAL_KOBIL_SCT) += kobil_sct.o 33obj-$(CONFIG_USB_SERIAL_KOBIL_SCT) += kobil_sct.o
34obj-$(CONFIG_USB_SERIAL_MCT_U232) += mct_u232.o 34obj-$(CONFIG_USB_SERIAL_MCT_U232) += mct_u232.o
35obj-$(CONFIG_USB_SERIAL_NAVMAN) += navman.o
35obj-$(CONFIG_USB_SERIAL_OMNINET) += omninet.o 36obj-$(CONFIG_USB_SERIAL_OMNINET) += omninet.o
36obj-$(CONFIG_USB_SERIAL_OPTION) += option.o 37obj-$(CONFIG_USB_SERIAL_OPTION) += option.o
37obj-$(CONFIG_USB_SERIAL_PL2303) += pl2303.o 38obj-$(CONFIG_USB_SERIAL_PL2303) += pl2303.o
diff --git a/drivers/usb/serial/cp2101.c b/drivers/usb/serial/cp2101.c
index dc7a069503e0..e0c2acdb3f06 100644
--- a/drivers/usb/serial/cp2101.c
+++ b/drivers/usb/serial/cp2101.c
@@ -32,7 +32,7 @@
32/* 32/*
33 * Version Information 33 * Version Information
34 */ 34 */
35#define DRIVER_VERSION "v0.06" 35#define DRIVER_VERSION "v0.07"
36#define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver" 36#define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver"
37 37
38/* 38/*
@@ -58,6 +58,7 @@ static struct usb_device_id id_table [] = {
58 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */ 58 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
59 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */ 59 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
60 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */ 60 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
61 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
61 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */ 62 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
62 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */ 63 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
63 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */ 64 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
@@ -169,9 +170,7 @@ static int cp2101_get_config(struct usb_serial_port* port, u8 request,
169 /* Number of integers required to contain the array */ 170 /* Number of integers required to contain the array */
170 length = (((size - 1) | 3) + 1)/4; 171 length = (((size - 1) | 3) + 1)/4;
171 172
172 buf = kmalloc (length * sizeof(u32), GFP_KERNEL); 173 buf = kcalloc(length, sizeof(u32), GFP_KERNEL);
173 memset(buf, 0, length * sizeof(u32));
174
175 if (!buf) { 174 if (!buf) {
176 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__); 175 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
177 return -ENOMEM; 176 return -ENOMEM;
diff --git a/drivers/usb/serial/cypress_m8.c b/drivers/usb/serial/cypress_m8.c
index 68067fe117a4..7212fbe3b6f2 100644
--- a/drivers/usb/serial/cypress_m8.c
+++ b/drivers/usb/serial/cypress_m8.c
@@ -98,10 +98,16 @@ static struct usb_device_id id_table_cyphidcomrs232 [] = {
98 { } /* Terminating entry */ 98 { } /* Terminating entry */
99}; 99};
100 100
101static struct usb_device_id id_table_nokiaca42v2 [] = {
102 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
103 { } /* Terminating entry */
104};
105
101static struct usb_device_id id_table_combined [] = { 106static struct usb_device_id id_table_combined [] = {
102 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) }, 107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
103 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) }, 108 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
104 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) }, 109 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
110 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
105 { } /* Terminating entry */ 111 { } /* Terminating entry */
106}; 112};
107 113
@@ -149,6 +155,7 @@ struct cypress_buf {
149/* function prototypes for the Cypress USB to serial device */ 155/* function prototypes for the Cypress USB to serial device */
150static int cypress_earthmate_startup (struct usb_serial *serial); 156static int cypress_earthmate_startup (struct usb_serial *serial);
151static int cypress_hidcom_startup (struct usb_serial *serial); 157static int cypress_hidcom_startup (struct usb_serial *serial);
158static int cypress_ca42v2_startup (struct usb_serial *serial);
152static void cypress_shutdown (struct usb_serial *serial); 159static void cypress_shutdown (struct usb_serial *serial);
153static int cypress_open (struct usb_serial_port *port, struct file *filp); 160static int cypress_open (struct usb_serial_port *port, struct file *filp);
154static void cypress_close (struct usb_serial_port *port, struct file *filp); 161static void cypress_close (struct usb_serial_port *port, struct file *filp);
@@ -235,6 +242,34 @@ static struct usb_serial_driver cypress_hidcom_device = {
235 .write_int_callback = cypress_write_int_callback, 242 .write_int_callback = cypress_write_int_callback,
236}; 243};
237 244
245static struct usb_serial_driver cypress_ca42v2_device = {
246 .driver = {
247 .owner = THIS_MODULE,
248 .name = "nokiaca42v2",
249 },
250 .description = "Nokia CA-42 V2 Adapter",
251 .id_table = id_table_nokiaca42v2,
252 .num_interrupt_in = 1,
253 .num_interrupt_out = 1,
254 .num_bulk_in = NUM_DONT_CARE,
255 .num_bulk_out = NUM_DONT_CARE,
256 .num_ports = 1,
257 .attach = cypress_ca42v2_startup,
258 .shutdown = cypress_shutdown,
259 .open = cypress_open,
260 .close = cypress_close,
261 .write = cypress_write,
262 .write_room = cypress_write_room,
263 .ioctl = cypress_ioctl,
264 .set_termios = cypress_set_termios,
265 .tiocmget = cypress_tiocmget,
266 .tiocmset = cypress_tiocmset,
267 .chars_in_buffer = cypress_chars_in_buffer,
268 .throttle = cypress_throttle,
269 .unthrottle = cypress_unthrottle,
270 .read_int_callback = cypress_read_int_callback,
271 .write_int_callback = cypress_write_int_callback,
272};
238 273
239/***************************************************************************** 274/*****************************************************************************
240 * Cypress serial helper functions 275 * Cypress serial helper functions
@@ -286,6 +321,12 @@ static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_m
286 __FUNCTION__); 321 __FUNCTION__);
287 new_baudrate = priv->baud_rate; 322 new_baudrate = priv->baud_rate;
288 } 323 }
324 } else if (priv->chiptype == CT_CA42V2) {
325 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
326 err("%s - failed setting baud rate, unsupported speed",
327 __FUNCTION__);
328 new_baudrate = priv->baud_rate;
329 }
289 } else if (priv->chiptype == CT_GENERIC) { 330 } else if (priv->chiptype == CT_GENERIC) {
290 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { 331 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
291 err("%s - failed setting baud rate, unsupported speed", 332 err("%s - failed setting baud rate, unsupported speed",
@@ -435,11 +476,10 @@ static int generic_startup (struct usb_serial *serial)
435 476
436 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number); 477 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
437 478
438 priv = kmalloc(sizeof (struct cypress_private), GFP_KERNEL); 479 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
439 if (!priv) 480 if (!priv)
440 return -ENOMEM; 481 return -ENOMEM;
441 482
442 memset(priv, 0x00, sizeof (struct cypress_private));
443 spin_lock_init(&priv->lock); 483 spin_lock_init(&priv->lock);
444 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE); 484 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
445 if (priv->buf == NULL) { 485 if (priv->buf == NULL) {
@@ -500,6 +540,25 @@ static int cypress_hidcom_startup (struct usb_serial *serial)
500} /* cypress_hidcom_startup */ 540} /* cypress_hidcom_startup */
501 541
502 542
543static int cypress_ca42v2_startup (struct usb_serial *serial)
544{
545 struct cypress_private *priv;
546
547 dbg("%s", __FUNCTION__);
548
549 if (generic_startup(serial)) {
550 dbg("%s - Failed setting up port %d", __FUNCTION__,
551 serial->port[0]->number);
552 return 1;
553 }
554
555 priv = usb_get_serial_port_data(serial->port[0]);
556 priv->chiptype = CT_CA42V2;
557
558 return 0;
559} /* cypress_ca42v2_startup */
560
561
503static void cypress_shutdown (struct usb_serial *serial) 562static void cypress_shutdown (struct usb_serial *serial)
504{ 563{
505 struct cypress_private *priv; 564 struct cypress_private *priv;
@@ -944,6 +1003,10 @@ static void cypress_set_termios (struct usb_serial_port *port,
944 *(tty->termios) = tty_std_termios; 1003 *(tty->termios) = tty_std_termios;
945 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | 1004 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
946 CLOCAL; 1005 CLOCAL;
1006 } else if (priv->chiptype == CT_CA42V2) {
1007 *(tty->termios) = tty_std_termios;
1008 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1009 CLOCAL;
947 } 1010 }
948 priv->termios_initialized = 1; 1011 priv->termios_initialized = 1;
949 } 1012 }
@@ -1542,6 +1605,9 @@ static int __init cypress_init(void)
1542 retval = usb_serial_register(&cypress_hidcom_device); 1605 retval = usb_serial_register(&cypress_hidcom_device);
1543 if (retval) 1606 if (retval)
1544 goto failed_hidcom_register; 1607 goto failed_hidcom_register;
1608 retval = usb_serial_register(&cypress_ca42v2_device);
1609 if (retval)
1610 goto failed_ca42v2_register;
1545 retval = usb_register(&cypress_driver); 1611 retval = usb_register(&cypress_driver);
1546 if (retval) 1612 if (retval)
1547 goto failed_usb_register; 1613 goto failed_usb_register;
@@ -1550,6 +1616,8 @@ static int __init cypress_init(void)
1550 return 0; 1616 return 0;
1551failed_usb_register: 1617failed_usb_register:
1552 usb_deregister(&cypress_driver); 1618 usb_deregister(&cypress_driver);
1619failed_ca42v2_register:
1620 usb_serial_deregister(&cypress_ca42v2_device);
1553failed_hidcom_register: 1621failed_hidcom_register:
1554 usb_serial_deregister(&cypress_hidcom_device); 1622 usb_serial_deregister(&cypress_hidcom_device);
1555failed_em_register: 1623failed_em_register:
@@ -1566,6 +1634,7 @@ static void __exit cypress_exit (void)
1566 usb_deregister (&cypress_driver); 1634 usb_deregister (&cypress_driver);
1567 usb_serial_deregister (&cypress_earthmate_device); 1635 usb_serial_deregister (&cypress_earthmate_device);
1568 usb_serial_deregister (&cypress_hidcom_device); 1636 usb_serial_deregister (&cypress_hidcom_device);
1637 usb_serial_deregister (&cypress_ca42v2_device);
1569} 1638}
1570 1639
1571 1640
diff --git a/drivers/usb/serial/cypress_m8.h b/drivers/usb/serial/cypress_m8.h
index 1fa119efe41a..e1c7c27e18b7 100644
--- a/drivers/usb/serial/cypress_m8.h
+++ b/drivers/usb/serial/cypress_m8.h
@@ -18,6 +18,10 @@
18/* Cypress HID->COM RS232 Adapter */ 18/* Cypress HID->COM RS232 Adapter */
19#define VENDOR_ID_CYPRESS 0x04b4 19#define VENDOR_ID_CYPRESS 0x04b4
20#define PRODUCT_ID_CYPHIDCOM 0x5500 20#define PRODUCT_ID_CYPHIDCOM 0x5500
21
22/* Nokia CA-42 USB to serial cable */
23#define VENDOR_ID_DAZZLE 0x07d0
24#define PRODUCT_ID_CA42 0x4101
21/* End of device listing */ 25/* End of device listing */
22 26
23/* Used for setting / requesting serial line settings */ 27/* Used for setting / requesting serial line settings */
@@ -34,6 +38,7 @@
34 38
35#define CT_EARTHMATE 0x01 39#define CT_EARTHMATE 0x01
36#define CT_CYPHIDCOM 0x02 40#define CT_CYPHIDCOM 0x02
41#define CT_CA42V2 0x03
37#define CT_GENERIC 0x0F 42#define CT_GENERIC 0x0F
38/* End of chiptype definitions */ 43/* End of chiptype definitions */
39 44
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index c145e1ed8429..f3af81b4dd29 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -492,6 +492,7 @@ static struct usb_device_id id_table_combined [] = {
492 { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_777_PID) }, 492 { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_777_PID) },
493 { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_8900F_PID) }, 493 { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_8900F_PID) },
494 { USB_DEVICE(FTDI_VID, FTDI_PCDJ_DAC2_PID) }, 494 { USB_DEVICE(FTDI_VID, FTDI_PCDJ_DAC2_PID) },
495 { USB_DEVICE(ICOM_ID1_VID, ICOM_ID1_PID) },
495 { }, /* Optional parameter entry */ 496 { }, /* Optional parameter entry */
496 { } /* Terminating entry */ 497 { } /* Terminating entry */
497}; 498};
@@ -1141,12 +1142,11 @@ static int ftdi_sio_attach (struct usb_serial *serial)
1141 1142
1142 dbg("%s",__FUNCTION__); 1143 dbg("%s",__FUNCTION__);
1143 1144
1144 priv = kmalloc(sizeof(struct ftdi_private), GFP_KERNEL); 1145 priv = kzalloc(sizeof(struct ftdi_private), GFP_KERNEL);
1145 if (!priv){ 1146 if (!priv){
1146 err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct ftdi_private)); 1147 err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct ftdi_private));
1147 return -ENOMEM; 1148 return -ENOMEM;
1148 } 1149 }
1149 memset(priv, 0, sizeof(*priv));
1150 1150
1151 spin_lock_init(&priv->rx_lock); 1151 spin_lock_init(&priv->rx_lock);
1152 init_waitqueue_head(&priv->delta_msr_wait); 1152 init_waitqueue_head(&priv->delta_msr_wait);
diff --git a/drivers/usb/serial/ftdi_sio.h b/drivers/usb/serial/ftdi_sio.h
index bdef3b8c731f..8da773c2744d 100644
--- a/drivers/usb/serial/ftdi_sio.h
+++ b/drivers/usb/serial/ftdi_sio.h
@@ -146,6 +146,13 @@
146#define KOBIL_CONV_KAAN_PID 0x2021 /* KOBIL_Konverter for KAAN */ 146#define KOBIL_CONV_KAAN_PID 0x2021 /* KOBIL_Konverter for KAAN */
147 147
148/* 148/*
149 * Icom ID-1 digital transceiver
150 */
151
152#define ICOM_ID1_VID 0x0C26
153#define ICOM_ID1_PID 0x0004
154
155/*
149 * DSS-20 Sync Station for Sony Ericsson P800 156 * DSS-20 Sync Station for Sony Ericsson P800
150 */ 157 */
151 158
diff --git a/drivers/usb/serial/garmin_gps.c b/drivers/usb/serial/garmin_gps.c
index d6f55e9dccae..5ec9bf5bac8d 100644
--- a/drivers/usb/serial/garmin_gps.c
+++ b/drivers/usb/serial/garmin_gps.c
@@ -1422,12 +1422,11 @@ static int garmin_attach (struct usb_serial *serial)
1422 1422
1423 dbg("%s", __FUNCTION__); 1423 dbg("%s", __FUNCTION__);
1424 1424
1425 garmin_data_p = kmalloc (sizeof(struct garmin_data), GFP_KERNEL); 1425 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1426 if (garmin_data_p == NULL) { 1426 if (garmin_data_p == NULL) {
1427 dev_err(&port->dev, "%s - Out of memory\n", __FUNCTION__); 1427 dev_err(&port->dev, "%s - Out of memory\n", __FUNCTION__);
1428 return -ENOMEM; 1428 return -ENOMEM;
1429 } 1429 }
1430 memset (garmin_data_p, 0, sizeof(struct garmin_data));
1431 init_timer(&garmin_data_p->timer); 1430 init_timer(&garmin_data_p->timer);
1432 spin_lock_init(&garmin_data_p->lock); 1431 spin_lock_init(&garmin_data_p->lock);
1433 INIT_LIST_HEAD(&garmin_data_p->pktlist); 1432 INIT_LIST_HEAD(&garmin_data_p->pktlist);
diff --git a/drivers/usb/serial/io_edgeport.c b/drivers/usb/serial/io_edgeport.c
index 3f29e6b0fd19..b606c5968102 100644
--- a/drivers/usb/serial/io_edgeport.c
+++ b/drivers/usb/serial/io_edgeport.c
@@ -2725,12 +2725,11 @@ static int edge_startup (struct usb_serial *serial)
2725 dev = serial->dev; 2725 dev = serial->dev;
2726 2726
2727 /* create our private serial structure */ 2727 /* create our private serial structure */
2728 edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL); 2728 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2729 if (edge_serial == NULL) { 2729 if (edge_serial == NULL) {
2730 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__); 2730 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2731 return -ENOMEM; 2731 return -ENOMEM;
2732 } 2732 }
2733 memset (edge_serial, 0, sizeof(struct edgeport_serial));
2734 spin_lock_init(&edge_serial->es_lock); 2733 spin_lock_init(&edge_serial->es_lock);
2735 edge_serial->serial = serial; 2734 edge_serial->serial = serial;
2736 usb_set_serial_data(serial, edge_serial); 2735 usb_set_serial_data(serial, edge_serial);
diff --git a/drivers/usb/serial/io_ti.c b/drivers/usb/serial/io_ti.c
index afc0f34b3a46..8e1e2253748b 100644
--- a/drivers/usb/serial/io_ti.c
+++ b/drivers/usb/serial/io_ti.c
@@ -2727,12 +2727,11 @@ static int edge_startup (struct usb_serial *serial)
2727 dev = serial->dev; 2727 dev = serial->dev;
2728 2728
2729 /* create our private serial structure */ 2729 /* create our private serial structure */
2730 edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL); 2730 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2731 if (edge_serial == NULL) { 2731 if (edge_serial == NULL) {
2732 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__); 2732 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2733 return -ENOMEM; 2733 return -ENOMEM;
2734 } 2734 }
2735 memset (edge_serial, 0, sizeof(struct edgeport_serial));
2736 sema_init(&edge_serial->es_sem, 1); 2735 sema_init(&edge_serial->es_sem, 1);
2737 edge_serial->serial = serial; 2736 edge_serial->serial = serial;
2738 usb_set_serial_data(serial, edge_serial); 2737 usb_set_serial_data(serial, edge_serial);
@@ -2745,12 +2744,11 @@ static int edge_startup (struct usb_serial *serial)
2745 2744
2746 /* set up our port private structures */ 2745 /* set up our port private structures */
2747 for (i = 0; i < serial->num_ports; ++i) { 2746 for (i = 0; i < serial->num_ports; ++i) {
2748 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL); 2747 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2749 if (edge_port == NULL) { 2748 if (edge_port == NULL) {
2750 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__); 2749 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2751 goto cleanup; 2750 goto cleanup;
2752 } 2751 }
2753 memset (edge_port, 0, sizeof(struct edgeport_port));
2754 spin_lock_init(&edge_port->ep_lock); 2752 spin_lock_init(&edge_port->ep_lock);
2755 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE); 2753 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2756 if (edge_port->ep_out_buf == NULL) { 2754 if (edge_port->ep_out_buf == NULL) {
diff --git a/drivers/usb/serial/ir-usb.c b/drivers/usb/serial/ir-usb.c
index a59010421444..426182ddc42a 100644
--- a/drivers/usb/serial/ir-usb.c
+++ b/drivers/usb/serial/ir-usb.c
@@ -184,10 +184,9 @@ static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev,
184 struct irda_class_desc *desc; 184 struct irda_class_desc *desc;
185 int ret; 185 int ret;
186 186
187 desc = kmalloc(sizeof (struct irda_class_desc), GFP_KERNEL); 187 desc = kzalloc(sizeof (struct irda_class_desc), GFP_KERNEL);
188 if (desc == NULL) 188 if (desc == NULL)
189 return NULL; 189 return NULL;
190 memset(desc, 0, sizeof(struct irda_class_desc));
191 190
192 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0), 191 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
193 IU_REQ_GET_CLASS_DESC, 192 IU_REQ_GET_CLASS_DESC,
diff --git a/drivers/usb/serial/keyspan.c b/drivers/usb/serial/keyspan.c
index 3b958e60f5e8..052b735c4fbd 100644
--- a/drivers/usb/serial/keyspan.c
+++ b/drivers/usb/serial/keyspan.c
@@ -2250,12 +2250,11 @@ static int keyspan_startup (struct usb_serial *serial)
2250 } 2250 }
2251 2251
2252 /* Setup private data for serial driver */ 2252 /* Setup private data for serial driver */
2253 s_priv = kmalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL); 2253 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2254 if (!s_priv) { 2254 if (!s_priv) {
2255 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__); 2255 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2256 return -ENOMEM; 2256 return -ENOMEM;
2257 } 2257 }
2258 memset(s_priv, 0, sizeof(struct keyspan_serial_private));
2259 2258
2260 s_priv->device_details = d_details; 2259 s_priv->device_details = d_details;
2261 usb_set_serial_data(serial, s_priv); 2260 usb_set_serial_data(serial, s_priv);
@@ -2263,12 +2262,11 @@ static int keyspan_startup (struct usb_serial *serial)
2263 /* Now setup per port private data */ 2262 /* Now setup per port private data */
2264 for (i = 0; i < serial->num_ports; i++) { 2263 for (i = 0; i < serial->num_ports; i++) {
2265 port = serial->port[i]; 2264 port = serial->port[i];
2266 p_priv = kmalloc(sizeof(struct keyspan_port_private), GFP_KERNEL); 2265 p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2267 if (!p_priv) { 2266 if (!p_priv) {
2268 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i); 2267 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2269 return (1); 2268 return (1);
2270 } 2269 }
2271 memset(p_priv, 0, sizeof(struct keyspan_port_private));
2272 p_priv->device_details = d_details; 2270 p_priv->device_details = d_details;
2273 usb_set_serial_port_data(port, p_priv); 2271 usb_set_serial_port_data(port, p_priv);
2274 } 2272 }
diff --git a/drivers/usb/serial/kobil_sct.c b/drivers/usb/serial/kobil_sct.c
index b8b213185d0f..87dfcd89ffab 100644
--- a/drivers/usb/serial/kobil_sct.c
+++ b/drivers/usb/serial/kobil_sct.c
@@ -255,11 +255,9 @@ static int kobil_open (struct usb_serial_port *port, struct file *filp)
255 } 255 }
256 256
257 // allocate memory for transfer buffer 257 // allocate memory for transfer buffer
258 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL); 258 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
259 if (! transfer_buffer) { 259 if (! transfer_buffer) {
260 return -ENOMEM; 260 return -ENOMEM;
261 } else {
262 memset(transfer_buffer, 0, transfer_buffer_length);
263 } 261 }
264 262
265 // allocate write_urb 263 // allocate write_urb
@@ -383,11 +381,10 @@ static void kobil_read_int_callback( struct urb *purb, struct pt_regs *regs)
383 381
384 // BEGIN DEBUG 382 // BEGIN DEBUG
385 /* 383 /*
386 dbg_data = (unsigned char *) kmalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL); 384 dbg_data = kzalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
387 if (! dbg_data) { 385 if (! dbg_data) {
388 return; 386 return;
389 } 387 }
390 memset(dbg_data, 0, (3 * purb->actual_length + 10));
391 for (i = 0; i < purb->actual_length; i++) { 388 for (i = 0; i < purb->actual_length; i++) {
392 sprintf(dbg_data +3*i, "%02X ", data[i]); 389 sprintf(dbg_data +3*i, "%02X ", data[i]);
393 } 390 }
@@ -518,11 +515,10 @@ static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
518 } 515 }
519 516
520 // allocate memory for transfer buffer 517 // allocate memory for transfer buffer
521 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL); 518 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
522 if (!transfer_buffer) { 519 if (!transfer_buffer) {
523 return -ENOMEM; 520 return -ENOMEM;
524 } 521 }
525 memset(transfer_buffer, 0, transfer_buffer_length);
526 522
527 result = usb_control_msg( port->serial->dev, 523 result = usb_control_msg( port->serial->dev,
528 usb_rcvctrlpipe(port->serial->dev, 0 ), 524 usb_rcvctrlpipe(port->serial->dev, 0 ),
@@ -564,11 +560,10 @@ static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
564 } 560 }
565 561
566 // allocate memory for transfer buffer 562 // allocate memory for transfer buffer
567 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL); 563 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
568 if (! transfer_buffer) { 564 if (! transfer_buffer) {
569 return -ENOMEM; 565 return -ENOMEM;
570 } 566 }
571 memset(transfer_buffer, 0, transfer_buffer_length);
572 567
573 if (set & TIOCM_RTS) 568 if (set & TIOCM_RTS)
574 rts = 1; 569 rts = 1;
@@ -655,11 +650,10 @@ static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
655 (struct termios __user *)arg)) 650 (struct termios __user *)arg))
656 return -EFAULT; 651 return -EFAULT;
657 652
658 settings = (unsigned char *) kmalloc(50, GFP_KERNEL); 653 settings = kzalloc(50, GFP_KERNEL);
659 if (! settings) { 654 if (! settings) {
660 return -ENOBUFS; 655 return -ENOBUFS;
661 } 656 }
662 memset(settings, 0, 50);
663 657
664 switch (priv->internal_termios.c_cflag & CBAUD) { 658 switch (priv->internal_termios.c_cflag & CBAUD) {
665 case B1200: 659 case B1200:
diff --git a/drivers/usb/serial/mct_u232.c b/drivers/usb/serial/mct_u232.c
index b6d6cab9c859..35bd29b6c408 100644
--- a/drivers/usb/serial/mct_u232.c
+++ b/drivers/usb/serial/mct_u232.c
@@ -348,10 +348,9 @@ static int mct_u232_startup (struct usb_serial *serial)
348 struct mct_u232_private *priv; 348 struct mct_u232_private *priv;
349 struct usb_serial_port *port, *rport; 349 struct usb_serial_port *port, *rport;
350 350
351 priv = kmalloc(sizeof(struct mct_u232_private), GFP_KERNEL); 351 priv = kzalloc(sizeof(struct mct_u232_private), GFP_KERNEL);
352 if (!priv) 352 if (!priv)
353 return -ENOMEM; 353 return -ENOMEM;
354 memset(priv, 0, sizeof(struct mct_u232_private));
355 spin_lock_init(&priv->lock); 354 spin_lock_init(&priv->lock);
356 usb_set_serial_port_data(serial->port[0], priv); 355 usb_set_serial_port_data(serial->port[0], priv);
357 356
diff --git a/drivers/usb/serial/navman.c b/drivers/usb/serial/navman.c
new file mode 100644
index 000000000000..7f544081032e
--- /dev/null
+++ b/drivers/usb/serial/navman.c
@@ -0,0 +1,157 @@
1/*
2 * Navman Serial USB driver
3 *
4 * Copyright (C) 2006 Greg Kroah-Hartman <gregkh@suse.de>
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
8 * version 2 as published by the Free Software Foundation.
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/tty.h>
14#include <linux/tty_flip.h>
15#include <linux/module.h>
16#include <linux/usb.h>
17#include "usb-serial.h"
18
19static int debug;
20
21static struct usb_device_id id_table [] = {
22 { USB_DEVICE(0x0a99, 0x0001) }, /* Talon Technology device */
23 { },
24};
25MODULE_DEVICE_TABLE(usb, id_table);
26
27static struct usb_driver navman_driver = {
28 .name = "navman",
29 .probe = usb_serial_probe,
30 .disconnect = usb_serial_disconnect,
31 .id_table = id_table,
32 .no_dynamic_id = 1,
33};
34
35static void navman_read_int_callback(struct urb *urb, struct pt_regs *regs)
36{
37 struct usb_serial_port *port = urb->context;
38 unsigned char *data = urb->transfer_buffer;
39 struct tty_struct *tty;
40 int result;
41
42 switch (urb->status) {
43 case 0:
44 /* success */
45 break;
46 case -ECONNRESET:
47 case -ENOENT:
48 case -ESHUTDOWN:
49 /* this urb is terminated, clean up */
50 dbg("%s - urb shutting down with status: %d",
51 __FUNCTION__, urb->status);
52 return;
53 default:
54 dbg("%s - nonzero urb status received: %d",
55 __FUNCTION__, urb->status);
56 goto exit;
57 }
58
59 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
60 urb->actual_length, data);
61
62 tty = port->tty;
63 if (tty && urb->actual_length) {
64 tty_buffer_request_room(tty, urb->actual_length);
65 tty_insert_flip_string(tty, data, urb->actual_length);
66 tty_flip_buffer_push(tty);
67 }
68
69exit:
70 result = usb_submit_urb(urb, GFP_ATOMIC);
71 if (result)
72 dev_err(&urb->dev->dev,
73 "%s - Error %d submitting interrupt urb\n",
74 __FUNCTION__, result);
75}
76
77static int navman_open(struct usb_serial_port *port, struct file *filp)
78{
79 int result = 0;
80
81 dbg("%s - port %d", __FUNCTION__, port->number);
82
83 if (port->interrupt_in_urb) {
84 dbg("%s - adding interrupt input for treo", __FUNCTION__);
85 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
86 if (result)
87 dev_err(&port->dev,
88 "%s - failed submitting interrupt urb, error %d\n",
89 __FUNCTION__, result);
90 }
91 return result;
92}
93
94static void navman_close(struct usb_serial_port *port, struct file *filp)
95{
96 dbg("%s - port %d", __FUNCTION__, port->number);
97
98 if (port->interrupt_in_urb)
99 usb_kill_urb(port->interrupt_in_urb);
100}
101
102static int navman_write(struct usb_serial_port *port,
103 const unsigned char *buf, int count)
104{
105 dbg("%s - port %d", __FUNCTION__, port->number);
106
107 /*
108 * This device can't write any data, only read from the device
109 * so we just silently eat all data sent to us and say it was
110 * successfully sent.
111 * Evil, I know, but do you have a better idea?
112 */
113
114 return count;
115}
116
117static struct usb_serial_driver navman_device = {
118 .driver = {
119 .owner = THIS_MODULE,
120 .name = "navman",
121 },
122 .id_table = id_table,
123 .num_interrupt_in = NUM_DONT_CARE,
124 .num_bulk_in = NUM_DONT_CARE,
125 .num_bulk_out = NUM_DONT_CARE,
126 .num_ports = 1,
127 .open = navman_open,
128 .close = navman_close,
129 .write = navman_write,
130 .read_int_callback = navman_read_int_callback,
131};
132
133static int __init navman_init(void)
134{
135 int retval;
136
137 retval = usb_serial_register(&navman_device);
138 if (retval)
139 return retval;
140 retval = usb_register(&navman_driver);
141 if (retval)
142 usb_serial_deregister(&navman_device);
143 return retval;
144}
145
146static void __exit navman_exit(void)
147{
148 usb_deregister(&navman_driver);
149 usb_serial_deregister(&navman_device);
150}
151
152module_init(navman_init);
153module_exit(navman_exit);
154MODULE_LICENSE("GPL");
155
156module_param(debug, bool, S_IRUGO | S_IWUSR);
157MODULE_PARM_DESC(debug, "Debug enabled or not");
diff --git a/drivers/usb/serial/omninet.c b/drivers/usb/serial/omninet.c
index 762d8ff9a1e4..4d40704dea2c 100644
--- a/drivers/usb/serial/omninet.c
+++ b/drivers/usb/serial/omninet.c
@@ -204,7 +204,7 @@ static void omninet_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
204 int i; 204 int i;
205 int result; 205 int result;
206 206
207// dbg("omninet_read_bulk_callback"); 207 dbg("%s - port %d", __FUNCTION__, port->number);
208 208
209 if (urb->status) { 209 if (urb->status) {
210 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status); 210 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
@@ -250,7 +250,7 @@ static int omninet_write (struct usb_serial_port *port, const unsigned char *buf
250 250
251 int result; 251 int result;
252 252
253// dbg("omninet_write port %d", port->number); 253 dbg("%s - port %d", __FUNCTION__, port->number);
254 254
255 if (count == 0) { 255 if (count == 0) {
256 dbg("%s - write request of 0 bytes", __FUNCTION__); 256 dbg("%s - write request of 0 bytes", __FUNCTION__);
@@ -302,7 +302,7 @@ static int omninet_write_room (struct usb_serial_port *port)
302 if (wport->write_urb_busy) 302 if (wport->write_urb_busy)
303 room = wport->bulk_out_size - OMNINET_HEADERLEN; 303 room = wport->bulk_out_size - OMNINET_HEADERLEN;
304 304
305// dbg("omninet_write_room returns %d", room); 305 dbg("%s - returns %d", __FUNCTION__, room);
306 306
307 return (room); 307 return (room);
308} 308}
@@ -312,7 +312,7 @@ static void omninet_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
312/* struct omninet_header *header = (struct omninet_header *) urb->transfer_buffer; */ 312/* struct omninet_header *header = (struct omninet_header *) urb->transfer_buffer; */
313 struct usb_serial_port *port = (struct usb_serial_port *) urb->context; 313 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
314 314
315// dbg("omninet_write_bulk_callback, port %0x\n", port); 315 dbg("%s - port %0x\n", __FUNCTION__, port->number);
316 316
317 port->write_urb_busy = 0; 317 port->write_urb_busy = 0;
318 if (urb->status) { 318 if (urb->status) {
@@ -321,8 +321,6 @@ static void omninet_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
321 } 321 }
322 322
323 schedule_work(&port->work); 323 schedule_work(&port->work);
324
325// dbg("omninet_write_bulk_callback, tty %0x\n", tty);
326} 324}
327 325
328 326
diff --git a/drivers/usb/serial/option.c b/drivers/usb/serial/option.c
index 52bdf6fe46f2..495db5755df9 100644
--- a/drivers/usb/serial/option.c
+++ b/drivers/usb/serial/option.c
@@ -102,7 +102,7 @@ static struct usb_driver option_driver = {
102 .no_dynamic_id = 1, 102 .no_dynamic_id = 1,
103}; 103};
104 104
105/* The card has three separate interfaces, wich the serial driver 105/* The card has three separate interfaces, which the serial driver
106 * recognizes separately, thus num_port=1. 106 * recognizes separately, thus num_port=1.
107 */ 107 */
108static struct usb_serial_driver option_3port_device = { 108static struct usb_serial_driver option_3port_device = {
@@ -631,13 +631,12 @@ static int option_startup(struct usb_serial *serial)
631 /* Now setup per port private data */ 631 /* Now setup per port private data */
632 for (i = 0; i < serial->num_ports; i++) { 632 for (i = 0; i < serial->num_ports; i++) {
633 port = serial->port[i]; 633 port = serial->port[i];
634 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL); 634 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
635 if (!portdata) { 635 if (!portdata) {
636 dbg("%s: kmalloc for option_port_private (%d) failed!.", 636 dbg("%s: kmalloc for option_port_private (%d) failed!.",
637 __FUNCTION__, i); 637 __FUNCTION__, i);
638 return (1); 638 return (1);
639 } 639 }
640 memset(portdata, 0, sizeof(struct option_port_private));
641 640
642 usb_set_serial_port_data(port, portdata); 641 usb_set_serial_port_data(port, portdata);
643 642
diff --git a/drivers/usb/serial/pl2303.c b/drivers/usb/serial/pl2303.c
index 37c81c08faad..b3014fda645c 100644
--- a/drivers/usb/serial/pl2303.c
+++ b/drivers/usb/serial/pl2303.c
@@ -77,6 +77,7 @@ static struct usb_device_id id_table [] = {
77 { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) }, 77 { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
78 { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) }, 78 { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
79 { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) }, 79 { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
80 { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
80 { } /* Terminating entry */ 81 { } /* Terminating entry */
81}; 82};
82 83
@@ -218,10 +219,9 @@ static int pl2303_startup (struct usb_serial *serial)
218 dbg("device type: %d", type); 219 dbg("device type: %d", type);
219 220
220 for (i = 0; i < serial->num_ports; ++i) { 221 for (i = 0; i < serial->num_ports; ++i) {
221 priv = kmalloc (sizeof (struct pl2303_private), GFP_KERNEL); 222 priv = kzalloc(sizeof(struct pl2303_private), GFP_KERNEL);
222 if (!priv) 223 if (!priv)
223 goto cleanup; 224 goto cleanup;
224 memset (priv, 0x00, sizeof (struct pl2303_private));
225 spin_lock_init(&priv->lock); 225 spin_lock_init(&priv->lock);
226 priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE); 226 priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE);
227 if (priv->buf == NULL) { 227 if (priv->buf == NULL) {
@@ -383,12 +383,11 @@ static void pl2303_set_termios (struct usb_serial_port *port, struct termios *ol
383 } 383 }
384 } 384 }
385 385
386 buf = kmalloc (7, GFP_KERNEL); 386 buf = kzalloc (7, GFP_KERNEL);
387 if (!buf) { 387 if (!buf) {
388 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__); 388 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
389 return; 389 return;
390 } 390 }
391 memset (buf, 0x00, 0x07);
392 391
393 i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0), 392 i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
394 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE, 393 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
@@ -828,6 +827,7 @@ static void pl2303_update_line_status(struct usb_serial_port *port,
828 spin_lock_irqsave(&priv->lock, flags); 827 spin_lock_irqsave(&priv->lock, flags);
829 priv->line_status = data[status_idx]; 828 priv->line_status = data[status_idx];
830 spin_unlock_irqrestore(&priv->lock, flags); 829 spin_unlock_irqrestore(&priv->lock, flags);
830 wake_up_interruptible (&priv->delta_msr_wait);
831 831
832exit: 832exit:
833 return; 833 return;
diff --git a/drivers/usb/serial/pl2303.h b/drivers/usb/serial/pl2303.h
index 9bc4755162ad..77901d143979 100644
--- a/drivers/usb/serial/pl2303.h
+++ b/drivers/usb/serial/pl2303.h
@@ -75,3 +75,7 @@
75/* Leadtek GPS 9531 (ID 0413:2101) */ 75/* Leadtek GPS 9531 (ID 0413:2101) */
76#define LEADTEK_VENDOR_ID 0x0413 76#define LEADTEK_VENDOR_ID 0x0413
77#define LEADTEK_9531_PRODUCT_ID 0x2101 77#define LEADTEK_9531_PRODUCT_ID 0x2101
78
79/* USB GSM cable from Speed Dragon Multimedia, Ltd */
80#define SPEEDDRAGON_VENDOR_ID 0x0e55
81#define SPEEDDRAGON_PRODUCT_ID 0x110b
diff --git a/drivers/usb/serial/ti_usb_3410_5052.c b/drivers/usb/serial/ti_usb_3410_5052.c
index c18db3257073..c3a2071b802d 100644
--- a/drivers/usb/serial/ti_usb_3410_5052.c
+++ b/drivers/usb/serial/ti_usb_3410_5052.c
@@ -416,12 +416,11 @@ static int ti_startup(struct usb_serial *serial)
416 dev->actconfig->desc.bConfigurationValue); 416 dev->actconfig->desc.bConfigurationValue);
417 417
418 /* create device structure */ 418 /* create device structure */
419 tdev = kmalloc(sizeof(struct ti_device), GFP_KERNEL); 419 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
420 if (tdev == NULL) { 420 if (tdev == NULL) {
421 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__); 421 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
422 return -ENOMEM; 422 return -ENOMEM;
423 } 423 }
424 memset(tdev, 0, sizeof(struct ti_device));
425 sema_init(&tdev->td_open_close_sem, 1); 424 sema_init(&tdev->td_open_close_sem, 1);
426 tdev->td_serial = serial; 425 tdev->td_serial = serial;
427 usb_set_serial_data(serial, tdev); 426 usb_set_serial_data(serial, tdev);
diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index b5c96e74a903..097f4e8488fe 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -564,12 +564,11 @@ static struct usb_serial * create_serial (struct usb_device *dev,
564{ 564{
565 struct usb_serial *serial; 565 struct usb_serial *serial;
566 566
567 serial = kmalloc (sizeof (*serial), GFP_KERNEL); 567 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
568 if (!serial) { 568 if (!serial) {
569 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__); 569 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
570 return NULL; 570 return NULL;
571 } 571 }
572 memset (serial, 0, sizeof(*serial));
573 serial->dev = usb_get_dev(dev); 572 serial->dev = usb_get_dev(dev);
574 serial->type = driver; 573 serial->type = driver;
575 serial->interface = interface; 574 serial->interface = interface;
@@ -778,10 +777,9 @@ int usb_serial_probe(struct usb_interface *interface,
778 serial->num_port_pointers = max_endpoints; 777 serial->num_port_pointers = max_endpoints;
779 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints); 778 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
780 for (i = 0; i < max_endpoints; ++i) { 779 for (i = 0; i < max_endpoints; ++i) {
781 port = kmalloc(sizeof(struct usb_serial_port), GFP_KERNEL); 780 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
782 if (!port) 781 if (!port)
783 goto probe_error; 782 goto probe_error;
784 memset(port, 0x00, sizeof(struct usb_serial_port));
785 port->number = i + serial->minor; 783 port->number = i + serial->minor;
786 port->serial = serial; 784 port->serial = serial;
787 spin_lock_init(&port->lock); 785 spin_lock_init(&port->lock);
diff --git a/drivers/usb/serial/visor.c b/drivers/usb/serial/visor.c
index 11a48d874752..f5c3841d4843 100644
--- a/drivers/usb/serial/visor.c
+++ b/drivers/usb/serial/visor.c
@@ -763,10 +763,9 @@ static int generic_startup(struct usb_serial *serial)
763 int i; 763 int i;
764 764
765 for (i = 0; i < serial->num_ports; ++i) { 765 for (i = 0; i < serial->num_ports; ++i) {
766 priv = kmalloc (sizeof(*priv), GFP_KERNEL); 766 priv = kzalloc (sizeof(*priv), GFP_KERNEL);
767 if (!priv) 767 if (!priv)
768 return -ENOMEM; 768 return -ENOMEM;
769 memset (priv, 0x00, sizeof(*priv));
770 spin_lock_init(&priv->lock); 769 spin_lock_init(&priv->lock);
771 usb_set_serial_port_data(serial->port[i], priv); 770 usb_set_serial_port_data(serial->port[i], priv);
772 } 771 }
diff --git a/drivers/usb/storage/datafab.c b/drivers/usb/storage/datafab.c
index 54e3e6c7ecd8..01d8971ad7db 100644
--- a/drivers/usb/storage/datafab.c
+++ b/drivers/usb/storage/datafab.c
@@ -512,13 +512,12 @@ int datafab_transport(struct scsi_cmnd * srb, struct us_data *us)
512 }; 512 };
513 513
514 if (!us->extra) { 514 if (!us->extra) {
515 us->extra = kmalloc(sizeof(struct datafab_info), GFP_NOIO); 515 us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
516 if (!us->extra) { 516 if (!us->extra) {
517 US_DEBUGP("datafab_transport: Gah! " 517 US_DEBUGP("datafab_transport: Gah! "
518 "Can't allocate storage for Datafab info struct!\n"); 518 "Can't allocate storage for Datafab info struct!\n");
519 return USB_STOR_TRANSPORT_ERROR; 519 return USB_STOR_TRANSPORT_ERROR;
520 } 520 }
521 memset(us->extra, 0, sizeof(struct datafab_info));
522 us->extra_destructor = datafab_info_destructor; 521 us->extra_destructor = datafab_info_destructor;
523 ((struct datafab_info *)us->extra)->lun = -1; 522 ((struct datafab_info *)us->extra)->lun = -1;
524 } 523 }
diff --git a/drivers/usb/storage/isd200.c b/drivers/usb/storage/isd200.c
index ecb328aa9ea1..6831dca93c1b 100644
--- a/drivers/usb/storage/isd200.c
+++ b/drivers/usb/storage/isd200.c
@@ -1361,21 +1361,19 @@ static int isd200_init_info(struct us_data *us)
1361 struct isd200_info *info; 1361 struct isd200_info *info;
1362 1362
1363 info = (struct isd200_info *) 1363 info = (struct isd200_info *)
1364 kmalloc(sizeof(struct isd200_info), GFP_KERNEL); 1364 kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1365 if (!info) 1365 if (!info)
1366 retStatus = ISD200_ERROR; 1366 retStatus = ISD200_ERROR;
1367 else { 1367 else {
1368 memset(info, 0, sizeof(struct isd200_info));
1369 info->id = (struct hd_driveid *) 1368 info->id = (struct hd_driveid *)
1370 kmalloc(sizeof(struct hd_driveid), GFP_KERNEL); 1369 kzalloc(sizeof(struct hd_driveid), GFP_KERNEL);
1371 info->RegsBuf = (unsigned char *) 1370 info->RegsBuf = (unsigned char *)
1372 kmalloc(sizeof(info->ATARegs), GFP_KERNEL); 1371 kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
1373 if (!info->id || !info->RegsBuf) { 1372 if (!info->id || !info->RegsBuf) {
1374 isd200_free_info_ptrs(info); 1373 isd200_free_info_ptrs(info);
1375 kfree(info); 1374 kfree(info);
1376 retStatus = ISD200_ERROR; 1375 retStatus = ISD200_ERROR;
1377 } else 1376 }
1378 memset(info->id, 0, sizeof(struct hd_driveid));
1379 } 1377 }
1380 1378
1381 if (retStatus == ISD200_GOOD) { 1379 if (retStatus == ISD200_GOOD) {
@@ -1384,7 +1382,7 @@ static int isd200_init_info(struct us_data *us)
1384 } else 1382 } else
1385 US_DEBUGP("ERROR - kmalloc failure\n"); 1383 US_DEBUGP("ERROR - kmalloc failure\n");
1386 1384
1387 return(retStatus); 1385 return retStatus;
1388} 1386}
1389 1387
1390/************************************************************************** 1388/**************************************************************************
diff --git a/drivers/usb/storage/jumpshot.c b/drivers/usb/storage/jumpshot.c
index aff9d51c327c..5031aa98f6a9 100644
--- a/drivers/usb/storage/jumpshot.c
+++ b/drivers/usb/storage/jumpshot.c
@@ -441,12 +441,11 @@ int jumpshot_transport(struct scsi_cmnd * srb, struct us_data *us)
441 }; 441 };
442 442
443 if (!us->extra) { 443 if (!us->extra) {
444 us->extra = kmalloc(sizeof(struct jumpshot_info), GFP_NOIO); 444 us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO);
445 if (!us->extra) { 445 if (!us->extra) {
446 US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n"); 446 US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n");
447 return USB_STOR_TRANSPORT_ERROR; 447 return USB_STOR_TRANSPORT_ERROR;
448 } 448 }
449 memset(us->extra, 0, sizeof(struct jumpshot_info));
450 us->extra_destructor = jumpshot_info_destructor; 449 us->extra_destructor = jumpshot_info_destructor;
451 } 450 }
452 451
diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
index 4ef5527028c5..5f11e19eaae3 100644
--- a/drivers/usb/storage/scsiglue.c
+++ b/drivers/usb/storage/scsiglue.c
@@ -47,6 +47,7 @@
47 47
48#include <linux/slab.h> 48#include <linux/slab.h>
49#include <linux/module.h> 49#include <linux/module.h>
50#include <linux/mutex.h>
50 51
51#include <scsi/scsi.h> 52#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h> 53#include <scsi/scsi_cmnd.h>
@@ -271,9 +272,9 @@ static int device_reset(struct scsi_cmnd *srb)
271 US_DEBUGP("%s called\n", __FUNCTION__); 272 US_DEBUGP("%s called\n", __FUNCTION__);
272 273
273 /* lock the device pointers and do the reset */ 274 /* lock the device pointers and do the reset */
274 down(&(us->dev_semaphore)); 275 mutex_lock(&(us->dev_mutex));
275 result = us->transport_reset(us); 276 result = us->transport_reset(us);
276 up(&(us->dev_semaphore)); 277 mutex_unlock(&us->dev_mutex);
277 278
278 return result < 0 ? FAILED : SUCCESS; 279 return result < 0 ? FAILED : SUCCESS;
279} 280}
@@ -286,9 +287,9 @@ static int bus_reset(struct scsi_cmnd *srb)
286 287
287 US_DEBUGP("%s called\n", __FUNCTION__); 288 US_DEBUGP("%s called\n", __FUNCTION__);
288 289
289 down(&(us->dev_semaphore)); 290 mutex_lock(&(us->dev_mutex));
290 result = usb_stor_port_reset(us); 291 result = usb_stor_port_reset(us);
291 up(&(us->dev_semaphore)); 292 mutex_unlock(&us->dev_mutex);
292 293
293 return result < 0 ? FAILED : SUCCESS; 294 return result < 0 ? FAILED : SUCCESS;
294} 295}
diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
index 8451779f4269..0b1b5b59ca7b 100644
--- a/drivers/usb/storage/sddr55.c
+++ b/drivers/usb/storage/sddr55.c
@@ -751,11 +751,10 @@ int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
751 struct sddr55_card_info *info; 751 struct sddr55_card_info *info;
752 752
753 if (!us->extra) { 753 if (!us->extra) {
754 us->extra = kmalloc( 754 us->extra = kzalloc(
755 sizeof(struct sddr55_card_info), GFP_NOIO); 755 sizeof(struct sddr55_card_info), GFP_NOIO);
756 if (!us->extra) 756 if (!us->extra)
757 return USB_STOR_TRANSPORT_ERROR; 757 return USB_STOR_TRANSPORT_ERROR;
758 memset(us->extra, 0, sizeof(struct sddr55_card_info));
759 us->extra_destructor = sddr55_card_info_destructor; 758 us->extra_destructor = sddr55_card_info_destructor;
760 } 759 }
761 760
diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index fea176d7e79a..f2bc5c9e23d5 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -1318,12 +1318,11 @@ int init_usbat(struct us_data *us)
1318 unsigned char subcountL = USBAT_ATA_LBA_ME; 1318 unsigned char subcountL = USBAT_ATA_LBA_ME;
1319 unsigned char *status = us->iobuf; 1319 unsigned char *status = us->iobuf;
1320 1320
1321 us->extra = kmalloc(sizeof(struct usbat_info), GFP_NOIO); 1321 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
1322 if (!us->extra) { 1322 if (!us->extra) {
1323 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n"); 1323 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1324 return 1; 1324 return 1;
1325 } 1325 }
1326 memset(us->extra, 0, sizeof(struct usbat_info));
1327 info = (struct usbat_info *) (us->extra); 1326 info = (struct usbat_info *) (us->extra);
1328 1327
1329 /* Enable peripheral control signals */ 1328 /* Enable peripheral control signals */
diff --git a/drivers/usb/storage/unusual_devs.h b/drivers/usb/storage/unusual_devs.h
index 31ca92056c27..c4a9dcff5f2b 100644
--- a/drivers/usb/storage/unusual_devs.h
+++ b/drivers/usb/storage/unusual_devs.h
@@ -62,6 +62,13 @@ UNUSUAL_DEV( 0x03ee, 0x6901, 0x0000, 0x0100,
62 US_SC_DEVICE, US_PR_DEVICE, NULL, 62 US_SC_DEVICE, US_PR_DEVICE, NULL,
63 US_FL_SINGLE_LUN ), 63 US_FL_SINGLE_LUN ),
64 64
65/* Reported by Rodolfo Quesada <rquesada@roqz.net> */
66UNUSUAL_DEV( 0x03ee, 0x6906, 0x0003, 0x0003,
67 "VIA Technologies Inc.",
68 "Mitsumi multi cardreader",
69 US_SC_DEVICE, US_PR_DEVICE, NULL,
70 US_FL_IGNORE_RESIDUE ),
71
65UNUSUAL_DEV( 0x03f0, 0x0107, 0x0200, 0x0200, 72UNUSUAL_DEV( 0x03f0, 0x0107, 0x0200, 0x0200,
66 "HP", 73 "HP",
67 "CD-Writer+", 74 "CD-Writer+",
@@ -120,6 +127,12 @@ UNUSUAL_DEV( 0x0419, 0xaaf6, 0x0100, 0x0100,
120 US_SC_DEVICE, US_PR_DEVICE, NULL, 127 US_SC_DEVICE, US_PR_DEVICE, NULL,
121 US_FL_IGNORE_RESIDUE ), 128 US_FL_IGNORE_RESIDUE ),
122 129
130/* Reported by Pete Zaitcev <zaitcev@redhat.com>, bz#176584 */
131UNUSUAL_DEV( 0x0420, 0x0001, 0x0100, 0x0100,
132 "GENERIC", "MP3 PLAYER", /* MyMusix PD-205 on the outside. */
133 US_SC_DEVICE, US_PR_DEVICE, NULL,
134 US_FL_IGNORE_RESIDUE ),
135
123/* Reported by Olaf Hering <olh@suse.de> from novell bug #105878 */ 136/* Reported by Olaf Hering <olh@suse.de> from novell bug #105878 */
124UNUSUAL_DEV( 0x0424, 0x0fdc, 0x0210, 0x0210, 137UNUSUAL_DEV( 0x0424, 0x0fdc, 0x0210, 0x0210,
125 "SMSC", 138 "SMSC",
@@ -760,12 +773,19 @@ UNUSUAL_DEV( 0x069b, 0x3004, 0x0001, 0x0001,
760 US_SC_DEVICE, US_PR_DEVICE, NULL, 773 US_SC_DEVICE, US_PR_DEVICE, NULL,
761 US_FL_FIX_CAPACITY ), 774 US_FL_FIX_CAPACITY ),
762 775
763UNUSUAL_DEV( 0x0781, 0x0001, 0x0200, 0x0200, 776/* Submitted by Roman Hodek <roman@hodek.net> */
777UNUSUAL_DEV( 0x0781, 0x0001, 0x0200, 0x0200,
764 "Sandisk", 778 "Sandisk",
765 "ImageMate SDDR-05a", 779 "ImageMate SDDR-05a",
766 US_SC_SCSI, US_PR_CB, NULL, 780 US_SC_SCSI, US_PR_CB, NULL,
767 US_FL_SINGLE_LUN ), 781 US_FL_SINGLE_LUN ),
768 782
783UNUSUAL_DEV( 0x0781, 0x0002, 0x0009, 0x0009,
784 "SanDisk Corporation",
785 "ImageMate CompactFlash USB",
786 US_SC_DEVICE, US_PR_DEVICE, NULL,
787 US_FL_FIX_CAPACITY ),
788
769#ifdef CONFIG_USB_STORAGE_USBAT 789#ifdef CONFIG_USB_STORAGE_USBAT
770UNUSUAL_DEV( 0x0781, 0x0005, 0x0005, 0x0005, 790UNUSUAL_DEV( 0x0781, 0x0005, 0x0005, 0x0005,
771 "Sandisk", 791 "Sandisk",
@@ -1073,6 +1093,16 @@ UNUSUAL_DEV( 0x0c0b, 0xa109, 0x0000, 0xffff,
1073 0), 1093 0),
1074#endif 1094#endif
1075 1095
1096/*
1097 * Pete Zaitcev <zaitcev@yahoo.com>, bz#164688.
1098 * The device blatantly ignores LUN and returns 1 in GetMaxLUN.
1099 */
1100UNUSUAL_DEV( 0x0c45, 0x1060, 0x0100, 0x0100,
1101 "Unknown",
1102 "Unknown",
1103 US_SC_DEVICE, US_PR_DEVICE, NULL,
1104 US_FL_SINGLE_LUN ),
1105
1076/* Submitted by Joris Struyve <joris@struyve.be> */ 1106/* Submitted by Joris Struyve <joris@struyve.be> */
1077UNUSUAL_DEV( 0x0d96, 0x410a, 0x0001, 0xffff, 1107UNUSUAL_DEV( 0x0d96, 0x410a, 0x0001, 0xffff,
1078 "Medion", 1108 "Medion",
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index dbcf23980ff1..dd108634348e 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -55,6 +55,7 @@
55#include <linux/init.h> 55#include <linux/init.h>
56#include <linux/slab.h> 56#include <linux/slab.h>
57#include <linux/kthread.h> 57#include <linux/kthread.h>
58#include <linux/mutex.h>
58 59
59#include <scsi/scsi.h> 60#include <scsi/scsi.h>
60#include <scsi/scsi_cmnd.h> 61#include <scsi/scsi_cmnd.h>
@@ -188,7 +189,7 @@ static int storage_suspend(struct usb_interface *iface, pm_message_t message)
188 struct us_data *us = usb_get_intfdata(iface); 189 struct us_data *us = usb_get_intfdata(iface);
189 190
190 /* Wait until no command is running */ 191 /* Wait until no command is running */
191 down(&us->dev_semaphore); 192 mutex_lock(&us->dev_mutex);
192 193
193 US_DEBUGP("%s\n", __FUNCTION__); 194 US_DEBUGP("%s\n", __FUNCTION__);
194 if (us->suspend_resume_hook) 195 if (us->suspend_resume_hook)
@@ -198,7 +199,7 @@ static int storage_suspend(struct usb_interface *iface, pm_message_t message)
198 /* When runtime PM is working, we'll set a flag to indicate 199 /* When runtime PM is working, we'll set a flag to indicate
199 * whether we should autoresume when a SCSI request arrives. */ 200 * whether we should autoresume when a SCSI request arrives. */
200 201
201 up(&us->dev_semaphore); 202 mutex_unlock(&us->dev_mutex);
202 return 0; 203 return 0;
203} 204}
204 205
@@ -206,14 +207,14 @@ static int storage_resume(struct usb_interface *iface)
206{ 207{
207 struct us_data *us = usb_get_intfdata(iface); 208 struct us_data *us = usb_get_intfdata(iface);
208 209
209 down(&us->dev_semaphore); 210 mutex_lock(&us->dev_mutex);
210 211
211 US_DEBUGP("%s\n", __FUNCTION__); 212 US_DEBUGP("%s\n", __FUNCTION__);
212 if (us->suspend_resume_hook) 213 if (us->suspend_resume_hook)
213 (us->suspend_resume_hook)(us, US_RESUME); 214 (us->suspend_resume_hook)(us, US_RESUME);
214 iface->dev.power.power_state.event = PM_EVENT_ON; 215 iface->dev.power.power_state.event = PM_EVENT_ON;
215 216
216 up(&us->dev_semaphore); 217 mutex_unlock(&us->dev_mutex);
217 return 0; 218 return 0;
218} 219}
219 220
@@ -276,12 +277,12 @@ static int usb_stor_control_thread(void * __us)
276 US_DEBUGP("*** thread awakened.\n"); 277 US_DEBUGP("*** thread awakened.\n");
277 278
278 /* lock the device pointers */ 279 /* lock the device pointers */
279 down(&(us->dev_semaphore)); 280 mutex_lock(&(us->dev_mutex));
280 281
281 /* if the device has disconnected, we are free to exit */ 282 /* if the device has disconnected, we are free to exit */
282 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 283 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
283 US_DEBUGP("-- exiting\n"); 284 US_DEBUGP("-- exiting\n");
284 up(&(us->dev_semaphore)); 285 mutex_unlock(&us->dev_mutex);
285 break; 286 break;
286 } 287 }
287 288
@@ -370,7 +371,7 @@ SkipForAbort:
370 scsi_unlock(host); 371 scsi_unlock(host);
371 372
372 /* unlock the device pointers */ 373 /* unlock the device pointers */
373 up(&(us->dev_semaphore)); 374 mutex_unlock(&us->dev_mutex);
374 } /* for (;;) */ 375 } /* for (;;) */
375 376
376 scsi_host_put(host); 377 scsi_host_put(host);
@@ -815,8 +816,8 @@ static void quiesce_and_remove_host(struct us_data *us)
815 * The thread will exit when it sees the DISCONNECTING flag. */ 816 * The thread will exit when it sees the DISCONNECTING flag. */
816 817
817 /* Wait for the current command to finish, then remove the host */ 818 /* Wait for the current command to finish, then remove the host */
818 down(&us->dev_semaphore); 819 mutex_lock(&us->dev_mutex);
819 up(&us->dev_semaphore); 820 mutex_unlock(&us->dev_mutex);
820 821
821 /* queuecommand won't accept any new commands and the control 822 /* queuecommand won't accept any new commands and the control
822 * thread won't execute a previously-queued command. If there 823 * thread won't execute a previously-queued command. If there
@@ -870,9 +871,9 @@ retry:
870 /* For bulk-only devices, determine the max LUN value */ 871 /* For bulk-only devices, determine the max LUN value */
871 if (us->protocol == US_PR_BULK && 872 if (us->protocol == US_PR_BULK &&
872 !(us->flags & US_FL_SINGLE_LUN)) { 873 !(us->flags & US_FL_SINGLE_LUN)) {
873 down(&us->dev_semaphore); 874 mutex_lock(&us->dev_mutex);
874 us->max_lun = usb_stor_Bulk_max_lun(us); 875 us->max_lun = usb_stor_Bulk_max_lun(us);
875 up(&us->dev_semaphore); 876 mutex_unlock(&us->dev_mutex);
876 } 877 }
877 scsi_scan_host(us_to_host(us)); 878 scsi_scan_host(us_to_host(us));
878 printk(KERN_DEBUG "usb-storage: device scan complete\n"); 879 printk(KERN_DEBUG "usb-storage: device scan complete\n");
@@ -912,7 +913,7 @@ static int storage_probe(struct usb_interface *intf,
912 913
913 us = host_to_us(host); 914 us = host_to_us(host);
914 memset(us, 0, sizeof(struct us_data)); 915 memset(us, 0, sizeof(struct us_data));
915 init_MUTEX(&(us->dev_semaphore)); 916 mutex_init(&(us->dev_mutex));
916 init_MUTEX_LOCKED(&(us->sema)); 917 init_MUTEX_LOCKED(&(us->sema));
917 init_completion(&(us->notify)); 918 init_completion(&(us->notify));
918 init_waitqueue_head(&us->delay_wait); 919 init_waitqueue_head(&us->delay_wait);
diff --git a/drivers/usb/storage/usb.h b/drivers/usb/storage/usb.h
index 7259fd1f6b0d..009fb0953a56 100644
--- a/drivers/usb/storage/usb.h
+++ b/drivers/usb/storage/usb.h
@@ -49,6 +49,7 @@
49#include <linux/blkdev.h> 49#include <linux/blkdev.h>
50#include <linux/smp_lock.h> 50#include <linux/smp_lock.h>
51#include <linux/completion.h> 51#include <linux/completion.h>
52#include <linux/mutex.h>
52#include <scsi/scsi_host.h> 53#include <scsi/scsi_host.h>
53 54
54struct us_data; 55struct us_data;
@@ -103,9 +104,9 @@ typedef void (*pm_hook)(struct us_data *, int); /* power management hook */
103struct us_data { 104struct us_data {
104 /* The device we're working with 105 /* The device we're working with
105 * It's important to note: 106 * It's important to note:
106 * (o) you must hold dev_semaphore to change pusb_dev 107 * (o) you must hold dev_mutex to change pusb_dev
107 */ 108 */
108 struct semaphore dev_semaphore; /* protect pusb_dev */ 109 struct mutex dev_mutex; /* protect pusb_dev */
109 struct usb_device *pusb_dev; /* this usb_device */ 110 struct usb_device *pusb_dev; /* this usb_device */
110 struct usb_interface *pusb_intf; /* this interface */ 111 struct usb_interface *pusb_intf; /* this interface */
111 struct us_unusual_dev *unusual_dev; /* device-filter entry */ 112 struct us_unusual_dev *unusual_dev; /* device-filter entry */