1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * This part handles the procFS entries (/proc/ZORAN[%d])
7 *
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * Currently maintained by:
11 *   Ronald Bultje    <rbultje@ronald.bitfreak.net>
12 *   Laurent Pinchart <laurent.pinchart@skynet.be>
13 *   Mailinglist      <mjpeg-users@lists.sf.net>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#include <linux/types.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/vmalloc.h>
34
35#include <linux/proc_fs.h>
36#include <linux/pci.h>
37#include <linux/i2c.h>
38#include <linux/i2c-algo-bit.h>
39#include <linux/videodev2.h>
40#include <linux/spinlock.h>
41#include <linux/sem.h>
42#include <linux/seq_file.h>
43
44#include <linux/ctype.h>
45#include <linux/poll.h>
46#include <asm/io.h>
47
48#include "videocodec.h"
49#include "zoran.h"
50#include "zoran_procfs.h"
51#include "zoran_card.h"
52
53#ifdef CONFIG_PROC_FS
54struct procfs_params_zr36067 {
55	char *name;
56	short reg;
57	u32 mask;
58	short bit;
59};
60
61static const struct procfs_params_zr36067 zr67[] = {
62	{"HSPol", 0x000, 1, 30},
63	{"HStart", 0x000, 0x3ff, 10},
64	{"HEnd", 0x000, 0x3ff, 0},
65
66	{"VSPol", 0x004, 1, 30},
67	{"VStart", 0x004, 0x3ff, 10},
68	{"VEnd", 0x004, 0x3ff, 0},
69
70	{"ExtFl", 0x008, 1, 26},
71	{"TopField", 0x008, 1, 25},
72	{"VCLKPol", 0x008, 1, 24},
73	{"DupFld", 0x008, 1, 20},
74	{"LittleEndian", 0x008, 1, 0},
75
76	{"HsyncStart", 0x10c, 0xffff, 16},
77	{"LineTot", 0x10c, 0xffff, 0},
78
79	{"NAX", 0x110, 0xffff, 16},
80	{"PAX", 0x110, 0xffff, 0},
81
82	{"NAY", 0x114, 0xffff, 16},
83	{"PAY", 0x114, 0xffff, 0},
84
85	/* {"",,,}, */
86
87	{NULL, 0, 0, 0},
88};
89
90static void
91setparam (struct zoran *zr,
92	  char         *name,
93	  char         *sval)
94{
95	int i = 0, reg0, reg, val;
96
97	while (zr67[i].name != NULL) {
98		if (!strncmp(name, zr67[i].name, strlen(zr67[i].name))) {
99			reg = reg0 = btread(zr67[i].reg);
100			reg &= ~(zr67[i].mask << zr67[i].bit);
101			if (!isdigit(sval[0]))
102				break;
103			val = simple_strtoul(sval, NULL, 0);
104			if ((val & ~zr67[i].mask))
105				break;
106			reg |= (val & zr67[i].mask) << zr67[i].bit;
107			dprintk(4,
108				KERN_INFO
109				"%s: setparam: setting ZR36067 register 0x%03x: 0x%08x=>0x%08x %s=%d\n",
110				ZR_DEVNAME(zr), zr67[i].reg, reg0, reg,
111				zr67[i].name, val);
112			btwrite(reg, zr67[i].reg);
113			break;
114		}
115		i++;
116	}
117}
118
119static int zoran_show(struct seq_file *p, void *v)
120{
121	struct zoran *zr = p->private;
122	int i;
123
124	seq_printf(p, "ZR36067 registers:\n");
125	for (i = 0; i < 0x130; i += 16)
126		seq_printf(p, "%03X %08X  %08X  %08X  %08X \n", i,
127			   btread(i), btread(i+4), btread(i+8), btread(i+12));
128	return 0;
129}
130
131static int zoran_open(struct inode *inode, struct file *file)
132{
133	struct zoran *data = PDE_DATA(inode);
134	return single_open(file, zoran_show, data);
135}
136
137static ssize_t zoran_write(struct file *file, const char __user *buffer,
138			size_t count, loff_t *ppos)
139{
140	struct zoran *zr = PDE_DATA(file_inode(file));
141	char *string, *sp;
142	char *line, *ldelim, *varname, *svar, *tdelim;
143
144	if (count > 32768)	/* Stupidity filter */
145		return -EINVAL;
146
147	string = sp = vmalloc(count + 1);
148	if (!string) {
149		dprintk(1,
150			KERN_ERR
151			"%s: write_proc: can not allocate memory\n",
152			ZR_DEVNAME(zr));
153		return -ENOMEM;
154	}
155	if (copy_from_user(string, buffer, count)) {
156		vfree (string);
157		return -EFAULT;
158	}
159	string[count] = 0;
160	dprintk(4, KERN_INFO "%s: write_proc: name=%pD count=%zu zr=%p\n",
161		ZR_DEVNAME(zr), file, count, zr);
162	ldelim = " \t\n";
163	tdelim = "=";
164	line = strpbrk(sp, ldelim);
165	while (line) {
166		*line = 0;
167		svar = strpbrk(sp, tdelim);
168		if (svar) {
169			*svar = 0;
170			varname = sp;
171			svar++;
172			setparam(zr, varname, svar);
173		}
174		sp = line + 1;
175		line = strpbrk(sp, ldelim);
176	}
177	vfree(string);
178
179	return count;
180}
181
182static const struct file_operations zoran_operations = {
183	.owner		= THIS_MODULE,
184	.open		= zoran_open,
185	.read		= seq_read,
186	.write		= zoran_write,
187	.llseek		= seq_lseek,
188	.release	= single_release,
189};
190#endif
191
192int
193zoran_proc_init (struct zoran *zr)
194{
195#ifdef CONFIG_PROC_FS
196	char name[8];
197
198	snprintf(name, 7, "zoran%d", zr->id);
199	zr->zoran_proc = proc_create_data(name, 0, NULL, &zoran_operations, zr);
200	if (zr->zoran_proc != NULL) {
201		dprintk(2,
202			KERN_INFO
203			"%s: procfs entry /proc/%s allocated. data=%p\n",
204			ZR_DEVNAME(zr), name, zr);
205	} else {
206		dprintk(1, KERN_ERR "%s: Unable to initialise /proc/%s\n",
207			ZR_DEVNAME(zr), name);
208		return 1;
209	}
210#endif
211	return 0;
212}
213
214void
215zoran_proc_cleanup (struct zoran *zr)
216{
217#ifdef CONFIG_PROC_FS
218	char name[8];
219
220	snprintf(name, 7, "zoran%d", zr->id);
221	if (zr->zoran_proc)
222		remove_proc_entry(name, NULL);
223	zr->zoran_proc = NULL;
224#endif
225}
226