Merge master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / drivers / media / video / btcx-risc.c
1 /*
2     $Id: btcx-risc.c,v 1.6 2005/02/21 13:57:59 kraxel Exp $
3
4     btcx-risc.c
5
6     bt848/bt878/cx2388x risc code generator.
7
8     (c) 2000-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
9
10     This program is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 */
25
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/pci.h>
30 #include <linux/interrupt.h>
31 #include <linux/videodev2.h>
32 #include <asm/page.h>
33 #include <asm/pgtable.h>
34
35 #include "btcx-risc.h"
36
37 MODULE_DESCRIPTION("some code shared by bttv and cx88xx drivers");
38 MODULE_AUTHOR("Gerd Knorr");
39 MODULE_LICENSE("GPL");
40
41 static unsigned int debug = 0;
42 module_param(debug, int, 0644);
43 MODULE_PARM_DESC(debug,"debug messages, default is 0 (no)");
44
45 /* ---------------------------------------------------------- */
46 /* allocate/free risc memory                                  */
47
48 static int memcnt;
49
50 void btcx_riscmem_free(struct pci_dev *pci,
51                        struct btcx_riscmem *risc)
52 {
53         if (NULL == risc->cpu)
54                 return;
55         if (debug) {
56                 memcnt--;
57                 printk("btcx: riscmem free [%d] dma=%lx\n",
58                        memcnt, (unsigned long)risc->dma);
59         }
60         pci_free_consistent(pci, risc->size, risc->cpu, risc->dma);
61         memset(risc,0,sizeof(*risc));
62 }
63
64 int btcx_riscmem_alloc(struct pci_dev *pci,
65                        struct btcx_riscmem *risc,
66                        unsigned int size)
67 {
68         u32 *cpu;
69         dma_addr_t dma;
70
71         if (NULL != risc->cpu && risc->size < size)
72                 btcx_riscmem_free(pci,risc);
73         if (NULL == risc->cpu) {
74                 cpu = pci_alloc_consistent(pci, size, &dma);
75                 if (NULL == cpu)
76                         return -ENOMEM;
77                 risc->cpu  = cpu;
78                 risc->dma  = dma;
79                 risc->size = size;
80                 if (debug) {
81                         memcnt++;
82                         printk("btcx: riscmem alloc [%d] dma=%lx cpu=%p size=%d\n",
83                                memcnt, (unsigned long)dma, cpu, size);
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  */