1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * VIDEO MOTION CODECs internal API for video devices
4 *
5 * Interface for MJPEG (and maybe later MPEG/WAVELETS) codec's
6 * bound to a master device.
7 *
8 * (c) 2002 Wolfgang Scherr <scherr@net4you.at>
9 */
10
11 #define VIDEOCODEC_VERSION "v0.2"
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18
19 // kernel config is here (procfs flag)
20
21 #ifdef CONFIG_PROC_FS
22 #include <linux/proc_fs.h>
23 #include <linux/seq_file.h>
24 #include <linux/uaccess.h>
25 #endif
26
27 #include "videocodec.h"
28
29 static int debug;
30 module_param(debug, int, 0);
31 MODULE_PARM_DESC(debug, "Debug level (0-4)");
32
33 #define dprintk(num, format, args...) \
34 do { \
35 if (debug >= num) \
36 printk(format, ##args); \
37 } while (0)
38
39 struct attached_list {
40 struct videocodec *codec;
41 struct attached_list *next;
42 };
43
44 struct codec_list {
45 const struct videocodec *codec;
46 int attached;
47 struct attached_list *list;
48 struct codec_list *next;
49 };
50
51 static struct codec_list *codeclist_top;
52
53 /* ================================================= */
54 /* function prototypes of the master/slave interface */
55 /* ================================================= */
56
videocodec_attach(struct videocodec_master * master)57 struct videocodec *videocodec_attach(struct videocodec_master *master)
58 {
59 struct codec_list *h = codeclist_top;
60 struct attached_list *a, *ptr;
61 struct videocodec *codec;
62 int res;
63
64 if (!master) {
65 pr_err("%s: no data\n", __func__);
66 return NULL;
67 }
68
69 dprintk(2, "%s: '%s', flags %lx, magic %lx\n", __func__,
70 master->name, master->flags, master->magic);
71
72 if (!h) {
73 pr_err("%s: no device available\n", __func__);
74 return NULL;
75 }
76
77 while (h) {
78 // attach only if the slave has at least the flags
79 // expected by the master
80 if ((master->flags & h->codec->flags) == master->flags) {
81 dprintk(4, "%s: try '%s'\n", __func__, h->codec->name);
82
83 if (!try_module_get(h->codec->owner))
84 return NULL;
85
86 codec = kmemdup(h->codec, sizeof(struct videocodec), GFP_KERNEL);
87 if (!codec)
88 goto out_module_put;
89
90 res = strlen(codec->name);
91 snprintf(codec->name + res, sizeof(codec->name) - res, "[%d]", h->attached);
92 codec->master_data = master;
93 res = codec->setup(codec);
94 if (res == 0) {
95 dprintk(3, "%s: '%s'\n", __func__, codec->name);
96 ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
97 if (!ptr)
98 goto out_kfree;
99 ptr->codec = codec;
100
101 a = h->list;
102 if (!a) {
103 h->list = ptr;
104 dprintk(4, "videocodec: first element\n");
105 } else {
106 while (a->next)
107 a = a->next; // find end
108 a->next = ptr;
109 dprintk(4, "videocodec: in after '%s'\n", h->codec->name);
110 }
111
112 h->attached += 1;
113 return codec;
114 } else {
115 kfree(codec);
116 }
117 }
118 h = h->next;
119 }
120
121 pr_err("%s: no codec found!\n", __func__);
122 return NULL;
123
124 out_module_put:
125 module_put(h->codec->owner);
126 out_kfree:
127 kfree(codec);
128 return NULL;
129 }
130 EXPORT_SYMBOL(videocodec_attach);
131
videocodec_detach(struct videocodec * codec)132 int videocodec_detach(struct videocodec *codec)
133 {
134 struct codec_list *h = codeclist_top;
135 struct attached_list *a, *prev;
136 int res;
137
138 if (!codec) {
139 pr_err("%s: no data\n", __func__);
140 return -EINVAL;
141 }
142
143 dprintk(2, "%s: '%s', type: %x, flags %lx, magic %lx\n", __func__,
144 codec->name, codec->type, codec->flags, codec->magic);
145
146 if (!h) {
147 pr_err("%s: no device left...\n", __func__);
148 return -ENXIO;
149 }
150
151 while (h) {
152 a = h->list;
153 prev = NULL;
154 while (a) {
155 if (codec == a->codec) {
156 res = a->codec->unset(a->codec);
157 if (res >= 0) {
158 dprintk(3, "%s: '%s'\n", __func__, a->codec->name);
159 a->codec->master_data = NULL;
160 } else {
161 pr_err("%s: '%s'\n", __func__, a->codec->name);
162 a->codec->master_data = NULL;
163 }
164 if (!prev) {
165 h->list = a->next;
166 dprintk(4, "videocodec: delete first\n");
167 } else {
168 prev->next = a->next;
169 dprintk(4, "videocodec: delete middle\n");
170 }
171 module_put(a->codec->owner);
172 kfree(a->codec);
173 kfree(a);
174 h->attached -= 1;
175 return 0;
176 }
177 prev = a;
178 a = a->next;
179 }
180 h = h->next;
181 }
182
183 pr_err("%s: given codec not found!\n", __func__);
184 return -EINVAL;
185 }
186 EXPORT_SYMBOL(videocodec_detach);
187
videocodec_register(const struct videocodec * codec)188 int videocodec_register(const struct videocodec *codec)
189 {
190 struct codec_list *ptr, *h = codeclist_top;
191
192 if (!codec) {
193 pr_err("%s: no data!\n", __func__);
194 return -EINVAL;
195 }
196
197 dprintk(2,
198 "videocodec: register '%s', type: %x, flags %lx, magic %lx\n",
199 codec->name, codec->type, codec->flags, codec->magic);
200
201 ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
202 if (!ptr)
203 return -ENOMEM;
204 ptr->codec = codec;
205
206 if (!h) {
207 codeclist_top = ptr;
208 dprintk(4, "videocodec: hooked in as first element\n");
209 } else {
210 while (h->next)
211 h = h->next; // find the end
212 h->next = ptr;
213 dprintk(4, "videocodec: hooked in after '%s'\n",
214 h->codec->name);
215 }
216
217 return 0;
218 }
219 EXPORT_SYMBOL(videocodec_register);
220
videocodec_unregister(const struct videocodec * codec)221 int videocodec_unregister(const struct videocodec *codec)
222 {
223 struct codec_list *prev = NULL, *h = codeclist_top;
224
225 if (!codec) {
226 pr_err("%s: no data!\n", __func__);
227 return -EINVAL;
228 }
229
230 dprintk(2,
231 "videocodec: unregister '%s', type: %x, flags %lx, magic %lx\n",
232 codec->name, codec->type, codec->flags, codec->magic);
233
234 if (!h) {
235 pr_err("%s: no device left...\n", __func__);
236 return -ENXIO;
237 }
238
239 while (h) {
240 if (codec == h->codec) {
241 if (h->attached) {
242 pr_err("videocodec: '%s' is used\n", h->codec->name);
243 return -EBUSY;
244 }
245 dprintk(3, "videocodec: unregister '%s' is ok.\n",
246 h->codec->name);
247 if (!prev) {
248 codeclist_top = h->next;
249 dprintk(4,
250 "videocodec: delete first element\n");
251 } else {
252 prev->next = h->next;
253 dprintk(4,
254 "videocodec: delete middle element\n");
255 }
256 kfree(h);
257 return 0;
258 }
259 prev = h;
260 h = h->next;
261 }
262
263 pr_err("%s: given codec not found!\n", __func__);
264 return -EINVAL;
265 }
266 EXPORT_SYMBOL(videocodec_unregister);
267
268 #ifdef CONFIG_PROC_FS
proc_videocodecs_show(struct seq_file * m,void * v)269 static int proc_videocodecs_show(struct seq_file *m, void *v)
270 {
271 struct codec_list *h = codeclist_top;
272 struct attached_list *a;
273
274 seq_printf(m, "<S>lave or attached <M>aster name type flags magic ");
275 seq_printf(m, "(connected as)\n");
276
277 while (h) {
278 seq_printf(m, "S %32s %04x %08lx %08lx (TEMPLATE)\n",
279 h->codec->name, h->codec->type,
280 h->codec->flags, h->codec->magic);
281 a = h->list;
282 while (a) {
283 seq_printf(m, "M %32s %04x %08lx %08lx (%s)\n",
284 a->codec->master_data->name,
285 a->codec->master_data->type,
286 a->codec->master_data->flags,
287 a->codec->master_data->magic,
288 a->codec->name);
289 a = a->next;
290 }
291 h = h->next;
292 }
293
294 return 0;
295 }
296 #endif
297
298 /* ===================== */
299 /* hook in driver module */
300 /* ===================== */
videocodec_init(void)301 static int __init videocodec_init(void)
302 {
303 #ifdef CONFIG_PROC_FS
304 static struct proc_dir_entry *videocodec_proc_entry;
305 #endif
306
307 pr_info("Linux video codec intermediate layer: %s\n", VIDEOCODEC_VERSION);
308
309 #ifdef CONFIG_PROC_FS
310 videocodec_proc_entry = proc_create_single("videocodecs", 0, NULL, proc_videocodecs_show);
311 if (!videocodec_proc_entry)
312 pr_err("videocodec: can't init procfs.\n");
313 #endif
314 return 0;
315 }
316
videocodec_exit(void)317 static void __exit videocodec_exit(void)
318 {
319 #ifdef CONFIG_PROC_FS
320 remove_proc_entry("videocodecs", NULL);
321 #endif
322 }
323
324 module_init(videocodec_init);
325 module_exit(videocodec_exit);
326
327 MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
328 MODULE_DESCRIPTION("Intermediate API module for video codecs "
329 VIDEOCODEC_VERSION);
330 MODULE_LICENSE("GPL");
331