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 "videocodec.h"
49 module_param(debug, int, 0);
50 MODULE_PARM_DESC(debug, "Debug level (0-4)");
52 #define dprintk(num, format, args...) \
55 printk(format, ##args); \
58 struct attached_list {
59 struct videocodec *codec;
60 struct attached_list *next;
64 const struct videocodec *codec;
66 struct attached_list *list;
67 struct codec_list *next;
70 static struct codec_list *codeclist_top = NULL;
72 /* ================================================= */
73 /* function prototypes of the master/slave interface */
74 /* ================================================= */
77 videocodec_attach (struct videocodec_master *master)
79 struct codec_list *h = codeclist_top;
80 struct attached_list *a, *ptr;
81 struct videocodec *codec;
85 dprintk(1, KERN_ERR "videocodec_attach: no data\n");
90 "videocodec_attach: '%s', type: %x, flags %lx, magic %lx\n",
91 master->name, master->type, master->flags, master->magic);
96 "videocodec_attach: no device available\n");
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",
107 if (!try_module_get(h->codec->owner))
111 kmalloc(sizeof(struct videocodec), GFP_KERNEL);
115 "videocodec_attach: no mem\n");
118 memcpy(codec, h->codec, sizeof(struct videocodec));
120 snprintf(codec->name, sizeof(codec->name),
121 "%s[%d]", codec->name, h->attached);
122 codec->master_data = master;
123 res = codec->setup(codec);
125 dprintk(3, "videocodec_attach '%s'\n",
127 ptr = (struct attached_list *)
128 kmalloc(sizeof(struct attached_list),
133 "videocodec_attach: no memory\n");
137 sizeof(struct attached_list));
144 "videocodec: first element\n");
147 a = a->next; // find end
150 "videocodec: in after '%s'\n",
163 dprintk(1, KERN_ERR "videocodec_attach: no codec found!\n");
167 module_put(h->codec->owner);
174 videocodec_detach (struct videocodec *codec)
176 struct codec_list *h = codeclist_top;
177 struct attached_list *a, *prev;
181 dprintk(1, KERN_ERR "videocodec_detach: no data\n");
186 "videocodec_detach: '%s', type: %x, flags %lx, magic %lx\n",
187 codec->name, codec->type, codec->flags, codec->magic);
191 KERN_ERR "videocodec_detach: no device left...\n");
199 if (codec == a->codec) {
200 res = a->codec->unset(a->codec);
203 "videocodec_detach: '%s'\n",
205 a->codec->master_data = NULL;
209 "videocodec_detach: '%s'\n",
211 a->codec->master_data = NULL;
216 "videocodec: delete first\n");
218 prev->next = a->next;
220 "videocodec: delete middle\n");
222 module_put(a->codec->owner);
234 dprintk(1, KERN_ERR "videocodec_detach: given codec not found!\n");
239 videocodec_register (const struct videocodec *codec)
241 struct codec_list *ptr, *h = codeclist_top;
244 dprintk(1, KERN_ERR "videocodec_register: no data!\n");
249 "videocodec: register '%s', type: %x, flags %lx, magic %lx\n",
250 codec->name, codec->type, codec->flags, codec->magic);
253 (struct codec_list *) kmalloc(sizeof(struct codec_list),
256 dprintk(1, KERN_ERR "videocodec_register: no memory\n");
259 memset(ptr, 0, sizeof(struct codec_list));
264 dprintk(4, "videocodec: hooked in as first element\n");
267 h = h->next; // find the end
269 dprintk(4, "videocodec: hooked in after '%s'\n",
277 videocodec_unregister (const struct videocodec *codec)
279 struct codec_list *prev = NULL, *h = codeclist_top;
282 dprintk(1, KERN_ERR "videocodec_unregister: no data!\n");
287 "videocodec: unregister '%s', type: %x, flags %lx, magic %lx\n",
288 codec->name, codec->type, codec->flags, codec->magic);
293 "videocodec_unregister: no device left...\n");
298 if (codec == h->codec) {
302 "videocodec: '%s' is used\n",
306 dprintk(3, "videocodec: unregister '%s' is ok.\n",
309 codeclist_top = h->next;
311 "videocodec: delete first element\n");
313 prev->next = h->next;
315 "videocodec: delete middle element\n");
326 "videocodec_unregister: given codec not found!\n");
330 #ifdef CONFIG_PROC_FS
335 static char *videocodec_buf = NULL;
336 static int videocodec_bufsize = 0;
339 videocodec_build_table (void)
341 struct codec_list *h = codeclist_top;
342 struct attached_list *a;
345 // sum up amount of slaves plus their attached masters
347 i += h->attached + 1;
351 size = LINESIZE * (i + 1);
353 dprintk(3, "videocodec_build table: %d entries, %d bytes\n", i,
356 kfree(videocodec_buf);
357 videocodec_buf = (char *) kmalloc(size, GFP_KERNEL);
360 i += scnprintf(videocodec_buf + i, size - 1,
361 "<S>lave or attached <M>aster name type flags magic ");
362 i += scnprintf(videocodec_buf + i, size -i - 1, "(connected as)\n");
366 if (i > (size - LINESIZE))
367 break; // security check
368 i += scnprintf(videocodec_buf + i, size -i -1,
369 "S %32s %04x %08lx %08lx (TEMPLATE)\n",
370 h->codec->name, h->codec->type,
371 h->codec->flags, h->codec->magic);
374 if (i > (size - LINESIZE))
375 break; // security check
376 i += scnprintf(videocodec_buf + i, size -i -1,
377 "M %32s %04x %08lx %08lx (%s)\n",
378 a->codec->master_data->name,
379 a->codec->master_data->type,
380 a->codec->master_data->flags,
381 a->codec->master_data->magic,
392 //typedef int (read_proc_t)(char *page, char **start, off_t off,
393 // int count, int *eof, void *data);
396 videocodec_info (char *buffer,
397 char **buffer_location,
405 dprintk(3, "videocodec_info: offset: %ld, len %d / size %d\n",
406 offset, buffer_length, videocodec_bufsize);
409 videocodec_bufsize = videocodec_build_table();
411 if ((offset < 0) || (offset >= videocodec_bufsize)) {
413 "videocodec_info: call delivers no result, return 0\n");
418 if (buffer_length < (videocodec_bufsize - offset)) {
419 dprintk(4, "videocodec_info: %ld needed, %d got\n",
420 videocodec_bufsize - offset, buffer_length);
421 size = buffer_length;
423 dprintk(4, "videocodec_info: last reading of %ld bytes\n",
424 videocodec_bufsize - offset);
425 size = videocodec_bufsize - offset;
429 memcpy(buffer, videocodec_buf + offset, size);
430 /* doesn't work... */
431 /* copy_to_user(buffer, videocodec_buf+offset, size); */
432 /* *buffer_location = videocodec_buf+offset; */
438 /* ===================== */
439 /* hook in driver module */
440 /* ===================== */
442 videocodec_init (void)
444 #ifdef CONFIG_PROC_FS
445 static struct proc_dir_entry *videocodec_proc_entry;
448 printk(KERN_INFO "Linux video codec intermediate layer: %s\n",
451 #ifdef CONFIG_PROC_FS
452 videocodec_buf = NULL;
453 videocodec_bufsize = 0;
455 videocodec_proc_entry = create_proc_entry("videocodecs", 0, NULL);
456 if (videocodec_proc_entry) {
457 videocodec_proc_entry->read_proc = videocodec_info;
458 videocodec_proc_entry->write_proc = NULL;
459 videocodec_proc_entry->data = NULL;
460 videocodec_proc_entry->owner = THIS_MODULE;
462 dprintk(1, KERN_ERR "videocodec: can't init procfs.\n");
469 videocodec_exit (void)
471 #ifdef CONFIG_PROC_FS
472 remove_proc_entry("videocodecs", NULL);
473 kfree(videocodec_buf);
477 EXPORT_SYMBOL(videocodec_attach);
478 EXPORT_SYMBOL(videocodec_detach);
479 EXPORT_SYMBOL(videocodec_register);
480 EXPORT_SYMBOL(videocodec_unregister);
482 module_init(videocodec_init);
483 module_exit(videocodec_exit);
485 MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
486 MODULE_DESCRIPTION("Intermediate API module for video codecs "
488 MODULE_LICENSE("GPL");