btcx-risc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. btcx-risc.c
  3. bt848/bt878/cx2388x risc code generator.
  4. (c) 2000-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/init.h>
  20. #include <linux/pci.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/videodev2.h>
  23. #include <asm/page.h>
  24. #include <asm/pgtable.h>
  25. #include "btcx-risc.h"
  26. MODULE_DESCRIPTION("some code shared by bttv and cx88xx drivers");
  27. MODULE_AUTHOR("Gerd Knorr");
  28. MODULE_LICENSE("GPL");
  29. static unsigned int debug;
  30. module_param(debug, int, 0644);
  31. MODULE_PARM_DESC(debug,"debug messages, default is 0 (no)");
  32. /* ---------------------------------------------------------- */
  33. /* allocate/free risc memory */
  34. static int memcnt;
  35. void btcx_riscmem_free(struct pci_dev *pci,
  36. struct btcx_riscmem *risc)
  37. {
  38. if (NULL == risc->cpu)
  39. return;
  40. if (debug) {
  41. memcnt--;
  42. printk("btcx: riscmem free [%d] dma=%lx\n",
  43. memcnt, (unsigned long)risc->dma);
  44. }
  45. pci_free_consistent(pci, risc->size, risc->cpu, risc->dma);
  46. memset(risc,0,sizeof(*risc));
  47. }
  48. int btcx_riscmem_alloc(struct pci_dev *pci,
  49. struct btcx_riscmem *risc,
  50. unsigned int size)
  51. {
  52. u32 *cpu;
  53. dma_addr_t dma;
  54. if (NULL != risc->cpu && risc->size < size)
  55. btcx_riscmem_free(pci,risc);
  56. if (NULL == risc->cpu) {
  57. cpu = pci_alloc_consistent(pci, size, &dma);
  58. if (NULL == cpu)
  59. return -ENOMEM;
  60. risc->cpu = cpu;
  61. risc->dma = dma;
  62. risc->size = size;
  63. if (debug) {
  64. memcnt++;
  65. printk("btcx: riscmem alloc [%d] dma=%lx cpu=%p size=%d\n",
  66. memcnt, (unsigned long)dma, cpu, size);
  67. }
  68. }
  69. memset(risc->cpu,0,risc->size);
  70. return 0;
  71. }
  72. /* ---------------------------------------------------------- */
  73. /* screen overlay helpers */
  74. int
  75. btcx_screen_clips(int swidth, int sheight, struct v4l2_rect *win,
  76. struct v4l2_clip *clips, unsigned int n)
  77. {
  78. if (win->left < 0) {
  79. /* left */
  80. clips[n].c.left = 0;
  81. clips[n].c.top = 0;
  82. clips[n].c.width = -win->left;
  83. clips[n].c.height = win->height;
  84. n++;
  85. }
  86. if (win->left + win->width > swidth) {
  87. /* right */
  88. clips[n].c.left = swidth - win->left;
  89. clips[n].c.top = 0;
  90. clips[n].c.width = win->width - clips[n].c.left;
  91. clips[n].c.height = win->height;
  92. n++;
  93. }
  94. if (win->top < 0) {
  95. /* top */
  96. clips[n].c.left = 0;
  97. clips[n].c.top = 0;
  98. clips[n].c.width = win->width;
  99. clips[n].c.height = -win->top;
  100. n++;
  101. }
  102. if (win->top + win->height > sheight) {
  103. /* bottom */
  104. clips[n].c.left = 0;
  105. clips[n].c.top = sheight - win->top;
  106. clips[n].c.width = win->width;
  107. clips[n].c.height = win->height - clips[n].c.top;
  108. n++;
  109. }
  110. return n;
  111. }
  112. int
  113. btcx_align(struct v4l2_rect *win, struct v4l2_clip *clips, unsigned int n, int mask)
  114. {
  115. s32 nx,nw,dx;
  116. unsigned int i;
  117. /* fixup window */
  118. nx = (win->left + mask) & ~mask;
  119. nw = (win->width) & ~mask;
  120. if (nx + nw > win->left + win->width)
  121. nw -= mask+1;
  122. dx = nx - win->left;
  123. win->left = nx;
  124. win->width = nw;
  125. if (debug)
  126. printk(KERN_DEBUG "btcx: window align %dx%d+%d+%d [dx=%d]\n",
  127. win->width, win->height, win->left, win->top, dx);
  128. /* fixup clips */
  129. for (i = 0; i < n; i++) {
  130. nx = (clips[i].c.left-dx) & ~mask;
  131. nw = (clips[i].c.width) & ~mask;
  132. if (nx + nw < clips[i].c.left-dx + clips[i].c.width)
  133. nw += mask+1;
  134. clips[i].c.left = nx;
  135. clips[i].c.width = nw;
  136. if (debug)
  137. printk(KERN_DEBUG "btcx: clip align %dx%d+%d+%d\n",
  138. clips[i].c.width, clips[i].c.height,
  139. clips[i].c.left, clips[i].c.top);
  140. }
  141. return 0;
  142. }
  143. void
  144. btcx_sort_clips(struct v4l2_clip *clips, unsigned int nclips)
  145. {
  146. struct v4l2_clip swap;
  147. int i,j,n;
  148. if (nclips < 2)
  149. return;
  150. for (i = nclips-2; i >= 0; i--) {
  151. for (n = 0, j = 0; j <= i; j++) {
  152. if (clips[j].c.left > clips[j+1].c.left) {
  153. swap = clips[j];
  154. clips[j] = clips[j+1];
  155. clips[j+1] = swap;
  156. n++;
  157. }
  158. }
  159. if (0 == n)
  160. break;
  161. }
  162. }
  163. void
  164. btcx_calc_skips(int line, int width, unsigned int *maxy,
  165. struct btcx_skiplist *skips, unsigned int *nskips,
  166. const struct v4l2_clip *clips, unsigned int nclips)
  167. {
  168. unsigned int clip,skip;
  169. int end,maxline;
  170. skip=0;
  171. maxline = 9999;
  172. for (clip = 0; clip < nclips; clip++) {
  173. /* sanity checks */
  174. if (clips[clip].c.left + clips[clip].c.width <= 0)
  175. continue;
  176. if (clips[clip].c.left > (signed)width)
  177. break;
  178. /* vertical range */
  179. if (line > clips[clip].c.top+clips[clip].c.height-1)
  180. continue;
  181. if (line < clips[clip].c.top) {
  182. if (maxline > clips[clip].c.top-1)
  183. maxline = clips[clip].c.top-1;
  184. continue;
  185. }
  186. if (maxline > clips[clip].c.top+clips[clip].c.height-1)
  187. maxline = clips[clip].c.top+clips[clip].c.height-1;
  188. /* horizontal range */
  189. if (0 == skip || clips[clip].c.left > skips[skip-1].end) {
  190. /* new one */
  191. skips[skip].start = clips[clip].c.left;
  192. if (skips[skip].start < 0)
  193. skips[skip].start = 0;
  194. skips[skip].end = clips[clip].c.left + clips[clip].c.width;
  195. if (skips[skip].end > width)
  196. skips[skip].end = width;
  197. skip++;
  198. } else {
  199. /* overlaps -- expand last one */
  200. end = clips[clip].c.left + clips[clip].c.width;
  201. if (skips[skip-1].end < end)
  202. skips[skip-1].end = end;
  203. if (skips[skip-1].end > width)
  204. skips[skip-1].end = width;
  205. }
  206. }
  207. *nskips = skip;
  208. *maxy = maxline;
  209. if (debug) {
  210. printk(KERN_DEBUG "btcx: skips line %d-%d:",line,maxline);
  211. for (skip = 0; skip < *nskips; skip++) {
  212. printk(" %d-%d",skips[skip].start,skips[skip].end);
  213. }
  214. printk("\n");
  215. }
  216. }
  217. /* ---------------------------------------------------------- */
  218. EXPORT_SYMBOL(btcx_riscmem_alloc);
  219. EXPORT_SYMBOL(btcx_riscmem_free);
  220. EXPORT_SYMBOL(btcx_screen_clips);
  221. EXPORT_SYMBOL(btcx_align);
  222. EXPORT_SYMBOL(btcx_sort_clips);
  223. EXPORT_SYMBOL(btcx_calc_skips);
  224. /*
  225. * Local variables:
  226. * c-basic-offset: 8
  227. * End:
  228. */