Rev 537 | 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 | { |
||
613 | giacomo | 54 | if (NULL == risc) |
55 | return; |
||
481 | giacomo | 56 | if (NULL == risc->cpu) |
57 | return; |
||
58 | pci_free_consistent(pci, risc->size, risc->cpu, risc->dma); |
||
613 | giacomo | 59 | vfree((void *)risc->cpu); |
481 | giacomo | 60 | memset(risc,0,sizeof(*risc)); |
61 | if (debug) { |
||
62 | memcnt--; |
||
63 | printk("btcx: riscmem free [%d]\n",memcnt); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | int btcx_riscmem_alloc(struct pci_dev *pci, |
||
68 | struct btcx_riscmem *risc, |
||
69 | unsigned int size) |
||
70 | { |
||
71 | u32 *cpu; |
||
72 | dma_addr_t dma; |
||
73 | |||
74 | if (NULL != risc->cpu && risc->size < size) |
||
75 | btcx_riscmem_free(pci,risc); |
||
76 | if (NULL == risc->cpu) { |
||
77 | cpu = pci_alloc_consistent(pci, size, &dma); |
||
78 | if (NULL == cpu) |
||
79 | return -ENOMEM; |
||
80 | risc->cpu = cpu; |
||
81 | risc->dma = dma; |
||
82 | risc->size = size; |
||
83 | if (debug) { |
||
84 | memcnt++; |
||
85 | printk("btcx: riscmem alloc size=%d [%d]\n",size,memcnt); |
||
86 | } |
||
87 | } |
||
88 | memset(risc->cpu,0,risc->size); |
||
89 | return 0; |
||
90 | } |
||
91 | |||
92 | /* ---------------------------------------------------------- */ |
||
93 | /* screen overlay helpers */ |
||
94 | |||
95 | int |
||
96 | btcx_screen_clips(int swidth, int sheight, struct v4l2_rect *win, |
||
97 | struct v4l2_clip *clips, unsigned int n) |
||
98 | { |
||
99 | if (win->left < 0) { |
||
100 | /* left */ |
||
101 | clips[n].c.left = 0; |
||
102 | clips[n].c.top = 0; |
||
103 | clips[n].c.width = -win->left; |
||
104 | clips[n].c.height = win->height; |
||
105 | n++; |
||
106 | } |
||
107 | if (win->left + win->width > swidth) { |
||
108 | /* right */ |
||
109 | clips[n].c.left = swidth - win->left; |
||
110 | clips[n].c.top = 0; |
||
111 | clips[n].c.width = win->width - clips[n].c.left; |
||
112 | clips[n].c.height = win->height; |
||
113 | n++; |
||
114 | } |
||
115 | if (win->top < 0) { |
||
116 | /* top */ |
||
117 | clips[n].c.left = 0; |
||
118 | clips[n].c.top = 0; |
||
119 | clips[n].c.width = win->width; |
||
120 | clips[n].c.height = -win->top; |
||
121 | n++; |
||
122 | } |
||
123 | if (win->top + win->height > sheight) { |
||
124 | /* bottom */ |
||
125 | clips[n].c.left = 0; |
||
126 | clips[n].c.top = sheight - win->top; |
||
127 | clips[n].c.width = win->width; |
||
128 | clips[n].c.height = win->height - clips[n].c.top; |
||
129 | n++; |
||
130 | } |
||
131 | return n; |
||
132 | } |
||
133 | |||
134 | int |
||
135 | btcx_align(struct v4l2_rect *win, struct v4l2_clip *clips, unsigned int n, int mask) |
||
136 | { |
||
137 | s32 nx,nw,dx; |
||
138 | unsigned int i; |
||
139 | |||
140 | /* fixup window */ |
||
141 | nx = (win->left + mask) & ~mask; |
||
142 | nw = (win->width) & ~mask; |
||
143 | if (nx + nw > win->left + win->width) |
||
144 | nw -= mask+1; |
||
145 | dx = nx - win->left; |
||
146 | win->left = nx; |
||
147 | win->width = nw; |
||
148 | if (debug) |
||
149 | printk(KERN_DEBUG "btcx: window align %dx%d+%d+%d [dx=%d]\n", |
||
150 | win->width, win->height, win->left, win->top, dx); |
||
151 | |||
152 | /* fixup clips */ |
||
153 | for (i = 0; i < n; i++) { |
||
154 | nx = (clips[i].c.left-dx) & ~mask; |
||
155 | nw = (clips[i].c.width) & ~mask; |
||
156 | if (nx + nw < clips[i].c.left-dx + clips[i].c.width) |
||
157 | nw += mask+1; |
||
158 | clips[i].c.left = nx; |
||
159 | clips[i].c.width = nw; |
||
160 | if (debug) |
||
161 | printk(KERN_DEBUG "btcx: clip align %dx%d+%d+%d\n", |
||
162 | clips[i].c.width, clips[i].c.height, |
||
163 | clips[i].c.left, clips[i].c.top); |
||
164 | } |
||
165 | return 0; |
||
166 | } |
||
167 | |||
168 | void |
||
169 | btcx_sort_clips(struct v4l2_clip *clips, unsigned int nclips) |
||
170 | { |
||
171 | struct v4l2_clip swap; |
||
172 | int i,j,n; |
||
173 | |||
174 | if (nclips < 2) |
||
175 | return; |
||
176 | for (i = nclips-2; i >= 0; i--) { |
||
177 | for (n = 0, j = 0; j <= i; j++) { |
||
178 | if (clips[j].c.left > clips[j+1].c.left) { |
||
179 | swap = clips[j]; |
||
180 | clips[j] = clips[j+1]; |
||
181 | clips[j+1] = swap; |
||
182 | n++; |
||
183 | } |
||
184 | } |
||
185 | if (0 == n) |
||
186 | break; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | void |
||
191 | btcx_calc_skips(int line, int width, unsigned int *maxy, |
||
192 | struct btcx_skiplist *skips, unsigned int *nskips, |
||
193 | const struct v4l2_clip *clips, unsigned int nclips) |
||
194 | { |
||
195 | unsigned int clip,skip; |
||
196 | int end,maxline; |
||
197 | |||
198 | skip=0; |
||
199 | maxline = 9999; |
||
200 | for (clip = 0; clip < nclips; clip++) { |
||
201 | |||
202 | /* sanity checks */ |
||
203 | if (clips[clip].c.left + clips[clip].c.width <= 0) |
||
204 | continue; |
||
205 | if (clips[clip].c.left > (signed)width) |
||
206 | break; |
||
207 | |||
208 | /* vertical range */ |
||
209 | if (line > clips[clip].c.top+clips[clip].c.height-1) |
||
210 | continue; |
||
211 | if (line < clips[clip].c.top) { |
||
212 | if (maxline > clips[clip].c.top-1) |
||
213 | maxline = clips[clip].c.top-1; |
||
214 | continue; |
||
215 | } |
||
216 | if (maxline > clips[clip].c.top+clips[clip].c.height-1) |
||
217 | maxline = clips[clip].c.top+clips[clip].c.height-1; |
||
218 | |||
219 | /* horizontal range */ |
||
220 | if (0 == skip || clips[clip].c.left > skips[skip-1].end) { |
||
221 | /* new one */ |
||
222 | skips[skip].start = clips[clip].c.left; |
||
223 | if (skips[skip].start < 0) |
||
224 | skips[skip].start = 0; |
||
225 | skips[skip].end = clips[clip].c.left + clips[clip].c.width; |
||
226 | if (skips[skip].end > width) |
||
227 | skips[skip].end = width; |
||
228 | skip++; |
||
229 | } else { |
||
230 | /* overlaps -- expand last one */ |
||
231 | end = clips[clip].c.left + clips[clip].c.width; |
||
232 | if (skips[skip-1].end < end) |
||
233 | skips[skip-1].end = end; |
||
234 | if (skips[skip-1].end > width) |
||
235 | skips[skip-1].end = width; |
||
236 | } |
||
237 | } |
||
238 | *nskips = skip; |
||
239 | *maxy = maxline; |
||
240 | |||
241 | if (debug) { |
||
242 | printk(KERN_DEBUG "btcx: skips line %d-%d:",line,maxline); |
||
243 | for (skip = 0; skip < *nskips; skip++) { |
||
244 | printk(" %d-%d",skips[skip].start,skips[skip].end); |
||
245 | } |
||
246 | printk("\n"); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /* ---------------------------------------------------------- */ |
||
251 | |||
252 | EXPORT_SYMBOL(btcx_riscmem_alloc); |
||
253 | EXPORT_SYMBOL(btcx_riscmem_free); |
||
254 | |||
255 | EXPORT_SYMBOL(btcx_screen_clips); |
||
256 | EXPORT_SYMBOL(btcx_align); |
||
257 | EXPORT_SYMBOL(btcx_sort_clips); |
||
258 | EXPORT_SYMBOL(btcx_calc_skips); |
||
259 | |||
260 | /* |
||
261 | * Local variables: |
||
262 | * c-basic-offset: 8 |
||
263 | * End: |
||
264 | */ |