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