1 // SPDX-License-Identifier: GPL-2.0-or-later
6 bt848/bt878/cx2388x risc code generator.
8 (c) 2000-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/pci.h>
18 #include <linux/interrupt.h>
19 #include <linux/videodev2.h>
20 #include <linux/pgtable.h>
23 #include "btcx-risc.h"
25 static unsigned int btcx_debug
;
26 module_param(btcx_debug
, int, 0644);
27 MODULE_PARM_DESC(btcx_debug
,"debug messages, default is 0 (no)");
29 #define dprintk(fmt, arg...) do { \
31 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
36 /* ---------------------------------------------------------- */
37 /* allocate/free risc memory */
41 void btcx_riscmem_free(struct pci_dev
*pci
,
42 struct btcx_riscmem
*risc
)
44 if (NULL
== risc
->cpu
)
48 dprintk("btcx: riscmem free [%d] dma=%lx\n",
49 memcnt
, (unsigned long)risc
->dma
);
51 dma_free_coherent(&pci
->dev
, risc
->size
, risc
->cpu
, risc
->dma
);
52 memset(risc
,0,sizeof(*risc
));
55 int btcx_riscmem_alloc(struct pci_dev
*pci
,
56 struct btcx_riscmem
*risc
,
62 if (NULL
!= risc
->cpu
&& risc
->size
< size
)
63 btcx_riscmem_free(pci
,risc
);
64 if (NULL
== risc
->cpu
) {
65 cpu
= dma_alloc_coherent(&pci
->dev
, size
, &dma
, GFP_KERNEL
);
73 dprintk("btcx: riscmem alloc [%d] dma=%lx cpu=%p size=%d\n",
74 memcnt
, (unsigned long)dma
, cpu
, size
);
79 /* ---------------------------------------------------------- */
80 /* screen overlay helpers */
83 btcx_screen_clips(int swidth
, int sheight
, struct v4l2_rect
*win
,
84 struct v4l2_clip
*clips
, unsigned int n
)
90 clips
[n
].c
.width
= -win
->left
;
91 clips
[n
].c
.height
= win
->height
;
94 if (win
->left
+ win
->width
> swidth
) {
96 clips
[n
].c
.left
= swidth
- win
->left
;
98 clips
[n
].c
.width
= win
->width
- clips
[n
].c
.left
;
99 clips
[n
].c
.height
= win
->height
;
106 clips
[n
].c
.width
= win
->width
;
107 clips
[n
].c
.height
= -win
->top
;
110 if (win
->top
+ win
->height
> sheight
) {
113 clips
[n
].c
.top
= sheight
- win
->top
;
114 clips
[n
].c
.width
= win
->width
;
115 clips
[n
].c
.height
= win
->height
- clips
[n
].c
.top
;
122 btcx_align(struct v4l2_rect
*win
, struct v4l2_clip
*clips
, unsigned int n
, int mask
)
128 nx
= (win
->left
+ mask
) & ~mask
;
129 nw
= (win
->width
) & ~mask
;
130 if (nx
+ nw
> win
->left
+ win
->width
)
135 dprintk("btcx: window align %dx%d+%d+%d [dx=%d]\n",
136 win
->width
, win
->height
, win
->left
, win
->top
, dx
);
139 for (i
= 0; i
< n
; i
++) {
140 nx
= (clips
[i
].c
.left
-dx
) & ~mask
;
141 nw
= (clips
[i
].c
.width
) & ~mask
;
142 if (nx
+ nw
< clips
[i
].c
.left
-dx
+ clips
[i
].c
.width
)
144 clips
[i
].c
.left
= nx
;
145 clips
[i
].c
.width
= nw
;
146 dprintk("btcx: clip align %dx%d+%d+%d\n",
147 clips
[i
].c
.width
, clips
[i
].c
.height
,
148 clips
[i
].c
.left
, clips
[i
].c
.top
);
154 btcx_sort_clips(struct v4l2_clip
*clips
, unsigned int nclips
)
160 for (i
= nclips
-2; i
>= 0; i
--) {
161 for (n
= 0, j
= 0; j
<= i
; j
++) {
162 if (clips
[j
].c
.left
> clips
[j
+1].c
.left
) {
163 swap(clips
[j
], clips
[j
+ 1]);
173 btcx_calc_skips(int line
, int width
, int *maxy
,
174 struct btcx_skiplist
*skips
, unsigned int *nskips
,
175 const struct v4l2_clip
*clips
, unsigned int nclips
)
177 unsigned int clip
,skip
;
182 for (clip
= 0; clip
< nclips
; clip
++) {
185 if (clips
[clip
].c
.left
+ clips
[clip
].c
.width
<= 0)
187 if (clips
[clip
].c
.left
> (signed)width
)
191 if (line
> clips
[clip
].c
.top
+clips
[clip
].c
.height
-1)
193 if (line
< clips
[clip
].c
.top
) {
194 if (maxline
> clips
[clip
].c
.top
-1)
195 maxline
= clips
[clip
].c
.top
-1;
198 if (maxline
> clips
[clip
].c
.top
+clips
[clip
].c
.height
-1)
199 maxline
= clips
[clip
].c
.top
+clips
[clip
].c
.height
-1;
201 /* horizontal range */
202 if (0 == skip
|| clips
[clip
].c
.left
> skips
[skip
-1].end
) {
204 skips
[skip
].start
= clips
[clip
].c
.left
;
205 if (skips
[skip
].start
< 0)
206 skips
[skip
].start
= 0;
207 skips
[skip
].end
= clips
[clip
].c
.left
+ clips
[clip
].c
.width
;
208 if (skips
[skip
].end
> width
)
209 skips
[skip
].end
= width
;
212 /* overlaps -- expand last one */
213 end
= clips
[clip
].c
.left
+ clips
[clip
].c
.width
;
214 if (skips
[skip
-1].end
< end
)
215 skips
[skip
-1].end
= end
;
216 if (skips
[skip
-1].end
> width
)
217 skips
[skip
-1].end
= width
;
224 dprintk("btcx: skips line %d-%d:", line
, maxline
);
225 for (skip
= 0; skip
< *nskips
; skip
++) {
226 pr_cont(" %d-%d", skips
[skip
].start
, skips
[skip
].end
);