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