aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'drivers')
-rw-r--r--drivers/Kconfig2
-rw-r--r--drivers/Makefile1
-rw-r--r--drivers/block/rbd.c816
-rw-r--r--drivers/block/rbd_types.h1
-rw-r--r--drivers/block/xen-blkfront.c5
-rw-r--r--drivers/char/random.c355
-rw-r--r--drivers/crypto/n2_core.c3
-rw-r--r--drivers/firmware/dmi_scan.c3
-rw-r--r--drivers/hid/hid-core.c1
-rw-r--r--drivers/hid/hid-ids.h1
-rw-r--r--drivers/hv/vmbus_drv.c3
-rw-r--r--drivers/i2c/busses/i2c-pmcmsp.c3
-rw-r--r--drivers/infiniband/core/cma.c5
-rw-r--r--drivers/infiniband/core/ucma.c19
-rw-r--r--drivers/infiniband/hw/ocrdma/ocrdma_verbs.c4
-rw-r--r--drivers/infiniband/hw/qib/qib.h10
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib.h56
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_cm.c16
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_main.c646
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_multicast.c57
-rw-r--r--drivers/input/serio/hp_sdc.c2
-rw-r--r--drivers/isdn/hardware/mISDN/avmfritz.c7
-rw-r--r--drivers/media/common/tuners/tuner-xc2028.c4
-rw-r--r--drivers/media/common/tuners/xc5000.c8
-rw-r--r--drivers/media/dvb/dvb-usb/az6007.c2
-rw-r--r--drivers/media/dvb/frontends/dib8000.c4
-rw-r--r--drivers/media/dvb/frontends/lgs8gxx.c5
-rw-r--r--drivers/media/dvb/frontends/rtl2832.c2
-rw-r--r--drivers/media/dvb/siano/smscoreapi.c39
-rw-r--r--drivers/media/radio/Kconfig33
-rw-r--r--drivers/media/radio/Makefile4
-rw-r--r--drivers/media/radio/radio-cadet.c388
-rw-r--r--drivers/media/radio/radio-shark.c376
-rw-r--r--drivers/media/radio/radio-shark2.c348
-rw-r--r--drivers/media/radio/radio-tea5777.c491
-rw-r--r--drivers/media/radio/radio-tea5777.h87
-rw-r--r--drivers/media/radio/si470x/radio-si470x-common.c283
-rw-r--r--drivers/media/radio/si470x/radio-si470x-i2c.c6
-rw-r--r--drivers/media/radio/si470x/radio-si470x-usb.c47
-rw-r--r--drivers/media/radio/si470x/radio-si470x.h7
-rw-r--r--drivers/media/rc/Kconfig11
-rw-r--r--drivers/media/rc/Makefile1
-rw-r--r--drivers/media/rc/ati_remote.c133
-rw-r--r--drivers/media/rc/iguanair.c639
-rw-r--r--drivers/media/rc/mceusb.c20
-rw-r--r--drivers/media/rc/rc-main.c5
-rw-r--r--drivers/media/video/adv7180.c235
-rw-r--r--drivers/media/video/bt8xx/bttv-cards.c10
-rw-r--r--drivers/media/video/bt8xx/bttv.h2
-rw-r--r--drivers/media/video/cx231xx/cx231xx-i2c.c8
-rw-r--r--drivers/media/video/cx231xx/cx231xx.h2
-rw-r--r--drivers/media/video/cx23885/cx23885-i2c.c10
-rw-r--r--drivers/media/video/cx23885/cx23885.h2
-rw-r--r--drivers/media/video/cx25821/cx25821-i2c.c10
-rw-r--r--drivers/media/video/cx25821/cx25821-medusa-video.c2
-rw-r--r--drivers/media/video/cx25821/cx25821.h2
-rw-r--r--drivers/media/video/davinci/Kconfig30
-rw-r--r--drivers/media/video/davinci/Makefile8
-rw-r--r--drivers/media/video/davinci/vpbe_display.c4
-rw-r--r--drivers/media/video/davinci/vpif.c45
-rw-r--r--drivers/media/video/davinci/vpif.h45
-rw-r--r--drivers/media/video/davinci/vpif_capture.c694
-rw-r--r--drivers/media/video/davinci/vpif_capture.h16
-rw-r--r--drivers/media/video/davinci/vpif_display.c684
-rw-r--r--drivers/media/video/davinci/vpif_display.h23
-rw-r--r--drivers/media/video/gspca/benq.c7
-rw-r--r--drivers/media/video/gspca/conex.c208
-rw-r--r--drivers/media/video/gspca/cpia1.c486
-rw-r--r--drivers/media/video/gspca/etoms.c221
-rw-r--r--drivers/media/video/gspca/finepix.c1
-rw-r--r--drivers/media/video/gspca/gl860/gl860.c1
-rw-r--r--drivers/media/video/gspca/gspca.c50
-rw-r--r--drivers/media/video/gspca/jeilinj.c219
-rw-r--r--drivers/media/video/gspca/jl2005bcd.c3
-rw-r--r--drivers/media/video/gspca/kinect.c10
-rw-r--r--drivers/media/video/gspca/konica.c289
-rw-r--r--drivers/media/video/gspca/m5602/m5602_core.c1
-rw-r--r--drivers/media/video/gspca/mars.c48
-rw-r--r--drivers/media/video/gspca/mr97310a.c439
-rw-r--r--drivers/media/video/gspca/nw80x.c203
-rw-r--r--drivers/media/video/gspca/ov519.c600
-rw-r--r--drivers/media/video/gspca/ov534.c570
-rw-r--r--drivers/media/video/gspca/ov534_9.c294
-rw-r--r--drivers/media/video/gspca/pac207.c1
-rw-r--r--drivers/media/video/gspca/pac7302.c372
-rw-r--r--drivers/media/video/gspca/pac7311.c1
-rw-r--r--drivers/media/video/gspca/se401.c184
-rw-r--r--drivers/media/video/gspca/sn9c2028.c7
-rw-r--r--drivers/media/video/gspca/sonixb.c622
-rw-r--r--drivers/media/video/gspca/sonixj.c1
-rw-r--r--drivers/media/video/gspca/spca1528.c271
-rw-r--r--drivers/media/video/gspca/spca500.c201
-rw-r--r--drivers/media/video/gspca/spca501.c257
-rw-r--r--drivers/media/video/gspca/spca505.c77
-rw-r--r--drivers/media/video/gspca/spca506.c209
-rw-r--r--drivers/media/video/gspca/spca508.c71
-rw-r--r--drivers/media/video/gspca/spca561.c393
-rw-r--r--drivers/media/video/gspca/sq905.c1
-rw-r--r--drivers/media/video/gspca/sq905c.c1
-rw-r--r--drivers/media/video/gspca/sq930x.c110
-rw-r--r--drivers/media/video/gspca/stk014.c188
-rw-r--r--drivers/media/video/gspca/stv0680.c7
-rw-r--r--drivers/media/video/gspca/sunplus.c237
-rw-r--r--drivers/media/video/gspca/t613.c824
-rw-r--r--drivers/media/video/gspca/topro.c459
-rw-r--r--drivers/media/video/gspca/tv8532.c125
-rw-r--r--drivers/media/video/gspca/vc032x.c694
-rw-r--r--drivers/media/video/gspca/vicam.c68
-rw-r--r--drivers/media/video/gspca/w996Xcf.c15
-rw-r--r--drivers/media/video/gspca/xirlink_cit.c473
-rw-r--r--drivers/media/video/m5mols/m5mols_controls.c4
-rw-r--r--drivers/media/video/mem2mem_testdev.c263
-rw-r--r--drivers/media/video/mx2_emmaprp.c10
-rw-r--r--drivers/media/video/ov2640.c6
-rw-r--r--drivers/media/video/ov772x.c8
-rw-r--r--drivers/media/video/ov9640.c1
-rw-r--r--drivers/media/video/pms.c1
-rw-r--r--drivers/media/video/s2255drv.c1
-rw-r--r--drivers/media/video/s5p-fimc/fimc-capture.c78
-rw-r--r--drivers/media/video/s5p-fimc/fimc-core.h3
-rw-r--r--drivers/media/video/s5p-fimc/fimc-m2m.c52
-rw-r--r--drivers/media/video/s5p-fimc/fimc-reg.c20
-rw-r--r--drivers/media/video/s5p-g2d/g2d.c9
-rw-r--r--drivers/media/video/s5p-jpeg/jpeg-core.c10
-rw-r--r--drivers/media/video/s5p-mfc/s5p_mfc_dec.c10
-rw-r--r--drivers/media/video/s5p-mfc/s5p_mfc_enc.c11
-rw-r--r--drivers/media/video/saa7164/saa7164-i2c.c20
-rw-r--r--drivers/media/video/saa7164/saa7164.h2
-rw-r--r--drivers/media/video/smiapp/smiapp-core.c1
-rw-r--r--drivers/media/video/soc_camera.c9
-rw-r--r--drivers/media/video/tlg2300/pd-main.c4
-rw-r--r--drivers/media/video/tvp5150.c2
-rw-r--r--drivers/media/video/tw9910.c8
-rw-r--r--drivers/media/video/v4l2-compat-ioctl32.c1
-rw-r--r--drivers/media/video/v4l2-ctrls.c3
-rw-r--r--drivers/media/video/v4l2-dev.c17
-rw-r--r--drivers/media/video/v4l2-ioctl.c122
-rw-r--r--drivers/media/video/v4l2-mem2mem.c18
-rw-r--r--drivers/media/video/via-camera.c2
-rw-r--r--drivers/media/video/videobuf-dma-contig.c55
-rw-r--r--drivers/media/video/videobuf2-core.c4
-rw-r--r--drivers/media/video/vivi.c26
-rw-r--r--drivers/mfd/ab3100-core.c4
-rw-r--r--drivers/mfd/tps65010.c3
-rw-r--r--drivers/mfd/twl-core.c9
-rw-r--r--drivers/mfd/wm831x-otp.c8
-rw-r--r--drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c3
-rw-r--r--drivers/net/ethernet/broadcom/tg3.c71
-rw-r--r--drivers/net/ethernet/broadcom/tg3.h8
-rw-r--r--drivers/net/ethernet/qlogic/qlge/qlge_main.c1
-rw-r--r--drivers/net/ethernet/seeq/seeq8005.c4
-rw-r--r--drivers/net/tun.c6
-rw-r--r--drivers/power/Kconfig1
-rw-r--r--drivers/power/bq27x00_battery.c155
-rw-r--r--drivers/power/charger-manager.c152
-rw-r--r--drivers/power/ds2781_battery.c2
-rw-r--r--drivers/power/gpio-charger.c2
-rw-r--r--drivers/power/lp8727_charger.c2
-rw-r--r--drivers/power/max17042_battery.c8
-rw-r--r--drivers/power/olpc_battery.c62
-rw-r--r--drivers/power/pda_power.c10
-rw-r--r--drivers/power/power_supply_core.c65
-rw-r--r--drivers/power/power_supply_sysfs.c8
-rw-r--r--drivers/power/sbs-battery.c2
-rw-r--r--drivers/power/smb347-charger.c123
-rw-r--r--drivers/power/test_power.c75
-rw-r--r--drivers/power/twl4030_charger.c80
-rw-r--r--drivers/rtc/rtc-wm831x.c24
-rw-r--r--drivers/staging/media/dt3155v4l/dt3155v4l.c15
-rw-r--r--drivers/staging/media/easycap/easycap_main.c1
-rw-r--r--drivers/staging/media/lirc/lirc_bt829.c2
-rw-r--r--drivers/staging/media/solo6x10/core.c13
-rw-r--r--drivers/thermal/thermal_sys.c2
-rw-r--r--drivers/tty/serial/uartlite.c3
-rw-r--r--drivers/usb/core/hub.c9
-rw-r--r--drivers/usb/gadget/goku_udc.c2
-rw-r--r--drivers/usb/gadget/pxa25x_udc.c12
-rw-r--r--drivers/usb/otg/isp1301_omap.c1
-rw-r--r--drivers/vfio/Kconfig16
-rw-r--r--drivers/vfio/Makefile3
-rw-r--r--drivers/vfio/pci/Kconfig8
-rw-r--r--drivers/vfio/pci/Makefile4
-rw-r--r--drivers/vfio/pci/vfio_pci.c579
-rw-r--r--drivers/vfio/pci/vfio_pci_config.c1540
-rw-r--r--drivers/vfio/pci/vfio_pci_intrs.c740
-rw-r--r--drivers/vfio/pci/vfio_pci_private.h91
-rw-r--r--drivers/vfio/pci/vfio_pci_rdwr.c269
-rw-r--r--drivers/vfio/vfio.c1420
-rw-r--r--drivers/vfio/vfio_iommu_type1.c753
189 files changed, 14584 insertions, 8878 deletions
diff --git a/drivers/Kconfig b/drivers/Kconfig
index 805c432c9439..ece958d3762e 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -112,6 +112,8 @@ source "drivers/auxdisplay/Kconfig"
112 112
113source "drivers/uio/Kconfig" 113source "drivers/uio/Kconfig"
114 114
115source "drivers/vfio/Kconfig"
116
115source "drivers/vlynq/Kconfig" 117source "drivers/vlynq/Kconfig"
116 118
117source "drivers/virtio/Kconfig" 119source "drivers/virtio/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index bd36f09f2246..5b421840c48d 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -60,6 +60,7 @@ obj-$(CONFIG_ATM) += atm/
60obj-$(CONFIG_FUSION) += message/ 60obj-$(CONFIG_FUSION) += message/
61obj-y += firewire/ 61obj-y += firewire/
62obj-$(CONFIG_UIO) += uio/ 62obj-$(CONFIG_UIO) += uio/
63obj-$(CONFIG_VFIO) += vfio/
63obj-y += cdrom/ 64obj-y += cdrom/
64obj-y += auxdisplay/ 65obj-y += auxdisplay/
65obj-$(CONFIG_PCCARD) += pcmcia/ 66obj-$(CONFIG_PCCARD) += pcmcia/
diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index 8f428a8ab003..9917943a3572 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -55,8 +55,6 @@
55 55
56#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */ 56#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
57 57
58#define RBD_MAX_MD_NAME_LEN (RBD_MAX_OBJ_NAME_LEN + sizeof(RBD_SUFFIX))
59#define RBD_MAX_POOL_NAME_LEN 64
60#define RBD_MAX_SNAP_NAME_LEN 32 58#define RBD_MAX_SNAP_NAME_LEN 32
61#define RBD_MAX_OPT_LEN 1024 59#define RBD_MAX_OPT_LEN 1024
62 60
@@ -78,13 +76,12 @@
78 */ 76 */
79struct rbd_image_header { 77struct rbd_image_header {
80 u64 image_size; 78 u64 image_size;
81 char block_name[32]; 79 char *object_prefix;
82 __u8 obj_order; 80 __u8 obj_order;
83 __u8 crypt_type; 81 __u8 crypt_type;
84 __u8 comp_type; 82 __u8 comp_type;
85 struct ceph_snap_context *snapc; 83 struct ceph_snap_context *snapc;
86 size_t snap_names_len; 84 size_t snap_names_len;
87 u64 snap_seq;
88 u32 total_snaps; 85 u32 total_snaps;
89 86
90 char *snap_names; 87 char *snap_names;
@@ -150,7 +147,7 @@ struct rbd_snap {
150 * a single device 147 * a single device
151 */ 148 */
152struct rbd_device { 149struct rbd_device {
153 int id; /* blkdev unique id */ 150 int dev_id; /* blkdev unique id */
154 151
155 int major; /* blkdev assigned major */ 152 int major; /* blkdev assigned major */
156 struct gendisk *disk; /* blkdev's gendisk and rq */ 153 struct gendisk *disk; /* blkdev's gendisk and rq */
@@ -163,20 +160,24 @@ struct rbd_device {
163 spinlock_t lock; /* queue lock */ 160 spinlock_t lock; /* queue lock */
164 161
165 struct rbd_image_header header; 162 struct rbd_image_header header;
166 char obj[RBD_MAX_OBJ_NAME_LEN]; /* rbd image name */ 163 char *image_name;
167 int obj_len; 164 size_t image_name_len;
168 char obj_md_name[RBD_MAX_MD_NAME_LEN]; /* hdr nm. */ 165 char *header_name;
169 char pool_name[RBD_MAX_POOL_NAME_LEN]; 166 char *pool_name;
170 int poolid; 167 int pool_id;
171 168
172 struct ceph_osd_event *watch_event; 169 struct ceph_osd_event *watch_event;
173 struct ceph_osd_request *watch_request; 170 struct ceph_osd_request *watch_request;
174 171
175 /* protects updating the header */ 172 /* protects updating the header */
176 struct rw_semaphore header_rwsem; 173 struct rw_semaphore header_rwsem;
177 char snap_name[RBD_MAX_SNAP_NAME_LEN]; 174 /* name of the snapshot this device reads from */
175 char *snap_name;
176 /* id of the snapshot this device reads from */
178 u64 snap_id; /* current snapshot id */ 177 u64 snap_id; /* current snapshot id */
179 int read_only; 178 /* whether the snap_id this device reads from still exists */
179 bool snap_exists;
180 int read_only;
180 181
181 struct list_head node; 182 struct list_head node;
182 183
@@ -201,8 +202,7 @@ static ssize_t rbd_snap_add(struct device *dev,
201 struct device_attribute *attr, 202 struct device_attribute *attr,
202 const char *buf, 203 const char *buf,
203 size_t count); 204 size_t count);
204static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev, 205static void __rbd_remove_snap_dev(struct rbd_snap *snap);
205 struct rbd_snap *snap);
206 206
207static ssize_t rbd_add(struct bus_type *bus, const char *buf, 207static ssize_t rbd_add(struct bus_type *bus, const char *buf,
208 size_t count); 208 size_t count);
@@ -240,7 +240,7 @@ static void rbd_put_dev(struct rbd_device *rbd_dev)
240 put_device(&rbd_dev->dev); 240 put_device(&rbd_dev->dev);
241} 241}
242 242
243static int __rbd_refresh_header(struct rbd_device *rbd_dev); 243static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver);
244 244
245static int rbd_open(struct block_device *bdev, fmode_t mode) 245static int rbd_open(struct block_device *bdev, fmode_t mode)
246{ 246{
@@ -273,9 +273,9 @@ static const struct block_device_operations rbd_bd_ops = {
273 273
274/* 274/*
275 * Initialize an rbd client instance. 275 * Initialize an rbd client instance.
276 * We own *opt. 276 * We own *ceph_opts.
277 */ 277 */
278static struct rbd_client *rbd_client_create(struct ceph_options *opt, 278static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts,
279 struct rbd_options *rbd_opts) 279 struct rbd_options *rbd_opts)
280{ 280{
281 struct rbd_client *rbdc; 281 struct rbd_client *rbdc;
@@ -291,10 +291,10 @@ static struct rbd_client *rbd_client_create(struct ceph_options *opt,
291 291
292 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); 292 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
293 293
294 rbdc->client = ceph_create_client(opt, rbdc, 0, 0); 294 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
295 if (IS_ERR(rbdc->client)) 295 if (IS_ERR(rbdc->client))
296 goto out_mutex; 296 goto out_mutex;
297 opt = NULL; /* Now rbdc->client is responsible for opt */ 297 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
298 298
299 ret = ceph_open_session(rbdc->client); 299 ret = ceph_open_session(rbdc->client);
300 if (ret < 0) 300 if (ret < 0)
@@ -317,23 +317,23 @@ out_mutex:
317 mutex_unlock(&ctl_mutex); 317 mutex_unlock(&ctl_mutex);
318 kfree(rbdc); 318 kfree(rbdc);
319out_opt: 319out_opt:
320 if (opt) 320 if (ceph_opts)
321 ceph_destroy_options(opt); 321 ceph_destroy_options(ceph_opts);
322 return ERR_PTR(ret); 322 return ERR_PTR(ret);
323} 323}
324 324
325/* 325/*
326 * Find a ceph client with specific addr and configuration. 326 * Find a ceph client with specific addr and configuration.
327 */ 327 */
328static struct rbd_client *__rbd_client_find(struct ceph_options *opt) 328static struct rbd_client *__rbd_client_find(struct ceph_options *ceph_opts)
329{ 329{
330 struct rbd_client *client_node; 330 struct rbd_client *client_node;
331 331
332 if (opt->flags & CEPH_OPT_NOSHARE) 332 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
333 return NULL; 333 return NULL;
334 334
335 list_for_each_entry(client_node, &rbd_client_list, node) 335 list_for_each_entry(client_node, &rbd_client_list, node)
336 if (ceph_compare_options(opt, client_node->client) == 0) 336 if (!ceph_compare_options(ceph_opts, client_node->client))
337 return client_node; 337 return client_node;
338 return NULL; 338 return NULL;
339} 339}
@@ -349,7 +349,7 @@ enum {
349 /* string args above */ 349 /* string args above */
350}; 350};
351 351
352static match_table_t rbdopt_tokens = { 352static match_table_t rbd_opts_tokens = {
353 {Opt_notify_timeout, "notify_timeout=%d"}, 353 {Opt_notify_timeout, "notify_timeout=%d"},
354 /* int args above */ 354 /* int args above */
355 /* string args above */ 355 /* string args above */
@@ -358,11 +358,11 @@ static match_table_t rbdopt_tokens = {
358 358
359static int parse_rbd_opts_token(char *c, void *private) 359static int parse_rbd_opts_token(char *c, void *private)
360{ 360{
361 struct rbd_options *rbdopt = private; 361 struct rbd_options *rbd_opts = private;
362 substring_t argstr[MAX_OPT_ARGS]; 362 substring_t argstr[MAX_OPT_ARGS];
363 int token, intval, ret; 363 int token, intval, ret;
364 364
365 token = match_token(c, rbdopt_tokens, argstr); 365 token = match_token(c, rbd_opts_tokens, argstr);
366 if (token < 0) 366 if (token < 0)
367 return -EINVAL; 367 return -EINVAL;
368 368
@@ -383,7 +383,7 @@ static int parse_rbd_opts_token(char *c, void *private)
383 383
384 switch (token) { 384 switch (token) {
385 case Opt_notify_timeout: 385 case Opt_notify_timeout:
386 rbdopt->notify_timeout = intval; 386 rbd_opts->notify_timeout = intval;
387 break; 387 break;
388 default: 388 default:
389 BUG_ON(token); 389 BUG_ON(token);
@@ -400,7 +400,7 @@ static struct rbd_client *rbd_get_client(const char *mon_addr,
400 char *options) 400 char *options)
401{ 401{
402 struct rbd_client *rbdc; 402 struct rbd_client *rbdc;
403 struct ceph_options *opt; 403 struct ceph_options *ceph_opts;
404 struct rbd_options *rbd_opts; 404 struct rbd_options *rbd_opts;
405 405
406 rbd_opts = kzalloc(sizeof(*rbd_opts), GFP_KERNEL); 406 rbd_opts = kzalloc(sizeof(*rbd_opts), GFP_KERNEL);
@@ -409,29 +409,29 @@ static struct rbd_client *rbd_get_client(const char *mon_addr,
409 409
410 rbd_opts->notify_timeout = RBD_NOTIFY_TIMEOUT_DEFAULT; 410 rbd_opts->notify_timeout = RBD_NOTIFY_TIMEOUT_DEFAULT;
411 411
412 opt = ceph_parse_options(options, mon_addr, 412 ceph_opts = ceph_parse_options(options, mon_addr,
413 mon_addr + mon_addr_len, 413 mon_addr + mon_addr_len,
414 parse_rbd_opts_token, rbd_opts); 414 parse_rbd_opts_token, rbd_opts);
415 if (IS_ERR(opt)) { 415 if (IS_ERR(ceph_opts)) {
416 kfree(rbd_opts); 416 kfree(rbd_opts);
417 return ERR_CAST(opt); 417 return ERR_CAST(ceph_opts);
418 } 418 }
419 419
420 spin_lock(&rbd_client_list_lock); 420 spin_lock(&rbd_client_list_lock);
421 rbdc = __rbd_client_find(opt); 421 rbdc = __rbd_client_find(ceph_opts);
422 if (rbdc) { 422 if (rbdc) {
423 /* using an existing client */ 423 /* using an existing client */
424 kref_get(&rbdc->kref); 424 kref_get(&rbdc->kref);
425 spin_unlock(&rbd_client_list_lock); 425 spin_unlock(&rbd_client_list_lock);
426 426
427 ceph_destroy_options(opt); 427 ceph_destroy_options(ceph_opts);
428 kfree(rbd_opts); 428 kfree(rbd_opts);
429 429
430 return rbdc; 430 return rbdc;
431 } 431 }
432 spin_unlock(&rbd_client_list_lock); 432 spin_unlock(&rbd_client_list_lock);
433 433
434 rbdc = rbd_client_create(opt, rbd_opts); 434 rbdc = rbd_client_create(ceph_opts, rbd_opts);
435 435
436 if (IS_ERR(rbdc)) 436 if (IS_ERR(rbdc))
437 kfree(rbd_opts); 437 kfree(rbd_opts);
@@ -480,46 +480,60 @@ static void rbd_coll_release(struct kref *kref)
480 kfree(coll); 480 kfree(coll);
481} 481}
482 482
483static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
484{
485 return !memcmp(&ondisk->text,
486 RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT));
487}
488
483/* 489/*
484 * Create a new header structure, translate header format from the on-disk 490 * Create a new header structure, translate header format from the on-disk
485 * header. 491 * header.
486 */ 492 */
487static int rbd_header_from_disk(struct rbd_image_header *header, 493static int rbd_header_from_disk(struct rbd_image_header *header,
488 struct rbd_image_header_ondisk *ondisk, 494 struct rbd_image_header_ondisk *ondisk,
489 u32 allocated_snaps, 495 u32 allocated_snaps)
490 gfp_t gfp_flags)
491{ 496{
492 u32 i, snap_count; 497 u32 snap_count;
493 498
494 if (memcmp(ondisk, RBD_HEADER_TEXT, sizeof(RBD_HEADER_TEXT))) 499 if (!rbd_dev_ondisk_valid(ondisk))
495 return -ENXIO; 500 return -ENXIO;
496 501
497 snap_count = le32_to_cpu(ondisk->snap_count); 502 snap_count = le32_to_cpu(ondisk->snap_count);
498 if (snap_count > (UINT_MAX - sizeof(struct ceph_snap_context)) 503 if (snap_count > (SIZE_MAX - sizeof(struct ceph_snap_context))
499 / sizeof (*ondisk)) 504 / sizeof (u64))
500 return -EINVAL; 505 return -EINVAL;
501 header->snapc = kmalloc(sizeof(struct ceph_snap_context) + 506 header->snapc = kmalloc(sizeof(struct ceph_snap_context) +
502 snap_count * sizeof(u64), 507 snap_count * sizeof(u64),
503 gfp_flags); 508 GFP_KERNEL);
504 if (!header->snapc) 509 if (!header->snapc)
505 return -ENOMEM; 510 return -ENOMEM;
506 511
507 header->snap_names_len = le64_to_cpu(ondisk->snap_names_len);
508 if (snap_count) { 512 if (snap_count) {
513 header->snap_names_len = le64_to_cpu(ondisk->snap_names_len);
509 header->snap_names = kmalloc(header->snap_names_len, 514 header->snap_names = kmalloc(header->snap_names_len,
510 gfp_flags); 515 GFP_KERNEL);
511 if (!header->snap_names) 516 if (!header->snap_names)
512 goto err_snapc; 517 goto err_snapc;
513 header->snap_sizes = kmalloc(snap_count * sizeof(u64), 518 header->snap_sizes = kmalloc(snap_count * sizeof(u64),
514 gfp_flags); 519 GFP_KERNEL);
515 if (!header->snap_sizes) 520 if (!header->snap_sizes)
516 goto err_names; 521 goto err_names;
517 } else { 522 } else {
523 WARN_ON(ondisk->snap_names_len);
524 header->snap_names_len = 0;
518 header->snap_names = NULL; 525 header->snap_names = NULL;
519 header->snap_sizes = NULL; 526 header->snap_sizes = NULL;
520 } 527 }
521 memcpy(header->block_name, ondisk->block_name, 528
529 header->object_prefix = kmalloc(sizeof (ondisk->block_name) + 1,
530 GFP_KERNEL);
531 if (!header->object_prefix)
532 goto err_sizes;
533
534 memcpy(header->object_prefix, ondisk->block_name,
522 sizeof(ondisk->block_name)); 535 sizeof(ondisk->block_name));
536 header->object_prefix[sizeof (ondisk->block_name)] = '\0';
523 537
524 header->image_size = le64_to_cpu(ondisk->image_size); 538 header->image_size = le64_to_cpu(ondisk->image_size);
525 header->obj_order = ondisk->options.order; 539 header->obj_order = ondisk->options.order;
@@ -527,11 +541,13 @@ static int rbd_header_from_disk(struct rbd_image_header *header,
527 header->comp_type = ondisk->options.comp_type; 541 header->comp_type = ondisk->options.comp_type;
528 542
529 atomic_set(&header->snapc->nref, 1); 543 atomic_set(&header->snapc->nref, 1);
530 header->snap_seq = le64_to_cpu(ondisk->snap_seq); 544 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
531 header->snapc->num_snaps = snap_count; 545 header->snapc->num_snaps = snap_count;
532 header->total_snaps = snap_count; 546 header->total_snaps = snap_count;
533 547
534 if (snap_count && allocated_snaps == snap_count) { 548 if (snap_count && allocated_snaps == snap_count) {
549 int i;
550
535 for (i = 0; i < snap_count; i++) { 551 for (i = 0; i < snap_count; i++) {
536 header->snapc->snaps[i] = 552 header->snapc->snaps[i] =
537 le64_to_cpu(ondisk->snaps[i].id); 553 le64_to_cpu(ondisk->snaps[i].id);
@@ -540,16 +556,22 @@ static int rbd_header_from_disk(struct rbd_image_header *header,
540 } 556 }
541 557
542 /* copy snapshot names */ 558 /* copy snapshot names */
543 memcpy(header->snap_names, &ondisk->snaps[i], 559 memcpy(header->snap_names, &ondisk->snaps[snap_count],
544 header->snap_names_len); 560 header->snap_names_len);
545 } 561 }
546 562
547 return 0; 563 return 0;
548 564
565err_sizes:
566 kfree(header->snap_sizes);
567 header->snap_sizes = NULL;
549err_names: 568err_names:
550 kfree(header->snap_names); 569 kfree(header->snap_names);
570 header->snap_names = NULL;
551err_snapc: 571err_snapc:
552 kfree(header->snapc); 572 kfree(header->snapc);
573 header->snapc = NULL;
574
553 return -ENOMEM; 575 return -ENOMEM;
554} 576}
555 577
@@ -575,52 +597,50 @@ static int snap_by_name(struct rbd_image_header *header, const char *snap_name,
575 return -ENOENT; 597 return -ENOENT;
576} 598}
577 599
578static int rbd_header_set_snap(struct rbd_device *dev, u64 *size) 600static int rbd_header_set_snap(struct rbd_device *rbd_dev, u64 *size)
579{ 601{
580 struct rbd_image_header *header = &dev->header; 602 int ret;
581 struct ceph_snap_context *snapc = header->snapc;
582 int ret = -ENOENT;
583
584 BUILD_BUG_ON(sizeof (dev->snap_name) < sizeof (RBD_SNAP_HEAD_NAME));
585 603
586 down_write(&dev->header_rwsem); 604 down_write(&rbd_dev->header_rwsem);
587 605
588 if (!memcmp(dev->snap_name, RBD_SNAP_HEAD_NAME, 606 if (!memcmp(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME,
589 sizeof (RBD_SNAP_HEAD_NAME))) { 607 sizeof (RBD_SNAP_HEAD_NAME))) {
590 if (header->total_snaps) 608 rbd_dev->snap_id = CEPH_NOSNAP;
591 snapc->seq = header->snap_seq; 609 rbd_dev->snap_exists = false;
592 else 610 rbd_dev->read_only = 0;
593 snapc->seq = 0;
594 dev->snap_id = CEPH_NOSNAP;
595 dev->read_only = 0;
596 if (size) 611 if (size)
597 *size = header->image_size; 612 *size = rbd_dev->header.image_size;
598 } else { 613 } else {
599 ret = snap_by_name(header, dev->snap_name, &snapc->seq, size); 614 u64 snap_id = 0;
615
616 ret = snap_by_name(&rbd_dev->header, rbd_dev->snap_name,
617 &snap_id, size);
600 if (ret < 0) 618 if (ret < 0)
601 goto done; 619 goto done;
602 dev->snap_id = snapc->seq; 620 rbd_dev->snap_id = snap_id;
603 dev->read_only = 1; 621 rbd_dev->snap_exists = true;
622 rbd_dev->read_only = 1;
604 } 623 }
605 624
606 ret = 0; 625 ret = 0;
607done: 626done:
608 up_write(&dev->header_rwsem); 627 up_write(&rbd_dev->header_rwsem);
609 return ret; 628 return ret;
610} 629}
611 630
612static void rbd_header_free(struct rbd_image_header *header) 631static void rbd_header_free(struct rbd_image_header *header)
613{ 632{
614 kfree(header->snapc); 633 kfree(header->object_prefix);
615 kfree(header->snap_names);
616 kfree(header->snap_sizes); 634 kfree(header->snap_sizes);
635 kfree(header->snap_names);
636 ceph_put_snap_context(header->snapc);
617} 637}
618 638
619/* 639/*
620 * get the actual striped segment name, offset and length 640 * get the actual striped segment name, offset and length
621 */ 641 */
622static u64 rbd_get_segment(struct rbd_image_header *header, 642static u64 rbd_get_segment(struct rbd_image_header *header,
623 const char *block_name, 643 const char *object_prefix,
624 u64 ofs, u64 len, 644 u64 ofs, u64 len,
625 char *seg_name, u64 *segofs) 645 char *seg_name, u64 *segofs)
626{ 646{
@@ -628,7 +648,7 @@ static u64 rbd_get_segment(struct rbd_image_header *header,
628 648
629 if (seg_name) 649 if (seg_name)
630 snprintf(seg_name, RBD_MAX_SEG_NAME_LEN, 650 snprintf(seg_name, RBD_MAX_SEG_NAME_LEN,
631 "%s.%012llx", block_name, seg); 651 "%s.%012llx", object_prefix, seg);
632 652
633 ofs = ofs & ((1 << header->obj_order) - 1); 653 ofs = ofs & ((1 << header->obj_order) - 1);
634 len = min_t(u64, len, (1 << header->obj_order) - ofs); 654 len = min_t(u64, len, (1 << header->obj_order) - ofs);
@@ -726,9 +746,8 @@ static struct bio *bio_chain_clone(struct bio **old, struct bio **next,
726 * split_bio will BUG_ON if this is not the case 746 * split_bio will BUG_ON if this is not the case
727 */ 747 */
728 dout("bio_chain_clone split! total=%d remaining=%d" 748 dout("bio_chain_clone split! total=%d remaining=%d"
729 "bi_size=%d\n", 749 "bi_size=%u\n",
730 (int)total, (int)len-total, 750 total, len - total, old_chain->bi_size);
731 (int)old_chain->bi_size);
732 751
733 /* split the bio. We'll release it either in the next 752 /* split the bio. We'll release it either in the next
734 call, or it will have to be released outside */ 753 call, or it will have to be released outside */
@@ -777,22 +796,24 @@ err_out:
777/* 796/*
778 * helpers for osd request op vectors. 797 * helpers for osd request op vectors.
779 */ 798 */
780static int rbd_create_rw_ops(struct ceph_osd_req_op **ops, 799static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops,
781 int num_ops, 800 int opcode, u32 payload_len)
782 int opcode, 801{
783 u32 payload_len) 802 struct ceph_osd_req_op *ops;
784{ 803
785 *ops = kzalloc(sizeof(struct ceph_osd_req_op) * (num_ops + 1), 804 ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO);
786 GFP_NOIO); 805 if (!ops)
787 if (!*ops) 806 return NULL;
788 return -ENOMEM; 807
789 (*ops)[0].op = opcode; 808 ops[0].op = opcode;
809
790 /* 810 /*
791 * op extent offset and length will be set later on 811 * op extent offset and length will be set later on
792 * in calc_raw_layout() 812 * in calc_raw_layout()
793 */ 813 */
794 (*ops)[0].payload_len = payload_len; 814 ops[0].payload_len = payload_len;
795 return 0; 815
816 return ops;
796} 817}
797 818
798static void rbd_destroy_ops(struct ceph_osd_req_op *ops) 819static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
@@ -808,8 +829,8 @@ static void rbd_coll_end_req_index(struct request *rq,
808 struct request_queue *q; 829 struct request_queue *q;
809 int min, max, i; 830 int min, max, i;
810 831
811 dout("rbd_coll_end_req_index %p index %d ret %d len %lld\n", 832 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
812 coll, index, ret, len); 833 coll, index, ret, (unsigned long long) len);
813 834
814 if (!rq) 835 if (!rq)
815 return; 836 return;
@@ -848,16 +869,15 @@ static void rbd_coll_end_req(struct rbd_request *req,
848 * Send ceph osd request 869 * Send ceph osd request
849 */ 870 */
850static int rbd_do_request(struct request *rq, 871static int rbd_do_request(struct request *rq,
851 struct rbd_device *dev, 872 struct rbd_device *rbd_dev,
852 struct ceph_snap_context *snapc, 873 struct ceph_snap_context *snapc,
853 u64 snapid, 874 u64 snapid,
854 const char *obj, u64 ofs, u64 len, 875 const char *object_name, u64 ofs, u64 len,
855 struct bio *bio, 876 struct bio *bio,
856 struct page **pages, 877 struct page **pages,
857 int num_pages, 878 int num_pages,
858 int flags, 879 int flags,
859 struct ceph_osd_req_op *ops, 880 struct ceph_osd_req_op *ops,
860 int num_reply,
861 struct rbd_req_coll *coll, 881 struct rbd_req_coll *coll,
862 int coll_index, 882 int coll_index,
863 void (*rbd_cb)(struct ceph_osd_request *req, 883 void (*rbd_cb)(struct ceph_osd_request *req,
@@ -887,15 +907,13 @@ static int rbd_do_request(struct request *rq,
887 req_data->coll_index = coll_index; 907 req_data->coll_index = coll_index;
888 } 908 }
889 909
890 dout("rbd_do_request obj=%s ofs=%lld len=%lld\n", obj, len, ofs); 910 dout("rbd_do_request object_name=%s ofs=%llu len=%llu\n", object_name,
891 911 (unsigned long long) ofs, (unsigned long long) len);
892 down_read(&dev->header_rwsem);
893 912
894 osdc = &dev->rbd_client->client->osdc; 913 osdc = &rbd_dev->rbd_client->client->osdc;
895 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops, 914 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
896 false, GFP_NOIO, pages, bio); 915 false, GFP_NOIO, pages, bio);
897 if (!req) { 916 if (!req) {
898 up_read(&dev->header_rwsem);
899 ret = -ENOMEM; 917 ret = -ENOMEM;
900 goto done_pages; 918 goto done_pages;
901 } 919 }
@@ -912,7 +930,7 @@ static int rbd_do_request(struct request *rq,
912 reqhead = req->r_request->front.iov_base; 930 reqhead = req->r_request->front.iov_base;
913 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP); 931 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
914 932
915 strncpy(req->r_oid, obj, sizeof(req->r_oid)); 933 strncpy(req->r_oid, object_name, sizeof(req->r_oid));
916 req->r_oid_len = strlen(req->r_oid); 934 req->r_oid_len = strlen(req->r_oid);
917 935
918 layout = &req->r_file_layout; 936 layout = &req->r_file_layout;
@@ -920,7 +938,7 @@ static int rbd_do_request(struct request *rq,
920 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); 938 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
921 layout->fl_stripe_count = cpu_to_le32(1); 939 layout->fl_stripe_count = cpu_to_le32(1);
922 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); 940 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
923 layout->fl_pg_pool = cpu_to_le32(dev->poolid); 941 layout->fl_pg_pool = cpu_to_le32(rbd_dev->pool_id);
924 ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno, 942 ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
925 req, ops); 943 req, ops);
926 944
@@ -929,7 +947,6 @@ static int rbd_do_request(struct request *rq,
929 snapc, 947 snapc,
930 &mtime, 948 &mtime,
931 req->r_oid, req->r_oid_len); 949 req->r_oid, req->r_oid_len);
932 up_read(&dev->header_rwsem);
933 950
934 if (linger_req) { 951 if (linger_req) {
935 ceph_osdc_set_request_linger(osdc, req); 952 ceph_osdc_set_request_linger(osdc, req);
@@ -944,8 +961,9 @@ static int rbd_do_request(struct request *rq,
944 ret = ceph_osdc_wait_request(osdc, req); 961 ret = ceph_osdc_wait_request(osdc, req);
945 if (ver) 962 if (ver)
946 *ver = le64_to_cpu(req->r_reassert_version.version); 963 *ver = le64_to_cpu(req->r_reassert_version.version);
947 dout("reassert_ver=%lld\n", 964 dout("reassert_ver=%llu\n",
948 le64_to_cpu(req->r_reassert_version.version)); 965 (unsigned long long)
966 le64_to_cpu(req->r_reassert_version.version));
949 ceph_osdc_put_request(req); 967 ceph_osdc_put_request(req);
950 } 968 }
951 return ret; 969 return ret;
@@ -979,7 +997,8 @@ static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
979 bytes = le64_to_cpu(op->extent.length); 997 bytes = le64_to_cpu(op->extent.length);
980 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ); 998 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
981 999
982 dout("rbd_req_cb bytes=%lld readop=%d rc=%d\n", bytes, read_op, rc); 1000 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1001 (unsigned long long) bytes, read_op, (int) rc);
983 1002
984 if (rc == -ENOENT && read_op) { 1003 if (rc == -ENOENT && read_op) {
985 zero_bio_chain(req_data->bio, 0); 1004 zero_bio_chain(req_data->bio, 0);
@@ -1006,14 +1025,12 @@ static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg
1006/* 1025/*
1007 * Do a synchronous ceph osd operation 1026 * Do a synchronous ceph osd operation
1008 */ 1027 */
1009static int rbd_req_sync_op(struct rbd_device *dev, 1028static int rbd_req_sync_op(struct rbd_device *rbd_dev,
1010 struct ceph_snap_context *snapc, 1029 struct ceph_snap_context *snapc,
1011 u64 snapid, 1030 u64 snapid,
1012 int opcode,
1013 int flags, 1031 int flags,
1014 struct ceph_osd_req_op *orig_ops, 1032 struct ceph_osd_req_op *ops,
1015 int num_reply, 1033 const char *object_name,
1016 const char *obj,
1017 u64 ofs, u64 len, 1034 u64 ofs, u64 len,
1018 char *buf, 1035 char *buf,
1019 struct ceph_osd_request **linger_req, 1036 struct ceph_osd_request **linger_req,
@@ -1022,45 +1039,28 @@ static int rbd_req_sync_op(struct rbd_device *dev,
1022 int ret; 1039 int ret;
1023 struct page **pages; 1040 struct page **pages;
1024 int num_pages; 1041 int num_pages;
1025 struct ceph_osd_req_op *ops = orig_ops; 1042
1026 u32 payload_len; 1043 BUG_ON(ops == NULL);
1027 1044
1028 num_pages = calc_pages_for(ofs , len); 1045 num_pages = calc_pages_for(ofs , len);
1029 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); 1046 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
1030 if (IS_ERR(pages)) 1047 if (IS_ERR(pages))
1031 return PTR_ERR(pages); 1048 return PTR_ERR(pages);
1032 1049
1033 if (!orig_ops) { 1050 ret = rbd_do_request(NULL, rbd_dev, snapc, snapid,
1034 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? len : 0); 1051 object_name, ofs, len, NULL,
1035 ret = rbd_create_rw_ops(&ops, 1, opcode, payload_len);
1036 if (ret < 0)
1037 goto done;
1038
1039 if ((flags & CEPH_OSD_FLAG_WRITE) && buf) {
1040 ret = ceph_copy_to_page_vector(pages, buf, ofs, len);
1041 if (ret < 0)
1042 goto done_ops;
1043 }
1044 }
1045
1046 ret = rbd_do_request(NULL, dev, snapc, snapid,
1047 obj, ofs, len, NULL,
1048 pages, num_pages, 1052 pages, num_pages,
1049 flags, 1053 flags,
1050 ops, 1054 ops,
1051 2,
1052 NULL, 0, 1055 NULL, 0,
1053 NULL, 1056 NULL,
1054 linger_req, ver); 1057 linger_req, ver);
1055 if (ret < 0) 1058 if (ret < 0)
1056 goto done_ops; 1059 goto done;
1057 1060
1058 if ((flags & CEPH_OSD_FLAG_READ) && buf) 1061 if ((flags & CEPH_OSD_FLAG_READ) && buf)
1059 ret = ceph_copy_from_page_vector(pages, buf, ofs, ret); 1062 ret = ceph_copy_from_page_vector(pages, buf, ofs, ret);
1060 1063
1061done_ops:
1062 if (!orig_ops)
1063 rbd_destroy_ops(ops);
1064done: 1064done:
1065 ceph_release_page_vector(pages, num_pages); 1065 ceph_release_page_vector(pages, num_pages);
1066 return ret; 1066 return ret;
@@ -1070,10 +1070,10 @@ done:
1070 * Do an asynchronous ceph osd operation 1070 * Do an asynchronous ceph osd operation
1071 */ 1071 */
1072static int rbd_do_op(struct request *rq, 1072static int rbd_do_op(struct request *rq,
1073 struct rbd_device *rbd_dev , 1073 struct rbd_device *rbd_dev,
1074 struct ceph_snap_context *snapc, 1074 struct ceph_snap_context *snapc,
1075 u64 snapid, 1075 u64 snapid,
1076 int opcode, int flags, int num_reply, 1076 int opcode, int flags,
1077 u64 ofs, u64 len, 1077 u64 ofs, u64 len,
1078 struct bio *bio, 1078 struct bio *bio,
1079 struct rbd_req_coll *coll, 1079 struct rbd_req_coll *coll,
@@ -1091,14 +1091,15 @@ static int rbd_do_op(struct request *rq,
1091 return -ENOMEM; 1091 return -ENOMEM;
1092 1092
1093 seg_len = rbd_get_segment(&rbd_dev->header, 1093 seg_len = rbd_get_segment(&rbd_dev->header,
1094 rbd_dev->header.block_name, 1094 rbd_dev->header.object_prefix,
1095 ofs, len, 1095 ofs, len,
1096 seg_name, &seg_ofs); 1096 seg_name, &seg_ofs);
1097 1097
1098 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0); 1098 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0);
1099 1099
1100 ret = rbd_create_rw_ops(&ops, 1, opcode, payload_len); 1100 ret = -ENOMEM;
1101 if (ret < 0) 1101 ops = rbd_create_rw_ops(1, opcode, payload_len);
1102 if (!ops)
1102 goto done; 1103 goto done;
1103 1104
1104 /* we've taken care of segment sizes earlier when we 1105 /* we've taken care of segment sizes earlier when we
@@ -1112,7 +1113,6 @@ static int rbd_do_op(struct request *rq,
1112 NULL, 0, 1113 NULL, 0,
1113 flags, 1114 flags,
1114 ops, 1115 ops,
1115 num_reply,
1116 coll, coll_index, 1116 coll, coll_index,
1117 rbd_req_cb, 0, NULL); 1117 rbd_req_cb, 0, NULL);
1118 1118
@@ -1136,7 +1136,6 @@ static int rbd_req_write(struct request *rq,
1136 return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP, 1136 return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP,
1137 CEPH_OSD_OP_WRITE, 1137 CEPH_OSD_OP_WRITE,
1138 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, 1138 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1139 2,
1140 ofs, len, bio, coll, coll_index); 1139 ofs, len, bio, coll, coll_index);
1141} 1140}
1142 1141
@@ -1155,55 +1154,58 @@ static int rbd_req_read(struct request *rq,
1155 snapid, 1154 snapid,
1156 CEPH_OSD_OP_READ, 1155 CEPH_OSD_OP_READ,
1157 CEPH_OSD_FLAG_READ, 1156 CEPH_OSD_FLAG_READ,
1158 2,
1159 ofs, len, bio, coll, coll_index); 1157 ofs, len, bio, coll, coll_index);
1160} 1158}
1161 1159
1162/* 1160/*
1163 * Request sync osd read 1161 * Request sync osd read
1164 */ 1162 */
1165static int rbd_req_sync_read(struct rbd_device *dev, 1163static int rbd_req_sync_read(struct rbd_device *rbd_dev,
1166 struct ceph_snap_context *snapc,
1167 u64 snapid, 1164 u64 snapid,
1168 const char *obj, 1165 const char *object_name,
1169 u64 ofs, u64 len, 1166 u64 ofs, u64 len,
1170 char *buf, 1167 char *buf,
1171 u64 *ver) 1168 u64 *ver)
1172{ 1169{
1173 return rbd_req_sync_op(dev, NULL, 1170 struct ceph_osd_req_op *ops;
1171 int ret;
1172
1173 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0);
1174 if (!ops)
1175 return -ENOMEM;
1176
1177 ret = rbd_req_sync_op(rbd_dev, NULL,
1174 snapid, 1178 snapid,
1175 CEPH_OSD_OP_READ,
1176 CEPH_OSD_FLAG_READ, 1179 CEPH_OSD_FLAG_READ,
1177 NULL, 1180 ops, object_name, ofs, len, buf, NULL, ver);
1178 1, obj, ofs, len, buf, NULL, ver); 1181 rbd_destroy_ops(ops);
1182
1183 return ret;
1179} 1184}
1180 1185
1181/* 1186/*
1182 * Request sync osd watch 1187 * Request sync osd watch
1183 */ 1188 */
1184static int rbd_req_sync_notify_ack(struct rbd_device *dev, 1189static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
1185 u64 ver, 1190 u64 ver,
1186 u64 notify_id, 1191 u64 notify_id)
1187 const char *obj)
1188{ 1192{
1189 struct ceph_osd_req_op *ops; 1193 struct ceph_osd_req_op *ops;
1190 struct page **pages = NULL;
1191 int ret; 1194 int ret;
1192 1195
1193 ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_NOTIFY_ACK, 0); 1196 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0);
1194 if (ret < 0) 1197 if (!ops)
1195 return ret; 1198 return -ENOMEM;
1196 1199
1197 ops[0].watch.ver = cpu_to_le64(dev->header.obj_version); 1200 ops[0].watch.ver = cpu_to_le64(ver);
1198 ops[0].watch.cookie = notify_id; 1201 ops[0].watch.cookie = notify_id;
1199 ops[0].watch.flag = 0; 1202 ops[0].watch.flag = 0;
1200 1203
1201 ret = rbd_do_request(NULL, dev, NULL, CEPH_NOSNAP, 1204 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
1202 obj, 0, 0, NULL, 1205 rbd_dev->header_name, 0, 0, NULL,
1203 pages, 0, 1206 NULL, 0,
1204 CEPH_OSD_FLAG_READ, 1207 CEPH_OSD_FLAG_READ,
1205 ops, 1208 ops,
1206 1,
1207 NULL, 0, 1209 NULL, 0,
1208 rbd_simple_req_cb, 0, NULL); 1210 rbd_simple_req_cb, 0, NULL);
1209 1211
@@ -1213,54 +1215,53 @@ static int rbd_req_sync_notify_ack(struct rbd_device *dev,
1213 1215
1214static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data) 1216static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1215{ 1217{
1216 struct rbd_device *dev = (struct rbd_device *)data; 1218 struct rbd_device *rbd_dev = (struct rbd_device *)data;
1219 u64 hver;
1217 int rc; 1220 int rc;
1218 1221
1219 if (!dev) 1222 if (!rbd_dev)
1220 return; 1223 return;
1221 1224
1222 dout("rbd_watch_cb %s notify_id=%lld opcode=%d\n", dev->obj_md_name, 1225 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1223 notify_id, (int)opcode); 1226 rbd_dev->header_name, (unsigned long long) notify_id,
1224 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); 1227 (unsigned int) opcode);
1225 rc = __rbd_refresh_header(dev); 1228 rc = rbd_refresh_header(rbd_dev, &hver);
1226 mutex_unlock(&ctl_mutex);
1227 if (rc) 1229 if (rc)
1228 pr_warning(RBD_DRV_NAME "%d got notification but failed to " 1230 pr_warning(RBD_DRV_NAME "%d got notification but failed to "
1229 " update snaps: %d\n", dev->major, rc); 1231 " update snaps: %d\n", rbd_dev->major, rc);
1230 1232
1231 rbd_req_sync_notify_ack(dev, ver, notify_id, dev->obj_md_name); 1233 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
1232} 1234}
1233 1235
1234/* 1236/*
1235 * Request sync osd watch 1237 * Request sync osd watch
1236 */ 1238 */
1237static int rbd_req_sync_watch(struct rbd_device *dev, 1239static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
1238 const char *obj,
1239 u64 ver)
1240{ 1240{
1241 struct ceph_osd_req_op *ops; 1241 struct ceph_osd_req_op *ops;
1242 struct ceph_osd_client *osdc = &dev->rbd_client->client->osdc; 1242 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1243 int ret;
1243 1244
1244 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_WATCH, 0); 1245 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1245 if (ret < 0) 1246 if (!ops)
1246 return ret; 1247 return -ENOMEM;
1247 1248
1248 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0, 1249 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
1249 (void *)dev, &dev->watch_event); 1250 (void *)rbd_dev, &rbd_dev->watch_event);
1250 if (ret < 0) 1251 if (ret < 0)
1251 goto fail; 1252 goto fail;
1252 1253
1253 ops[0].watch.ver = cpu_to_le64(ver); 1254 ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
1254 ops[0].watch.cookie = cpu_to_le64(dev->watch_event->cookie); 1255 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
1255 ops[0].watch.flag = 1; 1256 ops[0].watch.flag = 1;
1256 1257
1257 ret = rbd_req_sync_op(dev, NULL, 1258 ret = rbd_req_sync_op(rbd_dev, NULL,
1258 CEPH_NOSNAP, 1259 CEPH_NOSNAP,
1259 0,
1260 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, 1260 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1261 ops, 1261 ops,
1262 1, obj, 0, 0, NULL, 1262 rbd_dev->header_name,
1263 &dev->watch_request, NULL); 1263 0, 0, NULL,
1264 &rbd_dev->watch_request, NULL);
1264 1265
1265 if (ret < 0) 1266 if (ret < 0)
1266 goto fail_event; 1267 goto fail_event;
@@ -1269,8 +1270,8 @@ static int rbd_req_sync_watch(struct rbd_device *dev,
1269 return 0; 1270 return 0;
1270 1271
1271fail_event: 1272fail_event:
1272 ceph_osdc_cancel_event(dev->watch_event); 1273 ceph_osdc_cancel_event(rbd_dev->watch_event);
1273 dev->watch_event = NULL; 1274 rbd_dev->watch_event = NULL;
1274fail: 1275fail:
1275 rbd_destroy_ops(ops); 1276 rbd_destroy_ops(ops);
1276 return ret; 1277 return ret;
@@ -1279,64 +1280,65 @@ fail:
1279/* 1280/*
1280 * Request sync osd unwatch 1281 * Request sync osd unwatch
1281 */ 1282 */
1282static int rbd_req_sync_unwatch(struct rbd_device *dev, 1283static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
1283 const char *obj)
1284{ 1284{
1285 struct ceph_osd_req_op *ops; 1285 struct ceph_osd_req_op *ops;
1286 int ret;
1286 1287
1287 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_WATCH, 0); 1288 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1288 if (ret < 0) 1289 if (!ops)
1289 return ret; 1290 return -ENOMEM;
1290 1291
1291 ops[0].watch.ver = 0; 1292 ops[0].watch.ver = 0;
1292 ops[0].watch.cookie = cpu_to_le64(dev->watch_event->cookie); 1293 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
1293 ops[0].watch.flag = 0; 1294 ops[0].watch.flag = 0;
1294 1295
1295 ret = rbd_req_sync_op(dev, NULL, 1296 ret = rbd_req_sync_op(rbd_dev, NULL,
1296 CEPH_NOSNAP, 1297 CEPH_NOSNAP,
1297 0,
1298 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, 1298 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1299 ops, 1299 ops,
1300 1, obj, 0, 0, NULL, NULL, NULL); 1300 rbd_dev->header_name,
1301 0, 0, NULL, NULL, NULL);
1302
1301 1303
1302 rbd_destroy_ops(ops); 1304 rbd_destroy_ops(ops);
1303 ceph_osdc_cancel_event(dev->watch_event); 1305 ceph_osdc_cancel_event(rbd_dev->watch_event);
1304 dev->watch_event = NULL; 1306 rbd_dev->watch_event = NULL;
1305 return ret; 1307 return ret;
1306} 1308}
1307 1309
1308struct rbd_notify_info { 1310struct rbd_notify_info {
1309 struct rbd_device *dev; 1311 struct rbd_device *rbd_dev;
1310}; 1312};
1311 1313
1312static void rbd_notify_cb(u64 ver, u64 notify_id, u8 opcode, void *data) 1314static void rbd_notify_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1313{ 1315{
1314 struct rbd_device *dev = (struct rbd_device *)data; 1316 struct rbd_device *rbd_dev = (struct rbd_device *)data;
1315 if (!dev) 1317 if (!rbd_dev)
1316 return; 1318 return;
1317 1319
1318 dout("rbd_notify_cb %s notify_id=%lld opcode=%d\n", dev->obj_md_name, 1320 dout("rbd_notify_cb %s notify_id=%llu opcode=%u\n",
1319 notify_id, (int)opcode); 1321 rbd_dev->header_name, (unsigned long long) notify_id,
1322 (unsigned int) opcode);
1320} 1323}
1321 1324
1322/* 1325/*
1323 * Request sync osd notify 1326 * Request sync osd notify
1324 */ 1327 */
1325static int rbd_req_sync_notify(struct rbd_device *dev, 1328static int rbd_req_sync_notify(struct rbd_device *rbd_dev)
1326 const char *obj)
1327{ 1329{
1328 struct ceph_osd_req_op *ops; 1330 struct ceph_osd_req_op *ops;
1329 struct ceph_osd_client *osdc = &dev->rbd_client->client->osdc; 1331 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1330 struct ceph_osd_event *event; 1332 struct ceph_osd_event *event;
1331 struct rbd_notify_info info; 1333 struct rbd_notify_info info;
1332 int payload_len = sizeof(u32) + sizeof(u32); 1334 int payload_len = sizeof(u32) + sizeof(u32);
1333 int ret; 1335 int ret;
1334 1336
1335 ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_NOTIFY, payload_len); 1337 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY, payload_len);
1336 if (ret < 0) 1338 if (!ops)
1337 return ret; 1339 return -ENOMEM;
1338 1340
1339 info.dev = dev; 1341 info.rbd_dev = rbd_dev;
1340 1342
1341 ret = ceph_osdc_create_event(osdc, rbd_notify_cb, 1, 1343 ret = ceph_osdc_create_event(osdc, rbd_notify_cb, 1,
1342 (void *)&info, &event); 1344 (void *)&info, &event);
@@ -1349,12 +1351,12 @@ static int rbd_req_sync_notify(struct rbd_device *dev,
1349 ops[0].watch.prot_ver = RADOS_NOTIFY_VER; 1351 ops[0].watch.prot_ver = RADOS_NOTIFY_VER;
1350 ops[0].watch.timeout = 12; 1352 ops[0].watch.timeout = 12;
1351 1353
1352 ret = rbd_req_sync_op(dev, NULL, 1354 ret = rbd_req_sync_op(rbd_dev, NULL,
1353 CEPH_NOSNAP, 1355 CEPH_NOSNAP,
1354 0,
1355 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, 1356 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1356 ops, 1357 ops,
1357 1, obj, 0, 0, NULL, NULL, NULL); 1358 rbd_dev->header_name,
1359 0, 0, NULL, NULL, NULL);
1358 if (ret < 0) 1360 if (ret < 0)
1359 goto fail_event; 1361 goto fail_event;
1360 1362
@@ -1373,36 +1375,37 @@ fail:
1373/* 1375/*
1374 * Request sync osd read 1376 * Request sync osd read
1375 */ 1377 */
1376static int rbd_req_sync_exec(struct rbd_device *dev, 1378static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
1377 const char *obj, 1379 const char *object_name,
1378 const char *cls, 1380 const char *class_name,
1379 const char *method, 1381 const char *method_name,
1380 const char *data, 1382 const char *data,
1381 int len, 1383 int len,
1382 u64 *ver) 1384 u64 *ver)
1383{ 1385{
1384 struct ceph_osd_req_op *ops; 1386 struct ceph_osd_req_op *ops;
1385 int cls_len = strlen(cls); 1387 int class_name_len = strlen(class_name);
1386 int method_len = strlen(method); 1388 int method_name_len = strlen(method_name);
1387 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_CALL, 1389 int ret;
1388 cls_len + method_len + len);
1389 if (ret < 0)
1390 return ret;
1391 1390
1392 ops[0].cls.class_name = cls; 1391 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL,
1393 ops[0].cls.class_len = (__u8)cls_len; 1392 class_name_len + method_name_len + len);
1394 ops[0].cls.method_name = method; 1393 if (!ops)
1395 ops[0].cls.method_len = (__u8)method_len; 1394 return -ENOMEM;
1395
1396 ops[0].cls.class_name = class_name;
1397 ops[0].cls.class_len = (__u8) class_name_len;
1398 ops[0].cls.method_name = method_name;
1399 ops[0].cls.method_len = (__u8) method_name_len;
1396 ops[0].cls.argc = 0; 1400 ops[0].cls.argc = 0;
1397 ops[0].cls.indata = data; 1401 ops[0].cls.indata = data;
1398 ops[0].cls.indata_len = len; 1402 ops[0].cls.indata_len = len;
1399 1403
1400 ret = rbd_req_sync_op(dev, NULL, 1404 ret = rbd_req_sync_op(rbd_dev, NULL,
1401 CEPH_NOSNAP, 1405 CEPH_NOSNAP,
1402 0,
1403 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, 1406 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1404 ops, 1407 ops,
1405 1, obj, 0, 0, NULL, NULL, ver); 1408 object_name, 0, 0, NULL, NULL, ver);
1406 1409
1407 rbd_destroy_ops(ops); 1410 rbd_destroy_ops(ops);
1408 1411
@@ -1437,10 +1440,12 @@ static void rbd_rq_fn(struct request_queue *q)
1437 struct bio *bio; 1440 struct bio *bio;
1438 struct bio *rq_bio, *next_bio = NULL; 1441 struct bio *rq_bio, *next_bio = NULL;
1439 bool do_write; 1442 bool do_write;
1440 int size, op_size = 0; 1443 unsigned int size;
1444 u64 op_size = 0;
1441 u64 ofs; 1445 u64 ofs;
1442 int num_segs, cur_seg = 0; 1446 int num_segs, cur_seg = 0;
1443 struct rbd_req_coll *coll; 1447 struct rbd_req_coll *coll;
1448 struct ceph_snap_context *snapc;
1444 1449
1445 /* peek at request from block layer */ 1450 /* peek at request from block layer */
1446 if (!rq) 1451 if (!rq)
@@ -1467,23 +1472,38 @@ static void rbd_rq_fn(struct request_queue *q)
1467 1472
1468 spin_unlock_irq(q->queue_lock); 1473 spin_unlock_irq(q->queue_lock);
1469 1474
1475 down_read(&rbd_dev->header_rwsem);
1476
1477 if (rbd_dev->snap_id != CEPH_NOSNAP && !rbd_dev->snap_exists) {
1478 up_read(&rbd_dev->header_rwsem);
1479 dout("request for non-existent snapshot");
1480 spin_lock_irq(q->queue_lock);
1481 __blk_end_request_all(rq, -ENXIO);
1482 continue;
1483 }
1484
1485 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1486
1487 up_read(&rbd_dev->header_rwsem);
1488
1470 dout("%s 0x%x bytes at 0x%llx\n", 1489 dout("%s 0x%x bytes at 0x%llx\n",
1471 do_write ? "write" : "read", 1490 do_write ? "write" : "read",
1472 size, blk_rq_pos(rq) * SECTOR_SIZE); 1491 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
1473 1492
1474 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size); 1493 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1475 coll = rbd_alloc_coll(num_segs); 1494 coll = rbd_alloc_coll(num_segs);
1476 if (!coll) { 1495 if (!coll) {
1477 spin_lock_irq(q->queue_lock); 1496 spin_lock_irq(q->queue_lock);
1478 __blk_end_request_all(rq, -ENOMEM); 1497 __blk_end_request_all(rq, -ENOMEM);
1498 ceph_put_snap_context(snapc);
1479 continue; 1499 continue;
1480 } 1500 }
1481 1501
1482 do { 1502 do {
1483 /* a bio clone to be passed down to OSD req */ 1503 /* a bio clone to be passed down to OSD req */
1484 dout("rq->bio->bi_vcnt=%d\n", rq->bio->bi_vcnt); 1504 dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt);
1485 op_size = rbd_get_segment(&rbd_dev->header, 1505 op_size = rbd_get_segment(&rbd_dev->header,
1486 rbd_dev->header.block_name, 1506 rbd_dev->header.object_prefix,
1487 ofs, size, 1507 ofs, size,
1488 NULL, NULL); 1508 NULL, NULL);
1489 kref_get(&coll->kref); 1509 kref_get(&coll->kref);
@@ -1499,7 +1519,7 @@ static void rbd_rq_fn(struct request_queue *q)
1499 /* init OSD command: write or read */ 1519 /* init OSD command: write or read */
1500 if (do_write) 1520 if (do_write)
1501 rbd_req_write(rq, rbd_dev, 1521 rbd_req_write(rq, rbd_dev,
1502 rbd_dev->header.snapc, 1522 snapc,
1503 ofs, 1523 ofs,
1504 op_size, bio, 1524 op_size, bio,
1505 coll, cur_seg); 1525 coll, cur_seg);
@@ -1522,6 +1542,8 @@ next_seg:
1522 if (bp) 1542 if (bp)
1523 bio_pair_release(bp); 1543 bio_pair_release(bp);
1524 spin_lock_irq(q->queue_lock); 1544 spin_lock_irq(q->queue_lock);
1545
1546 ceph_put_snap_context(snapc);
1525 } 1547 }
1526} 1548}
1527 1549
@@ -1592,18 +1614,19 @@ static int rbd_read_header(struct rbd_device *rbd_dev,
1592 return -ENOMEM; 1614 return -ENOMEM;
1593 1615
1594 rc = rbd_req_sync_read(rbd_dev, 1616 rc = rbd_req_sync_read(rbd_dev,
1595 NULL, CEPH_NOSNAP, 1617 CEPH_NOSNAP,
1596 rbd_dev->obj_md_name, 1618 rbd_dev->header_name,
1597 0, len, 1619 0, len,
1598 (char *)dh, &ver); 1620 (char *)dh, &ver);
1599 if (rc < 0) 1621 if (rc < 0)
1600 goto out_dh; 1622 goto out_dh;
1601 1623
1602 rc = rbd_header_from_disk(header, dh, snap_count, GFP_KERNEL); 1624 rc = rbd_header_from_disk(header, dh, snap_count);
1603 if (rc < 0) { 1625 if (rc < 0) {
1604 if (rc == -ENXIO) 1626 if (rc == -ENXIO)
1605 pr_warning("unrecognized header format" 1627 pr_warning("unrecognized header format"
1606 " for image %s", rbd_dev->obj); 1628 " for image %s\n",
1629 rbd_dev->image_name);
1607 goto out_dh; 1630 goto out_dh;
1608 } 1631 }
1609 1632
@@ -1628,7 +1651,7 @@ out_dh:
1628/* 1651/*
1629 * create a snapshot 1652 * create a snapshot
1630 */ 1653 */
1631static int rbd_header_add_snap(struct rbd_device *dev, 1654static int rbd_header_add_snap(struct rbd_device *rbd_dev,
1632 const char *snap_name, 1655 const char *snap_name,
1633 gfp_t gfp_flags) 1656 gfp_t gfp_flags)
1634{ 1657{
@@ -1636,16 +1659,15 @@ static int rbd_header_add_snap(struct rbd_device *dev,
1636 u64 new_snapid; 1659 u64 new_snapid;
1637 int ret; 1660 int ret;
1638 void *data, *p, *e; 1661 void *data, *p, *e;
1639 u64 ver;
1640 struct ceph_mon_client *monc; 1662 struct ceph_mon_client *monc;
1641 1663
1642 /* we should create a snapshot only if we're pointing at the head */ 1664 /* we should create a snapshot only if we're pointing at the head */
1643 if (dev->snap_id != CEPH_NOSNAP) 1665 if (rbd_dev->snap_id != CEPH_NOSNAP)
1644 return -EINVAL; 1666 return -EINVAL;
1645 1667
1646 monc = &dev->rbd_client->client->monc; 1668 monc = &rbd_dev->rbd_client->client->monc;
1647 ret = ceph_monc_create_snapid(monc, dev->poolid, &new_snapid); 1669 ret = ceph_monc_create_snapid(monc, rbd_dev->pool_id, &new_snapid);
1648 dout("created snapid=%lld\n", new_snapid); 1670 dout("created snapid=%llu\n", (unsigned long long) new_snapid);
1649 if (ret < 0) 1671 if (ret < 0)
1650 return ret; 1672 return ret;
1651 1673
@@ -1659,19 +1681,13 @@ static int rbd_header_add_snap(struct rbd_device *dev,
1659 ceph_encode_string_safe(&p, e, snap_name, name_len, bad); 1681 ceph_encode_string_safe(&p, e, snap_name, name_len, bad);
1660 ceph_encode_64_safe(&p, e, new_snapid, bad); 1682 ceph_encode_64_safe(&p, e, new_snapid, bad);
1661 1683
1662 ret = rbd_req_sync_exec(dev, dev->obj_md_name, "rbd", "snap_add", 1684 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
1663 data, p - data, &ver); 1685 "rbd", "snap_add",
1686 data, p - data, NULL);
1664 1687
1665 kfree(data); 1688 kfree(data);
1666 1689
1667 if (ret < 0) 1690 return ret < 0 ? ret : 0;
1668 return ret;
1669
1670 down_write(&dev->header_rwsem);
1671 dev->header.snapc->seq = new_snapid;
1672 up_write(&dev->header_rwsem);
1673
1674 return 0;
1675bad: 1691bad:
1676 return -ERANGE; 1692 return -ERANGE;
1677} 1693}
@@ -1679,52 +1695,52 @@ bad:
1679static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev) 1695static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev)
1680{ 1696{
1681 struct rbd_snap *snap; 1697 struct rbd_snap *snap;
1698 struct rbd_snap *next;
1682 1699
1683 while (!list_empty(&rbd_dev->snaps)) { 1700 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
1684 snap = list_first_entry(&rbd_dev->snaps, struct rbd_snap, node); 1701 __rbd_remove_snap_dev(snap);
1685 __rbd_remove_snap_dev(rbd_dev, snap);
1686 }
1687} 1702}
1688 1703
1689/* 1704/*
1690 * only read the first part of the ondisk header, without the snaps info 1705 * only read the first part of the ondisk header, without the snaps info
1691 */ 1706 */
1692static int __rbd_refresh_header(struct rbd_device *rbd_dev) 1707static int __rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver)
1693{ 1708{
1694 int ret; 1709 int ret;
1695 struct rbd_image_header h; 1710 struct rbd_image_header h;
1696 u64 snap_seq;
1697 int follow_seq = 0;
1698 1711
1699 ret = rbd_read_header(rbd_dev, &h); 1712 ret = rbd_read_header(rbd_dev, &h);
1700 if (ret < 0) 1713 if (ret < 0)
1701 return ret; 1714 return ret;
1702 1715
1703 /* resized? */
1704 set_capacity(rbd_dev->disk, h.image_size / SECTOR_SIZE);
1705
1706 down_write(&rbd_dev->header_rwsem); 1716 down_write(&rbd_dev->header_rwsem);
1707 1717
1708 snap_seq = rbd_dev->header.snapc->seq; 1718 /* resized? */
1709 if (rbd_dev->header.total_snaps && 1719 if (rbd_dev->snap_id == CEPH_NOSNAP) {
1710 rbd_dev->header.snapc->snaps[0] == snap_seq) 1720 sector_t size = (sector_t) h.image_size / SECTOR_SIZE;
1711 /* pointing at the head, will need to follow that
1712 if head moves */
1713 follow_seq = 1;
1714 1721
1715 kfree(rbd_dev->header.snapc); 1722 dout("setting size to %llu sectors", (unsigned long long) size);
1716 kfree(rbd_dev->header.snap_names); 1723 set_capacity(rbd_dev->disk, size);
1724 }
1725
1726 /* rbd_dev->header.object_prefix shouldn't change */
1717 kfree(rbd_dev->header.snap_sizes); 1727 kfree(rbd_dev->header.snap_sizes);
1728 kfree(rbd_dev->header.snap_names);
1729 /* osd requests may still refer to snapc */
1730 ceph_put_snap_context(rbd_dev->header.snapc);
1718 1731
1732 if (hver)
1733 *hver = h.obj_version;
1734 rbd_dev->header.obj_version = h.obj_version;
1735 rbd_dev->header.image_size = h.image_size;
1719 rbd_dev->header.total_snaps = h.total_snaps; 1736 rbd_dev->header.total_snaps = h.total_snaps;
1720 rbd_dev->header.snapc = h.snapc; 1737 rbd_dev->header.snapc = h.snapc;
1721 rbd_dev->header.snap_names = h.snap_names; 1738 rbd_dev->header.snap_names = h.snap_names;
1722 rbd_dev->header.snap_names_len = h.snap_names_len; 1739 rbd_dev->header.snap_names_len = h.snap_names_len;
1723 rbd_dev->header.snap_sizes = h.snap_sizes; 1740 rbd_dev->header.snap_sizes = h.snap_sizes;
1724 if (follow_seq) 1741 /* Free the extra copy of the object prefix */
1725 rbd_dev->header.snapc->seq = rbd_dev->header.snapc->snaps[0]; 1742 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1726 else 1743 kfree(h.object_prefix);
1727 rbd_dev->header.snapc->seq = snap_seq;
1728 1744
1729 ret = __rbd_init_snaps_header(rbd_dev); 1745 ret = __rbd_init_snaps_header(rbd_dev);
1730 1746
@@ -1733,6 +1749,17 @@ static int __rbd_refresh_header(struct rbd_device *rbd_dev)
1733 return ret; 1749 return ret;
1734} 1750}
1735 1751
1752static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver)
1753{
1754 int ret;
1755
1756 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1757 ret = __rbd_refresh_header(rbd_dev, hver);
1758 mutex_unlock(&ctl_mutex);
1759
1760 return ret;
1761}
1762
1736static int rbd_init_disk(struct rbd_device *rbd_dev) 1763static int rbd_init_disk(struct rbd_device *rbd_dev)
1737{ 1764{
1738 struct gendisk *disk; 1765 struct gendisk *disk;
@@ -1762,7 +1789,7 @@ static int rbd_init_disk(struct rbd_device *rbd_dev)
1762 goto out; 1789 goto out;
1763 1790
1764 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", 1791 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
1765 rbd_dev->id); 1792 rbd_dev->dev_id);
1766 disk->major = rbd_dev->major; 1793 disk->major = rbd_dev->major;
1767 disk->first_minor = 0; 1794 disk->first_minor = 0;
1768 disk->fops = &rbd_bd_ops; 1795 disk->fops = &rbd_bd_ops;
@@ -1819,8 +1846,13 @@ static ssize_t rbd_size_show(struct device *dev,
1819 struct device_attribute *attr, char *buf) 1846 struct device_attribute *attr, char *buf)
1820{ 1847{
1821 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); 1848 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1849 sector_t size;
1850
1851 down_read(&rbd_dev->header_rwsem);
1852 size = get_capacity(rbd_dev->disk);
1853 up_read(&rbd_dev->header_rwsem);
1822 1854
1823 return sprintf(buf, "%llu\n", (unsigned long long)rbd_dev->header.image_size); 1855 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
1824} 1856}
1825 1857
1826static ssize_t rbd_major_show(struct device *dev, 1858static ssize_t rbd_major_show(struct device *dev,
@@ -1848,12 +1880,20 @@ static ssize_t rbd_pool_show(struct device *dev,
1848 return sprintf(buf, "%s\n", rbd_dev->pool_name); 1880 return sprintf(buf, "%s\n", rbd_dev->pool_name);
1849} 1881}
1850 1882
1883static ssize_t rbd_pool_id_show(struct device *dev,
1884 struct device_attribute *attr, char *buf)
1885{
1886 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1887
1888 return sprintf(buf, "%d\n", rbd_dev->pool_id);
1889}
1890
1851static ssize_t rbd_name_show(struct device *dev, 1891static ssize_t rbd_name_show(struct device *dev,
1852 struct device_attribute *attr, char *buf) 1892 struct device_attribute *attr, char *buf)
1853{ 1893{
1854 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); 1894 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1855 1895
1856 return sprintf(buf, "%s\n", rbd_dev->obj); 1896 return sprintf(buf, "%s\n", rbd_dev->image_name);
1857} 1897}
1858 1898
1859static ssize_t rbd_snap_show(struct device *dev, 1899static ssize_t rbd_snap_show(struct device *dev,
@@ -1871,23 +1911,18 @@ static ssize_t rbd_image_refresh(struct device *dev,
1871 size_t size) 1911 size_t size)
1872{ 1912{
1873 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); 1913 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1874 int rc; 1914 int ret;
1875 int ret = size;
1876
1877 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1878 1915
1879 rc = __rbd_refresh_header(rbd_dev); 1916 ret = rbd_refresh_header(rbd_dev, NULL);
1880 if (rc < 0)
1881 ret = rc;
1882 1917
1883 mutex_unlock(&ctl_mutex); 1918 return ret < 0 ? ret : size;
1884 return ret;
1885} 1919}
1886 1920
1887static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); 1921static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
1888static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); 1922static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
1889static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); 1923static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
1890static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); 1924static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
1925static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
1891static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); 1926static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
1892static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); 1927static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
1893static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); 1928static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
@@ -1898,6 +1933,7 @@ static struct attribute *rbd_attrs[] = {
1898 &dev_attr_major.attr, 1933 &dev_attr_major.attr,
1899 &dev_attr_client_id.attr, 1934 &dev_attr_client_id.attr,
1900 &dev_attr_pool.attr, 1935 &dev_attr_pool.attr,
1936 &dev_attr_pool_id.attr,
1901 &dev_attr_name.attr, 1937 &dev_attr_name.attr,
1902 &dev_attr_current_snap.attr, 1938 &dev_attr_current_snap.attr,
1903 &dev_attr_refresh.attr, 1939 &dev_attr_refresh.attr,
@@ -1977,15 +2013,13 @@ static struct device_type rbd_snap_device_type = {
1977 .release = rbd_snap_dev_release, 2013 .release = rbd_snap_dev_release,
1978}; 2014};
1979 2015
1980static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev, 2016static void __rbd_remove_snap_dev(struct rbd_snap *snap)
1981 struct rbd_snap *snap)
1982{ 2017{
1983 list_del(&snap->node); 2018 list_del(&snap->node);
1984 device_unregister(&snap->dev); 2019 device_unregister(&snap->dev);
1985} 2020}
1986 2021
1987static int rbd_register_snap_dev(struct rbd_device *rbd_dev, 2022static int rbd_register_snap_dev(struct rbd_snap *snap,
1988 struct rbd_snap *snap,
1989 struct device *parent) 2023 struct device *parent)
1990{ 2024{
1991 struct device *dev = &snap->dev; 2025 struct device *dev = &snap->dev;
@@ -2000,29 +2034,36 @@ static int rbd_register_snap_dev(struct rbd_device *rbd_dev,
2000 return ret; 2034 return ret;
2001} 2035}
2002 2036
2003static int __rbd_add_snap_dev(struct rbd_device *rbd_dev, 2037static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
2004 int i, const char *name, 2038 int i, const char *name)
2005 struct rbd_snap **snapp)
2006{ 2039{
2040 struct rbd_snap *snap;
2007 int ret; 2041 int ret;
2008 struct rbd_snap *snap = kzalloc(sizeof(*snap), GFP_KERNEL); 2042
2043 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
2009 if (!snap) 2044 if (!snap)
2010 return -ENOMEM; 2045 return ERR_PTR(-ENOMEM);
2046
2047 ret = -ENOMEM;
2011 snap->name = kstrdup(name, GFP_KERNEL); 2048 snap->name = kstrdup(name, GFP_KERNEL);
2049 if (!snap->name)
2050 goto err;
2051
2012 snap->size = rbd_dev->header.snap_sizes[i]; 2052 snap->size = rbd_dev->header.snap_sizes[i];
2013 snap->id = rbd_dev->header.snapc->snaps[i]; 2053 snap->id = rbd_dev->header.snapc->snaps[i];
2014 if (device_is_registered(&rbd_dev->dev)) { 2054 if (device_is_registered(&rbd_dev->dev)) {
2015 ret = rbd_register_snap_dev(rbd_dev, snap, 2055 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
2016 &rbd_dev->dev);
2017 if (ret < 0) 2056 if (ret < 0)
2018 goto err; 2057 goto err;
2019 } 2058 }
2020 *snapp = snap; 2059
2021 return 0; 2060 return snap;
2061
2022err: 2062err:
2023 kfree(snap->name); 2063 kfree(snap->name);
2024 kfree(snap); 2064 kfree(snap);
2025 return ret; 2065
2066 return ERR_PTR(ret);
2026} 2067}
2027 2068
2028/* 2069/*
@@ -2055,7 +2096,6 @@ static int __rbd_init_snaps_header(struct rbd_device *rbd_dev)
2055 const char *name, *first_name; 2096 const char *name, *first_name;
2056 int i = rbd_dev->header.total_snaps; 2097 int i = rbd_dev->header.total_snaps;
2057 struct rbd_snap *snap, *old_snap = NULL; 2098 struct rbd_snap *snap, *old_snap = NULL;
2058 int ret;
2059 struct list_head *p, *n; 2099 struct list_head *p, *n;
2060 2100
2061 first_name = rbd_dev->header.snap_names; 2101 first_name = rbd_dev->header.snap_names;
@@ -2070,8 +2110,15 @@ static int __rbd_init_snaps_header(struct rbd_device *rbd_dev)
2070 cur_id = rbd_dev->header.snapc->snaps[i - 1]; 2110 cur_id = rbd_dev->header.snapc->snaps[i - 1];
2071 2111
2072 if (!i || old_snap->id < cur_id) { 2112 if (!i || old_snap->id < cur_id) {
2073 /* old_snap->id was skipped, thus was removed */ 2113 /*
2074 __rbd_remove_snap_dev(rbd_dev, old_snap); 2114 * old_snap->id was skipped, thus was
2115 * removed. If this rbd_dev is mapped to
2116 * the removed snapshot, record that it no
2117 * longer exists, to prevent further I/O.
2118 */
2119 if (rbd_dev->snap_id == old_snap->id)
2120 rbd_dev->snap_exists = false;
2121 __rbd_remove_snap_dev(old_snap);
2075 continue; 2122 continue;
2076 } 2123 }
2077 if (old_snap->id == cur_id) { 2124 if (old_snap->id == cur_id) {
@@ -2091,9 +2138,9 @@ static int __rbd_init_snaps_header(struct rbd_device *rbd_dev)
2091 if (cur_id >= old_snap->id) 2138 if (cur_id >= old_snap->id)
2092 break; 2139 break;
2093 /* a new snapshot */ 2140 /* a new snapshot */
2094 ret = __rbd_add_snap_dev(rbd_dev, i - 1, name, &snap); 2141 snap = __rbd_add_snap_dev(rbd_dev, i - 1, name);
2095 if (ret < 0) 2142 if (IS_ERR(snap))
2096 return ret; 2143 return PTR_ERR(snap);
2097 2144
2098 /* note that we add it backward so using n and not p */ 2145 /* note that we add it backward so using n and not p */
2099 list_add(&snap->node, n); 2146 list_add(&snap->node, n);
@@ -2107,9 +2154,9 @@ static int __rbd_init_snaps_header(struct rbd_device *rbd_dev)
2107 WARN_ON(1); 2154 WARN_ON(1);
2108 return -EINVAL; 2155 return -EINVAL;
2109 } 2156 }
2110 ret = __rbd_add_snap_dev(rbd_dev, i - 1, name, &snap); 2157 snap = __rbd_add_snap_dev(rbd_dev, i - 1, name);
2111 if (ret < 0) 2158 if (IS_ERR(snap))
2112 return ret; 2159 return PTR_ERR(snap);
2113 list_add(&snap->node, &rbd_dev->snaps); 2160 list_add(&snap->node, &rbd_dev->snaps);
2114 } 2161 }
2115 2162
@@ -2129,14 +2176,13 @@ static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2129 dev->type = &rbd_device_type; 2176 dev->type = &rbd_device_type;
2130 dev->parent = &rbd_root_dev; 2177 dev->parent = &rbd_root_dev;
2131 dev->release = rbd_dev_release; 2178 dev->release = rbd_dev_release;
2132 dev_set_name(dev, "%d", rbd_dev->id); 2179 dev_set_name(dev, "%d", rbd_dev->dev_id);
2133 ret = device_register(dev); 2180 ret = device_register(dev);
2134 if (ret < 0) 2181 if (ret < 0)
2135 goto out; 2182 goto out;
2136 2183
2137 list_for_each_entry(snap, &rbd_dev->snaps, node) { 2184 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2138 ret = rbd_register_snap_dev(rbd_dev, snap, 2185 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
2139 &rbd_dev->dev);
2140 if (ret < 0) 2186 if (ret < 0)
2141 break; 2187 break;
2142 } 2188 }
@@ -2155,12 +2201,9 @@ static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
2155 int ret, rc; 2201 int ret, rc;
2156 2202
2157 do { 2203 do {
2158 ret = rbd_req_sync_watch(rbd_dev, rbd_dev->obj_md_name, 2204 ret = rbd_req_sync_watch(rbd_dev);
2159 rbd_dev->header.obj_version);
2160 if (ret == -ERANGE) { 2205 if (ret == -ERANGE) {
2161 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); 2206 rc = rbd_refresh_header(rbd_dev, NULL);
2162 rc = __rbd_refresh_header(rbd_dev);
2163 mutex_unlock(&ctl_mutex);
2164 if (rc < 0) 2207 if (rc < 0)
2165 return rc; 2208 return rc;
2166 } 2209 }
@@ -2177,7 +2220,7 @@ static atomic64_t rbd_id_max = ATOMIC64_INIT(0);
2177 */ 2220 */
2178static void rbd_id_get(struct rbd_device *rbd_dev) 2221static void rbd_id_get(struct rbd_device *rbd_dev)
2179{ 2222{
2180 rbd_dev->id = atomic64_inc_return(&rbd_id_max); 2223 rbd_dev->dev_id = atomic64_inc_return(&rbd_id_max);
2181 2224
2182 spin_lock(&rbd_dev_list_lock); 2225 spin_lock(&rbd_dev_list_lock);
2183 list_add_tail(&rbd_dev->node, &rbd_dev_list); 2226 list_add_tail(&rbd_dev->node, &rbd_dev_list);
@@ -2191,7 +2234,7 @@ static void rbd_id_get(struct rbd_device *rbd_dev)
2191static void rbd_id_put(struct rbd_device *rbd_dev) 2234static void rbd_id_put(struct rbd_device *rbd_dev)
2192{ 2235{
2193 struct list_head *tmp; 2236 struct list_head *tmp;
2194 int rbd_id = rbd_dev->id; 2237 int rbd_id = rbd_dev->dev_id;
2195 int max_id; 2238 int max_id;
2196 2239
2197 BUG_ON(rbd_id < 1); 2240 BUG_ON(rbd_id < 1);
@@ -2282,19 +2325,58 @@ static inline size_t copy_token(const char **buf,
2282} 2325}
2283 2326
2284/* 2327/*
2285 * This fills in the pool_name, obj, obj_len, snap_name, obj_len, 2328 * Finds the next token in *buf, dynamically allocates a buffer big
2329 * enough to hold a copy of it, and copies the token into the new
2330 * buffer. The copy is guaranteed to be terminated with '\0'. Note
2331 * that a duplicate buffer is created even for a zero-length token.
2332 *
2333 * Returns a pointer to the newly-allocated duplicate, or a null
2334 * pointer if memory for the duplicate was not available. If
2335 * the lenp argument is a non-null pointer, the length of the token
2336 * (not including the '\0') is returned in *lenp.
2337 *
2338 * If successful, the *buf pointer will be updated to point beyond
2339 * the end of the found token.
2340 *
2341 * Note: uses GFP_KERNEL for allocation.
2342 */
2343static inline char *dup_token(const char **buf, size_t *lenp)
2344{
2345 char *dup;
2346 size_t len;
2347
2348 len = next_token(buf);
2349 dup = kmalloc(len + 1, GFP_KERNEL);
2350 if (!dup)
2351 return NULL;
2352
2353 memcpy(dup, *buf, len);
2354 *(dup + len) = '\0';
2355 *buf += len;
2356
2357 if (lenp)
2358 *lenp = len;
2359
2360 return dup;
2361}
2362
2363/*
2364 * This fills in the pool_name, image_name, image_name_len, snap_name,
2286 * rbd_dev, rbd_md_name, and name fields of the given rbd_dev, based 2365 * rbd_dev, rbd_md_name, and name fields of the given rbd_dev, based
2287 * on the list of monitor addresses and other options provided via 2366 * on the list of monitor addresses and other options provided via
2288 * /sys/bus/rbd/add. 2367 * /sys/bus/rbd/add.
2368 *
2369 * Note: rbd_dev is assumed to have been initially zero-filled.
2289 */ 2370 */
2290static int rbd_add_parse_args(struct rbd_device *rbd_dev, 2371static int rbd_add_parse_args(struct rbd_device *rbd_dev,
2291 const char *buf, 2372 const char *buf,
2292 const char **mon_addrs, 2373 const char **mon_addrs,
2293 size_t *mon_addrs_size, 2374 size_t *mon_addrs_size,
2294 char *options, 2375 char *options,
2295 size_t options_size) 2376 size_t options_size)
2296{ 2377{
2297 size_t len; 2378 size_t len;
2379 int ret;
2298 2380
2299 /* The first four tokens are required */ 2381 /* The first four tokens are required */
2300 2382
@@ -2310,56 +2392,74 @@ static int rbd_add_parse_args(struct rbd_device *rbd_dev,
2310 if (!len || len >= options_size) 2392 if (!len || len >= options_size)
2311 return -EINVAL; 2393 return -EINVAL;
2312 2394
2313 len = copy_token(&buf, rbd_dev->pool_name, sizeof (rbd_dev->pool_name)); 2395 ret = -ENOMEM;
2314 if (!len || len >= sizeof (rbd_dev->pool_name)) 2396 rbd_dev->pool_name = dup_token(&buf, NULL);
2315 return -EINVAL; 2397 if (!rbd_dev->pool_name)
2316 2398 goto out_err;
2317 len = copy_token(&buf, rbd_dev->obj, sizeof (rbd_dev->obj));
2318 if (!len || len >= sizeof (rbd_dev->obj))
2319 return -EINVAL;
2320 2399
2321 /* We have the object length in hand, save it. */ 2400 rbd_dev->image_name = dup_token(&buf, &rbd_dev->image_name_len);
2401 if (!rbd_dev->image_name)
2402 goto out_err;
2322 2403
2323 rbd_dev->obj_len = len; 2404 /* Create the name of the header object */
2324 2405
2325 BUILD_BUG_ON(RBD_MAX_MD_NAME_LEN 2406 rbd_dev->header_name = kmalloc(rbd_dev->image_name_len
2326 < RBD_MAX_OBJ_NAME_LEN + sizeof (RBD_SUFFIX)); 2407 + sizeof (RBD_SUFFIX),
2327 sprintf(rbd_dev->obj_md_name, "%s%s", rbd_dev->obj, RBD_SUFFIX); 2408 GFP_KERNEL);
2409 if (!rbd_dev->header_name)
2410 goto out_err;
2411 sprintf(rbd_dev->header_name, "%s%s", rbd_dev->image_name, RBD_SUFFIX);
2328 2412
2329 /* 2413 /*
2330 * The snapshot name is optional, but it's an error if it's 2414 * The snapshot name is optional. If none is is supplied,
2331 * too long. If no snapshot is supplied, fill in the default. 2415 * we use the default value.
2332 */ 2416 */
2333 len = copy_token(&buf, rbd_dev->snap_name, sizeof (rbd_dev->snap_name)); 2417 rbd_dev->snap_name = dup_token(&buf, &len);
2334 if (!len) 2418 if (!rbd_dev->snap_name)
2419 goto out_err;
2420 if (!len) {
2421 /* Replace the empty name with the default */
2422 kfree(rbd_dev->snap_name);
2423 rbd_dev->snap_name
2424 = kmalloc(sizeof (RBD_SNAP_HEAD_NAME), GFP_KERNEL);
2425 if (!rbd_dev->snap_name)
2426 goto out_err;
2427
2335 memcpy(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME, 2428 memcpy(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME,
2336 sizeof (RBD_SNAP_HEAD_NAME)); 2429 sizeof (RBD_SNAP_HEAD_NAME));
2337 else if (len >= sizeof (rbd_dev->snap_name)) 2430 }
2338 return -EINVAL;
2339 2431
2340 return 0; 2432 return 0;
2433
2434out_err:
2435 kfree(rbd_dev->header_name);
2436 kfree(rbd_dev->image_name);
2437 kfree(rbd_dev->pool_name);
2438 rbd_dev->pool_name = NULL;
2439
2440 return ret;
2341} 2441}
2342 2442
2343static ssize_t rbd_add(struct bus_type *bus, 2443static ssize_t rbd_add(struct bus_type *bus,
2344 const char *buf, 2444 const char *buf,
2345 size_t count) 2445 size_t count)
2346{ 2446{
2347 struct rbd_device *rbd_dev; 2447 char *options;
2448 struct rbd_device *rbd_dev = NULL;
2348 const char *mon_addrs = NULL; 2449 const char *mon_addrs = NULL;
2349 size_t mon_addrs_size = 0; 2450 size_t mon_addrs_size = 0;
2350 char *options = NULL;
2351 struct ceph_osd_client *osdc; 2451 struct ceph_osd_client *osdc;
2352 int rc = -ENOMEM; 2452 int rc = -ENOMEM;
2353 2453
2354 if (!try_module_get(THIS_MODULE)) 2454 if (!try_module_get(THIS_MODULE))
2355 return -ENODEV; 2455 return -ENODEV;
2356 2456
2357 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
2358 if (!rbd_dev)
2359 goto err_nomem;
2360 options = kmalloc(count, GFP_KERNEL); 2457 options = kmalloc(count, GFP_KERNEL);
2361 if (!options) 2458 if (!options)
2362 goto err_nomem; 2459 goto err_nomem;
2460 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
2461 if (!rbd_dev)
2462 goto err_nomem;
2363 2463
2364 /* static rbd_device initialization */ 2464 /* static rbd_device initialization */
2365 spin_lock_init(&rbd_dev->lock); 2465 spin_lock_init(&rbd_dev->lock);
@@ -2367,15 +2467,13 @@ static ssize_t rbd_add(struct bus_type *bus,
2367 INIT_LIST_HEAD(&rbd_dev->snaps); 2467 INIT_LIST_HEAD(&rbd_dev->snaps);
2368 init_rwsem(&rbd_dev->header_rwsem); 2468 init_rwsem(&rbd_dev->header_rwsem);
2369 2469
2370 init_rwsem(&rbd_dev->header_rwsem);
2371
2372 /* generate unique id: find highest unique id, add one */ 2470 /* generate unique id: find highest unique id, add one */
2373 rbd_id_get(rbd_dev); 2471 rbd_id_get(rbd_dev);
2374 2472
2375 /* Fill in the device name, now that we have its id. */ 2473 /* Fill in the device name, now that we have its id. */
2376 BUILD_BUG_ON(DEV_NAME_LEN 2474 BUILD_BUG_ON(DEV_NAME_LEN
2377 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH); 2475 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
2378 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->id); 2476 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
2379 2477
2380 /* parse add command */ 2478 /* parse add command */
2381 rc = rbd_add_parse_args(rbd_dev, buf, &mon_addrs, &mon_addrs_size, 2479 rc = rbd_add_parse_args(rbd_dev, buf, &mon_addrs, &mon_addrs_size,
@@ -2395,7 +2493,7 @@ static ssize_t rbd_add(struct bus_type *bus,
2395 rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name); 2493 rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name);
2396 if (rc < 0) 2494 if (rc < 0)
2397 goto err_out_client; 2495 goto err_out_client;
2398 rbd_dev->poolid = rc; 2496 rbd_dev->pool_id = rc;
2399 2497
2400 /* register our block device */ 2498 /* register our block device */
2401 rc = register_blkdev(0, rbd_dev->name); 2499 rc = register_blkdev(0, rbd_dev->name);
@@ -2435,10 +2533,16 @@ err_out_blkdev:
2435err_out_client: 2533err_out_client:
2436 rbd_put_client(rbd_dev); 2534 rbd_put_client(rbd_dev);
2437err_put_id: 2535err_put_id:
2536 if (rbd_dev->pool_name) {
2537 kfree(rbd_dev->snap_name);
2538 kfree(rbd_dev->header_name);
2539 kfree(rbd_dev->image_name);
2540 kfree(rbd_dev->pool_name);
2541 }
2438 rbd_id_put(rbd_dev); 2542 rbd_id_put(rbd_dev);
2439err_nomem: 2543err_nomem:
2440 kfree(options);
2441 kfree(rbd_dev); 2544 kfree(rbd_dev);
2545 kfree(options);
2442 2546
2443 dout("Error adding device %s\n", buf); 2547 dout("Error adding device %s\n", buf);
2444 module_put(THIS_MODULE); 2548 module_put(THIS_MODULE);
@@ -2446,7 +2550,7 @@ err_nomem:
2446 return (ssize_t) rc; 2550 return (ssize_t) rc;
2447} 2551}
2448 2552
2449static struct rbd_device *__rbd_get_dev(unsigned long id) 2553static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
2450{ 2554{
2451 struct list_head *tmp; 2555 struct list_head *tmp;
2452 struct rbd_device *rbd_dev; 2556 struct rbd_device *rbd_dev;
@@ -2454,7 +2558,7 @@ static struct rbd_device *__rbd_get_dev(unsigned long id)
2454 spin_lock(&rbd_dev_list_lock); 2558 spin_lock(&rbd_dev_list_lock);
2455 list_for_each(tmp, &rbd_dev_list) { 2559 list_for_each(tmp, &rbd_dev_list) {
2456 rbd_dev = list_entry(tmp, struct rbd_device, node); 2560 rbd_dev = list_entry(tmp, struct rbd_device, node);
2457 if (rbd_dev->id == id) { 2561 if (rbd_dev->dev_id == dev_id) {
2458 spin_unlock(&rbd_dev_list_lock); 2562 spin_unlock(&rbd_dev_list_lock);
2459 return rbd_dev; 2563 return rbd_dev;
2460 } 2564 }
@@ -2474,7 +2578,7 @@ static void rbd_dev_release(struct device *dev)
2474 rbd_dev->watch_request); 2578 rbd_dev->watch_request);
2475 } 2579 }
2476 if (rbd_dev->watch_event) 2580 if (rbd_dev->watch_event)
2477 rbd_req_sync_unwatch(rbd_dev, rbd_dev->obj_md_name); 2581 rbd_req_sync_unwatch(rbd_dev);
2478 2582
2479 rbd_put_client(rbd_dev); 2583 rbd_put_client(rbd_dev);
2480 2584
@@ -2483,6 +2587,10 @@ static void rbd_dev_release(struct device *dev)
2483 unregister_blkdev(rbd_dev->major, rbd_dev->name); 2587 unregister_blkdev(rbd_dev->major, rbd_dev->name);
2484 2588
2485 /* done with the id, and with the rbd_dev */ 2589 /* done with the id, and with the rbd_dev */
2590 kfree(rbd_dev->snap_name);
2591 kfree(rbd_dev->header_name);
2592 kfree(rbd_dev->pool_name);
2593 kfree(rbd_dev->image_name);
2486 rbd_id_put(rbd_dev); 2594 rbd_id_put(rbd_dev);
2487 kfree(rbd_dev); 2595 kfree(rbd_dev);
2488 2596
@@ -2544,7 +2652,7 @@ static ssize_t rbd_snap_add(struct device *dev,
2544 if (ret < 0) 2652 if (ret < 0)
2545 goto err_unlock; 2653 goto err_unlock;
2546 2654
2547 ret = __rbd_refresh_header(rbd_dev); 2655 ret = __rbd_refresh_header(rbd_dev, NULL);
2548 if (ret < 0) 2656 if (ret < 0)
2549 goto err_unlock; 2657 goto err_unlock;
2550 2658
@@ -2553,7 +2661,7 @@ static ssize_t rbd_snap_add(struct device *dev,
2553 mutex_unlock(&ctl_mutex); 2661 mutex_unlock(&ctl_mutex);
2554 2662
2555 /* make a best effort, don't error if failed */ 2663 /* make a best effort, don't error if failed */
2556 rbd_req_sync_notify(rbd_dev, rbd_dev->obj_md_name); 2664 rbd_req_sync_notify(rbd_dev);
2557 2665
2558 ret = count; 2666 ret = count;
2559 kfree(name); 2667 kfree(name);
diff --git a/drivers/block/rbd_types.h b/drivers/block/rbd_types.h
index 950708688f17..0924e9e41a60 100644
--- a/drivers/block/rbd_types.h
+++ b/drivers/block/rbd_types.h
@@ -31,7 +31,6 @@
31#define RBD_MIN_OBJ_ORDER 16 31#define RBD_MIN_OBJ_ORDER 16
32#define RBD_MAX_OBJ_ORDER 30 32#define RBD_MAX_OBJ_ORDER 30
33 33
34#define RBD_MAX_OBJ_NAME_LEN 96
35#define RBD_MAX_SEG_NAME_LEN 128 34#define RBD_MAX_SEG_NAME_LEN 128
36 35
37#define RBD_COMP_NONE 0 36#define RBD_COMP_NONE 0
diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c
index e4fb3374dcd2..2c2d2e5c1597 100644
--- a/drivers/block/xen-blkfront.c
+++ b/drivers/block/xen-blkfront.c
@@ -888,9 +888,8 @@ static int setup_blkring(struct xenbus_device *dev,
888 if (err) 888 if (err)
889 goto fail; 889 goto fail;
890 890
891 err = bind_evtchn_to_irqhandler(info->evtchn, 891 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
892 blkif_interrupt, 892 "blkif", info);
893 IRQF_SAMPLE_RANDOM, "blkif", info);
894 if (err <= 0) { 893 if (err <= 0) {
895 xenbus_dev_fatal(dev, err, 894 xenbus_dev_fatal(dev, err,
896 "bind_evtchn_to_irqhandler failed"); 895 "bind_evtchn_to_irqhandler failed");
diff --git a/drivers/char/random.c b/drivers/char/random.c
index 4ec04a754733..b86eae9b77df 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -125,21 +125,26 @@
125 * The current exported interfaces for gathering environmental noise 125 * The current exported interfaces for gathering environmental noise
126 * from the devices are: 126 * from the devices are:
127 * 127 *
128 * void add_device_randomness(const void *buf, unsigned int size);
128 * void add_input_randomness(unsigned int type, unsigned int code, 129 * void add_input_randomness(unsigned int type, unsigned int code,
129 * unsigned int value); 130 * unsigned int value);
130 * void add_interrupt_randomness(int irq); 131 * void add_interrupt_randomness(int irq, int irq_flags);
131 * void add_disk_randomness(struct gendisk *disk); 132 * void add_disk_randomness(struct gendisk *disk);
132 * 133 *
134 * add_device_randomness() is for adding data to the random pool that
135 * is likely to differ between two devices (or possibly even per boot).
136 * This would be things like MAC addresses or serial numbers, or the
137 * read-out of the RTC. This does *not* add any actual entropy to the
138 * pool, but it initializes the pool to different values for devices
139 * that might otherwise be identical and have very little entropy
140 * available to them (particularly common in the embedded world).
141 *
133 * add_input_randomness() uses the input layer interrupt timing, as well as 142 * add_input_randomness() uses the input layer interrupt timing, as well as
134 * the event type information from the hardware. 143 * the event type information from the hardware.
135 * 144 *
136 * add_interrupt_randomness() uses the inter-interrupt timing as random 145 * add_interrupt_randomness() uses the interrupt timing as random
137 * inputs to the entropy pool. Note that not all interrupts are good 146 * inputs to the entropy pool. Using the cycle counters and the irq source
138 * sources of randomness! For example, the timer interrupts is not a 147 * as inputs, it feeds the randomness roughly once a second.
139 * good choice, because the periodicity of the interrupts is too
140 * regular, and hence predictable to an attacker. Network Interface
141 * Controller interrupts are a better measure, since the timing of the
142 * NIC interrupts are more unpredictable.
143 * 148 *
144 * add_disk_randomness() uses what amounts to the seek time of block 149 * add_disk_randomness() uses what amounts to the seek time of block
145 * layer request events, on a per-disk_devt basis, as input to the 150 * layer request events, on a per-disk_devt basis, as input to the
@@ -248,6 +253,8 @@
248#include <linux/percpu.h> 253#include <linux/percpu.h>
249#include <linux/cryptohash.h> 254#include <linux/cryptohash.h>
250#include <linux/fips.h> 255#include <linux/fips.h>
256#include <linux/ptrace.h>
257#include <linux/kmemcheck.h>
251 258
252#ifdef CONFIG_GENERIC_HARDIRQS 259#ifdef CONFIG_GENERIC_HARDIRQS
253# include <linux/irq.h> 260# include <linux/irq.h>
@@ -256,8 +263,12 @@
256#include <asm/processor.h> 263#include <asm/processor.h>
257#include <asm/uaccess.h> 264#include <asm/uaccess.h>
258#include <asm/irq.h> 265#include <asm/irq.h>
266#include <asm/irq_regs.h>
259#include <asm/io.h> 267#include <asm/io.h>
260 268
269#define CREATE_TRACE_POINTS
270#include <trace/events/random.h>
271
261/* 272/*
262 * Configuration information 273 * Configuration information
263 */ 274 */
@@ -266,6 +277,8 @@
266#define SEC_XFER_SIZE 512 277#define SEC_XFER_SIZE 512
267#define EXTRACT_SIZE 10 278#define EXTRACT_SIZE 10
268 279
280#define LONGS(x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long))
281
269/* 282/*
270 * The minimum number of bits of entropy before we wake up a read on 283 * The minimum number of bits of entropy before we wake up a read on
271 * /dev/random. Should be enough to do a significant reseed. 284 * /dev/random. Should be enough to do a significant reseed.
@@ -420,8 +433,10 @@ struct entropy_store {
420 /* read-write data: */ 433 /* read-write data: */
421 spinlock_t lock; 434 spinlock_t lock;
422 unsigned add_ptr; 435 unsigned add_ptr;
436 unsigned input_rotate;
423 int entropy_count; 437 int entropy_count;
424 int input_rotate; 438 int entropy_total;
439 unsigned int initialized:1;
425 __u8 last_data[EXTRACT_SIZE]; 440 __u8 last_data[EXTRACT_SIZE];
426}; 441};
427 442
@@ -454,6 +469,10 @@ static struct entropy_store nonblocking_pool = {
454 .pool = nonblocking_pool_data 469 .pool = nonblocking_pool_data
455}; 470};
456 471
472static __u32 const twist_table[8] = {
473 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
474 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
475
457/* 476/*
458 * This function adds bytes into the entropy "pool". It does not 477 * This function adds bytes into the entropy "pool". It does not
459 * update the entropy estimate. The caller should call 478 * update the entropy estimate. The caller should call
@@ -464,29 +483,24 @@ static struct entropy_store nonblocking_pool = {
464 * it's cheap to do so and helps slightly in the expected case where 483 * it's cheap to do so and helps slightly in the expected case where
465 * the entropy is concentrated in the low-order bits. 484 * the entropy is concentrated in the low-order bits.
466 */ 485 */
467static void mix_pool_bytes_extract(struct entropy_store *r, const void *in, 486static void _mix_pool_bytes(struct entropy_store *r, const void *in,
468 int nbytes, __u8 out[64]) 487 int nbytes, __u8 out[64])
469{ 488{
470 static __u32 const twist_table[8] = {
471 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
472 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
473 unsigned long i, j, tap1, tap2, tap3, tap4, tap5; 489 unsigned long i, j, tap1, tap2, tap3, tap4, tap5;
474 int input_rotate; 490 int input_rotate;
475 int wordmask = r->poolinfo->poolwords - 1; 491 int wordmask = r->poolinfo->poolwords - 1;
476 const char *bytes = in; 492 const char *bytes = in;
477 __u32 w; 493 __u32 w;
478 unsigned long flags;
479 494
480 /* Taps are constant, so we can load them without holding r->lock. */
481 tap1 = r->poolinfo->tap1; 495 tap1 = r->poolinfo->tap1;
482 tap2 = r->poolinfo->tap2; 496 tap2 = r->poolinfo->tap2;
483 tap3 = r->poolinfo->tap3; 497 tap3 = r->poolinfo->tap3;
484 tap4 = r->poolinfo->tap4; 498 tap4 = r->poolinfo->tap4;
485 tap5 = r->poolinfo->tap5; 499 tap5 = r->poolinfo->tap5;
486 500
487 spin_lock_irqsave(&r->lock, flags); 501 smp_rmb();
488 input_rotate = r->input_rotate; 502 input_rotate = ACCESS_ONCE(r->input_rotate);
489 i = r->add_ptr; 503 i = ACCESS_ONCE(r->add_ptr);
490 504
491 /* mix one byte at a time to simplify size handling and churn faster */ 505 /* mix one byte at a time to simplify size handling and churn faster */
492 while (nbytes--) { 506 while (nbytes--) {
@@ -513,19 +527,61 @@ static void mix_pool_bytes_extract(struct entropy_store *r, const void *in,
513 input_rotate += i ? 7 : 14; 527 input_rotate += i ? 7 : 14;
514 } 528 }
515 529
516 r->input_rotate = input_rotate; 530 ACCESS_ONCE(r->input_rotate) = input_rotate;
517 r->add_ptr = i; 531 ACCESS_ONCE(r->add_ptr) = i;
532 smp_wmb();
518 533
519 if (out) 534 if (out)
520 for (j = 0; j < 16; j++) 535 for (j = 0; j < 16; j++)
521 ((__u32 *)out)[j] = r->pool[(i - j) & wordmask]; 536 ((__u32 *)out)[j] = r->pool[(i - j) & wordmask];
537}
538
539static void __mix_pool_bytes(struct entropy_store *r, const void *in,
540 int nbytes, __u8 out[64])
541{
542 trace_mix_pool_bytes_nolock(r->name, nbytes, _RET_IP_);
543 _mix_pool_bytes(r, in, nbytes, out);
544}
545
546static void mix_pool_bytes(struct entropy_store *r, const void *in,
547 int nbytes, __u8 out[64])
548{
549 unsigned long flags;
522 550
551 trace_mix_pool_bytes(r->name, nbytes, _RET_IP_);
552 spin_lock_irqsave(&r->lock, flags);
553 _mix_pool_bytes(r, in, nbytes, out);
523 spin_unlock_irqrestore(&r->lock, flags); 554 spin_unlock_irqrestore(&r->lock, flags);
524} 555}
525 556
526static void mix_pool_bytes(struct entropy_store *r, const void *in, int bytes) 557struct fast_pool {
558 __u32 pool[4];
559 unsigned long last;
560 unsigned short count;
561 unsigned char rotate;
562 unsigned char last_timer_intr;
563};
564
565/*
566 * This is a fast mixing routine used by the interrupt randomness
567 * collector. It's hardcoded for an 128 bit pool and assumes that any
568 * locks that might be needed are taken by the caller.
569 */
570static void fast_mix(struct fast_pool *f, const void *in, int nbytes)
527{ 571{
528 mix_pool_bytes_extract(r, in, bytes, NULL); 572 const char *bytes = in;
573 __u32 w;
574 unsigned i = f->count;
575 unsigned input_rotate = f->rotate;
576
577 while (nbytes--) {
578 w = rol32(*bytes++, input_rotate & 31) ^ f->pool[i & 3] ^
579 f->pool[(i + 1) & 3];
580 f->pool[i & 3] = (w >> 3) ^ twist_table[w & 7];
581 input_rotate += (i++ & 3) ? 7 : 14;
582 }
583 f->count = i;
584 f->rotate = input_rotate;
529} 585}
530 586
531/* 587/*
@@ -533,30 +589,38 @@ static void mix_pool_bytes(struct entropy_store *r, const void *in, int bytes)
533 */ 589 */
534static void credit_entropy_bits(struct entropy_store *r, int nbits) 590static void credit_entropy_bits(struct entropy_store *r, int nbits)
535{ 591{
536 unsigned long flags; 592 int entropy_count, orig;
537 int entropy_count;
538 593
539 if (!nbits) 594 if (!nbits)
540 return; 595 return;
541 596
542 spin_lock_irqsave(&r->lock, flags);
543
544 DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name); 597 DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name);
545 entropy_count = r->entropy_count; 598retry:
599 entropy_count = orig = ACCESS_ONCE(r->entropy_count);
546 entropy_count += nbits; 600 entropy_count += nbits;
601
547 if (entropy_count < 0) { 602 if (entropy_count < 0) {
548 DEBUG_ENT("negative entropy/overflow\n"); 603 DEBUG_ENT("negative entropy/overflow\n");
549 entropy_count = 0; 604 entropy_count = 0;
550 } else if (entropy_count > r->poolinfo->POOLBITS) 605 } else if (entropy_count > r->poolinfo->POOLBITS)
551 entropy_count = r->poolinfo->POOLBITS; 606 entropy_count = r->poolinfo->POOLBITS;
552 r->entropy_count = entropy_count; 607 if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
608 goto retry;
609
610 if (!r->initialized && nbits > 0) {
611 r->entropy_total += nbits;
612 if (r->entropy_total > 128)
613 r->initialized = 1;
614 }
615
616 trace_credit_entropy_bits(r->name, nbits, entropy_count,
617 r->entropy_total, _RET_IP_);
553 618
554 /* should we wake readers? */ 619 /* should we wake readers? */
555 if (r == &input_pool && entropy_count >= random_read_wakeup_thresh) { 620 if (r == &input_pool && entropy_count >= random_read_wakeup_thresh) {
556 wake_up_interruptible(&random_read_wait); 621 wake_up_interruptible(&random_read_wait);
557 kill_fasync(&fasync, SIGIO, POLL_IN); 622 kill_fasync(&fasync, SIGIO, POLL_IN);
558 } 623 }
559 spin_unlock_irqrestore(&r->lock, flags);
560} 624}
561 625
562/********************************************************************* 626/*********************************************************************
@@ -572,42 +636,24 @@ struct timer_rand_state {
572 unsigned dont_count_entropy:1; 636 unsigned dont_count_entropy:1;
573}; 637};
574 638
575#ifndef CONFIG_GENERIC_HARDIRQS 639/*
576 640 * Add device- or boot-specific data to the input and nonblocking
577static struct timer_rand_state *irq_timer_state[NR_IRQS]; 641 * pools to help initialize them to unique values.
578 642 *
579static struct timer_rand_state *get_timer_rand_state(unsigned int irq) 643 * None of this adds any entropy, it is meant to avoid the
580{ 644 * problem of the nonblocking pool having similar initial state
581 return irq_timer_state[irq]; 645 * across largely identical devices.
582} 646 */
583 647void add_device_randomness(const void *buf, unsigned int size)
584static void set_timer_rand_state(unsigned int irq,
585 struct timer_rand_state *state)
586{
587 irq_timer_state[irq] = state;
588}
589
590#else
591
592static struct timer_rand_state *get_timer_rand_state(unsigned int irq)
593{
594 struct irq_desc *desc;
595
596 desc = irq_to_desc(irq);
597
598 return desc->timer_rand_state;
599}
600
601static void set_timer_rand_state(unsigned int irq,
602 struct timer_rand_state *state)
603{ 648{
604 struct irq_desc *desc; 649 unsigned long time = get_cycles() ^ jiffies;
605 650
606 desc = irq_to_desc(irq); 651 mix_pool_bytes(&input_pool, buf, size, NULL);
607 652 mix_pool_bytes(&input_pool, &time, sizeof(time), NULL);
608 desc->timer_rand_state = state; 653 mix_pool_bytes(&nonblocking_pool, buf, size, NULL);
654 mix_pool_bytes(&nonblocking_pool, &time, sizeof(time), NULL);
609} 655}
610#endif 656EXPORT_SYMBOL(add_device_randomness);
611 657
612static struct timer_rand_state input_timer_state; 658static struct timer_rand_state input_timer_state;
613 659
@@ -637,13 +683,9 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
637 goto out; 683 goto out;
638 684
639 sample.jiffies = jiffies; 685 sample.jiffies = jiffies;
640 686 sample.cycles = get_cycles();
641 /* Use arch random value, fall back to cycles */
642 if (!arch_get_random_int(&sample.cycles))
643 sample.cycles = get_cycles();
644
645 sample.num = num; 687 sample.num = num;
646 mix_pool_bytes(&input_pool, &sample, sizeof(sample)); 688 mix_pool_bytes(&input_pool, &sample, sizeof(sample), NULL);
647 689
648 /* 690 /*
649 * Calculate number of bits of randomness we probably added. 691 * Calculate number of bits of randomness we probably added.
@@ -700,17 +742,48 @@ void add_input_randomness(unsigned int type, unsigned int code,
700} 742}
701EXPORT_SYMBOL_GPL(add_input_randomness); 743EXPORT_SYMBOL_GPL(add_input_randomness);
702 744
703void add_interrupt_randomness(int irq) 745static DEFINE_PER_CPU(struct fast_pool, irq_randomness);
746
747void add_interrupt_randomness(int irq, int irq_flags)
704{ 748{
705 struct timer_rand_state *state; 749 struct entropy_store *r;
750 struct fast_pool *fast_pool = &__get_cpu_var(irq_randomness);
751 struct pt_regs *regs = get_irq_regs();
752 unsigned long now = jiffies;
753 __u32 input[4], cycles = get_cycles();
754
755 input[0] = cycles ^ jiffies;
756 input[1] = irq;
757 if (regs) {
758 __u64 ip = instruction_pointer(regs);
759 input[2] = ip;
760 input[3] = ip >> 32;
761 }
706 762
707 state = get_timer_rand_state(irq); 763 fast_mix(fast_pool, input, sizeof(input));
708 764
709 if (state == NULL) 765 if ((fast_pool->count & 1023) &&
766 !time_after(now, fast_pool->last + HZ))
710 return; 767 return;
711 768
712 DEBUG_ENT("irq event %d\n", irq); 769 fast_pool->last = now;
713 add_timer_randomness(state, 0x100 + irq); 770
771 r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool;
772 __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool), NULL);
773 /*
774 * If we don't have a valid cycle counter, and we see
775 * back-to-back timer interrupts, then skip giving credit for
776 * any entropy.
777 */
778 if (cycles == 0) {
779 if (irq_flags & __IRQF_TIMER) {
780 if (fast_pool->last_timer_intr)
781 return;
782 fast_pool->last_timer_intr = 1;
783 } else
784 fast_pool->last_timer_intr = 0;
785 }
786 credit_entropy_bits(r, 1);
714} 787}
715 788
716#ifdef CONFIG_BLOCK 789#ifdef CONFIG_BLOCK
@@ -742,7 +815,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void *buf,
742 */ 815 */
743static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes) 816static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
744{ 817{
745 __u32 tmp[OUTPUT_POOL_WORDS]; 818 __u32 tmp[OUTPUT_POOL_WORDS];
746 819
747 if (r->pull && r->entropy_count < nbytes * 8 && 820 if (r->pull && r->entropy_count < nbytes * 8 &&
748 r->entropy_count < r->poolinfo->POOLBITS) { 821 r->entropy_count < r->poolinfo->POOLBITS) {
@@ -761,7 +834,7 @@ static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
761 834
762 bytes = extract_entropy(r->pull, tmp, bytes, 835 bytes = extract_entropy(r->pull, tmp, bytes,
763 random_read_wakeup_thresh / 8, rsvd); 836 random_read_wakeup_thresh / 8, rsvd);
764 mix_pool_bytes(r, tmp, bytes); 837 mix_pool_bytes(r, tmp, bytes, NULL);
765 credit_entropy_bits(r, bytes*8); 838 credit_entropy_bits(r, bytes*8);
766 } 839 }
767} 840}
@@ -820,13 +893,19 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
820static void extract_buf(struct entropy_store *r, __u8 *out) 893static void extract_buf(struct entropy_store *r, __u8 *out)
821{ 894{
822 int i; 895 int i;
823 __u32 hash[5], workspace[SHA_WORKSPACE_WORDS]; 896 union {
897 __u32 w[5];
898 unsigned long l[LONGS(EXTRACT_SIZE)];
899 } hash;
900 __u32 workspace[SHA_WORKSPACE_WORDS];
824 __u8 extract[64]; 901 __u8 extract[64];
902 unsigned long flags;
825 903
826 /* Generate a hash across the pool, 16 words (512 bits) at a time */ 904 /* Generate a hash across the pool, 16 words (512 bits) at a time */
827 sha_init(hash); 905 sha_init(hash.w);
906 spin_lock_irqsave(&r->lock, flags);
828 for (i = 0; i < r->poolinfo->poolwords; i += 16) 907 for (i = 0; i < r->poolinfo->poolwords; i += 16)
829 sha_transform(hash, (__u8 *)(r->pool + i), workspace); 908 sha_transform(hash.w, (__u8 *)(r->pool + i), workspace);
830 909
831 /* 910 /*
832 * We mix the hash back into the pool to prevent backtracking 911 * We mix the hash back into the pool to prevent backtracking
@@ -837,13 +916,14 @@ static void extract_buf(struct entropy_store *r, __u8 *out)
837 * brute-forcing the feedback as hard as brute-forcing the 916 * brute-forcing the feedback as hard as brute-forcing the
838 * hash. 917 * hash.
839 */ 918 */
840 mix_pool_bytes_extract(r, hash, sizeof(hash), extract); 919 __mix_pool_bytes(r, hash.w, sizeof(hash.w), extract);
920 spin_unlock_irqrestore(&r->lock, flags);
841 921
842 /* 922 /*
843 * To avoid duplicates, we atomically extract a portion of the 923 * To avoid duplicates, we atomically extract a portion of the
844 * pool while mixing, and hash one final time. 924 * pool while mixing, and hash one final time.
845 */ 925 */
846 sha_transform(hash, extract, workspace); 926 sha_transform(hash.w, extract, workspace);
847 memset(extract, 0, sizeof(extract)); 927 memset(extract, 0, sizeof(extract));
848 memset(workspace, 0, sizeof(workspace)); 928 memset(workspace, 0, sizeof(workspace));
849 929
@@ -852,20 +932,32 @@ static void extract_buf(struct entropy_store *r, __u8 *out)
852 * pattern, we fold it in half. Thus, we always feed back 932 * pattern, we fold it in half. Thus, we always feed back
853 * twice as much data as we output. 933 * twice as much data as we output.
854 */ 934 */
855 hash[0] ^= hash[3]; 935 hash.w[0] ^= hash.w[3];
856 hash[1] ^= hash[4]; 936 hash.w[1] ^= hash.w[4];
857 hash[2] ^= rol32(hash[2], 16); 937 hash.w[2] ^= rol32(hash.w[2], 16);
858 memcpy(out, hash, EXTRACT_SIZE); 938
859 memset(hash, 0, sizeof(hash)); 939 /*
940 * If we have a architectural hardware random number
941 * generator, mix that in, too.
942 */
943 for (i = 0; i < LONGS(EXTRACT_SIZE); i++) {
944 unsigned long v;
945 if (!arch_get_random_long(&v))
946 break;
947 hash.l[i] ^= v;
948 }
949
950 memcpy(out, &hash, EXTRACT_SIZE);
951 memset(&hash, 0, sizeof(hash));
860} 952}
861 953
862static ssize_t extract_entropy(struct entropy_store *r, void *buf, 954static ssize_t extract_entropy(struct entropy_store *r, void *buf,
863 size_t nbytes, int min, int reserved) 955 size_t nbytes, int min, int reserved)
864{ 956{
865 ssize_t ret = 0, i; 957 ssize_t ret = 0, i;
866 __u8 tmp[EXTRACT_SIZE]; 958 __u8 tmp[EXTRACT_SIZE];
867 unsigned long flags;
868 959
960 trace_extract_entropy(r->name, nbytes, r->entropy_count, _RET_IP_);
869 xfer_secondary_pool(r, nbytes); 961 xfer_secondary_pool(r, nbytes);
870 nbytes = account(r, nbytes, min, reserved); 962 nbytes = account(r, nbytes, min, reserved);
871 963
@@ -873,6 +965,8 @@ static ssize_t extract_entropy(struct entropy_store *r, void *buf,
873 extract_buf(r, tmp); 965 extract_buf(r, tmp);
874 966
875 if (fips_enabled) { 967 if (fips_enabled) {
968 unsigned long flags;
969
876 spin_lock_irqsave(&r->lock, flags); 970 spin_lock_irqsave(&r->lock, flags);
877 if (!memcmp(tmp, r->last_data, EXTRACT_SIZE)) 971 if (!memcmp(tmp, r->last_data, EXTRACT_SIZE))
878 panic("Hardware RNG duplicated output!\n"); 972 panic("Hardware RNG duplicated output!\n");
@@ -898,6 +992,7 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
898 ssize_t ret = 0, i; 992 ssize_t ret = 0, i;
899 __u8 tmp[EXTRACT_SIZE]; 993 __u8 tmp[EXTRACT_SIZE];
900 994
995 trace_extract_entropy_user(r->name, nbytes, r->entropy_count, _RET_IP_);
901 xfer_secondary_pool(r, nbytes); 996 xfer_secondary_pool(r, nbytes);
902 nbytes = account(r, nbytes, 0, 0); 997 nbytes = account(r, nbytes, 0, 0);
903 998
@@ -931,17 +1026,35 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
931 1026
932/* 1027/*
933 * This function is the exported kernel interface. It returns some 1028 * This function is the exported kernel interface. It returns some
934 * number of good random numbers, suitable for seeding TCP sequence 1029 * number of good random numbers, suitable for key generation, seeding
935 * numbers, etc. 1030 * TCP sequence numbers, etc. It does not use the hw random number
1031 * generator, if available; use get_random_bytes_arch() for that.
936 */ 1032 */
937void get_random_bytes(void *buf, int nbytes) 1033void get_random_bytes(void *buf, int nbytes)
938{ 1034{
1035 extract_entropy(&nonblocking_pool, buf, nbytes, 0, 0);
1036}
1037EXPORT_SYMBOL(get_random_bytes);
1038
1039/*
1040 * This function will use the architecture-specific hardware random
1041 * number generator if it is available. The arch-specific hw RNG will
1042 * almost certainly be faster than what we can do in software, but it
1043 * is impossible to verify that it is implemented securely (as
1044 * opposed, to, say, the AES encryption of a sequence number using a
1045 * key known by the NSA). So it's useful if we need the speed, but
1046 * only if we're willing to trust the hardware manufacturer not to
1047 * have put in a back door.
1048 */
1049void get_random_bytes_arch(void *buf, int nbytes)
1050{
939 char *p = buf; 1051 char *p = buf;
940 1052
1053 trace_get_random_bytes(nbytes, _RET_IP_);
941 while (nbytes) { 1054 while (nbytes) {
942 unsigned long v; 1055 unsigned long v;
943 int chunk = min(nbytes, (int)sizeof(unsigned long)); 1056 int chunk = min(nbytes, (int)sizeof(unsigned long));
944 1057
945 if (!arch_get_random_long(&v)) 1058 if (!arch_get_random_long(&v))
946 break; 1059 break;
947 1060
@@ -950,9 +1063,11 @@ void get_random_bytes(void *buf, int nbytes)
950 nbytes -= chunk; 1063 nbytes -= chunk;
951 } 1064 }
952 1065
953 extract_entropy(&nonblocking_pool, p, nbytes, 0, 0); 1066 if (nbytes)
1067 extract_entropy(&nonblocking_pool, p, nbytes, 0, 0);
954} 1068}
955EXPORT_SYMBOL(get_random_bytes); 1069EXPORT_SYMBOL(get_random_bytes_arch);
1070
956 1071
957/* 1072/*
958 * init_std_data - initialize pool with system data 1073 * init_std_data - initialize pool with system data
@@ -966,23 +1081,30 @@ EXPORT_SYMBOL(get_random_bytes);
966static void init_std_data(struct entropy_store *r) 1081static void init_std_data(struct entropy_store *r)
967{ 1082{
968 int i; 1083 int i;
969 ktime_t now; 1084 ktime_t now = ktime_get_real();
970 unsigned long flags; 1085 unsigned long rv;
971 1086
972 spin_lock_irqsave(&r->lock, flags);
973 r->entropy_count = 0; 1087 r->entropy_count = 0;
974 spin_unlock_irqrestore(&r->lock, flags); 1088 r->entropy_total = 0;
975 1089 mix_pool_bytes(r, &now, sizeof(now), NULL);
976 now = ktime_get_real(); 1090 for (i = r->poolinfo->POOLBYTES; i > 0; i -= sizeof(rv)) {
977 mix_pool_bytes(r, &now, sizeof(now)); 1091 if (!arch_get_random_long(&rv))
978 for (i = r->poolinfo->POOLBYTES; i > 0; i -= sizeof flags) {
979 if (!arch_get_random_long(&flags))
980 break; 1092 break;
981 mix_pool_bytes(r, &flags, sizeof(flags)); 1093 mix_pool_bytes(r, &rv, sizeof(rv), NULL);
982 } 1094 }
983 mix_pool_bytes(r, utsname(), sizeof(*(utsname()))); 1095 mix_pool_bytes(r, utsname(), sizeof(*(utsname())), NULL);
984} 1096}
985 1097
1098/*
1099 * Note that setup_arch() may call add_device_randomness()
1100 * long before we get here. This allows seeding of the pools
1101 * with some platform dependent data very early in the boot
1102 * process. But it limits our options here. We must use
1103 * statically allocated structures that already have all
1104 * initializations complete at compile time. We should also
1105 * take care not to overwrite the precious per platform data
1106 * we were given.
1107 */
986static int rand_initialize(void) 1108static int rand_initialize(void)
987{ 1109{
988 init_std_data(&input_pool); 1110 init_std_data(&input_pool);
@@ -992,24 +1114,6 @@ static int rand_initialize(void)
992} 1114}
993module_init(rand_initialize); 1115module_init(rand_initialize);
994 1116
995void rand_initialize_irq(int irq)
996{
997 struct timer_rand_state *state;
998
999 state = get_timer_rand_state(irq);
1000
1001 if (state)
1002 return;
1003
1004 /*
1005 * If kzalloc returns null, we just won't use that entropy
1006 * source.
1007 */
1008 state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
1009 if (state)
1010 set_timer_rand_state(irq, state);
1011}
1012
1013#ifdef CONFIG_BLOCK 1117#ifdef CONFIG_BLOCK
1014void rand_initialize_disk(struct gendisk *disk) 1118void rand_initialize_disk(struct gendisk *disk)
1015{ 1119{
@@ -1117,7 +1221,7 @@ write_pool(struct entropy_store *r, const char __user *buffer, size_t count)
1117 count -= bytes; 1221 count -= bytes;
1118 p += bytes; 1222 p += bytes;
1119 1223
1120 mix_pool_bytes(r, buf, bytes); 1224 mix_pool_bytes(r, buf, bytes, NULL);
1121 cond_resched(); 1225 cond_resched();
1122 } 1226 }
1123 1227
@@ -1279,6 +1383,7 @@ static int proc_do_uuid(ctl_table *table, int write,
1279} 1383}
1280 1384
1281static int sysctl_poolsize = INPUT_POOL_WORDS * 32; 1385static int sysctl_poolsize = INPUT_POOL_WORDS * 32;
1386extern ctl_table random_table[];
1282ctl_table random_table[] = { 1387ctl_table random_table[] = {
1283 { 1388 {
1284 .procname = "poolsize", 1389 .procname = "poolsize",
@@ -1344,7 +1449,7 @@ late_initcall(random_int_secret_init);
1344 * value is not cryptographically secure but for several uses the cost of 1449 * value is not cryptographically secure but for several uses the cost of
1345 * depleting entropy is too high 1450 * depleting entropy is too high
1346 */ 1451 */
1347DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash); 1452static DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash);
1348unsigned int get_random_int(void) 1453unsigned int get_random_int(void)
1349{ 1454{
1350 __u32 *hash; 1455 __u32 *hash;
diff --git a/drivers/crypto/n2_core.c b/drivers/crypto/n2_core.c
index 67b97c5fd859..a8bd0310f8fe 100644
--- a/drivers/crypto/n2_core.c
+++ b/drivers/crypto/n2_core.c
@@ -1610,8 +1610,7 @@ static int spu_map_ino(struct platform_device *dev, struct spu_mdesc_info *ip,
1610 1610
1611 sprintf(p->irq_name, "%s-%d", irq_name, index); 1611 sprintf(p->irq_name, "%s-%d", irq_name, index);
1612 1612
1613 return request_irq(p->irq, handler, IRQF_SAMPLE_RANDOM, 1613 return request_irq(p->irq, handler, 0, p->irq_name, p);
1614 p->irq_name, p);
1615} 1614}
1616 1615
1617static struct kmem_cache *queue_cache[2]; 1616static struct kmem_cache *queue_cache[2];
diff --git a/drivers/firmware/dmi_scan.c b/drivers/firmware/dmi_scan.c
index 153980be4ee6..b298158cb922 100644
--- a/drivers/firmware/dmi_scan.c
+++ b/drivers/firmware/dmi_scan.c
@@ -6,6 +6,7 @@
6#include <linux/dmi.h> 6#include <linux/dmi.h>
7#include <linux/efi.h> 7#include <linux/efi.h>
8#include <linux/bootmem.h> 8#include <linux/bootmem.h>
9#include <linux/random.h>
9#include <asm/dmi.h> 10#include <asm/dmi.h>
10 11
11/* 12/*
@@ -111,6 +112,8 @@ static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
111 112
112 dmi_table(buf, dmi_len, dmi_num, decode, NULL); 113 dmi_table(buf, dmi_len, dmi_num, decode, NULL);
113 114
115 add_device_randomness(buf, dmi_len);
116
114 dmi_iounmap(buf, dmi_len); 117 dmi_iounmap(buf, dmi_len);
115 return 0; 118 return 0;
116} 119}
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 500844f04f93..60ea284407ce 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -1928,6 +1928,7 @@ static const struct hid_device_id hid_ignore_list[] = {
1928 { HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) }, 1928 { HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1929 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) }, 1929 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1930 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) }, 1930 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1931 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_RADIOSHARK) },
1931 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) }, 1932 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1932 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) }, 1933 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1933 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) }, 1934 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 41c34f21bd00..1dcb76ff51e3 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -333,6 +333,7 @@
333#define USB_VENDOR_ID_GRIFFIN 0x077d 333#define USB_VENDOR_ID_GRIFFIN 0x077d
334#define USB_DEVICE_ID_POWERMATE 0x0410 334#define USB_DEVICE_ID_POWERMATE 0x0410
335#define USB_DEVICE_ID_SOUNDKNOB 0x04AA 335#define USB_DEVICE_ID_SOUNDKNOB 0x04AA
336#define USB_DEVICE_ID_RADIOSHARK 0x627a
336 337
337#define USB_VENDOR_ID_GTCO 0x078c 338#define USB_VENDOR_ID_GTCO 0x078c
338#define USB_DEVICE_ID_GTCO_90 0x0090 339#define USB_DEVICE_ID_GTCO_90 0x0090
diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
index a220e5746d67..4748086eaaf2 100644
--- a/drivers/hv/vmbus_drv.c
+++ b/drivers/hv/vmbus_drv.c
@@ -545,8 +545,7 @@ static int vmbus_bus_init(int irq)
545 if (ret) 545 if (ret)
546 goto err_cleanup; 546 goto err_cleanup;
547 547
548 ret = request_irq(irq, vmbus_isr, IRQF_SAMPLE_RANDOM, 548 ret = request_irq(irq, vmbus_isr, 0, driver_name, hv_acpi_dev);
549 driver_name, hv_acpi_dev);
550 549
551 if (ret != 0) { 550 if (ret != 0) {
552 pr_err("Unable to request IRQ %d\n", 551 pr_err("Unable to request IRQ %d\n",
diff --git a/drivers/i2c/busses/i2c-pmcmsp.c b/drivers/i2c/busses/i2c-pmcmsp.c
index 07b7447ecbc9..3d71395ae1f7 100644
--- a/drivers/i2c/busses/i2c-pmcmsp.c
+++ b/drivers/i2c/busses/i2c-pmcmsp.c
@@ -306,8 +306,7 @@ static int __devinit pmcmsptwi_probe(struct platform_device *pldev)
306 pmcmsptwi_data.irq = platform_get_irq(pldev, 0); 306 pmcmsptwi_data.irq = platform_get_irq(pldev, 0);
307 if (pmcmsptwi_data.irq) { 307 if (pmcmsptwi_data.irq) {
308 rc = request_irq(pmcmsptwi_data.irq, &pmcmsptwi_interrupt, 308 rc = request_irq(pmcmsptwi_data.irq, &pmcmsptwi_interrupt,
309 IRQF_SHARED | IRQF_SAMPLE_RANDOM, 309 IRQF_SHARED, pldev->name, &pmcmsptwi_data);
310 pldev->name, &pmcmsptwi_data);
311 if (rc == 0) { 310 if (rc == 0) {
312 /* 311 /*
313 * Enable 'DONE' interrupt only. 312 * Enable 'DONE' interrupt only.
diff --git a/drivers/infiniband/core/cma.c b/drivers/infiniband/core/cma.c
index 5a335b5447c6..7172559ce0c1 100644
--- a/drivers/infiniband/core/cma.c
+++ b/drivers/infiniband/core/cma.c
@@ -3064,10 +3064,7 @@ static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3064 id_priv->id.port_num, &rec, 3064 id_priv->id.port_num, &rec,
3065 comp_mask, GFP_KERNEL, 3065 comp_mask, GFP_KERNEL,
3066 cma_ib_mc_handler, mc); 3066 cma_ib_mc_handler, mc);
3067 if (IS_ERR(mc->multicast.ib)) 3067 return PTR_RET(mc->multicast.ib);
3068 return PTR_ERR(mc->multicast.ib);
3069
3070 return 0;
3071} 3068}
3072 3069
3073static void iboe_mcast_work_handler(struct work_struct *work) 3070static void iboe_mcast_work_handler(struct work_struct *work)
diff --git a/drivers/infiniband/core/ucma.c b/drivers/infiniband/core/ucma.c
index 893cb879462c..6bf850422895 100644
--- a/drivers/infiniband/core/ucma.c
+++ b/drivers/infiniband/core/ucma.c
@@ -1002,23 +1002,18 @@ static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
1002 if (IS_ERR(ctx)) 1002 if (IS_ERR(ctx))
1003 return PTR_ERR(ctx); 1003 return PTR_ERR(ctx);
1004 1004
1005 optval = kmalloc(cmd.optlen, GFP_KERNEL); 1005 optval = memdup_user((void __user *) (unsigned long) cmd.optval,
1006 if (!optval) { 1006 cmd.optlen);
1007 ret = -ENOMEM; 1007 if (IS_ERR(optval)) {
1008 goto out1; 1008 ret = PTR_ERR(optval);
1009 } 1009 goto out;
1010
1011 if (copy_from_user(optval, (void __user *) (unsigned long) cmd.optval,
1012 cmd.optlen)) {
1013 ret = -EFAULT;
1014 goto out2;
1015 } 1010 }
1016 1011
1017 ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval, 1012 ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
1018 cmd.optlen); 1013 cmd.optlen);
1019out2:
1020 kfree(optval); 1014 kfree(optval);
1021out1: 1015
1016out:
1022 ucma_put_ctx(ctx); 1017 ucma_put_ctx(ctx);
1023 return ret; 1018 return ret;
1024} 1019}
diff --git a/drivers/infiniband/hw/ocrdma/ocrdma_verbs.c b/drivers/infiniband/hw/ocrdma/ocrdma_verbs.c
index b2f9784beb4a..cb5b7f7d4d38 100644
--- a/drivers/infiniband/hw/ocrdma/ocrdma_verbs.c
+++ b/drivers/infiniband/hw/ocrdma/ocrdma_verbs.c
@@ -893,7 +893,9 @@ static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev,
893 /* verify consumer QPs are not trying to use GSI QP's CQ */ 893 /* verify consumer QPs are not trying to use GSI QP's CQ */
894 if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) { 894 if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) {
895 if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) || 895 if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) ||
896 (dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq))) { 896 (dev->gsi_sqcq == get_ocrdma_cq(attrs->recv_cq)) ||
897 (dev->gsi_rqcq == get_ocrdma_cq(attrs->send_cq)) ||
898 (dev->gsi_rqcq == get_ocrdma_cq(attrs->recv_cq))) {
897 ocrdma_err("%s(%d) Consumer QP cannot use GSI CQs.\n", 899 ocrdma_err("%s(%d) Consumer QP cannot use GSI CQs.\n",
898 __func__, dev->id); 900 __func__, dev->id);
899 return -EINVAL; 901 return -EINVAL;
diff --git a/drivers/infiniband/hw/qib/qib.h b/drivers/infiniband/hw/qib/qib.h
index 6e19ec844d99..7b1b86690024 100644
--- a/drivers/infiniband/hw/qib/qib.h
+++ b/drivers/infiniband/hw/qib/qib.h
@@ -656,6 +656,11 @@ struct qib_pportdata {
656 /* 16 congestion entries with each entry corresponding to a SL */ 656 /* 16 congestion entries with each entry corresponding to a SL */
657 struct ib_cc_congestion_entry_shadow *congestion_entries; 657 struct ib_cc_congestion_entry_shadow *congestion_entries;
658 658
659 /* Maximum number of congestion control entries that the agent expects
660 * the manager to send.
661 */
662 u16 cc_supported_table_entries;
663
659 /* Total number of congestion control table entries */ 664 /* Total number of congestion control table entries */
660 u16 total_cct_entry; 665 u16 total_cct_entry;
661 666
@@ -667,11 +672,6 @@ struct qib_pportdata {
667 672
668 /* CA's max number of 64 entry units in the congestion control table */ 673 /* CA's max number of 64 entry units in the congestion control table */
669 u8 cc_max_table_entries; 674 u8 cc_max_table_entries;
670
671 /* Maximum number of congestion control entries that the agent expects
672 * the manager to send.
673 */
674 u8 cc_supported_table_entries;
675}; 675};
676 676
677/* Observers. Not to be taken lightly, possibly not to ship. */ 677/* Observers. Not to be taken lightly, possibly not to ship. */
diff --git a/drivers/infiniband/ulp/ipoib/ipoib.h b/drivers/infiniband/ulp/ipoib/ipoib.h
index 86df632ea612..ca43901ed861 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib.h
+++ b/drivers/infiniband/ulp/ipoib/ipoib.h
@@ -92,6 +92,8 @@ enum {
92 IPOIB_STOP_REAPER = 7, 92 IPOIB_STOP_REAPER = 7,
93 IPOIB_FLAG_ADMIN_CM = 9, 93 IPOIB_FLAG_ADMIN_CM = 9,
94 IPOIB_FLAG_UMCAST = 10, 94 IPOIB_FLAG_UMCAST = 10,
95 IPOIB_STOP_NEIGH_GC = 11,
96 IPOIB_NEIGH_TBL_FLUSH = 12,
95 97
96 IPOIB_MAX_BACKOFF_SECONDS = 16, 98 IPOIB_MAX_BACKOFF_SECONDS = 16,
97 99
@@ -260,6 +262,20 @@ struct ipoib_ethtool_st {
260 u16 max_coalesced_frames; 262 u16 max_coalesced_frames;
261}; 263};
262 264
265struct ipoib_neigh_hash {
266 struct ipoib_neigh __rcu **buckets;
267 struct rcu_head rcu;
268 u32 mask;
269 u32 size;
270};
271
272struct ipoib_neigh_table {
273 struct ipoib_neigh_hash __rcu *htbl;
274 rwlock_t rwlock;
275 atomic_t entries;
276 struct completion flushed;
277};
278
263/* 279/*
264 * Device private locking: network stack tx_lock protects members used 280 * Device private locking: network stack tx_lock protects members used
265 * in TX fast path, lock protects everything else. lock nests inside 281 * in TX fast path, lock protects everything else. lock nests inside
@@ -279,6 +295,8 @@ struct ipoib_dev_priv {
279 struct rb_root path_tree; 295 struct rb_root path_tree;
280 struct list_head path_list; 296 struct list_head path_list;
281 297
298 struct ipoib_neigh_table ntbl;
299
282 struct ipoib_mcast *broadcast; 300 struct ipoib_mcast *broadcast;
283 struct list_head multicast_list; 301 struct list_head multicast_list;
284 struct rb_root multicast_tree; 302 struct rb_root multicast_tree;
@@ -291,7 +309,7 @@ struct ipoib_dev_priv {
291 struct work_struct flush_heavy; 309 struct work_struct flush_heavy;
292 struct work_struct restart_task; 310 struct work_struct restart_task;
293 struct delayed_work ah_reap_task; 311 struct delayed_work ah_reap_task;
294 312 struct delayed_work neigh_reap_task;
295 struct ib_device *ca; 313 struct ib_device *ca;
296 u8 port; 314 u8 port;
297 u16 pkey; 315 u16 pkey;
@@ -377,13 +395,16 @@ struct ipoib_neigh {
377#ifdef CONFIG_INFINIBAND_IPOIB_CM 395#ifdef CONFIG_INFINIBAND_IPOIB_CM
378 struct ipoib_cm_tx *cm; 396 struct ipoib_cm_tx *cm;
379#endif 397#endif
380 union ib_gid dgid; 398 u8 daddr[INFINIBAND_ALEN];
381 struct sk_buff_head queue; 399 struct sk_buff_head queue;
382 400
383 struct neighbour *neighbour;
384 struct net_device *dev; 401 struct net_device *dev;
385 402
386 struct list_head list; 403 struct list_head list;
404 struct ipoib_neigh __rcu *hnext;
405 struct rcu_head rcu;
406 atomic_t refcnt;
407 unsigned long alive;
387}; 408};
388 409
389#define IPOIB_UD_MTU(ib_mtu) (ib_mtu - IPOIB_ENCAP_LEN) 410#define IPOIB_UD_MTU(ib_mtu) (ib_mtu - IPOIB_ENCAP_LEN)
@@ -394,21 +415,17 @@ static inline int ipoib_ud_need_sg(unsigned int ib_mtu)
394 return IPOIB_UD_BUF_SIZE(ib_mtu) > PAGE_SIZE; 415 return IPOIB_UD_BUF_SIZE(ib_mtu) > PAGE_SIZE;
395} 416}
396 417
397/* 418void ipoib_neigh_dtor(struct ipoib_neigh *neigh);
398 * We stash a pointer to our private neighbour information after our 419static inline void ipoib_neigh_put(struct ipoib_neigh *neigh)
399 * hardware address in neigh->ha. The ALIGN() expression here makes
400 * sure that this pointer is stored aligned so that an unaligned
401 * load is not needed to dereference it.
402 */
403static inline struct ipoib_neigh **to_ipoib_neigh(struct neighbour *neigh)
404{ 420{
405 return (void*) neigh + ALIGN(offsetof(struct neighbour, ha) + 421 if (atomic_dec_and_test(&neigh->refcnt))
406 INFINIBAND_ALEN, sizeof(void *)); 422 ipoib_neigh_dtor(neigh);
407} 423}
408 424struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr);
409struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neigh, 425struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
410 struct net_device *dev); 426 struct net_device *dev);
411void ipoib_neigh_free(struct net_device *dev, struct ipoib_neigh *neigh); 427void ipoib_neigh_free(struct ipoib_neigh *neigh);
428void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid);
412 429
413extern struct workqueue_struct *ipoib_workqueue; 430extern struct workqueue_struct *ipoib_workqueue;
414 431
@@ -425,7 +442,6 @@ static inline void ipoib_put_ah(struct ipoib_ah *ah)
425{ 442{
426 kref_put(&ah->ref, ipoib_free_ah); 443 kref_put(&ah->ref, ipoib_free_ah);
427} 444}
428
429int ipoib_open(struct net_device *dev); 445int ipoib_open(struct net_device *dev);
430int ipoib_add_pkey_attr(struct net_device *dev); 446int ipoib_add_pkey_attr(struct net_device *dev);
431int ipoib_add_umcast_attr(struct net_device *dev); 447int ipoib_add_umcast_attr(struct net_device *dev);
@@ -455,7 +471,7 @@ void ipoib_dev_cleanup(struct net_device *dev);
455 471
456void ipoib_mcast_join_task(struct work_struct *work); 472void ipoib_mcast_join_task(struct work_struct *work);
457void ipoib_mcast_carrier_on_task(struct work_struct *work); 473void ipoib_mcast_carrier_on_task(struct work_struct *work);
458void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb); 474void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb);
459 475
460void ipoib_mcast_restart_task(struct work_struct *work); 476void ipoib_mcast_restart_task(struct work_struct *work);
461int ipoib_mcast_start_thread(struct net_device *dev); 477int ipoib_mcast_start_thread(struct net_device *dev);
@@ -517,10 +533,10 @@ static inline int ipoib_cm_admin_enabled(struct net_device *dev)
517 test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); 533 test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
518} 534}
519 535
520static inline int ipoib_cm_enabled(struct net_device *dev, struct neighbour *n) 536static inline int ipoib_cm_enabled(struct net_device *dev, u8 *hwaddr)
521{ 537{
522 struct ipoib_dev_priv *priv = netdev_priv(dev); 538 struct ipoib_dev_priv *priv = netdev_priv(dev);
523 return IPOIB_CM_SUPPORTED(n->ha) && 539 return IPOIB_CM_SUPPORTED(hwaddr) &&
524 test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); 540 test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
525} 541}
526 542
@@ -575,7 +591,7 @@ static inline int ipoib_cm_admin_enabled(struct net_device *dev)
575{ 591{
576 return 0; 592 return 0;
577} 593}
578static inline int ipoib_cm_enabled(struct net_device *dev, struct neighbour *n) 594static inline int ipoib_cm_enabled(struct net_device *dev, u8 *hwaddr)
579 595
580{ 596{
581 return 0; 597 return 0;
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_cm.c b/drivers/infiniband/ulp/ipoib/ipoib_cm.c
index 6d66ab0dd92a..95ecf4eadf5f 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_cm.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_cm.c
@@ -811,9 +811,7 @@ void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
811 if (neigh) { 811 if (neigh) {
812 neigh->cm = NULL; 812 neigh->cm = NULL;
813 list_del(&neigh->list); 813 list_del(&neigh->list);
814 if (neigh->ah) 814 ipoib_neigh_free(neigh);
815 ipoib_put_ah(neigh->ah);
816 ipoib_neigh_free(dev, neigh);
817 815
818 tx->neigh = NULL; 816 tx->neigh = NULL;
819 } 817 }
@@ -1230,9 +1228,7 @@ static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1230 if (neigh) { 1228 if (neigh) {
1231 neigh->cm = NULL; 1229 neigh->cm = NULL;
1232 list_del(&neigh->list); 1230 list_del(&neigh->list);
1233 if (neigh->ah) 1231 ipoib_neigh_free(neigh);
1234 ipoib_put_ah(neigh->ah);
1235 ipoib_neigh_free(dev, neigh);
1236 1232
1237 tx->neigh = NULL; 1233 tx->neigh = NULL;
1238 } 1234 }
@@ -1279,7 +1275,7 @@ void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1279 list_move(&tx->list, &priv->cm.reap_list); 1275 list_move(&tx->list, &priv->cm.reap_list);
1280 queue_work(ipoib_workqueue, &priv->cm.reap_task); 1276 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1281 ipoib_dbg(priv, "Reap connection for gid %pI6\n", 1277 ipoib_dbg(priv, "Reap connection for gid %pI6\n",
1282 tx->neigh->dgid.raw); 1278 tx->neigh->daddr + 4);
1283 tx->neigh = NULL; 1279 tx->neigh = NULL;
1284 } 1280 }
1285} 1281}
@@ -1304,7 +1300,7 @@ static void ipoib_cm_tx_start(struct work_struct *work)
1304 p = list_entry(priv->cm.start_list.next, typeof(*p), list); 1300 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1305 list_del_init(&p->list); 1301 list_del_init(&p->list);
1306 neigh = p->neigh; 1302 neigh = p->neigh;
1307 qpn = IPOIB_QPN(neigh->neighbour->ha); 1303 qpn = IPOIB_QPN(neigh->daddr);
1308 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec); 1304 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1309 1305
1310 spin_unlock_irqrestore(&priv->lock, flags); 1306 spin_unlock_irqrestore(&priv->lock, flags);
@@ -1320,9 +1316,7 @@ static void ipoib_cm_tx_start(struct work_struct *work)
1320 if (neigh) { 1316 if (neigh) {
1321 neigh->cm = NULL; 1317 neigh->cm = NULL;
1322 list_del(&neigh->list); 1318 list_del(&neigh->list);
1323 if (neigh->ah) 1319 ipoib_neigh_free(neigh);
1324 ipoib_put_ah(neigh->ah);
1325 ipoib_neigh_free(dev, neigh);
1326 } 1320 }
1327 list_del(&p->list); 1321 list_del(&p->list);
1328 kfree(p); 1322 kfree(p);
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_main.c b/drivers/infiniband/ulp/ipoib/ipoib_main.c
index bbee4b2d7a13..97920b77a5d0 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_main.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_main.c
@@ -46,7 +46,8 @@
46#include <linux/ip.h> 46#include <linux/ip.h>
47#include <linux/in.h> 47#include <linux/in.h>
48 48
49#include <net/dst.h> 49#include <linux/jhash.h>
50#include <net/arp.h>
50 51
51MODULE_AUTHOR("Roland Dreier"); 52MODULE_AUTHOR("Roland Dreier");
52MODULE_DESCRIPTION("IP-over-InfiniBand net driver"); 53MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
@@ -84,6 +85,7 @@ struct ib_sa_client ipoib_sa_client;
84 85
85static void ipoib_add_one(struct ib_device *device); 86static void ipoib_add_one(struct ib_device *device);
86static void ipoib_remove_one(struct ib_device *device); 87static void ipoib_remove_one(struct ib_device *device);
88static void ipoib_neigh_reclaim(struct rcu_head *rp);
87 89
88static struct ib_client ipoib_client = { 90static struct ib_client ipoib_client = {
89 .name = "ipoib", 91 .name = "ipoib",
@@ -264,30 +266,15 @@ static int __path_add(struct net_device *dev, struct ipoib_path *path)
264 266
265static void path_free(struct net_device *dev, struct ipoib_path *path) 267static void path_free(struct net_device *dev, struct ipoib_path *path)
266{ 268{
267 struct ipoib_dev_priv *priv = netdev_priv(dev);
268 struct ipoib_neigh *neigh, *tn;
269 struct sk_buff *skb; 269 struct sk_buff *skb;
270 unsigned long flags;
271 270
272 while ((skb = __skb_dequeue(&path->queue))) 271 while ((skb = __skb_dequeue(&path->queue)))
273 dev_kfree_skb_irq(skb); 272 dev_kfree_skb_irq(skb);
274 273
275 spin_lock_irqsave(&priv->lock, flags); 274 ipoib_dbg(netdev_priv(dev), "path_free\n");
276
277 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
278 /*
279 * It's safe to call ipoib_put_ah() inside priv->lock
280 * here, because we know that path->ah will always
281 * hold one more reference, so ipoib_put_ah() will
282 * never do more than decrement the ref count.
283 */
284 if (neigh->ah)
285 ipoib_put_ah(neigh->ah);
286
287 ipoib_neigh_free(dev, neigh);
288 }
289 275
290 spin_unlock_irqrestore(&priv->lock, flags); 276 /* remove all neigh connected to this path */
277 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
291 278
292 if (path->ah) 279 if (path->ah)
293 ipoib_put_ah(path->ah); 280 ipoib_put_ah(path->ah);
@@ -458,19 +445,15 @@ static void path_rec_completion(int status,
458 } 445 }
459 kref_get(&path->ah->ref); 446 kref_get(&path->ah->ref);
460 neigh->ah = path->ah; 447 neigh->ah = path->ah;
461 memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
462 sizeof(union ib_gid));
463 448
464 if (ipoib_cm_enabled(dev, neigh->neighbour)) { 449 if (ipoib_cm_enabled(dev, neigh->daddr)) {
465 if (!ipoib_cm_get(neigh)) 450 if (!ipoib_cm_get(neigh))
466 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, 451 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
467 path, 452 path,
468 neigh)); 453 neigh));
469 if (!ipoib_cm_get(neigh)) { 454 if (!ipoib_cm_get(neigh)) {
470 list_del(&neigh->list); 455 list_del(&neigh->list);
471 if (neigh->ah) 456 ipoib_neigh_free(neigh);
472 ipoib_put_ah(neigh->ah);
473 ipoib_neigh_free(dev, neigh);
474 continue; 457 continue;
475 } 458 }
476 } 459 }
@@ -555,15 +538,15 @@ static int path_rec_start(struct net_device *dev,
555 return 0; 538 return 0;
556} 539}
557 540
558/* called with rcu_read_lock */ 541static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
559static void neigh_add_path(struct sk_buff *skb, struct neighbour *n, struct net_device *dev) 542 struct net_device *dev)
560{ 543{
561 struct ipoib_dev_priv *priv = netdev_priv(dev); 544 struct ipoib_dev_priv *priv = netdev_priv(dev);
562 struct ipoib_path *path; 545 struct ipoib_path *path;
563 struct ipoib_neigh *neigh; 546 struct ipoib_neigh *neigh;
564 unsigned long flags; 547 unsigned long flags;
565 548
566 neigh = ipoib_neigh_alloc(n, skb->dev); 549 neigh = ipoib_neigh_alloc(daddr, dev);
567 if (!neigh) { 550 if (!neigh) {
568 ++dev->stats.tx_dropped; 551 ++dev->stats.tx_dropped;
569 dev_kfree_skb_any(skb); 552 dev_kfree_skb_any(skb);
@@ -572,9 +555,9 @@ static void neigh_add_path(struct sk_buff *skb, struct neighbour *n, struct net_
572 555
573 spin_lock_irqsave(&priv->lock, flags); 556 spin_lock_irqsave(&priv->lock, flags);
574 557
575 path = __path_find(dev, n->ha + 4); 558 path = __path_find(dev, daddr + 4);
576 if (!path) { 559 if (!path) {
577 path = path_rec_create(dev, n->ha + 4); 560 path = path_rec_create(dev, daddr + 4);
578 if (!path) 561 if (!path)
579 goto err_path; 562 goto err_path;
580 563
@@ -586,17 +569,13 @@ static void neigh_add_path(struct sk_buff *skb, struct neighbour *n, struct net_
586 if (path->ah) { 569 if (path->ah) {
587 kref_get(&path->ah->ref); 570 kref_get(&path->ah->ref);
588 neigh->ah = path->ah; 571 neigh->ah = path->ah;
589 memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
590 sizeof(union ib_gid));
591 572
592 if (ipoib_cm_enabled(dev, neigh->neighbour)) { 573 if (ipoib_cm_enabled(dev, neigh->daddr)) {
593 if (!ipoib_cm_get(neigh)) 574 if (!ipoib_cm_get(neigh))
594 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh)); 575 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
595 if (!ipoib_cm_get(neigh)) { 576 if (!ipoib_cm_get(neigh)) {
596 list_del(&neigh->list); 577 list_del(&neigh->list);
597 if (neigh->ah) 578 ipoib_neigh_free(neigh);
598 ipoib_put_ah(neigh->ah);
599 ipoib_neigh_free(dev, neigh);
600 goto err_drop; 579 goto err_drop;
601 } 580 }
602 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) 581 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
@@ -608,7 +587,8 @@ static void neigh_add_path(struct sk_buff *skb, struct neighbour *n, struct net_
608 } 587 }
609 } else { 588 } else {
610 spin_unlock_irqrestore(&priv->lock, flags); 589 spin_unlock_irqrestore(&priv->lock, flags);
611 ipoib_send(dev, skb, path->ah, IPOIB_QPN(n->ha)); 590 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
591 ipoib_neigh_put(neigh);
612 return; 592 return;
613 } 593 }
614 } else { 594 } else {
@@ -621,35 +601,20 @@ static void neigh_add_path(struct sk_buff *skb, struct neighbour *n, struct net_
621 } 601 }
622 602
623 spin_unlock_irqrestore(&priv->lock, flags); 603 spin_unlock_irqrestore(&priv->lock, flags);
604 ipoib_neigh_put(neigh);
624 return; 605 return;
625 606
626err_list: 607err_list:
627 list_del(&neigh->list); 608 list_del(&neigh->list);
628 609
629err_path: 610err_path:
630 ipoib_neigh_free(dev, neigh); 611 ipoib_neigh_free(neigh);
631err_drop: 612err_drop:
632 ++dev->stats.tx_dropped; 613 ++dev->stats.tx_dropped;
633 dev_kfree_skb_any(skb); 614 dev_kfree_skb_any(skb);
634 615
635 spin_unlock_irqrestore(&priv->lock, flags); 616 spin_unlock_irqrestore(&priv->lock, flags);
636} 617 ipoib_neigh_put(neigh);
637
638/* called with rcu_read_lock */
639static void ipoib_path_lookup(struct sk_buff *skb, struct neighbour *n, struct net_device *dev)
640{
641 struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
642
643 /* Look up path record for unicasts */
644 if (n->ha[4] != 0xff) {
645 neigh_add_path(skb, n, dev);
646 return;
647 }
648
649 /* Add in the P_Key for multicasts */
650 n->ha[8] = (priv->pkey >> 8) & 0xff;
651 n->ha[9] = priv->pkey & 0xff;
652 ipoib_mcast_send(dev, n->ha + 4, skb);
653} 618}
654 619
655static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev, 620static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
@@ -710,96 +675,80 @@ static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
710{ 675{
711 struct ipoib_dev_priv *priv = netdev_priv(dev); 676 struct ipoib_dev_priv *priv = netdev_priv(dev);
712 struct ipoib_neigh *neigh; 677 struct ipoib_neigh *neigh;
713 struct neighbour *n = NULL; 678 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
679 struct ipoib_header *header;
714 unsigned long flags; 680 unsigned long flags;
715 681
716 rcu_read_lock(); 682 header = (struct ipoib_header *) skb->data;
717 if (likely(skb_dst(skb))) { 683
718 n = dst_neigh_lookup_skb(skb_dst(skb), skb); 684 if (unlikely(cb->hwaddr[4] == 0xff)) {
719 if (!n) { 685 /* multicast, arrange "if" according to probability */
686 if ((header->proto != htons(ETH_P_IP)) &&
687 (header->proto != htons(ETH_P_IPV6)) &&
688 (header->proto != htons(ETH_P_ARP)) &&
689 (header->proto != htons(ETH_P_RARP))) {
690 /* ethertype not supported by IPoIB */
720 ++dev->stats.tx_dropped; 691 ++dev->stats.tx_dropped;
721 dev_kfree_skb_any(skb); 692 dev_kfree_skb_any(skb);
722 goto unlock; 693 return NETDEV_TX_OK;
723 } 694 }
695 /* Add in the P_Key for multicast*/
696 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
697 cb->hwaddr[9] = priv->pkey & 0xff;
698
699 neigh = ipoib_neigh_get(dev, cb->hwaddr);
700 if (likely(neigh))
701 goto send_using_neigh;
702 ipoib_mcast_send(dev, cb->hwaddr, skb);
703 return NETDEV_TX_OK;
724 } 704 }
725 if (likely(n)) {
726 if (unlikely(!*to_ipoib_neigh(n))) {
727 ipoib_path_lookup(skb, n, dev);
728 goto unlock;
729 }
730
731 neigh = *to_ipoib_neigh(n);
732 705
733 if (unlikely((memcmp(&neigh->dgid.raw, 706 /* unicast, arrange "switch" according to probability */
734 n->ha + 4, 707 switch (header->proto) {
735 sizeof(union ib_gid))) || 708 case htons(ETH_P_IP):
736 (neigh->dev != dev))) { 709 case htons(ETH_P_IPV6):
737 spin_lock_irqsave(&priv->lock, flags); 710 neigh = ipoib_neigh_get(dev, cb->hwaddr);
738 /* 711 if (unlikely(!neigh)) {
739 * It's safe to call ipoib_put_ah() inside 712 neigh_add_path(skb, cb->hwaddr, dev);
740 * priv->lock here, because we know that 713 return NETDEV_TX_OK;
741 * path->ah will always hold one more reference,
742 * so ipoib_put_ah() will never do more than
743 * decrement the ref count.
744 */
745 if (neigh->ah)
746 ipoib_put_ah(neigh->ah);
747 list_del(&neigh->list);
748 ipoib_neigh_free(dev, neigh);
749 spin_unlock_irqrestore(&priv->lock, flags);
750 ipoib_path_lookup(skb, n, dev);
751 goto unlock;
752 } 714 }
715 break;
716 case htons(ETH_P_ARP):
717 case htons(ETH_P_RARP):
718 /* for unicast ARP and RARP should always perform path find */
719 unicast_arp_send(skb, dev, cb);
720 return NETDEV_TX_OK;
721 default:
722 /* ethertype not supported by IPoIB */
723 ++dev->stats.tx_dropped;
724 dev_kfree_skb_any(skb);
725 return NETDEV_TX_OK;
726 }
753 727
754 if (ipoib_cm_get(neigh)) { 728send_using_neigh:
755 if (ipoib_cm_up(neigh)) { 729 /* note we now hold a ref to neigh */
756 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh)); 730 if (ipoib_cm_get(neigh)) {
757 goto unlock; 731 if (ipoib_cm_up(neigh)) {
758 } 732 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
759 } else if (neigh->ah) { 733 goto unref;
760 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(n->ha));
761 goto unlock;
762 } 734 }
735 } else if (neigh->ah) {
736 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
737 goto unref;
738 }
763 739
764 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) { 740 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
765 spin_lock_irqsave(&priv->lock, flags); 741 spin_lock_irqsave(&priv->lock, flags);
766 __skb_queue_tail(&neigh->queue, skb); 742 __skb_queue_tail(&neigh->queue, skb);
767 spin_unlock_irqrestore(&priv->lock, flags); 743 spin_unlock_irqrestore(&priv->lock, flags);
768 } else {
769 ++dev->stats.tx_dropped;
770 dev_kfree_skb_any(skb);
771 }
772 } else { 744 } else {
773 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb; 745 ++dev->stats.tx_dropped;
774 746 dev_kfree_skb_any(skb);
775 if (cb->hwaddr[4] == 0xff) { 747 }
776 /* Add in the P_Key for multicast*/
777 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
778 cb->hwaddr[9] = priv->pkey & 0xff;
779 748
780 ipoib_mcast_send(dev, cb->hwaddr + 4, skb); 749unref:
781 } else { 750 ipoib_neigh_put(neigh);
782 /* unicast GID -- should be ARP or RARP reply */
783
784 if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
785 (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
786 ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x %pI6\n",
787 skb_dst(skb) ? "neigh" : "dst",
788 be16_to_cpup((__be16 *) skb->data),
789 IPOIB_QPN(cb->hwaddr),
790 cb->hwaddr + 4);
791 dev_kfree_skb_any(skb);
792 ++dev->stats.tx_dropped;
793 goto unlock;
794 }
795 751
796 unicast_arp_send(skb, dev, cb);
797 }
798 }
799unlock:
800 if (n)
801 neigh_release(n);
802 rcu_read_unlock();
803 return NETDEV_TX_OK; 752 return NETDEV_TX_OK;
804} 753}
805 754
@@ -821,6 +770,7 @@ static int ipoib_hard_header(struct sk_buff *skb,
821 const void *daddr, const void *saddr, unsigned len) 770 const void *daddr, const void *saddr, unsigned len)
822{ 771{
823 struct ipoib_header *header; 772 struct ipoib_header *header;
773 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
824 774
825 header = (struct ipoib_header *) skb_push(skb, sizeof *header); 775 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
826 776
@@ -828,14 +778,11 @@ static int ipoib_hard_header(struct sk_buff *skb,
828 header->reserved = 0; 778 header->reserved = 0;
829 779
830 /* 780 /*
831 * If we don't have a dst_entry structure, stuff the 781 * we don't rely on dst_entry structure, always stuff the
832 * destination address into skb->cb so we can figure out where 782 * destination address into skb->cb so we can figure out where
833 * to send the packet later. 783 * to send the packet later.
834 */ 784 */
835 if (!skb_dst(skb)) { 785 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
836 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
837 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
838 }
839 786
840 return 0; 787 return 0;
841} 788}
@@ -852,86 +799,438 @@ static void ipoib_set_mcast_list(struct net_device *dev)
852 queue_work(ipoib_workqueue, &priv->restart_task); 799 queue_work(ipoib_workqueue, &priv->restart_task);
853} 800}
854 801
855static void ipoib_neigh_cleanup(struct neighbour *n) 802static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
856{ 803{
857 struct ipoib_neigh *neigh; 804 /*
858 struct ipoib_dev_priv *priv = netdev_priv(n->dev); 805 * Use only the address parts that contributes to spreading
806 * The subnet prefix is not used as one can not connect to
807 * same remote port (GUID) using the same remote QPN via two
808 * different subnets.
809 */
810 /* qpn octets[1:4) & port GUID octets[12:20) */
811 u32 *daddr_32 = (u32 *) daddr;
812 u32 hv;
813
814 hv = jhash_3words(daddr_32[3], daddr_32[4], 0xFFFFFF & daddr_32[0], 0);
815 return hv & htbl->mask;
816}
817
818struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
819{
820 struct ipoib_dev_priv *priv = netdev_priv(dev);
821 struct ipoib_neigh_table *ntbl = &priv->ntbl;
822 struct ipoib_neigh_hash *htbl;
823 struct ipoib_neigh *neigh = NULL;
824 u32 hash_val;
825
826 rcu_read_lock_bh();
827
828 htbl = rcu_dereference_bh(ntbl->htbl);
829
830 if (!htbl)
831 goto out_unlock;
832
833 hash_val = ipoib_addr_hash(htbl, daddr);
834 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
835 neigh != NULL;
836 neigh = rcu_dereference_bh(neigh->hnext)) {
837 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
838 /* found, take one ref on behalf of the caller */
839 if (!atomic_inc_not_zero(&neigh->refcnt)) {
840 /* deleted */
841 neigh = NULL;
842 goto out_unlock;
843 }
844 neigh->alive = jiffies;
845 goto out_unlock;
846 }
847 }
848
849out_unlock:
850 rcu_read_unlock_bh();
851 return neigh;
852}
853
854static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
855{
856 struct ipoib_neigh_table *ntbl = &priv->ntbl;
857 struct ipoib_neigh_hash *htbl;
858 unsigned long neigh_obsolete;
859 unsigned long dt;
859 unsigned long flags; 860 unsigned long flags;
860 struct ipoib_ah *ah = NULL; 861 int i;
861 862
862 neigh = *to_ipoib_neigh(n); 863 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
863 if (neigh)
864 priv = netdev_priv(neigh->dev);
865 else
866 return; 864 return;
867 ipoib_dbg(priv,
868 "neigh_cleanup for %06x %pI6\n",
869 IPOIB_QPN(n->ha),
870 n->ha + 4);
871 865
872 spin_lock_irqsave(&priv->lock, flags); 866 write_lock_bh(&ntbl->rwlock);
867
868 htbl = rcu_dereference_protected(ntbl->htbl,
869 lockdep_is_held(&ntbl->rwlock));
870
871 if (!htbl)
872 goto out_unlock;
873
874 /* neigh is obsolete if it was idle for two GC periods */
875 dt = 2 * arp_tbl.gc_interval;
876 neigh_obsolete = jiffies - dt;
877 /* handle possible race condition */
878 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
879 goto out_unlock;
880
881 for (i = 0; i < htbl->size; i++) {
882 struct ipoib_neigh *neigh;
883 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
884
885 while ((neigh = rcu_dereference_protected(*np,
886 lockdep_is_held(&ntbl->rwlock))) != NULL) {
887 /* was the neigh idle for two GC periods */
888 if (time_after(neigh_obsolete, neigh->alive)) {
889 rcu_assign_pointer(*np,
890 rcu_dereference_protected(neigh->hnext,
891 lockdep_is_held(&ntbl->rwlock)));
892 /* remove from path/mc list */
893 spin_lock_irqsave(&priv->lock, flags);
894 list_del(&neigh->list);
895 spin_unlock_irqrestore(&priv->lock, flags);
896 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
897 } else {
898 np = &neigh->hnext;
899 }
873 900
874 if (neigh->ah) 901 }
875 ah = neigh->ah; 902 }
876 list_del(&neigh->list);
877 ipoib_neigh_free(n->dev, neigh);
878 903
879 spin_unlock_irqrestore(&priv->lock, flags); 904out_unlock:
905 write_unlock_bh(&ntbl->rwlock);
906}
880 907
881 if (ah) 908static void ipoib_reap_neigh(struct work_struct *work)
882 ipoib_put_ah(ah); 909{
910 struct ipoib_dev_priv *priv =
911 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
912
913 __ipoib_reap_neigh(priv);
914
915 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
916 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
917 arp_tbl.gc_interval);
883} 918}
884 919
885struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neighbour, 920
921static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
886 struct net_device *dev) 922 struct net_device *dev)
887{ 923{
888 struct ipoib_neigh *neigh; 924 struct ipoib_neigh *neigh;
889 925
890 neigh = kmalloc(sizeof *neigh, GFP_ATOMIC); 926 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
891 if (!neigh) 927 if (!neigh)
892 return NULL; 928 return NULL;
893 929
894 neigh->neighbour = neighbour;
895 neigh->dev = dev; 930 neigh->dev = dev;
896 memset(&neigh->dgid.raw, 0, sizeof (union ib_gid)); 931 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
897 *to_ipoib_neigh(neighbour) = neigh;
898 skb_queue_head_init(&neigh->queue); 932 skb_queue_head_init(&neigh->queue);
933 INIT_LIST_HEAD(&neigh->list);
899 ipoib_cm_set(neigh, NULL); 934 ipoib_cm_set(neigh, NULL);
935 /* one ref on behalf of the caller */
936 atomic_set(&neigh->refcnt, 1);
937
938 return neigh;
939}
940
941struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
942 struct net_device *dev)
943{
944 struct ipoib_dev_priv *priv = netdev_priv(dev);
945 struct ipoib_neigh_table *ntbl = &priv->ntbl;
946 struct ipoib_neigh_hash *htbl;
947 struct ipoib_neigh *neigh;
948 u32 hash_val;
949
950 write_lock_bh(&ntbl->rwlock);
951
952 htbl = rcu_dereference_protected(ntbl->htbl,
953 lockdep_is_held(&ntbl->rwlock));
954 if (!htbl) {
955 neigh = NULL;
956 goto out_unlock;
957 }
958
959 /* need to add a new neigh, but maybe some other thread succeeded?
960 * recalc hash, maybe hash resize took place so we do a search
961 */
962 hash_val = ipoib_addr_hash(htbl, daddr);
963 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
964 lockdep_is_held(&ntbl->rwlock));
965 neigh != NULL;
966 neigh = rcu_dereference_protected(neigh->hnext,
967 lockdep_is_held(&ntbl->rwlock))) {
968 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
969 /* found, take one ref on behalf of the caller */
970 if (!atomic_inc_not_zero(&neigh->refcnt)) {
971 /* deleted */
972 neigh = NULL;
973 break;
974 }
975 neigh->alive = jiffies;
976 goto out_unlock;
977 }
978 }
979
980 neigh = ipoib_neigh_ctor(daddr, dev);
981 if (!neigh)
982 goto out_unlock;
983
984 /* one ref on behalf of the hash table */
985 atomic_inc(&neigh->refcnt);
986 neigh->alive = jiffies;
987 /* put in hash */
988 rcu_assign_pointer(neigh->hnext,
989 rcu_dereference_protected(htbl->buckets[hash_val],
990 lockdep_is_held(&ntbl->rwlock)));
991 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
992 atomic_inc(&ntbl->entries);
993
994out_unlock:
995 write_unlock_bh(&ntbl->rwlock);
900 996
901 return neigh; 997 return neigh;
902} 998}
903 999
904void ipoib_neigh_free(struct net_device *dev, struct ipoib_neigh *neigh) 1000void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
905{ 1001{
1002 /* neigh reference count was dropprd to zero */
1003 struct net_device *dev = neigh->dev;
1004 struct ipoib_dev_priv *priv = netdev_priv(dev);
906 struct sk_buff *skb; 1005 struct sk_buff *skb;
907 *to_ipoib_neigh(neigh->neighbour) = NULL; 1006 if (neigh->ah)
1007 ipoib_put_ah(neigh->ah);
908 while ((skb = __skb_dequeue(&neigh->queue))) { 1008 while ((skb = __skb_dequeue(&neigh->queue))) {
909 ++dev->stats.tx_dropped; 1009 ++dev->stats.tx_dropped;
910 dev_kfree_skb_any(skb); 1010 dev_kfree_skb_any(skb);
911 } 1011 }
912 if (ipoib_cm_get(neigh)) 1012 if (ipoib_cm_get(neigh))
913 ipoib_cm_destroy_tx(ipoib_cm_get(neigh)); 1013 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1014 ipoib_dbg(netdev_priv(dev),
1015 "neigh free for %06x %pI6\n",
1016 IPOIB_QPN(neigh->daddr),
1017 neigh->daddr + 4);
914 kfree(neigh); 1018 kfree(neigh);
1019 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1020 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1021 complete(&priv->ntbl.flushed);
1022 }
1023}
1024
1025static void ipoib_neigh_reclaim(struct rcu_head *rp)
1026{
1027 /* Called as a result of removal from hash table */
1028 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1029 /* note TX context may hold another ref */
1030 ipoib_neigh_put(neigh);
915} 1031}
916 1032
917static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms) 1033void ipoib_neigh_free(struct ipoib_neigh *neigh)
918{ 1034{
919 parms->neigh_cleanup = ipoib_neigh_cleanup; 1035 struct net_device *dev = neigh->dev;
1036 struct ipoib_dev_priv *priv = netdev_priv(dev);
1037 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1038 struct ipoib_neigh_hash *htbl;
1039 struct ipoib_neigh __rcu **np;
1040 struct ipoib_neigh *n;
1041 u32 hash_val;
1042
1043 write_lock_bh(&ntbl->rwlock);
1044
1045 htbl = rcu_dereference_protected(ntbl->htbl,
1046 lockdep_is_held(&ntbl->rwlock));
1047 if (!htbl)
1048 goto out_unlock;
1049
1050 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1051 np = &htbl->buckets[hash_val];
1052 for (n = rcu_dereference_protected(*np,
1053 lockdep_is_held(&ntbl->rwlock));
1054 n != NULL;
1055 n = rcu_dereference_protected(neigh->hnext,
1056 lockdep_is_held(&ntbl->rwlock))) {
1057 if (n == neigh) {
1058 /* found */
1059 rcu_assign_pointer(*np,
1060 rcu_dereference_protected(neigh->hnext,
1061 lockdep_is_held(&ntbl->rwlock)));
1062 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1063 goto out_unlock;
1064 } else {
1065 np = &n->hnext;
1066 }
1067 }
1068
1069out_unlock:
1070 write_unlock_bh(&ntbl->rwlock);
1071
1072}
1073
1074static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1075{
1076 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1077 struct ipoib_neigh_hash *htbl;
1078 struct ipoib_neigh **buckets;
1079 u32 size;
1080
1081 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1082 ntbl->htbl = NULL;
1083 rwlock_init(&ntbl->rwlock);
1084 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1085 if (!htbl)
1086 return -ENOMEM;
1087 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1088 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1089 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1090 if (!buckets) {
1091 kfree(htbl);
1092 return -ENOMEM;
1093 }
1094 htbl->size = size;
1095 htbl->mask = (size - 1);
1096 htbl->buckets = buckets;
1097 ntbl->htbl = htbl;
1098 atomic_set(&ntbl->entries, 0);
1099
1100 /* start garbage collection */
1101 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1102 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
1103 arp_tbl.gc_interval);
920 1104
921 return 0; 1105 return 0;
922} 1106}
923 1107
1108static void neigh_hash_free_rcu(struct rcu_head *head)
1109{
1110 struct ipoib_neigh_hash *htbl = container_of(head,
1111 struct ipoib_neigh_hash,
1112 rcu);
1113 struct ipoib_neigh __rcu **buckets = htbl->buckets;
1114
1115 kfree(buckets);
1116 kfree(htbl);
1117}
1118
1119void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1120{
1121 struct ipoib_dev_priv *priv = netdev_priv(dev);
1122 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1123 struct ipoib_neigh_hash *htbl;
1124 unsigned long flags;
1125 int i;
1126
1127 /* remove all neigh connected to a given path or mcast */
1128 write_lock_bh(&ntbl->rwlock);
1129
1130 htbl = rcu_dereference_protected(ntbl->htbl,
1131 lockdep_is_held(&ntbl->rwlock));
1132
1133 if (!htbl)
1134 goto out_unlock;
1135
1136 for (i = 0; i < htbl->size; i++) {
1137 struct ipoib_neigh *neigh;
1138 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1139
1140 while ((neigh = rcu_dereference_protected(*np,
1141 lockdep_is_held(&ntbl->rwlock))) != NULL) {
1142 /* delete neighs belong to this parent */
1143 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1144 rcu_assign_pointer(*np,
1145 rcu_dereference_protected(neigh->hnext,
1146 lockdep_is_held(&ntbl->rwlock)));
1147 /* remove from parent list */
1148 spin_lock_irqsave(&priv->lock, flags);
1149 list_del(&neigh->list);
1150 spin_unlock_irqrestore(&priv->lock, flags);
1151 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1152 } else {
1153 np = &neigh->hnext;
1154 }
1155
1156 }
1157 }
1158out_unlock:
1159 write_unlock_bh(&ntbl->rwlock);
1160}
1161
1162static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1163{
1164 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1165 struct ipoib_neigh_hash *htbl;
1166 unsigned long flags;
1167 int i;
1168
1169 write_lock_bh(&ntbl->rwlock);
1170
1171 htbl = rcu_dereference_protected(ntbl->htbl,
1172 lockdep_is_held(&ntbl->rwlock));
1173 if (!htbl)
1174 goto out_unlock;
1175
1176 for (i = 0; i < htbl->size; i++) {
1177 struct ipoib_neigh *neigh;
1178 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1179
1180 while ((neigh = rcu_dereference_protected(*np,
1181 lockdep_is_held(&ntbl->rwlock))) != NULL) {
1182 rcu_assign_pointer(*np,
1183 rcu_dereference_protected(neigh->hnext,
1184 lockdep_is_held(&ntbl->rwlock)));
1185 /* remove from path/mc list */
1186 spin_lock_irqsave(&priv->lock, flags);
1187 list_del(&neigh->list);
1188 spin_unlock_irqrestore(&priv->lock, flags);
1189 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1190 }
1191 }
1192
1193 rcu_assign_pointer(ntbl->htbl, NULL);
1194 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1195
1196out_unlock:
1197 write_unlock_bh(&ntbl->rwlock);
1198}
1199
1200static void ipoib_neigh_hash_uninit(struct net_device *dev)
1201{
1202 struct ipoib_dev_priv *priv = netdev_priv(dev);
1203 int stopped;
1204
1205 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
1206 init_completion(&priv->ntbl.flushed);
1207 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1208
1209 /* Stop GC if called at init fail need to cancel work */
1210 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1211 if (!stopped)
1212 cancel_delayed_work(&priv->neigh_reap_task);
1213
1214 if (atomic_read(&priv->ntbl.entries)) {
1215 ipoib_flush_neighs(priv);
1216 wait_for_completion(&priv->ntbl.flushed);
1217 }
1218}
1219
1220
924int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port) 1221int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
925{ 1222{
926 struct ipoib_dev_priv *priv = netdev_priv(dev); 1223 struct ipoib_dev_priv *priv = netdev_priv(dev);
927 1224
1225 if (ipoib_neigh_hash_init(priv) < 0)
1226 goto out;
928 /* Allocate RX/TX "rings" to hold queued skbs */ 1227 /* Allocate RX/TX "rings" to hold queued skbs */
929 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring, 1228 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
930 GFP_KERNEL); 1229 GFP_KERNEL);
931 if (!priv->rx_ring) { 1230 if (!priv->rx_ring) {
932 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n", 1231 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
933 ca->name, ipoib_recvq_size); 1232 ca->name, ipoib_recvq_size);
934 goto out; 1233 goto out_neigh_hash_cleanup;
935 } 1234 }
936 1235
937 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring); 1236 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
@@ -954,6 +1253,8 @@ out_tx_ring_cleanup:
954out_rx_ring_cleanup: 1253out_rx_ring_cleanup:
955 kfree(priv->rx_ring); 1254 kfree(priv->rx_ring);
956 1255
1256out_neigh_hash_cleanup:
1257 ipoib_neigh_hash_uninit(dev);
957out: 1258out:
958 return -ENOMEM; 1259 return -ENOMEM;
959} 1260}
@@ -966,6 +1267,9 @@ void ipoib_dev_cleanup(struct net_device *dev)
966 1267
967 /* Delete any child interfaces first */ 1268 /* Delete any child interfaces first */
968 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) { 1269 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1270 /* Stop GC on child */
1271 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1272 cancel_delayed_work(&cpriv->neigh_reap_task);
969 unregister_netdev(cpriv->dev); 1273 unregister_netdev(cpriv->dev);
970 ipoib_dev_cleanup(cpriv->dev); 1274 ipoib_dev_cleanup(cpriv->dev);
971 free_netdev(cpriv->dev); 1275 free_netdev(cpriv->dev);
@@ -978,6 +1282,8 @@ void ipoib_dev_cleanup(struct net_device *dev)
978 1282
979 priv->rx_ring = NULL; 1283 priv->rx_ring = NULL;
980 priv->tx_ring = NULL; 1284 priv->tx_ring = NULL;
1285
1286 ipoib_neigh_hash_uninit(dev);
981} 1287}
982 1288
983static const struct header_ops ipoib_header_ops = { 1289static const struct header_ops ipoib_header_ops = {
@@ -992,7 +1298,6 @@ static const struct net_device_ops ipoib_netdev_ops = {
992 .ndo_start_xmit = ipoib_start_xmit, 1298 .ndo_start_xmit = ipoib_start_xmit,
993 .ndo_tx_timeout = ipoib_timeout, 1299 .ndo_tx_timeout = ipoib_timeout,
994 .ndo_set_rx_mode = ipoib_set_mcast_list, 1300 .ndo_set_rx_mode = ipoib_set_mcast_list,
995 .ndo_neigh_setup = ipoib_neigh_setup_dev,
996}; 1301};
997 1302
998static void ipoib_setup(struct net_device *dev) 1303static void ipoib_setup(struct net_device *dev)
@@ -1041,6 +1346,7 @@ static void ipoib_setup(struct net_device *dev)
1041 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy); 1346 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
1042 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task); 1347 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1043 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah); 1348 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1349 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1044} 1350}
1045 1351
1046struct ipoib_dev_priv *ipoib_intf_alloc(const char *name) 1352struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
@@ -1281,6 +1587,9 @@ sysfs_failed:
1281 1587
1282register_failed: 1588register_failed:
1283 ib_unregister_event_handler(&priv->event_handler); 1589 ib_unregister_event_handler(&priv->event_handler);
1590 /* Stop GC if started before flush */
1591 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1592 cancel_delayed_work(&priv->neigh_reap_task);
1284 flush_workqueue(ipoib_workqueue); 1593 flush_workqueue(ipoib_workqueue);
1285 1594
1286event_failed: 1595event_failed:
@@ -1347,6 +1656,9 @@ static void ipoib_remove_one(struct ib_device *device)
1347 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); 1656 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1348 rtnl_unlock(); 1657 rtnl_unlock();
1349 1658
1659 /* Stop GC */
1660 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1661 cancel_delayed_work(&priv->neigh_reap_task);
1350 flush_workqueue(ipoib_workqueue); 1662 flush_workqueue(ipoib_workqueue);
1351 1663
1352 unregister_netdev(priv->dev); 1664 unregister_netdev(priv->dev);
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
index 7cecb16d3d48..13f4aa7593c8 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
@@ -69,28 +69,13 @@ struct ipoib_mcast_iter {
69static void ipoib_mcast_free(struct ipoib_mcast *mcast) 69static void ipoib_mcast_free(struct ipoib_mcast *mcast)
70{ 70{
71 struct net_device *dev = mcast->dev; 71 struct net_device *dev = mcast->dev;
72 struct ipoib_dev_priv *priv = netdev_priv(dev);
73 struct ipoib_neigh *neigh, *tmp;
74 int tx_dropped = 0; 72 int tx_dropped = 0;
75 73
76 ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n", 74 ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
77 mcast->mcmember.mgid.raw); 75 mcast->mcmember.mgid.raw);
78 76
79 spin_lock_irq(&priv->lock); 77 /* remove all neigh connected to this mcast */
80 78 ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
81 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
82 /*
83 * It's safe to call ipoib_put_ah() inside priv->lock
84 * here, because we know that mcast->ah will always
85 * hold one more reference, so ipoib_put_ah() will
86 * never do more than decrement the ref count.
87 */
88 if (neigh->ah)
89 ipoib_put_ah(neigh->ah);
90 ipoib_neigh_free(dev, neigh);
91 }
92
93 spin_unlock_irq(&priv->lock);
94 79
95 if (mcast->ah) 80 if (mcast->ah)
96 ipoib_put_ah(mcast->ah); 81 ipoib_put_ah(mcast->ah);
@@ -655,17 +640,12 @@ static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
655 return 0; 640 return 0;
656} 641}
657 642
658void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb) 643void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
659{ 644{
660 struct ipoib_dev_priv *priv = netdev_priv(dev); 645 struct ipoib_dev_priv *priv = netdev_priv(dev);
661 struct dst_entry *dst = skb_dst(skb);
662 struct ipoib_mcast *mcast; 646 struct ipoib_mcast *mcast;
663 struct neighbour *n;
664 unsigned long flags; 647 unsigned long flags;
665 648 void *mgid = daddr + 4;
666 n = NULL;
667 if (dst)
668 n = dst_neigh_lookup_skb(dst, skb);
669 649
670 spin_lock_irqsave(&priv->lock, flags); 650 spin_lock_irqsave(&priv->lock, flags);
671 651
@@ -721,28 +701,29 @@ void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
721 701
722out: 702out:
723 if (mcast && mcast->ah) { 703 if (mcast && mcast->ah) {
724 if (n) { 704 struct ipoib_neigh *neigh;
725 if (!*to_ipoib_neigh(n)) { 705
726 struct ipoib_neigh *neigh; 706 spin_unlock_irqrestore(&priv->lock, flags);
727 707 neigh = ipoib_neigh_get(dev, daddr);
728 neigh = ipoib_neigh_alloc(n, skb->dev); 708 spin_lock_irqsave(&priv->lock, flags);
729 if (neigh) { 709 if (!neigh) {
730 kref_get(&mcast->ah->ref); 710 spin_unlock_irqrestore(&priv->lock, flags);
731 neigh->ah = mcast->ah; 711 neigh = ipoib_neigh_alloc(daddr, dev);
732 list_add_tail(&neigh->list, 712 spin_lock_irqsave(&priv->lock, flags);
733 &mcast->neigh_list); 713 if (neigh) {
734 } 714 kref_get(&mcast->ah->ref);
715 neigh->ah = mcast->ah;
716 list_add_tail(&neigh->list, &mcast->neigh_list);
735 } 717 }
736 neigh_release(n);
737 } 718 }
738 spin_unlock_irqrestore(&priv->lock, flags); 719 spin_unlock_irqrestore(&priv->lock, flags);
739 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN); 720 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
721 if (neigh)
722 ipoib_neigh_put(neigh);
740 return; 723 return;
741 } 724 }
742 725
743unlock: 726unlock:
744 if (n)
745 neigh_release(n);
746 spin_unlock_irqrestore(&priv->lock, flags); 727 spin_unlock_irqrestore(&priv->lock, flags);
747} 728}
748 729
diff --git a/drivers/input/serio/hp_sdc.c b/drivers/input/serio/hp_sdc.c
index 09a089996ded..d7a7e54f6465 100644
--- a/drivers/input/serio/hp_sdc.c
+++ b/drivers/input/serio/hp_sdc.c
@@ -878,7 +878,7 @@ static int __init hp_sdc_init(void)
878#endif 878#endif
879 879
880 errstr = "IRQ not available for"; 880 errstr = "IRQ not available for";
881 if (request_irq(hp_sdc.irq, &hp_sdc_isr, IRQF_SHARED|IRQF_SAMPLE_RANDOM, 881 if (request_irq(hp_sdc.irq, &hp_sdc_isr, IRQF_SHARED,
882 "HP SDC", &hp_sdc)) 882 "HP SDC", &hp_sdc))
883 goto err1; 883 goto err1;
884 884
diff --git a/drivers/isdn/hardware/mISDN/avmfritz.c b/drivers/isdn/hardware/mISDN/avmfritz.c
index c08fc605e56b..fa6ca4733725 100644
--- a/drivers/isdn/hardware/mISDN/avmfritz.c
+++ b/drivers/isdn/hardware/mISDN/avmfritz.c
@@ -449,7 +449,8 @@ hdlc_fill_fifo(struct bchannel *bch)
449{ 449{
450 struct fritzcard *fc = bch->hw; 450 struct fritzcard *fc = bch->hw;
451 struct hdlc_hw *hdlc; 451 struct hdlc_hw *hdlc;
452 int count, fs, cnt = 0, idx, fillempty = 0; 452 int count, fs, cnt = 0, idx;
453 bool fillempty = false;
453 u8 *p; 454 u8 *p;
454 u32 *ptr, val, addr; 455 u32 *ptr, val, addr;
455 456
@@ -462,7 +463,7 @@ hdlc_fill_fifo(struct bchannel *bch)
462 return; 463 return;
463 count = fs; 464 count = fs;
464 p = bch->fill; 465 p = bch->fill;
465 fillempty = 1; 466 fillempty = true;
466 } else { 467 } else {
467 count = bch->tx_skb->len - bch->tx_idx; 468 count = bch->tx_skb->len - bch->tx_idx;
468 if (count <= 0) 469 if (count <= 0)
@@ -477,7 +478,7 @@ hdlc_fill_fifo(struct bchannel *bch)
477 hdlc->ctrl.sr.cmd |= HDLC_CMD_XME; 478 hdlc->ctrl.sr.cmd |= HDLC_CMD_XME;
478 } 479 }
479 ptr = (u32 *)p; 480 ptr = (u32 *)p;
480 if (fillempty) { 481 if (!fillempty) {
481 pr_debug("%s.B%d: %d/%d/%d", fc->name, bch->nr, count, 482 pr_debug("%s.B%d: %d/%d/%d", fc->name, bch->nr, count,
482 bch->tx_idx, bch->tx_skb->len); 483 bch->tx_idx, bch->tx_skb->len);
483 bch->tx_idx += count; 484 bch->tx_idx += count;
diff --git a/drivers/media/common/tuners/tuner-xc2028.c b/drivers/media/common/tuners/tuner-xc2028.c
index f88f948efee2..ea0550eafe7d 100644
--- a/drivers/media/common/tuners/tuner-xc2028.c
+++ b/drivers/media/common/tuners/tuner-xc2028.c
@@ -756,7 +756,7 @@ retry:
756 * No need to reload base firmware if it matches and if the tuner 756 * No need to reload base firmware if it matches and if the tuner
757 * is not at sleep mode 757 * is not at sleep mode
758 */ 758 */
759 if ((priv->state = XC2028_ACTIVE) && 759 if ((priv->state == XC2028_ACTIVE) &&
760 (((BASE | new_fw.type) & BASE_TYPES) == 760 (((BASE | new_fw.type) & BASE_TYPES) ==
761 (priv->cur_fw.type & BASE_TYPES))) { 761 (priv->cur_fw.type & BASE_TYPES))) {
762 tuner_dbg("BASE firmware not changed.\n"); 762 tuner_dbg("BASE firmware not changed.\n");
@@ -978,7 +978,7 @@ static int xc2028_get_afc(struct dvb_frontend *fe, s32 *afc)
978 /* Get AFC */ 978 /* Get AFC */
979 rc = xc2028_get_reg(priv, XREG_FREQ_ERROR, &afc_reg); 979 rc = xc2028_get_reg(priv, XREG_FREQ_ERROR, &afc_reg);
980 if (rc < 0) 980 if (rc < 0)
981 return rc; 981 goto ret;
982 982
983 *afc = afc_reg * 15625; /* Hz */ 983 *afc = afc_reg * 15625; /* Hz */
984 984
diff --git a/drivers/media/common/tuners/xc5000.c b/drivers/media/common/tuners/xc5000.c
index bac8009e1d49..362a8d7c9738 100644
--- a/drivers/media/common/tuners/xc5000.c
+++ b/drivers/media/common/tuners/xc5000.c
@@ -210,13 +210,15 @@ struct xc5000_fw_cfg {
210 u16 size; 210 u16 size;
211}; 211};
212 212
213#define XC5000A_FIRMWARE "dvb-fe-xc5000-1.6.114.fw"
213static const struct xc5000_fw_cfg xc5000a_1_6_114 = { 214static const struct xc5000_fw_cfg xc5000a_1_6_114 = {
214 .name = "dvb-fe-xc5000-1.6.114.fw", 215 .name = XC5000A_FIRMWARE,
215 .size = 12401, 216 .size = 12401,
216}; 217};
217 218
219#define XC5000C_FIRMWARE "dvb-fe-xc5000c-41.024.5.fw"
218static const struct xc5000_fw_cfg xc5000c_41_024_5 = { 220static const struct xc5000_fw_cfg xc5000c_41_024_5 = {
219 .name = "dvb-fe-xc5000c-41.024.5.fw", 221 .name = XC5000C_FIRMWARE,
220 .size = 16497, 222 .size = 16497,
221}; 223};
222 224
@@ -1259,3 +1261,5 @@ EXPORT_SYMBOL(xc5000_attach);
1259MODULE_AUTHOR("Steven Toth"); 1261MODULE_AUTHOR("Steven Toth");
1260MODULE_DESCRIPTION("Xceive xc5000 silicon tuner driver"); 1262MODULE_DESCRIPTION("Xceive xc5000 silicon tuner driver");
1261MODULE_LICENSE("GPL"); 1263MODULE_LICENSE("GPL");
1264MODULE_FIRMWARE(XC5000A_FIRMWARE);
1265MODULE_FIRMWARE(XC5000C_FIRMWARE);
diff --git a/drivers/media/dvb/dvb-usb/az6007.c b/drivers/media/dvb/dvb-usb/az6007.c
index 8ffcad000ad3..86861e6f86d2 100644
--- a/drivers/media/dvb/dvb-usb/az6007.c
+++ b/drivers/media/dvb/dvb-usb/az6007.c
@@ -590,7 +590,7 @@ static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
590 int ret; 590 int ret;
591 591
592 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6); 592 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
593 memcpy(mac, st->data, sizeof(mac)); 593 memcpy(mac, st->data, 6);
594 594
595 if (ret > 0) 595 if (ret > 0)
596 deb_info("%s: mac is %pM\n", __func__, mac); 596 deb_info("%s: mac is %pM\n", __func__, mac);
diff --git a/drivers/media/dvb/frontends/dib8000.c b/drivers/media/dvb/frontends/dib8000.c
index 9ca34f495009..1f3bcb5a1de8 100644
--- a/drivers/media/dvb/frontends/dib8000.c
+++ b/drivers/media/dvb/frontends/dib8000.c
@@ -2680,12 +2680,14 @@ static int dib8000_tune(struct dvb_frontend *fe)
2680{ 2680{
2681 struct dib8000_state *state = fe->demodulator_priv; 2681 struct dib8000_state *state = fe->demodulator_priv;
2682 int ret = 0; 2682 int ret = 0;
2683 u16 lock, value, mode = fft_to_mode(state); 2683 u16 lock, value, mode;
2684 2684
2685 // we are already tuned - just resuming from suspend 2685 // we are already tuned - just resuming from suspend
2686 if (state == NULL) 2686 if (state == NULL)
2687 return -EINVAL; 2687 return -EINVAL;
2688 2688
2689 mode = fft_to_mode(state);
2690
2689 dib8000_set_bandwidth(fe, state->fe[0]->dtv_property_cache.bandwidth_hz / 1000); 2691 dib8000_set_bandwidth(fe, state->fe[0]->dtv_property_cache.bandwidth_hz / 1000);
2690 dib8000_set_channel(state, 0, 0); 2692 dib8000_set_channel(state, 0, 0);
2691 2693
diff --git a/drivers/media/dvb/frontends/lgs8gxx.c b/drivers/media/dvb/frontends/lgs8gxx.c
index 568363a10a31..c2ea2749ebed 100644
--- a/drivers/media/dvb/frontends/lgs8gxx.c
+++ b/drivers/media/dvb/frontends/lgs8gxx.c
@@ -40,6 +40,8 @@
40static int debug; 40static int debug;
41static int fake_signal_str = 1; 41static int fake_signal_str = 1;
42 42
43#define LGS8GXX_FIRMWARE "lgs8g75.fw"
44
43module_param(debug, int, 0644); 45module_param(debug, int, 0644);
44MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); 46MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
45 47
@@ -592,7 +594,7 @@ static int lgs8g75_init_data(struct lgs8gxx_state *priv)
592 int rc; 594 int rc;
593 int i; 595 int i;
594 596
595 rc = request_firmware(&fw, "lgs8g75.fw", &priv->i2c->dev); 597 rc = request_firmware(&fw, LGS8GXX_FIRMWARE, &priv->i2c->dev);
596 if (rc) 598 if (rc)
597 return rc; 599 return rc;
598 600
@@ -1070,3 +1072,4 @@ EXPORT_SYMBOL(lgs8gxx_attach);
1070MODULE_DESCRIPTION("Legend Silicon LGS8913/LGS8GXX DMB-TH demodulator driver"); 1072MODULE_DESCRIPTION("Legend Silicon LGS8913/LGS8GXX DMB-TH demodulator driver");
1071MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>"); 1073MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
1072MODULE_LICENSE("GPL"); 1074MODULE_LICENSE("GPL");
1075MODULE_FIRMWARE(LGS8GXX_FIRMWARE);
diff --git a/drivers/media/dvb/frontends/rtl2832.c b/drivers/media/dvb/frontends/rtl2832.c
index 2da592fb38ad..28269ccaeab7 100644
--- a/drivers/media/dvb/frontends/rtl2832.c
+++ b/drivers/media/dvb/frontends/rtl2832.c
@@ -589,7 +589,7 @@ static int rtl2832_set_frontend(struct dvb_frontend *fe)
589 return -EINVAL; 589 return -EINVAL;
590 } 590 }
591 591
592 for (j = 0; j < sizeof(bw_params[j]); j++) { 592 for (j = 0; j < sizeof(bw_params[0]); j++) {
593 ret = rtl2832_wr_regs(priv, 0x1c+j, 1, &bw_params[i][j], 1); 593 ret = rtl2832_wr_regs(priv, 0x1c+j, 1, &bw_params[i][j], 1);
594 if (ret) 594 if (ret)
595 goto err; 595 goto err;
diff --git a/drivers/media/dvb/siano/smscoreapi.c b/drivers/media/dvb/siano/smscoreapi.c
index 7331e8450d1a..9cc55546cc30 100644
--- a/drivers/media/dvb/siano/smscoreapi.c
+++ b/drivers/media/dvb/siano/smscoreapi.c
@@ -276,16 +276,13 @@ static void smscore_notify_clients(struct smscore_device_t *coredev)
276static int smscore_notify_callbacks(struct smscore_device_t *coredev, 276static int smscore_notify_callbacks(struct smscore_device_t *coredev,
277 struct device *device, int arrival) 277 struct device *device, int arrival)
278{ 278{
279 struct list_head *next, *first; 279 struct smscore_device_notifyee_t *elem;
280 int rc = 0; 280 int rc = 0;
281 281
282 /* note: must be called under g_deviceslock */ 282 /* note: must be called under g_deviceslock */
283 283
284 first = &g_smscore_notifyees; 284 list_for_each_entry(elem, &g_smscore_notifyees, entry) {
285 285 rc = elem->hotplug(coredev, device, arrival);
286 for (next = first->next; next != first; next = next->next) {
287 rc = ((struct smscore_device_notifyee_t *) next)->
288 hotplug(coredev, device, arrival);
289 if (rc < 0) 286 if (rc < 0)
290 break; 287 break;
291 } 288 }
@@ -940,29 +937,25 @@ static struct
940smscore_client_t *smscore_find_client(struct smscore_device_t *coredev, 937smscore_client_t *smscore_find_client(struct smscore_device_t *coredev,
941 int data_type, int id) 938 int data_type, int id)
942{ 939{
943 struct smscore_client_t *client = NULL; 940 struct list_head *first;
944 struct list_head *next, *first; 941 struct smscore_client_t *client;
945 unsigned long flags; 942 unsigned long flags;
946 struct list_head *firstid, *nextid; 943 struct list_head *firstid;
947 944 struct smscore_idlist_t *client_id;
948 945
949 spin_lock_irqsave(&coredev->clientslock, flags); 946 spin_lock_irqsave(&coredev->clientslock, flags);
950 first = &coredev->clients; 947 first = &coredev->clients;
951 for (next = first->next; 948 list_for_each_entry(client, first, entry) {
952 (next != first) && !client; 949 firstid = &client->idlist;
953 next = next->next) { 950 list_for_each_entry(client_id, firstid, entry) {
954 firstid = &((struct smscore_client_t *)next)->idlist; 951 if ((client_id->id == id) &&
955 for (nextid = firstid->next; 952 (client_id->data_type == data_type ||
956 nextid != firstid; 953 (client_id->data_type == 0)))
957 nextid = nextid->next) { 954 goto found;
958 if ((((struct smscore_idlist_t *)nextid)->id == id) &&
959 (((struct smscore_idlist_t *)nextid)->data_type == data_type ||
960 (((struct smscore_idlist_t *)nextid)->data_type == 0))) {
961 client = (struct smscore_client_t *) next;
962 break;
963 }
964 } 955 }
965 } 956 }
957 client = NULL;
958found:
966 spin_unlock_irqrestore(&coredev->clientslock, flags); 959 spin_unlock_irqrestore(&coredev->clientslock, flags);
967 return client; 960 return client;
968} 961}
diff --git a/drivers/media/radio/Kconfig b/drivers/media/radio/Kconfig
index 24ce5a47f955..8090b87b3066 100644
--- a/drivers/media/radio/Kconfig
+++ b/drivers/media/radio/Kconfig
@@ -57,6 +57,39 @@ config RADIO_MAXIRADIO
57 To compile this driver as a module, choose M here: the 57 To compile this driver as a module, choose M here: the
58 module will be called radio-maxiradio. 58 module will be called radio-maxiradio.
59 59
60config RADIO_SHARK
61 tristate "Griffin radioSHARK USB radio receiver"
62 depends on USB && SND
63 ---help---
64 Choose Y here if you have this radio receiver.
65
66 There are 2 versions of this device, this driver is for version 1,
67 which is white.
68
69 In order to control your radio card, you will need to use programs
70 that are compatible with the Video For Linux API. Information on
71 this API and pointers to "v4l" programs may be found at
72 <file:Documentation/video4linux/API.html>.
73
74 To compile this driver as a module, choose M here: the
75 module will be called radio-shark.
76
77config RADIO_SHARK2
78 tristate "Griffin radioSHARK2 USB radio receiver"
79 depends on USB
80 ---help---
81 Choose Y here if you have this radio receiver.
82
83 There are 2 versions of this device, this driver is for version 2,
84 which is black.
85
86 In order to control your radio card, you will need to use programs
87 that are compatible with the Video For Linux API. Information on
88 this API and pointers to "v4l" programs may be found at
89 <file:Documentation/video4linux/API.html>.
90
91 To compile this driver as a module, choose M here: the
92 module will be called radio-shark2.
60 93
61config I2C_SI4713 94config I2C_SI4713
62 tristate "I2C driver for Silicon Labs Si4713 device" 95 tristate "I2C driver for Silicon Labs Si4713 device"
diff --git a/drivers/media/radio/Makefile b/drivers/media/radio/Makefile
index ca8c7d134b95..c03ce4fe74e9 100644
--- a/drivers/media/radio/Makefile
+++ b/drivers/media/radio/Makefile
@@ -11,6 +11,8 @@ obj-$(CONFIG_RADIO_CADET) += radio-cadet.o
11obj-$(CONFIG_RADIO_TYPHOON) += radio-typhoon.o 11obj-$(CONFIG_RADIO_TYPHOON) += radio-typhoon.o
12obj-$(CONFIG_RADIO_TERRATEC) += radio-terratec.o 12obj-$(CONFIG_RADIO_TERRATEC) += radio-terratec.o
13obj-$(CONFIG_RADIO_MAXIRADIO) += radio-maxiradio.o 13obj-$(CONFIG_RADIO_MAXIRADIO) += radio-maxiradio.o
14obj-$(CONFIG_RADIO_SHARK) += radio-shark.o
15obj-$(CONFIG_RADIO_SHARK2) += shark2.o
14obj-$(CONFIG_RADIO_RTRACK) += radio-aimslab.o 16obj-$(CONFIG_RADIO_RTRACK) += radio-aimslab.o
15obj-$(CONFIG_RADIO_ZOLTRIX) += radio-zoltrix.o 17obj-$(CONFIG_RADIO_ZOLTRIX) += radio-zoltrix.o
16obj-$(CONFIG_RADIO_GEMTEK) += radio-gemtek.o 18obj-$(CONFIG_RADIO_GEMTEK) += radio-gemtek.o
@@ -29,4 +31,6 @@ obj-$(CONFIG_RADIO_TIMBERDALE) += radio-timb.o
29obj-$(CONFIG_RADIO_WL1273) += radio-wl1273.o 31obj-$(CONFIG_RADIO_WL1273) += radio-wl1273.o
30obj-$(CONFIG_RADIO_WL128X) += wl128x/ 32obj-$(CONFIG_RADIO_WL128X) += wl128x/
31 33
34shark2-objs := radio-shark2.o radio-tea5777.o
35
32ccflags-y += -Isound 36ccflags-y += -Isound
diff --git a/drivers/media/radio/radio-cadet.c b/drivers/media/radio/radio-cadet.c
index 16a089fad909..697a421c9940 100644
--- a/drivers/media/radio/radio-cadet.c
+++ b/drivers/media/radio/radio-cadet.c
@@ -41,6 +41,9 @@
41#include <linux/io.h> /* outb, outb_p */ 41#include <linux/io.h> /* outb, outb_p */
42#include <media/v4l2-device.h> 42#include <media/v4l2-device.h>
43#include <media/v4l2-ioctl.h> 43#include <media/v4l2-ioctl.h>
44#include <media/v4l2-ctrls.h>
45#include <media/v4l2-fh.h>
46#include <media/v4l2-event.h>
44 47
45MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath"); 48MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
46MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card."); 49MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
@@ -61,14 +64,15 @@ module_param(radio_nr, int, 0);
61struct cadet { 64struct cadet {
62 struct v4l2_device v4l2_dev; 65 struct v4l2_device v4l2_dev;
63 struct video_device vdev; 66 struct video_device vdev;
67 struct v4l2_ctrl_handler ctrl_handler;
64 int io; 68 int io;
65 int users; 69 bool is_fm_band;
66 int curtuner; 70 u32 curfreq;
67 int tunestat; 71 int tunestat;
68 int sigstrength; 72 int sigstrength;
69 wait_queue_head_t read_queue; 73 wait_queue_head_t read_queue;
70 struct timer_list readtimer; 74 struct timer_list readtimer;
71 __u8 rdsin, rdsout, rdsstat; 75 u8 rdsin, rdsout, rdsstat;
72 unsigned char rdsbuf[RDS_BUFFER]; 76 unsigned char rdsbuf[RDS_BUFFER];
73 struct mutex lock; 77 struct mutex lock;
74 int reading; 78 int reading;
@@ -81,9 +85,9 @@ static struct cadet cadet_card;
81 * The V4L API spec does not define any particular unit for the signal 85 * The V4L API spec does not define any particular unit for the signal
82 * strength value. These values are in microvolts of RF at the tuner's input. 86 * strength value. These values are in microvolts of RF at the tuner's input.
83 */ 87 */
84static __u16 sigtable[2][4] = { 88static u16 sigtable[2][4] = {
85 { 5, 10, 30, 150 }, 89 { 1835, 2621, 4128, 65535 },
86 { 28, 40, 63, 1000 } 90 { 2185, 4369, 13107, 65535 },
87}; 91};
88 92
89 93
@@ -91,14 +95,12 @@ static int cadet_getstereo(struct cadet *dev)
91{ 95{
92 int ret = V4L2_TUNER_SUB_MONO; 96 int ret = V4L2_TUNER_SUB_MONO;
93 97
94 if (dev->curtuner != 0) /* Only FM has stereo capability! */ 98 if (!dev->is_fm_band) /* Only FM has stereo capability! */
95 return V4L2_TUNER_SUB_MONO; 99 return V4L2_TUNER_SUB_MONO;
96 100
97 mutex_lock(&dev->lock);
98 outb(7, dev->io); /* Select tuner control */ 101 outb(7, dev->io); /* Select tuner control */
99 if ((inb(dev->io + 1) & 0x40) == 0) 102 if ((inb(dev->io + 1) & 0x40) == 0)
100 ret = V4L2_TUNER_SUB_STEREO; 103 ret = V4L2_TUNER_SUB_STEREO;
101 mutex_unlock(&dev->lock);
102 return ret; 104 return ret;
103} 105}
104 106
@@ -111,8 +113,6 @@ static unsigned cadet_gettune(struct cadet *dev)
111 * Prepare for read 113 * Prepare for read
112 */ 114 */
113 115
114 mutex_lock(&dev->lock);
115
116 outb(7, dev->io); /* Select tuner control */ 116 outb(7, dev->io); /* Select tuner control */
117 curvol = inb(dev->io + 1); /* Save current volume/mute setting */ 117 curvol = inb(dev->io + 1); /* Save current volume/mute setting */
118 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */ 118 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
@@ -134,8 +134,6 @@ static unsigned cadet_gettune(struct cadet *dev)
134 * Restore volume/mute setting 134 * Restore volume/mute setting
135 */ 135 */
136 outb(curvol, dev->io + 1); 136 outb(curvol, dev->io + 1);
137 mutex_unlock(&dev->lock);
138
139 return fifo; 137 return fifo;
140} 138}
141 139
@@ -152,20 +150,18 @@ static unsigned cadet_getfreq(struct cadet *dev)
152 /* 150 /*
153 * Convert to actual frequency 151 * Convert to actual frequency
154 */ 152 */
155 if (dev->curtuner == 0) { /* FM */ 153 if (!dev->is_fm_band) /* AM */
156 test = 12500; 154 return ((fifo & 0x7fff) - 450) * 16;
157 for (i = 0; i < 14; i++) { 155
158 if ((fifo & 0x01) != 0) 156 test = 12500;
159 freq += test; 157 for (i = 0; i < 14; i++) {
160 test = test << 1; 158 if ((fifo & 0x01) != 0)
161 fifo = fifo >> 1; 159 freq += test;
162 } 160 test = test << 1;
163 freq -= 10700000; /* IF frequency is 10.7 MHz */ 161 fifo = fifo >> 1;
164 freq = (freq * 16) / 1000000; /* Make it 1/16 MHz */
165 } 162 }
166 if (dev->curtuner == 1) /* AM */ 163 freq -= 10700000; /* IF frequency is 10.7 MHz */
167 freq = ((fifo & 0x7fff) - 2010) * 16; 164 freq = (freq * 16) / 1000; /* Make it 1/16 kHz */
168
169 return freq; 165 return freq;
170} 166}
171 167
@@ -174,8 +170,6 @@ static void cadet_settune(struct cadet *dev, unsigned fifo)
174 int i; 170 int i;
175 unsigned test; 171 unsigned test;
176 172
177 mutex_lock(&dev->lock);
178
179 outb(7, dev->io); /* Select tuner control */ 173 outb(7, dev->io); /* Select tuner control */
180 /* 174 /*
181 * Write the shift register 175 * Write the shift register
@@ -194,7 +188,6 @@ static void cadet_settune(struct cadet *dev, unsigned fifo)
194 test = 0x1c | ((fifo >> 23) & 0x02); 188 test = 0x1c | ((fifo >> 23) & 0x02);
195 outb(test, dev->io + 1); 189 outb(test, dev->io + 1);
196 } 190 }
197 mutex_unlock(&dev->lock);
198} 191}
199 192
200static void cadet_setfreq(struct cadet *dev, unsigned freq) 193static void cadet_setfreq(struct cadet *dev, unsigned freq)
@@ -203,13 +196,14 @@ static void cadet_setfreq(struct cadet *dev, unsigned freq)
203 int i, j, test; 196 int i, j, test;
204 int curvol; 197 int curvol;
205 198
199 dev->curfreq = freq;
206 /* 200 /*
207 * Formulate a fifo command 201 * Formulate a fifo command
208 */ 202 */
209 fifo = 0; 203 fifo = 0;
210 if (dev->curtuner == 0) { /* FM */ 204 if (dev->is_fm_band) { /* FM */
211 test = 102400; 205 test = 102400;
212 freq = (freq * 1000) / 16; /* Make it kHz */ 206 freq = freq / 16; /* Make it kHz */
213 freq += 10700; /* IF is 10700 kHz */ 207 freq += 10700; /* IF is 10700 kHz */
214 for (i = 0; i < 14; i++) { 208 for (i = 0; i < 14; i++) {
215 fifo = fifo << 1; 209 fifo = fifo << 1;
@@ -219,20 +213,17 @@ static void cadet_setfreq(struct cadet *dev, unsigned freq)
219 } 213 }
220 test = test >> 1; 214 test = test >> 1;
221 } 215 }
222 } 216 } else { /* AM */
223 if (dev->curtuner == 1) { /* AM */ 217 fifo = (freq / 16) + 450; /* Make it kHz */
224 fifo = (freq / 16) + 2010; /* Make it kHz */ 218 fifo |= 0x100000; /* Select AM Band */
225 fifo |= 0x100000; /* Select AM Band */
226 } 219 }
227 220
228 /* 221 /*
229 * Save current volume/mute setting 222 * Save current volume/mute setting
230 */ 223 */
231 224
232 mutex_lock(&dev->lock);
233 outb(7, dev->io); /* Select tuner control */ 225 outb(7, dev->io); /* Select tuner control */
234 curvol = inb(dev->io + 1); 226 curvol = inb(dev->io + 1);
235 mutex_unlock(&dev->lock);
236 227
237 /* 228 /*
238 * Tune the card 229 * Tune the card
@@ -240,49 +231,24 @@ static void cadet_setfreq(struct cadet *dev, unsigned freq)
240 for (j = 3; j > -1; j--) { 231 for (j = 3; j > -1; j--) {
241 cadet_settune(dev, fifo | (j << 16)); 232 cadet_settune(dev, fifo | (j << 16));
242 233
243 mutex_lock(&dev->lock);
244 outb(7, dev->io); /* Select tuner control */ 234 outb(7, dev->io); /* Select tuner control */
245 outb(curvol, dev->io + 1); 235 outb(curvol, dev->io + 1);
246 mutex_unlock(&dev->lock);
247 236
248 msleep(100); 237 msleep(100);
249 238
250 cadet_gettune(dev); 239 cadet_gettune(dev);
251 if ((dev->tunestat & 0x40) == 0) { /* Tuned */ 240 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
252 dev->sigstrength = sigtable[dev->curtuner][j]; 241 dev->sigstrength = sigtable[dev->is_fm_band][j];
253 return; 242 goto reset_rds;
254 } 243 }
255 } 244 }
256 dev->sigstrength = 0; 245 dev->sigstrength = 0;
246reset_rds:
247 outb(3, dev->io);
248 outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
257} 249}
258 250
259 251
260static int cadet_getvol(struct cadet *dev)
261{
262 int ret = 0;
263
264 mutex_lock(&dev->lock);
265
266 outb(7, dev->io); /* Select tuner control */
267 if ((inb(dev->io + 1) & 0x20) != 0)
268 ret = 0xffff;
269
270 mutex_unlock(&dev->lock);
271 return ret;
272}
273
274
275static void cadet_setvol(struct cadet *dev, int vol)
276{
277 mutex_lock(&dev->lock);
278 outb(7, dev->io); /* Select tuner control */
279 if (vol > 0)
280 outb(0x20, dev->io + 1);
281 else
282 outb(0x00, dev->io + 1);
283 mutex_unlock(&dev->lock);
284}
285
286static void cadet_handler(unsigned long data) 252static void cadet_handler(unsigned long data)
287{ 253{
288 struct cadet *dev = (void *)data; 254 struct cadet *dev = (void *)data;
@@ -295,7 +261,7 @@ static void cadet_handler(unsigned long data)
295 outb(0x80, dev->io); /* Select RDS fifo */ 261 outb(0x80, dev->io); /* Select RDS fifo */
296 while ((inb(dev->io) & 0x80) != 0) { 262 while ((inb(dev->io) & 0x80) != 0) {
297 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1); 263 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
298 if (dev->rdsin == dev->rdsout) 264 if (dev->rdsin + 1 == dev->rdsout)
299 printk(KERN_WARNING "cadet: RDS buffer overflow\n"); 265 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
300 else 266 else
301 dev->rdsin++; 267 dev->rdsin++;
@@ -314,11 +280,21 @@ static void cadet_handler(unsigned long data)
314 */ 280 */
315 init_timer(&dev->readtimer); 281 init_timer(&dev->readtimer);
316 dev->readtimer.function = cadet_handler; 282 dev->readtimer.function = cadet_handler;
317 dev->readtimer.data = (unsigned long)0; 283 dev->readtimer.data = data;
318 dev->readtimer.expires = jiffies + msecs_to_jiffies(50); 284 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
319 add_timer(&dev->readtimer); 285 add_timer(&dev->readtimer);
320} 286}
321 287
288static void cadet_start_rds(struct cadet *dev)
289{
290 dev->rdsstat = 1;
291 outb(0x80, dev->io); /* Select RDS fifo */
292 init_timer(&dev->readtimer);
293 dev->readtimer.function = cadet_handler;
294 dev->readtimer.data = (unsigned long)dev;
295 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
296 add_timer(&dev->readtimer);
297}
322 298
323static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos) 299static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
324{ 300{
@@ -327,28 +303,24 @@ static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo
327 int i = 0; 303 int i = 0;
328 304
329 mutex_lock(&dev->lock); 305 mutex_lock(&dev->lock);
330 if (dev->rdsstat == 0) { 306 if (dev->rdsstat == 0)
331 dev->rdsstat = 1; 307 cadet_start_rds(dev);
332 outb(0x80, dev->io); /* Select RDS fifo */
333 init_timer(&dev->readtimer);
334 dev->readtimer.function = cadet_handler;
335 dev->readtimer.data = (unsigned long)dev;
336 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
337 add_timer(&dev->readtimer);
338 }
339 if (dev->rdsin == dev->rdsout) { 308 if (dev->rdsin == dev->rdsout) {
309 if (file->f_flags & O_NONBLOCK) {
310 i = -EWOULDBLOCK;
311 goto unlock;
312 }
340 mutex_unlock(&dev->lock); 313 mutex_unlock(&dev->lock);
341 if (file->f_flags & O_NONBLOCK)
342 return -EWOULDBLOCK;
343 interruptible_sleep_on(&dev->read_queue); 314 interruptible_sleep_on(&dev->read_queue);
344 mutex_lock(&dev->lock); 315 mutex_lock(&dev->lock);
345 } 316 }
346 while (i < count && dev->rdsin != dev->rdsout) 317 while (i < count && dev->rdsin != dev->rdsout)
347 readbuf[i++] = dev->rdsbuf[dev->rdsout++]; 318 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
348 mutex_unlock(&dev->lock);
349 319
350 if (copy_to_user(data, readbuf, i)) 320 if (i && copy_to_user(data, readbuf, i))
351 return -EFAULT; 321 i = -EFAULT;
322unlock:
323 mutex_unlock(&dev->lock);
352 return i; 324 return i;
353} 325}
354 326
@@ -359,48 +331,58 @@ static int vidioc_querycap(struct file *file, void *priv,
359 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver)); 331 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
360 strlcpy(v->card, "ADS Cadet", sizeof(v->card)); 332 strlcpy(v->card, "ADS Cadet", sizeof(v->card));
361 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info)); 333 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
362 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO | 334 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
363 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE; 335 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
336 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
364 return 0; 337 return 0;
365} 338}
366 339
340static const struct v4l2_frequency_band bands[] = {
341 {
342 .index = 0,
343 .type = V4L2_TUNER_RADIO,
344 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
345 .rangelow = 8320, /* 520 kHz */
346 .rangehigh = 26400, /* 1650 kHz */
347 .modulation = V4L2_BAND_MODULATION_AM,
348 }, {
349 .index = 1,
350 .type = V4L2_TUNER_RADIO,
351 .capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
352 V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW |
353 V4L2_TUNER_CAP_FREQ_BANDS,
354 .rangelow = 1400000, /* 87.5 MHz */
355 .rangehigh = 1728000, /* 108.0 MHz */
356 .modulation = V4L2_BAND_MODULATION_FM,
357 },
358};
359
367static int vidioc_g_tuner(struct file *file, void *priv, 360static int vidioc_g_tuner(struct file *file, void *priv,
368 struct v4l2_tuner *v) 361 struct v4l2_tuner *v)
369{ 362{
370 struct cadet *dev = video_drvdata(file); 363 struct cadet *dev = video_drvdata(file);
371 364
365 if (v->index)
366 return -EINVAL;
372 v->type = V4L2_TUNER_RADIO; 367 v->type = V4L2_TUNER_RADIO;
373 switch (v->index) { 368 strlcpy(v->name, "Radio", sizeof(v->name));
374 case 0: 369 v->capability = bands[0].capability | bands[1].capability;
375 strlcpy(v->name, "FM", sizeof(v->name)); 370 v->rangelow = bands[0].rangelow; /* 520 kHz (start of AM band) */
376 v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS | 371 v->rangehigh = bands[1].rangehigh; /* 108.0 MHz (end of FM band) */
377 V4L2_TUNER_CAP_RDS_BLOCK_IO; 372 if (dev->is_fm_band) {
378 v->rangelow = 1400; /* 87.5 MHz */
379 v->rangehigh = 1728; /* 108.0 MHz */
380 v->rxsubchans = cadet_getstereo(dev); 373 v->rxsubchans = cadet_getstereo(dev);
381 switch (v->rxsubchans) { 374 outb(3, dev->io);
382 case V4L2_TUNER_SUB_MONO: 375 outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
383 v->audmode = V4L2_TUNER_MODE_MONO; 376 mdelay(100);
384 break; 377 outb(3, dev->io);
385 case V4L2_TUNER_SUB_STEREO: 378 if (inb(dev->io + 1) & 0x80)
386 v->audmode = V4L2_TUNER_MODE_STEREO; 379 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
387 break; 380 } else {
388 default:
389 break;
390 }
391 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
392 break;
393 case 1:
394 strlcpy(v->name, "AM", sizeof(v->name));
395 v->capability = V4L2_TUNER_CAP_LOW;
396 v->rangelow = 8320; /* 520 kHz */ 381 v->rangelow = 8320; /* 520 kHz */
397 v->rangehigh = 26400; /* 1650 kHz */ 382 v->rangehigh = 26400; /* 1650 kHz */
398 v->rxsubchans = V4L2_TUNER_SUB_MONO; 383 v->rxsubchans = V4L2_TUNER_SUB_MONO;
399 v->audmode = V4L2_TUNER_MODE_MONO;
400 break;
401 default:
402 return -EINVAL;
403 } 384 }
385 v->audmode = V4L2_TUNER_MODE_STEREO;
404 v->signal = dev->sigstrength; /* We might need to modify scaling of this */ 386 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
405 return 0; 387 return 0;
406} 388}
@@ -408,11 +390,17 @@ static int vidioc_g_tuner(struct file *file, void *priv,
408static int vidioc_s_tuner(struct file *file, void *priv, 390static int vidioc_s_tuner(struct file *file, void *priv,
409 struct v4l2_tuner *v) 391 struct v4l2_tuner *v)
410{ 392{
411 struct cadet *dev = video_drvdata(file); 393 return v->index ? -EINVAL : 0;
394}
412 395
413 if (v->index != 0 && v->index != 1) 396static int vidioc_enum_freq_bands(struct file *file, void *priv,
397 struct v4l2_frequency_band *band)
398{
399 if (band->tuner)
400 return -EINVAL;
401 if (band->index >= ARRAY_SIZE(bands))
414 return -EINVAL; 402 return -EINVAL;
415 dev->curtuner = v->index; 403 *band = bands[band->index];
416 return 0; 404 return 0;
417} 405}
418 406
@@ -421,9 +409,10 @@ static int vidioc_g_frequency(struct file *file, void *priv,
421{ 409{
422 struct cadet *dev = video_drvdata(file); 410 struct cadet *dev = video_drvdata(file);
423 411
424 f->tuner = dev->curtuner; 412 if (f->tuner)
413 return -EINVAL;
425 f->type = V4L2_TUNER_RADIO; 414 f->type = V4L2_TUNER_RADIO;
426 f->frequency = cadet_getfreq(dev); 415 f->frequency = dev->curfreq;
427 return 0; 416 return 0;
428} 417}
429 418
@@ -433,103 +422,46 @@ static int vidioc_s_frequency(struct file *file, void *priv,
433{ 422{
434 struct cadet *dev = video_drvdata(file); 423 struct cadet *dev = video_drvdata(file);
435 424
436 if (f->type != V4L2_TUNER_RADIO) 425 if (f->tuner)
437 return -EINVAL;
438 if (dev->curtuner == 0 && (f->frequency < 1400 || f->frequency > 1728))
439 return -EINVAL;
440 if (dev->curtuner == 1 && (f->frequency < 8320 || f->frequency > 26400))
441 return -EINVAL; 426 return -EINVAL;
427 dev->is_fm_band =
428 f->frequency >= (bands[0].rangehigh + bands[1].rangelow) / 2;
429 clamp(f->frequency, bands[dev->is_fm_band].rangelow,
430 bands[dev->is_fm_band].rangehigh);
442 cadet_setfreq(dev, f->frequency); 431 cadet_setfreq(dev, f->frequency);
443 return 0; 432 return 0;
444} 433}
445 434
446static int vidioc_queryctrl(struct file *file, void *priv, 435static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
447 struct v4l2_queryctrl *qc)
448{ 436{
449 switch (qc->id) { 437 struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
450 case V4L2_CID_AUDIO_MUTE:
451 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
452 case V4L2_CID_AUDIO_VOLUME:
453 return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
454 }
455 return -EINVAL;
456}
457
458static int vidioc_g_ctrl(struct file *file, void *priv,
459 struct v4l2_control *ctrl)
460{
461 struct cadet *dev = video_drvdata(file);
462 438
463 switch (ctrl->id) { 439 switch (ctrl->id) {
464 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */ 440 case V4L2_CID_AUDIO_MUTE:
465 ctrl->value = (cadet_getvol(dev) == 0); 441 outb(7, dev->io); /* Select tuner control */
466 break; 442 if (ctrl->val)
467 case V4L2_CID_AUDIO_VOLUME: 443 outb(0x00, dev->io + 1);
468 ctrl->value = cadet_getvol(dev);
469 break;
470 default:
471 return -EINVAL;
472 }
473 return 0;
474}
475
476static int vidioc_s_ctrl(struct file *file, void *priv,
477 struct v4l2_control *ctrl)
478{
479 struct cadet *dev = video_drvdata(file);
480
481 switch (ctrl->id){
482 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
483 if (ctrl->value)
484 cadet_setvol(dev, 0);
485 else 444 else
486 cadet_setvol(dev, 0xffff); 445 outb(0x20, dev->io + 1);
487 break; 446 return 0;
488 case V4L2_CID_AUDIO_VOLUME:
489 cadet_setvol(dev, ctrl->value);
490 break;
491 default:
492 return -EINVAL;
493 } 447 }
494 return 0; 448 return -EINVAL;
495}
496
497static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
498{
499 *i = 0;
500 return 0;
501}
502
503static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
504{
505 return i ? -EINVAL : 0;
506}
507
508static int vidioc_g_audio(struct file *file, void *priv,
509 struct v4l2_audio *a)
510{
511 a->index = 0;
512 strlcpy(a->name, "Radio", sizeof(a->name));
513 a->capability = V4L2_AUDCAP_STEREO;
514 return 0;
515}
516
517static int vidioc_s_audio(struct file *file, void *priv,
518 struct v4l2_audio *a)
519{
520 return a->index ? -EINVAL : 0;
521} 449}
522 450
523static int cadet_open(struct file *file) 451static int cadet_open(struct file *file)
524{ 452{
525 struct cadet *dev = video_drvdata(file); 453 struct cadet *dev = video_drvdata(file);
454 int err;
526 455
527 mutex_lock(&dev->lock); 456 mutex_lock(&dev->lock);
528 dev->users++; 457 err = v4l2_fh_open(file);
529 if (1 == dev->users) 458 if (err)
459 goto fail;
460 if (v4l2_fh_is_singular_file(file))
530 init_waitqueue_head(&dev->read_queue); 461 init_waitqueue_head(&dev->read_queue);
462fail:
531 mutex_unlock(&dev->lock); 463 mutex_unlock(&dev->lock);
532 return 0; 464 return err;
533} 465}
534 466
535static int cadet_release(struct file *file) 467static int cadet_release(struct file *file)
@@ -537,11 +469,11 @@ static int cadet_release(struct file *file)
537 struct cadet *dev = video_drvdata(file); 469 struct cadet *dev = video_drvdata(file);
538 470
539 mutex_lock(&dev->lock); 471 mutex_lock(&dev->lock);
540 dev->users--; 472 if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
541 if (0 == dev->users) {
542 del_timer_sync(&dev->readtimer); 473 del_timer_sync(&dev->readtimer);
543 dev->rdsstat = 0; 474 dev->rdsstat = 0;
544 } 475 }
476 v4l2_fh_release(file);
545 mutex_unlock(&dev->lock); 477 mutex_unlock(&dev->lock);
546 return 0; 478 return 0;
547} 479}
@@ -549,11 +481,19 @@ static int cadet_release(struct file *file)
549static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait) 481static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
550{ 482{
551 struct cadet *dev = video_drvdata(file); 483 struct cadet *dev = video_drvdata(file);
484 unsigned long req_events = poll_requested_events(wait);
485 unsigned int res = v4l2_ctrl_poll(file, wait);
552 486
553 poll_wait(file, &dev->read_queue, wait); 487 poll_wait(file, &dev->read_queue, wait);
488 if (dev->rdsstat == 0 && (req_events & (POLLIN | POLLRDNORM))) {
489 mutex_lock(&dev->lock);
490 if (dev->rdsstat == 0)
491 cadet_start_rds(dev);
492 mutex_unlock(&dev->lock);
493 }
554 if (dev->rdsin != dev->rdsout) 494 if (dev->rdsin != dev->rdsout)
555 return POLLIN | POLLRDNORM; 495 res |= POLLIN | POLLRDNORM;
556 return 0; 496 return res;
557} 497}
558 498
559 499
@@ -572,13 +512,14 @@ static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
572 .vidioc_s_tuner = vidioc_s_tuner, 512 .vidioc_s_tuner = vidioc_s_tuner,
573 .vidioc_g_frequency = vidioc_g_frequency, 513 .vidioc_g_frequency = vidioc_g_frequency,
574 .vidioc_s_frequency = vidioc_s_frequency, 514 .vidioc_s_frequency = vidioc_s_frequency,
575 .vidioc_queryctrl = vidioc_queryctrl, 515 .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
576 .vidioc_g_ctrl = vidioc_g_ctrl, 516 .vidioc_log_status = v4l2_ctrl_log_status,
577 .vidioc_s_ctrl = vidioc_s_ctrl, 517 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
578 .vidioc_g_audio = vidioc_g_audio, 518 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
579 .vidioc_s_audio = vidioc_s_audio, 519};
580 .vidioc_g_input = vidioc_g_input, 520
581 .vidioc_s_input = vidioc_s_input, 521static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
522 .s_ctrl = cadet_s_ctrl,
582}; 523};
583 524
584#ifdef CONFIG_PNP 525#ifdef CONFIG_PNP
@@ -628,8 +569,8 @@ static void cadet_probe(struct cadet *dev)
628 for (i = 0; i < 8; i++) { 569 for (i = 0; i < 8; i++) {
629 dev->io = iovals[i]; 570 dev->io = iovals[i];
630 if (request_region(dev->io, 2, "cadet-probe")) { 571 if (request_region(dev->io, 2, "cadet-probe")) {
631 cadet_setfreq(dev, 1410); 572 cadet_setfreq(dev, bands[1].rangelow);
632 if (cadet_getfreq(dev) == 1410) { 573 if (cadet_getfreq(dev) == bands[1].rangelow) {
633 release_region(dev->io, 2); 574 release_region(dev->io, 2);
634 return; 575 return;
635 } 576 }
@@ -648,7 +589,8 @@ static int __init cadet_init(void)
648{ 589{
649 struct cadet *dev = &cadet_card; 590 struct cadet *dev = &cadet_card;
650 struct v4l2_device *v4l2_dev = &dev->v4l2_dev; 591 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
651 int res; 592 struct v4l2_ctrl_handler *hdl;
593 int res = -ENODEV;
652 594
653 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name)); 595 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
654 mutex_init(&dev->lock); 596 mutex_init(&dev->lock);
@@ -680,23 +622,40 @@ static int __init cadet_init(void)
680 goto fail; 622 goto fail;
681 } 623 }
682 624
625 hdl = &dev->ctrl_handler;
626 v4l2_ctrl_handler_init(hdl, 2);
627 v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops,
628 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
629 v4l2_dev->ctrl_handler = hdl;
630 if (hdl->error) {
631 res = hdl->error;
632 v4l2_err(v4l2_dev, "Could not register controls\n");
633 goto err_hdl;
634 }
635
636 dev->is_fm_band = true;
637 dev->curfreq = bands[dev->is_fm_band].rangelow;
638 cadet_setfreq(dev, dev->curfreq);
683 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name)); 639 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
684 dev->vdev.v4l2_dev = v4l2_dev; 640 dev->vdev.v4l2_dev = v4l2_dev;
685 dev->vdev.fops = &cadet_fops; 641 dev->vdev.fops = &cadet_fops;
686 dev->vdev.ioctl_ops = &cadet_ioctl_ops; 642 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
687 dev->vdev.release = video_device_release_empty; 643 dev->vdev.release = video_device_release_empty;
644 dev->vdev.lock = &dev->lock;
645 set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
688 video_set_drvdata(&dev->vdev, dev); 646 video_set_drvdata(&dev->vdev, dev);
689 647
690 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) { 648 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0)
691 v4l2_device_unregister(v4l2_dev); 649 goto err_hdl;
692 release_region(dev->io, 2);
693 goto fail;
694 }
695 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io); 650 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
696 return 0; 651 return 0;
652err_hdl:
653 v4l2_ctrl_handler_free(hdl);
654 v4l2_device_unregister(v4l2_dev);
655 release_region(dev->io, 2);
697fail: 656fail:
698 pnp_unregister_driver(&cadet_pnp_driver); 657 pnp_unregister_driver(&cadet_pnp_driver);
699 return -ENODEV; 658 return res;
700} 659}
701 660
702static void __exit cadet_exit(void) 661static void __exit cadet_exit(void)
@@ -704,7 +663,10 @@ static void __exit cadet_exit(void)
704 struct cadet *dev = &cadet_card; 663 struct cadet *dev = &cadet_card;
705 664
706 video_unregister_device(&dev->vdev); 665 video_unregister_device(&dev->vdev);
666 v4l2_ctrl_handler_free(&dev->ctrl_handler);
707 v4l2_device_unregister(&dev->v4l2_dev); 667 v4l2_device_unregister(&dev->v4l2_dev);
668 outb(7, dev->io); /* Mute */
669 outb(0x00, dev->io + 1);
708 release_region(dev->io, 2); 670 release_region(dev->io, 2);
709 pnp_unregister_driver(&cadet_pnp_driver); 671 pnp_unregister_driver(&cadet_pnp_driver);
710} 672}
diff --git a/drivers/media/radio/radio-shark.c b/drivers/media/radio/radio-shark.c
new file mode 100644
index 000000000000..d0b6bb507634
--- /dev/null
+++ b/drivers/media/radio/radio-shark.c
@@ -0,0 +1,376 @@
1/*
2 * Linux V4L2 radio driver for the Griffin radioSHARK USB radio receiver
3 *
4 * Note the radioSHARK offers the audio through a regular USB audio device,
5 * this driver only handles the tuning.
6 *
7 * The info necessary to drive the shark was taken from the small userspace
8 * shark.c program by Michael Rolig, which he kindly placed in the Public
9 * Domain.
10 *
11 * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
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#include <linux/init.h>
29#include <linux/kernel.h>
30#include <linux/leds.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/usb.h>
34#include <linux/workqueue.h>
35#include <media/v4l2-device.h>
36#include <sound/tea575x-tuner.h>
37
38/*
39 * Version Information
40 */
41MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
42MODULE_DESCRIPTION("Griffin radioSHARK, USB radio receiver driver");
43MODULE_LICENSE("GPL");
44
45#define SHARK_IN_EP 0x83
46#define SHARK_OUT_EP 0x05
47
48#define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */
49#define TEA575X_BIT_BAND_MASK (3<<20)
50#define TEA575X_BIT_BAND_FM (0<<20)
51
52#define TB_LEN 6
53#define DRV_NAME "radioshark"
54
55#define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev)
56
57enum { BLUE_LED, BLUE_PULSE_LED, RED_LED, NO_LEDS };
58
59static void shark_led_set_blue(struct led_classdev *led_cdev,
60 enum led_brightness value);
61static void shark_led_set_blue_pulse(struct led_classdev *led_cdev,
62 enum led_brightness value);
63static void shark_led_set_red(struct led_classdev *led_cdev,
64 enum led_brightness value);
65
66static const struct led_classdev shark_led_templates[NO_LEDS] = {
67 [BLUE_LED] = {
68 .name = "%s:blue:",
69 .brightness = LED_OFF,
70 .max_brightness = 127,
71 .brightness_set = shark_led_set_blue,
72 },
73 [BLUE_PULSE_LED] = {
74 .name = "%s:blue-pulse:",
75 .brightness = LED_OFF,
76 .max_brightness = 255,
77 .brightness_set = shark_led_set_blue_pulse,
78 },
79 [RED_LED] = {
80 .name = "%s:red:",
81 .brightness = LED_OFF,
82 .max_brightness = 1,
83 .brightness_set = shark_led_set_red,
84 },
85};
86
87struct shark_device {
88 struct usb_device *usbdev;
89 struct v4l2_device v4l2_dev;
90 struct snd_tea575x tea;
91
92 struct work_struct led_work;
93 struct led_classdev leds[NO_LEDS];
94 char led_names[NO_LEDS][32];
95 atomic_t brightness[NO_LEDS];
96 unsigned long brightness_new;
97
98 u8 *transfer_buffer;
99 u32 last_val;
100};
101
102static atomic_t shark_instance = ATOMIC_INIT(0);
103
104static void shark_write_val(struct snd_tea575x *tea, u32 val)
105{
106 struct shark_device *shark = tea->private_data;
107 int i, res, actual_len;
108
109 /* Avoid unnecessary (slow) USB transfers */
110 if (shark->last_val == val)
111 return;
112
113 memset(shark->transfer_buffer, 0, TB_LEN);
114 shark->transfer_buffer[0] = 0xc0; /* Write shift register command */
115 for (i = 0; i < 4; i++)
116 shark->transfer_buffer[i] |= (val >> (24 - i * 8)) & 0xff;
117
118 res = usb_interrupt_msg(shark->usbdev,
119 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
120 shark->transfer_buffer, TB_LEN,
121 &actual_len, 1000);
122 if (res >= 0)
123 shark->last_val = val;
124 else
125 v4l2_err(&shark->v4l2_dev, "set-freq error: %d\n", res);
126}
127
128static u32 shark_read_val(struct snd_tea575x *tea)
129{
130 struct shark_device *shark = tea->private_data;
131 int i, res, actual_len;
132 u32 val = 0;
133
134 memset(shark->transfer_buffer, 0, TB_LEN);
135 shark->transfer_buffer[0] = 0x80;
136 res = usb_interrupt_msg(shark->usbdev,
137 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
138 shark->transfer_buffer, TB_LEN,
139 &actual_len, 1000);
140 if (res < 0) {
141 v4l2_err(&shark->v4l2_dev, "request-status error: %d\n", res);
142 return shark->last_val;
143 }
144
145 res = usb_interrupt_msg(shark->usbdev,
146 usb_rcvintpipe(shark->usbdev, SHARK_IN_EP),
147 shark->transfer_buffer, TB_LEN,
148 &actual_len, 1000);
149 if (res < 0) {
150 v4l2_err(&shark->v4l2_dev, "get-status error: %d\n", res);
151 return shark->last_val;
152 }
153
154 for (i = 0; i < 4; i++)
155 val |= shark->transfer_buffer[i] << (24 - i * 8);
156
157 shark->last_val = val;
158
159 /*
160 * The shark does not allow actually reading the stereo / mono pin :(
161 * So assume that when we're tuned to an FM station and mono has not
162 * been requested, that we're receiving stereo.
163 */
164 if (((val & TEA575X_BIT_BAND_MASK) == TEA575X_BIT_BAND_FM) &&
165 !(val & TEA575X_BIT_MONO))
166 shark->tea.stereo = true;
167 else
168 shark->tea.stereo = false;
169
170 return val;
171}
172
173static struct snd_tea575x_ops shark_tea_ops = {
174 .write_val = shark_write_val,
175 .read_val = shark_read_val,
176};
177
178static void shark_led_work(struct work_struct *work)
179{
180 struct shark_device *shark =
181 container_of(work, struct shark_device, led_work);
182 int i, res, brightness, actual_len;
183
184 /*
185 * We use the v4l2_dev lock and registered bit to ensure the device
186 * does not get unplugged and unreffed while we're running.
187 */
188 mutex_lock(&shark->tea.mutex);
189 if (!video_is_registered(&shark->tea.vd))
190 goto leave;
191
192 for (i = 0; i < 3; i++) {
193 if (!test_and_clear_bit(i, &shark->brightness_new))
194 continue;
195
196 brightness = atomic_read(&shark->brightness[i]);
197 memset(shark->transfer_buffer, 0, TB_LEN);
198 if (i != RED_LED) {
199 shark->transfer_buffer[0] = 0xA0 + i;
200 shark->transfer_buffer[1] = brightness;
201 } else
202 shark->transfer_buffer[0] = brightness ? 0xA9 : 0xA8;
203 res = usb_interrupt_msg(shark->usbdev,
204 usb_sndintpipe(shark->usbdev, 0x05),
205 shark->transfer_buffer, TB_LEN,
206 &actual_len, 1000);
207 if (res < 0)
208 v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
209 shark->led_names[i], res);
210 }
211leave:
212 mutex_unlock(&shark->tea.mutex);
213}
214
215static void shark_led_set_blue(struct led_classdev *led_cdev,
216 enum led_brightness value)
217{
218 struct shark_device *shark =
219 container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
220
221 atomic_set(&shark->brightness[BLUE_LED], value);
222 set_bit(BLUE_LED, &shark->brightness_new);
223 schedule_work(&shark->led_work);
224}
225
226static void shark_led_set_blue_pulse(struct led_classdev *led_cdev,
227 enum led_brightness value)
228{
229 struct shark_device *shark = container_of(led_cdev,
230 struct shark_device, leds[BLUE_PULSE_LED]);
231
232 atomic_set(&shark->brightness[BLUE_PULSE_LED], 256 - value);
233 set_bit(BLUE_PULSE_LED, &shark->brightness_new);
234 schedule_work(&shark->led_work);
235}
236
237static void shark_led_set_red(struct led_classdev *led_cdev,
238 enum led_brightness value)
239{
240 struct shark_device *shark =
241 container_of(led_cdev, struct shark_device, leds[RED_LED]);
242
243 atomic_set(&shark->brightness[RED_LED], value);
244 set_bit(RED_LED, &shark->brightness_new);
245 schedule_work(&shark->led_work);
246}
247
248static void usb_shark_disconnect(struct usb_interface *intf)
249{
250 struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
251 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
252 int i;
253
254 mutex_lock(&shark->tea.mutex);
255 v4l2_device_disconnect(&shark->v4l2_dev);
256 snd_tea575x_exit(&shark->tea);
257 mutex_unlock(&shark->tea.mutex);
258
259 for (i = 0; i < NO_LEDS; i++)
260 led_classdev_unregister(&shark->leds[i]);
261
262 v4l2_device_put(&shark->v4l2_dev);
263}
264
265static void usb_shark_release(struct v4l2_device *v4l2_dev)
266{
267 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
268
269 cancel_work_sync(&shark->led_work);
270 v4l2_device_unregister(&shark->v4l2_dev);
271 kfree(shark->transfer_buffer);
272 kfree(shark);
273}
274
275static int usb_shark_probe(struct usb_interface *intf,
276 const struct usb_device_id *id)
277{
278 struct shark_device *shark;
279 int i, retval = -ENOMEM;
280
281 shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
282 if (!shark)
283 return retval;
284
285 shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
286 if (!shark->transfer_buffer)
287 goto err_alloc_buffer;
288
289 /*
290 * Work around a bug in usbhid/hid-core.c, where it leaves a dangling
291 * pointer in intfdata causing v4l2-device.c to not set it. Which
292 * results in usb_shark_disconnect() referencing the dangling pointer
293 *
294 * REMOVE (as soon as the above bug is fixed, patch submitted)
295 */
296 usb_set_intfdata(intf, NULL);
297
298 shark->v4l2_dev.release = usb_shark_release;
299 v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
300 retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
301 if (retval) {
302 v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
303 goto err_reg_dev;
304 }
305
306 shark->usbdev = interface_to_usbdev(intf);
307 shark->tea.v4l2_dev = &shark->v4l2_dev;
308 shark->tea.private_data = shark;
309 shark->tea.radio_nr = -1;
310 shark->tea.ops = &shark_tea_ops;
311 shark->tea.cannot_mute = true;
312 strlcpy(shark->tea.card, "Griffin radioSHARK",
313 sizeof(shark->tea.card));
314 usb_make_path(shark->usbdev, shark->tea.bus_info,
315 sizeof(shark->tea.bus_info));
316
317 retval = snd_tea575x_init(&shark->tea, THIS_MODULE);
318 if (retval) {
319 v4l2_err(&shark->v4l2_dev, "couldn't init tea5757\n");
320 goto err_init_tea;
321 }
322
323 INIT_WORK(&shark->led_work, shark_led_work);
324 for (i = 0; i < NO_LEDS; i++) {
325 shark->leds[i] = shark_led_templates[i];
326 snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
327 shark->leds[i].name, shark->v4l2_dev.name);
328 shark->leds[i].name = shark->led_names[i];
329 /*
330 * We don't fail the probe if we fail to register the leds,
331 * because once we've called snd_tea575x_init, the /dev/radio0
332 * node may be opened from userspace holding a reference to us!
333 *
334 * Note we cannot register the leds first instead as
335 * shark_led_work depends on the v4l2 mutex and registered bit.
336 */
337 retval = led_classdev_register(&intf->dev, &shark->leds[i]);
338 if (retval)
339 v4l2_err(&shark->v4l2_dev,
340 "couldn't register led: %s\n",
341 shark->led_names[i]);
342 }
343
344 return 0;
345
346err_init_tea:
347 v4l2_device_unregister(&shark->v4l2_dev);
348err_reg_dev:
349 kfree(shark->transfer_buffer);
350err_alloc_buffer:
351 kfree(shark);
352
353 return retval;
354}
355
356/* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
357static struct usb_device_id usb_shark_device_table[] = {
358 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
359 USB_DEVICE_ID_MATCH_INT_CLASS,
360 .idVendor = 0x077d,
361 .idProduct = 0x627a,
362 .bcdDevice_lo = 0x0001,
363 .bcdDevice_hi = 0x0001,
364 .bInterfaceClass = 3,
365 },
366 { }
367};
368MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
369
370static struct usb_driver usb_shark_driver = {
371 .name = DRV_NAME,
372 .probe = usb_shark_probe,
373 .disconnect = usb_shark_disconnect,
374 .id_table = usb_shark_device_table,
375};
376module_usb_driver(usb_shark_driver);
diff --git a/drivers/media/radio/radio-shark2.c b/drivers/media/radio/radio-shark2.c
new file mode 100644
index 000000000000..b9575de3e7e8
--- /dev/null
+++ b/drivers/media/radio/radio-shark2.c
@@ -0,0 +1,348 @@
1/*
2 * Linux V4L2 radio driver for the Griffin radioSHARK2 USB radio receiver
3 *
4 * Note the radioSHARK2 offers the audio through a regular USB audio device,
5 * this driver only handles the tuning.
6 *
7 * The info necessary to drive the shark2 was taken from the small userspace
8 * shark2.c program by Hisaaki Shibata, which he kindly placed in the Public
9 * Domain.
10 *
11 * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
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#include <linux/init.h>
29#include <linux/kernel.h>
30#include <linux/leds.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/usb.h>
34#include <linux/workqueue.h>
35#include <media/v4l2-device.h>
36#include "radio-tea5777.h"
37
38MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
39MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver");
40MODULE_LICENSE("GPL");
41
42static int debug;
43module_param(debug, int, 0);
44MODULE_PARM_DESC(debug, "Debug level (0-1)");
45
46
47#define SHARK_IN_EP 0x83
48#define SHARK_OUT_EP 0x05
49
50#define TB_LEN 7
51#define DRV_NAME "radioshark2"
52
53#define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev)
54
55enum { BLUE_LED, RED_LED, NO_LEDS };
56
57static void shark_led_set_blue(struct led_classdev *led_cdev,
58 enum led_brightness value);
59static void shark_led_set_red(struct led_classdev *led_cdev,
60 enum led_brightness value);
61
62static const struct led_classdev shark_led_templates[NO_LEDS] = {
63 [BLUE_LED] = {
64 .name = "%s:blue:",
65 .brightness = LED_OFF,
66 .max_brightness = 127,
67 .brightness_set = shark_led_set_blue,
68 },
69 [RED_LED] = {
70 .name = "%s:red:",
71 .brightness = LED_OFF,
72 .max_brightness = 1,
73 .brightness_set = shark_led_set_red,
74 },
75};
76
77struct shark_device {
78 struct usb_device *usbdev;
79 struct v4l2_device v4l2_dev;
80 struct radio_tea5777 tea;
81
82 struct work_struct led_work;
83 struct led_classdev leds[NO_LEDS];
84 char led_names[NO_LEDS][32];
85 atomic_t brightness[NO_LEDS];
86 unsigned long brightness_new;
87
88 u8 *transfer_buffer;
89};
90
91static atomic_t shark_instance = ATOMIC_INIT(0);
92
93static int shark_write_reg(struct radio_tea5777 *tea, u64 reg)
94{
95 struct shark_device *shark = tea->private_data;
96 int i, res, actual_len;
97
98 memset(shark->transfer_buffer, 0, TB_LEN);
99 shark->transfer_buffer[0] = 0x81; /* Write register command */
100 for (i = 0; i < 6; i++)
101 shark->transfer_buffer[i + 1] = (reg >> (40 - i * 8)) & 0xff;
102
103 v4l2_dbg(1, debug, tea->v4l2_dev,
104 "shark2-write: %02x %02x %02x %02x %02x %02x %02x\n",
105 shark->transfer_buffer[0], shark->transfer_buffer[1],
106 shark->transfer_buffer[2], shark->transfer_buffer[3],
107 shark->transfer_buffer[4], shark->transfer_buffer[5],
108 shark->transfer_buffer[6]);
109
110 res = usb_interrupt_msg(shark->usbdev,
111 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
112 shark->transfer_buffer, TB_LEN,
113 &actual_len, 1000);
114 if (res < 0) {
115 v4l2_err(tea->v4l2_dev, "write error: %d\n", res);
116 return res;
117 }
118
119 return 0;
120}
121
122static int shark_read_reg(struct radio_tea5777 *tea, u32 *reg_ret)
123{
124 struct shark_device *shark = tea->private_data;
125 int i, res, actual_len;
126 u32 reg = 0;
127
128 memset(shark->transfer_buffer, 0, TB_LEN);
129 shark->transfer_buffer[0] = 0x82;
130 res = usb_interrupt_msg(shark->usbdev,
131 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
132 shark->transfer_buffer, TB_LEN,
133 &actual_len, 1000);
134 if (res < 0) {
135 v4l2_err(tea->v4l2_dev, "request-read error: %d\n", res);
136 return res;
137 }
138
139 res = usb_interrupt_msg(shark->usbdev,
140 usb_rcvintpipe(shark->usbdev, SHARK_IN_EP),
141 shark->transfer_buffer, TB_LEN,
142 &actual_len, 1000);
143 if (res < 0) {
144 v4l2_err(tea->v4l2_dev, "read error: %d\n", res);
145 return res;
146 }
147
148 for (i = 0; i < 3; i++)
149 reg |= shark->transfer_buffer[i] << (16 - i * 8);
150
151 v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-read: %02x %02x %02x\n",
152 shark->transfer_buffer[0], shark->transfer_buffer[1],
153 shark->transfer_buffer[2]);
154
155 *reg_ret = reg;
156 return 0;
157}
158
159static struct radio_tea5777_ops shark_tea_ops = {
160 .write_reg = shark_write_reg,
161 .read_reg = shark_read_reg,
162};
163
164static void shark_led_work(struct work_struct *work)
165{
166 struct shark_device *shark =
167 container_of(work, struct shark_device, led_work);
168 int i, res, brightness, actual_len;
169 /*
170 * We use the v4l2_dev lock and registered bit to ensure the device
171 * does not get unplugged and unreffed while we're running.
172 */
173 mutex_lock(&shark->tea.mutex);
174 if (!video_is_registered(&shark->tea.vd))
175 goto leave;
176
177 for (i = 0; i < 2; i++) {
178 if (!test_and_clear_bit(i, &shark->brightness_new))
179 continue;
180
181 brightness = atomic_read(&shark->brightness[i]);
182 memset(shark->transfer_buffer, 0, TB_LEN);
183 shark->transfer_buffer[0] = 0x83 + i;
184 shark->transfer_buffer[1] = brightness;
185 res = usb_interrupt_msg(shark->usbdev,
186 usb_sndintpipe(shark->usbdev,
187 SHARK_OUT_EP),
188 shark->transfer_buffer, TB_LEN,
189 &actual_len, 1000);
190 if (res < 0)
191 v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
192 shark->led_names[i], res);
193 }
194leave:
195 mutex_unlock(&shark->tea.mutex);
196}
197
198static void shark_led_set_blue(struct led_classdev *led_cdev,
199 enum led_brightness value)
200{
201 struct shark_device *shark =
202 container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
203
204 atomic_set(&shark->brightness[BLUE_LED], value);
205 set_bit(BLUE_LED, &shark->brightness_new);
206 schedule_work(&shark->led_work);
207}
208
209static void shark_led_set_red(struct led_classdev *led_cdev,
210 enum led_brightness value)
211{
212 struct shark_device *shark =
213 container_of(led_cdev, struct shark_device, leds[RED_LED]);
214
215 atomic_set(&shark->brightness[RED_LED], value);
216 set_bit(RED_LED, &shark->brightness_new);
217 schedule_work(&shark->led_work);
218}
219
220static void usb_shark_disconnect(struct usb_interface *intf)
221{
222 struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
223 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
224 int i;
225
226 mutex_lock(&shark->tea.mutex);
227 v4l2_device_disconnect(&shark->v4l2_dev);
228 radio_tea5777_exit(&shark->tea);
229 mutex_unlock(&shark->tea.mutex);
230
231 for (i = 0; i < NO_LEDS; i++)
232 led_classdev_unregister(&shark->leds[i]);
233
234 v4l2_device_put(&shark->v4l2_dev);
235}
236
237static void usb_shark_release(struct v4l2_device *v4l2_dev)
238{
239 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
240
241 cancel_work_sync(&shark->led_work);
242 v4l2_device_unregister(&shark->v4l2_dev);
243 kfree(shark->transfer_buffer);
244 kfree(shark);
245}
246
247static int usb_shark_probe(struct usb_interface *intf,
248 const struct usb_device_id *id)
249{
250 struct shark_device *shark;
251 int i, retval = -ENOMEM;
252
253 shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
254 if (!shark)
255 return retval;
256
257 shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
258 if (!shark->transfer_buffer)
259 goto err_alloc_buffer;
260
261 /*
262 * Work around a bug in usbhid/hid-core.c, where it leaves a dangling
263 * pointer in intfdata causing v4l2-device.c to not set it. Which
264 * results in usb_shark_disconnect() referencing the dangling pointer
265 *
266 * REMOVE (as soon as the above bug is fixed, patch submitted)
267 */
268 usb_set_intfdata(intf, NULL);
269
270 shark->v4l2_dev.release = usb_shark_release;
271 v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
272 retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
273 if (retval) {
274 v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
275 goto err_reg_dev;
276 }
277
278 shark->usbdev = interface_to_usbdev(intf);
279 shark->tea.v4l2_dev = &shark->v4l2_dev;
280 shark->tea.private_data = shark;
281 shark->tea.ops = &shark_tea_ops;
282 shark->tea.has_am = true;
283 shark->tea.write_before_read = true;
284 strlcpy(shark->tea.card, "Griffin radioSHARK2",
285 sizeof(shark->tea.card));
286 usb_make_path(shark->usbdev, shark->tea.bus_info,
287 sizeof(shark->tea.bus_info));
288
289 retval = radio_tea5777_init(&shark->tea, THIS_MODULE);
290 if (retval) {
291 v4l2_err(&shark->v4l2_dev, "couldn't init tea5777\n");
292 goto err_init_tea;
293 }
294
295 INIT_WORK(&shark->led_work, shark_led_work);
296 for (i = 0; i < NO_LEDS; i++) {
297 shark->leds[i] = shark_led_templates[i];
298 snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
299 shark->leds[i].name, shark->v4l2_dev.name);
300 shark->leds[i].name = shark->led_names[i];
301 /*
302 * We don't fail the probe if we fail to register the leds,
303 * because once we've called radio_tea5777_init, the /dev/radio0
304 * node may be opened from userspace holding a reference to us!
305 *
306 * Note we cannot register the leds first instead as
307 * shark_led_work depends on the v4l2 mutex and registered bit.
308 */
309 retval = led_classdev_register(&intf->dev, &shark->leds[i]);
310 if (retval)
311 v4l2_err(&shark->v4l2_dev,
312 "couldn't register led: %s\n",
313 shark->led_names[i]);
314 }
315
316 return 0;
317
318err_init_tea:
319 v4l2_device_unregister(&shark->v4l2_dev);
320err_reg_dev:
321 kfree(shark->transfer_buffer);
322err_alloc_buffer:
323 kfree(shark);
324
325 return retval;
326}
327
328/* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
329static struct usb_device_id usb_shark_device_table[] = {
330 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
331 USB_DEVICE_ID_MATCH_INT_CLASS,
332 .idVendor = 0x077d,
333 .idProduct = 0x627a,
334 .bcdDevice_lo = 0x0010,
335 .bcdDevice_hi = 0x0010,
336 .bInterfaceClass = 3,
337 },
338 { }
339};
340MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
341
342static struct usb_driver usb_shark_driver = {
343 .name = DRV_NAME,
344 .probe = usb_shark_probe,
345 .disconnect = usb_shark_disconnect,
346 .id_table = usb_shark_device_table,
347};
348module_usb_driver(usb_shark_driver);
diff --git a/drivers/media/radio/radio-tea5777.c b/drivers/media/radio/radio-tea5777.c
new file mode 100644
index 000000000000..5bc9fa62720b
--- /dev/null
+++ b/drivers/media/radio/radio-tea5777.c
@@ -0,0 +1,491 @@
1/*
2 * v4l2 driver for TEA5777 Philips AM/FM radio tuner chips
3 *
4 * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
5 *
6 * Based on the ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips:
7 *
8 * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
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#include <linux/delay.h>
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/sched.h>
30#include <linux/slab.h>
31#include <media/v4l2-device.h>
32#include <media/v4l2-dev.h>
33#include <media/v4l2-fh.h>
34#include <media/v4l2-ioctl.h>
35#include <media/v4l2-event.h>
36#include <asm/div64.h>
37#include "radio-tea5777.h"
38
39MODULE_AUTHOR("Hans de Goede <perex@perex.cz>");
40MODULE_DESCRIPTION("Routines for control of TEA5777 Philips AM/FM radio tuner chips");
41MODULE_LICENSE("GPL");
42
43/* Fixed FM only band for now, will implement multi-band support when the
44 VIDIOC_ENUM_FREQ_BANDS API is upstream */
45#define TEA5777_FM_RANGELOW (76000 * 16)
46#define TEA5777_FM_RANGEHIGH (108000 * 16)
47
48#define TEA5777_FM_IF 150 /* kHz */
49#define TEA5777_FM_FREQ_STEP 50 /* kHz */
50
51/* Write reg, common bits */
52#define TEA5777_W_MUTE_MASK (1LL << 47)
53#define TEA5777_W_MUTE_SHIFT 47
54#define TEA5777_W_AM_FM_MASK (1LL << 46)
55#define TEA5777_W_AM_FM_SHIFT 46
56#define TEA5777_W_STB_MASK (1LL << 45)
57#define TEA5777_W_STB_SHIFT 45
58
59#define TEA5777_W_IFCE_MASK (1LL << 29)
60#define TEA5777_W_IFCE_SHIFT 29
61#define TEA5777_W_IFW_MASK (1LL << 28)
62#define TEA5777_W_IFW_SHIFT 28
63#define TEA5777_W_HILO_MASK (1LL << 27)
64#define TEA5777_W_HILO_SHIFT 27
65#define TEA5777_W_DBUS_MASK (1LL << 26)
66#define TEA5777_W_DBUS_SHIFT 26
67
68#define TEA5777_W_INTEXT_MASK (1LL << 24)
69#define TEA5777_W_INTEXT_SHIFT 24
70#define TEA5777_W_P1_MASK (1LL << 23)
71#define TEA5777_W_P1_SHIFT 23
72#define TEA5777_W_P0_MASK (1LL << 22)
73#define TEA5777_W_P0_SHIFT 22
74#define TEA5777_W_PEN1_MASK (1LL << 21)
75#define TEA5777_W_PEN1_SHIFT 21
76#define TEA5777_W_PEN0_MASK (1LL << 20)
77#define TEA5777_W_PEN0_SHIFT 20
78
79#define TEA5777_W_CHP0_MASK (1LL << 18)
80#define TEA5777_W_CHP0_SHIFT 18
81#define TEA5777_W_DEEM_MASK (1LL << 17)
82#define TEA5777_W_DEEM_SHIFT 17
83
84#define TEA5777_W_SEARCH_MASK (1LL << 7)
85#define TEA5777_W_SEARCH_SHIFT 7
86#define TEA5777_W_PROGBLIM_MASK (1LL << 6)
87#define TEA5777_W_PROGBLIM_SHIFT 6
88#define TEA5777_W_UPDWN_MASK (1LL << 5)
89#define TEA5777_W_UPDWN_SHIFT 5
90#define TEA5777_W_SLEV_MASK (3LL << 3)
91#define TEA5777_W_SLEV_SHIFT 3
92
93/* Write reg, FM specific bits */
94#define TEA5777_W_FM_PLL_MASK (0x1fffLL << 32)
95#define TEA5777_W_FM_PLL_SHIFT 32
96#define TEA5777_W_FM_FREF_MASK (0x03LL << 30)
97#define TEA5777_W_FM_FREF_SHIFT 30
98#define TEA5777_W_FM_FREF_VALUE 0 /* 50 kHz tune steps, 150 kHz IF */
99
100#define TEA5777_W_FM_FORCEMONO_MASK (1LL << 15)
101#define TEA5777_W_FM_FORCEMONO_SHIFT 15
102#define TEA5777_W_FM_SDSOFF_MASK (1LL << 14)
103#define TEA5777_W_FM_SDSOFF_SHIFT 14
104#define TEA5777_W_FM_DOFF_MASK (1LL << 13)
105#define TEA5777_W_FM_DOFF_SHIFT 13
106
107#define TEA5777_W_FM_STEP_MASK (3LL << 1)
108#define TEA5777_W_FM_STEP_SHIFT 1
109
110/* Write reg, AM specific bits */
111#define TEA5777_W_AM_PLL_MASK (0x7ffLL << 34)
112#define TEA5777_W_AM_PLL_SHIFT 34
113#define TEA5777_W_AM_AGCRF_MASK (1LL << 33)
114#define TEA5777_W_AM_AGCRF_SHIFT 33
115#define TEA5777_W_AM_AGCIF_MASK (1LL << 32)
116#define TEA5777_W_AM_AGCIF_SHIFT 32
117#define TEA5777_W_AM_MWLW_MASK (1LL << 31)
118#define TEA5777_W_AM_MWLW_SHIFT 31
119#define TEA5777_W_AM_LNA_MASK (1LL << 30)
120#define TEA5777_W_AM_LNA_SHIFT 30
121
122#define TEA5777_W_AM_PEAK_MASK (1LL << 25)
123#define TEA5777_W_AM_PEAK_SHIFT 25
124
125#define TEA5777_W_AM_RFB_MASK (1LL << 16)
126#define TEA5777_W_AM_RFB_SHIFT 16
127#define TEA5777_W_AM_CALLIGN_MASK (1LL << 15)
128#define TEA5777_W_AM_CALLIGN_SHIFT 15
129#define TEA5777_W_AM_CBANK_MASK (0x7fLL << 8)
130#define TEA5777_W_AM_CBANK_SHIFT 8
131
132#define TEA5777_W_AM_DELAY_MASK (1LL << 2)
133#define TEA5777_W_AM_DELAY_SHIFT 2
134#define TEA5777_W_AM_STEP_MASK (1LL << 1)
135#define TEA5777_W_AM_STEP_SHIFT 1
136
137/* Read reg, common bits */
138#define TEA5777_R_LEVEL_MASK (0x0f << 17)
139#define TEA5777_R_LEVEL_SHIFT 17
140#define TEA5777_R_SFOUND_MASK (0x01 << 16)
141#define TEA5777_R_SFOUND_SHIFT 16
142#define TEA5777_R_BLIM_MASK (0x01 << 15)
143#define TEA5777_R_BLIM_SHIFT 15
144
145/* Read reg, FM specific bits */
146#define TEA5777_R_FM_STEREO_MASK (0x01 << 21)
147#define TEA5777_R_FM_STEREO_SHIFT 21
148#define TEA5777_R_FM_PLL_MASK 0x1fff
149#define TEA5777_R_FM_PLL_SHIFT 0
150
151static u32 tea5777_freq_to_v4l2_freq(struct radio_tea5777 *tea, u32 freq)
152{
153 return (freq * TEA5777_FM_FREQ_STEP + TEA5777_FM_IF) * 16;
154}
155
156static int radio_tea5777_set_freq(struct radio_tea5777 *tea)
157{
158 u64 freq;
159 int res;
160
161 freq = clamp_t(u32, tea->freq,
162 TEA5777_FM_RANGELOW, TEA5777_FM_RANGEHIGH) + 8;
163 do_div(freq, 16); /* to kHz */
164
165 freq -= TEA5777_FM_IF;
166 do_div(freq, TEA5777_FM_FREQ_STEP);
167
168 tea->write_reg &= ~(TEA5777_W_FM_PLL_MASK | TEA5777_W_FM_FREF_MASK);
169 tea->write_reg |= freq << TEA5777_W_FM_PLL_SHIFT;
170 tea->write_reg |= TEA5777_W_FM_FREF_VALUE << TEA5777_W_FM_FREF_SHIFT;
171
172 res = tea->ops->write_reg(tea, tea->write_reg);
173 if (res)
174 return res;
175
176 tea->needs_write = false;
177 tea->read_reg = -1;
178 tea->freq = tea5777_freq_to_v4l2_freq(tea, freq);
179
180 return 0;
181}
182
183static int radio_tea5777_update_read_reg(struct radio_tea5777 *tea, int wait)
184{
185 int res;
186
187 if (tea->read_reg != -1)
188 return 0;
189
190 if (tea->write_before_read && tea->needs_write) {
191 res = radio_tea5777_set_freq(tea);
192 if (res)
193 return res;
194 }
195
196 if (wait) {
197 if (schedule_timeout_interruptible(msecs_to_jiffies(wait)))
198 return -ERESTARTSYS;
199 }
200
201 res = tea->ops->read_reg(tea, &tea->read_reg);
202 if (res)
203 return res;
204
205 tea->needs_write = true;
206 return 0;
207}
208
209/*
210 * Linux Video interface
211 */
212
213static int vidioc_querycap(struct file *file, void *priv,
214 struct v4l2_capability *v)
215{
216 struct radio_tea5777 *tea = video_drvdata(file);
217
218 strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
219 strlcpy(v->card, tea->card, sizeof(v->card));
220 strlcat(v->card, " TEA5777", sizeof(v->card));
221 strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
222 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
223 v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
224 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
225 return 0;
226}
227
228static int vidioc_g_tuner(struct file *file, void *priv,
229 struct v4l2_tuner *v)
230{
231 struct radio_tea5777 *tea = video_drvdata(file);
232 int res;
233
234 if (v->index > 0)
235 return -EINVAL;
236
237 res = radio_tea5777_update_read_reg(tea, 0);
238 if (res)
239 return res;
240
241 memset(v, 0, sizeof(*v));
242 if (tea->has_am)
243 strlcpy(v->name, "AM/FM", sizeof(v->name));
244 else
245 strlcpy(v->name, "FM", sizeof(v->name));
246 v->type = V4L2_TUNER_RADIO;
247 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
248 V4L2_TUNER_CAP_HWSEEK_BOUNDED;
249 v->rangelow = TEA5777_FM_RANGELOW;
250 v->rangehigh = TEA5777_FM_RANGEHIGH;
251 v->rxsubchans = (tea->read_reg & TEA5777_R_FM_STEREO_MASK) ?
252 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
253 v->audmode = (tea->write_reg & TEA5777_W_FM_FORCEMONO_MASK) ?
254 V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
255 /* shift - 12 to convert 4-bits (0-15) scale to 16-bits (0-65535) */
256 v->signal = (tea->read_reg & TEA5777_R_LEVEL_MASK) >>
257 (TEA5777_R_LEVEL_SHIFT - 12);
258
259 /* Invalidate read_reg, so that next call we return up2date signal */
260 tea->read_reg = -1;
261
262 return 0;
263}
264
265static int vidioc_s_tuner(struct file *file, void *priv,
266 struct v4l2_tuner *v)
267{
268 struct radio_tea5777 *tea = video_drvdata(file);
269
270 if (v->index)
271 return -EINVAL;
272
273 if (v->audmode == V4L2_TUNER_MODE_MONO)
274 tea->write_reg |= TEA5777_W_FM_FORCEMONO_MASK;
275 else
276 tea->write_reg &= ~TEA5777_W_FM_FORCEMONO_MASK;
277
278 return radio_tea5777_set_freq(tea);
279}
280
281static int vidioc_g_frequency(struct file *file, void *priv,
282 struct v4l2_frequency *f)
283{
284 struct radio_tea5777 *tea = video_drvdata(file);
285
286 if (f->tuner != 0)
287 return -EINVAL;
288 f->type = V4L2_TUNER_RADIO;
289 f->frequency = tea->freq;
290 return 0;
291}
292
293static int vidioc_s_frequency(struct file *file, void *priv,
294 struct v4l2_frequency *f)
295{
296 struct radio_tea5777 *tea = video_drvdata(file);
297
298 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
299 return -EINVAL;
300
301 tea->freq = f->frequency;
302 return radio_tea5777_set_freq(tea);
303}
304
305static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
306 struct v4l2_hw_freq_seek *a)
307{
308 struct radio_tea5777 *tea = video_drvdata(file);
309 u32 orig_freq = tea->freq;
310 unsigned long timeout;
311 int res, spacing = 200 * 16; /* 200 kHz */
312 /* These are fixed *for now* */
313 const u32 seek_rangelow = TEA5777_FM_RANGELOW;
314 const u32 seek_rangehigh = TEA5777_FM_RANGEHIGH;
315
316 if (a->tuner || a->wrap_around)
317 return -EINVAL;
318
319 tea->write_reg |= TEA5777_W_PROGBLIM_MASK;
320 if (seek_rangelow != tea->seek_rangelow) {
321 tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
322 tea->freq = seek_rangelow;
323 res = radio_tea5777_set_freq(tea);
324 if (res)
325 goto leave;
326 tea->seek_rangelow = tea->freq;
327 }
328 if (seek_rangehigh != tea->seek_rangehigh) {
329 tea->write_reg |= TEA5777_W_UPDWN_MASK;
330 tea->freq = seek_rangehigh;
331 res = radio_tea5777_set_freq(tea);
332 if (res)
333 goto leave;
334 tea->seek_rangehigh = tea->freq;
335 }
336 tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
337
338 tea->write_reg |= TEA5777_W_SEARCH_MASK;
339 if (a->seek_upward) {
340 tea->write_reg |= TEA5777_W_UPDWN_MASK;
341 tea->freq = orig_freq + spacing;
342 } else {
343 tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
344 tea->freq = orig_freq - spacing;
345 }
346 res = radio_tea5777_set_freq(tea);
347 if (res)
348 goto leave;
349
350 timeout = jiffies + msecs_to_jiffies(5000);
351 for (;;) {
352 if (time_after(jiffies, timeout)) {
353 res = -ENODATA;
354 break;
355 }
356
357 res = radio_tea5777_update_read_reg(tea, 100);
358 if (res)
359 break;
360
361 /*
362 * Note we use tea->freq to track how far we've searched sofar
363 * this is necessary to ensure we continue seeking at the right
364 * point, in the write_before_read case.
365 */
366 tea->freq = (tea->read_reg & TEA5777_R_FM_PLL_MASK);
367 tea->freq = tea5777_freq_to_v4l2_freq(tea, tea->freq);
368
369 if ((tea->read_reg & TEA5777_R_SFOUND_MASK)) {
370 tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
371 return 0;
372 }
373
374 if (tea->read_reg & TEA5777_R_BLIM_MASK) {
375 res = -ENODATA;
376 break;
377 }
378
379 /* Force read_reg update */
380 tea->read_reg = -1;
381 }
382leave:
383 tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
384 tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
385 tea->freq = orig_freq;
386 radio_tea5777_set_freq(tea);
387 return res;
388}
389
390static int tea575x_s_ctrl(struct v4l2_ctrl *c)
391{
392 struct radio_tea5777 *tea =
393 container_of(c->handler, struct radio_tea5777, ctrl_handler);
394
395 switch (c->id) {
396 case V4L2_CID_AUDIO_MUTE:
397 if (c->val)
398 tea->write_reg |= TEA5777_W_MUTE_MASK;
399 else
400 tea->write_reg &= ~TEA5777_W_MUTE_MASK;
401
402 return radio_tea5777_set_freq(tea);
403 }
404
405 return -EINVAL;
406}
407
408static const struct v4l2_file_operations tea575x_fops = {
409 .unlocked_ioctl = video_ioctl2,
410 .open = v4l2_fh_open,
411 .release = v4l2_fh_release,
412 .poll = v4l2_ctrl_poll,
413};
414
415static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
416 .vidioc_querycap = vidioc_querycap,
417 .vidioc_g_tuner = vidioc_g_tuner,
418 .vidioc_s_tuner = vidioc_s_tuner,
419 .vidioc_g_frequency = vidioc_g_frequency,
420 .vidioc_s_frequency = vidioc_s_frequency,
421 .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
422 .vidioc_log_status = v4l2_ctrl_log_status,
423 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
424 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
425};
426
427static const struct video_device tea575x_radio = {
428 .ioctl_ops = &tea575x_ioctl_ops,
429 .release = video_device_release_empty,
430};
431
432static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
433 .s_ctrl = tea575x_s_ctrl,
434};
435
436int radio_tea5777_init(struct radio_tea5777 *tea, struct module *owner)
437{
438 int res;
439
440 tea->write_reg = (1LL << TEA5777_W_IFCE_SHIFT) |
441 (1LL << TEA5777_W_IFW_SHIFT) |
442 (1LL << TEA5777_W_INTEXT_SHIFT) |
443 (1LL << TEA5777_W_CHP0_SHIFT) |
444 (2LL << TEA5777_W_SLEV_SHIFT);
445 tea->freq = 90500 * 16; /* 90.5Mhz default */
446 res = radio_tea5777_set_freq(tea);
447 if (res) {
448 v4l2_err(tea->v4l2_dev, "can't set initial freq (%d)\n", res);
449 return res;
450 }
451
452 tea->vd = tea575x_radio;
453 video_set_drvdata(&tea->vd, tea);
454 mutex_init(&tea->mutex);
455 strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
456 tea->vd.lock = &tea->mutex;
457 tea->vd.v4l2_dev = tea->v4l2_dev;
458 tea->fops = tea575x_fops;
459 tea->fops.owner = owner;
460 tea->vd.fops = &tea->fops;
461 set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);
462
463 tea->vd.ctrl_handler = &tea->ctrl_handler;
464 v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
465 v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
466 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
467 res = tea->ctrl_handler.error;
468 if (res) {
469 v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
470 v4l2_ctrl_handler_free(&tea->ctrl_handler);
471 return res;
472 }
473 v4l2_ctrl_handler_setup(&tea->ctrl_handler);
474
475 res = video_register_device(&tea->vd, VFL_TYPE_RADIO, -1);
476 if (res) {
477 v4l2_err(tea->v4l2_dev, "can't register video device!\n");
478 v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
479 return res;
480 }
481
482 return 0;
483}
484EXPORT_SYMBOL_GPL(radio_tea5777_init);
485
486void radio_tea5777_exit(struct radio_tea5777 *tea)
487{
488 video_unregister_device(&tea->vd);
489 v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
490}
491EXPORT_SYMBOL_GPL(radio_tea5777_exit);
diff --git a/drivers/media/radio/radio-tea5777.h b/drivers/media/radio/radio-tea5777.h
new file mode 100644
index 000000000000..55cbd78df5ed
--- /dev/null
+++ b/drivers/media/radio/radio-tea5777.h
@@ -0,0 +1,87 @@
1#ifndef __RADIO_TEA5777_H
2#define __RADIO_TEA5777_H
3
4/*
5 * v4l2 driver for TEA5777 Philips AM/FM radio tuner chips
6 *
7 * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
8 *
9 * Based on the ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips:
10 *
11 * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
12 * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30#include <linux/videodev2.h>
31#include <media/v4l2-ctrls.h>
32#include <media/v4l2-dev.h>
33#include <media/v4l2-device.h>
34
35#define TEA575X_FMIF 10700
36#define TEA575X_AMIF 450
37
38struct radio_tea5777;
39
40struct radio_tea5777_ops {
41 /*
42 * Write the 6 bytes large write register of the tea5777
43 *
44 * val represents the 6 write registers, with byte 1 from the
45 * datasheet being the most significant byte (so byte 5 of the u64),
46 * and byte 6 from the datasheet being the least significant byte.
47 *
48 * returns 0 on success.
49 */
50 int (*write_reg)(struct radio_tea5777 *tea, u64 val);
51 /*
52 * Read the 3 bytes large read register of the tea5777
53 *
54 * The read value gets returned in val, akin to write_reg, byte 1 from
55 * the datasheet is stored as the most significant byte (so byte 2 of
56 * the u32), and byte 3 from the datasheet gets stored as the least
57 * significant byte (iow byte 0 of the u32).
58 *
59 * returns 0 on success.
60 */
61 int (*read_reg)(struct radio_tea5777 *tea, u32 *val);
62};
63
64struct radio_tea5777 {
65 struct v4l2_device *v4l2_dev;
66 struct v4l2_file_operations fops;
67 struct video_device vd; /* video device */
68 bool has_am; /* Device can tune to AM freqs */
69 bool write_before_read; /* must write before read quirk */
70 bool needs_write; /* for write before read quirk */
71 u32 freq; /* current frequency */
72 u32 seek_rangelow; /* current hwseek limits */
73 u32 seek_rangehigh;
74 u32 read_reg;
75 u64 write_reg;
76 struct mutex mutex;
77 struct radio_tea5777_ops *ops;
78 void *private_data;
79 u8 card[32];
80 u8 bus_info[32];
81 struct v4l2_ctrl_handler ctrl_handler;
82};
83
84int radio_tea5777_init(struct radio_tea5777 *tea, struct module *owner);
85void radio_tea5777_exit(struct radio_tea5777 *tea);
86
87#endif /* __RADIO_TEA5777_H */
diff --git a/drivers/media/radio/si470x/radio-si470x-common.c b/drivers/media/radio/si470x/radio-si470x-common.c
index d485b79222fd..9e38132afec6 100644
--- a/drivers/media/radio/si470x/radio-si470x-common.c
+++ b/drivers/media/radio/si470x/radio-si470x-common.c
@@ -4,6 +4,7 @@
4 * Driver for radios with Silicon Labs Si470x FM Radio Receivers 4 * Driver for radios with Silicon Labs Si470x FM Radio Receivers
5 * 5 *
6 * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net> 6 * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
7 * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
7 * 8 *
8 * This program is free software; you can redistribute it and/or modify 9 * 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 * it under the terms of the GNU General Public License as published by
@@ -127,14 +128,6 @@ static unsigned short space = 2;
127module_param(space, ushort, 0444); 128module_param(space, ushort, 0444);
128MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*"); 129MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
129 130
130/* Bottom of Band (MHz) */
131/* 0: 87.5 - 108 MHz (USA, Europe)*/
132/* 1: 76 - 108 MHz (Japan wide band) */
133/* 2: 76 - 90 MHz (Japan) */
134static unsigned short band = 1;
135module_param(band, ushort, 0444);
136MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
137
138/* De-emphasis */ 131/* De-emphasis */
139/* 0: 75 us (USA) */ 132/* 0: 75 us (USA) */
140/* 1: 50 us (Europe, Australia, Japan) */ 133/* 1: 50 us (Europe, Australia, Japan) */
@@ -152,19 +145,66 @@ static unsigned int seek_timeout = 5000;
152module_param(seek_timeout, uint, 0644); 145module_param(seek_timeout, uint, 0644);
153MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*"); 146MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
154 147
155 148static const struct v4l2_frequency_band bands[] = {
149 {
150 .type = V4L2_TUNER_RADIO,
151 .index = 0,
152 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
153 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
154 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
155 V4L2_TUNER_CAP_HWSEEK_WRAP,
156 .rangelow = 87500 * 16,
157 .rangehigh = 108000 * 16,
158 .modulation = V4L2_BAND_MODULATION_FM,
159 },
160 {
161 .type = V4L2_TUNER_RADIO,
162 .index = 1,
163 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
164 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
165 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
166 V4L2_TUNER_CAP_HWSEEK_WRAP,
167 .rangelow = 76000 * 16,
168 .rangehigh = 108000 * 16,
169 .modulation = V4L2_BAND_MODULATION_FM,
170 },
171 {
172 .type = V4L2_TUNER_RADIO,
173 .index = 2,
174 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
175 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
176 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
177 V4L2_TUNER_CAP_HWSEEK_WRAP,
178 .rangelow = 76000 * 16,
179 .rangehigh = 90000 * 16,
180 .modulation = V4L2_BAND_MODULATION_FM,
181 },
182};
156 183
157/************************************************************************** 184/**************************************************************************
158 * Generic Functions 185 * Generic Functions
159 **************************************************************************/ 186 **************************************************************************/
160 187
161/* 188/*
189 * si470x_set_band - set the band
190 */
191static int si470x_set_band(struct si470x_device *radio, int band)
192{
193 if (radio->band == band)
194 return 0;
195
196 radio->band = band;
197 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_BAND;
198 radio->registers[SYSCONFIG2] |= radio->band << 6;
199 return si470x_set_register(radio, SYSCONFIG2);
200}
201
202/*
162 * si470x_set_chan - set the channel 203 * si470x_set_chan - set the channel
163 */ 204 */
164static int si470x_set_chan(struct si470x_device *radio, unsigned short chan) 205static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
165{ 206{
166 int retval; 207 int retval;
167 unsigned long timeout;
168 bool timed_out = 0; 208 bool timed_out = 0;
169 209
170 /* start tuning */ 210 /* start tuning */
@@ -174,26 +214,12 @@ static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
174 if (retval < 0) 214 if (retval < 0)
175 goto done; 215 goto done;
176 216
177 /* currently I2C driver only uses interrupt way to tune */ 217 /* wait till tune operation has completed */
178 if (radio->stci_enabled) { 218 INIT_COMPLETION(radio->completion);
179 INIT_COMPLETION(radio->completion); 219 retval = wait_for_completion_timeout(&radio->completion,
180 220 msecs_to_jiffies(tune_timeout));
181 /* wait till tune operation has completed */ 221 if (!retval)
182 retval = wait_for_completion_timeout(&radio->completion, 222 timed_out = true;
183 msecs_to_jiffies(tune_timeout));
184 if (!retval)
185 timed_out = true;
186 } else {
187 /* wait till tune operation has completed */
188 timeout = jiffies + msecs_to_jiffies(tune_timeout);
189 do {
190 retval = si470x_get_register(radio, STATUSRSSI);
191 if (retval < 0)
192 goto stop;
193 timed_out = time_after(jiffies, timeout);
194 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
195 && (!timed_out));
196 }
197 223
198 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) 224 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
199 dev_warn(&radio->videodev.dev, "tune does not complete\n"); 225 dev_warn(&radio->videodev.dev, "tune does not complete\n");
@@ -201,7 +227,6 @@ static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
201 dev_warn(&radio->videodev.dev, 227 dev_warn(&radio->videodev.dev,
202 "tune timed out after %u ms\n", tune_timeout); 228 "tune timed out after %u ms\n", tune_timeout);
203 229
204stop:
205 /* stop tuning */ 230 /* stop tuning */
206 radio->registers[CHANNEL] &= ~CHANNEL_TUNE; 231 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
207 retval = si470x_set_register(radio, CHANNEL); 232 retval = si470x_set_register(radio, CHANNEL);
@@ -210,48 +235,39 @@ done:
210 return retval; 235 return retval;
211} 236}
212 237
213
214/* 238/*
215 * si470x_get_freq - get the frequency 239 * si470x_get_step - get channel spacing
216 */ 240 */
217static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq) 241static unsigned int si470x_get_step(struct si470x_device *radio)
218{ 242{
219 unsigned int spacing, band_bottom;
220 unsigned short chan;
221 int retval;
222
223 /* Spacing (kHz) */ 243 /* Spacing (kHz) */
224 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) { 244 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
225 /* 0: 200 kHz (USA, Australia) */ 245 /* 0: 200 kHz (USA, Australia) */
226 case 0: 246 case 0:
227 spacing = 0.200 * FREQ_MUL; break; 247 return 200 * 16;
228 /* 1: 100 kHz (Europe, Japan) */ 248 /* 1: 100 kHz (Europe, Japan) */
229 case 1: 249 case 1:
230 spacing = 0.100 * FREQ_MUL; break; 250 return 100 * 16;
231 /* 2: 50 kHz */ 251 /* 2: 50 kHz */
232 default: 252 default:
233 spacing = 0.050 * FREQ_MUL; break; 253 return 50 * 16;
234 }; 254 };
255}
235 256
236 /* Bottom of Band (MHz) */ 257
237 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) { 258/*
238 /* 0: 87.5 - 108 MHz (USA, Europe) */ 259 * si470x_get_freq - get the frequency
239 case 0: 260 */
240 band_bottom = 87.5 * FREQ_MUL; break; 261static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
241 /* 1: 76 - 108 MHz (Japan wide band) */ 262{
242 default: 263 int chan, retval;
243 band_bottom = 76 * FREQ_MUL; break;
244 /* 2: 76 - 90 MHz (Japan) */
245 case 2:
246 band_bottom = 76 * FREQ_MUL; break;
247 };
248 264
249 /* read channel */ 265 /* read channel */
250 retval = si470x_get_register(radio, READCHAN); 266 retval = si470x_get_register(radio, READCHAN);
251 chan = radio->registers[READCHAN] & READCHAN_READCHAN; 267 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
252 268
253 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */ 269 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
254 *freq = chan * spacing + band_bottom; 270 *freq = chan * si470x_get_step(radio) + bands[radio->band].rangelow;
255 271
256 return retval; 272 return retval;
257} 273}
@@ -262,44 +278,12 @@ static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
262 */ 278 */
263int si470x_set_freq(struct si470x_device *radio, unsigned int freq) 279int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
264{ 280{
265 unsigned int spacing, band_bottom, band_top;
266 unsigned short chan; 281 unsigned short chan;
267 282
268 /* Spacing (kHz) */ 283 freq = clamp(freq, bands[radio->band].rangelow,
269 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) { 284 bands[radio->band].rangehigh);
270 /* 0: 200 kHz (USA, Australia) */
271 case 0:
272 spacing = 0.200 * FREQ_MUL; break;
273 /* 1: 100 kHz (Europe, Japan) */
274 case 1:
275 spacing = 0.100 * FREQ_MUL; break;
276 /* 2: 50 kHz */
277 default:
278 spacing = 0.050 * FREQ_MUL; break;
279 };
280
281 /* Bottom/Top of Band (MHz) */
282 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
283 /* 0: 87.5 - 108 MHz (USA, Europe) */
284 case 0:
285 band_bottom = 87.5 * FREQ_MUL;
286 band_top = 108 * FREQ_MUL;
287 break;
288 /* 1: 76 - 108 MHz (Japan wide band) */
289 default:
290 band_bottom = 76 * FREQ_MUL;
291 band_top = 108 * FREQ_MUL;
292 break;
293 /* 2: 76 - 90 MHz (Japan) */
294 case 2:
295 band_bottom = 76 * FREQ_MUL;
296 band_top = 90 * FREQ_MUL;
297 break;
298 };
299
300 freq = clamp(freq, band_bottom, band_top);
301 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */ 285 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
302 chan = (freq - band_bottom) / spacing; 286 chan = (freq - bands[radio->band].rangelow) / si470x_get_step(radio);
303 287
304 return si470x_set_chan(radio, chan); 288 return si470x_set_chan(radio, chan);
305} 289}
@@ -309,19 +293,43 @@ int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
309 * si470x_set_seek - set seek 293 * si470x_set_seek - set seek
310 */ 294 */
311static int si470x_set_seek(struct si470x_device *radio, 295static int si470x_set_seek(struct si470x_device *radio,
312 unsigned int wrap_around, unsigned int seek_upward) 296 struct v4l2_hw_freq_seek *seek)
313{ 297{
314 int retval = 0; 298 int band, retval;
315 unsigned long timeout; 299 unsigned int freq;
316 bool timed_out = 0; 300 bool timed_out = 0;
317 301
302 /* set band */
303 if (seek->rangelow || seek->rangehigh) {
304 for (band = 0; band < ARRAY_SIZE(bands); band++) {
305 if (bands[band].rangelow == seek->rangelow &&
306 bands[band].rangehigh == seek->rangehigh)
307 break;
308 }
309 if (band == ARRAY_SIZE(bands))
310 return -EINVAL; /* No matching band found */
311 } else
312 band = 1; /* If nothing is specified seek 76 - 108 Mhz */
313
314 if (radio->band != band) {
315 retval = si470x_get_freq(radio, &freq);
316 if (retval)
317 return retval;
318 retval = si470x_set_band(radio, band);
319 if (retval)
320 return retval;
321 retval = si470x_set_freq(radio, freq);
322 if (retval)
323 return retval;
324 }
325
318 /* start seeking */ 326 /* start seeking */
319 radio->registers[POWERCFG] |= POWERCFG_SEEK; 327 radio->registers[POWERCFG] |= POWERCFG_SEEK;
320 if (wrap_around == 1) 328 if (seek->wrap_around)
321 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE; 329 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
322 else 330 else
323 radio->registers[POWERCFG] |= POWERCFG_SKMODE; 331 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
324 if (seek_upward == 1) 332 if (seek->seek_upward)
325 radio->registers[POWERCFG] |= POWERCFG_SEEKUP; 333 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
326 else 334 else
327 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP; 335 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
@@ -329,26 +337,12 @@ static int si470x_set_seek(struct si470x_device *radio,
329 if (retval < 0) 337 if (retval < 0)
330 return retval; 338 return retval;
331 339
332 /* currently I2C driver only uses interrupt way to seek */ 340 /* wait till tune operation has completed */
333 if (radio->stci_enabled) { 341 INIT_COMPLETION(radio->completion);
334 INIT_COMPLETION(radio->completion); 342 retval = wait_for_completion_timeout(&radio->completion,
335 343 msecs_to_jiffies(seek_timeout));
336 /* wait till seek operation has completed */ 344 if (!retval)
337 retval = wait_for_completion_timeout(&radio->completion, 345 timed_out = true;
338 msecs_to_jiffies(seek_timeout));
339 if (!retval)
340 timed_out = true;
341 } else {
342 /* wait till seek operation has completed */
343 timeout = jiffies + msecs_to_jiffies(seek_timeout);
344 do {
345 retval = si470x_get_register(radio, STATUSRSSI);
346 if (retval < 0)
347 goto stop;
348 timed_out = time_after(jiffies, timeout);
349 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
350 && (!timed_out));
351 }
352 346
353 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) 347 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
354 dev_warn(&radio->videodev.dev, "seek does not complete\n"); 348 dev_warn(&radio->videodev.dev, "seek does not complete\n");
@@ -356,7 +350,6 @@ static int si470x_set_seek(struct si470x_device *radio,
356 dev_warn(&radio->videodev.dev, 350 dev_warn(&radio->videodev.dev,
357 "seek failed / band limit reached\n"); 351 "seek failed / band limit reached\n");
358 352
359stop:
360 /* stop seeking */ 353 /* stop seeking */
361 radio->registers[POWERCFG] &= ~POWERCFG_SEEK; 354 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
362 retval = si470x_set_register(radio, POWERCFG); 355 retval = si470x_set_register(radio, POWERCFG);
@@ -391,8 +384,8 @@ int si470x_start(struct si470x_device *radio)
391 384
392 /* sysconfig 2 */ 385 /* sysconfig 2 */
393 radio->registers[SYSCONFIG2] = 386 radio->registers[SYSCONFIG2] =
394 (0x3f << 8) | /* SEEKTH */ 387 (0x1f << 8) | /* SEEKTH */
395 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */ 388 ((radio->band << 6) & SYSCONFIG2_BAND) |/* BAND */
396 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */ 389 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
397 15; /* VOLUME (max) */ 390 15; /* VOLUME (max) */
398 retval = si470x_set_register(radio, SYSCONFIG2); 391 retval = si470x_set_register(radio, SYSCONFIG2);
@@ -583,14 +576,16 @@ static int si470x_vidioc_g_tuner(struct file *file, void *priv,
583 struct v4l2_tuner *tuner) 576 struct v4l2_tuner *tuner)
584{ 577{
585 struct si470x_device *radio = video_drvdata(file); 578 struct si470x_device *radio = video_drvdata(file);
586 int retval; 579 int retval = 0;
587 580
588 if (tuner->index != 0) 581 if (tuner->index != 0)
589 return -EINVAL; 582 return -EINVAL;
590 583
591 retval = si470x_get_register(radio, STATUSRSSI); 584 if (!radio->status_rssi_auto_update) {
592 if (retval < 0) 585 retval = si470x_get_register(radio, STATUSRSSI);
593 return retval; 586 if (retval < 0)
587 return retval;
588 }
594 589
595 /* driver constants */ 590 /* driver constants */
596 strcpy(tuner->name, "FM"); 591 strcpy(tuner->name, "FM");
@@ -599,25 +594,8 @@ static int si470x_vidioc_g_tuner(struct file *file, void *priv,
599 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO | 594 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
600 V4L2_TUNER_CAP_HWSEEK_BOUNDED | 595 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
601 V4L2_TUNER_CAP_HWSEEK_WRAP; 596 V4L2_TUNER_CAP_HWSEEK_WRAP;
602 597 tuner->rangelow = 76 * FREQ_MUL;
603 /* range limits */ 598 tuner->rangehigh = 108 * FREQ_MUL;
604 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
605 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
606 default:
607 tuner->rangelow = 87.5 * FREQ_MUL;
608 tuner->rangehigh = 108 * FREQ_MUL;
609 break;
610 /* 1: 76 - 108 MHz (Japan wide band) */
611 case 1:
612 tuner->rangelow = 76 * FREQ_MUL;
613 tuner->rangehigh = 108 * FREQ_MUL;
614 break;
615 /* 2: 76 - 90 MHz (Japan) */
616 case 2:
617 tuner->rangelow = 76 * FREQ_MUL;
618 tuner->rangehigh = 90 * FREQ_MUL;
619 break;
620 };
621 599
622 /* stereo indicator == stereo (instead of mono) */ 600 /* stereo indicator == stereo (instead of mono) */
623 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0) 601 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
@@ -700,10 +678,18 @@ static int si470x_vidioc_s_frequency(struct file *file, void *priv,
700 struct v4l2_frequency *freq) 678 struct v4l2_frequency *freq)
701{ 679{
702 struct si470x_device *radio = video_drvdata(file); 680 struct si470x_device *radio = video_drvdata(file);
681 int retval;
703 682
704 if (freq->tuner != 0) 683 if (freq->tuner != 0)
705 return -EINVAL; 684 return -EINVAL;
706 685
686 if (freq->frequency < bands[radio->band].rangelow ||
687 freq->frequency > bands[radio->band].rangehigh) {
688 /* Switch to band 1 which covers everything we support */
689 retval = si470x_set_band(radio, 1);
690 if (retval)
691 return retval;
692 }
707 return si470x_set_freq(radio, freq->frequency); 693 return si470x_set_freq(radio, freq->frequency);
708} 694}
709 695
@@ -719,7 +705,21 @@ static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
719 if (seek->tuner != 0) 705 if (seek->tuner != 0)
720 return -EINVAL; 706 return -EINVAL;
721 707
722 return si470x_set_seek(radio, seek->wrap_around, seek->seek_upward); 708 return si470x_set_seek(radio, seek);
709}
710
711/*
712 * si470x_vidioc_enum_freq_bands - enumerate supported bands
713 */
714static int si470x_vidioc_enum_freq_bands(struct file *file, void *priv,
715 struct v4l2_frequency_band *band)
716{
717 if (band->tuner != 0)
718 return -EINVAL;
719 if (band->index >= ARRAY_SIZE(bands))
720 return -EINVAL;
721 *band = bands[band->index];
722 return 0;
723} 723}
724 724
725const struct v4l2_ctrl_ops si470x_ctrl_ops = { 725const struct v4l2_ctrl_ops si470x_ctrl_ops = {
@@ -736,6 +736,7 @@ static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
736 .vidioc_g_frequency = si470x_vidioc_g_frequency, 736 .vidioc_g_frequency = si470x_vidioc_g_frequency,
737 .vidioc_s_frequency = si470x_vidioc_s_frequency, 737 .vidioc_s_frequency = si470x_vidioc_s_frequency,
738 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek, 738 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
739 .vidioc_enum_freq_bands = si470x_vidioc_enum_freq_bands,
739 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 740 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
740 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 741 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
741}; 742};
diff --git a/drivers/media/radio/si470x/radio-si470x-i2c.c b/drivers/media/radio/si470x/radio-si470x-i2c.c
index a80044c5874e..643a6ff7c5d0 100644
--- a/drivers/media/radio/si470x/radio-si470x-i2c.c
+++ b/drivers/media/radio/si470x/radio-si470x-i2c.c
@@ -350,7 +350,9 @@ static int __devinit si470x_i2c_probe(struct i2c_client *client,
350 } 350 }
351 351
352 radio->client = client; 352 radio->client = client;
353 radio->band = 1; /* Default to 76 - 108 MHz */
353 mutex_init(&radio->lock); 354 mutex_init(&radio->lock);
355 init_completion(&radio->completion);
354 356
355 /* video device initialization */ 357 /* video device initialization */
356 radio->videodev = si470x_viddev_template; 358 radio->videodev = si470x_viddev_template;
@@ -406,10 +408,6 @@ static int __devinit si470x_i2c_probe(struct i2c_client *client,
406 radio->rd_index = 0; 408 radio->rd_index = 0;
407 init_waitqueue_head(&radio->read_queue); 409 init_waitqueue_head(&radio->read_queue);
408 410
409 /* mark Seek/Tune Complete Interrupt enabled */
410 radio->stci_enabled = true;
411 init_completion(&radio->completion);
412
413 retval = request_threaded_irq(client->irq, NULL, si470x_i2c_interrupt, 411 retval = request_threaded_irq(client->irq, NULL, si470x_i2c_interrupt,
414 IRQF_TRIGGER_FALLING, DRIVER_NAME, radio); 412 IRQF_TRIGGER_FALLING, DRIVER_NAME, radio);
415 if (retval) { 413 if (retval) {
diff --git a/drivers/media/radio/si470x/radio-si470x-usb.c b/drivers/media/radio/si470x/radio-si470x-usb.c
index f412f7ab270b..146be4263ea1 100644
--- a/drivers/media/radio/si470x/radio-si470x-usb.c
+++ b/drivers/media/radio/si470x/radio-si470x-usb.c
@@ -143,7 +143,7 @@ MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
143 * Software/Hardware Versions from Scratch Page 143 * Software/Hardware Versions from Scratch Page
144 **************************************************************************/ 144 **************************************************************************/
145#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6 145#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
146#define RADIO_SW_VERSION 7 146#define RADIO_SW_VERSION 1
147#define RADIO_HW_VERSION 1 147#define RADIO_HW_VERSION 1
148 148
149 149
@@ -399,12 +399,19 @@ static void si470x_int_in_callback(struct urb *urb)
399 } 399 }
400 } 400 }
401 401
402 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) 402 /* Sometimes the device returns len 0 packets */
403 if (urb->actual_length != RDS_REPORT_SIZE)
403 goto resubmit; 404 goto resubmit;
404 405
405 if (urb->actual_length > 0) { 406 radio->registers[STATUSRSSI] =
407 get_unaligned_be16(&radio->int_in_buffer[1]);
408
409 if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)
410 complete(&radio->completion);
411
412 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)) {
406 /* Update RDS registers with URB data */ 413 /* Update RDS registers with URB data */
407 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++) 414 for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++)
408 radio->registers[STATUSRSSI + regnr] = 415 radio->registers[STATUSRSSI + regnr] =
409 get_unaligned_be16(&radio->int_in_buffer[ 416 get_unaligned_be16(&radio->int_in_buffer[
410 regnr * RADIO_REGISTER_SIZE + 1]); 417 regnr * RADIO_REGISTER_SIZE + 1]);
@@ -480,6 +487,7 @@ resubmit:
480 radio->int_in_running = 0; 487 radio->int_in_running = 0;
481 } 488 }
482 } 489 }
490 radio->status_rssi_auto_update = radio->int_in_running;
483} 491}
484 492
485 493
@@ -534,13 +542,6 @@ static int si470x_start_usb(struct si470x_device *radio)
534{ 542{
535 int retval; 543 int retval;
536 544
537 /* start radio */
538 retval = si470x_start(radio);
539 if (retval < 0)
540 return retval;
541
542 v4l2_ctrl_handler_setup(&radio->hdl);
543
544 /* initialize interrupt urb */ 545 /* initialize interrupt urb */
545 usb_fill_int_urb(radio->int_in_urb, radio->usbdev, 546 usb_fill_int_urb(radio->int_in_urb, radio->usbdev,
546 usb_rcvintpipe(radio->usbdev, 547 usb_rcvintpipe(radio->usbdev,
@@ -560,6 +561,15 @@ static int si470x_start_usb(struct si470x_device *radio)
560 "submitting int urb failed (%d)\n", retval); 561 "submitting int urb failed (%d)\n", retval);
561 radio->int_in_running = 0; 562 radio->int_in_running = 0;
562 } 563 }
564 radio->status_rssi_auto_update = radio->int_in_running;
565
566 /* start radio */
567 retval = si470x_start(radio);
568 if (retval < 0)
569 return retval;
570
571 v4l2_ctrl_handler_setup(&radio->hdl);
572
563 return retval; 573 return retval;
564} 574}
565 575
@@ -587,7 +597,9 @@ static int si470x_usb_driver_probe(struct usb_interface *intf,
587 } 597 }
588 radio->usbdev = interface_to_usbdev(intf); 598 radio->usbdev = interface_to_usbdev(intf);
589 radio->intf = intf; 599 radio->intf = intf;
600 radio->band = 1; /* Default to 76 - 108 MHz */
590 mutex_init(&radio->lock); 601 mutex_init(&radio->lock);
602 init_completion(&radio->completion);
591 603
592 iface_desc = intf->cur_altsetting; 604 iface_desc = intf->cur_altsetting;
593 605
@@ -698,9 +710,6 @@ static int si470x_usb_driver_probe(struct usb_interface *intf,
698 "linux-media@vger.kernel.org\n"); 710 "linux-media@vger.kernel.org\n");
699 } 711 }
700 712
701 /* set initial frequency */
702 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
703
704 /* set led to connect state */ 713 /* set led to connect state */
705 si470x_set_led_state(radio, BLINK_GREEN_LED); 714 si470x_set_led_state(radio, BLINK_GREEN_LED);
706 715
@@ -723,6 +732,9 @@ static int si470x_usb_driver_probe(struct usb_interface *intf,
723 if (retval < 0) 732 if (retval < 0)
724 goto err_all; 733 goto err_all;
725 734
735 /* set initial frequency */
736 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
737
726 /* register video device */ 738 /* register video device */
727 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, 739 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
728 radio_nr); 740 radio_nr);
@@ -781,11 +793,16 @@ static int si470x_usb_driver_suspend(struct usb_interface *intf,
781static int si470x_usb_driver_resume(struct usb_interface *intf) 793static int si470x_usb_driver_resume(struct usb_interface *intf)
782{ 794{
783 struct si470x_device *radio = usb_get_intfdata(intf); 795 struct si470x_device *radio = usb_get_intfdata(intf);
796 int ret;
784 797
785 dev_info(&intf->dev, "resuming now...\n"); 798 dev_info(&intf->dev, "resuming now...\n");
786 799
787 /* start radio */ 800 /* start radio */
788 return si470x_start_usb(radio); 801 ret = si470x_start_usb(radio);
802 if (ret == 0)
803 v4l2_ctrl_handler_setup(&radio->hdl);
804
805 return ret;
789} 806}
790 807
791 808
diff --git a/drivers/media/radio/si470x/radio-si470x.h b/drivers/media/radio/si470x/radio-si470x.h
index 4921cab8e0fa..2f089b4252df 100644
--- a/drivers/media/radio/si470x/radio-si470x.h
+++ b/drivers/media/radio/si470x/radio-si470x.h
@@ -87,7 +87,7 @@
87 87
88#define SYSCONFIG2 5 /* System Configuration 2 */ 88#define SYSCONFIG2 5 /* System Configuration 2 */
89#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */ 89#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
90#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */ 90#define SYSCONFIG2_BAND 0x00c0 /* bits 07..06: Band Select */
91#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */ 91#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
92#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */ 92#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
93 93
@@ -147,6 +147,7 @@ struct si470x_device {
147 struct v4l2_device v4l2_dev; 147 struct v4l2_device v4l2_dev;
148 struct video_device videodev; 148 struct video_device videodev;
149 struct v4l2_ctrl_handler hdl; 149 struct v4l2_ctrl_handler hdl;
150 int band;
150 151
151 /* Silabs internal registers (0..15) */ 152 /* Silabs internal registers (0..15) */
152 unsigned short registers[RADIO_REGISTER_NUM]; 153 unsigned short registers[RADIO_REGISTER_NUM];
@@ -160,7 +161,7 @@ struct si470x_device {
160 unsigned int wr_index; 161 unsigned int wr_index;
161 162
162 struct completion completion; 163 struct completion completion;
163 bool stci_enabled; /* Seek/Tune Complete Interrupt */ 164 bool status_rssi_auto_update; /* Does RSSI get updated automatic? */
164 165
165#if defined(CONFIG_USB_SI470X) || defined(CONFIG_USB_SI470X_MODULE) 166#if defined(CONFIG_USB_SI470X) || defined(CONFIG_USB_SI470X_MODULE)
166 /* reference to USB and video device */ 167 /* reference to USB and video device */
@@ -189,7 +190,7 @@ struct si470x_device {
189 * Firmware Versions 190 * Firmware Versions
190 **************************************************************************/ 191 **************************************************************************/
191 192
192#define RADIO_FW_VERSION 15 193#define RADIO_FW_VERSION 12
193 194
194 195
195 196
diff --git a/drivers/media/rc/Kconfig b/drivers/media/rc/Kconfig
index 908ef70430e9..5180390be7ab 100644
--- a/drivers/media/rc/Kconfig
+++ b/drivers/media/rc/Kconfig
@@ -259,6 +259,17 @@ config IR_WINBOND_CIR
259 To compile this driver as a module, choose M here: the module will 259 To compile this driver as a module, choose M here: the module will
260 be called winbond_cir. 260 be called winbond_cir.
261 261
262config IR_IGUANA
263 tristate "IguanaWorks USB IR Transceiver"
264 depends on RC_CORE
265 select USB
266 ---help---
267 Say Y here if you want to use the IgaunaWorks USB IR Transceiver.
268 Both infrared receive and send are supported.
269
270 To compile this driver as a module, choose M here: the module will
271 be called iguanair.
272
262config RC_LOOPBACK 273config RC_LOOPBACK
263 tristate "Remote Control Loopback Driver" 274 tristate "Remote Control Loopback Driver"
264 depends on RC_CORE 275 depends on RC_CORE
diff --git a/drivers/media/rc/Makefile b/drivers/media/rc/Makefile
index 29f364f88a94..f871d1986c21 100644
--- a/drivers/media/rc/Makefile
+++ b/drivers/media/rc/Makefile
@@ -27,3 +27,4 @@ obj-$(CONFIG_IR_STREAMZAP) += streamzap.o
27obj-$(CONFIG_IR_WINBOND_CIR) += winbond-cir.o 27obj-$(CONFIG_IR_WINBOND_CIR) += winbond-cir.o
28obj-$(CONFIG_RC_LOOPBACK) += rc-loopback.o 28obj-$(CONFIG_RC_LOOPBACK) += rc-loopback.o
29obj-$(CONFIG_IR_GPIO_CIR) += gpio-ir-recv.o 29obj-$(CONFIG_IR_GPIO_CIR) += gpio-ir-recv.o
30obj-$(CONFIG_IR_IGUANA) += iguanair.o
diff --git a/drivers/media/rc/ati_remote.c b/drivers/media/rc/ati_remote.c
index 7be377fc1be8..8fa72e2dacb1 100644
--- a/drivers/media/rc/ati_remote.c
+++ b/drivers/media/rc/ati_remote.c
@@ -147,7 +147,8 @@ static bool mouse = true;
147module_param(mouse, bool, 0444); 147module_param(mouse, bool, 0444);
148MODULE_PARM_DESC(mouse, "Enable mouse device, default = yes"); 148MODULE_PARM_DESC(mouse, "Enable mouse device, default = yes");
149 149
150#define dbginfo(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0) 150#define dbginfo(dev, format, arg...) \
151 do { if (debug) dev_info(dev , format , ## arg); } while (0)
151#undef err 152#undef err
152#define err(format, arg...) printk(KERN_ERR format , ## arg) 153#define err(format, arg...) printk(KERN_ERR format , ## arg)
153 154
@@ -191,17 +192,41 @@ static const char *get_medion_keymap(struct usb_interface *interface)
191 return RC_MAP_MEDION_X10; 192 return RC_MAP_MEDION_X10;
192} 193}
193 194
194static const struct ati_receiver_type type_ati = { .default_keymap = RC_MAP_ATI_X10 }; 195static const struct ati_receiver_type type_ati = {
195static const struct ati_receiver_type type_medion = { .get_default_keymap = get_medion_keymap }; 196 .default_keymap = RC_MAP_ATI_X10
196static const struct ati_receiver_type type_firefly = { .default_keymap = RC_MAP_SNAPSTREAM_FIREFLY }; 197};
198static const struct ati_receiver_type type_medion = {
199 .get_default_keymap = get_medion_keymap
200};
201static const struct ati_receiver_type type_firefly = {
202 .default_keymap = RC_MAP_SNAPSTREAM_FIREFLY
203};
197 204
198static struct usb_device_id ati_remote_table[] = { 205static struct usb_device_id ati_remote_table[] = {
199 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati }, 206 {
200 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA2_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati }, 207 USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID),
201 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati }, 208 .driver_info = (unsigned long)&type_ati
202 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, NVIDIA_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati }, 209 },
203 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_medion }, 210 {
204 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, FIREFLY_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_firefly }, 211 USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA2_REMOTE_PRODUCT_ID),
212 .driver_info = (unsigned long)&type_ati
213 },
214 {
215 USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID),
216 .driver_info = (unsigned long)&type_ati
217 },
218 {
219 USB_DEVICE(ATI_REMOTE_VENDOR_ID, NVIDIA_REMOTE_PRODUCT_ID),
220 .driver_info = (unsigned long)&type_ati
221 },
222 {
223 USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID),
224 .driver_info = (unsigned long)&type_medion
225 },
226 {
227 USB_DEVICE(ATI_REMOTE_VENDOR_ID, FIREFLY_REMOTE_PRODUCT_ID),
228 .driver_info = (unsigned long)&type_firefly
229 },
205 {} /* Terminating entry */ 230 {} /* Terminating entry */
206}; 231};
207 232
@@ -296,25 +321,8 @@ static const struct {
296 {KIND_END, 0x00, EV_MAX + 1, 0, 0} 321 {KIND_END, 0x00, EV_MAX + 1, 0, 0}
297}; 322};
298 323
299/* Local function prototypes */
300static int ati_remote_sendpacket (struct ati_remote *ati_remote, u16 cmd, unsigned char *data);
301static void ati_remote_irq_out (struct urb *urb);
302static void ati_remote_irq_in (struct urb *urb);
303static void ati_remote_input_report (struct urb *urb);
304static int ati_remote_initialize (struct ati_remote *ati_remote);
305static int ati_remote_probe (struct usb_interface *interface, const struct usb_device_id *id);
306static void ati_remote_disconnect (struct usb_interface *interface);
307
308/* usb specific object to register with the usb subsystem */
309static struct usb_driver ati_remote_driver = {
310 .name = "ati_remote",
311 .probe = ati_remote_probe,
312 .disconnect = ati_remote_disconnect,
313 .id_table = ati_remote_table,
314};
315
316/* 324/*
317 * ati_remote_dump_input 325 * ati_remote_dump_input
318 */ 326 */
319static void ati_remote_dump(struct device *dev, unsigned char *data, 327static void ati_remote_dump(struct device *dev, unsigned char *data,
320 unsigned int len) 328 unsigned int len)
@@ -326,12 +334,14 @@ static void ati_remote_dump(struct device *dev, unsigned char *data,
326 dev_warn(dev, "Weird key %02x %02x %02x %02x\n", 334 dev_warn(dev, "Weird key %02x %02x %02x %02x\n",
327 data[0], data[1], data[2], data[3]); 335 data[0], data[1], data[2], data[3]);
328 else 336 else
329 dev_warn(dev, "Weird data, len=%d %02x %02x %02x %02x %02x %02x ...\n", 337 dev_warn(dev,
330 len, data[0], data[1], data[2], data[3], data[4], data[5]); 338 "Weird data, len=%d %02x %02x %02x %02x %02x %02x ...\n",
339 len, data[0], data[1], data[2], data[3], data[4],
340 data[5]);
331} 341}
332 342
333/* 343/*
334 * ati_remote_open 344 * ati_remote_open
335 */ 345 */
336static int ati_remote_open(struct ati_remote *ati_remote) 346static int ati_remote_open(struct ati_remote *ati_remote)
337{ 347{
@@ -355,7 +365,7 @@ out: mutex_unlock(&ati_remote->open_mutex);
355} 365}
356 366
357/* 367/*
358 * ati_remote_close 368 * ati_remote_close
359 */ 369 */
360static void ati_remote_close(struct ati_remote *ati_remote) 370static void ati_remote_close(struct ati_remote *ati_remote)
361{ 371{
@@ -390,7 +400,7 @@ static void ati_remote_rc_close(struct rc_dev *rdev)
390} 400}
391 401
392/* 402/*
393 * ati_remote_irq_out 403 * ati_remote_irq_out
394 */ 404 */
395static void ati_remote_irq_out(struct urb *urb) 405static void ati_remote_irq_out(struct urb *urb)
396{ 406{
@@ -408,11 +418,12 @@ static void ati_remote_irq_out(struct urb *urb)
408} 418}
409 419
410/* 420/*
411 * ati_remote_sendpacket 421 * ati_remote_sendpacket
412 * 422 *
413 * Used to send device initialization strings 423 * Used to send device initialization strings
414 */ 424 */
415static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd, unsigned char *data) 425static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd,
426 unsigned char *data)
416{ 427{
417 int retval = 0; 428 int retval = 0;
418 429
@@ -441,7 +452,7 @@ static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd, unsigne
441} 452}
442 453
443/* 454/*
444 * ati_remote_compute_accel 455 * ati_remote_compute_accel
445 * 456 *
446 * Implements acceleration curve for directional control pad 457 * Implements acceleration curve for directional control pad
447 * If elapsed time since last event is > 1/4 second, user "stopped", 458 * If elapsed time since last event is > 1/4 second, user "stopped",
@@ -478,7 +489,7 @@ static int ati_remote_compute_accel(struct ati_remote *ati_remote)
478} 489}
479 490
480/* 491/*
481 * ati_remote_report_input 492 * ati_remote_report_input
482 */ 493 */
483static void ati_remote_input_report(struct urb *urb) 494static void ati_remote_input_report(struct urb *urb)
484{ 495{
@@ -518,7 +529,8 @@ static void ati_remote_input_report(struct urb *urb)
518 remote_num = (data[3] >> 4) & 0x0f; 529 remote_num = (data[3] >> 4) & 0x0f;
519 if (channel_mask & (1 << (remote_num + 1))) { 530 if (channel_mask & (1 << (remote_num + 1))) {
520 dbginfo(&ati_remote->interface->dev, 531 dbginfo(&ati_remote->interface->dev,
521 "Masked input from channel 0x%02x: data %02x,%02x, mask= 0x%02lx\n", 532 "Masked input from channel 0x%02x: data %02x,%02x, "
533 "mask= 0x%02lx\n",
522 remote_num, data[1], data[2], channel_mask); 534 remote_num, data[1], data[2], channel_mask);
523 return; 535 return;
524 } 536 }
@@ -546,7 +558,9 @@ static void ati_remote_input_report(struct urb *urb)
546 if (wheel_keycode == KEY_RESERVED) { 558 if (wheel_keycode == KEY_RESERVED) {
547 /* scrollwheel was not mapped, assume mouse */ 559 /* scrollwheel was not mapped, assume mouse */
548 560
549 /* Look up event code index in the mouse translation table. */ 561 /* Look up event code index in the mouse translation
562 * table.
563 */
550 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) { 564 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
551 if (scancode == ati_remote_tbl[i].data) { 565 if (scancode == ati_remote_tbl[i].data) {
552 index = i; 566 index = i;
@@ -630,9 +644,9 @@ static void ati_remote_input_report(struct urb *urb)
630 } else { 644 } else {
631 645
632 /* 646 /*
633 * Other event kinds are from the directional control pad, and have an 647 * Other event kinds are from the directional control pad, and
634 * acceleration factor applied to them. Without this acceleration, the 648 * have an acceleration factor applied to them. Without this
635 * control pad is mostly unusable. 649 * acceleration, the control pad is mostly unusable.
636 */ 650 */
637 acc = ati_remote_compute_accel(ati_remote); 651 acc = ati_remote_compute_accel(ati_remote);
638 652
@@ -659,7 +673,8 @@ static void ati_remote_input_report(struct urb *urb)
659 input_report_rel(dev, REL_Y, acc); 673 input_report_rel(dev, REL_Y, acc);
660 break; 674 break;
661 default: 675 default:
662 dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n", 676 dev_dbg(&ati_remote->interface->dev,
677 "ati_remote kind=%d\n",
663 ati_remote_tbl[index].kind); 678 ati_remote_tbl[index].kind);
664 } 679 }
665 input_sync(dev); 680 input_sync(dev);
@@ -670,7 +685,7 @@ static void ati_remote_input_report(struct urb *urb)
670} 685}
671 686
672/* 687/*
673 * ati_remote_irq_in 688 * ati_remote_irq_in
674 */ 689 */
675static void ati_remote_irq_in(struct urb *urb) 690static void ati_remote_irq_in(struct urb *urb)
676{ 691{
@@ -684,22 +699,25 @@ static void ati_remote_irq_in(struct urb *urb)
684 case -ECONNRESET: /* unlink */ 699 case -ECONNRESET: /* unlink */
685 case -ENOENT: 700 case -ENOENT:
686 case -ESHUTDOWN: 701 case -ESHUTDOWN:
687 dev_dbg(&ati_remote->interface->dev, "%s: urb error status, unlink? \n", 702 dev_dbg(&ati_remote->interface->dev,
703 "%s: urb error status, unlink?\n",
688 __func__); 704 __func__);
689 return; 705 return;
690 default: /* error */ 706 default: /* error */
691 dev_dbg(&ati_remote->interface->dev, "%s: Nonzero urb status %d\n", 707 dev_dbg(&ati_remote->interface->dev,
708 "%s: Nonzero urb status %d\n",
692 __func__, urb->status); 709 __func__, urb->status);
693 } 710 }
694 711
695 retval = usb_submit_urb(urb, GFP_ATOMIC); 712 retval = usb_submit_urb(urb, GFP_ATOMIC);
696 if (retval) 713 if (retval)
697 dev_err(&ati_remote->interface->dev, "%s: usb_submit_urb()=%d\n", 714 dev_err(&ati_remote->interface->dev,
715 "%s: usb_submit_urb()=%d\n",
698 __func__, retval); 716 __func__, retval);
699} 717}
700 718
701/* 719/*
702 * ati_remote_alloc_buffers 720 * ati_remote_alloc_buffers
703 */ 721 */
704static int ati_remote_alloc_buffers(struct usb_device *udev, 722static int ati_remote_alloc_buffers(struct usb_device *udev,
705 struct ati_remote *ati_remote) 723 struct ati_remote *ati_remote)
@@ -726,7 +744,7 @@ static int ati_remote_alloc_buffers(struct usb_device *udev,
726} 744}
727 745
728/* 746/*
729 * ati_remote_free_buffers 747 * ati_remote_free_buffers
730 */ 748 */
731static void ati_remote_free_buffers(struct ati_remote *ati_remote) 749static void ati_remote_free_buffers(struct ati_remote *ati_remote)
732{ 750{
@@ -825,9 +843,10 @@ static int ati_remote_initialize(struct ati_remote *ati_remote)
825} 843}
826 844
827/* 845/*
828 * ati_remote_probe 846 * ati_remote_probe
829 */ 847 */
830static int ati_remote_probe(struct usb_interface *interface, const struct usb_device_id *id) 848static int ati_remote_probe(struct usb_interface *interface,
849 const struct usb_device_id *id)
831{ 850{
832 struct usb_device *udev = interface_to_usbdev(interface); 851 struct usb_device *udev = interface_to_usbdev(interface);
833 struct usb_host_interface *iface_host = interface->cur_altsetting; 852 struct usb_host_interface *iface_host = interface->cur_altsetting;
@@ -949,7 +968,7 @@ static int ati_remote_probe(struct usb_interface *interface, const struct usb_de
949} 968}
950 969
951/* 970/*
952 * ati_remote_disconnect 971 * ati_remote_disconnect
953 */ 972 */
954static void ati_remote_disconnect(struct usb_interface *interface) 973static void ati_remote_disconnect(struct usb_interface *interface)
955{ 974{
@@ -971,6 +990,14 @@ static void ati_remote_disconnect(struct usb_interface *interface)
971 kfree(ati_remote); 990 kfree(ati_remote);
972} 991}
973 992
993/* usb specific object to register with the usb subsystem */
994static struct usb_driver ati_remote_driver = {
995 .name = "ati_remote",
996 .probe = ati_remote_probe,
997 .disconnect = ati_remote_disconnect,
998 .id_table = ati_remote_table,
999};
1000
974module_usb_driver(ati_remote_driver); 1001module_usb_driver(ati_remote_driver);
975 1002
976MODULE_AUTHOR(DRIVER_AUTHOR); 1003MODULE_AUTHOR(DRIVER_AUTHOR);
diff --git a/drivers/media/rc/iguanair.c b/drivers/media/rc/iguanair.c
new file mode 100644
index 000000000000..5e2eaf8ba73e
--- /dev/null
+++ b/drivers/media/rc/iguanair.c
@@ -0,0 +1,639 @@
1/*
2 * IguanaWorks USB IR Transceiver support
3 *
4 * Copyright (C) 2012 Sean Young <sean@mess.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/usb.h>
25#include <linux/usb/input.h>
26#include <linux/slab.h>
27#include <linux/completion.h>
28#include <media/rc-core.h>
29
30#define DRIVER_NAME "iguanair"
31
32struct iguanair {
33 struct rc_dev *rc;
34
35 struct device *dev;
36 struct usb_device *udev;
37
38 int pipe_in, pipe_out;
39 uint8_t bufsize;
40 uint8_t version[2];
41
42 struct mutex lock;
43
44 /* receiver support */
45 bool receiver_on;
46 dma_addr_t dma_in;
47 uint8_t *buf_in;
48 struct urb *urb_in;
49 struct completion completion;
50
51 /* transmit support */
52 bool tx_overflow;
53 uint32_t carrier;
54 uint8_t cycle_overhead;
55 uint8_t channels;
56 uint8_t busy4;
57 uint8_t busy7;
58
59 char name[64];
60 char phys[64];
61};
62
63#define CMD_GET_VERSION 0x01
64#define CMD_GET_BUFSIZE 0x11
65#define CMD_GET_FEATURES 0x10
66#define CMD_SEND 0x15
67#define CMD_EXECUTE 0x1f
68#define CMD_RX_OVERFLOW 0x31
69#define CMD_TX_OVERFLOW 0x32
70#define CMD_RECEIVER_ON 0x12
71#define CMD_RECEIVER_OFF 0x14
72
73#define DIR_IN 0xdc
74#define DIR_OUT 0xcd
75
76#define MAX_PACKET_SIZE 8u
77#define TIMEOUT 1000
78
79struct packet {
80 uint16_t start;
81 uint8_t direction;
82 uint8_t cmd;
83};
84
85struct response_packet {
86 struct packet header;
87 uint8_t data[4];
88};
89
90struct send_packet {
91 struct packet header;
92 uint8_t length;
93 uint8_t channels;
94 uint8_t busy7;
95 uint8_t busy4;
96 uint8_t payload[0];
97};
98
99static void process_ir_data(struct iguanair *ir, unsigned len)
100{
101 if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
102 switch (ir->buf_in[3]) {
103 case CMD_TX_OVERFLOW:
104 ir->tx_overflow = true;
105 case CMD_RECEIVER_OFF:
106 case CMD_RECEIVER_ON:
107 case CMD_SEND:
108 complete(&ir->completion);
109 break;
110 case CMD_RX_OVERFLOW:
111 dev_warn(ir->dev, "receive overflow\n");
112 break;
113 default:
114 dev_warn(ir->dev, "control code %02x received\n",
115 ir->buf_in[3]);
116 break;
117 }
118 } else if (len >= 7) {
119 DEFINE_IR_RAW_EVENT(rawir);
120 unsigned i;
121
122 init_ir_raw_event(&rawir);
123
124 for (i = 0; i < 7; i++) {
125 if (ir->buf_in[i] == 0x80) {
126 rawir.pulse = false;
127 rawir.duration = US_TO_NS(21845);
128 } else {
129 rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
130 rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
131 21330;
132 }
133
134 ir_raw_event_store_with_filter(ir->rc, &rawir);
135 }
136
137 ir_raw_event_handle(ir->rc);
138 }
139}
140
141static void iguanair_rx(struct urb *urb)
142{
143 struct iguanair *ir;
144
145 if (!urb)
146 return;
147
148 ir = urb->context;
149 if (!ir) {
150 usb_unlink_urb(urb);
151 return;
152 }
153
154 switch (urb->status) {
155 case 0:
156 process_ir_data(ir, urb->actual_length);
157 break;
158 case -ECONNRESET:
159 case -ENOENT:
160 case -ESHUTDOWN:
161 usb_unlink_urb(urb);
162 return;
163 case -EPIPE:
164 default:
165 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
166 break;
167 }
168
169 usb_submit_urb(urb, GFP_ATOMIC);
170}
171
172static int iguanair_send(struct iguanair *ir, void *data, unsigned size,
173 struct response_packet *response, unsigned *res_len)
174{
175 unsigned offset, len;
176 int rc, transferred;
177
178 for (offset = 0; offset < size; offset += MAX_PACKET_SIZE) {
179 len = min(size - offset, MAX_PACKET_SIZE);
180
181 if (ir->tx_overflow)
182 return -EOVERFLOW;
183
184 rc = usb_interrupt_msg(ir->udev, ir->pipe_out, data + offset,
185 len, &transferred, TIMEOUT);
186 if (rc)
187 return rc;
188
189 if (transferred != len)
190 return -EIO;
191 }
192
193 if (response) {
194 rc = usb_interrupt_msg(ir->udev, ir->pipe_in, response,
195 sizeof(*response), res_len, TIMEOUT);
196 }
197
198 return rc;
199}
200
201static int iguanair_get_features(struct iguanair *ir)
202{
203 struct packet packet;
204 struct response_packet response;
205 int rc, len;
206
207 packet.start = 0;
208 packet.direction = DIR_OUT;
209 packet.cmd = CMD_GET_VERSION;
210
211 rc = iguanair_send(ir, &packet, sizeof(packet), &response, &len);
212 if (rc) {
213 dev_info(ir->dev, "failed to get version\n");
214 goto out;
215 }
216
217 if (len != 6) {
218 dev_info(ir->dev, "failed to get version\n");
219 rc = -EIO;
220 goto out;
221 }
222
223 ir->version[0] = response.data[0];
224 ir->version[1] = response.data[1];
225 ir->bufsize = 150;
226 ir->cycle_overhead = 65;
227
228 packet.cmd = CMD_GET_BUFSIZE;
229
230 rc = iguanair_send(ir, &packet, sizeof(packet), &response, &len);
231 if (rc) {
232 dev_info(ir->dev, "failed to get buffer size\n");
233 goto out;
234 }
235
236 if (len != 5) {
237 dev_info(ir->dev, "failed to get buffer size\n");
238 rc = -EIO;
239 goto out;
240 }
241
242 ir->bufsize = response.data[0];
243
244 if (ir->version[0] == 0 || ir->version[1] == 0)
245 goto out;
246
247 packet.cmd = CMD_GET_FEATURES;
248
249 rc = iguanair_send(ir, &packet, sizeof(packet), &response, &len);
250 if (rc) {
251 dev_info(ir->dev, "failed to get features\n");
252 goto out;
253 }
254
255 if (len < 5) {
256 dev_info(ir->dev, "failed to get features\n");
257 rc = -EIO;
258 goto out;
259 }
260
261 if (len > 5 && ir->version[0] >= 4)
262 ir->cycle_overhead = response.data[1];
263
264out:
265 return rc;
266}
267
268static int iguanair_receiver(struct iguanair *ir, bool enable)
269{
270 struct packet packet = { 0, DIR_OUT, enable ?
271 CMD_RECEIVER_ON : CMD_RECEIVER_OFF };
272 int rc;
273
274 INIT_COMPLETION(ir->completion);
275
276 rc = iguanair_send(ir, &packet, sizeof(packet), NULL, NULL);
277 if (rc)
278 return rc;
279
280 wait_for_completion_timeout(&ir->completion, TIMEOUT);
281
282 return 0;
283}
284
285/*
286 * The iguana ir creates the carrier by busy spinning after each pulse or
287 * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is
288 * broken down into 7-cycles and 4-cyles delays, with a preference for
289 * 4-cycle delays.
290 */
291static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
292{
293 struct iguanair *ir = dev->priv;
294
295 if (carrier < 25000 || carrier > 150000)
296 return -EINVAL;
297
298 mutex_lock(&ir->lock);
299
300 if (carrier != ir->carrier) {
301 uint32_t cycles, fours, sevens;
302
303 ir->carrier = carrier;
304
305 cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
306 ir->cycle_overhead;
307
308 /* make up the the remainer of 4-cycle blocks */
309 switch (cycles & 3) {
310 case 0:
311 sevens = 0;
312 break;
313 case 1:
314 sevens = 3;
315 break;
316 case 2:
317 sevens = 2;
318 break;
319 case 3:
320 sevens = 1;
321 break;
322 }
323
324 fours = (cycles - sevens * 7) / 4;
325
326 /* magic happens here */
327 ir->busy7 = (4 - sevens) * 2;
328 ir->busy4 = 110 - fours;
329 }
330
331 mutex_unlock(&ir->lock);
332
333 return carrier;
334}
335
336static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
337{
338 struct iguanair *ir = dev->priv;
339
340 if (mask > 15)
341 return 4;
342
343 mutex_lock(&ir->lock);
344 ir->channels = mask;
345 mutex_unlock(&ir->lock);
346
347 return 0;
348}
349
350static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
351{
352 struct iguanair *ir = dev->priv;
353 uint8_t space, *payload;
354 unsigned i, size, rc;
355 struct send_packet *packet;
356
357 mutex_lock(&ir->lock);
358
359 /* convert from us to carrier periods */
360 for (i = size = 0; i < count; i++) {
361 txbuf[i] = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
362 size += (txbuf[i] + 126) / 127;
363 }
364
365 packet = kmalloc(sizeof(*packet) + size, GFP_KERNEL);
366 if (!packet) {
367 rc = -ENOMEM;
368 goto out;
369 }
370
371 if (size > ir->bufsize) {
372 rc = -E2BIG;
373 goto out;
374 }
375
376 packet->header.start = 0;
377 packet->header.direction = DIR_OUT;
378 packet->header.cmd = CMD_SEND;
379 packet->length = size;
380 packet->channels = ir->channels << 4;
381 packet->busy7 = ir->busy7;
382 packet->busy4 = ir->busy4;
383
384 space = 0;
385 payload = packet->payload;
386
387 for (i = 0; i < count; i++) {
388 unsigned periods = txbuf[i];
389
390 while (periods > 127) {
391 *payload++ = 127 | space;
392 periods -= 127;
393 }
394
395 *payload++ = periods | space;
396 space ^= 0x80;
397 }
398
399 if (ir->receiver_on) {
400 rc = iguanair_receiver(ir, false);
401 if (rc) {
402 dev_warn(ir->dev, "disable receiver before transmit failed\n");
403 goto out;
404 }
405 }
406
407 ir->tx_overflow = false;
408
409 INIT_COMPLETION(ir->completion);
410
411 rc = iguanair_send(ir, packet, size + 8, NULL, NULL);
412
413 if (rc == 0) {
414 wait_for_completion_timeout(&ir->completion, TIMEOUT);
415 if (ir->tx_overflow)
416 rc = -EOVERFLOW;
417 }
418
419 ir->tx_overflow = false;
420
421 if (ir->receiver_on) {
422 if (iguanair_receiver(ir, true))
423 dev_warn(ir->dev, "re-enable receiver after transmit failed\n");
424 }
425
426out:
427 mutex_unlock(&ir->lock);
428 kfree(packet);
429
430 return rc;
431}
432
433static int iguanair_open(struct rc_dev *rdev)
434{
435 struct iguanair *ir = rdev->priv;
436 int rc;
437
438 mutex_lock(&ir->lock);
439
440 usb_submit_urb(ir->urb_in, GFP_KERNEL);
441
442 BUG_ON(ir->receiver_on);
443
444 rc = iguanair_receiver(ir, true);
445 if (rc == 0)
446 ir->receiver_on = true;
447
448 mutex_unlock(&ir->lock);
449
450 return rc;
451}
452
453static void iguanair_close(struct rc_dev *rdev)
454{
455 struct iguanair *ir = rdev->priv;
456 int rc;
457
458 mutex_lock(&ir->lock);
459
460 rc = iguanair_receiver(ir, false);
461 ir->receiver_on = false;
462 if (rc)
463 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
464
465 usb_kill_urb(ir->urb_in);
466
467 mutex_unlock(&ir->lock);
468}
469
470static int __devinit iguanair_probe(struct usb_interface *intf,
471 const struct usb_device_id *id)
472{
473 struct usb_device *udev = interface_to_usbdev(intf);
474 struct iguanair *ir;
475 struct rc_dev *rc;
476 int ret;
477 struct usb_host_interface *idesc;
478
479 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
480 rc = rc_allocate_device();
481 if (!ir || !rc) {
482 ret = ENOMEM;
483 goto out;
484 }
485
486 ir->buf_in = usb_alloc_coherent(udev, MAX_PACKET_SIZE, GFP_ATOMIC,
487 &ir->dma_in);
488 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
489
490 if (!ir->buf_in || !ir->urb_in) {
491 ret = ENOMEM;
492 goto out;
493 }
494
495 idesc = intf->altsetting;
496
497 if (idesc->desc.bNumEndpoints < 2) {
498 ret = -ENODEV;
499 goto out;
500 }
501
502 ir->rc = rc;
503 ir->dev = &intf->dev;
504 ir->udev = udev;
505 ir->pipe_in = usb_rcvintpipe(udev,
506 idesc->endpoint[0].desc.bEndpointAddress);
507 ir->pipe_out = usb_sndintpipe(udev,
508 idesc->endpoint[1].desc.bEndpointAddress);
509 mutex_init(&ir->lock);
510 init_completion(&ir->completion);
511
512 ret = iguanair_get_features(ir);
513 if (ret) {
514 dev_warn(&intf->dev, "failed to get device features");
515 goto out;
516 }
517
518 usb_fill_int_urb(ir->urb_in, ir->udev, ir->pipe_in, ir->buf_in,
519 MAX_PACKET_SIZE, iguanair_rx, ir,
520 idesc->endpoint[0].desc.bInterval);
521 ir->urb_in->transfer_dma = ir->dma_in;
522 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
523
524 snprintf(ir->name, sizeof(ir->name),
525 "IguanaWorks USB IR Transceiver version %d.%d",
526 ir->version[0], ir->version[1]);
527
528 usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
529
530 rc->input_name = ir->name;
531 rc->input_phys = ir->phys;
532 usb_to_input_id(ir->udev, &rc->input_id);
533 rc->dev.parent = &intf->dev;
534 rc->driver_type = RC_DRIVER_IR_RAW;
535 rc->allowed_protos = RC_TYPE_ALL;
536 rc->priv = ir;
537 rc->open = iguanair_open;
538 rc->close = iguanair_close;
539 rc->s_tx_mask = iguanair_set_tx_mask;
540 rc->s_tx_carrier = iguanair_set_tx_carrier;
541 rc->tx_ir = iguanair_tx;
542 rc->driver_name = DRIVER_NAME;
543 rc->map_name = RC_MAP_EMPTY;
544
545 iguanair_set_tx_carrier(rc, 38000);
546
547 ret = rc_register_device(rc);
548 if (ret < 0) {
549 dev_err(&intf->dev, "failed to register rc device %d", ret);
550 goto out;
551 }
552
553 usb_set_intfdata(intf, ir);
554
555 dev_info(&intf->dev, "Registered %s", ir->name);
556
557 return 0;
558out:
559 if (ir) {
560 usb_free_urb(ir->urb_in);
561 usb_free_coherent(udev, MAX_PACKET_SIZE, ir->buf_in,
562 ir->dma_in);
563 }
564 rc_free_device(rc);
565 kfree(ir);
566 return ret;
567}
568
569static void __devexit iguanair_disconnect(struct usb_interface *intf)
570{
571 struct iguanair *ir = usb_get_intfdata(intf);
572
573 usb_set_intfdata(intf, NULL);
574
575 usb_kill_urb(ir->urb_in);
576 usb_free_urb(ir->urb_in);
577 usb_free_coherent(ir->udev, MAX_PACKET_SIZE, ir->buf_in, ir->dma_in);
578 rc_unregister_device(ir->rc);
579 kfree(ir);
580}
581
582static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
583{
584 struct iguanair *ir = usb_get_intfdata(intf);
585 int rc = 0;
586
587 mutex_lock(&ir->lock);
588
589 if (ir->receiver_on) {
590 rc = iguanair_receiver(ir, false);
591 if (rc)
592 dev_warn(ir->dev, "failed to disable receiver for suspend\n");
593 }
594
595 mutex_unlock(&ir->lock);
596
597 return rc;
598}
599
600static int iguanair_resume(struct usb_interface *intf)
601{
602 struct iguanair *ir = usb_get_intfdata(intf);
603 int rc = 0;
604
605 mutex_lock(&ir->lock);
606
607 if (ir->receiver_on) {
608 rc = iguanair_receiver(ir, true);
609 if (rc)
610 dev_warn(ir->dev, "failed to enable receiver after resume\n");
611 }
612
613 mutex_unlock(&ir->lock);
614
615 return rc;
616}
617
618static const struct usb_device_id iguanair_table[] = {
619 { USB_DEVICE(0x1781, 0x0938) },
620 { }
621};
622
623static struct usb_driver iguanair_driver = {
624 .name = DRIVER_NAME,
625 .probe = iguanair_probe,
626 .disconnect = __devexit_p(iguanair_disconnect),
627 .suspend = iguanair_suspend,
628 .resume = iguanair_resume,
629 .reset_resume = iguanair_resume,
630 .id_table = iguanair_table
631};
632
633module_usb_driver(iguanair_driver);
634
635MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
636MODULE_AUTHOR("Sean Young <sean@mess.org>");
637MODULE_LICENSE("GPL");
638MODULE_DEVICE_TABLE(usb, iguanair_table);
639
diff --git a/drivers/media/rc/mceusb.c b/drivers/media/rc/mceusb.c
index 84e06d3aa696..f38d9a8c6880 100644
--- a/drivers/media/rc/mceusb.c
+++ b/drivers/media/rc/mceusb.c
@@ -199,6 +199,7 @@ static bool debug;
199#define VENDOR_REALTEK 0x0bda 199#define VENDOR_REALTEK 0x0bda
200#define VENDOR_TIVO 0x105a 200#define VENDOR_TIVO 0x105a
201#define VENDOR_CONEXANT 0x0572 201#define VENDOR_CONEXANT 0x0572
202#define VENDOR_TWISTEDMELON 0x2596
202 203
203enum mceusb_model_type { 204enum mceusb_model_type {
204 MCE_GEN2 = 0, /* Most boards */ 205 MCE_GEN2 = 0, /* Most boards */
@@ -391,6 +392,12 @@ static struct usb_device_id mceusb_dev_table[] = {
391 /* Conexant Hybrid TV RDU253S Polaris */ 392 /* Conexant Hybrid TV RDU253S Polaris */
392 { USB_DEVICE(VENDOR_CONEXANT, 0x58a5), 393 { USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
393 .driver_info = CX_HYBRID_TV }, 394 .driver_info = CX_HYBRID_TV },
395 /* Twisted Melon Inc. - Manta Mini Receiver */
396 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8008) },
397 /* Twisted Melon Inc. - Manta Pico Receiver */
398 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8016) },
399 /* Twisted Melon Inc. - Manta Transceiver */
400 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8042) },
394 /* Terminating entry */ 401 /* Terminating entry */
395 { } 402 { }
396}; 403};
@@ -410,14 +417,12 @@ struct mceusb_dev {
410 /* usb */ 417 /* usb */
411 struct usb_device *usbdev; 418 struct usb_device *usbdev;
412 struct urb *urb_in; 419 struct urb *urb_in;
413 struct usb_endpoint_descriptor *usb_ep_in;
414 struct usb_endpoint_descriptor *usb_ep_out; 420 struct usb_endpoint_descriptor *usb_ep_out;
415 421
416 /* buffers and dma */ 422 /* buffers and dma */
417 unsigned char *buf_in; 423 unsigned char *buf_in;
418 unsigned int len_in; 424 unsigned int len_in;
419 dma_addr_t dma_in; 425 dma_addr_t dma_in;
420 dma_addr_t dma_out;
421 426
422 enum { 427 enum {
423 CMD_HEADER = 0, 428 CMD_HEADER = 0,
@@ -686,7 +691,7 @@ static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
686 dev_info(dev, "Raw IR data, %d pulse/space samples\n", ir->rem); 691 dev_info(dev, "Raw IR data, %d pulse/space samples\n", ir->rem);
687} 692}
688 693
689static void mce_async_callback(struct urb *urb, struct pt_regs *regs) 694static void mce_async_callback(struct urb *urb)
690{ 695{
691 struct mceusb_dev *ir; 696 struct mceusb_dev *ir;
692 int len; 697 int len;
@@ -733,7 +738,7 @@ static void mce_request_packet(struct mceusb_dev *ir, unsigned char *data,
733 pipe = usb_sndintpipe(ir->usbdev, 738 pipe = usb_sndintpipe(ir->usbdev,
734 ir->usb_ep_out->bEndpointAddress); 739 ir->usb_ep_out->bEndpointAddress);
735 usb_fill_int_urb(async_urb, ir->usbdev, pipe, 740 usb_fill_int_urb(async_urb, ir->usbdev, pipe,
736 async_buf, size, (usb_complete_t)mce_async_callback, 741 async_buf, size, mce_async_callback,
737 ir, ir->usb_ep_out->bInterval); 742 ir, ir->usb_ep_out->bInterval);
738 memcpy(async_buf, data, size); 743 memcpy(async_buf, data, size);
739 744
@@ -1031,7 +1036,7 @@ static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
1031 ir_raw_event_handle(ir->rc); 1036 ir_raw_event_handle(ir->rc);
1032} 1037}
1033 1038
1034static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs) 1039static void mceusb_dev_recv(struct urb *urb)
1035{ 1040{
1036 struct mceusb_dev *ir; 1041 struct mceusb_dev *ir;
1037 int buf_len; 1042 int buf_len;
@@ -1331,7 +1336,6 @@ static int __devinit mceusb_dev_probe(struct usb_interface *intf,
1331 ir->model = model; 1336 ir->model = model;
1332 1337
1333 /* Saving usb interface data for use by the transmitter routine */ 1338 /* Saving usb interface data for use by the transmitter routine */
1334 ir->usb_ep_in = ep_in;
1335 ir->usb_ep_out = ep_out; 1339 ir->usb_ep_out = ep_out;
1336 1340
1337 if (dev->descriptor.iManufacturer 1341 if (dev->descriptor.iManufacturer
@@ -1349,8 +1353,8 @@ static int __devinit mceusb_dev_probe(struct usb_interface *intf,
1349 goto rc_dev_fail; 1353 goto rc_dev_fail;
1350 1354
1351 /* wire up inbound data handler */ 1355 /* wire up inbound data handler */
1352 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, 1356 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
1353 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval); 1357 mceusb_dev_recv, ir, ep_in->bInterval);
1354 ir->urb_in->transfer_dma = ir->dma_in; 1358 ir->urb_in->transfer_dma = ir->dma_in;
1355 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1359 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1356 1360
diff --git a/drivers/media/rc/rc-main.c b/drivers/media/rc/rc-main.c
index 6e16b09c24a9..cabc19c10515 100644
--- a/drivers/media/rc/rc-main.c
+++ b/drivers/media/rc/rc-main.c
@@ -775,10 +775,11 @@ static ssize_t show_protocols(struct device *device,
775 if (dev->driver_type == RC_DRIVER_SCANCODE) { 775 if (dev->driver_type == RC_DRIVER_SCANCODE) {
776 enabled = dev->rc_map.rc_type; 776 enabled = dev->rc_map.rc_type;
777 allowed = dev->allowed_protos; 777 allowed = dev->allowed_protos;
778 } else { 778 } else if (dev->raw) {
779 enabled = dev->raw->enabled_protocols; 779 enabled = dev->raw->enabled_protocols;
780 allowed = ir_raw_get_allowed_protocols(); 780 allowed = ir_raw_get_allowed_protocols();
781 } 781 } else
782 return -ENODEV;
782 783
783 IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n", 784 IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
784 (long long)allowed, 785 (long long)allowed,
diff --git a/drivers/media/video/adv7180.c b/drivers/media/video/adv7180.c
index 174bffacf117..45ecf8db1eae 100644
--- a/drivers/media/video/adv7180.c
+++ b/drivers/media/video/adv7180.c
@@ -26,11 +26,10 @@
26#include <media/v4l2-ioctl.h> 26#include <media/v4l2-ioctl.h>
27#include <linux/videodev2.h> 27#include <linux/videodev2.h>
28#include <media/v4l2-device.h> 28#include <media/v4l2-device.h>
29#include <media/v4l2-ctrls.h>
29#include <media/v4l2-chip-ident.h> 30#include <media/v4l2-chip-ident.h>
30#include <linux/mutex.h> 31#include <linux/mutex.h>
31 32
32#define DRIVER_NAME "adv7180"
33
34#define ADV7180_INPUT_CONTROL_REG 0x00 33#define ADV7180_INPUT_CONTROL_REG 0x00
35#define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00 34#define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00
36#define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10 35#define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
@@ -55,21 +54,21 @@
55 54
56#define ADV7180_AUTODETECT_ENABLE_REG 0x07 55#define ADV7180_AUTODETECT_ENABLE_REG 0x07
57#define ADV7180_AUTODETECT_DEFAULT 0x7f 56#define ADV7180_AUTODETECT_DEFAULT 0x7f
58 57/* Contrast */
59#define ADV7180_CON_REG 0x08 /*Unsigned */ 58#define ADV7180_CON_REG 0x08 /*Unsigned */
60#define CON_REG_MIN 0 59#define ADV7180_CON_MIN 0
61#define CON_REG_DEF 128 60#define ADV7180_CON_DEF 128
62#define CON_REG_MAX 255 61#define ADV7180_CON_MAX 255
63 62/* Brightness*/
64#define ADV7180_BRI_REG 0x0a /*Signed */ 63#define ADV7180_BRI_REG 0x0a /*Signed */
65#define BRI_REG_MIN -128 64#define ADV7180_BRI_MIN -128
66#define BRI_REG_DEF 0 65#define ADV7180_BRI_DEF 0
67#define BRI_REG_MAX 127 66#define ADV7180_BRI_MAX 127
68 67/* Hue */
69#define ADV7180_HUE_REG 0x0b /*Signed, inverted */ 68#define ADV7180_HUE_REG 0x0b /*Signed, inverted */
70#define HUE_REG_MIN -127 69#define ADV7180_HUE_MIN -127
71#define HUE_REG_DEF 0 70#define ADV7180_HUE_DEF 0
72#define HUE_REG_MAX 128 71#define ADV7180_HUE_MAX 128
73 72
74#define ADV7180_ADI_CTRL_REG 0x0e 73#define ADV7180_ADI_CTRL_REG 0x0e
75#define ADV7180_ADI_CTRL_IRQ_SPACE 0x20 74#define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
@@ -98,12 +97,12 @@
98#define ADV7180_ICONF1_ACTIVE_LOW 0x01 97#define ADV7180_ICONF1_ACTIVE_LOW 0x01
99#define ADV7180_ICONF1_PSYNC_ONLY 0x10 98#define ADV7180_ICONF1_PSYNC_ONLY 0x10
100#define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0 99#define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
101 100/* Saturation */
102#define ADV7180_SD_SAT_CB_REG 0xe3 /*Unsigned */ 101#define ADV7180_SD_SAT_CB_REG 0xe3 /*Unsigned */
103#define ADV7180_SD_SAT_CR_REG 0xe4 /*Unsigned */ 102#define ADV7180_SD_SAT_CR_REG 0xe4 /*Unsigned */
104#define SAT_REG_MIN 0 103#define ADV7180_SAT_MIN 0
105#define SAT_REG_DEF 128 104#define ADV7180_SAT_DEF 128
106#define SAT_REG_MAX 255 105#define ADV7180_SAT_MAX 255
107 106
108#define ADV7180_IRQ1_LOCK 0x01 107#define ADV7180_IRQ1_LOCK 0x01
109#define ADV7180_IRQ1_UNLOCK 0x02 108#define ADV7180_IRQ1_UNLOCK 0x02
@@ -121,18 +120,18 @@
121#define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F 120#define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F
122 121
123struct adv7180_state { 122struct adv7180_state {
123 struct v4l2_ctrl_handler ctrl_hdl;
124 struct v4l2_subdev sd; 124 struct v4l2_subdev sd;
125 struct work_struct work; 125 struct work_struct work;
126 struct mutex mutex; /* mutual excl. when accessing chip */ 126 struct mutex mutex; /* mutual excl. when accessing chip */
127 int irq; 127 int irq;
128 v4l2_std_id curr_norm; 128 v4l2_std_id curr_norm;
129 bool autodetect; 129 bool autodetect;
130 s8 brightness;
131 s16 hue;
132 u8 contrast;
133 u8 saturation;
134 u8 input; 130 u8 input;
135}; 131};
132#define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler, \
133 struct adv7180_state, \
134 ctrl_hdl)->sd)
136 135
137static v4l2_std_id adv7180_std_to_v4l2(u8 status1) 136static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
138{ 137{
@@ -237,7 +236,7 @@ static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
237 if (ret) 236 if (ret)
238 return ret; 237 return ret;
239 238
240 /*We cannot discriminate between LQFP and 40-pin LFCSP, so accept 239 /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
241 * all inputs and let the card driver take care of validation 240 * all inputs and let the card driver take care of validation
242 */ 241 */
243 if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input) 242 if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input)
@@ -316,117 +315,39 @@ out:
316 return ret; 315 return ret;
317} 316}
318 317
319static int adv7180_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc) 318static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
320{
321 switch (qc->id) {
322 case V4L2_CID_BRIGHTNESS:
323 return v4l2_ctrl_query_fill(qc, BRI_REG_MIN, BRI_REG_MAX,
324 1, BRI_REG_DEF);
325 case V4L2_CID_HUE:
326 return v4l2_ctrl_query_fill(qc, HUE_REG_MIN, HUE_REG_MAX,
327 1, HUE_REG_DEF);
328 case V4L2_CID_CONTRAST:
329 return v4l2_ctrl_query_fill(qc, CON_REG_MIN, CON_REG_MAX,
330 1, CON_REG_DEF);
331 case V4L2_CID_SATURATION:
332 return v4l2_ctrl_query_fill(qc, SAT_REG_MIN, SAT_REG_MAX,
333 1, SAT_REG_DEF);
334 default:
335 break;
336 }
337
338 return -EINVAL;
339}
340
341static int adv7180_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
342{
343 struct adv7180_state *state = to_state(sd);
344 int ret = mutex_lock_interruptible(&state->mutex);
345 if (ret)
346 return ret;
347
348 switch (ctrl->id) {
349 case V4L2_CID_BRIGHTNESS:
350 ctrl->value = state->brightness;
351 break;
352 case V4L2_CID_HUE:
353 ctrl->value = state->hue;
354 break;
355 case V4L2_CID_CONTRAST:
356 ctrl->value = state->contrast;
357 break;
358 case V4L2_CID_SATURATION:
359 ctrl->value = state->saturation;
360 break;
361 default:
362 ret = -EINVAL;
363 }
364
365 mutex_unlock(&state->mutex);
366 return ret;
367}
368
369static int adv7180_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
370{ 319{
320 struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
371 struct adv7180_state *state = to_state(sd); 321 struct adv7180_state *state = to_state(sd);
372 struct i2c_client *client = v4l2_get_subdevdata(sd); 322 struct i2c_client *client = v4l2_get_subdevdata(sd);
373 int ret = mutex_lock_interruptible(&state->mutex); 323 int ret = mutex_lock_interruptible(&state->mutex);
324 int val;
325
374 if (ret) 326 if (ret)
375 return ret; 327 return ret;
376 328 val = ctrl->val;
377 switch (ctrl->id) { 329 switch (ctrl->id) {
378 case V4L2_CID_BRIGHTNESS: 330 case V4L2_CID_BRIGHTNESS:
379 if ((ctrl->value > BRI_REG_MAX) 331 ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG, val);
380 || (ctrl->value < BRI_REG_MIN)) {
381 ret = -ERANGE;
382 break;
383 }
384 state->brightness = ctrl->value;
385 ret = i2c_smbus_write_byte_data(client,
386 ADV7180_BRI_REG,
387 state->brightness);
388 break; 332 break;
389 case V4L2_CID_HUE: 333 case V4L2_CID_HUE:
390 if ((ctrl->value > HUE_REG_MAX)
391 || (ctrl->value < HUE_REG_MIN)) {
392 ret = -ERANGE;
393 break;
394 }
395 state->hue = ctrl->value;
396 /*Hue is inverted according to HSL chart */ 334 /*Hue is inverted according to HSL chart */
397 ret = i2c_smbus_write_byte_data(client, 335 ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, -val);
398 ADV7180_HUE_REG, -state->hue);
399 break; 336 break;
400 case V4L2_CID_CONTRAST: 337 case V4L2_CID_CONTRAST:
401 if ((ctrl->value > CON_REG_MAX) 338 ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG, val);
402 || (ctrl->value < CON_REG_MIN)) {
403 ret = -ERANGE;
404 break;
405 }
406 state->contrast = ctrl->value;
407 ret = i2c_smbus_write_byte_data(client,
408 ADV7180_CON_REG,
409 state->contrast);
410 break; 339 break;
411 case V4L2_CID_SATURATION: 340 case V4L2_CID_SATURATION:
412 if ((ctrl->value > SAT_REG_MAX)
413 || (ctrl->value < SAT_REG_MIN)) {
414 ret = -ERANGE;
415 break;
416 }
417 /* 341 /*
418 *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE 342 *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
419 *Let's not confuse the user, everybody understands saturation 343 *Let's not confuse the user, everybody understands saturation
420 */ 344 */
421 state->saturation = ctrl->value; 345 ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
422 ret = i2c_smbus_write_byte_data(client, 346 val);
423 ADV7180_SD_SAT_CB_REG,
424 state->saturation);
425 if (ret < 0) 347 if (ret < 0)
426 break; 348 break;
427 ret = i2c_smbus_write_byte_data(client, 349 ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
428 ADV7180_SD_SAT_CR_REG, 350 val);
429 state->saturation);
430 break; 351 break;
431 default: 352 default:
432 ret = -EINVAL; 353 ret = -EINVAL;
@@ -436,6 +357,42 @@ static int adv7180_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
436 return ret; 357 return ret;
437} 358}
438 359
360static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
361 .s_ctrl = adv7180_s_ctrl,
362};
363
364static int adv7180_init_controls(struct adv7180_state *state)
365{
366 v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
367
368 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
369 V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
370 ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
371 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
372 V4L2_CID_CONTRAST, ADV7180_CON_MIN,
373 ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
374 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
375 V4L2_CID_SATURATION, ADV7180_SAT_MIN,
376 ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
377 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
378 V4L2_CID_HUE, ADV7180_HUE_MIN,
379 ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
380 state->sd.ctrl_handler = &state->ctrl_hdl;
381 if (state->ctrl_hdl.error) {
382 int err = state->ctrl_hdl.error;
383
384 v4l2_ctrl_handler_free(&state->ctrl_hdl);
385 return err;
386 }
387 v4l2_ctrl_handler_setup(&state->ctrl_hdl);
388
389 return 0;
390}
391static void adv7180_exit_controls(struct adv7180_state *state)
392{
393 v4l2_ctrl_handler_free(&state->ctrl_hdl);
394}
395
439static const struct v4l2_subdev_video_ops adv7180_video_ops = { 396static const struct v4l2_subdev_video_ops adv7180_video_ops = {
440 .querystd = adv7180_querystd, 397 .querystd = adv7180_querystd,
441 .g_input_status = adv7180_g_input_status, 398 .g_input_status = adv7180_g_input_status,
@@ -445,9 +402,9 @@ static const struct v4l2_subdev_video_ops adv7180_video_ops = {
445static const struct v4l2_subdev_core_ops adv7180_core_ops = { 402static const struct v4l2_subdev_core_ops adv7180_core_ops = {
446 .g_chip_ident = adv7180_g_chip_ident, 403 .g_chip_ident = adv7180_g_chip_ident,
447 .s_std = adv7180_s_std, 404 .s_std = adv7180_s_std,
448 .queryctrl = adv7180_queryctrl, 405 .queryctrl = v4l2_subdev_queryctrl,
449 .g_ctrl = adv7180_g_ctrl, 406 .g_ctrl = v4l2_subdev_g_ctrl,
450 .s_ctrl = adv7180_s_ctrl, 407 .s_ctrl = v4l2_subdev_s_ctrl,
451}; 408};
452 409
453static const struct v4l2_subdev_ops adv7180_ops = { 410static const struct v4l2_subdev_ops adv7180_ops = {
@@ -539,7 +496,7 @@ static int init_device(struct i2c_client *client, struct adv7180_state *state)
539 496
540 /* register for interrupts */ 497 /* register for interrupts */
541 if (state->irq > 0) { 498 if (state->irq > 0) {
542 ret = request_irq(state->irq, adv7180_irq, 0, DRIVER_NAME, 499 ret = request_irq(state->irq, adv7180_irq, 0, KBUILD_MODNAME,
543 state); 500 state);
544 if (ret) 501 if (ret)
545 return ret; 502 return ret;
@@ -580,31 +537,6 @@ static int init_device(struct i2c_client *client, struct adv7180_state *state)
580 return ret; 537 return ret;
581 } 538 }
582 539
583 /*Set default value for controls */
584 ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG,
585 state->brightness);
586 if (ret < 0)
587 return ret;
588
589 ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, state->hue);
590 if (ret < 0)
591 return ret;
592
593 ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG,
594 state->contrast);
595 if (ret < 0)
596 return ret;
597
598 ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
599 state->saturation);
600 if (ret < 0)
601 return ret;
602
603 ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
604 state->saturation);
605 if (ret < 0)
606 return ret;
607
608 return 0; 540 return 0;
609} 541}
610 542
@@ -632,25 +564,26 @@ static __devinit int adv7180_probe(struct i2c_client *client,
632 INIT_WORK(&state->work, adv7180_work); 564 INIT_WORK(&state->work, adv7180_work);
633 mutex_init(&state->mutex); 565 mutex_init(&state->mutex);
634 state->autodetect = true; 566 state->autodetect = true;
635 state->brightness = BRI_REG_DEF;
636 state->hue = HUE_REG_DEF;
637 state->contrast = CON_REG_DEF;
638 state->saturation = SAT_REG_DEF;
639 state->input = 0; 567 state->input = 0;
640 sd = &state->sd; 568 sd = &state->sd;
641 v4l2_i2c_subdev_init(sd, client, &adv7180_ops); 569 v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
642 570
643 ret = init_device(client, state); 571 ret = adv7180_init_controls(state);
644 if (0 != ret) 572 if (ret)
645 goto err_unreg_subdev; 573 goto err_unreg_subdev;
574 ret = init_device(client, state);
575 if (ret)
576 goto err_free_ctrl;
646 return 0; 577 return 0;
647 578
579err_free_ctrl:
580 adv7180_exit_controls(state);
648err_unreg_subdev: 581err_unreg_subdev:
649 mutex_destroy(&state->mutex); 582 mutex_destroy(&state->mutex);
650 v4l2_device_unregister_subdev(sd); 583 v4l2_device_unregister_subdev(sd);
651 kfree(state); 584 kfree(state);
652err: 585err:
653 printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", ret); 586 printk(KERN_ERR KBUILD_MODNAME ": Failed to probe: %d\n", ret);
654 return ret; 587 return ret;
655} 588}
656 589
@@ -678,7 +611,7 @@ static __devexit int adv7180_remove(struct i2c_client *client)
678} 611}
679 612
680static const struct i2c_device_id adv7180_id[] = { 613static const struct i2c_device_id adv7180_id[] = {
681 {DRIVER_NAME, 0}, 614 {KBUILD_MODNAME, 0},
682 {}, 615 {},
683}; 616};
684 617
@@ -716,7 +649,7 @@ MODULE_DEVICE_TABLE(i2c, adv7180_id);
716static struct i2c_driver adv7180_driver = { 649static struct i2c_driver adv7180_driver = {
717 .driver = { 650 .driver = {
718 .owner = THIS_MODULE, 651 .owner = THIS_MODULE,
719 .name = DRIVER_NAME, 652 .name = KBUILD_MODNAME,
720 }, 653 },
721 .probe = adv7180_probe, 654 .probe = adv7180_probe,
722 .remove = __devexit_p(adv7180_remove), 655 .remove = __devexit_p(adv7180_remove),
diff --git a/drivers/media/video/bt8xx/bttv-cards.c b/drivers/media/video/bt8xx/bttv-cards.c
index 5f3a00c2c4f6..38952faaffda 100644
--- a/drivers/media/video/bt8xx/bttv-cards.c
+++ b/drivers/media/video/bt8xx/bttv-cards.c
@@ -345,7 +345,7 @@ static struct CARD {
345 { 0x15401836, BTTV_BOARD_PV183, "Provideo PV183-7" }, 345 { 0x15401836, BTTV_BOARD_PV183, "Provideo PV183-7" },
346 { 0x15401837, BTTV_BOARD_PV183, "Provideo PV183-8" }, 346 { 0x15401837, BTTV_BOARD_PV183, "Provideo PV183-8" },
347 { 0x3116f200, BTTV_BOARD_TVT_TD3116, "Tongwei Video Technology TD-3116" }, 347 { 0x3116f200, BTTV_BOARD_TVT_TD3116, "Tongwei Video Technology TD-3116" },
348 348 { 0x02280279, BTTV_BOARD_APOSONIC_WDVR, "Aposonic W-DVR" },
349 { 0, -1, NULL } 349 { 0, -1, NULL }
350}; 350};
351 351
@@ -2818,6 +2818,14 @@ struct tvcard bttv_tvcards[] = {
2818 .pll = PLL_28, 2818 .pll = PLL_28,
2819 .tuner_type = TUNER_ABSENT, 2819 .tuner_type = TUNER_ABSENT,
2820 }, 2820 },
2821 [BTTV_BOARD_APOSONIC_WDVR] = {
2822 .name = "Aposonic W-DVR",
2823 .video_inputs = 4,
2824 .svhs = NO_SVHS,
2825 .muxsel = MUXSEL(2, 3, 1, 0),
2826 .tuner_type = TUNER_ABSENT,
2827 },
2828
2821}; 2829};
2822 2830
2823static const unsigned int bttv_num_tvcards = ARRAY_SIZE(bttv_tvcards); 2831static const unsigned int bttv_num_tvcards = ARRAY_SIZE(bttv_tvcards);
diff --git a/drivers/media/video/bt8xx/bttv.h b/drivers/media/video/bt8xx/bttv.h
index acfe2f3b92d9..79a11240a590 100644
--- a/drivers/media/video/bt8xx/bttv.h
+++ b/drivers/media/video/bt8xx/bttv.h
@@ -184,7 +184,7 @@
184#define BTTV_BOARD_GEOVISION_GV800S_SL 0x9e 184#define BTTV_BOARD_GEOVISION_GV800S_SL 0x9e
185#define BTTV_BOARD_PV183 0x9f 185#define BTTV_BOARD_PV183 0x9f
186#define BTTV_BOARD_TVT_TD3116 0xa0 186#define BTTV_BOARD_TVT_TD3116 0xa0
187 187#define BTTV_BOARD_APOSONIC_WDVR 0xa1
188 188
189/* more card-specific defines */ 189/* more card-specific defines */
190#define PT2254_L_CHANNEL 0x10 190#define PT2254_L_CHANNEL 0x10
diff --git a/drivers/media/video/cx231xx/cx231xx-i2c.c b/drivers/media/video/cx231xx/cx231xx-i2c.c
index 925f3a04e53c..781feed406f7 100644
--- a/drivers/media/video/cx231xx/cx231xx-i2c.c
+++ b/drivers/media/video/cx231xx/cx231xx-i2c.c
@@ -499,16 +499,12 @@ int cx231xx_i2c_register(struct cx231xx_i2c *bus)
499 499
500 BUG_ON(!dev->cx231xx_send_usb_command); 500 BUG_ON(!dev->cx231xx_send_usb_command);
501 501
502 memcpy(&bus->i2c_adap, &cx231xx_adap_template, sizeof(bus->i2c_adap)); 502 bus->i2c_adap = cx231xx_adap_template;
503 memcpy(&bus->i2c_algo, &cx231xx_algo, sizeof(bus->i2c_algo)); 503 bus->i2c_client = cx231xx_client_template;
504 memcpy(&bus->i2c_client, &cx231xx_client_template,
505 sizeof(bus->i2c_client));
506
507 bus->i2c_adap.dev.parent = &dev->udev->dev; 504 bus->i2c_adap.dev.parent = &dev->udev->dev;
508 505
509 strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name)); 506 strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
510 507
511 bus->i2c_algo.data = bus;
512 bus->i2c_adap.algo_data = bus; 508 bus->i2c_adap.algo_data = bus;
513 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev); 509 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
514 i2c_add_adapter(&bus->i2c_adap); 510 i2c_add_adapter(&bus->i2c_adap);
diff --git a/drivers/media/video/cx231xx/cx231xx.h b/drivers/media/video/cx231xx/cx231xx.h
index e17447554a0d..a89d020de948 100644
--- a/drivers/media/video/cx231xx/cx231xx.h
+++ b/drivers/media/video/cx231xx/cx231xx.h
@@ -26,7 +26,6 @@
26#include <linux/types.h> 26#include <linux/types.h>
27#include <linux/ioctl.h> 27#include <linux/ioctl.h>
28#include <linux/i2c.h> 28#include <linux/i2c.h>
29#include <linux/i2c-algo-bit.h>
30#include <linux/workqueue.h> 29#include <linux/workqueue.h>
31#include <linux/mutex.h> 30#include <linux/mutex.h>
32 31
@@ -481,7 +480,6 @@ struct cx231xx_i2c {
481 480
482 /* i2c i/o */ 481 /* i2c i/o */
483 struct i2c_adapter i2c_adap; 482 struct i2c_adapter i2c_adap;
484 struct i2c_algo_bit_data i2c_algo;
485 struct i2c_client i2c_client; 483 struct i2c_client i2c_client;
486 u32 i2c_rc; 484 u32 i2c_rc;
487 485
diff --git a/drivers/media/video/cx23885/cx23885-i2c.c b/drivers/media/video/cx23885/cx23885-i2c.c
index be1e21d8295c..4887314339cb 100644
--- a/drivers/media/video/cx23885/cx23885-i2c.c
+++ b/drivers/media/video/cx23885/cx23885-i2c.c
@@ -316,19 +316,13 @@ int cx23885_i2c_register(struct cx23885_i2c *bus)
316 316
317 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr); 317 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
318 318
319 memcpy(&bus->i2c_adap, &cx23885_i2c_adap_template, 319 bus->i2c_adap = cx23885_i2c_adap_template;
320 sizeof(bus->i2c_adap)); 320 bus->i2c_client = cx23885_i2c_client_template;
321 memcpy(&bus->i2c_algo, &cx23885_i2c_algo_template,
322 sizeof(bus->i2c_algo));
323 memcpy(&bus->i2c_client, &cx23885_i2c_client_template,
324 sizeof(bus->i2c_client));
325
326 bus->i2c_adap.dev.parent = &dev->pci->dev; 321 bus->i2c_adap.dev.parent = &dev->pci->dev;
327 322
328 strlcpy(bus->i2c_adap.name, bus->dev->name, 323 strlcpy(bus->i2c_adap.name, bus->dev->name,
329 sizeof(bus->i2c_adap.name)); 324 sizeof(bus->i2c_adap.name));
330 325
331 bus->i2c_algo.data = bus;
332 bus->i2c_adap.algo_data = bus; 326 bus->i2c_adap.algo_data = bus;
333 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev); 327 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
334 i2c_add_adapter(&bus->i2c_adap); 328 i2c_add_adapter(&bus->i2c_adap);
diff --git a/drivers/media/video/cx23885/cx23885.h b/drivers/media/video/cx23885/cx23885.h
index 13c37ec07ae7..5d560c747e09 100644
--- a/drivers/media/video/cx23885/cx23885.h
+++ b/drivers/media/video/cx23885/cx23885.h
@@ -21,7 +21,6 @@
21 21
22#include <linux/pci.h> 22#include <linux/pci.h>
23#include <linux/i2c.h> 23#include <linux/i2c.h>
24#include <linux/i2c-algo-bit.h>
25#include <linux/kdev_t.h> 24#include <linux/kdev_t.h>
26#include <linux/slab.h> 25#include <linux/slab.h>
27 26
@@ -247,7 +246,6 @@ struct cx23885_i2c {
247 246
248 /* i2c i/o */ 247 /* i2c i/o */
249 struct i2c_adapter i2c_adap; 248 struct i2c_adapter i2c_adap;
250 struct i2c_algo_bit_data i2c_algo;
251 struct i2c_client i2c_client; 249 struct i2c_client i2c_client;
252 u32 i2c_rc; 250 u32 i2c_rc;
253 251
diff --git a/drivers/media/video/cx25821/cx25821-i2c.c b/drivers/media/video/cx25821/cx25821-i2c.c
index 6311180f430c..9844549764c9 100644
--- a/drivers/media/video/cx25821/cx25821-i2c.c
+++ b/drivers/media/video/cx25821/cx25821-i2c.c
@@ -305,18 +305,12 @@ int cx25821_i2c_register(struct cx25821_i2c *bus)
305 305
306 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr); 306 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
307 307
308 memcpy(&bus->i2c_adap, &cx25821_i2c_adap_template, 308 bus->i2c_adap = cx25821_i2c_adap_template;
309 sizeof(bus->i2c_adap)); 309 bus->i2c_client = cx25821_i2c_client_template;
310 memcpy(&bus->i2c_algo, &cx25821_i2c_algo_template,
311 sizeof(bus->i2c_algo));
312 memcpy(&bus->i2c_client, &cx25821_i2c_client_template,
313 sizeof(bus->i2c_client));
314
315 bus->i2c_adap.dev.parent = &dev->pci->dev; 310 bus->i2c_adap.dev.parent = &dev->pci->dev;
316 311
317 strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name)); 312 strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
318 313
319 bus->i2c_algo.data = bus;
320 bus->i2c_adap.algo_data = bus; 314 bus->i2c_adap.algo_data = bus;
321 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev); 315 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
322 i2c_add_adapter(&bus->i2c_adap); 316 i2c_add_adapter(&bus->i2c_adap);
diff --git a/drivers/media/video/cx25821/cx25821-medusa-video.c b/drivers/media/video/cx25821/cx25821-medusa-video.c
index 313fb20a0b47..6a92e5c70c2a 100644
--- a/drivers/media/video/cx25821/cx25821-medusa-video.c
+++ b/drivers/media/video/cx25821/cx25821-medusa-video.c
@@ -499,7 +499,7 @@ static void medusa_set_decoderduration(struct cx25821_dev *dev, int decoder,
499 mutex_lock(&dev->lock); 499 mutex_lock(&dev->lock);
500 500
501 /* no support */ 501 /* no support */
502 if (decoder < VDEC_A && decoder > VDEC_H) { 502 if (decoder < VDEC_A || decoder > VDEC_H) {
503 mutex_unlock(&dev->lock); 503 mutex_unlock(&dev->lock);
504 return; 504 return;
505 } 505 }
diff --git a/drivers/media/video/cx25821/cx25821.h b/drivers/media/video/cx25821/cx25821.h
index 029f2934a6d8..8a9c0c869412 100644
--- a/drivers/media/video/cx25821/cx25821.h
+++ b/drivers/media/video/cx25821/cx25821.h
@@ -26,7 +26,6 @@
26 26
27#include <linux/pci.h> 27#include <linux/pci.h>
28#include <linux/i2c.h> 28#include <linux/i2c.h>
29#include <linux/i2c-algo-bit.h>
30#include <linux/interrupt.h> 29#include <linux/interrupt.h>
31#include <linux/delay.h> 30#include <linux/delay.h>
32#include <linux/sched.h> 31#include <linux/sched.h>
@@ -213,7 +212,6 @@ struct cx25821_i2c {
213 212
214 /* i2c i/o */ 213 /* i2c i/o */
215 struct i2c_adapter i2c_adap; 214 struct i2c_adapter i2c_adap;
216 struct i2c_algo_bit_data i2c_algo;
217 struct i2c_client i2c_client; 215 struct i2c_client i2c_client;
218 u32 i2c_rc; 216 u32 i2c_rc;
219 217
diff --git a/drivers/media/video/davinci/Kconfig b/drivers/media/video/davinci/Kconfig
index 9337b5605c90..52c5ca68cb3d 100644
--- a/drivers/media/video/davinci/Kconfig
+++ b/drivers/media/video/davinci/Kconfig
@@ -1,30 +1,34 @@
1config DISPLAY_DAVINCI_DM646X_EVM 1config VIDEO_DAVINCI_VPIF_DISPLAY
2 tristate "DM646x EVM Video Display" 2 tristate "DM646x/DA850/OMAPL138 EVM Video Display"
3 depends on VIDEO_DEV && MACH_DAVINCI_DM6467_EVM 3 depends on VIDEO_DEV && (MACH_DAVINCI_DM6467_EVM || MACH_DAVINCI_DA850_EVM)
4 select VIDEOBUF_DMA_CONTIG 4 select VIDEOBUF2_DMA_CONTIG
5 select VIDEO_DAVINCI_VPIF 5 select VIDEO_DAVINCI_VPIF
6 select VIDEO_ADV7343 6 select VIDEO_ADV7343 if VIDEO_HELPER_CHIPS_AUTO
7 select VIDEO_THS7303 7 select VIDEO_THS7303 if VIDEO_HELPER_CHIPS_AUTO
8 help 8 help
9 Support for DM6467 based display device. 9 Enables Davinci VPIF module used for display devices.
10 This module is common for following DM6467/DA850/OMAPL138
11 based display devices.
10 12
11 To compile this driver as a module, choose M here: the 13 To compile this driver as a module, choose M here: the
12 module will be called vpif_display. 14 module will be called vpif_display.
13 15
14config CAPTURE_DAVINCI_DM646X_EVM 16config VIDEO_DAVINCI_VPIF_CAPTURE
15 tristate "DM646x EVM Video Capture" 17 tristate "DM646x/DA850/OMAPL138 EVM Video Capture"
16 depends on VIDEO_DEV && MACH_DAVINCI_DM6467_EVM 18 depends on VIDEO_DEV && (MACH_DAVINCI_DM6467_EVM || MACH_DAVINCI_DA850_EVM)
17 select VIDEOBUF_DMA_CONTIG 19 select VIDEOBUF2_DMA_CONTIG
18 select VIDEO_DAVINCI_VPIF 20 select VIDEO_DAVINCI_VPIF
19 help 21 help
20 Support for DM6467 based capture device. 22 Enables Davinci VPIF module used for captur devices.
23 This module is common for following DM6467/DA850/OMAPL138
24 based capture devices.
21 25
22 To compile this driver as a module, choose M here: the 26 To compile this driver as a module, choose M here: the
23 module will be called vpif_capture. 27 module will be called vpif_capture.
24 28
25config VIDEO_DAVINCI_VPIF 29config VIDEO_DAVINCI_VPIF
26 tristate "DaVinci VPIF Driver" 30 tristate "DaVinci VPIF Driver"
27 depends on DISPLAY_DAVINCI_DM646X_EVM 31 depends on VIDEO_DAVINCI_VPIF_DISPLAY || VIDEO_DAVINCI_VPIF_CAPTURE
28 help 32 help
29 Support for DaVinci VPIF Driver. 33 Support for DaVinci VPIF Driver.
30 34
diff --git a/drivers/media/video/davinci/Makefile b/drivers/media/video/davinci/Makefile
index ae7dafb689ab..74ed92d09257 100644
--- a/drivers/media/video/davinci/Makefile
+++ b/drivers/media/video/davinci/Makefile
@@ -5,10 +5,10 @@
5# VPIF 5# VPIF
6obj-$(CONFIG_VIDEO_DAVINCI_VPIF) += vpif.o 6obj-$(CONFIG_VIDEO_DAVINCI_VPIF) += vpif.o
7 7
8#DM646x EVM Display driver 8#VPIF Display driver
9obj-$(CONFIG_DISPLAY_DAVINCI_DM646X_EVM) += vpif_display.o 9obj-$(CONFIG_VIDEO_DAVINCI_VPIF_DISPLAY) += vpif_display.o
10#DM646x EVM Capture driver 10#VPIF Capture driver
11obj-$(CONFIG_CAPTURE_DAVINCI_DM646X_EVM) += vpif_capture.o 11obj-$(CONFIG_VIDEO_DAVINCI_VPIF_CAPTURE) += vpif_capture.o
12 12
13# Capture: DM6446 and DM355 13# Capture: DM6446 and DM355
14obj-$(CONFIG_VIDEO_VPSS_SYSTEM) += vpss.o 14obj-$(CONFIG_VIDEO_VPSS_SYSTEM) += vpss.o
diff --git a/drivers/media/video/davinci/vpbe_display.c b/drivers/media/video/davinci/vpbe_display.c
index e106b72810a9..6fe7034bea7c 100644
--- a/drivers/media/video/davinci/vpbe_display.c
+++ b/drivers/media/video/davinci/vpbe_display.c
@@ -1083,7 +1083,7 @@ vpbe_display_s_dv_preset(struct file *file, void *priv,
1083 } 1083 }
1084 1084
1085 /* Set the given standard in the encoder */ 1085 /* Set the given standard in the encoder */
1086 if (NULL != vpbe_dev->ops.s_dv_preset) 1086 if (!vpbe_dev->ops.s_dv_preset)
1087 return -EINVAL; 1087 return -EINVAL;
1088 1088
1089 ret = vpbe_dev->ops.s_dv_preset(vpbe_dev, preset); 1089 ret = vpbe_dev->ops.s_dv_preset(vpbe_dev, preset);
@@ -1517,6 +1517,8 @@ static int vpbe_display_g_register(struct file *file, void *priv,
1517 struct v4l2_dbg_register *reg) 1517 struct v4l2_dbg_register *reg)
1518{ 1518{
1519 struct v4l2_dbg_match *match = &reg->match; 1519 struct v4l2_dbg_match *match = &reg->match;
1520 struct vpbe_fh *fh = file->private_data;
1521 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1520 1522
1521 if (match->type >= 2) { 1523 if (match->type >= 2) {
1522 v4l2_subdev_call(vpbe_dev->venc, 1524 v4l2_subdev_call(vpbe_dev->venc,
diff --git a/drivers/media/video/davinci/vpif.c b/drivers/media/video/davinci/vpif.c
index af9680273ff9..b3637aff8fee 100644
--- a/drivers/media/video/davinci/vpif.c
+++ b/drivers/media/video/davinci/vpif.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * vpif - DM646x Video Port Interface driver 2 * vpif - Video Port Interface driver
3 * VPIF is a receiver and transmitter for video data. It has two channels(0, 1) 3 * VPIF is a receiver and transmitter for video data. It has two channels(0, 1)
4 * that receiveing video byte stream and two channels(2, 3) for video output. 4 * that receiveing video byte stream and two channels(2, 3) for video output.
5 * The hardware supports SDTV, HDTV formats, raw data capture. 5 * The hardware supports SDTV, HDTV formats, raw data capture.
@@ -23,6 +23,8 @@
23#include <linux/spinlock.h> 23#include <linux/spinlock.h>
24#include <linux/kernel.h> 24#include <linux/kernel.h>
25#include <linux/io.h> 25#include <linux/io.h>
26#include <linux/clk.h>
27#include <linux/err.h>
26#include <mach/hardware.h> 28#include <mach/hardware.h>
27 29
28#include "vpif.h" 30#include "vpif.h"
@@ -40,6 +42,7 @@ static struct resource *res;
40spinlock_t vpif_lock; 42spinlock_t vpif_lock;
41 43
42void __iomem *vpif_base; 44void __iomem *vpif_base;
45struct clk *vpif_clk;
43 46
44/** 47/**
45 * ch_params: video standard configuration parameters for vpif 48 * ch_params: video standard configuration parameters for vpif
@@ -346,7 +349,7 @@ static void config_vpif_params(struct vpif_params *vpifparams,
346 349
347 value = regr(reg); 350 value = regr(reg);
348 /* Set data width */ 351 /* Set data width */
349 value &= ((~(unsigned int)(0x3)) << 352 value &= ~(0x3u <<
350 VPIF_CH_DATA_WIDTH_BIT); 353 VPIF_CH_DATA_WIDTH_BIT);
351 value |= ((vpifparams->params.data_sz) << 354 value |= ((vpifparams->params.data_sz) <<
352 VPIF_CH_DATA_WIDTH_BIT); 355 VPIF_CH_DATA_WIDTH_BIT);
@@ -434,10 +437,19 @@ static int __init vpif_probe(struct platform_device *pdev)
434 goto fail; 437 goto fail;
435 } 438 }
436 439
440 vpif_clk = clk_get(&pdev->dev, "vpif");
441 if (IS_ERR(vpif_clk)) {
442 status = PTR_ERR(vpif_clk);
443 goto clk_fail;
444 }
445 clk_enable(vpif_clk);
446
437 spin_lock_init(&vpif_lock); 447 spin_lock_init(&vpif_lock);
438 dev_info(&pdev->dev, "vpif probe success\n"); 448 dev_info(&pdev->dev, "vpif probe success\n");
439 return 0; 449 return 0;
440 450
451clk_fail:
452 iounmap(vpif_base);
441fail: 453fail:
442 release_mem_region(res->start, res_len); 454 release_mem_region(res->start, res_len);
443 return status; 455 return status;
@@ -445,15 +457,44 @@ fail:
445 457
446static int __devexit vpif_remove(struct platform_device *pdev) 458static int __devexit vpif_remove(struct platform_device *pdev)
447{ 459{
460 if (vpif_clk) {
461 clk_disable(vpif_clk);
462 clk_put(vpif_clk);
463 }
464
448 iounmap(vpif_base); 465 iounmap(vpif_base);
449 release_mem_region(res->start, res_len); 466 release_mem_region(res->start, res_len);
450 return 0; 467 return 0;
451} 468}
452 469
470#ifdef CONFIG_PM
471static int vpif_suspend(struct device *dev)
472{
473 clk_disable(vpif_clk);
474 return 0;
475}
476
477static int vpif_resume(struct device *dev)
478{
479 clk_enable(vpif_clk);
480 return 0;
481}
482
483static const struct dev_pm_ops vpif_pm = {
484 .suspend = vpif_suspend,
485 .resume = vpif_resume,
486};
487
488#define vpif_pm_ops (&vpif_pm)
489#else
490#define vpif_pm_ops NULL
491#endif
492
453static struct platform_driver vpif_driver = { 493static struct platform_driver vpif_driver = {
454 .driver = { 494 .driver = {
455 .name = "vpif", 495 .name = "vpif",
456 .owner = THIS_MODULE, 496 .owner = THIS_MODULE,
497 .pm = vpif_pm_ops,
457 }, 498 },
458 .remove = __devexit_p(vpif_remove), 499 .remove = __devexit_p(vpif_remove),
459 .probe = vpif_probe, 500 .probe = vpif_probe,
diff --git a/drivers/media/video/davinci/vpif.h b/drivers/media/video/davinci/vpif.h
index 8bcac65f9294..c2ce4d97c279 100644
--- a/drivers/media/video/davinci/vpif.h
+++ b/drivers/media/video/davinci/vpif.h
@@ -211,6 +211,12 @@ static inline void vpif_clr_bit(u32 reg, u32 bit)
211#define VPIF_CH3_INT_CTRL_SHIFT (6) 211#define VPIF_CH3_INT_CTRL_SHIFT (6)
212#define VPIF_CH_INT_CTRL_SHIFT (6) 212#define VPIF_CH_INT_CTRL_SHIFT (6)
213 213
214#define VPIF_CH2_CLIP_ANC_EN 14
215#define VPIF_CH2_CLIP_ACTIVE_EN 13
216
217#define VPIF_CH3_CLIP_ANC_EN 14
218#define VPIF_CH3_CLIP_ACTIVE_EN 13
219
214/* enabled interrupt on both the fields on vpid_ch0_ctrl register */ 220/* enabled interrupt on both the fields on vpid_ch0_ctrl register */
215#define channel0_intr_assert() (regw((regr(VPIF_CH0_CTRL)|\ 221#define channel0_intr_assert() (regw((regr(VPIF_CH0_CTRL)|\
216 (VPIF_INT_BOTH << VPIF_CH0_INT_CTRL_SHIFT)), VPIF_CH0_CTRL)) 222 (VPIF_INT_BOTH << VPIF_CH0_INT_CTRL_SHIFT)), VPIF_CH0_CTRL))
@@ -515,6 +521,30 @@ static inline void channel3_raw_enable(int enable, u8 index)
515 vpif_clr_bit(VPIF_CH3_CTRL, mask); 521 vpif_clr_bit(VPIF_CH3_CTRL, mask);
516} 522}
517 523
524/* function to enable clipping (for both active and blanking regions) on ch 2 */
525static inline void channel2_clipping_enable(int enable)
526{
527 if (enable) {
528 vpif_set_bit(VPIF_CH2_CTRL, VPIF_CH2_CLIP_ANC_EN);
529 vpif_set_bit(VPIF_CH2_CTRL, VPIF_CH2_CLIP_ACTIVE_EN);
530 } else {
531 vpif_clr_bit(VPIF_CH2_CTRL, VPIF_CH2_CLIP_ANC_EN);
532 vpif_clr_bit(VPIF_CH2_CTRL, VPIF_CH2_CLIP_ACTIVE_EN);
533 }
534}
535
536/* function to enable clipping (for both active and blanking regions) on ch 2 */
537static inline void channel3_clipping_enable(int enable)
538{
539 if (enable) {
540 vpif_set_bit(VPIF_CH3_CTRL, VPIF_CH3_CLIP_ANC_EN);
541 vpif_set_bit(VPIF_CH3_CTRL, VPIF_CH3_CLIP_ACTIVE_EN);
542 } else {
543 vpif_clr_bit(VPIF_CH3_CTRL, VPIF_CH3_CLIP_ANC_EN);
544 vpif_clr_bit(VPIF_CH3_CTRL, VPIF_CH3_CLIP_ACTIVE_EN);
545 }
546}
547
518/* inline function to set buffer addresses in case of Y/C non mux mode */ 548/* inline function to set buffer addresses in case of Y/C non mux mode */
519static inline void ch2_set_videobuf_addr_yc_nmux(unsigned long top_strt_luma, 549static inline void ch2_set_videobuf_addr_yc_nmux(unsigned long top_strt_luma,
520 unsigned long btm_strt_luma, 550 unsigned long btm_strt_luma,
@@ -569,6 +599,21 @@ static inline void ch3_set_vbi_addr(unsigned long top_strt_luma,
569 regw(btm_strt_luma, VPIF_CH3_BTM_STRT_ADD_VANC); 599 regw(btm_strt_luma, VPIF_CH3_BTM_STRT_ADD_VANC);
570} 600}
571 601
602static inline int vpif_intr_status(int channel)
603{
604 int status = 0;
605 int mask;
606
607 if (channel < 0 || channel > 3)
608 return 0;
609
610 mask = 1 << channel;
611 status = regr(VPIF_STATUS) & mask;
612 regw(status, VPIF_STATUS_CLR);
613
614 return status;
615}
616
572#define VPIF_MAX_NAME (30) 617#define VPIF_MAX_NAME (30)
573 618
574/* This structure will store size parameters as per the mode selected by user */ 619/* This structure will store size parameters as per the mode selected by user */
diff --git a/drivers/media/video/davinci/vpif_capture.c b/drivers/media/video/davinci/vpif_capture.c
index 96046957bf21..266025e5d81d 100644
--- a/drivers/media/video/davinci/vpif_capture.c
+++ b/drivers/media/video/davinci/vpif_capture.c
@@ -80,108 +80,45 @@ static struct vpif_config_params config_params = {
80/* global variables */ 80/* global variables */
81static struct vpif_device vpif_obj = { {NULL} }; 81static struct vpif_device vpif_obj = { {NULL} };
82static struct device *vpif_dev; 82static struct device *vpif_dev;
83 83static void vpif_calculate_offsets(struct channel_obj *ch);
84/** 84static void vpif_config_addr(struct channel_obj *ch, int muxmode);
85 * vpif_uservirt_to_phys : translate user/virtual address to phy address
86 * @virtp: user/virtual address
87 *
88 * This inline function is used to convert user space virtual address to
89 * physical address.
90 */
91static inline u32 vpif_uservirt_to_phys(u32 virtp)
92{
93 unsigned long physp = 0;
94 struct mm_struct *mm = current->mm;
95 struct vm_area_struct *vma;
96
97 vma = find_vma(mm, virtp);
98
99 /* For kernel direct-mapped memory, take the easy way */
100 if (virtp >= PAGE_OFFSET)
101 physp = virt_to_phys((void *)virtp);
102 else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
103 /**
104 * this will catch, kernel-allocated, mmaped-to-usermode
105 * addresses
106 */
107 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
108 else {
109 /* otherwise, use get_user_pages() for general userland pages */
110 int res, nr_pages = 1;
111 struct page *pages;
112
113 down_read(&current->mm->mmap_sem);
114
115 res = get_user_pages(current, current->mm,
116 virtp, nr_pages, 1, 0, &pages, NULL);
117 up_read(&current->mm->mmap_sem);
118
119 if (res == nr_pages)
120 physp = __pa(page_address(&pages[0]) +
121 (virtp & ~PAGE_MASK));
122 else {
123 vpif_err("get_user_pages failed\n");
124 return 0;
125 }
126 }
127 return physp;
128}
129 85
130/** 86/**
131 * buffer_prepare : callback function for buffer prepare 87 * buffer_prepare : callback function for buffer prepare
132 * @q : buffer queue ptr 88 * @vb: ptr to vb2_buffer
133 * @vb: ptr to video buffer
134 * @field: field info
135 * 89 *
136 * This is the callback function for buffer prepare when videobuf_qbuf() 90 * This is the callback function for buffer prepare when vb2_qbuf()
137 * function is called. The buffer is prepared and user space virtual address 91 * function is called. The buffer is prepared and user space virtual address
138 * or user address is converted into physical address 92 * or user address is converted into physical address
139 */ 93 */
140static int vpif_buffer_prepare(struct videobuf_queue *q, 94static int vpif_buffer_prepare(struct vb2_buffer *vb)
141 struct videobuf_buffer *vb,
142 enum v4l2_field field)
143{ 95{
144 /* Get the file handle object and channel object */ 96 /* Get the file handle object and channel object */
145 struct vpif_fh *fh = q->priv_data; 97 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
98 struct vb2_queue *q = vb->vb2_queue;
146 struct channel_obj *ch = fh->channel; 99 struct channel_obj *ch = fh->channel;
147 struct common_obj *common; 100 struct common_obj *common;
148 unsigned long addr; 101 unsigned long addr;
149 102
150
151 vpif_dbg(2, debug, "vpif_buffer_prepare\n"); 103 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
152 104
153 common = &ch->common[VPIF_VIDEO_INDEX]; 105 common = &ch->common[VPIF_VIDEO_INDEX];
154 106
155 /* If buffer is not initialized, initialize it */ 107 if (vb->state != VB2_BUF_STATE_ACTIVE &&
156 if (VIDEOBUF_NEEDS_INIT == vb->state) { 108 vb->state != VB2_BUF_STATE_PREPARED) {
157 vb->width = common->width; 109 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
158 vb->height = common->height; 110 if (vb2_plane_vaddr(vb, 0) &&
159 vb->size = vb->width * vb->height; 111 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
160 vb->field = field;
161 }
162 vb->state = VIDEOBUF_PREPARED;
163 /**
164 * if user pointer memory mechanism is used, get the physical
165 * address of the buffer
166 */
167 if (V4L2_MEMORY_USERPTR == common->memory) {
168 if (0 == vb->baddr) {
169 vpif_dbg(1, debug, "buffer address is 0\n");
170 return -EINVAL;
171
172 }
173 vb->boff = vpif_uservirt_to_phys(vb->baddr);
174 if (!IS_ALIGNED(vb->boff, 8))
175 goto exit; 112 goto exit;
176 } 113 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
177 114
178 addr = vb->boff; 115 if (q->streaming) {
179 if (q->streaming) { 116 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
180 if (!IS_ALIGNED((addr + common->ytop_off), 8) || 117 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
181 !IS_ALIGNED((addr + common->ybtm_off), 8) || 118 !IS_ALIGNED((addr + common->ctop_off), 8) ||
182 !IS_ALIGNED((addr + common->ctop_off), 8) || 119 !IS_ALIGNED((addr + common->cbtm_off), 8))
183 !IS_ALIGNED((addr + common->cbtm_off), 8)) 120 goto exit;
184 goto exit; 121 }
185 } 122 }
186 return 0; 123 return 0;
187exit: 124exit:
@@ -190,49 +127,79 @@ exit:
190} 127}
191 128
192/** 129/**
193 * vpif_buffer_setup : Callback function for buffer setup. 130 * vpif_buffer_queue_setup : Callback function for buffer setup.
194 * @q: buffer queue ptr 131 * @vq: vb2_queue ptr
195 * @count: number of buffers 132 * @fmt: v4l2 format
196 * @size: size of the buffer 133 * @nbuffers: ptr to number of buffers requested by application
134 * @nplanes:: contains number of distinct video planes needed to hold a frame
135 * @sizes[]: contains the size (in bytes) of each plane.
136 * @alloc_ctxs: ptr to allocation context
197 * 137 *
198 * This callback function is called when reqbuf() is called to adjust 138 * This callback function is called when reqbuf() is called to adjust
199 * the buffer count and buffer size 139 * the buffer count and buffer size
200 */ 140 */
201static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count, 141static int vpif_buffer_queue_setup(struct vb2_queue *vq,
202 unsigned int *size) 142 const struct v4l2_format *fmt,
143 unsigned int *nbuffers, unsigned int *nplanes,
144 unsigned int sizes[], void *alloc_ctxs[])
203{ 145{
204 /* Get the file handle object and channel object */ 146 /* Get the file handle object and channel object */
205 struct vpif_fh *fh = q->priv_data; 147 struct vpif_fh *fh = vb2_get_drv_priv(vq);
206 struct channel_obj *ch = fh->channel; 148 struct channel_obj *ch = fh->channel;
207 struct common_obj *common; 149 struct common_obj *common;
150 unsigned long size;
208 151
209 common = &ch->common[VPIF_VIDEO_INDEX]; 152 common = &ch->common[VPIF_VIDEO_INDEX];
210 153
211 vpif_dbg(2, debug, "vpif_buffer_setup\n"); 154 vpif_dbg(2, debug, "vpif_buffer_setup\n");
212 155
213 /* If memory type is not mmap, return */ 156 /* If memory type is not mmap, return */
214 if (V4L2_MEMORY_MMAP != common->memory) 157 if (V4L2_MEMORY_MMAP == common->memory) {
215 return 0; 158 /* Calculate the size of the buffer */
159 size = config_params.channel_bufsize[ch->channel_id];
160 /*
161 * Checking if the buffer size exceeds the available buffer
162 * ycmux_mode = 0 means 1 channel mode HD and
163 * ycmux_mode = 1 means 2 channels mode SD
164 */
165 if (ch->vpifparams.std_info.ycmux_mode == 0) {
166 if (config_params.video_limit[ch->channel_id])
167 while (size * *nbuffers >
168 (config_params.video_limit[0]
169 + config_params.video_limit[1]))
170 (*nbuffers)--;
171 } else {
172 if (config_params.video_limit[ch->channel_id])
173 while (size * *nbuffers >
174 config_params.video_limit[ch->channel_id])
175 (*nbuffers)--;
176 }
177
178 } else {
179 size = common->fmt.fmt.pix.sizeimage;
180 }
181
182 if (*nbuffers < config_params.min_numbuffers)
183 *nbuffers = config_params.min_numbuffers;
216 184
217 /* Calculate the size of the buffer */ 185 *nplanes = 1;
218 *size = config_params.channel_bufsize[ch->channel_id]; 186 sizes[0] = size;
187 alloc_ctxs[0] = common->alloc_ctx;
219 188
220 if (*count < config_params.min_numbuffers)
221 *count = config_params.min_numbuffers;
222 return 0; 189 return 0;
223} 190}
224 191
225/** 192/**
226 * vpif_buffer_queue : Callback function to add buffer to DMA queue 193 * vpif_buffer_queue : Callback function to add buffer to DMA queue
227 * @q: ptr to videobuf_queue 194 * @vb: ptr to vb2_buffer
228 * @vb: ptr to videobuf_buffer
229 */ 195 */
230static void vpif_buffer_queue(struct videobuf_queue *q, 196static void vpif_buffer_queue(struct vb2_buffer *vb)
231 struct videobuf_buffer *vb)
232{ 197{
233 /* Get the file handle object and channel object */ 198 /* Get the file handle object and channel object */
234 struct vpif_fh *fh = q->priv_data; 199 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
235 struct channel_obj *ch = fh->channel; 200 struct channel_obj *ch = fh->channel;
201 struct vpif_cap_buffer *buf = container_of(vb,
202 struct vpif_cap_buffer, vb);
236 struct common_obj *common; 203 struct common_obj *common;
237 204
238 common = &ch->common[VPIF_VIDEO_INDEX]; 205 common = &ch->common[VPIF_VIDEO_INDEX];
@@ -240,43 +207,189 @@ static void vpif_buffer_queue(struct videobuf_queue *q,
240 vpif_dbg(2, debug, "vpif_buffer_queue\n"); 207 vpif_dbg(2, debug, "vpif_buffer_queue\n");
241 208
242 /* add the buffer to the DMA queue */ 209 /* add the buffer to the DMA queue */
243 list_add_tail(&vb->queue, &common->dma_queue); 210 list_add_tail(&buf->list, &common->dma_queue);
244 /* Change state of the buffer */
245 vb->state = VIDEOBUF_QUEUED;
246} 211}
247 212
248/** 213/**
249 * vpif_buffer_release : Callback function to free buffer 214 * vpif_buf_cleanup : Callback function to free buffer
250 * @q: buffer queue ptr 215 * @vb: ptr to vb2_buffer
251 * @vb: ptr to video buffer
252 * 216 *
253 * This function is called from the videobuf layer to free memory 217 * This function is called from the videobuf2 layer to free memory
254 * allocated to the buffers 218 * allocated to the buffers
255 */ 219 */
256static void vpif_buffer_release(struct videobuf_queue *q, 220static void vpif_buf_cleanup(struct vb2_buffer *vb)
257 struct videobuf_buffer *vb)
258{ 221{
259 /* Get the file handle object and channel object */ 222 /* Get the file handle object and channel object */
260 struct vpif_fh *fh = q->priv_data; 223 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
224 struct vpif_cap_buffer *buf = container_of(vb,
225 struct vpif_cap_buffer, vb);
261 struct channel_obj *ch = fh->channel; 226 struct channel_obj *ch = fh->channel;
262 struct common_obj *common; 227 struct common_obj *common;
228 unsigned long flags;
263 229
264 common = &ch->common[VPIF_VIDEO_INDEX]; 230 common = &ch->common[VPIF_VIDEO_INDEX];
265 231
266 videobuf_dma_contig_free(q, vb); 232 spin_lock_irqsave(&common->irqlock, flags);
267 vb->state = VIDEOBUF_NEEDS_INIT; 233 if (vb->state == VB2_BUF_STATE_ACTIVE)
234 list_del_init(&buf->list);
235 spin_unlock_irqrestore(&common->irqlock, flags);
236
268} 237}
269 238
270static struct videobuf_queue_ops video_qops = { 239static void vpif_wait_prepare(struct vb2_queue *vq)
271 .buf_setup = vpif_buffer_setup, 240{
272 .buf_prepare = vpif_buffer_prepare, 241 struct vpif_fh *fh = vb2_get_drv_priv(vq);
273 .buf_queue = vpif_buffer_queue, 242 struct channel_obj *ch = fh->channel;
274 .buf_release = vpif_buffer_release, 243 struct common_obj *common;
275}; 244
245 common = &ch->common[VPIF_VIDEO_INDEX];
246 mutex_unlock(&common->lock);
247}
248
249static void vpif_wait_finish(struct vb2_queue *vq)
250{
251 struct vpif_fh *fh = vb2_get_drv_priv(vq);
252 struct channel_obj *ch = fh->channel;
253 struct common_obj *common;
254
255 common = &ch->common[VPIF_VIDEO_INDEX];
256 mutex_lock(&common->lock);
257}
258
259static int vpif_buffer_init(struct vb2_buffer *vb)
260{
261 struct vpif_cap_buffer *buf = container_of(vb,
262 struct vpif_cap_buffer, vb);
263
264 INIT_LIST_HEAD(&buf->list);
265
266 return 0;
267}
276 268
277static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = 269static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
278 { {1, 1} }; 270 { {1, 1} };
279 271
272static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
273{
274 struct vpif_capture_config *vpif_config_data =
275 vpif_dev->platform_data;
276 struct vpif_fh *fh = vb2_get_drv_priv(vq);
277 struct channel_obj *ch = fh->channel;
278 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
279 struct vpif_params *vpif = &ch->vpifparams;
280 unsigned long addr = 0;
281 int ret;
282
283 /* If buffer queue is empty, return error */
284 if (list_empty(&common->dma_queue)) {
285 vpif_dbg(1, debug, "buffer queue is empty\n");
286 return -EIO;
287 }
288
289 /* Get the next frame from the buffer queue */
290 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
291 struct vpif_cap_buffer, list);
292 /* Remove buffer from the buffer queue */
293 list_del(&common->cur_frm->list);
294 /* Mark state of the current frame to active */
295 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
296 /* Initialize field_id and started member */
297 ch->field_id = 0;
298 common->started = 1;
299 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
300
301 /* Calculate the offset for Y and C data in the buffer */
302 vpif_calculate_offsets(ch);
303
304 if ((vpif->std_info.frm_fmt &&
305 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
306 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
307 (!vpif->std_info.frm_fmt &&
308 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
309 vpif_dbg(1, debug, "conflict in field format and std format\n");
310 return -EINVAL;
311 }
312
313 /* configure 1 or 2 channel mode */
314 ret = vpif_config_data->setup_input_channel_mode
315 (vpif->std_info.ycmux_mode);
316
317 if (ret < 0) {
318 vpif_dbg(1, debug, "can't set vpif channel mode\n");
319 return ret;
320 }
321
322 /* Call vpif_set_params function to set the parameters and addresses */
323 ret = vpif_set_video_params(vpif, ch->channel_id);
324
325 if (ret < 0) {
326 vpif_dbg(1, debug, "can't set video params\n");
327 return ret;
328 }
329
330 common->started = ret;
331 vpif_config_addr(ch, ret);
332
333 common->set_addr(addr + common->ytop_off,
334 addr + common->ybtm_off,
335 addr + common->ctop_off,
336 addr + common->cbtm_off);
337
338 /**
339 * Set interrupt for both the fields in VPIF Register enable channel in
340 * VPIF register
341 */
342 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
343 channel0_intr_assert();
344 channel0_intr_enable(1);
345 enable_channel0(1);
346 }
347 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
348 (common->started == 2)) {
349 channel1_intr_assert();
350 channel1_intr_enable(1);
351 enable_channel1(1);
352 }
353 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
354
355 return 0;
356}
357
358/* abort streaming and wait for last buffer */
359static int vpif_stop_streaming(struct vb2_queue *vq)
360{
361 struct vpif_fh *fh = vb2_get_drv_priv(vq);
362 struct channel_obj *ch = fh->channel;
363 struct common_obj *common;
364
365 if (!vb2_is_streaming(vq))
366 return 0;
367
368 common = &ch->common[VPIF_VIDEO_INDEX];
369
370 /* release all active buffers */
371 while (!list_empty(&common->dma_queue)) {
372 common->next_frm = list_entry(common->dma_queue.next,
373 struct vpif_cap_buffer, list);
374 list_del(&common->next_frm->list);
375 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
376 }
377
378 return 0;
379}
380
381static struct vb2_ops video_qops = {
382 .queue_setup = vpif_buffer_queue_setup,
383 .wait_prepare = vpif_wait_prepare,
384 .wait_finish = vpif_wait_finish,
385 .buf_init = vpif_buffer_init,
386 .buf_prepare = vpif_buffer_prepare,
387 .start_streaming = vpif_start_streaming,
388 .stop_streaming = vpif_stop_streaming,
389 .buf_cleanup = vpif_buf_cleanup,
390 .buf_queue = vpif_buffer_queue,
391};
392
280/** 393/**
281 * vpif_process_buffer_complete: process a completed buffer 394 * vpif_process_buffer_complete: process a completed buffer
282 * @common: ptr to common channel object 395 * @common: ptr to common channel object
@@ -287,9 +400,9 @@ static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
287 */ 400 */
288static void vpif_process_buffer_complete(struct common_obj *common) 401static void vpif_process_buffer_complete(struct common_obj *common)
289{ 402{
290 do_gettimeofday(&common->cur_frm->ts); 403 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
291 common->cur_frm->state = VIDEOBUF_DONE; 404 vb2_buffer_done(&common->cur_frm->vb,
292 wake_up_interruptible(&common->cur_frm->done); 405 VB2_BUF_STATE_DONE);
293 /* Make curFrm pointing to nextFrm */ 406 /* Make curFrm pointing to nextFrm */
294 common->cur_frm = common->next_frm; 407 common->cur_frm = common->next_frm;
295} 408}
@@ -307,14 +420,11 @@ static void vpif_schedule_next_buffer(struct common_obj *common)
307 unsigned long addr = 0; 420 unsigned long addr = 0;
308 421
309 common->next_frm = list_entry(common->dma_queue.next, 422 common->next_frm = list_entry(common->dma_queue.next,
310 struct videobuf_buffer, queue); 423 struct vpif_cap_buffer, list);
311 /* Remove that buffer from the buffer queue */ 424 /* Remove that buffer from the buffer queue */
312 list_del(&common->next_frm->queue); 425 list_del(&common->next_frm->list);
313 common->next_frm->state = VIDEOBUF_ACTIVE; 426 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
314 if (V4L2_MEMORY_USERPTR == common->memory) 427 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
315 addr = common->next_frm->boff;
316 else
317 addr = videobuf_to_dma_contig(common->next_frm);
318 428
319 /* Set top and bottom field addresses in VPIF registers */ 429 /* Set top and bottom field addresses in VPIF registers */
320 common->set_addr(addr + common->ytop_off, 430 common->set_addr(addr + common->ytop_off,
@@ -341,6 +451,9 @@ static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
341 int fid = -1, i; 451 int fid = -1, i;
342 452
343 channel_id = *(int *)(dev_id); 453 channel_id = *(int *)(dev_id);
454 if (!vpif_intr_status(channel_id))
455 return IRQ_NONE;
456
344 ch = dev->dev[channel_id]; 457 ch = dev->dev[channel_id];
345 458
346 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field; 459 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
@@ -485,10 +598,7 @@ static void vpif_calculate_offsets(struct channel_obj *ch)
485 } else 598 } else
486 vid_ch->buf_field = common->fmt.fmt.pix.field; 599 vid_ch->buf_field = common->fmt.fmt.pix.field;
487 600
488 if (V4L2_MEMORY_USERPTR == common->memory) 601 sizeimage = common->fmt.fmt.pix.sizeimage;
489 sizeimage = common->fmt.fmt.pix.sizeimage;
490 else
491 sizeimage = config_params.channel_bufsize[ch->channel_id];
492 602
493 hpitch = common->fmt.fmt.pix.bytesperline; 603 hpitch = common->fmt.fmt.pix.bytesperline;
494 vpitch = sizeimage / (hpitch * 2); 604 vpitch = sizeimage / (hpitch * 2);
@@ -640,10 +750,7 @@ static int vpif_check_format(struct channel_obj *ch,
640 hpitch = vpif_params->std_info.width; 750 hpitch = vpif_params->std_info.width;
641 } 751 }
642 752
643 if (V4L2_MEMORY_USERPTR == common->memory) 753 sizeimage = pixfmt->sizeimage;
644 sizeimage = pixfmt->sizeimage;
645 else
646 sizeimage = config_params.channel_bufsize[ch->channel_id];
647 754
648 vpitch = sizeimage / (hpitch * 2); 755 vpitch = sizeimage / (hpitch * 2);
649 756
@@ -703,7 +810,7 @@ static void vpif_config_addr(struct channel_obj *ch, int muxmode)
703} 810}
704 811
705/** 812/**
706 * vpfe_mmap : It is used to map kernel space buffers into user spaces 813 * vpif_mmap : It is used to map kernel space buffers into user spaces
707 * @filep: file pointer 814 * @filep: file pointer
708 * @vma: ptr to vm_area_struct 815 * @vma: ptr to vm_area_struct
709 */ 816 */
@@ -716,7 +823,7 @@ static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
716 823
717 vpif_dbg(2, debug, "vpif_mmap\n"); 824 vpif_dbg(2, debug, "vpif_mmap\n");
718 825
719 return videobuf_mmap_mapper(&common->buffer_queue, vma); 826 return vb2_mmap(&common->buffer_queue, vma);
720} 827}
721 828
722/** 829/**
@@ -733,7 +840,7 @@ static unsigned int vpif_poll(struct file *filep, poll_table * wait)
733 vpif_dbg(2, debug, "vpif_poll\n"); 840 vpif_dbg(2, debug, "vpif_poll\n");
734 841
735 if (common->started) 842 if (common->started)
736 return videobuf_poll_stream(filep, &common->buffer_queue, wait); 843 return vb2_poll(&common->buffer_queue, filep, wait);
737 return 0; 844 return 0;
738} 845}
739 846
@@ -812,7 +919,7 @@ static int vpif_open(struct file *filep)
812 * vpif_release : function to clean up file close 919 * vpif_release : function to clean up file close
813 * @filep: file pointer 920 * @filep: file pointer
814 * 921 *
815 * This function deletes buffer queue, frees the buffers and the vpfe file 922 * This function deletes buffer queue, frees the buffers and the vpif file
816 * handle 923 * handle
817 */ 924 */
818static int vpif_release(struct file *filep) 925static int vpif_release(struct file *filep)
@@ -841,8 +948,8 @@ static int vpif_release(struct file *filep)
841 } 948 }
842 common->started = 0; 949 common->started = 0;
843 /* Free buffers allocated */ 950 /* Free buffers allocated */
844 videobuf_queue_cancel(&common->buffer_queue); 951 vb2_queue_release(&common->buffer_queue);
845 videobuf_mmap_free(&common->buffer_queue); 952 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
846 } 953 }
847 954
848 /* Decrement channel usrs counter */ 955 /* Decrement channel usrs counter */
@@ -872,6 +979,7 @@ static int vpif_reqbufs(struct file *file, void *priv,
872 struct channel_obj *ch = fh->channel; 979 struct channel_obj *ch = fh->channel;
873 struct common_obj *common; 980 struct common_obj *common;
874 u8 index = 0; 981 u8 index = 0;
982 struct vb2_queue *q;
875 983
876 vpif_dbg(2, debug, "vpif_reqbufs\n"); 984 vpif_dbg(2, debug, "vpif_reqbufs\n");
877 985
@@ -887,7 +995,7 @@ static int vpif_reqbufs(struct file *file, void *priv,
887 } 995 }
888 } 996 }
889 997
890 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type) 998 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
891 return -EINVAL; 999 return -EINVAL;
892 1000
893 index = VPIF_VIDEO_INDEX; 1001 index = VPIF_VIDEO_INDEX;
@@ -897,14 +1005,21 @@ static int vpif_reqbufs(struct file *file, void *priv,
897 if (0 != common->io_usrs) 1005 if (0 != common->io_usrs)
898 return -EBUSY; 1006 return -EBUSY;
899 1007
900 /* Initialize videobuf queue as per the buffer type */ 1008 /* Initialize videobuf2 queue as per the buffer type */
901 videobuf_queue_dma_contig_init(&common->buffer_queue, 1009 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
902 &video_qops, NULL, 1010 if (!common->alloc_ctx) {
903 &common->irqlock, 1011 vpif_err("Failed to get the context\n");
904 reqbuf->type, 1012 return -EINVAL;
905 common->fmt.fmt.pix.field, 1013 }
906 sizeof(struct videobuf_buffer), fh, 1014 q = &common->buffer_queue;
907 &common->lock); 1015 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1016 q->io_modes = VB2_MMAP | VB2_USERPTR;
1017 q->drv_priv = fh;
1018 q->ops = &video_qops;
1019 q->mem_ops = &vb2_dma_contig_memops;
1020 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1021
1022 vb2_queue_init(q);
908 1023
909 /* Set io allowed member of file handle to TRUE */ 1024 /* Set io allowed member of file handle to TRUE */
910 fh->io_allowed[index] = 1; 1025 fh->io_allowed[index] = 1;
@@ -915,7 +1030,7 @@ static int vpif_reqbufs(struct file *file, void *priv,
915 INIT_LIST_HEAD(&common->dma_queue); 1030 INIT_LIST_HEAD(&common->dma_queue);
916 1031
917 /* Allocate buffers */ 1032 /* Allocate buffers */
918 return videobuf_reqbufs(&common->buffer_queue, reqbuf); 1033 return vb2_reqbufs(&common->buffer_queue, reqbuf);
919} 1034}
920 1035
921/** 1036/**
@@ -941,7 +1056,7 @@ static int vpif_querybuf(struct file *file, void *priv,
941 return -EINVAL; 1056 return -EINVAL;
942 } 1057 }
943 1058
944 return videobuf_querybuf(&common->buffer_queue, buf); 1059 return vb2_querybuf(&common->buffer_queue, buf);
945} 1060}
946 1061
947/** 1062/**
@@ -957,10 +1072,6 @@ static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
957 struct channel_obj *ch = fh->channel; 1072 struct channel_obj *ch = fh->channel;
958 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1073 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
959 struct v4l2_buffer tbuf = *buf; 1074 struct v4l2_buffer tbuf = *buf;
960 struct videobuf_buffer *buf1;
961 unsigned long addr = 0;
962 unsigned long flags;
963 int ret = 0;
964 1075
965 vpif_dbg(2, debug, "vpif_qbuf\n"); 1076 vpif_dbg(2, debug, "vpif_qbuf\n");
966 1077
@@ -970,76 +1081,11 @@ static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
970 } 1081 }
971 1082
972 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) { 1083 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
973 vpif_err("fh io not allowed \n"); 1084 vpif_err("fh io not allowed\n");
974 return -EACCES; 1085 return -EACCES;
975 } 1086 }
976 1087
977 if (!(list_empty(&common->dma_queue)) || 1088 return vb2_qbuf(&common->buffer_queue, buf);
978 (common->cur_frm != common->next_frm) ||
979 !common->started ||
980 (common->started && (0 == ch->field_id)))
981 return videobuf_qbuf(&common->buffer_queue, buf);
982
983 /* bufferqueue is empty store buffer address in VPIF registers */
984 mutex_lock(&common->buffer_queue.vb_lock);
985 buf1 = common->buffer_queue.bufs[tbuf.index];
986
987 if ((buf1->state == VIDEOBUF_QUEUED) ||
988 (buf1->state == VIDEOBUF_ACTIVE)) {
989 vpif_err("invalid state\n");
990 goto qbuf_exit;
991 }
992
993 switch (buf1->memory) {
994 case V4L2_MEMORY_MMAP:
995 if (buf1->baddr == 0)
996 goto qbuf_exit;
997 break;
998
999 case V4L2_MEMORY_USERPTR:
1000 if (tbuf.length < buf1->bsize)
1001 goto qbuf_exit;
1002
1003 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
1004 && (buf1->baddr != tbuf.m.userptr)) {
1005 vpif_buffer_release(&common->buffer_queue, buf1);
1006 buf1->baddr = tbuf.m.userptr;
1007 }
1008 break;
1009
1010 default:
1011 goto qbuf_exit;
1012 }
1013
1014 local_irq_save(flags);
1015 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
1016 common->buffer_queue.field);
1017 if (ret < 0) {
1018 local_irq_restore(flags);
1019 goto qbuf_exit;
1020 }
1021
1022 buf1->state = VIDEOBUF_ACTIVE;
1023
1024 if (V4L2_MEMORY_USERPTR == common->memory)
1025 addr = buf1->boff;
1026 else
1027 addr = videobuf_to_dma_contig(buf1);
1028
1029 common->next_frm = buf1;
1030 common->set_addr(addr + common->ytop_off,
1031 addr + common->ybtm_off,
1032 addr + common->ctop_off,
1033 addr + common->cbtm_off);
1034
1035 local_irq_restore(flags);
1036 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
1037 mutex_unlock(&common->buffer_queue.vb_lock);
1038 return 0;
1039
1040qbuf_exit:
1041 mutex_unlock(&common->buffer_queue.vb_lock);
1042 return -EINVAL;
1043} 1089}
1044 1090
1045/** 1091/**
@@ -1056,8 +1102,8 @@ static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1056 1102
1057 vpif_dbg(2, debug, "vpif_dqbuf\n"); 1103 vpif_dbg(2, debug, "vpif_dqbuf\n");
1058 1104
1059 return videobuf_dqbuf(&common->buffer_queue, buf, 1105 return vb2_dqbuf(&common->buffer_queue, buf,
1060 file->f_flags & O_NONBLOCK); 1106 (file->f_flags & O_NONBLOCK));
1061} 1107}
1062 1108
1063/** 1109/**
@@ -1070,13 +1116,11 @@ static int vpif_streamon(struct file *file, void *priv,
1070 enum v4l2_buf_type buftype) 1116 enum v4l2_buf_type buftype)
1071{ 1117{
1072 1118
1073 struct vpif_capture_config *config = vpif_dev->platform_data;
1074 struct vpif_fh *fh = priv; 1119 struct vpif_fh *fh = priv;
1075 struct channel_obj *ch = fh->channel; 1120 struct channel_obj *ch = fh->channel;
1076 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1121 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1077 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id]; 1122 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1078 struct vpif_params *vpif; 1123 struct vpif_params *vpif;
1079 unsigned long addr = 0;
1080 int ret = 0; 1124 int ret = 0;
1081 1125
1082 vpif_dbg(2, debug, "vpif_streamon\n"); 1126 vpif_dbg(2, debug, "vpif_streamon\n");
@@ -1122,95 +1166,13 @@ static int vpif_streamon(struct file *file, void *priv,
1122 return ret; 1166 return ret;
1123 } 1167 }
1124 1168
1125 /* Call videobuf_streamon to start streaming in videobuf */ 1169 /* Call vb2_streamon to start streaming in videobuf2 */
1126 ret = videobuf_streamon(&common->buffer_queue); 1170 ret = vb2_streamon(&common->buffer_queue, buftype);
1127 if (ret) { 1171 if (ret) {
1128 vpif_dbg(1, debug, "videobuf_streamon\n"); 1172 vpif_dbg(1, debug, "vb2_streamon\n");
1129 return ret; 1173 return ret;
1130 } 1174 }
1131 1175
1132 /* If buffer queue is empty, return error */
1133 if (list_empty(&common->dma_queue)) {
1134 vpif_dbg(1, debug, "buffer queue is empty\n");
1135 ret = -EIO;
1136 goto exit;
1137 }
1138
1139 /* Get the next frame from the buffer queue */
1140 common->cur_frm = list_entry(common->dma_queue.next,
1141 struct videobuf_buffer, queue);
1142 common->next_frm = common->cur_frm;
1143
1144 /* Remove buffer from the buffer queue */
1145 list_del(&common->cur_frm->queue);
1146 /* Mark state of the current frame to active */
1147 common->cur_frm->state = VIDEOBUF_ACTIVE;
1148 /* Initialize field_id and started member */
1149 ch->field_id = 0;
1150 common->started = 1;
1151
1152 if (V4L2_MEMORY_USERPTR == common->memory)
1153 addr = common->cur_frm->boff;
1154 else
1155 addr = videobuf_to_dma_contig(common->cur_frm);
1156
1157 /* Calculate the offset for Y and C data in the buffer */
1158 vpif_calculate_offsets(ch);
1159
1160 if ((vpif->std_info.frm_fmt &&
1161 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
1162 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
1163 (!vpif->std_info.frm_fmt &&
1164 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1165 vpif_dbg(1, debug, "conflict in field format and std format\n");
1166 ret = -EINVAL;
1167 goto exit;
1168 }
1169
1170 /* configure 1 or 2 channel mode */
1171 ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
1172
1173 if (ret < 0) {
1174 vpif_dbg(1, debug, "can't set vpif channel mode\n");
1175 goto exit;
1176 }
1177
1178 /* Call vpif_set_params function to set the parameters and addresses */
1179 ret = vpif_set_video_params(vpif, ch->channel_id);
1180
1181 if (ret < 0) {
1182 vpif_dbg(1, debug, "can't set video params\n");
1183 goto exit;
1184 }
1185
1186 common->started = ret;
1187 vpif_config_addr(ch, ret);
1188
1189 common->set_addr(addr + common->ytop_off,
1190 addr + common->ybtm_off,
1191 addr + common->ctop_off,
1192 addr + common->cbtm_off);
1193
1194 /**
1195 * Set interrupt for both the fields in VPIF Register enable channel in
1196 * VPIF register
1197 */
1198 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
1199 channel0_intr_assert();
1200 channel0_intr_enable(1);
1201 enable_channel0(1);
1202 }
1203 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
1204 (common->started == 2)) {
1205 channel1_intr_assert();
1206 channel1_intr_enable(1);
1207 enable_channel1(1);
1208 }
1209 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1210 return ret;
1211
1212exit:
1213 videobuf_streamoff(&common->buffer_queue);
1214 return ret; 1176 return ret;
1215} 1177}
1216 1178
@@ -1265,7 +1227,7 @@ static int vpif_streamoff(struct file *file, void *priv,
1265 if (ret && (ret != -ENOIOCTLCMD)) 1227 if (ret && (ret != -ENOIOCTLCMD))
1266 vpif_dbg(1, debug, "stream off failed in subdev\n"); 1228 vpif_dbg(1, debug, "stream off failed in subdev\n");
1267 1229
1268 return videobuf_streamoff(&common->buffer_queue); 1230 return vb2_streamoff(&common->buffer_queue, buftype);
1269} 1231}
1270 1232
1271/** 1233/**
@@ -1679,7 +1641,7 @@ static int vpif_querycap(struct file *file, void *priv,
1679 1641
1680 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; 1642 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1681 strlcpy(cap->driver, "vpif capture", sizeof(cap->driver)); 1643 strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
1682 strlcpy(cap->bus_info, "DM646x Platform", sizeof(cap->bus_info)); 1644 strlcpy(cap->bus_info, "VPIF Platform", sizeof(cap->bus_info));
1683 strlcpy(cap->card, config->card_name, sizeof(cap->card)); 1645 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1684 1646
1685 return 0; 1647 return 0;
@@ -2168,6 +2130,7 @@ static __init int vpif_probe(struct platform_device *pdev)
2168 struct video_device *vfd; 2130 struct video_device *vfd;
2169 struct resource *res; 2131 struct resource *res;
2170 int subdev_count; 2132 int subdev_count;
2133 size_t size;
2171 2134
2172 vpif_dev = &pdev->dev; 2135 vpif_dev = &pdev->dev;
2173 2136
@@ -2186,8 +2149,8 @@ static __init int vpif_probe(struct platform_device *pdev)
2186 k = 0; 2149 k = 0;
2187 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) { 2150 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
2188 for (i = res->start; i <= res->end; i++) { 2151 for (i = res->start; i <= res->end; i++) {
2189 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED, 2152 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
2190 "DM646x_Capture", 2153 "VPIF_Capture",
2191 (void *)(&vpif_obj.dev[k]->channel_id))) { 2154 (void *)(&vpif_obj.dev[k]->channel_id))) {
2192 err = -EBUSY; 2155 err = -EBUSY;
2193 i--; 2156 i--;
@@ -2216,12 +2179,29 @@ static __init int vpif_probe(struct platform_device *pdev)
2216 vfd->v4l2_dev = &vpif_obj.v4l2_dev; 2179 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2217 vfd->release = video_device_release; 2180 vfd->release = video_device_release;
2218 snprintf(vfd->name, sizeof(vfd->name), 2181 snprintf(vfd->name, sizeof(vfd->name),
2219 "DM646x_VPIFCapture_DRIVER_V%s", 2182 "VPIF_Capture_DRIVER_V%s",
2220 VPIF_CAPTURE_VERSION); 2183 VPIF_CAPTURE_VERSION);
2221 /* Set video_dev to the video device */ 2184 /* Set video_dev to the video device */
2222 ch->video_dev = vfd; 2185 ch->video_dev = vfd;
2223 } 2186 }
2224 2187
2188 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2189 if (res) {
2190 size = resource_size(res);
2191 /* The resources are divided into two equal memory and when we
2192 * have HD output we can add them together
2193 */
2194 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2195 ch = vpif_obj.dev[j];
2196 ch->channel_id = j;
2197 /* only enabled if second resource exists */
2198 config_params.video_limit[ch->channel_id] = 0;
2199 if (size)
2200 config_params.video_limit[ch->channel_id] =
2201 size/2;
2202 }
2203 }
2204
2225 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) { 2205 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2226 ch = vpif_obj.dev[j]; 2206 ch = vpif_obj.dev[j];
2227 ch->channel_id = j; 2207 ch->channel_id = j;
@@ -2275,8 +2255,7 @@ static __init int vpif_probe(struct platform_device *pdev)
2275 vpif_obj.sd[i]->grp_id = 1 << i; 2255 vpif_obj.sd[i]->grp_id = 1 << i;
2276 } 2256 }
2277 2257
2278 v4l2_info(&vpif_obj.v4l2_dev, 2258 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2279 "DM646x VPIF capture driver initialized\n");
2280 return 0; 2259 return 0;
2281 2260
2282probe_subdev_out: 2261probe_subdev_out:
@@ -2333,26 +2312,70 @@ static int vpif_remove(struct platform_device *device)
2333 return 0; 2312 return 0;
2334} 2313}
2335 2314
2315#ifdef CONFIG_PM
2336/** 2316/**
2337 * vpif_suspend: vpif device suspend 2317 * vpif_suspend: vpif device suspend
2338 *
2339 * TODO: Add suspend code here
2340 */ 2318 */
2341static int 2319static int vpif_suspend(struct device *dev)
2342vpif_suspend(struct device *dev)
2343{ 2320{
2344 return -1; 2321
2322 struct common_obj *common;
2323 struct channel_obj *ch;
2324 int i;
2325
2326 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2327 /* Get the pointer to the channel object */
2328 ch = vpif_obj.dev[i];
2329 common = &ch->common[VPIF_VIDEO_INDEX];
2330 mutex_lock(&common->lock);
2331 if (ch->usrs && common->io_usrs) {
2332 /* Disable channel */
2333 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2334 enable_channel0(0);
2335 channel0_intr_enable(0);
2336 }
2337 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2338 common->started == 2) {
2339 enable_channel1(0);
2340 channel1_intr_enable(0);
2341 }
2342 }
2343 mutex_unlock(&common->lock);
2344 }
2345
2346 return 0;
2345} 2347}
2346 2348
2347/** 2349/*
2348 * vpif_resume: vpif device suspend 2350 * vpif_resume: vpif device suspend
2349 *
2350 * TODO: Add resume code here
2351 */ 2351 */
2352static int 2352static int vpif_resume(struct device *dev)
2353vpif_resume(struct device *dev)
2354{ 2353{
2355 return -1; 2354 struct common_obj *common;
2355 struct channel_obj *ch;
2356 int i;
2357
2358 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2359 /* Get the pointer to the channel object */
2360 ch = vpif_obj.dev[i];
2361 common = &ch->common[VPIF_VIDEO_INDEX];
2362 mutex_lock(&common->lock);
2363 if (ch->usrs && common->io_usrs) {
2364 /* Disable channel */
2365 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2366 enable_channel0(1);
2367 channel0_intr_enable(1);
2368 }
2369 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2370 common->started == 2) {
2371 enable_channel1(1);
2372 channel1_intr_enable(1);
2373 }
2374 }
2375 mutex_unlock(&common->lock);
2376 }
2377
2378 return 0;
2356} 2379}
2357 2380
2358static const struct dev_pm_ops vpif_dev_pm_ops = { 2381static const struct dev_pm_ops vpif_dev_pm_ops = {
@@ -2360,11 +2383,16 @@ static const struct dev_pm_ops vpif_dev_pm_ops = {
2360 .resume = vpif_resume, 2383 .resume = vpif_resume,
2361}; 2384};
2362 2385
2386#define vpif_pm_ops (&vpif_dev_pm_ops)
2387#else
2388#define vpif_pm_ops NULL
2389#endif
2390
2363static __refdata struct platform_driver vpif_driver = { 2391static __refdata struct platform_driver vpif_driver = {
2364 .driver = { 2392 .driver = {
2365 .name = "vpif_capture", 2393 .name = "vpif_capture",
2366 .owner = THIS_MODULE, 2394 .owner = THIS_MODULE,
2367 .pm = &vpif_dev_pm_ops, 2395 .pm = vpif_pm_ops,
2368 }, 2396 },
2369 .probe = vpif_probe, 2397 .probe = vpif_probe,
2370 .remove = vpif_remove, 2398 .remove = vpif_remove,
diff --git a/drivers/media/video/davinci/vpif_capture.h b/drivers/media/video/davinci/vpif_capture.h
index a693d4ebda55..3511510f43ee 100644
--- a/drivers/media/video/davinci/vpif_capture.h
+++ b/drivers/media/video/davinci/vpif_capture.h
@@ -26,7 +26,7 @@
26#include <media/v4l2-common.h> 26#include <media/v4l2-common.h>
27#include <media/v4l2-device.h> 27#include <media/v4l2-device.h>
28#include <media/videobuf-core.h> 28#include <media/videobuf-core.h>
29#include <media/videobuf-dma-contig.h> 29#include <media/videobuf2-dma-contig.h>
30#include <media/davinci/vpif_types.h> 30#include <media/davinci/vpif_types.h>
31 31
32#include "vpif.h" 32#include "vpif.h"
@@ -60,11 +60,16 @@ struct video_obj {
60 u32 input_idx; 60 u32 input_idx;
61}; 61};
62 62
63struct vpif_cap_buffer {
64 struct vb2_buffer vb;
65 struct list_head list;
66};
67
63struct common_obj { 68struct common_obj {
64 /* Pointer pointing to current v4l2_buffer */ 69 /* Pointer pointing to current v4l2_buffer */
65 struct videobuf_buffer *cur_frm; 70 struct vpif_cap_buffer *cur_frm;
66 /* Pointer pointing to current v4l2_buffer */ 71 /* Pointer pointing to current v4l2_buffer */
67 struct videobuf_buffer *next_frm; 72 struct vpif_cap_buffer *next_frm;
68 /* 73 /*
69 * This field keeps track of type of buffer exchange mechanism 74 * This field keeps track of type of buffer exchange mechanism
70 * user has selected 75 * user has selected
@@ -73,7 +78,9 @@ struct common_obj {
73 /* Used to store pixel format */ 78 /* Used to store pixel format */
74 struct v4l2_format fmt; 79 struct v4l2_format fmt;
75 /* Buffer queue used in video-buf */ 80 /* Buffer queue used in video-buf */
76 struct videobuf_queue buffer_queue; 81 struct vb2_queue buffer_queue;
82 /* allocator-specific contexts for each plane */
83 struct vb2_alloc_ctx *alloc_ctx;
77 /* Queue of filled frames */ 84 /* Queue of filled frames */
78 struct list_head dma_queue; 85 struct list_head dma_queue;
79 /* Used in video-buf */ 86 /* Used in video-buf */
@@ -151,6 +158,7 @@ struct vpif_config_params {
151 u32 min_bufsize[VPIF_CAPTURE_NUM_CHANNELS]; 158 u32 min_bufsize[VPIF_CAPTURE_NUM_CHANNELS];
152 u32 channel_bufsize[VPIF_CAPTURE_NUM_CHANNELS]; 159 u32 channel_bufsize[VPIF_CAPTURE_NUM_CHANNELS];
153 u8 default_device[VPIF_CAPTURE_NUM_CHANNELS]; 160 u8 default_device[VPIF_CAPTURE_NUM_CHANNELS];
161 u32 video_limit[VPIF_CAPTURE_NUM_CHANNELS];
154 u8 max_device_type; 162 u8 max_device_type;
155}; 163};
156/* Struct which keeps track of the line numbers for the sliced vbi service */ 164/* Struct which keeps track of the line numbers for the sliced vbi service */
diff --git a/drivers/media/video/davinci/vpif_display.c b/drivers/media/video/davinci/vpif_display.c
index e6488ee7db18..e129c98921ad 100644
--- a/drivers/media/video/davinci/vpif_display.c
+++ b/drivers/media/video/davinci/vpif_display.c
@@ -46,7 +46,7 @@ MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
46MODULE_LICENSE("GPL"); 46MODULE_LICENSE("GPL");
47MODULE_VERSION(VPIF_DISPLAY_VERSION); 47MODULE_VERSION(VPIF_DISPLAY_VERSION);
48 48
49#define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50) 49#define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
50 50
51#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg) 51#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
52#define vpif_dbg(level, debug, fmt, arg...) \ 52#define vpif_dbg(level, debug, fmt, arg...) \
@@ -82,89 +82,38 @@ static struct vpif_config_params config_params = {
82 82
83static struct vpif_device vpif_obj = { {NULL} }; 83static struct vpif_device vpif_obj = { {NULL} };
84static struct device *vpif_dev; 84static struct device *vpif_dev;
85static void vpif_calculate_offsets(struct channel_obj *ch);
86static void vpif_config_addr(struct channel_obj *ch, int muxmode);
85 87
86/* 88/*
87 * vpif_uservirt_to_phys: This function is used to convert user 89 * buffer_prepare: This is the callback function called from vb2_qbuf()
88 * space virtual address to physical address.
89 */
90static u32 vpif_uservirt_to_phys(u32 virtp)
91{
92 struct mm_struct *mm = current->mm;
93 unsigned long physp = 0;
94 struct vm_area_struct *vma;
95
96 vma = find_vma(mm, virtp);
97
98 /* For kernel direct-mapped memory, take the easy way */
99 if (virtp >= PAGE_OFFSET) {
100 physp = virt_to_phys((void *)virtp);
101 } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
102 /* this will catch, kernel-allocated, mmaped-to-usermode addr */
103 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
104 } else {
105 /* otherwise, use get_user_pages() for general userland pages */
106 int res, nr_pages = 1;
107 struct page *pages;
108 down_read(&current->mm->mmap_sem);
109
110 res = get_user_pages(current, current->mm,
111 virtp, nr_pages, 1, 0, &pages, NULL);
112 up_read(&current->mm->mmap_sem);
113
114 if (res == nr_pages) {
115 physp = __pa(page_address(&pages[0]) +
116 (virtp & ~PAGE_MASK));
117 } else {
118 vpif_err("get_user_pages failed\n");
119 return 0;
120 }
121 }
122
123 return physp;
124}
125
126/*
127 * buffer_prepare: This is the callback function called from videobuf_qbuf()
128 * function the buffer is prepared and user space virtual address is converted 90 * function the buffer is prepared and user space virtual address is converted
129 * into physical address 91 * into physical address
130 */ 92 */
131static int vpif_buffer_prepare(struct videobuf_queue *q, 93static int vpif_buffer_prepare(struct vb2_buffer *vb)
132 struct videobuf_buffer *vb,
133 enum v4l2_field field)
134{ 94{
135 struct vpif_fh *fh = q->priv_data; 95 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
96 struct vb2_queue *q = vb->vb2_queue;
136 struct common_obj *common; 97 struct common_obj *common;
137 unsigned long addr; 98 unsigned long addr;
138 99
139 common = &fh->channel->common[VPIF_VIDEO_INDEX]; 100 common = &fh->channel->common[VPIF_VIDEO_INDEX];
140 if (VIDEOBUF_NEEDS_INIT == vb->state) { 101 if (vb->state != VB2_BUF_STATE_ACTIVE &&
141 vb->width = common->width; 102 vb->state != VB2_BUF_STATE_PREPARED) {
142 vb->height = common->height; 103 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
143 vb->size = vb->width * vb->height; 104 if (vb2_plane_vaddr(vb, 0) &&
144 vb->field = field; 105 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
145 }
146 vb->state = VIDEOBUF_PREPARED;
147
148 /* if user pointer memory mechanism is used, get the physical
149 * address of the buffer */
150 if (V4L2_MEMORY_USERPTR == common->memory) {
151 if (!vb->baddr) {
152 vpif_err("buffer_address is 0\n");
153 return -EINVAL;
154 }
155
156 vb->boff = vpif_uservirt_to_phys(vb->baddr);
157 if (!ISALIGNED(vb->boff))
158 goto buf_align_exit; 106 goto buf_align_exit;
159 }
160 107
161 addr = vb->boff; 108 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
162 if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) { 109 if (q->streaming &&
163 if (!ISALIGNED(addr + common->ytop_off) || 110 (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
164 !ISALIGNED(addr + common->ybtm_off) || 111 if (!ISALIGNED(addr + common->ytop_off) ||
165 !ISALIGNED(addr + common->ctop_off) || 112 !ISALIGNED(addr + common->ybtm_off) ||
166 !ISALIGNED(addr + common->cbtm_off)) 113 !ISALIGNED(addr + common->ctop_off) ||
167 goto buf_align_exit; 114 !ISALIGNED(addr + common->cbtm_off))
115 goto buf_align_exit;
116 }
168 } 117 }
169 return 0; 118 return 0;
170 119
@@ -174,86 +123,255 @@ buf_align_exit:
174} 123}
175 124
176/* 125/*
177 * vpif_buffer_setup: This function allocates memory for the buffers 126 * vpif_buffer_queue_setup: This function allocates memory for the buffers
178 */ 127 */
179static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count, 128static int vpif_buffer_queue_setup(struct vb2_queue *vq,
180 unsigned int *size) 129 const struct v4l2_format *fmt,
130 unsigned int *nbuffers, unsigned int *nplanes,
131 unsigned int sizes[], void *alloc_ctxs[])
181{ 132{
182 struct vpif_fh *fh = q->priv_data; 133 struct vpif_fh *fh = vb2_get_drv_priv(vq);
183 struct channel_obj *ch = fh->channel; 134 struct channel_obj *ch = fh->channel;
184 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 135 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
136 unsigned long size;
137
138 if (V4L2_MEMORY_MMAP == common->memory) {
139 size = config_params.channel_bufsize[ch->channel_id];
140 /*
141 * Checking if the buffer size exceeds the available buffer
142 * ycmux_mode = 0 means 1 channel mode HD and
143 * ycmux_mode = 1 means 2 channels mode SD
144 */
145 if (ch->vpifparams.std_info.ycmux_mode == 0) {
146 if (config_params.video_limit[ch->channel_id])
147 while (size * *nbuffers >
148 (config_params.video_limit[0]
149 + config_params.video_limit[1]))
150 (*nbuffers)--;
151 } else {
152 if (config_params.video_limit[ch->channel_id])
153 while (size * *nbuffers >
154 config_params.video_limit[ch->channel_id])
155 (*nbuffers)--;
156 }
157 } else {
158 size = common->fmt.fmt.pix.sizeimage;
159 }
185 160
186 if (V4L2_MEMORY_MMAP != common->memory) 161 if (*nbuffers < config_params.min_numbuffers)
187 return 0; 162 *nbuffers = config_params.min_numbuffers;
188
189 *size = config_params.channel_bufsize[ch->channel_id];
190 if (*count < config_params.min_numbuffers)
191 *count = config_params.min_numbuffers;
192 163
164 *nplanes = 1;
165 sizes[0] = size;
166 alloc_ctxs[0] = common->alloc_ctx;
193 return 0; 167 return 0;
194} 168}
195 169
196/* 170/*
197 * vpif_buffer_queue: This function adds the buffer to DMA queue 171 * vpif_buffer_queue: This function adds the buffer to DMA queue
198 */ 172 */
199static void vpif_buffer_queue(struct videobuf_queue *q, 173static void vpif_buffer_queue(struct vb2_buffer *vb)
200 struct videobuf_buffer *vb)
201{ 174{
202 struct vpif_fh *fh = q->priv_data; 175 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
176 struct vpif_disp_buffer *buf = container_of(vb,
177 struct vpif_disp_buffer, vb);
178 struct channel_obj *ch = fh->channel;
203 struct common_obj *common; 179 struct common_obj *common;
204 180
205 common = &fh->channel->common[VPIF_VIDEO_INDEX]; 181 common = &ch->common[VPIF_VIDEO_INDEX];
206 182
207 /* add the buffer to the DMA queue */ 183 /* add the buffer to the DMA queue */
208 list_add_tail(&vb->queue, &common->dma_queue); 184 list_add_tail(&buf->list, &common->dma_queue);
209 vb->state = VIDEOBUF_QUEUED;
210} 185}
211 186
212/* 187/*
213 * vpif_buffer_release: This function is called from the videobuf layer to 188 * vpif_buf_cleanup: This function is called from the videobuf2 layer to
214 * free memory allocated to the buffers 189 * free memory allocated to the buffers
215 */ 190 */
216static void vpif_buffer_release(struct videobuf_queue *q, 191static void vpif_buf_cleanup(struct vb2_buffer *vb)
217 struct videobuf_buffer *vb) 192{
193 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
194 struct vpif_disp_buffer *buf = container_of(vb,
195 struct vpif_disp_buffer, vb);
196 struct channel_obj *ch = fh->channel;
197 struct common_obj *common;
198 unsigned long flags;
199
200 common = &ch->common[VPIF_VIDEO_INDEX];
201
202 spin_lock_irqsave(&common->irqlock, flags);
203 if (vb->state == VB2_BUF_STATE_ACTIVE)
204 list_del_init(&buf->list);
205 spin_unlock_irqrestore(&common->irqlock, flags);
206}
207
208static void vpif_wait_prepare(struct vb2_queue *vq)
218{ 209{
219 struct vpif_fh *fh = q->priv_data; 210 struct vpif_fh *fh = vb2_get_drv_priv(vq);
220 struct channel_obj *ch = fh->channel; 211 struct channel_obj *ch = fh->channel;
221 struct common_obj *common; 212 struct common_obj *common;
222 unsigned int buf_size = 0;
223 213
224 common = &ch->common[VPIF_VIDEO_INDEX]; 214 common = &ch->common[VPIF_VIDEO_INDEX];
215 mutex_unlock(&common->lock);
216}
225 217
226 videobuf_dma_contig_free(q, vb); 218static void vpif_wait_finish(struct vb2_queue *vq)
227 vb->state = VIDEOBUF_NEEDS_INIT; 219{
220 struct vpif_fh *fh = vb2_get_drv_priv(vq);
221 struct channel_obj *ch = fh->channel;
222 struct common_obj *common;
228 223
229 if (V4L2_MEMORY_MMAP != common->memory) 224 common = &ch->common[VPIF_VIDEO_INDEX];
230 return; 225 mutex_lock(&common->lock);
226}
231 227
232 buf_size = config_params.channel_bufsize[ch->channel_id]; 228static int vpif_buffer_init(struct vb2_buffer *vb)
229{
230 struct vpif_disp_buffer *buf = container_of(vb,
231 struct vpif_disp_buffer, vb);
232
233 INIT_LIST_HEAD(&buf->list);
234
235 return 0;
233} 236}
234 237
235static struct videobuf_queue_ops video_qops = {
236 .buf_setup = vpif_buffer_setup,
237 .buf_prepare = vpif_buffer_prepare,
238 .buf_queue = vpif_buffer_queue,
239 .buf_release = vpif_buffer_release,
240};
241static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} }; 238static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
242 239
240static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
241{
242 struct vpif_display_config *vpif_config_data =
243 vpif_dev->platform_data;
244 struct vpif_fh *fh = vb2_get_drv_priv(vq);
245 struct channel_obj *ch = fh->channel;
246 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
247 struct vpif_params *vpif = &ch->vpifparams;
248 unsigned long addr = 0;
249 int ret;
250
251 /* If buffer queue is empty, return error */
252 if (list_empty(&common->dma_queue)) {
253 vpif_err("buffer queue is empty\n");
254 return -EIO;
255 }
256
257 /* Get the next frame from the buffer queue */
258 common->next_frm = common->cur_frm =
259 list_entry(common->dma_queue.next,
260 struct vpif_disp_buffer, list);
261
262 list_del(&common->cur_frm->list);
263 /* Mark state of the current frame to active */
264 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
265
266 /* Initialize field_id and started member */
267 ch->field_id = 0;
268 common->started = 1;
269 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
270 /* Calculate the offset for Y and C data in the buffer */
271 vpif_calculate_offsets(ch);
272
273 if ((ch->vpifparams.std_info.frm_fmt &&
274 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
275 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
276 || (!ch->vpifparams.std_info.frm_fmt
277 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
278 vpif_err("conflict in field format and std format\n");
279 return -EINVAL;
280 }
281
282 /* clock settings */
283 ret =
284 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
285 ch->vpifparams.std_info.hd_sd);
286 if (ret < 0) {
287 vpif_err("can't set clock\n");
288 return ret;
289 }
290
291 /* set the parameters and addresses */
292 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
293 if (ret < 0)
294 return ret;
295
296 common->started = ret;
297 vpif_config_addr(ch, ret);
298 common->set_addr((addr + common->ytop_off),
299 (addr + common->ybtm_off),
300 (addr + common->ctop_off),
301 (addr + common->cbtm_off));
302
303 /* Set interrupt for both the fields in VPIF
304 Register enable channel in VPIF register */
305 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
306 channel2_intr_assert();
307 channel2_intr_enable(1);
308 enable_channel2(1);
309 if (vpif_config_data->ch2_clip_en)
310 channel2_clipping_enable(1);
311 }
312
313 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
314 || (common->started == 2)) {
315 channel3_intr_assert();
316 channel3_intr_enable(1);
317 enable_channel3(1);
318 if (vpif_config_data->ch3_clip_en)
319 channel3_clipping_enable(1);
320 }
321 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
322
323 return 0;
324}
325
326/* abort streaming and wait for last buffer */
327static int vpif_stop_streaming(struct vb2_queue *vq)
328{
329 struct vpif_fh *fh = vb2_get_drv_priv(vq);
330 struct channel_obj *ch = fh->channel;
331 struct common_obj *common;
332
333 if (!vb2_is_streaming(vq))
334 return 0;
335
336 common = &ch->common[VPIF_VIDEO_INDEX];
337
338 /* release all active buffers */
339 while (!list_empty(&common->dma_queue)) {
340 common->next_frm = list_entry(common->dma_queue.next,
341 struct vpif_disp_buffer, list);
342 list_del(&common->next_frm->list);
343 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
344 }
345
346 return 0;
347}
348
349static struct vb2_ops video_qops = {
350 .queue_setup = vpif_buffer_queue_setup,
351 .wait_prepare = vpif_wait_prepare,
352 .wait_finish = vpif_wait_finish,
353 .buf_init = vpif_buffer_init,
354 .buf_prepare = vpif_buffer_prepare,
355 .start_streaming = vpif_start_streaming,
356 .stop_streaming = vpif_stop_streaming,
357 .buf_cleanup = vpif_buf_cleanup,
358 .buf_queue = vpif_buffer_queue,
359};
360
243static void process_progressive_mode(struct common_obj *common) 361static void process_progressive_mode(struct common_obj *common)
244{ 362{
245 unsigned long addr = 0; 363 unsigned long addr = 0;
246 364
247 /* Get the next buffer from buffer queue */ 365 /* Get the next buffer from buffer queue */
248 common->next_frm = list_entry(common->dma_queue.next, 366 common->next_frm = list_entry(common->dma_queue.next,
249 struct videobuf_buffer, queue); 367 struct vpif_disp_buffer, list);
250 /* Remove that buffer from the buffer queue */ 368 /* Remove that buffer from the buffer queue */
251 list_del(&common->next_frm->queue); 369 list_del(&common->next_frm->list);
252 /* Mark status of the buffer as active */ 370 /* Mark status of the buffer as active */
253 common->next_frm->state = VIDEOBUF_ACTIVE; 371 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
254 372
255 /* Set top and bottom field addrs in VPIF registers */ 373 /* Set top and bottom field addrs in VPIF registers */
256 addr = videobuf_to_dma_contig(common->next_frm); 374 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
257 common->set_addr(addr + common->ytop_off, 375 common->set_addr(addr + common->ytop_off,
258 addr + common->ybtm_off, 376 addr + common->ybtm_off,
259 addr + common->ctop_off, 377 addr + common->ctop_off,
@@ -271,11 +389,10 @@ static void process_interlaced_mode(int fid, struct common_obj *common)
271 /* one frame is displayed If next frame is 389 /* one frame is displayed If next frame is
272 * available, release cur_frm and move on */ 390 * available, release cur_frm and move on */
273 /* Copy frame display time */ 391 /* Copy frame display time */
274 do_gettimeofday(&common->cur_frm->ts); 392 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
275 /* Change status of the cur_frm */ 393 /* Change status of the cur_frm */
276 common->cur_frm->state = VIDEOBUF_DONE; 394 vb2_buffer_done(&common->cur_frm->vb,
277 /* unlock semaphore on cur_frm */ 395 VB2_BUF_STATE_DONE);
278 wake_up_interruptible(&common->cur_frm->done);
279 /* Make cur_frm pointing to next_frm */ 396 /* Make cur_frm pointing to next_frm */
280 common->cur_frm = common->next_frm; 397 common->cur_frm = common->next_frm;
281 398
@@ -307,6 +424,9 @@ static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
307 int channel_id = 0; 424 int channel_id = 0;
308 425
309 channel_id = *(int *)(dev_id); 426 channel_id = *(int *)(dev_id);
427 if (!vpif_intr_status(channel_id + 2))
428 return IRQ_NONE;
429
310 ch = dev->dev[channel_id]; 430 ch = dev->dev[channel_id];
311 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field; 431 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
312 for (i = 0; i < VPIF_NUMOBJECTS; i++) { 432 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
@@ -323,9 +443,10 @@ static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
323 if (!channel_first_int[i][channel_id]) { 443 if (!channel_first_int[i][channel_id]) {
324 /* Mark status of the cur_frm to 444 /* Mark status of the cur_frm to
325 * done and unlock semaphore on it */ 445 * done and unlock semaphore on it */
326 do_gettimeofday(&common->cur_frm->ts); 446 do_gettimeofday(&common->cur_frm->vb.
327 common->cur_frm->state = VIDEOBUF_DONE; 447 v4l2_buf.timestamp);
328 wake_up_interruptible(&common->cur_frm->done); 448 vb2_buffer_done(&common->cur_frm->vb,
449 VB2_BUF_STATE_DONE);
329 /* Make cur_frm pointing to next_frm */ 450 /* Make cur_frm pointing to next_frm */
330 common->cur_frm = common->next_frm; 451 common->cur_frm = common->next_frm;
331 } 452 }
@@ -443,10 +564,7 @@ static void vpif_calculate_offsets(struct channel_obj *ch)
443 vid_ch->buf_field = common->fmt.fmt.pix.field; 564 vid_ch->buf_field = common->fmt.fmt.pix.field;
444 } 565 }
445 566
446 if (V4L2_MEMORY_USERPTR == common->memory) 567 sizeimage = common->fmt.fmt.pix.sizeimage;
447 sizeimage = common->fmt.fmt.pix.sizeimage;
448 else
449 sizeimage = config_params.channel_bufsize[ch->channel_id];
450 568
451 hpitch = common->fmt.fmt.pix.bytesperline; 569 hpitch = common->fmt.fmt.pix.bytesperline;
452 vpitch = sizeimage / (hpitch * 2); 570 vpitch = sizeimage / (hpitch * 2);
@@ -523,10 +641,7 @@ static int vpif_check_format(struct channel_obj *ch,
523 if (pixfmt->bytesperline <= 0) 641 if (pixfmt->bytesperline <= 0)
524 goto invalid_pitch_exit; 642 goto invalid_pitch_exit;
525 643
526 if (V4L2_MEMORY_USERPTR == common->memory) 644 sizeimage = pixfmt->sizeimage;
527 sizeimage = pixfmt->sizeimage;
528 else
529 sizeimage = config_params.channel_bufsize[ch->channel_id];
530 645
531 if (vpif_update_resolution(ch)) 646 if (vpif_update_resolution(ch))
532 return -EINVAL; 647 return -EINVAL;
@@ -583,7 +698,7 @@ static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
583 698
584 vpif_dbg(2, debug, "vpif_mmap\n"); 699 vpif_dbg(2, debug, "vpif_mmap\n");
585 700
586 return videobuf_mmap_mapper(&common->buffer_queue, vma); 701 return vb2_mmap(&common->buffer_queue, vma);
587} 702}
588 703
589/* 704/*
@@ -596,7 +711,7 @@ static unsigned int vpif_poll(struct file *filep, poll_table *wait)
596 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 711 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
597 712
598 if (common->started) 713 if (common->started)
599 return videobuf_poll_stream(filep, &common->buffer_queue, wait); 714 return vb2_poll(&common->buffer_queue, filep, wait);
600 715
601 return 0; 716 return 0;
602} 717}
@@ -665,9 +780,11 @@ static int vpif_release(struct file *filep)
665 channel3_intr_enable(0); 780 channel3_intr_enable(0);
666 } 781 }
667 common->started = 0; 782 common->started = 0;
783
668 /* Free buffers allocated */ 784 /* Free buffers allocated */
669 videobuf_queue_cancel(&common->buffer_queue); 785 vb2_queue_release(&common->buffer_queue);
670 videobuf_mmap_free(&common->buffer_queue); 786 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
787
671 common->numbuffers = 788 common->numbuffers =
672 config_params.numbuffers[ch->channel_id]; 789 config_params.numbuffers[ch->channel_id];
673 } 790 }
@@ -806,6 +923,7 @@ static int vpif_reqbufs(struct file *file, void *priv,
806 struct channel_obj *ch = fh->channel; 923 struct channel_obj *ch = fh->channel;
807 struct common_obj *common; 924 struct common_obj *common;
808 enum v4l2_field field; 925 enum v4l2_field field;
926 struct vb2_queue *q;
809 u8 index = 0; 927 u8 index = 0;
810 928
811 /* This file handle has not initialized the channel, 929 /* This file handle has not initialized the channel,
@@ -825,9 +943,8 @@ static int vpif_reqbufs(struct file *file, void *priv,
825 943
826 common = &ch->common[index]; 944 common = &ch->common[index];
827 945
828 if (common->fmt.type != reqbuf->type) 946 if (common->fmt.type != reqbuf->type || !vpif_dev)
829 return -EINVAL; 947 return -EINVAL;
830
831 if (0 != common->io_usrs) 948 if (0 != common->io_usrs)
832 return -EBUSY; 949 return -EBUSY;
833 950
@@ -839,14 +956,21 @@ static int vpif_reqbufs(struct file *file, void *priv,
839 } else { 956 } else {
840 field = V4L2_VBI_INTERLACED; 957 field = V4L2_VBI_INTERLACED;
841 } 958 }
959 /* Initialize videobuf2 queue as per the buffer type */
960 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
961 if (!common->alloc_ctx) {
962 vpif_err("Failed to get the context\n");
963 return -EINVAL;
964 }
965 q = &common->buffer_queue;
966 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
967 q->io_modes = VB2_MMAP | VB2_USERPTR;
968 q->drv_priv = fh;
969 q->ops = &video_qops;
970 q->mem_ops = &vb2_dma_contig_memops;
971 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
842 972
843 /* Initialize videobuf queue as per the buffer type */ 973 vb2_queue_init(q);
844 videobuf_queue_dma_contig_init(&common->buffer_queue,
845 &video_qops, NULL,
846 &common->irqlock,
847 reqbuf->type, field,
848 sizeof(struct videobuf_buffer), fh,
849 &common->lock);
850 974
851 /* Set io allowed member of file handle to TRUE */ 975 /* Set io allowed member of file handle to TRUE */
852 fh->io_allowed[index] = 1; 976 fh->io_allowed[index] = 1;
@@ -855,9 +979,8 @@ static int vpif_reqbufs(struct file *file, void *priv,
855 /* Store type of memory requested in channel object */ 979 /* Store type of memory requested in channel object */
856 common->memory = reqbuf->memory; 980 common->memory = reqbuf->memory;
857 INIT_LIST_HEAD(&common->dma_queue); 981 INIT_LIST_HEAD(&common->dma_queue);
858
859 /* Allocate buffers */ 982 /* Allocate buffers */
860 return videobuf_reqbufs(&common->buffer_queue, reqbuf); 983 return vb2_reqbufs(&common->buffer_queue, reqbuf);
861} 984}
862 985
863static int vpif_querybuf(struct file *file, void *priv, 986static int vpif_querybuf(struct file *file, void *priv,
@@ -870,22 +993,25 @@ static int vpif_querybuf(struct file *file, void *priv,
870 if (common->fmt.type != tbuf->type) 993 if (common->fmt.type != tbuf->type)
871 return -EINVAL; 994 return -EINVAL;
872 995
873 return videobuf_querybuf(&common->buffer_queue, tbuf); 996 return vb2_querybuf(&common->buffer_queue, tbuf);
874} 997}
875 998
876static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf) 999static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
877{ 1000{
1001 struct vpif_fh *fh = NULL;
1002 struct channel_obj *ch = NULL;
1003 struct common_obj *common = NULL;
878 1004
879 struct vpif_fh *fh = priv; 1005 if (!buf || !priv)
880 struct channel_obj *ch = fh->channel; 1006 return -EINVAL;
881 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1007
882 struct v4l2_buffer tbuf = *buf; 1008 fh = priv;
883 struct videobuf_buffer *buf1; 1009 ch = fh->channel;
884 unsigned long addr = 0; 1010 if (!ch)
885 unsigned long flags; 1011 return -EINVAL;
886 int ret = 0;
887 1012
888 if (common->fmt.type != tbuf.type) 1013 common = &(ch->common[VPIF_VIDEO_INDEX]);
1014 if (common->fmt.type != buf->type)
889 return -EINVAL; 1015 return -EINVAL;
890 1016
891 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) { 1017 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
@@ -893,73 +1019,7 @@ static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
893 return -EACCES; 1019 return -EACCES;
894 } 1020 }
895 1021
896 if (!(list_empty(&common->dma_queue)) || 1022 return vb2_qbuf(&common->buffer_queue, buf);
897 (common->cur_frm != common->next_frm) ||
898 !(common->started) ||
899 (common->started && (0 == ch->field_id)))
900 return videobuf_qbuf(&common->buffer_queue, buf);
901
902 /* bufferqueue is empty store buffer address in VPIF registers */
903 mutex_lock(&common->buffer_queue.vb_lock);
904 buf1 = common->buffer_queue.bufs[tbuf.index];
905 if (buf1->memory != tbuf.memory) {
906 vpif_err("invalid buffer type\n");
907 goto qbuf_exit;
908 }
909
910 if ((buf1->state == VIDEOBUF_QUEUED) ||
911 (buf1->state == VIDEOBUF_ACTIVE)) {
912 vpif_err("invalid state\n");
913 goto qbuf_exit;
914 }
915
916 switch (buf1->memory) {
917 case V4L2_MEMORY_MMAP:
918 if (buf1->baddr == 0)
919 goto qbuf_exit;
920 break;
921
922 case V4L2_MEMORY_USERPTR:
923 if (tbuf.length < buf1->bsize)
924 goto qbuf_exit;
925
926 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
927 && (buf1->baddr != tbuf.m.userptr)) {
928 vpif_buffer_release(&common->buffer_queue, buf1);
929 buf1->baddr = tbuf.m.userptr;
930 }
931 break;
932
933 default:
934 goto qbuf_exit;
935 }
936
937 local_irq_save(flags);
938 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
939 common->buffer_queue.field);
940 if (ret < 0) {
941 local_irq_restore(flags);
942 goto qbuf_exit;
943 }
944
945 buf1->state = VIDEOBUF_ACTIVE;
946 addr = buf1->boff;
947 common->next_frm = buf1;
948 if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
949 common->set_addr((addr + common->ytop_off),
950 (addr + common->ybtm_off),
951 (addr + common->ctop_off),
952 (addr + common->cbtm_off));
953 }
954
955 local_irq_restore(flags);
956 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
957 mutex_unlock(&common->buffer_queue.vb_lock);
958 return 0;
959
960qbuf_exit:
961 mutex_unlock(&common->buffer_queue.vb_lock);
962 return -EINVAL;
963} 1023}
964 1024
965static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id) 1025static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
@@ -969,7 +1029,7 @@ static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
969 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1029 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
970 int ret = 0; 1030 int ret = 0;
971 1031
972 if (!(*std_id & DM646X_V4L2_STD)) 1032 if (!(*std_id & VPIF_V4L2_STD))
973 return -EINVAL; 1033 return -EINVAL;
974 1034
975 if (common->started) { 1035 if (common->started) {
@@ -1026,7 +1086,7 @@ static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1026 struct channel_obj *ch = fh->channel; 1086 struct channel_obj *ch = fh->channel;
1027 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1087 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1028 1088
1029 return videobuf_dqbuf(&common->buffer_queue, p, 1089 return vb2_dqbuf(&common->buffer_queue, p,
1030 (file->f_flags & O_NONBLOCK)); 1090 (file->f_flags & O_NONBLOCK));
1031} 1091}
1032 1092
@@ -1037,10 +1097,6 @@ static int vpif_streamon(struct file *file, void *priv,
1037 struct channel_obj *ch = fh->channel; 1097 struct channel_obj *ch = fh->channel;
1038 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1098 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1039 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id]; 1099 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1040 struct vpif_params *vpif = &ch->vpifparams;
1041 struct vpif_display_config *vpif_config_data =
1042 vpif_dev->platform_data;
1043 unsigned long addr = 0;
1044 int ret = 0; 1100 int ret = 0;
1045 1101
1046 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1102 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
@@ -1072,82 +1128,13 @@ static int vpif_streamon(struct file *file, void *priv,
1072 if (ret < 0) 1128 if (ret < 0)
1073 return ret; 1129 return ret;
1074 1130
1075 /* Call videobuf_streamon to start streaming in videobuf */ 1131 /* Call vb2_streamon to start streaming in videobuf2 */
1076 ret = videobuf_streamon(&common->buffer_queue); 1132 ret = vb2_streamon(&common->buffer_queue, buftype);
1077 if (ret < 0) { 1133 if (ret < 0) {
1078 vpif_err("videobuf_streamon\n"); 1134 vpif_err("vb2_streamon\n");
1079 return ret; 1135 return ret;
1080 } 1136 }
1081 1137
1082 /* If buffer queue is empty, return error */
1083 if (list_empty(&common->dma_queue)) {
1084 vpif_err("buffer queue is empty\n");
1085 return -EIO;
1086 }
1087
1088 /* Get the next frame from the buffer queue */
1089 common->next_frm = common->cur_frm =
1090 list_entry(common->dma_queue.next,
1091 struct videobuf_buffer, queue);
1092
1093 list_del(&common->cur_frm->queue);
1094 /* Mark state of the current frame to active */
1095 common->cur_frm->state = VIDEOBUF_ACTIVE;
1096
1097 /* Initialize field_id and started member */
1098 ch->field_id = 0;
1099 common->started = 1;
1100 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1101 addr = common->cur_frm->boff;
1102 /* Calculate the offset for Y and C data in the buffer */
1103 vpif_calculate_offsets(ch);
1104
1105 if ((ch->vpifparams.std_info.frm_fmt &&
1106 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
1107 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
1108 || (!ch->vpifparams.std_info.frm_fmt
1109 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1110 vpif_err("conflict in field format and std format\n");
1111 return -EINVAL;
1112 }
1113
1114 /* clock settings */
1115 ret =
1116 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1117 ch->vpifparams.std_info.hd_sd);
1118 if (ret < 0) {
1119 vpif_err("can't set clock\n");
1120 return ret;
1121 }
1122
1123 /* set the parameters and addresses */
1124 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1125 if (ret < 0)
1126 return ret;
1127
1128 common->started = ret;
1129 vpif_config_addr(ch, ret);
1130 common->set_addr((addr + common->ytop_off),
1131 (addr + common->ybtm_off),
1132 (addr + common->ctop_off),
1133 (addr + common->cbtm_off));
1134
1135 /* Set interrupt for both the fields in VPIF
1136 Register enable channel in VPIF register */
1137 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1138 channel2_intr_assert();
1139 channel2_intr_enable(1);
1140 enable_channel2(1);
1141 }
1142
1143 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1144 || (common->started == 2)) {
1145 channel3_intr_assert();
1146 channel3_intr_enable(1);
1147 enable_channel3(1);
1148 }
1149 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1150 }
1151 return ret; 1138 return ret;
1152} 1139}
1153 1140
@@ -1157,6 +1144,8 @@ static int vpif_streamoff(struct file *file, void *priv,
1157 struct vpif_fh *fh = priv; 1144 struct vpif_fh *fh = priv;
1158 struct channel_obj *ch = fh->channel; 1145 struct channel_obj *ch = fh->channel;
1159 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1146 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1147 struct vpif_display_config *vpif_config_data =
1148 vpif_dev->platform_data;
1160 1149
1161 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1150 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1162 vpif_err("buffer type not supported\n"); 1151 vpif_err("buffer type not supported\n");
@@ -1176,18 +1165,22 @@ static int vpif_streamoff(struct file *file, void *priv,
1176 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1165 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1177 /* disable channel */ 1166 /* disable channel */
1178 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) { 1167 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1168 if (vpif_config_data->ch2_clip_en)
1169 channel2_clipping_enable(0);
1179 enable_channel2(0); 1170 enable_channel2(0);
1180 channel2_intr_enable(0); 1171 channel2_intr_enable(0);
1181 } 1172 }
1182 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) || 1173 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1183 (2 == common->started)) { 1174 (2 == common->started)) {
1175 if (vpif_config_data->ch3_clip_en)
1176 channel3_clipping_enable(0);
1184 enable_channel3(0); 1177 enable_channel3(0);
1185 channel3_intr_enable(0); 1178 channel3_intr_enable(0);
1186 } 1179 }
1187 } 1180 }
1188 1181
1189 common->started = 0; 1182 common->started = 0;
1190 return videobuf_streamoff(&common->buffer_queue); 1183 return vb2_streamoff(&common->buffer_queue, buftype);
1191} 1184}
1192 1185
1193static int vpif_cropcap(struct file *file, void *priv, 1186static int vpif_cropcap(struct file *file, void *priv,
@@ -1220,7 +1213,7 @@ static int vpif_enum_output(struct file *file, void *fh,
1220 1213
1221 strcpy(output->name, config->output[output->index]); 1214 strcpy(output->name, config->output[output->index]);
1222 output->type = V4L2_OUTPUT_TYPE_ANALOG; 1215 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1223 output->std = DM646X_V4L2_STD; 1216 output->std = VPIF_V4L2_STD;
1224 1217
1225 return 0; 1218 return 0;
1226} 1219}
@@ -1605,7 +1598,7 @@ static struct video_device vpif_video_template = {
1605 .name = "vpif", 1598 .name = "vpif",
1606 .fops = &vpif_fops, 1599 .fops = &vpif_fops,
1607 .ioctl_ops = &vpif_ioctl_ops, 1600 .ioctl_ops = &vpif_ioctl_ops,
1608 .tvnorms = DM646X_V4L2_STD, 1601 .tvnorms = VPIF_V4L2_STD,
1609 .current_norm = V4L2_STD_625_50, 1602 .current_norm = V4L2_STD_625_50,
1610 1603
1611}; 1604};
@@ -1687,9 +1680,9 @@ static __init int vpif_probe(struct platform_device *pdev)
1687 struct video_device *vfd; 1680 struct video_device *vfd;
1688 struct resource *res; 1681 struct resource *res;
1689 int subdev_count; 1682 int subdev_count;
1683 size_t size;
1690 1684
1691 vpif_dev = &pdev->dev; 1685 vpif_dev = &pdev->dev;
1692
1693 err = initialize_vpif(); 1686 err = initialize_vpif();
1694 1687
1695 if (err) { 1688 if (err) {
@@ -1706,8 +1699,8 @@ static __init int vpif_probe(struct platform_device *pdev)
1706 k = 0; 1699 k = 0;
1707 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) { 1700 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1708 for (i = res->start; i <= res->end; i++) { 1701 for (i = res->start; i <= res->end; i++) {
1709 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED, 1702 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
1710 "DM646x_Display", 1703 "VPIF_Display",
1711 (void *)(&vpif_obj.dev[k]->channel_id))) { 1704 (void *)(&vpif_obj.dev[k]->channel_id))) {
1712 err = -EBUSY; 1705 err = -EBUSY;
1713 goto vpif_int_err; 1706 goto vpif_int_err;
@@ -1737,13 +1730,31 @@ static __init int vpif_probe(struct platform_device *pdev)
1737 vfd->v4l2_dev = &vpif_obj.v4l2_dev; 1730 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1738 vfd->release = video_device_release; 1731 vfd->release = video_device_release;
1739 snprintf(vfd->name, sizeof(vfd->name), 1732 snprintf(vfd->name, sizeof(vfd->name),
1740 "DM646x_VPIFDisplay_DRIVER_V%s", 1733 "VPIF_Display_DRIVER_V%s",
1741 VPIF_DISPLAY_VERSION); 1734 VPIF_DISPLAY_VERSION);
1742 1735
1743 /* Set video_dev to the video device */ 1736 /* Set video_dev to the video device */
1744 ch->video_dev = vfd; 1737 ch->video_dev = vfd;
1745 } 1738 }
1746 1739
1740 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1741 if (res) {
1742 size = resource_size(res);
1743 /* The resources are divided into two equal memory and when
1744 * we have HD output we can add them together
1745 */
1746 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1747 ch = vpif_obj.dev[j];
1748 ch->channel_id = j;
1749
1750 /* only enabled if second resource exists */
1751 config_params.video_limit[ch->channel_id] = 0;
1752 if (size)
1753 config_params.video_limit[ch->channel_id] =
1754 size/2;
1755 }
1756 }
1757
1747 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) { 1758 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1748 ch = vpif_obj.dev[j]; 1759 ch = vpif_obj.dev[j];
1749 /* Initialize field of the channel objects */ 1760 /* Initialize field of the channel objects */
@@ -1823,7 +1834,7 @@ static __init int vpif_probe(struct platform_device *pdev)
1823 } 1834 }
1824 1835
1825 v4l2_info(&vpif_obj.v4l2_dev, 1836 v4l2_info(&vpif_obj.v4l2_dev,
1826 "DM646x VPIF display driver initialized\n"); 1837 " VPIF display driver initialized\n");
1827 return 0; 1838 return 0;
1828 1839
1829probe_subdev_out: 1840probe_subdev_out:
@@ -1871,10 +1882,81 @@ static int vpif_remove(struct platform_device *device)
1871 return 0; 1882 return 0;
1872} 1883}
1873 1884
1885#ifdef CONFIG_PM
1886static int vpif_suspend(struct device *dev)
1887{
1888 struct common_obj *common;
1889 struct channel_obj *ch;
1890 int i;
1891
1892 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1893 /* Get the pointer to the channel object */
1894 ch = vpif_obj.dev[i];
1895 common = &ch->common[VPIF_VIDEO_INDEX];
1896 mutex_lock(&common->lock);
1897 if (atomic_read(&ch->usrs) && common->io_usrs) {
1898 /* Disable channel */
1899 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1900 enable_channel2(0);
1901 channel2_intr_enable(0);
1902 }
1903 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1904 common->started == 2) {
1905 enable_channel3(0);
1906 channel3_intr_enable(0);
1907 }
1908 }
1909 mutex_unlock(&common->lock);
1910 }
1911
1912 return 0;
1913}
1914
1915static int vpif_resume(struct device *dev)
1916{
1917
1918 struct common_obj *common;
1919 struct channel_obj *ch;
1920 int i;
1921
1922 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1923 /* Get the pointer to the channel object */
1924 ch = vpif_obj.dev[i];
1925 common = &ch->common[VPIF_VIDEO_INDEX];
1926 mutex_lock(&common->lock);
1927 if (atomic_read(&ch->usrs) && common->io_usrs) {
1928 /* Enable channel */
1929 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1930 enable_channel2(1);
1931 channel2_intr_enable(1);
1932 }
1933 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1934 common->started == 2) {
1935 enable_channel3(1);
1936 channel3_intr_enable(1);
1937 }
1938 }
1939 mutex_unlock(&common->lock);
1940 }
1941
1942 return 0;
1943}
1944
1945static const struct dev_pm_ops vpif_pm = {
1946 .suspend = vpif_suspend,
1947 .resume = vpif_resume,
1948};
1949
1950#define vpif_pm_ops (&vpif_pm)
1951#else
1952#define vpif_pm_ops NULL
1953#endif
1954
1874static __refdata struct platform_driver vpif_driver = { 1955static __refdata struct platform_driver vpif_driver = {
1875 .driver = { 1956 .driver = {
1876 .name = "vpif_display", 1957 .name = "vpif_display",
1877 .owner = THIS_MODULE, 1958 .owner = THIS_MODULE,
1959 .pm = vpif_pm_ops,
1878 }, 1960 },
1879 .probe = vpif_probe, 1961 .probe = vpif_probe,
1880 .remove = vpif_remove, 1962 .remove = vpif_remove,
diff --git a/drivers/media/video/davinci/vpif_display.h b/drivers/media/video/davinci/vpif_display.h
index 56879d1a0684..8967ffb44058 100644
--- a/drivers/media/video/davinci/vpif_display.h
+++ b/drivers/media/video/davinci/vpif_display.h
@@ -1,5 +1,5 @@
1/* 1/*
2 * DM646x display header file 2 * VPIF display header file
3 * 3 *
4 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ 4 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
5 * 5 *
@@ -21,7 +21,7 @@
21#include <media/v4l2-common.h> 21#include <media/v4l2-common.h>
22#include <media/v4l2-device.h> 22#include <media/v4l2-device.h>
23#include <media/videobuf-core.h> 23#include <media/videobuf-core.h>
24#include <media/videobuf-dma-contig.h> 24#include <media/videobuf2-dma-contig.h>
25#include <media/davinci/vpif_types.h> 25#include <media/davinci/vpif_types.h>
26 26
27#include "vpif.h" 27#include "vpif.h"
@@ -73,21 +73,29 @@ struct vbi_obj {
73 * vbi data */ 73 * vbi data */
74}; 74};
75 75
76struct vpif_disp_buffer {
77 struct vb2_buffer vb;
78 struct list_head list;
79};
80
76struct common_obj { 81struct common_obj {
77 /* Buffer specific parameters */ 82 /* Buffer specific parameters */
78 u8 *fbuffers[VIDEO_MAX_FRAME]; /* List of buffer pointers for 83 u8 *fbuffers[VIDEO_MAX_FRAME]; /* List of buffer pointers for
79 * storing frames */ 84 * storing frames */
80 u32 numbuffers; /* number of buffers */ 85 u32 numbuffers; /* number of buffers */
81 struct videobuf_buffer *cur_frm; /* Pointer pointing to current 86 struct vpif_disp_buffer *cur_frm; /* Pointer pointing to current
82 * videobuf_buffer */ 87 * vb2_buffer */
83 struct videobuf_buffer *next_frm; /* Pointer pointing to next 88 struct vpif_disp_buffer *next_frm; /* Pointer pointing to next
84 * videobuf_buffer */ 89 * vb2_buffer */
85 enum v4l2_memory memory; /* This field keeps track of 90 enum v4l2_memory memory; /* This field keeps track of
86 * type of buffer exchange 91 * type of buffer exchange
87 * method user has selected */ 92 * method user has selected */
88 struct v4l2_format fmt; /* Used to store the format */ 93 struct v4l2_format fmt; /* Used to store the format */
89 struct videobuf_queue buffer_queue; /* Buffer queue used in 94 struct vb2_queue buffer_queue; /* Buffer queue used in
90 * video-buf */ 95 * video-buf */
96 /* allocator-specific contexts for each plane */
97 struct vb2_alloc_ctx *alloc_ctx;
98
91 struct list_head dma_queue; /* Queue of filled frames */ 99 struct list_head dma_queue; /* Queue of filled frames */
92 spinlock_t irqlock; /* Used in video-buf */ 100 spinlock_t irqlock; /* Used in video-buf */
93 101
@@ -158,6 +166,7 @@ struct vpif_config_params {
158 u32 min_bufsize[VPIF_DISPLAY_NUM_CHANNELS]; 166 u32 min_bufsize[VPIF_DISPLAY_NUM_CHANNELS];
159 u32 channel_bufsize[VPIF_DISPLAY_NUM_CHANNELS]; 167 u32 channel_bufsize[VPIF_DISPLAY_NUM_CHANNELS];
160 u8 numbuffers[VPIF_DISPLAY_NUM_CHANNELS]; 168 u8 numbuffers[VPIF_DISPLAY_NUM_CHANNELS];
169 u32 video_limit[VPIF_DISPLAY_NUM_CHANNELS];
161 u8 min_numbuffers; 170 u8 min_numbuffers;
162}; 171};
163 172
diff --git a/drivers/media/video/gspca/benq.c b/drivers/media/video/gspca/benq.c
index 9769f17915c0..352f32190e68 100644
--- a/drivers/media/video/gspca/benq.c
+++ b/drivers/media/video/gspca/benq.c
@@ -33,10 +33,6 @@ struct sd {
33 struct gspca_dev gspca_dev; /* !! must be the first item */ 33 struct gspca_dev gspca_dev; /* !! must be the first item */
34}; 34};
35 35
36/* V4L2 controls supported by the driver */
37static const struct ctrl sd_ctrls[] = {
38};
39
40static const struct v4l2_pix_format vga_mode[] = { 36static const struct v4l2_pix_format vga_mode[] = {
41 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 37 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
42 .bytesperline = 320, 38 .bytesperline = 320,
@@ -256,8 +252,6 @@ static void sd_isoc_irq(struct urb *urb)
256/* sub-driver description */ 252/* sub-driver description */
257static const struct sd_desc sd_desc = { 253static const struct sd_desc sd_desc = {
258 .name = MODULE_NAME, 254 .name = MODULE_NAME,
259 .ctrls = sd_ctrls,
260 .nctrls = ARRAY_SIZE(sd_ctrls),
261 .config = sd_config, 255 .config = sd_config,
262 .init = sd_init, 256 .init = sd_init,
263 .start = sd_start, 257 .start = sd_start,
@@ -288,6 +282,7 @@ static struct usb_driver sd_driver = {
288#ifdef CONFIG_PM 282#ifdef CONFIG_PM
289 .suspend = gspca_suspend, 283 .suspend = gspca_suspend,
290 .resume = gspca_resume, 284 .resume = gspca_resume,
285 .reset_resume = gspca_resume,
291#endif 286#endif
292}; 287};
293 288
diff --git a/drivers/media/video/gspca/conex.c b/drivers/media/video/gspca/conex.c
index f39fee0fd10f..c9052f20435e 100644
--- a/drivers/media/video/gspca/conex.c
+++ b/drivers/media/video/gspca/conex.c
@@ -31,74 +31,18 @@ MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
31MODULE_DESCRIPTION("GSPCA USB Conexant Camera Driver"); 31MODULE_DESCRIPTION("GSPCA USB Conexant Camera Driver");
32MODULE_LICENSE("GPL"); 32MODULE_LICENSE("GPL");
33 33
34#define QUALITY 50
35
34/* specific webcam descriptor */ 36/* specific webcam descriptor */
35struct sd { 37struct sd {
36 struct gspca_dev gspca_dev; /* !! must be the first item */ 38 struct gspca_dev gspca_dev; /* !! must be the first item */
37 39 struct v4l2_ctrl *brightness;
38 unsigned char brightness; 40 struct v4l2_ctrl *contrast;
39 unsigned char contrast; 41 struct v4l2_ctrl *sat;
40 unsigned char colors;
41 u8 quality;
42#define QUALITY_MIN 30
43#define QUALITY_MAX 60
44#define QUALITY_DEF 40
45 42
46 u8 jpeg_hdr[JPEG_HDR_SZ]; 43 u8 jpeg_hdr[JPEG_HDR_SZ];
47}; 44};
48 45
49/* V4L2 controls supported by the driver */
50static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
51static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
52static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
53static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
54static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
55static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
56
57static const struct ctrl sd_ctrls[] = {
58 {
59 {
60 .id = V4L2_CID_BRIGHTNESS,
61 .type = V4L2_CTRL_TYPE_INTEGER,
62 .name = "Brightness",
63 .minimum = 0,
64 .maximum = 255,
65 .step = 1,
66#define BRIGHTNESS_DEF 0xd4
67 .default_value = BRIGHTNESS_DEF,
68 },
69 .set = sd_setbrightness,
70 .get = sd_getbrightness,
71 },
72 {
73 {
74 .id = V4L2_CID_CONTRAST,
75 .type = V4L2_CTRL_TYPE_INTEGER,
76 .name = "Contrast",
77 .minimum = 0x0a,
78 .maximum = 0x1f,
79 .step = 1,
80#define CONTRAST_DEF 0x0c
81 .default_value = CONTRAST_DEF,
82 },
83 .set = sd_setcontrast,
84 .get = sd_getcontrast,
85 },
86 {
87 {
88 .id = V4L2_CID_SATURATION,
89 .type = V4L2_CTRL_TYPE_INTEGER,
90 .name = "Color",
91 .minimum = 0,
92 .maximum = 7,
93 .step = 1,
94#define COLOR_DEF 3
95 .default_value = COLOR_DEF,
96 },
97 .set = sd_setcolors,
98 .get = sd_getcolors,
99 },
100};
101
102static const struct v4l2_pix_format vga_mode[] = { 46static const struct v4l2_pix_format vga_mode[] = {
103 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 47 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
104 .bytesperline = 176, 48 .bytesperline = 176,
@@ -817,17 +761,11 @@ static void cx11646_init1(struct gspca_dev *gspca_dev)
817static int sd_config(struct gspca_dev *gspca_dev, 761static int sd_config(struct gspca_dev *gspca_dev,
818 const struct usb_device_id *id) 762 const struct usb_device_id *id)
819{ 763{
820 struct sd *sd = (struct sd *) gspca_dev;
821 struct cam *cam; 764 struct cam *cam;
822 765
823 cam = &gspca_dev->cam; 766 cam = &gspca_dev->cam;
824 cam->cam_mode = vga_mode; 767 cam->cam_mode = vga_mode;
825 cam->nmodes = ARRAY_SIZE(vga_mode); 768 cam->nmodes = ARRAY_SIZE(vga_mode);
826
827 sd->brightness = BRIGHTNESS_DEF;
828 sd->contrast = CONTRAST_DEF;
829 sd->colors = COLOR_DEF;
830 sd->quality = QUALITY_DEF;
831 return 0; 769 return 0;
832} 770}
833 771
@@ -849,7 +787,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
849 /* create the JPEG header */ 787 /* create the JPEG header */
850 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width, 788 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
851 0x22); /* JPEG 411 */ 789 0x22); /* JPEG 411 */
852 jpeg_set_qual(sd->jpeg_hdr, sd->quality); 790 jpeg_set_qual(sd->jpeg_hdr, QUALITY);
853 791
854 cx11646_initsize(gspca_dev); 792 cx11646_initsize(gspca_dev);
855 cx11646_fw(gspca_dev); 793 cx11646_fw(gspca_dev);
@@ -903,142 +841,99 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
903 gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 841 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
904} 842}
905 843
906static void setbrightness(struct gspca_dev *gspca_dev) 844static void setbrightness(struct gspca_dev *gspca_dev, s32 val, s32 sat)
907{ 845{
908 struct sd *sd = (struct sd *) gspca_dev;
909 __u8 regE5cbx[] = { 0x88, 0x00, 0xd4, 0x01, 0x88, 0x01, 0x01, 0x01 }; 846 __u8 regE5cbx[] = { 0x88, 0x00, 0xd4, 0x01, 0x88, 0x01, 0x01, 0x01 };
910 __u8 reg51c[2]; 847 __u8 reg51c[2];
911 __u8 bright;
912 __u8 colors;
913 848
914 bright = sd->brightness; 849 regE5cbx[2] = val;
915 regE5cbx[2] = bright;
916 reg_w(gspca_dev, 0x00e5, regE5cbx, 8); 850 reg_w(gspca_dev, 0x00e5, regE5cbx, 8);
917 reg_r(gspca_dev, 0x00e8, 8); 851 reg_r(gspca_dev, 0x00e8, 8);
918 reg_w(gspca_dev, 0x00e5, regE5c, 4); 852 reg_w(gspca_dev, 0x00e5, regE5c, 4);
919 reg_r(gspca_dev, 0x00e8, 1); /* 0x00 */ 853 reg_r(gspca_dev, 0x00e8, 1); /* 0x00 */
920 854
921 colors = sd->colors;
922 reg51c[0] = 0x77; 855 reg51c[0] = 0x77;
923 reg51c[1] = colors; 856 reg51c[1] = sat;
924 reg_w(gspca_dev, 0x0051, reg51c, 2); 857 reg_w(gspca_dev, 0x0051, reg51c, 2);
925 reg_w(gspca_dev, 0x0010, reg10, 2); 858 reg_w(gspca_dev, 0x0010, reg10, 2);
926 reg_w_val(gspca_dev, 0x0070, reg70); 859 reg_w_val(gspca_dev, 0x0070, reg70);
927} 860}
928 861
929static void setcontrast(struct gspca_dev *gspca_dev) 862static void setcontrast(struct gspca_dev *gspca_dev, s32 val, s32 sat)
930{ 863{
931 struct sd *sd = (struct sd *) gspca_dev;
932 __u8 regE5acx[] = { 0x88, 0x0a, 0x0c, 0x01 }; /* seem MSB */ 864 __u8 regE5acx[] = { 0x88, 0x0a, 0x0c, 0x01 }; /* seem MSB */
933/* __u8 regE5bcx[] = { 0x88, 0x0b, 0x12, 0x01}; * LSB */ 865/* __u8 regE5bcx[] = { 0x88, 0x0b, 0x12, 0x01}; * LSB */
934 __u8 reg51c[2]; 866 __u8 reg51c[2];
935 867
936 regE5acx[2] = sd->contrast; 868 regE5acx[2] = val;
937 reg_w(gspca_dev, 0x00e5, regE5acx, 4); 869 reg_w(gspca_dev, 0x00e5, regE5acx, 4);
938 reg_r(gspca_dev, 0x00e8, 1); /* 0x00 */ 870 reg_r(gspca_dev, 0x00e8, 1); /* 0x00 */
939 reg51c[0] = 0x77; 871 reg51c[0] = 0x77;
940 reg51c[1] = sd->colors; 872 reg51c[1] = sat;
941 reg_w(gspca_dev, 0x0051, reg51c, 2); 873 reg_w(gspca_dev, 0x0051, reg51c, 2);
942 reg_w(gspca_dev, 0x0010, reg10, 2); 874 reg_w(gspca_dev, 0x0010, reg10, 2);
943 reg_w_val(gspca_dev, 0x0070, reg70); 875 reg_w_val(gspca_dev, 0x0070, reg70);
944} 876}
945 877
946static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 878static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
947{
948 struct sd *sd = (struct sd *) gspca_dev;
949
950 sd->brightness = val;
951 if (gspca_dev->streaming)
952 setbrightness(gspca_dev);
953 return 0;
954}
955
956static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
957{
958 struct sd *sd = (struct sd *) gspca_dev;
959
960 *val = sd->brightness;
961 return 0;
962}
963
964static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
965{
966 struct sd *sd = (struct sd *) gspca_dev;
967
968 sd->contrast = val;
969 if (gspca_dev->streaming)
970 setcontrast(gspca_dev);
971 return 0;
972}
973
974static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
975{ 879{
976 struct sd *sd = (struct sd *) gspca_dev; 880 struct gspca_dev *gspca_dev =
881 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
882 struct sd *sd = (struct sd *)gspca_dev;
977 883
978 *val = sd->contrast; 884 gspca_dev->usb_err = 0;
979 return 0;
980}
981 885
982static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val) 886 if (!gspca_dev->streaming)
983{ 887 return 0;
984 struct sd *sd = (struct sd *) gspca_dev;
985 888
986 sd->colors = val; 889 switch (ctrl->id) {
987 if (gspca_dev->streaming) { 890 case V4L2_CID_BRIGHTNESS:
988 setbrightness(gspca_dev); 891 setbrightness(gspca_dev, ctrl->val, sd->sat->cur.val);
989 setcontrast(gspca_dev); 892 break;
893 case V4L2_CID_CONTRAST:
894 setcontrast(gspca_dev, ctrl->val, sd->sat->cur.val);
895 break;
896 case V4L2_CID_SATURATION:
897 setbrightness(gspca_dev, sd->brightness->cur.val, ctrl->val);
898 setcontrast(gspca_dev, sd->contrast->cur.val, ctrl->val);
899 break;
990 } 900 }
991 return 0; 901 return gspca_dev->usb_err;
992} 902}
993 903
994static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val) 904static const struct v4l2_ctrl_ops sd_ctrl_ops = {
995{ 905 .s_ctrl = sd_s_ctrl,
996 struct sd *sd = (struct sd *) gspca_dev; 906};
997
998 *val = sd->colors;
999 return 0;
1000}
1001
1002static int sd_set_jcomp(struct gspca_dev *gspca_dev,
1003 struct v4l2_jpegcompression *jcomp)
1004{
1005 struct sd *sd = (struct sd *) gspca_dev;
1006
1007 if (jcomp->quality < QUALITY_MIN)
1008 sd->quality = QUALITY_MIN;
1009 else if (jcomp->quality > QUALITY_MAX)
1010 sd->quality = QUALITY_MAX;
1011 else
1012 sd->quality = jcomp->quality;
1013 if (gspca_dev->streaming)
1014 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
1015 return 0;
1016}
1017 907
1018static int sd_get_jcomp(struct gspca_dev *gspca_dev, 908static int sd_init_controls(struct gspca_dev *gspca_dev)
1019 struct v4l2_jpegcompression *jcomp)
1020{ 909{
1021 struct sd *sd = (struct sd *) gspca_dev; 910 struct sd *sd = (struct sd *)gspca_dev;
1022 911 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1023 memset(jcomp, 0, sizeof *jcomp); 912
1024 jcomp->quality = sd->quality; 913 gspca_dev->vdev.ctrl_handler = hdl;
1025 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT 914 v4l2_ctrl_handler_init(hdl, 3);
1026 | V4L2_JPEG_MARKER_DQT; 915 sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
916 V4L2_CID_BRIGHTNESS, 0, 255, 1, 0xd4);
917 sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
918 V4L2_CID_CONTRAST, 0x0a, 0x1f, 1, 0x0c);
919 sd->sat = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
920 V4L2_CID_SATURATION, 0, 7, 1, 3);
921 if (hdl->error) {
922 pr_err("Could not initialize controls\n");
923 return hdl->error;
924 }
1027 return 0; 925 return 0;
1028} 926}
1029 927
1030/* sub-driver description */ 928/* sub-driver description */
1031static const struct sd_desc sd_desc = { 929static const struct sd_desc sd_desc = {
1032 .name = MODULE_NAME, 930 .name = MODULE_NAME,
1033 .ctrls = sd_ctrls,
1034 .nctrls = ARRAY_SIZE(sd_ctrls),
1035 .config = sd_config, 931 .config = sd_config,
1036 .init = sd_init, 932 .init = sd_init,
933 .init_controls = sd_init_controls,
1037 .start = sd_start, 934 .start = sd_start,
1038 .stop0 = sd_stop0, 935 .stop0 = sd_stop0,
1039 .pkt_scan = sd_pkt_scan, 936 .pkt_scan = sd_pkt_scan,
1040 .get_jcomp = sd_get_jcomp,
1041 .set_jcomp = sd_set_jcomp,
1042}; 937};
1043 938
1044/* -- module initialisation -- */ 939/* -- module initialisation -- */
@@ -1064,6 +959,7 @@ static struct usb_driver sd_driver = {
1064#ifdef CONFIG_PM 959#ifdef CONFIG_PM
1065 .suspend = gspca_suspend, 960 .suspend = gspca_suspend,
1066 .resume = gspca_resume, 961 .resume = gspca_resume,
962 .reset_resume = gspca_resume,
1067#endif 963#endif
1068}; 964};
1069 965
diff --git a/drivers/media/video/gspca/cpia1.c b/drivers/media/video/gspca/cpia1.c
index 8f33bbd091ad..2499a881d9a3 100644
--- a/drivers/media/video/gspca/cpia1.c
+++ b/drivers/media/video/gspca/cpia1.c
@@ -225,6 +225,15 @@ MODULE_LICENSE("GPL");
225#define FIRMWARE_VERSION(x, y) (sd->params.version.firmwareVersion == (x) && \ 225#define FIRMWARE_VERSION(x, y) (sd->params.version.firmwareVersion == (x) && \
226 sd->params.version.firmwareRevision == (y)) 226 sd->params.version.firmwareRevision == (y))
227 227
228#define CPIA1_CID_COMP_TARGET (V4L2_CTRL_CLASS_USER + 0x1000)
229#define BRIGHTNESS_DEF 50
230#define CONTRAST_DEF 48
231#define SATURATION_DEF 50
232#define FREQ_DEF V4L2_CID_POWER_LINE_FREQUENCY_50HZ
233#define ILLUMINATORS_1_DEF 0
234#define ILLUMINATORS_2_DEF 0
235#define COMP_TARGET_DEF CPIA_COMPRESSION_TARGET_QUALITY
236
228/* Developer's Guide Table 5 p 3-34 237/* Developer's Guide Table 5 p 3-34
229 * indexed by [mains][sensorFps.baserate][sensorFps.divisor]*/ 238 * indexed by [mains][sensorFps.baserate][sensorFps.divisor]*/
230static u8 flicker_jumps[2][2][4] = 239static u8 flicker_jumps[2][2][4] =
@@ -360,135 +369,9 @@ struct sd {
360 atomic_t fps; 369 atomic_t fps;
361 int exposure_count; 370 int exposure_count;
362 u8 exposure_status; 371 u8 exposure_status;
372 struct v4l2_ctrl *freq;
363 u8 mainsFreq; /* 0 = 50hz, 1 = 60hz */ 373 u8 mainsFreq; /* 0 = 50hz, 1 = 60hz */
364 u8 first_frame; 374 u8 first_frame;
365 u8 freq;
366};
367
368/* V4L2 controls supported by the driver */
369static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
370static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
371static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
372static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
373static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val);
374static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val);
375static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
376static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
377static int sd_setcomptarget(struct gspca_dev *gspca_dev, __s32 val);
378static int sd_getcomptarget(struct gspca_dev *gspca_dev, __s32 *val);
379static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val);
380static int sd_getilluminator1(struct gspca_dev *gspca_dev, __s32 *val);
381static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val);
382static int sd_getilluminator2(struct gspca_dev *gspca_dev, __s32 *val);
383
384static const struct ctrl sd_ctrls[] = {
385 {
386#define BRIGHTNESS_IDX 0
387 {
388 .id = V4L2_CID_BRIGHTNESS,
389 .type = V4L2_CTRL_TYPE_INTEGER,
390 .name = "Brightness",
391 .minimum = 0,
392 .maximum = 100,
393 .step = 1,
394#define BRIGHTNESS_DEF 50
395 .default_value = BRIGHTNESS_DEF,
396 .flags = 0,
397 },
398 .set = sd_setbrightness,
399 .get = sd_getbrightness,
400 },
401#define CONTRAST_IDX 1
402 {
403 {
404 .id = V4L2_CID_CONTRAST,
405 .type = V4L2_CTRL_TYPE_INTEGER,
406 .name = "Contrast",
407 .minimum = 0,
408 .maximum = 96,
409 .step = 8,
410#define CONTRAST_DEF 48
411 .default_value = CONTRAST_DEF,
412 },
413 .set = sd_setcontrast,
414 .get = sd_getcontrast,
415 },
416#define SATURATION_IDX 2
417 {
418 {
419 .id = V4L2_CID_SATURATION,
420 .type = V4L2_CTRL_TYPE_INTEGER,
421 .name = "Saturation",
422 .minimum = 0,
423 .maximum = 100,
424 .step = 1,
425#define SATURATION_DEF 50
426 .default_value = SATURATION_DEF,
427 },
428 .set = sd_setsaturation,
429 .get = sd_getsaturation,
430 },
431#define POWER_LINE_FREQUENCY_IDX 3
432 {
433 {
434 .id = V4L2_CID_POWER_LINE_FREQUENCY,
435 .type = V4L2_CTRL_TYPE_MENU,
436 .name = "Light frequency filter",
437 .minimum = 0,
438 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
439 .step = 1,
440#define FREQ_DEF 1
441 .default_value = FREQ_DEF,
442 },
443 .set = sd_setfreq,
444 .get = sd_getfreq,
445 },
446#define ILLUMINATORS_1_IDX 4
447 {
448 {
449 .id = V4L2_CID_ILLUMINATORS_1,
450 .type = V4L2_CTRL_TYPE_BOOLEAN,
451 .name = "Illuminator 1",
452 .minimum = 0,
453 .maximum = 1,
454 .step = 1,
455#define ILLUMINATORS_1_DEF 0
456 .default_value = ILLUMINATORS_1_DEF,
457 },
458 .set = sd_setilluminator1,
459 .get = sd_getilluminator1,
460 },
461#define ILLUMINATORS_2_IDX 5
462 {
463 {
464 .id = V4L2_CID_ILLUMINATORS_2,
465 .type = V4L2_CTRL_TYPE_BOOLEAN,
466 .name = "Illuminator 2",
467 .minimum = 0,
468 .maximum = 1,
469 .step = 1,
470#define ILLUMINATORS_2_DEF 0
471 .default_value = ILLUMINATORS_2_DEF,
472 },
473 .set = sd_setilluminator2,
474 .get = sd_getilluminator2,
475 },
476#define COMP_TARGET_IDX 6
477 {
478 {
479#define V4L2_CID_COMP_TARGET V4L2_CID_PRIVATE_BASE
480 .id = V4L2_CID_COMP_TARGET,
481 .type = V4L2_CTRL_TYPE_MENU,
482 .name = "Compression Target",
483 .minimum = 0,
484 .maximum = 1,
485 .step = 1,
486#define COMP_TARGET_DEF CPIA_COMPRESSION_TARGET_QUALITY
487 .default_value = COMP_TARGET_DEF,
488 },
489 .set = sd_setcomptarget,
490 .get = sd_getcomptarget,
491 },
492}; 375};
493 376
494static const struct v4l2_pix_format mode[] = { 377static const struct v4l2_pix_format mode[] = {
@@ -770,15 +653,6 @@ static void reset_camera_params(struct gspca_dev *gspca_dev)
770 params->apcor.gain2 = 0x16; 653 params->apcor.gain2 = 0x16;
771 params->apcor.gain4 = 0x24; 654 params->apcor.gain4 = 0x24;
772 params->apcor.gain8 = 0x34; 655 params->apcor.gain8 = 0x34;
773 params->flickerControl.flickerMode = 0;
774 params->flickerControl.disabled = 1;
775
776 params->flickerControl.coarseJump =
777 flicker_jumps[sd->mainsFreq]
778 [params->sensorFps.baserate]
779 [params->sensorFps.divisor];
780 params->flickerControl.allowableOverExposure =
781 find_over_exposure(params->colourParams.brightness);
782 params->vlOffset.gain1 = 20; 656 params->vlOffset.gain1 = 20;
783 params->vlOffset.gain2 = 24; 657 params->vlOffset.gain2 = 24;
784 params->vlOffset.gain4 = 26; 658 params->vlOffset.gain4 = 26;
@@ -798,6 +672,15 @@ static void reset_camera_params(struct gspca_dev *gspca_dev)
798 params->sensorFps.divisor = 1; 672 params->sensorFps.divisor = 1;
799 params->sensorFps.baserate = 1; 673 params->sensorFps.baserate = 1;
800 674
675 params->flickerControl.flickerMode = 0;
676 params->flickerControl.disabled = 1;
677 params->flickerControl.coarseJump =
678 flicker_jumps[sd->mainsFreq]
679 [params->sensorFps.baserate]
680 [params->sensorFps.divisor];
681 params->flickerControl.allowableOverExposure =
682 find_over_exposure(params->colourParams.brightness);
683
801 params->yuvThreshold.yThreshold = 6; /* From windows driver */ 684 params->yuvThreshold.yThreshold = 6; /* From windows driver */
802 params->yuvThreshold.uvThreshold = 6; /* From windows driver */ 685 params->yuvThreshold.uvThreshold = 6; /* From windows driver */
803 686
@@ -1110,9 +993,6 @@ static int command_setlights(struct gspca_dev *gspca_dev)
1110 struct sd *sd = (struct sd *) gspca_dev; 993 struct sd *sd = (struct sd *) gspca_dev;
1111 int ret, p1, p2; 994 int ret, p1, p2;
1112 995
1113 if (!sd->params.qx3.qx3_detected)
1114 return 0;
1115
1116 p1 = (sd->params.qx3.bottomlight == 0) << 1; 996 p1 = (sd->params.qx3.bottomlight == 0) << 1;
1117 p2 = (sd->params.qx3.toplight == 0) << 3; 997 p2 = (sd->params.qx3.toplight == 0) << 3;
1118 998
@@ -1551,8 +1431,10 @@ static void restart_flicker(struct gspca_dev *gspca_dev)
1551static int sd_config(struct gspca_dev *gspca_dev, 1431static int sd_config(struct gspca_dev *gspca_dev,
1552 const struct usb_device_id *id) 1432 const struct usb_device_id *id)
1553{ 1433{
1434 struct sd *sd = (struct sd *) gspca_dev;
1554 struct cam *cam; 1435 struct cam *cam;
1555 1436
1437 sd->mainsFreq = FREQ_DEF == V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
1556 reset_camera_params(gspca_dev); 1438 reset_camera_params(gspca_dev);
1557 1439
1558 PDEBUG(D_PROBE, "cpia CPiA camera detected (vid/pid 0x%04X:0x%04X)", 1440 PDEBUG(D_PROBE, "cpia CPiA camera detected (vid/pid 0x%04X:0x%04X)",
@@ -1562,8 +1444,25 @@ static int sd_config(struct gspca_dev *gspca_dev,
1562 cam->cam_mode = mode; 1444 cam->cam_mode = mode;
1563 cam->nmodes = ARRAY_SIZE(mode); 1445 cam->nmodes = ARRAY_SIZE(mode);
1564 1446
1565 sd_setfreq(gspca_dev, FREQ_DEF); 1447 goto_low_power(gspca_dev);
1448 /* Check the firmware version. */
1449 sd->params.version.firmwareVersion = 0;
1450 get_version_information(gspca_dev);
1451 if (sd->params.version.firmwareVersion != 1) {
1452 PDEBUG(D_ERR, "only firmware version 1 is supported (got: %d)",
1453 sd->params.version.firmwareVersion);
1454 return -ENODEV;
1455 }
1566 1456
1457 /* A bug in firmware 1-02 limits gainMode to 2 */
1458 if (sd->params.version.firmwareRevision <= 2 &&
1459 sd->params.exposure.gainMode > 2) {
1460 sd->params.exposure.gainMode = 2;
1461 }
1462
1463 /* set QX3 detected flag */
1464 sd->params.qx3.qx3_detected = (sd->params.pnpID.vendor == 0x0813 &&
1465 sd->params.pnpID.product == 0x0001);
1567 return 0; 1466 return 0;
1568} 1467}
1569 1468
@@ -1602,21 +1501,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
1602 /* Check the firmware version. */ 1501 /* Check the firmware version. */
1603 sd->params.version.firmwareVersion = 0; 1502 sd->params.version.firmwareVersion = 0;
1604 get_version_information(gspca_dev); 1503 get_version_information(gspca_dev);
1605 if (sd->params.version.firmwareVersion != 1) {
1606 PDEBUG(D_ERR, "only firmware version 1 is supported (got: %d)",
1607 sd->params.version.firmwareVersion);
1608 return -ENODEV;
1609 }
1610
1611 /* A bug in firmware 1-02 limits gainMode to 2 */
1612 if (sd->params.version.firmwareRevision <= 2 &&
1613 sd->params.exposure.gainMode > 2) {
1614 sd->params.exposure.gainMode = 2;
1615 }
1616
1617 /* set QX3 detected flag */
1618 sd->params.qx3.qx3_detected = (sd->params.pnpID.vendor == 0x0813 &&
1619 sd->params.pnpID.product == 0x0001);
1620 1504
1621 /* The fatal error checking should be done after 1505 /* The fatal error checking should be done after
1622 * the camera powers up (developer's guide p 3-38) */ 1506 * the camera powers up (developer's guide p 3-38) */
@@ -1785,9 +1669,6 @@ static int sd_init(struct gspca_dev *gspca_dev)
1785 or disable the illuminator controls, if this isn't a QX3 */ 1669 or disable the illuminator controls, if this isn't a QX3 */
1786 if (sd->params.qx3.qx3_detected) 1670 if (sd->params.qx3.qx3_detected)
1787 command_setlights(gspca_dev); 1671 command_setlights(gspca_dev);
1788 else
1789 gspca_dev->ctrl_dis |=
1790 ((1 << ILLUMINATORS_1_IDX) | (1 << ILLUMINATORS_2_IDX));
1791 1672
1792 sd_stopN(gspca_dev); 1673 sd_stopN(gspca_dev);
1793 1674
@@ -1871,235 +1752,123 @@ static void sd_dq_callback(struct gspca_dev *gspca_dev)
1871 do_command(gspca_dev, CPIA_COMMAND_ReadMCPorts, 0, 0, 0, 0); 1752 do_command(gspca_dev, CPIA_COMMAND_ReadMCPorts, 0, 0, 0, 0);
1872} 1753}
1873 1754
1874static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 1755static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1875{
1876 struct sd *sd = (struct sd *) gspca_dev;
1877 int ret;
1878
1879 sd->params.colourParams.brightness = val;
1880 sd->params.flickerControl.allowableOverExposure =
1881 find_over_exposure(sd->params.colourParams.brightness);
1882 if (gspca_dev->streaming) {
1883 ret = command_setcolourparams(gspca_dev);
1884 if (ret)
1885 return ret;
1886 return command_setflickerctrl(gspca_dev);
1887 }
1888 return 0;
1889}
1890
1891static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1892{ 1756{
1893 struct sd *sd = (struct sd *) gspca_dev; 1757 struct gspca_dev *gspca_dev =
1894 1758 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1895 *val = sd->params.colourParams.brightness; 1759 struct sd *sd = (struct sd *)gspca_dev;
1896 return 0;
1897}
1898 1760
1899static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val) 1761 gspca_dev->usb_err = 0;
1900{
1901 struct sd *sd = (struct sd *) gspca_dev;
1902 1762
1903 sd->params.colourParams.contrast = val; 1763 if (!gspca_dev->streaming && ctrl->id != V4L2_CID_POWER_LINE_FREQUENCY)
1904 if (gspca_dev->streaming) 1764 return 0;
1905 return command_setcolourparams(gspca_dev);
1906
1907 return 0;
1908}
1909
1910static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
1911{
1912 struct sd *sd = (struct sd *) gspca_dev;
1913
1914 *val = sd->params.colourParams.contrast;
1915 return 0;
1916}
1917
1918static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val)
1919{
1920 struct sd *sd = (struct sd *) gspca_dev;
1921
1922 sd->params.colourParams.saturation = val;
1923 if (gspca_dev->streaming)
1924 return command_setcolourparams(gspca_dev);
1925
1926 return 0;
1927}
1928
1929static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val)
1930{
1931 struct sd *sd = (struct sd *) gspca_dev;
1932
1933 *val = sd->params.colourParams.saturation;
1934 return 0;
1935}
1936
1937static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
1938{
1939 struct sd *sd = (struct sd *) gspca_dev;
1940 int on;
1941 1765
1942 switch (val) { 1766 switch (ctrl->id) {
1943 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */ 1767 case V4L2_CID_BRIGHTNESS:
1944 on = 0; 1768 sd->params.colourParams.brightness = ctrl->val;
1769 sd->params.flickerControl.allowableOverExposure =
1770 find_over_exposure(sd->params.colourParams.brightness);
1771 gspca_dev->usb_err = command_setcolourparams(gspca_dev);
1772 if (!gspca_dev->usb_err)
1773 gspca_dev->usb_err = command_setflickerctrl(gspca_dev);
1945 break; 1774 break;
1946 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */ 1775 case V4L2_CID_CONTRAST:
1947 on = 1; 1776 sd->params.colourParams.contrast = ctrl->val;
1948 sd->mainsFreq = 0; 1777 gspca_dev->usb_err = command_setcolourparams(gspca_dev);
1949 break; 1778 break;
1950 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */ 1779 case V4L2_CID_SATURATION:
1951 on = 1; 1780 sd->params.colourParams.saturation = ctrl->val;
1952 sd->mainsFreq = 1; 1781 gspca_dev->usb_err = command_setcolourparams(gspca_dev);
1953 break; 1782 break;
1954 default: 1783 case V4L2_CID_POWER_LINE_FREQUENCY:
1955 return -EINVAL; 1784 sd->mainsFreq = ctrl->val == V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
1956 } 1785 sd->params.flickerControl.coarseJump =
1957 1786 flicker_jumps[sd->mainsFreq]
1958 sd->freq = val; 1787 [sd->params.sensorFps.baserate]
1959 sd->params.flickerControl.coarseJump = 1788 [sd->params.sensorFps.divisor];
1960 flicker_jumps[sd->mainsFreq] 1789
1961 [sd->params.sensorFps.baserate] 1790 gspca_dev->usb_err = set_flicker(gspca_dev,
1962 [sd->params.sensorFps.divisor]; 1791 ctrl->val != V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
1963 1792 gspca_dev->streaming);
1964 return set_flicker(gspca_dev, on, gspca_dev->streaming);
1965}
1966
1967static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
1968{
1969 struct sd *sd = (struct sd *) gspca_dev;
1970
1971 *val = sd->freq;
1972 return 0;
1973}
1974
1975static int sd_setcomptarget(struct gspca_dev *gspca_dev, __s32 val)
1976{
1977 struct sd *sd = (struct sd *) gspca_dev;
1978
1979 sd->params.compressionTarget.frTargeting = val;
1980 if (gspca_dev->streaming)
1981 return command_setcompressiontarget(gspca_dev);
1982
1983 return 0;
1984}
1985
1986static int sd_getcomptarget(struct gspca_dev *gspca_dev, __s32 *val)
1987{
1988 struct sd *sd = (struct sd *) gspca_dev;
1989
1990 *val = sd->params.compressionTarget.frTargeting;
1991 return 0;
1992}
1993
1994static int sd_setilluminator(struct gspca_dev *gspca_dev, __s32 val, int n)
1995{
1996 struct sd *sd = (struct sd *) gspca_dev;
1997 int ret;
1998
1999 if (!sd->params.qx3.qx3_detected)
2000 return -EINVAL;
2001
2002 switch (n) {
2003 case 1:
2004 sd->params.qx3.bottomlight = val ? 1 : 0;
2005 break; 1793 break;
2006 case 2: 1794 case V4L2_CID_ILLUMINATORS_1:
2007 sd->params.qx3.toplight = val ? 1 : 0; 1795 sd->params.qx3.bottomlight = ctrl->val;
1796 gspca_dev->usb_err = command_setlights(gspca_dev);
2008 break; 1797 break;
2009 default: 1798 case V4L2_CID_ILLUMINATORS_2:
2010 return -EINVAL; 1799 sd->params.qx3.toplight = ctrl->val;
2011 } 1800 gspca_dev->usb_err = command_setlights(gspca_dev);
2012
2013 ret = command_setlights(gspca_dev);
2014 if (ret && ret != -EINVAL)
2015 ret = -EBUSY;
2016
2017 return ret;
2018}
2019
2020static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val)
2021{
2022 return sd_setilluminator(gspca_dev, val, 1);
2023}
2024
2025static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val)
2026{
2027 return sd_setilluminator(gspca_dev, val, 2);
2028}
2029
2030static int sd_getilluminator(struct gspca_dev *gspca_dev, __s32 *val, int n)
2031{
2032 struct sd *sd = (struct sd *) gspca_dev;
2033
2034 if (!sd->params.qx3.qx3_detected)
2035 return -EINVAL;
2036
2037 switch (n) {
2038 case 1:
2039 *val = sd->params.qx3.bottomlight;
2040 break; 1801 break;
2041 case 2: 1802 case CPIA1_CID_COMP_TARGET:
2042 *val = sd->params.qx3.toplight; 1803 sd->params.compressionTarget.frTargeting = ctrl->val;
1804 gspca_dev->usb_err = command_setcompressiontarget(gspca_dev);
2043 break; 1805 break;
2044 default:
2045 return -EINVAL;
2046 } 1806 }
2047 return 0; 1807 return gspca_dev->usb_err;
2048} 1808}
2049 1809
2050static int sd_getilluminator1(struct gspca_dev *gspca_dev, __s32 *val) 1810static const struct v4l2_ctrl_ops sd_ctrl_ops = {
2051{ 1811 .s_ctrl = sd_s_ctrl,
2052 return sd_getilluminator(gspca_dev, val, 1); 1812};
2053}
2054 1813
2055static int sd_getilluminator2(struct gspca_dev *gspca_dev, __s32 *val) 1814static int sd_init_controls(struct gspca_dev *gspca_dev)
2056{ 1815{
2057 return sd_getilluminator(gspca_dev, val, 2); 1816 struct sd *sd = (struct sd *)gspca_dev;
2058} 1817 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1818 static const char * const comp_target_menu[] = {
1819 "Quality",
1820 "Framerate",
1821 NULL
1822 };
1823 static const struct v4l2_ctrl_config comp_target = {
1824 .ops = &sd_ctrl_ops,
1825 .id = CPIA1_CID_COMP_TARGET,
1826 .type = V4L2_CTRL_TYPE_MENU,
1827 .name = "Compression Target",
1828 .qmenu = comp_target_menu,
1829 .max = 1,
1830 .def = COMP_TARGET_DEF,
1831 };
1832
1833 gspca_dev->vdev.ctrl_handler = hdl;
1834 v4l2_ctrl_handler_init(hdl, 7);
1835 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1836 V4L2_CID_BRIGHTNESS, 0, 100, 1, BRIGHTNESS_DEF);
1837 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1838 V4L2_CID_CONTRAST, 0, 96, 8, CONTRAST_DEF);
1839 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1840 V4L2_CID_SATURATION, 0, 100, 1, SATURATION_DEF);
1841 sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
1842 V4L2_CID_POWER_LINE_FREQUENCY,
1843 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0,
1844 FREQ_DEF);
1845 if (sd->params.qx3.qx3_detected) {
1846 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1847 V4L2_CID_ILLUMINATORS_1, 0, 1, 1,
1848 ILLUMINATORS_1_DEF);
1849 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1850 V4L2_CID_ILLUMINATORS_2, 0, 1, 1,
1851 ILLUMINATORS_2_DEF);
1852 }
1853 v4l2_ctrl_new_custom(hdl, &comp_target, NULL);
2059 1854
2060static int sd_querymenu(struct gspca_dev *gspca_dev, 1855 if (hdl->error) {
2061 struct v4l2_querymenu *menu) 1856 pr_err("Could not initialize controls\n");
2062{ 1857 return hdl->error;
2063 switch (menu->id) {
2064 case V4L2_CID_POWER_LINE_FREQUENCY:
2065 switch (menu->index) {
2066 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
2067 strcpy((char *) menu->name, "NoFliker");
2068 return 0;
2069 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
2070 strcpy((char *) menu->name, "50 Hz");
2071 return 0;
2072 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
2073 strcpy((char *) menu->name, "60 Hz");
2074 return 0;
2075 }
2076 break;
2077 case V4L2_CID_COMP_TARGET:
2078 switch (menu->index) {
2079 case CPIA_COMPRESSION_TARGET_QUALITY:
2080 strcpy((char *) menu->name, "Quality");
2081 return 0;
2082 case CPIA_COMPRESSION_TARGET_FRAMERATE:
2083 strcpy((char *) menu->name, "Framerate");
2084 return 0;
2085 }
2086 break;
2087 } 1858 }
2088 return -EINVAL; 1859 return 0;
2089} 1860}
2090 1861
2091/* sub-driver description */ 1862/* sub-driver description */
2092static const struct sd_desc sd_desc = { 1863static const struct sd_desc sd_desc = {
2093 .name = MODULE_NAME, 1864 .name = MODULE_NAME,
2094 .ctrls = sd_ctrls,
2095 .nctrls = ARRAY_SIZE(sd_ctrls),
2096 .config = sd_config, 1865 .config = sd_config,
2097 .init = sd_init, 1866 .init = sd_init,
1867 .init_controls = sd_init_controls,
2098 .start = sd_start, 1868 .start = sd_start,
2099 .stopN = sd_stopN, 1869 .stopN = sd_stopN,
2100 .dq_callback = sd_dq_callback, 1870 .dq_callback = sd_dq_callback,
2101 .pkt_scan = sd_pkt_scan, 1871 .pkt_scan = sd_pkt_scan,
2102 .querymenu = sd_querymenu,
2103#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) 1872#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2104 .other_input = 1, 1873 .other_input = 1,
2105#endif 1874#endif
@@ -2129,6 +1898,7 @@ static struct usb_driver sd_driver = {
2129#ifdef CONFIG_PM 1898#ifdef CONFIG_PM
2130 .suspend = gspca_suspend, 1899 .suspend = gspca_suspend,
2131 .resume = gspca_resume, 1900 .resume = gspca_resume,
1901 .reset_resume = gspca_resume,
2132#endif 1902#endif
2133}; 1903};
2134 1904
diff --git a/drivers/media/video/gspca/etoms.c b/drivers/media/video/gspca/etoms.c
index 81a4adbd9f7c..38f68e11c3a2 100644
--- a/drivers/media/video/gspca/etoms.c
+++ b/drivers/media/video/gspca/etoms.c
@@ -32,9 +32,6 @@ MODULE_LICENSE("GPL");
32struct sd { 32struct sd {
33 struct gspca_dev gspca_dev; /* !! must be the first item */ 33 struct gspca_dev gspca_dev; /* !! must be the first item */
34 34
35 unsigned char brightness;
36 unsigned char contrast;
37 unsigned char colors;
38 unsigned char autogain; 35 unsigned char autogain;
39 36
40 char sensor; 37 char sensor;
@@ -44,76 +41,6 @@ struct sd {
44#define AG_CNT_START 13 41#define AG_CNT_START 13
45}; 42};
46 43
47/* V4L2 controls supported by the driver */
48static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
49static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
50static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
51static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
52static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
53static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
54static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
55static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
56
57static const struct ctrl sd_ctrls[] = {
58 {
59 {
60 .id = V4L2_CID_BRIGHTNESS,
61 .type = V4L2_CTRL_TYPE_INTEGER,
62 .name = "Brightness",
63 .minimum = 1,
64 .maximum = 127,
65 .step = 1,
66#define BRIGHTNESS_DEF 63
67 .default_value = BRIGHTNESS_DEF,
68 },
69 .set = sd_setbrightness,
70 .get = sd_getbrightness,
71 },
72 {
73 {
74 .id = V4L2_CID_CONTRAST,
75 .type = V4L2_CTRL_TYPE_INTEGER,
76 .name = "Contrast",
77 .minimum = 0,
78 .maximum = 255,
79 .step = 1,
80#define CONTRAST_DEF 127
81 .default_value = CONTRAST_DEF,
82 },
83 .set = sd_setcontrast,
84 .get = sd_getcontrast,
85 },
86#define COLOR_IDX 2
87 {
88 {
89 .id = V4L2_CID_SATURATION,
90 .type = V4L2_CTRL_TYPE_INTEGER,
91 .name = "Color",
92 .minimum = 0,
93 .maximum = 15,
94 .step = 1,
95#define COLOR_DEF 7
96 .default_value = COLOR_DEF,
97 },
98 .set = sd_setcolors,
99 .get = sd_getcolors,
100 },
101 {
102 {
103 .id = V4L2_CID_AUTOGAIN,
104 .type = V4L2_CTRL_TYPE_BOOLEAN,
105 .name = "Auto Gain",
106 .minimum = 0,
107 .maximum = 1,
108 .step = 1,
109#define AUTOGAIN_DEF 1
110 .default_value = AUTOGAIN_DEF,
111 },
112 .set = sd_setautogain,
113 .get = sd_getautogain,
114 },
115};
116
117static const struct v4l2_pix_format vga_mode[] = { 44static const struct v4l2_pix_format vga_mode[] = {
118 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, 45 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
119 .bytesperline = 320, 46 .bytesperline = 320,
@@ -464,36 +391,31 @@ static void Et_init2(struct gspca_dev *gspca_dev)
464 reg_w_val(gspca_dev, 0x80, 0x20); /* 0x20; */ 391 reg_w_val(gspca_dev, 0x80, 0x20); /* 0x20; */
465} 392}
466 393
467static void setbrightness(struct gspca_dev *gspca_dev) 394static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
468{ 395{
469 struct sd *sd = (struct sd *) gspca_dev;
470 int i; 396 int i;
471 __u8 brightness = sd->brightness;
472 397
473 for (i = 0; i < 4; i++) 398 for (i = 0; i < 4; i++)
474 reg_w_val(gspca_dev, ET_O_RED + i, brightness); 399 reg_w_val(gspca_dev, ET_O_RED + i, val);
475} 400}
476 401
477static void setcontrast(struct gspca_dev *gspca_dev) 402static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
478{ 403{
479 struct sd *sd = (struct sd *) gspca_dev;
480 __u8 RGBG[] = { 0x80, 0x80, 0x80, 0x80, 0x00, 0x00 }; 404 __u8 RGBG[] = { 0x80, 0x80, 0x80, 0x80, 0x00, 0x00 };
481 __u8 contrast = sd->contrast;
482 405
483 memset(RGBG, contrast, sizeof(RGBG) - 2); 406 memset(RGBG, val, sizeof(RGBG) - 2);
484 reg_w(gspca_dev, ET_G_RED, RGBG, 6); 407 reg_w(gspca_dev, ET_G_RED, RGBG, 6);
485} 408}
486 409
487static void setcolors(struct gspca_dev *gspca_dev) 410static void setcolors(struct gspca_dev *gspca_dev, s32 val)
488{ 411{
489 struct sd *sd = (struct sd *) gspca_dev; 412 struct sd *sd = (struct sd *) gspca_dev;
490 __u8 I2cc[] = { 0x05, 0x02, 0x02, 0x05, 0x0d }; 413 __u8 I2cc[] = { 0x05, 0x02, 0x02, 0x05, 0x0d };
491 __u8 i2cflags = 0x01; 414 __u8 i2cflags = 0x01;
492 /* __u8 green = 0; */ 415 /* __u8 green = 0; */
493 __u8 colors = sd->colors;
494 416
495 I2cc[3] = colors; /* red */ 417 I2cc[3] = val; /* red */
496 I2cc[0] = 15 - colors; /* blue */ 418 I2cc[0] = 15 - val; /* blue */
497 /* green = 15 - ((((7*I2cc[0]) >> 2 ) + I2cc[3]) >> 1); */ 419 /* green = 15 - ((((7*I2cc[0]) >> 2 ) + I2cc[3]) >> 1); */
498 /* I2cc[1] = I2cc[2] = green; */ 420 /* I2cc[1] = I2cc[2] = green; */
499 if (sd->sensor == SENSOR_PAS106) { 421 if (sd->sensor == SENSOR_PAS106) {
@@ -504,15 +426,16 @@ static void setcolors(struct gspca_dev *gspca_dev)
504 I2cc[3], I2cc[0], green); */ 426 I2cc[3], I2cc[0], green); */
505} 427}
506 428
507static void getcolors(struct gspca_dev *gspca_dev) 429static s32 getcolors(struct gspca_dev *gspca_dev)
508{ 430{
509 struct sd *sd = (struct sd *) gspca_dev; 431 struct sd *sd = (struct sd *) gspca_dev;
510 432
511 if (sd->sensor == SENSOR_PAS106) { 433 if (sd->sensor == SENSOR_PAS106) {
512/* i2c_r(gspca_dev, PAS106_REG9); * blue */ 434/* i2c_r(gspca_dev, PAS106_REG9); * blue */
513 i2c_r(gspca_dev, PAS106_REG9 + 3); /* red */ 435 i2c_r(gspca_dev, PAS106_REG9 + 3); /* red */
514 sd->colors = gspca_dev->usb_buf[0] & 0x0f; 436 return gspca_dev->usb_buf[0] & 0x0f;
515 } 437 }
438 return 0;
516} 439}
517 440
518static void setautogain(struct gspca_dev *gspca_dev) 441static void setautogain(struct gspca_dev *gspca_dev)
@@ -622,8 +545,7 @@ static void Et_init1(struct gspca_dev *gspca_dev)
622 i2c_w(gspca_dev, PAS106_REG7, I2c4, sizeof I2c4, 1); 545 i2c_w(gspca_dev, PAS106_REG7, I2c4, sizeof I2c4, 1);
623 /* now set by fifo the whole colors setting */ 546 /* now set by fifo the whole colors setting */
624 reg_w(gspca_dev, ET_G_RED, GainRGBG, 6); 547 reg_w(gspca_dev, ET_G_RED, GainRGBG, 6);
625 getcolors(gspca_dev); 548 setcolors(gspca_dev, getcolors(gspca_dev));
626 setcolors(gspca_dev);
627} 549}
628 550
629/* this function is called at probe time */ 551/* this function is called at probe time */
@@ -641,12 +563,7 @@ static int sd_config(struct gspca_dev *gspca_dev,
641 } else { 563 } else {
642 cam->cam_mode = vga_mode; 564 cam->cam_mode = vga_mode;
643 cam->nmodes = ARRAY_SIZE(vga_mode); 565 cam->nmodes = ARRAY_SIZE(vga_mode);
644 gspca_dev->ctrl_dis = (1 << COLOR_IDX);
645 } 566 }
646 sd->brightness = BRIGHTNESS_DEF;
647 sd->contrast = CONTRAST_DEF;
648 sd->colors = COLOR_DEF;
649 sd->autogain = AUTOGAIN_DEF;
650 sd->ag_cnt = -1; 567 sd->ag_cnt = -1;
651 return 0; 568 return 0;
652} 569}
@@ -780,85 +697,68 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
780 } 697 }
781} 698}
782 699
783static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 700static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
784{
785 struct sd *sd = (struct sd *) gspca_dev;
786
787 sd->brightness = val;
788 if (gspca_dev->streaming)
789 setbrightness(gspca_dev);
790 return 0;
791}
792
793static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
794{
795 struct sd *sd = (struct sd *) gspca_dev;
796
797 *val = sd->brightness;
798 return 0;
799}
800
801static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
802{ 701{
803 struct sd *sd = (struct sd *) gspca_dev; 702 struct gspca_dev *gspca_dev =
804 703 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
805 sd->contrast = val; 704 struct sd *sd = (struct sd *)gspca_dev;
806 if (gspca_dev->streaming) 705
807 setcontrast(gspca_dev); 706 gspca_dev->usb_err = 0;
808 return 0; 707
809} 708 if (!gspca_dev->streaming)
810 709 return 0;
811static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val) 710
812{ 711 switch (ctrl->id) {
813 struct sd *sd = (struct sd *) gspca_dev; 712 case V4L2_CID_BRIGHTNESS:
814 713 setbrightness(gspca_dev, ctrl->val);
815 *val = sd->contrast; 714 break;
816 return 0; 715 case V4L2_CID_CONTRAST:
817} 716 setcontrast(gspca_dev, ctrl->val);
818 717 break;
819static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val) 718 case V4L2_CID_SATURATION:
820{ 719 setcolors(gspca_dev, ctrl->val);
821 struct sd *sd = (struct sd *) gspca_dev; 720 break;
822 721 case V4L2_CID_AUTOGAIN:
823 sd->colors = val; 722 sd->autogain = ctrl->val;
824 if (gspca_dev->streaming)
825 setcolors(gspca_dev);
826 return 0;
827}
828
829static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
830{
831 struct sd *sd = (struct sd *) gspca_dev;
832
833 *val = sd->colors;
834 return 0;
835}
836
837static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
838{
839 struct sd *sd = (struct sd *) gspca_dev;
840
841 sd->autogain = val;
842 if (gspca_dev->streaming)
843 setautogain(gspca_dev); 723 setautogain(gspca_dev);
844 return 0; 724 break;
725 }
726 return gspca_dev->usb_err;
845} 727}
846 728
847static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val) 729static const struct v4l2_ctrl_ops sd_ctrl_ops = {
848{ 730 .s_ctrl = sd_s_ctrl,
849 struct sd *sd = (struct sd *) gspca_dev; 731};
850 732
851 *val = sd->autogain; 733static int sd_init_controls(struct gspca_dev *gspca_dev)
734{
735 struct sd *sd = (struct sd *)gspca_dev;
736 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
737
738 gspca_dev->vdev.ctrl_handler = hdl;
739 v4l2_ctrl_handler_init(hdl, 4);
740 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
741 V4L2_CID_BRIGHTNESS, 1, 127, 1, 63);
742 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
743 V4L2_CID_CONTRAST, 0, 255, 1, 127);
744 if (sd->sensor == SENSOR_PAS106)
745 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
746 V4L2_CID_SATURATION, 0, 15, 1, 7);
747 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
748 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
749 if (hdl->error) {
750 pr_err("Could not initialize controls\n");
751 return hdl->error;
752 }
852 return 0; 753 return 0;
853} 754}
854 755
855/* sub-driver description */ 756/* sub-driver description */
856static const struct sd_desc sd_desc = { 757static const struct sd_desc sd_desc = {
857 .name = MODULE_NAME, 758 .name = MODULE_NAME,
858 .ctrls = sd_ctrls,
859 .nctrls = ARRAY_SIZE(sd_ctrls),
860 .config = sd_config, 759 .config = sd_config,
861 .init = sd_init, 760 .init = sd_init,
761 .init_controls = sd_init_controls,
862 .start = sd_start, 762 .start = sd_start,
863 .stopN = sd_stopN, 763 .stopN = sd_stopN,
864 .pkt_scan = sd_pkt_scan, 764 .pkt_scan = sd_pkt_scan,
@@ -892,6 +792,7 @@ static struct usb_driver sd_driver = {
892#ifdef CONFIG_PM 792#ifdef CONFIG_PM
893 .suspend = gspca_suspend, 793 .suspend = gspca_suspend,
894 .resume = gspca_resume, 794 .resume = gspca_resume,
795 .reset_resume = gspca_resume,
895#endif 796#endif
896}; 797};
897 798
diff --git a/drivers/media/video/gspca/finepix.c b/drivers/media/video/gspca/finepix.c
index 6e26c93b4656..c8f2201cc35a 100644
--- a/drivers/media/video/gspca/finepix.c
+++ b/drivers/media/video/gspca/finepix.c
@@ -299,6 +299,7 @@ static struct usb_driver sd_driver = {
299#ifdef CONFIG_PM 299#ifdef CONFIG_PM
300 .suspend = gspca_suspend, 300 .suspend = gspca_suspend,
301 .resume = gspca_resume, 301 .resume = gspca_resume,
302 .reset_resume = gspca_resume,
302#endif 303#endif
303}; 304};
304 305
diff --git a/drivers/media/video/gspca/gl860/gl860.c b/drivers/media/video/gspca/gl860/gl860.c
index c549574c1c7e..ced3b71f14e5 100644
--- a/drivers/media/video/gspca/gl860/gl860.c
+++ b/drivers/media/video/gspca/gl860/gl860.c
@@ -521,6 +521,7 @@ static struct usb_driver sd_driver = {
521#ifdef CONFIG_PM 521#ifdef CONFIG_PM
522 .suspend = gspca_suspend, 522 .suspend = gspca_suspend,
523 .resume = gspca_resume, 523 .resume = gspca_resume,
524 .reset_resume = gspca_resume,
524#endif 525#endif
525}; 526};
526 527
diff --git a/drivers/media/video/gspca/gspca.c b/drivers/media/video/gspca/gspca.c
index 31721eadc597..d4e8343f5b10 100644
--- a/drivers/media/video/gspca/gspca.c
+++ b/drivers/media/video/gspca/gspca.c
@@ -930,6 +930,7 @@ static int gspca_init_transfer(struct gspca_dev *gspca_dev)
930 goto out; 930 goto out;
931 } 931 }
932 gspca_dev->streaming = 1; 932 gspca_dev->streaming = 1;
933 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
933 934
934 /* some bulk transfers are started by the subdriver */ 935 /* some bulk transfers are started by the subdriver */
935 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0) 936 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
@@ -1049,12 +1050,6 @@ static int vidioc_g_register(struct file *file, void *priv,
1049{ 1050{
1050 struct gspca_dev *gspca_dev = video_drvdata(file); 1051 struct gspca_dev *gspca_dev = video_drvdata(file);
1051 1052
1052 if (!gspca_dev->sd_desc->get_chip_ident)
1053 return -ENOTTY;
1054
1055 if (!gspca_dev->sd_desc->get_register)
1056 return -ENOTTY;
1057
1058 gspca_dev->usb_err = 0; 1053 gspca_dev->usb_err = 0;
1059 return gspca_dev->sd_desc->get_register(gspca_dev, reg); 1054 return gspca_dev->sd_desc->get_register(gspca_dev, reg);
1060} 1055}
@@ -1064,12 +1059,6 @@ static int vidioc_s_register(struct file *file, void *priv,
1064{ 1059{
1065 struct gspca_dev *gspca_dev = video_drvdata(file); 1060 struct gspca_dev *gspca_dev = video_drvdata(file);
1066 1061
1067 if (!gspca_dev->sd_desc->get_chip_ident)
1068 return -ENOTTY;
1069
1070 if (!gspca_dev->sd_desc->set_register)
1071 return -ENOTTY;
1072
1073 gspca_dev->usb_err = 0; 1062 gspca_dev->usb_err = 0;
1074 return gspca_dev->sd_desc->set_register(gspca_dev, reg); 1063 return gspca_dev->sd_desc->set_register(gspca_dev, reg);
1075} 1064}
@@ -1080,9 +1069,6 @@ static int vidioc_g_chip_ident(struct file *file, void *priv,
1080{ 1069{
1081 struct gspca_dev *gspca_dev = video_drvdata(file); 1070 struct gspca_dev *gspca_dev = video_drvdata(file);
1082 1071
1083 if (!gspca_dev->sd_desc->get_chip_ident)
1084 return -ENOTTY;
1085
1086 gspca_dev->usb_err = 0; 1072 gspca_dev->usb_err = 0;
1087 return gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip); 1073 return gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1088} 1074}
@@ -1136,8 +1122,10 @@ static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1136 int mode; 1122 int mode;
1137 1123
1138 mode = gspca_dev->curr_mode; 1124 mode = gspca_dev->curr_mode;
1139 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode], 1125 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
1140 sizeof fmt->fmt.pix); 1126 /* some drivers use priv internally, zero it before giving it to
1127 userspace */
1128 fmt->fmt.pix.priv = 0;
1141 return 0; 1129 return 0;
1142} 1130}
1143 1131
@@ -1168,8 +1156,10 @@ static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1168/* else 1156/* else
1169 ; * no chance, return this mode */ 1157 ; * no chance, return this mode */
1170 } 1158 }
1171 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode], 1159 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
1172 sizeof fmt->fmt.pix); 1160 /* some drivers use priv internally, zero it before giving it to
1161 userspace */
1162 fmt->fmt.pix.priv = 0;
1173 return mode; /* used when s_fmt */ 1163 return mode; /* used when s_fmt */
1174} 1164}
1175 1165
@@ -1284,9 +1274,6 @@ static void gspca_release(struct v4l2_device *v4l2_device)
1284 struct gspca_dev *gspca_dev = 1274 struct gspca_dev *gspca_dev =
1285 container_of(v4l2_device, struct gspca_dev, v4l2_dev); 1275 container_of(v4l2_device, struct gspca_dev, v4l2_dev);
1286 1276
1287 PDEBUG(D_PROBE, "%s released",
1288 video_device_node_name(&gspca_dev->vdev));
1289
1290 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler); 1277 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
1291 v4l2_device_unregister(&gspca_dev->v4l2_dev); 1278 v4l2_device_unregister(&gspca_dev->v4l2_dev);
1292 kfree(gspca_dev->usb_buf); 1279 kfree(gspca_dev->usb_buf);
@@ -1694,8 +1681,6 @@ static int vidioc_g_jpegcomp(struct file *file, void *priv,
1694{ 1681{
1695 struct gspca_dev *gspca_dev = video_drvdata(file); 1682 struct gspca_dev *gspca_dev = video_drvdata(file);
1696 1683
1697 if (!gspca_dev->sd_desc->get_jcomp)
1698 return -ENOTTY;
1699 gspca_dev->usb_err = 0; 1684 gspca_dev->usb_err = 0;
1700 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp); 1685 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1701} 1686}
@@ -1705,8 +1690,6 @@ static int vidioc_s_jpegcomp(struct file *file, void *priv,
1705{ 1690{
1706 struct gspca_dev *gspca_dev = video_drvdata(file); 1691 struct gspca_dev *gspca_dev = video_drvdata(file);
1707 1692
1708 if (!gspca_dev->sd_desc->set_jcomp)
1709 return -ENOTTY;
1710 gspca_dev->usb_err = 0; 1693 gspca_dev->usb_err = 0;
1711 return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp); 1694 return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1712} 1695}
@@ -2290,6 +2273,20 @@ int gspca_dev_probe2(struct usb_interface *intf,
2290 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF); 2273 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
2291 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF); 2274 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
2292 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF); 2275 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
2276 if (!gspca_dev->sd_desc->get_chip_ident)
2277 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_CHIP_IDENT);
2278#ifdef CONFIG_VIDEO_ADV_DEBUG
2279 if (!gspca_dev->sd_desc->get_chip_ident ||
2280 !gspca_dev->sd_desc->get_register)
2281 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
2282 if (!gspca_dev->sd_desc->get_chip_ident ||
2283 !gspca_dev->sd_desc->set_register)
2284 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
2285#endif
2286 if (!gspca_dev->sd_desc->get_jcomp)
2287 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
2288 if (!gspca_dev->sd_desc->set_jcomp)
2289 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
2293 2290
2294 /* init video stuff */ 2291 /* init video stuff */
2295 ret = video_register_device(&gspca_dev->vdev, 2292 ret = video_register_device(&gspca_dev->vdev,
@@ -2429,7 +2426,6 @@ int gspca_resume(struct usb_interface *intf)
2429 */ 2426 */
2430 streaming = gspca_dev->streaming; 2427 streaming = gspca_dev->streaming;
2431 gspca_dev->streaming = 0; 2428 gspca_dev->streaming = 0;
2432 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
2433 if (streaming) 2429 if (streaming)
2434 ret = gspca_init_transfer(gspca_dev); 2430 ret = gspca_init_transfer(gspca_dev);
2435 mutex_unlock(&gspca_dev->usb_lock); 2431 mutex_unlock(&gspca_dev->usb_lock);
diff --git a/drivers/media/video/gspca/jeilinj.c b/drivers/media/video/gspca/jeilinj.c
index 5ab3f7e12760..26b99310d628 100644
--- a/drivers/media/video/gspca/jeilinj.c
+++ b/drivers/media/video/gspca/jeilinj.c
@@ -54,21 +54,13 @@ enum {
54#define CAMQUALITY_MIN 0 /* highest cam quality */ 54#define CAMQUALITY_MIN 0 /* highest cam quality */
55#define CAMQUALITY_MAX 97 /* lowest cam quality */ 55#define CAMQUALITY_MAX 97 /* lowest cam quality */
56 56
57enum e_ctrl {
58 LIGHTFREQ,
59 AUTOGAIN,
60 RED,
61 GREEN,
62 BLUE,
63 NCTRLS /* number of controls */
64};
65
66/* Structure to hold all of our device specific stuff */ 57/* Structure to hold all of our device specific stuff */
67struct sd { 58struct sd {
68 struct gspca_dev gspca_dev; /* !! must be the first item */ 59 struct gspca_dev gspca_dev; /* !! must be the first item */
69 struct gspca_ctrl ctrls[NCTRLS];
70 int blocks_left; 60 int blocks_left;
71 const struct v4l2_pix_format *cap_mode; 61 const struct v4l2_pix_format *cap_mode;
62 struct v4l2_ctrl *freq;
63 struct v4l2_ctrl *jpegqual;
72 /* Driver stuff */ 64 /* Driver stuff */
73 u8 type; 65 u8 type;
74 u8 quality; /* image quality */ 66 u8 quality; /* image quality */
@@ -139,23 +131,21 @@ static void jlj_read1(struct gspca_dev *gspca_dev, unsigned char response)
139 } 131 }
140} 132}
141 133
142static void setfreq(struct gspca_dev *gspca_dev) 134static void setfreq(struct gspca_dev *gspca_dev, s32 val)
143{ 135{
144 struct sd *sd = (struct sd *) gspca_dev;
145 u8 freq_commands[][2] = { 136 u8 freq_commands[][2] = {
146 {0x71, 0x80}, 137 {0x71, 0x80},
147 {0x70, 0x07} 138 {0x70, 0x07}
148 }; 139 };
149 140
150 freq_commands[0][1] |= (sd->ctrls[LIGHTFREQ].val >> 1); 141 freq_commands[0][1] |= val >> 1;
151 142
152 jlj_write2(gspca_dev, freq_commands[0]); 143 jlj_write2(gspca_dev, freq_commands[0]);
153 jlj_write2(gspca_dev, freq_commands[1]); 144 jlj_write2(gspca_dev, freq_commands[1]);
154} 145}
155 146
156static void setcamquality(struct gspca_dev *gspca_dev) 147static void setcamquality(struct gspca_dev *gspca_dev, s32 val)
157{ 148{
158 struct sd *sd = (struct sd *) gspca_dev;
159 u8 quality_commands[][2] = { 149 u8 quality_commands[][2] = {
160 {0x71, 0x1E}, 150 {0x71, 0x1E},
161 {0x70, 0x06} 151 {0x70, 0x06}
@@ -163,7 +153,7 @@ static void setcamquality(struct gspca_dev *gspca_dev)
163 u8 camquality; 153 u8 camquality;
164 154
165 /* adapt camera quality from jpeg quality */ 155 /* adapt camera quality from jpeg quality */
166 camquality = ((QUALITY_MAX - sd->quality) * CAMQUALITY_MAX) 156 camquality = ((QUALITY_MAX - val) * CAMQUALITY_MAX)
167 / (QUALITY_MAX - QUALITY_MIN); 157 / (QUALITY_MAX - QUALITY_MIN);
168 quality_commands[0][1] += camquality; 158 quality_commands[0][1] += camquality;
169 159
@@ -171,130 +161,58 @@ static void setcamquality(struct gspca_dev *gspca_dev)
171 jlj_write2(gspca_dev, quality_commands[1]); 161 jlj_write2(gspca_dev, quality_commands[1]);
172} 162}
173 163
174static void setautogain(struct gspca_dev *gspca_dev) 164static void setautogain(struct gspca_dev *gspca_dev, s32 val)
175{ 165{
176 struct sd *sd = (struct sd *) gspca_dev;
177 u8 autogain_commands[][2] = { 166 u8 autogain_commands[][2] = {
178 {0x94, 0x02}, 167 {0x94, 0x02},
179 {0xcf, 0x00} 168 {0xcf, 0x00}
180 }; 169 };
181 170
182 autogain_commands[1][1] = (sd->ctrls[AUTOGAIN].val << 4); 171 autogain_commands[1][1] = val << 4;
183 172
184 jlj_write2(gspca_dev, autogain_commands[0]); 173 jlj_write2(gspca_dev, autogain_commands[0]);
185 jlj_write2(gspca_dev, autogain_commands[1]); 174 jlj_write2(gspca_dev, autogain_commands[1]);
186} 175}
187 176
188static void setred(struct gspca_dev *gspca_dev) 177static void setred(struct gspca_dev *gspca_dev, s32 val)
189{ 178{
190 struct sd *sd = (struct sd *) gspca_dev;
191 u8 setred_commands[][2] = { 179 u8 setred_commands[][2] = {
192 {0x94, 0x02}, 180 {0x94, 0x02},
193 {0xe6, 0x00} 181 {0xe6, 0x00}
194 }; 182 };
195 183
196 setred_commands[1][1] = sd->ctrls[RED].val; 184 setred_commands[1][1] = val;
197 185
198 jlj_write2(gspca_dev, setred_commands[0]); 186 jlj_write2(gspca_dev, setred_commands[0]);
199 jlj_write2(gspca_dev, setred_commands[1]); 187 jlj_write2(gspca_dev, setred_commands[1]);
200} 188}
201 189
202static void setgreen(struct gspca_dev *gspca_dev) 190static void setgreen(struct gspca_dev *gspca_dev, s32 val)
203{ 191{
204 struct sd *sd = (struct sd *) gspca_dev;
205 u8 setgreen_commands[][2] = { 192 u8 setgreen_commands[][2] = {
206 {0x94, 0x02}, 193 {0x94, 0x02},
207 {0xe7, 0x00} 194 {0xe7, 0x00}
208 }; 195 };
209 196
210 setgreen_commands[1][1] = sd->ctrls[GREEN].val; 197 setgreen_commands[1][1] = val;
211 198
212 jlj_write2(gspca_dev, setgreen_commands[0]); 199 jlj_write2(gspca_dev, setgreen_commands[0]);
213 jlj_write2(gspca_dev, setgreen_commands[1]); 200 jlj_write2(gspca_dev, setgreen_commands[1]);
214} 201}
215 202
216static void setblue(struct gspca_dev *gspca_dev) 203static void setblue(struct gspca_dev *gspca_dev, s32 val)
217{ 204{
218 struct sd *sd = (struct sd *) gspca_dev;
219 u8 setblue_commands[][2] = { 205 u8 setblue_commands[][2] = {
220 {0x94, 0x02}, 206 {0x94, 0x02},
221 {0xe9, 0x00} 207 {0xe9, 0x00}
222 }; 208 };
223 209
224 setblue_commands[1][1] = sd->ctrls[BLUE].val; 210 setblue_commands[1][1] = val;
225 211
226 jlj_write2(gspca_dev, setblue_commands[0]); 212 jlj_write2(gspca_dev, setblue_commands[0]);
227 jlj_write2(gspca_dev, setblue_commands[1]); 213 jlj_write2(gspca_dev, setblue_commands[1]);
228} 214}
229 215
230static const struct ctrl sd_ctrls[NCTRLS] = {
231[LIGHTFREQ] = {
232 {
233 .id = V4L2_CID_POWER_LINE_FREQUENCY,
234 .type = V4L2_CTRL_TYPE_MENU,
235 .name = "Light frequency filter",
236 .minimum = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED, /* 1 */
237 .maximum = V4L2_CID_POWER_LINE_FREQUENCY_60HZ, /* 2 */
238 .step = 1,
239 .default_value = V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
240 },
241 .set_control = setfreq
242 },
243[AUTOGAIN] = {
244 {
245 .id = V4L2_CID_AUTOGAIN,
246 .type = V4L2_CTRL_TYPE_INTEGER,
247 .name = "Automatic Gain (and Exposure)",
248 .minimum = 0,
249 .maximum = 3,
250 .step = 1,
251#define AUTOGAIN_DEF 0
252 .default_value = AUTOGAIN_DEF,
253 },
254 .set_control = setautogain
255 },
256[RED] = {
257 {
258 .id = V4L2_CID_RED_BALANCE,
259 .type = V4L2_CTRL_TYPE_INTEGER,
260 .name = "red balance",
261 .minimum = 0,
262 .maximum = 3,
263 .step = 1,
264#define RED_BALANCE_DEF 2
265 .default_value = RED_BALANCE_DEF,
266 },
267 .set_control = setred
268 },
269
270[GREEN] = {
271 {
272 .id = V4L2_CID_GAIN,
273 .type = V4L2_CTRL_TYPE_INTEGER,
274 .name = "green balance",
275 .minimum = 0,
276 .maximum = 3,
277 .step = 1,
278#define GREEN_BALANCE_DEF 2
279 .default_value = GREEN_BALANCE_DEF,
280 },
281 .set_control = setgreen
282 },
283[BLUE] = {
284 {
285 .id = V4L2_CID_BLUE_BALANCE,
286 .type = V4L2_CTRL_TYPE_INTEGER,
287 .name = "blue balance",
288 .minimum = 0,
289 .maximum = 3,
290 .step = 1,
291#define BLUE_BALANCE_DEF 2
292 .default_value = BLUE_BALANCE_DEF,
293 },
294 .set_control = setblue
295 },
296};
297
298static int jlj_start(struct gspca_dev *gspca_dev) 216static int jlj_start(struct gspca_dev *gspca_dev)
299{ 217{
300 int i; 218 int i;
@@ -344,9 +262,9 @@ static int jlj_start(struct gspca_dev *gspca_dev)
344 if (start_commands[i].ack_wanted) 262 if (start_commands[i].ack_wanted)
345 jlj_read1(gspca_dev, response); 263 jlj_read1(gspca_dev, response);
346 } 264 }
347 setcamquality(gspca_dev); 265 setcamquality(gspca_dev, v4l2_ctrl_g_ctrl(sd->jpegqual));
348 msleep(2); 266 msleep(2);
349 setfreq(gspca_dev); 267 setfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->freq));
350 if (gspca_dev->usb_err < 0) 268 if (gspca_dev->usb_err < 0)
351 PDEBUG(D_ERR, "Start streaming command failed"); 269 PDEBUG(D_ERR, "Start streaming command failed");
352 return gspca_dev->usb_err; 270 return gspca_dev->usb_err;
@@ -403,7 +321,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
403 struct sd *dev = (struct sd *) gspca_dev; 321 struct sd *dev = (struct sd *) gspca_dev;
404 322
405 dev->type = id->driver_info; 323 dev->type = id->driver_info;
406 gspca_dev->cam.ctrls = dev->ctrls;
407 dev->quality = QUALITY_DEF; 324 dev->quality = QUALITY_DEF;
408 325
409 cam->cam_mode = jlj_mode; 326 cam->cam_mode = jlj_mode;
@@ -479,25 +396,81 @@ static const struct usb_device_id device_table[] = {
479 396
480MODULE_DEVICE_TABLE(usb, device_table); 397MODULE_DEVICE_TABLE(usb, device_table);
481 398
482static int sd_querymenu(struct gspca_dev *gspca_dev, 399static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
483 struct v4l2_querymenu *menu)
484{ 400{
485 switch (menu->id) { 401 struct gspca_dev *gspca_dev =
402 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
403 struct sd *sd = (struct sd *)gspca_dev;
404
405 gspca_dev->usb_err = 0;
406
407 if (!gspca_dev->streaming)
408 return 0;
409
410 switch (ctrl->id) {
486 case V4L2_CID_POWER_LINE_FREQUENCY: 411 case V4L2_CID_POWER_LINE_FREQUENCY:
487 switch (menu->index) { 412 setfreq(gspca_dev, ctrl->val);
488 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */ 413 break;
489 strcpy((char *) menu->name, "disable"); 414 case V4L2_CID_RED_BALANCE:
490 return 0; 415 setred(gspca_dev, ctrl->val);
491 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */ 416 break;
492 strcpy((char *) menu->name, "50 Hz"); 417 case V4L2_CID_GAIN:
493 return 0; 418 setgreen(gspca_dev, ctrl->val);
494 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */ 419 break;
495 strcpy((char *) menu->name, "60 Hz"); 420 case V4L2_CID_BLUE_BALANCE:
496 return 0; 421 setblue(gspca_dev, ctrl->val);
497 } 422 break;
423 case V4L2_CID_AUTOGAIN:
424 setautogain(gspca_dev, ctrl->val);
425 break;
426 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
427 jpeg_set_qual(sd->jpeg_hdr, ctrl->val);
428 setcamquality(gspca_dev, ctrl->val);
498 break; 429 break;
499 } 430 }
500 return -EINVAL; 431 return gspca_dev->usb_err;
432}
433
434static const struct v4l2_ctrl_ops sd_ctrl_ops = {
435 .s_ctrl = sd_s_ctrl,
436};
437
438static int sd_init_controls(struct gspca_dev *gspca_dev)
439{
440 struct sd *sd = (struct sd *)gspca_dev;
441 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
442 static const struct v4l2_ctrl_config custom_autogain = {
443 .ops = &sd_ctrl_ops,
444 .id = V4L2_CID_AUTOGAIN,
445 .type = V4L2_CTRL_TYPE_INTEGER,
446 .name = "Automatic Gain (and Exposure)",
447 .max = 3,
448 .step = 1,
449 .def = 0,
450 };
451
452 gspca_dev->vdev.ctrl_handler = hdl;
453 v4l2_ctrl_handler_init(hdl, 6);
454 sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
455 V4L2_CID_POWER_LINE_FREQUENCY,
456 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 1,
457 V4L2_CID_POWER_LINE_FREQUENCY_60HZ);
458 v4l2_ctrl_new_custom(hdl, &custom_autogain, NULL);
459 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
460 V4L2_CID_RED_BALANCE, 0, 3, 1, 2);
461 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
462 V4L2_CID_GAIN, 0, 3, 1, 2);
463 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
464 V4L2_CID_BLUE_BALANCE, 0, 3, 1, 2);
465 sd->jpegqual = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
466 V4L2_CID_JPEG_COMPRESSION_QUALITY,
467 QUALITY_MIN, QUALITY_MAX, 1, QUALITY_DEF);
468
469 if (hdl->error) {
470 pr_err("Could not initialize controls\n");
471 return hdl->error;
472 }
473 return 0;
501} 474}
502 475
503static int sd_set_jcomp(struct gspca_dev *gspca_dev, 476static int sd_set_jcomp(struct gspca_dev *gspca_dev,
@@ -505,16 +478,7 @@ static int sd_set_jcomp(struct gspca_dev *gspca_dev,
505{ 478{
506 struct sd *sd = (struct sd *) gspca_dev; 479 struct sd *sd = (struct sd *) gspca_dev;
507 480
508 if (jcomp->quality < QUALITY_MIN) 481 v4l2_ctrl_s_ctrl(sd->jpegqual, jcomp->quality);
509 sd->quality = QUALITY_MIN;
510 else if (jcomp->quality > QUALITY_MAX)
511 sd->quality = QUALITY_MAX;
512 else
513 sd->quality = jcomp->quality;
514 if (gspca_dev->streaming) {
515 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
516 setcamquality(gspca_dev);
517 }
518 return 0; 482 return 0;
519} 483}
520 484
@@ -524,7 +488,7 @@ static int sd_get_jcomp(struct gspca_dev *gspca_dev,
524 struct sd *sd = (struct sd *) gspca_dev; 488 struct sd *sd = (struct sd *) gspca_dev;
525 489
526 memset(jcomp, 0, sizeof *jcomp); 490 memset(jcomp, 0, sizeof *jcomp);
527 jcomp->quality = sd->quality; 491 jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
528 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT 492 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
529 | V4L2_JPEG_MARKER_DQT; 493 | V4L2_JPEG_MARKER_DQT;
530 return 0; 494 return 0;
@@ -546,12 +510,10 @@ static const struct sd_desc sd_desc_sportscam_dv15 = {
546 .name = MODULE_NAME, 510 .name = MODULE_NAME,
547 .config = sd_config, 511 .config = sd_config,
548 .init = sd_init, 512 .init = sd_init,
513 .init_controls = sd_init_controls,
549 .start = sd_start, 514 .start = sd_start,
550 .stopN = sd_stopN, 515 .stopN = sd_stopN,
551 .pkt_scan = sd_pkt_scan, 516 .pkt_scan = sd_pkt_scan,
552 .ctrls = sd_ctrls,
553 .nctrls = ARRAY_SIZE(sd_ctrls),
554 .querymenu = sd_querymenu,
555 .get_jcomp = sd_get_jcomp, 517 .get_jcomp = sd_get_jcomp,
556 .set_jcomp = sd_set_jcomp, 518 .set_jcomp = sd_set_jcomp,
557}; 519};
@@ -579,6 +541,7 @@ static struct usb_driver sd_driver = {
579#ifdef CONFIG_PM 541#ifdef CONFIG_PM
580 .suspend = gspca_suspend, 542 .suspend = gspca_suspend,
581 .resume = gspca_resume, 543 .resume = gspca_resume,
544 .reset_resume = gspca_resume,
582#endif 545#endif
583}; 546};
584 547
diff --git a/drivers/media/video/gspca/jl2005bcd.c b/drivers/media/video/gspca/jl2005bcd.c
index 9c591c7c6f54..cf9d9fca5b84 100644
--- a/drivers/media/video/gspca/jl2005bcd.c
+++ b/drivers/media/video/gspca/jl2005bcd.c
@@ -505,8 +505,6 @@ static void sd_stop0(struct gspca_dev *gspca_dev)
505/* sub-driver description */ 505/* sub-driver description */
506static const struct sd_desc sd_desc = { 506static const struct sd_desc sd_desc = {
507 .name = MODULE_NAME, 507 .name = MODULE_NAME,
508 /* .ctrls = none have been detected */
509 /* .nctrls = ARRAY_SIZE(sd_ctrls), */
510 .config = sd_config, 508 .config = sd_config,
511 .init = sd_init, 509 .init = sd_init,
512 .start = sd_start, 510 .start = sd_start,
@@ -536,6 +534,7 @@ static struct usb_driver sd_driver = {
536#ifdef CONFIG_PM 534#ifdef CONFIG_PM
537 .suspend = gspca_suspend, 535 .suspend = gspca_suspend,
538 .resume = gspca_resume, 536 .resume = gspca_resume,
537 .reset_resume = gspca_resume,
539#endif 538#endif
540}; 539};
541 540
diff --git a/drivers/media/video/gspca/kinect.c b/drivers/media/video/gspca/kinect.c
index e8e8f2fe9166..40ad6687ee5d 100644
--- a/drivers/media/video/gspca/kinect.c
+++ b/drivers/media/video/gspca/kinect.c
@@ -63,12 +63,6 @@ struct sd {
63 uint8_t ibuf[0x200]; /* input buffer for control commands */ 63 uint8_t ibuf[0x200]; /* input buffer for control commands */
64}; 64};
65 65
66/* V4L2 controls supported by the driver */
67/* controls prototypes here */
68
69static const struct ctrl sd_ctrls[] = {
70};
71
72#define MODE_640x480 0x0001 66#define MODE_640x480 0x0001
73#define MODE_640x488 0x0002 67#define MODE_640x488 0x0002
74#define MODE_1280x1024 0x0004 68#define MODE_1280x1024 0x0004
@@ -373,15 +367,12 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
373/* sub-driver description */ 367/* sub-driver description */
374static const struct sd_desc sd_desc = { 368static const struct sd_desc sd_desc = {
375 .name = MODULE_NAME, 369 .name = MODULE_NAME,
376 .ctrls = sd_ctrls,
377 .nctrls = ARRAY_SIZE(sd_ctrls),
378 .config = sd_config, 370 .config = sd_config,
379 .init = sd_init, 371 .init = sd_init,
380 .start = sd_start, 372 .start = sd_start,
381 .stopN = sd_stopN, 373 .stopN = sd_stopN,
382 .pkt_scan = sd_pkt_scan, 374 .pkt_scan = sd_pkt_scan,
383 /* 375 /*
384 .querymenu = sd_querymenu,
385 .get_streamparm = sd_get_streamparm, 376 .get_streamparm = sd_get_streamparm,
386 .set_streamparm = sd_set_streamparm, 377 .set_streamparm = sd_set_streamparm,
387 */ 378 */
@@ -410,6 +401,7 @@ static struct usb_driver sd_driver = {
410#ifdef CONFIG_PM 401#ifdef CONFIG_PM
411 .suspend = gspca_suspend, 402 .suspend = gspca_suspend,
412 .resume = gspca_resume, 403 .resume = gspca_resume,
404 .reset_resume = gspca_resume,
413#endif 405#endif
414}; 406};
415 407
diff --git a/drivers/media/video/gspca/konica.c b/drivers/media/video/gspca/konica.c
index f0c0d74dfe92..bbf91e07e38b 100644
--- a/drivers/media/video/gspca/konica.c
+++ b/drivers/media/video/gspca/konica.c
@@ -50,107 +50,8 @@ struct sd {
50 struct gspca_dev gspca_dev; /* !! must be the first item */ 50 struct gspca_dev gspca_dev; /* !! must be the first item */
51 struct urb *last_data_urb; 51 struct urb *last_data_urb;
52 u8 snapshot_pressed; 52 u8 snapshot_pressed;
53 u8 brightness;
54 u8 contrast;
55 u8 saturation;
56 u8 whitebal;
57 u8 sharpness;
58}; 53};
59 54
60/* V4L2 controls supported by the driver */
61static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
62static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
63static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
64static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
65static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val);
66static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val);
67static int sd_setwhitebal(struct gspca_dev *gspca_dev, __s32 val);
68static int sd_getwhitebal(struct gspca_dev *gspca_dev, __s32 *val);
69static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
70static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
71
72static const struct ctrl sd_ctrls[] = {
73#define SD_BRIGHTNESS 0
74 {
75 {
76 .id = V4L2_CID_BRIGHTNESS,
77 .type = V4L2_CTRL_TYPE_INTEGER,
78 .name = "Brightness",
79 .minimum = 0,
80 .maximum = 9,
81 .step = 1,
82#define BRIGHTNESS_DEFAULT 4
83 .default_value = BRIGHTNESS_DEFAULT,
84 .flags = 0,
85 },
86 .set = sd_setbrightness,
87 .get = sd_getbrightness,
88 },
89#define SD_CONTRAST 1
90 {
91 {
92 .id = V4L2_CID_CONTRAST,
93 .type = V4L2_CTRL_TYPE_INTEGER,
94 .name = "Contrast",
95 .minimum = 0,
96 .maximum = 9,
97 .step = 4,
98#define CONTRAST_DEFAULT 10
99 .default_value = CONTRAST_DEFAULT,
100 .flags = 0,
101 },
102 .set = sd_setcontrast,
103 .get = sd_getcontrast,
104 },
105#define SD_SATURATION 2
106 {
107 {
108 .id = V4L2_CID_SATURATION,
109 .type = V4L2_CTRL_TYPE_INTEGER,
110 .name = "Saturation",
111 .minimum = 0,
112 .maximum = 9,
113 .step = 1,
114#define SATURATION_DEFAULT 4
115 .default_value = SATURATION_DEFAULT,
116 .flags = 0,
117 },
118 .set = sd_setsaturation,
119 .get = sd_getsaturation,
120 },
121#define SD_WHITEBAL 3
122 {
123 {
124 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
125 .type = V4L2_CTRL_TYPE_INTEGER,
126 .name = "White Balance",
127 .minimum = 0,
128 .maximum = 33,
129 .step = 1,
130#define WHITEBAL_DEFAULT 25
131 .default_value = WHITEBAL_DEFAULT,
132 .flags = 0,
133 },
134 .set = sd_setwhitebal,
135 .get = sd_getwhitebal,
136 },
137#define SD_SHARPNESS 4
138 {
139 {
140 .id = V4L2_CID_SHARPNESS,
141 .type = V4L2_CTRL_TYPE_INTEGER,
142 .name = "Sharpness",
143 .minimum = 0,
144 .maximum = 9,
145 .step = 1,
146#define SHARPNESS_DEFAULT 4
147 .default_value = SHARPNESS_DEFAULT,
148 .flags = 0,
149 },
150 .set = sd_setsharpness,
151 .get = sd_getsharpness,
152 },
153};
154 55
155/* .priv is what goes to register 8 for this mode, known working values: 56/* .priv is what goes to register 8 for this mode, known working values:
156 0x00 -> 176x144, cropped 57 0x00 -> 176x144, cropped
@@ -202,7 +103,8 @@ static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
202 0, 103 0,
203 1000); 104 1000);
204 if (ret < 0) { 105 if (ret < 0) {
205 pr_err("reg_w err %d\n", ret); 106 pr_err("reg_w err writing %02x to %02x: %d\n",
107 value, index, ret);
206 gspca_dev->usb_err = ret; 108 gspca_dev->usb_err = ret;
207 } 109 }
208} 110}
@@ -223,7 +125,7 @@ static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
223 2, 125 2,
224 1000); 126 1000);
225 if (ret < 0) { 127 if (ret < 0) {
226 pr_err("reg_w err %d\n", ret); 128 pr_err("reg_r err %d\n", ret);
227 gspca_dev->usb_err = ret; 129 gspca_dev->usb_err = ret;
228 } 130 }
229} 131}
@@ -242,34 +144,33 @@ static void konica_stream_off(struct gspca_dev *gspca_dev)
242static int sd_config(struct gspca_dev *gspca_dev, 144static int sd_config(struct gspca_dev *gspca_dev,
243 const struct usb_device_id *id) 145 const struct usb_device_id *id)
244{ 146{
245 struct sd *sd = (struct sd *) gspca_dev;
246
247 gspca_dev->cam.cam_mode = vga_mode; 147 gspca_dev->cam.cam_mode = vga_mode;
248 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode); 148 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
249 gspca_dev->cam.no_urb_create = 1; 149 gspca_dev->cam.no_urb_create = 1;
250 150
251 sd->brightness = BRIGHTNESS_DEFAULT;
252 sd->contrast = CONTRAST_DEFAULT;
253 sd->saturation = SATURATION_DEFAULT;
254 sd->whitebal = WHITEBAL_DEFAULT;
255 sd->sharpness = SHARPNESS_DEFAULT;
256
257 return 0; 151 return 0;
258} 152}
259 153
260/* this function is called at probe and resume time */ 154/* this function is called at probe and resume time */
261static int sd_init(struct gspca_dev *gspca_dev) 155static int sd_init(struct gspca_dev *gspca_dev)
262{ 156{
263 /* HDG not sure if these 2 reads are needed */ 157 int i;
264 reg_r(gspca_dev, 0, 0x10); 158
265 PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x", 159 /*
266 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]); 160 * The konica needs a freaking large time to "boot" (approx 6.5 sec.),
267 reg_r(gspca_dev, 0, 0x10); 161 * and does not want to be bothered while doing so :|
268 PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x", 162 * Register 0x10 counts from 1 - 3, with 3 being "ready"
269 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]); 163 */
164 msleep(6000);
165 for (i = 0; i < 20; i++) {
166 reg_r(gspca_dev, 0, 0x10);
167 if (gspca_dev->usb_buf[0] == 3)
168 break;
169 msleep(100);
170 }
270 reg_w(gspca_dev, 0, 0x0d); 171 reg_w(gspca_dev, 0, 0x0d);
271 172
272 return 0; 173 return gspca_dev->usb_err;
273} 174}
274 175
275static int sd_start(struct gspca_dev *gspca_dev) 176static int sd_start(struct gspca_dev *gspca_dev)
@@ -289,12 +190,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
289 190
290 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize); 191 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
291 192
292 reg_w(gspca_dev, sd->brightness, BRIGHTNESS_REG);
293 reg_w(gspca_dev, sd->whitebal, WHITEBAL_REG);
294 reg_w(gspca_dev, sd->contrast, CONTRAST_REG);
295 reg_w(gspca_dev, sd->saturation, SATURATION_REG);
296 reg_w(gspca_dev, sd->sharpness, SHARPNESS_REG);
297
298 n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv; 193 n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
299 reg_w(gspca_dev, n, 0x08); 194 reg_w(gspca_dev, n, 0x08);
300 195
@@ -479,125 +374,82 @@ resubmit:
479 pr_err("usb_submit_urb(status_urb) ret %d\n", st); 374 pr_err("usb_submit_urb(status_urb) ret %d\n", st);
480} 375}
481 376
482static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 377static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
483{ 378{
484 struct sd *sd = (struct sd *) gspca_dev; 379 struct gspca_dev *gspca_dev =
485 380 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
486 sd->brightness = val;
487 if (gspca_dev->streaming) {
488 konica_stream_off(gspca_dev);
489 reg_w(gspca_dev, sd->brightness, BRIGHTNESS_REG);
490 konica_stream_on(gspca_dev);
491 }
492
493 return 0;
494}
495
496static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
497{
498 struct sd *sd = (struct sd *) gspca_dev;
499
500 *val = sd->brightness;
501 381
502 return 0; 382 gspca_dev->usb_err = 0;
503}
504 383
505static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val) 384 if (!gspca_dev->streaming)
506{ 385 return 0;
507 struct sd *sd = (struct sd *) gspca_dev;
508 386
509 sd->contrast = val; 387 switch (ctrl->id) {
510 if (gspca_dev->streaming) { 388 case V4L2_CID_BRIGHTNESS:
511 konica_stream_off(gspca_dev); 389 konica_stream_off(gspca_dev);
512 reg_w(gspca_dev, sd->contrast, CONTRAST_REG); 390 reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
513 konica_stream_on(gspca_dev); 391 konica_stream_on(gspca_dev);
514 } 392 break;
515 393 case V4L2_CID_CONTRAST:
516 return 0;
517}
518
519static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
520{
521 struct sd *sd = (struct sd *) gspca_dev;
522
523 *val = sd->contrast;
524
525 return 0;
526}
527
528static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val)
529{
530 struct sd *sd = (struct sd *) gspca_dev;
531
532 sd->saturation = val;
533 if (gspca_dev->streaming) {
534 konica_stream_off(gspca_dev); 394 konica_stream_off(gspca_dev);
535 reg_w(gspca_dev, sd->saturation, SATURATION_REG); 395 reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
536 konica_stream_on(gspca_dev); 396 konica_stream_on(gspca_dev);
537 } 397 break;
538 return 0; 398 case V4L2_CID_SATURATION:
539}
540
541static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val)
542{
543 struct sd *sd = (struct sd *) gspca_dev;
544
545 *val = sd->saturation;
546
547 return 0;
548}
549
550static int sd_setwhitebal(struct gspca_dev *gspca_dev, __s32 val)
551{
552 struct sd *sd = (struct sd *) gspca_dev;
553
554 sd->whitebal = val;
555 if (gspca_dev->streaming) {
556 konica_stream_off(gspca_dev); 399 konica_stream_off(gspca_dev);
557 reg_w(gspca_dev, sd->whitebal, WHITEBAL_REG); 400 reg_w(gspca_dev, ctrl->val, SATURATION_REG);
558 konica_stream_on(gspca_dev); 401 konica_stream_on(gspca_dev);
559 } 402 break;
560 return 0; 403 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
561}
562
563static int sd_getwhitebal(struct gspca_dev *gspca_dev, __s32 *val)
564{
565 struct sd *sd = (struct sd *) gspca_dev;
566
567 *val = sd->whitebal;
568
569 return 0;
570}
571
572static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
573{
574 struct sd *sd = (struct sd *) gspca_dev;
575
576 sd->sharpness = val;
577 if (gspca_dev->streaming) {
578 konica_stream_off(gspca_dev); 404 konica_stream_off(gspca_dev);
579 reg_w(gspca_dev, sd->sharpness, SHARPNESS_REG); 405 reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
580 konica_stream_on(gspca_dev); 406 konica_stream_on(gspca_dev);
407 break;
408 case V4L2_CID_SHARPNESS:
409 konica_stream_off(gspca_dev);
410 reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
411 konica_stream_on(gspca_dev);
412 break;
581 } 413 }
582 return 0; 414 return gspca_dev->usb_err;
583} 415}
584 416
585static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val) 417static const struct v4l2_ctrl_ops sd_ctrl_ops = {
586{ 418 .s_ctrl = sd_s_ctrl,
587 struct sd *sd = (struct sd *) gspca_dev; 419};
588
589 *val = sd->sharpness;
590 420
421static int sd_init_controls(struct gspca_dev *gspca_dev)
422{
423 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
424
425 gspca_dev->vdev.ctrl_handler = hdl;
426 v4l2_ctrl_handler_init(hdl, 5);
427 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
428 V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
429 /* Needs to be verified */
430 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
431 V4L2_CID_CONTRAST, 0, 9, 1, 4);
432 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
433 V4L2_CID_SATURATION, 0, 9, 1, 4);
434 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
435 V4L2_CID_WHITE_BALANCE_TEMPERATURE,
436 0, 33, 1, 25);
437 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
438 V4L2_CID_SHARPNESS, 0, 9, 1, 4);
439
440 if (hdl->error) {
441 pr_err("Could not initialize controls\n");
442 return hdl->error;
443 }
591 return 0; 444 return 0;
592} 445}
593 446
594/* sub-driver description */ 447/* sub-driver description */
595static const struct sd_desc sd_desc = { 448static const struct sd_desc sd_desc = {
596 .name = MODULE_NAME, 449 .name = MODULE_NAME,
597 .ctrls = sd_ctrls,
598 .nctrls = ARRAY_SIZE(sd_ctrls),
599 .config = sd_config, 450 .config = sd_config,
600 .init = sd_init, 451 .init = sd_init,
452 .init_controls = sd_init_controls,
601 .start = sd_start, 453 .start = sd_start,
602 .stopN = sd_stopN, 454 .stopN = sd_stopN,
603#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) 455#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
@@ -628,6 +480,7 @@ static struct usb_driver sd_driver = {
628#ifdef CONFIG_PM 480#ifdef CONFIG_PM
629 .suspend = gspca_suspend, 481 .suspend = gspca_suspend,
630 .resume = gspca_resume, 482 .resume = gspca_resume,
483 .reset_resume = gspca_resume,
631#endif 484#endif
632}; 485};
633 486
diff --git a/drivers/media/video/gspca/m5602/m5602_core.c b/drivers/media/video/gspca/m5602/m5602_core.c
index 0c4493675438..ed22638978ce 100644
--- a/drivers/media/video/gspca/m5602/m5602_core.c
+++ b/drivers/media/video/gspca/m5602/m5602_core.c
@@ -400,6 +400,7 @@ static struct usb_driver sd_driver = {
400#ifdef CONFIG_PM 400#ifdef CONFIG_PM
401 .suspend = gspca_suspend, 401 .suspend = gspca_suspend,
402 .resume = gspca_resume, 402 .resume = gspca_resume,
403 .reset_resume = gspca_resume,
403#endif 404#endif
404 .disconnect = m5602_disconnect 405 .disconnect = m5602_disconnect
405}; 406};
diff --git a/drivers/media/video/gspca/mars.c b/drivers/media/video/gspca/mars.c
index ec7b21ee79fb..ff2c5abf115b 100644
--- a/drivers/media/video/gspca/mars.c
+++ b/drivers/media/video/gspca/mars.c
@@ -30,6 +30,8 @@ MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
30MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver"); 30MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
31MODULE_LICENSE("GPL"); 31MODULE_LICENSE("GPL");
32 32
33#define QUALITY 50
34
33/* specific webcam descriptor */ 35/* specific webcam descriptor */
34struct sd { 36struct sd {
35 struct gspca_dev gspca_dev; /* !! must be the first item */ 37 struct gspca_dev gspca_dev; /* !! must be the first item */
@@ -42,13 +44,6 @@ struct sd {
42 struct v4l2_ctrl *illum_top; 44 struct v4l2_ctrl *illum_top;
43 struct v4l2_ctrl *illum_bottom; 45 struct v4l2_ctrl *illum_bottom;
44 }; 46 };
45 struct v4l2_ctrl *jpegqual;
46
47 u8 quality;
48#define QUALITY_MIN 40
49#define QUALITY_MAX 70
50#define QUALITY_DEF 50
51
52 u8 jpeg_hdr[JPEG_HDR_SZ]; 47 u8 jpeg_hdr[JPEG_HDR_SZ];
53}; 48};
54 49
@@ -194,9 +189,6 @@ static int mars_s_ctrl(struct v4l2_ctrl *ctrl)
194 case V4L2_CID_SHARPNESS: 189 case V4L2_CID_SHARPNESS:
195 setsharpness(gspca_dev, ctrl->val); 190 setsharpness(gspca_dev, ctrl->val);
196 break; 191 break;
197 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
198 jpeg_set_qual(sd->jpeg_hdr, ctrl->val);
199 break;
200 default: 192 default:
201 return -EINVAL; 193 return -EINVAL;
202 } 194 }
@@ -214,7 +206,7 @@ static int sd_init_controls(struct gspca_dev *gspca_dev)
214 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler; 206 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
215 207
216 gspca_dev->vdev.ctrl_handler = hdl; 208 gspca_dev->vdev.ctrl_handler = hdl;
217 v4l2_ctrl_handler_init(hdl, 7); 209 v4l2_ctrl_handler_init(hdl, 6);
218 sd->brightness = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops, 210 sd->brightness = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
219 V4L2_CID_BRIGHTNESS, 0, 30, 1, 15); 211 V4L2_CID_BRIGHTNESS, 0, 30, 1, 15);
220 sd->saturation = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops, 212 sd->saturation = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
@@ -229,9 +221,6 @@ static int sd_init_controls(struct gspca_dev *gspca_dev)
229 sd->illum_bottom = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops, 221 sd->illum_bottom = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
230 V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0); 222 V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0);
231 sd->illum_bottom->flags |= V4L2_CTRL_FLAG_UPDATE; 223 sd->illum_bottom->flags |= V4L2_CTRL_FLAG_UPDATE;
232 sd->jpegqual = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
233 V4L2_CID_JPEG_COMPRESSION_QUALITY,
234 QUALITY_MIN, QUALITY_MAX, 1, QUALITY_DEF);
235 if (hdl->error) { 224 if (hdl->error) {
236 pr_err("Could not initialize controls\n"); 225 pr_err("Could not initialize controls\n");
237 return hdl->error; 226 return hdl->error;
@@ -244,13 +233,11 @@ static int sd_init_controls(struct gspca_dev *gspca_dev)
244static int sd_config(struct gspca_dev *gspca_dev, 233static int sd_config(struct gspca_dev *gspca_dev,
245 const struct usb_device_id *id) 234 const struct usb_device_id *id)
246{ 235{
247 struct sd *sd = (struct sd *) gspca_dev;
248 struct cam *cam; 236 struct cam *cam;
249 237
250 cam = &gspca_dev->cam; 238 cam = &gspca_dev->cam;
251 cam->cam_mode = vga_mode; 239 cam->cam_mode = vga_mode;
252 cam->nmodes = ARRAY_SIZE(vga_mode); 240 cam->nmodes = ARRAY_SIZE(vga_mode);
253 sd->quality = QUALITY_DEF;
254 return 0; 241 return 0;
255} 242}
256 243
@@ -269,7 +256,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
269 /* create the JPEG header */ 256 /* create the JPEG header */
270 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width, 257 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
271 0x21); /* JPEG 422 */ 258 0x21); /* JPEG 422 */
272 jpeg_set_qual(sd->jpeg_hdr, v4l2_ctrl_g_ctrl(sd->jpegqual)); 259 jpeg_set_qual(sd->jpeg_hdr, QUALITY);
273 260
274 data = gspca_dev->usb_buf; 261 data = gspca_dev->usb_buf;
275 262
@@ -411,31 +398,6 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
411 gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 398 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
412} 399}
413 400
414static int sd_set_jcomp(struct gspca_dev *gspca_dev,
415 struct v4l2_jpegcompression *jcomp)
416{
417 struct sd *sd = (struct sd *) gspca_dev;
418 int ret;
419
420 ret = v4l2_ctrl_s_ctrl(sd->jpegqual, jcomp->quality);
421 if (ret)
422 return ret;
423 jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
424 return 0;
425}
426
427static int sd_get_jcomp(struct gspca_dev *gspca_dev,
428 struct v4l2_jpegcompression *jcomp)
429{
430 struct sd *sd = (struct sd *) gspca_dev;
431
432 memset(jcomp, 0, sizeof *jcomp);
433 jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
434 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
435 | V4L2_JPEG_MARKER_DQT;
436 return 0;
437}
438
439/* sub-driver description */ 401/* sub-driver description */
440static const struct sd_desc sd_desc = { 402static const struct sd_desc sd_desc = {
441 .name = MODULE_NAME, 403 .name = MODULE_NAME,
@@ -445,8 +407,6 @@ static const struct sd_desc sd_desc = {
445 .start = sd_start, 407 .start = sd_start,
446 .stopN = sd_stopN, 408 .stopN = sd_stopN,
447 .pkt_scan = sd_pkt_scan, 409 .pkt_scan = sd_pkt_scan,
448 .get_jcomp = sd_get_jcomp,
449 .set_jcomp = sd_set_jcomp,
450}; 410};
451 411
452/* -- module initialisation -- */ 412/* -- module initialisation -- */
diff --git a/drivers/media/video/gspca/mr97310a.c b/drivers/media/video/gspca/mr97310a.c
index d73e5bd3dbf7..8f4714df5990 100644
--- a/drivers/media/video/gspca/mr97310a.c
+++ b/drivers/media/video/gspca/mr97310a.c
@@ -67,6 +67,7 @@
67#define MR97310A_CS_GAIN_MAX 0x7ff 67#define MR97310A_CS_GAIN_MAX 0x7ff
68#define MR97310A_CS_GAIN_DEFAULT 0x110 68#define MR97310A_CS_GAIN_DEFAULT 0x110
69 69
70#define MR97310A_CID_CLOCKDIV (V4L2_CTRL_CLASS_USER + 0x1000)
70#define MR97310A_MIN_CLOCKDIV_MIN 3 71#define MR97310A_MIN_CLOCKDIV_MIN 3
71#define MR97310A_MIN_CLOCKDIV_MAX 8 72#define MR97310A_MIN_CLOCKDIV_MAX 8
72#define MR97310A_MIN_CLOCKDIV_DEFAULT 3 73#define MR97310A_MIN_CLOCKDIV_DEFAULT 3
@@ -84,17 +85,15 @@ MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
84/* specific webcam descriptor */ 85/* specific webcam descriptor */
85struct sd { 86struct sd {
86 struct gspca_dev gspca_dev; /* !! must be the first item */ 87 struct gspca_dev gspca_dev; /* !! must be the first item */
88 struct { /* exposure/min_clockdiv control cluster */
89 struct v4l2_ctrl *exposure;
90 struct v4l2_ctrl *min_clockdiv;
91 };
87 u8 sof_read; 92 u8 sof_read;
88 u8 cam_type; /* 0 is CIF and 1 is VGA */ 93 u8 cam_type; /* 0 is CIF and 1 is VGA */
89 u8 sensor_type; /* We use 0 and 1 here, too. */ 94 u8 sensor_type; /* We use 0 and 1 here, too. */
90 u8 do_lcd_stop; 95 u8 do_lcd_stop;
91 u8 adj_colors; 96 u8 adj_colors;
92
93 int brightness;
94 u16 exposure;
95 u32 gain;
96 u8 contrast;
97 u8 min_clockdiv;
98}; 97};
99 98
100struct sensor_w_data { 99struct sensor_w_data {
@@ -105,132 +104,6 @@ struct sensor_w_data {
105}; 104};
106 105
107static void sd_stopN(struct gspca_dev *gspca_dev); 106static void sd_stopN(struct gspca_dev *gspca_dev);
108static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
109static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
110static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
111static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
112static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
113static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
114static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
115static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
116static int sd_setmin_clockdiv(struct gspca_dev *gspca_dev, __s32 val);
117static int sd_getmin_clockdiv(struct gspca_dev *gspca_dev, __s32 *val);
118static void setbrightness(struct gspca_dev *gspca_dev);
119static void setexposure(struct gspca_dev *gspca_dev);
120static void setgain(struct gspca_dev *gspca_dev);
121static void setcontrast(struct gspca_dev *gspca_dev);
122
123/* V4L2 controls supported by the driver */
124static const struct ctrl sd_ctrls[] = {
125/* Separate brightness control description for Argus QuickClix as it has
126 * different limits from the other mr97310a cameras, and separate gain
127 * control for Sakar CyberPix camera. */
128 {
129#define NORM_BRIGHTNESS_IDX 0
130 {
131 .id = V4L2_CID_BRIGHTNESS,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Brightness",
134 .minimum = -254,
135 .maximum = 255,
136 .step = 1,
137 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
138 .flags = 0,
139 },
140 .set = sd_setbrightness,
141 .get = sd_getbrightness,
142 },
143 {
144#define ARGUS_QC_BRIGHTNESS_IDX 1
145 {
146 .id = V4L2_CID_BRIGHTNESS,
147 .type = V4L2_CTRL_TYPE_INTEGER,
148 .name = "Brightness",
149 .minimum = 0,
150 .maximum = 15,
151 .step = 1,
152 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
153 .flags = 0,
154 },
155 .set = sd_setbrightness,
156 .get = sd_getbrightness,
157 },
158 {
159#define EXPOSURE_IDX 2
160 {
161 .id = V4L2_CID_EXPOSURE,
162 .type = V4L2_CTRL_TYPE_INTEGER,
163 .name = "Exposure",
164 .minimum = MR97310A_EXPOSURE_MIN,
165 .maximum = MR97310A_EXPOSURE_MAX,
166 .step = 1,
167 .default_value = MR97310A_EXPOSURE_DEFAULT,
168 .flags = 0,
169 },
170 .set = sd_setexposure,
171 .get = sd_getexposure,
172 },
173 {
174#define GAIN_IDX 3
175 {
176 .id = V4L2_CID_GAIN,
177 .type = V4L2_CTRL_TYPE_INTEGER,
178 .name = "Gain",
179 .minimum = MR97310A_GAIN_MIN,
180 .maximum = MR97310A_GAIN_MAX,
181 .step = 1,
182 .default_value = MR97310A_GAIN_DEFAULT,
183 .flags = 0,
184 },
185 .set = sd_setgain,
186 .get = sd_getgain,
187 },
188 {
189#define SAKAR_CS_GAIN_IDX 4
190 {
191 .id = V4L2_CID_GAIN,
192 .type = V4L2_CTRL_TYPE_INTEGER,
193 .name = "Gain",
194 .minimum = MR97310A_CS_GAIN_MIN,
195 .maximum = MR97310A_CS_GAIN_MAX,
196 .step = 1,
197 .default_value = MR97310A_CS_GAIN_DEFAULT,
198 .flags = 0,
199 },
200 .set = sd_setgain,
201 .get = sd_getgain,
202 },
203 {
204#define CONTRAST_IDX 5
205 {
206 .id = V4L2_CID_CONTRAST,
207 .type = V4L2_CTRL_TYPE_INTEGER,
208 .name = "Contrast",
209 .minimum = MR97310A_CONTRAST_MIN,
210 .maximum = MR97310A_CONTRAST_MAX,
211 .step = 1,
212 .default_value = MR97310A_CONTRAST_DEFAULT,
213 .flags = 0,
214 },
215 .set = sd_setcontrast,
216 .get = sd_getcontrast,
217 },
218 {
219#define MIN_CLOCKDIV_IDX 6
220 {
221 .id = V4L2_CID_PRIVATE_BASE,
222 .type = V4L2_CTRL_TYPE_INTEGER,
223 .name = "Minimum Clock Divider",
224 .minimum = MR97310A_MIN_CLOCKDIV_MIN,
225 .maximum = MR97310A_MIN_CLOCKDIV_MAX,
226 .step = 1,
227 .default_value = MR97310A_MIN_CLOCKDIV_DEFAULT,
228 .flags = 0,
229 },
230 .set = sd_setmin_clockdiv,
231 .get = sd_getmin_clockdiv,
232 },
233};
234 107
235static const struct v4l2_pix_format vga_mode[] = { 108static const struct v4l2_pix_format vga_mode[] = {
236 {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE, 109 {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
@@ -481,7 +354,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
481{ 354{
482 struct sd *sd = (struct sd *) gspca_dev; 355 struct sd *sd = (struct sd *) gspca_dev;
483 struct cam *cam; 356 struct cam *cam;
484 int gain_default = MR97310A_GAIN_DEFAULT;
485 int err_code; 357 int err_code;
486 358
487 cam = &gspca_dev->cam; 359 cam = &gspca_dev->cam;
@@ -615,52 +487,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
615 sd->sensor_type); 487 sd->sensor_type);
616 } 488 }
617 489
618 /* Setup controls depending on camera type */
619 if (sd->cam_type == CAM_TYPE_CIF) {
620 /* No brightness for sensor_type 0 */
621 if (sd->sensor_type == 0)
622 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
623 (1 << ARGUS_QC_BRIGHTNESS_IDX) |
624 (1 << CONTRAST_IDX) |
625 (1 << SAKAR_CS_GAIN_IDX);
626 else
627 gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX) |
628 (1 << CONTRAST_IDX) |
629 (1 << SAKAR_CS_GAIN_IDX) |
630 (1 << MIN_CLOCKDIV_IDX);
631 } else {
632 /* All controls need to be disabled if VGA sensor_type is 0 */
633 if (sd->sensor_type == 0)
634 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
635 (1 << ARGUS_QC_BRIGHTNESS_IDX) |
636 (1 << EXPOSURE_IDX) |
637 (1 << GAIN_IDX) |
638 (1 << CONTRAST_IDX) |
639 (1 << SAKAR_CS_GAIN_IDX) |
640 (1 << MIN_CLOCKDIV_IDX);
641 else if (sd->sensor_type == 2) {
642 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
643 (1 << ARGUS_QC_BRIGHTNESS_IDX) |
644 (1 << GAIN_IDX) |
645 (1 << MIN_CLOCKDIV_IDX);
646 gain_default = MR97310A_CS_GAIN_DEFAULT;
647 } else if (sd->do_lcd_stop)
648 /* Argus QuickClix has different brightness limits */
649 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
650 (1 << CONTRAST_IDX) |
651 (1 << SAKAR_CS_GAIN_IDX);
652 else
653 gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX) |
654 (1 << CONTRAST_IDX) |
655 (1 << SAKAR_CS_GAIN_IDX);
656 }
657
658 sd->brightness = MR97310A_BRIGHTNESS_DEFAULT;
659 sd->exposure = MR97310A_EXPOSURE_DEFAULT;
660 sd->gain = gain_default;
661 sd->contrast = MR97310A_CONTRAST_DEFAULT;
662 sd->min_clockdiv = MR97310A_MIN_CLOCKDIV_DEFAULT;
663
664 return 0; 490 return 0;
665} 491}
666 492
@@ -952,11 +778,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
952 if (err_code < 0) 778 if (err_code < 0)
953 return err_code; 779 return err_code;
954 780
955 setbrightness(gspca_dev);
956 setcontrast(gspca_dev);
957 setexposure(gspca_dev);
958 setgain(gspca_dev);
959
960 return isoc_enable(gspca_dev); 781 return isoc_enable(gspca_dev);
961} 782}
962 783
@@ -971,37 +792,25 @@ static void sd_stopN(struct gspca_dev *gspca_dev)
971 lcd_stop(gspca_dev); 792 lcd_stop(gspca_dev);
972} 793}
973 794
974static void setbrightness(struct gspca_dev *gspca_dev) 795static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
975{ 796{
976 struct sd *sd = (struct sd *) gspca_dev; 797 struct sd *sd = (struct sd *) gspca_dev;
977 u8 val;
978 u8 sign_reg = 7; /* This reg and the next one used on CIF cams. */ 798 u8 sign_reg = 7; /* This reg and the next one used on CIF cams. */
979 u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */ 799 u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */
980 static const u8 quick_clix_table[] = 800 static const u8 quick_clix_table[] =
981 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 801 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
982 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15}; 802 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15};
983 /*
984 * This control is disabled for CIF type 1 and VGA type 0 cameras.
985 * It does not quite act linearly for the Argus QuickClix camera,
986 * but it does control brightness. The values are 0 - 15 only, and
987 * the table above makes them act consecutively.
988 */
989 if ((gspca_dev->ctrl_dis & (1 << NORM_BRIGHTNESS_IDX)) &&
990 (gspca_dev->ctrl_dis & (1 << ARGUS_QC_BRIGHTNESS_IDX)))
991 return;
992
993 if (sd->cam_type == CAM_TYPE_VGA) { 803 if (sd->cam_type == CAM_TYPE_VGA) {
994 sign_reg += 4; 804 sign_reg += 4;
995 value_reg += 4; 805 value_reg += 4;
996 } 806 }
997 807
998 /* Note register 7 is also seen as 0x8x or 0xCx in some dumps */ 808 /* Note register 7 is also seen as 0x8x or 0xCx in some dumps */
999 if (sd->brightness > 0) { 809 if (val > 0) {
1000 sensor_write1(gspca_dev, sign_reg, 0x00); 810 sensor_write1(gspca_dev, sign_reg, 0x00);
1001 val = sd->brightness;
1002 } else { 811 } else {
1003 sensor_write1(gspca_dev, sign_reg, 0x01); 812 sensor_write1(gspca_dev, sign_reg, 0x01);
1004 val = (257 - sd->brightness); 813 val = 257 - val;
1005 } 814 }
1006 /* Use lookup table for funky Argus QuickClix brightness */ 815 /* Use lookup table for funky Argus QuickClix brightness */
1007 if (sd->do_lcd_stop) 816 if (sd->do_lcd_stop)
@@ -1010,23 +819,20 @@ static void setbrightness(struct gspca_dev *gspca_dev)
1010 sensor_write1(gspca_dev, value_reg, val); 819 sensor_write1(gspca_dev, value_reg, val);
1011} 820}
1012 821
1013static void setexposure(struct gspca_dev *gspca_dev) 822static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 min_clockdiv)
1014{ 823{
1015 struct sd *sd = (struct sd *) gspca_dev; 824 struct sd *sd = (struct sd *) gspca_dev;
1016 int exposure = MR97310A_EXPOSURE_DEFAULT; 825 int exposure = MR97310A_EXPOSURE_DEFAULT;
1017 u8 buf[2]; 826 u8 buf[2];
1018 827
1019 if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX))
1020 return;
1021
1022 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) { 828 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
1023 /* This cam does not like exposure settings < 300, 829 /* This cam does not like exposure settings < 300,
1024 so scale 0 - 4095 to 300 - 4095 */ 830 so scale 0 - 4095 to 300 - 4095 */
1025 exposure = (sd->exposure * 9267) / 10000 + 300; 831 exposure = (expo * 9267) / 10000 + 300;
1026 sensor_write1(gspca_dev, 3, exposure >> 4); 832 sensor_write1(gspca_dev, 3, exposure >> 4);
1027 sensor_write1(gspca_dev, 4, exposure & 0x0f); 833 sensor_write1(gspca_dev, 4, exposure & 0x0f);
1028 } else if (sd->sensor_type == 2) { 834 } else if (sd->sensor_type == 2) {
1029 exposure = sd->exposure; 835 exposure = expo;
1030 exposure >>= 3; 836 exposure >>= 3;
1031 sensor_write1(gspca_dev, 3, exposure >> 8); 837 sensor_write1(gspca_dev, 3, exposure >> 8);
1032 sensor_write1(gspca_dev, 4, exposure & 0xff); 838 sensor_write1(gspca_dev, 4, exposure & 0xff);
@@ -1038,11 +844,11 @@ static void setexposure(struct gspca_dev *gspca_dev)
1038 844
1039 Note our 0 - 4095 exposure is mapped to 0 - 511 845 Note our 0 - 4095 exposure is mapped to 0 - 511
1040 milliseconds exposure time */ 846 milliseconds exposure time */
1041 u8 clockdiv = (60 * sd->exposure + 7999) / 8000; 847 u8 clockdiv = (60 * expo + 7999) / 8000;
1042 848
1043 /* Limit framerate to not exceed usb bandwidth */ 849 /* Limit framerate to not exceed usb bandwidth */
1044 if (clockdiv < sd->min_clockdiv && gspca_dev->width >= 320) 850 if (clockdiv < min_clockdiv && gspca_dev->width >= 320)
1045 clockdiv = sd->min_clockdiv; 851 clockdiv = min_clockdiv;
1046 else if (clockdiv < 2) 852 else if (clockdiv < 2)
1047 clockdiv = 2; 853 clockdiv = 2;
1048 854
@@ -1051,7 +857,7 @@ static void setexposure(struct gspca_dev *gspca_dev)
1051 857
1052 /* Frame exposure time in ms = 1000 * clockdiv / 60 -> 858 /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
1053 exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */ 859 exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
1054 exposure = (60 * 511 * sd->exposure) / (8000 * clockdiv); 860 exposure = (60 * 511 * expo) / (8000 * clockdiv);
1055 if (exposure > 511) 861 if (exposure > 511)
1056 exposure = 511; 862 exposure = 511;
1057 863
@@ -1065,125 +871,148 @@ static void setexposure(struct gspca_dev *gspca_dev)
1065 } 871 }
1066} 872}
1067 873
1068static void setgain(struct gspca_dev *gspca_dev) 874static void setgain(struct gspca_dev *gspca_dev, s32 val)
1069{ 875{
1070 struct sd *sd = (struct sd *) gspca_dev; 876 struct sd *sd = (struct sd *) gspca_dev;
1071 u8 gainreg; 877 u8 gainreg;
1072 878
1073 if ((gspca_dev->ctrl_dis & (1 << GAIN_IDX)) &&
1074 (gspca_dev->ctrl_dis & (1 << SAKAR_CS_GAIN_IDX)))
1075 return;
1076
1077 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) 879 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1)
1078 sensor_write1(gspca_dev, 0x0e, sd->gain); 880 sensor_write1(gspca_dev, 0x0e, val);
1079 else if (sd->cam_type == CAM_TYPE_VGA && sd->sensor_type == 2) 881 else if (sd->cam_type == CAM_TYPE_VGA && sd->sensor_type == 2)
1080 for (gainreg = 0x0a; gainreg < 0x11; gainreg += 2) { 882 for (gainreg = 0x0a; gainreg < 0x11; gainreg += 2) {
1081 sensor_write1(gspca_dev, gainreg, sd->gain >> 8); 883 sensor_write1(gspca_dev, gainreg, val >> 8);
1082 sensor_write1(gspca_dev, gainreg + 1, sd->gain & 0xff); 884 sensor_write1(gspca_dev, gainreg + 1, val & 0xff);
1083 } 885 }
1084 else 886 else
1085 sensor_write1(gspca_dev, 0x10, sd->gain); 887 sensor_write1(gspca_dev, 0x10, val);
1086}
1087
1088static void setcontrast(struct gspca_dev *gspca_dev)
1089{
1090 struct sd *sd = (struct sd *) gspca_dev;
1091
1092 if (gspca_dev->ctrl_dis & (1 << CONTRAST_IDX))
1093 return;
1094
1095 sensor_write1(gspca_dev, 0x1c, sd->contrast);
1096}
1097
1098
1099static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1100{
1101 struct sd *sd = (struct sd *) gspca_dev;
1102
1103 sd->brightness = val;
1104 if (gspca_dev->streaming)
1105 setbrightness(gspca_dev);
1106 return 0;
1107}
1108
1109static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1110{
1111 struct sd *sd = (struct sd *) gspca_dev;
1112
1113 *val = sd->brightness;
1114 return 0;
1115}
1116
1117static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1118{
1119 struct sd *sd = (struct sd *) gspca_dev;
1120
1121 sd->exposure = val;
1122 if (gspca_dev->streaming)
1123 setexposure(gspca_dev);
1124 return 0;
1125} 888}
1126 889
1127static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val) 890static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
1128{ 891{
1129 struct sd *sd = (struct sd *) gspca_dev; 892 sensor_write1(gspca_dev, 0x1c, val);
1130
1131 *val = sd->exposure;
1132 return 0;
1133} 893}
1134 894
1135static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val) 895static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1136{ 896{
1137 struct sd *sd = (struct sd *) gspca_dev; 897 struct gspca_dev *gspca_dev =
898 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
899 struct sd *sd = (struct sd *)gspca_dev;
1138 900
1139 sd->gain = val; 901 gspca_dev->usb_err = 0;
1140 if (gspca_dev->streaming)
1141 setgain(gspca_dev);
1142 return 0;
1143}
1144 902
1145static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val) 903 if (!gspca_dev->streaming)
1146{ 904 return 0;
1147 struct sd *sd = (struct sd *) gspca_dev;
1148
1149 *val = sd->gain;
1150 return 0;
1151}
1152 905
1153static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val) 906 switch (ctrl->id) {
1154{ 907 case V4L2_CID_BRIGHTNESS:
1155 struct sd *sd = (struct sd *) gspca_dev; 908 setbrightness(gspca_dev, ctrl->val);
1156 909 break;
1157 sd->contrast = val; 910 case V4L2_CID_CONTRAST:
1158 if (gspca_dev->streaming) 911 setcontrast(gspca_dev, ctrl->val);
1159 setcontrast(gspca_dev); 912 break;
1160 return 0; 913 case V4L2_CID_EXPOSURE:
914 setexposure(gspca_dev, sd->exposure->val,
915 sd->min_clockdiv ? sd->min_clockdiv->val : 0);
916 break;
917 case V4L2_CID_GAIN:
918 setgain(gspca_dev, ctrl->val);
919 break;
920 }
921 return gspca_dev->usb_err;
1161} 922}
1162 923
924static const struct v4l2_ctrl_ops sd_ctrl_ops = {
925 .s_ctrl = sd_s_ctrl,
926};
1163 927
1164static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val) 928static int sd_init_controls(struct gspca_dev *gspca_dev)
1165{
1166 struct sd *sd = (struct sd *) gspca_dev;
1167
1168 *val = sd->contrast;
1169 return 0;
1170}
1171
1172static int sd_setmin_clockdiv(struct gspca_dev *gspca_dev, __s32 val)
1173{ 929{
1174 struct sd *sd = (struct sd *) gspca_dev; 930 struct sd *sd = (struct sd *)gspca_dev;
931 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
932 static const struct v4l2_ctrl_config clockdiv = {
933 .ops = &sd_ctrl_ops,
934 .id = MR97310A_CID_CLOCKDIV,
935 .type = V4L2_CTRL_TYPE_INTEGER,
936 .name = "Minimum Clock Divider",
937 .min = MR97310A_MIN_CLOCKDIV_MIN,
938 .max = MR97310A_MIN_CLOCKDIV_MAX,
939 .step = 1,
940 .def = MR97310A_MIN_CLOCKDIV_DEFAULT,
941 };
942 bool has_brightness = false;
943 bool has_argus_brightness = false;
944 bool has_contrast = false;
945 bool has_gain = false;
946 bool has_cs_gain = false;
947 bool has_exposure = false;
948 bool has_clockdiv = false;
1175 949
1176 sd->min_clockdiv = val; 950 gspca_dev->vdev.ctrl_handler = hdl;
1177 if (gspca_dev->streaming) 951 v4l2_ctrl_handler_init(hdl, 4);
1178 setexposure(gspca_dev);
1179 return 0;
1180}
1181 952
1182static int sd_getmin_clockdiv(struct gspca_dev *gspca_dev, __s32 *val) 953 /* Setup controls depending on camera type */
1183{ 954 if (sd->cam_type == CAM_TYPE_CIF) {
1184 struct sd *sd = (struct sd *) gspca_dev; 955 /* No brightness for sensor_type 0 */
956 if (sd->sensor_type == 0)
957 has_exposure = has_gain = has_clockdiv = true;
958 else
959 has_exposure = has_gain = has_brightness = true;
960 } else {
961 /* All controls need to be disabled if VGA sensor_type is 0 */
962 if (sd->sensor_type == 0)
963 ; /* no controls! */
964 else if (sd->sensor_type == 2)
965 has_exposure = has_cs_gain = has_contrast = true;
966 else if (sd->do_lcd_stop)
967 has_exposure = has_gain = has_argus_brightness =
968 has_clockdiv = true;
969 else
970 has_exposure = has_gain = has_brightness =
971 has_clockdiv = true;
972 }
1185 973
1186 *val = sd->min_clockdiv; 974 /* Separate brightness control description for Argus QuickClix as it has
975 * different limits from the other mr97310a cameras, and separate gain
976 * control for Sakar CyberPix camera. */
977 /*
978 * This control is disabled for CIF type 1 and VGA type 0 cameras.
979 * It does not quite act linearly for the Argus QuickClix camera,
980 * but it does control brightness. The values are 0 - 15 only, and
981 * the table above makes them act consecutively.
982 */
983 if (has_brightness)
984 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
985 V4L2_CID_BRIGHTNESS, -254, 255, 1,
986 MR97310A_BRIGHTNESS_DEFAULT);
987 else if (has_argus_brightness)
988 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
989 V4L2_CID_BRIGHTNESS, 0, 15, 1,
990 MR97310A_BRIGHTNESS_DEFAULT);
991 if (has_contrast)
992 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
993 V4L2_CID_CONTRAST, MR97310A_CONTRAST_MIN,
994 MR97310A_CONTRAST_MAX, 1, MR97310A_CONTRAST_DEFAULT);
995 if (has_gain)
996 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
997 V4L2_CID_GAIN, MR97310A_GAIN_MIN, MR97310A_GAIN_MAX,
998 1, MR97310A_GAIN_DEFAULT);
999 else if (has_cs_gain)
1000 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_GAIN,
1001 MR97310A_CS_GAIN_MIN, MR97310A_CS_GAIN_MAX,
1002 1, MR97310A_CS_GAIN_DEFAULT);
1003 if (has_exposure)
1004 sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1005 V4L2_CID_EXPOSURE, MR97310A_EXPOSURE_MIN,
1006 MR97310A_EXPOSURE_MAX, 1, MR97310A_EXPOSURE_DEFAULT);
1007 if (has_clockdiv)
1008 sd->min_clockdiv = v4l2_ctrl_new_custom(hdl, &clockdiv, NULL);
1009
1010 if (hdl->error) {
1011 pr_err("Could not initialize controls\n");
1012 return hdl->error;
1013 }
1014 if (has_exposure && has_clockdiv)
1015 v4l2_ctrl_cluster(2, &sd->exposure);
1187 return 0; 1016 return 0;
1188} 1017}
1189 1018
@@ -1221,10 +1050,9 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1221/* sub-driver description */ 1050/* sub-driver description */
1222static const struct sd_desc sd_desc = { 1051static const struct sd_desc sd_desc = {
1223 .name = MODULE_NAME, 1052 .name = MODULE_NAME,
1224 .ctrls = sd_ctrls,
1225 .nctrls = ARRAY_SIZE(sd_ctrls),
1226 .config = sd_config, 1053 .config = sd_config,
1227 .init = sd_init, 1054 .init = sd_init,
1055 .init_controls = sd_init_controls,
1228 .start = sd_start, 1056 .start = sd_start,
1229 .stopN = sd_stopN, 1057 .stopN = sd_stopN,
1230 .pkt_scan = sd_pkt_scan, 1058 .pkt_scan = sd_pkt_scan,
@@ -1256,6 +1084,7 @@ static struct usb_driver sd_driver = {
1256#ifdef CONFIG_PM 1084#ifdef CONFIG_PM
1257 .suspend = gspca_suspend, 1085 .suspend = gspca_suspend,
1258 .resume = gspca_resume, 1086 .resume = gspca_resume,
1087 .reset_resume = gspca_resume,
1259#endif 1088#endif
1260}; 1089};
1261 1090
diff --git a/drivers/media/video/gspca/nw80x.c b/drivers/media/video/gspca/nw80x.c
index 42e021931e60..44c9964b1b3e 100644
--- a/drivers/media/video/gspca/nw80x.c
+++ b/drivers/media/video/gspca/nw80x.c
@@ -32,22 +32,10 @@ MODULE_LICENSE("GPL");
32 32
33static int webcam; 33static int webcam;
34 34
35/* controls */
36enum e_ctrl {
37 GAIN,
38 EXPOSURE,
39 AUTOGAIN,
40 NCTRLS /* number of controls */
41};
42
43#define AUTOGAIN_DEF 1
44
45/* specific webcam descriptor */ 35/* specific webcam descriptor */
46struct sd { 36struct sd {
47 struct gspca_dev gspca_dev; /* !! must be the first item */ 37 struct gspca_dev gspca_dev; /* !! must be the first item */
48 38
49 struct gspca_ctrl ctrls[NCTRLS];
50
51 u32 ae_res; 39 u32 ae_res;
52 s8 ag_cnt; 40 s8 ag_cnt;
53#define AG_CNT_START 13 41#define AG_CNT_START 13
@@ -1667,17 +1655,13 @@ static int swap_bits(int v)
1667 return r; 1655 return r;
1668} 1656}
1669 1657
1670static void setgain(struct gspca_dev *gspca_dev) 1658static void setgain(struct gspca_dev *gspca_dev, u8 val)
1671{ 1659{
1672 struct sd *sd = (struct sd *) gspca_dev; 1660 struct sd *sd = (struct sd *) gspca_dev;
1673 u8 val, v[2]; 1661 u8 v[2];
1674 1662
1675 val = sd->ctrls[GAIN].val;
1676 switch (sd->webcam) { 1663 switch (sd->webcam) {
1677 case P35u: 1664 case P35u:
1678 /* Note the control goes from 0-255 not 0-127, but anything
1679 above 127 just means amplifying noise */
1680 val >>= 1; /* 0 - 255 -> 0 - 127 */
1681 reg_w(gspca_dev, 0x1026, &val, 1); 1665 reg_w(gspca_dev, 0x1026, &val, 1);
1682 break; 1666 break;
1683 case Kr651us: 1667 case Kr651us:
@@ -1690,13 +1674,11 @@ static void setgain(struct gspca_dev *gspca_dev)
1690 } 1674 }
1691} 1675}
1692 1676
1693static void setexposure(struct gspca_dev *gspca_dev) 1677static void setexposure(struct gspca_dev *gspca_dev, s32 val)
1694{ 1678{
1695 struct sd *sd = (struct sd *) gspca_dev; 1679 struct sd *sd = (struct sd *) gspca_dev;
1696 s16 val;
1697 u8 v[2]; 1680 u8 v[2];
1698 1681
1699 val = sd->ctrls[EXPOSURE].val;
1700 switch (sd->webcam) { 1682 switch (sd->webcam) {
1701 case P35u: 1683 case P35u:
1702 v[0] = ((9 - val) << 3) | 0x01; 1684 v[0] = ((9 - val) << 3) | 0x01;
@@ -1713,14 +1695,12 @@ static void setexposure(struct gspca_dev *gspca_dev)
1713 } 1695 }
1714} 1696}
1715 1697
1716static void setautogain(struct gspca_dev *gspca_dev) 1698static void setautogain(struct gspca_dev *gspca_dev, s32 val)
1717{ 1699{
1718 struct sd *sd = (struct sd *) gspca_dev; 1700 struct sd *sd = (struct sd *) gspca_dev;
1719 int w, h; 1701 int w, h;
1720 1702
1721 if (gspca_dev->ctrl_dis & (1 << AUTOGAIN)) 1703 if (!val) {
1722 return;
1723 if (!sd->ctrls[AUTOGAIN].val) {
1724 sd->ag_cnt = -1; 1704 sd->ag_cnt = -1;
1725 return; 1705 return;
1726 } 1706 }
@@ -1763,7 +1743,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
1763 if ((unsigned) webcam >= NWEBCAMS) 1743 if ((unsigned) webcam >= NWEBCAMS)
1764 webcam = 0; 1744 webcam = 0;
1765 sd->webcam = webcam; 1745 sd->webcam = webcam;
1766 gspca_dev->cam.ctrls = sd->ctrls;
1767 gspca_dev->cam.needs_full_bandwidth = 1; 1746 gspca_dev->cam.needs_full_bandwidth = 1;
1768 sd->ag_cnt = -1; 1747 sd->ag_cnt = -1;
1769 1748
@@ -1834,33 +1813,7 @@ static int sd_config(struct gspca_dev *gspca_dev,
1834 break; 1813 break;
1835 } 1814 }
1836 } 1815 }
1837 switch (sd->webcam) {
1838 case P35u:
1839/* sd->ctrls[EXPOSURE].max = 9;
1840 * sd->ctrls[EXPOSURE].def = 9; */
1841 /* coarse expo auto gain function gain minimum, to avoid
1842 * a large settings jump the first auto adjustment */
1843 sd->ctrls[GAIN].def = 255 / 5 * 2;
1844 break;
1845 case Cvideopro:
1846 case DvcV6:
1847 case Kritter:
1848 gspca_dev->ctrl_dis = (1 << GAIN) | (1 << AUTOGAIN);
1849 /* fall thru */
1850 case Kr651us:
1851 sd->ctrls[EXPOSURE].max = 315;
1852 sd->ctrls[EXPOSURE].def = 150;
1853 break;
1854 default:
1855 gspca_dev->ctrl_dis = (1 << GAIN) | (1 << EXPOSURE)
1856 | (1 << AUTOGAIN);
1857 break;
1858 }
1859 1816
1860#if AUTOGAIN_DEF
1861 if (!(gspca_dev->ctrl_dis & (1 << AUTOGAIN)))
1862 gspca_dev->ctrl_inac = (1 << GAIN) | (1 << EXPOSURE);
1863#endif
1864 return gspca_dev->usb_err; 1817 return gspca_dev->usb_err;
1865} 1818}
1866 1819
@@ -1925,9 +1878,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
1925 break; 1878 break;
1926 } 1879 }
1927 1880
1928 setgain(gspca_dev);
1929 setexposure(gspca_dev);
1930 setautogain(gspca_dev);
1931 sd->exp_too_high_cnt = 0; 1881 sd->exp_too_high_cnt = 0;
1932 sd->exp_too_low_cnt = 0; 1882 sd->exp_too_low_cnt = 0;
1933 return gspca_dev->usb_err; 1883 return gspca_dev->usb_err;
@@ -1987,24 +1937,6 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1987 } 1937 }
1988} 1938}
1989 1939
1990static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1991{
1992 struct sd *sd = (struct sd *) gspca_dev;
1993
1994 sd->ctrls[AUTOGAIN].val = val;
1995 if (val)
1996 gspca_dev->ctrl_inac = (1 << GAIN) | (1 << EXPOSURE);
1997 else
1998 gspca_dev->ctrl_inac = 0;
1999 if (gspca_dev->streaming)
2000 setautogain(gspca_dev);
2001 return gspca_dev->usb_err;
2002}
2003
2004#define WANT_REGULAR_AUTOGAIN
2005#define WANT_COARSE_EXPO_AUTOGAIN
2006#include "autogain_functions.h"
2007
2008static void do_autogain(struct gspca_dev *gspca_dev) 1940static void do_autogain(struct gspca_dev *gspca_dev)
2009{ 1941{
2010 struct sd *sd = (struct sd *) gspca_dev; 1942 struct sd *sd = (struct sd *) gspca_dev;
@@ -2024,62 +1956,100 @@ static void do_autogain(struct gspca_dev *gspca_dev)
2024 1956
2025 switch (sd->webcam) { 1957 switch (sd->webcam) {
2026 case P35u: 1958 case P35u:
2027 coarse_grained_expo_autogain(gspca_dev, luma, 100, 5); 1959 gspca_coarse_grained_expo_autogain(gspca_dev, luma, 100, 5);
2028 break; 1960 break;
2029 default: 1961 default:
2030 auto_gain_n_exposure(gspca_dev, luma, 100, 5, 230, 0); 1962 gspca_expo_autogain(gspca_dev, luma, 100, 5, 230, 0);
2031 break; 1963 break;
2032 } 1964 }
2033} 1965}
2034 1966
2035/* V4L2 controls supported by the driver */ 1967
2036static const struct ctrl sd_ctrls[NCTRLS] = { 1968static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
2037[GAIN] = { 1969{
2038 { 1970 struct gspca_dev *gspca_dev =
2039 .id = V4L2_CID_GAIN, 1971 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
2040 .type = V4L2_CTRL_TYPE_INTEGER, 1972
2041 .name = "Gain", 1973 gspca_dev->usb_err = 0;
2042 .minimum = 0, 1974
2043 .maximum = 253, 1975 if (!gspca_dev->streaming)
2044 .step = 1, 1976 return 0;
2045 .default_value = 128 1977
2046 }, 1978 switch (ctrl->id) {
2047 .set_control = setgain 1979 /* autogain/gain/exposure control cluster */
2048 }, 1980 case V4L2_CID_AUTOGAIN:
2049[EXPOSURE] = { 1981 if (ctrl->is_new)
2050 { 1982 setautogain(gspca_dev, ctrl->val);
2051 .id = V4L2_CID_EXPOSURE, 1983 if (!ctrl->val) {
2052 .type = V4L2_CTRL_TYPE_INTEGER, 1984 if (gspca_dev->gain->is_new)
2053 .name = "Exposure", 1985 setgain(gspca_dev, gspca_dev->gain->val);
2054 .minimum = 0, 1986 if (gspca_dev->exposure->is_new)
2055 .maximum = 9, 1987 setexposure(gspca_dev,
2056 .step = 1, 1988 gspca_dev->exposure->val);
2057 .default_value = 9 1989 }
2058 }, 1990 break;
2059 .set_control = setexposure 1991 /* Some webcams only have exposure, so handle that separately from the
2060 }, 1992 autogain/gain/exposure cluster in the previous case. */
2061[AUTOGAIN] = { 1993 case V4L2_CID_EXPOSURE:
2062 { 1994 setexposure(gspca_dev, gspca_dev->exposure->val);
2063 .id = V4L2_CID_AUTOGAIN, 1995 break;
2064 .type = V4L2_CTRL_TYPE_BOOLEAN, 1996 }
2065 .name = "Auto Gain", 1997 return gspca_dev->usb_err;
2066 .minimum = 0, 1998}
2067 .maximum = 1, 1999
2068 .step = 1, 2000static const struct v4l2_ctrl_ops sd_ctrl_ops = {
2069 .default_value = AUTOGAIN_DEF, 2001 .s_ctrl = sd_s_ctrl,
2070 .flags = V4L2_CTRL_FLAG_UPDATE
2071 },
2072 .set = sd_setautogain
2073 },
2074}; 2002};
2075 2003
2004static int sd_init_controls(struct gspca_dev *gspca_dev)
2005{
2006 struct sd *sd = (struct sd *)gspca_dev;
2007 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
2008
2009 gspca_dev->vdev.ctrl_handler = hdl;
2010 v4l2_ctrl_handler_init(hdl, 3);
2011 switch (sd->webcam) {
2012 case P35u:
2013 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
2014 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
2015 /* For P35u choose coarse expo auto gain function gain minimum,
2016 * to avoid a large settings jump the first auto adjustment */
2017 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
2018 V4L2_CID_GAIN, 0, 127, 1, 127 / 5 * 2);
2019 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
2020 V4L2_CID_EXPOSURE, 0, 9, 1, 9);
2021 break;
2022 case Kr651us:
2023 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
2024 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
2025 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
2026 V4L2_CID_GAIN, 0, 253, 1, 128);
2027 /* fall through */
2028 case Cvideopro:
2029 case DvcV6:
2030 case Kritter:
2031 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
2032 V4L2_CID_EXPOSURE, 0, 315, 1, 150);
2033 break;
2034 default:
2035 break;
2036 }
2037
2038 if (hdl->error) {
2039 pr_err("Could not initialize controls\n");
2040 return hdl->error;
2041 }
2042 if (gspca_dev->autogain)
2043 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
2044 return 0;
2045}
2046
2076/* sub-driver description */ 2047/* sub-driver description */
2077static const struct sd_desc sd_desc = { 2048static const struct sd_desc sd_desc = {
2078 .name = MODULE_NAME, 2049 .name = MODULE_NAME,
2079 .ctrls = sd_ctrls,
2080 .nctrls = ARRAY_SIZE(sd_ctrls),
2081 .config = sd_config, 2050 .config = sd_config,
2082 .init = sd_init, 2051 .init = sd_init,
2052 .init_controls = sd_init_controls,
2083 .start = sd_start, 2053 .start = sd_start,
2084 .stopN = sd_stopN, 2054 .stopN = sd_stopN,
2085 .pkt_scan = sd_pkt_scan, 2055 .pkt_scan = sd_pkt_scan,
@@ -2117,6 +2087,7 @@ static struct usb_driver sd_driver = {
2117#ifdef CONFIG_PM 2087#ifdef CONFIG_PM
2118 .suspend = gspca_suspend, 2088 .suspend = gspca_suspend,
2119 .resume = gspca_resume, 2089 .resume = gspca_resume,
2090 .reset_resume = gspca_resume,
2120#endif 2091#endif
2121}; 2092};
2122 2093
diff --git a/drivers/media/video/gspca/ov519.c b/drivers/media/video/gspca/ov519.c
index 183457c5cfdb..bfc7cefa59f8 100644
--- a/drivers/media/video/gspca/ov519.c
+++ b/drivers/media/video/gspca/ov519.c
@@ -60,25 +60,20 @@ static int frame_rate;
60 * are getting "Failed to read sensor ID..." */ 60 * are getting "Failed to read sensor ID..." */
61static int i2c_detect_tries = 10; 61static int i2c_detect_tries = 10;
62 62
63/* controls */
64enum e_ctrl {
65 BRIGHTNESS,
66 CONTRAST,
67 EXPOSURE,
68 COLORS,
69 HFLIP,
70 VFLIP,
71 AUTOBRIGHT,
72 AUTOGAIN,
73 FREQ,
74 NCTRL /* number of controls */
75};
76
77/* ov519 device descriptor */ 63/* ov519 device descriptor */
78struct sd { 64struct sd {
79 struct gspca_dev gspca_dev; /* !! must be the first item */ 65 struct gspca_dev gspca_dev; /* !! must be the first item */
80 66
81 struct gspca_ctrl ctrls[NCTRL]; 67 struct v4l2_ctrl *jpegqual;
68 struct v4l2_ctrl *freq;
69 struct { /* h/vflip control cluster */
70 struct v4l2_ctrl *hflip;
71 struct v4l2_ctrl *vflip;
72 };
73 struct { /* autobrightness/brightness control cluster */
74 struct v4l2_ctrl *autobright;
75 struct v4l2_ctrl *brightness;
76 };
82 77
83 u8 packet_nr; 78 u8 packet_nr;
84 79
@@ -101,7 +96,6 @@ struct sd {
101 /* Determined by sensor type */ 96 /* Determined by sensor type */
102 u8 sif; 97 u8 sif;
103 98
104 u8 quality;
105#define QUALITY_MIN 50 99#define QUALITY_MIN 50
106#define QUALITY_MAX 70 100#define QUALITY_MAX 70
107#define QUALITY_DEF 50 101#define QUALITY_DEF 50
@@ -145,209 +139,112 @@ enum sensors {
145 really should move the sensor drivers to v4l2 sub drivers. */ 139 really should move the sensor drivers to v4l2 sub drivers. */
146#include "w996Xcf.c" 140#include "w996Xcf.c"
147 141
148/* V4L2 controls supported by the driver */ 142/* table of the disabled controls */
149static void setbrightness(struct gspca_dev *gspca_dev); 143struct ctrl_valid {
150static void setcontrast(struct gspca_dev *gspca_dev); 144 int has_brightness:1;
151static void setexposure(struct gspca_dev *gspca_dev); 145 int has_contrast:1;
152static void setcolors(struct gspca_dev *gspca_dev); 146 int has_exposure:1;
153static void sethvflip(struct gspca_dev *gspca_dev); 147 int has_autogain:1;
154static void setautobright(struct gspca_dev *gspca_dev); 148 int has_sat:1;
155static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val); 149 int has_hvflip:1;
156static void setfreq(struct gspca_dev *gspca_dev); 150 int has_autobright:1;
157static void setfreq_i(struct sd *sd); 151 int has_freq:1;
158 152};
159static const struct ctrl sd_ctrls[] = { 153
160[BRIGHTNESS] = { 154static const struct ctrl_valid valid_controls[] = {
161 { 155 [SEN_OV2610] = {
162 .id = V4L2_CID_BRIGHTNESS, 156 .has_exposure = 1,
163 .type = V4L2_CTRL_TYPE_INTEGER, 157 .has_autogain = 1,
164 .name = "Brightness",
165 .minimum = 0,
166 .maximum = 255,
167 .step = 1,
168 .default_value = 127,
169 },
170 .set_control = setbrightness,
171 }, 158 },
172[CONTRAST] = { 159 [SEN_OV2610AE] = {
173 { 160 .has_exposure = 1,
174 .id = V4L2_CID_CONTRAST, 161 .has_autogain = 1,
175 .type = V4L2_CTRL_TYPE_INTEGER,
176 .name = "Contrast",
177 .minimum = 0,
178 .maximum = 255,
179 .step = 1,
180 .default_value = 127,
181 },
182 .set_control = setcontrast,
183 }, 162 },
184[EXPOSURE] = { 163 [SEN_OV3610] = {
185 { 164 /* No controls */
186 .id = V4L2_CID_EXPOSURE,
187 .type = V4L2_CTRL_TYPE_INTEGER,
188 .name = "Exposure",
189 .minimum = 0,
190 .maximum = 255,
191 .step = 1,
192 .default_value = 127,
193 },
194 .set_control = setexposure,
195 }, 165 },
196[COLORS] = { 166 [SEN_OV6620] = {
197 { 167 .has_brightness = 1,
198 .id = V4L2_CID_SATURATION, 168 .has_contrast = 1,
199 .type = V4L2_CTRL_TYPE_INTEGER, 169 .has_sat = 1,
200 .name = "Color", 170 .has_autobright = 1,
201 .minimum = 0, 171 .has_freq = 1,
202 .maximum = 255,
203 .step = 1,
204 .default_value = 127,
205 },
206 .set_control = setcolors,
207 }, 172 },
208/* The flip controls work for sensors ov7660 and ov7670 only */ 173 [SEN_OV6630] = {
209[HFLIP] = { 174 .has_brightness = 1,
210 { 175 .has_contrast = 1,
211 .id = V4L2_CID_HFLIP, 176 .has_sat = 1,
212 .type = V4L2_CTRL_TYPE_BOOLEAN, 177 .has_autobright = 1,
213 .name = "Mirror", 178 .has_freq = 1,
214 .minimum = 0,
215 .maximum = 1,
216 .step = 1,
217 .default_value = 0,
218 },
219 .set_control = sethvflip,
220 }, 179 },
221[VFLIP] = { 180 [SEN_OV66308AF] = {
222 { 181 .has_brightness = 1,
223 .id = V4L2_CID_VFLIP, 182 .has_contrast = 1,
224 .type = V4L2_CTRL_TYPE_BOOLEAN, 183 .has_sat = 1,
225 .name = "Vflip", 184 .has_autobright = 1,
226 .minimum = 0, 185 .has_freq = 1,
227 .maximum = 1,
228 .step = 1,
229 .default_value = 0,
230 },
231 .set_control = sethvflip,
232 }, 186 },
233[AUTOBRIGHT] = { 187 [SEN_OV7610] = {
234 { 188 .has_brightness = 1,
235 .id = V4L2_CID_AUTOBRIGHTNESS, 189 .has_contrast = 1,
236 .type = V4L2_CTRL_TYPE_BOOLEAN, 190 .has_sat = 1,
237 .name = "Auto Brightness", 191 .has_autobright = 1,
238 .minimum = 0, 192 .has_freq = 1,
239 .maximum = 1,
240 .step = 1,
241 .default_value = 1,
242 },
243 .set_control = setautobright,
244 }, 193 },
245[AUTOGAIN] = { 194 [SEN_OV7620] = {
246 { 195 .has_brightness = 1,
247 .id = V4L2_CID_AUTOGAIN, 196 .has_contrast = 1,
248 .type = V4L2_CTRL_TYPE_BOOLEAN, 197 .has_sat = 1,
249 .name = "Auto Gain", 198 .has_autobright = 1,
250 .minimum = 0, 199 .has_freq = 1,
251 .maximum = 1,
252 .step = 1,
253 .default_value = 1,
254 .flags = V4L2_CTRL_FLAG_UPDATE
255 },
256 .set = sd_setautogain,
257 }, 200 },
258[FREQ] = { 201 [SEN_OV7620AE] = {
259 { 202 .has_brightness = 1,
260 .id = V4L2_CID_POWER_LINE_FREQUENCY, 203 .has_contrast = 1,
261 .type = V4L2_CTRL_TYPE_MENU, 204 .has_sat = 1,
262 .name = "Light frequency filter", 205 .has_autobright = 1,
263 .minimum = 0, 206 .has_freq = 1,
264 .maximum = 2, /* 0: no flicker, 1: 50Hz, 2:60Hz, 3: auto */ 207 },
265 .step = 1, 208 [SEN_OV7640] = {
266 .default_value = 0, 209 .has_brightness = 1,
267 }, 210 .has_sat = 1,
268 .set_control = setfreq, 211 .has_freq = 1,
212 },
213 [SEN_OV7648] = {
214 .has_brightness = 1,
215 .has_sat = 1,
216 .has_freq = 1,
217 },
218 [SEN_OV7660] = {
219 .has_brightness = 1,
220 .has_contrast = 1,
221 .has_sat = 1,
222 .has_hvflip = 1,
223 .has_freq = 1,
224 },
225 [SEN_OV7670] = {
226 .has_brightness = 1,
227 .has_contrast = 1,
228 .has_hvflip = 1,
229 .has_freq = 1,
230 },
231 [SEN_OV76BE] = {
232 .has_brightness = 1,
233 .has_contrast = 1,
234 .has_sat = 1,
235 .has_autobright = 1,
236 .has_freq = 1,
237 },
238 [SEN_OV8610] = {
239 .has_brightness = 1,
240 .has_contrast = 1,
241 .has_sat = 1,
242 .has_autobright = 1,
243 },
244 [SEN_OV9600] = {
245 .has_exposure = 1,
246 .has_autogain = 1,
269 }, 247 },
270};
271
272/* table of the disabled controls */
273static const unsigned ctrl_dis[] = {
274[SEN_OV2610] = ((1 << NCTRL) - 1) /* no control */
275 ^ ((1 << EXPOSURE) /* but exposure */
276 | (1 << AUTOGAIN)), /* and autogain */
277
278[SEN_OV2610AE] = ((1 << NCTRL) - 1) /* no control */
279 ^ ((1 << EXPOSURE) /* but exposure */
280 | (1 << AUTOGAIN)), /* and autogain */
281
282[SEN_OV3610] = (1 << NCTRL) - 1, /* no control */
283
284[SEN_OV6620] = (1 << HFLIP) |
285 (1 << VFLIP) |
286 (1 << EXPOSURE) |
287 (1 << AUTOGAIN),
288
289[SEN_OV6630] = (1 << HFLIP) |
290 (1 << VFLIP) |
291 (1 << EXPOSURE) |
292 (1 << AUTOGAIN),
293
294[SEN_OV66308AF] = (1 << HFLIP) |
295 (1 << VFLIP) |
296 (1 << EXPOSURE) |
297 (1 << AUTOGAIN),
298
299[SEN_OV7610] = (1 << HFLIP) |
300 (1 << VFLIP) |
301 (1 << EXPOSURE) |
302 (1 << AUTOGAIN),
303
304[SEN_OV7620] = (1 << HFLIP) |
305 (1 << VFLIP) |
306 (1 << EXPOSURE) |
307 (1 << AUTOGAIN),
308
309[SEN_OV7620AE] = (1 << HFLIP) |
310 (1 << VFLIP) |
311 (1 << EXPOSURE) |
312 (1 << AUTOGAIN),
313
314[SEN_OV7640] = (1 << HFLIP) |
315 (1 << VFLIP) |
316 (1 << AUTOBRIGHT) |
317 (1 << CONTRAST) |
318 (1 << EXPOSURE) |
319 (1 << AUTOGAIN),
320
321[SEN_OV7648] = (1 << HFLIP) |
322 (1 << VFLIP) |
323 (1 << AUTOBRIGHT) |
324 (1 << CONTRAST) |
325 (1 << EXPOSURE) |
326 (1 << AUTOGAIN),
327
328[SEN_OV7660] = (1 << AUTOBRIGHT) |
329 (1 << EXPOSURE) |
330 (1 << AUTOGAIN),
331
332[SEN_OV7670] = (1 << COLORS) |
333 (1 << AUTOBRIGHT) |
334 (1 << EXPOSURE) |
335 (1 << AUTOGAIN),
336
337[SEN_OV76BE] = (1 << HFLIP) |
338 (1 << VFLIP) |
339 (1 << EXPOSURE) |
340 (1 << AUTOGAIN),
341
342[SEN_OV8610] = (1 << HFLIP) |
343 (1 << VFLIP) |
344 (1 << EXPOSURE) |
345 (1 << AUTOGAIN) |
346 (1 << FREQ),
347[SEN_OV9600] = ((1 << NCTRL) - 1) /* no control */
348 ^ ((1 << EXPOSURE) /* but exposure */
349 | (1 << AUTOGAIN)), /* and autogain */
350
351}; 248};
352 249
353static const struct v4l2_pix_format ov519_vga_mode[] = { 250static const struct v4l2_pix_format ov519_vga_mode[] = {
@@ -3306,11 +3203,11 @@ static void ov519_set_fr(struct sd *sd)
3306 ov518_i2c_w(sd, OV7670_R11_CLKRC, clock); 3203 ov518_i2c_w(sd, OV7670_R11_CLKRC, clock);
3307} 3204}
3308 3205
3309static void setautogain(struct gspca_dev *gspca_dev) 3206static void setautogain(struct gspca_dev *gspca_dev, s32 val)
3310{ 3207{
3311 struct sd *sd = (struct sd *) gspca_dev; 3208 struct sd *sd = (struct sd *) gspca_dev;
3312 3209
3313 i2c_w_mask(sd, 0x13, sd->ctrls[AUTOGAIN].val ? 0x05 : 0x00, 0x05); 3210 i2c_w_mask(sd, 0x13, val ? 0x05 : 0x00, 0x05);
3314} 3211}
3315 3212
3316/* this function is called at probe time */ 3213/* this function is called at probe time */
@@ -3351,8 +3248,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
3351 break; 3248 break;
3352 } 3249 }
3353 3250
3354 gspca_dev->cam.ctrls = sd->ctrls;
3355 sd->quality = QUALITY_DEF;
3356 sd->frame_rate = 15; 3251 sd->frame_rate = 15;
3357 3252
3358 return 0; 3253 return 0;
@@ -3467,8 +3362,6 @@ static int sd_init(struct gspca_dev *gspca_dev)
3467 break; 3362 break;
3468 } 3363 }
3469 3364
3470 gspca_dev->ctrl_dis = ctrl_dis[sd->sensor];
3471
3472 /* initialize the sensor */ 3365 /* initialize the sensor */
3473 switch (sd->sensor) { 3366 switch (sd->sensor) {
3474 case SEN_OV2610: 3367 case SEN_OV2610:
@@ -3494,8 +3387,6 @@ static int sd_init(struct gspca_dev *gspca_dev)
3494 break; 3387 break;
3495 case SEN_OV6630: 3388 case SEN_OV6630:
3496 case SEN_OV66308AF: 3389 case SEN_OV66308AF:
3497 sd->ctrls[CONTRAST].def = 200;
3498 /* The default is too low for the ov6630 */
3499 write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)); 3390 write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30));
3500 break; 3391 break;
3501 default: 3392 default:
@@ -3522,26 +3413,12 @@ static int sd_init(struct gspca_dev *gspca_dev)
3522 sd->gspca_dev.curr_mode = 1; /* 640x480 */ 3413 sd->gspca_dev.curr_mode = 1; /* 640x480 */
3523 ov519_set_mode(sd); 3414 ov519_set_mode(sd);
3524 ov519_set_fr(sd); 3415 ov519_set_fr(sd);
3525 sd->ctrls[COLORS].max = 4; /* 0..4 */
3526 sd->ctrls[COLORS].val =
3527 sd->ctrls[COLORS].def = 2;
3528 setcolors(gspca_dev);
3529 sd->ctrls[CONTRAST].max = 6; /* 0..6 */
3530 sd->ctrls[CONTRAST].val =
3531 sd->ctrls[CONTRAST].def = 3;
3532 setcontrast(gspca_dev);
3533 sd->ctrls[BRIGHTNESS].max = 6; /* 0..6 */
3534 sd->ctrls[BRIGHTNESS].val =
3535 sd->ctrls[BRIGHTNESS].def = 3;
3536 setbrightness(gspca_dev);
3537 sd_reset_snapshot(gspca_dev); 3416 sd_reset_snapshot(gspca_dev);
3538 ov51x_restart(sd); 3417 ov51x_restart(sd);
3539 ov51x_stop(sd); /* not in win traces */ 3418 ov51x_stop(sd); /* not in win traces */
3540 ov51x_led_control(sd, 0); 3419 ov51x_led_control(sd, 0);
3541 break; 3420 break;
3542 case SEN_OV7670: 3421 case SEN_OV7670:
3543 sd->ctrls[FREQ].max = 3; /* auto */
3544 sd->ctrls[FREQ].def = 3;
3545 write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)); 3422 write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670));
3546 break; 3423 break;
3547 case SEN_OV8610: 3424 case SEN_OV8610:
@@ -4177,15 +4054,14 @@ static void mode_init_ov_sensor_regs(struct sd *sd)
4177} 4054}
4178 4055
4179/* this function works for bridge ov519 and sensors ov7660 and ov7670 only */ 4056/* this function works for bridge ov519 and sensors ov7660 and ov7670 only */
4180static void sethvflip(struct gspca_dev *gspca_dev) 4057static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
4181{ 4058{
4182 struct sd *sd = (struct sd *) gspca_dev; 4059 struct sd *sd = (struct sd *) gspca_dev;
4183 4060
4184 if (sd->gspca_dev.streaming) 4061 if (sd->gspca_dev.streaming)
4185 reg_w(sd, OV519_R51_RESET1, 0x0f); /* block stream */ 4062 reg_w(sd, OV519_R51_RESET1, 0x0f); /* block stream */
4186 i2c_w_mask(sd, OV7670_R1E_MVFP, 4063 i2c_w_mask(sd, OV7670_R1E_MVFP,
4187 OV7670_MVFP_MIRROR * sd->ctrls[HFLIP].val 4064 OV7670_MVFP_MIRROR * hflip | OV7670_MVFP_VFLIP * vflip,
4188 | OV7670_MVFP_VFLIP * sd->ctrls[VFLIP].val,
4189 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP); 4065 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
4190 if (sd->gspca_dev.streaming) 4066 if (sd->gspca_dev.streaming)
4191 reg_w(sd, OV519_R51_RESET1, 0x00); /* restart stream */ 4067 reg_w(sd, OV519_R51_RESET1, 0x00); /* restart stream */
@@ -4333,23 +4209,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
4333 4209
4334 set_ov_sensor_window(sd); 4210 set_ov_sensor_window(sd);
4335 4211
4336 if (!(sd->gspca_dev.ctrl_dis & (1 << CONTRAST)))
4337 setcontrast(gspca_dev);
4338 if (!(sd->gspca_dev.ctrl_dis & (1 << BRIGHTNESS)))
4339 setbrightness(gspca_dev);
4340 if (!(sd->gspca_dev.ctrl_dis & (1 << EXPOSURE)))
4341 setexposure(gspca_dev);
4342 if (!(sd->gspca_dev.ctrl_dis & (1 << COLORS)))
4343 setcolors(gspca_dev);
4344 if (!(sd->gspca_dev.ctrl_dis & ((1 << HFLIP) | (1 << VFLIP))))
4345 sethvflip(gspca_dev);
4346 if (!(sd->gspca_dev.ctrl_dis & (1 << AUTOBRIGHT)))
4347 setautobright(gspca_dev);
4348 if (!(sd->gspca_dev.ctrl_dis & (1 << AUTOGAIN)))
4349 setautogain(gspca_dev);
4350 if (!(sd->gspca_dev.ctrl_dis & (1 << FREQ)))
4351 setfreq_i(sd);
4352
4353 /* Force clear snapshot state in case the snapshot button was 4212 /* Force clear snapshot state in case the snapshot button was
4354 pressed while we weren't streaming */ 4213 pressed while we weren't streaming */
4355 sd->snapshot_needs_reset = 1; 4214 sd->snapshot_needs_reset = 1;
@@ -4605,10 +4464,9 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
4605 4464
4606/* -- management routines -- */ 4465/* -- management routines -- */
4607 4466
4608static void setbrightness(struct gspca_dev *gspca_dev) 4467static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
4609{ 4468{
4610 struct sd *sd = (struct sd *) gspca_dev; 4469 struct sd *sd = (struct sd *) gspca_dev;
4611 int val;
4612 static const struct ov_i2c_regvals brit_7660[][7] = { 4470 static const struct ov_i2c_regvals brit_7660[][7] = {
4613 {{0x0f, 0x6a}, {0x24, 0x40}, {0x25, 0x2b}, {0x26, 0x90}, 4471 {{0x0f, 0x6a}, {0x24, 0x40}, {0x25, 0x2b}, {0x26, 0x90},
4614 {0x27, 0xe0}, {0x28, 0xe0}, {0x2c, 0xe0}}, 4472 {0x27, 0xe0}, {0x28, 0xe0}, {0x2c, 0xe0}},
@@ -4626,7 +4484,6 @@ static void setbrightness(struct gspca_dev *gspca_dev)
4626 {0x27, 0x60}, {0x28, 0x60}, {0x2c, 0x60}} 4484 {0x27, 0x60}, {0x28, 0x60}, {0x2c, 0x60}}
4627 }; 4485 };
4628 4486
4629 val = sd->ctrls[BRIGHTNESS].val;
4630 switch (sd->sensor) { 4487 switch (sd->sensor) {
4631 case SEN_OV8610: 4488 case SEN_OV8610:
4632 case SEN_OV7610: 4489 case SEN_OV7610:
@@ -4640,9 +4497,7 @@ static void setbrightness(struct gspca_dev *gspca_dev)
4640 break; 4497 break;
4641 case SEN_OV7620: 4498 case SEN_OV7620:
4642 case SEN_OV7620AE: 4499 case SEN_OV7620AE:
4643 /* 7620 doesn't like manual changes when in auto mode */ 4500 i2c_w(sd, OV7610_REG_BRT, val);
4644 if (!sd->ctrls[AUTOBRIGHT].val)
4645 i2c_w(sd, OV7610_REG_BRT, val);
4646 break; 4501 break;
4647 case SEN_OV7660: 4502 case SEN_OV7660:
4648 write_i2c_regvals(sd, brit_7660[val], 4503 write_i2c_regvals(sd, brit_7660[val],
@@ -4656,10 +4511,9 @@ static void setbrightness(struct gspca_dev *gspca_dev)
4656 } 4511 }
4657} 4512}
4658 4513
4659static void setcontrast(struct gspca_dev *gspca_dev) 4514static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
4660{ 4515{
4661 struct sd *sd = (struct sd *) gspca_dev; 4516 struct sd *sd = (struct sd *) gspca_dev;
4662 int val;
4663 static const struct ov_i2c_regvals contrast_7660[][31] = { 4517 static const struct ov_i2c_regvals contrast_7660[][31] = {
4664 {{0x6c, 0xf0}, {0x6d, 0xf0}, {0x6e, 0xf8}, {0x6f, 0xa0}, 4518 {{0x6c, 0xf0}, {0x6d, 0xf0}, {0x6e, 0xf8}, {0x6f, 0xa0},
4665 {0x70, 0x58}, {0x71, 0x38}, {0x72, 0x30}, {0x73, 0x30}, 4519 {0x70, 0x58}, {0x71, 0x38}, {0x72, 0x30}, {0x73, 0x30},
@@ -4719,7 +4573,6 @@ static void setcontrast(struct gspca_dev *gspca_dev)
4719 {0x88, 0xf1}, {0x89, 0xf9}, {0x8a, 0xfd}}, 4573 {0x88, 0xf1}, {0x89, 0xf9}, {0x8a, 0xfd}},
4720 }; 4574 };
4721 4575
4722 val = sd->ctrls[CONTRAST].val;
4723 switch (sd->sensor) { 4576 switch (sd->sensor) {
4724 case SEN_OV7610: 4577 case SEN_OV7610:
4725 case SEN_OV6620: 4578 case SEN_OV6620:
@@ -4760,18 +4613,16 @@ static void setcontrast(struct gspca_dev *gspca_dev)
4760 } 4613 }
4761} 4614}
4762 4615
4763static void setexposure(struct gspca_dev *gspca_dev) 4616static void setexposure(struct gspca_dev *gspca_dev, s32 val)
4764{ 4617{
4765 struct sd *sd = (struct sd *) gspca_dev; 4618 struct sd *sd = (struct sd *) gspca_dev;
4766 4619
4767 if (!sd->ctrls[AUTOGAIN].val) 4620 i2c_w(sd, 0x10, val);
4768 i2c_w(sd, 0x10, sd->ctrls[EXPOSURE].val);
4769} 4621}
4770 4622
4771static void setcolors(struct gspca_dev *gspca_dev) 4623static void setcolors(struct gspca_dev *gspca_dev, s32 val)
4772{ 4624{
4773 struct sd *sd = (struct sd *) gspca_dev; 4625 struct sd *sd = (struct sd *) gspca_dev;
4774 int val;
4775 static const struct ov_i2c_regvals colors_7660[][6] = { 4626 static const struct ov_i2c_regvals colors_7660[][6] = {
4776 {{0x4f, 0x28}, {0x50, 0x2a}, {0x51, 0x02}, {0x52, 0x0a}, 4627 {{0x4f, 0x28}, {0x50, 0x2a}, {0x51, 0x02}, {0x52, 0x0a},
4777 {0x53, 0x19}, {0x54, 0x23}}, 4628 {0x53, 0x19}, {0x54, 0x23}},
@@ -4785,7 +4636,6 @@ static void setcolors(struct gspca_dev *gspca_dev)
4785 {0x53, 0x66}, {0x54, 0x8e}}, 4636 {0x53, 0x66}, {0x54, 0x8e}},
4786 }; 4637 };
4787 4638
4788 val = sd->ctrls[COLORS].val;
4789 switch (sd->sensor) { 4639 switch (sd->sensor) {
4790 case SEN_OV8610: 4640 case SEN_OV8610:
4791 case SEN_OV7610: 4641 case SEN_OV7610:
@@ -4819,34 +4669,18 @@ static void setcolors(struct gspca_dev *gspca_dev)
4819 } 4669 }
4820} 4670}
4821 4671
4822static void setautobright(struct gspca_dev *gspca_dev) 4672static void setautobright(struct gspca_dev *gspca_dev, s32 val)
4823{ 4673{
4824 struct sd *sd = (struct sd *) gspca_dev; 4674 struct sd *sd = (struct sd *) gspca_dev;
4825 4675
4826 i2c_w_mask(sd, 0x2d, sd->ctrls[AUTOBRIGHT].val ? 0x10 : 0x00, 0x10); 4676 i2c_w_mask(sd, 0x2d, val ? 0x10 : 0x00, 0x10);
4827} 4677}
4828 4678
4829static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val) 4679static void setfreq_i(struct sd *sd, s32 val)
4830{
4831 struct sd *sd = (struct sd *) gspca_dev;
4832
4833 sd->ctrls[AUTOGAIN].val = val;
4834 if (val) {
4835 gspca_dev->ctrl_inac |= (1 << EXPOSURE);
4836 } else {
4837 gspca_dev->ctrl_inac &= ~(1 << EXPOSURE);
4838 sd->ctrls[EXPOSURE].val = i2c_r(sd, 0x10);
4839 }
4840 if (gspca_dev->streaming)
4841 setautogain(gspca_dev);
4842 return gspca_dev->usb_err;
4843}
4844
4845static void setfreq_i(struct sd *sd)
4846{ 4680{
4847 if (sd->sensor == SEN_OV7660 4681 if (sd->sensor == SEN_OV7660
4848 || sd->sensor == SEN_OV7670) { 4682 || sd->sensor == SEN_OV7670) {
4849 switch (sd->ctrls[FREQ].val) { 4683 switch (val) {
4850 case 0: /* Banding filter disabled */ 4684 case 0: /* Banding filter disabled */
4851 i2c_w_mask(sd, OV7670_R13_COM8, 0, OV7670_COM8_BFILT); 4685 i2c_w_mask(sd, OV7670_R13_COM8, 0, OV7670_COM8_BFILT);
4852 break; 4686 break;
@@ -4868,7 +4702,7 @@ static void setfreq_i(struct sd *sd)
4868 break; 4702 break;
4869 } 4703 }
4870 } else { 4704 } else {
4871 switch (sd->ctrls[FREQ].val) { 4705 switch (val) {
4872 case 0: /* Banding filter disabled */ 4706 case 0: /* Banding filter disabled */
4873 i2c_w_mask(sd, 0x2d, 0x00, 0x04); 4707 i2c_w_mask(sd, 0x2d, 0x00, 0x04);
4874 i2c_w_mask(sd, 0x2a, 0x00, 0x80); 4708 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
@@ -4900,56 +4734,28 @@ static void setfreq_i(struct sd *sd)
4900 } 4734 }
4901 } 4735 }
4902} 4736}
4903static void setfreq(struct gspca_dev *gspca_dev) 4737
4738static void setfreq(struct gspca_dev *gspca_dev, s32 val)
4904{ 4739{
4905 struct sd *sd = (struct sd *) gspca_dev; 4740 struct sd *sd = (struct sd *) gspca_dev;
4906 4741
4907 setfreq_i(sd); 4742 setfreq_i(sd, val);
4908 4743
4909 /* Ugly but necessary */ 4744 /* Ugly but necessary */
4910 if (sd->bridge == BRIDGE_W9968CF) 4745 if (sd->bridge == BRIDGE_W9968CF)
4911 w9968cf_set_crop_window(sd); 4746 w9968cf_set_crop_window(sd);
4912} 4747}
4913 4748
4914static int sd_querymenu(struct gspca_dev *gspca_dev,
4915 struct v4l2_querymenu *menu)
4916{
4917 struct sd *sd = (struct sd *) gspca_dev;
4918
4919 switch (menu->id) {
4920 case V4L2_CID_POWER_LINE_FREQUENCY:
4921 switch (menu->index) {
4922 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
4923 strcpy((char *) menu->name, "NoFliker");
4924 return 0;
4925 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
4926 strcpy((char *) menu->name, "50 Hz");
4927 return 0;
4928 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
4929 strcpy((char *) menu->name, "60 Hz");
4930 return 0;
4931 case 3:
4932 if (sd->sensor != SEN_OV7670)
4933 return -EINVAL;
4934
4935 strcpy((char *) menu->name, "Automatic");
4936 return 0;
4937 }
4938 break;
4939 }
4940 return -EINVAL;
4941}
4942
4943static int sd_get_jcomp(struct gspca_dev *gspca_dev, 4749static int sd_get_jcomp(struct gspca_dev *gspca_dev,
4944 struct v4l2_jpegcompression *jcomp) 4750 struct v4l2_jpegcompression *jcomp)
4945{ 4751{
4946 struct sd *sd = (struct sd *) gspca_dev; 4752 struct sd *sd = (struct sd *) gspca_dev;
4947 4753
4948 if (sd->bridge != BRIDGE_W9968CF) 4754 if (sd->bridge != BRIDGE_W9968CF)
4949 return -EINVAL; 4755 return -ENOTTY;
4950 4756
4951 memset(jcomp, 0, sizeof *jcomp); 4757 memset(jcomp, 0, sizeof *jcomp);
4952 jcomp->quality = sd->quality; 4758 jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
4953 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT | 4759 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT |
4954 V4L2_JPEG_MARKER_DRI; 4760 V4L2_JPEG_MARKER_DRI;
4955 return 0; 4761 return 0;
@@ -4961,38 +4767,161 @@ static int sd_set_jcomp(struct gspca_dev *gspca_dev,
4961 struct sd *sd = (struct sd *) gspca_dev; 4767 struct sd *sd = (struct sd *) gspca_dev;
4962 4768
4963 if (sd->bridge != BRIDGE_W9968CF) 4769 if (sd->bridge != BRIDGE_W9968CF)
4964 return -EINVAL; 4770 return -ENOTTY;
4771
4772 v4l2_ctrl_s_ctrl(sd->jpegqual, jcomp->quality);
4773 return 0;
4774}
4965 4775
4966 if (gspca_dev->streaming) 4776static int sd_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
4967 return -EBUSY; 4777{
4778 struct gspca_dev *gspca_dev =
4779 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
4780 struct sd *sd = (struct sd *)gspca_dev;
4968 4781
4969 if (jcomp->quality < QUALITY_MIN) 4782 gspca_dev->usb_err = 0;
4970 sd->quality = QUALITY_MIN; 4783
4971 else if (jcomp->quality > QUALITY_MAX) 4784 switch (ctrl->id) {
4972 sd->quality = QUALITY_MAX; 4785 case V4L2_CID_AUTOGAIN:
4973 else 4786 gspca_dev->exposure->val = i2c_r(sd, 0x10);
4974 sd->quality = jcomp->quality; 4787 break;
4788 }
4789 return 0;
4790}
4791
4792static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
4793{
4794 struct gspca_dev *gspca_dev =
4795 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
4796 struct sd *sd = (struct sd *)gspca_dev;
4797
4798 gspca_dev->usb_err = 0;
4799
4800 if (!gspca_dev->streaming)
4801 return 0;
4802
4803 switch (ctrl->id) {
4804 case V4L2_CID_BRIGHTNESS:
4805 setbrightness(gspca_dev, ctrl->val);
4806 break;
4807 case V4L2_CID_CONTRAST:
4808 setcontrast(gspca_dev, ctrl->val);
4809 break;
4810 case V4L2_CID_POWER_LINE_FREQUENCY:
4811 setfreq(gspca_dev, ctrl->val);
4812 break;
4813 case V4L2_CID_AUTOBRIGHTNESS:
4814 if (ctrl->is_new)
4815 setautobright(gspca_dev, ctrl->val);
4816 if (!ctrl->val && sd->brightness->is_new)
4817 setbrightness(gspca_dev, sd->brightness->val);
4818 break;
4819 case V4L2_CID_SATURATION:
4820 setcolors(gspca_dev, ctrl->val);
4821 break;
4822 case V4L2_CID_HFLIP:
4823 sethvflip(gspca_dev, ctrl->val, sd->vflip->val);
4824 break;
4825 case V4L2_CID_AUTOGAIN:
4826 if (ctrl->is_new)
4827 setautogain(gspca_dev, ctrl->val);
4828 if (!ctrl->val && gspca_dev->exposure->is_new)
4829 setexposure(gspca_dev, gspca_dev->exposure->val);
4830 break;
4831 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
4832 return -EBUSY; /* Should never happen, as we grab the ctrl */
4833 }
4834 return gspca_dev->usb_err;
4835}
4975 4836
4976 /* Return resulting jcomp params to app */ 4837static const struct v4l2_ctrl_ops sd_ctrl_ops = {
4977 sd_get_jcomp(gspca_dev, jcomp); 4838 .g_volatile_ctrl = sd_g_volatile_ctrl,
4839 .s_ctrl = sd_s_ctrl,
4840};
4841
4842static int sd_init_controls(struct gspca_dev *gspca_dev)
4843{
4844 struct sd *sd = (struct sd *)gspca_dev;
4845 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
4846
4847 gspca_dev->vdev.ctrl_handler = hdl;
4848 v4l2_ctrl_handler_init(hdl, 10);
4849 if (valid_controls[sd->sensor].has_brightness)
4850 sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4851 V4L2_CID_BRIGHTNESS, 0,
4852 sd->sensor == SEN_OV7660 ? 6 : 255, 1,
4853 sd->sensor == SEN_OV7660 ? 3 : 127);
4854 if (valid_controls[sd->sensor].has_contrast) {
4855 if (sd->sensor == SEN_OV7660)
4856 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4857 V4L2_CID_CONTRAST, 0, 6, 1, 3);
4858 else
4859 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4860 V4L2_CID_CONTRAST, 0, 255, 1,
4861 (sd->sensor == SEN_OV6630 ||
4862 sd->sensor == SEN_OV66308AF) ? 200 : 127);
4863 }
4864 if (valid_controls[sd->sensor].has_sat)
4865 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4866 V4L2_CID_SATURATION, 0,
4867 sd->sensor == SEN_OV7660 ? 4 : 255, 1,
4868 sd->sensor == SEN_OV7660 ? 2 : 127);
4869 if (valid_controls[sd->sensor].has_exposure)
4870 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4871 V4L2_CID_EXPOSURE, 0, 255, 1, 127);
4872 if (valid_controls[sd->sensor].has_hvflip) {
4873 sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4874 V4L2_CID_HFLIP, 0, 1, 1, 0);
4875 sd->vflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4876 V4L2_CID_VFLIP, 0, 1, 1, 0);
4877 }
4878 if (valid_controls[sd->sensor].has_autobright)
4879 sd->autobright = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4880 V4L2_CID_AUTOBRIGHTNESS, 0, 1, 1, 1);
4881 if (valid_controls[sd->sensor].has_autogain)
4882 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4883 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
4884 if (valid_controls[sd->sensor].has_freq) {
4885 if (sd->sensor == SEN_OV7670)
4886 sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
4887 V4L2_CID_POWER_LINE_FREQUENCY,
4888 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
4889 V4L2_CID_POWER_LINE_FREQUENCY_AUTO);
4890 else
4891 sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
4892 V4L2_CID_POWER_LINE_FREQUENCY,
4893 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
4894 }
4895 if (sd->bridge == BRIDGE_W9968CF)
4896 sd->jpegqual = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4897 V4L2_CID_JPEG_COMPRESSION_QUALITY,
4898 QUALITY_MIN, QUALITY_MAX, 1, QUALITY_DEF);
4978 4899
4900 if (hdl->error) {
4901 pr_err("Could not initialize controls\n");
4902 return hdl->error;
4903 }
4904 if (gspca_dev->autogain)
4905 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, true);
4906 if (sd->autobright)
4907 v4l2_ctrl_auto_cluster(2, &sd->autobright, 0, false);
4908 if (sd->hflip)
4909 v4l2_ctrl_cluster(2, &sd->hflip);
4979 return 0; 4910 return 0;
4980} 4911}
4981 4912
4982/* sub-driver description */ 4913/* sub-driver description */
4983static const struct sd_desc sd_desc = { 4914static const struct sd_desc sd_desc = {
4984 .name = MODULE_NAME, 4915 .name = MODULE_NAME,
4985 .ctrls = sd_ctrls,
4986 .nctrls = ARRAY_SIZE(sd_ctrls),
4987 .config = sd_config, 4916 .config = sd_config,
4988 .init = sd_init, 4917 .init = sd_init,
4918 .init_controls = sd_init_controls,
4989 .isoc_init = sd_isoc_init, 4919 .isoc_init = sd_isoc_init,
4990 .start = sd_start, 4920 .start = sd_start,
4991 .stopN = sd_stopN, 4921 .stopN = sd_stopN,
4992 .stop0 = sd_stop0, 4922 .stop0 = sd_stop0,
4993 .pkt_scan = sd_pkt_scan, 4923 .pkt_scan = sd_pkt_scan,
4994 .dq_callback = sd_reset_snapshot, 4924 .dq_callback = sd_reset_snapshot,
4995 .querymenu = sd_querymenu,
4996 .get_jcomp = sd_get_jcomp, 4925 .get_jcomp = sd_get_jcomp,
4997 .set_jcomp = sd_set_jcomp, 4926 .set_jcomp = sd_set_jcomp,
4998#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) 4927#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
@@ -5052,6 +4981,7 @@ static struct usb_driver sd_driver = {
5052#ifdef CONFIG_PM 4981#ifdef CONFIG_PM
5053 .suspend = gspca_suspend, 4982 .suspend = gspca_suspend,
5054 .resume = gspca_resume, 4983 .resume = gspca_resume,
4984 .reset_resume = gspca_resume,
5055#endif 4985#endif
5056}; 4986};
5057 4987
diff --git a/drivers/media/video/gspca/ov534.c b/drivers/media/video/gspca/ov534.c
index 80c81dd6d68b..bb09d7884b89 100644
--- a/drivers/media/video/gspca/ov534.c
+++ b/drivers/media/video/gspca/ov534.c
@@ -35,6 +35,7 @@
35#include "gspca.h" 35#include "gspca.h"
36 36
37#include <linux/fixp-arith.h> 37#include <linux/fixp-arith.h>
38#include <media/v4l2-ctrls.h>
38 39
39#define OV534_REG_ADDRESS 0xf1 /* sensor address */ 40#define OV534_REG_ADDRESS 0xf1 /* sensor address */
40#define OV534_REG_SUBADDR 0xf2 41#define OV534_REG_SUBADDR 0xf2
@@ -53,29 +54,28 @@ MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
53MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver"); 54MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
54MODULE_LICENSE("GPL"); 55MODULE_LICENSE("GPL");
55 56
56/* controls */
57enum e_ctrl {
58 HUE,
59 SATURATION,
60 BRIGHTNESS,
61 CONTRAST,
62 GAIN,
63 EXPOSURE,
64 AGC,
65 AWB,
66 AEC,
67 SHARPNESS,
68 HFLIP,
69 VFLIP,
70 LIGHTFREQ,
71 NCTRLS /* number of controls */
72};
73
74/* specific webcam descriptor */ 57/* specific webcam descriptor */
75struct sd { 58struct sd {
76 struct gspca_dev gspca_dev; /* !! must be the first item */ 59 struct gspca_dev gspca_dev; /* !! must be the first item */
77 60
78 struct gspca_ctrl ctrls[NCTRLS]; 61 struct v4l2_ctrl_handler ctrl_handler;
62 struct v4l2_ctrl *hue;
63 struct v4l2_ctrl *saturation;
64 struct v4l2_ctrl *brightness;
65 struct v4l2_ctrl *contrast;
66 struct { /* gain control cluster */
67 struct v4l2_ctrl *autogain;
68 struct v4l2_ctrl *gain;
69 };
70 struct v4l2_ctrl *autowhitebalance;
71 struct { /* exposure control cluster */
72 struct v4l2_ctrl *autoexposure;
73 struct v4l2_ctrl *exposure;
74 };
75 struct v4l2_ctrl *sharpness;
76 struct v4l2_ctrl *hflip;
77 struct v4l2_ctrl *vflip;
78 struct v4l2_ctrl *plfreq;
79 79
80 __u32 last_pts; 80 __u32 last_pts;
81 u16 last_fid; 81 u16 last_fid;
@@ -89,181 +89,9 @@ enum sensors {
89 NSENSORS 89 NSENSORS
90}; 90};
91 91
92/* V4L2 controls supported by the driver */
93static void sethue(struct gspca_dev *gspca_dev);
94static void setsaturation(struct gspca_dev *gspca_dev);
95static void setbrightness(struct gspca_dev *gspca_dev);
96static void setcontrast(struct gspca_dev *gspca_dev);
97static void setgain(struct gspca_dev *gspca_dev);
98static void setexposure(struct gspca_dev *gspca_dev);
99static void setagc(struct gspca_dev *gspca_dev);
100static void setawb(struct gspca_dev *gspca_dev);
101static void setaec(struct gspca_dev *gspca_dev);
102static void setsharpness(struct gspca_dev *gspca_dev);
103static void sethvflip(struct gspca_dev *gspca_dev);
104static void setlightfreq(struct gspca_dev *gspca_dev);
105
106static int sd_start(struct gspca_dev *gspca_dev); 92static int sd_start(struct gspca_dev *gspca_dev);
107static void sd_stopN(struct gspca_dev *gspca_dev); 93static void sd_stopN(struct gspca_dev *gspca_dev);
108 94
109static const struct ctrl sd_ctrls[] = {
110[HUE] = {
111 {
112 .id = V4L2_CID_HUE,
113 .type = V4L2_CTRL_TYPE_INTEGER,
114 .name = "Hue",
115 .minimum = -90,
116 .maximum = 90,
117 .step = 1,
118 .default_value = 0,
119 },
120 .set_control = sethue
121 },
122[SATURATION] = {
123 {
124 .id = V4L2_CID_SATURATION,
125 .type = V4L2_CTRL_TYPE_INTEGER,
126 .name = "Saturation",
127 .minimum = 0,
128 .maximum = 255,
129 .step = 1,
130 .default_value = 64,
131 },
132 .set_control = setsaturation
133 },
134[BRIGHTNESS] = {
135 {
136 .id = V4L2_CID_BRIGHTNESS,
137 .type = V4L2_CTRL_TYPE_INTEGER,
138 .name = "Brightness",
139 .minimum = 0,
140 .maximum = 255,
141 .step = 1,
142 .default_value = 0,
143 },
144 .set_control = setbrightness
145 },
146[CONTRAST] = {
147 {
148 .id = V4L2_CID_CONTRAST,
149 .type = V4L2_CTRL_TYPE_INTEGER,
150 .name = "Contrast",
151 .minimum = 0,
152 .maximum = 255,
153 .step = 1,
154 .default_value = 32,
155 },
156 .set_control = setcontrast
157 },
158[GAIN] = {
159 {
160 .id = V4L2_CID_GAIN,
161 .type = V4L2_CTRL_TYPE_INTEGER,
162 .name = "Main Gain",
163 .minimum = 0,
164 .maximum = 63,
165 .step = 1,
166 .default_value = 20,
167 },
168 .set_control = setgain
169 },
170[EXPOSURE] = {
171 {
172 .id = V4L2_CID_EXPOSURE,
173 .type = V4L2_CTRL_TYPE_INTEGER,
174 .name = "Exposure",
175 .minimum = 0,
176 .maximum = 255,
177 .step = 1,
178 .default_value = 120,
179 },
180 .set_control = setexposure
181 },
182[AGC] = {
183 {
184 .id = V4L2_CID_AUTOGAIN,
185 .type = V4L2_CTRL_TYPE_BOOLEAN,
186 .name = "Auto Gain",
187 .minimum = 0,
188 .maximum = 1,
189 .step = 1,
190 .default_value = 1,
191 },
192 .set_control = setagc
193 },
194[AWB] = {
195 {
196 .id = V4L2_CID_AUTO_WHITE_BALANCE,
197 .type = V4L2_CTRL_TYPE_BOOLEAN,
198 .name = "Auto White Balance",
199 .minimum = 0,
200 .maximum = 1,
201 .step = 1,
202 .default_value = 1,
203 },
204 .set_control = setawb
205 },
206[AEC] = {
207 {
208 .id = V4L2_CID_EXPOSURE_AUTO,
209 .type = V4L2_CTRL_TYPE_BOOLEAN,
210 .name = "Auto Exposure",
211 .minimum = 0,
212 .maximum = 1,
213 .step = 1,
214 .default_value = 1,
215 },
216 .set_control = setaec
217 },
218[SHARPNESS] = {
219 {
220 .id = V4L2_CID_SHARPNESS,
221 .type = V4L2_CTRL_TYPE_INTEGER,
222 .name = "Sharpness",
223 .minimum = 0,
224 .maximum = 63,
225 .step = 1,
226 .default_value = 0,
227 },
228 .set_control = setsharpness
229 },
230[HFLIP] = {
231 {
232 .id = V4L2_CID_HFLIP,
233 .type = V4L2_CTRL_TYPE_BOOLEAN,
234 .name = "HFlip",
235 .minimum = 0,
236 .maximum = 1,
237 .step = 1,
238 .default_value = 0,
239 },
240 .set_control = sethvflip
241 },
242[VFLIP] = {
243 {
244 .id = V4L2_CID_VFLIP,
245 .type = V4L2_CTRL_TYPE_BOOLEAN,
246 .name = "VFlip",
247 .minimum = 0,
248 .maximum = 1,
249 .step = 1,
250 .default_value = 0,
251 },
252 .set_control = sethvflip
253 },
254[LIGHTFREQ] = {
255 {
256 .id = V4L2_CID_POWER_LINE_FREQUENCY,
257 .type = V4L2_CTRL_TYPE_MENU,
258 .name = "Light Frequency Filter",
259 .minimum = 0,
260 .maximum = 1,
261 .step = 1,
262 .default_value = 0,
263 },
264 .set_control = setlightfreq
265 },
266};
267 95
268static const struct v4l2_pix_format ov772x_mode[] = { 96static const struct v4l2_pix_format ov772x_mode[] = {
269 {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE, 97 {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
@@ -972,12 +800,10 @@ static void set_frame_rate(struct gspca_dev *gspca_dev)
972 PDEBUG(D_PROBE, "frame_rate: %d", r->fps); 800 PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
973} 801}
974 802
975static void sethue(struct gspca_dev *gspca_dev) 803static void sethue(struct gspca_dev *gspca_dev, s32 val)
976{ 804{
977 struct sd *sd = (struct sd *) gspca_dev; 805 struct sd *sd = (struct sd *) gspca_dev;
978 int val;
979 806
980 val = sd->ctrls[HUE].val;
981 if (sd->sensor == SENSOR_OV767x) { 807 if (sd->sensor == SENSOR_OV767x) {
982 /* TBD */ 808 /* TBD */
983 } else { 809 } else {
@@ -1014,12 +840,10 @@ static void sethue(struct gspca_dev *gspca_dev)
1014 } 840 }
1015} 841}
1016 842
1017static void setsaturation(struct gspca_dev *gspca_dev) 843static void setsaturation(struct gspca_dev *gspca_dev, s32 val)
1018{ 844{
1019 struct sd *sd = (struct sd *) gspca_dev; 845 struct sd *sd = (struct sd *) gspca_dev;
1020 int val;
1021 846
1022 val = sd->ctrls[SATURATION].val;
1023 if (sd->sensor == SENSOR_OV767x) { 847 if (sd->sensor == SENSOR_OV767x) {
1024 int i; 848 int i;
1025 static u8 color_tb[][6] = { 849 static u8 color_tb[][6] = {
@@ -1040,12 +864,10 @@ static void setsaturation(struct gspca_dev *gspca_dev)
1040 } 864 }
1041} 865}
1042 866
1043static void setbrightness(struct gspca_dev *gspca_dev) 867static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
1044{ 868{
1045 struct sd *sd = (struct sd *) gspca_dev; 869 struct sd *sd = (struct sd *) gspca_dev;
1046 int val;
1047 870
1048 val = sd->ctrls[BRIGHTNESS].val;
1049 if (sd->sensor == SENSOR_OV767x) { 871 if (sd->sensor == SENSOR_OV767x) {
1050 if (val < 0) 872 if (val < 0)
1051 val = 0x80 - val; 873 val = 0x80 - val;
@@ -1055,27 +877,18 @@ static void setbrightness(struct gspca_dev *gspca_dev)
1055 } 877 }
1056} 878}
1057 879
1058static void setcontrast(struct gspca_dev *gspca_dev) 880static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
1059{ 881{
1060 struct sd *sd = (struct sd *) gspca_dev; 882 struct sd *sd = (struct sd *) gspca_dev;
1061 u8 val;
1062 883
1063 val = sd->ctrls[CONTRAST].val;
1064 if (sd->sensor == SENSOR_OV767x) 884 if (sd->sensor == SENSOR_OV767x)
1065 sccb_reg_write(gspca_dev, 0x56, val); /* contras */ 885 sccb_reg_write(gspca_dev, 0x56, val); /* contras */
1066 else 886 else
1067 sccb_reg_write(gspca_dev, 0x9c, val); 887 sccb_reg_write(gspca_dev, 0x9c, val);
1068} 888}
1069 889
1070static void setgain(struct gspca_dev *gspca_dev) 890static void setgain(struct gspca_dev *gspca_dev, s32 val)
1071{ 891{
1072 struct sd *sd = (struct sd *) gspca_dev;
1073 u8 val;
1074
1075 if (sd->ctrls[AGC].val)
1076 return;
1077
1078 val = sd->ctrls[GAIN].val;
1079 switch (val & 0x30) { 892 switch (val & 0x30) {
1080 case 0x00: 893 case 0x00:
1081 val &= 0x0f; 894 val &= 0x0f;
@@ -1097,15 +910,15 @@ static void setgain(struct gspca_dev *gspca_dev)
1097 sccb_reg_write(gspca_dev, 0x00, val); 910 sccb_reg_write(gspca_dev, 0x00, val);
1098} 911}
1099 912
1100static void setexposure(struct gspca_dev *gspca_dev) 913static s32 getgain(struct gspca_dev *gspca_dev)
1101{ 914{
1102 struct sd *sd = (struct sd *) gspca_dev; 915 return sccb_reg_read(gspca_dev, 0x00);
1103 u8 val; 916}
1104 917
1105 if (sd->ctrls[AEC].val) 918static void setexposure(struct gspca_dev *gspca_dev, s32 val)
1106 return; 919{
920 struct sd *sd = (struct sd *) gspca_dev;
1107 921
1108 val = sd->ctrls[EXPOSURE].val;
1109 if (sd->sensor == SENSOR_OV767x) { 922 if (sd->sensor == SENSOR_OV767x) {
1110 923
1111 /* set only aec[9:2] */ 924 /* set only aec[9:2] */
@@ -1123,11 +936,23 @@ static void setexposure(struct gspca_dev *gspca_dev)
1123 } 936 }
1124} 937}
1125 938
1126static void setagc(struct gspca_dev *gspca_dev) 939static s32 getexposure(struct gspca_dev *gspca_dev)
1127{ 940{
1128 struct sd *sd = (struct sd *) gspca_dev; 941 struct sd *sd = (struct sd *) gspca_dev;
1129 942
1130 if (sd->ctrls[AGC].val) { 943 if (sd->sensor == SENSOR_OV767x) {
944 /* get only aec[9:2] */
945 return sccb_reg_read(gspca_dev, 0x10); /* aech */
946 } else {
947 u8 hi = sccb_reg_read(gspca_dev, 0x08);
948 u8 lo = sccb_reg_read(gspca_dev, 0x10);
949 return (hi << 8 | lo) >> 1;
950 }
951}
952
953static void setagc(struct gspca_dev *gspca_dev, s32 val)
954{
955 if (val) {
1131 sccb_reg_write(gspca_dev, 0x13, 956 sccb_reg_write(gspca_dev, 0x13,
1132 sccb_reg_read(gspca_dev, 0x13) | 0x04); 957 sccb_reg_read(gspca_dev, 0x13) | 0x04);
1133 sccb_reg_write(gspca_dev, 0x64, 958 sccb_reg_write(gspca_dev, 0x64,
@@ -1137,16 +962,14 @@ static void setagc(struct gspca_dev *gspca_dev)
1137 sccb_reg_read(gspca_dev, 0x13) & ~0x04); 962 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
1138 sccb_reg_write(gspca_dev, 0x64, 963 sccb_reg_write(gspca_dev, 0x64,
1139 sccb_reg_read(gspca_dev, 0x64) & ~0x03); 964 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
1140
1141 setgain(gspca_dev);
1142 } 965 }
1143} 966}
1144 967
1145static void setawb(struct gspca_dev *gspca_dev) 968static void setawb(struct gspca_dev *gspca_dev, s32 val)
1146{ 969{
1147 struct sd *sd = (struct sd *) gspca_dev; 970 struct sd *sd = (struct sd *) gspca_dev;
1148 971
1149 if (sd->ctrls[AWB].val) { 972 if (val) {
1150 sccb_reg_write(gspca_dev, 0x13, 973 sccb_reg_write(gspca_dev, 0x13,
1151 sccb_reg_read(gspca_dev, 0x13) | 0x02); 974 sccb_reg_read(gspca_dev, 0x13) | 0x02);
1152 if (sd->sensor == SENSOR_OV772x) 975 if (sd->sensor == SENSOR_OV772x)
@@ -1161,7 +984,7 @@ static void setawb(struct gspca_dev *gspca_dev)
1161 } 984 }
1162} 985}
1163 986
1164static void setaec(struct gspca_dev *gspca_dev) 987static void setaec(struct gspca_dev *gspca_dev, s32 val)
1165{ 988{
1166 struct sd *sd = (struct sd *) gspca_dev; 989 struct sd *sd = (struct sd *) gspca_dev;
1167 u8 data; 990 u8 data;
@@ -1169,31 +992,25 @@ static void setaec(struct gspca_dev *gspca_dev)
1169 data = sd->sensor == SENSOR_OV767x ? 992 data = sd->sensor == SENSOR_OV767x ?
1170 0x05 : /* agc + aec */ 993 0x05 : /* agc + aec */
1171 0x01; /* agc */ 994 0x01; /* agc */
1172 if (sd->ctrls[AEC].val) 995 switch (val) {
996 case V4L2_EXPOSURE_AUTO:
1173 sccb_reg_write(gspca_dev, 0x13, 997 sccb_reg_write(gspca_dev, 0x13,
1174 sccb_reg_read(gspca_dev, 0x13) | data); 998 sccb_reg_read(gspca_dev, 0x13) | data);
1175 else { 999 break;
1000 case V4L2_EXPOSURE_MANUAL:
1176 sccb_reg_write(gspca_dev, 0x13, 1001 sccb_reg_write(gspca_dev, 0x13,
1177 sccb_reg_read(gspca_dev, 0x13) & ~data); 1002 sccb_reg_read(gspca_dev, 0x13) & ~data);
1178 if (sd->sensor == SENSOR_OV767x) 1003 break;
1179 sd->ctrls[EXPOSURE].val =
1180 sccb_reg_read(gspca_dev, 10); /* aech */
1181 else
1182 setexposure(gspca_dev);
1183 } 1004 }
1184} 1005}
1185 1006
1186static void setsharpness(struct gspca_dev *gspca_dev) 1007static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
1187{ 1008{
1188 struct sd *sd = (struct sd *) gspca_dev;
1189 u8 val;
1190
1191 val = sd->ctrls[SHARPNESS].val;
1192 sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */ 1009 sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
1193 sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */ 1010 sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
1194} 1011}
1195 1012
1196static void sethvflip(struct gspca_dev *gspca_dev) 1013static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
1197{ 1014{
1198 struct sd *sd = (struct sd *) gspca_dev; 1015 struct sd *sd = (struct sd *) gspca_dev;
1199 u8 val; 1016 u8 val;
@@ -1201,28 +1018,27 @@ static void sethvflip(struct gspca_dev *gspca_dev)
1201 if (sd->sensor == SENSOR_OV767x) { 1018 if (sd->sensor == SENSOR_OV767x) {
1202 val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */ 1019 val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */
1203 val &= ~0x30; 1020 val &= ~0x30;
1204 if (sd->ctrls[HFLIP].val) 1021 if (hflip)
1205 val |= 0x20; 1022 val |= 0x20;
1206 if (sd->ctrls[VFLIP].val) 1023 if (vflip)
1207 val |= 0x10; 1024 val |= 0x10;
1208 sccb_reg_write(gspca_dev, 0x1e, val); 1025 sccb_reg_write(gspca_dev, 0x1e, val);
1209 } else { 1026 } else {
1210 val = sccb_reg_read(gspca_dev, 0x0c); 1027 val = sccb_reg_read(gspca_dev, 0x0c);
1211 val &= ~0xc0; 1028 val &= ~0xc0;
1212 if (sd->ctrls[HFLIP].val == 0) 1029 if (hflip == 0)
1213 val |= 0x40; 1030 val |= 0x40;
1214 if (sd->ctrls[VFLIP].val == 0) 1031 if (vflip == 0)
1215 val |= 0x80; 1032 val |= 0x80;
1216 sccb_reg_write(gspca_dev, 0x0c, val); 1033 sccb_reg_write(gspca_dev, 0x0c, val);
1217 } 1034 }
1218} 1035}
1219 1036
1220static void setlightfreq(struct gspca_dev *gspca_dev) 1037static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
1221{ 1038{
1222 struct sd *sd = (struct sd *) gspca_dev; 1039 struct sd *sd = (struct sd *) gspca_dev;
1223 u8 val;
1224 1040
1225 val = sd->ctrls[LIGHTFREQ].val ? 0x9e : 0x00; 1041 val = val ? 0x9e : 0x00;
1226 if (sd->sensor == SENSOR_OV767x) { 1042 if (sd->sensor == SENSOR_OV767x) {
1227 sccb_reg_write(gspca_dev, 0x2a, 0x00); 1043 sccb_reg_write(gspca_dev, 0x2a, 0x00);
1228 if (val) 1044 if (val)
@@ -1241,8 +1057,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
1241 1057
1242 cam = &gspca_dev->cam; 1058 cam = &gspca_dev->cam;
1243 1059
1244 cam->ctrls = sd->ctrls;
1245
1246 cam->cam_mode = ov772x_mode; 1060 cam->cam_mode = ov772x_mode;
1247 cam->nmodes = ARRAY_SIZE(ov772x_mode); 1061 cam->nmodes = ARRAY_SIZE(ov772x_mode);
1248 1062
@@ -1251,6 +1065,195 @@ static int sd_config(struct gspca_dev *gspca_dev,
1251 return 0; 1065 return 0;
1252} 1066}
1253 1067
1068static int ov534_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1069{
1070 struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1071 struct gspca_dev *gspca_dev = &sd->gspca_dev;
1072
1073 switch (ctrl->id) {
1074 case V4L2_CID_AUTOGAIN:
1075 gspca_dev->usb_err = 0;
1076 if (ctrl->val && sd->gain && gspca_dev->streaming)
1077 sd->gain->val = getgain(gspca_dev);
1078 return gspca_dev->usb_err;
1079
1080 case V4L2_CID_EXPOSURE_AUTO:
1081 gspca_dev->usb_err = 0;
1082 if (ctrl->val == V4L2_EXPOSURE_AUTO && sd->exposure &&
1083 gspca_dev->streaming)
1084 sd->exposure->val = getexposure(gspca_dev);
1085 return gspca_dev->usb_err;
1086 }
1087 return -EINVAL;
1088}
1089
1090static int ov534_s_ctrl(struct v4l2_ctrl *ctrl)
1091{
1092 struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1093 struct gspca_dev *gspca_dev = &sd->gspca_dev;
1094
1095 gspca_dev->usb_err = 0;
1096 if (!gspca_dev->streaming)
1097 return 0;
1098
1099 switch (ctrl->id) {
1100 case V4L2_CID_HUE:
1101 sethue(gspca_dev, ctrl->val);
1102 break;
1103 case V4L2_CID_SATURATION:
1104 setsaturation(gspca_dev, ctrl->val);
1105 break;
1106 case V4L2_CID_BRIGHTNESS:
1107 setbrightness(gspca_dev, ctrl->val);
1108 break;
1109 case V4L2_CID_CONTRAST:
1110 setcontrast(gspca_dev, ctrl->val);
1111 break;
1112 case V4L2_CID_AUTOGAIN:
1113 /* case V4L2_CID_GAIN: */
1114 setagc(gspca_dev, ctrl->val);
1115 if (!gspca_dev->usb_err && !ctrl->val && sd->gain)
1116 setgain(gspca_dev, sd->gain->val);
1117 break;
1118 case V4L2_CID_AUTO_WHITE_BALANCE:
1119 setawb(gspca_dev, ctrl->val);
1120 break;
1121 case V4L2_CID_EXPOSURE_AUTO:
1122 /* case V4L2_CID_EXPOSURE: */
1123 setaec(gspca_dev, ctrl->val);
1124 if (!gspca_dev->usb_err && ctrl->val == V4L2_EXPOSURE_MANUAL &&
1125 sd->exposure)
1126 setexposure(gspca_dev, sd->exposure->val);
1127 break;
1128 case V4L2_CID_SHARPNESS:
1129 setsharpness(gspca_dev, ctrl->val);
1130 break;
1131 case V4L2_CID_HFLIP:
1132 sethvflip(gspca_dev, ctrl->val, sd->vflip->val);
1133 break;
1134 case V4L2_CID_VFLIP:
1135 sethvflip(gspca_dev, sd->hflip->val, ctrl->val);
1136 break;
1137 case V4L2_CID_POWER_LINE_FREQUENCY:
1138 setlightfreq(gspca_dev, ctrl->val);
1139 break;
1140 }
1141 return gspca_dev->usb_err;
1142}
1143
1144static const struct v4l2_ctrl_ops ov534_ctrl_ops = {
1145 .g_volatile_ctrl = ov534_g_volatile_ctrl,
1146 .s_ctrl = ov534_s_ctrl,
1147};
1148
1149static int sd_init_controls(struct gspca_dev *gspca_dev)
1150{
1151 struct sd *sd = (struct sd *) gspca_dev;
1152 struct v4l2_ctrl_handler *hdl = &sd->ctrl_handler;
1153 /* parameters with different values between the supported sensors */
1154 int saturation_min;
1155 int saturation_max;
1156 int saturation_def;
1157 int brightness_min;
1158 int brightness_max;
1159 int brightness_def;
1160 int contrast_max;
1161 int contrast_def;
1162 int exposure_min;
1163 int exposure_max;
1164 int exposure_def;
1165 int hflip_def;
1166
1167 if (sd->sensor == SENSOR_OV767x) {
1168 saturation_min = 0,
1169 saturation_max = 6,
1170 saturation_def = 3,
1171 brightness_min = -127;
1172 brightness_max = 127;
1173 brightness_def = 0;
1174 contrast_max = 0x80;
1175 contrast_def = 0x40;
1176 exposure_min = 0x08;
1177 exposure_max = 0x60;
1178 exposure_def = 0x13;
1179 hflip_def = 1;
1180 } else {
1181 saturation_min = 0,
1182 saturation_max = 255,
1183 saturation_def = 64,
1184 brightness_min = 0;
1185 brightness_max = 255;
1186 brightness_def = 0;
1187 contrast_max = 255;
1188 contrast_def = 32;
1189 exposure_min = 0;
1190 exposure_max = 255;
1191 exposure_def = 120;
1192 hflip_def = 0;
1193 }
1194
1195 gspca_dev->vdev.ctrl_handler = hdl;
1196
1197 v4l2_ctrl_handler_init(hdl, 13);
1198
1199 if (sd->sensor == SENSOR_OV772x)
1200 sd->hue = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1201 V4L2_CID_HUE, -90, 90, 1, 0);
1202
1203 sd->saturation = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1204 V4L2_CID_SATURATION, saturation_min, saturation_max, 1,
1205 saturation_def);
1206 sd->brightness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1207 V4L2_CID_BRIGHTNESS, brightness_min, brightness_max, 1,
1208 brightness_def);
1209 sd->contrast = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1210 V4L2_CID_CONTRAST, 0, contrast_max, 1, contrast_def);
1211
1212 if (sd->sensor == SENSOR_OV772x) {
1213 sd->autogain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1214 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1215 sd->gain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1216 V4L2_CID_GAIN, 0, 63, 1, 20);
1217 }
1218
1219 sd->autoexposure = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1220 V4L2_CID_EXPOSURE_AUTO,
1221 V4L2_EXPOSURE_MANUAL, 0,
1222 V4L2_EXPOSURE_AUTO);
1223 sd->exposure = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1224 V4L2_CID_EXPOSURE, exposure_min, exposure_max, 1,
1225 exposure_def);
1226
1227 sd->autowhitebalance = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1228 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1229
1230 if (sd->sensor == SENSOR_OV772x)
1231 sd->sharpness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1232 V4L2_CID_SHARPNESS, 0, 63, 1, 0);
1233
1234 sd->hflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1235 V4L2_CID_HFLIP, 0, 1, 1, hflip_def);
1236 sd->vflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1237 V4L2_CID_VFLIP, 0, 1, 1, 0);
1238 sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1239 V4L2_CID_POWER_LINE_FREQUENCY,
1240 V4L2_CID_POWER_LINE_FREQUENCY_50HZ, 0,
1241 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1242
1243 if (hdl->error) {
1244 pr_err("Could not initialize controls\n");
1245 return hdl->error;
1246 }
1247
1248 if (sd->sensor == SENSOR_OV772x)
1249 v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, true);
1250
1251 v4l2_ctrl_auto_cluster(2, &sd->autoexposure, V4L2_EXPOSURE_MANUAL,
1252 true);
1253
1254 return 0;
1255}
1256
1254/* this function is called at probe and resume time */ 1257/* this function is called at probe and resume time */
1255static int sd_init(struct gspca_dev *gspca_dev) 1258static int sd_init(struct gspca_dev *gspca_dev)
1256{ 1259{
@@ -1286,24 +1289,6 @@ static int sd_init(struct gspca_dev *gspca_dev)
1286 1289
1287 if ((sensor_id & 0xfff0) == 0x7670) { 1290 if ((sensor_id & 0xfff0) == 0x7670) {
1288 sd->sensor = SENSOR_OV767x; 1291 sd->sensor = SENSOR_OV767x;
1289 gspca_dev->ctrl_dis = (1 << HUE) |
1290 (1 << GAIN) |
1291 (1 << AGC) |
1292 (1 << SHARPNESS); /* auto */
1293 sd->ctrls[SATURATION].min = 0,
1294 sd->ctrls[SATURATION].max = 6,
1295 sd->ctrls[SATURATION].def = 3,
1296 sd->ctrls[BRIGHTNESS].min = -127;
1297 sd->ctrls[BRIGHTNESS].max = 127;
1298 sd->ctrls[BRIGHTNESS].def = 0;
1299 sd->ctrls[CONTRAST].max = 0x80;
1300 sd->ctrls[CONTRAST].def = 0x40;
1301 sd->ctrls[EXPOSURE].min = 0x08;
1302 sd->ctrls[EXPOSURE].max = 0x60;
1303 sd->ctrls[EXPOSURE].def = 0x13;
1304 sd->ctrls[SHARPNESS].max = 9;
1305 sd->ctrls[SHARPNESS].def = 4;
1306 sd->ctrls[HFLIP].def = 1;
1307 gspca_dev->cam.cam_mode = ov767x_mode; 1292 gspca_dev->cam.cam_mode = ov767x_mode;
1308 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode); 1293 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1309 } else { 1294 } else {
@@ -1366,22 +1351,23 @@ static int sd_start(struct gspca_dev *gspca_dev)
1366 1351
1367 set_frame_rate(gspca_dev); 1352 set_frame_rate(gspca_dev);
1368 1353
1369 if (!(gspca_dev->ctrl_dis & (1 << HUE))) 1354 if (sd->hue)
1370 sethue(gspca_dev); 1355 sethue(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue));
1371 setsaturation(gspca_dev); 1356 setsaturation(gspca_dev, v4l2_ctrl_g_ctrl(sd->saturation));
1372 if (!(gspca_dev->ctrl_dis & (1 << AGC))) 1357 if (sd->autogain)
1373 setagc(gspca_dev); 1358 setagc(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
1374 setawb(gspca_dev); 1359 setawb(gspca_dev, v4l2_ctrl_g_ctrl(sd->autowhitebalance));
1375 setaec(gspca_dev); 1360 setaec(gspca_dev, v4l2_ctrl_g_ctrl(sd->autoexposure));
1376 if (!(gspca_dev->ctrl_dis & (1 << GAIN))) 1361 if (sd->gain)
1377 setgain(gspca_dev); 1362 setgain(gspca_dev, v4l2_ctrl_g_ctrl(sd->gain));
1378 setexposure(gspca_dev); 1363 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure));
1379 setbrightness(gspca_dev); 1364 setbrightness(gspca_dev, v4l2_ctrl_g_ctrl(sd->brightness));
1380 setcontrast(gspca_dev); 1365 setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
1381 if (!(gspca_dev->ctrl_dis & (1 << SHARPNESS))) 1366 if (sd->sharpness)
1382 setsharpness(gspca_dev); 1367 setsharpness(gspca_dev, v4l2_ctrl_g_ctrl(sd->sharpness));
1383 sethvflip(gspca_dev); 1368 sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip),
1384 setlightfreq(gspca_dev); 1369 v4l2_ctrl_g_ctrl(sd->vflip));
1370 setlightfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->plfreq));
1385 1371
1386 ov534_set_led(gspca_dev, 1); 1372 ov534_set_led(gspca_dev, 1);
1387 ov534_reg_write(gspca_dev, 0xe0, 0x00); 1373 ov534_reg_write(gspca_dev, 0xe0, 0x00);
@@ -1483,25 +1469,6 @@ scan_next:
1483 } while (remaining_len > 0); 1469 } while (remaining_len > 0);
1484} 1470}
1485 1471
1486static int sd_querymenu(struct gspca_dev *gspca_dev,
1487 struct v4l2_querymenu *menu)
1488{
1489 switch (menu->id) {
1490 case V4L2_CID_POWER_LINE_FREQUENCY:
1491 switch (menu->index) {
1492 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1493 strcpy((char *) menu->name, "Disabled");
1494 return 0;
1495 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1496 strcpy((char *) menu->name, "50 Hz");
1497 return 0;
1498 }
1499 break;
1500 }
1501
1502 return -EINVAL;
1503}
1504
1505/* get stream parameters (framerate) */ 1472/* get stream parameters (framerate) */
1506static void sd_get_streamparm(struct gspca_dev *gspca_dev, 1473static void sd_get_streamparm(struct gspca_dev *gspca_dev,
1507 struct v4l2_streamparm *parm) 1474 struct v4l2_streamparm *parm)
@@ -1536,14 +1503,12 @@ static void sd_set_streamparm(struct gspca_dev *gspca_dev,
1536/* sub-driver description */ 1503/* sub-driver description */
1537static const struct sd_desc sd_desc = { 1504static const struct sd_desc sd_desc = {
1538 .name = MODULE_NAME, 1505 .name = MODULE_NAME,
1539 .ctrls = sd_ctrls,
1540 .nctrls = ARRAY_SIZE(sd_ctrls),
1541 .config = sd_config, 1506 .config = sd_config,
1542 .init = sd_init, 1507 .init = sd_init,
1508 .init_controls = sd_init_controls,
1543 .start = sd_start, 1509 .start = sd_start,
1544 .stopN = sd_stopN, 1510 .stopN = sd_stopN,
1545 .pkt_scan = sd_pkt_scan, 1511 .pkt_scan = sd_pkt_scan,
1546 .querymenu = sd_querymenu,
1547 .get_streamparm = sd_get_streamparm, 1512 .get_streamparm = sd_get_streamparm,
1548 .set_streamparm = sd_set_streamparm, 1513 .set_streamparm = sd_set_streamparm,
1549}; 1514};
@@ -1572,6 +1537,7 @@ static struct usb_driver sd_driver = {
1572#ifdef CONFIG_PM 1537#ifdef CONFIG_PM
1573 .suspend = gspca_suspend, 1538 .suspend = gspca_suspend,
1574 .resume = gspca_resume, 1539 .resume = gspca_resume,
1540 .reset_resume = gspca_resume,
1575#endif 1541#endif
1576}; 1542};
1577 1543
diff --git a/drivers/media/video/gspca/ov534_9.c b/drivers/media/video/gspca/ov534_9.c
index 1fd41f0d2e95..c4cd028fe0b4 100644
--- a/drivers/media/video/gspca/ov534_9.c
+++ b/drivers/media/video/gspca/ov534_9.c
@@ -47,22 +47,9 @@ MODULE_AUTHOR("Jean-Francois Moine <moinejf@free.fr>");
47MODULE_DESCRIPTION("GSPCA/OV534_9 USB Camera Driver"); 47MODULE_DESCRIPTION("GSPCA/OV534_9 USB Camera Driver");
48MODULE_LICENSE("GPL"); 48MODULE_LICENSE("GPL");
49 49
50/* controls */
51enum e_ctrl {
52 BRIGHTNESS,
53 CONTRAST,
54 AUTOGAIN,
55 EXPOSURE,
56 SHARPNESS,
57 SATUR,
58 LIGHTFREQ,
59 NCTRLS /* number of controls */
60};
61
62/* specific webcam descriptor */ 50/* specific webcam descriptor */
63struct sd { 51struct sd {
64 struct gspca_dev gspca_dev; /* !! must be the first item */ 52 struct gspca_dev gspca_dev; /* !! must be the first item */
65 struct gspca_ctrl ctrls[NCTRLS];
66 __u32 last_pts; 53 __u32 last_pts;
67 u8 last_fid; 54 u8 last_fid;
68 55
@@ -75,103 +62,6 @@ enum sensors {
75 NSENSORS 62 NSENSORS
76}; 63};
77 64
78/* V4L2 controls supported by the driver */
79static void setbrightness(struct gspca_dev *gspca_dev);
80static void setcontrast(struct gspca_dev *gspca_dev);
81static void setautogain(struct gspca_dev *gspca_dev);
82static void setexposure(struct gspca_dev *gspca_dev);
83static void setsharpness(struct gspca_dev *gspca_dev);
84static void setsatur(struct gspca_dev *gspca_dev);
85static void setlightfreq(struct gspca_dev *gspca_dev);
86
87static const struct ctrl sd_ctrls[NCTRLS] = {
88[BRIGHTNESS] = {
89 {
90 .id = V4L2_CID_BRIGHTNESS,
91 .type = V4L2_CTRL_TYPE_INTEGER,
92 .name = "Brightness",
93 .minimum = 0,
94 .maximum = 15,
95 .step = 1,
96 .default_value = 7
97 },
98 .set_control = setbrightness
99 },
100[CONTRAST] = {
101 {
102 .id = V4L2_CID_CONTRAST,
103 .type = V4L2_CTRL_TYPE_INTEGER,
104 .name = "Contrast",
105 .minimum = 0,
106 .maximum = 15,
107 .step = 1,
108 .default_value = 3
109 },
110 .set_control = setcontrast
111 },
112[AUTOGAIN] = {
113 {
114 .id = V4L2_CID_AUTOGAIN,
115 .type = V4L2_CTRL_TYPE_BOOLEAN,
116 .name = "Autogain",
117 .minimum = 0,
118 .maximum = 1,
119 .step = 1,
120#define AUTOGAIN_DEF 1
121 .default_value = AUTOGAIN_DEF,
122 },
123 .set_control = setautogain
124 },
125[EXPOSURE] = {
126 {
127 .id = V4L2_CID_EXPOSURE,
128 .type = V4L2_CTRL_TYPE_INTEGER,
129 .name = "Exposure",
130 .minimum = 0,
131 .maximum = 3,
132 .step = 1,
133 .default_value = 0
134 },
135 .set_control = setexposure
136 },
137[SHARPNESS] = {
138 {
139 .id = V4L2_CID_SHARPNESS,
140 .type = V4L2_CTRL_TYPE_INTEGER,
141 .name = "Sharpness",
142 .minimum = -1, /* -1 = auto */
143 .maximum = 4,
144 .step = 1,
145 .default_value = -1
146 },
147 .set_control = setsharpness
148 },
149[SATUR] = {
150 {
151 .id = V4L2_CID_SATURATION,
152 .type = V4L2_CTRL_TYPE_INTEGER,
153 .name = "Saturation",
154 .minimum = 0,
155 .maximum = 4,
156 .step = 1,
157 .default_value = 2
158 },
159 .set_control = setsatur
160 },
161[LIGHTFREQ] = {
162 {
163 .id = V4L2_CID_POWER_LINE_FREQUENCY,
164 .type = V4L2_CTRL_TYPE_MENU,
165 .name = "Light frequency filter",
166 .minimum = 0,
167 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
168 .step = 1,
169 .default_value = 0
170 },
171 .set_control = setlightfreq
172 },
173};
174
175static const struct v4l2_pix_format ov965x_mode[] = { 65static const struct v4l2_pix_format ov965x_mode[] = {
176#define QVGA_MODE 0 66#define QVGA_MODE 0
177 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 67 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
@@ -1104,16 +994,14 @@ static void set_led(struct gspca_dev *gspca_dev, int status)
1104 } 994 }
1105} 995}
1106 996
1107static void setbrightness(struct gspca_dev *gspca_dev) 997static void setbrightness(struct gspca_dev *gspca_dev, s32 brightness)
1108{ 998{
1109 struct sd *sd = (struct sd *) gspca_dev; 999 struct sd *sd = (struct sd *) gspca_dev;
1110 u8 val; 1000 u8 val;
1111 s8 sval; 1001 s8 sval;
1112 1002
1113 if (gspca_dev->ctrl_dis & (1 << BRIGHTNESS))
1114 return;
1115 if (sd->sensor == SENSOR_OV562x) { 1003 if (sd->sensor == SENSOR_OV562x) {
1116 sval = sd->ctrls[BRIGHTNESS].val; 1004 sval = brightness;
1117 val = 0x76; 1005 val = 0x76;
1118 val += sval; 1006 val += sval;
1119 sccb_write(gspca_dev, 0x24, val); 1007 sccb_write(gspca_dev, 0x24, val);
@@ -1128,7 +1016,7 @@ static void setbrightness(struct gspca_dev *gspca_dev)
1128 val = 0xe6; 1016 val = 0xe6;
1129 sccb_write(gspca_dev, 0x26, val); 1017 sccb_write(gspca_dev, 0x26, val);
1130 } else { 1018 } else {
1131 val = sd->ctrls[BRIGHTNESS].val; 1019 val = brightness;
1132 if (val < 8) 1020 if (val < 8)
1133 val = 15 - val; /* f .. 8 */ 1021 val = 15 - val; /* f .. 8 */
1134 else 1022 else
@@ -1138,43 +1026,32 @@ static void setbrightness(struct gspca_dev *gspca_dev)
1138 } 1026 }
1139} 1027}
1140 1028
1141static void setcontrast(struct gspca_dev *gspca_dev) 1029static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
1142{ 1030{
1143 struct sd *sd = (struct sd *) gspca_dev;
1144
1145 if (gspca_dev->ctrl_dis & (1 << CONTRAST))
1146 return;
1147 sccb_write(gspca_dev, 0x56, /* cnst1 - contrast 1 ctrl coeff */ 1031 sccb_write(gspca_dev, 0x56, /* cnst1 - contrast 1 ctrl coeff */
1148 sd->ctrls[CONTRAST].val << 4); 1032 val << 4);
1149} 1033}
1150 1034
1151static void setautogain(struct gspca_dev *gspca_dev) 1035static void setautogain(struct gspca_dev *gspca_dev, s32 autogain)
1152{ 1036{
1153 struct sd *sd = (struct sd *) gspca_dev;
1154 u8 val; 1037 u8 val;
1155 1038
1156 if (gspca_dev->ctrl_dis & (1 << AUTOGAIN))
1157 return;
1158/*fixme: should adjust agc/awb/aec by different controls */ 1039/*fixme: should adjust agc/awb/aec by different controls */
1159 val = sccb_read(gspca_dev, 0x13); /* com8 */ 1040 val = sccb_read(gspca_dev, 0x13); /* com8 */
1160 sccb_write(gspca_dev, 0xff, 0x00); 1041 sccb_write(gspca_dev, 0xff, 0x00);
1161 if (sd->ctrls[AUTOGAIN].val) 1042 if (autogain)
1162 val |= 0x05; /* agc & aec */ 1043 val |= 0x05; /* agc & aec */
1163 else 1044 else
1164 val &= 0xfa; 1045 val &= 0xfa;
1165 sccb_write(gspca_dev, 0x13, val); 1046 sccb_write(gspca_dev, 0x13, val);
1166} 1047}
1167 1048
1168static void setexposure(struct gspca_dev *gspca_dev) 1049static void setexposure(struct gspca_dev *gspca_dev, s32 exposure)
1169{ 1050{
1170 struct sd *sd = (struct sd *) gspca_dev;
1171 u8 val;
1172 static const u8 expo[4] = {0x00, 0x25, 0x38, 0x5e}; 1051 static const u8 expo[4] = {0x00, 0x25, 0x38, 0x5e};
1052 u8 val;
1173 1053
1174 if (gspca_dev->ctrl_dis & (1 << EXPOSURE)) 1054 sccb_write(gspca_dev, 0x10, expo[exposure]); /* aec[9:2] */
1175 return;
1176 sccb_write(gspca_dev, 0x10, /* aec[9:2] */
1177 expo[sd->ctrls[EXPOSURE].val]);
1178 1055
1179 val = sccb_read(gspca_dev, 0x13); /* com8 */ 1056 val = sccb_read(gspca_dev, 0x13); /* com8 */
1180 sccb_write(gspca_dev, 0xff, 0x00); 1057 sccb_write(gspca_dev, 0xff, 0x00);
@@ -1185,14 +1062,8 @@ static void setexposure(struct gspca_dev *gspca_dev)
1185 sccb_write(gspca_dev, 0xa1, val & 0xe0); /* aec[15:10] = 0 */ 1062 sccb_write(gspca_dev, 0xa1, val & 0xe0); /* aec[15:10] = 0 */
1186} 1063}
1187 1064
1188static void setsharpness(struct gspca_dev *gspca_dev) 1065static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
1189{ 1066{
1190 struct sd *sd = (struct sd *) gspca_dev;
1191 s8 val;
1192
1193 if (gspca_dev->ctrl_dis & (1 << SHARPNESS))
1194 return;
1195 val = sd->ctrls[SHARPNESS].val;
1196 if (val < 0) { /* auto */ 1067 if (val < 0) { /* auto */
1197 val = sccb_read(gspca_dev, 0x42); /* com17 */ 1068 val = sccb_read(gspca_dev, 0x42); /* com17 */
1198 sccb_write(gspca_dev, 0xff, 0x00); 1069 sccb_write(gspca_dev, 0xff, 0x00);
@@ -1209,9 +1080,8 @@ static void setsharpness(struct gspca_dev *gspca_dev)
1209 sccb_write(gspca_dev, 0x42, val & 0xbf); 1080 sccb_write(gspca_dev, 0x42, val & 0xbf);
1210} 1081}
1211 1082
1212static void setsatur(struct gspca_dev *gspca_dev) 1083static void setsatur(struct gspca_dev *gspca_dev, s32 val)
1213{ 1084{
1214 struct sd *sd = (struct sd *) gspca_dev;
1215 u8 val1, val2, val3; 1085 u8 val1, val2, val3;
1216 static const u8 matrix[5][2] = { 1086 static const u8 matrix[5][2] = {
1217 {0x14, 0x38}, 1087 {0x14, 0x38},
@@ -1221,10 +1091,8 @@ static void setsatur(struct gspca_dev *gspca_dev)
1221 {0x48, 0x90} 1091 {0x48, 0x90}
1222 }; 1092 };
1223 1093
1224 if (gspca_dev->ctrl_dis & (1 << SATUR)) 1094 val1 = matrix[val][0];
1225 return; 1095 val2 = matrix[val][1];
1226 val1 = matrix[sd->ctrls[SATUR].val][0];
1227 val2 = matrix[sd->ctrls[SATUR].val][1];
1228 val3 = val1 + val2; 1096 val3 = val1 + val2;
1229 sccb_write(gspca_dev, 0x4f, val3); /* matrix coeff */ 1097 sccb_write(gspca_dev, 0x4f, val3); /* matrix coeff */
1230 sccb_write(gspca_dev, 0x50, val3); 1098 sccb_write(gspca_dev, 0x50, val3);
@@ -1239,16 +1107,13 @@ static void setsatur(struct gspca_dev *gspca_dev)
1239 sccb_write(gspca_dev, 0x41, val1); 1107 sccb_write(gspca_dev, 0x41, val1);
1240} 1108}
1241 1109
1242static void setlightfreq(struct gspca_dev *gspca_dev) 1110static void setlightfreq(struct gspca_dev *gspca_dev, s32 freq)
1243{ 1111{
1244 struct sd *sd = (struct sd *) gspca_dev;
1245 u8 val; 1112 u8 val;
1246 1113
1247 if (gspca_dev->ctrl_dis & (1 << LIGHTFREQ))
1248 return;
1249 val = sccb_read(gspca_dev, 0x13); /* com8 */ 1114 val = sccb_read(gspca_dev, 0x13); /* com8 */
1250 sccb_write(gspca_dev, 0xff, 0x00); 1115 sccb_write(gspca_dev, 0xff, 0x00);
1251 if (sd->ctrls[LIGHTFREQ].val == 0) { 1116 if (freq == 0) {
1252 sccb_write(gspca_dev, 0x13, val & 0xdf); 1117 sccb_write(gspca_dev, 0x13, val & 0xdf);
1253 return; 1118 return;
1254 } 1119 }
@@ -1256,7 +1121,7 @@ static void setlightfreq(struct gspca_dev *gspca_dev)
1256 1121
1257 val = sccb_read(gspca_dev, 0x42); /* com17 */ 1122 val = sccb_read(gspca_dev, 0x42); /* com17 */
1258 sccb_write(gspca_dev, 0xff, 0x00); 1123 sccb_write(gspca_dev, 0xff, 0x00);
1259 if (sd->ctrls[LIGHTFREQ].val == 1) 1124 if (freq == 1)
1260 val |= 0x01; 1125 val |= 0x01;
1261 else 1126 else
1262 val &= 0xfe; 1127 val &= 0xfe;
@@ -1267,13 +1132,6 @@ static void setlightfreq(struct gspca_dev *gspca_dev)
1267static int sd_config(struct gspca_dev *gspca_dev, 1132static int sd_config(struct gspca_dev *gspca_dev,
1268 const struct usb_device_id *id) 1133 const struct usb_device_id *id)
1269{ 1134{
1270 struct sd *sd = (struct sd *) gspca_dev;
1271
1272 gspca_dev->cam.ctrls = sd->ctrls;
1273
1274#if AUTOGAIN_DEF != 0
1275 gspca_dev->ctrl_inac |= (1 << EXPOSURE);
1276#endif
1277 return 0; 1135 return 0;
1278} 1136}
1279 1137
@@ -1330,9 +1188,6 @@ static int sd_init(struct gspca_dev *gspca_dev)
1330 gspca_dev->cam.cam_mode = ov971x_mode; 1188 gspca_dev->cam.cam_mode = ov971x_mode;
1331 gspca_dev->cam.nmodes = ARRAY_SIZE(ov971x_mode); 1189 gspca_dev->cam.nmodes = ARRAY_SIZE(ov971x_mode);
1332 1190
1333 /* no control yet */
1334 gspca_dev->ctrl_dis = (1 << NCTRLS) - 1;
1335
1336 gspca_dev->cam.bulk = 1; 1191 gspca_dev->cam.bulk = 1;
1337 gspca_dev->cam.bulk_size = 16384; 1192 gspca_dev->cam.bulk_size = 16384;
1338 gspca_dev->cam.bulk_nurbs = 2; 1193 gspca_dev->cam.bulk_nurbs = 2;
@@ -1358,16 +1213,6 @@ static int sd_init(struct gspca_dev *gspca_dev)
1358 reg_w(gspca_dev, 0x56, 0x17); 1213 reg_w(gspca_dev, 0x56, 0x17);
1359 } else if ((sensor_id & 0xfff0) == 0x5620) { 1214 } else if ((sensor_id & 0xfff0) == 0x5620) {
1360 sd->sensor = SENSOR_OV562x; 1215 sd->sensor = SENSOR_OV562x;
1361 gspca_dev->ctrl_dis = (1 << CONTRAST) |
1362 (1 << AUTOGAIN) |
1363 (1 << EXPOSURE) |
1364 (1 << SHARPNESS) |
1365 (1 << SATUR) |
1366 (1 << LIGHTFREQ);
1367
1368 sd->ctrls[BRIGHTNESS].min = -90;
1369 sd->ctrls[BRIGHTNESS].max = 90;
1370 sd->ctrls[BRIGHTNESS].def = 0;
1371 gspca_dev->cam.cam_mode = ov562x_mode; 1216 gspca_dev->cam.cam_mode = ov562x_mode;
1372 gspca_dev->cam.nmodes = ARRAY_SIZE(ov562x_mode); 1217 gspca_dev->cam.nmodes = ARRAY_SIZE(ov562x_mode);
1373 1218
@@ -1390,10 +1235,9 @@ static int sd_start(struct gspca_dev *gspca_dev)
1390 1235
1391 if (sd->sensor == SENSOR_OV971x) 1236 if (sd->sensor == SENSOR_OV971x)
1392 return gspca_dev->usb_err; 1237 return gspca_dev->usb_err;
1393 else if (sd->sensor == SENSOR_OV562x) { 1238 if (sd->sensor == SENSOR_OV562x)
1394 setbrightness(gspca_dev);
1395 return gspca_dev->usb_err; 1239 return gspca_dev->usb_err;
1396 } 1240
1397 switch (gspca_dev->curr_mode) { 1241 switch (gspca_dev->curr_mode) {
1398 case QVGA_MODE: /* 320x240 */ 1242 case QVGA_MODE: /* 320x240 */
1399 sccb_w_array(gspca_dev, ov965x_start_1_vga, 1243 sccb_w_array(gspca_dev, ov965x_start_1_vga,
@@ -1437,13 +1281,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
1437 ARRAY_SIZE(ov965x_start_2_sxga)); 1281 ARRAY_SIZE(ov965x_start_2_sxga));
1438 break; 1282 break;
1439 } 1283 }
1440 setlightfreq(gspca_dev);
1441 setautogain(gspca_dev);
1442 setbrightness(gspca_dev);
1443 setcontrast(gspca_dev);
1444 setexposure(gspca_dev);
1445 setsharpness(gspca_dev);
1446 setsatur(gspca_dev);
1447 1284
1448 reg_w(gspca_dev, 0xe0, 0x00); 1285 reg_w(gspca_dev, 0xe0, 0x00);
1449 reg_w(gspca_dev, 0xe0, 0x00); 1286 reg_w(gspca_dev, 0xe0, 0x00);
@@ -1541,38 +1378,94 @@ scan_next:
1541 } while (remaining_len > 0); 1378 } while (remaining_len > 0);
1542} 1379}
1543 1380
1544static int sd_querymenu(struct gspca_dev *gspca_dev, 1381static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1545 struct v4l2_querymenu *menu)
1546{ 1382{
1547 switch (menu->id) { 1383 struct gspca_dev *gspca_dev =
1384 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1385
1386 gspca_dev->usb_err = 0;
1387
1388 if (!gspca_dev->streaming)
1389 return 0;
1390
1391 switch (ctrl->id) {
1392 case V4L2_CID_BRIGHTNESS:
1393 setbrightness(gspca_dev, ctrl->val);
1394 break;
1395 case V4L2_CID_CONTRAST:
1396 setcontrast(gspca_dev, ctrl->val);
1397 break;
1398 case V4L2_CID_SATURATION:
1399 setsatur(gspca_dev, ctrl->val);
1400 break;
1548 case V4L2_CID_POWER_LINE_FREQUENCY: 1401 case V4L2_CID_POWER_LINE_FREQUENCY:
1549 switch (menu->index) { 1402 setlightfreq(gspca_dev, ctrl->val);
1550 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1551 strcpy((char *) menu->name, "NoFliker");
1552 return 0;
1553 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1554 strcpy((char *) menu->name, "50 Hz");
1555 return 0;
1556 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
1557 strcpy((char *) menu->name, "60 Hz");
1558 return 0;
1559 }
1560 break; 1403 break;
1404 case V4L2_CID_SHARPNESS:
1405 setsharpness(gspca_dev, ctrl->val);
1406 break;
1407 case V4L2_CID_AUTOGAIN:
1408 if (ctrl->is_new)
1409 setautogain(gspca_dev, ctrl->val);
1410 if (!ctrl->val && gspca_dev->exposure->is_new)
1411 setexposure(gspca_dev, gspca_dev->exposure->val);
1412 break;
1413 }
1414 return gspca_dev->usb_err;
1415}
1416
1417static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1418 .s_ctrl = sd_s_ctrl,
1419};
1420
1421static int sd_init_controls(struct gspca_dev *gspca_dev)
1422{
1423 struct sd *sd = (struct sd *)gspca_dev;
1424 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1425
1426 if (sd->sensor == SENSOR_OV971x)
1427 return 0;
1428 gspca_dev->vdev.ctrl_handler = hdl;
1429 v4l2_ctrl_handler_init(hdl, 7);
1430 if (sd->sensor == SENSOR_OV562x) {
1431 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1432 V4L2_CID_BRIGHTNESS, -90, 90, 1, 0);
1433 } else {
1434 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1435 V4L2_CID_BRIGHTNESS, 0, 15, 1, 7);
1436 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1437 V4L2_CID_CONTRAST, 0, 15, 1, 3);
1438 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1439 V4L2_CID_SATURATION, 0, 4, 1, 2);
1440 /* -1 = auto */
1441 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1442 V4L2_CID_SHARPNESS, -1, 4, 1, -1);
1443 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1444 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1445 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1446 V4L2_CID_EXPOSURE, 0, 3, 1, 0);
1447 v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
1448 V4L2_CID_POWER_LINE_FREQUENCY,
1449 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
1450 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
1561 } 1451 }
1562 return -EINVAL; 1452
1453 if (hdl->error) {
1454 pr_err("Could not initialize controls\n");
1455 return hdl->error;
1456 }
1457 return 0;
1563} 1458}
1564 1459
1565/* sub-driver description */ 1460/* sub-driver description */
1566static const struct sd_desc sd_desc = { 1461static const struct sd_desc sd_desc = {
1567 .name = MODULE_NAME, 1462 .name = MODULE_NAME,
1568 .ctrls = sd_ctrls,
1569 .nctrls = NCTRLS,
1570 .config = sd_config, 1463 .config = sd_config,
1571 .init = sd_init, 1464 .init = sd_init,
1465 .init_controls = sd_init_controls,
1572 .start = sd_start, 1466 .start = sd_start,
1573 .stopN = sd_stopN, 1467 .stopN = sd_stopN,
1574 .pkt_scan = sd_pkt_scan, 1468 .pkt_scan = sd_pkt_scan,
1575 .querymenu = sd_querymenu,
1576}; 1469};
1577 1470
1578/* -- module initialisation -- */ 1471/* -- module initialisation -- */
@@ -1600,6 +1493,7 @@ static struct usb_driver sd_driver = {
1600#ifdef CONFIG_PM 1493#ifdef CONFIG_PM
1601 .suspend = gspca_suspend, 1494 .suspend = gspca_suspend,
1602 .resume = gspca_resume, 1495 .resume = gspca_resume,
1496 .reset_resume = gspca_resume,
1603#endif 1497#endif
1604}; 1498};
1605 1499
diff --git a/drivers/media/video/gspca/pac207.c b/drivers/media/video/gspca/pac207.c
index fa661c6d6d55..d236d1791f78 100644
--- a/drivers/media/video/gspca/pac207.c
+++ b/drivers/media/video/gspca/pac207.c
@@ -462,6 +462,7 @@ static struct usb_driver sd_driver = {
462#ifdef CONFIG_PM 462#ifdef CONFIG_PM
463 .suspend = gspca_suspend, 463 .suspend = gspca_suspend,
464 .resume = gspca_resume, 464 .resume = gspca_resume,
465 .reset_resume = gspca_resume,
465#endif 466#endif
466}; 467};
467 468
diff --git a/drivers/media/video/gspca/pac7302.c b/drivers/media/video/gspca/pac7302.c
index a0369a58c4bb..4877f7ab3d59 100644
--- a/drivers/media/video/gspca/pac7302.c
+++ b/drivers/media/video/gspca/pac7302.c
@@ -84,31 +84,31 @@
84/* Include pac common sof detection functions */ 84/* Include pac common sof detection functions */
85#include "pac_common.h" 85#include "pac_common.h"
86 86
87#define PAC7302_GAIN_DEFAULT 15
88#define PAC7302_GAIN_KNEE 42
89#define PAC7302_EXPOSURE_DEFAULT 66 /* 33 ms / 30 fps */
90#define PAC7302_EXPOSURE_KNEE 133 /* 66 ms / 15 fps */
91
87MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>, " 92MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>, "
88 "Thomas Kaiser thomas@kaiser-linux.li"); 93 "Thomas Kaiser thomas@kaiser-linux.li");
89MODULE_DESCRIPTION("Pixart PAC7302"); 94MODULE_DESCRIPTION("Pixart PAC7302");
90MODULE_LICENSE("GPL"); 95MODULE_LICENSE("GPL");
91 96
92enum e_ctrl {
93 BRIGHTNESS,
94 CONTRAST,
95 COLORS,
96 WHITE_BALANCE,
97 RED_BALANCE,
98 BLUE_BALANCE,
99 GAIN,
100 AUTOGAIN,
101 EXPOSURE,
102 VFLIP,
103 HFLIP,
104 NCTRLS /* number of controls */
105};
106
107struct sd { 97struct sd {
108 struct gspca_dev gspca_dev; /* !! must be the first item */ 98 struct gspca_dev gspca_dev; /* !! must be the first item */
109 99
110 struct gspca_ctrl ctrls[NCTRLS]; 100 struct { /* brightness / contrast cluster */
111 101 struct v4l2_ctrl *brightness;
102 struct v4l2_ctrl *contrast;
103 };
104 struct v4l2_ctrl *saturation;
105 struct v4l2_ctrl *white_balance;
106 struct v4l2_ctrl *red_balance;
107 struct v4l2_ctrl *blue_balance;
108 struct { /* flip cluster */
109 struct v4l2_ctrl *hflip;
110 struct v4l2_ctrl *vflip;
111 };
112 u8 flags; 112 u8 flags;
113#define FL_HFLIP 0x01 /* mirrored by default */ 113#define FL_HFLIP 0x01 /* mirrored by default */
114#define FL_VFLIP 0x02 /* vertical flipped by default */ 114#define FL_VFLIP 0x02 /* vertical flipped by default */
@@ -119,160 +119,6 @@ struct sd {
119 atomic_t avg_lum; 119 atomic_t avg_lum;
120}; 120};
121 121
122/* V4L2 controls supported by the driver */
123static void setbrightcont(struct gspca_dev *gspca_dev);
124static void setcolors(struct gspca_dev *gspca_dev);
125static void setwhitebalance(struct gspca_dev *gspca_dev);
126static void setredbalance(struct gspca_dev *gspca_dev);
127static void setbluebalance(struct gspca_dev *gspca_dev);
128static void setgain(struct gspca_dev *gspca_dev);
129static void setexposure(struct gspca_dev *gspca_dev);
130static void setautogain(struct gspca_dev *gspca_dev);
131static void sethvflip(struct gspca_dev *gspca_dev);
132
133static const struct ctrl sd_ctrls[] = {
134[BRIGHTNESS] = {
135 {
136 .id = V4L2_CID_BRIGHTNESS,
137 .type = V4L2_CTRL_TYPE_INTEGER,
138 .name = "Brightness",
139 .minimum = 0,
140#define BRIGHTNESS_MAX 0x20
141 .maximum = BRIGHTNESS_MAX,
142 .step = 1,
143 .default_value = 0x10,
144 },
145 .set_control = setbrightcont
146 },
147[CONTRAST] = {
148 {
149 .id = V4L2_CID_CONTRAST,
150 .type = V4L2_CTRL_TYPE_INTEGER,
151 .name = "Contrast",
152 .minimum = 0,
153#define CONTRAST_MAX 255
154 .maximum = CONTRAST_MAX,
155 .step = 1,
156 .default_value = 127,
157 },
158 .set_control = setbrightcont
159 },
160[COLORS] = {
161 {
162 .id = V4L2_CID_SATURATION,
163 .type = V4L2_CTRL_TYPE_INTEGER,
164 .name = "Saturation",
165 .minimum = 0,
166#define COLOR_MAX 255
167 .maximum = COLOR_MAX,
168 .step = 1,
169 .default_value = 127
170 },
171 .set_control = setcolors
172 },
173[WHITE_BALANCE] = {
174 {
175 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
176 .type = V4L2_CTRL_TYPE_INTEGER,
177 .name = "White Balance",
178 .minimum = 0,
179 .maximum = 255,
180 .step = 1,
181 .default_value = 4,
182 },
183 .set_control = setwhitebalance
184 },
185[RED_BALANCE] = {
186 {
187 .id = V4L2_CID_RED_BALANCE,
188 .type = V4L2_CTRL_TYPE_INTEGER,
189 .name = "Red",
190 .minimum = 0,
191 .maximum = 3,
192 .step = 1,
193 .default_value = 1,
194 },
195 .set_control = setredbalance
196 },
197[BLUE_BALANCE] = {
198 {
199 .id = V4L2_CID_BLUE_BALANCE,
200 .type = V4L2_CTRL_TYPE_INTEGER,
201 .name = "Blue",
202 .minimum = 0,
203 .maximum = 3,
204 .step = 1,
205 .default_value = 1,
206 },
207 .set_control = setbluebalance
208 },
209[GAIN] = {
210 {
211 .id = V4L2_CID_GAIN,
212 .type = V4L2_CTRL_TYPE_INTEGER,
213 .name = "Gain",
214 .minimum = 0,
215 .maximum = 62,
216 .step = 1,
217#define GAIN_DEF 15
218#define GAIN_KNEE 46
219 .default_value = GAIN_DEF,
220 },
221 .set_control = setgain
222 },
223[EXPOSURE] = {
224 {
225 .id = V4L2_CID_EXPOSURE,
226 .type = V4L2_CTRL_TYPE_INTEGER,
227 .name = "Exposure",
228 .minimum = 0,
229 .maximum = 1023,
230 .step = 1,
231#define EXPOSURE_DEF 66 /* 33 ms / 30 fps */
232#define EXPOSURE_KNEE 133 /* 66 ms / 15 fps */
233 .default_value = EXPOSURE_DEF,
234 },
235 .set_control = setexposure
236 },
237[AUTOGAIN] = {
238 {
239 .id = V4L2_CID_AUTOGAIN,
240 .type = V4L2_CTRL_TYPE_BOOLEAN,
241 .name = "Auto Gain",
242 .minimum = 0,
243 .maximum = 1,
244 .step = 1,
245#define AUTOGAIN_DEF 1
246 .default_value = AUTOGAIN_DEF,
247 },
248 .set_control = setautogain,
249 },
250[HFLIP] = {
251 {
252 .id = V4L2_CID_HFLIP,
253 .type = V4L2_CTRL_TYPE_BOOLEAN,
254 .name = "Mirror",
255 .minimum = 0,
256 .maximum = 1,
257 .step = 1,
258 .default_value = 0,
259 },
260 .set_control = sethvflip,
261 },
262[VFLIP] = {
263 {
264 .id = V4L2_CID_VFLIP,
265 .type = V4L2_CTRL_TYPE_BOOLEAN,
266 .name = "Vflip",
267 .minimum = 0,
268 .maximum = 1,
269 .step = 1,
270 .default_value = 0,
271 },
272 .set_control = sethvflip
273 },
274};
275
276static const struct v4l2_pix_format vga_mode[] = { 122static const struct v4l2_pix_format vga_mode[] = {
277 {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE, 123 {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
278 .bytesperline = 640, 124 .bytesperline = 640,
@@ -516,8 +362,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
516 cam->cam_mode = vga_mode; /* only 640x480 */ 362 cam->cam_mode = vga_mode; /* only 640x480 */
517 cam->nmodes = ARRAY_SIZE(vga_mode); 363 cam->nmodes = ARRAY_SIZE(vga_mode);
518 364
519 gspca_dev->cam.ctrls = sd->ctrls;
520
521 sd->flags = id->driver_info; 365 sd->flags = id->driver_info;
522 return 0; 366 return 0;
523} 367}
@@ -536,9 +380,9 @@ static void setbrightcont(struct gspca_dev *gspca_dev)
536 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */ 380 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
537 for (i = 0; i < 10; i++) { 381 for (i = 0; i < 10; i++) {
538 v = max[i]; 382 v = max[i];
539 v += (sd->ctrls[BRIGHTNESS].val - BRIGHTNESS_MAX) 383 v += (sd->brightness->val - sd->brightness->maximum)
540 * 150 / BRIGHTNESS_MAX; /* 200 ? */ 384 * 150 / sd->brightness->maximum; /* 200 ? */
541 v -= delta[i] * sd->ctrls[CONTRAST].val / CONTRAST_MAX; 385 v -= delta[i] * sd->contrast->val / sd->contrast->maximum;
542 if (v < 0) 386 if (v < 0)
543 v = 0; 387 v = 0;
544 else if (v > 0xff) 388 else if (v > 0xff)
@@ -561,7 +405,8 @@ static void setcolors(struct gspca_dev *gspca_dev)
561 reg_w(gspca_dev, 0x11, 0x01); 405 reg_w(gspca_dev, 0x11, 0x01);
562 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */ 406 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
563 for (i = 0; i < 9; i++) { 407 for (i = 0; i < 9; i++) {
564 v = a[i] * sd->ctrls[COLORS].val / COLOR_MAX + b[i]; 408 v = a[i] * sd->saturation->val / sd->saturation->maximum;
409 v += b[i];
565 reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07); 410 reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07);
566 reg_w(gspca_dev, 0x0f + 2 * i + 1, v); 411 reg_w(gspca_dev, 0x0f + 2 * i + 1, v);
567 } 412 }
@@ -573,7 +418,7 @@ static void setwhitebalance(struct gspca_dev *gspca_dev)
573 struct sd *sd = (struct sd *) gspca_dev; 418 struct sd *sd = (struct sd *) gspca_dev;
574 419
575 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */ 420 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
576 reg_w(gspca_dev, 0xc6, sd->ctrls[WHITE_BALANCE].val); 421 reg_w(gspca_dev, 0xc6, sd->white_balance->val);
577 422
578 reg_w(gspca_dev, 0xdc, 0x01); 423 reg_w(gspca_dev, 0xdc, 0x01);
579} 424}
@@ -583,7 +428,7 @@ static void setredbalance(struct gspca_dev *gspca_dev)
583 struct sd *sd = (struct sd *) gspca_dev; 428 struct sd *sd = (struct sd *) gspca_dev;
584 429
585 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */ 430 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
586 reg_w(gspca_dev, 0xc5, sd->ctrls[RED_BALANCE].val); 431 reg_w(gspca_dev, 0xc5, sd->red_balance->val);
587 432
588 reg_w(gspca_dev, 0xdc, 0x01); 433 reg_w(gspca_dev, 0xdc, 0x01);
589} 434}
@@ -593,22 +438,21 @@ static void setbluebalance(struct gspca_dev *gspca_dev)
593 struct sd *sd = (struct sd *) gspca_dev; 438 struct sd *sd = (struct sd *) gspca_dev;
594 439
595 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */ 440 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
596 reg_w(gspca_dev, 0xc7, sd->ctrls[BLUE_BALANCE].val); 441 reg_w(gspca_dev, 0xc7, sd->blue_balance->val);
597 442
598 reg_w(gspca_dev, 0xdc, 0x01); 443 reg_w(gspca_dev, 0xdc, 0x01);
599} 444}
600 445
601static void setgain(struct gspca_dev *gspca_dev) 446static void setgain(struct gspca_dev *gspca_dev)
602{ 447{
603 struct sd *sd = (struct sd *) gspca_dev;
604 u8 reg10, reg12; 448 u8 reg10, reg12;
605 449
606 if (sd->ctrls[GAIN].val < 32) { 450 if (gspca_dev->gain->val < 32) {
607 reg10 = sd->ctrls[GAIN].val; 451 reg10 = gspca_dev->gain->val;
608 reg12 = 0; 452 reg12 = 0;
609 } else { 453 } else {
610 reg10 = 31; 454 reg10 = 31;
611 reg12 = sd->ctrls[GAIN].val - 31; 455 reg12 = gspca_dev->gain->val - 31;
612 } 456 }
613 457
614 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */ 458 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
@@ -621,7 +465,6 @@ static void setgain(struct gspca_dev *gspca_dev)
621 465
622static void setexposure(struct gspca_dev *gspca_dev) 466static void setexposure(struct gspca_dev *gspca_dev)
623{ 467{
624 struct sd *sd = (struct sd *) gspca_dev;
625 u8 clockdiv; 468 u8 clockdiv;
626 u16 exposure; 469 u16 exposure;
627 470
@@ -630,7 +473,7 @@ static void setexposure(struct gspca_dev *gspca_dev)
630 * no fps according to the formula: 90 / reg. sd->exposure is the 473 * no fps according to the formula: 90 / reg. sd->exposure is the
631 * desired exposure time in 0.5 ms. 474 * desired exposure time in 0.5 ms.
632 */ 475 */
633 clockdiv = (90 * sd->ctrls[EXPOSURE].val + 1999) / 2000; 476 clockdiv = (90 * gspca_dev->exposure->val + 1999) / 2000;
634 477
635 /* 478 /*
636 * Note clockdiv = 3 also works, but when running at 30 fps, depending 479 * Note clockdiv = 3 also works, but when running at 30 fps, depending
@@ -655,7 +498,7 @@ static void setexposure(struct gspca_dev *gspca_dev)
655 * frame exposure time in ms = 1000 * clockdiv / 90 -> 498 * frame exposure time in ms = 1000 * clockdiv / 90 ->
656 * exposure = (sd->exposure / 2) * 448 / (1000 * clockdiv / 90) 499 * exposure = (sd->exposure / 2) * 448 / (1000 * clockdiv / 90)
657 */ 500 */
658 exposure = (sd->ctrls[EXPOSURE].val * 45 * 448) / (1000 * clockdiv); 501 exposure = (gspca_dev->exposure->val * 45 * 448) / (1000 * clockdiv);
659 /* 0 = use full frametime, 448 = no exposure, reverse it */ 502 /* 0 = use full frametime, 448 = no exposure, reverse it */
660 exposure = 448 - exposure; 503 exposure = 448 - exposure;
661 504
@@ -668,37 +511,15 @@ static void setexposure(struct gspca_dev *gspca_dev)
668 reg_w(gspca_dev, 0x11, 0x01); 511 reg_w(gspca_dev, 0x11, 0x01);
669} 512}
670 513
671static void setautogain(struct gspca_dev *gspca_dev)
672{
673 struct sd *sd = (struct sd *) gspca_dev;
674
675 /*
676 * When switching to autogain set defaults to make sure
677 * we are on a valid point of the autogain gain /
678 * exposure knee graph, and give this change time to
679 * take effect before doing autogain.
680 */
681 if (sd->ctrls[AUTOGAIN].val) {
682 sd->ctrls[EXPOSURE].val = EXPOSURE_DEF;
683 sd->ctrls[GAIN].val = GAIN_DEF;
684 sd->autogain_ignore_frames =
685 PAC_AUTOGAIN_IGNORE_FRAMES;
686 } else {
687 sd->autogain_ignore_frames = -1;
688 }
689 setexposure(gspca_dev);
690 setgain(gspca_dev);
691}
692
693static void sethvflip(struct gspca_dev *gspca_dev) 514static void sethvflip(struct gspca_dev *gspca_dev)
694{ 515{
695 struct sd *sd = (struct sd *) gspca_dev; 516 struct sd *sd = (struct sd *) gspca_dev;
696 u8 data, hflip, vflip; 517 u8 data, hflip, vflip;
697 518
698 hflip = sd->ctrls[HFLIP].val; 519 hflip = sd->hflip->val;
699 if (sd->flags & FL_HFLIP) 520 if (sd->flags & FL_HFLIP)
700 hflip = !hflip; 521 hflip = !hflip;
701 vflip = sd->ctrls[VFLIP].val; 522 vflip = sd->vflip->val;
702 if (sd->flags & FL_VFLIP) 523 if (sd->flags & FL_VFLIP)
703 vflip = !vflip; 524 vflip = !vflip;
704 525
@@ -717,6 +538,112 @@ static int sd_init(struct gspca_dev *gspca_dev)
717 return gspca_dev->usb_err; 538 return gspca_dev->usb_err;
718} 539}
719 540
541static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
542{
543 struct gspca_dev *gspca_dev =
544 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
545 struct sd *sd = (struct sd *)gspca_dev;
546
547 gspca_dev->usb_err = 0;
548
549 if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
550 /* when switching to autogain set defaults to make sure
551 we are on a valid point of the autogain gain /
552 exposure knee graph, and give this change time to
553 take effect before doing autogain. */
554 gspca_dev->exposure->val = PAC7302_EXPOSURE_DEFAULT;
555 gspca_dev->gain->val = PAC7302_GAIN_DEFAULT;
556 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
557 }
558
559 if (!gspca_dev->streaming)
560 return 0;
561
562 switch (ctrl->id) {
563 case V4L2_CID_BRIGHTNESS:
564 setbrightcont(gspca_dev);
565 break;
566 case V4L2_CID_SATURATION:
567 setcolors(gspca_dev);
568 break;
569 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
570 setwhitebalance(gspca_dev);
571 break;
572 case V4L2_CID_RED_BALANCE:
573 setredbalance(gspca_dev);
574 break;
575 case V4L2_CID_BLUE_BALANCE:
576 setbluebalance(gspca_dev);
577 break;
578 case V4L2_CID_AUTOGAIN:
579 if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
580 setexposure(gspca_dev);
581 if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
582 setgain(gspca_dev);
583 break;
584 case V4L2_CID_HFLIP:
585 sethvflip(gspca_dev);
586 break;
587 default:
588 return -EINVAL;
589 }
590 return gspca_dev->usb_err;
591}
592
593static const struct v4l2_ctrl_ops sd_ctrl_ops = {
594 .s_ctrl = sd_s_ctrl,
595};
596
597/* this function is called at probe time */
598static int sd_init_controls(struct gspca_dev *gspca_dev)
599{
600 struct sd *sd = (struct sd *) gspca_dev;
601 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
602
603 gspca_dev->vdev.ctrl_handler = hdl;
604 v4l2_ctrl_handler_init(hdl, 11);
605
606 sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
607 V4L2_CID_BRIGHTNESS, 0, 32, 1, 16);
608 sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
609 V4L2_CID_CONTRAST, 0, 255, 1, 127);
610
611 sd->saturation = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
612 V4L2_CID_SATURATION, 0, 255, 1, 127);
613 sd->white_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
614 V4L2_CID_WHITE_BALANCE_TEMPERATURE,
615 0, 255, 1, 4);
616 sd->red_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
617 V4L2_CID_RED_BALANCE, 0, 3, 1, 1);
618 sd->blue_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
619 V4L2_CID_RED_BALANCE, 0, 3, 1, 1);
620
621 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
622 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
623 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
624 V4L2_CID_EXPOSURE, 0, 1023, 1,
625 PAC7302_EXPOSURE_DEFAULT);
626 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
627 V4L2_CID_GAIN, 0, 62, 1,
628 PAC7302_GAIN_DEFAULT);
629
630 sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
631 V4L2_CID_HFLIP, 0, 1, 1, 0);
632 sd->vflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
633 V4L2_CID_VFLIP, 0, 1, 1, 0);
634
635 if (hdl->error) {
636 pr_err("Could not initialize controls\n");
637 return hdl->error;
638 }
639
640 v4l2_ctrl_cluster(2, &sd->brightness);
641 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
642 v4l2_ctrl_cluster(2, &sd->hflip);
643 return 0;
644}
645
646/* -- start the camera -- */
720static int sd_start(struct gspca_dev *gspca_dev) 647static int sd_start(struct gspca_dev *gspca_dev)
721{ 648{
722 struct sd *sd = (struct sd *) gspca_dev; 649 struct sd *sd = (struct sd *) gspca_dev;
@@ -728,11 +655,13 @@ static int sd_start(struct gspca_dev *gspca_dev)
728 setwhitebalance(gspca_dev); 655 setwhitebalance(gspca_dev);
729 setredbalance(gspca_dev); 656 setredbalance(gspca_dev);
730 setbluebalance(gspca_dev); 657 setbluebalance(gspca_dev);
731 setautogain(gspca_dev); 658 setexposure(gspca_dev);
659 setgain(gspca_dev);
732 sethvflip(gspca_dev); 660 sethvflip(gspca_dev);
733 661
734 sd->sof_read = 0; 662 sd->sof_read = 0;
735 atomic_set(&sd->avg_lum, 270 + sd->ctrls[BRIGHTNESS].val); 663 sd->autogain_ignore_frames = 0;
664 atomic_set(&sd->avg_lum, 270 + sd->brightness->val);
736 665
737 /* start stream */ 666 /* start stream */
738 reg_w(gspca_dev, 0xff, 0x01); 667 reg_w(gspca_dev, 0xff, 0x01);
@@ -758,9 +687,6 @@ static void sd_stop0(struct gspca_dev *gspca_dev)
758 reg_w(gspca_dev, 0x78, 0x40); 687 reg_w(gspca_dev, 0x78, 0x40);
759} 688}
760 689
761#define WANT_REGULAR_AUTOGAIN
762#include "autogain_functions.h"
763
764static void do_autogain(struct gspca_dev *gspca_dev) 690static void do_autogain(struct gspca_dev *gspca_dev)
765{ 691{
766 struct sd *sd = (struct sd *) gspca_dev; 692 struct sd *sd = (struct sd *) gspca_dev;
@@ -774,11 +700,13 @@ static void do_autogain(struct gspca_dev *gspca_dev)
774 if (sd->autogain_ignore_frames > 0) { 700 if (sd->autogain_ignore_frames > 0) {
775 sd->autogain_ignore_frames--; 701 sd->autogain_ignore_frames--;
776 } else { 702 } else {
777 desired_lum = 270 + sd->ctrls[BRIGHTNESS].val; 703 desired_lum = 270 + sd->brightness->val;
778 704
779 auto_gain_n_exposure(gspca_dev, avg_lum, desired_lum, 705 if (gspca_expo_autogain(gspca_dev, avg_lum, desired_lum,
780 deadzone, GAIN_KNEE, EXPOSURE_KNEE); 706 deadzone, PAC7302_GAIN_KNEE,
781 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES; 707 PAC7302_EXPOSURE_KNEE))
708 sd->autogain_ignore_frames =
709 PAC_AUTOGAIN_IGNORE_FRAMES;
782 } 710 }
783} 711}
784 712
@@ -944,10 +872,9 @@ static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
944/* sub-driver description for pac7302 */ 872/* sub-driver description for pac7302 */
945static const struct sd_desc sd_desc = { 873static const struct sd_desc sd_desc = {
946 .name = KBUILD_MODNAME, 874 .name = KBUILD_MODNAME,
947 .ctrls = sd_ctrls,
948 .nctrls = ARRAY_SIZE(sd_ctrls),
949 .config = sd_config, 875 .config = sd_config,
950 .init = sd_init, 876 .init = sd_init,
877 .init_controls = sd_init_controls,
951 .start = sd_start, 878 .start = sd_start,
952 .stopN = sd_stopN, 879 .stopN = sd_stopN,
953 .stop0 = sd_stop0, 880 .stop0 = sd_stop0,
@@ -998,6 +925,7 @@ static struct usb_driver sd_driver = {
998#ifdef CONFIG_PM 925#ifdef CONFIG_PM
999 .suspend = gspca_suspend, 926 .suspend = gspca_suspend,
1000 .resume = gspca_resume, 927 .resume = gspca_resume,
928 .reset_resume = gspca_resume,
1001#endif 929#endif
1002}; 930};
1003 931
diff --git a/drivers/media/video/gspca/pac7311.c b/drivers/media/video/gspca/pac7311.c
index 115da169f32a..ba3558d3f017 100644
--- a/drivers/media/video/gspca/pac7311.c
+++ b/drivers/media/video/gspca/pac7311.c
@@ -694,6 +694,7 @@ static struct usb_driver sd_driver = {
694#ifdef CONFIG_PM 694#ifdef CONFIG_PM
695 .suspend = gspca_suspend, 695 .suspend = gspca_suspend,
696 .resume = gspca_resume, 696 .resume = gspca_resume,
697 .reset_resume = gspca_resume,
697#endif 698#endif
698}; 699};
699 700
diff --git a/drivers/media/video/gspca/se401.c b/drivers/media/video/gspca/se401.c
index bb70092c2229..a33cb78a839c 100644
--- a/drivers/media/video/gspca/se401.c
+++ b/drivers/media/video/gspca/se401.c
@@ -45,15 +45,6 @@ MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
45MODULE_DESCRIPTION("Endpoints se401"); 45MODULE_DESCRIPTION("Endpoints se401");
46MODULE_LICENSE("GPL"); 46MODULE_LICENSE("GPL");
47 47
48/* controls */
49enum e_ctrl {
50 BRIGHTNESS,
51 GAIN,
52 EXPOSURE,
53 FREQ,
54 NCTRL /* number of controls */
55};
56
57/* exposure change state machine states */ 48/* exposure change state machine states */
58enum { 49enum {
59 EXPO_CHANGED, 50 EXPO_CHANGED,
@@ -64,7 +55,11 @@ enum {
64/* specific webcam descriptor */ 55/* specific webcam descriptor */
65struct sd { 56struct sd {
66 struct gspca_dev gspca_dev; /* !! must be the first item */ 57 struct gspca_dev gspca_dev; /* !! must be the first item */
67 struct gspca_ctrl ctrls[NCTRL]; 58 struct { /* exposure/freq control cluster */
59 struct v4l2_ctrl *exposure;
60 struct v4l2_ctrl *freq;
61 };
62 bool has_brightness;
68 struct v4l2_pix_format fmts[MAX_MODES]; 63 struct v4l2_pix_format fmts[MAX_MODES];
69 int pixels_read; 64 int pixels_read;
70 int packet_read; 65 int packet_read;
@@ -77,60 +72,6 @@ struct sd {
77 int expo_change_state; 72 int expo_change_state;
78}; 73};
79 74
80static void setbrightness(struct gspca_dev *gspca_dev);
81static void setgain(struct gspca_dev *gspca_dev);
82static void setexposure(struct gspca_dev *gspca_dev);
83
84static const struct ctrl sd_ctrls[NCTRL] = {
85[BRIGHTNESS] = {
86 {
87 .id = V4L2_CID_BRIGHTNESS,
88 .type = V4L2_CTRL_TYPE_INTEGER,
89 .name = "Brightness",
90 .minimum = 0,
91 .maximum = 255,
92 .step = 1,
93 .default_value = 15,
94 },
95 .set_control = setbrightness
96 },
97[GAIN] = {
98 {
99 .id = V4L2_CID_GAIN,
100 .type = V4L2_CTRL_TYPE_INTEGER,
101 .name = "Gain",
102 .minimum = 0,
103 .maximum = 50, /* Really 63 but > 50 is not pretty */
104 .step = 1,
105 .default_value = 25,
106 },
107 .set_control = setgain
108 },
109[EXPOSURE] = {
110 {
111 .id = V4L2_CID_EXPOSURE,
112 .type = V4L2_CTRL_TYPE_INTEGER,
113 .name = "Exposure",
114 .minimum = 0,
115 .maximum = 32767,
116 .step = 1,
117 .default_value = 15000,
118 },
119 .set_control = setexposure
120 },
121[FREQ] = {
122 {
123 .id = V4L2_CID_POWER_LINE_FREQUENCY,
124 .type = V4L2_CTRL_TYPE_MENU,
125 .name = "Light frequency filter",
126 .minimum = 0,
127 .maximum = 2,
128 .step = 1,
129 .default_value = 0,
130 },
131 .set_control = setexposure
132 },
133};
134 75
135static void se401_write_req(struct gspca_dev *gspca_dev, u16 req, u16 value, 76static void se401_write_req(struct gspca_dev *gspca_dev, u16 req, u16 value,
136 int silent) 77 int silent)
@@ -224,22 +165,15 @@ static int se401_get_feature(struct gspca_dev *gspca_dev, u16 selector)
224 return gspca_dev->usb_buf[0] | (gspca_dev->usb_buf[1] << 8); 165 return gspca_dev->usb_buf[0] | (gspca_dev->usb_buf[1] << 8);
225} 166}
226 167
227static void setbrightness(struct gspca_dev *gspca_dev) 168static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
228{ 169{
229 struct sd *sd = (struct sd *) gspca_dev;
230
231 if (gspca_dev->ctrl_dis & (1 << BRIGHTNESS))
232 return;
233
234 /* HDG: this does not seem to do anything on my cam */ 170 /* HDG: this does not seem to do anything on my cam */
235 se401_write_req(gspca_dev, SE401_REQ_SET_BRT, 171 se401_write_req(gspca_dev, SE401_REQ_SET_BRT, val, 0);
236 sd->ctrls[BRIGHTNESS].val, 0);
237} 172}
238 173
239static void setgain(struct gspca_dev *gspca_dev) 174static void setgain(struct gspca_dev *gspca_dev, s32 val)
240{ 175{
241 struct sd *sd = (struct sd *) gspca_dev; 176 u16 gain = 63 - val;
242 u16 gain = 63 - sd->ctrls[GAIN].val;
243 177
244 /* red color gain */ 178 /* red color gain */
245 se401_set_feature(gspca_dev, HV7131_REG_ARCG, gain); 179 se401_set_feature(gspca_dev, HV7131_REG_ARCG, gain);
@@ -249,10 +183,10 @@ static void setgain(struct gspca_dev *gspca_dev)
249 se401_set_feature(gspca_dev, HV7131_REG_ABCG, gain); 183 se401_set_feature(gspca_dev, HV7131_REG_ABCG, gain);
250} 184}
251 185
252static void setexposure(struct gspca_dev *gspca_dev) 186static void setexposure(struct gspca_dev *gspca_dev, s32 val, s32 freq)
253{ 187{
254 struct sd *sd = (struct sd *) gspca_dev; 188 struct sd *sd = (struct sd *) gspca_dev;
255 int integration = sd->ctrls[EXPOSURE].val << 6; 189 int integration = val << 6;
256 u8 expose_h, expose_m, expose_l; 190 u8 expose_h, expose_m, expose_l;
257 191
258 /* Do this before the set_feature calls, for proper timing wrt 192 /* Do this before the set_feature calls, for proper timing wrt
@@ -262,9 +196,9 @@ static void setexposure(struct gspca_dev *gspca_dev)
262 through so be it */ 196 through so be it */
263 sd->expo_change_state = EXPO_CHANGED; 197 sd->expo_change_state = EXPO_CHANGED;
264 198
265 if (sd->ctrls[FREQ].val == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) 199 if (freq == V4L2_CID_POWER_LINE_FREQUENCY_50HZ)
266 integration = integration - integration % 106667; 200 integration = integration - integration % 106667;
267 if (sd->ctrls[FREQ].val == V4L2_CID_POWER_LINE_FREQUENCY_60HZ) 201 if (freq == V4L2_CID_POWER_LINE_FREQUENCY_60HZ)
268 integration = integration - integration % 88889; 202 integration = integration - integration % 88889;
269 203
270 expose_h = (integration >> 16); 204 expose_h = (integration >> 16);
@@ -375,15 +309,12 @@ static int sd_config(struct gspca_dev *gspca_dev,
375 cam->bulk = 1; 309 cam->bulk = 1;
376 cam->bulk_size = BULK_SIZE; 310 cam->bulk_size = BULK_SIZE;
377 cam->bulk_nurbs = 4; 311 cam->bulk_nurbs = 4;
378 cam->ctrls = sd->ctrls;
379 sd->resetlevel = 0x2d; /* Set initial resetlevel */ 312 sd->resetlevel = 0x2d; /* Set initial resetlevel */
380 313
381 /* See if the camera supports brightness */ 314 /* See if the camera supports brightness */
382 se401_read_req(gspca_dev, SE401_REQ_GET_BRT, 1); 315 se401_read_req(gspca_dev, SE401_REQ_GET_BRT, 1);
383 if (gspca_dev->usb_err) { 316 sd->has_brightness = !!gspca_dev->usb_err;
384 gspca_dev->ctrl_dis = (1 << BRIGHTNESS); 317 gspca_dev->usb_err = 0;
385 gspca_dev->usb_err = 0;
386 }
387 318
388 return 0; 319 return 0;
389} 320}
@@ -442,9 +373,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
442 } 373 }
443 se401_set_feature(gspca_dev, SE401_OPERATINGMODE, mode); 374 se401_set_feature(gspca_dev, SE401_OPERATINGMODE, mode);
444 375
445 setbrightness(gspca_dev);
446 setgain(gspca_dev);
447 setexposure(gspca_dev);
448 se401_set_feature(gspca_dev, HV7131_REG_ARLV, sd->resetlevel); 376 se401_set_feature(gspca_dev, HV7131_REG_ARLV, sd->resetlevel);
449 377
450 sd->packet_read = 0; 378 sd->packet_read = 0;
@@ -666,27 +594,6 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *data, int len)
666 sd_pkt_scan_janggu(gspca_dev, data, len); 594 sd_pkt_scan_janggu(gspca_dev, data, len);
667} 595}
668 596
669static int sd_querymenu(struct gspca_dev *gspca_dev,
670 struct v4l2_querymenu *menu)
671{
672 switch (menu->id) {
673 case V4L2_CID_POWER_LINE_FREQUENCY:
674 switch (menu->index) {
675 case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
676 strcpy((char *) menu->name, "NoFliker");
677 return 0;
678 case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
679 strcpy((char *) menu->name, "50 Hz");
680 return 0;
681 case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
682 strcpy((char *) menu->name, "60 Hz");
683 return 0;
684 }
685 break;
686 }
687 return -EINVAL;
688}
689
690#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) 597#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
691static int sd_int_pkt_scan(struct gspca_dev *gspca_dev, u8 *data, int len) 598static int sd_int_pkt_scan(struct gspca_dev *gspca_dev, u8 *data, int len)
692{ 599{
@@ -714,19 +621,73 @@ static int sd_int_pkt_scan(struct gspca_dev *gspca_dev, u8 *data, int len)
714} 621}
715#endif 622#endif
716 623
624static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
625{
626 struct gspca_dev *gspca_dev =
627 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
628 struct sd *sd = (struct sd *)gspca_dev;
629
630 gspca_dev->usb_err = 0;
631
632 if (!gspca_dev->streaming)
633 return 0;
634
635 switch (ctrl->id) {
636 case V4L2_CID_BRIGHTNESS:
637 setbrightness(gspca_dev, ctrl->val);
638 break;
639 case V4L2_CID_GAIN:
640 setgain(gspca_dev, ctrl->val);
641 break;
642 case V4L2_CID_EXPOSURE:
643 setexposure(gspca_dev, ctrl->val, sd->freq->val);
644 break;
645 }
646 return gspca_dev->usb_err;
647}
648
649static const struct v4l2_ctrl_ops sd_ctrl_ops = {
650 .s_ctrl = sd_s_ctrl,
651};
652
653static int sd_init_controls(struct gspca_dev *gspca_dev)
654{
655 struct sd *sd = (struct sd *)gspca_dev;
656 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
657
658 gspca_dev->vdev.ctrl_handler = hdl;
659 v4l2_ctrl_handler_init(hdl, 4);
660 if (sd->has_brightness)
661 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
662 V4L2_CID_BRIGHTNESS, 0, 255, 1, 15);
663 /* max is really 63 but > 50 is not pretty */
664 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
665 V4L2_CID_GAIN, 0, 50, 1, 25);
666 sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
667 V4L2_CID_EXPOSURE, 0, 32767, 1, 15000);
668 sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
669 V4L2_CID_POWER_LINE_FREQUENCY,
670 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
671
672 if (hdl->error) {
673 pr_err("Could not initialize controls\n");
674 return hdl->error;
675 }
676 v4l2_ctrl_cluster(2, &sd->exposure);
677 return 0;
678}
679
717/* sub-driver description */ 680/* sub-driver description */
718static const struct sd_desc sd_desc = { 681static const struct sd_desc sd_desc = {
719 .name = MODULE_NAME, 682 .name = MODULE_NAME,
720 .ctrls = sd_ctrls,
721 .nctrls = ARRAY_SIZE(sd_ctrls),
722 .config = sd_config, 683 .config = sd_config,
723 .init = sd_init, 684 .init = sd_init,
685 .init_controls = sd_init_controls,
724 .isoc_init = sd_isoc_init, 686 .isoc_init = sd_isoc_init,
725 .start = sd_start, 687 .start = sd_start,
726 .stopN = sd_stopN, 688 .stopN = sd_stopN,
727 .dq_callback = sd_dq_callback, 689 .dq_callback = sd_dq_callback,
728 .pkt_scan = sd_pkt_scan, 690 .pkt_scan = sd_pkt_scan,
729 .querymenu = sd_querymenu,
730#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) 691#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
731 .int_pkt_scan = sd_int_pkt_scan, 692 .int_pkt_scan = sd_int_pkt_scan,
732#endif 693#endif
@@ -769,6 +730,7 @@ static struct usb_driver sd_driver = {
769#ifdef CONFIG_PM 730#ifdef CONFIG_PM
770 .suspend = gspca_suspend, 731 .suspend = gspca_suspend,
771 .resume = gspca_resume, 732 .resume = gspca_resume,
733 .reset_resume = gspca_resume,
772#endif 734#endif
773 .pre_reset = sd_pre_reset, 735 .pre_reset = sd_pre_reset,
774 .post_reset = sd_post_reset, 736 .post_reset = sd_post_reset,
diff --git a/drivers/media/video/gspca/sn9c2028.c b/drivers/media/video/gspca/sn9c2028.c
index 478533cb1152..03fa3fd940b4 100644
--- a/drivers/media/video/gspca/sn9c2028.c
+++ b/drivers/media/video/gspca/sn9c2028.c
@@ -40,10 +40,6 @@ struct init_command {
40 unsigned char to_read; /* length to read. 0 means no reply requested */ 40 unsigned char to_read; /* length to read. 0 means no reply requested */
41}; 41};
42 42
43/* V4L2 controls supported by the driver */
44static const struct ctrl sd_ctrls[] = {
45};
46
47/* How to change the resolution of any of the VGA cams is unknown */ 43/* How to change the resolution of any of the VGA cams is unknown */
48static const struct v4l2_pix_format vga_mode[] = { 44static const struct v4l2_pix_format vga_mode[] = {
49 {640, 480, V4L2_PIX_FMT_SN9C2028, V4L2_FIELD_NONE, 45 {640, 480, V4L2_PIX_FMT_SN9C2028, V4L2_FIELD_NONE,
@@ -695,8 +691,6 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
695/* sub-driver description */ 691/* sub-driver description */
696static const struct sd_desc sd_desc = { 692static const struct sd_desc sd_desc = {
697 .name = MODULE_NAME, 693 .name = MODULE_NAME,
698 .ctrls = sd_ctrls,
699 .nctrls = ARRAY_SIZE(sd_ctrls),
700 .config = sd_config, 694 .config = sd_config,
701 .init = sd_init, 695 .init = sd_init,
702 .start = sd_start, 696 .start = sd_start,
@@ -734,6 +728,7 @@ static struct usb_driver sd_driver = {
734#ifdef CONFIG_PM 728#ifdef CONFIG_PM
735 .suspend = gspca_suspend, 729 .suspend = gspca_suspend,
736 .resume = gspca_resume, 730 .resume = gspca_resume,
731 .reset_resume = gspca_resume,
737#endif 732#endif
738}; 733};
739 734
diff --git a/drivers/media/video/gspca/sonixb.c b/drivers/media/video/gspca/sonixb.c
index e2bdf8f632f4..fd1f8d2d3b0b 100644
--- a/drivers/media/video/gspca/sonixb.c
+++ b/drivers/media/video/gspca/sonixb.c
@@ -56,26 +56,16 @@ MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
56MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver"); 56MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");
57MODULE_LICENSE("GPL"); 57MODULE_LICENSE("GPL");
58 58
59/* controls */
60enum e_ctrl {
61 BRIGHTNESS,
62 GAIN,
63 EXPOSURE,
64 AUTOGAIN,
65 FREQ,
66 NCTRLS /* number of controls */
67};
68
69/* specific webcam descriptor */ 59/* specific webcam descriptor */
70struct sd { 60struct sd {
71 struct gspca_dev gspca_dev; /* !! must be the first item */ 61 struct gspca_dev gspca_dev; /* !! must be the first item */
72 62
73 struct gspca_ctrl ctrls[NCTRLS]; 63 struct v4l2_ctrl *brightness;
64 struct v4l2_ctrl *plfreq;
74 65
75 atomic_t avg_lum; 66 atomic_t avg_lum;
76 int prev_avg_lum; 67 int prev_avg_lum;
77 int exp_too_low_cnt; 68 int exposure_knee;
78 int exp_too_high_cnt;
79 int header_read; 69 int header_read;
80 u8 header[12]; /* Header without sof marker */ 70 u8 header[12]; /* Header without sof marker */
81 71
@@ -107,24 +97,16 @@ struct sensor_data {
107 sensor_init_t *sensor_init; 97 sensor_init_t *sensor_init;
108 int sensor_init_size; 98 int sensor_init_size;
109 int flags; 99 int flags;
110 unsigned ctrl_dis;
111 __u8 sensor_addr; 100 __u8 sensor_addr;
112}; 101};
113 102
114/* sensor_data flags */ 103/* sensor_data flags */
115#define F_GAIN 0x01 /* has gain */ 104#define F_SIF 0x01 /* sif or vga */
116#define F_SIF 0x02 /* sif or vga */
117#define F_COARSE_EXPO 0x04 /* exposure control is coarse */
118 105
119/* priv field of struct v4l2_pix_format flags (do not use low nibble!) */ 106/* priv field of struct v4l2_pix_format flags (do not use low nibble!) */
120#define MODE_RAW 0x10 /* raw bayer mode */ 107#define MODE_RAW 0x10 /* raw bayer mode */
121#define MODE_REDUCED_SIF 0x20 /* vga mode (320x240 / 160x120) on sif cam */ 108#define MODE_REDUCED_SIF 0x20 /* vga mode (320x240 / 160x120) on sif cam */
122 109
123/* ctrl_dis helper macros */
124#define NO_EXPO ((1 << EXPOSURE) | (1 << AUTOGAIN))
125#define NO_FREQ (1 << FREQ)
126#define NO_BRIGHTNESS (1 << BRIGHTNESS)
127
128#define COMP 0xc7 /* 0x87 //0x07 */ 110#define COMP 0xc7 /* 0x87 //0x07 */
129#define COMP1 0xc9 /* 0x89 //0x09 */ 111#define COMP1 0xc9 /* 0x89 //0x09 */
130 112
@@ -133,12 +115,12 @@ struct sensor_data {
133 115
134#define SYS_CLK 0x04 116#define SYS_CLK 0x04
135 117
136#define SENS(bridge, sensor, _flags, _ctrl_dis, _sensor_addr) \ 118#define SENS(bridge, sensor, _flags, _sensor_addr) \
137{ \ 119{ \
138 .bridge_init = bridge, \ 120 .bridge_init = bridge, \
139 .sensor_init = sensor, \ 121 .sensor_init = sensor, \
140 .sensor_init_size = sizeof(sensor), \ 122 .sensor_init_size = sizeof(sensor), \
141 .flags = _flags, .ctrl_dis = _ctrl_dis, .sensor_addr = _sensor_addr \ 123 .flags = _flags, .sensor_addr = _sensor_addr \
142} 124}
143 125
144/* We calculate the autogain at the end of the transfer of a frame, at this 126/* We calculate the autogain at the end of the transfer of a frame, at this
@@ -147,87 +129,6 @@ struct sensor_data {
147 the new settings to come into effect before doing any other adjustments. */ 129 the new settings to come into effect before doing any other adjustments. */
148#define AUTOGAIN_IGNORE_FRAMES 1 130#define AUTOGAIN_IGNORE_FRAMES 1
149 131
150/* V4L2 controls supported by the driver */
151static void setbrightness(struct gspca_dev *gspca_dev);
152static void setgain(struct gspca_dev *gspca_dev);
153static void setexposure(struct gspca_dev *gspca_dev);
154static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
155static void setfreq(struct gspca_dev *gspca_dev);
156
157static const struct ctrl sd_ctrls[NCTRLS] = {
158[BRIGHTNESS] = {
159 {
160 .id = V4L2_CID_BRIGHTNESS,
161 .type = V4L2_CTRL_TYPE_INTEGER,
162 .name = "Brightness",
163 .minimum = 0,
164 .maximum = 255,
165 .step = 1,
166 .default_value = 127,
167 },
168 .set_control = setbrightness
169 },
170[GAIN] = {
171 {
172 .id = V4L2_CID_GAIN,
173 .type = V4L2_CTRL_TYPE_INTEGER,
174 .name = "Gain",
175 .minimum = 0,
176 .maximum = 255,
177 .step = 1,
178#define GAIN_KNEE 230
179 .default_value = 127,
180 },
181 .set_control = setgain
182 },
183[EXPOSURE] = {
184 {
185 .id = V4L2_CID_EXPOSURE,
186 .type = V4L2_CTRL_TYPE_INTEGER,
187 .name = "Exposure",
188 .minimum = 0,
189 .maximum = 1023,
190 .step = 1,
191 .default_value = 66,
192 /* 33 ms / 30 fps (except on PASXXX) */
193#define EXPOSURE_KNEE 200 /* 100 ms / 10 fps (except on PASXXX) */
194 .flags = 0,
195 },
196 .set_control = setexposure
197 },
198/* for coarse exposure */
199#define COARSE_EXPOSURE_MIN 2
200#define COARSE_EXPOSURE_MAX 15
201#define COARSE_EXPOSURE_DEF 2 /* 30 fps */
202[AUTOGAIN] = {
203 {
204 .id = V4L2_CID_AUTOGAIN,
205 .type = V4L2_CTRL_TYPE_BOOLEAN,
206 .name = "Automatic Gain (and Exposure)",
207 .minimum = 0,
208 .maximum = 1,
209 .step = 1,
210#define AUTOGAIN_DEF 1
211 .default_value = AUTOGAIN_DEF,
212 .flags = V4L2_CTRL_FLAG_UPDATE
213 },
214 .set = sd_setautogain,
215 },
216[FREQ] = {
217 {
218 .id = V4L2_CID_POWER_LINE_FREQUENCY,
219 .type = V4L2_CTRL_TYPE_MENU,
220 .name = "Light frequency filter",
221 .minimum = 0,
222 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
223 .step = 1,
224#define FREQ_DEF 0
225 .default_value = FREQ_DEF,
226 },
227 .set_control = setfreq
228 },
229};
230
231static const struct v4l2_pix_format vga_mode[] = { 132static const struct v4l2_pix_format vga_mode[] = {
232 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, 133 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
233 .bytesperline = 160, 134 .bytesperline = 160,
@@ -532,25 +433,27 @@ static const __u8 tas5130_sensor_init[][8] = {
532}; 433};
533 434
534static const struct sensor_data sensor_data[] = { 435static const struct sensor_data sensor_data[] = {
535SENS(initHv7131d, hv7131d_sensor_init, F_GAIN, NO_BRIGHTNESS|NO_FREQ, 0), 436 SENS(initHv7131d, hv7131d_sensor_init, 0, 0),
536SENS(initHv7131r, hv7131r_sensor_init, 0, NO_BRIGHTNESS|NO_EXPO|NO_FREQ, 0), 437 SENS(initHv7131r, hv7131r_sensor_init, 0, 0),
537SENS(initOv6650, ov6650_sensor_init, F_GAIN|F_SIF, 0, 0x60), 438 SENS(initOv6650, ov6650_sensor_init, F_SIF, 0x60),
538SENS(initOv7630, ov7630_sensor_init, F_GAIN, 0, 0x21), 439 SENS(initOv7630, ov7630_sensor_init, 0, 0x21),
539SENS(initPas106, pas106_sensor_init, F_GAIN|F_SIF, NO_FREQ, 0), 440 SENS(initPas106, pas106_sensor_init, F_SIF, 0),
540SENS(initPas202, pas202_sensor_init, F_GAIN, NO_FREQ, 0), 441 SENS(initPas202, pas202_sensor_init, 0, 0),
541SENS(initTas5110c, tas5110c_sensor_init, F_GAIN|F_SIF|F_COARSE_EXPO, 442 SENS(initTas5110c, tas5110c_sensor_init, F_SIF, 0),
542 NO_BRIGHTNESS|NO_FREQ, 0), 443 SENS(initTas5110d, tas5110d_sensor_init, F_SIF, 0),
543SENS(initTas5110d, tas5110d_sensor_init, F_GAIN|F_SIF|F_COARSE_EXPO, 444 SENS(initTas5130, tas5130_sensor_init, 0, 0),
544 NO_BRIGHTNESS|NO_FREQ, 0),
545SENS(initTas5130, tas5130_sensor_init, F_GAIN,
546 NO_BRIGHTNESS|NO_EXPO|NO_FREQ, 0),
547}; 445};
548 446
549/* get one byte in gspca_dev->usb_buf */ 447/* get one byte in gspca_dev->usb_buf */
550static void reg_r(struct gspca_dev *gspca_dev, 448static void reg_r(struct gspca_dev *gspca_dev,
551 __u16 value) 449 __u16 value)
552{ 450{
553 usb_control_msg(gspca_dev->dev, 451 int res;
452
453 if (gspca_dev->usb_err < 0)
454 return;
455
456 res = usb_control_msg(gspca_dev->dev,
554 usb_rcvctrlpipe(gspca_dev->dev, 0), 457 usb_rcvctrlpipe(gspca_dev->dev, 0),
555 0, /* request */ 458 0, /* request */
556 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 459 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
@@ -558,6 +461,12 @@ static void reg_r(struct gspca_dev *gspca_dev,
558 0, /* index */ 461 0, /* index */
559 gspca_dev->usb_buf, 1, 462 gspca_dev->usb_buf, 1,
560 500); 463 500);
464
465 if (res < 0) {
466 dev_err(gspca_dev->v4l2_dev.dev,
467 "Error reading register %02x: %d\n", value, res);
468 gspca_dev->usb_err = res;
469 }
561} 470}
562 471
563static void reg_w(struct gspca_dev *gspca_dev, 472static void reg_w(struct gspca_dev *gspca_dev,
@@ -565,14 +474,13 @@ static void reg_w(struct gspca_dev *gspca_dev,
565 const __u8 *buffer, 474 const __u8 *buffer,
566 int len) 475 int len)
567{ 476{
568#ifdef GSPCA_DEBUG 477 int res;
569 if (len > USB_BUF_SZ) { 478
570 PDEBUG(D_ERR|D_PACK, "reg_w: buffer overflow"); 479 if (gspca_dev->usb_err < 0)
571 return; 480 return;
572 } 481
573#endif
574 memcpy(gspca_dev->usb_buf, buffer, len); 482 memcpy(gspca_dev->usb_buf, buffer, len);
575 usb_control_msg(gspca_dev->dev, 483 res = usb_control_msg(gspca_dev->dev,
576 usb_sndctrlpipe(gspca_dev->dev, 0), 484 usb_sndctrlpipe(gspca_dev->dev, 0),
577 0x08, /* request */ 485 0x08, /* request */
578 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 486 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
@@ -580,30 +488,48 @@ static void reg_w(struct gspca_dev *gspca_dev,
580 0, /* index */ 488 0, /* index */
581 gspca_dev->usb_buf, len, 489 gspca_dev->usb_buf, len,
582 500); 490 500);
491
492 if (res < 0) {
493 dev_err(gspca_dev->v4l2_dev.dev,
494 "Error writing register %02x: %d\n", value, res);
495 gspca_dev->usb_err = res;
496 }
583} 497}
584 498
585static int i2c_w(struct gspca_dev *gspca_dev, const __u8 *buffer) 499static void i2c_w(struct gspca_dev *gspca_dev, const __u8 *buffer)
586{ 500{
587 int retry = 60; 501 int retry = 60;
588 502
503 if (gspca_dev->usb_err < 0)
504 return;
505
589 /* is i2c ready */ 506 /* is i2c ready */
590 reg_w(gspca_dev, 0x08, buffer, 8); 507 reg_w(gspca_dev, 0x08, buffer, 8);
591 while (retry--) { 508 while (retry--) {
509 if (gspca_dev->usb_err < 0)
510 return;
592 msleep(10); 511 msleep(10);
593 reg_r(gspca_dev, 0x08); 512 reg_r(gspca_dev, 0x08);
594 if (gspca_dev->usb_buf[0] & 0x04) { 513 if (gspca_dev->usb_buf[0] & 0x04) {
595 if (gspca_dev->usb_buf[0] & 0x08) 514 if (gspca_dev->usb_buf[0] & 0x08) {
596 return -1; 515 dev_err(gspca_dev->v4l2_dev.dev,
597 return 0; 516 "i2c write error\n");
517 gspca_dev->usb_err = -EIO;
518 }
519 return;
598 } 520 }
599 } 521 }
600 return -1; 522
523 dev_err(gspca_dev->v4l2_dev.dev, "i2c write timeout\n");
524 gspca_dev->usb_err = -EIO;
601} 525}
602 526
603static void i2c_w_vector(struct gspca_dev *gspca_dev, 527static void i2c_w_vector(struct gspca_dev *gspca_dev,
604 const __u8 buffer[][8], int len) 528 const __u8 buffer[][8], int len)
605{ 529{
606 for (;;) { 530 for (;;) {
531 if (gspca_dev->usb_err < 0)
532 return;
607 reg_w(gspca_dev, 0x08, *buffer, 8); 533 reg_w(gspca_dev, 0x08, *buffer, 8);
608 len -= 8; 534 len -= 8;
609 if (len <= 0) 535 if (len <= 0)
@@ -624,11 +550,10 @@ static void setbrightness(struct gspca_dev *gspca_dev)
624 550
625 /* change reg 0x06 */ 551 /* change reg 0x06 */
626 i2cOV[1] = sensor_data[sd->sensor].sensor_addr; 552 i2cOV[1] = sensor_data[sd->sensor].sensor_addr;
627 i2cOV[3] = sd->ctrls[BRIGHTNESS].val; 553 i2cOV[3] = sd->brightness->val;
628 if (i2c_w(gspca_dev, i2cOV) < 0) 554 i2c_w(gspca_dev, i2cOV);
629 goto err;
630 break; 555 break;
631 } 556 }
632 case SENSOR_PAS106: 557 case SENSOR_PAS106:
633 case SENSOR_PAS202: { 558 case SENSOR_PAS202: {
634 __u8 i2cpbright[] = 559 __u8 i2cpbright[] =
@@ -642,54 +567,49 @@ static void setbrightness(struct gspca_dev *gspca_dev)
642 i2cpdoit[2] = 0x13; 567 i2cpdoit[2] = 0x13;
643 } 568 }
644 569
645 if (sd->ctrls[BRIGHTNESS].val < 127) { 570 if (sd->brightness->val < 127) {
646 /* change reg 0x0b, signreg */ 571 /* change reg 0x0b, signreg */
647 i2cpbright[3] = 0x01; 572 i2cpbright[3] = 0x01;
648 /* set reg 0x0c, offset */ 573 /* set reg 0x0c, offset */
649 i2cpbright[4] = 127 - sd->ctrls[BRIGHTNESS].val; 574 i2cpbright[4] = 127 - sd->brightness->val;
650 } else 575 } else
651 i2cpbright[4] = sd->ctrls[BRIGHTNESS].val - 127; 576 i2cpbright[4] = sd->brightness->val - 127;
652 577
653 if (i2c_w(gspca_dev, i2cpbright) < 0) 578 i2c_w(gspca_dev, i2cpbright);
654 goto err; 579 i2c_w(gspca_dev, i2cpdoit);
655 if (i2c_w(gspca_dev, i2cpdoit) < 0) 580 break;
656 goto err; 581 }
582 default:
657 break; 583 break;
658 }
659 } 584 }
660 return;
661err:
662 PDEBUG(D_ERR, "i2c error brightness");
663} 585}
664 586
665static void setsensorgain(struct gspca_dev *gspca_dev) 587static void setgain(struct gspca_dev *gspca_dev)
666{ 588{
667 struct sd *sd = (struct sd *) gspca_dev; 589 struct sd *sd = (struct sd *) gspca_dev;
668 u8 gain = sd->ctrls[GAIN].val; 590 u8 gain = gspca_dev->gain->val;
669 591
670 switch (sd->sensor) { 592 switch (sd->sensor) {
671 case SENSOR_HV7131D: { 593 case SENSOR_HV7131D: {
672 __u8 i2c[] = 594 __u8 i2c[] =
673 {0xc0, 0x11, 0x31, 0x00, 0x00, 0x00, 0x00, 0x17}; 595 {0xc0, 0x11, 0x31, 0x00, 0x00, 0x00, 0x00, 0x17};
674 596
675 i2c[3] = 0x3f - (gain / 4); 597 i2c[3] = 0x3f - gain;
676 i2c[4] = 0x3f - (gain / 4); 598 i2c[4] = 0x3f - gain;
677 i2c[5] = 0x3f - (gain / 4); 599 i2c[5] = 0x3f - gain;
678 600
679 if (i2c_w(gspca_dev, i2c) < 0) 601 i2c_w(gspca_dev, i2c);
680 goto err;
681 break; 602 break;
682 } 603 }
683 case SENSOR_TAS5110C: 604 case SENSOR_TAS5110C:
684 case SENSOR_TAS5130CXX: { 605 case SENSOR_TAS5130CXX: {
685 __u8 i2c[] = 606 __u8 i2c[] =
686 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10}; 607 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
687 608
688 i2c[4] = 255 - gain; 609 i2c[4] = 255 - gain;
689 if (i2c_w(gspca_dev, i2c) < 0) 610 i2c_w(gspca_dev, i2c);
690 goto err;
691 break; 611 break;
692 } 612 }
693 case SENSOR_TAS5110D: { 613 case SENSOR_TAS5110D: {
694 __u8 i2c[] = { 614 __u8 i2c[] = {
695 0xb0, 0x61, 0x02, 0x00, 0x10, 0x00, 0x00, 0x17 }; 615 0xb0, 0x61, 0x02, 0x00, 0x10, 0x00, 0x00, 0x17 };
@@ -703,23 +623,25 @@ static void setsensorgain(struct gspca_dev *gspca_dev)
703 i2c[3] |= (gain & 0x04) << 3; 623 i2c[3] |= (gain & 0x04) << 3;
704 i2c[3] |= (gain & 0x02) << 5; 624 i2c[3] |= (gain & 0x02) << 5;
705 i2c[3] |= (gain & 0x01) << 7; 625 i2c[3] |= (gain & 0x01) << 7;
706 if (i2c_w(gspca_dev, i2c) < 0) 626 i2c_w(gspca_dev, i2c);
707 goto err;
708 break; 627 break;
709 } 628 }
710
711 case SENSOR_OV6650: 629 case SENSOR_OV6650:
712 gain >>= 1;
713 /* fall thru */
714 case SENSOR_OV7630: { 630 case SENSOR_OV7630: {
715 __u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10}; 631 __u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
716 632
633 /*
634 * The ov7630's gain is weird, at 32 the gain drops to the
635 * same level as at 16, so skip 32-47 (of the 0-63 scale).
636 */
637 if (sd->sensor == SENSOR_OV7630 && gain >= 32)
638 gain += 16;
639
717 i2c[1] = sensor_data[sd->sensor].sensor_addr; 640 i2c[1] = sensor_data[sd->sensor].sensor_addr;
718 i2c[3] = gain >> 2; 641 i2c[3] = gain;
719 if (i2c_w(gspca_dev, i2c) < 0) 642 i2c_w(gspca_dev, i2c);
720 goto err;
721 break; 643 break;
722 } 644 }
723 case SENSOR_PAS106: 645 case SENSOR_PAS106:
724 case SENSOR_PAS202: { 646 case SENSOR_PAS202: {
725 __u8 i2cpgain[] = 647 __u8 i2cpgain[] =
@@ -737,49 +659,27 @@ static void setsensorgain(struct gspca_dev *gspca_dev)
737 i2cpdoit[2] = 0x13; 659 i2cpdoit[2] = 0x13;
738 } 660 }
739 661
740 i2cpgain[3] = gain >> 3; 662 i2cpgain[3] = gain;
741 i2cpcolorgain[3] = gain >> 4; 663 i2cpcolorgain[3] = gain >> 1;
742 i2cpcolorgain[4] = gain >> 4; 664 i2cpcolorgain[4] = gain >> 1;
743 i2cpcolorgain[5] = gain >> 4; 665 i2cpcolorgain[5] = gain >> 1;
744 i2cpcolorgain[6] = gain >> 4; 666 i2cpcolorgain[6] = gain >> 1;
745
746 if (i2c_w(gspca_dev, i2cpgain) < 0)
747 goto err;
748 if (i2c_w(gspca_dev, i2cpcolorgain) < 0)
749 goto err;
750 if (i2c_w(gspca_dev, i2cpdoit) < 0)
751 goto err;
752 break;
753 }
754 }
755 return;
756err:
757 PDEBUG(D_ERR, "i2c error gain");
758}
759
760static void setgain(struct gspca_dev *gspca_dev)
761{
762 struct sd *sd = (struct sd *) gspca_dev;
763 __u8 gain;
764 __u8 buf[3] = { 0, 0, 0 };
765 667
766 if (sensor_data[sd->sensor].flags & F_GAIN) { 668 i2c_w(gspca_dev, i2cpgain);
767 /* Use the sensor gain to do the actual gain */ 669 i2c_w(gspca_dev, i2cpcolorgain);
768 setsensorgain(gspca_dev); 670 i2c_w(gspca_dev, i2cpdoit);
769 return; 671 break;
770 } 672 }
771 673 default:
772 if (sd->bridge == BRIDGE_103) { 674 if (sd->bridge == BRIDGE_103) {
773 gain = sd->ctrls[GAIN].val >> 1; 675 u8 buf[3] = { gain, gain, gain }; /* R, G, B */
774 buf[0] = gain; /* Red */ 676 reg_w(gspca_dev, 0x05, buf, 3);
775 buf[1] = gain; /* Green */ 677 } else {
776 buf[2] = gain; /* Blue */ 678 u8 buf[2];
777 reg_w(gspca_dev, 0x05, buf, 3); 679 buf[0] = gain << 4 | gain; /* Red and blue */
778 } else { 680 buf[1] = gain; /* Green */
779 gain = sd->ctrls[GAIN].val >> 4; 681 reg_w(gspca_dev, 0x10, buf, 2);
780 buf[0] = gain << 4 | gain; /* Red and blue */ 682 }
781 buf[1] = gain; /* Green */
782 reg_w(gspca_dev, 0x10, buf, 2);
783 } 683 }
784} 684}
785 685
@@ -792,31 +692,24 @@ static void setexposure(struct gspca_dev *gspca_dev)
792 /* Note the datasheet wrongly says line mode exposure uses reg 692 /* Note the datasheet wrongly says line mode exposure uses reg
793 0x26 and 0x27, testing has shown 0x25 + 0x26 */ 693 0x26 and 0x27, testing has shown 0x25 + 0x26 */
794 __u8 i2c[] = {0xc0, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x17}; 694 __u8 i2c[] = {0xc0, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x17};
795 /* The HV7131D's exposure goes from 0 - 65535, we scale our 695 u16 reg = gspca_dev->exposure->val;
796 exposure of 0-1023 to 0-6138. There are 2 reasons for this:
797 1) This puts our exposure knee of 200 at approx the point
798 where the framerate starts dropping
799 2) At 6138 the framerate has already dropped to 2 fps,
800 going any lower makes little sense */
801 u16 reg = sd->ctrls[EXPOSURE].val * 6;
802 696
803 i2c[3] = reg >> 8; 697 i2c[3] = reg >> 8;
804 i2c[4] = reg & 0xff; 698 i2c[4] = reg & 0xff;
805 if (i2c_w(gspca_dev, i2c) != 0) 699 i2c_w(gspca_dev, i2c);
806 goto err;
807 break; 700 break;
808 } 701 }
809 case SENSOR_TAS5110C: 702 case SENSOR_TAS5110C:
810 case SENSOR_TAS5110D: { 703 case SENSOR_TAS5110D: {
811 /* register 19's high nibble contains the sn9c10x clock divider 704 /* register 19's high nibble contains the sn9c10x clock divider
812 The high nibble configures the no fps according to the 705 The high nibble configures the no fps according to the
813 formula: 60 / high_nibble. With a maximum of 30 fps */ 706 formula: 60 / high_nibble. With a maximum of 30 fps */
814 u8 reg = sd->ctrls[EXPOSURE].val; 707 u8 reg = gspca_dev->exposure->val;
815 708
816 reg = (reg << 4) | 0x0b; 709 reg = (reg << 4) | 0x0b;
817 reg_w(gspca_dev, 0x19, &reg, 1); 710 reg_w(gspca_dev, 0x19, &reg, 1);
818 break; 711 break;
819 } 712 }
820 case SENSOR_OV6650: 713 case SENSOR_OV6650:
821 case SENSOR_OV7630: { 714 case SENSOR_OV7630: {
822 /* The ov6650 / ov7630 have 2 registers which both influence 715 /* The ov6650 / ov7630 have 2 registers which both influence
@@ -848,7 +741,7 @@ static void setexposure(struct gspca_dev *gspca_dev)
848 } else 741 } else
849 reg10_max = 0x41; 742 reg10_max = 0x41;
850 743
851 reg11 = (15 * sd->ctrls[EXPOSURE].val + 999) / 1000; 744 reg11 = (15 * gspca_dev->exposure->val + 999) / 1000;
852 if (reg11 < 1) 745 if (reg11 < 1)
853 reg11 = 1; 746 reg11 = 1;
854 else if (reg11 > 16) 747 else if (reg11 > 16)
@@ -861,16 +754,16 @@ static void setexposure(struct gspca_dev *gspca_dev)
861 reg11 = 4; 754 reg11 = 4;
862 755
863 /* frame exposure time in ms = 1000 * reg11 / 30 -> 756 /* frame exposure time in ms = 1000 * reg11 / 30 ->
864 reg10 = (sd->ctrls[EXPOSURE].val / 2) * reg10_max 757 reg10 = (gspca_dev->exposure->val / 2) * reg10_max
865 / (1000 * reg11 / 30) */ 758 / (1000 * reg11 / 30) */
866 reg10 = (sd->ctrls[EXPOSURE].val * 15 * reg10_max) 759 reg10 = (gspca_dev->exposure->val * 15 * reg10_max)
867 / (1000 * reg11); 760 / (1000 * reg11);
868 761
869 /* Don't allow this to get below 10 when using autogain, the 762 /* Don't allow this to get below 10 when using autogain, the
870 steps become very large (relatively) when below 10 causing 763 steps become very large (relatively) when below 10 causing
871 the image to oscilate from much too dark, to much too bright 764 the image to oscilate from much too dark, to much too bright
872 and back again. */ 765 and back again. */
873 if (sd->ctrls[AUTOGAIN].val && reg10 < 10) 766 if (gspca_dev->autogain->val && reg10 < 10)
874 reg10 = 10; 767 reg10 = 10;
875 else if (reg10 > reg10_max) 768 else if (reg10 > reg10_max)
876 reg10 = reg10_max; 769 reg10 = reg10_max;
@@ -884,12 +777,11 @@ static void setexposure(struct gspca_dev *gspca_dev)
884 if (sd->reg11 == reg11) 777 if (sd->reg11 == reg11)
885 i2c[0] = 0xa0; 778 i2c[0] = 0xa0;
886 779
887 if (i2c_w(gspca_dev, i2c) == 0) 780 i2c_w(gspca_dev, i2c);
781 if (gspca_dev->usb_err == 0)
888 sd->reg11 = reg11; 782 sd->reg11 = reg11;
889 else
890 goto err;
891 break; 783 break;
892 } 784 }
893 case SENSOR_PAS202: { 785 case SENSOR_PAS202: {
894 __u8 i2cpframerate[] = 786 __u8 i2cpframerate[] =
895 {0xb0, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x16}; 787 {0xb0, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x16};
@@ -909,28 +801,25 @@ static void setexposure(struct gspca_dev *gspca_dev)
909 frame exposure times (like we are doing with the ov chips), 801 frame exposure times (like we are doing with the ov chips),
910 as that sometimes leads to jumps in the exposure control, 802 as that sometimes leads to jumps in the exposure control,
911 which are bad for auto exposure. */ 803 which are bad for auto exposure. */
912 if (sd->ctrls[EXPOSURE].val < 200) { 804 if (gspca_dev->exposure->val < 200) {
913 i2cpexpo[3] = 255 - (sd->ctrls[EXPOSURE].val * 255) 805 i2cpexpo[3] = 255 - (gspca_dev->exposure->val * 255)
914 / 200; 806 / 200;
915 framerate_ctrl = 500; 807 framerate_ctrl = 500;
916 } else { 808 } else {
917 /* The PAS202's exposure control goes from 0 - 4095, 809 /* The PAS202's exposure control goes from 0 - 4095,
918 but anything below 500 causes vsync issues, so scale 810 but anything below 500 causes vsync issues, so scale
919 our 200-1023 to 500-4095 */ 811 our 200-1023 to 500-4095 */
920 framerate_ctrl = (sd->ctrls[EXPOSURE].val - 200) 812 framerate_ctrl = (gspca_dev->exposure->val - 200)
921 * 1000 / 229 + 500; 813 * 1000 / 229 + 500;
922 } 814 }
923 815
924 i2cpframerate[3] = framerate_ctrl >> 6; 816 i2cpframerate[3] = framerate_ctrl >> 6;
925 i2cpframerate[4] = framerate_ctrl & 0x3f; 817 i2cpframerate[4] = framerate_ctrl & 0x3f;
926 if (i2c_w(gspca_dev, i2cpframerate) < 0) 818 i2c_w(gspca_dev, i2cpframerate);
927 goto err; 819 i2c_w(gspca_dev, i2cpexpo);
928 if (i2c_w(gspca_dev, i2cpexpo) < 0) 820 i2c_w(gspca_dev, i2cpdoit);
929 goto err;
930 if (i2c_w(gspca_dev, i2cpdoit) < 0)
931 goto err;
932 break; 821 break;
933 } 822 }
934 case SENSOR_PAS106: { 823 case SENSOR_PAS106: {
935 __u8 i2cpframerate[] = 824 __u8 i2cpframerate[] =
936 {0xb1, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x14}; 825 {0xb1, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x14};
@@ -942,46 +831,40 @@ static void setexposure(struct gspca_dev *gspca_dev)
942 831
943 /* For values below 150 use partial frame exposure, above 832 /* For values below 150 use partial frame exposure, above
944 that use framerate ctrl */ 833 that use framerate ctrl */
945 if (sd->ctrls[EXPOSURE].val < 150) { 834 if (gspca_dev->exposure->val < 150) {
946 i2cpexpo[3] = 150 - sd->ctrls[EXPOSURE].val; 835 i2cpexpo[3] = 150 - gspca_dev->exposure->val;
947 framerate_ctrl = 300; 836 framerate_ctrl = 300;
948 } else { 837 } else {
949 /* The PAS106's exposure control goes from 0 - 4095, 838 /* The PAS106's exposure control goes from 0 - 4095,
950 but anything below 300 causes vsync issues, so scale 839 but anything below 300 causes vsync issues, so scale
951 our 150-1023 to 300-4095 */ 840 our 150-1023 to 300-4095 */
952 framerate_ctrl = (sd->ctrls[EXPOSURE].val - 150) 841 framerate_ctrl = (gspca_dev->exposure->val - 150)
953 * 1000 / 230 + 300; 842 * 1000 / 230 + 300;
954 } 843 }
955 844
956 i2cpframerate[3] = framerate_ctrl >> 4; 845 i2cpframerate[3] = framerate_ctrl >> 4;
957 i2cpframerate[4] = framerate_ctrl & 0x0f; 846 i2cpframerate[4] = framerate_ctrl & 0x0f;
958 if (i2c_w(gspca_dev, i2cpframerate) < 0) 847 i2c_w(gspca_dev, i2cpframerate);
959 goto err; 848 i2c_w(gspca_dev, i2cpexpo);
960 if (i2c_w(gspca_dev, i2cpexpo) < 0) 849 i2c_w(gspca_dev, i2cpdoit);
961 goto err; 850 break;
962 if (i2c_w(gspca_dev, i2cpdoit) < 0) 851 }
963 goto err; 852 default:
964 break; 853 break;
965 }
966 } 854 }
967 return;
968err:
969 PDEBUG(D_ERR, "i2c error exposure");
970} 855}
971 856
972static void setfreq(struct gspca_dev *gspca_dev) 857static void setfreq(struct gspca_dev *gspca_dev)
973{ 858{
974 struct sd *sd = (struct sd *) gspca_dev; 859 struct sd *sd = (struct sd *) gspca_dev;
975 860
976 switch (sd->sensor) { 861 if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630) {
977 case SENSOR_OV6650:
978 case SENSOR_OV7630: {
979 /* Framerate adjust register for artificial light 50 hz flicker 862 /* Framerate adjust register for artificial light 50 hz flicker
980 compensation, for the ov6650 this is identical to ov6630 863 compensation, for the ov6650 this is identical to ov6630
981 0x2b register, see ov6630 datasheet. 864 0x2b register, see ov6630 datasheet.
982 0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */ 865 0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */
983 __u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10}; 866 __u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};
984 switch (sd->ctrls[FREQ].val) { 867 switch (sd->plfreq->val) {
985 default: 868 default:
986/* case 0: * no filter*/ 869/* case 0: * no filter*/
987/* case 2: * 60 hz */ 870/* case 2: * 60 hz */
@@ -993,25 +876,17 @@ static void setfreq(struct gspca_dev *gspca_dev)
993 break; 876 break;
994 } 877 }
995 i2c[1] = sensor_data[sd->sensor].sensor_addr; 878 i2c[1] = sensor_data[sd->sensor].sensor_addr;
996 if (i2c_w(gspca_dev, i2c) < 0) 879 i2c_w(gspca_dev, i2c);
997 PDEBUG(D_ERR, "i2c error setfreq");
998 break;
999 }
1000 } 880 }
1001} 881}
1002 882
1003#define WANT_REGULAR_AUTOGAIN
1004#define WANT_COARSE_EXPO_AUTOGAIN
1005#include "autogain_functions.h"
1006
1007static void do_autogain(struct gspca_dev *gspca_dev) 883static void do_autogain(struct gspca_dev *gspca_dev)
1008{ 884{
1009 int deadzone, desired_avg_lum, result;
1010 struct sd *sd = (struct sd *) gspca_dev; 885 struct sd *sd = (struct sd *) gspca_dev;
1011 int avg_lum = atomic_read(&sd->avg_lum); 886 int deadzone, desired_avg_lum, avg_lum;
1012 887
1013 if ((gspca_dev->ctrl_dis & (1 << AUTOGAIN)) || 888 avg_lum = atomic_read(&sd->avg_lum);
1014 avg_lum == -1 || !sd->ctrls[AUTOGAIN].val) 889 if (avg_lum == -1)
1015 return; 890 return;
1016 891
1017 if (sd->autogain_ignore_frames > 0) { 892 if (sd->autogain_ignore_frames > 0) {
@@ -1030,22 +905,18 @@ static void do_autogain(struct gspca_dev *gspca_dev)
1030 desired_avg_lum = 13000; 905 desired_avg_lum = 13000;
1031 } 906 }
1032 907
1033 if (sensor_data[sd->sensor].flags & F_COARSE_EXPO) 908 if (sd->brightness)
1034 result = coarse_grained_expo_autogain(gspca_dev, avg_lum, 909 desired_avg_lum = sd->brightness->val * desired_avg_lum / 127;
1035 sd->ctrls[BRIGHTNESS].val 910
1036 * desired_avg_lum / 127, 911 if (gspca_dev->exposure->maximum < 500) {
1037 deadzone); 912 if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
1038 else 913 desired_avg_lum, deadzone))
1039 result = auto_gain_n_exposure(gspca_dev, avg_lum, 914 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
1040 sd->ctrls[BRIGHTNESS].val 915 } else {
1041 * desired_avg_lum / 127, 916 int gain_knee = gspca_dev->gain->maximum * 9 / 10;
1042 deadzone, GAIN_KNEE, EXPOSURE_KNEE); 917 if (gspca_expo_autogain(gspca_dev, avg_lum, desired_avg_lum,
1043 918 deadzone, gain_knee, sd->exposure_knee))
1044 if (result) { 919 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
1045 PDEBUG(D_FRAM, "autogain: gain changed: gain: %d expo: %d",
1046 (int) sd->ctrls[GAIN].val,
1047 (int) sd->ctrls[EXPOSURE].val);
1048 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
1049 } 920 }
1050} 921}
1051 922
@@ -1064,14 +935,7 @@ static int sd_config(struct gspca_dev *gspca_dev,
1064 sd->sensor = id->driver_info >> 8; 935 sd->sensor = id->driver_info >> 8;
1065 sd->bridge = id->driver_info & 0xff; 936 sd->bridge = id->driver_info & 0xff;
1066 937
1067 gspca_dev->ctrl_dis = sensor_data[sd->sensor].ctrl_dis;
1068#if AUTOGAIN_DEF
1069 if (!(gspca_dev->ctrl_dis & (1 << AUTOGAIN)))
1070 gspca_dev->ctrl_inac = (1 << GAIN) | (1 << EXPOSURE);
1071#endif
1072
1073 cam = &gspca_dev->cam; 938 cam = &gspca_dev->cam;
1074 cam->ctrls = sd->ctrls;
1075 if (!(sensor_data[sd->sensor].flags & F_SIF)) { 939 if (!(sensor_data[sd->sensor].flags & F_SIF)) {
1076 cam->cam_mode = vga_mode; 940 cam->cam_mode = vga_mode;
1077 cam->nmodes = ARRAY_SIZE(vga_mode); 941 cam->nmodes = ARRAY_SIZE(vga_mode);
@@ -1087,18 +951,143 @@ static int sd_config(struct gspca_dev *gspca_dev,
1087/* this function is called at probe and resume time */ 951/* this function is called at probe and resume time */
1088static int sd_init(struct gspca_dev *gspca_dev) 952static int sd_init(struct gspca_dev *gspca_dev)
1089{ 953{
1090 struct sd *sd = (struct sd *) gspca_dev;
1091 const __u8 stop = 0x09; /* Disable stream turn of LED */ 954 const __u8 stop = 0x09; /* Disable stream turn of LED */
1092 955
1093 if (sensor_data[sd->sensor].flags & F_COARSE_EXPO) { 956 reg_w(gspca_dev, 0x01, &stop, 1);
1094 sd->ctrls[EXPOSURE].min = COARSE_EXPOSURE_MIN; 957
1095 sd->ctrls[EXPOSURE].max = COARSE_EXPOSURE_MAX; 958 return gspca_dev->usb_err;
1096 sd->ctrls[EXPOSURE].def = COARSE_EXPOSURE_DEF; 959}
1097 if (sd->ctrls[EXPOSURE].val > COARSE_EXPOSURE_MAX) 960
1098 sd->ctrls[EXPOSURE].val = COARSE_EXPOSURE_DEF; 961static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
962{
963 struct gspca_dev *gspca_dev =
964 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
965 struct sd *sd = (struct sd *)gspca_dev;
966
967 gspca_dev->usb_err = 0;
968
969 if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
970 /* when switching to autogain set defaults to make sure
971 we are on a valid point of the autogain gain /
972 exposure knee graph, and give this change time to
973 take effect before doing autogain. */
974 gspca_dev->gain->val = gspca_dev->gain->default_value;
975 gspca_dev->exposure->val = gspca_dev->exposure->default_value;
976 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
1099 } 977 }
1100 978
1101 reg_w(gspca_dev, 0x01, &stop, 1); 979 if (!gspca_dev->streaming)
980 return 0;
981
982 switch (ctrl->id) {
983 case V4L2_CID_BRIGHTNESS:
984 setbrightness(gspca_dev);
985 break;
986 case V4L2_CID_AUTOGAIN:
987 if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
988 setexposure(gspca_dev);
989 if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
990 setgain(gspca_dev);
991 break;
992 case V4L2_CID_POWER_LINE_FREQUENCY:
993 setfreq(gspca_dev);
994 break;
995 default:
996 return -EINVAL;
997 }
998 return gspca_dev->usb_err;
999}
1000
1001static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1002 .s_ctrl = sd_s_ctrl,
1003};
1004
1005/* this function is called at probe time */
1006static int sd_init_controls(struct gspca_dev *gspca_dev)
1007{
1008 struct sd *sd = (struct sd *) gspca_dev;
1009 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1010
1011 gspca_dev->vdev.ctrl_handler = hdl;
1012 v4l2_ctrl_handler_init(hdl, 5);
1013
1014 if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630 ||
1015 sd->sensor == SENSOR_PAS106 || sd->sensor == SENSOR_PAS202)
1016 sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1017 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1018
1019 /* Gain range is sensor dependent */
1020 switch (sd->sensor) {
1021 case SENSOR_OV6650:
1022 case SENSOR_PAS106:
1023 case SENSOR_PAS202:
1024 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1025 V4L2_CID_GAIN, 0, 31, 1, 15);
1026 break;
1027 case SENSOR_OV7630:
1028 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1029 V4L2_CID_GAIN, 0, 47, 1, 31);
1030 break;
1031 case SENSOR_HV7131D:
1032 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1033 V4L2_CID_GAIN, 0, 63, 1, 31);
1034 break;
1035 case SENSOR_TAS5110C:
1036 case SENSOR_TAS5110D:
1037 case SENSOR_TAS5130CXX:
1038 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1039 V4L2_CID_GAIN, 0, 255, 1, 127);
1040 break;
1041 default:
1042 if (sd->bridge == BRIDGE_103) {
1043 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1044 V4L2_CID_GAIN, 0, 127, 1, 63);
1045 } else {
1046 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1047 V4L2_CID_GAIN, 0, 15, 1, 7);
1048 }
1049 }
1050
1051 /* Exposure range is sensor dependent, and not all have exposure */
1052 switch (sd->sensor) {
1053 case SENSOR_HV7131D:
1054 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1055 V4L2_CID_EXPOSURE, 0, 8191, 1, 482);
1056 sd->exposure_knee = 964;
1057 break;
1058 case SENSOR_OV6650:
1059 case SENSOR_OV7630:
1060 case SENSOR_PAS106:
1061 case SENSOR_PAS202:
1062 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1063 V4L2_CID_EXPOSURE, 0, 1023, 1, 66);
1064 sd->exposure_knee = 200;
1065 break;
1066 case SENSOR_TAS5110C:
1067 case SENSOR_TAS5110D:
1068 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1069 V4L2_CID_EXPOSURE, 2, 15, 1, 2);
1070 break;
1071 }
1072
1073 if (gspca_dev->exposure) {
1074 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1075 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1076 }
1077
1078 if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630)
1079 sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
1080 V4L2_CID_POWER_LINE_FREQUENCY,
1081 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0,
1082 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1083
1084 if (hdl->error) {
1085 pr_err("Could not initialize controls\n");
1086 return hdl->error;
1087 }
1088
1089 if (gspca_dev->autogain)
1090 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
1102 1091
1103 return 0; 1092 return 0;
1104} 1093}
@@ -1242,10 +1231,10 @@ static int sd_start(struct gspca_dev *gspca_dev)
1242 1231
1243 sd->frames_to_drop = 0; 1232 sd->frames_to_drop = 0;
1244 sd->autogain_ignore_frames = 0; 1233 sd->autogain_ignore_frames = 0;
1245 sd->exp_too_high_cnt = 0; 1234 gspca_dev->exp_too_high_cnt = 0;
1246 sd->exp_too_low_cnt = 0; 1235 gspca_dev->exp_too_low_cnt = 0;
1247 atomic_set(&sd->avg_lum, -1); 1236 atomic_set(&sd->avg_lum, -1);
1248 return 0; 1237 return gspca_dev->usb_err;
1249} 1238}
1250 1239
1251static void sd_stopN(struct gspca_dev *gspca_dev) 1240static void sd_stopN(struct gspca_dev *gspca_dev)
@@ -1387,37 +1376,6 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1387 } 1376 }
1388} 1377}
1389 1378
1390static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1391{
1392 struct sd *sd = (struct sd *) gspca_dev;
1393
1394 sd->ctrls[AUTOGAIN].val = val;
1395 sd->exp_too_high_cnt = 0;
1396 sd->exp_too_low_cnt = 0;
1397
1398 /* when switching to autogain set defaults to make sure
1399 we are on a valid point of the autogain gain /
1400 exposure knee graph, and give this change time to
1401 take effect before doing autogain. */
1402 if (sd->ctrls[AUTOGAIN].val
1403 && !(sensor_data[sd->sensor].flags & F_COARSE_EXPO)) {
1404 sd->ctrls[EXPOSURE].val = sd->ctrls[EXPOSURE].def;
1405 sd->ctrls[GAIN].val = sd->ctrls[GAIN].def;
1406 if (gspca_dev->streaming) {
1407 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
1408 setexposure(gspca_dev);
1409 setgain(gspca_dev);
1410 }
1411 }
1412
1413 if (sd->ctrls[AUTOGAIN].val)
1414 gspca_dev->ctrl_inac = (1 << GAIN) | (1 << EXPOSURE);
1415 else
1416 gspca_dev->ctrl_inac = 0;
1417
1418 return 0;
1419}
1420
1421static int sd_querymenu(struct gspca_dev *gspca_dev, 1379static int sd_querymenu(struct gspca_dev *gspca_dev,
1422 struct v4l2_querymenu *menu) 1380 struct v4l2_querymenu *menu)
1423{ 1381{
@@ -1461,10 +1419,9 @@ static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1461/* sub-driver description */ 1419/* sub-driver description */
1462static const struct sd_desc sd_desc = { 1420static const struct sd_desc sd_desc = {
1463 .name = MODULE_NAME, 1421 .name = MODULE_NAME,
1464 .ctrls = sd_ctrls,
1465 .nctrls = ARRAY_SIZE(sd_ctrls),
1466 .config = sd_config, 1422 .config = sd_config,
1467 .init = sd_init, 1423 .init = sd_init,
1424 .init_controls = sd_init_controls,
1468 .start = sd_start, 1425 .start = sd_start,
1469 .stopN = sd_stopN, 1426 .stopN = sd_stopN,
1470 .pkt_scan = sd_pkt_scan, 1427 .pkt_scan = sd_pkt_scan,
@@ -1529,6 +1486,7 @@ static struct usb_driver sd_driver = {
1529#ifdef CONFIG_PM 1486#ifdef CONFIG_PM
1530 .suspend = gspca_suspend, 1487 .suspend = gspca_suspend,
1531 .resume = gspca_resume, 1488 .resume = gspca_resume,
1489 .reset_resume = gspca_resume,
1532#endif 1490#endif
1533}; 1491};
1534 1492
diff --git a/drivers/media/video/gspca/sonixj.c b/drivers/media/video/gspca/sonixj.c
index f38faa9b37c3..150b2df40f7f 100644
--- a/drivers/media/video/gspca/sonixj.c
+++ b/drivers/media/video/gspca/sonixj.c
@@ -3199,6 +3199,7 @@ static struct usb_driver sd_driver = {
3199#ifdef CONFIG_PM 3199#ifdef CONFIG_PM
3200 .suspend = gspca_suspend, 3200 .suspend = gspca_suspend,
3201 .resume = gspca_resume, 3201 .resume = gspca_resume,
3202 .reset_resume = gspca_resume,
3202#endif 3203#endif
3203}; 3204};
3204 3205
diff --git a/drivers/media/video/gspca/spca1528.c b/drivers/media/video/gspca/spca1528.c
index 070b9c33b517..14d635277d71 100644
--- a/drivers/media/video/gspca/spca1528.c
+++ b/drivers/media/video/gspca/spca1528.c
@@ -33,102 +33,11 @@ MODULE_LICENSE("GPL");
33struct sd { 33struct sd {
34 struct gspca_dev gspca_dev; /* !! must be the first item */ 34 struct gspca_dev gspca_dev; /* !! must be the first item */
35 35
36 u8 brightness;
37 u8 contrast;
38 u8 hue;
39 u8 color;
40 u8 sharpness;
41
42 u8 pkt_seq; 36 u8 pkt_seq;
43 37
44 u8 jpeg_hdr[JPEG_HDR_SZ]; 38 u8 jpeg_hdr[JPEG_HDR_SZ];
45}; 39};
46 40
47/* V4L2 controls supported by the driver */
48static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
49static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
50static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
51static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
52static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val);
53static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val);
54static int sd_setcolor(struct gspca_dev *gspca_dev, __s32 val);
55static int sd_getcolor(struct gspca_dev *gspca_dev, __s32 *val);
56static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
57static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
58
59static const struct ctrl sd_ctrls[] = {
60 {
61 {
62 .id = V4L2_CID_BRIGHTNESS,
63 .type = V4L2_CTRL_TYPE_INTEGER,
64 .name = "Brightness",
65 .minimum = 0,
66 .maximum = 255,
67 .step = 1,
68#define BRIGHTNESS_DEF 128
69 .default_value = BRIGHTNESS_DEF,
70 },
71 .set = sd_setbrightness,
72 .get = sd_getbrightness,
73 },
74 {
75 {
76 .id = V4L2_CID_CONTRAST,
77 .type = V4L2_CTRL_TYPE_INTEGER,
78 .name = "Contrast",
79 .minimum = 0,
80 .maximum = 8,
81 .step = 1,
82#define CONTRAST_DEF 1
83 .default_value = CONTRAST_DEF,
84 },
85 .set = sd_setcontrast,
86 .get = sd_getcontrast,
87 },
88 {
89 {
90 .id = V4L2_CID_HUE,
91 .type = V4L2_CTRL_TYPE_INTEGER,
92 .name = "Hue",
93 .minimum = 0,
94 .maximum = 255,
95 .step = 1,
96#define HUE_DEF 0
97 .default_value = HUE_DEF,
98 },
99 .set = sd_sethue,
100 .get = sd_gethue,
101 },
102 {
103 {
104 .id = V4L2_CID_SATURATION,
105 .type = V4L2_CTRL_TYPE_INTEGER,
106 .name = "Saturation",
107 .minimum = 0,
108 .maximum = 8,
109 .step = 1,
110#define COLOR_DEF 1
111 .default_value = COLOR_DEF,
112 },
113 .set = sd_setcolor,
114 .get = sd_getcolor,
115 },
116 {
117 {
118 .id = V4L2_CID_SHARPNESS,
119 .type = V4L2_CTRL_TYPE_INTEGER,
120 .name = "Sharpness",
121 .minimum = 0,
122 .maximum = 255,
123 .step = 1,
124#define SHARPNESS_DEF 0
125 .default_value = SHARPNESS_DEF,
126 },
127 .set = sd_setsharpness,
128 .get = sd_getsharpness,
129 },
130};
131
132static const struct v4l2_pix_format vga_mode[] = { 41static const struct v4l2_pix_format vga_mode[] = {
133/* (does not work correctly) 42/* (does not work correctly)
134 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 43 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
@@ -259,58 +168,40 @@ static void wait_status_1(struct gspca_dev *gspca_dev)
259 gspca_dev->usb_err = -ETIME; 168 gspca_dev->usb_err = -ETIME;
260} 169}
261 170
262static void setbrightness(struct gspca_dev *gspca_dev) 171static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
263{ 172{
264 struct sd *sd = (struct sd *) gspca_dev; 173 reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
265
266 reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, sd->brightness);
267} 174}
268 175
269static void setcontrast(struct gspca_dev *gspca_dev) 176static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
270{ 177{
271 struct sd *sd = (struct sd *) gspca_dev; 178 reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
272
273 reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, sd->contrast);
274} 179}
275 180
276static void sethue(struct gspca_dev *gspca_dev) 181static void sethue(struct gspca_dev *gspca_dev, s32 val)
277{ 182{
278 struct sd *sd = (struct sd *) gspca_dev; 183 reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
279
280 reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, sd->hue);
281} 184}
282 185
283static void setcolor(struct gspca_dev *gspca_dev) 186static void setcolor(struct gspca_dev *gspca_dev, s32 val)
284{ 187{
285 struct sd *sd = (struct sd *) gspca_dev; 188 reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
286
287 reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, sd->color);
288} 189}
289 190
290static void setsharpness(struct gspca_dev *gspca_dev) 191static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
291{ 192{
292 struct sd *sd = (struct sd *) gspca_dev; 193 reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
293
294 reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, sd->sharpness);
295} 194}
296 195
297/* this function is called at probe time */ 196/* this function is called at probe time */
298static int sd_config(struct gspca_dev *gspca_dev, 197static int sd_config(struct gspca_dev *gspca_dev,
299 const struct usb_device_id *id) 198 const struct usb_device_id *id)
300{ 199{
301 struct sd *sd = (struct sd *) gspca_dev;
302
303 gspca_dev->cam.cam_mode = vga_mode; 200 gspca_dev->cam.cam_mode = vga_mode;
304 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode); 201 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
305 gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */ 202 gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */
306 /*fixme: 256 in ms-win traces*/ 203 /*fixme: 256 in ms-win traces*/
307 204
308 sd->brightness = BRIGHTNESS_DEF;
309 sd->contrast = CONTRAST_DEF;
310 sd->hue = HUE_DEF;
311 sd->color = COLOR_DEF;
312 sd->sharpness = SHARPNESS_DEF;
313
314 return 0; 205 return 0;
315} 206}
316 207
@@ -370,14 +261,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
370 /* the JPEG quality shall be 85% */ 261 /* the JPEG quality shall be 85% */
371 jpeg_set_qual(sd->jpeg_hdr, 85); 262 jpeg_set_qual(sd->jpeg_hdr, 85);
372 263
373 /* set the controls */
374 setbrightness(gspca_dev);
375 setcontrast(gspca_dev);
376 sethue(gspca_dev);
377 setcolor(gspca_dev);
378 setsharpness(gspca_dev);
379
380 msleep(5);
381 reg_r(gspca_dev, 0x00, 0x2520, 1); 264 reg_r(gspca_dev, 0x00, 0x2520, 1);
382 msleep(8); 265 msleep(8);
383 266
@@ -457,103 +340,70 @@ err:
457 gspca_dev->last_packet_type = DISCARD_PACKET; 340 gspca_dev->last_packet_type = DISCARD_PACKET;
458} 341}
459 342
460static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 343static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
461{
462 struct sd *sd = (struct sd *) gspca_dev;
463
464 sd->brightness = val;
465 if (gspca_dev->streaming)
466 setbrightness(gspca_dev);
467 return gspca_dev->usb_err;
468}
469
470static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
471{
472 struct sd *sd = (struct sd *) gspca_dev;
473
474 *val = sd->brightness;
475 return 0;
476}
477
478static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
479{ 344{
480 struct sd *sd = (struct sd *) gspca_dev; 345 struct gspca_dev *gspca_dev =
481 346 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
482 sd->contrast = val; 347
483 if (gspca_dev->streaming) 348 gspca_dev->usb_err = 0;
484 setcontrast(gspca_dev); 349
485 return gspca_dev->usb_err; 350 if (!gspca_dev->streaming)
486} 351 return 0;
487 352
488static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val) 353 switch (ctrl->id) {
489{ 354 case V4L2_CID_BRIGHTNESS:
490 struct sd *sd = (struct sd *) gspca_dev; 355 setbrightness(gspca_dev, ctrl->val);
491 356 break;
492 *val = sd->contrast; 357 case V4L2_CID_CONTRAST:
493 return 0; 358 setcontrast(gspca_dev, ctrl->val);
494} 359 break;
495 360 case V4L2_CID_HUE:
496static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val) 361 sethue(gspca_dev, ctrl->val);
497{ 362 break;
498 struct sd *sd = (struct sd *) gspca_dev; 363 case V4L2_CID_SATURATION:
499 364 setcolor(gspca_dev, ctrl->val);
500 sd->hue = val; 365 break;
501 if (gspca_dev->streaming) 366 case V4L2_CID_SHARPNESS:
502 sethue(gspca_dev); 367 setsharpness(gspca_dev, ctrl->val);
503 return gspca_dev->usb_err; 368 break;
504} 369 }
505
506static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val)
507{
508 struct sd *sd = (struct sd *) gspca_dev;
509
510 *val = sd->hue;
511 return 0;
512}
513
514static int sd_setcolor(struct gspca_dev *gspca_dev, __s32 val)
515{
516 struct sd *sd = (struct sd *) gspca_dev;
517
518 sd->color = val;
519 if (gspca_dev->streaming)
520 setcolor(gspca_dev);
521 return gspca_dev->usb_err; 370 return gspca_dev->usb_err;
522} 371}
523 372
524static int sd_getcolor(struct gspca_dev *gspca_dev, __s32 *val) 373static const struct v4l2_ctrl_ops sd_ctrl_ops = {
525{ 374 .s_ctrl = sd_s_ctrl,
526 struct sd *sd = (struct sd *) gspca_dev; 375};
527
528 *val = sd->color;
529 return 0;
530}
531
532static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
533{
534 struct sd *sd = (struct sd *) gspca_dev;
535
536 sd->sharpness = val;
537 if (gspca_dev->streaming)
538 setsharpness(gspca_dev);
539 return gspca_dev->usb_err;
540}
541 376
542static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val) 377static int sd_init_controls(struct gspca_dev *gspca_dev)
543{ 378{
544 struct sd *sd = (struct sd *) gspca_dev; 379 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
545 380
546 *val = sd->sharpness; 381 gspca_dev->vdev.ctrl_handler = hdl;
382 v4l2_ctrl_handler_init(hdl, 5);
383 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
384 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
385 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
386 V4L2_CID_CONTRAST, 0, 8, 1, 1);
387 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
388 V4L2_CID_HUE, 0, 255, 1, 0);
389 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
390 V4L2_CID_SATURATION, 0, 8, 1, 1);
391 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
392 V4L2_CID_SHARPNESS, 0, 255, 1, 0);
393
394 if (hdl->error) {
395 pr_err("Could not initialize controls\n");
396 return hdl->error;
397 }
547 return 0; 398 return 0;
548} 399}
549 400
550/* sub-driver description */ 401/* sub-driver description */
551static const struct sd_desc sd_desc = { 402static const struct sd_desc sd_desc = {
552 .name = MODULE_NAME, 403 .name = MODULE_NAME,
553 .ctrls = sd_ctrls,
554 .nctrls = ARRAY_SIZE(sd_ctrls),
555 .config = sd_config, 404 .config = sd_config,
556 .init = sd_init, 405 .init = sd_init,
406 .init_controls = sd_init_controls,
557 .isoc_init = sd_isoc_init, 407 .isoc_init = sd_isoc_init,
558 .start = sd_start, 408 .start = sd_start,
559 .stopN = sd_stopN, 409 .stopN = sd_stopN,
@@ -587,6 +437,7 @@ static struct usb_driver sd_driver = {
587#ifdef CONFIG_PM 437#ifdef CONFIG_PM
588 .suspend = gspca_suspend, 438 .suspend = gspca_suspend,
589 .resume = gspca_resume, 439 .resume = gspca_resume,
440 .reset_resume = gspca_resume,
590#endif 441#endif
591}; 442};
592 443
diff --git a/drivers/media/video/gspca/spca500.c b/drivers/media/video/gspca/spca500.c
index 103984708c77..25cb68d0556d 100644
--- a/drivers/media/video/gspca/spca500.c
+++ b/drivers/media/video/gspca/spca500.c
@@ -30,18 +30,12 @@ MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
30MODULE_DESCRIPTION("GSPCA/SPCA500 USB Camera Driver"); 30MODULE_DESCRIPTION("GSPCA/SPCA500 USB Camera Driver");
31MODULE_LICENSE("GPL"); 31MODULE_LICENSE("GPL");
32 32
33#define QUALITY 85
34
33/* specific webcam descriptor */ 35/* specific webcam descriptor */
34struct sd { 36struct sd {
35 struct gspca_dev gspca_dev; /* !! must be the first item */ 37 struct gspca_dev gspca_dev; /* !! must be the first item */
36 38
37 unsigned char brightness;
38 unsigned char contrast;
39 unsigned char colors;
40 u8 quality;
41#define QUALITY_MIN 70
42#define QUALITY_MAX 95
43#define QUALITY_DEF 85
44
45 char subtype; 39 char subtype;
46#define AgfaCl20 0 40#define AgfaCl20 0
47#define AiptekPocketDV 1 41#define AiptekPocketDV 1
@@ -62,59 +56,6 @@ struct sd {
62 u8 jpeg_hdr[JPEG_HDR_SZ]; 56 u8 jpeg_hdr[JPEG_HDR_SZ];
63}; 57};
64 58
65/* V4L2 controls supported by the driver */
66static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
67static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
68static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
69static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
70static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
71static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
72
73static const struct ctrl sd_ctrls[] = {
74 {
75 {
76 .id = V4L2_CID_BRIGHTNESS,
77 .type = V4L2_CTRL_TYPE_INTEGER,
78 .name = "Brightness",
79 .minimum = 0,
80 .maximum = 255,
81 .step = 1,
82#define BRIGHTNESS_DEF 127
83 .default_value = BRIGHTNESS_DEF,
84 },
85 .set = sd_setbrightness,
86 .get = sd_getbrightness,
87 },
88 {
89 {
90 .id = V4L2_CID_CONTRAST,
91 .type = V4L2_CTRL_TYPE_INTEGER,
92 .name = "Contrast",
93 .minimum = 0,
94 .maximum = 63,
95 .step = 1,
96#define CONTRAST_DEF 31
97 .default_value = CONTRAST_DEF,
98 },
99 .set = sd_setcontrast,
100 .get = sd_getcontrast,
101 },
102 {
103 {
104 .id = V4L2_CID_SATURATION,
105 .type = V4L2_CTRL_TYPE_INTEGER,
106 .name = "Color",
107 .minimum = 0,
108 .maximum = 63,
109 .step = 1,
110#define COLOR_DEF 31
111 .default_value = COLOR_DEF,
112 },
113 .set = sd_setcolors,
114 .get = sd_getcolors,
115 },
116};
117
118static const struct v4l2_pix_format vga_mode[] = { 59static const struct v4l2_pix_format vga_mode[] = {
119 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 60 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
120 .bytesperline = 320, 61 .bytesperline = 320,
@@ -641,10 +582,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
641 cam->cam_mode = sif_mode; 582 cam->cam_mode = sif_mode;
642 cam->nmodes = ARRAY_SIZE(sif_mode); 583 cam->nmodes = ARRAY_SIZE(sif_mode);
643 } 584 }
644 sd->brightness = BRIGHTNESS_DEF;
645 sd->contrast = CONTRAST_DEF;
646 sd->colors = COLOR_DEF;
647 sd->quality = QUALITY_DEF;
648 return 0; 585 return 0;
649} 586}
650 587
@@ -673,7 +610,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
673 /* create the JPEG header */ 610 /* create the JPEG header */
674 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width, 611 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
675 0x22); /* JPEG 411 */ 612 0x22); /* JPEG 411 */
676 jpeg_set_qual(sd->jpeg_hdr, sd->quality); 613 jpeg_set_qual(sd->jpeg_hdr, QUALITY);
677 614
678 if (sd->subtype == LogitechClickSmart310) { 615 if (sd->subtype == LogitechClickSmart310) {
679 xmult = 0x16; 616 xmult = 0x16;
@@ -934,122 +871,79 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
934 gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 871 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
935} 872}
936 873
937static void setbrightness(struct gspca_dev *gspca_dev) 874static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
938{ 875{
939 struct sd *sd = (struct sd *) gspca_dev;
940
941 reg_w(gspca_dev, 0x00, 0x8167, 876 reg_w(gspca_dev, 0x00, 0x8167,
942 (__u8) (sd->brightness - 128)); 877 (__u8) (val - 128));
943} 878}
944 879
945static void setcontrast(struct gspca_dev *gspca_dev) 880static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
946{ 881{
947 struct sd *sd = (struct sd *) gspca_dev; 882 reg_w(gspca_dev, 0x00, 0x8168, val);
948
949 reg_w(gspca_dev, 0x00, 0x8168, sd->contrast);
950} 883}
951 884
952static void setcolors(struct gspca_dev *gspca_dev) 885static void setcolors(struct gspca_dev *gspca_dev, s32 val)
953{ 886{
954 struct sd *sd = (struct sd *) gspca_dev; 887 reg_w(gspca_dev, 0x00, 0x8169, val);
955
956 reg_w(gspca_dev, 0x00, 0x8169, sd->colors);
957} 888}
958 889
959static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 890static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
960{ 891{
961 struct sd *sd = (struct sd *) gspca_dev; 892 struct gspca_dev *gspca_dev =
893 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
962 894
963 sd->brightness = val; 895 gspca_dev->usb_err = 0;
964 if (gspca_dev->streaming)
965 setbrightness(gspca_dev);
966 return 0;
967}
968 896
969static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val) 897 if (!gspca_dev->streaming)
970{ 898 return 0;
971 struct sd *sd = (struct sd *) gspca_dev;
972
973 *val = sd->brightness;
974 return 0;
975}
976
977static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
978{
979 struct sd *sd = (struct sd *) gspca_dev;
980
981 sd->contrast = val;
982 if (gspca_dev->streaming)
983 setcontrast(gspca_dev);
984 return 0;
985}
986
987static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
988{
989 struct sd *sd = (struct sd *) gspca_dev;
990 899
991 *val = sd->contrast; 900 switch (ctrl->id) {
992 return 0; 901 case V4L2_CID_BRIGHTNESS:
993} 902 setbrightness(gspca_dev, ctrl->val);
994 903 break;
995static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val) 904 case V4L2_CID_CONTRAST:
996{ 905 setcontrast(gspca_dev, ctrl->val);
997 struct sd *sd = (struct sd *) gspca_dev; 906 break;
998 907 case V4L2_CID_SATURATION:
999 sd->colors = val; 908 setcolors(gspca_dev, ctrl->val);
1000 if (gspca_dev->streaming) 909 break;
1001 setcolors(gspca_dev); 910 }
1002 return 0; 911 return gspca_dev->usb_err;
1003}
1004
1005static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
1006{
1007 struct sd *sd = (struct sd *) gspca_dev;
1008
1009 *val = sd->colors;
1010 return 0;
1011} 912}
1012 913
1013static int sd_set_jcomp(struct gspca_dev *gspca_dev, 914static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1014 struct v4l2_jpegcompression *jcomp) 915 .s_ctrl = sd_s_ctrl,
1015{ 916};
1016 struct sd *sd = (struct sd *) gspca_dev;
1017
1018 if (jcomp->quality < QUALITY_MIN)
1019 sd->quality = QUALITY_MIN;
1020 else if (jcomp->quality > QUALITY_MAX)
1021 sd->quality = QUALITY_MAX;
1022 else
1023 sd->quality = jcomp->quality;
1024 if (gspca_dev->streaming)
1025 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
1026 return 0;
1027}
1028 917
1029static int sd_get_jcomp(struct gspca_dev *gspca_dev, 918static int sd_init_controls(struct gspca_dev *gspca_dev)
1030 struct v4l2_jpegcompression *jcomp)
1031{ 919{
1032 struct sd *sd = (struct sd *) gspca_dev; 920 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1033 921
1034 memset(jcomp, 0, sizeof *jcomp); 922 gspca_dev->vdev.ctrl_handler = hdl;
1035 jcomp->quality = sd->quality; 923 v4l2_ctrl_handler_init(hdl, 3);
1036 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT 924 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1037 | V4L2_JPEG_MARKER_DQT; 925 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
926 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
927 V4L2_CID_CONTRAST, 0, 63, 1, 31);
928 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
929 V4L2_CID_SATURATION, 0, 63, 1, 31);
930
931 if (hdl->error) {
932 pr_err("Could not initialize controls\n");
933 return hdl->error;
934 }
1038 return 0; 935 return 0;
1039} 936}
1040 937
1041/* sub-driver description */ 938/* sub-driver description */
1042static const struct sd_desc sd_desc = { 939static const struct sd_desc sd_desc = {
1043 .name = MODULE_NAME, 940 .name = MODULE_NAME,
1044 .ctrls = sd_ctrls,
1045 .nctrls = ARRAY_SIZE(sd_ctrls),
1046 .config = sd_config, 941 .config = sd_config,
1047 .init = sd_init, 942 .init = sd_init,
943 .init_controls = sd_init_controls,
1048 .start = sd_start, 944 .start = sd_start,
1049 .stopN = sd_stopN, 945 .stopN = sd_stopN,
1050 .pkt_scan = sd_pkt_scan, 946 .pkt_scan = sd_pkt_scan,
1051 .get_jcomp = sd_get_jcomp,
1052 .set_jcomp = sd_set_jcomp,
1053}; 947};
1054 948
1055/* -- module initialisation -- */ 949/* -- module initialisation -- */
@@ -1089,6 +983,7 @@ static struct usb_driver sd_driver = {
1089#ifdef CONFIG_PM 983#ifdef CONFIG_PM
1090 .suspend = gspca_suspend, 984 .suspend = gspca_suspend,
1091 .resume = gspca_resume, 985 .resume = gspca_resume,
986 .reset_resume = gspca_resume,
1092#endif 987#endif
1093}; 988};
1094 989
diff --git a/drivers/media/video/gspca/spca501.c b/drivers/media/video/gspca/spca501.c
index 9c16821addd4..3b7f777785b4 100644
--- a/drivers/media/video/gspca/spca501.c
+++ b/drivers/media/video/gspca/spca501.c
@@ -49,91 +49,6 @@ struct sd {
49#define ViewQuestM318B 6 49#define ViewQuestM318B 6
50}; 50};
51 51
52/* V4L2 controls supported by the driver */
53static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
54static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
55static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
56static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
57static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
58static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
59static int sd_setblue_balance(struct gspca_dev *gspca_dev, __s32 val);
60static int sd_getblue_balance(struct gspca_dev *gspca_dev, __s32 *val);
61static int sd_setred_balance(struct gspca_dev *gspca_dev, __s32 val);
62static int sd_getred_balance(struct gspca_dev *gspca_dev, __s32 *val);
63
64static const struct ctrl sd_ctrls[] = {
65#define MY_BRIGHTNESS 0
66 {
67 {
68 .id = V4L2_CID_BRIGHTNESS,
69 .type = V4L2_CTRL_TYPE_INTEGER,
70 .name = "Brightness",
71 .minimum = 0,
72 .maximum = 127,
73 .step = 1,
74 .default_value = 0,
75 },
76 .set = sd_setbrightness,
77 .get = sd_getbrightness,
78 },
79#define MY_CONTRAST 1
80 {
81 {
82 .id = V4L2_CID_CONTRAST,
83 .type = V4L2_CTRL_TYPE_INTEGER,
84 .name = "Contrast",
85 .minimum = 0,
86 .maximum = 64725,
87 .step = 1,
88 .default_value = 64725,
89 },
90 .set = sd_setcontrast,
91 .get = sd_getcontrast,
92 },
93#define MY_COLOR 2
94 {
95 {
96 .id = V4L2_CID_SATURATION,
97 .type = V4L2_CTRL_TYPE_INTEGER,
98 .name = "Color",
99 .minimum = 0,
100 .maximum = 63,
101 .step = 1,
102 .default_value = 20,
103 },
104 .set = sd_setcolors,
105 .get = sd_getcolors,
106 },
107#define MY_BLUE_BALANCE 3
108 {
109 {
110 .id = V4L2_CID_BLUE_BALANCE,
111 .type = V4L2_CTRL_TYPE_INTEGER,
112 .name = "Blue Balance",
113 .minimum = 0,
114 .maximum = 127,
115 .step = 1,
116 .default_value = 0,
117 },
118 .set = sd_setblue_balance,
119 .get = sd_getblue_balance,
120 },
121#define MY_RED_BALANCE 4
122 {
123 {
124 .id = V4L2_CID_RED_BALANCE,
125 .type = V4L2_CTRL_TYPE_INTEGER,
126 .name = "Red Balance",
127 .minimum = 0,
128 .maximum = 127,
129 .step = 1,
130 .default_value = 0,
131 },
132 .set = sd_setred_balance,
133 .get = sd_getred_balance,
134 },
135};
136
137static const struct v4l2_pix_format vga_mode[] = { 52static const struct v4l2_pix_format vga_mode[] = {
138 {160, 120, V4L2_PIX_FMT_SPCA501, V4L2_FIELD_NONE, 53 {160, 120, V4L2_PIX_FMT_SPCA501, V4L2_FIELD_NONE,
139 .bytesperline = 160, 54 .bytesperline = 160,
@@ -1878,42 +1793,32 @@ static int write_vector(struct gspca_dev *gspca_dev,
1878 return 0; 1793 return 0;
1879} 1794}
1880 1795
1881static void setbrightness(struct gspca_dev *gspca_dev) 1796static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
1882{ 1797{
1883 struct sd *sd = (struct sd *) gspca_dev; 1798 reg_write(gspca_dev->dev, SPCA501_REG_CCDSP, 0x12, val);
1884
1885 reg_write(gspca_dev->dev, SPCA501_REG_CCDSP, 0x12, sd->brightness);
1886} 1799}
1887 1800
1888static void setcontrast(struct gspca_dev *gspca_dev) 1801static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
1889{ 1802{
1890 struct sd *sd = (struct sd *) gspca_dev;
1891
1892 reg_write(gspca_dev->dev, 0x00, 0x00, 1803 reg_write(gspca_dev->dev, 0x00, 0x00,
1893 (sd->contrast >> 8) & 0xff); 1804 (val >> 8) & 0xff);
1894 reg_write(gspca_dev->dev, 0x00, 0x01, 1805 reg_write(gspca_dev->dev, 0x00, 0x01,
1895 sd->contrast & 0xff); 1806 val & 0xff);
1896} 1807}
1897 1808
1898static void setcolors(struct gspca_dev *gspca_dev) 1809static void setcolors(struct gspca_dev *gspca_dev, s32 val)
1899{ 1810{
1900 struct sd *sd = (struct sd *) gspca_dev; 1811 reg_write(gspca_dev->dev, SPCA501_REG_CCDSP, 0x0c, val);
1901
1902 reg_write(gspca_dev->dev, SPCA501_REG_CCDSP, 0x0c, sd->colors);
1903} 1812}
1904 1813
1905static void setblue_balance(struct gspca_dev *gspca_dev) 1814static void setblue_balance(struct gspca_dev *gspca_dev, s32 val)
1906{ 1815{
1907 struct sd *sd = (struct sd *) gspca_dev; 1816 reg_write(gspca_dev->dev, SPCA501_REG_CCDSP, 0x11, val);
1908
1909 reg_write(gspca_dev->dev, SPCA501_REG_CCDSP, 0x11, sd->blue_balance);
1910} 1817}
1911 1818
1912static void setred_balance(struct gspca_dev *gspca_dev) 1819static void setred_balance(struct gspca_dev *gspca_dev, s32 val)
1913{ 1820{
1914 struct sd *sd = (struct sd *) gspca_dev; 1821 reg_write(gspca_dev->dev, SPCA501_REG_CCDSP, 0x13, val);
1915
1916 reg_write(gspca_dev->dev, SPCA501_REG_CCDSP, 0x13, sd->red_balance);
1917} 1822}
1918 1823
1919/* this function is called at probe time */ 1824/* this function is called at probe time */
@@ -1927,9 +1832,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
1927 cam->cam_mode = vga_mode; 1832 cam->cam_mode = vga_mode;
1928 cam->nmodes = ARRAY_SIZE(vga_mode); 1833 cam->nmodes = ARRAY_SIZE(vga_mode);
1929 sd->subtype = id->driver_info; 1834 sd->subtype = id->driver_info;
1930 sd->brightness = sd_ctrls[MY_BRIGHTNESS].qctrl.default_value;
1931 sd->contrast = sd_ctrls[MY_CONTRAST].qctrl.default_value;
1932 sd->colors = sd_ctrls[MY_COLOR].qctrl.default_value;
1933 1835
1934 return 0; 1836 return 0;
1935} 1837}
@@ -2008,13 +1910,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
2008 } 1910 }
2009 reg_write(dev, SPCA501_REG_CTLRL, 0x01, 0x02); 1911 reg_write(dev, SPCA501_REG_CTLRL, 0x01, 0x02);
2010 1912
2011 /* HDG atleast the Intel CreateAndShare needs to have one of its
2012 * brightness / contrast / color set otherwise it assumes what seems
2013 * max contrast. Note that strange enough setting any of these is
2014 * enough to fix the max contrast problem, to be sure we set all 3 */
2015 setbrightness(gspca_dev);
2016 setcontrast(gspca_dev);
2017 setcolors(gspca_dev);
2018 return 0; 1913 return 0;
2019} 1914}
2020 1915
@@ -2053,103 +1948,70 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
2053 gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 1948 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
2054} 1949}
2055 1950
2056static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 1951static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
2057{ 1952{
2058 struct sd *sd = (struct sd *) gspca_dev; 1953 struct gspca_dev *gspca_dev =
1954 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
2059 1955
2060 sd->brightness = val; 1956 gspca_dev->usb_err = 0;
2061 if (gspca_dev->streaming)
2062 setbrightness(gspca_dev);
2063 return 0;
2064}
2065
2066static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
2067{
2068 struct sd *sd = (struct sd *) gspca_dev;
2069 1957
2070 *val = sd->brightness; 1958 if (!gspca_dev->streaming)
2071 return 0; 1959 return 0;
2072}
2073
2074static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
2075{
2076 struct sd *sd = (struct sd *) gspca_dev;
2077
2078 sd->contrast = val;
2079 if (gspca_dev->streaming)
2080 setcontrast(gspca_dev);
2081 return 0;
2082}
2083
2084static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
2085{
2086 struct sd *sd = (struct sd *) gspca_dev;
2087
2088 *val = sd->contrast;
2089 return 0;
2090}
2091
2092static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
2093{
2094 struct sd *sd = (struct sd *) gspca_dev;
2095 1960
2096 sd->colors = val; 1961 switch (ctrl->id) {
2097 if (gspca_dev->streaming) 1962 case V4L2_CID_BRIGHTNESS:
2098 setcolors(gspca_dev); 1963 setbrightness(gspca_dev, ctrl->val);
2099 return 0; 1964 break;
2100} 1965 case V4L2_CID_CONTRAST:
2101 1966 setcontrast(gspca_dev, ctrl->val);
2102static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val) 1967 break;
2103{ 1968 case V4L2_CID_SATURATION:
2104 struct sd *sd = (struct sd *) gspca_dev; 1969 setcolors(gspca_dev, ctrl->val);
2105 1970 break;
2106 *val = sd->colors; 1971 case V4L2_CID_BLUE_BALANCE:
2107 return 0; 1972 setblue_balance(gspca_dev, ctrl->val);
2108} 1973 break;
2109 1974 case V4L2_CID_RED_BALANCE:
2110static int sd_setblue_balance(struct gspca_dev *gspca_dev, __s32 val) 1975 setred_balance(gspca_dev, ctrl->val);
2111{ 1976 break;
2112 struct sd *sd = (struct sd *) gspca_dev; 1977 }
2113 1978 return gspca_dev->usb_err;
2114 sd->blue_balance = val;
2115 if (gspca_dev->streaming)
2116 setblue_balance(gspca_dev);
2117 return 0;
2118}
2119
2120static int sd_getblue_balance(struct gspca_dev *gspca_dev, __s32 *val)
2121{
2122 struct sd *sd = (struct sd *) gspca_dev;
2123
2124 *val = sd->blue_balance;
2125 return 0;
2126} 1979}
2127 1980
2128static int sd_setred_balance(struct gspca_dev *gspca_dev, __s32 val) 1981static const struct v4l2_ctrl_ops sd_ctrl_ops = {
2129{ 1982 .s_ctrl = sd_s_ctrl,
2130 struct sd *sd = (struct sd *) gspca_dev; 1983};
2131
2132 sd->red_balance = val;
2133 if (gspca_dev->streaming)
2134 setred_balance(gspca_dev);
2135 return 0;
2136}
2137 1984
2138static int sd_getred_balance(struct gspca_dev *gspca_dev, __s32 *val) 1985static int sd_init_controls(struct gspca_dev *gspca_dev)
2139{ 1986{
2140 struct sd *sd = (struct sd *) gspca_dev; 1987 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
2141 1988
2142 *val = sd->red_balance; 1989 gspca_dev->vdev.ctrl_handler = hdl;
1990 v4l2_ctrl_handler_init(hdl, 5);
1991 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1992 V4L2_CID_BRIGHTNESS, 0, 127, 1, 0);
1993 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1994 V4L2_CID_CONTRAST, 0, 64725, 1, 64725);
1995 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1996 V4L2_CID_SATURATION, 0, 63, 1, 20);
1997 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1998 V4L2_CID_BLUE_BALANCE, 0, 127, 1, 0);
1999 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
2000 V4L2_CID_RED_BALANCE, 0, 127, 1, 0);
2001
2002 if (hdl->error) {
2003 pr_err("Could not initialize controls\n");
2004 return hdl->error;
2005 }
2143 return 0; 2006 return 0;
2144} 2007}
2145 2008
2146/* sub-driver description */ 2009/* sub-driver description */
2147static const struct sd_desc sd_desc = { 2010static const struct sd_desc sd_desc = {
2148 .name = MODULE_NAME, 2011 .name = MODULE_NAME,
2149 .ctrls = sd_ctrls,
2150 .nctrls = ARRAY_SIZE(sd_ctrls),
2151 .config = sd_config, 2012 .config = sd_config,
2152 .init = sd_init, 2013 .init = sd_init,
2014 .init_controls = sd_init_controls,
2153 .start = sd_start, 2015 .start = sd_start,
2154 .stopN = sd_stopN, 2016 .stopN = sd_stopN,
2155 .stop0 = sd_stop0, 2017 .stop0 = sd_stop0,
@@ -2185,6 +2047,7 @@ static struct usb_driver sd_driver = {
2185#ifdef CONFIG_PM 2047#ifdef CONFIG_PM
2186 .suspend = gspca_suspend, 2048 .suspend = gspca_suspend,
2187 .resume = gspca_resume, 2049 .resume = gspca_resume,
2050 .reset_resume = gspca_resume,
2188#endif 2051#endif
2189}; 2052};
2190 2053
diff --git a/drivers/media/video/gspca/spca505.c b/drivers/media/video/gspca/spca505.c
index 1320f35e39f2..bc7d67c3cb04 100644
--- a/drivers/media/video/gspca/spca505.c
+++ b/drivers/media/video/gspca/spca505.c
@@ -33,34 +33,11 @@ MODULE_LICENSE("GPL");
33struct sd { 33struct sd {
34 struct gspca_dev gspca_dev; /* !! must be the first item */ 34 struct gspca_dev gspca_dev; /* !! must be the first item */
35 35
36 u8 brightness;
37
38 u8 subtype; 36 u8 subtype;
39#define IntelPCCameraPro 0 37#define IntelPCCameraPro 0
40#define Nxultra 1 38#define Nxultra 1
41}; 39};
42 40
43/* V4L2 controls supported by the driver */
44static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
45static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
46
47static const struct ctrl sd_ctrls[] = {
48 {
49 {
50 .id = V4L2_CID_BRIGHTNESS,
51 .type = V4L2_CTRL_TYPE_INTEGER,
52 .name = "Brightness",
53 .minimum = 0,
54 .maximum = 255,
55 .step = 1,
56#define BRIGHTNESS_DEF 127
57 .default_value = BRIGHTNESS_DEF,
58 },
59 .set = sd_setbrightness,
60 .get = sd_getbrightness,
61 },
62};
63
64static const struct v4l2_pix_format vga_mode[] = { 41static const struct v4l2_pix_format vga_mode[] = {
65 {160, 120, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE, 42 {160, 120, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
66 .bytesperline = 160, 43 .bytesperline = 160,
@@ -633,7 +610,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
633 cam->nmodes = ARRAY_SIZE(vga_mode); 610 cam->nmodes = ARRAY_SIZE(vga_mode);
634 else /* no 640x480 for IntelPCCameraPro */ 611 else /* no 640x480 for IntelPCCameraPro */
635 cam->nmodes = ARRAY_SIZE(vga_mode) - 1; 612 cam->nmodes = ARRAY_SIZE(vga_mode) - 1;
636 sd->brightness = BRIGHTNESS_DEF;
637 613
638 return 0; 614 return 0;
639} 615}
@@ -651,11 +627,8 @@ static int sd_init(struct gspca_dev *gspca_dev)
651 return 0; 627 return 0;
652} 628}
653 629
654static void setbrightness(struct gspca_dev *gspca_dev) 630static void setbrightness(struct gspca_dev *gspca_dev, s32 brightness)
655{ 631{
656 struct sd *sd = (struct sd *) gspca_dev;
657 u8 brightness = sd->brightness;
658
659 reg_write(gspca_dev->dev, 0x05, 0x00, (255 - brightness) >> 6); 632 reg_write(gspca_dev->dev, 0x05, 0x00, (255 - brightness) >> 6);
660 reg_write(gspca_dev->dev, 0x05, 0x01, (255 - brightness) << 2); 633 reg_write(gspca_dev->dev, 0x05, 0x01, (255 - brightness) << 2);
661} 634}
@@ -706,13 +679,9 @@ static int sd_start(struct gspca_dev *gspca_dev)
706 reg_write(dev, SPCA50X_REG_COMPRESS, 0x06, mode_tb[mode][1]); 679 reg_write(dev, SPCA50X_REG_COMPRESS, 0x06, mode_tb[mode][1]);
707 reg_write(dev, SPCA50X_REG_COMPRESS, 0x07, mode_tb[mode][2]); 680 reg_write(dev, SPCA50X_REG_COMPRESS, 0x07, mode_tb[mode][2]);
708 681
709 ret = reg_write(dev, SPCA50X_REG_USB, 682 return reg_write(dev, SPCA50X_REG_USB,
710 SPCA50X_USB_CTRL, 683 SPCA50X_USB_CTRL,
711 SPCA50X_CUSB_ENABLE); 684 SPCA50X_CUSB_ENABLE);
712
713 setbrightness(gspca_dev);
714
715 return ret;
716} 685}
717 686
718static void sd_stopN(struct gspca_dev *gspca_dev) 687static void sd_stopN(struct gspca_dev *gspca_dev)
@@ -756,30 +725,49 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
756 } 725 }
757} 726}
758 727
759static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 728static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
760{ 729{
761 struct sd *sd = (struct sd *) gspca_dev; 730 struct gspca_dev *gspca_dev =
731 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
762 732
763 sd->brightness = val; 733 gspca_dev->usb_err = 0;
764 if (gspca_dev->streaming) 734
765 setbrightness(gspca_dev); 735 if (!gspca_dev->streaming)
766 return 0; 736 return 0;
737
738 switch (ctrl->id) {
739 case V4L2_CID_BRIGHTNESS:
740 setbrightness(gspca_dev, ctrl->val);
741 break;
742 }
743 return gspca_dev->usb_err;
767} 744}
768 745
769static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val) 746static const struct v4l2_ctrl_ops sd_ctrl_ops = {
747 .s_ctrl = sd_s_ctrl,
748};
749
750static int sd_init_controls(struct gspca_dev *gspca_dev)
770{ 751{
771 struct sd *sd = (struct sd *) gspca_dev; 752 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
772 753
773 *val = sd->brightness; 754 gspca_dev->vdev.ctrl_handler = hdl;
755 v4l2_ctrl_handler_init(hdl, 5);
756 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
757 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
758
759 if (hdl->error) {
760 pr_err("Could not initialize controls\n");
761 return hdl->error;
762 }
774 return 0; 763 return 0;
775} 764}
776 765
777/* sub-driver description */ 766/* sub-driver description */
778static const struct sd_desc sd_desc = { 767static const struct sd_desc sd_desc = {
779 .name = MODULE_NAME, 768 .name = MODULE_NAME,
780 .ctrls = sd_ctrls,
781 .nctrls = ARRAY_SIZE(sd_ctrls),
782 .config = sd_config, 769 .config = sd_config,
770 .init_controls = sd_init_controls,
783 .init = sd_init, 771 .init = sd_init,
784 .start = sd_start, 772 .start = sd_start,
785 .stopN = sd_stopN, 773 .stopN = sd_stopN,
@@ -812,6 +800,7 @@ static struct usb_driver sd_driver = {
812#ifdef CONFIG_PM 800#ifdef CONFIG_PM
813 .suspend = gspca_suspend, 801 .suspend = gspca_suspend,
814 .resume = gspca_resume, 802 .resume = gspca_resume,
803 .reset_resume = gspca_resume,
815#endif 804#endif
816}; 805};
817 806
diff --git a/drivers/media/video/gspca/spca506.c b/drivers/media/video/gspca/spca506.c
index 54eed87672d2..969bb5a4cd93 100644
--- a/drivers/media/video/gspca/spca506.c
+++ b/drivers/media/video/gspca/spca506.c
@@ -33,83 +33,10 @@ MODULE_LICENSE("GPL");
33struct sd { 33struct sd {
34 struct gspca_dev gspca_dev; /* !! must be the first item */ 34 struct gspca_dev gspca_dev; /* !! must be the first item */
35 35
36 unsigned char brightness;
37 unsigned char contrast;
38 unsigned char colors;
39 unsigned char hue;
40 char norme; 36 char norme;
41 char channel; 37 char channel;
42}; 38};
43 39
44/* V4L2 controls supported by the driver */
45static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
46static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
47static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
48static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
49static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
50static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
51static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val);
52static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val);
53
54static const struct ctrl sd_ctrls[] = {
55#define SD_BRIGHTNESS 0
56 {
57 {
58 .id = V4L2_CID_BRIGHTNESS,
59 .type = V4L2_CTRL_TYPE_INTEGER,
60 .name = "Brightness",
61 .minimum = 0,
62 .maximum = 0xff,
63 .step = 1,
64 .default_value = 0x80,
65 },
66 .set = sd_setbrightness,
67 .get = sd_getbrightness,
68 },
69#define SD_CONTRAST 1
70 {
71 {
72 .id = V4L2_CID_CONTRAST,
73 .type = V4L2_CTRL_TYPE_INTEGER,
74 .name = "Contrast",
75 .minimum = 0,
76 .maximum = 0xff,
77 .step = 1,
78 .default_value = 0x47,
79 },
80 .set = sd_setcontrast,
81 .get = sd_getcontrast,
82 },
83#define SD_COLOR 2
84 {
85 {
86 .id = V4L2_CID_SATURATION,
87 .type = V4L2_CTRL_TYPE_INTEGER,
88 .name = "Saturation",
89 .minimum = 0,
90 .maximum = 0xff,
91 .step = 1,
92 .default_value = 0x40,
93 },
94 .set = sd_setcolors,
95 .get = sd_getcolors,
96 },
97#define SD_HUE 3
98 {
99 {
100 .id = V4L2_CID_HUE,
101 .type = V4L2_CTRL_TYPE_INTEGER,
102 .name = "Hue",
103 .minimum = 0,
104 .maximum = 0xff,
105 .step = 1,
106 .default_value = 0,
107 },
108 .set = sd_sethue,
109 .get = sd_gethue,
110 },
111};
112
113static const struct v4l2_pix_format vga_mode[] = { 40static const struct v4l2_pix_format vga_mode[] = {
114 {160, 120, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE, 41 {160, 120, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
115 .bytesperline = 160, 42 .bytesperline = 160,
@@ -281,16 +208,11 @@ static void spca506_Setsize(struct gspca_dev *gspca_dev, __u16 code,
281static int sd_config(struct gspca_dev *gspca_dev, 208static int sd_config(struct gspca_dev *gspca_dev,
282 const struct usb_device_id *id) 209 const struct usb_device_id *id)
283{ 210{
284 struct sd *sd = (struct sd *) gspca_dev;
285 struct cam *cam; 211 struct cam *cam;
286 212
287 cam = &gspca_dev->cam; 213 cam = &gspca_dev->cam;
288 cam->cam_mode = vga_mode; 214 cam->cam_mode = vga_mode;
289 cam->nmodes = ARRAY_SIZE(vga_mode); 215 cam->nmodes = ARRAY_SIZE(vga_mode);
290 sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
291 sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
292 sd->colors = sd_ctrls[SD_COLOR].qctrl.default_value;
293 sd->hue = sd_ctrls[SD_HUE].qctrl.default_value;
294 return 0; 216 return 0;
295} 217}
296 218
@@ -564,121 +486,93 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
564 } 486 }
565} 487}
566 488
567static void setbrightness(struct gspca_dev *gspca_dev) 489static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
568{ 490{
569 struct sd *sd = (struct sd *) gspca_dev;
570
571 spca506_Initi2c(gspca_dev); 491 spca506_Initi2c(gspca_dev);
572 spca506_WriteI2c(gspca_dev, sd->brightness, SAA7113_bright); 492 spca506_WriteI2c(gspca_dev, val, SAA7113_bright);
573 spca506_WriteI2c(gspca_dev, 0x01, 0x09); 493 spca506_WriteI2c(gspca_dev, 0x01, 0x09);
574} 494}
575 495
576static void setcontrast(struct gspca_dev *gspca_dev) 496static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
577{ 497{
578 struct sd *sd = (struct sd *) gspca_dev;
579
580 spca506_Initi2c(gspca_dev); 498 spca506_Initi2c(gspca_dev);
581 spca506_WriteI2c(gspca_dev, sd->contrast, SAA7113_contrast); 499 spca506_WriteI2c(gspca_dev, val, SAA7113_contrast);
582 spca506_WriteI2c(gspca_dev, 0x01, 0x09); 500 spca506_WriteI2c(gspca_dev, 0x01, 0x09);
583} 501}
584 502
585static void setcolors(struct gspca_dev *gspca_dev) 503static void setcolors(struct gspca_dev *gspca_dev, s32 val)
586{ 504{
587 struct sd *sd = (struct sd *) gspca_dev;
588
589 spca506_Initi2c(gspca_dev); 505 spca506_Initi2c(gspca_dev);
590 spca506_WriteI2c(gspca_dev, sd->colors, SAA7113_saturation); 506 spca506_WriteI2c(gspca_dev, val, SAA7113_saturation);
591 spca506_WriteI2c(gspca_dev, 0x01, 0x09); 507 spca506_WriteI2c(gspca_dev, 0x01, 0x09);
592} 508}
593 509
594static void sethue(struct gspca_dev *gspca_dev) 510static void sethue(struct gspca_dev *gspca_dev, s32 val)
595{ 511{
596 struct sd *sd = (struct sd *) gspca_dev;
597
598 spca506_Initi2c(gspca_dev); 512 spca506_Initi2c(gspca_dev);
599 spca506_WriteI2c(gspca_dev, sd->hue, SAA7113_hue); 513 spca506_WriteI2c(gspca_dev, val, SAA7113_hue);
600 spca506_WriteI2c(gspca_dev, 0x01, 0x09); 514 spca506_WriteI2c(gspca_dev, 0x01, 0x09);
601} 515}
602 516
603static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 517static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
604{
605 struct sd *sd = (struct sd *) gspca_dev;
606
607 sd->brightness = val;
608 if (gspca_dev->streaming)
609 setbrightness(gspca_dev);
610 return 0;
611}
612
613static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
614{
615 struct sd *sd = (struct sd *) gspca_dev;
616
617 *val = sd->brightness;
618 return 0;
619}
620
621static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
622{ 518{
623 struct sd *sd = (struct sd *) gspca_dev; 519 struct gspca_dev *gspca_dev =
520 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
624 521
625 sd->contrast = val; 522 gspca_dev->usb_err = 0;
626 if (gspca_dev->streaming)
627 setcontrast(gspca_dev);
628 return 0;
629}
630 523
631static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val) 524 if (!gspca_dev->streaming)
632{ 525 return 0;
633 struct sd *sd = (struct sd *) gspca_dev;
634 526
635 *val = sd->contrast; 527 switch (ctrl->id) {
636 return 0; 528 case V4L2_CID_BRIGHTNESS:
637} 529 setbrightness(gspca_dev, ctrl->val);
638 530 break;
639static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val) 531 case V4L2_CID_CONTRAST:
640{ 532 setcontrast(gspca_dev, ctrl->val);
641 struct sd *sd = (struct sd *) gspca_dev; 533 break;
642 534 case V4L2_CID_SATURATION:
643 sd->colors = val; 535 setcolors(gspca_dev, ctrl->val);
644 if (gspca_dev->streaming) 536 break;
645 setcolors(gspca_dev); 537 case V4L2_CID_HUE:
646 return 0; 538 sethue(gspca_dev, ctrl->val);
647} 539 break;
648 540 }
649static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val) 541 return gspca_dev->usb_err;
650{
651 struct sd *sd = (struct sd *) gspca_dev;
652
653 *val = sd->colors;
654 return 0;
655} 542}
656 543
657static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val) 544static const struct v4l2_ctrl_ops sd_ctrl_ops = {
658{ 545 .s_ctrl = sd_s_ctrl,
659 struct sd *sd = (struct sd *) gspca_dev; 546};
660
661 sd->hue = val;
662 if (gspca_dev->streaming)
663 sethue(gspca_dev);
664 return 0;
665}
666 547
667static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val) 548static int sd_init_controls(struct gspca_dev *gspca_dev)
668{ 549{
669 struct sd *sd = (struct sd *) gspca_dev; 550 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
670 551
671 *val = sd->hue; 552 gspca_dev->vdev.ctrl_handler = hdl;
553 v4l2_ctrl_handler_init(hdl, 4);
554 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
555 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
556 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
557 V4L2_CID_CONTRAST, 0, 255, 1, 0x47);
558 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
559 V4L2_CID_SATURATION, 0, 255, 1, 0x40);
560 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
561 V4L2_CID_HUE, 0, 255, 1, 0);
562
563 if (hdl->error) {
564 pr_err("Could not initialize controls\n");
565 return hdl->error;
566 }
672 return 0; 567 return 0;
673} 568}
674 569
675/* sub-driver description */ 570/* sub-driver description */
676static const struct sd_desc sd_desc = { 571static const struct sd_desc sd_desc = {
677 .name = MODULE_NAME, 572 .name = MODULE_NAME,
678 .ctrls = sd_ctrls,
679 .nctrls = ARRAY_SIZE(sd_ctrls),
680 .config = sd_config, 573 .config = sd_config,
681 .init = sd_init, 574 .init = sd_init,
575 .init_controls = sd_init_controls,
682 .start = sd_start, 576 .start = sd_start,
683 .stopN = sd_stopN, 577 .stopN = sd_stopN,
684 .pkt_scan = sd_pkt_scan, 578 .pkt_scan = sd_pkt_scan,
@@ -711,6 +605,7 @@ static struct usb_driver sd_driver = {
711#ifdef CONFIG_PM 605#ifdef CONFIG_PM
712 .suspend = gspca_suspend, 606 .suspend = gspca_suspend,
713 .resume = gspca_resume, 607 .resume = gspca_resume,
608 .reset_resume = gspca_resume,
714#endif 609#endif
715}; 610};
716 611
diff --git a/drivers/media/video/gspca/spca508.c b/drivers/media/video/gspca/spca508.c
index df4e16996461..1286b4170b88 100644
--- a/drivers/media/video/gspca/spca508.c
+++ b/drivers/media/video/gspca/spca508.c
@@ -32,8 +32,6 @@ MODULE_LICENSE("GPL");
32struct sd { 32struct sd {
33 struct gspca_dev gspca_dev; /* !! must be the first item */ 33 struct gspca_dev gspca_dev; /* !! must be the first item */
34 34
35 u8 brightness;
36
37 u8 subtype; 35 u8 subtype;
38#define CreativeVista 0 36#define CreativeVista 0
39#define HamaUSBSightcam 1 37#define HamaUSBSightcam 1
@@ -43,27 +41,6 @@ struct sd {
43#define ViewQuestVQ110 5 41#define ViewQuestVQ110 5
44}; 42};
45 43
46/* V4L2 controls supported by the driver */
47static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
48static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
49
50static const struct ctrl sd_ctrls[] = {
51 {
52 {
53 .id = V4L2_CID_BRIGHTNESS,
54 .type = V4L2_CTRL_TYPE_INTEGER,
55 .name = "Brightness",
56 .minimum = 0,
57 .maximum = 255,
58 .step = 1,
59#define BRIGHTNESS_DEF 128
60 .default_value = BRIGHTNESS_DEF,
61 },
62 .set = sd_setbrightness,
63 .get = sd_getbrightness,
64 },
65};
66
67static const struct v4l2_pix_format sif_mode[] = { 44static const struct v4l2_pix_format sif_mode[] = {
68 {160, 120, V4L2_PIX_FMT_SPCA508, V4L2_FIELD_NONE, 45 {160, 120, V4L2_PIX_FMT_SPCA508, V4L2_FIELD_NONE,
69 .bytesperline = 160, 46 .bytesperline = 160,
@@ -1411,7 +1388,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
1411 cam->nmodes = ARRAY_SIZE(sif_mode); 1388 cam->nmodes = ARRAY_SIZE(sif_mode);
1412 1389
1413 sd->subtype = id->driver_info; 1390 sd->subtype = id->driver_info;
1414 sd->brightness = BRIGHTNESS_DEF;
1415 1391
1416 init_data = init_data_tb[sd->subtype]; 1392 init_data = init_data_tb[sd->subtype];
1417 return write_vector(gspca_dev, init_data); 1393 return write_vector(gspca_dev, init_data);
@@ -1471,11 +1447,8 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1471 } 1447 }
1472} 1448}
1473 1449
1474static void setbrightness(struct gspca_dev *gspca_dev) 1450static void setbrightness(struct gspca_dev *gspca_dev, s32 brightness)
1475{ 1451{
1476 struct sd *sd = (struct sd *) gspca_dev;
1477 u8 brightness = sd->brightness;
1478
1479 /* MX seem contrast */ 1452 /* MX seem contrast */
1480 reg_write(gspca_dev->dev, 0x8651, brightness); 1453 reg_write(gspca_dev->dev, 0x8651, brightness);
1481 reg_write(gspca_dev->dev, 0x8652, brightness); 1454 reg_write(gspca_dev->dev, 0x8652, brightness);
@@ -1483,31 +1456,50 @@ static void setbrightness(struct gspca_dev *gspca_dev)
1483 reg_write(gspca_dev->dev, 0x8654, brightness); 1456 reg_write(gspca_dev->dev, 0x8654, brightness);
1484} 1457}
1485 1458
1486static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 1459static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1487{ 1460{
1488 struct sd *sd = (struct sd *) gspca_dev; 1461 struct gspca_dev *gspca_dev =
1462 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1489 1463
1490 sd->brightness = val; 1464 gspca_dev->usb_err = 0;
1491 if (gspca_dev->streaming) 1465
1492 setbrightness(gspca_dev); 1466 if (!gspca_dev->streaming)
1493 return 0; 1467 return 0;
1468
1469 switch (ctrl->id) {
1470 case V4L2_CID_BRIGHTNESS:
1471 setbrightness(gspca_dev, ctrl->val);
1472 break;
1473 }
1474 return gspca_dev->usb_err;
1494} 1475}
1495 1476
1496static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val) 1477static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1478 .s_ctrl = sd_s_ctrl,
1479};
1480
1481static int sd_init_controls(struct gspca_dev *gspca_dev)
1497{ 1482{
1498 struct sd *sd = (struct sd *) gspca_dev; 1483 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1499 1484
1500 *val = sd->brightness; 1485 gspca_dev->vdev.ctrl_handler = hdl;
1486 v4l2_ctrl_handler_init(hdl, 5);
1487 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1488 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1489
1490 if (hdl->error) {
1491 pr_err("Could not initialize controls\n");
1492 return hdl->error;
1493 }
1501 return 0; 1494 return 0;
1502} 1495}
1503 1496
1504/* sub-driver description */ 1497/* sub-driver description */
1505static const struct sd_desc sd_desc = { 1498static const struct sd_desc sd_desc = {
1506 .name = MODULE_NAME, 1499 .name = MODULE_NAME,
1507 .ctrls = sd_ctrls,
1508 .nctrls = ARRAY_SIZE(sd_ctrls),
1509 .config = sd_config, 1500 .config = sd_config,
1510 .init = sd_init, 1501 .init = sd_init,
1502 .init_controls = sd_init_controls,
1511 .start = sd_start, 1503 .start = sd_start,
1512 .stopN = sd_stopN, 1504 .stopN = sd_stopN,
1513 .pkt_scan = sd_pkt_scan, 1505 .pkt_scan = sd_pkt_scan,
@@ -1541,6 +1533,7 @@ static struct usb_driver sd_driver = {
1541#ifdef CONFIG_PM 1533#ifdef CONFIG_PM
1542 .suspend = gspca_suspend, 1534 .suspend = gspca_suspend,
1543 .resume = gspca_resume, 1535 .resume = gspca_resume,
1536 .reset_resume = gspca_resume,
1544#endif 1537#endif
1545}; 1538};
1546 1539
diff --git a/drivers/media/video/gspca/spca561.c b/drivers/media/video/gspca/spca561.c
index 4a5f209ce719..cfe71dd6747d 100644
--- a/drivers/media/video/gspca/spca561.c
+++ b/drivers/media/video/gspca/spca561.c
@@ -31,39 +31,17 @@ MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
31MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver"); 31MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
32MODULE_LICENSE("GPL"); 32MODULE_LICENSE("GPL");
33 33
34#define EXPOSURE_MAX (2047 + 325)
35
34/* specific webcam descriptor */ 36/* specific webcam descriptor */
35struct sd { 37struct sd {
36 struct gspca_dev gspca_dev; /* !! must be the first item */ 38 struct gspca_dev gspca_dev; /* !! must be the first item */
37 39
38 __u16 exposure; /* rev12a only */ 40 struct { /* hue/contrast control cluster */
39#define EXPOSURE_MIN 1 41 struct v4l2_ctrl *contrast;
40#define EXPOSURE_DEF 700 /* == 10 fps */ 42 struct v4l2_ctrl *hue;
41#define EXPOSURE_MAX (2047 + 325) /* see setexposure */ 43 };
42 44 struct v4l2_ctrl *autogain;
43 __u8 contrast; /* rev72a only */
44#define CONTRAST_MIN 0x00
45#define CONTRAST_DEF 0x20
46#define CONTRAST_MAX 0x3f
47
48 __u8 brightness; /* rev72a only */
49#define BRIGHTNESS_MIN 0
50#define BRIGHTNESS_DEF 0x20
51#define BRIGHTNESS_MAX 0x3f
52
53 __u8 white;
54#define HUE_MIN 1
55#define HUE_DEF 0x40
56#define HUE_MAX 0x7f
57
58 __u8 autogain;
59#define AUTOGAIN_MIN 0
60#define AUTOGAIN_DEF 1
61#define AUTOGAIN_MAX 1
62
63 __u8 gain; /* rev12a only */
64#define GAIN_MIN 0
65#define GAIN_DEF 63
66#define GAIN_MAX 255
67 45
68#define EXPO12A_DEF 3 46#define EXPO12A_DEF 3
69 __u8 expo12a; /* expo/gain? for rev 12a */ 47 __u8 expo12a; /* expo/gain? for rev 12a */
@@ -461,12 +439,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
461 cam->cam_mode = sif_072a_mode; 439 cam->cam_mode = sif_072a_mode;
462 cam->nmodes = ARRAY_SIZE(sif_072a_mode); 440 cam->nmodes = ARRAY_SIZE(sif_072a_mode);
463 } 441 }
464 sd->brightness = BRIGHTNESS_DEF;
465 sd->contrast = CONTRAST_DEF;
466 sd->white = HUE_DEF;
467 sd->exposure = EXPOSURE_DEF;
468 sd->autogain = AUTOGAIN_DEF;
469 sd->gain = GAIN_DEF;
470 sd->expo12a = EXPO12A_DEF; 442 sd->expo12a = EXPO12A_DEF;
471 return 0; 443 return 0;
472} 444}
@@ -491,66 +463,49 @@ static int sd_init_72a(struct gspca_dev *gspca_dev)
491 return 0; 463 return 0;
492} 464}
493 465
494/* rev 72a only */ 466static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
495static void setbrightness(struct gspca_dev *gspca_dev)
496{ 467{
497 struct sd *sd = (struct sd *) gspca_dev; 468 struct sd *sd = (struct sd *) gspca_dev;
498 struct usb_device *dev = gspca_dev->dev; 469 struct usb_device *dev = gspca_dev->dev;
499 __u8 value; 470 __u16 reg;
500 471
501 value = sd->brightness; 472 if (sd->chip_revision == Rev012A)
473 reg = 0x8610;
474 else
475 reg = 0x8611;
502 476
503 /* offsets for white balance */ 477 reg_w_val(dev, reg + 0, val); /* R */
504 reg_w_val(dev, 0x8611, value); /* R */ 478 reg_w_val(dev, reg + 1, val); /* Gr */
505 reg_w_val(dev, 0x8612, value); /* Gr */ 479 reg_w_val(dev, reg + 2, val); /* B */
506 reg_w_val(dev, 0x8613, value); /* B */ 480 reg_w_val(dev, reg + 3, val); /* Gb */
507 reg_w_val(dev, 0x8614, value); /* Gb */
508} 481}
509 482
510static void setwhite(struct gspca_dev *gspca_dev) 483static void setwhite(struct gspca_dev *gspca_dev, s32 white, s32 contrast)
511{ 484{
512 struct sd *sd = (struct sd *) gspca_dev; 485 struct sd *sd = (struct sd *) gspca_dev;
513 __u16 white; 486 struct usb_device *dev = gspca_dev->dev;
514 __u8 blue, red; 487 __u8 blue, red;
515 __u16 reg; 488 __u16 reg;
516 489
517 /* try to emulate MS-win as possible */ 490 /* try to emulate MS-win as possible */
518 white = sd->white;
519 red = 0x20 + white * 3 / 8; 491 red = 0x20 + white * 3 / 8;
520 blue = 0x90 - white * 5 / 8; 492 blue = 0x90 - white * 5 / 8;
521 if (sd->chip_revision == Rev012A) { 493 if (sd->chip_revision == Rev012A) {
522 reg = 0x8614; 494 reg = 0x8614;
523 } else { 495 } else {
524 reg = 0x8651; 496 reg = 0x8651;
525 red += sd->contrast - 0x20; 497 red += contrast - 0x20;
526 blue += sd->contrast - 0x20; 498 blue += contrast - 0x20;
499 reg_w_val(dev, 0x8652, contrast + 0x20); /* Gr */
500 reg_w_val(dev, 0x8654, contrast + 0x20); /* Gb */
527 } 501 }
528 reg_w_val(gspca_dev->dev, reg, red); 502 reg_w_val(dev, reg, red);
529 reg_w_val(gspca_dev->dev, reg + 2, blue); 503 reg_w_val(dev, reg + 2, blue);
530}
531
532static void setcontrast(struct gspca_dev *gspca_dev)
533{
534 struct sd *sd = (struct sd *) gspca_dev;
535 struct usb_device *dev = gspca_dev->dev;
536 __u8 value;
537
538 if (sd->chip_revision != Rev072A)
539 return;
540 value = sd->contrast + 0x20;
541
542 /* gains for white balance */
543 setwhite(gspca_dev);
544/* reg_w_val(dev, 0x8651, value); * R - done by setwhite */
545 reg_w_val(dev, 0x8652, value); /* Gr */
546/* reg_w_val(dev, 0x8653, value); * B - done by setwhite */
547 reg_w_val(dev, 0x8654, value); /* Gb */
548} 504}
549 505
550/* rev 12a only */ 506/* rev 12a only */
551static void setexposure(struct gspca_dev *gspca_dev) 507static void setexposure(struct gspca_dev *gspca_dev, s32 val)
552{ 508{
553 struct sd *sd = (struct sd *) gspca_dev;
554 int i, expo = 0; 509 int i, expo = 0;
555 510
556 /* Register 0x8309 controls exposure for the spca561, 511 /* Register 0x8309 controls exposure for the spca561,
@@ -572,8 +527,8 @@ static void setexposure(struct gspca_dev *gspca_dev)
572 int table[] = { 0, 450, 550, 625, EXPOSURE_MAX }; 527 int table[] = { 0, 450, 550, 625, EXPOSURE_MAX };
573 528
574 for (i = 0; i < ARRAY_SIZE(table) - 1; i++) { 529 for (i = 0; i < ARRAY_SIZE(table) - 1; i++) {
575 if (sd->exposure <= table[i + 1]) { 530 if (val <= table[i + 1]) {
576 expo = sd->exposure - table[i]; 531 expo = val - table[i];
577 if (i) 532 if (i)
578 expo += 300; 533 expo += 300;
579 expo |= i << 11; 534 expo |= i << 11;
@@ -587,29 +542,27 @@ static void setexposure(struct gspca_dev *gspca_dev)
587} 542}
588 543
589/* rev 12a only */ 544/* rev 12a only */
590static void setgain(struct gspca_dev *gspca_dev) 545static void setgain(struct gspca_dev *gspca_dev, s32 val)
591{ 546{
592 struct sd *sd = (struct sd *) gspca_dev;
593
594 /* gain reg low 6 bits 0-63 gain, bit 6 and 7, both double the 547 /* gain reg low 6 bits 0-63 gain, bit 6 and 7, both double the
595 sensitivity when set, so 31 + one of them set == 63, and 15 548 sensitivity when set, so 31 + one of them set == 63, and 15
596 with both of them set == 63 */ 549 with both of them set == 63 */
597 if (sd->gain < 64) 550 if (val < 64)
598 gspca_dev->usb_buf[0] = sd->gain; 551 gspca_dev->usb_buf[0] = val;
599 else if (sd->gain < 128) 552 else if (val < 128)
600 gspca_dev->usb_buf[0] = (sd->gain / 2) | 0x40; 553 gspca_dev->usb_buf[0] = (val / 2) | 0x40;
601 else 554 else
602 gspca_dev->usb_buf[0] = (sd->gain / 4) | 0xc0; 555 gspca_dev->usb_buf[0] = (val / 4) | 0xc0;
603 556
604 gspca_dev->usb_buf[1] = 0; 557 gspca_dev->usb_buf[1] = 0;
605 reg_w_buf(gspca_dev, 0x8335, 2); 558 reg_w_buf(gspca_dev, 0x8335, 2);
606} 559}
607 560
608static void setautogain(struct gspca_dev *gspca_dev) 561static void setautogain(struct gspca_dev *gspca_dev, s32 val)
609{ 562{
610 struct sd *sd = (struct sd *) gspca_dev; 563 struct sd *sd = (struct sd *) gspca_dev;
611 564
612 if (sd->autogain) 565 if (val)
613 sd->ag_cnt = AG_CNT_START; 566 sd->ag_cnt = AG_CNT_START;
614 else 567 else
615 sd->ag_cnt = -1; 568 sd->ag_cnt = -1;
@@ -644,9 +597,6 @@ static int sd_start_12a(struct gspca_dev *gspca_dev)
644 memcpy(gspca_dev->usb_buf, Reg8391, 8); 597 memcpy(gspca_dev->usb_buf, Reg8391, 8);
645 reg_w_buf(gspca_dev, 0x8391, 8); 598 reg_w_buf(gspca_dev, 0x8391, 8);
646 reg_w_buf(gspca_dev, 0x8390, 8); 599 reg_w_buf(gspca_dev, 0x8390, 8);
647 setwhite(gspca_dev);
648 setgain(gspca_dev);
649 setexposure(gspca_dev);
650 600
651 /* Led ON (bit 3 -> 0 */ 601 /* Led ON (bit 3 -> 0 */
652 reg_w_val(gspca_dev->dev, 0x8114, 0x00); 602 reg_w_val(gspca_dev->dev, 0x8114, 0x00);
@@ -654,6 +604,7 @@ static int sd_start_12a(struct gspca_dev *gspca_dev)
654} 604}
655static int sd_start_72a(struct gspca_dev *gspca_dev) 605static int sd_start_72a(struct gspca_dev *gspca_dev)
656{ 606{
607 struct sd *sd = (struct sd *) gspca_dev;
657 struct usb_device *dev = gspca_dev->dev; 608 struct usb_device *dev = gspca_dev->dev;
658 int Clck; 609 int Clck;
659 int mode; 610 int mode;
@@ -683,9 +634,10 @@ static int sd_start_72a(struct gspca_dev *gspca_dev)
683 reg_w_val(dev, 0x8702, 0x81); 634 reg_w_val(dev, 0x8702, 0x81);
684 reg_w_val(dev, 0x8500, mode); /* mode */ 635 reg_w_val(dev, 0x8500, mode); /* mode */
685 write_sensor_72a(gspca_dev, rev72a_init_sensor2); 636 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
686 setcontrast(gspca_dev); 637 setwhite(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue),
638 v4l2_ctrl_g_ctrl(sd->contrast));
687/* setbrightness(gspca_dev); * fixme: bad values */ 639/* setbrightness(gspca_dev); * fixme: bad values */
688 setautogain(gspca_dev); 640 setautogain(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
689 reg_w_val(dev, 0x8112, 0x10 | 0x20); 641 reg_w_val(dev, 0x8112, 0x10 | 0x20);
690 return 0; 642 return 0;
691} 643}
@@ -819,221 +771,96 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
819 gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 771 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
820} 772}
821 773
822/* rev 72a only */ 774static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
823static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
824{ 775{
825 struct sd *sd = (struct sd *) gspca_dev; 776 struct gspca_dev *gspca_dev =
777 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
778 struct sd *sd = (struct sd *)gspca_dev;
826 779
827 sd->brightness = val; 780 gspca_dev->usb_err = 0;
828 if (gspca_dev->streaming)
829 setbrightness(gspca_dev);
830 return 0;
831}
832
833static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
834{
835 struct sd *sd = (struct sd *) gspca_dev;
836
837 *val = sd->brightness;
838 return 0;
839}
840
841/* rev 72a only */
842static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
843{
844 struct sd *sd = (struct sd *) gspca_dev;
845 781
846 sd->contrast = val; 782 if (!gspca_dev->streaming)
847 if (gspca_dev->streaming) 783 return 0;
848 setcontrast(gspca_dev);
849 return 0;
850}
851
852static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
853{
854 struct sd *sd = (struct sd *) gspca_dev;
855
856 *val = sd->contrast;
857 return 0;
858}
859
860static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
861{
862 struct sd *sd = (struct sd *) gspca_dev;
863 784
864 sd->autogain = val; 785 switch (ctrl->id) {
865 if (gspca_dev->streaming) 786 case V4L2_CID_BRIGHTNESS:
866 setautogain(gspca_dev); 787 setbrightness(gspca_dev, ctrl->val);
867 return 0; 788 break;
868} 789 case V4L2_CID_CONTRAST:
869 790 /* hue/contrast control cluster for 72a */
870static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val) 791 setwhite(gspca_dev, sd->hue->val, ctrl->val);
871{ 792 break;
872 struct sd *sd = (struct sd *) gspca_dev; 793 case V4L2_CID_HUE:
873 794 /* just plain hue control for 12a */
874 *val = sd->autogain; 795 setwhite(gspca_dev, ctrl->val, 0);
875 return 0; 796 break;
876} 797 case V4L2_CID_EXPOSURE:
877 798 setexposure(gspca_dev, ctrl->val);
878static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val) 799 break;
879{ 800 case V4L2_CID_GAIN:
880 struct sd *sd = (struct sd *) gspca_dev; 801 setgain(gspca_dev, ctrl->val);
881 802 break;
882 sd->white = val; 803 case V4L2_CID_AUTOGAIN:
883 if (gspca_dev->streaming) 804 setautogain(gspca_dev, ctrl->val);
884 setwhite(gspca_dev); 805 break;
885 return 0; 806 }
807 return gspca_dev->usb_err;
886} 808}
887 809
888static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val) 810static const struct v4l2_ctrl_ops sd_ctrl_ops = {
889{ 811 .s_ctrl = sd_s_ctrl,
890 struct sd *sd = (struct sd *) gspca_dev; 812};
891
892 *val = sd->white;
893 return 0;
894}
895 813
896/* rev12a only */ 814static int sd_init_controls_12a(struct gspca_dev *gspca_dev)
897static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
898{ 815{
899 struct sd *sd = (struct sd *) gspca_dev; 816 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
900 817
901 sd->exposure = val; 818 gspca_dev->vdev.ctrl_handler = hdl;
902 if (gspca_dev->streaming) 819 v4l2_ctrl_handler_init(hdl, 3);
903 setexposure(gspca_dev); 820 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
904 return 0; 821 V4L2_CID_HUE, 1, 0x7f, 1, 0x40);
905} 822 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
823 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
824 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
825 V4L2_CID_EXPOSURE, 1, EXPOSURE_MAX, 1, 700);
826 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
827 V4L2_CID_GAIN, 0, 255, 1, 63);
906 828
907static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val) 829 if (hdl->error) {
908{ 830 pr_err("Could not initialize controls\n");
909 struct sd *sd = (struct sd *) gspca_dev; 831 return hdl->error;
910 832 }
911 *val = sd->exposure;
912 return 0; 833 return 0;
913} 834}
914 835
915/* rev12a only */ 836static int sd_init_controls_72a(struct gspca_dev *gspca_dev)
916static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
917{ 837{
918 struct sd *sd = (struct sd *) gspca_dev; 838 struct sd *sd = (struct sd *)gspca_dev;
919 839 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
920 sd->gain = val;
921 if (gspca_dev->streaming)
922 setgain(gspca_dev);
923 return 0;
924}
925 840
926static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val) 841 gspca_dev->vdev.ctrl_handler = hdl;
927{ 842 v4l2_ctrl_handler_init(hdl, 4);
928 struct sd *sd = (struct sd *) gspca_dev; 843 sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
844 V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x20);
845 sd->hue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
846 V4L2_CID_HUE, 1, 0x7f, 1, 0x40);
847 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
848 V4L2_CID_BRIGHTNESS, 0, 0x3f, 1, 0x20);
849 sd->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
850 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
929 851
930 *val = sd->gain; 852 if (hdl->error) {
853 pr_err("Could not initialize controls\n");
854 return hdl->error;
855 }
856 v4l2_ctrl_cluster(2, &sd->contrast);
931 return 0; 857 return 0;
932} 858}
933 859
934/* control tables */
935static const struct ctrl sd_ctrls_12a[] = {
936 {
937 {
938 .id = V4L2_CID_HUE,
939 .type = V4L2_CTRL_TYPE_INTEGER,
940 .name = "Hue",
941 .minimum = HUE_MIN,
942 .maximum = HUE_MAX,
943 .step = 1,
944 .default_value = HUE_DEF,
945 },
946 .set = sd_setwhite,
947 .get = sd_getwhite,
948 },
949 {
950 {
951 .id = V4L2_CID_EXPOSURE,
952 .type = V4L2_CTRL_TYPE_INTEGER,
953 .name = "Exposure",
954 .minimum = EXPOSURE_MIN,
955 .maximum = EXPOSURE_MAX,
956 .step = 1,
957 .default_value = EXPOSURE_DEF,
958 },
959 .set = sd_setexposure,
960 .get = sd_getexposure,
961 },
962 {
963 {
964 .id = V4L2_CID_GAIN,
965 .type = V4L2_CTRL_TYPE_INTEGER,
966 .name = "Gain",
967 .minimum = GAIN_MIN,
968 .maximum = GAIN_MAX,
969 .step = 1,
970 .default_value = GAIN_DEF,
971 },
972 .set = sd_setgain,
973 .get = sd_getgain,
974 },
975};
976
977static const struct ctrl sd_ctrls_72a[] = {
978 {
979 {
980 .id = V4L2_CID_HUE,
981 .type = V4L2_CTRL_TYPE_INTEGER,
982 .name = "Hue",
983 .minimum = HUE_MIN,
984 .maximum = HUE_MAX,
985 .step = 1,
986 .default_value = HUE_DEF,
987 },
988 .set = sd_setwhite,
989 .get = sd_getwhite,
990 },
991 {
992 {
993 .id = V4L2_CID_BRIGHTNESS,
994 .type = V4L2_CTRL_TYPE_INTEGER,
995 .name = "Brightness",
996 .minimum = BRIGHTNESS_MIN,
997 .maximum = BRIGHTNESS_MAX,
998 .step = 1,
999 .default_value = BRIGHTNESS_DEF,
1000 },
1001 .set = sd_setbrightness,
1002 .get = sd_getbrightness,
1003 },
1004 {
1005 {
1006 .id = V4L2_CID_CONTRAST,
1007 .type = V4L2_CTRL_TYPE_INTEGER,
1008 .name = "Contrast",
1009 .minimum = CONTRAST_MIN,
1010 .maximum = CONTRAST_MAX,
1011 .step = 1,
1012 .default_value = CONTRAST_DEF,
1013 },
1014 .set = sd_setcontrast,
1015 .get = sd_getcontrast,
1016 },
1017 {
1018 {
1019 .id = V4L2_CID_AUTOGAIN,
1020 .type = V4L2_CTRL_TYPE_BOOLEAN,
1021 .name = "Auto Gain",
1022 .minimum = AUTOGAIN_MIN,
1023 .maximum = AUTOGAIN_MAX,
1024 .step = 1,
1025 .default_value = AUTOGAIN_DEF,
1026 },
1027 .set = sd_setautogain,
1028 .get = sd_getautogain,
1029 },
1030};
1031
1032/* sub-driver description */ 860/* sub-driver description */
1033static const struct sd_desc sd_desc_12a = { 861static const struct sd_desc sd_desc_12a = {
1034 .name = MODULE_NAME, 862 .name = MODULE_NAME,
1035 .ctrls = sd_ctrls_12a, 863 .init_controls = sd_init_controls_12a,
1036 .nctrls = ARRAY_SIZE(sd_ctrls_12a),
1037 .config = sd_config, 864 .config = sd_config,
1038 .init = sd_init_12a, 865 .init = sd_init_12a,
1039 .start = sd_start_12a, 866 .start = sd_start_12a,
@@ -1045,8 +872,7 @@ static const struct sd_desc sd_desc_12a = {
1045}; 872};
1046static const struct sd_desc sd_desc_72a = { 873static const struct sd_desc sd_desc_72a = {
1047 .name = MODULE_NAME, 874 .name = MODULE_NAME,
1048 .ctrls = sd_ctrls_72a, 875 .init_controls = sd_init_controls_72a,
1049 .nctrls = ARRAY_SIZE(sd_ctrls_72a),
1050 .config = sd_config, 876 .config = sd_config,
1051 .init = sd_init_72a, 877 .init = sd_init_72a,
1052 .start = sd_start_72a, 878 .start = sd_start_72a,
@@ -1103,6 +929,7 @@ static struct usb_driver sd_driver = {
1103#ifdef CONFIG_PM 929#ifdef CONFIG_PM
1104 .suspend = gspca_suspend, 930 .suspend = gspca_suspend,
1105 .resume = gspca_resume, 931 .resume = gspca_resume,
932 .reset_resume = gspca_resume,
1106#endif 933#endif
1107}; 934};
1108 935
diff --git a/drivers/media/video/gspca/sq905.c b/drivers/media/video/gspca/sq905.c
index 04f54654a026..a8ac97931ad6 100644
--- a/drivers/media/video/gspca/sq905.c
+++ b/drivers/media/video/gspca/sq905.c
@@ -433,6 +433,7 @@ static struct usb_driver sd_driver = {
433#ifdef CONFIG_PM 433#ifdef CONFIG_PM
434 .suspend = gspca_suspend, 434 .suspend = gspca_suspend,
435 .resume = gspca_resume, 435 .resume = gspca_resume,
436 .reset_resume = gspca_resume,
436#endif 437#endif
437}; 438};
438 439
diff --git a/drivers/media/video/gspca/sq905c.c b/drivers/media/video/gspca/sq905c.c
index f34ddb0570c8..2c2f3d2f357f 100644
--- a/drivers/media/video/gspca/sq905c.c
+++ b/drivers/media/video/gspca/sq905c.c
@@ -340,6 +340,7 @@ static struct usb_driver sd_driver = {
340#ifdef CONFIG_PM 340#ifdef CONFIG_PM
341 .suspend = gspca_suspend, 341 .suspend = gspca_suspend,
342 .resume = gspca_resume, 342 .resume = gspca_resume,
343 .reset_resume = gspca_resume,
343#endif 344#endif
344}; 345};
345 346
diff --git a/drivers/media/video/gspca/sq930x.c b/drivers/media/video/gspca/sq930x.c
index 1a8ba9b3550a..3e1e486af883 100644
--- a/drivers/media/video/gspca/sq930x.c
+++ b/drivers/media/video/gspca/sq930x.c
@@ -36,8 +36,10 @@ MODULE_LICENSE("GPL");
36struct sd { 36struct sd {
37 struct gspca_dev gspca_dev; /* !! must be the first item */ 37 struct gspca_dev gspca_dev; /* !! must be the first item */
38 38
39 u16 expo; 39 struct { /* exposure/gain control cluster */
40 u8 gain; 40 struct v4l2_ctrl *exposure;
41 struct v4l2_ctrl *gain;
42 };
41 43
42 u8 do_ctrl; 44 u8 do_ctrl;
43 u8 gpio[2]; 45 u8 gpio[2];
@@ -55,42 +57,6 @@ enum sensors {
55 SENSOR_OV9630, 57 SENSOR_OV9630,
56}; 58};
57 59
58static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val);
59static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val);
60static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
61static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
62
63static const struct ctrl sd_ctrls[] = {
64 {
65 {
66 .id = V4L2_CID_EXPOSURE,
67 .type = V4L2_CTRL_TYPE_INTEGER,
68 .name = "Exposure",
69 .minimum = 0x0001,
70 .maximum = 0x0fff,
71 .step = 1,
72#define EXPO_DEF 0x0356
73 .default_value = EXPO_DEF,
74 },
75 .set = sd_setexpo,
76 .get = sd_getexpo,
77 },
78 {
79 {
80 .id = V4L2_CID_GAIN,
81 .type = V4L2_CTRL_TYPE_INTEGER,
82 .name = "Gain",
83 .minimum = 0x01,
84 .maximum = 0xff,
85 .step = 1,
86#define GAIN_DEF 0x8d
87 .default_value = GAIN_DEF,
88 },
89 .set = sd_setgain,
90 .get = sd_getgain,
91 },
92};
93
94static struct v4l2_pix_format vga_mode[] = { 60static struct v4l2_pix_format vga_mode[] = {
95 {320, 240, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE, 61 {320, 240, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
96 .bytesperline = 320, 62 .bytesperline = 320,
@@ -791,7 +757,7 @@ static void lz24bp_ppl(struct sd *sd, u16 ppl)
791 ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2); 757 ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
792} 758}
793 759
794static void setexposure(struct gspca_dev *gspca_dev) 760static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 gain)
795{ 761{
796 struct sd *sd = (struct sd *) gspca_dev; 762 struct sd *sd = (struct sd *) gspca_dev;
797 int i, integclks, intstartclk, frameclks, min_frclk; 763 int i, integclks, intstartclk, frameclks, min_frclk;
@@ -799,7 +765,7 @@ static void setexposure(struct gspca_dev *gspca_dev)
799 u16 cmd; 765 u16 cmd;
800 u8 buf[15]; 766 u8 buf[15];
801 767
802 integclks = sd->expo; 768 integclks = expo;
803 i = 0; 769 i = 0;
804 cmd = SQ930_CTRL_SET_EXPOSURE; 770 cmd = SQ930_CTRL_SET_EXPOSURE;
805 771
@@ -818,7 +784,7 @@ static void setexposure(struct gspca_dev *gspca_dev)
818 buf[i++] = intstartclk; 784 buf[i++] = intstartclk;
819 buf[i++] = frameclks >> 8; 785 buf[i++] = frameclks >> 8;
820 buf[i++] = frameclks; 786 buf[i++] = frameclks;
821 buf[i++] = sd->gain; 787 buf[i++] = gain;
822 break; 788 break;
823 default: /* cmos */ 789 default: /* cmos */
824/* case SENSOR_MI0360: */ 790/* case SENSOR_MI0360: */
@@ -834,7 +800,7 @@ static void setexposure(struct gspca_dev *gspca_dev)
834 buf[i++] = 0x35; /* reg = global gain */ 800 buf[i++] = 0x35; /* reg = global gain */
835 buf[i++] = 0x00; /* val H */ 801 buf[i++] = 0x00; /* val H */
836 buf[i++] = sensor->i2c_dum; 802 buf[i++] = sensor->i2c_dum;
837 buf[i++] = 0x80 + sd->gain / 2; /* val L */ 803 buf[i++] = 0x80 + gain / 2; /* val L */
838 buf[i++] = 0x00; 804 buf[i++] = 0x00;
839 buf[i++] = 0x00; 805 buf[i++] = 0x00;
840 buf[i++] = 0x00; 806 buf[i++] = 0x00;
@@ -860,9 +826,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
860 826
861 cam->bulk = 1; 827 cam->bulk = 1;
862 828
863 sd->gain = GAIN_DEF;
864 sd->expo = EXPO_DEF;
865
866 return 0; 829 return 0;
867} 830}
868 831
@@ -1089,7 +1052,8 @@ static void sd_dq_callback(struct gspca_dev *gspca_dev)
1089 return; 1052 return;
1090 sd->do_ctrl = 0; 1053 sd->do_ctrl = 0;
1091 1054
1092 setexposure(gspca_dev); 1055 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure),
1056 v4l2_ctrl_g_ctrl(sd->gain));
1093 1057
1094 gspca_dev->cam.bulk_nurbs = 1; 1058 gspca_dev->cam.bulk_nurbs = 1;
1095 ret = usb_submit_urb(gspca_dev->urb[0], GFP_ATOMIC); 1059 ret = usb_submit_urb(gspca_dev->urb[0], GFP_ATOMIC);
@@ -1113,48 +1077,55 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1113 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0); 1077 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
1114} 1078}
1115 1079
1116static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val) 1080static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1117{ 1081{
1082 struct gspca_dev *gspca_dev =
1083 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1118 struct sd *sd = (struct sd *) gspca_dev; 1084 struct sd *sd = (struct sd *) gspca_dev;
1119 1085
1120 sd->gain = val; 1086 gspca_dev->usb_err = 0;
1121 if (gspca_dev->streaming)
1122 sd->do_ctrl = 1;
1123 return 0;
1124}
1125 1087
1126static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val) 1088 if (!gspca_dev->streaming)
1127{ 1089 return 0;
1128 struct sd *sd = (struct sd *) gspca_dev;
1129 1090
1130 *val = sd->gain; 1091 switch (ctrl->id) {
1131 return 0; 1092 case V4L2_CID_EXPOSURE:
1093 setexposure(gspca_dev, ctrl->val, sd->gain->val);
1094 break;
1095 }
1096 return gspca_dev->usb_err;
1132} 1097}
1133static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val)
1134{
1135 struct sd *sd = (struct sd *) gspca_dev;
1136 1098
1137 sd->expo = val; 1099static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1138 if (gspca_dev->streaming) 1100 .s_ctrl = sd_s_ctrl,
1139 sd->do_ctrl = 1; 1101};
1140 return 0;
1141}
1142 1102
1143static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val) 1103static int sd_init_controls(struct gspca_dev *gspca_dev)
1144{ 1104{
1105 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1145 struct sd *sd = (struct sd *) gspca_dev; 1106 struct sd *sd = (struct sd *) gspca_dev;
1146 1107
1147 *val = sd->expo; 1108 gspca_dev->vdev.ctrl_handler = hdl;
1109 v4l2_ctrl_handler_init(hdl, 2);
1110 sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1111 V4L2_CID_EXPOSURE, 1, 0xfff, 1, 0x356);
1112 sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1113 V4L2_CID_GAIN, 1, 255, 1, 0x8d);
1114
1115 if (hdl->error) {
1116 pr_err("Could not initialize controls\n");
1117 return hdl->error;
1118 }
1119 v4l2_ctrl_cluster(2, &sd->exposure);
1148 return 0; 1120 return 0;
1149} 1121}
1150 1122
1151/* sub-driver description */ 1123/* sub-driver description */
1152static const struct sd_desc sd_desc = { 1124static const struct sd_desc sd_desc = {
1153 .name = MODULE_NAME, 1125 .name = MODULE_NAME,
1154 .ctrls = sd_ctrls,
1155 .nctrls = ARRAY_SIZE(sd_ctrls),
1156 .config = sd_config, 1126 .config = sd_config,
1157 .init = sd_init, 1127 .init = sd_init,
1128 .init_controls = sd_init_controls,
1158 .isoc_init = sd_isoc_init, 1129 .isoc_init = sd_isoc_init,
1159 .start = sd_start, 1130 .start = sd_start,
1160 .stopN = sd_stopN, 1131 .stopN = sd_stopN,
@@ -1194,6 +1165,7 @@ static struct usb_driver sd_driver = {
1194#ifdef CONFIG_PM 1165#ifdef CONFIG_PM
1195 .suspend = gspca_suspend, 1166 .suspend = gspca_suspend,
1196 .resume = gspca_resume, 1167 .resume = gspca_resume,
1168 .reset_resume = gspca_resume,
1197#endif 1169#endif
1198}; 1170};
1199 1171
diff --git a/drivers/media/video/gspca/stk014.c b/drivers/media/video/gspca/stk014.c
index 4ae7cc8f463a..8c0982607f25 100644
--- a/drivers/media/video/gspca/stk014.c
+++ b/drivers/media/video/gspca/stk014.c
@@ -29,86 +29,14 @@ MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
29MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver"); 29MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
30MODULE_LICENSE("GPL"); 30MODULE_LICENSE("GPL");
31 31
32/* controls */ 32#define QUALITY 50
33enum e_ctrl {
34 BRIGHTNESS,
35 CONTRAST,
36 COLORS,
37 LIGHTFREQ,
38 NCTRLS /* number of controls */
39};
40 33
41/* specific webcam descriptor */ 34/* specific webcam descriptor */
42struct sd { 35struct sd {
43 struct gspca_dev gspca_dev; /* !! must be the first item */ 36 struct gspca_dev gspca_dev; /* !! must be the first item */
44
45 struct gspca_ctrl ctrls[NCTRLS];
46
47 u8 quality;
48#define QUALITY_MIN 70
49#define QUALITY_MAX 95
50#define QUALITY_DEF 88
51
52 u8 jpeg_hdr[JPEG_HDR_SZ]; 37 u8 jpeg_hdr[JPEG_HDR_SZ];
53}; 38};
54 39
55/* V4L2 controls supported by the driver */
56static void setbrightness(struct gspca_dev *gspca_dev);
57static void setcontrast(struct gspca_dev *gspca_dev);
58static void setcolors(struct gspca_dev *gspca_dev);
59static void setlightfreq(struct gspca_dev *gspca_dev);
60
61static const struct ctrl sd_ctrls[NCTRLS] = {
62[BRIGHTNESS] = {
63 {
64 .id = V4L2_CID_BRIGHTNESS,
65 .type = V4L2_CTRL_TYPE_INTEGER,
66 .name = "Brightness",
67 .minimum = 0,
68 .maximum = 255,
69 .step = 1,
70 .default_value = 127,
71 },
72 .set_control = setbrightness
73 },
74[CONTRAST] = {
75 {
76 .id = V4L2_CID_CONTRAST,
77 .type = V4L2_CTRL_TYPE_INTEGER,
78 .name = "Contrast",
79 .minimum = 0,
80 .maximum = 255,
81 .step = 1,
82 .default_value = 127,
83 },
84 .set_control = setcontrast
85 },
86[COLORS] = {
87 {
88 .id = V4L2_CID_SATURATION,
89 .type = V4L2_CTRL_TYPE_INTEGER,
90 .name = "Color",
91 .minimum = 0,
92 .maximum = 255,
93 .step = 1,
94 .default_value = 127,
95 },
96 .set_control = setcolors
97 },
98[LIGHTFREQ] = {
99 {
100 .id = V4L2_CID_POWER_LINE_FREQUENCY,
101 .type = V4L2_CTRL_TYPE_MENU,
102 .name = "Light frequency filter",
103 .minimum = 1,
104 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
105 .step = 1,
106 .default_value = 1,
107 },
108 .set_control = setlightfreq
109 },
110};
111
112static const struct v4l2_pix_format vga_mode[] = { 40static const struct v4l2_pix_format vga_mode[] = {
113 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 41 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
114 .bytesperline = 320, 42 .bytesperline = 320,
@@ -255,41 +183,36 @@ static void set_par(struct gspca_dev *gspca_dev,
255 snd_val(gspca_dev, 0x003f08, parval); 183 snd_val(gspca_dev, 0x003f08, parval);
256} 184}
257 185
258static void setbrightness(struct gspca_dev *gspca_dev) 186static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
259{ 187{
260 struct sd *sd = (struct sd *) gspca_dev;
261 int parval; 188 int parval;
262 189
263 parval = 0x06000000 /* whiteness */ 190 parval = 0x06000000 /* whiteness */
264 + (sd->ctrls[BRIGHTNESS].val << 16); 191 + (val << 16);
265 set_par(gspca_dev, parval); 192 set_par(gspca_dev, parval);
266} 193}
267 194
268static void setcontrast(struct gspca_dev *gspca_dev) 195static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
269{ 196{
270 struct sd *sd = (struct sd *) gspca_dev;
271 int parval; 197 int parval;
272 198
273 parval = 0x07000000 /* contrast */ 199 parval = 0x07000000 /* contrast */
274 + (sd->ctrls[CONTRAST].val << 16); 200 + (val << 16);
275 set_par(gspca_dev, parval); 201 set_par(gspca_dev, parval);
276} 202}
277 203
278static void setcolors(struct gspca_dev *gspca_dev) 204static void setcolors(struct gspca_dev *gspca_dev, s32 val)
279{ 205{
280 struct sd *sd = (struct sd *) gspca_dev;
281 int parval; 206 int parval;
282 207
283 parval = 0x08000000 /* saturation */ 208 parval = 0x08000000 /* saturation */
284 + (sd->ctrls[COLORS].val << 16); 209 + (val << 16);
285 set_par(gspca_dev, parval); 210 set_par(gspca_dev, parval);
286} 211}
287 212
288static void setlightfreq(struct gspca_dev *gspca_dev) 213static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
289{ 214{
290 struct sd *sd = (struct sd *) gspca_dev; 215 set_par(gspca_dev, val == 1
291
292 set_par(gspca_dev, sd->ctrls[LIGHTFREQ].val == 1
293 ? 0x33640000 /* 50 Hz */ 216 ? 0x33640000 /* 50 Hz */
294 : 0x33780000); /* 60 Hz */ 217 : 0x33780000); /* 60 Hz */
295} 218}
@@ -298,12 +221,8 @@ static void setlightfreq(struct gspca_dev *gspca_dev)
298static int sd_config(struct gspca_dev *gspca_dev, 221static int sd_config(struct gspca_dev *gspca_dev,
299 const struct usb_device_id *id) 222 const struct usb_device_id *id)
300{ 223{
301 struct sd *sd = (struct sd *) gspca_dev;
302
303 gspca_dev->cam.cam_mode = vga_mode; 224 gspca_dev->cam.cam_mode = vga_mode;
304 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode); 225 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
305 gspca_dev->cam.ctrls = sd->ctrls;
306 sd->quality = QUALITY_DEF;
307 return 0; 226 return 0;
308} 227}
309 228
@@ -333,7 +252,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
333 /* create the JPEG header */ 252 /* create the JPEG header */
334 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width, 253 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
335 0x22); /* JPEG 411 */ 254 0x22); /* JPEG 411 */
336 jpeg_set_qual(sd->jpeg_hdr, sd->quality); 255 jpeg_set_qual(sd->jpeg_hdr, QUALITY);
337 256
338 /* work on alternate 1 */ 257 /* work on alternate 1 */
339 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1); 258 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
@@ -365,14 +284,10 @@ static int sd_start(struct gspca_dev *gspca_dev)
365 reg_w(gspca_dev, 0x0640, 0); 284 reg_w(gspca_dev, 0x0640, 0);
366 reg_w(gspca_dev, 0x0650, 0); 285 reg_w(gspca_dev, 0x0650, 0);
367 reg_w(gspca_dev, 0x0660, 0); 286 reg_w(gspca_dev, 0x0660, 0);
368 setbrightness(gspca_dev); /* whiteness */
369 setcontrast(gspca_dev); /* contrast */
370 setcolors(gspca_dev); /* saturation */
371 set_par(gspca_dev, 0x09800000); /* Red ? */ 287 set_par(gspca_dev, 0x09800000); /* Red ? */
372 set_par(gspca_dev, 0x0a800000); /* Green ? */ 288 set_par(gspca_dev, 0x0a800000); /* Green ? */
373 set_par(gspca_dev, 0x0b800000); /* Blue ? */ 289 set_par(gspca_dev, 0x0b800000); /* Blue ? */
374 set_par(gspca_dev, 0x0d030000); /* Gamma ? */ 290 set_par(gspca_dev, 0x0d030000); /* Gamma ? */
375 setlightfreq(gspca_dev);
376 291
377 /* start the video flow */ 292 /* start the video flow */
378 set_par(gspca_dev, 0x01000000); 293 set_par(gspca_dev, 0x01000000);
@@ -435,62 +350,70 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
435 gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 350 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
436} 351}
437 352
438static int sd_querymenu(struct gspca_dev *gspca_dev, 353static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
439 struct v4l2_querymenu *menu)
440{ 354{
441 static const char *freq_nm[3] = {"NoFliker", "50 Hz", "60 Hz"}; 355 struct gspca_dev *gspca_dev =
356 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
442 357
443 switch (menu->id) { 358 gspca_dev->usb_err = 0;
444 case V4L2_CID_POWER_LINE_FREQUENCY:
445 if ((unsigned) menu->index >= ARRAY_SIZE(freq_nm))
446 break;
447 strcpy((char *) menu->name, freq_nm[menu->index]);
448 return 0;
449 }
450 return -EINVAL;
451}
452 359
453static int sd_set_jcomp(struct gspca_dev *gspca_dev, 360 if (!gspca_dev->streaming)
454 struct v4l2_jpegcompression *jcomp) 361 return 0;
455{
456 struct sd *sd = (struct sd *) gspca_dev;
457 362
458 if (jcomp->quality < QUALITY_MIN) 363 switch (ctrl->id) {
459 sd->quality = QUALITY_MIN; 364 case V4L2_CID_BRIGHTNESS:
460 else if (jcomp->quality > QUALITY_MAX) 365 setbrightness(gspca_dev, ctrl->val);
461 sd->quality = QUALITY_MAX; 366 break;
462 else 367 case V4L2_CID_CONTRAST:
463 sd->quality = jcomp->quality; 368 setcontrast(gspca_dev, ctrl->val);
464 if (gspca_dev->streaming) 369 break;
465 jpeg_set_qual(sd->jpeg_hdr, sd->quality); 370 case V4L2_CID_SATURATION:
371 setcolors(gspca_dev, ctrl->val);
372 break;
373 case V4L2_CID_POWER_LINE_FREQUENCY:
374 setlightfreq(gspca_dev, ctrl->val);
375 break;
376 }
466 return gspca_dev->usb_err; 377 return gspca_dev->usb_err;
467} 378}
468 379
469static int sd_get_jcomp(struct gspca_dev *gspca_dev, 380static const struct v4l2_ctrl_ops sd_ctrl_ops = {
470 struct v4l2_jpegcompression *jcomp) 381 .s_ctrl = sd_s_ctrl,
471{ 382};
472 struct sd *sd = (struct sd *) gspca_dev;
473 383
474 memset(jcomp, 0, sizeof *jcomp); 384static int sd_init_controls(struct gspca_dev *gspca_dev)
475 jcomp->quality = sd->quality; 385{
476 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT 386 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
477 | V4L2_JPEG_MARKER_DQT; 387
388 gspca_dev->vdev.ctrl_handler = hdl;
389 v4l2_ctrl_handler_init(hdl, 4);
390 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
391 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
392 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
393 V4L2_CID_CONTRAST, 0, 255, 1, 127);
394 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
395 V4L2_CID_SATURATION, 0, 255, 1, 127);
396 v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
397 V4L2_CID_POWER_LINE_FREQUENCY,
398 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 1,
399 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
400
401 if (hdl->error) {
402 pr_err("Could not initialize controls\n");
403 return hdl->error;
404 }
478 return 0; 405 return 0;
479} 406}
480 407
481/* sub-driver description */ 408/* sub-driver description */
482static const struct sd_desc sd_desc = { 409static const struct sd_desc sd_desc = {
483 .name = MODULE_NAME, 410 .name = MODULE_NAME,
484 .ctrls = sd_ctrls,
485 .nctrls = NCTRLS,
486 .config = sd_config, 411 .config = sd_config,
487 .init = sd_init, 412 .init = sd_init,
413 .init_controls = sd_init_controls,
488 .start = sd_start, 414 .start = sd_start,
489 .stopN = sd_stopN, 415 .stopN = sd_stopN,
490 .pkt_scan = sd_pkt_scan, 416 .pkt_scan = sd_pkt_scan,
491 .querymenu = sd_querymenu,
492 .get_jcomp = sd_get_jcomp,
493 .set_jcomp = sd_set_jcomp,
494}; 417};
495 418
496/* -- module initialisation -- */ 419/* -- module initialisation -- */
@@ -516,6 +439,7 @@ static struct usb_driver sd_driver = {
516#ifdef CONFIG_PM 439#ifdef CONFIG_PM
517 .suspend = gspca_suspend, 440 .suspend = gspca_suspend,
518 .resume = gspca_resume, 441 .resume = gspca_resume,
442 .reset_resume = gspca_resume,
519#endif 443#endif
520}; 444};
521 445
diff --git a/drivers/media/video/gspca/stv0680.c b/drivers/media/video/gspca/stv0680.c
index 461ed645f309..67605272aaa8 100644
--- a/drivers/media/video/gspca/stv0680.c
+++ b/drivers/media/video/gspca/stv0680.c
@@ -46,10 +46,6 @@ struct sd {
46 u8 current_mode; 46 u8 current_mode;
47}; 47};
48 48
49/* V4L2 controls supported by the driver */
50static const struct ctrl sd_ctrls[] = {
51};
52
53static int stv_sndctrl(struct gspca_dev *gspca_dev, int set, u8 req, u16 val, 49static int stv_sndctrl(struct gspca_dev *gspca_dev, int set, u8 req, u16 val,
54 int size) 50 int size)
55{ 51{
@@ -318,8 +314,6 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
318/* sub-driver description */ 314/* sub-driver description */
319static const struct sd_desc sd_desc = { 315static const struct sd_desc sd_desc = {
320 .name = MODULE_NAME, 316 .name = MODULE_NAME,
321 .ctrls = sd_ctrls,
322 .nctrls = ARRAY_SIZE(sd_ctrls),
323 .config = sd_config, 317 .config = sd_config,
324 .init = sd_init, 318 .init = sd_init,
325 .start = sd_start, 319 .start = sd_start,
@@ -352,6 +346,7 @@ static struct usb_driver sd_driver = {
352#ifdef CONFIG_PM 346#ifdef CONFIG_PM
353 .suspend = gspca_suspend, 347 .suspend = gspca_suspend,
354 .resume = gspca_resume, 348 .resume = gspca_resume,
349 .reset_resume = gspca_resume,
355#endif 350#endif
356}; 351};
357 352
diff --git a/drivers/media/video/gspca/sunplus.c b/drivers/media/video/gspca/sunplus.c
index c80f0c0c75b6..9ccfcb1c6479 100644
--- a/drivers/media/video/gspca/sunplus.c
+++ b/drivers/media/video/gspca/sunplus.c
@@ -30,18 +30,13 @@ MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
30MODULE_DESCRIPTION("GSPCA/SPCA5xx USB Camera Driver"); 30MODULE_DESCRIPTION("GSPCA/SPCA5xx USB Camera Driver");
31MODULE_LICENSE("GPL"); 31MODULE_LICENSE("GPL");
32 32
33#define QUALITY 85
34
33/* specific webcam descriptor */ 35/* specific webcam descriptor */
34struct sd { 36struct sd {
35 struct gspca_dev gspca_dev; /* !! must be the first item */ 37 struct gspca_dev gspca_dev; /* !! must be the first item */
36 38
37 s8 brightness; 39 bool autogain;
38 u8 contrast;
39 u8 colors;
40 u8 autogain;
41 u8 quality;
42#define QUALITY_MIN 70
43#define QUALITY_MAX 95
44#define QUALITY_DEF 85
45 40
46 u8 bridge; 41 u8 bridge;
47#define BRIDGE_SPCA504 0 42#define BRIDGE_SPCA504 0
@@ -59,75 +54,6 @@ struct sd {
59 u8 jpeg_hdr[JPEG_HDR_SZ]; 54 u8 jpeg_hdr[JPEG_HDR_SZ];
60}; 55};
61 56
62/* V4L2 controls supported by the driver */
63static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
64static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
65static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
66static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
67static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
68static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
69static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
70static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
71
72static const struct ctrl sd_ctrls[] = {
73 {
74 {
75 .id = V4L2_CID_BRIGHTNESS,
76 .type = V4L2_CTRL_TYPE_INTEGER,
77 .name = "Brightness",
78 .minimum = -128,
79 .maximum = 127,
80 .step = 1,
81#define BRIGHTNESS_DEF 0
82 .default_value = BRIGHTNESS_DEF,
83 },
84 .set = sd_setbrightness,
85 .get = sd_getbrightness,
86 },
87 {
88 {
89 .id = V4L2_CID_CONTRAST,
90 .type = V4L2_CTRL_TYPE_INTEGER,
91 .name = "Contrast",
92 .minimum = 0,
93 .maximum = 0xff,
94 .step = 1,
95#define CONTRAST_DEF 0x20
96 .default_value = CONTRAST_DEF,
97 },
98 .set = sd_setcontrast,
99 .get = sd_getcontrast,
100 },
101 {
102 {
103 .id = V4L2_CID_SATURATION,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Color",
106 .minimum = 0,
107 .maximum = 0xff,
108 .step = 1,
109#define COLOR_DEF 0x1a
110 .default_value = COLOR_DEF,
111 },
112 .set = sd_setcolors,
113 .get = sd_getcolors,
114 },
115 {
116 {
117 .id = V4L2_CID_AUTOGAIN,
118 .type = V4L2_CTRL_TYPE_BOOLEAN,
119 .name = "Auto Gain",
120 .minimum = 0,
121 .maximum = 1,
122 .step = 1,
123#define AUTOGAIN_DEF 1
124 .default_value = AUTOGAIN_DEF,
125 },
126 .set = sd_setautogain,
127 .get = sd_getautogain,
128 },
129};
130
131static const struct v4l2_pix_format vga_mode[] = { 57static const struct v4l2_pix_format vga_mode[] = {
132 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 58 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
133 .bytesperline = 320, 59 .bytesperline = 320,
@@ -597,31 +523,31 @@ static void spca504B_setQtable(struct gspca_dev *gspca_dev)
597 spca504B_PollingDataReady(gspca_dev); 523 spca504B_PollingDataReady(gspca_dev);
598} 524}
599 525
600static void setbrightness(struct gspca_dev *gspca_dev) 526static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
601{ 527{
602 struct sd *sd = (struct sd *) gspca_dev; 528 struct sd *sd = (struct sd *) gspca_dev;
603 u16 reg; 529 u16 reg;
604 530
605 reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f0 : 0x21a7; 531 reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f0 : 0x21a7;
606 reg_w_riv(gspca_dev, 0x00, reg, sd->brightness); 532 reg_w_riv(gspca_dev, 0x00, reg, val);
607} 533}
608 534
609static void setcontrast(struct gspca_dev *gspca_dev) 535static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
610{ 536{
611 struct sd *sd = (struct sd *) gspca_dev; 537 struct sd *sd = (struct sd *) gspca_dev;
612 u16 reg; 538 u16 reg;
613 539
614 reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f1 : 0x21a8; 540 reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f1 : 0x21a8;
615 reg_w_riv(gspca_dev, 0x00, reg, sd->contrast); 541 reg_w_riv(gspca_dev, 0x00, reg, val);
616} 542}
617 543
618static void setcolors(struct gspca_dev *gspca_dev) 544static void setcolors(struct gspca_dev *gspca_dev, s32 val)
619{ 545{
620 struct sd *sd = (struct sd *) gspca_dev; 546 struct sd *sd = (struct sd *) gspca_dev;
621 u16 reg; 547 u16 reg;
622 548
623 reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f6 : 0x21ae; 549 reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f6 : 0x21ae;
624 reg_w_riv(gspca_dev, 0x00, reg, sd->colors); 550 reg_w_riv(gspca_dev, 0x00, reg, val);
625} 551}
626 552
627static void init_ctl_reg(struct gspca_dev *gspca_dev) 553static void init_ctl_reg(struct gspca_dev *gspca_dev)
@@ -629,10 +555,6 @@ static void init_ctl_reg(struct gspca_dev *gspca_dev)
629 struct sd *sd = (struct sd *) gspca_dev; 555 struct sd *sd = (struct sd *) gspca_dev;
630 int pollreg = 1; 556 int pollreg = 1;
631 557
632 setbrightness(gspca_dev);
633 setcontrast(gspca_dev);
634 setcolors(gspca_dev);
635
636 switch (sd->bridge) { 558 switch (sd->bridge) {
637 case BRIDGE_SPCA504: 559 case BRIDGE_SPCA504:
638 case BRIDGE_SPCA504C: 560 case BRIDGE_SPCA504C:
@@ -704,11 +626,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
704 cam->nmodes = ARRAY_SIZE(vga_mode2); 626 cam->nmodes = ARRAY_SIZE(vga_mode2);
705 break; 627 break;
706 } 628 }
707 sd->brightness = BRIGHTNESS_DEF;
708 sd->contrast = CONTRAST_DEF;
709 sd->colors = COLOR_DEF;
710 sd->autogain = AUTOGAIN_DEF;
711 sd->quality = QUALITY_DEF;
712 return 0; 629 return 0;
713} 630}
714 631
@@ -807,7 +724,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
807 /* create the JPEG header */ 724 /* create the JPEG header */
808 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width, 725 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
809 0x22); /* JPEG 411 */ 726 0x22); /* JPEG 411 */
810 jpeg_set_qual(sd->jpeg_hdr, sd->quality); 727 jpeg_set_qual(sd->jpeg_hdr, QUALITY);
811 728
812 if (sd->bridge == BRIDGE_SPCA504B) 729 if (sd->bridge == BRIDGE_SPCA504B)
813 spca504B_setQtable(gspca_dev); 730 spca504B_setQtable(gspca_dev);
@@ -1012,116 +929,69 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1012 gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 929 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1013} 930}
1014 931
1015static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 932static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1016{
1017 struct sd *sd = (struct sd *) gspca_dev;
1018
1019 sd->brightness = val;
1020 if (gspca_dev->streaming)
1021 setbrightness(gspca_dev);
1022 return gspca_dev->usb_err;
1023}
1024
1025static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1026{
1027 struct sd *sd = (struct sd *) gspca_dev;
1028
1029 *val = sd->brightness;
1030 return 0;
1031}
1032
1033static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
1034{ 933{
1035 struct sd *sd = (struct sd *) gspca_dev; 934 struct gspca_dev *gspca_dev =
935 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
936 struct sd *sd = (struct sd *)gspca_dev;
1036 937
1037 sd->contrast = val; 938 gspca_dev->usb_err = 0;
1038 if (gspca_dev->streaming)
1039 setcontrast(gspca_dev);
1040 return gspca_dev->usb_err;
1041}
1042 939
1043static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val) 940 if (!gspca_dev->streaming)
1044{ 941 return 0;
1045 struct sd *sd = (struct sd *) gspca_dev;
1046 942
1047 *val = sd->contrast; 943 switch (ctrl->id) {
1048 return 0; 944 case V4L2_CID_BRIGHTNESS:
1049} 945 setbrightness(gspca_dev, ctrl->val);
1050 946 break;
1051static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val) 947 case V4L2_CID_CONTRAST:
1052{ 948 setcontrast(gspca_dev, ctrl->val);
1053 struct sd *sd = (struct sd *) gspca_dev; 949 break;
1054 950 case V4L2_CID_SATURATION:
1055 sd->colors = val; 951 setcolors(gspca_dev, ctrl->val);
1056 if (gspca_dev->streaming) 952 break;
1057 setcolors(gspca_dev); 953 case V4L2_CID_AUTOGAIN:
954 sd->autogain = ctrl->val;
955 break;
956 }
1058 return gspca_dev->usb_err; 957 return gspca_dev->usb_err;
1059} 958}
1060 959
1061static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val) 960static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1062{ 961 .s_ctrl = sd_s_ctrl,
1063 struct sd *sd = (struct sd *) gspca_dev; 962};
1064
1065 *val = sd->colors;
1066 return 0;
1067}
1068
1069static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1070{
1071 struct sd *sd = (struct sd *) gspca_dev;
1072
1073 sd->autogain = val;
1074 return 0;
1075}
1076
1077static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1078{
1079 struct sd *sd = (struct sd *) gspca_dev;
1080
1081 *val = sd->autogain;
1082 return 0;
1083}
1084
1085static int sd_set_jcomp(struct gspca_dev *gspca_dev,
1086 struct v4l2_jpegcompression *jcomp)
1087{
1088 struct sd *sd = (struct sd *) gspca_dev;
1089
1090 if (jcomp->quality < QUALITY_MIN)
1091 sd->quality = QUALITY_MIN;
1092 else if (jcomp->quality > QUALITY_MAX)
1093 sd->quality = QUALITY_MAX;
1094 else
1095 sd->quality = jcomp->quality;
1096 if (gspca_dev->streaming)
1097 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
1098 return gspca_dev->usb_err;
1099}
1100 963
1101static int sd_get_jcomp(struct gspca_dev *gspca_dev, 964static int sd_init_controls(struct gspca_dev *gspca_dev)
1102 struct v4l2_jpegcompression *jcomp)
1103{ 965{
1104 struct sd *sd = (struct sd *) gspca_dev; 966 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1105 967
1106 memset(jcomp, 0, sizeof *jcomp); 968 gspca_dev->vdev.ctrl_handler = hdl;
1107 jcomp->quality = sd->quality; 969 v4l2_ctrl_handler_init(hdl, 4);
1108 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT 970 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1109 | V4L2_JPEG_MARKER_DQT; 971 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
972 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
973 V4L2_CID_CONTRAST, 0, 255, 1, 0x20);
974 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
975 V4L2_CID_SATURATION, 0, 255, 1, 0x1a);
976 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
977 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
978
979 if (hdl->error) {
980 pr_err("Could not initialize controls\n");
981 return hdl->error;
982 }
1110 return 0; 983 return 0;
1111} 984}
1112 985
1113/* sub-driver description */ 986/* sub-driver description */
1114static const struct sd_desc sd_desc = { 987static const struct sd_desc sd_desc = {
1115 .name = MODULE_NAME, 988 .name = MODULE_NAME,
1116 .ctrls = sd_ctrls,
1117 .nctrls = ARRAY_SIZE(sd_ctrls),
1118 .config = sd_config, 989 .config = sd_config,
1119 .init = sd_init, 990 .init = sd_init,
991 .init_controls = sd_init_controls,
1120 .start = sd_start, 992 .start = sd_start,
1121 .stopN = sd_stopN, 993 .stopN = sd_stopN,
1122 .pkt_scan = sd_pkt_scan, 994 .pkt_scan = sd_pkt_scan,
1123 .get_jcomp = sd_get_jcomp,
1124 .set_jcomp = sd_set_jcomp,
1125}; 995};
1126 996
1127/* -- module initialisation -- */ 997/* -- module initialisation -- */
@@ -1208,6 +1078,7 @@ static struct usb_driver sd_driver = {
1208#ifdef CONFIG_PM 1078#ifdef CONFIG_PM
1209 .suspend = gspca_suspend, 1079 .suspend = gspca_suspend,
1210 .resume = gspca_resume, 1080 .resume = gspca_resume,
1081 .reset_resume = gspca_resume,
1211#endif 1082#endif
1212}; 1083};
1213 1084
diff --git a/drivers/media/video/gspca/t613.c b/drivers/media/video/gspca/t613.c
index 9b9f85a8e60e..8bc6c3ceec2c 100644
--- a/drivers/media/video/gspca/t613.c
+++ b/drivers/media/video/gspca/t613.c
@@ -34,28 +34,19 @@
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include "gspca.h" 35#include "gspca.h"
36 36
37#define V4L2_CID_EFFECTS (V4L2_CID_PRIVATE_BASE + 0)
38
39MODULE_AUTHOR("Leandro Costantino <le_costantino@pixartargentina.com.ar>"); 37MODULE_AUTHOR("Leandro Costantino <le_costantino@pixartargentina.com.ar>");
40MODULE_DESCRIPTION("GSPCA/T613 (JPEG Compliance) USB Camera Driver"); 38MODULE_DESCRIPTION("GSPCA/T613 (JPEG Compliance) USB Camera Driver");
41MODULE_LICENSE("GPL"); 39MODULE_LICENSE("GPL");
42 40
43struct sd { 41struct sd {
44 struct gspca_dev gspca_dev; /* !! must be the first item */ 42 struct gspca_dev gspca_dev; /* !! must be the first item */
45 43 struct v4l2_ctrl *freq;
46 u8 brightness; 44 struct { /* awb / color gains control cluster */
47 u8 contrast; 45 struct v4l2_ctrl *awb;
48 u8 colors; 46 struct v4l2_ctrl *gain;
49 u8 autogain; 47 struct v4l2_ctrl *red_balance;
50 u8 gamma; 48 struct v4l2_ctrl *blue_balance;
51 u8 sharpness; 49 };
52 u8 freq;
53 u8 red_gain;
54 u8 blue_gain;
55 u8 green_gain;
56 u8 awb; /* set default r/g/b and activate */
57 u8 mirror;
58 u8 effect;
59 50
60 u8 sensor; 51 u8 sensor;
61 u8 button_pressed; 52 u8 button_pressed;
@@ -67,245 +58,31 @@ enum sensors {
67 SENSOR_LT168G, /* must verify if this is the actual model */ 58 SENSOR_LT168G, /* must verify if this is the actual model */
68}; 59};
69 60
70/* V4L2 controls supported by the driver */
71static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
72static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
73static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
74static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
75static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
76static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
77static int sd_setlowlight(struct gspca_dev *gspca_dev, __s32 val);
78static int sd_getlowlight(struct gspca_dev *gspca_dev, __s32 *val);
79static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val);
80static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val);
81static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
82static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
83static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
84static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
85
86static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val);
87static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val);
88static int sd_setblue_gain(struct gspca_dev *gspca_dev, __s32 val);
89static int sd_getblue_gain(struct gspca_dev *gspca_dev, __s32 *val);
90static int sd_setred_gain(struct gspca_dev *gspca_dev, __s32 val);
91static int sd_getred_gain(struct gspca_dev *gspca_dev, __s32 *val);
92static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
93static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
94
95static int sd_setmirror(struct gspca_dev *gspca_dev, __s32 val);
96static int sd_getmirror(struct gspca_dev *gspca_dev, __s32 *val);
97static int sd_seteffect(struct gspca_dev *gspca_dev, __s32 val);
98static int sd_geteffect(struct gspca_dev *gspca_dev, __s32 *val);
99
100static const struct ctrl sd_ctrls[] = {
101 {
102 {
103 .id = V4L2_CID_BRIGHTNESS,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Brightness",
106 .minimum = 0,
107 .maximum = 14,
108 .step = 1,
109#define BRIGHTNESS_DEF 8
110 .default_value = BRIGHTNESS_DEF,
111 },
112 .set = sd_setbrightness,
113 .get = sd_getbrightness,
114 },
115 {
116 {
117 .id = V4L2_CID_CONTRAST,
118 .type = V4L2_CTRL_TYPE_INTEGER,
119 .name = "Contrast",
120 .minimum = 0,
121 .maximum = 0x0d,
122 .step = 1,
123#define CONTRAST_DEF 0x07
124 .default_value = CONTRAST_DEF,
125 },
126 .set = sd_setcontrast,
127 .get = sd_getcontrast,
128 },
129 {
130 {
131 .id = V4L2_CID_SATURATION,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Color",
134 .minimum = 0,
135 .maximum = 0x0f,
136 .step = 1,
137#define COLORS_DEF 0x05
138 .default_value = COLORS_DEF,
139 },
140 .set = sd_setcolors,
141 .get = sd_getcolors,
142 },
143#define GAMMA_MAX 16
144#define GAMMA_DEF 10
145 {
146 {
147 .id = V4L2_CID_GAMMA, /* (gamma on win) */
148 .type = V4L2_CTRL_TYPE_INTEGER,
149 .name = "Gamma",
150 .minimum = 0,
151 .maximum = GAMMA_MAX - 1,
152 .step = 1,
153 .default_value = GAMMA_DEF,
154 },
155 .set = sd_setgamma,
156 .get = sd_getgamma,
157 },
158 {
159 {
160 .id = V4L2_CID_BACKLIGHT_COMPENSATION, /* Activa lowlight,
161 * some apps dont bring up the
162 * backligth_compensation control) */
163 .type = V4L2_CTRL_TYPE_INTEGER,
164 .name = "Low Light",
165 .minimum = 0,
166 .maximum = 1,
167 .step = 1,
168#define AUTOGAIN_DEF 0x01
169 .default_value = AUTOGAIN_DEF,
170 },
171 .set = sd_setlowlight,
172 .get = sd_getlowlight,
173 },
174 {
175 {
176 .id = V4L2_CID_HFLIP,
177 .type = V4L2_CTRL_TYPE_BOOLEAN,
178 .name = "Mirror Image",
179 .minimum = 0,
180 .maximum = 1,
181 .step = 1,
182#define MIRROR_DEF 0
183 .default_value = MIRROR_DEF,
184 },
185 .set = sd_setmirror,
186 .get = sd_getmirror
187 },
188 {
189 {
190 .id = V4L2_CID_POWER_LINE_FREQUENCY,
191 .type = V4L2_CTRL_TYPE_MENU,
192 .name = "Light Frequency Filter",
193 .minimum = 1, /* 1 -> 0x50, 2->0x60 */
194 .maximum = 2,
195 .step = 1,
196#define FREQ_DEF 1
197 .default_value = FREQ_DEF,
198 },
199 .set = sd_setfreq,
200 .get = sd_getfreq},
201
202 {
203 {
204 .id = V4L2_CID_AUTO_WHITE_BALANCE,
205 .type = V4L2_CTRL_TYPE_INTEGER,
206 .name = "Auto White Balance",
207 .minimum = 0,
208 .maximum = 1,
209 .step = 1,
210#define AWB_DEF 0
211 .default_value = AWB_DEF,
212 },
213 .set = sd_setawb,
214 .get = sd_getawb
215 },
216 {
217 {
218 .id = V4L2_CID_SHARPNESS,
219 .type = V4L2_CTRL_TYPE_INTEGER,
220 .name = "Sharpness",
221 .minimum = 0,
222 .maximum = 15,
223 .step = 1,
224#define SHARPNESS_DEF 0x06
225 .default_value = SHARPNESS_DEF,
226 },
227 .set = sd_setsharpness,
228 .get = sd_getsharpness,
229 },
230 {
231 {
232 .id = V4L2_CID_EFFECTS,
233 .type = V4L2_CTRL_TYPE_MENU,
234 .name = "Webcam Effects",
235 .minimum = 0,
236 .maximum = 4,
237 .step = 1,
238#define EFFECTS_DEF 0
239 .default_value = EFFECTS_DEF,
240 },
241 .set = sd_seteffect,
242 .get = sd_geteffect
243 },
244 {
245 {
246 .id = V4L2_CID_BLUE_BALANCE,
247 .type = V4L2_CTRL_TYPE_INTEGER,
248 .name = "Blue Balance",
249 .minimum = 0x10,
250 .maximum = 0x40,
251 .step = 1,
252#define BLUE_GAIN_DEF 0x20
253 .default_value = BLUE_GAIN_DEF,
254 },
255 .set = sd_setblue_gain,
256 .get = sd_getblue_gain,
257 },
258 {
259 {
260 .id = V4L2_CID_RED_BALANCE,
261 .type = V4L2_CTRL_TYPE_INTEGER,
262 .name = "Red Balance",
263 .minimum = 0x10,
264 .maximum = 0x40,
265 .step = 1,
266#define RED_GAIN_DEF 0x20
267 .default_value = RED_GAIN_DEF,
268 },
269 .set = sd_setred_gain,
270 .get = sd_getred_gain,
271 },
272 {
273 {
274 .id = V4L2_CID_GAIN,
275 .type = V4L2_CTRL_TYPE_INTEGER,
276 .name = "Gain",
277 .minimum = 0x10,
278 .maximum = 0x40,
279 .step = 1,
280#define GAIN_DEF 0x20
281 .default_value = GAIN_DEF,
282 },
283 .set = sd_setgain,
284 .get = sd_getgain,
285 },
286};
287
288static const struct v4l2_pix_format vga_mode_t16[] = { 61static const struct v4l2_pix_format vga_mode_t16[] = {
289 {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 62 {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
290 .bytesperline = 160, 63 .bytesperline = 160,
291 .sizeimage = 160 * 120 * 4 / 8 + 590, 64 .sizeimage = 160 * 120 * 4 / 8 + 590,
292 .colorspace = V4L2_COLORSPACE_JPEG, 65 .colorspace = V4L2_COLORSPACE_JPEG,
293 .priv = 4}, 66 .priv = 4},
67#if 0 /* HDG: broken with my test cam, so lets disable it */
294 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 68 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
295 .bytesperline = 176, 69 .bytesperline = 176,
296 .sizeimage = 176 * 144 * 3 / 8 + 590, 70 .sizeimage = 176 * 144 * 3 / 8 + 590,
297 .colorspace = V4L2_COLORSPACE_JPEG, 71 .colorspace = V4L2_COLORSPACE_JPEG,
298 .priv = 3}, 72 .priv = 3},
73#endif
299 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 74 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
300 .bytesperline = 320, 75 .bytesperline = 320,
301 .sizeimage = 320 * 240 * 3 / 8 + 590, 76 .sizeimage = 320 * 240 * 3 / 8 + 590,
302 .colorspace = V4L2_COLORSPACE_JPEG, 77 .colorspace = V4L2_COLORSPACE_JPEG,
303 .priv = 2}, 78 .priv = 2},
79#if 0 /* HDG: broken with my test cam, so lets disable it */
304 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 80 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
305 .bytesperline = 352, 81 .bytesperline = 352,
306 .sizeimage = 352 * 288 * 3 / 8 + 590, 82 .sizeimage = 352 * 288 * 3 / 8 + 590,
307 .colorspace = V4L2_COLORSPACE_JPEG, 83 .colorspace = V4L2_COLORSPACE_JPEG,
308 .priv = 1}, 84 .priv = 1},
85#endif
309 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 86 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
310 .bytesperline = 640, 87 .bytesperline = 640,
311 .sizeimage = 640 * 480 * 3 / 8 + 590, 88 .sizeimage = 640 * 480 * 3 / 8 + 590,
@@ -454,17 +231,6 @@ static const struct additional_sensor_data sensor_data[] = {
454}; 231};
455 232
456#define MAX_EFFECTS 7 233#define MAX_EFFECTS 7
457/* easily done by soft, this table could be removed,
458 * i keep it here just in case */
459static char *effects_control[MAX_EFFECTS] = {
460 "Normal",
461 "Emboss", /* disabled */
462 "Monochrome",
463 "Sepia",
464 "Sketch",
465 "Sun Effect", /* disabled */
466 "Negative",
467};
468static const u8 effects_table[MAX_EFFECTS][6] = { 234static const u8 effects_table[MAX_EFFECTS][6] = {
469 {0xa8, 0xe8, 0xc6, 0xd2, 0xc0, 0x00}, /* Normal */ 235 {0xa8, 0xe8, 0xc6, 0xd2, 0xc0, 0x00}, /* Normal */
470 {0xa8, 0xc8, 0xc6, 0x52, 0xc0, 0x04}, /* Repujar */ 236 {0xa8, 0xc8, 0xc6, 0x52, 0xc0, 0x04}, /* Repujar */
@@ -475,7 +241,8 @@ static const u8 effects_table[MAX_EFFECTS][6] = {
475 {0xa8, 0xc8, 0xc6, 0xd2, 0xc0, 0x40}, /* Negative */ 241 {0xa8, 0xc8, 0xc6, 0xd2, 0xc0, 0x40}, /* Negative */
476}; 242};
477 243
478static const u8 gamma_table[GAMMA_MAX][17] = { 244#define GAMMA_MAX (15)
245static const u8 gamma_table[GAMMA_MAX+1][17] = {
479/* gamma table from cam1690.ini */ 246/* gamma table from cam1690.ini */
480 {0x00, 0x00, 0x01, 0x04, 0x08, 0x0e, 0x16, 0x21, /* 0 */ 247 {0x00, 0x00, 0x01, 0x04, 0x08, 0x0e, 0x16, 0x21, /* 0 */
481 0x2e, 0x3d, 0x50, 0x65, 0x7d, 0x99, 0xb8, 0xdb, 248 0x2e, 0x3d, 0x50, 0x65, 0x7d, 0x99, 0xb8, 0xdb,
@@ -683,38 +450,18 @@ static void om6802_sensor_init(struct gspca_dev *gspca_dev)
683static int sd_config(struct gspca_dev *gspca_dev, 450static int sd_config(struct gspca_dev *gspca_dev,
684 const struct usb_device_id *id) 451 const struct usb_device_id *id)
685{ 452{
686 struct sd *sd = (struct sd *) gspca_dev; 453 struct cam *cam = &gspca_dev->cam;
687 struct cam *cam;
688
689 cam = &gspca_dev->cam;
690 454
691 cam->cam_mode = vga_mode_t16; 455 cam->cam_mode = vga_mode_t16;
692 cam->nmodes = ARRAY_SIZE(vga_mode_t16); 456 cam->nmodes = ARRAY_SIZE(vga_mode_t16);
693 457
694 sd->brightness = BRIGHTNESS_DEF;
695 sd->contrast = CONTRAST_DEF;
696 sd->colors = COLORS_DEF;
697 sd->gamma = GAMMA_DEF;
698 sd->autogain = AUTOGAIN_DEF;
699 sd->mirror = MIRROR_DEF;
700 sd->freq = FREQ_DEF;
701 sd->awb = AWB_DEF;
702 sd->sharpness = SHARPNESS_DEF;
703 sd->effect = EFFECTS_DEF;
704 sd->red_gain = RED_GAIN_DEF;
705 sd->blue_gain = BLUE_GAIN_DEF;
706 sd->green_gain = GAIN_DEF * 3 - RED_GAIN_DEF - BLUE_GAIN_DEF;
707
708 return 0; 458 return 0;
709} 459}
710 460
711static void setbrightness(struct gspca_dev *gspca_dev) 461static void setbrightness(struct gspca_dev *gspca_dev, s32 brightness)
712{ 462{
713 struct sd *sd = (struct sd *) gspca_dev;
714 unsigned int brightness;
715 u8 set6[4] = { 0x8f, 0x24, 0xc3, 0x00 }; 463 u8 set6[4] = { 0x8f, 0x24, 0xc3, 0x00 };
716 464
717 brightness = sd->brightness;
718 if (brightness < 7) { 465 if (brightness < 7) {
719 set6[1] = 0x26; 466 set6[1] = 0x26;
720 set6[3] = 0x70 - brightness * 0x10; 467 set6[3] = 0x70 - brightness * 0x10;
@@ -725,10 +472,8 @@ static void setbrightness(struct gspca_dev *gspca_dev)
725 reg_w_buf(gspca_dev, set6, sizeof set6); 472 reg_w_buf(gspca_dev, set6, sizeof set6);
726} 473}
727 474
728static void setcontrast(struct gspca_dev *gspca_dev) 475static void setcontrast(struct gspca_dev *gspca_dev, s32 contrast)
729{ 476{
730 struct sd *sd = (struct sd *) gspca_dev;
731 unsigned int contrast = sd->contrast;
732 u16 reg_to_write; 477 u16 reg_to_write;
733 478
734 if (contrast < 7) 479 if (contrast < 7)
@@ -739,89 +484,62 @@ static void setcontrast(struct gspca_dev *gspca_dev)
739 reg_w(gspca_dev, reg_to_write); 484 reg_w(gspca_dev, reg_to_write);
740} 485}
741 486
742static void setcolors(struct gspca_dev *gspca_dev) 487static void setcolors(struct gspca_dev *gspca_dev, s32 val)
743{ 488{
744 struct sd *sd = (struct sd *) gspca_dev;
745 u16 reg_to_write; 489 u16 reg_to_write;
746 490
747 reg_to_write = 0x80bb + sd->colors * 0x100; /* was 0xc0 */ 491 reg_to_write = 0x80bb + val * 0x100; /* was 0xc0 */
748 reg_w(gspca_dev, reg_to_write); 492 reg_w(gspca_dev, reg_to_write);
749} 493}
750 494
751static void setgamma(struct gspca_dev *gspca_dev) 495static void setgamma(struct gspca_dev *gspca_dev, s32 val)
752{ 496{
753 struct sd *sd = (struct sd *) gspca_dev;
754
755 PDEBUG(D_CONF, "Gamma: %d", sd->gamma); 497 PDEBUG(D_CONF, "Gamma: %d", sd->gamma);
756 reg_w_ixbuf(gspca_dev, 0x90, 498 reg_w_ixbuf(gspca_dev, 0x90,
757 gamma_table[sd->gamma], sizeof gamma_table[0]); 499 gamma_table[val], sizeof gamma_table[0]);
758} 500}
759 501
760static void setRGB(struct gspca_dev *gspca_dev) 502static void setawb_n_RGB(struct gspca_dev *gspca_dev)
761{ 503{
762 struct sd *sd = (struct sd *) gspca_dev; 504 struct sd *sd = (struct sd *) gspca_dev;
763 u8 all_gain_reg[6] = 505 u8 all_gain_reg[8] = {
764 {0x87, 0x00, 0x88, 0x00, 0x89, 0x00}; 506 0x87, 0x00, 0x88, 0x00, 0x89, 0x00, 0x80, 0x00 };
507 s32 red_gain, blue_gain, green_gain;
508
509 green_gain = sd->gain->val;
510
511 red_gain = green_gain + sd->red_balance->val;
512 if (red_gain > 0x40)
513 red_gain = 0x40;
514 else if (red_gain < 0x10)
515 red_gain = 0x10;
516
517 blue_gain = green_gain + sd->blue_balance->val;
518 if (blue_gain > 0x40)
519 blue_gain = 0x40;
520 else if (blue_gain < 0x10)
521 blue_gain = 0x10;
522
523 all_gain_reg[1] = red_gain;
524 all_gain_reg[3] = blue_gain;
525 all_gain_reg[5] = green_gain;
526 all_gain_reg[7] = sensor_data[sd->sensor].reg80;
527 if (!sd->awb->val)
528 all_gain_reg[7] &= ~0x04; /* AWB off */
765 529
766 all_gain_reg[1] = sd->red_gain;
767 all_gain_reg[3] = sd->blue_gain;
768 all_gain_reg[5] = sd->green_gain;
769 reg_w_buf(gspca_dev, all_gain_reg, sizeof all_gain_reg); 530 reg_w_buf(gspca_dev, all_gain_reg, sizeof all_gain_reg);
770} 531}
771 532
772/* Generic fnc for r/b balance, exposure and awb */ 533static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
773static void setawb(struct gspca_dev *gspca_dev)
774{ 534{
775 struct sd *sd = (struct sd *) gspca_dev;
776 u16 reg80;
777
778 reg80 = (sensor_data[sd->sensor].reg80 << 8) | 0x80;
779
780 /* on awb leave defaults values */
781 if (!sd->awb) {
782 /* shoud we wait here.. */
783 /* update and reset RGB gains with webcam values */
784 sd->red_gain = reg_r(gspca_dev, 0x0087);
785 sd->blue_gain = reg_r(gspca_dev, 0x0088);
786 sd->green_gain = reg_r(gspca_dev, 0x0089);
787 reg80 &= ~0x0400; /* AWB off */
788 }
789 reg_w(gspca_dev, reg80);
790 reg_w(gspca_dev, reg80);
791}
792
793static void init_gains(struct gspca_dev *gspca_dev)
794{
795 struct sd *sd = (struct sd *) gspca_dev;
796 u16 reg80;
797 u8 all_gain_reg[8] =
798 {0x87, 0x00, 0x88, 0x00, 0x89, 0x00, 0x80, 0x00};
799
800 all_gain_reg[1] = sd->red_gain;
801 all_gain_reg[3] = sd->blue_gain;
802 all_gain_reg[5] = sd->green_gain;
803 reg80 = sensor_data[sd->sensor].reg80;
804 if (!sd->awb)
805 reg80 &= ~0x04;
806 all_gain_reg[7] = reg80;
807 reg_w_buf(gspca_dev, all_gain_reg, sizeof all_gain_reg);
808
809 reg_w(gspca_dev, (sd->red_gain << 8) + 0x87);
810 reg_w(gspca_dev, (sd->blue_gain << 8) + 0x88);
811 reg_w(gspca_dev, (sd->green_gain << 8) + 0x89);
812}
813
814static void setsharpness(struct gspca_dev *gspca_dev)
815{
816 struct sd *sd = (struct sd *) gspca_dev;
817 u16 reg_to_write; 535 u16 reg_to_write;
818 536
819 reg_to_write = 0x0aa6 + 0x1000 * sd->sharpness; 537 reg_to_write = 0x0aa6 + 0x1000 * val;
820 538
821 reg_w(gspca_dev, reg_to_write); 539 reg_w(gspca_dev, reg_to_write);
822} 540}
823 541
824static void setfreq(struct gspca_dev *gspca_dev) 542static void setfreq(struct gspca_dev *gspca_dev, s32 val)
825{ 543{
826 struct sd *sd = (struct sd *) gspca_dev; 544 struct sd *sd = (struct sd *) gspca_dev;
827 u8 reg66; 545 u8 reg66;
@@ -829,7 +547,7 @@ static void setfreq(struct gspca_dev *gspca_dev)
829 547
830 switch (sd->sensor) { 548 switch (sd->sensor) {
831 case SENSOR_LT168G: 549 case SENSOR_LT168G:
832 if (sd->freq != 0) 550 if (val != 0)
833 freq[3] = 0xa8; 551 freq[3] = 0xa8;
834 reg66 = 0x41; 552 reg66 = 0x41;
835 break; 553 break;
@@ -840,7 +558,7 @@ static void setfreq(struct gspca_dev *gspca_dev)
840 reg66 = 0x40; 558 reg66 = 0x40;
841 break; 559 break;
842 } 560 }
843 switch (sd->freq) { 561 switch (val) {
844 case 0: /* no flicker */ 562 case 0: /* no flicker */
845 freq[3] = 0xf0; 563 freq[3] = 0xf0;
846 break; 564 break;
@@ -941,14 +659,9 @@ static int sd_init(struct gspca_dev *gspca_dev)
941 reg_w(gspca_dev, (sensor->reg80 << 8) + 0x80); 659 reg_w(gspca_dev, (sensor->reg80 << 8) + 0x80);
942 reg_w(gspca_dev, (sensor->reg80 << 8) + 0x80); 660 reg_w(gspca_dev, (sensor->reg80 << 8) + 0x80);
943 reg_w(gspca_dev, (sensor->reg8e << 8) + 0x8e); 661 reg_w(gspca_dev, (sensor->reg8e << 8) + 0x8e);
944 662 reg_w(gspca_dev, (0x20 << 8) + 0x87);
945 setbrightness(gspca_dev); 663 reg_w(gspca_dev, (0x20 << 8) + 0x88);
946 setcontrast(gspca_dev); 664 reg_w(gspca_dev, (0x20 << 8) + 0x89);
947 setgamma(gspca_dev);
948 setcolors(gspca_dev);
949 setsharpness(gspca_dev);
950 init_gains(gspca_dev);
951 setfreq(gspca_dev);
952 665
953 reg_w_buf(gspca_dev, sensor->data5, sizeof sensor->data5); 666 reg_w_buf(gspca_dev, sensor->data5, sizeof sensor->data5);
954 reg_w_buf(gspca_dev, sensor->nset8, sizeof sensor->nset8); 667 reg_w_buf(gspca_dev, sensor->nset8, sizeof sensor->nset8);
@@ -968,31 +681,44 @@ static int sd_init(struct gspca_dev *gspca_dev)
968 return 0; 681 return 0;
969} 682}
970 683
971static void setmirror(struct gspca_dev *gspca_dev) 684static void setmirror(struct gspca_dev *gspca_dev, s32 val)
972{ 685{
973 struct sd *sd = (struct sd *) gspca_dev;
974 u8 hflipcmd[8] = 686 u8 hflipcmd[8] =
975 {0x62, 0x07, 0x63, 0x03, 0x64, 0x00, 0x60, 0x09}; 687 {0x62, 0x07, 0x63, 0x03, 0x64, 0x00, 0x60, 0x09};
976 688
977 if (sd->mirror) 689 if (val)
978 hflipcmd[3] = 0x01; 690 hflipcmd[3] = 0x01;
979 691
980 reg_w_buf(gspca_dev, hflipcmd, sizeof hflipcmd); 692 reg_w_buf(gspca_dev, hflipcmd, sizeof hflipcmd);
981} 693}
982 694
983static void seteffect(struct gspca_dev *gspca_dev) 695static void seteffect(struct gspca_dev *gspca_dev, s32 val)
984{ 696{
985 struct sd *sd = (struct sd *) gspca_dev; 697 int idx = 0;
986 698
987 reg_w_buf(gspca_dev, effects_table[sd->effect], 699 switch (val) {
988 sizeof effects_table[0]); 700 case V4L2_COLORFX_NONE:
989 if (sd->effect == 1 || sd->effect == 5) { 701 break;
990 PDEBUG(D_CONF, 702 case V4L2_COLORFX_BW:
991 "This effect have been disabled for webcam \"safety\""); 703 idx = 2;
992 return; 704 break;
705 case V4L2_COLORFX_SEPIA:
706 idx = 3;
707 break;
708 case V4L2_COLORFX_SKETCH:
709 idx = 4;
710 break;
711 case V4L2_COLORFX_NEGATIVE:
712 idx = 6;
713 break;
714 default:
715 break;
993 } 716 }
994 717
995 if (sd->effect == 1 || sd->effect == 4) 718 reg_w_buf(gspca_dev, effects_table[idx],
719 sizeof effects_table[0]);
720
721 if (val == V4L2_COLORFX_SKETCH)
996 reg_w(gspca_dev, 0x4aa6); 722 reg_w(gspca_dev, 0x4aa6);
997 else 723 else
998 reg_w(gspca_dev, 0xfaa6); 724 reg_w(gspca_dev, 0xfaa6);
@@ -1070,7 +796,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
1070 break; 796 break;
1071 } 797 }
1072 sensor = &sensor_data[sd->sensor]; 798 sensor = &sensor_data[sd->sensor];
1073 setfreq(gspca_dev); 799 setfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->freq));
1074 reg_r(gspca_dev, 0x0012); 800 reg_r(gspca_dev, 0x0012);
1075 reg_w_buf(gspca_dev, t2, sizeof t2); 801 reg_w_buf(gspca_dev, t2, sizeof t2);
1076 reg_w_ixbuf(gspca_dev, 0xb3, t3, sizeof t3); 802 reg_w_ixbuf(gspca_dev, 0xb3, t3, sizeof t3);
@@ -1142,296 +868,157 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1142 gspca_frame_add(gspca_dev, pkt_type, data, len); 868 gspca_frame_add(gspca_dev, pkt_type, data, len);
1143} 869}
1144 870
1145static int sd_setblue_gain(struct gspca_dev *gspca_dev, __s32 val) 871static int sd_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1146{
1147 struct sd *sd = (struct sd *) gspca_dev;
1148
1149 sd->blue_gain = val;
1150 if (gspca_dev->streaming)
1151 reg_w(gspca_dev, (val << 8) + 0x88);
1152 return 0;
1153}
1154
1155static int sd_getblue_gain(struct gspca_dev *gspca_dev, __s32 *val)
1156{
1157 struct sd *sd = (struct sd *) gspca_dev;
1158
1159 *val = sd->blue_gain;
1160 return 0;
1161}
1162
1163static int sd_setred_gain(struct gspca_dev *gspca_dev, __s32 val)
1164{
1165 struct sd *sd = (struct sd *) gspca_dev;
1166
1167 sd->red_gain = val;
1168 if (gspca_dev->streaming)
1169 reg_w(gspca_dev, (val << 8) + 0x87);
1170
1171 return 0;
1172}
1173
1174static int sd_getred_gain(struct gspca_dev *gspca_dev, __s32 *val)
1175{
1176 struct sd *sd = (struct sd *) gspca_dev;
1177
1178 *val = sd->red_gain;
1179 return 0;
1180}
1181
1182static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1183{
1184 struct sd *sd = (struct sd *) gspca_dev;
1185 u16 psg, nsg;
1186
1187 psg = sd->red_gain + sd->blue_gain + sd->green_gain;
1188 nsg = val * 3;
1189 sd->red_gain = sd->red_gain * nsg / psg;
1190 if (sd->red_gain > 0x40)
1191 sd->red_gain = 0x40;
1192 else if (sd->red_gain < 0x10)
1193 sd->red_gain = 0x10;
1194 sd->blue_gain = sd->blue_gain * nsg / psg;
1195 if (sd->blue_gain > 0x40)
1196 sd->blue_gain = 0x40;
1197 else if (sd->blue_gain < 0x10)
1198 sd->blue_gain = 0x10;
1199 sd->green_gain = sd->green_gain * nsg / psg;
1200 if (sd->green_gain > 0x40)
1201 sd->green_gain = 0x40;
1202 else if (sd->green_gain < 0x10)
1203 sd->green_gain = 0x10;
1204
1205 if (gspca_dev->streaming)
1206 setRGB(gspca_dev);
1207 return 0;
1208}
1209
1210static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1211{
1212 struct sd *sd = (struct sd *) gspca_dev;
1213
1214 *val = (sd->red_gain + sd->blue_gain + sd->green_gain) / 3;
1215 return 0;
1216}
1217
1218static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1219{
1220 struct sd *sd = (struct sd *) gspca_dev;
1221
1222 sd->brightness = val;
1223 if (gspca_dev->streaming)
1224 setbrightness(gspca_dev);
1225 return 0;
1226}
1227
1228static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1229{
1230 struct sd *sd = (struct sd *) gspca_dev;
1231
1232 *val = sd->brightness;
1233 return *val;
1234}
1235
1236static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val)
1237{
1238 struct sd *sd = (struct sd *) gspca_dev;
1239
1240 sd->awb = val;
1241 if (gspca_dev->streaming)
1242 setawb(gspca_dev);
1243 return 0;
1244}
1245
1246static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val)
1247{
1248 struct sd *sd = (struct sd *) gspca_dev;
1249
1250 *val = sd->awb;
1251 return *val;
1252}
1253
1254static int sd_setmirror(struct gspca_dev *gspca_dev, __s32 val)
1255{
1256 struct sd *sd = (struct sd *) gspca_dev;
1257
1258 sd->mirror = val;
1259 if (gspca_dev->streaming)
1260 setmirror(gspca_dev);
1261 return 0;
1262}
1263
1264static int sd_getmirror(struct gspca_dev *gspca_dev, __s32 *val)
1265{
1266 struct sd *sd = (struct sd *) gspca_dev;
1267
1268 *val = sd->mirror;
1269 return *val;
1270}
1271
1272static int sd_seteffect(struct gspca_dev *gspca_dev, __s32 val)
1273{
1274 struct sd *sd = (struct sd *) gspca_dev;
1275
1276 sd->effect = val;
1277 if (gspca_dev->streaming)
1278 seteffect(gspca_dev);
1279 return 0;
1280}
1281
1282static int sd_geteffect(struct gspca_dev *gspca_dev, __s32 *val)
1283{
1284 struct sd *sd = (struct sd *) gspca_dev;
1285
1286 *val = sd->effect;
1287 return *val;
1288}
1289
1290static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
1291{
1292 struct sd *sd = (struct sd *) gspca_dev;
1293
1294 sd->contrast = val;
1295 if (gspca_dev->streaming)
1296 setcontrast(gspca_dev);
1297 return 0;
1298}
1299
1300static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
1301{
1302 struct sd *sd = (struct sd *) gspca_dev;
1303
1304 *val = sd->contrast;
1305 return *val;
1306}
1307
1308static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
1309{
1310 struct sd *sd = (struct sd *) gspca_dev;
1311
1312 sd->colors = val;
1313 if (gspca_dev->streaming)
1314 setcolors(gspca_dev);
1315 return 0;
1316}
1317
1318static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
1319{
1320 struct sd *sd = (struct sd *) gspca_dev;
1321
1322 *val = sd->colors;
1323 return 0;
1324}
1325
1326static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val)
1327{
1328 struct sd *sd = (struct sd *) gspca_dev;
1329
1330 sd->gamma = val;
1331 if (gspca_dev->streaming)
1332 setgamma(gspca_dev);
1333 return 0;
1334}
1335
1336static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val)
1337{
1338 struct sd *sd = (struct sd *) gspca_dev;
1339
1340 *val = sd->gamma;
1341 return 0;
1342}
1343
1344static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
1345{ 872{
1346 struct sd *sd = (struct sd *) gspca_dev; 873 struct gspca_dev *gspca_dev =
1347 874 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1348 sd->freq = val; 875 struct sd *sd = (struct sd *)gspca_dev;
1349 if (gspca_dev->streaming) 876 s32 red_gain, blue_gain, green_gain;
1350 setfreq(gspca_dev); 877
878 gspca_dev->usb_err = 0;
879
880 switch (ctrl->id) {
881 case V4L2_CID_AUTO_WHITE_BALANCE:
882 red_gain = reg_r(gspca_dev, 0x0087);
883 if (red_gain > 0x40)
884 red_gain = 0x40;
885 else if (red_gain < 0x10)
886 red_gain = 0x10;
887
888 blue_gain = reg_r(gspca_dev, 0x0088);
889 if (blue_gain > 0x40)
890 blue_gain = 0x40;
891 else if (blue_gain < 0x10)
892 blue_gain = 0x10;
893
894 green_gain = reg_r(gspca_dev, 0x0089);
895 if (green_gain > 0x40)
896 green_gain = 0x40;
897 else if (green_gain < 0x10)
898 green_gain = 0x10;
899
900 sd->gain->val = green_gain;
901 sd->red_balance->val = red_gain - green_gain;
902 sd->blue_balance->val = blue_gain - green_gain;
903 break;
904 }
1351 return 0; 905 return 0;
1352} 906}
1353 907
1354static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val) 908static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1355{ 909{
1356 struct sd *sd = (struct sd *) gspca_dev; 910 struct gspca_dev *gspca_dev =
911 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1357 912
1358 *val = sd->freq; 913 gspca_dev->usb_err = 0;
1359 return 0;
1360}
1361 914
1362static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val) 915 if (!gspca_dev->streaming)
1363{ 916 return 0;
1364 struct sd *sd = (struct sd *) gspca_dev;
1365 917
1366 sd->sharpness = val; 918 switch (ctrl->id) {
1367 if (gspca_dev->streaming) 919 case V4L2_CID_BRIGHTNESS:
1368 setsharpness(gspca_dev); 920 setbrightness(gspca_dev, ctrl->val);
1369 return 0; 921 break;
922 case V4L2_CID_CONTRAST:
923 setcontrast(gspca_dev, ctrl->val);
924 break;
925 case V4L2_CID_SATURATION:
926 setcolors(gspca_dev, ctrl->val);
927 break;
928 case V4L2_CID_GAMMA:
929 setgamma(gspca_dev, ctrl->val);
930 break;
931 case V4L2_CID_HFLIP:
932 setmirror(gspca_dev, ctrl->val);
933 break;
934 case V4L2_CID_SHARPNESS:
935 setsharpness(gspca_dev, ctrl->val);
936 break;
937 case V4L2_CID_POWER_LINE_FREQUENCY:
938 setfreq(gspca_dev, ctrl->val);
939 break;
940 case V4L2_CID_BACKLIGHT_COMPENSATION:
941 reg_w(gspca_dev, ctrl->val ? 0xf48e : 0xb48e);
942 break;
943 case V4L2_CID_AUTO_WHITE_BALANCE:
944 setawb_n_RGB(gspca_dev);
945 break;
946 case V4L2_CID_COLORFX:
947 seteffect(gspca_dev, ctrl->val);
948 break;
949 }
950 return gspca_dev->usb_err;
1370} 951}
1371 952
1372static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val) 953static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1373{ 954 .g_volatile_ctrl = sd_g_volatile_ctrl,
1374 struct sd *sd = (struct sd *) gspca_dev; 955 .s_ctrl = sd_s_ctrl,
1375 956};
1376 *val = sd->sharpness;
1377 return 0;
1378}
1379 957
1380/* Low Light set here......*/ 958static int sd_init_controls(struct gspca_dev *gspca_dev)
1381static int sd_setlowlight(struct gspca_dev *gspca_dev, __s32 val)
1382{ 959{
1383 struct sd *sd = (struct sd *) gspca_dev; 960 struct sd *sd = (struct sd *)gspca_dev;
961 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
962
963 gspca_dev->vdev.ctrl_handler = hdl;
964 v4l2_ctrl_handler_init(hdl, 12);
965 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
966 V4L2_CID_BRIGHTNESS, 0, 14, 1, 8);
967 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
968 V4L2_CID_CONTRAST, 0, 0x0d, 1, 7);
969 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
970 V4L2_CID_SATURATION, 0, 0xf, 1, 5);
971 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
972 V4L2_CID_GAMMA, 0, GAMMA_MAX, 1, 10);
973 /* Activate lowlight, some apps dont bring up the
974 backlight_compensation control) */
975 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
976 V4L2_CID_BACKLIGHT_COMPENSATION, 0, 1, 1, 1);
977 if (sd->sensor == SENSOR_TAS5130A)
978 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
979 V4L2_CID_HFLIP, 0, 1, 1, 0);
980 sd->awb = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
981 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
982 sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
983 V4L2_CID_GAIN, 0x10, 0x40, 1, 0x20);
984 sd->blue_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
985 V4L2_CID_BLUE_BALANCE, -0x30, 0x30, 1, 0);
986 sd->red_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
987 V4L2_CID_RED_BALANCE, -0x30, 0x30, 1, 0);
988 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
989 V4L2_CID_SHARPNESS, 0, 15, 1, 6);
990 v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
991 V4L2_CID_COLORFX, V4L2_COLORFX_SKETCH,
992 ~((1 << V4L2_COLORFX_NONE) |
993 (1 << V4L2_COLORFX_BW) |
994 (1 << V4L2_COLORFX_SEPIA) |
995 (1 << V4L2_COLORFX_SKETCH) |
996 (1 << V4L2_COLORFX_NEGATIVE)),
997 V4L2_COLORFX_NONE);
998 sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
999 V4L2_CID_POWER_LINE_FREQUENCY,
1000 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 1,
1001 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
1002
1003 if (hdl->error) {
1004 pr_err("Could not initialize controls\n");
1005 return hdl->error;
1006 }
1384 1007
1385 sd->autogain = val; 1008 v4l2_ctrl_auto_cluster(4, &sd->awb, 0, true);
1386 if (val != 0)
1387 reg_w(gspca_dev, 0xf48e);
1388 else
1389 reg_w(gspca_dev, 0xb48e);
1390 return 0;
1391}
1392 1009
1393static int sd_getlowlight(struct gspca_dev *gspca_dev, __s32 *val)
1394{
1395 struct sd *sd = (struct sd *) gspca_dev;
1396
1397 *val = sd->autogain;
1398 return 0; 1010 return 0;
1399} 1011}
1400 1012
1401static int sd_querymenu(struct gspca_dev *gspca_dev,
1402 struct v4l2_querymenu *menu)
1403{
1404 static const char *freq_nm[3] = {"NoFliker", "50 Hz", "60 Hz"};
1405
1406 switch (menu->id) {
1407 case V4L2_CID_POWER_LINE_FREQUENCY:
1408 if ((unsigned) menu->index >= ARRAY_SIZE(freq_nm))
1409 break;
1410 strcpy((char *) menu->name, freq_nm[menu->index]);
1411 return 0;
1412 case V4L2_CID_EFFECTS:
1413 if ((unsigned) menu->index < ARRAY_SIZE(effects_control)) {
1414 strlcpy((char *) menu->name,
1415 effects_control[menu->index],
1416 sizeof menu->name);
1417 return 0;
1418 }
1419 break;
1420 }
1421 return -EINVAL;
1422}
1423
1424/* sub-driver description */ 1013/* sub-driver description */
1425static const struct sd_desc sd_desc = { 1014static const struct sd_desc sd_desc = {
1426 .name = MODULE_NAME, 1015 .name = MODULE_NAME,
1427 .ctrls = sd_ctrls,
1428 .nctrls = ARRAY_SIZE(sd_ctrls),
1429 .config = sd_config, 1016 .config = sd_config,
1430 .init = sd_init, 1017 .init = sd_init,
1018 .init_controls = sd_init_controls,
1431 .start = sd_start, 1019 .start = sd_start,
1432 .stopN = sd_stopN, 1020 .stopN = sd_stopN,
1433 .pkt_scan = sd_pkt_scan, 1021 .pkt_scan = sd_pkt_scan,
1434 .querymenu = sd_querymenu,
1435#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) 1022#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
1436 .other_input = 1, 1023 .other_input = 1,
1437#endif 1024#endif
@@ -1460,6 +1047,7 @@ static struct usb_driver sd_driver = {
1460#ifdef CONFIG_PM 1047#ifdef CONFIG_PM
1461 .suspend = gspca_suspend, 1048 .suspend = gspca_suspend,
1462 .resume = gspca_resume, 1049 .resume = gspca_resume,
1050 .reset_resume = gspca_resume,
1463#endif 1051#endif
1464}; 1052};
1465 1053
diff --git a/drivers/media/video/gspca/topro.c b/drivers/media/video/gspca/topro.c
index c6326d177a3d..a6055246cb9d 100644
--- a/drivers/media/video/gspca/topro.c
+++ b/drivers/media/video/gspca/topro.c
@@ -120,24 +120,13 @@ static const u8 jpeg_head[] = {
120#define JPEG_HDR_SZ 521 120#define JPEG_HDR_SZ 521
121}; 121};
122 122
123enum e_ctrl {
124 EXPOSURE,
125 QUALITY,
126 SHARPNESS,
127 RGAIN,
128 GAIN,
129 BGAIN,
130 GAMMA,
131 AUTOGAIN,
132 NCTRLS /* number of controls */
133};
134
135#define AUTOGAIN_DEF 1
136
137struct sd { 123struct sd {
138 struct gspca_dev gspca_dev; /* !! must be the first item */ 124 struct gspca_dev gspca_dev; /* !! must be the first item */
139 125 struct v4l2_ctrl *jpegqual;
140 struct gspca_ctrl ctrls[NCTRLS]; 126 struct v4l2_ctrl *sharpness;
127 struct v4l2_ctrl *gamma;
128 struct v4l2_ctrl *blue;
129 struct v4l2_ctrl *red;
141 130
142 u8 framerate; 131 u8 framerate;
143 u8 quality; /* webcam current JPEG quality (0..16) */ 132 u8 quality; /* webcam current JPEG quality (0..16) */
@@ -1415,32 +1404,33 @@ static void soi763a_6810_init(struct gspca_dev *gspca_dev)
1415} 1404}
1416 1405
1417/* set the gain and exposure */ 1406/* set the gain and exposure */
1418static void setexposure(struct gspca_dev *gspca_dev) 1407static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 gain,
1408 s32 blue, s32 red)
1419{ 1409{
1420 struct sd *sd = (struct sd *) gspca_dev; 1410 struct sd *sd = (struct sd *) gspca_dev;
1421 1411
1422 if (sd->sensor == SENSOR_CX0342) { 1412 if (sd->sensor == SENSOR_CX0342) {
1423 int expo; 1413 expo = (expo << 2) - 1;
1424
1425 expo = (sd->ctrls[EXPOSURE].val << 2) - 1;
1426 i2c_w(gspca_dev, CX0342_EXPO_LINE_L, expo); 1414 i2c_w(gspca_dev, CX0342_EXPO_LINE_L, expo);
1427 i2c_w(gspca_dev, CX0342_EXPO_LINE_H, expo >> 8); 1415 i2c_w(gspca_dev, CX0342_EXPO_LINE_H, expo >> 8);
1428 if (sd->bridge == BRIDGE_TP6800) 1416 if (sd->bridge == BRIDGE_TP6800)
1429 i2c_w(gspca_dev, CX0342_RAW_GBGAIN_H, 1417 i2c_w(gspca_dev, CX0342_RAW_GBGAIN_H,
1430 sd->ctrls[GAIN].val >> 8); 1418 gain >> 8);
1431 i2c_w(gspca_dev, CX0342_RAW_GBGAIN_L, sd->ctrls[GAIN].val); 1419 i2c_w(gspca_dev, CX0342_RAW_GBGAIN_L, gain);
1432 if (sd->bridge == BRIDGE_TP6800) 1420 if (sd->bridge == BRIDGE_TP6800)
1433 i2c_w(gspca_dev, CX0342_RAW_GRGAIN_H, 1421 i2c_w(gspca_dev, CX0342_RAW_GRGAIN_H,
1434 sd->ctrls[GAIN].val >> 8); 1422 gain >> 8);
1435 i2c_w(gspca_dev, CX0342_RAW_GRGAIN_L, sd->ctrls[GAIN].val); 1423 i2c_w(gspca_dev, CX0342_RAW_GRGAIN_L, gain);
1436 if (sd->bridge == BRIDGE_TP6800) 1424 if (sd->sensor == SENSOR_CX0342) {
1437 i2c_w(gspca_dev, CX0342_RAW_BGAIN_H, 1425 if (sd->bridge == BRIDGE_TP6800)
1438 sd->ctrls[BGAIN].val >> 8); 1426 i2c_w(gspca_dev, CX0342_RAW_BGAIN_H,
1439 i2c_w(gspca_dev, CX0342_RAW_BGAIN_L, sd->ctrls[BGAIN].val); 1427 blue >> 8);
1440 if (sd->bridge == BRIDGE_TP6800) 1428 i2c_w(gspca_dev, CX0342_RAW_BGAIN_L, blue);
1441 i2c_w(gspca_dev, CX0342_RAW_RGAIN_H, 1429 if (sd->bridge == BRIDGE_TP6800)
1442 sd->ctrls[RGAIN].val >> 8); 1430 i2c_w(gspca_dev, CX0342_RAW_RGAIN_H,
1443 i2c_w(gspca_dev, CX0342_RAW_RGAIN_L, sd->ctrls[RGAIN].val); 1431 red >> 8);
1432 i2c_w(gspca_dev, CX0342_RAW_RGAIN_L, red);
1433 }
1444 i2c_w(gspca_dev, CX0342_SYS_CTRL_0, 1434 i2c_w(gspca_dev, CX0342_SYS_CTRL_0,
1445 sd->bridge == BRIDGE_TP6800 ? 0x80 : 0x81); 1435 sd->bridge == BRIDGE_TP6800 ? 0x80 : 0x81);
1446 return; 1436 return;
@@ -1448,10 +1438,10 @@ static void setexposure(struct gspca_dev *gspca_dev)
1448 1438
1449 /* soi763a */ 1439 /* soi763a */
1450 i2c_w(gspca_dev, 0x10, /* AEC_H (exposure time) */ 1440 i2c_w(gspca_dev, 0x10, /* AEC_H (exposure time) */
1451 sd->ctrls[EXPOSURE].val); 1441 expo);
1452/* i2c_w(gspca_dev, 0x76, 0x02); * AEC_L ([1:0] */ 1442/* i2c_w(gspca_dev, 0x76, 0x02); * AEC_L ([1:0] */
1453 i2c_w(gspca_dev, 0x00, /* gain */ 1443 i2c_w(gspca_dev, 0x00, /* gain */
1454 sd->ctrls[GAIN].val); 1444 gain);
1455} 1445}
1456 1446
1457/* set the JPEG quantization tables */ 1447/* set the JPEG quantization tables */
@@ -1472,12 +1462,10 @@ static void set_dqt(struct gspca_dev *gspca_dev, u8 q)
1472} 1462}
1473 1463
1474/* set the JPEG compression quality factor */ 1464/* set the JPEG compression quality factor */
1475static void setquality(struct gspca_dev *gspca_dev) 1465static void setquality(struct gspca_dev *gspca_dev, s32 q)
1476{ 1466{
1477 struct sd *sd = (struct sd *) gspca_dev; 1467 struct sd *sd = (struct sd *) gspca_dev;
1478 u16 q;
1479 1468
1480 q = sd->ctrls[QUALITY].val;
1481 if (q != 16) 1469 if (q != 16)
1482 q = 15 - q; 1470 q = 15 - q;
1483 1471
@@ -1508,10 +1496,9 @@ static const u8 color_gain[NSENSORS][18] = {
1508 0xd5, 0x00, 0x46, 0x03, 0xdc, 0x03}, /* V R/G/B */ 1496 0xd5, 0x00, 0x46, 0x03, 0xdc, 0x03}, /* V R/G/B */
1509}; 1497};
1510 1498
1511static void setgamma(struct gspca_dev *gspca_dev) 1499static void setgamma(struct gspca_dev *gspca_dev, s32 gamma)
1512{ 1500{
1513 struct sd *sd = (struct sd *) gspca_dev; 1501 struct sd *sd = (struct sd *) gspca_dev;
1514 int gamma;
1515#define NGAMMA 6 1502#define NGAMMA 6
1516 static const u8 gamma_tb[NGAMMA][3][1024] = { 1503 static const u8 gamma_tb[NGAMMA][3][1024] = {
1517 { /* gamma 0 - from tp6800 + soi763a */ 1504 { /* gamma 0 - from tp6800 + soi763a */
@@ -3836,7 +3823,6 @@ static void setgamma(struct gspca_dev *gspca_dev)
3836 if (sd->bridge == BRIDGE_TP6810) 3823 if (sd->bridge == BRIDGE_TP6810)
3837 reg_w(gspca_dev, 0x02, 0x28); 3824 reg_w(gspca_dev, 0x02, 0x28);
3838/* msleep(50); */ 3825/* msleep(50); */
3839 gamma = sd->ctrls[GAMMA].val;
3840 bulk_w(gspca_dev, 0x00, gamma_tb[gamma][0], 1024); 3826 bulk_w(gspca_dev, 0x00, gamma_tb[gamma][0], 1024);
3841 bulk_w(gspca_dev, 0x01, gamma_tb[gamma][1], 1024); 3827 bulk_w(gspca_dev, 0x01, gamma_tb[gamma][1], 1024);
3842 bulk_w(gspca_dev, 0x02, gamma_tb[gamma][2], 1024); 3828 bulk_w(gspca_dev, 0x02, gamma_tb[gamma][2], 1024);
@@ -3864,43 +3850,35 @@ static void setgamma(struct gspca_dev *gspca_dev)
3864/* msleep(50); */ 3850/* msleep(50); */
3865} 3851}
3866 3852
3867static void setsharpness(struct gspca_dev *gspca_dev) 3853static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
3868{ 3854{
3869 struct sd *sd = (struct sd *) gspca_dev; 3855 struct sd *sd = (struct sd *) gspca_dev;
3870 u8 val;
3871 3856
3872 if (sd->bridge == BRIDGE_TP6800) { 3857 if (sd->bridge == BRIDGE_TP6800) {
3873 val = sd->ctrls[SHARPNESS].val 3858 val |= 0x08; /* grid compensation enable */
3874 | 0x08; /* grid compensation enable */
3875 if (gspca_dev->width == 640) 3859 if (gspca_dev->width == 640)
3876 reg_w(gspca_dev, TP6800_R78_FORMAT, 0x00); /* vga */ 3860 reg_w(gspca_dev, TP6800_R78_FORMAT, 0x00); /* vga */
3877 else 3861 else
3878 val |= 0x04; /* scaling down enable */ 3862 val |= 0x04; /* scaling down enable */
3879 reg_w(gspca_dev, TP6800_R5D_DEMOSAIC_CFG, val); 3863 reg_w(gspca_dev, TP6800_R5D_DEMOSAIC_CFG, val);
3880 } else { 3864 } else {
3881 val = (sd->ctrls[SHARPNESS].val << 5) | 0x08; 3865 val = (val << 5) | 0x08;
3882 reg_w(gspca_dev, 0x59, val); 3866 reg_w(gspca_dev, 0x59, val);
3883 } 3867 }
3884} 3868}
3885 3869
3886static void setautogain(struct gspca_dev *gspca_dev) 3870static void setautogain(struct gspca_dev *gspca_dev, s32 val)
3887{ 3871{
3888 struct sd *sd = (struct sd *) gspca_dev; 3872 struct sd *sd = (struct sd *) gspca_dev;
3889 3873
3890 if (gspca_dev->ctrl_dis & (1 << AUTOGAIN)) 3874 sd->ag_cnt = val ? AG_CNT_START : -1;
3891 return;
3892 if (sd->ctrls[AUTOGAIN].val) {
3893 sd->ag_cnt = AG_CNT_START;
3894 gspca_dev->ctrl_inac |= (1 << EXPOSURE) | (1 << GAIN);
3895 } else {
3896 sd->ag_cnt = -1;
3897 gspca_dev->ctrl_inac &= ~((1 << EXPOSURE) | (1 << GAIN));
3898 }
3899} 3875}
3900 3876
3901/* set the resolution for sensor cx0342 */ 3877/* set the resolution for sensor cx0342 */
3902static void set_resolution(struct gspca_dev *gspca_dev) 3878static void set_resolution(struct gspca_dev *gspca_dev)
3903{ 3879{
3880 struct sd *sd = (struct sd *) gspca_dev;
3881
3904 reg_w(gspca_dev, TP6800_R21_ENDP_1_CTL, 0x00); 3882 reg_w(gspca_dev, TP6800_R21_ENDP_1_CTL, 0x00);
3905 if (gspca_dev->width == 320) { 3883 if (gspca_dev->width == 320) {
3906 reg_w(gspca_dev, TP6800_R3F_FRAME_RATE, 0x06); 3884 reg_w(gspca_dev, TP6800_R3F_FRAME_RATE, 0x06);
@@ -3926,8 +3904,9 @@ static void set_resolution(struct gspca_dev *gspca_dev)
3926 i2c_w(gspca_dev, CX0342_SYS_CTRL_0, 0x01); 3904 i2c_w(gspca_dev, CX0342_SYS_CTRL_0, 0x01);
3927 bulk_w(gspca_dev, 0x03, color_gain[SENSOR_CX0342], 3905 bulk_w(gspca_dev, 0x03, color_gain[SENSOR_CX0342],
3928 ARRAY_SIZE(color_gain[0])); 3906 ARRAY_SIZE(color_gain[0]));
3929 setgamma(gspca_dev); 3907 setgamma(gspca_dev, v4l2_ctrl_g_ctrl(sd->gamma));
3930 setquality(gspca_dev); 3908 if (sd->sensor == SENSOR_SOI763A)
3909 setquality(gspca_dev, v4l2_ctrl_g_ctrl(sd->jpegqual));
3931} 3910}
3932 3911
3933/* convert the frame rate to a tp68x0 value */ 3912/* convert the frame rate to a tp68x0 value */
@@ -3963,7 +3942,7 @@ static int get_fr_idx(struct gspca_dev *gspca_dev)
3963 return i; 3942 return i;
3964} 3943}
3965 3944
3966static void setframerate(struct gspca_dev *gspca_dev) 3945static void setframerate(struct gspca_dev *gspca_dev, s32 val)
3967{ 3946{
3968 struct sd *sd = (struct sd *) gspca_dev; 3947 struct sd *sd = (struct sd *) gspca_dev;
3969 u8 fr_idx; 3948 u8 fr_idx;
@@ -3974,7 +3953,7 @@ static void setframerate(struct gspca_dev *gspca_dev)
3974 reg_r(gspca_dev, 0x7b); 3953 reg_r(gspca_dev, 0x7b);
3975 reg_w(gspca_dev, 0x7b, 3954 reg_w(gspca_dev, 0x7b,
3976 sd->sensor == SENSOR_CX0342 ? 0x10 : 0x90); 3955 sd->sensor == SENSOR_CX0342 ? 0x10 : 0x90);
3977 if (sd->ctrls[EXPOSURE].val >= 128) 3956 if (val >= 128)
3978 fr_idx = 0xf0; /* lower frame rate */ 3957 fr_idx = 0xf0; /* lower frame rate */
3979 } 3958 }
3980 3959
@@ -3984,43 +3963,43 @@ static void setframerate(struct gspca_dev *gspca_dev)
3984 i2c_w(gspca_dev, CX0342_AUTO_ADC_CALIB, 0x01); 3963 i2c_w(gspca_dev, CX0342_AUTO_ADC_CALIB, 0x01);
3985} 3964}
3986 3965
3987static void setrgain(struct gspca_dev *gspca_dev) 3966static void setrgain(struct gspca_dev *gspca_dev, s32 rgain)
3988{ 3967{
3989 struct sd *sd = (struct sd *) gspca_dev;
3990 int rgain;
3991
3992 rgain = sd->ctrls[RGAIN].val;
3993 i2c_w(gspca_dev, CX0342_RAW_RGAIN_H, rgain >> 8); 3968 i2c_w(gspca_dev, CX0342_RAW_RGAIN_H, rgain >> 8);
3994 i2c_w(gspca_dev, CX0342_RAW_RGAIN_L, rgain); 3969 i2c_w(gspca_dev, CX0342_RAW_RGAIN_L, rgain);
3995 i2c_w(gspca_dev, CX0342_SYS_CTRL_0, 0x80); 3970 i2c_w(gspca_dev, CX0342_SYS_CTRL_0, 0x80);
3996} 3971}
3997 3972
3998static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val) 3973static int sd_setgain(struct gspca_dev *gspca_dev)
3999{ 3974{
4000 struct sd *sd = (struct sd *) gspca_dev; 3975 struct sd *sd = (struct sd *) gspca_dev;
3976 s32 val = gspca_dev->gain->val;
4001 3977
4002 if (sd->sensor == SENSOR_CX0342) { 3978 if (sd->sensor == SENSOR_CX0342) {
4003 sd->ctrls[BGAIN].val = sd->ctrls[BGAIN].val 3979 s32 old = gspca_dev->gain->cur.val ?
4004 * val / sd->ctrls[GAIN].val; 3980 gspca_dev->gain->cur.val : 1;
4005 if (sd->ctrls[BGAIN].val > 4095) 3981
4006 sd->ctrls[BGAIN].val = 4095; 3982 sd->blue->val = sd->blue->val * val / old;
4007 sd->ctrls[RGAIN].val = sd->ctrls[RGAIN].val 3983 if (sd->blue->val > 4095)
4008 * val / sd->ctrls[GAIN].val; 3984 sd->blue->val = 4095;
4009 if (sd->ctrls[RGAIN].val > 4095) 3985 sd->red->val = sd->red->val * val / old;
4010 sd->ctrls[RGAIN].val = 4095; 3986 if (sd->red->val > 4095)
3987 sd->red->val = 4095;
3988 }
3989 if (gspca_dev->streaming) {
3990 if (sd->sensor == SENSOR_CX0342)
3991 setexposure(gspca_dev, gspca_dev->exposure->val,
3992 gspca_dev->gain->val,
3993 sd->blue->val, sd->red->val);
3994 else
3995 setexposure(gspca_dev, gspca_dev->exposure->val,
3996 gspca_dev->gain->val, 0, 0);
4011 } 3997 }
4012 sd->ctrls[GAIN].val = val;
4013 if (gspca_dev->streaming)
4014 setexposure(gspca_dev);
4015 return gspca_dev->usb_err; 3998 return gspca_dev->usb_err;
4016} 3999}
4017 4000
4018static void setbgain(struct gspca_dev *gspca_dev) 4001static void setbgain(struct gspca_dev *gspca_dev, s32 bgain)
4019{ 4002{
4020 struct sd *sd = (struct sd *) gspca_dev;
4021 int bgain;
4022
4023 bgain = sd->ctrls[BGAIN].val;
4024 i2c_w(gspca_dev, CX0342_RAW_BGAIN_H, bgain >> 8); 4003 i2c_w(gspca_dev, CX0342_RAW_BGAIN_H, bgain >> 8);
4025 i2c_w(gspca_dev, CX0342_RAW_BGAIN_L, bgain); 4004 i2c_w(gspca_dev, CX0342_RAW_BGAIN_L, bgain);
4026 i2c_w(gspca_dev, CX0342_SYS_CTRL_0, 0x80); 4005 i2c_w(gspca_dev, CX0342_SYS_CTRL_0, 0x80);
@@ -4040,7 +4019,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
4040 framerates : framerates_6810; 4019 framerates : framerates_6810;
4041 4020
4042 sd->framerate = 30; /* default: 30 fps */ 4021 sd->framerate = 30; /* default: 30 fps */
4043 gspca_dev->cam.ctrls = sd->ctrls;
4044 return 0; 4022 return 0;
4045} 4023}
4046 4024
@@ -4108,32 +4086,16 @@ static int sd_init(struct gspca_dev *gspca_dev)
4108 } 4086 }
4109 if (sd->sensor == SENSOR_SOI763A) { 4087 if (sd->sensor == SENSOR_SOI763A) {
4110 pr_info("Sensor soi763a\n"); 4088 pr_info("Sensor soi763a\n");
4111 sd->ctrls[GAMMA].def = sd->bridge == BRIDGE_TP6800 ? 0 : 1;
4112 sd->ctrls[GAIN].max = 15;
4113 sd->ctrls[GAIN].def = 3;
4114 gspca_dev->ctrl_dis = (1 << RGAIN) | (1 << BGAIN);
4115 if (sd->bridge == BRIDGE_TP6810) { 4089 if (sd->bridge == BRIDGE_TP6810) {
4116 soi763a_6810_init(gspca_dev); 4090 soi763a_6810_init(gspca_dev);
4117#if AUTOGAIN_DEF
4118 gspca_dev->ctrl_inac |= (1 << EXPOSURE) | (1 << GAIN);
4119#endif
4120 } else {
4121 gspca_dev->ctrl_dis |= (1 << AUTOGAIN);
4122 } 4091 }
4123 } else { 4092 } else {
4124 pr_info("Sensor cx0342\n"); 4093 pr_info("Sensor cx0342\n");
4125 if (sd->bridge == BRIDGE_TP6810) { 4094 if (sd->bridge == BRIDGE_TP6810) {
4126 cx0342_6810_init(gspca_dev); 4095 cx0342_6810_init(gspca_dev);
4127#if AUTOGAIN_DEF
4128 gspca_dev->ctrl_inac |= (1 << EXPOSURE) | (1 << GAIN);
4129#endif
4130 } else {
4131 gspca_dev->ctrl_dis |= (1 << AUTOGAIN);
4132 } 4096 }
4133 } 4097 }
4134 4098
4135 if (sd->bridge == BRIDGE_TP6810)
4136 sd->ctrls[QUALITY].def = 0; /* auto quality */
4137 set_dqt(gspca_dev, 0); 4099 set_dqt(gspca_dev, 0);
4138 return 0; 4100 return 0;
4139} 4101}
@@ -4207,8 +4169,9 @@ static void set_led(struct gspca_dev *gspca_dev, int on)
4207 4169
4208static void cx0342_6800_start(struct gspca_dev *gspca_dev) 4170static void cx0342_6800_start(struct gspca_dev *gspca_dev)
4209{ 4171{
4172 struct sd *sd = (struct sd *) gspca_dev;
4210 static const struct cmd reg_init[] = { 4173 static const struct cmd reg_init[] = {
4211/*fixme: is this usefull?*/ 4174 /* fixme: is this useful? */
4212 {TP6800_R17_GPIO_IO, 0x9f}, 4175 {TP6800_R17_GPIO_IO, 0x9f},
4213 {TP6800_R16_GPIO_PD, 0x40}, 4176 {TP6800_R16_GPIO_PD, 0x40},
4214 {TP6800_R10_SIF_TYPE, 0x00}, /* i2c 8 bits */ 4177 {TP6800_R10_SIF_TYPE, 0x00}, /* i2c 8 bits */
@@ -4279,13 +4242,21 @@ static void cx0342_6800_start(struct gspca_dev *gspca_dev)
4279 reg_w(gspca_dev, TP6800_R54_DARK_CFG, 0x00); 4242 reg_w(gspca_dev, TP6800_R54_DARK_CFG, 0x00);
4280 i2c_w(gspca_dev, CX0342_EXPO_LINE_H, 0x00); 4243 i2c_w(gspca_dev, CX0342_EXPO_LINE_H, 0x00);
4281 i2c_w(gspca_dev, CX0342_SYS_CTRL_0, 0x01); 4244 i2c_w(gspca_dev, CX0342_SYS_CTRL_0, 0x01);
4282 setexposure(gspca_dev); 4245 if (sd->sensor == SENSOR_CX0342)
4246 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure),
4247 v4l2_ctrl_g_ctrl(gspca_dev->gain),
4248 v4l2_ctrl_g_ctrl(sd->blue),
4249 v4l2_ctrl_g_ctrl(sd->red));
4250 else
4251 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure),
4252 v4l2_ctrl_g_ctrl(gspca_dev->gain), 0, 0);
4283 set_led(gspca_dev, 1); 4253 set_led(gspca_dev, 1);
4284 set_resolution(gspca_dev); 4254 set_resolution(gspca_dev);
4285} 4255}
4286 4256
4287static void cx0342_6810_start(struct gspca_dev *gspca_dev) 4257static void cx0342_6810_start(struct gspca_dev *gspca_dev)
4288{ 4258{
4259 struct sd *sd = (struct sd *) gspca_dev;
4289 static const struct cmd sensor_init_2[] = { 4260 static const struct cmd sensor_init_2[] = {
4290 {CX0342_EXPO_LINE_L, 0x6f}, 4261 {CX0342_EXPO_LINE_L, 0x6f},
4291 {CX0342_EXPO_LINE_H, 0x02}, 4262 {CX0342_EXPO_LINE_H, 0x02},
@@ -4366,10 +4337,10 @@ static void cx0342_6810_start(struct gspca_dev *gspca_dev)
4366 reg_w(gspca_dev, 0x07, 0x85); 4337 reg_w(gspca_dev, 0x07, 0x85);
4367 reg_w(gspca_dev, TP6800_R78_FORMAT, 0x01); /* qvga */ 4338 reg_w(gspca_dev, TP6800_R78_FORMAT, 0x01); /* qvga */
4368 } 4339 }
4369 setgamma(gspca_dev); 4340 setgamma(gspca_dev, v4l2_ctrl_g_ctrl(sd->gamma));
4370 reg_w_buf(gspca_dev, tp6810_bridge_start, 4341 reg_w_buf(gspca_dev, tp6810_bridge_start,
4371 ARRAY_SIZE(tp6810_bridge_start)); 4342 ARRAY_SIZE(tp6810_bridge_start));
4372 setsharpness(gspca_dev); 4343 setsharpness(gspca_dev, v4l2_ctrl_g_ctrl(sd->sharpness));
4373 bulk_w(gspca_dev, 0x03, color_gain[SENSOR_CX0342], 4344 bulk_w(gspca_dev, 0x03, color_gain[SENSOR_CX0342],
4374 ARRAY_SIZE(color_gain[0])); 4345 ARRAY_SIZE(color_gain[0]));
4375 reg_w(gspca_dev, TP6800_R3F_FRAME_RATE, 0x87); 4346 reg_w(gspca_dev, TP6800_R3F_FRAME_RATE, 0x87);
@@ -4380,11 +4351,12 @@ static void cx0342_6810_start(struct gspca_dev *gspca_dev)
4380 i2c_w_buf(gspca_dev, sensor_init_5, ARRAY_SIZE(sensor_init_5)); 4351 i2c_w_buf(gspca_dev, sensor_init_5, ARRAY_SIZE(sensor_init_5));
4381 4352
4382 set_led(gspca_dev, 1); 4353 set_led(gspca_dev, 1);
4383/* setquality(gspca_dev); */ 4354/* setquality(gspca_dev, v4l2_ctrl_g_ctrl(sd->jpegqual)); */
4384} 4355}
4385 4356
4386static void soi763a_6800_start(struct gspca_dev *gspca_dev) 4357static void soi763a_6800_start(struct gspca_dev *gspca_dev)
4387{ 4358{
4359 struct sd *sd = (struct sd *) gspca_dev;
4388 static const struct cmd reg_init[] = { 4360 static const struct cmd reg_init[] = {
4389 {TP6800_R79_QUALITY, 0x04}, 4361 {TP6800_R79_QUALITY, 0x04},
4390 {TP6800_R79_QUALITY, 0x01}, 4362 {TP6800_R79_QUALITY, 0x01},
@@ -4484,19 +4456,28 @@ static void soi763a_6800_start(struct gspca_dev *gspca_dev)
4484 reg_w(gspca_dev, TP6800_R5C_EDGE_THRLD, 0x10); 4456 reg_w(gspca_dev, TP6800_R5C_EDGE_THRLD, 0x10);
4485 reg_w(gspca_dev, TP6800_R54_DARK_CFG, 0x00); 4457 reg_w(gspca_dev, TP6800_R54_DARK_CFG, 0x00);
4486 4458
4487 setsharpness(gspca_dev); 4459 setsharpness(gspca_dev, v4l2_ctrl_g_ctrl(sd->sharpness));
4488 4460
4489 bulk_w(gspca_dev, 0x03, color_gain[SENSOR_SOI763A], 4461 bulk_w(gspca_dev, 0x03, color_gain[SENSOR_SOI763A],
4490 ARRAY_SIZE(color_gain[0])); 4462 ARRAY_SIZE(color_gain[0]));
4491 4463
4492 set_led(gspca_dev, 1); 4464 set_led(gspca_dev, 1);
4493 setexposure(gspca_dev); 4465 if (sd->sensor == SENSOR_CX0342)
4494 setquality(gspca_dev); 4466 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure),
4495 setgamma(gspca_dev); 4467 v4l2_ctrl_g_ctrl(gspca_dev->gain),
4468 v4l2_ctrl_g_ctrl(sd->blue),
4469 v4l2_ctrl_g_ctrl(sd->red));
4470 else
4471 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure),
4472 v4l2_ctrl_g_ctrl(gspca_dev->gain), 0, 0);
4473 if (sd->sensor == SENSOR_SOI763A)
4474 setquality(gspca_dev, v4l2_ctrl_g_ctrl(sd->jpegqual));
4475 setgamma(gspca_dev, v4l2_ctrl_g_ctrl(sd->gamma));
4496} 4476}
4497 4477
4498static void soi763a_6810_start(struct gspca_dev *gspca_dev) 4478static void soi763a_6810_start(struct gspca_dev *gspca_dev)
4499{ 4479{
4480 struct sd *sd = (struct sd *) gspca_dev;
4500 static const struct cmd bridge_init_2[] = { 4481 static const struct cmd bridge_init_2[] = {
4501 {TP6800_R7A_BLK_THRLD, 0x00}, 4482 {TP6800_R7A_BLK_THRLD, 0x00},
4502 {TP6800_R79_QUALITY, 0x04}, 4483 {TP6800_R79_QUALITY, 0x04},
@@ -4520,7 +4501,14 @@ static void soi763a_6810_start(struct gspca_dev *gspca_dev)
4520 reg_w(gspca_dev, 0x22, gspca_dev->alt); 4501 reg_w(gspca_dev, 0x22, gspca_dev->alt);
4521 bulk_w(gspca_dev, 0x03, color_null, sizeof color_null); 4502 bulk_w(gspca_dev, 0x03, color_null, sizeof color_null);
4522 reg_w(gspca_dev, 0x59, 0x40); 4503 reg_w(gspca_dev, 0x59, 0x40);
4523 setexposure(gspca_dev); 4504 if (sd->sensor == SENSOR_CX0342)
4505 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure),
4506 v4l2_ctrl_g_ctrl(gspca_dev->gain),
4507 v4l2_ctrl_g_ctrl(sd->blue),
4508 v4l2_ctrl_g_ctrl(sd->red));
4509 else
4510 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure),
4511 v4l2_ctrl_g_ctrl(gspca_dev->gain), 0, 0);
4524 reg_w_buf(gspca_dev, bridge_init_2, ARRAY_SIZE(bridge_init_2)); 4512 reg_w_buf(gspca_dev, bridge_init_2, ARRAY_SIZE(bridge_init_2));
4525 reg_w_buf(gspca_dev, tp6810_ov_init_common, 4513 reg_w_buf(gspca_dev, tp6810_ov_init_common,
4526 ARRAY_SIZE(tp6810_ov_init_common)); 4514 ARRAY_SIZE(tp6810_ov_init_common));
@@ -4534,7 +4522,7 @@ static void soi763a_6810_start(struct gspca_dev *gspca_dev)
4534 reg_w(gspca_dev, 0x07, 0x85); 4522 reg_w(gspca_dev, 0x07, 0x85);
4535 reg_w(gspca_dev, TP6800_R78_FORMAT, 0x01); /* qvga */ 4523 reg_w(gspca_dev, TP6800_R78_FORMAT, 0x01); /* qvga */
4536 } 4524 }
4537 setgamma(gspca_dev); 4525 setgamma(gspca_dev, v4l2_ctrl_g_ctrl(sd->gamma));
4538 reg_w_buf(gspca_dev, tp6810_bridge_start, 4526 reg_w_buf(gspca_dev, tp6810_bridge_start,
4539 ARRAY_SIZE(tp6810_bridge_start)); 4527 ARRAY_SIZE(tp6810_bridge_start));
4540 4528
@@ -4545,12 +4533,19 @@ static void soi763a_6810_start(struct gspca_dev *gspca_dev)
4545 4533
4546 reg_w(gspca_dev, 0x00, 0x00); 4534 reg_w(gspca_dev, 0x00, 0x00);
4547 4535
4548 setsharpness(gspca_dev); 4536 setsharpness(gspca_dev, v4l2_ctrl_g_ctrl(sd->sharpness));
4549 bulk_w(gspca_dev, 0x03, color_gain[SENSOR_SOI763A], 4537 bulk_w(gspca_dev, 0x03, color_gain[SENSOR_SOI763A],
4550 ARRAY_SIZE(color_gain[0])); 4538 ARRAY_SIZE(color_gain[0]));
4551 set_led(gspca_dev, 1); 4539 set_led(gspca_dev, 1);
4552 reg_w(gspca_dev, TP6800_R3F_FRAME_RATE, 0xf0); 4540 reg_w(gspca_dev, TP6800_R3F_FRAME_RATE, 0xf0);
4553 setexposure(gspca_dev); 4541 if (sd->sensor == SENSOR_CX0342)
4542 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure),
4543 v4l2_ctrl_g_ctrl(gspca_dev->gain),
4544 v4l2_ctrl_g_ctrl(sd->blue),
4545 v4l2_ctrl_g_ctrl(sd->red));
4546 else
4547 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure),
4548 v4l2_ctrl_g_ctrl(gspca_dev->gain), 0, 0);
4554 reg_w_buf(gspca_dev, bridge_init_6, ARRAY_SIZE(bridge_init_6)); 4549 reg_w_buf(gspca_dev, bridge_init_6, ARRAY_SIZE(bridge_init_6));
4555} 4550}
4556 4551
@@ -4576,12 +4571,25 @@ static int sd_start(struct gspca_dev *gspca_dev)
4576 reg_w(gspca_dev, 0x80, 0x03); 4571 reg_w(gspca_dev, 0x80, 0x03);
4577 reg_w(gspca_dev, 0x82, gspca_dev->curr_mode ? 0x0a : 0x0e); 4572 reg_w(gspca_dev, 0x82, gspca_dev->curr_mode ? 0x0a : 0x0e);
4578 4573
4579 setexposure(gspca_dev); 4574 if (sd->sensor == SENSOR_CX0342)
4580 setquality(gspca_dev); 4575 setexposure(gspca_dev,
4581 setautogain(gspca_dev); 4576 v4l2_ctrl_g_ctrl(gspca_dev->exposure),
4577 v4l2_ctrl_g_ctrl(gspca_dev->gain),
4578 v4l2_ctrl_g_ctrl(sd->blue),
4579 v4l2_ctrl_g_ctrl(sd->red));
4580 else
4581 setexposure(gspca_dev,
4582 v4l2_ctrl_g_ctrl(gspca_dev->exposure),
4583 v4l2_ctrl_g_ctrl(gspca_dev->gain), 0, 0);
4584 if (sd->sensor == SENSOR_SOI763A)
4585 setquality(gspca_dev,
4586 v4l2_ctrl_g_ctrl(sd->jpegqual));
4587 if (sd->bridge == BRIDGE_TP6810)
4588 setautogain(gspca_dev,
4589 v4l2_ctrl_g_ctrl(gspca_dev->autogain));
4582 } 4590 }
4583 4591
4584 setframerate(gspca_dev); 4592 setframerate(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure));
4585 4593
4586 return gspca_dev->usb_err; 4594 return gspca_dev->usb_err;
4587} 4595}
@@ -4672,12 +4680,6 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
4672 } 4680 }
4673} 4681}
4674 4682
4675/* -- do autogain -- */
4676/* gain setting is done in setexposure() for tp6810 */
4677static void setgain(struct gspca_dev *gspca_dev) {}
4678#define WANT_REGULAR_AUTOGAIN
4679#include "autogain_functions.h"
4680
4681static void sd_dq_callback(struct gspca_dev *gspca_dev) 4683static void sd_dq_callback(struct gspca_dev *gspca_dev)
4682{ 4684{
4683 struct sd *sd = (struct sd *) gspca_dev; 4685 struct sd *sd = (struct sd *) gspca_dev;
@@ -4739,17 +4741,19 @@ static void sd_dq_callback(struct gspca_dev *gspca_dev)
4739 luma /= 4; 4741 luma /= 4;
4740 reg_w(gspca_dev, 0x7d, 0x00); 4742 reg_w(gspca_dev, 0x7d, 0x00);
4741 4743
4742 expo = sd->ctrls[EXPOSURE].val; 4744 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
4743 ret = auto_gain_n_exposure(gspca_dev, luma, 4745 ret = gspca_expo_autogain(gspca_dev, luma,
4744 60, /* desired luma */ 4746 60, /* desired luma */
4745 6, /* dead zone */ 4747 6, /* dead zone */
4746 2, /* gain knee */ 4748 2, /* gain knee */
4747 70); /* expo knee */ 4749 70); /* expo knee */
4748 sd->ag_cnt = AG_CNT_START; 4750 sd->ag_cnt = AG_CNT_START;
4749 if (sd->bridge == BRIDGE_TP6810) { 4751 if (sd->bridge == BRIDGE_TP6810) {
4750 if ((expo >= 128 && sd->ctrls[EXPOSURE].val < 128) 4752 int new_expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
4751 || (expo < 128 && sd->ctrls[EXPOSURE].val >= 128)) 4753
4752 setframerate(gspca_dev); 4754 if ((expo >= 128 && new_expo < 128)
4755 || (expo < 128 && new_expo >= 128))
4756 setframerate(gspca_dev, new_expo);
4753 } 4757 }
4754 break; 4758 break;
4755 } 4759 }
@@ -4789,7 +4793,7 @@ static void sd_set_streamparm(struct gspca_dev *gspca_dev,
4789 4793
4790 sd->framerate = tpf->denominator / tpf->numerator; 4794 sd->framerate = tpf->denominator / tpf->numerator;
4791 if (gspca_dev->streaming) 4795 if (gspca_dev->streaming)
4792 setframerate(gspca_dev); 4796 setframerate(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure));
4793 4797
4794 /* Return the actual framerate */ 4798 /* Return the actual framerate */
4795 i = get_fr_idx(gspca_dev); 4799 i = get_fr_idx(gspca_dev);
@@ -4806,12 +4810,10 @@ static int sd_set_jcomp(struct gspca_dev *gspca_dev,
4806{ 4810{
4807 struct sd *sd = (struct sd *) gspca_dev; 4811 struct sd *sd = (struct sd *) gspca_dev;
4808 4812
4809 if (sd->sensor == SENSOR_SOI763A) 4813 if (sd->sensor != SENSOR_SOI763A)
4810 jpeg_set_qual(sd->jpeg_hdr, jcomp->quality); 4814 return -ENOTTY;
4811/* else 4815 v4l2_ctrl_s_ctrl(sd->jpegqual, jcomp->quality);
4812 fixme: TODO 4816 return 0;
4813*/
4814 return gspca_dev->usb_err;
4815} 4817}
4816 4818
4817static int sd_get_jcomp(struct gspca_dev *gspca_dev, 4819static int sd_get_jcomp(struct gspca_dev *gspca_dev,
@@ -4819,118 +4821,109 @@ static int sd_get_jcomp(struct gspca_dev *gspca_dev,
4819{ 4821{
4820 struct sd *sd = (struct sd *) gspca_dev; 4822 struct sd *sd = (struct sd *) gspca_dev;
4821 4823
4824 if (sd->sensor != SENSOR_SOI763A)
4825 return -ENOTTY;
4822 memset(jcomp, 0, sizeof *jcomp); 4826 memset(jcomp, 0, sizeof *jcomp);
4823 jcomp->quality = jpeg_q[sd->quality]; 4827 jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
4824 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT 4828 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
4825 | V4L2_JPEG_MARKER_DQT; 4829 | V4L2_JPEG_MARKER_DQT;
4826 return 0; 4830 return 0;
4827} 4831}
4828 4832
4829static struct ctrl sd_ctrls[NCTRLS] = { 4833static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
4830[EXPOSURE] = { 4834{
4831 { 4835 struct gspca_dev *gspca_dev =
4832 .id = V4L2_CID_EXPOSURE, 4836 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
4833 .type = V4L2_CTRL_TYPE_INTEGER, 4837 struct sd *sd = (struct sd *)gspca_dev;
4834 .name = "Exposure", 4838
4835 .minimum = 0x01, 4839 gspca_dev->usb_err = 0;
4836 .maximum = 0xdc, 4840
4837 .step = 1, 4841 if (!gspca_dev->streaming)
4838 .default_value = 0x4e, 4842 return 0;
4839 }, 4843
4840 .set_control = setexposure 4844 switch (ctrl->id) {
4841 }, 4845 case V4L2_CID_SHARPNESS:
4842[QUALITY] = { 4846 setsharpness(gspca_dev, ctrl->val);
4843 { 4847 break;
4844 .id = V4L2_CID_PRIVATE_BASE, 4848 case V4L2_CID_GAMMA:
4845 .type = V4L2_CTRL_TYPE_INTEGER, 4849 setgamma(gspca_dev, ctrl->val);
4846 .name = "Compression quality", 4850 break;
4847 .minimum = 0, 4851 case V4L2_CID_BLUE_BALANCE:
4848 .maximum = 15, 4852 setbgain(gspca_dev, ctrl->val);
4849 .step = 1, 4853 break;
4850 .default_value = 13, 4854 case V4L2_CID_RED_BALANCE:
4851 }, 4855 setrgain(gspca_dev, ctrl->val);
4852 .set_control = setquality 4856 break;
4853 }, 4857 case V4L2_CID_EXPOSURE:
4854[RGAIN] = { 4858 sd_setgain(gspca_dev);
4855 { 4859 break;
4856 .id = V4L2_CID_RED_BALANCE, 4860 case V4L2_CID_AUTOGAIN:
4857 .type = V4L2_CTRL_TYPE_INTEGER, 4861 if (ctrl->val)
4858 .name = "Red balance", 4862 break;
4859 .minimum = 0, 4863 sd_setgain(gspca_dev);
4860 .maximum = 4095, 4864 break;
4861 .step = 1, 4865 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
4862 .default_value = 256, 4866 jpeg_set_qual(sd->jpeg_hdr, ctrl->val);
4863 }, 4867 break;
4864 .set_control = setrgain 4868 }
4865 }, 4869 return gspca_dev->usb_err;
4866[GAIN] = { 4870}
4867 { 4871
4868 .id = V4L2_CID_GAIN, 4872static const struct v4l2_ctrl_ops sd_ctrl_ops = {
4869 .type = V4L2_CTRL_TYPE_INTEGER, 4873 .s_ctrl = sd_s_ctrl,
4870 .name = "Gain",
4871 .minimum = 0,
4872 .maximum = 4095,
4873 .step = 1,
4874 .default_value = 256,
4875 },
4876 .set = sd_setgain
4877 },
4878[BGAIN] = {
4879 {
4880 .id = V4L2_CID_BLUE_BALANCE,
4881 .type = V4L2_CTRL_TYPE_INTEGER,
4882 .name = "Blue balance",
4883 .minimum = 0,
4884 .maximum = 4095,
4885 .step = 1,
4886 .default_value = 256,
4887 },
4888 .set_control = setbgain
4889 },
4890[SHARPNESS] = {
4891 {
4892 .id = V4L2_CID_SHARPNESS,
4893 .type = V4L2_CTRL_TYPE_INTEGER,
4894 .name = "Sharpness",
4895 .minimum = 0,
4896 .maximum = 3,
4897 .step = 1,
4898 .default_value = 2,
4899 },
4900 .set_control = setsharpness
4901 },
4902[GAMMA] = {
4903 {
4904 .id = V4L2_CID_GAMMA,
4905 .type = V4L2_CTRL_TYPE_INTEGER,
4906 .name = "Gamma",
4907 .minimum = 0,
4908 .maximum = NGAMMA - 1,
4909 .step = 1,
4910 .default_value = 1,
4911 },
4912 .set_control = setgamma
4913 },
4914[AUTOGAIN] = {
4915 {
4916 .id = V4L2_CID_AUTOGAIN,
4917 .type = V4L2_CTRL_TYPE_BOOLEAN,
4918 .name = "Auto Gain",
4919 .minimum = 0,
4920 .maximum = 1,
4921 .step = 1,
4922 .default_value = AUTOGAIN_DEF
4923 },
4924 .set_control = setautogain
4925 },
4926}; 4874};
4927 4875
4876static int sd_init_controls(struct gspca_dev *gspca_dev)
4877{
4878 struct sd *sd = (struct sd *)gspca_dev;
4879 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
4880
4881 gspca_dev->vdev.ctrl_handler = hdl;
4882 v4l2_ctrl_handler_init(hdl, 4);
4883 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4884 V4L2_CID_EXPOSURE, 1, 0xdc, 1, 0x4e);
4885 if (sd->sensor == SENSOR_CX0342) {
4886 sd->red = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4887 V4L2_CID_RED_BALANCE, 0, 4095, 1, 256);
4888 sd->blue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4889 V4L2_CID_BLUE_BALANCE, 0, 4095, 1, 256);
4890 }
4891 if (sd->sensor == SENSOR_SOI763A)
4892 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4893 V4L2_CID_GAIN, 0, 15, 1, 3);
4894 else
4895 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4896 V4L2_CID_GAIN, 0, 4095, 1, 256);
4897 sd->sharpness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4898 V4L2_CID_SHARPNESS, 0, 3, 1, 2);
4899 sd->gamma = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4900 V4L2_CID_GAMMA, 0, NGAMMA - 1, 1,
4901 (sd->sensor == SENSOR_SOI763A &&
4902 sd->bridge == BRIDGE_TP6800) ? 0 : 1);
4903 if (sd->bridge == BRIDGE_TP6810)
4904 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4905 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
4906 if (sd->sensor == SENSOR_SOI763A)
4907 sd->jpegqual = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4908 V4L2_CID_JPEG_COMPRESSION_QUALITY,
4909 0, 15, 1, (sd->bridge == BRIDGE_TP6810) ? 0 : 13);
4910
4911 if (hdl->error) {
4912 pr_err("Could not initialize controls\n");
4913 return hdl->error;
4914 }
4915 if (gspca_dev->autogain)
4916 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
4917 else
4918 v4l2_ctrl_cluster(2, &gspca_dev->exposure);
4919 return 0;
4920}
4921
4928static const struct sd_desc sd_desc = { 4922static const struct sd_desc sd_desc = {
4929 .name = KBUILD_MODNAME, 4923 .name = KBUILD_MODNAME,
4930 .ctrls = sd_ctrls,
4931 .nctrls = NCTRLS,
4932 .config = sd_config, 4924 .config = sd_config,
4933 .init = sd_init, 4925 .init = sd_init,
4926 .init_controls = sd_init_controls,
4934 .isoc_init = sd_isoc_init, 4927 .isoc_init = sd_isoc_init,
4935 .start = sd_start, 4928 .start = sd_start,
4936 .stopN = sd_stopN, 4929 .stopN = sd_stopN,
diff --git a/drivers/media/video/gspca/tv8532.c b/drivers/media/video/gspca/tv8532.c
index c8922c5ffbf5..8591324a53e1 100644
--- a/drivers/media/video/gspca/tv8532.c
+++ b/drivers/media/video/gspca/tv8532.c
@@ -30,49 +30,9 @@ MODULE_LICENSE("GPL");
30struct sd { 30struct sd {
31 struct gspca_dev gspca_dev; /* !! must be the first item */ 31 struct gspca_dev gspca_dev; /* !! must be the first item */
32 32
33 __u16 exposure;
34 __u16 gain;
35
36 __u8 packet; 33 __u8 packet;
37}; 34};
38 35
39/* V4L2 controls supported by the driver */
40static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
41static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
42static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
43static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
44
45static const struct ctrl sd_ctrls[] = {
46 {
47 {
48 .id = V4L2_CID_EXPOSURE,
49 .type = V4L2_CTRL_TYPE_INTEGER,
50 .name = "Exposure",
51 .minimum = 1,
52 .maximum = 0x18f,
53 .step = 1,
54#define EXPOSURE_DEF 0x18f
55 .default_value = EXPOSURE_DEF,
56 },
57 .set = sd_setexposure,
58 .get = sd_getexposure,
59 },
60 {
61 {
62 .id = V4L2_CID_GAIN,
63 .type = V4L2_CTRL_TYPE_INTEGER,
64 .name = "Gain",
65 .minimum = 0,
66 .maximum = 0x7ff,
67 .step = 1,
68#define GAIN_DEF 0x100
69 .default_value = GAIN_DEF,
70 },
71 .set = sd_setgain,
72 .get = sd_getgain,
73 },
74};
75
76static const struct v4l2_pix_format sif_mode[] = { 36static const struct v4l2_pix_format sif_mode[] = {
77 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, 37 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
78 .bytesperline = 176, 38 .bytesperline = 176,
@@ -202,15 +162,12 @@ static void tv_8532WriteEEprom(struct gspca_dev *gspca_dev)
202static int sd_config(struct gspca_dev *gspca_dev, 162static int sd_config(struct gspca_dev *gspca_dev,
203 const struct usb_device_id *id) 163 const struct usb_device_id *id)
204{ 164{
205 struct sd *sd = (struct sd *) gspca_dev;
206 struct cam *cam; 165 struct cam *cam;
207 166
208 cam = &gspca_dev->cam; 167 cam = &gspca_dev->cam;
209 cam->cam_mode = sif_mode; 168 cam->cam_mode = sif_mode;
210 cam->nmodes = ARRAY_SIZE(sif_mode); 169 cam->nmodes = ARRAY_SIZE(sif_mode);
211 170
212 sd->exposure = EXPOSURE_DEF;
213 sd->gain = GAIN_DEF;
214 return 0; 171 return 0;
215} 172}
216 173
@@ -241,23 +198,19 @@ static int sd_init(struct gspca_dev *gspca_dev)
241 return 0; 198 return 0;
242} 199}
243 200
244static void setexposure(struct gspca_dev *gspca_dev) 201static void setexposure(struct gspca_dev *gspca_dev, s32 val)
245{ 202{
246 struct sd *sd = (struct sd *) gspca_dev; 203 reg_w2(gspca_dev, R1C_AD_EXPOSE_TIMEL, val);
247
248 reg_w2(gspca_dev, R1C_AD_EXPOSE_TIMEL, sd->exposure);
249 reg_w1(gspca_dev, R00_PART_CONTROL, LATENT_CHANGE | EXPO_CHANGE); 204 reg_w1(gspca_dev, R00_PART_CONTROL, LATENT_CHANGE | EXPO_CHANGE);
250 /* 0x84 */ 205 /* 0x84 */
251} 206}
252 207
253static void setgain(struct gspca_dev *gspca_dev) 208static void setgain(struct gspca_dev *gspca_dev, s32 val)
254{ 209{
255 struct sd *sd = (struct sd *) gspca_dev; 210 reg_w2(gspca_dev, R20_GAIN_G1L, val);
256 211 reg_w2(gspca_dev, R22_GAIN_RL, val);
257 reg_w2(gspca_dev, R20_GAIN_G1L, sd->gain); 212 reg_w2(gspca_dev, R24_GAIN_BL, val);
258 reg_w2(gspca_dev, R22_GAIN_RL, sd->gain); 213 reg_w2(gspca_dev, R26_GAIN_G2L, val);
259 reg_w2(gspca_dev, R24_GAIN_BL, sd->gain);
260 reg_w2(gspca_dev, R26_GAIN_G2L, sd->gain);
261} 214}
262 215
263/* -- start the camera -- */ 216/* -- start the camera -- */
@@ -289,9 +242,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
289 242
290 tv_8532_setReg(gspca_dev); 243 tv_8532_setReg(gspca_dev);
291 244
292 setexposure(gspca_dev);
293 setgain(gspca_dev);
294
295 /************************************************/ 245 /************************************************/
296 reg_w1(gspca_dev, R31_UPD, 0x01); /* update registers */ 246 reg_w1(gspca_dev, R31_UPD, 0x01); /* update registers */
297 msleep(200); 247 msleep(200);
@@ -339,49 +289,55 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
339 data + gspca_dev->width + 5, gspca_dev->width); 289 data + gspca_dev->width + 5, gspca_dev->width);
340} 290}
341 291
342static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val) 292static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
343{ 293{
344 struct sd *sd = (struct sd *) gspca_dev; 294 struct gspca_dev *gspca_dev =
295 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
345 296
346 sd->exposure = val; 297 gspca_dev->usb_err = 0;
347 if (gspca_dev->streaming)
348 setexposure(gspca_dev);
349 return 0;
350}
351 298
352static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val) 299 if (!gspca_dev->streaming)
353{ 300 return 0;
354 struct sd *sd = (struct sd *) gspca_dev;
355 301
356 *val = sd->exposure; 302 switch (ctrl->id) {
357 return 0; 303 case V4L2_CID_EXPOSURE:
304 setexposure(gspca_dev, ctrl->val);
305 break;
306 case V4L2_CID_GAIN:
307 setgain(gspca_dev, ctrl->val);
308 break;
309 }
310 return gspca_dev->usb_err;
358} 311}
359 312
360static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val) 313static const struct v4l2_ctrl_ops sd_ctrl_ops = {
361{ 314 .s_ctrl = sd_s_ctrl,
362 struct sd *sd = (struct sd *) gspca_dev; 315};
363
364 sd->gain = val;
365 if (gspca_dev->streaming)
366 setgain(gspca_dev);
367 return 0;
368}
369 316
370static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val) 317static int sd_init_controls(struct gspca_dev *gspca_dev)
371{ 318{
372 struct sd *sd = (struct sd *) gspca_dev; 319 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
373 320
374 *val = sd->gain; 321 gspca_dev->vdev.ctrl_handler = hdl;
322 v4l2_ctrl_handler_init(hdl, 2);
323 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
324 V4L2_CID_EXPOSURE, 0, 0x18f, 1, 0x18f);
325 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
326 V4L2_CID_GAIN, 0, 0x7ff, 1, 0x100);
327
328 if (hdl->error) {
329 pr_err("Could not initialize controls\n");
330 return hdl->error;
331 }
375 return 0; 332 return 0;
376} 333}
377 334
378/* sub-driver description */ 335/* sub-driver description */
379static const struct sd_desc sd_desc = { 336static const struct sd_desc sd_desc = {
380 .name = MODULE_NAME, 337 .name = MODULE_NAME,
381 .ctrls = sd_ctrls,
382 .nctrls = ARRAY_SIZE(sd_ctrls),
383 .config = sd_config, 338 .config = sd_config,
384 .init = sd_init, 339 .init = sd_init,
340 .init_controls = sd_init_controls,
385 .start = sd_start, 341 .start = sd_start,
386 .stopN = sd_stopN, 342 .stopN = sd_stopN,
387 .pkt_scan = sd_pkt_scan, 343 .pkt_scan = sd_pkt_scan,
@@ -415,6 +371,7 @@ static struct usb_driver sd_driver = {
415#ifdef CONFIG_PM 371#ifdef CONFIG_PM
416 .suspend = gspca_suspend, 372 .suspend = gspca_suspend,
417 .resume = gspca_resume, 373 .resume = gspca_resume,
374 .reset_resume = gspca_resume,
418#endif 375#endif
419}; 376};
420 377
diff --git a/drivers/media/video/gspca/vc032x.c b/drivers/media/video/gspca/vc032x.c
index 208f6b2d512a..f21fd1677c38 100644
--- a/drivers/media/video/gspca/vc032x.c
+++ b/drivers/media/video/gspca/vc032x.c
@@ -33,18 +33,10 @@ MODULE_LICENSE("GPL");
33/* specific webcam descriptor */ 33/* specific webcam descriptor */
34struct sd { 34struct sd {
35 struct gspca_dev gspca_dev; /* !! must be the first item */ 35 struct gspca_dev gspca_dev; /* !! must be the first item */
36 36 struct { /* hvflip cluster */
37 u8 brightness; 37 struct v4l2_ctrl *hflip;
38 u8 contrast; 38 struct v4l2_ctrl *vflip;
39 u8 colors; 39 };
40 u8 hflip;
41 u8 vflip;
42 u8 lightfreq;
43 s8 sharpness;
44 u16 exposure;
45 u8 gain;
46 u8 autogain;
47 u8 backlight;
48 40
49 u8 image_offset; 41 u8 image_offset;
50 42
@@ -73,252 +65,6 @@ enum sensors {
73 NSENSORS 65 NSENSORS
74}; 66};
75 67
76/* V4L2 controls supported by the driver */
77static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
78static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
79static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
80static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
81static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
82static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
83static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
84static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
85static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
86static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
87static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
88static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
89static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
90static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
91static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
92static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
93static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
94static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
95static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
96static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
97static int sd_setbacklight(struct gspca_dev *gspca_dev, __s32 val);
98static int sd_getbacklight(struct gspca_dev *gspca_dev, __s32 *val);
99
100static const struct ctrl sd_ctrls[] = {
101#define BRIGHTNESS_IDX 0
102 {
103 {
104 .id = V4L2_CID_BRIGHTNESS,
105 .type = V4L2_CTRL_TYPE_INTEGER,
106 .name = "Brightness",
107 .minimum = 0,
108 .maximum = 255,
109 .step = 1,
110#define BRIGHTNESS_DEF 128
111 .default_value = BRIGHTNESS_DEF,
112 },
113 .set = sd_setbrightness,
114 .get = sd_getbrightness,
115 },
116#define CONTRAST_IDX 1
117 {
118 {
119 .id = V4L2_CID_CONTRAST,
120 .type = V4L2_CTRL_TYPE_INTEGER,
121 .name = "Contrast",
122 .minimum = 0,
123 .maximum = 255,
124 .step = 1,
125#define CONTRAST_DEF 127
126 .default_value = CONTRAST_DEF,
127 },
128 .set = sd_setcontrast,
129 .get = sd_getcontrast,
130 },
131#define COLORS_IDX 2
132 {
133 {
134 .id = V4L2_CID_SATURATION,
135 .type = V4L2_CTRL_TYPE_INTEGER,
136 .name = "Saturation",
137 .minimum = 1,
138 .maximum = 127,
139 .step = 1,
140#define COLOR_DEF 63
141 .default_value = COLOR_DEF,
142 },
143 .set = sd_setcolors,
144 .get = sd_getcolors,
145 },
146/* next 2 controls work with some sensors only */
147#define HFLIP_IDX 3
148 {
149 {
150 .id = V4L2_CID_HFLIP,
151 .type = V4L2_CTRL_TYPE_BOOLEAN,
152 .name = "Mirror",
153 .minimum = 0,
154 .maximum = 1,
155 .step = 1,
156#define HFLIP_DEF 0
157 .default_value = HFLIP_DEF,
158 },
159 .set = sd_sethflip,
160 .get = sd_gethflip,
161 },
162#define VFLIP_IDX 4
163 {
164 {
165 .id = V4L2_CID_VFLIP,
166 .type = V4L2_CTRL_TYPE_BOOLEAN,
167 .name = "Vflip",
168 .minimum = 0,
169 .maximum = 1,
170 .step = 1,
171#define VFLIP_DEF 0
172 .default_value = VFLIP_DEF,
173 },
174 .set = sd_setvflip,
175 .get = sd_getvflip,
176 },
177#define LIGHTFREQ_IDX 5
178 {
179 {
180 .id = V4L2_CID_POWER_LINE_FREQUENCY,
181 .type = V4L2_CTRL_TYPE_MENU,
182 .name = "Light frequency filter",
183 .minimum = 0,
184 .maximum = 2, /* 0: No, 1: 50Hz, 2:60Hz */
185 .step = 1,
186#define FREQ_DEF 1
187 .default_value = FREQ_DEF,
188 },
189 .set = sd_setfreq,
190 .get = sd_getfreq,
191 },
192#define SHARPNESS_IDX 6
193 {
194 {
195 .id = V4L2_CID_SHARPNESS,
196 .type = V4L2_CTRL_TYPE_INTEGER,
197 .name = "Sharpness",
198 .minimum = -1,
199 .maximum = 2,
200 .step = 1,
201#define SHARPNESS_DEF -1
202 .default_value = SHARPNESS_DEF,
203 },
204 .set = sd_setsharpness,
205 .get = sd_getsharpness,
206 },
207#define GAIN_IDX 7
208 {
209 {
210 .id = V4L2_CID_GAIN,
211 .type = V4L2_CTRL_TYPE_INTEGER,
212 .name = "Gain",
213 .minimum = 0,
214 .maximum = 78,
215 .step = 1,
216#define GAIN_DEF 0
217 .default_value = GAIN_DEF,
218 },
219 .set = sd_setgain,
220 .get = sd_getgain,
221 },
222#define EXPOSURE_IDX 8
223 {
224 {
225 .id = V4L2_CID_EXPOSURE,
226 .type = V4L2_CTRL_TYPE_INTEGER,
227 .name = "Exposure",
228#define EXPOSURE_DEF 450
229 .minimum = 0,
230 .maximum = 4095,
231 .step = 1,
232 .default_value = EXPOSURE_DEF,
233 },
234 .set = sd_setexposure,
235 .get = sd_getexposure,
236 },
237#define AUTOGAIN_IDX 9
238 {
239 {
240 .id = V4L2_CID_AUTOGAIN,
241 .type = V4L2_CTRL_TYPE_BOOLEAN,
242 .name = "Automatic Gain and Exposure",
243 .minimum = 0,
244 .maximum = 1,
245 .step = 1,
246#define AUTOGAIN_DEF 1
247 .default_value = AUTOGAIN_DEF,
248 },
249 .set = sd_setautogain,
250 .get = sd_getautogain,
251 },
252#define BACKLIGHT_IDX 10
253 {
254 {
255 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
256 .type = V4L2_CTRL_TYPE_BOOLEAN,
257 .name = "Backlight Compensation",
258 .minimum = 0,
259 .maximum = 15,
260 .step = 1,
261#define BACKLIGHT_DEF 15
262 .default_value = BACKLIGHT_DEF,
263 },
264 .set = sd_setbacklight,
265 .get = sd_getbacklight,
266 },
267};
268
269/* table of the disabled controls */
270static u32 ctrl_dis[NSENSORS] = {
271 [SENSOR_HV7131R] =
272 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
273 | (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX)
274 | (1 << SHARPNESS_IDX)
275 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
276 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
277 [SENSOR_MI0360] =
278 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
279 | (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX)
280 | (1 << SHARPNESS_IDX)
281 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
282 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
283 [SENSOR_MI1310_SOC] =
284 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
285 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX)
286 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
287 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
288 [SENSOR_MI1320] =
289 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
290 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX)
291 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
292 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
293 [SENSOR_MI1320_SOC] =
294 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
295 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX)
296 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
297 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
298 [SENSOR_OV7660] =
299 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
300 | (1 << LIGHTFREQ_IDX) | (1 << SHARPNESS_IDX)
301 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
302 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
303 [SENSOR_OV7670] =
304 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
305 | (1 << SHARPNESS_IDX)
306 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
307 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
308 [SENSOR_PO1200] =
309 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
310 | (1 << LIGHTFREQ_IDX)
311 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
312 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
313 [SENSOR_PO3130NC] =
314 (1 << BRIGHTNESS_IDX) | (1 << CONTRAST_IDX) | (1 << COLORS_IDX)
315 | (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX)
316 | (1 << SHARPNESS_IDX)
317 | (1 << GAIN_IDX) | (1 << EXPOSURE_IDX)
318 | (1 << AUTOGAIN_IDX) | (1 << BACKLIGHT_IDX),
319 [SENSOR_POxxxx] =
320 (1 << HFLIP_IDX) | (1 << VFLIP_IDX) | (1 << LIGHTFREQ_IDX),
321};
322 68
323static const struct v4l2_pix_format vc0321_mode[] = { 69static const struct v4l2_pix_format vc0321_mode[] = {
324 {320, 240, V4L2_PIX_FMT_YVYU, V4L2_FIELD_NONE, 70 {320, 240, V4L2_PIX_FMT_YVYU, V4L2_FIELD_NONE,
@@ -3405,18 +3151,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
3405 (id->idProduct == 0x0892 || id->idProduct == 0x0896)) 3151 (id->idProduct == 0x0892 || id->idProduct == 0x0896))
3406 sd->sensor = SENSOR_POxxxx; /* no probe */ 3152 sd->sensor = SENSOR_POxxxx; /* no probe */
3407 3153
3408 sd->brightness = BRIGHTNESS_DEF;
3409 sd->contrast = CONTRAST_DEF;
3410 sd->colors = COLOR_DEF;
3411 sd->hflip = HFLIP_DEF;
3412 sd->vflip = VFLIP_DEF;
3413 sd->lightfreq = FREQ_DEF;
3414 sd->sharpness = SHARPNESS_DEF;
3415 sd->gain = GAIN_DEF;
3416 sd->exposure = EXPOSURE_DEF;
3417 sd->autogain = AUTOGAIN_DEF;
3418 sd->backlight = BACKLIGHT_DEF;
3419
3420 return 0; 3154 return 0;
3421} 3155}
3422 3156
@@ -3512,7 +3246,6 @@ static int sd_init(struct gspca_dev *gspca_dev)
3512 } 3246 }
3513 } 3247 }
3514 cam->npkt = npkt[sd->sensor]; 3248 cam->npkt = npkt[sd->sensor];
3515 gspca_dev->ctrl_dis = ctrl_dis[sd->sensor];
3516 3249
3517 if (sd->sensor == SENSOR_OV7670) 3250 if (sd->sensor == SENSOR_OV7670)
3518 sd->flags |= FL_HFLIP | FL_VFLIP; 3251 sd->flags |= FL_HFLIP | FL_VFLIP;
@@ -3534,14 +3267,11 @@ static int sd_init(struct gspca_dev *gspca_dev)
3534 return gspca_dev->usb_err; 3267 return gspca_dev->usb_err;
3535} 3268}
3536 3269
3537static void setbrightness(struct gspca_dev *gspca_dev) 3270static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
3538{ 3271{
3539 struct sd *sd = (struct sd *) gspca_dev;
3540 u8 data; 3272 u8 data;
3541 3273
3542 if (gspca_dev->ctrl_dis & (1 << BRIGHTNESS_IDX)) 3274 data = val;
3543 return;
3544 data = sd->brightness;
3545 if (data >= 0x80) 3275 if (data >= 0x80)
3546 data &= 0x7f; 3276 data &= 0x7f;
3547 else 3277 else
@@ -3549,36 +3279,27 @@ static void setbrightness(struct gspca_dev *gspca_dev)
3549 i2c_write(gspca_dev, 0x98, &data, 1); 3279 i2c_write(gspca_dev, 0x98, &data, 1);
3550} 3280}
3551 3281
3552static void setcontrast(struct gspca_dev *gspca_dev) 3282static void setcontrast(struct gspca_dev *gspca_dev, u8 val)
3553{ 3283{
3554 struct sd *sd = (struct sd *) gspca_dev; 3284 i2c_write(gspca_dev, 0x99, &val, 1);
3555
3556 if (gspca_dev->ctrl_dis & (1 << CONTRAST_IDX))
3557 return;
3558 i2c_write(gspca_dev, 0x99, &sd->contrast, 1);
3559} 3285}
3560 3286
3561static void setcolors(struct gspca_dev *gspca_dev) 3287static void setcolors(struct gspca_dev *gspca_dev, u8 val)
3562{ 3288{
3563 struct sd *sd = (struct sd *) gspca_dev;
3564 u8 data; 3289 u8 data;
3565 3290
3566 if (gspca_dev->ctrl_dis & (1 << COLORS_IDX)) 3291 data = val - (val >> 3) - 1;
3567 return;
3568 data = sd->colors - (sd->colors >> 3) - 1;
3569 i2c_write(gspca_dev, 0x94, &data, 1); 3292 i2c_write(gspca_dev, 0x94, &data, 1);
3570 i2c_write(gspca_dev, 0x95, &sd->colors, 1); 3293 i2c_write(gspca_dev, 0x95, &val, 1);
3571} 3294}
3572 3295
3573static void sethvflip(struct gspca_dev *gspca_dev) 3296static void sethvflip(struct gspca_dev *gspca_dev, bool hflip, bool vflip)
3574{ 3297{
3575 struct sd *sd = (struct sd *) gspca_dev; 3298 struct sd *sd = (struct sd *) gspca_dev;
3576 u8 data[2], hflip, vflip; 3299 u8 data[2];
3577 3300
3578 hflip = sd->hflip;
3579 if (sd->flags & FL_HFLIP) 3301 if (sd->flags & FL_HFLIP)
3580 hflip = !hflip; 3302 hflip = !hflip;
3581 vflip = sd->vflip;
3582 if (sd->flags & FL_VFLIP) 3303 if (sd->flags & FL_VFLIP)
3583 vflip = !vflip; 3304 vflip = !vflip;
3584 switch (sd->sensor) { 3305 switch (sd->sensor) {
@@ -3610,7 +3331,7 @@ static void sethvflip(struct gspca_dev *gspca_dev)
3610 } 3331 }
3611} 3332}
3612 3333
3613static void setlightfreq(struct gspca_dev *gspca_dev) 3334static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
3614{ 3335{
3615 struct sd *sd = (struct sd *) gspca_dev; 3336 struct sd *sd = (struct sd *) gspca_dev;
3616 static const u8 (*ov7660_freq_tb[3])[4] = 3337 static const u8 (*ov7660_freq_tb[3])[4] =
@@ -3618,10 +3339,10 @@ static void setlightfreq(struct gspca_dev *gspca_dev)
3618 3339
3619 if (sd->sensor != SENSOR_OV7660) 3340 if (sd->sensor != SENSOR_OV7660)
3620 return; 3341 return;
3621 usb_exchange(gspca_dev, ov7660_freq_tb[sd->lightfreq]); 3342 usb_exchange(gspca_dev, ov7660_freq_tb[val]);
3622} 3343}
3623 3344
3624static void setsharpness(struct gspca_dev *gspca_dev) 3345static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
3625{ 3346{
3626 struct sd *sd = (struct sd *) gspca_dev; 3347 struct sd *sd = (struct sd *) gspca_dev;
3627 u8 data; 3348 u8 data;
@@ -3630,51 +3351,41 @@ static void setsharpness(struct gspca_dev *gspca_dev)
3630 case SENSOR_PO1200: 3351 case SENSOR_PO1200:
3631 data = 0; 3352 data = 0;
3632 i2c_write(gspca_dev, 0x03, &data, 1); 3353 i2c_write(gspca_dev, 0x03, &data, 1);
3633 if (sd->sharpness < 0) 3354 if (val < 0)
3634 data = 0x6a; 3355 data = 0x6a;
3635 else 3356 else
3636 data = 0xb5 + sd->sharpness * 3; 3357 data = 0xb5 + val * 3;
3637 i2c_write(gspca_dev, 0x61, &data, 1); 3358 i2c_write(gspca_dev, 0x61, &data, 1);
3638 break; 3359 break;
3639 case SENSOR_POxxxx: 3360 case SENSOR_POxxxx:
3640 if (sd->sharpness < 0) 3361 if (val < 0)
3641 data = 0x7e; /* def = max */ 3362 data = 0x7e; /* def = max */
3642 else 3363 else
3643 data = 0x60 + sd->sharpness * 0x0f; 3364 data = 0x60 + val * 0x0f;
3644 i2c_write(gspca_dev, 0x59, &data, 1); 3365 i2c_write(gspca_dev, 0x59, &data, 1);
3645 break; 3366 break;
3646 } 3367 }
3647} 3368}
3648static void setgain(struct gspca_dev *gspca_dev) 3369static void setgain(struct gspca_dev *gspca_dev, u8 val)
3649{ 3370{
3650 struct sd *sd = (struct sd *) gspca_dev; 3371 i2c_write(gspca_dev, 0x15, &val, 1);
3651
3652 if (gspca_dev->ctrl_dis & (1 << GAIN_IDX))
3653 return;
3654 i2c_write(gspca_dev, 0x15, &sd->gain, 1);
3655} 3372}
3656 3373
3657static void setexposure(struct gspca_dev *gspca_dev) 3374static void setexposure(struct gspca_dev *gspca_dev, s32 val)
3658{ 3375{
3659 struct sd *sd = (struct sd *) gspca_dev;
3660 u8 data; 3376 u8 data;
3661 3377
3662 if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX)) 3378 data = val >> 8;
3663 return;
3664 data = sd->exposure >> 8;
3665 i2c_write(gspca_dev, 0x1a, &data, 1); 3379 i2c_write(gspca_dev, 0x1a, &data, 1);
3666 data = sd->exposure; 3380 data = val;
3667 i2c_write(gspca_dev, 0x1b, &data, 1); 3381 i2c_write(gspca_dev, 0x1b, &data, 1);
3668} 3382}
3669 3383
3670static void setautogain(struct gspca_dev *gspca_dev) 3384static void setautogain(struct gspca_dev *gspca_dev, s32 val)
3671{ 3385{
3672 struct sd *sd = (struct sd *) gspca_dev;
3673 static const u8 data[2] = {0x28, 0x3c}; 3386 static const u8 data[2] = {0x28, 0x3c};
3674 3387
3675 if (gspca_dev->ctrl_dis & (1 << AUTOGAIN_IDX)) 3388 i2c_write(gspca_dev, 0xd1, &data[val], 1);
3676 return;
3677 i2c_write(gspca_dev, 0xd1, &data[sd->autogain], 1);
3678} 3389}
3679 3390
3680static void setgamma(struct gspca_dev *gspca_dev) 3391static void setgamma(struct gspca_dev *gspca_dev)
@@ -3683,30 +3394,29 @@ static void setgamma(struct gspca_dev *gspca_dev)
3683 usb_exchange(gspca_dev, poxxxx_gamma); 3394 usb_exchange(gspca_dev, poxxxx_gamma);
3684} 3395}
3685 3396
3686static void setbacklight(struct gspca_dev *gspca_dev) 3397static void setbacklight(struct gspca_dev *gspca_dev, s32 val)
3687{ 3398{
3688 struct sd *sd = (struct sd *) gspca_dev;
3689 u16 v; 3399 u16 v;
3690 u8 data; 3400 u8 data;
3691 3401
3692 data = (sd->backlight << 4) | 0x0f; 3402 data = (val << 4) | 0x0f;
3693 i2c_write(gspca_dev, 0xaa, &data, 1); 3403 i2c_write(gspca_dev, 0xaa, &data, 1);
3694 v = 613 + 12 * sd->backlight; 3404 v = 613 + 12 * val;
3695 data = v >> 8; 3405 data = v >> 8;
3696 i2c_write(gspca_dev, 0xc4, &data, 1); 3406 i2c_write(gspca_dev, 0xc4, &data, 1);
3697 data = v; 3407 data = v;
3698 i2c_write(gspca_dev, 0xc5, &data, 1); 3408 i2c_write(gspca_dev, 0xc5, &data, 1);
3699 v = 1093 - 12 * sd->backlight; 3409 v = 1093 - 12 * val;
3700 data = v >> 8; 3410 data = v >> 8;
3701 i2c_write(gspca_dev, 0xc6, &data, 1); 3411 i2c_write(gspca_dev, 0xc6, &data, 1);
3702 data = v; 3412 data = v;
3703 i2c_write(gspca_dev, 0xc7, &data, 1); 3413 i2c_write(gspca_dev, 0xc7, &data, 1);
3704 v = 342 + 9 * sd->backlight; 3414 v = 342 + 9 * val;
3705 data = v >> 8; 3415 data = v >> 8;
3706 i2c_write(gspca_dev, 0xc8, &data, 1); 3416 i2c_write(gspca_dev, 0xc8, &data, 1);
3707 data = v; 3417 data = v;
3708 i2c_write(gspca_dev, 0xc9, &data, 1); 3418 i2c_write(gspca_dev, 0xc9, &data, 1);
3709 v = 702 - 9 * sd->backlight; 3419 v = 702 - 9 * val;
3710 data = v >> 8; 3420 data = v >> 8;
3711 i2c_write(gspca_dev, 0xca, &data, 1); 3421 i2c_write(gspca_dev, 0xca, &data, 1);
3712 data = v; 3422 data = v;
@@ -3833,14 +3543,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
3833/* case SENSOR_POxxxx: */ 3543/* case SENSOR_POxxxx: */
3834 usb_exchange(gspca_dev, poxxxx_init_common); 3544 usb_exchange(gspca_dev, poxxxx_init_common);
3835 setgamma(gspca_dev); 3545 setgamma(gspca_dev);
3836 setbacklight(gspca_dev);
3837 setbrightness(gspca_dev);
3838 setcontrast(gspca_dev);
3839 setcolors(gspca_dev);
3840 setsharpness(gspca_dev);
3841 setautogain(gspca_dev);
3842 setexposure(gspca_dev);
3843 setgain(gspca_dev);
3844 usb_exchange(gspca_dev, poxxxx_init_start_3); 3546 usb_exchange(gspca_dev, poxxxx_init_start_3);
3845 if (mode) 3547 if (mode)
3846 init = poxxxx_initQVGA; 3548 init = poxxxx_initQVGA;
@@ -3873,8 +3575,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
3873 break; 3575 break;
3874 } 3576 }
3875 msleep(100); 3577 msleep(100);
3876 sethvflip(gspca_dev);
3877 setlightfreq(gspca_dev);
3878 } 3578 }
3879 switch (sd->sensor) { 3579 switch (sd->sensor) {
3880 case SENSOR_OV7670: 3580 case SENSOR_OV7670:
@@ -3960,233 +3660,152 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
3960 gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 3660 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
3961} 3661}
3962 3662
3963static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) 3663static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
3964{
3965 struct sd *sd = (struct sd *) gspca_dev;
3966
3967 sd->brightness = val;
3968 if (gspca_dev->streaming)
3969 setbrightness(gspca_dev);
3970 return gspca_dev->usb_err;
3971}
3972
3973static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
3974{
3975 struct sd *sd = (struct sd *) gspca_dev;
3976
3977 *val = sd->brightness;
3978 return 0;
3979}
3980
3981static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
3982{
3983 struct sd *sd = (struct sd *) gspca_dev;
3984
3985 sd->contrast = val;
3986 if (gspca_dev->streaming)
3987 setcontrast(gspca_dev);
3988 return gspca_dev->usb_err;
3989}
3990
3991static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
3992{
3993 struct sd *sd = (struct sd *) gspca_dev;
3994
3995 *val = sd->contrast;
3996 return 0;
3997}
3998
3999static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
4000{ 3664{
4001 struct sd *sd = (struct sd *) gspca_dev; 3665 struct gspca_dev *gspca_dev =
4002 3666 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
4003 sd->colors = val; 3667 struct sd *sd = (struct sd *)gspca_dev;
4004 if (gspca_dev->streaming)
4005 setcolors(gspca_dev);
4006 return gspca_dev->usb_err;
4007}
4008
4009static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
4010{
4011 struct sd *sd = (struct sd *) gspca_dev;
4012
4013 *val = sd->colors;
4014 return 0;
4015}
4016
4017static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
4018{
4019 struct sd *sd = (struct sd *) gspca_dev;
4020
4021 sd->hflip = val;
4022 if (gspca_dev->streaming)
4023 sethvflip(gspca_dev);
4024 return gspca_dev->usb_err;
4025}
4026
4027static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
4028{
4029 struct sd *sd = (struct sd *) gspca_dev;
4030
4031 *val = sd->hflip;
4032 return 0;
4033}
4034
4035static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
4036{
4037 struct sd *sd = (struct sd *) gspca_dev;
4038
4039 sd->vflip = val;
4040 if (gspca_dev->streaming)
4041 sethvflip(gspca_dev);
4042 return gspca_dev->usb_err;
4043}
4044
4045static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
4046{
4047 struct sd *sd = (struct sd *) gspca_dev;
4048 3668
4049 *val = sd->vflip; 3669 gspca_dev->usb_err = 0;
4050 return 0;
4051}
4052 3670
4053static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val) 3671 if (!gspca_dev->streaming && ctrl->id != V4L2_CID_POWER_LINE_FREQUENCY)
4054{ 3672 return 0;
4055 struct sd *sd = (struct sd *) gspca_dev;
4056
4057 sd->lightfreq = val;
4058 if (gspca_dev->streaming)
4059 setlightfreq(gspca_dev);
4060 return gspca_dev->usb_err;
4061}
4062
4063static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
4064{
4065 struct sd *sd = (struct sd *) gspca_dev;
4066
4067 *val = sd->lightfreq;
4068 return 0;
4069}
4070
4071static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
4072{
4073 struct sd *sd = (struct sd *) gspca_dev;
4074
4075 sd->sharpness = val;
4076 if (gspca_dev->streaming)
4077 setsharpness(gspca_dev);
4078 return gspca_dev->usb_err;
4079}
4080
4081static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
4082{
4083 struct sd *sd = (struct sd *) gspca_dev;
4084
4085 *val = sd->sharpness;
4086 return 0;
4087}
4088
4089static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
4090{
4091 struct sd *sd = (struct sd *) gspca_dev;
4092
4093 sd->gain = val;
4094 if (gspca_dev->streaming)
4095 setgain(gspca_dev);
4096 return gspca_dev->usb_err;
4097}
4098
4099static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
4100{
4101 struct sd *sd = (struct sd *) gspca_dev;
4102
4103 *val = sd->gain;
4104 return 0;
4105}
4106
4107static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
4108{
4109 struct sd *sd = (struct sd *) gspca_dev;
4110
4111 sd->exposure = val;
4112 if (gspca_dev->streaming)
4113 setexposure(gspca_dev);
4114 return gspca_dev->usb_err;
4115}
4116
4117static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
4118{
4119 struct sd *sd = (struct sd *) gspca_dev;
4120
4121 *val = sd->exposure;
4122 return 0;
4123}
4124
4125static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
4126{
4127 struct sd *sd = (struct sd *) gspca_dev;
4128
4129 sd->autogain = val;
4130 if (gspca_dev->streaming)
4131 setautogain(gspca_dev);
4132 3673
3674 switch (ctrl->id) {
3675 case V4L2_CID_BRIGHTNESS:
3676 setbrightness(gspca_dev, ctrl->val);
3677 break;
3678 case V4L2_CID_CONTRAST:
3679 setcontrast(gspca_dev, ctrl->val);
3680 break;
3681 case V4L2_CID_SATURATION:
3682 setcolors(gspca_dev, ctrl->val);
3683 break;
3684 case V4L2_CID_HFLIP:
3685 sethvflip(gspca_dev, sd->hflip->val, sd->vflip->val);
3686 break;
3687 case V4L2_CID_SHARPNESS:
3688 setsharpness(gspca_dev, ctrl->val);
3689 break;
3690 case V4L2_CID_AUTOGAIN:
3691 setautogain(gspca_dev, ctrl->val);
3692 break;
3693 case V4L2_CID_GAIN:
3694 setgain(gspca_dev, ctrl->val);
3695 break;
3696 case V4L2_CID_EXPOSURE:
3697 setexposure(gspca_dev, ctrl->val);
3698 break;
3699 case V4L2_CID_BACKLIGHT_COMPENSATION:
3700 setbacklight(gspca_dev, ctrl->val);
3701 break;
3702 case V4L2_CID_POWER_LINE_FREQUENCY:
3703 setlightfreq(gspca_dev, ctrl->val);
3704 break;
3705 }
4133 return gspca_dev->usb_err; 3706 return gspca_dev->usb_err;
4134} 3707}
4135 3708
4136static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val) 3709static const struct v4l2_ctrl_ops sd_ctrl_ops = {
4137{ 3710 .s_ctrl = sd_s_ctrl,
4138 struct sd *sd = (struct sd *) gspca_dev; 3711};
4139
4140 *val = sd->autogain;
4141 return 0;
4142}
4143
4144static int sd_setbacklight(struct gspca_dev *gspca_dev, __s32 val)
4145{
4146 struct sd *sd = (struct sd *) gspca_dev;
4147
4148 sd->backlight = val;
4149 if (gspca_dev->streaming)
4150 setbacklight(gspca_dev);
4151
4152 return gspca_dev->usb_err;
4153}
4154 3712
4155static int sd_getbacklight(struct gspca_dev *gspca_dev, __s32 *val) 3713static int sd_init_controls(struct gspca_dev *gspca_dev)
4156{ 3714{
4157 struct sd *sd = (struct sd *) gspca_dev; 3715 struct sd *sd = (struct sd *)gspca_dev;
3716 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
3717 bool has_brightness = false;
3718 bool has_contrast = false;
3719 bool has_sat = false;
3720 bool has_hvflip = false;
3721 bool has_freq = false;
3722 bool has_backlight = false;
3723 bool has_exposure = false;
3724 bool has_autogain = false;
3725 bool has_gain = false;
3726 bool has_sharpness = false;
4158 3727
4159 *val = sd->backlight; 3728 switch (sd->sensor) {
4160 return 0; 3729 case SENSOR_HV7131R:
4161} 3730 case SENSOR_MI0360:
4162 3731 case SENSOR_PO3130NC:
4163static int sd_querymenu(struct gspca_dev *gspca_dev, 3732 break;
4164 struct v4l2_querymenu *menu) 3733 case SENSOR_MI1310_SOC:
4165{ 3734 case SENSOR_MI1320:
4166 static const char *freq_nm[3] = {"NoFliker", "50 Hz", "60 Hz"}; 3735 case SENSOR_MI1320_SOC:
3736 case SENSOR_OV7660:
3737 has_hvflip = true;
3738 break;
3739 case SENSOR_OV7670:
3740 has_hvflip = has_freq = true;
3741 break;
3742 case SENSOR_PO1200:
3743 has_hvflip = has_sharpness = true;
3744 break;
3745 case SENSOR_POxxxx:
3746 has_brightness = has_contrast = has_sat = has_backlight =
3747 has_exposure = has_autogain = has_gain =
3748 has_sharpness = true;
3749 break;
3750 }
4167 3751
4168 switch (menu->id) { 3752 gspca_dev->vdev.ctrl_handler = hdl;
4169 case V4L2_CID_POWER_LINE_FREQUENCY: 3753 v4l2_ctrl_handler_init(hdl, 8);
4170 if (menu->index >= ARRAY_SIZE(freq_nm)) 3754 if (has_brightness)
4171 break; 3755 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4172 strcpy((char *) menu->name, freq_nm[menu->index]); 3756 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
4173 return 0; 3757 if (has_contrast)
3758 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3759 V4L2_CID_CONTRAST, 0, 255, 1, 127);
3760 if (has_sat)
3761 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3762 V4L2_CID_SATURATION, 1, 127, 1, 63);
3763 if (has_hvflip) {
3764 sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3765 V4L2_CID_HFLIP, 0, 1, 1, 0);
3766 sd->vflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3767 V4L2_CID_VFLIP, 0, 1, 1, 0);
4174 } 3768 }
4175 return -EINVAL; 3769 if (has_sharpness)
3770 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3771 V4L2_CID_SHARPNESS, -1, 2, 1, -1);
3772 if (has_freq)
3773 v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
3774 V4L2_CID_POWER_LINE_FREQUENCY,
3775 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0,
3776 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
3777 if (has_autogain)
3778 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3779 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
3780 if (has_gain)
3781 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3782 V4L2_CID_GAIN, 0, 78, 1, 0);
3783 if (has_exposure)
3784 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3785 V4L2_CID_EXPOSURE, 0, 4095, 1, 450);
3786 if (has_backlight)
3787 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3788 V4L2_CID_BACKLIGHT_COMPENSATION, 0, 15, 1, 15);
3789
3790 if (hdl->error) {
3791 pr_err("Could not initialize controls\n");
3792 return hdl->error;
3793 }
3794 if (sd->hflip)
3795 v4l2_ctrl_cluster(2, &sd->hflip);
3796 return 0;
4176} 3797}
4177 3798
4178/* sub-driver description */ 3799/* sub-driver description */
4179static const struct sd_desc sd_desc = { 3800static const struct sd_desc sd_desc = {
4180 .name = MODULE_NAME, 3801 .name = MODULE_NAME,
4181 .ctrls = sd_ctrls, 3802 .init_controls = sd_init_controls,
4182 .nctrls = ARRAY_SIZE(sd_ctrls),
4183 .config = sd_config, 3803 .config = sd_config,
4184 .init = sd_init, 3804 .init = sd_init,
4185 .start = sd_start, 3805 .start = sd_start,
4186 .stopN = sd_stopN, 3806 .stopN = sd_stopN,
4187 .stop0 = sd_stop0, 3807 .stop0 = sd_stop0,
4188 .pkt_scan = sd_pkt_scan, 3808 .pkt_scan = sd_pkt_scan,
4189 .querymenu = sd_querymenu,
4190}; 3809};
4191 3810
4192/* -- module initialisation -- */ 3811/* -- module initialisation -- */
@@ -4227,6 +3846,7 @@ static struct usb_driver sd_driver = {
4227#ifdef CONFIG_PM 3846#ifdef CONFIG_PM
4228 .suspend = gspca_suspend, 3847 .suspend = gspca_suspend,
4229 .resume = gspca_resume, 3848 .resume = gspca_resume,
3849 .reset_resume = gspca_resume,
4230#endif 3850#endif
4231}; 3851};
4232 3852
diff --git a/drivers/media/video/gspca/vicam.c b/drivers/media/video/gspca/vicam.c
index 15a30f7a4b2a..b1a64b912666 100644
--- a/drivers/media/video/gspca/vicam.c
+++ b/drivers/media/video/gspca/vicam.c
@@ -44,17 +44,10 @@ MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
44MODULE_LICENSE("GPL"); 44MODULE_LICENSE("GPL");
45MODULE_FIRMWARE(VICAM_FIRMWARE); 45MODULE_FIRMWARE(VICAM_FIRMWARE);
46 46
47enum e_ctrl {
48 GAIN,
49 EXPOSURE,
50 NCTRL /* number of controls */
51};
52
53struct sd { 47struct sd {
54 struct gspca_dev gspca_dev; /* !! must be the first item */ 48 struct gspca_dev gspca_dev; /* !! must be the first item */
55 struct work_struct work_struct; 49 struct work_struct work_struct;
56 struct workqueue_struct *work_thread; 50 struct workqueue_struct *work_thread;
57 struct gspca_ctrl ctrls[NCTRL];
58}; 51};
59 52
60/* The vicam sensor has a resolution of 512 x 244, with I believe square 53/* The vicam sensor has a resolution of 512 x 244, with I believe square
@@ -86,31 +79,6 @@ static struct v4l2_pix_format vicam_mode[] = {
86 .colorspace = V4L2_COLORSPACE_SRGB,}, 79 .colorspace = V4L2_COLORSPACE_SRGB,},
87}; 80};
88 81
89static const struct ctrl sd_ctrls[] = {
90[GAIN] = {
91 {
92 .id = V4L2_CID_GAIN,
93 .type = V4L2_CTRL_TYPE_INTEGER,
94 .name = "Gain",
95 .minimum = 0,
96 .maximum = 255,
97 .step = 1,
98 .default_value = 200,
99 },
100 },
101[EXPOSURE] = {
102 {
103 .id = V4L2_CID_EXPOSURE,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Exposure",
106 .minimum = 0,
107 .maximum = 2047,
108 .step = 1,
109 .default_value = 256,
110 },
111 },
112};
113
114static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request, 82static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
115 u16 value, u16 index, u8 *data, u16 len) 83 u16 value, u16 index, u8 *data, u16 len)
116{ 84{
@@ -146,12 +114,13 @@ static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
146 */ 114 */
147static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size) 115static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
148{ 116{
149 struct sd *sd = (struct sd *)gspca_dev;
150 int ret, unscaled_height, act_len = 0; 117 int ret, unscaled_height, act_len = 0;
151 u8 *req_data = gspca_dev->usb_buf; 118 u8 *req_data = gspca_dev->usb_buf;
119 s32 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
120 s32 gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
152 121
153 memset(req_data, 0, 16); 122 memset(req_data, 0, 16);
154 req_data[0] = sd->ctrls[GAIN].val; 123 req_data[0] = gain;
155 if (gspca_dev->width == 256) 124 if (gspca_dev->width == 256)
156 req_data[1] |= 0x01; /* low nibble x-scale */ 125 req_data[1] |= 0x01; /* low nibble x-scale */
157 if (gspca_dev->height <= 122) { 126 if (gspca_dev->height <= 122) {
@@ -167,9 +136,9 @@ static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
167 else /* Up to 244 lines with req_data[3] == 0x08 */ 136 else /* Up to 244 lines with req_data[3] == 0x08 */
168 req_data[3] = 0x08; /* vend? */ 137 req_data[3] = 0x08; /* vend? */
169 138
170 if (sd->ctrls[EXPOSURE].val < 256) { 139 if (expo < 256) {
171 /* Frame rate maxed out, use partial frame expo time */ 140 /* Frame rate maxed out, use partial frame expo time */
172 req_data[4] = 255 - sd->ctrls[EXPOSURE].val; 141 req_data[4] = 255 - expo;
173 req_data[5] = 0x00; 142 req_data[5] = 0x00;
174 req_data[6] = 0x00; 143 req_data[6] = 0x00;
175 req_data[7] = 0x01; 144 req_data[7] = 0x01;
@@ -177,8 +146,8 @@ static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
177 /* Modify frame rate */ 146 /* Modify frame rate */
178 req_data[4] = 0x00; 147 req_data[4] = 0x00;
179 req_data[5] = 0x00; 148 req_data[5] = 0x00;
180 req_data[6] = sd->ctrls[EXPOSURE].val & 0xFF; 149 req_data[6] = expo & 0xFF;
181 req_data[7] = sd->ctrls[EXPOSURE].val >> 8; 150 req_data[7] = expo >> 8;
182 } 151 }
183 req_data[8] = ((244 - unscaled_height) / 2) & ~0x01; /* vstart */ 152 req_data[8] = ((244 - unscaled_height) / 2) & ~0x01; /* vstart */
184 /* bytes 9-15 do not seem to affect exposure or image quality */ 153 /* bytes 9-15 do not seem to affect exposure or image quality */
@@ -260,7 +229,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
260 cam->bulk_size = 64; 229 cam->bulk_size = 64;
261 cam->cam_mode = vicam_mode; 230 cam->cam_mode = vicam_mode;
262 cam->nmodes = ARRAY_SIZE(vicam_mode); 231 cam->nmodes = ARRAY_SIZE(vicam_mode);
263 cam->ctrls = sd->ctrls;
264 232
265 INIT_WORK(&sd->work_struct, vicam_dostream); 233 INIT_WORK(&sd->work_struct, vicam_dostream);
266 234
@@ -335,6 +303,24 @@ static void sd_stop0(struct gspca_dev *gspca_dev)
335 vicam_set_camera_power(gspca_dev, 0); 303 vicam_set_camera_power(gspca_dev, 0);
336} 304}
337 305
306static int sd_init_controls(struct gspca_dev *gspca_dev)
307{
308 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
309
310 gspca_dev->vdev.ctrl_handler = hdl;
311 v4l2_ctrl_handler_init(hdl, 2);
312 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, NULL,
313 V4L2_CID_EXPOSURE, 0, 2047, 1, 256);
314 gspca_dev->gain = v4l2_ctrl_new_std(hdl, NULL,
315 V4L2_CID_GAIN, 0, 255, 1, 200);
316
317 if (hdl->error) {
318 pr_err("Could not initialize controls\n");
319 return hdl->error;
320 }
321 return 0;
322}
323
338/* Table of supported USB devices */ 324/* Table of supported USB devices */
339static const struct usb_device_id device_table[] = { 325static const struct usb_device_id device_table[] = {
340 {USB_DEVICE(0x04c1, 0x009d)}, 326 {USB_DEVICE(0x04c1, 0x009d)},
@@ -347,10 +333,9 @@ MODULE_DEVICE_TABLE(usb, device_table);
347/* sub-driver description */ 333/* sub-driver description */
348static const struct sd_desc sd_desc = { 334static const struct sd_desc sd_desc = {
349 .name = MODULE_NAME, 335 .name = MODULE_NAME,
350 .ctrls = sd_ctrls,
351 .nctrls = ARRAY_SIZE(sd_ctrls),
352 .config = sd_config, 336 .config = sd_config,
353 .init = sd_init, 337 .init = sd_init,
338 .init_controls = sd_init_controls,
354 .start = sd_start, 339 .start = sd_start,
355 .stop0 = sd_stop0, 340 .stop0 = sd_stop0,
356}; 341};
@@ -373,6 +358,7 @@ static struct usb_driver sd_driver = {
373#ifdef CONFIG_PM 358#ifdef CONFIG_PM
374 .suspend = gspca_suspend, 359 .suspend = gspca_suspend,
375 .resume = gspca_resume, 360 .resume = gspca_resume,
361 .reset_resume = gspca_resume,
376#endif 362#endif
377}; 363};
378 364
diff --git a/drivers/media/video/gspca/w996Xcf.c b/drivers/media/video/gspca/w996Xcf.c
index 27d2cef0692a..9e3a909e0a00 100644
--- a/drivers/media/video/gspca/w996Xcf.c
+++ b/drivers/media/video/gspca/w996Xcf.c
@@ -404,9 +404,14 @@ static void w9968cf_set_crop_window(struct sd *sd)
404 } 404 }
405 405
406 if (sd->sensor == SEN_OV7620) { 406 if (sd->sensor == SEN_OV7620) {
407 /* Sigh, this is dependend on the clock / framerate changes 407 /*
408 made by the frequency control, sick. */ 408 * Sigh, this is dependend on the clock / framerate changes
409 if (sd->ctrls[FREQ].val == 1) { 409 * made by the frequency control, sick.
410 *
411 * Note we cannot use v4l2_ctrl_g_ctrl here, as we get called
412 * from ov519.c:setfreq() with the ctrl lock held!
413 */
414 if (sd->freq->val == 1) {
410 start_cropx = 277; 415 start_cropx = 277;
411 start_cropy = 37; 416 start_cropy = 37;
412 } else { 417 } else {
@@ -474,8 +479,9 @@ static void w9968cf_mode_init_regs(struct sd *sd)
474 /* We may get called multiple times (usb isoc bw negotiat.) */ 479 /* We may get called multiple times (usb isoc bw negotiat.) */
475 jpeg_define(sd->jpeg_hdr, sd->gspca_dev.height, 480 jpeg_define(sd->jpeg_hdr, sd->gspca_dev.height,
476 sd->gspca_dev.width, 0x22); /* JPEG 420 */ 481 sd->gspca_dev.width, 0x22); /* JPEG 420 */
477 jpeg_set_qual(sd->jpeg_hdr, sd->quality); 482 jpeg_set_qual(sd->jpeg_hdr, v4l2_ctrl_g_ctrl(sd->jpegqual));
478 w9968cf_upload_quantizationtables(sd); 483 w9968cf_upload_quantizationtables(sd);
484 v4l2_ctrl_grab(sd->jpegqual, true);
479 } 485 }
480 486
481 /* Video Capture Control Register */ 487 /* Video Capture Control Register */
@@ -514,6 +520,7 @@ static void w9968cf_mode_init_regs(struct sd *sd)
514 520
515static void w9968cf_stop0(struct sd *sd) 521static void w9968cf_stop0(struct sd *sd)
516{ 522{
523 v4l2_ctrl_grab(sd->jpegqual, false);
517 reg_w(sd, 0x39, 0x0000); /* disable JPEG encoder */ 524 reg_w(sd, 0x39, 0x0000); /* disable JPEG encoder */
518 reg_w(sd, 0x16, 0x0000); /* stop video capture */ 525 reg_w(sd, 0x16, 0x0000); /* stop video capture */
519} 526}
diff --git a/drivers/media/video/gspca/xirlink_cit.c b/drivers/media/video/gspca/xirlink_cit.c
index ecada178bceb..13b8d395d210 100644
--- a/drivers/media/video/gspca/xirlink_cit.c
+++ b/drivers/media/video/gspca/xirlink_cit.c
@@ -53,6 +53,7 @@ MODULE_PARM_DESC(rca_input,
53/* specific webcam descriptor */ 53/* specific webcam descriptor */
54struct sd { 54struct sd {
55 struct gspca_dev gspca_dev; /* !! must be the first item */ 55 struct gspca_dev gspca_dev; /* !! must be the first item */
56 struct v4l2_ctrl *lighting;
56 u8 model; 57 u8 model;
57#define CIT_MODEL0 0 /* bcd version 0.01 cams ie the xvp-500 */ 58#define CIT_MODEL0 0 /* bcd version 0.01 cams ie the xvp-500 */
58#define CIT_MODEL1 1 /* The model 1 - 4 nomenclature comes from the old */ 59#define CIT_MODEL1 1 /* The model 1 - 4 nomenclature comes from the old */
@@ -65,127 +66,10 @@ struct sd {
65 u8 stop_on_control_change; 66 u8 stop_on_control_change;
66 u8 sof_read; 67 u8 sof_read;
67 u8 sof_len; 68 u8 sof_len;
68 u8 contrast;
69 u8 brightness;
70 u8 hue;
71 u8 sharpness;
72 u8 lighting;
73 u8 hflip;
74}; 69};
75 70
76/* V4L2 controls supported by the driver */
77static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
78static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
79static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
80static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
81static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val);
82static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val);
83static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
84static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
85static int sd_setlighting(struct gspca_dev *gspca_dev, __s32 val);
86static int sd_getlighting(struct gspca_dev *gspca_dev, __s32 *val);
87static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
88static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
89static void sd_stop0(struct gspca_dev *gspca_dev); 71static void sd_stop0(struct gspca_dev *gspca_dev);
90 72
91static const struct ctrl sd_ctrls[] = {
92#define SD_BRIGHTNESS 0
93 {
94 {
95 .id = V4L2_CID_BRIGHTNESS,
96 .type = V4L2_CTRL_TYPE_INTEGER,
97 .name = "Brightness",
98 .minimum = 0,
99 .maximum = 63,
100 .step = 1,
101#define BRIGHTNESS_DEFAULT 32
102 .default_value = BRIGHTNESS_DEFAULT,
103 .flags = 0,
104 },
105 .set = sd_setbrightness,
106 .get = sd_getbrightness,
107 },
108#define SD_CONTRAST 1
109 {
110 {
111 .id = V4L2_CID_CONTRAST,
112 .type = V4L2_CTRL_TYPE_INTEGER,
113 .name = "contrast",
114 .minimum = 0,
115 .maximum = 20,
116 .step = 1,
117#define CONTRAST_DEFAULT 10
118 .default_value = CONTRAST_DEFAULT,
119 .flags = 0,
120 },
121 .set = sd_setcontrast,
122 .get = sd_getcontrast,
123 },
124#define SD_HUE 2
125 {
126 {
127 .id = V4L2_CID_HUE,
128 .type = V4L2_CTRL_TYPE_INTEGER,
129 .name = "Hue",
130 .minimum = 0,
131 .maximum = 127,
132 .step = 1,
133#define HUE_DEFAULT 63
134 .default_value = HUE_DEFAULT,
135 .flags = 0,
136 },
137 .set = sd_sethue,
138 .get = sd_gethue,
139 },
140#define SD_SHARPNESS 3
141 {
142 {
143 .id = V4L2_CID_SHARPNESS,
144 .type = V4L2_CTRL_TYPE_INTEGER,
145 .name = "Sharpness",
146 .minimum = 0,
147 .maximum = 6,
148 .step = 1,
149#define SHARPNESS_DEFAULT 3
150 .default_value = SHARPNESS_DEFAULT,
151 .flags = 0,
152 },
153 .set = sd_setsharpness,
154 .get = sd_getsharpness,
155 },
156#define SD_LIGHTING 4
157 {
158 {
159 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
160 .type = V4L2_CTRL_TYPE_INTEGER,
161 .name = "Lighting",
162 .minimum = 0,
163 .maximum = 2,
164 .step = 1,
165#define LIGHTING_DEFAULT 1
166 .default_value = LIGHTING_DEFAULT,
167 .flags = 0,
168 },
169 .set = sd_setlighting,
170 .get = sd_getlighting,
171 },
172#define SD_HFLIP 5
173 {
174 {
175 .id = V4L2_CID_HFLIP,
176 .type = V4L2_CTRL_TYPE_BOOLEAN,
177 .name = "Mirror",
178 .minimum = 0,
179 .maximum = 1,
180 .step = 1,
181#define HFLIP_DEFAULT 0
182 .default_value = HFLIP_DEFAULT,
183 },
184 .set = sd_sethflip,
185 .get = sd_gethflip,
186 },
187};
188
189static const struct v4l2_pix_format cif_yuv_mode[] = { 73static const struct v4l2_pix_format cif_yuv_mode[] = {
190 {176, 144, V4L2_PIX_FMT_CIT_YYVYUY, V4L2_FIELD_NONE, 74 {176, 144, V4L2_PIX_FMT_CIT_YYVYUY, V4L2_FIELD_NONE,
191 .bytesperline = 176, 75 .bytesperline = 176,
@@ -995,56 +879,36 @@ static int sd_config(struct gspca_dev *gspca_dev,
995 case CIT_MODEL0: 879 case CIT_MODEL0:
996 cam->cam_mode = model0_mode; 880 cam->cam_mode = model0_mode;
997 cam->nmodes = ARRAY_SIZE(model0_mode); 881 cam->nmodes = ARRAY_SIZE(model0_mode);
998 gspca_dev->ctrl_dis = ~((1 << SD_CONTRAST) | (1 << SD_HFLIP));
999 sd->sof_len = 4; 882 sd->sof_len = 4;
1000 break; 883 break;
1001 case CIT_MODEL1: 884 case CIT_MODEL1:
1002 cam->cam_mode = cif_yuv_mode; 885 cam->cam_mode = cif_yuv_mode;
1003 cam->nmodes = ARRAY_SIZE(cif_yuv_mode); 886 cam->nmodes = ARRAY_SIZE(cif_yuv_mode);
1004 gspca_dev->ctrl_dis = (1 << SD_HUE) | (1 << SD_HFLIP);
1005 sd->sof_len = 4; 887 sd->sof_len = 4;
1006 break; 888 break;
1007 case CIT_MODEL2: 889 case CIT_MODEL2:
1008 cam->cam_mode = model2_mode + 1; /* no 160x120 */ 890 cam->cam_mode = model2_mode + 1; /* no 160x120 */
1009 cam->nmodes = 3; 891 cam->nmodes = 3;
1010 gspca_dev->ctrl_dis = (1 << SD_CONTRAST) |
1011 (1 << SD_SHARPNESS) |
1012 (1 << SD_HFLIP);
1013 break; 892 break;
1014 case CIT_MODEL3: 893 case CIT_MODEL3:
1015 cam->cam_mode = vga_yuv_mode; 894 cam->cam_mode = vga_yuv_mode;
1016 cam->nmodes = ARRAY_SIZE(vga_yuv_mode); 895 cam->nmodes = ARRAY_SIZE(vga_yuv_mode);
1017 gspca_dev->ctrl_dis = (1 << SD_HUE) |
1018 (1 << SD_LIGHTING) |
1019 (1 << SD_HFLIP);
1020 sd->stop_on_control_change = 1; 896 sd->stop_on_control_change = 1;
1021 sd->sof_len = 4; 897 sd->sof_len = 4;
1022 break; 898 break;
1023 case CIT_MODEL4: 899 case CIT_MODEL4:
1024 cam->cam_mode = model2_mode; 900 cam->cam_mode = model2_mode;
1025 cam->nmodes = ARRAY_SIZE(model2_mode); 901 cam->nmodes = ARRAY_SIZE(model2_mode);
1026 gspca_dev->ctrl_dis = (1 << SD_CONTRAST) |
1027 (1 << SD_SHARPNESS) |
1028 (1 << SD_LIGHTING) |
1029 (1 << SD_HFLIP);
1030 break; 902 break;
1031 case CIT_IBM_NETCAM_PRO: 903 case CIT_IBM_NETCAM_PRO:
1032 cam->cam_mode = vga_yuv_mode; 904 cam->cam_mode = vga_yuv_mode;
1033 cam->nmodes = 2; /* no 640 x 480 */ 905 cam->nmodes = 2; /* no 640 x 480 */
1034 cam->input_flags = V4L2_IN_ST_VFLIP; 906 cam->input_flags = V4L2_IN_ST_VFLIP;
1035 gspca_dev->ctrl_dis = ~(1 << SD_CONTRAST);
1036 sd->stop_on_control_change = 1; 907 sd->stop_on_control_change = 1;
1037 sd->sof_len = 4; 908 sd->sof_len = 4;
1038 break; 909 break;
1039 } 910 }
1040 911
1041 sd->brightness = BRIGHTNESS_DEFAULT;
1042 sd->contrast = CONTRAST_DEFAULT;
1043 sd->hue = HUE_DEFAULT;
1044 sd->sharpness = SHARPNESS_DEFAULT;
1045 sd->lighting = LIGHTING_DEFAULT;
1046 sd->hflip = HFLIP_DEFAULT;
1047
1048 return 0; 912 return 0;
1049} 913}
1050 914
@@ -1287,7 +1151,7 @@ static int sd_init(struct gspca_dev *gspca_dev)
1287 return 0; 1151 return 0;
1288} 1152}
1289 1153
1290static int cit_set_brightness(struct gspca_dev *gspca_dev) 1154static int cit_set_brightness(struct gspca_dev *gspca_dev, s32 val)
1291{ 1155{
1292 struct sd *sd = (struct sd *) gspca_dev; 1156 struct sd *sd = (struct sd *) gspca_dev;
1293 int i; 1157 int i;
@@ -1299,19 +1163,19 @@ static int cit_set_brightness(struct gspca_dev *gspca_dev)
1299 break; 1163 break;
1300 case CIT_MODEL1: 1164 case CIT_MODEL1:
1301 /* Model 1: Brightness range 0 - 63 */ 1165 /* Model 1: Brightness range 0 - 63 */
1302 cit_Packet_Format1(gspca_dev, 0x0031, sd->brightness); 1166 cit_Packet_Format1(gspca_dev, 0x0031, val);
1303 cit_Packet_Format1(gspca_dev, 0x0032, sd->brightness); 1167 cit_Packet_Format1(gspca_dev, 0x0032, val);
1304 cit_Packet_Format1(gspca_dev, 0x0033, sd->brightness); 1168 cit_Packet_Format1(gspca_dev, 0x0033, val);
1305 break; 1169 break;
1306 case CIT_MODEL2: 1170 case CIT_MODEL2:
1307 /* Model 2: Brightness range 0x60 - 0xee */ 1171 /* Model 2: Brightness range 0x60 - 0xee */
1308 /* Scale 0 - 63 to 0x60 - 0xee */ 1172 /* Scale 0 - 63 to 0x60 - 0xee */
1309 i = 0x60 + sd->brightness * 2254 / 1000; 1173 i = 0x60 + val * 2254 / 1000;
1310 cit_model2_Packet1(gspca_dev, 0x001a, i); 1174 cit_model2_Packet1(gspca_dev, 0x001a, i);
1311 break; 1175 break;
1312 case CIT_MODEL3: 1176 case CIT_MODEL3:
1313 /* Model 3: Brightness range 'i' in [0x0C..0x3F] */ 1177 /* Model 3: Brightness range 'i' in [0x0C..0x3F] */
1314 i = sd->brightness; 1178 i = val;
1315 if (i < 0x0c) 1179 if (i < 0x0c)
1316 i = 0x0c; 1180 i = 0x0c;
1317 cit_model3_Packet1(gspca_dev, 0x0036, i); 1181 cit_model3_Packet1(gspca_dev, 0x0036, i);
@@ -1319,7 +1183,7 @@ static int cit_set_brightness(struct gspca_dev *gspca_dev)
1319 case CIT_MODEL4: 1183 case CIT_MODEL4:
1320 /* Model 4: Brightness range 'i' in [0x04..0xb4] */ 1184 /* Model 4: Brightness range 'i' in [0x04..0xb4] */
1321 /* Scale 0 - 63 to 0x04 - 0xb4 */ 1185 /* Scale 0 - 63 to 0x04 - 0xb4 */
1322 i = 0x04 + sd->brightness * 2794 / 1000; 1186 i = 0x04 + val * 2794 / 1000;
1323 cit_model4_BrightnessPacket(gspca_dev, i); 1187 cit_model4_BrightnessPacket(gspca_dev, i);
1324 break; 1188 break;
1325 } 1189 }
@@ -1327,7 +1191,7 @@ static int cit_set_brightness(struct gspca_dev *gspca_dev)
1327 return 0; 1191 return 0;
1328} 1192}
1329 1193
1330static int cit_set_contrast(struct gspca_dev *gspca_dev) 1194static int cit_set_contrast(struct gspca_dev *gspca_dev, s32 val)
1331{ 1195{
1332 struct sd *sd = (struct sd *) gspca_dev; 1196 struct sd *sd = (struct sd *) gspca_dev;
1333 1197
@@ -1335,16 +1199,16 @@ static int cit_set_contrast(struct gspca_dev *gspca_dev)
1335 case CIT_MODEL0: { 1199 case CIT_MODEL0: {
1336 int i; 1200 int i;
1337 /* gain 0-15, 0-20 -> 0-15 */ 1201 /* gain 0-15, 0-20 -> 0-15 */
1338 i = sd->contrast * 1000 / 1333; 1202 i = val * 1000 / 1333;
1339 cit_write_reg(gspca_dev, i, 0x0422); 1203 cit_write_reg(gspca_dev, i, 0x0422);
1340 /* gain 0-31, may not be lower then 0x0422, 0-20 -> 0-31 */ 1204 /* gain 0-31, may not be lower then 0x0422, 0-20 -> 0-31 */
1341 i = sd->contrast * 2000 / 1333; 1205 i = val * 2000 / 1333;
1342 cit_write_reg(gspca_dev, i, 0x0423); 1206 cit_write_reg(gspca_dev, i, 0x0423);
1343 /* gain 0-127, may not be lower then 0x0423, 0-20 -> 0-63 */ 1207 /* gain 0-127, may not be lower then 0x0423, 0-20 -> 0-63 */
1344 i = sd->contrast * 4000 / 1333; 1208 i = val * 4000 / 1333;
1345 cit_write_reg(gspca_dev, i, 0x0424); 1209 cit_write_reg(gspca_dev, i, 0x0424);
1346 /* gain 0-127, may not be lower then 0x0424, , 0-20 -> 0-127 */ 1210 /* gain 0-127, may not be lower then 0x0424, , 0-20 -> 0-127 */
1347 i = sd->contrast * 8000 / 1333; 1211 i = val * 8000 / 1333;
1348 cit_write_reg(gspca_dev, i, 0x0425); 1212 cit_write_reg(gspca_dev, i, 0x0425);
1349 break; 1213 break;
1350 } 1214 }
@@ -1355,7 +1219,7 @@ static int cit_set_contrast(struct gspca_dev *gspca_dev)
1355 case CIT_MODEL1: 1219 case CIT_MODEL1:
1356 { 1220 {
1357 /* Scale 0 - 20 to 15 - 0 */ 1221 /* Scale 0 - 20 to 15 - 0 */
1358 int i, new_contrast = (20 - sd->contrast) * 1000 / 1333; 1222 int i, new_contrast = (20 - val) * 1000 / 1333;
1359 for (i = 0; i < cit_model1_ntries; i++) { 1223 for (i = 0; i < cit_model1_ntries; i++) {
1360 cit_Packet_Format1(gspca_dev, 0x0014, new_contrast); 1224 cit_Packet_Format1(gspca_dev, 0x0014, new_contrast);
1361 cit_send_FF_04_02(gspca_dev); 1225 cit_send_FF_04_02(gspca_dev);
@@ -1377,20 +1241,20 @@ static int cit_set_contrast(struct gspca_dev *gspca_dev)
1377 { 0x01, 0x0e, 0x16 }, 1241 { 0x01, 0x0e, 0x16 },
1378 { 0x01, 0x10, 0x16 } /* Maximum */ 1242 { 0x01, 0x10, 0x16 } /* Maximum */
1379 }; 1243 };
1380 int i = sd->contrast / 3; 1244 int i = val / 3;
1381 cit_model3_Packet1(gspca_dev, 0x0067, cv[i].cv1); 1245 cit_model3_Packet1(gspca_dev, 0x0067, cv[i].cv1);
1382 cit_model3_Packet1(gspca_dev, 0x005b, cv[i].cv2); 1246 cit_model3_Packet1(gspca_dev, 0x005b, cv[i].cv2);
1383 cit_model3_Packet1(gspca_dev, 0x005c, cv[i].cv3); 1247 cit_model3_Packet1(gspca_dev, 0x005c, cv[i].cv3);
1384 break; 1248 break;
1385 } 1249 }
1386 case CIT_IBM_NETCAM_PRO: 1250 case CIT_IBM_NETCAM_PRO:
1387 cit_model3_Packet1(gspca_dev, 0x005b, sd->contrast + 1); 1251 cit_model3_Packet1(gspca_dev, 0x005b, val + 1);
1388 break; 1252 break;
1389 } 1253 }
1390 return 0; 1254 return 0;
1391} 1255}
1392 1256
1393static int cit_set_hue(struct gspca_dev *gspca_dev) 1257static int cit_set_hue(struct gspca_dev *gspca_dev, s32 val)
1394{ 1258{
1395 struct sd *sd = (struct sd *) gspca_dev; 1259 struct sd *sd = (struct sd *) gspca_dev;
1396 1260
@@ -1401,7 +1265,7 @@ static int cit_set_hue(struct gspca_dev *gspca_dev)
1401 /* No hue control for these models */ 1265 /* No hue control for these models */
1402 break; 1266 break;
1403 case CIT_MODEL2: 1267 case CIT_MODEL2:
1404 cit_model2_Packet1(gspca_dev, 0x0024, sd->hue); 1268 cit_model2_Packet1(gspca_dev, 0x0024, val);
1405 /* cit_model2_Packet1(gspca_dev, 0x0020, sat); */ 1269 /* cit_model2_Packet1(gspca_dev, 0x0020, sat); */
1406 break; 1270 break;
1407 case CIT_MODEL3: { 1271 case CIT_MODEL3: {
@@ -1409,7 +1273,7 @@ static int cit_set_hue(struct gspca_dev *gspca_dev)
1409 /* TESTME according to the ibmcam driver this does not work */ 1273 /* TESTME according to the ibmcam driver this does not work */
1410 if (0) { 1274 if (0) {
1411 /* Scale 0 - 127 to 0x05 - 0x37 */ 1275 /* Scale 0 - 127 to 0x05 - 0x37 */
1412 int i = 0x05 + sd->hue * 1000 / 2540; 1276 int i = 0x05 + val * 1000 / 2540;
1413 cit_model3_Packet1(gspca_dev, 0x007e, i); 1277 cit_model3_Packet1(gspca_dev, 0x007e, i);
1414 } 1278 }
1415 break; 1279 break;
@@ -1435,14 +1299,14 @@ static int cit_set_hue(struct gspca_dev *gspca_dev)
1435 cit_write_reg(gspca_dev, 160, 0x012e); /* Red gain */ 1299 cit_write_reg(gspca_dev, 160, 0x012e); /* Red gain */
1436 cit_write_reg(gspca_dev, 160, 0x0130); /* Blue gain */ 1300 cit_write_reg(gspca_dev, 160, 0x0130); /* Blue gain */
1437 cit_write_reg(gspca_dev, 0x8a28, 0x0124); 1301 cit_write_reg(gspca_dev, 0x8a28, 0x0124);
1438 cit_write_reg(gspca_dev, sd->hue, 0x012d); /* Hue */ 1302 cit_write_reg(gspca_dev, val, 0x012d); /* Hue */
1439 cit_write_reg(gspca_dev, 0xf545, 0x0124); 1303 cit_write_reg(gspca_dev, 0xf545, 0x0124);
1440 break; 1304 break;
1441 } 1305 }
1442 return 0; 1306 return 0;
1443} 1307}
1444 1308
1445static int cit_set_sharpness(struct gspca_dev *gspca_dev) 1309static int cit_set_sharpness(struct gspca_dev *gspca_dev, s32 val)
1446{ 1310{
1447 struct sd *sd = (struct sd *) gspca_dev; 1311 struct sd *sd = (struct sd *) gspca_dev;
1448 1312
@@ -1459,7 +1323,7 @@ static int cit_set_sharpness(struct gspca_dev *gspca_dev)
1459 0x11, 0x13, 0x16, 0x18, 0x1a, 0x8, 0x0a }; 1323 0x11, 0x13, 0x16, 0x18, 0x1a, 0x8, 0x0a };
1460 1324
1461 for (i = 0; i < cit_model1_ntries; i++) 1325 for (i = 0; i < cit_model1_ntries; i++)
1462 cit_PacketFormat2(gspca_dev, 0x0013, sa[sd->sharpness]); 1326 cit_PacketFormat2(gspca_dev, 0x0013, sa[val]);
1463 break; 1327 break;
1464 } 1328 }
1465 case CIT_MODEL3: 1329 case CIT_MODEL3:
@@ -1482,10 +1346,10 @@ static int cit_set_sharpness(struct gspca_dev *gspca_dev)
1482 { 0x03, 0x06, 0x05, 0x14 }, 1346 { 0x03, 0x06, 0x05, 0x14 },
1483 { 0x03, 0x07, 0x05, 0x14 } /* Sharpest */ 1347 { 0x03, 0x07, 0x05, 0x14 } /* Sharpest */
1484 }; 1348 };
1485 cit_model3_Packet1(gspca_dev, 0x0060, sv[sd->sharpness].sv1); 1349 cit_model3_Packet1(gspca_dev, 0x0060, sv[val].sv1);
1486 cit_model3_Packet1(gspca_dev, 0x0061, sv[sd->sharpness].sv2); 1350 cit_model3_Packet1(gspca_dev, 0x0061, sv[val].sv2);
1487 cit_model3_Packet1(gspca_dev, 0x0062, sv[sd->sharpness].sv3); 1351 cit_model3_Packet1(gspca_dev, 0x0062, sv[val].sv3);
1488 cit_model3_Packet1(gspca_dev, 0x0063, sv[sd->sharpness].sv4); 1352 cit_model3_Packet1(gspca_dev, 0x0063, sv[val].sv4);
1489 break; 1353 break;
1490 } 1354 }
1491 } 1355 }
@@ -1510,7 +1374,7 @@ static int cit_set_sharpness(struct gspca_dev *gspca_dev)
1510 * 1/5/00 Created. 1374 * 1/5/00 Created.
1511 * 2/20/00 Added support for Model 2 cameras. 1375 * 2/20/00 Added support for Model 2 cameras.
1512 */ 1376 */
1513static void cit_set_lighting(struct gspca_dev *gspca_dev) 1377static void cit_set_lighting(struct gspca_dev *gspca_dev, s32 val)
1514{ 1378{
1515 struct sd *sd = (struct sd *) gspca_dev; 1379 struct sd *sd = (struct sd *) gspca_dev;
1516 1380
@@ -1524,19 +1388,19 @@ static void cit_set_lighting(struct gspca_dev *gspca_dev)
1524 case CIT_MODEL1: { 1388 case CIT_MODEL1: {
1525 int i; 1389 int i;
1526 for (i = 0; i < cit_model1_ntries; i++) 1390 for (i = 0; i < cit_model1_ntries; i++)
1527 cit_Packet_Format1(gspca_dev, 0x0027, sd->lighting); 1391 cit_Packet_Format1(gspca_dev, 0x0027, val);
1528 break; 1392 break;
1529 } 1393 }
1530 } 1394 }
1531} 1395}
1532 1396
1533static void cit_set_hflip(struct gspca_dev *gspca_dev) 1397static void cit_set_hflip(struct gspca_dev *gspca_dev, s32 val)
1534{ 1398{
1535 struct sd *sd = (struct sd *) gspca_dev; 1399 struct sd *sd = (struct sd *) gspca_dev;
1536 1400
1537 switch (sd->model) { 1401 switch (sd->model) {
1538 case CIT_MODEL0: 1402 case CIT_MODEL0:
1539 if (sd->hflip) 1403 if (val)
1540 cit_write_reg(gspca_dev, 0x0020, 0x0115); 1404 cit_write_reg(gspca_dev, 0x0020, 0x0115);
1541 else 1405 else
1542 cit_write_reg(gspca_dev, 0x0040, 0x0115); 1406 cit_write_reg(gspca_dev, 0x0040, 0x0115);
@@ -1831,7 +1695,8 @@ static int cit_start_model1(struct gspca_dev *gspca_dev)
1831 cit_PacketFormat2(gspca_dev, 0x13, 0x1a); 1695 cit_PacketFormat2(gspca_dev, 0x13, 0x1a);
1832 1696
1833 /* Default lighting conditions */ 1697 /* Default lighting conditions */
1834 cit_Packet_Format1(gspca_dev, 0x0027, sd->lighting); 1698 cit_Packet_Format1(gspca_dev, 0x0027,
1699 v4l2_ctrl_g_ctrl(sd->lighting));
1835 } 1700 }
1836 1701
1837 /* Assorted init */ 1702 /* Assorted init */
@@ -2049,9 +1914,10 @@ static int cit_start_model2(struct gspca_dev *gspca_dev)
2049 break; 1914 break;
2050 } 1915 }
2051 1916
2052 /* FIXME this cannot be changed while streaming, so we 1917 cit_model2_Packet1(gspca_dev, 0x0028, v4l2_ctrl_g_ctrl(sd->lighting));
2053 should report a grabbed flag for this control. */ 1918 /* model2 cannot change the backlight compensation while streaming */
2054 cit_model2_Packet1(gspca_dev, 0x0028, sd->lighting); 1919 v4l2_ctrl_grab(sd->lighting, true);
1920
2055 /* color balance rg2 */ 1921 /* color balance rg2 */
2056 cit_model2_Packet1(gspca_dev, 0x001e, 0x002f); 1922 cit_model2_Packet1(gspca_dev, 0x001e, 0x002f);
2057 /* saturation */ 1923 /* saturation */
@@ -2755,13 +2621,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
2755 break; 2621 break;
2756 } 2622 }
2757 2623
2758 cit_set_brightness(gspca_dev);
2759 cit_set_contrast(gspca_dev);
2760 cit_set_hue(gspca_dev);
2761 cit_set_sharpness(gspca_dev);
2762 cit_set_lighting(gspca_dev);
2763 cit_set_hflip(gspca_dev);
2764
2765 /* Program max isoc packet size */ 2624 /* Program max isoc packet size */
2766 cit_write_reg(gspca_dev, packet_size >> 8, 0x0106); 2625 cit_write_reg(gspca_dev, packet_size >> 8, 0x0106);
2767 cit_write_reg(gspca_dev, packet_size & 0xff, 0x0107); 2626 cit_write_reg(gspca_dev, packet_size & 0xff, 0x0107);
@@ -2857,6 +2716,8 @@ static void sd_stop0(struct gspca_dev *gspca_dev)
2857 cit_write_reg(gspca_dev, 0x81, 0x0100); /* LED Off */ 2716 cit_write_reg(gspca_dev, 0x81, 0x0100); /* LED Off */
2858 break; 2717 break;
2859 case CIT_MODEL2: 2718 case CIT_MODEL2:
2719 v4l2_ctrl_grab(sd->lighting, false);
2720 /* Fall through! */
2860 case CIT_MODEL4: 2721 case CIT_MODEL4:
2861 cit_model2_Packet1(gspca_dev, 0x0030, 0x0004); 2722 cit_model2_Packet1(gspca_dev, 0x0030, 0x0004);
2862 2723
@@ -3055,152 +2916,6 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
3055 gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 2916 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
3056} 2917}
3057 2918
3058static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
3059{
3060 struct sd *sd = (struct sd *) gspca_dev;
3061
3062 sd->brightness = val;
3063 if (gspca_dev->streaming) {
3064 if (sd->stop_on_control_change)
3065 sd_stopN(gspca_dev);
3066 cit_set_brightness(gspca_dev);
3067 if (sd->stop_on_control_change)
3068 cit_restart_stream(gspca_dev);
3069 }
3070
3071 return 0;
3072}
3073
3074static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
3075{
3076 struct sd *sd = (struct sd *) gspca_dev;
3077
3078 *val = sd->brightness;
3079
3080 return 0;
3081}
3082
3083static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
3084{
3085 struct sd *sd = (struct sd *) gspca_dev;
3086
3087 sd->contrast = val;
3088 if (gspca_dev->streaming) {
3089 if (sd->stop_on_control_change)
3090 sd_stopN(gspca_dev);
3091 cit_set_contrast(gspca_dev);
3092 if (sd->stop_on_control_change)
3093 cit_restart_stream(gspca_dev);
3094 }
3095
3096 return 0;
3097}
3098
3099static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
3100{
3101 struct sd *sd = (struct sd *) gspca_dev;
3102
3103 *val = sd->contrast;
3104
3105 return 0;
3106}
3107
3108static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val)
3109{
3110 struct sd *sd = (struct sd *) gspca_dev;
3111
3112 sd->hue = val;
3113 if (gspca_dev->streaming) {
3114 if (sd->stop_on_control_change)
3115 sd_stopN(gspca_dev);
3116 cit_set_hue(gspca_dev);
3117 if (sd->stop_on_control_change)
3118 cit_restart_stream(gspca_dev);
3119 }
3120 return 0;
3121}
3122
3123static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val)
3124{
3125 struct sd *sd = (struct sd *) gspca_dev;
3126
3127 *val = sd->hue;
3128
3129 return 0;
3130}
3131
3132static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
3133{
3134 struct sd *sd = (struct sd *) gspca_dev;
3135
3136 sd->sharpness = val;
3137 if (gspca_dev->streaming) {
3138 if (sd->stop_on_control_change)
3139 sd_stopN(gspca_dev);
3140 cit_set_sharpness(gspca_dev);
3141 if (sd->stop_on_control_change)
3142 cit_restart_stream(gspca_dev);
3143 }
3144 return 0;
3145}
3146
3147static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
3148{
3149 struct sd *sd = (struct sd *) gspca_dev;
3150
3151 *val = sd->sharpness;
3152
3153 return 0;
3154}
3155
3156static int sd_setlighting(struct gspca_dev *gspca_dev, __s32 val)
3157{
3158 struct sd *sd = (struct sd *) gspca_dev;
3159
3160 sd->lighting = val;
3161 if (gspca_dev->streaming) {
3162 if (sd->stop_on_control_change)
3163 sd_stopN(gspca_dev);
3164 cit_set_lighting(gspca_dev);
3165 if (sd->stop_on_control_change)
3166 cit_restart_stream(gspca_dev);
3167 }
3168 return 0;
3169}
3170
3171static int sd_getlighting(struct gspca_dev *gspca_dev, __s32 *val)
3172{
3173 struct sd *sd = (struct sd *) gspca_dev;
3174
3175 *val = sd->lighting;
3176
3177 return 0;
3178}
3179
3180static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
3181{
3182 struct sd *sd = (struct sd *) gspca_dev;
3183
3184 sd->hflip = val;
3185 if (gspca_dev->streaming) {
3186 if (sd->stop_on_control_change)
3187 sd_stopN(gspca_dev);
3188 cit_set_hflip(gspca_dev);
3189 if (sd->stop_on_control_change)
3190 cit_restart_stream(gspca_dev);
3191 }
3192 return 0;
3193}
3194
3195static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
3196{
3197 struct sd *sd = (struct sd *) gspca_dev;
3198
3199 *val = sd->hflip;
3200
3201 return 0;
3202}
3203
3204#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) 2919#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
3205static void cit_check_button(struct gspca_dev *gspca_dev) 2920static void cit_check_button(struct gspca_dev *gspca_dev)
3206{ 2921{
@@ -3234,13 +2949,117 @@ static void cit_check_button(struct gspca_dev *gspca_dev)
3234} 2949}
3235#endif 2950#endif
3236 2951
2952static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
2953{
2954 struct gspca_dev *gspca_dev =
2955 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
2956 struct sd *sd = (struct sd *)gspca_dev;
2957
2958 gspca_dev->usb_err = 0;
2959
2960 if (!gspca_dev->streaming)
2961 return 0;
2962
2963 if (sd->stop_on_control_change)
2964 sd_stopN(gspca_dev);
2965 switch (ctrl->id) {
2966 case V4L2_CID_BRIGHTNESS:
2967 cit_set_brightness(gspca_dev, ctrl->val);
2968 break;
2969 case V4L2_CID_CONTRAST:
2970 cit_set_contrast(gspca_dev, ctrl->val);
2971 break;
2972 case V4L2_CID_HUE:
2973 cit_set_hue(gspca_dev, ctrl->val);
2974 break;
2975 case V4L2_CID_HFLIP:
2976 cit_set_hflip(gspca_dev, ctrl->val);
2977 break;
2978 case V4L2_CID_SHARPNESS:
2979 cit_set_sharpness(gspca_dev, ctrl->val);
2980 break;
2981 case V4L2_CID_BACKLIGHT_COMPENSATION:
2982 cit_set_lighting(gspca_dev, ctrl->val);
2983 break;
2984 }
2985 if (sd->stop_on_control_change)
2986 cit_restart_stream(gspca_dev);
2987 return gspca_dev->usb_err;
2988}
2989
2990static const struct v4l2_ctrl_ops sd_ctrl_ops = {
2991 .s_ctrl = sd_s_ctrl,
2992};
2993
2994static int sd_init_controls(struct gspca_dev *gspca_dev)
2995{
2996 struct sd *sd = (struct sd *)gspca_dev;
2997 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
2998 bool has_brightness;
2999 bool has_contrast;
3000 bool has_hue;
3001 bool has_sharpness;
3002 bool has_lighting;
3003 bool has_hflip;
3004
3005 has_brightness = has_contrast = has_hue =
3006 has_sharpness = has_hflip = has_lighting = false;
3007 switch (sd->model) {
3008 case CIT_MODEL0:
3009 has_contrast = has_hflip = true;
3010 break;
3011 case CIT_MODEL1:
3012 has_brightness = has_contrast =
3013 has_sharpness = has_lighting = true;
3014 break;
3015 case CIT_MODEL2:
3016 has_brightness = has_hue = has_lighting = true;
3017 break;
3018 case CIT_MODEL3:
3019 has_brightness = has_contrast = has_sharpness = true;
3020 break;
3021 case CIT_MODEL4:
3022 has_brightness = has_hue = true;
3023 break;
3024 case CIT_IBM_NETCAM_PRO:
3025 has_brightness = has_hue =
3026 has_sharpness = has_hflip = has_lighting = true;
3027 break;
3028 }
3029 gspca_dev->vdev.ctrl_handler = hdl;
3030 v4l2_ctrl_handler_init(hdl, 5);
3031 if (has_brightness)
3032 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3033 V4L2_CID_BRIGHTNESS, 0, 63, 1, 32);
3034 if (has_contrast)
3035 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3036 V4L2_CID_CONTRAST, 0, 20, 1, 10);
3037 if (has_hue)
3038 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3039 V4L2_CID_HUE, 0, 127, 1, 63);
3040 if (has_sharpness)
3041 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3042 V4L2_CID_SHARPNESS, 0, 6, 1, 3);
3043 if (has_lighting)
3044 sd->lighting = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3045 V4L2_CID_BACKLIGHT_COMPENSATION, 0, 2, 1, 1);
3046 if (has_hflip)
3047 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
3048 V4L2_CID_HFLIP, 0, 1, 1, 0);
3049
3050 if (hdl->error) {
3051 pr_err("Could not initialize controls\n");
3052 return hdl->error;
3053 }
3054 return 0;
3055}
3056
3237/* sub-driver description */ 3057/* sub-driver description */
3238static const struct sd_desc sd_desc = { 3058static const struct sd_desc sd_desc = {
3239 .name = MODULE_NAME, 3059 .name = MODULE_NAME,
3240 .ctrls = sd_ctrls,
3241 .nctrls = ARRAY_SIZE(sd_ctrls),
3242 .config = sd_config, 3060 .config = sd_config,
3243 .init = sd_init, 3061 .init = sd_init,
3062 .init_controls = sd_init_controls,
3244 .start = sd_start, 3063 .start = sd_start,
3245 .stopN = sd_stopN, 3064 .stopN = sd_stopN,
3246 .stop0 = sd_stop0, 3065 .stop0 = sd_stop0,
@@ -3253,10 +3072,9 @@ static const struct sd_desc sd_desc = {
3253 3072
3254static const struct sd_desc sd_desc_isoc_nego = { 3073static const struct sd_desc sd_desc_isoc_nego = {
3255 .name = MODULE_NAME, 3074 .name = MODULE_NAME,
3256 .ctrls = sd_ctrls,
3257 .nctrls = ARRAY_SIZE(sd_ctrls),
3258 .config = sd_config, 3075 .config = sd_config,
3259 .init = sd_init, 3076 .init = sd_init,
3077 .init_controls = sd_init_controls,
3260 .start = sd_start, 3078 .start = sd_start,
3261 .isoc_init = sd_isoc_init, 3079 .isoc_init = sd_isoc_init,
3262 .isoc_nego = sd_isoc_nego, 3080 .isoc_nego = sd_isoc_nego,
@@ -3320,6 +3138,7 @@ static struct usb_driver sd_driver = {
3320#ifdef CONFIG_PM 3138#ifdef CONFIG_PM
3321 .suspend = gspca_suspend, 3139 .suspend = gspca_suspend,
3322 .resume = gspca_resume, 3140 .resume = gspca_resume,
3141 .reset_resume = gspca_resume,
3323#endif 3142#endif
3324}; 3143};
3325 3144
diff --git a/drivers/media/video/m5mols/m5mols_controls.c b/drivers/media/video/m5mols/m5mols_controls.c
index 392a028730e2..fdbc205a2969 100644
--- a/drivers/media/video/m5mols/m5mols_controls.c
+++ b/drivers/media/video/m5mols/m5mols_controls.c
@@ -527,8 +527,8 @@ static const struct v4l2_ctrl_ops m5mols_ctrl_ops = {
527 527
528/* Supported manual ISO values */ 528/* Supported manual ISO values */
529static const s64 iso_qmenu[] = { 529static const s64 iso_qmenu[] = {
530 /* AE_ISO: 0x01...0x07 */ 530 /* AE_ISO: 0x01...0x07 (ISO: 50...3200) */
531 50, 100, 200, 400, 800, 1600, 3200 531 50000, 100000, 200000, 400000, 800000, 1600000, 3200000
532}; 532};
533 533
534/* Supported Exposure Bias values, -2.0EV...+2.0EV */ 534/* Supported Exposure Bias values, -2.0EV...+2.0EV */
diff --git a/drivers/media/video/mem2mem_testdev.c b/drivers/media/video/mem2mem_testdev.c
index f08cf38a496d..7efe9ad7acc7 100644
--- a/drivers/media/video/mem2mem_testdev.c
+++ b/drivers/media/video/mem2mem_testdev.c
@@ -27,6 +27,8 @@
27#include <media/v4l2-mem2mem.h> 27#include <media/v4l2-mem2mem.h>
28#include <media/v4l2-device.h> 28#include <media/v4l2-device.h>
29#include <media/v4l2-ioctl.h> 29#include <media/v4l2-ioctl.h>
30#include <media/v4l2-ctrls.h>
31#include <media/v4l2-event.h>
30#include <media/videobuf2-vmalloc.h> 32#include <media/videobuf2-vmalloc.h>
31 33
32#define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev" 34#define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
@@ -101,6 +103,8 @@ static struct m2mtest_fmt formats[] = {
101 }, 103 },
102}; 104};
103 105
106#define NUM_FORMATS ARRAY_SIZE(formats)
107
104/* Per-queue, driver-specific private data */ 108/* Per-queue, driver-specific private data */
105struct m2mtest_q_data { 109struct m2mtest_q_data {
106 unsigned int width; 110 unsigned int width;
@@ -114,50 +118,8 @@ enum {
114 V4L2_M2M_DST = 1, 118 V4L2_M2M_DST = 1,
115}; 119};
116 120
117#define V4L2_CID_TRANS_TIME_MSEC V4L2_CID_PRIVATE_BASE 121#define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
118#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_PRIVATE_BASE + 1) 122#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
119
120static struct v4l2_queryctrl m2mtest_ctrls[] = {
121 {
122 .id = V4L2_CID_HFLIP,
123 .type = V4L2_CTRL_TYPE_BOOLEAN,
124 .name = "Mirror",
125 .minimum = 0,
126 .maximum = 1,
127 .step = 1,
128 .default_value = 0,
129 .flags = 0,
130 }, {
131 .id = V4L2_CID_VFLIP,
132 .type = V4L2_CTRL_TYPE_BOOLEAN,
133 .name = "Vertical Mirror",
134 .minimum = 0,
135 .maximum = 1,
136 .step = 1,
137 .default_value = 0,
138 .flags = 0,
139 }, {
140 .id = V4L2_CID_TRANS_TIME_MSEC,
141 .type = V4L2_CTRL_TYPE_INTEGER,
142 .name = "Transaction time (msec)",
143 .minimum = 1,
144 .maximum = 10000,
145 .step = 100,
146 .default_value = 1000,
147 .flags = 0,
148 }, {
149 .id = V4L2_CID_TRANS_NUM_BUFS,
150 .type = V4L2_CTRL_TYPE_INTEGER,
151 .name = "Buffers per transaction",
152 .minimum = 1,
153 .maximum = MEM2MEM_DEF_NUM_BUFS,
154 .step = 1,
155 .default_value = 1,
156 .flags = 0,
157 },
158};
159
160#define NUM_FORMATS ARRAY_SIZE(formats)
161 123
162static struct m2mtest_fmt *find_format(struct v4l2_format *f) 124static struct m2mtest_fmt *find_format(struct v4l2_format *f)
163{ 125{
@@ -190,8 +152,11 @@ struct m2mtest_dev {
190}; 152};
191 153
192struct m2mtest_ctx { 154struct m2mtest_ctx {
155 struct v4l2_fh fh;
193 struct m2mtest_dev *dev; 156 struct m2mtest_dev *dev;
194 157
158 struct v4l2_ctrl_handler hdl;
159
195 /* Processed buffers in this transaction */ 160 /* Processed buffers in this transaction */
196 u8 num_processed; 161 u8 num_processed;
197 162
@@ -206,12 +171,19 @@ struct m2mtest_ctx {
206 /* Processing mode */ 171 /* Processing mode */
207 int mode; 172 int mode;
208 173
174 enum v4l2_colorspace colorspace;
175
209 struct v4l2_m2m_ctx *m2m_ctx; 176 struct v4l2_m2m_ctx *m2m_ctx;
210 177
211 /* Source and destination queue data */ 178 /* Source and destination queue data */
212 struct m2mtest_q_data q_data[2]; 179 struct m2mtest_q_data q_data[2];
213}; 180};
214 181
182static inline struct m2mtest_ctx *file2ctx(struct file *file)
183{
184 return container_of(file->private_data, struct m2mtest_ctx, fh);
185}
186
215static struct m2mtest_q_data *get_q_data(struct m2mtest_ctx *ctx, 187static struct m2mtest_q_data *get_q_data(struct m2mtest_ctx *ctx,
216 enum v4l2_buf_type type) 188 enum v4l2_buf_type type)
217{ 189{
@@ -227,18 +199,6 @@ static struct m2mtest_q_data *get_q_data(struct m2mtest_ctx *ctx,
227} 199}
228 200
229 201
230static struct v4l2_queryctrl *get_ctrl(int id)
231{
232 int i;
233
234 for (i = 0; i < ARRAY_SIZE(m2mtest_ctrls); ++i) {
235 if (id == m2mtest_ctrls[i].id)
236 return &m2mtest_ctrls[i];
237 }
238
239 return NULL;
240}
241
242static int device_process(struct m2mtest_ctx *ctx, 202static int device_process(struct m2mtest_ctx *ctx,
243 struct vb2_buffer *in_vb, 203 struct vb2_buffer *in_vb,
244 struct vb2_buffer *out_vb) 204 struct vb2_buffer *out_vb)
@@ -470,10 +430,9 @@ static int vidioc_querycap(struct file *file, void *priv,
470{ 430{
471 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1); 431 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
472 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1); 432 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
473 cap->bus_info[0] = 0; 433 strlcpy(cap->bus_info, MEM2MEM_NAME, sizeof(cap->bus_info));
474 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT 434 cap->capabilities = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
475 | V4L2_CAP_STREAMING; 435 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
476
477 return 0; 436 return 0;
478} 437}
479 438
@@ -536,6 +495,7 @@ static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
536 f->fmt.pix.pixelformat = q_data->fmt->fourcc; 495 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
537 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3; 496 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
538 f->fmt.pix.sizeimage = q_data->sizeimage; 497 f->fmt.pix.sizeimage = q_data->sizeimage;
498 f->fmt.pix.colorspace = ctx->colorspace;
539 499
540 return 0; 500 return 0;
541} 501}
@@ -543,13 +503,13 @@ static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
543static int vidioc_g_fmt_vid_out(struct file *file, void *priv, 503static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
544 struct v4l2_format *f) 504 struct v4l2_format *f)
545{ 505{
546 return vidioc_g_fmt(priv, f); 506 return vidioc_g_fmt(file2ctx(file), f);
547} 507}
548 508
549static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 509static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
550 struct v4l2_format *f) 510 struct v4l2_format *f)
551{ 511{
552 return vidioc_g_fmt(priv, f); 512 return vidioc_g_fmt(file2ctx(file), f);
553} 513}
554 514
555static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt) 515static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
@@ -588,7 +548,7 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
588 struct v4l2_format *f) 548 struct v4l2_format *f)
589{ 549{
590 struct m2mtest_fmt *fmt; 550 struct m2mtest_fmt *fmt;
591 struct m2mtest_ctx *ctx = priv; 551 struct m2mtest_ctx *ctx = file2ctx(file);
592 552
593 fmt = find_format(f); 553 fmt = find_format(f);
594 if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) { 554 if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
@@ -597,6 +557,7 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
597 f->fmt.pix.pixelformat); 557 f->fmt.pix.pixelformat);
598 return -EINVAL; 558 return -EINVAL;
599 } 559 }
560 f->fmt.pix.colorspace = ctx->colorspace;
600 561
601 return vidioc_try_fmt(f, fmt); 562 return vidioc_try_fmt(f, fmt);
602} 563}
@@ -605,7 +566,7 @@ static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
605 struct v4l2_format *f) 566 struct v4l2_format *f)
606{ 567{
607 struct m2mtest_fmt *fmt; 568 struct m2mtest_fmt *fmt;
608 struct m2mtest_ctx *ctx = priv; 569 struct m2mtest_ctx *ctx = file2ctx(file);
609 570
610 fmt = find_format(f); 571 fmt = find_format(f);
611 if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) { 572 if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
@@ -614,6 +575,8 @@ static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
614 f->fmt.pix.pixelformat); 575 f->fmt.pix.pixelformat);
615 return -EINVAL; 576 return -EINVAL;
616 } 577 }
578 if (!f->fmt.pix.colorspace)
579 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
617 580
618 return vidioc_try_fmt(f, fmt); 581 return vidioc_try_fmt(f, fmt);
619} 582}
@@ -658,25 +621,29 @@ static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
658 if (ret) 621 if (ret)
659 return ret; 622 return ret;
660 623
661 return vidioc_s_fmt(priv, f); 624 return vidioc_s_fmt(file2ctx(file), f);
662} 625}
663 626
664static int vidioc_s_fmt_vid_out(struct file *file, void *priv, 627static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
665 struct v4l2_format *f) 628 struct v4l2_format *f)
666{ 629{
630 struct m2mtest_ctx *ctx = file2ctx(file);
667 int ret; 631 int ret;
668 632
669 ret = vidioc_try_fmt_vid_out(file, priv, f); 633 ret = vidioc_try_fmt_vid_out(file, priv, f);
670 if (ret) 634 if (ret)
671 return ret; 635 return ret;
672 636
673 return vidioc_s_fmt(priv, f); 637 ret = vidioc_s_fmt(file2ctx(file), f);
638 if (!ret)
639 ctx->colorspace = f->fmt.pix.colorspace;
640 return ret;
674} 641}
675 642
676static int vidioc_reqbufs(struct file *file, void *priv, 643static int vidioc_reqbufs(struct file *file, void *priv,
677 struct v4l2_requestbuffers *reqbufs) 644 struct v4l2_requestbuffers *reqbufs)
678{ 645{
679 struct m2mtest_ctx *ctx = priv; 646 struct m2mtest_ctx *ctx = file2ctx(file);
680 647
681 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs); 648 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
682} 649}
@@ -684,21 +651,21 @@ static int vidioc_reqbufs(struct file *file, void *priv,
684static int vidioc_querybuf(struct file *file, void *priv, 651static int vidioc_querybuf(struct file *file, void *priv,
685 struct v4l2_buffer *buf) 652 struct v4l2_buffer *buf)
686{ 653{
687 struct m2mtest_ctx *ctx = priv; 654 struct m2mtest_ctx *ctx = file2ctx(file);
688 655
689 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf); 656 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
690} 657}
691 658
692static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf) 659static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
693{ 660{
694 struct m2mtest_ctx *ctx = priv; 661 struct m2mtest_ctx *ctx = file2ctx(file);
695 662
696 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf); 663 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
697} 664}
698 665
699static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf) 666static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
700{ 667{
701 struct m2mtest_ctx *ctx = priv; 668 struct m2mtest_ctx *ctx = file2ctx(file);
702 669
703 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf); 670 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
704} 671}
@@ -706,7 +673,7 @@ static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
706static int vidioc_streamon(struct file *file, void *priv, 673static int vidioc_streamon(struct file *file, void *priv,
707 enum v4l2_buf_type type) 674 enum v4l2_buf_type type)
708{ 675{
709 struct m2mtest_ctx *ctx = priv; 676 struct m2mtest_ctx *ctx = file2ctx(file);
710 677
711 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type); 678 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
712} 679}
@@ -714,101 +681,37 @@ static int vidioc_streamon(struct file *file, void *priv,
714static int vidioc_streamoff(struct file *file, void *priv, 681static int vidioc_streamoff(struct file *file, void *priv,
715 enum v4l2_buf_type type) 682 enum v4l2_buf_type type)
716{ 683{
717 struct m2mtest_ctx *ctx = priv; 684 struct m2mtest_ctx *ctx = file2ctx(file);
718 685
719 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type); 686 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
720} 687}
721 688
722static int vidioc_queryctrl(struct file *file, void *priv, 689static int m2mtest_s_ctrl(struct v4l2_ctrl *ctrl)
723 struct v4l2_queryctrl *qc)
724{
725 struct v4l2_queryctrl *c;
726
727 c = get_ctrl(qc->id);
728 if (!c)
729 return -EINVAL;
730
731 *qc = *c;
732 return 0;
733}
734
735static int vidioc_g_ctrl(struct file *file, void *priv,
736 struct v4l2_control *ctrl)
737{ 690{
738 struct m2mtest_ctx *ctx = priv; 691 struct m2mtest_ctx *ctx =
692 container_of(ctrl->handler, struct m2mtest_ctx, hdl);
739 693
740 switch (ctrl->id) { 694 switch (ctrl->id) {
741 case V4L2_CID_HFLIP: 695 case V4L2_CID_HFLIP:
742 ctrl->value = (ctx->mode & MEM2MEM_HFLIP) ? 1 : 0; 696 if (ctrl->val)
743 break;
744
745 case V4L2_CID_VFLIP:
746 ctrl->value = (ctx->mode & MEM2MEM_VFLIP) ? 1 : 0;
747 break;
748
749 case V4L2_CID_TRANS_TIME_MSEC:
750 ctrl->value = ctx->transtime;
751 break;
752
753 case V4L2_CID_TRANS_NUM_BUFS:
754 ctrl->value = ctx->translen;
755 break;
756
757 default:
758 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
759 return -EINVAL;
760 }
761
762 return 0;
763}
764
765static int check_ctrl_val(struct m2mtest_ctx *ctx, struct v4l2_control *ctrl)
766{
767 struct v4l2_queryctrl *c;
768
769 c = get_ctrl(ctrl->id);
770 if (!c)
771 return -EINVAL;
772
773 if (ctrl->value < c->minimum || ctrl->value > c->maximum) {
774 v4l2_err(&ctx->dev->v4l2_dev, "Value out of range\n");
775 return -ERANGE;
776 }
777
778 return 0;
779}
780
781static int vidioc_s_ctrl(struct file *file, void *priv,
782 struct v4l2_control *ctrl)
783{
784 struct m2mtest_ctx *ctx = priv;
785 int ret = 0;
786
787 ret = check_ctrl_val(ctx, ctrl);
788 if (ret != 0)
789 return ret;
790
791 switch (ctrl->id) {
792 case V4L2_CID_HFLIP:
793 if (ctrl->value)
794 ctx->mode |= MEM2MEM_HFLIP; 697 ctx->mode |= MEM2MEM_HFLIP;
795 else 698 else
796 ctx->mode &= ~MEM2MEM_HFLIP; 699 ctx->mode &= ~MEM2MEM_HFLIP;
797 break; 700 break;
798 701
799 case V4L2_CID_VFLIP: 702 case V4L2_CID_VFLIP:
800 if (ctrl->value) 703 if (ctrl->val)
801 ctx->mode |= MEM2MEM_VFLIP; 704 ctx->mode |= MEM2MEM_VFLIP;
802 else 705 else
803 ctx->mode &= ~MEM2MEM_VFLIP; 706 ctx->mode &= ~MEM2MEM_VFLIP;
804 break; 707 break;
805 708
806 case V4L2_CID_TRANS_TIME_MSEC: 709 case V4L2_CID_TRANS_TIME_MSEC:
807 ctx->transtime = ctrl->value; 710 ctx->transtime = ctrl->val;
808 break; 711 break;
809 712
810 case V4L2_CID_TRANS_NUM_BUFS: 713 case V4L2_CID_TRANS_NUM_BUFS:
811 ctx->translen = ctrl->value; 714 ctx->translen = ctrl->val;
812 break; 715 break;
813 716
814 default: 717 default:
@@ -819,6 +722,10 @@ static int vidioc_s_ctrl(struct file *file, void *priv,
819 return 0; 722 return 0;
820} 723}
821 724
725static const struct v4l2_ctrl_ops m2mtest_ctrl_ops = {
726 .s_ctrl = m2mtest_s_ctrl,
727};
728
822 729
823static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = { 730static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
824 .vidioc_querycap = vidioc_querycap, 731 .vidioc_querycap = vidioc_querycap,
@@ -841,10 +748,8 @@ static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
841 748
842 .vidioc_streamon = vidioc_streamon, 749 .vidioc_streamon = vidioc_streamon,
843 .vidioc_streamoff = vidioc_streamoff, 750 .vidioc_streamoff = vidioc_streamoff,
844 751 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
845 .vidioc_queryctrl = vidioc_queryctrl, 752 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
846 .vidioc_g_ctrl = vidioc_g_ctrl,
847 .vidioc_s_ctrl = vidioc_s_ctrl,
848}; 753};
849 754
850 755
@@ -956,6 +861,28 @@ static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *ds
956 return vb2_queue_init(dst_vq); 861 return vb2_queue_init(dst_vq);
957} 862}
958 863
864static const struct v4l2_ctrl_config m2mtest_ctrl_trans_time_msec = {
865 .ops = &m2mtest_ctrl_ops,
866 .id = V4L2_CID_TRANS_TIME_MSEC,
867 .name = "Transaction Time (msec)",
868 .type = V4L2_CTRL_TYPE_INTEGER,
869 .def = 1001,
870 .min = 1,
871 .max = 10001,
872 .step = 100,
873};
874
875static const struct v4l2_ctrl_config m2mtest_ctrl_trans_num_bufs = {
876 .ops = &m2mtest_ctrl_ops,
877 .id = V4L2_CID_TRANS_NUM_BUFS,
878 .name = "Buffers Per Transaction",
879 .type = V4L2_CTRL_TYPE_INTEGER,
880 .def = 1,
881 .min = 1,
882 .max = MEM2MEM_DEF_NUM_BUFS,
883 .step = 1,
884};
885
959/* 886/*
960 * File operations 887 * File operations
961 */ 888 */
@@ -963,30 +890,51 @@ static int m2mtest_open(struct file *file)
963{ 890{
964 struct m2mtest_dev *dev = video_drvdata(file); 891 struct m2mtest_dev *dev = video_drvdata(file);
965 struct m2mtest_ctx *ctx = NULL; 892 struct m2mtest_ctx *ctx = NULL;
893 struct v4l2_ctrl_handler *hdl;
966 894
967 ctx = kzalloc(sizeof *ctx, GFP_KERNEL); 895 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
968 if (!ctx) 896 if (!ctx)
969 return -ENOMEM; 897 return -ENOMEM;
970 898
971 file->private_data = ctx; 899 v4l2_fh_init(&ctx->fh, video_devdata(file));
900 file->private_data = &ctx->fh;
972 ctx->dev = dev; 901 ctx->dev = dev;
973 ctx->translen = MEM2MEM_DEF_TRANSLEN; 902 hdl = &ctx->hdl;
974 ctx->transtime = MEM2MEM_DEF_TRANSTIME; 903 v4l2_ctrl_handler_init(hdl, 4);
975 ctx->num_processed = 0; 904 v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
976 ctx->mode = 0; 905 v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
906 v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_time_msec, NULL);
907 v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_num_bufs, NULL);
908 if (hdl->error) {
909 int err = hdl->error;
910
911 v4l2_ctrl_handler_free(hdl);
912 return err;
913 }
914 ctx->fh.ctrl_handler = hdl;
915 v4l2_ctrl_handler_setup(hdl);
977 916
978 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0]; 917 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
979 ctx->q_data[V4L2_M2M_DST].fmt = &formats[0]; 918 ctx->q_data[V4L2_M2M_SRC].width = 640;
919 ctx->q_data[V4L2_M2M_SRC].height = 480;
920 ctx->q_data[V4L2_M2M_SRC].sizeimage =
921 ctx->q_data[V4L2_M2M_SRC].width *
922 ctx->q_data[V4L2_M2M_SRC].height *
923 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
924 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
925 ctx->colorspace = V4L2_COLORSPACE_REC709;
980 926
981 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init); 927 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
982 928
983 if (IS_ERR(ctx->m2m_ctx)) { 929 if (IS_ERR(ctx->m2m_ctx)) {
984 int ret = PTR_ERR(ctx->m2m_ctx); 930 int ret = PTR_ERR(ctx->m2m_ctx);
985 931
932 v4l2_ctrl_handler_free(hdl);
986 kfree(ctx); 933 kfree(ctx);
987 return ret; 934 return ret;
988 } 935 }
989 936
937 v4l2_fh_add(&ctx->fh);
990 atomic_inc(&dev->num_inst); 938 atomic_inc(&dev->num_inst);
991 939
992 dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx); 940 dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
@@ -997,10 +945,13 @@ static int m2mtest_open(struct file *file)
997static int m2mtest_release(struct file *file) 945static int m2mtest_release(struct file *file)
998{ 946{
999 struct m2mtest_dev *dev = video_drvdata(file); 947 struct m2mtest_dev *dev = video_drvdata(file);
1000 struct m2mtest_ctx *ctx = file->private_data; 948 struct m2mtest_ctx *ctx = file2ctx(file);
1001 949
1002 dprintk(dev, "Releasing instance %p\n", ctx); 950 dprintk(dev, "Releasing instance %p\n", ctx);
1003 951
952 v4l2_fh_del(&ctx->fh);
953 v4l2_fh_exit(&ctx->fh);
954 v4l2_ctrl_handler_free(&ctx->hdl);
1004 v4l2_m2m_ctx_release(ctx->m2m_ctx); 955 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1005 kfree(ctx); 956 kfree(ctx);
1006 957
@@ -1012,14 +963,14 @@ static int m2mtest_release(struct file *file)
1012static unsigned int m2mtest_poll(struct file *file, 963static unsigned int m2mtest_poll(struct file *file,
1013 struct poll_table_struct *wait) 964 struct poll_table_struct *wait)
1014{ 965{
1015 struct m2mtest_ctx *ctx = file->private_data; 966 struct m2mtest_ctx *ctx = file2ctx(file);
1016 967
1017 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait); 968 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1018} 969}
1019 970
1020static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma) 971static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
1021{ 972{
1022 struct m2mtest_ctx *ctx = file->private_data; 973 struct m2mtest_ctx *ctx = file2ctx(file);
1023 974
1024 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma); 975 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1025} 976}
diff --git a/drivers/media/video/mx2_emmaprp.c b/drivers/media/video/mx2_emmaprp.c
index 0bd5815de369..5f8a6f5b98f9 100644
--- a/drivers/media/video/mx2_emmaprp.c
+++ b/drivers/media/video/mx2_emmaprp.c
@@ -396,9 +396,13 @@ static int vidioc_querycap(struct file *file, void *priv,
396{ 396{
397 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1); 397 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
398 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1); 398 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
399 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT 399 /*
400 | V4L2_CAP_STREAMING; 400 * This is only a mem-to-mem video device. The capture and output
401 401 * device capability flags are left only for backward compatibility
402 * and are scheduled for removal.
403 */
404 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
405 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
402 return 0; 406 return 0;
403} 407}
404 408
diff --git a/drivers/media/video/ov2640.c b/drivers/media/video/ov2640.c
index 3c2c5d3bcc6b..7c44d1fe3c87 100644
--- a/drivers/media/video/ov2640.c
+++ b/drivers/media/video/ov2640.c
@@ -837,10 +837,8 @@ static int ov2640_g_fmt(struct v4l2_subdev *sd,
837 837
838 if (!priv->win) { 838 if (!priv->win) {
839 u32 width = W_SVGA, height = H_SVGA; 839 u32 width = W_SVGA, height = H_SVGA;
840 int ret = ov2640_set_params(client, &width, &height, 840 priv->win = ov2640_select_win(&width, &height);
841 V4L2_MBUS_FMT_UYVY8_2X8); 841 priv->cfmt_code = V4L2_MBUS_FMT_UYVY8_2X8;
842 if (ret < 0)
843 return ret;
844 } 842 }
845 843
846 mf->width = priv->win->width; 844 mf->width = priv->win->width;
diff --git a/drivers/media/video/ov772x.c b/drivers/media/video/ov772x.c
index 74e77d327ed8..6d79b89b8603 100644
--- a/drivers/media/video/ov772x.c
+++ b/drivers/media/video/ov772x.c
@@ -880,15 +880,11 @@ static int ov772x_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
880static int ov772x_g_fmt(struct v4l2_subdev *sd, 880static int ov772x_g_fmt(struct v4l2_subdev *sd,
881 struct v4l2_mbus_framefmt *mf) 881 struct v4l2_mbus_framefmt *mf)
882{ 882{
883 struct i2c_client *client = v4l2_get_subdevdata(sd);
884 struct ov772x_priv *priv = container_of(sd, struct ov772x_priv, subdev); 883 struct ov772x_priv *priv = container_of(sd, struct ov772x_priv, subdev);
885 884
886 if (!priv->win || !priv->cfmt) { 885 if (!priv->win || !priv->cfmt) {
887 u32 width = VGA_WIDTH, height = VGA_HEIGHT; 886 priv->cfmt = &ov772x_cfmts[0];
888 int ret = ov772x_set_params(client, &width, &height, 887 priv->win = ov772x_select_win(VGA_WIDTH, VGA_HEIGHT);
889 V4L2_MBUS_FMT_YUYV8_2X8);
890 if (ret < 0)
891 return ret;
892 } 888 }
893 889
894 mf->width = priv->win->width; 890 mf->width = priv->win->width;
diff --git a/drivers/media/video/ov9640.c b/drivers/media/video/ov9640.c
index 23412debb36b..9ed4ba4236c4 100644
--- a/drivers/media/video/ov9640.c
+++ b/drivers/media/video/ov9640.c
@@ -605,6 +605,7 @@ static int ov9640_video_probe(struct i2c_client *client)
605 devname = "ov9640"; 605 devname = "ov9640";
606 priv->model = V4L2_IDENT_OV9640; 606 priv->model = V4L2_IDENT_OV9640;
607 priv->revision = 2; 607 priv->revision = 2;
608 break;
608 case OV9640_V3: 609 case OV9640_V3:
609 devname = "ov9640"; 610 devname = "ov9640";
610 priv->model = V4L2_IDENT_OV9640; 611 priv->model = V4L2_IDENT_OV9640;
diff --git a/drivers/media/video/pms.c b/drivers/media/video/pms.c
index b4c679b3fb0f..77f9c92186f4 100644
--- a/drivers/media/video/pms.c
+++ b/drivers/media/video/pms.c
@@ -30,7 +30,6 @@
30#include <linux/ioport.h> 30#include <linux/ioport.h>
31#include <linux/init.h> 31#include <linux/init.h>
32#include <linux/mutex.h> 32#include <linux/mutex.h>
33#include <linux/slab.h>
34#include <linux/uaccess.h> 33#include <linux/uaccess.h>
35#include <linux/isa.h> 34#include <linux/isa.h>
36#include <asm/io.h> 35#include <asm/io.h>
diff --git a/drivers/media/video/s2255drv.c b/drivers/media/video/s2255drv.c
index 01c2179f0520..95007dda0c93 100644
--- a/drivers/media/video/s2255drv.c
+++ b/drivers/media/video/s2255drv.c
@@ -2686,3 +2686,4 @@ MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2686MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)"); 2686MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2687MODULE_LICENSE("GPL"); 2687MODULE_LICENSE("GPL");
2688MODULE_VERSION(S2255_VERSION); 2688MODULE_VERSION(S2255_VERSION);
2689MODULE_FIRMWARE(FIRMWARE_FILE_NAME);
diff --git a/drivers/media/video/s5p-fimc/fimc-capture.c b/drivers/media/video/s5p-fimc/fimc-capture.c
index 6a34183564d2..8e413dd3c0b0 100644
--- a/drivers/media/video/s5p-fimc/fimc-capture.c
+++ b/drivers/media/video/s5p-fimc/fimc-capture.c
@@ -480,48 +480,59 @@ static int fimc_capture_set_default_format(struct fimc_dev *fimc);
480static int fimc_capture_open(struct file *file) 480static int fimc_capture_open(struct file *file)
481{ 481{
482 struct fimc_dev *fimc = video_drvdata(file); 482 struct fimc_dev *fimc = video_drvdata(file);
483 int ret; 483 int ret = -EBUSY;
484 484
485 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); 485 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
486 486
487 if (mutex_lock_interruptible(&fimc->lock))
488 return -ERESTARTSYS;
489
487 if (fimc_m2m_active(fimc)) 490 if (fimc_m2m_active(fimc))
488 return -EBUSY; 491 goto unlock;
489 492
490 set_bit(ST_CAPT_BUSY, &fimc->state); 493 set_bit(ST_CAPT_BUSY, &fimc->state);
491 ret = pm_runtime_get_sync(&fimc->pdev->dev); 494 ret = pm_runtime_get_sync(&fimc->pdev->dev);
492 if (ret < 0) 495 if (ret < 0)
493 return ret; 496 goto unlock;
494 497
495 ret = v4l2_fh_open(file); 498 ret = v4l2_fh_open(file);
496 if (ret) 499 if (ret) {
497 return ret; 500 pm_runtime_put(&fimc->pdev->dev);
498 501 goto unlock;
499 if (++fimc->vid_cap.refcnt != 1) 502 }
500 return 0;
501 503
502 ret = fimc_pipeline_initialize(&fimc->pipeline, 504 if (++fimc->vid_cap.refcnt == 1) {
505 ret = fimc_pipeline_initialize(&fimc->pipeline,
503 &fimc->vid_cap.vfd->entity, true); 506 &fimc->vid_cap.vfd->entity, true);
504 if (ret < 0) {
505 clear_bit(ST_CAPT_BUSY, &fimc->state);
506 pm_runtime_put_sync(&fimc->pdev->dev);
507 fimc->vid_cap.refcnt--;
508 v4l2_fh_release(file);
509 return ret;
510 }
511 ret = fimc_capture_ctrls_create(fimc);
512 507
513 if (!ret && !fimc->vid_cap.user_subdev_api) 508 if (!ret && !fimc->vid_cap.user_subdev_api)
514 ret = fimc_capture_set_default_format(fimc); 509 ret = fimc_capture_set_default_format(fimc);
510
511 if (!ret)
512 ret = fimc_capture_ctrls_create(fimc);
515 513
514 if (ret < 0) {
515 clear_bit(ST_CAPT_BUSY, &fimc->state);
516 pm_runtime_put_sync(&fimc->pdev->dev);
517 fimc->vid_cap.refcnt--;
518 v4l2_fh_release(file);
519 }
520 }
521unlock:
522 mutex_unlock(&fimc->lock);
516 return ret; 523 return ret;
517} 524}
518 525
519static int fimc_capture_close(struct file *file) 526static int fimc_capture_close(struct file *file)
520{ 527{
521 struct fimc_dev *fimc = video_drvdata(file); 528 struct fimc_dev *fimc = video_drvdata(file);
529 int ret;
522 530
523 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); 531 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
524 532
533 if (mutex_lock_interruptible(&fimc->lock))
534 return -ERESTARTSYS;
535
525 if (--fimc->vid_cap.refcnt == 0) { 536 if (--fimc->vid_cap.refcnt == 0) {
526 clear_bit(ST_CAPT_BUSY, &fimc->state); 537 clear_bit(ST_CAPT_BUSY, &fimc->state);
527 fimc_stop_capture(fimc, false); 538 fimc_stop_capture(fimc, false);
@@ -535,22 +546,40 @@ static int fimc_capture_close(struct file *file)
535 vb2_queue_release(&fimc->vid_cap.vbq); 546 vb2_queue_release(&fimc->vid_cap.vbq);
536 fimc_ctrls_delete(fimc->vid_cap.ctx); 547 fimc_ctrls_delete(fimc->vid_cap.ctx);
537 } 548 }
538 return v4l2_fh_release(file); 549
550 ret = v4l2_fh_release(file);
551
552 mutex_unlock(&fimc->lock);
553 return ret;
539} 554}
540 555
541static unsigned int fimc_capture_poll(struct file *file, 556static unsigned int fimc_capture_poll(struct file *file,
542 struct poll_table_struct *wait) 557 struct poll_table_struct *wait)
543{ 558{
544 struct fimc_dev *fimc = video_drvdata(file); 559 struct fimc_dev *fimc = video_drvdata(file);
560 int ret;
545 561
546 return vb2_poll(&fimc->vid_cap.vbq, file, wait); 562 if (mutex_lock_interruptible(&fimc->lock))
563 return POLL_ERR;
564
565 ret = vb2_poll(&fimc->vid_cap.vbq, file, wait);
566 mutex_unlock(&fimc->lock);
567
568 return ret;
547} 569}
548 570
549static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma) 571static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
550{ 572{
551 struct fimc_dev *fimc = video_drvdata(file); 573 struct fimc_dev *fimc = video_drvdata(file);
574 int ret;
575
576 if (mutex_lock_interruptible(&fimc->lock))
577 return -ERESTARTSYS;
552 578
553 return vb2_mmap(&fimc->vid_cap.vbq, vma); 579 ret = vb2_mmap(&fimc->vid_cap.vbq, vma);
580 mutex_unlock(&fimc->lock);
581
582 return ret;
554} 583}
555 584
556static const struct v4l2_file_operations fimc_capture_fops = { 585static const struct v4l2_file_operations fimc_capture_fops = {
@@ -1589,10 +1618,7 @@ static int fimc_register_capture_device(struct fimc_dev *fimc,
1589 vfd->minor = -1; 1618 vfd->minor = -1;
1590 vfd->release = video_device_release; 1619 vfd->release = video_device_release;
1591 vfd->lock = &fimc->lock; 1620 vfd->lock = &fimc->lock;
1592 /* Locking in file operations other than ioctl should be done 1621
1593 by the driver, not the V4L2 core.
1594 This driver needs auditing so that this flag can be removed. */
1595 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
1596 video_set_drvdata(vfd, fimc); 1622 video_set_drvdata(vfd, fimc);
1597 1623
1598 vid_cap = &fimc->vid_cap; 1624 vid_cap = &fimc->vid_cap;
diff --git a/drivers/media/video/s5p-fimc/fimc-core.h b/drivers/media/video/s5p-fimc/fimc-core.h
index 95b27ae5cf27..808ccc621846 100644
--- a/drivers/media/video/s5p-fimc/fimc-core.h
+++ b/drivers/media/video/s5p-fimc/fimc-core.h
@@ -27,9 +27,6 @@
27#include <media/v4l2-mediabus.h> 27#include <media/v4l2-mediabus.h>
28#include <media/s5p_fimc.h> 28#include <media/s5p_fimc.h>
29 29
30#define err(fmt, args...) \
31 printk(KERN_ERR "%s:%d: " fmt "\n", __func__, __LINE__, ##args)
32
33#define dbg(fmt, args...) \ 30#define dbg(fmt, args...) \
34 pr_debug("%s:%d: " fmt "\n", __func__, __LINE__, ##args) 31 pr_debug("%s:%d: " fmt "\n", __func__, __LINE__, ##args)
35 32
diff --git a/drivers/media/video/s5p-fimc/fimc-m2m.c b/drivers/media/video/s5p-fimc/fimc-m2m.c
index 4c58e0570962..c587011d80ef 100644
--- a/drivers/media/video/s5p-fimc/fimc-m2m.c
+++ b/drivers/media/video/s5p-fimc/fimc-m2m.c
@@ -259,7 +259,12 @@ static int fimc_m2m_querycap(struct file *file, void *fh,
259 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1); 259 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
260 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1); 260 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
261 cap->bus_info[0] = 0; 261 cap->bus_info[0] = 0;
262 cap->capabilities = V4L2_CAP_STREAMING | 262 /*
263 * This is only a mem-to-mem video device. The capture and output
264 * device capability flags are left only for backward compatibility
265 * and are scheduled for removal.
266 */
267 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE |
263 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE; 268 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
264 269
265 return 0; 270 return 0;
@@ -642,21 +647,25 @@ static int fimc_m2m_open(struct file *file)
642{ 647{
643 struct fimc_dev *fimc = video_drvdata(file); 648 struct fimc_dev *fimc = video_drvdata(file);
644 struct fimc_ctx *ctx; 649 struct fimc_ctx *ctx;
645 int ret; 650 int ret = -EBUSY;
646 651
647 dbg("pid: %d, state: 0x%lx, refcnt: %d", 652 dbg("pid: %d, state: 0x%lx, refcnt: %d",
648 task_pid_nr(current), fimc->state, fimc->vid_cap.refcnt); 653 task_pid_nr(current), fimc->state, fimc->vid_cap.refcnt);
649 654
655 if (mutex_lock_interruptible(&fimc->lock))
656 return -ERESTARTSYS;
650 /* 657 /*
651 * Return if the corresponding video capture node 658 * Return if the corresponding video capture node
652 * is already opened. 659 * is already opened.
653 */ 660 */
654 if (fimc->vid_cap.refcnt > 0) 661 if (fimc->vid_cap.refcnt > 0)
655 return -EBUSY; 662 goto unlock;
656 663
657 ctx = kzalloc(sizeof *ctx, GFP_KERNEL); 664 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
658 if (!ctx) 665 if (!ctx) {
659 return -ENOMEM; 666 ret = -ENOMEM;
667 goto unlock;
668 }
660 v4l2_fh_init(&ctx->fh, fimc->m2m.vfd); 669 v4l2_fh_init(&ctx->fh, fimc->m2m.vfd);
661 ctx->fimc_dev = fimc; 670 ctx->fimc_dev = fimc;
662 671
@@ -687,6 +696,8 @@ static int fimc_m2m_open(struct file *file)
687 696
688 if (fimc->m2m.refcnt++ == 0) 697 if (fimc->m2m.refcnt++ == 0)
689 set_bit(ST_M2M_RUN, &fimc->state); 698 set_bit(ST_M2M_RUN, &fimc->state);
699
700 mutex_unlock(&fimc->lock);
690 return 0; 701 return 0;
691 702
692error_c: 703error_c:
@@ -695,6 +706,8 @@ error_fh:
695 v4l2_fh_del(&ctx->fh); 706 v4l2_fh_del(&ctx->fh);
696 v4l2_fh_exit(&ctx->fh); 707 v4l2_fh_exit(&ctx->fh);
697 kfree(ctx); 708 kfree(ctx);
709unlock:
710 mutex_unlock(&fimc->lock);
698 return ret; 711 return ret;
699} 712}
700 713
@@ -706,6 +719,9 @@ static int fimc_m2m_release(struct file *file)
706 dbg("pid: %d, state: 0x%lx, refcnt= %d", 719 dbg("pid: %d, state: 0x%lx, refcnt= %d",
707 task_pid_nr(current), fimc->state, fimc->m2m.refcnt); 720 task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
708 721
722 if (mutex_lock_interruptible(&fimc->lock))
723 return -ERESTARTSYS;
724
709 v4l2_m2m_ctx_release(ctx->m2m_ctx); 725 v4l2_m2m_ctx_release(ctx->m2m_ctx);
710 fimc_ctrls_delete(ctx); 726 fimc_ctrls_delete(ctx);
711 v4l2_fh_del(&ctx->fh); 727 v4l2_fh_del(&ctx->fh);
@@ -714,6 +730,8 @@ static int fimc_m2m_release(struct file *file)
714 if (--fimc->m2m.refcnt <= 0) 730 if (--fimc->m2m.refcnt <= 0)
715 clear_bit(ST_M2M_RUN, &fimc->state); 731 clear_bit(ST_M2M_RUN, &fimc->state);
716 kfree(ctx); 732 kfree(ctx);
733
734 mutex_unlock(&fimc->lock);
717 return 0; 735 return 0;
718} 736}
719 737
@@ -721,16 +739,32 @@ static unsigned int fimc_m2m_poll(struct file *file,
721 struct poll_table_struct *wait) 739 struct poll_table_struct *wait)
722{ 740{
723 struct fimc_ctx *ctx = fh_to_ctx(file->private_data); 741 struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
742 struct fimc_dev *fimc = ctx->fimc_dev;
743 int ret;
744
745 if (mutex_lock_interruptible(&fimc->lock))
746 return -ERESTARTSYS;
747
748 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
749 mutex_unlock(&fimc->lock);
724 750
725 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait); 751 return ret;
726} 752}
727 753
728 754
729static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma) 755static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
730{ 756{
731 struct fimc_ctx *ctx = fh_to_ctx(file->private_data); 757 struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
758 struct fimc_dev *fimc = ctx->fimc_dev;
759 int ret;
760
761 if (mutex_lock_interruptible(&fimc->lock))
762 return -ERESTARTSYS;
732 763
733 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma); 764 ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
765 mutex_unlock(&fimc->lock);
766
767 return ret;
734} 768}
735 769
736static const struct v4l2_file_operations fimc_m2m_fops = { 770static const struct v4l2_file_operations fimc_m2m_fops = {
@@ -772,10 +806,6 @@ int fimc_register_m2m_device(struct fimc_dev *fimc,
772 vfd->minor = -1; 806 vfd->minor = -1;
773 vfd->release = video_device_release; 807 vfd->release = video_device_release;
774 vfd->lock = &fimc->lock; 808 vfd->lock = &fimc->lock;
775 /* Locking in file operations other than ioctl should be done
776 by the driver, not the V4L2 core.
777 This driver needs auditing so that this flag can be removed. */
778 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
779 809
780 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.m2m", fimc->id); 810 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.m2m", fimc->id);
781 video_set_drvdata(vfd, fimc); 811 video_set_drvdata(vfd, fimc);
diff --git a/drivers/media/video/s5p-fimc/fimc-reg.c b/drivers/media/video/s5p-fimc/fimc-reg.c
index 1fc4ce8446f5..0e3eb9ce4f98 100644
--- a/drivers/media/video/s5p-fimc/fimc-reg.c
+++ b/drivers/media/video/s5p-fimc/fimc-reg.c
@@ -667,7 +667,8 @@ int fimc_hw_set_camera_type(struct fimc_dev *fimc,
667 FIMC_REG_CIGCTRL_SELCAM_MIPI | FIMC_REG_CIGCTRL_CAMIF_SELWB | 667 FIMC_REG_CIGCTRL_SELCAM_MIPI | FIMC_REG_CIGCTRL_CAMIF_SELWB |
668 FIMC_REG_CIGCTRL_SELCAM_MIPI_A | FIMC_REG_CIGCTRL_CAM_JPEG); 668 FIMC_REG_CIGCTRL_SELCAM_MIPI_A | FIMC_REG_CIGCTRL_CAM_JPEG);
669 669
670 if (cam->bus_type == FIMC_MIPI_CSI2) { 670 switch (cam->bus_type) {
671 case FIMC_MIPI_CSI2:
671 cfg |= FIMC_REG_CIGCTRL_SELCAM_MIPI; 672 cfg |= FIMC_REG_CIGCTRL_SELCAM_MIPI;
672 673
673 if (cam->mux_id == 0) 674 if (cam->mux_id == 0)
@@ -683,23 +684,24 @@ int fimc_hw_set_camera_type(struct fimc_dev *fimc,
683 cfg |= FIMC_REG_CIGCTRL_CAM_JPEG; 684 cfg |= FIMC_REG_CIGCTRL_CAM_JPEG;
684 break; 685 break;
685 default: 686 default:
686 v4l2_err(fimc->vid_cap.vfd, 687 v4l2_err(vid_cap->vfd,
687 "Not supported camera pixel format: %d", 688 "Not supported camera pixel format: %#x\n",
688 vid_cap->mf.code); 689 vid_cap->mf.code);
689 return -EINVAL; 690 return -EINVAL;
690 } 691 }
691 tmp |= (csis_data_alignment == 32) << 8; 692 tmp |= (csis_data_alignment == 32) << 8;
692 693
693 writel(tmp, fimc->regs + FIMC_REG_CSIIMGFMT); 694 writel(tmp, fimc->regs + FIMC_REG_CSIIMGFMT);
694 695 break;
695 } else if (cam->bus_type == FIMC_ITU_601 || 696 case FIMC_ITU_601...FIMC_ITU_656:
696 cam->bus_type == FIMC_ITU_656) {
697 if (cam->mux_id == 0) /* ITU-A, ITU-B: 0, 1 */ 697 if (cam->mux_id == 0) /* ITU-A, ITU-B: 0, 1 */
698 cfg |= FIMC_REG_CIGCTRL_SELCAM_ITU_A; 698 cfg |= FIMC_REG_CIGCTRL_SELCAM_ITU_A;
699 } else if (cam->bus_type == FIMC_LCD_WB) { 699 break;
700 case FIMC_LCD_WB:
700 cfg |= FIMC_REG_CIGCTRL_CAMIF_SELWB; 701 cfg |= FIMC_REG_CIGCTRL_CAMIF_SELWB;
701 } else { 702 break;
702 err("invalid camera bus type selected\n"); 703 default:
704 v4l2_err(vid_cap->vfd, "Invalid camera bus type selected\n");
703 return -EINVAL; 705 return -EINVAL;
704 } 706 }
705 writel(cfg, fimc->regs + FIMC_REG_CIGCTRL); 707 writel(cfg, fimc->regs + FIMC_REG_CIGCTRL);
diff --git a/drivers/media/video/s5p-g2d/g2d.c b/drivers/media/video/s5p-g2d/g2d.c
index 7c98ee7377ee..7c2200435206 100644
--- a/drivers/media/video/s5p-g2d/g2d.c
+++ b/drivers/media/video/s5p-g2d/g2d.c
@@ -290,8 +290,13 @@ static int vidioc_querycap(struct file *file, void *priv,
290 strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1); 290 strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1);
291 cap->bus_info[0] = 0; 291 cap->bus_info[0] = 0;
292 cap->version = KERNEL_VERSION(1, 0, 0); 292 cap->version = KERNEL_VERSION(1, 0, 0);
293 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT 293 /*
294 | V4L2_CAP_STREAMING; 294 * This is only a mem-to-mem video device. The capture and output
295 * device capability flags are left only for backward compatibility
296 * and are scheduled for removal.
297 */
298 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
299 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
295 return 0; 300 return 0;
296} 301}
297 302
diff --git a/drivers/media/video/s5p-jpeg/jpeg-core.c b/drivers/media/video/s5p-jpeg/jpeg-core.c
index 95f23024b17d..813b801238d1 100644
--- a/drivers/media/video/s5p-jpeg/jpeg-core.c
+++ b/drivers/media/video/s5p-jpeg/jpeg-core.c
@@ -489,9 +489,13 @@ static int s5p_jpeg_querycap(struct file *file, void *priv,
489 sizeof(cap->card)); 489 sizeof(cap->card));
490 } 490 }
491 cap->bus_info[0] = 0; 491 cap->bus_info[0] = 0;
492 cap->capabilities = V4L2_CAP_STREAMING | 492 /*
493 V4L2_CAP_VIDEO_CAPTURE | 493 * This is only a mem-to-mem video device. The capture and output
494 V4L2_CAP_VIDEO_OUTPUT; 494 * device capability flags are left only for backward compatibility
495 * and are scheduled for removal.
496 */
497 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M |
498 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
495 return 0; 499 return 0;
496} 500}
497 501
diff --git a/drivers/media/video/s5p-mfc/s5p_mfc_dec.c b/drivers/media/video/s5p-mfc/s5p_mfc_dec.c
index feea867f318c..c5d567f87d77 100644
--- a/drivers/media/video/s5p-mfc/s5p_mfc_dec.c
+++ b/drivers/media/video/s5p-mfc/s5p_mfc_dec.c
@@ -220,8 +220,14 @@ static int vidioc_querycap(struct file *file, void *priv,
220 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1); 220 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
221 cap->bus_info[0] = 0; 221 cap->bus_info[0] = 0;
222 cap->version = KERNEL_VERSION(1, 0, 0); 222 cap->version = KERNEL_VERSION(1, 0, 0);
223 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE_MPLANE | 223 /*
224 V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_STREAMING; 224 * This is only a mem-to-mem video device. The capture and output
225 * device capability flags are left only for backward compatibility
226 * and are scheduled for removal.
227 */
228 cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
229 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
230 V4L2_CAP_VIDEO_OUTPUT_MPLANE;
225 return 0; 231 return 0;
226} 232}
227 233
diff --git a/drivers/media/video/s5p-mfc/s5p_mfc_enc.c b/drivers/media/video/s5p-mfc/s5p_mfc_enc.c
index 158b78989b89..aa1c244cf66e 100644
--- a/drivers/media/video/s5p-mfc/s5p_mfc_enc.c
+++ b/drivers/media/video/s5p-mfc/s5p_mfc_enc.c
@@ -779,9 +779,14 @@ static int vidioc_querycap(struct file *file, void *priv,
779 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1); 779 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
780 cap->bus_info[0] = 0; 780 cap->bus_info[0] = 0;
781 cap->version = KERNEL_VERSION(1, 0, 0); 781 cap->version = KERNEL_VERSION(1, 0, 0);
782 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE_MPLANE 782 /*
783 | V4L2_CAP_VIDEO_OUTPUT_MPLANE 783 * This is only a mem-to-mem video device. The capture and output
784 | V4L2_CAP_STREAMING; 784 * device capability flags are left only for backward compatibility
785 * and are scheduled for removal.
786 */
787 cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
788 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
789 V4L2_CAP_VIDEO_OUTPUT_MPLANE;
785 return 0; 790 return 0;
786} 791}
787 792
diff --git a/drivers/media/video/saa7164/saa7164-i2c.c b/drivers/media/video/saa7164/saa7164-i2c.c
index 26148f76cba2..4f7e3b42263f 100644
--- a/drivers/media/video/saa7164/saa7164-i2c.c
+++ b/drivers/media/video/saa7164/saa7164-i2c.c
@@ -69,15 +69,6 @@ err:
69 return retval; 69 return retval;
70} 70}
71 71
72void saa7164_call_i2c_clients(struct saa7164_i2c *bus, unsigned int cmd,
73 void *arg)
74{
75 if (bus->i2c_rc != 0)
76 return;
77
78 i2c_clients_command(&bus->i2c_adap, cmd, arg);
79}
80
81static u32 saa7164_functionality(struct i2c_adapter *adap) 72static u32 saa7164_functionality(struct i2c_adapter *adap)
82{ 73{
83 return I2C_FUNC_I2C; 74 return I2C_FUNC_I2C;
@@ -106,21 +97,14 @@ int saa7164_i2c_register(struct saa7164_i2c *bus)
106 97
107 dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr); 98 dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr);
108 99
109 memcpy(&bus->i2c_adap, &saa7164_i2c_adap_template, 100 bus->i2c_adap = saa7164_i2c_adap_template;
110 sizeof(bus->i2c_adap)); 101 bus->i2c_client = saa7164_i2c_client_template;
111
112 memcpy(&bus->i2c_algo, &saa7164_i2c_algo_template,
113 sizeof(bus->i2c_algo));
114
115 memcpy(&bus->i2c_client, &saa7164_i2c_client_template,
116 sizeof(bus->i2c_client));
117 102
118 bus->i2c_adap.dev.parent = &dev->pci->dev; 103 bus->i2c_adap.dev.parent = &dev->pci->dev;
119 104
120 strlcpy(bus->i2c_adap.name, bus->dev->name, 105 strlcpy(bus->i2c_adap.name, bus->dev->name,
121 sizeof(bus->i2c_adap.name)); 106 sizeof(bus->i2c_adap.name));
122 107
123 bus->i2c_algo.data = bus;
124 bus->i2c_adap.algo_data = bus; 108 bus->i2c_adap.algo_data = bus;
125 i2c_set_adapdata(&bus->i2c_adap, bus); 109 i2c_set_adapdata(&bus->i2c_adap, bus);
126 i2c_add_adapter(&bus->i2c_adap); 110 i2c_add_adapter(&bus->i2c_adap);
diff --git a/drivers/media/video/saa7164/saa7164.h b/drivers/media/video/saa7164/saa7164.h
index 8d120e3baf70..35219b9b0fbc 100644
--- a/drivers/media/video/saa7164/saa7164.h
+++ b/drivers/media/video/saa7164/saa7164.h
@@ -46,7 +46,6 @@
46 46
47#include <linux/pci.h> 47#include <linux/pci.h>
48#include <linux/i2c.h> 48#include <linux/i2c.h>
49#include <linux/i2c-algo-bit.h>
50#include <linux/kdev_t.h> 49#include <linux/kdev_t.h>
51#include <linux/mutex.h> 50#include <linux/mutex.h>
52#include <linux/crc32.h> 51#include <linux/crc32.h>
@@ -251,7 +250,6 @@ struct saa7164_i2c {
251 250
252 /* I2C I/O */ 251 /* I2C I/O */
253 struct i2c_adapter i2c_adap; 252 struct i2c_adapter i2c_adap;
254 struct i2c_algo_bit_data i2c_algo;
255 struct i2c_client i2c_client; 253 struct i2c_client i2c_client;
256 u32 i2c_rc; 254 u32 i2c_rc;
257}; 255};
diff --git a/drivers/media/video/smiapp/smiapp-core.c b/drivers/media/video/smiapp/smiapp-core.c
index f466a7edcb2a..bfd47c106134 100644
--- a/drivers/media/video/smiapp/smiapp-core.c
+++ b/drivers/media/video/smiapp/smiapp-core.c
@@ -33,7 +33,6 @@
33#include <linux/module.h> 33#include <linux/module.h>
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/regulator/consumer.h> 35#include <linux/regulator/consumer.h>
36#include <linux/slab.h>
37#include <linux/v4l2-mediabus.h> 36#include <linux/v4l2-mediabus.h>
38#include <media/v4l2-device.h> 37#include <media/v4l2-device.h>
39 38
diff --git a/drivers/media/video/soc_camera.c b/drivers/media/video/soc_camera.c
index 0421bf9453b4..b03ffecb7438 100644
--- a/drivers/media/video/soc_camera.c
+++ b/drivers/media/video/soc_camera.c
@@ -62,7 +62,7 @@ static int soc_camera_power_on(struct soc_camera_device *icd,
62 } 62 }
63 63
64 if (icl->power) { 64 if (icl->power) {
65 ret = icl->power(icd->pdev, 1); 65 ret = icl->power(icd->control, 1);
66 if (ret < 0) { 66 if (ret < 0) {
67 dev_err(icd->pdev, 67 dev_err(icd->pdev,
68 "Platform failed to power-on the camera.\n"); 68 "Platform failed to power-on the camera.\n");
@@ -78,7 +78,7 @@ static int soc_camera_power_on(struct soc_camera_device *icd,
78 78
79esdpwr: 79esdpwr:
80 if (icl->power) 80 if (icl->power)
81 icl->power(icd->pdev, 0); 81 icl->power(icd->control, 0);
82elinkpwr: 82elinkpwr:
83 regulator_bulk_disable(icl->num_regulators, 83 regulator_bulk_disable(icl->num_regulators,
84 icl->regulators); 84 icl->regulators);
@@ -95,7 +95,7 @@ static int soc_camera_power_off(struct soc_camera_device *icd,
95 return ret; 95 return ret;
96 96
97 if (icl->power) { 97 if (icl->power) {
98 ret = icl->power(icd->pdev, 0); 98 ret = icl->power(icd->control, 0);
99 if (ret < 0) { 99 if (ret < 0) {
100 dev_err(icd->pdev, 100 dev_err(icd->pdev,
101 "Platform failed to power-off the camera.\n"); 101 "Platform failed to power-off the camera.\n");
@@ -1518,6 +1518,7 @@ static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1518} 1518}
1519 1519
1520static struct platform_driver __refdata soc_camera_pdrv = { 1520static struct platform_driver __refdata soc_camera_pdrv = {
1521 .probe = soc_camera_pdrv_probe,
1521 .remove = __devexit_p(soc_camera_pdrv_remove), 1522 .remove = __devexit_p(soc_camera_pdrv_remove),
1522 .driver = { 1523 .driver = {
1523 .name = "soc-camera-pdrv", 1524 .name = "soc-camera-pdrv",
@@ -1527,7 +1528,7 @@ static struct platform_driver __refdata soc_camera_pdrv = {
1527 1528
1528static int __init soc_camera_init(void) 1529static int __init soc_camera_init(void)
1529{ 1530{
1530 return platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe); 1531 return platform_driver_register(&soc_camera_pdrv);
1531} 1532}
1532 1533
1533static void __exit soc_camera_exit(void) 1534static void __exit soc_camera_exit(void)
diff --git a/drivers/media/video/tlg2300/pd-main.c b/drivers/media/video/tlg2300/pd-main.c
index c096b3f74200..7b1f6ebd0e2c 100644
--- a/drivers/media/video/tlg2300/pd-main.c
+++ b/drivers/media/video/tlg2300/pd-main.c
@@ -53,7 +53,8 @@ int debug_mode;
53module_param(debug_mode, int, 0644); 53module_param(debug_mode, int, 0644);
54MODULE_PARM_DESC(debug_mode, "0 = disable, 1 = enable, 2 = verbose"); 54MODULE_PARM_DESC(debug_mode, "0 = disable, 1 = enable, 2 = verbose");
55 55
56static const char *firmware_name = "tlg2300_firmware.bin"; 56#define TLG2300_FIRMWARE "tlg2300_firmware.bin"
57static const char *firmware_name = TLG2300_FIRMWARE;
57static struct usb_driver poseidon_driver; 58static struct usb_driver poseidon_driver;
58static LIST_HEAD(pd_device_list); 59static LIST_HEAD(pd_device_list);
59 60
@@ -532,3 +533,4 @@ MODULE_AUTHOR("Telegent Systems");
532MODULE_DESCRIPTION("For tlg2300-based USB device "); 533MODULE_DESCRIPTION("For tlg2300-based USB device ");
533MODULE_LICENSE("GPL"); 534MODULE_LICENSE("GPL");
534MODULE_VERSION("0.0.2"); 535MODULE_VERSION("0.0.2");
536MODULE_FIRMWARE(TLG2300_FIRMWARE);
diff --git a/drivers/media/video/tvp5150.c b/drivers/media/video/tvp5150.c
index 0d897cb1774a..a751b6c146fd 100644
--- a/drivers/media/video/tvp5150.c
+++ b/drivers/media/video/tvp5150.c
@@ -257,7 +257,7 @@ static inline void tvp5150_selmux(struct v4l2_subdev *sd)
257 int opmode = 0; 257 int opmode = 0;
258 struct tvp5150 *decoder = to_tvp5150(sd); 258 struct tvp5150 *decoder = to_tvp5150(sd);
259 int input = 0; 259 int input = 0;
260 unsigned char val; 260 int val;
261 261
262 if ((decoder->output & TVP5150_BLACK_SCREEN) || !decoder->enable) 262 if ((decoder->output & TVP5150_BLACK_SCREEN) || !decoder->enable)
263 input = 8; 263 input = 8;
diff --git a/drivers/media/video/tw9910.c b/drivers/media/video/tw9910.c
index 8768efb8508a..9f53eacb66e3 100644
--- a/drivers/media/video/tw9910.c
+++ b/drivers/media/video/tw9910.c
@@ -699,11 +699,9 @@ static int tw9910_g_fmt(struct v4l2_subdev *sd,
699 struct tw9910_priv *priv = to_tw9910(client); 699 struct tw9910_priv *priv = to_tw9910(client);
700 700
701 if (!priv->scale) { 701 if (!priv->scale) {
702 int ret; 702 priv->scale = tw9910_select_norm(priv->norm, 640, 480);
703 u32 width = 640, height = 480; 703 if (!priv->scale)
704 ret = tw9910_set_frame(sd, &width, &height); 704 return -EINVAL;
705 if (ret < 0)
706 return ret;
707 } 705 }
708 706
709 mf->width = priv->scale->width; 707 mf->width = priv->scale->width;
diff --git a/drivers/media/video/v4l2-compat-ioctl32.c b/drivers/media/video/v4l2-compat-ioctl32.c
index ac365cfb3706..9ebd5c540d10 100644
--- a/drivers/media/video/v4l2-compat-ioctl32.c
+++ b/drivers/media/video/v4l2-compat-ioctl32.c
@@ -1025,6 +1025,7 @@ long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
1025 case VIDIOC_ENUM_DV_TIMINGS: 1025 case VIDIOC_ENUM_DV_TIMINGS:
1026 case VIDIOC_QUERY_DV_TIMINGS: 1026 case VIDIOC_QUERY_DV_TIMINGS:
1027 case VIDIOC_DV_TIMINGS_CAP: 1027 case VIDIOC_DV_TIMINGS_CAP:
1028 case VIDIOC_ENUM_FREQ_BANDS:
1028 ret = do_video_ioctl(file, cmd, arg); 1029 ret = do_video_ioctl(file, cmd, arg);
1029 break; 1030 break;
1030 1031
diff --git a/drivers/media/video/v4l2-ctrls.c b/drivers/media/video/v4l2-ctrls.c
index 9abd9abd4502..b6a2ee71e5c3 100644
--- a/drivers/media/video/v4l2-ctrls.c
+++ b/drivers/media/video/v4l2-ctrls.c
@@ -755,6 +755,7 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
755 case V4L2_CID_HUE_AUTO: 755 case V4L2_CID_HUE_AUTO:
756 case V4L2_CID_CHROMA_AGC: 756 case V4L2_CID_CHROMA_AGC:
757 case V4L2_CID_COLOR_KILLER: 757 case V4L2_CID_COLOR_KILLER:
758 case V4L2_CID_AUTOBRIGHTNESS:
758 case V4L2_CID_MPEG_AUDIO_MUTE: 759 case V4L2_CID_MPEG_AUDIO_MUTE:
759 case V4L2_CID_MPEG_VIDEO_MUTE: 760 case V4L2_CID_MPEG_VIDEO_MUTE:
760 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: 761 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
@@ -2120,7 +2121,7 @@ static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
2120 2121
2121 /* First zero the helper field in the master control references */ 2122 /* First zero the helper field in the master control references */
2122 for (i = 0; i < cs->count; i++) 2123 for (i = 0; i < cs->count; i++)
2123 helpers[i].mref->helper = 0; 2124 helpers[i].mref->helper = NULL;
2124 for (i = 0, h = helpers; i < cs->count; i++, h++) { 2125 for (i = 0, h = helpers; i < cs->count; i++, h++) {
2125 struct v4l2_ctrl_ref *mref = h->mref; 2126 struct v4l2_ctrl_ref *mref = h->mref;
2126 2127
diff --git a/drivers/media/video/v4l2-dev.c b/drivers/media/video/v4l2-dev.c
index af70f931727c..07aeafca9eaa 100644
--- a/drivers/media/video/v4l2-dev.c
+++ b/drivers/media/video/v4l2-dev.c
@@ -478,12 +478,12 @@ static int v4l2_open(struct inode *inode, struct file *filp)
478 } 478 }
479 479
480err: 480err:
481 /* decrease the refcount in case of an error */
482 if (ret)
483 video_put(vdev);
484 if (vdev->debug) 481 if (vdev->debug)
485 printk(KERN_DEBUG "%s: open (%d)\n", 482 printk(KERN_DEBUG "%s: open (%d)\n",
486 video_device_node_name(vdev), ret); 483 video_device_node_name(vdev), ret);
484 /* decrease the refcount in case of an error */
485 if (ret)
486 video_put(vdev);
487 return ret; 487 return ret;
488} 488}
489 489
@@ -500,12 +500,12 @@ static int v4l2_release(struct inode *inode, struct file *filp)
500 if (test_bit(V4L2_FL_LOCK_ALL_FOPS, &vdev->flags)) 500 if (test_bit(V4L2_FL_LOCK_ALL_FOPS, &vdev->flags))
501 mutex_unlock(vdev->lock); 501 mutex_unlock(vdev->lock);
502 } 502 }
503 /* decrease the refcount unconditionally since the release()
504 return value is ignored. */
505 video_put(vdev);
506 if (vdev->debug) 503 if (vdev->debug)
507 printk(KERN_DEBUG "%s: release\n", 504 printk(KERN_DEBUG "%s: release\n",
508 video_device_node_name(vdev)); 505 video_device_node_name(vdev));
506 /* decrease the refcount unconditionally since the release()
507 return value is ignored. */
508 video_put(vdev);
509 return ret; 509 return ret;
510} 510}
511 511
@@ -697,7 +697,8 @@ static void determine_valid_ioctls(struct video_device *vdev)
697 SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd); 697 SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
698 SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd); 698 SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
699 SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd); 699 SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
700 if (ops->vidioc_g_parm || vdev->vfl_type == VFL_TYPE_GRABBER) 700 if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER &&
701 (ops->vidioc_g_std || vdev->tvnorms)))
701 set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls); 702 set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
702 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm); 703 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
703 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner); 704 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
@@ -729,6 +730,8 @@ static void determine_valid_ioctls(struct video_device *vdev)
729 SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event); 730 SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
730 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs); 731 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
731 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf); 732 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
733 if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
734 set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
732 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls, 735 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
733 BASE_VIDIOC_PRIVATE); 736 BASE_VIDIOC_PRIVATE);
734} 737}
diff --git a/drivers/media/video/v4l2-ioctl.c b/drivers/media/video/v4l2-ioctl.c
index 4b4059d64272..c3b7b5f59b32 100644
--- a/drivers/media/video/v4l2-ioctl.c
+++ b/drivers/media/video/v4l2-ioctl.c
@@ -825,6 +825,17 @@ static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
825 p->service_lines[1][i]); 825 p->service_lines[1][i]);
826} 826}
827 827
828static void v4l_print_freq_band(const void *arg, bool write_only)
829{
830 const struct v4l2_frequency_band *p = arg;
831
832 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
833 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
834 p->tuner, p->type, p->index,
835 p->capability, p->rangelow,
836 p->rangehigh, p->modulation);
837}
838
828static void v4l_print_u32(const void *arg, bool write_only) 839static void v4l_print_u32(const void *arg, bool write_only)
829{ 840{
830 pr_cont("value=%u\n", *(const u32 *)arg); 841 pr_cont("value=%u\n", *(const u32 *)arg);
@@ -1245,10 +1256,14 @@ static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1245{ 1256{
1246 struct video_device *vfd = video_devdata(file); 1257 struct video_device *vfd = video_devdata(file);
1247 struct v4l2_tuner *p = arg; 1258 struct v4l2_tuner *p = arg;
1259 int err;
1248 1260
1249 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ? 1261 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1250 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 1262 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1251 return ops->vidioc_g_tuner(file, fh, p); 1263 err = ops->vidioc_g_tuner(file, fh, p);
1264 if (!err)
1265 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1266 return err;
1252} 1267}
1253 1268
1254static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops, 1269static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
@@ -1262,6 +1277,18 @@ static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1262 return ops->vidioc_s_tuner(file, fh, p); 1277 return ops->vidioc_s_tuner(file, fh, p);
1263} 1278}
1264 1279
1280static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1281 struct file *file, void *fh, void *arg)
1282{
1283 struct v4l2_modulator *p = arg;
1284 int err;
1285
1286 err = ops->vidioc_g_modulator(file, fh, p);
1287 if (!err)
1288 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1289 return err;
1290}
1291
1265static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops, 1292static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1266 struct file *file, void *fh, void *arg) 1293 struct file *file, void *fh, void *arg)
1267{ 1294{
@@ -1486,7 +1513,8 @@ static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1486{ 1513{
1487 struct video_device *vfd = video_devdata(file); 1514 struct video_device *vfd = video_devdata(file);
1488 struct v4l2_queryctrl *p = arg; 1515 struct v4l2_queryctrl *p = arg;
1489 struct v4l2_fh *vfh = fh; 1516 struct v4l2_fh *vfh =
1517 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1490 1518
1491 if (vfh && vfh->ctrl_handler) 1519 if (vfh && vfh->ctrl_handler)
1492 return v4l2_queryctrl(vfh->ctrl_handler, p); 1520 return v4l2_queryctrl(vfh->ctrl_handler, p);
@@ -1502,7 +1530,8 @@ static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1502{ 1530{
1503 struct video_device *vfd = video_devdata(file); 1531 struct video_device *vfd = video_devdata(file);
1504 struct v4l2_querymenu *p = arg; 1532 struct v4l2_querymenu *p = arg;
1505 struct v4l2_fh *vfh = fh; 1533 struct v4l2_fh *vfh =
1534 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1506 1535
1507 if (vfh && vfh->ctrl_handler) 1536 if (vfh && vfh->ctrl_handler)
1508 return v4l2_querymenu(vfh->ctrl_handler, p); 1537 return v4l2_querymenu(vfh->ctrl_handler, p);
@@ -1518,7 +1547,8 @@ static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1518{ 1547{
1519 struct video_device *vfd = video_devdata(file); 1548 struct video_device *vfd = video_devdata(file);
1520 struct v4l2_control *p = arg; 1549 struct v4l2_control *p = arg;
1521 struct v4l2_fh *vfh = fh; 1550 struct v4l2_fh *vfh =
1551 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1522 struct v4l2_ext_controls ctrls; 1552 struct v4l2_ext_controls ctrls;
1523 struct v4l2_ext_control ctrl; 1553 struct v4l2_ext_control ctrl;
1524 1554
@@ -1551,7 +1581,8 @@ static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1551{ 1581{
1552 struct video_device *vfd = video_devdata(file); 1582 struct video_device *vfd = video_devdata(file);
1553 struct v4l2_control *p = arg; 1583 struct v4l2_control *p = arg;
1554 struct v4l2_fh *vfh = fh; 1584 struct v4l2_fh *vfh =
1585 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1555 struct v4l2_ext_controls ctrls; 1586 struct v4l2_ext_controls ctrls;
1556 struct v4l2_ext_control ctrl; 1587 struct v4l2_ext_control ctrl;
1557 1588
@@ -1579,7 +1610,8 @@ static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1579{ 1610{
1580 struct video_device *vfd = video_devdata(file); 1611 struct video_device *vfd = video_devdata(file);
1581 struct v4l2_ext_controls *p = arg; 1612 struct v4l2_ext_controls *p = arg;
1582 struct v4l2_fh *vfh = fh; 1613 struct v4l2_fh *vfh =
1614 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1583 1615
1584 p->error_idx = p->count; 1616 p->error_idx = p->count;
1585 if (vfh && vfh->ctrl_handler) 1617 if (vfh && vfh->ctrl_handler)
@@ -1597,7 +1629,8 @@ static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1597{ 1629{
1598 struct video_device *vfd = video_devdata(file); 1630 struct video_device *vfd = video_devdata(file);
1599 struct v4l2_ext_controls *p = arg; 1631 struct v4l2_ext_controls *p = arg;
1600 struct v4l2_fh *vfh = fh; 1632 struct v4l2_fh *vfh =
1633 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1601 1634
1602 p->error_idx = p->count; 1635 p->error_idx = p->count;
1603 if (vfh && vfh->ctrl_handler) 1636 if (vfh && vfh->ctrl_handler)
@@ -1615,7 +1648,8 @@ static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1615{ 1648{
1616 struct video_device *vfd = video_devdata(file); 1649 struct video_device *vfd = video_devdata(file);
1617 struct v4l2_ext_controls *p = arg; 1650 struct v4l2_ext_controls *p = arg;
1618 struct v4l2_fh *vfh = fh; 1651 struct v4l2_fh *vfh =
1652 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1619 1653
1620 p->error_idx = p->count; 1654 p->error_idx = p->count;
1621 if (vfh && vfh->ctrl_handler) 1655 if (vfh && vfh->ctrl_handler)
@@ -1798,6 +1832,57 @@ static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1798 return ops->vidioc_g_sliced_vbi_cap(file, fh, p); 1832 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1799} 1833}
1800 1834
1835static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
1836 struct file *file, void *fh, void *arg)
1837{
1838 struct video_device *vfd = video_devdata(file);
1839 struct v4l2_frequency_band *p = arg;
1840 enum v4l2_tuner_type type;
1841 int err;
1842
1843 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1844 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1845
1846 if (type != p->type)
1847 return -EINVAL;
1848 if (ops->vidioc_enum_freq_bands)
1849 return ops->vidioc_enum_freq_bands(file, fh, p);
1850 if (ops->vidioc_g_tuner) {
1851 struct v4l2_tuner t = {
1852 .index = p->tuner,
1853 .type = type,
1854 };
1855
1856 err = ops->vidioc_g_tuner(file, fh, &t);
1857 if (err)
1858 return err;
1859 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1860 p->rangelow = t.rangelow;
1861 p->rangehigh = t.rangehigh;
1862 p->modulation = (type == V4L2_TUNER_RADIO) ?
1863 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1864 return 0;
1865 }
1866 if (ops->vidioc_g_modulator) {
1867 struct v4l2_modulator m = {
1868 .index = p->tuner,
1869 };
1870
1871 if (type != V4L2_TUNER_RADIO)
1872 return -EINVAL;
1873 err = ops->vidioc_g_modulator(file, fh, &m);
1874 if (err)
1875 return err;
1876 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1877 p->rangelow = m.rangelow;
1878 p->rangehigh = m.rangehigh;
1879 p->modulation = (type == V4L2_TUNER_RADIO) ?
1880 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1881 return 0;
1882 }
1883 return -ENOTTY;
1884}
1885
1801struct v4l2_ioctl_info { 1886struct v4l2_ioctl_info {
1802 unsigned int ioctl; 1887 unsigned int ioctl;
1803 u32 flags; 1888 u32 flags;
@@ -1806,7 +1891,7 @@ struct v4l2_ioctl_info {
1806 u32 offset; 1891 u32 offset;
1807 int (*func)(const struct v4l2_ioctl_ops *ops, 1892 int (*func)(const struct v4l2_ioctl_ops *ops,
1808 struct file *file, void *fh, void *p); 1893 struct file *file, void *fh, void *p);
1809 }; 1894 } u;
1810 void (*debug)(const void *arg, bool write_only); 1895 void (*debug)(const void *arg, bool write_only);
1811}; 1896};
1812 1897
@@ -1831,9 +1916,7 @@ struct v4l2_ioctl_info {
1831 .ioctl = _ioctl, \ 1916 .ioctl = _ioctl, \
1832 .flags = _flags | INFO_FL_STD, \ 1917 .flags = _flags | INFO_FL_STD, \
1833 .name = #_ioctl, \ 1918 .name = #_ioctl, \
1834 { \ 1919 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
1835 .offset = offsetof(struct v4l2_ioctl_ops, _vidioc),\
1836 }, \
1837 .debug = _debug, \ 1920 .debug = _debug, \
1838 } 1921 }
1839 1922
@@ -1842,9 +1925,7 @@ struct v4l2_ioctl_info {
1842 .ioctl = _ioctl, \ 1925 .ioctl = _ioctl, \
1843 .flags = _flags | INFO_FL_FUNC, \ 1926 .flags = _flags | INFO_FL_FUNC, \
1844 .name = #_ioctl, \ 1927 .name = #_ioctl, \
1845 { \ 1928 .u.func = _func, \
1846 .func = _func, \
1847 }, \
1848 .debug = _debug, \ 1929 .debug = _debug, \
1849 } 1930 }
1850 1931
@@ -1883,7 +1964,7 @@ static struct v4l2_ioctl_info v4l2_ioctls[] = {
1883 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)), 1964 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
1884 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0), 1965 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
1885 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO), 1966 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
1886 IOCTL_INFO_STD(VIDIOC_G_MODULATOR, vidioc_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)), 1967 IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
1887 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO), 1968 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
1888 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)), 1969 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
1889 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO), 1970 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
@@ -1904,7 +1985,7 @@ static struct v4l2_ioctl_info v4l2_ioctls[] = {
1904 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0), 1985 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
1905 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL), 1986 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
1906 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL), 1987 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
1907 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, 0), 1988 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
1908 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)), 1989 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
1909 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)), 1990 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
1910 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0), 1991 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
@@ -1929,7 +2010,8 @@ static struct v4l2_ioctl_info v4l2_ioctls[] = {
1929 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE), 2010 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
1930 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0), 2011 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
1931 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0), 2012 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
1932 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, 0), 2013 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
2014 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
1933}; 2015};
1934#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls) 2016#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
1935 2017
@@ -2042,11 +2124,11 @@ static long __video_do_ioctl(struct file *file,
2042 if (info->flags & INFO_FL_STD) { 2124 if (info->flags & INFO_FL_STD) {
2043 typedef int (*vidioc_op)(struct file *file, void *fh, void *p); 2125 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2044 const void *p = vfd->ioctl_ops; 2126 const void *p = vfd->ioctl_ops;
2045 const vidioc_op *vidioc = p + info->offset; 2127 const vidioc_op *vidioc = p + info->u.offset;
2046 2128
2047 ret = (*vidioc)(file, fh, arg); 2129 ret = (*vidioc)(file, fh, arg);
2048 } else if (info->flags & INFO_FL_FUNC) { 2130 } else if (info->flags & INFO_FL_FUNC) {
2049 ret = info->func(ops, file, fh, arg); 2131 ret = info->u.func(ops, file, fh, arg);
2050 } else if (!ops->vidioc_default) { 2132 } else if (!ops->vidioc_default) {
2051 ret = -ENOTTY; 2133 ret = -ENOTTY;
2052 } else { 2134 } else {
diff --git a/drivers/media/video/v4l2-mem2mem.c b/drivers/media/video/v4l2-mem2mem.c
index 975d0fa938c6..97b48318aee1 100644
--- a/drivers/media/video/v4l2-mem2mem.c
+++ b/drivers/media/video/v4l2-mem2mem.c
@@ -19,6 +19,9 @@
19 19
20#include <media/videobuf2-core.h> 20#include <media/videobuf2-core.h>
21#include <media/v4l2-mem2mem.h> 21#include <media/v4l2-mem2mem.h>
22#include <media/v4l2-dev.h>
23#include <media/v4l2-fh.h>
24#include <media/v4l2-event.h>
22 25
23MODULE_DESCRIPTION("Mem to mem device framework for videobuf"); 26MODULE_DESCRIPTION("Mem to mem device framework for videobuf");
24MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); 27MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
@@ -407,11 +410,24 @@ EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff);
407unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 410unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
408 struct poll_table_struct *wait) 411 struct poll_table_struct *wait)
409{ 412{
413 struct video_device *vfd = video_devdata(file);
414 unsigned long req_events = poll_requested_events(wait);
410 struct vb2_queue *src_q, *dst_q; 415 struct vb2_queue *src_q, *dst_q;
411 struct vb2_buffer *src_vb = NULL, *dst_vb = NULL; 416 struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
412 unsigned int rc = 0; 417 unsigned int rc = 0;
413 unsigned long flags; 418 unsigned long flags;
414 419
420 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
421 struct v4l2_fh *fh = file->private_data;
422
423 if (v4l2_event_pending(fh))
424 rc = POLLPRI;
425 else if (req_events & POLLPRI)
426 poll_wait(file, &fh->wait, wait);
427 if (!(req_events & (POLLOUT | POLLWRNORM | POLLIN | POLLRDNORM)))
428 return rc;
429 }
430
415 src_q = v4l2_m2m_get_src_vq(m2m_ctx); 431 src_q = v4l2_m2m_get_src_vq(m2m_ctx);
416 dst_q = v4l2_m2m_get_dst_vq(m2m_ctx); 432 dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
417 433
@@ -422,7 +438,7 @@ unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
422 */ 438 */
423 if ((!src_q->streaming || list_empty(&src_q->queued_list)) 439 if ((!src_q->streaming || list_empty(&src_q->queued_list))
424 && (!dst_q->streaming || list_empty(&dst_q->queued_list))) { 440 && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
425 rc = POLLERR; 441 rc |= POLLERR;
426 goto end; 442 goto end;
427 } 443 }
428 444
diff --git a/drivers/media/video/via-camera.c b/drivers/media/video/via-camera.c
index 308e150a39bc..eb404c2ce270 100644
--- a/drivers/media/video/via-camera.c
+++ b/drivers/media/video/via-camera.c
@@ -963,7 +963,7 @@ static int viacam_do_try_fmt(struct via_camera *cam,
963 963
964 upix->pixelformat = f->pixelformat; 964 upix->pixelformat = f->pixelformat;
965 viacam_fmt_pre(upix, spix); 965 viacam_fmt_pre(upix, spix);
966 v4l2_fill_mbus_format(&mbus_fmt, upix, f->mbus_code); 966 v4l2_fill_mbus_format(&mbus_fmt, spix, f->mbus_code);
967 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt); 967 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
968 v4l2_fill_pix_format(spix, &mbus_fmt); 968 v4l2_fill_pix_format(spix, &mbus_fmt);
969 viacam_fmt_post(upix, spix); 969 viacam_fmt_post(upix, spix);
diff --git a/drivers/media/video/videobuf-dma-contig.c b/drivers/media/video/videobuf-dma-contig.c
index 9b9a06fdd0f0..3a43ba0959bf 100644
--- a/drivers/media/video/videobuf-dma-contig.c
+++ b/drivers/media/video/videobuf-dma-contig.c
@@ -56,7 +56,7 @@ static int __videobuf_dc_alloc(struct device *dev,
56 dev_err(dev, "dma_map_single failed\n"); 56 dev_err(dev, "dma_map_single failed\n");
57 57
58 free_pages_exact(mem->vaddr, mem->size); 58 free_pages_exact(mem->vaddr, mem->size);
59 mem->vaddr = 0; 59 mem->vaddr = NULL;
60 return err; 60 return err;
61 } 61 }
62 } 62 }
@@ -359,32 +359,43 @@ static int __videobuf_mmap_mapper(struct videobuf_queue *q,
359 size = vma->vm_end - vma->vm_start; 359 size = vma->vm_end - vma->vm_start;
360 size = (size < mem->size) ? size : mem->size; 360 size = (size < mem->size) ? size : mem->size;
361 361
362 if (!mem->cached) 362 if (!mem->cached) {
363 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 363 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
364 364 retval = remap_pfn_range(vma, vma->vm_start,
365 pos = (unsigned long)mem->vaddr; 365 mem->dma_handle >> PAGE_SHIFT,
366 366 size, vma->vm_page_prot);
367 while (size > 0) {
368 page = virt_to_page((void *)pos);
369 if (NULL == page) {
370 dev_err(q->dev, "mmap: virt_to_page failed\n");
371 __videobuf_dc_free(q->dev, mem);
372 goto error;
373 }
374 retval = vm_insert_page(vma, start, page);
375 if (retval) { 367 if (retval) {
376 dev_err(q->dev, "mmap: insert failed with error %d\n", 368 dev_err(q->dev, "mmap: remap failed with error %d. ",
377 retval); 369 retval);
378 __videobuf_dc_free(q->dev, mem); 370 dma_free_coherent(q->dev, mem->size,
371 mem->vaddr, mem->dma_handle);
379 goto error; 372 goto error;
380 } 373 }
381 start += PAGE_SIZE; 374 } else {
382 pos += PAGE_SIZE; 375 pos = (unsigned long)mem->vaddr;
383 376
384 if (size > PAGE_SIZE) 377 while (size > 0) {
385 size -= PAGE_SIZE; 378 page = virt_to_page((void *)pos);
386 else 379 if (NULL == page) {
387 size = 0; 380 dev_err(q->dev, "mmap: virt_to_page failed\n");
381 __videobuf_dc_free(q->dev, mem);
382 goto error;
383 }
384 retval = vm_insert_page(vma, start, page);
385 if (retval) {
386 dev_err(q->dev, "mmap: insert failed with error %d\n",
387 retval);
388 __videobuf_dc_free(q->dev, mem);
389 goto error;
390 }
391 start += PAGE_SIZE;
392 pos += PAGE_SIZE;
393
394 if (size > PAGE_SIZE)
395 size -= PAGE_SIZE;
396 else
397 size = 0;
398 }
388 } 399 }
389 400
390 vma->vm_ops = &videobuf_vm_ops; 401 vma->vm_ops = &videobuf_vm_ops;
diff --git a/drivers/media/video/videobuf2-core.c b/drivers/media/video/videobuf2-core.c
index 4e0290ab5071..268c7dd4f823 100644
--- a/drivers/media/video/videobuf2-core.c
+++ b/drivers/media/video/videobuf2-core.c
@@ -715,8 +715,8 @@ static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create
715} 715}
716 716
717/** 717/**
718 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and 718 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
719 * type values. 719 * memory and type values.
720 * @q: videobuf2 queue 720 * @q: videobuf2 queue
721 * @create: creation parameters, passed from userspace to vidioc_create_bufs 721 * @create: creation parameters, passed from userspace to vidioc_create_bufs
722 * handler in driver 722 * handler in driver
diff --git a/drivers/media/video/vivi.c b/drivers/media/video/vivi.c
index 1e8c4f3ab602..a05494b71b20 100644
--- a/drivers/media/video/vivi.c
+++ b/drivers/media/video/vivi.c
@@ -230,7 +230,6 @@ struct vivi_dev {
230 struct vivi_fmt *fmt; 230 struct vivi_fmt *fmt;
231 unsigned int width, height; 231 unsigned int width, height;
232 struct vb2_queue vb_vidq; 232 struct vb2_queue vb_vidq;
233 enum v4l2_field field;
234 unsigned int field_count; 233 unsigned int field_count;
235 234
236 u8 bars[9][3]; 235 u8 bars[9][3];
@@ -623,7 +622,7 @@ static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
623 622
624 dev->mv_count += 2; 623 dev->mv_count += 2;
625 624
626 buf->vb.v4l2_buf.field = dev->field; 625 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
627 dev->field_count++; 626 dev->field_count++;
628 buf->vb.v4l2_buf.sequence = dev->field_count >> 1; 627 buf->vb.v4l2_buf.sequence = dev->field_count >> 1;
629 do_gettimeofday(&ts); 628 do_gettimeofday(&ts);
@@ -925,7 +924,7 @@ static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
925 924
926 f->fmt.pix.width = dev->width; 925 f->fmt.pix.width = dev->width;
927 f->fmt.pix.height = dev->height; 926 f->fmt.pix.height = dev->height;
928 f->fmt.pix.field = dev->field; 927 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
929 f->fmt.pix.pixelformat = dev->fmt->fourcc; 928 f->fmt.pix.pixelformat = dev->fmt->fourcc;
930 f->fmt.pix.bytesperline = 929 f->fmt.pix.bytesperline =
931 (f->fmt.pix.width * dev->fmt->depth) >> 3; 930 (f->fmt.pix.width * dev->fmt->depth) >> 3;
@@ -944,25 +943,16 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
944{ 943{
945 struct vivi_dev *dev = video_drvdata(file); 944 struct vivi_dev *dev = video_drvdata(file);
946 struct vivi_fmt *fmt; 945 struct vivi_fmt *fmt;
947 enum v4l2_field field;
948 946
949 fmt = get_format(f); 947 fmt = get_format(f);
950 if (!fmt) { 948 if (!fmt) {
951 dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n", 949 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
952 f->fmt.pix.pixelformat); 950 f->fmt.pix.pixelformat);
953 return -EINVAL; 951 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
954 } 952 fmt = get_format(f);
955
956 field = f->fmt.pix.field;
957
958 if (field == V4L2_FIELD_ANY) {
959 field = V4L2_FIELD_INTERLACED;
960 } else if (V4L2_FIELD_INTERLACED != field) {
961 dprintk(dev, 1, "Field type invalid.\n");
962 return -EINVAL;
963 } 953 }
964 954
965 f->fmt.pix.field = field; 955 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
966 v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2, 956 v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2,
967 &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0); 957 &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0);
968 f->fmt.pix.bytesperline = 958 f->fmt.pix.bytesperline =
@@ -996,7 +986,6 @@ static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
996 dev->pixelsize = dev->fmt->depth / 8; 986 dev->pixelsize = dev->fmt->depth / 8;
997 dev->width = f->fmt.pix.width; 987 dev->width = f->fmt.pix.width;
998 dev->height = f->fmt.pix.height; 988 dev->height = f->fmt.pix.height;
999 dev->field = f->fmt.pix.field;
1000 989
1001 return 0; 990 return 0;
1002} 991}
@@ -1330,9 +1319,6 @@ static int __init vivi_create_instance(int inst)
1330 /* Now that everything is fine, let's add it to device list */ 1319 /* Now that everything is fine, let's add it to device list */
1331 list_add_tail(&dev->vivi_devlist, &vivi_devlist); 1320 list_add_tail(&dev->vivi_devlist, &vivi_devlist);
1332 1321
1333 if (video_nr != -1)
1334 video_nr++;
1335
1336 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n", 1322 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1337 video_device_node_name(vfd)); 1323 video_device_node_name(vfd));
1338 return 0; 1324 return 0;
diff --git a/drivers/mfd/ab3100-core.c b/drivers/mfd/ab3100-core.c
index 4276aab4f196..78fca2902c8d 100644
--- a/drivers/mfd/ab3100-core.c
+++ b/drivers/mfd/ab3100-core.c
@@ -409,8 +409,6 @@ static irqreturn_t ab3100_irq_handler(int irq, void *data)
409 u32 fatevent; 409 u32 fatevent;
410 int err; 410 int err;
411 411
412 add_interrupt_randomness(irq);
413
414 err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1, 412 err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
415 event_regs, 3); 413 event_regs, 3);
416 if (err) 414 if (err)
@@ -936,8 +934,6 @@ static int __devinit ab3100_probe(struct i2c_client *client,
936 IRQF_ONESHOT, "ab3100-core", ab3100); 934 IRQF_ONESHOT, "ab3100-core", ab3100);
937 if (err) 935 if (err)
938 goto exit_no_irq; 936 goto exit_no_irq;
939 /* This real unpredictable IRQ is of course sampled for entropy */
940 rand_initialize_irq(client->irq);
941 937
942 err = abx500_register_ops(&client->dev, &ab3100_ops); 938 err = abx500_register_ops(&client->dev, &ab3100_ops);
943 if (err) 939 if (err)
diff --git a/drivers/mfd/tps65010.c b/drivers/mfd/tps65010.c
index 93d5fdf020c7..da2691f22e11 100644
--- a/drivers/mfd/tps65010.c
+++ b/drivers/mfd/tps65010.c
@@ -563,8 +563,7 @@ static int tps65010_probe(struct i2c_client *client,
563 */ 563 */
564 if (client->irq > 0) { 564 if (client->irq > 0) {
565 status = request_irq(client->irq, tps65010_irq, 565 status = request_irq(client->irq, tps65010_irq,
566 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING, 566 IRQF_TRIGGER_FALLING, DRIVER_NAME, tps);
567 DRIVER_NAME, tps);
568 if (status < 0) { 567 if (status < 0) {
569 dev_dbg(&client->dev, "can't get IRQ %d, err %d\n", 568 dev_dbg(&client->dev, "can't get IRQ %d, err %d\n",
570 client->irq, status); 569 client->irq, status);
diff --git a/drivers/mfd/twl-core.c b/drivers/mfd/twl-core.c
index b012efd29e01..1c32afed28aa 100644
--- a/drivers/mfd/twl-core.c
+++ b/drivers/mfd/twl-core.c
@@ -717,8 +717,9 @@ add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
717 static struct regulator_consumer_supply usb1v8 = { 717 static struct regulator_consumer_supply usb1v8 = {
718 .supply = "usb1v8", 718 .supply = "usb1v8",
719 }; 719 };
720 static struct regulator_consumer_supply usb3v1 = { 720 static struct regulator_consumer_supply usb3v1[] = {
721 .supply = "usb3v1", 721 { .supply = "usb3v1" },
722 { .supply = "bci3v1" },
722 }; 723 };
723 724
724 /* First add the regulators so that they can be used by transceiver */ 725 /* First add the regulators so that they can be used by transceiver */
@@ -746,7 +747,7 @@ add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
746 return PTR_ERR(child); 747 return PTR_ERR(child);
747 748
748 child = add_regulator_linked(TWL4030_REG_VUSB3V1, 749 child = add_regulator_linked(TWL4030_REG_VUSB3V1,
749 &usb_fixed, &usb3v1, 1, 750 &usb_fixed, usb3v1, 2,
750 features); 751 features);
751 if (IS_ERR(child)) 752 if (IS_ERR(child))
752 return PTR_ERR(child); 753 return PTR_ERR(child);
@@ -767,7 +768,7 @@ add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
767 if (twl_has_regulator() && child) { 768 if (twl_has_regulator() && child) {
768 usb1v5.dev_name = dev_name(child); 769 usb1v5.dev_name = dev_name(child);
769 usb1v8.dev_name = dev_name(child); 770 usb1v8.dev_name = dev_name(child);
770 usb3v1.dev_name = dev_name(child); 771 usb3v1[0].dev_name = dev_name(child);
771 } 772 }
772 } 773 }
773 if (twl_has_usb() && pdata->usb && twl_class_is_6030()) { 774 if (twl_has_usb() && pdata->usb && twl_class_is_6030()) {
diff --git a/drivers/mfd/wm831x-otp.c b/drivers/mfd/wm831x-otp.c
index f742745ff354..b90f3e06b6c9 100644
--- a/drivers/mfd/wm831x-otp.c
+++ b/drivers/mfd/wm831x-otp.c
@@ -18,6 +18,7 @@
18#include <linux/bcd.h> 18#include <linux/bcd.h>
19#include <linux/delay.h> 19#include <linux/delay.h>
20#include <linux/mfd/core.h> 20#include <linux/mfd/core.h>
21#include <linux/random.h>
21 22
22#include <linux/mfd/wm831x/core.h> 23#include <linux/mfd/wm831x/core.h>
23#include <linux/mfd/wm831x/otp.h> 24#include <linux/mfd/wm831x/otp.h>
@@ -66,6 +67,7 @@ static DEVICE_ATTR(unique_id, 0444, wm831x_unique_id_show, NULL);
66 67
67int wm831x_otp_init(struct wm831x *wm831x) 68int wm831x_otp_init(struct wm831x *wm831x)
68{ 69{
70 char uuid[WM831X_UNIQUE_ID_LEN];
69 int ret; 71 int ret;
70 72
71 ret = device_create_file(wm831x->dev, &dev_attr_unique_id); 73 ret = device_create_file(wm831x->dev, &dev_attr_unique_id);
@@ -73,6 +75,12 @@ int wm831x_otp_init(struct wm831x *wm831x)
73 dev_err(wm831x->dev, "Unique ID attribute not created: %d\n", 75 dev_err(wm831x->dev, "Unique ID attribute not created: %d\n",
74 ret); 76 ret);
75 77
78 ret = wm831x_unique_id_read(wm831x, uuid);
79 if (ret == 0)
80 add_device_randomness(uuid, sizeof(uuid));
81 else
82 dev_err(wm831x->dev, "Failed to read UUID: %d\n", ret);
83
76 return ret; 84 return ret;
77} 85}
78 86
diff --git a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c
index 9aaf863b4237..dd451c3dd83d 100644
--- a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c
+++ b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c
@@ -9360,8 +9360,7 @@ static int __devinit bnx2x_prev_mark_path(struct bnx2x *bp)
9360 struct bnx2x_prev_path_list *tmp_list; 9360 struct bnx2x_prev_path_list *tmp_list;
9361 int rc; 9361 int rc;
9362 9362
9363 tmp_list = (struct bnx2x_prev_path_list *) 9363 tmp_list = kmalloc(sizeof(struct bnx2x_prev_path_list), GFP_KERNEL);
9364 kmalloc(sizeof(struct bnx2x_prev_path_list), GFP_KERNEL);
9365 if (!tmp_list) { 9364 if (!tmp_list) {
9366 BNX2X_ERR("Failed to allocate 'bnx2x_prev_path_list'\n"); 9365 BNX2X_ERR("Failed to allocate 'bnx2x_prev_path_list'\n");
9367 return -ENOMEM; 9366 return -ENOMEM;
diff --git a/drivers/net/ethernet/broadcom/tg3.c b/drivers/net/ethernet/broadcom/tg3.c
index 9a009fd6ea1b..bf906c51d82a 100644
--- a/drivers/net/ethernet/broadcom/tg3.c
+++ b/drivers/net/ethernet/broadcom/tg3.c
@@ -92,7 +92,7 @@ static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
92 92
93#define DRV_MODULE_NAME "tg3" 93#define DRV_MODULE_NAME "tg3"
94#define TG3_MAJ_NUM 3 94#define TG3_MAJ_NUM 3
95#define TG3_MIN_NUM 123 95#define TG3_MIN_NUM 124
96#define DRV_MODULE_VERSION \ 96#define DRV_MODULE_VERSION \
97 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM) 97 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
98#define DRV_MODULE_RELDATE "March 21, 2012" 98#define DRV_MODULE_RELDATE "March 21, 2012"
@@ -672,6 +672,12 @@ static int tg3_ape_lock(struct tg3 *tp, int locknum)
672 else 672 else
673 bit = 1 << tp->pci_fn; 673 bit = 1 << tp->pci_fn;
674 break; 674 break;
675 case TG3_APE_LOCK_PHY0:
676 case TG3_APE_LOCK_PHY1:
677 case TG3_APE_LOCK_PHY2:
678 case TG3_APE_LOCK_PHY3:
679 bit = APE_LOCK_REQ_DRIVER;
680 break;
675 default: 681 default:
676 return -EINVAL; 682 return -EINVAL;
677 } 683 }
@@ -723,6 +729,12 @@ static void tg3_ape_unlock(struct tg3 *tp, int locknum)
723 else 729 else
724 bit = 1 << tp->pci_fn; 730 bit = 1 << tp->pci_fn;
725 break; 731 break;
732 case TG3_APE_LOCK_PHY0:
733 case TG3_APE_LOCK_PHY1:
734 case TG3_APE_LOCK_PHY2:
735 case TG3_APE_LOCK_PHY3:
736 bit = APE_LOCK_GRANT_DRIVER;
737 break;
726 default: 738 default:
727 return; 739 return;
728 } 740 }
@@ -1052,6 +1064,8 @@ static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1052 udelay(80); 1064 udelay(80);
1053 } 1065 }
1054 1066
1067 tg3_ape_lock(tp, tp->phy_ape_lock);
1068
1055 *val = 0x0; 1069 *val = 0x0;
1056 1070
1057 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) & 1071 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
@@ -1086,6 +1100,8 @@ static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1086 udelay(80); 1100 udelay(80);
1087 } 1101 }
1088 1102
1103 tg3_ape_unlock(tp, tp->phy_ape_lock);
1104
1089 return ret; 1105 return ret;
1090} 1106}
1091 1107
@@ -1105,6 +1121,8 @@ static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1105 udelay(80); 1121 udelay(80);
1106 } 1122 }
1107 1123
1124 tg3_ape_lock(tp, tp->phy_ape_lock);
1125
1108 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) & 1126 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
1109 MI_COM_PHY_ADDR_MASK); 1127 MI_COM_PHY_ADDR_MASK);
1110 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) & 1128 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
@@ -1135,6 +1153,8 @@ static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1135 udelay(80); 1153 udelay(80);
1136 } 1154 }
1137 1155
1156 tg3_ape_unlock(tp, tp->phy_ape_lock);
1157
1138 return ret; 1158 return ret;
1139} 1159}
1140 1160
@@ -9066,8 +9086,7 @@ static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
9066 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 || 9086 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
9067 tg3_flag(tp, 57765_PLUS)) { 9087 tg3_flag(tp, 57765_PLUS)) {
9068 val = tr32(TG3_RDMA_RSRVCTRL_REG); 9088 val = tr32(TG3_RDMA_RSRVCTRL_REG);
9069 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 || 9089 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
9070 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9071 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK | 9090 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9072 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK | 9091 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9073 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK); 9092 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
@@ -9257,6 +9276,19 @@ static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
9257 tw32_f(RDMAC_MODE, rdmac_mode); 9276 tw32_f(RDMAC_MODE, rdmac_mode);
9258 udelay(40); 9277 udelay(40);
9259 9278
9279 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
9280 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
9281 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
9282 break;
9283 }
9284 if (i < TG3_NUM_RDMA_CHANNELS) {
9285 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9286 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
9287 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9288 tg3_flag_set(tp, 5719_RDMA_BUG);
9289 }
9290 }
9291
9260 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE); 9292 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
9261 if (!tg3_flag(tp, 5705_PLUS)) 9293 if (!tg3_flag(tp, 5705_PLUS))
9262 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE); 9294 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
@@ -9616,6 +9648,16 @@ static void tg3_periodic_fetch_stats(struct tg3 *tp)
9616 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST); 9648 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9617 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST); 9649 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9618 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST); 9650 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9651 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
9652 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
9653 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
9654 u32 val;
9655
9656 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9657 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
9658 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9659 tg3_flag_clear(tp, 5719_RDMA_BUG);
9660 }
9619 9661
9620 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS); 9662 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9621 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS); 9663 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
@@ -12482,10 +12524,12 @@ static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
12482{ 12524{
12483 struct tg3 *tp = netdev_priv(dev); 12525 struct tg3 *tp = netdev_priv(dev);
12484 12526
12485 if (!tp->hw_stats) 12527 spin_lock_bh(&tp->lock);
12528 if (!tp->hw_stats) {
12529 spin_unlock_bh(&tp->lock);
12486 return &tp->net_stats_prev; 12530 return &tp->net_stats_prev;
12531 }
12487 12532
12488 spin_lock_bh(&tp->lock);
12489 tg3_get_nstats(tp, stats); 12533 tg3_get_nstats(tp, stats);
12490 spin_unlock_bh(&tp->lock); 12534 spin_unlock_bh(&tp->lock);
12491 12535
@@ -13648,6 +13692,23 @@ static int __devinit tg3_phy_probe(struct tg3 *tp)
13648 tg3_flag_set(tp, PAUSE_AUTONEG); 13692 tg3_flag_set(tp, PAUSE_AUTONEG);
13649 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX; 13693 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13650 13694
13695 if (tg3_flag(tp, ENABLE_APE)) {
13696 switch (tp->pci_fn) {
13697 case 0:
13698 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
13699 break;
13700 case 1:
13701 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
13702 break;
13703 case 2:
13704 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
13705 break;
13706 case 3:
13707 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
13708 break;
13709 }
13710 }
13711
13651 if (tg3_flag(tp, USE_PHYLIB)) 13712 if (tg3_flag(tp, USE_PHYLIB))
13652 return tg3_phy_init(tp); 13713 return tg3_phy_init(tp);
13653 13714
diff --git a/drivers/net/ethernet/broadcom/tg3.h b/drivers/net/ethernet/broadcom/tg3.h
index a1b75cd67b9d..6d52cb286826 100644
--- a/drivers/net/ethernet/broadcom/tg3.h
+++ b/drivers/net/ethernet/broadcom/tg3.h
@@ -1376,7 +1376,11 @@
1376#define TG3_LSO_RD_DMA_CRPTEN_CTRL 0x00004910 1376#define TG3_LSO_RD_DMA_CRPTEN_CTRL 0x00004910
1377#define TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K 0x00030000 1377#define TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K 0x00030000
1378#define TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K 0x000c0000 1378#define TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K 0x000c0000
1379/* 0x4914 --> 0x4c00 unused */ 1379#define TG3_LSO_RD_DMA_TX_LENGTH_WA 0x02000000
1380/* 0x4914 --> 0x4be0 unused */
1381
1382#define TG3_NUM_RDMA_CHANNELS 4
1383#define TG3_RDMA_LENGTH 0x00004be0
1380 1384
1381/* Write DMA control registers */ 1385/* Write DMA control registers */
1382#define WDMAC_MODE 0x00004c00 1386#define WDMAC_MODE 0x00004c00
@@ -2959,6 +2963,7 @@ enum TG3_FLAGS {
2959 TG3_FLAG_L1PLLPD_EN, 2963 TG3_FLAG_L1PLLPD_EN,
2960 TG3_FLAG_APE_HAS_NCSI, 2964 TG3_FLAG_APE_HAS_NCSI,
2961 TG3_FLAG_4K_FIFO_LIMIT, 2965 TG3_FLAG_4K_FIFO_LIMIT,
2966 TG3_FLAG_5719_RDMA_BUG,
2962 TG3_FLAG_RESET_TASK_PENDING, 2967 TG3_FLAG_RESET_TASK_PENDING,
2963 TG3_FLAG_5705_PLUS, 2968 TG3_FLAG_5705_PLUS,
2964 TG3_FLAG_IS_5788, 2969 TG3_FLAG_IS_5788,
@@ -3107,6 +3112,7 @@ struct tg3 {
3107 int old_link; 3112 int old_link;
3108 3113
3109 u8 phy_addr; 3114 u8 phy_addr;
3115 u8 phy_ape_lock;
3110 3116
3111 /* PHY info */ 3117 /* PHY info */
3112 u32 phy_id; 3118 u32 phy_id;
diff --git a/drivers/net/ethernet/qlogic/qlge/qlge_main.c b/drivers/net/ethernet/qlogic/qlge/qlge_main.c
index 3769f5711cc3..b53a3b60b648 100644
--- a/drivers/net/ethernet/qlogic/qlge/qlge_main.c
+++ b/drivers/net/ethernet/qlogic/qlge/qlge_main.c
@@ -4682,6 +4682,7 @@ static int __devinit qlge_probe(struct pci_dev *pdev,
4682 NETIF_F_HW_VLAN_TX | NETIF_F_RXCSUM; 4682 NETIF_F_HW_VLAN_TX | NETIF_F_RXCSUM;
4683 ndev->features = ndev->hw_features | 4683 ndev->features = ndev->hw_features |
4684 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER; 4684 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
4685 ndev->vlan_features = ndev->hw_features;
4685 4686
4686 if (test_bit(QL_DMA64, &qdev->flags)) 4687 if (test_bit(QL_DMA64, &qdev->flags))
4687 ndev->features |= NETIF_F_HIGHDMA; 4688 ndev->features |= NETIF_F_HIGHDMA;
diff --git a/drivers/net/ethernet/seeq/seeq8005.c b/drivers/net/ethernet/seeq/seeq8005.c
index 698edbbfc149..d6e50de71186 100644
--- a/drivers/net/ethernet/seeq/seeq8005.c
+++ b/drivers/net/ethernet/seeq/seeq8005.c
@@ -736,9 +736,7 @@ MODULE_PARM_DESC(irq, "SEEQ 8005 IRQ number");
736int __init init_module(void) 736int __init init_module(void)
737{ 737{
738 dev_seeq = seeq8005_probe(-1); 738 dev_seeq = seeq8005_probe(-1);
739 if (IS_ERR(dev_seeq)) 739 return PTR_RET(dev_seeq);
740 return PTR_ERR(dev_seeq);
741 return 0;
742} 740}
743 741
744void __exit cleanup_module(void) 742void __exit cleanup_module(void)
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index c62163e272cd..926d4db5cb38 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1379,10 +1379,12 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1379 int vnet_hdr_sz; 1379 int vnet_hdr_sz;
1380 int ret; 1380 int ret;
1381 1381
1382 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) 1382 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
1383 if (copy_from_user(&ifr, argp, ifreq_len)) 1383 if (copy_from_user(&ifr, argp, ifreq_len))
1384 return -EFAULT; 1384 return -EFAULT;
1385 1385 } else {
1386 memset(&ifr, 0, sizeof(ifr));
1387 }
1386 if (cmd == TUNGETFEATURES) { 1388 if (cmd == TUNGETFEATURES) {
1387 /* Currently this just means: "what IFF flags are valid?". 1389 /* Currently this just means: "what IFF flags are valid?".
1388 * This is needed because we never checked for invalid flags on 1390 * This is needed because we never checked for invalid flags on
diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index aa764ecc4e60..c1892f321c46 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -268,6 +268,7 @@ config CHARGER_GPIO
268config CHARGER_MANAGER 268config CHARGER_MANAGER
269 bool "Battery charger manager for multiple chargers" 269 bool "Battery charger manager for multiple chargers"
270 depends on REGULATOR && RTC_CLASS 270 depends on REGULATOR && RTC_CLASS
271 select EXTCON
271 help 272 help
272 Say Y to enable charger-manager support, which allows multiple 273 Say Y to enable charger-manager support, which allows multiple
273 chargers attached to a battery and multiple batteries attached to a 274 chargers attached to a battery and multiple batteries attached to a
diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c
index f5d6d379f2fb..181ddece5181 100644
--- a/drivers/power/bq27x00_battery.c
+++ b/drivers/power/bq27x00_battery.c
@@ -22,6 +22,7 @@
22 * Datasheets: 22 * Datasheets:
23 * http://focus.ti.com/docs/prod/folders/print/bq27000.html 23 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
24 * http://focus.ti.com/docs/prod/folders/print/bq27500.html 24 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
25 * http://www.ti.com/product/bq27425-g1
25 */ 26 */
26 27
27#include <linux/module.h> 28#include <linux/module.h>
@@ -51,6 +52,7 @@
51#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */ 52#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
52#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */ 53#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
53#define BQ27x00_REG_AE 0x22 /* Available energy */ 54#define BQ27x00_REG_AE 0x22 /* Available energy */
55#define BQ27x00_POWER_AVG 0x24
54 56
55#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ 57#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
56#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */ 58#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
@@ -66,15 +68,21 @@
66#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */ 68#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
67#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */ 69#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
68#define BQ27500_FLAG_FC BIT(9) 70#define BQ27500_FLAG_FC BIT(9)
71#define BQ27500_FLAG_OTC BIT(15)
72
73/* bq27425 register addresses are same as bq27x00 addresses minus 4 */
74#define BQ27425_REG_OFFSET 0x04
75#define BQ27425_REG_SOC 0x18 /* Register address plus offset */
69 76
70#define BQ27000_RS 20 /* Resistor sense */ 77#define BQ27000_RS 20 /* Resistor sense */
78#define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
71 79
72struct bq27x00_device_info; 80struct bq27x00_device_info;
73struct bq27x00_access_methods { 81struct bq27x00_access_methods {
74 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single); 82 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
75}; 83};
76 84
77enum bq27x00_chip { BQ27000, BQ27500 }; 85enum bq27x00_chip { BQ27000, BQ27500, BQ27425};
78 86
79struct bq27x00_reg_cache { 87struct bq27x00_reg_cache {
80 int temperature; 88 int temperature;
@@ -86,6 +94,8 @@ struct bq27x00_reg_cache {
86 int capacity; 94 int capacity;
87 int energy; 95 int energy;
88 int flags; 96 int flags;
97 int power_avg;
98 int health;
89}; 99};
90 100
91struct bq27x00_device_info { 101struct bq27x00_device_info {
@@ -123,6 +133,22 @@ static enum power_supply_property bq27x00_battery_props[] = {
123 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 133 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
124 POWER_SUPPLY_PROP_CYCLE_COUNT, 134 POWER_SUPPLY_PROP_CYCLE_COUNT,
125 POWER_SUPPLY_PROP_ENERGY_NOW, 135 POWER_SUPPLY_PROP_ENERGY_NOW,
136 POWER_SUPPLY_PROP_POWER_AVG,
137 POWER_SUPPLY_PROP_HEALTH,
138};
139
140static enum power_supply_property bq27425_battery_props[] = {
141 POWER_SUPPLY_PROP_STATUS,
142 POWER_SUPPLY_PROP_PRESENT,
143 POWER_SUPPLY_PROP_VOLTAGE_NOW,
144 POWER_SUPPLY_PROP_CURRENT_NOW,
145 POWER_SUPPLY_PROP_CAPACITY,
146 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
147 POWER_SUPPLY_PROP_TEMP,
148 POWER_SUPPLY_PROP_TECHNOLOGY,
149 POWER_SUPPLY_PROP_CHARGE_FULL,
150 POWER_SUPPLY_PROP_CHARGE_NOW,
151 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
126}; 152};
127 153
128static unsigned int poll_interval = 360; 154static unsigned int poll_interval = 360;
@@ -137,10 +163,24 @@ MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
137static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg, 163static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
138 bool single) 164 bool single)
139{ 165{
166 if (di->chip == BQ27425)
167 return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
140 return di->bus.read(di, reg, single); 168 return di->bus.read(di, reg, single);
141} 169}
142 170
143/* 171/*
172 * Higher versions of the chip like BQ27425 and BQ27500
173 * differ from BQ27000 and BQ27200 in calculation of certain
174 * parameters. Hence we need to check for the chip type.
175 */
176static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di)
177{
178 if (di->chip == BQ27425 || di->chip == BQ27500)
179 return true;
180 return false;
181}
182
183/*
144 * Return the battery Relative State-of-Charge 184 * Return the battery Relative State-of-Charge
145 * Or < 0 if something fails. 185 * Or < 0 if something fails.
146 */ 186 */
@@ -150,6 +190,8 @@ static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
150 190
151 if (di->chip == BQ27500) 191 if (di->chip == BQ27500)
152 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false); 192 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
193 else if (di->chip == BQ27425)
194 rsoc = bq27x00_read(di, BQ27425_REG_SOC, false);
153 else 195 else
154 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true); 196 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
155 197
@@ -174,7 +216,7 @@ static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
174 return charge; 216 return charge;
175 } 217 }
176 218
177 if (di->chip == BQ27500) 219 if (bq27xxx_is_chip_version_higher(di))
178 charge *= 1000; 220 charge *= 1000;
179 else 221 else
180 charge = charge * 3570 / BQ27000_RS; 222 charge = charge * 3570 / BQ27000_RS;
@@ -208,7 +250,7 @@ static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
208{ 250{
209 int ilmd; 251 int ilmd;
210 252
211 if (di->chip == BQ27500) 253 if (bq27xxx_is_chip_version_higher(di))
212 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false); 254 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
213 else 255 else
214 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true); 256 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
@@ -218,7 +260,7 @@ static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
218 return ilmd; 260 return ilmd;
219 } 261 }
220 262
221 if (di->chip == BQ27500) 263 if (bq27xxx_is_chip_version_higher(di))
222 ilmd *= 1000; 264 ilmd *= 1000;
223 else 265 else
224 ilmd = ilmd * 256 * 3570 / BQ27000_RS; 266 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
@@ -262,7 +304,7 @@ static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
262 return temp; 304 return temp;
263 } 305 }
264 306
265 if (di->chip == BQ27500) 307 if (bq27xxx_is_chip_version_higher(di))
266 temp -= 2731; 308 temp -= 2731;
267 else 309 else
268 temp = ((temp * 5) - 5463) / 2; 310 temp = ((temp * 5) - 5463) / 2;
@@ -306,14 +348,70 @@ static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
306 return tval * 60; 348 return tval * 60;
307} 349}
308 350
351/*
352 * Read a power avg register.
353 * Return < 0 if something fails.
354 */
355static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
356{
357 int tval;
358
359 tval = bq27x00_read(di, reg, false);
360 if (tval < 0) {
361 dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
362 reg, tval);
363 return tval;
364 }
365
366 if (di->chip == BQ27500)
367 return tval;
368 else
369 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
370}
371
372/*
373 * Read flag register.
374 * Return < 0 if something fails.
375 */
376static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
377{
378 int tval;
379
380 tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
381 if (tval < 0) {
382 dev_err(di->dev, "error reading flag register:%d\n", tval);
383 return tval;
384 }
385
386 if ((di->chip == BQ27500)) {
387 if (tval & BQ27500_FLAG_SOCF)
388 tval = POWER_SUPPLY_HEALTH_DEAD;
389 else if (tval & BQ27500_FLAG_OTC)
390 tval = POWER_SUPPLY_HEALTH_OVERHEAT;
391 else
392 tval = POWER_SUPPLY_HEALTH_GOOD;
393 return tval;
394 } else {
395 if (tval & BQ27000_FLAG_EDV1)
396 tval = POWER_SUPPLY_HEALTH_DEAD;
397 else
398 tval = POWER_SUPPLY_HEALTH_GOOD;
399 return tval;
400 }
401
402 return -1;
403}
404
309static void bq27x00_update(struct bq27x00_device_info *di) 405static void bq27x00_update(struct bq27x00_device_info *di)
310{ 406{
311 struct bq27x00_reg_cache cache = {0, }; 407 struct bq27x00_reg_cache cache = {0, };
312 bool is_bq27500 = di->chip == BQ27500; 408 bool is_bq27500 = di->chip == BQ27500;
409 bool is_bq27425 = di->chip == BQ27425;
313 410
314 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500); 411 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
315 if (cache.flags >= 0) { 412 if (cache.flags >= 0) {
316 if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) { 413 if (!is_bq27500 && !is_bq27425
414 && (cache.flags & BQ27000_FLAG_CI)) {
317 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n"); 415 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
318 cache.capacity = -ENODATA; 416 cache.capacity = -ENODATA;
319 cache.energy = -ENODATA; 417 cache.energy = -ENODATA;
@@ -321,16 +419,30 @@ static void bq27x00_update(struct bq27x00_device_info *di)
321 cache.time_to_empty_avg = -ENODATA; 419 cache.time_to_empty_avg = -ENODATA;
322 cache.time_to_full = -ENODATA; 420 cache.time_to_full = -ENODATA;
323 cache.charge_full = -ENODATA; 421 cache.charge_full = -ENODATA;
422 cache.health = -ENODATA;
324 } else { 423 } else {
325 cache.capacity = bq27x00_battery_read_rsoc(di); 424 cache.capacity = bq27x00_battery_read_rsoc(di);
326 cache.energy = bq27x00_battery_read_energy(di); 425 if (!is_bq27425) {
327 cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE); 426 cache.energy = bq27x00_battery_read_energy(di);
328 cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP); 427 cache.time_to_empty =
329 cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF); 428 bq27x00_battery_read_time(di,
429 BQ27x00_REG_TTE);
430 cache.time_to_empty_avg =
431 bq27x00_battery_read_time(di,
432 BQ27x00_REG_TTECP);
433 cache.time_to_full =
434 bq27x00_battery_read_time(di,
435 BQ27x00_REG_TTF);
436 }
330 cache.charge_full = bq27x00_battery_read_lmd(di); 437 cache.charge_full = bq27x00_battery_read_lmd(di);
438 cache.health = bq27x00_battery_read_health(di);
331 } 439 }
332 cache.temperature = bq27x00_battery_read_temperature(di); 440 cache.temperature = bq27x00_battery_read_temperature(di);
441 if (!is_bq27425)
442 cache.cycle_count = bq27x00_battery_read_cyct(di);
333 cache.cycle_count = bq27x00_battery_read_cyct(di); 443 cache.cycle_count = bq27x00_battery_read_cyct(di);
444 cache.power_avg =
445 bq27x00_battery_read_pwr_avg(di, BQ27x00_POWER_AVG);
334 446
335 /* We only have to read charge design full once */ 447 /* We only have to read charge design full once */
336 if (di->charge_design_full <= 0) 448 if (di->charge_design_full <= 0)
@@ -376,7 +488,7 @@ static int bq27x00_battery_current(struct bq27x00_device_info *di,
376 return curr; 488 return curr;
377 } 489 }
378 490
379 if (di->chip == BQ27500) { 491 if (bq27xxx_is_chip_version_higher(di)) {
380 /* bq27500 returns signed value */ 492 /* bq27500 returns signed value */
381 val->intval = (int)((s16)curr) * 1000; 493 val->intval = (int)((s16)curr) * 1000;
382 } else { 494 } else {
@@ -397,7 +509,7 @@ static int bq27x00_battery_status(struct bq27x00_device_info *di,
397{ 509{
398 int status; 510 int status;
399 511
400 if (di->chip == BQ27500) { 512 if (bq27xxx_is_chip_version_higher(di)) {
401 if (di->cache.flags & BQ27500_FLAG_FC) 513 if (di->cache.flags & BQ27500_FLAG_FC)
402 status = POWER_SUPPLY_STATUS_FULL; 514 status = POWER_SUPPLY_STATUS_FULL;
403 else if (di->cache.flags & BQ27500_FLAG_DSC) 515 else if (di->cache.flags & BQ27500_FLAG_DSC)
@@ -425,7 +537,7 @@ static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
425{ 537{
426 int level; 538 int level;
427 539
428 if (di->chip == BQ27500) { 540 if (bq27xxx_is_chip_version_higher(di)) {
429 if (di->cache.flags & BQ27500_FLAG_FC) 541 if (di->cache.flags & BQ27500_FLAG_FC)
430 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 542 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
431 else if (di->cache.flags & BQ27500_FLAG_SOC1) 543 else if (di->cache.flags & BQ27500_FLAG_SOC1)
@@ -550,6 +662,12 @@ static int bq27x00_battery_get_property(struct power_supply *psy,
550 case POWER_SUPPLY_PROP_ENERGY_NOW: 662 case POWER_SUPPLY_PROP_ENERGY_NOW:
551 ret = bq27x00_simple_value(di->cache.energy, val); 663 ret = bq27x00_simple_value(di->cache.energy, val);
552 break; 664 break;
665 case POWER_SUPPLY_PROP_POWER_AVG:
666 ret = bq27x00_simple_value(di->cache.power_avg, val);
667 break;
668 case POWER_SUPPLY_PROP_HEALTH:
669 ret = bq27x00_simple_value(di->cache.health, val);
670 break;
553 default: 671 default:
554 return -EINVAL; 672 return -EINVAL;
555 } 673 }
@@ -570,8 +688,14 @@ static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
570 int ret; 688 int ret;
571 689
572 di->bat.type = POWER_SUPPLY_TYPE_BATTERY; 690 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
573 di->bat.properties = bq27x00_battery_props; 691 di->chip = BQ27425;
574 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); 692 if (di->chip == BQ27425) {
693 di->bat.properties = bq27425_battery_props;
694 di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props);
695 } else {
696 di->bat.properties = bq27x00_battery_props;
697 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
698 }
575 di->bat.get_property = bq27x00_battery_get_property; 699 di->bat.get_property = bq27x00_battery_get_property;
576 di->bat.external_power_changed = bq27x00_external_power_changed; 700 di->bat.external_power_changed = bq27x00_external_power_changed;
577 701
@@ -729,6 +853,7 @@ static int bq27x00_battery_remove(struct i2c_client *client)
729static const struct i2c_device_id bq27x00_id[] = { 853static const struct i2c_device_id bq27x00_id[] = {
730 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ 854 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
731 { "bq27500", BQ27500 }, 855 { "bq27500", BQ27500 },
856 { "bq27425", BQ27425 },
732 {}, 857 {},
733}; 858};
734MODULE_DEVICE_TABLE(i2c, bq27x00_id); 859MODULE_DEVICE_TABLE(i2c, bq27x00_id);
diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c
index 86935ec18954..526e5c931294 100644
--- a/drivers/power/charger-manager.c
+++ b/drivers/power/charger-manager.c
@@ -271,16 +271,13 @@ static int try_charger_enable(struct charger_manager *cm, bool enable)
271 if (enable) { 271 if (enable) {
272 if (cm->emergency_stop) 272 if (cm->emergency_stop)
273 return -EAGAIN; 273 return -EAGAIN;
274 err = regulator_bulk_enable(desc->num_charger_regulators, 274 for (i = 0 ; i < desc->num_charger_regulators ; i++)
275 desc->charger_regulators); 275 regulator_enable(desc->charger_regulators[i].consumer);
276 } else { 276 } else {
277 /* 277 /*
278 * Abnormal battery state - Stop charging forcibly, 278 * Abnormal battery state - Stop charging forcibly,
279 * even if charger was enabled at the other places 279 * even if charger was enabled at the other places
280 */ 280 */
281 err = regulator_bulk_disable(desc->num_charger_regulators,
282 desc->charger_regulators);
283
284 for (i = 0; i < desc->num_charger_regulators; i++) { 281 for (i = 0; i < desc->num_charger_regulators; i++) {
285 if (regulator_is_enabled( 282 if (regulator_is_enabled(
286 desc->charger_regulators[i].consumer)) { 283 desc->charger_regulators[i].consumer)) {
@@ -288,7 +285,7 @@ static int try_charger_enable(struct charger_manager *cm, bool enable)
288 desc->charger_regulators[i].consumer); 285 desc->charger_regulators[i].consumer);
289 dev_warn(cm->dev, 286 dev_warn(cm->dev,
290 "Disable regulator(%s) forcibly.\n", 287 "Disable regulator(%s) forcibly.\n",
291 desc->charger_regulators[i].supply); 288 desc->charger_regulators[i].regulator_name);
292 } 289 }
293 } 290 }
294 } 291 }
@@ -994,11 +991,92 @@ int setup_charger_manager(struct charger_global_desc *gd)
994} 991}
995EXPORT_SYMBOL_GPL(setup_charger_manager); 992EXPORT_SYMBOL_GPL(setup_charger_manager);
996 993
994/**
995 * charger_extcon_work - enable/diable charger according to the state
996 * of charger cable
997 *
998 * @work: work_struct of the function charger_extcon_work.
999 */
1000static void charger_extcon_work(struct work_struct *work)
1001{
1002 struct charger_cable *cable =
1003 container_of(work, struct charger_cable, wq);
1004 int ret;
1005
1006 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1007 ret = regulator_set_current_limit(cable->charger->consumer,
1008 cable->min_uA, cable->max_uA);
1009 if (ret < 0) {
1010 pr_err("Cannot set current limit of %s (%s)\n",
1011 cable->charger->regulator_name, cable->name);
1012 return;
1013 }
1014
1015 pr_info("Set current limit of %s : %duA ~ %duA\n",
1016 cable->charger->regulator_name,
1017 cable->min_uA, cable->max_uA);
1018 }
1019
1020 try_charger_enable(cable->cm, cable->attached);
1021}
1022
1023/**
1024 * charger_extcon_notifier - receive the state of charger cable
1025 * when registered cable is attached or detached.
1026 *
1027 * @self: the notifier block of the charger_extcon_notifier.
1028 * @event: the cable state.
1029 * @ptr: the data pointer of notifier block.
1030 */
1031static int charger_extcon_notifier(struct notifier_block *self,
1032 unsigned long event, void *ptr)
1033{
1034 struct charger_cable *cable =
1035 container_of(self, struct charger_cable, nb);
1036
1037 cable->attached = event;
1038 schedule_work(&cable->wq);
1039
1040 return NOTIFY_DONE;
1041}
1042
1043/**
1044 * charger_extcon_init - register external connector to use it
1045 * as the charger cable
1046 *
1047 * @cm: the Charger Manager representing the battery.
1048 * @cable: the Charger cable representing the external connector.
1049 */
1050static int charger_extcon_init(struct charger_manager *cm,
1051 struct charger_cable *cable)
1052{
1053 int ret = 0;
1054
1055 /*
1056 * Charger manager use Extcon framework to identify
1057 * the charger cable among various external connector
1058 * cable (e.g., TA, USB, MHL, Dock).
1059 */
1060 INIT_WORK(&cable->wq, charger_extcon_work);
1061 cable->nb.notifier_call = charger_extcon_notifier;
1062 ret = extcon_register_interest(&cable->extcon_dev,
1063 cable->extcon_name, cable->name, &cable->nb);
1064 if (ret < 0) {
1065 pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1066 cable->extcon_name,
1067 cable->name);
1068 ret = -EINVAL;
1069 }
1070
1071 return ret;
1072}
1073
997static int charger_manager_probe(struct platform_device *pdev) 1074static int charger_manager_probe(struct platform_device *pdev)
998{ 1075{
999 struct charger_desc *desc = dev_get_platdata(&pdev->dev); 1076 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1000 struct charger_manager *cm; 1077 struct charger_manager *cm;
1001 int ret = 0, i = 0; 1078 int ret = 0, i = 0;
1079 int j = 0;
1002 union power_supply_propval val; 1080 union power_supply_propval val;
1003 1081
1004 if (g_desc && !rtc_dev && g_desc->rtc_name) { 1082 if (g_desc && !rtc_dev && g_desc->rtc_name) {
@@ -1167,11 +1245,31 @@ static int charger_manager_probe(struct platform_device *pdev)
1167 goto err_register; 1245 goto err_register;
1168 } 1246 }
1169 1247
1170 ret = regulator_bulk_get(&pdev->dev, desc->num_charger_regulators, 1248 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1171 desc->charger_regulators); 1249 struct charger_regulator *charger
1172 if (ret) { 1250 = &desc->charger_regulators[i];
1173 dev_err(&pdev->dev, "Cannot get charger regulators.\n"); 1251
1174 goto err_bulk_get; 1252 charger->consumer = regulator_get(&pdev->dev,
1253 charger->regulator_name);
1254 if (charger->consumer == NULL) {
1255 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1256 charger->regulator_name);
1257 ret = -EINVAL;
1258 goto err_chg_get;
1259 }
1260
1261 for (j = 0 ; j < charger->num_cables ; j++) {
1262 struct charger_cable *cable = &charger->cables[j];
1263
1264 ret = charger_extcon_init(cm, cable);
1265 if (ret < 0) {
1266 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1267 charger->regulator_name);
1268 goto err_extcon;
1269 }
1270 cable->charger = charger;
1271 cable->cm = cm;
1272 }
1175 } 1273 }
1176 1274
1177 ret = try_charger_enable(cm, true); 1275 ret = try_charger_enable(cm, true);
@@ -1197,9 +1295,19 @@ static int charger_manager_probe(struct platform_device *pdev)
1197 return 0; 1295 return 0;
1198 1296
1199err_chg_enable: 1297err_chg_enable:
1200 regulator_bulk_free(desc->num_charger_regulators, 1298err_extcon:
1201 desc->charger_regulators); 1299 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1202err_bulk_get: 1300 struct charger_regulator *charger
1301 = &desc->charger_regulators[i];
1302 for (j = 0 ; j < charger->num_cables ; j++) {
1303 struct charger_cable *cable = &charger->cables[j];
1304 extcon_unregister_interest(&cable->extcon_dev);
1305 }
1306 }
1307err_chg_get:
1308 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1309 regulator_put(desc->charger_regulators[i].consumer);
1310
1203 power_supply_unregister(&cm->charger_psy); 1311 power_supply_unregister(&cm->charger_psy);
1204err_register: 1312err_register:
1205 kfree(cm->charger_psy.properties); 1313 kfree(cm->charger_psy.properties);
@@ -1218,6 +1326,8 @@ static int __devexit charger_manager_remove(struct platform_device *pdev)
1218{ 1326{
1219 struct charger_manager *cm = platform_get_drvdata(pdev); 1327 struct charger_manager *cm = platform_get_drvdata(pdev);
1220 struct charger_desc *desc = cm->desc; 1328 struct charger_desc *desc = cm->desc;
1329 int i = 0;
1330 int j = 0;
1221 1331
1222 /* Remove from the list */ 1332 /* Remove from the list */
1223 mutex_lock(&cm_list_mtx); 1333 mutex_lock(&cm_list_mtx);
@@ -1229,8 +1339,18 @@ static int __devexit charger_manager_remove(struct platform_device *pdev)
1229 if (delayed_work_pending(&cm_monitor_work)) 1339 if (delayed_work_pending(&cm_monitor_work))
1230 cancel_delayed_work_sync(&cm_monitor_work); 1340 cancel_delayed_work_sync(&cm_monitor_work);
1231 1341
1232 regulator_bulk_free(desc->num_charger_regulators, 1342 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1233 desc->charger_regulators); 1343 struct charger_regulator *charger
1344 = &desc->charger_regulators[i];
1345 for (j = 0 ; j < charger->num_cables ; j++) {
1346 struct charger_cable *cable = &charger->cables[j];
1347 extcon_unregister_interest(&cable->extcon_dev);
1348 }
1349 }
1350
1351 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1352 regulator_put(desc->charger_regulators[i].consumer);
1353
1234 power_supply_unregister(&cm->charger_psy); 1354 power_supply_unregister(&cm->charger_psy);
1235 1355
1236 try_charger_enable(cm, false); 1356 try_charger_enable(cm, false);
diff --git a/drivers/power/ds2781_battery.c b/drivers/power/ds2781_battery.c
index 5f92a4bb33f9..7a1ff4e4cf9a 100644
--- a/drivers/power/ds2781_battery.c
+++ b/drivers/power/ds2781_battery.c
@@ -64,7 +64,7 @@ static inline int ds2781_battery_io(struct ds2781_device_info *dev_info,
64 return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io); 64 return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io);
65} 65}
66 66
67int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf, 67static int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf,
68 int addr, size_t count) 68 int addr, size_t count)
69{ 69{
70 return ds2781_battery_io(dev_info, buf, addr, count, 0); 70 return ds2781_battery_io(dev_info, buf, addr, count, 0);
diff --git a/drivers/power/gpio-charger.c b/drivers/power/gpio-charger.c
index 8672c9177dd7..cb2aa3195687 100644
--- a/drivers/power/gpio-charger.c
+++ b/drivers/power/gpio-charger.c
@@ -54,7 +54,7 @@ static int gpio_charger_get_property(struct power_supply *psy,
54 54
55 switch (psp) { 55 switch (psp) {
56 case POWER_SUPPLY_PROP_ONLINE: 56 case POWER_SUPPLY_PROP_ONLINE:
57 val->intval = gpio_get_value(pdata->gpio); 57 val->intval = gpio_get_value_cansleep(pdata->gpio);
58 val->intval ^= pdata->gpio_active_low; 58 val->intval ^= pdata->gpio_active_low;
59 break; 59 break;
60 default: 60 default:
diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c
index d8b75780bfef..6a364f4798f7 100644
--- a/drivers/power/lp8727_charger.c
+++ b/drivers/power/lp8727_charger.c
@@ -15,7 +15,7 @@
15#include <linux/interrupt.h> 15#include <linux/interrupt.h>
16#include <linux/i2c.h> 16#include <linux/i2c.h>
17#include <linux/power_supply.h> 17#include <linux/power_supply.h>
18#include <linux/lp8727.h> 18#include <linux/platform_data/lp8727.h>
19 19
20#define DEBOUNCE_MSEC 270 20#define DEBOUNCE_MSEC 270
21 21
diff --git a/drivers/power/max17042_battery.c b/drivers/power/max17042_battery.c
index 140788b309f8..74abc6c755b4 100644
--- a/drivers/power/max17042_battery.c
+++ b/drivers/power/max17042_battery.c
@@ -113,6 +113,7 @@ static enum power_supply_property max17042_battery_props[] = {
113 POWER_SUPPLY_PROP_VOLTAGE_OCV, 113 POWER_SUPPLY_PROP_VOLTAGE_OCV,
114 POWER_SUPPLY_PROP_CAPACITY, 114 POWER_SUPPLY_PROP_CAPACITY,
115 POWER_SUPPLY_PROP_CHARGE_FULL, 115 POWER_SUPPLY_PROP_CHARGE_FULL,
116 POWER_SUPPLY_PROP_CHARGE_COUNTER,
116 POWER_SUPPLY_PROP_TEMP, 117 POWER_SUPPLY_PROP_TEMP,
117 POWER_SUPPLY_PROP_CURRENT_NOW, 118 POWER_SUPPLY_PROP_CURRENT_NOW,
118 POWER_SUPPLY_PROP_CURRENT_AVG, 119 POWER_SUPPLY_PROP_CURRENT_AVG,
@@ -201,6 +202,13 @@ static int max17042_get_property(struct power_supply *psy,
201 202
202 val->intval = ret * 1000 / 2; 203 val->intval = ret * 1000 / 2;
203 break; 204 break;
205 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
206 ret = max17042_read_reg(chip->client, MAX17042_QH);
207 if (ret < 0)
208 return ret;
209
210 val->intval = ret * 1000 / 2;
211 break;
204 case POWER_SUPPLY_PROP_TEMP: 212 case POWER_SUPPLY_PROP_TEMP:
205 ret = max17042_read_reg(chip->client, MAX17042_TEMP); 213 ret = max17042_read_reg(chip->client, MAX17042_TEMP);
206 if (ret < 0) 214 if (ret < 0)
diff --git a/drivers/power/olpc_battery.c b/drivers/power/olpc_battery.c
index 7385092f9bc8..55b10b813353 100644
--- a/drivers/power/olpc_battery.c
+++ b/drivers/power/olpc_battery.c
@@ -231,11 +231,9 @@ static int olpc_bat_get_charge_full_design(union power_supply_propval *val)
231 231
232 case POWER_SUPPLY_TECHNOLOGY_LiFe: 232 case POWER_SUPPLY_TECHNOLOGY_LiFe:
233 switch (mfr) { 233 switch (mfr) {
234 case 1: /* Gold Peak */ 234 case 1: /* Gold Peak, fall through */
235 val->intval = 2800000;
236 break;
237 case 2: /* BYD */ 235 case 2: /* BYD */
238 val->intval = 3100000; 236 val->intval = 2800000;
239 break; 237 break;
240 default: 238 default:
241 return -EIO; 239 return -EIO;
@@ -267,6 +265,55 @@ static int olpc_bat_get_charge_now(union power_supply_propval *val)
267 return 0; 265 return 0;
268} 266}
269 267
268static int olpc_bat_get_voltage_max_design(union power_supply_propval *val)
269{
270 uint8_t ec_byte;
271 union power_supply_propval tech;
272 int mfr;
273 int ret;
274
275 ret = olpc_bat_get_tech(&tech);
276 if (ret)
277 return ret;
278
279 ec_byte = BAT_ADDR_MFR_TYPE;
280 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
281 if (ret)
282 return ret;
283
284 mfr = ec_byte >> 4;
285
286 switch (tech.intval) {
287 case POWER_SUPPLY_TECHNOLOGY_NiMH:
288 switch (mfr) {
289 case 1: /* Gold Peak */
290 val->intval = 6000000;
291 break;
292 default:
293 return -EIO;
294 }
295 break;
296
297 case POWER_SUPPLY_TECHNOLOGY_LiFe:
298 switch (mfr) {
299 case 1: /* Gold Peak */
300 val->intval = 6400000;
301 break;
302 case 2: /* BYD */
303 val->intval = 6500000;
304 break;
305 default:
306 return -EIO;
307 }
308 break;
309
310 default:
311 return -EIO;
312 }
313
314 return ret;
315}
316
270/********************************************************************* 317/*********************************************************************
271 * Battery properties 318 * Battery properties
272 *********************************************************************/ 319 *********************************************************************/
@@ -401,6 +448,11 @@ static int olpc_bat_get_property(struct power_supply *psy,
401 sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf)); 448 sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
402 val->strval = bat_serial; 449 val->strval = bat_serial;
403 break; 450 break;
451 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
452 ret = olpc_bat_get_voltage_max_design(val);
453 if (ret)
454 return ret;
455 break;
404 default: 456 default:
405 ret = -EINVAL; 457 ret = -EINVAL;
406 break; 458 break;
@@ -428,6 +480,7 @@ static enum power_supply_property olpc_xo1_bat_props[] = {
428 POWER_SUPPLY_PROP_MANUFACTURER, 480 POWER_SUPPLY_PROP_MANUFACTURER,
429 POWER_SUPPLY_PROP_SERIAL_NUMBER, 481 POWER_SUPPLY_PROP_SERIAL_NUMBER,
430 POWER_SUPPLY_PROP_CHARGE_COUNTER, 482 POWER_SUPPLY_PROP_CHARGE_COUNTER,
483 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
431}; 484};
432 485
433/* XO-1.5 does not have ambient temperature property */ 486/* XO-1.5 does not have ambient temperature property */
@@ -449,6 +502,7 @@ static enum power_supply_property olpc_xo15_bat_props[] = {
449 POWER_SUPPLY_PROP_MANUFACTURER, 502 POWER_SUPPLY_PROP_MANUFACTURER,
450 POWER_SUPPLY_PROP_SERIAL_NUMBER, 503 POWER_SUPPLY_PROP_SERIAL_NUMBER,
451 POWER_SUPPLY_PROP_CHARGE_COUNTER, 504 POWER_SUPPLY_PROP_CHARGE_COUNTER,
505 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
452}; 506};
453 507
454/* EEPROM reading goes completely around the power_supply API, sadly */ 508/* EEPROM reading goes completely around the power_supply API, sadly */
diff --git a/drivers/power/pda_power.c b/drivers/power/pda_power.c
index 8dbcd53c5e67..7312f2651647 100644
--- a/drivers/power/pda_power.c
+++ b/drivers/power/pda_power.c
@@ -24,11 +24,7 @@
24 24
25static inline unsigned int get_irq_flags(struct resource *res) 25static inline unsigned int get_irq_flags(struct resource *res)
26{ 26{
27 unsigned int flags = IRQF_SAMPLE_RANDOM | IRQF_SHARED; 27 return IRQF_SHARED | (res->flags & IRQF_TRIGGER_MASK);
28
29 flags |= res->flags & IRQF_TRIGGER_MASK;
30
31 return flags;
32} 28}
33 29
34static struct device *dev; 30static struct device *dev;
@@ -134,13 +130,13 @@ static void update_charger(void)
134 regulator_set_current_limit(ac_draw, max_uA, max_uA); 130 regulator_set_current_limit(ac_draw, max_uA, max_uA);
135 if (!regulator_enabled) { 131 if (!regulator_enabled) {
136 dev_dbg(dev, "charger on (AC)\n"); 132 dev_dbg(dev, "charger on (AC)\n");
137 regulator_enable(ac_draw); 133 WARN_ON(regulator_enable(ac_draw));
138 regulator_enabled = 1; 134 regulator_enabled = 1;
139 } 135 }
140 } else { 136 } else {
141 if (regulator_enabled) { 137 if (regulator_enabled) {
142 dev_dbg(dev, "charger off\n"); 138 dev_dbg(dev, "charger off\n");
143 regulator_disable(ac_draw); 139 WARN_ON(regulator_disable(ac_draw));
144 regulator_enabled = 0; 140 regulator_enabled = 0;
145 } 141 }
146 } 142 }
diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index 6ad612726785..08cc8a3c15af 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -17,6 +17,7 @@
17#include <linux/device.h> 17#include <linux/device.h>
18#include <linux/err.h> 18#include <linux/err.h>
19#include <linux/power_supply.h> 19#include <linux/power_supply.h>
20#include <linux/thermal.h>
20#include "power_supply.h" 21#include "power_supply.h"
21 22
22/* exported for the APM Power driver, APM emulation */ 23/* exported for the APM Power driver, APM emulation */
@@ -169,6 +170,63 @@ static void power_supply_dev_release(struct device *dev)
169 kfree(dev); 170 kfree(dev);
170} 171}
171 172
173#ifdef CONFIG_THERMAL
174static int power_supply_read_temp(struct thermal_zone_device *tzd,
175 unsigned long *temp)
176{
177 struct power_supply *psy;
178 union power_supply_propval val;
179 int ret;
180
181 WARN_ON(tzd == NULL);
182 psy = tzd->devdata;
183 ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
184
185 /* Convert tenths of degree Celsius to milli degree Celsius. */
186 if (!ret)
187 *temp = val.intval * 100;
188
189 return ret;
190}
191
192static struct thermal_zone_device_ops psy_tzd_ops = {
193 .get_temp = power_supply_read_temp,
194};
195
196static int psy_register_thermal(struct power_supply *psy)
197{
198 int i;
199
200 /* Register battery zone device psy reports temperature */
201 for (i = 0; i < psy->num_properties; i++) {
202 if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
203 psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
204 psy, &psy_tzd_ops, 0, 0, 0, 0);
205 if (IS_ERR(psy->tzd))
206 return PTR_ERR(psy->tzd);
207 break;
208 }
209 }
210 return 0;
211}
212
213static void psy_unregister_thermal(struct power_supply *psy)
214{
215 if (IS_ERR_OR_NULL(psy->tzd))
216 return;
217 thermal_zone_device_unregister(psy->tzd);
218}
219#else
220static int psy_register_thermal(struct power_supply *psy)
221{
222 return 0;
223}
224
225static void psy_unregister_thermal(struct power_supply *psy)
226{
227}
228#endif
229
172int power_supply_register(struct device *parent, struct power_supply *psy) 230int power_supply_register(struct device *parent, struct power_supply *psy)
173{ 231{
174 struct device *dev; 232 struct device *dev;
@@ -197,6 +255,10 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
197 if (rc) 255 if (rc)
198 goto device_add_failed; 256 goto device_add_failed;
199 257
258 rc = psy_register_thermal(psy);
259 if (rc)
260 goto register_thermal_failed;
261
200 rc = power_supply_create_triggers(psy); 262 rc = power_supply_create_triggers(psy);
201 if (rc) 263 if (rc)
202 goto create_triggers_failed; 264 goto create_triggers_failed;
@@ -206,6 +268,8 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
206 goto success; 268 goto success;
207 269
208create_triggers_failed: 270create_triggers_failed:
271 psy_unregister_thermal(psy);
272register_thermal_failed:
209 device_del(dev); 273 device_del(dev);
210kobject_set_name_failed: 274kobject_set_name_failed:
211device_add_failed: 275device_add_failed:
@@ -220,6 +284,7 @@ void power_supply_unregister(struct power_supply *psy)
220 cancel_work_sync(&psy->changed_work); 284 cancel_work_sync(&psy->changed_work);
221 sysfs_remove_link(&psy->dev->kobj, "powers"); 285 sysfs_remove_link(&psy->dev->kobj, "powers");
222 power_supply_remove_triggers(psy); 286 power_supply_remove_triggers(psy);
287 psy_unregister_thermal(psy);
223 device_unregister(psy->dev); 288 device_unregister(psy->dev);
224} 289}
225EXPORT_SYMBOL_GPL(power_supply_unregister); 290EXPORT_SYMBOL_GPL(power_supply_unregister);
diff --git a/drivers/power/power_supply_sysfs.c b/drivers/power/power_supply_sysfs.c
index 4150747f9186..1d96614a17a4 100644
--- a/drivers/power/power_supply_sysfs.c
+++ b/drivers/power/power_supply_sysfs.c
@@ -159,6 +159,8 @@ static struct device_attribute power_supply_attrs[] = {
159 POWER_SUPPLY_ATTR(charge_now), 159 POWER_SUPPLY_ATTR(charge_now),
160 POWER_SUPPLY_ATTR(charge_avg), 160 POWER_SUPPLY_ATTR(charge_avg),
161 POWER_SUPPLY_ATTR(charge_counter), 161 POWER_SUPPLY_ATTR(charge_counter),
162 POWER_SUPPLY_ATTR(constant_charge_current),
163 POWER_SUPPLY_ATTR(constant_charge_voltage),
162 POWER_SUPPLY_ATTR(energy_full_design), 164 POWER_SUPPLY_ATTR(energy_full_design),
163 POWER_SUPPLY_ATTR(energy_empty_design), 165 POWER_SUPPLY_ATTR(energy_empty_design),
164 POWER_SUPPLY_ATTR(energy_full), 166 POWER_SUPPLY_ATTR(energy_full),
@@ -166,9 +168,15 @@ static struct device_attribute power_supply_attrs[] = {
166 POWER_SUPPLY_ATTR(energy_now), 168 POWER_SUPPLY_ATTR(energy_now),
167 POWER_SUPPLY_ATTR(energy_avg), 169 POWER_SUPPLY_ATTR(energy_avg),
168 POWER_SUPPLY_ATTR(capacity), 170 POWER_SUPPLY_ATTR(capacity),
171 POWER_SUPPLY_ATTR(capacity_alert_min),
172 POWER_SUPPLY_ATTR(capacity_alert_max),
169 POWER_SUPPLY_ATTR(capacity_level), 173 POWER_SUPPLY_ATTR(capacity_level),
170 POWER_SUPPLY_ATTR(temp), 174 POWER_SUPPLY_ATTR(temp),
175 POWER_SUPPLY_ATTR(temp_alert_min),
176 POWER_SUPPLY_ATTR(temp_alert_max),
171 POWER_SUPPLY_ATTR(temp_ambient), 177 POWER_SUPPLY_ATTR(temp_ambient),
178 POWER_SUPPLY_ATTR(temp_ambient_alert_min),
179 POWER_SUPPLY_ATTR(temp_ambient_alert_max),
172 POWER_SUPPLY_ATTR(time_to_empty_now), 180 POWER_SUPPLY_ATTR(time_to_empty_now),
173 POWER_SUPPLY_ATTR(time_to_empty_avg), 181 POWER_SUPPLY_ATTR(time_to_empty_avg),
174 POWER_SUPPLY_ATTR(time_to_full_now), 182 POWER_SUPPLY_ATTR(time_to_full_now),
diff --git a/drivers/power/sbs-battery.c b/drivers/power/sbs-battery.c
index a5b6849d4123..a65e8f54157e 100644
--- a/drivers/power/sbs-battery.c
+++ b/drivers/power/sbs-battery.c
@@ -469,7 +469,7 @@ static int sbs_get_property(struct power_supply *psy,
469 469
470 case POWER_SUPPLY_PROP_TECHNOLOGY: 470 case POWER_SUPPLY_PROP_TECHNOLOGY:
471 val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 471 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
472 break; 472 goto done; /* don't trigger power_supply_changed()! */
473 473
474 case POWER_SUPPLY_PROP_ENERGY_NOW: 474 case POWER_SUPPLY_PROP_ENERGY_NOW:
475 case POWER_SUPPLY_PROP_ENERGY_FULL: 475 case POWER_SUPPLY_PROP_ENERGY_FULL:
diff --git a/drivers/power/smb347-charger.c b/drivers/power/smb347-charger.c
index f8eedd8a676f..332dd0110bda 100644
--- a/drivers/power/smb347-charger.c
+++ b/drivers/power/smb347-charger.c
@@ -196,6 +196,14 @@ static const unsigned int ccc_tbl[] = {
196 1200000, 196 1200000,
197}; 197};
198 198
199/* Convert register value to current using lookup table */
200static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
201{
202 if (val >= size)
203 return -EINVAL;
204 return tbl[val];
205}
206
199/* Convert current to register value using lookup table */ 207/* Convert current to register value using lookup table */
200static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val) 208static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
201{ 209{
@@ -841,22 +849,101 @@ fail:
841 return ret; 849 return ret;
842} 850}
843 851
852/*
853 * Returns the constant charge current programmed
854 * into the charger in uA.
855 */
856static int get_const_charge_current(struct smb347_charger *smb)
857{
858 int ret, intval;
859 unsigned int v;
860
861 if (!smb347_is_ps_online(smb))
862 return -ENODATA;
863
864 ret = regmap_read(smb->regmap, STAT_B, &v);
865 if (ret < 0)
866 return ret;
867
868 /*
869 * The current value is composition of FCC and PCC values
870 * and we can detect which table to use from bit 5.
871 */
872 if (v & 0x20) {
873 intval = hw_to_current(fcc_tbl, ARRAY_SIZE(fcc_tbl), v & 7);
874 } else {
875 v >>= 3;
876 intval = hw_to_current(pcc_tbl, ARRAY_SIZE(pcc_tbl), v & 7);
877 }
878
879 return intval;
880}
881
882/*
883 * Returns the constant charge voltage programmed
884 * into the charger in uV.
885 */
886static int get_const_charge_voltage(struct smb347_charger *smb)
887{
888 int ret, intval;
889 unsigned int v;
890
891 if (!smb347_is_ps_online(smb))
892 return -ENODATA;
893
894 ret = regmap_read(smb->regmap, STAT_A, &v);
895 if (ret < 0)
896 return ret;
897
898 v &= STAT_A_FLOAT_VOLTAGE_MASK;
899 if (v > 0x3d)
900 v = 0x3d;
901
902 intval = 3500000 + v * 20000;
903
904 return intval;
905}
906
844static int smb347_mains_get_property(struct power_supply *psy, 907static int smb347_mains_get_property(struct power_supply *psy,
845 enum power_supply_property prop, 908 enum power_supply_property prop,
846 union power_supply_propval *val) 909 union power_supply_propval *val)
847{ 910{
848 struct smb347_charger *smb = 911 struct smb347_charger *smb =
849 container_of(psy, struct smb347_charger, mains); 912 container_of(psy, struct smb347_charger, mains);
913 int ret;
850 914
851 if (prop == POWER_SUPPLY_PROP_ONLINE) { 915 switch (prop) {
916 case POWER_SUPPLY_PROP_ONLINE:
852 val->intval = smb->mains_online; 917 val->intval = smb->mains_online;
853 return 0; 918 break;
919
920 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
921 ret = get_const_charge_voltage(smb);
922 if (ret < 0)
923 return ret;
924 else
925 val->intval = ret;
926 break;
927
928 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
929 ret = get_const_charge_current(smb);
930 if (ret < 0)
931 return ret;
932 else
933 val->intval = ret;
934 break;
935
936 default:
937 return -EINVAL;
854 } 938 }
855 return -EINVAL; 939
940 return 0;
856} 941}
857 942
858static enum power_supply_property smb347_mains_properties[] = { 943static enum power_supply_property smb347_mains_properties[] = {
859 POWER_SUPPLY_PROP_ONLINE, 944 POWER_SUPPLY_PROP_ONLINE,
945 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
946 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
860}; 947};
861 948
862static int smb347_usb_get_property(struct power_supply *psy, 949static int smb347_usb_get_property(struct power_supply *psy,
@@ -865,16 +952,40 @@ static int smb347_usb_get_property(struct power_supply *psy,
865{ 952{
866 struct smb347_charger *smb = 953 struct smb347_charger *smb =
867 container_of(psy, struct smb347_charger, usb); 954 container_of(psy, struct smb347_charger, usb);
955 int ret;
868 956
869 if (prop == POWER_SUPPLY_PROP_ONLINE) { 957 switch (prop) {
958 case POWER_SUPPLY_PROP_ONLINE:
870 val->intval = smb->usb_online; 959 val->intval = smb->usb_online;
871 return 0; 960 break;
961
962 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
963 ret = get_const_charge_voltage(smb);
964 if (ret < 0)
965 return ret;
966 else
967 val->intval = ret;
968 break;
969
970 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
971 ret = get_const_charge_current(smb);
972 if (ret < 0)
973 return ret;
974 else
975 val->intval = ret;
976 break;
977
978 default:
979 return -EINVAL;
872 } 980 }
873 return -EINVAL; 981
982 return 0;
874} 983}
875 984
876static enum power_supply_property smb347_usb_properties[] = { 985static enum power_supply_property smb347_usb_properties[] = {
877 POWER_SUPPLY_PROP_ONLINE, 986 POWER_SUPPLY_PROP_ONLINE,
987 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
988 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
878}; 989};
879 990
880static int smb347_battery_get_property(struct power_supply *psy, 991static int smb347_battery_get_property(struct power_supply *psy,
diff --git a/drivers/power/test_power.c b/drivers/power/test_power.c
index b527c93bf2f3..b99a452a4fda 100644
--- a/drivers/power/test_power.c
+++ b/drivers/power/test_power.c
@@ -22,11 +22,13 @@
22#include <linux/vermagic.h> 22#include <linux/vermagic.h>
23 23
24static int ac_online = 1; 24static int ac_online = 1;
25static int usb_online = 1;
25static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING; 26static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
26static int battery_health = POWER_SUPPLY_HEALTH_GOOD; 27static int battery_health = POWER_SUPPLY_HEALTH_GOOD;
27static int battery_present = 1; /* true */ 28static int battery_present = 1; /* true */
28static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION; 29static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
29static int battery_capacity = 50; 30static int battery_capacity = 50;
31static int battery_voltage = 3300;
30 32
31static int test_power_get_ac_property(struct power_supply *psy, 33static int test_power_get_ac_property(struct power_supply *psy,
32 enum power_supply_property psp, 34 enum power_supply_property psp,
@@ -42,6 +44,20 @@ static int test_power_get_ac_property(struct power_supply *psy,
42 return 0; 44 return 0;
43} 45}
44 46
47static int test_power_get_usb_property(struct power_supply *psy,
48 enum power_supply_property psp,
49 union power_supply_propval *val)
50{
51 switch (psp) {
52 case POWER_SUPPLY_PROP_ONLINE:
53 val->intval = usb_online;
54 break;
55 default:
56 return -EINVAL;
57 }
58 return 0;
59}
60
45static int test_power_get_battery_property(struct power_supply *psy, 61static int test_power_get_battery_property(struct power_supply *psy,
46 enum power_supply_property psp, 62 enum power_supply_property psp,
47 union power_supply_propval *val) 63 union power_supply_propval *val)
@@ -86,6 +102,12 @@ static int test_power_get_battery_property(struct power_supply *psy,
86 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: 102 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
87 val->intval = 3600; 103 val->intval = 3600;
88 break; 104 break;
105 case POWER_SUPPLY_PROP_TEMP:
106 val->intval = 26;
107 break;
108 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
109 val->intval = battery_voltage;
110 break;
89 default: 111 default:
90 pr_info("%s: some properties deliberately report errors.\n", 112 pr_info("%s: some properties deliberately report errors.\n",
91 __func__); 113 __func__);
@@ -114,6 +136,8 @@ static enum power_supply_property test_power_battery_props[] = {
114 POWER_SUPPLY_PROP_MODEL_NAME, 136 POWER_SUPPLY_PROP_MODEL_NAME,
115 POWER_SUPPLY_PROP_MANUFACTURER, 137 POWER_SUPPLY_PROP_MANUFACTURER,
116 POWER_SUPPLY_PROP_SERIAL_NUMBER, 138 POWER_SUPPLY_PROP_SERIAL_NUMBER,
139 POWER_SUPPLY_PROP_TEMP,
140 POWER_SUPPLY_PROP_VOLTAGE_NOW,
117}; 141};
118 142
119static char *test_power_ac_supplied_to[] = { 143static char *test_power_ac_supplied_to[] = {
@@ -135,6 +159,14 @@ static struct power_supply test_power_supplies[] = {
135 .properties = test_power_battery_props, 159 .properties = test_power_battery_props,
136 .num_properties = ARRAY_SIZE(test_power_battery_props), 160 .num_properties = ARRAY_SIZE(test_power_battery_props),
137 .get_property = test_power_get_battery_property, 161 .get_property = test_power_get_battery_property,
162 }, {
163 .name = "test_usb",
164 .type = POWER_SUPPLY_TYPE_USB,
165 .supplied_to = test_power_ac_supplied_to,
166 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
167 .properties = test_power_ac_props,
168 .num_properties = ARRAY_SIZE(test_power_ac_props),
169 .get_property = test_power_get_usb_property,
138 }, 170 },
139}; 171};
140 172
@@ -167,6 +199,7 @@ static void __exit test_power_exit(void)
167 199
168 /* Let's see how we handle changes... */ 200 /* Let's see how we handle changes... */
169 ac_online = 0; 201 ac_online = 0;
202 usb_online = 0;
170 battery_status = POWER_SUPPLY_STATUS_DISCHARGING; 203 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
171 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) 204 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
172 power_supply_changed(&test_power_supplies[i]); 205 power_supply_changed(&test_power_supplies[i]);
@@ -275,6 +308,19 @@ static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
275 return strlen(buffer); 308 return strlen(buffer);
276} 309}
277 310
311static int param_set_usb_online(const char *key, const struct kernel_param *kp)
312{
313 usb_online = map_get_value(map_ac_online, key, usb_online);
314 power_supply_changed(&test_power_supplies[2]);
315 return 0;
316}
317
318static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
319{
320 strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown"));
321 return strlen(buffer);
322}
323
278static int param_set_battery_status(const char *key, 324static int param_set_battery_status(const char *key,
279 const struct kernel_param *kp) 325 const struct kernel_param *kp)
280{ 326{
@@ -350,13 +396,31 @@ static int param_set_battery_capacity(const char *key,
350 396
351#define param_get_battery_capacity param_get_int 397#define param_get_battery_capacity param_get_int
352 398
399static int param_set_battery_voltage(const char *key,
400 const struct kernel_param *kp)
401{
402 int tmp;
403
404 if (1 != sscanf(key, "%d", &tmp))
405 return -EINVAL;
406
407 battery_voltage = tmp;
408 power_supply_changed(&test_power_supplies[1]);
409 return 0;
410}
353 411
412#define param_get_battery_voltage param_get_int
354 413
355static struct kernel_param_ops param_ops_ac_online = { 414static struct kernel_param_ops param_ops_ac_online = {
356 .set = param_set_ac_online, 415 .set = param_set_ac_online,
357 .get = param_get_ac_online, 416 .get = param_get_ac_online,
358}; 417};
359 418
419static struct kernel_param_ops param_ops_usb_online = {
420 .set = param_set_usb_online,
421 .get = param_get_usb_online,
422};
423
360static struct kernel_param_ops param_ops_battery_status = { 424static struct kernel_param_ops param_ops_battery_status = {
361 .set = param_set_battery_status, 425 .set = param_set_battery_status,
362 .get = param_get_battery_status, 426 .get = param_get_battery_status,
@@ -382,18 +446,27 @@ static struct kernel_param_ops param_ops_battery_capacity = {
382 .get = param_get_battery_capacity, 446 .get = param_get_battery_capacity,
383}; 447};
384 448
449static struct kernel_param_ops param_ops_battery_voltage = {
450 .set = param_set_battery_voltage,
451 .get = param_get_battery_voltage,
452};
385 453
386#define param_check_ac_online(name, p) __param_check(name, p, void); 454#define param_check_ac_online(name, p) __param_check(name, p, void);
455#define param_check_usb_online(name, p) __param_check(name, p, void);
387#define param_check_battery_status(name, p) __param_check(name, p, void); 456#define param_check_battery_status(name, p) __param_check(name, p, void);
388#define param_check_battery_present(name, p) __param_check(name, p, void); 457#define param_check_battery_present(name, p) __param_check(name, p, void);
389#define param_check_battery_technology(name, p) __param_check(name, p, void); 458#define param_check_battery_technology(name, p) __param_check(name, p, void);
390#define param_check_battery_health(name, p) __param_check(name, p, void); 459#define param_check_battery_health(name, p) __param_check(name, p, void);
391#define param_check_battery_capacity(name, p) __param_check(name, p, void); 460#define param_check_battery_capacity(name, p) __param_check(name, p, void);
461#define param_check_battery_voltage(name, p) __param_check(name, p, void);
392 462
393 463
394module_param(ac_online, ac_online, 0644); 464module_param(ac_online, ac_online, 0644);
395MODULE_PARM_DESC(ac_online, "AC charging state <on|off>"); 465MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
396 466
467module_param(usb_online, usb_online, 0644);
468MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
469
397module_param(battery_status, battery_status, 0644); 470module_param(battery_status, battery_status, 0644);
398MODULE_PARM_DESC(battery_status, 471MODULE_PARM_DESC(battery_status,
399 "battery status <charging|discharging|not-charging|full>"); 472 "battery status <charging|discharging|not-charging|full>");
@@ -413,6 +486,8 @@ MODULE_PARM_DESC(battery_health,
413module_param(battery_capacity, battery_capacity, 0644); 486module_param(battery_capacity, battery_capacity, 0644);
414MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)"); 487MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
415 488
489module_param(battery_voltage, battery_voltage, 0644);
490MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
416 491
417MODULE_DESCRIPTION("Power supply driver for testing"); 492MODULE_DESCRIPTION("Power supply driver for testing");
418MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); 493MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
diff --git a/drivers/power/twl4030_charger.c b/drivers/power/twl4030_charger.c
index 7cacbaa68efe..15f4d5d8611b 100644
--- a/drivers/power/twl4030_charger.c
+++ b/drivers/power/twl4030_charger.c
@@ -22,6 +22,7 @@
22#include <linux/power_supply.h> 22#include <linux/power_supply.h>
23#include <linux/notifier.h> 23#include <linux/notifier.h>
24#include <linux/usb/otg.h> 24#include <linux/usb/otg.h>
25#include <linux/regulator/machine.h>
25 26
26#define TWL4030_BCIMSTATEC 0x02 27#define TWL4030_BCIMSTATEC 0x02
27#define TWL4030_BCIICHG 0x08 28#define TWL4030_BCIICHG 0x08
@@ -29,6 +30,7 @@
29#define TWL4030_BCIVBUS 0x0c 30#define TWL4030_BCIVBUS 0x0c
30#define TWL4030_BCIMFSTS4 0x10 31#define TWL4030_BCIMFSTS4 0x10
31#define TWL4030_BCICTL1 0x23 32#define TWL4030_BCICTL1 0x23
33#define TWL4030_BB_CFG 0x12
32 34
33#define TWL4030_BCIAUTOWEN BIT(5) 35#define TWL4030_BCIAUTOWEN BIT(5)
34#define TWL4030_CONFIG_DONE BIT(4) 36#define TWL4030_CONFIG_DONE BIT(4)
@@ -38,6 +40,17 @@
38#define TWL4030_USBFASTMCHG BIT(2) 40#define TWL4030_USBFASTMCHG BIT(2)
39#define TWL4030_STS_VBUS BIT(7) 41#define TWL4030_STS_VBUS BIT(7)
40#define TWL4030_STS_USB_ID BIT(2) 42#define TWL4030_STS_USB_ID BIT(2)
43#define TWL4030_BBCHEN BIT(4)
44#define TWL4030_BBSEL_MASK 0b1100
45#define TWL4030_BBSEL_2V5 0b0000
46#define TWL4030_BBSEL_3V0 0b0100
47#define TWL4030_BBSEL_3V1 0b1000
48#define TWL4030_BBSEL_3V2 0b1100
49#define TWL4030_BBISEL_MASK 0b11
50#define TWL4030_BBISEL_25uA 0b00
51#define TWL4030_BBISEL_150uA 0b01
52#define TWL4030_BBISEL_500uA 0b10
53#define TWL4030_BBISEL_1000uA 0b11
41 54
42/* BCI interrupts */ 55/* BCI interrupts */
43#define TWL4030_WOVF BIT(0) /* Watchdog overflow */ 56#define TWL4030_WOVF BIT(0) /* Watchdog overflow */
@@ -75,6 +88,8 @@ struct twl4030_bci {
75 struct work_struct work; 88 struct work_struct work;
76 int irq_chg; 89 int irq_chg;
77 int irq_bci; 90 int irq_bci;
91 struct regulator *usb_reg;
92 int usb_enabled;
78 93
79 unsigned long event; 94 unsigned long event;
80}; 95};
@@ -104,7 +119,7 @@ static int twl4030_bci_read(u8 reg, u8 *val)
104 119
105static int twl4030_clear_set_boot_bci(u8 clear, u8 set) 120static int twl4030_clear_set_boot_bci(u8 clear, u8 set)
106{ 121{
107 return twl4030_clear_set(TWL4030_MODULE_PM_MASTER, 0, 122 return twl4030_clear_set(TWL4030_MODULE_PM_MASTER, clear,
108 TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set, 123 TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set,
109 TWL4030_PM_MASTER_BOOT_BCI); 124 TWL4030_PM_MASTER_BOOT_BCI);
110} 125}
@@ -152,14 +167,14 @@ static int twl4030_bci_have_vbus(struct twl4030_bci *bci)
152} 167}
153 168
154/* 169/*
155 * Enable/Disable USB Charge funtionality. 170 * Enable/Disable USB Charge functionality.
156 */ 171 */
157static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable) 172static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
158{ 173{
159 int ret; 174 int ret;
160 175
161 if (enable) { 176 if (enable) {
162 /* Check for USB charger conneted */ 177 /* Check for USB charger connected */
163 if (!twl4030_bci_have_vbus(bci)) 178 if (!twl4030_bci_have_vbus(bci))
164 return -ENODEV; 179 return -ENODEV;
165 180
@@ -172,6 +187,12 @@ static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
172 return -EACCES; 187 return -EACCES;
173 } 188 }
174 189
190 /* Need to keep regulator on */
191 if (!bci->usb_enabled) {
192 regulator_enable(bci->usb_reg);
193 bci->usb_enabled = 1;
194 }
195
175 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */ 196 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
176 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB); 197 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB);
177 if (ret < 0) 198 if (ret < 0)
@@ -182,6 +203,10 @@ static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
182 TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4); 203 TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4);
183 } else { 204 } else {
184 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0); 205 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0);
206 if (bci->usb_enabled) {
207 regulator_disable(bci->usb_reg);
208 bci->usb_enabled = 0;
209 }
185 } 210 }
186 211
187 return ret; 212 return ret;
@@ -203,6 +228,49 @@ static int twl4030_charger_enable_ac(bool enable)
203} 228}
204 229
205/* 230/*
231 * Enable/Disable charging of Backup Battery.
232 */
233static int twl4030_charger_enable_backup(int uvolt, int uamp)
234{
235 int ret;
236 u8 flags;
237
238 if (uvolt < 2500000 ||
239 uamp < 25) {
240 /* disable charging of backup battery */
241 ret = twl4030_clear_set(TWL4030_MODULE_PM_RECEIVER,
242 TWL4030_BBCHEN, 0, TWL4030_BB_CFG);
243 return ret;
244 }
245
246 flags = TWL4030_BBCHEN;
247 if (uvolt >= 3200000)
248 flags |= TWL4030_BBSEL_3V2;
249 else if (uvolt >= 3100000)
250 flags |= TWL4030_BBSEL_3V1;
251 else if (uvolt >= 3000000)
252 flags |= TWL4030_BBSEL_3V0;
253 else
254 flags |= TWL4030_BBSEL_2V5;
255
256 if (uamp >= 1000)
257 flags |= TWL4030_BBISEL_1000uA;
258 else if (uamp >= 500)
259 flags |= TWL4030_BBISEL_500uA;
260 else if (uamp >= 150)
261 flags |= TWL4030_BBISEL_150uA;
262 else
263 flags |= TWL4030_BBISEL_25uA;
264
265 ret = twl4030_clear_set(TWL4030_MODULE_PM_RECEIVER,
266 TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK,
267 flags,
268 TWL4030_BB_CFG);
269
270 return ret;
271}
272
273/*
206 * TWL4030 CHG_PRES (AC charger presence) events 274 * TWL4030 CHG_PRES (AC charger presence) events
207 */ 275 */
208static irqreturn_t twl4030_charger_interrupt(int irq, void *arg) 276static irqreturn_t twl4030_charger_interrupt(int irq, void *arg)
@@ -425,6 +493,7 @@ static enum power_supply_property twl4030_charger_props[] = {
425static int __init twl4030_bci_probe(struct platform_device *pdev) 493static int __init twl4030_bci_probe(struct platform_device *pdev)
426{ 494{
427 struct twl4030_bci *bci; 495 struct twl4030_bci *bci;
496 struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
428 int ret; 497 int ret;
429 u32 reg; 498 u32 reg;
430 499
@@ -456,6 +525,8 @@ static int __init twl4030_bci_probe(struct platform_device *pdev)
456 bci->usb.num_properties = ARRAY_SIZE(twl4030_charger_props); 525 bci->usb.num_properties = ARRAY_SIZE(twl4030_charger_props);
457 bci->usb.get_property = twl4030_bci_get_property; 526 bci->usb.get_property = twl4030_bci_get_property;
458 527
528 bci->usb_reg = regulator_get(bci->dev, "bci3v1");
529
459 ret = power_supply_register(&pdev->dev, &bci->usb); 530 ret = power_supply_register(&pdev->dev, &bci->usb);
460 if (ret) { 531 if (ret) {
461 dev_err(&pdev->dev, "failed to register usb: %d\n", ret); 532 dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
@@ -504,6 +575,8 @@ static int __init twl4030_bci_probe(struct platform_device *pdev)
504 575
505 twl4030_charger_enable_ac(true); 576 twl4030_charger_enable_ac(true);
506 twl4030_charger_enable_usb(bci, true); 577 twl4030_charger_enable_usb(bci, true);
578 twl4030_charger_enable_backup(pdata->bb_uvolt,
579 pdata->bb_uamp);
507 580
508 return 0; 581 return 0;
509 582
@@ -532,6 +605,7 @@ static int __exit twl4030_bci_remove(struct platform_device *pdev)
532 605
533 twl4030_charger_enable_ac(false); 606 twl4030_charger_enable_ac(false);
534 twl4030_charger_enable_usb(bci, false); 607 twl4030_charger_enable_usb(bci, false);
608 twl4030_charger_enable_backup(0, 0);
535 609
536 /* mask interrupts */ 610 /* mask interrupts */
537 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff, 611 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
diff --git a/drivers/rtc/rtc-wm831x.c b/drivers/rtc/rtc-wm831x.c
index 59c6245e0421..ea5c6f857ca5 100644
--- a/drivers/rtc/rtc-wm831x.c
+++ b/drivers/rtc/rtc-wm831x.c
@@ -24,7 +24,7 @@
24#include <linux/mfd/wm831x/core.h> 24#include <linux/mfd/wm831x/core.h>
25#include <linux/delay.h> 25#include <linux/delay.h>
26#include <linux/platform_device.h> 26#include <linux/platform_device.h>
27 27#include <linux/random.h>
28 28
29/* 29/*
30 * R16416 (0x4020) - RTC Write Counter 30 * R16416 (0x4020) - RTC Write Counter
@@ -96,6 +96,26 @@ struct wm831x_rtc {
96 unsigned int alarm_enabled:1; 96 unsigned int alarm_enabled:1;
97}; 97};
98 98
99static void wm831x_rtc_add_randomness(struct wm831x *wm831x)
100{
101 int ret;
102 u16 reg;
103
104 /*
105 * The write counter contains a pseudo-random number which is
106 * regenerated every time we set the RTC so it should be a
107 * useful per-system source of entropy.
108 */
109 ret = wm831x_reg_read(wm831x, WM831X_RTC_WRITE_COUNTER);
110 if (ret >= 0) {
111 reg = ret;
112 add_device_randomness(&reg, sizeof(reg));
113 } else {
114 dev_warn(wm831x->dev, "Failed to read RTC write counter: %d\n",
115 ret);
116 }
117}
118
99/* 119/*
100 * Read current time and date in RTC 120 * Read current time and date in RTC
101 */ 121 */
@@ -431,6 +451,8 @@ static int wm831x_rtc_probe(struct platform_device *pdev)
431 alm_irq, ret); 451 alm_irq, ret);
432 } 452 }
433 453
454 wm831x_rtc_add_randomness(wm831x);
455
434 return 0; 456 return 0;
435 457
436err: 458err:
diff --git a/drivers/staging/media/dt3155v4l/dt3155v4l.c b/drivers/staging/media/dt3155v4l/dt3155v4l.c
index c365cdf714ea..ebe5a27c06f5 100644
--- a/drivers/staging/media/dt3155v4l/dt3155v4l.c
+++ b/drivers/staging/media/dt3155v4l/dt3155v4l.c
@@ -971,20 +971,7 @@ static struct pci_driver pci_driver = {
971 .remove = __devexit_p(dt3155_remove), 971 .remove = __devexit_p(dt3155_remove),
972}; 972};
973 973
974static int __init 974module_pci_driver(pci_driver);
975dt3155_init_module(void)
976{
977 return pci_register_driver(&pci_driver);
978}
979
980static void __exit
981dt3155_exit_module(void)
982{
983 pci_unregister_driver(&pci_driver);
984}
985
986module_init(dt3155_init_module);
987module_exit(dt3155_exit_module);
988 975
989MODULE_DESCRIPTION("video4linux pci-driver for dt3155 frame grabber"); 976MODULE_DESCRIPTION("video4linux pci-driver for dt3155 frame grabber");
990MODULE_AUTHOR("Marin Mitov <mitov@issp.bas.bg>"); 977MODULE_AUTHOR("Marin Mitov <mitov@issp.bas.bg>");
diff --git a/drivers/staging/media/easycap/easycap_main.c b/drivers/staging/media/easycap/easycap_main.c
index a1c45e4dcdce..8269c77dbf7d 100644
--- a/drivers/staging/media/easycap/easycap_main.c
+++ b/drivers/staging/media/easycap/easycap_main.c
@@ -3083,6 +3083,7 @@ static int create_video_urbs(struct easycap *peasycap)
3083 peasycap->allocation_video_urb += 1; 3083 peasycap->allocation_video_urb += 1;
3084 pdata_urb = kzalloc(sizeof(struct data_urb), GFP_KERNEL); 3084 pdata_urb = kzalloc(sizeof(struct data_urb), GFP_KERNEL);
3085 if (!pdata_urb) { 3085 if (!pdata_urb) {
3086 usb_free_urb(purb);
3086 SAM("ERROR: Could not allocate struct data_urb.\n"); 3087 SAM("ERROR: Could not allocate struct data_urb.\n");
3087 return -ENOMEM; 3088 return -ENOMEM;
3088 } 3089 }
diff --git a/drivers/staging/media/lirc/lirc_bt829.c b/drivers/staging/media/lirc/lirc_bt829.c
index 4d20e9f74118..951007a3fc96 100644
--- a/drivers/staging/media/lirc/lirc_bt829.c
+++ b/drivers/staging/media/lirc/lirc_bt829.c
@@ -171,7 +171,7 @@ static void cycle_delay(int cycle)
171} 171}
172 172
173 173
174static int poll_main() 174static int poll_main(void)
175{ 175{
176 unsigned char status_high, status_low; 176 unsigned char status_high, status_low;
177 177
diff --git a/drivers/staging/media/solo6x10/core.c b/drivers/staging/media/solo6x10/core.c
index d2fd842e37cf..3ee9b125797f 100644
--- a/drivers/staging/media/solo6x10/core.c
+++ b/drivers/staging/media/solo6x10/core.c
@@ -318,15 +318,4 @@ static struct pci_driver solo_pci_driver = {
318 .remove = solo_pci_remove, 318 .remove = solo_pci_remove,
319}; 319};
320 320
321static int __init solo_module_init(void) 321module_pci_driver(solo_pci_driver);
322{
323 return pci_register_driver(&solo_pci_driver);
324}
325
326static void __exit solo_module_exit(void)
327{
328 pci_unregister_driver(&solo_pci_driver);
329}
330
331module_init(solo_module_init);
332module_exit(solo_module_exit);
diff --git a/drivers/thermal/thermal_sys.c b/drivers/thermal/thermal_sys.c
index 2d7a9fe8f365..2ab31e4f02cc 100644
--- a/drivers/thermal/thermal_sys.c
+++ b/drivers/thermal/thermal_sys.c
@@ -1251,7 +1251,7 @@ static void remove_trip_attrs(struct thermal_zone_device *tz)
1251 * longer needed. The passive cooling formula uses tc1 and tc2 as described in 1251 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1252 * section 11.1.5.1 of the ACPI specification 3.0. 1252 * section 11.1.5.1 of the ACPI specification 3.0.
1253 */ 1253 */
1254struct thermal_zone_device *thermal_zone_device_register(char *type, 1254struct thermal_zone_device *thermal_zone_device_register(const char *type,
1255 int trips, int mask, void *devdata, 1255 int trips, int mask, void *devdata,
1256 const struct thermal_zone_device_ops *ops, 1256 const struct thermal_zone_device_ops *ops,
1257 int tc1, int tc2, int passive_delay, int polling_delay) 1257 int tc1, int tc2, int passive_delay, int polling_delay)
diff --git a/drivers/tty/serial/uartlite.c b/drivers/tty/serial/uartlite.c
index 6cd414341d5e..6579ffdd8e9b 100644
--- a/drivers/tty/serial/uartlite.c
+++ b/drivers/tty/serial/uartlite.c
@@ -216,8 +216,7 @@ static int ulite_startup(struct uart_port *port)
216{ 216{
217 int ret; 217 int ret;
218 218
219 ret = request_irq(port->irq, ulite_isr, 219 ret = request_irq(port->irq, ulite_isr, IRQF_SHARED, "uartlite", port);
220 IRQF_SHARED | IRQF_SAMPLE_RANDOM, "uartlite", port);
221 if (ret) 220 if (ret)
222 return ret; 221 return ret;
223 222
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 821126eb8176..128a804c42f4 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -25,6 +25,7 @@
25#include <linux/kthread.h> 25#include <linux/kthread.h>
26#include <linux/mutex.h> 26#include <linux/mutex.h>
27#include <linux/freezer.h> 27#include <linux/freezer.h>
28#include <linux/random.h>
28 29
29#include <asm/uaccess.h> 30#include <asm/uaccess.h>
30#include <asm/byteorder.h> 31#include <asm/byteorder.h>
@@ -2181,6 +2182,14 @@ int usb_new_device(struct usb_device *udev)
2181 /* Tell the world! */ 2182 /* Tell the world! */
2182 announce_device(udev); 2183 announce_device(udev);
2183 2184
2185 if (udev->serial)
2186 add_device_randomness(udev->serial, strlen(udev->serial));
2187 if (udev->product)
2188 add_device_randomness(udev->product, strlen(udev->product));
2189 if (udev->manufacturer)
2190 add_device_randomness(udev->manufacturer,
2191 strlen(udev->manufacturer));
2192
2184 device_enable_async_suspend(&udev->dev); 2193 device_enable_async_suspend(&udev->dev);
2185 2194
2186 /* 2195 /*
diff --git a/drivers/usb/gadget/goku_udc.c b/drivers/usb/gadget/goku_udc.c
index 3d28fb976c78..9fd7886cfa9a 100644
--- a/drivers/usb/gadget/goku_udc.c
+++ b/drivers/usb/gadget/goku_udc.c
@@ -1836,7 +1836,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1836 /* init to known state, then setup irqs */ 1836 /* init to known state, then setup irqs */
1837 udc_reset(dev); 1837 udc_reset(dev);
1838 udc_reinit (dev); 1838 udc_reinit (dev);
1839 if (request_irq(pdev->irq, goku_irq, IRQF_SHARED/*|IRQF_SAMPLE_RANDOM*/, 1839 if (request_irq(pdev->irq, goku_irq, IRQF_SHARED,
1840 driver_name, dev) != 0) { 1840 driver_name, dev) != 0) {
1841 DBG(dev, "request interrupt %d failed\n", pdev->irq); 1841 DBG(dev, "request interrupt %d failed\n", pdev->irq);
1842 retval = -EBUSY; 1842 retval = -EBUSY;
diff --git a/drivers/usb/gadget/pxa25x_udc.c b/drivers/usb/gadget/pxa25x_udc.c
index 53c093b941e5..907ad3ecb341 100644
--- a/drivers/usb/gadget/pxa25x_udc.c
+++ b/drivers/usb/gadget/pxa25x_udc.c
@@ -2201,19 +2201,15 @@ static int __init pxa25x_udc_probe(struct platform_device *pdev)
2201 2201
2202#ifdef CONFIG_ARCH_LUBBOCK 2202#ifdef CONFIG_ARCH_LUBBOCK
2203 if (machine_is_lubbock()) { 2203 if (machine_is_lubbock()) {
2204 retval = request_irq(LUBBOCK_USB_DISC_IRQ, 2204 retval = request_irq(LUBBOCK_USB_DISC_IRQ, lubbock_vbus_irq,
2205 lubbock_vbus_irq, 2205 0, driver_name, dev);
2206 IRQF_SAMPLE_RANDOM,
2207 driver_name, dev);
2208 if (retval != 0) { 2206 if (retval != 0) {
2209 pr_err("%s: can't get irq %i, err %d\n", 2207 pr_err("%s: can't get irq %i, err %d\n",
2210 driver_name, LUBBOCK_USB_DISC_IRQ, retval); 2208 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2211 goto err_irq_lub; 2209 goto err_irq_lub;
2212 } 2210 }
2213 retval = request_irq(LUBBOCK_USB_IRQ, 2211 retval = request_irq(LUBBOCK_USB_IRQ, lubbock_vbus_irq,
2214 lubbock_vbus_irq, 2212 0, driver_name, dev);
2215 IRQF_SAMPLE_RANDOM,
2216 driver_name, dev);
2217 if (retval != 0) { 2213 if (retval != 0) {
2218 pr_err("%s: can't get irq %i, err %d\n", 2214 pr_err("%s: can't get irq %i, err %d\n",
2219 driver_name, LUBBOCK_USB_IRQ, retval); 2215 driver_name, LUBBOCK_USB_IRQ, retval);
diff --git a/drivers/usb/otg/isp1301_omap.c b/drivers/usb/otg/isp1301_omap.c
index 575fc815c932..7a88667742b6 100644
--- a/drivers/usb/otg/isp1301_omap.c
+++ b/drivers/usb/otg/isp1301_omap.c
@@ -1576,7 +1576,6 @@ isp1301_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1576 isp->irq_type = IRQF_TRIGGER_FALLING; 1576 isp->irq_type = IRQF_TRIGGER_FALLING;
1577 } 1577 }
1578 1578
1579 isp->irq_type |= IRQF_SAMPLE_RANDOM;
1580 status = request_irq(i2c->irq, isp1301_irq, 1579 status = request_irq(i2c->irq, isp1301_irq,
1581 isp->irq_type, DRIVER_NAME, isp); 1580 isp->irq_type, DRIVER_NAME, isp);
1582 if (status < 0) { 1581 if (status < 0) {
diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig
new file mode 100644
index 000000000000..7cd5dec0abd1
--- /dev/null
+++ b/drivers/vfio/Kconfig
@@ -0,0 +1,16 @@
1config VFIO_IOMMU_TYPE1
2 tristate
3 depends on VFIO
4 default n
5
6menuconfig VFIO
7 tristate "VFIO Non-Privileged userspace driver framework"
8 depends on IOMMU_API
9 select VFIO_IOMMU_TYPE1 if X86
10 help
11 VFIO provides a framework for secure userspace device drivers.
12 See Documentation/vfio.txt for more details.
13
14 If you don't know what to do here, say N.
15
16source "drivers/vfio/pci/Kconfig"
diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile
new file mode 100644
index 000000000000..2398d4a0e38b
--- /dev/null
+++ b/drivers/vfio/Makefile
@@ -0,0 +1,3 @@
1obj-$(CONFIG_VFIO) += vfio.o
2obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o
3obj-$(CONFIG_VFIO_PCI) += pci/
diff --git a/drivers/vfio/pci/Kconfig b/drivers/vfio/pci/Kconfig
new file mode 100644
index 000000000000..5980758563eb
--- /dev/null
+++ b/drivers/vfio/pci/Kconfig
@@ -0,0 +1,8 @@
1config VFIO_PCI
2 tristate "VFIO support for PCI devices"
3 depends on VFIO && PCI && EVENTFD
4 help
5 Support for the PCI VFIO bus driver. This is required to make
6 use of PCI drivers using the VFIO framework.
7
8 If you don't know what to do here, say N.
diff --git a/drivers/vfio/pci/Makefile b/drivers/vfio/pci/Makefile
new file mode 100644
index 000000000000..131079255fd9
--- /dev/null
+++ b/drivers/vfio/pci/Makefile
@@ -0,0 +1,4 @@
1
2vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
3
4obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
new file mode 100644
index 000000000000..6968b7232232
--- /dev/null
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -0,0 +1,579 @@
1/*
2 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
3 * Author: Alex Williamson <alex.williamson@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Derived from original vfio:
10 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
11 * Author: Tom Lyon, pugs@cisco.com
12 */
13
14#include <linux/device.h>
15#include <linux/eventfd.h>
16#include <linux/interrupt.h>
17#include <linux/iommu.h>
18#include <linux/module.h>
19#include <linux/mutex.h>
20#include <linux/notifier.h>
21#include <linux/pci.h>
22#include <linux/pm_runtime.h>
23#include <linux/slab.h>
24#include <linux/types.h>
25#include <linux/uaccess.h>
26#include <linux/vfio.h>
27
28#include "vfio_pci_private.h"
29
30#define DRIVER_VERSION "0.2"
31#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
32#define DRIVER_DESC "VFIO PCI - User Level meta-driver"
33
34static bool nointxmask;
35module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
36MODULE_PARM_DESC(nointxmask,
37 "Disable support for PCI 2.3 style INTx masking. If this resolves problems for specific devices, report lspci -vvvxxx to linux-pci@vger.kernel.org so the device can be fixed automatically via the broken_intx_masking flag.");
38
39static int vfio_pci_enable(struct vfio_pci_device *vdev)
40{
41 struct pci_dev *pdev = vdev->pdev;
42 int ret;
43 u16 cmd;
44 u8 msix_pos;
45
46 vdev->reset_works = (pci_reset_function(pdev) == 0);
47 pci_save_state(pdev);
48 vdev->pci_saved_state = pci_store_saved_state(pdev);
49 if (!vdev->pci_saved_state)
50 pr_debug("%s: Couldn't store %s saved state\n",
51 __func__, dev_name(&pdev->dev));
52
53 ret = vfio_config_init(vdev);
54 if (ret)
55 goto out;
56
57 if (likely(!nointxmask))
58 vdev->pci_2_3 = pci_intx_mask_supported(pdev);
59
60 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
61 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
62 cmd &= ~PCI_COMMAND_INTX_DISABLE;
63 pci_write_config_word(pdev, PCI_COMMAND, cmd);
64 }
65
66 msix_pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
67 if (msix_pos) {
68 u16 flags;
69 u32 table;
70
71 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
72 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
73
74 vdev->msix_bar = table & PCI_MSIX_FLAGS_BIRMASK;
75 vdev->msix_offset = table & ~PCI_MSIX_FLAGS_BIRMASK;
76 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
77 } else
78 vdev->msix_bar = 0xFF;
79
80 ret = pci_enable_device(pdev);
81 if (ret)
82 goto out;
83
84 return ret;
85
86out:
87 kfree(vdev->pci_saved_state);
88 vdev->pci_saved_state = NULL;
89 vfio_config_free(vdev);
90 return ret;
91}
92
93static void vfio_pci_disable(struct vfio_pci_device *vdev)
94{
95 int bar;
96
97 pci_disable_device(vdev->pdev);
98
99 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
100 VFIO_IRQ_SET_ACTION_TRIGGER,
101 vdev->irq_type, 0, 0, NULL);
102
103 vdev->virq_disabled = false;
104
105 vfio_config_free(vdev);
106
107 pci_reset_function(vdev->pdev);
108
109 if (pci_load_and_free_saved_state(vdev->pdev,
110 &vdev->pci_saved_state) == 0)
111 pci_restore_state(vdev->pdev);
112 else
113 pr_info("%s: Couldn't reload %s saved state\n",
114 __func__, dev_name(&vdev->pdev->dev));
115
116 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
117 if (!vdev->barmap[bar])
118 continue;
119 pci_iounmap(vdev->pdev, vdev->barmap[bar]);
120 pci_release_selected_regions(vdev->pdev, 1 << bar);
121 vdev->barmap[bar] = NULL;
122 }
123}
124
125static void vfio_pci_release(void *device_data)
126{
127 struct vfio_pci_device *vdev = device_data;
128
129 if (atomic_dec_and_test(&vdev->refcnt))
130 vfio_pci_disable(vdev);
131
132 module_put(THIS_MODULE);
133}
134
135static int vfio_pci_open(void *device_data)
136{
137 struct vfio_pci_device *vdev = device_data;
138
139 if (!try_module_get(THIS_MODULE))
140 return -ENODEV;
141
142 if (atomic_inc_return(&vdev->refcnt) == 1) {
143 int ret = vfio_pci_enable(vdev);
144 if (ret) {
145 module_put(THIS_MODULE);
146 return ret;
147 }
148 }
149
150 return 0;
151}
152
153static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
154{
155 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
156 u8 pin;
157 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
158 if (pin)
159 return 1;
160
161 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
162 u8 pos;
163 u16 flags;
164
165 pos = pci_find_capability(vdev->pdev, PCI_CAP_ID_MSI);
166 if (pos) {
167 pci_read_config_word(vdev->pdev,
168 pos + PCI_MSI_FLAGS, &flags);
169
170 return 1 << (flags & PCI_MSI_FLAGS_QMASK);
171 }
172 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
173 u8 pos;
174 u16 flags;
175
176 pos = pci_find_capability(vdev->pdev, PCI_CAP_ID_MSIX);
177 if (pos) {
178 pci_read_config_word(vdev->pdev,
179 pos + PCI_MSIX_FLAGS, &flags);
180
181 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
182 }
183 }
184
185 return 0;
186}
187
188static long vfio_pci_ioctl(void *device_data,
189 unsigned int cmd, unsigned long arg)
190{
191 struct vfio_pci_device *vdev = device_data;
192 unsigned long minsz;
193
194 if (cmd == VFIO_DEVICE_GET_INFO) {
195 struct vfio_device_info info;
196
197 minsz = offsetofend(struct vfio_device_info, num_irqs);
198
199 if (copy_from_user(&info, (void __user *)arg, minsz))
200 return -EFAULT;
201
202 if (info.argsz < minsz)
203 return -EINVAL;
204
205 info.flags = VFIO_DEVICE_FLAGS_PCI;
206
207 if (vdev->reset_works)
208 info.flags |= VFIO_DEVICE_FLAGS_RESET;
209
210 info.num_regions = VFIO_PCI_NUM_REGIONS;
211 info.num_irqs = VFIO_PCI_NUM_IRQS;
212
213 return copy_to_user((void __user *)arg, &info, minsz);
214
215 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
216 struct pci_dev *pdev = vdev->pdev;
217 struct vfio_region_info info;
218
219 minsz = offsetofend(struct vfio_region_info, offset);
220
221 if (copy_from_user(&info, (void __user *)arg, minsz))
222 return -EFAULT;
223
224 if (info.argsz < minsz)
225 return -EINVAL;
226
227 switch (info.index) {
228 case VFIO_PCI_CONFIG_REGION_INDEX:
229 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
230 info.size = pdev->cfg_size;
231 info.flags = VFIO_REGION_INFO_FLAG_READ |
232 VFIO_REGION_INFO_FLAG_WRITE;
233 break;
234 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
235 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
236 info.size = pci_resource_len(pdev, info.index);
237 if (!info.size) {
238 info.flags = 0;
239 break;
240 }
241
242 info.flags = VFIO_REGION_INFO_FLAG_READ |
243 VFIO_REGION_INFO_FLAG_WRITE;
244 if (pci_resource_flags(pdev, info.index) &
245 IORESOURCE_MEM && info.size >= PAGE_SIZE)
246 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
247 break;
248 case VFIO_PCI_ROM_REGION_INDEX:
249 {
250 void __iomem *io;
251 size_t size;
252
253 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
254 info.flags = 0;
255
256 /* Report the BAR size, not the ROM size */
257 info.size = pci_resource_len(pdev, info.index);
258 if (!info.size)
259 break;
260
261 /* Is it really there? */
262 io = pci_map_rom(pdev, &size);
263 if (!io || !size) {
264 info.size = 0;
265 break;
266 }
267 pci_unmap_rom(pdev, io);
268
269 info.flags = VFIO_REGION_INFO_FLAG_READ;
270 break;
271 }
272 default:
273 return -EINVAL;
274 }
275
276 return copy_to_user((void __user *)arg, &info, minsz);
277
278 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
279 struct vfio_irq_info info;
280
281 minsz = offsetofend(struct vfio_irq_info, count);
282
283 if (copy_from_user(&info, (void __user *)arg, minsz))
284 return -EFAULT;
285
286 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
287 return -EINVAL;
288
289 info.flags = VFIO_IRQ_INFO_EVENTFD;
290
291 info.count = vfio_pci_get_irq_count(vdev, info.index);
292
293 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
294 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
295 VFIO_IRQ_INFO_AUTOMASKED);
296 else
297 info.flags |= VFIO_IRQ_INFO_NORESIZE;
298
299 return copy_to_user((void __user *)arg, &info, minsz);
300
301 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
302 struct vfio_irq_set hdr;
303 u8 *data = NULL;
304 int ret = 0;
305
306 minsz = offsetofend(struct vfio_irq_set, count);
307
308 if (copy_from_user(&hdr, (void __user *)arg, minsz))
309 return -EFAULT;
310
311 if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
312 hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
313 VFIO_IRQ_SET_ACTION_TYPE_MASK))
314 return -EINVAL;
315
316 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
317 size_t size;
318
319 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
320 size = sizeof(uint8_t);
321 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
322 size = sizeof(int32_t);
323 else
324 return -EINVAL;
325
326 if (hdr.argsz - minsz < hdr.count * size ||
327 hdr.count > vfio_pci_get_irq_count(vdev, hdr.index))
328 return -EINVAL;
329
330 data = kmalloc(hdr.count * size, GFP_KERNEL);
331 if (!data)
332 return -ENOMEM;
333
334 if (copy_from_user(data, (void __user *)(arg + minsz),
335 hdr.count * size)) {
336 kfree(data);
337 return -EFAULT;
338 }
339 }
340
341 mutex_lock(&vdev->igate);
342
343 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
344 hdr.start, hdr.count, data);
345
346 mutex_unlock(&vdev->igate);
347 kfree(data);
348
349 return ret;
350
351 } else if (cmd == VFIO_DEVICE_RESET)
352 return vdev->reset_works ?
353 pci_reset_function(vdev->pdev) : -EINVAL;
354
355 return -ENOTTY;
356}
357
358static ssize_t vfio_pci_read(void *device_data, char __user *buf,
359 size_t count, loff_t *ppos)
360{
361 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
362 struct vfio_pci_device *vdev = device_data;
363 struct pci_dev *pdev = vdev->pdev;
364
365 if (index >= VFIO_PCI_NUM_REGIONS)
366 return -EINVAL;
367
368 if (index == VFIO_PCI_CONFIG_REGION_INDEX)
369 return vfio_pci_config_readwrite(vdev, buf, count, ppos, false);
370 else if (index == VFIO_PCI_ROM_REGION_INDEX)
371 return vfio_pci_mem_readwrite(vdev, buf, count, ppos, false);
372 else if (pci_resource_flags(pdev, index) & IORESOURCE_IO)
373 return vfio_pci_io_readwrite(vdev, buf, count, ppos, false);
374 else if (pci_resource_flags(pdev, index) & IORESOURCE_MEM)
375 return vfio_pci_mem_readwrite(vdev, buf, count, ppos, false);
376
377 return -EINVAL;
378}
379
380static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
381 size_t count, loff_t *ppos)
382{
383 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
384 struct vfio_pci_device *vdev = device_data;
385 struct pci_dev *pdev = vdev->pdev;
386
387 if (index >= VFIO_PCI_NUM_REGIONS)
388 return -EINVAL;
389
390 if (index == VFIO_PCI_CONFIG_REGION_INDEX)
391 return vfio_pci_config_readwrite(vdev, (char __user *)buf,
392 count, ppos, true);
393 else if (index == VFIO_PCI_ROM_REGION_INDEX)
394 return -EINVAL;
395 else if (pci_resource_flags(pdev, index) & IORESOURCE_IO)
396 return vfio_pci_io_readwrite(vdev, (char __user *)buf,
397 count, ppos, true);
398 else if (pci_resource_flags(pdev, index) & IORESOURCE_MEM) {
399 return vfio_pci_mem_readwrite(vdev, (char __user *)buf,
400 count, ppos, true);
401 }
402
403 return -EINVAL;
404}
405
406static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
407{
408 struct vfio_pci_device *vdev = device_data;
409 struct pci_dev *pdev = vdev->pdev;
410 unsigned int index;
411 u64 phys_len, req_len, pgoff, req_start, phys;
412 int ret;
413
414 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
415
416 if (vma->vm_end < vma->vm_start)
417 return -EINVAL;
418 if ((vma->vm_flags & VM_SHARED) == 0)
419 return -EINVAL;
420 if (index >= VFIO_PCI_ROM_REGION_INDEX)
421 return -EINVAL;
422 if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
423 return -EINVAL;
424
425 phys_len = pci_resource_len(pdev, index);
426 req_len = vma->vm_end - vma->vm_start;
427 pgoff = vma->vm_pgoff &
428 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
429 req_start = pgoff << PAGE_SHIFT;
430
431 if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
432 return -EINVAL;
433
434 if (index == vdev->msix_bar) {
435 /*
436 * Disallow mmaps overlapping the MSI-X table; users don't
437 * get to touch this directly. We could find somewhere
438 * else to map the overlap, but page granularity is only
439 * a recommendation, not a requirement, so the user needs
440 * to know which bits are real. Requiring them to mmap
441 * around the table makes that clear.
442 */
443
444 /* If neither entirely above nor below, then it overlaps */
445 if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
446 req_start + req_len <= vdev->msix_offset))
447 return -EINVAL;
448 }
449
450 /*
451 * Even though we don't make use of the barmap for the mmap,
452 * we need to request the region and the barmap tracks that.
453 */
454 if (!vdev->barmap[index]) {
455 ret = pci_request_selected_regions(pdev,
456 1 << index, "vfio-pci");
457 if (ret)
458 return ret;
459
460 vdev->barmap[index] = pci_iomap(pdev, index, 0);
461 }
462
463 vma->vm_private_data = vdev;
464 vma->vm_flags |= (VM_IO | VM_RESERVED);
465 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
466
467 phys = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
468
469 return remap_pfn_range(vma, vma->vm_start, phys,
470 req_len, vma->vm_page_prot);
471}
472
473static const struct vfio_device_ops vfio_pci_ops = {
474 .name = "vfio-pci",
475 .open = vfio_pci_open,
476 .release = vfio_pci_release,
477 .ioctl = vfio_pci_ioctl,
478 .read = vfio_pci_read,
479 .write = vfio_pci_write,
480 .mmap = vfio_pci_mmap,
481};
482
483static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
484{
485 u8 type;
486 struct vfio_pci_device *vdev;
487 struct iommu_group *group;
488 int ret;
489
490 pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
491 if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
492 return -EINVAL;
493
494 group = iommu_group_get(&pdev->dev);
495 if (!group)
496 return -EINVAL;
497
498 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
499 if (!vdev) {
500 iommu_group_put(group);
501 return -ENOMEM;
502 }
503
504 vdev->pdev = pdev;
505 vdev->irq_type = VFIO_PCI_NUM_IRQS;
506 mutex_init(&vdev->igate);
507 spin_lock_init(&vdev->irqlock);
508 atomic_set(&vdev->refcnt, 0);
509
510 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
511 if (ret) {
512 iommu_group_put(group);
513 kfree(vdev);
514 }
515
516 return ret;
517}
518
519static void vfio_pci_remove(struct pci_dev *pdev)
520{
521 struct vfio_pci_device *vdev;
522
523 vdev = vfio_del_group_dev(&pdev->dev);
524 if (!vdev)
525 return;
526
527 iommu_group_put(pdev->dev.iommu_group);
528 kfree(vdev);
529}
530
531static struct pci_driver vfio_pci_driver = {
532 .name = "vfio-pci",
533 .id_table = NULL, /* only dynamic ids */
534 .probe = vfio_pci_probe,
535 .remove = vfio_pci_remove,
536};
537
538static void __exit vfio_pci_cleanup(void)
539{
540 pci_unregister_driver(&vfio_pci_driver);
541 vfio_pci_virqfd_exit();
542 vfio_pci_uninit_perm_bits();
543}
544
545static int __init vfio_pci_init(void)
546{
547 int ret;
548
549 /* Allocate shared config space permision data used by all devices */
550 ret = vfio_pci_init_perm_bits();
551 if (ret)
552 return ret;
553
554 /* Start the virqfd cleanup handler */
555 ret = vfio_pci_virqfd_init();
556 if (ret)
557 goto out_virqfd;
558
559 /* Register and scan for devices */
560 ret = pci_register_driver(&vfio_pci_driver);
561 if (ret)
562 goto out_driver;
563
564 return 0;
565
566out_virqfd:
567 vfio_pci_virqfd_exit();
568out_driver:
569 vfio_pci_uninit_perm_bits();
570 return ret;
571}
572
573module_init(vfio_pci_init);
574module_exit(vfio_pci_cleanup);
575
576MODULE_VERSION(DRIVER_VERSION);
577MODULE_LICENSE("GPL v2");
578MODULE_AUTHOR(DRIVER_AUTHOR);
579MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
new file mode 100644
index 000000000000..8b8f7d11e102
--- /dev/null
+++ b/drivers/vfio/pci/vfio_pci_config.c
@@ -0,0 +1,1540 @@
1/*
2 * VFIO PCI config space virtualization
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 */
15
16/*
17 * This code handles reading and writing of PCI configuration registers.
18 * This is hairy because we want to allow a lot of flexibility to the
19 * user driver, but cannot trust it with all of the config fields.
20 * Tables determine which fields can be read and written, as well as
21 * which fields are 'virtualized' - special actions and translations to
22 * make it appear to the user that he has control, when in fact things
23 * must be negotiated with the underlying OS.
24 */
25
26#include <linux/fs.h>
27#include <linux/pci.h>
28#include <linux/uaccess.h>
29#include <linux/vfio.h>
30
31#include "vfio_pci_private.h"
32
33#define PCI_CFG_SPACE_SIZE 256
34
35/* Useful "pseudo" capabilities */
36#define PCI_CAP_ID_BASIC 0
37#define PCI_CAP_ID_INVALID 0xFF
38
39#define is_bar(offset) \
40 ((offset >= PCI_BASE_ADDRESS_0 && offset < PCI_BASE_ADDRESS_5 + 4) || \
41 (offset >= PCI_ROM_ADDRESS && offset < PCI_ROM_ADDRESS + 4))
42
43/*
44 * Lengths of PCI Config Capabilities
45 * 0: Removed from the user visible capability list
46 * FF: Variable length
47 */
48static u8 pci_cap_length[] = {
49 [PCI_CAP_ID_BASIC] = PCI_STD_HEADER_SIZEOF, /* pci config header */
50 [PCI_CAP_ID_PM] = PCI_PM_SIZEOF,
51 [PCI_CAP_ID_AGP] = PCI_AGP_SIZEOF,
52 [PCI_CAP_ID_VPD] = PCI_CAP_VPD_SIZEOF,
53 [PCI_CAP_ID_SLOTID] = 0, /* bridge - don't care */
54 [PCI_CAP_ID_MSI] = 0xFF, /* 10, 14, 20, or 24 */
55 [PCI_CAP_ID_CHSWP] = 0, /* cpci - not yet */
56 [PCI_CAP_ID_PCIX] = 0xFF, /* 8 or 24 */
57 [PCI_CAP_ID_HT] = 0xFF, /* hypertransport */
58 [PCI_CAP_ID_VNDR] = 0xFF, /* variable */
59 [PCI_CAP_ID_DBG] = 0, /* debug - don't care */
60 [PCI_CAP_ID_CCRC] = 0, /* cpci - not yet */
61 [PCI_CAP_ID_SHPC] = 0, /* hotswap - not yet */
62 [PCI_CAP_ID_SSVID] = 0, /* bridge - don't care */
63 [PCI_CAP_ID_AGP3] = 0, /* AGP8x - not yet */
64 [PCI_CAP_ID_SECDEV] = 0, /* secure device not yet */
65 [PCI_CAP_ID_EXP] = 0xFF, /* 20 or 44 */
66 [PCI_CAP_ID_MSIX] = PCI_CAP_MSIX_SIZEOF,
67 [PCI_CAP_ID_SATA] = 0xFF,
68 [PCI_CAP_ID_AF] = PCI_CAP_AF_SIZEOF,
69};
70
71/*
72 * Lengths of PCIe/PCI-X Extended Config Capabilities
73 * 0: Removed or masked from the user visible capabilty list
74 * FF: Variable length
75 */
76static u16 pci_ext_cap_length[] = {
77 [PCI_EXT_CAP_ID_ERR] = PCI_ERR_ROOT_COMMAND,
78 [PCI_EXT_CAP_ID_VC] = 0xFF,
79 [PCI_EXT_CAP_ID_DSN] = PCI_EXT_CAP_DSN_SIZEOF,
80 [PCI_EXT_CAP_ID_PWR] = PCI_EXT_CAP_PWR_SIZEOF,
81 [PCI_EXT_CAP_ID_RCLD] = 0, /* root only - don't care */
82 [PCI_EXT_CAP_ID_RCILC] = 0, /* root only - don't care */
83 [PCI_EXT_CAP_ID_RCEC] = 0, /* root only - don't care */
84 [PCI_EXT_CAP_ID_MFVC] = 0xFF,
85 [PCI_EXT_CAP_ID_VC9] = 0xFF, /* same as CAP_ID_VC */
86 [PCI_EXT_CAP_ID_RCRB] = 0, /* root only - don't care */
87 [PCI_EXT_CAP_ID_VNDR] = 0xFF,
88 [PCI_EXT_CAP_ID_CAC] = 0, /* obsolete */
89 [PCI_EXT_CAP_ID_ACS] = 0xFF,
90 [PCI_EXT_CAP_ID_ARI] = PCI_EXT_CAP_ARI_SIZEOF,
91 [PCI_EXT_CAP_ID_ATS] = PCI_EXT_CAP_ATS_SIZEOF,
92 [PCI_EXT_CAP_ID_SRIOV] = PCI_EXT_CAP_SRIOV_SIZEOF,
93 [PCI_EXT_CAP_ID_MRIOV] = 0, /* not yet */
94 [PCI_EXT_CAP_ID_MCAST] = PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF,
95 [PCI_EXT_CAP_ID_PRI] = PCI_EXT_CAP_PRI_SIZEOF,
96 [PCI_EXT_CAP_ID_AMD_XXX] = 0, /* not yet */
97 [PCI_EXT_CAP_ID_REBAR] = 0xFF,
98 [PCI_EXT_CAP_ID_DPA] = 0xFF,
99 [PCI_EXT_CAP_ID_TPH] = 0xFF,
100 [PCI_EXT_CAP_ID_LTR] = PCI_EXT_CAP_LTR_SIZEOF,
101 [PCI_EXT_CAP_ID_SECPCI] = 0, /* not yet */
102 [PCI_EXT_CAP_ID_PMUX] = 0, /* not yet */
103 [PCI_EXT_CAP_ID_PASID] = 0, /* not yet */
104};
105
106/*
107 * Read/Write Permission Bits - one bit for each bit in capability
108 * Any field can be read if it exists, but what is read depends on
109 * whether the field is 'virtualized', or just pass thru to the
110 * hardware. Any virtualized field is also virtualized for writes.
111 * Writes are only permitted if they have a 1 bit here.
112 */
113struct perm_bits {
114 u8 *virt; /* read/write virtual data, not hw */
115 u8 *write; /* writeable bits */
116 int (*readfn)(struct vfio_pci_device *vdev, int pos, int count,
117 struct perm_bits *perm, int offset, __le32 *val);
118 int (*writefn)(struct vfio_pci_device *vdev, int pos, int count,
119 struct perm_bits *perm, int offset, __le32 val);
120};
121
122#define NO_VIRT 0
123#define ALL_VIRT 0xFFFFFFFFU
124#define NO_WRITE 0
125#define ALL_WRITE 0xFFFFFFFFU
126
127static int vfio_user_config_read(struct pci_dev *pdev, int offset,
128 __le32 *val, int count)
129{
130 int ret = -EINVAL;
131 u32 tmp_val = 0;
132
133 switch (count) {
134 case 1:
135 {
136 u8 tmp;
137 ret = pci_user_read_config_byte(pdev, offset, &tmp);
138 tmp_val = tmp;
139 break;
140 }
141 case 2:
142 {
143 u16 tmp;
144 ret = pci_user_read_config_word(pdev, offset, &tmp);
145 tmp_val = tmp;
146 break;
147 }
148 case 4:
149 ret = pci_user_read_config_dword(pdev, offset, &tmp_val);
150 break;
151 }
152
153 *val = cpu_to_le32(tmp_val);
154
155 return pcibios_err_to_errno(ret);
156}
157
158static int vfio_user_config_write(struct pci_dev *pdev, int offset,
159 __le32 val, int count)
160{
161 int ret = -EINVAL;
162 u32 tmp_val = le32_to_cpu(val);
163
164 switch (count) {
165 case 1:
166 ret = pci_user_write_config_byte(pdev, offset, tmp_val);
167 break;
168 case 2:
169 ret = pci_user_write_config_word(pdev, offset, tmp_val);
170 break;
171 case 4:
172 ret = pci_user_write_config_dword(pdev, offset, tmp_val);
173 break;
174 }
175
176 return pcibios_err_to_errno(ret);
177}
178
179static int vfio_default_config_read(struct vfio_pci_device *vdev, int pos,
180 int count, struct perm_bits *perm,
181 int offset, __le32 *val)
182{
183 __le32 virt = 0;
184
185 memcpy(val, vdev->vconfig + pos, count);
186
187 memcpy(&virt, perm->virt + offset, count);
188
189 /* Any non-virtualized bits? */
190 if (cpu_to_le32(~0U >> (32 - (count * 8))) != virt) {
191 struct pci_dev *pdev = vdev->pdev;
192 __le32 phys_val = 0;
193 int ret;
194
195 ret = vfio_user_config_read(pdev, pos, &phys_val, count);
196 if (ret)
197 return ret;
198
199 *val = (phys_val & ~virt) | (*val & virt);
200 }
201
202 return count;
203}
204
205static int vfio_default_config_write(struct vfio_pci_device *vdev, int pos,
206 int count, struct perm_bits *perm,
207 int offset, __le32 val)
208{
209 __le32 virt = 0, write = 0;
210
211 memcpy(&write, perm->write + offset, count);
212
213 if (!write)
214 return count; /* drop, no writable bits */
215
216 memcpy(&virt, perm->virt + offset, count);
217
218 /* Virtualized and writable bits go to vconfig */
219 if (write & virt) {
220 __le32 virt_val = 0;
221
222 memcpy(&virt_val, vdev->vconfig + pos, count);
223
224 virt_val &= ~(write & virt);
225 virt_val |= (val & (write & virt));
226
227 memcpy(vdev->vconfig + pos, &virt_val, count);
228 }
229
230 /* Non-virtualzed and writable bits go to hardware */
231 if (write & ~virt) {
232 struct pci_dev *pdev = vdev->pdev;
233 __le32 phys_val = 0;
234 int ret;
235
236 ret = vfio_user_config_read(pdev, pos, &phys_val, count);
237 if (ret)
238 return ret;
239
240 phys_val &= ~(write & ~virt);
241 phys_val |= (val & (write & ~virt));
242
243 ret = vfio_user_config_write(pdev, pos, phys_val, count);
244 if (ret)
245 return ret;
246 }
247
248 return count;
249}
250
251/* Allow direct read from hardware, except for capability next pointer */
252static int vfio_direct_config_read(struct vfio_pci_device *vdev, int pos,
253 int count, struct perm_bits *perm,
254 int offset, __le32 *val)
255{
256 int ret;
257
258 ret = vfio_user_config_read(vdev->pdev, pos, val, count);
259 if (ret)
260 return pcibios_err_to_errno(ret);
261
262 if (pos >= PCI_CFG_SPACE_SIZE) { /* Extended cap header mangling */
263 if (offset < 4)
264 memcpy(val, vdev->vconfig + pos, count);
265 } else if (pos >= PCI_STD_HEADER_SIZEOF) { /* Std cap mangling */
266 if (offset == PCI_CAP_LIST_ID && count > 1)
267 memcpy(val, vdev->vconfig + pos,
268 min(PCI_CAP_FLAGS, count));
269 else if (offset == PCI_CAP_LIST_NEXT)
270 memcpy(val, vdev->vconfig + pos, 1);
271 }
272
273 return count;
274}
275
276static int vfio_direct_config_write(struct vfio_pci_device *vdev, int pos,
277 int count, struct perm_bits *perm,
278 int offset, __le32 val)
279{
280 int ret;
281
282 ret = vfio_user_config_write(vdev->pdev, pos, val, count);
283 if (ret)
284 return ret;
285
286 return count;
287}
288
289/* Default all regions to read-only, no-virtualization */
290static struct perm_bits cap_perms[PCI_CAP_ID_MAX + 1] = {
291 [0 ... PCI_CAP_ID_MAX] = { .readfn = vfio_direct_config_read }
292};
293static struct perm_bits ecap_perms[PCI_EXT_CAP_ID_MAX + 1] = {
294 [0 ... PCI_EXT_CAP_ID_MAX] = { .readfn = vfio_direct_config_read }
295};
296
297static void free_perm_bits(struct perm_bits *perm)
298{
299 kfree(perm->virt);
300 kfree(perm->write);
301 perm->virt = NULL;
302 perm->write = NULL;
303}
304
305static int alloc_perm_bits(struct perm_bits *perm, int size)
306{
307 /*
308 * Round up all permission bits to the next dword, this lets us
309 * ignore whether a read/write exceeds the defined capability
310 * structure. We can do this because:
311 * - Standard config space is already dword aligned
312 * - Capabilities are all dword alinged (bits 0:1 of next reserved)
313 * - Express capabilities defined as dword aligned
314 */
315 size = round_up(size, 4);
316
317 /*
318 * Zero state is
319 * - All Readable, None Writeable, None Virtualized
320 */
321 perm->virt = kzalloc(size, GFP_KERNEL);
322 perm->write = kzalloc(size, GFP_KERNEL);
323 if (!perm->virt || !perm->write) {
324 free_perm_bits(perm);
325 return -ENOMEM;
326 }
327
328 perm->readfn = vfio_default_config_read;
329 perm->writefn = vfio_default_config_write;
330
331 return 0;
332}
333
334/*
335 * Helper functions for filling in permission tables
336 */
337static inline void p_setb(struct perm_bits *p, int off, u8 virt, u8 write)
338{
339 p->virt[off] = virt;
340 p->write[off] = write;
341}
342
343/* Handle endian-ness - pci and tables are little-endian */
344static inline void p_setw(struct perm_bits *p, int off, u16 virt, u16 write)
345{
346 *(__le16 *)(&p->virt[off]) = cpu_to_le16(virt);
347 *(__le16 *)(&p->write[off]) = cpu_to_le16(write);
348}
349
350/* Handle endian-ness - pci and tables are little-endian */
351static inline void p_setd(struct perm_bits *p, int off, u32 virt, u32 write)
352{
353 *(__le32 *)(&p->virt[off]) = cpu_to_le32(virt);
354 *(__le32 *)(&p->write[off]) = cpu_to_le32(write);
355}
356
357/*
358 * Restore the *real* BARs after we detect a FLR or backdoor reset.
359 * (backdoor = some device specific technique that we didn't catch)
360 */
361static void vfio_bar_restore(struct vfio_pci_device *vdev)
362{
363 struct pci_dev *pdev = vdev->pdev;
364 u32 *rbar = vdev->rbar;
365 int i;
366
367 if (pdev->is_virtfn)
368 return;
369
370 pr_info("%s: %s reset recovery - restoring bars\n",
371 __func__, dev_name(&pdev->dev));
372
373 for (i = PCI_BASE_ADDRESS_0; i <= PCI_BASE_ADDRESS_5; i += 4, rbar++)
374 pci_user_write_config_dword(pdev, i, *rbar);
375
376 pci_user_write_config_dword(pdev, PCI_ROM_ADDRESS, *rbar);
377}
378
379static __le32 vfio_generate_bar_flags(struct pci_dev *pdev, int bar)
380{
381 unsigned long flags = pci_resource_flags(pdev, bar);
382 u32 val;
383
384 if (flags & IORESOURCE_IO)
385 return cpu_to_le32(PCI_BASE_ADDRESS_SPACE_IO);
386
387 val = PCI_BASE_ADDRESS_SPACE_MEMORY;
388
389 if (flags & IORESOURCE_PREFETCH)
390 val |= PCI_BASE_ADDRESS_MEM_PREFETCH;
391
392 if (flags & IORESOURCE_MEM_64)
393 val |= PCI_BASE_ADDRESS_MEM_TYPE_64;
394
395 return cpu_to_le32(val);
396}
397
398/*
399 * Pretend we're hardware and tweak the values of the *virtual* PCI BARs
400 * to reflect the hardware capabilities. This implements BAR sizing.
401 */
402static void vfio_bar_fixup(struct vfio_pci_device *vdev)
403{
404 struct pci_dev *pdev = vdev->pdev;
405 int i;
406 __le32 *bar;
407 u64 mask;
408
409 bar = (__le32 *)&vdev->vconfig[PCI_BASE_ADDRESS_0];
410
411 for (i = PCI_STD_RESOURCES; i <= PCI_STD_RESOURCE_END; i++, bar++) {
412 if (!pci_resource_start(pdev, i)) {
413 *bar = 0; /* Unmapped by host = unimplemented to user */
414 continue;
415 }
416
417 mask = ~(pci_resource_len(pdev, i) - 1);
418
419 *bar &= cpu_to_le32((u32)mask);
420 *bar |= vfio_generate_bar_flags(pdev, i);
421
422 if (*bar & cpu_to_le32(PCI_BASE_ADDRESS_MEM_TYPE_64)) {
423 bar++;
424 *bar &= cpu_to_le32((u32)(mask >> 32));
425 i++;
426 }
427 }
428
429 bar = (__le32 *)&vdev->vconfig[PCI_ROM_ADDRESS];
430
431 /*
432 * NB. we expose the actual BAR size here, regardless of whether
433 * we can read it. When we report the REGION_INFO for the ROM
434 * we report what PCI tells us is the actual ROM size.
435 */
436 if (pci_resource_start(pdev, PCI_ROM_RESOURCE)) {
437 mask = ~(pci_resource_len(pdev, PCI_ROM_RESOURCE) - 1);
438 mask |= PCI_ROM_ADDRESS_ENABLE;
439 *bar &= cpu_to_le32((u32)mask);
440 } else
441 *bar = 0;
442
443 vdev->bardirty = false;
444}
445
446static int vfio_basic_config_read(struct vfio_pci_device *vdev, int pos,
447 int count, struct perm_bits *perm,
448 int offset, __le32 *val)
449{
450 if (is_bar(offset)) /* pos == offset for basic config */
451 vfio_bar_fixup(vdev);
452
453 count = vfio_default_config_read(vdev, pos, count, perm, offset, val);
454
455 /* Mask in virtual memory enable for SR-IOV devices */
456 if (offset == PCI_COMMAND && vdev->pdev->is_virtfn) {
457 u16 cmd = le16_to_cpu(*(__le16 *)&vdev->vconfig[PCI_COMMAND]);
458 u32 tmp_val = le32_to_cpu(*val);
459
460 tmp_val |= cmd & PCI_COMMAND_MEMORY;
461 *val = cpu_to_le32(tmp_val);
462 }
463
464 return count;
465}
466
467static int vfio_basic_config_write(struct vfio_pci_device *vdev, int pos,
468 int count, struct perm_bits *perm,
469 int offset, __le32 val)
470{
471 struct pci_dev *pdev = vdev->pdev;
472 __le16 *virt_cmd;
473 u16 new_cmd = 0;
474 int ret;
475
476 virt_cmd = (__le16 *)&vdev->vconfig[PCI_COMMAND];
477
478 if (offset == PCI_COMMAND) {
479 bool phys_mem, virt_mem, new_mem, phys_io, virt_io, new_io;
480 u16 phys_cmd;
481
482 ret = pci_user_read_config_word(pdev, PCI_COMMAND, &phys_cmd);
483 if (ret)
484 return ret;
485
486 new_cmd = le32_to_cpu(val);
487
488 phys_mem = !!(phys_cmd & PCI_COMMAND_MEMORY);
489 virt_mem = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_MEMORY);
490 new_mem = !!(new_cmd & PCI_COMMAND_MEMORY);
491
492 phys_io = !!(phys_cmd & PCI_COMMAND_IO);
493 virt_io = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_IO);
494 new_io = !!(new_cmd & PCI_COMMAND_IO);
495
496 /*
497 * If the user is writing mem/io enable (new_mem/io) and we
498 * think it's already enabled (virt_mem/io), but the hardware
499 * shows it disabled (phys_mem/io, then the device has
500 * undergone some kind of backdoor reset and needs to be
501 * restored before we allow it to enable the bars.
502 * SR-IOV devices will trigger this, but we catch them later
503 */
504 if ((new_mem && virt_mem && !phys_mem) ||
505 (new_io && virt_io && !phys_io))
506 vfio_bar_restore(vdev);
507 }
508
509 count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
510 if (count < 0)
511 return count;
512
513 /*
514 * Save current memory/io enable bits in vconfig to allow for
515 * the test above next time.
516 */
517 if (offset == PCI_COMMAND) {
518 u16 mask = PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
519
520 *virt_cmd &= cpu_to_le16(~mask);
521 *virt_cmd |= cpu_to_le16(new_cmd & mask);
522 }
523
524 /* Emulate INTx disable */
525 if (offset >= PCI_COMMAND && offset <= PCI_COMMAND + 1) {
526 bool virt_intx_disable;
527
528 virt_intx_disable = !!(le16_to_cpu(*virt_cmd) &
529 PCI_COMMAND_INTX_DISABLE);
530
531 if (virt_intx_disable && !vdev->virq_disabled) {
532 vdev->virq_disabled = true;
533 vfio_pci_intx_mask(vdev);
534 } else if (!virt_intx_disable && vdev->virq_disabled) {
535 vdev->virq_disabled = false;
536 vfio_pci_intx_unmask(vdev);
537 }
538 }
539
540 if (is_bar(offset))
541 vdev->bardirty = true;
542
543 return count;
544}
545
546/* Permissions for the Basic PCI Header */
547static int __init init_pci_cap_basic_perm(struct perm_bits *perm)
548{
549 if (alloc_perm_bits(perm, PCI_STD_HEADER_SIZEOF))
550 return -ENOMEM;
551
552 perm->readfn = vfio_basic_config_read;
553 perm->writefn = vfio_basic_config_write;
554
555 /* Virtualized for SR-IOV functions, which just have FFFF */
556 p_setw(perm, PCI_VENDOR_ID, (u16)ALL_VIRT, NO_WRITE);
557 p_setw(perm, PCI_DEVICE_ID, (u16)ALL_VIRT, NO_WRITE);
558
559 /*
560 * Virtualize INTx disable, we use it internally for interrupt
561 * control and can emulate it for non-PCI 2.3 devices.
562 */
563 p_setw(perm, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE, (u16)ALL_WRITE);
564
565 /* Virtualize capability list, we might want to skip/disable */
566 p_setw(perm, PCI_STATUS, PCI_STATUS_CAP_LIST, NO_WRITE);
567
568 /* No harm to write */
569 p_setb(perm, PCI_CACHE_LINE_SIZE, NO_VIRT, (u8)ALL_WRITE);
570 p_setb(perm, PCI_LATENCY_TIMER, NO_VIRT, (u8)ALL_WRITE);
571 p_setb(perm, PCI_BIST, NO_VIRT, (u8)ALL_WRITE);
572
573 /* Virtualize all bars, can't touch the real ones */
574 p_setd(perm, PCI_BASE_ADDRESS_0, ALL_VIRT, ALL_WRITE);
575 p_setd(perm, PCI_BASE_ADDRESS_1, ALL_VIRT, ALL_WRITE);
576 p_setd(perm, PCI_BASE_ADDRESS_2, ALL_VIRT, ALL_WRITE);
577 p_setd(perm, PCI_BASE_ADDRESS_3, ALL_VIRT, ALL_WRITE);
578 p_setd(perm, PCI_BASE_ADDRESS_4, ALL_VIRT, ALL_WRITE);
579 p_setd(perm, PCI_BASE_ADDRESS_5, ALL_VIRT, ALL_WRITE);
580 p_setd(perm, PCI_ROM_ADDRESS, ALL_VIRT, ALL_WRITE);
581
582 /* Allow us to adjust capability chain */
583 p_setb(perm, PCI_CAPABILITY_LIST, (u8)ALL_VIRT, NO_WRITE);
584
585 /* Sometimes used by sw, just virtualize */
586 p_setb(perm, PCI_INTERRUPT_LINE, (u8)ALL_VIRT, (u8)ALL_WRITE);
587 return 0;
588}
589
590/* Permissions for the Power Management capability */
591static int __init init_pci_cap_pm_perm(struct perm_bits *perm)
592{
593 if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_PM]))
594 return -ENOMEM;
595
596 /*
597 * We always virtualize the next field so we can remove
598 * capabilities from the chain if we want to.
599 */
600 p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
601
602 /*
603 * Power management is defined *per function*,
604 * so we let the user write this
605 */
606 p_setd(perm, PCI_PM_CTRL, NO_VIRT, ALL_WRITE);
607 return 0;
608}
609
610/* Permissions for PCI-X capability */
611static int __init init_pci_cap_pcix_perm(struct perm_bits *perm)
612{
613 /* Alloc 24, but only 8 are used in v0 */
614 if (alloc_perm_bits(perm, PCI_CAP_PCIX_SIZEOF_V2))
615 return -ENOMEM;
616
617 p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
618
619 p_setw(perm, PCI_X_CMD, NO_VIRT, (u16)ALL_WRITE);
620 p_setd(perm, PCI_X_ECC_CSR, NO_VIRT, ALL_WRITE);
621 return 0;
622}
623
624/* Permissions for PCI Express capability */
625static int __init init_pci_cap_exp_perm(struct perm_bits *perm)
626{
627 /* Alloc larger of two possible sizes */
628 if (alloc_perm_bits(perm, PCI_CAP_EXP_ENDPOINT_SIZEOF_V2))
629 return -ENOMEM;
630
631 p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
632
633 /*
634 * Allow writes to device control fields (includes FLR!)
635 * but not to devctl_phantom which could confuse IOMMU
636 * or to the ARI bit in devctl2 which is set at probe time
637 */
638 p_setw(perm, PCI_EXP_DEVCTL, NO_VIRT, ~PCI_EXP_DEVCTL_PHANTOM);
639 p_setw(perm, PCI_EXP_DEVCTL2, NO_VIRT, ~PCI_EXP_DEVCTL2_ARI);
640 return 0;
641}
642
643/* Permissions for Advanced Function capability */
644static int __init init_pci_cap_af_perm(struct perm_bits *perm)
645{
646 if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_AF]))
647 return -ENOMEM;
648
649 p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
650 p_setb(perm, PCI_AF_CTRL, NO_VIRT, PCI_AF_CTRL_FLR);
651 return 0;
652}
653
654/* Permissions for Advanced Error Reporting extended capability */
655static int __init init_pci_ext_cap_err_perm(struct perm_bits *perm)
656{
657 u32 mask;
658
659 if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_ERR]))
660 return -ENOMEM;
661
662 /*
663 * Virtualize the first dword of all express capabilities
664 * because it includes the next pointer. This lets us later
665 * remove capabilities from the chain if we need to.
666 */
667 p_setd(perm, 0, ALL_VIRT, NO_WRITE);
668
669 /* Writable bits mask */
670 mask = PCI_ERR_UNC_TRAIN | /* Training */
671 PCI_ERR_UNC_DLP | /* Data Link Protocol */
672 PCI_ERR_UNC_SURPDN | /* Surprise Down */
673 PCI_ERR_UNC_POISON_TLP | /* Poisoned TLP */
674 PCI_ERR_UNC_FCP | /* Flow Control Protocol */
675 PCI_ERR_UNC_COMP_TIME | /* Completion Timeout */
676 PCI_ERR_UNC_COMP_ABORT | /* Completer Abort */
677 PCI_ERR_UNC_UNX_COMP | /* Unexpected Completion */
678 PCI_ERR_UNC_RX_OVER | /* Receiver Overflow */
679 PCI_ERR_UNC_MALF_TLP | /* Malformed TLP */
680 PCI_ERR_UNC_ECRC | /* ECRC Error Status */
681 PCI_ERR_UNC_UNSUP | /* Unsupported Request */
682 PCI_ERR_UNC_ACSV | /* ACS Violation */
683 PCI_ERR_UNC_INTN | /* internal error */
684 PCI_ERR_UNC_MCBTLP | /* MC blocked TLP */
685 PCI_ERR_UNC_ATOMEG | /* Atomic egress blocked */
686 PCI_ERR_UNC_TLPPRE; /* TLP prefix blocked */
687 p_setd(perm, PCI_ERR_UNCOR_STATUS, NO_VIRT, mask);
688 p_setd(perm, PCI_ERR_UNCOR_MASK, NO_VIRT, mask);
689 p_setd(perm, PCI_ERR_UNCOR_SEVER, NO_VIRT, mask);
690
691 mask = PCI_ERR_COR_RCVR | /* Receiver Error Status */
692 PCI_ERR_COR_BAD_TLP | /* Bad TLP Status */
693 PCI_ERR_COR_BAD_DLLP | /* Bad DLLP Status */
694 PCI_ERR_COR_REP_ROLL | /* REPLAY_NUM Rollover */
695 PCI_ERR_COR_REP_TIMER | /* Replay Timer Timeout */
696 PCI_ERR_COR_ADV_NFAT | /* Advisory Non-Fatal */
697 PCI_ERR_COR_INTERNAL | /* Corrected Internal */
698 PCI_ERR_COR_LOG_OVER; /* Header Log Overflow */
699 p_setd(perm, PCI_ERR_COR_STATUS, NO_VIRT, mask);
700 p_setd(perm, PCI_ERR_COR_MASK, NO_VIRT, mask);
701
702 mask = PCI_ERR_CAP_ECRC_GENE | /* ECRC Generation Enable */
703 PCI_ERR_CAP_ECRC_CHKE; /* ECRC Check Enable */
704 p_setd(perm, PCI_ERR_CAP, NO_VIRT, mask);
705 return 0;
706}
707
708/* Permissions for Power Budgeting extended capability */
709static int __init init_pci_ext_cap_pwr_perm(struct perm_bits *perm)
710{
711 if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_PWR]))
712 return -ENOMEM;
713
714 p_setd(perm, 0, ALL_VIRT, NO_WRITE);
715
716 /* Writing the data selector is OK, the info is still read-only */
717 p_setb(perm, PCI_PWR_DATA, NO_VIRT, (u8)ALL_WRITE);
718 return 0;
719}
720
721/*
722 * Initialize the shared permission tables
723 */
724void vfio_pci_uninit_perm_bits(void)
725{
726 free_perm_bits(&cap_perms[PCI_CAP_ID_BASIC]);
727
728 free_perm_bits(&cap_perms[PCI_CAP_ID_PM]);
729 free_perm_bits(&cap_perms[PCI_CAP_ID_PCIX]);
730 free_perm_bits(&cap_perms[PCI_CAP_ID_EXP]);
731 free_perm_bits(&cap_perms[PCI_CAP_ID_AF]);
732
733 free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
734 free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
735}
736
737int __init vfio_pci_init_perm_bits(void)
738{
739 int ret;
740
741 /* Basic config space */
742 ret = init_pci_cap_basic_perm(&cap_perms[PCI_CAP_ID_BASIC]);
743
744 /* Capabilities */
745 ret |= init_pci_cap_pm_perm(&cap_perms[PCI_CAP_ID_PM]);
746 cap_perms[PCI_CAP_ID_VPD].writefn = vfio_direct_config_write;
747 ret |= init_pci_cap_pcix_perm(&cap_perms[PCI_CAP_ID_PCIX]);
748 cap_perms[PCI_CAP_ID_VNDR].writefn = vfio_direct_config_write;
749 ret |= init_pci_cap_exp_perm(&cap_perms[PCI_CAP_ID_EXP]);
750 ret |= init_pci_cap_af_perm(&cap_perms[PCI_CAP_ID_AF]);
751
752 /* Extended capabilities */
753 ret |= init_pci_ext_cap_err_perm(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
754 ret |= init_pci_ext_cap_pwr_perm(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
755 ecap_perms[PCI_EXT_CAP_ID_VNDR].writefn = vfio_direct_config_write;
756
757 if (ret)
758 vfio_pci_uninit_perm_bits();
759
760 return ret;
761}
762
763static int vfio_find_cap_start(struct vfio_pci_device *vdev, int pos)
764{
765 u8 cap;
766 int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
767 PCI_STD_HEADER_SIZEOF;
768 base /= 4;
769 pos /= 4;
770
771 cap = vdev->pci_config_map[pos];
772
773 if (cap == PCI_CAP_ID_BASIC)
774 return 0;
775
776 /* XXX Can we have to abutting capabilities of the same type? */
777 while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap)
778 pos--;
779
780 return pos * 4;
781}
782
783static int vfio_msi_config_read(struct vfio_pci_device *vdev, int pos,
784 int count, struct perm_bits *perm,
785 int offset, __le32 *val)
786{
787 /* Update max available queue size from msi_qmax */
788 if (offset <= PCI_MSI_FLAGS && offset + count >= PCI_MSI_FLAGS) {
789 __le16 *flags;
790 int start;
791
792 start = vfio_find_cap_start(vdev, pos);
793
794 flags = (__le16 *)&vdev->vconfig[start];
795
796 *flags &= cpu_to_le16(~PCI_MSI_FLAGS_QMASK);
797 *flags |= cpu_to_le16(vdev->msi_qmax << 1);
798 }
799
800 return vfio_default_config_read(vdev, pos, count, perm, offset, val);
801}
802
803static int vfio_msi_config_write(struct vfio_pci_device *vdev, int pos,
804 int count, struct perm_bits *perm,
805 int offset, __le32 val)
806{
807 count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
808 if (count < 0)
809 return count;
810
811 /* Fixup and write configured queue size and enable to hardware */
812 if (offset <= PCI_MSI_FLAGS && offset + count >= PCI_MSI_FLAGS) {
813 __le16 *pflags;
814 u16 flags;
815 int start, ret;
816
817 start = vfio_find_cap_start(vdev, pos);
818
819 pflags = (__le16 *)&vdev->vconfig[start + PCI_MSI_FLAGS];
820
821 flags = le16_to_cpu(*pflags);
822
823 /* MSI is enabled via ioctl */
824 if (!is_msi(vdev))
825 flags &= ~PCI_MSI_FLAGS_ENABLE;
826
827 /* Check queue size */
828 if ((flags & PCI_MSI_FLAGS_QSIZE) >> 4 > vdev->msi_qmax) {
829 flags &= ~PCI_MSI_FLAGS_QSIZE;
830 flags |= vdev->msi_qmax << 4;
831 }
832
833 /* Write back to virt and to hardware */
834 *pflags = cpu_to_le16(flags);
835 ret = pci_user_write_config_word(vdev->pdev,
836 start + PCI_MSI_FLAGS,
837 flags);
838 if (ret)
839 return pcibios_err_to_errno(ret);
840 }
841
842 return count;
843}
844
845/*
846 * MSI determination is per-device, so this routine gets used beyond
847 * initialization time. Don't add __init
848 */
849static int init_pci_cap_msi_perm(struct perm_bits *perm, int len, u16 flags)
850{
851 if (alloc_perm_bits(perm, len))
852 return -ENOMEM;
853
854 perm->readfn = vfio_msi_config_read;
855 perm->writefn = vfio_msi_config_write;
856
857 p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
858
859 /*
860 * The upper byte of the control register is reserved,
861 * just setup the lower byte.
862 */
863 p_setb(perm, PCI_MSI_FLAGS, (u8)ALL_VIRT, (u8)ALL_WRITE);
864 p_setd(perm, PCI_MSI_ADDRESS_LO, ALL_VIRT, ALL_WRITE);
865 if (flags & PCI_MSI_FLAGS_64BIT) {
866 p_setd(perm, PCI_MSI_ADDRESS_HI, ALL_VIRT, ALL_WRITE);
867 p_setw(perm, PCI_MSI_DATA_64, (u16)ALL_VIRT, (u16)ALL_WRITE);
868 if (flags & PCI_MSI_FLAGS_MASKBIT) {
869 p_setd(perm, PCI_MSI_MASK_64, NO_VIRT, ALL_WRITE);
870 p_setd(perm, PCI_MSI_PENDING_64, NO_VIRT, ALL_WRITE);
871 }
872 } else {
873 p_setw(perm, PCI_MSI_DATA_32, (u16)ALL_VIRT, (u16)ALL_WRITE);
874 if (flags & PCI_MSI_FLAGS_MASKBIT) {
875 p_setd(perm, PCI_MSI_MASK_32, NO_VIRT, ALL_WRITE);
876 p_setd(perm, PCI_MSI_PENDING_32, NO_VIRT, ALL_WRITE);
877 }
878 }
879 return 0;
880}
881
882/* Determine MSI CAP field length; initialize msi_perms on 1st call per vdev */
883static int vfio_msi_cap_len(struct vfio_pci_device *vdev, u8 pos)
884{
885 struct pci_dev *pdev = vdev->pdev;
886 int len, ret;
887 u16 flags;
888
889 ret = pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &flags);
890 if (ret)
891 return pcibios_err_to_errno(ret);
892
893 len = 10; /* Minimum size */
894 if (flags & PCI_MSI_FLAGS_64BIT)
895 len += 4;
896 if (flags & PCI_MSI_FLAGS_MASKBIT)
897 len += 10;
898
899 if (vdev->msi_perm)
900 return len;
901
902 vdev->msi_perm = kmalloc(sizeof(struct perm_bits), GFP_KERNEL);
903 if (!vdev->msi_perm)
904 return -ENOMEM;
905
906 ret = init_pci_cap_msi_perm(vdev->msi_perm, len, flags);
907 if (ret)
908 return ret;
909
910 return len;
911}
912
913/* Determine extended capability length for VC (2 & 9) and MFVC */
914static int vfio_vc_cap_len(struct vfio_pci_device *vdev, u16 pos)
915{
916 struct pci_dev *pdev = vdev->pdev;
917 u32 tmp;
918 int ret, evcc, phases, vc_arb;
919 int len = PCI_CAP_VC_BASE_SIZEOF;
920
921 ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_REG1, &tmp);
922 if (ret)
923 return pcibios_err_to_errno(ret);
924
925 evcc = tmp & PCI_VC_REG1_EVCC; /* extended vc count */
926 ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_REG2, &tmp);
927 if (ret)
928 return pcibios_err_to_errno(ret);
929
930 if (tmp & PCI_VC_REG2_128_PHASE)
931 phases = 128;
932 else if (tmp & PCI_VC_REG2_64_PHASE)
933 phases = 64;
934 else if (tmp & PCI_VC_REG2_32_PHASE)
935 phases = 32;
936 else
937 phases = 0;
938
939 vc_arb = phases * 4;
940
941 /*
942 * Port arbitration tables are root & switch only;
943 * function arbitration tables are function 0 only.
944 * In either case, we'll never let user write them so
945 * we don't care how big they are
946 */
947 len += (1 + evcc) * PCI_CAP_VC_PER_VC_SIZEOF;
948 if (vc_arb) {
949 len = round_up(len, 16);
950 len += vc_arb / 8;
951 }
952 return len;
953}
954
955static int vfio_cap_len(struct vfio_pci_device *vdev, u8 cap, u8 pos)
956{
957 struct pci_dev *pdev = vdev->pdev;
958 u16 word;
959 u8 byte;
960 int ret;
961
962 switch (cap) {
963 case PCI_CAP_ID_MSI:
964 return vfio_msi_cap_len(vdev, pos);
965 case PCI_CAP_ID_PCIX:
966 ret = pci_read_config_word(pdev, pos + PCI_X_CMD, &word);
967 if (ret)
968 return pcibios_err_to_errno(ret);
969
970 if (PCI_X_CMD_VERSION(word)) {
971 vdev->extended_caps = true;
972 return PCI_CAP_PCIX_SIZEOF_V2;
973 } else
974 return PCI_CAP_PCIX_SIZEOF_V0;
975 case PCI_CAP_ID_VNDR:
976 /* length follows next field */
977 ret = pci_read_config_byte(pdev, pos + PCI_CAP_FLAGS, &byte);
978 if (ret)
979 return pcibios_err_to_errno(ret);
980
981 return byte;
982 case PCI_CAP_ID_EXP:
983 /* length based on version */
984 ret = pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &word);
985 if (ret)
986 return pcibios_err_to_errno(ret);
987
988 if ((word & PCI_EXP_FLAGS_VERS) == 1)
989 return PCI_CAP_EXP_ENDPOINT_SIZEOF_V1;
990 else {
991 vdev->extended_caps = true;
992 return PCI_CAP_EXP_ENDPOINT_SIZEOF_V2;
993 }
994 case PCI_CAP_ID_HT:
995 ret = pci_read_config_byte(pdev, pos + 3, &byte);
996 if (ret)
997 return pcibios_err_to_errno(ret);
998
999 return (byte & HT_3BIT_CAP_MASK) ?
1000 HT_CAP_SIZEOF_SHORT : HT_CAP_SIZEOF_LONG;
1001 case PCI_CAP_ID_SATA:
1002 ret = pci_read_config_byte(pdev, pos + PCI_SATA_REGS, &byte);
1003 if (ret)
1004 return pcibios_err_to_errno(ret);
1005
1006 byte &= PCI_SATA_REGS_MASK;
1007 if (byte == PCI_SATA_REGS_INLINE)
1008 return PCI_SATA_SIZEOF_LONG;
1009 else
1010 return PCI_SATA_SIZEOF_SHORT;
1011 default:
1012 pr_warn("%s: %s unknown length for pci cap 0x%x@0x%x\n",
1013 dev_name(&pdev->dev), __func__, cap, pos);
1014 }
1015
1016 return 0;
1017}
1018
1019static int vfio_ext_cap_len(struct vfio_pci_device *vdev, u16 ecap, u16 epos)
1020{
1021 struct pci_dev *pdev = vdev->pdev;
1022 u8 byte;
1023 u32 dword;
1024 int ret;
1025
1026 switch (ecap) {
1027 case PCI_EXT_CAP_ID_VNDR:
1028 ret = pci_read_config_dword(pdev, epos + PCI_VSEC_HDR, &dword);
1029 if (ret)
1030 return pcibios_err_to_errno(ret);
1031
1032 return dword >> PCI_VSEC_HDR_LEN_SHIFT;
1033 case PCI_EXT_CAP_ID_VC:
1034 case PCI_EXT_CAP_ID_VC9:
1035 case PCI_EXT_CAP_ID_MFVC:
1036 return vfio_vc_cap_len(vdev, epos);
1037 case PCI_EXT_CAP_ID_ACS:
1038 ret = pci_read_config_byte(pdev, epos + PCI_ACS_CAP, &byte);
1039 if (ret)
1040 return pcibios_err_to_errno(ret);
1041
1042 if (byte & PCI_ACS_EC) {
1043 int bits;
1044
1045 ret = pci_read_config_byte(pdev,
1046 epos + PCI_ACS_EGRESS_BITS,
1047 &byte);
1048 if (ret)
1049 return pcibios_err_to_errno(ret);
1050
1051 bits = byte ? round_up(byte, 32) : 256;
1052 return 8 + (bits / 8);
1053 }
1054 return 8;
1055
1056 case PCI_EXT_CAP_ID_REBAR:
1057 ret = pci_read_config_byte(pdev, epos + PCI_REBAR_CTRL, &byte);
1058 if (ret)
1059 return pcibios_err_to_errno(ret);
1060
1061 byte &= PCI_REBAR_CTRL_NBAR_MASK;
1062 byte >>= PCI_REBAR_CTRL_NBAR_SHIFT;
1063
1064 return 4 + (byte * 8);
1065 case PCI_EXT_CAP_ID_DPA:
1066 ret = pci_read_config_byte(pdev, epos + PCI_DPA_CAP, &byte);
1067 if (ret)
1068 return pcibios_err_to_errno(ret);
1069
1070 byte &= PCI_DPA_CAP_SUBSTATE_MASK;
1071 byte = round_up(byte + 1, 4);
1072 return PCI_DPA_BASE_SIZEOF + byte;
1073 case PCI_EXT_CAP_ID_TPH:
1074 ret = pci_read_config_dword(pdev, epos + PCI_TPH_CAP, &dword);
1075 if (ret)
1076 return pcibios_err_to_errno(ret);
1077
1078 if ((dword & PCI_TPH_CAP_LOC_MASK) == PCI_TPH_LOC_CAP) {
1079 int sts;
1080
1081 sts = byte & PCI_TPH_CAP_ST_MASK;
1082 sts >>= PCI_TPH_CAP_ST_SHIFT;
1083 return PCI_TPH_BASE_SIZEOF + round_up(sts * 2, 4);
1084 }
1085 return PCI_TPH_BASE_SIZEOF;
1086 default:
1087 pr_warn("%s: %s unknown length for pci ecap 0x%x@0x%x\n",
1088 dev_name(&pdev->dev), __func__, ecap, epos);
1089 }
1090
1091 return 0;
1092}
1093
1094static int vfio_fill_vconfig_bytes(struct vfio_pci_device *vdev,
1095 int offset, int size)
1096{
1097 struct pci_dev *pdev = vdev->pdev;
1098 int ret = 0;
1099
1100 /*
1101 * We try to read physical config space in the largest chunks
1102 * we can, assuming that all of the fields support dword access.
1103 * pci_save_state() makes this same assumption and seems to do ok.
1104 */
1105 while (size) {
1106 int filled;
1107
1108 if (size >= 4 && !(offset % 4)) {
1109 __le32 *dwordp = (__le32 *)&vdev->vconfig[offset];
1110 u32 dword;
1111
1112 ret = pci_read_config_dword(pdev, offset, &dword);
1113 if (ret)
1114 return ret;
1115 *dwordp = cpu_to_le32(dword);
1116 filled = 4;
1117 } else if (size >= 2 && !(offset % 2)) {
1118 __le16 *wordp = (__le16 *)&vdev->vconfig[offset];
1119 u16 word;
1120
1121 ret = pci_read_config_word(pdev, offset, &word);
1122 if (ret)
1123 return ret;
1124 *wordp = cpu_to_le16(word);
1125 filled = 2;
1126 } else {
1127 u8 *byte = &vdev->vconfig[offset];
1128 ret = pci_read_config_byte(pdev, offset, byte);
1129 if (ret)
1130 return ret;
1131 filled = 1;
1132 }
1133
1134 offset += filled;
1135 size -= filled;
1136 }
1137
1138 return ret;
1139}
1140
1141static int vfio_cap_init(struct vfio_pci_device *vdev)
1142{
1143 struct pci_dev *pdev = vdev->pdev;
1144 u8 *map = vdev->pci_config_map;
1145 u16 status;
1146 u8 pos, *prev, cap;
1147 int loops, ret, caps = 0;
1148
1149 /* Any capabilities? */
1150 ret = pci_read_config_word(pdev, PCI_STATUS, &status);
1151 if (ret)
1152 return ret;
1153
1154 if (!(status & PCI_STATUS_CAP_LIST))
1155 return 0; /* Done */
1156
1157 ret = pci_read_config_byte(pdev, PCI_CAPABILITY_LIST, &pos);
1158 if (ret)
1159 return ret;
1160
1161 /* Mark the previous position in case we want to skip a capability */
1162 prev = &vdev->vconfig[PCI_CAPABILITY_LIST];
1163
1164 /* We can bound our loop, capabilities are dword aligned */
1165 loops = (PCI_CFG_SPACE_SIZE - PCI_STD_HEADER_SIZEOF) / PCI_CAP_SIZEOF;
1166 while (pos && loops--) {
1167 u8 next;
1168 int i, len = 0;
1169
1170 ret = pci_read_config_byte(pdev, pos, &cap);
1171 if (ret)
1172 return ret;
1173
1174 ret = pci_read_config_byte(pdev,
1175 pos + PCI_CAP_LIST_NEXT, &next);
1176 if (ret)
1177 return ret;
1178
1179 if (cap <= PCI_CAP_ID_MAX) {
1180 len = pci_cap_length[cap];
1181 if (len == 0xFF) { /* Variable length */
1182 len = vfio_cap_len(vdev, cap, pos);
1183 if (len < 0)
1184 return len;
1185 }
1186 }
1187
1188 if (!len) {
1189 pr_info("%s: %s hiding cap 0x%x\n",
1190 __func__, dev_name(&pdev->dev), cap);
1191 *prev = next;
1192 pos = next;
1193 continue;
1194 }
1195
1196 /* Sanity check, do we overlap other capabilities? */
1197 for (i = 0; i < len; i += 4) {
1198 if (likely(map[(pos + i) / 4] == PCI_CAP_ID_INVALID))
1199 continue;
1200
1201 pr_warn("%s: %s pci config conflict @0x%x, was cap 0x%x now cap 0x%x\n",
1202 __func__, dev_name(&pdev->dev),
1203 pos + i, map[pos + i], cap);
1204 }
1205
1206 memset(map + (pos / 4), cap, len / 4);
1207 ret = vfio_fill_vconfig_bytes(vdev, pos, len);
1208 if (ret)
1209 return ret;
1210
1211 prev = &vdev->vconfig[pos + PCI_CAP_LIST_NEXT];
1212 pos = next;
1213 caps++;
1214 }
1215
1216 /* If we didn't fill any capabilities, clear the status flag */
1217 if (!caps) {
1218 __le16 *vstatus = (__le16 *)&vdev->vconfig[PCI_STATUS];
1219 *vstatus &= ~cpu_to_le16(PCI_STATUS_CAP_LIST);
1220 }
1221
1222 return 0;
1223}
1224
1225static int vfio_ecap_init(struct vfio_pci_device *vdev)
1226{
1227 struct pci_dev *pdev = vdev->pdev;
1228 u8 *map = vdev->pci_config_map;
1229 u16 epos;
1230 __le32 *prev = NULL;
1231 int loops, ret, ecaps = 0;
1232
1233 if (!vdev->extended_caps)
1234 return 0;
1235
1236 epos = PCI_CFG_SPACE_SIZE;
1237
1238 loops = (pdev->cfg_size - PCI_CFG_SPACE_SIZE) / PCI_CAP_SIZEOF;
1239
1240 while (loops-- && epos >= PCI_CFG_SPACE_SIZE) {
1241 u32 header;
1242 u16 ecap;
1243 int i, len = 0;
1244 bool hidden = false;
1245
1246 ret = pci_read_config_dword(pdev, epos, &header);
1247 if (ret)
1248 return ret;
1249
1250 ecap = PCI_EXT_CAP_ID(header);
1251
1252 if (ecap <= PCI_EXT_CAP_ID_MAX) {
1253 len = pci_ext_cap_length[ecap];
1254 if (len == 0xFF) {
1255 len = vfio_ext_cap_len(vdev, ecap, epos);
1256 if (len < 0)
1257 return ret;
1258 }
1259 }
1260
1261 if (!len) {
1262 pr_info("%s: %s hiding ecap 0x%x@0x%x\n",
1263 __func__, dev_name(&pdev->dev), ecap, epos);
1264
1265 /* If not the first in the chain, we can skip over it */
1266 if (prev) {
1267 u32 val = epos = PCI_EXT_CAP_NEXT(header);
1268 *prev &= cpu_to_le32(~(0xffcU << 20));
1269 *prev |= cpu_to_le32(val << 20);
1270 continue;
1271 }
1272
1273 /*
1274 * Otherwise, fill in a placeholder, the direct
1275 * readfn will virtualize this automatically
1276 */
1277 len = PCI_CAP_SIZEOF;
1278 hidden = true;
1279 }
1280
1281 for (i = 0; i < len; i += 4) {
1282 if (likely(map[(epos + i) / 4] == PCI_CAP_ID_INVALID))
1283 continue;
1284
1285 pr_warn("%s: %s pci config conflict @0x%x, was ecap 0x%x now ecap 0x%x\n",
1286 __func__, dev_name(&pdev->dev),
1287 epos + i, map[epos + i], ecap);
1288 }
1289
1290 /*
1291 * Even though ecap is 2 bytes, we're currently a long way
1292 * from exceeding 1 byte capabilities. If we ever make it
1293 * up to 0xFF we'll need to up this to a two-byte, byte map.
1294 */
1295 BUILD_BUG_ON(PCI_EXT_CAP_ID_MAX >= PCI_CAP_ID_INVALID);
1296
1297 memset(map + (epos / 4), ecap, len / 4);
1298 ret = vfio_fill_vconfig_bytes(vdev, epos, len);
1299 if (ret)
1300 return ret;
1301
1302 /*
1303 * If we're just using this capability to anchor the list,
1304 * hide the real ID. Only count real ecaps. XXX PCI spec
1305 * indicates to use cap id = 0, version = 0, next = 0 if
1306 * ecaps are absent, hope users check all the way to next.
1307 */
1308 if (hidden)
1309 *(__le32 *)&vdev->vconfig[epos] &=
1310 cpu_to_le32((0xffcU << 20));
1311 else
1312 ecaps++;
1313
1314 prev = (__le32 *)&vdev->vconfig[epos];
1315 epos = PCI_EXT_CAP_NEXT(header);
1316 }
1317
1318 if (!ecaps)
1319 *(u32 *)&vdev->vconfig[PCI_CFG_SPACE_SIZE] = 0;
1320
1321 return 0;
1322}
1323
1324/*
1325 * For each device we allocate a pci_config_map that indicates the
1326 * capability occupying each dword and thus the struct perm_bits we
1327 * use for read and write. We also allocate a virtualized config
1328 * space which tracks reads and writes to bits that we emulate for
1329 * the user. Initial values filled from device.
1330 *
1331 * Using shared stuct perm_bits between all vfio-pci devices saves
1332 * us from allocating cfg_size buffers for virt and write for every
1333 * device. We could remove vconfig and allocate individual buffers
1334 * for each area requring emulated bits, but the array of pointers
1335 * would be comparable in size (at least for standard config space).
1336 */
1337int vfio_config_init(struct vfio_pci_device *vdev)
1338{
1339 struct pci_dev *pdev = vdev->pdev;
1340 u8 *map, *vconfig;
1341 int ret;
1342
1343 /*
1344 * Config space, caps and ecaps are all dword aligned, so we can
1345 * use one byte per dword to record the type.
1346 */
1347 map = kmalloc(pdev->cfg_size / 4, GFP_KERNEL);
1348 if (!map)
1349 return -ENOMEM;
1350
1351 vconfig = kmalloc(pdev->cfg_size, GFP_KERNEL);
1352 if (!vconfig) {
1353 kfree(map);
1354 return -ENOMEM;
1355 }
1356
1357 vdev->pci_config_map = map;
1358 vdev->vconfig = vconfig;
1359
1360 memset(map, PCI_CAP_ID_BASIC, PCI_STD_HEADER_SIZEOF / 4);
1361 memset(map + (PCI_STD_HEADER_SIZEOF / 4), PCI_CAP_ID_INVALID,
1362 (pdev->cfg_size - PCI_STD_HEADER_SIZEOF) / 4);
1363
1364 ret = vfio_fill_vconfig_bytes(vdev, 0, PCI_STD_HEADER_SIZEOF);
1365 if (ret)
1366 goto out;
1367
1368 vdev->bardirty = true;
1369
1370 /*
1371 * XXX can we just pci_load_saved_state/pci_restore_state?
1372 * may need to rebuild vconfig after that
1373 */
1374
1375 /* For restore after reset */
1376 vdev->rbar[0] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_0]);
1377 vdev->rbar[1] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_1]);
1378 vdev->rbar[2] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_2]);
1379 vdev->rbar[3] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_3]);
1380 vdev->rbar[4] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_4]);
1381 vdev->rbar[5] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_5]);
1382 vdev->rbar[6] = le32_to_cpu(*(__le32 *)&vconfig[PCI_ROM_ADDRESS]);
1383
1384 if (pdev->is_virtfn) {
1385 *(__le16 *)&vconfig[PCI_VENDOR_ID] = cpu_to_le16(pdev->vendor);
1386 *(__le16 *)&vconfig[PCI_DEVICE_ID] = cpu_to_le16(pdev->device);
1387 }
1388
1389 ret = vfio_cap_init(vdev);
1390 if (ret)
1391 goto out;
1392
1393 ret = vfio_ecap_init(vdev);
1394 if (ret)
1395 goto out;
1396
1397 return 0;
1398
1399out:
1400 kfree(map);
1401 vdev->pci_config_map = NULL;
1402 kfree(vconfig);
1403 vdev->vconfig = NULL;
1404 return pcibios_err_to_errno(ret);
1405}
1406
1407void vfio_config_free(struct vfio_pci_device *vdev)
1408{
1409 kfree(vdev->vconfig);
1410 vdev->vconfig = NULL;
1411 kfree(vdev->pci_config_map);
1412 vdev->pci_config_map = NULL;
1413 kfree(vdev->msi_perm);
1414 vdev->msi_perm = NULL;
1415}
1416
1417static ssize_t vfio_config_do_rw(struct vfio_pci_device *vdev, char __user *buf,
1418 size_t count, loff_t *ppos, bool iswrite)
1419{
1420 struct pci_dev *pdev = vdev->pdev;
1421 struct perm_bits *perm;
1422 __le32 val = 0;
1423 int cap_start = 0, offset;
1424 u8 cap_id;
1425 ssize_t ret = count;
1426
1427 if (*ppos < 0 || *ppos + count > pdev->cfg_size)
1428 return -EFAULT;
1429
1430 /*
1431 * gcc can't seem to figure out we're a static function, only called
1432 * with count of 1/2/4 and hits copy_from_user_overflow without this.
1433 */
1434 if (count > sizeof(val))
1435 return -EINVAL;
1436
1437 cap_id = vdev->pci_config_map[*ppos / 4];
1438
1439 if (cap_id == PCI_CAP_ID_INVALID) {
1440 if (iswrite)
1441 return ret; /* drop */
1442
1443 /*
1444 * Per PCI spec 3.0, section 6.1, reads from reserved and
1445 * unimplemented registers return 0
1446 */
1447 if (copy_to_user(buf, &val, count))
1448 return -EFAULT;
1449
1450 return ret;
1451 }
1452
1453 /*
1454 * All capabilities are minimum 4 bytes and aligned on dword
1455 * boundaries. Since we don't support unaligned accesses, we're
1456 * only ever accessing a single capability.
1457 */
1458 if (*ppos >= PCI_CFG_SPACE_SIZE) {
1459 WARN_ON(cap_id > PCI_EXT_CAP_ID_MAX);
1460
1461 perm = &ecap_perms[cap_id];
1462 cap_start = vfio_find_cap_start(vdev, *ppos);
1463
1464 } else {
1465 WARN_ON(cap_id > PCI_CAP_ID_MAX);
1466
1467 perm = &cap_perms[cap_id];
1468
1469 if (cap_id == PCI_CAP_ID_MSI)
1470 perm = vdev->msi_perm;
1471
1472 if (cap_id > PCI_CAP_ID_BASIC)
1473 cap_start = vfio_find_cap_start(vdev, *ppos);
1474 }
1475
1476 WARN_ON(!cap_start && cap_id != PCI_CAP_ID_BASIC);
1477 WARN_ON(cap_start > *ppos);
1478
1479 offset = *ppos - cap_start;
1480
1481 if (iswrite) {
1482 if (!perm->writefn)
1483 return ret;
1484
1485 if (copy_from_user(&val, buf, count))
1486 return -EFAULT;
1487
1488 ret = perm->writefn(vdev, *ppos, count, perm, offset, val);
1489 } else {
1490 if (perm->readfn) {
1491 ret = perm->readfn(vdev, *ppos, count,
1492 perm, offset, &val);
1493 if (ret < 0)
1494 return ret;
1495 }
1496
1497 if (copy_to_user(buf, &val, count))
1498 return -EFAULT;
1499 }
1500
1501 return ret;
1502}
1503
1504ssize_t vfio_pci_config_readwrite(struct vfio_pci_device *vdev,
1505 char __user *buf, size_t count,
1506 loff_t *ppos, bool iswrite)
1507{
1508 size_t done = 0;
1509 int ret = 0;
1510 loff_t pos = *ppos;
1511
1512 pos &= VFIO_PCI_OFFSET_MASK;
1513
1514 /*
1515 * We want to both keep the access size the caller users as well as
1516 * support reading large chunks of config space in a single call.
1517 * PCI doesn't support unaligned accesses, so we can safely break
1518 * those apart.
1519 */
1520 while (count) {
1521 if (count >= 4 && !(pos % 4))
1522 ret = vfio_config_do_rw(vdev, buf, 4, &pos, iswrite);
1523 else if (count >= 2 && !(pos % 2))
1524 ret = vfio_config_do_rw(vdev, buf, 2, &pos, iswrite);
1525 else
1526 ret = vfio_config_do_rw(vdev, buf, 1, &pos, iswrite);
1527
1528 if (ret < 0)
1529 return ret;
1530
1531 count -= ret;
1532 done += ret;
1533 buf += ret;
1534 pos += ret;
1535 }
1536
1537 *ppos += done;
1538
1539 return done;
1540}
diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_intrs.c
new file mode 100644
index 000000000000..211a4920b88a
--- /dev/null
+++ b/drivers/vfio/pci/vfio_pci_intrs.c
@@ -0,0 +1,740 @@
1/*
2 * VFIO PCI interrupt handling
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 */
15
16#include <linux/device.h>
17#include <linux/interrupt.h>
18#include <linux/eventfd.h>
19#include <linux/pci.h>
20#include <linux/file.h>
21#include <linux/poll.h>
22#include <linux/vfio.h>
23#include <linux/wait.h>
24#include <linux/workqueue.h>
25
26#include "vfio_pci_private.h"
27
28/*
29 * IRQfd - generic
30 */
31struct virqfd {
32 struct vfio_pci_device *vdev;
33 struct eventfd_ctx *eventfd;
34 int (*handler)(struct vfio_pci_device *, void *);
35 void (*thread)(struct vfio_pci_device *, void *);
36 void *data;
37 struct work_struct inject;
38 wait_queue_t wait;
39 poll_table pt;
40 struct work_struct shutdown;
41 struct virqfd **pvirqfd;
42};
43
44static struct workqueue_struct *vfio_irqfd_cleanup_wq;
45
46int __init vfio_pci_virqfd_init(void)
47{
48 vfio_irqfd_cleanup_wq =
49 create_singlethread_workqueue("vfio-irqfd-cleanup");
50 if (!vfio_irqfd_cleanup_wq)
51 return -ENOMEM;
52
53 return 0;
54}
55
56void vfio_pci_virqfd_exit(void)
57{
58 destroy_workqueue(vfio_irqfd_cleanup_wq);
59}
60
61static void virqfd_deactivate(struct virqfd *virqfd)
62{
63 queue_work(vfio_irqfd_cleanup_wq, &virqfd->shutdown);
64}
65
66static int virqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
67{
68 struct virqfd *virqfd = container_of(wait, struct virqfd, wait);
69 unsigned long flags = (unsigned long)key;
70
71 if (flags & POLLIN) {
72 /* An event has been signaled, call function */
73 if ((!virqfd->handler ||
74 virqfd->handler(virqfd->vdev, virqfd->data)) &&
75 virqfd->thread)
76 schedule_work(&virqfd->inject);
77 }
78
79 if (flags & POLLHUP)
80 /* The eventfd is closing, detach from VFIO */
81 virqfd_deactivate(virqfd);
82
83 return 0;
84}
85
86static void virqfd_ptable_queue_proc(struct file *file,
87 wait_queue_head_t *wqh, poll_table *pt)
88{
89 struct virqfd *virqfd = container_of(pt, struct virqfd, pt);
90 add_wait_queue(wqh, &virqfd->wait);
91}
92
93static void virqfd_shutdown(struct work_struct *work)
94{
95 struct virqfd *virqfd = container_of(work, struct virqfd, shutdown);
96 struct virqfd **pvirqfd = virqfd->pvirqfd;
97 u64 cnt;
98
99 eventfd_ctx_remove_wait_queue(virqfd->eventfd, &virqfd->wait, &cnt);
100 flush_work(&virqfd->inject);
101 eventfd_ctx_put(virqfd->eventfd);
102
103 kfree(virqfd);
104 *pvirqfd = NULL;
105}
106
107static void virqfd_inject(struct work_struct *work)
108{
109 struct virqfd *virqfd = container_of(work, struct virqfd, inject);
110 if (virqfd->thread)
111 virqfd->thread(virqfd->vdev, virqfd->data);
112}
113
114static int virqfd_enable(struct vfio_pci_device *vdev,
115 int (*handler)(struct vfio_pci_device *, void *),
116 void (*thread)(struct vfio_pci_device *, void *),
117 void *data, struct virqfd **pvirqfd, int fd)
118{
119 struct file *file = NULL;
120 struct eventfd_ctx *ctx = NULL;
121 struct virqfd *virqfd;
122 int ret = 0;
123 unsigned int events;
124
125 if (*pvirqfd)
126 return -EBUSY;
127
128 virqfd = kzalloc(sizeof(*virqfd), GFP_KERNEL);
129 if (!virqfd)
130 return -ENOMEM;
131
132 virqfd->pvirqfd = pvirqfd;
133 *pvirqfd = virqfd;
134 virqfd->vdev = vdev;
135 virqfd->handler = handler;
136 virqfd->thread = thread;
137 virqfd->data = data;
138
139 INIT_WORK(&virqfd->shutdown, virqfd_shutdown);
140 INIT_WORK(&virqfd->inject, virqfd_inject);
141
142 file = eventfd_fget(fd);
143 if (IS_ERR(file)) {
144 ret = PTR_ERR(file);
145 goto fail;
146 }
147
148 ctx = eventfd_ctx_fileget(file);
149 if (IS_ERR(ctx)) {
150 ret = PTR_ERR(ctx);
151 goto fail;
152 }
153
154 virqfd->eventfd = ctx;
155
156 /*
157 * Install our own custom wake-up handling so we are notified via
158 * a callback whenever someone signals the underlying eventfd.
159 */
160 init_waitqueue_func_entry(&virqfd->wait, virqfd_wakeup);
161 init_poll_funcptr(&virqfd->pt, virqfd_ptable_queue_proc);
162
163 events = file->f_op->poll(file, &virqfd->pt);
164
165 /*
166 * Check if there was an event already pending on the eventfd
167 * before we registered and trigger it as if we didn't miss it.
168 */
169 if (events & POLLIN) {
170 if ((!handler || handler(vdev, data)) && thread)
171 schedule_work(&virqfd->inject);
172 }
173
174 /*
175 * Do not drop the file until the irqfd is fully initialized,
176 * otherwise we might race against the POLLHUP.
177 */
178 fput(file);
179
180 return 0;
181
182fail:
183 if (ctx && !IS_ERR(ctx))
184 eventfd_ctx_put(ctx);
185
186 if (file && !IS_ERR(file))
187 fput(file);
188
189 kfree(virqfd);
190 *pvirqfd = NULL;
191
192 return ret;
193}
194
195static void virqfd_disable(struct virqfd *virqfd)
196{
197 if (!virqfd)
198 return;
199
200 virqfd_deactivate(virqfd);
201
202 /* Block until we know all outstanding shutdown jobs have completed. */
203 flush_workqueue(vfio_irqfd_cleanup_wq);
204}
205
206/*
207 * INTx
208 */
209static void vfio_send_intx_eventfd(struct vfio_pci_device *vdev, void *unused)
210{
211 if (likely(is_intx(vdev) && !vdev->virq_disabled))
212 eventfd_signal(vdev->ctx[0].trigger, 1);
213}
214
215void vfio_pci_intx_mask(struct vfio_pci_device *vdev)
216{
217 struct pci_dev *pdev = vdev->pdev;
218 unsigned long flags;
219
220 spin_lock_irqsave(&vdev->irqlock, flags);
221
222 /*
223 * Masking can come from interrupt, ioctl, or config space
224 * via INTx disable. The latter means this can get called
225 * even when not using intx delivery. In this case, just
226 * try to have the physical bit follow the virtual bit.
227 */
228 if (unlikely(!is_intx(vdev))) {
229 if (vdev->pci_2_3)
230 pci_intx(pdev, 0);
231 } else if (!vdev->ctx[0].masked) {
232 /*
233 * Can't use check_and_mask here because we always want to
234 * mask, not just when something is pending.
235 */
236 if (vdev->pci_2_3)
237 pci_intx(pdev, 0);
238 else
239 disable_irq_nosync(pdev->irq);
240
241 vdev->ctx[0].masked = true;
242 }
243
244 spin_unlock_irqrestore(&vdev->irqlock, flags);
245}
246
247/*
248 * If this is triggered by an eventfd, we can't call eventfd_signal
249 * or else we'll deadlock on the eventfd wait queue. Return >0 when
250 * a signal is necessary, which can then be handled via a work queue
251 * or directly depending on the caller.
252 */
253int vfio_pci_intx_unmask_handler(struct vfio_pci_device *vdev, void *unused)
254{
255 struct pci_dev *pdev = vdev->pdev;
256 unsigned long flags;
257 int ret = 0;
258
259 spin_lock_irqsave(&vdev->irqlock, flags);
260
261 /*
262 * Unmasking comes from ioctl or config, so again, have the
263 * physical bit follow the virtual even when not using INTx.
264 */
265 if (unlikely(!is_intx(vdev))) {
266 if (vdev->pci_2_3)
267 pci_intx(pdev, 1);
268 } else if (vdev->ctx[0].masked && !vdev->virq_disabled) {
269 /*
270 * A pending interrupt here would immediately trigger,
271 * but we can avoid that overhead by just re-sending
272 * the interrupt to the user.
273 */
274 if (vdev->pci_2_3) {
275 if (!pci_check_and_unmask_intx(pdev))
276 ret = 1;
277 } else
278 enable_irq(pdev->irq);
279
280 vdev->ctx[0].masked = (ret > 0);
281 }
282
283 spin_unlock_irqrestore(&vdev->irqlock, flags);
284
285 return ret;
286}
287
288void vfio_pci_intx_unmask(struct vfio_pci_device *vdev)
289{
290 if (vfio_pci_intx_unmask_handler(vdev, NULL) > 0)
291 vfio_send_intx_eventfd(vdev, NULL);
292}
293
294static irqreturn_t vfio_intx_handler(int irq, void *dev_id)
295{
296 struct vfio_pci_device *vdev = dev_id;
297 unsigned long flags;
298 int ret = IRQ_NONE;
299
300 spin_lock_irqsave(&vdev->irqlock, flags);
301
302 if (!vdev->pci_2_3) {
303 disable_irq_nosync(vdev->pdev->irq);
304 vdev->ctx[0].masked = true;
305 ret = IRQ_HANDLED;
306 } else if (!vdev->ctx[0].masked && /* may be shared */
307 pci_check_and_mask_intx(vdev->pdev)) {
308 vdev->ctx[0].masked = true;
309 ret = IRQ_HANDLED;
310 }
311
312 spin_unlock_irqrestore(&vdev->irqlock, flags);
313
314 if (ret == IRQ_HANDLED)
315 vfio_send_intx_eventfd(vdev, NULL);
316
317 return ret;
318}
319
320static int vfio_intx_enable(struct vfio_pci_device *vdev)
321{
322 if (!is_irq_none(vdev))
323 return -EINVAL;
324
325 if (!vdev->pdev->irq)
326 return -ENODEV;
327
328 vdev->ctx = kzalloc(sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
329 if (!vdev->ctx)
330 return -ENOMEM;
331
332 vdev->num_ctx = 1;
333 vdev->irq_type = VFIO_PCI_INTX_IRQ_INDEX;
334
335 return 0;
336}
337
338static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)
339{
340 struct pci_dev *pdev = vdev->pdev;
341 unsigned long irqflags = IRQF_SHARED;
342 struct eventfd_ctx *trigger;
343 unsigned long flags;
344 int ret;
345
346 if (vdev->ctx[0].trigger) {
347 free_irq(pdev->irq, vdev);
348 kfree(vdev->ctx[0].name);
349 eventfd_ctx_put(vdev->ctx[0].trigger);
350 vdev->ctx[0].trigger = NULL;
351 }
352
353 if (fd < 0) /* Disable only */
354 return 0;
355
356 vdev->ctx[0].name = kasprintf(GFP_KERNEL, "vfio-intx(%s)",
357 pci_name(pdev));
358 if (!vdev->ctx[0].name)
359 return -ENOMEM;
360
361 trigger = eventfd_ctx_fdget(fd);
362 if (IS_ERR(trigger)) {
363 kfree(vdev->ctx[0].name);
364 return PTR_ERR(trigger);
365 }
366
367 if (!vdev->pci_2_3)
368 irqflags = 0;
369
370 ret = request_irq(pdev->irq, vfio_intx_handler,
371 irqflags, vdev->ctx[0].name, vdev);
372 if (ret) {
373 kfree(vdev->ctx[0].name);
374 eventfd_ctx_put(trigger);
375 return ret;
376 }
377
378 vdev->ctx[0].trigger = trigger;
379
380 /*
381 * INTx disable will stick across the new irq setup,
382 * disable_irq won't.
383 */
384 spin_lock_irqsave(&vdev->irqlock, flags);
385 if (!vdev->pci_2_3 && (vdev->ctx[0].masked || vdev->virq_disabled))
386 disable_irq_nosync(pdev->irq);
387 spin_unlock_irqrestore(&vdev->irqlock, flags);
388
389 return 0;
390}
391
392static void vfio_intx_disable(struct vfio_pci_device *vdev)
393{
394 vfio_intx_set_signal(vdev, -1);
395 virqfd_disable(vdev->ctx[0].unmask);
396 virqfd_disable(vdev->ctx[0].mask);
397 vdev->irq_type = VFIO_PCI_NUM_IRQS;
398 vdev->num_ctx = 0;
399 kfree(vdev->ctx);
400}
401
402/*
403 * MSI/MSI-X
404 */
405static irqreturn_t vfio_msihandler(int irq, void *arg)
406{
407 struct eventfd_ctx *trigger = arg;
408
409 eventfd_signal(trigger, 1);
410 return IRQ_HANDLED;
411}
412
413static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
414{
415 struct pci_dev *pdev = vdev->pdev;
416 int ret;
417
418 if (!is_irq_none(vdev))
419 return -EINVAL;
420
421 vdev->ctx = kzalloc(nvec * sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
422 if (!vdev->ctx)
423 return -ENOMEM;
424
425 if (msix) {
426 int i;
427
428 vdev->msix = kzalloc(nvec * sizeof(struct msix_entry),
429 GFP_KERNEL);
430 if (!vdev->msix) {
431 kfree(vdev->ctx);
432 return -ENOMEM;
433 }
434
435 for (i = 0; i < nvec; i++)
436 vdev->msix[i].entry = i;
437
438 ret = pci_enable_msix(pdev, vdev->msix, nvec);
439 if (ret) {
440 kfree(vdev->msix);
441 kfree(vdev->ctx);
442 return ret;
443 }
444 } else {
445 ret = pci_enable_msi_block(pdev, nvec);
446 if (ret) {
447 kfree(vdev->ctx);
448 return ret;
449 }
450 }
451
452 vdev->num_ctx = nvec;
453 vdev->irq_type = msix ? VFIO_PCI_MSIX_IRQ_INDEX :
454 VFIO_PCI_MSI_IRQ_INDEX;
455
456 if (!msix) {
457 /*
458 * Compute the virtual hardware field for max msi vectors -
459 * it is the log base 2 of the number of vectors.
460 */
461 vdev->msi_qmax = fls(nvec * 2 - 1) - 1;
462 }
463
464 return 0;
465}
466
467static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
468 int vector, int fd, bool msix)
469{
470 struct pci_dev *pdev = vdev->pdev;
471 int irq = msix ? vdev->msix[vector].vector : pdev->irq + vector;
472 char *name = msix ? "vfio-msix" : "vfio-msi";
473 struct eventfd_ctx *trigger;
474 int ret;
475
476 if (vector >= vdev->num_ctx)
477 return -EINVAL;
478
479 if (vdev->ctx[vector].trigger) {
480 free_irq(irq, vdev->ctx[vector].trigger);
481 kfree(vdev->ctx[vector].name);
482 eventfd_ctx_put(vdev->ctx[vector].trigger);
483 vdev->ctx[vector].trigger = NULL;
484 }
485
486 if (fd < 0)
487 return 0;
488
489 vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "%s[%d](%s)",
490 name, vector, pci_name(pdev));
491 if (!vdev->ctx[vector].name)
492 return -ENOMEM;
493
494 trigger = eventfd_ctx_fdget(fd);
495 if (IS_ERR(trigger)) {
496 kfree(vdev->ctx[vector].name);
497 return PTR_ERR(trigger);
498 }
499
500 ret = request_irq(irq, vfio_msihandler, 0,
501 vdev->ctx[vector].name, trigger);
502 if (ret) {
503 kfree(vdev->ctx[vector].name);
504 eventfd_ctx_put(trigger);
505 return ret;
506 }
507
508 vdev->ctx[vector].trigger = trigger;
509
510 return 0;
511}
512
513static int vfio_msi_set_block(struct vfio_pci_device *vdev, unsigned start,
514 unsigned count, int32_t *fds, bool msix)
515{
516 int i, j, ret = 0;
517
518 if (start + count > vdev->num_ctx)
519 return -EINVAL;
520
521 for (i = 0, j = start; i < count && !ret; i++, j++) {
522 int fd = fds ? fds[i] : -1;
523 ret = vfio_msi_set_vector_signal(vdev, j, fd, msix);
524 }
525
526 if (ret) {
527 for (--j; j >= start; j--)
528 vfio_msi_set_vector_signal(vdev, j, -1, msix);
529 }
530
531 return ret;
532}
533
534static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)
535{
536 struct pci_dev *pdev = vdev->pdev;
537 int i;
538
539 vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix);
540
541 for (i = 0; i < vdev->num_ctx; i++) {
542 virqfd_disable(vdev->ctx[i].unmask);
543 virqfd_disable(vdev->ctx[i].mask);
544 }
545
546 if (msix) {
547 pci_disable_msix(vdev->pdev);
548 kfree(vdev->msix);
549 } else
550 pci_disable_msi(pdev);
551
552 vdev->irq_type = VFIO_PCI_NUM_IRQS;
553 vdev->num_ctx = 0;
554 kfree(vdev->ctx);
555}
556
557/*
558 * IOCTL support
559 */
560static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
561 unsigned index, unsigned start,
562 unsigned count, uint32_t flags, void *data)
563{
564 if (!is_intx(vdev) || start != 0 || count != 1)
565 return -EINVAL;
566
567 if (flags & VFIO_IRQ_SET_DATA_NONE) {
568 vfio_pci_intx_unmask(vdev);
569 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
570 uint8_t unmask = *(uint8_t *)data;
571 if (unmask)
572 vfio_pci_intx_unmask(vdev);
573 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
574 int32_t fd = *(int32_t *)data;
575 if (fd >= 0)
576 return virqfd_enable(vdev, vfio_pci_intx_unmask_handler,
577 vfio_send_intx_eventfd, NULL,
578 &vdev->ctx[0].unmask, fd);
579
580 virqfd_disable(vdev->ctx[0].unmask);
581 }
582
583 return 0;
584}
585
586static int vfio_pci_set_intx_mask(struct vfio_pci_device *vdev,
587 unsigned index, unsigned start,
588 unsigned count, uint32_t flags, void *data)
589{
590 if (!is_intx(vdev) || start != 0 || count != 1)
591 return -EINVAL;
592
593 if (flags & VFIO_IRQ_SET_DATA_NONE) {
594 vfio_pci_intx_mask(vdev);
595 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
596 uint8_t mask = *(uint8_t *)data;
597 if (mask)
598 vfio_pci_intx_mask(vdev);
599 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
600 return -ENOTTY; /* XXX implement me */
601 }
602
603 return 0;
604}
605
606static int vfio_pci_set_intx_trigger(struct vfio_pci_device *vdev,
607 unsigned index, unsigned start,
608 unsigned count, uint32_t flags, void *data)
609{
610 if (is_intx(vdev) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
611 vfio_intx_disable(vdev);
612 return 0;
613 }
614
615 if (!(is_intx(vdev) || is_irq_none(vdev)) || start != 0 || count != 1)
616 return -EINVAL;
617
618 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
619 int32_t fd = *(int32_t *)data;
620 int ret;
621
622 if (is_intx(vdev))
623 return vfio_intx_set_signal(vdev, fd);
624
625 ret = vfio_intx_enable(vdev);
626 if (ret)
627 return ret;
628
629 ret = vfio_intx_set_signal(vdev, fd);
630 if (ret)
631 vfio_intx_disable(vdev);
632
633 return ret;
634 }
635
636 if (!is_intx(vdev))
637 return -EINVAL;
638
639 if (flags & VFIO_IRQ_SET_DATA_NONE) {
640 vfio_send_intx_eventfd(vdev, NULL);
641 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
642 uint8_t trigger = *(uint8_t *)data;
643 if (trigger)
644 vfio_send_intx_eventfd(vdev, NULL);
645 }
646 return 0;
647}
648
649static int vfio_pci_set_msi_trigger(struct vfio_pci_device *vdev,
650 unsigned index, unsigned start,
651 unsigned count, uint32_t flags, void *data)
652{
653 int i;
654 bool msix = (index == VFIO_PCI_MSIX_IRQ_INDEX) ? true : false;
655
656 if (irq_is(vdev, index) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
657 vfio_msi_disable(vdev, msix);
658 return 0;
659 }
660
661 if (!(irq_is(vdev, index) || is_irq_none(vdev)))
662 return -EINVAL;
663
664 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
665 int32_t *fds = data;
666 int ret;
667
668 if (vdev->irq_type == index)
669 return vfio_msi_set_block(vdev, start, count,
670 fds, msix);
671
672 ret = vfio_msi_enable(vdev, start + count, msix);
673 if (ret)
674 return ret;
675
676 ret = vfio_msi_set_block(vdev, start, count, fds, msix);
677 if (ret)
678 vfio_msi_disable(vdev, msix);
679
680 return ret;
681 }
682
683 if (!irq_is(vdev, index) || start + count > vdev->num_ctx)
684 return -EINVAL;
685
686 for (i = start; i < start + count; i++) {
687 if (!vdev->ctx[i].trigger)
688 continue;
689 if (flags & VFIO_IRQ_SET_DATA_NONE) {
690 eventfd_signal(vdev->ctx[i].trigger, 1);
691 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
692 uint8_t *bools = data;
693 if (bools[i - start])
694 eventfd_signal(vdev->ctx[i].trigger, 1);
695 }
696 }
697 return 0;
698}
699
700int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev, uint32_t flags,
701 unsigned index, unsigned start, unsigned count,
702 void *data)
703{
704 int (*func)(struct vfio_pci_device *vdev, unsigned index,
705 unsigned start, unsigned count, uint32_t flags,
706 void *data) = NULL;
707
708 switch (index) {
709 case VFIO_PCI_INTX_IRQ_INDEX:
710 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
711 case VFIO_IRQ_SET_ACTION_MASK:
712 func = vfio_pci_set_intx_mask;
713 break;
714 case VFIO_IRQ_SET_ACTION_UNMASK:
715 func = vfio_pci_set_intx_unmask;
716 break;
717 case VFIO_IRQ_SET_ACTION_TRIGGER:
718 func = vfio_pci_set_intx_trigger;
719 break;
720 }
721 break;
722 case VFIO_PCI_MSI_IRQ_INDEX:
723 case VFIO_PCI_MSIX_IRQ_INDEX:
724 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
725 case VFIO_IRQ_SET_ACTION_MASK:
726 case VFIO_IRQ_SET_ACTION_UNMASK:
727 /* XXX Need masking support exported */
728 break;
729 case VFIO_IRQ_SET_ACTION_TRIGGER:
730 func = vfio_pci_set_msi_trigger;
731 break;
732 }
733 break;
734 }
735
736 if (!func)
737 return -ENOTTY;
738
739 return func(vdev, index, start, count, flags, data);
740}
diff --git a/drivers/vfio/pci/vfio_pci_private.h b/drivers/vfio/pci/vfio_pci_private.h
new file mode 100644
index 000000000000..611827cba8cd
--- /dev/null
+++ b/drivers/vfio/pci/vfio_pci_private.h
@@ -0,0 +1,91 @@
1/*
2 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
3 * Author: Alex Williamson <alex.williamson@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Derived from original vfio:
10 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
11 * Author: Tom Lyon, pugs@cisco.com
12 */
13
14#include <linux/mutex.h>
15#include <linux/pci.h>
16
17#ifndef VFIO_PCI_PRIVATE_H
18#define VFIO_PCI_PRIVATE_H
19
20#define VFIO_PCI_OFFSET_SHIFT 40
21
22#define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
23#define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
24#define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
25
26struct vfio_pci_irq_ctx {
27 struct eventfd_ctx *trigger;
28 struct virqfd *unmask;
29 struct virqfd *mask;
30 char *name;
31 bool masked;
32};
33
34struct vfio_pci_device {
35 struct pci_dev *pdev;
36 void __iomem *barmap[PCI_STD_RESOURCE_END + 1];
37 u8 *pci_config_map;
38 u8 *vconfig;
39 struct perm_bits *msi_perm;
40 spinlock_t irqlock;
41 struct mutex igate;
42 struct msix_entry *msix;
43 struct vfio_pci_irq_ctx *ctx;
44 int num_ctx;
45 int irq_type;
46 u8 msi_qmax;
47 u8 msix_bar;
48 u16 msix_size;
49 u32 msix_offset;
50 u32 rbar[7];
51 bool pci_2_3;
52 bool virq_disabled;
53 bool reset_works;
54 bool extended_caps;
55 bool bardirty;
56 struct pci_saved_state *pci_saved_state;
57 atomic_t refcnt;
58};
59
60#define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
61#define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
62#define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
63#define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
64#define irq_is(vdev, type) (vdev->irq_type == type)
65
66extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
67extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
68
69extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
70 uint32_t flags, unsigned index,
71 unsigned start, unsigned count, void *data);
72
73extern ssize_t vfio_pci_config_readwrite(struct vfio_pci_device *vdev,
74 char __user *buf, size_t count,
75 loff_t *ppos, bool iswrite);
76extern ssize_t vfio_pci_mem_readwrite(struct vfio_pci_device *vdev,
77 char __user *buf, size_t count,
78 loff_t *ppos, bool iswrite);
79extern ssize_t vfio_pci_io_readwrite(struct vfio_pci_device *vdev,
80 char __user *buf, size_t count,
81 loff_t *ppos, bool iswrite);
82
83extern int vfio_pci_init_perm_bits(void);
84extern void vfio_pci_uninit_perm_bits(void);
85
86extern int vfio_pci_virqfd_init(void);
87extern void vfio_pci_virqfd_exit(void);
88
89extern int vfio_config_init(struct vfio_pci_device *vdev);
90extern void vfio_config_free(struct vfio_pci_device *vdev);
91#endif /* VFIO_PCI_PRIVATE_H */
diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
new file mode 100644
index 000000000000..4362d9e7baa3
--- /dev/null
+++ b/drivers/vfio/pci/vfio_pci_rdwr.c
@@ -0,0 +1,269 @@
1/*
2 * VFIO PCI I/O Port & MMIO access
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 */
15
16#include <linux/fs.h>
17#include <linux/pci.h>
18#include <linux/uaccess.h>
19#include <linux/io.h>
20
21#include "vfio_pci_private.h"
22
23/* I/O Port BAR access */
24ssize_t vfio_pci_io_readwrite(struct vfio_pci_device *vdev, char __user *buf,
25 size_t count, loff_t *ppos, bool iswrite)
26{
27 struct pci_dev *pdev = vdev->pdev;
28 loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
29 int bar = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
30 void __iomem *io;
31 size_t done = 0;
32
33 if (!pci_resource_start(pdev, bar))
34 return -EINVAL;
35
36 if (pos + count > pci_resource_len(pdev, bar))
37 return -EINVAL;
38
39 if (!vdev->barmap[bar]) {
40 int ret;
41
42 ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
43 if (ret)
44 return ret;
45
46 vdev->barmap[bar] = pci_iomap(pdev, bar, 0);
47
48 if (!vdev->barmap[bar]) {
49 pci_release_selected_regions(pdev, 1 << bar);
50 return -EINVAL;
51 }
52 }
53
54 io = vdev->barmap[bar];
55
56 while (count) {
57 int filled;
58
59 if (count >= 3 && !(pos % 4)) {
60 __le32 val;
61
62 if (iswrite) {
63 if (copy_from_user(&val, buf, 4))
64 return -EFAULT;
65
66 iowrite32(le32_to_cpu(val), io + pos);
67 } else {
68 val = cpu_to_le32(ioread32(io + pos));
69
70 if (copy_to_user(buf, &val, 4))
71 return -EFAULT;
72 }
73
74 filled = 4;
75
76 } else if ((pos % 2) == 0 && count >= 2) {
77 __le16 val;
78
79 if (iswrite) {
80 if (copy_from_user(&val, buf, 2))
81 return -EFAULT;
82
83 iowrite16(le16_to_cpu(val), io + pos);
84 } else {
85 val = cpu_to_le16(ioread16(io + pos));
86
87 if (copy_to_user(buf, &val, 2))
88 return -EFAULT;
89 }
90
91 filled = 2;
92 } else {
93 u8 val;
94
95 if (iswrite) {
96 if (copy_from_user(&val, buf, 1))
97 return -EFAULT;
98
99 iowrite8(val, io + pos);
100 } else {
101 val = ioread8(io + pos);
102
103 if (copy_to_user(buf, &val, 1))
104 return -EFAULT;
105 }
106
107 filled = 1;
108 }
109
110 count -= filled;
111 done += filled;
112 buf += filled;
113 pos += filled;
114 }
115
116 *ppos += done;
117
118 return done;
119}
120
121/*
122 * MMIO BAR access
123 * We handle two excluded ranges here as well, if the user tries to read
124 * the ROM beyond what PCI tells us is available or the MSI-X table region,
125 * we return 0xFF and writes are dropped.
126 */
127ssize_t vfio_pci_mem_readwrite(struct vfio_pci_device *vdev, char __user *buf,
128 size_t count, loff_t *ppos, bool iswrite)
129{
130 struct pci_dev *pdev = vdev->pdev;
131 loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
132 int bar = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
133 void __iomem *io;
134 resource_size_t end;
135 size_t done = 0;
136 size_t x_start = 0, x_end = 0; /* excluded range */
137
138 if (!pci_resource_start(pdev, bar))
139 return -EINVAL;
140
141 end = pci_resource_len(pdev, bar);
142
143 if (pos > end)
144 return -EINVAL;
145
146 if (pos == end)
147 return 0;
148
149 if (pos + count > end)
150 count = end - pos;
151
152 if (bar == PCI_ROM_RESOURCE) {
153 io = pci_map_rom(pdev, &x_start);
154 x_end = end;
155 } else {
156 if (!vdev->barmap[bar]) {
157 int ret;
158
159 ret = pci_request_selected_regions(pdev, 1 << bar,
160 "vfio");
161 if (ret)
162 return ret;
163
164 vdev->barmap[bar] = pci_iomap(pdev, bar, 0);
165
166 if (!vdev->barmap[bar]) {
167 pci_release_selected_regions(pdev, 1 << bar);
168 return -EINVAL;
169 }
170 }
171
172 io = vdev->barmap[bar];
173
174 if (bar == vdev->msix_bar) {
175 x_start = vdev->msix_offset;
176 x_end = vdev->msix_offset + vdev->msix_size;
177 }
178 }
179
180 if (!io)
181 return -EINVAL;
182
183 while (count) {
184 size_t fillable, filled;
185
186 if (pos < x_start)
187 fillable = x_start - pos;
188 else if (pos >= x_end)
189 fillable = end - pos;
190 else
191 fillable = 0;
192
193 if (fillable >= 4 && !(pos % 4) && (count >= 4)) {
194 __le32 val;
195
196 if (iswrite) {
197 if (copy_from_user(&val, buf, 4))
198 goto out;
199
200 iowrite32(le32_to_cpu(val), io + pos);
201 } else {
202 val = cpu_to_le32(ioread32(io + pos));
203
204 if (copy_to_user(buf, &val, 4))
205 goto out;
206 }
207
208 filled = 4;
209 } else if (fillable >= 2 && !(pos % 2) && (count >= 2)) {
210 __le16 val;
211
212 if (iswrite) {
213 if (copy_from_user(&val, buf, 2))
214 goto out;
215
216 iowrite16(le16_to_cpu(val), io + pos);
217 } else {
218 val = cpu_to_le16(ioread16(io + pos));
219
220 if (copy_to_user(buf, &val, 2))
221 goto out;
222 }
223
224 filled = 2;
225 } else if (fillable) {
226 u8 val;
227
228 if (iswrite) {
229 if (copy_from_user(&val, buf, 1))
230 goto out;
231
232 iowrite8(val, io + pos);
233 } else {
234 val = ioread8(io + pos);
235
236 if (copy_to_user(buf, &val, 1))
237 goto out;
238 }
239
240 filled = 1;
241 } else {
242 /* Drop writes, fill reads with FF */
243 if (!iswrite) {
244 char val = 0xFF;
245 size_t i;
246
247 for (i = 0; i < x_end - pos; i++) {
248 if (put_user(val, buf + i))
249 goto out;
250 }
251 }
252
253 filled = x_end - pos;
254 }
255
256 count -= filled;
257 done += filled;
258 buf += filled;
259 pos += filled;
260 }
261
262 *ppos += done;
263
264out:
265 if (bar == PCI_ROM_RESOURCE)
266 pci_unmap_rom(pdev, io);
267
268 return count ? -EFAULT : done;
269}
diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
new file mode 100644
index 000000000000..9591e2b509d7
--- /dev/null
+++ b/drivers/vfio/vfio.c
@@ -0,0 +1,1420 @@
1/*
2 * VFIO core
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 */
15
16#include <linux/cdev.h>
17#include <linux/compat.h>
18#include <linux/device.h>
19#include <linux/file.h>
20#include <linux/anon_inodes.h>
21#include <linux/fs.h>
22#include <linux/idr.h>
23#include <linux/iommu.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/mutex.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/string.h>
30#include <linux/uaccess.h>
31#include <linux/vfio.h>
32#include <linux/wait.h>
33
34#define DRIVER_VERSION "0.3"
35#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
36#define DRIVER_DESC "VFIO - User Level meta-driver"
37
38static struct vfio {
39 struct class *class;
40 struct list_head iommu_drivers_list;
41 struct mutex iommu_drivers_lock;
42 struct list_head group_list;
43 struct idr group_idr;
44 struct mutex group_lock;
45 struct cdev group_cdev;
46 struct device *dev;
47 dev_t devt;
48 struct cdev cdev;
49 wait_queue_head_t release_q;
50} vfio;
51
52struct vfio_iommu_driver {
53 const struct vfio_iommu_driver_ops *ops;
54 struct list_head vfio_next;
55};
56
57struct vfio_container {
58 struct kref kref;
59 struct list_head group_list;
60 struct mutex group_lock;
61 struct vfio_iommu_driver *iommu_driver;
62 void *iommu_data;
63};
64
65struct vfio_group {
66 struct kref kref;
67 int minor;
68 atomic_t container_users;
69 struct iommu_group *iommu_group;
70 struct vfio_container *container;
71 struct list_head device_list;
72 struct mutex device_lock;
73 struct device *dev;
74 struct notifier_block nb;
75 struct list_head vfio_next;
76 struct list_head container_next;
77};
78
79struct vfio_device {
80 struct kref kref;
81 struct device *dev;
82 const struct vfio_device_ops *ops;
83 struct vfio_group *group;
84 struct list_head group_next;
85 void *device_data;
86};
87
88/**
89 * IOMMU driver registration
90 */
91int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
92{
93 struct vfio_iommu_driver *driver, *tmp;
94
95 driver = kzalloc(sizeof(*driver), GFP_KERNEL);
96 if (!driver)
97 return -ENOMEM;
98
99 driver->ops = ops;
100
101 mutex_lock(&vfio.iommu_drivers_lock);
102
103 /* Check for duplicates */
104 list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
105 if (tmp->ops == ops) {
106 mutex_unlock(&vfio.iommu_drivers_lock);
107 kfree(driver);
108 return -EINVAL;
109 }
110 }
111
112 list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
113
114 mutex_unlock(&vfio.iommu_drivers_lock);
115
116 return 0;
117}
118EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
119
120void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
121{
122 struct vfio_iommu_driver *driver;
123
124 mutex_lock(&vfio.iommu_drivers_lock);
125 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
126 if (driver->ops == ops) {
127 list_del(&driver->vfio_next);
128 mutex_unlock(&vfio.iommu_drivers_lock);
129 kfree(driver);
130 return;
131 }
132 }
133 mutex_unlock(&vfio.iommu_drivers_lock);
134}
135EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
136
137/**
138 * Group minor allocation/free - both called with vfio.group_lock held
139 */
140static int vfio_alloc_group_minor(struct vfio_group *group)
141{
142 int ret, minor;
143
144again:
145 if (unlikely(idr_pre_get(&vfio.group_idr, GFP_KERNEL) == 0))
146 return -ENOMEM;
147
148 /* index 0 is used by /dev/vfio/vfio */
149 ret = idr_get_new_above(&vfio.group_idr, group, 1, &minor);
150 if (ret == -EAGAIN)
151 goto again;
152 if (ret || minor > MINORMASK) {
153 if (minor > MINORMASK)
154 idr_remove(&vfio.group_idr, minor);
155 return -ENOSPC;
156 }
157
158 return minor;
159}
160
161static void vfio_free_group_minor(int minor)
162{
163 idr_remove(&vfio.group_idr, minor);
164}
165
166static int vfio_iommu_group_notifier(struct notifier_block *nb,
167 unsigned long action, void *data);
168static void vfio_group_get(struct vfio_group *group);
169
170/**
171 * Container objects - containers are created when /dev/vfio/vfio is
172 * opened, but their lifecycle extends until the last user is done, so
173 * it's freed via kref. Must support container/group/device being
174 * closed in any order.
175 */
176static void vfio_container_get(struct vfio_container *container)
177{
178 kref_get(&container->kref);
179}
180
181static void vfio_container_release(struct kref *kref)
182{
183 struct vfio_container *container;
184 container = container_of(kref, struct vfio_container, kref);
185
186 kfree(container);
187}
188
189static void vfio_container_put(struct vfio_container *container)
190{
191 kref_put(&container->kref, vfio_container_release);
192}
193
194/**
195 * Group objects - create, release, get, put, search
196 */
197static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
198{
199 struct vfio_group *group, *tmp;
200 struct device *dev;
201 int ret, minor;
202
203 group = kzalloc(sizeof(*group), GFP_KERNEL);
204 if (!group)
205 return ERR_PTR(-ENOMEM);
206
207 kref_init(&group->kref);
208 INIT_LIST_HEAD(&group->device_list);
209 mutex_init(&group->device_lock);
210 atomic_set(&group->container_users, 0);
211 group->iommu_group = iommu_group;
212
213 group->nb.notifier_call = vfio_iommu_group_notifier;
214
215 /*
216 * blocking notifiers acquire a rwsem around registering and hold
217 * it around callback. Therefore, need to register outside of
218 * vfio.group_lock to avoid A-B/B-A contention. Our callback won't
219 * do anything unless it can find the group in vfio.group_list, so
220 * no harm in registering early.
221 */
222 ret = iommu_group_register_notifier(iommu_group, &group->nb);
223 if (ret) {
224 kfree(group);
225 return ERR_PTR(ret);
226 }
227
228 mutex_lock(&vfio.group_lock);
229
230 minor = vfio_alloc_group_minor(group);
231 if (minor < 0) {
232 mutex_unlock(&vfio.group_lock);
233 kfree(group);
234 return ERR_PTR(minor);
235 }
236
237 /* Did we race creating this group? */
238 list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
239 if (tmp->iommu_group == iommu_group) {
240 vfio_group_get(tmp);
241 vfio_free_group_minor(minor);
242 mutex_unlock(&vfio.group_lock);
243 kfree(group);
244 return tmp;
245 }
246 }
247
248 dev = device_create(vfio.class, NULL, MKDEV(MAJOR(vfio.devt), minor),
249 group, "%d", iommu_group_id(iommu_group));
250 if (IS_ERR(dev)) {
251 vfio_free_group_minor(minor);
252 mutex_unlock(&vfio.group_lock);
253 kfree(group);
254 return (struct vfio_group *)dev; /* ERR_PTR */
255 }
256
257 group->minor = minor;
258 group->dev = dev;
259
260 list_add(&group->vfio_next, &vfio.group_list);
261
262 mutex_unlock(&vfio.group_lock);
263
264 return group;
265}
266
267static void vfio_group_release(struct kref *kref)
268{
269 struct vfio_group *group = container_of(kref, struct vfio_group, kref);
270
271 WARN_ON(!list_empty(&group->device_list));
272
273 device_destroy(vfio.class, MKDEV(MAJOR(vfio.devt), group->minor));
274 list_del(&group->vfio_next);
275 vfio_free_group_minor(group->minor);
276
277 mutex_unlock(&vfio.group_lock);
278
279 /*
280 * Unregister outside of lock. A spurious callback is harmless now
281 * that the group is no longer in vfio.group_list.
282 */
283 iommu_group_unregister_notifier(group->iommu_group, &group->nb);
284
285 kfree(group);
286}
287
288static void vfio_group_put(struct vfio_group *group)
289{
290 mutex_lock(&vfio.group_lock);
291 /*
292 * Release needs to unlock to unregister the notifier, so only
293 * unlock if not released.
294 */
295 if (!kref_put(&group->kref, vfio_group_release))
296 mutex_unlock(&vfio.group_lock);
297}
298
299/* Assume group_lock or group reference is held */
300static void vfio_group_get(struct vfio_group *group)
301{
302 kref_get(&group->kref);
303}
304
305/*
306 * Not really a try as we will sleep for mutex, but we need to make
307 * sure the group pointer is valid under lock and get a reference.
308 */
309static struct vfio_group *vfio_group_try_get(struct vfio_group *group)
310{
311 struct vfio_group *target = group;
312
313 mutex_lock(&vfio.group_lock);
314 list_for_each_entry(group, &vfio.group_list, vfio_next) {
315 if (group == target) {
316 vfio_group_get(group);
317 mutex_unlock(&vfio.group_lock);
318 return group;
319 }
320 }
321 mutex_unlock(&vfio.group_lock);
322
323 return NULL;
324}
325
326static
327struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group)
328{
329 struct vfio_group *group;
330
331 mutex_lock(&vfio.group_lock);
332 list_for_each_entry(group, &vfio.group_list, vfio_next) {
333 if (group->iommu_group == iommu_group) {
334 vfio_group_get(group);
335 mutex_unlock(&vfio.group_lock);
336 return group;
337 }
338 }
339 mutex_unlock(&vfio.group_lock);
340
341 return NULL;
342}
343
344static struct vfio_group *vfio_group_get_from_minor(int minor)
345{
346 struct vfio_group *group;
347
348 mutex_lock(&vfio.group_lock);
349 group = idr_find(&vfio.group_idr, minor);
350 if (!group) {
351 mutex_unlock(&vfio.group_lock);
352 return NULL;
353 }
354 vfio_group_get(group);
355 mutex_unlock(&vfio.group_lock);
356
357 return group;
358}
359
360/**
361 * Device objects - create, release, get, put, search
362 */
363static
364struct vfio_device *vfio_group_create_device(struct vfio_group *group,
365 struct device *dev,
366 const struct vfio_device_ops *ops,
367 void *device_data)
368{
369 struct vfio_device *device;
370 int ret;
371
372 device = kzalloc(sizeof(*device), GFP_KERNEL);
373 if (!device)
374 return ERR_PTR(-ENOMEM);
375
376 kref_init(&device->kref);
377 device->dev = dev;
378 device->group = group;
379 device->ops = ops;
380 device->device_data = device_data;
381
382 ret = dev_set_drvdata(dev, device);
383 if (ret) {
384 kfree(device);
385 return ERR_PTR(ret);
386 }
387
388 /* No need to get group_lock, caller has group reference */
389 vfio_group_get(group);
390
391 mutex_lock(&group->device_lock);
392 list_add(&device->group_next, &group->device_list);
393 mutex_unlock(&group->device_lock);
394
395 return device;
396}
397
398static void vfio_device_release(struct kref *kref)
399{
400 struct vfio_device *device = container_of(kref,
401 struct vfio_device, kref);
402 struct vfio_group *group = device->group;
403
404 mutex_lock(&group->device_lock);
405 list_del(&device->group_next);
406 mutex_unlock(&group->device_lock);
407
408 dev_set_drvdata(device->dev, NULL);
409
410 kfree(device);
411
412 /* vfio_del_group_dev may be waiting for this device */
413 wake_up(&vfio.release_q);
414}
415
416/* Device reference always implies a group reference */
417static void vfio_device_put(struct vfio_device *device)
418{
419 kref_put(&device->kref, vfio_device_release);
420 vfio_group_put(device->group);
421}
422
423static void vfio_device_get(struct vfio_device *device)
424{
425 vfio_group_get(device->group);
426 kref_get(&device->kref);
427}
428
429static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
430 struct device *dev)
431{
432 struct vfio_device *device;
433
434 mutex_lock(&group->device_lock);
435 list_for_each_entry(device, &group->device_list, group_next) {
436 if (device->dev == dev) {
437 vfio_device_get(device);
438 mutex_unlock(&group->device_lock);
439 return device;
440 }
441 }
442 mutex_unlock(&group->device_lock);
443 return NULL;
444}
445
446/*
447 * Whitelist some drivers that we know are safe (no dma) or just sit on
448 * a device. It's not always practical to leave a device within a group
449 * driverless as it could get re-bound to something unsafe.
450 */
451static const char * const vfio_driver_whitelist[] = { "pci-stub" };
452
453static bool vfio_whitelisted_driver(struct device_driver *drv)
454{
455 int i;
456
457 for (i = 0; i < ARRAY_SIZE(vfio_driver_whitelist); i++) {
458 if (!strcmp(drv->name, vfio_driver_whitelist[i]))
459 return true;
460 }
461
462 return false;
463}
464
465/*
466 * A vfio group is viable for use by userspace if all devices are either
467 * driver-less or bound to a vfio or whitelisted driver. We test the
468 * latter by the existence of a struct vfio_device matching the dev.
469 */
470static int vfio_dev_viable(struct device *dev, void *data)
471{
472 struct vfio_group *group = data;
473 struct vfio_device *device;
474
475 if (!dev->driver || vfio_whitelisted_driver(dev->driver))
476 return 0;
477
478 device = vfio_group_get_device(group, dev);
479 if (device) {
480 vfio_device_put(device);
481 return 0;
482 }
483
484 return -EINVAL;
485}
486
487/**
488 * Async device support
489 */
490static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev)
491{
492 struct vfio_device *device;
493
494 /* Do we already know about it? We shouldn't */
495 device = vfio_group_get_device(group, dev);
496 if (WARN_ON_ONCE(device)) {
497 vfio_device_put(device);
498 return 0;
499 }
500
501 /* Nothing to do for idle groups */
502 if (!atomic_read(&group->container_users))
503 return 0;
504
505 /* TODO Prevent device auto probing */
506 WARN("Device %s added to live group %d!\n", dev_name(dev),
507 iommu_group_id(group->iommu_group));
508
509 return 0;
510}
511
512static int vfio_group_nb_del_dev(struct vfio_group *group, struct device *dev)
513{
514 struct vfio_device *device;
515
516 /*
517 * Expect to fall out here. If a device was in use, it would
518 * have been bound to a vfio sub-driver, which would have blocked
519 * in .remove at vfio_del_group_dev. Sanity check that we no
520 * longer track the device, so it's safe to remove.
521 */
522 device = vfio_group_get_device(group, dev);
523 if (likely(!device))
524 return 0;
525
526 WARN("Device %s removed from live group %d!\n", dev_name(dev),
527 iommu_group_id(group->iommu_group));
528
529 vfio_device_put(device);
530 return 0;
531}
532
533static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev)
534{
535 /* We don't care what happens when the group isn't in use */
536 if (!atomic_read(&group->container_users))
537 return 0;
538
539 return vfio_dev_viable(dev, group);
540}
541
542static int vfio_iommu_group_notifier(struct notifier_block *nb,
543 unsigned long action, void *data)
544{
545 struct vfio_group *group = container_of(nb, struct vfio_group, nb);
546 struct device *dev = data;
547
548 /*
549 * Need to go through a group_lock lookup to get a reference or
550 * we risk racing a group being removed. Leave a WARN_ON for
551 * debuging, but if the group no longer exists, a spurious notify
552 * is harmless.
553 */
554 group = vfio_group_try_get(group);
555 if (WARN_ON(!group))
556 return NOTIFY_OK;
557
558 switch (action) {
559 case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
560 vfio_group_nb_add_dev(group, dev);
561 break;
562 case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
563 vfio_group_nb_del_dev(group, dev);
564 break;
565 case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
566 pr_debug("%s: Device %s, group %d binding to driver\n",
567 __func__, dev_name(dev),
568 iommu_group_id(group->iommu_group));
569 break;
570 case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
571 pr_debug("%s: Device %s, group %d bound to driver %s\n",
572 __func__, dev_name(dev),
573 iommu_group_id(group->iommu_group), dev->driver->name);
574 BUG_ON(vfio_group_nb_verify(group, dev));
575 break;
576 case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
577 pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
578 __func__, dev_name(dev),
579 iommu_group_id(group->iommu_group), dev->driver->name);
580 break;
581 case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
582 pr_debug("%s: Device %s, group %d unbound from driver\n",
583 __func__, dev_name(dev),
584 iommu_group_id(group->iommu_group));
585 /*
586 * XXX An unbound device in a live group is ok, but we'd
587 * really like to avoid the above BUG_ON by preventing other
588 * drivers from binding to it. Once that occurs, we have to
589 * stop the system to maintain isolation. At a minimum, we'd
590 * want a toggle to disable driver auto probe for this device.
591 */
592 break;
593 }
594
595 vfio_group_put(group);
596 return NOTIFY_OK;
597}
598
599/**
600 * VFIO driver API
601 */
602int vfio_add_group_dev(struct device *dev,
603 const struct vfio_device_ops *ops, void *device_data)
604{
605 struct iommu_group *iommu_group;
606 struct vfio_group *group;
607 struct vfio_device *device;
608
609 iommu_group = iommu_group_get(dev);
610 if (!iommu_group)
611 return -EINVAL;
612
613 group = vfio_group_get_from_iommu(iommu_group);
614 if (!group) {
615 group = vfio_create_group(iommu_group);
616 if (IS_ERR(group)) {
617 iommu_group_put(iommu_group);
618 return PTR_ERR(group);
619 }
620 }
621
622 device = vfio_group_get_device(group, dev);
623 if (device) {
624 WARN(1, "Device %s already exists on group %d\n",
625 dev_name(dev), iommu_group_id(iommu_group));
626 vfio_device_put(device);
627 vfio_group_put(group);
628 iommu_group_put(iommu_group);
629 return -EBUSY;
630 }
631
632 device = vfio_group_create_device(group, dev, ops, device_data);
633 if (IS_ERR(device)) {
634 vfio_group_put(group);
635 iommu_group_put(iommu_group);
636 return PTR_ERR(device);
637 }
638
639 /*
640 * Added device holds reference to iommu_group and vfio_device
641 * (which in turn holds reference to vfio_group). Drop extra
642 * group reference used while acquiring device.
643 */
644 vfio_group_put(group);
645
646 return 0;
647}
648EXPORT_SYMBOL_GPL(vfio_add_group_dev);
649
650/* Test whether a struct device is present in our tracking */
651static bool vfio_dev_present(struct device *dev)
652{
653 struct iommu_group *iommu_group;
654 struct vfio_group *group;
655 struct vfio_device *device;
656
657 iommu_group = iommu_group_get(dev);
658 if (!iommu_group)
659 return false;
660
661 group = vfio_group_get_from_iommu(iommu_group);
662 if (!group) {
663 iommu_group_put(iommu_group);
664 return false;
665 }
666
667 device = vfio_group_get_device(group, dev);
668 if (!device) {
669 vfio_group_put(group);
670 iommu_group_put(iommu_group);
671 return false;
672 }
673
674 vfio_device_put(device);
675 vfio_group_put(group);
676 iommu_group_put(iommu_group);
677 return true;
678}
679
680/*
681 * Decrement the device reference count and wait for the device to be
682 * removed. Open file descriptors for the device... */
683void *vfio_del_group_dev(struct device *dev)
684{
685 struct vfio_device *device = dev_get_drvdata(dev);
686 struct vfio_group *group = device->group;
687 struct iommu_group *iommu_group = group->iommu_group;
688 void *device_data = device->device_data;
689
690 vfio_device_put(device);
691
692 /* TODO send a signal to encourage this to be released */
693 wait_event(vfio.release_q, !vfio_dev_present(dev));
694
695 iommu_group_put(iommu_group);
696
697 return device_data;
698}
699EXPORT_SYMBOL_GPL(vfio_del_group_dev);
700
701/**
702 * VFIO base fd, /dev/vfio/vfio
703 */
704static long vfio_ioctl_check_extension(struct vfio_container *container,
705 unsigned long arg)
706{
707 struct vfio_iommu_driver *driver = container->iommu_driver;
708 long ret = 0;
709
710 switch (arg) {
711 /* No base extensions yet */
712 default:
713 /*
714 * If no driver is set, poll all registered drivers for
715 * extensions and return the first positive result. If
716 * a driver is already set, further queries will be passed
717 * only to that driver.
718 */
719 if (!driver) {
720 mutex_lock(&vfio.iommu_drivers_lock);
721 list_for_each_entry(driver, &vfio.iommu_drivers_list,
722 vfio_next) {
723 if (!try_module_get(driver->ops->owner))
724 continue;
725
726 ret = driver->ops->ioctl(NULL,
727 VFIO_CHECK_EXTENSION,
728 arg);
729 module_put(driver->ops->owner);
730 if (ret > 0)
731 break;
732 }
733 mutex_unlock(&vfio.iommu_drivers_lock);
734 } else
735 ret = driver->ops->ioctl(container->iommu_data,
736 VFIO_CHECK_EXTENSION, arg);
737 }
738
739 return ret;
740}
741
742/* hold container->group_lock */
743static int __vfio_container_attach_groups(struct vfio_container *container,
744 struct vfio_iommu_driver *driver,
745 void *data)
746{
747 struct vfio_group *group;
748 int ret = -ENODEV;
749
750 list_for_each_entry(group, &container->group_list, container_next) {
751 ret = driver->ops->attach_group(data, group->iommu_group);
752 if (ret)
753 goto unwind;
754 }
755
756 return ret;
757
758unwind:
759 list_for_each_entry_continue_reverse(group, &container->group_list,
760 container_next) {
761 driver->ops->detach_group(data, group->iommu_group);
762 }
763
764 return ret;
765}
766
767static long vfio_ioctl_set_iommu(struct vfio_container *container,
768 unsigned long arg)
769{
770 struct vfio_iommu_driver *driver;
771 long ret = -ENODEV;
772
773 mutex_lock(&container->group_lock);
774
775 /*
776 * The container is designed to be an unprivileged interface while
777 * the group can be assigned to specific users. Therefore, only by
778 * adding a group to a container does the user get the privilege of
779 * enabling the iommu, which may allocate finite resources. There
780 * is no unset_iommu, but by removing all the groups from a container,
781 * the container is deprivileged and returns to an unset state.
782 */
783 if (list_empty(&container->group_list) || container->iommu_driver) {
784 mutex_unlock(&container->group_lock);
785 return -EINVAL;
786 }
787
788 mutex_lock(&vfio.iommu_drivers_lock);
789 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
790 void *data;
791
792 if (!try_module_get(driver->ops->owner))
793 continue;
794
795 /*
796 * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
797 * so test which iommu driver reported support for this
798 * extension and call open on them. We also pass them the
799 * magic, allowing a single driver to support multiple
800 * interfaces if they'd like.
801 */
802 if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
803 module_put(driver->ops->owner);
804 continue;
805 }
806
807 /* module reference holds the driver we're working on */
808 mutex_unlock(&vfio.iommu_drivers_lock);
809
810 data = driver->ops->open(arg);
811 if (IS_ERR(data)) {
812 ret = PTR_ERR(data);
813 module_put(driver->ops->owner);
814 goto skip_drivers_unlock;
815 }
816
817 ret = __vfio_container_attach_groups(container, driver, data);
818 if (!ret) {
819 container->iommu_driver = driver;
820 container->iommu_data = data;
821 } else {
822 driver->ops->release(data);
823 module_put(driver->ops->owner);
824 }
825
826 goto skip_drivers_unlock;
827 }
828
829 mutex_unlock(&vfio.iommu_drivers_lock);
830skip_drivers_unlock:
831 mutex_unlock(&container->group_lock);
832
833 return ret;
834}
835
836static long vfio_fops_unl_ioctl(struct file *filep,
837 unsigned int cmd, unsigned long arg)
838{
839 struct vfio_container *container = filep->private_data;
840 struct vfio_iommu_driver *driver;
841 void *data;
842 long ret = -EINVAL;
843
844 if (!container)
845 return ret;
846
847 driver = container->iommu_driver;
848 data = container->iommu_data;
849
850 switch (cmd) {
851 case VFIO_GET_API_VERSION:
852 ret = VFIO_API_VERSION;
853 break;
854 case VFIO_CHECK_EXTENSION:
855 ret = vfio_ioctl_check_extension(container, arg);
856 break;
857 case VFIO_SET_IOMMU:
858 ret = vfio_ioctl_set_iommu(container, arg);
859 break;
860 default:
861 if (driver) /* passthrough all unrecognized ioctls */
862 ret = driver->ops->ioctl(data, cmd, arg);
863 }
864
865 return ret;
866}
867
868#ifdef CONFIG_COMPAT
869static long vfio_fops_compat_ioctl(struct file *filep,
870 unsigned int cmd, unsigned long arg)
871{
872 arg = (unsigned long)compat_ptr(arg);
873 return vfio_fops_unl_ioctl(filep, cmd, arg);
874}
875#endif /* CONFIG_COMPAT */
876
877static int vfio_fops_open(struct inode *inode, struct file *filep)
878{
879 struct vfio_container *container;
880
881 container = kzalloc(sizeof(*container), GFP_KERNEL);
882 if (!container)
883 return -ENOMEM;
884
885 INIT_LIST_HEAD(&container->group_list);
886 mutex_init(&container->group_lock);
887 kref_init(&container->kref);
888
889 filep->private_data = container;
890
891 return 0;
892}
893
894static int vfio_fops_release(struct inode *inode, struct file *filep)
895{
896 struct vfio_container *container = filep->private_data;
897
898 filep->private_data = NULL;
899
900 vfio_container_put(container);
901
902 return 0;
903}
904
905/*
906 * Once an iommu driver is set, we optionally pass read/write/mmap
907 * on to the driver, allowing management interfaces beyond ioctl.
908 */
909static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
910 size_t count, loff_t *ppos)
911{
912 struct vfio_container *container = filep->private_data;
913 struct vfio_iommu_driver *driver = container->iommu_driver;
914
915 if (unlikely(!driver || !driver->ops->read))
916 return -EINVAL;
917
918 return driver->ops->read(container->iommu_data, buf, count, ppos);
919}
920
921static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
922 size_t count, loff_t *ppos)
923{
924 struct vfio_container *container = filep->private_data;
925 struct vfio_iommu_driver *driver = container->iommu_driver;
926
927 if (unlikely(!driver || !driver->ops->write))
928 return -EINVAL;
929
930 return driver->ops->write(container->iommu_data, buf, count, ppos);
931}
932
933static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
934{
935 struct vfio_container *container = filep->private_data;
936 struct vfio_iommu_driver *driver = container->iommu_driver;
937
938 if (unlikely(!driver || !driver->ops->mmap))
939 return -EINVAL;
940
941 return driver->ops->mmap(container->iommu_data, vma);
942}
943
944static const struct file_operations vfio_fops = {
945 .owner = THIS_MODULE,
946 .open = vfio_fops_open,
947 .release = vfio_fops_release,
948 .read = vfio_fops_read,
949 .write = vfio_fops_write,
950 .unlocked_ioctl = vfio_fops_unl_ioctl,
951#ifdef CONFIG_COMPAT
952 .compat_ioctl = vfio_fops_compat_ioctl,
953#endif
954 .mmap = vfio_fops_mmap,
955};
956
957/**
958 * VFIO Group fd, /dev/vfio/$GROUP
959 */
960static void __vfio_group_unset_container(struct vfio_group *group)
961{
962 struct vfio_container *container = group->container;
963 struct vfio_iommu_driver *driver;
964
965 mutex_lock(&container->group_lock);
966
967 driver = container->iommu_driver;
968 if (driver)
969 driver->ops->detach_group(container->iommu_data,
970 group->iommu_group);
971
972 group->container = NULL;
973 list_del(&group->container_next);
974
975 /* Detaching the last group deprivileges a container, remove iommu */
976 if (driver && list_empty(&container->group_list)) {
977 driver->ops->release(container->iommu_data);
978 module_put(driver->ops->owner);
979 container->iommu_driver = NULL;
980 container->iommu_data = NULL;
981 }
982
983 mutex_unlock(&container->group_lock);
984
985 vfio_container_put(container);
986}
987
988/*
989 * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
990 * if there was no container to unset. Since the ioctl is called on
991 * the group, we know that still exists, therefore the only valid
992 * transition here is 1->0.
993 */
994static int vfio_group_unset_container(struct vfio_group *group)
995{
996 int users = atomic_cmpxchg(&group->container_users, 1, 0);
997
998 if (!users)
999 return -EINVAL;
1000 if (users != 1)
1001 return -EBUSY;
1002
1003 __vfio_group_unset_container(group);
1004
1005 return 0;
1006}
1007
1008/*
1009 * When removing container users, anything that removes the last user
1010 * implicitly removes the group from the container. That is, if the
1011 * group file descriptor is closed, as well as any device file descriptors,
1012 * the group is free.
1013 */
1014static void vfio_group_try_dissolve_container(struct vfio_group *group)
1015{
1016 if (0 == atomic_dec_if_positive(&group->container_users))
1017 __vfio_group_unset_container(group);
1018}
1019
1020static int vfio_group_set_container(struct vfio_group *group, int container_fd)
1021{
1022 struct file *filep;
1023 struct vfio_container *container;
1024 struct vfio_iommu_driver *driver;
1025 int ret = 0;
1026
1027 if (atomic_read(&group->container_users))
1028 return -EINVAL;
1029
1030 filep = fget(container_fd);
1031 if (!filep)
1032 return -EBADF;
1033
1034 /* Sanity check, is this really our fd? */
1035 if (filep->f_op != &vfio_fops) {
1036 fput(filep);
1037 return -EINVAL;
1038 }
1039
1040 container = filep->private_data;
1041 WARN_ON(!container); /* fget ensures we don't race vfio_release */
1042
1043 mutex_lock(&container->group_lock);
1044
1045 driver = container->iommu_driver;
1046 if (driver) {
1047 ret = driver->ops->attach_group(container->iommu_data,
1048 group->iommu_group);
1049 if (ret)
1050 goto unlock_out;
1051 }
1052
1053 group->container = container;
1054 list_add(&group->container_next, &container->group_list);
1055
1056 /* Get a reference on the container and mark a user within the group */
1057 vfio_container_get(container);
1058 atomic_inc(&group->container_users);
1059
1060unlock_out:
1061 mutex_unlock(&container->group_lock);
1062 fput(filep);
1063
1064 return ret;
1065}
1066
1067static bool vfio_group_viable(struct vfio_group *group)
1068{
1069 return (iommu_group_for_each_dev(group->iommu_group,
1070 group, vfio_dev_viable) == 0);
1071}
1072
1073static const struct file_operations vfio_device_fops;
1074
1075static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
1076{
1077 struct vfio_device *device;
1078 struct file *filep;
1079 int ret = -ENODEV;
1080
1081 if (0 == atomic_read(&group->container_users) ||
1082 !group->container->iommu_driver || !vfio_group_viable(group))
1083 return -EINVAL;
1084
1085 mutex_lock(&group->device_lock);
1086 list_for_each_entry(device, &group->device_list, group_next) {
1087 if (strcmp(dev_name(device->dev), buf))
1088 continue;
1089
1090 ret = device->ops->open(device->device_data);
1091 if (ret)
1092 break;
1093 /*
1094 * We can't use anon_inode_getfd() because we need to modify
1095 * the f_mode flags directly to allow more than just ioctls
1096 */
1097 ret = get_unused_fd();
1098 if (ret < 0) {
1099 device->ops->release(device->device_data);
1100 break;
1101 }
1102
1103 filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
1104 device, O_RDWR);
1105 if (IS_ERR(filep)) {
1106 put_unused_fd(ret);
1107 ret = PTR_ERR(filep);
1108 device->ops->release(device->device_data);
1109 break;
1110 }
1111
1112 /*
1113 * TODO: add an anon_inode interface to do this.
1114 * Appears to be missing by lack of need rather than
1115 * explicitly prevented. Now there's need.
1116 */
1117 filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1118
1119 fd_install(ret, filep);
1120
1121 vfio_device_get(device);
1122 atomic_inc(&group->container_users);
1123 break;
1124 }
1125 mutex_unlock(&group->device_lock);
1126
1127 return ret;
1128}
1129
1130static long vfio_group_fops_unl_ioctl(struct file *filep,
1131 unsigned int cmd, unsigned long arg)
1132{
1133 struct vfio_group *group = filep->private_data;
1134 long ret = -ENOTTY;
1135
1136 switch (cmd) {
1137 case VFIO_GROUP_GET_STATUS:
1138 {
1139 struct vfio_group_status status;
1140 unsigned long minsz;
1141
1142 minsz = offsetofend(struct vfio_group_status, flags);
1143
1144 if (copy_from_user(&status, (void __user *)arg, minsz))
1145 return -EFAULT;
1146
1147 if (status.argsz < minsz)
1148 return -EINVAL;
1149
1150 status.flags = 0;
1151
1152 if (vfio_group_viable(group))
1153 status.flags |= VFIO_GROUP_FLAGS_VIABLE;
1154
1155 if (group->container)
1156 status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
1157
1158 if (copy_to_user((void __user *)arg, &status, minsz))
1159 return -EFAULT;
1160
1161 ret = 0;
1162 break;
1163 }
1164 case VFIO_GROUP_SET_CONTAINER:
1165 {
1166 int fd;
1167
1168 if (get_user(fd, (int __user *)arg))
1169 return -EFAULT;
1170
1171 if (fd < 0)
1172 return -EINVAL;
1173
1174 ret = vfio_group_set_container(group, fd);
1175 break;
1176 }
1177 case VFIO_GROUP_UNSET_CONTAINER:
1178 ret = vfio_group_unset_container(group);
1179 break;
1180 case VFIO_GROUP_GET_DEVICE_FD:
1181 {
1182 char *buf;
1183
1184 buf = strndup_user((const char __user *)arg, PAGE_SIZE);
1185 if (IS_ERR(buf))
1186 return PTR_ERR(buf);
1187
1188 ret = vfio_group_get_device_fd(group, buf);
1189 kfree(buf);
1190 break;
1191 }
1192 }
1193
1194 return ret;
1195}
1196
1197#ifdef CONFIG_COMPAT
1198static long vfio_group_fops_compat_ioctl(struct file *filep,
1199 unsigned int cmd, unsigned long arg)
1200{
1201 arg = (unsigned long)compat_ptr(arg);
1202 return vfio_group_fops_unl_ioctl(filep, cmd, arg);
1203}
1204#endif /* CONFIG_COMPAT */
1205
1206static int vfio_group_fops_open(struct inode *inode, struct file *filep)
1207{
1208 struct vfio_group *group;
1209
1210 group = vfio_group_get_from_minor(iminor(inode));
1211 if (!group)
1212 return -ENODEV;
1213
1214 if (group->container) {
1215 vfio_group_put(group);
1216 return -EBUSY;
1217 }
1218
1219 filep->private_data = group;
1220
1221 return 0;
1222}
1223
1224static int vfio_group_fops_release(struct inode *inode, struct file *filep)
1225{
1226 struct vfio_group *group = filep->private_data;
1227
1228 filep->private_data = NULL;
1229
1230 vfio_group_try_dissolve_container(group);
1231
1232 vfio_group_put(group);
1233
1234 return 0;
1235}
1236
1237static const struct file_operations vfio_group_fops = {
1238 .owner = THIS_MODULE,
1239 .unlocked_ioctl = vfio_group_fops_unl_ioctl,
1240#ifdef CONFIG_COMPAT
1241 .compat_ioctl = vfio_group_fops_compat_ioctl,
1242#endif
1243 .open = vfio_group_fops_open,
1244 .release = vfio_group_fops_release,
1245};
1246
1247/**
1248 * VFIO Device fd
1249 */
1250static int vfio_device_fops_release(struct inode *inode, struct file *filep)
1251{
1252 struct vfio_device *device = filep->private_data;
1253
1254 device->ops->release(device->device_data);
1255
1256 vfio_group_try_dissolve_container(device->group);
1257
1258 vfio_device_put(device);
1259
1260 return 0;
1261}
1262
1263static long vfio_device_fops_unl_ioctl(struct file *filep,
1264 unsigned int cmd, unsigned long arg)
1265{
1266 struct vfio_device *device = filep->private_data;
1267
1268 if (unlikely(!device->ops->ioctl))
1269 return -EINVAL;
1270
1271 return device->ops->ioctl(device->device_data, cmd, arg);
1272}
1273
1274static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
1275 size_t count, loff_t *ppos)
1276{
1277 struct vfio_device *device = filep->private_data;
1278
1279 if (unlikely(!device->ops->read))
1280 return -EINVAL;
1281
1282 return device->ops->read(device->device_data, buf, count, ppos);
1283}
1284
1285static ssize_t vfio_device_fops_write(struct file *filep,
1286 const char __user *buf,
1287 size_t count, loff_t *ppos)
1288{
1289 struct vfio_device *device = filep->private_data;
1290
1291 if (unlikely(!device->ops->write))
1292 return -EINVAL;
1293
1294 return device->ops->write(device->device_data, buf, count, ppos);
1295}
1296
1297static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1298{
1299 struct vfio_device *device = filep->private_data;
1300
1301 if (unlikely(!device->ops->mmap))
1302 return -EINVAL;
1303
1304 return device->ops->mmap(device->device_data, vma);
1305}
1306
1307#ifdef CONFIG_COMPAT
1308static long vfio_device_fops_compat_ioctl(struct file *filep,
1309 unsigned int cmd, unsigned long arg)
1310{
1311 arg = (unsigned long)compat_ptr(arg);
1312 return vfio_device_fops_unl_ioctl(filep, cmd, arg);
1313}
1314#endif /* CONFIG_COMPAT */
1315
1316static const struct file_operations vfio_device_fops = {
1317 .owner = THIS_MODULE,
1318 .release = vfio_device_fops_release,
1319 .read = vfio_device_fops_read,
1320 .write = vfio_device_fops_write,
1321 .unlocked_ioctl = vfio_device_fops_unl_ioctl,
1322#ifdef CONFIG_COMPAT
1323 .compat_ioctl = vfio_device_fops_compat_ioctl,
1324#endif
1325 .mmap = vfio_device_fops_mmap,
1326};
1327
1328/**
1329 * Module/class support
1330 */
1331static char *vfio_devnode(struct device *dev, umode_t *mode)
1332{
1333 return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
1334}
1335
1336static int __init vfio_init(void)
1337{
1338 int ret;
1339
1340 idr_init(&vfio.group_idr);
1341 mutex_init(&vfio.group_lock);
1342 mutex_init(&vfio.iommu_drivers_lock);
1343 INIT_LIST_HEAD(&vfio.group_list);
1344 INIT_LIST_HEAD(&vfio.iommu_drivers_list);
1345 init_waitqueue_head(&vfio.release_q);
1346
1347 vfio.class = class_create(THIS_MODULE, "vfio");
1348 if (IS_ERR(vfio.class)) {
1349 ret = PTR_ERR(vfio.class);
1350 goto err_class;
1351 }
1352
1353 vfio.class->devnode = vfio_devnode;
1354
1355 ret = alloc_chrdev_region(&vfio.devt, 0, MINORMASK, "vfio");
1356 if (ret)
1357 goto err_base_chrdev;
1358
1359 cdev_init(&vfio.cdev, &vfio_fops);
1360 ret = cdev_add(&vfio.cdev, vfio.devt, 1);
1361 if (ret)
1362 goto err_base_cdev;
1363
1364 vfio.dev = device_create(vfio.class, NULL, vfio.devt, NULL, "vfio");
1365 if (IS_ERR(vfio.dev)) {
1366 ret = PTR_ERR(vfio.dev);
1367 goto err_base_dev;
1368 }
1369
1370 /* /dev/vfio/$GROUP */
1371 cdev_init(&vfio.group_cdev, &vfio_group_fops);
1372 ret = cdev_add(&vfio.group_cdev,
1373 MKDEV(MAJOR(vfio.devt), 1), MINORMASK - 1);
1374 if (ret)
1375 goto err_groups_cdev;
1376
1377 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
1378
1379 /*
1380 * Attempt to load known iommu-drivers. This gives us a working
1381 * environment without the user needing to explicitly load iommu
1382 * drivers.
1383 */
1384 request_module_nowait("vfio_iommu_type1");
1385
1386 return 0;
1387
1388err_groups_cdev:
1389 device_destroy(vfio.class, vfio.devt);
1390err_base_dev:
1391 cdev_del(&vfio.cdev);
1392err_base_cdev:
1393 unregister_chrdev_region(vfio.devt, MINORMASK);
1394err_base_chrdev:
1395 class_destroy(vfio.class);
1396 vfio.class = NULL;
1397err_class:
1398 return ret;
1399}
1400
1401static void __exit vfio_cleanup(void)
1402{
1403 WARN_ON(!list_empty(&vfio.group_list));
1404
1405 idr_destroy(&vfio.group_idr);
1406 cdev_del(&vfio.group_cdev);
1407 device_destroy(vfio.class, vfio.devt);
1408 cdev_del(&vfio.cdev);
1409 unregister_chrdev_region(vfio.devt, MINORMASK);
1410 class_destroy(vfio.class);
1411 vfio.class = NULL;
1412}
1413
1414module_init(vfio_init);
1415module_exit(vfio_cleanup);
1416
1417MODULE_VERSION(DRIVER_VERSION);
1418MODULE_LICENSE("GPL v2");
1419MODULE_AUTHOR(DRIVER_AUTHOR);
1420MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
new file mode 100644
index 000000000000..6f3fbc48a6c7
--- /dev/null
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -0,0 +1,753 @@
1/*
2 * VFIO: IOMMU DMA mapping support for Type1 IOMMU
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 *
15 * We arbitrarily define a Type1 IOMMU as one matching the below code.
16 * It could be called the x86 IOMMU as it's designed for AMD-Vi & Intel
17 * VT-d, but that makes it harder to re-use as theoretically anyone
18 * implementing a similar IOMMU could make use of this. We expect the
19 * IOMMU to support the IOMMU API and have few to no restrictions around
20 * the IOVA range that can be mapped. The Type1 IOMMU is currently
21 * optimized for relatively static mappings of a userspace process with
22 * userpsace pages pinned into memory. We also assume devices and IOMMU
23 * domains are PCI based as the IOMMU API is still centered around a
24 * device/bus interface rather than a group interface.
25 */
26
27#include <linux/compat.h>
28#include <linux/device.h>
29#include <linux/fs.h>
30#include <linux/iommu.h>
31#include <linux/module.h>
32#include <linux/mm.h>
33#include <linux/pci.h> /* pci_bus_type */
34#include <linux/sched.h>
35#include <linux/slab.h>
36#include <linux/uaccess.h>
37#include <linux/vfio.h>
38#include <linux/workqueue.h>
39
40#define DRIVER_VERSION "0.2"
41#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
42#define DRIVER_DESC "Type1 IOMMU driver for VFIO"
43
44static bool allow_unsafe_interrupts;
45module_param_named(allow_unsafe_interrupts,
46 allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR);
47MODULE_PARM_DESC(allow_unsafe_interrupts,
48 "Enable VFIO IOMMU support for on platforms without interrupt remapping support.");
49
50struct vfio_iommu {
51 struct iommu_domain *domain;
52 struct mutex lock;
53 struct list_head dma_list;
54 struct list_head group_list;
55 bool cache;
56};
57
58struct vfio_dma {
59 struct list_head next;
60 dma_addr_t iova; /* Device address */
61 unsigned long vaddr; /* Process virtual addr */
62 long npage; /* Number of pages */
63 int prot; /* IOMMU_READ/WRITE */
64};
65
66struct vfio_group {
67 struct iommu_group *iommu_group;
68 struct list_head next;
69};
70
71/*
72 * This code handles mapping and unmapping of user data buffers
73 * into DMA'ble space using the IOMMU
74 */
75
76#define NPAGE_TO_SIZE(npage) ((size_t)(npage) << PAGE_SHIFT)
77
78struct vwork {
79 struct mm_struct *mm;
80 long npage;
81 struct work_struct work;
82};
83
84/* delayed decrement/increment for locked_vm */
85static void vfio_lock_acct_bg(struct work_struct *work)
86{
87 struct vwork *vwork = container_of(work, struct vwork, work);
88 struct mm_struct *mm;
89
90 mm = vwork->mm;
91 down_write(&mm->mmap_sem);
92 mm->locked_vm += vwork->npage;
93 up_write(&mm->mmap_sem);
94 mmput(mm);
95 kfree(vwork);
96}
97
98static void vfio_lock_acct(long npage)
99{
100 struct vwork *vwork;
101 struct mm_struct *mm;
102
103 if (!current->mm)
104 return; /* process exited */
105
106 if (down_write_trylock(&current->mm->mmap_sem)) {
107 current->mm->locked_vm += npage;
108 up_write(&current->mm->mmap_sem);
109 return;
110 }
111
112 /*
113 * Couldn't get mmap_sem lock, so must setup to update
114 * mm->locked_vm later. If locked_vm were atomic, we
115 * wouldn't need this silliness
116 */
117 vwork = kmalloc(sizeof(struct vwork), GFP_KERNEL);
118 if (!vwork)
119 return;
120 mm = get_task_mm(current);
121 if (!mm) {
122 kfree(vwork);
123 return;
124 }
125 INIT_WORK(&vwork->work, vfio_lock_acct_bg);
126 vwork->mm = mm;
127 vwork->npage = npage;
128 schedule_work(&vwork->work);
129}
130
131/*
132 * Some mappings aren't backed by a struct page, for example an mmap'd
133 * MMIO range for our own or another device. These use a different
134 * pfn conversion and shouldn't be tracked as locked pages.
135 */
136static bool is_invalid_reserved_pfn(unsigned long pfn)
137{
138 if (pfn_valid(pfn)) {
139 bool reserved;
140 struct page *tail = pfn_to_page(pfn);
141 struct page *head = compound_trans_head(tail);
142 reserved = !!(PageReserved(head));
143 if (head != tail) {
144 /*
145 * "head" is not a dangling pointer
146 * (compound_trans_head takes care of that)
147 * but the hugepage may have been split
148 * from under us (and we may not hold a
149 * reference count on the head page so it can
150 * be reused before we run PageReferenced), so
151 * we've to check PageTail before returning
152 * what we just read.
153 */
154 smp_rmb();
155 if (PageTail(tail))
156 return reserved;
157 }
158 return PageReserved(tail);
159 }
160
161 return true;
162}
163
164static int put_pfn(unsigned long pfn, int prot)
165{
166 if (!is_invalid_reserved_pfn(pfn)) {
167 struct page *page = pfn_to_page(pfn);
168 if (prot & IOMMU_WRITE)
169 SetPageDirty(page);
170 put_page(page);
171 return 1;
172 }
173 return 0;
174}
175
176/* Unmap DMA region */
177static long __vfio_dma_do_unmap(struct vfio_iommu *iommu, dma_addr_t iova,
178 long npage, int prot)
179{
180 long i, unlocked = 0;
181
182 for (i = 0; i < npage; i++, iova += PAGE_SIZE) {
183 unsigned long pfn;
184
185 pfn = iommu_iova_to_phys(iommu->domain, iova) >> PAGE_SHIFT;
186 if (pfn) {
187 iommu_unmap(iommu->domain, iova, PAGE_SIZE);
188 unlocked += put_pfn(pfn, prot);
189 }
190 }
191 return unlocked;
192}
193
194static void vfio_dma_unmap(struct vfio_iommu *iommu, dma_addr_t iova,
195 long npage, int prot)
196{
197 long unlocked;
198
199 unlocked = __vfio_dma_do_unmap(iommu, iova, npage, prot);
200 vfio_lock_acct(-unlocked);
201}
202
203static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn)
204{
205 struct page *page[1];
206 struct vm_area_struct *vma;
207 int ret = -EFAULT;
208
209 if (get_user_pages_fast(vaddr, 1, !!(prot & IOMMU_WRITE), page) == 1) {
210 *pfn = page_to_pfn(page[0]);
211 return 0;
212 }
213
214 down_read(&current->mm->mmap_sem);
215
216 vma = find_vma_intersection(current->mm, vaddr, vaddr + 1);
217
218 if (vma && vma->vm_flags & VM_PFNMAP) {
219 *pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
220 if (is_invalid_reserved_pfn(*pfn))
221 ret = 0;
222 }
223
224 up_read(&current->mm->mmap_sem);
225
226 return ret;
227}
228
229/* Map DMA region */
230static int __vfio_dma_map(struct vfio_iommu *iommu, dma_addr_t iova,
231 unsigned long vaddr, long npage, int prot)
232{
233 dma_addr_t start = iova;
234 long i, locked = 0;
235 int ret;
236
237 /* Verify that pages are not already mapped */
238 for (i = 0; i < npage; i++, iova += PAGE_SIZE)
239 if (iommu_iova_to_phys(iommu->domain, iova))
240 return -EBUSY;
241
242 iova = start;
243
244 if (iommu->cache)
245 prot |= IOMMU_CACHE;
246
247 /*
248 * XXX We break mappings into pages and use get_user_pages_fast to
249 * pin the pages in memory. It's been suggested that mlock might
250 * provide a more efficient mechanism, but nothing prevents the
251 * user from munlocking the pages, which could then allow the user
252 * access to random host memory. We also have no guarantee from the
253 * IOMMU API that the iommu driver can unmap sub-pages of previous
254 * mappings. This means we might lose an entire range if a single
255 * page within it is unmapped. Single page mappings are inefficient,
256 * but provide the most flexibility for now.
257 */
258 for (i = 0; i < npage; i++, iova += PAGE_SIZE, vaddr += PAGE_SIZE) {
259 unsigned long pfn = 0;
260
261 ret = vaddr_get_pfn(vaddr, prot, &pfn);
262 if (ret) {
263 __vfio_dma_do_unmap(iommu, start, i, prot);
264 return ret;
265 }
266
267 /*
268 * Only add actual locked pages to accounting
269 * XXX We're effectively marking a page locked for every
270 * IOVA page even though it's possible the user could be
271 * backing multiple IOVAs with the same vaddr. This over-
272 * penalizes the user process, but we currently have no
273 * easy way to do this properly.
274 */
275 if (!is_invalid_reserved_pfn(pfn))
276 locked++;
277
278 ret = iommu_map(iommu->domain, iova,
279 (phys_addr_t)pfn << PAGE_SHIFT,
280 PAGE_SIZE, prot);
281 if (ret) {
282 /* Back out mappings on error */
283 put_pfn(pfn, prot);
284 __vfio_dma_do_unmap(iommu, start, i, prot);
285 return ret;
286 }
287 }
288 vfio_lock_acct(locked);
289 return 0;
290}
291
292static inline bool ranges_overlap(dma_addr_t start1, size_t size1,
293 dma_addr_t start2, size_t size2)
294{
295 if (start1 < start2)
296 return (start2 - start1 < size1);
297 else if (start2 < start1)
298 return (start1 - start2 < size2);
299 return (size1 > 0 && size2 > 0);
300}
301
302static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
303 dma_addr_t start, size_t size)
304{
305 struct vfio_dma *dma;
306
307 list_for_each_entry(dma, &iommu->dma_list, next) {
308 if (ranges_overlap(dma->iova, NPAGE_TO_SIZE(dma->npage),
309 start, size))
310 return dma;
311 }
312 return NULL;
313}
314
315static long vfio_remove_dma_overlap(struct vfio_iommu *iommu, dma_addr_t start,
316 size_t size, struct vfio_dma *dma)
317{
318 struct vfio_dma *split;
319 long npage_lo, npage_hi;
320
321 /* Existing dma region is completely covered, unmap all */
322 if (start <= dma->iova &&
323 start + size >= dma->iova + NPAGE_TO_SIZE(dma->npage)) {
324 vfio_dma_unmap(iommu, dma->iova, dma->npage, dma->prot);
325 list_del(&dma->next);
326 npage_lo = dma->npage;
327 kfree(dma);
328 return npage_lo;
329 }
330
331 /* Overlap low address of existing range */
332 if (start <= dma->iova) {
333 size_t overlap;
334
335 overlap = start + size - dma->iova;
336 npage_lo = overlap >> PAGE_SHIFT;
337
338 vfio_dma_unmap(iommu, dma->iova, npage_lo, dma->prot);
339 dma->iova += overlap;
340 dma->vaddr += overlap;
341 dma->npage -= npage_lo;
342 return npage_lo;
343 }
344
345 /* Overlap high address of existing range */
346 if (start + size >= dma->iova + NPAGE_TO_SIZE(dma->npage)) {
347 size_t overlap;
348
349 overlap = dma->iova + NPAGE_TO_SIZE(dma->npage) - start;
350 npage_hi = overlap >> PAGE_SHIFT;
351
352 vfio_dma_unmap(iommu, start, npage_hi, dma->prot);
353 dma->npage -= npage_hi;
354 return npage_hi;
355 }
356
357 /* Split existing */
358 npage_lo = (start - dma->iova) >> PAGE_SHIFT;
359 npage_hi = dma->npage - (size >> PAGE_SHIFT) - npage_lo;
360
361 split = kzalloc(sizeof *split, GFP_KERNEL);
362 if (!split)
363 return -ENOMEM;
364
365 vfio_dma_unmap(iommu, start, size >> PAGE_SHIFT, dma->prot);
366
367 dma->npage = npage_lo;
368
369 split->npage = npage_hi;
370 split->iova = start + size;
371 split->vaddr = dma->vaddr + NPAGE_TO_SIZE(npage_lo) + size;
372 split->prot = dma->prot;
373 list_add(&split->next, &iommu->dma_list);
374 return size >> PAGE_SHIFT;
375}
376
377static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
378 struct vfio_iommu_type1_dma_unmap *unmap)
379{
380 long ret = 0, npage = unmap->size >> PAGE_SHIFT;
381 struct vfio_dma *dma, *tmp;
382 uint64_t mask;
383
384 mask = ((uint64_t)1 << __ffs(iommu->domain->ops->pgsize_bitmap)) - 1;
385
386 if (unmap->iova & mask)
387 return -EINVAL;
388 if (unmap->size & mask)
389 return -EINVAL;
390
391 /* XXX We still break these down into PAGE_SIZE */
392 WARN_ON(mask & PAGE_MASK);
393
394 mutex_lock(&iommu->lock);
395
396 list_for_each_entry_safe(dma, tmp, &iommu->dma_list, next) {
397 if (ranges_overlap(dma->iova, NPAGE_TO_SIZE(dma->npage),
398 unmap->iova, unmap->size)) {
399 ret = vfio_remove_dma_overlap(iommu, unmap->iova,
400 unmap->size, dma);
401 if (ret > 0)
402 npage -= ret;
403 if (ret < 0 || npage == 0)
404 break;
405 }
406 }
407 mutex_unlock(&iommu->lock);
408 return ret > 0 ? 0 : (int)ret;
409}
410
411static int vfio_dma_do_map(struct vfio_iommu *iommu,
412 struct vfio_iommu_type1_dma_map *map)
413{
414 struct vfio_dma *dma, *pdma = NULL;
415 dma_addr_t iova = map->iova;
416 unsigned long locked, lock_limit, vaddr = map->vaddr;
417 size_t size = map->size;
418 int ret = 0, prot = 0;
419 uint64_t mask;
420 long npage;
421
422 mask = ((uint64_t)1 << __ffs(iommu->domain->ops->pgsize_bitmap)) - 1;
423
424 /* READ/WRITE from device perspective */
425 if (map->flags & VFIO_DMA_MAP_FLAG_WRITE)
426 prot |= IOMMU_WRITE;
427 if (map->flags & VFIO_DMA_MAP_FLAG_READ)
428 prot |= IOMMU_READ;
429
430 if (!prot)
431 return -EINVAL; /* No READ/WRITE? */
432
433 if (vaddr & mask)
434 return -EINVAL;
435 if (iova & mask)
436 return -EINVAL;
437 if (size & mask)
438 return -EINVAL;
439
440 /* XXX We still break these down into PAGE_SIZE */
441 WARN_ON(mask & PAGE_MASK);
442
443 /* Don't allow IOVA wrap */
444 if (iova + size && iova + size < iova)
445 return -EINVAL;
446
447 /* Don't allow virtual address wrap */
448 if (vaddr + size && vaddr + size < vaddr)
449 return -EINVAL;
450
451 npage = size >> PAGE_SHIFT;
452 if (!npage)
453 return -EINVAL;
454
455 mutex_lock(&iommu->lock);
456
457 if (vfio_find_dma(iommu, iova, size)) {
458 ret = -EBUSY;
459 goto out_lock;
460 }
461
462 /* account for locked pages */
463 locked = current->mm->locked_vm + npage;
464 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
465 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
466 pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n",
467 __func__, rlimit(RLIMIT_MEMLOCK));
468 ret = -ENOMEM;
469 goto out_lock;
470 }
471
472 ret = __vfio_dma_map(iommu, iova, vaddr, npage, prot);
473 if (ret)
474 goto out_lock;
475
476 /* Check if we abut a region below - nothing below 0 */
477 if (iova) {
478 dma = vfio_find_dma(iommu, iova - 1, 1);
479 if (dma && dma->prot == prot &&
480 dma->vaddr + NPAGE_TO_SIZE(dma->npage) == vaddr) {
481
482 dma->npage += npage;
483 iova = dma->iova;
484 vaddr = dma->vaddr;
485 npage = dma->npage;
486 size = NPAGE_TO_SIZE(npage);
487
488 pdma = dma;
489 }
490 }
491
492 /* Check if we abut a region above - nothing above ~0 + 1 */
493 if (iova + size) {
494 dma = vfio_find_dma(iommu, iova + size, 1);
495 if (dma && dma->prot == prot &&
496 dma->vaddr == vaddr + size) {
497
498 dma->npage += npage;
499 dma->iova = iova;
500 dma->vaddr = vaddr;
501
502 /*
503 * If merged above and below, remove previously
504 * merged entry. New entry covers it.
505 */
506 if (pdma) {
507 list_del(&pdma->next);
508 kfree(pdma);
509 }
510 pdma = dma;
511 }
512 }
513
514 /* Isolated, new region */
515 if (!pdma) {
516 dma = kzalloc(sizeof *dma, GFP_KERNEL);
517 if (!dma) {
518 ret = -ENOMEM;
519 vfio_dma_unmap(iommu, iova, npage, prot);
520 goto out_lock;
521 }
522
523 dma->npage = npage;
524 dma->iova = iova;
525 dma->vaddr = vaddr;
526 dma->prot = prot;
527 list_add(&dma->next, &iommu->dma_list);
528 }
529
530out_lock:
531 mutex_unlock(&iommu->lock);
532 return ret;
533}
534
535static int vfio_iommu_type1_attach_group(void *iommu_data,
536 struct iommu_group *iommu_group)
537{
538 struct vfio_iommu *iommu = iommu_data;
539 struct vfio_group *group, *tmp;
540 int ret;
541
542 group = kzalloc(sizeof(*group), GFP_KERNEL);
543 if (!group)
544 return -ENOMEM;
545
546 mutex_lock(&iommu->lock);
547
548 list_for_each_entry(tmp, &iommu->group_list, next) {
549 if (tmp->iommu_group == iommu_group) {
550 mutex_unlock(&iommu->lock);
551 kfree(group);
552 return -EINVAL;
553 }
554 }
555
556 /*
557 * TODO: Domain have capabilities that might change as we add
558 * groups (see iommu->cache, currently never set). Check for
559 * them and potentially disallow groups to be attached when it
560 * would change capabilities (ugh).
561 */
562 ret = iommu_attach_group(iommu->domain, iommu_group);
563 if (ret) {
564 mutex_unlock(&iommu->lock);
565 kfree(group);
566 return ret;
567 }
568
569 group->iommu_group = iommu_group;
570 list_add(&group->next, &iommu->group_list);
571
572 mutex_unlock(&iommu->lock);
573
574 return 0;
575}
576
577static void vfio_iommu_type1_detach_group(void *iommu_data,
578 struct iommu_group *iommu_group)
579{
580 struct vfio_iommu *iommu = iommu_data;
581 struct vfio_group *group;
582
583 mutex_lock(&iommu->lock);
584
585 list_for_each_entry(group, &iommu->group_list, next) {
586 if (group->iommu_group == iommu_group) {
587 iommu_detach_group(iommu->domain, iommu_group);
588 list_del(&group->next);
589 kfree(group);
590 break;
591 }
592 }
593
594 mutex_unlock(&iommu->lock);
595}
596
597static void *vfio_iommu_type1_open(unsigned long arg)
598{
599 struct vfio_iommu *iommu;
600
601 if (arg != VFIO_TYPE1_IOMMU)
602 return ERR_PTR(-EINVAL);
603
604 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
605 if (!iommu)
606 return ERR_PTR(-ENOMEM);
607
608 INIT_LIST_HEAD(&iommu->group_list);
609 INIT_LIST_HEAD(&iommu->dma_list);
610 mutex_init(&iommu->lock);
611
612 /*
613 * Wish we didn't have to know about bus_type here.
614 */
615 iommu->domain = iommu_domain_alloc(&pci_bus_type);
616 if (!iommu->domain) {
617 kfree(iommu);
618 return ERR_PTR(-EIO);
619 }
620
621 /*
622 * Wish we could specify required capabilities rather than create
623 * a domain, see what comes out and hope it doesn't change along
624 * the way. Fortunately we know interrupt remapping is global for
625 * our iommus.
626 */
627 if (!allow_unsafe_interrupts &&
628 !iommu_domain_has_cap(iommu->domain, IOMMU_CAP_INTR_REMAP)) {
629 pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",
630 __func__);
631 iommu_domain_free(iommu->domain);
632 kfree(iommu);
633 return ERR_PTR(-EPERM);
634 }
635
636 return iommu;
637}
638
639static void vfio_iommu_type1_release(void *iommu_data)
640{
641 struct vfio_iommu *iommu = iommu_data;
642 struct vfio_group *group, *group_tmp;
643 struct vfio_dma *dma, *dma_tmp;
644
645 list_for_each_entry_safe(group, group_tmp, &iommu->group_list, next) {
646 iommu_detach_group(iommu->domain, group->iommu_group);
647 list_del(&group->next);
648 kfree(group);
649 }
650
651 list_for_each_entry_safe(dma, dma_tmp, &iommu->dma_list, next) {
652 vfio_dma_unmap(iommu, dma->iova, dma->npage, dma->prot);
653 list_del(&dma->next);
654 kfree(dma);
655 }
656
657 iommu_domain_free(iommu->domain);
658 iommu->domain = NULL;
659 kfree(iommu);
660}
661
662static long vfio_iommu_type1_ioctl(void *iommu_data,
663 unsigned int cmd, unsigned long arg)
664{
665 struct vfio_iommu *iommu = iommu_data;
666 unsigned long minsz;
667
668 if (cmd == VFIO_CHECK_EXTENSION) {
669 switch (arg) {
670 case VFIO_TYPE1_IOMMU:
671 return 1;
672 default:
673 return 0;
674 }
675 } else if (cmd == VFIO_IOMMU_GET_INFO) {
676 struct vfio_iommu_type1_info info;
677
678 minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes);
679
680 if (copy_from_user(&info, (void __user *)arg, minsz))
681 return -EFAULT;
682
683 if (info.argsz < minsz)
684 return -EINVAL;
685
686 info.flags = 0;
687
688 info.iova_pgsizes = iommu->domain->ops->pgsize_bitmap;
689
690 return copy_to_user((void __user *)arg, &info, minsz);
691
692 } else if (cmd == VFIO_IOMMU_MAP_DMA) {
693 struct vfio_iommu_type1_dma_map map;
694 uint32_t mask = VFIO_DMA_MAP_FLAG_READ |
695 VFIO_DMA_MAP_FLAG_WRITE;
696
697 minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
698
699 if (copy_from_user(&map, (void __user *)arg, minsz))
700 return -EFAULT;
701
702 if (map.argsz < minsz || map.flags & ~mask)
703 return -EINVAL;
704
705 return vfio_dma_do_map(iommu, &map);
706
707 } else if (cmd == VFIO_IOMMU_UNMAP_DMA) {
708 struct vfio_iommu_type1_dma_unmap unmap;
709
710 minsz = offsetofend(struct vfio_iommu_type1_dma_unmap, size);
711
712 if (copy_from_user(&unmap, (void __user *)arg, minsz))
713 return -EFAULT;
714
715 if (unmap.argsz < minsz || unmap.flags)
716 return -EINVAL;
717
718 return vfio_dma_do_unmap(iommu, &unmap);
719 }
720
721 return -ENOTTY;
722}
723
724static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
725 .name = "vfio-iommu-type1",
726 .owner = THIS_MODULE,
727 .open = vfio_iommu_type1_open,
728 .release = vfio_iommu_type1_release,
729 .ioctl = vfio_iommu_type1_ioctl,
730 .attach_group = vfio_iommu_type1_attach_group,
731 .detach_group = vfio_iommu_type1_detach_group,
732};
733
734static int __init vfio_iommu_type1_init(void)
735{
736 if (!iommu_present(&pci_bus_type))
737 return -ENODEV;
738
739 return vfio_register_iommu_driver(&vfio_iommu_driver_ops_type1);
740}
741
742static void __exit vfio_iommu_type1_cleanup(void)
743{
744 vfio_unregister_iommu_driver(&vfio_iommu_driver_ops_type1);
745}
746
747module_init(vfio_iommu_type1_init);
748module_exit(vfio_iommu_type1_cleanup);
749
750MODULE_VERSION(DRIVER_VERSION);
751MODULE_LICENSE("GPL v2");
752MODULE_AUTHOR(DRIVER_AUTHOR);
753MODULE_DESCRIPTION(DRIVER_DESC);