V4L/DVB: saa7164: move dereference under NULL check
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / saa7164 / saa7164-buffer.c
blobddd25d32723dc0436f2477a641afaa0d75d9f2d3
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
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.
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
15 * GNU General Public License for more details.
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.
22 #include <linux/slab.h>
24 #include "saa7164.h"
26 /* The PCI address space for buffer handling looks like this:
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 | +----------------+
63 | +----------------+
64 +->| pt[1] TS data |
65 | +----------------+
66 | etc
69 /* Allocate a new buffer structure and associated PCI space in bytes.
70 * len must be a multiple of sizeof(u64)
72 struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_tsport *port,
73 u32 len)
75 struct saa7164_buffer *buf = 0;
76 struct saa7164_dev *dev = port->dev;
77 int i;
79 if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
80 log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
81 goto ret;
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;
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;
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;
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;
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);
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);
117 /* Format the Page Table Entries to point into the data buffer */
118 for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
120 *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
124 goto ret;
126 fail2:
127 pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
128 fail1:
129 kfree(buf);
131 buf = 0;
132 ret:
133 return buf;
136 int saa7164_buffer_dealloc(struct saa7164_tsport *port,
137 struct saa7164_buffer *buf)
139 struct saa7164_dev *dev;
141 if (!buf || !port)
142 return SAA_ERR_BAD_PARAMETER;
143 dev = port->dev;
145 dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n", __func__, buf);
147 if (buf->flags != SAA7164_BUFFER_FREE)
148 log_warn(" freeing a non-free buffer\n");
150 pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
151 pci_free_consistent(port->dev->pci, buf->pt_size, buf->pt_cpu,
152 buf->pt_dma);
154 kfree(buf);
156 return SAA_OK;