Linux-2.6.12-rc2
[linux-2.6-block.git] / drivers / media / video / videocodec.c
CommitLineData
1da177e4
LT
1/*
2 * VIDEO MOTION CODECs internal API for video devices
3 *
4 * Interface for MJPEG (and maybe later MPEG/WAVELETS) codec's
5 * bound to a master device.
6 *
7 * (c) 2002 Wolfgang Scherr <scherr@net4you.at>
8 *
9 * $Id: videocodec.c,v 1.1.2.8 2003/03/29 07:16:04 rbultje Exp $
10 *
11 * ------------------------------------------------------------------------
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 * ------------------------------------------------------------------------
28 */
29
30#define VIDEOCODEC_VERSION "v0.2"
31
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/types.h>
36#include <linux/slab.h>
37
38// kernel config is here (procfs flag)
39#include <linux/config.h>
40
41#ifdef CONFIG_PROC_FS
42#include <linux/proc_fs.h>
43#include <asm/uaccess.h>
44#endif
45
46#include "videocodec.h"
47
48static int debug = 0;
49module_param(debug, int, 0);
50MODULE_PARM_DESC(debug, "Debug level (0-4)");
51
52#define dprintk(num, format, args...) \
53 do { \
54 if (debug >= num) \
55 printk(format, ##args); \
56 } while (0)
57
58struct attached_list {
59 struct videocodec *codec;
60 struct attached_list *next;
61};
62
63struct codec_list {
64 const struct videocodec *codec;
65 int attached;
66 struct attached_list *list;
67 struct codec_list *next;
68};
69
70static struct codec_list *codeclist_top = NULL;
71
72/* ================================================= */
73/* function prototypes of the master/slave interface */
74/* ================================================= */
75
76struct videocodec *
77videocodec_attach (struct videocodec_master *master)
78{
79 struct codec_list *h = codeclist_top;
80 struct attached_list *a, *ptr;
81 struct videocodec *codec;
82 int res;
83
84 if (!master) {
85 dprintk(1, KERN_ERR "videocodec_attach: no data\n");
86 return NULL;
87 }
88
89 dprintk(2,
90 "videocodec_attach: '%s', type: %x, flags %lx, magic %lx\n",
91 master->name, master->type, master->flags, master->magic);
92
93 if (!h) {
94 dprintk(1,
95 KERN_ERR
96 "videocodec_attach: no device available\n");
97 return NULL;
98 }
99
100 while (h) {
101 // attach only if the slave has at least the flags
102 // expected by the master
103 if ((master->flags & h->codec->flags) == master->flags) {
104 dprintk(4, "videocodec_attach: try '%s'\n",
105 h->codec->name);
106
107 if (!try_module_get(h->codec->owner))
108 return NULL;
109
110 codec =
111 kmalloc(sizeof(struct videocodec), GFP_KERNEL);
112 if (!codec) {
113 dprintk(1,
114 KERN_ERR
115 "videocodec_attach: no mem\n");
116 goto out_module_put;
117 }
118 memcpy(codec, h->codec, sizeof(struct videocodec));
119
120 snprintf(codec->name, sizeof(codec->name),
121 "%s[%d]", codec->name, h->attached);
122 codec->master_data = master;
123 res = codec->setup(codec);
124 if (res == 0) {
125 dprintk(3, "videocodec_attach '%s'\n",
126 codec->name);
127 ptr = (struct attached_list *)
128 kmalloc(sizeof(struct attached_list),
129 GFP_KERNEL);
130 if (!ptr) {
131 dprintk(1,
132 KERN_ERR
133 "videocodec_attach: no memory\n");
134 goto out_kfree;
135 }
136 memset(ptr, 0,
137 sizeof(struct attached_list));
138 ptr->codec = codec;
139
140 a = h->list;
141 if (!a) {
142 h->list = ptr;
143 dprintk(4,
144 "videocodec: first element\n");
145 } else {
146 while (a->next)
147 a = a->next; // find end
148 a->next = ptr;
149 dprintk(4,
150 "videocodec: in after '%s'\n",
151 h->codec->name);
152 }
153
154 h->attached += 1;
155 return codec;
156 } else {
157 kfree(codec);
158 }
159 }
160 h = h->next;
161 }
162
163 dprintk(1, KERN_ERR "videocodec_attach: no codec found!\n");
164 return NULL;
165
166 out_module_put:
167 module_put(h->codec->owner);
168 out_kfree:
169 kfree(codec);
170 return NULL;
171}
172
173int
174videocodec_detach (struct videocodec *codec)
175{
176 struct codec_list *h = codeclist_top;
177 struct attached_list *a, *prev;
178 int res;
179
180 if (!codec) {
181 dprintk(1, KERN_ERR "videocodec_detach: no data\n");
182 return -EINVAL;
183 }
184
185 dprintk(2,
186 "videocodec_detach: '%s', type: %x, flags %lx, magic %lx\n",
187 codec->name, codec->type, codec->flags, codec->magic);
188
189 if (!h) {
190 dprintk(1,
191 KERN_ERR "videocodec_detach: no device left...\n");
192 return -ENXIO;
193 }
194
195 while (h) {
196 a = h->list;
197 prev = NULL;
198 while (a) {
199 if (codec == a->codec) {
200 res = a->codec->unset(a->codec);
201 if (res >= 0) {
202 dprintk(3,
203 "videocodec_detach: '%s'\n",
204 a->codec->name);
205 a->codec->master_data = NULL;
206 } else {
207 dprintk(1,
208 KERN_ERR
209 "videocodec_detach: '%s'\n",
210 a->codec->name);
211 a->codec->master_data = NULL;
212 }
213 if (prev == NULL) {
214 h->list = a->next;
215 dprintk(4,
216 "videocodec: delete first\n");
217 } else {
218 prev->next = a->next;
219 dprintk(4,
220 "videocodec: delete middle\n");
221 }
222 module_put(a->codec->owner);
223 kfree(a->codec);
224 kfree(a);
225 h->attached -= 1;
226 return 0;
227 }
228 prev = a;
229 a = a->next;
230 }
231 h = h->next;
232 }
233
234 dprintk(1, KERN_ERR "videocodec_detach: given codec not found!\n");
235 return -EINVAL;
236}
237
238int
239videocodec_register (const struct videocodec *codec)
240{
241 struct codec_list *ptr, *h = codeclist_top;
242
243 if (!codec) {
244 dprintk(1, KERN_ERR "videocodec_register: no data!\n");
245 return -EINVAL;
246 }
247
248 dprintk(2,
249 "videocodec: register '%s', type: %x, flags %lx, magic %lx\n",
250 codec->name, codec->type, codec->flags, codec->magic);
251
252 ptr =
253 (struct codec_list *) kmalloc(sizeof(struct codec_list),
254 GFP_KERNEL);
255 if (!ptr) {
256 dprintk(1, KERN_ERR "videocodec_register: no memory\n");
257 return -ENOMEM;
258 }
259 memset(ptr, 0, sizeof(struct codec_list));
260 ptr->codec = codec;
261
262 if (!h) {
263 codeclist_top = ptr;
264 dprintk(4, "videocodec: hooked in as first element\n");
265 } else {
266 while (h->next)
267 h = h->next; // find the end
268 h->next = ptr;
269 dprintk(4, "videocodec: hooked in after '%s'\n",
270 h->codec->name);
271 }
272
273 return 0;
274}
275
276int
277videocodec_unregister (const struct videocodec *codec)
278{
279 struct codec_list *prev = NULL, *h = codeclist_top;
280
281 if (!codec) {
282 dprintk(1, KERN_ERR "videocodec_unregister: no data!\n");
283 return -EINVAL;
284 }
285
286 dprintk(2,
287 "videocodec: unregister '%s', type: %x, flags %lx, magic %lx\n",
288 codec->name, codec->type, codec->flags, codec->magic);
289
290 if (!h) {
291 dprintk(1,
292 KERN_ERR
293 "videocodec_unregister: no device left...\n");
294 return -ENXIO;
295 }
296
297 while (h) {
298 if (codec == h->codec) {
299 if (h->attached) {
300 dprintk(1,
301 KERN_ERR
302 "videocodec: '%s' is used\n",
303 h->codec->name);
304 return -EBUSY;
305 }
306 dprintk(3, "videocodec: unregister '%s' is ok.\n",
307 h->codec->name);
308 if (prev == NULL) {
309 codeclist_top = h->next;
310 dprintk(4,
311 "videocodec: delete first element\n");
312 } else {
313 prev->next = h->next;
314 dprintk(4,
315 "videocodec: delete middle element\n");
316 }
317 kfree(h);
318 return 0;
319 }
320 prev = h;
321 h = h->next;
322 }
323
324 dprintk(1,
325 KERN_ERR
326 "videocodec_unregister: given codec not found!\n");
327 return -EINVAL;
328}
329
330#ifdef CONFIG_PROC_FS
331/* ============ */
332/* procfs stuff */
333/* ============ */
334
335static char *videocodec_buf = NULL;
336static int videocodec_bufsize = 0;
337
338static int
339videocodec_build_table (void)
340{
341 struct codec_list *h = codeclist_top;
342 struct attached_list *a;
343 int i = 0, size;
344
345 // sum up amount of slaves plus their attached masters
346 while (h) {
347 i += h->attached + 1;
348 h = h->next;
349 }
350#define LINESIZE 100
351 size = LINESIZE * (i + 1);
352
353 dprintk(3, "videocodec_build table: %d entries, %d bytes\n", i,
354 size);
355
356 if (videocodec_buf)
357 kfree(videocodec_buf);
358 videocodec_buf = (char *) kmalloc(size, GFP_KERNEL);
359
360 i = 0;
361 i += scnprintf(videocodec_buf + i, size - 1,
362 "<S>lave or attached <M>aster name type flags magic ");
363 i += scnprintf(videocodec_buf + i, size -i - 1, "(connected as)\n");
364
365 h = codeclist_top;
366 while (h) {
367 if (i > (size - LINESIZE))
368 break; // security check
369 i += scnprintf(videocodec_buf + i, size -i -1,
370 "S %32s %04x %08lx %08lx (TEMPLATE)\n",
371 h->codec->name, h->codec->type,
372 h->codec->flags, h->codec->magic);
373 a = h->list;
374 while (a) {
375 if (i > (size - LINESIZE))
376 break; // security check
377 i += scnprintf(videocodec_buf + i, size -i -1,
378 "M %32s %04x %08lx %08lx (%s)\n",
379 a->codec->master_data->name,
380 a->codec->master_data->type,
381 a->codec->master_data->flags,
382 a->codec->master_data->magic,
383 a->codec->name);
384 a = a->next;
385 }
386 h = h->next;
387 }
388
389 return i;
390}
391
392//The definition:
393//typedef int (read_proc_t)(char *page, char **start, off_t off,
394// int count, int *eof, void *data);
395
396static int
397videocodec_info (char *buffer,
398 char **buffer_location,
399 off_t offset,
400 int buffer_length,
401 int *eof,
402 void *data)
403{
404 int size;
405
406 dprintk(3, "videocodec_info: offset: %ld, len %d / size %d\n",
407 offset, buffer_length, videocodec_bufsize);
408
409 if (offset == 0) {
410 videocodec_bufsize = videocodec_build_table();
411 }
412 if ((offset < 0) || (offset >= videocodec_bufsize)) {
413 dprintk(4,
414 "videocodec_info: call delivers no result, return 0\n");
415 *eof = 1;
416 return 0;
417 }
418
419 if (buffer_length < (videocodec_bufsize - offset)) {
420 dprintk(4, "videocodec_info: %ld needed, %d got\n",
421 videocodec_bufsize - offset, buffer_length);
422 size = buffer_length;
423 } else {
424 dprintk(4, "videocodec_info: last reading of %ld bytes\n",
425 videocodec_bufsize - offset);
426 size = videocodec_bufsize - offset;
427 *eof = 1;
428 }
429
430 memcpy(buffer, videocodec_buf + offset, size);
431 /* doesn't work... */
432 /* copy_to_user(buffer, videocodec_buf+offset, size); */
433 /* *buffer_location = videocodec_buf+offset; */
434
435 return size;
436}
437#endif
438
439/* ===================== */
440/* hook in driver module */
441/* ===================== */
442static int __init
443videocodec_init (void)
444{
445#ifdef CONFIG_PROC_FS
446 static struct proc_dir_entry *videocodec_proc_entry;
447#endif
448
449 printk(KERN_INFO "Linux video codec intermediate layer: %s\n",
450 VIDEOCODEC_VERSION);
451
452#ifdef CONFIG_PROC_FS
453 videocodec_buf = NULL;
454 videocodec_bufsize = 0;
455
456 videocodec_proc_entry = create_proc_entry("videocodecs", 0, NULL);
457 if (videocodec_proc_entry) {
458 videocodec_proc_entry->read_proc = videocodec_info;
459 videocodec_proc_entry->write_proc = NULL;
460 videocodec_proc_entry->data = NULL;
461 videocodec_proc_entry->owner = THIS_MODULE;
462 } else {
463 dprintk(1, KERN_ERR "videocodec: can't init procfs.\n");
464 }
465#endif
466 return 0;
467}
468
469static void __exit
470videocodec_exit (void)
471{
472#ifdef CONFIG_PROC_FS
473 remove_proc_entry("videocodecs", NULL);
474 if (videocodec_buf)
475 kfree(videocodec_buf);
476#endif
477}
478
479EXPORT_SYMBOL(videocodec_attach);
480EXPORT_SYMBOL(videocodec_detach);
481EXPORT_SYMBOL(videocodec_register);
482EXPORT_SYMBOL(videocodec_unregister);
483
484module_init(videocodec_init);
485module_exit(videocodec_exit);
486
487MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
488MODULE_DESCRIPTION("Intermediate API module for video codecs "
489 VIDEOCODEC_VERSION);
490MODULE_LICENSE("GPL");