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