diff options
Diffstat (limited to 'drivers/media/video/saa7164/saa7164-buffer.c')
-rw-r--r-- | drivers/media/video/saa7164/saa7164-buffer.c | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/drivers/media/video/saa7164/saa7164-buffer.c b/drivers/media/video/saa7164/saa7164-buffer.c new file mode 100644 index 000000000000..4176544ee019 --- /dev/null +++ b/drivers/media/video/saa7164/saa7164-buffer.c | |||
@@ -0,0 +1,158 @@ | |||
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 "saa7164.h" | ||
23 | |||
24 | /* The PCI address space for buffer handling looks like this: | ||
25 | |||
26 | +-u32 wide-------------+ | ||
27 | | + | ||
28 | +-u64 wide------------------------------------+ | ||
29 | + + | ||
30 | +----------------------+ | ||
31 | | CurrentBufferPtr + Pointer to current PCI buffer >-+ | ||
32 | +----------------------+ | | ||
33 | | Unused + | | ||
34 | +----------------------+ | | ||
35 | | Pitch + = 188 (bytes) | | ||
36 | +----------------------+ | | ||
37 | | PCI buffer size + = pitch * number of lines (312) | | ||
38 | +----------------------+ | | ||
39 | |0| Buf0 Write Offset + | | ||
40 | +----------------------+ v | ||
41 | |1| Buf1 Write Offset + | | ||
42 | +----------------------+ | | ||
43 | |2| Buf2 Write Offset + | | ||
44 | +----------------------+ | | ||
45 | |3| Buf3 Write Offset + | | ||
46 | +----------------------+ | | ||
47 | ... More write offsets | | ||
48 | +---------------------------------------------+ | | ||
49 | +0| set of ptrs to PCI pagetables + | | ||
50 | +---------------------------------------------+ | | ||
51 | +1| set of ptrs to PCI pagetables + <--------+ | ||
52 | +---------------------------------------------+ | ||
53 | +2| set of ptrs to PCI pagetables + | ||
54 | +---------------------------------------------+ | ||
55 | +3| set of ptrs to PCI pagetables + >--+ | ||
56 | +---------------------------------------------+ | | ||
57 | ... More buffer pointers | +----------------+ | ||
58 | +->| pt[0] TS data | | ||
59 | | +----------------+ | ||
60 | | | ||
61 | | +----------------+ | ||
62 | +->| pt[1] TS data | | ||
63 | | +----------------+ | ||
64 | | etc | ||
65 | */ | ||
66 | |||
67 | /* Allocate a new buffer structure and associated PCI space in bytes. | ||
68 | * len must be a multiple of sizeof(u64) | ||
69 | */ | ||
70 | struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_tsport *port, | ||
71 | u32 len) | ||
72 | { | ||
73 | struct saa7164_buffer *buf = 0; | ||
74 | struct saa7164_dev *dev = port->dev; | ||
75 | int i; | ||
76 | |||
77 | if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) { | ||
78 | log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__); | ||
79 | goto ret; | ||
80 | } | ||
81 | |||
82 | buf = kzalloc(sizeof(struct saa7164_buffer), GFP_KERNEL); | ||
83 | if (buf == NULL) { | ||
84 | log_warn("%s() SAA_ERR_NO_RESOURCES\n", __func__); | ||
85 | goto ret; | ||
86 | } | ||
87 | |||
88 | buf->port = port; | ||
89 | buf->flags = SAA7164_BUFFER_FREE; | ||
90 | /* TODO: arg len is being ignored */ | ||
91 | buf->pci_size = SAA7164_PT_ENTRIES * 0x1000; | ||
92 | buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000; | ||
93 | |||
94 | /* Allocate contiguous memory */ | ||
95 | buf->cpu = pci_alloc_consistent(port->dev->pci, buf->pci_size, | ||
96 | &buf->dma); | ||
97 | if (!buf->cpu) | ||
98 | goto fail1; | ||
99 | |||
100 | buf->pt_cpu = pci_alloc_consistent(port->dev->pci, buf->pt_size, | ||
101 | &buf->pt_dma); | ||
102 | if (!buf->pt_cpu) | ||
103 | goto fail2; | ||
104 | |||
105 | /* init the buffers to a known pattern, easier during debugging */ | ||
106 | memset(buf->cpu, 0xff, buf->pci_size); | ||
107 | memset(buf->pt_cpu, 0xff, buf->pt_size); | ||
108 | |||
109 | dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p\n", __func__, buf); | ||
110 | dprintk(DBGLVL_BUF, " pci_cpu @ 0x%llx dma @ 0x%llx len = 0x%x\n", | ||
111 | (u64)buf->cpu, (u64)buf->dma, buf->pci_size); | ||
112 | dprintk(DBGLVL_BUF, " pt_cpu @ 0x%llx pt_dma @ 0x%llx len = 0x%x\n", | ||
113 | (u64)buf->pt_cpu, (u64)buf->pt_dma, buf->pt_size); | ||
114 | |||
115 | /* Format the Page Table Entries to point into the data buffer */ | ||
116 | for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) { | ||
117 | |||
118 | *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */ | ||
119 | |||
120 | dprintk(DBGLVL_BUF, " pt[%02d] = 0x%llx -> 0x%llx\n", | ||
121 | i, (u64)buf->pt_cpu, (u64)*(buf->pt_cpu)); | ||
122 | |||
123 | } | ||
124 | |||
125 | goto ret; | ||
126 | |||
127 | fail2: | ||
128 | pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma); | ||
129 | fail1: | ||
130 | kfree(buf); | ||
131 | |||
132 | buf = 0; | ||
133 | ret: | ||
134 | return buf; | ||
135 | } | ||
136 | |||
137 | int saa7164_buffer_dealloc(struct saa7164_tsport *port, | ||
138 | struct saa7164_buffer *buf) | ||
139 | { | ||
140 | struct saa7164_dev *dev = port->dev; | ||
141 | |||
142 | if ((buf == 0) || (port == 0)) | ||
143 | return SAA_ERR_BAD_PARAMETER; | ||
144 | |||
145 | dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n", __func__, buf); | ||
146 | |||
147 | if (buf->flags != SAA7164_BUFFER_FREE) | ||
148 | log_warn(" freeing a non-free buffer\n"); | ||
149 | |||
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); | ||
153 | |||
154 | kfree(buf); | ||
155 | |||
156 | return SAA_OK; | ||
157 | } | ||
158 | |||