2 * VIDEO MOTION CODECs internal API for video devices
4 * Interface for MJPEG (and maybe later MPEG/WAVELETS) codec's
5 * bound to a master device.
7 * (c) 2002 Wolfgang Scherr <scherr@net4you.at>
9 * $Id: videocodec.c,v 1.1.2.8 2003/03/29 07:16:04 rbultje Exp $
11 * ------------------------------------------------------------------------
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.
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.
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.
27 * ------------------------------------------------------------------------
30 #define VIDEOCODEC_VERSION "v0.2"
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>
38 // kernel config is here (procfs flag)
39 #include <linux/config.h>
42 #include <linux/proc_fs.h>
43 #include <asm/uaccess.h>
46 #include <linux/version.h>
47 #ifndef KERNEL_VERSION
48 #define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))
51 #include "videocodec.h"
54 MODULE_PARM(debug, "i");
55 MODULE_PARM_DESC(debug, "Debug level (0-4)");
57 #define dprintk(num, format, args...) \
60 printk(format, ##args); \
63 struct attached_list {
64 struct videocodec *codec;
65 struct attached_list *next;
69 const struct videocodec *codec;
71 struct attached_list *list;
72 struct codec_list *next;
75 static struct codec_list *codeclist_top = NULL;
77 /* ================================================= */
78 /* function prototypes of the master/slave interface */
79 /* ================================================= */
82 videocodec_attach (struct videocodec_master *master)
84 struct codec_list *h = codeclist_top;
85 struct attached_list *a, *ptr;
86 struct videocodec *codec;
90 dprintk(1, KERN_ERR "videocodec_attach: no data\n");
95 "videocodec_attach: '%s', type: %x, flags %lx, magic %lx\n",
96 master->name, master->type, master->flags, master->magic);
101 "videocodec_attach: no device available\n");
106 // attach only if the slave has at least the flags
107 // expected by the master
108 if ((master->flags & h->codec->flags) == master->flags) {
109 dprintk(4, "videocodec_attach: try '%s'\n",
112 kmalloc(sizeof(struct videocodec), GFP_KERNEL);
116 "videocodec_attach: no mem\n");
119 memcpy(codec, h->codec, sizeof(struct videocodec));
121 snprintf(codec->name, sizeof(codec->name),
122 "%s[%d]", codec->name, h->attached);
123 codec->master_data = master;
124 res = codec->setup(codec);
126 dprintk(3, "videocodec_attach '%s'\n",
128 ptr = (struct attached_list *)
129 kmalloc(sizeof(struct attached_list),
134 "videocodec_attach: no memory\n");
139 sizeof(struct attached_list));
142 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
145 if (!try_module_get(THIS_MODULE)) {
148 "videocodec: failed to increment usecount\n");
159 "videocodec: first element\n");
162 a = a->next; // find end
165 "videocodec: in after '%s'\n",
178 dprintk(1, KERN_ERR "videocodec_attach: no codec found!\n");
183 videocodec_detach (struct videocodec *codec)
185 struct codec_list *h = codeclist_top;
186 struct attached_list *a, *prev;
190 dprintk(1, KERN_ERR "videocodec_detach: no data\n");
195 "videocodec_detach: '%s', type: %x, flags %lx, magic %lx\n",
196 codec->name, codec->type, codec->flags, codec->magic);
200 KERN_ERR "videocodec_detach: no device left...\n");
208 if (codec == a->codec) {
209 res = a->codec->unset(a->codec);
212 "videocodec_detach: '%s'\n",
214 a->codec->master_data = NULL;
218 "videocodec_detach: '%s'\n",
220 a->codec->master_data = NULL;
225 "videocodec: delete first\n");
227 prev->next = a->next;
229 "videocodec: delete middle\n");
235 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
238 module_put(THIS_MODULE);
249 dprintk(1, KERN_ERR "videocodec_detach: given codec not found!\n");
254 videocodec_register (const struct videocodec *codec)
256 struct codec_list *ptr, *h = codeclist_top;
259 dprintk(1, KERN_ERR "videocodec_register: no data!\n");
264 "videocodec: register '%s', type: %x, flags %lx, magic %lx\n",
265 codec->name, codec->type, codec->flags, codec->magic);
268 (struct codec_list *) kmalloc(sizeof(struct codec_list),
271 dprintk(1, KERN_ERR "videocodec_register: no memory\n");
274 memset(ptr, 0, sizeof(struct codec_list));
277 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
280 if (!try_module_get(THIS_MODULE)) {
283 "videocodec: failed to increment module count\n");
291 dprintk(4, "videocodec: hooked in as first element\n");
294 h = h->next; // find the end
296 dprintk(4, "videocodec: hooked in after '%s'\n",
304 videocodec_unregister (const struct videocodec *codec)
306 struct codec_list *prev = NULL, *h = codeclist_top;
309 dprintk(1, KERN_ERR "videocodec_unregister: no data!\n");
314 "videocodec: unregister '%s', type: %x, flags %lx, magic %lx\n",
315 codec->name, codec->type, codec->flags, codec->magic);
320 "videocodec_unregister: no device left...\n");
325 if (codec == h->codec) {
329 "videocodec: '%s' is used\n",
333 dprintk(3, "videocodec: unregister '%s' is ok.\n",
336 codeclist_top = h->next;
338 "videocodec: delete first element\n");
340 prev->next = h->next;
342 "videocodec: delete middle element\n");
346 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
349 module_put(THIS_MODULE);
360 "videocodec_unregister: given codec not found!\n");
364 #ifdef CONFIG_PROC_FS
369 static char *videocodec_buf = NULL;
370 static int videocodec_bufsize = 0;
373 videocodec_build_table (void)
375 struct codec_list *h = codeclist_top;
376 struct attached_list *a;
379 // sum up amount of slaves plus their attached masters
381 i += h->attached + 1;
385 size = LINESIZE * (i + 1);
387 dprintk(3, "videocodec_build table: %d entries, %d bytes\n", i,
391 kfree(videocodec_buf);
392 videocodec_buf = (char *) kmalloc(size, GFP_KERNEL);
395 i += scnprintf(videocodec_buf + i, size - 1,
396 "<S>lave or attached <M>aster name type flags magic ");
397 i += scnprintf(videocodec_buf + i, size -i - 1, "(connected as)\n");
401 if (i > (size - LINESIZE))
402 break; // security check
403 i += scnprintf(videocodec_buf + i, size -i -1,
404 "S %32s %04x %08lx %08lx (TEMPLATE)\n",
405 h->codec->name, h->codec->type,
406 h->codec->flags, h->codec->magic);
409 if (i > (size - LINESIZE))
410 break; // security check
411 i += scnprintf(videocodec_buf + i, size -i -1,
412 "M %32s %04x %08lx %08lx (%s)\n",
413 a->codec->master_data->name,
414 a->codec->master_data->type,
415 a->codec->master_data->flags,
416 a->codec->master_data->magic,
427 //typedef int (read_proc_t)(char *page, char **start, off_t off,
428 // int count, int *eof, void *data);
431 videocodec_info (char *buffer,
432 char **buffer_location,
440 dprintk(3, "videocodec_info: offset: %ld, len %d / size %d\n",
441 offset, buffer_length, videocodec_bufsize);
444 videocodec_bufsize = videocodec_build_table();
446 if ((offset < 0) || (offset >= videocodec_bufsize)) {
448 "videocodec_info: call delivers no result, return 0\n");
453 if (buffer_length < (videocodec_bufsize - offset)) {
454 dprintk(4, "videocodec_info: %ld needed, %d got\n",
455 videocodec_bufsize - offset, buffer_length);
456 size = buffer_length;
458 dprintk(4, "videocodec_info: last reading of %ld bytes\n",
459 videocodec_bufsize - offset);
460 size = videocodec_bufsize - offset;
464 memcpy(buffer, videocodec_buf + offset, size);
465 /* doesn't work... */
466 /* copy_to_user(buffer, videocodec_buf+offset, size); */
467 /* *buffer_location = videocodec_buf+offset; */
473 /* ===================== */
474 /* hook in driver module */
475 /* ===================== */
477 videocodec_init (void)
479 #ifdef CONFIG_PROC_FS
480 static struct proc_dir_entry *videocodec_proc_entry;
483 printk(KERN_INFO "Linux video codec intermediate layer: %s\n",
486 #ifdef CONFIG_PROC_FS
487 videocodec_buf = NULL;
488 videocodec_bufsize = 0;
490 videocodec_proc_entry = create_proc_entry("videocodecs", 0, 0);
491 if (videocodec_proc_entry) {
492 videocodec_proc_entry->read_proc = videocodec_info;
493 videocodec_proc_entry->write_proc = NULL;
494 videocodec_proc_entry->data = NULL;
495 videocodec_proc_entry->owner = THIS_MODULE;
497 dprintk(1, KERN_ERR "videocodec: can't init procfs.\n");
504 videocodec_exit (void)
506 #ifdef CONFIG_PROC_FS
507 remove_proc_entry("videocodecs", 0);
509 kfree(videocodec_buf);
513 EXPORT_SYMBOL(videocodec_attach);
514 EXPORT_SYMBOL(videocodec_detach);
515 EXPORT_SYMBOL(videocodec_register);
516 EXPORT_SYMBOL(videocodec_unregister);
518 module_init(videocodec_init);
519 module_exit(videocodec_exit);
521 MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
522 MODULE_DESCRIPTION("Intermediate API module for video codecs "
524 MODULE_LICENSE("GPL");