btcx-risc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/module.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. static unsigned int btcx_debug;
  27. module_param(btcx_debug, int, 0644);
  28. MODULE_PARM_DESC(btcx_debug,"debug messages, default is 0 (no)");
  29. #define dprintk(fmt, arg...) do { \
  30. if (btcx_debug) \
  31. printk(KERN_DEBUG pr_fmt("%s: " fmt), \
  32. __func__, ##arg); \
  33. } while (0)
  34. /* ---------------------------------------------------------- */
  35. /* allocate/free risc memory */
  36. static int memcnt;
  37. void btcx_riscmem_free(struct pci_dev *pci,
  38. struct btcx_riscmem *risc)
  39. {
  40. if (NULL == risc->cpu)
  41. return;
  42. memcnt--;
  43. dprintk("btcx: riscmem free [%d] dma=%lx\n",
  44. memcnt, (unsigned long)risc->dma);
  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. __le32 *cpu;
  53. dma_addr_t dma = 0;
  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. memcnt++;
  64. dprintk("btcx: riscmem alloc [%d] dma=%lx cpu=%p size=%d\n",
  65. memcnt, (unsigned long)dma, cpu, size);
  66. }
  67. memset(risc->cpu,0,risc->size);
  68. return 0;
  69. }
  70. /* ---------------------------------------------------------- */
  71. /* screen overlay helpers */
  72. int
  73. btcx_screen_clips(int swidth, int sheight, struct v4l2_rect *win,
  74. struct v4l2_clip *clips, unsigned int n)
  75. {
  76. if (win->left < 0) {
  77. /* left */
  78. clips[n].c.left = 0;
  79. clips[n].c.top = 0;
  80. clips[n].c.width = -win->left;
  81. clips[n].c.height = win->height;
  82. n++;
  83. }
  84. if (win->left + win->width > swidth) {
  85. /* right */
  86. clips[n].c.left = swidth - win->left;
  87. clips[n].c.top = 0;
  88. clips[n].c.width = win->width - clips[n].c.left;
  89. clips[n].c.height = win->height;
  90. n++;
  91. }
  92. if (win->top < 0) {
  93. /* top */
  94. clips[n].c.left = 0;
  95. clips[n].c.top = 0;
  96. clips[n].c.width = win->width;
  97. clips[n].c.height = -win->top;
  98. n++;
  99. }
  100. if (win->top + win->height > sheight) {
  101. /* bottom */
  102. clips[n].c.left = 0;
  103. clips[n].c.top = sheight - win->top;
  104. clips[n].c.width = win->width;
  105. clips[n].c.height = win->height - clips[n].c.top;
  106. n++;
  107. }
  108. return n;
  109. }
  110. int
  111. btcx_align(struct v4l2_rect *win, struct v4l2_clip *clips, unsigned int n, int mask)
  112. {
  113. s32 nx,nw,dx;
  114. unsigned int i;
  115. /* fixup window */
  116. nx = (win->left + mask) & ~mask;
  117. nw = (win->width) & ~mask;
  118. if (nx + nw > win->left + win->width)
  119. nw -= mask+1;
  120. dx = nx - win->left;
  121. win->left = nx;
  122. win->width = nw;
  123. dprintk("btcx: window align %dx%d+%d+%d [dx=%d]\n",
  124. win->width, win->height, win->left, win->top, dx);
  125. /* fixup clips */
  126. for (i = 0; i < n; i++) {
  127. nx = (clips[i].c.left-dx) & ~mask;
  128. nw = (clips[i].c.width) & ~mask;
  129. if (nx + nw < clips[i].c.left-dx + clips[i].c.width)
  130. nw += mask+1;
  131. clips[i].c.left = nx;
  132. clips[i].c.width = nw;
  133. dprintk("btcx: clip align %dx%d+%d+%d\n",
  134. clips[i].c.width, clips[i].c.height,
  135. clips[i].c.left, clips[i].c.top);
  136. }
  137. return 0;
  138. }
  139. void
  140. btcx_sort_clips(struct v4l2_clip *clips, unsigned int nclips)
  141. {
  142. int i,j,n;
  143. if (nclips < 2)
  144. return;
  145. for (i = nclips-2; i >= 0; i--) {
  146. for (n = 0, j = 0; j <= i; j++) {
  147. if (clips[j].c.left > clips[j+1].c.left) {
  148. swap(clips[j], clips[j + 1]);
  149. n++;
  150. }
  151. }
  152. if (0 == n)
  153. break;
  154. }
  155. }
  156. void
  157. btcx_calc_skips(int line, int width, int *maxy,
  158. struct btcx_skiplist *skips, unsigned int *nskips,
  159. const struct v4l2_clip *clips, unsigned int nclips)
  160. {
  161. unsigned int clip,skip;
  162. int end, maxline;
  163. skip=0;
  164. maxline = 9999;
  165. for (clip = 0; clip < nclips; clip++) {
  166. /* sanity checks */
  167. if (clips[clip].c.left + clips[clip].c.width <= 0)
  168. continue;
  169. if (clips[clip].c.left > (signed)width)
  170. break;
  171. /* vertical range */
  172. if (line > clips[clip].c.top+clips[clip].c.height-1)
  173. continue;
  174. if (line < clips[clip].c.top) {
  175. if (maxline > clips[clip].c.top-1)
  176. maxline = clips[clip].c.top-1;
  177. continue;
  178. }
  179. if (maxline > clips[clip].c.top+clips[clip].c.height-1)
  180. maxline = clips[clip].c.top+clips[clip].c.height-1;
  181. /* horizontal range */
  182. if (0 == skip || clips[clip].c.left > skips[skip-1].end) {
  183. /* new one */
  184. skips[skip].start = clips[clip].c.left;
  185. if (skips[skip].start < 0)
  186. skips[skip].start = 0;
  187. skips[skip].end = clips[clip].c.left + clips[clip].c.width;
  188. if (skips[skip].end > width)
  189. skips[skip].end = width;
  190. skip++;
  191. } else {
  192. /* overlaps -- expand last one */
  193. end = clips[clip].c.left + clips[clip].c.width;
  194. if (skips[skip-1].end < end)
  195. skips[skip-1].end = end;
  196. if (skips[skip-1].end > width)
  197. skips[skip-1].end = width;
  198. }
  199. }
  200. *nskips = skip;
  201. *maxy = maxline;
  202. if (btcx_debug) {
  203. dprintk("btcx: skips line %d-%d:", line, maxline);
  204. for (skip = 0; skip < *nskips; skip++) {
  205. pr_cont(" %d-%d", skips[skip].start, skips[skip].end);
  206. }
  207. pr_cont("\n");
  208. }
  209. }