videocodec.c 9.1 KB

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