+- add patches.fixes/linux-post-2.6.3-20040220
[linux-flexiantxendom0-3.2.10.git] / drivers / media / video / videocodec.c
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 <linux/version.h>
47 #ifndef KERNEL_VERSION
48 #define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))
49 #endif
50
51 #include "videocodec.h"
52
53 static int debug = 0;
54 MODULE_PARM(debug, "i");
55 MODULE_PARM_DESC(debug, "Debug level (0-4)");
56
57 #define dprintk(num, format, args...) \
58         do { \
59                 if (debug >= num) \
60                         printk(format, ##args); \
61         } while (0)
62
63 struct attached_list {
64         struct videocodec *codec;
65         struct attached_list *next;
66 };
67
68 struct codec_list {
69         const struct videocodec *codec;
70         int attached;
71         struct attached_list *list;
72         struct codec_list *next;
73 };
74
75 static struct codec_list *codeclist_top = NULL;
76
77 /* ================================================= */
78 /* function prototypes of the master/slave interface */
79 /* ================================================= */
80
81 struct videocodec *
82 videocodec_attach (struct videocodec_master *master)
83 {
84         struct codec_list *h = codeclist_top;
85         struct attached_list *a, *ptr;
86         struct videocodec *codec;
87         int res;
88
89         if (!master) {
90                 dprintk(1, KERN_ERR "videocodec_attach: no data\n");
91                 return NULL;
92         }
93
94         dprintk(2,
95                 "videocodec_attach: '%s', type: %x, flags %lx, magic %lx\n",
96                 master->name, master->type, master->flags, master->magic);
97
98         if (!h) {
99                 dprintk(1,
100                         KERN_ERR
101                         "videocodec_attach: no device available\n");
102                 return NULL;
103         }
104
105         while (h) {
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",
110                                 h->codec->name);
111                         codec =
112                             kmalloc(sizeof(struct videocodec), GFP_KERNEL);
113                         if (!codec) {
114                                 dprintk(1,
115                                         KERN_ERR
116                                         "videocodec_attach: no mem\n");
117                                 return NULL;
118                         }
119                         memcpy(codec, h->codec, sizeof(struct videocodec));
120
121                         snprintf(codec->name, sizeof(codec->name),
122                                  "%s[%d]", codec->name, h->attached);
123                         codec->master_data = master;
124                         res = codec->setup(codec);
125                         if (res == 0) {
126                                 dprintk(3, "videocodec_attach '%s'\n",
127                                         codec->name);
128                                 ptr = (struct attached_list *)
129                                     kmalloc(sizeof(struct attached_list),
130                                             GFP_KERNEL);
131                                 if (!ptr) {
132                                         dprintk(1,
133                                                 KERN_ERR
134                                                 "videocodec_attach: no memory\n");
135                                         kfree(codec);
136                                         return NULL;
137                                 }
138                                 memset(ptr, 0,
139                                        sizeof(struct attached_list));
140                                 ptr->codec = codec;
141
142 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
143                                 MOD_INC_USE_COUNT;
144 #else
145                                 if (!try_module_get(THIS_MODULE)) {
146                                         dprintk(1,
147                                                 KERN_ERR
148                                                 "videocodec: failed to increment usecount\n");
149                                         kfree(codec);
150                                         kfree(ptr);
151                                         return NULL;
152                                 }
153 #endif
154
155                                 a = h->list;
156                                 if (!a) {
157                                         h->list = ptr;
158                                         dprintk(4,
159                                                 "videocodec: first element\n");
160                                 } else {
161                                         while (a->next)
162                                                 a = a->next;    // find end
163                                         a->next = ptr;
164                                         dprintk(4,
165                                                 "videocodec: in after '%s'\n",
166                                                 h->codec->name);
167                                 }
168
169                                 h->attached += 1;
170                                 return codec;
171                         } else {
172                                 kfree(codec);
173                         }
174                 }
175                 h = h->next;
176         }
177
178         dprintk(1, KERN_ERR "videocodec_attach: no codec found!\n");
179         return NULL;
180 }
181
182 int
183 videocodec_detach (struct videocodec *codec)
184 {
185         struct codec_list *h = codeclist_top;
186         struct attached_list *a, *prev;
187         int res;
188
189         if (!codec) {
190                 dprintk(1, KERN_ERR "videocodec_detach: no data\n");
191                 return -EINVAL;
192         }
193
194         dprintk(2,
195                 "videocodec_detach: '%s', type: %x, flags %lx, magic %lx\n",
196                 codec->name, codec->type, codec->flags, codec->magic);
197
198         if (!h) {
199                 dprintk(1,
200                         KERN_ERR "videocodec_detach: no device left...\n");
201                 return -ENXIO;
202         }
203
204         while (h) {
205                 a = h->list;
206                 prev = NULL;
207                 while (a) {
208                         if (codec == a->codec) {
209                                 res = a->codec->unset(a->codec);
210                                 if (res >= 0) {
211                                         dprintk(3,
212                                                 "videocodec_detach: '%s'\n",
213                                                 a->codec->name);
214                                         a->codec->master_data = NULL;
215                                 } else {
216                                         dprintk(1,
217                                                 KERN_ERR
218                                                 "videocodec_detach: '%s'\n",
219                                                 a->codec->name);
220                                         a->codec->master_data = NULL;
221                                 }
222                                 if (prev == NULL) {
223                                         h->list = a->next;
224                                         dprintk(4,
225                                                 "videocodec: delete first\n");
226                                 } else {
227                                         prev->next = a->next;
228                                         dprintk(4,
229                                                 "videocodec: delete middle\n");
230                                 }
231                                 kfree(a->codec);
232                                 kfree(a);
233                                 h->attached -= 1;
234
235 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
236                                 MOD_DEC_USE_COUNT;
237 #else
238                                 module_put(THIS_MODULE);
239 #endif
240
241                                 return 0;
242                         }
243                         prev = a;
244                         a = a->next;
245                 }
246                 h = h->next;
247         }
248
249         dprintk(1, KERN_ERR "videocodec_detach: given codec not found!\n");
250         return -EINVAL;
251 }
252
253 int
254 videocodec_register (const struct videocodec *codec)
255 {
256         struct codec_list *ptr, *h = codeclist_top;
257
258         if (!codec) {
259                 dprintk(1, KERN_ERR "videocodec_register: no data!\n");
260                 return -EINVAL;
261         }
262
263         dprintk(2,
264                 "videocodec: register '%s', type: %x, flags %lx, magic %lx\n",
265                 codec->name, codec->type, codec->flags, codec->magic);
266
267         ptr =
268             (struct codec_list *) kmalloc(sizeof(struct codec_list),
269                                           GFP_KERNEL);
270         if (!ptr) {
271                 dprintk(1, KERN_ERR "videocodec_register: no memory\n");
272                 return -ENOMEM;
273         }
274         memset(ptr, 0, sizeof(struct codec_list));
275         ptr->codec = codec;
276
277 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
278         MOD_INC_USE_COUNT;
279 #else
280         if (!try_module_get(THIS_MODULE)) {
281                 dprintk(1,
282                         KERN_ERR
283                         "videocodec: failed to increment module count\n");
284                 kfree(ptr);
285                 return -ENODEV;
286         }
287 #endif
288
289         if (!h) {
290                 codeclist_top = ptr;
291                 dprintk(4, "videocodec: hooked in as first element\n");
292         } else {
293                 while (h->next)
294                         h = h->next;    // find the end
295                 h->next = ptr;
296                 dprintk(4, "videocodec: hooked in after '%s'\n",
297                         h->codec->name);
298         }
299
300         return 0;
301 }
302
303 int
304 videocodec_unregister (const struct videocodec *codec)
305 {
306         struct codec_list *prev = NULL, *h = codeclist_top;
307
308         if (!codec) {
309                 dprintk(1, KERN_ERR "videocodec_unregister: no data!\n");
310                 return -EINVAL;
311         }
312
313         dprintk(2,
314                 "videocodec: unregister '%s', type: %x, flags %lx, magic %lx\n",
315                 codec->name, codec->type, codec->flags, codec->magic);
316
317         if (!h) {
318                 dprintk(1,
319                         KERN_ERR
320                         "videocodec_unregister: no device left...\n");
321                 return -ENXIO;
322         }
323
324         while (h) {
325                 if (codec == h->codec) {
326                         if (h->attached) {
327                                 dprintk(1,
328                                         KERN_ERR
329                                         "videocodec: '%s' is used\n",
330                                         h->codec->name);
331                                 return -EBUSY;
332                         }
333                         dprintk(3, "videocodec: unregister '%s' is ok.\n",
334                                 h->codec->name);
335                         if (prev == NULL) {
336                                 codeclist_top = h->next;
337                                 dprintk(4,
338                                         "videocodec: delete first element\n");
339                         } else {
340                                 prev->next = h->next;
341                                 dprintk(4,
342                                         "videocodec: delete middle element\n");
343                         }
344                         kfree(h);
345
346 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
347                         MOD_DEC_USE_COUNT;
348 #else
349                         module_put(THIS_MODULE);
350 #endif
351
352                         return 0;
353                 }
354                 prev = h;
355                 h = h->next;
356         }
357
358         dprintk(1,
359                 KERN_ERR
360                 "videocodec_unregister: given codec not found!\n");
361         return -EINVAL;
362 }
363
364 #ifdef CONFIG_PROC_FS
365 /* ============ */
366 /* procfs stuff */
367 /* ============ */
368
369 static char *videocodec_buf = NULL;
370 static int videocodec_bufsize = 0;
371
372 static int
373 videocodec_build_table (void)
374 {
375         struct codec_list *h = codeclist_top;
376         struct attached_list *a;
377         int i = 0, size;
378
379         // sum up amount of slaves plus their attached masters
380         while (h) {
381                 i += h->attached + 1;
382                 h = h->next;
383         }
384 #define LINESIZE 100
385         size = LINESIZE * (i + 1);
386
387         dprintk(3, "videocodec_build table: %d entries, %d bytes\n", i,
388                 size);
389
390         if (videocodec_buf)
391                 kfree(videocodec_buf);
392         videocodec_buf = (char *) kmalloc(size, GFP_KERNEL);
393
394         i = 0;
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");
398
399         h = codeclist_top;
400         while (h) {
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);
407                 a = h->list;
408                 while (a) {
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,
417                                       a->codec->name);
418                         a = a->next;
419                 }
420                 h = h->next;
421         }
422
423         return i;
424 }
425
426 //The definition:
427 //typedef int (read_proc_t)(char *page, char **start, off_t off,
428 //                          int count, int *eof, void *data);
429
430 static int
431 videocodec_info (char  *buffer,
432                  char **buffer_location,
433                  off_t  offset,
434                  int    buffer_length,
435                  int   *eof,
436                  void  *data)
437 {
438         int size;
439
440         dprintk(3, "videocodec_info: offset: %ld, len %d / size %d\n",
441                 offset, buffer_length, videocodec_bufsize);
442
443         if (offset == 0) {
444                 videocodec_bufsize = videocodec_build_table();
445         }
446         if ((offset < 0) || (offset >= videocodec_bufsize)) {
447                 dprintk(4,
448                         "videocodec_info: call delivers no result, return 0\n");
449                 *eof = 1;
450                 return 0;
451         }
452
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;
457         } else {
458                 dprintk(4, "videocodec_info: last reading of %ld bytes\n",
459                         videocodec_bufsize - offset);
460                 size = videocodec_bufsize - offset;
461                 *eof = 1;
462         }
463
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; */
468
469         return size;
470 }
471 #endif
472
473 /* ===================== */
474 /* hook in driver module */
475 /* ===================== */
476 static int __init
477 videocodec_init (void)
478 {
479 #ifdef CONFIG_PROC_FS
480         static struct proc_dir_entry *videocodec_proc_entry;
481 #endif
482
483         printk(KERN_INFO "Linux video codec intermediate layer: %s\n",
484                VIDEOCODEC_VERSION);
485
486 #ifdef CONFIG_PROC_FS
487         videocodec_buf = NULL;
488         videocodec_bufsize = 0;
489
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;
496         } else {
497                 dprintk(1, KERN_ERR "videocodec: can't init procfs.\n");
498         }
499 #endif
500         return 0;
501 }
502
503 static void __exit
504 videocodec_exit (void)
505 {
506 #ifdef CONFIG_PROC_FS
507         remove_proc_entry("videocodecs", 0);
508         if (videocodec_buf)
509                 kfree(videocodec_buf);
510 #endif
511 }
512
513 EXPORT_SYMBOL(videocodec_attach);
514 EXPORT_SYMBOL(videocodec_detach);
515 EXPORT_SYMBOL(videocodec_register);
516 EXPORT_SYMBOL(videocodec_unregister);
517
518 module_init(videocodec_init);
519 module_exit(videocodec_exit);
520
521 MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
522 MODULE_DESCRIPTION("Intermediate API module for video codecs "
523                    VIDEOCODEC_VERSION);
524 MODULE_LICENSE("GPL");