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/videodev.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
52extern int *zr_debug;
53
54#define dprintk(num, format, args...) \
55	do { \
56		if (*zr_debug >= num) \
57			printk(format, ##args); \
58	} while (0)
59
60#ifdef CONFIG_PROC_FS
61struct procfs_params_zr36067 {
62	char *name;
63	short reg;
64	u32 mask;
65	short bit;
66};
67
68static const struct procfs_params_zr36067 zr67[] = {
69	{"HSPol", 0x000, 1, 30},
70	{"HStart", 0x000, 0x3ff, 10},
71	{"HEnd", 0x000, 0x3ff, 0},
72
73	{"VSPol", 0x004, 1, 30},
74	{"VStart", 0x004, 0x3ff, 10},
75	{"VEnd", 0x004, 0x3ff, 0},
76
77	{"ExtFl", 0x008, 1, 26},
78	{"TopField", 0x008, 1, 25},
79	{"VCLKPol", 0x008, 1, 24},
80	{"DupFld", 0x008, 1, 20},
81	{"LittleEndian", 0x008, 1, 0},
82
83	{"HsyncStart", 0x10c, 0xffff, 16},
84	{"LineTot", 0x10c, 0xffff, 0},
85
86	{"NAX", 0x110, 0xffff, 16},
87	{"PAX", 0x110, 0xffff, 0},
88
89	{"NAY", 0x114, 0xffff, 16},
90	{"PAY", 0x114, 0xffff, 0},
91
92	/* {"",,,}, */
93
94	{NULL, 0, 0, 0},
95};
96
97static void
98setparam (struct zoran *zr,
99	  char         *name,
100	  char         *sval)
101{
102	int i = 0, reg0, reg, val;
103
104	while (zr67[i].name != NULL) {
105		if (!strncmp(name, zr67[i].name, strlen(zr67[i].name))) {
106			reg = reg0 = btread(zr67[i].reg);
107			reg &= ~(zr67[i].mask << zr67[i].bit);
108			if (!isdigit(sval[0]))
109				break;
110			val = simple_strtoul(sval, NULL, 0);
111			if ((val & ~zr67[i].mask))
112				break;
113			reg |= (val & zr67[i].mask) << zr67[i].bit;
114			dprintk(4,
115				KERN_INFO
116				"%s: setparam: setting ZR36067 register 0x%03x: 0x%08x=>0x%08x %s=%d\n",
117				ZR_DEVNAME(zr), zr67[i].reg, reg0, reg,
118				zr67[i].name, val);
119			btwrite(reg, zr67[i].reg);
120			break;
121		}
122		i++;
123	}
124}
125
126static int zoran_show(struct seq_file *p, void *v)
127{
128	struct zoran *zr = p->private;
129	int i;
130
131	seq_printf(p, "ZR36067 registers:\n");
132	for (i = 0; i < 0x130; i += 16)
133		seq_printf(p, "%03X %08X  %08X  %08X  %08X \n", i,
134			   btread(i), btread(i+4), btread(i+8), btread(i+12));
135	return 0;
136}
137
138static int zoran_open(struct inode *inode, struct file *file)
139{
140	struct zoran *data = PDE(inode)->data;
141	return single_open(file, zoran_show, data);
142}
143
144static ssize_t zoran_write(struct file *file, const char __user *buffer,
145			size_t count, loff_t *ppos)
146{
147	struct zoran *zr = PDE(file->f_path.dentry->d_inode)->data;
148	char *string, *sp;
149	char *line, *ldelim, *varname, *svar, *tdelim;
150
151	if (count > 32768)	/* Stupidity filter */
152		return -EINVAL;
153
154	string = sp = vmalloc(count + 1);
155	if (!string) {
156		dprintk(1,
157			KERN_ERR
158			"%s: write_proc: can not allocate memory\n",
159			ZR_DEVNAME(zr));
160		return -ENOMEM;
161	}
162	if (copy_from_user(string, buffer, count)) {
163		vfree (string);
164		return -EFAULT;
165	}
166	string[count] = 0;
167	dprintk(4, KERN_INFO "%s: write_proc: name=%s count=%zu zr=%p\n",
168		ZR_DEVNAME(zr), file->f_path.dentry->d_name.name, count, zr);
169	ldelim = " \t\n";
170	tdelim = "=";
171	line = strpbrk(sp, ldelim);
172	while (line) {
173		*line = 0;
174		svar = strpbrk(sp, tdelim);
175		if (svar) {
176			*svar = 0;
177			varname = sp;
178			svar++;
179			setparam(zr, varname, svar);
180		}
181		sp = line + 1;
182		line = strpbrk(sp, ldelim);
183	}
184	vfree(string);
185
186	return count;
187}
188
189static const struct file_operations zoran_operations = {
190	.open		= zoran_open,
191	.read		= seq_read,
192	.write		= zoran_write,
193	.llseek		= seq_lseek,
194	.release	= single_release,
195};
196#endif
197
198int
199zoran_proc_init (struct zoran *zr)
200{
201#ifdef CONFIG_PROC_FS
202	char name[8];
203
204	snprintf(name, 7, "zoran%d", zr->id);
205	if ((zr->zoran_proc = create_proc_entry(name, 0, NULL))) {
206		zr->zoran_proc->data = zr;
207		zr->zoran_proc->owner = THIS_MODULE;
208		zr->zoran_proc->proc_fops = &zoran_operations;
209		dprintk(2,
210			KERN_INFO
211			"%s: procfs entry /proc/%s allocated. data=%p\n",
212			ZR_DEVNAME(zr), name, zr->zoran_proc->data);
213	} else {
214		dprintk(1, KERN_ERR "%s: Unable to initialise /proc/%s\n",
215			ZR_DEVNAME(zr), name);
216		return 1;
217	}
218#endif
219	return 0;
220}
221
222void
223zoran_proc_cleanup (struct zoran *zr)
224{
225#ifdef CONFIG_PROC_FS
226	char name[8];
227
228	snprintf(name, 7, "zoran%d", zr->id);
229	if (zr->zoran_proc)
230		remove_proc_entry(name, NULL);
231	zr->zoran_proc = NULL;
232#endif
233}
234