videocodec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 = (struct attached_list *)
  109. kmalloc(sizeof(struct attached_list),
  110. GFP_KERNEL);
  111. if (!ptr) {
  112. dprintk(1,
  113. KERN_ERR
  114. "videocodec_attach: no memory\n");
  115. goto out_kfree;
  116. }
  117. memset(ptr, 0,
  118. sizeof(struct attached_list));
  119. ptr->codec = codec;
  120. a = h->list;
  121. if (!a) {
  122. h->list = ptr;
  123. dprintk(4,
  124. "videocodec: first element\n");
  125. } else {
  126. while (a->next)
  127. a = a->next; // find end
  128. a->next = ptr;
  129. dprintk(4,
  130. "videocodec: in after '%s'\n",
  131. h->codec->name);
  132. }
  133. h->attached += 1;
  134. return codec;
  135. } else {
  136. kfree(codec);
  137. }
  138. }
  139. h = h->next;
  140. }
  141. dprintk(1, KERN_ERR "videocodec_attach: no codec found!\n");
  142. return NULL;
  143. out_module_put:
  144. module_put(h->codec->owner);
  145. out_kfree:
  146. kfree(codec);
  147. return NULL;
  148. }
  149. int
  150. videocodec_detach (struct videocodec *codec)
  151. {
  152. struct codec_list *h = codeclist_top;
  153. struct attached_list *a, *prev;
  154. int res;
  155. if (!codec) {
  156. dprintk(1, KERN_ERR "videocodec_detach: no data\n");
  157. return -EINVAL;
  158. }
  159. dprintk(2,
  160. "videocodec_detach: '%s', type: %x, flags %lx, magic %lx\n",
  161. codec->name, codec->type, codec->flags, codec->magic);
  162. if (!h) {
  163. dprintk(1,
  164. KERN_ERR "videocodec_detach: no device left...\n");
  165. return -ENXIO;
  166. }
  167. while (h) {
  168. a = h->list;
  169. prev = NULL;
  170. while (a) {
  171. if (codec == a->codec) {
  172. res = a->codec->unset(a->codec);
  173. if (res >= 0) {
  174. dprintk(3,
  175. "videocodec_detach: '%s'\n",
  176. a->codec->name);
  177. a->codec->master_data = NULL;
  178. } else {
  179. dprintk(1,
  180. KERN_ERR
  181. "videocodec_detach: '%s'\n",
  182. a->codec->name);
  183. a->codec->master_data = NULL;
  184. }
  185. if (prev == NULL) {
  186. h->list = a->next;
  187. dprintk(4,
  188. "videocodec: delete first\n");
  189. } else {
  190. prev->next = a->next;
  191. dprintk(4,
  192. "videocodec: delete middle\n");
  193. }
  194. module_put(a->codec->owner);
  195. kfree(a->codec);
  196. kfree(a);
  197. h->attached -= 1;
  198. return 0;
  199. }
  200. prev = a;
  201. a = a->next;
  202. }
  203. h = h->next;
  204. }
  205. dprintk(1, KERN_ERR "videocodec_detach: given codec not found!\n");
  206. return -EINVAL;
  207. }
  208. int
  209. videocodec_register (const struct videocodec *codec)
  210. {
  211. struct codec_list *ptr, *h = codeclist_top;
  212. if (!codec) {
  213. dprintk(1, KERN_ERR "videocodec_register: no data!\n");
  214. return -EINVAL;
  215. }
  216. dprintk(2,
  217. "videocodec: register '%s', type: %x, flags %lx, magic %lx\n",
  218. codec->name, codec->type, codec->flags, codec->magic);
  219. ptr =
  220. (struct codec_list *) kmalloc(sizeof(struct codec_list),
  221. GFP_KERNEL);
  222. if (!ptr) {
  223. dprintk(1, KERN_ERR "videocodec_register: no memory\n");
  224. return -ENOMEM;
  225. }
  226. memset(ptr, 0, sizeof(struct codec_list));
  227. ptr->codec = codec;
  228. if (!h) {
  229. codeclist_top = ptr;
  230. dprintk(4, "videocodec: hooked in as first element\n");
  231. } else {
  232. while (h->next)
  233. h = h->next; // find the end
  234. h->next = ptr;
  235. dprintk(4, "videocodec: hooked in after '%s'\n",
  236. h->codec->name);
  237. }
  238. return 0;
  239. }
  240. int
  241. videocodec_unregister (const struct videocodec *codec)
  242. {
  243. struct codec_list *prev = NULL, *h = codeclist_top;
  244. if (!codec) {
  245. dprintk(1, KERN_ERR "videocodec_unregister: no data!\n");
  246. return -EINVAL;
  247. }
  248. dprintk(2,
  249. "videocodec: unregister '%s', type: %x, flags %lx, magic %lx\n",
  250. codec->name, codec->type, codec->flags, codec->magic);
  251. if (!h) {
  252. dprintk(1,
  253. KERN_ERR
  254. "videocodec_unregister: no device left...\n");
  255. return -ENXIO;
  256. }
  257. while (h) {
  258. if (codec == h->codec) {
  259. if (h->attached) {
  260. dprintk(1,
  261. KERN_ERR
  262. "videocodec: '%s' is used\n",
  263. h->codec->name);
  264. return -EBUSY;
  265. }
  266. dprintk(3, "videocodec: unregister '%s' is ok.\n",
  267. h->codec->name);
  268. if (prev == NULL) {
  269. codeclist_top = h->next;
  270. dprintk(4,
  271. "videocodec: delete first element\n");
  272. } else {
  273. prev->next = h->next;
  274. dprintk(4,
  275. "videocodec: delete middle element\n");
  276. }
  277. kfree(h);
  278. return 0;
  279. }
  280. prev = h;
  281. h = h->next;
  282. }
  283. dprintk(1,
  284. KERN_ERR
  285. "videocodec_unregister: given codec not found!\n");
  286. return -EINVAL;
  287. }
  288. #ifdef CONFIG_PROC_FS
  289. /* ============ */
  290. /* procfs stuff */
  291. /* ============ */
  292. static char *videocodec_buf = NULL;
  293. static int videocodec_bufsize = 0;
  294. static int
  295. videocodec_build_table (void)
  296. {
  297. struct codec_list *h = codeclist_top;
  298. struct attached_list *a;
  299. int i = 0, size;
  300. // sum up amount of slaves plus their attached masters
  301. while (h) {
  302. i += h->attached + 1;
  303. h = h->next;
  304. }
  305. #define LINESIZE 100
  306. size = LINESIZE * (i + 1);
  307. dprintk(3, "videocodec_build table: %d entries, %d bytes\n", i,
  308. size);
  309. if (videocodec_buf)
  310. kfree(videocodec_buf);
  311. videocodec_buf = (char *) kmalloc(size, GFP_KERNEL);
  312. i = 0;
  313. i += scnprintf(videocodec_buf + i, size - 1,
  314. "<S>lave or attached <M>aster name type flags magic ");
  315. i += scnprintf(videocodec_buf + i, size -i - 1, "(connected as)\n");
  316. h = codeclist_top;
  317. while (h) {
  318. if (i > (size - LINESIZE))
  319. break; // security check
  320. i += scnprintf(videocodec_buf + i, size -i -1,
  321. "S %32s %04x %08lx %08lx (TEMPLATE)\n",
  322. h->codec->name, h->codec->type,
  323. h->codec->flags, h->codec->magic);
  324. a = h->list;
  325. while (a) {
  326. if (i > (size - LINESIZE))
  327. break; // security check
  328. i += scnprintf(videocodec_buf + i, size -i -1,
  329. "M %32s %04x %08lx %08lx (%s)\n",
  330. a->codec->master_data->name,
  331. a->codec->master_data->type,
  332. a->codec->master_data->flags,
  333. a->codec->master_data->magic,
  334. a->codec->name);
  335. a = a->next;
  336. }
  337. h = h->next;
  338. }
  339. return i;
  340. }
  341. //The definition:
  342. //typedef int (read_proc_t)(char *page, char **start, off_t off,
  343. // int count, int *eof, void *data);
  344. static int
  345. videocodec_info (char *buffer,
  346. char **buffer_location,
  347. off_t offset,
  348. int buffer_length,
  349. int *eof,
  350. void *data)
  351. {
  352. int size;
  353. dprintk(3, "videocodec_info: offset: %ld, len %d / size %d\n",
  354. offset, buffer_length, videocodec_bufsize);
  355. if (offset == 0) {
  356. videocodec_bufsize = videocodec_build_table();
  357. }
  358. if ((offset < 0) || (offset >= videocodec_bufsize)) {
  359. dprintk(4,
  360. "videocodec_info: call delivers no result, return 0\n");
  361. *eof = 1;
  362. return 0;
  363. }
  364. if (buffer_length < (videocodec_bufsize - offset)) {
  365. dprintk(4, "videocodec_info: %ld needed, %d got\n",
  366. videocodec_bufsize - offset, buffer_length);
  367. size = buffer_length;
  368. } else {
  369. dprintk(4, "videocodec_info: last reading of %ld bytes\n",
  370. videocodec_bufsize - offset);
  371. size = videocodec_bufsize - offset;
  372. *eof = 1;
  373. }
  374. memcpy(buffer, videocodec_buf + offset, size);
  375. /* doesn't work... */
  376. /* copy_to_user(buffer, videocodec_buf+offset, size); */
  377. /* *buffer_location = videocodec_buf+offset; */
  378. return size;
  379. }
  380. #endif
  381. /* ===================== */
  382. /* hook in driver module */
  383. /* ===================== */
  384. static int __init
  385. videocodec_init (void)
  386. {
  387. #ifdef CONFIG_PROC_FS
  388. static struct proc_dir_entry *videocodec_proc_entry;
  389. #endif
  390. printk(KERN_INFO "Linux video codec intermediate layer: %s\n",
  391. VIDEOCODEC_VERSION);
  392. #ifdef CONFIG_PROC_FS
  393. videocodec_buf = NULL;
  394. videocodec_bufsize = 0;
  395. videocodec_proc_entry = create_proc_entry("videocodecs", 0, NULL);
  396. if (videocodec_proc_entry) {
  397. videocodec_proc_entry->read_proc = videocodec_info;
  398. videocodec_proc_entry->write_proc = NULL;
  399. videocodec_proc_entry->data = NULL;
  400. videocodec_proc_entry->owner = THIS_MODULE;
  401. } else {
  402. dprintk(1, KERN_ERR "videocodec: can't init procfs.\n");
  403. }
  404. #endif
  405. return 0;
  406. }
  407. static void __exit
  408. videocodec_exit (void)
  409. {
  410. #ifdef CONFIG_PROC_FS
  411. remove_proc_entry("videocodecs", NULL);
  412. if (videocodec_buf)
  413. kfree(videocodec_buf);
  414. #endif
  415. }
  416. EXPORT_SYMBOL(videocodec_attach);
  417. EXPORT_SYMBOL(videocodec_detach);
  418. EXPORT_SYMBOL(videocodec_register);
  419. EXPORT_SYMBOL(videocodec_unregister);
  420. module_init(videocodec_init);
  421. module_exit(videocodec_exit);
  422. MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
  423. MODULE_DESCRIPTION("Intermediate API module for video codecs "
  424. VIDEOCODEC_VERSION);
  425. MODULE_LICENSE("GPL");