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 if (!try_module_get(h->codec->owner))
116 kmalloc(sizeof(struct videocodec), GFP_KERNEL);
120 "videocodec_attach: no mem\n");
123 memcpy(codec, h->codec, sizeof(struct videocodec));
125 snprintf(codec->name, sizeof(codec->name),
126 "%s[%d]", codec->name, h->attached);
127 codec->master_data = master;
128 res = codec->setup(codec);
130 dprintk(3, "videocodec_attach '%s'\n",
132 ptr = (struct attached_list *)
133 kmalloc(sizeof(struct attached_list),
138 "videocodec_attach: no memory\n");
142 sizeof(struct attached_list));
149 "videocodec: first element\n");
152 a = a->next; // find end
155 "videocodec: in after '%s'\n",
168 dprintk(1, KERN_ERR "videocodec_attach: no codec found!\n");
172 module_put(h->codec->owner);
179 videocodec_detach (struct videocodec *codec)
181 struct codec_list *h = codeclist_top;
182 struct attached_list *a, *prev;
186 dprintk(1, KERN_ERR "videocodec_detach: no data\n");
191 "videocodec_detach: '%s', type: %x, flags %lx, magic %lx\n",
192 codec->name, codec->type, codec->flags, codec->magic);
196 KERN_ERR "videocodec_detach: no device left...\n");
204 if (codec == a->codec) {
205 res = a->codec->unset(a->codec);
208 "videocodec_detach: '%s'\n",
210 a->codec->master_data = NULL;
214 "videocodec_detach: '%s'\n",
216 a->codec->master_data = NULL;
221 "videocodec: delete first\n");
223 prev->next = a->next;
225 "videocodec: delete middle\n");
227 module_put(a->codec->owner);
239 dprintk(1, KERN_ERR "videocodec_detach: given codec not found!\n");
244 videocodec_register (const struct videocodec *codec)
246 struct codec_list *ptr, *h = codeclist_top;
249 dprintk(1, KERN_ERR "videocodec_register: no data!\n");
254 "videocodec: register '%s', type: %x, flags %lx, magic %lx\n",
255 codec->name, codec->type, codec->flags, codec->magic);
258 (struct codec_list *) kmalloc(sizeof(struct codec_list),
261 dprintk(1, KERN_ERR "videocodec_register: no memory\n");
264 memset(ptr, 0, sizeof(struct codec_list));
269 dprintk(4, "videocodec: hooked in as first element\n");
272 h = h->next; // find the end
274 dprintk(4, "videocodec: hooked in after '%s'\n",
282 videocodec_unregister (const struct videocodec *codec)
284 struct codec_list *prev = NULL, *h = codeclist_top;
287 dprintk(1, KERN_ERR "videocodec_unregister: no data!\n");
292 "videocodec: unregister '%s', type: %x, flags %lx, magic %lx\n",
293 codec->name, codec->type, codec->flags, codec->magic);
298 "videocodec_unregister: no device left...\n");
303 if (codec == h->codec) {
307 "videocodec: '%s' is used\n",
311 dprintk(3, "videocodec: unregister '%s' is ok.\n",
314 codeclist_top = h->next;
316 "videocodec: delete first element\n");
318 prev->next = h->next;
320 "videocodec: delete middle element\n");
331 "videocodec_unregister: given codec not found!\n");
335 #ifdef CONFIG_PROC_FS
340 static char *videocodec_buf = NULL;
341 static int videocodec_bufsize = 0;
344 videocodec_build_table (void)
346 struct codec_list *h = codeclist_top;
347 struct attached_list *a;
350 // sum up amount of slaves plus their attached masters
352 i += h->attached + 1;
356 size = LINESIZE * (i + 1);
358 dprintk(3, "videocodec_build table: %d entries, %d bytes\n", i,
362 kfree(videocodec_buf);
363 videocodec_buf = (char *) kmalloc(size, GFP_KERNEL);
366 i += scnprintf(videocodec_buf + i, size - 1,
367 "<S>lave or attached <M>aster name type flags magic ");
368 i += scnprintf(videocodec_buf + i, size -i - 1, "(connected as)\n");
372 if (i > (size - LINESIZE))
373 break; // security check
374 i += scnprintf(videocodec_buf + i, size -i -1,
375 "S %32s %04x %08lx %08lx (TEMPLATE)\n",
376 h->codec->name, h->codec->type,
377 h->codec->flags, h->codec->magic);
380 if (i > (size - LINESIZE))
381 break; // security check
382 i += scnprintf(videocodec_buf + i, size -i -1,
383 "M %32s %04x %08lx %08lx (%s)\n",
384 a->codec->master_data->name,
385 a->codec->master_data->type,
386 a->codec->master_data->flags,
387 a->codec->master_data->magic,
398 //typedef int (read_proc_t)(char *page, char **start, off_t off,
399 // int count, int *eof, void *data);
402 videocodec_info (char *buffer,
403 char **buffer_location,
411 dprintk(3, "videocodec_info: offset: %ld, len %d / size %d\n",
412 offset, buffer_length, videocodec_bufsize);
415 videocodec_bufsize = videocodec_build_table();
417 if ((offset < 0) || (offset >= videocodec_bufsize)) {
419 "videocodec_info: call delivers no result, return 0\n");
424 if (buffer_length < (videocodec_bufsize - offset)) {
425 dprintk(4, "videocodec_info: %ld needed, %d got\n",
426 videocodec_bufsize - offset, buffer_length);
427 size = buffer_length;
429 dprintk(4, "videocodec_info: last reading of %ld bytes\n",
430 videocodec_bufsize - offset);
431 size = videocodec_bufsize - offset;
435 memcpy(buffer, videocodec_buf + offset, size);
436 /* doesn't work... */
437 /* copy_to_user(buffer, videocodec_buf+offset, size); */
438 /* *buffer_location = videocodec_buf+offset; */
444 /* ===================== */
445 /* hook in driver module */
446 /* ===================== */
448 videocodec_init (void)
450 #ifdef CONFIG_PROC_FS
451 static struct proc_dir_entry *videocodec_proc_entry;
454 printk(KERN_INFO "Linux video codec intermediate layer: %s\n",
457 #ifdef CONFIG_PROC_FS
458 videocodec_buf = NULL;
459 videocodec_bufsize = 0;
461 videocodec_proc_entry = create_proc_entry("videocodecs", 0, NULL);
462 if (videocodec_proc_entry) {
463 videocodec_proc_entry->read_proc = videocodec_info;
464 videocodec_proc_entry->write_proc = NULL;
465 videocodec_proc_entry->data = NULL;
466 videocodec_proc_entry->owner = THIS_MODULE;
468 dprintk(1, KERN_ERR "videocodec: can't init procfs.\n");
475 videocodec_exit (void)
477 #ifdef CONFIG_PROC_FS
478 remove_proc_entry("videocodecs", NULL);
480 kfree(videocodec_buf);
484 EXPORT_SYMBOL(videocodec_attach);
485 EXPORT_SYMBOL(videocodec_detach);
486 EXPORT_SYMBOL(videocodec_register);
487 EXPORT_SYMBOL(videocodec_unregister);
489 module_init(videocodec_init);
490 module_exit(videocodec_exit);
492 MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
493 MODULE_DESCRIPTION("Intermediate API module for video codecs "
495 MODULE_LICENSE("GPL");