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/init.h>
  19. #include <linux/pci.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/videodev2.h>
  22. #include <asm/page.h>
  23. #include <asm/pgtable.h>
  24. #include "btcx-risc.h"
  25. MODULE_DESCRIPTION("some code shared by bttv and cx88xx drivers");
  26. MODULE_AUTHOR("Gerd Knorr");
  27. MODULE_LICENSE("GPL");
  28. static unsigned int debug;
  29. module_param(debug, int, 0644);
  30. MODULE_PARM_DESC(debug,"debug messages, default is 0 (no)");
  31. /* ---------------------------------------------------------- */
  32. /* allocate/free risc memory */
  33. static int memcnt;
  34. void btcx_riscmem_free(struct pci_dev *pci,
  35. struct btcx_riscmem *risc)
  36. {
  37. if (NULL == risc->cpu)
  38. return;
  39. if (debug) {
  40. memcnt--;
  41. printk("btcx: riscmem free [%d] dma=%lx\n",
  42. memcnt, (unsigned long)risc->dma);
  43. }
  44. pci_free_consistent(pci, risc->size, risc->cpu, risc->dma);
  45. memset(risc,0,sizeof(*risc));
  46. }
  47. int btcx_riscmem_alloc(struct pci_dev *pci,
  48. struct btcx_riscmem *risc,
  49. unsigned int size)
  50. {
  51. __le32 *cpu;
  52. dma_addr_t dma = 0;
  53. if (NULL != risc->cpu && risc->size < size)
  54. btcx_riscmem_free(pci,risc);
  55. if (NULL == risc->cpu) {
  56. cpu = pci_alloc_consistent(pci, size, &dma);
  57. if (NULL == cpu)
  58. return -ENOMEM;
  59. risc->cpu = cpu;
  60. risc->dma = dma;
  61. risc->size = size;
  62. if (debug) {
  63. memcnt++;
  64. printk("btcx: riscmem alloc [%d] dma=%lx cpu=%p size=%d\n",
  65. memcnt, (unsigned long)dma, cpu, size);
  66. }
  67. }
  68. memset(risc->cpu,0,risc->size);
  69. return 0;
  70. }
  71. /* ---------------------------------------------------------- */
  72. /* screen overlay helpers */
  73. int
  74. btcx_screen_clips(int swidth, int sheight, struct v4l2_rect *win,
  75. struct v4l2_clip *clips, unsigned int n)
  76. {
  77. if (win->left < 0) {
  78. /* left */
  79. clips[n].c.left = 0;
  80. clips[n].c.top = 0;
  81. clips[n].c.width = -win->left;
  82. clips[n].c.height = win->height;
  83. n++;
  84. }
  85. if (win->left + win->width > swidth) {
  86. /* right */
  87. clips[n].c.left = swidth - win->left;
  88. clips[n].c.top = 0;
  89. clips[n].c.width = win->width - clips[n].c.left;
  90. clips[n].c.height = win->height;
  91. n++;
  92. }
  93. if (win->top < 0) {
  94. /* top */
  95. clips[n].c.left = 0;
  96. clips[n].c.top = 0;
  97. clips[n].c.width = win->width;
  98. clips[n].c.height = -win->top;
  99. n++;
  100. }
  101. if (win->top + win->height > sheight) {
  102. /* bottom */
  103. clips[n].c.left = 0;
  104. clips[n].c.top = sheight - win->top;
  105. clips[n].c.width = win->width;
  106. clips[n].c.height = win->height - clips[n].c.top;
  107. n++;
  108. }
  109. return n;
  110. }
  111. int
  112. btcx_align(struct v4l2_rect *win, struct v4l2_clip *clips, unsigned int n, int mask)
  113. {
  114. s32 nx,nw,dx;
  115. unsigned int i;
  116. /* fixup window */
  117. nx = (win->left + mask) & ~mask;
  118. nw = (win->width) & ~mask;
  119. if (nx + nw > win->left + win->width)
  120. nw -= mask+1;
  121. dx = nx - win->left;
  122. win->left = nx;
  123. win->width = nw;
  124. if (debug)
  125. printk(KERN_DEBUG "btcx: window align %dx%d+%d+%d [dx=%d]\n",
  126. win->width, win->height, win->left, win->top, dx);
  127. /* fixup clips */
  128. for (i = 0; i < n; i++) {
  129. nx = (clips[i].c.left-dx) & ~mask;
  130. nw = (clips[i].c.width) & ~mask;
  131. if (nx + nw < clips[i].c.left-dx + clips[i].c.width)
  132. nw += mask+1;
  133. clips[i].c.left = nx;
  134. clips[i].c.width = nw;
  135. if (debug)
  136. printk(KERN_DEBUG "btcx: clip align %dx%d+%d+%d\n",
  137. clips[i].c.width, clips[i].c.height,
  138. clips[i].c.left, clips[i].c.top);
  139. }
  140. return 0;
  141. }
  142. void
  143. btcx_sort_clips(struct v4l2_clip *clips, unsigned int nclips)
  144. {
  145. struct v4l2_clip swap;
  146. int i,j,n;
  147. if (nclips < 2)
  148. return;
  149. for (i = nclips-2; i >= 0; i--) {
  150. for (n = 0, j = 0; j <= i; j++) {
  151. if (clips[j].c.left > clips[j+1].c.left) {
  152. swap = clips[j];
  153. clips[j] = clips[j+1];
  154. clips[j+1] = swap;
  155. n++;
  156. }
  157. }
  158. if (0 == n)
  159. break;
  160. }
  161. }
  162. void
  163. btcx_calc_skips(int line, int width, int *maxy,
  164. struct btcx_skiplist *skips, unsigned int *nskips,
  165. const struct v4l2_clip *clips, unsigned int nclips)
  166. {
  167. unsigned int clip,skip;
  168. int end, maxline;
  169. skip=0;
  170. maxline = 9999;
  171. for (clip = 0; clip < nclips; clip++) {
  172. /* sanity checks */
  173. if (clips[clip].c.left + clips[clip].c.width <= 0)
  174. continue;
  175. if (clips[clip].c.left > (signed)width)
  176. break;
  177. /* vertical range */
  178. if (line > clips[clip].c.top+clips[clip].c.height-1)
  179. continue;
  180. if (line < clips[clip].c.top) {
  181. if (maxline > clips[clip].c.top-1)
  182. maxline = clips[clip].c.top-1;
  183. continue;
  184. }
  185. if (maxline > clips[clip].c.top+clips[clip].c.height-1)
  186. maxline = clips[clip].c.top+clips[clip].c.height-1;
  187. /* horizontal range */
  188. if (0 == skip || clips[clip].c.left > skips[skip-1].end) {
  189. /* new one */
  190. skips[skip].start = clips[clip].c.left;
  191. if (skips[skip].start < 0)
  192. skips[skip].start = 0;
  193. skips[skip].end = clips[clip].c.left + clips[clip].c.width;
  194. if (skips[skip].end > width)
  195. skips[skip].end = width;
  196. skip++;
  197. } else {
  198. /* overlaps -- expand last one */
  199. end = clips[clip].c.left + clips[clip].c.width;
  200. if (skips[skip-1].end < end)
  201. skips[skip-1].end = end;
  202. if (skips[skip-1].end > width)
  203. skips[skip-1].end = width;
  204. }
  205. }
  206. *nskips = skip;
  207. *maxy = maxline;
  208. if (debug) {
  209. printk(KERN_DEBUG "btcx: skips line %d-%d:",line,maxline);
  210. for (skip = 0; skip < *nskips; skip++) {
  211. printk(" %d-%d",skips[skip].start,skips[skip].end);
  212. }
  213. printk("\n");
  214. }
  215. }
  216. /* ---------------------------------------------------------- */
  217. EXPORT_SYMBOL(btcx_riscmem_alloc);
  218. EXPORT_SYMBOL(btcx_riscmem_free);
  219. EXPORT_SYMBOL(btcx_screen_clips);
  220. EXPORT_SYMBOL(btcx_align);
  221. EXPORT_SYMBOL(btcx_sort_clips);
  222. EXPORT_SYMBOL(btcx_calc_skips);
  223. /*
  224. * Local variables:
  225. * c-basic-offset: 8
  226. * End:
  227. */