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