1/*
2 *  Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 *  Various common ioctl() support functions
5 *
6 *  Copyright (c) 2009 Andy Walls <awalls@md.metrocast.net>
7 *
8 *  This program is free software; you can redistribute it and/or modify
9 *  it under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation; either version 2 of the License, or
11 *  (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *
17 *  GNU General Public License for more details.
18 */
19
20#include "cx23885.h"
21#include "cx23885-ioctl.h"
22
23#ifdef CONFIG_VIDEO_ADV_DEBUG
24int cx23885_g_chip_info(struct file *file, void *fh,
25			 struct v4l2_dbg_chip_info *chip)
26{
27	struct cx23885_dev *dev = video_drvdata(file);
28
29	if (chip->match.addr > 1)
30		return -EINVAL;
31	if (chip->match.addr == 1) {
32		if (dev->v4l_device == NULL)
33			return -EINVAL;
34		strlcpy(chip->name, "cx23417", sizeof(chip->name));
35	} else {
36		strlcpy(chip->name, dev->v4l2_dev.name, sizeof(chip->name));
37	}
38	return 0;
39}
40
41static int cx23417_g_register(struct cx23885_dev *dev,
42			      struct v4l2_dbg_register *reg)
43{
44	u32 value;
45
46	if (dev->v4l_device == NULL)
47		return -EINVAL;
48
49	if ((reg->reg & 0x3) != 0 || reg->reg >= 0x10000)
50		return -EINVAL;
51
52	if (mc417_register_read(dev, (u16) reg->reg, &value))
53		return -EINVAL; /* V4L2 spec, but -EREMOTEIO really */
54
55	reg->size = 4;
56	reg->val = value;
57	return 0;
58}
59
60int cx23885_g_register(struct file *file, void *fh,
61		       struct v4l2_dbg_register *reg)
62{
63	struct cx23885_dev *dev = video_drvdata(file);
64
65	if (reg->match.addr > 1)
66		return -EINVAL;
67	if (reg->match.addr)
68		return cx23417_g_register(dev, reg);
69
70	if ((reg->reg & 0x3) != 0 || reg->reg >= pci_resource_len(dev->pci, 0))
71		return -EINVAL;
72
73	reg->size = 4;
74	reg->val = cx_read(reg->reg);
75	return 0;
76}
77
78static int cx23417_s_register(struct cx23885_dev *dev,
79			      const struct v4l2_dbg_register *reg)
80{
81	if (dev->v4l_device == NULL)
82		return -EINVAL;
83
84	if ((reg->reg & 0x3) != 0 || reg->reg >= 0x10000)
85		return -EINVAL;
86
87	if (mc417_register_write(dev, (u16) reg->reg, (u32) reg->val))
88		return -EINVAL; /* V4L2 spec, but -EREMOTEIO really */
89	return 0;
90}
91
92int cx23885_s_register(struct file *file, void *fh,
93		       const struct v4l2_dbg_register *reg)
94{
95	struct cx23885_dev *dev = video_drvdata(file);
96
97	if (reg->match.addr > 1)
98		return -EINVAL;
99	if (reg->match.addr)
100		return cx23417_s_register(dev, reg);
101
102	if ((reg->reg & 0x3) != 0 || reg->reg >= pci_resource_len(dev->pci, 0))
103		return -EINVAL;
104
105	cx_write(reg->reg, reg->val);
106	return 0;
107}
108#endif
109