videocodec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 <asm/uaccess.h>
  39. #endif
  40. #include "videocodec.h"
  41. static int debug = 0;
  42. module_param(debug, int, 0);
  43. MODULE_PARM_DESC(debug, "Debug level (0-4)");
  44. #define dprintk(num, format, args...) \
  45. do { \
  46. if (debug >= num) \
  47. printk(format, ##args); \
  48. } while (0)
  49. struct attached_list {
  50. struct videocodec *codec;
  51. struct attached_list *next;
  52. };
  53. struct codec_list {
  54. const struct videocodec *codec;
  55. int attached;
  56. struct attached_list *list;
  57. struct codec_list *next;
  58. };
  59. static struct codec_list *codeclist_top = NULL;
  60. /* ================================================= */
  61. /* function prototypes of the master/slave interface */
  62. /* ================================================= */
  63. struct videocodec *
  64. videocodec_attach (struct videocodec_master *master)
  65. {
  66. struct codec_list *h = codeclist_top;
  67. struct attached_list *a, *ptr;
  68. struct videocodec *codec;
  69. int res;
  70. if (!master) {
  71. dprintk(1, KERN_ERR "videocodec_attach: no data\n");
  72. return NULL;
  73. }
  74. dprintk(2,
  75. "videocodec_attach: '%s', type: %x, flags %lx, magic %lx\n",
  76. master->name, master->type, master->flags, master->magic);
  77. if (!h) {
  78. dprintk(1,
  79. KERN_ERR
  80. "videocodec_attach: no device available\n");
  81. return NULL;
  82. }
  83. while (h) {
  84. // attach only if the slave has at least the flags
  85. // expected by the master
  86. if ((master->flags & h->codec->flags) == master->flags) {
  87. dprintk(4, "videocodec_attach: try '%s'\n",
  88. h->codec->name);
  89. if (!try_module_get(h->codec->owner))
  90. return NULL;
  91. codec =
  92. kmalloc(sizeof(struct videocodec), GFP_KERNEL);
  93. if (!codec) {
  94. dprintk(1,
  95. KERN_ERR
  96. "videocodec_attach: no mem\n");
  97. goto out_module_put;
  98. }
  99. memcpy(codec, h->codec, sizeof(struct videocodec));
  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. /* ============ */
  282. /* procfs stuff */
  283. /* ============ */
  284. static char *videocodec_buf = NULL;
  285. static int videocodec_bufsize = 0;
  286. static int
  287. videocodec_build_table (void)
  288. {
  289. struct codec_list *h = codeclist_top;
  290. struct attached_list *a;
  291. int i = 0, size;
  292. // sum up amount of slaves plus their attached masters
  293. while (h) {
  294. i += h->attached + 1;
  295. h = h->next;
  296. }
  297. #define LINESIZE 100
  298. size = LINESIZE * (i + 1);
  299. dprintk(3, "videocodec_build table: %d entries, %d bytes\n", i,
  300. size);
  301. kfree(videocodec_buf);
  302. videocodec_buf = kmalloc(size, GFP_KERNEL);
  303. i = 0;
  304. i += scnprintf(videocodec_buf + i, size - 1,
  305. "<S>lave or attached <M>aster name type flags magic ");
  306. i += scnprintf(videocodec_buf + i, size -i - 1, "(connected as)\n");
  307. h = codeclist_top;
  308. while (h) {
  309. if (i > (size - LINESIZE))
  310. break; // security check
  311. i += scnprintf(videocodec_buf + i, size -i -1,
  312. "S %32s %04x %08lx %08lx (TEMPLATE)\n",
  313. h->codec->name, h->codec->type,
  314. h->codec->flags, h->codec->magic);
  315. a = h->list;
  316. while (a) {
  317. if (i > (size - LINESIZE))
  318. break; // security check
  319. i += scnprintf(videocodec_buf + i, size -i -1,
  320. "M %32s %04x %08lx %08lx (%s)\n",
  321. a->codec->master_data->name,
  322. a->codec->master_data->type,
  323. a->codec->master_data->flags,
  324. a->codec->master_data->magic,
  325. a->codec->name);
  326. a = a->next;
  327. }
  328. h = h->next;
  329. }
  330. return i;
  331. }
  332. //The definition:
  333. //typedef int (read_proc_t)(char *page, char **start, off_t off,
  334. // int count, int *eof, void *data);
  335. static int
  336. videocodec_info (char *buffer,
  337. char **buffer_location,
  338. off_t offset,
  339. int buffer_length,
  340. int *eof,
  341. void *data)
  342. {
  343. int size;
  344. dprintk(3, "videocodec_info: offset: %ld, len %d / size %d\n",
  345. offset, buffer_length, videocodec_bufsize);
  346. if (offset == 0) {
  347. videocodec_bufsize = videocodec_build_table();
  348. }
  349. if ((offset < 0) || (offset >= videocodec_bufsize)) {
  350. dprintk(4,
  351. "videocodec_info: call delivers no result, return 0\n");
  352. *eof = 1;
  353. return 0;
  354. }
  355. if (buffer_length < (videocodec_bufsize - offset)) {
  356. dprintk(4, "videocodec_info: %ld needed, %d got\n",
  357. videocodec_bufsize - offset, buffer_length);
  358. size = buffer_length;
  359. } else {
  360. dprintk(4, "videocodec_info: last reading of %ld bytes\n",
  361. videocodec_bufsize - offset);
  362. size = videocodec_bufsize - offset;
  363. *eof = 1;
  364. }
  365. memcpy(buffer, videocodec_buf + offset, size);
  366. /* doesn't work... */
  367. /* copy_to_user(buffer, videocodec_buf+offset, size); */
  368. /* *buffer_location = videocodec_buf+offset; */
  369. return size;
  370. }
  371. #endif
  372. /* ===================== */
  373. /* hook in driver module */
  374. /* ===================== */
  375. static int __init
  376. videocodec_init (void)
  377. {
  378. #ifdef CONFIG_PROC_FS
  379. static struct proc_dir_entry *videocodec_proc_entry;
  380. #endif
  381. printk(KERN_INFO "Linux video codec intermediate layer: %s\n",
  382. VIDEOCODEC_VERSION);
  383. #ifdef CONFIG_PROC_FS
  384. videocodec_buf = NULL;
  385. videocodec_bufsize = 0;
  386. videocodec_proc_entry = create_proc_entry("videocodecs", 0, NULL);
  387. if (videocodec_proc_entry) {
  388. videocodec_proc_entry->read_proc = videocodec_info;
  389. videocodec_proc_entry->write_proc = NULL;
  390. videocodec_proc_entry->data = NULL;
  391. videocodec_proc_entry->owner = THIS_MODULE;
  392. } else {
  393. dprintk(1, KERN_ERR "videocodec: can't init procfs.\n");
  394. }
  395. #endif
  396. return 0;
  397. }
  398. static void __exit
  399. videocodec_exit (void)
  400. {
  401. #ifdef CONFIG_PROC_FS
  402. remove_proc_entry("videocodecs", NULL);
  403. kfree(videocodec_buf);
  404. #endif
  405. }
  406. EXPORT_SYMBOL(videocodec_attach);
  407. EXPORT_SYMBOL(videocodec_detach);
  408. EXPORT_SYMBOL(videocodec_register);
  409. EXPORT_SYMBOL(videocodec_unregister);
  410. module_init(videocodec_init);
  411. module_exit(videocodec_exit);
  412. MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
  413. MODULE_DESCRIPTION("Intermediate API module for video codecs "
  414. VIDEOCODEC_VERSION);
  415. MODULE_LICENSE("GPL");