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