include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / media / video / saa7164 / saa7164-buffer.c
1 /*
2  *  Driver for the NXP SAA7164 PCIe bridge
3  *
4  *  Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/slab.h>
23
24 #include "saa7164.h"
25
26 /* The PCI address space for buffer handling looks like this:
27
28  +-u32 wide-------------+
29  |                      +
30  +-u64 wide------------------------------------+
31  +                                             +
32  +----------------------+
33  | CurrentBufferPtr     + Pointer to current PCI buffer >-+
34  +----------------------+                                 |
35  | Unused               +                                 |
36  +----------------------+                                 |
37  | Pitch                + = 188 (bytes)                   |
38  +----------------------+                                 |
39  | PCI buffer size      + = pitch * number of lines (312) |
40  +----------------------+                                 |
41  |0| Buf0 Write Offset  +                                 |
42  +----------------------+                                 v
43  |1| Buf1 Write Offset  +                                 |
44  +----------------------+                                 |
45  |2| Buf2 Write Offset  +                                 |
46  +----------------------+                                 |
47  |3| Buf3 Write Offset  +                                 |
48  +----------------------+                                 |
49  ... More write offsets                                   |
50  +---------------------------------------------+          |
51  +0| set of ptrs to PCI pagetables             +          |
52  +---------------------------------------------+          |
53  +1| set of ptrs to PCI pagetables             + <--------+
54  +---------------------------------------------+
55  +2| set of ptrs to PCI pagetables             +
56  +---------------------------------------------+
57  +3| set of ptrs to PCI pagetables             + >--+
58  +---------------------------------------------+    |
59  ... More buffer pointers                           |  +----------------+
60                                                     +->| pt[0] TS data  |
61                                                     |  +----------------+
62                                                     |
63                                                     |  +----------------+
64                                                     +->| pt[1] TS data  |
65                                                     |  +----------------+
66                                                     | etc
67  */
68
69 /* Allocate a new buffer structure and associated PCI space in bytes.
70  * len must be a multiple of sizeof(u64)
71  */
72 struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_tsport *port,
73         u32 len)
74 {
75         struct saa7164_buffer *buf = 0;
76         struct saa7164_dev *dev = port->dev;
77         int i;
78
79         if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
80                 log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
81                 goto ret;
82         }
83
84         buf = kzalloc(sizeof(struct saa7164_buffer), GFP_KERNEL);
85         if (buf == NULL) {
86                 log_warn("%s() SAA_ERR_NO_RESOURCES\n", __func__);
87                 goto ret;
88         }
89
90         buf->port = port;
91         buf->flags = SAA7164_BUFFER_FREE;
92         /* TODO: arg len is being ignored */
93         buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
94         buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
95
96         /* Allocate contiguous memory */
97         buf->cpu = pci_alloc_consistent(port->dev->pci, buf->pci_size,
98                 &buf->dma);
99         if (!buf->cpu)
100                 goto fail1;
101
102         buf->pt_cpu = pci_alloc_consistent(port->dev->pci, buf->pt_size,
103                 &buf->pt_dma);
104         if (!buf->pt_cpu)
105                 goto fail2;
106
107         /* init the buffers to a known pattern, easier during debugging */
108         memset(buf->cpu, 0xff, buf->pci_size);
109         memset(buf->pt_cpu, 0xff, buf->pt_size);
110
111         dprintk(DBGLVL_BUF, "%s()   allocated buffer @ 0x%p\n", __func__, buf);
112         dprintk(DBGLVL_BUF, "  pci_cpu @ 0x%p    dma @ 0x%08lx len = 0x%x\n",
113                 buf->cpu, (long)buf->dma, buf->pci_size);
114         dprintk(DBGLVL_BUF, "   pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
115                 buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
116
117         /* Format the Page Table Entries to point into the data buffer */
118         for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
119
120                 *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
121
122         }
123
124         goto ret;
125
126 fail2:
127         pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
128 fail1:
129         kfree(buf);
130
131         buf = 0;
132 ret:
133         return buf;
134 }
135
136 int saa7164_buffer_dealloc(struct saa7164_tsport *port,
137         struct saa7164_buffer *buf)
138 {
139         struct saa7164_dev *dev = port->dev;
140
141         if ((buf == 0) || (port == 0))
142                 return SAA_ERR_BAD_PARAMETER;
143
144         dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n", __func__, buf);
145
146         if (buf->flags != SAA7164_BUFFER_FREE)
147                 log_warn(" freeing a non-free buffer\n");
148
149         pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
150         pci_free_consistent(port->dev->pci, buf->pt_size, buf->pt_cpu,
151                 buf->pt_dma);
152
153         kfree(buf);
154
155         return SAA_OK;
156 }
157