videocodec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. #include <linux/config.h>
  37. #ifdef CONFIG_PROC_FS
  38. #include <linux/proc_fs.h>
  39. #include <asm/uaccess.h>
  40. #endif
  41. #include "videocodec.h"
  42. static int debug = 0;
  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', type: %x, flags %lx, magic %lx\n",
  77. master->name, master->type, 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. /* ============ */
  283. /* procfs stuff */
  284. /* ============ */
  285. static char *videocodec_buf = NULL;
  286. static int videocodec_bufsize = 0;
  287. static int
  288. videocodec_build_table (void)
  289. {
  290. struct codec_list *h = codeclist_top;
  291. struct attached_list *a;
  292. int i = 0, size;
  293. // sum up amount of slaves plus their attached masters
  294. while (h) {
  295. i += h->attached + 1;
  296. h = h->next;
  297. }
  298. #define LINESIZE 100
  299. size = LINESIZE * (i + 1);
  300. dprintk(3, "videocodec_build table: %d entries, %d bytes\n", i,
  301. size);
  302. kfree(videocodec_buf);
  303. videocodec_buf = (char *) kmalloc(size, GFP_KERNEL);
  304. i = 0;
  305. i += scnprintf(videocodec_buf + i, size - 1,
  306. "<S>lave or attached <M>aster name type flags magic ");
  307. i += scnprintf(videocodec_buf + i, size -i - 1, "(connected as)\n");
  308. h = codeclist_top;
  309. while (h) {
  310. if (i > (size - LINESIZE))
  311. break; // security check
  312. i += scnprintf(videocodec_buf + i, size -i -1,
  313. "S %32s %04x %08lx %08lx (TEMPLATE)\n",
  314. h->codec->name, h->codec->type,
  315. h->codec->flags, h->codec->magic);
  316. a = h->list;
  317. while (a) {
  318. if (i > (size - LINESIZE))
  319. break; // security check
  320. i += scnprintf(videocodec_buf + i, size -i -1,
  321. "M %32s %04x %08lx %08lx (%s)\n",
  322. a->codec->master_data->name,
  323. a->codec->master_data->type,
  324. a->codec->master_data->flags,
  325. a->codec->master_data->magic,
  326. a->codec->name);
  327. a = a->next;
  328. }
  329. h = h->next;
  330. }
  331. return i;
  332. }
  333. //The definition:
  334. //typedef int (read_proc_t)(char *page, char **start, off_t off,
  335. // int count, int *eof, void *data);
  336. static int
  337. videocodec_info (char *buffer,
  338. char **buffer_location,
  339. off_t offset,
  340. int buffer_length,
  341. int *eof,
  342. void *data)
  343. {
  344. int size;
  345. dprintk(3, "videocodec_info: offset: %ld, len %d / size %d\n",
  346. offset, buffer_length, videocodec_bufsize);
  347. if (offset == 0) {
  348. videocodec_bufsize = videocodec_build_table();
  349. }
  350. if ((offset < 0) || (offset >= videocodec_bufsize)) {
  351. dprintk(4,
  352. "videocodec_info: call delivers no result, return 0\n");
  353. *eof = 1;
  354. return 0;
  355. }
  356. if (buffer_length < (videocodec_bufsize - offset)) {
  357. dprintk(4, "videocodec_info: %ld needed, %d got\n",
  358. videocodec_bufsize - offset, buffer_length);
  359. size = buffer_length;
  360. } else {
  361. dprintk(4, "videocodec_info: last reading of %ld bytes\n",
  362. videocodec_bufsize - offset);
  363. size = videocodec_bufsize - offset;
  364. *eof = 1;
  365. }
  366. memcpy(buffer, videocodec_buf + offset, size);
  367. /* doesn't work... */
  368. /* copy_to_user(buffer, videocodec_buf+offset, size); */
  369. /* *buffer_location = videocodec_buf+offset; */
  370. return size;
  371. }
  372. #endif
  373. /* ===================== */
  374. /* hook in driver module */
  375. /* ===================== */
  376. static int __init
  377. videocodec_init (void)
  378. {
  379. #ifdef CONFIG_PROC_FS
  380. static struct proc_dir_entry *videocodec_proc_entry;
  381. #endif
  382. printk(KERN_INFO "Linux video codec intermediate layer: %s\n",
  383. VIDEOCODEC_VERSION);
  384. #ifdef CONFIG_PROC_FS
  385. videocodec_buf = NULL;
  386. videocodec_bufsize = 0;
  387. videocodec_proc_entry = create_proc_entry("videocodecs", 0, NULL);
  388. if (videocodec_proc_entry) {
  389. videocodec_proc_entry->read_proc = videocodec_info;
  390. videocodec_proc_entry->write_proc = NULL;
  391. videocodec_proc_entry->data = NULL;
  392. videocodec_proc_entry->owner = THIS_MODULE;
  393. } else {
  394. dprintk(1, KERN_ERR "videocodec: can't init procfs.\n");
  395. }
  396. #endif
  397. return 0;
  398. }
  399. static void __exit
  400. videocodec_exit (void)
  401. {
  402. #ifdef CONFIG_PROC_FS
  403. remove_proc_entry("videocodecs", NULL);
  404. kfree(videocodec_buf);
  405. #endif
  406. }
  407. EXPORT_SYMBOL(videocodec_attach);
  408. EXPORT_SYMBOL(videocodec_detach);
  409. EXPORT_SYMBOL(videocodec_register);
  410. EXPORT_SYMBOL(videocodec_unregister);
  411. module_init(videocodec_init);
  412. module_exit(videocodec_exit);
  413. MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
  414. MODULE_DESCRIPTION("Intermediate API module for video codecs "
  415. VIDEOCODEC_VERSION);
  416. MODULE_LICENSE("GPL");