1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * VIDEO MOTION CODECs internal API for video devices
5 * Interface for MJPEG (and maybe later MPEG/WAVELETS) codec's
6 * bound to a master device.
8 * (c) 2002 Wolfgang Scherr <scherr@net4you.at>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
17 #include "videocodec.h"
19 static int videocodec_debug;
20 module_param(videocodec_debug, int, 0);
21 MODULE_PARM_DESC(videocodec_debug, "Debug level (0-4)");
23 #define dprintk(num, format, args...) \
25 if (videocodec_debug >= num) \
26 printk(format, ##args); \
29 struct attached_list {
30 struct videocodec *codec;
31 struct attached_list *next;
35 const struct videocodec *codec;
37 struct attached_list *list;
38 struct codec_list *next;
41 static struct codec_list *codeclist_top;
43 /* ================================================= */
44 /* function prototypes of the master/slave interface */
45 /* ================================================= */
47 struct videocodec *videocodec_attach(struct videocodec_master *master)
49 struct codec_list *h = codeclist_top;
50 struct attached_list *a, *ptr;
51 struct videocodec *codec;
55 pr_err("%s: no data\n", __func__);
59 dprintk(2, "%s: '%s', flags %lx, magic %lx\n", __func__,
60 master->name, master->flags, master->magic);
63 pr_err("%s: no device available\n", __func__);
68 // attach only if the slave has at least the flags
69 // expected by the master
70 if ((master->flags & h->codec->flags) == master->flags) {
71 dprintk(4, "%s: try '%s'\n", __func__, h->codec->name);
73 codec = kmemdup(h->codec, sizeof(struct videocodec), GFP_KERNEL);
77 res = strlen(codec->name);
78 snprintf(codec->name + res, sizeof(codec->name) - res, "[%d]", h->attached);
79 codec->master_data = master;
80 res = codec->setup(codec);
82 dprintk(3, "%s: '%s'\n", __func__, codec->name);
83 ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
91 dprintk(4, "videocodec: first element\n");
94 a = a->next; // find end
96 dprintk(4, "videocodec: in after '%s'\n", h->codec->name);
108 pr_err("%s: no codec found!\n", __func__);
116 int videocodec_detach(struct videocodec *codec)
118 struct codec_list *h = codeclist_top;
119 struct attached_list *a, *prev;
123 pr_err("%s: no data\n", __func__);
127 dprintk(2, "%s: '%s', type: %x, flags %lx, magic %lx\n", __func__,
128 codec->name, codec->type, codec->flags, codec->magic);
131 pr_err("%s: no device left...\n", __func__);
139 if (codec == a->codec) {
140 res = a->codec->unset(a->codec);
142 dprintk(3, "%s: '%s'\n", __func__, a->codec->name);
143 a->codec->master_data = NULL;
145 pr_err("%s: '%s'\n", __func__, a->codec->name);
146 a->codec->master_data = NULL;
150 dprintk(4, "videocodec: delete first\n");
152 prev->next = a->next;
153 dprintk(4, "videocodec: delete middle\n");
166 pr_err("%s: given codec not found!\n", __func__);
170 int videocodec_register(const struct videocodec *codec)
172 struct codec_list *ptr, *h = codeclist_top;
175 pr_err("%s: no data!\n", __func__);
180 "videocodec: register '%s', type: %x, flags %lx, magic %lx\n",
181 codec->name, codec->type, codec->flags, codec->magic);
183 ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
190 dprintk(4, "videocodec: hooked in as first element\n");
193 h = h->next; // find the end
195 dprintk(4, "videocodec: hooked in after '%s'\n",
202 int videocodec_unregister(const struct videocodec *codec)
204 struct codec_list *prev = NULL, *h = codeclist_top;
207 pr_err("%s: no data!\n", __func__);
212 "videocodec: unregister '%s', type: %x, flags %lx, magic %lx\n",
213 codec->name, codec->type, codec->flags, codec->magic);
216 pr_err("%s: no device left...\n", __func__);
221 if (codec == h->codec) {
223 pr_err("videocodec: '%s' is used\n", h->codec->name);
226 dprintk(3, "videocodec: unregister '%s' is ok.\n",
229 codeclist_top = h->next;
231 "videocodec: delete first element\n");
233 prev->next = h->next;
235 "videocodec: delete middle element\n");
244 pr_err("%s: given codec not found!\n", __func__);
248 int videocodec_debugfs_show(struct seq_file *m)
250 struct codec_list *h = codeclist_top;
251 struct attached_list *a;
253 seq_printf(m, "<S>lave or attached <M>aster name type flags magic ");
254 seq_printf(m, "(connected as)\n");
257 seq_printf(m, "S %32s %04x %08lx %08lx (TEMPLATE)\n",
258 h->codec->name, h->codec->type,
259 h->codec->flags, h->codec->magic);
262 seq_printf(m, "M %32s %04x %08lx %08lx (%s)\n",
263 a->codec->master_data->name,
264 a->codec->master_data->type,
265 a->codec->master_data->flags,
266 a->codec->master_data->magic,